From 858fe116661c5b10ad1cef339ab4ead2ed2ab59e Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Tue, 19 Sep 2023 11:42:10 +0100 Subject: [PATCH 01/10] Fixed some diagnostics warnings Moved examples to tofix because fixing them is besides the point right now. --- .gitignore | 2 +- 06/Lexer.lua | 128 ------------------ 06/Makefile | 26 ++-- 06/README.md | 1 + 06/{ => deps}/lpeglabel/.gitignore | 1 + 06/{ => deps}/lpeglabel/HISTORY | 0 06/{ => deps}/lpeglabel/LICENSE | 0 06/{ => deps}/lpeglabel/README.md | 0 06/{ => deps}/lpeglabel/examples/expRec.lua | 4 +- .../lpeglabel/examples/expRecAut.lua | 4 +- 06/{ => deps}/lpeglabel/examples/farthest.lua | 0 06/{ => deps}/lpeglabel/examples/listId1.lua | 0 06/{ => deps}/lpeglabel/examples/listId2.lua | 0 .../lpeglabel/examples/listId2Rec2.lua | 0 .../lpeglabel/examples/listId2Rec2Cap.lua | 0 .../lpeglabel/examples/listIdRe1.lua | 0 .../lpeglabel/examples/listIdRe2.lua | 0 06/{ => deps}/lpeglabel/examples/tiny.lua | 0 .../lpeglabel/examples/typedlua/test.lua | 0 .../lpeglabel/examples/typedlua/tlerror.lua | 0 .../lpeglabel/examples/typedlua/tllexer.lua | 0 .../lpeglabel/examples/typedlua/tlp.lua | 0 .../lpeglabel/examples/typedlua/tlparser.lua | 0 06/{ => deps}/lpeglabel/lpcap.c | 0 06/{ => deps}/lpeglabel/lpcap.h | 0 06/{ => deps}/lpeglabel/lpcode.c | 0 06/{ => deps}/lpeglabel/lpcode.h | 0 06/{ => deps}/lpeglabel/lpeglabel-logo.png | Bin 06/{ => deps}/lpeglabel/lpprint.c | 0 06/{ => deps}/lpeglabel/lpprint.h | 0 06/{ => deps}/lpeglabel/lptree.c | 0 06/{ => deps}/lpeglabel/lptree.h | 0 06/{ => deps}/lpeglabel/lptypes.h | 0 06/{ => deps}/lpeglabel/lpvm.c | 0 06/{ => deps}/lpeglabel/lpvm.h | 0 06/{ => deps}/lpeglabel/makefile | 10 +- 06/{ => deps}/lpeglabel/relabel.lua | 3 +- .../rockspecs/lpeglabel-0.12.2-1.rockspec | 0 .../rockspecs/lpeglabel-0.12.2-2.rockspec | 0 .../rockspecs/lpeglabel-1.0.0-1.rockspec | 0 .../rockspecs/lpeglabel-1.1.0-1.rockspec | 0 .../rockspecs/lpeglabel-1.2.0-1.rockspec | 0 06/{ => deps}/lpeglabel/test.lua | 2 +- 06/{ => deps}/lpeglabel/testlabel.lua | 0 06/{ => deps}/lpeglabel/testrelabelparser.lua | 0 06/{ => deps}/lua-5.4.6/.gitignore | 0 06/{ => deps}/lua-5.4.6/Makefile | 0 06/{ => deps}/lua-5.4.6/README | 0 06/{ => deps}/lua-5.4.6/doc/contents.html | 0 06/{ => deps}/lua-5.4.6/doc/index.css | 0 06/{ => deps}/lua-5.4.6/doc/logo.gif | Bin 06/{ => deps}/lua-5.4.6/doc/lua.1 | 0 06/{ => deps}/lua-5.4.6/doc/lua.css | 0 06/{ => deps}/lua-5.4.6/doc/luac.1 | 0 06/{ => deps}/lua-5.4.6/doc/manual.css | 0 06/{ => deps}/lua-5.4.6/doc/manual.html | 0 .../lua-5.4.6/doc/osi-certified-72x60.png | Bin 06/{ => deps}/lua-5.4.6/doc/readme.html | 0 06/{ => deps}/lua-5.4.6/src/Makefile | 10 +- 06/{ => deps}/lua-5.4.6/src/lapi.c | 0 06/{ => deps}/lua-5.4.6/src/lapi.h | 0 06/{ => deps}/lua-5.4.6/src/lauxlib.c | 0 06/{ => deps}/lua-5.4.6/src/lauxlib.h | 0 06/{ => deps}/lua-5.4.6/src/lbaselib.c | 0 06/{ => deps}/lua-5.4.6/src/lcode.c | 0 06/{ => deps}/lua-5.4.6/src/lcode.h | 0 06/{ => deps}/lua-5.4.6/src/lcorolib.c | 0 06/{ => deps}/lua-5.4.6/src/lctype.c | 0 06/{ => deps}/lua-5.4.6/src/lctype.h | 0 06/{ => deps}/lua-5.4.6/src/ldblib.c | 0 06/{ => deps}/lua-5.4.6/src/ldebug.c | 0 06/{ => deps}/lua-5.4.6/src/ldebug.h | 0 06/{ => deps}/lua-5.4.6/src/ldo.c | 0 06/{ => deps}/lua-5.4.6/src/ldo.h | 0 06/{ => deps}/lua-5.4.6/src/ldump.c | 0 06/{ => deps}/lua-5.4.6/src/lfunc.c | 0 06/{ => deps}/lua-5.4.6/src/lfunc.h | 0 06/{ => deps}/lua-5.4.6/src/lgc.c | 0 06/{ => deps}/lua-5.4.6/src/lgc.h | 0 06/{ => deps}/lua-5.4.6/src/linit.c | 0 06/{ => deps}/lua-5.4.6/src/liolib.c | 0 06/{ => deps}/lua-5.4.6/src/ljumptab.h | 0 06/{ => deps}/lua-5.4.6/src/llex.c | 0 06/{ => deps}/lua-5.4.6/src/llex.h | 0 06/{ => deps}/lua-5.4.6/src/llimits.h | 0 06/{ => deps}/lua-5.4.6/src/lmathlib.c | 0 06/{ => deps}/lua-5.4.6/src/lmem.c | 0 06/{ => deps}/lua-5.4.6/src/lmem.h | 0 06/{ => deps}/lua-5.4.6/src/loadlib.c | 0 06/{ => deps}/lua-5.4.6/src/lobject.c | 0 06/{ => deps}/lua-5.4.6/src/lobject.h | 0 06/{ => deps}/lua-5.4.6/src/lopcodes.c | 0 06/{ => deps}/lua-5.4.6/src/lopcodes.h | 0 06/{ => deps}/lua-5.4.6/src/lopnames.h | 0 06/{ => deps}/lua-5.4.6/src/loslib.c | 0 06/{ => deps}/lua-5.4.6/src/lparser.c | 0 06/{ => deps}/lua-5.4.6/src/lparser.h | 0 06/{ => deps}/lua-5.4.6/src/lprefix.h | 0 06/{ => deps}/lua-5.4.6/src/lstate.c | 0 06/{ => deps}/lua-5.4.6/src/lstate.h | 0 06/{ => deps}/lua-5.4.6/src/lstring.c | 0 06/{ => deps}/lua-5.4.6/src/lstring.h | 0 06/{ => deps}/lua-5.4.6/src/lstrlib.c | 0 06/{ => deps}/lua-5.4.6/src/ltable.c | 0 06/{ => deps}/lua-5.4.6/src/ltable.h | 0 06/{ => deps}/lua-5.4.6/src/ltablib.c | 0 06/{ => deps}/lua-5.4.6/src/ltm.c | 0 06/{ => deps}/lua-5.4.6/src/ltm.h | 0 06/{ => deps}/lua-5.4.6/src/lua.c | 0 06/{ => deps}/lua-5.4.6/src/lua.h | 0 06/{ => deps}/lua-5.4.6/src/lua.hpp | 0 06/{ => deps}/lua-5.4.6/src/luac.c | 0 06/{ => deps}/lua-5.4.6/src/luaconf.h | 0 06/{ => deps}/lua-5.4.6/src/lualib.h | 0 06/{ => deps}/lua-5.4.6/src/lundump.c | 0 06/{ => deps}/lua-5.4.6/src/lundump.h | 0 06/{ => deps}/lua-5.4.6/src/lutf8lib.c | 0 06/{ => deps}/lua-5.4.6/src/lvm.c | 0 06/{ => deps}/lua-5.4.6/src/lvm.h | 0 06/{ => deps}/lua-5.4.6/src/lzio.c | 0 06/{ => deps}/lua-5.4.6/src/lzio.h | 0 06/{ => deps}/parser-gen/LICENSE | 0 06/{ => deps}/parser-gen/README.md | 0 06/{ => deps}/parser-gen/equals.lua | 0 06/{ => deps}/parser-gen/errorgen.lua | 0 06/{ => deps}/parser-gen/parser-gen-tests.lua | 29 ++-- 06/{ => deps}/parser-gen/parser-gen.lua | 25 ++-- .../parser-gen/parsers/lua-parser-tests.lua | 0 .../parser-gen/parsers/lua-parser.lua | 0 .../parser-gen/parsers/tiny-parser.lua | 0 06/{ => deps}/parser-gen/peg-parser-tests.lua | 2 +- 06/{ => deps}/parser-gen/peg-parser.lua | 2 +- .../rockspecs/parser-gen-1.0.rockspec | 0 .../rockspecs/parser-gen-1.1.rockspec | 0 .../rockspecs/parser-gen-1.2.rockspec | 0 06/{ => deps}/parser-gen/stack.lua | 3 +- 06/lpeglabel/old_makefile | 60 -------- 06/src/main.lua | 16 +-- .../parsers => tofix}/lua-5.3.4-tests/all.lua | 0 .../parsers => tofix}/lua-5.3.4-tests/api.lua | 1 + .../lua-5.3.4-tests/attrib.lua | 1 + .../parsers => tofix}/lua-5.3.4-tests/big.lua | 0 .../lua-5.3.4-tests/bitwise.lua | 0 .../lua-5.3.4-tests/calls.lua | 0 .../lua-5.3.4-tests/closure.lua | 0 .../lua-5.3.4-tests/code.lua | 0 .../lua-5.3.4-tests/constructs.lua | 0 .../lua-5.3.4-tests/coroutine.lua | 0 .../parsers => tofix}/lua-5.3.4-tests/db.lua | 0 .../lua-5.3.4-tests/errors.lua | 0 .../lua-5.3.4-tests/events.lua | 0 .../lua-5.3.4-tests/files.lua | 0 .../parsers => tofix}/lua-5.3.4-tests/gc.lua | 0 .../lua-5.3.4-tests/goto.lua | 2 +- .../lua-5.3.4-tests/literals.lua | 0 .../lua-5.3.4-tests/locals.lua | 0 .../lua-5.3.4-tests/main.lua | 0 .../lua-5.3.4-tests/math.lua | 0 .../lua-5.3.4-tests/nextvar.lua | 0 .../parsers => tofix}/lua-5.3.4-tests/pm.lua | 0 .../lua-5.3.4-tests/sort.lua | 0 .../lua-5.3.4-tests/strings.lua | 0 .../lua-5.3.4-tests/tpack.lua | 0 .../lua-5.3.4-tests/utf8.lua | 0 .../lua-5.3.4-tests/vararg.lua | 0 .../lua-5.3.4-tests/verybig.lua | 0 166 files changed, 68 insertions(+), 264 deletions(-) delete mode 100644 06/Lexer.lua rename 06/{ => deps}/lpeglabel/.gitignore (57%) rename 06/{ => deps}/lpeglabel/HISTORY (100%) rename 06/{ => deps}/lpeglabel/LICENSE (100%) rename 06/{ => deps}/lpeglabel/README.md (100%) rename 06/{ => deps}/lpeglabel/examples/expRec.lua (97%) rename 06/{ => deps}/lpeglabel/examples/expRecAut.lua (97%) rename 06/{ => deps}/lpeglabel/examples/farthest.lua (100%) rename 06/{ => deps}/lpeglabel/examples/listId1.lua (100%) rename 06/{ => deps}/lpeglabel/examples/listId2.lua (100%) rename 06/{ => deps}/lpeglabel/examples/listId2Rec2.lua (100%) rename 06/{ => deps}/lpeglabel/examples/listId2Rec2Cap.lua (100%) rename 06/{ => deps}/lpeglabel/examples/listIdRe1.lua (100%) rename 06/{ => deps}/lpeglabel/examples/listIdRe2.lua (100%) rename 06/{ => deps}/lpeglabel/examples/tiny.lua (100%) rename 06/{ => deps}/lpeglabel/examples/typedlua/test.lua (100%) rename 06/{ => deps}/lpeglabel/examples/typedlua/tlerror.lua (100%) rename 06/{ => deps}/lpeglabel/examples/typedlua/tllexer.lua (100%) rename 06/{ => deps}/lpeglabel/examples/typedlua/tlp.lua (100%) rename 06/{ => deps}/lpeglabel/examples/typedlua/tlparser.lua (100%) rename 06/{ => deps}/lpeglabel/lpcap.c (100%) rename 06/{ => deps}/lpeglabel/lpcap.h (100%) rename 06/{ => deps}/lpeglabel/lpcode.c (100%) rename 06/{ => deps}/lpeglabel/lpcode.h (100%) rename 06/{ => deps}/lpeglabel/lpeglabel-logo.png (100%) rename 06/{ => deps}/lpeglabel/lpprint.c (100%) rename 06/{ => deps}/lpeglabel/lpprint.h (100%) rename 06/{ => deps}/lpeglabel/lptree.c (100%) rename 06/{ => deps}/lpeglabel/lptree.h (100%) rename 06/{ => deps}/lpeglabel/lptypes.h (100%) rename 06/{ => deps}/lpeglabel/lpvm.c (100%) rename 06/{ => deps}/lpeglabel/lpvm.h (100%) rename 06/{ => deps}/lpeglabel/makefile (76%) rename 06/{ => deps}/lpeglabel/relabel.lua (98%) rename 06/{ => deps}/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec (100%) rename 06/{ => deps}/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec (100%) rename 06/{ => deps}/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec (100%) rename 06/{ => deps}/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec (100%) rename 06/{ => deps}/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec (100%) rename 06/{ => deps}/lpeglabel/test.lua (99%) rename 06/{ => deps}/lpeglabel/testlabel.lua (100%) rename 06/{ => deps}/lpeglabel/testrelabelparser.lua (100%) rename 06/{ => deps}/lua-5.4.6/.gitignore (100%) rename 06/{ => deps}/lua-5.4.6/Makefile (100%) rename 06/{ => deps}/lua-5.4.6/README (100%) rename 06/{ => deps}/lua-5.4.6/doc/contents.html (100%) rename 06/{ => deps}/lua-5.4.6/doc/index.css (100%) rename 06/{ => deps}/lua-5.4.6/doc/logo.gif (100%) rename 06/{ => deps}/lua-5.4.6/doc/lua.1 (100%) rename 06/{ => deps}/lua-5.4.6/doc/lua.css (100%) rename 06/{ => deps}/lua-5.4.6/doc/luac.1 (100%) rename 06/{ => deps}/lua-5.4.6/doc/manual.css (100%) rename 06/{ => deps}/lua-5.4.6/doc/manual.html (100%) rename 06/{ => deps}/lua-5.4.6/doc/osi-certified-72x60.png (100%) rename 06/{ => deps}/lua-5.4.6/doc/readme.html (100%) rename 06/{ => deps}/lua-5.4.6/src/Makefile (95%) rename 06/{ => deps}/lua-5.4.6/src/lapi.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lapi.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lauxlib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lauxlib.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lbaselib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lcode.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lcode.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lcorolib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lctype.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lctype.h (100%) rename 06/{ => deps}/lua-5.4.6/src/ldblib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ldebug.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ldebug.h (100%) rename 06/{ => deps}/lua-5.4.6/src/ldo.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ldo.h (100%) rename 06/{ => deps}/lua-5.4.6/src/ldump.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lfunc.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lfunc.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lgc.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lgc.h (100%) rename 06/{ => deps}/lua-5.4.6/src/linit.c (100%) rename 06/{ => deps}/lua-5.4.6/src/liolib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ljumptab.h (100%) rename 06/{ => deps}/lua-5.4.6/src/llex.c (100%) rename 06/{ => deps}/lua-5.4.6/src/llex.h (100%) rename 06/{ => deps}/lua-5.4.6/src/llimits.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lmathlib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lmem.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lmem.h (100%) rename 06/{ => deps}/lua-5.4.6/src/loadlib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lobject.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lobject.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lopcodes.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lopcodes.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lopnames.h (100%) rename 06/{ => deps}/lua-5.4.6/src/loslib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lparser.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lparser.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lprefix.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lstate.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lstate.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lstring.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lstring.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lstrlib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ltable.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ltable.h (100%) rename 06/{ => deps}/lua-5.4.6/src/ltablib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ltm.c (100%) rename 06/{ => deps}/lua-5.4.6/src/ltm.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lua.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lua.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lua.hpp (100%) rename 06/{ => deps}/lua-5.4.6/src/luac.c (100%) rename 06/{ => deps}/lua-5.4.6/src/luaconf.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lualib.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lundump.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lundump.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lutf8lib.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lvm.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lvm.h (100%) rename 06/{ => deps}/lua-5.4.6/src/lzio.c (100%) rename 06/{ => deps}/lua-5.4.6/src/lzio.h (100%) rename 06/{ => deps}/parser-gen/LICENSE (100%) rename 06/{ => deps}/parser-gen/README.md (100%) rename 06/{ => deps}/parser-gen/equals.lua (100%) rename 06/{ => deps}/parser-gen/errorgen.lua (100%) rename 06/{ => deps}/parser-gen/parser-gen-tests.lua (85%) rename 06/{ => deps}/parser-gen/parser-gen.lua (97%) rename 06/{ => deps}/parser-gen/parsers/lua-parser-tests.lua (100%) rename 06/{ => deps}/parser-gen/parsers/lua-parser.lua (100%) rename 06/{ => deps}/parser-gen/parsers/tiny-parser.lua (100%) rename 06/{ => deps}/parser-gen/peg-parser-tests.lua (99%) rename 06/{ => deps}/parser-gen/peg-parser.lua (99%) rename 06/{ => deps}/parser-gen/rockspecs/parser-gen-1.0.rockspec (100%) rename 06/{ => deps}/parser-gen/rockspecs/parser-gen-1.1.rockspec (100%) rename 06/{ => deps}/parser-gen/rockspecs/parser-gen-1.2.rockspec (100%) rename 06/{ => deps}/parser-gen/stack.lua (95%) delete mode 100644 06/lpeglabel/old_makefile rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/all.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/api.lua (99%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/attrib.lua (99%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/big.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/bitwise.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/calls.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/closure.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/code.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/constructs.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/coroutine.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/db.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/errors.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/events.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/files.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/gc.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/goto.lua (98%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/literals.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/locals.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/main.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/math.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/nextvar.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/pm.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/sort.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/strings.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/tpack.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/utf8.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/vararg.lua (100%) rename {06/parser-gen/parsers => tofix}/lua-5.3.4-tests/verybig.lua (100%) diff --git a/.gitignore b/.gitignore index e920e17..c9a2d35 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ out??? *.out tags TAGS -markdown +markdown \ No newline at end of file diff --git a/06/Lexer.lua b/06/Lexer.lua deleted file mode 100644 index b7051c1..0000000 --- a/06/Lexer.lua +++ /dev/null @@ -1,128 +0,0 @@ -Reverse = { - "addrspace", "align", "allowzero", "and", "anyframe", "anytype", "asm", "async", - "await", "break", "callconv", "catch", "comptime", "const", "continue", "defer", - "else", "enum", "errdefer", "error", "export", "extern", "fn", "for", "if", - "inline", "noalias", "noinline", "IDENTIFIER", "nosuspend", "opaque", "or", - "orelse", "packed", "pub", "resume", "return", "linksection", "struct", "suspend", - "switch", "test", "threadlocal", "try", "union", "unreachable", "usingnamespace", - "var", "volatile", "while", "INVALID", ".**", "!", "|", "||", "|=", "=", "==", - "=>", "!=", "(", ")", ";", "%", "%=", "{", "}", "[", "]", ".", ".*", "..", "...", - "^", "^=", "+", "++", "+=", "+%", "+%=", "+|", "+|=", "-", "-=", "-%", "-%=", "-|", - "-|=", "*", "*=", "**", "*%", "*%=", "*|", "*|=", "->", ":", "/", "/=", ",", "&", - "&=", "?", "<", "<=", "<<", "<<=", "<<|", "<<|=", ">", ">=", ">>", ">>=", "~", - "STRING_LITERAL", "CHAR_LITERAL", "EOF", "BUILTIN", "NUMBER_LITERAL", "DOC_COMMENT", - "CONTAINER_DOC_COMMENT", -} - -Tokens = { - ADDRSPACE = 1, ALIGN = 2, - ALLOWZERO = 3, AND = 4, - ANYFRAME = 5, ANYTYPE = 6, - ASM = 7, ASYNC = 8, - AWAIT = 9, BREAK = 10, - CALLCONV = 11, CATCH = 12, - COMPTIME = 13, CONST = 14, - CONTINUE = 15, DEFER = 16, - ELSE = 17, ENUM = 18, - ERRDEFER = 19, ERROR = 20, - EXPORT = 21, EXTERN = 22, - FN = 23, FOR = 24, - IF = 25, INLINE = 26, - NOALIAS = 27, NOINLINE = 28, - IDENTIFIER = 29, NOSUSPEND = 30, - OPAQUE = 31, OR = 32, - ORELSE = 33, PACKED = 34, - PUB = 35, RESUME = 36, - RETURN = 37, LINKSECTION = 38, - STRUCT = 39, SUSPEND = 40, - SWITCH = 41, TEST = 42, - THREADLOCAL = 43, TRY = 44, - UNION = 45, UNREACHABLE = 46, - USINGNAMESPACE = 47, VAR = 48, - VOLATILE = 49, WHILE = 50, - INVALID = 51, INVALID_PERIODASTERISKS = 52, - BANG = 53, PIPE = 54, - PIPE_PIPE = 55, PIPE_EQUAL = 56, - EQUAL = 57, EQUAL_EQUAL = 58, - EQUAL_ANGLE_BRACKET_RIGHT = 59, BANG_EQUAL = 60, - L_PAREN = 61, R_PAREN = 62, - SEMICOLON = 63, PERCENT = 64, - PERCENT_EQUAL = 65, L_BRACE = 66, - R_BRACE = 67, L_BRACKET = 68, - R_BRACKET = 69, PERIOD = 70, - PERIOD_ASTERISK = 71, ELLIPSIS2 = 72, - ELLIPSIS3 = 73, CARET = 74, - CARET_EQUAL = 75, PLUS = 76, - PLUS_PLUS = 77, PLUS_EQUAL = 78, - PLUS_PERCENT = 79, PLUS_PERCENT_EQUAL = 80, - PLUS_PIPE = 81, PLUS_PIPE_EQUAL = 82, - MINUS = 83, MINUS_EQUAL = 84, - MINUS_PERCENT = 85, MINUS_PERCENT_EQUAL = 86, - MINUS_PIPE = 87, MINUS_PIPE_EQUAL = 88, - ASTERISK = 89, ASTERISK_EQUAL = 90, - ASTERISK_ASTERISK = 91, ASTERISK_PERCENT = 92, - ASTERISK_PERCENT_EQUAL = 93, ASTERISK_PIPE = 94, - ASTERISK_PIPE_EQUAL = 95, ARROW = 96, - COLON = 97, SLASH = 98, - SLASH_EQUAL = 99, COMMA = 100, - AMPERSAND = 101, AMPERSAND_EQUAL = 102, - QUESTION_MARK = 103, ANGLE_BRACKET_LEFT = 104, - ANGLE_BRACKET_LEFT_EQUAL = 105, ANGLE_BRACKET_ANGLE_BRACKET_LEFT = 106, - ANGLE_BRACKET_ANGLE_BRACKET_LEFT_EQUAL = 107, ANGLE_BRACKET_ANGLE_BRACKET_LEFT_PIPE = 108, - ANGLE_BRACKET_ANGLE_BRACKET_LEFT_PIPE_EQUAL = 109, ANGLE_BRACKET_RIGHT = 110, - ANGLE_BRACKET_RIGHT_EQUAL = 111, ANGLE_BRACKET_ANGLE_BRACKET_RIGHT = 112, - ANGLE_BRACKET_ANGLE_BRACKET_RIGHT_EQUAL = 113, TILDE = 114, - STRING_LITERAL = 115, CHAR_LITERAL = 116, - EOF = 117, BUILTIN = 118, - NUMBER_LITERAL = 119, DOC_COMMENT = 120, - CONTAINER_DOC_COMMENT = 121, -} - - -State = { - START = 1, IDENTIFIER = 2, - BUILTIN = 3, STRING_LITERAL = 5, - STRING_LITERAL_BACKSLASH = 6, MULTILINE_STRING_LITERAL_LINE = 7, - CHAR_LITERAL = 8, CHAR_LITERAL_BACKSLASH = 9, - CHAR_LITERAL_HEX_ESCAPE = 10, CHAR_LITERAL_UNICODE_ESCAPE_SAW_U = 11, - CHAR_LITERAL_UNICODE_ESCAPE = 12, CHAR_LITERAL_UNICODE_INVALID = 13, - CHAR_LITERAL_UNICODE = 14, CHAR_LITERAL_END = 15, - BACKSLASH = 16, EQUAL = 17, - BANG = 18, PIPE = 19, - MINUS = 20, MINUS_PERCENT = 21, - MINUS_PIPE = 22, ASTERISK = 23, - ASTERISK_PERCENT = 24, ASTERISK_PIPE = 25, - SLASH = 26, LINE_COMMENT_START = 27, - LINE_COMMENT = 28, DOC_COMMENT_START = 29, - DOC_COMMENT = 30, INT = 31, - INT_EXPONENT = 32, INT_PERIOD = 33, - FLOAT = 34, FLOAT_EXPONENT = 35, - AMPERSAND = 36, CARET = 37, - PERCENT = 38, PLUS = 39, - PLUS_PERCENT = 40, PLUS_PIPE = 41, - ANGLE_BRACKET_LEFT = 42, ANGLE_BRACKET_ANGLE_BRACKET_LEFT = 43, - ANGLE_BRACKET_ANGLE_BRACKET_LEFT_PIPE = 44, ANGLE_BRACKET_RIGHT = 45, - ANGLE_BRACKET_ANGLE_BRACKET_RIGHT = 46, PERIOD = 47, - PERIOD_2 = 48, PERIOD_ASTERISK = 49, - SAW_AT_SIGN = 50, -} - -Lexer = { - Reverse, - Tokens, - State, - x = 0, -} - -function Lexer:new() - local l = {} - setmetatable(l, self) - self.__index = self - return l -end - -function Lexer:lex(char) - -end - -return Lexer \ No newline at end of file diff --git a/06/Makefile b/06/Makefile index e770cd4..6d58744 100644 --- a/06/Makefile +++ b/06/Makefile @@ -1,23 +1,26 @@ CC= ../05/tcc-0.9.27/tcc TCCINST= ../05/tcc-bootstrap MUSLINST= ../05/musl-bootstrap -LUAINST= lua-bootstrap +LUAINST= deps/lua-bootstrap -CFLAGS= -I $(MUSLINST)/include -I $(LUAINST)/include -I lpeglabel +CFLAGS= -I $(MUSLINST)/include -I $(LUAINST)/include -I deps/lpeglabel SRCS = $(sort $(wildcard **/*.c)) OBJS = $(SRCS:.c=.o) all: zsh -lua-bootstrap/lib/liblua.a: - $(MAKE) -j8 -C lua-5.4.6 - $(MAKE) -C lua-5.4.6 install +deps/lua-bootstrap/lib/liblua.a: + $(MAKE) -j8 -C deps/lua-5.4.6 + $(MAKE) -C deps/lua-5.4.6 install -%.o: %.c lua-bootstrap/lib/liblua.a +deps/lpeglabel/lpeglabel.a: deps/lua-bootstrap/lib/liblua.a + $(MAKE) -C deps/lpeglabel + +%.o: %.c deps/lua-bootstrap/lib/liblua.a $(CC) $(CFLAGS) -c -o $@ $< -zsh: $(OBJS) lua-bootstrap/lib/liblua.a - $(CC) -nostdlib -B $(TCCINST) -o zsh $(OBJS) $(LUAINST)/lib/liblua.a $(MUSLINST)/lib/*.[oa] +zsh: $(OBJS) deps/lua-bootstrap/lib/liblua.a deps/lpeglabel/lpeglabel.a + $(CC) -nostdlib -B $(TCCINST) -o zsh $(OBJS) deps/lpeglabel/lpeglabel.a $(LUAINST)/lib/liblua.a $(MUSLINST)/lib/*.[oa] run: zsh ./zsh @@ -25,9 +28,10 @@ run: zsh c: rm -f zsh rm -f src/*.o - rm -f lpeglabel/*.o + rm -f deps/lpeglabel/*.o clean: c - rm -rf lua-bootstrap - $(MAKE) -C lua-5.4.6 clean + rm -rf deps/lua-bootstrap + $(MAKE) -C deps/lua-5.4.6 clean + $(MAKE) -C deps/lpeglabel clean diff --git a/06/README.md b/06/README.md index 85a97a8..319565c 100644 --- a/06/README.md +++ b/06/README.md @@ -14,3 +14,4 @@ Implement backslashed escaped characters in grammar parser Implement single line comments starting with # in grammar parser + diff --git a/06/lpeglabel/.gitignore b/06/deps/lpeglabel/.gitignore similarity index 57% rename from 06/lpeglabel/.gitignore rename to 06/deps/lpeglabel/.gitignore index 8ddf7f8..0e425ae 100644 --- a/06/lpeglabel/.gitignore +++ b/06/deps/lpeglabel/.gitignore @@ -1,2 +1,3 @@ lpeglabel.so +lpeglabel.a *.o \ No newline at end of file diff --git a/06/lpeglabel/HISTORY b/06/deps/lpeglabel/HISTORY similarity index 100% rename from 06/lpeglabel/HISTORY rename to 06/deps/lpeglabel/HISTORY diff --git a/06/lpeglabel/LICENSE b/06/deps/lpeglabel/LICENSE similarity index 100% rename from 06/lpeglabel/LICENSE rename to 06/deps/lpeglabel/LICENSE diff --git a/06/lpeglabel/README.md b/06/deps/lpeglabel/README.md similarity index 100% rename from 06/lpeglabel/README.md rename to 06/deps/lpeglabel/README.md diff --git a/06/lpeglabel/examples/expRec.lua b/06/deps/lpeglabel/examples/expRec.lua similarity index 97% rename from 06/lpeglabel/examples/expRec.lua rename to 06/deps/lpeglabel/examples/expRec.lua index 52bd40a..f70b581 100644 --- a/06/lpeglabel/examples/expRec.lua +++ b/06/deps/lpeglabel/examples/expRec.lua @@ -1,5 +1,5 @@ -local m = require"lpeglabel" -local re = require"relabel" +local m = require "lpeglabel" +local re = require "relabel" local labels = { {"ExpTermFirst", "expected an expression"}, diff --git a/06/lpeglabel/examples/expRecAut.lua b/06/deps/lpeglabel/examples/expRecAut.lua similarity index 97% rename from 06/lpeglabel/examples/expRecAut.lua rename to 06/deps/lpeglabel/examples/expRecAut.lua index b8280a7..c3d7efb 100644 --- a/06/lpeglabel/examples/expRecAut.lua +++ b/06/deps/lpeglabel/examples/expRecAut.lua @@ -1,5 +1,5 @@ -local m = require"lpeglabel" -local re = require"relabel" +local m = require "lpeglabel" +local re = require "relabel" local num = m.R("09")^1 / tonumber local op = m.S("+-") diff --git a/06/lpeglabel/examples/farthest.lua b/06/deps/lpeglabel/examples/farthest.lua similarity index 100% rename from 06/lpeglabel/examples/farthest.lua rename to 06/deps/lpeglabel/examples/farthest.lua diff --git a/06/lpeglabel/examples/listId1.lua b/06/deps/lpeglabel/examples/listId1.lua similarity index 100% rename from 06/lpeglabel/examples/listId1.lua rename to 06/deps/lpeglabel/examples/listId1.lua diff --git a/06/lpeglabel/examples/listId2.lua b/06/deps/lpeglabel/examples/listId2.lua similarity index 100% rename from 06/lpeglabel/examples/listId2.lua rename to 06/deps/lpeglabel/examples/listId2.lua diff --git a/06/lpeglabel/examples/listId2Rec2.lua b/06/deps/lpeglabel/examples/listId2Rec2.lua similarity index 100% rename from 06/lpeglabel/examples/listId2Rec2.lua rename to 06/deps/lpeglabel/examples/listId2Rec2.lua diff --git a/06/lpeglabel/examples/listId2Rec2Cap.lua b/06/deps/lpeglabel/examples/listId2Rec2Cap.lua similarity index 100% rename from 06/lpeglabel/examples/listId2Rec2Cap.lua rename to 06/deps/lpeglabel/examples/listId2Rec2Cap.lua diff --git a/06/lpeglabel/examples/listIdRe1.lua b/06/deps/lpeglabel/examples/listIdRe1.lua similarity index 100% rename from 06/lpeglabel/examples/listIdRe1.lua rename to 06/deps/lpeglabel/examples/listIdRe1.lua diff --git a/06/lpeglabel/examples/listIdRe2.lua b/06/deps/lpeglabel/examples/listIdRe2.lua similarity index 100% rename from 06/lpeglabel/examples/listIdRe2.lua rename to 06/deps/lpeglabel/examples/listIdRe2.lua diff --git a/06/lpeglabel/examples/tiny.lua b/06/deps/lpeglabel/examples/tiny.lua similarity index 100% rename from 06/lpeglabel/examples/tiny.lua rename to 06/deps/lpeglabel/examples/tiny.lua diff --git a/06/lpeglabel/examples/typedlua/test.lua b/06/deps/lpeglabel/examples/typedlua/test.lua similarity index 100% rename from 06/lpeglabel/examples/typedlua/test.lua rename to 06/deps/lpeglabel/examples/typedlua/test.lua diff --git a/06/lpeglabel/examples/typedlua/tlerror.lua b/06/deps/lpeglabel/examples/typedlua/tlerror.lua similarity index 100% rename from 06/lpeglabel/examples/typedlua/tlerror.lua rename to 06/deps/lpeglabel/examples/typedlua/tlerror.lua diff --git a/06/lpeglabel/examples/typedlua/tllexer.lua b/06/deps/lpeglabel/examples/typedlua/tllexer.lua similarity index 100% rename from 06/lpeglabel/examples/typedlua/tllexer.lua rename to 06/deps/lpeglabel/examples/typedlua/tllexer.lua diff --git a/06/lpeglabel/examples/typedlua/tlp.lua b/06/deps/lpeglabel/examples/typedlua/tlp.lua similarity index 100% rename from 06/lpeglabel/examples/typedlua/tlp.lua rename to 06/deps/lpeglabel/examples/typedlua/tlp.lua diff --git a/06/lpeglabel/examples/typedlua/tlparser.lua b/06/deps/lpeglabel/examples/typedlua/tlparser.lua similarity index 100% rename from 06/lpeglabel/examples/typedlua/tlparser.lua rename to 06/deps/lpeglabel/examples/typedlua/tlparser.lua diff --git a/06/lpeglabel/lpcap.c b/06/deps/lpeglabel/lpcap.c similarity index 100% rename from 06/lpeglabel/lpcap.c rename to 06/deps/lpeglabel/lpcap.c diff --git a/06/lpeglabel/lpcap.h b/06/deps/lpeglabel/lpcap.h similarity index 100% rename from 06/lpeglabel/lpcap.h rename to 06/deps/lpeglabel/lpcap.h diff --git a/06/lpeglabel/lpcode.c b/06/deps/lpeglabel/lpcode.c similarity index 100% rename from 06/lpeglabel/lpcode.c rename to 06/deps/lpeglabel/lpcode.c diff --git a/06/lpeglabel/lpcode.h b/06/deps/lpeglabel/lpcode.h similarity index 100% rename from 06/lpeglabel/lpcode.h rename to 06/deps/lpeglabel/lpcode.h diff --git a/06/lpeglabel/lpeglabel-logo.png b/06/deps/lpeglabel/lpeglabel-logo.png similarity index 100% rename from 06/lpeglabel/lpeglabel-logo.png rename to 06/deps/lpeglabel/lpeglabel-logo.png diff --git a/06/lpeglabel/lpprint.c b/06/deps/lpeglabel/lpprint.c similarity index 100% rename from 06/lpeglabel/lpprint.c rename to 06/deps/lpeglabel/lpprint.c diff --git a/06/lpeglabel/lpprint.h b/06/deps/lpeglabel/lpprint.h similarity index 100% rename from 06/lpeglabel/lpprint.h rename to 06/deps/lpeglabel/lpprint.h diff --git a/06/lpeglabel/lptree.c b/06/deps/lpeglabel/lptree.c similarity index 100% rename from 06/lpeglabel/lptree.c rename to 06/deps/lpeglabel/lptree.c diff --git a/06/lpeglabel/lptree.h b/06/deps/lpeglabel/lptree.h similarity index 100% rename from 06/lpeglabel/lptree.h rename to 06/deps/lpeglabel/lptree.h diff --git a/06/lpeglabel/lptypes.h b/06/deps/lpeglabel/lptypes.h similarity index 100% rename from 06/lpeglabel/lptypes.h rename to 06/deps/lpeglabel/lptypes.h diff --git a/06/lpeglabel/lpvm.c b/06/deps/lpeglabel/lpvm.c similarity index 100% rename from 06/lpeglabel/lpvm.c rename to 06/deps/lpeglabel/lpvm.c diff --git a/06/lpeglabel/lpvm.h b/06/deps/lpeglabel/lpvm.h similarity index 100% rename from 06/lpeglabel/lpvm.h rename to 06/deps/lpeglabel/lpvm.h diff --git a/06/lpeglabel/makefile b/06/deps/lpeglabel/makefile similarity index 76% rename from 06/lpeglabel/makefile rename to 06/deps/lpeglabel/makefile index 31b3e14..d0c67c9 100644 --- a/06/lpeglabel/makefile +++ b/06/deps/lpeglabel/makefile @@ -22,21 +22,21 @@ CWARNS = -Wall -Wextra -pedantic \ # -Wunreachable-code \ -CFLAGS = $(CWARNS) -nostdinc -B ../../05/tcc-bootstrap -I ../../05/musl-bootstrap/include -I$(LUADIR) -CC = ../../05/tcc-0.9.27/tcc +CFLAGS = $(CWARNS) -nostdinc -B ../../../05/tcc-bootstrap -I ../../../05/musl-bootstrap/include -I$(LUADIR) +CC = ../../../05/tcc-0.9.27/tcc FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o # For Linux linux: - make lpeglabel.so "DLLFLAGS = -shared -nostdlib -B ../../05/tcc-bootstrap " + make lpeglabel.a # # For Mac OS # macosx: # make lpeglabel.so "DLLFLAGS = -bundle -undefined dynamic_lookup" -lpeglabel.so: $(FILES) - $(CC) $(DLLFLAGS) $(FILES) -o lpeglabel.so ../lua-bootstrap/lib/liblua.a ../../05/musl-bootstrap/lib/libc.a +lpeglabel.a: $(FILES) + $(CC) -ar c lpeglabel.a $(FILES) $(FILES): makefile diff --git a/06/lpeglabel/relabel.lua b/06/deps/lpeglabel/relabel.lua similarity index 98% rename from 06/lpeglabel/relabel.lua rename to 06/deps/lpeglabel/relabel.lua index 1089d34..33a332c 100644 --- a/06/lpeglabel/relabel.lua +++ b/06/deps/lpeglabel/relabel.lua @@ -4,8 +4,9 @@ local tonumber, type, print, error, ipairs = tonumber, type, print, error, ipairs local pcall = pcall local setmetatable = setmetatable -local unpack, tinsert, concat = table.unpack or unpack, table.insert, table.concat +local unpack, tinsert, concat = table.unpack, table.insert, table.concat local rep = string.rep +-- m is defined in main.c and is equivalent to require ('lpeglabel' local m = m -- 'm' will be used to parse expressions, and 'mm' will be used to diff --git a/06/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec similarity index 100% rename from 06/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec rename to 06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec diff --git a/06/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec similarity index 100% rename from 06/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec rename to 06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec diff --git a/06/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec similarity index 100% rename from 06/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec rename to 06/deps/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec diff --git a/06/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec similarity index 100% rename from 06/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec rename to 06/deps/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec diff --git a/06/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec similarity index 100% rename from 06/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec rename to 06/deps/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec diff --git a/06/lpeglabel/test.lua b/06/deps/lpeglabel/test.lua similarity index 99% rename from 06/lpeglabel/test.lua rename to 06/deps/lpeglabel/test.lua index d5922ac..b985126 100755 --- a/06/lpeglabel/test.lua +++ b/06/deps/lpeglabel/test.lua @@ -12,7 +12,7 @@ local a, b, c, d, e, f, g, p, t -- compatibility with Lua 5.2 -local unpack = rawget(table, "unpack") or unpack +local unpack = rawget(table, "unpack") local loadstring = rawget(_G, "loadstring") or load diff --git a/06/lpeglabel/testlabel.lua b/06/deps/lpeglabel/testlabel.lua similarity index 100% rename from 06/lpeglabel/testlabel.lua rename to 06/deps/lpeglabel/testlabel.lua diff --git a/06/lpeglabel/testrelabelparser.lua b/06/deps/lpeglabel/testrelabelparser.lua similarity index 100% rename from 06/lpeglabel/testrelabelparser.lua rename to 06/deps/lpeglabel/testrelabelparser.lua diff --git a/06/lua-5.4.6/.gitignore b/06/deps/lua-5.4.6/.gitignore similarity index 100% rename from 06/lua-5.4.6/.gitignore rename to 06/deps/lua-5.4.6/.gitignore diff --git a/06/lua-5.4.6/Makefile b/06/deps/lua-5.4.6/Makefile similarity index 100% rename from 06/lua-5.4.6/Makefile rename to 06/deps/lua-5.4.6/Makefile diff --git a/06/lua-5.4.6/README b/06/deps/lua-5.4.6/README similarity index 100% rename from 06/lua-5.4.6/README rename to 06/deps/lua-5.4.6/README diff --git a/06/lua-5.4.6/doc/contents.html b/06/deps/lua-5.4.6/doc/contents.html similarity index 100% rename from 06/lua-5.4.6/doc/contents.html rename to 06/deps/lua-5.4.6/doc/contents.html diff --git a/06/lua-5.4.6/doc/index.css b/06/deps/lua-5.4.6/doc/index.css similarity index 100% rename from 06/lua-5.4.6/doc/index.css rename to 06/deps/lua-5.4.6/doc/index.css diff --git a/06/lua-5.4.6/doc/logo.gif b/06/deps/lua-5.4.6/doc/logo.gif similarity index 100% rename from 06/lua-5.4.6/doc/logo.gif rename to 06/deps/lua-5.4.6/doc/logo.gif diff --git a/06/lua-5.4.6/doc/lua.1 b/06/deps/lua-5.4.6/doc/lua.1 similarity index 100% rename from 06/lua-5.4.6/doc/lua.1 rename to 06/deps/lua-5.4.6/doc/lua.1 diff --git a/06/lua-5.4.6/doc/lua.css b/06/deps/lua-5.4.6/doc/lua.css similarity index 100% rename from 06/lua-5.4.6/doc/lua.css rename to 06/deps/lua-5.4.6/doc/lua.css diff --git a/06/lua-5.4.6/doc/luac.1 b/06/deps/lua-5.4.6/doc/luac.1 similarity index 100% rename from 06/lua-5.4.6/doc/luac.1 rename to 06/deps/lua-5.4.6/doc/luac.1 diff --git a/06/lua-5.4.6/doc/manual.css b/06/deps/lua-5.4.6/doc/manual.css similarity index 100% rename from 06/lua-5.4.6/doc/manual.css rename to 06/deps/lua-5.4.6/doc/manual.css diff --git a/06/lua-5.4.6/doc/manual.html b/06/deps/lua-5.4.6/doc/manual.html similarity index 100% rename from 06/lua-5.4.6/doc/manual.html rename to 06/deps/lua-5.4.6/doc/manual.html diff --git a/06/lua-5.4.6/doc/osi-certified-72x60.png b/06/deps/lua-5.4.6/doc/osi-certified-72x60.png similarity index 100% rename from 06/lua-5.4.6/doc/osi-certified-72x60.png rename to 06/deps/lua-5.4.6/doc/osi-certified-72x60.png diff --git a/06/lua-5.4.6/doc/readme.html b/06/deps/lua-5.4.6/doc/readme.html similarity index 100% rename from 06/lua-5.4.6/doc/readme.html rename to 06/deps/lua-5.4.6/doc/readme.html diff --git a/06/lua-5.4.6/src/Makefile b/06/deps/lua-5.4.6/src/Makefile similarity index 95% rename from 06/lua-5.4.6/src/Makefile rename to 06/deps/lua-5.4.6/src/Makefile index 694a3fb..bc879e5 100644 --- a/06/lua-5.4.6/src/Makefile +++ b/06/deps/lua-5.4.6/src/Makefile @@ -6,12 +6,12 @@ # Your platform. See PLATS for possible values. PLAT= linux -CC= ../../../05/tcc-0.9.27/tcc -CFLAGS= -nostdinc -B ../../../05/tcc-boostrap -I ../../../05/musl-bootstrap/include $(SYSCFLAGS) $(MYCFLAGS) +CC= ../../../../05/tcc-0.9.27/tcc +CFLAGS= -nostdinc -B ../../../../05/tcc-boostrap -I ../../../../05/musl-bootstrap/include $(SYSCFLAGS) $(MYCFLAGS) LDFLAGS= -nostdlib $(SYSLDFLAGS) $(MYLDFLAGS) LIBS= $(SYSLIBS) $(MYLIBS) -AR= ../../../05/tcc-0.9.27/tcc -ar rc +AR= ../../../../05/tcc-0.9.27/tcc -ar rc RANLIB= ranlib RM= rm -f UNAME= uname @@ -61,10 +61,10 @@ $(LUA_A): $(BASE_O) $(RANLIB) $@ $(LUA_T): $(LUA_O) $(LUA_A) - $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) ../../../05/musl-bootstrap/lib/*.[oa] + $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) ../../../../05/musl-bootstrap/lib/*.[oa] $(LUAC_T): $(LUAC_O) $(LUA_A) - $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) ../../../05/musl-bootstrap/lib/*.[oa] + $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) ../../../../05/musl-bootstrap/lib/*.[oa] test: ./$(LUA_T) -v diff --git a/06/lua-5.4.6/src/lapi.c b/06/deps/lua-5.4.6/src/lapi.c similarity index 100% rename from 06/lua-5.4.6/src/lapi.c rename to 06/deps/lua-5.4.6/src/lapi.c diff --git a/06/lua-5.4.6/src/lapi.h b/06/deps/lua-5.4.6/src/lapi.h similarity index 100% rename from 06/lua-5.4.6/src/lapi.h rename to 06/deps/lua-5.4.6/src/lapi.h diff --git a/06/lua-5.4.6/src/lauxlib.c b/06/deps/lua-5.4.6/src/lauxlib.c similarity index 100% rename from 06/lua-5.4.6/src/lauxlib.c rename to 06/deps/lua-5.4.6/src/lauxlib.c diff --git a/06/lua-5.4.6/src/lauxlib.h b/06/deps/lua-5.4.6/src/lauxlib.h similarity index 100% rename from 06/lua-5.4.6/src/lauxlib.h rename to 06/deps/lua-5.4.6/src/lauxlib.h diff --git a/06/lua-5.4.6/src/lbaselib.c b/06/deps/lua-5.4.6/src/lbaselib.c similarity index 100% rename from 06/lua-5.4.6/src/lbaselib.c rename to 06/deps/lua-5.4.6/src/lbaselib.c diff --git a/06/lua-5.4.6/src/lcode.c b/06/deps/lua-5.4.6/src/lcode.c similarity index 100% rename from 06/lua-5.4.6/src/lcode.c rename to 06/deps/lua-5.4.6/src/lcode.c diff --git a/06/lua-5.4.6/src/lcode.h b/06/deps/lua-5.4.6/src/lcode.h similarity index 100% rename from 06/lua-5.4.6/src/lcode.h rename to 06/deps/lua-5.4.6/src/lcode.h diff --git a/06/lua-5.4.6/src/lcorolib.c b/06/deps/lua-5.4.6/src/lcorolib.c similarity index 100% rename from 06/lua-5.4.6/src/lcorolib.c rename to 06/deps/lua-5.4.6/src/lcorolib.c diff --git a/06/lua-5.4.6/src/lctype.c b/06/deps/lua-5.4.6/src/lctype.c similarity index 100% rename from 06/lua-5.4.6/src/lctype.c rename to 06/deps/lua-5.4.6/src/lctype.c diff --git a/06/lua-5.4.6/src/lctype.h b/06/deps/lua-5.4.6/src/lctype.h similarity index 100% rename from 06/lua-5.4.6/src/lctype.h rename to 06/deps/lua-5.4.6/src/lctype.h diff --git a/06/lua-5.4.6/src/ldblib.c b/06/deps/lua-5.4.6/src/ldblib.c similarity index 100% rename from 06/lua-5.4.6/src/ldblib.c rename to 06/deps/lua-5.4.6/src/ldblib.c diff --git a/06/lua-5.4.6/src/ldebug.c b/06/deps/lua-5.4.6/src/ldebug.c similarity index 100% rename from 06/lua-5.4.6/src/ldebug.c rename to 06/deps/lua-5.4.6/src/ldebug.c diff --git a/06/lua-5.4.6/src/ldebug.h b/06/deps/lua-5.4.6/src/ldebug.h similarity index 100% rename from 06/lua-5.4.6/src/ldebug.h rename to 06/deps/lua-5.4.6/src/ldebug.h diff --git a/06/lua-5.4.6/src/ldo.c b/06/deps/lua-5.4.6/src/ldo.c similarity index 100% rename from 06/lua-5.4.6/src/ldo.c rename to 06/deps/lua-5.4.6/src/ldo.c diff --git a/06/lua-5.4.6/src/ldo.h b/06/deps/lua-5.4.6/src/ldo.h similarity index 100% rename from 06/lua-5.4.6/src/ldo.h rename to 06/deps/lua-5.4.6/src/ldo.h diff --git a/06/lua-5.4.6/src/ldump.c b/06/deps/lua-5.4.6/src/ldump.c similarity index 100% rename from 06/lua-5.4.6/src/ldump.c rename to 06/deps/lua-5.4.6/src/ldump.c diff --git a/06/lua-5.4.6/src/lfunc.c b/06/deps/lua-5.4.6/src/lfunc.c similarity index 100% rename from 06/lua-5.4.6/src/lfunc.c rename to 06/deps/lua-5.4.6/src/lfunc.c diff --git a/06/lua-5.4.6/src/lfunc.h b/06/deps/lua-5.4.6/src/lfunc.h similarity index 100% rename from 06/lua-5.4.6/src/lfunc.h rename to 06/deps/lua-5.4.6/src/lfunc.h diff --git a/06/lua-5.4.6/src/lgc.c b/06/deps/lua-5.4.6/src/lgc.c similarity index 100% rename from 06/lua-5.4.6/src/lgc.c rename to 06/deps/lua-5.4.6/src/lgc.c diff --git a/06/lua-5.4.6/src/lgc.h b/06/deps/lua-5.4.6/src/lgc.h similarity index 100% rename from 06/lua-5.4.6/src/lgc.h rename to 06/deps/lua-5.4.6/src/lgc.h diff --git a/06/lua-5.4.6/src/linit.c b/06/deps/lua-5.4.6/src/linit.c similarity index 100% rename from 06/lua-5.4.6/src/linit.c rename to 06/deps/lua-5.4.6/src/linit.c diff --git a/06/lua-5.4.6/src/liolib.c b/06/deps/lua-5.4.6/src/liolib.c similarity index 100% rename from 06/lua-5.4.6/src/liolib.c rename to 06/deps/lua-5.4.6/src/liolib.c diff --git a/06/lua-5.4.6/src/ljumptab.h b/06/deps/lua-5.4.6/src/ljumptab.h similarity index 100% rename from 06/lua-5.4.6/src/ljumptab.h rename to 06/deps/lua-5.4.6/src/ljumptab.h diff --git a/06/lua-5.4.6/src/llex.c b/06/deps/lua-5.4.6/src/llex.c similarity index 100% rename from 06/lua-5.4.6/src/llex.c rename to 06/deps/lua-5.4.6/src/llex.c diff --git a/06/lua-5.4.6/src/llex.h b/06/deps/lua-5.4.6/src/llex.h similarity index 100% rename from 06/lua-5.4.6/src/llex.h rename to 06/deps/lua-5.4.6/src/llex.h diff --git a/06/lua-5.4.6/src/llimits.h b/06/deps/lua-5.4.6/src/llimits.h similarity index 100% rename from 06/lua-5.4.6/src/llimits.h rename to 06/deps/lua-5.4.6/src/llimits.h diff --git a/06/lua-5.4.6/src/lmathlib.c b/06/deps/lua-5.4.6/src/lmathlib.c similarity index 100% rename from 06/lua-5.4.6/src/lmathlib.c rename to 06/deps/lua-5.4.6/src/lmathlib.c diff --git a/06/lua-5.4.6/src/lmem.c b/06/deps/lua-5.4.6/src/lmem.c similarity index 100% rename from 06/lua-5.4.6/src/lmem.c rename to 06/deps/lua-5.4.6/src/lmem.c diff --git a/06/lua-5.4.6/src/lmem.h b/06/deps/lua-5.4.6/src/lmem.h similarity index 100% rename from 06/lua-5.4.6/src/lmem.h rename to 06/deps/lua-5.4.6/src/lmem.h diff --git a/06/lua-5.4.6/src/loadlib.c b/06/deps/lua-5.4.6/src/loadlib.c similarity index 100% rename from 06/lua-5.4.6/src/loadlib.c rename to 06/deps/lua-5.4.6/src/loadlib.c diff --git a/06/lua-5.4.6/src/lobject.c b/06/deps/lua-5.4.6/src/lobject.c similarity index 100% rename from 06/lua-5.4.6/src/lobject.c rename to 06/deps/lua-5.4.6/src/lobject.c diff --git a/06/lua-5.4.6/src/lobject.h b/06/deps/lua-5.4.6/src/lobject.h similarity index 100% rename from 06/lua-5.4.6/src/lobject.h rename to 06/deps/lua-5.4.6/src/lobject.h diff --git a/06/lua-5.4.6/src/lopcodes.c b/06/deps/lua-5.4.6/src/lopcodes.c similarity index 100% rename from 06/lua-5.4.6/src/lopcodes.c rename to 06/deps/lua-5.4.6/src/lopcodes.c diff --git a/06/lua-5.4.6/src/lopcodes.h b/06/deps/lua-5.4.6/src/lopcodes.h similarity index 100% rename from 06/lua-5.4.6/src/lopcodes.h rename to 06/deps/lua-5.4.6/src/lopcodes.h diff --git a/06/lua-5.4.6/src/lopnames.h b/06/deps/lua-5.4.6/src/lopnames.h similarity index 100% rename from 06/lua-5.4.6/src/lopnames.h rename to 06/deps/lua-5.4.6/src/lopnames.h diff --git a/06/lua-5.4.6/src/loslib.c b/06/deps/lua-5.4.6/src/loslib.c similarity index 100% rename from 06/lua-5.4.6/src/loslib.c rename to 06/deps/lua-5.4.6/src/loslib.c diff --git a/06/lua-5.4.6/src/lparser.c b/06/deps/lua-5.4.6/src/lparser.c similarity index 100% rename from 06/lua-5.4.6/src/lparser.c rename to 06/deps/lua-5.4.6/src/lparser.c diff --git a/06/lua-5.4.6/src/lparser.h b/06/deps/lua-5.4.6/src/lparser.h similarity index 100% rename from 06/lua-5.4.6/src/lparser.h rename to 06/deps/lua-5.4.6/src/lparser.h diff --git a/06/lua-5.4.6/src/lprefix.h b/06/deps/lua-5.4.6/src/lprefix.h similarity index 100% rename from 06/lua-5.4.6/src/lprefix.h rename to 06/deps/lua-5.4.6/src/lprefix.h diff --git a/06/lua-5.4.6/src/lstate.c b/06/deps/lua-5.4.6/src/lstate.c similarity index 100% rename from 06/lua-5.4.6/src/lstate.c rename to 06/deps/lua-5.4.6/src/lstate.c diff --git a/06/lua-5.4.6/src/lstate.h b/06/deps/lua-5.4.6/src/lstate.h similarity index 100% rename from 06/lua-5.4.6/src/lstate.h rename to 06/deps/lua-5.4.6/src/lstate.h diff --git a/06/lua-5.4.6/src/lstring.c b/06/deps/lua-5.4.6/src/lstring.c similarity index 100% rename from 06/lua-5.4.6/src/lstring.c rename to 06/deps/lua-5.4.6/src/lstring.c diff --git a/06/lua-5.4.6/src/lstring.h b/06/deps/lua-5.4.6/src/lstring.h similarity index 100% rename from 06/lua-5.4.6/src/lstring.h rename to 06/deps/lua-5.4.6/src/lstring.h diff --git a/06/lua-5.4.6/src/lstrlib.c b/06/deps/lua-5.4.6/src/lstrlib.c similarity index 100% rename from 06/lua-5.4.6/src/lstrlib.c rename to 06/deps/lua-5.4.6/src/lstrlib.c diff --git a/06/lua-5.4.6/src/ltable.c b/06/deps/lua-5.4.6/src/ltable.c similarity index 100% rename from 06/lua-5.4.6/src/ltable.c rename to 06/deps/lua-5.4.6/src/ltable.c diff --git a/06/lua-5.4.6/src/ltable.h b/06/deps/lua-5.4.6/src/ltable.h similarity index 100% rename from 06/lua-5.4.6/src/ltable.h rename to 06/deps/lua-5.4.6/src/ltable.h diff --git a/06/lua-5.4.6/src/ltablib.c b/06/deps/lua-5.4.6/src/ltablib.c similarity index 100% rename from 06/lua-5.4.6/src/ltablib.c rename to 06/deps/lua-5.4.6/src/ltablib.c diff --git a/06/lua-5.4.6/src/ltm.c b/06/deps/lua-5.4.6/src/ltm.c similarity index 100% rename from 06/lua-5.4.6/src/ltm.c rename to 06/deps/lua-5.4.6/src/ltm.c diff --git a/06/lua-5.4.6/src/ltm.h b/06/deps/lua-5.4.6/src/ltm.h similarity index 100% rename from 06/lua-5.4.6/src/ltm.h rename to 06/deps/lua-5.4.6/src/ltm.h diff --git a/06/lua-5.4.6/src/lua.c b/06/deps/lua-5.4.6/src/lua.c similarity index 100% rename from 06/lua-5.4.6/src/lua.c rename to 06/deps/lua-5.4.6/src/lua.c diff --git a/06/lua-5.4.6/src/lua.h b/06/deps/lua-5.4.6/src/lua.h similarity index 100% rename from 06/lua-5.4.6/src/lua.h rename to 06/deps/lua-5.4.6/src/lua.h diff --git a/06/lua-5.4.6/src/lua.hpp b/06/deps/lua-5.4.6/src/lua.hpp similarity index 100% rename from 06/lua-5.4.6/src/lua.hpp rename to 06/deps/lua-5.4.6/src/lua.hpp diff --git a/06/lua-5.4.6/src/luac.c b/06/deps/lua-5.4.6/src/luac.c similarity index 100% rename from 06/lua-5.4.6/src/luac.c rename to 06/deps/lua-5.4.6/src/luac.c diff --git a/06/lua-5.4.6/src/luaconf.h b/06/deps/lua-5.4.6/src/luaconf.h similarity index 100% rename from 06/lua-5.4.6/src/luaconf.h rename to 06/deps/lua-5.4.6/src/luaconf.h diff --git a/06/lua-5.4.6/src/lualib.h b/06/deps/lua-5.4.6/src/lualib.h similarity index 100% rename from 06/lua-5.4.6/src/lualib.h rename to 06/deps/lua-5.4.6/src/lualib.h diff --git a/06/lua-5.4.6/src/lundump.c b/06/deps/lua-5.4.6/src/lundump.c similarity index 100% rename from 06/lua-5.4.6/src/lundump.c rename to 06/deps/lua-5.4.6/src/lundump.c diff --git a/06/lua-5.4.6/src/lundump.h b/06/deps/lua-5.4.6/src/lundump.h similarity index 100% rename from 06/lua-5.4.6/src/lundump.h rename to 06/deps/lua-5.4.6/src/lundump.h diff --git a/06/lua-5.4.6/src/lutf8lib.c b/06/deps/lua-5.4.6/src/lutf8lib.c similarity index 100% rename from 06/lua-5.4.6/src/lutf8lib.c rename to 06/deps/lua-5.4.6/src/lutf8lib.c diff --git a/06/lua-5.4.6/src/lvm.c b/06/deps/lua-5.4.6/src/lvm.c similarity index 100% rename from 06/lua-5.4.6/src/lvm.c rename to 06/deps/lua-5.4.6/src/lvm.c diff --git a/06/lua-5.4.6/src/lvm.h b/06/deps/lua-5.4.6/src/lvm.h similarity index 100% rename from 06/lua-5.4.6/src/lvm.h rename to 06/deps/lua-5.4.6/src/lvm.h diff --git a/06/lua-5.4.6/src/lzio.c b/06/deps/lua-5.4.6/src/lzio.c similarity index 100% rename from 06/lua-5.4.6/src/lzio.c rename to 06/deps/lua-5.4.6/src/lzio.c diff --git a/06/lua-5.4.6/src/lzio.h b/06/deps/lua-5.4.6/src/lzio.h similarity index 100% rename from 06/lua-5.4.6/src/lzio.h rename to 06/deps/lua-5.4.6/src/lzio.h diff --git a/06/parser-gen/LICENSE b/06/deps/parser-gen/LICENSE similarity index 100% rename from 06/parser-gen/LICENSE rename to 06/deps/parser-gen/LICENSE diff --git a/06/parser-gen/README.md b/06/deps/parser-gen/README.md similarity index 100% rename from 06/parser-gen/README.md rename to 06/deps/parser-gen/README.md diff --git a/06/parser-gen/equals.lua b/06/deps/parser-gen/equals.lua similarity index 100% rename from 06/parser-gen/equals.lua rename to 06/deps/parser-gen/equals.lua diff --git a/06/parser-gen/errorgen.lua b/06/deps/parser-gen/errorgen.lua similarity index 100% rename from 06/parser-gen/errorgen.lua rename to 06/deps/parser-gen/errorgen.lua diff --git a/06/parser-gen/parser-gen-tests.lua b/06/deps/parser-gen/parser-gen-tests.lua similarity index 85% rename from 06/parser-gen/parser-gen-tests.lua rename to 06/deps/parser-gen/parser-gen-tests.lua index 522df52..a1ae62d 100644 --- a/06/parser-gen/parser-gen-tests.lua +++ b/06/deps/parser-gen/parser-gen-tests.lua @@ -1,14 +1,5 @@ -local pg = require "parser-gen" -local peg = require "peg-parser" -local re = require "relabel" - -local eq = require "equals" - -local equals = eq.equals - - -local pr = peg.print_r - +local pg = require("parser-gen") +local equals = require("equals").equals -- terminals -- space allowed @@ -141,20 +132,18 @@ assert(errs[1]["msg"] == "Expected C") -- SELF-DESCRIPTION -pg.setlabels(peg.labels) -gram = pg.compile(peg.gram, peg.defs,_,true) -res1, errs = pg.parse(peg.gram,gram) +pg.setlabels(pg.labels) +gram = pg.compile(pg.gram, pg.defs,_,true) +res1, errs = pg.parse(pg.gram,gram) assert(res1) -- parse succesful --[[ this test is invalid since tool added ^LABEL syntax -r = re.compile(peg.gram,peg.defs) -res2 = r:match(peg.gram) +r = re.compile(pg.gram,pg.defs) +res2 = r:match(pg.gram) ---peg.print_r(res2) +--pg.print_r(res2) assert(equals(res1, res2)) ]]-- - - -print("all tests succesful") \ No newline at end of file +print("all tests succesful") diff --git a/06/parser-gen/parser-gen.lua b/06/deps/parser-gen/parser-gen.lua similarity index 97% rename from 06/parser-gen/parser-gen.lua rename to 06/deps/parser-gen/parser-gen.lua index 99b6aec..128de68 100644 --- a/06/parser-gen/parser-gen.lua +++ b/06/deps/parser-gen/parser-gen.lua @@ -1,21 +1,17 @@ -local peg = require "parser-gen.peg-parser" -local eg = require "parser-gen.errorgen" -local s = require "parser-gen.stack" +local f = (...):match("(.-)[^%.]+$") +local peg = require(f .. "peg-parser") +local eg = require(f .. "errorgen") +local s = require(f .. "stack") --- create stack for tokens inside captures. nil - not inside capture, 0 - inside capture, 1 - token found inside capture +-- Create stack for tokens inside captures. +-- nil - not inside capture, +-- 0 - inside capture, +-- 1 - token found inside capture local tokenstack = s.Stack:Create() - - - local subject, errors, errorfunc - --- Lua 5.1 compatibility: -local unpack = unpack or table.unpack - +local unpack = table.unpack; local Predef = { nl = m.P"\n", cr = m.P"\r", tab = m.P"\t" } - - local mem = {} -- for compiled grammars local function updatelocale() @@ -559,7 +555,8 @@ end - +-- could be wrong +local follow = eg.follow local pg = {compile=compile, setlabels=setlabels, parse=parse,follow=follow, calcline = peg.calcline, usenodes = usenodes} diff --git a/06/parser-gen/parsers/lua-parser-tests.lua b/06/deps/parser-gen/parsers/lua-parser-tests.lua similarity index 100% rename from 06/parser-gen/parsers/lua-parser-tests.lua rename to 06/deps/parser-gen/parsers/lua-parser-tests.lua diff --git a/06/parser-gen/parsers/lua-parser.lua b/06/deps/parser-gen/parsers/lua-parser.lua similarity index 100% rename from 06/parser-gen/parsers/lua-parser.lua rename to 06/deps/parser-gen/parsers/lua-parser.lua diff --git a/06/parser-gen/parsers/tiny-parser.lua b/06/deps/parser-gen/parsers/tiny-parser.lua similarity index 100% rename from 06/parser-gen/parsers/tiny-parser.lua rename to 06/deps/parser-gen/parsers/tiny-parser.lua diff --git a/06/parser-gen/peg-parser-tests.lua b/06/deps/parser-gen/peg-parser-tests.lua similarity index 99% rename from 06/parser-gen/peg-parser-tests.lua rename to 06/deps/parser-gen/peg-parser-tests.lua index 588b101..2d072d1 100644 --- a/06/parser-gen/peg-parser-tests.lua +++ b/06/deps/parser-gen/peg-parser-tests.lua @@ -1,4 +1,4 @@ -local peg = require "peg-parser" +local peg = require("peg-parser") local f = peg.pegToAST local eq = require "equals" diff --git a/06/parser-gen/peg-parser.lua b/06/deps/parser-gen/peg-parser.lua similarity index 99% rename from 06/parser-gen/peg-parser.lua rename to 06/deps/parser-gen/peg-parser.lua index f6fad7f..dc6d874 100644 --- a/06/parser-gen/peg-parser.lua +++ b/06/deps/parser-gen/peg-parser.lua @@ -1,4 +1,4 @@ -local re = require "lpeglabel.relabel" +local re = require("deps.lpeglabel.relabel") local peg = {} diff --git a/06/parser-gen/rockspecs/parser-gen-1.0.rockspec b/06/deps/parser-gen/rockspecs/parser-gen-1.0.rockspec similarity index 100% rename from 06/parser-gen/rockspecs/parser-gen-1.0.rockspec rename to 06/deps/parser-gen/rockspecs/parser-gen-1.0.rockspec diff --git a/06/parser-gen/rockspecs/parser-gen-1.1.rockspec b/06/deps/parser-gen/rockspecs/parser-gen-1.1.rockspec similarity index 100% rename from 06/parser-gen/rockspecs/parser-gen-1.1.rockspec rename to 06/deps/parser-gen/rockspecs/parser-gen-1.1.rockspec diff --git a/06/parser-gen/rockspecs/parser-gen-1.2.rockspec b/06/deps/parser-gen/rockspecs/parser-gen-1.2.rockspec similarity index 100% rename from 06/parser-gen/rockspecs/parser-gen-1.2.rockspec rename to 06/deps/parser-gen/rockspecs/parser-gen-1.2.rockspec diff --git a/06/parser-gen/stack.lua b/06/deps/parser-gen/stack.lua similarity index 95% rename from 06/parser-gen/stack.lua rename to 06/deps/parser-gen/stack.lua index 3367192..b2ca5bf 100644 --- a/06/parser-gen/stack.lua +++ b/06/deps/parser-gen/stack.lua @@ -1,7 +1,6 @@ -- Stack Table -- Uses a table as stack, use :push(value) and
:pop() --- Lua 5.1 compatible -local unpack = unpack or table.unpack +local unpack = table.unpack -- GLOBAL local Stack = {} diff --git a/06/lpeglabel/old_makefile b/06/lpeglabel/old_makefile deleted file mode 100644 index 6936888..0000000 --- a/06/lpeglabel/old_makefile +++ /dev/null @@ -1,60 +0,0 @@ - -COPT = -O2 -# COPT = -DLPEG_DEBUG -g - -CWARNS = -Wall -Wextra -pedantic \ - -Waggregate-return \ - -Wcast-align \ - -Wcast-qual \ - -Wdisabled-optimization \ - -Wpointer-arith \ - -Wshadow \ - -Wsign-compare \ - -Wundef \ - -Wwrite-strings \ - -Wbad-function-cast \ - -Wdeclaration-after-statement \ - -Wmissing-prototypes \ - -Wnested-externs \ - -Wstrict-prototypes \ -# -Wunreachable-code \ - - -CFLAGS = $(CWARNS) -nostdinc -B ../../05/tcc-bootstrap -I ../../05/musl-bootstrap/include -I$(LUADIR) -CC = ../../05/tcc-0.9.27/tcc - -FILES = lplvm.o lplcap.o lpltree.o lplcode.o lplprint.o -# For Linux -linux: - make lpeglabel.so "DLLFLAGS = -shared -nostdlib -B ../../05/tcc-bootstrap " - -# For Mac OS -# macosx: -# make lpeglabel.so "DLLFLAGS = -bundle -undefined dynamic_lookup" - -# For Windows -# windows: -# make lpeglabel.dll "DLLFLAGS = -shared -fPIC" - -lpeglabel.so: $(FILES) - $(CC) $(DLLFLAGS) $(FILES) -o lpeglabel.so ../lua-bootstrap/lib/liblua.a ../../05/musl-bootstrap/lib/libc.a -lpeglabel.dll: $(FILES) - $(CC) $(DLLFLAGS) $(FILES) -o lpeglabel.dll $(LUADIR)/bin/lua53.dll - -$(FILES): makefile - -test: test.lua testlabel.lua testrelabelparser.lua relabel.lua lpeglabel.so - lua test.lua - lua testlabel.lua - lua testrelabelparser.lua - -clean: - rm -f $(FILES) lpeglabel.so - - -lplcap.o: lplcap.c lplcap.h lpltypes.h -lplcode.o: lplcode.c lpltypes.h lplcode.h lpltree.h lplvm.h lplcap.h -lplprint.o: lplprint.c lpltypes.h lplprint.h lpltree.h lplvm.h lplcap.h -lpltree.o: lpltree.c lpltypes.h lplcap.h lplcode.h lpltree.h lplvm.h lplprint.h -lplvm.o: lplvm.c lplcap.h lpltypes.h lplvm.h lplprint.h lpltree.h - diff --git a/06/src/main.lua b/06/src/main.lua index 9429ccc..71c3d90 100644 --- a/06/src/main.lua +++ b/06/src/main.lua @@ -1,13 +1,11 @@ -local pg = require("parser-gen.parser-gen") -local peg = require("parser-gen.peg-parser") +local pg = require("deps.parser-gen.parser-gen") local util = require("src.util") +local errs = {errMissingThen = "Missing Then"} -- one custom error +pg.setlabels(errs) --- local errs = {errMissingThen = "Missing Then"} -- one custom error --- pg.setlabels(errs) - --- local f = assert(io.open("zig.peg", "r")) --- local zig_grammar = f:read("*all") --- f:close() --- pg.compile(zig_grammar) +local f = assert(io.open("zig.peg", "r")) +local zig_grammar = f:read("*all") +f:close() +pg.compile(zig_grammar) diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/all.lua b/tofix/lua-5.3.4-tests/all.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/all.lua rename to tofix/lua-5.3.4-tests/all.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/api.lua b/tofix/lua-5.3.4-tests/api.lua similarity index 99% rename from 06/parser-gen/parsers/lua-5.3.4-tests/api.lua rename to tofix/lua-5.3.4-tests/api.lua index c27ee97..d509248 100644 --- a/06/parser-gen/parsers/lua-5.3.4-tests/api.lua +++ b/tofix/lua-5.3.4-tests/api.lua @@ -1,3 +1,4 @@ +---@diagnostic disable: * -- $Id: api.lua,v 1.147 2016/11/07 13:06:25 roberto Exp $ -- See Copyright Notice in file all.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/attrib.lua b/tofix/lua-5.3.4-tests/attrib.lua similarity index 99% rename from 06/parser-gen/parsers/lua-5.3.4-tests/attrib.lua rename to tofix/lua-5.3.4-tests/attrib.lua index 993a96c..041f1aa 100644 --- a/06/parser-gen/parsers/lua-5.3.4-tests/attrib.lua +++ b/tofix/lua-5.3.4-tests/attrib.lua @@ -1,3 +1,4 @@ +---@diagnostic disable: unbalanced-assignments -- $Id: attrib.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $ -- See Copyright Notice in file all.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/big.lua b/tofix/lua-5.3.4-tests/big.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/big.lua rename to tofix/lua-5.3.4-tests/big.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/bitwise.lua b/tofix/lua-5.3.4-tests/bitwise.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/bitwise.lua rename to tofix/lua-5.3.4-tests/bitwise.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/calls.lua b/tofix/lua-5.3.4-tests/calls.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/calls.lua rename to tofix/lua-5.3.4-tests/calls.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/closure.lua b/tofix/lua-5.3.4-tests/closure.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/closure.lua rename to tofix/lua-5.3.4-tests/closure.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/code.lua b/tofix/lua-5.3.4-tests/code.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/code.lua rename to tofix/lua-5.3.4-tests/code.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/constructs.lua b/tofix/lua-5.3.4-tests/constructs.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/constructs.lua rename to tofix/lua-5.3.4-tests/constructs.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/coroutine.lua b/tofix/lua-5.3.4-tests/coroutine.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/coroutine.lua rename to tofix/lua-5.3.4-tests/coroutine.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/db.lua b/tofix/lua-5.3.4-tests/db.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/db.lua rename to tofix/lua-5.3.4-tests/db.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/errors.lua b/tofix/lua-5.3.4-tests/errors.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/errors.lua rename to tofix/lua-5.3.4-tests/errors.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/events.lua b/tofix/lua-5.3.4-tests/events.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/events.lua rename to tofix/lua-5.3.4-tests/events.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/files.lua b/tofix/lua-5.3.4-tests/files.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/files.lua rename to tofix/lua-5.3.4-tests/files.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/gc.lua b/tofix/lua-5.3.4-tests/gc.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/gc.lua rename to tofix/lua-5.3.4-tests/gc.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/goto.lua b/tofix/lua-5.3.4-tests/goto.lua similarity index 98% rename from 06/parser-gen/parsers/lua-5.3.4-tests/goto.lua rename to tofix/lua-5.3.4-tests/goto.lua index 0372aa9..69bc154 100644 --- a/06/parser-gen/parsers/lua-5.3.4-tests/goto.lua +++ b/tofix/lua-5.3.4-tests/goto.lua @@ -68,7 +68,7 @@ do end -- goto to correct label when nested -do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3' +-- do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3' -- ok to jump over local dec. to end of block do diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/literals.lua b/tofix/lua-5.3.4-tests/literals.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/literals.lua rename to tofix/lua-5.3.4-tests/literals.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/locals.lua b/tofix/lua-5.3.4-tests/locals.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/locals.lua rename to tofix/lua-5.3.4-tests/locals.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/main.lua b/tofix/lua-5.3.4-tests/main.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/main.lua rename to tofix/lua-5.3.4-tests/main.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/math.lua b/tofix/lua-5.3.4-tests/math.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/math.lua rename to tofix/lua-5.3.4-tests/math.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/nextvar.lua b/tofix/lua-5.3.4-tests/nextvar.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/nextvar.lua rename to tofix/lua-5.3.4-tests/nextvar.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/pm.lua b/tofix/lua-5.3.4-tests/pm.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/pm.lua rename to tofix/lua-5.3.4-tests/pm.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/sort.lua b/tofix/lua-5.3.4-tests/sort.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/sort.lua rename to tofix/lua-5.3.4-tests/sort.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/strings.lua b/tofix/lua-5.3.4-tests/strings.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/strings.lua rename to tofix/lua-5.3.4-tests/strings.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/tpack.lua b/tofix/lua-5.3.4-tests/tpack.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/tpack.lua rename to tofix/lua-5.3.4-tests/tpack.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/utf8.lua b/tofix/lua-5.3.4-tests/utf8.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/utf8.lua rename to tofix/lua-5.3.4-tests/utf8.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/vararg.lua b/tofix/lua-5.3.4-tests/vararg.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/vararg.lua rename to tofix/lua-5.3.4-tests/vararg.lua diff --git a/06/parser-gen/parsers/lua-5.3.4-tests/verybig.lua b/tofix/lua-5.3.4-tests/verybig.lua similarity index 100% rename from 06/parser-gen/parsers/lua-5.3.4-tests/verybig.lua rename to tofix/lua-5.3.4-tests/verybig.lua From fe22cc76654692f8ac814893ffe7b9d6d912fd2e Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Tue, 19 Sep 2023 11:42:10 +0100 Subject: [PATCH 02/10] Big Commit I accidentally made this commit really big after some git mishaps. Working on zig grammar, finally kinda works. Fixed some diagnostics warnings Moved examples to tofix because fixing them is besides the point right now. --- .ignore | 10 + 06/README.md | 1 + 06/arith.peg | 5 + 06/deps/parser-gen/peg-parser.lua | 2 + 06/kitty | 13 + 06/src/ast.lua | 32 ++ 06/src/main.lua | 23 +- 06/test.zig | 6 + 06/zig.peg | 506 +++++++++++++++--------------- kitty | 17 + kitty.conf | 5 + scripts/build.sh | 3 + scripts/run.sh | 3 + 13 files changed, 367 insertions(+), 259 deletions(-) create mode 100644 .ignore create mode 100644 06/arith.peg create mode 100755 06/kitty create mode 100644 06/src/ast.lua create mode 100644 06/test.zig create mode 100755 kitty create mode 100644 kitty.conf create mode 100755 scripts/build.sh create mode 100755 scripts/run.sh diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..347cc95 --- /dev/null +++ b/.ignore @@ -0,0 +1,10 @@ +00* +01* +02* +03* +04* +05* +06/deps/lua- +06/deps/lua-* + +tofix diff --git a/06/README.md b/06/README.md index 319565c..2922ed5 100644 --- a/06/README.md +++ b/06/README.md @@ -8,6 +8,7 @@ nloaded' failed!" Write rules into makefile to run tests 1. Create test runner if it doesn't exist for lpeglable and parser-gen 2. Update tests to lua 5.4 +2. Fix diagnostics in tofix 3. Write a 'make test' rule Implement backslashed escaped characters in grammar parser diff --git a/06/arith.peg b/06/arith.peg new file mode 100644 index 0000000..cf994a4 --- /dev/null +++ b/06/arith.peg @@ -0,0 +1,5 @@ +Expr <- Sum +Sum <- Product (('+' / '-') Product)* +Product <- Power (('*' / '/') Power)* +Power <- Value ('^' Power)? +Value <- [0-9]+ / '(' Expr ')' \ No newline at end of file diff --git a/06/deps/parser-gen/peg-parser.lua b/06/deps/parser-gen/peg-parser.lua index dc6d874..1448511 100644 --- a/06/deps/parser-gen/peg-parser.lua +++ b/06/deps/parser-gen/peg-parser.lua @@ -1,4 +1,5 @@ local re = require("deps.lpeglabel.relabel") +local util = require("src.util") local peg = {} @@ -256,6 +257,7 @@ function peg.pegToAST(input, defs) table.insert(err, string.rep(" ", col-1) .. "^") error("syntax error(s) in pattern\n" .. table.concat(err, "\n"), 3) end + return r end diff --git a/06/kitty b/06/kitty new file mode 100755 index 0000000..5a16c7f --- /dev/null +++ b/06/kitty @@ -0,0 +1,13 @@ +new_tab code +layout horizontal +launch --title "code" bash + +new_tab terminal +layout horizontal +launch bash + +new_tab ranger +enabled_layouts horizontal,stack +layout horizontal +launch ranger + diff --git a/06/src/ast.lua b/06/src/ast.lua new file mode 100644 index 0000000..93b4c6d --- /dev/null +++ b/06/src/ast.lua @@ -0,0 +1,32 @@ +util = require('src.util') + +local function parse_tree_to_ast(parsetree) + stack = {{i=1,node=parsetree}}; + + while #stack > 0 do + current = table.remove(stack, 1) + + for i = current.i, #current.node, 1 do + inner = current.node[i] + if type(inner) == "table" then + current.i = i + 1; + table.insert(stack, current) + table.insert(stack, {i=1, node=inner}) + break; + else + print(inner) + end + end + end + + return {} +end + +local function print_ast(ast) + +end + +return { + parse_tree_to_ast = parse_tree_to_ast, + print = print_ast +} \ No newline at end of file diff --git a/06/src/main.lua b/06/src/main.lua index 71c3d90..d1a6ab9 100644 --- a/06/src/main.lua +++ b/06/src/main.lua @@ -1,11 +1,24 @@ local pg = require("deps.parser-gen.parser-gen") local util = require("src.util") - -local errs = {errMissingThen = "Missing Then"} -- one custom error -pg.setlabels(errs) +local ast = require("src.ast") local f = assert(io.open("zig.peg", "r")) -local zig_grammar = f:read("*all") +local raw = f:read("*all") f:close() -pg.compile(zig_grammar) +local grammar = pg.compile(raw) + +f = assert(io.open("test.zig", "r")) +raw = f:read("*all") +f:close() + +local errors = 0 +local function printerror(desc,line,col,sfail,trec) + errors = errors+1 + print("Error #"..errors..": "..desc.." on line "..line.."(col "..col..")") +end + +local result, errors = pg.parse(raw, grammar, printerror) + +my_ast = ast.parse_tree_to_ast(result) +ast.print(my_ast) diff --git a/06/test.zig b/06/test.zig new file mode 100644 index 0000000..6e4c9c5 --- /dev/null +++ b/06/test.zig @@ -0,0 +1,6 @@ + + + +pub fn main() void { + const x = 0; +} \ No newline at end of file diff --git a/06/zig.peg b/06/zig.peg index a8f2033..b4976d9 100644 --- a/06/zig.peg +++ b/06/zig.peg @@ -1,69 +1,69 @@ -Root <- skip container_doc_comment? ContainerMembers eof +Root <- container_doc_comment? container_members eof -ContainerMembers <- ContainerDeclarations (ContainerField COMMA)* (ContainerField / ContainerDeclarations) +container_members <- container_declarations (container_field COMMA)* (container_field / container_declarations) -ContainerDeclarations - <- TestDecl ContainerDeclarations - / TopLevelComptime ContainerDeclarations - / doc_comment? KEYWORD_pub? TopLevelDecl ContainerDeclarations - +container_declarations + <- test_decl container_declarations + / top_level_comptime container_declarations + / doc_comment? KEYWORD_PUB? top_level_decl container_declarations + / end_of_word -TestDecl <- doc_comment? KEYWORD_test STRINGLITERALSINGLE? Block +test_decl <- doc_comment? KEYWORD_TEST block -TopLevelComptime <- doc_comment? KEYWORD_comptime BlockExpr +top_level_comptime <- doc_comment? KEYWORD_COMPTIME block_expr -TopLevelDecl - <- (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE? / (KEYWORD_inline / KEYWORD_noinline))? FnProto (SEMICOLON / Block) - / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl - / KEYWORD_usingnamespace Expr SEMICOLON +top_level_decl + <- (KEYWORD_EXPORT / KEYWORD_EXTERN / (KEYWORD_INLINE / KEYWORD_NOINLINE))? fn_proto (SEMICOLON / block) + / (KEYWORD_EXPORT / KEYWORD_EXTERN )? KEYWORD_THREADLOCAL? var_decl + / KEYWORD_USINGNAMESPACE expr SEMICOLON -FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? CallConv? EXCLAMATIONMARK? TypeExpr +fn_proto <- KEYWORD_FN IDENTIFIER? LPAREN param_decl_list RPAREN byte_align? link_section? call_conv? EXCLAMATIONMARK? type_expr -VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? LinkSection? (EQUAL Expr)? SEMICOLON +var_decl <- (KEYWORD_CONST / KEYWORD_VAR) IDENTIFIER (COLON type_expr)? byte_align? link_section? (EQUAL expr)? SEMICOLON -ContainerField <- doc_comment? KEYWORD_comptime? IDENTIFIER (COLON (KEYWORD_anytype / TypeExpr) ByteAlign?)? (EQUAL Expr)? +container_field <- doc_comment? KEYWORD_COMPTIME? IDENTIFIER (COLON (KEYWORD_ANYTYPE / type_expr) byte_align?)? (EQUAL expr)? -Statement - <- KEYWORD_comptime? VarDecl - / KEYWORD_comptime BlockExprStatement - / KEYWORD_nosuspend BlockExprStatement - / KEYWORD_suspend BlockExprStatement - / KEYWORD_defer BlockExprStatement - / KEYWORD_errdefer Payload? BlockExprStatement - / IfStatement - / LabeledStatement - / SwitchExpr - / AssignExpr SEMICOLON +statement + <- KEYWORD_COMPTIME? var_decl + / KEYWORD_COMPTIME block_expr_satement + / KEYWORD_NOSUSPEND block_expr_satement + / KEYWORD_SUSPEND block_expr_satement + / KEYWORD_DEFER block_expr_satement + / KEYWORD_ERRDEFER payload? block_expr_satement + / if_statement + / labeled_statement + / switch_expr + / assign_expr SEMICOLON -IfStatement - <- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )? - / IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) +if_statement + <- if_prefix block_expr ( KEYWORD_ELSE payload? statement )? + / if_prefix assign_expr ( SEMICOLON / KEYWORD_ELSE payload? statement ) -LabeledStatement <- BlockLabel? (Block / LoopStatement) +labeled_statement <- block_label? (block / loop_statement) -LoopStatement <- KEYWORD_inline? (ForStatement / WhileStatement) +loop_statement <- KEYWORD_INLINE? (for_statement / while_statement) -ForStatement - <- ForPrefix BlockExpr ( KEYWORD_else Statement )? - / ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement ) +for_statement + <- for_prefix block_expr ( KEYWORD_ELSE statement )? + / for_prefix assign_expr ( SEMICOLON / KEYWORD_ELSE statement ) -WhileStatement - <- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )? - / WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement ) +while_statement + <- WhilePrefix block_expr ( KEYWORD_ELSE payload? statement )? + / WhilePrefix assign_expr ( SEMICOLON / KEYWORD_ELSE payload? statement ) -BlockExprStatement - <- BlockExpr - / AssignExpr SEMICOLON +block_expr_satement + <- block_expr + / assign_expr SEMICOLON -BlockExpr <- BlockLabel? Block +block_expr <- block_label? block -AssignExpr <- Expr (AssignOp Expr)? +assign_expr <- expr (AssignOp expr)? -Expr <- BoolOrExpr +expr <- bool_or_expr -BoolOrExpr <- BoolAndExpr (KEYWORD_or BoolAndExpr)* +bool_or_expr <- BoolAndExpr (KEYWORD_OR BoolAndExpr)* -BoolAndExpr <- CompareExpr (KEYWORD_and CompareExpr)* +BoolAndExpr <- CompareExpr (KEYWORD_AND CompareExpr)* CompareExpr <- BitwiseExpr (CompareOp BitwiseExpr)? @@ -80,39 +80,39 @@ PrefixExpr <- PrefixOp* PrimaryExpr PrimaryExpr <- AsmExpr / IfExpr - / KEYWORD_break BreakLabel? Expr? - / KEYWORD_comptime Expr - / KEYWORD_nosuspend Expr - / KEYWORD_continue BreakLabel? - / KEYWORD_resume Expr - / KEYWORD_return Expr? - / BlockLabel? LoopExpr - / Block + / KEYWORD_BREAK BreakLabel? expr? + / KEYWORD_COMPTIME expr + / KEYWORD_NOSUSPEND expr + / KEYWORD_CONTINUE BreakLabel? + / KEYWORD_RESUME expr + / KEYWORD_RETURN expr? + / block_label? LoopExpr + / block / CurlySuffixExpr -IfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)? +IfExpr <- if_prefix expr (KEYWORD_ELSE payload? expr)? -Block <- LBRACE Statement* RBRACE +block <- LBRACE statement* RBRACE -LoopExpr <- KEYWORD_inline? (ForExpr / WhileExpr) +LoopExpr <- KEYWORD_INLINE? (ForExpr / WhileExpr) -ForExpr <- ForPrefix Expr (KEYWORD_else Expr)? +ForExpr <- for_prefix expr (KEYWORD_ELSE expr)? -WhileExpr <- WhilePrefix Expr (KEYWORD_else Payload? Expr)? +WhileExpr <- WhilePrefix expr (KEYWORD_ELSE payload? expr)? -CurlySuffixExpr <- TypeExpr InitList? +CurlySuffixExpr <- type_expr InitList? InitList <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE - / LBRACE Expr (COMMA Expr)* COMMA? RBRACE + / LBRACE expr (COMMA expr)* COMMA? RBRACE / LBRACE RBRACE -TypeExpr <- PrefixTypeOp* ErrorUnionExpr +type_expr <- PrefixTypeOp* ErrorUnionExpr -ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK TypeExpr)? +ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK type_expr)? SuffixExpr - <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments + <- KEYWORD_ASYNC PrimaryTypeExpr SuffixOp* FnCallArguments / PrimaryTypeExpr (SuffixOp / FnCallArguments)* PrimaryTypeExpr @@ -123,91 +123,91 @@ PrimaryTypeExpr / DOT InitList / ErrorSetDecl / FLOAT - / FnProto + / fn_proto / GroupedExpr / LabeledTypeExpr / IDENTIFIER / IfTypeExpr / INTEGER - / KEYWORD_comptime TypeExpr - / KEYWORD_error DOT IDENTIFIER - / KEYWORD_anyframe - / KEYWORD_unreachable + / KEYWORD_COMPTIME type_expr + / KEYWORD_ERROR DOT IDENTIFIER + / KEYWORD_ANYFRAME + / KEYWORD_UNREACHABLE / STRINGLITERAL - / SwitchExpr + / switch_expr -ContainerDecl <- (KEYWORD_extern / KEYWORD_packed)? ContainerDeclAuto +ContainerDecl <- (KEYWORD_EXTERN / KEYWORD_PACKED)? ContainerDeclAuto -ErrorSetDecl <- KEYWORD_error LBRACE IdentifierList RBRACE +ErrorSetDecl <- KEYWORD_ERROR LBRACE IdentifierList RBRACE -GroupedExpr <- LPAREN Expr RPAREN +GroupedExpr <- LPAREN expr RPAREN -IfTypeExpr <- IfPrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? +IfTypeExpr <- if_prefix type_expr (KEYWORD_ELSE payload? type_expr)? LabeledTypeExpr - <- BlockLabel Block - / BlockLabel? LoopTypeExpr + <- block_label block + / block_label? LoopTypeExpr -LoopTypeExpr <- KEYWORD_inline? (ForTypeExpr / WhileTypeExpr) +LoopTypeExpr <- KEYWORD_INLINE? (ForTypeExpr / WhileTypeExpr) -ForTypeExpr <- ForPrefix TypeExpr (KEYWORD_else TypeExpr)? +ForTypeExpr <- for_prefix type_expr (KEYWORD_ELSE type_expr)? -WhileTypeExpr <- WhilePrefix TypeExpr (KEYWORD_else Payload? TypeExpr)? +WhileTypeExpr <- WhilePrefix type_expr (KEYWORD_ELSE payload? type_expr)? -SwitchExpr <- KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE +switch_expr <- KEYWORD_SWITCH LPAREN expr RPAREN LBRACE switch_prong_list RBRACE -AsmExpr <- KEYWORD_asm KEYWORD_volatile? LPAREN Expr AsmOutput? RPAREN +AsmExpr <- KEYWORD_ASM KEYWORD_VOLATILE? LPAREN expr AsmOutput? RPAREN AsmOutput <- COLON AsmOutputList AsmInput? -AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPAREN +AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW type_expr / IDENTIFIER) RPAREN AsmInput <- COLON AsmInputList AsmClobbers? -AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN +AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN expr RPAREN AsmClobbers <- COLON StringList BreakLabel <- COLON IDENTIFIER -BlockLabel <- IDENTIFIER COLON +block_label <- IDENTIFIER COLON -FieldInit <- DOT IDENTIFIER EQUAL Expr +FieldInit <- DOT IDENTIFIER EQUAL expr -WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN +while_continue_expr <- COLON LPAREN assign_expr RPAREN -LinkSection <- KEYWORD_linksection LPAREN Expr RPAREN +link_section <- KEYWORD_LINKSECTION LPAREN expr RPAREN -CallConv <- KEYWORD_callconv LPAREN Expr RPAREN +call_conv <- KEYWORD_CALLCONV LPAREN expr RPAREN -ParamDecl - <- doc_comment? (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType +param_decl + <- doc_comment? (KEYWORD_NOALIAS / KEYWORD_COMPTIME)? (IDENTIFIER COLON)? param_type / DOT3 -ParamType - <- KEYWORD_anytype - / TypeExpr +param_type + <- KEYWORD_ANYTYPE + / type_expr -IfPrefix <- KEYWORD_if LPAREN Expr RPAREN PtrPayload? +if_prefix <- KEYWORD_IF LPAREN expr RPAREN PtrPayload? -WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr? +WhilePrefix <- KEYWORD_WHILE LPAREN expr RPAREN PtrPayload? while_continue_expr? -ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload +for_prefix <- KEYWORD_FOR LPAREN expr RPAREN ptr_index_payload -Payload <- PIPE IDENTIFIER PIPE +payload <- PIPE IDENTIFIER PIPE PtrPayload <- PIPE ASTERISK? IDENTIFIER PIPE -PtrIndexPayload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE +ptr_index_payload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE -SwitchProng <- SwitchCase EQUALRARROW PtrPayload? AssignExpr +SwitchProng <- SwitchCase EQUALRARROW PtrPayload? assign_expr SwitchCase <- SwitchItem (COMMA SwitchItem)* COMMA? - / KEYWORD_else + / KEYWORD_ELSE -SwitchItem <- Expr (DOT3 Expr)? +SwitchItem <- expr (DOT3 expr)? AssignOp <- ASTERISKEQUAL @@ -237,8 +237,8 @@ BitwiseOp <- AMPERSAND / CARET / PIPE - / KEYWORD_orelse - / KEYWORD_catch Payload? + / KEYWORD_ORELSE + / KEYWORD_CATCH payload? BitShiftOp <- LARROW2 @@ -265,46 +265,46 @@ PrefixOp / TILDE / MINUSPERCENT / AMPERSAND - / KEYWORD_try - / KEYWORD_await + / KEYWORD_TRY + / KEYWORD_AWAIT PrefixTypeOp <- QUESTIONMARK - / KEYWORD_anyframe MINUSRARROW - / SliceTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* - / PtrTypeStart (KEYWORD_align LPAREN Expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)* + / KEYWORD_ANYFRAME MINUSRARROW + / SliceTypeStart (byte_align / KEYWORD_CONST / KEYWORD_VOLATILE / KEYWORD_ALLOWZERO)* + / PtrTypeStart (KEYWORD_ALIGN LPAREN expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_CONST / KEYWORD_VOLATILE / KEYWORD_ALLOWZERO)* / ArrayTypeStart SuffixOp - <- LBRACKET Expr (DOT2 (Expr? (COLON Expr)?)?)? RBRACKET + <- LBRACKET expr (DOT2 (expr? (COLON expr)?)?)? RBRACKET / DOT IDENTIFIER / DOTASTERISK / DOTQUESTIONMARK -FnCallArguments <- LPAREN ExprList RPAREN +FnCallArguments <- LPAREN expr_list RPAREN -SliceTypeStart <- LBRACKET (COLON Expr)? RBRACKET +SliceTypeStart <- LBRACKET (COLON expr)? RBRACKET PtrTypeStart <- ASTERISK / ASTERISK2 - / LBRACKET ASTERISK (LETTERC / COLON Expr)? RBRACKET + / LBRACKET ASTERISK (LETTERC / COLON expr)? RBRACKET -ArrayTypeStart <- LBRACKET Expr (COLON Expr)? RBRACKET +ArrayTypeStart <- LBRACKET expr (COLON expr)? RBRACKET -ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? ContainerMembers RBRACE +ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? container_members RBRACE ContainerDeclType - <- KEYWORD_struct - / KEYWORD_opaque - / KEYWORD_enum (LPAREN Expr RPAREN)? - / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)? + <- KEYWORD_STRUCT + / KEYWORD_OPAQUE + / KEYWORD_ENUM (LPAREN expr RPAREN)? + / KEYWORD_UNION (LPAREN (KEYWORD_ENUM (LPAREN expr RPAREN)? / expr) RPAREN)? -ByteAlign <- KEYWORD_align LPAREN Expr RPAREN +byte_align <- KEYWORD_ALIGN LPAREN expr RPAREN IdentifierList <- (doc_comment? IDENTIFIER COMMA)* (doc_comment? IDENTIFIER)? -SwitchProngList <- (SwitchProng COMMA)* SwitchProng? +switch_prong_list <- (SwitchProng COMMA)* SwitchProng? AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem? @@ -312,9 +312,9 @@ AsmInputList <- (AsmInputItem COMMA)* AsmInputItem? StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL? -ParamDeclList <- (ParamDecl COMMA)* ParamDecl? +param_decl_list <- (param_decl COMMA)* param_decl? -ExprList <- (Expr COMMA)* Expr? +expr_list <- (expr COMMA)* expr? eof <- !. bin <- [01] @@ -374,145 +374,143 @@ container_doc_comment <- ('//!' [^\n]* [ \n]*)+ doc_comment <- ('///' [^\n]* [ \n]*)+ line_comment <- '//' ![!/][^\n]* / '////' [^\n]* line_string <- ("\\\\" [^\n]* [ \n]*)+ -skip <- ([ \n] / line_comment)* -CHAR_LITERAL <- "'" char_char "'" skip +CHAR_LITERAL <- "'" char_char "'" FLOAT - <- "0x" hex_int "." hex_int ([pP] [-+]? dec_int)? skip - / dec_int "." dec_int ([eE] [-+]? dec_int)? skip - / "0x" hex_int [pP] [-+]? dec_int skip - / dec_int [eE] [-+]? dec_int skip + <- "0x" hex_int "." hex_int ([pP] [-+]? dec_int)? + / dec_int "." dec_int ([eE] [-+]? dec_int)? + / "0x" hex_int [pP] [-+]? dec_int + / dec_int [eE] [-+]? dec_int INTEGER - <- "0b" bin_int skip - / "0o" oct_int skip - / "0x" hex_int skip - / dec_int skip -STRINGLITERALSINGLE <- "\"" string_char* "\"" skip + <- "0b" bin_int + / "0o" oct_int + / "0x" hex_int + / dec_int +STRINGLITERALSINGLE <- '"' string_char* '"' STRINGLITERAL <- STRINGLITERALSINGLE - / (line_string skip)+ + / (line_string)+ IDENTIFIER - <- !keyword [A-Za-z_] [A-Za-z0-9_]* skip - / "@\"" string_char* "\"" skip -BUILTINIDENTIFIER <- "@"[A-Za-z_][A-Za-z0-9_]* skip + <- !KEYWORD [A-Za-z_] [A-Za-z0-9_]* +BUILTINIDENTIFIER <- "@"[A-Za-z_][A-Za-z0-9_]* -AMPERSAND <- '&' ![=] skip -AMPERSANDEQUAL <- '&=' skip -ASTERISK <- '*' ![*%=] skip -ASTERISK2 <- '**' skip -ASTERISKEQUAL <- '*=' skip -ASTERISKPERCENT <- '*%' ![=] skip -ASTERISKPERCENTEQUAL <- '*%=' skip -CARET <- '^' ![=] skip -CARETEQUAL <- '^=' skip -COLON <- ':' skip -COMMA <- ',' skip -DOT <- '.' ![*.?] skip -DOT2 <- '..' ![.] skip -DOT3 <- '...' skip -DOTASTERISK <- '.*' skip -DOTQUESTIONMARK <- '.?' skip -EQUAL <- '=' ![>=] skip -EQUALEQUAL <- '==' skip -EQUALRARROW <- '=>' skip -EXCLAMATIONMARK <- '!' ![=] skip -EXCLAMATIONMARKEQUAL <- '!=' skip -LARROW <- '<' ![<=] skip -LARROW2 <- '<<' ![=] skip -LARROW2EQUAL <- '<<=' skip -LARROWEQUAL <- '<=' skip -LBRACE <- '{' skip -LBRACKET <- '[' skip -LPAREN <- '(' skip -MINUS <- '-' ![%=>] skip -MINUSEQUAL <- '-=' skip -MINUSPERCENT <- '-%' ![=] skip -MINUSPERCENTEQUAL <- '-%=' skip -MINUSRARROW <- '->' skip -PERCENT <- '%' ![=] skip -PERCENTEQUAL <- '%=' skip -PIPE <- '|' ![|=] skip -PIPE2 <- '||' skip -PIPEEQUAL <- '|=' skip -PLUS <- '+' ![%+=] skip -PLUS2 <- '++' skip -PLUSEQUAL <- '+=' skip -PLUSPERCENT <- '+%' ![=] skip -PLUSPERCENTEQUAL <- '+%=' skip -LETTERC <- 'c' skip -QUESTIONMARK <- '?' skip -RARROW <- '>' ![>=] skip -RARROW2 <- '>>' ![=] skip -RARROW2EQUAL <- '>>=' skip -RARROWEQUAL <- '>=' skip -RBRACE <- '}' skip -RBRACKET <- ']' skip -RPAREN <- ')' skip -SEMICOLON <- ';' skip -SLASH <- '/' ![=] skip -SLASHEQUAL <- '/=' skip -TILDE <- '~' skip +AMPERSAND <- '&' ![=] +AMPERSANDEQUAL <- '&=' +ASTERISK <- '*' ![*%=] +ASTERISK2 <- '**' +ASTERISKEQUAL <- '*=' +ASTERISKPERCENT <- '*%' ![=] +ASTERISKPERCENTEQUAL <- '*%=' +CARET <- '^' ![=] +CARETEQUAL <- '^=' +COLON <- ':' +COMMA <- ',' +DOT <- '.' ![*.?] +DOT2 <- '..' ![.] +DOT3 <- '...' +DOTASTERISK <- '.*' +DOTQUESTIONMARK <- '.?' +EQUAL <- '=' ![>=] +EQUALEQUAL <- '==' +EQUALRARROW <- '=>' +EXCLAMATIONMARK <- '!' ![=] +EXCLAMATIONMARKEQUAL <- '!=' +LARROW <- '<' ![<=] +LARROW2 <- '<<' ![=] +LARROW2EQUAL <- '<<=' +LARROWEQUAL <- '<=' +LBRACE <- '{' +LBRACKET <- '[' +LPAREN <- '(' +MINUS <- '-' ![%=>] +MINUSEQUAL <- '-=' +MINUSPERCENT <- '-%' ![=] +MINUSPERCENTEQUAL <- '-%=' +MINUSRARROW <- '->' +PERCENT <- '%' ![=] +PERCENTEQUAL <- '%=' +PIPE <- '|' ![|=] +PIPE2 <- '||' +PIPEEQUAL <- '|=' +PLUS <- '+' ![%+=] +PLUS2 <- '++' +PLUSEQUAL <- '+=' +PLUSPERCENT <- '+%' ![=] +PLUSPERCENTEQUAL <- '+%=' +LETTERC <- 'c' +QUESTIONMARK <- '?' +RARROW <- '>' ![>=] +RARROW2 <- '>>' ![=] +RARROW2EQUAL <- '>>=' +RARROWEQUAL <- '>=' +RBRACE <- '}' +RBRACKET <- ']' +RPAREN <- ')' +SEMICOLON <- ';' +SLASH <- '/' ![=] +SLASHEQUAL <- '/=' +TILDE <- '~' -end_of_word <- ![a-zA-Z0-9_] skip -KEYWORD_align <- 'align' end_of_word -KEYWORD_allowzero <- 'allowzero' end_of_word -KEYWORD_and <- 'and' end_of_word -KEYWORD_anyframe <- 'anyframe' end_of_word -KEYWORD_anytype <- 'anytype' end_of_word -KEYWORD_asm <- 'asm' end_of_word -KEYWORD_async <- 'async' end_of_word -KEYWORD_await <- 'await' end_of_word -KEYWORD_break <- 'break' end_of_word -KEYWORD_callconv <- 'callconv' end_of_word -KEYWORD_catch <- 'catch' end_of_word -KEYWORD_comptime <- 'comptime' end_of_word -KEYWORD_const <- 'const' end_of_word -KEYWORD_continue <- 'continue' end_of_word -KEYWORD_defer <- 'defer' end_of_word -KEYWORD_else <- 'else' end_of_word -KEYWORD_enum <- 'enum' end_of_word -KEYWORD_errdefer <- 'errdefer' end_of_word -KEYWORD_error <- 'error' end_of_word -KEYWORD_export <- 'export' end_of_word -KEYWORD_extern <- 'extern' end_of_word -KEYWORD_fn <- 'fn' end_of_word -KEYWORD_for <- 'for' end_of_word -KEYWORD_if <- 'if' end_of_word -KEYWORD_inline <- 'inline' end_of_word -KEYWORD_noalias <- 'noalias' end_of_word -KEYWORD_nosuspend <- 'nosuspend' end_of_word -KEYWORD_noinline <- 'noinline' end_of_word -KEYWORD_opaque <- 'opaque' end_of_word -KEYWORD_or <- 'or' end_of_word -KEYWORD_orelse <- 'orelse' end_of_word -KEYWORD_packed <- 'packed' end_of_word -KEYWORD_pub <- 'pub' end_of_word -KEYWORD_resume <- 'resume' end_of_word -KEYWORD_return <- 'return' end_of_word -KEYWORD_linksection <- 'linksection' end_of_word -KEYWORD_struct <- 'struct' end_of_word -KEYWORD_suspend <- 'suspend' end_of_word -KEYWORD_switch <- 'switch' end_of_word -KEYWORD_test <- 'test' end_of_word -KEYWORD_threadlocal <- 'threadlocal' end_of_word -KEYWORD_try <- 'try' end_of_word -KEYWORD_union <- 'union' end_of_word -KEYWORD_unreachable <- 'unreachable' end_of_word -KEYWORD_usingnamespace <- 'usingnamespace' end_of_word -KEYWORD_var <- 'var' end_of_word -KEYWORD_volatile <- 'volatile' end_of_word -KEYWORD_while <- 'while' end_of_word +fragment end_of_word <- ![a-zA-Z0-9_] +KEYWORD_ALIGN <- 'align' end_of_word +KEYWORD_ALLOWZERO <- 'allowzero' end_of_word +KEYWORD_AND <- 'and' end_of_word +KEYWORD_ANYFRAME <- 'anyframe' end_of_word +KEYWORD_ANYTYPE <- 'anytype' end_of_word +KEYWORD_ASM <- 'asm' end_of_word +KEYWORD_ASYNC <- 'async' end_of_word +KEYWORD_AWAIT <- 'await' end_of_word +KEYWORD_BREAK <- 'break' end_of_word +KEYWORD_CALLCONV <- 'callconv' end_of_word +KEYWORD_CATCH <- 'catch' end_of_word +KEYWORD_COMPTIME <- 'comptime' end_of_word +KEYWORD_CONST <- 'const' end_of_word +KEYWORD_CONTINUE <- 'continue' end_of_word +KEYWORD_DEFER <- 'defer' end_of_word +KEYWORD_ELSE <- 'else' end_of_word +KEYWORD_ENUM <- 'enum' end_of_word +KEYWORD_ERRDEFER <- 'errdefer' end_of_word +KEYWORD_ERROR <- 'error' end_of_word +KEYWORD_EXPORT <- 'export' end_of_word +KEYWORD_EXTERN <- 'extern' end_of_word +KEYWORD_FN <- 'fn' end_of_word +KEYWORD_FOR <- 'for' end_of_word +KEYWORD_IF <- 'if' end_of_word +KEYWORD_INLINE <- 'inline' end_of_word +KEYWORD_NOALIAS <- 'noalias' end_of_word +KEYWORD_NOSUSPEND <- 'nosuspend' end_of_word +KEYWORD_NOINLINE <- 'noinline' end_of_word +KEYWORD_OPAQUE <- 'opaque' end_of_word +KEYWORD_OR <- 'or' end_of_word +KEYWORD_ORELSE <- 'orelse' end_of_word +KEYWORD_PACKED <- 'packed' end_of_word +KEYWORD_PUB <- 'pub' end_of_word +KEYWORD_RESUME <- 'resume' end_of_word +KEYWORD_RETURN <- 'return' end_of_word +KEYWORD_LINKSECTION <- 'linksection' end_of_word +KEYWORD_STRUCT <- 'struct' end_of_word +KEYWORD_SUSPEND <- 'suspend' end_of_word +KEYWORD_SWITCH <- 'switch' end_of_word +KEYWORD_TEST <- 'test' end_of_word +KEYWORD_THREADLOCAL <- 'threadlocal' end_of_word +KEYWORD_TRY <- 'try' end_of_word +KEYWORD_UNION <- 'union' end_of_word +KEYWORD_UNREACHABLE <- 'unreachable' end_of_word +KEYWORD_USINGNAMESPACE <- 'usingnamespace' end_of_word +KEYWORD_VAR <- 'var' end_of_word +KEYWORD_VOLATILE <- 'volatile' end_of_word +KEYWORD_WHILE <- 'while' end_of_word -keyword <- KEYWORD_align / KEYWORD_allowzero / KEYWORD_and / KEYWORD_anyframe - / KEYWORD_anytype / KEYWORD_asm / KEYWORD_async / KEYWORD_await - / KEYWORD_break / KEYWORD_callconv / KEYWORD_catch / KEYWORD_comptime - / KEYWORD_const / KEYWORD_continue / KEYWORD_defer / KEYWORD_else - / KEYWORD_enum / KEYWORD_errdefer / KEYWORD_error / KEYWORD_export - / KEYWORD_extern / KEYWORD_fn / KEYWORD_for / KEYWORD_if - / KEYWORD_inline / KEYWORD_noalias / KEYWORD_nosuspend / KEYWORD_noinline - / KEYWORD_opaque / KEYWORD_or / KEYWORD_orelse / KEYWORD_packed - / KEYWORD_pub / KEYWORD_resume / KEYWORD_return / KEYWORD_linksection - / KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch / KEYWORD_test - / KEYWORD_threadlocal / KEYWORD_try / KEYWORD_union / KEYWORD_unreachable - / KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while \ No newline at end of file +KEYWORD <- KEYWORD_ALIGN / KEYWORD_ALLOWZERO / KEYWORD_AND / KEYWORD_ANYFRAME + / KEYWORD_ANYTYPE / KEYWORD_ASM / KEYWORD_ASYNC / KEYWORD_AWAIT + / KEYWORD_BREAK / KEYWORD_CALLCONV / KEYWORD_CATCH / KEYWORD_COMPTIME + / KEYWORD_CONST / KEYWORD_CONTINUE / KEYWORD_DEFER / KEYWORD_ELSE + / KEYWORD_ENUM / KEYWORD_ERRDEFER / KEYWORD_ERROR / KEYWORD_EXPORT + / KEYWORD_EXTERN / KEYWORD_FN / KEYWORD_FOR / KEYWORD_IF + / KEYWORD_INLINE / KEYWORD_NOALIAS / KEYWORD_NOSUSPEND / KEYWORD_NOINLINE + / KEYWORD_OPAQUE / KEYWORD_OR / KEYWORD_ORELSE / KEYWORD_PACKED + / KEYWORD_PUB / KEYWORD_RESUME / KEYWORD_RETURN / KEYWORD_LINKSECTION + / KEYWORD_STRUCT / KEYWORD_SUSPEND / KEYWORD_SWITCH / KEYWORD_TEST + / KEYWORD_THREADLOCAL / KEYWORD_TRY / KEYWORD_UNION / KEYWORD_UNREACHABLE + / KEYWORD_USINGNAMESPACE / KEYWORD_VAR / KEYWORD_VOLATILE / KEYWORD_WHILE \ No newline at end of file diff --git a/kitty b/kitty new file mode 100755 index 0000000..78d2ae8 --- /dev/null +++ b/kitty @@ -0,0 +1,17 @@ +new_tab code +layout horizontal +launch --title "code" bash + +new_tab terminal +layout horizontal +launch bash + +new_tab webdev +layout horizontal +launch bash + +new_tab ranger +enabled_layouts horizontal,stack +layout horizontal +launch ranger + diff --git a/kitty.conf b/kitty.conf new file mode 100644 index 0000000..e79a8ca --- /dev/null +++ b/kitty.conf @@ -0,0 +1,5 @@ +map alt+, launch ./scripts/run.sh +map alt+. launch ./scripts/build.sh +map alt+shift+. launch ./scripts/test.sh +map alt+shfit+, launch ./scripts/debug.sh +map alt+shift+m launch ./scripts/script2.sh diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..a15896f --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +./bootstrap.sh diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..86b46b6 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +cd 06 && ./zsh 2>&1 | less -R +F \ No newline at end of file From 56a6e78765ed8d4d68d3a8771a18aed61bbd78ba Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Tue, 1 Apr 2025 18:16:28 +0100 Subject: [PATCH 03/10] update --- .ignore => .helix/.ignore | 0 00/Makefile | 2 +- 01/Makefile | 6 +- 02/Makefile | 6 +- 03/Makefile | 6 +- 04/Makefile | 6 +- 04a/Makefile | 6 +- 05/Makefile | 2 +- 06/arith.peg | 5 - 06/kitty | 13 -- 06/test.zig | 8 +- Makefile | 8 +- markdown.c | 239 --------------------------- bootstrap.sh => scripts/bootstrap.sh | 0 kitty => scripts/kitty | 0 kitty.conf => scripts/kitty.conf | 0 16 files changed, 18 insertions(+), 289 deletions(-) rename .ignore => .helix/.ignore (100%) delete mode 100644 06/arith.peg delete mode 100755 06/kitty delete mode 100644 markdown.c rename bootstrap.sh => scripts/bootstrap.sh (100%) rename kitty => scripts/kitty (100%) rename kitty.conf => scripts/kitty.conf (100%) diff --git a/.ignore b/.helix/.ignore similarity index 100% rename from .ignore rename to .helix/.ignore diff --git a/00/Makefile b/00/Makefile index b882d3e..3dd4a18 100644 --- a/00/Makefile +++ b/00/Makefile @@ -1,4 +1,4 @@ -all: README.html out00 +all: out00 out00: in00 ./hexcompile %.html: %.md ../markdown diff --git a/01/Makefile b/01/Makefile index f40b401..585706d 100644 --- a/01/Makefile +++ b/01/Makefile @@ -1,9 +1,7 @@ -all: out01 README.html +all: out01 out01: in01 out00 ./out00 out00: in00 ../00/hexcompile -%.html: %.md ../markdown - ../markdown $< clean: - rm -f out00 out01 README.html + rm -f out00 out01 diff --git a/02/Makefile b/02/Makefile index 17b0b0e..8e9a823 100644 --- a/02/Makefile +++ b/02/Makefile @@ -1,9 +1,7 @@ -all: out01 out02 README.html +all: out01 out02 out01: in01 ../01/out00 out02: out01 in02 ./out01 -%.html: %.md ../markdown - ../markdown $< clean: - rm -f out01 out02 README.html + rm -f out01 out02 diff --git a/03/Makefile b/03/Makefile index 2a50640..5443e6e 100644 --- a/03/Makefile +++ b/03/Makefile @@ -1,9 +1,7 @@ -all: out02 out03 README.html +all: out02 out03 out02: in02 ../02/out01 ../02/out01 out03: out02 in03 ./out02 -%.html: %.md ../markdown - ../markdown $< clean: - rm -f out* README.html + rm -f out* diff --git a/04/Makefile b/04/Makefile index ae9568c..28e2dc8 100644 --- a/04/Makefile +++ b/04/Makefile @@ -1,11 +1,9 @@ -all: out03 guessing_game.out out04 README.html +all: out03 guessing_game.out out04 out03: in03 ../03/out02 ../03/out02 -%.html: %.md ../markdown - ../markdown $< out04: in04 out03 ./out03 %.out: % out03 ./out03 $< $@ clean: - rm -f out* README.html *.out + rm -f out* *.out diff --git a/04a/Makefile b/04a/Makefile index f88d708..b8df194 100644 --- a/04a/Makefile +++ b/04a/Makefile @@ -1,9 +1,7 @@ -all: out04 out04a README.html +all: out04 out04a out04: in04 ../04/out03 ../04/out03 out04a: in04a out04 ./out04 -%.html: %.md ../markdown - ../markdown $< clean: - rm -f out* README.html + rm -f out* diff --git a/05/Makefile b/05/Makefile index 74765e1..bb8e458 100644 --- a/05/Makefile +++ b/05/Makefile @@ -47,4 +47,4 @@ $(TCC)1a: $(TCCDIR)/*.c $(TCCDIR)/*.h clean: rm -rf musl-bootstrap rm -rf tcc-bootstrap - rm -f out* README.html *.out *.o $(TCCDIR)/tcc[0123456] $(TCCDIR)/tcc[0123456]a $(TCCDIR)/lib/*.[oa] + rm -f out* *.out *.o $(TCCDIR)/tcc[0123456] $(TCCDIR)/tcc[0123456]a $(TCCDIR)/lib/*.[oa] diff --git a/06/arith.peg b/06/arith.peg deleted file mode 100644 index cf994a4..0000000 --- a/06/arith.peg +++ /dev/null @@ -1,5 +0,0 @@ -Expr <- Sum -Sum <- Product (('+' / '-') Product)* -Product <- Power (('*' / '/') Power)* -Power <- Value ('^' Power)? -Value <- [0-9]+ / '(' Expr ')' \ No newline at end of file diff --git a/06/kitty b/06/kitty deleted file mode 100755 index 5a16c7f..0000000 --- a/06/kitty +++ /dev/null @@ -1,13 +0,0 @@ -new_tab code -layout horizontal -launch --title "code" bash - -new_tab terminal -layout horizontal -launch bash - -new_tab ranger -enabled_layouts horizontal,stack -layout horizontal -launch ranger - diff --git a/06/test.zig b/06/test.zig index 6e4c9c5..8ae7065 100644 --- a/06/test.zig +++ b/06/test.zig @@ -1,6 +1,4 @@ - - - pub fn main() void { - const x = 0; -} \ No newline at end of file + const x = 0; + if (x) {} +} diff --git a/Makefile b/Makefile index c9647d7..07f2436 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -all: markdown README.html +all: $(MAKE) -C 00 $(MAKE) -C 01 $(MAKE) -C 02 @@ -8,6 +8,8 @@ all: markdown README.html # don't compile all of 05 because it takes a while $(MAKE) -C 05 $(MAKE) -C 05 tcc + $(MAKE) -C 05 tcc-files + $(MAKE) -C 05 musl $(MAKE) -C 06 clean: $(MAKE) -C 00 clean @@ -20,7 +22,3 @@ clean: $(MAKE) -C 06 clean rm -f markdown rm -f README.html -markdown: markdown.c - $(CC) -O2 -o markdown -Wall -Wconversion -Wshadow -std=c89 markdown.c -README.html: markdown README.md - ./markdown README.md diff --git a/markdown.c b/markdown.c deleted file mode 100644 index a99e179..0000000 --- a/markdown.c +++ /dev/null @@ -1,239 +0,0 @@ -/* -a little program to convert markdown to html, for READMEs -I was using markdown.pl but that has some annoying problems -This doesn't support all of markdown; I'll add more as I need it. -*/ - -#include -#include -#include - -/* output text with *s for italics and stuff */ -static void output_md_text(FILE *out, int *flags, int line_number, const char *text) { - enum { - FLAG_I = 0x01, /* italics */ - FLAG_B = 0x02, - FLAG_CODE = 0x04 - }; - const char *p; - - for (p = text; *p; ++p) { - if ((*flags & FLAG_CODE) && *p != '`') { - switch (*p) { - case '<': fprintf(out, "<"); break; - case '>': fprintf(out, ">"); break; - case '&': fprintf(out, "&"); break; - default: putc(*p, out); break; - } - continue; - } - switch (*p) { - case '\\': - ++p; - if (*p == '\0') { - fprintf(stderr, "line %d: Unterminated \\.\n", line_number); - exit(-1); - } - fprintf(out, "%c", *p); - break; - case '*': - if (p[1] == '*') { - /* bold */ - if (*flags & FLAG_B) { - fprintf(out, ""); - *flags &= ~FLAG_B; - } else { - fprintf(out, ""); - *flags |= FLAG_B; - } - ++p; - } else { - /* italics */ - if (*flags & FLAG_I) { - fprintf(out, ""); - *flags &= ~FLAG_I; - } else { - fprintf(out, ""); - *flags |= FLAG_I; - } - } - break; - case '`': - /* code */ - if (*flags & FLAG_CODE) { - fprintf(out, ""); - *flags &= ~FLAG_CODE; - } else { - fprintf(out, ""); - *flags |= FLAG_CODE; - } - break; - case '[': { - /* link */ - char url2[256] = {0}; - const char *label, *url, *label_end, *url_end; - char *dot; - int n_label, n_url; - - label = p+1; - label_end = strchr(label, ']'); - if (!label_end) { - fprintf(stderr, "line %d: Unterminated link.\n", line_number); - exit(-1); - } - if (label_end[1] != '(') { - fprintf(stderr, "line %d: Bad link syntax.\n", line_number); - exit(-1); - } - url = label_end + 2; - url_end = strchr(url, ')'); - if (!url_end) { - fprintf(stderr, "line %d: Unterminated URL.\n", line_number); - exit(-1); - } - - n_label = (int)(label_end - label); - n_url = (int)(url_end - url); - if (n_url > sizeof url2-8) - n_url = sizeof url2-8; - sprintf(url2, "%.*s", n_url, url); - dot = strrchr(url2, '.'); - if (dot && strcmp(dot, ".md") == 0) { - /* replace links to md files with links to html files */ - strcpy(dot, ".html"); - } - fprintf(out, "%.*s", - url2, n_label, label); - p = url_end; - } break; - case '-': - if (p[1] == '-') { - /* em dash */ - fprintf(out, "—"); - ++p; - } else { - goto default_case; - } - break; - default: - default_case: - putc(*p, out); - break; - } - } -} - -int main(int argc, char **argv) { - FILE *in, *out; - char line[1024] = {0}; - char title[256] = {0}; - int flags = 0, txtflags = 0; - int line_number = 0; - enum { - FLAG_UL = 1 - }; - - if (argc < 2) { - fprintf(stderr, "Please provide an input file.\n"); - return -1; - } - - { - const char *in_filename = argv[1]; - char out_filename[256] = {0}; - char *dot; - strncpy(out_filename, argv[1], 200); - dot = strrchr(out_filename, '.'); - if (!dot || strcmp(dot, ".md") != 0) { - fprintf(stderr, "Input filename does not end in .md\n"); - return -1; - } - *dot = '\0'; - strcpy(title, out_filename); - strcpy(dot, ".html"); - - - in = fopen(in_filename, "rb"); - out = fopen(out_filename, "wb"); - } - - if (!in) { - perror("Couldn't open input file"); - return -1; - } - if (!out) { - perror("Couldn't open output file"); - return -1; - } - - fprintf(out, - "\n" - "\n" - "\n" - "\n" - "\n" - "\n" - "%s\n" - "\n" - "\n" - "

\n", title - ); - while (fgets(line, sizeof line, in)) { - ++line_number; - line[strcspn(line, "\r\n")] = '\0'; - - if (line[0] == '#') { - /* heading */ - int n = 1; - while (line[n] == '#') ++n; - fprintf(out, "

", n); - output_md_text(out, &txtflags, line_number, line + n); - fprintf(out, "

\n", n); - } else if (line[0] == '\0') { - if (flags & FLAG_UL) { - fprintf(out, "\n"); - flags &= ~FLAG_UL; - } - fprintf(out, "

\n

\n"); - } else if (strncmp(line, "- ", 2) == 0) { - /* bullet */ - if (flags & FLAG_UL) { - fprintf(out, "

  • "); - } else { - fprintf(out, "
    • "); - flags |= FLAG_UL; - } - output_md_text(out, &txtflags, line_number, line + 2); - fprintf(out, "\n"); - } else if (strncmp(line, "```", 3) == 0) { - fprintf(out, "
      \n");
      -			
      -			while (fgets(line, sizeof line, in)) {
      -				char *p;
      -				++line_number;
      -				if (strncmp(line, "```", 3) == 0)
      -					break;
      -				for (p = line; *p; ++p) {
      -					switch (*p) {
      -					case '<': fprintf(out, "<"); break;
      -					case '>': fprintf(out, ">"); break;
      -					case '&': fprintf(out, "&"); break;
      -					default: fputc(*p, out); break;
      -					}
      -				}
      -			}
      -
      -			fprintf(out, "
      \n"); - } else { - output_md_text(out, &txtflags, line_number, line); - fprintf(out, "\n"); - } - - - - } - fprintf(out, "

      \n\n\n"); - return 0; -} diff --git a/bootstrap.sh b/scripts/bootstrap.sh similarity index 100% rename from bootstrap.sh rename to scripts/bootstrap.sh diff --git a/kitty b/scripts/kitty similarity index 100% rename from kitty rename to scripts/kitty diff --git a/kitty.conf b/scripts/kitty.conf similarity index 100% rename from kitty.conf rename to scripts/kitty.conf From 35a88970c28f57c496cf0c76ed31448124c44506 Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Sat, 5 Apr 2025 10:55:40 +0100 Subject: [PATCH 04/10] working on it --- .helix/.ignore | 10 - 05/Makefile | 16 +- 05/musl-0.6.0/config.mak | 2 +- 05/musl-final/.gitignore | 3 + 05/musl-final/COPYING | 504 +++ 05/musl-final/COPYRIGHT | 40 + 05/musl-final/INSTALL | 58 + 05/musl-final/Makefile | 113 + 05/musl-final/README | 44 + 05/musl-final/WHATSNEW | 40 + 05/musl-final/arch/i386/atomic.h | 110 + 05/musl-final/arch/i386/bits/alltypes.h.sh | 131 + 05/musl-final/arch/i386/bits/endian.h | 1 + 05/musl-final/arch/i386/bits/errno.h | 132 + 05/musl-final/arch/i386/bits/fcntl.h | 60 + 05/musl-final/arch/i386/bits/fenv.h | 34 + 05/musl-final/arch/i386/bits/float.h | 11 + 05/musl-final/arch/i386/bits/in.h | 133 + 05/musl-final/arch/i386/bits/ioctl.h | 197 + 05/musl-final/arch/i386/bits/ipc.h | 25 + 05/musl-final/arch/i386/bits/limits.h | 33 + 05/musl-final/arch/i386/bits/mman.h | 50 + 05/musl-final/arch/i386/bits/posix.h | 2 + 05/musl-final/arch/i386/bits/pthread.h | 6 + 05/musl-final/arch/i386/bits/reg.h | 19 + 05/musl-final/arch/i386/bits/setjmp.h | 1 + 05/musl-final/arch/i386/bits/shm.h | 24 + 05/musl-final/arch/i386/bits/signal.h | 107 + 05/musl-final/arch/i386/bits/socket.h | 210 + 05/musl-final/arch/i386/bits/stat.h | 22 + 05/musl-final/arch/i386/bits/statfs.h | 16 + 05/musl-final/arch/i386/bits/stdarg.h | 9 + 05/musl-final/arch/i386/bits/stdint.h | 23 + 05/musl-final/arch/i386/bits/sysmacros.h | 7 + 05/musl-final/arch/i386/bits/tcp.h | 1 + 05/musl-final/arch/i386/bits/termios.h | 158 + 05/musl-final/arch/i386/bits/user.h | 77 + 05/musl-final/arch/i386/bits/wait.h | 13 + 05/musl-final/arch/i386/bits/wexitstatus.h | 9 + 05/musl-final/arch/i386/pthread_arch.h | 6 + 05/musl-final/arch/i386/syscall.h | 541 +++ 05/musl-final/arch/x86_64/atomic.h | 109 + 05/musl-final/arch/x86_64/bits/alltypes.h | 468 +++ 05/musl-final/arch/x86_64/bits/alltypes.h.sh | 142 + 05/musl-final/arch/x86_64/bits/endian.h | 1 + 05/musl-final/arch/x86_64/bits/errno.h | 132 + 05/musl-final/arch/x86_64/bits/fcntl.h | 60 + 05/musl-final/arch/x86_64/bits/fenv.h | 34 + 05/musl-final/arch/x86_64/bits/float.h | 11 + 05/musl-final/arch/x86_64/bits/in.h | 133 + 05/musl-final/arch/x86_64/bits/ioctl.h | 197 + 05/musl-final/arch/x86_64/bits/ipc.h | 25 + 05/musl-final/arch/x86_64/bits/limits.h | 34 + 05/musl-final/arch/x86_64/bits/mman.h | 50 + 05/musl-final/arch/x86_64/bits/posix.h | 2 + 05/musl-final/arch/x86_64/bits/pthread.h | 6 + 05/musl-final/arch/x86_64/bits/reg.h | 29 + 05/musl-final/arch/x86_64/bits/setjmp.h | 1 + 05/musl-final/arch/x86_64/bits/shm.h | 24 + 05/musl-final/arch/x86_64/bits/signal.h | 115 + 05/musl-final/arch/x86_64/bits/socket.h | 210 + 05/musl-final/arch/x86_64/bits/stat.h | 42 + 05/musl-final/arch/x86_64/bits/statfs.h | 15 + 05/musl-final/arch/x86_64/bits/stdarg.h | 79 + 05/musl-final/arch/x86_64/bits/stdint.h | 23 + 05/musl-final/arch/x86_64/bits/sysmacros.h | 7 + 05/musl-final/arch/x86_64/bits/tcp.h | 1 + 05/musl-final/arch/x86_64/bits/termios.h | 158 + 05/musl-final/arch/x86_64/bits/user.h | 77 + 05/musl-final/arch/x86_64/bits/wait.h | 13 + 05/musl-final/arch/x86_64/bits/wexitstatus.h | 9 + 05/musl-final/arch/x86_64/pthread_arch.h | 6 + 05/musl-final/arch/x86_64/syscall.h | 433 +++ 05/musl-final/config.mak | 27 + 05/musl-final/crt/crt1.c | 0 05/musl-final/crt/crti.c | 0 05/musl-final/crt/crtn.c | 0 05/musl-final/crt/i386/crt1.s | 17 + 05/musl-final/crt/x86_64/crt1.s | 16 + 05/musl-final/dist/config.mak | 25 + 05/musl-final/include/alloca.h | 9 + 05/musl-final/include/arpa/inet.h | 33 + 05/musl-final/include/arpa/nameser.h | 465 +++ 05/musl-final/include/arpa/telnet.h | 251 ++ 05/musl-final/include/assert.h | 17 + 05/musl-final/include/bits | 1 + 05/musl-final/include/byteswap.h | 0 05/musl-final/include/cpio.h | 29 + 05/musl-final/include/ctype.h | 56 + 05/musl-final/include/dirent.h | 68 + 05/musl-final/include/elf.h | 2524 +++++++++++++ 05/musl-final/include/endian.h | 17 + 05/musl-final/include/errno.h | 18 + 05/musl-final/include/fcntl.h | 43 + 05/musl-final/include/features.h | 1 + 05/musl-final/include/fenv.h | 28 + 05/musl-final/include/float.h | 34 + 05/musl-final/include/fnmatch.h | 25 + 05/musl-final/include/ftw.h | 31 + 05/musl-final/include/getopt.h | 33 + 05/musl-final/include/glob.h | 41 + 05/musl-final/include/grp.h | 35 + 05/musl-final/include/iconv.h | 22 + 05/musl-final/include/inttypes.h | 227 ++ 05/musl-final/include/iso646.h | 20 + 05/musl-final/include/langinfo.h | 87 + 05/musl-final/include/libgen.h | 15 + 05/musl-final/include/libintl.h | 25 + 05/musl-final/include/limits.h | 109 + 05/musl-final/include/linux/loop.h | 69 + 05/musl-final/include/linux/version.h | 2 + 05/musl-final/include/locale.h | 85 + 05/musl-final/include/malloc.h | 0 05/musl-final/include/math.h | 302 ++ 05/musl-final/include/mntent.h | 43 + 05/musl-final/include/net/ethernet.h | 47 + 05/musl-final/include/net/if.h | 122 + 05/musl-final/include/net/if_arp.h | 133 + 05/musl-final/include/net/route.h | 116 + 05/musl-final/include/netdb.h | 148 + 05/musl-final/include/netinet/icmp6.h | 305 ++ 05/musl-final/include/netinet/if_ether.h | 108 + 05/musl-final/include/netinet/in.h | 158 + 05/musl-final/include/netinet/ip.h | 186 + 05/musl-final/include/netinet/ip6.h | 142 + 05/musl-final/include/netinet/ip_icmp.h | 192 + 05/musl-final/include/netinet/tcp.h | 6 + 05/musl-final/include/netinet/udp.h | 35 + 05/musl-final/include/nl_types.h | 24 + 05/musl-final/include/paths.h | 40 + 05/musl-final/include/poll.h | 35 + 05/musl-final/include/pthread.h | 185 + 05/musl-final/include/pty.h | 17 + 05/musl-final/include/pwd.h | 38 + 05/musl-final/include/regex.h | 59 + 05/musl-final/include/resolv.h | 143 + 05/musl-final/include/sched.h | 37 + 05/musl-final/include/search.h | 6 + 05/musl-final/include/semaphore.h | 27 + 05/musl-final/include/setjmp.h | 38 + 05/musl-final/include/shadow.h | 44 + 05/musl-final/include/signal.h | 105 + 05/musl-final/include/stdarg.h | 87 + 05/musl-final/include/stdbool.h | 13 + 05/musl-final/include/stddef.h | 19 + 05/musl-final/include/stdint.h | 107 + 05/musl-final/include/stdio.h | 163 + 05/musl-final/include/stdlib.h | 134 + 05/musl-final/include/string.h | 78 + 05/musl-final/include/strings.h | 29 + 05/musl-final/include/stropts.h | 16 + 05/musl-final/include/sys/epoll.h | 61 + 05/musl-final/include/sys/file.h | 21 + 05/musl-final/include/sys/ioctl.h | 14 + 05/musl-final/include/sys/ipc.h | 23 + 05/musl-final/include/sys/kd.h | 8 + 05/musl-final/include/sys/klog.h | 14 + 05/musl-final/include/sys/mman.h | 37 + 05/musl-final/include/sys/mount.h | 28 + 05/musl-final/include/sys/msg.h | 36 + 05/musl-final/include/sys/mtio.h | 276 ++ 05/musl-final/include/sys/param.h | 15 + 05/musl-final/include/sys/poll.h | 1 + 05/musl-final/include/sys/prctl.h | 64 + 05/musl-final/include/sys/procfs.h | 81 + 05/musl-final/include/sys/ptrace.h | 77 + 05/musl-final/include/sys/reboot.h | 18 + 05/musl-final/include/sys/reg.h | 9 + 05/musl-final/include/sys/resource.h | 74 + 05/musl-final/include/sys/select.h | 34 + 05/musl-final/include/sys/sem.h | 69 + 05/musl-final/include/sys/shm.h | 18 + 05/musl-final/include/sys/signalfd.h | 40 + 05/musl-final/include/sys/socket.h | 65 + 05/musl-final/include/sys/soundcard.h | 1 + 05/musl-final/include/sys/stat.h | 95 + 05/musl-final/include/sys/statfs.h | 10 + 05/musl-final/include/sys/statvfs.h | 30 + 05/musl-final/include/sys/stropts.h | 1 + 05/musl-final/include/sys/swap.h | 11 + 05/musl-final/include/sys/sysctl.h | 9 + 05/musl-final/include/sys/sysinfo.h | 29 + 05/musl-final/include/sys/sysmacros.h | 6 + 05/musl-final/include/sys/time.h | 49 + 05/musl-final/include/sys/times.h | 17 + 05/musl-final/include/sys/types.h | 70 + 05/musl-final/include/sys/ucontext.h | 1 + 05/musl-final/include/sys/uio.h | 13 + 05/musl-final/include/sys/un.h | 13 + 05/musl-final/include/sys/user.h | 15 + 05/musl-final/include/sys/utsname.h | 21 + 05/musl-final/include/sys/vfs.h | 1 + 05/musl-final/include/sys/vt.h | 1 + 05/musl-final/include/sys/wait.h | 28 + 05/musl-final/include/syslog.h | 54 + 05/musl-final/include/tar.h | 33 + 05/musl-final/include/termios.h | 39 + 05/musl-final/include/time.h | 114 + 05/musl-final/include/ucontext.h | 38 + 05/musl-final/include/ulimit.h | 17 + 05/musl-final/include/unistd.h | 468 +++ 05/musl-final/include/utime.h | 24 + 05/musl-final/include/utmp.h | 23 + 05/musl-final/include/utmpx.h | 52 + 05/musl-final/include/wchar.h | 160 + 05/musl-final/include/wctype.h | 45 + 05/musl-final/include/wordexp.h | 40 + 05/musl-final/src/conf/confstr.c | 17 + 05/musl-final/src/conf/fpathconf.c | 35 + 05/musl-final/src/conf/pathconf.c | 6 + 05/musl-final/src/conf/sysconf.c | 222 ++ .../src/ctype/__ctype_get_mb_cur_max.c | 6 + 05/musl-final/src/ctype/isalnum.c | 6 + 05/musl-final/src/ctype/isalpha.c | 7 + 05/musl-final/src/ctype/isascii.c | 6 + 05/musl-final/src/ctype/isblank.c | 6 + 05/musl-final/src/ctype/iscntrl.c | 6 + 05/musl-final/src/ctype/isdigit.c | 7 + 05/musl-final/src/ctype/isgraph.c | 4 + 05/musl-final/src/ctype/islower.c | 7 + 05/musl-final/src/ctype/isprint.c | 4 + 05/musl-final/src/ctype/ispunct.c | 6 + 05/musl-final/src/ctype/isspace.c | 6 + 05/musl-final/src/ctype/isupper.c | 7 + 05/musl-final/src/ctype/iswalnum.c | 9 + 05/musl-final/src/ctype/iswalpha.c | 6 + 05/musl-final/src/ctype/iswblank.c | 8 + 05/musl-final/src/ctype/iswcntrl.c | 10 + 05/musl-final/src/ctype/iswctype.c | 63 + 05/musl-final/src/ctype/iswdigit.c | 9 + 05/musl-final/src/ctype/iswgraph.c | 7 + 05/musl-final/src/ctype/iswlower.c | 6 + 05/musl-final/src/ctype/iswprint.c | 10 + 05/musl-final/src/ctype/iswpunct.c | 138 + 05/musl-final/src/ctype/iswspace.c | 15 + 05/musl-final/src/ctype/iswupper.c | 6 + 05/musl-final/src/ctype/iswxdigit.c | 7 + 05/musl-final/src/ctype/isxdigit.c | 6 + 05/musl-final/src/ctype/toascii.c | 7 + 05/musl-final/src/ctype/tolower.c | 7 + 05/musl-final/src/ctype/toupper.c | 7 + 05/musl-final/src/ctype/towctrans.c | 246 ++ 05/musl-final/src/ctype/wcswidth.c | 8 + 05/musl-final/src/ctype/wctrans.c | 16 + 05/musl-final/src/ctype/wcwidth.c | 186 + 05/musl-final/src/dirent/__dirent.h | 9 + 05/musl-final/src/dirent/__getdents.c | 12 + 05/musl-final/src/dirent/alphasort.c | 10 + 05/musl-final/src/dirent/closedir.c | 11 + 05/musl-final/src/dirent/dirfd.c | 7 + 05/musl-final/src/dirent/fdopendir.c | 26 + 05/musl-final/src/dirent/opendir.c | 25 + 05/musl-final/src/dirent/readdir.c | 32 + 05/musl-final/src/dirent/readdir_r.c | 30 + 05/musl-final/src/dirent/rewinddir.c | 13 + 05/musl-final/src/dirent/scandir.c | 50 + 05/musl-final/src/dirent/seekdir.c | 12 + 05/musl-final/src/dirent/telldir.c | 7 + 05/musl-final/src/env/__environ.c | 7 + 05/musl-final/src/env/__libc_start_main.c | 30 + 05/musl-final/src/env/clearenv.c | 9 + 05/musl-final/src/env/getenv.c | 14 + 05/musl-final/src/env/putenv.c | 59 + 05/musl-final/src/env/setenv.c | 31 + 05/musl-final/src/env/unsetenv.c | 32 + 05/musl-final/src/errno/__errno_location.c | 11 + 05/musl-final/src/errno/__strerror.h | 101 + 05/musl-final/src/errno/strerror.c | 22 + 05/musl-final/src/exit/_Exit.c | 9 + 05/musl-final/src/exit/abort.c | 8 + 05/musl-final/src/exit/assert.c | 9 + 05/musl-final/src/exit/atexit.c | 57 + 05/musl-final/src/exit/exit.c | 33 + 05/musl-final/src/fcntl/creat.c | 9 + 05/musl-final/src/fcntl/fcntl.c | 22 + 05/musl-final/src/fcntl/open.c | 21 + 05/musl-final/src/fcntl/openat.c | 21 + 05/musl-final/src/internal/futex.h | 16 + 05/musl-final/src/internal/libc.c | 3 + 05/musl-final/src/internal/libc.h | 43 + 05/musl-final/src/internal/locale_impl.h | 5 + 05/musl-final/src/internal/pthread_impl.h | 79 + 05/musl-final/src/internal/stdio_impl.h | 100 + 05/musl-final/src/internal/syscall.c | 11 + 05/musl-final/src/ipc/ftok.c | 10 + 05/musl-final/src/ipc/ipc.h | 13 + 05/musl-final/src/ipc/semctl.c | 18 + 05/musl-final/src/ipc/semget.c | 12 + 05/musl-final/src/ipc/semop.c | 12 + 05/musl-final/src/ipc/shmat.c | 17 + 05/musl-final/src/ipc/shmctl.c | 12 + 05/musl-final/src/ipc/shmdt.c | 12 + 05/musl-final/src/ipc/shmget.c | 12 + 05/musl-final/src/linux/brk.c | 6 + 05/musl-final/src/linux/chroot.c | 7 + 05/musl-final/src/linux/daemon.c | 32 + 05/musl-final/src/linux/epoll_create.c | 7 + 05/musl-final/src/linux/epoll_create1.c | 7 + 05/musl-final/src/linux/epoll_ctl.c | 7 + 05/musl-final/src/linux/epoll_pwait.c | 7 + 05/musl-final/src/linux/epoll_wait.c | 7 + 05/musl-final/src/linux/getdtablesize.c | 9 + 05/musl-final/src/linux/gethostid.c | 4 + 05/musl-final/src/linux/getopt_long.c | 52 + 05/musl-final/src/linux/getpagesize.c | 7 + 05/musl-final/src/linux/getpass.c | 39 + 05/musl-final/src/linux/initgroups.c | 15 + 05/musl-final/src/linux/klogctl.c | 6 + 05/musl-final/src/linux/mntent.c | 57 + 05/musl-final/src/linux/mount.c | 7 + 05/musl-final/src/linux/prctl.c | 13 + 05/musl-final/src/linux/reboot.c | 8 + 05/musl-final/src/linux/sbrk.c | 7 + 05/musl-final/src/linux/sendfile.c | 10 + 05/musl-final/src/linux/setgroups.c | 7 + 05/musl-final/src/linux/sethostname.c | 7 + 05/musl-final/src/linux/settimeofday.c | 7 + 05/musl-final/src/linux/signalfd.c | 7 + 05/musl-final/src/linux/stime.c | 7 + 05/musl-final/src/linux/swapoff.c | 7 + 05/musl-final/src/linux/swapon.c | 7 + 05/musl-final/src/linux/sysinfo.c | 8 + 05/musl-final/src/linux/umount.c | 7 + 05/musl-final/src/linux/umount2.c | 7 + 05/musl-final/src/linux/utimes.c | 12 + 05/musl-final/src/linux/wait3.c | 10 + 05/musl-final/src/linux/wait4.c | 18 + 05/musl-final/src/locale/catclose.c | 6 + 05/musl-final/src/locale/catgets.c | 6 + 05/musl-final/src/locale/catopen.c | 6 + 05/musl-final/src/locale/duplocale.c | 11 + 05/musl-final/src/locale/freelocale.c | 7 + 05/musl-final/src/locale/iconv.c | 568 +++ 05/musl-final/src/locale/intl.c | 67 + 05/musl-final/src/locale/isalnum_l.c | 6 + 05/musl-final/src/locale/isalpha_l.c | 6 + 05/musl-final/src/locale/isblank_l.c | 6 + 05/musl-final/src/locale/iscntrl_l.c | 6 + 05/musl-final/src/locale/isdigit_l.c | 6 + 05/musl-final/src/locale/isgraph_l.c | 6 + 05/musl-final/src/locale/islower_l.c | 6 + 05/musl-final/src/locale/isprint_l.c | 6 + 05/musl-final/src/locale/ispunct_l.c | 6 + 05/musl-final/src/locale/isspace_l.c | 6 + 05/musl-final/src/locale/isupper_l.c | 6 + 05/musl-final/src/locale/isxdigit_l.c | 6 + 05/musl-final/src/locale/langinfo.c | 58 + 05/musl-final/src/locale/localeconv.c | 22 + 05/musl-final/src/locale/newlocale.c | 11 + 05/musl-final/src/locale/nl_langinfo.c | 13 + 05/musl-final/src/locale/setlocale.c | 9 + 05/musl-final/src/locale/strcoll.c | 6 + 05/musl-final/src/locale/strxfrm.c | 9 + 05/musl-final/src/locale/tmp | 390 ++ 05/musl-final/src/locale/tolower_l.c | 6 + 05/musl-final/src/locale/toupper_l.c | 6 + 05/musl-final/src/locale/wcscoll.c | 7 + 05/musl-final/src/locale/wcsxfrm.c | 12 + 05/musl-final/src/malloc/DESIGN | 22 + 05/musl-final/src/malloc/__brk.c | 7 + 05/musl-final/src/malloc/__simple_malloc.c | 44 + 05/musl-final/src/malloc/calloc.c | 23 + 05/musl-final/src/malloc/malloc.c | 515 +++ 05/musl-final/src/malloc/memalign.c | 13 + 05/musl-final/src/malloc/posix_memalign.c | 47 + 05/musl-final/src/math/__fpclassify.c | 14 + 05/musl-final/src/math/__fpclassifyf.c | 14 + 05/musl-final/src/math/__fpclassifyl.c | 16 + 05/musl-final/src/math/__log1p.h | 94 + 05/musl-final/src/math/__log1pf.h | 35 + 05/musl-final/src/math/__x86_64/e_sqrt.s | 3 + 05/musl-final/src/math/__x86_64/e_sqrtf.s | 3 + 05/musl-final/src/math/e_acos.c | 99 + 05/musl-final/src/math/e_acosf.c | 77 + 05/musl-final/src/math/e_acosh.c | 59 + 05/musl-final/src/math/e_acoshf.c | 45 + 05/musl-final/src/math/e_asin.c | 109 + 05/musl-final/src/math/e_asinf.c | 80 + 05/musl-final/src/math/e_atan2.c | 120 + 05/musl-final/src/math/e_atan2f.c | 93 + 05/musl-final/src/math/e_atanh.c | 59 + 05/musl-final/src/math/e_atanhf.c | 42 + 05/musl-final/src/math/e_cosh.c | 82 + 05/musl-final/src/math/e_coshf.c | 59 + 05/musl-final/src/math/e_exp.c | 155 + 05/musl-final/src/math/e_expf.c | 91 + 05/musl-final/src/math/e_fmod.c | 129 + 05/musl-final/src/math/e_fmodf.c | 101 + 05/musl-final/src/math/e_hypot.c | 121 + 05/musl-final/src/math/e_hypotf.c | 79 + 05/musl-final/src/math/e_log.c | 131 + 05/musl-final/src/math/e_log10.c | 83 + 05/musl-final/src/math/e_log10f.c | 51 + 05/musl-final/src/math/e_logf.c | 81 + 05/musl-final/src/math/e_pow.c | 300 ++ 05/musl-final/src/math/e_powf.c | 243 ++ 05/musl-final/src/math/e_rem_pio2.c | 163 + 05/musl-final/src/math/e_rem_pio2f.c | 175 + 05/musl-final/src/math/e_remainder.c | 69 + 05/musl-final/src/math/e_remainderf.c | 61 + 05/musl-final/src/math/e_scalb.c | 35 + 05/musl-final/src/math/e_scalbf.c | 31 + 05/musl-final/src/math/e_sinh.c | 75 + 05/musl-final/src/math/e_sinhf.c | 56 + 05/musl-final/src/math/e_sqrt.c | 442 +++ 05/musl-final/src/math/e_sqrtf.c | 85 + 05/musl-final/src/math/i386/e_exp.s | 36 + 05/musl-final/src/math/i386/e_expf.s | 1 + 05/musl-final/src/math/i386/e_log.s | 6 + 05/musl-final/src/math/i386/e_log10.s | 6 + 05/musl-final/src/math/i386/e_log10f.s | 6 + 05/musl-final/src/math/i386/e_logf.s | 6 + 05/musl-final/src/math/i386/e_remainder.s | 16 + 05/musl-final/src/math/i386/e_remainderf.s | 0 05/musl-final/src/math/i386/e_sqrt.s | 4 + 05/musl-final/src/math/i386/e_sqrtf.s | 4 + 05/musl-final/src/math/i386/s_ceil.s | 0 05/musl-final/src/math/i386/s_ceilf.s | 0 05/musl-final/src/math/i386/s_fabs.s | 5 + 05/musl-final/src/math/i386/s_fabsf.s | 5 + 05/musl-final/src/math/i386/s_floor.s | 0 05/musl-final/src/math/i386/s_floorf.s | 0 05/musl-final/src/math/i386/s_ldexp.s | 0 05/musl-final/src/math/i386/s_ldexpf.s | 0 05/musl-final/src/math/i386/s_rint.s | 5 + 05/musl-final/src/math/i386/s_rintf.s | 5 + 05/musl-final/src/math/i386/s_scalbln.s | 11 + 05/musl-final/src/math/i386/s_scalblnf.s | 11 + 05/musl-final/src/math/i386/s_trunc.s | 36 + 05/musl-final/src/math/i386/s_truncf.s | 0 05/musl-final/src/math/k_cos.c | 85 + 05/musl-final/src/math/k_cosf.c | 52 + 05/musl-final/src/math/k_rem_pio2.c | 300 ++ 05/musl-final/src/math/k_rem_pio2f.c | 192 + 05/musl-final/src/math/k_sin.c | 68 + 05/musl-final/src/math/k_sinf.c | 42 + 05/musl-final/src/math/k_tan.c | 149 + 05/musl-final/src/math/k_tanf.c | 105 + 05/musl-final/src/math/log.c | 142 + 05/musl-final/src/math/log10.c | 86 + 05/musl-final/src/math/log10f.c | 72 + 05/musl-final/src/math/log10l.c | 186 + 05/musl-final/src/math/log1p.c | 172 + 05/musl-final/src/math/log1pf.c | 112 + 05/musl-final/src/math/log1pl.c | 176 + 05/musl-final/src/math/log2.c | 109 + 05/musl-final/src/math/log2f.c | 82 + 05/musl-final/src/math/log2l.c | 182 + 05/musl-final/src/math/logb.c | 20 + 05/musl-final/src/math/logbf.c | 12 + 05/musl-final/src/math/logbl.c | 19 + 05/musl-final/src/math/logf.c | 90 + 05/musl-final/src/math/logl.c | 174 + 05/musl-final/src/math/math_private.h | 143 + 05/musl-final/src/math/s_asinh.c | 53 + 05/musl-final/src/math/s_asinhf.c | 45 + 05/musl-final/src/math/s_atan.c | 115 + 05/musl-final/src/math/s_atanf.c | 95 + 05/musl-final/src/math/s_cbrt.c | 77 + 05/musl-final/src/math/s_cbrtf.c | 67 + 05/musl-final/src/math/s_ceil.c | 68 + 05/musl-final/src/math/s_ceilf.c | 49 + 05/musl-final/src/math/s_copysign.c | 30 + 05/musl-final/src/math/s_copysignf.c | 33 + 05/musl-final/src/math/s_cos.c | 74 + 05/musl-final/src/math/s_cosf.c | 47 + 05/musl-final/src/math/s_erf.c | 298 ++ 05/musl-final/src/math/s_erff.c | 207 + 05/musl-final/src/math/s_expm1.c | 217 ++ 05/musl-final/src/math/s_expm1f.c | 122 + 05/musl-final/src/math/s_fabs.c | 27 + 05/musl-final/src/math/s_fabsf.c | 30 + 05/musl-final/src/math/s_floor.c | 69 + 05/musl-final/src/math/s_floorf.c | 58 + 05/musl-final/src/math/s_ilogb.c | 45 + 05/musl-final/src/math/s_ilogbf.c | 37 + 05/musl-final/src/math/s_ldexp.c | 6 + 05/musl-final/src/math/s_ldexpf.c | 6 + 05/musl-final/src/math/s_llrint.c | 8 + 05/musl-final/src/math/s_log1p.c | 157 + 05/musl-final/src/math/s_log1pf.c | 96 + 05/musl-final/src/math/s_logb.c | 34 + 05/musl-final/src/math/s_logbf.c | 31 + 05/musl-final/src/math/s_lrint.c | 8 + 05/musl-final/src/math/s_lrintf.c | 8 + 05/musl-final/src/math/s_modf.c | 71 + 05/musl-final/src/math/s_modff.c | 52 + 05/musl-final/src/math/s_nextafter.c | 72 + 05/musl-final/src/math/s_nextafterf.c | 63 + 05/musl-final/src/math/s_remquo.c | 149 + 05/musl-final/src/math/s_remquof.c | 118 + 05/musl-final/src/math/s_rint.c | 80 + 05/musl-final/src/math/s_rintf.c | 45 + 05/musl-final/src/math/s_round.c | 48 + 05/musl-final/src/math/s_roundf.c | 48 + 05/musl-final/src/math/s_scalbln.c | 61 + 05/musl-final/src/math/s_scalblnf.c | 57 + 05/musl-final/src/math/s_sin.c | 74 + 05/musl-final/src/math/s_sinf.c | 45 + 05/musl-final/src/math/s_tan.c | 68 + 05/musl-final/src/math/s_tanf.c | 40 + 05/musl-final/src/math/s_tanh.c | 74 + 05/musl-final/src/math/s_tanhf.c | 52 + 05/musl-final/src/math/s_trunc.c | 58 + 05/musl-final/src/math/s_truncf.c | 50 + 05/musl-final/src/misc/basename.c | 12 + 05/musl-final/src/misc/bswap_32.c | 7 + 05/musl-final/src/misc/bswap_64.c | 9 + 05/musl-final/src/misc/crypt.c | 2578 +++++++++++++ 05/musl-final/src/misc/cuserid.c | 14 + 05/musl-final/src/misc/dirname.c | 15 + 05/musl-final/src/misc/ffs.c | 9 + 05/musl-final/src/misc/ftw.c | 9 + 05/musl-final/src/misc/getdomainname.c | 9 + 05/musl-final/src/misc/getgrouplist.c | 11 + 05/musl-final/src/misc/getopt.c | 63 + 05/musl-final/src/misc/getpriority.c | 9 + 05/musl-final/src/misc/getrlimit.c | 15 + 05/musl-final/src/misc/getrusage.c | 20 + 05/musl-final/src/misc/getsubopt.c | 23 + 05/musl-final/src/misc/ioctl.c | 13 + 05/musl-final/src/misc/lockf.c | 33 + 05/musl-final/src/misc/nftw.c | 121 + 05/musl-final/src/misc/openpty.c | 33 + 05/musl-final/src/misc/pty.c | 35 + 05/musl-final/src/misc/realpath.c | 6 + 05/musl-final/src/misc/sched_yield.c | 10 + 05/musl-final/src/misc/setpriority.c | 7 + 05/musl-final/src/misc/setrlimit.c | 11 + 05/musl-final/src/misc/syslog.c | 115 + 05/musl-final/src/misc/uname.c | 8 + 05/musl-final/src/mman/madvise.c | 13 + 05/musl-final/src/mman/mlock.c | 7 + 05/musl-final/src/mman/mlockall.c | 7 + 05/musl-final/src/mman/mmap.c | 32 + 05/musl-final/src/mman/mprotect.c | 7 + 05/musl-final/src/mman/mremap.c | 19 + 05/musl-final/src/mman/msync.c | 8 + 05/musl-final/src/mman/munlock.c | 7 + 05/musl-final/src/mman/munlockall.c | 7 + 05/musl-final/src/mman/munmap.c | 14 + 05/musl-final/src/mman/posix_madvise.c | 6 + 05/musl-final/src/multibyte/btowc.c | 7 + 05/musl-final/src/multibyte/decode.c | 47 + 05/musl-final/src/multibyte/internal.c | 60 + 05/musl-final/src/multibyte/internal.h | 61 + 05/musl-final/src/multibyte/mblen.c | 17 + 05/musl-final/src/multibyte/mbrlen.c | 18 + 05/musl-final/src/multibyte/mbrtowc.c | 58 + 05/musl-final/src/multibyte/mbsinit.c | 17 + 05/musl-final/src/multibyte/mbsnrtowcs.c | 61 + 05/musl-final/src/multibyte/mbsrtowcs.c | 121 + 05/musl-final/src/multibyte/mbstowcs.c | 18 + 05/musl-final/src/multibyte/mbtowc.c | 19 + 05/musl-final/src/multibyte/wcrtomb.c | 38 + 05/musl-final/src/multibyte/wcsnrtombs.c | 51 + 05/musl-final/src/multibyte/wcsrtombs.c | 58 + 05/musl-final/src/multibyte/wcstombs.c | 17 + 05/musl-final/src/multibyte/wctob.c | 8 + 05/musl-final/src/multibyte/wctomb.c | 18 + 05/musl-final/src/network/__dns.c | 267 ++ 05/musl-final/src/network/__dns.h | 14 + 05/musl-final/src/network/__ipparse.c | 40 + 05/musl-final/src/network/accept.c | 12 + 05/musl-final/src/network/bind.c | 7 + 05/musl-final/src/network/connect.c | 12 + 05/musl-final/src/network/dn_expand.c | 28 + 05/musl-final/src/network/ent.c | 26 + 05/musl-final/src/network/freeaddrinfo.c | 7 + 05/musl-final/src/network/gai_strerror.c | 21 + 05/musl-final/src/network/getaddrinfo.c | 224 ++ 05/musl-final/src/network/gethostbyaddr.c | 15 + 05/musl-final/src/network/gethostbyaddr_r.c | 71 + 05/musl-final/src/network/gethostbyname.c | 63 + 05/musl-final/src/network/gethostbyname2.c | 16 + 05/musl-final/src/network/gethostbyname2_r.c | 99 + 05/musl-final/src/network/gethostbyname_r.c | 11 + 05/musl-final/src/network/getnameinfo.c | 54 + 05/musl-final/src/network/getpeername.c | 7 + 05/musl-final/src/network/getservbyname.c | 12 + 05/musl-final/src/network/getservbyname_r.c | 41 + 05/musl-final/src/network/getservbyport.c | 12 + 05/musl-final/src/network/getservbyport_r.c | 43 + 05/musl-final/src/network/getsockname.c | 7 + 05/musl-final/src/network/getsockopt.c | 7 + 05/musl-final/src/network/h_errno.c | 1 + 05/musl-final/src/network/hstrerror.c | 16 + 05/musl-final/src/network/htonl.c | 10 + 05/musl-final/src/network/htons.c | 10 + 05/musl-final/src/network/in6addr_any.c | 3 + 05/musl-final/src/network/in6addr_loopback.c | 3 + 05/musl-final/src/network/inet_addr.c | 11 + 05/musl-final/src/network/inet_aton.c | 7 + 05/musl-final/src/network/inet_ntoa.c | 10 + 05/musl-final/src/network/inet_ntop.c | 48 + 05/musl-final/src/network/inet_pton.c | 31 + 05/musl-final/src/network/listen.c | 7 + 05/musl-final/src/network/ntohl.c | 10 + 05/musl-final/src/network/ntohs.c | 10 + 05/musl-final/src/network/proto.c | 58 + 05/musl-final/src/network/recv.c | 8 + 05/musl-final/src/network/recvfrom.c | 12 + 05/musl-final/src/network/recvmsg.c | 12 + 05/musl-final/src/network/res_init.c | 4 + 05/musl-final/src/network/res_query.c | 20 + 05/musl-final/src/network/send.c | 8 + 05/musl-final/src/network/sendmsg.c | 12 + 05/musl-final/src/network/sendto.c | 12 + 05/musl-final/src/network/serv.c | 16 + 05/musl-final/src/network/setsockopt.c | 7 + 05/musl-final/src/network/shutdown.c | 7 + 05/musl-final/src/network/sockatmark.c | 10 + 05/musl-final/src/network/socket.c | 7 + 05/musl-final/src/network/socketpair.c | 7 + 05/musl-final/src/passwd/getgr_r.c | 53 + 05/musl-final/src/passwd/getgrent.c | 39 + 05/musl-final/src/passwd/getgrent_a.c | 46 + 05/musl-final/src/passwd/getpw_r.c | 46 + 05/musl-final/src/passwd/getpwent.c | 39 + 05/musl-final/src/passwd/getpwent_a.c | 37 + 05/musl-final/src/passwd/getspent.c | 14 + 05/musl-final/src/passwd/getspnam.c | 17 + 05/musl-final/src/passwd/getspnam_r.c | 91 + 05/musl-final/src/passwd/lckpwdf.c | 11 + 05/musl-final/src/passwd/pwf.h | 13 + 05/musl-final/src/prng/__rand48_step.c | 14 + 05/musl-final/src/prng/__seed48.c | 1 + 05/musl-final/src/prng/drand48.c | 19 + 05/musl-final/src/prng/lcong48.c | 9 + 05/musl-final/src/prng/lrand48.c | 15 + 05/musl-final/src/prng/mrand48.c | 15 + 05/musl-final/src/prng/rand.c | 13 + 05/musl-final/src/prng/rand_r.c | 6 + 05/musl-final/src/prng/random.c | 8 + 05/musl-final/src/prng/seed48.c | 12 + 05/musl-final/src/prng/srand48.c | 6 + 05/musl-final/src/prng/srandom.c | 8 + 05/musl-final/src/process/execl.c | 20 + 05/musl-final/src/process/execle.c | 22 + 05/musl-final/src/process/execlp.c | 20 + 05/musl-final/src/process/execv.c | 8 + 05/musl-final/src/process/execve.c | 8 + 05/musl-final/src/process/execvp.c | 34 + 05/musl-final/src/process/fork.c | 9 + 05/musl-final/src/process/system.c | 45 + 05/musl-final/src/process/vfork.c | 8 + 05/musl-final/src/process/wait.c | 6 + 05/musl-final/src/process/waitid.c | 7 + 05/musl-final/src/process/waitpid.c | 7 + 05/musl-final/src/regex/fnmatch.c | 150 + 05/musl-final/src/regex/glob.c | 238 ++ 05/musl-final/src/regex/regcomp.c | 3362 +++++++++++++++++ 05/musl-final/src/regex/regerror.c | 75 + 05/musl-final/src/regex/regexec.c | 1107 ++++++ 05/musl-final/src/regex/tre-mem.c | 163 + 05/musl-final/src/regex/tre.h | 269 ++ 05/musl-final/src/select/poll.c | 12 + 05/musl-final/src/select/pselect.c | 15 + 05/musl-final/src/select/select.c | 12 + 05/musl-final/src/setjmp/i386/longjmp.s | 22 + 05/musl-final/src/setjmp/i386/setjmp.s | 23 + 05/musl-final/src/setjmp/longjmp.c | 0 05/musl-final/src/setjmp/setjmp.c | 0 05/musl-final/src/setjmp/x86_64/longjmp.s | 24 + 05/musl-final/src/setjmp/x86_64/setjmp.s | 25 + 05/musl-final/src/signal/getitimer.c | 12 + 05/musl-final/src/signal/i386/restore.s | 15 + 05/musl-final/src/signal/i386/sigsetjmp.s | 13 + 05/musl-final/src/signal/kill.c | 7 + 05/musl-final/src/signal/killpg.c | 11 + 05/musl-final/src/signal/raise.c | 7 + 05/musl-final/src/signal/restore.c | 0 05/musl-final/src/signal/setitimer.c | 15 + 05/musl-final/src/signal/sigaction.c | 53 + 05/musl-final/src/signal/sigaddset.c | 13 + 05/musl-final/src/signal/sigaltstack.c | 8 + 05/musl-final/src/signal/sigdelset.c | 13 + 05/musl-final/src/signal/sigemptyset.c | 8 + 05/musl-final/src/signal/sigfillset.c | 8 + 05/musl-final/src/signal/sighold.c | 11 + 05/musl-final/src/signal/sigignore.c | 12 + 05/musl-final/src/signal/siginterrupt.c | 13 + 05/musl-final/src/signal/sigismember.c | 12 + 05/musl-final/src/signal/siglongjmp.c | 12 + 05/musl-final/src/signal/signal.c | 16 + 05/musl-final/src/signal/sigpause.c | 11 + 05/musl-final/src/signal/sigpending.c | 7 + 05/musl-final/src/signal/sigprocmask.c | 44 + 05/musl-final/src/signal/sigqueue.c | 14 + 05/musl-final/src/signal/sigrelse.c | 11 + 05/musl-final/src/signal/sigrtmax.c | 4 + 05/musl-final/src/signal/sigrtmin.c | 4 + 05/musl-final/src/signal/sigset.c | 28 + 05/musl-final/src/signal/sigsetjmp.c | 17 + 05/musl-final/src/signal/sigsuspend.c | 7 + 05/musl-final/src/signal/sigtimedwait.c | 12 + 05/musl-final/src/signal/sigwait.c | 11 + 05/musl-final/src/signal/sigwaitinfo.c | 7 + 05/musl-final/src/signal/x86_64/restore.s | 11 + 05/musl-final/src/signal/x86_64/sigsetjmp.s | 11 + 05/musl-final/src/stat/chmod.c | 7 + 05/musl-final/src/stat/fchmod.c | 7 + 05/musl-final/src/stat/fchmodat.c | 7 + 05/musl-final/src/stat/fstat.c | 10 + 05/musl-final/src/stat/fstatat.c | 10 + 05/musl-final/src/stat/fstatvfs.c | 13 + 05/musl-final/src/stat/lstat.c | 10 + 05/musl-final/src/stat/mkdir.c | 7 + 05/musl-final/src/stat/mkdirat.c | 7 + 05/musl-final/src/stat/mkfifo.c | 6 + 05/musl-final/src/stat/mkfifoat.c | 6 + 05/musl-final/src/stat/mknod.c | 10 + 05/musl-final/src/stat/mknodat.c | 7 + 05/musl-final/src/stat/stat.c | 10 + 05/musl-final/src/stat/statvfs.c | 13 + 05/musl-final/src/stat/umask.c | 7 + 05/musl-final/src/stdio/__fclose_ca.c | 6 + 05/musl-final/src/stdio/__fdopen.c | 52 + 05/musl-final/src/stdio/__fopen_rb_ca.c | 18 + 05/musl-final/src/stdio/__fpending.c | 6 + 05/musl-final/src/stdio/__ofl.c | 3 + 05/musl-final/src/stdio/__overflow.c | 52 + 05/musl-final/src/stdio/__scanf.c | 487 +++ 05/musl-final/src/stdio/__scanf.h | 16 + 05/musl-final/src/stdio/__stdio_close.c | 6 + 05/musl-final/src/stdio/__stdio_read.c | 6 + 05/musl-final/src/stdio/__stdio_seek.c | 21 + 05/musl-final/src/stdio/__stdio_write.c | 9 + 05/musl-final/src/stdio/__uflow.c | 7 + 05/musl-final/src/stdio/__underflow.c | 38 + 05/musl-final/src/stdio/asprintf.c | 14 + 05/musl-final/src/stdio/clearerr.c | 16 + 05/musl-final/src/stdio/dprintf.c | 12 + 05/musl-final/src/stdio/fclose.c | 21 + 05/musl-final/src/stdio/feof.c | 13 + 05/musl-final/src/stdio/ferror.c | 13 + 05/musl-final/src/stdio/fflush.c | 54 + 05/musl-final/src/stdio/fgetc.c | 19 + 05/musl-final/src/stdio/fgetpos.c | 11 + 05/musl-final/src/stdio/fgets.c | 37 + 05/musl-final/src/stdio/fgetwc.c | 51 + 05/musl-final/src/stdio/fgetws.c | 27 + 05/musl-final/src/stdio/fileno.c | 11 + 05/musl-final/src/stdio/fmemopen.c | 16 + 05/musl-final/src/stdio/fopen.c | 34 + 05/musl-final/src/stdio/fprintf.c | 12 + 05/musl-final/src/stdio/fputc.c | 19 + 05/musl-final/src/stdio/fputs.c | 15 + 05/musl-final/src/stdio/fputwc.c | 33 + 05/musl-final/src/stdio/fputws.c | 23 + 05/musl-final/src/stdio/fread.c | 51 + 05/musl-final/src/stdio/freopen.c | 47 + 05/musl-final/src/stdio/fscanf.c | 12 + 05/musl-final/src/stdio/fseek.c | 38 + 05/musl-final/src/stdio/fsetpos.c | 8 + 05/musl-final/src/stdio/ftell.c | 35 + 05/musl-final/src/stdio/fwide.c | 10 + 05/musl-final/src/stdio/fwrite.c | 59 + 05/musl-final/src/stdio/fwscanf.c | 13 + 05/musl-final/src/stdio/getc.c | 6 + 05/musl-final/src/stdio/getc_unlocked.c | 8 + 05/musl-final/src/stdio/getchar.c | 6 + 05/musl-final/src/stdio/getchar_unlocked.c | 6 + 05/musl-final/src/stdio/getdelim.c | 59 + 05/musl-final/src/stdio/getline.c | 6 + 05/musl-final/src/stdio/gets.c | 8 + 05/musl-final/src/stdio/getw.c | 7 + 05/musl-final/src/stdio/getwc.c | 6 + 05/musl-final/src/stdio/getwchar.c | 8 + 05/musl-final/src/stdio/pclose.c | 10 + 05/musl-final/src/stdio/perror.c | 27 + 05/musl-final/src/stdio/popen.c | 43 + 05/musl-final/src/stdio/printf.c | 12 + 05/musl-final/src/stdio/putc.c | 8 + 05/musl-final/src/stdio/putc_unlocked.c | 8 + 05/musl-final/src/stdio/putchar.c | 6 + 05/musl-final/src/stdio/putchar_unlocked.c | 7 + 05/musl-final/src/stdio/puts.c | 6 + 05/musl-final/src/stdio/putw.c | 6 + 05/musl-final/src/stdio/putwc.c | 6 + 05/musl-final/src/stdio/putwchar.c | 8 + 05/musl-final/src/stdio/remove.c | 7 + 05/musl-final/src/stdio/rename.c | 7 + 05/musl-final/src/stdio/rewind.c | 6 + 05/musl-final/src/stdio/scanf.c | 12 + 05/musl-final/src/stdio/setbuf.c | 6 + 05/musl-final/src/stdio/setvbuf.c | 22 + 05/musl-final/src/stdio/snprintf.c | 13 + 05/musl-final/src/stdio/sprintf.c | 12 + 05/musl-final/src/stdio/sscanf.c | 12 + 05/musl-final/src/stdio/stderr.c | 13 + 05/musl-final/src/stdio/stdin.c | 13 + 05/musl-final/src/stdio/stdout.c | 17 + 05/musl-final/src/stdio/swscanf.c | 13 + 05/musl-final/src/stdio/tempnam.c | 42 + 05/musl-final/src/stdio/tmpfile.c | 23 + 05/musl-final/src/stdio/tmpnam.c | 38 + 05/musl-final/src/stdio/ungetc.c | 33 + 05/musl-final/src/stdio/ungetwc.c | 45 + 05/musl-final/src/stdio/vasprintf.c | 27 + 05/musl-final/src/stdio/vdprintf.c | 14 + 05/musl-final/src/stdio/vfprintf.c | 646 ++++ 05/musl-final/src/stdio/vfscanf.c | 43 + 05/musl-final/src/stdio/vfwscanf.c | 28 + 05/musl-final/src/stdio/vprintf.c | 6 + 05/musl-final/src/stdio/vscanf.c | 7 + 05/musl-final/src/stdio/vsnprintf.c | 33 + 05/musl-final/src/stdio/vsprintf.c | 7 + 05/musl-final/src/stdio/vsscanf.c | 21 + 05/musl-final/src/stdio/vswscanf.c | 19 + 05/musl-final/src/stdio/vwscanf.c | 8 + 05/musl-final/src/stdio/wscanf.c | 13 + 05/musl-final/src/stdlib/abs.c | 4 + 05/musl-final/src/stdlib/atof.c | 6 + 05/musl-final/src/stdlib/atoi.c | 15 + 05/musl-final/src/stdlib/atol.c | 16 + 05/musl-final/src/stdlib/atoll.c | 16 + 05/musl-final/src/stdlib/bsearch.c | 20 + 05/musl-final/src/stdlib/div.c | 6 + 05/musl-final/src/stdlib/frexp.c | 23 + 05/musl-final/src/stdlib/frexpf.c | 23 + 05/musl-final/src/stdlib/frexpl.c | 25 + 05/musl-final/src/stdlib/imaxabs.c | 6 + 05/musl-final/src/stdlib/imaxdiv.c | 6 + 05/musl-final/src/stdlib/labs.c | 4 + 05/musl-final/src/stdlib/ldiv.c | 6 + 05/musl-final/src/stdlib/llabs.c | 4 + 05/musl-final/src/stdlib/lldiv.c | 6 + 05/musl-final/src/stdlib/qsort.c | 50 + 05/musl-final/src/stdlib/strtod.c | 6 + 05/musl-final/src/stdlib/strtof.c | 6 + 05/musl-final/src/stdlib/strtoimax.c | 25 + 05/musl-final/src/stdlib/strtol.c | 17 + 05/musl-final/src/stdlib/strtold.c | 99 + 05/musl-final/src/stdlib/strtoll.c | 17 + 05/musl-final/src/stdlib/strtoul.c | 14 + 05/musl-final/src/stdlib/strtoull.c | 14 + 05/musl-final/src/stdlib/strtoumax.c | 123 + 05/musl-final/src/stdlib/wcstoimax.c | 25 + 05/musl-final/src/stdlib/wcstol.c | 18 + 05/musl-final/src/stdlib/wcstoll.c | 18 + 05/musl-final/src/stdlib/wcstoul.c | 15 + 05/musl-final/src/stdlib/wcstoull.c | 15 + 05/musl-final/src/stdlib/wcstoumax.c | 48 + 05/musl-final/src/string/bcmp.c | 7 + 05/musl-final/src/string/bcopy.c | 7 + 05/musl-final/src/string/bzero.c | 7 + 05/musl-final/src/string/index.c | 7 + 05/musl-final/src/string/memchr.c | 24 + 05/musl-final/src/string/memcmp.c | 8 + 05/musl-final/src/string/memcpy.c | 29 + 05/musl-final/src/string/memmove.c | 14 + 05/musl-final/src/string/mempcpy.c | 7 + 05/musl-final/src/string/memset.c | 21 + 05/musl-final/src/string/rindex.c | 7 + 05/musl-final/src/string/stpcpy.c | 29 + 05/musl-final/src/string/stpncpy.c | 32 + 05/musl-final/src/string/strcasecmp.c | 9 + 05/musl-final/src/string/strcasestr.c | 7 + 05/musl-final/src/string/strcat.c | 7 + 05/musl-final/src/string/strchr.c | 23 + 05/musl-final/src/string/strchrnul.c | 7 + 05/musl-final/src/string/strcmp.c | 7 + 05/musl-final/src/string/strcpy.c | 16 + 05/musl-final/src/string/strcspn.c | 20 + 05/musl-final/src/string/strdup.c | 19 + 05/musl-final/src/string/strerror_r.c | 11 + 05/musl-final/src/string/strlcat.c | 9 + 05/musl-final/src/string/strlcpy.c | 32 + 05/musl-final/src/string/strlen.c | 21 + 05/musl-final/src/string/strncasecmp.c | 10 + 05/musl-final/src/string/strncat.c | 10 + 05/musl-final/src/string/strncmp.c | 9 + 05/musl-final/src/string/strncpy.c | 9 + 05/musl-final/src/string/strndup.c | 12 + 05/musl-final/src/string/strnlen.c | 7 + 05/musl-final/src/string/strpbrk.c | 7 + 05/musl-final/src/string/strrchr.c | 9 + 05/musl-final/src/string/strsep.c | 12 + 05/musl-final/src/string/strsignal.c | 98 + 05/musl-final/src/string/strspn.c | 22 + 05/musl-final/src/string/strstr.c | 166 + 05/musl-final/src/string/strtok.c | 13 + 05/musl-final/src/string/strtok_r.c | 12 + 05/musl-final/src/string/swab.c | 13 + 05/musl-final/src/string/wcscat.c | 7 + 05/musl-final/src/string/wcschr.c | 8 + 05/musl-final/src/string/wcscmp.c | 7 + 05/musl-final/src/string/wcscpy.c | 8 + 05/musl-final/src/string/wcscspn.c | 10 + 05/musl-final/src/string/wcslen.c | 8 + 05/musl-final/src/string/wcsncat.c | 10 + 05/musl-final/src/string/wcsncmp.c | 7 + 05/musl-final/src/string/wcsncpy.c | 9 + 05/musl-final/src/string/wcspbrk.c | 7 + 05/musl-final/src/string/wcsrchr.c | 8 + 05/musl-final/src/string/wcsspn.c | 8 + 05/musl-final/src/string/wcsstr.c | 117 + 05/musl-final/src/string/wcswcs.c | 6 + 05/musl-final/src/string/wmemchr.c | 8 + 05/musl-final/src/string/wmemcmp.c | 8 + 05/musl-final/src/string/wmemcpy.c | 9 + 05/musl-final/src/string/wmemmove.c | 11 + 05/musl-final/src/string/wmemset.c | 9 + 05/musl-final/src/stub/utmpx.c | 30 + 05/musl-final/src/syscall.s | 30 + 05/musl-final/src/temp/mkdtemp.c | 23 + 05/musl-final/src/temp/mkstemp.c | 28 + 05/musl-final/src/temp/mktemp.c | 31 + 05/musl-final/src/termios/cfgetospeed.c | 12 + 05/musl-final/src/termios/cfsetospeed.c | 22 + 05/musl-final/src/termios/tcdrain.c | 7 + 05/musl-final/src/termios/tcflow.c | 7 + 05/musl-final/src/termios/tcflush.c | 7 + 05/musl-final/src/termios/tcgetattr.c | 10 + 05/musl-final/src/termios/tcgetsid.c | 10 + 05/musl-final/src/termios/tcsendbreak.c | 8 + 05/musl-final/src/termios/tcsetattr.c | 13 + 05/musl-final/src/thread/__futex.c | 8 + 05/musl-final/src/thread/__lock.c | 12 + 05/musl-final/src/thread/__set_thread_area.c | 0 05/musl-final/src/thread/__timedwait.c | 21 + 05/musl-final/src/thread/__unmapself.c | 0 05/musl-final/src/thread/__wait.c | 16 + 05/musl-final/src/thread/__wake.c | 9 + 05/musl-final/src/thread/cancellation.c | 14 + 05/musl-final/src/thread/clone.c | 0 .../src/thread/i386/__set_thread_area.s | 22 + 05/musl-final/src/thread/i386/__unmapself.s | 22 + 05/musl-final/src/thread/i386/clone.s | 27 + .../src/thread/pthread_attr_destroy.c | 6 + .../src/thread/pthread_attr_getdetachstate.c | 7 + .../src/thread/pthread_attr_getguardsize.c | 7 + .../src/thread/pthread_attr_getscope.c | 6 + .../src/thread/pthread_attr_getstacksize.c | 7 + 05/musl-final/src/thread/pthread_attr_init.c | 7 + .../src/thread/pthread_attr_setdetachstate.c | 7 + .../src/thread/pthread_attr_setguardsize.c | 8 + .../src/thread/pthread_attr_setscope.c | 6 + .../src/thread/pthread_attr_setstacksize.c | 8 + .../src/thread/pthread_barrier_destroy.c | 6 + .../src/thread/pthread_barrier_init.c | 8 + .../src/thread/pthread_barrier_wait.c | 31 + 05/musl-final/src/thread/pthread_cancel.c | 7 + .../src/thread/pthread_cond_broadcast.c | 8 + .../src/thread/pthread_cond_destroy.c | 6 + 05/musl-final/src/thread/pthread_cond_init.c | 7 + .../src/thread/pthread_cond_signal.c | 8 + .../src/thread/pthread_cond_timedwait.c | 26 + 05/musl-final/src/thread/pthread_cond_wait.c | 6 + 05/musl-final/src/thread/pthread_create.c | 228 ++ 05/musl-final/src/thread/pthread_detach.c | 11 + 05/musl-final/src/thread/pthread_equal.c | 6 + .../src/thread/pthread_getspecific.c | 8 + 05/musl-final/src/thread/pthread_join.c | 12 + 05/musl-final/src/thread/pthread_key_create.c | 25 + 05/musl-final/src/thread/pthread_key_delete.c | 7 + 05/musl-final/src/thread/pthread_kill.c | 7 + .../src/thread/pthread_mutex_destroy.c | 6 + 05/musl-final/src/thread/pthread_mutex_init.c | 9 + 05/musl-final/src/thread/pthread_mutex_lock.c | 9 + .../src/thread/pthread_mutex_timedlock.c | 15 + .../src/thread/pthread_mutex_trylock.c | 28 + .../src/thread/pthread_mutex_unlock.c | 19 + .../src/thread/pthread_mutexattr_destroy.c | 6 + .../src/thread/pthread_mutexattr_gettype.c | 7 + .../src/thread/pthread_mutexattr_init.c | 7 + .../src/thread/pthread_mutexattr_settype.c | 8 + 05/musl-final/src/thread/pthread_once.c | 38 + .../src/thread/pthread_rwlock_destroy.c | 6 + .../src/thread/pthread_rwlock_init.c | 9 + .../src/thread/pthread_rwlock_rdlock.c | 8 + .../src/thread/pthread_rwlock_timedrdlock.c | 15 + .../src/thread/pthread_rwlock_timedwrlock.c | 17 + .../src/thread/pthread_rwlock_tryrdlock.c | 13 + .../src/thread/pthread_rwlock_trywrlock.c | 13 + .../src/thread/pthread_rwlock_unlock.c | 17 + .../src/thread/pthread_rwlock_wrlock.c | 13 + 05/musl-final/src/thread/pthread_self.c | 33 + .../src/thread/pthread_setcancelstate.c | 10 + .../src/thread/pthread_setcanceltype.c | 10 + .../src/thread/pthread_setspecific.c | 18 + .../src/thread/pthread_spin_destroy.c | 6 + 05/musl-final/src/thread/pthread_spin_init.c | 6 + 05/musl-final/src/thread/pthread_spin_lock.c | 7 + .../src/thread/pthread_spin_trylock.c | 6 + .../src/thread/pthread_spin_unlock.c | 6 + 05/musl-final/src/thread/pthread_testcancel.c | 7 + .../src/thread/x86_64/__set_thread_area.s | 11 + 05/musl-final/src/thread/x86_64/__unmapself.s | 24 + 05/musl-final/src/thread/x86_64/clone.s | 22 + 05/musl-final/src/time/__asctime.c | 27 + 05/musl-final/src/time/__time.h | 9 + 05/musl-final/src/time/__time_to_tm.c | 81 + 05/musl-final/src/time/__tm_to_time.c | 33 + 05/musl-final/src/time/asctime.c | 9 + 05/musl-final/src/time/asctime_r.c | 8 + 05/musl-final/src/time/clock.c | 9 + 05/musl-final/src/time/clock_gettime.c | 12 + 05/musl-final/src/time/ctime.c | 6 + 05/musl-final/src/time/ctime_r.c | 8 + 05/musl-final/src/time/difftime.c | 6 + 05/musl-final/src/time/gettimeofday.c | 9 + 05/musl-final/src/time/gmtime.c | 11 + 05/musl-final/src/time/gmtime_r.c | 10 + 05/musl-final/src/time/localtime.c | 12 + 05/musl-final/src/time/localtime_r.c | 11 + 05/musl-final/src/time/mktime.c | 24 + 05/musl-final/src/time/nanosleep.c | 13 + 05/musl-final/src/time/strftime.c | 172 + 05/musl-final/src/time/strptime.c | 178 + 05/musl-final/src/time/time.c | 12 + 05/musl-final/src/time/times.c | 7 + 05/musl-final/src/time/timezone.s | 27 + 05/musl-final/src/time/tzset.c | 173 + 05/musl-final/src/time/utime.c | 12 + 05/musl-final/src/unistd/_exit.c | 7 + 05/musl-final/src/unistd/access.c | 7 + 05/musl-final/src/unistd/alarm.c | 7 + 05/musl-final/src/unistd/chdir.c | 7 + 05/musl-final/src/unistd/chown.c | 7 + 05/musl-final/src/unistd/close.c | 11 + 05/musl-final/src/unistd/ctermid.c | 23 + 05/musl-final/src/unistd/dup.c | 7 + 05/musl-final/src/unistd/dup2.c | 7 + 05/musl-final/src/unistd/faccessat.c | 7 + 05/musl-final/src/unistd/fchdir.c | 7 + 05/musl-final/src/unistd/fchown.c | 7 + 05/musl-final/src/unistd/fchownat.c | 7 + 05/musl-final/src/unistd/fdatasync.c | 7 + 05/musl-final/src/unistd/fsync.c | 8 + 05/musl-final/src/unistd/ftruncate.c | 10 + 05/musl-final/src/unistd/getcwd.c | 8 + 05/musl-final/src/unistd/getegid.c | 7 + 05/musl-final/src/unistd/geteuid.c | 7 + 05/musl-final/src/unistd/getgid.c | 7 + 05/musl-final/src/unistd/getgroups.c | 7 + 05/musl-final/src/unistd/gethostname.c | 14 + 05/musl-final/src/unistd/getlogin.c | 7 + 05/musl-final/src/unistd/getlogin_r.c | 13 + 05/musl-final/src/unistd/getpgid.c | 7 + 05/musl-final/src/unistd/getpgrp.c | 7 + 05/musl-final/src/unistd/getpid.c | 7 + 05/musl-final/src/unistd/getppid.c | 7 + 05/musl-final/src/unistd/getsid.c | 7 + 05/musl-final/src/unistd/getuid.c | 7 + 05/musl-final/src/unistd/isatty.c | 8 + 05/musl-final/src/unistd/lchown.c | 7 + 05/musl-final/src/unistd/link.c | 7 + 05/musl-final/src/unistd/linkat.c | 7 + 05/musl-final/src/unistd/lseek.c | 15 + 05/musl-final/src/unistd/nice.c | 12 + 05/musl-final/src/unistd/pause.c | 12 + 05/musl-final/src/unistd/pipe.c | 7 + 05/musl-final/src/unistd/pread.c | 14 + 05/musl-final/src/unistd/pwrite.c | 14 + 05/musl-final/src/unistd/read.c | 12 + 05/musl-final/src/unistd/readlink.c | 7 + 05/musl-final/src/unistd/readlinkat.c | 7 + 05/musl-final/src/unistd/readv.c | 12 + 05/musl-final/src/unistd/renameat.c | 7 + 05/musl-final/src/unistd/rmdir.c | 7 + 05/musl-final/src/unistd/setegid.c | 6 + 05/musl-final/src/unistd/seteuid.c | 6 + 05/musl-final/src/unistd/setgid.c | 9 + 05/musl-final/src/unistd/setpgid.c | 7 + 05/musl-final/src/unistd/setpgrp.c | 6 + 05/musl-final/src/unistd/setregid.c | 9 + 05/musl-final/src/unistd/setreuid.c | 9 + 05/musl-final/src/unistd/setsid.c | 7 + 05/musl-final/src/unistd/setuid.c | 9 + 05/musl-final/src/unistd/sleep.c | 10 + 05/musl-final/src/unistd/symlink.c | 7 + 05/musl-final/src/unistd/symlinkat.c | 7 + 05/musl-final/src/unistd/sync.c | 7 + 05/musl-final/src/unistd/tcgetpgrp.c | 11 + 05/musl-final/src/unistd/tcsetpgrp.c | 9 + 05/musl-final/src/unistd/truncate.c | 10 + 05/musl-final/src/unistd/ttyname.c | 14 + 05/musl-final/src/unistd/ttyname_r.c | 19 + 05/musl-final/src/unistd/ualarm.c | 8 + 05/musl-final/src/unistd/unlink.c | 7 + 05/musl-final/src/unistd/unlinkat.c | 7 + 05/musl-final/src/unistd/usleep.c | 11 + 05/musl-final/src/unistd/write.c | 12 + 05/musl-final/src/unistd/writev.c | 12 + 05/musl-final/tools/gen-musl-gcc.sh | 30 + 05/test.c | 8 +- 06/Makefile | 31 +- 06/src/ast.lua | 16 +- 06/src/main.lua | 8 +- 06/test.zig | 5 +- scripts/build.sh | 2 +- scripts/run.sh | 2 +- 1094 files changed, 51093 insertions(+), 51 deletions(-) delete mode 100644 .helix/.ignore create mode 100644 05/musl-final/.gitignore create mode 100644 05/musl-final/COPYING create mode 100644 05/musl-final/COPYRIGHT create mode 100644 05/musl-final/INSTALL create mode 100644 05/musl-final/Makefile create mode 100644 05/musl-final/README create mode 100644 05/musl-final/WHATSNEW create mode 100644 05/musl-final/arch/i386/atomic.h create mode 100755 05/musl-final/arch/i386/bits/alltypes.h.sh create mode 100644 05/musl-final/arch/i386/bits/endian.h create mode 100644 05/musl-final/arch/i386/bits/errno.h create mode 100644 05/musl-final/arch/i386/bits/fcntl.h create mode 100644 05/musl-final/arch/i386/bits/fenv.h create mode 100644 05/musl-final/arch/i386/bits/float.h create mode 100644 05/musl-final/arch/i386/bits/in.h create mode 100644 05/musl-final/arch/i386/bits/ioctl.h create mode 100644 05/musl-final/arch/i386/bits/ipc.h create mode 100644 05/musl-final/arch/i386/bits/limits.h create mode 100644 05/musl-final/arch/i386/bits/mman.h create mode 100644 05/musl-final/arch/i386/bits/posix.h create mode 100644 05/musl-final/arch/i386/bits/pthread.h create mode 100644 05/musl-final/arch/i386/bits/reg.h create mode 100644 05/musl-final/arch/i386/bits/setjmp.h create mode 100644 05/musl-final/arch/i386/bits/shm.h create mode 100644 05/musl-final/arch/i386/bits/signal.h create mode 100644 05/musl-final/arch/i386/bits/socket.h create mode 100644 05/musl-final/arch/i386/bits/stat.h create mode 100644 05/musl-final/arch/i386/bits/statfs.h create mode 100644 05/musl-final/arch/i386/bits/stdarg.h create mode 100644 05/musl-final/arch/i386/bits/stdint.h create mode 100644 05/musl-final/arch/i386/bits/sysmacros.h create mode 100644 05/musl-final/arch/i386/bits/tcp.h create mode 100644 05/musl-final/arch/i386/bits/termios.h create mode 100644 05/musl-final/arch/i386/bits/user.h create mode 100644 05/musl-final/arch/i386/bits/wait.h create mode 100644 05/musl-final/arch/i386/bits/wexitstatus.h create mode 100644 05/musl-final/arch/i386/pthread_arch.h create mode 100644 05/musl-final/arch/i386/syscall.h create mode 100644 05/musl-final/arch/x86_64/atomic.h create mode 100644 05/musl-final/arch/x86_64/bits/alltypes.h create mode 100755 05/musl-final/arch/x86_64/bits/alltypes.h.sh create mode 100644 05/musl-final/arch/x86_64/bits/endian.h create mode 100644 05/musl-final/arch/x86_64/bits/errno.h create mode 100644 05/musl-final/arch/x86_64/bits/fcntl.h create mode 100644 05/musl-final/arch/x86_64/bits/fenv.h create mode 100644 05/musl-final/arch/x86_64/bits/float.h create mode 100644 05/musl-final/arch/x86_64/bits/in.h create mode 100644 05/musl-final/arch/x86_64/bits/ioctl.h create mode 100644 05/musl-final/arch/x86_64/bits/ipc.h create mode 100644 05/musl-final/arch/x86_64/bits/limits.h create mode 100644 05/musl-final/arch/x86_64/bits/mman.h create mode 100644 05/musl-final/arch/x86_64/bits/posix.h create mode 100644 05/musl-final/arch/x86_64/bits/pthread.h create mode 100644 05/musl-final/arch/x86_64/bits/reg.h create mode 100644 05/musl-final/arch/x86_64/bits/setjmp.h create mode 100644 05/musl-final/arch/x86_64/bits/shm.h create mode 100644 05/musl-final/arch/x86_64/bits/signal.h create mode 100644 05/musl-final/arch/x86_64/bits/socket.h create mode 100644 05/musl-final/arch/x86_64/bits/stat.h create mode 100644 05/musl-final/arch/x86_64/bits/statfs.h create mode 100644 05/musl-final/arch/x86_64/bits/stdarg.h create mode 100644 05/musl-final/arch/x86_64/bits/stdint.h create mode 100644 05/musl-final/arch/x86_64/bits/sysmacros.h create mode 100644 05/musl-final/arch/x86_64/bits/tcp.h create mode 100644 05/musl-final/arch/x86_64/bits/termios.h create mode 100644 05/musl-final/arch/x86_64/bits/user.h create mode 100644 05/musl-final/arch/x86_64/bits/wait.h create mode 100644 05/musl-final/arch/x86_64/bits/wexitstatus.h create mode 100644 05/musl-final/arch/x86_64/pthread_arch.h create mode 100644 05/musl-final/arch/x86_64/syscall.h create mode 100644 05/musl-final/config.mak create mode 100644 05/musl-final/crt/crt1.c create mode 100644 05/musl-final/crt/crti.c create mode 100644 05/musl-final/crt/crtn.c create mode 100644 05/musl-final/crt/i386/crt1.s create mode 100644 05/musl-final/crt/x86_64/crt1.s create mode 100644 05/musl-final/dist/config.mak create mode 100644 05/musl-final/include/alloca.h create mode 100644 05/musl-final/include/arpa/inet.h create mode 100644 05/musl-final/include/arpa/nameser.h create mode 100644 05/musl-final/include/arpa/telnet.h create mode 100644 05/musl-final/include/assert.h create mode 120000 05/musl-final/include/bits create mode 100644 05/musl-final/include/byteswap.h create mode 100644 05/musl-final/include/cpio.h create mode 100644 05/musl-final/include/ctype.h create mode 100644 05/musl-final/include/dirent.h create mode 100644 05/musl-final/include/elf.h create mode 100644 05/musl-final/include/endian.h create mode 100644 05/musl-final/include/errno.h create mode 100644 05/musl-final/include/fcntl.h create mode 100644 05/musl-final/include/features.h create mode 100644 05/musl-final/include/fenv.h create mode 100644 05/musl-final/include/float.h create mode 100644 05/musl-final/include/fnmatch.h create mode 100644 05/musl-final/include/ftw.h create mode 100644 05/musl-final/include/getopt.h create mode 100644 05/musl-final/include/glob.h create mode 100644 05/musl-final/include/grp.h create mode 100644 05/musl-final/include/iconv.h create mode 100644 05/musl-final/include/inttypes.h create mode 100644 05/musl-final/include/iso646.h create mode 100644 05/musl-final/include/langinfo.h create mode 100644 05/musl-final/include/libgen.h create mode 100644 05/musl-final/include/libintl.h create mode 100644 05/musl-final/include/limits.h create mode 100644 05/musl-final/include/linux/loop.h create mode 100644 05/musl-final/include/linux/version.h create mode 100644 05/musl-final/include/locale.h create mode 100644 05/musl-final/include/malloc.h create mode 100644 05/musl-final/include/math.h create mode 100644 05/musl-final/include/mntent.h create mode 100644 05/musl-final/include/net/ethernet.h create mode 100644 05/musl-final/include/net/if.h create mode 100644 05/musl-final/include/net/if_arp.h create mode 100644 05/musl-final/include/net/route.h create mode 100644 05/musl-final/include/netdb.h create mode 100644 05/musl-final/include/netinet/icmp6.h create mode 100644 05/musl-final/include/netinet/if_ether.h create mode 100644 05/musl-final/include/netinet/in.h create mode 100644 05/musl-final/include/netinet/ip.h create mode 100644 05/musl-final/include/netinet/ip6.h create mode 100644 05/musl-final/include/netinet/ip_icmp.h create mode 100644 05/musl-final/include/netinet/tcp.h create mode 100644 05/musl-final/include/netinet/udp.h create mode 100644 05/musl-final/include/nl_types.h create mode 100644 05/musl-final/include/paths.h create mode 100644 05/musl-final/include/poll.h create mode 100644 05/musl-final/include/pthread.h create mode 100644 05/musl-final/include/pty.h create mode 100644 05/musl-final/include/pwd.h create mode 100644 05/musl-final/include/regex.h create mode 100644 05/musl-final/include/resolv.h create mode 100644 05/musl-final/include/sched.h create mode 100644 05/musl-final/include/search.h create mode 100644 05/musl-final/include/semaphore.h create mode 100644 05/musl-final/include/setjmp.h create mode 100644 05/musl-final/include/shadow.h create mode 100644 05/musl-final/include/signal.h create mode 100644 05/musl-final/include/stdarg.h create mode 100644 05/musl-final/include/stdbool.h create mode 100644 05/musl-final/include/stddef.h create mode 100644 05/musl-final/include/stdint.h create mode 100644 05/musl-final/include/stdio.h create mode 100644 05/musl-final/include/stdlib.h create mode 100644 05/musl-final/include/string.h create mode 100644 05/musl-final/include/strings.h create mode 100644 05/musl-final/include/stropts.h create mode 100644 05/musl-final/include/sys/epoll.h create mode 100644 05/musl-final/include/sys/file.h create mode 100644 05/musl-final/include/sys/ioctl.h create mode 100644 05/musl-final/include/sys/ipc.h create mode 100644 05/musl-final/include/sys/kd.h create mode 100644 05/musl-final/include/sys/klog.h create mode 100644 05/musl-final/include/sys/mman.h create mode 100644 05/musl-final/include/sys/mount.h create mode 100644 05/musl-final/include/sys/msg.h create mode 100644 05/musl-final/include/sys/mtio.h create mode 100644 05/musl-final/include/sys/param.h create mode 100644 05/musl-final/include/sys/poll.h create mode 100644 05/musl-final/include/sys/prctl.h create mode 100644 05/musl-final/include/sys/procfs.h create mode 100644 05/musl-final/include/sys/ptrace.h create mode 100644 05/musl-final/include/sys/reboot.h create mode 100644 05/musl-final/include/sys/reg.h create mode 100644 05/musl-final/include/sys/resource.h create mode 100644 05/musl-final/include/sys/select.h create mode 100644 05/musl-final/include/sys/sem.h create mode 100644 05/musl-final/include/sys/shm.h create mode 100644 05/musl-final/include/sys/signalfd.h create mode 100644 05/musl-final/include/sys/socket.h create mode 100644 05/musl-final/include/sys/soundcard.h create mode 100644 05/musl-final/include/sys/stat.h create mode 100644 05/musl-final/include/sys/statfs.h create mode 100644 05/musl-final/include/sys/statvfs.h create mode 100644 05/musl-final/include/sys/stropts.h create mode 100644 05/musl-final/include/sys/swap.h create mode 100644 05/musl-final/include/sys/sysctl.h create mode 100644 05/musl-final/include/sys/sysinfo.h create mode 100644 05/musl-final/include/sys/sysmacros.h create mode 100644 05/musl-final/include/sys/time.h create mode 100644 05/musl-final/include/sys/times.h create mode 100644 05/musl-final/include/sys/types.h create mode 100644 05/musl-final/include/sys/ucontext.h create mode 100644 05/musl-final/include/sys/uio.h create mode 100644 05/musl-final/include/sys/un.h create mode 100644 05/musl-final/include/sys/user.h create mode 100644 05/musl-final/include/sys/utsname.h create mode 100644 05/musl-final/include/sys/vfs.h create mode 100644 05/musl-final/include/sys/vt.h create mode 100644 05/musl-final/include/sys/wait.h create mode 100644 05/musl-final/include/syslog.h create mode 100644 05/musl-final/include/tar.h create mode 100644 05/musl-final/include/termios.h create mode 100644 05/musl-final/include/time.h create mode 100644 05/musl-final/include/ucontext.h create mode 100644 05/musl-final/include/ulimit.h create mode 100644 05/musl-final/include/unistd.h create mode 100644 05/musl-final/include/utime.h create mode 100644 05/musl-final/include/utmp.h create mode 100644 05/musl-final/include/utmpx.h create mode 100644 05/musl-final/include/wchar.h create mode 100644 05/musl-final/include/wctype.h create mode 100644 05/musl-final/include/wordexp.h create mode 100644 05/musl-final/src/conf/confstr.c create mode 100644 05/musl-final/src/conf/fpathconf.c create mode 100644 05/musl-final/src/conf/pathconf.c create mode 100644 05/musl-final/src/conf/sysconf.c create mode 100644 05/musl-final/src/ctype/__ctype_get_mb_cur_max.c create mode 100644 05/musl-final/src/ctype/isalnum.c create mode 100644 05/musl-final/src/ctype/isalpha.c create mode 100644 05/musl-final/src/ctype/isascii.c create mode 100644 05/musl-final/src/ctype/isblank.c create mode 100644 05/musl-final/src/ctype/iscntrl.c create mode 100644 05/musl-final/src/ctype/isdigit.c create mode 100644 05/musl-final/src/ctype/isgraph.c create mode 100644 05/musl-final/src/ctype/islower.c create mode 100644 05/musl-final/src/ctype/isprint.c create mode 100644 05/musl-final/src/ctype/ispunct.c create mode 100644 05/musl-final/src/ctype/isspace.c create mode 100644 05/musl-final/src/ctype/isupper.c create mode 100644 05/musl-final/src/ctype/iswalnum.c create mode 100644 05/musl-final/src/ctype/iswalpha.c create mode 100644 05/musl-final/src/ctype/iswblank.c create mode 100644 05/musl-final/src/ctype/iswcntrl.c create mode 100644 05/musl-final/src/ctype/iswctype.c create mode 100644 05/musl-final/src/ctype/iswdigit.c create mode 100644 05/musl-final/src/ctype/iswgraph.c create mode 100644 05/musl-final/src/ctype/iswlower.c create mode 100644 05/musl-final/src/ctype/iswprint.c create mode 100644 05/musl-final/src/ctype/iswpunct.c create mode 100644 05/musl-final/src/ctype/iswspace.c create mode 100644 05/musl-final/src/ctype/iswupper.c create mode 100644 05/musl-final/src/ctype/iswxdigit.c create mode 100644 05/musl-final/src/ctype/isxdigit.c create mode 100644 05/musl-final/src/ctype/toascii.c create mode 100644 05/musl-final/src/ctype/tolower.c create mode 100644 05/musl-final/src/ctype/toupper.c create mode 100644 05/musl-final/src/ctype/towctrans.c create mode 100644 05/musl-final/src/ctype/wcswidth.c create mode 100644 05/musl-final/src/ctype/wctrans.c create mode 100644 05/musl-final/src/ctype/wcwidth.c create mode 100644 05/musl-final/src/dirent/__dirent.h create mode 100644 05/musl-final/src/dirent/__getdents.c create mode 100644 05/musl-final/src/dirent/alphasort.c create mode 100644 05/musl-final/src/dirent/closedir.c create mode 100644 05/musl-final/src/dirent/dirfd.c create mode 100644 05/musl-final/src/dirent/fdopendir.c create mode 100644 05/musl-final/src/dirent/opendir.c create mode 100644 05/musl-final/src/dirent/readdir.c create mode 100644 05/musl-final/src/dirent/readdir_r.c create mode 100644 05/musl-final/src/dirent/rewinddir.c create mode 100644 05/musl-final/src/dirent/scandir.c create mode 100644 05/musl-final/src/dirent/seekdir.c create mode 100644 05/musl-final/src/dirent/telldir.c create mode 100644 05/musl-final/src/env/__environ.c create mode 100644 05/musl-final/src/env/__libc_start_main.c create mode 100644 05/musl-final/src/env/clearenv.c create mode 100644 05/musl-final/src/env/getenv.c create mode 100644 05/musl-final/src/env/putenv.c create mode 100644 05/musl-final/src/env/setenv.c create mode 100644 05/musl-final/src/env/unsetenv.c create mode 100644 05/musl-final/src/errno/__errno_location.c create mode 100644 05/musl-final/src/errno/__strerror.h create mode 100644 05/musl-final/src/errno/strerror.c create mode 100644 05/musl-final/src/exit/_Exit.c create mode 100644 05/musl-final/src/exit/abort.c create mode 100644 05/musl-final/src/exit/assert.c create mode 100644 05/musl-final/src/exit/atexit.c create mode 100644 05/musl-final/src/exit/exit.c create mode 100644 05/musl-final/src/fcntl/creat.c create mode 100644 05/musl-final/src/fcntl/fcntl.c create mode 100644 05/musl-final/src/fcntl/open.c create mode 100644 05/musl-final/src/fcntl/openat.c create mode 100644 05/musl-final/src/internal/futex.h create mode 100644 05/musl-final/src/internal/libc.c create mode 100644 05/musl-final/src/internal/libc.h create mode 100644 05/musl-final/src/internal/locale_impl.h create mode 100644 05/musl-final/src/internal/pthread_impl.h create mode 100644 05/musl-final/src/internal/stdio_impl.h create mode 100644 05/musl-final/src/internal/syscall.c create mode 100644 05/musl-final/src/ipc/ftok.c create mode 100644 05/musl-final/src/ipc/ipc.h create mode 100644 05/musl-final/src/ipc/semctl.c create mode 100644 05/musl-final/src/ipc/semget.c create mode 100644 05/musl-final/src/ipc/semop.c create mode 100644 05/musl-final/src/ipc/shmat.c create mode 100644 05/musl-final/src/ipc/shmctl.c create mode 100644 05/musl-final/src/ipc/shmdt.c create mode 100644 05/musl-final/src/ipc/shmget.c create mode 100644 05/musl-final/src/linux/brk.c create mode 100644 05/musl-final/src/linux/chroot.c create mode 100644 05/musl-final/src/linux/daemon.c create mode 100644 05/musl-final/src/linux/epoll_create.c create mode 100644 05/musl-final/src/linux/epoll_create1.c create mode 100644 05/musl-final/src/linux/epoll_ctl.c create mode 100644 05/musl-final/src/linux/epoll_pwait.c create mode 100644 05/musl-final/src/linux/epoll_wait.c create mode 100644 05/musl-final/src/linux/getdtablesize.c create mode 100644 05/musl-final/src/linux/gethostid.c create mode 100644 05/musl-final/src/linux/getopt_long.c create mode 100644 05/musl-final/src/linux/getpagesize.c create mode 100644 05/musl-final/src/linux/getpass.c create mode 100644 05/musl-final/src/linux/initgroups.c create mode 100644 05/musl-final/src/linux/klogctl.c create mode 100644 05/musl-final/src/linux/mntent.c create mode 100644 05/musl-final/src/linux/mount.c create mode 100644 05/musl-final/src/linux/prctl.c create mode 100644 05/musl-final/src/linux/reboot.c create mode 100644 05/musl-final/src/linux/sbrk.c create mode 100644 05/musl-final/src/linux/sendfile.c create mode 100644 05/musl-final/src/linux/setgroups.c create mode 100644 05/musl-final/src/linux/sethostname.c create mode 100644 05/musl-final/src/linux/settimeofday.c create mode 100644 05/musl-final/src/linux/signalfd.c create mode 100644 05/musl-final/src/linux/stime.c create mode 100644 05/musl-final/src/linux/swapoff.c create mode 100644 05/musl-final/src/linux/swapon.c create mode 100644 05/musl-final/src/linux/sysinfo.c create mode 100644 05/musl-final/src/linux/umount.c create mode 100644 05/musl-final/src/linux/umount2.c create mode 100644 05/musl-final/src/linux/utimes.c create mode 100644 05/musl-final/src/linux/wait3.c create mode 100644 05/musl-final/src/linux/wait4.c create mode 100644 05/musl-final/src/locale/catclose.c create mode 100644 05/musl-final/src/locale/catgets.c create mode 100644 05/musl-final/src/locale/catopen.c create mode 100644 05/musl-final/src/locale/duplocale.c create mode 100644 05/musl-final/src/locale/freelocale.c create mode 100644 05/musl-final/src/locale/iconv.c create mode 100644 05/musl-final/src/locale/intl.c create mode 100644 05/musl-final/src/locale/isalnum_l.c create mode 100644 05/musl-final/src/locale/isalpha_l.c create mode 100644 05/musl-final/src/locale/isblank_l.c create mode 100644 05/musl-final/src/locale/iscntrl_l.c create mode 100644 05/musl-final/src/locale/isdigit_l.c create mode 100644 05/musl-final/src/locale/isgraph_l.c create mode 100644 05/musl-final/src/locale/islower_l.c create mode 100644 05/musl-final/src/locale/isprint_l.c create mode 100644 05/musl-final/src/locale/ispunct_l.c create mode 100644 05/musl-final/src/locale/isspace_l.c create mode 100644 05/musl-final/src/locale/isupper_l.c create mode 100644 05/musl-final/src/locale/isxdigit_l.c create mode 100644 05/musl-final/src/locale/langinfo.c create mode 100644 05/musl-final/src/locale/localeconv.c create mode 100644 05/musl-final/src/locale/newlocale.c create mode 100644 05/musl-final/src/locale/nl_langinfo.c create mode 100644 05/musl-final/src/locale/setlocale.c create mode 100644 05/musl-final/src/locale/strcoll.c create mode 100644 05/musl-final/src/locale/strxfrm.c create mode 100644 05/musl-final/src/locale/tmp create mode 100644 05/musl-final/src/locale/tolower_l.c create mode 100644 05/musl-final/src/locale/toupper_l.c create mode 100644 05/musl-final/src/locale/wcscoll.c create mode 100644 05/musl-final/src/locale/wcsxfrm.c create mode 100644 05/musl-final/src/malloc/DESIGN create mode 100644 05/musl-final/src/malloc/__brk.c create mode 100644 05/musl-final/src/malloc/__simple_malloc.c create mode 100644 05/musl-final/src/malloc/calloc.c create mode 100644 05/musl-final/src/malloc/malloc.c create mode 100644 05/musl-final/src/malloc/memalign.c create mode 100644 05/musl-final/src/malloc/posix_memalign.c create mode 100644 05/musl-final/src/math/__fpclassify.c create mode 100644 05/musl-final/src/math/__fpclassifyf.c create mode 100644 05/musl-final/src/math/__fpclassifyl.c create mode 100644 05/musl-final/src/math/__log1p.h create mode 100644 05/musl-final/src/math/__log1pf.h create mode 100644 05/musl-final/src/math/__x86_64/e_sqrt.s create mode 100644 05/musl-final/src/math/__x86_64/e_sqrtf.s create mode 100644 05/musl-final/src/math/e_acos.c create mode 100644 05/musl-final/src/math/e_acosf.c create mode 100644 05/musl-final/src/math/e_acosh.c create mode 100644 05/musl-final/src/math/e_acoshf.c create mode 100644 05/musl-final/src/math/e_asin.c create mode 100644 05/musl-final/src/math/e_asinf.c create mode 100644 05/musl-final/src/math/e_atan2.c create mode 100644 05/musl-final/src/math/e_atan2f.c create mode 100644 05/musl-final/src/math/e_atanh.c create mode 100644 05/musl-final/src/math/e_atanhf.c create mode 100644 05/musl-final/src/math/e_cosh.c create mode 100644 05/musl-final/src/math/e_coshf.c create mode 100644 05/musl-final/src/math/e_exp.c create mode 100644 05/musl-final/src/math/e_expf.c create mode 100644 05/musl-final/src/math/e_fmod.c create mode 100644 05/musl-final/src/math/e_fmodf.c create mode 100644 05/musl-final/src/math/e_hypot.c create mode 100644 05/musl-final/src/math/e_hypotf.c create mode 100644 05/musl-final/src/math/e_log.c create mode 100644 05/musl-final/src/math/e_log10.c create mode 100644 05/musl-final/src/math/e_log10f.c create mode 100644 05/musl-final/src/math/e_logf.c create mode 100644 05/musl-final/src/math/e_pow.c create mode 100644 05/musl-final/src/math/e_powf.c create mode 100644 05/musl-final/src/math/e_rem_pio2.c create mode 100644 05/musl-final/src/math/e_rem_pio2f.c create mode 100644 05/musl-final/src/math/e_remainder.c create mode 100644 05/musl-final/src/math/e_remainderf.c create mode 100644 05/musl-final/src/math/e_scalb.c create mode 100644 05/musl-final/src/math/e_scalbf.c create mode 100644 05/musl-final/src/math/e_sinh.c create mode 100644 05/musl-final/src/math/e_sinhf.c create mode 100644 05/musl-final/src/math/e_sqrt.c create mode 100644 05/musl-final/src/math/e_sqrtf.c create mode 100644 05/musl-final/src/math/i386/e_exp.s create mode 100644 05/musl-final/src/math/i386/e_expf.s create mode 100644 05/musl-final/src/math/i386/e_log.s create mode 100644 05/musl-final/src/math/i386/e_log10.s create mode 100644 05/musl-final/src/math/i386/e_log10f.s create mode 100644 05/musl-final/src/math/i386/e_logf.s create mode 100644 05/musl-final/src/math/i386/e_remainder.s create mode 100644 05/musl-final/src/math/i386/e_remainderf.s create mode 100644 05/musl-final/src/math/i386/e_sqrt.s create mode 100644 05/musl-final/src/math/i386/e_sqrtf.s create mode 100644 05/musl-final/src/math/i386/s_ceil.s create mode 100644 05/musl-final/src/math/i386/s_ceilf.s create mode 100644 05/musl-final/src/math/i386/s_fabs.s create mode 100644 05/musl-final/src/math/i386/s_fabsf.s create mode 100644 05/musl-final/src/math/i386/s_floor.s create mode 100644 05/musl-final/src/math/i386/s_floorf.s create mode 100644 05/musl-final/src/math/i386/s_ldexp.s create mode 100644 05/musl-final/src/math/i386/s_ldexpf.s create mode 100644 05/musl-final/src/math/i386/s_rint.s create mode 100644 05/musl-final/src/math/i386/s_rintf.s create mode 100644 05/musl-final/src/math/i386/s_scalbln.s create mode 100644 05/musl-final/src/math/i386/s_scalblnf.s create mode 100644 05/musl-final/src/math/i386/s_trunc.s create mode 100644 05/musl-final/src/math/i386/s_truncf.s create mode 100644 05/musl-final/src/math/k_cos.c create mode 100644 05/musl-final/src/math/k_cosf.c create mode 100644 05/musl-final/src/math/k_rem_pio2.c create mode 100644 05/musl-final/src/math/k_rem_pio2f.c create mode 100644 05/musl-final/src/math/k_sin.c create mode 100644 05/musl-final/src/math/k_sinf.c create mode 100644 05/musl-final/src/math/k_tan.c create mode 100644 05/musl-final/src/math/k_tanf.c create mode 100644 05/musl-final/src/math/log.c create mode 100644 05/musl-final/src/math/log10.c create mode 100644 05/musl-final/src/math/log10f.c create mode 100644 05/musl-final/src/math/log10l.c create mode 100644 05/musl-final/src/math/log1p.c create mode 100644 05/musl-final/src/math/log1pf.c create mode 100644 05/musl-final/src/math/log1pl.c create mode 100644 05/musl-final/src/math/log2.c create mode 100644 05/musl-final/src/math/log2f.c create mode 100644 05/musl-final/src/math/log2l.c create mode 100644 05/musl-final/src/math/logb.c create mode 100644 05/musl-final/src/math/logbf.c create mode 100644 05/musl-final/src/math/logbl.c create mode 100644 05/musl-final/src/math/logf.c create mode 100644 05/musl-final/src/math/logl.c create mode 100644 05/musl-final/src/math/math_private.h create mode 100644 05/musl-final/src/math/s_asinh.c create mode 100644 05/musl-final/src/math/s_asinhf.c create mode 100644 05/musl-final/src/math/s_atan.c create mode 100644 05/musl-final/src/math/s_atanf.c create mode 100644 05/musl-final/src/math/s_cbrt.c create mode 100644 05/musl-final/src/math/s_cbrtf.c create mode 100644 05/musl-final/src/math/s_ceil.c create mode 100644 05/musl-final/src/math/s_ceilf.c create mode 100644 05/musl-final/src/math/s_copysign.c create mode 100644 05/musl-final/src/math/s_copysignf.c create mode 100644 05/musl-final/src/math/s_cos.c create mode 100644 05/musl-final/src/math/s_cosf.c create mode 100644 05/musl-final/src/math/s_erf.c create mode 100644 05/musl-final/src/math/s_erff.c create mode 100644 05/musl-final/src/math/s_expm1.c create mode 100644 05/musl-final/src/math/s_expm1f.c create mode 100644 05/musl-final/src/math/s_fabs.c create mode 100644 05/musl-final/src/math/s_fabsf.c create mode 100644 05/musl-final/src/math/s_floor.c create mode 100644 05/musl-final/src/math/s_floorf.c create mode 100644 05/musl-final/src/math/s_ilogb.c create mode 100644 05/musl-final/src/math/s_ilogbf.c create mode 100644 05/musl-final/src/math/s_ldexp.c create mode 100644 05/musl-final/src/math/s_ldexpf.c create mode 100644 05/musl-final/src/math/s_llrint.c create mode 100644 05/musl-final/src/math/s_log1p.c create mode 100644 05/musl-final/src/math/s_log1pf.c create mode 100644 05/musl-final/src/math/s_logb.c create mode 100644 05/musl-final/src/math/s_logbf.c create mode 100644 05/musl-final/src/math/s_lrint.c create mode 100644 05/musl-final/src/math/s_lrintf.c create mode 100644 05/musl-final/src/math/s_modf.c create mode 100644 05/musl-final/src/math/s_modff.c create mode 100644 05/musl-final/src/math/s_nextafter.c create mode 100644 05/musl-final/src/math/s_nextafterf.c create mode 100644 05/musl-final/src/math/s_remquo.c create mode 100644 05/musl-final/src/math/s_remquof.c create mode 100644 05/musl-final/src/math/s_rint.c create mode 100644 05/musl-final/src/math/s_rintf.c create mode 100644 05/musl-final/src/math/s_round.c create mode 100644 05/musl-final/src/math/s_roundf.c create mode 100644 05/musl-final/src/math/s_scalbln.c create mode 100644 05/musl-final/src/math/s_scalblnf.c create mode 100644 05/musl-final/src/math/s_sin.c create mode 100644 05/musl-final/src/math/s_sinf.c create mode 100644 05/musl-final/src/math/s_tan.c create mode 100644 05/musl-final/src/math/s_tanf.c create mode 100644 05/musl-final/src/math/s_tanh.c create mode 100644 05/musl-final/src/math/s_tanhf.c create mode 100644 05/musl-final/src/math/s_trunc.c create mode 100644 05/musl-final/src/math/s_truncf.c create mode 100644 05/musl-final/src/misc/basename.c create mode 100644 05/musl-final/src/misc/bswap_32.c create mode 100644 05/musl-final/src/misc/bswap_64.c create mode 100644 05/musl-final/src/misc/crypt.c create mode 100644 05/musl-final/src/misc/cuserid.c create mode 100644 05/musl-final/src/misc/dirname.c create mode 100644 05/musl-final/src/misc/ffs.c create mode 100644 05/musl-final/src/misc/ftw.c create mode 100644 05/musl-final/src/misc/getdomainname.c create mode 100644 05/musl-final/src/misc/getgrouplist.c create mode 100644 05/musl-final/src/misc/getopt.c create mode 100644 05/musl-final/src/misc/getpriority.c create mode 100644 05/musl-final/src/misc/getrlimit.c create mode 100644 05/musl-final/src/misc/getrusage.c create mode 100644 05/musl-final/src/misc/getsubopt.c create mode 100644 05/musl-final/src/misc/ioctl.c create mode 100644 05/musl-final/src/misc/lockf.c create mode 100644 05/musl-final/src/misc/nftw.c create mode 100644 05/musl-final/src/misc/openpty.c create mode 100644 05/musl-final/src/misc/pty.c create mode 100644 05/musl-final/src/misc/realpath.c create mode 100644 05/musl-final/src/misc/sched_yield.c create mode 100644 05/musl-final/src/misc/setpriority.c create mode 100644 05/musl-final/src/misc/setrlimit.c create mode 100644 05/musl-final/src/misc/syslog.c create mode 100644 05/musl-final/src/misc/uname.c create mode 100644 05/musl-final/src/mman/madvise.c create mode 100644 05/musl-final/src/mman/mlock.c create mode 100644 05/musl-final/src/mman/mlockall.c create mode 100644 05/musl-final/src/mman/mmap.c create mode 100644 05/musl-final/src/mman/mprotect.c create mode 100644 05/musl-final/src/mman/mremap.c create mode 100644 05/musl-final/src/mman/msync.c create mode 100644 05/musl-final/src/mman/munlock.c create mode 100644 05/musl-final/src/mman/munlockall.c create mode 100644 05/musl-final/src/mman/munmap.c create mode 100644 05/musl-final/src/mman/posix_madvise.c create mode 100644 05/musl-final/src/multibyte/btowc.c create mode 100644 05/musl-final/src/multibyte/decode.c create mode 100644 05/musl-final/src/multibyte/internal.c create mode 100644 05/musl-final/src/multibyte/internal.h create mode 100644 05/musl-final/src/multibyte/mblen.c create mode 100644 05/musl-final/src/multibyte/mbrlen.c create mode 100644 05/musl-final/src/multibyte/mbrtowc.c create mode 100644 05/musl-final/src/multibyte/mbsinit.c create mode 100644 05/musl-final/src/multibyte/mbsnrtowcs.c create mode 100644 05/musl-final/src/multibyte/mbsrtowcs.c create mode 100644 05/musl-final/src/multibyte/mbstowcs.c create mode 100644 05/musl-final/src/multibyte/mbtowc.c create mode 100644 05/musl-final/src/multibyte/wcrtomb.c create mode 100644 05/musl-final/src/multibyte/wcsnrtombs.c create mode 100644 05/musl-final/src/multibyte/wcsrtombs.c create mode 100644 05/musl-final/src/multibyte/wcstombs.c create mode 100644 05/musl-final/src/multibyte/wctob.c create mode 100644 05/musl-final/src/multibyte/wctomb.c create mode 100644 05/musl-final/src/network/__dns.c create mode 100644 05/musl-final/src/network/__dns.h create mode 100644 05/musl-final/src/network/__ipparse.c create mode 100644 05/musl-final/src/network/accept.c create mode 100644 05/musl-final/src/network/bind.c create mode 100644 05/musl-final/src/network/connect.c create mode 100644 05/musl-final/src/network/dn_expand.c create mode 100644 05/musl-final/src/network/ent.c create mode 100644 05/musl-final/src/network/freeaddrinfo.c create mode 100644 05/musl-final/src/network/gai_strerror.c create mode 100644 05/musl-final/src/network/getaddrinfo.c create mode 100644 05/musl-final/src/network/gethostbyaddr.c create mode 100644 05/musl-final/src/network/gethostbyaddr_r.c create mode 100644 05/musl-final/src/network/gethostbyname.c create mode 100644 05/musl-final/src/network/gethostbyname2.c create mode 100644 05/musl-final/src/network/gethostbyname2_r.c create mode 100644 05/musl-final/src/network/gethostbyname_r.c create mode 100644 05/musl-final/src/network/getnameinfo.c create mode 100644 05/musl-final/src/network/getpeername.c create mode 100644 05/musl-final/src/network/getservbyname.c create mode 100644 05/musl-final/src/network/getservbyname_r.c create mode 100644 05/musl-final/src/network/getservbyport.c create mode 100644 05/musl-final/src/network/getservbyport_r.c create mode 100644 05/musl-final/src/network/getsockname.c create mode 100644 05/musl-final/src/network/getsockopt.c create mode 100644 05/musl-final/src/network/h_errno.c create mode 100644 05/musl-final/src/network/hstrerror.c create mode 100644 05/musl-final/src/network/htonl.c create mode 100644 05/musl-final/src/network/htons.c create mode 100644 05/musl-final/src/network/in6addr_any.c create mode 100644 05/musl-final/src/network/in6addr_loopback.c create mode 100644 05/musl-final/src/network/inet_addr.c create mode 100644 05/musl-final/src/network/inet_aton.c create mode 100644 05/musl-final/src/network/inet_ntoa.c create mode 100644 05/musl-final/src/network/inet_ntop.c create mode 100644 05/musl-final/src/network/inet_pton.c create mode 100644 05/musl-final/src/network/listen.c create mode 100644 05/musl-final/src/network/ntohl.c create mode 100644 05/musl-final/src/network/ntohs.c create mode 100644 05/musl-final/src/network/proto.c create mode 100644 05/musl-final/src/network/recv.c create mode 100644 05/musl-final/src/network/recvfrom.c create mode 100644 05/musl-final/src/network/recvmsg.c create mode 100644 05/musl-final/src/network/res_init.c create mode 100644 05/musl-final/src/network/res_query.c create mode 100644 05/musl-final/src/network/send.c create mode 100644 05/musl-final/src/network/sendmsg.c create mode 100644 05/musl-final/src/network/sendto.c create mode 100644 05/musl-final/src/network/serv.c create mode 100644 05/musl-final/src/network/setsockopt.c create mode 100644 05/musl-final/src/network/shutdown.c create mode 100644 05/musl-final/src/network/sockatmark.c create mode 100644 05/musl-final/src/network/socket.c create mode 100644 05/musl-final/src/network/socketpair.c create mode 100644 05/musl-final/src/passwd/getgr_r.c create mode 100644 05/musl-final/src/passwd/getgrent.c create mode 100644 05/musl-final/src/passwd/getgrent_a.c create mode 100644 05/musl-final/src/passwd/getpw_r.c create mode 100644 05/musl-final/src/passwd/getpwent.c create mode 100644 05/musl-final/src/passwd/getpwent_a.c create mode 100644 05/musl-final/src/passwd/getspent.c create mode 100644 05/musl-final/src/passwd/getspnam.c create mode 100644 05/musl-final/src/passwd/getspnam_r.c create mode 100644 05/musl-final/src/passwd/lckpwdf.c create mode 100644 05/musl-final/src/passwd/pwf.h create mode 100644 05/musl-final/src/prng/__rand48_step.c create mode 100644 05/musl-final/src/prng/__seed48.c create mode 100644 05/musl-final/src/prng/drand48.c create mode 100644 05/musl-final/src/prng/lcong48.c create mode 100644 05/musl-final/src/prng/lrand48.c create mode 100644 05/musl-final/src/prng/mrand48.c create mode 100644 05/musl-final/src/prng/rand.c create mode 100644 05/musl-final/src/prng/rand_r.c create mode 100644 05/musl-final/src/prng/random.c create mode 100644 05/musl-final/src/prng/seed48.c create mode 100644 05/musl-final/src/prng/srand48.c create mode 100644 05/musl-final/src/prng/srandom.c create mode 100644 05/musl-final/src/process/execl.c create mode 100644 05/musl-final/src/process/execle.c create mode 100644 05/musl-final/src/process/execlp.c create mode 100644 05/musl-final/src/process/execv.c create mode 100644 05/musl-final/src/process/execve.c create mode 100644 05/musl-final/src/process/execvp.c create mode 100644 05/musl-final/src/process/fork.c create mode 100644 05/musl-final/src/process/system.c create mode 100644 05/musl-final/src/process/vfork.c create mode 100644 05/musl-final/src/process/wait.c create mode 100644 05/musl-final/src/process/waitid.c create mode 100644 05/musl-final/src/process/waitpid.c create mode 100644 05/musl-final/src/regex/fnmatch.c create mode 100644 05/musl-final/src/regex/glob.c create mode 100644 05/musl-final/src/regex/regcomp.c create mode 100644 05/musl-final/src/regex/regerror.c create mode 100644 05/musl-final/src/regex/regexec.c create mode 100644 05/musl-final/src/regex/tre-mem.c create mode 100644 05/musl-final/src/regex/tre.h create mode 100644 05/musl-final/src/select/poll.c create mode 100644 05/musl-final/src/select/pselect.c create mode 100644 05/musl-final/src/select/select.c create mode 100644 05/musl-final/src/setjmp/i386/longjmp.s create mode 100644 05/musl-final/src/setjmp/i386/setjmp.s create mode 100644 05/musl-final/src/setjmp/longjmp.c create mode 100644 05/musl-final/src/setjmp/setjmp.c create mode 100644 05/musl-final/src/setjmp/x86_64/longjmp.s create mode 100644 05/musl-final/src/setjmp/x86_64/setjmp.s create mode 100644 05/musl-final/src/signal/getitimer.c create mode 100644 05/musl-final/src/signal/i386/restore.s create mode 100644 05/musl-final/src/signal/i386/sigsetjmp.s create mode 100644 05/musl-final/src/signal/kill.c create mode 100644 05/musl-final/src/signal/killpg.c create mode 100644 05/musl-final/src/signal/raise.c create mode 100644 05/musl-final/src/signal/restore.c create mode 100644 05/musl-final/src/signal/setitimer.c create mode 100644 05/musl-final/src/signal/sigaction.c create mode 100644 05/musl-final/src/signal/sigaddset.c create mode 100644 05/musl-final/src/signal/sigaltstack.c create mode 100644 05/musl-final/src/signal/sigdelset.c create mode 100644 05/musl-final/src/signal/sigemptyset.c create mode 100644 05/musl-final/src/signal/sigfillset.c create mode 100644 05/musl-final/src/signal/sighold.c create mode 100644 05/musl-final/src/signal/sigignore.c create mode 100644 05/musl-final/src/signal/siginterrupt.c create mode 100644 05/musl-final/src/signal/sigismember.c create mode 100644 05/musl-final/src/signal/siglongjmp.c create mode 100644 05/musl-final/src/signal/signal.c create mode 100644 05/musl-final/src/signal/sigpause.c create mode 100644 05/musl-final/src/signal/sigpending.c create mode 100644 05/musl-final/src/signal/sigprocmask.c create mode 100644 05/musl-final/src/signal/sigqueue.c create mode 100644 05/musl-final/src/signal/sigrelse.c create mode 100644 05/musl-final/src/signal/sigrtmax.c create mode 100644 05/musl-final/src/signal/sigrtmin.c create mode 100644 05/musl-final/src/signal/sigset.c create mode 100644 05/musl-final/src/signal/sigsetjmp.c create mode 100644 05/musl-final/src/signal/sigsuspend.c create mode 100644 05/musl-final/src/signal/sigtimedwait.c create mode 100644 05/musl-final/src/signal/sigwait.c create mode 100644 05/musl-final/src/signal/sigwaitinfo.c create mode 100644 05/musl-final/src/signal/x86_64/restore.s create mode 100644 05/musl-final/src/signal/x86_64/sigsetjmp.s create mode 100644 05/musl-final/src/stat/chmod.c create mode 100644 05/musl-final/src/stat/fchmod.c create mode 100644 05/musl-final/src/stat/fchmodat.c create mode 100644 05/musl-final/src/stat/fstat.c create mode 100644 05/musl-final/src/stat/fstatat.c create mode 100644 05/musl-final/src/stat/fstatvfs.c create mode 100644 05/musl-final/src/stat/lstat.c create mode 100644 05/musl-final/src/stat/mkdir.c create mode 100644 05/musl-final/src/stat/mkdirat.c create mode 100644 05/musl-final/src/stat/mkfifo.c create mode 100644 05/musl-final/src/stat/mkfifoat.c create mode 100644 05/musl-final/src/stat/mknod.c create mode 100644 05/musl-final/src/stat/mknodat.c create mode 100644 05/musl-final/src/stat/stat.c create mode 100644 05/musl-final/src/stat/statvfs.c create mode 100644 05/musl-final/src/stat/umask.c create mode 100644 05/musl-final/src/stdio/__fclose_ca.c create mode 100644 05/musl-final/src/stdio/__fdopen.c create mode 100644 05/musl-final/src/stdio/__fopen_rb_ca.c create mode 100644 05/musl-final/src/stdio/__fpending.c create mode 100644 05/musl-final/src/stdio/__ofl.c create mode 100644 05/musl-final/src/stdio/__overflow.c create mode 100644 05/musl-final/src/stdio/__scanf.c create mode 100644 05/musl-final/src/stdio/__scanf.h create mode 100644 05/musl-final/src/stdio/__stdio_close.c create mode 100644 05/musl-final/src/stdio/__stdio_read.c create mode 100644 05/musl-final/src/stdio/__stdio_seek.c create mode 100644 05/musl-final/src/stdio/__stdio_write.c create mode 100644 05/musl-final/src/stdio/__uflow.c create mode 100644 05/musl-final/src/stdio/__underflow.c create mode 100644 05/musl-final/src/stdio/asprintf.c create mode 100644 05/musl-final/src/stdio/clearerr.c create mode 100644 05/musl-final/src/stdio/dprintf.c create mode 100644 05/musl-final/src/stdio/fclose.c create mode 100644 05/musl-final/src/stdio/feof.c create mode 100644 05/musl-final/src/stdio/ferror.c create mode 100644 05/musl-final/src/stdio/fflush.c create mode 100644 05/musl-final/src/stdio/fgetc.c create mode 100644 05/musl-final/src/stdio/fgetpos.c create mode 100644 05/musl-final/src/stdio/fgets.c create mode 100644 05/musl-final/src/stdio/fgetwc.c create mode 100644 05/musl-final/src/stdio/fgetws.c create mode 100644 05/musl-final/src/stdio/fileno.c create mode 100644 05/musl-final/src/stdio/fmemopen.c create mode 100644 05/musl-final/src/stdio/fopen.c create mode 100644 05/musl-final/src/stdio/fprintf.c create mode 100644 05/musl-final/src/stdio/fputc.c create mode 100644 05/musl-final/src/stdio/fputs.c create mode 100644 05/musl-final/src/stdio/fputwc.c create mode 100644 05/musl-final/src/stdio/fputws.c create mode 100644 05/musl-final/src/stdio/fread.c create mode 100644 05/musl-final/src/stdio/freopen.c create mode 100644 05/musl-final/src/stdio/fscanf.c create mode 100644 05/musl-final/src/stdio/fseek.c create mode 100644 05/musl-final/src/stdio/fsetpos.c create mode 100644 05/musl-final/src/stdio/ftell.c create mode 100644 05/musl-final/src/stdio/fwide.c create mode 100644 05/musl-final/src/stdio/fwrite.c create mode 100644 05/musl-final/src/stdio/fwscanf.c create mode 100644 05/musl-final/src/stdio/getc.c create mode 100644 05/musl-final/src/stdio/getc_unlocked.c create mode 100644 05/musl-final/src/stdio/getchar.c create mode 100644 05/musl-final/src/stdio/getchar_unlocked.c create mode 100644 05/musl-final/src/stdio/getdelim.c create mode 100644 05/musl-final/src/stdio/getline.c create mode 100644 05/musl-final/src/stdio/gets.c create mode 100644 05/musl-final/src/stdio/getw.c create mode 100644 05/musl-final/src/stdio/getwc.c create mode 100644 05/musl-final/src/stdio/getwchar.c create mode 100644 05/musl-final/src/stdio/pclose.c create mode 100644 05/musl-final/src/stdio/perror.c create mode 100644 05/musl-final/src/stdio/popen.c create mode 100644 05/musl-final/src/stdio/printf.c create mode 100644 05/musl-final/src/stdio/putc.c create mode 100644 05/musl-final/src/stdio/putc_unlocked.c create mode 100644 05/musl-final/src/stdio/putchar.c create mode 100644 05/musl-final/src/stdio/putchar_unlocked.c create mode 100644 05/musl-final/src/stdio/puts.c create mode 100644 05/musl-final/src/stdio/putw.c create mode 100644 05/musl-final/src/stdio/putwc.c create mode 100644 05/musl-final/src/stdio/putwchar.c create mode 100644 05/musl-final/src/stdio/remove.c create mode 100644 05/musl-final/src/stdio/rename.c create mode 100644 05/musl-final/src/stdio/rewind.c create mode 100644 05/musl-final/src/stdio/scanf.c create mode 100644 05/musl-final/src/stdio/setbuf.c create mode 100644 05/musl-final/src/stdio/setvbuf.c create mode 100644 05/musl-final/src/stdio/snprintf.c create mode 100644 05/musl-final/src/stdio/sprintf.c create mode 100644 05/musl-final/src/stdio/sscanf.c create mode 100644 05/musl-final/src/stdio/stderr.c create mode 100644 05/musl-final/src/stdio/stdin.c create mode 100644 05/musl-final/src/stdio/stdout.c create mode 100644 05/musl-final/src/stdio/swscanf.c create mode 100644 05/musl-final/src/stdio/tempnam.c create mode 100644 05/musl-final/src/stdio/tmpfile.c create mode 100644 05/musl-final/src/stdio/tmpnam.c create mode 100644 05/musl-final/src/stdio/ungetc.c create mode 100644 05/musl-final/src/stdio/ungetwc.c create mode 100644 05/musl-final/src/stdio/vasprintf.c create mode 100644 05/musl-final/src/stdio/vdprintf.c create mode 100644 05/musl-final/src/stdio/vfprintf.c create mode 100644 05/musl-final/src/stdio/vfscanf.c create mode 100644 05/musl-final/src/stdio/vfwscanf.c create mode 100644 05/musl-final/src/stdio/vprintf.c create mode 100644 05/musl-final/src/stdio/vscanf.c create mode 100644 05/musl-final/src/stdio/vsnprintf.c create mode 100644 05/musl-final/src/stdio/vsprintf.c create mode 100644 05/musl-final/src/stdio/vsscanf.c create mode 100644 05/musl-final/src/stdio/vswscanf.c create mode 100644 05/musl-final/src/stdio/vwscanf.c create mode 100644 05/musl-final/src/stdio/wscanf.c create mode 100644 05/musl-final/src/stdlib/abs.c create mode 100644 05/musl-final/src/stdlib/atof.c create mode 100644 05/musl-final/src/stdlib/atoi.c create mode 100644 05/musl-final/src/stdlib/atol.c create mode 100644 05/musl-final/src/stdlib/atoll.c create mode 100644 05/musl-final/src/stdlib/bsearch.c create mode 100644 05/musl-final/src/stdlib/div.c create mode 100644 05/musl-final/src/stdlib/frexp.c create mode 100644 05/musl-final/src/stdlib/frexpf.c create mode 100644 05/musl-final/src/stdlib/frexpl.c create mode 100644 05/musl-final/src/stdlib/imaxabs.c create mode 100644 05/musl-final/src/stdlib/imaxdiv.c create mode 100644 05/musl-final/src/stdlib/labs.c create mode 100644 05/musl-final/src/stdlib/ldiv.c create mode 100644 05/musl-final/src/stdlib/llabs.c create mode 100644 05/musl-final/src/stdlib/lldiv.c create mode 100644 05/musl-final/src/stdlib/qsort.c create mode 100644 05/musl-final/src/stdlib/strtod.c create mode 100644 05/musl-final/src/stdlib/strtof.c create mode 100644 05/musl-final/src/stdlib/strtoimax.c create mode 100644 05/musl-final/src/stdlib/strtol.c create mode 100644 05/musl-final/src/stdlib/strtold.c create mode 100644 05/musl-final/src/stdlib/strtoll.c create mode 100644 05/musl-final/src/stdlib/strtoul.c create mode 100644 05/musl-final/src/stdlib/strtoull.c create mode 100644 05/musl-final/src/stdlib/strtoumax.c create mode 100644 05/musl-final/src/stdlib/wcstoimax.c create mode 100644 05/musl-final/src/stdlib/wcstol.c create mode 100644 05/musl-final/src/stdlib/wcstoll.c create mode 100644 05/musl-final/src/stdlib/wcstoul.c create mode 100644 05/musl-final/src/stdlib/wcstoull.c create mode 100644 05/musl-final/src/stdlib/wcstoumax.c create mode 100644 05/musl-final/src/string/bcmp.c create mode 100644 05/musl-final/src/string/bcopy.c create mode 100644 05/musl-final/src/string/bzero.c create mode 100644 05/musl-final/src/string/index.c create mode 100644 05/musl-final/src/string/memchr.c create mode 100644 05/musl-final/src/string/memcmp.c create mode 100644 05/musl-final/src/string/memcpy.c create mode 100644 05/musl-final/src/string/memmove.c create mode 100644 05/musl-final/src/string/mempcpy.c create mode 100644 05/musl-final/src/string/memset.c create mode 100644 05/musl-final/src/string/rindex.c create mode 100644 05/musl-final/src/string/stpcpy.c create mode 100644 05/musl-final/src/string/stpncpy.c create mode 100644 05/musl-final/src/string/strcasecmp.c create mode 100644 05/musl-final/src/string/strcasestr.c create mode 100644 05/musl-final/src/string/strcat.c create mode 100644 05/musl-final/src/string/strchr.c create mode 100644 05/musl-final/src/string/strchrnul.c create mode 100644 05/musl-final/src/string/strcmp.c create mode 100644 05/musl-final/src/string/strcpy.c create mode 100644 05/musl-final/src/string/strcspn.c create mode 100644 05/musl-final/src/string/strdup.c create mode 100644 05/musl-final/src/string/strerror_r.c create mode 100644 05/musl-final/src/string/strlcat.c create mode 100644 05/musl-final/src/string/strlcpy.c create mode 100644 05/musl-final/src/string/strlen.c create mode 100644 05/musl-final/src/string/strncasecmp.c create mode 100644 05/musl-final/src/string/strncat.c create mode 100644 05/musl-final/src/string/strncmp.c create mode 100644 05/musl-final/src/string/strncpy.c create mode 100644 05/musl-final/src/string/strndup.c create mode 100644 05/musl-final/src/string/strnlen.c create mode 100644 05/musl-final/src/string/strpbrk.c create mode 100644 05/musl-final/src/string/strrchr.c create mode 100644 05/musl-final/src/string/strsep.c create mode 100644 05/musl-final/src/string/strsignal.c create mode 100644 05/musl-final/src/string/strspn.c create mode 100644 05/musl-final/src/string/strstr.c create mode 100644 05/musl-final/src/string/strtok.c create mode 100644 05/musl-final/src/string/strtok_r.c create mode 100644 05/musl-final/src/string/swab.c create mode 100644 05/musl-final/src/string/wcscat.c create mode 100644 05/musl-final/src/string/wcschr.c create mode 100644 05/musl-final/src/string/wcscmp.c create mode 100644 05/musl-final/src/string/wcscpy.c create mode 100644 05/musl-final/src/string/wcscspn.c create mode 100644 05/musl-final/src/string/wcslen.c create mode 100644 05/musl-final/src/string/wcsncat.c create mode 100644 05/musl-final/src/string/wcsncmp.c create mode 100644 05/musl-final/src/string/wcsncpy.c create mode 100644 05/musl-final/src/string/wcspbrk.c create mode 100644 05/musl-final/src/string/wcsrchr.c create mode 100644 05/musl-final/src/string/wcsspn.c create mode 100644 05/musl-final/src/string/wcsstr.c create mode 100644 05/musl-final/src/string/wcswcs.c create mode 100644 05/musl-final/src/string/wmemchr.c create mode 100644 05/musl-final/src/string/wmemcmp.c create mode 100644 05/musl-final/src/string/wmemcpy.c create mode 100644 05/musl-final/src/string/wmemmove.c create mode 100644 05/musl-final/src/string/wmemset.c create mode 100644 05/musl-final/src/stub/utmpx.c create mode 100644 05/musl-final/src/syscall.s create mode 100644 05/musl-final/src/temp/mkdtemp.c create mode 100644 05/musl-final/src/temp/mkstemp.c create mode 100644 05/musl-final/src/temp/mktemp.c create mode 100644 05/musl-final/src/termios/cfgetospeed.c create mode 100644 05/musl-final/src/termios/cfsetospeed.c create mode 100644 05/musl-final/src/termios/tcdrain.c create mode 100644 05/musl-final/src/termios/tcflow.c create mode 100644 05/musl-final/src/termios/tcflush.c create mode 100644 05/musl-final/src/termios/tcgetattr.c create mode 100644 05/musl-final/src/termios/tcgetsid.c create mode 100644 05/musl-final/src/termios/tcsendbreak.c create mode 100644 05/musl-final/src/termios/tcsetattr.c create mode 100644 05/musl-final/src/thread/__futex.c create mode 100644 05/musl-final/src/thread/__lock.c create mode 100644 05/musl-final/src/thread/__set_thread_area.c create mode 100644 05/musl-final/src/thread/__timedwait.c create mode 100644 05/musl-final/src/thread/__unmapself.c create mode 100644 05/musl-final/src/thread/__wait.c create mode 100644 05/musl-final/src/thread/__wake.c create mode 100644 05/musl-final/src/thread/cancellation.c create mode 100644 05/musl-final/src/thread/clone.c create mode 100644 05/musl-final/src/thread/i386/__set_thread_area.s create mode 100644 05/musl-final/src/thread/i386/__unmapself.s create mode 100644 05/musl-final/src/thread/i386/clone.s create mode 100644 05/musl-final/src/thread/pthread_attr_destroy.c create mode 100644 05/musl-final/src/thread/pthread_attr_getdetachstate.c create mode 100644 05/musl-final/src/thread/pthread_attr_getguardsize.c create mode 100644 05/musl-final/src/thread/pthread_attr_getscope.c create mode 100644 05/musl-final/src/thread/pthread_attr_getstacksize.c create mode 100644 05/musl-final/src/thread/pthread_attr_init.c create mode 100644 05/musl-final/src/thread/pthread_attr_setdetachstate.c create mode 100644 05/musl-final/src/thread/pthread_attr_setguardsize.c create mode 100644 05/musl-final/src/thread/pthread_attr_setscope.c create mode 100644 05/musl-final/src/thread/pthread_attr_setstacksize.c create mode 100644 05/musl-final/src/thread/pthread_barrier_destroy.c create mode 100644 05/musl-final/src/thread/pthread_barrier_init.c create mode 100644 05/musl-final/src/thread/pthread_barrier_wait.c create mode 100644 05/musl-final/src/thread/pthread_cancel.c create mode 100644 05/musl-final/src/thread/pthread_cond_broadcast.c create mode 100644 05/musl-final/src/thread/pthread_cond_destroy.c create mode 100644 05/musl-final/src/thread/pthread_cond_init.c create mode 100644 05/musl-final/src/thread/pthread_cond_signal.c create mode 100644 05/musl-final/src/thread/pthread_cond_timedwait.c create mode 100644 05/musl-final/src/thread/pthread_cond_wait.c create mode 100644 05/musl-final/src/thread/pthread_create.c create mode 100644 05/musl-final/src/thread/pthread_detach.c create mode 100644 05/musl-final/src/thread/pthread_equal.c create mode 100644 05/musl-final/src/thread/pthread_getspecific.c create mode 100644 05/musl-final/src/thread/pthread_join.c create mode 100644 05/musl-final/src/thread/pthread_key_create.c create mode 100644 05/musl-final/src/thread/pthread_key_delete.c create mode 100644 05/musl-final/src/thread/pthread_kill.c create mode 100644 05/musl-final/src/thread/pthread_mutex_destroy.c create mode 100644 05/musl-final/src/thread/pthread_mutex_init.c create mode 100644 05/musl-final/src/thread/pthread_mutex_lock.c create mode 100644 05/musl-final/src/thread/pthread_mutex_timedlock.c create mode 100644 05/musl-final/src/thread/pthread_mutex_trylock.c create mode 100644 05/musl-final/src/thread/pthread_mutex_unlock.c create mode 100644 05/musl-final/src/thread/pthread_mutexattr_destroy.c create mode 100644 05/musl-final/src/thread/pthread_mutexattr_gettype.c create mode 100644 05/musl-final/src/thread/pthread_mutexattr_init.c create mode 100644 05/musl-final/src/thread/pthread_mutexattr_settype.c create mode 100644 05/musl-final/src/thread/pthread_once.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_destroy.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_init.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_rdlock.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_timedrdlock.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_timedwrlock.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_tryrdlock.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_trywrlock.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_unlock.c create mode 100644 05/musl-final/src/thread/pthread_rwlock_wrlock.c create mode 100644 05/musl-final/src/thread/pthread_self.c create mode 100644 05/musl-final/src/thread/pthread_setcancelstate.c create mode 100644 05/musl-final/src/thread/pthread_setcanceltype.c create mode 100644 05/musl-final/src/thread/pthread_setspecific.c create mode 100644 05/musl-final/src/thread/pthread_spin_destroy.c create mode 100644 05/musl-final/src/thread/pthread_spin_init.c create mode 100644 05/musl-final/src/thread/pthread_spin_lock.c create mode 100644 05/musl-final/src/thread/pthread_spin_trylock.c create mode 100644 05/musl-final/src/thread/pthread_spin_unlock.c create mode 100644 05/musl-final/src/thread/pthread_testcancel.c create mode 100644 05/musl-final/src/thread/x86_64/__set_thread_area.s create mode 100644 05/musl-final/src/thread/x86_64/__unmapself.s create mode 100644 05/musl-final/src/thread/x86_64/clone.s create mode 100644 05/musl-final/src/time/__asctime.c create mode 100644 05/musl-final/src/time/__time.h create mode 100644 05/musl-final/src/time/__time_to_tm.c create mode 100644 05/musl-final/src/time/__tm_to_time.c create mode 100644 05/musl-final/src/time/asctime.c create mode 100644 05/musl-final/src/time/asctime_r.c create mode 100644 05/musl-final/src/time/clock.c create mode 100644 05/musl-final/src/time/clock_gettime.c create mode 100644 05/musl-final/src/time/ctime.c create mode 100644 05/musl-final/src/time/ctime_r.c create mode 100644 05/musl-final/src/time/difftime.c create mode 100644 05/musl-final/src/time/gettimeofday.c create mode 100644 05/musl-final/src/time/gmtime.c create mode 100644 05/musl-final/src/time/gmtime_r.c create mode 100644 05/musl-final/src/time/localtime.c create mode 100644 05/musl-final/src/time/localtime_r.c create mode 100644 05/musl-final/src/time/mktime.c create mode 100644 05/musl-final/src/time/nanosleep.c create mode 100644 05/musl-final/src/time/strftime.c create mode 100644 05/musl-final/src/time/strptime.c create mode 100644 05/musl-final/src/time/time.c create mode 100644 05/musl-final/src/time/times.c create mode 100644 05/musl-final/src/time/timezone.s create mode 100644 05/musl-final/src/time/tzset.c create mode 100644 05/musl-final/src/time/utime.c create mode 100644 05/musl-final/src/unistd/_exit.c create mode 100644 05/musl-final/src/unistd/access.c create mode 100644 05/musl-final/src/unistd/alarm.c create mode 100644 05/musl-final/src/unistd/chdir.c create mode 100644 05/musl-final/src/unistd/chown.c create mode 100644 05/musl-final/src/unistd/close.c create mode 100644 05/musl-final/src/unistd/ctermid.c create mode 100644 05/musl-final/src/unistd/dup.c create mode 100644 05/musl-final/src/unistd/dup2.c create mode 100644 05/musl-final/src/unistd/faccessat.c create mode 100644 05/musl-final/src/unistd/fchdir.c create mode 100644 05/musl-final/src/unistd/fchown.c create mode 100644 05/musl-final/src/unistd/fchownat.c create mode 100644 05/musl-final/src/unistd/fdatasync.c create mode 100644 05/musl-final/src/unistd/fsync.c create mode 100644 05/musl-final/src/unistd/ftruncate.c create mode 100644 05/musl-final/src/unistd/getcwd.c create mode 100644 05/musl-final/src/unistd/getegid.c create mode 100644 05/musl-final/src/unistd/geteuid.c create mode 100644 05/musl-final/src/unistd/getgid.c create mode 100644 05/musl-final/src/unistd/getgroups.c create mode 100644 05/musl-final/src/unistd/gethostname.c create mode 100644 05/musl-final/src/unistd/getlogin.c create mode 100644 05/musl-final/src/unistd/getlogin_r.c create mode 100644 05/musl-final/src/unistd/getpgid.c create mode 100644 05/musl-final/src/unistd/getpgrp.c create mode 100644 05/musl-final/src/unistd/getpid.c create mode 100644 05/musl-final/src/unistd/getppid.c create mode 100644 05/musl-final/src/unistd/getsid.c create mode 100644 05/musl-final/src/unistd/getuid.c create mode 100644 05/musl-final/src/unistd/isatty.c create mode 100644 05/musl-final/src/unistd/lchown.c create mode 100644 05/musl-final/src/unistd/link.c create mode 100644 05/musl-final/src/unistd/linkat.c create mode 100644 05/musl-final/src/unistd/lseek.c create mode 100644 05/musl-final/src/unistd/nice.c create mode 100644 05/musl-final/src/unistd/pause.c create mode 100644 05/musl-final/src/unistd/pipe.c create mode 100644 05/musl-final/src/unistd/pread.c create mode 100644 05/musl-final/src/unistd/pwrite.c create mode 100644 05/musl-final/src/unistd/read.c create mode 100644 05/musl-final/src/unistd/readlink.c create mode 100644 05/musl-final/src/unistd/readlinkat.c create mode 100644 05/musl-final/src/unistd/readv.c create mode 100644 05/musl-final/src/unistd/renameat.c create mode 100644 05/musl-final/src/unistd/rmdir.c create mode 100644 05/musl-final/src/unistd/setegid.c create mode 100644 05/musl-final/src/unistd/seteuid.c create mode 100644 05/musl-final/src/unistd/setgid.c create mode 100644 05/musl-final/src/unistd/setpgid.c create mode 100644 05/musl-final/src/unistd/setpgrp.c create mode 100644 05/musl-final/src/unistd/setregid.c create mode 100644 05/musl-final/src/unistd/setreuid.c create mode 100644 05/musl-final/src/unistd/setsid.c create mode 100644 05/musl-final/src/unistd/setuid.c create mode 100644 05/musl-final/src/unistd/sleep.c create mode 100644 05/musl-final/src/unistd/symlink.c create mode 100644 05/musl-final/src/unistd/symlinkat.c create mode 100644 05/musl-final/src/unistd/sync.c create mode 100644 05/musl-final/src/unistd/tcgetpgrp.c create mode 100644 05/musl-final/src/unistd/tcsetpgrp.c create mode 100644 05/musl-final/src/unistd/truncate.c create mode 100644 05/musl-final/src/unistd/ttyname.c create mode 100644 05/musl-final/src/unistd/ttyname_r.c create mode 100644 05/musl-final/src/unistd/ualarm.c create mode 100644 05/musl-final/src/unistd/unlink.c create mode 100644 05/musl-final/src/unistd/unlinkat.c create mode 100644 05/musl-final/src/unistd/usleep.c create mode 100644 05/musl-final/src/unistd/write.c create mode 100644 05/musl-final/src/unistd/writev.c create mode 100644 05/musl-final/tools/gen-musl-gcc.sh diff --git a/.helix/.ignore b/.helix/.ignore deleted file mode 100644 index 347cc95..0000000 --- a/.helix/.ignore +++ /dev/null @@ -1,10 +0,0 @@ -00* -01* -02* -03* -04* -05* -06/deps/lua- -06/deps/lua-* - -tofix diff --git a/05/Makefile b/05/Makefile index bb8e458..ab43748 100644 --- a/05/Makefile +++ b/05/Makefile @@ -2,7 +2,7 @@ TCCDIR=tcc-0.9.27 TCC=$(TCCDIR)/tcc TCC0=$(TCC)0 TCCINST=tcc-bootstrap -all: out04 a.out $(TCCDIR)/lib/libtcc1.a +all: testapp in04: *.b ../04a/out04 ../04a/out04 main.b in04 out04: in04 ../04/out03 @@ -13,12 +13,6 @@ out04: in04 ../04/out03 ./out04 $< $@ a.out: main.c *.h out04 ./out04 -test.out: test.s.o test.c.o - $(TCC0) -static -nostdlib test.s.o test.c.o -o test.out -test.s.o: $(TCC0) test.s - $(TCC0) -static -nostdlib -c test.s -o test.s.o -test.c.o: $(TCC0) test.c - $(TCC0) -static -nostdlib -c test.c -o test.c.o $(TCC0): $(TCCDIR)/*.c $(TCCDIR)/*.h out04 cd $(TCCDIR) && ../out04 tcc.c tcc0 $(TCCDIR)/lib/libtcc1.a: $(TCC0) $(TCCDIR)/lib/*.[cS] @@ -37,6 +31,11 @@ tcc-files: $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/include/*.h $(TCC): $(TCC0) musl cd $(TCCDIR) && ./tcc0 -nostdinc -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include tcc.c ../musl-bootstrap/lib/*.[oa] -o tcc tcc: $(TCC) +musl-final: tcc + $(MAKE) -j8 -C musl-final + $(MAKE) -C musl-final install +testapp: tcc musl-final + $(TCC) -Wall -g -static -nostdinc -nostdlib -I musl-final/include test.c musl-final/lib/*.[oa] -MD -o test $(TCC)2: $(TCC)1 cd $(TCCDIR) && ./tcc1 tcc.c -o tcc2 @@ -45,6 +44,9 @@ $(TCC)0a: $(TCCDIR)/*.c $(TCCDIR)/*.h $(TCC)1a: $(TCCDIR)/*.c $(TCCDIR)/*.h cd $(TCCDIR) && ./tcc0a tcc.c -o tcc1a clean: + $(MAKE) -C musl-final clean + $(MAKE) -C musl-0.6.0 clean rm -rf musl-bootstrap + rm -rf musl-bootstrap-final rm -rf tcc-bootstrap rm -f out* *.out *.o $(TCCDIR)/tcc[0123456] $(TCCDIR)/tcc[0123456]a $(TCCDIR)/lib/*.[oa] diff --git a/05/musl-0.6.0/config.mak b/05/musl-0.6.0/config.mak index 81b8052..6c1acde 100644 --- a/05/musl-0.6.0/config.mak +++ b/05/musl-0.6.0/config.mak @@ -14,7 +14,7 @@ exec_prefix = ../musl-bootstrap # Uncomment if you want to build i386 musl on a 64-bit host #CFLAGS += -m32 -#CFLAGS += -g +CFLAGS += -g # Uncomment for smaller code size. #CFLAGS += -fomit-frame-pointer -mno-accumulate-outgoing-args diff --git a/05/musl-final/.gitignore b/05/musl-final/.gitignore new file mode 100644 index 0000000..16b8a37 --- /dev/null +++ b/05/musl-final/.gitignore @@ -0,0 +1,3 @@ +*.o +*.a +musl-gcc diff --git a/05/musl-final/COPYING b/05/musl-final/COPYING new file mode 100644 index 0000000..223ede7 --- /dev/null +++ b/05/musl-final/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/05/musl-final/COPYRIGHT b/05/musl-final/COPYRIGHT new file mode 100644 index 0000000..92d8992 --- /dev/null +++ b/05/musl-final/COPYRIGHT @@ -0,0 +1,40 @@ +musl as a whole is licensed under the GNU LGPL version 2.1 or later. +See the file COPYING for the text of this license. + +See below for the copyright status on all code included in musl: + +The TRE regular expression implementation (src/regex/reg* and +src/regex/tre*) is Copyright © 2001-2006 Ville Laurikari and licensed +under the terms of the GNU LGPL version 2.1 or later. The included +version was heavily modified in Spring 2006 by Rich Felker in the +interests of size, simplicity, and namespace cleanliness. + +Most of the math library code (src/math/*) is Copyright © 1993 Sun +Microsystems, Inc. Some files are Copyright © 2003 Steven G. Kargl and +labelled as such. All have been licensed under extremely permissive +terms which are compatible with the GNU LGPL. See the comments in the +individual files for details. + +The implementation of DES for crypt (src/misc/crypt.c) is Copyright © +1994 David Burren. It is licensed under a BSD license compatible with +the GNU LGPL. + +The x86_64 port was written by Nicholas J. Kain. See individual files +for their copyright status. + +All files which have no copyright comments are original works +Copyright © 2005-2011 Rich Felker, the main author of this library. +The decision to exclude such comments is intentional, as it should be +possible to carry around the complete source code on tiny storage +media. All public header files (include/*) should be treated as Public +Domain as they intentionally contain no content which can be covered +by copyright. Some source modules may fall in this category as well. +If you believe that a file is so trivial that it should be in the +Public Domain, please contact me and, if I agree, I will explicitly +release it from copyright. + +The following files are trivial, in my opinion not copyrightable in +the first place, and hereby explicitly released to the Public Domain: + +All public headers: include/* +Startup files: crt/* diff --git a/05/musl-final/INSTALL b/05/musl-final/INSTALL new file mode 100644 index 0000000..5dd7b67 --- /dev/null +++ b/05/musl-final/INSTALL @@ -0,0 +1,58 @@ + +A quick-and-simple guide to installing musl: + + +STEP 1: Configuration + +Edit config.mak to override installation prefix, compiler options, +target architecture, etc. as needed. Currently supported archs are +i386 and x86_64. Otherwise, the defaults should be okay for trying out +musl with static linking only. + +DO NOT set the prefix to /, /usr, or even /usr/local unless you really +know what you're doing! You'll probably break your system such that +you'll no longer be able to compile and link programs against glibc! +This kind of setup should only be used if you're building a system +where musl is the default/primary/only libc. + +The default prefix is /usr/local/musl for a reason, but some people +may prefer /opt/musl or $HOME/musl. + + +STEP 2: Compiling + +Run "make". (GNU make is required.) + + +STEP 3: Installation + +With appropriate privileges, run "make install". + + +STEP 4: Using the gcc wrapper. + +musl comes with a script "musl-gcc" (installed in /usr/local/bin by +default) that can be used to compile and link C programs against musl. +It requires a version of gcc with the -wrapper option (gcc 4.x should +work). For example: + +cat > hello.c < +int main() +{ + printf("hello, world!\n"); + return 0; +} +EOF +musl-gcc hello.c +./a.out + +For compiling programs that use autoconf, you'll need to configure +them with a command like this: + +CC=musl-gcc ./configure + +Be aware that (at present) libraries linked against glibc are unlikely +to be usable, and the musl-gcc wrapper inhibits search of the system +library paths in any case. You'll need to compile any prerequisite +libraries (like ncurses, glib, etc.) yourself. diff --git a/05/musl-final/Makefile b/05/musl-final/Makefile new file mode 100644 index 0000000..6c89eb1 --- /dev/null +++ b/05/musl-final/Makefile @@ -0,0 +1,113 @@ +# +# Makefile for musl (requires GNU make) +# +# This is how simple every makefile should be... +# No, I take that back - actually most should be less than half this size. +# +# Use config.mak to override any of the following variables. +# Do not make changes here. +# + +CC = ../tcc-0.9.27/tcc +exec_prefix = ../musl-bootstrap-final +bindir = $(exec_prefix)/bin + +prefix = /usr/local/musl-bootstrap +includedir = $(prefix)/include +libdir = $(prefix)/lib + +SRCS = $(sort $(wildcard src/*/*.c)) +OBJS = $(SRCS:.c=.o) src/alloca86_64-bt.o src/alloca86_64.o src/libtcc1.o src/va_list.o src/syscall.o +LOBJS = $(OBJS:.o=.lo) +GENH = include/bits/alltypes.h + +CFLAGS = -Os -nostdinc -ffreestanding -std=c99 -D_XOPEN_SOURCE=700 -pipe +LDFLAGS = -nostdlib -shared -Wl,-Bsymbolic +INC = -I./include -I./src/internal -I./arch/$(ARCH) +PIC = -fPIC +AR = $(CC) -ar +RANLIB = +OBJCOPY = + +ALL_INCLUDES = $(sort $(wildcard include/*.h include/*/*.h) $(GENH)) + +EMPTY_LIB_NAMES = m rt pthread crypt util xnet resolv +EMPTY_LIBS = $(EMPTY_LIB_NAMES:%=lib/lib%.a) +CRT_LIBS = lib/crt1.o lib/crti.o lib/crtn.o +LIBC_LIBS = lib/libc.a +ALL_LIBS = $(LIBC_LIBS) $(CRT_LIBS) + +ALL_TOOLS = tools/musl-gcc + +-include config.mak + +all: $(ALL_LIBS) $(ALL_TOOLS) + +install: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(ALL_INCLUDES:include/%=$(DESTDIR)$(includedir)/%) $(ALL_TOOLS:tools/%=$(DESTDIR)$(bindir)/%) + +src/syscall.o: src/syscall.s + $(CC) -c -o $@ $< +src/%.o: ../tcc-0.9.27/lib/%.o + cp $< $@ +clean: + rm -f crt/*.o + rm -f $(OBJS) + rm -f $(LOBJS) + rm -f $(ALL_LIBS) lib/*.[ao] lib/*.so + rm -f $(ALL_TOOLS) + rm -f $(GENH) + rm -f include/bits + +include/bits: + @test "$(ARCH)" || { echo "Please set ARCH in config.mak before running make." ; exit 1 ; } + ln -sf ../arch/$(ARCH)/bits $@ + +include/bits/alltypes.h.sh: include/bits + +include/bits/alltypes.h: include/bits/alltypes.h.sh + sh $< > $@ + +%.o: $(ARCH)/%.s + $(CC) $(CFLAGS) $(INC) -c -o $@ $< + +%.o: $(ARCH)/%.s + $(CC) $(CFLAGS) $(INC) -c -o $@ $< + +%.o: %.c $(GENH) + $(CC) $(CFLAGS) $(INC) -c -o $@ $< + +%.lo: $(ARCH)/%.s + $(CC) $(CFLAGS) $(INC) $(PIC) -c -o $@ $< + +%.lo: %.c $(GENH) + $(CC) $(CFLAGS) $(INC) $(PIC) -c -o $@ $< + +lib/libc.so: $(LOBJS) + $(CC) $(LDFLAGS) -o $@ $(LOBJS) -lgcc + $(OBJCOPY) --weaken $@ + +lib/libc.a: $(OBJS) + mkdir -p lib + $(AR) rc $@ $(OBJS) + # $(RANLIB) $@ + +$(EMPTY_LIBS): + rm -f $@ + $(AR) rc $@ + +lib/%.o: crt/%.o + cp $< $@ + +tools/musl-gcc: tools/gen-musl-gcc.sh config.mak + sh $< "$(prefix)" > $@ || { rm -f $@ ; exit 1 ; } + chmod +x $@ + +$(DESTDIR)$(bindir)/%: tools/% + install -D $< $@ + +$(DESTDIR)$(prefix)/%: % + install -D -m 644 $< $@ + +.PRECIOUS: $(CRT_LIBS:lib/%=crt/%) + +.PHONY: all clean install diff --git a/05/musl-final/README b/05/musl-final/README new file mode 100644 index 0000000..a57541c --- /dev/null +++ b/05/musl-final/README @@ -0,0 +1,44 @@ + +musl libc - a new standard library to power a new generation of +Linux-based devices. musl is lightweight, fast, simple, free, and +strives to be correct in the sense of standards-conformance and +safety. + +musl is an alternative to glibc, eglibc, uClibc, dietlibc, and klibc. +For reasons why one might prefer musl, please see the FAQ and libc +comparison chart on the project website, + + http://www.etalabs.net/musl/ + +For installation instructions, see the INSTALL file. + +Please refer to the COPYRIGHT file for details on the copyright status +of code included in musl, and the COPYING file for the license (LGPL) +under which the library as a whole is distributed. + + + +Greetings libc hackers! + +This package is an _alpha_ release of musl, intended for the curious +and the adventurous. While it can be used to build a complete small +Linux system (musl is self-hosted on the system I use to develop it), +at this point doing so requires a lot of manual effort. Nonetheless, I +hope low-level Linux enthusiasts will try out building some compact +static binaries with musl using the provided gcc wrapper (which allows +you to link programs with musl on a "standard" glibc Linux system), +find whatever embarassing bugs I've let slip through, and provide +feedback on issues encountered building various software against musl. + +Please visit #musl on Freenode IRC or contact me via email at dalias +AT etalabs DOT net for bug reports, support requests, or to get +involved in development. As this has been a one-person project so far, +mailing lists will be setup in due time on an as-needed basis. + +Thank you for trying out musl. + +Cheers, + +Rich Felker / dalias + + diff --git a/05/musl-final/WHATSNEW b/05/musl-final/WHATSNEW new file mode 100644 index 0000000..360d467 --- /dev/null +++ b/05/musl-final/WHATSNEW @@ -0,0 +1,40 @@ +0.5.0 - initial release + +0.5.9 - signal ABI bugfix, various cleanup and fixes: + +sigset_t was wrongly defined as 1024 bytes instead of 1024 bits, +breaking the intended ABI compatibility with the LSB/glibc sigaction +structure. users should upgrade immediately and rebuild any libraries +or object files that might be using the incorrect definitions. + +improved security against DoS with tcb shadow passwords by checking +that the file opened was really an ordinary file. + +fixed a bug in the implementation of atomic ops that could have +allowed the compiler to incorrectly reorder them (in practice, gcc +with the default settings on i386 was not reordering them). + +greatly improved conformance to the C and POSIX standards regarding +what the standard header files make visible. _POSIX_C_SOURCE is now +needed to get POSIX functions in standard C headers, and _XOPEN_SOURCE +or _GNU_SOURCE are required to get XSI interfaces or GNU extensions, +respectively. + +many internal improvements have been made to the syscall-related code +in preparation for porting to x86_64 and other archs. + +0.6.0 - x86_64 port, various important bugs fixed + +new x86_64 (amd64) architecture port, contributed by Nicholas J. Kain, +along with PORTING guide. source tree layout and build system have +been improved to accommodate further ports. + +various bugs that were introduced while making the headers respect C +and POSIX namespace standards have been fixed. conformance to the +standards has been improved. + +fixed an inefficiency in qsort that triggered a bug (occasionaly +internal compiler error) in some versions of gcc. + +fixed a major bug in the printf %n specifier that prevented it from +working and caused memory corruption. diff --git a/05/musl-final/arch/i386/atomic.h b/05/musl-final/arch/i386/atomic.h new file mode 100644 index 0000000..e74e453 --- /dev/null +++ b/05/musl-final/arch/i386/atomic.h @@ -0,0 +1,110 @@ +#ifndef _INTERNAA_ATOMIC_H +#define _INTERNAA_ATOMIC_H + +#include + +static inline int a_ctz_64(uint64_t x) +{ + int r; + __asm__( "bsf %1,%0 ; jnz 1f ; bsf %2,%0 ; addl $32,%0\n1:" + : "=r"(r) : "r"((unsigned)x), "r"((unsigned)(x>>32)) ); + return r; +} + + +static inline void a_and_64(volatile uint64_t *p, uint64_t v) +{ + __asm__( "lock ; andl %1, (%0) ; lock ; andl %2, 4(%0)" + : : "r"((long *)p), "r"((unsigned)v), "r"((unsigned)(v>>32)) : "memory" ); +} + +static inline void a_or_64(volatile uint64_t *p, uint64_t v) +{ + __asm__( "lock ; orl %1, (%0) ; lock ; orl %2, 4(%0)" + : : "r"((long *)p), "r"((unsigned)v), "r"((unsigned)(v>>32)) : "memory" ); +} + +static inline void a_store_l(volatile void *p, long x) +{ + __asm__( "movl %1, %0" : "=m"(*(long *)p) : "r"(x) : "memory" ); +} + +static inline void a_or_l(volatile void *p, long v) +{ + __asm__( "lock ; orl %1, %0" + : "=m"(*(long *)p) : "r"(v) : "memory" ); +} + +static inline void *a_cas_p(volatile void *p, void *t, void *s) +{ + __asm__( "lock ; cmpxchg %3, %1" + : "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) : "memory" ); + return t; +} + +static inline long a_cas_l(volatile void *p, long t, long s) +{ + __asm__( "lock ; cmpxchg %3, %1" + : "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) : "memory" ); + return t; +} + +static inline void *a_swap_p(void *volatile *x, void *v) +{ + __asm__( "xchg %0, %1" : "=r"(v), "=m"(*(void **)x) : "0"(v) : "memory" ); + return v; +} +static inline long a_swap_l(volatile void *x, long v) +{ + __asm__( "xchg %0, %1" : "=r"(v), "=m"(*(long *)x) : "0"(v) : "memory" ); + return v; +} + +static inline void a_or(volatile void *p, int v) +{ + __asm__( "lock ; orl %1, %0" + : "=m"(*(int *)p) : "r"(v) : "memory" ); +} + +static inline void a_and(volatile void *p, int v) +{ + __asm__( "lock ; andl %1, %0" + : "=m"(*(int *)p) : "r"(v) : "memory" ); +} + +static inline int a_swap(volatile int *x, int v) +{ + __asm__( "xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" ); + return v; +} + +#define a_xchg a_swap + +static inline int a_fetch_add(volatile int *x, int v) +{ + __asm__( "lock ; xadd %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" ); + return v; +} + +static inline void a_inc(volatile int *x) +{ + __asm__( "lock ; incl %0" : "=m"(*x) : "m"(*x) : "memory" ); +} + +static inline void a_dec(volatile int *x) +{ + __asm__( "lock ; decl %0" : "=m"(*x) : "m"(*x) : "memory" ); +} + +static inline void a_store(volatile int *p, int x) +{ + __asm__( "movl %1, %0" : "=m"(*p) : "r"(x) : "memory" ); +} + +static inline void a_spin() +{ + __asm__ __volatile__( "pause" : : : "memory" ); +} + + +#endif diff --git a/05/musl-final/arch/i386/bits/alltypes.h.sh b/05/musl-final/arch/i386/bits/alltypes.h.sh new file mode 100755 index 0000000..27289ea --- /dev/null +++ b/05/musl-final/arch/i386/bits/alltypes.h.sh @@ -0,0 +1,131 @@ +#!/bin/sh +sed -e << EOF \ +'/^TYPEDEF/s/TYPEDEF \(.*\) \([^ ]*\);$/#if defined(__NEED_\2) \&\& !defined(__DEFINED_\2)\ +typedef \1 \2;\ +#define __DEFINED_\2\ +#endif\ +/ +/^STRUCT/s/STRUCT * \([^ ]*\) \(.*\);$/#if defined(__NEED_struct_\1) \&\& !defined(__DEFINED_struct_\1)\ +struct \1 \2;\ +#define __DEFINED_struct_\1\ +#endif\ +/ +/^UNION/s/UNION * \([^ ]*\) \(.*\);$/#if defined(__NEED_union_\1) \&\& !defined(__DEFINED_union_\1)\ +union \1 \2;\ +#define __DEFINED_union_\1\ +#endif\ +/' + +TYPEDEF unsigned size_t; +TYPEDEF int ssize_t; +TYPEDEF long ptrdiff_t; +TYPEDEF struct __va_list * va_list; + +TYPEDEF long wchar_t; +TYPEDEF long wint_t; +TYPEDEF long wctrans_t; +TYPEDEF long wctype_t; + +TYPEDEF char int8_t; +TYPEDEF short int16_t; +TYPEDEF int int32_t; +TYPEDEF long long int64_t; + +TYPEDEF unsigned char uint8_t; +TYPEDEF unsigned short uint16_t; +TYPEDEF unsigned int uint32_t; +TYPEDEF unsigned long long uint64_t; + +TYPEDEF unsigned char __uint8_t; +TYPEDEF unsigned short __uint16_t; +TYPEDEF unsigned int __uint32_t; +TYPEDEF unsigned long long __uint64_t; + +TYPEDEF int8_t int_least8_t; +TYPEDEF int16_t int_least16_t; +TYPEDEF int32_t int_least32_t; +TYPEDEF int64_t int_least64_t; + +TYPEDEF uint8_t uint_least8_t; +TYPEDEF uint16_t uint_least16_t; +TYPEDEF uint32_t uint_least32_t; +TYPEDEF uint64_t uint_least64_t; + +TYPEDEF int8_t int_fast8_t; +TYPEDEF int int_fast16_t; +TYPEDEF int int_fast32_t; +TYPEDEF int64_t int_fast64_t; + +TYPEDEF unsigned char uint_fast8_t; +TYPEDEF unsigned int uint_fast16_t; +TYPEDEF unsigned int uint_fast32_t; +TYPEDEF uint64_t uint_fast64_t; + +TYPEDEF long intptr_t; +TYPEDEF unsigned long uintptr_t; + +TYPEDEF long long intmax_t; +TYPEDEF unsigned long long uintmax_t; + +TYPEDEF long time_t; +TYPEDEF unsigned useconds_t; +TYPEDEF int suseconds_t; +STRUCT timeval { time_t tv_sec; int tv_usec; }; +STRUCT timespec { time_t tv_sec; long tv_nsec; }; + +TYPEDEF int pid_t; +TYPEDEF int id_t; +TYPEDEF int uid_t; +TYPEDEF int gid_t; +TYPEDEF int key_t; + +TYPEDEF struct __pthread * pthread_t; +TYPEDEF int pthread_once_t; +TYPEDEF int pthread_key_t; +TYPEDEF int pthread_spinlock_t; + +TYPEDEF struct { union { int __i[9]; size_t __s[2]; } __u; } pthread_attr_t; +TYPEDEF unsigned pthread_mutexattr_t; +TYPEDEF unsigned pthread_condattr_t; +TYPEDEF unsigned pthread_barrierattr_t; +TYPEDEF struct { unsigned __attr[2]; } pthread_rwlockattr_t; + +TYPEDEF struct { union { int __i[6]; void *__p[1]; } __u; } pthread_mutex_t; +TYPEDEF struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t; +TYPEDEF struct { union { int __i[8]; void *__p[1]; } __u; } pthread_rwlock_t; +TYPEDEF struct { union { int __i[5]; void *__p[1]; } __u; } pthread_barrier_t; + +TYPEDEF long long off_t; + +TYPEDEF unsigned int mode_t; + +TYPEDEF unsigned int nlink_t; +TYPEDEF unsigned long long ino_t; +TYPEDEF long long dev_t; +TYPEDEF long blksize_t; +TYPEDEF long long blkcnt_t; +TYPEDEF unsigned long long fsblkcnt_t; +TYPEDEF unsigned long long fsfilcnt_t; + +TYPEDEF void * timer_t; +TYPEDEF int clockid_t; +TYPEDEF unsigned long clock_t; + +TYPEDEF struct { unsigned long __bits[128/sizeof(long)]; } sigset_t; +TYPEDEF struct __siginfo siginfo_t; + +TYPEDEF unsigned int socklen_t; +TYPEDEF unsigned short sa_family_t; +TYPEDEF unsigned short in_port_t; +TYPEDEF unsigned int in_addr_t; +STRUCT in_addr { in_addr_t s_addr; }; + +TYPEDEF struct __FILE_s FILE; + +TYPEDEF int nl_item; + +TYPEDEF struct __locale * locale_t; + +STRUCT iovec { void *iov_base; size_t iov_len; }; + +EOF diff --git a/05/musl-final/arch/i386/bits/endian.h b/05/musl-final/arch/i386/bits/endian.h new file mode 100644 index 0000000..172c338 --- /dev/null +++ b/05/musl-final/arch/i386/bits/endian.h @@ -0,0 +1 @@ +#define __BYTE_ORDER __LITTLE_ENDIAN diff --git a/05/musl-final/arch/i386/bits/errno.h b/05/musl-final/arch/i386/bits/errno.h new file mode 100644 index 0000000..ec3d363 --- /dev/null +++ b/05/musl-final/arch/i386/bits/errno.h @@ -0,0 +1,132 @@ +#define EPERM 1 +#define ENOENT 2 +#define ESRCH 3 +#define EINTR 4 +#define EIO 5 +#define ENXIO 6 +#define E2BIG 7 +#define ENOEXEC 8 +#define EBADF 9 +#define ECHILD 10 +#define EAGAIN 11 +#define ENOMEM 12 +#define EACCES 13 +#define EFAULT 14 +#define ENOTBLK 15 +#define EBUSY 16 +#define EEXIST 17 +#define EXDEV 18 +#define ENODEV 19 +#define ENOTDIR 20 +#define EISDIR 21 +#define EINVAL 22 +#define ENFILE 23 +#define EMFILE 24 +#define ENOTTY 25 +#define ETXTBSY 26 +#define EFBIG 27 +#define ENOSPC 28 +#define ESPIPE 29 +#define EROFS 30 +#define EMLINK 31 +#define EPIPE 32 +#define EDOM 33 +#define ERANGE 34 +#define EDEADLK 35 +#define ENAMETOOLONG 36 +#define ENOLCK 37 +#define ENOSYS 38 +#define ENOTEMPTY 39 +#define ELOOP 40 +#define EWOULDBLOCK EAGAIN +#define ENOMSG 42 +#define EIDRM 43 +#define ECHRNG 44 +#define EL2NSYNC 45 +#define EL3HLT 46 +#define EL3RST 47 +#define ELNRNG 48 +#define EUNATCH 49 +#define ENOCSI 50 +#define EL2HLT 51 +#define EBADE 52 +#define EBADR 53 +#define EXFULL 54 +#define ENOANO 55 +#define EBADRQC 56 +#define EBADSLT 57 +#define EDEADLOCK EDEADLK +#define EBFONT 59 +#define ENOSTR 60 +#define ENODATA 61 +#define ETIME 62 +#define ENOSR 63 +#define ENONET 64 +#define ENOPKG 65 +#define EREMOTE 66 +#define ENOLINK 67 +#define EADV 68 +#define ESRMNT 69 +#define ECOMM 70 +#define EPROTO 71 +#define EMULTIHOP 72 +#define EDOTDOT 73 +#define EBADMSG 74 +#define EOVERFLOW 75 +#define ENOTUNIQ 76 +#define EBADFD 77 +#define EREMCHG 78 +#define ELIBACC 79 +#define ELIBBAD 80 +#define ELIBSCN 81 +#define ELIBMAX 82 +#define ELIBEXEC 83 +#define EILSEQ 84 +#define ERESTART 85 +#define ESTRPIPE 86 +#define EUSERS 87 +#define ENOTSOCK 88 +#define EDESTADDRREQ 89 +#define EMSGSIZE 90 +#define EPROTOTYPE 91 +#define ENOPROTOOPT 92 +#define EPROTONOSUPPORT 93 +#define ESOCKTNOSUPPORT 94 +#define EOPNOTSUPP 95 +#define EPFNOSUPPORT 96 +#define EAFNOSUPPORT 97 +#define EADDRINUSE 98 +#define EADDRNOTAVAIL 99 +#define ENETDOWN 100 +#define ENETUNREACH 101 +#define ENETRESET 102 +#define ECONNABORTED 103 +#define ECONNRESET 104 +#define ENOBUFS 105 +#define EISCONN 106 +#define ENOTCONN 107 +#define ESHUTDOWN 108 +#define ETOOMANYREFS 109 +#define ETIMEDOUT 110 +#define ECONNREFUSED 111 +#define EHOSTDOWN 112 +#define EHOSTUNREACH 113 +#define EALREADY 114 +#define EINPROGRESS 115 +#define ESTALE 116 +#define EUCLEAN 117 +#define ENOTNAM 118 +#define ENAVAIL 119 +#define EISNAM 120 +#define EREMOTEIO 121 +#define EDQUOT 122 +#define ENOMEDIUM 123 +#define EMEDIUMTYPE 124 +#define ECANCELED 125 +#define ENOKEY 126 +#define EKEYEXPIRED 127 +#define EKEYREVOKED 128 +#define EKEYREJECTED 129 +#define EOWNERDEAD 130 +#define ENOTRECOVERABLE 131 +#define ERFKILL 132 diff --git a/05/musl-final/arch/i386/bits/fcntl.h b/05/musl-final/arch/i386/bits/fcntl.h new file mode 100644 index 0000000..802c3d1 --- /dev/null +++ b/05/musl-final/arch/i386/bits/fcntl.h @@ -0,0 +1,60 @@ +#define O_ACCMODE 03 +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 + +#define O_CREAT 0100 +#define O_EXCL 0200 +#define O_NOCTTY 0400 +#define O_TRUNC 01000 +#define O_APPEND 02000 +#define O_NONBLOCK 04000 +#define O_SYNC 010000 +#define O_DIRECTORY 0200000 +#define O_NOFOLLOW 0400000 +#define O_CLOEXEC 02000000 + +#ifdef _GNU_SOURCE +#define O_NDELAY O_NONBLOCK +#define O_ASYNC 020000 +#define O_DIRECT 040000 +#define O_NOATIME 01000000 +#define F_DUPFD_CLOEXEC 1030 +#define FAPPENT O_APPEND +#define FFSYNC O_FSYNC +#define FASYNC O_ASYNC +#define FNONBLOCK O_NONBLOCK +#define FNDELAY O_NDELAY +#endif + +#define F_DUPFD 0 +#define F_GETFD 1 +#define F_SETFD 2 +#define F_GETFL 3 +#define F_SETFL 4 + +#define F_SETOWN 8 +#define F_GETOWN 9 + +#define F_GETLK 12 +#define F_SETLK 13 +#define F_SETLKW 14 + +#define FD_CLOEXEC 1 + +#define F_RDLCK 0 +#define F_WRLCK 1 +#define F_UNLCK 2 + +#define AT_FDCWD (-100) +#define AT_SYMLINK_NOFOLLOW 0x100 +#define AT_REMOVEDIR 0x200 +#define AT_SYMLINK_FOLLOW 0x400 +#define AT_EACCESS 0x200 + +#define POSIX_FADV_NORMAL 0 +#define POSIX_FADV_RANDOM 1 +#define POSIX_FADV_SEQUENTIAL 2 +#define POSIX_FADV_WILLNEED 3 +#define POSIX_FADV_DONTNEED 4 +#define POSIX_FADV_NOREUSE 5 diff --git a/05/musl-final/arch/i386/bits/fenv.h b/05/musl-final/arch/i386/bits/fenv.h new file mode 100644 index 0000000..24df041 --- /dev/null +++ b/05/musl-final/arch/i386/bits/fenv.h @@ -0,0 +1,34 @@ +#define FE_INVALID 1 +#define __FE_DENORM 2 +#define FE_DIVBYZERO 4 +#define FE_OVERFLOW 8 +#define FE_UNDERFLOW 16 +#define FE_INEXACT 32 + +#define FE_ALL_EXCEPT 63 + +#define FE_TONEAREST 0 +#define FE_DOWNWARD 0x400 +#define FE_UPWARD 0x800 +#define FE_TOWARDZERO 0xc00 + +typedef unsigned short fexcept_t; + +typedef struct { + unsigned short __control_word; + unsigned short __unused1; + unsigned short __status_word; + unsigned short __unused2; + unsigned short __tags; + unsigned short __unused3; + unsigned int __eip; + unsigned short __cs_selector; + unsigned int __opcode:11; + unsigned int __unused4:5; + unsigned int __data_offset; + unsigned short __data_selector; + unsigned short __unused5; + unsigned int __mxcsr; +} fenv_t; + +#define FE_DFL_ENV ((const fenv_t *) -1) diff --git a/05/musl-final/arch/i386/bits/float.h b/05/musl-final/arch/i386/bits/float.h new file mode 100644 index 0000000..162b3e4 --- /dev/null +++ b/05/musl-final/arch/i386/bits/float.h @@ -0,0 +1,11 @@ +#define LDBL_MIN 3.3621031431120935063e-4932L +#define LDBL_MAX 1.1897314953572317650e+4932L +#define LDBL_EPSILON 1.0842021724855044340e-19L + +#define LDBL_MANT_DIG 64 +#define LDBL_MIN_EXP (-16381) +#define LDBL_MAX_EXP 16384 + +#define LDBL_DIG 18 +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_10_EXP 4932 diff --git a/05/musl-final/arch/i386/bits/in.h b/05/musl-final/arch/i386/bits/in.h new file mode 100644 index 0000000..b233706 --- /dev/null +++ b/05/musl-final/arch/i386/bits/in.h @@ -0,0 +1,133 @@ +#define IP_TOS 1 +#define IP_TTL 2 +#define IP_HDRINCL 3 +#define IP_OPTIONS 4 +#define IP_ROUTER_ALERT 5 +#define IP_RECVOPTS 6 +#define IP_RETOPTS 7 +//#define IP_PKTINFO 8 +#define IP_PKTOPTIONS 9 +#define IP_PMTUDISC 10 +#define IP_MTU_DISCOVER 10 +#define IP_RECVERR 11 +#define IP_RECVTTL 12 +#define IP_RECVTOS 13 +#define IP_MTU 14 +#define IP_FREEBIND 15 +#define IP_IPSEC_POLICY 16 +#define IP_XFRM_POLICY 17 +#define IP_PASSSEC 18 +#define IP_TRANSPARENT 19 +#define IP_ORIGDSTADDR 20 +#define IP_RECVORIGDSTADDR IP_ORIGDSTADDR +#define IP_MINTTL 21 +#define IP_MULTICAST_IF 32 +#define IP_MULTICAST_TTL 33 +#define IP_MULTICAST_LOOP 34 +#define IP_ADD_MEMBERSHIP 35 +#define IP_DROP_MEMBERSHIP 36 +#define IP_UNBLOCK_SOURCE 37 +#define IP_BLOCK_SOURCE 38 +#define IP_ADD_SOURCE_MEMBERSHIP 39 +#define IP_DROP_SOURCE_MEMBERSHIP 40 +#define IP_MSFILTER 41 + +#define IP_RECVRETOPTS IP_RETOPTS + +#define IP_PMTUDISC_DONT 0 +#define IP_PMTUDISC_WANT 1 +#define IP_PMTUDISC_DO 2 +#define IP_PMTUDISC_PROBE 3 + +#define SOL_IP 0 + +#define IP_DEFAULT_MULTICAST_TTL 1 +#define IP_DEFAULT_MULTICAST_LOOP 1 +#define IP_MAX_MEMBERSHIPS 20 + +struct ip_opts +{ + struct in_addr ip_dst; + char ip_opts[40]; +}; + +struct ip_mreq +{ + struct in_addr imr_multiaddr; + struct in_addr imr_interface; +}; + +struct ip_mreqn +{ + struct in_addr imr_multiaddr; + struct in_addr imr_address; + int imr_ifindex; +}; + +struct in_pktinfo +{ + int ipi_ifindex; + struct in_addr ipi_spec_dst; + struct in_addr ipi_addr; +}; + +#define IPV6_ADDRFORM 1 +#define IPV6_2292PKTINFO 2 +#define IPV6_2292HOPOPTS 3 +#define IPV6_2292DSTOPTS 4 +#define IPV6_2292RTHDR 5 +#define IPV6_2292PKTOPTIONS 6 +#define IPV6_CHECKSUM 7 +#define IPV6_2292HOPLIMIT 8 +#define SCM_SRCRT IPV6_RXSRCRT +#define IPV6_NEXTHOP 9 +#define IPV6_AUTHHDR 10 +#define IPV6_UNICAST_HOPS 16 +#define IPV6_MULTICAST_IF 17 +#define IPV6_MULTICAST_HOPS 18 +#define IPV6_MULTICAST_LOOP 19 +#define IPV6_JOIN_GROUP 20 +#define IPV6_LEAVE_GROUP 21 +#define IPV6_ROUTER_ALERT 22 +#define IPV6_MTU_DISCOVER 23 +#define IPV6_MTU 24 +#define IPV6_RECVERR 25 +#define IPV6_V6ONLY 26 +#define IPV6_JOIN_ANYCAST 27 +#define IPV6_LEAVE_ANYCAST 28 +#define IPV6_IPSEC_POLICY 34 +#define IPV6_XFRM_POLICY 35 + +#define IPV6_RECVPKTINFO 49 +#define IPV6_PKTINFO 50 +#define IPV6_RECVHOPLIMIT 51 +#define IPV6_HOPLIMIT 52 +#define IPV6_RECVHOPOPTS 53 +#define IPV6_HOPOPTS 54 +#define IPV6_RTHDRDSTOPTS 55 +#define IPV6_RECVRTHDR 56 +#define IPV6_RTHDR 57 +#define IPV6_RECVDSTOPTS 58 +#define IPV6_DSTOPTS 59 + +#define IPV6_RECVTCLASS 66 +#define IPV6_TCLASS 67 + +#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP +#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP +#define IPV6_RXHOPOPTS IPV6_HOPOPTS +#define IPV6_RXDSTOPTS IPV6_DSTOPTS + + +#define IPV6_PMTUDISC_DONT 0 +#define IPV6_PMTUDISC_WANT 1 +#define IPV6_PMTUDISC_DO 2 +#define IPV6_PMTUDISC_PROBE 3 + +#define SOL_IPV6 41 +#define SOL_ICMPV6 58 + +#define IPV6_RTHDR_LOOSE 0 +#define IPV6_RTHDR_STRICT 1 + +#define IPV6_RTHDR_TYPE_0 0 diff --git a/05/musl-final/arch/i386/bits/ioctl.h b/05/musl-final/arch/i386/bits/ioctl.h new file mode 100644 index 0000000..336c71c --- /dev/null +++ b/05/musl-final/arch/i386/bits/ioctl.h @@ -0,0 +1,197 @@ +#define _IOC(a,b,c,d) ( ((a)<<30) | ((b)<<8) | (c) | ((d)<<16) ) +#define _IOC_NONE 0U +#define _IOC_WRITE 1U +#define _IOC_READ 2U + +#define _IO(a,b) _IOC(_IOC_NONE,(a),(b),0) +#define _IOW(a,b,c) _IOC(1,(a),(b),sizeof(c)) +#define _IOR(a,b,c) _IOC(2,(a),(b),sizeof(c)) +#define _IOWR(a,b,c) _IOC(3,(a),(b),sizeof(c)) + +#define TCGETS 0x5401 +#define TCSETS 0x5402 +#define TCSETSW 0x5403 +#define TCSETSF 0x5404 +#define TCGETA 0x5405 +#define TCSETA 0x5406 +#define TCSETAW 0x5407 +#define TCSETAF 0x5408 +#define TCSBRK 0x5409 +#define TCXONC 0x540A +#define TCFLSH 0x540B +#define TIOCEXCL 0x540C +#define TIOCNXCL 0x540D +#define TIOCSCTTY 0x540E +#define TIOCGPGRP 0x540F +#define TIOCSPGRP 0x5410 +#define TIOCOUTQ 0x5411 +#define TIOCSTI 0x5412 +#define TIOCGWINSZ 0x5413 +#define TIOCSWINSZ 0x5414 +#define TIOCMGET 0x5415 +#define TIOCMBIS 0x5416 +#define TIOCMBIC 0x5417 +#define TIOCMSET 0x5418 +#define TIOCGSOFTCAR 0x5419 +#define TIOCSSOFTCAR 0x541A +#define FIONREAD 0x541B +#define TIOCINQ FIONREAD +#define TIOCLINUX 0x541C +#define TIOCCONS 0x541D +#define TIOCGSERIAL 0x541E +#define TIOCSSERIAL 0x541F +#define TIOCPKT 0x5420 +#define FIONBIO 0x5421 +#define TIOCNOTTY 0x5422 +#define TIOCSETD 0x5423 +#define TIOCGETD 0x5424 +#define TCSBRKP 0x5425 +#define TIOCTTYGSTRUCT 0x5426 +#define TIOCSBRK 0x5427 +#define TIOCCBRK 0x5428 +#define TIOCGSID 0x5429 +#define TIOCGPTN 0x80045430 +#define TIOCSPTLCK 0x40045431 +#define TCGETX 0x5432 +#define TCSETX 0x5433 +#define TCSETXF 0x5434 +#define TCSETXW 0x5435 + +#define FIONCLEX 0x5450 +#define FIOCLEX 0x5451 +#define FIOASYNC 0x5452 +#define TIOCSERCONFIG 0x5453 +#define TIOCSERGWILD 0x5454 +#define TIOCSERSWILD 0x5455 +#define TIOCGLCKTRMIOS 0x5456 +#define TIOCSLCKTRMIOS 0x5457 +#define TIOCSERGSTRUCT 0x5458 +#define TIOCSERGETLSR 0x5459 +#define TIOCSERGETMULTI 0x545A +#define TIOCSERSETMULTI 0x545B + +#define TIOCMIWAIT 0x545C +#define TIOCGICOUNT 0x545D +#define TIOCGHAYESESP 0x545E +#define TIOCSHAYESESP 0x545F +#define FIOQSIZE 0x5460 + +#define TIOCPKT_DATA 0 +#define TIOCPKT_FLUSHREAD 1 +#define TIOCPKT_FLUSHWRITE 2 +#define TIOCPKT_STOP 4 +#define TIOCPKT_START 8 +#define TIOCPKT_NOSTOP 16 +#define TIOCPKT_DOSTOP 32 +#define TIOCPKT_IOCTL 64 + +#define TIOCSER_TEMT 0x01 + +struct winsize { + unsigned short ws_row; + unsigned short ws_col; + unsigned short ws_xpixel; + unsigned short ws_ypixel; +}; + +#define TIOCM_LE 0x001 +#define TIOCM_DTR 0x002 +#define TIOCM_RTS 0x004 +#define TIOCM_ST 0x008 +#define TIOCM_SR 0x010 +#define TIOCM_CTS 0x020 +#define TIOCM_CAR 0x040 +#define TIOCM_RNG 0x080 +#define TIOCM_DSR 0x100 +#define TIOCM_CD TIOCM_CAR +#define TIOCM_RI TIOCM_RNG +#define TIOCM_OUT1 0x2000 +#define TIOCM_OUT2 0x4000 +#define TIOCM_LOOP 0x8000 +#define TIOCM_MODEM_BITS TIOCM_OUT2 + +#define N_TTY 0 +#define N_SLIP 1 +#define N_MOUSE 2 +#define N_PPP 3 +#define N_STRIP 4 +#define N_AX25 5 +#define N_X25 6 +#define N_6PACK 7 +#define N_MASC 8 +#define N_R3964 9 +#define N_PROFIBUS_FDL 10 +#define N_IRDA 11 +#define N_SMSBLOCK 12 +#define N_HDLC 13 +#define N_SYNC_PPP 14 +#define N_HCI 15 + +#define FIOSETOWN 0x8901 +#define SIOCSPGRP 0x8902 +#define FIOGETOWN 0x8903 +#define SIOCGPGRP 0x8904 +#define SIOCATMARK 0x8905 +#define SIOCGSTAMP 0x8906 + +#define SIOCADDRT 0x890B +#define SIOCDELRT 0x890C +#define SIOCRTMSG 0x890D + +#define SIOCGIFNAME 0x8910 +#define SIOCSIFLINK 0x8911 +#define SIOCGIFCONF 0x8912 +#define SIOCGIFFLAGS 0x8913 +#define SIOCSIFFLAGS 0x8914 +#define SIOCGIFADDR 0x8915 +#define SIOCSIFADDR 0x8916 +#define SIOCGIFDSTADDR 0x8917 +#define SIOCSIFDSTADDR 0x8918 +#define SIOCGIFBRDADDR 0x8919 +#define SIOCSIFBRDADDR 0x891a +#define SIOCGIFNETMASK 0x891b +#define SIOCSIFNETMASK 0x891c +#define SIOCGIFMETRIC 0x891d +#define SIOCSIFMETRIC 0x891e +#define SIOCGIFMEM 0x891f +#define SIOCSIFMEM 0x8920 +#define SIOCGIFMTU 0x8921 +#define SIOCSIFMTU 0x8922 +#define SIOCSIFHWADDR 0x8924 +#define SIOCGIFENCAP 0x8925 +#define SIOCSIFENCAP 0x8926 +#define SIOCGIFHWADDR 0x8927 +#define SIOCGIFSLAVE 0x8929 +#define SIOCSIFSLAVE 0x8930 +#define SIOCADDMULTI 0x8931 +#define SIOCDELMULTI 0x8932 +#define SIOCGIFINDEX 0x8933 +#define SIOGIFINDEX SIOCGIFINDEX +#define SIOCSIFPFLAGS 0x8934 +#define SIOCGIFPFLAGS 0x8935 +#define SIOCDIFADDR 0x8936 +#define SIOCSIFHWBROADCAST 0x8937 +#define SIOCGIFCOUNT 0x8938 + +#define SIOCGIFBR 0x8940 +#define SIOCSIFBR 0x8941 + +#define SIOCGIFTXQLEN 0x8942 +#define SIOCSIFTXQLEN 0x8943 + +#define SIOCDARP 0x8953 +#define SIOCGARP 0x8954 +#define SIOCSARP 0x8955 + +#define SIOCDRARP 0x8960 +#define SIOCGRARP 0x8961 +#define SIOCSRARP 0x8962 + +#define SIOCGIFMAP 0x8970 +#define SIOCSIFMAP 0x8971 + +#define SIOCADDDLCI 0x8980 +#define SIOCDELDLCI 0x8981 + +#define SIOCDEVPRIVATE 0x89F0 +#define SIOCPROTOPRIVATE 0x89E0 diff --git a/05/musl-final/arch/i386/bits/ipc.h b/05/musl-final/arch/i386/bits/ipc.h new file mode 100644 index 0000000..08316a3 --- /dev/null +++ b/05/musl-final/arch/i386/bits/ipc.h @@ -0,0 +1,25 @@ +#define IPC_CREAT 01000 +#define IPC_EXCL 02000 +#define IPC_NOWAIT 04000 + +#define IPC_RMID 0 +#define IPC_SET 1 +#define IPC_STAT 2 +#ifdef _GNU_SOURCE +#define IPC_INFO 3 +#endif + +#define IPC_PRIVATE ((key_t) 0) + +struct ipc_perm +{ + key_t key; + uid_t uid; + gid_t gid; + uid_t cuid; + gid_t cgid; + mode_t mode; + int seq; + long __pad1; + long __pad2; +}; diff --git a/05/musl-final/arch/i386/bits/limits.h b/05/musl-final/arch/i386/bits/limits.h new file mode 100644 index 0000000..4e99245 --- /dev/null +++ b/05/musl-final/arch/i386/bits/limits.h @@ -0,0 +1,33 @@ +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define PIPE_BUF 4096 +#define PAGESIZE 4096 +#define PAGE_SIZE PAGESIZE +#define FILESIZEBITS 64 +#define NAME_MAX 255 +#define SYMLINK_MAX 255 +#define PATH_MAX 4096 +#define NZERO 20 +#define NGROUPS_MAX 32 +#define ARG_MAX 131072 +#define IOV_MAX 1024 +#define SYMLOOP_MAX 40 +#define WORD_BIT 32 +#define LONG_BIT 32 +#endif + +#define SHRT_MIN (-1-0x7fff) +#define SHRT_MAX 0x7fff +#define USHRT_MAX 0xffff + +#define INT_MIN (-1-0x7fffffff) +#define INT_MAX 0x7fffffff +#define UINT_MAX 0xffffffffU + +#define LONG_MIN (-1-0x7fffffffL) +#define LONG_MAX 0x7fffffffL +#define ULONG_MAX 0xffffffffUL + +#define LLONG_MIN (-1-0x7fffffffffffffffLL) +#define LLONG_MAX 0x7fffffffffffffffLL +#define ULLONG_MAX 0xffffffffffffffffULL diff --git a/05/musl-final/arch/i386/bits/mman.h b/05/musl-final/arch/i386/bits/mman.h new file mode 100644 index 0000000..d133fa2 --- /dev/null +++ b/05/musl-final/arch/i386/bits/mman.h @@ -0,0 +1,50 @@ +#define MAP_FAILED ((void *) -1) + +#define PROT_NONE 0 +#define PROT_READ 1 +#define PROT_WRITE 2 +#define PROT_EXEC 4 +#define PROT_GROWSDOWN 0x01000000 +#define PROT_GROWSUP 0x02000000 + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_FIXED 0x10 + +/* linux extensions */ +#define MAP_TYPE 0x0f +#define MAP_FILE 0x00 +#define MAP_ANON 0x20 +#define MAP_ANONYMOUS MAP_ANON +#define MAP_32BIT 0x40 + +#define MADV_NORMAL 0 +#define MADV_RANDOM 1 +#define MADV_SEQUENTIAL 2 +#define MADV_WILLNEED 3 +#define MADV_DONTNEED 4 +#define MADV_REMOVE 9 +#define MADV_DONTFORK 10 +#define MADV_DOFORK 11 +#define MADV_MERGEABLE 12 +#define MADV_UNMERGEABLE 13 +#define MADV_HUGEPAGE 14 +#define MADV_NOHUGEPAGE 15 +#define MADV_HWPOISON 100 + +#define POSIX_MADV_NORMAL 0 +#define POSIX_MADV_RANDOM 1 +#define POSIX_MADV_SEQUENTIAL 2 +#define POSIX_MADV_WILLNEED 3 +#define POSIX_MADV_DONTNEED 0 + +#define MS_ASYNC 1 +#define MS_INVALIDATE 2 +#define MS_SYNC 4 + +#define MCL_CURRENT 1 +#define MCL_FUTURE 2 + +/* linux extensions */ +#define MREMAP_MAYMOVE 1 +#define MREMAP_FIXED 2 diff --git a/05/musl-final/arch/i386/bits/posix.h b/05/musl-final/arch/i386/bits/posix.h new file mode 100644 index 0000000..30a3871 --- /dev/null +++ b/05/musl-final/arch/i386/bits/posix.h @@ -0,0 +1,2 @@ +#define _POSIX_V6_ILP32_OFFBIG 1 +#define _POSIX_V7_ILP32_OFFBIG 1 diff --git a/05/musl-final/arch/i386/bits/pthread.h b/05/musl-final/arch/i386/bits/pthread.h new file mode 100644 index 0000000..7d19065 --- /dev/null +++ b/05/musl-final/arch/i386/bits/pthread.h @@ -0,0 +1,6 @@ +struct __ptcb { + long __jb[7]; + int __dummy; + struct __ptcb *__next; + void *__ptrs[3]; +}; diff --git a/05/musl-final/arch/i386/bits/reg.h b/05/musl-final/arch/i386/bits/reg.h new file mode 100644 index 0000000..8bc2582 --- /dev/null +++ b/05/musl-final/arch/i386/bits/reg.h @@ -0,0 +1,19 @@ +#undef __WORDSIZE +#define __WORDSIZE 32 +#define EBX 0 +#define ECX 1 +#define EDX 2 +#define ESI 3 +#define EDI 4 +#define EBP 5 +#define EAX 6 +#define DS 7 +#define ES 8 +#define FS 9 +#define GS 10 +#define ORIG_EAX 11 +#define EIP 12 +#define CS 13 +#define EFL 14 +#define UESP 15 +#define SS 16 diff --git a/05/musl-final/arch/i386/bits/setjmp.h b/05/musl-final/arch/i386/bits/setjmp.h new file mode 100644 index 0000000..b6595bd --- /dev/null +++ b/05/musl-final/arch/i386/bits/setjmp.h @@ -0,0 +1 @@ +typedef unsigned long jmp_buf [7]; diff --git a/05/musl-final/arch/i386/bits/shm.h b/05/musl-final/arch/i386/bits/shm.h new file mode 100644 index 0000000..bc95a1c --- /dev/null +++ b/05/musl-final/arch/i386/bits/shm.h @@ -0,0 +1,24 @@ +#define SHMLBA 4096 + +#define SHM_RDONLY 010000 +#define SHM_RND 020000 +#define SHM_REMAP 040000 +#define SHM_EXEC 0100000 + +/* linux extensions */ +#define SHM_LOCK 11 +#define SHM_UNLOCK 12 + +struct shmid_ds +{ + struct ipc_perm shm_perm; + size_t shm_segsz; + time_t shm_atime; + time_t shm_dtime; + time_t shm_ctime; + pid_t shm_cpid; + pid_t shm_lpid; + unsigned long shm_nattch; + unsigned long __pad1; + unsigned long __pad2; +}; diff --git a/05/musl-final/arch/i386/bits/signal.h b/05/musl-final/arch/i386/bits/signal.h new file mode 100644 index 0000000..8827efd --- /dev/null +++ b/05/musl-final/arch/i386/bits/signal.h @@ -0,0 +1,107 @@ +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +struct __siginfo +{ + int si_signo; + int si_errno; + int si_code; + union + { + char __pad[128 - 3*sizeof(int)]; + struct { + pid_t si_pid; + uid_t si_uid; + union sigval si_sigval; + } __rt; + struct { + unsigned int si_timer1; + unsigned int si_timer2; + } __timer; + struct { + pid_t si_pid; + uid_t si_uid; + int si_status; + clock_t si_utime; + clock_t si_stime; + } __sigchld; + struct { + void *si_addr; + } __sigfault; + struct { + long int si_band; + int si_fd; + } __sigpoll; + } __si_fields; +}; + +#define si_pid __si_fields.__sigchld.si_pid +#define si_uid __si_fields.__sigchld.si_uid +#define si_status __si_fields.__sigchld.si_status +#define si_utime __si_fields.__sigchld.si_utime +#define si_stime __si_fields.__sigchld.si_stime +#define si_value __si_fields.__rt.si_sigval +#define si_addr __si_fields.__sigfault.si_addr +#define si_band __si_fields.__sigpoll.si_band + +#define SA_NOCLDSTOP 1 +#define SA_NOCLDWAIT 2 +#define SA_SIGINFO 4 +#define SA_ONSTACK 0x08000000 +#define SA_RESTART 0x10000000 +#define SA_NODEFER 0x40000000 +#define SA_RESETHAND 0x80000000 +#define SA_RESTORER 0x04000000 + +#define SS_ONSTACK 1 +#define SS_DISABLE 2 + +#define SIG_BLOCK 0 +#define SIG_UNBLOCK 1 +#define SIG_SETMASK 2 + +#define SIG_HOLD ((void (*)(int)) 2) + +#endif + +#ifdef _GNU_SOURCE +#define NSIG 64 +#endif + +#define SIG_ERR ((void (*)(int))-1) +#define SIG_DFL ((void (*)(int)) 0) +#define SIG_IGN ((void (*)(int)) 1) + +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGTRAP 5 +#define SIGABRT 6 +#define SIGBUS 7 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGSTKFLT 16 +#define SIGCHLD 17 +#define SIGCONT 18 +#define SIGSTOP 19 +#define SIGTSTP 20 +#define SIGTTIN 21 +#define SIGTTOU 22 +#define SIGURG 23 +#define SIGXCPU 24 +#define SIGXFSZ 25 +#define SIGVTALRM 26 +#define SIGPROF 27 +#define SIGWINCH 28 +#define SIGIO 29 +#define SIGPOLL 29 +#define SIGPWR 30 +#define SIGSYS 31 +#define SIGUNUSED SIGSYS diff --git a/05/musl-final/arch/i386/bits/socket.h b/05/musl-final/arch/i386/bits/socket.h new file mode 100644 index 0000000..b323062 --- /dev/null +++ b/05/musl-final/arch/i386/bits/socket.h @@ -0,0 +1,210 @@ +struct msghdr +{ + void *msg_name; + socklen_t msg_namelen; + struct iovec *msg_iov; + int msg_iovlen; + void *msg_control; + socklen_t msg_controllen; + int msg_flags; +}; + +struct cmsghdr +{ + socklen_t cmsg_len; + int cmsg_level; + int cmsg_type; +}; + +struct ucred +{ + pid_t pid; + uid_t uid; + gid_t gid; +}; + +struct linger +{ + int l_onoff; + int l_linger; +}; + +#define SHUT_RD 0 +#define SHUT_WD 1 +#define SHUT_RDWR 2 + +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#define SOCK_RAW 3 +#define SOCK_RDM 4 +#define SOCK_SEQPACKET 5 +#define SOCK_DCCP 6 +#define SOCK_PACKET 10 + +/* linux extensions */ +#define SOCK_CLOEXEC 02000000 +#define SOCK_NONBLOCK 04000 + +#define PF_UNSPEC 0 +#define PF_LOCAL 1 +#define PF_UNIX PF_LOCAL +#define PF_FILE PF_LOCAL +#define PF_INET 2 +#define PF_AX25 3 +#define PF_IPX 4 +#define PF_APPLETALK 5 +#define PF_NETROM 6 +#define PF_BRIDGE 7 +#define PF_ATMPVC 8 +#define PF_X25 9 +#define PF_INET6 10 +#define PF_ROSE 11 +#define PF_DECnet 12 +#define PF_NETBEUI 13 +#define PF_SECURITY 14 +#define PF_KEY 15 +#define PF_NETLINK 16 +#define PF_ROUTE PF_NETLINK +#define PF_PACKET 17 +#define PF_ASH 18 +#define PF_ECONET 19 +#define PF_ATMSVC 20 +#define PF_SNA 22 +#define PF_IRDA 23 +#define PF_PPPOX 24 +#define PF_WANPIPE 25 +#define PF_BLUETOOTH 31 +#define PF_IUCV 32 +#define PF_RXRPC 33 +#define PF_ISDN 34 +#define PF_PHONET 35 +#define PF_IEEE802154 36 +#define PF_CAIF 37 +#define PF_ALG 38 +#define PF_MAX 39 + +#define AF_UNSPEC PF_UNSPEC +#define AF_LOCAL PF_LOCAL +#define AF_UNIX AF_LOCAL +#define AF_FILE AF_LOCAL +#define AF_INET PF_INET +#define AF_AX25 PF_AX25 +#define AF_IPX PF_IPX +#define AF_APPLETALK PF_APPLETALK +#define AF_NETROM PF_NETROM +#define AF_BRIDGE PF_BRIDGE +#define AF_ATMPVC PF_ATMPVC +#define AF_X25 PF_X25 +#define AF_INET6 PF_INET6 +#define AF_ROSE PF_ROSE +#define AF_DECnet PF_DECnet +#define AF_NETBEUI PF_NETBEUI +#define AF_SECURITY PF_SECURITY +#define AF_KEY PF_KEY +#define AF_NETLINK PF_NETLINK +#define AF_ROUTE AF_NETLINK +#define AF_PACKET PF_PACKET +#define AF_ASH PF_ASH +#define AF_ECONET PF_ECONET +#define AF_ATMSVC PF_ATMSVC +#define AF_SNA PF_SNA +#define AF_IRDA PF_IRDA +#define AF_PPPOX PF_PPPOX +#define AF_WANPIPE PF_WANPIPE +#define AF_BLUETOOTH PF_BLUETOOTH +#define AF_IUCV PF_IUCV +#define AF_RXRPC PF_RXRPC +#define AF_ISDN PF_ISDN +#define AF_PHONET PF_PHONET +#define AF_IEEE802154 PF_IEEE802154 +#define AF_CAIF PF_CAIF +#define AF_ALG PF_ALG +#define AF_MAX PF_MAX + +#define SO_DEBUG 1 +#define SO_REUSEADDR 2 +#define SO_TYPE 3 +#define SO_ERROR 4 +#define SO_DONTROUTE 5 +#define SO_BROADCAST 6 +#define SO_SNDBUF 7 +#define SO_RCVBUF 8 +#define SO_KEEPALIVE 9 +#define SO_OOBINLINE 10 +#define SO_NO_CHECK 11 +#define SO_PRIORITY 12 +#define SO_LINGER 13 +#define SO_BSDCOMPAT 14 +#define SO_REUSEPORT 15 +#define SO_PASSCRED 16 +#define SO_PEERCRED 17 +#define SO_RCVLOWAT 18 +#define SO_SNDLOWAT 19 +#define SO_RCVTIMEO 20 +#define SO_SNDTIMEO 21 + +#define SO_SECURITY_AUTHENTICATION 22 +#define SO_SECURITY_ENCRYPTION_TRANSPORT 23 +#define SO_SECURITY_ENCRYPTION_NETWORK 24 + +#define SO_BINDTODEVICE 25 + +#define SO_ATTACH_FILTER 26 +#define SO_DETACH_FILTER 27 + +#define SO_PEERNAME 28 +#define SO_TIMESTAMP 29 +#define SCM_TIMESTAMP SO_TIMESTAMP + +#define SO_ACCEPTCONN 30 + +#define SOL_SOCKET 1 + +/* ??? */ +#define SOL_RAW 255 +#define SOL_DECNET 261 +#define SOL_X25 262 +#define SOL_PACKET 263 +#define SOL_ATM 264 +#define SOL_AAL 265 +#define SOL_IRDA 266 + +#define SOMAXCONN 128 + +#define MSG_OOB 0x0001 +#define MSG_PEEK 0x0002 +#define MSG_DONTROUTE 0x0004 +#define MSG_CTRUNC 0x0008 +#define MSG_PROXY 0x0010 +#define MSG_TRUNC 0x0020 +#define MSG_DONTWAIT 0x0040 +#define MSG_EOR 0x0080 +#define MSG_WAITALL 0x0100 +#define MSG_FIN 0x0200 +#define MSD_SYN 0x0400 +#define MSG_CONFIRM 0x0800 +#define MSG_RST 0x1000 +#define MSG_ERRQUEUE 0x2000 +#define MSG_NOSIGNAL 0x4000 +#define MSG_MORE 0x8000 +#define MSG_WAITFORONE 0x10000 +#define MSG_CMSG_CLOEXEC 0x40000000 + +/* Internal use only!! to make CMSG_NXTHDR definition readable by mortals */ +#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) +#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) +#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) + +#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) +#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) ? (struct cmsghdr *)0 : \ + (__CMSG_NEXT(cmsg) + sizeof (struct cmsghdr) >= __MHDR_END(mhdr) ? (struct cmsghdr *)0 : \ + ((struct cmsghdr *)__CMSG_NEXT(cmsg)))) +#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) + +/* Are these valid? */ +#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) +#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) +#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) + +#define SCM_RIGHTS 0x01 +#define SCM_CREDENTIALS 0x02 diff --git a/05/musl-final/arch/i386/bits/stat.h b/05/musl-final/arch/i386/bits/stat.h new file mode 100644 index 0000000..bb9314a --- /dev/null +++ b/05/musl-final/arch/i386/bits/stat.h @@ -0,0 +1,22 @@ +/* copied from kernel definition, but with padding replaced + * by the corresponding correctly-sized userspace types. */ + +struct stat +{ + dev_t st_dev; + int __st_dev_padding; + long __st_ino_truncated; + mode_t st_mode; + nlink_t st_nlink; + uid_t st_uid; + gid_t st_gid; + dev_t st_rdev; + int __st_rdev_padding; + off_t st_size; + blksize_t st_blksize; + blkcnt_t st_blocks; + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; + ino_t st_ino; +}; diff --git a/05/musl-final/arch/i386/bits/statfs.h b/05/musl-final/arch/i386/bits/statfs.h new file mode 100644 index 0000000..9dda440 --- /dev/null +++ b/05/musl-final/arch/i386/bits/statfs.h @@ -0,0 +1,16 @@ +struct statvfs { + unsigned long f_type; + unsigned long f_bsize; + fsblkcnt_t f_blocks; + fsblkcnt_t f_bfree; + fsblkcnt_t f_bavail; + fsfilcnt_t f_files; + fsfilcnt_t f_ffree; + unsigned long f_fsid; + unsigned long __unused; + unsigned long f_namemax; + unsigned long f_frsize; + fsfilcnt_t f_favail; + unsigned long f_flag; + unsigned long __reserved[2]; +}; diff --git a/05/musl-final/arch/i386/bits/stdarg.h b/05/musl-final/arch/i386/bits/stdarg.h new file mode 100644 index 0000000..6b1830d --- /dev/null +++ b/05/musl-final/arch/i386/bits/stdarg.h @@ -0,0 +1,9 @@ +#define __VA_ALIGNED_SIZE(x) ((sizeof(x) + sizeof(int) - 1) & ~(sizeof(int) - 1)) + +#define va_start(ap, last) ((ap) = (void *)(((char *)&(last)) + __VA_ALIGNED_SIZE(last))) +#define va_end(ap) ((void)0) +#define va_copy(dest, src) ((dest) = (src)) + +#define va_arg(ap, type) \ + ( ((ap) = (va_list)((char *)(ap) + __VA_ALIGNED_SIZE(type))), \ + *(type *)(void *)((char *)(ap) - __VA_ALIGNED_SIZE(type)) ) diff --git a/05/musl-final/arch/i386/bits/stdint.h b/05/musl-final/arch/i386/bits/stdint.h new file mode 100644 index 0000000..8e21a8c --- /dev/null +++ b/05/musl-final/arch/i386/bits/stdint.h @@ -0,0 +1,23 @@ +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST64_MIN INT64_MIN + +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MAX INT32_MAX +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MAX INT64_MAX + +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT32_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +#define INTPTR_MIN INT32_MIN +#define INTPTR_MAX INT32_MAX +#define UINTPTR_MAX UINT32_MAX +#define PTRDIFF_MIN INT32_MIN +#define PTRDIFF_MAX INT32_MAX +#define SIG_ATOMIC_MIN INT32_MIN +#define SIG_ATOMIC_MAX INT32_MAX +#define SIZE_MAX UINT32_MAX diff --git a/05/musl-final/arch/i386/bits/sysmacros.h b/05/musl-final/arch/i386/bits/sysmacros.h new file mode 100644 index 0000000..da29022 --- /dev/null +++ b/05/musl-final/arch/i386/bits/sysmacros.h @@ -0,0 +1,7 @@ +#define major(x) (((x) >> 8) & 0xff) +#define minor(x) ((x) & 0xff) +#define makedev(x,y) (((x)<<8)|((y)&0xff)) + +//#define makedev(x,y) \ +// ((x)*0x100000001ULL)&(0xfffffffffff0) +// ((y)*0x1001 & 0xffff0ff) diff --git a/05/musl-final/arch/i386/bits/tcp.h b/05/musl-final/arch/i386/bits/tcp.h new file mode 100644 index 0000000..923231b --- /dev/null +++ b/05/musl-final/arch/i386/bits/tcp.h @@ -0,0 +1 @@ +#define TCP_NODELAY 1 diff --git a/05/musl-final/arch/i386/bits/termios.h b/05/musl-final/arch/i386/bits/termios.h new file mode 100644 index 0000000..316baeb --- /dev/null +++ b/05/musl-final/arch/i386/bits/termios.h @@ -0,0 +1,158 @@ +struct termios +{ + tcflag_t c_iflag; + tcflag_t c_oflag; + tcflag_t c_cflag; + tcflag_t c_lflag; + cc_t c_line; + cc_t c_cc[NCCS]; + speed_t __c_ispeed; + speed_t __c_ospeed; +}; + +#define VINTR 0 +#define VQUIT 1 +#define VERASE 2 +#define VKILL 3 +#define VEOF 4 +#define VTIME 5 +#define VMIN 6 +#define VSWTC 7 +#define VSTART 8 +#define VSTOP 9 +#define VSUSP 10 +#define VEOL 11 +#define VREPRINT 12 +#define VDISCARD 13 +#define VWERASE 14 +#define VLNEXT 15 +#define VEOL2 16 + +#define IGNBRK 0000001 +#define BRKINT 0000002 +#define IGNPAR 0000004 +#define PARMRK 0000010 +#define INPCK 0000020 +#define ISTRIP 0000040 +#define INLCR 0000100 +#define IGNCR 0000200 +#define ICRNL 0000400 +#define IUCLC 0001000 +#define IXON 0002000 +#define IXANY 0004000 +#define IXOFF 0010000 +#define IMAXBEL 0020000 + +#define OPOST 0000001 +#define OLCUC 0000002 +#define ONLCR 0000004 +#define OCRNL 0000010 +#define ONOCR 0000020 +#define ONLRET 0000040 +#define OFILL 0000100 +#define OFDEL 0000200 +#define NLDLY 0000400 +#define NL0 0000000 +#define NL1 0000400 +#define CRDLY 0003000 +#define CR0 0000000 +#define CR1 0001000 +#define CR2 0002000 +#define CR3 0003000 +#define TABDLY 0014000 +#define TAB0 0000000 +#define TAB1 0004000 +#define TAB2 0010000 +#define TAB3 0014000 +#define BSDLY 0020000 +#define BS0 0000000 +#define BS1 0020000 +#define FFDLY 0100000 +#define FF0 0000000 +#define FF1 0100000 + +#define VTDLY 0040000 +#define VT0 0000000 +#define VT1 0040000 + +/* ?? */ +#define XTABS 0014000 + +#define B0 0000000 +#define B50 0000001 +#define B75 0000002 +#define B110 0000003 +#define B134 0000004 +#define B150 0000005 +#define B200 0000006 +#define B300 0000007 +#define B600 0000010 +#define B1200 0000011 +#define B1800 0000012 +#define B2400 0000013 +#define B4800 0000014 +#define B9600 0000015 +#define B19200 0000016 +#define B38400 0000017 + +#define B57600 0010001 +#define B115200 0010002 +#define B230400 0010003 +#define B460800 0010004 +#define B500000 0010005 +#define B576000 0010006 +#define B921600 0010007 +#define B1000000 0010010 +#define B1152000 0010011 +#define B1500000 0010012 +#define B2000000 0010013 +#define B2500000 0010014 +#define B3000000 0010015 +#define B3500000 0010016 +#define B4000000 0010017 + +#define CBAUD 0010017 + +#define CSIZE 0000060 +#define CS5 0000000 +#define CS6 0000020 +#define CS7 0000040 +#define CS8 0000060 +#define CSTOPB 0000100 +#define CREAD 0000200 +#define PARENB 0000400 +#define PARODD 0001000 +#define HUPCL 0002000 +#define CLOCAL 0004000 + +#define CRTSCTS 020000000000 + +#define ISIG 0000001 +#define ICANON 0000002 +#define ECHO 0000010 +#define ECHOE 0000020 +#define ECHOK 0000040 +#define ECHONL 0000100 +#define NOFLSH 0000200 +#define TOSTOP 0000400 +#define IEXTEN 0100000 + +/* Extensions? */ +#define ECHOCTL 0001000 +#define ECHOPRT 0002000 +#define ECHOKE 0004000 +#define FLUSHO 0010000 +#define PENDIN 0040000 + +#define TCOOFF 0 +#define TCOON 1 +#define TCIOFF 2 +#define TCION 3 + +#define TCIFLUSH 0 +#define TCOFLUSH 1 +#define TCIOFLUSH 2 + +#define TCSANOW 0 +#define TCSADRAIN 1 +#define TCSAFLUSH 2 diff --git a/05/musl-final/arch/i386/bits/user.h b/05/musl-final/arch/i386/bits/user.h new file mode 100644 index 0000000..13ff027 --- /dev/null +++ b/05/musl-final/arch/i386/bits/user.h @@ -0,0 +1,77 @@ +#undef __WORDSIZE +#define __WORDSIZE 32 + +struct user_fpregs_struct +{ + long int cwd; + long int swd; + long int twd; + long int fip; + long int fcs; + long int foo; + long int fos; + long int st_space[20]; +}; + +struct user_fpxregs_struct +{ + unsigned short int cwd; + unsigned short int swd; + unsigned short int twd; + unsigned short int fop; + long int fip; + long int fcs; + long int foo; + long int fos; + long int mxcsr; + long int reserved; + long int st_space[32]; + long int xmm_space[32]; + long int padding[56]; +}; + +struct user_regs_struct +{ + long int ebx; + long int ecx; + long int edx; + long int esi; + long int edi; + long int ebp; + long int eax; + long int xds; + long int xes; + long int xfs; + long int xgs; + long int orig_eax; + long int eip; + long int xcs; + long int eflags; + long int esp; + long int xss; +}; + +struct user +{ + struct user_regs_struct regs; + int u_fpvalid; + struct user_fpregs_struct i387; + unsigned long int u_tsize; + unsigned long int u_dsize; + unsigned long int u_ssize; + unsigned long start_code; + unsigned long start_stack; + long int signal; + int reserved; + struct user_regs_struct *u_ar0; + struct user_fpregs_struct *u_fpstate; + unsigned long int magic; + char u_comm[32]; + int u_debugreg[8]; +}; + +#define PAGE_MASK (~(PAGE_SIZE-1)) +#define NBPG PAGE_SIZE +#define UPAGES 1 +#define HOST_TEXT_START_ADDR (u.start_code) +#define HOST_STACK_END_ADDR (u.start_stack + u.u_ssize * NBPG) diff --git a/05/musl-final/arch/i386/bits/wait.h b/05/musl-final/arch/i386/bits/wait.h new file mode 100644 index 0000000..ae3f101 --- /dev/null +++ b/05/musl-final/arch/i386/bits/wait.h @@ -0,0 +1,13 @@ +#define WNOHANG 1 +#define WUNTRACED 2 + +#define WSTOPPED 2 +#define WEXITED 4 +#define WCONTINUED 8 +#define WNOWAIT 0x1000000 + +#if defined(__XOPEN_SOURCE) || defined(__GNU_SOURCE) +#define P_ALL 0 +#define P_PID 1 +#define P_PGID 2 +#endif diff --git a/05/musl-final/arch/i386/bits/wexitstatus.h b/05/musl-final/arch/i386/bits/wexitstatus.h new file mode 100644 index 0000000..34a8023 --- /dev/null +++ b/05/musl-final/arch/i386/bits/wexitstatus.h @@ -0,0 +1,9 @@ +#ifndef WEXITSTATUS +#define WEXITSTATUS(s) (((s) & 0xff00) >> 8) +#define WTERMSIG(s) ((s) & 0x7f) +#define WSTOPSIG(s) WEXITSTATUS(s) +#define WCOREDUMP(s) ((s) & 0x80) +#define WIFEXITED(s) (!WTERMSIG(s)) +#define WIFSTOPPED(s) (((s) & 0xff) == 0x7f) +#define WIFSIGNALED(s) (!WIFSTOPPED(s) && !WIFEXITED(s)) +#endif diff --git a/05/musl-final/arch/i386/pthread_arch.h b/05/musl-final/arch/i386/pthread_arch.h new file mode 100644 index 0000000..155b484 --- /dev/null +++ b/05/musl-final/arch/i386/pthread_arch.h @@ -0,0 +1,6 @@ +static inline struct pthread *__pthread_self() +{ + struct pthread *self; + __asm__ ("movl %%gs:0,%0" : "=r" (self) ); + return self; +} diff --git a/05/musl-final/arch/i386/syscall.h b/05/musl-final/arch/i386/syscall.h new file mode 100644 index 0000000..ee871a9 --- /dev/null +++ b/05/musl-final/arch/i386/syscall.h @@ -0,0 +1,541 @@ +#ifndef _SYSCALL_H +#define _SYSCALL_H + +#define SYSCALL_LL(x) \ +((union { long long ll; long l[2]; }){ .ll = x }).l[0], \ +((union { long long ll; long l[2]; }){ .ll = x }).l[1] + +#define SYSCALL_SIGSET_SIZE 8 + +#if defined(SYSCALL_NORETURN) +static inline long __syscall_ret(unsigned long r) +{ + for(;;); + return 0; +} +#elif defined(SYSCALL_RETURN_ERRNO) +static inline long __syscall_ret(unsigned long r) +{ + return -r; +} +#else +extern long __syscall_ret(unsigned long); +#endif + +#define SYSCALL0 "int $128" + +#ifdef __PIC__ +#define SYSCALL "xchgl %%ebx,%2\n\t" SYSCALL0 "\n\txchgl %%ebx,%2" +#define EBX "m" +#else +#define SYSCALL SYSCALL0 +#define EBX "b" +#endif + +static inline long syscall0(long n) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL0 : "=a"(ret) : "a"(n) : "memory"); + return __syscall_ret(ret); +} + +static inline long syscall1(long n, long a1) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1) : "memory"); + return __syscall_ret(ret); +} + +static inline long syscall2(long n, long a1, long a2) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2) : "memory"); + return __syscall_ret(ret); +} + +static inline long syscall3(long n, long a1, long a2, long a3) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2), "d"(a3) : "memory"); + return __syscall_ret(ret); +} + +static inline long syscall4(long n, long a1, long a2, long a3, long a4) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4) : "memory"); + return __syscall_ret(ret); +} + +static inline long syscall5(long n, long a1, long a2, long a3, long a4, long a5) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5) : "memory"); + return __syscall_ret(ret); +} + +#ifdef __PIC__ +/* note: it's probably only safe to use this when a6 is on the stack */ +static inline long syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6) +{ + unsigned long ret; + __asm__ __volatile__ ("xchgl %%ebx,%2 ; pushl %1 ; pushl %%ebp ; movl %%eax,%%ebp ; movl 4(%%esp),%%eax ; int $128 ; popl %%ebp ; popl %%ecx ; xchgl %%ebx,%2" + : "=a"(ret) : "g"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5), "a"(a6) : "memory"); + return __syscall_ret(ret); +} +#else +static inline long syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6) +{ + unsigned long ret; + __asm__ __volatile__ ("pushl %%ebp ; mov %1, %%ebp ; xchg %%ebp, %7 ; int $128 ; popl %%ebp" + : "=a"(ret) : "g"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5), "a"(a6) : "memory"); + return __syscall_ret(ret); +} +#endif + +#define __NR_restart_syscall 0 +#define __NR_exit 1 +#define __NR_fork 2 +#define __NR_read 3 +#define __NR_write 4 +#define __NR_open 5 +#define __NR_close 6 +#define __NR_waitpid 7 +#define __NR_creat 8 +#define __NR_link 9 +#define __NR_unlink 10 +#define __NR_execve 11 +#define __NR_chdir 12 +#define __NR_time 13 +#define __NR_mknod 14 +#define __NR_chmod 15 +#define __NR_lchown 16 +#define __NR_break 17 +#define __NR_oldstat 18 +#define __NR_lseek 19 +#define __NR_getpid 20 +#define __NR_mount 21 +#define __NR_umount 22 +#define __NR_setuid 23 +#define __NR_getuid 24 +#define __NR_stime 25 +#define __NR_ptrace 26 +#define __NR_alarm 27 +#define __NR_oldfstat 28 +#define __NR_pause 29 +#define __NR_utime 30 +#define __NR_stty 31 +#define __NR_gtty 32 +#define __NR_access 33 +#define __NR_nice 34 +#define __NR_ftime 35 +#define __NR_sync 36 +#define __NR_kill 37 +#define __NR_rename 38 +#define __NR_mkdir 39 +#define __NR_rmdir 40 +#define __NR_dup 41 +#define __NR_pipe 42 +#define __NR_times 43 +#define __NR_prof 44 +#define __NR_brk 45 +#define __NR_setgid 46 +#define __NR_getgid 47 +#define __NR_signal 48 +#define __NR_geteuid 49 +#define __NR_getegid 50 +#define __NR_acct 51 +#define __NR_umount2 52 +#define __NR_lock 53 +#define __NR_ioctl 54 +#define __NR_fcntl 55 +#define __NR_mpx 56 +#define __NR_setpgid 57 +#define __NR_ulimit 58 +#define __NR_oldolduname 59 +#define __NR_umask 60 +#define __NR_chroot 61 +#define __NR_ustat 62 +#define __NR_dup2 63 +#define __NR_getppid 64 +#define __NR_getpgrp 65 +#define __NR_setsid 66 +#define __NR_sigaction 67 +#define __NR_sgetmask 68 +#define __NR_ssetmask 69 +#define __NR_setreuid 70 +#define __NR_setregid 71 +#define __NR_sigsuspend 72 +#define __NR_sigpending 73 +#define __NR_sethostname 74 +#define __NR_setrlimit 75 +#define __NR_getrlimit 76 /* Back compatible 2Gig limited rlimit */ +#define __NR_getrusage 77 +#define __NR_gettimeofday 78 +#define __NR_settimeofday 79 +#define __NR_getgroups 80 +#define __NR_setgroups 81 +#define __NR_select 82 +#define __NR_symlink 83 +#define __NR_oldlstat 84 +#define __NR_readlink 85 +#define __NR_uselib 86 +#define __NR_swapon 87 +#define __NR_reboot 88 +#define __NR_readdir 89 +#define __NR_mmap 90 +#define __NR_munmap 91 +#define __NR_truncate 92 +#define __NR_ftruncate 93 +#define __NR_fchmod 94 +#define __NR_fchown 95 +#define __NR_getpriority 96 +#define __NR_setpriority 97 +#define __NR_profil 98 +#define __NR_statfs 99 +#define __NR_fstatfs 100 +#define __NR_ioperm 101 +#define __NR_socketcall 102 +#define __NR_syslog 103 +#define __NR_setitimer 104 +#define __NR_getitimer 105 +#define __NR_stat 106 +#define __NR_lstat 107 +#define __NR_fstat 108 +#define __NR_olduname 109 +#define __NR_iopl 110 +#define __NR_vhangup 111 +#define __NR_idle 112 +#define __NR_vm86old 113 +#define __NR_wait4 114 +#define __NR_swapoff 115 +#define __NR_sysinfo 116 +#define __NR_ipc 117 +#define __NR_fsync 118 +#define __NR_sigreturn 119 +#define __NR_clone 120 +#define __NR_setdomainname 121 +#define __NR_uname 122 +#define __NR_modify_ldt 123 +#define __NR_adjtimex 124 +#define __NR_mprotect 125 +#define __NR_sigprocmask 126 +#define __NR_create_module 127 +#define __NR_init_module 128 +#define __NR_delete_module 129 +#define __NR_get_kernel_syms 130 +#define __NR_quotactl 131 +#define __NR_getpgid 132 +#define __NR_fchdir 133 +#define __NR_bdflush 134 +#define __NR_sysfs 135 +#define __NR_personality 136 +#define __NR_afs_syscall 137 +#define __NR_setfsuid 138 +#define __NR_setfsgid 139 +#define __NR__llseek 140 +#define __NR_getdents 141 +#define __NR__newselect 142 +#define __NR_flock 143 +#define __NR_msync 144 +#define __NR_readv 145 +#define __NR_writev 146 +#define __NR_getsid 147 +#define __NR_fdatasync 148 +#define __NR__sysctl 149 +#define __NR_mlock 150 +#define __NR_munlock 151 +#define __NR_mlockall 152 +#define __NR_munlockall 153 +#define __NR_sched_setparam 154 +#define __NR_sched_getparam 155 +#define __NR_sched_setscheduler 156 +#define __NR_sched_getscheduler 157 +#define __NR_sched_yield 158 +#define __NR_sched_get_priority_max 159 +#define __NR_sched_get_priority_min 160 +#define __NR_sched_rr_get_interval 161 +#define __NR_nanosleep 162 +#define __NR_mremap 163 +#define __NR_setresuid 164 +#define __NR_getresuid 165 +#define __NR_vm86 166 +#define __NR_query_module 167 +#define __NR_poll 168 +#define __NR_nfsservctl 169 +#define __NR_setresgid 170 +#define __NR_getresgid 171 +#define __NR_prctl 172 +#define __NR_rt_sigreturn 173 +#define __NR_rt_sigaction 174 +#define __NR_rt_sigprocmask 175 +#define __NR_rt_sigpending 176 +#define __NR_rt_sigtimedwait 177 +#define __NR_rt_sigqueueinfo 178 +#define __NR_rt_sigsuspend 179 +#define __NR_pread64 180 +#define __NR_pwrite64 181 +#define __NR_chown 182 +#define __NR_getcwd 183 +#define __NR_capget 184 +#define __NR_capset 185 +#define __NR_sigaltstack 186 +#define __NR_sendfile 187 +#define __NR_getpmsg 188 +#define __NR_putpmsg 189 +#define __NR_vfork 190 +#define __NR_ugetrlimit 191 +#define __NR_mmap2 192 +#define __NR_truncate64 193 +#define __NR_ftruncate64 194 +#define __NR_stat64 195 +#define __NR_lstat64 196 +#define __NR_fstat64 197 +#define __NR_lchown32 198 +#define __NR_getuid32 199 +#define __NR_getgid32 200 +#define __NR_geteuid32 201 +#define __NR_getegid32 202 +#define __NR_setreuid32 203 +#define __NR_setregid32 204 +#define __NR_getgroups32 205 +#define __NR_setgroups32 206 +#define __NR_fchown32 207 +#define __NR_setresuid32 208 +#define __NR_getresuid32 209 +#define __NR_setresgid32 210 +#define __NR_getresgid32 211 +#define __NR_chown32 212 +#define __NR_setuid32 213 +#define __NR_setgid32 214 +#define __NR_setfsuid32 215 +#define __NR_setfsgid32 216 +#define __NR_pivot_root 217 +#define __NR_mincore 218 +#define __NR_madvise 219 +#define __NR_madvise1 219 +#define __NR_getdents64 220 +#define __NR_fcntl64 221 +/* 223 is unused */ +#define __NR_gettid 224 +#define __NR_readahead 225 +#define __NR_setxattr 226 +#define __NR_lsetxattr 227 +#define __NR_fsetxattr 228 +#define __NR_getxattr 229 +#define __NR_lgetxattr 230 +#define __NR_fgetxattr 231 +#define __NR_listxattr 232 +#define __NR_llistxattr 233 +#define __NR_flistxattr 234 +#define __NR_removexattr 235 +#define __NR_lremovexattr 236 +#define __NR_fremovexattr 237 +#define __NR_tkill 238 +#define __NR_sendfile64 239 +#define __NR_futex 240 +#define __NR_sched_setaffinity 241 +#define __NR_sched_getaffinity 242 +#define __NR_set_thread_area 243 +#define __NR_get_thread_area 244 +#define __NR_io_setup 245 +#define __NR_io_destroy 246 +#define __NR_io_getevents 247 +#define __NR_io_submit 248 +#define __NR_io_cancel 249 +#define __NR_fadvise64 250 +/* 251 is available for reuse (was briefly sys_set_zone_reclaim) */ +#define __NR_exit_group 252 +#define __NR_lookup_dcookie 253 +#define __NR_epoll_create 254 +#define __NR_epoll_ctl 255 +#define __NR_epoll_wait 256 +#define __NR_remap_file_pages 257 +#define __NR_set_tid_address 258 +#define __NR_timer_create 259 +#define __NR_timer_settime (__NR_timer_create+1) +#define __NR_timer_gettime (__NR_timer_create+2) +#define __NR_timer_getoverrun (__NR_timer_create+3) +#define __NR_timer_delete (__NR_timer_create+4) +#define __NR_clock_settime (__NR_timer_create+5) +#define __NR_clock_gettime (__NR_timer_create+6) +#define __NR_clock_getres (__NR_timer_create+7) +#define __NR_clock_nanosleep (__NR_timer_create+8) +#define __NR_statfs64 268 +#define __NR_fstatfs64 269 +#define __NR_tgkill 270 +#define __NR_utimes 271 +#define __NR_fadvise64_64 272 +#define __NR_vserver 273 +#define __NR_mbind 274 +#define __NR_get_mempolicy 275 +#define __NR_set_mempolicy 276 +#define __NR_mq_open 277 +#define __NR_mq_unlink (__NR_mq_open+1) +#define __NR_mq_timedsend (__NR_mq_open+2) +#define __NR_mq_timedreceive (__NR_mq_open+3) +#define __NR_mq_notify (__NR_mq_open+4) +#define __NR_mq_getsetattr (__NR_mq_open+5) +#define __NR_kexec_load 283 +#define __NR_waitid 284 +/* #define __NR_sys_setaltroot 285 */ +#define __NR_add_key 286 +#define __NR_request_key 287 +#define __NR_keyctl 288 +#define __NR_ioprio_set 289 +#define __NR_ioprio_get 290 +#define __NR_inotify_init 291 +#define __NR_inotify_add_watch 292 +#define __NR_inotify_rm_watch 293 +#define __NR_migrate_pages 294 +#define __NR_openat 295 +#define __NR_mkdirat 296 +#define __NR_mknodat 297 +#define __NR_fchownat 298 +#define __NR_futimesat 299 +#define __NR_fstatat64 300 +#define __NR_unlinkat 301 +#define __NR_renameat 302 +#define __NR_linkat 303 +#define __NR_symlinkat 304 +#define __NR_readlinkat 305 +#define __NR_fchmodat 306 +#define __NR_faccessat 307 +#define __NR_pselect6 308 +#define __NR_ppoll 309 +#define __NR_unshare 310 +#define __NR_set_robust_list 311 +#define __NR_get_robust_list 312 +#define __NR_splice 313 +#define __NR_sync_file_range 314 +#define __NR_tee 315 +#define __NR_vmsplice 316 +#define __NR_move_pages 317 +#define __NR_getcpu 318 +#define __NR_epoll_pwait 319 +#define __NR_utimensat 320 +#define __NR_signalfd 321 +#define __NR_timerfd_create 322 +#define __NR_eventfd 323 +#define __NR_fallocate 324 +#define __NR_timerfd_settime 325 +#define __NR_timerfd_gettime 326 +#define __NR_signalfd4 327 +#define __NR_eventfd2 328 +#define __NR_epoll_create1 329 +#define __NR_dup3 330 +#define __NR_pipe2 331 +#define __NR_inotify_init1 332 +#define __NR_preadv 333 +#define __NR_pwritev 334 + +/* fixup legacy 16-bit junk */ +#undef __NR_lchown +#undef __NR_getuid +#undef __NR_getgid +#undef __NR_geteuid +#undef __NR_getegid +#undef __NR_setreuid +#undef __NR_setregid +#undef __NR_getgroups +#undef __NR_setgroups +#undef __NR_fchown +#undef __NR_setresuid +#undef __NR_getresuid +#undef __NR_setresgid +#undef __NR_getresgid +#undef __NR_chown +#undef __NR_setuid +#undef __NR_setgid +#undef __NR_setfsuid +#undef __NR_setfsgid +#define __NR_lchown __NR_lchown32 +#define __NR_getuid __NR_getuid32 +#define __NR_getgid __NR_getgid32 +#define __NR_geteuid __NR_geteuid32 +#define __NR_getegid __NR_getegid32 +#define __NR_setreuid __NR_setreuid32 +#define __NR_setregid __NR_setregid32 +#define __NR_getgroups __NR_getgroups32 +#define __NR_setgroups __NR_setgroups32 +#define __NR_fchown __NR_fchown32 +#define __NR_setresuid __NR_setresuid32 +#define __NR_getresuid __NR_getresuid32 +#define __NR_setresgid __NR_setresgid32 +#define __NR_getresgid __NR_getresgid32 +#define __NR_chown __NR_chown32 +#define __NR_setuid __NR_setuid32 +#define __NR_setgid __NR_setgid32 +#define __NR_setfsuid __NR_setfsuid32 +#define __NR_setfsgid __NR_setfsgid32 + + +/* fixup legacy 32-bit-vs-lfs64 junk */ +#undef __NR_getdents +#undef __NR_ftruncate +#undef __NR_truncate +#undef __NR_stat +#undef __NR_fstat +#undef __NR_lstat +#undef __NR_statfs +#undef __NR_fstatfs +#define __NR_getdents __NR_getdents64 +#define __NR_ftruncate __NR_ftruncate64 +#define __NR_truncate __NR_truncate64 +#define __NR_stat __NR_stat64 +#define __NR_fstat __NR_fstat64 +#define __NR_lstat __NR_lstat64 +#define __NR_statfs __NR_statfs64 +#define __NR_fstatfs __NR_fstatfs64 +#define __NR_fstatat __NR_fstatat64 +#define __NR_pread __NR_pread64 +#define __NR_pwrite __NR_pwrite64 + +#undef __NR_getrlimit +#define __NR_getrlimit __NR_ugetrlimit + +#undef __NR_select +#define __NR_select __NR__newselect + + +#define __SC_socket 1 +#define __SC_bind 2 +#define __SC_connect 3 +#define __SC_listen 4 +#define __SC_accept 5 +#define __SC_getsockname 6 +#define __SC_getpeername 7 +#define __SC_socketpair 8 +#define __SC_send 9 +#define __SC_recv 10 +#define __SC_sendto 11 +#define __SC_recvfrom 12 +#define __SC_shutdown 13 +#define __SC_setsockopt 14 +#define __SC_getsockopt 15 +#define __SC_sendmsg 16 +#define __SC_recvmsg 17 + + +#define socketcall(nm, a, b, c, d, e, f) syscall2(__NR_socketcall, __SC_##nm, \ + (long)(long [6]){ (long)a, (long)b, (long)c, (long)d, (long)e, (long)f }) + + +#undef O_LARGEFILE +#define O_LARGEFILE 0100000 + +/* the following are needed for iso c functions to use */ +#define __syscall_open(filename, flags, mode) syscall3(__NR_open, (long)(filename), (flags)|O_LARGEFILE, (mode)) +#define __syscall_read(fd, buf, len) syscall3(__NR_read, (fd), (long)(buf), (len)) +#define __syscall_write(fd, buf, len) syscall3(__NR_write, (fd), (long)(buf), (len)) +#define __syscall_close(fd) syscall1(__NR_close, (fd)) +#define __syscall_fcntl(fd, cmd, arg) syscall3(__NR_fcntl64, (fd), (cmd), (long)(arg)) +#define __syscall_dup2(old, new) syscall2(__NR_dup2, (old), (new)) +#define __syscall_unlink(path) syscall1(__NR_unlink, (long)(path)) +#define __syscall_getpid() syscall0(__NR_getpid) +#define __syscall_kill(pid,sig) syscall2(__NR_kill, (pid), (sig)) +#define __syscall_sigaction(sig,new,old) syscall4(__NR_rt_sigaction, (sig), (long)(new), (long)(old), SYSCALL_SIGSET_SIZE) +#define __syscall_ioctl(fd,ioc,arg) syscall3(__NR_ioctl, (fd), (ioc), (long)(arg)) +#define __syscall_exit(code) syscall1(__NR_exit, code) + +#endif diff --git a/05/musl-final/arch/x86_64/atomic.h b/05/musl-final/arch/x86_64/atomic.h new file mode 100644 index 0000000..7a665c1 --- /dev/null +++ b/05/musl-final/arch/x86_64/atomic.h @@ -0,0 +1,109 @@ +#ifndef _INTERNAA_ATOMIC_H +#define _INTERNAA_ATOMIC_H + +#include + +static inline int a_ctz_64(uint64_t x) +{ + long r; + __asm__( "bsf %1,%0" : "=r"(r) : "r"(x) ); + return r; +} + + +static inline void a_and_64(volatile uint64_t *p, uint64_t v) +{ + __asm__( "lock ; andq %1, %0" + : "=m"(*(long *)p) : "r"(v) : "memory" ); +} + +static inline void a_or_64(volatile uint64_t *p, uint64_t v) +{ + __asm__( "lock ; orq %1, %0" + : "=m"(*(long *)p) : "r"(v) : "memory" ); +} + +static inline void a_store_l(volatile void *p, long x) +{ + __asm__( "movq %1, %0" : "=m"(*(long *)p) : "r"(x) : "memory" ); +} + +static inline void a_or_l(volatile void *p, long v) +{ + __asm__( "lock ; orq %1, %0" + : "=m"(*(long *)p) : "r"(v) : "memory" ); +} + +static inline void *a_cas_p(volatile void *p, void *t, void *s) +{ + __asm__( "lock ; cmpxchg %3, %1" + : "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) : "memory" ); + return t; +} + +static inline long a_cas_l(volatile void *p, long t, long s) +{ + __asm__( "lock ; cmpxchg %3, %1" + : "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) : "memory" ); + return t; +} + +static inline void *a_swap_p(void *volatile *x, void *v) +{ + __asm__( "xchg %0, %1" : "=r"(v), "=m"(*(void **)x) : "0"(v) : "memory" ); + return v; +} +static inline long a_swap_l(volatile void *x, long v) +{ + __asm__( "xchg %0, %1" : "=r"(v), "=m"(*(long *)x) : "0"(v) : "memory" ); + return v; +} + +static inline void a_or(volatile void *p, int v) +{ + __asm__( "lock ; orl %1, %0" + : "=m"(*(int *)p) : "r"(v) : "memory" ); +} + +static inline void a_and(volatile void *p, int v) +{ + __asm__( "lock ; andl %1, %0" + : "=m"(*(int *)p) : "r"(v) : "memory" ); +} + +static inline int a_swap(volatile int *x, int v) +{ + __asm__( "xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" ); + return v; +} + +#define a_xchg a_swap + +static inline int a_fetch_add(volatile int *x, int v) +{ + __asm__( "lock ; xadd %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory" ); + return v; +} + +static inline void a_inc(volatile int *x) +{ + __asm__( "lock ; incl %0" : "=m"(*x) : "m"(*x) : "memory" ); +} + +static inline void a_dec(volatile int *x) +{ + __asm__( "lock ; decl %0" : "=m"(*x) : "m"(*x) : "memory" ); +} + +static inline void a_store(volatile int *p, int x) +{ + __asm__( "movl %1, %0" : "=m"(*p) : "r"(x) : "memory" ); +} + +static inline void a_spin() +{ + __asm__ __volatile__( "pause" : : : "memory" ); +} + + +#endif diff --git a/05/musl-final/arch/x86_64/bits/alltypes.h b/05/musl-final/arch/x86_64/bits/alltypes.h new file mode 100644 index 0000000..f07dc5c --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/alltypes.h @@ -0,0 +1,468 @@ + +#include + +#if defined(__NEED_size_t) && !defined(__DEFINED_size_t) +typedef unsigned long size_t; +#define __DEFINED_size_t +#endif + +#if defined(__NEED_ssize_t) && !defined(__DEFINED_ssize_t) +typedef long ssize_t; +#define __DEFINED_ssize_t +#endif + +#if defined(__NEED_ptrdiff_t) && !defined(__DEFINED_ptrdiff_t) +typedef long ptrdiff_t; +#define __DEFINED_ptrdiff_t +#endif + +#if 0 +#if defined(__NEED_va_list) && !defined(__DEFINED_va_list) +typedef __builtin_va_list va_list; +#define __DEFINED_va_list +#endif + +#endif + +#if defined(__NEED_wchar_t) && !defined(__DEFINED_wchar_t) +typedef int wchar_t; +#define __DEFINED_wchar_t +#endif + +#if defined(__NEED_wint_t) && !defined(__DEFINED_wint_t) +typedef int wint_t; +#define __DEFINED_wint_t +#endif + +#if defined(__NEED_wctrans_t) && !defined(__DEFINED_wctrans_t) +typedef int wctrans_t; +#define __DEFINED_wctrans_t +#endif + +#if defined(__NEED_wctype_t) && !defined(__DEFINED_wctype_t) +typedef int wctype_t; +#define __DEFINED_wctype_t +#endif + + +#if defined(__NEED_int8_t) && !defined(__DEFINED_int8_t) +typedef char int8_t; +#define __DEFINED_int8_t +#endif + +#if defined(__NEED_int16_t) && !defined(__DEFINED_int16_t) +typedef short int16_t; +#define __DEFINED_int16_t +#endif + +#if defined(__NEED_int32_t) && !defined(__DEFINED_int32_t) +typedef int int32_t; +#define __DEFINED_int32_t +#endif + +#if defined(__NEED_int64_t) && !defined(__DEFINED_int64_t) +typedef long int64_t; +#define __DEFINED_int64_t +#endif + + +#if defined(__NEED_uint8_t) && !defined(__DEFINED_uint8_t) +typedef unsigned char uint8_t; +#define __DEFINED_uint8_t +#endif + +#if defined(__NEED_uint16_t) && !defined(__DEFINED_uint16_t) +typedef unsigned short uint16_t; +#define __DEFINED_uint16_t +#endif + +#if defined(__NEED_uint32_t) && !defined(__DEFINED_uint32_t) +typedef unsigned int uint32_t; +#define __DEFINED_uint32_t +#endif + +#if defined(__NEED_uint64_t) && !defined(__DEFINED_uint64_t) +typedef unsigned long uint64_t; +#define __DEFINED_uint64_t +#endif + + +#if defined(__NEED___uint8_t) && !defined(__DEFINED___uint8_t) +typedef unsigned char __uint8_t; +#define __DEFINED___uint8_t +#endif + +#if defined(__NEED___uint16_t) && !defined(__DEFINED___uint16_t) +typedef unsigned short __uint16_t; +#define __DEFINED___uint16_t +#endif + +#if defined(__NEED___uint32_t) && !defined(__DEFINED___uint32_t) +typedef unsigned int __uint32_t; +#define __DEFINED___uint32_t +#endif + +#if defined(__NEED___uint64_t) && !defined(__DEFINED___uint64_t) +typedef unsigned long __uint64_t; +#define __DEFINED___uint64_t +#endif + + +#if defined(__NEED_int_least8_t) && !defined(__DEFINED_int_least8_t) +typedef int8_t int_least8_t; +#define __DEFINED_int_least8_t +#endif + +#if defined(__NEED_int_least16_t) && !defined(__DEFINED_int_least16_t) +typedef int16_t int_least16_t; +#define __DEFINED_int_least16_t +#endif + +#if defined(__NEED_int_least32_t) && !defined(__DEFINED_int_least32_t) +typedef int32_t int_least32_t; +#define __DEFINED_int_least32_t +#endif + +#if defined(__NEED_int_least64_t) && !defined(__DEFINED_int_least64_t) +typedef int64_t int_least64_t; +#define __DEFINED_int_least64_t +#endif + + +#if defined(__NEED_uint_least8_t) && !defined(__DEFINED_uint_least8_t) +typedef uint8_t uint_least8_t; +#define __DEFINED_uint_least8_t +#endif + +#if defined(__NEED_uint_least16_t) && !defined(__DEFINED_uint_least16_t) +typedef uint16_t uint_least16_t; +#define __DEFINED_uint_least16_t +#endif + +#if defined(__NEED_uint_least32_t) && !defined(__DEFINED_uint_least32_t) +typedef uint32_t uint_least32_t; +#define __DEFINED_uint_least32_t +#endif + +#if defined(__NEED_uint_least64_t) && !defined(__DEFINED_uint_least64_t) +typedef uint64_t uint_least64_t; +#define __DEFINED_uint_least64_t +#endif + + +#if defined(__NEED_int_fast8_t) && !defined(__DEFINED_int_fast8_t) +typedef int8_t int_fast8_t; +#define __DEFINED_int_fast8_t +#endif + +#if defined(__NEED_int_fast16_t) && !defined(__DEFINED_int_fast16_t) +typedef int int_fast16_t; +#define __DEFINED_int_fast16_t +#endif + +#if defined(__NEED_int_fast32_t) && !defined(__DEFINED_int_fast32_t) +typedef int int_fast32_t; +#define __DEFINED_int_fast32_t +#endif + +#if defined(__NEED_int_fast64_t) && !defined(__DEFINED_int_fast64_t) +typedef int64_t int_fast64_t; +#define __DEFINED_int_fast64_t +#endif + + +#if defined(__NEED_uint_fast8_t) && !defined(__DEFINED_uint_fast8_t) +typedef unsigned char uint_fast8_t; +#define __DEFINED_uint_fast8_t +#endif + +#if defined(__NEED_uint_fast16_t) && !defined(__DEFINED_uint_fast16_t) +typedef unsigned int uint_fast16_t; +#define __DEFINED_uint_fast16_t +#endif + +#if defined(__NEED_uint_fast32_t) && !defined(__DEFINED_uint_fast32_t) +typedef unsigned int uint_fast32_t; +#define __DEFINED_uint_fast32_t +#endif + +#if defined(__NEED_uint_fast64_t) && !defined(__DEFINED_uint_fast64_t) +typedef uint64_t uint_fast64_t; +#define __DEFINED_uint_fast64_t +#endif + + +#if defined(__NEED_intptr_t) && !defined(__DEFINED_intptr_t) +typedef long intptr_t; +#define __DEFINED_intptr_t +#endif + +#if defined(__NEED_uintptr_t) && !defined(__DEFINED_uintptr_t) +typedef unsigned long uintptr_t; +#define __DEFINED_uintptr_t +#endif + + +#if defined(__NEED_intmax_t) && !defined(__DEFINED_intmax_t) +typedef long long intmax_t; +#define __DEFINED_intmax_t +#endif + +#if defined(__NEED_uintmax_t) && !defined(__DEFINED_uintmax_t) +typedef unsigned long long uintmax_t; +#define __DEFINED_uintmax_t +#endif + + +#if defined(__NEED_time_t) && !defined(__DEFINED_time_t) +typedef long time_t; +#define __DEFINED_time_t +#endif + +#if defined(__NEED_useconds_t) && !defined(__DEFINED_useconds_t) +typedef long useconds_t; +#define __DEFINED_useconds_t +#endif + +#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t) +typedef long suseconds_t; +#define __DEFINED_suseconds_t +#endif + +#if defined(__NEED_struct_timeval) && !defined(__DEFINED_struct_timeval) +struct timeval { time_t tv_sec; int tv_usec; }; +#define __DEFINED_struct_timeval +#endif + +#if defined(__NEED_struct_timespec) && !defined(__DEFINED_struct_timespec) +struct timespec { time_t tv_sec; unsigned long tv_nsec; }; +#define __DEFINED_struct_timespec +#endif + + +#if defined(__NEED_pid_t) && !defined(__DEFINED_pid_t) +typedef int pid_t; +#define __DEFINED_pid_t +#endif + +#if defined(__NEED_id_t) && !defined(__DEFINED_id_t) +typedef int id_t; +#define __DEFINED_id_t +#endif + +#if defined(__NEED_uid_t) && !defined(__DEFINED_uid_t) +typedef unsigned int uid_t; +#define __DEFINED_uid_t +#endif + +#if defined(__NEED_gid_t) && !defined(__DEFINED_gid_t) +typedef unsigned int gid_t; +#define __DEFINED_gid_t +#endif + +#if defined(__NEED_key_t) && !defined(__DEFINED_key_t) +typedef int key_t; +#define __DEFINED_key_t +#endif + + +#if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t) +typedef struct __pthread * pthread_t; +#define __DEFINED_pthread_t +#endif + +#if defined(__NEED_pthread_once_t) && !defined(__DEFINED_pthread_once_t) +typedef int pthread_once_t; +#define __DEFINED_pthread_once_t +#endif + +#if defined(__NEED_pthread_key_t) && !defined(__DEFINED_pthread_key_t) +typedef int pthread_key_t; +#define __DEFINED_pthread_key_t +#endif + +#if defined(__NEED_pthread_spinlock_t) && !defined(__DEFINED_pthread_spinlock_t) +typedef int pthread_spinlock_t; +#define __DEFINED_pthread_spinlock_t +#endif + + +#if defined(__NEED_pthread_attr_t) && !defined(__DEFINED_pthread_attr_t) +typedef struct { union { int __i[14]; size_t __s[2]; } __u; } pthread_attr_t; +#define __DEFINED_pthread_attr_t +#endif + +#if defined(__NEED_pthread_mutexattr_t) && !defined(__DEFINED_pthread_mutexattr_t) +typedef unsigned pthread_mutexattr_t; +#define __DEFINED_pthread_mutexattr_t +#endif + +#if defined(__NEED_pthread_condattr_t) && !defined(__DEFINED_pthread_condattr_t) +typedef unsigned pthread_condattr_t; +#define __DEFINED_pthread_condattr_t +#endif + +#if defined(__NEED_pthread_barrierattr_t) && !defined(__DEFINED_pthread_barrierattr_t) +typedef unsigned pthread_barrierattr_t; +#define __DEFINED_pthread_barrierattr_t +#endif + +#if defined(__NEED_pthread_rwlockattr_t) && !defined(__DEFINED_pthread_rwlockattr_t) +typedef struct { unsigned __attr[2]; } pthread_rwlockattr_t; +#define __DEFINED_pthread_rwlockattr_t +#endif + + +#if defined(__NEED_pthread_mutex_t) && !defined(__DEFINED_pthread_mutex_t) +typedef struct { union { int __i[10]; void *__p[1]; } __u; } pthread_mutex_t; +#define __DEFINED_pthread_mutex_t +#endif + +#if defined(__NEED_pthread_cond_t) && !defined(__DEFINED_pthread_cond_t) +typedef struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t; +#define __DEFINED_pthread_cond_t +#endif + +#if defined(__NEED_pthread_rwlock_t) && !defined(__DEFINED_pthread_rwlock_t) +typedef struct { union { int __i[14]; void *__p[1]; } __u; } pthread_rwlock_t; +#define __DEFINED_pthread_rwlock_t +#endif + +#if defined(__NEED_pthread_barrier_t) && !defined(__DEFINED_pthread_barrier_t) +typedef struct { union { int __i[8]; void *__p[1]; } __u; } pthread_barrier_t; +#define __DEFINED_pthread_barrier_t +#endif + + +#if defined(__NEED_off_t) && !defined(__DEFINED_off_t) +typedef long off_t; +#define __DEFINED_off_t +#endif + + +#if defined(__NEED_mode_t) && !defined(__DEFINED_mode_t) +typedef unsigned int mode_t; +#define __DEFINED_mode_t +#endif + + +#if defined(__NEED_nlink_t) && !defined(__DEFINED_nlink_t) +typedef unsigned long nlink_t; +#define __DEFINED_nlink_t +#endif + +#if defined(__NEED_ino_t) && !defined(__DEFINED_ino_t) +typedef unsigned long long ino_t; +#define __DEFINED_ino_t +#endif + +#if defined(__NEED_dev_t) && !defined(__DEFINED_dev_t) +typedef unsigned long dev_t; +#define __DEFINED_dev_t +#endif + +#if defined(__NEED_blksize_t) && !defined(__DEFINED_blksize_t) +typedef long blksize_t; +#define __DEFINED_blksize_t +#endif + +#if defined(__NEED_blkcnt_t) && !defined(__DEFINED_blkcnt_t) +typedef long long blkcnt_t; +#define __DEFINED_blkcnt_t +#endif + +#if defined(__NEED_fsblkcnt_t) && !defined(__DEFINED_fsblkcnt_t) +typedef unsigned long long fsblkcnt_t; +#define __DEFINED_fsblkcnt_t +#endif + +#if defined(__NEED_fsfilcnt_t) && !defined(__DEFINED_fsfilcnt_t) +typedef unsigned long long fsfilcnt_t; +#define __DEFINED_fsfilcnt_t +#endif + + +#if defined(__NEED_timer_t) && !defined(__DEFINED_timer_t) +typedef void * timer_t; +#define __DEFINED_timer_t +#endif + +#if defined(__NEED_clockid_t) && !defined(__DEFINED_clockid_t) +typedef int clockid_t; +#define __DEFINED_clockid_t +#endif + +#if defined(__NEED_clock_t) && !defined(__DEFINED_clock_t) +typedef long clock_t; +#define __DEFINED_clock_t +#endif + + +#if defined(__NEED_sigset_t) && !defined(__DEFINED_sigset_t) +typedef struct { unsigned long __bits[128/sizeof(long)]; } sigset_t; +#define __DEFINED_sigset_t +#endif + +#if 1 +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +typedef struct __siginfo siginfo_t; +#define __DEFINED_siginfo_t +#endif + +#else +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +#define siginfo_t struct __siginfo +#define __DEFINED_siginfo_t +#endif +#endif + +#if defined(__NEED_socklen_t) && !defined(__DEFINED_socklen_t) +typedef unsigned int socklen_t; +#define __DEFINED_socklen_t +#endif + +#if defined(__NEED_sa_family_t) && !defined(__DEFINED_sa_family_t) +typedef unsigned short sa_family_t; +#define __DEFINED_sa_family_t +#endif + +#if defined(__NEED_in_port_t) && !defined(__DEFINED_in_port_t) +typedef unsigned short in_port_t; +#define __DEFINED_in_port_t +#endif + +#if defined(__NEED_in_addr_t) && !defined(__DEFINED_in_addr_t) +typedef unsigned int in_addr_t; +#define __DEFINED_in_addr_t +#endif + +#if defined(__NEED_struct_in_addr) && !defined(__DEFINED_struct_in_addr) +struct in_addr { in_addr_t s_addr; }; +#define __DEFINED_struct_in_addr +#endif + + +#if defined(__NEED_FILE) && !defined(__DEFINED_FILE) +typedef struct __FILE_s FILE; +#define __DEFINED_FILE +#endif + + +#if defined(__NEED_nl_item) && !defined(__DEFINED_nl_item) +typedef int nl_item; +#define __DEFINED_nl_item +#endif + + +#if defined(__NEED_locale_t) && !defined(__DEFINED_locale_t) +typedef struct __locale * locale_t; +#define __DEFINED_locale_t +#endif + + +#if defined(__NEED_struct_iovec) && !defined(__DEFINED_struct_iovec) +struct iovec { void *iov_base; size_t iov_len; }; +#define __DEFINED_struct_iovec +#endif + + diff --git a/05/musl-final/arch/x86_64/bits/alltypes.h.sh b/05/musl-final/arch/x86_64/bits/alltypes.h.sh new file mode 100755 index 0000000..d9f4f7d --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/alltypes.h.sh @@ -0,0 +1,142 @@ +#!/bin/sh +sed -e << EOF \ +'/^TYPEDEF/s/TYPEDEF \(.*\) \([^ ]*\);$/#if defined(__NEED_\2) \&\& !defined(__DEFINED_\2)\ +typedef \1 \2;\ +#define __DEFINED_\2\ +#endif\ +/ +/^STRUCT/s/STRUCT * \([^ ]*\) \(.*\);$/#if defined(__NEED_struct_\1) \&\& !defined(__DEFINED_struct_\1)\ +struct \1 \2;\ +#define __DEFINED_struct_\1\ +#endif\ +/ +/^UNION/s/UNION * \([^ ]*\) \(.*\);$/#if defined(__NEED_union_\1) \&\& !defined(__DEFINED_union_\1)\ +union \1 \2;\ +#define __DEFINED_union_\1\ +#endif\ +/' + +#include + +TYPEDEF unsigned long size_t; +TYPEDEF long ssize_t; +TYPEDEF long ptrdiff_t; +#if 0 +TYPEDEF __builtin_va_list va_list; +#endif + +TYPEDEF int wchar_t; +TYPEDEF int wint_t; +TYPEDEF int wctrans_t; +TYPEDEF int wctype_t; + +TYPEDEF char int8_t; +TYPEDEF short int16_t; +TYPEDEF int int32_t; +TYPEDEF long int64_t; + +TYPEDEF unsigned char uint8_t; +TYPEDEF unsigned short uint16_t; +TYPEDEF unsigned int uint32_t; +TYPEDEF unsigned long uint64_t; + +TYPEDEF unsigned char __uint8_t; +TYPEDEF unsigned short __uint16_t; +TYPEDEF unsigned int __uint32_t; +TYPEDEF unsigned long __uint64_t; + +TYPEDEF int8_t int_least8_t; +TYPEDEF int16_t int_least16_t; +TYPEDEF int32_t int_least32_t; +TYPEDEF int64_t int_least64_t; + +TYPEDEF uint8_t uint_least8_t; +TYPEDEF uint16_t uint_least16_t; +TYPEDEF uint32_t uint_least32_t; +TYPEDEF uint64_t uint_least64_t; + +TYPEDEF int8_t int_fast8_t; +TYPEDEF int int_fast16_t; +TYPEDEF int int_fast32_t; +TYPEDEF int64_t int_fast64_t; + +TYPEDEF unsigned char uint_fast8_t; +TYPEDEF unsigned int uint_fast16_t; +TYPEDEF unsigned int uint_fast32_t; +TYPEDEF uint64_t uint_fast64_t; + +TYPEDEF long intptr_t; +TYPEDEF unsigned long uintptr_t; + +TYPEDEF long long intmax_t; +TYPEDEF unsigned long long uintmax_t; + +TYPEDEF long time_t; +TYPEDEF long useconds_t; +TYPEDEF long suseconds_t; +STRUCT timeval { time_t tv_sec; int tv_usec; }; +STRUCT timespec { time_t tv_sec; unsigned long tv_nsec; }; + +TYPEDEF int pid_t; +TYPEDEF int id_t; +TYPEDEF unsigned int uid_t; +TYPEDEF unsigned int gid_t; +TYPEDEF int key_t; + +TYPEDEF struct __pthread * pthread_t; +TYPEDEF int pthread_once_t; +TYPEDEF int pthread_key_t; +TYPEDEF int pthread_spinlock_t; + +TYPEDEF struct { union { int __i[14]; size_t __s[2]; } __u; } pthread_attr_t; +TYPEDEF unsigned pthread_mutexattr_t; +TYPEDEF unsigned pthread_condattr_t; +TYPEDEF unsigned pthread_barrierattr_t; +TYPEDEF struct { unsigned __attr[2]; } pthread_rwlockattr_t; + +TYPEDEF struct { union { int __i[10]; void *__p[1]; } __u; } pthread_mutex_t; +TYPEDEF struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t; +TYPEDEF struct { union { int __i[14]; void *__p[1]; } __u; } pthread_rwlock_t; +TYPEDEF struct { union { int __i[8]; void *__p[1]; } __u; } pthread_barrier_t; + +TYPEDEF long off_t; + +TYPEDEF unsigned int mode_t; + +TYPEDEF unsigned long nlink_t; +TYPEDEF unsigned long long ino_t; +TYPEDEF unsigned long dev_t; +TYPEDEF long blksize_t; +TYPEDEF long long blkcnt_t; +TYPEDEF unsigned long long fsblkcnt_t; +TYPEDEF unsigned long long fsfilcnt_t; + +TYPEDEF void * timer_t; +TYPEDEF int clockid_t; +TYPEDEF long clock_t; + +TYPEDEF struct { unsigned long __bits[128/sizeof(long)]; } sigset_t; +#if 1 +TYPEDEF struct __siginfo siginfo_t; +#else +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +#define siginfo_t struct __siginfo +#define __DEFINED_siginfo_t +#endif +#endif + +TYPEDEF unsigned int socklen_t; +TYPEDEF unsigned short sa_family_t; +TYPEDEF unsigned short in_port_t; +TYPEDEF unsigned int in_addr_t; +STRUCT in_addr { in_addr_t s_addr; }; + +TYPEDEF struct __FILE_s FILE; + +TYPEDEF int nl_item; + +TYPEDEF struct __locale * locale_t; + +STRUCT iovec { void *iov_base; size_t iov_len; }; + +EOF diff --git a/05/musl-final/arch/x86_64/bits/endian.h b/05/musl-final/arch/x86_64/bits/endian.h new file mode 100644 index 0000000..172c338 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/endian.h @@ -0,0 +1 @@ +#define __BYTE_ORDER __LITTLE_ENDIAN diff --git a/05/musl-final/arch/x86_64/bits/errno.h b/05/musl-final/arch/x86_64/bits/errno.h new file mode 100644 index 0000000..ec3d363 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/errno.h @@ -0,0 +1,132 @@ +#define EPERM 1 +#define ENOENT 2 +#define ESRCH 3 +#define EINTR 4 +#define EIO 5 +#define ENXIO 6 +#define E2BIG 7 +#define ENOEXEC 8 +#define EBADF 9 +#define ECHILD 10 +#define EAGAIN 11 +#define ENOMEM 12 +#define EACCES 13 +#define EFAULT 14 +#define ENOTBLK 15 +#define EBUSY 16 +#define EEXIST 17 +#define EXDEV 18 +#define ENODEV 19 +#define ENOTDIR 20 +#define EISDIR 21 +#define EINVAL 22 +#define ENFILE 23 +#define EMFILE 24 +#define ENOTTY 25 +#define ETXTBSY 26 +#define EFBIG 27 +#define ENOSPC 28 +#define ESPIPE 29 +#define EROFS 30 +#define EMLINK 31 +#define EPIPE 32 +#define EDOM 33 +#define ERANGE 34 +#define EDEADLK 35 +#define ENAMETOOLONG 36 +#define ENOLCK 37 +#define ENOSYS 38 +#define ENOTEMPTY 39 +#define ELOOP 40 +#define EWOULDBLOCK EAGAIN +#define ENOMSG 42 +#define EIDRM 43 +#define ECHRNG 44 +#define EL2NSYNC 45 +#define EL3HLT 46 +#define EL3RST 47 +#define ELNRNG 48 +#define EUNATCH 49 +#define ENOCSI 50 +#define EL2HLT 51 +#define EBADE 52 +#define EBADR 53 +#define EXFULL 54 +#define ENOANO 55 +#define EBADRQC 56 +#define EBADSLT 57 +#define EDEADLOCK EDEADLK +#define EBFONT 59 +#define ENOSTR 60 +#define ENODATA 61 +#define ETIME 62 +#define ENOSR 63 +#define ENONET 64 +#define ENOPKG 65 +#define EREMOTE 66 +#define ENOLINK 67 +#define EADV 68 +#define ESRMNT 69 +#define ECOMM 70 +#define EPROTO 71 +#define EMULTIHOP 72 +#define EDOTDOT 73 +#define EBADMSG 74 +#define EOVERFLOW 75 +#define ENOTUNIQ 76 +#define EBADFD 77 +#define EREMCHG 78 +#define ELIBACC 79 +#define ELIBBAD 80 +#define ELIBSCN 81 +#define ELIBMAX 82 +#define ELIBEXEC 83 +#define EILSEQ 84 +#define ERESTART 85 +#define ESTRPIPE 86 +#define EUSERS 87 +#define ENOTSOCK 88 +#define EDESTADDRREQ 89 +#define EMSGSIZE 90 +#define EPROTOTYPE 91 +#define ENOPROTOOPT 92 +#define EPROTONOSUPPORT 93 +#define ESOCKTNOSUPPORT 94 +#define EOPNOTSUPP 95 +#define EPFNOSUPPORT 96 +#define EAFNOSUPPORT 97 +#define EADDRINUSE 98 +#define EADDRNOTAVAIL 99 +#define ENETDOWN 100 +#define ENETUNREACH 101 +#define ENETRESET 102 +#define ECONNABORTED 103 +#define ECONNRESET 104 +#define ENOBUFS 105 +#define EISCONN 106 +#define ENOTCONN 107 +#define ESHUTDOWN 108 +#define ETOOMANYREFS 109 +#define ETIMEDOUT 110 +#define ECONNREFUSED 111 +#define EHOSTDOWN 112 +#define EHOSTUNREACH 113 +#define EALREADY 114 +#define EINPROGRESS 115 +#define ESTALE 116 +#define EUCLEAN 117 +#define ENOTNAM 118 +#define ENAVAIL 119 +#define EISNAM 120 +#define EREMOTEIO 121 +#define EDQUOT 122 +#define ENOMEDIUM 123 +#define EMEDIUMTYPE 124 +#define ECANCELED 125 +#define ENOKEY 126 +#define EKEYEXPIRED 127 +#define EKEYREVOKED 128 +#define EKEYREJECTED 129 +#define EOWNERDEAD 130 +#define ENOTRECOVERABLE 131 +#define ERFKILL 132 diff --git a/05/musl-final/arch/x86_64/bits/fcntl.h b/05/musl-final/arch/x86_64/bits/fcntl.h new file mode 100644 index 0000000..802c3d1 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/fcntl.h @@ -0,0 +1,60 @@ +#define O_ACCMODE 03 +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 + +#define O_CREAT 0100 +#define O_EXCL 0200 +#define O_NOCTTY 0400 +#define O_TRUNC 01000 +#define O_APPEND 02000 +#define O_NONBLOCK 04000 +#define O_SYNC 010000 +#define O_DIRECTORY 0200000 +#define O_NOFOLLOW 0400000 +#define O_CLOEXEC 02000000 + +#ifdef _GNU_SOURCE +#define O_NDELAY O_NONBLOCK +#define O_ASYNC 020000 +#define O_DIRECT 040000 +#define O_NOATIME 01000000 +#define F_DUPFD_CLOEXEC 1030 +#define FAPPENT O_APPEND +#define FFSYNC O_FSYNC +#define FASYNC O_ASYNC +#define FNONBLOCK O_NONBLOCK +#define FNDELAY O_NDELAY +#endif + +#define F_DUPFD 0 +#define F_GETFD 1 +#define F_SETFD 2 +#define F_GETFL 3 +#define F_SETFL 4 + +#define F_SETOWN 8 +#define F_GETOWN 9 + +#define F_GETLK 12 +#define F_SETLK 13 +#define F_SETLKW 14 + +#define FD_CLOEXEC 1 + +#define F_RDLCK 0 +#define F_WRLCK 1 +#define F_UNLCK 2 + +#define AT_FDCWD (-100) +#define AT_SYMLINK_NOFOLLOW 0x100 +#define AT_REMOVEDIR 0x200 +#define AT_SYMLINK_FOLLOW 0x400 +#define AT_EACCESS 0x200 + +#define POSIX_FADV_NORMAL 0 +#define POSIX_FADV_RANDOM 1 +#define POSIX_FADV_SEQUENTIAL 2 +#define POSIX_FADV_WILLNEED 3 +#define POSIX_FADV_DONTNEED 4 +#define POSIX_FADV_NOREUSE 5 diff --git a/05/musl-final/arch/x86_64/bits/fenv.h b/05/musl-final/arch/x86_64/bits/fenv.h new file mode 100644 index 0000000..24df041 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/fenv.h @@ -0,0 +1,34 @@ +#define FE_INVALID 1 +#define __FE_DENORM 2 +#define FE_DIVBYZERO 4 +#define FE_OVERFLOW 8 +#define FE_UNDERFLOW 16 +#define FE_INEXACT 32 + +#define FE_ALL_EXCEPT 63 + +#define FE_TONEAREST 0 +#define FE_DOWNWARD 0x400 +#define FE_UPWARD 0x800 +#define FE_TOWARDZERO 0xc00 + +typedef unsigned short fexcept_t; + +typedef struct { + unsigned short __control_word; + unsigned short __unused1; + unsigned short __status_word; + unsigned short __unused2; + unsigned short __tags; + unsigned short __unused3; + unsigned int __eip; + unsigned short __cs_selector; + unsigned int __opcode:11; + unsigned int __unused4:5; + unsigned int __data_offset; + unsigned short __data_selector; + unsigned short __unused5; + unsigned int __mxcsr; +} fenv_t; + +#define FE_DFL_ENV ((const fenv_t *) -1) diff --git a/05/musl-final/arch/x86_64/bits/float.h b/05/musl-final/arch/x86_64/bits/float.h new file mode 100644 index 0000000..162b3e4 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/float.h @@ -0,0 +1,11 @@ +#define LDBL_MIN 3.3621031431120935063e-4932L +#define LDBL_MAX 1.1897314953572317650e+4932L +#define LDBL_EPSILON 1.0842021724855044340e-19L + +#define LDBL_MANT_DIG 64 +#define LDBL_MIN_EXP (-16381) +#define LDBL_MAX_EXP 16384 + +#define LDBL_DIG 18 +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_10_EXP 4932 diff --git a/05/musl-final/arch/x86_64/bits/in.h b/05/musl-final/arch/x86_64/bits/in.h new file mode 100644 index 0000000..b233706 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/in.h @@ -0,0 +1,133 @@ +#define IP_TOS 1 +#define IP_TTL 2 +#define IP_HDRINCL 3 +#define IP_OPTIONS 4 +#define IP_ROUTER_ALERT 5 +#define IP_RECVOPTS 6 +#define IP_RETOPTS 7 +//#define IP_PKTINFO 8 +#define IP_PKTOPTIONS 9 +#define IP_PMTUDISC 10 +#define IP_MTU_DISCOVER 10 +#define IP_RECVERR 11 +#define IP_RECVTTL 12 +#define IP_RECVTOS 13 +#define IP_MTU 14 +#define IP_FREEBIND 15 +#define IP_IPSEC_POLICY 16 +#define IP_XFRM_POLICY 17 +#define IP_PASSSEC 18 +#define IP_TRANSPARENT 19 +#define IP_ORIGDSTADDR 20 +#define IP_RECVORIGDSTADDR IP_ORIGDSTADDR +#define IP_MINTTL 21 +#define IP_MULTICAST_IF 32 +#define IP_MULTICAST_TTL 33 +#define IP_MULTICAST_LOOP 34 +#define IP_ADD_MEMBERSHIP 35 +#define IP_DROP_MEMBERSHIP 36 +#define IP_UNBLOCK_SOURCE 37 +#define IP_BLOCK_SOURCE 38 +#define IP_ADD_SOURCE_MEMBERSHIP 39 +#define IP_DROP_SOURCE_MEMBERSHIP 40 +#define IP_MSFILTER 41 + +#define IP_RECVRETOPTS IP_RETOPTS + +#define IP_PMTUDISC_DONT 0 +#define IP_PMTUDISC_WANT 1 +#define IP_PMTUDISC_DO 2 +#define IP_PMTUDISC_PROBE 3 + +#define SOL_IP 0 + +#define IP_DEFAULT_MULTICAST_TTL 1 +#define IP_DEFAULT_MULTICAST_LOOP 1 +#define IP_MAX_MEMBERSHIPS 20 + +struct ip_opts +{ + struct in_addr ip_dst; + char ip_opts[40]; +}; + +struct ip_mreq +{ + struct in_addr imr_multiaddr; + struct in_addr imr_interface; +}; + +struct ip_mreqn +{ + struct in_addr imr_multiaddr; + struct in_addr imr_address; + int imr_ifindex; +}; + +struct in_pktinfo +{ + int ipi_ifindex; + struct in_addr ipi_spec_dst; + struct in_addr ipi_addr; +}; + +#define IPV6_ADDRFORM 1 +#define IPV6_2292PKTINFO 2 +#define IPV6_2292HOPOPTS 3 +#define IPV6_2292DSTOPTS 4 +#define IPV6_2292RTHDR 5 +#define IPV6_2292PKTOPTIONS 6 +#define IPV6_CHECKSUM 7 +#define IPV6_2292HOPLIMIT 8 +#define SCM_SRCRT IPV6_RXSRCRT +#define IPV6_NEXTHOP 9 +#define IPV6_AUTHHDR 10 +#define IPV6_UNICAST_HOPS 16 +#define IPV6_MULTICAST_IF 17 +#define IPV6_MULTICAST_HOPS 18 +#define IPV6_MULTICAST_LOOP 19 +#define IPV6_JOIN_GROUP 20 +#define IPV6_LEAVE_GROUP 21 +#define IPV6_ROUTER_ALERT 22 +#define IPV6_MTU_DISCOVER 23 +#define IPV6_MTU 24 +#define IPV6_RECVERR 25 +#define IPV6_V6ONLY 26 +#define IPV6_JOIN_ANYCAST 27 +#define IPV6_LEAVE_ANYCAST 28 +#define IPV6_IPSEC_POLICY 34 +#define IPV6_XFRM_POLICY 35 + +#define IPV6_RECVPKTINFO 49 +#define IPV6_PKTINFO 50 +#define IPV6_RECVHOPLIMIT 51 +#define IPV6_HOPLIMIT 52 +#define IPV6_RECVHOPOPTS 53 +#define IPV6_HOPOPTS 54 +#define IPV6_RTHDRDSTOPTS 55 +#define IPV6_RECVRTHDR 56 +#define IPV6_RTHDR 57 +#define IPV6_RECVDSTOPTS 58 +#define IPV6_DSTOPTS 59 + +#define IPV6_RECVTCLASS 66 +#define IPV6_TCLASS 67 + +#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP +#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP +#define IPV6_RXHOPOPTS IPV6_HOPOPTS +#define IPV6_RXDSTOPTS IPV6_DSTOPTS + + +#define IPV6_PMTUDISC_DONT 0 +#define IPV6_PMTUDISC_WANT 1 +#define IPV6_PMTUDISC_DO 2 +#define IPV6_PMTUDISC_PROBE 3 + +#define SOL_IPV6 41 +#define SOL_ICMPV6 58 + +#define IPV6_RTHDR_LOOSE 0 +#define IPV6_RTHDR_STRICT 1 + +#define IPV6_RTHDR_TYPE_0 0 diff --git a/05/musl-final/arch/x86_64/bits/ioctl.h b/05/musl-final/arch/x86_64/bits/ioctl.h new file mode 100644 index 0000000..7f3c038 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/ioctl.h @@ -0,0 +1,197 @@ +#define _IOC(a,b,c,d) ( ((a)<<30) | ((b)<<8) | (c) | ((d)<<16) ) +#define _IOC_NONE 0U +#define _IOC_WRITE 1U +#define _IOC_READ 2U + +#define _IO(a,b) _IOC(_IOC_NONE,(a),(b),0) +#define _IOW(a,b,c) _IOC(1,(a),(b),sizeof(c)) +#define _IOR(a,b,c) _IOC(2,(a),(b),sizeof(c)) +#define _IOWR(a,b,c) _IOC(3,(a),(b),sizeof(c)) + +#define TCGETS 0x5401 +#define TCSETS 0x5402 +#define TCSETSW 0x5403 +#define TCSETSF 0x5404 +#define TCGETA 0x5405 +#define TCSETA 0x5406 +#define TCSETAW 0x5407 +#define TCSETAF 0x5408 +#define TCSBRK 0x5409 +#define TCXONC 0x540A +#define TCFLSH 0x540B +#define TIOCEXCL 0x540C +#define TIOCNXCL 0x540D +#define TIOCSCTTY 0x540E +#define TIOCGPGRP 0x540F +#define TIOCSPGRP 0x5410 +#define TIOCOUTQ 0x5411 +#define TIOCSTI 0x5412 +#define TIOCGWINSZ 0x5413 +#define TIOCSWINSZ 0x5414 +#define TIOCMGET 0x5415 +#define TIOCMBIS 0x5416 +#define TIOCMBIC 0x5417 +#define TIOCMSET 0x5418 +#define TIOCGSOFTCAR 0x5419 +#define TIOCSSOFTCAR 0x541A +#define FIONREAD 0x541B +#define TIOCINQ FIONREAD +#define TIOCLINUX 0x541C +#define TIOCCONS 0x541D +#define TIOCGSERIAL 0x541E +#define TIOCSSERIAL 0x541F +#define TIOCPKT 0x5420 +#define FIONBIO 0x5421 +#define TIOCNOTTY 0x5422 +#define TIOCSETD 0x5423 +#define TIOCGETD 0x5424 +#define TCSBRKP 0x5425 +#define TIOCTTYGSTRUCT 0x5426 +#define TIOCSBRK 0x5427 +#define TIOCCBRK 0x5428 +#define TIOCGSID 0x5429 +#define TIOCGPTN 0x80045430 +#define TIOCSPTLCK 0x40045431 +#define TCGETX 0x5432 +#define TCSETX 0x5433 +#define TCSETXF 0x5434 +#define TCSETXW 0x5435 + +#define FIONCLEX 0x5450 +#define FIOCLEX 0x5451 +#define FIOASYNC 0x5452 +#define TIOCSERCONFIG 0x5453 +#define TIOCSERGWILD 0x5454 +#define TIOCSERSWILD 0x5455 +#define TIOCGLCKTRMIOS 0x5456 +#define TIOCSLCKTRMIOS 0x5457 +#define TIOCSERGSTRUCT 0x5458 +#define TIOCSERGETLSR 0x5459 +#define TIOCSERGETMULTI 0x545A +#define TIOCSERSETMULTI 0x545B + +#define TIOCMIWAIT 0x545C +#define TIOCGICOUNT 0x545D +#define TIOCGHAYESESP 0x545E +#define TIOCSHAYESESP 0x545F +#define FIOQSIZE 0x5460 + +#define TIOCPKT_DATA 0 +#define TIOCPKT_FLUSHREAD 1 +#define TIOCPKT_FLUSHWRITE 2 +#define TIOCPKT_STOP 4 +#define TIOCPKT_START 8 +#define TIOCPKT_NOSTOP 16 +#define TIOCPKT_DOSTOP 32 +#define TIOCPKT_IOCTL 64 + +#define TIOCSER_TEMT 0x01 + +struct winsize { + unsigned short ws_row; + unsigned short ws_col; + unsigned short ws_xpixel; + unsigned short ws_ypixel; +}; + +#define TIOCM_LE 0x001 +#define TIOCM_DTR 0x002 +#define TIOCM_RTS 0x004 +#define TIOCM_ST 0x008 +#define TIOCM_SR 0x010 +#define TIOCM_CTS 0x020 +#define TIOCM_CAR 0x040 +#define TIOCM_RNG 0x080 +#define TIOCM_DSR 0x100 +#define TIOCM_CD TIOCM_CAR +#define TIOCM_RI TIOCM_RNG +#define TIOCM_OUT1 0x2000 +#define TIOCM_OUT2 0x4000 +#define TIOCM_LOOP 0x8000 +#define TIOCM_MODEM_BITS TIOCM_OUT2 + +#define N_TTY 0 +#define N_SLIP 1 +#define N_MOUSE 2 +#define N_PPP 3 +#define N_STRIP 4 +#define N_AX25 5 +#define N_X25 6 +#define N_6PACK 7 +#define N_MASC 8 +#define N_R3964 9 +#define N_PROFIBUS_FDL 10 +#define N_IRDA 11 +#define N_SMSBLOCK 12 +#define N_HDLC 13 +#define N_SYNC_PPP 14 +#define N_HCI 15 + +#define FIOSETOWN 0x8901 +#define SIOCSPGRP 0x8902 +#define FIOGETOWN 0x8903 +#define SIOCGPGRP 0x8904 +#define SIOCATMARK 0x8905 +#define SIOCGSTAMP 0x8906 + +#define SIOCADDRT 0x890B +#define SIOCDELRT 0x890C +#define SIOCRTMSG 0x890D + +#define SIOCGIFNAME 0x8910 +#define SIOCSIFLINK 0x8911 +#define SIOCGIFCONF 0x8912 +#define SIOCGIFFLAGS 0x8913 +#define SIOCSIFFLAGS 0x8914 +#define SIOCGIFADDR 0x8915 +#define SIOCSIFADDR 0x8916 +#define SIOCGIFDSTADDR 0x8917 +#define SIOCSIFDSTADDR 0x8918 +#define SIOCGIFBRDADDR 0x8919 +#define SIOCSIFBRDADDR 0x891a +#define SIOCGIFNETMASK 0x891b +#define SIOCSIFNETMASK 0x891c +#define SIOCGIFMETRIC 0x891d +#define SIOCSIFMETRIC 0x891e +#define SIOCGIFMEM 0x891f +#define SIOCSIFMEM 0x8920 +#define SIOCGIFMTU 0x8921 +#define SIOCSIFMTU 0x8922 +#define SIOCSIFHWADDR 0x8924 +#define SIOCGIFENCAP 0x8925 +#define SIOCSIFENCAP 0x8926 +#define SIOCGIFHWADDR 0x8927 +#define SIOCGIFSLAVE 0x8929 +#define SIOCSIFSLAVE 0x8930 +#define SIOCADDMULTI 0x8931 +#define SIOCDELMULTI 0x8932 +#define SIOCGIFINDEX 0x8933 +#define SIOGIFINDEX SIOCGIFINDEX +#define SIOCSIFPFLAGS 0x8934 +#define SIOCGIFPFLAGS 0x8935 +#define SIOCDIFADDR 0x8936 +#define SIOCSIFHWBROADCAST 0x8937 +#define SIOCGIFCOUNT 0x8938 + +#define SIOCGIFBR 0x8940 +#define SIOCSIFBR 0x8941 + +#define SIOCGIFTXQLEN 0x8942 +#define SIOCSIFTXQLEN 0x8943 + +#define SIOCDARP 0x8953 +#define SIOCGARP 0x8954 +#define SIOCSARP 0x8955 + +#define SIOCDRARP 0x8960 +#define SIOCGRARP 0x8961 +#define SIOCSRARP 0x8962 + +#define SIOCGIFMAP 0x8970 +#define SIOCSIFMAP 0x8971 + +#define SIOCADDDLCI 0x8980 +#define SIOCDELDLCI 0x8981 + +#define SIOCDEVPRIVATE 0x89F0 +#define SIOCPROTOPRIVATE 0x89E0 diff --git a/05/musl-final/arch/x86_64/bits/ipc.h b/05/musl-final/arch/x86_64/bits/ipc.h new file mode 100644 index 0000000..04fbefe --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/ipc.h @@ -0,0 +1,25 @@ +#define IPC_CREAT 01000 +#define IPC_EXCL 02000 +#define IPC_NOWAIT 04000 + +#define IPC_RMID 0 +#define IPC_SET 1 +#define IPC_STAT 2 +#ifdef _GNU_SOURCE +# define IPC_INFO 3 /* See ipcs. */ +#endif + +#define IPC_PRIVATE ((key_t) 0) + +struct ipc_perm +{ + key_t key; + uid_t uid; + gid_t gid; + uid_t cuid; + gid_t cgid; + mode_t mode; + int seq; + long __pad1; + long __pad2; +}; diff --git a/05/musl-final/arch/x86_64/bits/limits.h b/05/musl-final/arch/x86_64/bits/limits.h new file mode 100644 index 0000000..4654489 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/limits.h @@ -0,0 +1,34 @@ +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define PIPE_BUF 4096 +#define PAGESIZE 4096 +#define PAGE_SIZE PAGESIZE +#define FILESIZEBITS 64 +#define NAME_MAX 255 +#define SYMLINK_MAX 255 +#define PATH_MAX 4096 +#define NZERO 20 +#define NGROUPS_MAX 32 +#define ARG_MAX 131072 +#define IOV_MAX 1024 +#define SYMLOOP_MAX 40 +#define WORD_BIT 64 +#define LONG_BIT 64 +#endif + +#define SHRT_MIN (-1-0x7fff) +#define SHRT_MAX 0x7fff +#define USHRT_MAX 0xffff + +#define INT_MIN (-1-0x7fffffff) +#define INT_MAX 0x7fffffff +#define UINT_MAX 0xffffffffU + +#define LONG_MIN (-1-0x7fffffffffffffffL) +#define LONG_MAX 0x7fffffffffffffffL +#define ULONG_MAX 0xffffffffffffffffUL + +#define LLONG_MIN (-1-0x7fffffffffffffffLL) +#define LLONG_MAX 0x7fffffffffffffffLL +#define ULLONG_MAX 0xffffffffffffffffULL + diff --git a/05/musl-final/arch/x86_64/bits/mman.h b/05/musl-final/arch/x86_64/bits/mman.h new file mode 100644 index 0000000..cd6cb84 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/mman.h @@ -0,0 +1,50 @@ +#define MAP_FAILED ((void *) -1) + +#define PROT_NONE 0 +#define PROT_READ 1 +#define PROT_WRITE 2 +#define PROT_EXEC 4 +#define PROT_GROWSDOWN 0x01000000 +#define PROT_GROWSUP 0x02000000 + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_FIXED 0x10 + +/* linux extensions */ +#define MAP_TYPE 0x0f +#define MAP_FILE 0x00 +#define MAP_ANON 0x20 +#define MAP_ANONYMOUS MAP_ANON +#define MAP_32BIT 0x40 + +#define MADV_NORMAL 0 +#define MADV_RANDOM 1 +#define MADV_SEQUENTIAL 2 +#define MADV_WILLNEED 3 +#define MADV_DONTNEED 4 +#define MADV_REMOVE 9 +#define MADV_DONTFORK 10 +#define MADV_DOFORK 11 +#define MADV_MERGEABLE 12 +#define MADV_UNMERGEABLE 13 +#define MADV_HUGEPAGE 14 +#define MADV_NOHUGEPAGE 15 +#define MADV_HWPOISON 100 + +#define POSIX_MADV_NORMAL 0 +#define POSIX_MADV_RANDOM 1 +#define POSIX_MADV_SEQUENTIAL 2 +#define POSIX_MADV_WILLNEED 3 +#define POSIX_MADV_DONTNEED 0 + +#define MS_ASYNC 1 +#define MS_INVALIDATE 2 +#define MS_SYNC 4 + +#define MCL_CURRENT 1 +#define MCL_FUTURE 2 + +/* linux extensions */ +#define MREMAP_MAYMOVE 1 +#define MREMAP_FIXED 2 diff --git a/05/musl-final/arch/x86_64/bits/posix.h b/05/musl-final/arch/x86_64/bits/posix.h new file mode 100644 index 0000000..d9f01fd --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/posix.h @@ -0,0 +1,2 @@ +#define _POSIX_V6_LP64_OFF64 1 +#define _POSIX_V6_LP64_OFF64 1 diff --git a/05/musl-final/arch/x86_64/bits/pthread.h b/05/musl-final/arch/x86_64/bits/pthread.h new file mode 100644 index 0000000..7d19065 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/pthread.h @@ -0,0 +1,6 @@ +struct __ptcb { + long __jb[7]; + int __dummy; + struct __ptcb *__next; + void *__ptrs[3]; +}; diff --git a/05/musl-final/arch/x86_64/bits/reg.h b/05/musl-final/arch/x86_64/bits/reg.h new file mode 100644 index 0000000..a4df04c --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/reg.h @@ -0,0 +1,29 @@ +#undef __WORDSIZE +#define __WORDSIZE 64 +#define R15 0 +#define R14 1 +#define R13 2 +#define R12 3 +#define RBP 4 +#define RBX 5 +#define R11 6 +#define R10 7 +#define R9 8 +#define R8 9 +#define RAX 10 +#define RCX 11 +#define RDX 12 +#define RSI 13 +#define RDI 14 +#define ORIG_RAX 15 +#define RIP 16 +#define CS 17 +#define EFLAGS 18 +#define RSP 19 +#define SS 20 +#define FS_BASE 21 +#define GS_BASE 22 +#define DS 23 +#define ES 24 +#define FS 25 +#define GS 26 diff --git a/05/musl-final/arch/x86_64/bits/setjmp.h b/05/musl-final/arch/x86_64/bits/setjmp.h new file mode 100644 index 0000000..c5b9e4a --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/setjmp.h @@ -0,0 +1 @@ +typedef unsigned long jmp_buf [9]; diff --git a/05/musl-final/arch/x86_64/bits/shm.h b/05/musl-final/arch/x86_64/bits/shm.h new file mode 100644 index 0000000..bc95a1c --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/shm.h @@ -0,0 +1,24 @@ +#define SHMLBA 4096 + +#define SHM_RDONLY 010000 +#define SHM_RND 020000 +#define SHM_REMAP 040000 +#define SHM_EXEC 0100000 + +/* linux extensions */ +#define SHM_LOCK 11 +#define SHM_UNLOCK 12 + +struct shmid_ds +{ + struct ipc_perm shm_perm; + size_t shm_segsz; + time_t shm_atime; + time_t shm_dtime; + time_t shm_ctime; + pid_t shm_cpid; + pid_t shm_lpid; + unsigned long shm_nattch; + unsigned long __pad1; + unsigned long __pad2; +}; diff --git a/05/musl-final/arch/x86_64/bits/signal.h b/05/musl-final/arch/x86_64/bits/signal.h new file mode 100644 index 0000000..64ece19 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/signal.h @@ -0,0 +1,115 @@ +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +struct __siginfo +{ + int si_signo; + int si_errno; + int si_code; + union + { + int __pad[(128 - 4*sizeof(int)) / sizeof(int)]; + struct { + pid_t si_pid; + uid_t si_uid; + } __kill; + struct { + void *si_timerid; + int si_overrun; + char __pad[sizeof(uid_t) - sizeof(int)]; + union sigval si_sigval; + int si_private; + } __timer; + struct { + pid_t si_pid; + uid_t si_uid; + union sigval si_sigval; + } __rt; + struct { + pid_t si_pid; + uid_t si_uid; + int si_status; + clock_t si_utime; + clock_t si_stime; + } __sigchld; + struct { + void *si_addr; + short addr_lsb; + } __sigfault; + struct { + long si_band; + int si_fd; + } __sigpoll; + } __si_fields; +}; + +#define si_pid __si_fields.__sigchld.si_pid +#define si_uid __si_fields.__sigchld.si_uid +#define si_status __si_fields.__sigchld.si_status +#define si_utime __si_fields.__sigchld.si_utime +#define si_stime __si_fields.__sigchld.si_stime +#define si_value __si_fields.__rt.si_sigval +#define si_addr __si_fields.__sigfault.si_addr +#define si_band __si_fields.__sigpoll.si_band + +#define SA_NOCLDSTOP 1 +#define SA_NOCLDWAIT 2 +#define SA_SIGINFO 4 +#define SA_ONSTACK 0x08000000 +#define SA_RESTART 0x10000000 +#define SA_NODEFER 0x40000000 +#define SA_RESETHAND 0x80000000 +#define SA_RESTORER 0x04000000 + +#define SS_ONSTACK 1 +#define SS_DISABLE 2 + +#define SIG_BLOCK 0 +#define SIG_UNBLOCK 1 +#define SIG_SETMASK 2 + +#define SIG_HOLD ((void (*)(int)) 2) + +#endif + +#ifdef _GNU_SOURCE +#define NSIG 64 +#endif + +#define SIG_ERR ((void (*)(int))-1) +#define SIG_DFL ((void (*)(int)) 0) +#define SIG_IGN ((void (*)(int)) 1) + +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGTRAP 5 +#define SIGABRT 6 +#define SIGBUS 7 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGSTKFLT 16 +#define SIGCHLD 17 +#define SIGCONT 18 +#define SIGSTOP 19 +#define SIGTSTP 20 +#define SIGTTIN 21 +#define SIGTTOU 22 +#define SIGURG 23 +#define SIGXCPU 24 +#define SIGXFSZ 25 +#define SIGVTALRM 26 +#define SIGPROF 27 +#define SIGWINCH 28 +#define SIGIO 29 +#define SIGPOLL 29 +#define SIGPWR 30 +#define SIGSYS 31 +#define SIGUNUSED SIGSYS diff --git a/05/musl-final/arch/x86_64/bits/socket.h b/05/musl-final/arch/x86_64/bits/socket.h new file mode 100644 index 0000000..b323062 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/socket.h @@ -0,0 +1,210 @@ +struct msghdr +{ + void *msg_name; + socklen_t msg_namelen; + struct iovec *msg_iov; + int msg_iovlen; + void *msg_control; + socklen_t msg_controllen; + int msg_flags; +}; + +struct cmsghdr +{ + socklen_t cmsg_len; + int cmsg_level; + int cmsg_type; +}; + +struct ucred +{ + pid_t pid; + uid_t uid; + gid_t gid; +}; + +struct linger +{ + int l_onoff; + int l_linger; +}; + +#define SHUT_RD 0 +#define SHUT_WD 1 +#define SHUT_RDWR 2 + +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#define SOCK_RAW 3 +#define SOCK_RDM 4 +#define SOCK_SEQPACKET 5 +#define SOCK_DCCP 6 +#define SOCK_PACKET 10 + +/* linux extensions */ +#define SOCK_CLOEXEC 02000000 +#define SOCK_NONBLOCK 04000 + +#define PF_UNSPEC 0 +#define PF_LOCAL 1 +#define PF_UNIX PF_LOCAL +#define PF_FILE PF_LOCAL +#define PF_INET 2 +#define PF_AX25 3 +#define PF_IPX 4 +#define PF_APPLETALK 5 +#define PF_NETROM 6 +#define PF_BRIDGE 7 +#define PF_ATMPVC 8 +#define PF_X25 9 +#define PF_INET6 10 +#define PF_ROSE 11 +#define PF_DECnet 12 +#define PF_NETBEUI 13 +#define PF_SECURITY 14 +#define PF_KEY 15 +#define PF_NETLINK 16 +#define PF_ROUTE PF_NETLINK +#define PF_PACKET 17 +#define PF_ASH 18 +#define PF_ECONET 19 +#define PF_ATMSVC 20 +#define PF_SNA 22 +#define PF_IRDA 23 +#define PF_PPPOX 24 +#define PF_WANPIPE 25 +#define PF_BLUETOOTH 31 +#define PF_IUCV 32 +#define PF_RXRPC 33 +#define PF_ISDN 34 +#define PF_PHONET 35 +#define PF_IEEE802154 36 +#define PF_CAIF 37 +#define PF_ALG 38 +#define PF_MAX 39 + +#define AF_UNSPEC PF_UNSPEC +#define AF_LOCAL PF_LOCAL +#define AF_UNIX AF_LOCAL +#define AF_FILE AF_LOCAL +#define AF_INET PF_INET +#define AF_AX25 PF_AX25 +#define AF_IPX PF_IPX +#define AF_APPLETALK PF_APPLETALK +#define AF_NETROM PF_NETROM +#define AF_BRIDGE PF_BRIDGE +#define AF_ATMPVC PF_ATMPVC +#define AF_X25 PF_X25 +#define AF_INET6 PF_INET6 +#define AF_ROSE PF_ROSE +#define AF_DECnet PF_DECnet +#define AF_NETBEUI PF_NETBEUI +#define AF_SECURITY PF_SECURITY +#define AF_KEY PF_KEY +#define AF_NETLINK PF_NETLINK +#define AF_ROUTE AF_NETLINK +#define AF_PACKET PF_PACKET +#define AF_ASH PF_ASH +#define AF_ECONET PF_ECONET +#define AF_ATMSVC PF_ATMSVC +#define AF_SNA PF_SNA +#define AF_IRDA PF_IRDA +#define AF_PPPOX PF_PPPOX +#define AF_WANPIPE PF_WANPIPE +#define AF_BLUETOOTH PF_BLUETOOTH +#define AF_IUCV PF_IUCV +#define AF_RXRPC PF_RXRPC +#define AF_ISDN PF_ISDN +#define AF_PHONET PF_PHONET +#define AF_IEEE802154 PF_IEEE802154 +#define AF_CAIF PF_CAIF +#define AF_ALG PF_ALG +#define AF_MAX PF_MAX + +#define SO_DEBUG 1 +#define SO_REUSEADDR 2 +#define SO_TYPE 3 +#define SO_ERROR 4 +#define SO_DONTROUTE 5 +#define SO_BROADCAST 6 +#define SO_SNDBUF 7 +#define SO_RCVBUF 8 +#define SO_KEEPALIVE 9 +#define SO_OOBINLINE 10 +#define SO_NO_CHECK 11 +#define SO_PRIORITY 12 +#define SO_LINGER 13 +#define SO_BSDCOMPAT 14 +#define SO_REUSEPORT 15 +#define SO_PASSCRED 16 +#define SO_PEERCRED 17 +#define SO_RCVLOWAT 18 +#define SO_SNDLOWAT 19 +#define SO_RCVTIMEO 20 +#define SO_SNDTIMEO 21 + +#define SO_SECURITY_AUTHENTICATION 22 +#define SO_SECURITY_ENCRYPTION_TRANSPORT 23 +#define SO_SECURITY_ENCRYPTION_NETWORK 24 + +#define SO_BINDTODEVICE 25 + +#define SO_ATTACH_FILTER 26 +#define SO_DETACH_FILTER 27 + +#define SO_PEERNAME 28 +#define SO_TIMESTAMP 29 +#define SCM_TIMESTAMP SO_TIMESTAMP + +#define SO_ACCEPTCONN 30 + +#define SOL_SOCKET 1 + +/* ??? */ +#define SOL_RAW 255 +#define SOL_DECNET 261 +#define SOL_X25 262 +#define SOL_PACKET 263 +#define SOL_ATM 264 +#define SOL_AAL 265 +#define SOL_IRDA 266 + +#define SOMAXCONN 128 + +#define MSG_OOB 0x0001 +#define MSG_PEEK 0x0002 +#define MSG_DONTROUTE 0x0004 +#define MSG_CTRUNC 0x0008 +#define MSG_PROXY 0x0010 +#define MSG_TRUNC 0x0020 +#define MSG_DONTWAIT 0x0040 +#define MSG_EOR 0x0080 +#define MSG_WAITALL 0x0100 +#define MSG_FIN 0x0200 +#define MSD_SYN 0x0400 +#define MSG_CONFIRM 0x0800 +#define MSG_RST 0x1000 +#define MSG_ERRQUEUE 0x2000 +#define MSG_NOSIGNAL 0x4000 +#define MSG_MORE 0x8000 +#define MSG_WAITFORONE 0x10000 +#define MSG_CMSG_CLOEXEC 0x40000000 + +/* Internal use only!! to make CMSG_NXTHDR definition readable by mortals */ +#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) +#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) +#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) + +#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) +#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) ? (struct cmsghdr *)0 : \ + (__CMSG_NEXT(cmsg) + sizeof (struct cmsghdr) >= __MHDR_END(mhdr) ? (struct cmsghdr *)0 : \ + ((struct cmsghdr *)__CMSG_NEXT(cmsg)))) +#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) + +/* Are these valid? */ +#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) +#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) +#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) + +#define SCM_RIGHTS 0x01 +#define SCM_CREDENTIALS 0x02 diff --git a/05/musl-final/arch/x86_64/bits/stat.h b/05/musl-final/arch/x86_64/bits/stat.h new file mode 100644 index 0000000..64dab33 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/stat.h @@ -0,0 +1,42 @@ +/* copied from kernel definition, but with padding replaced + * by the corresponding correctly-sized userspace types. */ + +struct stat { + unsigned long st_dev; + ino_t st_ino; + nlink_t st_nlink; + + mode_t st_mode; + uid_t st_uid; + gid_t st_gid; + unsigned int __pad0; + dev_t st_rdev; + off_t st_size; + blksize_t st_blksize; + blkcnt_t st_blocks; + + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; + long __unused[3]; +}; + +struct stat64 { + unsigned long st_dev; + ino_t st_ino; + nlink_t st_nlink; + + mode_t st_mode; + uid_t st_uid; + gid_t st_gid; + unsigned int __pad0; + dev_t st_rdev; + off_t st_size; + blksize_t st_blksize; + blkcnt_t st_blocks; + + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; + long __unused[3]; +}; diff --git a/05/musl-final/arch/x86_64/bits/statfs.h b/05/musl-final/arch/x86_64/bits/statfs.h new file mode 100644 index 0000000..51bf8a5 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/statfs.h @@ -0,0 +1,15 @@ +struct statvfs { + unsigned long f_type; + unsigned long f_bsize; + fsblkcnt_t f_blocks; + fsblkcnt_t f_bfree; + fsblkcnt_t f_bavail; + fsfilcnt_t f_files; + fsfilcnt_t f_ffree; + unsigned long f_fsid; + unsigned long f_namemax; + unsigned long f_frsize; + fsfilcnt_t f_favail; + unsigned long f_flag; + unsigned long __reserved[2]; +}; diff --git a/05/musl-final/arch/x86_64/bits/stdarg.h b/05/musl-final/arch/x86_64/bits/stdarg.h new file mode 100644 index 0000000..10ce733 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/stdarg.h @@ -0,0 +1,79 @@ +#ifndef _STDARG_H +#define _STDARG_H + +#ifdef __x86_64__ +#ifndef _WIN64 + +//This should be in sync with the declaration on our lib/libtcc1.c +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; + +typedef __va_list_struct va_list[1]; + +void __va_start(__va_list_struct *ap, void *fp); +void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align); + +#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0)) +#define va_arg(ap, type) \ + (*(type *)(__va_arg(ap, __builtin_va_arg_types(type), sizeof(type), __alignof__(type)))) +#define va_copy(dest, src) (*(dest) = *(src)) +#define va_end(ap) + +/* avoid conflicting definition for va_list on Macs. */ +#define _VA_LIST_T + +#else /* _WIN64 */ +typedef char *va_list; +#define va_start(ap,last) __builtin_va_start(ap,last) +#define va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \ + ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8)) +#define va_copy(dest, src) ((dest) = (src)) +#define va_end(ap) +#endif + +#elif __arm__ +typedef char *va_list; +#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) +#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) + +#elif defined(__aarch64__) +typedef struct { + void *__stack; + void *__gr_top; + void *__vr_top; + int __gr_offs; + int __vr_offs; +} va_list; +#define va_start(ap, last) __va_start(ap, last) +#define va_arg(ap, type) __va_arg(ap, type) +#define va_end(ap) +#define va_copy(dest, src) ((dest) = (src)) + +#else /* __i386__ */ +typedef char *va_list; +/* only correct for i386 */ +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) +#endif + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED + +#endif /* _STDARG_H */ diff --git a/05/musl-final/arch/x86_64/bits/stdint.h b/05/musl-final/arch/x86_64/bits/stdint.h new file mode 100644 index 0000000..878e4de --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/stdint.h @@ -0,0 +1,23 @@ +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST64_MIN INT64_MIN + +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MAX INT32_MAX +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MAX INT64_MAX + +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT32_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +#define INTPTR_MIN INT64_MIN +#define INTPTR_MAX INT64_MAX +#define UINTPTR_MAX UINT64_MAX +#define PTRDIFF_MIN INT64_MIN +#define PTRDIFF_MAX INT64_MAX +#define SIG_ATOMIC_MIN INT64_MIN +#define SIG_ATOMIC_MAX INT64_MAX +#define SIZE_MAX UINT64_MAX diff --git a/05/musl-final/arch/x86_64/bits/sysmacros.h b/05/musl-final/arch/x86_64/bits/sysmacros.h new file mode 100644 index 0000000..da29022 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/sysmacros.h @@ -0,0 +1,7 @@ +#define major(x) (((x) >> 8) & 0xff) +#define minor(x) ((x) & 0xff) +#define makedev(x,y) (((x)<<8)|((y)&0xff)) + +//#define makedev(x,y) \ +// ((x)*0x100000001ULL)&(0xfffffffffff0) +// ((y)*0x1001 & 0xffff0ff) diff --git a/05/musl-final/arch/x86_64/bits/tcp.h b/05/musl-final/arch/x86_64/bits/tcp.h new file mode 100644 index 0000000..923231b --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/tcp.h @@ -0,0 +1 @@ +#define TCP_NODELAY 1 diff --git a/05/musl-final/arch/x86_64/bits/termios.h b/05/musl-final/arch/x86_64/bits/termios.h new file mode 100644 index 0000000..316baeb --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/termios.h @@ -0,0 +1,158 @@ +struct termios +{ + tcflag_t c_iflag; + tcflag_t c_oflag; + tcflag_t c_cflag; + tcflag_t c_lflag; + cc_t c_line; + cc_t c_cc[NCCS]; + speed_t __c_ispeed; + speed_t __c_ospeed; +}; + +#define VINTR 0 +#define VQUIT 1 +#define VERASE 2 +#define VKILL 3 +#define VEOF 4 +#define VTIME 5 +#define VMIN 6 +#define VSWTC 7 +#define VSTART 8 +#define VSTOP 9 +#define VSUSP 10 +#define VEOL 11 +#define VREPRINT 12 +#define VDISCARD 13 +#define VWERASE 14 +#define VLNEXT 15 +#define VEOL2 16 + +#define IGNBRK 0000001 +#define BRKINT 0000002 +#define IGNPAR 0000004 +#define PARMRK 0000010 +#define INPCK 0000020 +#define ISTRIP 0000040 +#define INLCR 0000100 +#define IGNCR 0000200 +#define ICRNL 0000400 +#define IUCLC 0001000 +#define IXON 0002000 +#define IXANY 0004000 +#define IXOFF 0010000 +#define IMAXBEL 0020000 + +#define OPOST 0000001 +#define OLCUC 0000002 +#define ONLCR 0000004 +#define OCRNL 0000010 +#define ONOCR 0000020 +#define ONLRET 0000040 +#define OFILL 0000100 +#define OFDEL 0000200 +#define NLDLY 0000400 +#define NL0 0000000 +#define NL1 0000400 +#define CRDLY 0003000 +#define CR0 0000000 +#define CR1 0001000 +#define CR2 0002000 +#define CR3 0003000 +#define TABDLY 0014000 +#define TAB0 0000000 +#define TAB1 0004000 +#define TAB2 0010000 +#define TAB3 0014000 +#define BSDLY 0020000 +#define BS0 0000000 +#define BS1 0020000 +#define FFDLY 0100000 +#define FF0 0000000 +#define FF1 0100000 + +#define VTDLY 0040000 +#define VT0 0000000 +#define VT1 0040000 + +/* ?? */ +#define XTABS 0014000 + +#define B0 0000000 +#define B50 0000001 +#define B75 0000002 +#define B110 0000003 +#define B134 0000004 +#define B150 0000005 +#define B200 0000006 +#define B300 0000007 +#define B600 0000010 +#define B1200 0000011 +#define B1800 0000012 +#define B2400 0000013 +#define B4800 0000014 +#define B9600 0000015 +#define B19200 0000016 +#define B38400 0000017 + +#define B57600 0010001 +#define B115200 0010002 +#define B230400 0010003 +#define B460800 0010004 +#define B500000 0010005 +#define B576000 0010006 +#define B921600 0010007 +#define B1000000 0010010 +#define B1152000 0010011 +#define B1500000 0010012 +#define B2000000 0010013 +#define B2500000 0010014 +#define B3000000 0010015 +#define B3500000 0010016 +#define B4000000 0010017 + +#define CBAUD 0010017 + +#define CSIZE 0000060 +#define CS5 0000000 +#define CS6 0000020 +#define CS7 0000040 +#define CS8 0000060 +#define CSTOPB 0000100 +#define CREAD 0000200 +#define PARENB 0000400 +#define PARODD 0001000 +#define HUPCL 0002000 +#define CLOCAL 0004000 + +#define CRTSCTS 020000000000 + +#define ISIG 0000001 +#define ICANON 0000002 +#define ECHO 0000010 +#define ECHOE 0000020 +#define ECHOK 0000040 +#define ECHONL 0000100 +#define NOFLSH 0000200 +#define TOSTOP 0000400 +#define IEXTEN 0100000 + +/* Extensions? */ +#define ECHOCTL 0001000 +#define ECHOPRT 0002000 +#define ECHOKE 0004000 +#define FLUSHO 0010000 +#define PENDIN 0040000 + +#define TCOOFF 0 +#define TCOON 1 +#define TCIOFF 2 +#define TCION 3 + +#define TCIFLUSH 0 +#define TCOFLUSH 1 +#define TCIOFLUSH 2 + +#define TCSANOW 0 +#define TCSADRAIN 1 +#define TCSAFLUSH 2 diff --git a/05/musl-final/arch/x86_64/bits/user.h b/05/musl-final/arch/x86_64/bits/user.h new file mode 100644 index 0000000..13ff027 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/user.h @@ -0,0 +1,77 @@ +#undef __WORDSIZE +#define __WORDSIZE 32 + +struct user_fpregs_struct +{ + long int cwd; + long int swd; + long int twd; + long int fip; + long int fcs; + long int foo; + long int fos; + long int st_space[20]; +}; + +struct user_fpxregs_struct +{ + unsigned short int cwd; + unsigned short int swd; + unsigned short int twd; + unsigned short int fop; + long int fip; + long int fcs; + long int foo; + long int fos; + long int mxcsr; + long int reserved; + long int st_space[32]; + long int xmm_space[32]; + long int padding[56]; +}; + +struct user_regs_struct +{ + long int ebx; + long int ecx; + long int edx; + long int esi; + long int edi; + long int ebp; + long int eax; + long int xds; + long int xes; + long int xfs; + long int xgs; + long int orig_eax; + long int eip; + long int xcs; + long int eflags; + long int esp; + long int xss; +}; + +struct user +{ + struct user_regs_struct regs; + int u_fpvalid; + struct user_fpregs_struct i387; + unsigned long int u_tsize; + unsigned long int u_dsize; + unsigned long int u_ssize; + unsigned long start_code; + unsigned long start_stack; + long int signal; + int reserved; + struct user_regs_struct *u_ar0; + struct user_fpregs_struct *u_fpstate; + unsigned long int magic; + char u_comm[32]; + int u_debugreg[8]; +}; + +#define PAGE_MASK (~(PAGE_SIZE-1)) +#define NBPG PAGE_SIZE +#define UPAGES 1 +#define HOST_TEXT_START_ADDR (u.start_code) +#define HOST_STACK_END_ADDR (u.start_stack + u.u_ssize * NBPG) diff --git a/05/musl-final/arch/x86_64/bits/wait.h b/05/musl-final/arch/x86_64/bits/wait.h new file mode 100644 index 0000000..ae3f101 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/wait.h @@ -0,0 +1,13 @@ +#define WNOHANG 1 +#define WUNTRACED 2 + +#define WSTOPPED 2 +#define WEXITED 4 +#define WCONTINUED 8 +#define WNOWAIT 0x1000000 + +#if defined(__XOPEN_SOURCE) || defined(__GNU_SOURCE) +#define P_ALL 0 +#define P_PID 1 +#define P_PGID 2 +#endif diff --git a/05/musl-final/arch/x86_64/bits/wexitstatus.h b/05/musl-final/arch/x86_64/bits/wexitstatus.h new file mode 100644 index 0000000..34a8023 --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/wexitstatus.h @@ -0,0 +1,9 @@ +#ifndef WEXITSTATUS +#define WEXITSTATUS(s) (((s) & 0xff00) >> 8) +#define WTERMSIG(s) ((s) & 0x7f) +#define WSTOPSIG(s) WEXITSTATUS(s) +#define WCOREDUMP(s) ((s) & 0x80) +#define WIFEXITED(s) (!WTERMSIG(s)) +#define WIFSTOPPED(s) (((s) & 0xff) == 0x7f) +#define WIFSIGNALED(s) (!WIFSTOPPED(s) && !WIFEXITED(s)) +#endif diff --git a/05/musl-final/arch/x86_64/pthread_arch.h b/05/musl-final/arch/x86_64/pthread_arch.h new file mode 100644 index 0000000..40d2679 --- /dev/null +++ b/05/musl-final/arch/x86_64/pthread_arch.h @@ -0,0 +1,6 @@ +static inline struct pthread *__pthread_self() +{ + struct pthread *self; + __asm__ ("movq %%fs:0,%0" : "=r" (self) ); + return self; +} diff --git a/05/musl-final/arch/x86_64/syscall.h b/05/musl-final/arch/x86_64/syscall.h new file mode 100644 index 0000000..acebd76 --- /dev/null +++ b/05/musl-final/arch/x86_64/syscall.h @@ -0,0 +1,433 @@ +#ifndef _SYSCALL_H +#define _SYSCALL_H + +#define SYSCALL_LL(x) x, 0 +#define SYSCALL_SIGSET_SIZE 8 + +#if defined(SYSCALL_NORETURN) +static inline long __syscall_ret(unsigned long r) +{ + for(;;); + return 0; +} +#elif defined(SYSCALL_RETURN_ERRNO) +static inline long __syscall_ret(unsigned long r) +{ + return -r; +} +#else +extern long __syscall_ret(unsigned long); +#endif + +// 64: di, si, dx, r10, r8, r9 +// 32: ebx, ecx, edx, esi, edi, ebp + +#define SYSCALL "syscall" + +#if 0 +static inline long syscall0(long n) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n) : "rcx", "r11", "memory"); + return __syscall_ret(ret); +} + +static inline long syscall1(long n, long a1) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1) : "rcx", "r11", "memory"); + return __syscall_ret(ret); +} + +static inline long syscall2(long n, long a1, long a2) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2) + : "rcx", "r11", "memory"); + return __syscall_ret(ret); +} + +static inline long syscall3(long n, long a1, long a2, long a3) +{ + unsigned long ret; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), + "d"(a3) : "rcx", "r11", "memory"); + return __syscall_ret(ret); +} + +static inline long syscall4(long n, long a1, long a2, long a3, long a4) +{ + unsigned long ret; + register long r10 __asm__("r10") = a4; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), + "d"(a3), "r"(r10): "rcx", "r11", "memory"); + return __syscall_ret(ret); +} + +static inline long syscall5(long n, long a1, long a2, long a3, long a4, + long a5) +{ + unsigned long ret; + register long r10 __asm__("r10") = a4; + register long r8 __asm__("r8") = a5; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), + "d"(a3), "r"(r10), "r"(r8) : "rcx", "r11", "memory"); + return __syscall_ret(ret); +} + + +static inline long syscall6(long n, long a1, long a2, long a3, long a4, + long a5, long a6) +{ + unsigned long ret; + register long r10 __asm__("r10") = a4; + register long r8 __asm__("r8") = a5; + register long r9 __asm__("r9") = a6; + __asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), "D"(a1), "S"(a2), + "d"(a3), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory"); + return __syscall_ret(ret); +} +#else +extern long syscall0(long); +extern long syscall1(long, long); +extern long syscall2(long, long, long); +extern long syscall3(long, long, long, long); +extern long syscall4(long, long, long, long, long); +extern long syscall5(long, long, long, long, long, long); +extern long syscall6(long, long, long, long, long, long, long); +#endif + +#define __NR_read 0 +#define __NR_write 1 +#define __NR_open 2 +#define __NR_close 3 +#define __NR_stat 4 +#define __NR_fstat 5 +#define __NR_lstat 6 +#define __NR_poll 7 +#define __NR_lseek 8 +#define __NR_mmap 9 +#define __NR_mprotect 10 +#define __NR_munmap 11 +#define __NR_brk 12 +#define __NR_rt_sigaction 13 +#define __NR_rt_sigprocmask 14 +#define __NR_rt_sigreturn 15 +#define __NR_ioctl 16 +#define __NR_pread64 17 +#define __NR_pwrite64 18 +#define __NR_readv 19 +#define __NR_writev 20 +#define __NR_access 21 +#define __NR_pipe 22 +#define __NR_select 23 +#define __NR_sched_yield 24 +#define __NR_mremap 25 +#define __NR_msync 26 +#define __NR_mincore 27 +#define __NR_madvise 28 +#define __NR_shmget 29 +#define __NR_shmat 30 +#define __NR_shmctl 31 +#define __NR_dup 32 +#define __NR_dup2 33 +#define __NR_pause 34 +#define __NR_nanosleep 35 +#define __NR_getitimer 36 +#define __NR_alarm 37 +#define __NR_setitimer 38 +#define __NR_getpid 39 +#define __NR_sendfile 40 +#define __NR_socket 41 +#define __NR_connect 42 +#define __NR_accept 43 +#define __NR_sendto 44 +#define __NR_recvfrom 45 +#define __NR_sendmsg 46 +#define __NR_recvmsg 47 +#define __NR_shutdown 48 +#define __NR_bind 49 +#define __NR_listen 50 +#define __NR_getsockname 51 +#define __NR_getpeername 52 +#define __NR_socketpair 53 +#define __NR_setsockopt 54 +#define __NR_getsockopt 55 +#define __NR_clone 56 +#define __NR_fork 57 +#define __NR_vfork 58 +#define __NR_execve 59 +#define __NR_exit 60 +#define __NR_wait4 61 +#define __NR_kill 62 +#define __NR_uname 63 +#define __NR_semget 64 +#define __NR_semop 65 +#define __NR_semctl 66 +#define __NR_shmdt 67 +#define __NR_msgget 68 +#define __NR_msgsnd 69 +#define __NR_msgrcv 70 +#define __NR_msgctl 71 +#define __NR_fcntl 72 +#define __NR_flock 73 +#define __NR_fsync 74 +#define __NR_fdatasync 75 +#define __NR_truncate 76 +#define __NR_ftruncate 77 +#define __NR_getdents 78 +#define __NR_getcwd 79 +#define __NR_chdir 80 +#define __NR_fchdir 81 +#define __NR_rename 82 +#define __NR_mkdir 83 +#define __NR_rmdir 84 +#define __NR_creat 85 +#define __NR_link 86 +#define __NR_unlink 87 +#define __NR_symlink 88 +#define __NR_readlink 89 +#define __NR_chmod 90 +#define __NR_fchmod 91 +#define __NR_chown 92 +#define __NR_fchown 93 +#define __NR_lchown 94 +#define __NR_umask 95 +#define __NR_gettimeofday 96 +#define __NR_getrlimit 97 +#define __NR_getrusage 98 +#define __NR_sysinfo 99 +#define __NR_times 100 +#define __NR_ptrace 101 +#define __NR_getuid 102 +#define __NR_syslog 103 +#define __NR_getgid 104 +#define __NR_setuid 105 +#define __NR_setgid 106 +#define __NR_geteuid 107 +#define __NR_getegid 108 +#define __NR_setpgid 109 +#define __NR_getppid 110 +#define __NR_getpgrp 111 +#define __NR_setsid 112 +#define __NR_setreuid 113 +#define __NR_setregid 114 +#define __NR_getgroups 115 +#define __NR_setgroups 116 +#define __NR_setresuid 117 +#define __NR_getresuid 118 +#define __NR_setresgid 119 +#define __NR_getresgid 120 +#define __NR_getpgid 121 +#define __NR_setfsuid 122 +#define __NR_setfsgid 123 +#define __NR_getsid 124 +#define __NR_capget 125 +#define __NR_capset 126 +#define __NR_rt_sigpending 127 +#define __NR_rt_sigtimedwait 128 +#define __NR_rt_sigqueueinfo 129 +#define __NR_rt_sigsuspend 130 +#define __NR_sigaltstack 131 +#define __NR_utime 132 +#define __NR_mknod 133 +#define __NR_uselib 134 +#define __NR_personality 135 +#define __NR_ustat 136 +#define __NR_statfs 137 +#define __NR_fstatfs 138 +#define __NR_sysfs 139 +#define __NR_getpriority 140 +#define __NR_setpriority 141 +#define __NR_sched_setparam 142 +#define __NR_sched_getparam 143 +#define __NR_sched_setscheduler 144 +#define __NR_sched_getscheduler 145 +#define __NR_sched_get_priority_max 146 +#define __NR_sched_get_priority_min 147 +#define __NR_sched_rr_get_interval 148 +#define __NR_mlock 149 +#define __NR_munlock 150 +#define __NR_mlockall 151 +#define __NR_munlockall 152 +#define __NR_vhangup 153 +#define __NR_modify_ldt 154 +#define __NR_pivot_root 155 +#define __NR__sysctl 156 +#define __NR_prctl 157 +#define __NR_arch_prctl 158 +#define __NR_adjtimex 159 +#define __NR_setrlimit 160 +#define __NR_chroot 161 +#define __NR_sync 162 +#define __NR_acct 163 +#define __NR_settimeofday 164 +#define __NR_mount 165 +#define __NR_umount2 166 +#define __NR_swapon 167 +#define __NR_swapoff 168 +#define __NR_reboot 169 +#define __NR_sethostname 170 +#define __NR_setdomainname 171 +#define __NR_iopl 172 +#define __NR_ioperm 173 +#define __NR_create_module 174 +#define __NR_init_module 175 +#define __NR_delete_module 176 +#define __NR_get_kernel_syms 177 +#define __NR_query_module 178 +#define __NR_quotactl 179 +#define __NR_nfsservctl 180 +#define __NR_getpmsg 181 +#define __NR_putpmsg 182 +#define __NR_afs_syscall 183 +#define __NR_tuxcall 184 +#define __NR_security 185 +#define __NR_gettid 186 +#define __NR_readahead 187 +#define __NR_setxattr 188 +#define __NR_lsetxattr 189 +#define __NR_fsetxattr 190 +#define __NR_getxattr 191 +#define __NR_lgetxattr 192 +#define __NR_fgetxattr 193 +#define __NR_listxattr 194 +#define __NR_llistxattr 195 +#define __NR_flistxattr 196 +#define __NR_removexattr 197 +#define __NR_lremovexattr 198 +#define __NR_fremovexattr 199 +#define __NR_tkill 200 +#define __NR_time 201 +#define __NR_futex 202 +#define __NR_sched_setaffinity 203 +#define __NR_sched_getaffinity 204 +#define __NR_set_thread_area 205 +#define __NR_io_setup 206 +#define __NR_io_destroy 207 +#define __NR_io_getevents 208 +#define __NR_io_submit 209 +#define __NR_io_cancel 210 +#define __NR_get_thread_area 211 +#define __NR_lookup_dcookie 212 +#define __NR_epoll_create 213 +#define __NR_epoll_ctl_old 214 +#define __NR_epoll_wait_old 215 +#define __NR_remap_file_pages 216 +#define __NR_getdents64 217 +#define __NR_set_tid_address 218 +#define __NR_restart_syscall 219 +#define __NR_semtimedop 220 +#define __NR_fadvise64 221 +#define __NR_timer_create 222 +#define __NR_timer_settime 223 +#define __NR_timer_gettime 224 +#define __NR_timer_getoverrun 225 +#define __NR_timer_delete 226 +#define __NR_clock_settime 227 +#define __NR_clock_gettime 228 +#define __NR_clock_getres 229 +#define __NR_clock_nanosleep 230 +#define __NR_exit_group 231 +#define __NR_epoll_wait 232 +#define __NR_epoll_ctl 233 +#define __NR_tgkill 234 +#define __NR_utimes 235 +#define __NR_vserver 236 +#define __NR_mbind 237 +#define __NR_set_mempolicy 238 +#define __NR_get_mempolicy 239 +#define __NR_mq_open 240 +#define __NR_mq_unlink 241 +#define __NR_mq_timedsend 242 +#define __NR_mq_timedreceive 243 +#define __NR_mq_notify 244 +#define __NR_mq_getsetattr 245 +#define __NR_kexec_load 246 +#define __NR_waitid 247 +#define __NR_add_key 248 +#define __NR_request_key 249 +#define __NR_keyctl 250 +#define __NR_ioprio_set 251 +#define __NR_ioprio_get 252 +#define __NR_inotify_init 253 +#define __NR_inotify_add_watch 254 +#define __NR_inotify_rm_watch 255 +#define __NR_migrate_pages 256 +#define __NR_openat 257 +#define __NR_mkdirat 258 +#define __NR_mknodat 259 +#define __NR_fchownat 260 +#define __NR_futimesat 261 +#define __NR_newfstatat 262 +#define __NR_unlinkat 263 +#define __NR_renameat 264 +#define __NR_linkat 265 +#define __NR_symlinkat 266 +#define __NR_readlinkat 267 +#define __NR_fchmodat 268 +#define __NR_faccessat 269 +#define __NR_pselect6 270 +#define __NR_ppoll 271 +#define __NR_unshare 272 +#define __NR_set_robust_list 273 +#define __NR_get_robust_list 274 +#define __NR_splice 275 +#define __NR_tee 276 +#define __NR_sync_file_range 277 +#define __NR_vmsplice 278 +#define __NR_move_pages 279 +#define __NR_utimensat 280 +#define __NR_epoll_pwait 281 +#define __NR_signalfd 282 +#define __NR_timerfd_create 283 +#define __NR_eventfd 284 +#define __NR_fallocate 285 +#define __NR_timerfd_settime 286 +#define __NR_timerfd_gettime 287 +#define __NR_accept4 288 +#define __NR_signalfd4 289 +#define __NR_eventfd2 290 +#define __NR_epoll_create1 291 +#define __NR_dup3 292 +#define __NR_pipe2 293 +#define __NR_inotify_init1 294 +#define __NR_preadv 295 +#define __NR_pwritev 296 +#define __NR_rt_tgsigqueueinfo 297 +#define __NR_perf_event_open 298 +#define __NR_recvmmsg 299 +#define __NR_fanotify_init 300 +#define __NR_fanotify_mark 301 +#define __NR_prlimit64 302 + +#undef __NR_fstatat +#undef __NR_pread +#undef __NR_pwrite +#undef __NR_getdents +#define __NR_fstatat __NR_newfstatat +#define __NR_pread __NR_pread64 +#define __NR_pwrite __NR_pwrite64 +#define __NR_getdents __NR_getdents64 + +#undef O_LARGEFILE +#define O_LARGEFILE 0100000 + +#define socketcall(nm, a, b, c, d, e, f) syscall6(__NR_##nm, \ + (long)a, (long)b, (long)c, (long)d, (long)e, (long)f) + +/* the following are needed for iso c functions to use */ +#define __syscall_open(filename, flags, mode) syscall3(__NR_open, (long)(filename), (flags)|O_LARGEFILE, (mode)) +#define __syscall_read(fd, buf, len) syscall3(__NR_read, (fd), (long)(buf), (len)) +#define __syscall_write(fd, buf, len) syscall3(__NR_write, (fd), (long)(buf), (len)) +#define __syscall_close(fd) syscall1(__NR_close, (fd)) +#define __syscall_fcntl(fd, cmd, arg) syscall3(__NR_fcntl, (fd), (cmd), (long)(arg)) +#define __syscall_dup2(old, new) syscall2(__NR_dup2, (old), (new)) +#define __syscall_unlink(path) syscall1(__NR_unlink, (long)(path)) +#define __syscall_getpid() syscall0(__NR_getpid) +#define __syscall_kill(pid,sig) syscall2(__NR_kill, (pid), (sig)) +#define __syscall_sigaction(sig,new,old) syscall4(__NR_rt_sigaction, (sig), (long)(new), (long)(old), SYSCALL_SIGSET_SIZE) +#define __syscall_ioctl(fd,ioc,arg) syscall3(__NR_ioctl, (fd), (ioc), (long)(arg)) +#define __syscall_exit(code) syscall1(__NR_exit, code) + +#endif diff --git a/05/musl-final/config.mak b/05/musl-final/config.mak new file mode 100644 index 0000000..16a8e73 --- /dev/null +++ b/05/musl-final/config.mak @@ -0,0 +1,27 @@ +# +# musl config.mak template (original in dist/config.mak) +# + +# Target CPU architecture. Supported values: i386, x86_64 +ARCH = x86_64 + +# Installation prefix. DO NOT use /, /usr, or /usr/local ! +prefix = ../musl-bootstrap-final + +# Installation prefix for musl-gcc compiler wrapper. +exec_prefix = ../musl-bootstrap-final + +# Uncomment if you want to build i386 musl on a 64-bit host +#CFLAGS += -m32 + +CFLAGS += -g + +# Uncomment for smaller code size. +#CFLAGS += -fomit-frame-pointer -mno-accumulate-outgoing-args + +# Uncomment for warnings (as errors). Might need tuning to your gcc version. +#CFLAGS += -Werror -Wall -Wpointer-arith -Wcast-align -Wno-parentheses -Wno-char-subscripts -Wno-uninitialized -Wno-sequence-point -Wno-missing-braces -Wno-unused-value +#CFLAGS += -Wno-pointer-sign + +# Uncomment if you want to build a shared library (experimental). +#LIBC_LIBS += lib/libc.so diff --git a/05/musl-final/crt/crt1.c b/05/musl-final/crt/crt1.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/crt/crti.c b/05/musl-final/crt/crti.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/crt/crtn.c b/05/musl-final/crt/crtn.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/crt/i386/crt1.s b/05/musl-final/crt/i386/crt1.s new file mode 100644 index 0000000..050adec --- /dev/null +++ b/05/musl-final/crt/i386/crt1.s @@ -0,0 +1,17 @@ +.text +.global _start +_start: + xorl %ebp,%ebp + popl %ecx + movl %esp,%eax + andl $-16,%esp + pushl %esp + pushl %esp + pushl %edx + pushl $0 + pushl $0 + pushl %eax + pushl %ecx + pushl $main + call __libc_start_main +.L0: jmp .L0 diff --git a/05/musl-final/crt/x86_64/crt1.s b/05/musl-final/crt/x86_64/crt1.s new file mode 100644 index 0000000..45cbb9d --- /dev/null +++ b/05/musl-final/crt/x86_64/crt1.s @@ -0,0 +1,16 @@ +/* Written 2011 Nicholas J. Kain, released as Public Domain */ +.text +.global _start +_start: + xor %rbp,%rbp /* rbp:undefined -> mark as zero 0 (ABI) */ + mov %rdx,%r9 /* 6th arg: ptr to register with atexit() */ + pop %rsi /* 2nd arg: argc */ + mov %rsp,%rdx /* 3rd arg: argv */ + andq $-16,%rsp /* align stack pointer */ + push %rax /* 8th arg: glibc ABI compatible */ + push %rsp /* 7th arg: glibc ABI compatible */ + xor %r8,%r8 /* 5th arg: always 0 */ + xor %rcx,%rcx /* 4th arg: always 0 */ + mov $main,%rdi /* 1st arg: application entry ip */ + call __libc_start_main /* musl init will run the program */ +.L0: jmp .L0 diff --git a/05/musl-final/dist/config.mak b/05/musl-final/dist/config.mak new file mode 100644 index 0000000..452a93a --- /dev/null +++ b/05/musl-final/dist/config.mak @@ -0,0 +1,25 @@ +# +# musl config.mak template (original in dist/config.mak) +# + +# Target CPU architecture. Supported values: i386, x86_64 +ARCH = i386 + +# Installation prefix. DO NOT use /, /usr, or /usr/local ! +prefix = /usr/local/musl + +# Installation prefix for musl-gcc compiler wrapper. +exec_prefix = /usr/local + +# Uncomment if you want to build i386 musl on a 64-bit host +#CFLAGS += -m32 + +# Uncomment for smaller code size. +#CFLAGS += -fomit-frame-pointer -mno-accumulate-outgoing-args + +# Uncomment for warnings (as errors). Might need tuning to your gcc version. +#CFLAGS += -Werror -Wall -Wpointer-arith -Wcast-align -Wno-parentheses -Wno-char-subscripts -Wno-uninitialized -Wno-sequence-point -Wno-missing-braces -Wno-unused-value +#CFLAGS += -Wno-pointer-sign + +# Uncomment if you want to build a shared library (experimental). +#LIBC_LIBS += lib/libc.so diff --git a/05/musl-final/include/alloca.h b/05/musl-final/include/alloca.h new file mode 100644 index 0000000..ac78e46 --- /dev/null +++ b/05/musl-final/include/alloca.h @@ -0,0 +1,9 @@ +#ifndef _ALLOCA_H +#define _ALLOCA_H + +#define __NEED_size_t +#include + +void *alloca(size_t); + +#endif diff --git a/05/musl-final/include/arpa/inet.h b/05/musl-final/include/arpa/inet.h new file mode 100644 index 0000000..9342713 --- /dev/null +++ b/05/musl-final/include/arpa/inet.h @@ -0,0 +1,33 @@ +#ifndef _ARPA_INET_H +#define _ARPA_INET_H + +#include +#include + +#define __NEED_socklen_t +#define __NEED_in_addr_t +#define __NEED_in_port_t +#define __NEED_uint16_t +#define __NEED_uint32_t +#define __NEED_struct_in_addr + +#include + +uint32_t htonl(uint32_t); +uint16_t htons(uint16_t); +uint32_t ntohl(uint32_t); +uint16_t ntohs(uint16_t); + +in_addr_t inet_addr (const char *); +char *inet_ntoa (struct in_addr); +int inet_pton (int, const char *, void *); +const char *inet_ntop (int, const void *, char *, socklen_t); + +int inet_aton (const char *, struct in_addr *); /* nonstandard but widely used */ + +#undef INET_ADDRSTRLEN +#undef INET6_ADDRSTRLEN +#define INET_ADDRSTRLEN 16 +#define INET6_ADDRSTRLEN 46 + +#endif diff --git a/05/musl-final/include/arpa/nameser.h b/05/musl-final/include/arpa/nameser.h new file mode 100644 index 0000000..0f85460 --- /dev/null +++ b/05/musl-final/include/arpa/nameser.h @@ -0,0 +1,465 @@ +#ifndef _ARPA_NAMESER_H_ +#define _ARPA_NAMESER_H_ + +#define __NAMESER 19991006 +#define NS_PACKETSZ 512 +#define NS_MAXDNAME 1025 +#define NS_MAXMSG 65535 +#define NS_MAXCDNAME 255 +#define NS_MAXLABEL 63 +#define NS_HFIXEDSZ 12 +#define NS_QFIXEDSZ 4 +#define NS_RRFIXEDSZ 10 +#define NS_INT32SZ 4 +#define NS_INT16SZ 2 +#define NS_INT8SZ 1 +#define NS_INADDRSZ 4 +#define NS_IN6ADDRSZ 16 +#define NS_CMPRSFLGS 0xc0 +#define NS_DEFAULTPORT 53 + +typedef enum __ns_sect { + ns_s_qd = 0, + ns_s_zn = 0, + ns_s_an = 1, + ns_s_pr = 1, + ns_s_ns = 2, + ns_s_ud = 2, + ns_s_ar = 3, + ns_s_max = 4 +} ns_sect; + +typedef struct __ns_msg { + const unsigned char *_msg, *_eom; + uint16_t _id, _flags, _counts[ns_s_max]; + const unsigned char *_sections[ns_s_max]; + ns_sect _sect; + int _rrnum; + const unsigned char *_msg_ptr; +} ns_msg; + +struct _ns_flagdata { int mask, shift; }; +extern const struct _ns_flagdata _ns_flagdata[]; + +#define ns_msg_id(handle) ((handle)._id + 0) +#define ns_msg_base(handle) ((handle)._msg + 0) +#define ns_msg_end(handle) ((handle)._eom + 0) +#define ns_msg_size(handle) ((handle)._eom - (handle)._msg) +#define ns_msg_count(handle, section) ((handle)._counts[section] + 0) + +typedef struct __ns_rr { + char name[NS_MAXDNAME]; + uint16_t type; + uint16_t rr_class; + uint32_t ttl; + uint16_t rdlength; + const unsigned char *rdata; +} ns_rr; + +#define ns_rr_name(rr) (((rr).name[0] != '\0') ? (rr).name : ".") +#define ns_rr_type(rr) ((ns_type)((rr).type + 0)) +#define ns_rr_class(rr) ((ns_class)((rr).rr_class + 0)) +#define ns_rr_ttl(rr) ((rr).ttl + 0) +#define ns_rr_rdlen(rr) ((rr).rdlength + 0) +#define ns_rr_rdata(rr) ((rr).rdata + 0) + +typedef enum __ns_flag { + ns_f_qr, + ns_f_opcode, + ns_f_aa, + ns_f_tc, + ns_f_rd, + ns_f_ra, + ns_f_z, + ns_f_ad, + ns_f_cd, + ns_f_rcode, + ns_f_max +} ns_flag; + +typedef enum __ns_opcode { + ns_o_query = 0, + ns_o_iquery = 1, + ns_o_status = 2, + ns_o_notify = 4, + ns_o_update = 5, + ns_o_max = 6 +} ns_opcode; + +typedef enum __ns_rcode { + ns_r_noerror = 0, + ns_r_formerr = 1, + ns_r_servfail = 2, + ns_r_nxdomain = 3, + ns_r_notimpl = 4, + ns_r_refused = 5, + ns_r_yxdomain = 6, + ns_r_yxrrset = 7, + ns_r_nxrrset = 8, + ns_r_notauth = 9, + ns_r_notzone = 10, + ns_r_max = 11, + ns_r_badvers = 16, + ns_r_badsig = 16, + ns_r_badkey = 17, + ns_r_badtime = 18 +} ns_rcode; + +typedef enum __ns_update_operation { + ns_uop_delete = 0, + ns_uop_add = 1, + ns_uop_max = 2 +} ns_update_operation; + +struct ns_tsig_key { + char name[NS_MAXDNAME], alg[NS_MAXDNAME]; + unsigned char *data; + int len; +}; +typedef struct ns_tsig_key ns_tsig_key; + +struct ns_tcp_tsig_state { + int counter; + struct dst_key *key; + void *ctx; + unsigned char sig[NS_PACKETSZ]; + int siglen; +}; +typedef struct ns_tcp_tsig_state ns_tcp_tsig_state; + +#define NS_TSIG_FUDGE 300 +#define NS_TSIG_TCP_COUNT 100 +#define NS_TSIG_ALG_HMAC_MD5 "HMAC-MD5.SIG-ALG.REG.INT" + +#define NS_TSIG_ERROR_NO_TSIG -10 +#define NS_TSIG_ERROR_NO_SPACE -11 +#define NS_TSIG_ERROR_FORMERR -12 + +typedef enum __ns_type { + ns_t_invalid = 0, + ns_t_a = 1, + ns_t_ns = 2, + ns_t_md = 3, + ns_t_mf = 4, + ns_t_cname = 5, + ns_t_soa = 6, + ns_t_mb = 7, + ns_t_mg = 8, + ns_t_mr = 9, + ns_t_null = 10, + ns_t_wks = 11, + ns_t_ptr = 12, + ns_t_hinfo = 13, + ns_t_minfo = 14, + ns_t_mx = 15, + ns_t_txt = 16, + ns_t_rp = 17, + ns_t_afsdb = 18, + ns_t_x25 = 19, + ns_t_isdn = 20, + ns_t_rt = 21, + ns_t_nsap = 22, + ns_t_nsap_ptr = 23, + ns_t_sig = 24, + ns_t_key = 25, + ns_t_px = 26, + ns_t_gpos = 27, + ns_t_aaaa = 28, + ns_t_loc = 29, + ns_t_nxt = 30, + ns_t_eid = 31, + ns_t_nimloc = 32, + ns_t_srv = 33, + ns_t_atma = 34, + ns_t_naptr = 35, + ns_t_kx = 36, + ns_t_cert = 37, + ns_t_a6 = 38, + ns_t_dname = 39, + ns_t_sink = 40, + ns_t_opt = 41, + ns_t_apl = 42, + ns_t_tkey = 249, + ns_t_tsig = 250, + ns_t_ixfr = 251, + ns_t_axfr = 252, + ns_t_mailb = 253, + ns_t_maila = 254, + ns_t_any = 255, + ns_t_zxfr = 256, + ns_t_max = 65536 +} ns_type; + +#define ns_t_qt_p(t) (ns_t_xfr_p(t) || (t) == ns_t_any || \ + (t) == ns_t_mailb || (t) == ns_t_maila) +#define ns_t_mrr_p(t) ((t) == ns_t_tsig || (t) == ns_t_opt) +#define ns_t_rr_p(t) (!ns_t_qt_p(t) && !ns_t_mrr_p(t)) +#define ns_t_udp_p(t) ((t) != ns_t_axfr && (t) != ns_t_zxfr) +#define ns_t_xfr_p(t) ((t) == ns_t_axfr || (t) == ns_t_ixfr || \ + (t) == ns_t_zxfr) + +typedef enum __ns_class { + ns_c_invalid = 0, + ns_c_in = 1, + ns_c_2 = 2, + ns_c_chaos = 3, + ns_c_hs = 4, + ns_c_none = 254, + ns_c_any = 255, + ns_c_max = 65536 +} ns_class; + +typedef enum __ns_key_types { + ns_kt_rsa = 1, + ns_kt_dh = 2, + ns_kt_dsa = 3, + ns_kt_private = 254 +} ns_key_types; + +typedef enum __ns_cert_types { + cert_t_pkix = 1, + cert_t_spki = 2, + cert_t_pgp = 3, + cert_t_url = 253, + cert_t_oid = 254 +} ns_cert_types; + +#define NS_KEY_TYPEMASK 0xC000 +#define NS_KEY_TYPE_AUTH_CONF 0x0000 +#define NS_KEY_TYPE_CONF_ONLY 0x8000 +#define NS_KEY_TYPE_AUTH_ONLY 0x4000 +#define NS_KEY_TYPE_NO_KEY 0xC000 +#define NS_KEY_NO_AUTH 0x8000 +#define NS_KEY_NO_CONF 0x4000 +#define NS_KEY_RESERVED2 0x2000 +#define NS_KEY_EXTENDED_FLAGS 0x1000 +#define NS_KEY_RESERVED4 0x0800 +#define NS_KEY_RESERVED5 0x0400 +#define NS_KEY_NAME_TYPE 0x0300 +#define NS_KEY_NAME_USER 0x0000 +#define NS_KEY_NAME_ENTITY 0x0200 +#define NS_KEY_NAME_ZONE 0x0100 +#define NS_KEY_NAME_RESERVED 0x0300 +#define NS_KEY_RESERVED8 0x0080 +#define NS_KEY_RESERVED9 0x0040 +#define NS_KEY_RESERVED10 0x0020 +#define NS_KEY_RESERVED11 0x0010 +#define NS_KEY_SIGNATORYMASK 0x000F +#define NS_KEY_RESERVED_BITMASK ( NS_KEY_RESERVED2 | \ + NS_KEY_RESERVED4 | \ + NS_KEY_RESERVED5 | \ + NS_KEY_RESERVED8 | \ + NS_KEY_RESERVED9 | \ + NS_KEY_RESERVED10 | \ + NS_KEY_RESERVED11 ) +#define NS_KEY_RESERVED_BITMASK2 0xFFFF +#define NS_ALG_MD5RSA 1 +#define NS_ALG_DH 2 +#define NS_ALG_DSA 3 +#define NS_ALG_DSS NS_ALG_DSA +#define NS_ALG_EXPIRE_ONLY 253 +#define NS_ALG_PRIVATE_OID 254 + +#define NS_KEY_PROT_TLS 1 +#define NS_KEY_PROT_EMAIL 2 +#define NS_KEY_PROT_DNSSEC 3 +#define NS_KEY_PROT_IPSEC 4 +#define NS_KEY_PROT_ANY 255 + +#define NS_MD5RSA_MIN_BITS 512 +#define NS_MD5RSA_MAX_BITS 4096 +#define NS_MD5RSA_MAX_BYTES ((NS_MD5RSA_MAX_BITS+7/8)*2+3) +#define NS_MD5RSA_MAX_BASE64 (((NS_MD5RSA_MAX_BYTES+2)/3)*4) +#define NS_MD5RSA_MIN_SIZE ((NS_MD5RSA_MIN_BITS+7)/8) +#define NS_MD5RSA_MAX_SIZE ((NS_MD5RSA_MAX_BITS+7)/8) + +#define NS_DSA_SIG_SIZE 41 +#define NS_DSA_MIN_SIZE 213 +#define NS_DSA_MAX_BYTES 405 + +#define NS_SIG_TYPE 0 +#define NS_SIG_ALG 2 +#define NS_SIG_LABELS 3 +#define NS_SIG_OTTL 4 +#define NS_SIG_EXPIR 8 +#define NS_SIG_SIGNED 12 +#define NS_SIG_FOOT 16 +#define NS_SIG_SIGNER 18 +#define NS_NXT_BITS 8 +#define NS_NXT_BIT_SET( n,p) (p[(n)/NS_NXT_BITS] |= (0x80>>((n)%NS_NXT_BITS))) +#define NS_NXT_BIT_CLEAR(n,p) (p[(n)/NS_NXT_BITS] &= ~(0x80>>((n)%NS_NXT_BITS))) +#define NS_NXT_BIT_ISSET(n,p) (p[(n)/NS_NXT_BITS] & (0x80>>((n)%NS_NXT_BITS))) +#define NS_NXT_MAX 127 + +#define NS_OPT_DNSSEC_OK 0x8000U +#define NS_OPT_NSID 3 + +#define NS_GET16(s, cp) do { \ + register const unsigned char *t_cp = (const unsigned char *)(cp); \ + (s) = ((uint16_t)t_cp[0] << 8) \ + | ((uint16_t)t_cp[1]) \ + ; \ + (cp) += NS_INT16SZ; \ +} while (0) + +#define NS_GET32(l, cp) do { \ + register const unsigned char *t_cp = (const unsigned char *)(cp); \ + (l) = ((uint32_t)t_cp[0] << 24) \ + | ((uint32_t)t_cp[1] << 16) \ + | ((uint32_t)t_cp[2] << 8) \ + | ((uint32_t)t_cp[3]) \ + ; \ + (cp) += NS_INT32SZ; \ +} while (0) + +#define NS_PUT16(s, cp) do { \ + register uint16_t t_s = (uint16_t)(s); \ + register unsigned char *t_cp = (unsigned char *)(cp); \ + *t_cp++ = t_s >> 8; \ + *t_cp = t_s; \ + (cp) += NS_INT16SZ; \ +} while (0) + +#define NS_PUT32(l, cp) do { \ + register uint32_t t_l = (uint32_t)(l); \ + register unsigned char *t_cp = (unsigned char *)(cp); \ + *t_cp++ = t_l >> 24; \ + *t_cp++ = t_l >> 16; \ + *t_cp++ = t_l >> 8; \ + *t_cp = t_l; \ + (cp) += NS_INT32SZ; \ +} while (0) + + + + +#define __BIND 19950621 + +typedef struct { + unsigned id :16; +#if __BYTE_ORDER == __BIG_ENDIAN + unsigned qr: 1; + unsigned opcode: 4; + unsigned aa: 1; + unsigned tc: 1; + unsigned rd: 1; + unsigned ra: 1; + unsigned unused :1; + unsigned ad: 1; + unsigned cd: 1; + unsigned rcode :4; +#else + unsigned rd :1; + unsigned tc :1; + unsigned aa :1; + unsigned opcode :4; + unsigned qr :1; + unsigned rcode :4; + unsigned cd: 1; + unsigned ad: 1; + unsigned unused :1; + unsigned ra :1; +#endif + unsigned qdcount :16; + unsigned ancount :16; + unsigned nscount :16; + unsigned arcount :16; +} HEADER; + +#define PACKETSZ NS_PACKETSZ +#define MAXDNAME NS_MAXDNAME +#define MAXCDNAME NS_MAXCDNAME +#define MAXLABEL NS_MAXLABEL +#define HFIXEDSZ NS_HFIXEDSZ +#define QFIXEDSZ NS_QFIXEDSZ +#define RRFIXEDSZ NS_RRFIXEDSZ +#define INT32SZ NS_INT32SZ +#define INT16SZ NS_INT16SZ +#define INT8SZ NS_INT8SZ +#define INADDRSZ NS_INADDRSZ +#define IN6ADDRSZ NS_IN6ADDRSZ +#define INDIR_MASK NS_CMPRSFLGS +#define NAMESERVER_PORT NS_DEFAULTPORT + +#define S_ZONE ns_s_zn +#define S_PREREQ ns_s_pr +#define S_UPDATE ns_s_ud +#define S_ADDT ns_s_ar + +#define QUERY ns_o_query +#define IQUERY ns_o_iquery +#define STATUS ns_o_status +#define NS_NOTIFY_OP ns_o_notify +#define NS_UPDATE_OP ns_o_update + +#define NOERROR ns_r_noerror +#define FORMERR ns_r_formerr +#define SERVFAIL ns_r_servfail +#define NXDOMAIN ns_r_nxdomain +#define NOTIMP ns_r_notimpl +#define REFUSED ns_r_refused +#define YXDOMAIN ns_r_yxdomain +#define YXRRSET ns_r_yxrrset +#define NXRRSET ns_r_nxrrset +#define NOTAUTH ns_r_notauth +#define NOTZONE ns_r_notzone + +#define DELETE ns_uop_delete +#define ADD ns_uop_add + +#define T_A ns_t_a +#define T_NS ns_t_ns +#define T_MD ns_t_md +#define T_MF ns_t_mf +#define T_CNAME ns_t_cname +#define T_SOA ns_t_soa +#define T_MB ns_t_mb +#define T_MG ns_t_mg +#define T_MR ns_t_mr +#define T_NULL ns_t_null +#define T_WKS ns_t_wks +#define T_PTR ns_t_ptr +#define T_HINFO ns_t_hinfo +#define T_MINFO ns_t_minfo +#define T_MX ns_t_mx +#define T_TXT ns_t_txt +#define T_RP ns_t_rp +#define T_AFSDB ns_t_afsdb +#define T_X25 ns_t_x25 +#define T_ISDN ns_t_isdn +#define T_RT ns_t_rt +#define T_NSAP ns_t_nsap +#define T_NSAP_PTR ns_t_nsap_ptr +#define T_SIG ns_t_sig +#define T_KEY ns_t_key +#define T_PX ns_t_px +#define T_GPOS ns_t_gpos +#define T_AAAA ns_t_aaaa +#define T_LOC ns_t_loc +#define T_NXT ns_t_nxt +#define T_EID ns_t_eid +#define T_NIMLOC ns_t_nimloc +#define T_SRV ns_t_srv +#define T_ATMA ns_t_atma +#define T_NAPTR ns_t_naptr +#define T_A6 ns_t_a6 +#define T_DNAME ns_t_dname +#define T_TSIG ns_t_tsig +#define T_IXFR ns_t_ixfr +#define T_AXFR ns_t_axfr +#define T_MAILB ns_t_mailb +#define T_MAILA ns_t_maila +#define T_ANY ns_t_any + +#define C_IN ns_c_in +#define C_CHAOS ns_c_chaos +#define C_HS ns_c_hs +#define C_NONE ns_c_none +#define C_ANY ns_c_any + +#define GETSHORT NS_GET16 +#define GETLONG NS_GET32 +#define PUTSHORT NS_PUT16 +#define PUTLONG NS_PUT32 + +#endif diff --git a/05/musl-final/include/arpa/telnet.h b/05/musl-final/include/arpa/telnet.h new file mode 100644 index 0000000..e2ad974 --- /dev/null +++ b/05/musl-final/include/arpa/telnet.h @@ -0,0 +1,251 @@ +#ifndef _ARPA_TELNET_H +#define _ARPA_TELNET_H + +#define IAC 255 +#define DONT 254 +#define DO 253 +#define WONT 252 +#define WILL 251 +#define SB 250 +#define GA 249 +#define EL 248 +#define EC 247 +#define AYT 246 +#define AO 245 +#define IP 244 +#define BREAK 243 +#define DM 242 +#define NOP 241 +#define SE 240 +#define EOR 239 +#define ABORT 238 +#define SUSP 237 +#define xEOF 236 + +#define SYNCH 242 + +#define telcmds ((char [][6]){ "EOF", "SUSP", "ABORT", "EOR", "SE", "NOP", "DMARK", "BRK", "IP", "AO", "AYT", "EC", "EL", "GA", "SB", "WILL", "WONT", "DO", "DONT", "IAC", 0 }) + +#define TELCMD_FIRST xEOF +#define TELCMD_LAST IAC +#define TELCMD_OK(x) ((unsigned int)(x) <= TELCMD_LAST && \ + (unsigned int)(x) >= TELCMD_FIRST) +#define TELCMD(x) telcmds[(x)-TELCMD_FIRST] + +#define TELOPT_BINARY 0 +#define TELOPT_ECHO 1 +#define TELOPT_RCP 2 +#define TELOPT_SGA 3 +#define TELOPT_NAMS 4 +#define TELOPT_STATUS 5 +#define TELOPT_TM 6 +#define TELOPT_RCTE 7 +#define TELOPT_NAOL 8 +#define TELOPT_NAOP 9 +#define TELOPT_NAOCRD 10 +#define TELOPT_NAOHTS 11 +#define TELOPT_NAOHTD 12 +#define TELOPT_NAOFFD 13 +#define TELOPT_NAOVTS 14 +#define TELOPT_NAOVTD 15 +#define TELOPT_NAOLFD 16 +#define TELOPT_XASCII 17 +#define TELOPT_LOGOUT 18 +#define TELOPT_BM 19 +#define TELOPT_DET 20 +#define TELOPT_SUPDUP 21 +#define TELOPT_SUPDUPOUTPUT 22 +#define TELOPT_SNDLOC 23 +#define TELOPT_TTYPE 24 +#define TELOPT_EOR 25 +#define TELOPT_TUID 26 +#define TELOPT_OUTMRK 27 +#define TELOPT_TTYLOC 28 +#define TELOPT_3270REGIME 29 +#define TELOPT_X3PAD 30 +#define TELOPT_NAWS 31 +#define TELOPT_TSPEED 32 +#define TELOPT_LFLOW 33 +#define TELOPT_LINEMODE 34 +#define TELOPT_XDISPLOC 35 +#define TELOPT_OLD_ENVIRON 36 +#define TELOPT_AUTHENTICATION 37/* Authenticate */ +#define TELOPT_ENCRYPT 38 +#define TELOPT_NEW_ENVIRON 39 +#define TELOPT_EXOPL 255 + + +#define NTELOPTS (1+TELOPT_NEW_ENVIRON) +#ifdef TELOPTS +char *telopts[NTELOPTS+1] = { + "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME", + "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP", + "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS", + "NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO", + "DATA ENTRY TERMINAL", "SUPDUP", "SUPDUP OUTPUT", + "SEND LOCATION", "TERMINAL TYPE", "END OF RECORD", + "TACACS UID", "OUTPUT MARKING", "TTYLOC", + "3270 REGIME", "X.3 PAD", "NAWS", "TSPEED", "LFLOW", + "LINEMODE", "XDISPLOC", "OLD-ENVIRON", "AUTHENTICATION", + "ENCRYPT", "NEW-ENVIRON", + 0, +}; +#define TELOPT_FIRST TELOPT_BINARY +#define TELOPT_LAST TELOPT_NEW_ENVIRON +#define TELOPT_OK(x) ((unsigned int)(x) <= TELOPT_LAST) +#define TELOPT(x) telopts[(x)-TELOPT_FIRST] +#endif + +#define TELQUAL_IS 0 +#define TELQUAL_SEND 1 +#define TELQUAL_INFO 2 +#define TELQUAL_REPLY 2 +#define TELQUAL_NAME 3 + +#define LFLOW_OFF 0 +#define LFLOW_ON 1 +#define LFLOW_RESTART_ANY 2 +#define LFLOW_RESTART_XON 3 + + +#define LM_MODE 1 +#define LM_FORWARDMASK 2 +#define LM_SLC 3 + +#define MODE_EDIT 0x01 +#define MODE_TRAPSIG 0x02 +#define MODE_ACK 0x04 +#define MODE_SOFT_TAB 0x08 +#define MODE_LIT_ECHO 0x10 + +#define MODE_MASK 0x1f + +#define MODE_FLOW 0x0100 +#define MODE_ECHO 0x0200 +#define MODE_INBIN 0x0400 +#define MODE_OUTBIN 0x0800 +#define MODE_FORCE 0x1000 + +#define SLC_SYNCH 1 +#define SLC_BRK 2 +#define SLC_IP 3 +#define SLC_AO 4 +#define SLC_AYT 5 +#define SLC_EOR 6 +#define SLC_ABORT 7 +#define SLC_EOF 8 +#define SLC_SUSP 9 +#define SLC_EC 10 +#define SLC_EL 11 +#define SLC_EW 12 +#define SLC_RP 13 +#define SLC_LNEXT 14 +#define SLC_XON 15 +#define SLC_XOFF 16 +#define SLC_FORW1 17 +#define SLC_FORW2 18 + +#define NSLC 18 + +#define SLC_NAMELIST "0", "SYNCH", "BRK", "IP", "AO", "AYT", "EOR", \ + "ABORT", "EOF", "SUSP", "EC", "EL", "EW", "RP", \ + "LNEXT", "XON", "XOFF", "FORW1", "FORW2", 0, +#ifdef SLC_NAMES +char *slc_names[] = { + SLC_NAMELIST +}; +#else +extern char *slc_names[]; +#define SLC_NAMES SLC_NAMELIST +#endif + +#define SLC_NAME_OK(x) ((unsigned int)(x) <= NSLC) +#define SLC_NAME(x) slc_names[x] + +#define SLC_NOSUPPORT 0 +#define SLC_CANTCHANGE 1 +#define SLC_VARIABLE 2 +#define SLC_DEFAULT 3 +#define SLC_LEVELBITS 0x03 + +#define SLC_FUNC 0 +#define SLC_FLAGS 1 +#define SLC_VALUE 2 + +#define SLC_ACK 0x80 +#define SLC_FLUSHIN 0x40 +#define SLC_FLUSHOUT 0x20 + +#define OLD_ENV_VAR 1 +#define OLD_ENV_VALUE 0 +#define NEW_ENV_VAR 0 +#define NEW_ENV_VALUE 1 +#define ENV_ESC 2 +#define ENV_USERVAR 3 + +#define AUTH_WHO_CLIENT 0 +#define AUTH_WHO_SERVER 1 +#define AUTH_WHO_MASK 1 + +#define AUTH_HOW_ONE_WAY 0 +#define AUTH_HOW_MUTUAL 2 +#define AUTH_HOW_MASK 2 + +#define AUTHTYPE_NULL 0 +#define AUTHTYPE_KERBEROS_V4 1 +#define AUTHTYPE_KERBEROS_V5 2 +#define AUTHTYPE_SPX 3 +#define AUTHTYPE_MINK 4 +#define AUTHTYPE_CNT 5 + +#define AUTHTYPE_TEST 99 + +#ifdef AUTH_NAMES +char *authtype_names[] = { + "NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK", 0, +}; +#else +extern char *authtype_names[]; +#endif + +#define AUTHTYPE_NAME_OK(x) ((unsigned int)(x) < AUTHTYPE_CNT) +#define AUTHTYPE_NAME(x) authtype_names[x] + +#define ENCRYPT_IS 0 +#define ENCRYPT_SUPPORT 1 +#define ENCRYPT_REPLY 2 +#define ENCRYPT_START 3 +#define ENCRYPT_END 4 +#define ENCRYPT_REQSTART 5 +#define ENCRYPT_REQEND 6 +#define ENCRYPT_ENC_KEYID 7 +#define ENCRYPT_DEC_KEYID 8 +#define ENCRYPT_CNT 9 + +#define ENCTYPE_ANY 0 +#define ENCTYPE_DES_CFB64 1 +#define ENCTYPE_DES_OFB64 2 +#define ENCTYPE_CNT 3 + +#ifdef ENCRYPT_NAMES +char *encrypt_names[] = { + "IS", "SUPPORT", "REPLY", "START", "END", + "REQUEST-START", "REQUEST-END", "ENC-KEYID", "DEC-KEYID", + 0, +}; +char *enctype_names[] = { + "ANY", "DES_CFB64", "DES_OFB64", 0, +}; +#else +extern char *encrypt_names[]; +extern char *enctype_names[]; +#endif + + +#define ENCRYPT_NAME_OK(x) ((unsigned int)(x) < ENCRYPT_CNT) +#define ENCRYPT_NAME(x) encrypt_names[x] + +#define ENCTYPE_NAME_OK(x) ((unsigned int)(x) < ENCTYPE_CNT) +#define ENCTYPE_NAME(x) enctype_names[x] + +#endif diff --git a/05/musl-final/include/assert.h b/05/musl-final/include/assert.h new file mode 100644 index 0000000..bad2ccd --- /dev/null +++ b/05/musl-final/include/assert.h @@ -0,0 +1,17 @@ +#undef assert + +#ifdef NDEBUG +#define assert(x) (void)0 +#else +#define assert(x) ((x) || (__assert_fail(#x, __FILE__, __LINE__, __func__),0)) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +void __assert_fail (const char *, const char *, int, const char *); + +#ifdef __cplusplus +} +#endif diff --git a/05/musl-final/include/bits b/05/musl-final/include/bits new file mode 120000 index 0000000..ed2f0a7 --- /dev/null +++ b/05/musl-final/include/bits @@ -0,0 +1 @@ +../arch/x86_64/bits \ No newline at end of file diff --git a/05/musl-final/include/byteswap.h b/05/musl-final/include/byteswap.h new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/include/cpio.h b/05/musl-final/include/cpio.h new file mode 100644 index 0000000..39a1f8b --- /dev/null +++ b/05/musl-final/include/cpio.h @@ -0,0 +1,29 @@ +#ifndef _CPIO_H +#define _CPIO_H + +#define MAGIC "070707" + +#define C_IRUSR 000400 +#define C_IWUSR 000200 +#define C_IXUSR 000100 +#define C_IRGRP 000040 +#define C_IWGRP 000020 +#define C_IXGRP 000010 +#define C_IROTH 000004 +#define C_IWOTH 000002 +#define C_IXOTH 000001 + +#define C_ISUID 004000 +#define C_ISGID 002000 +#define C_ISVTX 001000 + +#define C_ISBLK 060000 +#define C_ISCHR 020000 +#define C_ISDIR 040000 +#define C_ISFIFO 010000 +#define C_ISSOCK 0140000 +#define C_ISLNK 0120000 +#define C_ISCTG 0110000 +#define C_ISREG 0100000 + +#endif diff --git a/05/musl-final/include/ctype.h b/05/musl-final/include/ctype.h new file mode 100644 index 0000000..97b9737 --- /dev/null +++ b/05/musl-final/include/ctype.h @@ -0,0 +1,56 @@ +#ifndef _CTYPE_H +#define _CTYPE_H + +int isalnum(int); +int isalpha(int); +int isblank(int); +int iscntrl(int); +int isdigit(int); +int isgraph(int); +int islower(int); +int isprint(int); +int ispunct(int); +int isspace(int); +int isupper(int); +int isxdigit(int); +int tolower(int); +int toupper(int); + +#define isalpha(a) ((unsigned)(((a)|32)-'a') < 26) +#define isdigit(a) ((unsigned)((a)-'0') < 10) +#define islower(a) ((unsigned)((a)-'a') < 26) +#define isupper(a) ((unsigned)((a)-'A') < 26) +#define isprint(a) ((unsigned)((a)-0x20) < 0x5f) +#define isgraph(a) ((unsigned)((a)-0x21) < 0x5e) + + + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +#define __NEED_locale_t +#include + +int isalnum_l(int, locale_t); +int isalpha_l(int, locale_t); +int isblank_l(int, locale_t); +int iscntrl_l(int, locale_t); +int isdigit_l(int, locale_t); +int isgraph_l(int, locale_t); +int islower_l(int, locale_t); +int isprint_l(int, locale_t); +int ispunct_l(int, locale_t); +int isspace_l(int, locale_t); +int isupper_l(int, locale_t); +int isxdigit_l(int, locale_t); +int tolower_l(int, locale_t); +int toupper_l(int, locale_t); + +int isascii(int); +int toascii(int); +#define _tolower(a) ((a)|0x20) +#define _toupper(a) ((a)&0x5f) + +#endif + +#endif diff --git a/05/musl-final/include/dirent.h b/05/musl-final/include/dirent.h new file mode 100644 index 0000000..2e8c996 --- /dev/null +++ b/05/musl-final/include/dirent.h @@ -0,0 +1,68 @@ +#ifndef _DIRENT_H +#define _DIRENT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_ino_t +#define __NEED_off_t + +#include + +typedef struct __DIR_s DIR; + +struct dirent +{ + ino_t d_ino; + off_t d_off; + unsigned short d_reclen; + unsigned char d_type; + char d_name[1]; +}; + +int closedir(DIR *); +DIR *fdopendir(int); +DIR *opendir(const char *); +struct dirent *readdir(DIR *); +int readdir_r(DIR *, struct dirent *, struct dirent **); +void rewinddir(DIR *); +void seekdir(DIR *, long); +long telldir(DIR *); +int dirfd(DIR *); + +int alphasort(const struct dirent **, const struct dirent **); +int scandir(const char *, struct dirent ***, int (*)(const struct dirent *), int (*)(const struct dirent **, const struct dirent **)); + +/* File types for `d_type'. */ +enum + { + DT_UNKNOWN = 0, +# define DT_UNKNOWN DT_UNKNOWN + DT_FIFO = 1, +# define DT_FIFO DT_FIFO + DT_CHR = 2, +# define DT_CHR DT_CHR + DT_DIR = 4, +# define DT_DIR DT_DIR + DT_BLK = 6, +# define DT_BLK DT_BLK + DT_REG = 8, +# define DT_REG DT_REG + DT_LNK = 10, +# define DT_LNK DT_LNK + DT_SOCK = 12, +# define DT_SOCK DT_SOCK + DT_WHT = 14 +# define DT_WHT DT_WHT + }; + +/* Convert between stat structure types and directory types. */ +# define IFTODT(mode) (((mode) & 0170000) >> 12) +# define DTTOIF(dirtype) ((dirtype) << 12) + +#ifdef __cplusplus +extern } +#endif + +#endif diff --git a/05/musl-final/include/elf.h b/05/musl-final/include/elf.h new file mode 100644 index 0000000..b011e78 --- /dev/null +++ b/05/musl-final/include/elf.h @@ -0,0 +1,2524 @@ +#ifndef _ELF_H +#define _ELF_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef uint16_t Elf32_Half; +typedef uint16_t Elf64_Half; + +typedef uint32_t Elf32_Word; +typedef int32_t Elf32_Sword; +typedef uint32_t Elf64_Word; +typedef int32_t Elf64_Sword; + +typedef uint64_t Elf32_Xword; +typedef int64_t Elf32_Sxword; +typedef uint64_t Elf64_Xword; +typedef int64_t Elf64_Sxword; + +typedef uint32_t Elf32_Addr; +typedef uint64_t Elf64_Addr; + +typedef uint32_t Elf32_Off; +typedef uint64_t Elf64_Off; + +typedef uint16_t Elf32_Section; +typedef uint16_t Elf64_Section; + +typedef Elf32_Half Elf32_Versym; +typedef Elf64_Half Elf64_Versym; + +#define EI_NIDENT (16) + +typedef struct { + unsigned char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; + Elf32_Half e_phentsize; + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; +} Elf32_Ehdr; + +typedef struct { + unsigned char e_ident[EI_NIDENT]; + Elf64_Half e_type; + Elf64_Half e_machine; + Elf64_Word e_version; + Elf64_Addr e_entry; + Elf64_Off e_phoff; + Elf64_Off e_shoff; + Elf64_Word e_flags; + Elf64_Half e_ehsize; + Elf64_Half e_phentsize; + Elf64_Half e_phnum; + Elf64_Half e_shentsize; + Elf64_Half e_shnum; + Elf64_Half e_shstrndx; +} Elf64_Ehdr; + +#define EI_MAG0 0 +#define ELFMAG0 0x7f + +#define EI_MAG1 1 +#define ELFMAG1 'E' + +#define EI_MAG2 2 +#define ELFMAG2 'L' + +#define EI_MAG3 3 +#define ELFMAG3 'F' + + +#define ELFMAG "\177ELF" +#define SELFMAG 4 + +#define EI_CLASS 4 +#define ELFCLASSNONE 0 +#define ELFCLASS32 1 +#define ELFCLASS64 2 +#define ELFCLASSNUM 3 + +#define EI_DATA 5 +#define ELFDATANONE 0 +#define ELFDATA2LSB 1 +#define ELFDATA2MSB 2 +#define ELFDATANUM 3 + +#define EI_VERSION 6 + + +#define EI_OSABI 7 +#define ELFOSABI_NONE 0 +#define ELFOSABI_SYSV 0 +#define ELFOSABI_HPUX 1 +#define ELFOSABI_NETBSD 2 +#define ELFOSABI_LINUX 3 +#define ELFOSABI_SOLARIS 6 +#define ELFOSABI_AIX 7 +#define ELFOSABI_IRIX 8 +#define ELFOSABI_FREEBSD 9 +#define ELFOSABI_TRU64 10 +#define ELFOSABI_MODESTO 11 +#define ELFOSABI_OPENBSD 12 +#define ELFOSABI_ARM 97 +#define ELFOSABI_STANDALONE 255 + +#define EI_ABIVERSION 8 + +#define EI_PAD 9 + + + +#define ET_NONE 0 +#define ET_REL 1 +#define ET_EXEC 2 +#define ET_DYN 3 +#define ET_CORE 4 +#define ET_NUM 5 +#define ET_LOOS 0xfe00 +#define ET_HIOS 0xfeff +#define ET_LOPROC 0xff00 +#define ET_HIPROC 0xffff + + + +#define EM_NONE 0 +#define EM_M32 1 +#define EM_SPARC 2 +#define EM_386 3 +#define EM_68K 4 +#define EM_88K 5 +#define EM_860 7 +#define EM_MIPS 8 +#define EM_S370 9 +#define EM_MIPS_RS3_LE 10 + +#define EM_PARISC 15 +#define EM_VPP500 17 +#define EM_SPARC32PLUS 18 +#define EM_960 19 +#define EM_PPC 20 +#define EM_PPC64 21 +#define EM_S390 22 + +#define EM_V800 36 +#define EM_FR20 37 +#define EM_RH32 38 +#define EM_RCE 39 +#define EM_ARM 40 +#define EM_FAKE_ALPHA 41 +#define EM_SH 42 +#define EM_SPARCV9 43 +#define EM_TRICORE 44 +#define EM_ARC 45 +#define EM_H8_300 46 +#define EM_H8_300H 47 +#define EM_H8S 48 +#define EM_H8_500 49 +#define EM_IA_64 50 +#define EM_MIPS_X 51 +#define EM_COLDFIRE 52 +#define EM_68HC12 53 +#define EM_MMA 54 +#define EM_PCP 55 +#define EM_NCPU 56 +#define EM_NDR1 57 +#define EM_STARCORE 58 +#define EM_ME16 59 +#define EM_ST100 60 +#define EM_TINYJ 61 +#define EM_X86_64 62 +#define EM_PDSP 63 + +#define EM_FX66 66 +#define EM_ST9PLUS 67 +#define EM_ST7 68 +#define EM_68HC16 69 +#define EM_68HC11 70 +#define EM_68HC08 71 +#define EM_68HC05 72 +#define EM_SVX 73 +#define EM_ST19 74 +#define EM_VAX 75 +#define EM_CRIS 76 +#define EM_JAVELIN 77 +#define EM_FIREPATH 78 +#define EM_ZSP 79 +#define EM_MMIX 80 +#define EM_HUANY 81 +#define EM_PRISM 82 +#define EM_AVR 83 +#define EM_FR30 84 +#define EM_D10V 85 +#define EM_D30V 86 +#define EM_V850 87 +#define EM_M32R 88 +#define EM_MN10300 89 +#define EM_MN10200 90 +#define EM_PJ 91 +#define EM_OPENRISC 92 +#define EM_ARC_A5 93 +#define EM_XTENSA 94 +#define EM_NUM 95 +#define EM_ALPHA 0x9026 + +#define EV_NONE 0 +#define EV_CURRENT 1 +#define EV_NUM 2 + +typedef struct { + Elf32_Word sh_name; + Elf32_Word sh_type; + Elf32_Word sh_flags; + Elf32_Addr sh_addr; + Elf32_Off sh_offset; + Elf32_Word sh_size; + Elf32_Word sh_link; + Elf32_Word sh_info; + Elf32_Word sh_addralign; + Elf32_Word sh_entsize; +} Elf32_Shdr; + +typedef struct { + Elf64_Word sh_name; + Elf64_Word sh_type; + Elf64_Xword sh_flags; + Elf64_Addr sh_addr; + Elf64_Off sh_offset; + Elf64_Xword sh_size; + Elf64_Word sh_link; + Elf64_Word sh_info; + Elf64_Xword sh_addralign; + Elf64_Xword sh_entsize; +} Elf64_Shdr; + + + +#define SHN_UNDEF 0 +#define SHN_LORESERVE 0xff00 +#define SHN_LOPROC 0xff00 +#define SHN_BEFORE 0xff00 + +#define SHN_AFTER 0xff01 + +#define SHN_HIPROC 0xff1f +#define SHN_LOOS 0xff20 +#define SHN_HIOS 0xff3f +#define SHN_ABS 0xfff1 +#define SHN_COMMON 0xfff2 +#define SHN_XINDEX 0xffff +#define SHN_HIRESERVE 0xffff + + + +#define SHT_NULL 0 +#define SHT_PROGBITS 1 +#define SHT_SYMTAB 2 +#define SHT_STRTAB 3 +#define SHT_RELA 4 +#define SHT_HASH 5 +#define SHT_DYNAMIC 6 +#define SHT_NOTE 7 +#define SHT_NOBITS 8 +#define SHT_REL 9 +#define SHT_SHLIB 10 +#define SHT_DYNSYM 11 +#define SHT_INIT_ARRAY 14 +#define SHT_FINI_ARRAY 15 +#define SHT_PREINIT_ARRAY 16 +#define SHT_GROUP 17 +#define SHT_SYMTAB_SHNDX 18 +#define SHT_NUM 19 +#define SHT_LOOS 0x60000000 +#define SHT_GNU_ATTRIBUTES 0x6ffffff5 +#define SHT_GNU_HASH 0x6ffffff6 +#define SHT_GNU_LIBLIST 0x6ffffff7 +#define SHT_CHECKSUM 0x6ffffff8 +#define SHT_LOSUNW 0x6ffffffa +#define SHT_SUNW_move 0x6ffffffa +#define SHT_SUNW_COMDAT 0x6ffffffb +#define SHT_SUNW_syminfo 0x6ffffffc +#define SHT_GNU_verdef 0x6ffffffd +#define SHT_GNU_verneed 0x6ffffffe +#define SHT_GNU_versym 0x6fffffff +#define SHT_HISUNW 0x6fffffff +#define SHT_HIOS 0x6fffffff +#define SHT_LOPROC 0x70000000 +#define SHT_HIPROC 0x7fffffff +#define SHT_LOUSER 0x80000000 +#define SHT_HIUSER 0x8fffffff + +#define SHF_WRITE (1 << 0) +#define SHF_ALLOC (1 << 1) +#define SHF_EXECINSTR (1 << 2) +#define SHF_MERGE (1 << 4) +#define SHF_STRINGS (1 << 5) +#define SHF_INFO_LINK (1 << 6) +#define SHF_LINK_ORDER (1 << 7) +#define SHF_OS_NONCONFORMING (1 << 8) + +#define SHF_GROUP (1 << 9) +#define SHF_TLS (1 << 10) +#define SHF_MASKOS 0x0ff00000 +#define SHF_MASKPROC 0xf0000000 +#define SHF_ORDERED (1 << 30) +#define SHF_EXCLUDE (1 << 31) + +#define GRP_COMDAT 0x1 + +typedef struct { + Elf32_Word st_name; + Elf32_Addr st_value; + Elf32_Word st_size; + unsigned char st_info; + unsigned char st_other; + Elf32_Section st_shndx; +} Elf32_Sym; + +typedef struct { + Elf64_Word st_name; + unsigned char st_info; + unsigned char st_other; + Elf64_Section st_shndx; + Elf64_Addr st_value; + Elf64_Xword st_size; +} Elf64_Sym; + +typedef struct { + Elf32_Half si_boundto; + Elf32_Half si_flags; +} Elf32_Syminfo; + +typedef struct { + Elf64_Half si_boundto; + Elf64_Half si_flags; +} Elf64_Syminfo; + +#define SYMINFO_BT_SELF 0xffff +#define SYMINFO_BT_PARENT 0xfffe +#define SYMINFO_BT_LOWRESERVE 0xff00 + +#define SYMINFO_FLG_DIRECT 0x0001 +#define SYMINFO_FLG_PASSTHRU 0x0002 +#define SYMINFO_FLG_COPY 0x0004 +#define SYMINFO_FLG_LAZYLOAD 0x0008 + +#define SYMINFO_NONE 0 +#define SYMINFO_CURRENT 1 +#define SYMINFO_NUM 2 + +#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) +#define ELF32_ST_TYPE(val) ((val) & 0xf) +#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) + +#define ELF64_ST_BIND(val) ELF32_ST_BIND (val) +#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) +#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) + +#define STB_LOCAL 0 +#define STB_GLOBAL 1 +#define STB_WEAK 2 +#define STB_NUM 3 +#define STB_LOOS 10 +#define STB_GNU_UNIQUE 10 +#define STB_HIOS 12 +#define STB_LOPROC 13 +#define STB_HIPROC 15 + +#define STT_NOTYPE 0 +#define STT_OBJECT 1 +#define STT_FUNC 2 +#define STT_SECTION 3 +#define STT_FILE 4 +#define STT_COMMON 5 +#define STT_TLS 6 +#define STT_NUM 7 +#define STT_LOOS 10 +#define STT_GNU_IFUNC 10 +#define STT_HIOS 12 +#define STT_LOPROC 13 +#define STT_HIPROC 15 + +#define STN_UNDEF 0 + +#define ELF32_ST_VISIBILITY(o) ((o) & 0x03) +#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) + +#define STV_DEFAULT 0 +#define STV_INTERNAL 1 +#define STV_HIDDEN 2 +#define STV_PROTECTED 3 + + + + +typedef struct +{ + Elf32_Addr r_offset; + Elf32_Word r_info; +} Elf32_Rel; + +typedef struct { + Elf64_Addr r_offset; + Elf64_Xword r_info; +} Elf64_Rel; + + + +typedef struct { + Elf32_Addr r_offset; + Elf32_Word r_info; + Elf32_Sword r_addend; +} Elf32_Rela; + +typedef struct { + Elf64_Addr r_offset; + Elf64_Xword r_info; + Elf64_Sxword r_addend; +} Elf64_Rela; + + + +#define ELF32_R_SYM(val) ((val) >> 8) +#define ELF32_R_TYPE(val) ((val) & 0xff) +#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) + +#define ELF64_R_SYM(i) ((i) >> 32) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) +#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type)) + + + +typedef struct { + Elf32_Word p_type; + Elf32_Off p_offset; + Elf32_Addr p_vaddr; + Elf32_Addr p_paddr; + Elf32_Word p_filesz; + Elf32_Word p_memsz; + Elf32_Word p_flags; + Elf32_Word p_align; +} Elf32_Phdr; + +typedef struct { + Elf64_Word p_type; + Elf64_Word p_flags; + Elf64_Off p_offset; + Elf64_Addr p_vaddr; + Elf64_Addr p_paddr; + Elf64_Xword p_filesz; + Elf64_Xword p_memsz; + Elf64_Xword p_align; +} Elf64_Phdr; + + + +#define PT_NULL 0 +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_TLS 7 +#define PT_NUM 8 +#define PT_LOOS 0x60000000 +#define PT_GNU_EH_FRAME 0x6474e550 +#define PT_GNU_STACK 0x6474e551 +#define PT_GNU_RELRO 0x6474e552 +#define PT_LOSUNW 0x6ffffffa +#define PT_SUNWBSS 0x6ffffffa +#define PT_SUNWSTACK 0x6ffffffb +#define PT_HISUNW 0x6fffffff +#define PT_HIOS 0x6fffffff +#define PT_LOPROC 0x70000000 +#define PT_HIPROC 0x7fffffff + + + +#define PF_X (1 << 0) +#define PF_W (1 << 1) +#define PF_R (1 << 2) +#define PF_MASKOS 0x0ff00000 +#define PF_MASKPROC 0xf0000000 + + + +#define NT_PRSTATUS 1 +#define NT_FPREGSET 2 +#define NT_PRPSINFO 3 +#define NT_PRXREG 4 +#define NT_TASKSTRUCT 4 +#define NT_PLATFORM 5 +#define NT_AUXV 6 +#define NT_GWINDOWS 7 +#define NT_ASRS 8 +#define NT_PSTATUS 10 +#define NT_PSINFO 13 +#define NT_PRCRED 14 +#define NT_UTSNAME 15 +#define NT_LWPSTATUS 16 +#define NT_LWPSINFO 17 +#define NT_PRFPXREG 20 +#define NT_PRXFPREG 0x46e62b7f +#define NT_PPC_VMX 0x100 +#define NT_PPC_SPE 0x101 +#define NT_PPC_VSX 0x102 +#define NT_386_TLS 0x200 +#define NT_386_IOPERM 0x201 +#define NT_VERSION 1 + + + + +typedef struct { + Elf32_Sword d_tag; + union { + Elf32_Word d_val; + Elf32_Addr d_ptr; + } d_un; +} Elf32_Dyn; + +typedef struct { + Elf64_Sxword d_tag; + union { + Elf64_Xword d_val; + Elf64_Addr d_ptr; + } d_un; +} Elf64_Dyn; + + + +#define DT_NULL 0 +#define DT_NEEDED 1 +#define DT_PLTRELSZ 2 +#define DT_PLTGOT 3 +#define DT_HASH 4 +#define DT_STRTAB 5 +#define DT_SYMTAB 6 +#define DT_RELA 7 +#define DT_RELASZ 8 +#define DT_RELAENT 9 +#define DT_STRSZ 10 +#define DT_SYMENT 11 +#define DT_INIT 12 +#define DT_FINI 13 +#define DT_SONAME 14 +#define DT_RPATH 15 +#define DT_SYMBOLIC 16 +#define DT_REL 17 +#define DT_RELSZ 18 +#define DT_RELENT 19 +#define DT_PLTREL 20 +#define DT_DEBUG 21 +#define DT_TEXTREL 22 +#define DT_JMPREL 23 +#define DT_BIND_NOW 24 +#define DT_INIT_ARRAY 25 +#define DT_FINI_ARRAY 26 +#define DT_INIT_ARRAYSZ 27 +#define DT_FINI_ARRAYSZ 28 +#define DT_RUNPATH 29 +#define DT_FLAGS 30 +#define DT_ENCODING 32 +#define DT_PREINIT_ARRAY 32 +#define DT_PREINIT_ARRAYSZ 33 +#define DT_NUM 34 +#define DT_LOOS 0x6000000d +#define DT_HIOS 0x6ffff000 +#define DT_LOPROC 0x70000000 +#define DT_HIPROC 0x7fffffff +#define DT_PROCNUM DT_MIPS_NUM + +#define DT_VALRNGLO 0x6ffffd00 +#define DT_GNU_PRELINKED 0x6ffffdf5 +#define DT_GNU_CONFLICTSZ 0x6ffffdf6 +#define DT_GNU_LIBLISTSZ 0x6ffffdf7 +#define DT_CHECKSUM 0x6ffffdf8 +#define DT_PLTPADSZ 0x6ffffdf9 +#define DT_MOVEENT 0x6ffffdfa +#define DT_MOVESZ 0x6ffffdfb +#define DT_FEATURE_1 0x6ffffdfc +#define DT_POSFLAG_1 0x6ffffdfd + +#define DT_SYMINSZ 0x6ffffdfe +#define DT_SYMINENT 0x6ffffdff +#define DT_VALRNGHI 0x6ffffdff +#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag)) +#define DT_VALNUM 12 + +#define DT_ADDRRNGLO 0x6ffffe00 +#define DT_GNU_HASH 0x6ffffef5 +#define DT_TLSDESC_PLT 0x6ffffef6 +#define DT_TLSDESC_GOT 0x6ffffef7 +#define DT_GNU_CONFLICT 0x6ffffef8 +#define DT_GNU_LIBLIST 0x6ffffef9 +#define DT_CONFIG 0x6ffffefa +#define DT_DEPAUDIT 0x6ffffefb +#define DT_AUDIT 0x6ffffefc +#define DT_PLTPAD 0x6ffffefd +#define DT_MOVETAB 0x6ffffefe +#define DT_SYMINFO 0x6ffffeff +#define DT_ADDRRNGHI 0x6ffffeff +#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag)) +#define DT_ADDRNUM 11 + + + +#define DT_VERSYM 0x6ffffff0 + +#define DT_RELACOUNT 0x6ffffff9 +#define DT_RELCOUNT 0x6ffffffa + + +#define DT_FLAGS_1 0x6ffffffb +#define DT_VERDEF 0x6ffffffc + +#define DT_VERDEFNUM 0x6ffffffd +#define DT_VERNEED 0x6ffffffe + +#define DT_VERNEEDNUM 0x6fffffff +#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) +#define DT_VERSIONTAGNUM 16 + + + +#define DT_AUXILIARY 0x7ffffffd +#define DT_FILTER 0x7fffffff +#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1) +#define DT_EXTRANUM 3 + + +#define DF_ORIGIN 0x00000001 +#define DF_SYMBOLIC 0x00000002 +#define DF_TEXTREL 0x00000004 +#define DF_BIND_NOW 0x00000008 +#define DF_STATIC_TLS 0x00000010 + + + +#define DF_1_NOW 0x00000001 +#define DF_1_GLOBAL 0x00000002 +#define DF_1_GROUP 0x00000004 +#define DF_1_NODELETE 0x00000008 +#define DF_1_LOADFLTR 0x00000010 +#define DF_1_INITFIRST 0x00000020 +#define DF_1_NOOPEN 0x00000040 +#define DF_1_ORIGIN 0x00000080 +#define DF_1_DIRECT 0x00000100 +#define DF_1_TRANS 0x00000200 +#define DF_1_INTERPOSE 0x00000400 +#define DF_1_NODEFLIB 0x00000800 +#define DF_1_NODUMP 0x00001000 +#define DF_1_CONFALT 0x00002000 +#define DF_1_ENDFILTEE 0x00004000 +#define DF_1_DISPRELDNE 0x00008000 +#define DF_1_DISPRELPND 0x00010000 + + +#define DTF_1_PARINIT 0x00000001 +#define DTF_1_CONFEXP 0x00000002 + + +#define DF_P1_LAZYLOAD 0x00000001 +#define DF_P1_GROUPPERM 0x00000002 + + + + +typedef struct { + Elf32_Half vd_version; + Elf32_Half vd_flags; + Elf32_Half vd_ndx; + Elf32_Half vd_cnt; + Elf32_Word vd_hash; + Elf32_Word vd_aux; + Elf32_Word vd_next; +} Elf32_Verdef; + +typedef struct { + Elf64_Half vd_version; + Elf64_Half vd_flags; + Elf64_Half vd_ndx; + Elf64_Half vd_cnt; + Elf64_Word vd_hash; + Elf64_Word vd_aux; + Elf64_Word vd_next; +} Elf64_Verdef; + + + +#define VER_DEF_NONE 0 +#define VER_DEF_CURRENT 1 +#define VER_DEF_NUM 2 + + +#define VER_FLG_BASE 0x1 +#define VER_FLG_WEAK 0x2 + + +#define VER_NDX_LOCAL 0 +#define VER_NDX_GLOBAL 1 +#define VER_NDX_LORESERVE 0xff00 +#define VER_NDX_ELIMINATE 0xff01 + + + +typedef struct { + Elf32_Word vda_name; + Elf32_Word vda_next; +} Elf32_Verdaux; + +typedef struct { + Elf64_Word vda_name; + Elf64_Word vda_next; +} Elf64_Verdaux; + + + + +typedef struct { + Elf32_Half vn_version; + Elf32_Half vn_cnt; + Elf32_Word vn_file; + Elf32_Word vn_aux; + Elf32_Word vn_next; +} Elf32_Verneed; + +typedef struct { + Elf64_Half vn_version; + Elf64_Half vn_cnt; + Elf64_Word vn_file; + Elf64_Word vn_aux; + Elf64_Word vn_next; +} Elf64_Verneed; + + + +#define VER_NEED_NONE 0 +#define VER_NEED_CURRENT 1 +#define VER_NEED_NUM 2 + + + +typedef struct { + Elf32_Word vna_hash; + Elf32_Half vna_flags; + Elf32_Half vna_other; + Elf32_Word vna_name; + Elf32_Word vna_next; +} Elf32_Vernaux; + +typedef struct { + Elf64_Word vna_hash; + Elf64_Half vna_flags; + Elf64_Half vna_other; + Elf64_Word vna_name; + Elf64_Word vna_next; +} Elf64_Vernaux; + + + +#define VER_FLG_WEAK 0x2 + + + +typedef struct { + uint32_t a_type; + union { + uint32_t a_val; + } a_un; +} Elf32_auxv_t; + +typedef struct { + uint64_t a_type; + union { + uint64_t a_val; + } a_un; +} Elf64_auxv_t; + + + +#define AT_NULL 0 +#define AT_IGNORE 1 +#define AT_EXECFD 2 +#define AT_PHDR 3 +#define AT_PHENT 4 +#define AT_PHNUM 5 +#define AT_PAGESZ 6 +#define AT_BASE 7 +#define AT_FLAGS 8 +#define AT_ENTRY 9 +#define AT_NOTELF 10 +#define AT_UID 11 +#define AT_EUID 12 +#define AT_GID 13 +#define AT_EGID 14 +#define AT_CLKTCK 17 + + +#define AT_PLATFORM 15 +#define AT_HWCAP 16 + + + + +#define AT_FPUCW 18 + + +#define AT_DCACHEBSIZE 19 +#define AT_ICACHEBSIZE 20 +#define AT_UCACHEBSIZE 21 + + + +#define AT_IGNOREPPC 22 + +#define AT_SECURE 23 + +#define AT_BASE_PLATFORM 24 + +#define AT_RANDOM 25 + +#define AT_EXECFN 31 + + + +#define AT_SYSINFO 32 +#define AT_SYSINFO_EHDR 33 + + + +#define AT_L1I_CACHESHAPE 34 +#define AT_L1D_CACHESHAPE 35 +#define AT_L2_CACHESHAPE 36 +#define AT_L3_CACHESHAPE 37 + + + + +typedef struct { + Elf32_Word n_namesz; + Elf32_Word n_descsz; + Elf32_Word n_type; +} Elf32_Nhdr; + +typedef struct { + Elf64_Word n_namesz; + Elf64_Word n_descsz; + Elf64_Word n_type; +} Elf64_Nhdr; + + + + +#define ELF_NOTE_SOLARIS "SUNW Solaris" + + +#define ELF_NOTE_GNU "GNU" + + + + + +#define ELF_NOTE_PAGESIZE_HINT 1 + + +#define NT_GNU_ABI_TAG 1 +#define ELF_NOTE_ABI NT_GNU_ABI_TAG + + + +#define ELF_NOTE_OS_LINUX 0 +#define ELF_NOTE_OS_GNU 1 +#define ELF_NOTE_OS_SOLARIS2 2 +#define ELF_NOTE_OS_FREEBSD 3 + +#define NT_GNU_BUILD_ID 3 +#define NT_GNU_GOLD_VERSION 4 + + + +typedef struct { + Elf32_Xword m_value; + Elf32_Word m_info; + Elf32_Word m_poffset; + Elf32_Half m_repeat; + Elf32_Half m_stride; +} Elf32_Move; + +typedef struct { + Elf64_Xword m_value; + Elf64_Xword m_info; + Elf64_Xword m_poffset; + Elf64_Half m_repeat; + Elf64_Half m_stride; +} Elf64_Move; + + +#define ELF32_M_SYM(info) ((info) >> 8) +#define ELF32_M_SIZE(info) ((unsigned char) (info)) +#define ELF32_M_INFO(sym, size) (((sym) << 8) + (unsigned char) (size)) + +#define ELF64_M_SYM(info) ELF32_M_SYM (info) +#define ELF64_M_SIZE(info) ELF32_M_SIZE (info) +#define ELF64_M_INFO(sym, size) ELF32_M_INFO (sym, size) + +#define EF_CPU32 0x00810000 + +#define R_68K_NONE 0 +#define R_68K_32 1 +#define R_68K_16 2 +#define R_68K_8 3 +#define R_68K_PC32 4 +#define R_68K_PC16 5 +#define R_68K_PC8 6 +#define R_68K_GOT32 7 +#define R_68K_GOT16 8 +#define R_68K_GOT8 9 +#define R_68K_GOT32O 10 +#define R_68K_GOT16O 11 +#define R_68K_GOT8O 12 +#define R_68K_PLT32 13 +#define R_68K_PLT16 14 +#define R_68K_PLT8 15 +#define R_68K_PLT32O 16 +#define R_68K_PLT16O 17 +#define R_68K_PLT8O 18 +#define R_68K_COPY 19 +#define R_68K_GLOB_DAT 20 +#define R_68K_JMP_SLOT 21 +#define R_68K_RELATIVE 22 +#define R_68K_NUM 23 + +#define R_386_NONE 0 +#define R_386_32 1 +#define R_386_PC32 2 +#define R_386_GOT32 3 +#define R_386_PLT32 4 +#define R_386_COPY 5 +#define R_386_GLOB_DAT 6 +#define R_386_JMP_SLOT 7 +#define R_386_RELATIVE 8 +#define R_386_GOTOFF 9 +#define R_386_GOTPC 10 +#define R_386_32PLT 11 +#define R_386_TLS_TPOFF 14 +#define R_386_TLS_IE 15 +#define R_386_TLS_GOTIE 16 +#define R_386_TLS_LE 17 +#define R_386_TLS_GD 18 +#define R_386_TLS_LDM 19 +#define R_386_16 20 +#define R_386_PC16 21 +#define R_386_8 22 +#define R_386_PC8 23 +#define R_386_TLS_GD_32 24 +#define R_386_TLS_GD_PUSH 25 +#define R_386_TLS_GD_CALL 26 +#define R_386_TLS_GD_POP 27 +#define R_386_TLS_LDM_32 28 +#define R_386_TLS_LDM_PUSH 29 +#define R_386_TLS_LDM_CALL 30 +#define R_386_TLS_LDM_POP 31 +#define R_386_TLS_LDO_32 32 +#define R_386_TLS_IE_32 33 +#define R_386_TLS_LE_32 34 +#define R_386_TLS_DTPMOD32 35 +#define R_386_TLS_DTPOFF32 36 +#define R_386_TLS_TPOFF32 37 +#define R_386_TLS_GOTDESC 39 +#define R_386_TLS_DESC_CALL 40 +#define R_386_TLS_DESC 41 +#define R_386_IRELATIVE 42 +#define R_386_NUM 43 + + + + + +#define STT_SPARC_REGISTER 13 + + + +#define EF_SPARCV9_MM 3 +#define EF_SPARCV9_TSO 0 +#define EF_SPARCV9_PSO 1 +#define EF_SPARCV9_RMO 2 +#define EF_SPARC_LEDATA 0x800000 +#define EF_SPARC_EXT_MASK 0xFFFF00 +#define EF_SPARC_32PLUS 0x000100 +#define EF_SPARC_SUN_US1 0x000200 +#define EF_SPARC_HAL_R1 0x000400 +#define EF_SPARC_SUN_US3 0x000800 + + + +#define R_SPARC_NONE 0 +#define R_SPARC_8 1 +#define R_SPARC_16 2 +#define R_SPARC_32 3 +#define R_SPARC_DISP8 4 +#define R_SPARC_DISP16 5 +#define R_SPARC_DISP32 6 +#define R_SPARC_WDISP30 7 +#define R_SPARC_WDISP22 8 +#define R_SPARC_HI22 9 +#define R_SPARC_22 10 +#define R_SPARC_13 11 +#define R_SPARC_LO10 12 +#define R_SPARC_GOT10 13 +#define R_SPARC_GOT13 14 +#define R_SPARC_GOT22 15 +#define R_SPARC_PC10 16 +#define R_SPARC_PC22 17 +#define R_SPARC_WPLT30 18 +#define R_SPARC_COPY 19 +#define R_SPARC_GLOB_DAT 20 +#define R_SPARC_JMP_SLOT 21 +#define R_SPARC_RELATIVE 22 +#define R_SPARC_UA32 23 + + + +#define R_SPARC_PLT32 24 +#define R_SPARC_HIPLT22 25 +#define R_SPARC_LOPLT10 26 +#define R_SPARC_PCPLT32 27 +#define R_SPARC_PCPLT22 28 +#define R_SPARC_PCPLT10 29 +#define R_SPARC_10 30 +#define R_SPARC_11 31 +#define R_SPARC_64 32 +#define R_SPARC_OLO10 33 +#define R_SPARC_HH22 34 +#define R_SPARC_HM10 35 +#define R_SPARC_LM22 36 +#define R_SPARC_PC_HH22 37 +#define R_SPARC_PC_HM10 38 +#define R_SPARC_PC_LM22 39 +#define R_SPARC_WDISP16 40 +#define R_SPARC_WDISP19 41 +#define R_SPARC_GLOB_JMP 42 +#define R_SPARC_7 43 +#define R_SPARC_5 44 +#define R_SPARC_6 45 +#define R_SPARC_DISP64 46 +#define R_SPARC_PLT64 47 +#define R_SPARC_HIX22 48 +#define R_SPARC_LOX10 49 +#define R_SPARC_H44 50 +#define R_SPARC_M44 51 +#define R_SPARC_L44 52 +#define R_SPARC_REGISTER 53 +#define R_SPARC_UA64 54 +#define R_SPARC_UA16 55 +#define R_SPARC_TLS_GD_HI22 56 +#define R_SPARC_TLS_GD_LO10 57 +#define R_SPARC_TLS_GD_ADD 58 +#define R_SPARC_TLS_GD_CALL 59 +#define R_SPARC_TLS_LDM_HI22 60 +#define R_SPARC_TLS_LDM_LO10 61 +#define R_SPARC_TLS_LDM_ADD 62 +#define R_SPARC_TLS_LDM_CALL 63 +#define R_SPARC_TLS_LDO_HIX22 64 +#define R_SPARC_TLS_LDO_LOX10 65 +#define R_SPARC_TLS_LDO_ADD 66 +#define R_SPARC_TLS_IE_HI22 67 +#define R_SPARC_TLS_IE_LO10 68 +#define R_SPARC_TLS_IE_LD 69 +#define R_SPARC_TLS_IE_LDX 70 +#define R_SPARC_TLS_IE_ADD 71 +#define R_SPARC_TLS_LE_HIX22 72 +#define R_SPARC_TLS_LE_LOX10 73 +#define R_SPARC_TLS_DTPMOD32 74 +#define R_SPARC_TLS_DTPMOD64 75 +#define R_SPARC_TLS_DTPOFF32 76 +#define R_SPARC_TLS_DTPOFF64 77 +#define R_SPARC_TLS_TPOFF32 78 +#define R_SPARC_TLS_TPOFF64 79 +#define R_SPARC_GOTDATA_HIX22 80 +#define R_SPARC_GOTDATA_LOX10 81 +#define R_SPARC_GOTDATA_OP_HIX22 82 +#define R_SPARC_GOTDATA_OP_LOX10 83 +#define R_SPARC_GOTDATA_OP 84 +#define R_SPARC_H34 85 +#define R_SPARC_SIZE32 86 +#define R_SPARC_SIZE64 87 +#define R_SPARC_GNU_VTINHERIT 250 +#define R_SPARC_GNU_VTENTRY 251 +#define R_SPARC_REV32 252 + +#define R_SPARC_NUM 253 + + + +#define DT_SPARC_REGISTER 0x70000001 +#define DT_SPARC_NUM 2 + + + +#define HWCAP_SPARC_FLUSH 1 +#define HWCAP_SPARC_STBAR 2 +#define HWCAP_SPARC_SWAP 4 +#define HWCAP_SPARC_MULDIV 8 +#define HWCAP_SPARC_V9 16 +#define HWCAP_SPARC_ULTRA3 32 +#define HWCAP_SPARC_BLKINIT 64 +#define HWCAP_SPARC_N2 128 + + + + + +#define EF_MIPS_NOREORDER 1 +#define EF_MIPS_PIC 2 +#define EF_MIPS_CPIC 4 +#define EF_MIPS_XGOT 8 +#define EF_MIPS_64BIT_WHIRL 16 +#define EF_MIPS_ABI2 32 +#define EF_MIPS_ABI_ON32 64 +#define EF_MIPS_ARCH 0xf0000000 + + + +#define EF_MIPS_ARCH_1 0x00000000 +#define EF_MIPS_ARCH_2 0x10000000 +#define EF_MIPS_ARCH_3 0x20000000 +#define EF_MIPS_ARCH_4 0x30000000 +#define EF_MIPS_ARCH_5 0x40000000 +#define EF_MIPS_ARCH_32 0x60000000 +#define EF_MIPS_ARCH_64 0x70000000 + + + +#define E_MIPS_ARCH_1 0x00000000 +#define E_MIPS_ARCH_2 0x10000000 +#define E_MIPS_ARCH_3 0x20000000 +#define E_MIPS_ARCH_4 0x30000000 +#define E_MIPS_ARCH_5 0x40000000 +#define E_MIPS_ARCH_32 0x60000000 +#define E_MIPS_ARCH_64 0x70000000 + + + +#define SHN_MIPS_ACOMMON 0xff00 +#define SHN_MIPS_TEXT 0xff01 +#define SHN_MIPS_DATA 0xff02 +#define SHN_MIPS_SCOMMON 0xff03 +#define SHN_MIPS_SUNDEFINED 0xff04 + + + +#define SHT_MIPS_LIBLIST 0x70000000 +#define SHT_MIPS_MSYM 0x70000001 +#define SHT_MIPS_CONFLICT 0x70000002 +#define SHT_MIPS_GPTAB 0x70000003 +#define SHT_MIPS_UCODE 0x70000004 +#define SHT_MIPS_DEBUG 0x70000005 +#define SHT_MIPS_REGINFO 0x70000006 +#define SHT_MIPS_PACKAGE 0x70000007 +#define SHT_MIPS_PACKSYM 0x70000008 +#define SHT_MIPS_RELD 0x70000009 +#define SHT_MIPS_IFACE 0x7000000b +#define SHT_MIPS_CONTENT 0x7000000c +#define SHT_MIPS_OPTIONS 0x7000000d +#define SHT_MIPS_SHDR 0x70000010 +#define SHT_MIPS_FDESC 0x70000011 +#define SHT_MIPS_EXTSYM 0x70000012 +#define SHT_MIPS_DENSE 0x70000013 +#define SHT_MIPS_PDESC 0x70000014 +#define SHT_MIPS_LOCSYM 0x70000015 +#define SHT_MIPS_AUXSYM 0x70000016 +#define SHT_MIPS_OPTSYM 0x70000017 +#define SHT_MIPS_LOCSTR 0x70000018 +#define SHT_MIPS_LINE 0x70000019 +#define SHT_MIPS_RFDESC 0x7000001a +#define SHT_MIPS_DELTASYM 0x7000001b +#define SHT_MIPS_DELTAINST 0x7000001c +#define SHT_MIPS_DELTACLASS 0x7000001d +#define SHT_MIPS_DWARF 0x7000001e +#define SHT_MIPS_DELTADECL 0x7000001f +#define SHT_MIPS_SYMBOL_LIB 0x70000020 +#define SHT_MIPS_EVENTS 0x70000021 +#define SHT_MIPS_TRANSLATE 0x70000022 +#define SHT_MIPS_PIXIE 0x70000023 +#define SHT_MIPS_XLATE 0x70000024 +#define SHT_MIPS_XLATE_DEBUG 0x70000025 +#define SHT_MIPS_WHIRL 0x70000026 +#define SHT_MIPS_EH_REGION 0x70000027 +#define SHT_MIPS_XLATE_OLD 0x70000028 +#define SHT_MIPS_PDR_EXCEPTION 0x70000029 + + + +#define SHF_MIPS_GPREL 0x10000000 +#define SHF_MIPS_MERGE 0x20000000 +#define SHF_MIPS_ADDR 0x40000000 +#define SHF_MIPS_STRINGS 0x80000000 +#define SHF_MIPS_NOSTRIP 0x08000000 +#define SHF_MIPS_LOCAL 0x04000000 +#define SHF_MIPS_NAMES 0x02000000 +#define SHF_MIPS_NODUPE 0x01000000 + + + + + +#define STO_MIPS_DEFAULT 0x0 +#define STO_MIPS_INTERNAL 0x1 +#define STO_MIPS_HIDDEN 0x2 +#define STO_MIPS_PROTECTED 0x3 +#define STO_MIPS_PLT 0x8 +#define STO_MIPS_SC_ALIGN_UNUSED 0xff + + +#define STB_MIPS_SPLIT_COMMON 13 + + + +typedef union { + struct { + Elf32_Word gt_current_g_value; + Elf32_Word gt_unused; + } gt_header; + struct { + Elf32_Word gt_g_value; + Elf32_Word gt_bytes; + } gt_entry; +} Elf32_gptab; + + + +typedef struct { + Elf32_Word ri_gprmask; + Elf32_Word ri_cprmask[4]; + Elf32_Sword ri_gp_value; +} Elf32_RegInfo; + + + +typedef struct { + unsigned char kind; + + unsigned char size; + Elf32_Section section; + + Elf32_Word info; +} Elf_Options; + + + +#define ODK_NULL 0 +#define ODK_REGINFO 1 +#define ODK_EXCEPTIONS 2 +#define ODK_PAD 3 +#define ODK_HWPATCH 4 +#define ODK_FILL 5 +#define ODK_TAGS 6 +#define ODK_HWAND 7 +#define ODK_HWOR 8 + + + +#define OEX_FPU_MIN 0x1f +#define OEX_FPU_MAX 0x1f00 +#define OEX_PAGE0 0x10000 +#define OEX_SMM 0x20000 +#define OEX_FPDBUG 0x40000 +#define OEX_PRECISEFP OEX_FPDBUG +#define OEX_DISMISS 0x80000 + +#define OEX_FPU_INVAL 0x10 +#define OEX_FPU_DIV0 0x08 +#define OEX_FPU_OFLO 0x04 +#define OEX_FPU_UFLO 0x02 +#define OEX_FPU_INEX 0x01 + + + +#define OHW_R4KEOP 0x1 +#define OHW_R8KPFETCH 0x2 +#define OHW_R5KEOP 0x4 +#define OHW_R5KCVTL 0x8 + +#define OPAD_PREFIX 0x1 +#define OPAD_POSTFIX 0x2 +#define OPAD_SYMBOL 0x4 + + + +typedef struct { + Elf32_Word hwp_flags1; + Elf32_Word hwp_flags2; +} Elf_Options_Hw; + + + +#define OHWA0_R4KEOP_CHECKED 0x00000001 +#define OHWA1_R4KEOP_CLEAN 0x00000002 + + + +#define R_MIPS_NONE 0 +#define R_MIPS_16 1 +#define R_MIPS_32 2 +#define R_MIPS_REL32 3 +#define R_MIPS_26 4 +#define R_MIPS_HI16 5 +#define R_MIPS_LO16 6 +#define R_MIPS_GPREL16 7 +#define R_MIPS_LITERAL 8 +#define R_MIPS_GOT16 9 +#define R_MIPS_PC16 10 +#define R_MIPS_CALL16 11 +#define R_MIPS_GPREL32 12 + +#define R_MIPS_SHIFT5 16 +#define R_MIPS_SHIFT6 17 +#define R_MIPS_64 18 +#define R_MIPS_GOT_DISP 19 +#define R_MIPS_GOT_PAGE 20 +#define R_MIPS_GOT_OFST 21 +#define R_MIPS_GOT_HI16 22 +#define R_MIPS_GOT_LO16 23 +#define R_MIPS_SUB 24 +#define R_MIPS_INSERT_A 25 +#define R_MIPS_INSERT_B 26 +#define R_MIPS_DELETE 27 +#define R_MIPS_HIGHER 28 +#define R_MIPS_HIGHEST 29 +#define R_MIPS_CALL_HI16 30 +#define R_MIPS_CALL_LO16 31 +#define R_MIPS_SCN_DISP 32 +#define R_MIPS_REL16 33 +#define R_MIPS_ADD_IMMEDIATE 34 +#define R_MIPS_PJUMP 35 +#define R_MIPS_RELGOT 36 +#define R_MIPS_JALR 37 +#define R_MIPS_TLS_DTPMOD32 38 +#define R_MIPS_TLS_DTPREL32 39 +#define R_MIPS_TLS_DTPMOD64 40 +#define R_MIPS_TLS_DTPREL64 41 +#define R_MIPS_TLS_GD 42 +#define R_MIPS_TLS_LDM 43 +#define R_MIPS_TLS_DTPREL_HI16 44 +#define R_MIPS_TLS_DTPREL_LO16 45 +#define R_MIPS_TLS_GOTTPREL 46 +#define R_MIPS_TLS_TPREL32 47 +#define R_MIPS_TLS_TPREL64 48 +#define R_MIPS_TLS_TPREL_HI16 49 +#define R_MIPS_TLS_TPREL_LO16 50 +#define R_MIPS_GLOB_DAT 51 +#define R_MIPS_COPY 126 +#define R_MIPS_JUMP_SLOT 127 + +#define R_MIPS_NUM 128 + + + +#define PT_MIPS_REGINFO 0x70000000 +#define PT_MIPS_RTPROC 0x70000001 +#define PT_MIPS_OPTIONS 0x70000002 + + + +#define PF_MIPS_LOCAL 0x10000000 + + + +#define DT_MIPS_RLD_VERSION 0x70000001 +#define DT_MIPS_TIME_STAMP 0x70000002 +#define DT_MIPS_ICHECKSUM 0x70000003 +#define DT_MIPS_IVERSION 0x70000004 +#define DT_MIPS_FLAGS 0x70000005 +#define DT_MIPS_BASE_ADDRESS 0x70000006 +#define DT_MIPS_MSYM 0x70000007 +#define DT_MIPS_CONFLICT 0x70000008 +#define DT_MIPS_LIBLIST 0x70000009 +#define DT_MIPS_LOCAL_GOTNO 0x7000000a +#define DT_MIPS_CONFLICTNO 0x7000000b +#define DT_MIPS_LIBLISTNO 0x70000010 +#define DT_MIPS_SYMTABNO 0x70000011 +#define DT_MIPS_UNREFEXTNO 0x70000012 +#define DT_MIPS_GOTSYM 0x70000013 +#define DT_MIPS_HIPAGENO 0x70000014 +#define DT_MIPS_RLD_MAP 0x70000016 +#define DT_MIPS_DELTA_CLASS 0x70000017 +#define DT_MIPS_DELTA_CLASS_NO 0x70000018 + +#define DT_MIPS_DELTA_INSTANCE 0x70000019 +#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a + +#define DT_MIPS_DELTA_RELOC 0x7000001b +#define DT_MIPS_DELTA_RELOC_NO 0x7000001c + +#define DT_MIPS_DELTA_SYM 0x7000001d + +#define DT_MIPS_DELTA_SYM_NO 0x7000001e + +#define DT_MIPS_DELTA_CLASSSYM 0x70000020 + +#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 + +#define DT_MIPS_CXX_FLAGS 0x70000022 +#define DT_MIPS_PIXIE_INIT 0x70000023 +#define DT_MIPS_SYMBOL_LIB 0x70000024 +#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025 +#define DT_MIPS_LOCAL_GOTIDX 0x70000026 +#define DT_MIPS_HIDDEN_GOTIDX 0x70000027 +#define DT_MIPS_PROTECTED_GOTIDX 0x70000028 +#define DT_MIPS_OPTIONS 0x70000029 +#define DT_MIPS_INTERFACE 0x7000002a +#define DT_MIPS_DYNSTR_ALIGN 0x7000002b +#define DT_MIPS_INTERFACE_SIZE 0x7000002c +#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d + +#define DT_MIPS_PERF_SUFFIX 0x7000002e + +#define DT_MIPS_COMPACT_SIZE 0x7000002f +#define DT_MIPS_GP_VALUE 0x70000030 +#define DT_MIPS_AUX_DYNAMIC 0x70000031 + +#define DT_MIPS_PLTGOT 0x70000032 + +#define DT_MIPS_RWPLT 0x70000034 +#define DT_MIPS_NUM 0x35 + + + +#define RHF_NONE 0 +#define RHF_QUICKSTART (1 << 0) +#define RHF_NOTPOT (1 << 1) +#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) +#define RHF_NO_MOVE (1 << 3) +#define RHF_SGI_ONLY (1 << 4) +#define RHF_GUARANTEE_INIT (1 << 5) +#define RHF_DELTA_C_PLUS_PLUS (1 << 6) +#define RHF_GUARANTEE_START_INIT (1 << 7) +#define RHF_PIXIE (1 << 8) +#define RHF_DEFAULT_DELAY_LOAD (1 << 9) +#define RHF_REQUICKSTART (1 << 10) +#define RHF_REQUICKSTARTED (1 << 11) +#define RHF_CORD (1 << 12) +#define RHF_NO_UNRES_UNDEF (1 << 13) +#define RHF_RLD_ORDER_SAFE (1 << 14) + + + +typedef struct +{ + Elf32_Word l_name; + Elf32_Word l_time_stamp; + Elf32_Word l_checksum; + Elf32_Word l_version; + Elf32_Word l_flags; +} Elf32_Lib; + +typedef struct +{ + Elf64_Word l_name; + Elf64_Word l_time_stamp; + Elf64_Word l_checksum; + Elf64_Word l_version; + Elf64_Word l_flags; +} Elf64_Lib; + + + + +#define LL_NONE 0 +#define LL_EXACT_MATCH (1 << 0) +#define LL_IGNORE_INT_VER (1 << 1) +#define LL_REQUIRE_MINOR (1 << 2) +#define LL_EXPORTS (1 << 3) +#define LL_DELAY_LOAD (1 << 4) +#define LL_DELTA (1 << 5) + + + +typedef Elf32_Addr Elf32_Conflict; + + + + + + +#define EF_PARISC_TRAPNIL 0x00010000 +#define EF_PARISC_EXT 0x00020000 +#define EF_PARISC_LSB 0x00040000 +#define EF_PARISC_WIDE 0x00080000 +#define EF_PARISC_NO_KABP 0x00100000 + +#define EF_PARISC_LAZYSWAP 0x00400000 +#define EF_PARISC_ARCH 0x0000ffff + + + +#define EFA_PARISC_1_0 0x020b +#define EFA_PARISC_1_1 0x0210 +#define EFA_PARISC_2_0 0x0214 + + + +#define SHN_PARISC_ANSI_COMMON 0xff00 + +#define SHN_PARISC_HUGE_COMMON 0xff01 + + + +#define SHT_PARISC_EXT 0x70000000 +#define SHT_PARISC_UNWIND 0x70000001 +#define SHT_PARISC_DOC 0x70000002 + + + +#define SHF_PARISC_SHORT 0x20000000 +#define SHF_PARISC_HUGE 0x40000000 +#define SHF_PARISC_SBP 0x80000000 + + + +#define STT_PARISC_MILLICODE 13 + +#define STT_HP_OPAQUE (STT_LOOS + 0x1) +#define STT_HP_STUB (STT_LOOS + 0x2) + + + +#define R_PARISC_NONE 0 +#define R_PARISC_DIR32 1 +#define R_PARISC_DIR21L 2 +#define R_PARISC_DIR17R 3 +#define R_PARISC_DIR17F 4 +#define R_PARISC_DIR14R 6 +#define R_PARISC_PCREL32 9 +#define R_PARISC_PCREL21L 10 +#define R_PARISC_PCREL17R 11 +#define R_PARISC_PCREL17F 12 +#define R_PARISC_PCREL14R 14 +#define R_PARISC_DPREL21L 18 +#define R_PARISC_DPREL14R 22 +#define R_PARISC_GPREL21L 26 +#define R_PARISC_GPREL14R 30 +#define R_PARISC_LTOFF21L 34 +#define R_PARISC_LTOFF14R 38 +#define R_PARISC_SECREL32 41 +#define R_PARISC_SEGBASE 48 +#define R_PARISC_SEGREL32 49 +#define R_PARISC_PLTOFF21L 50 +#define R_PARISC_PLTOFF14R 54 +#define R_PARISC_LTOFF_FPTR32 57 +#define R_PARISC_LTOFF_FPTR21L 58 +#define R_PARISC_LTOFF_FPTR14R 62 +#define R_PARISC_FPTR64 64 +#define R_PARISC_PLABEL32 65 +#define R_PARISC_PLABEL21L 66 +#define R_PARISC_PLABEL14R 70 +#define R_PARISC_PCREL64 72 +#define R_PARISC_PCREL22F 74 +#define R_PARISC_PCREL14WR 75 +#define R_PARISC_PCREL14DR 76 +#define R_PARISC_PCREL16F 77 +#define R_PARISC_PCREL16WF 78 +#define R_PARISC_PCREL16DF 79 +#define R_PARISC_DIR64 80 +#define R_PARISC_DIR14WR 83 +#define R_PARISC_DIR14DR 84 +#define R_PARISC_DIR16F 85 +#define R_PARISC_DIR16WF 86 +#define R_PARISC_DIR16DF 87 +#define R_PARISC_GPREL64 88 +#define R_PARISC_GPREL14WR 91 +#define R_PARISC_GPREL14DR 92 +#define R_PARISC_GPREL16F 93 +#define R_PARISC_GPREL16WF 94 +#define R_PARISC_GPREL16DF 95 +#define R_PARISC_LTOFF64 96 +#define R_PARISC_LTOFF14WR 99 +#define R_PARISC_LTOFF14DR 100 +#define R_PARISC_LTOFF16F 101 +#define R_PARISC_LTOFF16WF 102 +#define R_PARISC_LTOFF16DF 103 +#define R_PARISC_SECREL64 104 +#define R_PARISC_SEGREL64 112 +#define R_PARISC_PLTOFF14WR 115 +#define R_PARISC_PLTOFF14DR 116 +#define R_PARISC_PLTOFF16F 117 +#define R_PARISC_PLTOFF16WF 118 +#define R_PARISC_PLTOFF16DF 119 +#define R_PARISC_LTOFF_FPTR64 120 +#define R_PARISC_LTOFF_FPTR14WR 123 +#define R_PARISC_LTOFF_FPTR14DR 124 +#define R_PARISC_LTOFF_FPTR16F 125 +#define R_PARISC_LTOFF_FPTR16WF 126 +#define R_PARISC_LTOFF_FPTR16DF 127 +#define R_PARISC_LORESERVE 128 +#define R_PARISC_COPY 128 +#define R_PARISC_IPLT 129 +#define R_PARISC_EPLT 130 +#define R_PARISC_TPREL32 153 +#define R_PARISC_TPREL21L 154 +#define R_PARISC_TPREL14R 158 +#define R_PARISC_LTOFF_TP21L 162 +#define R_PARISC_LTOFF_TP14R 166 +#define R_PARISC_LTOFF_TP14F 167 +#define R_PARISC_TPREL64 216 +#define R_PARISC_TPREL14WR 219 +#define R_PARISC_TPREL14DR 220 +#define R_PARISC_TPREL16F 221 +#define R_PARISC_TPREL16WF 222 +#define R_PARISC_TPREL16DF 223 +#define R_PARISC_LTOFF_TP64 224 +#define R_PARISC_LTOFF_TP14WR 227 +#define R_PARISC_LTOFF_TP14DR 228 +#define R_PARISC_LTOFF_TP16F 229 +#define R_PARISC_LTOFF_TP16WF 230 +#define R_PARISC_LTOFF_TP16DF 231 +#define R_PARISC_GNU_VTENTRY 232 +#define R_PARISC_GNU_VTINHERIT 233 +#define R_PARISC_TLS_GD21L 234 +#define R_PARISC_TLS_GD14R 235 +#define R_PARISC_TLS_GDCALL 236 +#define R_PARISC_TLS_LDM21L 237 +#define R_PARISC_TLS_LDM14R 238 +#define R_PARISC_TLS_LDMCALL 239 +#define R_PARISC_TLS_LDO21L 240 +#define R_PARISC_TLS_LDO14R 241 +#define R_PARISC_TLS_DTPMOD32 242 +#define R_PARISC_TLS_DTPMOD64 243 +#define R_PARISC_TLS_DTPOFF32 244 +#define R_PARISC_TLS_DTPOFF64 245 +#define R_PARISC_TLS_LE21L R_PARISC_TPREL21L +#define R_PARISC_TLS_LE14R R_PARISC_TPREL14R +#define R_PARISC_TLS_IE21L R_PARISC_LTOFF_TP21L +#define R_PARISC_TLS_IE14R R_PARISC_LTOFF_TP14R +#define R_PARISC_TLS_TPREL32 R_PARISC_TPREL32 +#define R_PARISC_TLS_TPREL64 R_PARISC_TPREL64 +#define R_PARISC_HIRESERVE 255 + + + +#define PT_HP_TLS (PT_LOOS + 0x0) +#define PT_HP_CORE_NONE (PT_LOOS + 0x1) +#define PT_HP_CORE_VERSION (PT_LOOS + 0x2) +#define PT_HP_CORE_KERNEL (PT_LOOS + 0x3) +#define PT_HP_CORE_COMM (PT_LOOS + 0x4) +#define PT_HP_CORE_PROC (PT_LOOS + 0x5) +#define PT_HP_CORE_LOADABLE (PT_LOOS + 0x6) +#define PT_HP_CORE_STACK (PT_LOOS + 0x7) +#define PT_HP_CORE_SHM (PT_LOOS + 0x8) +#define PT_HP_CORE_MMF (PT_LOOS + 0x9) +#define PT_HP_PARALLEL (PT_LOOS + 0x10) +#define PT_HP_FASTBIND (PT_LOOS + 0x11) +#define PT_HP_OPT_ANNOT (PT_LOOS + 0x12) +#define PT_HP_HSL_ANNOT (PT_LOOS + 0x13) +#define PT_HP_STACK (PT_LOOS + 0x14) + +#define PT_PARISC_ARCHEXT 0x70000000 +#define PT_PARISC_UNWIND 0x70000001 + + + +#define PF_PARISC_SBP 0x08000000 + +#define PF_HP_PAGE_SIZE 0x00100000 +#define PF_HP_FAR_SHARED 0x00200000 +#define PF_HP_NEAR_SHARED 0x00400000 +#define PF_HP_CODE 0x01000000 +#define PF_HP_MODIFY 0x02000000 +#define PF_HP_LAZYSWAP 0x04000000 +#define PF_HP_SBP 0x08000000 + + + + + + +#define EF_ALPHA_32BIT 1 +#define EF_ALPHA_CANRELAX 2 + + + + +#define SHT_ALPHA_DEBUG 0x70000001 +#define SHT_ALPHA_REGINFO 0x70000002 + + + +#define SHF_ALPHA_GPREL 0x10000000 + + +#define STO_ALPHA_NOPV 0x80 +#define STO_ALPHA_STD_GPLOAD 0x88 + + + +#define R_ALPHA_NONE 0 +#define R_ALPHA_REFLONG 1 +#define R_ALPHA_REFQUAD 2 +#define R_ALPHA_GPREL32 3 +#define R_ALPHA_LITERAL 4 +#define R_ALPHA_LITUSE 5 +#define R_ALPHA_GPDISP 6 +#define R_ALPHA_BRADDR 7 +#define R_ALPHA_HINT 8 +#define R_ALPHA_SREL16 9 +#define R_ALPHA_SREL32 10 +#define R_ALPHA_SREL64 11 +#define R_ALPHA_GPRELHIGH 17 +#define R_ALPHA_GPRELLOW 18 +#define R_ALPHA_GPREL16 19 +#define R_ALPHA_COPY 24 +#define R_ALPHA_GLOB_DAT 25 +#define R_ALPHA_JMP_SLOT 26 +#define R_ALPHA_RELATIVE 27 +#define R_ALPHA_TLS_GD_HI 28 +#define R_ALPHA_TLSGD 29 +#define R_ALPHA_TLS_LDM 30 +#define R_ALPHA_DTPMOD64 31 +#define R_ALPHA_GOTDTPREL 32 +#define R_ALPHA_DTPREL64 33 +#define R_ALPHA_DTPRELHI 34 +#define R_ALPHA_DTPRELLO 35 +#define R_ALPHA_DTPREL16 36 +#define R_ALPHA_GOTTPREL 37 +#define R_ALPHA_TPREL64 38 +#define R_ALPHA_TPRELHI 39 +#define R_ALPHA_TPRELLO 40 +#define R_ALPHA_TPREL16 41 + +#define R_ALPHA_NUM 46 + + +#define LITUSE_ALPHA_ADDR 0 +#define LITUSE_ALPHA_BASE 1 +#define LITUSE_ALPHA_BYTOFF 2 +#define LITUSE_ALPHA_JSR 3 +#define LITUSE_ALPHA_TLS_GD 4 +#define LITUSE_ALPHA_TLS_LDM 5 + + +#define DT_ALPHA_PLTRO (DT_LOPROC + 0) +#define DT_ALPHA_NUM 1 + + + + +#define EF_PPC_EMB 0x80000000 + + +#define EF_PPC_RELOCATABLE 0x00010000 +#define EF_PPC_RELOCATABLE_LIB 0x00008000 + + + +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 +#define R_PPC_ADDR24 2 +#define R_PPC_ADDR16 3 +#define R_PPC_ADDR16_LO 4 +#define R_PPC_ADDR16_HI 5 +#define R_PPC_ADDR16_HA 6 +#define R_PPC_ADDR14 7 +#define R_PPC_ADDR14_BRTAKEN 8 +#define R_PPC_ADDR14_BRNTAKEN 9 +#define R_PPC_REL24 10 +#define R_PPC_REL14 11 +#define R_PPC_REL14_BRTAKEN 12 +#define R_PPC_REL14_BRNTAKEN 13 +#define R_PPC_GOT16 14 +#define R_PPC_GOT16_LO 15 +#define R_PPC_GOT16_HI 16 +#define R_PPC_GOT16_HA 17 +#define R_PPC_PLTREL24 18 +#define R_PPC_COPY 19 +#define R_PPC_GLOB_DAT 20 +#define R_PPC_JMP_SLOT 21 +#define R_PPC_RELATIVE 22 +#define R_PPC_LOCAL24PC 23 +#define R_PPC_UADDR32 24 +#define R_PPC_UADDR16 25 +#define R_PPC_REL32 26 +#define R_PPC_PLT32 27 +#define R_PPC_PLTREL32 28 +#define R_PPC_PLT16_LO 29 +#define R_PPC_PLT16_HI 30 +#define R_PPC_PLT16_HA 31 +#define R_PPC_SDAREL16 32 +#define R_PPC_SECTOFF 33 +#define R_PPC_SECTOFF_LO 34 +#define R_PPC_SECTOFF_HI 35 +#define R_PPC_SECTOFF_HA 36 + + +#define R_PPC_TLS 67 +#define R_PPC_DTPMOD32 68 +#define R_PPC_TPREL16 69 +#define R_PPC_TPREL16_LO 70 +#define R_PPC_TPREL16_HI 71 +#define R_PPC_TPREL16_HA 72 +#define R_PPC_TPREL32 73 +#define R_PPC_DTPREL16 74 +#define R_PPC_DTPREL16_LO 75 +#define R_PPC_DTPREL16_HI 76 +#define R_PPC_DTPREL16_HA 77 +#define R_PPC_DTPREL32 78 +#define R_PPC_GOT_TLSGD16 79 +#define R_PPC_GOT_TLSGD16_LO 80 +#define R_PPC_GOT_TLSGD16_HI 81 +#define R_PPC_GOT_TLSGD16_HA 82 +#define R_PPC_GOT_TLSLD16 83 +#define R_PPC_GOT_TLSLD16_LO 84 +#define R_PPC_GOT_TLSLD16_HI 85 +#define R_PPC_GOT_TLSLD16_HA 86 +#define R_PPC_GOT_TPREL16 87 +#define R_PPC_GOT_TPREL16_LO 88 +#define R_PPC_GOT_TPREL16_HI 89 +#define R_PPC_GOT_TPREL16_HA 90 +#define R_PPC_GOT_DTPREL16 91 +#define R_PPC_GOT_DTPREL16_LO 92 +#define R_PPC_GOT_DTPREL16_HI 93 +#define R_PPC_GOT_DTPREL16_HA 94 + + + +#define R_PPC_EMB_NADDR32 101 +#define R_PPC_EMB_NADDR16 102 +#define R_PPC_EMB_NADDR16_LO 103 +#define R_PPC_EMB_NADDR16_HI 104 +#define R_PPC_EMB_NADDR16_HA 105 +#define R_PPC_EMB_SDAI16 106 +#define R_PPC_EMB_SDA2I16 107 +#define R_PPC_EMB_SDA2REL 108 +#define R_PPC_EMB_SDA21 109 +#define R_PPC_EMB_MRKREF 110 +#define R_PPC_EMB_RELSEC16 111 +#define R_PPC_EMB_RELST_LO 112 +#define R_PPC_EMB_RELST_HI 113 +#define R_PPC_EMB_RELST_HA 114 +#define R_PPC_EMB_BIT_FLD 115 +#define R_PPC_EMB_RELSDA 116 + + +#define R_PPC_DIAB_SDA21_LO 180 +#define R_PPC_DIAB_SDA21_HI 181 +#define R_PPC_DIAB_SDA21_HA 182 +#define R_PPC_DIAB_RELSDA_LO 183 +#define R_PPC_DIAB_RELSDA_HI 184 +#define R_PPC_DIAB_RELSDA_HA 185 + + +#define R_PPC_IRELATIVE 248 + + +#define R_PPC_REL16 249 +#define R_PPC_REL16_LO 250 +#define R_PPC_REL16_HI 251 +#define R_PPC_REL16_HA 252 + + + +#define R_PPC_TOC16 255 + + +#define DT_PPC_GOT (DT_LOPROC + 0) +#define DT_PPC_NUM 1 + + +#define R_PPC64_NONE R_PPC_NONE +#define R_PPC64_ADDR32 R_PPC_ADDR32 +#define R_PPC64_ADDR24 R_PPC_ADDR24 +#define R_PPC64_ADDR16 R_PPC_ADDR16 +#define R_PPC64_ADDR16_LO R_PPC_ADDR16_LO +#define R_PPC64_ADDR16_HI R_PPC_ADDR16_HI +#define R_PPC64_ADDR16_HA R_PPC_ADDR16_HA +#define R_PPC64_ADDR14 R_PPC_ADDR14 +#define R_PPC64_ADDR14_BRTAKEN R_PPC_ADDR14_BRTAKEN +#define R_PPC64_ADDR14_BRNTAKEN R_PPC_ADDR14_BRNTAKEN +#define R_PPC64_REL24 R_PPC_REL24 +#define R_PPC64_REL14 R_PPC_REL14 +#define R_PPC64_REL14_BRTAKEN R_PPC_REL14_BRTAKEN +#define R_PPC64_REL14_BRNTAKEN R_PPC_REL14_BRNTAKEN +#define R_PPC64_GOT16 R_PPC_GOT16 +#define R_PPC64_GOT16_LO R_PPC_GOT16_LO +#define R_PPC64_GOT16_HI R_PPC_GOT16_HI +#define R_PPC64_GOT16_HA R_PPC_GOT16_HA + +#define R_PPC64_COPY R_PPC_COPY +#define R_PPC64_GLOB_DAT R_PPC_GLOB_DAT +#define R_PPC64_JMP_SLOT R_PPC_JMP_SLOT +#define R_PPC64_RELATIVE R_PPC_RELATIVE + +#define R_PPC64_UADDR32 R_PPC_UADDR32 +#define R_PPC64_UADDR16 R_PPC_UADDR16 +#define R_PPC64_REL32 R_PPC_REL32 +#define R_PPC64_PLT32 R_PPC_PLT32 +#define R_PPC64_PLTREL32 R_PPC_PLTREL32 +#define R_PPC64_PLT16_LO R_PPC_PLT16_LO +#define R_PPC64_PLT16_HI R_PPC_PLT16_HI +#define R_PPC64_PLT16_HA R_PPC_PLT16_HA + +#define R_PPC64_SECTOFF R_PPC_SECTOFF +#define R_PPC64_SECTOFF_LO R_PPC_SECTOFF_LO +#define R_PPC64_SECTOFF_HI R_PPC_SECTOFF_HI +#define R_PPC64_SECTOFF_HA R_PPC_SECTOFF_HA +#define R_PPC64_ADDR30 37 +#define R_PPC64_ADDR64 38 +#define R_PPC64_ADDR16_HIGHER 39 +#define R_PPC64_ADDR16_HIGHERA 40 +#define R_PPC64_ADDR16_HIGHEST 41 +#define R_PPC64_ADDR16_HIGHESTA 42 +#define R_PPC64_UADDR64 43 +#define R_PPC64_REL64 44 +#define R_PPC64_PLT64 45 +#define R_PPC64_PLTREL64 46 +#define R_PPC64_TOC16 47 +#define R_PPC64_TOC16_LO 48 +#define R_PPC64_TOC16_HI 49 +#define R_PPC64_TOC16_HA 50 +#define R_PPC64_TOC 51 +#define R_PPC64_PLTGOT16 52 +#define R_PPC64_PLTGOT16_LO 53 +#define R_PPC64_PLTGOT16_HI 54 +#define R_PPC64_PLTGOT16_HA 55 + +#define R_PPC64_ADDR16_DS 56 +#define R_PPC64_ADDR16_LO_DS 57 +#define R_PPC64_GOT16_DS 58 +#define R_PPC64_GOT16_LO_DS 59 +#define R_PPC64_PLT16_LO_DS 60 +#define R_PPC64_SECTOFF_DS 61 +#define R_PPC64_SECTOFF_LO_DS 62 +#define R_PPC64_TOC16_DS 63 +#define R_PPC64_TOC16_LO_DS 64 +#define R_PPC64_PLTGOT16_DS 65 +#define R_PPC64_PLTGOT16_LO_DS 66 + + +#define R_PPC64_TLS 67 +#define R_PPC64_DTPMOD64 68 +#define R_PPC64_TPREL16 69 +#define R_PPC64_TPREL16_LO 70 +#define R_PPC64_TPREL16_HI 71 +#define R_PPC64_TPREL16_HA 72 +#define R_PPC64_TPREL64 73 +#define R_PPC64_DTPREL16 74 +#define R_PPC64_DTPREL16_LO 75 +#define R_PPC64_DTPREL16_HI 76 +#define R_PPC64_DTPREL16_HA 77 +#define R_PPC64_DTPREL64 78 +#define R_PPC64_GOT_TLSGD16 79 +#define R_PPC64_GOT_TLSGD16_LO 80 +#define R_PPC64_GOT_TLSGD16_HI 81 +#define R_PPC64_GOT_TLSGD16_HA 82 +#define R_PPC64_GOT_TLSLD16 83 +#define R_PPC64_GOT_TLSLD16_LO 84 +#define R_PPC64_GOT_TLSLD16_HI 85 +#define R_PPC64_GOT_TLSLD16_HA 86 +#define R_PPC64_GOT_TPREL16_DS 87 +#define R_PPC64_GOT_TPREL16_LO_DS 88 +#define R_PPC64_GOT_TPREL16_HI 89 +#define R_PPC64_GOT_TPREL16_HA 90 +#define R_PPC64_GOT_DTPREL16_DS 91 +#define R_PPC64_GOT_DTPREL16_LO_DS 92 +#define R_PPC64_GOT_DTPREL16_HI 93 +#define R_PPC64_GOT_DTPREL16_HA 94 +#define R_PPC64_TPREL16_DS 95 +#define R_PPC64_TPREL16_LO_DS 96 +#define R_PPC64_TPREL16_HIGHER 97 +#define R_PPC64_TPREL16_HIGHERA 98 +#define R_PPC64_TPREL16_HIGHEST 99 +#define R_PPC64_TPREL16_HIGHESTA 100 +#define R_PPC64_DTPREL16_DS 101 +#define R_PPC64_DTPREL16_LO_DS 102 +#define R_PPC64_DTPREL16_HIGHER 103 +#define R_PPC64_DTPREL16_HIGHERA 104 +#define R_PPC64_DTPREL16_HIGHEST 105 +#define R_PPC64_DTPREL16_HIGHESTA 106 + + +#define R_PPC64_JMP_IREL 247 +#define R_PPC64_IRELATIVE 248 +#define R_PPC64_REL16 249 +#define R_PPC64_REL16_LO 250 +#define R_PPC64_REL16_HI 251 +#define R_PPC64_REL16_HA 252 + + +#define DT_PPC64_GLINK (DT_LOPROC + 0) +#define DT_PPC64_OPD (DT_LOPROC + 1) +#define DT_PPC64_OPDSZ (DT_LOPROC + 2) +#define DT_PPC64_NUM 3 + + + + + +#define EF_ARM_RELEXEC 0x01 +#define EF_ARM_HASENTRY 0x02 +#define EF_ARM_INTERWORK 0x04 +#define EF_ARM_APCS_26 0x08 +#define EF_ARM_APCS_FLOAT 0x10 +#define EF_ARM_PIC 0x20 +#define EF_ARM_ALIGN8 0x40 +#define EF_ARM_NEW_ABI 0x80 +#define EF_ARM_OLD_ABI 0x100 +#define EF_ARM_SOFT_FLOAT 0x200 +#define EF_ARM_VFP_FLOAT 0x400 +#define EF_ARM_MAVERICK_FLOAT 0x800 + + + + +#define EF_ARM_SYMSARESORTED 0x04 +#define EF_ARM_DYNSYMSUSESEGIDX 0x08 +#define EF_ARM_MAPSYMSFIRST 0x10 +#define EF_ARM_EABIMASK 0XFF000000 + + +#define EF_ARM_BE8 0x00800000 +#define EF_ARM_LE8 0x00400000 + +#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK) +#define EF_ARM_EABI_UNKNOWN 0x00000000 +#define EF_ARM_EABI_VER1 0x01000000 +#define EF_ARM_EABI_VER2 0x02000000 +#define EF_ARM_EABI_VER3 0x03000000 +#define EF_ARM_EABI_VER4 0x04000000 +#define EF_ARM_EABI_VER5 0x05000000 + + +#define STT_ARM_TFUNC STT_LOPROC +#define STT_ARM_16BIT STT_HIPROC + + +#define SHF_ARM_ENTRYSECT 0x10000000 +#define SHF_ARM_COMDEF 0x80000000 + + + +#define PF_ARM_SB 0x10000000 + +#define PF_ARM_PI 0x20000000 +#define PF_ARM_ABS 0x40000000 + + +#define PT_ARM_EXIDX (PT_LOPROC + 1) + + +#define SHT_ARM_EXIDX (SHT_LOPROC + 1) +#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) +#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) + + + + +#define R_ARM_NONE 0 +#define R_ARM_PC24 1 +#define R_ARM_ABS32 2 +#define R_ARM_REL32 3 +#define R_ARM_PC13 4 +#define R_ARM_ABS16 5 +#define R_ARM_ABS12 6 +#define R_ARM_THM_ABS5 7 +#define R_ARM_ABS8 8 +#define R_ARM_SBREL32 9 +#define R_ARM_THM_PC22 10 +#define R_ARM_THM_PC8 11 +#define R_ARM_AMP_VCALL9 12 +#define R_ARM_SWI24 13 +#define R_ARM_THM_SWI8 14 +#define R_ARM_XPC25 15 +#define R_ARM_THM_XPC22 16 +#define R_ARM_TLS_DTPMOD32 17 +#define R_ARM_TLS_DTPOFF32 18 +#define R_ARM_TLS_TPOFF32 19 +#define R_ARM_COPY 20 +#define R_ARM_GLOB_DAT 21 +#define R_ARM_JUMP_SLOT 22 +#define R_ARM_RELATIVE 23 +#define R_ARM_GOTOFF 24 +#define R_ARM_GOTPC 25 +#define R_ARM_GOT32 26 +#define R_ARM_PLT32 27 +#define R_ARM_ALU_PCREL_7_0 32 +#define R_ARM_ALU_PCREL_15_8 33 +#define R_ARM_ALU_PCREL_23_15 34 +#define R_ARM_LDR_SBREL_11_0 35 +#define R_ARM_ALU_SBREL_19_12 36 +#define R_ARM_ALU_SBREL_27_20 37 +#define R_ARM_GNU_VTENTRY 100 +#define R_ARM_GNU_VTINHERIT 101 +#define R_ARM_THM_PC11 102 +#define R_ARM_THM_PC9 103 +#define R_ARM_TLS_GD32 104 + +#define R_ARM_TLS_LDM32 105 + +#define R_ARM_TLS_LDO32 106 + +#define R_ARM_TLS_IE32 107 + +#define R_ARM_TLS_LE32 108 + +#define R_ARM_RXPC25 249 +#define R_ARM_RSBREL32 250 +#define R_ARM_THM_RPC22 251 +#define R_ARM_RREL32 252 +#define R_ARM_RABS22 253 +#define R_ARM_RPC24 254 +#define R_ARM_RBASE 255 + +#define R_ARM_NUM 256 + + + + +#define EF_IA_64_MASKOS 0x0000000f +#define EF_IA_64_ABI64 0x00000010 +#define EF_IA_64_ARCH 0xff000000 + + +#define PT_IA_64_ARCHEXT (PT_LOPROC + 0) +#define PT_IA_64_UNWIND (PT_LOPROC + 1) +#define PT_IA_64_HP_OPT_ANOT (PT_LOOS + 0x12) +#define PT_IA_64_HP_HSL_ANOT (PT_LOOS + 0x13) +#define PT_IA_64_HP_STACK (PT_LOOS + 0x14) + + +#define PF_IA_64_NORECOV 0x80000000 + + +#define SHT_IA_64_EXT (SHT_LOPROC + 0) +#define SHT_IA_64_UNWIND (SHT_LOPROC + 1) + + +#define SHF_IA_64_SHORT 0x10000000 +#define SHF_IA_64_NORECOV 0x20000000 + + +#define DT_IA_64_PLT_RESERVE (DT_LOPROC + 0) +#define DT_IA_64_NUM 1 + + +#define R_IA64_NONE 0x00 +#define R_IA64_IMM14 0x21 +#define R_IA64_IMM22 0x22 +#define R_IA64_IMM64 0x23 +#define R_IA64_DIR32MSB 0x24 +#define R_IA64_DIR32LSB 0x25 +#define R_IA64_DIR64MSB 0x26 +#define R_IA64_DIR64LSB 0x27 +#define R_IA64_GPREL22 0x2a +#define R_IA64_GPREL64I 0x2b +#define R_IA64_GPREL32MSB 0x2c +#define R_IA64_GPREL32LSB 0x2d +#define R_IA64_GPREL64MSB 0x2e +#define R_IA64_GPREL64LSB 0x2f +#define R_IA64_LTOFF22 0x32 +#define R_IA64_LTOFF64I 0x33 +#define R_IA64_PLTOFF22 0x3a +#define R_IA64_PLTOFF64I 0x3b +#define R_IA64_PLTOFF64MSB 0x3e +#define R_IA64_PLTOFF64LSB 0x3f +#define R_IA64_FPTR64I 0x43 +#define R_IA64_FPTR32MSB 0x44 +#define R_IA64_FPTR32LSB 0x45 +#define R_IA64_FPTR64MSB 0x46 +#define R_IA64_FPTR64LSB 0x47 +#define R_IA64_PCREL60B 0x48 +#define R_IA64_PCREL21B 0x49 +#define R_IA64_PCREL21M 0x4a +#define R_IA64_PCREL21F 0x4b +#define R_IA64_PCREL32MSB 0x4c +#define R_IA64_PCREL32LSB 0x4d +#define R_IA64_PCREL64MSB 0x4e +#define R_IA64_PCREL64LSB 0x4f +#define R_IA64_LTOFF_FPTR22 0x52 +#define R_IA64_LTOFF_FPTR64I 0x53 +#define R_IA64_LTOFF_FPTR32MSB 0x54 +#define R_IA64_LTOFF_FPTR32LSB 0x55 +#define R_IA64_LTOFF_FPTR64MSB 0x56 +#define R_IA64_LTOFF_FPTR64LSB 0x57 +#define R_IA64_SEGREL32MSB 0x5c +#define R_IA64_SEGREL32LSB 0x5d +#define R_IA64_SEGREL64MSB 0x5e +#define R_IA64_SEGREL64LSB 0x5f +#define R_IA64_SECREL32MSB 0x64 +#define R_IA64_SECREL32LSB 0x65 +#define R_IA64_SECREL64MSB 0x66 +#define R_IA64_SECREL64LSB 0x67 +#define R_IA64_REL32MSB 0x6c +#define R_IA64_REL32LSB 0x6d +#define R_IA64_REL64MSB 0x6e +#define R_IA64_REL64LSB 0x6f +#define R_IA64_LTV32MSB 0x74 +#define R_IA64_LTV32LSB 0x75 +#define R_IA64_LTV64MSB 0x76 +#define R_IA64_LTV64LSB 0x77 +#define R_IA64_PCREL21BI 0x79 +#define R_IA64_PCREL22 0x7a +#define R_IA64_PCREL64I 0x7b +#define R_IA64_IPLTMSB 0x80 +#define R_IA64_IPLTLSB 0x81 +#define R_IA64_COPY 0x84 +#define R_IA64_SUB 0x85 +#define R_IA64_LTOFF22X 0x86 +#define R_IA64_LDXMOV 0x87 +#define R_IA64_TPREL14 0x91 +#define R_IA64_TPREL22 0x92 +#define R_IA64_TPREL64I 0x93 +#define R_IA64_TPREL64MSB 0x96 +#define R_IA64_TPREL64LSB 0x97 +#define R_IA64_LTOFF_TPREL22 0x9a +#define R_IA64_DTPMOD64MSB 0xa6 +#define R_IA64_DTPMOD64LSB 0xa7 +#define R_IA64_LTOFF_DTPMOD22 0xaa +#define R_IA64_DTPREL14 0xb1 +#define R_IA64_DTPREL22 0xb2 +#define R_IA64_DTPREL64I 0xb3 +#define R_IA64_DTPREL32MSB 0xb4 +#define R_IA64_DTPREL32LSB 0xb5 +#define R_IA64_DTPREL64MSB 0xb6 +#define R_IA64_DTPREL64LSB 0xb7 +#define R_IA64_LTOFF_DTPREL22 0xba + + + + +#define R_SH_NONE 0 +#define R_SH_DIR32 1 +#define R_SH_REL32 2 +#define R_SH_DIR8WPN 3 +#define R_SH_IND12W 4 +#define R_SH_DIR8WPL 5 +#define R_SH_DIR8WPZ 6 +#define R_SH_DIR8BP 7 +#define R_SH_DIR8W 8 +#define R_SH_DIR8L 9 +#define R_SH_SWITCH16 25 +#define R_SH_SWITCH32 26 +#define R_SH_USES 27 +#define R_SH_COUNT 28 +#define R_SH_ALIGN 29 +#define R_SH_CODE 30 +#define R_SH_DATA 31 +#define R_SH_LABEL 32 +#define R_SH_SWITCH8 33 +#define R_SH_GNU_VTINHERIT 34 +#define R_SH_GNU_VTENTRY 35 +#define R_SH_TLS_GD_32 144 +#define R_SH_TLS_LD_32 145 +#define R_SH_TLS_LDO_32 146 +#define R_SH_TLS_IE_32 147 +#define R_SH_TLS_LE_32 148 +#define R_SH_TLS_DTPMOD32 149 +#define R_SH_TLS_DTPOFF32 150 +#define R_SH_TLS_TPOFF32 151 +#define R_SH_GOT32 160 +#define R_SH_PLT32 161 +#define R_SH_COPY 162 +#define R_SH_GLOB_DAT 163 +#define R_SH_JMP_SLOT 164 +#define R_SH_RELATIVE 165 +#define R_SH_GOTOFF 166 +#define R_SH_GOTPC 167 + +#define R_SH_NUM 256 + + + +#define R_390_NONE 0 +#define R_390_8 1 +#define R_390_12 2 +#define R_390_16 3 +#define R_390_32 4 +#define R_390_PC32 5 +#define R_390_GOT12 6 +#define R_390_GOT32 7 +#define R_390_PLT32 8 +#define R_390_COPY 9 +#define R_390_GLOB_DAT 10 +#define R_390_JMP_SLOT 11 +#define R_390_RELATIVE 12 +#define R_390_GOTOFF32 13 +#define R_390_GOTPC 14 +#define R_390_GOT16 15 +#define R_390_PC16 16 +#define R_390_PC16DBL 17 +#define R_390_PLT16DBL 18 +#define R_390_PC32DBL 19 +#define R_390_PLT32DBL 20 +#define R_390_GOTPCDBL 21 +#define R_390_64 22 +#define R_390_PC64 23 +#define R_390_GOT64 24 +#define R_390_PLT64 25 +#define R_390_GOTENT 26 +#define R_390_GOTOFF16 27 +#define R_390_GOTOFF64 28 +#define R_390_GOTPLT12 29 +#define R_390_GOTPLT16 30 +#define R_390_GOTPLT32 31 +#define R_390_GOTPLT64 32 +#define R_390_GOTPLTENT 33 +#define R_390_PLTOFF16 34 +#define R_390_PLTOFF32 35 +#define R_390_PLTOFF64 36 +#define R_390_TLS_LOAD 37 +#define R_390_TLS_GDCALL 38 + +#define R_390_TLS_LDCALL 39 + +#define R_390_TLS_GD32 40 + +#define R_390_TLS_GD64 41 + +#define R_390_TLS_GOTIE12 42 + +#define R_390_TLS_GOTIE32 43 + +#define R_390_TLS_GOTIE64 44 + +#define R_390_TLS_LDM32 45 + +#define R_390_TLS_LDM64 46 + +#define R_390_TLS_IE32 47 + +#define R_390_TLS_IE64 48 + +#define R_390_TLS_IEENT 49 + +#define R_390_TLS_LE32 50 + +#define R_390_TLS_LE64 51 + +#define R_390_TLS_LDO32 52 + +#define R_390_TLS_LDO64 53 + +#define R_390_TLS_DTPMOD 54 +#define R_390_TLS_DTPOFF 55 +#define R_390_TLS_TPOFF 56 + +#define R_390_20 57 +#define R_390_GOT20 58 +#define R_390_GOTPLT20 59 +#define R_390_TLS_GOTIE20 60 + + +#define R_390_NUM 61 + + + +#define R_CRIS_NONE 0 +#define R_CRIS_8 1 +#define R_CRIS_16 2 +#define R_CRIS_32 3 +#define R_CRIS_8_PCREL 4 +#define R_CRIS_16_PCREL 5 +#define R_CRIS_32_PCREL 6 +#define R_CRIS_GNU_VTINHERIT 7 +#define R_CRIS_GNU_VTENTRY 8 +#define R_CRIS_COPY 9 +#define R_CRIS_GLOB_DAT 10 +#define R_CRIS_JUMP_SLOT 11 +#define R_CRIS_RELATIVE 12 +#define R_CRIS_16_GOT 13 +#define R_CRIS_32_GOT 14 +#define R_CRIS_16_GOTPLT 15 +#define R_CRIS_32_GOTPLT 16 +#define R_CRIS_32_GOTREL 17 +#define R_CRIS_32_PLT_GOTREL 18 +#define R_CRIS_32_PLT_PCREL 19 + +#define R_CRIS_NUM 20 + + + +#define R_X86_64_NONE 0 +#define R_X86_64_64 1 +#define R_X86_64_PC32 2 +#define R_X86_64_GOT32 3 +#define R_X86_64_PLT32 4 +#define R_X86_64_COPY 5 +#define R_X86_64_GLOB_DAT 6 +#define R_X86_64_JUMP_SLOT 7 +#define R_X86_64_RELATIVE 8 +#define R_X86_64_GOTPCREL 9 + +#define R_X86_64_32 10 +#define R_X86_64_32S 11 +#define R_X86_64_16 12 +#define R_X86_64_PC16 13 +#define R_X86_64_8 14 +#define R_X86_64_PC8 15 +#define R_X86_64_DTPMOD64 16 +#define R_X86_64_DTPOFF64 17 +#define R_X86_64_TPOFF64 18 +#define R_X86_64_TLSGD 19 + +#define R_X86_64_TLSLD 20 + +#define R_X86_64_DTPOFF32 21 +#define R_X86_64_GOTTPOFF 22 + +#define R_X86_64_TPOFF32 23 +#define R_X86_64_PC64 24 +#define R_X86_64_GOTOFF64 25 +#define R_X86_64_GOTPC32 26 + + +#define R_X86_64_GOTPC32_TLSDESC 34 +#define R_X86_64_TLSDESC_CALL 35 + +#define R_X86_64_TLSDESC 36 +#define R_X86_64_IRELATIVE 37 + +#define R_X86_64_NUM 38 + + + +#define R_MN10300_NONE 0 +#define R_MN10300_32 1 +#define R_MN10300_16 2 +#define R_MN10300_8 3 +#define R_MN10300_PCREL32 4 +#define R_MN10300_PCREL16 5 +#define R_MN10300_PCREL8 6 +#define R_MN10300_GNU_VTINHERIT 7 +#define R_MN10300_GNU_VTENTRY 8 +#define R_MN10300_24 9 +#define R_MN10300_GOTPC32 10 +#define R_MN10300_GOTPC16 11 +#define R_MN10300_GOTOFF32 12 +#define R_MN10300_GOTOFF24 13 +#define R_MN10300_GOTOFF16 14 +#define R_MN10300_PLT32 15 +#define R_MN10300_PLT16 16 +#define R_MN10300_GOT32 17 +#define R_MN10300_GOT24 18 +#define R_MN10300_GOT16 19 +#define R_MN10300_COPY 20 +#define R_MN10300_GLOB_DAT 21 +#define R_MN10300_JMP_SLOT 22 +#define R_MN10300_RELATIVE 23 + +#define R_MN10300_NUM 24 + + + +#define R_M32R_NONE 0 +#define R_M32R_16 1 +#define R_M32R_32 2 +#define R_M32R_24 3 +#define R_M32R_10_PCREL 4 +#define R_M32R_18_PCREL 5 +#define R_M32R_26_PCREL 6 +#define R_M32R_HI16_ULO 7 +#define R_M32R_HI16_SLO 8 +#define R_M32R_LO16 9 +#define R_M32R_SDA16 10 +#define R_M32R_GNU_VTINHERIT 11 +#define R_M32R_GNU_VTENTRY 12 + +#define R_M32R_16_RELA 33 +#define R_M32R_32_RELA 34 +#define R_M32R_24_RELA 35 +#define R_M32R_10_PCREL_RELA 36 +#define R_M32R_18_PCREL_RELA 37 +#define R_M32R_26_PCREL_RELA 38 +#define R_M32R_HI16_ULO_RELA 39 +#define R_M32R_HI16_SLO_RELA 40 +#define R_M32R_LO16_RELA 41 +#define R_M32R_SDA16_RELA 42 +#define R_M32R_RELA_GNU_VTINHERIT 43 +#define R_M32R_RELA_GNU_VTENTRY 44 +#define R_M32R_REL32 45 + +#define R_M32R_GOT24 48 +#define R_M32R_26_PLTREL 49 +#define R_M32R_COPY 50 +#define R_M32R_GLOB_DAT 51 +#define R_M32R_JMP_SLOT 52 +#define R_M32R_RELATIVE 53 +#define R_M32R_GOTOFF 54 +#define R_M32R_GOTPC24 55 +#define R_M32R_GOT16_HI_ULO 56 + +#define R_M32R_GOT16_HI_SLO 57 + +#define R_M32R_GOT16_LO 58 +#define R_M32R_GOTPC_HI_ULO 59 + +#define R_M32R_GOTPC_HI_SLO 60 + +#define R_M32R_GOTPC_LO 61 + +#define R_M32R_GOTOFF_HI_ULO 62 + +#define R_M32R_GOTOFF_HI_SLO 63 + +#define R_M32R_GOTOFF_LO 64 +#define R_M32R_NUM 256 + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/05/musl-final/include/endian.h b/05/musl-final/include/endian.h new file mode 100644 index 0000000..bcf6766 --- /dev/null +++ b/05/musl-final/include/endian.h @@ -0,0 +1,17 @@ +#ifndef _ENDIAN_H +#define _ENDIAN_H + +#define __LITTLE_ENDIAN 1234 +#define __BIG_ENDIAN 4321 +#define __PDP_ENDIAN 3412 + +#include + +#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) +#define BIG_ENDIAN __BIG_ENDIAN +#define LITTLE_ENDIAN __LITTLE_ENDIAN +#define PDP_ENDIAN __PDP_ENDIAN +#define BYTE_ORDER __BYTE_ORDER +#endif + +#endif diff --git a/05/musl-final/include/errno.h b/05/musl-final/include/errno.h new file mode 100644 index 0000000..d9c2c9c --- /dev/null +++ b/05/musl-final/include/errno.h @@ -0,0 +1,18 @@ +#ifndef _ERRNO_H +#define _ERRNO_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +extern int *__errno_location(void); +#define errno (*__errno_location()) + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/05/musl-final/include/fcntl.h b/05/musl-final/include/fcntl.h new file mode 100644 index 0000000..61a6cb5 --- /dev/null +++ b/05/musl-final/include/fcntl.h @@ -0,0 +1,43 @@ +#ifndef _FCNTL_H +#define _FCNTL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_off_t +#define __NEED_pid_t +#define __NEED_mode_t + +#include + +#include + +struct flock +{ + short l_type; + short l_whence; + off_t l_start; + off_t l_len; + pid_t l_pid; +}; + +int creat(const char *, mode_t); +int fcntl(int, int, ...); +int open(const char *, int, ...); +int openat(int, const char *, int, ...); +int posix_fadvise(int, off_t, off_t, int); +int posix_fallocate(int, off_t, off_t); + +#undef SEEK_SET +#undef SEEK_CUR +#undef SEEK_END +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/features.h b/05/musl-final/include/features.h new file mode 100644 index 0000000..851afb6 --- /dev/null +++ b/05/musl-final/include/features.h @@ -0,0 +1 @@ +#warning "features.h is bogus" diff --git a/05/musl-final/include/fenv.h b/05/musl-final/include/fenv.h new file mode 100644 index 0000000..05de990 --- /dev/null +++ b/05/musl-final/include/fenv.h @@ -0,0 +1,28 @@ +#ifndef _FENV_H +#define _FENV_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +int feclearexcept(int); +int fegetexceptflag(fexcept_t *, int); +int feraiseexcept(int); +int fesetexceptflag(const fexcept_t *, int); +int fetestexcept(int); + +int fegetround(void); +int fesetround(int); + +int fegetenv(fenv_t *); +int feholdexcept(fenv_t *); +int fesetenv(const fenv_t *); +int feupdateenv(const fenv_t *); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/05/musl-final/include/float.h b/05/musl-final/include/float.h new file mode 100644 index 0000000..ef91f37 --- /dev/null +++ b/05/musl-final/include/float.h @@ -0,0 +1,34 @@ +#ifndef _FLOAT_H +#define _FLOAT_H + +#define FLT_RADIX 2 + +#define FLT_MIN 1.17549435e-38F +#define FLT_MAX 3.40282347e+38F +#define FLT_EPSILON 1.19209290e-07F + +#define FLT_MANT_DIG 24 +#define FLT_MIN_EXP (-125) +#define FLT_MAX_EXP 128 + +#define FLT_DIG 6 +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_10_EXP 38 + +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_EPSILON 2.2204460492503131e-16 + +#define DBL_MANT_DIG 53 +#define DBL_MIN_EXP (-1021) +#define DBL_MAX_EXP 1024 + +#define DBL_DIG 15 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_10_EXP 308 + +#include + +#define DECIMAL_DIG LDBL_DIG + +#endif diff --git a/05/musl-final/include/fnmatch.h b/05/musl-final/include/fnmatch.h new file mode 100644 index 0000000..b7aee50 --- /dev/null +++ b/05/musl-final/include/fnmatch.h @@ -0,0 +1,25 @@ +#ifndef _FNMATCH_H +#define _FNMATCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define FNM_PATHNAME 0x1 +#define FNM_NOESCAPE 0x2 +#define FNM_PERIOD 0x4 + +#ifdef _GNU_SOURCE +#define FNM_CASEFOLD 0x10 +#endif + +#define FNM_NOMATCH 1 +#define FNM_NOSYS (-1) + +int fnmatch(const char *, const char *, int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/ftw.h b/05/musl-final/include/ftw.h new file mode 100644 index 0000000..8d31c6d --- /dev/null +++ b/05/musl-final/include/ftw.h @@ -0,0 +1,31 @@ +#ifndef _FTW_H +#define _FTW_H + +/* Normally we do not nest header includes. However useless bloat + * like ftw may be treated as a special case. Otherwise we would + * have to deal with duplicating all the stat.h mess. */ +#include + +#define FTW_F 1 +#define FTW_D 2 +#define FTW_DNR 3 +#define FTW_NS 4 +#define FTW_SL 5 +#define FTW_DP 6 +#define FTW_SLN 7 + +#define FTW_PHYS 1 +#define FTW_MOUNT 2 +#define FTW_CHDIR 4 +#define FTW_DEPTH 8 + +struct FTW +{ + int base; + int level; +}; + +int ftw(const char *, int (*)(const char *, const struct stat *, int), int); +int nftw(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int); + +#endif diff --git a/05/musl-final/include/getopt.h b/05/musl-final/include/getopt.h new file mode 100644 index 0000000..0de05ae --- /dev/null +++ b/05/musl-final/include/getopt.h @@ -0,0 +1,33 @@ +#ifndef _GETOPT_H +#define _GETOPT_H + +#ifdef __cplusplus +extern "C" { +#endif + +int getopt(int, char * const [], const char *); +extern char *optarg; +extern int optind, opterr, optopt; + +#ifdef _GNU_SOURCE +struct option +{ + const char *name; + int has_arg; + int *flag; + int val; +}; + +int getopt_long(int, char *const *, const char *, const struct option *, int *); +int getopt_long_only(int, char *const *, const char *, const struct option *, int *); + +#define no_argument 0 +#define required_argument 1 +#define optional_argument 2 +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/glob.h b/05/musl-final/include/glob.h new file mode 100644 index 0000000..185912d --- /dev/null +++ b/05/musl-final/include/glob.h @@ -0,0 +1,41 @@ +#ifndef _GLOB_H +#define _GLOB_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t + +#include + +typedef struct { + size_t gl_pathc; + char **gl_pathv; + size_t gl_offs; + int __dummy1; + void *__dummy2[5]; +} glob_t; + +int glob(const char *, int, int (*)(const char *, int), glob_t *); +void globfree(glob_t *); + +#define GLOB_ERR 0x01 +#define GLOB_MARK 0x02 +#define GLOB_NOSORT 0x04 +#define GLOB_DOOFFS 0x08 +#define GLOB_NOCHECK 0x10 +#define GLOB_APPEND 0x20 +#define GLOB_NOESCAPE 0x40 +#define GLOB_PERIOD 0x80 + +#define GLOB_NOSPACE 1 +#define GLOB_ABORTED 2 +#define GLOB_NOMATCH 3 +#define GLOB_NOSYS 4 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/grp.h b/05/musl-final/include/grp.h new file mode 100644 index 0000000..d4a3526 --- /dev/null +++ b/05/musl-final/include/grp.h @@ -0,0 +1,35 @@ +#ifndef _GRP_H +#define _GRP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t +#define __NEED_gid_t + +#include + +struct group +{ + char *gr_name; + char *gr_passwd; + gid_t gr_gid; + char **gr_mem; +}; + +struct group *getgrgid(gid_t); +struct group *getgrnam(const char *); + +int getgrgid_r(gid_t, struct group *, char *, size_t, struct group **); +int getgrnam_r(const char *, struct group *, char *, size_t, struct group **); + +struct group *getgrent(void); +void endgrent(void); +void setgrent(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/iconv.h b/05/musl-final/include/iconv.h new file mode 100644 index 0000000..f2ccaf8 --- /dev/null +++ b/05/musl-final/include/iconv.h @@ -0,0 +1,22 @@ +#ifndef _ICONV_H +#define _ICONV_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t + +#include + +typedef void *iconv_t; + +iconv_t iconv_open(const char *, const char *); +size_t iconv(iconv_t, char **, size_t *, char **, size_t *); +int iconv_close(iconv_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/inttypes.h b/05/musl-final/include/inttypes.h new file mode 100644 index 0000000..13ba6e0 --- /dev/null +++ b/05/musl-final/include/inttypes.h @@ -0,0 +1,227 @@ +#ifndef _INTTYPES_H +#define _INTTYPES_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define __NEED_wchar_t +#include + +typedef struct { intmax_t quot, rem; } imaxdiv_t; + +intmax_t imaxabs(intmax_t); +imaxdiv_t imaxdiv(intmax_t, intmax_t); + +intmax_t strtoimax(const char *, char **, int); +uintmax_t strtoumax(const char *, char **, int); + +intmax_t wcstoimax(const wchar_t *, wchar_t **, int); +uintmax_t wcstoumax(const wchar_t *, wchar_t **, int); + +#if !defined __cplusplus || defined __STDC_FORMAT_MACROS + +#define __PRI64 "ll" +#define __PRIPTR "l" + +#define PRId8 "d" +#define PRId16 "d" +#define PRId32 "d" +#define PRId64 __PRI64 "d" + +#define PRIdLEAST8 "d" +#define PRIdLEAST16 "d" +#define PRIdLEAST32 "d" +#define PRIdLEAST64 __PRI64 "d" + +#define PRIdFAST8 "d" +#define PRIdFAST16 "d" +#define PRIdFAST32 "d" +#define PRIdFAST64 __PRI64 "d" + +#define PRIi8 "i" +#define PRIi16 "i" +#define PRIi32 "i" +#define PRIi64 __PRI64 "i" + +#define PRIiLEAST8 "i" +#define PRIiLEAST16 "i" +#define PRIiLEAST32 "i" +#define PRIiLEAST64 __PRI64 "i" + +#define PRIiFAST8 "i" +#define PRIiFAST16 "i" +#define PRIiFAST32 "i" +#define PRIiFAST64 __PRI64 "i" + +#define PRIo8 "o" +#define PRIo16 "o" +#define PRIo32 "o" +#define PRIo64 __PRI64 "o" + +#define PRIoLEAST8 "o" +#define PRIoLEAST16 "o" +#define PRIoLEAST32 "o" +#define PRIoLEAST64 __PRI64 "o" + +#define PRIoFAST8 "o" +#define PRIoFAST16 "o" +#define PRIoFAST32 "o" +#define PRIoFAST64 __PRI64 "o" + +#define PRIu8 "u" +#define PRIu16 "u" +#define PRIu32 "u" +#define PRIu64 __PRI64 "u" + +#define PRIuLEAST8 "u" +#define PRIuLEAST16 "u" +#define PRIuLEAST32 "u" +#define PRIuLEAST64 __PRI64 "u" + +#define PRIuFAST8 "u" +#define PRIuFAST16 "u" +#define PRIuFAST32 "u" +#define PRIuFAST64 __PRI64 "u" + +#define PRIx8 "x" +#define PRIx16 "x" +#define PRIx32 "x" +#define PRIx64 __PRI64 "x" + +#define PRIxLEAST8 "x" +#define PRIxLEAST16 "x" +#define PRIxLEAST32 "x" +#define PRIxLEAST64 __PRI64 "x" + +#define PRIxFAST8 "x" +#define PRIxFAST16 "x" +#define PRIxFAST32 "x" +#define PRIxFAST64 __PRI64 "x" + +#define PRIX8 "X" +#define PRIX16 "X" +#define PRIX32 "X" +#define PRIX64 __PRI64 "X" + +#define PRIXLEAST8 "X" +#define PRIXLEAST16 "X" +#define PRIXLEAST32 "X" +#define PRIXLEAST64 __PRI64 "X" + +#define PRIXFAST8 "X" +#define PRIXFAST16 "X" +#define PRIXFAST32 "X" +#define PRIXFAST64 __PRI64 "X" + +#define PRIdMAX __PRI64 "d" +#define PRIiMAX __PRI64 "i" +#define PRIoMAX __PRI64 "o" +#define PRIuMAX __PRI64 "u" +#define PRIxMAX __PRI64 "x" +#define PRIXMAX __PRI64 "X" + +#define PRIdPTR __PRIPTR "d" +#define PRIiPTR __PRIPTR "i" +#define PRIoPTR __PRIPTR "o" +#define PRIuPTR __PRIPTR "u" +#define PRIxPTR __PRIPTR "x" +#define PRIXPTR __PRIPTR "X" + +#define SCNd8 "hhd" +#define SCNd16 "hd" +#define SCNd32 "d" +#define SCNd64 __PRI64 "d" + +#define SCNdLEAST8 "hhd" +#define SCNdLEAST16 "hd" +#define SCNdLEAST32 "d" +#define SCNdLEAST64 __PRI64 "d" + +#define SCNdFAST8 "hhd" +#define SCNdFAST16 __PRIPTR "d" +#define SCNdFAST32 __PRIPTR "d" +#define SCNdFAST64 __PRI64 "d" + +#define SCNi8 "hhi" +#define SCNi16 "hi" +#define SCNi32 "i" +#define SCNi64 __PRI64 "i" + +#define SCNiLEAST8 "hhi" +#define SCNiLEAST16 "hi" +#define SCNiLEAST32 "i" +#define SCNiLEAST64 __PRI64 "i" + +#define SCNiFAST8 "hhi" +#define SCNiFAST16 __PRIPTR "i" +#define SCNiFAST32 __PRIPTR "i" +#define SCNiFAST64 __PRI64 "i" + +#define SCNu8 "hhu" +#define SCNu16 "hu" +#define SCNu32 "u" +#define SCNu64 __PRI64 "u" + +#define SCNuLEAST8 "hhu" +#define SCNuLEAST16 "hu" +#define SCNuLEAST32 "u" +#define SCNuLEAST64 __PRI64 "u" + +#define SCNuFAST8 "hhu" +#define SCNuFAST16 __PRIPTR "u" +#define SCNuFAST32 __PRIPTR "u" +#define SCNuFAST64 __PRI64 "u" + +#define SCNo8 "hho" +#define SCNo16 "ho" +#define SCNo32 "o" +#define SCNo64 __PRI64 "o" + +#define SCNoLEAST8 "hho" +#define SCNoLEAST16 "ho" +#define SCNoLEAST32 "o" +#define SCNoLEAST64 __PRI64 "o" + +#define SCNoFAST8 "hho" +#define SCNoFAST16 __PRIPTR "o" +#define SCNoFAST32 __PRIPTR "o" +#define SCNoFAST64 __PRI64 "o" + +#define SCNx8 "hhx" +#define SCNx16 "hx" +#define SCNx32 "x" +#define SCNx64 __PRI64 "x" + +#define SCNxLEAST8 "hhx" +#define SCNxLEAST16 "hx" +#define SCNxLEAST32 "x" +#define SCNxLEAST64 __PRI64 "x" + +#define SCNxFAST8 "hhx" +#define SCNxFAST16 __PRIPTR "x" +#define SCNxFAST32 __PRIPTR "x" +#define SCNxFAST64 __PRI64 "x" + +#define SCNdMAX __PRI64 "d" +#define SCNiMAX __PRI64 "i" +#define SCNoMAX __PRI64 "o" +#define SCNuMAX __PRI64 "u" +#define SCNxMAX __PRI64 "x" + +#define SCNdPTR __PRIPTR "d" +#define SCNiPTR __PRIPTR "i" +#define SCNoPTR __PRIPTR "o" +#define SCNuPTR __PRIPTR "u" +#define SCNxPTR __PRIPTR "x" + +#endif + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/05/musl-final/include/iso646.h b/05/musl-final/include/iso646.h new file mode 100644 index 0000000..88ff53d --- /dev/null +++ b/05/musl-final/include/iso646.h @@ -0,0 +1,20 @@ +#ifndef _ISO646_H +#define _ISO646_H + +#ifndef __cplusplus + +#define and && +#define and_eq &= +#define bitand & +#define bitor | +#define compl ~ +#define not ! +#define not_eq != +#define or || +#define or_eq |= +#define xor ^ +#define xor_eq ^= + +#endif + +#endif diff --git a/05/musl-final/include/langinfo.h b/05/musl-final/include/langinfo.h new file mode 100644 index 0000000..b9c5569 --- /dev/null +++ b/05/musl-final/include/langinfo.h @@ -0,0 +1,87 @@ +#ifndef _LANGINFO_H +#define _LANGINFO_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_nl_item +#define __NEED_locale_t + +#include + +#define ABDAY_1 0x20000 +#define ABDAY_2 0x20001 +#define ABDAY_3 0x20002 +#define ABDAY_4 0x20003 +#define ABDAY_5 0x20004 +#define ABDAY_6 0x20005 +#define ABDAY_7 0x20006 + +#define DAY_1 0x20007 +#define DAY_2 0x20008 +#define DAY_3 0x20009 +#define DAY_4 0x2000A +#define DAY_5 0x2000B +#define DAY_6 0x2000C +#define DAY_7 0x2000D + +#define ABMON_1 0x2000E +#define ABMON_2 0x2000F +#define ABMON_3 0x20010 +#define ABMON_4 0x20011 +#define ABMON_5 0x20012 +#define ABMON_6 0x20013 +#define ABMON_7 0x20014 +#define ABMON_8 0x20015 +#define ABMON_9 0x20016 +#define ABMON_10 0x20017 +#define ABMON_11 0x20018 +#define ABMON_12 0x20019 + +#define MON_1 0x2001A +#define MON_2 0x2001B +#define MON_3 0x2001C +#define MON_4 0x2001D +#define MON_5 0x2001E +#define MON_6 0x2001F +#define MON_7 0x20020 +#define MON_8 0x20021 +#define MON_9 0x20022 +#define MON_10 0x20023 +#define MON_11 0x20024 +#define MON_12 0x20025 + +#define AM_STR 0x20026 +#define PM_STR 0x20027 + +#define D_T_FMT 0x20028 +#define D_FMT 0x20029 +#define T_FMT 0x2002A +#define T_FMT_AMPM 0x2002B + +#define ERA 0x2002C +#define ERA_D_FMT 0x2002E +#define ALT_DIGITS 0x2002F +#define ERA_D_T_FMT 0x20030 +#define ERA_T_FMT 0x20031 + +#define CODESET 14 + +#define CRNCYSTR 0x4000F + +#define RADIXCHAR 0x10000 +#define THOUSEP 0x10001 +#define YESEXPR 0x50000 +#define NOEXPR 0x50001 +#define YESSTR 0x50002 +#define NOSTR 0x50003 + +char *nl_langinfo(nl_item); +char *nl_langinfo_l(nl_item, locale_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/libgen.h b/05/musl-final/include/libgen.h new file mode 100644 index 0000000..7c7fd9c --- /dev/null +++ b/05/musl-final/include/libgen.h @@ -0,0 +1,15 @@ +#ifndef _LIBGEN_H +#define _LIBGEN_H + +#ifdef __cplusplus +extern "C" { +#endif + +char *dirname(char *); +char *basename(char *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/libintl.h b/05/musl-final/include/libintl.h new file mode 100644 index 0000000..a2dada6 --- /dev/null +++ b/05/musl-final/include/libintl.h @@ -0,0 +1,25 @@ +#ifndef _LIBINTL_H +#define _LIBINTL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __USE_GNU_GETTEXT 1 +#define __GNU_GETTEXT_SUPPORTED_REVISION(major) ((major) == 0 ? 1 : -1) + +char *gettext(const char *); +char *dgettext(const char *, const char *); +char *dcgettext(const char *, const char *, int); +char *ngettext(const char *, const char *, unsigned long); +char *dngettext(const char *, const char *, const char *, unsigned long); +char *dcngettext(const char *, const char *, const char *, unsigned long, int); +char *textdomain(const char *); +char *bindtextdomain (const char *, const char *); +char *bind_textdomain_codeset(const char *, const char *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/limits.h b/05/musl-final/include/limits.h new file mode 100644 index 0000000..6757012 --- /dev/null +++ b/05/musl-final/include/limits.h @@ -0,0 +1,109 @@ +#ifndef _LIMITS_H +#define _LIMITS_H + +/* Most limits are system-specific */ + +#include + +/* Some universal constants... */ + +#define CHAR_BIT 8 +#define SCHAR_MIN (-128) +#define SCHAR_MAX 127 +#define UCHAR_MAX 255 +#define CHAR_MIN (-128) +#define CHAR_MAX 127 + +#define MB_LEN_MAX 4 + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +#define SSIZE_MAX LONG_MAX +#define TZNAME_MAX 6 +#define TTY_NAME_MAX 20 +#define HOST_NAME_MAX 255 + +/* Implementation choices... */ + +#define PTHREAD_KEYS_MAX 1024 +#define PTHREAD_STACK_MIN PAGE_SIZE +#define PTHREAD_DESTRUCTOR_ITERATIONS 4 + +/* Arbitrary numbers... */ + +#define BC_BASE_MAX 99 +#define BC_DIM_MAX 2048 +#define BC_SCALE_MAX 99 +#define BC_STRING_MAX 1000 +#define CHARCLASS_NAME_MAX 14 +#define COLL_WEIGHTS_MAX 2 +#define EXPR_NEST_MAX 32 +#define LINE_MAX 4096 +#define RE_DUP_MAX 255 + +#define NL_ARGMAX 9 +#define NL_LANGMAX 32 +#define NL_MSGMAX 32767 +#define NL_NMAX (MB_LEN_MAX*4) +#define NL_SETMAX 255 +#define NL_TEXTMAX 2048 + +#endif + +/* POSIX/SUS requirements follow. These numbers come directly + * from SUS and have nothing to do with the host system. */ + +#define _POSIX_AIO_LISTIO_MAX 2 +#define _POSIX_AIO_MAX 1 +#define _POSIX_ARG_MAX 4096 +#define _POSIX_CHILD_MAX 25 +#define _POSIX_CLOCKRES_MIN 20000000 +#define _POSIX_DELAYTIMER_MAX 32 +#define _POSIX_HOST_NAME_MAX 255 +#define _POSIX_LINK_MAX 8 +#define _POSIX_LOGIN_NAME_MAX 9 +#define _POSIX_MAX_CANON 255 +#define _POSIX_MAX_INPUT 255 +#define _POSIX_MQ_OPEN_MAX 8 +#define _POSIX_MQ_PRIO_MAX 32 +#define _POSIX_NAME_MAX 14 +#define _POSIX_NGROUPS_MAX 8 +#define _POSIX_OPEN_MAX 20 +#define _POSIX_PATH_MAX 256 +#define _POSIX_PIPE_BUF 512 +#define _POSIX_RE_DUP_MAX 255 +#define _POSIX_RTSIG_MAX 8 +#define _POSIX_SEM_NSEMS_MAX 256 +#define _POSIX_SEM_VALUE_MAX 32767 +#define _POSIX_SIGQUEUE_MAX 32 +#define _POSIX_SSIZE_MAX 32767 +#define _POSIX_STREAM_MAX 8 +#define _POSIX_SS_REPL_MAX 4 +#define _POSIX_SYMLINK_MAX 255 +#define _POSIX_SYMLOOP_MAX 8 +#define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4 +#define _POSIX_THREAD_KEYS_MAX 128 +#define _POSIX_THREAD_THREADS_MAX 64 +#define _POSIX_TIMER_MAX 32 +#define _POSIX_TRACE_EVENT_NAME_MAX 30 +#define _POSIX_TRACE_NAME_MAX 8 +#define _POSIX_TRACE_SYS_MAX 8 +#define _POSIX_TRACE_USER_EVENT_MAX 32 +#define _POSIX_TTY_NAME_MAX 9 +#define _POSIX_TZNAME_MAX 6 +#define _POSIX2_BC_BASE_MAX 99 +#define _POSIX2_BC_DIM_MAX 2048 +#define _POSIX2_BC_SCALE_MAX 99 +#define _POSIX2_BC_STRING_MAX 1000 +#define _POSIX2_CHARCLASS_NAME_MAX 14 +#define _POSIX2_COLL_WEIGHTS_MAX 2 +#define _POSIX2_EXPR_NEST_MAX 32 +#define _POSIX2_LINE_MAX 2048 +#define _POSIX2_RE_DUP_MAX 255 + +#define _XOPEN_IOV_MAX 16 +#define _XOPEN_NAME_MAX 255 +#define _XOPEN_PATH_MAX 1024 + +#endif diff --git a/05/musl-final/include/linux/loop.h b/05/musl-final/include/linux/loop.h new file mode 100644 index 0000000..41aad40 --- /dev/null +++ b/05/musl-final/include/linux/loop.h @@ -0,0 +1,69 @@ +#ifndef _LINUX_LOOP_H +#define _LINUX_LOOP_H + +#include + +#define LO_NAME_SIZE 64 +#define LO_KEY_SIZE 32 + +enum { + LO_FLAGS_READ_ONLY = 1, + LO_FLAGS_USE_AOPS = 2, + LO_FLAGS_AUTOCLEAR = 4, +}; + +struct loop_info { + int lo_number; + long lo_device; + unsigned long lo_inode; + long lo_rdevice; + int lo_offset; + int lo_encrypt_type; + int lo_encrypt_key_size; + int lo_flags; + char lo_name[LO_NAME_SIZE]; + unsigned char lo_encrypt_key[LO_KEY_SIZE]; + unsigned long lo_init[2]; + char reserved[4]; +}; + +struct loop_info64 { + uint64_t lo_device; + uint64_t lo_inode; + uint64_t lo_rdevice; + uint64_t lo_offset; + uint64_t lo_sizelimit; + uint32_t lo_number; + uint32_t lo_encrypt_type; + uint32_t lo_encrypt_key_size; + uint32_t lo_flags; + uint8_t lo_file_name[LO_NAME_SIZE]; + uint8_t lo_crypt_name[LO_NAME_SIZE]; + uint8_t lo_encrypt_key[LO_KEY_SIZE]; + uint64_t lo_init[2]; +}; + + +#define LO_CRYPT_NONE 0 +#define LO_CRYPT_XOR 1 +#define LO_CRYPT_DES 2 +#define LO_CRYPT_FISH2 3 +#define LO_CRYPT_BLOW 4 +#define LO_CRYPT_CAST128 5 +#define LO_CRYPT_IDEA 6 +#define LO_CRYPT_DUMMY 9 +#define LO_CRYPT_SKIPJACK 10 +#define LO_CRYPT_CRYPTOAPI 18 +#define MAX_LO_CRYPT 20 + + +#define LOOP_SET_FD 0x4C00 +#define LOOP_CLR_FD 0x4C01 +#define LOOP_SET_STATUS 0x4C02 +#define LOOP_GET_STATUS 0x4C03 +#define LOOP_SET_STATUS64 0x4C04 +#define LOOP_GET_STATUS64 0x4C05 +#define LOOP_CHANGE_FD 0x4C06 +#define LOOP_SET_CAPACITY 0x4C07 + +#endif diff --git a/05/musl-final/include/linux/version.h b/05/musl-final/include/linux/version.h new file mode 100644 index 0000000..e6a89ef --- /dev/null +++ b/05/musl-final/include/linux/version.h @@ -0,0 +1,2 @@ +#define LINUX_VERSION_CODE 132638 +#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) diff --git a/05/musl-final/include/locale.h b/05/musl-final/include/locale.h new file mode 100644 index 0000000..9da4726 --- /dev/null +++ b/05/musl-final/include/locale.h @@ -0,0 +1,85 @@ +#ifndef _LOCALE_H +#define _LOCALE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#define LC_CTYPE 0 +#define LC_NUMERIC 1 +#define LC_TIME 2 +#define LC_COLLATE 3 +#define LC_MONETARY 4 +#define LC_MESSAGES 5 +#define LC_ALL 6 + +struct lconv { + char *decimal_point; + char *thousands_sep; + char *grouping; + + char *int_curr_symbol; + char *currency_symbol; + char *mon_decimal_point; + char *mon_thousands_sep; + char *mon_grouping; + char *positive_sign; + char *negative_sign; + char int_frac_digits; + char frac_digits; + char p_cs_precedes; + char p_sep_by_space; + char n_cs_precedes; + char n_sep_by_space; + char p_sign_posn; + char n_sign_posn; + char int_p_cs_precedes; + char int_p_sep_by_space; + char int_n_cs_precedes; + char int_n_sep_by_space; + char int_p_sign_posn; + char int_n_sign_posn; +}; + + +char *setlocale (int, const char *); +struct lconv *localeconv(void); + + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +#define __NEED_locale_t + +#include + +#define LC_GLOBAL_LOCALE ((locale_t)-1) + +#define LC_CTYPE_MASK (1< + +#define __MAKE_FLOAT(i) (((union { int __i; float __f; }){ .__i = i }).__f) + +#define NAN __MAKE_FLOAT(0x7fc00000) +#define INFINITY __MAKE_FLOAT(0x7f800000) + +#define HUGE_VALF INFINITY +#define HUGE_VAL ((double)INFINITY) +#define HUGE_VALL ((long double)INFINITY) + +#define MAXFLOAT __MAKE_FLOAT(0x7f7fffff) + +#define MATH_ERRNO 1 +#define MATH_EXCEPT 2 +#define math_errhandling 2 + +#define FP_ILOGBNAN (((unsigned)-1)>>1) +#define FP_ILOGB0 (~FP_ILOGBNAN) + +#define FP_NAN 0 +#define FP_INFINITE 1 +#define FP_ZERO 2 +#define FP_SUBNORMAL 3 +#define FP_NORMAL 4 + +int __fpclassifyf(float); +int __fpclassify(double); +int __fpclassifyl(long double); + +#define fpclassify(x) ( \ + sizeof(x) == sizeof(float) ? __fpclassifyf(x) : \ + sizeof(x) == sizeof(double) ? __fpclassify(x) : \ + __fpclassifyl(x) ) + +#define isinf(x) (fpclassify(x) == FP_INFINITE) +#define isnan(x) (fpclassify(x) == FP_NAN) +#define isnormal(x) (fpclassify(x) == FP_NORMAL) +#define isfinite(x) (fpclassify(x) > FP_INFINITE) + +double acos(double); +float acosf(float); +long double acosl(long double); + +double acosh(double); +float acoshf(float); +long double acoshl(long double); + +double asin(double); +float asinf(float); +long double asinl(long double); + +double asinh(double); +float asinhf(float); +long double asinhl(long double); + +double atan(double); +float atanf(float); +long double atanl(long double); + +double atan2(double, double); +float atan2f(float, float); +long double atan2l(long double, long double); + +double atanh(double); +float atanhf(float); +long double atanhl(long double); + +double cbrt(double); +float cbrtf(float); +long double cbrtl(long double); + +double ceil(double); +float ceilf(float); +long double ceill(long double); + +double copysign(double, double); +float copysignf(float, float); +long double copysignl(long double, long double); + +double cos(double); +float cosf(float); +long double cosl(long double); + +double cosh(double); +float coshf(float); +long double coshl(long double); + +double erf(double); +float erff(float); +long double erfl(long double); + +double erfc(double); +float erfcf(float); +long double erfcl(long double); + +double exp(double); +float expf(float); +long double expl(long double); + +double exp2(double); +float exp2f(float); +long double exp2l(long double); + +double expm1(double); +float expm1f(float); +long double expm1l(long double); + +double fabs(double); +float fabsf(float); +long double fabsl(long double); + +double fdim(double, double); +float fdimf(float, float); +long double fdiml(long double, long double); + +double floor(double); +float floorf(float); +long double floorl(long double); + +double fma(double, double, double); +float fmaf(float, float, float); +long double fmal(long double, long double, long double); + +double fmax(double, double); +float fmaxf(float, float); +long double fmaxl(long double, long double); + +double fmin(double, double); +float fminf(float, float); +long double fminl(long double, long double); + +double fmod(double, double); +float fmodf(float, float); +long double fmodl(long double, long double); + +double frexp(double, int *); +float frexpf(float value, int *); +long double frexpl(long double value, int *); + +double hypot(double, double); +float hypotf(float, float); +long double hypotl(long double, long double); + +int ilogb(double); +int ilogbf(float); +int ilogbl(long double); + +double ldexp(double, int); +float ldexpf(float, int); +long double ldexpl(long double, int); + +double lgamma(double); +float lgammaf(float); +long double lgammal(long double); + +long long llrint(double); +long long llrintf(float); +long long llrintl(long double); + +long long llround(double); +long long llroundf(float); +long long llroundl(long double); + +double log(double); +float logf(float); +long double logl(long double); + +double log10(double); +float log10f(float); +long double log10l(long double); + +double log1p(double); +float log1pf(float); +long double log1pl(long double); + +double log2(double); +float log2f(float); +long double log2l(long double); + +double logb(double); +float logbf(float); +long double logbl(long double); + +long lrint(double); +long lrintf(float); +long lrintl(long double); + +long lround(double); +long lroundf(float); +long lroundl(long double); + +double modf(double, double *); +float modff(float, float *); +long double modfl(long double, long double *); + +double nan(const char *); +float nanf(const char *); +long double nanl(const char *); + +double nearbyint(double); +float nearbyintf(float); +long double nearbyintl(long double); + +double nextafter(double, double); +float nextafterf(float, float); +long double nextafterl(long double, long double); + +double nexttoward(double, long double); +float nexttowardf(float, long double); +long double nexttowardl(long double, long double); + +double pow(double, double); +float powf(float, float); +long double powl(long double, long double); + +double remainder(double, double); +float remainderf(float, float); +long double remainderl(long double, long double); + +double remquo(double, double, int *); +float remquof(float, float, int *); +long double remquol(long double, long double, int *); + +double rint(double); +float rintf(float); +long double rintl(long double); + +double round(double); +float roundf(float); +long double roundl(long double); + +double scalbln(double, long); +float scalblnf(float, long); +long double scalblnl(long double, long); + +double scalbn(double, int); +float scalbnf(float, int); +long double scalbnl(long double, int); + +double sin(double); +float sinf(float); +long double sinl(long double); + +double sinh(double); +float sinhf(float); +long double sinhl(long double); + +double sqrt(double); +float sqrtf(float); +long double sqrtl(long double); + +double tan(double); +float tanf(float); +long double tanl(long double); + +double tanh(double); +float tanhf(float); +long double tanhl(long double); + +double tgamma(double); +float tgammaf(float); +long double tgammal(long double); + +double trunc(double); +float truncf(float); +long double truncl(long double); + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define M_E 2.7182818284590452354 /* e */ +#define M_LOG2E 1.4426950408889634074 /* log_2 e */ +#define M_LOG10E 0.43429448190325182765 /* log_10 e */ +#define M_LN2 0.69314718055994530942 /* log_e 2 */ +#define M_LN10 2.30258509299404568402 /* log_e 10 */ +#define M_PI 3.14159265358979323846 /* pi */ +#define M_PI_2 1.57079632679489661923 /* pi/2 */ +#define M_PI_4 0.78539816339744830962 /* pi/4 */ +#define M_1_PI 0.31830988618379067154 /* 1/pi */ +#define M_2_PI 0.63661977236758134308 /* 2/pi */ +#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ +#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ +#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ +double j0(double); +double j1(double); +double jn(int, double); +double y0(double); +double y1(double); +double yn(int, double); +extern int signgam; +#endif + +#ifdef _GNU_SOURCE +double scalb(double, double); +#endif + +#endif diff --git a/05/musl-final/include/mntent.h b/05/musl-final/include/mntent.h new file mode 100644 index 0000000..c0fed7f --- /dev/null +++ b/05/musl-final/include/mntent.h @@ -0,0 +1,43 @@ +#ifndef _MNTENT_H +#define _MNTENT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_FILE +#include + +#define MOUNTED "/etc/mtab" + +#define MNTTYPE_IGNORE "ignore" +#define MNTTYPE_NFS "nfs" +#define MNTTYPE_SWAP "swap" +#define MNTOPT_DEFAULTS "defaults" +#define MNTOPT_RO "ro" +#define MNTOPT_RW "rw" +#define MNTOPT_SUID "suid" +#define MNTOPT_NOSUID "nosuid" +#define MNTOPT_NOAUTO "noauto" + +struct mntent +{ + char *mnt_fsname; + char *mnt_dir; + char *mnt_type; + char *mnt_opts; + int mnt_freq; + int mnt_passno; +}; + +FILE *setmntent(const char *, const char *); +int endmntent(FILE *); +struct mntent *getmntent(FILE *); +int addmntent(FILE *, const struct mntent *); +char *hasmntopt(const struct mntent *, const char *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/net/ethernet.h b/05/musl-final/include/net/ethernet.h new file mode 100644 index 0000000..a536502 --- /dev/null +++ b/05/musl-final/include/net/ethernet.h @@ -0,0 +1,47 @@ +#ifndef __NET_ETHERNET_H +#define __NET_ETHERNET_H 1 + +#include +#include +#include + +struct ether_addr { + uint8_t ether_addr_octet[ETH_ALEN]; +}; + +struct ether_header { + uint8_t ether_dhost[ETH_ALEN]; + uint8_t ether_shost[ETH_ALEN]; + uint16_t ether_type; +}; + +#define ETHERTYPE_PUP 0x0200 +#define ETHERTYPE_SPRITE 0x0500 +#define ETHERTYPE_IP 0x0800 +#define ETHERTYPE_ARP 0x0806 +#define ETHERTYPE_REVARP 0x8035 +#define ETHERTYPE_AT 0x809B +#define ETHERTYPE_AARP 0x80F3 +#define ETHERTYPE_VLAN 0x8100 +#define ETHERTYPE_IPX 0x8137 +#define ETHERTYPE_IPV6 0x86dd +#define ETHERTYPE_LOOPBACK 0x9000 + + +#define ETHER_ADDR_LEN ETH_ALEN +#define ETHER_TYPE_LEN 2 +#define ETHER_CRC_LEN 4 +#define ETHER_HDR_LEN ETH_HLEN +#define ETHER_MIN_LEN (ETH_ZLEN + ETHER_CRC_LEN) +#define ETHER_MAX_LEN (ETH_FRAME_LEN + ETHER_CRC_LEN) + +#define ETHER_IS_VALID_LEN(foo) \ + ((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN) + +#define ETHERTYPE_TRAIL 0x1000 +#define ETHERTYPE_NTRAILER 16 + +#define ETHERMTU ETH_DATA_LEN +#define ETHERMIN (ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN) + +#endif diff --git a/05/musl-final/include/net/if.h b/05/musl-final/include/net/if.h new file mode 100644 index 0000000..2ae5f89 --- /dev/null +++ b/05/musl-final/include/net/if.h @@ -0,0 +1,122 @@ +#ifndef _NET_IF_H +#define _NET_IF_H + +#define IF_NAMESIZE 16 + +struct if_nameindex +{ + unsigned int if_index; + char *if_name; +}; + +unsigned int if_nametoindex (const char *); +char *if_indextoname (unsigned int, char *); +struct if_nameindex *if_nameindex (void); +void if_freenameindex (struct if_nameindex *); + + + + +#ifdef _GNU_SOURCE + +#include + +#define IFF_UP 0x1 +#define IFF_BROADCAST 0x2 +#define IFF_DEBUG 0x4 +#define IFF_LOOPBACK 0x8 +#define IFF_POINTOPOINT 0x10 +#define IFF_NOTRAILERS 0x20 +#define IFF_RUNNING 0x40 +#define IFF_NOARP 0x80 +#define IFF_PROMISC 0x100 +#define IFF_ALLMULTI 0x200 +#define IFF_MASTER 0x400 +#define IFF_SLAVE 0x800 +#define IFF_MULTICAST 0x1000 +#define IFF_PORTSEL 0x2000 +#define IFF_AUTOMEDIA 0x4000 +#define IFF_DYNAMIC 0x8000 + +struct ifaddr { + struct sockaddr ifa_addr; + union { + struct sockaddr ifu_broadaddr; + struct sockaddr ifu_dstaddr; + } ifa_ifu; + struct iface *ifa_ifp; + struct ifaddr *ifa_next; +}; + +#define ifa_broadaddr ifa_ifu.ifu_broadaddr +#define ifa_dstaddr ifa_ifu.ifu_dstaddr + +struct ifmap { + unsigned long int mem_start; + unsigned long int mem_end; + unsigned short int base_addr; + unsigned char irq; + unsigned char dma; + unsigned char port; +}; + +#define IFHWADDRLEN 6 +#define IFNAMSIZ IF_NAMESIZE + +struct ifreq { + union { + char ifrn_name[IFNAMSIZ]; + } ifr_ifrn; + union { + struct sockaddr ifru_addr; + struct sockaddr ifru_dstaddr; + struct sockaddr ifru_broadaddr; + struct sockaddr ifru_netmask; + struct sockaddr ifru_hwaddr; + short int ifru_flags; + int ifru_ivalue; + int ifru_mtu; + struct ifmap ifru_map; + char ifru_slave[IFNAMSIZ]; + char ifru_newname[IFNAMSIZ]; + void *ifru_data; + } ifr_ifru; +}; + +#define ifr_name ifr_ifrn.ifrn_name +#define ifr_hwaddr ifr_ifru.ifru_hwaddr +#define ifr_addr ifr_ifru.ifru_addr +#define ifr_dstaddr ifr_ifru.ifru_dstaddr +#define ifr_broadaddr ifr_ifru.ifru_broadaddr +#define ifr_netmask ifr_ifru.ifru_netmask +#define ifr_flags ifr_ifru.ifru_flags +#define ifr_metric ifr_ifru.ifru_ivalue +#define ifr_mtu ifr_ifru.ifru_mtu +#define ifr_map ifr_ifru.ifru_map +#define ifr_slave ifr_ifru.ifru_slave +#define ifr_data ifr_ifru.ifru_data +#define ifr_ifindex ifr_ifru.ifru_ivalue +#define ifr_bandwidth ifr_ifru.ifru_ivalue +#define ifr_qlen ifr_ifru.ifru_ivalue +#define ifr_newname ifr_ifru.ifru_newname +#define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0) +#define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0) +#define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0) + +struct ifconf { + int ifc_len; + union { + void *ifcu_buf; + struct ifreq *ifcu_req; + } ifc_ifcu; +}; + +#define ifc_buf ifc_ifcu.ifcu_buf +#define ifc_req ifc_ifcu.ifcu_req +#define _IOT_ifconf _IOT(_IOTS(struct ifconf),1,0,0,0,0) + +#endif + + + +#endif diff --git a/05/musl-final/include/net/if_arp.h b/05/musl-final/include/net/if_arp.h new file mode 100644 index 0000000..bb5eadf --- /dev/null +++ b/05/musl-final/include/net/if_arp.h @@ -0,0 +1,133 @@ +/* Nonstandard header */ +#ifndef _NET_IF_ARP_H +#define _NET_IF_ARP_H +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#define MAX_ADDR_LEN 7 + +#define ARPOP_REQUEST 1 +#define ARPOP_REPLY 2 +#define ARPOP_RREQUEST 3 +#define ARPOP_RREPLY 4 +#define ARPOP_InREQUEST 8 +#define ARPOP_InREPLY 9 +#define ARPOP_NAK 10 + +struct arphdr { + uint16_t ar_hrd; + uint16_t ar_pro; + uint8_t ar_hln; + uint8_t ar_pln; + uint16_t ar_op; +}; + + +#define ARPHRD_NETROM 0 +#define ARPHRD_ETHER 1 +#define ARPHRD_EETHER 2 +#define ARPHRD_AX25 3 +#define ARPHRD_PRONET 4 +#define ARPHRD_CHAOS 5 +#define ARPHRD_IEEE802 6 +#define ARPHRD_ARCNET 7 +#define ARPHRD_APPLETLK 8 +#define ARPHRD_DLCI 15 +#define ARPHRD_ATM 19 +#define ARPHRD_METRICOM 23 +#define ARPHRD_IEEE1394 24 +#define ARPHRD_EUI64 27 +#define ARPHRD_INFINIBAND 32 +#define ARPHRD_SLIP 256 +#define ARPHRD_CSLIP 257 +#define ARPHRD_SLIP6 258 +#define ARPHRD_CSLIP6 259 +#define ARPHRD_RSRVD 260 +#define ARPHRD_ADAPT 264 +#define ARPHRD_ROSE 270 +#define ARPHRD_X25 271 +#define ARPHRD_HWX25 272 +#define ARPHRD_PPP 512 +#define ARPHRD_CISCO 513 +#define ARPHRD_HDLC ARPHRD_CISCO +#define ARPHRD_LAPB 516 +#define ARPHRD_DDCMP 517 +#define ARPHRD_RAWHDLC 518 + +#define ARPHRD_TUNNEL 768 +#define ARPHRD_TUNNEL6 769 +#define ARPHRD_FRAD 770 +#define ARPHRD_SKIP 771 +#define ARPHRD_LOOPBACK 772 +#define ARPHRD_LOCALTLK 773 +#define ARPHRD_FDDI 774 +#define ARPHRD_BIF 775 +#define ARPHRD_SIT 776 +#define ARPHRD_IPDDP 777 +#define ARPHRD_IPGRE 778 +#define ARPHRD_PIMREG 779 +#define ARPHRD_HIPPI 780 +#define ARPHRD_ASH 781 +#define ARPHRD_ECONET 782 +#define ARPHRD_IRDA 783 +#define ARPHRD_FCPP 784 +#define ARPHRD_FCAL 785 +#define ARPHRD_FCPL 786 +#define ARPHRD_FCFABRIC 787 +#define ARPHRD_IEEE802_TR 800 +#define ARPHRD_IEEE80211 801 +#define ARPHRD_IEEE80211_PRISM 802 +#define ARPHRD_IEEE80211_RADIOTAP 803 +#define ARPHRD_IEEE802154 804 +#define ARPHRD_IEEE802154_PHY 805 + +#define ARPHRD_VOID 0xFFFF +#define ARPHRD_NONE 0xFFFE + +struct arpreq { + struct sockaddr arp_pa; + struct sockaddr arp_ha; + int arp_flags; + struct sockaddr arp_netmask; + char arp_dev[16]; +}; + +struct arpreq_old { + struct sockaddr arp_pa; + struct sockaddr arp_ha; + int arp_flags; + struct sockaddr arp_netmask; +}; + +#define ATF_COM 0x02 +#define ATF_PERM 0x04 +#define ATF_PUBL 0x08 +#define ATF_USETRAILERS 0x10 +#define ATF_NETMASK 0x20 +#define ATF_DONTPUB 0x40 +#define ATF_MAGIC 0x80 + +#define ARPD_UPDATE 0x01 +#define ARPD_LOOKUP 0x02 +#define ARPD_FLUSH 0x03 + +struct arpd_request { + uint16_t req; + uint32_t ip; + uint32_t dev; + uint32_t stamp; + uint32_t updated; + uint8_t ha[MAX_ADDR_LEN]; +}; + + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/net/route.h b/05/musl-final/include/net/route.h new file mode 100644 index 0000000..3aaab97 --- /dev/null +++ b/05/musl-final/include/net/route.h @@ -0,0 +1,116 @@ +#ifndef _NET_ROUTE_H +#define _NET_ROUTE_H + +#include +#include +#include +#include + + +struct rtentry { + unsigned long int rt_pad1; + struct sockaddr rt_dst; + struct sockaddr rt_gateway; + struct sockaddr rt_genmask; + unsigned short int rt_flags; + short int rt_pad2; + unsigned long int rt_pad3; + unsigned char rt_tos; + unsigned char rt_class; + short int rt_pad4[sizeof(long)/2-1]; + short int rt_metric; + char *rt_dev; + unsigned long int rt_mtu; + unsigned long int rt_window; + unsigned short int rt_irtt; +}; + +#define rt_mss rt_mtu + + +struct in6_rtmsg { + struct in6_addr rtmsg_dst; + struct in6_addr rtmsg_src; + struct in6_addr rtmsg_gateway; + uint32_t rtmsg_type; + uint16_t rtmsg_dst_len; + uint16_t rtmsg_src_len; + uint32_t rtmsg_metric; + unsigned long int rtmsg_info; + uint32_t rtmsg_flags; + int rtmsg_ifindex; +}; + + +#define RTF_UP 0x0001 +#define RTF_GATEWAY 0x0002 + +#define RTF_HOST 0x0004 +#define RTF_REINSTATE 0x0008 +#define RTF_DYNAMIC 0x0010 +#define RTF_MODIFIED 0x0020 +#define RTF_MTU 0x0040 +#define RTF_MSS RTF_MTU +#define RTF_WINDOW 0x0080 +#define RTF_IRTT 0x0100 +#define RTF_REJECT 0x0200 +#define RTF_STATIC 0x0400 +#define RTF_XRESOLVE 0x0800 +#define RTF_NOFORWARD 0x1000 +#define RTF_THROW 0x2000 +#define RTF_NOPMTUDISC 0x4000 + +#define RTF_DEFAULT 0x00010000 +#define RTF_ALLONLINK 0x00020000 +#define RTF_ADDRCONF 0x00040000 + +#define RTF_LINKRT 0x00100000 +#define RTF_NONEXTHOP 0x00200000 + +#define RTF_CACHE 0x01000000 +#define RTF_FLOW 0x02000000 +#define RTF_POLICY 0x04000000 + +#define RTCF_VALVE 0x00200000 +#define RTCF_MASQ 0x00400000 +#define RTCF_NAT 0x00800000 +#define RTCF_DOREDIRECT 0x01000000 +#define RTCF_LOG 0x02000000 +#define RTCF_DIRECTSRC 0x04000000 + +#define RTF_LOCAL 0x80000000 +#define RTF_INTERFACE 0x40000000 +#define RTF_MULTICAST 0x20000000 +#define RTF_BROADCAST 0x10000000 +#define RTF_NAT 0x08000000 + +#define RTF_ADDRCLASSMASK 0xF8000000 +#define RT_ADDRCLASS(flags) ((uint32_t) flags >> 23) + +#define RT_TOS(tos) ((tos) & IPTOS_TOS_MASK) + +#define RT_LOCALADDR(flags) ((flags & RTF_ADDRCLASSMASK) \ + == (RTF_LOCAL|RTF_INTERFACE)) + +#define RT_CLASS_UNSPEC 0 +#define RT_CLASS_DEFAULT 253 + +#define RT_CLASS_MAIN 254 +#define RT_CLASS_LOCAL 255 +#define RT_CLASS_MAX 255 + + +#define RTMSG_ACK NLMSG_ACK +#define RTMSG_OVERRUN NLMSG_OVERRUN + +#define RTMSG_NEWDEVICE 0x11 +#define RTMSG_DELDEVICE 0x12 +#define RTMSG_NEWROUTE 0x21 +#define RTMSG_DELROUTE 0x22 +#define RTMSG_NEWRULE 0x31 +#define RTMSG_DELRULE 0x32 +#define RTMSG_CONTROL 0x40 + +#define RTMSG_AR_FAILED 0x51 + +#endif diff --git a/05/musl-final/include/netdb.h b/05/musl-final/include/netdb.h new file mode 100644 index 0000000..5f1dd3f --- /dev/null +++ b/05/musl-final/include/netdb.h @@ -0,0 +1,148 @@ +#ifndef _NETDB_H +#define _NETDB_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef _GNU_SOURCE +#define __NEED_size_t +#endif + +#define __NEED_socklen_t +#define __NEED_uint32_t + +#include + +struct addrinfo +{ + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + socklen_t ai_addrlen; + struct sockaddr *ai_addr; + char *ai_canonname; + struct addrinfo *ai_next; +}; + +#define IPPORT_RESERVED 1024 + +#define AI_PASSIVE 0x01 +#define AI_CANONNAME 0x02 +#define AI_NUMERICHOST 0x04 +#define AI_V4MAPPED 0x08 +#define AI_ALL 0x10 +#define AI_ADDRCONFIG 0x20 +#define AI_NUMERICSERV 0x400 + + +#define NI_NUMERICHOST 0x01 +#define NI_NUMERICSERV 0x02 +#define NI_NOFQDN 0x04 +#define NI_NAMEREQD 0x08 +#define NI_DGRAM 0x10 +/*#define NI_NUMERICSCOPE */ + +#define NI_MAXHOST 255 +#define NI_MAXSERV 32 + +#define EAI_BADFLAGS -1 +#define EAI_NONAME -2 +#define EAI_AGAIN -3 +#define EAI_FAIL -4 +#define EAI_FAMILY -6 +#define EAI_SOCKTYPE -7 +#define EAI_SERVICE -8 +#define EAI_MEMORY -10 +#define EAI_SYSTEM -11 +#define EAI_OVERFLOW -12 + +int getaddrinfo (const char *, const char *, const struct addrinfo *, struct addrinfo **); +void freeaddrinfo (struct addrinfo *); +int getnameinfo (const struct sockaddr *, socklen_t, char *, socklen_t, char *, socklen_t, int); +const char *gai_strerror(int); + + +/* Legacy functions follow (marked OBsolete in SUS) */ + +struct netent +{ + char *n_name; + char **n_aliases; + int n_addrtype; + uint32_t n_net; +}; + +struct hostent +{ + char *h_name; + char **h_aliases; + int h_addrtype; + int h_length; + char **h_addr_list; +}; +#define h_addr h_addr_list[0] + +struct servent +{ + char *s_name; + char **s_aliases; + int s_port; + char *s_proto; +}; + +struct protoent +{ + char *p_name; + char **p_aliases; + int p_proto; +}; + +extern int h_errno; + +#define HOST_NOT_FOUND 1 +#define TRY_AGAIN 2 +#define NO_RECOVERY 3 +#define NO_DATA 4 + +void sethostent (int); +void endhostent (void); +struct hostent *gethostent (void); +struct hostent *gethostbyaddr (const void *, socklen_t, int); +struct hostent *gethostbyname (const char *); + +void setnetent (int); +void endnetent (void); +struct netent *getnetent (void); +struct netent *getnetbyaddr (uint32_t, int); +struct netent *getnetbyname (const char *); + +void setservent (int); +void endservent (void); +struct servent *getservent (void); +struct servent *getservbyname (const char *, const char *); +struct servent *getservbyport (int, const char *); + +void setprotoent (int); +void endprotoent (void); +struct protoent *getprotoent (void); +struct protoent *getprotobyname (const char *); +struct protoent *getprotobynumber (int); + +#ifdef _GNU_SOURCE +const char *hstrerror(int); +int gethostbyname_r(const char *, struct hostent *, char *, size_t, struct hostent **, int *); +int gethostbyname2_r(const char *, int, struct hostent *, char *, size_t, struct hostent **, int *); +struct hostent *gethostbyname2(const char *, int); +int gethostbyaddr_r(const void *, socklen_t, int, struct hostent *, char *, size_t, struct hostent **, int *); +int getservbyport_r(int, const char *, struct servent *, char *, size_t, struct servent **); +int getservbyname_r(const char *, const char *, struct servent *, char *, size_t, struct servent **); +#endif + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/netinet/icmp6.h b/05/musl-final/include/netinet/icmp6.h new file mode 100644 index 0000000..01269e7 --- /dev/null +++ b/05/musl-final/include/netinet/icmp6.h @@ -0,0 +1,305 @@ +#ifndef _NETINET_ICMP6_H +#define _NETINET_ICMP6_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + +#define ICMP6_FILTER 1 + +#define ICMP6_FILTER_BLOCK 1 +#define ICMP6_FILTER_PASS 2 +#define ICMP6_FILTER_BLOCKOTHERS 3 +#define ICMP6_FILTER_PASSONLY 4 + +struct icmp6_filter { + uint32_t icmp6_filt[8]; +}; + +struct icmp6_hdr { + uint8_t icmp6_type; + uint8_t icmp6_code; + uint16_t icmp6_cksum; + union { + uint32_t icmp6_un_data32[1]; + uint16_t icmp6_un_data16[2]; + uint8_t icmp6_un_data8[4]; + } icmp6_dataun; +}; + +#define icmp6_data32 icmp6_dataun.icmp6_un_data32 +#define icmp6_data16 icmp6_dataun.icmp6_un_data16 +#define icmp6_data8 icmp6_dataun.icmp6_un_data8 +#define icmp6_pptr icmp6_data32[0] +#define icmp6_mtu icmp6_data32[0] +#define icmp6_id icmp6_data16[0] +#define icmp6_seq icmp6_data16[1] +#define icmp6_maxdelay icmp6_data16[0] + +#define ICMP6_DST_UNREACH 1 +#define ICMP6_PACKET_TOO_BIG 2 +#define ICMP6_TIME_EXCEEDED 3 +#define ICMP6_PARAM_PROB 4 + +#define ICMP6_INFOMSG_MASK 0x80 + +#define ICMP6_ECHO_REQUEST 128 +#define ICMP6_ECHO_REPLY 129 +#define MLD_LISTENER_QUERY 130 +#define MLD_LISTENER_REPORT 131 +#define MLD_LISTENER_REDUCTION 132 + +#define ICMP6_DST_UNREACH_NOROUTE 0 +#define ICMP6_DST_UNREACH_ADMIN 1 +#define ICMP6_DST_UNREACH_BEYONDSCOPE 2 +#define ICMP6_DST_UNREACH_ADDR 3 +#define ICMP6_DST_UNREACH_NOPORT 4 + +#define ICMP6_TIME_EXCEED_TRANSIT 0 +#define ICMP6_TIME_EXCEED_REASSEMBLY 1 + +#define ICMP6_PARAMPROB_HEADER 0 +#define ICMP6_PARAMPROB_NEXTHEADER 1 +#define ICMP6_PARAMPROB_OPTION 2 + +#define ICMP6_FILTER_WILLPASS(type, filterp) \ + ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) == 0) + +#define ICMP6_FILTER_WILLBLOCK(type, filterp) \ + ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) != 0) + +#define ICMP6_FILTER_SETPASS(type, filterp) \ + ((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31)))) + +#define ICMP6_FILTER_SETBLOCK(type, filterp) \ + ((((filterp)->icmp6_filt[(type) >> 5]) |= (1 << ((type) & 31)))) + +#define ICMP6_FILTER_SETPASSALL(filterp) \ + memset (filterp, 0, sizeof (struct icmp6_filter)); + +#define ICMP6_FILTER_SETBLOCKALL(filterp) \ + memset (filterp, 0xFF, sizeof (struct icmp6_filter)); + +#define ND_ROUTER_SOLICIT 133 +#define ND_ROUTER_ADVERT 134 +#define ND_NEIGHBOR_SOLICIT 135 +#define ND_NEIGHBOR_ADVERT 136 +#define ND_REDIRECT 137 + +struct nd_router_solicit { + struct icmp6_hdr nd_rs_hdr; +}; + +#define nd_rs_type nd_rs_hdr.icmp6_type +#define nd_rs_code nd_rs_hdr.icmp6_code +#define nd_rs_cksum nd_rs_hdr.icmp6_cksum +#define nd_rs_reserved nd_rs_hdr.icmp6_data32[0] + +struct nd_router_advert { + struct icmp6_hdr nd_ra_hdr; + uint32_t nd_ra_reachable; + uint32_t nd_ra_retransmit; +}; + +#define nd_ra_type nd_ra_hdr.icmp6_type +#define nd_ra_code nd_ra_hdr.icmp6_code +#define nd_ra_cksum nd_ra_hdr.icmp6_cksum +#define nd_ra_curhoplimit nd_ra_hdr.icmp6_data8[0] +#define nd_ra_flags_reserved nd_ra_hdr.icmp6_data8[1] +#define ND_RA_FLAG_MANAGED 0x80 +#define ND_RA_FLAG_OTHER 0x40 +#define ND_RA_FLAG_HOME_AGENT 0x20 +#define nd_ra_router_lifetime nd_ra_hdr.icmp6_data16[1] + +struct nd_neighbor_solicit { + struct icmp6_hdr nd_ns_hdr; + struct in6_addr nd_ns_target; +}; + +#define nd_ns_type nd_ns_hdr.icmp6_type +#define nd_ns_code nd_ns_hdr.icmp6_code +#define nd_ns_cksum nd_ns_hdr.icmp6_cksum +#define nd_ns_reserved nd_ns_hdr.icmp6_data32[0] + +struct nd_neighbor_advert { + struct icmp6_hdr nd_na_hdr; + struct in6_addr nd_na_target; +}; + +#define nd_na_type nd_na_hdr.icmp6_type +#define nd_na_code nd_na_hdr.icmp6_code +#define nd_na_cksum nd_na_hdr.icmp6_cksum +#define nd_na_flags_reserved nd_na_hdr.icmp6_data32[0] +#if __BYTE_ORDER == __BIG_ENDIAN +#define ND_NA_FLAG_ROUTER 0x80000000 +#define ND_NA_FLAG_SOLICITED 0x40000000 +#define ND_NA_FLAG_OVERRIDE 0x20000000 +#else +#define ND_NA_FLAG_ROUTER 0x00000080 +#define ND_NA_FLAG_SOLICITED 0x00000040 +#define ND_NA_FLAG_OVERRIDE 0x00000020 +#endif + +struct nd_redirect { + struct icmp6_hdr nd_rd_hdr; + struct in6_addr nd_rd_target; + struct in6_addr nd_rd_dst; +}; + +#define nd_rd_type nd_rd_hdr.icmp6_type +#define nd_rd_code nd_rd_hdr.icmp6_code +#define nd_rd_cksum nd_rd_hdr.icmp6_cksum +#define nd_rd_reserved nd_rd_hdr.icmp6_data32[0] + +struct nd_opt_hdr { + uint8_t nd_opt_type; + uint8_t nd_opt_len; +}; + +#define ND_OPT_SOURCE_LINKADDR 1 +#define ND_OPT_TARGET_LINKADDR 2 +#define ND_OPT_PREFIX_INFORMATION 3 +#define ND_OPT_REDIRECTED_HEADER 4 +#define ND_OPT_MTU 5 +#define ND_OPT_RTR_ADV_INTERVAL 7 +#define ND_OPT_HOME_AGENT_INFO 8 + +struct nd_opt_prefix_info { + uint8_t nd_opt_pi_type; + uint8_t nd_opt_pi_len; + uint8_t nd_opt_pi_prefix_len; + uint8_t nd_opt_pi_flags_reserved; + uint32_t nd_opt_pi_valid_time; + uint32_t nd_opt_pi_preferred_time; + uint32_t nd_opt_pi_reserved2; + struct in6_addr nd_opt_pi_prefix; +}; + +#define ND_OPT_PI_FLAG_ONLINK 0x80 +#define ND_OPT_PI_FLAG_AUTO 0x40 +#define ND_OPT_PI_FLAG_RADDR 0x20 + +struct nd_opt_rd_hdr { + uint8_t nd_opt_rh_type; + uint8_t nd_opt_rh_len; + uint16_t nd_opt_rh_reserved1; + uint32_t nd_opt_rh_reserved2; +}; + +struct nd_opt_mtu { + uint8_t nd_opt_mtu_type; + uint8_t nd_opt_mtu_len; + uint16_t nd_opt_mtu_reserved; + uint32_t nd_opt_mtu_mtu; +}; + +struct mld_hdr { + struct icmp6_hdr mld_icmp6_hdr; + struct in6_addr mld_addr; +}; + +#define mld_type mld_icmp6_hdr.icmp6_type +#define mld_code mld_icmp6_hdr.icmp6_code +#define mld_cksum mld_icmp6_hdr.icmp6_cksum +#define mld_maxdelay mld_icmp6_hdr.icmp6_data16[0] +#define mld_reserved mld_icmp6_hdr.icmp6_data16[1] + +#define ICMP6_ROUTER_RENUMBERING 138 + +struct icmp6_router_renum { + struct icmp6_hdr rr_hdr; + uint8_t rr_segnum; + uint8_t rr_flags; + uint16_t rr_maxdelay; + uint32_t rr_reserved; +}; + +#define rr_type rr_hdr.icmp6_type +#define rr_code rr_hdr.icmp6_code +#define rr_cksum rr_hdr.icmp6_cksum +#define rr_seqnum rr_hdr.icmp6_data32[0] + +#define ICMP6_RR_FLAGS_TEST 0x80 +#define ICMP6_RR_FLAGS_REQRESULT 0x40 +#define ICMP6_RR_FLAGS_FORCEAPPLY 0x20 +#define ICMP6_RR_FLAGS_SPECSITE 0x10 +#define ICMP6_RR_FLAGS_PREVDONE 0x08 + +struct rr_pco_match { + uint8_t rpm_code; + uint8_t rpm_len; + uint8_t rpm_ordinal; + uint8_t rpm_matchlen; + uint8_t rpm_minlen; + uint8_t rpm_maxlen; + uint16_t rpm_reserved; + struct in6_addr rpm_prefix; +}; + +#define RPM_PCO_ADD 1 +#define RPM_PCO_CHANGE 2 +#define RPM_PCO_SETGLOBAL 3 + +struct rr_pco_use { + uint8_t rpu_uselen; + uint8_t rpu_keeplen; + uint8_t rpu_ramask; + uint8_t rpu_raflags; + uint32_t rpu_vltime; + uint32_t rpu_pltime; + uint32_t rpu_flags; + struct in6_addr rpu_prefix; +}; + +#define ICMP6_RR_PCOUSE_RAFLAGS_ONLINK 0x20 +#define ICMP6_RR_PCOUSE_RAFLAGS_AUTO 0x10 + +#if __BYTE_ORDER == __BIG_ENDIAN +#define ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME 0x80000000 +#define ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME 0x40000000 +#else +#define ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME 0x80 +#define ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME 0x40 +#endif + +struct rr_result { + uint16_t rrr_flags; + uint8_t rrr_ordinal; + uint8_t rrr_matchedlen; + uint32_t rrr_ifid; + struct in6_addr rrr_prefix; +}; + +#if __BYTE_ORDER == __BIG_ENDIAN +#define ICMP6_RR_RESULT_FLAGS_OOB 0x0002 +#define ICMP6_RR_RESULT_FLAGS_FORBIDDEN 0x0001 +#else +#define ICMP6_RR_RESULT_FLAGS_OOB 0x0200 +#define ICMP6_RR_RESULT_FLAGS_FORBIDDEN 0x0100 +#endif + +struct nd_opt_adv_interval { + uint8_t nd_opt_adv_interval_type; + uint8_t nd_opt_adv_interval_len; + uint16_t nd_opt_adv_interval_reserved; + uint32_t nd_opt_adv_interval_ival; +}; + +struct nd_opt_home_agent_info { + uint8_t nd_opt_home_agent_info_type; + uint8_t nd_opt_home_agent_info_len; + uint16_t nd_opt_home_agent_info_reserved; + uint16_t nd_opt_home_agent_info_preference; + uint16_t nd_opt_home_agent_info_lifetime; +}; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/netinet/if_ether.h b/05/musl-final/include/netinet/if_ether.h new file mode 100644 index 0000000..65a838a --- /dev/null +++ b/05/musl-final/include/netinet/if_ether.h @@ -0,0 +1,108 @@ +#ifndef _NETINET_IF_ETHER_H +#define _NETINET_IF_ETHER_H + +#include +#include + +#define ETH_ALEN 6 +#define ETH_HLEN 14 +#define ETH_ZLEN 60 +#define ETH_DATA_LEN 1500 +#define ETH_FRAME_LEN 1514 +#define ETH_FCS_LEN 4 + +#define ETH_P_LOOP 0x0060 +#define ETH_P_PUP 0x0200 +#define ETH_P_PUPAT 0x0201 +#define ETH_P_IP 0x0800 +#define ETH_P_X25 0x0805 +#define ETH_P_ARP 0x0806 +#define ETH_P_BPQ 0x08FF +#define ETH_P_IEEEPUP 0x0a00 +#define ETH_P_IEEEPUPAT 0x0a01 +#define ETH_P_DEC 0x6000 +#define ETH_P_DNA_DL 0x6001 +#define ETH_P_DNA_RC 0x6002 +#define ETH_P_DNA_RT 0x6003 +#define ETH_P_LAT 0x6004 +#define ETH_P_DIAG 0x6005 +#define ETH_P_CUST 0x6006 +#define ETH_P_SCA 0x6007 +#define ETH_P_TEB 0x6558 +#define ETH_P_RARP 0x8035 +#define ETH_P_ATALK 0x809B +#define ETH_P_AARP 0x80F3 +#define ETH_P_8021Q 0x8100 +#define ETH_P_IPX 0x8137 +#define ETH_P_IPV6 0x86DD +#define ETH_P_PAUSE 0x8808 +#define ETH_P_SLOW 0x8809 +#define ETH_P_WCCP 0x883E +#define ETH_P_PPP_DISC 0x8863 +#define ETH_P_PPP_SES 0x8864 +#define ETH_P_MPLS_UC 0x8847 +#define ETH_P_MPLS_MC 0x8848 +#define ETH_P_ATMMPOA 0x884c +#define ETH_P_ATMFATE 0x8884 +#define ETH_P_PAE 0x888E +#define ETH_P_AOE 0x88A2 +#define ETH_P_TIPC 0x88CA +#define ETH_P_FCOE 0x8906 +#define ETH_P_EDSA 0xDADA + +#define ETH_P_802_3 0x0001 +#define ETH_P_AX25 0x0002 +#define ETH_P_ALL 0x0003 +#define ETH_P_802_2 0x0004 +#define ETH_P_SNAP 0x0005 +#define ETH_P_DDCMP 0x0006 +#define ETH_P_WAN_PPP 0x0007 +#define ETH_P_PPP_MP 0x0008 +#define ETH_P_LOCALTALK 0x0009 +#define ETH_P_CAN 0x000C +#define ETH_P_PPPTALK 0x0010 +#define ETH_P_TR_802_2 0x0011 +#define ETH_P_MOBITEX 0x0015 +#define ETH_P_CONTROL 0x0016 +#define ETH_P_IRDA 0x0017 +#define ETH_P_ECONET 0x0018 +#define ETH_P_HDLC 0x0019 +#define ETH_P_ARCNET 0x001A +#define ETH_P_DSA 0x001B +#define ETH_P_TRAILER 0x001C +#define ETH_P_PHONET 0x00F5 + +struct ethhdr { + uint8_t h_dest[ETH_ALEN]; + uint8_t h_source[ETH_ALEN]; + uint16_t h_proto; +}; + +#include +#include + +struct ether_arp { + struct arphdr ea_hdr; + uint8_t arp_sha[ETH_ALEN]; + uint8_t arp_spa[4]; + uint8_t arp_tha[ETH_ALEN]; + uint8_t arp_tpa[4]; +}; +#define arp_hrd ea_hdr.ar_hrd +#define arp_pro ea_hdr.ar_pro +#define arp_hln ea_hdr.ar_hln +#define arp_pln ea_hdr.ar_pln +#define arp_op ea_hdr.ar_op + +#define ETHER_MAP_IP_MULTICAST(ipaddr, enaddr) \ +do { \ + (enaddr)[0] = 0x01; \ + (enaddr)[1] = 0x00; \ + (enaddr)[2] = 0x5e; \ + (enaddr)[3] = ((uint8_t *)ipaddr)[1] & 0x7f; \ + (enaddr)[4] = ((uint8_t *)ipaddr)[2]; \ + (enaddr)[5] = ((uint8_t *)ipaddr)[3]; \ +} while(0) + + +#endif diff --git a/05/musl-final/include/netinet/in.h b/05/musl-final/include/netinet/in.h new file mode 100644 index 0000000..a641f6a --- /dev/null +++ b/05/musl-final/include/netinet/in.h @@ -0,0 +1,158 @@ +#ifndef _NETINET_IN_H +#define _NETINET_IN_H + +#define __NEED_in_addr_t +#define __NEED_in_port_t +#define __NEED_sa_family_t +#define __NEED_struct_in_addr +#define __NEED_uint8_t +#define __NEED_uint16_t +#define __NEED_uint32_t + +#include + +struct sockaddr_in +{ + sa_family_t sin_family; + in_port_t sin_port; + struct in_addr sin_addr; + uint8_t sin_zero[8]; +}; + +struct in6_addr +{ + union { + uint8_t __s6_addr[16]; + uint32_t __s6_addr32[4]; + } __in6_union; +}; +#define s6_addr __in6_union.__s6_addr +#define s6_addr32 __in6_union.__s6_addr32 + +struct sockaddr_in6 +{ + sa_family_t sin6_family; + in_port_t sin6_port; + uint32_t sin6_flowinfo; + struct in6_addr sin6_addr; + uint32_t sin6_scope_id; +}; + +struct ipv6_mreq +{ + struct in6_addr ipv6mr_multiaddr; + unsigned ipv6mr_interface; +}; + +#define INADDR_ANY ((in_addr_t) 0x00000000) +#define INADDR_BROADCAST ((in_addr_t) 0xffffffff) +#define INADDR_NONE ((in_addr_t) 0xffffffff) +#define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) + +#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } } +#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } } + +extern const struct in6_addr in6addr_any, in6addr_loopback; + +#undef INET_ADDRSTRLEN +#undef INET6_ADDRSTRLEN +#define INET_ADDRSTRLEN 16 +#define INET6_ADDRSTRLEN 46 + +uint32_t htonl(uint32_t); +uint16_t htons(uint16_t); +uint32_t ntohl(uint32_t); +uint16_t ntohs(uint16_t); + +#define IPPROTO_IP 0 +#define IPPROTO_ICMP 1 +#define IPPROTO_IGMP 2 +#define IPPROTO_IPIP 4 +#define IPPROTO_TCP 6 +#define IPPROTO_EGP 8 +#define IPPROTO_PUP 12 +#define IPPROTO_UDP 17 +#define IPPROTO_IDP 22 +#define IPPROTO_TP 29 +#define IPPROTO_IPV6 41 +#define IPPROTO_ROUTING 43 +#define IPPROTO_FRAGMENT 44 +#define IPPROTO_RSVP 46 +#define IPPROTO_GRE 47 +#define IPPROTO_ESP 50 +#define IPPROTO_AH 51 +#define IPPROTO_ICMPV6 58 +#define IPPROTO_NONE 59 +#define IPPROTO_DSTOPTS 60 +#define IPPROTO_MTP 92 +#define IPPROTO_ENCAP 98 +#define IPPROTO_PIM 103 +#define IPPROTO_COMP 108 +#define IPPROTO_RAW 255 + +#define IN6_IS_ADDR_UNSPECIFIED(a) \ + (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \ + ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0) + +#define IN6_IS_ADDR_LOOPBACK(a) \ + (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \ + ((uint32_t *) (a))[2] == 0 && \ + ((uint8_t *) (a))[12] == 0 && ((uint8_t *) (a))[13] == 0 && \ + ((uint8_t *) (a))[14] == 0 && ((uint8_t *) (a))[15] == 1 ) + +#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff) + +#define IN6_IS_ADDR_LINKLOCAL(a) \ + ((((uint8_t *) (a))[0]) == 0xfe && (((uint8_t *) (a))[1] & 0xc0) == 0x80) + +#define IN6_IS_ADDR_SITELOCAL(a) \ + ((((uint8_t *) (a))[0]) == 0xfe && (((uint8_t *) (a))[1] & 0xc0) == 0xc0) + +#define IN6_IS_ADDR_V4MAPPED(a) \ + (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \ + ((uint8_t *) (a))[8] == 0 && ((uint8_t *) (a))[9] == 0 && \ + ((uint8_t *) (a))[10] == 0xff && ((uint8_t *) (a))[11] == 0xff) + +#define IN6_IS_ADDR_V4COMPAT(a) \ + (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \ + ((uint32_t *) (a))[2] == 0 && ((uint8_t *) (a))[15] > 1) + +#define IN6_IS_ADDR_MC_NODELOCAL(a) \ + (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x1)) + +#define IN6_IS_ADDR_MC_LINKLOCAL(a) \ + (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x2)) + +#define IN6_IS_ADDR_MC_SITELOCAL(a) \ + (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x5)) + +#define IN6_IS_ADDR_MC_ORGLOCAL(a) \ + (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0x8)) + +#define IN6_IS_ADDR_MC_GLOBAL(a) \ + (IN6_IS_ADDR_MULTICAST(a) && ((((uint8_t *) (a))[1] & 0xf) == 0xe)) + +#include + +#if 1 /* FIXME: namespace violation */ +#define IN_CLASSA(a) ((((in_addr_t)(a)) & 0x80000000) == 0) +#define IN_CLASSA_NET 0xff000000 +#define IN_CLASSA_NSHIFT 24 +#define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET) +#define IN_CLASSA_MAX 128 +#define IN_CLASSB(a) ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000) +#define IN_CLASSB_NET 0xffff0000 +#define IN_CLASSB_NSHIFT 16 +#define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET) +#define IN_CLASSB_MAX 65536 +#define IN_CLASSC(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000) +#define IN_CLASSC_NET 0xffffff00 +#define IN_CLASSC_NSHIFT 8 +#define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET) +#define IN_CLASSD(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000) +#define IN_MULTICAST(a) IN_CLASSD(a) +#define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000) +#define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000) +#endif + +#endif diff --git a/05/musl-final/include/netinet/ip.h b/05/musl-final/include/netinet/ip.h new file mode 100644 index 0000000..4118741 --- /dev/null +++ b/05/musl-final/include/netinet/ip.h @@ -0,0 +1,186 @@ +#ifndef _NETINET_IP_H +#define _NETINET_IP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +struct timestamp { + uint8_t len; + uint8_t ptr; +#if __BYTE_ORDER == __LITTLE_ENDIAN + unsigned int flags:4; + unsigned int overflow:4; +#else + unsigned int overflow:4; + unsigned int flags:4; +#endif + uint32_t data[9]; + }; + +struct iphdr { +#if __BYTE_ORDER == __LITTLE_ENDIAN + unsigned int ihl:4; + unsigned int version:4; +#else + unsigned int version:4; + unsigned int ihl:4; +#endif + uint8_t tos; + uint16_t tot_len; + uint16_t id; + uint16_t frag_off; + uint8_t ttl; + uint8_t protocol; + uint16_t check; + uint32_t saddr; + uint32_t daddr; +}; + +struct ip { +#if __BYTE_ORDER == __LITTLE_ENDIAN + unsigned int ip_hl:4; + unsigned int ip_v:4; +#else + unsigned int ip_v:4; + unsigned int ip_hl:4; +#endif + uint8_t ip_tos; + uint16_t ip_len; + uint16_t ip_id; + uint16_t ip_off; + uint8_t ip_ttl; + uint8_t ip_p; + uint16_t ip_sum; + struct in_addr ip_src, ip_dst; +}; + +#define IP_RF 0x8000 +#define IP_DF 0x4000 +#define IP_MF 0x2000 +#define IP_OFFMASK 0x1fff + +struct ip_timestamp { + uint8_t ipt_code; + uint8_t ipt_len; + uint8_t ipt_ptr; +#if __BYTE_ORDER == __LITTLE_ENDIAN + unsigned int ipt_flg:4; + unsigned int ipt_oflw:4; +#else + unsigned int ipt_oflw:4; + unsigned int ipt_flg:4; +#endif + uint32_t data[9]; +}; + +#define IPVERSION 4 +#define IP_MAXPACKET 65535 + +#define IPTOS_ECN_MASK 0x03 +#define IPTOS_ECN(x) ((x) & IPTOS_ECN_MASK) +#define IPTOS_ECN_NOT_ECT 0x00 +#define IPTOS_ECN_ECT1 0x01 +#define IPTOS_ECN_ECT0 0x02 +#define IPTOS_ECN_CE 0x03 + +#define IPTOS_DSCP_MASK 0xfc +#define IPTOS_DSCP(x) ((x) & IPTOS_DSCP_MASK) +#define IPTOS_DSCP_AF11 0x28 +#define IPTOS_DSCP_AF12 0x30 +#define IPTOS_DSCP_AF13 0x38 +#define IPTOS_DSCP_AF21 0x48 +#define IPTOS_DSCP_AF22 0x50 +#define IPTOS_DSCP_AF23 0x58 +#define IPTOS_DSCP_AF31 0x68 +#define IPTOS_DSCP_AF32 0x70 +#define IPTOS_DSCP_AF33 0x78 +#define IPTOS_DSCP_AF41 0x88 +#define IPTOS_DSCP_AF42 0x90 +#define IPTOS_DSCP_AF43 0x98 +#define IPTOS_DSCP_EF 0xb8 + +#define IPTOS_TOS_MASK 0x1E +#define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK) +#define IPTOS_LOWDELAY 0x10 +#define IPTOS_THROUGHPUT 0x08 +#define IPTOS_RELIABILITY 0x04 +#define IPTOS_LOWCOST 0x02 +#define IPTOS_MINCOST IPTOS_LOWCOST + +#define IPTOS_PREC_MASK 0xe0 +#define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK) +#define IPTOS_PREC_NETCONTROL 0xe0 +#define IPTOS_PREC_INTERNETCONTROL 0xc0 +#define IPTOS_PREC_CRITIC_ECP 0xa0 +#define IPTOS_PREC_FLASHOVERRIDE 0x80 +#define IPTOS_PREC_FLASH 0x60 +#define IPTOS_PREC_IMMEDIATE 0x40 +#define IPTOS_PREC_PRIORITY 0x20 +#define IPTOS_PREC_ROUTINE 0x00 + +#define IPOPT_COPY 0x80 +#define IPOPT_CLASS_MASK 0x60 +#define IPOPT_NUMBER_MASK 0x1f + +#define IPOPT_COPIED(o) ((o) & IPOPT_COPY) +#define IPOPT_CLASS(o) ((o) & IPOPT_CLASS_MASK) +#define IPOPT_NUMBER(o) ((o) & IPOPT_NUMBER_MASK) + +#define IPOPT_CONTROL 0x00 +#define IPOPT_RESERVED1 0x20 +#define IPOPT_DEBMEAS 0x40 +#define IPOPT_MEASUREMENT IPOPT_DEBMEAS +#define IPOPT_RESERVED2 0x60 + +#define IPOPT_EOL 0 +#define IPOPT_END IPOPT_EOL +#define IPOPT_NOP 1 +#define IPOPT_NOOP IPOPT_NOP + +#define IPOPT_RR 7 +#define IPOPT_TS 68 +#define IPOPT_TIMESTAMP IPOPT_TS +#define IPOPT_SECURITY 130 +#define IPOPT_SEC IPOPT_SECURITY +#define IPOPT_LSRR 131 +#define IPOPT_SATID 136 +#define IPOPT_SID IPOPT_SATID +#define IPOPT_SSRR 137 +#define IPOPT_RA 148 + +#define IPOPT_OPTVAL 0 +#define IPOPT_OLEN 1 +#define IPOPT_OFFSET 2 +#define IPOPT_MINOFF 4 + +#define MAX_IPOPTLEN 40 + +#define IPOPT_TS_TSONLY 0 +#define IPOPT_TS_TSANDADDR 1 +#define IPOPT_TS_PRESPEC 3 + +#define IPOPT_SECUR_UNCLASS 0x0000 +#define IPOPT_SECUR_CONFID 0xf135 +#define IPOPT_SECUR_EFTO 0x789a +#define IPOPT_SECUR_MMMM 0xbc4d +#define IPOPT_SECUR_RESTR 0xaf13 +#define IPOPT_SECUR_SECRET 0xd788 +#define IPOPT_SECUR_TOPSECRET 0x6bc5 + +#define MAXTTL 255 +#define IPDEFTTL 64 +#define IPFRAGTTL 60 +#define IPTTLDEC 1 + +#define IP_MSS 576 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/netinet/ip6.h b/05/musl-final/include/netinet/ip6.h new file mode 100644 index 0000000..a4347a5 --- /dev/null +++ b/05/musl-final/include/netinet/ip6.h @@ -0,0 +1,142 @@ +#ifndef _NETINET_IP6_H +#define _NETINET_IP6_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +struct ip6_hdr { + union { + struct ip6_hdrctl { + uint32_t ip6_un1_flow; + uint16_t ip6_un1_plen; + uint8_t ip6_un1_nxt; + uint8_t ip6_un1_hlim; + } ip6_un1; + uint8_t ip6_un2_vfc; + } ip6_ctlun; + struct in6_addr ip6_src; + struct in6_addr ip6_dst; +}; + +#define ip6_vfc ip6_ctlun.ip6_un2_vfc +#define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow +#define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen +#define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt +#define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim +#define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim + +struct ip6_ext { + uint8_t ip6e_nxt; + uint8_t ip6e_len; +}; + +struct ip6_hbh { + uint8_t ip6h_nxt; + uint8_t ip6h_len; +}; + +struct ip6_dest { + uint8_t ip6d_nxt; + uint8_t ip6d_len; +}; + +struct ip6_rthdr { + uint8_t ip6r_nxt; + uint8_t ip6r_len; + uint8_t ip6r_type; + uint8_t ip6r_segleft; +}; + +struct ip6_rthdr0 { + uint8_t ip6r0_nxt; + uint8_t ip6r0_len; + uint8_t ip6r0_type; + uint8_t ip6r0_segleft; + uint8_t ip6r0_reserved; + uint8_t ip6r0_slmap[3]; + struct in6_addr ip6r0_addr[]; +}; + +struct ip6_frag { + uint8_t ip6f_nxt; + uint8_t ip6f_reserved; + uint16_t ip6f_offlg; + uint32_t ip6f_ident; +}; + +#if __BYTE_ORDER == __BIG_ENDIAN +#define IP6F_OFF_MASK 0xfff8 +#define IP6F_RESERVED_MASK 0x0006 +#define IP6F_MORE_FRAG 0x0001 +#else +#define IP6F_OFF_MASK 0xf8ff +#define IP6F_RESERVED_MASK 0x0600 +#define IP6F_MORE_FRAG 0x0100 +#endif + +struct ip6_opt { + uint8_t ip6o_type; + uint8_t ip6o_len; +}; + +#define IP6OPT_TYPE(o) ((o) & 0xc0) +#define IP6OPT_TYPE_SKIP 0x00 +#define IP6OPT_TYPE_DISCARD 0x40 +#define IP6OPT_TYPE_FORCEICMP 0x80 +#define IP6OPT_TYPE_ICMP 0xc0 +#define IP6OPT_TYPE_MUTABLE 0x20 + +#define IP6OPT_PAD1 0 +#define IP6OPT_PADN 1 + +#define IP6OPT_JUMBO 0xc2 +#define IP6OPT_NSAP_ADDR 0xc3 +#define IP6OPT_TUNNEL_LIMIT 0x04 +#define IP6OPT_ROUTER_ALERT 0x05 + +struct ip6_opt_jumbo { + uint8_t ip6oj_type; + uint8_t ip6oj_len; + uint8_t ip6oj_jumbo_len[4]; +}; +#define IP6OPT_JUMBO_LEN 6 + +struct ip6_opt_nsap { + uint8_t ip6on_type; + uint8_t ip6on_len; + uint8_t ip6on_src_nsap_len; + uint8_t ip6on_dst_nsap_len; +}; + +struct ip6_opt_tunnel { + uint8_t ip6ot_type; + uint8_t ip6ot_len; + uint8_t ip6ot_encap_limit; +}; + +struct ip6_opt_router { + uint8_t ip6or_type; + uint8_t ip6or_len; + uint8_t ip6or_value[2]; +}; + +#if __BYTE_ORDER == __BIG_ENDIAN +#define IP6_ALERT_MLD 0x0000 +#define IP6_ALERT_RSVP 0x0001 +#define IP6_ALERT_AN 0x0002 +#else +#define IP6_ALERT_MLD 0x0000 +#define IP6_ALERT_RSVP 0x0100 +#define IP6_ALERT_AN 0x0200 +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/netinet/ip_icmp.h b/05/musl-final/include/netinet/ip_icmp.h new file mode 100644 index 0000000..2f4a86d --- /dev/null +++ b/05/musl-final/include/netinet/ip_icmp.h @@ -0,0 +1,192 @@ +#ifndef _NETINET_IP_ICMP_H +#define _NETINET_IP_ICMP_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct icmphdr { + uint8_t type; + uint8_t code; + uint16_t checksum; + union { + struct { + uint16_t id; + uint16_t sequence; + } echo; + uint32_t gateway; + struct { + uint16_t __unused; + uint16_t mtu; + } frag; + } un; +}; + +#define ICMP_ECHOREPLY 0 +#define ICMP_DEST_UNREACH 3 +#define ICMP_SOURCE_QUENCH 4 +#define ICMP_REDIRECT 5 +#define ICMP_ECHO 8 +#define ICMP_TIME_EXCEEDED 11 +#define ICMP_PARAMETERPROB 12 +#define ICMP_TIMESTAMP 13 +#define ICMP_TIMESTAMPREPLY 14 +#define ICMP_INFO_REQUEST 15 +#define ICMP_INFO_REPLY 16 +#define ICMP_ADDRESS 17 +#define ICMP_ADDRESSREPLY 18 +#define NR_ICMP_TYPES 18 + + +#define ICMP_NET_UNREACH 0 +#define ICMP_HOST_UNREACH 1 +#define ICMP_PROT_UNREACH 2 +#define ICMP_PORT_UNREACH 3 +#define ICMP_FRAG_NEEDED 4 +#define ICMP_SR_FAILED 5 +#define ICMP_NET_UNKNOWN 6 +#define ICMP_HOST_UNKNOWN 7 +#define ICMP_HOST_ISOLATED 8 +#define ICMP_NET_ANO 9 +#define ICMP_HOST_ANO 10 +#define ICMP_NET_UNR_TOS 11 +#define ICMP_HOST_UNR_TOS 12 +#define ICMP_PKT_FILTERED 13 +#define ICMP_PREC_VIOLATION 14 +#define ICMP_PREC_CUTOFF 15 +#define NR_ICMP_UNREACH 15 + +#define ICMP_REDIR_NET 0 +#define ICMP_REDIR_HOST 1 +#define ICMP_REDIR_NETTOS 2 +#define ICMP_REDIR_HOSTTOS 3 + +#define ICMP_EXC_TTL 0 +#define ICMP_EXC_FRAGTIME 1 + + +struct icmp_ra_addr { + uint32_t ira_addr; + uint32_t ira_preference; +}; + +struct icmp { + uint8_t icmp_type; + uint8_t icmp_code; + uint16_t icmp_cksum; + union { + uint8_t ih_pptr; + struct in_addr ih_gwaddr; + struct ih_idseq { + uint16_t icd_id; + uint16_t icd_seq; + } ih_idseq; + uint32_t ih_void; + + struct ih_pmtu { + uint16_t ipm_void; + uint16_t ipm_nextmtu; + } ih_pmtu; + + struct ih_rtradv { + uint8_t irt_num_addrs; + uint8_t irt_wpa; + uint16_t irt_lifetime; + } ih_rtradv; + } icmp_hun; + union { + struct { + uint32_t its_otime; + uint32_t its_rtime; + uint32_t its_ttime; + } id_ts; + struct { + struct ip idi_ip; + } id_ip; + struct icmp_ra_addr id_radv; + uint32_t id_mask; + uint8_t id_data[1]; + } icmp_dun; +}; + +#define icmp_pptr icmp_hun.ih_pptr +#define icmp_gwaddr icmp_hun.ih_gwaddr +#define icmp_id icmp_hun.ih_idseq.icd_id +#define icmp_seq icmp_hun.ih_idseq.icd_seq +#define icmp_void icmp_hun.ih_void +#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void +#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu +#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs +#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa +#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime +#define icmp_otime icmp_dun.id_ts.its_otime +#define icmp_rtime icmp_dun.id_ts.its_rtime +#define icmp_ttime icmp_dun.id_ts.its_ttime +#define icmp_ip icmp_dun.id_ip.idi_ip +#define icmp_radv icmp_dun.id_radv +#define icmp_mask icmp_dun.id_mask +#define icmp_data icmp_dun.id_data + +#define ICMP_MINLEN 8 +#define ICMP_TSLEN (8 + 3 * sizeof (n_time)) +#define ICMP_MASKLEN 12 +#define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) +#define ICMP_ADVLEN(p) (8 + ((p)->icmp_ip.ip_hl << 2) + 8) + +#define ICMP_UNREACH 3 +#define ICMP_SOURCEQUENCH 4 +#define ICMP_ROUTERADVERT 9 +#define ICMP_ROUTERSOLICIT 10 +#define ICMP_TIMXCEED 11 +#define ICMP_PARAMPROB 12 +#define ICMP_TSTAMP 13 +#define ICMP_TSTAMPREPLY 14 +#define ICMP_IREQ 15 +#define ICMP_IREQREPLY 16 +#define ICMP_MASKREQ 17 +#define ICMP_MASKREPLY 18 +#define ICMP_MAXTYPE 18 + +#define ICMP_UNREACH_NET 0 +#define ICMP_UNREACH_HOST 1 +#define ICMP_UNREACH_PROTOCOL 2 +#define ICMP_UNREACH_PORT 3 +#define ICMP_UNREACH_NEEDFRAG 4 +#define ICMP_UNREACH_SRCFAIL 5 +#define ICMP_UNREACH_NET_UNKNOWN 6 +#define ICMP_UNREACH_HOST_UNKNOWN 7 +#define ICMP_UNREACH_ISOLATED 8 +#define ICMP_UNREACH_NET_PROHIB 9 +#define ICMP_UNREACH_HOST_PROHIB 10 +#define ICMP_UNREACH_TOSNET 11 +#define ICMP_UNREACH_TOSHOST 12 +#define ICMP_UNREACH_FILTER_PROHIB 13 +#define ICMP_UNREACH_HOST_PRECEDENCE 14 +#define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 + +#define ICMP_REDIRECT_NET 0 +#define ICMP_REDIRECT_HOST 1 +#define ICMP_REDIRECT_TOSNET 2 +#define ICMP_REDIRECT_TOSHOST 3 + +#define ICMP_TIMXCEED_INTRANS 0 +#define ICMP_TIMXCEED_REASS 1 + +#define ICMP_PARAMPROB_OPTABSENT 1 + +#define ICMP_INFOTYPE(type) \ + ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \ + (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \ + (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \ + (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \ + (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/netinet/tcp.h b/05/musl-final/include/netinet/tcp.h new file mode 100644 index 0000000..ea32ca0 --- /dev/null +++ b/05/musl-final/include/netinet/tcp.h @@ -0,0 +1,6 @@ +#ifndef _NETINET_TCP_H +#define _NETINET_TCP_H + +#include + +#endif diff --git a/05/musl-final/include/netinet/udp.h b/05/musl-final/include/netinet/udp.h new file mode 100644 index 0000000..15b9145 --- /dev/null +++ b/05/musl-final/include/netinet/udp.h @@ -0,0 +1,35 @@ +#ifndef _NETINET_UDP_H +#define _NETINET_UDP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct udphdr { + uint16_t source; + uint16_t dest; + uint16_t len; + uint16_t check; +}; + +#define uh_sport source +#define uh_dport dest +#define uh_ulen len +#define uh_sum check + +#define UDP_CORK 1 +#define UDP_ENCAP 100 + +#define UDP_ENCAP_ESPINUDP_NON_IKE 1 +#define UDP_ENCAP_ESPINUDP 2 +#define UDP_ENCAP_L2TPINUDP 3 + +#define SOL_UDP 17 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/nl_types.h b/05/musl-final/include/nl_types.h new file mode 100644 index 0000000..ca61efb --- /dev/null +++ b/05/musl-final/include/nl_types.h @@ -0,0 +1,24 @@ +#ifndef _NL_TYPES_H +#define _NL_TYPES_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define NL_SETD 1 +#define NL_CAT_LOCALE 1 + +#define __NEED_nl_item +#include + +typedef long nl_catd; + +nl_catd catopen (const char *, int); +char *catgets (nl_catd, int, int, const char *); +int catclose (nl_catd); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/paths.h b/05/musl-final/include/paths.h new file mode 100644 index 0000000..8a38ba7 --- /dev/null +++ b/05/musl-final/include/paths.h @@ -0,0 +1,40 @@ +#ifndef _PATHS_H_ +#define _PATHS_H_ + +#define _PATH_DEFPATH "/usr/local/bin:/bin:/usr/bin" +#define _PATH_STDPATH "/bin:/usr/bin:/sbin:/usr/sbin" + +#define _PATH_BSHELL "/bin/sh" +#define _PATH_CONSOLE "/dev/console" +#define _PATH_CSHELL "/bin/csh" +#define _PATH_DEVDB "/var/run/dev.db" +#define _PATH_DEVNULL "/dev/null" +#define _PATH_DRUM "/dev/drum" +#define _PATH_GSHADOW "/etc/gshadow" +#define _PATH_KLOG "/proc/kmsg" +#define _PATH_KMEM "/dev/kmem" +#define _PATH_LASTLOG "/var/log/lastlog" +#define _PATH_MAILDIR "/var/mail" +#define _PATH_MAN "/usr/share/man" +#define _PATH_MEM "/dev/mem" +#define _PATH_MNTTAB "/etc/fstab" +#define _PATH_MOUNTED "/etc/mtab" +#define _PATH_NOLOGIN "/etc/nologin" +#define _PATH_PRESERVE "/var/lib" +#define _PATH_RWHODIR "/var/spool/rwho" +#define _PATH_SENDMAIL "/usr/sbin/sendmail" +#define _PATH_SHADOW "/etc/shadow" +#define _PATH_SHELLS "/etc/shells" +#define _PATH_TTY "/dev/tty" +#define _PATH_UNIX "/boot/vmlinux" +#define _PATH_UTMP "/var/run/utmp" +#define _PATH_VI "/usr/bin/vi" +#define _PATH_WTMP "/var/log/wtmp" + +#define _PATH_DEV "/dev/" +#define _PATH_TMP "/tmp/" +#define _PATH_VARDB "/var/lib/misc/" +#define _PATH_VARRUN "/var/run/" +#define _PATH_VARTMP "/var/tmp/" + +#endif diff --git a/05/musl-final/include/poll.h b/05/musl-final/include/poll.h new file mode 100644 index 0000000..f868ab5 --- /dev/null +++ b/05/musl-final/include/poll.h @@ -0,0 +1,35 @@ +#ifndef _POLL_H +#define _POLL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define POLLIN 0x001 +#define POLLPRI 0x002 +#define POLLOUT 0x004 +#define POLLERR 0x008 +#define POLLHUP 0x010 +#define POLLNVAL 0x020 +#define POLLRDNORM 0x040 +#define POLLRDBAND 0x080 +#define POLLWRNORM 0x100 +#define POLLWRBAND 0x200 +#define POLLMSG 0x400 + +typedef unsigned int nfds_t; + +struct pollfd +{ + int fd; + short events; + short revents; +}; + +int poll (struct pollfd *, nfds_t, int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/pthread.h b/05/musl-final/include/pthread.h new file mode 100644 index 0000000..749a0e8 --- /dev/null +++ b/05/musl-final/include/pthread.h @@ -0,0 +1,185 @@ +#ifndef _PTHREAD_H +#define _PTHREAD_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_time_t +#define __NEED_struct_timespec +#define __NEED_sigset_t +#define __NEED_pthread_t +#define __NEED_pthread_attr_t +#define __NEED_pthread_mutexattr_t +#define __NEED_pthread_condattr_t +#define __NEED_pthread_rwlockattr_t +#define __NEED_pthread_barrierattr_t +#define __NEED_pthread_mutex_t +#define __NEED_pthread_cond_t +#define __NEED_pthread_rwlock_t +#define __NEED_pthread_barrier_t +#define __NEED_pthread_spinlock_t +#define __NEED_pthread_key_t +#define __NEED_pthread_once_t +#define __NEED_size_t + +#include + +#include +#include + +#define PTHREAD_CREATE_JOINABLE 0 +#define PTHREAD_CREATE_DETACHED 1 + +#define PTHREAD_MUTEX_NORMAL 0 +#define PTHREAD_MUTEX_DEFAULT 0 +#define PTHREAD_MUTEX_RECURSIVE 1 +#define PTHREAD_MUTEX_ERRORCHECK 2 + +#define PTHREAD_MUTEX_STALLED 0 +#define PTHREAD_MUTEX_ROBUST 1 + +#define PTHREAD_PRIO_NONE 0 +#define PTHREAD_PRIO_INHERIT 1 +#define PTHREAD_PRIO_PROTECT 2 + +#define PTHREAD_INHERIT_SCHED 0 +#define PTHREAD_EXPLICIT_SCHED 1 + +#define PTHREAD_SCOPE_SYSTEM 0 +#define PTHREAD_SCOPE_PROCESS 1 + +#define PTHREAD_PROCESS_PRIVATE 0 +#define PTHREAD_PROCESS_SHARED 1 + + +#define PTHREAD_MUTEX_INITIALIZER {0} +#define PTHREAD_RWLOCK_INITIALIZER {0} +#define PTHREAD_COND_INITIALIZER {0} +#define PTHREAD_ONCE_INIT 0 + + +#define PTHREAD_CANCEL_ENABLE 0 +#define PTHREAD_CANCEL_DISABLE 1 + +#define PTHREAD_CANCEL_DEFERRED 0 +#define PTHREAD_CANCEL_ASYNCHRONOUS 1 + +#define PTHREAD_CANCELLED ((void *)-1) + + +#define PTHREAD_BARRIER_SERIAL_THREAD (-1) + + +int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); +int pthread_detach(pthread_t); +void pthread_exit(void *); +int pthread_join(pthread_t, void **); + +pthread_t pthread_self(void); +int pthread_equal(pthread_t, pthread_t); + +int pthread_setcancelstate(int, int *); +int pthread_setcanceltype(int, int *); +void pthread_testcancel(void); +int pthread_cancel(pthread_t); + +int pthread_once(pthread_once_t *, void (*)(void)); + +int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +int pthread_mutex_lock(pthread_mutex_t *); +int pthread_mutex_unlock(pthread_mutex_t *); +int pthread_mutex_trylock(pthread_mutex_t *); +int pthread_mutex_timedlock(pthread_mutex_t *, const struct timespec *); +int pthread_mutex_destroy(pthread_mutex_t *); + +int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); +int pthread_cond_destroy(pthread_cond_t *); +int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); +int pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *, const struct timespec *); +int pthread_cond_broadcast(pthread_cond_t *); +int pthread_cond_signal(pthread_cond_t *); + +int pthread_rwlock_init(pthread_rwlock_t *, const pthread_rwlockattr_t *); +int pthread_rwlock_destroy(pthread_rwlock_t *); +int pthread_rwlock_rdlock(pthread_rwlock_t *); +int pthread_rwlock_tryrdlock(pthread_rwlock_t *); +int pthread_rwlock_timedrdlock(pthread_rwlock_t *, const struct timespec *); +int pthread_rwlock_wrlock(pthread_rwlock_t *); +int pthread_rwlock_trywrlock(pthread_rwlock_t *); +int pthread_rwlock_timedwrlock(pthread_rwlock_t *, const struct timespec *); +int pthread_rwlock_unlock(pthread_rwlock_t *); + +int pthread_spin_init(pthread_spinlock_t *, int); +int pthread_spin_destroy(pthread_spinlock_t *); +int pthread_spin_lock(pthread_spinlock_t *); +int pthread_spin_trylock(pthread_spinlock_t *); +int pthread_spin_unlock(pthread_spinlock_t *); + +int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, unsigned); +int pthread_barrier_destroy(pthread_barrier_t *); +int pthread_barrier_wait(pthread_barrier_t *); + +int pthread_key_create(pthread_key_t *, void (*)(void *)); +int pthread_key_delete(pthread_key_t); +void *pthread_getspecific(pthread_key_t); +int pthread_setspecific(pthread_key_t, const void *); + +int pthread_attr_init(pthread_attr_t *); +int pthread_attr_destroy(pthread_attr_t *); + +int pthread_attr_getguardsize(pthread_attr_t *, size_t *); +int pthread_attr_setguardsize(pthread_attr_t *, size_t); +int pthread_attr_getstacksize(pthread_attr_t *, size_t *); +int pthread_attr_setstacksize(pthread_attr_t *, size_t); +int pthread_attr_getdetachstate(pthread_attr_t *, int *); +int pthread_attr_setdetachstate(pthread_attr_t *, int); +int pthread_attr_getstack(pthread_attr_t *, void **, size_t *); +int pthread_attr_setstack(pthread_attr_t *, void *, size_t); +int pthread_attr_getscope(pthread_attr_t *, int *); +int pthread_attr_setscope(pthread_attr_t *, int); +int pthread_attr_getschedpolicy(pthread_attr_t *, int *); +int pthread_attr_setschedpolicy(pthread_attr_t *, int); +int pthread_attr_getschedparam(pthread_attr_t *, struct sched_param *); +int pthread_attr_setschedparam(pthread_attr_t *, const struct sched_param *); +int pthread_attr_getinheritsched(pthread_attr_t *, int *); +int pthread_attr_setinheritsched(pthread_attr_t *, int); + +int pthread_mutexattr_destroy(pthread_mutexattr_t *); +int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *, int *); +int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *, int *); +int pthread_mutexattr_getpshared(const pthread_mutexattr_t *, int *); +int pthread_mutexattr_getrobust(const pthread_mutexattr_t *, int *); +int pthread_mutexattr_gettype(const pthread_mutexattr_t *, int *); +int pthread_mutexattr_init(pthread_mutexattr_t *); +int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int); +int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int); +int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); +int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int); +int pthread_mutexattr_settype(pthread_mutexattr_t *, int); + +int pthread_barrierattr_destroy(pthread_barrierattr_t *); +int pthread_barrierattr_getpshared(const pthread_barrierattr_t *, int *); +int pthread_barrierattr_init(pthread_barrierattr_t *); +int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); + +#include + +int __setjmp(void *); +void __pthread_register_cancel(struct __ptcb *); +void __pthread_unregister_cancel(struct __ptcb *); +void __pthread_unwind_next(struct __ptcb *); + +#define pthread_cleanup_push(f, x) \ +do { struct __ptcb __cb; void (*__f)(void *) = (f); void *__x = (x); \ +if (__setjmp(__cb.__jb)) __f(__x), __pthread_unwind_next(&__cb); \ +__pthread_register_cancel(&__cb); { + +#define pthread_cleanup_pop(r) ; } \ +__pthread_unregister_cancel(&__cb); \ +if (r) __f(__x); } while (0) + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/pty.h b/05/musl-final/include/pty.h new file mode 100644 index 0000000..9444e5e --- /dev/null +++ b/05/musl-final/include/pty.h @@ -0,0 +1,17 @@ +#ifndef _PTY_H +#define _PTY_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +int openpty(int *, int *, char *, const struct termios *, const struct winsize *); + +#ifdef __cplusplus +extern } +#endif + +#endif diff --git a/05/musl-final/include/pwd.h b/05/musl-final/include/pwd.h new file mode 100644 index 0000000..5abfbfd --- /dev/null +++ b/05/musl-final/include/pwd.h @@ -0,0 +1,38 @@ +#ifndef _PWD_H +#define _PWD_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t +#define __NEED_uid_t +#define __NEED_gid_t + +#include + +struct passwd +{ + char *pw_name; + char *pw_passwd; + uid_t pw_uid; + gid_t pw_gid; + char *pw_gecos; + char *pw_dir; + char *pw_shell; +}; + +void setpwent (void); +void endpwent (void); +struct passwd *getpwent (void); + +struct passwd *getpwuid (uid_t); +struct passwd *getpwnam (const char *); +int getpwuid_r (uid_t, struct passwd *, char *, size_t, struct passwd **); +int getpwnam_r (const char *, struct passwd *, char *, size_t, struct passwd **); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/regex.h b/05/musl-final/include/regex.h new file mode 100644 index 0000000..3673bfa --- /dev/null +++ b/05/musl-final/include/regex.h @@ -0,0 +1,59 @@ +#ifndef _REGEX_H +#define _REGEX_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t + +#include + +typedef long regoff_t; + +typedef struct { + size_t re_nsub; + void *__opaque; +} regex_t; + +typedef struct { + regoff_t rm_so; + regoff_t rm_eo; +} regmatch_t; + +#define REG_EXTENDED 1 +#define REG_ICASE 2 +#define REG_NEWLINE 4 +#define REG_NOSUB 8 + +#define REG_NOTBOL 1 +#define REG_NOTEOL 2 + +#define REG_OK 0 +#define REG_NOMATCH 1 +#define REG_BADPAT 2 +#define REG_ECOLLATE 3 +#define REG_ECTYPE 4 +#define REG_EESCAPE 5 +#define REG_ESUBREG 6 +#define REG_EBRACK 7 +#define REG_EPAREN 8 +#define REG_EBRACE 9 +#define REG_BADBR 10 +#define REG_ERANGE 11 +#define REG_ESPACE 12 +#define REG_BADRPT 13 + +#define REG_ENOSYS -1 + +int regcomp(regex_t *, const char *, int); +int regexec(const regex_t *, const char *, size_t, regmatch_t [], int); +void regfree(regex_t *); + +size_t regerror(int, const regex_t *, char *, size_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/resolv.h b/05/musl-final/include/resolv.h new file mode 100644 index 0000000..3b77fc7 --- /dev/null +++ b/05/musl-final/include/resolv.h @@ -0,0 +1,143 @@ +#ifndef _RESOLV_H_ +#define _RESOLV_H_ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define MAXNS 3 +#define MAXDFLSRCH 3 +#define MAXDNSRCH 6 +#define LOCALDOMAINPARTS 2 + +#define RES_TIMEOUT 5 +#define MAXRESOLVSORT 10 +#define RES_MAXNDOTS 15 +#define RES_MAXRETRANS 30 +#define RES_MAXRETRY 5 +#define RES_DFLRETRY 2 +#define RES_MAXTIME 65535 + +/* unused; purely for broken apps */ +typedef struct __res_state { + int retrans; + int retry; + unsigned long options; + int nscount; + struct sockaddr_in nsaddr_list[MAXNS]; +# define nsaddr nsaddr_list[0] + unsigned short id; + char *dnsrch[MAXDNSRCH+1]; + char defdname[256]; + unsigned long pfcode; + unsigned ndots:4; + unsigned nsort:4; + unsigned ipv6_unavail:1; + unsigned unused:23; + struct { + struct in_addr addr; + uint32_t mask; + } sort_list[MAXRESOLVSORT]; + void *qhook; + void *rhook; + int res_h_errno; + int _vcsock; + unsigned _flags; + union { + char pad[52]; + struct { + uint16_t nscount; + uint16_t nsmap[MAXNS]; + int nssocks[MAXNS]; + uint16_t nscount6; + uint16_t nsinit; + struct sockaddr_in6 *nsaddrs[MAXNS]; + unsigned int _initstamp[2]; + } _ext; + } _u; +} *res_state; + +#define __RES 19991006 + +#ifndef _PATH_RESCONF +#define _PATH_RESCONF "/etc/resolv.conf" +#endif + +struct res_sym { + int number; + char *name; + char *humanname; +}; + +#define RES_F_VC 0x00000001 +#define RES_F_CONN 0x00000002 +#define RES_F_EDNS0ERR 0x00000004 + +#define RES_EXHAUSTIVE 0x00000001 + +#define RES_INIT 0x00000001 +#define RES_DEBUG 0x00000002 +#define RES_AAONLY 0x00000004 +#define RES_USEVC 0x00000008 +#define RES_PRIMARY 0x00000010 +#define RES_IGNTC 0x00000020 +#define RES_RECURSE 0x00000040 +#define RES_DEFNAMES 0x00000080 +#define RES_STAYOPEN 0x00000100 +#define RES_DNSRCH 0x00000200 +#define RES_INSECURE1 0x00000400 +#define RES_INSECURE2 0x00000800 +#define RES_NOALIASES 0x00001000 +#define RES_USE_INET6 0x00002000 +#define RES_ROTATE 0x00004000 +#define RES_NOCHECKNAME 0x00008000 +#define RES_KEEPTSIG 0x00010000 +#define RES_BLAST 0x00020000 +#define RES_USEBSTRING 0x00040000 +#define RES_NOIP6DOTINT 0x00080000 +#define RES_USE_EDNS0 0x00100000 +#define RES_SNGLKUP 0x00200000 +#define RES_SNGLKUPREOP 0x00400000 +#define RES_USE_DNSSEC 0x00800000 + +#define RES_DEFAULT (RES_RECURSE|RES_DEFNAMES|RES_DNSRCH|RES_NOIP6DOTINT) + +#define RES_PRF_STATS 0x00000001 +#define RES_PRF_UPDATE 0x00000002 +#define RES_PRF_CLASS 0x00000004 +#define RES_PRF_CMD 0x00000008 +#define RES_PRF_QUES 0x00000010 +#define RES_PRF_ANS 0x00000020 +#define RES_PRF_AUTH 0x00000040 +#define RES_PRF_ADD 0x00000080 +#define RES_PRF_HEAD1 0x00000100 +#define RES_PRF_HEAD2 0x00000200 +#define RES_PRF_TTLID 0x00000400 +#define RES_PRF_HEADX 0x00000800 +#define RES_PRF_QUERY 0x00001000 +#define RES_PRF_REPLY 0x00002000 +#define RES_PRF_INIT 0x00004000 + +extern struct __res_state *__res_state(void); +#define _res (*__res_state()) + +struct rrec; + +int res_init(void); +int res_query(const char *, int, int, unsigned char *, int); +int res_querydomain(const char *, const char *, int, int, unsigned char *, int); +int res_search(const char *, int, int, unsigned char *, int); +int res_mkquery(int, const char *, int, int, char *, int, struct rrec *, char *, int); +int res_send(const char *, int, char *, int); +int dn_comp(unsigned char *, unsigned char *, int, unsigned char **, unsigned char *, unsigned char **); +int dn_expand(unsigned char *, unsigned char *, unsigned char *, unsigned char *, int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/sched.h b/05/musl-final/include/sched.h new file mode 100644 index 0000000..4d8bd3d --- /dev/null +++ b/05/musl-final/include/sched.h @@ -0,0 +1,37 @@ +#ifndef _SCHED_H +#define _SCHED_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_struct_timespec +#define __NEED_pid_t +#define __NEED_time_t + +#include + +struct sched_param { + int sched_priority; + int sched_ss_low_priority; + struct timespec sched_ss_repl_period; + struct timespec sched_ss_init_budget; + int sched_ss_max_repl; +}; + +int sched_get_priority_max(int); +int sched_get_priority_min(int); +int sched_getparam(pid_t, struct sched_param *); +int sched_getscheduler(pid_t); +int sched_rr_get_interval(pid_t, struct timespec *); +int sched_setparam(pid_t, const struct sched_param *); +int sched_setscheduler(pid_t, int, const struct sched_param *); +int sched_yield(void); + +#define SCHED_OTHER 0 +#define SCHED_FIFO 1 +#define SCHED_RR 2 + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/search.h b/05/musl-final/include/search.h new file mode 100644 index 0000000..9254ed0 --- /dev/null +++ b/05/musl-final/include/search.h @@ -0,0 +1,6 @@ +#ifndef _SEARCH_H +#define _SEARCH_H + +// FIXME!!! + +#endif diff --git a/05/musl-final/include/semaphore.h b/05/musl-final/include/semaphore.h new file mode 100644 index 0000000..5b68986 --- /dev/null +++ b/05/musl-final/include/semaphore.h @@ -0,0 +1,27 @@ +#ifndef _SEMAPHORE_H +#define _SEMAPHORE_H +#ifdef __cplusplus +extern "C" { +#endif + +#define SEM_FAILED ((sem_t *)0) + +typedef struct { + long __val[4]; +} sem_t; + +int sem_close(sem_t *); +int sem_destroy(sem_t *); +int sem_getvalue(sem_t *, int *); +int sem_init(sem_t *, int, unsigned); +sem_t *sem_open(const char *, int, ...); +int sem_post(sem_t *); +int sem_timedwait(sem_t *, const struct timespec *); +int sem_trywait(sem_t *); +int sem_unlink(const char *); +int sem_wait(sem_t *); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/setjmp.h b/05/musl-final/include/setjmp.h new file mode 100644 index 0000000..b024c44 --- /dev/null +++ b/05/musl-final/include/setjmp.h @@ -0,0 +1,38 @@ +#ifndef _SETJMP_H +#define _SETJMP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +typedef unsigned long sigjmp_buf[(128+sizeof(jmp_buf))/sizeof(long)]; +#ifdef _GNU_SOURCE +#define jmp_buf sigjmp_buf +#endif +int sigsetjmp (sigjmp_buf, int); +void siglongjmp (sigjmp_buf, int); +#endif + + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +int _setjmp (jmp_buf); +void _longjmp (jmp_buf, int); +#endif + + +int setjmp (jmp_buf); +void longjmp (jmp_buf, int); + +#define setjmp setjmp +#define longjmp longjmp + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/shadow.h b/05/musl-final/include/shadow.h new file mode 100644 index 0000000..2b1be41 --- /dev/null +++ b/05/musl-final/include/shadow.h @@ -0,0 +1,44 @@ +#ifndef _SHADOW_H +#define _SHADOW_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_FILE +#define __NEED_size_t + +#include + +#define SHADOW "/etc/shadow" + +struct spwd { + char *sp_namp; + char *sp_pwdp; + long sp_lstchg; + long sp_min; + long sp_max; + long sp_warn; + long sp_inact; + long sp_expire; + unsigned long sp_flag; +}; + +void setspent(void); +void endspent(void); +struct spwd *getspent(void); +struct spwd *fgetspent(FILE *); +struct spwd *sgetspent(const char *); +int putspent(const struct spwd *, FILE *); + +struct spwd *getspnam(const char *); +int getspnam_r(const char *, struct spwd *, char *, size_t, struct spwd **); + +int lckpwdf(void); +int ulckpwdf(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/signal.h b/05/musl-final/include/signal.h new file mode 100644 index 0000000..9db7b2e --- /dev/null +++ b/05/musl-final/include/signal.h @@ -0,0 +1,105 @@ +#ifndef _SIGNAL_H +#define _SIGNAL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +#define __NEED_size_t +#define __NEED_pid_t +#define __NEED_uid_t +#define __NEED_struct_timespec +#define __NEED_pthread_t +#define __NEED_time_t +#define __NEED_clock_t +#define __NEED_sigset_t +#define __NEED_siginfo_t + +#include + +struct sigaction +{ + union { + void (*sa_handler)(int); + void (*sa_sigaction)(int, siginfo_t *, void *); + } __sa_handler; + sigset_t sa_mask; + int sa_flags; + void (*__sa_restorer)(void); +}; +#define sa_handler __sa_handler.sa_handler +#define sa_sigaction __sa_handler.sa_sigaction + +typedef struct +{ + void *ss_sp; + int ss_flags; + size_t ss_size; +} stack_t; + +union sigval +{ + int sival_int; + void *sival_ptr; +}; + +int __libc_current_sigrtmin(void); +int __libc_current_sigrtmax(void); + +#define SIGRTMIN (__libc_current_sigrtmin()) +#define SIGRTMAX (__libc_current_sigrtmax()) + +int kill(pid_t, int); + +int sigemptyset(sigset_t *); +int sigfillset(sigset_t *); +int sigaddset(sigset_t *, int); +int sigdelset(sigset_t *, int); +int sigismember(const sigset_t *, int); + +int sigprocmask(int, const sigset_t *, sigset_t *); +int sigsuspend(const sigset_t *); +int sigaction(int, const struct sigaction *, struct sigaction *); +int sigpending(sigset_t *); +int sigwait(const sigset_t *, int *); +int sigwaitinfo(const sigset_t *, siginfo_t *); +int sigtimedwait(const sigset_t *, siginfo_t *, const struct timespec *); +int sigqueue(pid_t, int, const union sigval); + +int pthread_sigmask(int, const sigset_t *, sigset_t *); +int pthread_kill(pthread_t, int); + +#endif + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +int killpg(pid_t, int); +int sigaltstack(const stack_t *, stack_t *); +int sighold(int); +int sigignore(int); +int siginterrupt(int, int); +int sigpause(int); +int sigrelse(int); +void (*sigset(int, void (*)(int)))(int); +#endif + +#ifdef _GNU_SOURCE +typedef int (*sighandler_t)(int); +void (*bsd_signal(int, void (*)(int)))(int); +#define __siginfo siginfo +#endif + +#include + +typedef int sig_atomic_t; + +void (*signal(int, void (*)(int)))(int); +int raise(int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/stdarg.h b/05/musl-final/include/stdarg.h new file mode 100644 index 0000000..1a455d2 --- /dev/null +++ b/05/musl-final/include/stdarg.h @@ -0,0 +1,87 @@ +#ifndef _STDARG_H +#define _STDARG_H + +#ifdef __GNUC__ +#define va_list __builtin_va_list +#define va_arg __builtin_va_arg +#define va_start __builtin_va_start +#define va_end __builtin_va_end +#define va_copy __builtin_va_copy +#else +#ifdef __x86_64__ +#ifndef _WIN64 + +//This should be in sync with the declaration on our lib/libtcc1.c +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; + +typedef __va_list_struct va_list[1]; + +void __va_start(__va_list_struct *ap, void *fp); +void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align); + +#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0)) +#define va_arg(ap, type) \ + (*(type *)(__va_arg(ap, __builtin_va_arg_types(type), sizeof(type), __alignof__(type)))) +#define va_copy(dest, src) (*(dest) = *(src)) +#define va_end(ap) + +/* avoid conflicting definition for va_list on Macs. */ +#define _VA_LIST_T + +#else /* _WIN64 */ +typedef char *va_list; +#define va_start(ap,last) __builtin_va_start(ap,last) +#define va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \ + ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8)) +#define va_copy(dest, src) ((dest) = (src)) +#define va_end(ap) +#endif + +#elif __arm__ +typedef char *va_list; +#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) +#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) + +#elif defined(__aarch64__) +typedef struct { + void *__stack; + void *__gr_top; + void *__vr_top; + int __gr_offs; + int __vr_offs; +} va_list; +#define va_start(ap, last) __va_start(ap, last) +#define va_arg(ap, type) __va_arg(ap, type) +#define va_end(ap) +#define va_copy(dest, src) ((dest) = (src)) + +#else /* __i386__ */ +typedef char *va_list; +/* only correct for i386 */ +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) +#endif + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED +#endif + +#endif /* _STDARG_H */ diff --git a/05/musl-final/include/stdbool.h b/05/musl-final/include/stdbool.h new file mode 100644 index 0000000..3d8fbf2 --- /dev/null +++ b/05/musl-final/include/stdbool.h @@ -0,0 +1,13 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +#ifndef __cplusplus + +#define true 1 +#define false 0 + +typedef _Bool bool; + +#endif + +#endif diff --git a/05/musl-final/include/stddef.h b/05/musl-final/include/stddef.h new file mode 100644 index 0000000..dbf5a4a --- /dev/null +++ b/05/musl-final/include/stddef.h @@ -0,0 +1,19 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#define __NEED_ptrdiff_t +#define __NEED_size_t +#define __NEED_wchar_t + +#include + +#define offsetof(type, member) ((size_t)( (char *)&(((type *)0)->member) - (char *)0 )) + +#endif diff --git a/05/musl-final/include/stdint.h b/05/musl-final/include/stdint.h new file mode 100644 index 0000000..4d24fd2 --- /dev/null +++ b/05/musl-final/include/stdint.h @@ -0,0 +1,107 @@ +#ifndef _STDINT_H +#define _STDINT_H + +#define __NEED_int8_t +#define __NEED_int16_t +#define __NEED_int32_t +#define __NEED_int64_t + +#define __NEED_uint8_t +#define __NEED_uint16_t +#define __NEED_uint32_t +#define __NEED_uint64_t + +#define __NEED_int_least8_t +#define __NEED_int_least16_t +#define __NEED_int_least32_t +#define __NEED_int_least64_t + +#define __NEED_uint_least8_t +#define __NEED_uint_least16_t +#define __NEED_uint_least32_t +#define __NEED_uint_least64_t + +#define __NEED_int_fast8_t +#define __NEED_int_fast16_t +#define __NEED_int_fast32_t +#define __NEED_int_fast64_t + +#define __NEED_uint_fast8_t +#define __NEED_uint_fast16_t +#define __NEED_uint_fast32_t +#define __NEED_uint_fast64_t + +#define __NEED_intptr_t +#define __NEED_uintptr_t +#define __NEED_intmax_t +#define __NEED_uintmax_t + +#include + +#if !defined __cplusplus || defined __STDC_LIMIT_MACROS + +#define INT8_MIN (-1-0x7f) +#define INT16_MIN (-1-0x7fff) +#define INT32_MIN (-1-0x7fffffff) +#define INT64_MIN (-1-0x7fffffffffffffffLL) + +#define INT8_MAX (0x7f) +#define INT16_MAX (0x7fff) +#define INT32_MAX (0x7fffffff) +#define INT64_MAX (0x7fffffffffffffffLL) + +#define UINT8_MAX (0xff) +#define UINT16_MAX (0xffff) +#define UINT32_MAX (0xffffffff) +#define UINT64_MAX (0xffffffffffffffffULL) + +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST64_MIN INT64_MIN + +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MAX INT64_MAX + +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +#undef WCHAR_MIN +#undef WCHAR_MAX +#undef WINT_MIN +#undef WINT_MAX +#define WCHAR_MIN INT32_MIN +#define WCHAR_MAX INT32_MAX +#define WINT_MIN INT32_MIN +#define WINT_MAX INT32_MAX + +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +#include + +#endif + +#if !defined __cplusplus || defined __STDC_CONSTANT_MACROS + +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c +#define INT64_C(c) c ## LL + +#define UINT8_C(c) c ## U +#define UINT16_C(c) c ## U +#define UINT32_C(c) c ## U +#define UINT64_C(c) c ## ULL + +#define INTMAX_C(c) c ## LL +#define UINTMAX_C(c) c ## ULL + +#endif + +#endif diff --git a/05/musl-final/include/stdio.h b/05/musl-final/include/stdio.h new file mode 100644 index 0000000..4abb1e6 --- /dev/null +++ b/05/musl-final/include/stdio.h @@ -0,0 +1,163 @@ +#ifndef _STDIO_H +#define _STDIO_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_FILE +#define __NEED_va_list +#define __NEED_size_t + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define __NEED_ssize_t +#define __NEED_off_t +#endif + +#include + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#undef EOF +#define EOF (-1) + +#undef SEEK_SET +#undef SEEK_CUR +#undef SEEK_END +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 + +#define _IOFBF 0 +#define _IOLBF 1 +#define _IONBF 2 + +#define BUFSIZ 1024 +#define FILENAME_MAX 4095 +#define FOPEN_MAX 1000 +#define TMP_MAX 10000 +#define L_tmpnam 20 + +typedef union { + char __opaque[16]; + double __align; +} fpos_t; + +extern FILE *const stdin; +extern FILE *const stdout; +extern FILE *const stderr; + +#define stdin (stdin) +#define stdout (stdout) +#define stderr (stderr) + +FILE *fopen(const char *, const char *); +FILE *freopen(const char *, const char *, FILE *); +int fclose(FILE *); + +int remove(const char *); +int rename(const char *, const char *); + +int feof(FILE *); +int ferror(FILE *); +int fflush(FILE *); +void clearerr(FILE *); + +int fseek(FILE *, long, int); +long ftell(FILE *); +void rewind(FILE *); + +int fgetpos(FILE *, fpos_t *); +int fsetpos(FILE *, const fpos_t *); + +size_t fread(void *, size_t, size_t, FILE *); +size_t fwrite(const void *, size_t, size_t, FILE *); + +int fgetc(FILE *); +int getc(FILE *); +int getchar(void); +int ungetc(int, FILE *); + +int fputc(int, FILE *); +int putc(int, FILE *); +int putchar(int); + +char *fgets(char *, int, FILE *); +char *gets(char *); + +int fputs(const char *, FILE *); +int puts(const char *); + +int printf(const char *, ...); +int fprintf(FILE *, const char *, ...); +int sprintf(char *, const char *, ...); +int snprintf(char *, size_t, const char *, ...); + +int vprintf(const char *, va_list); +int vfprintf(FILE *, const char *, va_list); +int vsprintf(char *, const char *, va_list); +int vsnprintf(char *, size_t, const char *, va_list); + +int scanf(const char *, ...); +int fscanf(FILE *, const char *, ...); +int sscanf(const char *, const char *, ...); +int vscanf(const char *, va_list); +int vfscanf(FILE *, const char *, va_list); +int vsscanf(const char *, const char *, va_list); + +void perror(const char *); + +int setvbuf(FILE *, char *, int, size_t); +void setbuf(FILE *, char *); + +char *tmpnam(char *); +FILE *tmpfile(void); + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +FILE *fdopen(int, const char *); +FILE *popen(const char *, const char *); +int pclose(FILE *); +int fileno(FILE *); +int fseeko(FILE *, off_t, int); +off_t ftello(FILE *); +int dprintf(int, const char *, ...); +int vdprintf(int, const char *, va_list); +void flockfile(FILE *); +int ftrylockfile(FILE *); +void funlockfile(FILE *); +int getc_unlocked(FILE *); +int getchar_unlocked(void); +int putc_unlocked(int, FILE *); +int putchar_unlocked(int); +ssize_t getdelim(char **, size_t *, int, FILE *); +ssize_t getline(char **, size_t *, FILE *); +int renameat(int, const char *, int, const char *); +char *ctermid(char *); +#define L_ctermid 20 +#endif + + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define P_tmpdir "/tmp" +char *tempnam(const char *, const char *); +#endif + +#if defined(_GNU_SOURCE) +#define L_cuserid 20 +char *cuserid(char *); +#undef off64_t +#define off64_t off_t +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/stdlib.h b/05/musl-final/include/stdlib.h new file mode 100644 index 0000000..047a5f3 --- /dev/null +++ b/05/musl-final/include/stdlib.h @@ -0,0 +1,134 @@ +#ifndef _STDLIB_H +#define _STDLIB_H + +#ifdef __cplusplus +extern "C" { +#endif + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#define __NEED_size_t +#define __NEED_wchar_t + +#include + +int atoi (const char *); +long atol (const char *); +long long atoll (const char *); +double atof (const char *); + +float strtof (const char *, char **); +double strtod (const char *, char **); +long double strtold (const char *, char **); + +long strtol (const char *, char **, int); +unsigned long strtoul (const char *, char **, int); +long long strtoll (const char *, char **, int); +unsigned long long strtoull (const char *, char **, int); + +int rand (void); +void srand (unsigned); + +void *malloc (size_t); +void *calloc (size_t, size_t); +void *realloc (void *, size_t); +void free (void *); + +void abort (void); +int atexit (void (*) (void)); +void exit (int); +void _Exit (int); + +char *getenv (const char *); + +int system (const char *); + +void *bsearch (const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); +void qsort (void *, size_t, size_t, int (*)(const void *, const void *)); + +int abs (int); +long labs (long); +long long llabs (long long); + +typedef struct { int quot, rem; } div_t; +typedef struct { long quot, rem; } ldiv_t; +typedef struct { long long quot, rem; } lldiv_t; + +div_t div (int, int); +ldiv_t ldiv (long, long); +lldiv_t lldiv (long long, long long); + +int mblen (const char *, size_t); +int mbtowc (wchar_t *, const char *, size_t); +int wctomb (char *, wchar_t); +size_t mbstowcs (wchar_t *, const char *, size_t); +size_t wcstombs (char *, const wchar_t *, size_t); + +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + +#define MB_CUR_MAX ((size_t)+4) + +#define RAND_MAX (0x7fffffff) + + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +#ifndef WEXITSTATUS +#include +#endif + +int posix_memalign (void **, size_t, size_t); +int setenv (const char *, const char *, int); +int unsetenv (const char *); +int mkstemp (char *); +char *mkdtemp (char *); +int getsubopt (char **, char *const *, char **); +int rand_r (unsigned *); + +#endif + + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +int putenv (char *); +int posix_openpt (int); +int grantpt (int); +int unlockpt (int); +char *ptsname (int); +char *realpath (const char *, char *); +char *l64a (long); +long a64l (const char *); +void setkey (const char *); +long int random (void); +void srandom (unsigned int); +char *initstate (unsigned int, char *, size_t); +char *setstate (char *); +double drand48 (void); +double erand48 (unsigned short [3]); +long int lrand48 (void); +long int nrand48 (unsigned short [3]); +long mrand48 (void); +long jrand48 (unsigned short [3]); +void srand48 (long); +unsigned short *seed48 (unsigned short [3]); +void lcong48 (unsigned short [7]); +#endif + +#if defined(_GNU_SOURCE) +char *mktemp (char *); +void *valloc (size_t); +void *memalign(size_t, size_t); +#endif + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/string.h b/05/musl-final/include/string.h new file mode 100644 index 0000000..fb18537 --- /dev/null +++ b/05/musl-final/include/string.h @@ -0,0 +1,78 @@ +#ifndef _STRING_H +#define _STRING_H + +#ifdef __cplusplus +extern "C" { +#endif + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#define __NEED_size_t +#include + +void *memcpy (void *, const void *, size_t); +void *memmove (void *, const void *, size_t); +void *memccpy (void *, const void *, int, size_t); +void *memset (void *, int, size_t); +int memcmp (const void *, const void *, size_t); +void *memchr (const void *, int, size_t); + +char *strcpy (char *, const char *); +char *strncpy (char *, const char *, size_t); + +char *strcat (char *, const char *); +char *strncat (char *, const char *, size_t); + +int strcmp (const char *, const char *); +int strncmp (const char *, const char *, size_t); + +int strcoll (const char *, const char *); +size_t strxfrm (char *, const char *, size_t); + +char *strchr (const char *, int); +char *strrchr (const char *, int); + +size_t strcspn (const char *, const char *); +size_t strspn (const char *, const char *); +char *strpbrk (const char *, const char *); +char *strstr (const char *, const char *); +char *strtok (char *, const char *); + +size_t strlen (const char *); + +char *strerror (int); + + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +char *strtok_r (char *, const char *, char **); +int strerror_r (int, char *, size_t); +char *stpcpy(char *, const char *); +char *stpncpy(char *, const char *, size_t); +size_t strnlen (const char *, size_t); +char *strdup (const char *); +char *strndup (const char *, size_t); +#endif + +#ifdef _BSD_SOURCE +size_t strlcat (char *, const char *, size_t); +size_t strlcpy (char *, const char *, size_t); +#endif + +#ifdef _GNU_SOURCE +int strcasecmp (const char *, const char *); +int strncasecmp (const char *, const char *, size_t); +char *strchrnul(const char *, int); +char *strcasestr(const char *, const char *); +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/strings.h b/05/musl-final/include/strings.h new file mode 100644 index 0000000..aa35668 --- /dev/null +++ b/05/musl-final/include/strings.h @@ -0,0 +1,29 @@ +#ifndef _STRINGS_H +#define _STRINGS_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#define __NEED_size_t +#include + + +int bcmp (const void *, const void *, size_t); +void bcopy (const void *, void *, size_t); +void bzero (void *, size_t); + +int ffs (int); + +char *index (const char *, int); +char *rindex (const char *, int); + +int strcasecmp (const char *, const char *); +int strncasecmp (const char *, const char *, size_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/stropts.h b/05/musl-final/include/stropts.h new file mode 100644 index 0000000..80776ba --- /dev/null +++ b/05/musl-final/include/stropts.h @@ -0,0 +1,16 @@ +#ifndef _STROPTS_H +#define _STROPTS_H + +#ifdef __cplusplus +extern "C" { +#endif + +// FIXME + +int ioctl (int, int, ...); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/sys/epoll.h b/05/musl-final/include/sys/epoll.h new file mode 100644 index 0000000..3530a9a --- /dev/null +++ b/05/musl-final/include/sys/epoll.h @@ -0,0 +1,61 @@ +#ifndef _SYS_EPOLL_H +#define _SYS_EPOLL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define __NEED_sigset_t + +#include + +#define EPOLL_CLOEXEC 02000000 +#define EPOLL_NONBLOCK 04000 + +enum EPOLL_EVENTS { __EPOLL_DUMMY }; +#define EPOLLIN 0x001 +#define EPOLLPRI 0x002 +#define EPOLLOUT 0x004 +#define EPOLLRDNORM 0x040 +#define EPOLLRDBAND 0x080 +#define EPOLLWRNORM 0x100 +#define EPOLLWRBAND 0x200 +#define EPOLLMSG 0x400 +#define EPOLLERR 0x008 +#define EPOLLHUP 0x010 +#define EPOLLRDHUP 0x2000 +#define EPOLLONESHOT (1U<<30) +#define EPOLLET (1U<<31) + +#define EPOLL_CTL_ADD 1 +#define EPOLL_CTL_DEL 2 +#define EPOLL_CTL_MOD 3 + +typedef union epoll_data { + void *ptr; + int fd; + uint32_t u32; + uint64_t u64; +} epoll_data_t; + +struct epoll_event { + uint32_t events; + epoll_data_t data; +} __attribute__ ((__packed__)); + + +int epoll_create(int); +int epoll_create1(int); +int epoll_ctl(int, int, int, struct epoll_event *); +int epoll_wait(int, struct epoll_event *, int, int); +int epoll_pwait(int, struct epoll_event *, int, int, const sigset_t *); + + +#ifdef __cplusplus +} +#endif + +#endif /* sys/epoll.h */ diff --git a/05/musl-final/include/sys/file.h b/05/musl-final/include/sys/file.h new file mode 100644 index 0000000..8b2eb01 --- /dev/null +++ b/05/musl-final/include/sys/file.h @@ -0,0 +1,21 @@ +#ifndef _SYS_FILE_H +#define _SYS_FILE_H +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) + +#define LOCK_SH 1 +#define LOCK_EX 2 +#define LOCK_NB 4 +#define LOCK_UN 8 + +extern int flock(int, int); + +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/ioctl.h b/05/musl-final/include/sys/ioctl.h new file mode 100644 index 0000000..d0415b3 --- /dev/null +++ b/05/musl-final/include/sys/ioctl.h @@ -0,0 +1,14 @@ +#ifndef _SYS_IOCTL_H +#define _SYS_IOCTL_H +#ifdef __cplusplus +extern "C" { +#endif + +#include + +int ioctl (int, int, ...); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/ipc.h b/05/musl-final/include/sys/ipc.h new file mode 100644 index 0000000..9de9f3e --- /dev/null +++ b/05/musl-final/include/sys/ipc.h @@ -0,0 +1,23 @@ +#ifndef _SYS_IPC_H +#define _SYS_IPC_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_uid_t +#define __NEED_gid_t +#define __NEED_mode_t +#define __NEED_key_t + +#include + +#include + +key_t ftok (const char *, int); + +#ifdef __cplusplus +} +#endif +#endif + + diff --git a/05/musl-final/include/sys/kd.h b/05/musl-final/include/sys/kd.h new file mode 100644 index 0000000..793fd59 --- /dev/null +++ b/05/musl-final/include/sys/kd.h @@ -0,0 +1,8 @@ +#ifndef _SYS_KD_H +#define _SYS_KD_H + +#define _LINUX_TYPES_H +#include +#undef _LINUX_TYPES_H + +#endif diff --git a/05/musl-final/include/sys/klog.h b/05/musl-final/include/sys/klog.h new file mode 100644 index 0000000..aa66684 --- /dev/null +++ b/05/musl-final/include/sys/klog.h @@ -0,0 +1,14 @@ +#ifndef _SYS_KLOG_H +#define _SYS_KLOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +int klogctl (int, char *, int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/sys/mman.h b/05/musl-final/include/sys/mman.h new file mode 100644 index 0000000..6aede8b --- /dev/null +++ b/05/musl-final/include/sys/mman.h @@ -0,0 +1,37 @@ +#ifndef _SYS_MMAN_H +#define _SYS_MMAN_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_mode_t +#define __NEED_size_t +#define __NEED_off_t + +#include + +#include + +void *mmap (void *, size_t, int, int, int, off_t); +int munmap (void *, size_t); + +int mprotect (void *, size_t, int); +int msync (void *, size_t, int); + +int posix_madvise (void *, size_t, int); + +int mlock (const void *, size_t); +int munlock (const void *, size_t); +int mlockall (int); +int munlockall (void); + +/* linux extension */ +void *mremap (void *, size_t, size_t, int, ...); + +int shm_open (const char *, int, mode_t); +int shm_unlink (const char *); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/mount.h b/05/musl-final/include/sys/mount.h new file mode 100644 index 0000000..2c685f6 --- /dev/null +++ b/05/musl-final/include/sys/mount.h @@ -0,0 +1,28 @@ +#ifndef _SYS_MOUNT_H +#define _SYS_MOUNT_H + +#define MS_RDONLY 1 +#define MS_NOSUID 2 +#define MS_NODEV 4 +#define MS_NOEXEC 8 +#define MS_SYNCHRONOUS 16 +#define MS_REMOUNT 32 +#define MS_MANDLOCK 64 +#define S_WRITE 128 +#define S_APPEND 256 +#define S_IMMUTABLE 512 +#define MS_NOATIME 1024 +#define MS_NODIRATIME 2048 +#define MS_BIND 4096 +#define MS_MOVE 8192 +#define MS_SILENT 32768 + +#define MS_MGC_VAL 0xc0ed0000 + +#define MNT_FORCE 1 + +int mount(const char *, const char *, const char *, unsigned long, const void *); +int umount(const char *); +int umount2(const char *, int); + +#endif diff --git a/05/musl-final/include/sys/msg.h b/05/musl-final/include/sys/msg.h new file mode 100644 index 0000000..9fc42bc --- /dev/null +++ b/05/musl-final/include/sys/msg.h @@ -0,0 +1,36 @@ +#ifndef _SYS_MSG_H +#define _SYS_MSG_H + +#include + +#define __NEED_pid_t +#define __NEED_key_t +#define __NEED_time_t +#define __NEED_size_t +#define __NEED_ssize_t + +#include + +typedef unsigned long msgqnum_t; +typedef unsigned long msglen_t; + +struct msqid_ds +{ + struct ipc_perm msg_perm; + time_t msg_stime; + time_t msg_rtime; + time_t msg_ctime; + msgqnum_t msg_qnum; + msglen_t msg_qbytes; + pid_t msg_lspid; + pid_t msd_lrpid; +}; + +#define MSG_NOERROR 010000 + +int msgctl (int, int, struct msqid_ds *); +int msgget (key_t, int); +int msgrcv (int, void *, size_t, long, int); +int msgsnd (int, const void *, size_t, int); + +#endif diff --git a/05/musl-final/include/sys/mtio.h b/05/musl-final/include/sys/mtio.h new file mode 100644 index 0000000..b77b7e7 --- /dev/null +++ b/05/musl-final/include/sys/mtio.h @@ -0,0 +1,276 @@ +/* Structures and definitions for magnetic tape I/O control commands. + Copyright (C) 1996-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Written by H. Bergman . */ + +#ifndef _SYS_MTIO_H +#define _SYS_MTIO_H 1 + +/* Get necessary definitions from system and kernel headers. */ +#include +#include + + +/* Structure for MTIOCTOP - magnetic tape operation command. */ +struct mtop + { + short int mt_op; /* Operations defined below. */ + int mt_count; /* How many of them. */ + }; +#define _IOT_mtop /* Hurd ioctl type field. */ \ + _IOT (_IOTS (short), 1, _IOTS (int), 1, 0, 0) + +/* Magnetic Tape operations [Not all operations supported by all drivers]. */ +#define MTRESET 0 /* +reset drive in case of problems. */ +#define MTFSF 1 /* Forward space over FileMark, + * position at first record of next file. */ +#define MTBSF 2 /* Backward space FileMark (position before FM). */ +#define MTFSR 3 /* Forward space record. */ +#define MTBSR 4 /* Backward space record. */ +#define MTWEOF 5 /* Write an end-of-file record (mark). */ +#define MTREW 6 /* Rewind. */ +#define MTOFFL 7 /* Rewind and put the drive offline (eject?). */ +#define MTNOP 8 /* No op, set status only (read with MTIOCGET). */ +#define MTRETEN 9 /* Retension tape. */ +#define MTBSFM 10 /* +backward space FileMark, position at FM. */ +#define MTFSFM 11 /* +forward space FileMark, position at FM. */ +#define MTEOM 12 /* Goto end of recorded media (for appending files). + MTEOM positions after the last FM, ready for + appending another file. */ +#define MTERASE 13 /* Erase tape -- be careful! */ + +#define MTRAS1 14 /* Run self test 1 (nondestructive). */ +#define MTRAS2 15 /* Run self test 2 (destructive). */ +#define MTRAS3 16 /* Reserved for self test 3. */ + +#define MTSETBLK 20 /* Set block length (SCSI). */ +#define MTSETDENSITY 21 /* Set tape density (SCSI). */ +#define MTSEEK 22 /* Seek to block (Tandberg, etc.). */ +#define MTTELL 23 /* Tell block (Tandberg, etc.). */ +#define MTSETDRVBUFFER 24 /* Set the drive buffering according to SCSI-2. + Ordinary buffered operation with code 1. */ +#define MTFSS 25 /* Space forward over setmarks. */ +#define MTBSS 26 /* Space backward over setmarks. */ +#define MTWSM 27 /* Write setmarks. */ + +#define MTLOCK 28 /* Lock the drive door. */ +#define MTUNLOCK 29 /* Unlock the drive door. */ +#define MTLOAD 30 /* Execute the SCSI load command. */ +#define MTUNLOAD 31 /* Execute the SCSI unload command. */ +#define MTCOMPRESSION 32/* Control compression with SCSI mode page 15. */ +#define MTSETPART 33 /* Change the active tape partition. */ +#define MTMKPART 34 /* Format the tape with one or two partitions. */ + +/* structure for MTIOCGET - mag tape get status command */ + +struct mtget + { + long int mt_type; /* Type of magtape device. */ + long int mt_resid; /* Residual count: (not sure) + number of bytes ignored, or + number of files not skipped, or + number of records not skipped. */ + /* The following registers are device dependent. */ + long int mt_dsreg; /* Status register. */ + long int mt_gstat; /* Generic (device independent) status. */ + long int mt_erreg; /* Error register. */ + /* The next two fields are not always used. */ + int mt_fileno; /* Number of current file on tape. */ + int mt_blkno; /* Current block number. */ + }; +#define _IOT_mtget /* Hurd ioctl type field. */ \ + _IOT (_IOTS (long), 7, 0, 0, 0, 0) + + +/* Constants for mt_type. Not all of these are supported, and + these are not all of the ones that are supported. */ +#define MT_ISUNKNOWN 0x01 +#define MT_ISQIC02 0x02 /* Generic QIC-02 tape streamer. */ +#define MT_ISWT5150 0x03 /* Wangtek 5150EQ, QIC-150, QIC-02. */ +#define MT_ISARCHIVE_5945L2 0x04 /* Archive 5945L-2, QIC-24, QIC-02?. */ +#define MT_ISCMSJ500 0x05 /* CMS Jumbo 500 (QIC-02?). */ +#define MT_ISTDC3610 0x06 /* Tandberg 6310, QIC-24. */ +#define MT_ISARCHIVE_VP60I 0x07 /* Archive VP60i, QIC-02. */ +#define MT_ISARCHIVE_2150L 0x08 /* Archive Viper 2150L. */ +#define MT_ISARCHIVE_2060L 0x09 /* Archive Viper 2060L. */ +#define MT_ISARCHIVESC499 0x0A /* Archive SC-499 QIC-36 controller. */ +#define MT_ISQIC02_ALL_FEATURES 0x0F /* Generic QIC-02 with all features. */ +#define MT_ISWT5099EEN24 0x11 /* Wangtek 5099-een24, 60MB, QIC-24. */ +#define MT_ISTEAC_MT2ST 0x12 /* Teac MT-2ST 155mb drive, + Teac DC-1 card (Wangtek type). */ +#define MT_ISEVEREX_FT40A 0x32 /* Everex FT40A (QIC-40). */ +#define MT_ISDDS1 0x51 /* DDS device without partitions. */ +#define MT_ISDDS2 0x52 /* DDS device with partitions. */ +#define MT_ISSCSI1 0x71 /* Generic ANSI SCSI-1 tape unit. */ +#define MT_ISSCSI2 0x72 /* Generic ANSI SCSI-2 tape unit. */ + +/* QIC-40/80/3010/3020 ftape supported drives. + 20bit vendor ID + 0x800000 (see vendors.h in ftape distribution). */ +#define MT_ISFTAPE_UNKNOWN 0x800000 /* obsolete */ +#define MT_ISFTAPE_FLAG 0x800000 + +struct mt_tape_info + { + long int t_type; /* Device type id (mt_type). */ + char *t_name; /* Descriptive name. */ + }; + +#define MT_TAPE_INFO \ + { \ + {MT_ISUNKNOWN, "Unknown type of tape device"}, \ + {MT_ISQIC02, "Generic QIC-02 tape streamer"}, \ + {MT_ISWT5150, "Wangtek 5150, QIC-150"}, \ + {MT_ISARCHIVE_5945L2, "Archive 5945L-2"}, \ + {MT_ISCMSJ500, "CMS Jumbo 500"}, \ + {MT_ISTDC3610, "Tandberg TDC 3610, QIC-24"}, \ + {MT_ISARCHIVE_VP60I, "Archive VP60i, QIC-02"}, \ + {MT_ISARCHIVE_2150L, "Archive Viper 2150L"}, \ + {MT_ISARCHIVE_2060L, "Archive Viper 2060L"}, \ + {MT_ISARCHIVESC499, "Archive SC-499 QIC-36 controller"}, \ + {MT_ISQIC02_ALL_FEATURES, "Generic QIC-02 tape, all features"}, \ + {MT_ISWT5099EEN24, "Wangtek 5099-een24, 60MB"}, \ + {MT_ISTEAC_MT2ST, "Teac MT-2ST 155mb data cassette drive"}, \ + {MT_ISEVEREX_FT40A, "Everex FT40A, QIC-40"}, \ + {MT_ISSCSI1, "Generic SCSI-1 tape"}, \ + {MT_ISSCSI2, "Generic SCSI-2 tape"}, \ + {0, NULL} \ + } + + +/* Structure for MTIOCPOS - mag tape get position command. */ + +struct mtpos + { + long int mt_blkno; /* Current block number. */ + }; +#define _IOT_mtpos /* Hurd ioctl type field. */ \ + _IOT_SIMPLE (long) + + +/* Structure for MTIOCGETCONFIG/MTIOCSETCONFIG primarily intended + as an interim solution for QIC-02 until DDI is fully implemented. */ +struct mtconfiginfo + { + long int mt_type; /* Drive type. */ + long int ifc_type; /* Interface card type. */ + unsigned short int irqnr; /* IRQ number to use. */ + unsigned short int dmanr; /* DMA channel to use. */ + unsigned short int port; /* IO port base address. */ + + unsigned long int debug; /* Debugging flags. */ + + unsigned have_dens:1; + unsigned have_bsf:1; + unsigned have_fsr:1; + unsigned have_bsr:1; + unsigned have_eod:1; + unsigned have_seek:1; + unsigned have_tell:1; + unsigned have_ras1:1; + unsigned have_ras2:1; + unsigned have_ras3:1; + unsigned have_qfa:1; + + unsigned pad1:5; + char reserved[10]; + }; +#define _IOT_mtconfiginfo /* Hurd ioctl type field. */ \ + _IOT (_IOTS (long), 2, _IOTS (short), 3, _IOTS (long), 1) /* XXX wrong */ + + +/* Magnetic tape I/O control commands. */ +#define MTIOCTOP _IOW('m', 1, struct mtop) /* Do a mag tape op. */ +#define MTIOCGET _IOR('m', 2, struct mtget) /* Get tape status. */ +#define MTIOCPOS _IOR('m', 3, struct mtpos) /* Get tape position.*/ + +/* The next two are used by the QIC-02 driver for runtime reconfiguration. + See tpqic02.h for struct mtconfiginfo. */ +#define MTIOCGETCONFIG _IOR('m', 4, struct mtconfiginfo) /* Get tape config.*/ +#define MTIOCSETCONFIG _IOW('m', 5, struct mtconfiginfo) /* Set tape config.*/ + +/* Generic Mag Tape (device independent) status macros for examining + mt_gstat -- HP-UX compatible. + There is room for more generic status bits here, but I don't + know which of them are reserved. At least three or so should + be added to make this really useful. */ +#define GMT_EOF(x) ((x) & 0x80000000) +#define GMT_BOT(x) ((x) & 0x40000000) +#define GMT_EOT(x) ((x) & 0x20000000) +#define GMT_SM(x) ((x) & 0x10000000) /* DDS setmark */ +#define GMT_EOD(x) ((x) & 0x08000000) /* DDS EOD */ +#define GMT_WR_PROT(x) ((x) & 0x04000000) +/* #define GMT_ ? ((x) & 0x02000000) */ +#define GMT_ONLINE(x) ((x) & 0x01000000) +#define GMT_D_6250(x) ((x) & 0x00800000) +#define GMT_D_1600(x) ((x) & 0x00400000) +#define GMT_D_800(x) ((x) & 0x00200000) +/* #define GMT_ ? ((x) & 0x00100000) */ +/* #define GMT_ ? ((x) & 0x00080000) */ +#define GMT_DR_OPEN(x) ((x) & 0x00040000) /* Door open (no tape). */ +/* #define GMT_ ? ((x) & 0x00020000) */ +#define GMT_IM_REP_EN(x) ((x) & 0x00010000) /* Immediate report mode.*/ +/* 16 generic status bits unused. */ + + +/* SCSI-tape specific definitions. Bitfield shifts in the status */ +#define MT_ST_BLKSIZE_SHIFT 0 +#define MT_ST_BLKSIZE_MASK 0xffffff +#define MT_ST_DENSITY_SHIFT 24 +#define MT_ST_DENSITY_MASK 0xff000000 + +#define MT_ST_SOFTERR_SHIFT 0 +#define MT_ST_SOFTERR_MASK 0xffff + +/* Bitfields for the MTSETDRVBUFFER ioctl. */ +#define MT_ST_OPTIONS 0xf0000000 +#define MT_ST_BOOLEANS 0x10000000 +#define MT_ST_SETBOOLEANS 0x30000000 +#define MT_ST_CLEARBOOLEANS 0x40000000 +#define MT_ST_WRITE_THRESHOLD 0x20000000 +#define MT_ST_DEF_BLKSIZE 0x50000000 +#define MT_ST_DEF_OPTIONS 0x60000000 + +#define MT_ST_BUFFER_WRITES 0x1 +#define MT_ST_ASYNC_WRITES 0x2 +#define MT_ST_READ_AHEAD 0x4 +#define MT_ST_DEBUGGING 0x8 +#define MT_ST_TWO_FM 0x10 +#define MT_ST_FAST_MTEOM 0x20 +#define MT_ST_AUTO_LOCK 0x40 +#define MT_ST_DEF_WRITES 0x80 +#define MT_ST_CAN_BSR 0x100 +#define MT_ST_NO_BLKLIMS 0x200 +#define MT_ST_CAN_PARTITIONS 0x400 +#define MT_ST_SCSI2LOGICAL 0x800 + +/* The mode parameters to be controlled. Parameter chosen with bits 20-28. */ +#define MT_ST_CLEAR_DEFAULT 0xfffff +#define MT_ST_DEF_DENSITY (MT_ST_DEF_OPTIONS | 0x100000) +#define MT_ST_DEF_COMPRESSION (MT_ST_DEF_OPTIONS | 0x200000) +#define MT_ST_DEF_DRVBUFFER (MT_ST_DEF_OPTIONS | 0x300000) + +/* The offset for the arguments for the special HP changer load command. */ +#define MT_ST_HPLOADER_OFFSET 10000 + + +/* Specify default tape device. */ +#ifndef DEFTAPE +# define DEFTAPE "/dev/tape" +#endif + +#endif /* mtio.h */ diff --git a/05/musl-final/include/sys/param.h b/05/musl-final/include/sys/param.h new file mode 100644 index 0000000..ded4a16 --- /dev/null +++ b/05/musl-final/include/sys/param.h @@ -0,0 +1,15 @@ +#undef MAXSYMLINKS +#define MAXSYMLINKS 20 + +#undef MAXHOSTNAMELEN +#define MAXHOSTNAMELEN 64 + +#undef MAXNAMLEN +#define MAXNAMLEN NAME_MAX + +#undef MAXPATHLEN +#define MAXPATHLEN PATH_MAX + +#include +#include +#include diff --git a/05/musl-final/include/sys/poll.h b/05/musl-final/include/sys/poll.h new file mode 100644 index 0000000..779ec77 --- /dev/null +++ b/05/musl-final/include/sys/poll.h @@ -0,0 +1 @@ +#include diff --git a/05/musl-final/include/sys/prctl.h b/05/musl-final/include/sys/prctl.h new file mode 100644 index 0000000..cd34848 --- /dev/null +++ b/05/musl-final/include/sys/prctl.h @@ -0,0 +1,64 @@ +#ifndef _SYS_PRCTL_H +#define _SYS_PRCTL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define PR_SET_PDEATHSIG 1 +#define PR_GET_PDEATHSIG 2 +#define PR_GET_DUMPABLE 3 +#define PR_SET_DUMPABLE 4 +#define PR_GET_UNALIGN 5 +#define PR_SET_UNALIGN 6 +#define PR_UNALIGN_NOPRINT 1 +#define PR_UNALIGN_SIGBUS 2 +#define PR_GET_KEEPCAPS 7 +#define PR_SET_KEEPCAPS 8 +#define PR_GET_FPEMU 9 +#define PR_SET_FPEMU 10 +#define PR_FPEMU_NOPRINT 1 +#define PR_FPEMU_SIGFPE 2 +#define PR_GET_FPEXC 11 +#define PR_SET_FPEXC 12 +#define PR_FP_EXC_SW_ENABLE 0x80 +#define PR_FP_EXC_DIV 0x010000 +#define PR_FP_EXC_OVF 0x020000 +#define PR_FP_EXC_UND 0x040000 +#define PR_FP_EXC_RES 0x080000 +#define PR_FP_EXC_INV 0x100000 +#define PR_FP_EXC_DISABLED 0 +#define PR_FP_EXC_NONRECOV 1 +#define PR_FP_EXC_ASYNC 2 +#define PR_FP_EXC_PRECISE 3 +#define PR_GET_TIMING 13 +#define PR_SET_TIMING 14 +#define PR_TIMING_STATISTICAL 0 +#define PR_TIMING_TIMESTAMP 1 +#define PR_SET_NAME 15 +#define PR_GET_NAME 16 +#define PR_GET_ENDIAN 19 +#define PR_SET_ENDIAN 20 +#define PR_ENDIAN_BIG +#define PR_ENDIAN_LITTLE +#define PR_ENDIAN_PPC_LITTLE +#define PR_GET_SECCOMP 21 +#define PR_SET_SECCOMP 22 +#define PR_CAPBSET_READ 23 +#define PR_CAPBSET_DROP 24 +#define PR_GET_TSC 25 +#define PR_SET_TSC 26 +#define PR_TSC_ENABLE 1 +#define PR_TSC_SIGSEGV 2 +#define PR_GET_SECUREBITS 27 +#define PR_SET_SECUREBITS 28 +#define PR_SET_TIMERSLACK 29 +#define PR_GET_TIMERSLACK 30 + +int prctl (int, ...); + +#ifdef __cplusplus +extern "C" { +#endif + +#endif diff --git a/05/musl-final/include/sys/procfs.h b/05/musl-final/include/sys/procfs.h new file mode 100644 index 0000000..20b4847 --- /dev/null +++ b/05/musl-final/include/sys/procfs.h @@ -0,0 +1,81 @@ +#ifndef _SYS_PROCFS_H +#define _SYS_PROCFS_H +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +typedef unsigned long elf_greg_t; +#define ELF_NGREG (sizeof (struct user_regs_struct) / sizeof(elf_greg_t)) +typedef elf_greg_t elf_gregset_t[ELF_NGREG]; + +#if __WORDSIZE == 32 +typedef struct user_fpregs_struct elf_fpregset_t; +typedef struct user_fpxregs_struct elf_fpxregset_t; +#else +typedef struct user_fpregs_struct elf_fpregset_t; +#endif + +struct elf_siginfo { + int si_signo; + int si_code; + int si_errno; +}; + +struct elf_prstatus { + struct elf_siginfo pr_info; + short int pr_cursig; + unsigned long int pr_sigpend; + unsigned long int pr_sighold; + pid_t pr_pid; + pid_t pr_ppid; + pid_t pr_pgrp; + pid_t pr_sid; + struct timeval pr_utime; + struct timeval pr_stime; + struct timeval pr_cutime; + struct timeval pr_cstime; + elf_gregset_t pr_reg; + int pr_fpvalid; +}; + + +#define ELF_PRARGSZ 80 + +struct elf_prpsinfo + { + char pr_state; + char pr_sname; + char pr_zomb; + char pr_nice; + unsigned long int pr_flag; +#if __WORDSIZE == 32 + unsigned short int pr_uid; + unsigned short int pr_gid; +#else + unsigned int pr_uid; + unsigned int pr_gid; +#endif + int pr_pid, pr_ppid, pr_pgrp, pr_sid; + char pr_fname[16]; + char pr_psargs[ELF_PRARGSZ]; +}; + + +typedef void *psaddr_t; +typedef elf_gregset_t prgregset_t; +typedef elf_fpregset_t prfpregset_t; +typedef pid_t lwpid_t; +typedef struct elf_prstatus prstatus_t; +typedef struct elf_prpsinfo prpsinfo_t; + + + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/ptrace.h b/05/musl-final/include/sys/ptrace.h new file mode 100644 index 0000000..0459d1b --- /dev/null +++ b/05/musl-final/include/sys/ptrace.h @@ -0,0 +1,77 @@ +#ifndef _SYS_PTRACE_H +#define _SYS_PTRACE_H +#ifdef __cplusplus +extern "C" { +#endif + +#define PTRACE_TRACEME 0 +#define PT_TRACE_ME PTRACE_TRACEME + +#define PTRACE_PEEKTEXT 1 +#define PTRACE_PEEKDATA 2 +#define PTRACE_PEEKUSER 3 +#define PTRACE_POKETEXT 4 +#define PTRACE_POKEDATA 5 +#define PTRACE_POKEUSER 6 +#define PTRACE_CONT 7 +#define PTRACE_KILL 8 +#define PTRACE_SINGLESTEP 9 +#define PTRACE_GETREGS 12 +#define PTRACE_SETREGS 13 +#define PTRACE_GETFPREGS 14 +#define PTRACE_SETFPREGS 15 +#define PTRACE_ATTACH 16 +#define PTRACE_DETACH 17 +#define PTRACE_GETFPXREGS 18 +#define PTRACE_SETFPXREGS 19 +#define PTRACE_SYSCALL 24 +#define PTRACE_SETOPTIONS 0x4200 +#define PTRACE_GETEVENTMSG 0x4201 +#define PTRACE_GETSIGINFO 0x4202 +#define PTRACE_SETSIGINFO 0x4203 + +#define PT_READ_I PTRACE_PEEKTEXT +#define PT_READ_D PTRACE_PEEKDATA +#define PT_READ_U PTRACE_PEEKUSER +#define PT_WRITE_I PTRACE_POKETEXT +#define PT_WRITE_D PTRACE_POKEDATA +#define PT_WRITE_U PTRACE_POKEUSER +#define PT_CONTINUE PTRACE_CONT +#define PT_KILL PTRACE_KILL +#define PT_STEP PTRACE_SINGLESTEP +#define PT_GETREGS PTRACE_GETREGS +#define PT_SETREGS PTRACE_SETREGS +#define PT_GETFPREGS PTRACE_GETFPREGS +#define PT_SETFPREGS PTRACE_SETFPREGS +#define PT_ATTACH PTRACE_ATTACH +#define PT_DETACH PTRACE_DETACH +#define PT_GETFPXREGS PTRACE_GETFPXREGS +#define PT_SETFPXREGS PTRACE_SETFPXREGS +#define PT_SYSCALL PTRACE_SYSCALL +#define PT_SETOPTIONS PTRACE_SETOPTIONS +#define PT_GETEVENTMSG PTRACE_GETEVENTMSG +#define PT_GETSIGINFO PTRACE_GETSIGINFO +#define PT_SETSIGINFO PTRACE_SETSIGINFO + +#define PTRACE_O_TRACESYSGOOD 0x00000001 +#define PTRACE_O_TRACEFORK 0x00000002 +#define PTRACE_O_TRACEVFORK 0x00000004 +#define PTRACE_O_TRACECLONE 0x00000008 +#define PTRACE_O_TRACEEXEC 0x00000010 +#define PTRACE_O_TRACEVFORKDONE 0x00000020 +#define PTRACE_O_TRACEEXIT 0x00000040 +#define PTRACE_O_MASK 0x0000007f + +#define PTRACE_EVENT_FORK 1 +#define PTRACE_EVENT_VFORK 2 +#define PTRACE_EVENT_CLONE 3 +#define PTRACE_EVENT_EXEC 4 +#define PTRACE_EVENT_VFORK_DONE 5 +#define PTRACE_EVENT_EXIT 6 + +long int ptrace(int, ...); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/reboot.h b/05/musl-final/include/sys/reboot.h new file mode 100644 index 0000000..26cc308 --- /dev/null +++ b/05/musl-final/include/sys/reboot.h @@ -0,0 +1,18 @@ +#ifndef _SYS_REBOOT_H +#define _SYS_REBOOT_H +#ifdef __cplusplus +extern "C" { +#endif + +#define RB_AUTOBOOT 0x01234567 +#define RB_HALT_SYSTEM 0xcdef0123 +#define RB_ENABLE_CAD 0x89abcdef +#define RB_DISABLE_CAD 0 +#define RB_POWER_OFF 0x4321fedc + +int reboot(int); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/reg.h b/05/musl-final/include/sys/reg.h new file mode 100644 index 0000000..ebf3fff --- /dev/null +++ b/05/musl-final/include/sys/reg.h @@ -0,0 +1,9 @@ +#ifndef _SYS_USER_H +#define _SYS_USER_H + +#include +#include + +#include + +#endif diff --git a/05/musl-final/include/sys/resource.h b/05/musl-final/include/sys/resource.h new file mode 100644 index 0000000..99e383c --- /dev/null +++ b/05/musl-final/include/sys/resource.h @@ -0,0 +1,74 @@ +#ifndef _SYS_RESOURCE_H +#define _SYS_RESOURCE_H + +#define __NEED_id_t +#define __NEED_time_t +#define __NEED_struct_timeval +#define __NEED_struct_rusage + +#include + +typedef unsigned long long rlim_t; + +struct rlimit +{ + rlim_t rlim_cur; + rlim_t rlim_max; +}; + +struct rusage +{ + struct timeval ru_utime; + struct timeval ru_stime; + /* linux extentions, but useful */ + long ru_maxrss; + long ru_ixrss; + long ru_idrss; + long ru_isrss; + long ru_minflt; + long ru_majflt; + long ru_nswap; + long ru_inblock; + long ru_oublock; + long ru_msgsnd; + long ru_msgrcv; + long ru_nsignals; + long ru_nvcsw; + long ru_nivcsw; + /* room for more... */ + long __reserved[16]; +}; + +int getrlimit (int, struct rlimit *); +int setrlimit (int, const struct rlimit *); +int getrusage (int, struct rusage *); + +int getpriority (int, id_t); +int setpriority (int, id_t, int); + +#define PRIO_PROCESS 0 +#define PRIO_PGRP 1 +#define PRIO_USER 2 + +#define RUSAGE_SELF 0 +#define RUSAGE_CHILDREN 1 + +#define RLIM_INFINITY (~0ULL) +#define RLIM_SAVED_CUR RLIM_INFINITY +#define RLIM_SAVED_MAX RLIM_INFINITY + +#define RLIMIT_CPU 0 +#define RLIMIT_FSIZE 1 +#define RLIMIT_DATA 2 +#define RLIMIT_STACK 3 +#define RLIMIT_CORE 4 +#define RLIMIT_RSS 5 +#define RLIMIT_NOFILE 7 +#define RLIMIT_AS 9 +#define RLIMIT_NPROC 6 +#define RLIMIT_MEMLOCK 8 +#define RLIMIT_LOCKS 10 + + + +#endif diff --git a/05/musl-final/include/sys/select.h b/05/musl-final/include/sys/select.h new file mode 100644 index 0000000..c44eada --- /dev/null +++ b/05/musl-final/include/sys/select.h @@ -0,0 +1,34 @@ +#ifndef _SYS_SELECT_H +#define _SYS_SELECT_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t +#define __NEED_time_t +#define __NEED_struct_timeval +#define __NEED_struct_timespec +#define __NEED_sigset_t + +#include + +#define FD_SETSIZE 1024 + +typedef struct +{ + unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)]; +} fd_set; + +#define FD_ZERO(s) do { int __i; unsigned long *__b=(s)->fds_bits; for(__i=sizeof (fd_set)/sizeof (long); __i; __i--) *__b++=0; } while(0) +#define FD_SET(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] |= (1<<((d)%(8*sizeof(long))))) +#define FD_CLR(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] &= ~(1<<((d)%(8*sizeof(long))))) +#define FD_ISSET(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] & (1<<((d)%(8*sizeof(long))))) + +int select (int, fd_set *, fd_set *, fd_set *, struct timeval *); +int pselect (int, fd_set *, fd_set *, fd_set *, const struct timespec *, const sigset_t *); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/sem.h b/05/musl-final/include/sys/sem.h new file mode 100644 index 0000000..15d8b68 --- /dev/null +++ b/05/musl-final/include/sys/sem.h @@ -0,0 +1,69 @@ +#ifndef _SYS_SEM_H +#define _SYS_SEM_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t +#include + +#include + +#define SEM_UNDO 0x1000 +#define GETPID 11 +#define GETVAL 12 +#define GETALL 13 +#define GETNCNT 14 +#define GETZCNT 15 +#define SETVAL 16 +#define SETALL 17 + +struct semid_ds { + struct ipc_perm sem_perm; + long sem_otime; + unsigned long __unused1; + long sem_ctime; + unsigned long __unused2; + unsigned long sem_nsems; + unsigned long __unused3; + unsigned long __unused4; +}; + +#define _SEM_SEMUN_UNDEFINED 1 + +#define SEM_STAT 18 +#define SEM_INFO 19 + +struct seminfo { + int semmap; + int semmni; + int semmns; + int semmnu; + int semmsl; + int semopm; + int semume; + int semusz; + int semvmx; + int semaem; +}; + +struct sembuf { + unsigned short sem_num; + short sem_op; + short sem_flg; +}; + +int semctl(int, int, int, ...); +int semget(key_t, int, int); +int semop(int, struct sembuf *, size_t); + +#ifdef _GNU_SOURCE +#define __NEED_struct_timespec +#include +int semtimedop(int, struct sembuf *, size_t, const struct timespec *); +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/shm.h b/05/musl-final/include/sys/shm.h new file mode 100644 index 0000000..6ebb941 --- /dev/null +++ b/05/musl-final/include/sys/shm.h @@ -0,0 +1,18 @@ +#ifndef _SYS_SHM_H +#define _SYS_SHM_H + +#define __NEED_time_t +#define __NEED_size_t +#define __NEED_pid_t + +#include + +#include +#include + +void *shmat(int, const void *, int); +int shmctl(int, int, struct shmid_ds *); +int shmdt(const void *); +int shmget(key_t, size_t, int); + +#endif diff --git a/05/musl-final/include/sys/signalfd.h b/05/musl-final/include/sys/signalfd.h new file mode 100644 index 0000000..895664b --- /dev/null +++ b/05/musl-final/include/sys/signalfd.h @@ -0,0 +1,40 @@ +#ifndef _SYS_SIGNALFD_H +#define _SYS_SIGNALFD_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define __NEED_sigset_t + +#include + +int signalfd(int, const sigset_t *, int); + +struct signalfd_siginfo { + uint32_t ssi_signo; + int32_t ssi_errno; + int32_t ssi_code; + uint32_t ssi_pid; + uint32_t ssi_uid; + int32_t ssi_fd; + uint32_t ssi_tid; + uint32_t ssi_band; + uint32_t ssi_overrun; + uint32_t ssi_trapno; + int32_t ssi_status; + int32_t ssi_int; + uintptr_t ssi_ptr; + uint64_t ssi_utime; + uint64_t ssi_stime; + uint64_t ssi_addr; + uint8_t pad[128-12*4-sizeof(void *)-3*8]; +}; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/sys/socket.h b/05/musl-final/include/sys/socket.h new file mode 100644 index 0000000..4005eb6 --- /dev/null +++ b/05/musl-final/include/sys/socket.h @@ -0,0 +1,65 @@ +#ifndef _SYS_SOCKET_H +#define _SYS_SOCKET_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_socklen_t +#define __NEED_sa_family_t +#define __NEED_size_t +#define __NEED_ssize_t +#define __NEED_uid_t +#define __NEED_pid_t +#define __NEED_gid_t +#define __NEED_struct_iovec + +#include + +#include + +struct sockaddr +{ + sa_family_t sa_family; + char sa_data[14]; +}; + +struct sockaddr_storage +{ + sa_family_t ss_family; + long long __ss_align; + char __ss_padding[128 - sizeof(sa_family_t) - sizeof(long long)]; +}; + +int socket (int, int, int); +int socketpair (int, int, int, int [2]); + +int shutdown (int, int); + +int bind (int, const struct sockaddr *, socklen_t); +int connect (int, const struct sockaddr *, socklen_t); +int listen (int, int); +int accept (int, struct sockaddr *, socklen_t *); + +int getsockname (int, struct sockaddr *, socklen_t *); +int getpeername (int, struct sockaddr *, socklen_t *); + +ssize_t send (int, const void *, size_t, int); +ssize_t recv (int, void *, size_t, int); +ssize_t sendto (int, const void *, size_t, int, const struct sockaddr *, socklen_t); +ssize_t recvfrom (int, void *, size_t, int, struct sockaddr *, socklen_t *); +ssize_t sendmsg (int, const struct msghdr *, int); +ssize_t recvmsg (int, struct msghdr *, int); + +int getsockopt (int, int, int, void *, socklen_t *); +int setsockopt (int, int, int, const void *, socklen_t); + +int sockatmark (int); + +#define SHUT_RD 0 +#define SHUT_WR 1 +#define SHUT_RDWR 2 + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/soundcard.h b/05/musl-final/include/sys/soundcard.h new file mode 100644 index 0000000..fade986 --- /dev/null +++ b/05/musl-final/include/sys/soundcard.h @@ -0,0 +1 @@ +#include diff --git a/05/musl-final/include/sys/stat.h b/05/musl-final/include/sys/stat.h new file mode 100644 index 0000000..627890f --- /dev/null +++ b/05/musl-final/include/sys/stat.h @@ -0,0 +1,95 @@ +#ifndef _SYS_STAT_H +#define _SYS_STAT_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_dev_t +#define __NEED_ino_t +#define __NEED_mode_t +#define __NEED_nlink_t +#define __NEED_uid_t +#define __NEED_gid_t +#define __NEED_off_t +#define __NEED_time_t +#define __NEED_blksize_t +#define __NEED_blkcnt_t +#define __NEED_struct_timespec + +#include + +#include + +#define st_atime st_atim.tv_sec +#define st_mtime st_mtim.tv_sec +#define st_ctime st_ctim.tv_sec + +#define S_IFMT 0170000 + +#define S_IFDIR 0040000 +#define S_IFCHR 0020000 +#define S_IFBLK 0060000 +#define S_IFREG 0100000 +#define S_IFIFO 0010000 +#define S_IFLNK 0120000 +#define S_IFSOCK 0140000 + +#define S_TYPEISMQ(buf) 0 +#define S_TYPEISSEM(buf) 0 +#define S_TYPEISSHM(buf) 0 +#define S_TYPEISTMO(buf) 0 + +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) +#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) +#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) +#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) +#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) +#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) + +#define S_ISUID 04000 +#define S_ISGID 02000 +#define S_ISVTX 01000 + +#define S_IREAD 0400 +#define S_IWRITE 0200 +#define S_IEXEC 0100 + +#define S_IRUSR 0400 +#define S_IWUSR 0200 +#define S_IXUSR 0100 +#define S_IRWXU 0700 + +#define S_IRGRP 0040 +#define S_IWGRP 0020 +#define S_IXGRP 0010 +#define S_IRWXG 0070 + +#define S_IROTH 0004 +#define S_IWOTH 0002 +#define S_IXOTH 0001 +#define S_IRWXO 0007 + + +int stat(const char *, struct stat *); +int fstat(int, struct stat *); +int lstat(const char *, struct stat *); +int fstatat(int, const char *, struct stat *, int); +int chmod(const char *, mode_t); +int fchmod(int, mode_t); +int fchmodat(int, const char *, mode_t, int); +mode_t umask(mode_t); +int mkdir(const char *, mode_t); +int mknod(const char *, mode_t, dev_t); +int mkfifo(const char *, mode_t); +int mkdirat(int, const char *, mode_t); +int mknodat(int, const char *, mode_t, dev_t); +int mkfifoat(int, const char *, mode_t); + + +#ifdef __cplusplus +} +#endif +#endif + + diff --git a/05/musl-final/include/sys/statfs.h b/05/musl-final/include/sys/statfs.h new file mode 100644 index 0000000..7eaa7e7 --- /dev/null +++ b/05/musl-final/include/sys/statfs.h @@ -0,0 +1,10 @@ +#ifndef _SYS_STATFS_H +#define _SYS_STATFS_H + +#include + +#define statfs statvfs +#define fstatfs fstatvfs +#define f_namelen f_namemax + +#endif diff --git a/05/musl-final/include/sys/statvfs.h b/05/musl-final/include/sys/statvfs.h new file mode 100644 index 0000000..6479be6 --- /dev/null +++ b/05/musl-final/include/sys/statvfs.h @@ -0,0 +1,30 @@ +#ifndef _SYS_STATVFS_H +#define _SYS_STATVFS_H + + +#define __NEED_fsblkcnt_t +#define __NEED_fsfilcnt_t +#include + +#include + +int statvfs (const char *, struct statvfs *); +int fstatvfs (int, struct statvfs *); + +#define ST_RDONLY 1 +#define ST_NOSUID 2 + +#if 0 +#define ST_NODEV 4 +#define ST_NOEXEC 8 +#define ST_SYNCHRONOUS 16 +#define ST_MANDLOCK 64 +#define ST_WRITE 128 +#define ST_APPEND 256 +#define ST_IMMUTABLE 512 +#define ST_NOATIME 1024 +#define ST_NODIRATIME 2048 +#endif + + +#endif diff --git a/05/musl-final/include/sys/stropts.h b/05/musl-final/include/sys/stropts.h new file mode 100644 index 0000000..5b5bc02 --- /dev/null +++ b/05/musl-final/include/sys/stropts.h @@ -0,0 +1 @@ +#include diff --git a/05/musl-final/include/sys/swap.h b/05/musl-final/include/sys/swap.h new file mode 100644 index 0000000..c5824f1 --- /dev/null +++ b/05/musl-final/include/sys/swap.h @@ -0,0 +1,11 @@ +#ifndef _SYS_SWAP_H +#define _SYS_SWAP_H + +#define SWAP_FLAG_PREFER 0x8000 +#define SWAP_FLAG_PRIO_MASK 0x7fff +#define SWAP_FLAG_PRIO_SHIFT 0 + +int swapon (const char *, int); +int swapoff (const char *); + +#endif diff --git a/05/musl-final/include/sys/sysctl.h b/05/musl-final/include/sys/sysctl.h new file mode 100644 index 0000000..af5ca8b --- /dev/null +++ b/05/musl-final/include/sys/sysctl.h @@ -0,0 +1,9 @@ +#ifndef _SYS_SYSCTL_H +#define _SYS_SYSCTL_H + +#define __NEED_size_t +#include + +int sysctl (int *, int, void *, size_t *, void *, size_t); + +#endif diff --git a/05/musl-final/include/sys/sysinfo.h b/05/musl-final/include/sys/sysinfo.h new file mode 100644 index 0000000..ca52088 --- /dev/null +++ b/05/musl-final/include/sys/sysinfo.h @@ -0,0 +1,29 @@ +#ifndef _SYS_SYSINFO_H +#define _SYS_SYSINFO_H + +/* ?? */ +#define SI_LOAD_SHIFT 16 + +struct sysinfo { + unsigned long long uptime; + unsigned long loads[3]; + unsigned long procs; + unsigned long long totalram; + unsigned long long freeram; + unsigned long long sharedram; + unsigned long long bufferram; + unsigned long long totalswap; + unsigned long long freeswap; + unsigned long long totalhigh; + unsigned long long freehigh; + unsigned long mem_unit; + char __reserved[256]; +}; + +int sysinfo (struct sysinfo *); +int get_nprocs_conf (void); +int get_nprocs (void); +long long get_phys_pages (void); +long long get_avphys_pages (void); + +#endif diff --git a/05/musl-final/include/sys/sysmacros.h b/05/musl-final/include/sys/sysmacros.h new file mode 100644 index 0000000..79eba3b --- /dev/null +++ b/05/musl-final/include/sys/sysmacros.h @@ -0,0 +1,6 @@ +#ifndef _SYSMACROS_H +#define _SYSMACROS_H + +#include + +#endif diff --git a/05/musl-final/include/sys/time.h b/05/musl-final/include/sys/time.h new file mode 100644 index 0000000..edaf6af --- /dev/null +++ b/05/musl-final/include/sys/time.h @@ -0,0 +1,49 @@ +#ifndef _SYS_TIME_H +#define _SYS_TIME_H +#ifdef __cplusplus +extern "C" { +#endif + +/* All symbols from select.h except pselect are required anyway... */ +#include + +#define __NEED_time_t +#define __NEED_suseconds_t +#define __NEED_struct_timeval + +#include + + + +int gettimeofday (struct timeval *, void *); + +/* extensions */ +int settimeofday (const struct timeval *, void *); +int adjtime (const struct timeval *, struct timeval *); + + +#define ITIMER_REAL 0 +#define ITIMER_VIRTUAL 1 +#define ITIMER_PROF 2 + +struct itimerval +{ + struct timeval it_interval; + struct timeval it_value; +}; + +int getitimer (int, struct itimerval *); +int setitimer (int, const struct itimerval *, struct itimerval *); +int utimes (const char *, const struct timeval [2]); + +#ifdef _GNU_SOURCE +struct timezone { + int tz_minuteswest; + int tz_dsttime; +}; +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/times.h b/05/musl-final/include/sys/times.h new file mode 100644 index 0000000..aca743d --- /dev/null +++ b/05/musl-final/include/sys/times.h @@ -0,0 +1,17 @@ +#ifndef _SYS_TIMES_H +#define _SYS_TIMES_H + +#define __NEED_clock_t +#include + +struct tms +{ + clock_t tms_utime; + clock_t tms_stime; + clock_t tms_cutime; + clock_t tms_cstime; +}; + +clock_t times (struct tms *); + +#endif diff --git a/05/musl-final/include/sys/types.h b/05/musl-final/include/sys/types.h new file mode 100644 index 0000000..7a126b0 --- /dev/null +++ b/05/musl-final/include/sys/types.h @@ -0,0 +1,70 @@ +#ifndef _SYS_TYPES_H +#define _SYS_TYPES_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_loff_t +#define __NEED_ino_t +#define __NEED_dev_t +#define __NEED_uid_t +#define __NEED_gid_t +#define __NEED_mode_t +#define __NEED_nlink_t +#define __NEED_off_t +#define __NEED_pid_t +#define __NEED_size_t +#define __NEED_ssize_t +#define __NEED_time_t +#define __NEED_timer_t +#define __NEED_clockid_t + +#define __NEED_int8_t +#define __NEED_int16_t +#define __NEED_int32_t +#define __NEED_int64_t + +#define __NEED_u_int8_t +#define __NEED_u_int16_t +#define __NEED_u_int32_t +#define __NEED_u_int64_t + +#define __NEED_register_t + +#define __NEED_blkcnt_t +#define __NEED_fsblkcnt_t +#define __NEED_fsfilcnt_t + +#define __NEED_id_t +#define __NEED_key_t +#define __NEED_clock_t +#define __NEED_useconds_t +#define __NEED_suseconds_t +#define __NEED_blksize_t + +#define __NEED_pthread_t +#define __NEED_pthread_attr_t +#define __NEED_pthread_mutexattr_t +#define __NEED_pthread_condattr_t +#define __NEED_pthread_rwlockattr_t +#define __NEED_pthread_barrierattr_t +#define __NEED_pthread_mutex_t +#define __NEED_pthread_cond_t +#define __NEED_pthread_rwlock_t +#define __NEED_pthread_barrier_t +#define __NEED_pthread_spinlock_t +#define __NEED_pthread_key_t +#define __NEED_pthread_once_t + +#include + +#ifdef _GNU_SOURCE +typedef unsigned long caddr_t; +#endif + +#ifdef __cplusplus +} +#endif +#endif + + diff --git a/05/musl-final/include/sys/ucontext.h b/05/musl-final/include/sys/ucontext.h new file mode 100644 index 0000000..5fdbd63 --- /dev/null +++ b/05/musl-final/include/sys/ucontext.h @@ -0,0 +1 @@ +#include diff --git a/05/musl-final/include/sys/uio.h b/05/musl-final/include/sys/uio.h new file mode 100644 index 0000000..11b6bd2 --- /dev/null +++ b/05/musl-final/include/sys/uio.h @@ -0,0 +1,13 @@ +#ifndef _SYS_UIO_H +#define _SYS_UIO_H + +#define __NEED_size_t +#define __NEED_ssize_t +#define __NEED_struct_iovec + +#include + +ssize_t readv (int, const struct iovec *, int); +ssize_t writev (int, const struct iovec *, int); + +#endif diff --git a/05/musl-final/include/sys/un.h b/05/musl-final/include/sys/un.h new file mode 100644 index 0000000..769dac6 --- /dev/null +++ b/05/musl-final/include/sys/un.h @@ -0,0 +1,13 @@ +#ifndef _SYS_UN_H +#define _SYS_UN_H + +#define __NEED_sa_family_t +#include + +struct sockaddr_un +{ + sa_family_t sun_family; + char sun_path[108]; +}; + +#endif diff --git a/05/musl-final/include/sys/user.h b/05/musl-final/include/sys/user.h new file mode 100644 index 0000000..24ac940 --- /dev/null +++ b/05/musl-final/include/sys/user.h @@ -0,0 +1,15 @@ +#ifndef _SYS_USER_H +#define _SYS_USER_H +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#include + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/sys/utsname.h b/05/musl-final/include/sys/utsname.h new file mode 100644 index 0000000..383e825 --- /dev/null +++ b/05/musl-final/include/sys/utsname.h @@ -0,0 +1,21 @@ +#ifndef _SYS_UTSNAME_H +#define _SYS_UTSNAME_H + +struct utsname +{ + char sysname[65]; + char nodename[65]; + char release[65]; + char version[65]; + char machine[65]; +#ifdef _GNU_SOURCE + char domainname[65]; +#else + char __domainname[65]; +#endif +}; + +int uname (struct utsname *); + + +#endif diff --git a/05/musl-final/include/sys/vfs.h b/05/musl-final/include/sys/vfs.h new file mode 100644 index 0000000..a899db2 --- /dev/null +++ b/05/musl-final/include/sys/vfs.h @@ -0,0 +1 @@ +#include diff --git a/05/musl-final/include/sys/vt.h b/05/musl-final/include/sys/vt.h new file mode 100644 index 0000000..834abfb --- /dev/null +++ b/05/musl-final/include/sys/vt.h @@ -0,0 +1 @@ +#include diff --git a/05/musl-final/include/sys/wait.h b/05/musl-final/include/sys/wait.h new file mode 100644 index 0000000..a185153 --- /dev/null +++ b/05/musl-final/include/sys/wait.h @@ -0,0 +1,28 @@ +#ifndef _SYS_WAIT_H +#define _SYS_WAIT_H +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_pid_t +#define __NEED_id_t +#define __NEED_time_t +#define __NEED_struct_timeval +#define __NEED_siginfo_t +#include + +typedef int idtype_t; + +pid_t wait (int *); +int waitid (idtype_t, id_t, siginfo_t *, int); +pid_t waitpid (pid_t, int *, int ); +//pid_t wait3 (int *, int, struct rusage *); +//pid_t wait4 (pid_t, int *, int, struct rusage *); + +#include +#include + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/syslog.h b/05/musl-final/include/syslog.h new file mode 100644 index 0000000..119c9bc --- /dev/null +++ b/05/musl-final/include/syslog.h @@ -0,0 +1,54 @@ +#ifndef _SYS_SYSLOG_H +#define _SYS_SYSLOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define LOG_EMERG 0 +#define LOG_ALERT 1 +#define LOG_CRIT 2 +#define LOG_ERR 3 +#define LOG_WARNING 4 +#define LOG_NOTICE 5 +#define LOG_INFO 6 +#define LOG_DEBUG 7 + +#define LOG_MASK(p) (1<<(p)) + +#define LOG_KERN (0<<3) +#define LOG_USER (1<<3) +#define LOG_MAIL (2<<3) +#define LOG_DAEMON (3<<3) +#define LOG_AUTH (4<<3) +#define LOG_SYSLOG (5<<3) +#define LOG_LPR (6<<3) +#define LOG_NEWS (7<<3) +#define LOG_UUCP (8<<3) +#define LOG_CRON (9<<3) + +#define LOG_LOCAL0 (16<<3) +#define LOG_LOCAL1 (17<<3) +#define LOG_LOCAL2 (18<<3) +#define LOG_LOCAL3 (19<<3) +#define LOG_LOCAL4 (20<<3) +#define LOG_LOCAL5 (21<<3) +#define LOG_LOCAL6 (22<<3) +#define LOG_LOCAL7 (23<<3) + +#define LOG_PID 0x01 +#define LOG_CONS 0x02 +#define LOG_ODELAY 0x04 +#define LOG_NDELAY 0x08 +#define LOG_NOWAIT 0x10 + +void closelog (void); +void openlog (const char *, int, int); +int setlogmask (int); +void syslog (int, const char *, ...); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/tar.h b/05/musl-final/include/tar.h new file mode 100644 index 0000000..be58984 --- /dev/null +++ b/05/musl-final/include/tar.h @@ -0,0 +1,33 @@ +#ifndef _TAR_H +#define _TAR_H + +#define TSUID 04000 +#define TSGID 02000 +#define TSVTX 01000 +#define TUREAD 00400 +#define TUWRITE 00200 +#define TUEXEC 00100 +#define TGREAD 00040 +#define TGWRITE 00020 +#define TGEXEC 00010 +#define TOREAD 00004 +#define TOWRITE 00002 +#define TOEXEC 00001 + +#define REGTYPE '0' +#define AREGTYPE '\0' +#define LNKTYPE '1' +#define SYMTYPE '2' +#define CHRTYPE '3' +#define BLKTYPE '4' +#define DIRTYPE '5' +#define FIFOTYPE '6' +#define CONTTYPE '7' + +#define TMAGIC "ustar" +#define TMAGLEN 6 + +#define TVERSION "00" +#define TVERSLEN 2 + +#endif diff --git a/05/musl-final/include/termios.h b/05/musl-final/include/termios.h new file mode 100644 index 0000000..4c46562 --- /dev/null +++ b/05/musl-final/include/termios.h @@ -0,0 +1,39 @@ +#ifndef _TERMIOS_H +#define _TERMIOS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_pid_t + +#include + +typedef unsigned char cc_t; +typedef unsigned int speed_t; +typedef unsigned int tcflag_t; + +#define NCCS 32 + +#include + +speed_t cfgetospeed (const struct termios *); +speed_t cfgetispeed (const struct termios *); +int cfsetospeed (struct termios *, speed_t); +int cfsetispeed (struct termios *, speed_t); + +int tcgetattr (int, struct termios *); +int tcsetattr (int, int, const struct termios *); + +int tcsendbreak (int, int); +int tcdrain (int); +int tcflush (int, int); +int tcflow (int, int); + +pid_t tcgetsid (int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/time.h b/05/musl-final/include/time.h new file mode 100644 index 0000000..4cec647 --- /dev/null +++ b/05/musl-final/include/time.h @@ -0,0 +1,114 @@ +#ifndef _TIME_H +#define _TIME_H + +#ifdef __cplusplus +extern "C" { +#endif + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + + +#define __NEED_size_t +#define __NEED_time_t +#define __NEED_clock_t + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define __NEED_struct_timespec +#define __NEED_clockid_t +#define __NEED_timer_t +#define __NEED_pid_t +#endif + +#include + +struct tm +{ + int tm_sec; + int tm_min; + int tm_hour; + int tm_mday; + int tm_mon; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; + long __tm_gmtoff; + const char *__tm_zone; +}; + +clock_t clock (void); +time_t time (time_t *); +double difftime (time_t, time_t); +time_t mktime (struct tm *); +size_t strftime (char *, size_t, const char *, const struct tm *); +struct tm *gmtime (const time_t *); +struct tm *localtime (const time_t *); +char *asctime (const struct tm *); +char *ctime (const time_t *); + +#define CLOCKS_PER_SEC 1000000UL + + +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) + +struct tm *gmtime_r (const time_t *, struct tm *); +struct tm *localtime_r (const time_t *, struct tm *); +char *asctime_r (const struct tm *, char *); +char *ctime_r (const time_t *, char *); + +void tzset (void); + +struct itimerspec +{ + struct timespec it_interval; + struct timespec it_value; +}; + +#define CLOCK_REALTIME 0 +#define CLOCK_MONOTONIC 1 +#define CLOCK_PROCESS_CPUTIME_ID 2 +#define CLOCK_THREAD_CPUTIME_ID 3 + +#define TIMER_ABSTIME 1 + +int nanosleep (const struct timespec *, struct timespec *); +int clock_getres (clockid_t, struct timespec *); +int clock_gettime (clockid_t, struct timespec *); +int clock_settime (clockid_t, const struct timespec *); +int clock_nanosleep (clockid_t, int, const struct timespec *, struct timespec *); +int clock_getcpuclockid (pid_t, clockid_t *); + +/* FIXME..?? */ +struct sigevent; +int timer_create (clockid_t, struct sigevent *, timer_t *); +int timer_delete (timer_t); +int timer_settime (timer_t, int, const struct itimerspec *, struct itimerspec *); +int timer_gettime (timer_t, struct itimerspec *); +int timer_getoverrun (timer_t); + +#endif + + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +char *strptime (const char *, const char *, struct tm *); +extern int daylight; +extern long timezone; +extern char *tzname[2]; +extern int getdate_err; +extern struct tm *getdate (const char *); +#endif + + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/05/musl-final/include/ucontext.h b/05/musl-final/include/ucontext.h new file mode 100644 index 0000000..96c27cb --- /dev/null +++ b/05/musl-final/include/ucontext.h @@ -0,0 +1,38 @@ +#ifndef _UCONTEXT_H +#define _UCONTEXT_H +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct __fpstate { + unsigned long __x[7]; + unsigned char __y[80]; + unsigned long __z; +}; + +typedef struct { + unsigned long __gregs[19]; + void *__fpregs; + unsigned long __oldmask, __cr2; +} mcontext_t; + +typedef struct ucontext { + unsigned long uc_flags; + struct ucontext *uc_link; + stack_t uc_stack; + mcontext_t uc_mcontext; + sigset_t uc_sigmask; + struct __fpstate __fpregs_mem; +} ucontext_t; + +int getcontext(ucontext_t *); +void makecontext(ucontext_t *, void (*)(void), int, ...); +int setcontext(const ucontext_t *); +int swapcontext(ucontext_t *, const ucontext_t *); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/musl-final/include/ulimit.h b/05/musl-final/include/ulimit.h new file mode 100644 index 0000000..efdcd31 --- /dev/null +++ b/05/musl-final/include/ulimit.h @@ -0,0 +1,17 @@ +#ifndef _ULIMIT_H +#define _ULIMIT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define UL_GETFSIZE 1 +#define UL_SETFSIZE 2 + +long ulimit (int, ...); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/unistd.h b/05/musl-final/include/unistd.h new file mode 100644 index 0000000..b9e4814 --- /dev/null +++ b/05/musl-final/include/unistd.h @@ -0,0 +1,468 @@ +#ifndef _UNISTD_H +#define _UNISTD_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +#undef SEEK_SET +#undef SEEK_CUR +#undef SEEK_END +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#define __NEED_size_t +#define __NEED_ssize_t +#define __NEED_uid_t +#define __NEED_gid_t +#define __NEED_off_t +#define __NEED_pid_t +#define __NEED_useconds_t +#define __NEED_intptr_t + +#include + +int pipe(int [2]); +int close(int); +int dup(int); +int dup2(int, int); +off_t lseek(int, off_t, int); +int fsync(int); +int fdatasync(int); + +ssize_t read(int, void *, size_t); +ssize_t write(int, const void *, size_t); +ssize_t pread(int, void *, size_t, off_t); +ssize_t pwrite(int, const void *, size_t, off_t); + +int chown(const char *, uid_t, gid_t); +int fchown(int, uid_t, gid_t); +int lchown(const char *, uid_t, gid_t); +int fchownat(int, const char *, uid_t, gid_t, int); + +int link(const char *, const char *); +int linkat(int, const char *, int, const char *, int); +int symlink(const char *, const char *); +int symlinkat(const char *, int, const char *); +int readlink(const char *, char *, size_t); +int readlinkat(int, const char *, char *, size_t); +int unlink(const char *); +int unlinkat(int, const char *, int); +int rmdir(const char *); +int truncate(const char *, off_t); +int ftruncate(int, off_t); + +#define F_OK 0 +#define R_OK 4 +#define W_OK 2 +#define X_OK 1 +int access(const char *, int); +int faccessat(int, const char *, int, int); + +int chdir(const char *); +int fchdir(int); +char *getcwd(char *, size_t); + +unsigned alarm(unsigned); +unsigned sleep(unsigned); +int pause(void); + +pid_t fork(void); +int execve(const char *, char *const [], char *const []); +int execv(const char *, char *const []); +int execle(const char *, ...); +int execl(const char *, ...); +int execvp(const char *, char *const []); +int execlp(const char *, ...); +void _exit(int); + +pid_t getpid(void); +pid_t getppid(void); +pid_t getpgrp(void); +pid_t getpgid(pid_t); +int setpgid(pid_t, pid_t); +pid_t setsid(void); +pid_t getsid(pid_t); +char *ttyname(int); +int ttyname_r(int, char *, size_t); +int isatty(int); +pid_t tcgetpgrp(int); +int tcsetpgrp(int, pid_t); + +uid_t getuid(void); +uid_t geteuid(void); +gid_t getgid(void); +gid_t getegid(void); +int getgroups(int, gid_t []); +int setuid(uid_t); +int setreuid(uid_t, uid_t); +int seteuid(uid_t); +int setgid(gid_t); +int setregid(gid_t, gid_t); +int setegid(gid_t); + +char *getlogin(void); +int getlogin_r(char *, size_t); +int gethostname(char *, size_t); +char *ctermid(char *); + +int getopt(int, char * const [], const char *); +extern char *optarg; +extern int optind, opterr, optopt; + +long pathconf(const char *, int); +long fpathconf(int, int); +long sysconf(int); +size_t confstr(int, char *, size_t); + +#define F_ULOCK 0 +#define F_LOCK 1 +#define F_TLOCK 2 +#define F_TEST 3 + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +int lockf(int, int, off_t); +int setpgrp(void); +char *crypt(const char *, const char *); +void encrypt(char *, int); +void swab(const void *, void *, ssize_t); +long gethostid(void); +int nice(int); +void sync(void); +#endif + +#ifdef _GNU_SOURCE +pid_t vfork(void); +int vhangup(void); +int chroot(const char *); +int getpagesize(void); +int sethostname(const char *, size_t); +int usleep(useconds_t); +useconds_t ualarm(useconds_t, useconds_t); +int setgroups(int, const gid_t []); +#endif + +#define _XOPEN_VERSION 700 +#define _XOPEN_UNIX 1 +#define _XOPEN_ENH_I18N 1 + +#define _POSIX_VERSION 200809L +#define _POSIX2_VERSION _POSIX_VERSION + +#define _POSIX_CHOWN_RESTRICTED 1 +#define _POSIX_IPV6 _POSIX_VERSION +#define _POSIX_JOB_CONTROL 1 +#define _POSIX_MAPPED_FILES _POSIX_VERSION +#define _POSIX_MEMLOCK _POSIX_VERSION +#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION +#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION +#define _POSIX_NO_TRUNC 1 +#define _POSIX_RAW_SOCKETS _POSIX_VERSION +#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION +#define _POSIX_REGEXP 1 +#define _POSIX_SAVED_IDS 1 +#define _POSIX_SHELL 1 +#define _POSIX_VDISABLE 0 + +#define _POSIX_THREADS _POSIX_VERSION +#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION +#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION +#define _POSIX_TIMERS _POSIX_VERSION +#define _POSIX_TIMEOUTS _POSIX_VERSION +#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION +#define _POSIX_CPUTIME _POSIX_VERSION +#define _POSIX_CLOCK_SELECTION _POSIX_VERSION +#define _POSIX_BARRIERS _POSIX_VERSION +#define _POSIX_SPIN_LOCKS _POSIX_VERSION +#define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION +#define _POSIX_ASYNCHRONOUS_IO _POSIX_VERSION +#define _POSIX_SEMAPHORES _POSIX_VERSION + +#define _POSIX2_C_BIND _POSIX_VERSION + +#include + + + +#define _PC_LINK_MAX 0 +#define _PC_MAX_CANON 1 +#define _PC_MAX_INPUT 2 +#define _PC_NAME_MAX 3 +#define _PC_PATH_MAX 4 +#define _PC_PIPE_BUF 5 +#define _PC_CHOWN_RESTRICTED 6 +#define _PC_NO_TRUNC 7 +#define _PC_VDISABLE 8 +#define _PC_SYNC_IO 9 +#define _PC_ASYNC_IO 10 +#define _PC_PRIO_IO 11 +#define _PC_SOCK_MAXBUF 12 +#define _PC_FILESIZEBITS 13 +#define _PC_REC_INCR_XFER_SIZE 14 +#define _PC_REC_MAX_XFER_SIZE 15 +#define _PC_REC_MIN_XFER_SIZE 16 +#define _PC_REC_XFER_ALIGN 17 +#define _PC_ALLOC_SIZE_MIN 18 +#define _PC_SYMLINK_MAX 19 +#define _PC_2_SYMLINKS 20 + +#define _SC_ARG_MAX 0 +#define _SC_CHILD_MAX 1 +#define _SC_CLK_TCK 2 +#define _SC_NGROUPS_MAX 3 +#define _SC_OPEN_MAX 4 +#define _SC_STREAM_MAX 5 +#define _SC_TZNAME_MAX 6 +#define _SC_JOB_CONTROL 7 +#define _SC_SAVED_IDS 8 +#define _SC_REALTIME_SIGNALS 9 +#define _SC_PRIORITY_SCHEDULING 10 +#define _SC_TIMERS 11 +#define _SC_ASYNCHRONOUS_IO 12 +#define _SC_PRIORITIZED_IO 13 +#define _SC_SYNCHRONIZED_IO 14 +#define _SC_FSYNC 15 +#define _SC_MAPPED_FILES 16 +#define _SC_MEMLOCK 17 +#define _SC_MEMLOCK_RANGE 18 +#define _SC_MEMORY_PROTECTION 19 +#define _SC_MESSAGE_PASSING 20 +#define _SC_SEMAPHORES 21 +#define _SC_SHARED_MEMORY_OBJECTS 22 +#define _SC_AIO_LISTIO_MAX 23 +#define _SC_AIO_MAX 24 +#define _SC_AIO_PRIO_DELTA_MAX 25 +#define _SC_DELAYTIMER_MAX 26 +#define _SC_MQ_OPEN_MAX 27 +#define _SC_MQ_PRIO_MAX 28 +#define _SC_VERSION 29 +#define _SC_PAGE_SIZE 30 +#define _SC_PAGESIZE 30 /* !! */ +#define _SC_RTSIG_MAX 31 +#define _SC_SEM_NSEMS_MAX 32 +#define _SC_SEM_VALUE_MAX 33 +#define _SC_SIGQUEUE_MAX 34 +#define _SC_TIMER_MAX 35 +#define _SC_BC_BASE_MAX 36 +#define _SC_BC_DIM_MAX 37 +#define _SC_BC_SCALE_MAX 38 +#define _SC_BC_STRING_MAX 39 +#define _SC_COLL_WEIGHTS_MAX 40 +#define _SC_EQUIV_CLASS_MAX 41 +#define _SC_EXPR_NEST_MAX 42 +#define _SC_LINE_MAX 43 +#define _SC_RE_DUP_MAX 44 +#define _SC_CHARCLASS_NAME_MAX 45 +#define _SC_2_VERSION 46 +#define _SC_2_C_BIND 47 +#define _SC_2_C_DEV 48 +#define _SC_2_FORT_DEV 49 +#define _SC_2_FORT_RUN 50 +#define _SC_2_SW_DEV 51 +#define _SC_2_LOCALEDEF 52 +#define _SC_PII 53 +#define _SC_PII_XTI 54 +#define _SC_PII_SOCKET 55 +#define _SC_PII_INTERNET 56 +#define _SC_PII_OSI 57 +#define _SC_POLL 58 +#define _SC_SELECT 59 +#define _SC_UIO_MAXIOV 60 /* !! */ +#define _SC_IOV_MAX 60 +#define _SC_PII_INTERNET_STREAM 61 +#define _SC_PII_INTERNET_DGRAM 62 +#define _SC_PII_OSI_COTS 63 +#define _SC_PII_OSI_CLTS 64 +#define _SC_PII_OSI_M 65 +#define _SC_T_IOV_MAX 66 +#define _SC_THREADS 67 +#define _SC_THREAD_SAFE_FUNCTIONS 68 +#define _SC_GETGR_R_SIZE_MAX 69 +#define _SC_GETPW_R_SIZE_MAX 70 +#define _SC_LOGIN_NAME_MAX 71 +#define _SC_TTY_NAME_MAX 72 +#define _SC_THREAD_DESTRUCTOR_ITERATIONS 73 +#define _SC_THREAD_KEYS_MAX 74 +#define _SC_THREAD_STACK_MIN 75 +#define _SC_THREAD_THREADS_MAX 76 +#define _SC_THREAD_ATTR_STACKADDR 77 +#define _SC_THREAD_ATTR_STACKSIZE 78 +#define _SC_THREAD_PRIORITY_SCHEDULING 79 +#define _SC_THREAD_PRIO_INHERIT 80 +#define _SC_THREAD_PRIO_PROTECT 81 +#define _SC_THREAD_PROCESS_SHARED 82 +#define _SC_NPROCESSORS_CONF 83 +#define _SC_NPROCESSORS_ONLN 84 +#define _SC_PHYS_PAGES 85 +#define _SC_AVPHYS_PAGES 86 +#define _SC_ATEXIT_MAX 87 +#define _SC_PASS_MAX 88 +#define _SC_XOPEN_VERSION 89 +#define _SC_XOPEN_XCU_VERSION 90 +#define _SC_XOPEN_UNIX 91 +#define _SC_XOPEN_CRYPT 92 +#define _SC_XOPEN_ENH_I18N 93 +#define _SC_XOPEN_SHM 94 +#define _SC_2_CHAR_TERM 95 +#define _SC_2_C_VERSION 96 +#define _SC_2_UPE 97 +#define _SC_XOPEN_XPG2 98 +#define _SC_XOPEN_XPG3 99 +#define _SC_XOPEN_XPG4 100 +#define _SC_CHAR_BIT 101 +#define _SC_CHAR_MAX 102 +#define _SC_CHAR_MIN 103 +#define _SC_INT_MAX 104 +#define _SC_INT_MIN 105 +#define _SC_LONG_BIT 106 +#define _SC_WORD_BIT 107 +#define _SC_MB_LEN_MAX 108 +#define _SC_NZERO 109 +#define _SC_SSIZE_MAX 110 +#define _SC_SCHAR_MAX 111 +#define _SC_SCHAR_MIN 112 +#define _SC_SHRT_MAX 113 +#define _SC_SHRT_MIN 114 +#define _SC_UCHAR_MAX 115 +#define _SC_UINT_MAX 116 +#define _SC_ULONG_MAX 117 +#define _SC_USHRT_MAX 118 +#define _SC_NL_ARGMAX 119 +#define _SC_NL_LANGMAX 120 +#define _SC_NL_MSGMAX 121 +#define _SC_NL_NMAX 122 +#define _SC_NL_SETMAX 123 +#define _SC_NL_TEXTMAX 124 +#define _SC_XBS5_ILP32_OFF32 125 +#define _SC_XBS5_ILP32_OFFBIG 126 +#define _SC_XBS5_LP64_OFF64 127 +#define _SC_XBS5_LPBIG_OFFBIG 128 +#define _SC_XOPEN_LEGACY 129 +#define _SC_XOPEN_REALTIME 130 +#define _SC_XOPEN_REALTIME_THREADS 131 +#define _SC_ADVISORY_INFO 132 +#define _SC_BARRIERS 133 +#define _SC_BASE 134 +#define _SC_C_LANG_SUPPORT 135 +#define _SC_C_LANG_SUPPORT_R 136 +#define _SC_CLOCK_SELECTION 137 +#define _SC_CPUTIME 138 +#define _SC_THREAD_CPUTIME 139 +#define _SC_DEVICE_IO 140 +#define _SC_DEVICE_SPECIFIC 141 +#define _SC_DEVICE_SPECIFIC_R 142 +#define _SC_FD_MGMT 143 +#define _SC_FIFO 144 +#define _SC_PIPE 145 +#define _SC_FILE_ATTRIBUTES 146 +#define _SC_FILE_LOCKING 147 +#define _SC_FILE_SYSTEM 148 +#define _SC_MONOTONIC_CLOCK 149 +#define _SC_MULTI_PROCESS 150 +#define _SC_SINGLE_PROCESS 151 +#define _SC_NETWORKING 152 +#define _SC_READER_WRITER_LOCKS 153 +#define _SC_SPIN_LOCKS 154 +#define _SC_REGEXP 155 +#define _SC_REGEX_VERSION 156 +#define _SC_SHELL 157 +#define _SC_SIGNALS 158 +#define _SC_SPAWN 159 +#define _SC_SPORADIC_SERVER 160 +#define _SC_THREAD_SPORADIC_SERVER 161 +#define _SC_SYSTEM_DATABASE 162 +#define _SC_SYSTEM_DATABASE_R 163 +#define _SC_TIMEOUTS 164 +#define _SC_TYPED_MEMORY_OBJECTS 165 +#define _SC_USER_GROUPS 166 +#define _SC_USER_GROUPS_R 167 +#define _SC_2_PBS 168 +#define _SC_2_PBS_ACCOUNTING 169 +#define _SC_2_PBS_LOCATE 170 +#define _SC_2_PBS_MESSAGE 171 +#define _SC_2_PBS_TRACK 172 +#define _SC_SYMLOOP_MAX 173 +#define _SC_STREAMS 174 +#define _SC_2_PBS_CHECKPOINT 175 +#define _SC_V6_ILP32_OFF32 176 +#define _SC_V6_ILP32_OFFBIG 177 +#define _SC_V6_LP64_OFF64 178 +#define _SC_V6_LPBIG_OFFBIG 179 +#define _SC_HOST_NAME_MAX 180 +#define _SC_TRACE 181 +#define _SC_TRACE_EVENT_FILTER 182 +#define _SC_TRACE_INHERIT 183 +#define _SC_TRACE_LOG 184 + +#define _SC_IPV6 235 +#define _SC_RAW_SOCKETS 236 +#define _SC_V7_ILP32_OFF32 237 +#define _SC_V7_ILP32_OFFBIG 238 +#define _SC_V7_LP64_OFF64 239 +#define _SC_V7_LPBIG_OFFBIG 240 +#define _SC_SS_REPL_MAX 241 +#define _SC_TRACE_EVENT_NAME_MAX 242 +#define _SC_TRACE_NAME_MAX 243 +#define _SC_TRACE_SYS_MAX 244 +#define _SC_TRACE_USER_EVENT_MAX 245 +#define _SC_XOPEN_STREAMS 246 +#define _SC_THREAD_ROBUST_PRIO_INHERIT 247 +#define _SC_THREAD_ROBUST_PRIO_PROTECT 248 + +#define _CS_PATH 0 +#define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS 1 +#define _CS_GNU_LIBC_VERSION 2 +#define _CS_GNU_LIBPTHREAD_VERSION 3 +#define _CS_POSIX_V5_WIDTH_RESTRICTED_ENVS 4 +#define _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS 5 + +#define _CS_POSIX_V6_ILP32_OFF32_CFLAGS 1116 +#define _CS_POSIX_V6_ILP32_OFF32_LDFLAGS 1117 +#define _CS_POSIX_V6_ILP32_OFF32_LIBS 1118 +#define _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS 1119 +#define _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS 1120 +#define _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS 1121 +#define _CS_POSIX_V6_ILP32_OFFBIG_LIBS 1122 +#define _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS 1123 +#define _CS_POSIX_V6_LP64_OFF64_CFLAGS 1124 +#define _CS_POSIX_V6_LP64_OFF64_LDFLAGS 1125 +#define _CS_POSIX_V6_LP64_OFF64_LIBS 1126 +#define _CS_POSIX_V6_LP64_OFF64_LINTFLAGS 1127 +#define _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS 1128 +#define _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS 1129 +#define _CS_POSIX_V6_LPBIG_OFFBIG_LIBS 1130 +#define _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS 1131 +#define _CS_POSIX_V7_ILP32_OFF32_CFLAGS 1132 +#define _CS_POSIX_V7_ILP32_OFF32_LDFLAGS 1133 +#define _CS_POSIX_V7_ILP32_OFF32_LIBS 1134 +#define _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS 1135 +#define _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS 1136 +#define _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS 1137 +#define _CS_POSIX_V7_ILP32_OFFBIG_LIBS 1138 +#define _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS 1139 +#define _CS_POSIX_V7_LP64_OFF64_CFLAGS 1140 +#define _CS_POSIX_V7_LP64_OFF64_LDFLAGS 1141 +#define _CS_POSIX_V7_LP64_OFF64_LIBS 1142 +#define _CS_POSIX_V7_LP64_OFF64_LINTFLAGS 1143 +#define _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS 1144 +#define _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS 1145 +#define _CS_POSIX_V7_LPBIG_OFFBIG_LIBS 1146 +#define _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS 1147 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/utime.h b/05/musl-final/include/utime.h new file mode 100644 index 0000000..ec82e0f --- /dev/null +++ b/05/musl-final/include/utime.h @@ -0,0 +1,24 @@ +#ifndef _UTIME_H +#define _UTIME_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_time_t + +#include + +struct utimbuf +{ + time_t actime; + time_t modtime; +}; + +int utime (const char *, const struct utimbuf *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/utmp.h b/05/musl-final/include/utmp.h new file mode 100644 index 0000000..f60ff88 --- /dev/null +++ b/05/musl-final/include/utmp.h @@ -0,0 +1,23 @@ +#ifndef _UTMP_H +#define _UTMP_H + +#include + +#define ut_time ut_tv.tv_sec +#define ut_name ut_user +#define ut_exit __ut_exit +#define e_termination __e_termination +#define e_exit __e_exit +#define utmp utmpx +#define endutent endutxent +#define getutent getutxent +#define setutent setutxent +#define getutid getutxid +#define getutline getutxline +#define pututline pututxline +#define utmpname(x) (-1) + +#define _PATH_UTMP "/dev/null" +#define _PATH_WTMP "/dev/null" + +#endif diff --git a/05/musl-final/include/utmpx.h b/05/musl-final/include/utmpx.h new file mode 100644 index 0000000..9a2e307 --- /dev/null +++ b/05/musl-final/include/utmpx.h @@ -0,0 +1,52 @@ +#ifndef _UTMPX_H +#define _UTMPX_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_pid_t +#define __NEED_time_t +#define __NEED_struct_timeval + +#include + +struct utmpx +{ + short ut_type; + pid_t ut_pid; + char ut_line[32]; + char ut_id[4]; + char ut_user[32]; + char ut_host[256]; + struct { + short __e_termination; + short __e_exit; + } __ut_exit; + long ut_session; + struct timeval ut_tv; + unsigned ut_addr_v6[4]; + char __unused[20]; +}; + +void endutxent(void); +struct utmpx *getutxent(void); +struct utmpx *getutxid(const struct utmpx *); +struct utmpx *getutxline(const struct utmpx *); +struct utmpx *pututxline(const struct utmpx *); +void setutxent(void); + +#define EMPTY 0 +#define BOOT_TIME 2 +#define NEW_TIME 3 +#define OLD_TIME 4 +#define INIT_PROCESS 5 +#define LOGIN_PROCESS 6 +#define USER_PROCESS 7 +#define DEAD_PROCESS 8 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/wchar.h b/05/musl-final/include/wchar.h new file mode 100644 index 0000000..53d619b --- /dev/null +++ b/05/musl-final/include/wchar.h @@ -0,0 +1,160 @@ +#ifndef _WCHAR_H +#define _WCHAR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_FILE +#define __NEED_va_list +#define __NEED_size_t +#define __NEED_wchar_t +#define __NEED_wint_t + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +#define __NEED_wctype_t +#endif + +#include + +#undef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void*)0) +#endif + +#undef WCHAR_MIN +#undef WCHAR_MAX +#define WCHAR_MIN (-1-0x7fffffff) +#define WCHAR_MAX (0x7fffffff) + +#undef WEOF +#define WEOF (-1) + + +typedef struct +{ + unsigned __opaque1, __opaque2; +} mbstate_t; + +wchar_t *wcscpy (wchar_t *, const wchar_t *); +wchar_t *wcsncpy (wchar_t *, const wchar_t *, size_t); + +wchar_t *wcscat (wchar_t *, const wchar_t *); +wchar_t *wcsncat (wchar_t *, const wchar_t *, size_t); + +int wcscmp (const wchar_t *, const wchar_t *); +int wcsncmp (const wchar_t *, const wchar_t *, size_t); + +size_t wcsxfrm (wchar_t *, const wchar_t *, size_t n); + +wchar_t *wcschr (const wchar_t *, wchar_t); +wchar_t *wcsrchr (const wchar_t *, wchar_t); + +size_t wcscspn (const wchar_t *, const wchar_t *); +size_t wcsspn (const wchar_t *, const wchar_t *); +wchar_t *wcspbrk (const wchar_t *, const wchar_t *); + +wchar_t *wcstok (wchar_t *, const wchar_t *, wchar_t **); + +size_t wcslen (const wchar_t *); + +wchar_t *wcsstr (const wchar_t *, const wchar_t *); +wchar_t *wcswcs (const wchar_t *, const wchar_t *); + +wchar_t *wmemchr (const wchar_t *, wchar_t, size_t); +int wmemcmp (const wchar_t *, const wchar_t *, size_t); +wchar_t *wmemcpy (wchar_t *, const wchar_t *, size_t); +wchar_t *wmemmove (wchar_t *, const wchar_t *, size_t); +wchar_t *wmemset (wchar_t *, wchar_t, size_t); + +wint_t btowc (int); +int wctob (wint_t); + +int mbsinit (const mbstate_t *); +size_t mbrtowc (wchar_t *, const char *, size_t, mbstate_t *); +size_t wcrtomb (char *, wchar_t, mbstate_t *); + +size_t mbrlen (const char *, size_t, mbstate_t *); + +size_t mbsrtowcs (wchar_t *, const char **, size_t, mbstate_t *); +size_t wcsrtombs (char *, const wchar_t **, size_t, mbstate_t *); + +float wcstof (const wchar_t *, wchar_t **); +double wcstod (const wchar_t *, wchar_t **); +long double wcstold (const wchar_t *, wchar_t **); + +long wcstol (const wchar_t *, wchar_t **, int); +unsigned long wcstoul (const wchar_t *, wchar_t **, int); + +long long wcstoll (const wchar_t *, wchar_t **, int); +unsigned long long wcstoull (const wchar_t *, wchar_t **, int); + + + +int fwide (FILE *, int); + + +int wprintf (const wchar_t *, ...); +int fwprintf (FILE *, const wchar_t *, ...); +int swprintf (wchar_t *, const wchar_t *, ...); + +int vwprintf (const wchar_t *, va_list); +int vfwprintf (FILE *, const wchar_t *, va_list); +int vswprintf (wchar_t *, const wchar_t *, va_list); + +int wscanf (const wchar_t *, ...); +int fwscanf (FILE *, const wchar_t *, ...); +int swscanf (const wchar_t *, const wchar_t *, ...); + +int vwscanf (const wchar_t *, va_list); +int vfwscanf (FILE *, const wchar_t *, va_list); +int vswscanf (const wchar_t *, const wchar_t *, va_list); + +wint_t fgetwc (FILE *); +wint_t getwc (FILE *); +wint_t getwchar (void); + +wint_t fputwc (wchar_t, FILE *); +wint_t putwc (wchar_t, FILE *); +wint_t putwchar (wchar_t); + +wchar_t *fgetws (wchar_t *, int, FILE *); +int fputws (const wchar_t *, FILE *); + +wint_t ungetwc (wint_t, FILE *); + +struct tm; +size_t wcsftime (wchar_t *, size_t, const wchar_t *, const struct tm *); + +#undef iswdigit + +#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) +int wcwidth (wchar_t); +int wcswidth (const wchar_t *, size_t); +int iswalnum(wint_t); +int iswalpha(wint_t); +int iswblank(wint_t); +int iswcntrl(wint_t); +int iswdigit(wint_t); +int iswgraph(wint_t); +int iswlower(wint_t); +int iswprint(wint_t); +int iswpunct(wint_t); +int iswspace(wint_t); +int iswupper(wint_t); +int iswxdigit(wint_t); +int iswctype(wint_t, wctype_t); +wint_t towlower(wint_t); +wint_t towupper(wint_t); +wctype_t wctype(const char *); +#undef iswdigit +#define iswdigit(a) ((unsigned)(a)-'0' < 10) +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/wctype.h b/05/musl-final/include/wctype.h new file mode 100644 index 0000000..af9be06 --- /dev/null +++ b/05/musl-final/include/wctype.h @@ -0,0 +1,45 @@ +#ifndef _WCTYPE_H +#define _WCTYPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_wint_t +#define __NEED_wctrans_t +#define __NEED_wctype_t + +#include + +#undef WEOF +#define WEOF (-1) + +#undef iswdigit + +int iswalnum(wint_t); +int iswalpha(wint_t); +int iswblank(wint_t); +int iswcntrl(wint_t); +int iswdigit(wint_t); +int iswgraph(wint_t); +int iswlower(wint_t); +int iswprint(wint_t); +int iswpunct(wint_t); +int iswspace(wint_t); +int iswupper(wint_t); +int iswxdigit(wint_t); +int iswctype(wint_t, wctype_t); +wint_t towctrans(wint_t, wctrans_t); +wint_t towlower(wint_t); +wint_t towupper(wint_t); +wctrans_t wctrans(const char *); +wctype_t wctype(const char *); + +#undef iswdigit +#define iswdigit(a) ((unsigned)((a)-L'0') < 10) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/include/wordexp.h b/05/musl-final/include/wordexp.h new file mode 100644 index 0000000..91995c4 --- /dev/null +++ b/05/musl-final/include/wordexp.h @@ -0,0 +1,40 @@ +#ifndef _WORDEXP_H +#define _WORDEXP_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t + +#include + +#define WRDE_DOOFFS 1 +#define WRDE_APPEND 2 +#define WRDE_NOCMD 4 +#define WRDE_REUSE 8 +#define WRDE_SHOWERR 16 +#define WRDE_UNDEF 32 + +typedef struct +{ + size_t we_wordc; + char **wc_wordv; + size_t we_offs; +} wordexp_t; + +#define WRDE_NOSYS -1 +#define WRDE_NOSPACE 1 +#define WRDE_BADCHAR 2 +#define WRDE_BADVAL 3 +#define WRDE_CMDSUB 4 +#define WRDE_SYNTAX 5 + +int wordexp (const char *, wordexp_t *, int); +void wordfree (wordexp_t *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/musl-final/src/conf/confstr.c b/05/musl-final/src/conf/confstr.c new file mode 100644 index 0000000..4332f72 --- /dev/null +++ b/05/musl-final/src/conf/confstr.c @@ -0,0 +1,17 @@ +#include +#include +#include + +size_t confstr(int name, char *buf, size_t len) +{ + const char *s = ""; + if (!name) { + s = "/bin:/usr/bin"; + } else if ((name&~4U)!=1 && name-_CS_POSIX_V6_ILP32_OFF32_CFLAGS>31U) { + errno = EINVAL; + return 0; + } + // snprintf is overkill but avoid wasting code size to implement + // this completely useless function and its truncation semantics + return snprintf(buf, len, "%s", s); +} diff --git a/05/musl-final/src/conf/fpathconf.c b/05/musl-final/src/conf/fpathconf.c new file mode 100644 index 0000000..af7e4d3 --- /dev/null +++ b/05/musl-final/src/conf/fpathconf.c @@ -0,0 +1,35 @@ +#include +#include +#include + +long fpathconf(int fd, int name) +{ + static const short values[] = { + [_PC_LINK_MAX] = _POSIX_LINK_MAX, + [_PC_MAX_CANON] = _POSIX_MAX_CANON, + [_PC_MAX_INPUT] = _POSIX_MAX_INPUT, + [_PC_NAME_MAX] = NAME_MAX, + [_PC_PATH_MAX] = PATH_MAX, + [_PC_PIPE_BUF] = PIPE_BUF, + [_PC_CHOWN_RESTRICTED] = 1, + [_PC_NO_TRUNC] = 1, + [_PC_VDISABLE] = 0, + [_PC_SYNC_IO] = 0, + [_PC_ASYNC_IO] = 0, + [_PC_PRIO_IO] = 0, + [_PC_SOCK_MAXBUF] = -1, + [_PC_FILESIZEBITS] = sizeof(off_t), + [_PC_REC_INCR_XFER_SIZE] = PAGE_SIZE, + [_PC_REC_MAX_XFER_SIZE] = PAGE_SIZE, + [_PC_REC_MIN_XFER_SIZE] = PAGE_SIZE, + [_PC_REC_XFER_ALIGN] = PAGE_SIZE, + [_PC_ALLOC_SIZE_MIN] = PAGE_SIZE, + [_PC_SYMLINK_MAX] = SYMLINK_MAX, + [_PC_2_SYMLINKS] = 1 + }; + if (name > sizeof(values)/sizeof(values[0])) { + errno = EINVAL; + return -1; + } + return values[name]; +} diff --git a/05/musl-final/src/conf/pathconf.c b/05/musl-final/src/conf/pathconf.c new file mode 100644 index 0000000..01e19c5 --- /dev/null +++ b/05/musl-final/src/conf/pathconf.c @@ -0,0 +1,6 @@ +#include + +long pathconf(const char *path, int name) +{ + return fpathconf(-1, name); +} diff --git a/05/musl-final/src/conf/sysconf.c b/05/musl-final/src/conf/sysconf.c new file mode 100644 index 0000000..cdaeb2a --- /dev/null +++ b/05/musl-final/src/conf/sysconf.c @@ -0,0 +1,222 @@ +#include +#include +#include + +#define VER (-2) +#define OFLOW (-3) + +long sysconf(int name) +{ + static const short values[] = { + [_SC_ARG_MAX] = OFLOW, + [_SC_CHILD_MAX] = -1, + [_SC_CLK_TCK] = 100, + [_SC_NGROUPS_MAX] = 32, + [_SC_OPEN_MAX] = 1024, + [_SC_STREAM_MAX] = -1, + [_SC_TZNAME_MAX] = TZNAME_MAX, + [_SC_JOB_CONTROL] = 1, + [_SC_SAVED_IDS] = 1, + [_SC_REALTIME_SIGNALS] = 1, + [_SC_PRIORITY_SCHEDULING] = -1, + [_SC_TIMERS] = VER, + [_SC_ASYNCHRONOUS_IO] = VER, + [_SC_PRIORITIZED_IO] = -1, + [_SC_SYNCHRONIZED_IO] = -1, + [_SC_FSYNC] = -1, + [_SC_MAPPED_FILES] = VER, + [_SC_MEMLOCK] = VER, + [_SC_MEMLOCK_RANGE] = VER, + [_SC_MEMORY_PROTECTION] = VER, + [_SC_MESSAGE_PASSING] = -1, + [_SC_SEMAPHORES] = VER, + [_SC_SHARED_MEMORY_OBJECTS] = -1, + [_SC_AIO_LISTIO_MAX] = -1, + [_SC_AIO_MAX] = -1, + [_SC_AIO_PRIO_DELTA_MAX] = 0, /* ?? */ + [_SC_DELAYTIMER_MAX] = _POSIX_DELAYTIMER_MAX, + [_SC_MQ_OPEN_MAX] = -1, + [_SC_MQ_PRIO_MAX] = _POSIX_MQ_PRIO_MAX, + [_SC_VERSION] = VER, + [_SC_PAGE_SIZE] = PAGE_SIZE, + [_SC_RTSIG_MAX] = 63, /* ?? */ + [_SC_SEM_NSEMS_MAX] = _POSIX_SEM_NSEMS_MAX, + [_SC_SEM_VALUE_MAX] = _POSIX_SEM_VALUE_MAX, + [_SC_SIGQUEUE_MAX] = -1, + [_SC_TIMER_MAX] = -1, + [_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX, + [_SC_BC_DIM_MAX] = _POSIX2_BC_DIM_MAX, + [_SC_BC_SCALE_MAX] = _POSIX2_BC_SCALE_MAX, + [_SC_BC_STRING_MAX] = _POSIX2_BC_STRING_MAX, + [_SC_COLL_WEIGHTS_MAX] = COLL_WEIGHTS_MAX, + [_SC_EQUIV_CLASS_MAX] = -1, /* ?? */ + [_SC_EXPR_NEST_MAX] = -1, + [_SC_LINE_MAX] = -1, + [_SC_RE_DUP_MAX] = RE_DUP_MAX, + [_SC_CHARCLASS_NAME_MAX] = -1, /* ?? */ + [_SC_2_VERSION] = VER, + [_SC_2_C_BIND] = VER, + [_SC_2_C_DEV] = -1, + [_SC_2_FORT_DEV] = -1, + [_SC_2_FORT_RUN] = -1, + [_SC_2_SW_DEV] = -1, + [_SC_2_LOCALEDEF] = -1, + [_SC_PII] = -1, /* ????????? */ + [_SC_PII_XTI] = -1, + [_SC_PII_SOCKET] = -1, + [_SC_PII_INTERNET] = -1, + [_SC_PII_OSI] = -1, + [_SC_POLL] = 1, + [_SC_SELECT] = 1, + [_SC_IOV_MAX] = IOV_MAX, + [_SC_PII_INTERNET_STREAM] = -1, + [_SC_PII_INTERNET_DGRAM] = -1, + [_SC_PII_OSI_COTS] = -1, + [_SC_PII_OSI_CLTS] = -1, + [_SC_PII_OSI_M] = -1, + [_SC_T_IOV_MAX] = -1, + [_SC_THREADS] = VER, + [_SC_THREAD_SAFE_FUNCTIONS] = VER, + [_SC_GETGR_R_SIZE_MAX] = -1, + [_SC_GETPW_R_SIZE_MAX] = -1, + [_SC_LOGIN_NAME_MAX] = 256, + [_SC_TTY_NAME_MAX] = TTY_NAME_MAX, + [_SC_THREAD_DESTRUCTOR_ITERATIONS] = _POSIX_THREAD_DESTRUCTOR_ITERATIONS, + [_SC_THREAD_KEYS_MAX] = -1, + [_SC_THREAD_STACK_MIN] = 2*PAGE_SIZE, + [_SC_THREAD_THREADS_MAX] = -1, + [_SC_THREAD_ATTR_STACKADDR] = -1, + [_SC_THREAD_ATTR_STACKSIZE] = VER, + [_SC_THREAD_PRIORITY_SCHEDULING] = -1, + [_SC_THREAD_PRIO_INHERIT] = -1, + [_SC_THREAD_PRIO_PROTECT] = -1, + [_SC_THREAD_PROCESS_SHARED] = VER, + [_SC_NPROCESSORS_CONF] = -1, + [_SC_NPROCESSORS_ONLN] = -1, + [_SC_PHYS_PAGES] = -1, + [_SC_AVPHYS_PAGES] = -1, + [_SC_ATEXIT_MAX] = -1, + [_SC_PASS_MAX] = -1, + [_SC_XOPEN_VERSION] = _XOPEN_VERSION, + [_SC_XOPEN_XCU_VERSION] = _XOPEN_VERSION, + [_SC_XOPEN_UNIX] = -1, + [_SC_XOPEN_CRYPT] = -1, + [_SC_XOPEN_ENH_I18N] = 1, + [_SC_XOPEN_SHM] = 1, + [_SC_2_CHAR_TERM] = -1, + [_SC_2_C_VERSION] = -1, + [_SC_2_UPE] = -1, + [_SC_XOPEN_XPG2] = -1, + [_SC_XOPEN_XPG3] = -1, + [_SC_XOPEN_XPG4] = -1, + [_SC_CHAR_BIT] = -1, + [_SC_CHAR_MAX] = -1, + [_SC_CHAR_MIN] = -1, + [_SC_INT_MAX] = -1, + [_SC_INT_MIN] = -1, + [_SC_LONG_BIT] = -1, + [_SC_WORD_BIT] = -1, + [_SC_MB_LEN_MAX] = -1, + [_SC_NZERO] = NZERO, + [_SC_SSIZE_MAX] = -1, + [_SC_SCHAR_MAX] = -1, + [_SC_SCHAR_MIN] = -1, + [_SC_SHRT_MAX] = -1, + [_SC_SHRT_MIN] = -1, + [_SC_UCHAR_MAX] = -1, + [_SC_UINT_MAX] = -1, + [_SC_ULONG_MAX] = -1, + [_SC_USHRT_MAX] = -1, + [_SC_NL_ARGMAX] = -1, + [_SC_NL_LANGMAX] = -1, + [_SC_NL_MSGMAX] = -1, + [_SC_NL_NMAX] = -1, + [_SC_NL_SETMAX] = -1, + [_SC_NL_TEXTMAX] = -1, + [_SC_XBS5_ILP32_OFF32] = -1, + [_SC_XBS5_ILP32_OFFBIG] = 2*(sizeof(long)==4)-1, + [_SC_XBS5_LP64_OFF64] = 2*(sizeof(long)==8)-1, + [_SC_XBS5_LPBIG_OFFBIG] = 2*(sizeof(long)>=8)-1, + [_SC_XOPEN_LEGACY] = -1, + [_SC_XOPEN_REALTIME] = -1, + [_SC_XOPEN_REALTIME_THREADS] = -1, + [_SC_ADVISORY_INFO] = -1, + [_SC_BARRIERS] = VER, + [_SC_BASE] = -1, + [_SC_C_LANG_SUPPORT] = -1, + [_SC_C_LANG_SUPPORT_R] = -1, + [_SC_CLOCK_SELECTION] = VER, + [_SC_CPUTIME] = VER, + [_SC_THREAD_CPUTIME] = -1, + [_SC_DEVICE_IO] = -1, + [_SC_DEVICE_SPECIFIC] = -1, + [_SC_DEVICE_SPECIFIC_R] = -1, + [_SC_FD_MGMT] = -1, + [_SC_FIFO] = -1, + [_SC_PIPE] = -1, + [_SC_FILE_ATTRIBUTES] = -1, + [_SC_FILE_LOCKING] = -1, + [_SC_FILE_SYSTEM] = -1, + [_SC_MONOTONIC_CLOCK] = VER, + [_SC_MULTI_PROCESS] = -1, + [_SC_SINGLE_PROCESS] = -1, + [_SC_NETWORKING] = -1, + [_SC_READER_WRITER_LOCKS] = VER, + [_SC_SPIN_LOCKS] = VER, + [_SC_REGEXP] = 1, + [_SC_REGEX_VERSION] = -1, + [_SC_SHELL] = 1, + [_SC_SIGNALS] = -1, + [_SC_SPAWN] = -1, + [_SC_SPORADIC_SERVER] = -1, + [_SC_THREAD_SPORADIC_SERVER] = -1, + [_SC_SYSTEM_DATABASE] = -1, + [_SC_SYSTEM_DATABASE_R] = -1, + [_SC_TIMEOUTS] = VER, + [_SC_TYPED_MEMORY_OBJECTS] = -1, + [_SC_USER_GROUPS] = -1, + [_SC_USER_GROUPS_R] = -1, + [_SC_2_PBS] = -1, + [_SC_2_PBS_ACCOUNTING] = -1, + [_SC_2_PBS_LOCATE] = -1, + [_SC_2_PBS_MESSAGE] = -1, + [_SC_2_PBS_TRACK] = -1, + [_SC_SYMLOOP_MAX] = SYMLOOP_MAX, + [_SC_STREAMS] = 0, + [_SC_2_PBS_CHECKPOINT] = -1, + [_SC_V6_ILP32_OFF32] = -1, + [_SC_V6_ILP32_OFFBIG] = 2*(sizeof(long)==4)-1, + [_SC_V6_LP64_OFF64] = 2*(sizeof(long)==8)-1, + [_SC_V6_LPBIG_OFFBIG] = 2*(sizeof(long)>=8)-1, + [_SC_HOST_NAME_MAX] = HOST_NAME_MAX, + [_SC_TRACE] = -1, + [_SC_TRACE_EVENT_FILTER] = -1, + [_SC_TRACE_INHERIT] = -1, + [_SC_TRACE_LOG] = -1, + + [_SC_IPV6] = VER, + [_SC_RAW_SOCKETS] = VER, + [_SC_V7_ILP32_OFF32] = -1, + [_SC_V7_ILP32_OFFBIG] = 2*(sizeof(long)==4)-1, + [_SC_V7_LP64_OFF64] = 2*(sizeof(long)==8)-1, + [_SC_V7_LPBIG_OFFBIG] = 2*(sizeof(long)>=8)-1, + [_SC_SS_REPL_MAX] = -1, + [_SC_TRACE_EVENT_NAME_MAX] = -1, + [_SC_TRACE_NAME_MAX] = -1, + [_SC_TRACE_SYS_MAX] = -1, + [_SC_TRACE_USER_EVENT_MAX] = -1, + [_SC_XOPEN_STREAMS] = 0, + [_SC_THREAD_ROBUST_PRIO_INHERIT] = -1, + [_SC_THREAD_ROBUST_PRIO_PROTECT] = -1, + }; + if (name > sizeof(values)/sizeof(values[0])) { + errno = EINVAL; + return -1; + } else if (values[name] == VER) { + return _POSIX_VERSION; + } else if (values[name] == OFLOW) { + return ARG_MAX; + } else { + return values[name]; + } +} diff --git a/05/musl-final/src/ctype/__ctype_get_mb_cur_max.c b/05/musl-final/src/ctype/__ctype_get_mb_cur_max.c new file mode 100644 index 0000000..42e4ee7 --- /dev/null +++ b/05/musl-final/src/ctype/__ctype_get_mb_cur_max.c @@ -0,0 +1,6 @@ +#include + +size_t __ctype_get_mb_cur_max() +{ + return 4; +} diff --git a/05/musl-final/src/ctype/isalnum.c b/05/musl-final/src/ctype/isalnum.c new file mode 100644 index 0000000..e3d2cf0 --- /dev/null +++ b/05/musl-final/src/ctype/isalnum.c @@ -0,0 +1,6 @@ +#include + +int isalnum(int c) +{ + return isalpha(c) || isdigit(c); +} diff --git a/05/musl-final/src/ctype/isalpha.c b/05/musl-final/src/ctype/isalpha.c new file mode 100644 index 0000000..53e115c --- /dev/null +++ b/05/musl-final/src/ctype/isalpha.c @@ -0,0 +1,7 @@ +#include +#undef isalpha + +int isalpha(int c) +{ + return ((unsigned)c|32)-'a' < 26; +} diff --git a/05/musl-final/src/ctype/isascii.c b/05/musl-final/src/ctype/isascii.c new file mode 100644 index 0000000..3af0a10 --- /dev/null +++ b/05/musl-final/src/ctype/isascii.c @@ -0,0 +1,6 @@ +#include + +int isascii(int c) +{ + return !(c&~0x7f); +} diff --git a/05/musl-final/src/ctype/isblank.c b/05/musl-final/src/ctype/isblank.c new file mode 100644 index 0000000..957400b --- /dev/null +++ b/05/musl-final/src/ctype/isblank.c @@ -0,0 +1,6 @@ +#include + +int isblank(int c) +{ + return (c == ' ' || c == '\t'); +} diff --git a/05/musl-final/src/ctype/iscntrl.c b/05/musl-final/src/ctype/iscntrl.c new file mode 100644 index 0000000..92ed7f0 --- /dev/null +++ b/05/musl-final/src/ctype/iscntrl.c @@ -0,0 +1,6 @@ +#include + +int iscntrl(int c) +{ + return (unsigned)c < 0x20 || c == 0x7f; +} diff --git a/05/musl-final/src/ctype/isdigit.c b/05/musl-final/src/ctype/isdigit.c new file mode 100644 index 0000000..0bc82a6 --- /dev/null +++ b/05/musl-final/src/ctype/isdigit.c @@ -0,0 +1,7 @@ +#include +#undef isdigit + +int isdigit(int c) +{ + return (unsigned)c-'0' < 10; +} diff --git a/05/musl-final/src/ctype/isgraph.c b/05/musl-final/src/ctype/isgraph.c new file mode 100644 index 0000000..98979d1 --- /dev/null +++ b/05/musl-final/src/ctype/isgraph.c @@ -0,0 +1,4 @@ +int isgraph(int c) +{ + return (unsigned)c-0x21 < 0x5e; +} diff --git a/05/musl-final/src/ctype/islower.c b/05/musl-final/src/ctype/islower.c new file mode 100644 index 0000000..d72fb21 --- /dev/null +++ b/05/musl-final/src/ctype/islower.c @@ -0,0 +1,7 @@ +#include +#undef islower + +int islower(int c) +{ + return (unsigned)c-'a' < 26; +} diff --git a/05/musl-final/src/ctype/isprint.c b/05/musl-final/src/ctype/isprint.c new file mode 100644 index 0000000..504e66e --- /dev/null +++ b/05/musl-final/src/ctype/isprint.c @@ -0,0 +1,4 @@ +int isprint(int c) +{ + return (unsigned)c-0x20 < 0x5f; +} diff --git a/05/musl-final/src/ctype/ispunct.c b/05/musl-final/src/ctype/ispunct.c new file mode 100644 index 0000000..fc45535 --- /dev/null +++ b/05/musl-final/src/ctype/ispunct.c @@ -0,0 +1,6 @@ +#include + +int ispunct(int c) +{ + return isgraph(c) && !isalnum(c); +} diff --git a/05/musl-final/src/ctype/isspace.c b/05/musl-final/src/ctype/isspace.c new file mode 100644 index 0000000..8e535aa --- /dev/null +++ b/05/musl-final/src/ctype/isspace.c @@ -0,0 +1,6 @@ +#include + +int isspace(int c) +{ + return c == ' ' || (unsigned)c-'\t' < 5; +} diff --git a/05/musl-final/src/ctype/isupper.c b/05/musl-final/src/ctype/isupper.c new file mode 100644 index 0000000..f09d88c --- /dev/null +++ b/05/musl-final/src/ctype/isupper.c @@ -0,0 +1,7 @@ +#include +#undef isupper + +int isupper(int c) +{ + return (unsigned)c-'A' < 26; +} diff --git a/05/musl-final/src/ctype/iswalnum.c b/05/musl-final/src/ctype/iswalnum.c new file mode 100644 index 0000000..d3b5667 --- /dev/null +++ b/05/musl-final/src/ctype/iswalnum.c @@ -0,0 +1,9 @@ +#include +#include + +#undef iswalnum + +int iswalnum(wint_t wc) +{ + return (unsigned)wc-'0' < 10 || iswalpha(wc); +} diff --git a/05/musl-final/src/ctype/iswalpha.c b/05/musl-final/src/ctype/iswalpha.c new file mode 100644 index 0000000..0f031ea --- /dev/null +++ b/05/musl-final/src/ctype/iswalpha.c @@ -0,0 +1,6 @@ +#include + +int iswalpha(wint_t wc) +{ + return (32U|wc)-'a'<26; +} diff --git a/05/musl-final/src/ctype/iswblank.c b/05/musl-final/src/ctype/iswblank.c new file mode 100644 index 0000000..bc6196f --- /dev/null +++ b/05/musl-final/src/ctype/iswblank.c @@ -0,0 +1,8 @@ +#include +#include +#include + +int iswblank(wint_t wc) +{ + return isblank(wc); +} diff --git a/05/musl-final/src/ctype/iswcntrl.c b/05/musl-final/src/ctype/iswcntrl.c new file mode 100644 index 0000000..93942b0 --- /dev/null +++ b/05/musl-final/src/ctype/iswcntrl.c @@ -0,0 +1,10 @@ +#include +#include + +int iswcntrl(wint_t wc) +{ + return (unsigned)wc < 32 + || (unsigned)(wc-0x7f) < 33 + || (unsigned)(wc-0x2028) < 2 + || (unsigned)(wc-0xfff9) < 3; +} diff --git a/05/musl-final/src/ctype/iswctype.c b/05/musl-final/src/ctype/iswctype.c new file mode 100644 index 0000000..d917975 --- /dev/null +++ b/05/musl-final/src/ctype/iswctype.c @@ -0,0 +1,63 @@ +#include +#include +#include + +#define WCTYPE_ALNUM 1 +#define WCTYPE_ALPHA 2 +#define WCTYPE_BLANK 3 +#define WCTYPE_CNTRL 4 +#define WCTYPE_DIGIT 5 +#define WCTYPE_GRAPH 6 +#define WCTYPE_LOWER 7 +#define WCTYPE_PRINT 8 +#define WCTYPE_PUNCT 9 +#define WCTYPE_SPACE 10 +#define WCTYPE_UPPER 11 +#define WCTYPE_XDIGIT 12 + +int iswctype(wint_t wc, wctype_t type) +{ + switch (type) { + case WCTYPE_ALNUM: + return iswalnum(wc); + case WCTYPE_ALPHA: + return iswalpha(wc); + case WCTYPE_BLANK: + return iswblank(wc); + case WCTYPE_CNTRL: + return iswcntrl(wc); + case WCTYPE_DIGIT: + return iswdigit(wc); + case WCTYPE_GRAPH: + return iswgraph(wc); + case WCTYPE_LOWER: + return iswlower(wc); + case WCTYPE_PRINT: + return iswprint(wc); + case WCTYPE_PUNCT: + return iswpunct(wc); + case WCTYPE_SPACE: + return iswspace(wc); + case WCTYPE_UPPER: + return iswupper(wc); + case WCTYPE_XDIGIT: + return iswxdigit(wc); + } + return 0; +} + +wctype_t wctype(const char *s) +{ + int i; + const char *p; + /* order must match! */ + static const char names[] = + "alnum\0" "alpha\0" "blank\0" + "cntrl\0" "digit\0" "graph\0" + "lower\0" "print\0" "punct\0" + "space\0" "upper\0" "xdigit"; + for (i=1, p=names; *p; i++, p+=6) + if (*s == *p && !strcmp(s, p)) + return i; + return 0; +} diff --git a/05/musl-final/src/ctype/iswdigit.c b/05/musl-final/src/ctype/iswdigit.c new file mode 100644 index 0000000..0487145 --- /dev/null +++ b/05/musl-final/src/ctype/iswdigit.c @@ -0,0 +1,9 @@ +#include +#include + +#undef iswdigit + +int iswdigit(wint_t wc) +{ + return (unsigned)wc-'0' < 10; +} diff --git a/05/musl-final/src/ctype/iswgraph.c b/05/musl-final/src/ctype/iswgraph.c new file mode 100644 index 0000000..fdc9785 --- /dev/null +++ b/05/musl-final/src/ctype/iswgraph.c @@ -0,0 +1,7 @@ +#include + +int iswgraph(wint_t wc) +{ + /* ISO C defines this function as: */ + return !iswspace(wc) && iswprint(wc); +} diff --git a/05/musl-final/src/ctype/iswlower.c b/05/musl-final/src/ctype/iswlower.c new file mode 100644 index 0000000..0a568e7 --- /dev/null +++ b/05/musl-final/src/ctype/iswlower.c @@ -0,0 +1,6 @@ +#include + +int iswlower(wint_t wc) +{ + return towupper(wc) != wc; +} diff --git a/05/musl-final/src/ctype/iswprint.c b/05/musl-final/src/ctype/iswprint.c new file mode 100644 index 0000000..7717671 --- /dev/null +++ b/05/musl-final/src/ctype/iswprint.c @@ -0,0 +1,10 @@ +#include + +int iswprint(wint_t wc) +{ + unsigned c = wc; + /* assume any non-control, non-illegal codepoint is printable */ + if (c>0x10ffff || c-0xd800<0x800 || (c&0xfffe)==0xfffe || iswcntrl(c)) + return 0; + return 1; +} diff --git a/05/musl-final/src/ctype/iswpunct.c b/05/musl-final/src/ctype/iswpunct.c new file mode 100644 index 0000000..1414c30 --- /dev/null +++ b/05/musl-final/src/ctype/iswpunct.c @@ -0,0 +1,138 @@ +#include +#include + +/* The below data is derived from classes (P.|Sm) plus Pattern_Syntax */ + +#define R(a,b) { (b), (b)-(a) } + +static const struct range { + uint32_t base:20; + uint32_t len:12; +} ranges[] = { +R(0x21, 0x2f), +R(0x3a, 0x40), +R(0x5b, 0x60), +R(0x7b, 0x7e), +R(0xa1, 0xa7), +R(0xa9, 0xa9), +R(0xab, 0xac), +R(0xae, 0xae), +R(0xb0, 0xb1), +R(0xb6, 0xb7), +R(0xbb, 0xbb), +R(0xbf, 0xbf), +R(0xd7, 0xd7), +R(0xf7, 0xf7), +R(0x37e, 0x37e), +R(0x387, 0x387), +R(0x3f6, 0x3f6), +R(0x55a, 0x55f), +R(0x589, 0x58a), +R(0x5be, 0x5be), +R(0x5c0, 0x5c0), +R(0x5c3, 0x5c3), +R(0x5c6, 0x5c6), +R(0x5f3, 0x5f4), +R(0x606, 0x60a), +R(0x60c, 0x60d), +R(0x61b, 0x61b), +R(0x61e, 0x61f), +R(0x66a, 0x66d), +R(0x6d4, 0x6d4), +R(0x700, 0x70d), +R(0x7f7, 0x7f9), +R(0x964, 0x965), +R(0x970, 0x970), +R(0xdf4, 0xdf4), +R(0xe4f, 0xe4f), +R(0xe5a, 0xe5b), +R(0xf04, 0xf12), +R(0xf3a, 0xf3d), +R(0xf85, 0xf85), +R(0xfd0, 0xfd4), +R(0x104a, 0x104f), +R(0x10fb, 0x10fb), +R(0x1361, 0x1368), +R(0x166d, 0x166e), +R(0x1680, 0x1680), +R(0x169b, 0x169c), +R(0x16eb, 0x16ed), +R(0x1735, 0x1736), +R(0x17d4, 0x17d6), +R(0x17d8, 0x17da), +R(0x1800, 0x180a), +R(0x180e, 0x180e), +R(0x1944, 0x1945), +R(0x19de, 0x19df), +R(0x1a1e, 0x1a1f), +R(0x1b5a, 0x1b60), +R(0x1c3b, 0x1c3f), +R(0x1c7e, 0x1c7f), +R(0x2010, 0x2027), +R(0x2030, 0x205e), +R(0x207a, 0x207e), +R(0x208a, 0x208e), +R(0x2140, 0x2144), +R(0x214b, 0x214b), +R(0x2190, 0x245f), +R(0x2500, 0x2775), +R(0x2794, 0x2bff), +R(0x2cf9, 0x2cfc), +R(0x2cfe, 0x2cff), +R(0x2e00, 0x2e7f), +R(0x3001, 0x3003), +R(0x3008, 0x3020), +R(0x3030, 0x3030), +R(0x303d, 0x303d), +R(0x30a0, 0x30a0), +R(0x30fb, 0x30fb), +R(0xa60d, 0xa60f), +R(0xa874, 0xa877), +R(0xa8ce, 0xa8cf), +R(0xa92e, 0xa92f), +R(0xa95f, 0xa95f), +R(0xfb29, 0xfb29), +R(0xfd3e, 0xfd3f), +R(0xfe10, 0xfe19), +R(0xfe30, 0xfe52), +R(0xfe54, 0xfe66), +R(0xfe68, 0xfe68), +R(0xfe6a, 0xfe6b), +R(0xff01, 0xff03), +R(0xff05, 0xff0f), +R(0xff1a, 0xff20), +R(0xff3b, 0xff3d), +R(0xff3f, 0xff3f), +R(0xff5b, 0xff65), +R(0xffe2, 0xffe2), +R(0xffe9, 0xffec), +R(0x10100, 0x10101), +R(0x1039f, 0x1039f), +R(0x103d0, 0x103d0), +R(0x1091f, 0x1091f), +R(0x1093f, 0x1093f), +R(0x10a50, 0x10a58), +R(0x12470, 0x12473), +R(0x1d6c1, 0x1d6c1), +R(0x1d6db, 0x1d6db), +R(0x1d6fb, 0x1d6fb), +R(0x1d715, 0x1d715), +R(0x1d735, 0x1d735), +R(0x1d74f, 0x1d74f), +R(0x1d76f, 0x1d76f), +R(0x1d789, 0x1d789), +R(0x1d7a9, 0x1d7a9), +R(0x1d7c3, 0x1d7c3), +}; + +int iswpunct(wint_t wc) +{ + unsigned c = wc; + int a = 0; + int n = sizeof ranges / sizeof ranges[0]; + do { + n >>= 1; + a += n+1 & (signed)(ranges[a+n].base-c)>>31; + } while (n); + return ranges[a].base-c <= ranges[a].len; +} diff --git a/05/musl-final/src/ctype/iswspace.c b/05/musl-final/src/ctype/iswspace.c new file mode 100644 index 0000000..68a1743 --- /dev/null +++ b/05/musl-final/src/ctype/iswspace.c @@ -0,0 +1,15 @@ +#include +#include +#include + +int iswspace(wint_t wc) +{ + static const wchar_t spaces[] = { + ' ', '\t', '\n', '\r', 11, 12, 0x0085, + 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, + 0x2006, 0x2008, 0x2009, 0x200a, 0x200b, + 0x2028, 0x2029, 0x2050, 0x3000, 0 + }; + if (wcschr(spaces, wc)) return 1; + return 0; +} diff --git a/05/musl-final/src/ctype/iswupper.c b/05/musl-final/src/ctype/iswupper.c new file mode 100644 index 0000000..eae59a7 --- /dev/null +++ b/05/musl-final/src/ctype/iswupper.c @@ -0,0 +1,6 @@ +#include + +int iswupper(wint_t wc) +{ + return towlower(wc) != wc; +} diff --git a/05/musl-final/src/ctype/iswxdigit.c b/05/musl-final/src/ctype/iswxdigit.c new file mode 100644 index 0000000..229a469 --- /dev/null +++ b/05/musl-final/src/ctype/iswxdigit.c @@ -0,0 +1,7 @@ +#include +#include + +int iswxdigit(wint_t wc) +{ + return (unsigned)(wc-'0') < 10 || (unsigned)((wc|32)-'a') < 6; +} diff --git a/05/musl-final/src/ctype/isxdigit.c b/05/musl-final/src/ctype/isxdigit.c new file mode 100644 index 0000000..ae68a3d --- /dev/null +++ b/05/musl-final/src/ctype/isxdigit.c @@ -0,0 +1,6 @@ +#include + +int isxdigit(int c) +{ + return isdigit(c) || ((unsigned)c|32)-'a' < 6; +} diff --git a/05/musl-final/src/ctype/toascii.c b/05/musl-final/src/ctype/toascii.c new file mode 100644 index 0000000..f0e48e8 --- /dev/null +++ b/05/musl-final/src/ctype/toascii.c @@ -0,0 +1,7 @@ +#include + +/* nonsense function that should NEVER be used! */ +int toascii(int c) +{ + return c & 0x7f; +} diff --git a/05/musl-final/src/ctype/tolower.c b/05/musl-final/src/ctype/tolower.c new file mode 100644 index 0000000..b56f3c5 --- /dev/null +++ b/05/musl-final/src/ctype/tolower.c @@ -0,0 +1,7 @@ +#include + +int tolower(int c) +{ + if (isupper(c)) return c | 32; + return c; +} diff --git a/05/musl-final/src/ctype/toupper.c b/05/musl-final/src/ctype/toupper.c new file mode 100644 index 0000000..1799f03 --- /dev/null +++ b/05/musl-final/src/ctype/toupper.c @@ -0,0 +1,7 @@ +#include + +int toupper(int c) +{ + if (islower(c)) return c & 0x5f; + return c; +} diff --git a/05/musl-final/src/ctype/towctrans.c b/05/musl-final/src/ctype/towctrans.c new file mode 100644 index 0000000..0b1eed0 --- /dev/null +++ b/05/musl-final/src/ctype/towctrans.c @@ -0,0 +1,246 @@ +#include +#include +#include + +#define CASEMAP(u1,u2,l) { (u1), (l)-(u1), (u2)-(u1)+1 } +#define CASELACE(u1,u2) CASEMAP((u1),(u2),(u1)+1) + +static const struct { + unsigned short upper; + signed char lower; + unsigned char len; +} casemaps[] = { + CASEMAP('A','Z','a'), + CASEMAP(0xc0,0xde,0xe0), + + CASELACE(0x0100,0x012e), + CASELACE(0x0132,0x0136), + CASELACE(0x0139,0x0147), + CASELACE(0x014a,0x0176), + CASELACE(0x0179,0x017d), + + CASELACE(0x370,0x372), + CASEMAP(0x391,0x3a1,0x3b1), + CASEMAP(0x3a3,0x3ab,0x3c3), + CASEMAP(0x400,0x40f,0x450), + CASEMAP(0x410,0x42f,0x430), + + CASELACE(0x460,0x480), + CASELACE(0x48a,0x4be), + CASELACE(0x4c1,0x4cd), + CASELACE(0x4d0,0x50e), + + CASEMAP(0x531,0x556,0x561), + + CASELACE(0x01a0,0x01a4), + CASELACE(0x01b3,0x01b5), + CASELACE(0x01cd,0x01db), + CASELACE(0x01de,0x01ee), + CASELACE(0x01f8,0x021e), + CASELACE(0x0222,0x0232), + CASELACE(0x03d8,0x03ee), + + CASELACE(0x1e00,0x1e94), + CASELACE(0x1ea0,0x1efe), + + CASEMAP(0x1f08,0x1f0f,0x1f00), + CASEMAP(0x1f18,0x1f1d,0x1f10), + CASEMAP(0x1f28,0x1f2f,0x1f20), + CASEMAP(0x1f38,0x1f3f,0x1f30), + CASEMAP(0x1f48,0x1f4d,0x1f40), + + CASEMAP(0x1f68,0x1f6f,0x1f60), + CASEMAP(0x1f88,0x1f8f,0x1f80), + CASEMAP(0x1f98,0x1f9f,0x1f90), + CASEMAP(0x1fa8,0x1faf,0x1fa0), + CASEMAP(0x1fb8,0x1fb9,0x1fb0), + CASEMAP(0x1fba,0x1fbb,0x1f70), + CASEMAP(0x1fc8,0x1fcb,0x1f72), + CASEMAP(0x1fd8,0x1fd9,0x1fd0), + CASEMAP(0x1fda,0x1fdb,0x1f76), + CASEMAP(0x1fe8,0x1fe9,0x1fe0), + CASEMAP(0x1fea,0x1feb,0x1f7a), + CASEMAP(0x1ff8,0x1ff9,0x1f78), + CASEMAP(0x1ffa,0x1ffb,0x1f7c), + + CASELACE(0x246,0x24e), + CASELACE(0x510,0x512), + CASEMAP(0x2160,0x216f,0x2170), + CASEMAP(0x2c00,0x2c2e,0x2c30), + CASELACE(0x2c67,0x2c6b), + CASELACE(0x2c80,0x2ce2), + + CASELACE(0xa722,0xa72e), + CASELACE(0xa732,0xa76e), + CASELACE(0xa779,0xa77b), + CASELACE(0xa77e,0xa786), + + CASEMAP(0xff21,0xff3a,0xff41), + { 0,0,0 } +}; + +static const unsigned short pairs[][2] = { + { 'I', 0x0131 }, + { 'S', 0x017f }, + { 0x0130, 'i' }, + { 0x0178, 0x00ff }, + { 0x0181, 0x0253 }, + { 0x0182, 0x0183 }, + { 0x0184, 0x0185 }, + { 0x0186, 0x0254 }, + { 0x0187, 0x0188 }, + { 0x0189, 0x0256 }, + { 0x018a, 0x0257 }, + { 0x018b, 0x018c }, + { 0x018e, 0x01dd }, + { 0x018f, 0x0259 }, + { 0x0190, 0x025b }, + { 0x0191, 0x0192 }, + { 0x0193, 0x0260 }, + { 0x0194, 0x0263 }, + { 0x0196, 0x0269 }, + { 0x0197, 0x0268 }, + { 0x0198, 0x0199 }, + { 0x019c, 0x026f }, + { 0x019d, 0x0272 }, + { 0x019f, 0x0275 }, + { 0x01a6, 0x0280 }, + { 0x01a7, 0x01a8 }, + { 0x01a9, 0x0283 }, + { 0x01ac, 0x01ad }, + { 0x01ae, 0x0288 }, + { 0x01af, 0x01b0 }, + { 0x01b1, 0x028a }, + { 0x01b2, 0x028b }, + { 0x01b7, 0x0292 }, + { 0x01b8, 0x01b9 }, + { 0x01bc, 0x01bd }, + { 0x01c4, 0x01c6 }, + { 0x01c4, 0x01c5 }, + { 0x01c5, 0x01c6 }, + { 0x01c7, 0x01c9 }, + { 0x01c7, 0x01c8 }, + { 0x01c8, 0x01c9 }, + { 0x01ca, 0x01cc }, + { 0x01ca, 0x01cb }, + { 0x01cb, 0x01cc }, + { 0x01f1, 0x01f3 }, + { 0x01f1, 0x01f2 }, + { 0x01f2, 0x01f3 }, + { 0x01f4, 0x01f5 }, + { 0x01f6, 0x0195 }, + { 0x01f7, 0x01bf }, + { 0x0220, 0x019e }, + { 0x0386, 0x03ac }, + { 0x0388, 0x03ad }, + { 0x0389, 0x03ae }, + { 0x038a, 0x03af }, + { 0x038c, 0x03cc }, + { 0x038e, 0x03cd }, + { 0x038f, 0x03ce }, + { 0x0399, 0x0345 }, + { 0x0399, 0x1fbe }, + { 0x03a3, 0x03c2 }, + { 0x03f7, 0x03f8 }, + { 0x03fa, 0x03fb }, + { 0x1e60, 0x1e9b }, + + { 0x1f59, 0x1f51 }, + { 0x1f5b, 0x1f53 }, + { 0x1f5d, 0x1f55 }, + { 0x1f5f, 0x1f57 }, + { 0x1fbc, 0x1fb3 }, + { 0x1fcc, 0x1fc3 }, + { 0x1fec, 0x1fe5 }, + { 0x1ffc, 0x1ff3 }, + + { 0x23a, 0x2c65 }, + { 0x23b, 0x23c }, + { 0x23d, 0x19a }, + { 0x23e, 0x2c66 }, + { 0x241, 0x242 }, + { 0x243, 0x180 }, + { 0x244, 0x289 }, + { 0x245, 0x28c }, + { 0x3f4, 0x3b8 }, + { 0x3f9, 0x3f2 }, + { 0x3fd, 0x37b }, + { 0x3fe, 0x37c }, + { 0x3ff, 0x37d }, + { 0x4c0, 0x4cf }, + + { 0x2126, 0x3c9 }, + { 0x212a, 'k' }, + { 0x212b, 0xe5 }, + { 0x2132, 0x214e }, + { 0x2183, 0x2184 }, + { 0x2c60, 0x2c61 }, + { 0x2c62, 0x26b }, + { 0x2c63, 0x1d7d }, + { 0x2c64, 0x27d }, + { 0x2c6d, 0x251 }, + { 0x2c6e, 0x271 }, + { 0x2c6f, 0x250 }, + { 0x2c72, 0x2c73 }, + { 0x2c75, 0x2c76 }, + + { 0xa77d, 0x1d79 }, + + /* bogus greek 'symbol' letters */ + { 0x376, 0x377 }, + { 0x39c, 0xb5 }, + { 0x392, 0x3d0 }, + { 0x398, 0x3d1 }, + { 0x3a6, 0x3d5 }, + { 0x3a0, 0x3d6 }, + { 0x39a, 0x3f0 }, + { 0x3a1, 0x3f1 }, + { 0x395, 0x3f5 }, + { 0x3cf, 0x3d7 }, + + { 0,0 } +}; + + +static wchar_t __towcase(wchar_t wc, int lower) +{ + int i; + int lmul = 2*lower-1; + int lmask = lower-1; + if ((unsigned)wc - 0x10400 < 0x50) + return wc + lmul*0x28; + /* no letters with case in these large ranges */ + if (!iswalpha(wc) + || (unsigned)wc - 0x0600 <= 0x0fff-0x0600 + || (unsigned)wc - 0x2e00 <= 0xa6ff-0x2e00 + || (unsigned)wc - 0xa800 <= 0xfeff-0xa800) + return wc; + /* special case because the diff between upper/lower is too big */ + if ((unsigned)wc - 0x10a0 < 0x26 || (unsigned)wc - 0x2d00 < 0x26) + return wc + lmul*(0x2d00-0x10a0); + for (i=0; casemaps[i].len; i++) { + int base = casemaps[i].upper + (lmask & casemaps[i].lower); + if ((unsigned)wc-base < casemaps[i].len) { + if (casemaps[i].lower == 1) + return wc + lower - ((wc-casemaps[i].upper)&1); + return wc + lmul*casemaps[i].lower; + } + } + for (i=0; pairs[i][1-lower]; i++) { + if (pairs[i][1-lower] == wc) + return pairs[i][lower]; + } + if ((unsigned)wc - 0x10428 + (lower<<5) + (lower<<3) < 0x28) + return wc - 0x28 + (lower<<10) + (lower<<6); + return wc; +} + +wint_t towupper(wint_t wc) +{ + return __towcase(wc, 0); +} + +wint_t towlower(wint_t wc) +{ + return __towcase(wc, 1); +} diff --git a/05/musl-final/src/ctype/wcswidth.c b/05/musl-final/src/ctype/wcswidth.c new file mode 100644 index 0000000..5c8a5a4 --- /dev/null +++ b/05/musl-final/src/ctype/wcswidth.c @@ -0,0 +1,8 @@ +#include + +int wcswidth(const wchar_t *wcs, size_t n) +{ + int l=0, k=0; + for (; n-- && *wcs && (k = wcwidth(*wcs)) >= 0; l+=k, wcs++); + return (k < 0) ? k : l; +} diff --git a/05/musl-final/src/ctype/wctrans.c b/05/musl-final/src/ctype/wctrans.c new file mode 100644 index 0000000..03e9fd6 --- /dev/null +++ b/05/musl-final/src/ctype/wctrans.c @@ -0,0 +1,16 @@ +#include +#include + +wctrans_t wctrans(const char *class) +{ + if (!strcmp(class, "toupper")) return 1; + if (!strcmp(class, "tolower")) return 2; + return 0; +} + +wint_t towctrans(wint_t wc, wctrans_t trans) +{ + if (trans == 1) return towupper(wc); + if (trans == 2) return towlower(wc); + return wc; +} diff --git a/05/musl-final/src/ctype/wcwidth.c b/05/musl-final/src/ctype/wcwidth.c new file mode 100644 index 0000000..eaafd7c --- /dev/null +++ b/05/musl-final/src/ctype/wcwidth.c @@ -0,0 +1,186 @@ +#include +#include +#include + +#define R(a,b,w) { (b), (w)/2, (b)-(a) } + +static const struct range { + uint32_t base:20; + uint32_t width:1; + uint32_t len:11; +} ranges[] = { + R(0x0300, 0x036F, 0), + R(0x0483, 0x0486, 0), + R(0x0488, 0x0489, 0), + R(0x0591, 0x05BD, 0), + R(0x05BF, 0x05BF, 0), + R(0x05C1, 0x05C2, 0), + R(0x05C4, 0x05C5, 0), + R(0x05C7, 0x05C7, 0), + R(0x0600, 0x0603, 0), + R(0x0610, 0x0615, 0), + R(0x064B, 0x065E, 0), + R(0x0670, 0x0670, 0), + R(0x06D6, 0x06E4, 0), + R(0x06E7, 0x06E8, 0), + R(0x06EA, 0x06ED, 0), + R(0x070F, 0x070F, 0), + R(0x0711, 0x0711, 0), + R(0x0730, 0x074A, 0), + R(0x07A6, 0x07B0, 0), + R(0x07EB, 0x07F3, 0), + R(0x0901, 0x0902, 0), + R(0x093C, 0x093C, 0), + R(0x0941, 0x0948, 0), + R(0x094D, 0x094D, 0), + R(0x0951, 0x0954, 0), + R(0x0962, 0x0963, 0), + R(0x0981, 0x0981, 0), + R(0x09BC, 0x09BC, 0), + R(0x09C1, 0x09C4, 0), + R(0x09CD, 0x09CD, 0), + R(0x09E2, 0x09E3, 0), + R(0x0A01, 0x0A02, 0), + R(0x0A3C, 0x0A3C, 0), + R(0x0A41, 0x0A42, 0), + R(0x0A47, 0x0A48, 0), + R(0x0A4B, 0x0A4D, 0), + R(0x0A70, 0x0A71, 0), + R(0x0A81, 0x0A82, 0), + R(0x0ABC, 0x0ABC, 0), + R(0x0AC1, 0x0AC5, 0), + R(0x0AC7, 0x0AC8, 0), + R(0x0ACD, 0x0ACD, 0), + R(0x0AE2, 0x0AE3, 0), + R(0x0B01, 0x0B01, 0), + R(0x0B3C, 0x0B3C, 0), + R(0x0B3F, 0x0B3F, 0), + R(0x0B41, 0x0B43, 0), + R(0x0B4D, 0x0B4D, 0), + R(0x0B56, 0x0B56, 0), + R(0x0B82, 0x0B82, 0), + R(0x0BC0, 0x0BC0, 0), + R(0x0BCD, 0x0BCD, 0), + R(0x0C3E, 0x0C40, 0), + R(0x0C46, 0x0C48, 0), + R(0x0C4A, 0x0C4D, 0), + R(0x0C55, 0x0C56, 0), + R(0x0CBC, 0x0CBC, 0), + R(0x0CBF, 0x0CBF, 0), + R(0x0CC6, 0x0CC6, 0), + R(0x0CCC, 0x0CCD, 0), + R(0x0CE2, 0x0CE3, 0), + R(0x0D41, 0x0D43, 0), + R(0x0D4D, 0x0D4D, 0), + R(0x0DCA, 0x0DCA, 0), + R(0x0DD2, 0x0DD4, 0), + R(0x0DD6, 0x0DD6, 0), + R(0x0E31, 0x0E31, 0), + R(0x0E34, 0x0E3A, 0), + R(0x0E47, 0x0E4E, 0), + R(0x0EB1, 0x0EB1, 0), + R(0x0EB4, 0x0EB9, 0), + R(0x0EBB, 0x0EBC, 0), + R(0x0EC8, 0x0ECD, 0), + R(0x0F18, 0x0F19, 0), + R(0x0F35, 0x0F35, 0), + R(0x0F37, 0x0F37, 0), + R(0x0F39, 0x0F39, 0), + R(0x0F71, 0x0F7E, 0), + R(0x0F80, 0x0F84, 0), + R(0x0F86, 0x0F87, 0), + R(0x0F90, 0x0F97, 0), + R(0x0F99, 0x0FBC, 0), + R(0x0FC6, 0x0FC6, 0), + R(0x102D, 0x1030, 0), + R(0x1032, 0x1032, 0), + R(0x1036, 0x1037, 0), + R(0x1039, 0x1039, 0), + R(0x1058, 0x1059, 0), + R(0x1100, 0x115F, 2), + R(0x1160, 0x11FF, 0), + R(0x135F, 0x135F, 0), + R(0x1712, 0x1714, 0), + R(0x1732, 0x1734, 0), + R(0x1752, 0x1753, 0), + R(0x1772, 0x1773, 0), + R(0x17B4, 0x17B5, 0), + R(0x17B7, 0x17BD, 0), + R(0x17C6, 0x17C6, 0), + R(0x17C9, 0x17D3, 0), + R(0x17DD, 0x17DD, 0), + R(0x180B, 0x180D, 0), + R(0x18A9, 0x18A9, 0), + R(0x1920, 0x1922, 0), + R(0x1927, 0x1928, 0), + R(0x1932, 0x1932, 0), + R(0x1939, 0x193B, 0), + R(0x1A17, 0x1A18, 0), + R(0x1B00, 0x1B03, 0), + R(0x1B34, 0x1B34, 0), + R(0x1B36, 0x1B3A, 0), + R(0x1B3C, 0x1B3C, 0), + R(0x1B42, 0x1B42, 0), + R(0x1B6B, 0x1B73, 0), + R(0x1DC0, 0x1DCA, 0), + R(0x1DFE, 0x1DFF, 0), + R(0x200B, 0x200F, 0), + R(0x202A, 0x202E, 0), + R(0x2060, 0x2063, 0), + R(0x206A, 0x206F, 0), + R(0x20D0, 0x20EF, 0), + R(0x2329, 0x232A, 2), + R(0x2E80, 0x3029, 2), + R(0x302A, 0x302F, 0), + R(0x3030, 0x303E, 2), + R(0x3099, 0x309A, 0), + R(0xA806, 0xA806, 0), + R(0xA80B, 0xA80B, 0), + R(0xA825, 0xA826, 0), + R(0xF900, 0xFAFF, 2), + R(0xFB1E, 0xFB1E, 0), + R(0xFE00, 0xFE0F, 0), + R(0xFE20, 0xFE23, 0), + R(0xFE30, 0xFE6F, 2), + R(0xFEFF, 0xFEFF, 0), + R(0xFF00, 0xFF60, 2), + R(0xFFE0, 0xFFE6, 2), + R(0x10A01, 0x10A03, 0), + R(0x10A05, 0x10A06, 0), + R(0x10A0C, 0x10A0F, 0), + R(0x10A38, 0x10A3A, 0), + R(0x10A3F, 0x10A3F, 0), + R(0x1D167, 0x1D169, 0), + R(0x1D173, 0x1D182, 0), + R(0x1D185, 0x1D18B, 0), + R(0x1D1AA, 0x1D1AD, 0), + R(0x1D242, 0x1D244, 0), + R(0xE0001, 0xE0001, 0), + R(0xE0020, 0xE007F, 0), + R(0xE0100, 0xE01EF, 0), +}; + +/* Note: because the len field is only 10 bits, we must special-case + * the two huge ranges of full width characters and exclude them + * from the binary search table. */ + +int wcwidth(wchar_t wc) +{ + int a, n; + uint32_t c = wc; + + if (c-0x20 < 0x5f) return 1; + if (!iswprint(c)) return wc ? -1 : 0; + if (c-0x20000 < 0x20000) return 2; + + /* The following code is a branchless binary search. */ + a = 0; + n = sizeof ranges / sizeof ranges[0]; + do { + n >>= 1; + a += n+1 & (signed)(ranges[a+n].base-c)>>31; + } while (n); + if (ranges[a].base-c <= ranges[a].len) + return 2*ranges[a].width; + return 1 + (c-0x3040 < 0xd800-0x3040); +} diff --git a/05/musl-final/src/dirent/__dirent.h b/05/musl-final/src/dirent/__dirent.h new file mode 100644 index 0000000..07b3ee6 --- /dev/null +++ b/05/musl-final/src/dirent/__dirent.h @@ -0,0 +1,9 @@ +struct __DIR_s +{ + int lock; + int fd; + off_t tell; + int buf_pos; + int buf_end; + char buf[2048]; +}; diff --git a/05/musl-final/src/dirent/__getdents.c b/05/musl-final/src/dirent/__getdents.c new file mode 100644 index 0000000..dc37344 --- /dev/null +++ b/05/musl-final/src/dirent/__getdents.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +int __getdents(int fd, struct dirent *buf, size_t len) +{ + return syscall3(__NR_getdents, fd, (long)buf, len); +} + +weak_alias(__getdents, getdents); + +LFS64(getdents); diff --git a/05/musl-final/src/dirent/alphasort.c b/05/musl-final/src/dirent/alphasort.c new file mode 100644 index 0000000..42050fb --- /dev/null +++ b/05/musl-final/src/dirent/alphasort.c @@ -0,0 +1,10 @@ +#include +#include +#include "libc.h" + +int alphasort(const struct dirent **a, const struct dirent **b) +{ + return strcoll((*a)->d_name, (*b)->d_name); +} + +LFS64(alphasort); diff --git a/05/musl-final/src/dirent/closedir.c b/05/musl-final/src/dirent/closedir.c new file mode 100644 index 0000000..81e9591 --- /dev/null +++ b/05/musl-final/src/dirent/closedir.c @@ -0,0 +1,11 @@ +#include +#include +#include "__dirent.h" +#include "libc.h" + +int closedir(DIR *dir) +{ + int ret = close(dir->fd); + free(dir); + return ret; +} diff --git a/05/musl-final/src/dirent/dirfd.c b/05/musl-final/src/dirent/dirfd.c new file mode 100644 index 0000000..6c86007 --- /dev/null +++ b/05/musl-final/src/dirent/dirfd.c @@ -0,0 +1,7 @@ +#include +#include "__dirent.h" + +int dirfd(DIR *d) +{ + return d->fd; +} diff --git a/05/musl-final/src/dirent/fdopendir.c b/05/musl-final/src/dirent/fdopendir.c new file mode 100644 index 0000000..c4b8e61 --- /dev/null +++ b/05/musl-final/src/dirent/fdopendir.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +#include +#include "__dirent.h" + +DIR *fdopendir(int fd) +{ + DIR *dir; + struct stat st; + + if (fstat(fd, &st) < 0 || !S_ISDIR(st.st_mode)) { + errno = ENOTDIR; + return 0; + } + if (!(dir = calloc(1, sizeof *dir))) { + return 0; + } + + fcntl(fd, F_SETFD, FD_CLOEXEC); + dir->fd = fd; + return dir; +} diff --git a/05/musl-final/src/dirent/opendir.c b/05/musl-final/src/dirent/opendir.c new file mode 100644 index 0000000..cefe6ce --- /dev/null +++ b/05/musl-final/src/dirent/opendir.c @@ -0,0 +1,25 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include "__dirent.h" + +DIR *opendir(const char *name) +{ + int fd; + DIR *dir; + + if ((fd = open(name, O_RDONLY|O_DIRECTORY)) < 0) + return 0; + fcntl(fd, F_SETFD, FD_CLOEXEC); + if (!(dir = calloc(1, sizeof *dir))) { + close(fd); + return 0; + } + dir->fd = fd; + return dir; +} diff --git a/05/musl-final/src/dirent/readdir.c b/05/musl-final/src/dirent/readdir.c new file mode 100644 index 0000000..1aeb25a --- /dev/null +++ b/05/musl-final/src/dirent/readdir.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#include "__dirent.h" +#include "syscall.h" +#include "libc.h" + +int __getdents(int, struct dirent *, size_t); + +struct dirent *readdir(DIR *dir) +{ + struct dirent *de; + + if (dir->buf_pos >= dir->buf_end) { + int len = __getdents(dir->fd, (void *)dir->buf, sizeof dir->buf); + if (len < 0) { + dir->lock = 0; + return NULL; + } else if (len == 0) return 0; + dir->buf_end = len; + dir->buf_pos = 0; + } + de = (void *)(dir->buf + dir->buf_pos); + dir->buf_pos += de->d_reclen; + dir->tell = de->d_off; + return de; +} + +LFS64(readdir); diff --git a/05/musl-final/src/dirent/readdir_r.c b/05/musl-final/src/dirent/readdir_r.c new file mode 100644 index 0000000..58f6032 --- /dev/null +++ b/05/musl-final/src/dirent/readdir_r.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include "__dirent.h" +#include "libc.h" + +int readdir_r(DIR *dir, struct dirent *buf, struct dirent **result) +{ + struct dirent *de; + int errno_save = errno; + int ret; + + LOCK(&dir->lock); + errno = 0; + de = readdir(dir); + if ((ret = errno)) { + UNLOCK(&dir->lock); + return ret; + } + errno = errno_save; + if (de) memcpy(buf, de, de->d_reclen); + else buf = NULL; + + UNLOCK(&dir->lock); + *result = buf; + return 0; +} + +LFS64_2(readdir_r, readdir64_r); diff --git a/05/musl-final/src/dirent/rewinddir.c b/05/musl-final/src/dirent/rewinddir.c new file mode 100644 index 0000000..c6138f7 --- /dev/null +++ b/05/musl-final/src/dirent/rewinddir.c @@ -0,0 +1,13 @@ +#include +#include +#include "__dirent.h" +#include "libc.h" + +void rewinddir(DIR *dir) +{ + LOCK(&dir->lock); + lseek(dir->fd, 0, SEEK_SET); + dir->buf_pos = dir->buf_end = 0; + dir->tell = 0; + UNLOCK(&dir->lock); +} diff --git a/05/musl-final/src/dirent/scandir.c b/05/musl-final/src/dirent/scandir.c new file mode 100644 index 0000000..6a0a999 --- /dev/null +++ b/05/musl-final/src/dirent/scandir.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include + +int scandir(const char *path, struct dirent ***res, + int (*sel)(const struct dirent *), + int (*cmp)(const struct dirent **, const struct dirent **)) +{ + DIR *d = opendir(path); + struct dirent *de, **names=0, **tmp; + size_t cnt=0, len=0, size; + int old_errno = errno; + + if (!d) return -1; + + while ((errno=0), (de = readdir(d))) { + if (sel && !sel(de)) continue; + if (cnt >= len) { + len = 2*len+1; + if (len > SIZE_MAX/sizeof *names) break; + tmp = realloc(names, len * sizeof *names); + if (!tmp) break; + names = tmp; + } + size = offsetof(struct dirent,d_name) + strlen(de->d_name) + 1; + names[cnt] = malloc(size); + if (!names[cnt]) break; + memcpy(names[cnt++], de, size); + } + + closedir(d); + + if (errno) { + old_errno = errno; + if (names) while (cnt-->0) free(names[cnt]); + free(names); + errno = old_errno; + return -1; + } + + if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp); + *res = names; + return cnt; +} + +LFS64(scandir); diff --git a/05/musl-final/src/dirent/seekdir.c b/05/musl-final/src/dirent/seekdir.c new file mode 100644 index 0000000..81a0e33 --- /dev/null +++ b/05/musl-final/src/dirent/seekdir.c @@ -0,0 +1,12 @@ +#include +#include +#include "__dirent.h" +#include "libc.h" + +void seekdir(DIR *dir, long off) +{ + LOCK(&dir->lock); + dir->tell = lseek(dir->fd, off, SEEK_SET); + dir->buf_pos = dir->buf_end = 0; + UNLOCK(&dir->lock); +} diff --git a/05/musl-final/src/dirent/telldir.c b/05/musl-final/src/dirent/telldir.c new file mode 100644 index 0000000..cf25acf --- /dev/null +++ b/05/musl-final/src/dirent/telldir.c @@ -0,0 +1,7 @@ +#include +#include "__dirent.h" + +long telldir(DIR *dir) +{ + return dir->tell; +} diff --git a/05/musl-final/src/env/__environ.c b/05/musl-final/src/env/__environ.c new file mode 100644 index 0000000..8c32952 --- /dev/null +++ b/05/musl-final/src/env/__environ.c @@ -0,0 +1,7 @@ +#include "libc.h" + +#undef environ +char **___environ; +weak_alias(___environ, __environ); +weak_alias(___environ, _environ); +weak_alias(___environ, environ); diff --git a/05/musl-final/src/env/__libc_start_main.c b/05/musl-final/src/env/__libc_start_main.c new file mode 100644 index 0000000..d4f360e --- /dev/null +++ b/05/musl-final/src/env/__libc_start_main.c @@ -0,0 +1,30 @@ +#include "libc.h" + +#if 0 +/* Any use of __environ/environ will override this symbol. */ +char **__dummy_environ = (void *)-1; +weak_alias(__dummy_environ, ___environ); +#else +extern char **___environ; +#endif + +int __libc_start_main( + int (*main)(int, char **, char **), int argc, char **argv, + int (*init)(int, char **, char **), void (*fini)(void), + void (*ldso_fini)(void)) +{ + /* Save the environment if it may be used by libc/application */ + char **envp = argv+argc+1; + if (___environ != (void *)-1) ___environ = envp; + + /* Avoid writing 0 and triggering unnecessary COW */ + if (ldso_fini) libc.ldso_fini = ldso_fini; + if (fini) libc.fini = fini; + + /* Execute constructors (static) linked into the application */ + if (init) init(argc, argv, envp); + + /* Pass control to to application */ + exit(main(argc, argv, envp)); + return 0; +} diff --git a/05/musl-final/src/env/clearenv.c b/05/musl-final/src/env/clearenv.c new file mode 100644 index 0000000..a2475ce --- /dev/null +++ b/05/musl-final/src/env/clearenv.c @@ -0,0 +1,9 @@ +#include + +extern char **__environ; + +int clearenv() +{ + __environ[0] = 0; + return 0; +} diff --git a/05/musl-final/src/env/getenv.c b/05/musl-final/src/env/getenv.c new file mode 100644 index 0000000..00c1bce --- /dev/null +++ b/05/musl-final/src/env/getenv.c @@ -0,0 +1,14 @@ +#include +#include +#include "libc.h" + +char *getenv(const char *name) +{ + int i; + size_t l = strlen(name); + if (!__environ || !*name || strchr(name, '=')) return NULL; + for (i=0; __environ[i] && (strncmp(name, __environ[i], l) + || __environ[i][l] != '='); i++); + if (__environ[i]) return __environ[i] + l+1; + return NULL; +} diff --git a/05/musl-final/src/env/putenv.c b/05/musl-final/src/env/putenv.c new file mode 100644 index 0000000..181a418 --- /dev/null +++ b/05/musl-final/src/env/putenv.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +extern char **__environ; +char **__env_map; + +int __putenv(char *s, int a) +{ + int i=0, j=0; + char *end = strchr(s, '='); + size_t l = end-s+1; + char **newenv = 0; + char **newmap = 0; + static char **oldenv; + + if (!end || l == 1) return -1; + for (; __environ[i] && memcmp(s, __environ[i], l); i++); + if (a) { + if (!__env_map) { + __env_map = calloc(2, sizeof(char *)); + if (__env_map) __env_map[0] = s; + } else { + for (; __env_map[j] && __env_map[j] != __environ[i]; j++); + if (!__env_map[j]) { + newmap = realloc(__env_map, sizeof(char *)*(j+2)); + if (newmap) { + __env_map = newmap; + __env_map[j] = s; + __env_map[j+1] = NULL; + } + } else { + free(__env_map[j]); + } + } + } + if (!__environ[i]) { + newenv = malloc(sizeof(char *)*(i+2)); + if (!newenv) { + if (a && __env_map) __env_map[j] = 0; + return -1; + } + memcpy(newenv, __environ, sizeof(char *)*i); + newenv[i] = s; + newenv[i+1] = 0; + __environ = newenv; + free(oldenv); + oldenv = __environ; + } + + __environ[i] = s; + return 0; +} + +int putenv(char *s) +{ + return __putenv(s, 0); +} diff --git a/05/musl-final/src/env/setenv.c b/05/musl-final/src/env/setenv.c new file mode 100644 index 0000000..03e165c --- /dev/null +++ b/05/musl-final/src/env/setenv.c @@ -0,0 +1,31 @@ +#include +#include +#include + +int __putenv(char *s, int a); + +int setenv(const char *var, const char *value, int overwrite) +{ + char *s; + int l1, l2; + + if (strchr(var, '=')) { + errno = EINVAL; + return -1; + } + if (!overwrite && getenv(var)) return 0; + + l1 = strlen(var); + l2 = strlen(value); + s = malloc(l1+l2+2); + memcpy(s, var, l1); + s[l1] = '='; + memcpy(s+l1+1, value, l2); + s[l1+l2+1] = 0; + if (__putenv(s, 1)) { + free(s); + errno = ENOMEM; + return -1; + } + return 0; +} diff --git a/05/musl-final/src/env/unsetenv.c b/05/musl-final/src/env/unsetenv.c new file mode 100644 index 0000000..7493d97 --- /dev/null +++ b/05/musl-final/src/env/unsetenv.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +extern char **__environ; +extern char **__env_map; + +int unsetenv(const char *name) +{ + int i, j; + size_t l = strlen(name); + + if (!*name || strchr(name, '=')) { + errno = EINVAL; + return -1; + } +again: + for (i=0; __environ[i] && (memcmp(name, __environ[i], l) || __environ[i][l] != '='); i++); + if (__environ[i]) { + if (__env_map) { + for (j=0; __env_map[j] && __env_map[j] != __environ[i]; j++); + free (__env_map[j]); + for (; __env_map[j]; j++) + __env_map[j] = __env_map[j+1]; + } + for (; __environ[i]; i++) + __environ[i] = __environ[i+1]; + goto again; + } + return 0; +} diff --git a/05/musl-final/src/errno/__errno_location.c b/05/musl-final/src/errno/__errno_location.c new file mode 100644 index 0000000..0a220b6 --- /dev/null +++ b/05/musl-final/src/errno/__errno_location.c @@ -0,0 +1,11 @@ +#include +#include "libc.h" + +#undef errno +int errno; + +int *__errno_location(void) +{ + if (libc.errno_location) return libc.errno_location(); + return &errno; +} diff --git a/05/musl-final/src/errno/__strerror.h b/05/musl-final/src/errno/__strerror.h new file mode 100644 index 0000000..00eaf93 --- /dev/null +++ b/05/musl-final/src/errno/__strerror.h @@ -0,0 +1,101 @@ +/* This file is sorted such that 'errors' which represent exceptional + * conditions under which a correct program may fail come first, followed + * by messages that indicate an incorrect program or system failure. The + * macro E() along with double-inclusion is used to ensure that ordering + * of the strings remains synchronized. */ + +E(EILSEQ, "Illegal byte sequence") +E(EDOM, "Argument outside domain") +E(ERANGE, "Result not representable") + +E(ENOTTY, "Not a tty") +E(EACCES, "Permission denied") +E(EPERM, "Operation not permitted") +E(ENOENT, "No such file or directory") +E(ESRCH, "No such process") +E(EEXIST, "File exists") + +E(EOVERFLOW, "Value too large for defined data type") +E(ENOSPC, "No space left on device") +E(ENOMEM, "Out of memory") + +E(EBUSY, "Device or resource busy") +E(EINTR, "Interrupted system call") +E(EAGAIN, "Operation would block") +E(ESPIPE, "Illegal seek") + +E(EXDEV, "Cross-device link") +E(EROFS, "Read-only file system") +E(ENOTEMPTY, "Directory not empty") + +E(ECONNRESET, "Connection reset by peer") +E(ETIMEDOUT, "Connection timed out") +E(ECONNREFUSED, "Connection refused") +E(EHOSTDOWN, "Host is down") +E(EHOSTUNREACH, "No route to host") +E(EADDRINUSE, "Address already in use") + +E(EPIPE, "Broken pipe") +E(EIO, "I/O error") +E(ENXIO, "No such device or address") +E(ENOTBLK, "Block device required") +E(ENODEV, "No such device") +E(ENOTDIR, "Not a directory") +E(EISDIR, "Is a directory") +E(ETXTBSY, "Text file busy") +E(ENOEXEC, "Exec format error") + +E(EINVAL, "Invalid argument") + +E(E2BIG, "Argument list too long") +E(ELOOP, "Too many levels of symbolic links") +E(ENAMETOOLONG, "Filename too long") +E(ENFILE, "File table overflow") +E(EMFILE, "Too many open files") +E(EBADF, "Bad file number") +E(ECHILD, "No child processes") +E(EFAULT, "Bad address") +E(EFBIG, "File too large") +E(EMLINK, "Too many links") +E(ENOLCK, "No record locks available") + +E(EDEADLK, "Resource deadlock would occur") +E(ENOSYS, "Function not supported") +E(ENOMSG, "No message of desired type") +E(EIDRM, "Identifier removed") +E(ENOSTR, "Device not a stream") +E(ENODATA, "No data available") +E(ETIME, "Timer expired") +E(ENOSR, "Out of streams resources") +E(ENOLINK, "Link has been severed") +E(EPROTO, "Protocol error") +E(EBADMSG, "Not a data message") +E(EBADFD, "File descriptor in bad state") +E(ENOTSOCK, "Socket operation on non-socket") +E(EDESTADDRREQ, "Destination address required") +E(EMSGSIZE, "Message too long") +E(EPROTOTYPE, "Protocol wrong type for socket") +E(ENOPROTOOPT, "Protocol not available") +E(EPROTONOSUPPORT,"Protocol not supported") +E(ESOCKTNOSUPPORT,"Socket type not supported") +E(EOPNOTSUPP, "Operation not supported on socket") +E(EPFNOSUPPORT, "Protocol family not supported") +E(EAFNOSUPPORT, "Address family not supported by protocol") +E(EADDRNOTAVAIL,"Cannot assign requested address") +E(ENETDOWN, "Network is down") +E(ENETUNREACH, "Network is unreachable") +E(ENETRESET, "Network dropped connection because of reset") +E(ECONNABORTED, "Software caused connection abort") +E(ENOBUFS, "No buffer space available") +E(EISCONN, "Socket is connected") +E(ENOTCONN, "Socket is not connected") +E(ESHUTDOWN, "Cannot send after socket shutdown") +E(EALREADY, "Operation already in progress") +E(EINPROGRESS, "Operation now in progress") +E(ESTALE, "Stale NFS file handle") +E(EREMOTEIO, "Remote I/O error") +E(EDQUOT, "Quota exceeded") +E(ENOMEDIUM, "No medium found") +E(EMEDIUMTYPE, "Wrong medium type") + +E(0, "Invalid error number") diff --git a/05/musl-final/src/errno/strerror.c b/05/musl-final/src/errno/strerror.c new file mode 100644 index 0000000..b8fbc6d --- /dev/null +++ b/05/musl-final/src/errno/strerror.c @@ -0,0 +1,22 @@ +#include +#include + +#define E(a,b) a, +static const unsigned char errid[] = { +#include "__strerror.h" +}; + +#undef E +#define E(a,b) b "\0" +static const char errmsg[] = +#include "__strerror.h" +; + +char *strerror(int e) +{ + const char *s; + int i; + for (i=0; errid[i] && errid[i] != e; i++); + for (s=errmsg; i; s++, i--) for (; *s; s++); + return (char *)s; +} diff --git a/05/musl-final/src/exit/_Exit.c b/05/musl-final/src/exit/_Exit.c new file mode 100644 index 0000000..8ef85a8 --- /dev/null +++ b/05/musl-final/src/exit/_Exit.c @@ -0,0 +1,9 @@ +#include +#define SYSCALL_NORETURN +#include "syscall.h" + +void _Exit(int ec) +{ + syscall1(__NR_exit_group, ec); + syscall1(__NR_exit, ec); +} diff --git a/05/musl-final/src/exit/abort.c b/05/musl-final/src/exit/abort.c new file mode 100644 index 0000000..9a1c3d4 --- /dev/null +++ b/05/musl-final/src/exit/abort.c @@ -0,0 +1,8 @@ +#include +#include + +void abort(void) +{ + raise(SIGABRT); + for (;;); +} diff --git a/05/musl-final/src/exit/assert.c b/05/musl-final/src/exit/assert.c new file mode 100644 index 0000000..e87442a --- /dev/null +++ b/05/musl-final/src/exit/assert.c @@ -0,0 +1,9 @@ +#include +#include + +void __assert_fail(const char *expr, const char *file, int line, const char *func) +{ + fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line); + fflush(NULL); + abort(); +} diff --git a/05/musl-final/src/exit/atexit.c b/05/musl-final/src/exit/atexit.c new file mode 100644 index 0000000..49c060e --- /dev/null +++ b/05/musl-final/src/exit/atexit.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include "libc.h" + +/* Ensure that at least 32 atexit handlers can be registered without malloc */ +#define COUNT 32 + +static struct fl +{ + struct fl *next; + void (*f[COUNT])(void); +} builtin, *head; + +static int run_atexit_functions(void) +{ + int i; + for (; head; head=head->next) { + for (i=COUNT-1; i>=0 && !head->f[i]; i--); + for (; i>=0; i--) head->f[i](); + } + return 0; +} + +int (*const __funcs_on_exit)(void) = run_atexit_functions; + +int atexit(void (*func)(void)) +{ + static int lock; + int i; + + /* Hook for atexit extensions */ + if (libc.atexit) return libc.atexit(func); + + LOCK(&lock); + + /* Defer initialization of head so it can be in BSS */ + if (!head) head = &builtin; + + /* If the current function list is full, add a new one */ + if (head->f[COUNT-1]) { + struct fl *new_fl = calloc(sizeof(struct fl), 1); + if (!new_fl) { + UNLOCK(&lock); + return -1; + } + new_fl->next = head; + head = new_fl; + } + + /* Append function to the list. */ + for (i=0; if[i]; i++); + head->f[i] = func; + + UNLOCK(&lock); + return 0; +} diff --git a/05/musl-final/src/exit/exit.c b/05/musl-final/src/exit/exit.c new file mode 100644 index 0000000..2e4fa68 --- /dev/null +++ b/05/musl-final/src/exit/exit.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include "libc.h" + +#if 0 +/* __overflow.c and atexit.c override these */ +static int (*const dummy)() = 0; +weak_alias(dummy, __funcs_on_exit); +weak_alias(dummy, __fflush_on_exit); +#else +extern int (*const __funcs_on_exit)(void); +extern int (*const __fflush_on_exit)(FILE *); +#endif + +void exit(int code) +{ + static int lock; + + /* If more than one thread calls exit, hang until _Exit ends it all */ + LOCK(&lock); + + /* Only do atexit & stdio flush if they were actually used */ + if (__funcs_on_exit) __funcs_on_exit(); + if (__fflush_on_exit) __fflush_on_exit(0); + + /* Destructor s**t is kept separate from atexit to avoid bloat */ + if (libc.fini) libc.fini(); + if (libc.ldso_fini) libc.ldso_fini(); + + _Exit(code); + for(;;); +} diff --git a/05/musl-final/src/fcntl/creat.c b/05/musl-final/src/fcntl/creat.c new file mode 100644 index 0000000..be05faa --- /dev/null +++ b/05/musl-final/src/fcntl/creat.c @@ -0,0 +1,9 @@ +#include +#include "libc.h" + +int creat(const char *filename, mode_t mode) +{ + return open(filename, O_CREAT|O_WRONLY|O_TRUNC, mode); +} + +LFS64(creat); diff --git a/05/musl-final/src/fcntl/fcntl.c b/05/musl-final/src/fcntl/fcntl.c new file mode 100644 index 0000000..464dbf0 --- /dev/null +++ b/05/musl-final/src/fcntl/fcntl.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include "syscall.h" +#include "libc.h" + +int fcntl(int fd, int cmd, ...) +{ + int r; + long arg; + va_list ap; + va_start(ap, cmd); + arg = va_arg(ap, long); + va_end(ap); + if (cmd == F_SETFL) arg |= O_LARGEFILE; + if (cmd == F_SETLKW) CANCELPT_BEGIN; + r = __syscall_fcntl(fd, cmd, arg); + if (cmd == F_SETLKW) CANCELPT_END; + return r; +} + +LFS64(fcntl); diff --git a/05/musl-final/src/fcntl/open.c b/05/musl-final/src/fcntl/open.c new file mode 100644 index 0000000..4c1a591 --- /dev/null +++ b/05/musl-final/src/fcntl/open.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include "syscall.h" +#include "libc.h" + +int open(const char *filename, int flags, ...) +{ + int r; + mode_t mode; + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + CANCELPT_BEGIN; + r = __syscall_open(filename, flags, mode); + CANCELPT_END; + return r; +} + +LFS64(open); diff --git a/05/musl-final/src/fcntl/openat.c b/05/musl-final/src/fcntl/openat.c new file mode 100644 index 0000000..eefa090 --- /dev/null +++ b/05/musl-final/src/fcntl/openat.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include "syscall.h" +#include "libc.h" + +int openat(int fd, const char *filename, int flags, ...) +{ + int r; + mode_t mode; + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + CANCELPT_BEGIN; + r = syscall4(__NR_openat, fd, (long)filename, flags|O_LARGEFILE, mode); + CANCELPT_END; + return r; +} + +LFS64(openat); diff --git a/05/musl-final/src/internal/futex.h b/05/musl-final/src/internal/futex.h new file mode 100644 index 0000000..c045382 --- /dev/null +++ b/05/musl-final/src/internal/futex.h @@ -0,0 +1,16 @@ +#ifndef _INTERNAL_FUTEX_H +#define _INTERNAL_FUTEX_H + +#define FUTEX_WAIT 0 +#define FUTEX_WAKE 1 +#define FUTEX_FD 2 +#define FUTEX_REQUEUE 3 +#define FUTEX_CMP_REQUEUE 4 +#define FUTEX_WAKE_OP 5 +#define FUTEX_LOCK_PI 6 +#define FUTEX_UNLOCK_PI 7 +#define FUTEX_TRYLOCK_PI 8 + +int __futex(volatile int *, int, int, void *); + +#endif diff --git a/05/musl-final/src/internal/libc.c b/05/musl-final/src/internal/libc.c new file mode 100644 index 0000000..5e8e9d9 --- /dev/null +++ b/05/musl-final/src/internal/libc.c @@ -0,0 +1,3 @@ +#include "libc.h" + +struct libc libc; diff --git a/05/musl-final/src/internal/libc.h b/05/musl-final/src/internal/libc.h new file mode 100644 index 0000000..e353f36 --- /dev/null +++ b/05/musl-final/src/internal/libc.h @@ -0,0 +1,43 @@ +#ifndef LIBC_H +#define LIBC_H + +#include +#include + +#define libc __libc +extern struct libc { + void (*lock)(volatile int *); + void (*cancelpt)(int); + int (*atexit)(void (*)(void)); + void (*fini)(void); + void (*ldso_fini)(void); + int *(*errno_location)(void); + volatile int threads_minus_1; + int (*rsyscall)(int, long, long, long, long, long, long); + void (**tsd_keys)(void *); +} libc; + + +/* Designed to avoid any overhead in non-threaded processes */ +void __lock(volatile int *); +#define LOCK(x) (libc.threads_minus_1 ? (__lock(x),1) : ((void)(x),1)) +#define UNLOCK(x) (*(x)=0) +#define CANCELPT(x) (libc.cancelpt ? libc.cancelpt((x)),0 : (void)(x),0) +#define CANCELPT_BEGIN CANCELPT(1) +#define CANCELPT_END CANCELPT(0) + +extern char **__environ; +#define environ __environ + +#undef weak_alias +#define weak_alias(old, new) \ + extern __typeof(old) new __attribute__((weak, alias(#old))) + +#undef LFS64_2 +//#define LFS64_2(x, y) weak_alias(x, y) +#define LFS64_2(x, y) extern __typeof(x) y + +#undef LFS64 +#define LFS64(x) LFS64_2(x, x##64) + +#endif diff --git a/05/musl-final/src/internal/locale_impl.h b/05/musl-final/src/internal/locale_impl.h new file mode 100644 index 0000000..c268124 --- /dev/null +++ b/05/musl-final/src/internal/locale_impl.h @@ -0,0 +1,5 @@ +#include + +struct __locale { + int dummy; +}; diff --git a/05/musl-final/src/internal/pthread_impl.h b/05/musl-final/src/internal/pthread_impl.h new file mode 100644 index 0000000..615af70 --- /dev/null +++ b/05/musl-final/src/internal/pthread_impl.h @@ -0,0 +1,79 @@ +#ifndef _PTHREAD_IMPL_H +#define _PTHREAD_IMPL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" +#include "syscall.h" +#include "atomic.h" +#include "futex.h" + +#define pthread __pthread + +struct pthread { + struct pthread *self; + unsigned long tlsdesc[4]; + pid_t tid, pid; + int tsd_used, errno_val, *errno_ptr; + volatile int canceldisable, cancelasync, cancelpoint, cancel; + unsigned char *map_base; + size_t map_size; + void *start_arg; + void *(*start)(void *); + void *result; + jmp_buf exit_jmp_buf; + int detached; + int exitlock; + struct __ptcb *cancelbuf; + void **tsd; + pthread_attr_t attr; +}; + +#define __SU (sizeof(size_t)/sizeof(int)) + +#define _a_stacksize __u.__s[0] +#define _a_guardsize __u.__s[1] +#define _a_detach __u.__i[2*__SU+0] +#define _m_type __u.__i[0] +#define _m_lock __u.__i[1] +#define _m_waiters __u.__i[2] +#define _m_owner __u.__i[3] +#define _c_block __u.__i[0] +#define _rw_wrlock __u.__i[0] +#define _rw_readers __u.__i[1] +#define _rw_waiters __u.__i[2] +#define _rw_owner __u.__i[3] +#define _b_count __u.__i[0] +#define _b_limit __u.__i[1] +#define _b_left __u.__i[2] +#define _b_waiters __u.__i[3] + +#include "pthread_arch.h" + +#define SIGCANCEL 32 +#define SIGSYSCALL 33 +#define SIGTIMER 32 /* ?? */ + +int __set_thread_area(void *); +int __libc_sigaction(int, const struct sigaction *, struct sigaction *); +int __libc_sigprocmask(int, const sigset_t *, sigset_t *); +void __lock(volatile int *); +void __unmapself(void *, size_t); + +int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int); +void __wait(volatile int *, volatile int *, int, int); +void __wake(volatile int *, int, int); + +#define DEFAULT_STACK_SIZE (16384-PAGE_SIZE) +#define DEFAULT_GUARD_SIZE PAGE_SIZE + +#endif diff --git a/05/musl-final/src/internal/stdio_impl.h b/05/musl-final/src/internal/stdio_impl.h new file mode 100644 index 0000000..1e9159f --- /dev/null +++ b/05/musl-final/src/internal/stdio_impl.h @@ -0,0 +1,100 @@ +#ifndef _STDIO_IMPL_H +#define _STDIO_IMPL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "syscall.h" +#include "libc.h" + +#define UNGET 4 + +#define FLOCK(f) LOCK(&f->lock) +#define FUNLOCK(f) UNLOCK(&f->lock) + +#define F_PERM 1 +#define F_NORD 4 +#define F_NOWR 8 +#define F_EOF 16 +#define F_ERR 32 + +struct __FILE_s { + unsigned flags; + unsigned char *rpos, *rstop; + unsigned char *rend, *wend; + unsigned char *wpos, *wstop; + unsigned char *wbase; + unsigned char *dummy01[3]; + unsigned char *buf; + size_t buf_size; + FILE *prev, *next; + int fd; + int pipe_pid; + long dummy2; + short dummy3; + char dummy4; + signed char lbf; + int lock; + int lockcount; + void *owner; + off_t off; + int (*flush)(FILE *); + void **wide_data; /* must be NULL */ + size_t (*read)(FILE *, unsigned char *, size_t); + size_t (*write)(FILE *, const unsigned char *, size_t); + off_t (*seek)(FILE *, off_t, int); + int mode; + int (*close)(FILE *); +}; + +size_t __stdio_read(FILE *, unsigned char *, size_t); +size_t __stdio_write(FILE *, const unsigned char *, size_t); +off_t __stdio_seek(FILE *, off_t, int); +int __stdio_close(FILE *); + +int __overflow(FILE *, int); +int __oflow(FILE *); +int __uflow(FILE *); +int __underflow(FILE *); + +int __fseeko(FILE *, off_t, int); +int __fseeko_unlocked(FILE *, off_t, int); +off_t __ftello(FILE *); +off_t __ftello_unlocked(FILE *); +size_t __fwritex(const unsigned char *, size_t, FILE *); +int __putc_unlocked(int, FILE *); + +FILE *__fdopen(int, const char *); + +extern struct ofl +{ + FILE *head; + int lock; +} __ofl; + +#define OFLLOCK() LOCK(&__ofl.lock) +#define OFLUNLOCK() UNLOCK(&__ofl.lock) +#define ofl_head (__ofl.head) + +#define feof(f) ((f)->flags & F_EOF) +#define ferror(f) ((f)->flags & F_ERR) + +/* Caller-allocated FILE * operations */ +FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t); +int __fclose_ca(FILE *); + +#endif diff --git a/05/musl-final/src/internal/syscall.c b/05/musl-final/src/internal/syscall.c new file mode 100644 index 0000000..4f159e0 --- /dev/null +++ b/05/musl-final/src/internal/syscall.c @@ -0,0 +1,11 @@ +#include +#include + +long __syscall_ret(unsigned long r) +{ + if (r >= (unsigned long)-1 - 4096) { + errno = -(long)r; + return -1; + } + return (long)r; +} diff --git a/05/musl-final/src/ipc/ftok.c b/05/musl-final/src/ipc/ftok.c new file mode 100644 index 0000000..cd6002e --- /dev/null +++ b/05/musl-final/src/ipc/ftok.c @@ -0,0 +1,10 @@ +#include +#include + +key_t ftok(const char *path, int id) +{ + struct stat st; + if (stat(path, &st) < 0) return -1; + + return ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | ((id & 0xff) << 24)); +} diff --git a/05/musl-final/src/ipc/ipc.h b/05/musl-final/src/ipc/ipc.h new file mode 100644 index 0000000..9edd5ec --- /dev/null +++ b/05/musl-final/src/ipc/ipc.h @@ -0,0 +1,13 @@ +#define IPCOP_semop 1 +#define IPCOP_semget 2 +#define IPCOP_semctl 3 +#define IPCOP_msgsnd 11 +#define IPCOP_msgrcv 12 +#define IPCOP_msgget 13 +#define IPCOP_msgctl 14 +#define IPCOP_shmat 21 +#define IPCOP_shmdt 22 +#define IPCOP_shmget 23 +#define IPCOP_shmctl 24 + +#define IPC_MODERN 0x100 diff --git a/05/musl-final/src/ipc/semctl.c b/05/musl-final/src/ipc/semctl.c new file mode 100644 index 0000000..7ada116 --- /dev/null +++ b/05/musl-final/src/ipc/semctl.c @@ -0,0 +1,18 @@ +#include +#include +#include "syscall.h" +#include "ipc.h" + +int semctl(int id, int num, int cmd, ...) +{ + long arg; + va_list ap; + va_start(ap, cmd); + arg = va_arg(ap, long); + va_end(ap); +#ifdef __NR_semctl + return syscall4(__NR_semctl, id, num, cmd, arg); +#else + return syscall5(__NR_ipc, IPCOP_semctl, id, num, cmd | 0x100, (long)&arg); +#endif +} diff --git a/05/musl-final/src/ipc/semget.c b/05/musl-final/src/ipc/semget.c new file mode 100644 index 0000000..2dcf6ea --- /dev/null +++ b/05/musl-final/src/ipc/semget.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "ipc.h" + +int semget(key_t key, int n, int fl) +{ +#ifdef __NR_semget + return syscall3(__NR_semget, key, n, fl); +#else + return syscall4(__NR_ipc, IPCOP_semget, key, n, fl); +#endif +} diff --git a/05/musl-final/src/ipc/semop.c b/05/musl-final/src/ipc/semop.c new file mode 100644 index 0000000..48d8a65 --- /dev/null +++ b/05/musl-final/src/ipc/semop.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "ipc.h" + +int semop(int id, struct sembuf *buf, size_t n) +{ +#ifdef __NR_semop + return syscall3(__NR_semop, id, (long)buf, n); +#else + return syscall5(__NR_ipc, IPCOP_semop, id, n, 0, (long)buf); +#endif +} diff --git a/05/musl-final/src/ipc/shmat.c b/05/musl-final/src/ipc/shmat.c new file mode 100644 index 0000000..98a2cd4 --- /dev/null +++ b/05/musl-final/src/ipc/shmat.c @@ -0,0 +1,17 @@ +#include +#include "syscall.h" +#include "ipc.h" + +#ifdef __NR_shmat +void *shmat(int id, const void *addr, int flag) +{ + return (void *)syscall3(__NR_shmat, id, (long)addr, flag); +} +#else +void *shmat(int id, const void *addr, int flag) +{ + unsigned long ret; + ret = syscall5(__NR_ipc, IPCOP_shmat, id, flag, (long)&addr, (long)addr); + return (ret > -(unsigned long)SHMLBA) ? (void *)ret : (void *)addr; +} +#endif diff --git a/05/musl-final/src/ipc/shmctl.c b/05/musl-final/src/ipc/shmctl.c new file mode 100644 index 0000000..da357fa --- /dev/null +++ b/05/musl-final/src/ipc/shmctl.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "ipc.h" + +int shmctl(int id, int cmd, struct shmid_ds *buf) +{ +#ifdef __NR_shmctl + return syscall3(__NR_shmctl, id, cmd, (long)buf); +#else + return syscall4(__NR_ipc, IPCOP_shmctl, id, cmd | IPC_MODERN, (long)buf); +#endif +} diff --git a/05/musl-final/src/ipc/shmdt.c b/05/musl-final/src/ipc/shmdt.c new file mode 100644 index 0000000..e04188f --- /dev/null +++ b/05/musl-final/src/ipc/shmdt.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "ipc.h" + +int shmdt(const void *addr) +{ +#ifdef __NR_shmdt + return syscall1(__NR_shmdt, (long)addr); +#else + return syscall2(__NR_ipc, IPCOP_shmdt, (long)addr); +#endif +} diff --git a/05/musl-final/src/ipc/shmget.c b/05/musl-final/src/ipc/shmget.c new file mode 100644 index 0000000..86e254a --- /dev/null +++ b/05/musl-final/src/ipc/shmget.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "ipc.h" + +int shmget(key_t key, size_t size, int flag) +{ +#ifdef __NR_shmget + return syscall3(__NR_shmget, key, size, flag); +#else + return syscall4(__NR_ipc, IPCOP_shmget, key, size, flag); +#endif +} diff --git a/05/musl-final/src/linux/brk.c b/05/musl-final/src/linux/brk.c new file mode 100644 index 0000000..3c2982c --- /dev/null +++ b/05/musl-final/src/linux/brk.c @@ -0,0 +1,6 @@ +#include "syscall.h" + +int brk(void *end) +{ + return -(syscall1(__NR_brk, (long)end) == -1); +} diff --git a/05/musl-final/src/linux/chroot.c b/05/musl-final/src/linux/chroot.c new file mode 100644 index 0000000..81363a6 --- /dev/null +++ b/05/musl-final/src/linux/chroot.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int chroot(const char *path) +{ + return syscall1(__NR_chroot, (long)path); +} diff --git a/05/musl-final/src/linux/daemon.c b/05/musl-final/src/linux/daemon.c new file mode 100644 index 0000000..afb8019 --- /dev/null +++ b/05/musl-final/src/linux/daemon.c @@ -0,0 +1,32 @@ +#include +#include + +int daemon(int nochdir, int noclose) +{ + if (!nochdir && chdir("/")) + return -1; + if (!noclose) { + int fd, failed = 0; + if ((fd = open("/dev/null", O_RDWR)) < 0) return -1; + if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0) + failed++; + if (fd > 2) close(fd); + if (failed) return -1; + } + + switch(fork()) { + case 0: break; + case -1: return -1; + default: _exit(0); + } + + if (setsid() < 0) return -1; + + switch(fork()) { + case 0: break; + case -1: return -1; + default: _exit(0); + } + + return 0; +} diff --git a/05/musl-final/src/linux/epoll_create.c b/05/musl-final/src/linux/epoll_create.c new file mode 100644 index 0000000..c9dea8c --- /dev/null +++ b/05/musl-final/src/linux/epoll_create.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int epoll_create(int size) +{ + return syscall1(__NR_epoll_create, size); +} diff --git a/05/musl-final/src/linux/epoll_create1.c b/05/musl-final/src/linux/epoll_create1.c new file mode 100644 index 0000000..2e82e99 --- /dev/null +++ b/05/musl-final/src/linux/epoll_create1.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int epoll_create1(int flags) +{ + return syscall1(__NR_epoll_create1, flags); +} diff --git a/05/musl-final/src/linux/epoll_ctl.c b/05/musl-final/src/linux/epoll_ctl.c new file mode 100644 index 0000000..4214f40 --- /dev/null +++ b/05/musl-final/src/linux/epoll_ctl.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int epoll_ctl(int fd, int op, int fd2, struct epoll_event *ev) +{ + return syscall4(__NR_epoll_ctl, fd, op, fd2, (long)ev); +} diff --git a/05/musl-final/src/linux/epoll_pwait.c b/05/musl-final/src/linux/epoll_pwait.c new file mode 100644 index 0000000..5aaacba --- /dev/null +++ b/05/musl-final/src/linux/epoll_pwait.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int epoll_pwait(int fd, struct epoll_event *ev, int cnt, int to, const sigset_t *sigs) +{ + return syscall6(__NR_epoll_pwait, fd, (long)ev, cnt, to, (long)sigs, 8); +} diff --git a/05/musl-final/src/linux/epoll_wait.c b/05/musl-final/src/linux/epoll_wait.c new file mode 100644 index 0000000..8a68ebd --- /dev/null +++ b/05/musl-final/src/linux/epoll_wait.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int epoll_wait(int fd, struct epoll_event *ev, int cnt, int to) +{ + return syscall4(__NR_epoll_wait, fd, (long)ev, cnt, to); +} diff --git a/05/musl-final/src/linux/getdtablesize.c b/05/musl-final/src/linux/getdtablesize.c new file mode 100644 index 0000000..623a6af --- /dev/null +++ b/05/musl-final/src/linux/getdtablesize.c @@ -0,0 +1,9 @@ +#include +#include + +int getdtablesize(void) +{ + struct rlimit rl; + getrlimit(RLIMIT_NOFILE, &rl); + return rl.rlim_max < INT_MAX ? rl.rlim_max : INT_MAX; +} diff --git a/05/musl-final/src/linux/gethostid.c b/05/musl-final/src/linux/gethostid.c new file mode 100644 index 0000000..ea65611 --- /dev/null +++ b/05/musl-final/src/linux/gethostid.c @@ -0,0 +1,4 @@ +long gethostid() +{ + return 0; +} diff --git a/05/musl-final/src/linux/getopt_long.c b/05/musl-final/src/linux/getopt_long.c new file mode 100644 index 0000000..d80cd1b --- /dev/null +++ b/05/musl-final/src/linux/getopt_long.c @@ -0,0 +1,52 @@ +#define _GNU_SOURCE +#include +#include +#include + +static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) +{ + if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1; + if ((longonly && argv[optind][1]) || + (argv[optind][1] == '-' && argv[optind][2])) + { + int i; + for (i=0; longopts[i].name; i++) { + const char *name = longopts[i].name; + char *opt = argv[optind]+1; + if (*opt == '-') opt++; + while (*name && *name++ == *opt++); + if (*name || (*opt && *opt != '=')) continue; + if (*opt == '=') { + if (!longopts[i].has_arg) continue; + optarg = opt+1; + } else { + if (longopts[i].has_arg == required_argument) { + if (!(optarg = argv[++optind])) + return ':'; + } else optarg = NULL; + } + optind++; + if (idx) *idx = i; + if (longopts[i].flag) { + *longopts[i].flag = longopts[i].val; + return 0; + } + return longopts[i].val; + } + if (argv[optind][1] == '-') { + optind++; + return '?'; + } + } + return getopt(argc, argv, optstring); +} + +int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx) +{ + return __getopt_long(argc, argv, optstring, longopts, idx, 0); +} + +int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx) +{ + return __getopt_long(argc, argv, optstring, longopts, idx, 1); +} diff --git a/05/musl-final/src/linux/getpagesize.c b/05/musl-final/src/linux/getpagesize.c new file mode 100644 index 0000000..5ede652 --- /dev/null +++ b/05/musl-final/src/linux/getpagesize.c @@ -0,0 +1,7 @@ +#include +#include + +int getpagesize(void) +{ + return PAGE_SIZE; +} diff --git a/05/musl-final/src/linux/getpass.c b/05/musl-final/src/linux/getpass.c new file mode 100644 index 0000000..d439a2a --- /dev/null +++ b/05/musl-final/src/linux/getpass.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +char *getpass(const char *prompt) +{ + int fd; + struct termios s, t; + ssize_t l; + static char password[128]; + + if ((fd = open("/dev/tty", O_RDONLY|O_NOCTTY)) < 0) fd = 0; + + tcgetattr(fd, &t); + s = t; + t.c_lflag &= ~(ECHO|ISIG); + t.c_lflag |= ICANON; + t.c_iflag &= ~(INLCR|IGNCR); + t.c_iflag |= ICRNL; + tcsetattr(fd, TCSAFLUSH, &t); + tcdrain(fd); + + fputs(prompt, stderr); + fflush(stderr); + + l = read(fd, password, sizeof password); + if (l >= 0) { + if (l > 0 && password[l-1] == '\n') l--; + password[l] = 0; + } + + tcsetattr(fd, TCSAFLUSH, &s); + + if (fd > 2) close(fd); + + return password; +} diff --git a/05/musl-final/src/linux/initgroups.c b/05/musl-final/src/linux/initgroups.c new file mode 100644 index 0000000..ef9bc10 --- /dev/null +++ b/05/musl-final/src/linux/initgroups.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +int getgrouplist(const char *, gid_t, gid_t *, int *); +int setgroups(size_t, const gid_t *); + +int initgroups(const char *user, gid_t gid) +{ + gid_t groups[NGROUPS_MAX]; + int count; + if (getgrouplist(user, gid, groups, &count) < 0) return -1; + return setgroups(count, groups); +} diff --git a/05/musl-final/src/linux/klogctl.c b/05/musl-final/src/linux/klogctl.c new file mode 100644 index 0000000..976f29e --- /dev/null +++ b/05/musl-final/src/linux/klogctl.c @@ -0,0 +1,6 @@ +#include "syscall.h" + +int klogctl (int type, char *buf, int len) +{ + return syscall3(__NR_syslog, type, (long)buf, len); +} diff --git a/05/musl-final/src/linux/mntent.c b/05/musl-final/src/linux/mntent.c new file mode 100644 index 0000000..e373566 --- /dev/null +++ b/05/musl-final/src/linux/mntent.c @@ -0,0 +1,57 @@ +#include +#include +#include + +FILE *setmntent(const char *name, const char *mode) +{ + return fopen(name, mode); +} + +int endmntent(FILE *f) +{ + fclose(f); + return 1; +} + +struct mntent *getmntent(FILE *f) +{ + static char linebuf[256]; + static struct mntent mnt; + int cnt, n[8]; + + mnt.mnt_freq = 0; + mnt.mnt_passno = 0; + + do { + fgets(linebuf, sizeof linebuf, f); + if (feof(f)) return NULL; + cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d", + n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, + &mnt.mnt_freq, &mnt.mnt_passno); + } while (cnt >= 8 && linebuf[n[0]] != '#'); + + linebuf[n[1]] = 0; + linebuf[n[3]] = 0; + linebuf[n[5]] = 0; + linebuf[n[7]] = 0; + + mnt.mnt_fsname = linebuf+n[0]; + mnt.mnt_dir = linebuf+n[2]; + mnt.mnt_type = linebuf+n[4]; + mnt.mnt_opts = linebuf+n[6]; + + return &mnt; +} + +int addmntent(FILE *f, const struct mntent *mnt) +{ + fseek(f, 0, SEEK_END); + return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n", + mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts, + mnt->mnt_freq, mnt->mnt_passno) < 0; +} + +char *hasmntopt(const struct mntent *mnt, const char *opt) +{ + return strstr(mnt->mnt_opts, opt); +} diff --git a/05/musl-final/src/linux/mount.c b/05/musl-final/src/linux/mount.c new file mode 100644 index 0000000..8e3cc12 --- /dev/null +++ b/05/musl-final/src/linux/mount.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mount(const char *special, const char *dir, const char *fstype, unsigned long flags, const void *data) +{ + return syscall5(__NR_mount, (long)special, (long)dir, (long)fstype, flags, (long)data); +} diff --git a/05/musl-final/src/linux/prctl.c b/05/musl-final/src/linux/prctl.c new file mode 100644 index 0000000..d551683 --- /dev/null +++ b/05/musl-final/src/linux/prctl.c @@ -0,0 +1,13 @@ +#include +#include +#include "syscall.h" + +int prctl(int op, ...) +{ + unsigned long x[4]; + int i; + va_list ap; + va_start(ap, op); + for (i=0; i<4; i++) x[i] = va_arg(ap, unsigned long); + return syscall5(__NR_prctl, op, x[0], x[1], x[2], x[3]); +} diff --git a/05/musl-final/src/linux/reboot.c b/05/musl-final/src/linux/reboot.c new file mode 100644 index 0000000..68830d8 --- /dev/null +++ b/05/musl-final/src/linux/reboot.c @@ -0,0 +1,8 @@ +#include +#include + +int reboot(int type) +{ + errno = ENOSYS; + return -1; +} diff --git a/05/musl-final/src/linux/sbrk.c b/05/musl-final/src/linux/sbrk.c new file mode 100644 index 0000000..56f60d1 --- /dev/null +++ b/05/musl-final/src/linux/sbrk.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +void *sbrk(ptrdiff_t inc) +{ + return (void *)syscall1(__NR_brk, syscall1(__NR_brk, 0)+inc); +} diff --git a/05/musl-final/src/linux/sendfile.c b/05/musl-final/src/linux/sendfile.c new file mode 100644 index 0000000..bfbc40a --- /dev/null +++ b/05/musl-final/src/linux/sendfile.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t sendfile(int out_fd, int in_fd, off_t *ofs, size_t count) +{ + return syscall4(__NR_sendfile, out_fd, in_fd, (long)ofs, count); +} + +LFS64(sendfile); diff --git a/05/musl-final/src/linux/setgroups.c b/05/musl-final/src/linux/setgroups.c new file mode 100644 index 0000000..4d57801 --- /dev/null +++ b/05/musl-final/src/linux/setgroups.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int setgroups(int count, const gid_t list[]) +{ + return syscall2(__NR_setgroups, count, (long)list); +} diff --git a/05/musl-final/src/linux/sethostname.c b/05/musl-final/src/linux/sethostname.c new file mode 100644 index 0000000..c94325e --- /dev/null +++ b/05/musl-final/src/linux/sethostname.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int sethostname(const char *name, size_t len) +{ + return syscall2(__NR_sethostname, (long)name, len); +} diff --git a/05/musl-final/src/linux/settimeofday.c b/05/musl-final/src/linux/settimeofday.c new file mode 100644 index 0000000..bd7e410 --- /dev/null +++ b/05/musl-final/src/linux/settimeofday.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int settimeofday(const struct timeval *tv, void *tz) +{ + return syscall2(__NR_settimeofday, (long)tv, 0); +} diff --git a/05/musl-final/src/linux/signalfd.c b/05/musl-final/src/linux/signalfd.c new file mode 100644 index 0000000..ecda263 --- /dev/null +++ b/05/musl-final/src/linux/signalfd.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int signalfd(int fd, const sigset_t *sigs, int flags) +{ + return syscall3(__NR_signalfd, fd, (long)sigs, 8); +} diff --git a/05/musl-final/src/linux/stime.c b/05/musl-final/src/linux/stime.c new file mode 100644 index 0000000..ec3ba82 --- /dev/null +++ b/05/musl-final/src/linux/stime.c @@ -0,0 +1,7 @@ +#include + +int stime(time_t *t) +{ + struct timeval tv = { .tv_sec = *t, .tv_usec = 0 }; + return settimeofday(&tv, (void *)0); +} diff --git a/05/musl-final/src/linux/swapoff.c b/05/musl-final/src/linux/swapoff.c new file mode 100644 index 0000000..4f19823 --- /dev/null +++ b/05/musl-final/src/linux/swapoff.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int swapoff(const char *path) +{ + return syscall1(__NR_swapoff, (long)path); +} diff --git a/05/musl-final/src/linux/swapon.c b/05/musl-final/src/linux/swapon.c new file mode 100644 index 0000000..5c75247 --- /dev/null +++ b/05/musl-final/src/linux/swapon.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int swapon(const char *path, int flags) +{ + return syscall2(__NR_swapon, (long)path, flags); +} diff --git a/05/musl-final/src/linux/sysinfo.c b/05/musl-final/src/linux/sysinfo.c new file mode 100644 index 0000000..c61b7aa --- /dev/null +++ b/05/musl-final/src/linux/sysinfo.c @@ -0,0 +1,8 @@ +#include "syscall.h" + +struct sysinfo; + +int sysinfo(struct sysinfo *info) +{ + return syscall1(__NR_sysinfo, (long)info); +} diff --git a/05/musl-final/src/linux/umount.c b/05/musl-final/src/linux/umount.c new file mode 100644 index 0000000..f709b33 --- /dev/null +++ b/05/musl-final/src/linux/umount.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int umount(const char *special) +{ + return syscall2(__NR_umount2, (long)special, 0); +} diff --git a/05/musl-final/src/linux/umount2.c b/05/musl-final/src/linux/umount2.c new file mode 100644 index 0000000..ff0803c --- /dev/null +++ b/05/musl-final/src/linux/umount2.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int umount2(const char *special, int flags) +{ + return syscall2(__NR_umount2, (long)special, flags); +} diff --git a/05/musl-final/src/linux/utimes.c b/05/musl-final/src/linux/utimes.c new file mode 100644 index 0000000..59ee1a8 --- /dev/null +++ b/05/musl-final/src/linux/utimes.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" + +int utimes(const char *path, const struct timeval times[2]) +{ + long ktimes[2]; + if (times) { + ktimes[0] = times[0].tv_sec; + ktimes[1] = times[1].tv_sec; + } + return syscall2(__NR_utime, (long)path, times ? (long)ktimes : 0); +} diff --git a/05/musl-final/src/linux/wait3.c b/05/musl-final/src/linux/wait3.c new file mode 100644 index 0000000..d1d91b0 --- /dev/null +++ b/05/musl-final/src/linux/wait3.c @@ -0,0 +1,10 @@ +#include +#include +#include "syscall.h" + +pid_t wait4(pid_t, int *, int, struct rusage *); + +pid_t wait3(int *status, int options, struct rusage *usage) +{ + return wait4(-1, status, options, usage); +} diff --git a/05/musl-final/src/linux/wait4.c b/05/musl-final/src/linux/wait4.c new file mode 100644 index 0000000..5dd505a --- /dev/null +++ b/05/musl-final/src/linux/wait4.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include "syscall.h" + +pid_t wait4(pid_t pid, int *status, int options, struct rusage *usage) +{ + pid_t ret = syscall4(__NR_wait4, pid, (long)status, options, (long)usage); + /* Fixup kernel time_t... */ + if (usage) { + long kusage[4]; + memcpy(kusage, usage, sizeof kusage); + memmove((struct timeval *)usage + 2, (long *)usage + 4, sizeof *usage - 2*sizeof(struct timeval)); + usage->ru_utime = (struct timeval){ kusage[0], kusage[1] }; + usage->ru_stime = (struct timeval){ kusage[2], kusage[3] }; + } + return ret; +} diff --git a/05/musl-final/src/locale/catclose.c b/05/musl-final/src/locale/catclose.c new file mode 100644 index 0000000..02cd3e5 --- /dev/null +++ b/05/musl-final/src/locale/catclose.c @@ -0,0 +1,6 @@ +#include + +int catclose (nl_catd catd) +{ + return 0; +} diff --git a/05/musl-final/src/locale/catgets.c b/05/musl-final/src/locale/catgets.c new file mode 100644 index 0000000..bbee898 --- /dev/null +++ b/05/musl-final/src/locale/catgets.c @@ -0,0 +1,6 @@ +#include + +char *catgets (nl_catd catd, int set_id, int msg_id, const char *s) +{ + return (char *)s; +} diff --git a/05/musl-final/src/locale/catopen.c b/05/musl-final/src/locale/catopen.c new file mode 100644 index 0000000..4423c4d --- /dev/null +++ b/05/musl-final/src/locale/catopen.c @@ -0,0 +1,6 @@ +#include + +nl_catd catopen (const char *name, int oflag) +{ + return (nl_catd)-1; +} diff --git a/05/musl-final/src/locale/duplocale.c b/05/musl-final/src/locale/duplocale.c new file mode 100644 index 0000000..dd445d4 --- /dev/null +++ b/05/musl-final/src/locale/duplocale.c @@ -0,0 +1,11 @@ +#include +#include +#include "locale_impl.h" + +locale_t duplocale(locale_t old) +{ + locale_t new; + new = calloc(1, sizeof *new); + if (new) memcpy(new, old, sizeof *new); + return new; +} diff --git a/05/musl-final/src/locale/freelocale.c b/05/musl-final/src/locale/freelocale.c new file mode 100644 index 0000000..4e089f2 --- /dev/null +++ b/05/musl-final/src/locale/freelocale.c @@ -0,0 +1,7 @@ +#include +#include "locale_impl.h" + +void freelocale(locale_t l) +{ + free(l); +} diff --git a/05/musl-final/src/locale/iconv.c b/05/musl-final/src/locale/iconv.c new file mode 100644 index 0000000..ce2c364 --- /dev/null +++ b/05/musl-final/src/locale/iconv.c @@ -0,0 +1,568 @@ +#include +#include +#include +#include +#include +#include +#include + +#define UTF_32BE 000 +#define UTF_16LE 001 +#define UTF_16BE 002 +#define UTF_32LE 003 +#define UCS2BE 004 +#define UCS2LE 005 +#define WCHAR_T 007 + +#define US_ASCII 021 +#define UTF_8 022 +#define LATIN_9 024 +#define TIS_620 025 +#define JIS_0201 026 + +#define EUC 031 +#define EUC_TW 032 +#define SHIFT_JIS 033 +#define BIG5 034 +#define GBK 035 + +/* FIXME: these are not implemented yet + * EUC: A1-FE A1-FE + * GBK: 81-FE 40-7E,80-FE + * Big5: A1-FE 40-7E,A1-FE + */ + +/* Definitions of charmaps. Each charmap consists of: + * 1. Empty-string-terminated list of null-terminated aliases. + * 2. Special type code or bits per character. + * 3. Number of elided entries (128 for specials). + * 4. Character table (size determined by fields 2 and 3). */ + +static const unsigned char charmaps[] = +"utf8\0\0\022\x80" +"wchart\0\0\007\x80" + +"ucs2\0ucs2be\0\0\004\x80" +"ucs2le\0\0\005\x80" + +"utf16\0utf16be\0\0\002\x80" +"utf16le\0\0\001\x80" + +"ucs4\0ucs4be\0utf32\0utf32be\0\0\000\x80" +"ucs4le\0utf32le\0\0\003\x80" + +"ascii\0iso646\0usascii\0\0\021\x80" +"latin1\0iso88591\0\0\x09\x80" +"latin9\0iso885915\0\0\024\x80" +"tis620\0iso885911\0\0\025\x80" +"jis0201\0\0\026\x80" + +"iso88592\0\0\x0a\x21" +"\x04\x61\x1b\x14\x29\x3d\x69\x75\x0a\x2a" +"\x60\x79\x45\x56\x5e\xad\xf4\xb5\x17\x2c" +"\x05\x6d\x2b\x14\x2d\x3e\x6d\x75\x2c\x2e" +"\x61\x7d\x55\x96\x5e\xdd\xfa\xc5\x17\x55" +"\xc1\x08\x23\x10\x31\x39\x19\x74\x0c\x43" +"\xc9\x60\xb4\x8c\x46\xcd\x38\xe3\x10\x44" +"\x43\x1d\x35\x0d\x35\x50\x59\x73\x0d\x56" +"\x6e\x69\x03\x17\x37\xdd\x88\xf5\x4d\x55" +"\xe1\x88\x33\x10\x39\x3a\x1d\x74\x4e\x43" +"\xe9\x64\xb4\xce\x46\xed\xb8\xf3\x50\x44" +"\x44\x21\x35\x0f\x3d\x51\xd9\x73\x4f\x56" +"\x6f\xe9\x13\x17\x3f\xfd\x8c\x95\x2d" + +"iso88593\0\0\x0a\x21" +"\x26\x61\x3b\x0a\x29\x00\x90\x74\x0a\x2a" +"\x30\x79\xe5\x11\x4d\xad\x00\xb0\x17\x2c" +"\x27\xc9\x32\x0b\x2d\xb5\x94\x74\x0b\x2e" +"\x31\x7d\xf5\x51\x4d\xbd\x00\xc0\x17\x30" +"\xc1\x08\x03\x00\x31\x0a\x21\x74\x0c\x32" +"\xc9\x28\xb3\x0c\x33\xcd\x38\xf3\x0c\x00" +"\xd1\x48\x33\x0d\x35\x20\x59\x73\x0d\x47" +"\xd9\x68\xb3\x0d\x37\x6c\x71\xf5\x0d\x38" +"\xe1\x88\x03\x00\x39\x0b\x25\x74\x0e\x3a" +"\xe9\xa8\xb3\x0e\x3b\xed\xb8\xf3\x0e\x00" +"\xf1\xc8\x33\x0f\x3d\x21\xd9\x73\x4f\x47" +"\xf9\xe8\xb3\x0f\x3f\x6d\x75\x95\x2d" + +"iso88594\0\0\x0a\x21" +"\x04\xe1\x64\x15\x29\x28\xed\x74\x0a\x2a" +"\x60\x49\x24\x92\x59\xad\xf4\xf5\x0a\x2c" +"\x05\x6d\x7b\x15\x2d\x29\xf1\x74\x2c\x2e" +"\x61\x4d\x34\xd2\x59\x4a\xf9\xb5\x14\x40" +"\xc1\x08\x33\x0c\x31\xc5\x18\xe3\x12\x43" +"\xc9\x60\xb4\x8c\x45\xcd\x38\xa3\x12\x44" +"\x45\x31\x65\x13\x35\xd5\x58\x73\x0d\x36" +"\x72\x69\xb3\x0d\x37\x68\xa9\xf5\x4d\x40" +"\xe1\x88\x33\x0e\x39\xe5\x98\xf3\x52\x43" +"\xe9\x64\xb4\xce\x45\xed\xb8\xb3\x52\x44" +"\x46\x35\x75\x13\x3d\xf5\xd8\x73\x0f\x3e" +"\x73\xe9\xb3\x0f\x3f\x69\xad\x95\x2d" + +"iso88595\0\0\x0e\x21" +"\x01\x84\x00\x31\x40\x10\x10\x05\x84\x01" +"\x71\x40\x20\x10\x09\x84\x02\xb1\x40\x30" +"\x10\xad\x80\x03\xf1\x40\x40\x10\x11\x84" +"\x04\x31\x41\x50\x10\x15\x84\x05\x71\x41" +"\x60\x10\x19\x84\x06\xb1\x41\x70\x10\x1d" +"\x84\x07\xf1\x41\x80\x10\x21\x84\x08\x31" +"\x42\x90\x10\x25\x84\x09\x71\x42\xa0\x10" +"\x29\x84\x0a\xb1\x42\xb0\x10\x2d\x84\x0b" +"\xf1\x42\xc0\x10\x31\x84\x0c\x31\x43\xd0" +"\x10\x35\x84\x0d\x71\x43\xe0\x10\x39\x84" +"\x0e\xb1\x43\xf0\x10\x3d\x84\x0f\xf1\x43" +"\x00\x11\x41\x84\x10\x31\x44\x10\x11\x45" +"\x84\x11\x71\x44\x20\x11\x49\x84\x12\xb1" +"\x44\x30\x11\x4d\x84\x13\xf1\x44\x58\x84" +"\x51\x84\x14\x31\x45\x50\x11\x55\x84\x15" +"\x71\x45\x60\x11\x59\x84\x16\xb1\x45\x70" +"\x11\xa7\x80\x17\xf1\x45\x00" + +"iso88596\0\0\x0b\x21" +"\x00\x00\x00\x00\x48\x01\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x18\xdc\x0a\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\xc0\x86\x01\x00\x00" +"\x00\x7c\x18\x00\x21\x16\xf1\x88\x49\x5c" +"\x62\x13\x9f\x18\xc5\x29\x56\xf1\x8a\x59" +"\xdc\x62\x17\xbf\x18\xc6\x31\x96\xf1\x8c" +"\x69\x5c\x63\x1b\xdf\x18\xc7\x39\xd6\x31" +"\x00\x00\x00\x00\x00\x00\x00\xc8\x41\x16" +"\xf2\x90\x89\x5c\x64\x23\x1f\x19\xc9\x49" +"\x56\xf2\x92\x99\xdc\x64\x27\x3f\x19\xca" +"\x51\x96\x32\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00" + +"iso88597\0\0\x0e\x21" +"\x18\x60\x06\x38\x0a\xb0\x82\xaf\xa0\x29" +"\x70\x0a\xa0\x02\xa9\x80\xde\xb0\x0a\xb0" +"\x02\xad\x00\x00\x50\x01\xc2\x02\xb1\x80" +"\x2c\x30\x0b\x10\x0e\x85\x83\xe1\x70\x0b" +"\x20\x0e\x89\x83\xe2\xb0\x0b\x30\x0e\xbd" +"\x80\xe3\xf0\x38\x40\x0e\x91\x83\xe4\x30" +"\x39\x50\x0e\x95\x83\xe5\x70\x39\x60\x0e" +"\x99\x83\xe6\xb0\x39\x70\x0e\x9d\x83\xe7" +"\xf0\x39\x80\x0e\xa1\x03\x00\x30\x3a\x90" +"\x0e\xa5\x83\xe9\x70\x3a\xa0\x0e\xa9\x83" +"\xea\xb0\x3a\xb0\x0e\xad\x83\xeb\xf0\x3a" +"\xc0\x0e\xb1\x83\xec\x30\x3b\xd0\x0e\xb5" +"\x83\xed\x70\x3b\xe0\x0e\xb9\x83\xee\xb0" +"\x3b\xf0\x0e\xbd\x83\xef\xf0\x3b\x00\x0f" +"\xc1\x83\xf0\x30\x3c\x10\x0f\xc5\x83\xf1" +"\x70\x3c\x20\x0f\xc9\x83\xf2\xb0\x3c\x30" +"\x0f\xcd\x83\xf3\x00\x00\x00" + +"iso88598\0\0\x0e\x21" +"\x00\x80\x28\x30\x0a\x90\x02\xa5\x80\x29" +"\x70\x0a\xa0\x02\xa9\xc0\x35\xb0\x0a\xb0" +"\x02\xad\x80\x2b\xf0\x0a\xc0\x02\xb1\x80" +"\x2c\x30\x0b\xd0\x02\xb5\x80\x2d\x70\x0b" +"\xe0\x02\xb9\xc0\x3d\xb0\x0b\xf0\x02\xbd" +"\x80\x2f\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x70\x01" +"\x42\x17\xd1\x85\x74\x31\x5d\x50\x17\xd5" +"\x85\x75\x71\x5d\x60\x17\xd9\x85\x76\xb1" +"\x5d\x70\x17\xdd\x85\x77\xf1\x5d\x80\x17" +"\xe1\x85\x78\x31\x5e\x90\x17\xe5\x85\x79" +"\x71\x5e\xa0\x17\xe9\x85\x7a\x01\x00\x00" +"\x00\x0e\xe0\x03\x08\x00\x00" + +"iso88599\0\0\x09\x50" +"\x1e\xa3\x49\x9b\x46\xad\x9a\xb5\x6b\xd8" +"\xb2\x69\xdb\xc6\x0d\xa6\xd7\x6f\xe0\xc2" +"\x89\x1b\x47\xae\x9c\xb9\x73\xe8\xd2\xa9" +"\x5b\xc7\xae\x9d\xbb\x77\x1f\xe3\xc9\x9b" +"\x47\xaf\x9e\xbd\x7b\xf8\xf2\xe9\xdb\xc7" +"\x2f\xe6\xd7\x7f" + +"iso885910\0\0\x0e\x21" +"\x04\x81\x44\x20\x12\xa8\x04\x28\x81\x4d" +"\x70\x0a\xec\x04\x10\x01\x58\x60\x16\xf4" +"\x05\xad\x80\x5a\xa0\x14\xc0\x02\x05\xc1" +"\x44\x30\x12\xac\x04\x29\xc1\x4d\x70\x0b" +"\xf0\x04\x11\x41\x58\x70\x16\xf8\x05\x15" +"\xe0\x5a\xb0\x14\x00\x04\xc1\x80\x30\x30" +"\x0c\x10\x03\xc5\x80\x31\xe0\x12\x30\x04" +"\xc9\x00\x46\xb0\x0c\x58\x04\xcd\x80\x33" +"\xf0\x0c\x40\x03\x45\x01\x53\x30\x0d\x50" +"\x03\xd5\x80\x35\x80\x16\x60\x03\x72\x81" +"\x36\xb0\x0d\x70\x03\xdd\x80\x37\xf0\x0d" +"\x04\x04\xe1\x80\x38\x30\x0e\x90\x03\xe5" +"\x80\x39\xf0\x12\x34\x04\xe9\x40\x46\xb0" +"\x0e\x5c\x04\xed\x80\x3b\xf0\x0e\xc0\x03" +"\x46\x41\x53\x30\x0f\xd0\x03\xf5\x80\x3d" +"\x90\x16\xe0\x03\x73\x81\x3e\xb0\x0f\xf0" +"\x03\xfd\x80\x3f\x80\x13\x00" + +"iso885913\0\0\x0e\x21" +"\x1d\xa0\x28\x30\x0a\x90\x02\x1e\xa0\x29" +"\x70\x0a\x60\x03\xa9\x80\x55\xb0\x0a\xb0" +"\x02\xad\x80\x2b\x60\x0c\xc0\x02\xb1\x80" +"\x2c\x30\x0b\x70\x80\xb5\x80\x2d\x70\x0b" +"\xe0\x03\xb9\xc0\x55\xb0\x0b\xf0\x02\xbd" +"\x80\x2f\x60\x0e\x10\x04\x2e\x01\x40\x60" +"\x10\x10\x03\xc5\x00\x46\x20\x11\x30\x04" +"\xc9\x40\x5e\x60\x11\x88\x04\x36\x81\x4a" +"\xb0\x13\x80\x05\x43\x41\x51\x30\x0d\x30" +"\x05\xd5\x80\x35\x70\x0d\xc8\x05\x41\x81" +"\x56\xa0\x16\x70\x03\x7b\x41\x5f\xf0\x0d" +"\x14\x04\x2f\x41\x40\x70\x10\x90\x03\xe5" +"\x40\x46\x30\x11\x34\x04\xe9\x80\x5e\x70" +"\x11\x8c\x04\x37\xc1\x4a\xc0\x13\x84\x05" +"\x44\x81\x51\x30\x0f\x34\x05\xf5\x80\x3d" +"\x70\x0f\xcc\x05\x42\xc1\x56\xb0\x16\xf0" +"\x03\x7c\x81\x5f\x90\x01\x02" + +"iso885914\0\0\x0d\x21" +"\x02\x7e\xc0\x8f\x02\x85\xb0\x10\x14\xfc" +"\x29\x00\xf4\xa9\x40\xd0\x2f\x78\x79\xdf" +"\x0a\x5c\x01\x5e\xf0\xf0\x1f\x1e\x24\x84" +"\x04\x20\x1f\xe4\x6d\x81\x95\x0f\xf4\x57" +"\x7e\xd0\x83\xf9\x79\x4f\xe8\x0b\x7d\x98" +"\x07\x06\xc1\x40\x18\x0c\x03\x62\x50\x0c" +"\x8c\xc1\x31\x40\x06\xc9\x40\x19\x2c\x03" +"\x66\xd0\x0c\x9c\xc1\x33\xa0\x0b\xd1\x40" +"\x1a\x4c\x03\x6a\x50\x0d\xac\x81\x9a\xc7" +"\x06\xd9\x40\x1b\x6c\x03\x6e\xd0\x0d\xec" +"\xc2\x37\x00\x07\xe1\x40\x1c\x8c\x03\x72" +"\x50\x0e\xcc\xc1\x39\x40\x07\xe9\x40\x1d" +"\xac\x03\x76\xd0\x0e\xdc\xc1\x3b\xa8\x0b" +"\xf1\x40\x1e\xcc\x03\x7a\x50\x0f\xec\xc1" +"\x9a\xc7\x07\xf9\x40\x1f\xec\x03\x7e\xd0" +"\x0f\xee\xc2\x3f\x00" + +"iso885916\0\0\x0e\x21" +"\x04\x41\x41\x10\x14\xb0\x82\x1e\x20\x58" +"\x70\x0a\x84\x05\xa9\x00\x86\xb0\x0a\xe4" +"\x05\xad\x80\x5e\xb0\x17\xc0\x02\xb1\x00" +"\x43\x20\x14\xf4\x05\x1d\xa0\x2d\x70\x0b" +"\xf8\x05\x0d\x41\x86\xb0\x0b\x48\x05\x53" +"\x01\x5e\xc0\x17\x00\x03\xc1\x80\x30\x20" +"\x10\x10\x03\x06\x81\x31\x70\x0c\x20\x03" +"\xc9\x80\x32\xb0\x0c\x30\x03\xcd\x80\x33" +"\xf0\x0c\x40\x04\x43\x81\x34\x30\x0d\x50" +"\x03\x50\x81\x35\xa0\x15\xc0\x05\xd9\x80" +"\x36\xb0\x0d\x70\x03\x18\x81\x86\xf0\x0d" +"\x80\x03\xe1\x80\x38\x30\x10\x90\x03\x07" +"\x81\x39\x70\x0e\xa0\x03\xe9\x80\x3a\xb0" +"\x0e\xb0\x03\xed\x80\x3b\xf0\x0e\x44\x04" +"\x44\x81\x3c\x30\x0f\xd0\x03\x51\x81\x3d" +"\xb0\x15\xc4\x05\xf9\x80\x3e\xb0\x0f\xf0" +"\x03\x19\xc1\x86\xf0\x0f\x00" + +"windows1252\0\0\x0e\x00" +"\xac\x20\x00\xa0\x01\x4a\x06\x1e\xa0\x09" +"\x08\x02\x86\x80\xc6\x02\x0c\x08\x16\xe4" +"\x80\x52\x01\x00\xd0\x17\x00\x00\x00\x00" +"\x06\x98\x01\x72\x80\x1d\xa0\x08\x38\x01" +"\x52\x80\xdc\x82\x48\x18\x16\xe8\x80\x53" +"\x01\x00\xe0\x17\xe0\x05\xa0\x40\x28\x20" +"\x0a\x8c\x02\xa4\x40\x29\x60\x0a\x9c\x02" +"\xa8\x40\x2a\xa0\x0a\xac\x02\xac\x40\x2b" +"\xe0\x0a\xbc\x02\xb0\x40\x2c\x20\x0b\xcc" +"\x02\xb4\x40\x2d\x60\x0b\xdc\x02\xb8\x40" +"\x2e\xa0\x0b\xec\x02\xbc\x40\x2f\xe0\x0b" +"\xfc\x02\xc0\x40\x30\x20\x0c\x0c\x03\xc4" +"\x40\x31\x60\x0c\x1c\x03\xc8\x40\x32\xa0" +"\x0c\x2c\x03\xcc\x40\x33\xe0\x0c\x3c\x03" +"\xd0\x40\x34\x20\x0d\x4c\x03\xd4\x40\x35" +"\x60\x0d\x5c\x03\xd8\x40\x36\xa0\x0d\x6c" +"\x03\xdc\x40\x37\xe0\x0d\x7c\x03\xe0\x40" +"\x38\x20\x0e\x8c\x03\xe4\x40\x39\x60\x0e" +"\x9c\x03\xe8\x40\x3a\xa0\x0e\xac\x03\xec" +"\x40\x3b\xe0\x0e\xbc\x03\xf0\x40\x3c\x20" +"\x0f\xcc\x03\xf4\x40\x3d\x60\x0f\xdc\x03" +"\xf8\x40\x3e\xa0\x0f\xec\x03\xfc\x40\x3f" +"\xe0\x0f\xfc\x03" +; + + + +static int fuzzycmp(const char *a, const char *b) +{ + for (; *a && *b; a++, b++) { + while (*a && (*a|32U)-'a'>26 && *a-'0'>10U) a++; + if ((*a|32U) != *b) return 1; + } + return *a != *b; +} + +static size_t find_charmap(const char *name) +{ + const unsigned char *s; + for (s=charmaps; *s; ) { + if (!fuzzycmp(name, s)) { + for (; *s; s+=strlen(s)+1); + return s+1-charmaps; + } + s += strlen(s)+1; + if (!*s) s += ((128-s[2])*s[1]+7)/8 + 3; + } + return -1; +} + +iconv_t iconv_open(const char *to, const char *from) +{ + size_t f, t; + + if ((t = find_charmap(to)) < 0 || (f = find_charmap(from)) < 0) { + errno = EINVAL; + return (iconv_t)-1; + } + + return (void *)(f<<16 | t); +} + +int iconv_close(iconv_t cd) +{ + return 0; +} + +static unsigned get_16(const unsigned char *s, int e) +{ + e &= 1; + return s[e]<<8 | s[1-e]; +} + +static void put_16(unsigned char *s, unsigned c, int e) +{ + e &= 1; + s[e] = c>>8; + s[1-e] = c; +} + +static unsigned get_32(const unsigned char *s, int e) +{ + return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3]; +} + +static void put_32(unsigned char *s, unsigned c, int e) +{ + s[e^0] = c>>24; + s[e^1] = c>>16; + s[e^2] = c>>8; + s[e^3] = c; +} + + + +#define GET_MAPPING(m, i, n) ( (1<<(n))-1 & ( \ + (m)[(i)*(n)/8] >> ((n)%8*(i)%8) | \ + (m)[(i)*(n)/8+1] << 8-((n)%8*(i)%8) | \ + (m)[(i)*(n)/8+2] << 16-((n)%8*(i)%8) ) ) + +static unsigned get_mapping(const unsigned char *m, unsigned c, unsigned n) +{ + switch (n) { + default: + case 9: return m[c*9/8]>>c%8 | m[c*9/8+1]<<8-c%8 & (1<>2*c%8 | m[c*10/8+1]<<8-2*c%8 & (1< +size_t iconv(iconv_t cd0, char **in, size_t *inb, char **out, size_t *outb) +{ + size_t x=0; + unsigned long cd = (unsigned long)cd0; + unsigned to = cd & 0xffff; + unsigned from = cd >> 16; + const unsigned char *map = charmaps+from+2; + const unsigned char *tomap = charmaps+to+2; + mbstate_t st = {0}; + wchar_t wc; + unsigned c, d; + size_t k, l; + int err; + unsigned elide = map[-1] + 128; + unsigned toelide = tomap[-1] + 128; + unsigned char type = map[-2]; + unsigned char totype = tomap[-2]; + + if (!in || !*in || !*inb) return 0; + + for (; *inb; *in+=l, *inb-=l) { + c = *(unsigned char *)*in; + l = 1; + if (type < 8 || c >= 0x80) switch (type) { + case UTF_8: + l = mbrtowc_utf8(&wc, *in, *inb, &st); + if (!l) l++; + else if (l == (size_t)-1) goto ilseq; + else if (l == (size_t)-2) goto starved; + c = wc; + break; + case LATIN_9: + if ((unsigned)c - 0xa4 <= 0xbe - 0xa4) { + static const unsigned char map[] = { + 0, 0x60, 0, 0x61, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0x7d, 0, 0, 0, 0x7e, 0, 0, 0, + 0x52, 0x53, 0x78 + }; + if (c == 0xa4) c = 0x20ac; + else if (map[c-0xa5]) c = 0x100 | map[c-0xa5]; + } + break; + case TIS_620: + if (c >= 0xa1) c += 0x0e01-0xa1; + break; + case JIS_0201: + if (c >= 0xa1) + if (c <= 0xdf) c += 0xff61-0xa1; + else goto ilseq; + break; + case 9: case 10: case 11: case 13: case 14: + if (c < elide) break; + c = get_mapping(map, c-elide, type); + if (!c) { + case US_ASCII: + goto ilseq; + } + break; + case WCHAR_T: + l = sizeof(wchar_t); + if (*inb < l) goto starved; + c = *(wchar_t *)*in; + if (0) { + case UTF_32BE: + case UTF_32LE: + l = 4; + if (*inb < 4) goto starved; + c = get_32(*in, type); + } + if (c-0xd800u < 0x800u || c >= 0x110000u) goto ilseq; + break; + case UCS2BE: + case UCS2LE: + case UTF_16BE: + case UTF_16LE: + l = 2; + if (*inb < 2) goto starved; + c = get_16(*in, type); + if ((unsigned)(c-0xdc00) < 0x400) goto ilseq; + if ((unsigned)(c-0xd800) < 0x400) { + if (type-UCS2BE < 2U) goto ilseq; + l = 4; + if (*inb < 4) goto starved; + d = get_16(*in + 2, from); + if ((unsigned)(c-0xdc00) >= 0x400) goto ilseq; + c = ((c-0xd800)<<10) | (d-0xdc00); + } + break; + } + + switch (totype) { + case WCHAR_T: + if (*outb < sizeof(wchar_t)) goto toobig; + *(wchar_t *)*out = c; + *out += sizeof(wchar_t); + *outb -= sizeof(wchar_t); + break; + case UTF_8: + if (*outb < 4) { + char tmp[4]; + k = wctomb_utf8(tmp, c); + if (*outb < k) goto toobig; + memcpy(*out, tmp, k); + } else k = wctomb_utf8(*out, c); + *out += k; + *outb -= k; + break; + case TIS_620: + if (c-0xe01u <= 0xff-0xa1) + c -= 0xe01-0xa1; + else if (c >= 0xa1) + goto ascii; + goto revout; + case JIS_0201: + if (c-0xff61u <= 0xdf-0xa1) + c -= 0xff61-0xa1; + else if (c >= 0xa1) + goto ascii; + goto revout; + case LATIN_9: + if (c == 0x20ac) { + c=0xa4; + } else if (c-0x150u<=0x12 && (1<0x100 || + c-0xa5u<=0xbeu-0xa5 + && (1< 0x7f) x++, c='*'; + case 9: case 10: case 11: case 13: case 14: + if (*outb < 1) goto toobig; + if (c < toelide) { + revout: + *(*out)++ = c; + *outb -= 1; + break; + } + for (d=0; d<256-toelide; d++) { + if (c == get_mapping(tomap, d, totype)) { + c = d + toelide; + goto revout; + } + } + x++; + c = '*'; + goto revout; + case UCS2BE: + case UCS2LE: + case UTF_16BE: + case UTF_16LE: + if (c < 0x10000) { + if (*outb < 2) goto toobig; + put_16(*out, c, totype); + *out += 2; + *outb -= 2; + break; + } + if (type-UCS2BE < 2U) goto ilseq; + if (*outb < 4) goto toobig; + put_16(*out, (c>>10)|0xd800, totype); + put_16(*out + 2, (c&0x3ff)|0xdc00, totype); + *out += 4; + *outb -= 4; + break; + case UTF_32BE: + case UTF_32LE: + if (*outb < 4) goto toobig; + put_32(*out, c, totype); + *out += 4; + *outb -= 4; + break; + } + } + return x; +ilseq: + err = EILSEQ; + x = -1; + goto end; +toobig: + err = E2BIG; + goto end; +starved: + err = EINVAL; +end: + errno = err; + return x; +} diff --git a/05/musl-final/src/locale/intl.c b/05/musl-final/src/locale/intl.c new file mode 100644 index 0000000..964f7da --- /dev/null +++ b/05/musl-final/src/locale/intl.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +char *gettext(const char *msgid) +{ + return (char *) msgid; +} + +char *dgettext(const char *domainname, const char *msgid) +{ + return (char *) msgid; +} + +char *dcgettext(const char *domainname, const char *msgid, int category) +{ + return (char *) msgid; +} + +char *ngettext(const char *msgid1, const char *msgid2, unsigned long int n) +{ + return (char *) ((n == 1) ? msgid1 : msgid2); +} + +char *dngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n) +{ + return (char *) ((n == 1) ? msgid1 : msgid2); +} + +char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category) +{ + return (char *) ((n == 1) ? msgid1 : msgid2); +} + +char *textdomain(const char *domainname) +{ + static const char default_str[] = "messages"; + + if (domainname && *domainname && strcmp(domainname, default_str)) { + errno = EINVAL; + return NULL; + } + return (char *) default_str; +} + +char *bindtextdomain(const char *domainname, const char *dirname) +{ + static const char dir[] = "/"; + + if (!domainname || !*domainname + || (dirname && ((dirname[0] != '/') || dirname[1])) + ) { + errno = EINVAL; + return NULL; + } + + return (char *) dir; +} + +char *bind_textdomain_codeset(const char *domainname, const char *codeset) +{ + if (!domainname || !*domainname || (codeset && strcasecmp(codeset, "UTF-8"))) { + errno = EINVAL; + } + return NULL; +} diff --git a/05/musl-final/src/locale/isalnum_l.c b/05/musl-final/src/locale/isalnum_l.c new file mode 100644 index 0000000..b8a6eef --- /dev/null +++ b/05/musl-final/src/locale/isalnum_l.c @@ -0,0 +1,6 @@ +#include + +int isalnum_l(int c, locale_t l) +{ + return isalnum(c); +} diff --git a/05/musl-final/src/locale/isalpha_l.c b/05/musl-final/src/locale/isalpha_l.c new file mode 100644 index 0000000..2e1205c --- /dev/null +++ b/05/musl-final/src/locale/isalpha_l.c @@ -0,0 +1,6 @@ +#include + +int isalpha_l(int c, locale_t l) +{ + return isalpha(c); +} diff --git a/05/musl-final/src/locale/isblank_l.c b/05/musl-final/src/locale/isblank_l.c new file mode 100644 index 0000000..27479aa --- /dev/null +++ b/05/musl-final/src/locale/isblank_l.c @@ -0,0 +1,6 @@ +#include + +int isblank_l(int c, locale_t l) +{ + return isblank(c); +} diff --git a/05/musl-final/src/locale/iscntrl_l.c b/05/musl-final/src/locale/iscntrl_l.c new file mode 100644 index 0000000..ca596fa --- /dev/null +++ b/05/musl-final/src/locale/iscntrl_l.c @@ -0,0 +1,6 @@ +#include + +int iscntrl_l(int c, locale_t l) +{ + return iscntrl(c); +} diff --git a/05/musl-final/src/locale/isdigit_l.c b/05/musl-final/src/locale/isdigit_l.c new file mode 100644 index 0000000..c8ae7bd --- /dev/null +++ b/05/musl-final/src/locale/isdigit_l.c @@ -0,0 +1,6 @@ +#include + +int isdigit_l(int c, locale_t l) +{ + return isdigit(c); +} diff --git a/05/musl-final/src/locale/isgraph_l.c b/05/musl-final/src/locale/isgraph_l.c new file mode 100644 index 0000000..713a86e --- /dev/null +++ b/05/musl-final/src/locale/isgraph_l.c @@ -0,0 +1,6 @@ +#include + +int isgraph_l(int c, locale_t l) +{ + return isgraph(c); +} diff --git a/05/musl-final/src/locale/islower_l.c b/05/musl-final/src/locale/islower_l.c new file mode 100644 index 0000000..25ec97a --- /dev/null +++ b/05/musl-final/src/locale/islower_l.c @@ -0,0 +1,6 @@ +#include + +int islower_l(int c, locale_t l) +{ + return islower(c); +} diff --git a/05/musl-final/src/locale/isprint_l.c b/05/musl-final/src/locale/isprint_l.c new file mode 100644 index 0000000..79ef351 --- /dev/null +++ b/05/musl-final/src/locale/isprint_l.c @@ -0,0 +1,6 @@ +#include + +int isprint_l(int c, locale_t l) +{ + return isprint(c); +} diff --git a/05/musl-final/src/locale/ispunct_l.c b/05/musl-final/src/locale/ispunct_l.c new file mode 100644 index 0000000..1c0bd04 --- /dev/null +++ b/05/musl-final/src/locale/ispunct_l.c @@ -0,0 +1,6 @@ +#include + +int ispunct_l(int c, locale_t l) +{ + return ispunct(c); +} diff --git a/05/musl-final/src/locale/isspace_l.c b/05/musl-final/src/locale/isspace_l.c new file mode 100644 index 0000000..e1a0efe --- /dev/null +++ b/05/musl-final/src/locale/isspace_l.c @@ -0,0 +1,6 @@ +#include + +int isspace_l(int c, locale_t l) +{ + return isspace(c); +} diff --git a/05/musl-final/src/locale/isupper_l.c b/05/musl-final/src/locale/isupper_l.c new file mode 100644 index 0000000..11ba703 --- /dev/null +++ b/05/musl-final/src/locale/isupper_l.c @@ -0,0 +1,6 @@ +#include + +int isupper_l(int c, locale_t l) +{ + return isupper(c); +} diff --git a/05/musl-final/src/locale/isxdigit_l.c b/05/musl-final/src/locale/isxdigit_l.c new file mode 100644 index 0000000..68649d0 --- /dev/null +++ b/05/musl-final/src/locale/isxdigit_l.c @@ -0,0 +1,6 @@ +#include + +int isxdigit_l(int c, locale_t l) +{ + return isxdigit(c); +} diff --git a/05/musl-final/src/locale/langinfo.c b/05/musl-final/src/locale/langinfo.c new file mode 100644 index 0000000..f7f5601 --- /dev/null +++ b/05/musl-final/src/locale/langinfo.c @@ -0,0 +1,58 @@ +#include +#include + +static const char c_time[] = + "Sun\0" "Mon\0" "Tue\0" "Wed\0" "Thu\0" "Fri\0" "Sat\0" + "Sunday\0" "Monday\0" "Tuesday\0" "Wednesday\0" + "Thursday\0" "Friday\0" "Saturday\0" + "Jan\0" "Feb\0" "Mar\0" "Apr\0" "May\0" "Jun\0" + "Jul\0" "Aug\0" "Sep\0" "Oct\0" "Nov\0" "Dec\0" + "January\0" "February\0" "March\0" "April\0" + "May\0" "June\0" "July\0" "August\0" + "September\0" "October\0" "November\0" "December\0" + "AM\0" "PM\0" + "%a %b %e %T %Y\0" + "%m/%d/%y\0" + "%H:%M:%S\0" + "%I:%M:%S %p\0" + "\0" + "%m/%d/%y\0" + "0123456789" + "%a %b %e %T %Y\0" + "%H:%M:%S"; + +static const char c_messages[] = "^[yY]\0" "^[nN]"; +static const char c_numeric[] = ".\0" ""; + +const char *__langinfo(nl_item item) +{ + int cat = item >> 16; + int idx = item & 65535; + const char *str; + + if (item == CODESET) return "UTF-8"; + + switch (cat) { + case LC_NUMERIC: + if (idx > 1) return NULL; + str = c_numeric; + break; + case LC_TIME: + if (idx > 0x31) return NULL; + str = c_time; + break; + case LC_MONETARY: + if (idx > 0) return NULL; + str = ""; + break; + case LC_MESSAGES: + if (idx > 1) return NULL; + str = c_messages; + break; + default: + return NULL; + } + + for (; idx; idx--, str++) for (; *str; str++); + return str; +} diff --git a/05/musl-final/src/locale/localeconv.c b/05/musl-final/src/locale/localeconv.c new file mode 100644 index 0000000..d79d1c0 --- /dev/null +++ b/05/musl-final/src/locale/localeconv.c @@ -0,0 +1,22 @@ +#include +#include +#include + +struct lconv *localeconv(void) +{ + static struct lconv *posix_lconv; + if (posix_lconv) return posix_lconv; + posix_lconv = malloc(sizeof *posix_lconv); + memset(posix_lconv, -1, sizeof *posix_lconv); + posix_lconv->decimal_point = "."; + posix_lconv->thousands_sep = ""; + posix_lconv->grouping = "\xff"; + posix_lconv->int_curr_symbol = ""; //"\xc2\xa4"; + posix_lconv->currency_symbol = ""; + posix_lconv->mon_decimal_point = ""; + posix_lconv->mon_thousands_sep = ""; + posix_lconv->mon_grouping = "\xff"; + posix_lconv->positive_sign = ""; // "+"; + posix_lconv->negative_sign = ""; // "-"; + return posix_lconv; +} diff --git a/05/musl-final/src/locale/newlocale.c b/05/musl-final/src/locale/newlocale.c new file mode 100644 index 0000000..986e796 --- /dev/null +++ b/05/musl-final/src/locale/newlocale.c @@ -0,0 +1,11 @@ +#include +#include +#include "locale_impl.h" + +locale_t newlocale(int mask, const char *name, locale_t base) +{ + if (*name && strcmp(name, "C") && strcmp(name, "POSIX")) + return 0; + if (!base) base = calloc(1, sizeof *base); + return base; +} diff --git a/05/musl-final/src/locale/nl_langinfo.c b/05/musl-final/src/locale/nl_langinfo.c new file mode 100644 index 0000000..bb3a2c4 --- /dev/null +++ b/05/musl-final/src/locale/nl_langinfo.c @@ -0,0 +1,13 @@ +#include + +// FIXME: other items + +char *nl_langinfo(nl_item item) +{ + switch (item) { + case CODESET: + return "UTF-8"; + default: + return ""; + } +} diff --git a/05/musl-final/src/locale/setlocale.c b/05/musl-final/src/locale/setlocale.c new file mode 100644 index 0000000..28f29b8 --- /dev/null +++ b/05/musl-final/src/locale/setlocale.c @@ -0,0 +1,9 @@ +#include + +char *setlocale(int category, const char *locale) +{ + /* Note: plain "C" would be better, but puts some broken + * software into legacy 8-bit-codepage mode, ignoring + * the standard library's multibyte encoding */ + return "C.UTF-8"; +} diff --git a/05/musl-final/src/locale/strcoll.c b/05/musl-final/src/locale/strcoll.c new file mode 100644 index 0000000..30bccd6 --- /dev/null +++ b/05/musl-final/src/locale/strcoll.c @@ -0,0 +1,6 @@ +#include + +int strcoll(const char *l, const char *r) +{ + return strcmp(l, r); +} diff --git a/05/musl-final/src/locale/strxfrm.c b/05/musl-final/src/locale/strxfrm.c new file mode 100644 index 0000000..8f12339 --- /dev/null +++ b/05/musl-final/src/locale/strxfrm.c @@ -0,0 +1,9 @@ +#include + +/* collate only by code points */ +size_t strxfrm(char *dest, const char *src, size_t n) +{ + size_t l = strlen(src); + if (n > l) strcpy(dest, src); + return l; +} diff --git a/05/musl-final/src/locale/tmp b/05/musl-final/src/locale/tmp new file mode 100644 index 0000000..aa71779 --- /dev/null +++ b/05/musl-final/src/locale/tmp @@ -0,0 +1,390 @@ +"iso88591\0\x08\x80" +"iso88592\0\x0a\x21" +"\x04\x61\x1b\x14\x29\x3d\x69\x75\x0a\x2a" +"\x60\x79\x45\x56\x5e\xad\xf4\xb5\x17\x2c" +"\x05\x6d\x2b\x14\x2d\x3e\x6d\x75\x2c\x2e" +"\x61\x7d\x55\x96\x5e\xdd\xfa\xc5\x17\x55" +"\xc1\x08\x23\x10\x31\x39\x19\x74\x0c\x43" +"\xc9\x60\xb4\x8c\x46\xcd\x38\xe3\x10\x44" +"\x43\x1d\x35\x0d\x35\x50\x59\x73\x0d\x56" +"\x6e\x69\x03\x17\x37\xdd\x88\xf5\x4d\x55" +"\xe1\x88\x33\x10\x39\x3a\x1d\x74\x4e\x43" +"\xe9\x64\xb4\xce\x46\xed\xb8\xf3\x50\x44" +"\x44\x21\x35\x0f\x3d\x51\xd9\x73\x4f\x56" +"\x6f\xe9\x13\x17\x3f\xfd\x8c\x95\x2d" +"iso88593\0\x0a0x21" +"\x26\x61\x3b\x0a\x29\x00\x90\x74\x0a\x2a" +"\x30\x79\xe5\x11\x4d\xad\x00\xb0\x17\x2c" +"\x27\xc9\x32\x0b\x2d\xb5\x94\x74\x0b\x2e" +"\x31\x7d\xf5\x51\x4d\xbd\x00\xc0\x17\x30" +"\xc1\x08\x03\x00\x31\x0a\x21\x74\x0c\x32" +"\xc9\x28\xb3\x0c\x33\xcd\x38\xf3\x0c\x00" +"\xd1\x48\x33\x0d\x35\x20\x59\x73\x0d\x47" +"\xd9\x68\xb3\x0d\x37\x6c\x71\xf5\x0d\x38" +"\xe1\x88\x03\x00\x39\x0b\x25\x74\x0e\x3a" +"\xe9\xa8\xb3\x0e\x3b\xed\xb8\xf3\x0e\x00" +"\xf1\xc8\x33\x0f\x3d\x21\xd9\x73\x4f\x47" +"\xf9\xe8\xb3\x0f\x3f\x6d\x75\x95\x2d" +"iso88594\0\x0a\x21" +"\x04\xe1\x64\x15\x29\x28\xed\x74\x0a\x2a" +"\x60\x49\x24\x92\x59\xad\xf4\xf5\x0a\x2c" +"\x05\x6d\x7b\x15\x2d\x29\xf1\x74\x2c\x2e" +"\x61\x4d\x34\xd2\x59\x4a\xf9\xb5\x14\x40" +"\xc1\x08\x33\x0c\x31\xc5\x18\xe3\x12\x43" +"\xc9\x60\xb4\x8c\x45\xcd\x38\xa3\x12\x44" +"\x45\x31\x65\x13\x35\xd5\x58\x73\x0d\x36" +"\x72\x69\xb3\x0d\x37\x68\xa9\xf5\x4d\x40" +"\xe1\x88\x33\x0e\x39\xe5\x98\xf3\x52\x43" +"\xe9\x64\xb4\xce\x45\xed\xb8\xb3\x52\x44" +"\x46\x35\x75\x13\x3d\xf5\xd8\x73\x0f\x3e" +"\x73\xe9\xb3\x0f\x3f\x69\xad\x95\x2d" +"iso88595\0\x0d\x21" +"\x01\x84\x00\x30\x40\x10\x10\x05\x84\x01" +"\x70\x40\x20\x10\x09\x84\x02\xb0\x40\x30" +"\x10\xad\x80\x03\xf0\x40\x40\x10\x11\x84" +"\x04\x30\x41\x50\x10\x15\x84\x05\x70\x41" +"\x60\x10\x19\x84\x06\xb0\x41\x70\x10\x1d" +"\x84\x07\xf0\x41\x80\x10\x21\x84\x08\x30" +"\x42\x90\x10\x25\x84\x09\x70\x42\xa0\x10" +"\x29\x84\x0a\xb0\x42\xb0\x10\x2d\x84\x0b" +"\xf0\x42\xc0\x10\x31\x84\x0c\x30\x43\xd0" +"\x10\x35\x84\x0d\x70\x43\xe0\x10\x39\x84" +"\x0e\xb0\x43\xf0\x10\x3d\x84\x0f\xf0\x43" +"\x00\x11\x41\x84\x10\x30\x44\x10\x11\x45" +"\x84\x11\x70\x44\x20\x11\x49\x84\x12\xb0" +"\x44\x30\x11\x4d\x84\x13\xf0\x44\x58\x84" +"\x51\x84\x14\x30\x45\x50\x11\x55\x84\x15" +"\x70\x45\x60\x11\x59\x84\x16\xb0\x45\x70" +"\x11\xa7\x80\x17\xf0\x45\x00" +"iso88596\0\x0b\x21" +"\x00\x00\x00\x00\x48\x01\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x18\xdc\x0a\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\xc0\x86\x00\x00\x00" +"\x00\x7c\x18\x00\x21\x16\xf1\x88\x48\x5c" +"\x62\x13\x9c\x18\xc5\x29\x56\xf1\x8a\x58" +"\xdc\x62\x17\xbc\x18\xc6\x31\x96\xf1\x8c" +"\x68\x5c\x63\x1b\xdc\x18\xc7\x39\xd6\x31" +"\x00\x00\x00\x00\x00\x00\x00\xc8\x41\x16" +"\xf2\x90\x88\x5c\x64\x23\x1c\x19\xc9\x49" +"\x56\xf2\x92\x98\xdc\x64\x27\x3c\x19\xca" +"\x51\x96\x32\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00" +"iso88597\0\x0d\x21" +"\x18\x60\x06\x30\x0a\xb0\x82\xaf\xa0\x29" +"\x70\x0a\xa0\x02\xa9\x80\xde\xb0\x0a\xb0" +"\x02\xad\x00\x00\x50\x01\xc0\x02\xb1\x80" +"\x2c\x30\x0b\x10\x0e\x85\x83\xe1\x70\x0b" +"\x20\x0e\x89\x83\xe2\xb0\x0b\x30\x0e\xbd" +"\x80\xe3\xf0\x38\x40\x0e\x91\x83\xe4\x30" +"\x39\x50\x0e\x95\x83\xe5\x70\x39\x60\x0e" +"\x99\x83\xe6\xb0\x39\x70\x0e\x9d\x83\xe7" +"\xf0\x39\x80\x0e\xa1\x03\x00\x30\x3a\x90" +"\x0e\xa5\x83\xe9\x70\x3a\xa0\x0e\xa9\x83" +"\xea\xb0\x3a\xb0\x0e\xad\x83\xeb\xf0\x3a" +"\xc0\x0e\xb1\x83\xec\x30\x3b\xd0\x0e\xb5" +"\x83\xed\x70\x3b\xe0\x0e\xb9\x83\xee\xb0" +"\x3b\xf0\x0e\xbd\x83\xef\xf0\x3b\x00\x0f" +"\xc1\x83\xf0\x30\x3c\x10\x0f\xc5\x83\xf1" +"\x70\x3c\x20\x0f\xc9\x83\xf2\xb0\x3c\x30" +"\x0f\xcd\x83\xf3\x00\x00\x00" +"iso88598\0\x0d\x21" +"\x00\x80\x28\x30\x0a\x90\x02\xa5\x80\x29" +"\x70\x0a\xa0\x02\xa9\xc0\x35\xb0\x0a\xb0" +"\x02\xad\x80\x2b\xf0\x0a\xc0\x02\xb1\x80" +"\x2c\x30\x0b\xd0\x02\xb5\x80\x2d\x70\x0b" +"\xe0\x02\xb9\xc0\x3d\xb0\x0b\xf0\x02\xbd" +"\x80\x2f\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +"\x00\x00\x00\x00\x00\x00\x00\x00\x70\x01" +"\x40\x17\xd1\x85\x74\x30\x5d\x50\x17\xd5" +"\x85\x75\x70\x5d\x60\x17\xd9\x85\x76\xb0" +"\x5d\x70\x17\xdd\x85\x77\xf0\x5d\x80\x17" +"\xe1\x85\x78\x30\x5e\x90\x17\xe5\x85\x79" +"\x70\x5e\xa0\x17\xe9\x85\x7a\x00\x00\x00" +"\x00\x0e\xe0\x03\x00\x00\x00" +"iso88599\0\x09\x50" +"\x1e\xa3\x49\x9b\x46\xad\x9a\xb5\x6b\xd8" +"\xb2\x69\xdb\xc6\x0d\xa6\xd7\x6f\xe0\xc2" +"\x89\x1b\x47\xae\x9c\xb9\x73\xe8\xd2\xa9" +"\x5b\xc7\xae\x9d\xbb\x77\x1f\xe3\xc9\x9b" +"\x47\xaf\x9e\xbd\x7b\xf8\xf2\xe9\xdb\xc7" +"\x2f\xe6\xd7\x7f" +"iso885910\0\x0d\x21" +"\x04\x81\x44\x20\x12\xa8\x04\x28\x81\x4d" +"\x70\x0a\xec\x04\x10\x01\x58\x60\x16\xf4" +"\x05\xad\x80\x5a\xa0\x14\xc0\x02\x05\xc1" +"\x44\x30\x12\xac\x04\x29\xc1\x4d\x70\x0b" +"\xf0\x04\x11\x41\x58\x70\x16\xf8\x05\x15" +"\xe0\x5a\xb0\x14\x00\x04\xc1\x80\x30\x30" +"\x0c\x10\x03\xc5\x80\x31\xe0\x12\x30\x04" +"\xc9\x00\x46\xb0\x0c\x58\x04\xcd\x80\x33" +"\xf0\x0c\x40\x03\x45\x01\x53\x30\x0d\x50" +"\x03\xd5\x80\x35\x80\x16\x60\x03\x72\x81" +"\x36\xb0\x0d\x70\x03\xdd\x80\x37\xf0\x0d" +"\x04\x04\xe1\x80\x38\x30\x0e\x90\x03\xe5" +"\x80\x39\xf0\x12\x34\x04\xe9\x40\x46\xb0" +"\x0e\x5c\x04\xed\x80\x3b\xf0\x0e\xc0\x03" +"\x46\x41\x53\x30\x0f\xd0\x03\xf5\x80\x3d" +"\x90\x16\xe0\x03\x73\x81\x3e\xb0\x0f\xf0" +"\x03\xfd\x80\x3f\x80\x13\x00" +"iso885913\0\x0d\x21" +"\x1d\xa0\x28\x30\x0a\x90\x02\x1e\xa0\x29" +"\x70\x0a\x60\x03\xa9\x80\x55\xb0\x0a\xb0" +"\x02\xad\x80\x2b\x60\x0c\xc0\x02\xb1\x80" +"\x2c\x30\x0b\x70\x80\xb5\x80\x2d\x70\x0b" +"\xe0\x03\xb9\xc0\x55\xb0\x0b\xf0\x02\xbd" +"\x80\x2f\x60\x0e\x10\x04\x2e\x01\x40\x60" +"\x10\x10\x03\xc5\x00\x46\x20\x11\x30\x04" +"\xc9\x40\x5e\x60\x11\x88\x04\x36\x81\x4a" +"\xb0\x13\x80\x05\x43\x41\x51\x30\x0d\x30" +"\x05\xd5\x80\x35\x70\x0d\xc8\x05\x41\x81" +"\x56\xa0\x16\x70\x03\x7b\x41\x5f\xf0\x0d" +"\x14\x04\x2f\x41\x40\x70\x10\x90\x03\xe5" +"\x40\x46\x30\x11\x34\x04\xe9\x80\x5e\x70" +"\x11\x8c\x04\x37\xc1\x4a\xc0\x13\x84\x05" +"\x44\x81\x51\x30\x0f\x34\x05\xf5\x80\x3d" +"\x70\x0f\xcc\x05\x42\xc1\x56\xb0\x16\xf0" +"\x03\x7c\x81\x5f\x90\x01\x00" +"iso885914\0\x0c\x21" +"\x02\x7e\xc0\x8c\x02\x85\xb0\x10\x14\xfc" +"\x29\x00\xf4\xa9\x40\xd0\x2c\x78\x79\xd0" +"\x0a\x5c\x01\x5e\xf0\xf0\x1f\x1e\x24\x84" +"\x04\x20\x10\xe4\x6c\x81\x95\x08\xf4\x57" +"\x7e\xd0\x80\xf9\x79\x40\xe8\x0a\x7d\x98" +"\x00\x06\xc1\x40\x18\x0c\x03\x62\x50\x0c" +"\x8c\xc1\x31\x40\x06\xc9\x40\x19\x2c\x03" +"\x66\xd0\x0c\x9c\xc1\x33\xa0\x0b\xd1\x40" +"\x1a\x4c\x03\x6a\x50\x0d\xac\x81\x9a\xc0" +"\x06\xd9\x40\x1b\x6c\x03\x6e\xd0\x0d\xec" +"\xc2\x37\x00\x07\xe1\x40\x1c\x8c\x03\x72" +"\x50\x0e\xcc\xc1\x39\x40\x07\xe9\x40\x1d" +"\xac\x03\x76\xd0\x0e\xdc\xc1\x3b\xa8\x0b" +"\xf1\x40\x1e\xcc\x03\x7a\x50\x0f\xec\xc1" +"\x9a\xc0\x07\xf9\x40\x1f\xec\x03\x7e\xd0" +"\x0f\xee\xc2\x3f\x00" +"iso885916\0\x0d\x21" +"\x04\x41\x41\x10\x14\xb0\x82\x1e\x20\x58" +"\x70\x0a\x84\x05\xa9\x00\x86\xb0\x0a\xe4" +"\x05\xad\x80\x5e\xb0\x17\xc0\x02\xb1\x00" +"\x43\x20\x14\xf4\x05\x1d\xa0\x2d\x70\x0b" +"\xf8\x05\x0d\x41\x86\xb0\x0b\x48\x05\x53" +"\x01\x5e\xc0\x17\x00\x03\xc1\x80\x30\x20" +"\x10\x10\x03\x06\x81\x31\x70\x0c\x20\x03" +"\xc9\x80\x32\xb0\x0c\x30\x03\xcd\x80\x33" +"\xf0\x0c\x40\x04\x43\x81\x34\x30\x0d\x50" +"\x03\x50\x81\x35\xa0\x15\xc0\x05\xd9\x80" +"\x36\xb0\x0d\x70\x03\x18\x81\x86\xf0\x0d" +"\x80\x03\xe1\x80\x38\x30\x10\x90\x03\x07" +"\x81\x39\x70\x0e\xa0\x03\xe9\x80\x3a\xb0" +"\x0e\xb0\x03\xed\x80\x3b\xf0\x0e\x44\x04" +"\x44\x81\x3c\x30\x0f\xd0\x03\x51\x81\x3d" +"\xb0\x15\xc4\x05\xf9\x80\x3e\xb0\x0f\xf0" +"\x03\x19\xc1\x86\xf0\x0f\x00" +; + + +'i','s','o','8','8','5','9','1',0, +8,128, +'i','s','o','8','8','5','9','2',0, +10, 33, +0x04, 0x61, 0x1b, 0x14, 0x29, 0x3d, 0x69, 0x75, 0x0a, 0x2a, +0x60, 0x79, 0x45, 0x56, 0x5e, 0xad, 0xf4, 0xb5, 0x17, 0x2c, +0x05, 0x6d, 0x2b, 0x14, 0x2d, 0x3e, 0x6d, 0x75, 0x2c, 0x2e, +0x61, 0x7d, 0x55, 0x96, 0x5e, 0xdd, 0xfa, 0xc5, 0x17, 0x55, +0xc1, 0x08, 0x23, 0x10, 0x31, 0x39, 0x19, 0x74, 0x0c, 0x43, +0xc9, 0x60, 0xb4, 0x8c, 0x46, 0xcd, 0x38, 0xe3, 0x10, 0x44, +0x43, 0x1d, 0x35, 0x0d, 0x35, 0x50, 0x59, 0x73, 0x0d, 0x56, +0x6e, 0x69, 0x03, 0x17, 0x37, 0xdd, 0x88, 0xf5, 0x4d, 0x55, +0xe1, 0x88, 0x33, 0x10, 0x39, 0x3a, 0x1d, 0x74, 0x4e, 0x43, +0xe9, 0x64, 0xb4, 0xce, 0x46, 0xed, 0xb8, 0xf3, 0x50, 0x44, +0x44, 0x21, 0x35, 0x0f, 0x3d, 0x51, 0xd9, 0x73, 0x4f, 0x56, +0x6f, 0xe9, 0x13, 0x17, 0x3f, 0xfd, 0x8c, 0x95, 0x2d, +'i','s','o','8','8','5','9','3',0, +10, 33, +0x26, 0x61, 0x3b, 0x0a, 0x29, 0x00, 0x90, 0x74, 0x0a, 0x2a, +0x30, 0x79, 0xe5, 0x11, 0x4d, 0xad, 0x00, 0xb0, 0x17, 0x2c, +0x27, 0xc9, 0x32, 0x0b, 0x2d, 0xb5, 0x94, 0x74, 0x0b, 0x2e, +0x31, 0x7d, 0xf5, 0x51, 0x4d, 0xbd, 0x00, 0xc0, 0x17, 0x30, +0xc1, 0x08, 0x03, 0x00, 0x31, 0x0a, 0x21, 0x74, 0x0c, 0x32, +0xc9, 0x28, 0xb3, 0x0c, 0x33, 0xcd, 0x38, 0xf3, 0x0c, 0x00, +0xd1, 0x48, 0x33, 0x0d, 0x35, 0x20, 0x59, 0x73, 0x0d, 0x47, +0xd9, 0x68, 0xb3, 0x0d, 0x37, 0x6c, 0x71, 0xf5, 0x0d, 0x38, +0xe1, 0x88, 0x03, 0x00, 0x39, 0x0b, 0x25, 0x74, 0x0e, 0x3a, +0xe9, 0xa8, 0xb3, 0x0e, 0x3b, 0xed, 0xb8, 0xf3, 0x0e, 0x00, +0xf1, 0xc8, 0x33, 0x0f, 0x3d, 0x21, 0xd9, 0x73, 0x4f, 0x47, +0xf9, 0xe8, 0xb3, 0x0f, 0x3f, 0x6d, 0x75, 0x95, 0x2d, +'i','s','o','8','8','5','9','4',0, +10, 33, +0x04, 0xe1, 0x64, 0x15, 0x29, 0x28, 0xed, 0x74, 0x0a, 0x2a, +0x60, 0x49, 0x24, 0x92, 0x59, 0xad, 0xf4, 0xf5, 0x0a, 0x2c, +0x05, 0x6d, 0x7b, 0x15, 0x2d, 0x29, 0xf1, 0x74, 0x2c, 0x2e, +0x61, 0x4d, 0x34, 0xd2, 0x59, 0x4a, 0xf9, 0xb5, 0x14, 0x40, +0xc1, 0x08, 0x33, 0x0c, 0x31, 0xc5, 0x18, 0xe3, 0x12, 0x43, +0xc9, 0x60, 0xb4, 0x8c, 0x45, 0xcd, 0x38, 0xa3, 0x12, 0x44, +0x45, 0x31, 0x65, 0x13, 0x35, 0xd5, 0x58, 0x73, 0x0d, 0x36, +0x72, 0x69, 0xb3, 0x0d, 0x37, 0x68, 0xa9, 0xf5, 0x4d, 0x40, +0xe1, 0x88, 0x33, 0x0e, 0x39, 0xe5, 0x98, 0xf3, 0x52, 0x43, +0xe9, 0x64, 0xb4, 0xce, 0x45, 0xed, 0xb8, 0xb3, 0x52, 0x44, +0x46, 0x35, 0x75, 0x13, 0x3d, 0xf5, 0xd8, 0x73, 0x0f, 0x3e, +0x73, 0xe9, 0xb3, 0x0f, 0x3f, 0x69, 0xad, 0x95, 0x2d, +'i','s','o','8','8','5','9','5',0, +14, 33, +0x01, 0x84, 0x00, 0x30, 0x40, 0x10, 0x10, 0x05, 0x84, 0x01, +0x70, 0x40, 0x20, 0x10, 0x09, 0x84, 0x02, 0xb0, 0x40, 0x30, +0x10, 0xad, 0x80, 0x03, 0xf0, 0x40, 0x40, 0x10, 0x11, 0x84, +0x04, 0x30, 0x41, 0x50, 0x10, 0x15, 0x84, 0x05, 0x70, 0x41, +0x60, 0x10, 0x19, 0x84, 0x06, 0xb0, 0x41, 0x70, 0x10, 0x1d, +0x84, 0x07, 0xf0, 0x41, 0x80, 0x10, 0x21, 0x84, 0x08, 0x30, +0x42, 0x90, 0x10, 0x25, 0x84, 0x09, 0x70, 0x42, 0xa0, 0x10, +0x29, 0x84, 0x0a, 0xb0, 0x42, 0xb0, 0x10, 0x2d, 0x84, 0x0b, +0xf0, 0x42, 0xc0, 0x10, 0x31, 0x84, 0x0c, 0x30, 0x43, 0xd0, +0x10, 0x35, 0x84, 0x0d, 0x70, 0x43, 0xe0, 0x10, 0x39, 0x84, +0x0e, 0xb0, 0x43, 0xf0, 0x10, 0x3d, 0x84, 0x0f, 0xf0, 0x43, +0x00, 0x11, 0x41, 0x84, 0x10, 0x30, 0x44, 0x10, 0x11, 0x45, +0x84, 0x11, 0x70, 0x44, 0x20, 0x11, 0x49, 0x84, 0x12, 0xb0, +0x44, 0x30, 0x11, 0x4d, 0x84, 0x13, 0xf0, 0x44, 0x58, 0x84, +0x51, 0x84, 0x14, 0x30, 0x45, 0x50, 0x11, 0x55, 0x84, 0x15, +0x70, 0x45, 0x60, 0x11, 0x59, 0x84, 0x16, 0xb0, 0x45, 0x70, +0x11, 0xa7, 0x80, 0x17, 0xf0, 0x45, 0x00, +'i','s','o','8','8','5','9','6',0, +11, 33, +0x00, 0x00, 0x00, 0x00, 0x48, 0x01, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xdc, 0x0a, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x86, 0x00, 0x00, 0x00, +0x00, 0x7c, 0x18, 0x00, 0x21, 0x16, 0xf1, 0x88, 0x48, 0x5c, +0x62, 0x13, 0x9c, 0x18, 0xc5, 0x29, 0x56, 0xf1, 0x8a, 0x58, +0xdc, 0x62, 0x17, 0xbc, 0x18, 0xc6, 0x31, 0x96, 0xf1, 0x8c, +0x68, 0x5c, 0x63, 0x1b, 0xdc, 0x18, 0xc7, 0x39, 0xd6, 0x31, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x41, 0x16, +0xf2, 0x90, 0x88, 0x5c, 0x64, 0x23, 0x1c, 0x19, 0xc9, 0x49, +0x56, 0xf2, 0x92, 0x98, 0xdc, 0x64, 0x27, 0x3c, 0x19, 0xca, +0x51, 0x96, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, +'i','s','o','8','8','5','9','7',0, +14, 33, +0x18, 0x60, 0x06, 0x30, 0x0a, 0xb0, 0x82, 0xaf, 0xa0, 0x29, +0x70, 0x0a, 0xa0, 0x02, 0xa9, 0x80, 0xde, 0xb0, 0x0a, 0xb0, +0x02, 0xad, 0x00, 0x00, 0x50, 0x01, 0xc0, 0x02, 0xb1, 0x80, +0x2c, 0x30, 0x0b, 0x10, 0x0e, 0x85, 0x83, 0xe1, 0x70, 0x0b, +0x20, 0x0e, 0x89, 0x83, 0xe2, 0xb0, 0x0b, 0x30, 0x0e, 0xbd, +0x80, 0xe3, 0xf0, 0x38, 0x40, 0x0e, 0x91, 0x83, 0xe4, 0x30, +0x39, 0x50, 0x0e, 0x95, 0x83, 0xe5, 0x70, 0x39, 0x60, 0x0e, +0x99, 0x83, 0xe6, 0xb0, 0x39, 0x70, 0x0e, 0x9d, 0x83, 0xe7, +0xf0, 0x39, 0x80, 0x0e, 0xa1, 0x03, 0x00, 0x30, 0x3a, 0x90, +0x0e, 0xa5, 0x83, 0xe9, 0x70, 0x3a, 0xa0, 0x0e, 0xa9, 0x83, +0xea, 0xb0, 0x3a, 0xb0, 0x0e, 0xad, 0x83, 0xeb, 0xf0, 0x3a, +0xc0, 0x0e, 0xb1, 0x83, 0xec, 0x30, 0x3b, 0xd0, 0x0e, 0xb5, +0x83, 0xed, 0x70, 0x3b, 0xe0, 0x0e, 0xb9, 0x83, 0xee, 0xb0, +0x3b, 0xf0, 0x0e, 0xbd, 0x83, 0xef, 0xf0, 0x3b, 0x00, 0x0f, +0xc1, 0x83, 0xf0, 0x30, 0x3c, 0x10, 0x0f, 0xc5, 0x83, 0xf1, +0x70, 0x3c, 0x20, 0x0f, 0xc9, 0x83, 0xf2, 0xb0, 0x3c, 0x30, +0x0f, 0xcd, 0x83, 0xf3, 0x00, 0x00, 0x00, +'i','s','o','8','8','5','9','8',0, +14, 33, +0x00, 0x80, 0x28, 0x30, 0x0a, 0x90, 0x02, 0xa5, 0x80, 0x29, +0x70, 0x0a, 0xa0, 0x02, 0xa9, 0xc0, 0x35, 0xb0, 0x0a, 0xb0, +0x02, 0xad, 0x80, 0x2b, 0xf0, 0x0a, 0xc0, 0x02, 0xb1, 0x80, +0x2c, 0x30, 0x0b, 0xd0, 0x02, 0xb5, 0x80, 0x2d, 0x70, 0x0b, +0xe0, 0x02, 0xb9, 0xc0, 0x3d, 0xb0, 0x0b, 0xf0, 0x02, 0xbd, +0x80, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x01, +0x40, 0x17, 0xd1, 0x85, 0x74, 0x30, 0x5d, 0x50, 0x17, 0xd5, +0x85, 0x75, 0x70, 0x5d, 0x60, 0x17, 0xd9, 0x85, 0x76, 0xb0, +0x5d, 0x70, 0x17, 0xdd, 0x85, 0x77, 0xf0, 0x5d, 0x80, 0x17, +0xe1, 0x85, 0x78, 0x30, 0x5e, 0x90, 0x17, 0xe5, 0x85, 0x79, +0x70, 0x5e, 0xa0, 0x17, 0xe9, 0x85, 0x7a, 0x00, 0x00, 0x00, +0x00, 0x0e, 0xe0, 0x03, 0x00, 0x00, 0x00, +'i','s','o','8','8','5','9','9',0, +9, 80, +0x1e, 0xa3, 0x49, 0x9b, 0x46, 0xad, 0x9a, 0xb5, 0x6b, 0xd8, +0xb2, 0x69, 0xdb, 0xc6, 0x0d, 0xa6, 0xd7, 0x6f, 0xe0, 0xc2, +0x89, 0x1b, 0x47, 0xae, 0x9c, 0xb9, 0x73, 0xe8, 0xd2, 0xa9, +0x5b, 0xc7, 0xae, 0x9d, 0xbb, 0x77, 0x1f, 0xe3, 0xc9, 0x9b, +0x47, 0xaf, 0x9e, 0xbd, 0x7b, 0xf8, 0xf2, 0xe9, 0xdb, 0xc7, +0x2f, 0xe6, 0xd7, 0x7f, +'i','s','o','8','8','5','9','1','0',0, +14, 33, +0x04, 0x81, 0x44, 0x20, 0x12, 0xa8, 0x04, 0x28, 0x81, 0x4d, +0x70, 0x0a, 0xec, 0x04, 0x10, 0x01, 0x58, 0x60, 0x16, 0xf4, +0x05, 0xad, 0x80, 0x5a, 0xa0, 0x14, 0xc0, 0x02, 0x05, 0xc1, +0x44, 0x30, 0x12, 0xac, 0x04, 0x29, 0xc1, 0x4d, 0x70, 0x0b, +0xf0, 0x04, 0x11, 0x41, 0x58, 0x70, 0x16, 0xf8, 0x05, 0x15, +0xe0, 0x5a, 0xb0, 0x14, 0x00, 0x04, 0xc1, 0x80, 0x30, 0x30, +0x0c, 0x10, 0x03, 0xc5, 0x80, 0x31, 0xe0, 0x12, 0x30, 0x04, +0xc9, 0x00, 0x46, 0xb0, 0x0c, 0x58, 0x04, 0xcd, 0x80, 0x33, +0xf0, 0x0c, 0x40, 0x03, 0x45, 0x01, 0x53, 0x30, 0x0d, 0x50, +0x03, 0xd5, 0x80, 0x35, 0x80, 0x16, 0x60, 0x03, 0x72, 0x81, +0x36, 0xb0, 0x0d, 0x70, 0x03, 0xdd, 0x80, 0x37, 0xf0, 0x0d, +0x04, 0x04, 0xe1, 0x80, 0x38, 0x30, 0x0e, 0x90, 0x03, 0xe5, +0x80, 0x39, 0xf0, 0x12, 0x34, 0x04, 0xe9, 0x40, 0x46, 0xb0, +0x0e, 0x5c, 0x04, 0xed, 0x80, 0x3b, 0xf0, 0x0e, 0xc0, 0x03, +0x46, 0x41, 0x53, 0x30, 0x0f, 0xd0, 0x03, 0xf5, 0x80, 0x3d, +0x90, 0x16, 0xe0, 0x03, 0x73, 0x81, 0x3e, 0xb0, 0x0f, 0xf0, +0x03, 0xfd, 0x80, 0x3f, 0x80, 0x13, 0x00, +'i','s','o','8','8','5','9','1','3',0, +14, 33, +0x1d, 0xa0, 0x28, 0x30, 0x0a, 0x90, 0x02, 0x1e, 0xa0, 0x29, +0x70, 0x0a, 0x60, 0x03, 0xa9, 0x80, 0x55, 0xb0, 0x0a, 0xb0, +0x02, 0xad, 0x80, 0x2b, 0x60, 0x0c, 0xc0, 0x02, 0xb1, 0x80, +0x2c, 0x30, 0x0b, 0x70, 0x80, 0xb5, 0x80, 0x2d, 0x70, 0x0b, +0xe0, 0x03, 0xb9, 0xc0, 0x55, 0xb0, 0x0b, 0xf0, 0x02, 0xbd, +0x80, 0x2f, 0x60, 0x0e, 0x10, 0x04, 0x2e, 0x01, 0x40, 0x60, +0x10, 0x10, 0x03, 0xc5, 0x00, 0x46, 0x20, 0x11, 0x30, 0x04, +0xc9, 0x40, 0x5e, 0x60, 0x11, 0x88, 0x04, 0x36, 0x81, 0x4a, +0xb0, 0x13, 0x80, 0x05, 0x43, 0x41, 0x51, 0x30, 0x0d, 0x30, +0x05, 0xd5, 0x80, 0x35, 0x70, 0x0d, 0xc8, 0x05, 0x41, 0x81, +0x56, 0xa0, 0x16, 0x70, 0x03, 0x7b, 0x41, 0x5f, 0xf0, 0x0d, +0x14, 0x04, 0x2f, 0x41, 0x40, 0x70, 0x10, 0x90, 0x03, 0xe5, +0x40, 0x46, 0x30, 0x11, 0x34, 0x04, 0xe9, 0x80, 0x5e, 0x70, +0x11, 0x8c, 0x04, 0x37, 0xc1, 0x4a, 0xc0, 0x13, 0x84, 0x05, +0x44, 0x81, 0x51, 0x30, 0x0f, 0x34, 0x05, 0xf5, 0x80, 0x3d, +0x70, 0x0f, 0xcc, 0x05, 0x42, 0xc1, 0x56, 0xb0, 0x16, 0xf0, +0x03, 0x7c, 0x81, 0x5f, 0x90, 0x01, 0x00, +'i','s','o','8','8','5','9','1','4',0, +13, 33, +0x02, 0x7e, 0xc0, 0x8c, 0x02, 0x85, 0xb0, 0x10, 0x14, 0xfc, +0x29, 0x00, 0xf4, 0xa9, 0x40, 0xd0, 0x2c, 0x78, 0x79, 0xd0, +0x0a, 0x5c, 0x01, 0x5e, 0xf0, 0xf0, 0x1f, 0x1e, 0x24, 0x84, +0x04, 0x20, 0x10, 0xe4, 0x6c, 0x81, 0x95, 0x08, 0xf4, 0x57, +0x7e, 0xd0, 0x80, 0xf9, 0x79, 0x40, 0xe8, 0x0a, 0x7d, 0x98, +0x00, 0x06, 0xc1, 0x40, 0x18, 0x0c, 0x03, 0x62, 0x50, 0x0c, +0x8c, 0xc1, 0x31, 0x40, 0x06, 0xc9, 0x40, 0x19, 0x2c, 0x03, +0x66, 0xd0, 0x0c, 0x9c, 0xc1, 0x33, 0xa0, 0x0b, 0xd1, 0x40, +0x1a, 0x4c, 0x03, 0x6a, 0x50, 0x0d, 0xac, 0x81, 0x9a, 0xc0, +0x06, 0xd9, 0x40, 0x1b, 0x6c, 0x03, 0x6e, 0xd0, 0x0d, 0xec, +0xc2, 0x37, 0x00, 0x07, 0xe1, 0x40, 0x1c, 0x8c, 0x03, 0x72, +0x50, 0x0e, 0xcc, 0xc1, 0x39, 0x40, 0x07, 0xe9, 0x40, 0x1d, +0xac, 0x03, 0x76, 0xd0, 0x0e, 0xdc, 0xc1, 0x3b, 0xa8, 0x0b, +0xf1, 0x40, 0x1e, 0xcc, 0x03, 0x7a, 0x50, 0x0f, 0xec, 0xc1, +0x9a, 0xc0, 0x07, 0xf9, 0x40, 0x1f, 0xec, 0x03, 0x7e, 0xd0, +0x0f, 0xee, 0xc2, 0x3f, 0x00, +'i','s','o','8','8','5','9','1','6',0, +14, 33, +0x04, 0x41, 0x41, 0x10, 0x14, 0xb0, 0x82, 0x1e, 0x20, 0x58, +0x70, 0x0a, 0x84, 0x05, 0xa9, 0x00, 0x86, 0xb0, 0x0a, 0xe4, +0x05, 0xad, 0x80, 0x5e, 0xb0, 0x17, 0xc0, 0x02, 0xb1, 0x00, +0x43, 0x20, 0x14, 0xf4, 0x05, 0x1d, 0xa0, 0x2d, 0x70, 0x0b, +0xf8, 0x05, 0x0d, 0x41, 0x86, 0xb0, 0x0b, 0x48, 0x05, 0x53, +0x01, 0x5e, 0xc0, 0x17, 0x00, 0x03, 0xc1, 0x80, 0x30, 0x20, +0x10, 0x10, 0x03, 0x06, 0x81, 0x31, 0x70, 0x0c, 0x20, 0x03, +0xc9, 0x80, 0x32, 0xb0, 0x0c, 0x30, 0x03, 0xcd, 0x80, 0x33, +0xf0, 0x0c, 0x40, 0x04, 0x43, 0x81, 0x34, 0x30, 0x0d, 0x50, +0x03, 0x50, 0x81, 0x35, 0xa0, 0x15, 0xc0, 0x05, 0xd9, 0x80, +0x36, 0xb0, 0x0d, 0x70, 0x03, 0x18, 0x81, 0x86, 0xf0, 0x0d, +0x80, 0x03, 0xe1, 0x80, 0x38, 0x30, 0x10, 0x90, 0x03, 0x07, +0x81, 0x39, 0x70, 0x0e, 0xa0, 0x03, 0xe9, 0x80, 0x3a, 0xb0, +0x0e, 0xb0, 0x03, 0xed, 0x80, 0x3b, 0xf0, 0x0e, 0x44, 0x04, +0x44, 0x81, 0x3c, 0x30, 0x0f, 0xd0, 0x03, 0x51, 0x81, 0x3d, +0xb0, 0x15, 0xc4, 0x05, 0xf9, 0x80, 0x3e, 0xb0, 0x0f, 0xf0, +0x03, 0x19, 0xc1, 0x86, 0xf0, 0x0f, 0x00, diff --git a/05/musl-final/src/locale/tolower_l.c b/05/musl-final/src/locale/tolower_l.c new file mode 100644 index 0000000..ba27791 --- /dev/null +++ b/05/musl-final/src/locale/tolower_l.c @@ -0,0 +1,6 @@ +#include + +int tolower_l(int c, locale_t l) +{ + return tolower(c); +} diff --git a/05/musl-final/src/locale/toupper_l.c b/05/musl-final/src/locale/toupper_l.c new file mode 100644 index 0000000..73f2f39 --- /dev/null +++ b/05/musl-final/src/locale/toupper_l.c @@ -0,0 +1,6 @@ +#include + +int toupper_l(int c, locale_t l) +{ + return toupper(c); +} diff --git a/05/musl-final/src/locale/wcscoll.c b/05/musl-final/src/locale/wcscoll.c new file mode 100644 index 0000000..cdbce1c --- /dev/null +++ b/05/musl-final/src/locale/wcscoll.c @@ -0,0 +1,7 @@ +#include + +/* FIXME: stub */ +int wcscoll(const wchar_t *l, const wchar_t *r) +{ + return wcscmp(l, r); +} diff --git a/05/musl-final/src/locale/wcsxfrm.c b/05/musl-final/src/locale/wcsxfrm.c new file mode 100644 index 0000000..5f76e5a --- /dev/null +++ b/05/musl-final/src/locale/wcsxfrm.c @@ -0,0 +1,12 @@ +#include + +/* collate only by code points */ +size_t wcsxfrm(wchar_t *dest, const wchar_t *src, size_t n) +{ + size_t l = wcslen(src); + if (l >= n) { + wmemcpy(dest, src, n-1); + dest[n-1] = 0; + } else wcscpy(dest, src); + return l; +} diff --git a/05/musl-final/src/malloc/DESIGN b/05/musl-final/src/malloc/DESIGN new file mode 100644 index 0000000..58b0523 --- /dev/null +++ b/05/musl-final/src/malloc/DESIGN @@ -0,0 +1,22 @@ + + +In principle, this memory allocator is roughly equivalent to Doug +Lea's dlmalloc with fine-grained locking. + + + +malloc: + +Uses a freelist binned by chunk size, with a bitmap to optimize +searching for the smallest non-empty bin which can satisfy an +allocation. If no free chunks are available, it creates a new chunk of +the requested size and attempts to merge it with any existing free +chunk immediately below the newly created chunk. + +Whether the chunk was obtained from a bin or newly created, it's +likely to be larger than the requested allocation. malloc always +finishes its work by passing the new chunk to realloc, which will +split it into two chunks and free the tail portion. + + + diff --git a/05/musl-final/src/malloc/__brk.c b/05/musl-final/src/malloc/__brk.c new file mode 100644 index 0000000..e3b3af3 --- /dev/null +++ b/05/musl-final/src/malloc/__brk.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +uintptr_t __brk(uintptr_t newbrk) +{ + return syscall1(__NR_brk, newbrk); +} diff --git a/05/musl-final/src/malloc/__simple_malloc.c b/05/musl-final/src/malloc/__simple_malloc.c new file mode 100644 index 0000000..49b74c8 --- /dev/null +++ b/05/musl-final/src/malloc/__simple_malloc.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include "libc.h" + +uintptr_t __brk(uintptr_t); + +#define ALIGN 16 + +void *__simple_malloc(size_t n) +{ + static uintptr_t cur, brk; + uintptr_t base, new; + static int lock; + size_t align=1; + + if (n < SIZE_MAX - ALIGN) + while (align SIZE_MAX - brk) goto fail; + + base = cur + align-1 & -align; + if (base+n > brk) { + new = base+n + PAGE_SIZE-1 & -PAGE_SIZE; + if (__brk(new) != new) goto fail; + brk = new; + } + cur = base+n; + UNLOCK(&lock); + + return (void *)base; + +fail: + UNLOCK(&lock); + errno = ENOMEM; + return 0; +} + +weak_alias(__simple_malloc, malloc); diff --git a/05/musl-final/src/malloc/calloc.c b/05/musl-final/src/malloc/calloc.c new file mode 100644 index 0000000..9d57456 --- /dev/null +++ b/05/musl-final/src/malloc/calloc.c @@ -0,0 +1,23 @@ +#include +#include +#include + +void *calloc(size_t m, size_t n) +{ + void *p; + size_t *z; + if (n && m > (size_t)-1/n) { + errno = ENOMEM; + return 0; + } + n *= m; + p = malloc(n); + if (!p) return 0; + /* Only do this for non-mmapped chunks */ + if (((size_t *)p)[-1] & 7) { + /* Only write words that are not already zero */ + m = (n + sizeof *z - 1)/sizeof *z; + for (z=p; m; m--, z++) if (*z) *z=0; + } + return p; +} diff --git a/05/musl-final/src/malloc/malloc.c b/05/musl-final/src/malloc/malloc.c new file mode 100644 index 0000000..29f9760 --- /dev/null +++ b/05/musl-final/src/malloc/malloc.c @@ -0,0 +1,515 @@ +#include +#include +#include +#include +#include +#include +#include "libc.h" +#include "atomic.h" +#include "pthread_impl.h" + +uintptr_t __brk(uintptr_t); +void *__mmap(void *, size_t, int, int, int, off_t); +int __munmap(void *, size_t); +void *__mremap(void *, size_t, size_t, int, ...); +int __madvise(void *, size_t, int); + +struct chunk { + size_t data[1]; + struct chunk *next; + struct chunk *prev; +}; + +struct bin { + int lock[2]; + struct chunk *head; + struct chunk *tail; +}; + +static struct { + uintptr_t brk; + size_t *heap; + uint64_t binmap; + struct bin bins[64]; + int brk_lock[2]; + int free_lock[2]; +} mal; + + +#define SIZE_ALIGN (4*sizeof(size_t)) +#define SIZE_MASK (-SIZE_ALIGN) +#define OVERHEAD (2*sizeof(size_t)) +#define MMAP_THRESHOLD (0x1c00*SIZE_ALIGN) +#define DONTCARE 16 +#define RECLAIM 163840 + +#define CHUNK_SIZE(c) ((c)->data[0] & SIZE_MASK) +#define CHUNK_PSIZE(c) ((c)->data[-1] & SIZE_MASK) +#define PREV_CHUNK(c) ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c))) +#define NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CHUNK_SIZE(c))) +#define MEM_TO_CHUNK(p) (struct chunk *)((size_t *)p - 1) +#define CHUNK_TO_MEM(c) (void *)((c)->data+1) +#define BIN_TO_CHUNK(i) (MEM_TO_CHUNK(&mal.bins[i].head)) + +#define C_INUSE ((size_t)1) +#define C_FLAGS ((size_t)3) +#define C_SIZE SIZE_MASK + +#define IS_MMAPPED(c) !((c)->data[0] & (C_INUSE)) + + +/* Synchronization tools */ + +static void lock(volatile int *lk) +{ + if (!libc.threads_minus_1) return; + while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1); +} + +static void unlock(volatile int *lk) +{ + if (!libc.threads_minus_1) return; + a_store(lk, 0); + if (lk[1]) __wake(lk, 1, 1); +} + +static void lock_bin(int i) +{ + if (libc.threads_minus_1) + lock(mal.bins[i].lock); + if (!mal.bins[i].head) + mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i); +} + +static void unlock_bin(int i) +{ + if (!libc.threads_minus_1) return; + unlock(mal.bins[i].lock); +} + +static int first_set(uint64_t x) +{ +#if 1 + return a_ctz_64(x); +#else + static const char debruijn64[64] = { + 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, + 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, + 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, + 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 + }; + static const char debruijn32[32] = { + 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, + 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 + }; + if (sizeof(long) < 8) { + uint32_t y = x; + if (!y) { + y = x>>32; + return 32 + debruijn32[(y&-y)*0x076be629 >> 27]; + } + return debruijn32[(y&-y)*0x076be629 >> 27]; + } + return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58]; +#endif +} + +static int bin_index(size_t x) +{ + x = x / SIZE_ALIGN - 1; + if (x <= 32) return x; + if (x > 0x1c00) return 63; + return ((union { float v; uint32_t r; }){ x }.r>>21) - 496; +} + +static int bin_index_up(size_t x) +{ + x = x / SIZE_ALIGN - 1; + if (x <= 32) return x; + return ((union { float v; uint32_t r; }){ x }.r+0x1fffff>>21) - 496; +} + +#if 0 +void __dump_heap(int x) +{ + struct chunk *c; + int i; + for (c = (void *)mal.heap; CHUNK_SIZE(c); c = NEXT_CHUNK(c)) + fprintf(stderr, "base %p size %zu (%d) flags %d/%d\n", + c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)), + c->data[0] & 15, + NEXT_CHUNK(c)->data[-1] & 15); + for (i=0; i<64; i++) { + if (mal.bins[i].head != BIN_TO_CHUNK(i) && mal.bins[i].head) { + fprintf(stderr, "bin %d: %p\n", i, mal.bins[i].head); + if (!(mal.binmap & 1ULL< SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail; + new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE; + n = new - mal.brk; + + if (__brk(new) != new) goto fail; + + w = MEM_TO_CHUNK(new); + w->data[-1] = n | C_INUSE; + w->data[0] = 0 | C_INUSE; + + w = MEM_TO_CHUNK(mal.brk); + w->data[0] = n | C_INUSE; + mal.brk = new; + + unlock(mal.brk_lock); + + return w; +fail: + unlock(mal.brk_lock); + return 0; +} + +static int init_malloc() +{ + static int init, waiters; + int state; + struct chunk *c; + + if (init == 2) return 0; + + while ((state=a_swap(&init, 1)) == 1) + __wait(&init, &waiters, 1, 1); + if (state) { + a_store(&init, 2); + return 0; + } + + mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN; + + c = expand_heap(1); + + if (!c) { + a_store(&init, 0); + if (waiters) __wake(&init, 1, 1); + return -1; + } + + mal.heap = (void *)c; + c->data[-1] = 0 | C_INUSE; + free(CHUNK_TO_MEM(c)); + + a_store(&init, 2); + if (waiters) __wake(&init, -1, 1); + return 0; +} + +static int adjust_size(size_t *n) +{ + /* Result of pointer difference must fit in ptrdiff_t. */ + if (*n > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) { + errno = ENOMEM; + return -1; + } + *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK; + return 0; +} + +static void unbin(struct chunk *c, int i) +{ + if (c->prev == c->next) + a_and_64(&mal.binmap, ~(1ULL<prev->next = c->next; + c->next->prev = c->prev; + c->data[0] |= C_INUSE; + NEXT_CHUNK(c)->data[-1] |= C_INUSE; +} + +static int alloc_fwd(struct chunk *c) +{ + int i; + size_t k; + while (!((k=c->data[0]) & C_INUSE)) { + i = bin_index(k); + lock_bin(i); + if (c->data[0] == k) { + unbin(c, i); + unlock_bin(i); + return 1; + } + unlock_bin(i); + } + return 0; +} + +static int alloc_rev(struct chunk *c) +{ + int i; + size_t k; + while (!((k=c->data[-1]) & C_INUSE)) { + i = bin_index(k); + lock_bin(i); + if (c->data[-1] == k) { + unbin(PREV_CHUNK(c), i); + unlock_bin(i); + return 1; + } + unlock_bin(i); + } + return 0; +} + + +/* pretrim - trims a chunk _prior_ to removing it from its bin. + * Must be called with i as the ideal bin for size n, j the bin + * for the _free_ chunk self, and bin j locked. */ +static int pretrim(struct chunk *self, size_t n, int i, int j) +{ + size_t n1; + struct chunk *next, *split; + + /* We cannot pretrim if it would require re-binning. */ + if (j < 40) return 0; + if (j < i+3) { + if (j != 63) return 0; + n1 = CHUNK_SIZE(self); + if (n1-n <= MMAP_THRESHOLD) return 0; + } else { + n1 = CHUNK_SIZE(self); + } + if (bin_index(n1-n) != j) return 0; + + next = NEXT_CHUNK(self); + split = (void *)((char *)self + n); + + split->prev = self->prev; + split->next = self->next; + split->prev->next = split; + split->next->prev = split; + split->data[-1] = n | C_INUSE; + split->data[0] = n1-n; + next->data[-1] = n1-n; + self->data[0] = n | C_INUSE; + return 1; +} + +static void trim(struct chunk *self, size_t n) +{ + size_t n1 = CHUNK_SIZE(self); + struct chunk *next, *split; + + if (n >= n1 - DONTCARE) return; + + next = NEXT_CHUNK(self); + split = (void *)((char *)self + n); + + split->data[-1] = n | C_INUSE; + split->data[0] = n1-n | C_INUSE; + next->data[-1] = n1-n | C_INUSE; + self->data[0] = n | C_INUSE; + + free(CHUNK_TO_MEM(split)); +} + +void *malloc(size_t n) +{ + struct chunk *c; + int i, j; +if (n == 0) n = 1;/* everyone depends on this behavior for some fucking reason */ + if (!n || adjust_size(&n) < 0) return 0; + + if (n > MMAP_THRESHOLD) { + size_t len = n + PAGE_SIZE - 1 & -PAGE_SIZE; + char *base = __mmap(0, len, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (base == (void *)-1) return 0; + c = (void *)(base + SIZE_ALIGN - sizeof(size_t)); + c->data[0] = len - (SIZE_ALIGN - sizeof(size_t)); + c->data[-1] = SIZE_ALIGN - sizeof(size_t); + return CHUNK_TO_MEM(c); + } + + i = bin_index_up(n); + for (;;) { + uint64_t mask = mal.binmap & -(1ULL<data[-1] = c->data[0] = + x->data[0] + CHUNK_SIZE(c); + } + break; + } + j = first_set(mask); + lock_bin(j); + c = mal.bins[j].head; + if (c != BIN_TO_CHUNK(j) && j == bin_index(c->data[0])) { + if (!pretrim(c, n, i, j)) unbin(c, j); + unlock_bin(j); + break; + } + unlock_bin(j); + } + + /* Now patch up in case we over-allocated */ + trim(c, n); + + return CHUNK_TO_MEM(c); +} + +void *realloc(void *p, size_t n) +{ + struct chunk *self, *next; + size_t n0, n1; + void *new; + + if (!p) return malloc(n); + else if (!n) return free(p), (void *)0; + + if (adjust_size(&n) < 0) return 0; + + self = MEM_TO_CHUNK(p); + n1 = n0 = CHUNK_SIZE(self); + + if (IS_MMAPPED(self)) { + size_t extra = self->data[-1]; + char *base = (char *)self - extra; + size_t oldlen = n0 + extra; + size_t newlen = n + extra; + if (newlen < PAGE_SIZE && (new = malloc(n))) { + memcpy(new, p, n-OVERHEAD); + free(p); + return new; + } + newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; + if (oldlen == newlen) return p; + base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); + if (base == (void *)-1) + return newlen < oldlen ? p : 0; + self = (void *)(base + extra); + self->data[0] = newlen - extra; + return CHUNK_TO_MEM(self); + } + + next = NEXT_CHUNK(self); + + /* Merge adjacent chunks if we need more space. This is not + * a waste of time even if we fail to get enough space, because our + * subsequent call to free would otherwise have to do the merge. */ + if (n > n1 && alloc_fwd(next)) { + n1 += CHUNK_SIZE(next); + next = NEXT_CHUNK(next); + } + /* FIXME: find what's wrong here and reenable it..? */ + if (0 && n > n1 && alloc_rev(self)) { + self = PREV_CHUNK(self); + n1 += CHUNK_SIZE(self); + } + self->data[0] = n1 | C_INUSE; + next->data[-1] = n1 | C_INUSE; + + /* If we got enough space, split off the excess and return */ + if (n <= n1) { + //memmove(CHUNK_TO_MEM(self), p, n0-OVERHEAD); + trim(self, n); + return CHUNK_TO_MEM(self); + } + + /* As a last resort, allocate a new chunk and copy to it. */ + new = malloc(n-OVERHEAD); + if (!new) return 0; + memcpy(new, p, n0-OVERHEAD); + free(CHUNK_TO_MEM(self)); + return new; +} + +void free(void *p) +{ + struct chunk *self = MEM_TO_CHUNK(p); + struct chunk *next; + size_t final_size, new_size, size; + int reclaim=0; + int i; + + if (!p) return; + + if (IS_MMAPPED(self)) { + size_t extra = self->data[-1]; + char *base = (char *)self - extra; + size_t len = CHUNK_SIZE(self) + extra; + __munmap(base, len); + return; + } + + final_size = new_size = CHUNK_SIZE(self); + next = NEXT_CHUNK(self); + + for (;;) { + /* Replace middle of large chunks with fresh zero pages */ + if (reclaim && (self->data[-1] & next->data[0] & C_INUSE)) { + uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE; + uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE; +#if 1 + __madvise((void *)a, b-a, MADV_DONTNEED); +#else + __mmap((void *)a, b-a, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0); +#endif + } + + if (self->data[-1] & next->data[0] & C_INUSE) { + self->data[0] = final_size | C_INUSE; + next->data[-1] = final_size | C_INUSE; + i = bin_index(final_size); + lock_bin(i); + lock(mal.free_lock); + if (self->data[-1] & next->data[0] & C_INUSE) + break; + unlock(mal.free_lock); + unlock_bin(i); + } + + if (alloc_rev(self)) { + self = PREV_CHUNK(self); + size = CHUNK_SIZE(self); + final_size += size; + if (new_size+size > RECLAIM && (new_size+size^size) > size) + reclaim = 1; + } + + if (alloc_fwd(next)) { + size = CHUNK_SIZE(next); + final_size += size; + if (new_size+size > RECLAIM && (new_size+size^size) > size) + reclaim = 1; + next = NEXT_CHUNK(next); + } + } + + self->data[0] = final_size; + next->data[-1] = final_size; + unlock(mal.free_lock); + + self->next = BIN_TO_CHUNK(i); + self->prev = mal.bins[i].tail; + self->next->prev = self; + self->prev->next = self; + + if (!(mal.binmap & 1ULL< +#include + +void *memalign(size_t align, size_t len) +{ + void *mem; + int ret; + if ((ret = posix_memalign(&mem, align, len))) { + errno = ret; + return 0; + } + return mem; +} diff --git a/05/musl-final/src/malloc/posix_memalign.c b/05/musl-final/src/malloc/posix_memalign.c new file mode 100644 index 0000000..ef86260 --- /dev/null +++ b/05/musl-final/src/malloc/posix_memalign.c @@ -0,0 +1,47 @@ +#include +#include +#include + +/* This function should work with most dlmalloc-like chunk bookkeeping + * systems, but it's only guaranteed to work with the native implementation + * used in this library. */ + +int posix_memalign(void **res, size_t align, size_t len) +{ + unsigned char *mem, *new, *end; + size_t header, footer; + + if ((align & -align) != align) return EINVAL; + if (len > SIZE_MAX - align) return ENOMEM; + + if (align <= 4*sizeof(size_t)) { + if (!(mem = malloc(len))) + return errno; + *res = mem; + return 0; + } + + if (!(mem = malloc(len + align-1))) + return errno; + + header = ((size_t *)mem)[-1]; + end = mem + (header & -8); + footer = ((size_t *)end)[-2]; + new = (void *)((uintptr_t)mem + align-1 & -align); + + if (!(header & 7)) { + ((size_t *)new)[-2] = ((size_t *)mem)[-2] + (new-mem); + ((size_t *)new)[-1] = ((size_t *)mem)[-1] - (new-mem); + *res = new; + return 0; + } + + ((size_t *)mem)[-1] = header&7 | new-mem; + ((size_t *)new)[-2] = footer&7 | new-mem; + ((size_t *)new)[-1] = header&7 | end-new; + ((size_t *)end)[-2] = footer&7 | end-new; + + if (new != mem) free(mem); + *res = new; + return 0; +} diff --git a/05/musl-final/src/math/__fpclassify.c b/05/musl-final/src/math/__fpclassify.c new file mode 100644 index 0000000..1605110 --- /dev/null +++ b/05/musl-final/src/math/__fpclassify.c @@ -0,0 +1,14 @@ +#include +#include + +int __fpclassify(double __x) +{ + union { + double __d; + __uint64_t __i; + } __y = { __x }; + int __ee = __y.__i>>52 & 0x7ff; + if (!__ee) return __y.__i<<1 ? FP_SUBNORMAL : FP_ZERO; + if (__ee==0x7ff) return __y.__i<<12 ? FP_NAN : FP_INFINITE; + return FP_NORMAL; +} diff --git a/05/musl-final/src/math/__fpclassifyf.c b/05/musl-final/src/math/__fpclassifyf.c new file mode 100644 index 0000000..bf59d0d --- /dev/null +++ b/05/musl-final/src/math/__fpclassifyf.c @@ -0,0 +1,14 @@ +#include +#include + +int __fpclassifyf(float __x) +{ + union { + float __f; + __uint32_t __i; + } __y = { __x }; + int __ee = __y.__i>>23 & 0xff; + if (!__ee) return __y.__i<<1 ? FP_SUBNORMAL : FP_ZERO; + if (__ee==0xff) return __y.__i<<9 ? FP_NAN : FP_INFINITE; + return FP_NORMAL; +} diff --git a/05/musl-final/src/math/__fpclassifyl.c b/05/musl-final/src/math/__fpclassifyl.c new file mode 100644 index 0000000..4f93bef --- /dev/null +++ b/05/musl-final/src/math/__fpclassifyl.c @@ -0,0 +1,16 @@ +#include +#include + +/* FIXME: move this to arch-specific file */ +int __fpclassifyl(long double __x) +{ + union { + long double __ld; + __uint16_t __hw[5]; + __uint64_t __m; + } __y = { __x }; + int __ee = __y.__hw[4]&0x7fff; + if (!__ee) return __y.__m ? FP_SUBNORMAL : FP_ZERO; + if (__ee==0x7fff) return __y.__m ? FP_NAN : FP_INFINITE; + return FP_NORMAL; +} diff --git a/05/musl-final/src/math/__log1p.h b/05/musl-final/src/math/__log1p.h new file mode 100644 index 0000000..ec2c77b --- /dev/null +++ b/05/musl-final/src/math/__log1p.h @@ -0,0 +1,94 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/k_log.h */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* + * __log1p(f): + * Return log(1+f) - f for 1+f in ~[sqrt(2)/2, sqrt(2)]. + * + * The following describes the overall strategy for computing + * logarithms in base e. The argument reduction and adding the final + * term of the polynomial are done by the caller for increased accuracy + * when different bases are used. + * + * Method : + * 1. Argument Reduction: find k and f such that + * x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * 2. Approximation of log(1+f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Reme algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s + * (the values of Lg1 to Lg7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lg1*s +...+Lg7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log(1+f) = f - s*(f - R) (if f is not too large) + * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) + * + * 3. Finally, log(x) = k*ln2 + log(1+f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log(x) is NaN with signal if x < 0 (including -INF) ; + * log(+INF) is +INF; log(0) is -INF with signal; + * log(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +static const double +Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ +Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ +Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ +Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ +Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ +Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ +Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ + +/* + * We always inline __log1p(), since doing so produces a + * substantial performance improvement (~40% on amd64). + */ +static inline double __log1p(double f) +{ + double hfsq,s,z,R,w,t1,t2; + + s = f/(2.0+f); + z = s*s; + w = z*z; + t1= w*(Lg2+w*(Lg4+w*Lg6)); + t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); + R = t2+t1; + hfsq = 0.5*f*f; + return s*(hfsq+R); +} diff --git a/05/musl-final/src/math/__log1pf.h b/05/musl-final/src/math/__log1pf.h new file mode 100644 index 0000000..99492c5 --- /dev/null +++ b/05/musl-final/src/math/__log1pf.h @@ -0,0 +1,35 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/k_logf.h */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* + * See comments in __log1p.h. + */ + +/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */ +static const float +Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */ +Lg2 = 0xccce13.0p-25, /* 0.40000972152 */ +Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */ +Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */ + +static inline float __log1pf(float f) +{ + float hfsq,s,z,R,w,t1,t2; + + s = f/(2.0f + f); + z = s*s; + w = z*z; + t1 = w*(Lg2+w*Lg4); + t2 = z*(Lg1+w*Lg3); + R = t2+t1; + hfsq = 0.5f * f * f; + return s*(hfsq+R); +} diff --git a/05/musl-final/src/math/__x86_64/e_sqrt.s b/05/musl-final/src/math/__x86_64/e_sqrt.s new file mode 100644 index 0000000..64de7d6 --- /dev/null +++ b/05/musl-final/src/math/__x86_64/e_sqrt.s @@ -0,0 +1,3 @@ +.global sqrt +sqrt: sqrtsd %xmm0, %xmm0 + ret diff --git a/05/musl-final/src/math/__x86_64/e_sqrtf.s b/05/musl-final/src/math/__x86_64/e_sqrtf.s new file mode 100644 index 0000000..2c77076 --- /dev/null +++ b/05/musl-final/src/math/__x86_64/e_sqrtf.s @@ -0,0 +1,3 @@ +.global sqrtf +sqrtf: sqrtss %xmm0, %xmm0 + ret diff --git a/05/musl-final/src/math/e_acos.c b/05/musl-final/src/math/e_acos.c new file mode 100644 index 0000000..e023639 --- /dev/null +++ b/05/musl-final/src/math/e_acos.c @@ -0,0 +1,99 @@ +/* @(#)e_acos.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* acos(x) + * Method : + * acos(x) = pi/2 - asin(x) + * acos(-x) = pi/2 + asin(x) + * For |x|<=0.5 + * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) + * For x>0.5 + * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) + * = 2asin(sqrt((1-x)/2)) + * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) + * = 2f + (2c + 2s*z*R(z)) + * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term + * for f so that f+c ~ sqrt(z). + * For x<-0.5 + * acos(x) = pi - 2asin(sqrt((1-|x|)/2)) + * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z) + * + * Special cases: + * if x is NaN, return x itself; + * if |x|>1, return NaN with invalid signal. + * + * Function needed: sqrt + */ + +#include +#include "math_private.h" + +static const double +one= 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ +pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */ +pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ +pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ +pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ +pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ +pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ +pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ +pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ +pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ +qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ +qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ +qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ +qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ + +double +acos(double x) +{ + double z,p,q,r,w,s,c,df; + int32_t hx,ix; + GET_HIGH_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x3ff00000) { /* |x| >= 1 */ + uint32_t lx; + GET_LOW_WORD(lx,x); + if(((ix-0x3ff00000)|lx)==0) { /* |x|==1 */ + if(hx>0) return 0.0; /* acos(1) = 0 */ + else return pi+2.0*pio2_lo; /* acos(-1)= pi */ + } + return (x-x)/(x-x); /* acos(|x|>1) is NaN */ + } + if(ix<0x3fe00000) { /* |x| < 0.5 */ + if(ix<=0x3c600000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/ + z = x*x; + p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); + q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); + r = p/q; + return pio2_hi - (x - (pio2_lo-x*r)); + } else if (hx<0) { /* x < -0.5 */ + z = (one+x)*0.5; + p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); + q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); + s = sqrt(z); + r = p/q; + w = r*s-pio2_lo; + return pi - 2.0*(s+w); + } else { /* x > 0.5 */ + z = (one-x)*0.5; + s = sqrt(z); + df = s; + SET_LOW_WORD(df,0); + c = (z-df*df)/(s+df); + p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); + q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); + r = p/q; + w = r*s+c; + return 2.0*(df+w); + } +} diff --git a/05/musl-final/src/math/e_acosf.c b/05/musl-final/src/math/e_acosf.c new file mode 100644 index 0000000..4c59781 --- /dev/null +++ b/05/musl-final/src/math/e_acosf.c @@ -0,0 +1,77 @@ +/* e_acosf.c -- float version of e_acos.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +one = 1.0000000000e+00, /* 0x3F800000 */ +pi = 3.1415925026e+00, /* 0x40490fda */ +pio2_hi = 1.5707962513e+00, /* 0x3fc90fda */ +pio2_lo = 7.5497894159e-08, /* 0x33a22168 */ +pS0 = 1.6666667163e-01, /* 0x3e2aaaab */ +pS1 = -3.2556581497e-01, /* 0xbea6b090 */ +pS2 = 2.0121252537e-01, /* 0x3e4e0aa8 */ +pS3 = -4.0055535734e-02, /* 0xbd241146 */ +pS4 = 7.9153501429e-04, /* 0x3a4f7f04 */ +pS5 = 3.4793309169e-05, /* 0x3811ef08 */ +qS1 = -2.4033949375e+00, /* 0xc019d139 */ +qS2 = 2.0209457874e+00, /* 0x4001572d */ +qS3 = -6.8828397989e-01, /* 0xbf303361 */ +qS4 = 7.7038154006e-02; /* 0x3d9dc62e */ + +float +acosf(float x) +{ + float z,p,q,r,w,s,c,df; + int32_t hx,ix; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix==0x3f800000) { /* |x|==1 */ + if(hx>0) return 0.0; /* acos(1) = 0 */ + else return pi+(float)2.0*pio2_lo; /* acos(-1)= pi */ + } else if(ix>0x3f800000) { /* |x| >= 1 */ + return (x-x)/(x-x); /* acos(|x|>1) is NaN */ + } + if(ix<0x3f000000) { /* |x| < 0.5 */ + if(ix<=0x23000000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/ + z = x*x; + p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); + q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); + r = p/q; + return pio2_hi - (x - (pio2_lo-x*r)); + } else if (hx<0) { /* x < -0.5 */ + z = (one+x)*(float)0.5; + p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); + q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); + s = sqrtf(z); + r = p/q; + w = r*s-pio2_lo; + return pi - (float)2.0*(s+w); + } else { /* x > 0.5 */ + int32_t idf; + z = (one-x)*(float)0.5; + s = sqrtf(z); + df = s; + GET_FLOAT_WORD(idf,df); + SET_FLOAT_WORD(df,idf&0xfffff000); + c = (z-df*df)/(s+df); + p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); + q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); + r = p/q; + w = r*s+c; + return (float)2.0*(df+w); + } +} diff --git a/05/musl-final/src/math/e_acosh.c b/05/musl-final/src/math/e_acosh.c new file mode 100644 index 0000000..8b454e7 --- /dev/null +++ b/05/musl-final/src/math/e_acosh.c @@ -0,0 +1,59 @@ + +/* @(#)e_acosh.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* acosh(x) + * Method : + * Based on + * acosh(x) = log [ x + sqrt(x*x-1) ] + * we have + * acosh(x) := log(x)+ln2, if x is large; else + * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else + * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1. + * + * Special cases: + * acosh(x) is NaN with signal if x<1. + * acosh(NaN) is NaN without signal. + */ + +#include +#include "math_private.h" + +static const double +one = 1.0, +ln2 = 6.93147180559945286227e-01; /* 0x3FE62E42, 0xFEFA39EF */ + +double +acosh(double x) +{ + double t; + int32_t hx; + uint32_t lx; + EXTRACT_WORDS(hx,lx,x); + if(hx<0x3ff00000) { /* x < 1 */ + return (x-x)/(x-x); + } else if(hx >=0x41b00000) { /* x > 2**28 */ + if(hx >=0x7ff00000) { /* x is inf of NaN */ + return x+x; + } else + return log(x)+ln2; /* acosh(huge)=log(2x) */ + } else if(((hx-0x3ff00000)|lx)==0) { + return 0.0; /* acosh(1) = 0 */ + } else if (hx > 0x40000000) { /* 2**28 > x > 2 */ + t=x*x; + return log(2.0*x-one/(x+sqrt(t-one))); + } else { /* 1 +#include "math_private.h" + +static const float +one = 1.0, +ln2 = 6.9314718246e-01; /* 0x3f317218 */ + +float +acoshf(float x) +{ + float t; + int32_t hx; + GET_FLOAT_WORD(hx,x); + if(hx<0x3f800000) { /* x < 1 */ + return (x-x)/(x-x); + } else if(hx >=0x4d800000) { /* x > 2**28 */ + if(hx >=0x7f800000) { /* x is inf of NaN */ + return x+x; + } else + return logf(x)+ln2; /* acosh(huge)=log(2x) */ + } else if (hx==0x3f800000) { + return 0.0; /* acosh(1) = 0 */ + } else if (hx > 0x40000000) { /* 2**28 > x > 2 */ + t=x*x; + return logf((float)2.0*x-one/(x+sqrtf(t-one))); + } else { /* 10.98 + * asin(x) = pi/2 - 2*(s+s*z*R(z)) + * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) + * For x<=0.98, let pio4_hi = pio2_hi/2, then + * f = hi part of s; + * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z) + * and + * asin(x) = pi/2 - 2*(s+s*z*R(z)) + * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) + * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c)) + * + * Special cases: + * if x is NaN, return x itself; + * if |x|>1, return NaN with invalid signal. + * + */ + + +#include +#include "math_private.h" + +static const double +one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ +huge = 1.000e+300, +pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ +pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ +pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */ + /* coefficient for R(x^2) */ +pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ +pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ +pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ +pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ +pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ +pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ +qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ +qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ +qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ +qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ + +double +asin(double x) +{ + double t=0.0,w,p,q,c,r,s; + int32_t hx,ix; + GET_HIGH_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>= 0x3ff00000) { /* |x|>= 1 */ + uint32_t lx; + GET_LOW_WORD(lx,x); + if(((ix-0x3ff00000)|lx)==0) + /* asin(1)=+-pi/2 with inexact */ + return x*pio2_hi+x*pio2_lo; + return (x-x)/(x-x); /* asin(|x|>1) is NaN */ + } else if (ix<0x3fe00000) { /* |x|<0.5 */ + if(ix<0x3e400000) { /* if |x| < 2**-27 */ + if(huge+x>one) return x;/* return x with inexact if x!=0*/ + } else + t = x*x; + p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); + q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); + w = p/q; + return x+x*w; + } + /* 1> |x|>= 0.5 */ + w = one-fabs(x); + t = w*0.5; + p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); + q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); + s = sqrt(t); + if(ix>=0x3FEF3333) { /* if |x| > 0.975 */ + w = p/q; + t = pio2_hi-(2.0*(s+s*w)-pio2_lo); + } else { + w = s; + SET_LOW_WORD(w,0); + c = (t-w*w)/(s+w); + r = p/q; + p = 2.0*s*r-(pio2_lo-2.0*c); + q = pio4_hi-2.0*w; + t = pio4_hi-(p-q); + } + if(hx>0) return t; else return -t; +} diff --git a/05/musl-final/src/math/e_asinf.c b/05/musl-final/src/math/e_asinf.c new file mode 100644 index 0000000..9c69397 --- /dev/null +++ b/05/musl-final/src/math/e_asinf.c @@ -0,0 +1,80 @@ +/* e_asinf.c -- float version of e_asin.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +one = 1.0000000000e+00, /* 0x3F800000 */ +huge = 1.000e+30, +pio2_hi = 1.5707962513e+00, /* 0x3fc90fda */ +pio2_lo = 7.5497894159e-08, /* 0x33a22168 */ +pio4_hi = 7.8539818525e-01, /* 0x3f490fdb */ + /* coefficient for R(x^2) */ +pS0 = 1.6666667163e-01, /* 0x3e2aaaab */ +pS1 = -3.2556581497e-01, /* 0xbea6b090 */ +pS2 = 2.0121252537e-01, /* 0x3e4e0aa8 */ +pS3 = -4.0055535734e-02, /* 0xbd241146 */ +pS4 = 7.9153501429e-04, /* 0x3a4f7f04 */ +pS5 = 3.4793309169e-05, /* 0x3811ef08 */ +qS1 = -2.4033949375e+00, /* 0xc019d139 */ +qS2 = 2.0209457874e+00, /* 0x4001572d */ +qS3 = -6.8828397989e-01, /* 0xbf303361 */ +qS4 = 7.7038154006e-02; /* 0x3d9dc62e */ + +float +asinf(float x) +{ + float t=0.0,w,p,q,c,r,s; + int32_t hx,ix; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix==0x3f800000) { + /* asin(1)=+-pi/2 with inexact */ + return x*pio2_hi+x*pio2_lo; + } else if(ix> 0x3f800000) { /* |x|>= 1 */ + return (x-x)/(x-x); /* asin(|x|>1) is NaN */ + } else if (ix<0x3f000000) { /* |x|<0.5 */ + if(ix<0x32000000) { /* if |x| < 2**-27 */ + if(huge+x>one) return x;/* return x with inexact if x!=0*/ + } else + t = x*x; + p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); + q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); + w = p/q; + return x+x*w; + } + /* 1> |x|>= 0.5 */ + w = one-fabsf(x); + t = w*(float)0.5; + p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); + q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); + s = sqrtf(t); + if(ix>=0x3F79999A) { /* if |x| > 0.975 */ + w = p/q; + t = pio2_hi-((float)2.0*(s+s*w)-pio2_lo); + } else { + int32_t iw; + w = s; + GET_FLOAT_WORD(iw,w); + SET_FLOAT_WORD(w,iw&0xfffff000); + c = (t-w*w)/(s+w); + r = p/q; + p = (float)2.0*s*r-(pio2_lo-(float)2.0*c); + q = pio4_hi-(float)2.0*w; + t = pio4_hi-(p-q); + } + if(hx>0) return t; else return -t; +} diff --git a/05/musl-final/src/math/e_atan2.c b/05/musl-final/src/math/e_atan2.c new file mode 100644 index 0000000..dd02116 --- /dev/null +++ b/05/musl-final/src/math/e_atan2.c @@ -0,0 +1,120 @@ + +/* @(#)e_atan2.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* atan2(y,x) + * Method : + * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). + * 2. Reduce x to positive by (if x and y are unexceptional): + * ARG (x+iy) = arctan(y/x) ... if x > 0, + * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, + * + * Special cases: + * + * ATAN2((anything), NaN ) is NaN; + * ATAN2(NAN , (anything) ) is NaN; + * ATAN2(+-0, +(anything but NaN)) is +-0 ; + * ATAN2(+-0, -(anything but NaN)) is +-pi ; + * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; + * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; + * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; + * ATAN2(+-INF,+INF ) is +-pi/4 ; + * ATAN2(+-INF,-INF ) is +-3pi/4; + * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const double +tiny = 1.0e-300, +zero = 0.0, +pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ +pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ +pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ +pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ + +double +atan2(double y, double x) +{ + double z; + int32_t k,m,hx,hy,ix,iy; + uint32_t lx,ly; + + EXTRACT_WORDS(hx,lx,x); + ix = hx&0x7fffffff; + EXTRACT_WORDS(hy,ly,y); + iy = hy&0x7fffffff; + if(((ix|((lx|-lx)>>31))>0x7ff00000)|| + ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ + return x+y; + if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */ + m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ + + /* when y = 0 */ + if((iy|ly)==0) { + switch(m) { + case 0: + case 1: return y; /* atan(+-0,+anything)=+-0 */ + case 2: return pi+tiny;/* atan(+0,-anything) = pi */ + case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ + } + } + /* when x = 0 */ + if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; + + /* when x is INF */ + if(ix==0x7ff00000) { + if(iy==0x7ff00000) { + switch(m) { + case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ + case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ + case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ + case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ + } + } else { + switch(m) { + case 0: return zero ; /* atan(+...,+INF) */ + case 1: return -zero ; /* atan(-...,+INF) */ + case 2: return pi+tiny ; /* atan(+...,-INF) */ + case 3: return -pi-tiny ; /* atan(-...,-INF) */ + } + } + } + /* when y is INF */ + if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; + + /* compute y/x */ + k = (iy-ix)>>20; + if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ + else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ + else z=atan(fabs(y/x)); /* safe to do y/x */ + switch (m) { + case 0: return z ; /* atan(+,+) */ + case 1: { + uint32_t zh; + GET_HIGH_WORD(zh,z); + SET_HIGH_WORD(z,zh ^ 0x80000000); + } + return z ; /* atan(-,+) */ + case 2: return pi-(z-pi_lo);/* atan(+,-) */ + default: /* case 3 */ + return (z-pi_lo)-pi;/* atan(-,-) */ + } +} diff --git a/05/musl-final/src/math/e_atan2f.c b/05/musl-final/src/math/e_atan2f.c new file mode 100644 index 0000000..535e10a --- /dev/null +++ b/05/musl-final/src/math/e_atan2f.c @@ -0,0 +1,93 @@ +/* e_atan2f.c -- float version of e_atan2.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +tiny = 1.0e-30, +zero = 0.0, +pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */ +pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */ +pi = 3.1415927410e+00, /* 0x40490fdb */ +pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */ + +float +atan2f(float y, float x) +{ + float z; + int32_t k,m,hx,hy,ix,iy; + + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + GET_FLOAT_WORD(hy,y); + iy = hy&0x7fffffff; + if((ix>0x7f800000)|| + (iy>0x7f800000)) /* x or y is NaN */ + return x+y; + if(hx==0x3f800000) return atanf(y); /* x=1.0 */ + m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ + + /* when y = 0 */ + if(iy==0) { + switch(m) { + case 0: + case 1: return y; /* atan(+-0,+anything)=+-0 */ + case 2: return pi+tiny;/* atan(+0,-anything) = pi */ + case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ + } + } + /* when x = 0 */ + if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; + + /* when x is INF */ + if(ix==0x7f800000) { + if(iy==0x7f800000) { + switch(m) { + case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ + case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ + case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ + case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ + } + } else { + switch(m) { + case 0: return zero ; /* atan(+...,+INF) */ + case 1: return -zero ; /* atan(-...,+INF) */ + case 2: return pi+tiny ; /* atan(+...,-INF) */ + case 3: return -pi-tiny ; /* atan(-...,-INF) */ + } + } + } + /* when y is INF */ + if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; + + /* compute y/x */ + k = (iy-ix)>>23; + if(k > 60) z=pi_o_2+(float)0.5*pi_lo; /* |y/x| > 2**60 */ + else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ + else z=atanf(fabsf(y/x)); /* safe to do y/x */ + switch (m) { + case 0: return z ; /* atan(+,+) */ + case 1: { + uint32_t zh; + GET_FLOAT_WORD(zh,z); + SET_FLOAT_WORD(z,zh ^ 0x80000000); + } + return z ; /* atan(-,+) */ + case 2: return pi-(z-pi_lo);/* atan(+,-) */ + default: /* case 3 */ + return (z-pi_lo)-pi;/* atan(-,-) */ + } +} diff --git a/05/musl-final/src/math/e_atanh.c b/05/musl-final/src/math/e_atanh.c new file mode 100644 index 0000000..45f1c96 --- /dev/null +++ b/05/musl-final/src/math/e_atanh.c @@ -0,0 +1,59 @@ + +/* @(#)e_atanh.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* atanh(x) + * Method : + * 1.Reduced x to positive by atanh(-x) = -atanh(x) + * 2.For x>=0.5 + * 1 2x x + * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------) + * 2 1 - x 1 - x + * + * For x<0.5 + * atanh(x) = 0.5*log1p(2x+2x*x/(1-x)) + * + * Special cases: + * atanh(x) is NaN if |x| > 1 with signal; + * atanh(NaN) is that NaN with no signal; + * atanh(+-1) is +-INF with signal. + * + */ + +#include +#include "math_private.h" + +static const double one = 1.0, huge = 1e300; +static const double zero = 0.0; + +double +atanh(double x) +{ + double t; + int32_t hx,ix; + uint32_t lx; + EXTRACT_WORDS(hx,lx,x); + ix = hx&0x7fffffff; + if ((ix|((lx|(-lx))>>31))>0x3ff00000) /* |x|>1 */ + return (x-x)/(x-x); + if(ix==0x3ff00000) + return x/zero; + if(ix<0x3e300000&&(huge+x)>zero) return x; /* x<2**-28 */ + SET_HIGH_WORD(x,ix); + if(ix<0x3fe00000) { /* x < 0.5 */ + t = x+x; + t = 0.5*log1p(t+t*x/(one-x)); + } else + t = 0.5*log1p((x+x)/(one-x)); + if(hx>=0) return t; else return -t; +} diff --git a/05/musl-final/src/math/e_atanhf.c b/05/musl-final/src/math/e_atanhf.c new file mode 100644 index 0000000..7356cfc --- /dev/null +++ b/05/musl-final/src/math/e_atanhf.c @@ -0,0 +1,42 @@ +/* e_atanhf.c -- float version of e_atanh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one = 1.0, huge = 1e30; + +static const float zero = 0.0; + +float +atanhf(float x) +{ + float t; + int32_t hx,ix; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if (ix>0x3f800000) /* |x|>1 */ + return (x-x)/(x-x); + if(ix==0x3f800000) + return x/zero; + if(ix<0x31800000&&(huge+x)>zero) return x; /* x<2**-28 */ + SET_FLOAT_WORD(x,ix); + if(ix<0x3f000000) { /* x < 0.5 */ + t = x+x; + t = (float)0.5*log1pf(t+t*x/(one-x)); + } else + t = (float)0.5*log1pf((x+x)/(one-x)); + if(hx>=0) return t; else return -t; +} diff --git a/05/musl-final/src/math/e_cosh.c b/05/musl-final/src/math/e_cosh.c new file mode 100644 index 0000000..ad425bd --- /dev/null +++ b/05/musl-final/src/math/e_cosh.c @@ -0,0 +1,82 @@ + +/* @(#)e_cosh.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* cosh(x) + * Method : + * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2 + * 1. Replace x by |x| (cosh(x) = cosh(-x)). + * 2. + * [ exp(x) - 1 ]^2 + * 0 <= x <= ln2/2 : cosh(x) := 1 + ------------------- + * 2*exp(x) + * + * exp(x) + 1/exp(x) + * ln2/2 <= x <= 22 : cosh(x) := ------------------- + * 2 + * 22 <= x <= lnovft : cosh(x) := exp(x)/2 + * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2) + * ln2ovft < x : cosh(x) := huge*huge (overflow) + * + * Special cases: + * cosh(x) is |x| if x is +INF, -INF, or NaN. + * only cosh(0)=1 is exact for finite x. + */ + +#include +#include "math_private.h" + +static const double one = 1.0, half=0.5, huge = 1.0e300; + +double +cosh(double x) +{ + double t,w; + int32_t ix; + uint32_t lx; + + /* High word of |x|. */ + GET_HIGH_WORD(ix,x); + ix &= 0x7fffffff; + + /* x is INF or NaN */ + if(ix>=0x7ff00000) return x*x; + + /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */ + if(ix<0x3fd62e43) { + t = expm1(fabs(x)); + w = one+t; + if (ix<0x3c800000) return w; /* cosh(tiny) = 1 */ + return one+(t*t)/(w+w); + } + + /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */ + if (ix < 0x40360000) { + t = exp(fabs(x)); + return half*t+half/t; + } + + /* |x| in [22, log(maxdouble)] return half*exp(|x|) */ + if (ix < 0x40862E42) return half*exp(fabs(x)); + + /* |x| in [log(maxdouble), overflowthresold] */ + GET_LOW_WORD(lx,x); + if (ix<0x408633CE || + ((ix==0x408633ce)&&(lx<=(uint32_t)0x8fb9f87d))) { + w = exp(half*fabs(x)); + t = half*w; + return t*w; + } + + /* |x| > overflowthresold, cosh(x) overflow */ + return huge*huge; +} diff --git a/05/musl-final/src/math/e_coshf.c b/05/musl-final/src/math/e_coshf.c new file mode 100644 index 0000000..6db1088 --- /dev/null +++ b/05/musl-final/src/math/e_coshf.c @@ -0,0 +1,59 @@ +/* e_coshf.c -- float version of e_cosh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one = 1.0, half=0.5, huge = 1.0e30; + +float +coshf(float x) +{ + float t,w; + int32_t ix; + + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; + + /* x is INF or NaN */ + if(ix>=0x7f800000) return x*x; + + /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */ + if(ix<0x3eb17218) { + t = expm1f(fabsf(x)); + w = one+t; + if (ix<0x24000000) return w; /* cosh(tiny) = 1 */ + return one+(t*t)/(w+w); + } + + /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */ + if (ix < 0x41b00000) { + t = expf(fabsf(x)); + return half*t+half/t; + } + + /* |x| in [22, log(maxdouble)] return half*exp(|x|) */ + if (ix < 0x42b17180) return half*expf(fabsf(x)); + + /* |x| in [log(maxdouble), overflowthresold] */ + if (ix<=0x42b2d4fc) { + w = expf(half*fabsf(x)); + t = half*w; + return t*w; + } + + /* |x| > overflowthresold, cosh(x) overflow */ + return huge*huge; +} diff --git a/05/musl-final/src/math/e_exp.c b/05/musl-final/src/math/e_exp.c new file mode 100644 index 0000000..66107b9 --- /dev/null +++ b/05/musl-final/src/math/e_exp.c @@ -0,0 +1,155 @@ + +/* @(#)e_exp.c 1.6 04/04/22 */ +/* + * ==================================================== + * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. + * + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* exp(x) + * Returns the exponential of x. + * + * Method + * 1. Argument reduction: + * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. + * Given x, find r and integer k such that + * + * x = k*ln2 + r, |r| <= 0.5*ln2. + * + * Here r will be represented as r = hi-lo for better + * accuracy. + * + * 2. Approximation of exp(r) by a special rational function on + * the interval [0,0.34658]: + * Write + * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... + * We use a special Remes algorithm on [0,0.34658] to generate + * a polynomial of degree 5 to approximate R. The maximum error + * of this polynomial approximation is bounded by 2**-59. In + * other words, + * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 + * (where z=r*r, and the values of P1 to P5 are listed below) + * and + * | 5 | -59 + * | 2.0+P1*z+...+P5*z - R(z) | <= 2 + * | | + * The computation of exp(r) thus becomes + * 2*r + * exp(r) = 1 + ------- + * R - r + * r*R1(r) + * = 1 + r + ----------- (for better accuracy) + * 2 - R1(r) + * where + * 2 4 10 + * R1(r) = r - (P1*r + P2*r + ... + P5*r ). + * + * 3. Scale back to obtain exp(x): + * From step 1, we have + * exp(x) = 2^k * exp(r) + * + * Special cases: + * exp(INF) is INF, exp(NaN) is NaN; + * exp(-INF) is 0, and + * for finite argument, only exp(0)=1 is exact. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Misc. info. + * For IEEE double + * if x > 7.09782712893383973096e+02 then exp(x) overflow + * if x < -7.45133219101941108420e+02 then exp(x) underflow + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const double +one = 1.0, +halF[2] = {0.5,-0.5,}, +huge = 1.0e+300, +twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/ +o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */ +u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */ +ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ + -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */ +ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ + -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */ +invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ +P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ +P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ +P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ +P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ +P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ + + +double +exp(double x) /* default IEEE double exp */ +{ + double y,hi=0.0,lo=0.0,c,t; + int32_t k=0,xsb; + uint32_t hx; + + GET_HIGH_WORD(hx,x); + xsb = (hx>>31)&1; /* sign bit of x */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out non-finite argument */ + if(hx >= 0x40862E42) { /* if |x|>=709.78... */ + if(hx>=0x7ff00000) { + uint32_t lx; + GET_LOW_WORD(lx,x); + if(((hx&0xfffff)|lx)!=0) + return x+x; /* NaN */ + else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ + } + if(x > o_threshold) return huge*huge; /* overflow */ + if(x < u_threshold) return twom1000*twom1000; /* underflow */ + } + + /* argument reduction */ + if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ + if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ + hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; + } else { + k = (int)(invln2*x+halF[xsb]); + t = k; + hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ + lo = t*ln2LO[0]; + } + x = hi - lo; + } + else if(hx < 0x3e300000) { /* when |x|<2**-28 */ + if(huge+x>one) return one+x;/* trigger inexact */ + } + else k = 0; + + /* x is now in primary range */ + t = x*x; + c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); + if(k==0) return one-((x*c)/(c-2.0)-x); + else y = one-((lo-(x*c)/(2.0-c))-hi); + if(k >= -1021) { + uint32_t hy; + GET_HIGH_WORD(hy,y); + SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */ + return y; + } else { + uint32_t hy; + GET_HIGH_WORD(hy,y); + SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */ + return y*twom1000; + } +} diff --git a/05/musl-final/src/math/e_expf.c b/05/musl-final/src/math/e_expf.c new file mode 100644 index 0000000..99818ed --- /dev/null +++ b/05/musl-final/src/math/e_expf.c @@ -0,0 +1,91 @@ +/* e_expf.c -- float version of e_exp.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +one = 1.0, +halF[2] = {0.5,-0.5,}, +huge = 1.0e+30, +twom100 = 7.8886090522e-31, /* 2**-100=0x0d800000 */ +o_threshold= 8.8721679688e+01, /* 0x42b17180 */ +u_threshold= -1.0397208405e+02, /* 0xc2cff1b5 */ +ln2HI[2] ={ 6.9313812256e-01, /* 0x3f317180 */ + -6.9313812256e-01,}, /* 0xbf317180 */ +ln2LO[2] ={ 9.0580006145e-06, /* 0x3717f7d1 */ + -9.0580006145e-06,}, /* 0xb717f7d1 */ +invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */ +P1 = 1.6666667163e-01, /* 0x3e2aaaab */ +P2 = -2.7777778450e-03, /* 0xbb360b61 */ +P3 = 6.6137559770e-05, /* 0x388ab355 */ +P4 = -1.6533901999e-06, /* 0xb5ddea0e */ +P5 = 4.1381369442e-08; /* 0x3331bb4c */ + +float +expf(float x) /* default IEEE double exp */ +{ + float y,hi=0.0,lo=0.0,c,t; + int32_t k=0,xsb; + uint32_t hx; + + GET_FLOAT_WORD(hx,x); + xsb = (hx>>31)&1; /* sign bit of x */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out non-finite argument */ + if(hx >= 0x42b17218) { /* if |x|>=88.721... */ + if(hx>0x7f800000) + return x+x; /* NaN */ + if(hx==0x7f800000) + return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ + if(x > o_threshold) return huge*huge; /* overflow */ + if(x < u_threshold) return twom100*twom100; /* underflow */ + } + + /* argument reduction */ + if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ + if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ + hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; + } else { + k = invln2*x+halF[xsb]; + t = k; + hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ + lo = t*ln2LO[0]; + } + x = hi - lo; + } + else if(hx < 0x31800000) { /* when |x|<2**-28 */ + if(huge+x>one) return one+x;/* trigger inexact */ + } + else k = 0; + + /* x is now in primary range */ + t = x*x; + c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); + if(k==0) return one-((x*c)/(c-(float)2.0)-x); + else y = one-((lo-(x*c)/((float)2.0-c))-hi); + if(k >= -125) { + uint32_t hy; + GET_FLOAT_WORD(hy,y); + SET_FLOAT_WORD(y,hy+(k<<23)); /* add k to y's exponent */ + return y; + } else { + uint32_t hy; + GET_FLOAT_WORD(hy,y); + SET_FLOAT_WORD(y,hy+((k+100)<<23)); /* add k to y's exponent */ + return y*twom100; + } +} diff --git a/05/musl-final/src/math/e_fmod.c b/05/musl-final/src/math/e_fmod.c new file mode 100644 index 0000000..99afe48 --- /dev/null +++ b/05/musl-final/src/math/e_fmod.c @@ -0,0 +1,129 @@ + +/* @(#)e_fmod.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fmod(x,y) + * Return x mod y in exact arithmetic + * Method: shift and subtract + */ + +#include +#include "math_private.h" + +static const double one = 1.0, Zero[] = {0.0, -0.0,}; + +double +fmod(double x, double y) +{ + int32_t n,hx,hy,hz,ix,iy,sx,i; + uint32_t lx,ly,lz; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hy,ly,y); + sx = hx&0x80000000; /* sign of x */ + hx ^=sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ + ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ + return (x*y)/(x*y); + if(hx<=hy) { + if((hx>31]; /* |x|=|y| return x*0*/ + } + + /* determine ix = ilogb(x) */ + if(hx<0x00100000) { /* subnormal x */ + if(hx==0) { + for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; + } else { + for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; + } + } else ix = (hx>>20)-1023; + + /* determine iy = ilogb(y) */ + if(hy<0x00100000) { /* subnormal y */ + if(hy==0) { + for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; + } else { + for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; + } + } else iy = (hy>>20)-1023; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if(ix >= -1022) + hx = 0x00100000|(0x000fffff&hx); + else { /* subnormal x, shift x to normal */ + n = -1022-ix; + if(n<=31) { + hx = (hx<>(32-n)); + lx <<= n; + } else { + hx = lx<<(n-32); + lx = 0; + } + } + if(iy >= -1022) + hy = 0x00100000|(0x000fffff&hy); + else { /* subnormal y, shift y to normal */ + n = -1022-iy; + if(n<=31) { + hy = (hy<>(32-n)); + ly <<= n; + } else { + hy = ly<<(n-32); + ly = 0; + } + } + + /* fix point fmod */ + n = ix - iy; + while(n--) { + hz=hx-hy;lz=lx-ly; if(lx>31); lx = lx+lx;} + else { + if((hz|lz)==0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx>>31]; + hx = hz+hz+(lz>>31); lx = lz+lz; + } + } + hz=hx-hy;lz=lx-ly; if(lx=0) {hx=hz;lx=lz;} + + /* convert back to floating value and restore the sign */ + if((hx|lx)==0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx>>31]; + while(hx<0x00100000) { /* normalize x */ + hx = hx+hx+(lx>>31); lx = lx+lx; + iy -= 1; + } + if(iy>= -1022) { /* normalize output */ + hx = ((hx-0x00100000)|((iy+1023)<<20)); + INSERT_WORDS(x,hx|sx,lx); + } else { /* subnormal output */ + n = -1022 - iy; + if(n<=20) { + lx = (lx>>n)|((uint32_t)hx<<(32-n)); + hx >>= n; + } else if (n<=31) { + lx = (hx<<(32-n))|(lx>>n); hx = sx; + } else { + lx = hx>>(n-32); hx = sx; + } + INSERT_WORDS(x,hx|sx,lx); + x *= one; /* create necessary signal */ + } + return x; /* exact output */ +} diff --git a/05/musl-final/src/math/e_fmodf.c b/05/musl-final/src/math/e_fmodf.c new file mode 100644 index 0000000..fe86cb0 --- /dev/null +++ b/05/musl-final/src/math/e_fmodf.c @@ -0,0 +1,101 @@ +/* e_fmodf.c -- float version of e_fmod.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fmodf(x,y) + * Return x mod y in exact arithmetic + * Method: shift and subtract + */ + +#include +#include "math_private.h" + +static const float one = 1.0, Zero[] = {0.0, -0.0,}; + +float +fmodf(float x, float y) +{ + int32_t n,hx,hy,hz,ix,iy,sx,i; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hy,y); + sx = hx&0x80000000; /* sign of x */ + hx ^=sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if(hy==0||(hx>=0x7f800000)|| /* y=0,or x not finite */ + (hy>0x7f800000)) /* or y is NaN */ + return (x*y)/(x*y); + if(hx>31]; /* |x|=|y| return x*0*/ + + /* determine ix = ilogb(x) */ + if(hx<0x00800000) { /* subnormal x */ + for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1; + } else ix = (hx>>23)-127; + + /* determine iy = ilogb(y) */ + if(hy<0x00800000) { /* subnormal y */ + for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1; + } else iy = (hy>>23)-127; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if(ix >= -126) + hx = 0x00800000|(0x007fffff&hx); + else { /* subnormal x, shift x to normal */ + n = -126-ix; + hx = hx<= -126) + hy = 0x00800000|(0x007fffff&hy); + else { /* subnormal y, shift y to normal */ + n = -126-iy; + hy = hy<>31]; + hx = hz+hz; + } + } + hz=hx-hy; + if(hz>=0) {hx=hz;} + + /* convert back to floating value and restore the sign */ + if(hx==0) /* return sign(x)*0 */ + return Zero[(uint32_t)sx>>31]; + while(hx<0x00800000) { /* normalize x */ + hx = hx+hx; + iy -= 1; + } + if(iy>= -126) { /* normalize output */ + hx = ((hx-0x00800000)|((iy+127)<<23)); + SET_FLOAT_WORD(x,hx|sx); + } else { /* subnormal output */ + n = -126 - iy; + hx >>= n; + SET_FLOAT_WORD(x,hx|sx); + x *= one; /* create necessary signal */ + } + return x; /* exact output */ +} diff --git a/05/musl-final/src/math/e_hypot.c b/05/musl-final/src/math/e_hypot.c new file mode 100644 index 0000000..e925adc --- /dev/null +++ b/05/musl-final/src/math/e_hypot.c @@ -0,0 +1,121 @@ + +/* @(#)e_hypot.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* hypot(x,y) + * + * Method : + * If (assume round-to-nearest) z=x*x+y*y + * has error less than sqrt(2)/2 ulp, than + * sqrt(z) has error less than 1 ulp (exercise). + * + * So, compute sqrt(x*x+y*y) with some care as + * follows to get the error below 1 ulp: + * + * Assume x>y>0; + * (if possible, set rounding to round-to-nearest) + * 1. if x > 2y use + * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y + * where x1 = x with lower 32 bits cleared, x2 = x-x1; else + * 2. if x <= 2y use + * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y)) + * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, + * y1= y with lower 32 bits chopped, y2 = y-y1. + * + * NOTE: scaling may be necessary if some argument is too + * large or too tiny + * + * Special cases: + * hypot(x,y) is INF if x or y is +INF or -INF; else + * hypot(x,y) is NAN if x or y is NAN. + * + * Accuracy: + * hypot(x,y) returns sqrt(x^2+y^2) with error less + * than 1 ulps (units in the last place) + */ + +#include +#include "math_private.h" + +double +hypot(double x, double y) +{ + double a=x,b=y,t1,t2,y1,y2,w; + int32_t j,k,ha,hb; + + GET_HIGH_WORD(ha,x); + ha &= 0x7fffffff; + GET_HIGH_WORD(hb,y); + hb &= 0x7fffffff; + if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} + SET_HIGH_WORD(a,ha); /* a <- |a| */ + SET_HIGH_WORD(b,hb); /* b <- |b| */ + if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */ + k=0; + if(ha > 0x5f300000) { /* a>2**500 */ + if(ha >= 0x7ff00000) { /* Inf or NaN */ + uint32_t low; + w = a+b; /* for sNaN */ + GET_LOW_WORD(low,a); + if(((ha&0xfffff)|low)==0) w = a; + GET_LOW_WORD(low,b); + if(((hb^0x7ff00000)|low)==0) w = b; + return w; + } + /* scale a and b by 2**-600 */ + ha -= 0x25800000; hb -= 0x25800000; k += 600; + SET_HIGH_WORD(a,ha); + SET_HIGH_WORD(b,hb); + } + if(hb < 0x20b00000) { /* b < 2**-500 */ + if(hb <= 0x000fffff) { /* subnormal b or 0 */ + uint32_t low; + GET_LOW_WORD(low,b); + if((hb|low)==0) return a; + t1=0; + SET_HIGH_WORD(t1,0x7fd00000); /* t1=2^1022 */ + b *= t1; + a *= t1; + k -= 1022; + } else { /* scale a and b by 2^600 */ + ha += 0x25800000; /* a *= 2^600 */ + hb += 0x25800000; /* b *= 2^600 */ + k -= 600; + SET_HIGH_WORD(a,ha); + SET_HIGH_WORD(b,hb); + } + } + /* medium size a and b */ + w = a-b; + if (w>b) { + t1 = 0; + SET_HIGH_WORD(t1,ha); + t2 = a-t1; + w = sqrt(t1*t1-(b*(-b)-t2*(a+t1))); + } else { + a = a+a; + y1 = 0; + SET_HIGH_WORD(y1,hb); + y2 = b - y1; + t1 = 0; + SET_HIGH_WORD(t1,ha+0x00100000); + t2 = a - t1; + w = sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b))); + } + if(k!=0) { + uint32_t high; + t1 = 1.0; + GET_HIGH_WORD(high,t1); + SET_HIGH_WORD(t1,high+(k<<20)); + return t1*w; + } else return w; +} diff --git a/05/musl-final/src/math/e_hypotf.c b/05/musl-final/src/math/e_hypotf.c new file mode 100644 index 0000000..1377355 --- /dev/null +++ b/05/musl-final/src/math/e_hypotf.c @@ -0,0 +1,79 @@ +/* e_hypotf.c -- float version of e_hypot.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +float +hypotf(float x, float y) +{ + float a=x,b=y,t1,t2,y1,y2,w; + int32_t j,k,ha,hb; + + GET_FLOAT_WORD(ha,x); + ha &= 0x7fffffff; + GET_FLOAT_WORD(hb,y); + hb &= 0x7fffffff; + if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} + SET_FLOAT_WORD(a,ha); /* a <- |a| */ + SET_FLOAT_WORD(b,hb); /* b <- |b| */ + if((ha-hb)>0xf000000) {return a+b;} /* x/y > 2**30 */ + k=0; + if(ha > 0x58800000) { /* a>2**50 */ + if(ha >= 0x7f800000) { /* Inf or NaN */ + w = a+b; /* for sNaN */ + if(ha == 0x7f800000) w = a; + if(hb == 0x7f800000) w = b; + return w; + } + /* scale a and b by 2**-68 */ + ha -= 0x22000000; hb -= 0x22000000; k += 68; + SET_FLOAT_WORD(a,ha); + SET_FLOAT_WORD(b,hb); + } + if(hb < 0x26800000) { /* b < 2**-50 */ + if(hb <= 0x007fffff) { /* subnormal b or 0 */ + if(hb==0) return a; + SET_FLOAT_WORD(t1,0x7e800000); /* t1=2^126 */ + b *= t1; + a *= t1; + k -= 126; + } else { /* scale a and b by 2^68 */ + ha += 0x22000000; /* a *= 2^68 */ + hb += 0x22000000; /* b *= 2^68 */ + k -= 68; + SET_FLOAT_WORD(a,ha); + SET_FLOAT_WORD(b,hb); + } + } + /* medium size a and b */ + w = a-b; + if (w>b) { + SET_FLOAT_WORD(t1,ha&0xfffff000); + t2 = a-t1; + w = sqrtf(t1*t1-(b*(-b)-t2*(a+t1))); + } else { + a = a+a; + SET_FLOAT_WORD(y1,hb&0xfffff000); + y2 = b - y1; + SET_FLOAT_WORD(t1,ha+0x00800000); + t2 = a - t1; + w = sqrtf(t1*y1-(w*(-w)-(t1*y2+t2*b))); + } + if(k!=0) { + SET_FLOAT_WORD(t1,0x3f800000+(k<<23)); + return t1*w; + } else return w; +} diff --git a/05/musl-final/src/math/e_log.c b/05/musl-final/src/math/e_log.c new file mode 100644 index 0000000..9eb0e44 --- /dev/null +++ b/05/musl-final/src/math/e_log.c @@ -0,0 +1,131 @@ + +/* @(#)e_log.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* log(x) + * Return the logrithm of x + * + * Method : + * 1. Argument Reduction: find k and f such that + * x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * 2. Approximation of log(1+f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Reme algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s + * (the values of Lg1 to Lg7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lg1*s +...+Lg7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log(1+f) = f - s*(f - R) (if f is not too large) + * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) + * + * 3. Finally, log(x) = k*ln2 + log(1+f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log(x) is NaN with signal if x < 0 (including -INF) ; + * log(+INF) is +INF; log(0) is -INF with signal; + * log(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const double +ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ +ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ +two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ +Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ +Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ +Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ +Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ +Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ +Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ +Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ + +static const double zero = 0.0; + +double +log(double x) +{ + double hfsq,f,s,z,R,w,t1,t2,dk; + int32_t k,hx,i,j; + uint32_t lx; + + EXTRACT_WORDS(hx,lx,x); + + k=0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx&0x7fffffff)|lx)==0) + return -two54/zero; /* log(+-0)=-inf */ + if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ + k -= 54; x *= two54; /* subnormal number, scale up x */ + GET_HIGH_WORD(hx,x); + } + if (hx >= 0x7ff00000) return x+x; + k += (hx>>20)-1023; + hx &= 0x000fffff; + i = (hx+0x95f64)&0x100000; + SET_HIGH_WORD(x,hx|(i^0x3ff00000)); /* normalize x or x/2 */ + k += (i>>20); + f = x-1.0; + if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */ + if(f==zero) { if(k==0) return zero; else {dk=(double)k; + return dk*ln2_hi+dk*ln2_lo;} } + R = f*f*(0.5-0.33333333333333333*f); + if(k==0) return f-R; else {dk=(double)k; + return dk*ln2_hi-((R-dk*ln2_lo)-f);} + } + s = f/(2.0+f); + dk = (double)k; + z = s*s; + i = hx-0x6147a; + w = z*z; + j = 0x6b851-hx; + t1= w*(Lg2+w*(Lg4+w*Lg6)); + t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); + i |= j; + R = t2+t1; + if(i>0) { + hfsq=0.5*f*f; + if(k==0) return f-(hfsq-s*(hfsq+R)); else + return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); + } else { + if(k==0) return f-s*(f-R); else + return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f); + } +} diff --git a/05/musl-final/src/math/e_log10.c b/05/musl-final/src/math/e_log10.c new file mode 100644 index 0000000..3be179f --- /dev/null +++ b/05/musl-final/src/math/e_log10.c @@ -0,0 +1,83 @@ + +/* @(#)e_log10.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* log10(x) + * Return the base 10 logarithm of x + * + * Method : + * Let log10_2hi = leading 40 bits of log10(2) and + * log10_2lo = log10(2) - log10_2hi, + * ivln10 = 1/log(10) rounded. + * Then + * n = ilogb(x), + * if(n<0) n = n+1; + * x = scalbn(x,-n); + * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x)) + * + * Note 1: + * To guarantee log10(10**n)=n, where 10**n is normal, the rounding + * mode must set to Round-to-Nearest. + * Note 2: + * [1/log(10)] rounded to 53 bits has error .198 ulps; + * log10 is monotonic at all binary break points. + * + * Special cases: + * log10(x) is NaN with signal if x < 0; + * log10(+INF) is +INF with no signal; log10(0) is -INF with signal; + * log10(NaN) is that NaN with no signal; + * log10(10**N) = N for N=0,1,...,22. + * + * Constants: + * The hexadecimal values are the intended ones for the following constants. + * The decimal values may be used, provided that the compiler will convert + * from decimal to binary accurately enough to produce the hexadecimal values + * shown. + */ + +#include +#include "math_private.h" + +static const double +two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ +ivln10 = 4.34294481903251816668e-01, /* 0x3FDBCB7B, 0x1526E50E */ +log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */ +log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */ + +static const double zero = 0.0; + +double +log10(double x) +{ + double y,z; + int32_t i,k,hx; + uint32_t lx; + + EXTRACT_WORDS(hx,lx,x); + + k=0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx&0x7fffffff)|lx)==0) + return -two54/zero; /* log(+-0)=-inf */ + if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ + k -= 54; x *= two54; /* subnormal number, scale up x */ + GET_HIGH_WORD(hx,x); + } + if (hx >= 0x7ff00000) return x+x; + k += (hx>>20)-1023; + i = ((uint32_t)k&0x80000000)>>31; + hx = (hx&0x000fffff)|((0x3ff-i)<<20); + y = (double)(k+i); + SET_HIGH_WORD(x,hx); + z = y*log10_2lo + ivln10*log(x); + return z+y*log10_2hi; +} diff --git a/05/musl-final/src/math/e_log10f.c b/05/musl-final/src/math/e_log10f.c new file mode 100644 index 0000000..8fc5c5c --- /dev/null +++ b/05/musl-final/src/math/e_log10f.c @@ -0,0 +1,51 @@ +/* e_log10f.c -- float version of e_log10.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +two25 = 3.3554432000e+07, /* 0x4c000000 */ +ivln10 = 4.3429449201e-01, /* 0x3ede5bd9 */ +log10_2hi = 3.0102920532e-01, /* 0x3e9a2080 */ +log10_2lo = 7.9034151668e-07; /* 0x355427db */ + +static const float zero = 0.0; + +float +log10f(float x) +{ + float y,z; + int32_t i,k,hx; + + GET_FLOAT_WORD(hx,x); + + k=0; + if (hx < 0x00800000) { /* x < 2**-126 */ + if ((hx&0x7fffffff)==0) + return -two25/zero; /* log(+-0)=-inf */ + if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ + k -= 25; x *= two25; /* subnormal number, scale up x */ + GET_FLOAT_WORD(hx,x); + } + if (hx >= 0x7f800000) return x+x; + k += (hx>>23)-127; + i = ((uint32_t)k&0x80000000)>>31; + hx = (hx&0x007fffff)|((0x7f-i)<<23); + y = (float)(k+i); + SET_FLOAT_WORD(x,hx); + z = y*log10_2lo + ivln10*logf(x); + return z+y*log10_2hi; +} diff --git a/05/musl-final/src/math/e_logf.c b/05/musl-final/src/math/e_logf.c new file mode 100644 index 0000000..46a8b8c --- /dev/null +++ b/05/musl-final/src/math/e_logf.c @@ -0,0 +1,81 @@ +/* e_logf.c -- float version of e_log.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ +ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ +two25 = 3.355443200e+07, /* 0x4c000000 */ +Lg1 = 6.6666668653e-01, /* 3F2AAAAB */ +Lg2 = 4.0000000596e-01, /* 3ECCCCCD */ +Lg3 = 2.8571429849e-01, /* 3E924925 */ +Lg4 = 2.2222198546e-01, /* 3E638E29 */ +Lg5 = 1.8183572590e-01, /* 3E3A3325 */ +Lg6 = 1.5313838422e-01, /* 3E1CD04F */ +Lg7 = 1.4798198640e-01; /* 3E178897 */ + +static const float zero = 0.0; + +float +logf(float x) +{ + float hfsq,f,s,z,R,w,t1,t2,dk; + int32_t k,ix,i,j; + + GET_FLOAT_WORD(ix,x); + + k=0; + if (ix < 0x00800000) { /* x < 2**-126 */ + if ((ix&0x7fffffff)==0) + return -two25/zero; /* log(+-0)=-inf */ + if (ix<0) return (x-x)/zero; /* log(-#) = NaN */ + k -= 25; x *= two25; /* subnormal number, scale up x */ + GET_FLOAT_WORD(ix,x); + } + if (ix >= 0x7f800000) return x+x; + k += (ix>>23)-127; + ix &= 0x007fffff; + i = (ix+(0x95f64<<3))&0x800000; + SET_FLOAT_WORD(x,ix|(i^0x3f800000)); /* normalize x or x/2 */ + k += (i>>23); + f = x-(float)1.0; + if((0x007fffff&(15+ix))<16) { /* |f| < 2**-20 */ + if(f==zero) { if(k==0) return zero; else {dk=(float)k; + return dk*ln2_hi+dk*ln2_lo;} } + R = f*f*((float)0.5-(float)0.33333333333333333*f); + if(k==0) return f-R; else {dk=(float)k; + return dk*ln2_hi-((R-dk*ln2_lo)-f);} + } + s = f/((float)2.0+f); + dk = (float)k; + z = s*s; + i = ix-(0x6147a<<3); + w = z*z; + j = (0x6b851<<3)-ix; + t1= w*(Lg2+w*(Lg4+w*Lg6)); + t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); + i |= j; + R = t2+t1; + if(i>0) { + hfsq=(float)0.5*f*f; + if(k==0) return f-(hfsq-s*(hfsq+R)); else + return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); + } else { + if(k==0) return f-s*(f-R); else + return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f); + } +} diff --git a/05/musl-final/src/math/e_pow.c b/05/musl-final/src/math/e_pow.c new file mode 100644 index 0000000..aad2428 --- /dev/null +++ b/05/musl-final/src/math/e_pow.c @@ -0,0 +1,300 @@ +/* @(#)e_pow.c 1.5 04/04/22 SMI */ +/* + * ==================================================== + * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. + * + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* pow(x,y) return x**y + * + * n + * Method: Let x = 2 * (1+f) + * 1. Compute and return log2(x) in two pieces: + * log2(x) = w1 + w2, + * where w1 has 53-24 = 29 bit trailing zeros. + * 2. Perform y*log2(x) = n+y' by simulating muti-precision + * arithmetic, where |y'|<=0.5. + * 3. Return x**y = 2**n*exp(y'*log2) + * + * Special cases: + * 1. (anything) ** 0 is 1 + * 2. (anything) ** 1 is itself + * 3. (anything) ** NAN is NAN + * 4. NAN ** (anything except 0) is NAN + * 5. +-(|x| > 1) ** +INF is +INF + * 6. +-(|x| > 1) ** -INF is +0 + * 7. +-(|x| < 1) ** +INF is +0 + * 8. +-(|x| < 1) ** -INF is +INF + * 9. +-1 ** +-INF is NAN + * 10. +0 ** (+anything except 0, NAN) is +0 + * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 + * 12. +0 ** (-anything except 0, NAN) is +INF + * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF + * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) + * 15. +INF ** (+anything except 0,NAN) is +INF + * 16. +INF ** (-anything except 0,NAN) is +0 + * 17. -INF ** (anything) = -0 ** (-anything) + * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) + * 19. (-anything except 0 and inf) ** (non-integer) is NAN + * + * Accuracy: + * pow(x,y) returns x**y nearly rounded. In particular + * pow(integer,integer) + * always returns the correct integer provided it is + * representable. + * + * Constants : + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const double +bp[] = {1.0, 1.5,}, +dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */ +dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */ +zero = 0.0, +one = 1.0, +two = 2.0, +two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */ +huge = 1.0e300, +tiny = 1.0e-300, + /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ +L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */ +L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */ +L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */ +L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */ +L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */ +L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */ +P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ +P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ +P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ +P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ +P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */ +lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */ +lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */ +lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */ +ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */ +cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ +cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */ +cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ +ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */ +ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ +ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ + +double +pow(double x, double y) +{ + double z,ax,z_h,z_l,p_h,p_l; + double y1,t1,t2,r,s,t,u,v,w; + int32_t i,j,k,yisint,n; + int32_t hx,hy,ix,iy; + uint32_t lx,ly; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hy,ly,y); + ix = hx&0x7fffffff; iy = hy&0x7fffffff; + + /* y==zero: x**0 = 1 */ + if((iy|ly)==0) return one; + + /* +-NaN return x+y */ + if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || + iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) + return x+y; + + /* determine if y is an odd int when x < 0 + * yisint = 0 ... y is not an integer + * yisint = 1 ... y is an odd int + * yisint = 2 ... y is an even int + */ + yisint = 0; + if(hx<0) { + if(iy>=0x43400000) yisint = 2; /* even integer y */ + else if(iy>=0x3ff00000) { + k = (iy>>20)-0x3ff; /* exponent */ + if(k>20) { + j = ly>>(52-k); + if((j<<(52-k))==ly) yisint = 2-(j&1); + } else if(ly==0) { + j = iy>>(20-k); + if((j<<(20-k))==iy) yisint = 2-(j&1); + } + } + } + + /* special value of y */ + if(ly==0) { + if (iy==0x7ff00000) { /* y is +-inf */ + if(((ix-0x3ff00000)|lx)==0) + return y - y; /* inf**+-1 is NaN */ + else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */ + return (hy>=0)? y: zero; + else /* (|x|<1)**-,+inf = inf,0 */ + return (hy<0)?-y: zero; + } + if(iy==0x3ff00000) { /* y is +-1 */ + if(hy<0) return one/x; else return x; + } + if(hy==0x40000000) return x*x; /* y is 2 */ + if(hy==0x3fe00000) { /* y is 0.5 */ + if(hx>=0) /* x >= +0 */ + return sqrt(x); + } + } + + ax = fabs(x); + /* special value of x */ + if(lx==0) { + if(ix==0x7ff00000||ix==0||ix==0x3ff00000){ + z = ax; /*x is +-0,+-inf,+-1*/ + if(hy<0) z = one/z; /* z = (1/|x|) */ + if(hx<0) { + if(((ix-0x3ff00000)|yisint)==0) { + z = (z-z)/(z-z); /* (-1)**non-int is NaN */ + } else if(yisint==1) + z = -z; /* (x<0)**odd = -(|x|**odd) */ + } + return z; + } + } + + /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be + n = (hx>>31)+1; + but ANSI C says a right shift of a signed negative quantity is + implementation defined. */ + n = ((uint32_t)hx>>31)-1; + + /* (x<0)**(non-int) is NaN */ + if((n|yisint)==0) return (x-x)/(x-x); + + s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ + if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */ + + /* |y| is huge */ + if(iy>0x41e00000) { /* if |y| > 2**31 */ + if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */ + if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny; + if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny; + } + /* over/underflow if x is not close to one */ + if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny; + if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny; + /* now |1-x| is tiny <= 2**-20, suffice to compute + log(x) by x-x^2/2+x^3/3-x^4/4 */ + t = ax-one; /* t has 20 trailing zeros */ + w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25)); + u = ivln2_h*t; /* ivln2_h has 21 sig. bits */ + v = t*ivln2_l-w*ivln2; + t1 = u+v; + SET_LOW_WORD(t1,0); + t2 = v-(t1-u); + } else { + double ss,s2,s_h,s_l,t_h,t_l; + n = 0; + /* take care subnormal number */ + if(ix<0x00100000) + {ax *= two53; n -= 53; GET_HIGH_WORD(ix,ax); } + n += ((ix)>>20)-0x3ff; + j = ix&0x000fffff; + /* determine interval */ + ix = j|0x3ff00000; /* normalize ix */ + if(j<=0x3988E) k=0; /* |x|>1)|0x20000000)+0x00080000+(k<<18)); + t_l = ax - (t_h-bp[k]); + s_l = v*((u-s_h*t_h)-s_h*t_l); + /* compute log(ax) */ + s2 = ss*ss; + r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); + r += s_l*(s_h+ss); + s2 = s_h*s_h; + t_h = 3.0+s2+r; + SET_LOW_WORD(t_h,0); + t_l = r-((t_h-3.0)-s2); + /* u+v = ss*(1+...) */ + u = s_h*t_h; + v = s_l*t_h+t_l*ss; + /* 2/(3log2)*(ss+...) */ + p_h = u+v; + SET_LOW_WORD(p_h,0); + p_l = v-(p_h-u); + z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ + z_l = cp_l*p_h+p_l*cp+dp_l[k]; + /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ + t = (double)n; + t1 = (((z_h+z_l)+dp_h[k])+t); + SET_LOW_WORD(t1,0); + t2 = z_l-(((t1-t)-dp_h[k])-z_h); + } + + /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ + y1 = y; + SET_LOW_WORD(y1,0); + p_l = (y-y1)*t1+y*t2; + p_h = y1*t1; + z = p_l+p_h; + EXTRACT_WORDS(j,i,z); + if (j>=0x40900000) { /* z >= 1024 */ + if(((j-0x40900000)|i)!=0) /* if z > 1024 */ + return s*huge*huge; /* overflow */ + else { + if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */ + } + } else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */ + if(((j-0xc090cc00)|i)!=0) /* z < -1075 */ + return s*tiny*tiny; /* underflow */ + else { + if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */ + } + } + /* + * compute 2**(p_h+p_l) + */ + i = j&0x7fffffff; + k = (i>>20)-0x3ff; + n = 0; + if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ + n = j+(0x00100000>>(k+1)); + k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */ + t = zero; + SET_HIGH_WORD(t,n&~(0x000fffff>>k)); + n = ((n&0x000fffff)|0x00100000)>>(20-k); + if(j<0) n = -n; + p_h -= t; + } + t = p_l+p_h; + SET_LOW_WORD(t,0); + u = t*lg2_h; + v = (p_l-(t-p_h))*lg2+t*lg2_l; + z = u+v; + w = v-(z-u); + t = z*z; + t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); + r = (z*t1)/(t1-two)-(w+z*w); + z = one-(r-z); + GET_HIGH_WORD(j,z); + j += (n<<20); + if((j>>20)<=0) z = scalbn(z,n); /* subnormal output */ + else SET_HIGH_WORD(z,j); + return s*z; +} diff --git a/05/musl-final/src/math/e_powf.c b/05/musl-final/src/math/e_powf.c new file mode 100644 index 0000000..ae61c24 --- /dev/null +++ b/05/musl-final/src/math/e_powf.c @@ -0,0 +1,243 @@ +/* e_powf.c -- float version of e_pow.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +bp[] = {1.0, 1.5,}, +dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */ +dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */ +zero = 0.0, +one = 1.0, +two = 2.0, +two24 = 16777216.0, /* 0x4b800000 */ +huge = 1.0e30, +tiny = 1.0e-30, + /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ +L1 = 6.0000002384e-01, /* 0x3f19999a */ +L2 = 4.2857143283e-01, /* 0x3edb6db7 */ +L3 = 3.3333334327e-01, /* 0x3eaaaaab */ +L4 = 2.7272811532e-01, /* 0x3e8ba305 */ +L5 = 2.3066075146e-01, /* 0x3e6c3255 */ +L6 = 2.0697501302e-01, /* 0x3e53f142 */ +P1 = 1.6666667163e-01, /* 0x3e2aaaab */ +P2 = -2.7777778450e-03, /* 0xbb360b61 */ +P3 = 6.6137559770e-05, /* 0x388ab355 */ +P4 = -1.6533901999e-06, /* 0xb5ddea0e */ +P5 = 4.1381369442e-08, /* 0x3331bb4c */ +lg2 = 6.9314718246e-01, /* 0x3f317218 */ +lg2_h = 6.93145752e-01, /* 0x3f317200 */ +lg2_l = 1.42860654e-06, /* 0x35bfbe8c */ +ovt = 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */ +cp = 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */ +cp_h = 9.6179199219e-01, /* 0x3f763800 =head of cp */ +cp_l = 4.7017383622e-06, /* 0x369dc3a0 =tail of cp_h */ +ivln2 = 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */ +ivln2_h = 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/ +ivln2_l = 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/ + +float +powf(float x, float y) +{ + float z,ax,z_h,z_l,p_h,p_l; + float y1,t1,t2,r,s,sn,t,u,v,w; + int32_t i,j,k,yisint,n; + int32_t hx,hy,ix,iy,is; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hy,y); + ix = hx&0x7fffffff; iy = hy&0x7fffffff; + + /* y==zero: x**0 = 1 */ + if(iy==0) return one; + + /* +-NaN return x+y */ + if(ix > 0x7f800000 || + iy > 0x7f800000) + return x+y; + + /* determine if y is an odd int when x < 0 + * yisint = 0 ... y is not an integer + * yisint = 1 ... y is an odd int + * yisint = 2 ... y is an even int + */ + yisint = 0; + if(hx<0) { + if(iy>=0x4b800000) yisint = 2; /* even integer y */ + else if(iy>=0x3f800000) { + k = (iy>>23)-0x7f; /* exponent */ + j = iy>>(23-k); + if((j<<(23-k))==iy) yisint = 2-(j&1); + } + } + + /* special value of y */ + if (iy==0x7f800000) { /* y is +-inf */ + if (ix==0x3f800000) + return y - y; /* inf**+-1 is NaN */ + else if (ix > 0x3f800000)/* (|x|>1)**+-inf = inf,0 */ + return (hy>=0)? y: zero; + else /* (|x|<1)**-,+inf = inf,0 */ + return (hy<0)?-y: zero; + } + if(iy==0x3f800000) { /* y is +-1 */ + if(hy<0) return one/x; else return x; + } + if(hy==0x40000000) return x*x; /* y is 2 */ + if(hy==0x3f000000) { /* y is 0.5 */ + if(hx>=0) /* x >= +0 */ + return sqrtf(x); + } + + ax = fabsf(x); + /* special value of x */ + if(ix==0x7f800000||ix==0||ix==0x3f800000){ + z = ax; /*x is +-0,+-inf,+-1*/ + if(hy<0) z = one/z; /* z = (1/|x|) */ + if(hx<0) { + if(((ix-0x3f800000)|yisint)==0) { + z = (z-z)/(z-z); /* (-1)**non-int is NaN */ + } else if(yisint==1) + z = -z; /* (x<0)**odd = -(|x|**odd) */ + } + return z; + } + + n = ((uint32_t)hx>>31)-1; + + /* (x<0)**(non-int) is NaN */ + if((n|yisint)==0) return (x-x)/(x-x); + + sn = one; /* s (sign of result -ve**odd) = -1 else = 1 */ + if((n|(yisint-1))==0) sn = -one;/* (-ve)**(odd int) */ + + /* |y| is huge */ + if(iy>0x4d000000) { /* if |y| > 2**27 */ + /* over/underflow if x is not close to one */ + if(ix<0x3f7ffff8) return (hy<0)? sn*huge*huge:sn*tiny*tiny; + if(ix>0x3f800007) return (hy>0)? sn*huge*huge:sn*tiny*tiny; + /* now |1-x| is tiny <= 2**-20, suffice to compute + log(x) by x-x^2/2+x^3/3-x^4/4 */ + t = ax-1; /* t has 20 trailing zeros */ + w = (t*t)*((float)0.5-t*((float)0.333333333333-t*(float)0.25)); + u = ivln2_h*t; /* ivln2_h has 16 sig. bits */ + v = t*ivln2_l-w*ivln2; + t1 = u+v; + GET_FLOAT_WORD(is,t1); + SET_FLOAT_WORD(t1,is&0xfffff000); + t2 = v-(t1-u); + } else { + float s2,s_h,s_l,t_h,t_l; + n = 0; + /* take care subnormal number */ + if(ix<0x00800000) + {ax *= two24; n -= 24; GET_FLOAT_WORD(ix,ax); } + n += ((ix)>>23)-0x7f; + j = ix&0x007fffff; + /* determine interval */ + ix = j|0x3f800000; /* normalize ix */ + if(j<=0x1cc471) k=0; /* |x|>1)&0xfffff000)|0x20000000; + SET_FLOAT_WORD(t_h,is+0x00400000+(k<<21)); + t_l = ax - (t_h-bp[k]); + s_l = v*((u-s_h*t_h)-s_h*t_l); + /* compute log(ax) */ + s2 = s*s; + r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); + r += s_l*(s_h+s); + s2 = s_h*s_h; + t_h = (float)3.0+s2+r; + GET_FLOAT_WORD(is,t_h); + SET_FLOAT_WORD(t_h,is&0xfffff000); + t_l = r-((t_h-(float)3.0)-s2); + /* u+v = s*(1+...) */ + u = s_h*t_h; + v = s_l*t_h+t_l*s; + /* 2/(3log2)*(s+...) */ + p_h = u+v; + GET_FLOAT_WORD(is,p_h); + SET_FLOAT_WORD(p_h,is&0xfffff000); + p_l = v-(p_h-u); + z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ + z_l = cp_l*p_h+p_l*cp+dp_l[k]; + /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ + t = (float)n; + t1 = (((z_h+z_l)+dp_h[k])+t); + GET_FLOAT_WORD(is,t1); + SET_FLOAT_WORD(t1,is&0xfffff000); + t2 = z_l-(((t1-t)-dp_h[k])-z_h); + } + + /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ + GET_FLOAT_WORD(is,y); + SET_FLOAT_WORD(y1,is&0xfffff000); + p_l = (y-y1)*t1+y*t2; + p_h = y1*t1; + z = p_l+p_h; + GET_FLOAT_WORD(j,z); + if (j>0x43000000) /* if z > 128 */ + return sn*huge*huge; /* overflow */ + else if (j==0x43000000) { /* if z == 128 */ + if(p_l+ovt>z-p_h) return sn*huge*huge; /* overflow */ + } + else if ((j&0x7fffffff)>0x43160000) /* z <= -150 */ + return sn*tiny*tiny; /* underflow */ + else if (j==0xc3160000){ /* z == -150 */ + if(p_l<=z-p_h) return sn*tiny*tiny; /* underflow */ + } + /* + * compute 2**(p_h+p_l) + */ + i = j&0x7fffffff; + k = (i>>23)-0x7f; + n = 0; + if(i>0x3f000000) { /* if |z| > 0.5, set n = [z+0.5] */ + n = j+(0x00800000>>(k+1)); + k = ((n&0x7fffffff)>>23)-0x7f; /* new k for n */ + SET_FLOAT_WORD(t,n&~(0x007fffff>>k)); + n = ((n&0x007fffff)|0x00800000)>>(23-k); + if(j<0) n = -n; + p_h -= t; + } + t = p_l+p_h; + GET_FLOAT_WORD(is,t); + SET_FLOAT_WORD(t,is&0xffff8000); + u = t*lg2_h; + v = (p_l-(t-p_h))*lg2+t*lg2_l; + z = u+v; + w = v-(z-u); + t = z*z; + t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); + r = (z*t1)/(t1-two)-(w+z*w); + z = one-(r-z); + GET_FLOAT_WORD(j,z); + j += (n<<23); + if((j>>23)<=0) z = scalbnf(z,n); /* subnormal output */ + else SET_FLOAT_WORD(z,j); + return sn*z; +} diff --git a/05/musl-final/src/math/e_rem_pio2.c b/05/musl-final/src/math/e_rem_pio2.c new file mode 100644 index 0000000..9eee36a --- /dev/null +++ b/05/musl-final/src/math/e_rem_pio2.c @@ -0,0 +1,163 @@ + +/* @(#)e_rem_pio2.c 1.4 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* __ieee754_rem_pio2(x,y) + * + * return the remainder of x rem pi/2 in y[0]+y[1] + * use __kernel_rem_pio2() + */ + +#include +#include "math_private.h" + +/* + * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi + */ +static const int32_t two_over_pi[] = { +0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, +0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A, +0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129, +0xA73EE8, 0x8235F5, 0x2EBB44, 0x84E99C, 0x7026B4, 0x5F7E41, +0x3991D6, 0x398353, 0x39F49C, 0x845F8B, 0xBDF928, 0x3B1FF8, +0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, 0x367ECF, +0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5, +0xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08, +0x560330, 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3, +0x91615E, 0xE61B08, 0x659985, 0x5F14A0, 0x68408D, 0xFFD880, +0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B, +}; + +static const int32_t npio2_hw[] = { +0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C, +0x4025FDBB, 0x402921FB, 0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C, +0x40346B9C, 0x4035FDBB, 0x40378FDB, 0x403921FB, 0x403AB41B, 0x403C463A, +0x403DD85A, 0x403F6A7A, 0x40407E4C, 0x4041475C, 0x4042106C, 0x4042D97C, +0x4043A28C, 0x40446B9C, 0x404534AC, 0x4045FDBB, 0x4046C6CB, 0x40478FDB, +0x404858EB, 0x404921FB, +}; + +/* + * invpio2: 53 bits of 2/pi + * pio2_1: first 33 bit of pi/2 + * pio2_1t: pi/2 - pio2_1 + * pio2_2: second 33 bit of pi/2 + * pio2_2t: pi/2 - (pio2_1+pio2_2) + * pio2_3: third 33 bit of pi/2 + * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) + */ + +static const double +zero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ +half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */ +two24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */ +invpio2 = 6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */ +pio2_1 = 1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */ +pio2_1t = 6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */ +pio2_2 = 6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */ +pio2_2t = 2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */ +pio2_3 = 2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */ +pio2_3t = 8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */ + +int32_t __ieee754_rem_pio2(double x, double *y) +{ + double z,w,t,r,fn; + double tx[3]; + int32_t e0,i,j,nx,n,ix,hx; + uint32_t low; + + GET_HIGH_WORD(hx,x); /* high word of x */ + ix = hx&0x7fffffff; + if(ix<=0x3fe921fb) /* |x| ~<= pi/4 , no need for reduction */ + {y[0] = x; y[1] = 0; return 0;} + if(ix<0x4002d97c) { /* |x| < 3pi/4, special case with n=+-1 */ + if(hx>0) { + z = x - pio2_1; + if(ix!=0x3ff921fb) { /* 33+53 bit pi is good enough */ + y[0] = z - pio2_1t; + y[1] = (z-y[0])-pio2_1t; + } else { /* near pi/2, use 33+33+53 bit pi */ + z -= pio2_2; + y[0] = z - pio2_2t; + y[1] = (z-y[0])-pio2_2t; + } + return 1; + } else { /* negative x */ + z = x + pio2_1; + if(ix!=0x3ff921fb) { /* 33+53 bit pi is good enough */ + y[0] = z + pio2_1t; + y[1] = (z-y[0])+pio2_1t; + } else { /* near pi/2, use 33+33+53 bit pi */ + z += pio2_2; + y[0] = z + pio2_2t; + y[1] = (z-y[0])+pio2_2t; + } + return -1; + } + } + if(ix<=0x413921fb) { /* |x| ~<= 2^19*(pi/2), medium size */ + t = fabs(x); + n = (int32_t) (t*invpio2+half); + fn = (double)n; + r = t-fn*pio2_1; + w = fn*pio2_1t; /* 1st round good to 85 bit */ + if(n<32&&ix!=npio2_hw[n-1]) { + y[0] = r-w; /* quick check no cancellation */ + } else { + uint32_t high; + j = ix>>20; + y[0] = r-w; + GET_HIGH_WORD(high,y[0]); + i = j-((high>>20)&0x7ff); + if(i>16) { /* 2nd iteration needed, good to 118 */ + t = r; + w = fn*pio2_2; + r = t-w; + w = fn*pio2_2t-((t-r)-w); + y[0] = r-w; + GET_HIGH_WORD(high,y[0]); + i = j-((high>>20)&0x7ff); + if(i>49) { /* 3rd iteration need, 151 bits acc */ + t = r; /* will cover all possible cases */ + w = fn*pio2_3; + r = t-w; + w = fn*pio2_3t-((t-r)-w); + y[0] = r-w; + } + } + } + y[1] = (r-y[0])-w; + if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} + else return n; + } + /* + * all other (large) arguments + */ + if(ix>=0x7ff00000) { /* x is inf or NaN */ + y[0]=y[1]=x-x; return 0; + } + /* set z = scalbn(|x|,ilogb(x)-23) */ + GET_LOW_WORD(low,x); + e0 = (ix>>20)-1046; /* e0 = ilogb(z)-23; */ + INSERT_WORDS(z, ix - ((int32_t)(e0<<20)), low); + for(i=0;i<2;i++) { + tx[i] = (double)((int32_t)(z)); + z = (z-tx[i])*two24; + } + tx[2] = z; + nx = 3; + while(tx[nx-1]==zero) nx--; /* skip zero term */ + n = __kernel_rem_pio2(tx,y,e0,nx,2,two_over_pi); + if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} + return n; +} diff --git a/05/musl-final/src/math/e_rem_pio2f.c b/05/musl-final/src/math/e_rem_pio2f.c new file mode 100644 index 0000000..4992ea0 --- /dev/null +++ b/05/musl-final/src/math/e_rem_pio2f.c @@ -0,0 +1,175 @@ +/* e_rem_pio2f.c -- float version of e_rem_pio2.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* __ieee754_rem_pio2f(x,y) + * + * return the remainder of x rem pi/2 in y[0]+y[1] + * use __kernel_rem_pio2f() + */ + +#include +#include "math_private.h" + +/* + * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi + */ +static const int32_t two_over_pi[] = { +0xA2, 0xF9, 0x83, 0x6E, 0x4E, 0x44, 0x15, 0x29, 0xFC, +0x27, 0x57, 0xD1, 0xF5, 0x34, 0xDD, 0xC0, 0xDB, 0x62, +0x95, 0x99, 0x3C, 0x43, 0x90, 0x41, 0xFE, 0x51, 0x63, +0xAB, 0xDE, 0xBB, 0xC5, 0x61, 0xB7, 0x24, 0x6E, 0x3A, +0x42, 0x4D, 0xD2, 0xE0, 0x06, 0x49, 0x2E, 0xEA, 0x09, +0xD1, 0x92, 0x1C, 0xFE, 0x1D, 0xEB, 0x1C, 0xB1, 0x29, +0xA7, 0x3E, 0xE8, 0x82, 0x35, 0xF5, 0x2E, 0xBB, 0x44, +0x84, 0xE9, 0x9C, 0x70, 0x26, 0xB4, 0x5F, 0x7E, 0x41, +0x39, 0x91, 0xD6, 0x39, 0x83, 0x53, 0x39, 0xF4, 0x9C, +0x84, 0x5F, 0x8B, 0xBD, 0xF9, 0x28, 0x3B, 0x1F, 0xF8, +0x97, 0xFF, 0xDE, 0x05, 0x98, 0x0F, 0xEF, 0x2F, 0x11, +0x8B, 0x5A, 0x0A, 0x6D, 0x1F, 0x6D, 0x36, 0x7E, 0xCF, +0x27, 0xCB, 0x09, 0xB7, 0x4F, 0x46, 0x3F, 0x66, 0x9E, +0x5F, 0xEA, 0x2D, 0x75, 0x27, 0xBA, 0xC7, 0xEB, 0xE5, +0xF1, 0x7B, 0x3D, 0x07, 0x39, 0xF7, 0x8A, 0x52, 0x92, +0xEA, 0x6B, 0xFB, 0x5F, 0xB1, 0x1F, 0x8D, 0x5D, 0x08, +0x56, 0x03, 0x30, 0x46, 0xFC, 0x7B, 0x6B, 0xAB, 0xF0, +0xCF, 0xBC, 0x20, 0x9A, 0xF4, 0x36, 0x1D, 0xA9, 0xE3, +0x91, 0x61, 0x5E, 0xE6, 0x1B, 0x08, 0x65, 0x99, 0x85, +0x5F, 0x14, 0xA0, 0x68, 0x40, 0x8D, 0xFF, 0xD8, 0x80, +0x4D, 0x73, 0x27, 0x31, 0x06, 0x06, 0x15, 0x56, 0xCA, +0x73, 0xA8, 0xC9, 0x60, 0xE2, 0x7B, 0xC0, 0x8C, 0x6B, +}; + +/* This array is like the one in e_rem_pio2.c, but the numbers are + single precision and the last 8 bits are forced to 0. */ +static const int32_t npio2_hw[] = { +0x3fc90f00, 0x40490f00, 0x4096cb00, 0x40c90f00, 0x40fb5300, 0x4116cb00, +0x412fed00, 0x41490f00, 0x41623100, 0x417b5300, 0x418a3a00, 0x4196cb00, +0x41a35c00, 0x41afed00, 0x41bc7e00, 0x41c90f00, 0x41d5a000, 0x41e23100, +0x41eec200, 0x41fb5300, 0x4203f200, 0x420a3a00, 0x42108300, 0x4216cb00, +0x421d1400, 0x42235c00, 0x4229a500, 0x422fed00, 0x42363600, 0x423c7e00, +0x4242c700, 0x42490f00 +}; + +/* + * invpio2: 24 bits of 2/pi + * pio2_1: first 17 bit of pi/2 + * pio2_1t: pi/2 - pio2_1 + * pio2_2: second 17 bit of pi/2 + * pio2_2t: pi/2 - (pio2_1+pio2_2) + * pio2_3: third 17 bit of pi/2 + * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) + */ + +static const float +zero = 0.0000000000e+00, /* 0x00000000 */ +half = 5.0000000000e-01, /* 0x3f000000 */ +two8 = 2.5600000000e+02, /* 0x43800000 */ +invpio2 = 6.3661980629e-01, /* 0x3f22f984 */ +pio2_1 = 1.5707855225e+00, /* 0x3fc90f80 */ +pio2_1t = 1.0804334124e-05, /* 0x37354443 */ +pio2_2 = 1.0804273188e-05, /* 0x37354400 */ +pio2_2t = 6.0770999344e-11, /* 0x2e85a308 */ +pio2_3 = 6.0770943833e-11, /* 0x2e85a300 */ +pio2_3t = 6.1232342629e-17; /* 0x248d3132 */ + +int32_t __ieee754_rem_pio2f(float x, float *y) +{ + float z,w,t,r,fn; + float tx[3]; + int32_t e0,i,j,nx,n,ix,hx; + + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix<=0x3f490fd8) /* |x| ~<= pi/4 , no need for reduction */ + {y[0] = x; y[1] = 0; return 0;} + if(ix<0x4016cbe4) { /* |x| < 3pi/4, special case with n=+-1 */ + if(hx>0) { + z = x - pio2_1; + if((ix&0xfffffff0)!=0x3fc90fd0) { /* 24+24 bit pi OK */ + y[0] = z - pio2_1t; + y[1] = (z-y[0])-pio2_1t; + } else { /* near pi/2, use 24+24+24 bit pi */ + z -= pio2_2; + y[0] = z - pio2_2t; + y[1] = (z-y[0])-pio2_2t; + } + return 1; + } else { /* negative x */ + z = x + pio2_1; + if((ix&0xfffffff0)!=0x3fc90fd0) { /* 24+24 bit pi OK */ + y[0] = z + pio2_1t; + y[1] = (z-y[0])+pio2_1t; + } else { /* near pi/2, use 24+24+24 bit pi */ + z += pio2_2; + y[0] = z + pio2_2t; + y[1] = (z-y[0])+pio2_2t; + } + return -1; + } + } + if(ix<=0x43490f80) { /* |x| ~<= 2^7*(pi/2), medium size */ + t = fabsf(x); + n = (int32_t) (t*invpio2+half); + fn = (float)n; + r = t-fn*pio2_1; + w = fn*pio2_1t; /* 1st round good to 40 bit */ + if(n<32&&(ix&0xffffff00)!=npio2_hw[n-1]) { + y[0] = r-w; /* quick check no cancellation */ + } else { + uint32_t high; + j = ix>>23; + y[0] = r-w; + GET_FLOAT_WORD(high,y[0]); + i = j-((high>>23)&0xff); + if(i>8) { /* 2nd iteration needed, good to 57 */ + t = r; + w = fn*pio2_2; + r = t-w; + w = fn*pio2_2t-((t-r)-w); + y[0] = r-w; + GET_FLOAT_WORD(high,y[0]); + i = j-((high>>23)&0xff); + if(i>25) { /* 3rd iteration need, 74 bits acc */ + t = r; /* will cover all possible cases */ + w = fn*pio2_3; + r = t-w; + w = fn*pio2_3t-((t-r)-w); + y[0] = r-w; + } + } + } + y[1] = (r-y[0])-w; + if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} + else return n; + } + /* + * all other (large) arguments + */ + if(ix>=0x7f800000) { /* x is inf or NaN */ + y[0]=y[1]=x-x; return 0; + } + /* set z = scalbn(|x|,ilogb(x)-7) */ + e0 = (ix>>23)-134; /* e0 = ilogb(z)-7; */ + SET_FLOAT_WORD(z, ix - ((int32_t)(e0<<23))); + for(i=0;i<2;i++) { + tx[i] = (float)((int32_t)(z)); + z = (z-tx[i])*two8; + } + tx[2] = z; + nx = 3; + while(tx[nx-1]==zero) nx--; /* skip zero term */ + n = __kernel_rem_pio2f(tx,y,e0,nx,2,two_over_pi); + if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} + return n; +} diff --git a/05/musl-final/src/math/e_remainder.c b/05/musl-final/src/math/e_remainder.c new file mode 100644 index 0000000..9cb5691 --- /dev/null +++ b/05/musl-final/src/math/e_remainder.c @@ -0,0 +1,69 @@ + +/* @(#)e_remainder.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* remainder(x,p) + * Return : + * returns x REM p = x - [x/p]*p as if in infinite + * precise arithmetic, where [x/p] is the (infinite bit) + * integer nearest x/p (in half way case choose the even one). + * Method : + * Based on fmod() return x-[x/p]chopped*p exactlp. + */ + +#include +#include "math_private.h" + +static const double zero = 0.0; + + +double +remainder(double x, double p) +{ + int32_t hx,hp; + uint32_t sx,lx,lp; + double p_half; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hp,lp,p); + sx = hx&0x80000000; + hp &= 0x7fffffff; + hx &= 0x7fffffff; + + /* purge off exception values */ + if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */ + if((hx>=0x7ff00000)|| /* x not finite */ + ((hp>=0x7ff00000)&& /* p is NaN */ + (((hp-0x7ff00000)|lp)!=0))) + return (x*p)/(x*p); + + + if (hp<=0x7fdfffff) x = fmod(x,p+p); /* now x < 2p */ + if (((hx-hp)|(lx-lp))==0) return zero*x; + x = fabs(x); + p = fabs(p); + if (hp<0x00200000) { + if(x+x>p) { + x-=p; + if(x+x>=p) x -= p; + } + } else { + p_half = 0.5*p; + if(x>p_half) { + x-=p; + if(x>=p_half) x -= p; + } + } + GET_HIGH_WORD(hx,x); + SET_HIGH_WORD(x,hx^sx); + return x; +} diff --git a/05/musl-final/src/math/e_remainderf.c b/05/musl-final/src/math/e_remainderf.c new file mode 100644 index 0000000..c292367 --- /dev/null +++ b/05/musl-final/src/math/e_remainderf.c @@ -0,0 +1,61 @@ +/* e_remainderf.c -- float version of e_remainder.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float zero = 0.0; + + +float +remainderf(float x, float p) +{ + int32_t hx,hp; + uint32_t sx; + float p_half; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hp,p); + sx = hx&0x80000000; + hp &= 0x7fffffff; + hx &= 0x7fffffff; + + /* purge off exception values */ + if(hp==0) return (x*p)/(x*p); /* p = 0 */ + if((hx>=0x7f800000)|| /* x not finite */ + ((hp>0x7f800000))) /* p is NaN */ + return (x*p)/(x*p); + + + if (hp<=0x7effffff) x = fmodf(x,p+p); /* now x < 2p */ + if ((hx-hp)==0) return zero*x; + x = fabsf(x); + p = fabsf(p); + if (hp<0x01000000) { + if(x+x>p) { + x-=p; + if(x+x>=p) x -= p; + } + } else { + p_half = (float)0.5*p; + if(x>p_half) { + x-=p; + if(x>=p_half) x -= p; + } + } + GET_FLOAT_WORD(hx,x); + SET_FLOAT_WORD(x,hx^sx); + return x; +} diff --git a/05/musl-final/src/math/e_scalb.c b/05/musl-final/src/math/e_scalb.c new file mode 100644 index 0000000..cee2b44 --- /dev/null +++ b/05/musl-final/src/math/e_scalb.c @@ -0,0 +1,35 @@ + +/* @(#)e_scalb.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * scalb(x, fn) is provide for + * passing various standard test suite. One + * should use scalbn() instead. + */ + +#include +#include "math_private.h" + +double +scalb(double x, double fn) +{ + if (isnan(x)||isnan(fn)) return x*fn; + if (!isfinite(fn)) { + if(fn>0.0) return x*fn; + else return x/(-fn); + } + if (rint(fn)!=fn) return (fn-fn)/(fn-fn); + if ( fn > 65000.0) return scalbn(x, 65000); + if (-fn > 65000.0) return scalbn(x,-65000); + return scalbn(x,(int)fn); +} diff --git a/05/musl-final/src/math/e_scalbf.c b/05/musl-final/src/math/e_scalbf.c new file mode 100644 index 0000000..de7d7f6 --- /dev/null +++ b/05/musl-final/src/math/e_scalbf.c @@ -0,0 +1,31 @@ +/* e_scalbf.c -- float version of e_scalb.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +float +scalbf(float x, float fn) +{ + if (isnan(x)||isnan(fn)) return x*fn; + if (!isfinite(fn)) { + if(fn>(float)0.0) return x*fn; + else return x/(-fn); + } + if (rintf(fn)!=fn) return (fn-fn)/(fn-fn); + if ( fn > (float)65000.0) return scalbnf(x, 65000); + if (-fn > (float)65000.0) return scalbnf(x,-65000); + return scalbnf(x,(int)fn); +} diff --git a/05/musl-final/src/math/e_sinh.c b/05/musl-final/src/math/e_sinh.c new file mode 100644 index 0000000..3a57427 --- /dev/null +++ b/05/musl-final/src/math/e_sinh.c @@ -0,0 +1,75 @@ + +/* @(#)e_sinh.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* sinh(x) + * Method : + * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2 + * 1. Replace x by |x| (sinh(-x) = -sinh(x)). + * 2. + * E + E/(E+1) + * 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x) + * 2 + * + * 22 <= x <= lnovft : sinh(x) := exp(x)/2 + * lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2) + * ln2ovft < x : sinh(x) := x*shuge (overflow) + * + * Special cases: + * sinh(x) is |x| if x is +INF, -INF, or NaN. + * only sinh(0)=0 is exact for finite x. + */ + +#include +#include "math_private.h" + +static const double one = 1.0, shuge = 1.0e307; + +double +sinh(double x) +{ + double t,w,h; + int32_t ix,jx; + uint32_t lx; + + /* High word of |x|. */ + GET_HIGH_WORD(jx,x); + ix = jx&0x7fffffff; + + /* x is INF or NaN */ + if(ix>=0x7ff00000) return x+x; + + h = 0.5; + if (jx<0) h = -h; + /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */ + if (ix < 0x40360000) { /* |x|<22 */ + if (ix<0x3e300000) /* |x|<2**-28 */ + if(shuge+x>one) return x;/* sinh(tiny) = tiny with inexact */ + t = expm1(fabs(x)); + if(ix<0x3ff00000) return h*(2.0*t-t*t/(t+one)); + return h*(t+t/(t+one)); + } + + /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */ + if (ix < 0x40862E42) return h*exp(fabs(x)); + + /* |x| in [log(maxdouble), overflowthresold] */ + GET_LOW_WORD(lx,x); + if (ix<0x408633CE || ((ix==0x408633ce)&&(lx<=(uint32_t)0x8fb9f87d))) { + w = exp(0.5*fabs(x)); + t = h*w; + return t*w; + } + + /* |x| > overflowthresold, sinh(x) overflow */ + return x*shuge; +} diff --git a/05/musl-final/src/math/e_sinhf.c b/05/musl-final/src/math/e_sinhf.c new file mode 100644 index 0000000..fe60608 --- /dev/null +++ b/05/musl-final/src/math/e_sinhf.c @@ -0,0 +1,56 @@ +/* e_sinhf.c -- float version of e_sinh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one = 1.0, shuge = 1.0e37; + +float +sinhf(float x) +{ + float t,w,h; + int32_t ix,jx; + + GET_FLOAT_WORD(jx,x); + ix = jx&0x7fffffff; + + /* x is INF or NaN */ + if(ix>=0x7f800000) return x+x; + + h = 0.5; + if (jx<0) h = -h; + /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */ + if (ix < 0x41b00000) { /* |x|<22 */ + if (ix<0x31800000) /* |x|<2**-28 */ + if(shuge+x>one) return x;/* sinh(tiny) = tiny with inexact */ + t = expm1f(fabsf(x)); + if(ix<0x3f800000) return h*((float)2.0*t-t*t/(t+one)); + return h*(t+t/(t+one)); + } + + /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */ + if (ix < 0x42b17180) return h*expf(fabsf(x)); + + /* |x| in [log(maxdouble), overflowthresold] */ + if (ix<=0x42b2d4fc) { + w = expf((float)0.5*fabsf(x)); + t = h*w; + return t*w; + } + + /* |x| > overflowthresold, sinh(x) overflow */ + return x*shuge; +} diff --git a/05/musl-final/src/math/e_sqrt.c b/05/musl-final/src/math/e_sqrt.c new file mode 100644 index 0000000..2bc6874 --- /dev/null +++ b/05/musl-final/src/math/e_sqrt.c @@ -0,0 +1,442 @@ + +/* @(#)e_sqrt.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* sqrt(x) + * Return correctly rounded sqrt. + * ------------------------------------------ + * | Use the hardware sqrt if you have one | + * ------------------------------------------ + * Method: + * Bit by bit method using integer arithmetic. (Slow, but portable) + * 1. Normalization + * Scale x to y in [1,4) with even powers of 2: + * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then + * sqrt(x) = 2^k * sqrt(y) + * 2. Bit by bit computation + * Let q = sqrt(y) truncated to i bit after binary point (q = 1), + * i 0 + * i+1 2 + * s = 2*q , and y = 2 * ( y - q ). (1) + * i i i i + * + * To compute q from q , one checks whether + * i+1 i + * + * -(i+1) 2 + * (q + 2 ) <= y. (2) + * i + * -(i+1) + * If (2) is false, then q = q ; otherwise q = q + 2 . + * i+1 i i+1 i + * + * With some algebric manipulation, it is not difficult to see + * that (2) is equivalent to + * -(i+1) + * s + 2 <= y (3) + * i i + * + * The advantage of (3) is that s and y can be computed by + * i i + * the following recurrence formula: + * if (3) is false + * + * s = s , y = y ; (4) + * i+1 i i+1 i + * + * otherwise, + * -i -(i+1) + * s = s + 2 , y = y - s - 2 (5) + * i+1 i i+1 i i + * + * One may easily use induction to prove (4) and (5). + * Note. Since the left hand side of (3) contain only i+2 bits, + * it does not necessary to do a full (53-bit) comparison + * in (3). + * 3. Final rounding + * After generating the 53 bits result, we compute one more bit. + * Together with the remainder, we can decide whether the + * result is exact, bigger than 1/2ulp, or less than 1/2ulp + * (it will never equal to 1/2ulp). + * The rounding mode can be detected by checking whether + * huge + tiny is equal to huge, and whether huge - tiny is + * equal to huge for some floating point number "huge" and "tiny". + * + * Special cases: + * sqrt(+-0) = +-0 ... exact + * sqrt(inf) = inf + * sqrt(-ve) = NaN ... with invalid signal + * sqrt(NaN) = NaN ... with invalid signal for signaling NaN + * + * Other methods : see the appended file at the end of the program below. + *--------------- + */ + +#include +#include "math_private.h" + +static const double one = 1.0, tiny=1.0e-300; + +double +sqrt(double x) +{ + double z; + int32_t sign = (int)0x80000000; + int32_t ix0,s0,q,m,t,i; + uint32_t r,t1,s1,ix1,q1; + + EXTRACT_WORDS(ix0,ix1,x); + + /* take care of Inf and NaN */ + if((ix0&0x7ff00000)==0x7ff00000) { + return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf + sqrt(-inf)=sNaN */ + } + /* take care of zero */ + if(ix0<=0) { + if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */ + else if(ix0<0) + return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ + } + /* normalize x */ + m = (ix0>>20); + if(m==0) { /* subnormal x */ + while(ix0==0) { + m -= 21; + ix0 |= (ix1>>11); ix1 <<= 21; + } + for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1; + m -= i-1; + ix0 |= (ix1>>(32-i)); + ix1 <<= i; + } + m -= 1023; /* unbias exponent */ + ix0 = (ix0&0x000fffff)|0x00100000; + if(m&1){ /* odd m, double x to make it even */ + ix0 += ix0 + ((ix1&sign)>>31); + ix1 += ix1; + } + m >>= 1; /* m = [m/2] */ + + /* generate sqrt(x) bit by bit */ + ix0 += ix0 + ((ix1&sign)>>31); + ix1 += ix1; + q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ + r = 0x00200000; /* r = moving bit from right to left */ + + while(r!=0) { + t = s0+r; + if(t<=ix0) { + s0 = t+r; + ix0 -= t; + q += r; + } + ix0 += ix0 + ((ix1&sign)>>31); + ix1 += ix1; + r>>=1; + } + + r = sign; + while(r!=0) { + t1 = s1+r; + t = s0; + if((t>31); + ix1 += ix1; + r>>=1; + } + + /* use floating add to find out rounding direction */ + if((ix0|ix1)!=0) { + z = one-tiny; /* trigger inexact flag */ + if (z>=one) { + z = one+tiny; + if (q1==(uint32_t)0xffffffff) { q1=0; q += 1;} + else if (z>one) { + if (q1==(uint32_t)0xfffffffe) q+=1; + q1+=2; + } else + q1 += (q1&1); + } + } + ix0 = (q>>1)+0x3fe00000; + ix1 = q1>>1; + if ((q&1)==1) ix1 |= sign; + ix0 += (m <<20); + INSERT_WORDS(z,ix0,ix1); + return z; +} + +/* +Other methods (use floating-point arithmetic) +------------- +(This is a copy of a drafted paper by Prof W. Kahan +and K.C. Ng, written in May, 1986) + + Two algorithms are given here to implement sqrt(x) + (IEEE double precision arithmetic) in software. + Both supply sqrt(x) correctly rounded. The first algorithm (in + Section A) uses newton iterations and involves four divisions. + The second one uses reciproot iterations to avoid division, but + requires more multiplications. Both algorithms need the ability + to chop results of arithmetic operations instead of round them, + and the INEXACT flag to indicate when an arithmetic operation + is executed exactly with no roundoff error, all part of the + standard (IEEE 754-1985). The ability to perform shift, add, + subtract and logical AND operations upon 32-bit words is needed + too, though not part of the standard. + +A. sqrt(x) by Newton Iteration + + (1) Initial approximation + + Let x0 and x1 be the leading and the trailing 32-bit words of + a floating point number x (in IEEE double format) respectively + + 1 11 52 ...widths + ------------------------------------------------------ + x: |s| e | f | + ------------------------------------------------------ + msb lsb msb lsb ...order + + + ------------------------ ------------------------ + x0: |s| e | f1 | x1: | f2 | + ------------------------ ------------------------ + + By performing shifts and subtracts on x0 and x1 (both regarded + as integers), we obtain an 8-bit approximation of sqrt(x) as + follows. + + k := (x0>>1) + 0x1ff80000; + y0 := k - T1[31&(k>>15)]. ... y ~ sqrt(x) to 8 bits + Here k is a 32-bit integer and T1[] is an integer array containing + correction terms. Now magically the floating value of y (y's + leading 32-bit word is y0, the value of its trailing word is 0) + approximates sqrt(x) to almost 8-bit. + + Value of T1: + static int T1[32]= { + 0, 1024, 3062, 5746, 9193, 13348, 18162, 23592, + 29598, 36145, 43202, 50740, 58733, 67158, 75992, 85215, + 83599, 71378, 60428, 50647, 41945, 34246, 27478, 21581, + 16499, 12183, 8588, 5674, 3403, 1742, 661, 130,}; + + (2) Iterative refinement + + Apply Heron's rule three times to y, we have y approximates + sqrt(x) to within 1 ulp (Unit in the Last Place): + + y := (y+x/y)/2 ... almost 17 sig. bits + y := (y+x/y)/2 ... almost 35 sig. bits + y := y-(y-x/y)/2 ... within 1 ulp + + + Remark 1. + Another way to improve y to within 1 ulp is: + + y := (y+x/y) ... almost 17 sig. bits to 2*sqrt(x) + y := y - 0x00100006 ... almost 18 sig. bits to sqrt(x) + + 2 + (x-y )*y + y := y + 2* ---------- ...within 1 ulp + 2 + 3y + x + + + This formula has one division fewer than the one above; however, + it requires more multiplications and additions. Also x must be + scaled in advance to avoid spurious overflow in evaluating the + expression 3y*y+x. Hence it is not recommended uless division + is slow. If division is very slow, then one should use the + reciproot algorithm given in section B. + + (3) Final adjustment + + By twiddling y's last bit it is possible to force y to be + correctly rounded according to the prevailing rounding mode + as follows. Let r and i be copies of the rounding mode and + inexact flag before entering the square root program. Also we + use the expression y+-ulp for the next representable floating + numbers (up and down) of y. Note that y+-ulp = either fixed + point y+-1, or multiply y by nextafter(1,+-inf) in chopped + mode. + + I := FALSE; ... reset INEXACT flag I + R := RZ; ... set rounding mode to round-toward-zero + z := x/y; ... chopped quotient, possibly inexact + If(not I) then { ... if the quotient is exact + if(z=y) { + I := i; ... restore inexact flag + R := r; ... restore rounded mode + return sqrt(x):=y. + } else { + z := z - ulp; ... special rounding + } + } + i := TRUE; ... sqrt(x) is inexact + If (r=RN) then z=z+ulp ... rounded-to-nearest + If (r=RP) then { ... round-toward-+inf + y = y+ulp; z=z+ulp; + } + y := y+z; ... chopped sum + y0:=y0-0x00100000; ... y := y/2 is correctly rounded. + I := i; ... restore inexact flag + R := r; ... restore rounded mode + return sqrt(x):=y. + + (4) Special cases + + Square root of +inf, +-0, or NaN is itself; + Square root of a negative number is NaN with invalid signal. + + +B. sqrt(x) by Reciproot Iteration + + (1) Initial approximation + + Let x0 and x1 be the leading and the trailing 32-bit words of + a floating point number x (in IEEE double format) respectively + (see section A). By performing shifs and subtracts on x0 and y0, + we obtain a 7.8-bit approximation of 1/sqrt(x) as follows. + + k := 0x5fe80000 - (x0>>1); + y0:= k - T2[63&(k>>14)]. ... y ~ 1/sqrt(x) to 7.8 bits + + Here k is a 32-bit integer and T2[] is an integer array + containing correction terms. Now magically the floating + value of y (y's leading 32-bit word is y0, the value of + its trailing word y1 is set to zero) approximates 1/sqrt(x) + to almost 7.8-bit. + + Value of T2: + static int T2[64]= { + 0x1500, 0x2ef8, 0x4d67, 0x6b02, 0x87be, 0xa395, 0xbe7a, 0xd866, + 0xf14a, 0x1091b,0x11fcd,0x13552,0x14999,0x15c98,0x16e34,0x17e5f, + 0x18d03,0x19a01,0x1a545,0x1ae8a,0x1b5c4,0x1bb01,0x1bfde,0x1c28d, + 0x1c2de,0x1c0db,0x1ba73,0x1b11c,0x1a4b5,0x1953d,0x18266,0x16be0, + 0x1683e,0x179d8,0x18a4d,0x19992,0x1a789,0x1b445,0x1bf61,0x1c989, + 0x1d16d,0x1d77b,0x1dddf,0x1e2ad,0x1e5bf,0x1e6e8,0x1e654,0x1e3cd, + 0x1df2a,0x1d635,0x1cb16,0x1be2c,0x1ae4e,0x19bde,0x1868e,0x16e2e, + 0x1527f,0x1334a,0x11051,0xe951, 0xbe01, 0x8e0d, 0x5924, 0x1edd,}; + + (2) Iterative refinement + + Apply Reciproot iteration three times to y and multiply the + result by x to get an approximation z that matches sqrt(x) + to about 1 ulp. To be exact, we will have + -1ulp < sqrt(x)-z<1.0625ulp. + + ... set rounding mode to Round-to-nearest + y := y*(1.5-0.5*x*y*y) ... almost 15 sig. bits to 1/sqrt(x) + y := y*((1.5-2^-30)+0.5*x*y*y)... about 29 sig. bits to 1/sqrt(x) + ... special arrangement for better accuracy + z := x*y ... 29 bits to sqrt(x), with z*y<1 + z := z + 0.5*z*(1-z*y) ... about 1 ulp to sqrt(x) + + Remark 2. The constant 1.5-2^-30 is chosen to bias the error so that + (a) the term z*y in the final iteration is always less than 1; + (b) the error in the final result is biased upward so that + -1 ulp < sqrt(x) - z < 1.0625 ulp + instead of |sqrt(x)-z|<1.03125ulp. + + (3) Final adjustment + + By twiddling y's last bit it is possible to force y to be + correctly rounded according to the prevailing rounding mode + as follows. Let r and i be copies of the rounding mode and + inexact flag before entering the square root program. Also we + use the expression y+-ulp for the next representable floating + numbers (up and down) of y. Note that y+-ulp = either fixed + point y+-1, or multiply y by nextafter(1,+-inf) in chopped + mode. + + R := RZ; ... set rounding mode to round-toward-zero + switch(r) { + case RN: ... round-to-nearest + if(x<= z*(z-ulp)...chopped) z = z - ulp; else + if(x<= z*(z+ulp)...chopped) z = z; else z = z+ulp; + break; + case RZ:case RM: ... round-to-zero or round-to--inf + R:=RP; ... reset rounding mod to round-to-+inf + if(x=(z+ulp)*(z+ulp) ...rounded up) z = z+ulp; + break; + case RP: ... round-to-+inf + if(x>(z+ulp)*(z+ulp)...chopped) z = z+2*ulp; else + if(x>z*z ...chopped) z = z+ulp; + break; + } + + Remark 3. The above comparisons can be done in fixed point. For + example, to compare x and w=z*z chopped, it suffices to compare + x1 and w1 (the trailing parts of x and w), regarding them as + two's complement integers. + + ...Is z an exact square root? + To determine whether z is an exact square root of x, let z1 be the + trailing part of z, and also let x0 and x1 be the leading and + trailing parts of x. + + If ((z1&0x03ffffff)!=0) ... not exact if trailing 26 bits of z!=0 + I := 1; ... Raise Inexact flag: z is not exact + else { + j := 1 - [(x0>>20)&1] ... j = logb(x) mod 2 + k := z1 >> 26; ... get z's 25-th and 26-th + fraction bits + I := i or (k&j) or ((k&(j+j+1))!=(x1&3)); + } + R:= r ... restore rounded mode + return sqrt(x):=z. + + If multiplication is cheaper then the foregoing red tape, the + Inexact flag can be evaluated by + + I := i; + I := (z*z!=x) or I. + + Note that z*z can overwrite I; this value must be sensed if it is + True. + + Remark 4. If z*z = x exactly, then bit 25 to bit 0 of z1 must be + zero. + + -------------------- + z1: | f2 | + -------------------- + bit 31 bit 0 + + Further more, bit 27 and 26 of z1, bit 0 and 1 of x1, and the odd + or even of logb(x) have the following relations: + + ------------------------------------------------- + bit 27,26 of z1 bit 1,0 of x1 logb(x) + ------------------------------------------------- + 00 00 odd and even + 01 01 even + 10 10 odd + 10 00 even + 11 01 even + ------------------------------------------------- + + (4) Special cases (see (4) of Section A). + + */ + diff --git a/05/musl-final/src/math/e_sqrtf.c b/05/musl-final/src/math/e_sqrtf.c new file mode 100644 index 0000000..03a15be --- /dev/null +++ b/05/musl-final/src/math/e_sqrtf.c @@ -0,0 +1,85 @@ +/* e_sqrtf.c -- float version of e_sqrt.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one = 1.0, tiny=1.0e-30; + +float +sqrtf(float x) +{ + float z; + int32_t sign = (int)0x80000000; + int32_t ix,s,q,m,t,i; + uint32_t r; + + GET_FLOAT_WORD(ix,x); + + /* take care of Inf and NaN */ + if((ix&0x7f800000)==0x7f800000) { + return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf + sqrt(-inf)=sNaN */ + } + /* take care of zero */ + if(ix<=0) { + if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */ + else if(ix<0) + return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ + } + /* normalize x */ + m = (ix>>23); + if(m==0) { /* subnormal x */ + for(i=0;(ix&0x00800000)==0;i++) ix<<=1; + m -= i-1; + } + m -= 127; /* unbias exponent */ + ix = (ix&0x007fffff)|0x00800000; + if(m&1) /* odd m, double x to make it even */ + ix += ix; + m >>= 1; /* m = [m/2] */ + + /* generate sqrt(x) bit by bit */ + ix += ix; + q = s = 0; /* q = sqrt(x) */ + r = 0x01000000; /* r = moving bit from right to left */ + + while(r!=0) { + t = s+r; + if(t<=ix) { + s = t+r; + ix -= t; + q += r; + } + ix += ix; + r>>=1; + } + + /* use floating add to find out rounding direction */ + if(ix!=0) { + z = one-tiny; /* trigger inexact flag */ + if (z>=one) { + z = one+tiny; + if (z>one) + q += 2; + else + q += (q&1); + } + } + ix = (q>>1)+0x3f000000; + ix += (m <<23); + SET_FLOAT_WORD(z,ix); + return z; +} diff --git a/05/musl-final/src/math/i386/e_exp.s b/05/musl-final/src/math/i386/e_exp.s new file mode 100644 index 0000000..d6c54a3 --- /dev/null +++ b/05/musl-final/src/math/i386/e_exp.s @@ -0,0 +1,36 @@ +.global expf +expf: + mov 4(%esp),%eax + flds 4(%esp) + shr $23,%eax + inc %al + jz 1f + jmp 0f + +.global exp +exp: + mov 8(%esp),%eax + fldl 4(%esp) + shl %eax + cmp $0xffe00000,%eax + jae 1f + +0: fldl2e + fmulp + fst %st(1) + frndint + fst %st(2) + fsubrp + f2xm1 + fld1 + faddp + fscale + fstp %st(1) + ret + +1: fsts 4(%esp) + cmpl $0xff800000,4(%esp) + jnz 1f + fstp %st(0) + fldz +1: ret diff --git a/05/musl-final/src/math/i386/e_expf.s b/05/musl-final/src/math/i386/e_expf.s new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/05/musl-final/src/math/i386/e_expf.s @@ -0,0 +1 @@ + diff --git a/05/musl-final/src/math/i386/e_log.s b/05/musl-final/src/math/i386/e_log.s new file mode 100644 index 0000000..34b8d38 --- /dev/null +++ b/05/musl-final/src/math/i386/e_log.s @@ -0,0 +1,6 @@ +.global log +log: + fldln2 + fldl 4(%esp) + fyl2x + ret diff --git a/05/musl-final/src/math/i386/e_log10.s b/05/musl-final/src/math/i386/e_log10.s new file mode 100644 index 0000000..7f48941 --- /dev/null +++ b/05/musl-final/src/math/i386/e_log10.s @@ -0,0 +1,6 @@ +.global log10 +log10: + fldlg2 + fldl 4(%esp) + fyl2x + ret diff --git a/05/musl-final/src/math/i386/e_log10f.s b/05/musl-final/src/math/i386/e_log10f.s new file mode 100644 index 0000000..311486e --- /dev/null +++ b/05/musl-final/src/math/i386/e_log10f.s @@ -0,0 +1,6 @@ +.global log10f +log10f: + fldlg2 + flds 4(%esp) + fyl2x + ret diff --git a/05/musl-final/src/math/i386/e_logf.s b/05/musl-final/src/math/i386/e_logf.s new file mode 100644 index 0000000..b8beec0 --- /dev/null +++ b/05/musl-final/src/math/i386/e_logf.s @@ -0,0 +1,6 @@ +.global logf +logf: + fldln2 + flds 4(%esp) + fyl2x + ret diff --git a/05/musl-final/src/math/i386/e_remainder.s b/05/musl-final/src/math/i386/e_remainder.s new file mode 100644 index 0000000..b7ff3ef --- /dev/null +++ b/05/musl-final/src/math/i386/e_remainder.s @@ -0,0 +1,16 @@ +.global remainderf +remainderf: + flds 8(%esp) + flds 4(%esp) + jmp 1f + +.global remainder +remainder: + fldl 12(%esp) + fldl 4(%esp) +1: fprem1 + fstsw %ax + sahf + jp 1b + fstp %st(1) + ret diff --git a/05/musl-final/src/math/i386/e_remainderf.s b/05/musl-final/src/math/i386/e_remainderf.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/e_sqrt.s b/05/musl-final/src/math/i386/e_sqrt.s new file mode 100644 index 0000000..11314dc --- /dev/null +++ b/05/musl-final/src/math/i386/e_sqrt.s @@ -0,0 +1,4 @@ +.global sqrt +sqrt: fldl 4(%esp) + fsqrt + ret diff --git a/05/musl-final/src/math/i386/e_sqrtf.s b/05/musl-final/src/math/i386/e_sqrtf.s new file mode 100644 index 0000000..015e24c --- /dev/null +++ b/05/musl-final/src/math/i386/e_sqrtf.s @@ -0,0 +1,4 @@ +.global sqrtf +sqrtf: flds 4(%esp) + fsqrt + ret diff --git a/05/musl-final/src/math/i386/s_ceil.s b/05/musl-final/src/math/i386/s_ceil.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/s_ceilf.s b/05/musl-final/src/math/i386/s_ceilf.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/s_fabs.s b/05/musl-final/src/math/i386/s_fabs.s new file mode 100644 index 0000000..10c70f3 --- /dev/null +++ b/05/musl-final/src/math/i386/s_fabs.s @@ -0,0 +1,5 @@ +.global fabs +fabs: + fldl 4(%esp) + fabs + ret diff --git a/05/musl-final/src/math/i386/s_fabsf.s b/05/musl-final/src/math/i386/s_fabsf.s new file mode 100644 index 0000000..4544269 --- /dev/null +++ b/05/musl-final/src/math/i386/s_fabsf.s @@ -0,0 +1,5 @@ +.global fabsf +fabsf: + flds 4(%esp) + fabs + ret diff --git a/05/musl-final/src/math/i386/s_floor.s b/05/musl-final/src/math/i386/s_floor.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/s_floorf.s b/05/musl-final/src/math/i386/s_floorf.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/s_ldexp.s b/05/musl-final/src/math/i386/s_ldexp.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/s_ldexpf.s b/05/musl-final/src/math/i386/s_ldexpf.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/i386/s_rint.s b/05/musl-final/src/math/i386/s_rint.s new file mode 100644 index 0000000..5ba4ab4 --- /dev/null +++ b/05/musl-final/src/math/i386/s_rint.s @@ -0,0 +1,5 @@ +.global rint +rint: + fldl 4(%esp) + frndint + ret diff --git a/05/musl-final/src/math/i386/s_rintf.s b/05/musl-final/src/math/i386/s_rintf.s new file mode 100644 index 0000000..d7aacd8 --- /dev/null +++ b/05/musl-final/src/math/i386/s_rintf.s @@ -0,0 +1,5 @@ +.global rintf +rintf: + flds 4(%esp) + frndint + ret diff --git a/05/musl-final/src/math/i386/s_scalbln.s b/05/musl-final/src/math/i386/s_scalbln.s new file mode 100644 index 0000000..bd022b4 --- /dev/null +++ b/05/musl-final/src/math/i386/s_scalbln.s @@ -0,0 +1,11 @@ +.global ldexp +.global scalbn +.global scalbln +ldexp: +scalbn: +scalbln: + fildl 12(%esp) + fldl 4(%esp) + fscale + fstp %st(1) + ret diff --git a/05/musl-final/src/math/i386/s_scalblnf.s b/05/musl-final/src/math/i386/s_scalblnf.s new file mode 100644 index 0000000..379ec91 --- /dev/null +++ b/05/musl-final/src/math/i386/s_scalblnf.s @@ -0,0 +1,11 @@ +.global ldexpf +.global scalbnf +.global scalblnf +ldexpf: +scalbnf: +scalblnf: + fildl 8(%esp) + flds 4(%esp) + fscale + fstp %st(1) + ret diff --git a/05/musl-final/src/math/i386/s_trunc.s b/05/musl-final/src/math/i386/s_trunc.s new file mode 100644 index 0000000..0773891 --- /dev/null +++ b/05/musl-final/src/math/i386/s_trunc.s @@ -0,0 +1,36 @@ +.global ceilf +ceilf: flds 4(%esp) + jmp 1f + +.global ceil +ceil: fldl 4(%esp) +1: mov $0x08fb,%edx + jmp 0f + +.global floorf +floorf: flds 4(%esp) + jmp 1f + +.global floor +floor: fldl 4(%esp) +1: mov $0x04f7,%edx + jmp 0f + +.global truncf +truncf: flds 4(%esp) + jmp 1f + +.global trunc +trunc: fldl 4(%esp) +1: mov $0x0cff,%edx + +0: fstcw 4(%esp) + mov 5(%esp),%ah + or %dh,%ah + and %dl,%ah + xchg %ah,5(%esp) + fldcw 4(%esp) + frndint + mov %ah,5(%esp) + fldcw 4(%esp) + ret diff --git a/05/musl-final/src/math/i386/s_truncf.s b/05/musl-final/src/math/i386/s_truncf.s new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/math/k_cos.c b/05/musl-final/src/math/k_cos.c new file mode 100644 index 0000000..22e9841 --- /dev/null +++ b/05/musl-final/src/math/k_cos.c @@ -0,0 +1,85 @@ + +/* @(#)k_cos.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * __kernel_cos( x, y ) + * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * + * Algorithm + * 1. Since cos(-x) = cos(x), we need only to consider positive x. + * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0. + * 3. cos(x) is approximated by a polynomial of degree 14 on + * [0,pi/4] + * 4 14 + * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x + * where the remez error is + * + * | 2 4 6 8 10 12 14 | -58 + * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 + * | | + * + * 4 6 8 10 12 14 + * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then + * cos(x) = 1 - x*x/2 + r + * since cos(x+y) ~ cos(x) - sin(x)*y + * ~ cos(x) - x*y, + * a correction term is necessary in cos(x) and hence + * cos(x+y) = 1 - (x*x/2 - (r - x*y)) + * For better accuracy when x > 0.3, let qx = |x|/4 with + * the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125. + * Then + * cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)). + * Note that 1-qx and (x*x/2-qx) is EXACT here, and the + * magnitude of the latter is at least a quarter of x*x/2, + * thus, reducing the rounding error in the subtraction. + */ + +#include +#include "math_private.h" + +static const double +one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ +C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */ +C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */ +C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */ +C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */ +C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */ +C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */ + +double +__kernel_cos(double x, double y) +{ + double a,hz,z,r,qx; + int32_t ix; + GET_HIGH_WORD(ix,x); + ix &= 0x7fffffff; /* ix = |x|'s high word*/ + if(ix<0x3e400000) { /* if x < 2**27 */ + if(((int)x)==0) return one; /* generate inexact */ + } + z = x*x; + r = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*C6))))); + if(ix < 0x3FD33333) /* if |x| < 0.3 */ + return one - (0.5*z - (z*r - x*y)); + else { + if(ix > 0x3fe90000) { /* x > 0.78125 */ + qx = 0.28125; + } else { + INSERT_WORDS(qx,ix-0x00200000,0); /* x/4 */ + } + hz = 0.5*z-qx; + a = one-qx; + return a - (hz - (z*r-x*y)); + } +} diff --git a/05/musl-final/src/math/k_cosf.c b/05/musl-final/src/math/k_cosf.c new file mode 100644 index 0000000..61dc374 --- /dev/null +++ b/05/musl-final/src/math/k_cosf.c @@ -0,0 +1,52 @@ +/* k_cosf.c -- float version of k_cos.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +one = 1.0000000000e+00, /* 0x3f800000 */ +C1 = 4.1666667908e-02, /* 0x3d2aaaab */ +C2 = -1.3888889225e-03, /* 0xbab60b61 */ +C3 = 2.4801587642e-05, /* 0x37d00d01 */ +C4 = -2.7557314297e-07, /* 0xb493f27c */ +C5 = 2.0875723372e-09, /* 0x310f74f6 */ +C6 = -1.1359647598e-11; /* 0xad47d74e */ + +float +__kernel_cosf(float x, float y) +{ + float a,hz,z,r,qx; + int32_t ix; + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; /* ix = |x|'s high word*/ + if(ix<0x32000000) { /* if x < 2**27 */ + if(((int)x)==0) return one; /* generate inexact */ + } + z = x*x; + r = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*C6))))); + if(ix < 0x3e99999a) /* if |x| < 0.3 */ + return one - ((float)0.5*z - (z*r - x*y)); + else { + if(ix > 0x3f480000) { /* x > 0.78125 */ + qx = (float)0.28125; + } else { + SET_FLOAT_WORD(qx,ix-0x01000000); /* x/4 */ + } + hz = (float)0.5*z-qx; + a = one-qx; + return a - (hz - (z*r-x*y)); + } +} diff --git a/05/musl-final/src/math/k_rem_pio2.c b/05/musl-final/src/math/k_rem_pio2.c new file mode 100644 index 0000000..d993e4f --- /dev/null +++ b/05/musl-final/src/math/k_rem_pio2.c @@ -0,0 +1,300 @@ + +/* @(#)k_rem_pio2.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * __kernel_rem_pio2(x,y,e0,nx,prec,ipio2) + * double x[],y[]; int e0,nx,prec; int ipio2[]; + * + * __kernel_rem_pio2 return the last three digits of N with + * y = x - N*pi/2 + * so that |y| < pi/2. + * + * The method is to compute the integer (mod 8) and fraction parts of + * (2/pi)*x without doing the full multiplication. In general we + * skip the part of the product that are known to be a huge integer ( + * more accurately, = 0 mod 8 ). Thus the number of operations are + * independent of the exponent of the input. + * + * (2/pi) is represented by an array of 24-bit integers in ipio2[]. + * + * Input parameters: + * x[] The input value (must be positive) is broken into nx + * pieces of 24-bit integers in double precision format. + * x[i] will be the i-th 24 bit of x. The scaled exponent + * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0 + * match x's up to 24 bits. + * + * Example of breaking a double positive z into x[0]+x[1]+x[2]: + * e0 = ilogb(z)-23 + * z = scalbn(z,-e0) + * for i = 0,1,2 + * x[i] = floor(z) + * z = (z-x[i])*2**24 + * + * + * y[] ouput result in an array of double precision numbers. + * The dimension of y[] is: + * 24-bit precision 1 + * 53-bit precision 2 + * 64-bit precision 2 + * 113-bit precision 3 + * The actual value is the sum of them. Thus for 113-bit + * precison, one may have to do something like: + * + * long double t,w,r_head, r_tail; + * t = (long double)y[2] + (long double)y[1]; + * w = (long double)y[0]; + * r_head = t+w; + * r_tail = w - (r_head - t); + * + * e0 The exponent of x[0] + * + * nx dimension of x[] + * + * prec an integer indicating the precision: + * 0 24 bits (single) + * 1 53 bits (double) + * 2 64 bits (extended) + * 3 113 bits (quad) + * + * ipio2[] + * integer array, contains the (24*i)-th to (24*i+23)-th + * bit of 2/pi after binary point. The corresponding + * floating value is + * + * ipio2[i] * 2^(-24(i+1)). + * + * External function: + * double scalbn(), floor(); + * + * + * Here is the description of some local variables: + * + * jk jk+1 is the initial number of terms of ipio2[] needed + * in the computation. The recommended value is 2,3,4, + * 6 for single, double, extended,and quad. + * + * jz local integer variable indicating the number of + * terms of ipio2[] used. + * + * jx nx - 1 + * + * jv index for pointing to the suitable ipio2[] for the + * computation. In general, we want + * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8 + * is an integer. Thus + * e0-3-24*jv >= 0 or (e0-3)/24 >= jv + * Hence jv = max(0,(e0-3)/24). + * + * jp jp+1 is the number of terms in PIo2[] needed, jp = jk. + * + * q[] double array with integral value, representing the + * 24-bits chunk of the product of x and 2/pi. + * + * q0 the corresponding exponent of q[0]. Note that the + * exponent for q[i] would be q0-24*i. + * + * PIo2[] double precision array, obtained by cutting pi/2 + * into 24 bits chunks. + * + * f[] ipio2[] in floating point + * + * iq[] integer array by breaking up q[] in 24-bits chunk. + * + * fq[] final product of x*(2/pi) in fq[0],..,fq[jk] + * + * ih integer. If >0 it indicates q[] is >= 0.5, hence + * it also indicates the *sign* of the result. + * + */ + + +/* + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const int init_jk[] = {2,3,4,6}; /* initial value for jk */ + +static const double PIo2[] = { + 1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */ + 7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */ + 5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */ + 3.28200341580791294123e-22, /* 0x3B78CC51, 0x60000000 */ + 1.27065575308067607349e-29, /* 0x39F01B83, 0x80000000 */ + 1.22933308981111328932e-36, /* 0x387A2520, 0x40000000 */ + 2.73370053816464559624e-44, /* 0x36E38222, 0x80000000 */ + 2.16741683877804819444e-51, /* 0x3569F31D, 0x00000000 */ +}; + +static const double +zero = 0.0, +one = 1.0, +two24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */ +twon24 = 5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */ + + int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int32_t *ipio2) +{ + int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih; + double z,fw,f[20],fq[20],q[20]; + + /* initialize jk*/ + jk = init_jk[prec]; + jp = jk; + + /* determine jx,jv,q0, note that 3>q0 */ + jx = nx-1; + jv = (e0-3)/24; if(jv<0) jv=0; + q0 = e0-24*(jv+1); + + /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ + j = jv-jx; m = jx+jk; + for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j]; + + /* compute q[0],q[1],...q[jk] */ + for (i=0;i<=jk;i++) { + for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw; + } + + jz = jk; +recompute: + /* distill q[] into iq[] reversingly */ + for(i=0,j=jz,z=q[jz];j>0;i++,j--) { + fw = (double)((int32_t)(twon24* z)); + iq[i] = (int32_t)(z-two24*fw); + z = q[j-1]+fw; + } + + /* compute n */ + z = scalbn(z,q0); /* actual value of z */ + z -= 8.0*floor(z*0.125); /* trim off integer >= 8 */ + n = (int32_t) z; + z -= (double)n; + ih = 0; + if(q0>0) { /* need iq[jz-1] to determine n */ + i = (iq[jz-1]>>(24-q0)); n += i; + iq[jz-1] -= i<<(24-q0); + ih = iq[jz-1]>>(23-q0); + } + else if(q0==0) ih = iq[jz-1]>>23; + else if(z>=0.5) ih=2; + + if(ih>0) { /* q > 0.5 */ + n += 1; carry = 0; + for(i=0;i0) { /* rare case: chance is 1 in 12 */ + switch(q0) { + case 1: + iq[jz-1] &= 0x7fffff; break; + case 2: + iq[jz-1] &= 0x3fffff; break; + } + } + if(ih==2) { + z = one - z; + if(carry!=0) z -= scalbn(one,q0); + } + } + + /* check if recomputation is needed */ + if(z==zero) { + j = 0; + for (i=jz-1;i>=jk;i--) j |= iq[i]; + if(j==0) { /* need recomputation */ + for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */ + + for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */ + f[jx+i] = (double) ipio2[jv+i]; + for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; + q[i] = fw; + } + jz += k; + goto recompute; + } + } + + /* chop off zero terms */ + if(z==0.0) { + jz -= 1; q0 -= 24; + while(iq[jz]==0) { jz--; q0-=24;} + } else { /* break z into 24-bit if necessary */ + z = scalbn(z,-q0); + if(z>=two24) { + fw = (double)((int32_t)(twon24*z)); + iq[jz] = (int32_t)(z-two24*fw); + jz += 1; q0 += 24; + iq[jz] = (int32_t) fw; + } else iq[jz] = (int32_t) z ; + } + + /* convert integer "bit" chunk to floating-point value */ + fw = scalbn(one,q0); + for(i=jz;i>=0;i--) { + q[i] = fw*(double)iq[i]; fw*=twon24; + } + + /* compute PIo2[0,...,jp]*q[jz,...,0] */ + for(i=jz;i>=0;i--) { + for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k]; + fq[jz-i] = fw; + } + + /* compress fq[] into y[] */ + switch(prec) { + case 0: + fw = 0.0; + for (i=jz;i>=0;i--) fw += fq[i]; + y[0] = (ih==0)? fw: -fw; + break; + case 1: + case 2: + fw = 0.0; + for (i=jz;i>=0;i--) fw += fq[i]; + y[0] = (ih==0)? fw: -fw; + fw = fq[0]-fw; + for (i=1;i<=jz;i++) fw += fq[i]; + y[1] = (ih==0)? fw: -fw; + break; + case 3: /* painful */ + for (i=jz;i>0;i--) { + fw = fq[i-1]+fq[i]; + fq[i] += fq[i-1]-fw; + fq[i-1] = fw; + } + for (i=jz;i>1;i--) { + fw = fq[i-1]+fq[i]; + fq[i] += fq[i-1]-fw; + fq[i-1] = fw; + } + for (fw=0.0,i=jz;i>=2;i--) fw += fq[i]; + if(ih==0) { + y[0] = fq[0]; y[1] = fq[1]; y[2] = fw; + } else { + y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw; + } + } + return n&7; +} diff --git a/05/musl-final/src/math/k_rem_pio2f.c b/05/musl-final/src/math/k_rem_pio2f.c new file mode 100644 index 0000000..b543f08 --- /dev/null +++ b/05/musl-final/src/math/k_rem_pio2f.c @@ -0,0 +1,192 @@ +/* k_rem_pio2f.c -- float version of k_rem_pio2.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +/* In the float version, the input parameter x contains 8 bit + integers, not 24 bit integers. 113 bit precision is not supported. */ + +static const int init_jk[] = {4,7,9}; /* initial value for jk */ + +static const float PIo2[] = { + 1.5703125000e+00, /* 0x3fc90000 */ + 4.5776367188e-04, /* 0x39f00000 */ + 2.5987625122e-05, /* 0x37da0000 */ + 7.5437128544e-08, /* 0x33a20000 */ + 6.0026650317e-11, /* 0x2e840000 */ + 7.3896444519e-13, /* 0x2b500000 */ + 5.3845816694e-15, /* 0x27c20000 */ + 5.6378512969e-18, /* 0x22d00000 */ + 8.3009228831e-20, /* 0x1fc40000 */ + 3.2756352257e-22, /* 0x1bc60000 */ + 6.3331015649e-25, /* 0x17440000 */ +}; + +static const float +zero = 0.0, +one = 1.0, +two8 = 2.5600000000e+02, /* 0x43800000 */ +twon8 = 3.9062500000e-03; /* 0x3b800000 */ + + int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const int32_t *ipio2) +{ + int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih; + float z,fw,f[20],fq[20],q[20]; + + /* initialize jk*/ + jk = init_jk[prec]; + jp = jk; + + /* determine jx,jv,q0, note that 3>q0 */ + jx = nx-1; + jv = (e0-3)/8; if(jv<0) jv=0; + q0 = e0-8*(jv+1); + + /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ + j = jv-jx; m = jx+jk; + for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (float) ipio2[j]; + + /* compute q[0],q[1],...q[jk] */ + for (i=0;i<=jk;i++) { + for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw; + } + + jz = jk; +recompute: + /* distill q[] into iq[] reversingly */ + for(i=0,j=jz,z=q[jz];j>0;i++,j--) { + fw = (float)((int32_t)(twon8* z)); + iq[i] = (int32_t)(z-two8*fw); + z = q[j-1]+fw; + } + + /* compute n */ + z = scalbnf(z,q0); /* actual value of z */ + z -= (float)8.0*floorf(z*(float)0.125); /* trim off integer >= 8 */ + n = (int32_t) z; + z -= (float)n; + ih = 0; + if(q0>0) { /* need iq[jz-1] to determine n */ + i = (iq[jz-1]>>(8-q0)); n += i; + iq[jz-1] -= i<<(8-q0); + ih = iq[jz-1]>>(7-q0); + } + else if(q0==0) ih = iq[jz-1]>>7; + else if(z>=(float)0.5) ih=2; + + if(ih>0) { /* q > 0.5 */ + n += 1; carry = 0; + for(i=0;i0) { /* rare case: chance is 1 in 12 */ + switch(q0) { + case 1: + iq[jz-1] &= 0x7f; break; + case 2: + iq[jz-1] &= 0x3f; break; + } + } + if(ih==2) { + z = one - z; + if(carry!=0) z -= scalbnf(one,q0); + } + } + + /* check if recomputation is needed */ + if(z==zero) { + j = 0; + for (i=jz-1;i>=jk;i--) j |= iq[i]; + if(j==0) { /* need recomputation */ + for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */ + + for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */ + f[jx+i] = (float) ipio2[jv+i]; + for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; + q[i] = fw; + } + jz += k; + goto recompute; + } + } + + /* chop off zero terms */ + if(z==(float)0.0) { + jz -= 1; q0 -= 8; + while(iq[jz]==0) { jz--; q0-=8;} + } else { /* break z into 8-bit if necessary */ + z = scalbnf(z,-q0); + if(z>=two8) { + fw = (float)((int32_t)(twon8*z)); + iq[jz] = (int32_t)(z-two8*fw); + jz += 1; q0 += 8; + iq[jz] = (int32_t) fw; + } else iq[jz] = (int32_t) z ; + } + + /* convert integer "bit" chunk to floating-point value */ + fw = scalbnf(one,q0); + for(i=jz;i>=0;i--) { + q[i] = fw*(float)iq[i]; fw*=twon8; + } + + /* compute PIo2[0,...,jp]*q[jz,...,0] */ + for(i=jz;i>=0;i--) { + for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k]; + fq[jz-i] = fw; + } + + /* compress fq[] into y[] */ + switch(prec) { + case 0: + fw = 0.0; + for (i=jz;i>=0;i--) fw += fq[i]; + y[0] = (ih==0)? fw: -fw; + break; + case 1: + case 2: + fw = 0.0; + for (i=jz;i>=0;i--) fw += fq[i]; + y[0] = (ih==0)? fw: -fw; + fw = fq[0]-fw; + for (i=1;i<=jz;i++) fw += fq[i]; + y[1] = (ih==0)? fw: -fw; + break; + case 3: /* painful */ + for (i=jz;i>0;i--) { + fw = fq[i-1]+fq[i]; + fq[i] += fq[i-1]-fw; + fq[i-1] = fw; + } + for (i=jz;i>1;i--) { + fw = fq[i-1]+fq[i]; + fq[i] += fq[i-1]-fw; + fq[i-1] = fw; + } + for (fw=0.0,i=jz;i>=2;i--) fw += fq[i]; + if(ih==0) { + y[0] = fq[0]; y[1] = fq[1]; y[2] = fw; + } else { + y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw; + } + } + return n&7; +} diff --git a/05/musl-final/src/math/k_sin.c b/05/musl-final/src/math/k_sin.c new file mode 100644 index 0000000..9def258 --- /dev/null +++ b/05/musl-final/src/math/k_sin.c @@ -0,0 +1,68 @@ + +/* @(#)k_sin.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* __kernel_sin( x, y, iy) + * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * Input iy indicates whether y is 0. (if iy=0, y assume to be 0). + * + * Algorithm + * 1. Since sin(-x) = -sin(x), we need only to consider positive x. + * 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0. + * 3. sin(x) is approximated by a polynomial of degree 13 on + * [0,pi/4] + * 3 13 + * sin(x) ~ x + S1*x + ... + S6*x + * where + * + * |sin(x) 2 4 6 8 10 12 | -58 + * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2 + * | x | + * + * 4. sin(x+y) = sin(x) + sin'(x')*y + * ~ sin(x) + (1-x*x/2)*y + * For better accuracy, let + * 3 2 2 2 2 + * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6)))) + * then 3 2 + * sin(x) = x + (S1*x + (x *(r-y/2)+y)) + */ + +#include +#include "math_private.h" + +static const double +half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */ +S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */ +S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */ +S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */ +S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */ +S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */ +S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */ + +double +__kernel_sin(double x, double y, int iy) +{ + double z,r,v; + int32_t ix; + GET_HIGH_WORD(ix,x); + ix &= 0x7fffffff; /* high word of x */ + if(ix<0x3e400000) /* |x| < 2**-27 */ + {if((int)x==0) return x;} /* generate inexact */ + z = x*x; + v = z*x; + r = S2+z*(S3+z*(S4+z*(S5+z*S6))); + if(iy==0) return x+v*(S1+z*r); + else return x-((z*(half*y-v*r)-y)-v*S1); +} diff --git a/05/musl-final/src/math/k_sinf.c b/05/musl-final/src/math/k_sinf.c new file mode 100644 index 0000000..617f614 --- /dev/null +++ b/05/musl-final/src/math/k_sinf.c @@ -0,0 +1,42 @@ +/* k_sinf.c -- float version of k_sin.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +half = 5.0000000000e-01,/* 0x3f000000 */ +S1 = -1.6666667163e-01, /* 0xbe2aaaab */ +S2 = 8.3333337680e-03, /* 0x3c088889 */ +S3 = -1.9841270114e-04, /* 0xb9500d01 */ +S4 = 2.7557314297e-06, /* 0x3638ef1b */ +S5 = -2.5050759689e-08, /* 0xb2d72f34 */ +S6 = 1.5896910177e-10; /* 0x2f2ec9d3 */ + +float +__kernel_sinf(float x, float y, int iy) +{ + float z,r,v; + int32_t ix; + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; /* high word of x */ + if(ix<0x32000000) /* |x| < 2**-27 */ + {if((int)x==0) return x;} /* generate inexact */ + z = x*x; + v = z*x; + r = S2+z*(S3+z*(S4+z*(S5+z*S6))); + if(iy==0) return x+v*(S1+z*r); + else return x-((z*(half*y-v*r)-y)-v*S1); +} diff --git a/05/musl-final/src/math/k_tan.c b/05/musl-final/src/math/k_tan.c new file mode 100644 index 0000000..f721ae6 --- /dev/null +++ b/05/musl-final/src/math/k_tan.c @@ -0,0 +1,149 @@ +/* @(#)k_tan.c 1.5 04/04/22 SMI */ + +/* + * ==================================================== + * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. + * + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* __kernel_tan( x, y, k ) + * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * Input k indicates whether tan (if k = 1) or -1/tan (if k = -1) is returned. + * + * Algorithm + * 1. Since tan(-x) = -tan(x), we need only to consider positive x. + * 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0. + * 3. tan(x) is approximated by a odd polynomial of degree 27 on + * [0,0.67434] + * 3 27 + * tan(x) ~ x + T1*x + ... + T13*x + * where + * + * |tan(x) 2 4 26 | -59.2 + * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 + * | x | + * + * Note: tan(x+y) = tan(x) + tan'(x)*y + * ~ tan(x) + (1+x*x)*y + * Therefore, for better accuracy in computing tan(x+y), let + * 3 2 2 2 2 + * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) + * then + * 3 2 + * tan(x+y) = x + (T1*x + (x *(r+y)+y)) + * + * 4. For x in [0.67434,pi/4], let y = pi/4 - x, then + * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y)) + * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y))) + */ + +#include +#include "math_private.h" +static const double xxx[] = { + 3.33333333333334091986e-01, /* 3FD55555, 55555563 */ + 1.33333333333201242699e-01, /* 3FC11111, 1110FE7A */ + 5.39682539762260521377e-02, /* 3FABA1BA, 1BB341FE */ + 2.18694882948595424599e-02, /* 3F9664F4, 8406D637 */ + 8.86323982359930005737e-03, /* 3F8226E3, E96E8493 */ + 3.59207910759131235356e-03, /* 3F6D6D22, C9560328 */ + 1.45620945432529025516e-03, /* 3F57DBC8, FEE08315 */ + 5.88041240820264096874e-04, /* 3F4344D8, F2F26501 */ + 2.46463134818469906812e-04, /* 3F3026F7, 1A8D1068 */ + 7.81794442939557092300e-05, /* 3F147E88, A03792A6 */ + 7.14072491382608190305e-05, /* 3F12B80F, 32F0A7E9 */ + -1.85586374855275456654e-05, /* BEF375CB, DB605373 */ + 2.59073051863633712884e-05, /* 3EFB2A70, 74BF7AD4 */ +/* one */ 1.00000000000000000000e+00, /* 3FF00000, 00000000 */ +/* pio4 */ 7.85398163397448278999e-01, /* 3FE921FB, 54442D18 */ +/* pio4lo */ 3.06161699786838301793e-17 /* 3C81A626, 33145C07 */ +}; +#define one xxx[13] +#define pio4 xxx[14] +#define pio4lo xxx[15] +#define T xxx +/* INDENT ON */ + +double +__kernel_tan(double x, double y, int iy) { + double z, r, v, w, s; + int32_t ix, hx; + + GET_HIGH_WORD(hx,x); + ix = hx & 0x7fffffff; /* high word of |x| */ + if (ix < 0x3e300000) { /* x < 2**-28 */ + if ((int) x == 0) { /* generate inexact */ + uint32_t low; + GET_LOW_WORD(low,x); + if (((ix | low) | (iy + 1)) == 0) + return one / fabs(x); + else { + if (iy == 1) + return x; + else { /* compute -1 / (x+y) carefully */ + double a, t; + + z = w = x + y; + SET_LOW_WORD(z, 0); + v = y - (z - x); + t = a = -one / w; + SET_LOW_WORD(t, 0); + s = one + t * z; + return t + a * (s + t * v); + } + } + } + } + if (ix >= 0x3FE59428) { /* |x| >= 0.6744 */ + if (hx < 0) { + x = -x; + y = -y; + } + z = pio4 - x; + w = pio4lo - y; + x = z + w; + y = 0.0; + } + z = x * x; + w = z * z; + /* + * Break x^5*(T[1]+x^2*T[2]+...) into + * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + + * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) + */ + r = T[1] + w * (T[3] + w * (T[5] + w * (T[7] + w * (T[9] + + w * T[11])))); + v = z * (T[2] + w * (T[4] + w * (T[6] + w * (T[8] + w * (T[10] + + w * T[12]))))); + s = z * x; + r = y + z * (s * (r + v) + y); + r += T[0] * s; + w = x + r; + if (ix >= 0x3FE59428) { + v = (double) iy; + return (double) (1 - ((hx >> 30) & 2)) * + (v - 2.0 * (x - (w * w / (w + v) - r))); + } + if (iy == 1) + return w; + else { + /* + * if allow error up to 2 ulp, simply return + * -1.0 / (x+r) here + */ + /* compute -1.0 / (x+r) accurately */ + double a, t; + z = w; + SET_LOW_WORD(z,0); + v = r - (z - x); /* z+v = r+x */ + t = a = -1.0 / w; /* a = -1.0/w */ + SET_LOW_WORD(t,0); + s = 1.0 + t * z; + return t + a * (s + t * v); + } +} diff --git a/05/musl-final/src/math/k_tanf.c b/05/musl-final/src/math/k_tanf.c new file mode 100644 index 0000000..99ede58 --- /dev/null +++ b/05/musl-final/src/math/k_tanf.c @@ -0,0 +1,105 @@ +/* k_tanf.c -- float version of k_tan.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. + * + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" +static const float +one = 1.0000000000e+00, /* 0x3f800000 */ +pio4 = 7.8539812565e-01, /* 0x3f490fda */ +pio4lo= 3.7748947079e-08, /* 0x33222168 */ +T[] = { + 3.3333334327e-01, /* 0x3eaaaaab */ + 1.3333334029e-01, /* 0x3e088889 */ + 5.3968254477e-02, /* 0x3d5d0dd1 */ + 2.1869488060e-02, /* 0x3cb327a4 */ + 8.8632395491e-03, /* 0x3c11371f */ + 3.5920790397e-03, /* 0x3b6b6916 */ + 1.4562094584e-03, /* 0x3abede48 */ + 5.8804126456e-04, /* 0x3a1a26c8 */ + 2.4646313977e-04, /* 0x398137b9 */ + 7.8179444245e-05, /* 0x38a3f445 */ + 7.1407252108e-05, /* 0x3895c07a */ + -1.8558637748e-05, /* 0xb79bae5f */ + 2.5907305826e-05, /* 0x37d95384 */ +}; + +float +__kernel_tanf(float x, float y, int iy) +{ + float z,r,v,w,s; + int32_t ix,hx; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; /* high word of |x| */ + if(ix<0x31800000) { /* x < 2**-28 */ + if ((int) x == 0) { /* generate inexact */ + if ((ix | (iy + 1)) == 0) + return one / fabsf(x); + else { + if (iy == 1) + return x; + else { /* compute -1 / (x+y) carefully */ + double a, t; + + z = w = x + y; + GET_FLOAT_WORD(ix, z); + SET_FLOAT_WORD(z, ix & 0xfffff000); + v = y - (z - x); + t = a = -one / w; + GET_FLOAT_WORD(ix, t); + SET_FLOAT_WORD(t, ix & 0xfffff000); + s = one + t * z; + return t + a * (s + t * v); + } + } + } + } + if(ix>=0x3f2ca140) { /* |x|>=0.6744 */ + if(hx<0) {x = -x; y = -y;} + z = pio4-x; + w = pio4lo-y; + x = z+w; y = 0.0; + } + z = x*x; + w = z*z; + /* Break x^5*(T[1]+x^2*T[2]+...) into + * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + + * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) + */ + r = T[1]+w*(T[3]+w*(T[5]+w*(T[7]+w*(T[9]+w*T[11])))); + v = z*(T[2]+w*(T[4]+w*(T[6]+w*(T[8]+w*(T[10]+w*T[12]))))); + s = z*x; + r = y + z*(s*(r+v)+y); + r += T[0]*s; + w = x+r; + if(ix>=0x3f2ca140) { + v = (float)iy; + return (float)(1-((hx>>30)&2))*(v-(float)2.0*(x-(w*w/(w+v)-r))); + } + if(iy==1) return w; + else { /* if allow error up to 2 ulp, + simply return -1.0/(x+r) here */ + /* compute -1.0/(x+r) accurately */ + float a,t; + int32_t i; + z = w; + GET_FLOAT_WORD(i,z); + SET_FLOAT_WORD(z,i&0xfffff000); + v = r-(z - x); /* z+v = r+x */ + t = a = -(float)1.0/w; /* a = -1.0/w */ + GET_FLOAT_WORD(i,t); + SET_FLOAT_WORD(t,i&0xfffff000); + s = (float)1.0+t*z; + return t+a*(s+t*v); + } +} diff --git a/05/musl-final/src/math/log.c b/05/musl-final/src/math/log.c new file mode 100644 index 0000000..71ad73d --- /dev/null +++ b/05/musl-final/src/math/log.c @@ -0,0 +1,142 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/e_log.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* log(x) + * Return the logrithm of x + * + * Method : + * 1. Argument Reduction: find k and f such that + * x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * 2. Approximation of log(1+f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Remez algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s + * (the values of Lg1 to Lg7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lg1*s +...+Lg7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log(1+f) = f - s*(f - R) (if f is not too large) + * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) + * + * 3. Finally, log(x) = k*ln2 + log(1+f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log(x) is NaN with signal if x < 0 (including -INF) ; + * log(+INF) is +INF; log(0) is -INF with signal; + * log(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "math_private.h" +#include "math.h" +#include + +static const double +ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ +ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ +two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ +Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ +Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ +Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ +Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ +Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ +Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ +Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ + +static const double zero = 0.0; + +double log(double x) +{ + double hfsq,f,s,z,R,w,t1,t2,dk; + int32_t k,hx,i,j; + uint32_t lx; + + EXTRACT_WORDS(hx, lx, x); + + k = 0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx&0x7fffffff)|lx) == 0) + return -two54/zero; /* log(+-0)=-inf */ + if (hx < 0) + return (x-x)/zero; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 54; + x *= two54; + GET_HIGH_WORD(hx,x); + } + if (hx >= 0x7ff00000) + return x+x; + k += (hx>>20) - 1023; + hx &= 0x000fffff; + i = (hx+0x95f64)&0x100000; + SET_HIGH_WORD(x, hx|(i^0x3ff00000)); /* normalize x or x/2 */ + k += i>>20; + f = x - 1.0; + if ((0x000fffff&(2+hx)) < 3) { /* -2**-20 <= f < 2**-20 */ + if (f == zero) { + if (k == 0) { + return zero; + } + dk = (double)k; + return dk*ln2_hi + dk*ln2_lo; + } + R = f*f*(0.5-0.33333333333333333*f); + if (k == 0) + return f - R; + dk = (double)k; + return dk*ln2_hi - ((R-dk*ln2_lo)-f); + } + s = f/(2.0+f); + dk = (double)k; + z = s*s; + i = hx - 0x6147a; + w = z*z; + j = 0x6b851 - hx; + t1 = w*(Lg2+w*(Lg4+w*Lg6)); + t2 = z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); + i |= j; + R = t2 + t1; + if (i > 0) { + hfsq = 0.5*f*f; + if (k == 0) + return f - (hfsq-s*(hfsq+R)); + return dk*ln2_hi - ((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); + } else { + if (k == 0) + return f - s*(f-R); + return dk*ln2_hi - ((s*(f-R)-dk*ln2_lo)-f); + } +} diff --git a/05/musl-final/src/math/log10.c b/05/musl-final/src/math/log10.c new file mode 100644 index 0000000..fa2b1e3 --- /dev/null +++ b/05/musl-final/src/math/log10.c @@ -0,0 +1,86 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/e_log10.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* + * Return the base 10 logarithm of x. See e_log.c and k_log.h for most + * comments. + * + * log10(x) = (f - 0.5*f*f + k_log1p(f)) / ln10 + k * log10(2) + * in not-quite-routine extra precision. + */ + +#include "math_private.h" +#include "math.h" +#include "__log1p.h" +#include + +static const double +two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ +ivln10hi = 4.34294481878168880939e-01, /* 0x3fdbcb7b, 0x15200000 */ +ivln10lo = 2.50829467116452752298e-11, /* 0x3dbb9438, 0xca9aadd5 */ +log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */ +log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */ + +static const double zero = 0.0; + +double log10(double x) +{ + double f,hfsq,hi,lo,r,val_hi,val_lo,w,y,y2; + int32_t i,k,hx; + uint32_t lx; + + EXTRACT_WORDS(hx, lx, x); + + k = 0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx&0x7fffffff)|lx) == 0) + return -two54/zero; /* log(+-0)=-inf */ + if (hx<0) + return (x-x)/zero; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 54; + x *= two54; + GET_HIGH_WORD(hx, x); + } + if (hx >= 0x7ff00000) + return x+x; + if (hx == 0x3ff00000 && lx == 0) + return zero; /* log(1) = +0 */ + k += (hx>>20) - 1023; + hx &= 0x000fffff; + i = (hx+0x95f64)&0x100000; + SET_HIGH_WORD(x, hx|(i^0x3ff00000)); /* normalize x or x/2 */ + k += i>>20; + y = (double)k; + f = x - 1.0; + hfsq = 0.5*f*f; + r = __log1p(f); + + /* See log2.c for details. */ + hi = f - hfsq; + SET_LOW_WORD(hi, 0); + lo = (f - hi) - hfsq + r; + val_hi = hi*ivln10hi; + y2 = y*log10_2hi; + val_lo = y*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi; + + /* + * Extra precision in for adding y*log10_2hi is not strictly needed + * since there is no very large cancellation near x = sqrt(2) or + * x = 1/sqrt(2), but we do it anyway since it costs little on CPUs + * with some parallelism and it reduces the error for many args. + */ + w = y2 + val_hi; + val_lo += (y2 - w) + val_hi; + val_hi = w; + + return val_lo + val_hi; +} diff --git a/05/musl-final/src/math/log10f.c b/05/musl-final/src/math/log10f.c new file mode 100644 index 0000000..804359f --- /dev/null +++ b/05/musl-final/src/math/log10f.c @@ -0,0 +1,72 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/e_log10f.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* + * See comments in log10.c. + */ + +#include "math.h" +#include "__log1pf.h" +#include + +static const float +two25 = 3.3554432000e+07, /* 0x4c000000 */ +ivln10hi = 4.3432617188e-01, /* 0x3ede6000 */ +ivln10lo = -3.1689971365e-05, /* 0xb804ead9 */ +log10_2hi = 3.0102920532e-01, /* 0x3e9a2080 */ +log10_2lo = 7.9034151668e-07; /* 0x355427db */ + +static const float zero = 0.0; + +float log10f(float x) +{ + float f,hfsq,hi,lo,r,y; + int32_t i,k,hx; + + GET_FLOAT_WORD(hx, x); + + k = 0; + if (hx < 0x00800000) { /* x < 2**-126 */ + if ((hx&0x7fffffff) == 0) + return -two25/zero; /* log(+-0)=-inf */ + if (hx < 0) + return (x-x)/zero; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 25; + x *= two25; + GET_FLOAT_WORD(hx, x); + } + if (hx >= 0x7f800000) + return x+x; + if (hx == 0x3f800000) + return zero; /* log(1) = +0 */ + k += (hx>>23) - 127; + hx &= 0x007fffff; + i = (hx+(0x4afb0d))&0x800000; + SET_FLOAT_WORD(x, hx|(i^0x3f800000)); /* normalize x or x/2 */ + k += i>>23; + y = (float)k; + f = x - 1.0f; + hfsq = 0.5f * f * f; + r = __log1pf(f); + +// FIXME +// /* See log2f.c and log2.c for details. */ +// if (sizeof(float_t) > sizeof(float)) +// return (r - hfsq + f) * ((float_t)ivln10lo + ivln10hi) + +// y * ((float_t)log10_2lo + log10_2hi); + hi = f - hfsq; + GET_FLOAT_WORD(hx, hi); + SET_FLOAT_WORD(hi, hx&0xfffff000); + lo = (f - hi) - hfsq + r; + return y*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi + + hi*ivln10hi + y*log10_2hi; +} diff --git a/05/musl-final/src/math/log10l.c b/05/musl-final/src/math/log10l.c new file mode 100644 index 0000000..a378fbb --- /dev/null +++ b/05/musl-final/src/math/log10l.c @@ -0,0 +1,186 @@ +/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_log10l.c */ +/* + * Copyright (c) 2008 Stephen L. Moshier + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * Common logarithm, long double precision + * + * + * SYNOPSIS: + * + * long double x, y, log10l(); + * + * y = log10l( x ); + * + * + * DESCRIPTION: + * + * Returns the base 10 logarithm of x. + * + * The argument is separated into its exponent and fractional + * parts. If the exponent is between -1 and +1, the logarithm + * of the fraction is approximated by + * + * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). + * + * Otherwise, setting z = 2(x-1)/x+1), + * + * log(x) = z + z**3 P(z)/Q(z). + * + * + * ACCURACY: + * + * Relative error: + * arithmetic domain # trials peak rms + * IEEE 0.5, 2.0 30000 9.0e-20 2.6e-20 + * IEEE exp(+-10000) 30000 6.0e-20 2.3e-20 + * + * In the tests over the interval exp(+-10000), the logarithms + * of the random arguments were uniformly distributed over + * [-10000, +10000]. + * + * ERROR MESSAGES: + * + * log singularity: x = 0; returns MINLOG + * log domain: x < 0; returns MINLOG + */ + +#include "math.h" + +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +long double log10l(long double x) +{ + return log10(x); +} +#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 +/* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 6.2e-22 + */ +static long double P[] = { + 4.9962495940332550844739E-1L, + 1.0767376367209449010438E1L, + 7.7671073698359539859595E1L, + 2.5620629828144409632571E2L, + 4.2401812743503691187826E2L, + 3.4258224542413922935104E2L, + 1.0747524399916215149070E2L, +}; +static long double Q[] = { +/* 1.0000000000000000000000E0,*/ + 2.3479774160285863271658E1L, + 1.9444210022760132894510E2L, + 7.7952888181207260646090E2L, + 1.6911722418503949084863E3L, + 2.0307734695595183428202E3L, + 1.2695660352705325274404E3L, + 3.2242573199748645407652E2L, +}; + +/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), + * where z = 2(x-1)/(x+1) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 6.16e-22 + */ +static long double R[4] = { + 1.9757429581415468984296E-3L, +-7.1990767473014147232598E-1L, + 1.0777257190312272158094E1L, +-3.5717684488096787370998E1L, +}; +static long double S[4] = { +/* 1.00000000000000000000E0L,*/ +-2.6201045551331104417768E1L, + 1.9361891836232102174846E2L, +-4.2861221385716144629696E2L, +}; +/* log10(2) */ +#define L102A 0.3125L +#define L102B -1.1470004336018804786261e-2L +/* log10(e) */ +#define L10EA 0.5L +#define L10EB -6.5705518096748172348871e-2L + +#define SQRTH 0.70710678118654752440L + +long double log10l(long double x) +{ + long double y; + volatile long double z; + int e; + + if (isnan(x)) + return x; + if(x <= 0.0L) { + if(x == 0.0L) + return -1.0L / (x - x); + return (x - x) / (x - x); + } + if (x == INFINITY) + return INFINITY; + /* separate mantissa from exponent */ + /* Note, frexp is used so that denormal numbers + * will be handled properly. + */ + x = frexpl(x, &e); + + /* logarithm using log(x) = z + z**3 P(z)/Q(z), + * where z = 2(x-1)/x+1) + */ + if (e > 2 || e < -2) { + if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ + e -= 1; + z = x - 0.5L; + y = 0.5L * z + 0.5L; + } else { /* 2 (x-1)/(x+1) */ + z = x - 0.5L; + z -= 0.5L; + y = 0.5L * x + 0.5L; + } + x = z / y; + z = x*x; + y = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); + goto done; + } + + /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ + if (x < SQRTH) { + e -= 1; + x = ldexpl(x, 1) - 1.0L; /* 2x - 1 */ + } else { + x = x - 1.0L; + } + z = x*x; + y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 7)); + y = y - ldexpl(z, -1); /* -0.5x^2 + ... */ + +done: + /* Multiply log of fraction by log10(e) + * and base 2 exponent by log10(2). + * + * ***CAUTION*** + * + * This sequence of operations is critical and it may + * be horribly defeated by some compiler optimizers. + */ + z = y * (L10EB); + z += x * (L10EB); + z += e * (L102B); + z += y * (L10EA); + z += x * (L10EA); + z += e * (L102A); + return z; +} +#endif diff --git a/05/musl-final/src/math/log1p.c b/05/musl-final/src/math/log1p.c new file mode 100644 index 0000000..8f22e87 --- /dev/null +++ b/05/musl-final/src/math/log1p.c @@ -0,0 +1,172 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/s_log1p.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* double log1p(double x) + * + * Method : + * 1. Argument Reduction: find k and f such that + * 1+x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * Note. If k=0, then f=x is exact. However, if k!=0, then f + * may not be representable exactly. In that case, a correction + * term is need. Let u=1+x rounded. Let c = (1+x)-u, then + * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), + * and add back the correction term c/u. + * (Note: when x > 2**53, one can simply return log(x)) + * + * 2. Approximation of log1p(f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Reme algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s + * (the values of Lp1 to Lp7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lp1*s +...+Lp7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log1p(f) = f - (hfsq - s*(hfsq+R)). + * + * 3. Finally, log1p(x) = k*ln2 + log1p(f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log1p(x) is NaN with signal if x < -1 (including -INF) ; + * log1p(+INF) is +INF; log1p(-1) is -INF with signal; + * log1p(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + * + * Note: Assuming log() return accurate answer, the following + * algorithm can be used to compute log1p(x) to within a few ULP: + * + * u = 1+x; + * if(u==1.0) return x ; else + * return log(u)*(x/(u-1.0)); + * + * See HP-15C Advanced Functions Handbook, p.193. + */ + +#include "math.h" +#include + +static const double +ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ +ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ +two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ +Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ +Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ +Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ +Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ +Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ +Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ +Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ + +static const double zero = 0.0; + +// double log1p(double x) +// { +// double hfsq,f,c,s,z,R,u; +// int32_t k,hx,hu,ax; + +// GET_HIGH_WORD(hx, x); +// ax = hx & 0x7fffffff; + +// k = 1; +// if (hx < 0x3FDA827A) { /* 1+x < sqrt(2)+ */ +// if (ax >= 0x3ff00000) { /* x <= -1.0 */ +// if (x == -1.0) +// return -two54/zero; /* log1p(-1)=+inf */ +// return (x-x)/(x-x); /* log1p(x<-1)=NaN */ +// } +// if (ax < 0x3e200000) { /* |x| < 2**-29 */ +// /* raise inexact */ +// if (two54 + x > zero && ax < 0x3c900000) /* |x| < 2**-54 */ +// return x; +// return x - x*x*0.5; +// } +// if (hx > 0 || hx <= (int32_t)0xbfd2bec4) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ +// k = 0; +// f = x; +// hu = 1; +// } +// } +// if (hx >= 0x7ff00000) +// return x+x; +// if (k != 0) { +// if (hx < 0x43400000) { +// STRICT_ASSIGN(double, u, 1.0 + x); +// GET_HIGH_WORD(hu, u); +// k = (hu>>20) - 1023; +// c = k > 0 ? 1.0-(u-x) : x-(u-1.0); /* correction term */ +// c /= u; +// } else { +// u = x; +// GET_HIGH_WORD(hu,u); +// k = (hu>>20) - 1023; +// c = 0; +// } +// hu &= 0x000fffff; +// /* +// * The approximation to sqrt(2) used in thresholds is not +// * critical. However, the ones used above must give less +// * strict bounds than the one here so that the k==0 case is +// * never reached from here, since here we have committed to +// * using the correction term but don't use it if k==0. +// */ +// if (hu < 0x6a09e) { /* u ~< sqrt(2) */ +// SET_HIGH_WORD(u, hu|0x3ff00000); /* normalize u */ +// } else { +// k += 1; +// SET_HIGH_WORD(u, hu|0x3fe00000); /* normalize u/2 */ +// hu = (0x00100000-hu)>>2; +// } +// f = u - 1.0; +// } +// hfsq = 0.5*f*f; +// if (hu == 0) { /* |f| < 2**-20 */ +// if (f == zero) { +// if(k == 0) +// return zero; +// c += k*ln2_lo; +// return k*ln2_hi + c; +// } +// R = hfsq*(1.0 - 0.66666666666666666*f); +// if (k == 0) +// return f - R; +// return k*ln2_hi - ((R-(k*ln2_lo+c))-f); +// } +// s = f/(2.0+f); +// z = s*s; +// R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); +// if (k == 0) +// return f - (hfsq-s*(hfsq+R)); +// return k*ln2_hi - ((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); +// } diff --git a/05/musl-final/src/math/log1pf.c b/05/musl-final/src/math/log1pf.c new file mode 100644 index 0000000..d746e67 --- /dev/null +++ b/05/musl-final/src/math/log1pf.c @@ -0,0 +1,112 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/s_log1pf.c */ +/* + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "math.h" +#include + +static const float +ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ +ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ +two25 = 3.355443200e+07, /* 0x4c000000 */ +Lp1 = 6.6666668653e-01, /* 3F2AAAAB */ +Lp2 = 4.0000000596e-01, /* 3ECCCCCD */ +Lp3 = 2.8571429849e-01, /* 3E924925 */ +Lp4 = 2.2222198546e-01, /* 3E638E29 */ +Lp5 = 1.8183572590e-01, /* 3E3A3325 */ +Lp6 = 1.5313838422e-01, /* 3E1CD04F */ +Lp7 = 1.4798198640e-01; /* 3E178897 */ + +static const float zero = 0.0; + +// float log1pf(float x) +// { +// float hfsq,f,c,s,z,R,u; +// int32_t k,hx,hu,ax; + +// GET_FLOAT_WORD(hx, x); +// ax = hx & 0x7fffffff; + +// k = 1; +// if (hx < 0x3ed413d0) { /* 1+x < sqrt(2)+ */ +// if (ax >= 0x3f800000) { /* x <= -1.0 */ +// if (x == -1.0f) +// return -two25/zero; /* log1p(-1)=+inf */ +// return (x-x)/(x-x); /* log1p(x<-1)=NaN */ +// } +// if (ax < 0x38000000) { /* |x| < 2**-15 */ +// /* raise inexact */ +// if (two25 + x > zero && ax < 0x33800000) /* |x| < 2**-24 */ +// return x; +// return x - x*x*0.5f; +// } +// if (hx > 0 || hx <= (int32_t)0xbe95f619) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ +// k = 0; +// f = x; +// hu = 1; +// } +// } +// if (hx >= 0x7f800000) +// return x+x; +// if (k != 0) { +// if (hx < 0x5a000000) { +// STRICT_ASSIGN(float, u, 1.0f + x); +// GET_FLOAT_WORD(hu, u); +// k = (hu>>23) - 127; +// /* correction term */ +// c = k > 0 ? 1.0f-(u-x) : x-(u-1.0f); +// c /= u; +// } else { +// u = x; +// GET_FLOAT_WORD(hu,u); +// k = (hu>>23) - 127; +// c = 0; +// } +// hu &= 0x007fffff; +// /* +// * The approximation to sqrt(2) used in thresholds is not +// * critical. However, the ones used above must give less +// * strict bounds than the one here so that the k==0 case is +// * never reached from here, since here we have committed to +// * using the correction term but don't use it if k==0. +// */ +// if (hu < 0x3504f4) { /* u < sqrt(2) */ +// SET_FLOAT_WORD(u, hu|0x3f800000); /* normalize u */ +// } else { +// k += 1; +// SET_FLOAT_WORD(u, hu|0x3f000000); /* normalize u/2 */ +// hu = (0x00800000-hu)>>2; +// } +// f = u - 1.0f; +// } +// hfsq = 0.5f * f * f; +// if (hu == 0) { /* |f| < 2**-20 */ +// if (f == zero) { +// if (k == 0) +// return zero; +// c += k*ln2_lo; +// return k*ln2_hi+c; +// } +// R = hfsq*(1.0f - 0.66666666666666666f * f); +// if (k == 0) +// return f - R; +// return k*ln2_hi - ((R-(k*ln2_lo+c))-f); +// } +// s = f/(2.0f + f); +// z = s*s; +// R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); +// if (k == 0) +// return f - (hfsq-s*(hfsq+R)); +// return k*ln2_hi - ((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); +// } diff --git a/05/musl-final/src/math/log1pl.c b/05/musl-final/src/math/log1pl.c new file mode 100644 index 0000000..2eda3a6 --- /dev/null +++ b/05/musl-final/src/math/log1pl.c @@ -0,0 +1,176 @@ +/* origin: OpenBSD /usr/src/lib/libm/src/ld80/s_log1pl.c */ +/* + * Copyright (c) 2008 Stephen L. Moshier + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * Relative error logarithm + * Natural logarithm of 1+x, long double precision + * + * + * SYNOPSIS: + * + * long double x, y, log1pl(); + * + * y = log1pl( x ); + * + * + * DESCRIPTION: + * + * Returns the base e (2.718...) logarithm of 1+x. + * + * The argument 1+x is separated into its exponent and fractional + * parts. If the exponent is between -1 and +1, the logarithm + * of the fraction is approximated by + * + * log(1+x) = x - 0.5 x^2 + x^3 P(x)/Q(x). + * + * Otherwise, setting z = 2(x-1)/x+1), + * + * log(x) = z + z^3 P(z)/Q(z). + * + * + * ACCURACY: + * + * Relative error: + * arithmetic domain # trials peak rms + * IEEE -1.0, 9.0 100000 8.2e-20 2.5e-20 + * + * ERROR MESSAGES: + * + * log singularity: x-1 = 0; returns -INFINITY + * log domain: x-1 < 0; returns NAN + */ + +#include "math.h" + +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +long double log1pl(long double x) +{ + return log1p(x); +} +#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 +/* Coefficients for log(1+x) = x - x^2 / 2 + x^3 P(x)/Q(x) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 2.32e-20 + */ +static long double P[] = { + 4.5270000862445199635215E-5L, + 4.9854102823193375972212E-1L, + 6.5787325942061044846969E0L, + 2.9911919328553073277375E1L, + 6.0949667980987787057556E1L, + 5.7112963590585538103336E1L, + 2.0039553499201281259648E1L, +}; +static long double Q[] = { +/* 1.0000000000000000000000E0,*/ + 1.5062909083469192043167E1L, + 8.3047565967967209469434E1L, + 2.2176239823732856465394E2L, + 3.0909872225312059774938E2L, + 2.1642788614495947685003E2L, + 6.0118660497603843919306E1L, +}; + +/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), + * where z = 2(x-1)/(x+1) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 6.16e-22 + */ +static long double R[4] = { + 1.9757429581415468984296E-3L, +-7.1990767473014147232598E-1L, + 1.0777257190312272158094E1L, +-3.5717684488096787370998E1L, +}; +static long double S[4] = { +/* 1.00000000000000000000E0L,*/ +-2.6201045551331104417768E1L, + 1.9361891836232102174846E2L, +-4.2861221385716144629696E2L, +}; +static const long double C1 = 6.9314575195312500000000E-1L; +static const long double C2 = 1.4286068203094172321215E-6L; + +#define SQRTH 0.70710678118654752440L + +long double log1pl(long double xm1) +{ + long double x, y, z; + int e; + + if (isnan(xm1)) + return xm1; + if (xm1 == INFINITY) + return xm1; + if (xm1 == 0.0) + return xm1; + + x = xm1 + 1.0L; + + /* Test for domain errors. */ + if (x <= 0.0L) { + if (x == 0.0L) + return -INFINITY; + return NAN; + } + + /* Separate mantissa from exponent. + Use frexp so that denormal numbers will be handled properly. */ + x = frexpl(x, &e); + + /* logarithm using log(x) = z + z^3 P(z)/Q(z), + where z = 2(x-1)/x+1) */ + if (e > 2 || e < -2) { + if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ + e -= 1; + z = x - 0.5L; + y = 0.5L * z + 0.5L; + } else { /* 2 (x-1)/(x+1) */ + z = x - 0.5L; + z -= 0.5L; + y = 0.5L * x + 0.5L; + } + x = z / y; + z = x*x; + z = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); + z = z + e * C2; + z = z + x; + z = z + e * C1; + return z; + } + + /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ + if (x < SQRTH) { + e -= 1; + if (e != 0) + x = 2.0 * x - 1.0L; + else + x = xm1; + } else { + if (e != 0) + x = x - 1.0L; + else + x = xm1; + } + z = x*x; + y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 6)); + y = y + e * C2; + z = y - 0.5 * z; + z = z + x; + z = z + e * C1; + return z; +} +#endif diff --git a/05/musl-final/src/math/log2.c b/05/musl-final/src/math/log2.c new file mode 100644 index 0000000..21290d2 --- /dev/null +++ b/05/musl-final/src/math/log2.c @@ -0,0 +1,109 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/e_log2.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* + * Return the base 2 logarithm of x. See log.c and __log1p.h for most + * comments. + * + * This reduces x to {k, 1+f} exactly as in e_log.c, then calls the kernel, + * then does the combining and scaling steps + * log2(x) = (f - 0.5*f*f + k_log1p(f)) / ln2 + k + * in not-quite-routine extra precision. + */ + +#include "math_private.h" +#include "math.h" +#include "__log1p.h" +#include + +static const double +two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ +ivln2hi = 1.44269504072144627571e+00, /* 0x3ff71547, 0x65200000 */ +ivln2lo = 1.67517131648865118353e-10; /* 0x3de705fc, 0x2eefa200 */ + +static const double zero = 0.0; + +double log2(double x) +{ + double f,hfsq,hi,lo,r,val_hi,val_lo,w,y; + int32_t i,k,hx; + uint32_t lx; + + EXTRACT_WORDS(hx, lx, x); + + k = 0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx&0x7fffffff)|lx) == 0) + return -two54/zero; /* log(+-0)=-inf */ + if (hx < 0) + return (x-x)/zero; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 54; + x *= two54; + GET_HIGH_WORD(hx, x); + } + if (hx >= 0x7ff00000) + return x+x; + if (hx == 0x3ff00000 && lx == 0) + return zero; /* log(1) = +0 */ + k += (hx>>20) - 1023; + hx &= 0x000fffff; + i = (hx+0x95f64) & 0x100000; + SET_HIGH_WORD(x, hx|(i^0x3ff00000)); /* normalize x or x/2 */ + k += i>>20; + y = (double)k; + f = x - 1.0; + hfsq = 0.5*f*f; + r = __log1p(f); + + /* + * f-hfsq must (for args near 1) be evaluated in extra precision + * to avoid a large cancellation when x is near sqrt(2) or 1/sqrt(2). + * This is fairly efficient since f-hfsq only depends on f, so can + * be evaluated in parallel with R. Not combining hfsq with R also + * keeps R small (though not as small as a true `lo' term would be), + * so that extra precision is not needed for terms involving R. + * + * Compiler bugs involving extra precision used to break Dekker's + * theorem for spitting f-hfsq as hi+lo, unless double_t was used + * or the multi-precision calculations were avoided when double_t + * has extra precision. These problems are now automatically + * avoided as a side effect of the optimization of combining the + * Dekker splitting step with the clear-low-bits step. + * + * y must (for args near sqrt(2) and 1/sqrt(2)) be added in extra + * precision to avoid a very large cancellation when x is very near + * these values. Unlike the above cancellations, this problem is + * specific to base 2. It is strange that adding +-1 is so much + * harder than adding +-ln2 or +-log10_2. + * + * This uses Dekker's theorem to normalize y+val_hi, so the + * compiler bugs are back in some configurations, sigh. And I + * don't want to used double_t to avoid them, since that gives a + * pessimization and the support for avoiding the pessimization + * is not yet available. + * + * The multi-precision calculations for the multiplications are + * routine. + */ + hi = f - hfsq; + SET_LOW_WORD(hi, 0); + lo = (f - hi) - hfsq + r; + val_hi = hi*ivln2hi; + val_lo = (lo+hi)*ivln2lo + lo*ivln2hi; + + /* spadd(val_hi, val_lo, y), except for not using double_t: */ + w = y + val_hi; + val_lo += (y - w) + val_hi; + val_hi = w; + + return val_lo + val_hi; +} diff --git a/05/musl-final/src/math/log2f.c b/05/musl-final/src/math/log2f.c new file mode 100644 index 0000000..81c017f --- /dev/null +++ b/05/musl-final/src/math/log2f.c @@ -0,0 +1,82 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/e_log2f.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* + * See comments in log2.c. + */ + +#include "math.h" +#include "__log1pf.h" +#include + +static const float +two25 = 3.3554432000e+07, /* 0x4c000000 */ +ivln2hi = 1.4428710938e+00, /* 0x3fb8b000 */ +ivln2lo = -1.7605285393e-04; /* 0xb9389ad4 */ + +static const float zero = 0.0; + +float log2f(float x) +{ + float f,hfsq,hi,lo,r,y; + int32_t i,k,hx; + + GET_FLOAT_WORD(hx, x); + + k = 0; + if (hx < 0x00800000) { /* x < 2**-126 */ + if ((hx&0x7fffffff) == 0) + return -two25/zero; /* log(+-0)=-inf */ + if (hx < 0) + return (x-x)/zero; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 25; + x *= two25; + GET_FLOAT_WORD(hx, x); + } + if (hx >= 0x7f800000) + return x+x; + if (hx == 0x3f800000) + return zero; /* log(1) = +0 */ + k += (hx>>23) - 127; + hx &= 0x007fffff; + i = (hx+(0x4afb0d))&0x800000; + SET_FLOAT_WORD(x, hx|(i^0x3f800000)); /* normalize x or x/2 */ + k += i>>23; + y = (float)k; + f = x - 1.0f; + hfsq = 0.5f * f * f; + r = __log1pf(f); + + /* + * We no longer need to avoid falling into the multi-precision + * calculations due to compiler bugs breaking Dekker's theorem. + * Keep avoiding this as an optimization. See log2.c for more + * details (some details are here only because the optimization + * is not yet available in double precision). + * + * Another compiler bug turned up. With gcc on i386, + * (ivln2lo + ivln2hi) would be evaluated in float precision + * despite runtime evaluations using double precision. So we + * must cast one of its terms to float_t. This makes the whole + * expression have type float_t, so return is forced to waste + * time clobbering its extra precision. + */ +// FIXME +// if (sizeof(float_t) > sizeof(float)) +// return (r - hfsq + f) * ((float_t)ivln2lo + ivln2hi) + y; + + hi = f - hfsq; + GET_FLOAT_WORD(hx,hi); + SET_FLOAT_WORD(hi,hx&0xfffff000); + lo = (f - hi) - hfsq + r; + return (lo+hi)*ivln2lo + lo*ivln2hi + hi*ivln2hi + y; +} diff --git a/05/musl-final/src/math/log2l.c b/05/musl-final/src/math/log2l.c new file mode 100644 index 0000000..f490500 --- /dev/null +++ b/05/musl-final/src/math/log2l.c @@ -0,0 +1,182 @@ +/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_log2l.c */ +/* + * Copyright (c) 2008 Stephen L. Moshier + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * Base 2 logarithm, long double precision + * + * + * SYNOPSIS: + * + * long double x, y, log2l(); + * + * y = log2l( x ); + * + * + * DESCRIPTION: + * + * Returns the base 2 logarithm of x. + * + * The argument is separated into its exponent and fractional + * parts. If the exponent is between -1 and +1, the (natural) + * logarithm of the fraction is approximated by + * + * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). + * + * Otherwise, setting z = 2(x-1)/x+1), + * + * log(x) = z + z**3 P(z)/Q(z). + * + * + * ACCURACY: + * + * Relative error: + * arithmetic domain # trials peak rms + * IEEE 0.5, 2.0 30000 9.8e-20 2.7e-20 + * IEEE exp(+-10000) 70000 5.4e-20 2.3e-20 + * + * In the tests over the interval exp(+-10000), the logarithms + * of the random arguments were uniformly distributed over + * [-10000, +10000]. + * + * ERROR MESSAGES: + * + * log singularity: x = 0; returns -INFINITY + * log domain: x < 0; returns NAN + */ + +#include "math.h" + +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +long double log2l(long double x) +{ + return log2(x); +} +#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 +/* Coefficients for ln(1+x) = x - x**2/2 + x**3 P(x)/Q(x) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 6.2e-22 + */ +static long double P[] = { + 4.9962495940332550844739E-1L, + 1.0767376367209449010438E1L, + 7.7671073698359539859595E1L, + 2.5620629828144409632571E2L, + 4.2401812743503691187826E2L, + 3.4258224542413922935104E2L, + 1.0747524399916215149070E2L, +}; +static long double Q[] = { +/* 1.0000000000000000000000E0,*/ + 2.3479774160285863271658E1L, + 1.9444210022760132894510E2L, + 7.7952888181207260646090E2L, + 1.6911722418503949084863E3L, + 2.0307734695595183428202E3L, + 1.2695660352705325274404E3L, + 3.2242573199748645407652E2L, +}; + +/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), + * where z = 2(x-1)/(x+1) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 6.16e-22 + */ +static long double R[4] = { + 1.9757429581415468984296E-3L, +-7.1990767473014147232598E-1L, + 1.0777257190312272158094E1L, +-3.5717684488096787370998E1L, +}; +static long double S[4] = { +/* 1.00000000000000000000E0L,*/ +-2.6201045551331104417768E1L, + 1.9361891836232102174846E2L, +-4.2861221385716144629696E2L, +}; +/* log2(e) - 1 */ +#define LOG2EA 4.4269504088896340735992e-1L + +#define SQRTH 0.70710678118654752440L + +long double log2l(long double x) +{ + volatile long double z; + long double y; + int e; + + if (isnan(x)) + return x; + if (x == INFINITY) + return x; + if (x <= 0.0L) { + if (x == 0.0L) + return -INFINITY; + return NAN; + } + + /* separate mantissa from exponent */ + /* Note, frexp is used so that denormal numbers + * will be handled properly. + */ + x = frexpl(x, &e); + + /* logarithm using log(x) = z + z**3 P(z)/Q(z), + * where z = 2(x-1)/x+1) + */ + if (e > 2 || e < -2) { + if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ + e -= 1; + z = x - 0.5L; + y = 0.5L * z + 0.5L; + } else { /* 2 (x-1)/(x+1) */ + z = x - 0.5L; + z -= 0.5L; + y = 0.5L * x + 0.5L; + } + x = z / y; + z = x*x; + y = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); + goto done; + } + + /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ + if (x < SQRTH) { + e -= 1; + x = ldexpl(x, 1) - 1.0L; /* 2x - 1 */ + } else { + x = x - 1.0L; + } + z = x*x; + y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 7)); + y = y - ldexpl(z, -1); /* -0.5x^2 + ... */ + +done: + /* Multiply log of fraction by log2(e) + * and base 2 exponent by 1 + * + * ***CAUTION*** + * + * This sequence of operations is critical and it may + * be horribly defeated by some compiler optimizers. + */ + z = y * LOG2EA; + z += x * LOG2EA; + z += y; + z += x; + z += e; + return z; +} +#endif diff --git a/05/musl-final/src/math/logb.c b/05/musl-final/src/math/logb.c new file mode 100644 index 0000000..2eb81f4 --- /dev/null +++ b/05/musl-final/src/math/logb.c @@ -0,0 +1,20 @@ +#include +#include "math.h" + +/* +special cases: + logb(+-0) = -inf + logb(+-inf) = +inf + logb(nan) = nan +these are calculated at runtime to raise fp exceptions +*/ + +double logb(double x) { + int i = ilogb(x); + + if (i == FP_ILOGB0) + return -1.0/fabs(x); + if (i == FP_ILOGBNAN || i == INT_MAX) + return x * x; + return i; +} diff --git a/05/musl-final/src/math/logbf.c b/05/musl-final/src/math/logbf.c new file mode 100644 index 0000000..c04eb29 --- /dev/null +++ b/05/musl-final/src/math/logbf.c @@ -0,0 +1,12 @@ +#include +#include "math.h" + +float logbf(float x) { + int i = ilogbf(x); + + if (i == FP_ILOGB0) + return -1.0f/fabsf(x); + if (i == FP_ILOGBNAN || i == INT_MAX) + return x * x; + return i; +} diff --git a/05/musl-final/src/math/logbl.c b/05/musl-final/src/math/logbl.c new file mode 100644 index 0000000..704c853 --- /dev/null +++ b/05/musl-final/src/math/logbl.c @@ -0,0 +1,19 @@ +#include +#include "math.h" +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +long double logbl(long double x) +{ + return logb(x); +} +#else +long double logbl(long double x) +{ + int i = ilogbl(x); + + if (i == FP_ILOGB0) + return -1.0/fabsl(x); + if (i == FP_ILOGBNAN || i == INT_MAX) + return x * x; + return i; +} +#endif diff --git a/05/musl-final/src/math/logf.c b/05/musl-final/src/math/logf.c new file mode 100644 index 0000000..9e49b7a --- /dev/null +++ b/05/musl-final/src/math/logf.c @@ -0,0 +1,90 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */ +/* + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "math.h" +#include + +static const float +ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ +ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ +two25 = 3.355443200e+07, /* 0x4c000000 */ +/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */ +Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */ +Lg2 = 0xccce13.0p-25, /* 0.40000972152 */ +Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */ +Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */ + +static const float zero = 0.0; + +float logf(float x) +{ + float hfsq,f,s,z,R,w,t1,t2,dk; + int32_t k,ix,i,j; + + GET_FLOAT_WORD(ix, x); + + k = 0; + if (ix < 0x00800000) { /* x < 2**-126 */ + if ((ix & 0x7fffffff) == 0) + return -two25/zero; /* log(+-0)=-inf */ + if (ix < 0) + return (x-x)/zero; /* log(-#) = NaN */ + /* subnormal number, scale up x */ + k -= 25; + x *= two25; + GET_FLOAT_WORD(ix, x); + } + if (ix >= 0x7f800000) + return x+x; + k += (ix>>23) - 127; + ix &= 0x007fffff; + i = (ix + (0x95f64<<3)) & 0x800000; + SET_FLOAT_WORD(x, ix|(i^0x3f800000)); /* normalize x or x/2 */ + k += i>>23; + f = x - 1.0f; + if ((0x007fffff & (0x8000 + ix)) < 0xc000) { /* -2**-9 <= f < 2**-9 */ + if (f == zero) { + if (k == 0) + return zero; + dk = (float)k; + return dk*ln2_hi + dk*ln2_lo; + } + R = f*f*(0.5f - 0.33333333333333333f*f); + if (k == 0) + return f-R; + dk = (float)k; + return dk*ln2_hi - ((R-dk*ln2_lo)-f); + } + s = f/(2.0f + f); + dk = (float)k; + z = s*s; + i = ix-(0x6147a<<3); + w = z*z; + j = (0x6b851<<3)-ix; + t1= w*(Lg2+w*Lg4); + t2= z*(Lg1+w*Lg3); + i |= j; + R = t2 + t1; + if (i > 0) { + hfsq = 0.5f * f * f; + if (k == 0) + return f - (hfsq-s*(hfsq+R)); + return dk*ln2_hi - ((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); + } else { + if (k == 0) + return f - s*(f-R); + return dk*ln2_hi - ((s*(f-R)-dk*ln2_lo)-f); + } +} diff --git a/05/musl-final/src/math/logl.c b/05/musl-final/src/math/logl.c new file mode 100644 index 0000000..15d7083 --- /dev/null +++ b/05/musl-final/src/math/logl.c @@ -0,0 +1,174 @@ +/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_logl.c */ +/* + * Copyright (c) 2008 Stephen L. Moshier + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* + * Natural logarithm, long double precision + * + * + * SYNOPSIS: + * + * long double x, y, logl(); + * + * y = logl( x ); + * + * + * DESCRIPTION: + * + * Returns the base e (2.718...) logarithm of x. + * + * The argument is separated into its exponent and fractional + * parts. If the exponent is between -1 and +1, the logarithm + * of the fraction is approximated by + * + * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). + * + * Otherwise, setting z = 2(x-1)/x+1), + * + * log(x) = z + z**3 P(z)/Q(z). + * + * + * ACCURACY: + * + * Relative error: + * arithmetic domain # trials peak rms + * IEEE 0.5, 2.0 150000 8.71e-20 2.75e-20 + * IEEE exp(+-10000) 100000 5.39e-20 2.34e-20 + * + * In the tests over the interval exp(+-10000), the logarithms + * of the random arguments were uniformly distributed over + * [-10000, +10000]. + * + * ERROR MESSAGES: + * + * log singularity: x = 0; returns -INFINITY + * log domain: x < 0; returns NAN + */ + +#include "math.h" + +#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +long double logl(long double x) +{ + return log(x); +} +#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 +/* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 2.32e-20 + */ +static long double P[] = { + 4.5270000862445199635215E-5L, + 4.9854102823193375972212E-1L, + 6.5787325942061044846969E0L, + 2.9911919328553073277375E1L, + 6.0949667980987787057556E1L, + 5.7112963590585538103336E1L, + 2.0039553499201281259648E1L, +}; +static long double Q[] = { +/* 1.0000000000000000000000E0,*/ + 1.5062909083469192043167E1L, + 8.3047565967967209469434E1L, + 2.2176239823732856465394E2L, + 3.0909872225312059774938E2L, + 2.1642788614495947685003E2L, + 6.0118660497603843919306E1L, +}; + +/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), + * where z = 2(x-1)/(x+1) + * 1/sqrt(2) <= x < sqrt(2) + * Theoretical peak relative error = 6.16e-22 + */ +static long double R[4] = { + 1.9757429581415468984296E-3L, +-7.1990767473014147232598E-1L, + 1.0777257190312272158094E1L, +-3.5717684488096787370998E1L, +}; +static long double S[4] = { +/* 1.00000000000000000000E0L,*/ +-2.6201045551331104417768E1L, + 1.9361891836232102174846E2L, +-4.2861221385716144629696E2L, +}; +static const long double C1 = 6.9314575195312500000000E-1L; +static const long double C2 = 1.4286068203094172321215E-6L; + +#define SQRTH 0.70710678118654752440L + +long double logl(long double x) +{ + long double y, z; + int e; + + if (isnan(x)) + return x; + if (x == INFINITY) + return x; + if (x <= 0.0L) { + if (x == 0.0L) + return -INFINITY; + return NAN; + } + + /* separate mantissa from exponent */ + /* Note, frexp is used so that denormal numbers + * will be handled properly. + */ + x = frexpl(x, &e); + + /* logarithm using log(x) = z + z**3 P(z)/Q(z), + * where z = 2(x-1)/x+1) + */ + if (e > 2 || e < -2) { + if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ + e -= 1; + z = x - 0.5L; + y = 0.5L * z + 0.5L; + } else { /* 2 (x-1)/(x+1) */ + z = x - 0.5L; + z -= 0.5L; + y = 0.5L * x + 0.5L; + } + x = z / y; + z = x*x; + z = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); + z = z + e * C2; + z = z + x; + z = z + e * C1; + return z; + } + + /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ + if (x < SQRTH) { + e -= 1; + x = ldexpl(x, 1) - 1.0L; /* 2x - 1 */ + } else { + x = x - 1.0L; + } + z = x*x; + y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 6)); + y = y + e * C2; + z = y - ldexpl(z, -1); /* y - 0.5 * z */ + /* Note, the sum of above terms does not exceed x/4, + * so it contributes at most about 1/4 lsb to the error. + */ + z = z + x; + z = z + e * C1; /* This sum has an error of 1/2 lsb. */ + return z; +} +#endif diff --git a/05/musl-final/src/math/math_private.h b/05/musl-final/src/math/math_private.h new file mode 100644 index 0000000..28a6a19 --- /dev/null +++ b/05/musl-final/src/math/math_private.h @@ -0,0 +1,143 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#ifndef _MATH_PRIVATE_H_ +#define _MATH_PRIVATE_H_ + +#include + +/* + * The original fdlibm code used statements like: + * n0 = ((*(int*)&one)>>29)^1; * index of high word * + * ix0 = *(n0+(int*)&x); * high word of x * + * ix1 = *((1-n0)+(int*)&x); * low word of x * + * to dig two 32 bit words out of the 64 bit IEEE floating point + * value. That is non-ANSI, and, moreover, the gcc instruction + * scheduler gets it wrong. We instead use the following macros. + * Unlike the original code, we determine the endianness at compile + * time, not at run time; I don't see much benefit to selecting + * endianness at run time. + */ + +/* + * A union which permits us to convert between a double and two 32 bit + * ints. + */ + +typedef union +{ + double value; + uint64_t words; +} ieee_double_shape_type; + +/* Get two 32 bit ints from a double. */ + +#define EXTRACT_WORDS(ix0,ix1,d) \ +do { \ + ieee_double_shape_type ew_u; \ + ew_u.value = (d); \ + (ix0) = ew_u.words >> 32; \ + (ix1) = (uint32_t)ew_u.words; \ +} while (0) + +/* Get the more significant 32 bit int from a double. */ + +#define GET_HIGH_WORD(i,d) \ +do { \ + ieee_double_shape_type gh_u; \ + gh_u.value = (d); \ + (i) = gh_u.words >> 32; \ +} while (0) + +/* Get the less significant 32 bit int from a double. */ + +#define GET_LOW_WORD(i,d) \ +do { \ + ieee_double_shape_type gl_u; \ + gl_u.value = (d); \ + (i) = (uint32_t)gl_u.words; \ +} while (0) + +/* Set a double from two 32 bit ints. */ + +#define INSERT_WORDS(d,ix0,ix1) \ +do { \ + ieee_double_shape_type iw_u; \ + iw_u.words = ((uint64_t)(ix0) << 32) | (ix1); \ + (d) = iw_u.value; \ +} while (0) + +/* Set the more significant 32 bits of a double from an int. */ + +#define SET_HIGH_WORD(d,v) \ +do { \ + ieee_double_shape_type sh_u; \ + sh_u.value = (d); \ + sh_u.words &= 0xffffffff; \ + sh_u.words |= ((uint64_t)(v) << 32); \ + (d) = sh_u.value; \ +} while (0) + +/* Set the less significant 32 bits of a double from an int. */ + +#define SET_LOW_WORD(d,v) \ +do { \ + ieee_double_shape_type sl_u; \ + sl_u.value = (d); \ + sl_u.words &= 0xffffffff00000000ull; \ + sl_u.words |= (uint32_t)(v); \ + (d) = sl_u.value; \ +} while (0) + +/* + * A union which permits us to convert between a float and a 32 bit + * int. + */ + +typedef union +{ + float value; + uint32_t word; +} ieee_float_shape_type; + +/* Get a 32 bit int from a float. */ + +#define GET_FLOAT_WORD(i,d) \ +do { \ + ieee_float_shape_type gf_u; \ + gf_u.value = (d); \ + (i) = gf_u.word; \ +} while (0) + +/* Set a float from a 32 bit int. */ + +#define SET_FLOAT_WORD(d,i) \ +do { \ + ieee_float_shape_type sf_u; \ + sf_u.word = (i); \ + (d) = sf_u.value; \ +} while (0) + +/* fdlibm kernel function */ +int __ieee754_rem_pio2(double,double*); +double __kernel_sin(double,double,int); +double __kernel_cos(double,double); +double __kernel_tan(double,double,int); +int __kernel_rem_pio2(double*,double*,int,int,int,const int*); + +/* float versions of fdlibm kernel functions */ +int __ieee754_rem_pio2f(float,float*); +float __kernel_sinf(float,float,int); +float __kernel_cosf(float,float); +float __kernel_tanf(float,float,int); +int __kernel_rem_pio2f(float*,float*,int,int,int,const int*); + +#endif /* !_MATH_PRIVATE_H_ */ diff --git a/05/musl-final/src/math/s_asinh.c b/05/musl-final/src/math/s_asinh.c new file mode 100644 index 0000000..2601609 --- /dev/null +++ b/05/musl-final/src/math/s_asinh.c @@ -0,0 +1,53 @@ +/* @(#)s_asinh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* asinh(x) + * Method : + * Based on + * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ] + * we have + * asinh(x) := x if 1+x*x=1, + * := sign(x)*(log(x)+ln2)) for large |x|, else + * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else + * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2))) + */ + +#include +#include "math_private.h" + +static const double +one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ +ln2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */ +huge= 1.00000000000000000000e+300; + +double +asinh(double x) +{ + double t,w; + int32_t hx,ix; + GET_HIGH_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x7ff00000) return x+x; /* x is inf or NaN */ + if(ix< 0x3e300000) { /* |x|<2**-28 */ + if(huge+x>one) return x; /* return x inexact except 0 */ + } + if(ix>0x41b00000) { /* |x| > 2**28 */ + w = log(fabs(x))+ln2; + } else if (ix>0x40000000) { /* 2**28 > |x| > 2.0 */ + t = fabs(x); + w = log(2.0*t+one/(sqrt(x*x+one)+t)); + } else { /* 2.0 > |x| > 2**-28 */ + t = x*x; + w =log1p(fabs(x)+t/(one+sqrt(one+t))); + } + if(hx>0) return w; else return -w; +} diff --git a/05/musl-final/src/math/s_asinhf.c b/05/musl-final/src/math/s_asinhf.c new file mode 100644 index 0000000..04f8d07 --- /dev/null +++ b/05/musl-final/src/math/s_asinhf.c @@ -0,0 +1,45 @@ +/* s_asinhf.c -- float version of s_asinh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +one = 1.0000000000e+00, /* 0x3F800000 */ +ln2 = 6.9314718246e-01, /* 0x3f317218 */ +huge= 1.0000000000e+30; + +float +asinhf(float x) +{ + float t,w; + int32_t hx,ix; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x7f800000) return x+x; /* x is inf or NaN */ + if(ix< 0x31800000) { /* |x|<2**-28 */ + if(huge+x>one) return x; /* return x inexact except 0 */ + } + if(ix>0x4d800000) { /* |x| > 2**28 */ + w = logf(fabsf(x))+ln2; + } else if (ix>0x40000000) { /* 2**28 > |x| > 2.0 */ + t = fabsf(x); + w = logf((float)2.0*t+one/(sqrtf(x*x+one)+t)); + } else { /* 2.0 > |x| > 2**-28 */ + t = x*x; + w =log1pf(fabsf(x)+t/(one+sqrtf(one+t))); + } + if(hx>0) return w; else return -w; +} diff --git a/05/musl-final/src/math/s_atan.c b/05/musl-final/src/math/s_atan.c new file mode 100644 index 0000000..1faac02 --- /dev/null +++ b/05/musl-final/src/math/s_atan.c @@ -0,0 +1,115 @@ +/* @(#)s_atan.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* atan(x) + * Method + * 1. Reduce x to positive by atan(x) = -atan(-x). + * 2. According to the integer k=4t+0.25 chopped, t=x, the argument + * is further reduced to one of the following intervals and the + * arctangent of t is evaluated by the corresponding formula: + * + * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) + * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) + * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) + * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) + * [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const double atanhi[] = { + 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ + 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ + 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ + 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ +}; + +static const double atanlo[] = { + 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ + 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ + 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ + 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */ +}; + +static const double aT[] = { + 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */ + -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */ + 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */ + -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */ + 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */ + -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */ + 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */ + -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */ + 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */ + -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */ + 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */ +}; + + static const double +one = 1.0, +huge = 1.0e300; + +double +atan(double x) +{ + double w,s1,s2,z; + int32_t ix,hx,id; + + GET_HIGH_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x44100000) { /* if |x| >= 2^66 */ + uint32_t low; + GET_LOW_WORD(low,x); + if(ix>0x7ff00000|| + (ix==0x7ff00000&&(low!=0))) + return x+x; /* NaN */ + if(hx>0) return atanhi[3]+atanlo[3]; + else return -atanhi[3]-atanlo[3]; + } if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ + if (ix < 0x3e200000) { /* |x| < 2^-29 */ + if(huge+x>one) return x; /* raise inexact */ + } + id = -1; + } else { + x = fabs(x); + if (ix < 0x3ff30000) { /* |x| < 1.1875 */ + if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */ + id = 0; x = (2.0*x-one)/(2.0+x); + } else { /* 11/16<=|x|< 19/16 */ + id = 1; x = (x-one)/(x+one); + } + } else { + if (ix < 0x40038000) { /* |x| < 2.4375 */ + id = 2; x = (x-1.5)/(one+1.5*x); + } else { /* 2.4375 <= |x| < 2^66 */ + id = 3; x = -1.0/x; + } + }} + /* end of argument reduction */ + z = x*x; + w = z*z; + /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ + s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10]))))); + s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9])))); + if (id<0) return x - x*(s1+s2); + else { + z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x); + return (hx<0)? -z:z; + } +} diff --git a/05/musl-final/src/math/s_atanf.c b/05/musl-final/src/math/s_atanf.c new file mode 100644 index 0000000..03067e1 --- /dev/null +++ b/05/musl-final/src/math/s_atanf.c @@ -0,0 +1,95 @@ +/* s_atanf.c -- float version of s_atan.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float atanhi[] = { + 4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */ + 7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */ + 9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */ + 1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */ +}; + +static const float atanlo[] = { + 5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */ + 3.7748947079e-08, /* atan(1.0)lo 0x33222168 */ + 3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */ + 7.5497894159e-08, /* atan(inf)lo 0x33a22168 */ +}; + +static const float aT[] = { + 3.3333334327e-01, /* 0x3eaaaaaa */ + -2.0000000298e-01, /* 0xbe4ccccd */ + 1.4285714924e-01, /* 0x3e124925 */ + -1.1111110449e-01, /* 0xbde38e38 */ + 9.0908870101e-02, /* 0x3dba2e6e */ + -7.6918758452e-02, /* 0xbd9d8795 */ + 6.6610731184e-02, /* 0x3d886b35 */ + -5.8335702866e-02, /* 0xbd6ef16b */ + 4.9768779427e-02, /* 0x3d4bda59 */ + -3.6531571299e-02, /* 0xbd15a221 */ + 1.6285819933e-02, /* 0x3c8569d7 */ +}; + + static const float +one = 1.0, +huge = 1.0e30; + +float +atanf(float x) +{ + float w,s1,s2,z; + int32_t ix,hx,id; + + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x50800000) { /* if |x| >= 2^34 */ + if(ix>0x7f800000) + return x+x; /* NaN */ + if(hx>0) return atanhi[3]+atanlo[3]; + else return -atanhi[3]-atanlo[3]; + } if (ix < 0x3ee00000) { /* |x| < 0.4375 */ + if (ix < 0x31000000) { /* |x| < 2^-29 */ + if(huge+x>one) return x; /* raise inexact */ + } + id = -1; + } else { + x = fabsf(x); + if (ix < 0x3f980000) { /* |x| < 1.1875 */ + if (ix < 0x3f300000) { /* 7/16 <=|x|<11/16 */ + id = 0; x = ((float)2.0*x-one)/((float)2.0+x); + } else { /* 11/16<=|x|< 19/16 */ + id = 1; x = (x-one)/(x+one); + } + } else { + if (ix < 0x401c0000) { /* |x| < 2.4375 */ + id = 2; x = (x-(float)1.5)/(one+(float)1.5*x); + } else { /* 2.4375 <= |x| < 2^66 */ + id = 3; x = -(float)1.0/x; + } + }} + /* end of argument reduction */ + z = x*x; + w = z*z; + /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ + s1 = z*(aT[0]+w*(aT[2]+w*(aT[4]+w*(aT[6]+w*(aT[8]+w*aT[10]))))); + s2 = w*(aT[1]+w*(aT[3]+w*(aT[5]+w*(aT[7]+w*aT[9])))); + if (id<0) return x - x*(s1+s2); + else { + z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x); + return (hx<0)? -z:z; + } +} diff --git a/05/musl-final/src/math/s_cbrt.c b/05/musl-final/src/math/s_cbrt.c new file mode 100644 index 0000000..8adcb19 --- /dev/null +++ b/05/musl-final/src/math/s_cbrt.c @@ -0,0 +1,77 @@ +/* @(#)s_cbrt.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +/* cbrt(x) + * Return cube root of x + */ +static const uint32_t + B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ + B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ + +static const double +C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ +D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ +E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ +F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ +G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ + +double +cbrt(double x) +{ + int32_t hx; + double r,s,t=0.0,w; + uint32_t sign; + uint32_t high,low; + + GET_HIGH_WORD(hx,x); + sign=hx&0x80000000; /* sign= sign(x) */ + hx ^=sign; + if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ + GET_LOW_WORD(low,x); + if((hx|low)==0) + return(x); /* cbrt(0) is itself */ + + SET_HIGH_WORD(x,hx); /* x <- |x| */ + /* rough cbrt to 5 bits */ + if(hx<0x00100000) /* subnormal number */ + {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ + t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); + } + else + SET_HIGH_WORD(t,hx/3+B1); + + + /* new cbrt to 23 bits, may be implemented in single precision */ + r=t*t/x; + s=C+r*t; + t*=G+F/(s+E+D/s); + + /* chopped to 20 bits and make it larger than cbrt(x) */ + GET_HIGH_WORD(high,t); + INSERT_WORDS(t,high+0x00000001,0); + + + /* one step newton iteration to 53 bits with error less than 0.667 ulps */ + s=t*t; /* t*t is exact */ + r=x/s; + w=t+t; + r=(r-t)/(w+r); /* r-s is exact */ + t=t+t*r; + + /* retore the sign bit */ + GET_HIGH_WORD(high,t); + SET_HIGH_WORD(t,high|sign); + return(t); +} diff --git a/05/musl-final/src/math/s_cbrtf.c b/05/musl-final/src/math/s_cbrtf.c new file mode 100644 index 0000000..e7b46de --- /dev/null +++ b/05/musl-final/src/math/s_cbrtf.c @@ -0,0 +1,67 @@ +/* s_cbrtf.c -- float version of s_cbrt.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +/* cbrtf(x) + * Return cube root of x + */ +static const unsigned + B1 = 709958130, /* B1 = (84+2/3-0.03306235651)*2**23 */ + B2 = 642849266; /* B2 = (76+2/3-0.03306235651)*2**23 */ + +static const float +C = 5.4285717010e-01, /* 19/35 = 0x3f0af8b0 */ +D = -7.0530611277e-01, /* -864/1225 = 0xbf348ef1 */ +E = 1.4142856598e+00, /* 99/70 = 0x3fb50750 */ +F = 1.6071428061e+00, /* 45/28 = 0x3fcdb6db */ +G = 3.5714286566e-01; /* 5/14 = 0x3eb6db6e */ + +float +cbrtf(float x) +{ + float r,s,t; + int32_t hx; + uint32_t sign; + uint32_t high; + + GET_FLOAT_WORD(hx,x); + sign=hx&0x80000000; /* sign= sign(x) */ + hx ^=sign; + if(hx>=0x7f800000) return(x+x); /* cbrt(NaN,INF) is itself */ + if(hx==0) + return(x); /* cbrt(0) is itself */ + + SET_FLOAT_WORD(x,hx); /* x <- |x| */ + /* rough cbrt to 5 bits */ + if(hx<0x00800000) /* subnormal number */ + {SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */ + t*=x; GET_FLOAT_WORD(high,t); SET_FLOAT_WORD(t,high/3+B2); + } + else + SET_FLOAT_WORD(t,hx/3+B1); + + + /* new cbrt to 23 bits */ + r=t*t/x; + s=C+r*t; + t*=G+F/(s+E+D/s); + + /* retore the sign bit */ + GET_FLOAT_WORD(high,t); + SET_FLOAT_WORD(t,high|sign); + return(t); +} diff --git a/05/musl-final/src/math/s_ceil.c b/05/musl-final/src/math/s_ceil.c new file mode 100644 index 0000000..1670cad --- /dev/null +++ b/05/musl-final/src/math/s_ceil.c @@ -0,0 +1,68 @@ +/* @(#)s_ceil.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * ceil(x) + * Return x rounded toward -inf to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to ceil(x). + */ + +#include +#include "math_private.h" + +static const double huge = 1.0e300; + +double +ceil(double x) +{ + int32_t i0,i1,j0; + uint32_t i,j; + EXTRACT_WORDS(i0,i1,x); + j0 = ((i0>>20)&0x7ff)-0x3ff; + if(j0<20) { + if(j0<0) { /* raise inexact if x != 0 */ + if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ + if(i0<0) {i0=0x80000000;i1=0;} + else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;} + } + } else { + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) return x; /* x is integral */ + if(huge+x>0.0) { /* raise inexact flag */ + if(i0>0) i0 += (0x00100000)>>j0; + i0 &= (~i); i1=0; + } + } + } else if (j0>51) { + if(j0==0x400) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } else { + i = ((uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) return x; /* x is integral */ + if(huge+x>0.0) { /* raise inexact flag */ + if(i0>0) { + if(j0==20) i0+=1; + else { + j = i1 + (1<<(52-j0)); + if(j +#include "math_private.h" + +static const float huge = 1.0e30; + +float +ceilf(float x) +{ + int32_t i0,j0; + uint32_t i; + + GET_FLOAT_WORD(i0,x); + j0 = ((i0>>23)&0xff)-0x7f; + if(j0<23) { + if(j0<0) { /* raise inexact if x != 0 */ + if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */ + if(i0<0) {i0=0x80000000;} + else if(i0!=0) { i0=0x3f800000;} + } + } else { + i = (0x007fffff)>>j0; + if((i0&i)==0) return x; /* x is integral */ + if(huge+x>(float)0.0) { /* raise inexact flag */ + if(i0>0) i0 += (0x00800000)>>j0; + i0 &= (~i); + } + } + } else { + if(j0==0x80) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } + SET_FLOAT_WORD(x,i0); + return x; +} diff --git a/05/musl-final/src/math/s_copysign.c b/05/musl-final/src/math/s_copysign.c new file mode 100644 index 0000000..59d3877 --- /dev/null +++ b/05/musl-final/src/math/s_copysign.c @@ -0,0 +1,30 @@ +/* @(#)s_copysign.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * copysign(double x, double y) + * copysign(x,y) returns a value with the magnitude of x and + * with the sign bit of y. + */ + +#include +#include "math_private.h" + +double +copysign(double x, double y) +{ + uint32_t hx,hy; + GET_HIGH_WORD(hx,x); + GET_HIGH_WORD(hy,y); + SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000)); + return x; +} diff --git a/05/musl-final/src/math/s_copysignf.c b/05/musl-final/src/math/s_copysignf.c new file mode 100644 index 0000000..d650e8e --- /dev/null +++ b/05/musl-final/src/math/s_copysignf.c @@ -0,0 +1,33 @@ +/* s_copysignf.c -- float version of s_copysign.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * copysignf(float x, float y) + * copysignf(x,y) returns a value with the magnitude of x and + * with the sign bit of y. + */ + +#include +#include "math_private.h" + +float +copysignf(float x, float y) +{ + uint32_t ix,iy; + GET_FLOAT_WORD(ix,x); + GET_FLOAT_WORD(iy,y); + SET_FLOAT_WORD(x,(ix&0x7fffffff)|(iy&0x80000000)); + return x; +} diff --git a/05/musl-final/src/math/s_cos.c b/05/musl-final/src/math/s_cos.c new file mode 100644 index 0000000..1893ab1 --- /dev/null +++ b/05/musl-final/src/math/s_cos.c @@ -0,0 +1,74 @@ +/* @(#)s_cos.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* cos(x) + * Return cosine function of x. + * + * kernel function: + * __kernel_sin ... sine function on [-pi/4,pi/4] + * __kernel_cos ... cosine function on [-pi/4,pi/4] + * __ieee754_rem_pio2 ... argument reduction routine + * + * Method. + * Let S,C and T denote the sin, cos and tan respectively on + * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 + * in [-pi/4 , +pi/4], and let n = k mod 4. + * We have + * + * n sin(x) cos(x) tan(x) + * ---------------------------------------------------------- + * 0 S C T + * 1 C -S -1/T + * 2 -S -C T + * 3 -C S -1/T + * ---------------------------------------------------------- + * + * Special cases: + * Let trig be any of sin, cos, or tan. + * trig(+-INF) is NaN, with signals; + * trig(NaN) is that NaN; + * + * Accuracy: + * TRIG(x) returns trig(x) nearly rounded + */ + +#include +#include "math_private.h" + +double +cos(double x) +{ + double y[2],z=0.0; + int32_t n, ix; + + /* High word of x. */ + GET_HIGH_WORD(ix,x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if(ix <= 0x3fe921fb) return __kernel_cos(x,z); + + /* cos(Inf or NaN) is NaN */ + else if (ix>=0x7ff00000) return x-x; + + /* argument reduction needed */ + else { + n = __ieee754_rem_pio2(x,y); + switch(n&3) { + case 0: return __kernel_cos(y[0],y[1]); + case 1: return -__kernel_sin(y[0],y[1],1); + case 2: return -__kernel_cos(y[0],y[1]); + default: + return __kernel_sin(y[0],y[1],1); + } + } +} diff --git a/05/musl-final/src/math/s_cosf.c b/05/musl-final/src/math/s_cosf.c new file mode 100644 index 0000000..14b8e98 --- /dev/null +++ b/05/musl-final/src/math/s_cosf.c @@ -0,0 +1,47 @@ +/* s_cosf.c -- float version of s_cos.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one=1.0; + +float +cosf(float x) +{ + float y[2],z=0.0; + int32_t n,ix; + + GET_FLOAT_WORD(ix,x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if(ix <= 0x3f490fd8) return __kernel_cosf(x,z); + + /* cos(Inf or NaN) is NaN */ + else if (ix>=0x7f800000) return x-x; + + /* argument reduction needed */ + else { + n = __ieee754_rem_pio2f(x,y); + switch(n&3) { + case 0: return __kernel_cosf(y[0],y[1]); + case 1: return -__kernel_sinf(y[0],y[1],1); + case 2: return -__kernel_cosf(y[0],y[1]); + default: + return __kernel_sinf(y[0],y[1],1); + } + } +} diff --git a/05/musl-final/src/math/s_erf.c b/05/musl-final/src/math/s_erf.c new file mode 100644 index 0000000..e321fee --- /dev/null +++ b/05/musl-final/src/math/s_erf.c @@ -0,0 +1,298 @@ +/* @(#)s_erf.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* double erf(double x) + * double erfc(double x) + * x + * 2 |\ + * erf(x) = --------- | exp(-t*t)dt + * sqrt(pi) \| + * 0 + * + * erfc(x) = 1-erf(x) + * Note that + * erf(-x) = -erf(x) + * erfc(-x) = 2 - erfc(x) + * + * Method: + * 1. For |x| in [0, 0.84375] + * erf(x) = x + x*R(x^2) + * erfc(x) = 1 - erf(x) if x in [-.84375,0.25] + * = 0.5 + ((0.5-x)-x*R) if x in [0.25,0.84375] + * where R = P/Q where P is an odd poly of degree 8 and + * Q is an odd poly of degree 10. + * -57.90 + * | R - (erf(x)-x)/x | <= 2 + * + * + * Remark. The formula is derived by noting + * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....) + * and that + * 2/sqrt(pi) = 1.128379167095512573896158903121545171688 + * is close to one. The interval is chosen because the fix + * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is + * near 0.6174), and by some experiment, 0.84375 is chosen to + * guarantee the error is less than one ulp for erf. + * + * 2. For |x| in [0.84375,1.25], let s = |x| - 1, and + * c = 0.84506291151 rounded to single (24 bits) + * erf(x) = sign(x) * (c + P1(s)/Q1(s)) + * erfc(x) = (1-c) - P1(s)/Q1(s) if x > 0 + * 1+(c+P1(s)/Q1(s)) if x < 0 + * |P1/Q1 - (erf(|x|)-c)| <= 2**-59.06 + * Remark: here we use the taylor series expansion at x=1. + * erf(1+s) = erf(1) + s*Poly(s) + * = 0.845.. + P1(s)/Q1(s) + * That is, we use rational approximation to approximate + * erf(1+s) - (c = (single)0.84506291151) + * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25] + * where + * P1(s) = degree 6 poly in s + * Q1(s) = degree 6 poly in s + * + * 3. For x in [1.25,1/0.35(~2.857143)], + * erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1) + * erf(x) = 1 - erfc(x) + * where + * R1(z) = degree 7 poly in z, (z=1/x^2) + * S1(z) = degree 8 poly in z + * + * 4. For x in [1/0.35,28] + * erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0 + * = 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6 x >= 28 + * erf(x) = sign(x) *(1 - tiny) (raise inexact) + * erfc(x) = tiny*tiny (raise underflow) if x > 0 + * = 2 - tiny if x<0 + * + * 7. Special case: + * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1, + * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2, + * erfc/erf(NaN) is NaN + */ + + +#include +#include "math_private.h" + +static const double +tiny = 1e-300, +half= 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */ +one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ +two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */ + /* c = (float)0.84506291151 */ +erx = 8.45062911510467529297e-01, /* 0x3FEB0AC1, 0x60000000 */ +/* + * Coefficients for approximation to erf on [0,0.84375] + */ +efx = 1.28379167095512586316e-01, /* 0x3FC06EBA, 0x8214DB69 */ +efx8= 1.02703333676410069053e+00, /* 0x3FF06EBA, 0x8214DB69 */ +pp0 = 1.28379167095512558561e-01, /* 0x3FC06EBA, 0x8214DB68 */ +pp1 = -3.25042107247001499370e-01, /* 0xBFD4CD7D, 0x691CB913 */ +pp2 = -2.84817495755985104766e-02, /* 0xBF9D2A51, 0xDBD7194F */ +pp3 = -5.77027029648944159157e-03, /* 0xBF77A291, 0x236668E4 */ +pp4 = -2.37630166566501626084e-05, /* 0xBEF8EAD6, 0x120016AC */ +qq1 = 3.97917223959155352819e-01, /* 0x3FD97779, 0xCDDADC09 */ +qq2 = 6.50222499887672944485e-02, /* 0x3FB0A54C, 0x5536CEBA */ +qq3 = 5.08130628187576562776e-03, /* 0x3F74D022, 0xC4D36B0F */ +qq4 = 1.32494738004321644526e-04, /* 0x3F215DC9, 0x221C1A10 */ +qq5 = -3.96022827877536812320e-06, /* 0xBED09C43, 0x42A26120 */ +/* + * Coefficients for approximation to erf in [0.84375,1.25] + */ +pa0 = -2.36211856075265944077e-03, /* 0xBF6359B8, 0xBEF77538 */ +pa1 = 4.14856118683748331666e-01, /* 0x3FDA8D00, 0xAD92B34D */ +pa2 = -3.72207876035701323847e-01, /* 0xBFD7D240, 0xFBB8C3F1 */ +pa3 = 3.18346619901161753674e-01, /* 0x3FD45FCA, 0x805120E4 */ +pa4 = -1.10894694282396677476e-01, /* 0xBFBC6398, 0x3D3E28EC */ +pa5 = 3.54783043256182359371e-02, /* 0x3FA22A36, 0x599795EB */ +pa6 = -2.16637559486879084300e-03, /* 0xBF61BF38, 0x0A96073F */ +qa1 = 1.06420880400844228286e-01, /* 0x3FBB3E66, 0x18EEE323 */ +qa2 = 5.40397917702171048937e-01, /* 0x3FE14AF0, 0x92EB6F33 */ +qa3 = 7.18286544141962662868e-02, /* 0x3FB2635C, 0xD99FE9A7 */ +qa4 = 1.26171219808761642112e-01, /* 0x3FC02660, 0xE763351F */ +qa5 = 1.36370839120290507362e-02, /* 0x3F8BEDC2, 0x6B51DD1C */ +qa6 = 1.19844998467991074170e-02, /* 0x3F888B54, 0x5735151D */ +/* + * Coefficients for approximation to erfc in [1.25,1/0.35] + */ +ra0 = -9.86494403484714822705e-03, /* 0xBF843412, 0x600D6435 */ +ra1 = -6.93858572707181764372e-01, /* 0xBFE63416, 0xE4BA7360 */ +ra2 = -1.05586262253232909814e+01, /* 0xC0251E04, 0x41B0E726 */ +ra3 = -6.23753324503260060396e+01, /* 0xC04F300A, 0xE4CBA38D */ +ra4 = -1.62396669462573470355e+02, /* 0xC0644CB1, 0x84282266 */ +ra5 = -1.84605092906711035994e+02, /* 0xC067135C, 0xEBCCABB2 */ +ra6 = -8.12874355063065934246e+01, /* 0xC0545265, 0x57E4D2F2 */ +ra7 = -9.81432934416914548592e+00, /* 0xC023A0EF, 0xC69AC25C */ +sa1 = 1.96512716674392571292e+01, /* 0x4033A6B9, 0xBD707687 */ +sa2 = 1.37657754143519042600e+02, /* 0x4061350C, 0x526AE721 */ +sa3 = 4.34565877475229228821e+02, /* 0x407B290D, 0xD58A1A71 */ +sa4 = 6.45387271733267880336e+02, /* 0x40842B19, 0x21EC2868 */ +sa5 = 4.29008140027567833386e+02, /* 0x407AD021, 0x57700314 */ +sa6 = 1.08635005541779435134e+02, /* 0x405B28A3, 0xEE48AE2C */ +sa7 = 6.57024977031928170135e+00, /* 0x401A47EF, 0x8E484A93 */ +sa8 = -6.04244152148580987438e-02, /* 0xBFAEEFF2, 0xEE749A62 */ +/* + * Coefficients for approximation to erfc in [1/.35,28] + */ +rb0 = -9.86494292470009928597e-03, /* 0xBF843412, 0x39E86F4A */ +rb1 = -7.99283237680523006574e-01, /* 0xBFE993BA, 0x70C285DE */ +rb2 = -1.77579549177547519889e+01, /* 0xC031C209, 0x555F995A */ +rb3 = -1.60636384855821916062e+02, /* 0xC064145D, 0x43C5ED98 */ +rb4 = -6.37566443368389627722e+02, /* 0xC083EC88, 0x1375F228 */ +rb5 = -1.02509513161107724954e+03, /* 0xC0900461, 0x6A2E5992 */ +rb6 = -4.83519191608651397019e+02, /* 0xC07E384E, 0x9BDC383F */ +sb1 = 3.03380607434824582924e+01, /* 0x403E568B, 0x261D5190 */ +sb2 = 3.25792512996573918826e+02, /* 0x40745CAE, 0x221B9F0A */ +sb3 = 1.53672958608443695994e+03, /* 0x409802EB, 0x189D5118 */ +sb4 = 3.19985821950859553908e+03, /* 0x40A8FFB7, 0x688C246A */ +sb5 = 2.55305040643316442583e+03, /* 0x40A3F219, 0xCEDF3BE6 */ +sb6 = 4.74528541206955367215e+02, /* 0x407DA874, 0xE79FE763 */ +sb7 = -2.24409524465858183362e+01; /* 0xC03670E2, 0x42712D62 */ + +double +erf(double x) +{ + int32_t hx,ix,i; + double R,S,P,Q,s,y,z,r; + GET_HIGH_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x7ff00000) { /* erf(nan)=nan */ + i = ((uint32_t)hx>>31)<<1; + return (double)(1-i)+one/x; /* erf(+-inf)=+-1 */ + } + + if(ix < 0x3feb0000) { /* |x|<0.84375 */ + if(ix < 0x3e300000) { /* |x|<2**-28 */ + if (ix < 0x00800000) + return 0.125*(8.0*x+efx8*x); /*avoid underflow */ + return x + efx*x; + } + z = x*x; + r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4))); + s = one+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5)))); + y = r/s; + return x + x*y; + } + if(ix < 0x3ff40000) { /* 0.84375 <= |x| < 1.25 */ + s = fabs(x)-one; + P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); + Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); + if(hx>=0) return erx + P/Q; else return -erx - P/Q; + } + if (ix >= 0x40180000) { /* inf>|x|>=6 */ + if(hx>=0) return one-tiny; else return tiny-one; + } + x = fabs(x); + s = one/(x*x); + if(ix< 0x4006DB6E) { /* |x| < 1/0.35 */ + R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*( + ra5+s*(ra6+s*ra7)))))); + S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*( + sa5+s*(sa6+s*(sa7+s*sa8))))))); + } else { /* |x| >= 1/0.35 */ + R=rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*( + rb5+s*rb6))))); + S=one+s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*( + sb5+s*(sb6+s*sb7)))))); + } + z = x; + SET_LOW_WORD(z,0); + r = exp(-z*z-0.5625)*exp((z-x)*(z+x)+R/S); + if(hx>=0) return one-r/x; else return r/x-one; +} + +double +erfc(double x) +{ + int32_t hx,ix; + double R,S,P,Q,s,y,z,r; + GET_HIGH_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x7ff00000) { /* erfc(nan)=nan */ + /* erfc(+-inf)=0,2 */ + return (double)(((uint32_t)hx>>31)<<1)+one/x; + } + + if(ix < 0x3feb0000) { /* |x|<0.84375 */ + if(ix < 0x3c700000) /* |x|<2**-56 */ + return one-x; + z = x*x; + r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4))); + s = one+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5)))); + y = r/s; + if(hx < 0x3fd00000) { /* x<1/4 */ + return one-(x+x*y); + } else { + r = x*y; + r += (x-half); + return half - r ; + } + } + if(ix < 0x3ff40000) { /* 0.84375 <= |x| < 1.25 */ + s = fabs(x)-one; + P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); + Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); + if(hx>=0) { + z = one-erx; return z - P/Q; + } else { + z = erx+P/Q; return one+z; + } + } + if (ix < 0x403c0000) { /* |x|<28 */ + x = fabs(x); + s = one/(x*x); + if(ix< 0x4006DB6D) { /* |x| < 1/.35 ~ 2.857143*/ + R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*( + ra5+s*(ra6+s*ra7)))))); + S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*( + sa5+s*(sa6+s*(sa7+s*sa8))))))); + } else { /* |x| >= 1/.35 ~ 2.857143 */ + if(hx<0&&ix>=0x40180000) return two-tiny;/* x < -6 */ + R=rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*( + rb5+s*rb6))))); + S=one+s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*( + sb5+s*(sb6+s*sb7)))))); + } + z = x; + SET_LOW_WORD(z,0); + r = exp(-z*z-0.5625)* + exp((z-x)*(z+x)+R/S); + if(hx>0) return r/x; else return two-r/x; + } else { + if(hx>0) return tiny*tiny; else return two-tiny; + } +} diff --git a/05/musl-final/src/math/s_erff.c b/05/musl-final/src/math/s_erff.c new file mode 100644 index 0000000..28e2f7b --- /dev/null +++ b/05/musl-final/src/math/s_erff.c @@ -0,0 +1,207 @@ +/* s_erff.c -- float version of s_erf.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +tiny = 1e-30, +half= 5.0000000000e-01, /* 0x3F000000 */ +one = 1.0000000000e+00, /* 0x3F800000 */ +two = 2.0000000000e+00, /* 0x40000000 */ + /* c = (subfloat)0.84506291151 */ +erx = 8.4506291151e-01, /* 0x3f58560b */ +/* + * Coefficients for approximation to erf on [0,0.84375] + */ +efx = 1.2837916613e-01, /* 0x3e0375d4 */ +efx8= 1.0270333290e+00, /* 0x3f8375d4 */ +pp0 = 1.2837916613e-01, /* 0x3e0375d4 */ +pp1 = -3.2504209876e-01, /* 0xbea66beb */ +pp2 = -2.8481749818e-02, /* 0xbce9528f */ +pp3 = -5.7702702470e-03, /* 0xbbbd1489 */ +pp4 = -2.3763017452e-05, /* 0xb7c756b1 */ +qq1 = 3.9791721106e-01, /* 0x3ecbbbce */ +qq2 = 6.5022252500e-02, /* 0x3d852a63 */ +qq3 = 5.0813062117e-03, /* 0x3ba68116 */ +qq4 = 1.3249473704e-04, /* 0x390aee49 */ +qq5 = -3.9602282413e-06, /* 0xb684e21a */ +/* + * Coefficients for approximation to erf in [0.84375,1.25] + */ +pa0 = -2.3621185683e-03, /* 0xbb1acdc6 */ +pa1 = 4.1485610604e-01, /* 0x3ed46805 */ +pa2 = -3.7220788002e-01, /* 0xbebe9208 */ +pa3 = 3.1834661961e-01, /* 0x3ea2fe54 */ +pa4 = -1.1089469492e-01, /* 0xbde31cc2 */ +pa5 = 3.5478305072e-02, /* 0x3d1151b3 */ +pa6 = -2.1663755178e-03, /* 0xbb0df9c0 */ +qa1 = 1.0642088205e-01, /* 0x3dd9f331 */ +qa2 = 5.4039794207e-01, /* 0x3f0a5785 */ +qa3 = 7.1828655899e-02, /* 0x3d931ae7 */ +qa4 = 1.2617121637e-01, /* 0x3e013307 */ +qa5 = 1.3637083583e-02, /* 0x3c5f6e13 */ +qa6 = 1.1984500103e-02, /* 0x3c445aa3 */ +/* + * Coefficients for approximation to erfc in [1.25,1/0.35] + */ +ra0 = -9.8649440333e-03, /* 0xbc21a093 */ +ra1 = -6.9385856390e-01, /* 0xbf31a0b7 */ +ra2 = -1.0558626175e+01, /* 0xc128f022 */ +ra3 = -6.2375331879e+01, /* 0xc2798057 */ +ra4 = -1.6239666748e+02, /* 0xc322658c */ +ra5 = -1.8460508728e+02, /* 0xc3389ae7 */ +ra6 = -8.1287437439e+01, /* 0xc2a2932b */ +ra7 = -9.8143291473e+00, /* 0xc11d077e */ +sa1 = 1.9651271820e+01, /* 0x419d35ce */ +sa2 = 1.3765776062e+02, /* 0x4309a863 */ +sa3 = 4.3456588745e+02, /* 0x43d9486f */ +sa4 = 6.4538726807e+02, /* 0x442158c9 */ +sa5 = 4.2900814819e+02, /* 0x43d6810b */ +sa6 = 1.0863500214e+02, /* 0x42d9451f */ +sa7 = 6.5702495575e+00, /* 0x40d23f7c */ +sa8 = -6.0424413532e-02, /* 0xbd777f97 */ +/* + * Coefficients for approximation to erfc in [1/.35,28] + */ +rb0 = -9.8649431020e-03, /* 0xbc21a092 */ +rb1 = -7.9928326607e-01, /* 0xbf4c9dd4 */ +rb2 = -1.7757955551e+01, /* 0xc18e104b */ +rb3 = -1.6063638306e+02, /* 0xc320a2ea */ +rb4 = -6.3756646729e+02, /* 0xc41f6441 */ +rb5 = -1.0250950928e+03, /* 0xc480230b */ +rb6 = -4.8351919556e+02, /* 0xc3f1c275 */ +sb1 = 3.0338060379e+01, /* 0x41f2b459 */ +sb2 = 3.2579251099e+02, /* 0x43a2e571 */ +sb3 = 1.5367296143e+03, /* 0x44c01759 */ +sb4 = 3.1998581543e+03, /* 0x4547fdbb */ +sb5 = 2.5530502930e+03, /* 0x451f90ce */ +sb6 = 4.7452853394e+02, /* 0x43ed43a7 */ +sb7 = -2.2440952301e+01; /* 0xc1b38712 */ + +float +erff(float x) +{ + int32_t hx,ix,i; + float R,S,P,Q,s,y,z,r; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x7f800000) { /* erf(nan)=nan */ + i = ((uint32_t)hx>>31)<<1; + return (float)(1-i)+one/x; /* erf(+-inf)=+-1 */ + } + + if(ix < 0x3f580000) { /* |x|<0.84375 */ + if(ix < 0x31800000) { /* |x|<2**-28 */ + if (ix < 0x04000000) + /*avoid underflow */ + return (float)0.125*((float)8.0*x+efx8*x); + return x + efx*x; + } + z = x*x; + r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4))); + s = one+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5)))); + y = r/s; + return x + x*y; + } + if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ + s = fabsf(x)-one; + P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); + Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); + if(hx>=0) return erx + P/Q; else return -erx - P/Q; + } + if (ix >= 0x40c00000) { /* inf>|x|>=6 */ + if(hx>=0) return one-tiny; else return tiny-one; + } + x = fabsf(x); + s = one/(x*x); + if(ix< 0x4036DB6E) { /* |x| < 1/0.35 */ + R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*( + ra5+s*(ra6+s*ra7)))))); + S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*( + sa5+s*(sa6+s*(sa7+s*sa8))))))); + } else { /* |x| >= 1/0.35 */ + R=rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*( + rb5+s*rb6))))); + S=one+s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*( + sb5+s*(sb6+s*sb7)))))); + } + GET_FLOAT_WORD(ix,x); + SET_FLOAT_WORD(z,ix&0xfffff000); + r = expf(-z*z-(float)0.5625)*expf((z-x)*(z+x)+R/S); + if(hx>=0) return one-r/x; else return r/x-one; +} + +float +erfcf(float x) +{ + int32_t hx,ix; + float R,S,P,Q,s,y,z,r; + GET_FLOAT_WORD(hx,x); + ix = hx&0x7fffffff; + if(ix>=0x7f800000) { /* erfc(nan)=nan */ + /* erfc(+-inf)=0,2 */ + return (float)(((uint32_t)hx>>31)<<1)+one/x; + } + + if(ix < 0x3f580000) { /* |x|<0.84375 */ + if(ix < 0x23800000) /* |x|<2**-56 */ + return one-x; + z = x*x; + r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4))); + s = one+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5)))); + y = r/s; + if(hx < 0x3e800000) { /* x<1/4 */ + return one-(x+x*y); + } else { + r = x*y; + r += (x-half); + return half - r ; + } + } + if(ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ + s = fabsf(x)-one; + P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); + Q = one+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); + if(hx>=0) { + z = one-erx; return z - P/Q; + } else { + z = erx+P/Q; return one+z; + } + } + if (ix < 0x41e00000) { /* |x|<28 */ + x = fabsf(x); + s = one/(x*x); + if(ix< 0x4036DB6D) { /* |x| < 1/.35 ~ 2.857143*/ + R=ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*( + ra5+s*(ra6+s*ra7)))))); + S=one+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*( + sa5+s*(sa6+s*(sa7+s*sa8))))))); + } else { /* |x| >= 1/.35 ~ 2.857143 */ + if(hx<0&&ix>=0x40c00000) return two-tiny;/* x < -6 */ + R=rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*( + rb5+s*rb6))))); + S=one+s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*( + sb5+s*(sb6+s*sb7)))))); + } + GET_FLOAT_WORD(ix,x); + SET_FLOAT_WORD(z,ix&0xfffff000); + r = expf(-z*z-(float)0.5625)* + expf((z-x)*(z+x)+R/S); + if(hx>0) return r/x; else return two-r/x; + } else { + if(hx>0) return tiny*tiny; else return two-tiny; + } +} diff --git a/05/musl-final/src/math/s_expm1.c b/05/musl-final/src/math/s_expm1.c new file mode 100644 index 0000000..6f1f667 --- /dev/null +++ b/05/musl-final/src/math/s_expm1.c @@ -0,0 +1,217 @@ +/* @(#)s_expm1.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* expm1(x) + * Returns exp(x)-1, the exponential of x minus 1. + * + * Method + * 1. Argument reduction: + * Given x, find r and integer k such that + * + * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 + * + * Here a correction term c will be computed to compensate + * the error in r when rounded to a floating-point number. + * + * 2. Approximating expm1(r) by a special rational function on + * the interval [0,0.34658]: + * Since + * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... + * we define R1(r*r) by + * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) + * That is, + * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) + * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) + * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... + * We use a special Reme algorithm on [0,0.347] to generate + * a polynomial of degree 5 in r*r to approximate R1. The + * maximum error of this polynomial approximation is bounded + * by 2**-61. In other words, + * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 + * where Q1 = -1.6666666666666567384E-2, + * Q2 = 3.9682539681370365873E-4, + * Q3 = -9.9206344733435987357E-6, + * Q4 = 2.5051361420808517002E-7, + * Q5 = -6.2843505682382617102E-9; + * (where z=r*r, and the values of Q1 to Q5 are listed below) + * with error bounded by + * | 5 | -61 + * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 + * | | + * + * expm1(r) = exp(r)-1 is then computed by the following + * specific way which minimize the accumulation rounding error: + * 2 3 + * r r [ 3 - (R1 + R1*r/2) ] + * expm1(r) = r + --- + --- * [--------------------] + * 2 2 [ 6 - r*(3 - R1*r/2) ] + * + * To compensate the error in the argument reduction, we use + * expm1(r+c) = expm1(r) + c + expm1(r)*c + * ~ expm1(r) + c + r*c + * Thus c+r*c will be added in as the correction terms for + * expm1(r+c). Now rearrange the term to avoid optimization + * screw up: + * ( 2 2 ) + * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) + * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) + * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) + * ( ) + * + * = r - E + * 3. Scale back to obtain expm1(x): + * From step 1, we have + * expm1(x) = either 2^k*[expm1(r)+1] - 1 + * = or 2^k*[expm1(r) + (1-2^-k)] + * 4. Implementation notes: + * (A). To save one multiplication, we scale the coefficient Qi + * to Qi*2^i, and replace z by (x^2)/2. + * (B). To achieve maximum accuracy, we compute expm1(x) by + * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) + * (ii) if k=0, return r-E + * (iii) if k=-1, return 0.5*(r-E)-0.5 + * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) + * else return 1.0+2.0*(r-E); + * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) + * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else + * (vii) return 2^k(1-((E+2^-k)-r)) + * + * Special cases: + * expm1(INF) is INF, expm1(NaN) is NaN; + * expm1(-INF) is -1, and + * for finite argument, only expm1(0)=0 is exact. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Misc. info. + * For IEEE double + * if x > 7.09782712893383973096e+02 then expm1(x) overflow + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include +#include "math_private.h" + +static const double +one = 1.0, +huge = 1.0e+300, +tiny = 1.0e-300, +o_threshold = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */ +ln2_hi = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */ +ln2_lo = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */ +invln2 = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */ + /* scaled coefficients related to expm1 */ +Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */ +Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */ +Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */ +Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */ +Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */ + +double +expm1(double x) +{ + double y,hi,lo,c=0.0,t,e,hxs,hfx,r1; + int32_t k,xsb; + uint32_t hx; + + GET_HIGH_WORD(hx,x); + xsb = hx&0x80000000; /* sign bit of x */ + if(xsb==0) y=x; else y= -x; /* y = |x| */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out huge and non-finite argument */ + if(hx >= 0x4043687A) { /* if |x|>=56*ln2 */ + if(hx >= 0x40862E42) { /* if |x|>=709.78... */ + if(hx>=0x7ff00000) { + uint32_t low; + GET_LOW_WORD(low,x); + if(((hx&0xfffff)|low)!=0) + return x+x; /* NaN */ + else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */ + } + if(x > o_threshold) return huge*huge; /* overflow */ + } + if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */ + if(x+tiny<0.0) /* raise inexact */ + return tiny-one; /* return -1 */ + } + } + + /* argument reduction */ + if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ + if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ + if(xsb==0) + {hi = x - ln2_hi; lo = ln2_lo; k = 1;} + else + {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} + } else { + k = invln2*x+((xsb==0)?0.5:-0.5); + t = k; + hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ + lo = t*ln2_lo; + } + x = hi - lo; + c = (hi-x)-lo; + } + else if(hx < 0x3c900000) { /* when |x|<2**-54, return x */ + t = huge+x; /* return x with inexact flags when x!=0 */ + return x - (t-(huge+x)); + } + else k = 0; + + /* x is now in primary range */ + hfx = 0.5*x; + hxs = x*hfx; + r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); + t = 3.0-r1*hfx; + e = hxs*((r1-t)/(6.0 - x*t)); + if(k==0) return x - (x*e-hxs); /* c is 0 */ + else { + e = (x*(e-c)-c); + e -= hxs; + if(k== -1) return 0.5*(x-e)-0.5; + if(k==1) { + if(x < -0.25) return -2.0*(e-(x+0.5)); + else return one+2.0*(x-e); + } + if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ + uint32_t high; + y = one-(e-x); + GET_HIGH_WORD(high,y); + SET_HIGH_WORD(y,high+(k<<20)); /* add k to y's exponent */ + return y-one; + } + t = one; + if(k<20) { + uint32_t high; + SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k)); /* t=1-2^-k */ + y = t-(e-x); + GET_HIGH_WORD(high,y); + SET_HIGH_WORD(y,high+(k<<20)); /* add k to y's exponent */ + } else { + uint32_t high; + SET_HIGH_WORD(t,((0x3ff-k)<<20)); /* 2^-k */ + y = x-(e+t); + y += one; + GET_HIGH_WORD(high,y); + SET_HIGH_WORD(y,high+(k<<20)); /* add k to y's exponent */ + } + } + return y; +} diff --git a/05/musl-final/src/math/s_expm1f.c b/05/musl-final/src/math/s_expm1f.c new file mode 100644 index 0000000..b22cf0f --- /dev/null +++ b/05/musl-final/src/math/s_expm1f.c @@ -0,0 +1,122 @@ +/* s_expm1f.c -- float version of s_expm1.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +one = 1.0, +huge = 1.0e+30, +tiny = 1.0e-30, +o_threshold = 8.8721679688e+01,/* 0x42b17180 */ +ln2_hi = 6.9313812256e-01,/* 0x3f317180 */ +ln2_lo = 9.0580006145e-06,/* 0x3717f7d1 */ +invln2 = 1.4426950216e+00,/* 0x3fb8aa3b */ + /* scaled coefficients related to expm1 */ +Q1 = -3.3333335072e-02, /* 0xbd088889 */ +Q2 = 1.5873016091e-03, /* 0x3ad00d01 */ +Q3 = -7.9365076090e-05, /* 0xb8a670cd */ +Q4 = 4.0082177293e-06, /* 0x36867e54 */ +Q5 = -2.0109921195e-07; /* 0xb457edbb */ + +float +expm1f(float x) +{ + float y,hi,lo,c=0.0,t,e,hxs,hfx,r1; + int32_t k,xsb; + uint32_t hx; + + GET_FLOAT_WORD(hx,x); + xsb = hx&0x80000000; /* sign bit of x */ + if(xsb==0) y=x; else y= -x; /* y = |x| */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out huge and non-finite argument */ + if(hx >= 0x4195b844) { /* if |x|>=27*ln2 */ + if(hx >= 0x42b17218) { /* if |x|>=88.721... */ + if(hx>0x7f800000) + return x+x; /* NaN */ + if(hx==0x7f800000) + return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */ + if(x > o_threshold) return huge*huge; /* overflow */ + } + if(xsb!=0) { /* x < -27*ln2, return -1.0 with inexact */ + if(x+tiny<(float)0.0) /* raise inexact */ + return tiny-one; /* return -1 */ + } + } + + /* argument reduction */ + if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ + if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ + if(xsb==0) + {hi = x - ln2_hi; lo = ln2_lo; k = 1;} + else + {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} + } else { + k = invln2*x+((xsb==0)?(float)0.5:(float)-0.5); + t = k; + hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ + lo = t*ln2_lo; + } + x = hi - lo; + c = (hi-x)-lo; + } + else if(hx < 0x33000000) { /* when |x|<2**-25, return x */ + t = huge+x; /* return x with inexact flags when x!=0 */ + return x - (t-(huge+x)); + } + else k = 0; + + /* x is now in primary range */ + hfx = (float)0.5*x; + hxs = x*hfx; + r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); + t = (float)3.0-r1*hfx; + e = hxs*((r1-t)/((float)6.0 - x*t)); + if(k==0) return x - (x*e-hxs); /* c is 0 */ + else { + e = (x*(e-c)-c); + e -= hxs; + if(k== -1) return (float)0.5*(x-e)-(float)0.5; + if(k==1) { + if(x < (float)-0.25) return -(float)2.0*(e-(x+(float)0.5)); + else return one+(float)2.0*(x-e); + } + if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ + int32_t i; + y = one-(e-x); + GET_FLOAT_WORD(i,y); + SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */ + return y-one; + } + t = one; + if(k<23) { + int32_t i; + SET_FLOAT_WORD(t,0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */ + y = t-(e-x); + GET_FLOAT_WORD(i,y); + SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */ + } else { + int32_t i; + SET_FLOAT_WORD(t,((0x7f-k)<<23)); /* 2^-k */ + y = x-(e+t); + y += one; + GET_FLOAT_WORD(i,y); + SET_FLOAT_WORD(y,i+(k<<23)); /* add k to y's exponent */ + } + } + return y; +} diff --git a/05/musl-final/src/math/s_fabs.c b/05/musl-final/src/math/s_fabs.c new file mode 100644 index 0000000..7443325 --- /dev/null +++ b/05/musl-final/src/math/s_fabs.c @@ -0,0 +1,27 @@ +/* @(#)s_fabs.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fabs(x) returns the absolute value of x. + */ + +#include +#include "math_private.h" + +double +fabs(double x) +{ + uint32_t high; + GET_HIGH_WORD(high,x); + SET_HIGH_WORD(x,high&0x7fffffff); + return x; +} diff --git a/05/musl-final/src/math/s_fabsf.c b/05/musl-final/src/math/s_fabsf.c new file mode 100644 index 0000000..655d57d --- /dev/null +++ b/05/musl-final/src/math/s_fabsf.c @@ -0,0 +1,30 @@ +/* s_fabsf.c -- float version of s_fabs.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fabsf(x) returns the absolute value of x. + */ + +#include +#include "math_private.h" + +float +fabsf(float x) +{ + uint32_t ix; + GET_FLOAT_WORD(ix,x); + SET_FLOAT_WORD(x,ix&0x7fffffff); + return x; +} diff --git a/05/musl-final/src/math/s_floor.c b/05/musl-final/src/math/s_floor.c new file mode 100644 index 0000000..273cf6f --- /dev/null +++ b/05/musl-final/src/math/s_floor.c @@ -0,0 +1,69 @@ +/* @(#)s_floor.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * floor(x) + * Return x rounded toward -inf to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to floor(x). + */ + +#include +#include "math_private.h" + +static const double huge = 1.0e300; + +double +floor(double x) +{ + int32_t i0,i1,j0; + uint32_t i,j; + EXTRACT_WORDS(i0,i1,x); + j0 = ((i0>>20)&0x7ff)-0x3ff; + if(j0<20) { + if(j0<0) { /* raise inexact if x != 0 */ + if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ + if(i0>=0) {i0=i1=0;} + else if(((i0&0x7fffffff)|i1)!=0) + { i0=0xbff00000;i1=0;} + } + } else { + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) return x; /* x is integral */ + if(huge+x>0.0) { /* raise inexact flag */ + if(i0<0) i0 += (0x00100000)>>j0; + i0 &= (~i); i1=0; + } + } + } else if (j0>51) { + if(j0==0x400) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } else { + i = ((uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) return x; /* x is integral */ + if(huge+x>0.0) { /* raise inexact flag */ + if(i0<0) { + if(j0==20) i0+=1; + else { + j = i1+(1<<(52-j0)); + if(j +#include "math_private.h" + +static const float huge = 1.0e30; + +float +floorf(float x) +{ + int32_t i0,j0; + uint32_t i; + GET_FLOAT_WORD(i0,x); + j0 = ((i0>>23)&0xff)-0x7f; + if(j0<23) { + if(j0<0) { /* raise inexact if x != 0 */ + if(huge+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */ + if(i0>=0) {i0=0;} + else if((i0&0x7fffffff)!=0) + { i0=0xbf800000;} + } + } else { + i = (0x007fffff)>>j0; + if((i0&i)==0) return x; /* x is integral */ + if(huge+x>(float)0.0) { /* raise inexact flag */ + if(i0<0) i0 += (0x00800000)>>j0; + i0 &= (~i); + } + } + } else { + if(j0==0x80) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } + SET_FLOAT_WORD(x,i0); + return x; +} diff --git a/05/musl-final/src/math/s_ilogb.c b/05/musl-final/src/math/s_ilogb.c new file mode 100644 index 0000000..f1ac498 --- /dev/null +++ b/05/musl-final/src/math/s_ilogb.c @@ -0,0 +1,45 @@ +/* @(#)s_ilogb.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* ilogb(double x) + * return the binary exponent of non-zero x + * ilogb(0) = FP_ILOGB0 + * ilogb(NaN) = FP_ILOGBNAN (no signal is raised) + * ilogb(inf) = INT_MAX (no signal is raised) + */ + +#include + +#include +#include "math_private.h" + +int ilogb(double x) +{ + int32_t hx,lx,ix; + + EXTRACT_WORDS(hx,lx,x); + hx &= 0x7fffffff; + if(hx<0x00100000) { + if((hx|lx)==0) + return FP_ILOGB0; + else /* subnormal x */ + if(hx==0) { + for (ix = -1043; lx>0; lx<<=1) ix -=1; + } else { + for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1; + } + return ix; + } + else if (hx<0x7ff00000) return (hx>>20)-1023; + else if (hx>0x7ff00000 || lx!=0) return FP_ILOGBNAN; + else return INT_MAX; +} diff --git a/05/musl-final/src/math/s_ilogbf.c b/05/musl-final/src/math/s_ilogbf.c new file mode 100644 index 0000000..30359fe --- /dev/null +++ b/05/musl-final/src/math/s_ilogbf.c @@ -0,0 +1,37 @@ +/* s_ilogbf.c -- float version of s_ilogb.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include + +#include +#include "math_private.h" + +int ilogbf(float x) +{ + int32_t hx,ix; + + GET_FLOAT_WORD(hx,x); + hx &= 0x7fffffff; + if(hx<0x00800000) { + if(hx==0) + return FP_ILOGB0; + else /* subnormal x */ + for (ix = -126,hx<<=8; hx>0; hx<<=1) ix -=1; + return ix; + } + else if (hx<0x7f800000) return (hx>>23)-127; + else if (hx>0x7f800000) return FP_ILOGBNAN; + else return INT_MAX; +} diff --git a/05/musl-final/src/math/s_ldexp.c b/05/musl-final/src/math/s_ldexp.c new file mode 100644 index 0000000..f4d1cd6 --- /dev/null +++ b/05/musl-final/src/math/s_ldexp.c @@ -0,0 +1,6 @@ +#include + +double ldexp(double x, int n) +{ + return scalbn(x, n); +} diff --git a/05/musl-final/src/math/s_ldexpf.c b/05/musl-final/src/math/s_ldexpf.c new file mode 100644 index 0000000..3bad5f3 --- /dev/null +++ b/05/musl-final/src/math/s_ldexpf.c @@ -0,0 +1,6 @@ +#include + +float ldexpf(float x, int n) +{ + return scalbnf(x, n); +} diff --git a/05/musl-final/src/math/s_llrint.c b/05/musl-final/src/math/s_llrint.c new file mode 100644 index 0000000..2b1e00d --- /dev/null +++ b/05/musl-final/src/math/s_llrint.c @@ -0,0 +1,8 @@ +#include + +// FIXME: incorrect exception behavior + +long long llrint(double x) +{ + return rint(x); +} diff --git a/05/musl-final/src/math/s_log1p.c b/05/musl-final/src/math/s_log1p.c new file mode 100644 index 0000000..886d5ab --- /dev/null +++ b/05/musl-final/src/math/s_log1p.c @@ -0,0 +1,157 @@ +/* @(#)s_log1p.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* double log1p(double x) + * + * Method : + * 1. Argument Reduction: find k and f such that + * 1+x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * Note. If k=0, then f=x is exact. However, if k!=0, then f + * may not be representable exactly. In that case, a correction + * term is need. Let u=1+x rounded. Let c = (1+x)-u, then + * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), + * and add back the correction term c/u. + * (Note: when x > 2**53, one can simply return log(x)) + * + * 2. Approximation of log1p(f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Reme algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s + * (the values of Lp1 to Lp7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lp1*s +...+Lp7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log1p(f) = f - (hfsq - s*(hfsq+R)). + * + * 3. Finally, log1p(x) = k*ln2 + log1p(f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log1p(x) is NaN with signal if x < -1 (including -INF) ; + * log1p(+INF) is +INF; log1p(-1) is -INF with signal; + * log1p(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + * + * Note: Assuming log() return accurate answer, the following + * algorithm can be used to compute log1p(x) to within a few ULP: + * + * u = 1+x; + * if(u==1.0) return x ; else + * return log(u)*(x/(u-1.0)); + * + * See HP-15C Advanced Functions Handbook, p.193. + */ + +#include +#include "math_private.h" + +static const double +ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ +ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ +two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ +Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ +Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ +Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ +Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ +Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ +Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ +Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ + +static const double zero = 0.0; + +double +log1p(double x) +{ + double hfsq,f=0,c=0,s,z,R,u; + int32_t k,hx,hu=0,ax; + + GET_HIGH_WORD(hx,x); + ax = hx&0x7fffffff; + + k = 1; + if (hx < 0x3FDA827A) { /* x < 0.41422 */ + if(ax>=0x3ff00000) { /* x <= -1.0 */ + if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */ + else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ + } + if(ax<0x3e200000) { /* |x| < 2**-29 */ + if(two54+x>zero /* raise inexact */ + &&ax<0x3c900000) /* |x| < 2**-54 */ + return x; + else + return x - x*x*0.5; + } + if(hx>0||hx<=((int32_t)0xbfd2bec3)) { + k=0;f=x;hu=1;} /* -0.2929= 0x7ff00000) return x+x; + if(k!=0) { + if(hx<0x43400000) { + u = 1.0+x; + GET_HIGH_WORD(hu,u); + k = (hu>>20)-1023; + c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */ + c /= u; + } else { + u = x; + GET_HIGH_WORD(hu,u); + k = (hu>>20)-1023; + c = 0; + } + hu &= 0x000fffff; + if(hu<0x6a09e) { + SET_HIGH_WORD(u,hu|0x3ff00000); /* normalize u */ + } else { + k += 1; + SET_HIGH_WORD(u,hu|0x3fe00000); /* normalize u/2 */ + hu = (0x00100000-hu)>>2; + } + f = u-1.0; + } + hfsq=0.5*f*f; + if(hu==0) { /* |f| < 2**-20 */ + if(f==zero) { if(k==0) return zero; + else {c += k*ln2_lo; return k*ln2_hi+c;} } + R = hfsq*(1.0-0.66666666666666666*f); + if(k==0) return f-R; else + return k*ln2_hi-((R-(k*ln2_lo+c))-f); + } + s = f/(2.0+f); + z = s*s; + R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); + if(k==0) return f-(hfsq-s*(hfsq+R)); else + return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); +} diff --git a/05/musl-final/src/math/s_log1pf.c b/05/musl-final/src/math/s_log1pf.c new file mode 100644 index 0000000..dcdd6bb --- /dev/null +++ b/05/musl-final/src/math/s_log1pf.c @@ -0,0 +1,96 @@ +/* s_log1pf.c -- float version of s_log1p.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ +ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ +two25 = 3.355443200e+07, /* 0x4c000000 */ +Lp1 = 6.6666668653e-01, /* 3F2AAAAB */ +Lp2 = 4.0000000596e-01, /* 3ECCCCCD */ +Lp3 = 2.8571429849e-01, /* 3E924925 */ +Lp4 = 2.2222198546e-01, /* 3E638E29 */ +Lp5 = 1.8183572590e-01, /* 3E3A3325 */ +Lp6 = 1.5313838422e-01, /* 3E1CD04F */ +Lp7 = 1.4798198640e-01; /* 3E178897 */ + +static const float zero = 0.0; + +float +log1pf(float x) +{ + float hfsq,f=0,c=0,s,z,R,u; + int32_t k,hx,hu=0,ax; + + GET_FLOAT_WORD(hx,x); + ax = hx&0x7fffffff; + + k = 1; + if (hx < 0x3ed413d7) { /* x < 0.41422 */ + if(ax>=0x3f800000) { /* x <= -1.0 */ + if(x==(float)-1.0) return -two25/zero; /* log1p(-1)=+inf */ + else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ + } + if(ax<0x31000000) { /* |x| < 2**-29 */ + if(two25+x>zero /* raise inexact */ + &&ax<0x24800000) /* |x| < 2**-54 */ + return x; + else + return x - x*x*(float)0.5; + } + if(hx>0||hx<=((int32_t)0xbe95f61f)) { + k=0;f=x;hu=1;} /* -0.2929= 0x7f800000) return x+x; + if(k!=0) { + if(hx<0x5a000000) { + u = (float)1.0+x; + GET_FLOAT_WORD(hu,u); + k = (hu>>23)-127; + /* correction term */ + c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0); + c /= u; + } else { + u = x; + GET_FLOAT_WORD(hu,u); + k = (hu>>23)-127; + c = 0; + } + hu &= 0x007fffff; + if(hu<0x3504f7) { + SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */ + } else { + k += 1; + SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */ + hu = (0x00800000-hu)>>2; + } + f = u-(float)1.0; + } + hfsq=(float)0.5*f*f; + if(hu==0) { /* |f| < 2**-20 */ + if(f==zero) { if(k==0) return zero; + else {c += k*ln2_lo; return k*ln2_hi+c;} } + R = hfsq*((float)1.0-(float)0.66666666666666666*f); + if(k==0) return f-R; else + return k*ln2_hi-((R-(k*ln2_lo+c))-f); + } + s = f/((float)2.0+f); + z = s*s; + R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); + if(k==0) return f-(hfsq-s*(hfsq+R)); else + return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); +} diff --git a/05/musl-final/src/math/s_logb.c b/05/musl-final/src/math/s_logb.c new file mode 100644 index 0000000..be399c7 --- /dev/null +++ b/05/musl-final/src/math/s_logb.c @@ -0,0 +1,34 @@ +/* @(#)s_logb.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * double logb(x) + * IEEE 754 logb. Included to pass IEEE test suite. Not recommend. + * Use ilogb instead. + */ + +#include +#include "math_private.h" + +double +logb(double x) +{ + int32_t lx,ix; + EXTRACT_WORDS(ix,lx,x); + ix &= 0x7fffffff; /* high |x| */ + if((ix|lx)==0) return -1.0/fabs(x); + if(ix>=0x7ff00000) return x*x; + if((ix>>=20)==0) /* IEEE 754 logb */ + return -1022.0; + else + return (double) (ix-1023); +} diff --git a/05/musl-final/src/math/s_logbf.c b/05/musl-final/src/math/s_logbf.c new file mode 100644 index 0000000..747664d --- /dev/null +++ b/05/musl-final/src/math/s_logbf.c @@ -0,0 +1,31 @@ +/* s_logbf.c -- float version of s_logb.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +float +logbf(float x) +{ + int32_t ix; + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; /* high |x| */ + if(ix==0) return (float)-1.0/fabsf(x); + if(ix>=0x7f800000) return x*x; + if((ix>>=23)==0) /* IEEE 754 logb */ + return -126.0; + else + return (float) (ix-127); +} diff --git a/05/musl-final/src/math/s_lrint.c b/05/musl-final/src/math/s_lrint.c new file mode 100644 index 0000000..da8e198 --- /dev/null +++ b/05/musl-final/src/math/s_lrint.c @@ -0,0 +1,8 @@ +#include + +// FIXME: incorrect exception behavior + +long lrint(double x) +{ + return rint(x); +} diff --git a/05/musl-final/src/math/s_lrintf.c b/05/musl-final/src/math/s_lrintf.c new file mode 100644 index 0000000..d0b469b --- /dev/null +++ b/05/musl-final/src/math/s_lrintf.c @@ -0,0 +1,8 @@ +#include + +// FIXME: incorrect exception behavior + +long lrintf(float x) +{ + return rintf(x); +} diff --git a/05/musl-final/src/math/s_modf.c b/05/musl-final/src/math/s_modf.c new file mode 100644 index 0000000..a5528d6 --- /dev/null +++ b/05/musl-final/src/math/s_modf.c @@ -0,0 +1,71 @@ +/* @(#)s_modf.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * modf(double x, double *iptr) + * return fraction part of x, and return x's integral part in *iptr. + * Method: + * Bit twiddling. + * + * Exception: + * No exception. + */ + +#include +#include "math_private.h" + +static const double one = 1.0; + +double +modf(double x, double *iptr) +{ + int32_t i0,i1,j0; + uint32_t i; + EXTRACT_WORDS(i0,i1,x); + j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */ + if(j0<20) { /* integer part in high x */ + if(j0<0) { /* |x|<1 */ + INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */ + return x; + } else { + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) { /* x is integral */ + uint32_t high; + *iptr = x; + GET_HIGH_WORD(high,x); + INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */ + return x; + } else { + INSERT_WORDS(*iptr,i0&(~i),0); + return x - *iptr; + } + } + } else if (j0>51) { /* no fraction part */ + uint32_t high; + *iptr = x*one; + GET_HIGH_WORD(high,x); + INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */ + return x; + } else { /* fraction part in low x */ + i = ((uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) { /* x is integral */ + uint32_t high; + *iptr = x; + GET_HIGH_WORD(high,x); + INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */ + return x; + } else { + INSERT_WORDS(*iptr,i0,i1&(~i)); + return x - *iptr; + } + } +} diff --git a/05/musl-final/src/math/s_modff.c b/05/musl-final/src/math/s_modff.c new file mode 100644 index 0000000..de4dfd2 --- /dev/null +++ b/05/musl-final/src/math/s_modff.c @@ -0,0 +1,52 @@ +/* s_modff.c -- float version of s_modf.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one = 1.0; + +float +modff(float x, float *iptr) +{ + int32_t i0,j0; + uint32_t i; + GET_FLOAT_WORD(i0,x); + j0 = ((i0>>23)&0xff)-0x7f; /* exponent of x */ + if(j0<23) { /* integer part in x */ + if(j0<0) { /* |x|<1 */ + SET_FLOAT_WORD(*iptr,i0&0x80000000); /* *iptr = +-0 */ + return x; + } else { + i = (0x007fffff)>>j0; + if((i0&i)==0) { /* x is integral */ + uint32_t ix; + *iptr = x; + GET_FLOAT_WORD(ix,x); + SET_FLOAT_WORD(x,ix&0x80000000); /* return +-0 */ + return x; + } else { + SET_FLOAT_WORD(*iptr,i0&(~i)); + return x - *iptr; + } + } + } else { /* no fraction part */ + uint32_t ix; + *iptr = x*one; + GET_FLOAT_WORD(ix,x); + SET_FLOAT_WORD(x,ix&0x80000000); /* return +-0 */ + return x; + } +} diff --git a/05/musl-final/src/math/s_nextafter.c b/05/musl-final/src/math/s_nextafter.c new file mode 100644 index 0000000..46d298e --- /dev/null +++ b/05/musl-final/src/math/s_nextafter.c @@ -0,0 +1,72 @@ +/* @(#)s_nextafter.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* IEEE functions + * nextafter(x,y) + * return the next machine floating-point number of x in the + * direction toward y. + * Special cases: + */ + +#include +#include "math_private.h" + +double +nextafter(double x, double y) +{ + volatile double t; + int32_t hx,hy,ix,iy; + uint32_t lx,ly; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hy,ly,y); + ix = hx&0x7fffffff; /* |x| */ + iy = hy&0x7fffffff; /* |y| */ + + if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */ + ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ + return x+y; + if(x==y) return y; /* x=y, return y */ + if((ix|lx)==0) { /* x == 0 */ + INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */ + t = x*x; + if(t==x) return t; else return x; /* raise underflow flag */ + } + if(hx>=0) { /* x > 0 */ + if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */ + if(lx==0) hx -= 1; + lx -= 1; + } else { /* x < y, x += ulp */ + lx += 1; + if(lx==0) hx += 1; + } + } else { /* x < 0 */ + if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */ + if(lx==0) hx -= 1; + lx -= 1; + } else { /* x > y, x += ulp */ + lx += 1; + if(lx==0) hx += 1; + } + } + hy = hx&0x7ff00000; + if(hy>=0x7ff00000) return x+x; /* overflow */ + if(hy<0x00100000) { /* underflow */ + t = x*x; + if(t!=x) { /* raise underflow flag */ + INSERT_WORDS(y,hx,lx); + return y; + } + } + INSERT_WORDS(x,hx,lx); + return x; +} diff --git a/05/musl-final/src/math/s_nextafterf.c b/05/musl-final/src/math/s_nextafterf.c new file mode 100644 index 0000000..7ce0883 --- /dev/null +++ b/05/musl-final/src/math/s_nextafterf.c @@ -0,0 +1,63 @@ +/* s_nextafterf.c -- float version of s_nextafter.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +float +nextafterf(float x, float y) +{ + volatile float t; + int32_t hx,hy,ix,iy; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hy,y); + ix = hx&0x7fffffff; /* |x| */ + iy = hy&0x7fffffff; /* |y| */ + + if((ix>0x7f800000) || /* x is nan */ + (iy>0x7f800000)) /* y is nan */ + return x+y; + if(x==y) return y; /* x=y, return y */ + if(ix==0) { /* x == 0 */ + SET_FLOAT_WORD(x,(hy&0x80000000)|1);/* return +-minsubnormal */ + t = x*x; + if(t==x) return t; else return x; /* raise underflow flag */ + } + if(hx>=0) { /* x > 0 */ + if(hx>hy) { /* x > y, x -= ulp */ + hx -= 1; + } else { /* x < y, x += ulp */ + hx += 1; + } + } else { /* x < 0 */ + if(hy>=0||hx>hy){ /* x < y, x -= ulp */ + hx -= 1; + } else { /* x > y, x += ulp */ + hx += 1; + } + } + hy = hx&0x7f800000; + if(hy>=0x7f800000) return x+x; /* overflow */ + if(hy<0x00800000) { /* underflow */ + t = x*x; + if(t!=x) { /* raise underflow flag */ + SET_FLOAT_WORD(y,hx); + return y; + } + } + SET_FLOAT_WORD(x,hx); + return x; +} diff --git a/05/musl-final/src/math/s_remquo.c b/05/musl-final/src/math/s_remquo.c new file mode 100644 index 0000000..1a2992d --- /dev/null +++ b/05/musl-final/src/math/s_remquo.c @@ -0,0 +1,149 @@ +/* @(#)e_fmod.c 1.3 95/01/18 */ +/*- + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const double Zero[] = {0.0, -0.0,}; + +/* + * Return the IEEE remainder and set *quo to the last n bits of the + * quotient, rounded to the nearest integer. We choose n=31 because + * we wind up computing all the integer bits of the quotient anyway as + * a side-effect of computing the remainder by the shift and subtract + * method. In practice, this is far more bits than are needed to use + * remquo in reduction algorithms. + */ +double +remquo(double x, double y, int *quo) +{ + int32_t n,hx,hy,hz,ix,iy,sx,i; + uint32_t lx,ly,lz,q,sxy; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hy,ly,y); + sxy = (hx ^ hy) & 0x80000000; + sx = hx&0x80000000; /* sign of x */ + hx ^=sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ + ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ + return (x*y)/(x*y); + if(hx<=hy) { + if((hx>31]; /* |x|=|y| return x*0*/ + } + } + + /* determine ix = ilogb(x) */ + if(hx<0x00100000) { /* subnormal x */ + if(hx==0) { + for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; + } else { + for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; + } + } else ix = (hx>>20)-1023; + + /* determine iy = ilogb(y) */ + if(hy<0x00100000) { /* subnormal y */ + if(hy==0) { + for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; + } else { + for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; + } + } else iy = (hy>>20)-1023; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if(ix >= -1022) + hx = 0x00100000|(0x000fffff&hx); + else { /* subnormal x, shift x to normal */ + n = -1022-ix; + if(n<=31) { + hx = (hx<>(32-n)); + lx <<= n; + } else { + hx = lx<<(n-32); + lx = 0; + } + } + if(iy >= -1022) + hy = 0x00100000|(0x000fffff&hy); + else { /* subnormal y, shift y to normal */ + n = -1022-iy; + if(n<=31) { + hy = (hy<>(32-n)); + ly <<= n; + } else { + hy = ly<<(n-32); + ly = 0; + } + } + + /* fix point fmod */ + n = ix - iy; + q = 0; + while(n--) { + hz=hx-hy;lz=lx-ly; if(lx>31); lx = lx+lx;} + else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;} + q <<= 1; + } + hz=hx-hy;lz=lx-ly; if(lx=0) {hx=hz;lx=lz;q++;} + + /* convert back to floating value and restore the sign */ + if((hx|lx)==0) { /* return sign(x)*0 */ + *quo = (sxy ? -q : q); + return Zero[(uint32_t)sx>>31]; + } + while(hx<0x00100000) { /* normalize x */ + hx = hx+hx+(lx>>31); lx = lx+lx; + iy -= 1; + } + if(iy>= -1022) { /* normalize output */ + hx = ((hx-0x00100000)|((iy+1023)<<20)); + } else { /* subnormal output */ + n = -1022 - iy; + if(n<=20) { + lx = (lx>>n)|((uint32_t)hx<<(32-n)); + hx >>= n; + } else if (n<=31) { + lx = (hx<<(32-n))|(lx>>n); hx = sx; + } else { + lx = hx>>(n-32); hx = sx; + } + } +fixup: + INSERT_WORDS(x,hx,lx); + y = fabs(y); + if (y < 0x1p-1021) { + if (x+x>y || (x+x==y && (q & 1))) { + q++; + x-=y; + } + } else if (x>0.5*y || (x==0.5*y && (q & 1))) { + q++; + x-=y; + } + GET_HIGH_WORD(hx,x); + SET_HIGH_WORD(x,hx^sx); + q &= 0x7fffffff; + *quo = (sxy ? -q : q); + return x; +} diff --git a/05/musl-final/src/math/s_remquof.c b/05/musl-final/src/math/s_remquof.c new file mode 100644 index 0000000..be2a561 --- /dev/null +++ b/05/musl-final/src/math/s_remquof.c @@ -0,0 +1,118 @@ +/* @(#)e_fmod.c 1.3 95/01/18 */ +/*- + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float Zero[] = {0.0, -0.0,}; + +/* + * Return the IEEE remainder and set *quo to the last n bits of the + * quotient, rounded to the nearest integer. We choose n=31 because + * we wind up computing all the integer bits of the quotient anyway as + * a side-effect of computing the remainder by the shift and subtract + * method. In practice, this is far more bits than are needed to use + * remquo in reduction algorithms. + */ +float +remquof(float x, float y, int *quo) +{ + int32_t n,hx,hy,hz,ix,iy,sx,i; + uint32_t q,sxy; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hy,y); + sxy = (hx ^ hy) & 0x80000000; + sx = hx&0x80000000; /* sign of x */ + hx ^=sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */ + return (x*y)/(x*y); + if(hx>31]; /* |x|=|y| return x*0*/ + } + + /* determine ix = ilogb(x) */ + if(hx<0x00800000) { /* subnormal x */ + for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1; + } else ix = (hx>>23)-127; + + /* determine iy = ilogb(y) */ + if(hy<0x00800000) { /* subnormal y */ + for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1; + } else iy = (hy>>23)-127; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if(ix >= -126) + hx = 0x00800000|(0x007fffff&hx); + else { /* subnormal x, shift x to normal */ + n = -126-ix; + hx <<= n; + } + if(iy >= -126) + hy = 0x00800000|(0x007fffff&hy); + else { /* subnormal y, shift y to normal */ + n = -126-iy; + hy <<= n; + } + + /* fix point fmod */ + n = ix - iy; + q = 0; + while(n--) { + hz=hx-hy; + if(hz<0) hx = hx << 1; + else {hx = hz << 1; q++;} + q <<= 1; + } + hz=hx-hy; + if(hz>=0) {hx=hz;q++;} + + /* convert back to floating value and restore the sign */ + if(hx==0) { /* return sign(x)*0 */ + *quo = (sxy ? -q : q); + return Zero[(uint32_t)sx>>31]; + } + while(hx<0x00800000) { /* normalize x */ + hx <<= 1; + iy -= 1; + } + if(iy>= -126) { /* normalize output */ + hx = ((hx-0x00800000)|((iy+127)<<23)); + } else { /* subnormal output */ + n = -126 - iy; + hx >>= n; + } +fixup: + SET_FLOAT_WORD(x,hx); + y = fabsf(y); + if (y < 0x1p-125f) { + if (x+x>y || (x+x==y && (q & 1))) { + q++; + x-=y; + } + } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) { + q++; + x-=y; + } + GET_FLOAT_WORD(hx,x); + SET_FLOAT_WORD(x,hx^sx); + q &= 0x7fffffff; + *quo = (sxy ? -q : q); + return x; +} diff --git a/05/musl-final/src/math/s_rint.c b/05/musl-final/src/math/s_rint.c new file mode 100644 index 0000000..c80e01e --- /dev/null +++ b/05/musl-final/src/math/s_rint.c @@ -0,0 +1,80 @@ +/* @(#)s_rint.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * rint(x) + * Return x rounded to integral value according to the prevailing + * rounding mode. + * Method: + * Using floating addition. + * Exception: + * Inexact flag raised if x not equal to rint(x). + */ + +#include +#include "math_private.h" + +/* + * TWO23 is long double instead of double to avoid a bug in gcc. Without + * this, gcc thinks that TWO23[sx]+x and w-TWO23[sx] already have double + * precision and doesn't clip them to double precision when they are + * assigned and returned. + */ +static const /*long*/ double +TWO52[2]={ + 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ + -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ +}; + +double +rint(double x) +{ + int32_t i0,j0,sx; + uint32_t i,i1; + double w,t; + EXTRACT_WORDS(i0,i1,x); + sx = (i0>>31)&1; + j0 = ((i0>>20)&0x7ff)-0x3ff; + if(j0<20) { + if(j0<0) { + if(((i0&0x7fffffff)|i1)==0) return x; + i1 |= (i0&0x0fffff); + i0 &= 0xfffe0000; + i0 |= ((i1|-i1)>>12)&0x80000; + SET_HIGH_WORD(x,i0); + w = TWO52[sx]+x; + t = w-TWO52[sx]; + GET_HIGH_WORD(i0,t); + SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31)); + return t; + } else { + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) return x; /* x is integral */ + i>>=1; + if(((i0&i)|i1)!=0) { + if(j0==19) i1 = 0x40000000; else + i0 = (i0&(~i))|((0x20000)>>j0); + } + } + } else if (j0>51) { + if(j0==0x400) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } else { + i = ((uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) return x; /* x is integral */ + i>>=1; + if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20)); + } + INSERT_WORDS(x,i0,i1); + w = TWO52[sx]+x; + return w-TWO52[sx]; +} diff --git a/05/musl-final/src/math/s_rintf.c b/05/musl-final/src/math/s_rintf.c new file mode 100644 index 0000000..c441870 --- /dev/null +++ b/05/musl-final/src/math/s_rintf.c @@ -0,0 +1,45 @@ +/* s_rintf.c -- float version of s_rint.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +TWO23[2]={ + 8.3886080000e+06, /* 0x4b000000 */ + -8.3886080000e+06, /* 0xcb000000 */ +}; + +float +rintf(float x) +{ + int32_t i0,j0,sx; + volatile float w,t; /* volatile works around gcc bug */ + GET_FLOAT_WORD(i0,x); + sx = (i0>>31)&1; + j0 = ((i0>>23)&0xff)-0x7f; + if(j0<23) { + if(j0<0) { + if((i0&0x7fffffff)==0) return x; + w = TWO23[sx]+x; + t = w-TWO23[sx]; + return t; + } + w = TWO23[sx]+x; + return w-TWO23[sx]; + } + if(j0==0x80) return x+x; /* inf or NaN */ + else return x; /* x is integral */ +} diff --git a/05/musl-final/src/math/s_round.c b/05/musl-final/src/math/s_round.c new file mode 100644 index 0000000..d5bea7a --- /dev/null +++ b/05/musl-final/src/math/s_round.c @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2003, Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +double +round(double x) +{ + double t; + + if (!isfinite(x)) + return (x); + + if (x >= 0.0) { + t = ceil(x); + if (t - x > 0.5) + t -= 1.0; + return (t); + } else { + t = ceil(-x); + if (t + x > 0.5) + t -= 1.0; + return (-t); + } +} diff --git a/05/musl-final/src/math/s_roundf.c b/05/musl-final/src/math/s_roundf.c new file mode 100644 index 0000000..c4fc3e1 --- /dev/null +++ b/05/musl-final/src/math/s_roundf.c @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2003, Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +float +roundf(float x) +{ + float t; + + if (!isfinite(x)) + return (x); + + if (x >= 0.0) { + t = ceilf(x); + if (t - x > 0.5) + t -= 1.0; + return (t); + } else { + t = ceilf(-x); + if (t + x > 0.5) + t -= 1.0; + return (-t); + } +} diff --git a/05/musl-final/src/math/s_scalbln.c b/05/musl-final/src/math/s_scalbln.c new file mode 100644 index 0000000..12b9391 --- /dev/null +++ b/05/musl-final/src/math/s_scalbln.c @@ -0,0 +1,61 @@ +/* @(#)s_scalbn.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * scalbn (double x, int n) + * scalbn(x,n) returns x* 2**n computed by exponent + * manipulation rather than by actually performing an + * exponentiation or a multiplication. + */ + +#include +#include "math_private.h" + +static const double +two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ +twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ +huge = 1.0e+300, +tiny = 1.0e-300; + +double +scalbln (double x, long n) +{ + int32_t k,hx,lx; + EXTRACT_WORDS(hx,lx,x); + k = (hx&0x7ff00000)>>20; /* extract exponent */ + if (k==0) { /* 0 or subnormal x */ + if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ + x *= two54; + GET_HIGH_WORD(hx,x); + k = ((hx&0x7ff00000)>>20) - 54; + if (n< -50000) return tiny*x; /*underflow*/ + } + if (k==0x7ff) return x+x; /* NaN or Inf */ + k = k+n; + if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */ + if (k > 0) /* normal result */ + {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} + if (k <= -54) { + if (n > 50000) /* in case integer overflow in n+k */ + return huge*copysign(huge,x); /*overflow*/ + else return tiny*copysign(tiny,x); /*underflow*/ + } + k += 54; /* subnormal result */ + SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); + return x*twom54; +} + +double +scalbn (double x, int n) +{ + return scalbln(x, n); +} diff --git a/05/musl-final/src/math/s_scalblnf.c b/05/musl-final/src/math/s_scalblnf.c new file mode 100644 index 0000000..21e7641 --- /dev/null +++ b/05/musl-final/src/math/s_scalblnf.c @@ -0,0 +1,57 @@ +/* s_scalbnf.c -- float version of s_scalbn.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float +two25 = 3.355443200e+07, /* 0x4c000000 */ +twom25 = 2.9802322388e-08, /* 0x33000000 */ +huge = 1.0e+30, +tiny = 1.0e-30; + +float +scalblnf (float x, long n) +{ + int32_t k,ix; + GET_FLOAT_WORD(ix,x); + k = (ix&0x7f800000)>>23; /* extract exponent */ + if (k==0) { /* 0 or subnormal x */ + if ((ix&0x7fffffff)==0) return x; /* +-0 */ + x *= two25; + GET_FLOAT_WORD(ix,x); + k = ((ix&0x7f800000)>>23) - 25; + if (n< -50000) return tiny*x; /*underflow*/ + } + if (k==0xff) return x+x; /* NaN or Inf */ + k = k+n; + if (k > 0xfe) return huge*copysignf(huge,x); /* overflow */ + if (k > 0) /* normal result */ + {SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); return x;} + if (k <= -25) { + if (n > 50000) /* in case integer overflow in n+k */ + return huge*copysignf(huge,x); /*overflow*/ + else return tiny*copysignf(tiny,x); /*underflow*/ + } + k += 25; /* subnormal result */ + SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); + return x*twom25; +} + +float +scalbnf (float x, int n) +{ + return scalblnf(x, n); +} diff --git a/05/musl-final/src/math/s_sin.c b/05/musl-final/src/math/s_sin.c new file mode 100644 index 0000000..2a2774e --- /dev/null +++ b/05/musl-final/src/math/s_sin.c @@ -0,0 +1,74 @@ +/* @(#)s_sin.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* sin(x) + * Return sine function of x. + * + * kernel function: + * __kernel_sin ... sine function on [-pi/4,pi/4] + * __kernel_cos ... cose function on [-pi/4,pi/4] + * __ieee754_rem_pio2 ... argument reduction routine + * + * Method. + * Let S,C and T denote the sin, cos and tan respectively on + * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 + * in [-pi/4 , +pi/4], and let n = k mod 4. + * We have + * + * n sin(x) cos(x) tan(x) + * ---------------------------------------------------------- + * 0 S C T + * 1 C -S -1/T + * 2 -S -C T + * 3 -C S -1/T + * ---------------------------------------------------------- + * + * Special cases: + * Let trig be any of sin, cos, or tan. + * trig(+-INF) is NaN, with signals; + * trig(NaN) is that NaN; + * + * Accuracy: + * TRIG(x) returns trig(x) nearly rounded + */ + +#include +#include "math_private.h" + +double +sin(double x) +{ + double y[2],z=0.0; + int32_t n, ix; + + /* High word of x. */ + GET_HIGH_WORD(ix,x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if(ix <= 0x3fe921fb) return __kernel_sin(x,z,0); + + /* sin(Inf or NaN) is NaN */ + else if (ix>=0x7ff00000) return x-x; + + /* argument reduction needed */ + else { + n = __ieee754_rem_pio2(x,y); + switch(n&3) { + case 0: return __kernel_sin(y[0],y[1],1); + case 1: return __kernel_cos(y[0],y[1]); + case 2: return -__kernel_sin(y[0],y[1],1); + default: + return -__kernel_cos(y[0],y[1]); + } + } +} diff --git a/05/musl-final/src/math/s_sinf.c b/05/musl-final/src/math/s_sinf.c new file mode 100644 index 0000000..d2b8e80 --- /dev/null +++ b/05/musl-final/src/math/s_sinf.c @@ -0,0 +1,45 @@ +/* s_sinf.c -- float version of s_sin.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +float +sinf(float x) +{ + float y[2],z=0.0; + int32_t n, ix; + + GET_FLOAT_WORD(ix,x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if(ix <= 0x3f490fd8) return __kernel_sinf(x,z,0); + + /* sin(Inf or NaN) is NaN */ + else if (ix>=0x7f800000) return x-x; + + /* argument reduction needed */ + else { + n = __ieee754_rem_pio2f(x,y); + switch(n&3) { + case 0: return __kernel_sinf(y[0],y[1],1); + case 1: return __kernel_cosf(y[0],y[1]); + case 2: return -__kernel_sinf(y[0],y[1],1); + default: + return -__kernel_cosf(y[0],y[1]); + } + } +} diff --git a/05/musl-final/src/math/s_tan.c b/05/musl-final/src/math/s_tan.c new file mode 100644 index 0000000..3333cb3 --- /dev/null +++ b/05/musl-final/src/math/s_tan.c @@ -0,0 +1,68 @@ +/* @(#)s_tan.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* tan(x) + * Return tangent function of x. + * + * kernel function: + * __kernel_tan ... tangent function on [-pi/4,pi/4] + * __ieee754_rem_pio2 ... argument reduction routine + * + * Method. + * Let S,C and T denote the sin, cos and tan respectively on + * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 + * in [-pi/4 , +pi/4], and let n = k mod 4. + * We have + * + * n sin(x) cos(x) tan(x) + * ---------------------------------------------------------- + * 0 S C T + * 1 C -S -1/T + * 2 -S -C T + * 3 -C S -1/T + * ---------------------------------------------------------- + * + * Special cases: + * Let trig be any of sin, cos, or tan. + * trig(+-INF) is NaN, with signals; + * trig(NaN) is that NaN; + * + * Accuracy: + * TRIG(x) returns trig(x) nearly rounded + */ + +#include +#include "math_private.h" + +double +tan(double x) +{ + double y[2],z=0.0; + int32_t n, ix; + + /* High word of x. */ + GET_HIGH_WORD(ix,x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1); + + /* tan(Inf or NaN) is NaN */ + else if (ix>=0x7ff00000) return x-x; /* NaN */ + + /* argument reduction needed */ + else { + n = __ieee754_rem_pio2(x,y); + return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even + -1 -- n odd */ + } +} diff --git a/05/musl-final/src/math/s_tanf.c b/05/musl-final/src/math/s_tanf.c new file mode 100644 index 0000000..660dd9c --- /dev/null +++ b/05/musl-final/src/math/s_tanf.c @@ -0,0 +1,40 @@ +/* s_tanf.c -- float version of s_tan.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +float +tanf(float x) +{ + float y[2],z=0.0; + int32_t n, ix; + + GET_FLOAT_WORD(ix,x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if(ix <= 0x3f490fda) return __kernel_tanf(x,z,1); + + /* tan(Inf or NaN) is NaN */ + else if (ix>=0x7f800000) return x-x; /* NaN */ + + /* argument reduction needed */ + else { + n = __ieee754_rem_pio2f(x,y); + return __kernel_tanf(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even + -1 -- n odd */ + } +} diff --git a/05/musl-final/src/math/s_tanh.c b/05/musl-final/src/math/s_tanh.c new file mode 100644 index 0000000..78b8e84 --- /dev/null +++ b/05/musl-final/src/math/s_tanh.c @@ -0,0 +1,74 @@ +/* @(#)s_tanh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* Tanh(x) + * Return the Hyperbolic Tangent of x + * + * Method : + * x -x + * e - e + * 0. tanh(x) is defined to be ----------- + * x -x + * e + e + * 1. reduce x to non-negative by tanh(-x) = -tanh(x). + * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) + * -t + * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) + * t + 2 + * 2 + * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) + * t + 2 + * 22.0 < x <= INF : tanh(x) := 1. + * + * Special cases: + * tanh(NaN) is NaN; + * only tanh(0)=0 is exact for finite argument. + */ + +#include +#include "math_private.h" + +static const double one=1.0, two=2.0, tiny = 1.0e-300; + +double +tanh(double x) +{ + double t,z; + int32_t jx,ix; + + /* High word of |x|. */ + GET_HIGH_WORD(jx,x); + ix = jx&0x7fffffff; + + /* x is INF or NaN */ + if(ix>=0x7ff00000) { + if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ + else return one/x-one; /* tanh(NaN) = NaN */ + } + + /* |x| < 22 */ + if (ix < 0x40360000) { /* |x|<22 */ + if (ix<0x3c800000) /* |x|<2**-55 */ + return x*(one+x); /* tanh(small) = small */ + if (ix>=0x3ff00000) { /* |x|>=1 */ + t = expm1(two*fabs(x)); + z = one - two/(t+two); + } else { + t = expm1(-two*fabs(x)); + z= -t/(t+two); + } + /* |x| > 22, return +-1 */ + } else { + z = one - tiny; /* raised inexact flag */ + } + return (jx>=0)? z: -z; +} diff --git a/05/musl-final/src/math/s_tanhf.c b/05/musl-final/src/math/s_tanhf.c new file mode 100644 index 0000000..a082040 --- /dev/null +++ b/05/musl-final/src/math/s_tanhf.c @@ -0,0 +1,52 @@ +/* s_tanhf.c -- float version of s_tanh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "math_private.h" + +static const float one=1.0, two=2.0, tiny = 1.0e-30; + +float +tanhf(float x) +{ + float t,z; + int32_t jx,ix; + + GET_FLOAT_WORD(jx,x); + ix = jx&0x7fffffff; + + /* x is INF or NaN */ + if(ix>=0x7f800000) { + if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ + else return one/x-one; /* tanh(NaN) = NaN */ + } + + /* |x| < 22 */ + if (ix < 0x41b00000) { /* |x|<22 */ + if (ix<0x24000000) /* |x|<2**-55 */ + return x*(one+x); /* tanh(small) = small */ + if (ix>=0x3f800000) { /* |x|>=1 */ + t = expm1f(two*fabsf(x)); + z = one - two/(t+two); + } else { + t = expm1f(-two*fabsf(x)); + z= -t/(t+two); + } + /* |x| > 22, return +-1 */ + } else { + z = one - tiny; /* raised inexact flag */ + } + return (jx>=0)? z: -z; +} diff --git a/05/musl-final/src/math/s_trunc.c b/05/musl-final/src/math/s_trunc.c new file mode 100644 index 0000000..02c6556 --- /dev/null +++ b/05/musl-final/src/math/s_trunc.c @@ -0,0 +1,58 @@ +/* @(#)s_floor.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * trunc(x) + * Return x rounded toward 0 to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to trunc(x). + */ + +#include +#include "math_private.h" + +static const double huge = 1.0e300; + +double +trunc(double x) +{ + int32_t i0,i1,j0; + uint32_t i,j; + EXTRACT_WORDS(i0,i1,x); + j0 = ((i0>>20)&0x7ff)-0x3ff; + if(j0<20) { + if(j0<0) { /* raise inexact if x != 0 */ + if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */ + i0 &= 0x80000000U; + i1 = 0; + } + } else { + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) return x; /* x is integral */ + if(huge+x>0.0) { /* raise inexact flag */ + i0 &= (~i); i1=0; + } + } + } else if (j0>51) { + if(j0==0x400) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } else { + i = ((uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) return x; /* x is integral */ + if(huge+x>0.0) /* raise inexact flag */ + i1 &= (~i); + } + INSERT_WORDS(x,i0,i1); + return x; +} diff --git a/05/musl-final/src/math/s_truncf.c b/05/musl-final/src/math/s_truncf.c new file mode 100644 index 0000000..c253e62 --- /dev/null +++ b/05/musl-final/src/math/s_truncf.c @@ -0,0 +1,50 @@ +/* @(#)s_floor.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * truncf(x) + * Return x rounded toward 0 to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to truncf(x). + */ + +#include +#include "math_private.h" + +static const float huge = 1.0e30F; + +float +truncf(float x) +{ + int32_t i0,j0; + uint32_t i; + GET_FLOAT_WORD(i0,x); + j0 = ((i0>>23)&0xff)-0x7f; + if(j0<23) { + if(j0<0) { /* raise inexact if x != 0 */ + if(huge+x>0.0F) /* |x|<1, so return 0*sign(x) */ + i0 &= 0x80000000; + } else { + i = (0x007fffff)>>j0; + if((i0&i)==0) return x; /* x is integral */ + if(huge+x>0.0F) /* raise inexact flag */ + i0 &= (~i); + } + } else { + if(j0==0x80) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } + SET_FLOAT_WORD(x,i0); + return x; +} diff --git a/05/musl-final/src/misc/basename.c b/05/musl-final/src/misc/basename.c new file mode 100644 index 0000000..c87e00c --- /dev/null +++ b/05/musl-final/src/misc/basename.c @@ -0,0 +1,12 @@ +#include +#include + +char *basename(char *s) +{ + size_t i; + if (!s || !*s) return "."; + i = strlen(s)-1; + for (; i&&s[i]=='/'; i--) s[i] = 0; + for (; i&&s[i-1]!='/'; i--); + return s+i; +} diff --git a/05/musl-final/src/misc/bswap_32.c b/05/musl-final/src/misc/bswap_32.c new file mode 100644 index 0000000..a2418ca --- /dev/null +++ b/05/musl-final/src/misc/bswap_32.c @@ -0,0 +1,7 @@ +#include +#include + +uint32_t bswap_32(uint32_t x) +{ + return x>>24 | x>>16&0xff00 | x<<16&0xff0000 | x<<24; +} diff --git a/05/musl-final/src/misc/bswap_64.c b/05/musl-final/src/misc/bswap_64.c new file mode 100644 index 0000000..961300b --- /dev/null +++ b/05/musl-final/src/misc/bswap_64.c @@ -0,0 +1,9 @@ +#include +#include + +uint32_t bswap_32(uint32_t); + +uint64_t bswap_64(uint64_t x) +{ + return bswap_32(x)+0LL<<32 | bswap_32(x>>32); +} diff --git a/05/musl-final/src/misc/crypt.c b/05/musl-final/src/misc/crypt.c new file mode 100644 index 0000000..42918ef --- /dev/null +++ b/05/musl-final/src/misc/crypt.c @@ -0,0 +1,2578 @@ +/* + * FreeSec: libcrypt for NetBSD + * + * Copyright (c) 1994 David Burren + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the author nor the names of other contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This is an original implementation of the DES and the crypt(3) interfaces + * by David Burren . + * + * An excellent reference on the underlying algorithm (and related + * algorithms) is: + * + * B. Schneier, Applied Cryptography: protocols, algorithms, + * and source code in C, John Wiley & Sons, 1994. + * + * Note that in that book's description of DES the lookups for the initial, + * pbox, and final permutations are inverted (this has been brought to the + * attention of the author). A list of errata for this book has been + * posted to the sci.crypt newsgroup by the author and is available for FTP. + * + * ARCHITECTURE ASSUMPTIONS: + * It is assumed that the 8-byte arrays passed by reference can be + * addressed as arrays of uint32_t's (ie. the CPU is not picky about + * alignment). + */ + +#include +#include "libc.h" + +static int i64c(int i) +{ + i &= 0x3f; + if (i == 0) + return '.'; + if (i == 1) + return '/'; + if (i < 12) + return ('0' - 2 + i); + if (i < 38) + return ('A' - 12 + i); + return ('a' - 38 + i); +} + +/* Generated by const_des_init() (removed) */ + +static const uint8_t final_perm[64] = { + 0x39, 0x31, 0x29, 0x21, 0x19, 0x11, 0x09, 0x01, 0x3b, 0x33, 0x2b, 0x23, 0x1b, 0x13, 0x0b, 0x03, + 0x3d, 0x35, 0x2d, 0x25, 0x1d, 0x15, 0x0d, 0x05, 0x3f, 0x37, 0x2f, 0x27, 0x1f, 0x17, 0x0f, 0x07, + 0x38, 0x30, 0x28, 0x20, 0x18, 0x10, 0x08, 0x00, 0x3a, 0x32, 0x2a, 0x22, 0x1a, 0x12, 0x0a, 0x02, + 0x3c, 0x34, 0x2c, 0x24, 0x1c, 0x14, 0x0c, 0x04, 0x3e, 0x36, 0x2e, 0x26, 0x1e, 0x16, 0x0e, 0x06, +}; +static const uint8_t m_sbox[4][4096] = { + { + 0xef, 0xe3, 0xe1, 0xed, 0xe8, 0xe4, 0xee, 0xe7, + 0xe6, 0xef, 0xeb, 0xe2, 0xe3, 0xe8, 0xe4, 0xee, + 0xe9, 0xec, 0xe7, 0xe0, 0xe2, 0xe1, 0xed, 0xea, + 0xec, 0xe6, 0xe0, 0xe9, 0xe5, 0xeb, 0xea, 0xe5, + 0xe0, 0xed, 0xee, 0xe8, 0xe7, 0xea, 0xeb, 0xe1, + 0xea, 0xe3, 0xe4, 0xef, 0xed, 0xe4, 0xe1, 0xe2, + 0xe5, 0xeb, 0xe8, 0xe6, 0xec, 0xe7, 0xe6, 0xec, + 0xe9, 0xe0, 0xe3, 0xe5, 0xe2, 0xee, 0xef, 0xe9, + 0x0f, 0x03, 0x01, 0x0d, 0x08, 0x04, 0x0e, 0x07, + 0x06, 0x0f, 0x0b, 0x02, 0x03, 0x08, 0x04, 0x0e, + 0x09, 0x0c, 0x07, 0x00, 0x02, 0x01, 0x0d, 0x0a, + 0x0c, 0x06, 0x00, 0x09, 0x05, 0x0b, 0x0a, 0x05, + 0x00, 0x0d, 0x0e, 0x08, 0x07, 0x0a, 0x0b, 0x01, + 0x0a, 0x03, 0x04, 0x0f, 0x0d, 0x04, 0x01, 0x02, + 0x05, 0x0b, 0x08, 0x06, 0x0c, 0x07, 0x06, 0x0c, + 0x09, 0x00, 0x03, 0x05, 0x02, 0x0e, 0x0f, 0x09, + 0x4f, 0x43, 0x41, 0x4d, 0x48, 0x44, 0x4e, 0x47, + 0x46, 0x4f, 0x4b, 0x42, 0x43, 0x48, 0x44, 0x4e, + 0x49, 0x4c, 0x47, 0x40, 0x42, 0x41, 0x4d, 0x4a, + 0x4c, 0x46, 0x40, 0x49, 0x45, 0x4b, 0x4a, 0x45, + 0x40, 0x4d, 0x4e, 0x48, 0x47, 0x4a, 0x4b, 0x41, + 0x4a, 0x43, 0x44, 0x4f, 0x4d, 0x44, 0x41, 0x42, + 0x45, 0x4b, 0x48, 0x46, 0x4c, 0x47, 0x46, 0x4c, + 0x49, 0x40, 0x43, 0x45, 0x42, 0x4e, 0x4f, 0x49, + 0xff, 0xf3, 0xf1, 0xfd, 0xf8, 0xf4, 0xfe, 0xf7, + 0xf6, 0xff, 0xfb, 0xf2, 0xf3, 0xf8, 0xf4, 0xfe, + 0xf9, 0xfc, 0xf7, 0xf0, 0xf2, 0xf1, 0xfd, 0xfa, + 0xfc, 0xf6, 0xf0, 0xf9, 0xf5, 0xfb, 0xfa, 0xf5, + 0xf0, 0xfd, 0xfe, 0xf8, 0xf7, 0xfa, 0xfb, 0xf1, + 0xfa, 0xf3, 0xf4, 0xff, 0xfd, 0xf4, 0xf1, 0xf2, + 0xf5, 0xfb, 0xf8, 0xf6, 0xfc, 0xf7, 0xf6, 0xfc, + 0xf9, 0xf0, 0xf3, 0xf5, 0xf2, 0xfe, 0xff, 0xf9, + 0xdf, 0xd3, 0xd1, 0xdd, 0xd8, 0xd4, 0xde, 0xd7, + 0xd6, 0xdf, 0xdb, 0xd2, 0xd3, 0xd8, 0xd4, 0xde, + 0xd9, 0xdc, 0xd7, 0xd0, 0xd2, 0xd1, 0xdd, 0xda, + 0xdc, 0xd6, 0xd0, 0xd9, 0xd5, 0xdb, 0xda, 0xd5, + 0xd0, 0xdd, 0xde, 0xd8, 0xd7, 0xda, 0xdb, 0xd1, + 0xda, 0xd3, 0xd4, 0xdf, 0xdd, 0xd4, 0xd1, 0xd2, + 0xd5, 0xdb, 0xd8, 0xd6, 0xdc, 0xd7, 0xd6, 0xdc, + 0xd9, 0xd0, 0xd3, 0xd5, 0xd2, 0xde, 0xdf, 0xd9, + 0x7f, 0x73, 0x71, 0x7d, 0x78, 0x74, 0x7e, 0x77, + 0x76, 0x7f, 0x7b, 0x72, 0x73, 0x78, 0x74, 0x7e, + 0x79, 0x7c, 0x77, 0x70, 0x72, 0x71, 0x7d, 0x7a, + 0x7c, 0x76, 0x70, 0x79, 0x75, 0x7b, 0x7a, 0x75, + 0x70, 0x7d, 0x7e, 0x78, 0x77, 0x7a, 0x7b, 0x71, + 0x7a, 0x73, 0x74, 0x7f, 0x7d, 0x74, 0x71, 0x72, + 0x75, 0x7b, 0x78, 0x76, 0x7c, 0x77, 0x76, 0x7c, + 0x79, 0x70, 0x73, 0x75, 0x72, 0x7e, 0x7f, 0x79, + 0x1f, 0x13, 0x11, 0x1d, 0x18, 0x14, 0x1e, 0x17, + 0x16, 0x1f, 0x1b, 0x12, 0x13, 0x18, 0x14, 0x1e, + 0x19, 0x1c, 0x17, 0x10, 0x12, 0x11, 0x1d, 0x1a, + 0x1c, 0x16, 0x10, 0x19, 0x15, 0x1b, 0x1a, 0x15, + 0x10, 0x1d, 0x1e, 0x18, 0x17, 0x1a, 0x1b, 0x11, + 0x1a, 0x13, 0x14, 0x1f, 0x1d, 0x14, 0x11, 0x12, + 0x15, 0x1b, 0x18, 0x16, 0x1c, 0x17, 0x16, 0x1c, + 0x19, 0x10, 0x13, 0x15, 0x12, 0x1e, 0x1f, 0x19, + 0x4f, 0x43, 0x41, 0x4d, 0x48, 0x44, 0x4e, 0x47, + 0x46, 0x4f, 0x4b, 0x42, 0x43, 0x48, 0x44, 0x4e, + 0x49, 0x4c, 0x47, 0x40, 0x42, 0x41, 0x4d, 0x4a, + 0x4c, 0x46, 0x40, 0x49, 0x45, 0x4b, 0x4a, 0x45, + 0x40, 0x4d, 0x4e, 0x48, 0x47, 0x4a, 0x4b, 0x41, + 0x4a, 0x43, 0x44, 0x4f, 0x4d, 0x44, 0x41, 0x42, + 0x45, 0x4b, 0x48, 0x46, 0x4c, 0x47, 0x46, 0x4c, + 0x49, 0x40, 0x43, 0x45, 0x42, 0x4e, 0x4f, 0x49, + 0x2f, 0x23, 0x21, 0x2d, 0x28, 0x24, 0x2e, 0x27, + 0x26, 0x2f, 0x2b, 0x22, 0x23, 0x28, 0x24, 0x2e, + 0x29, 0x2c, 0x27, 0x20, 0x22, 0x21, 0x2d, 0x2a, + 0x2c, 0x26, 0x20, 0x29, 0x25, 0x2b, 0x2a, 0x25, + 0x20, 0x2d, 0x2e, 0x28, 0x27, 0x2a, 0x2b, 0x21, + 0x2a, 0x23, 0x24, 0x2f, 0x2d, 0x24, 0x21, 0x22, + 0x25, 0x2b, 0x28, 0x26, 0x2c, 0x27, 0x26, 0x2c, + 0x29, 0x20, 0x23, 0x25, 0x22, 0x2e, 0x2f, 0x29, + 0xef, 0xe3, 0xe1, 0xed, 0xe8, 0xe4, 0xee, 0xe7, + 0xe6, 0xef, 0xeb, 0xe2, 0xe3, 0xe8, 0xe4, 0xee, + 0xe9, 0xec, 0xe7, 0xe0, 0xe2, 0xe1, 0xed, 0xea, + 0xec, 0xe6, 0xe0, 0xe9, 0xe5, 0xeb, 0xea, 0xe5, + 0xe0, 0xed, 0xee, 0xe8, 0xe7, 0xea, 0xeb, 0xe1, + 0xea, 0xe3, 0xe4, 0xef, 0xed, 0xe4, 0xe1, 0xe2, + 0xe5, 0xeb, 0xe8, 0xe6, 0xec, 0xe7, 0xe6, 0xec, + 0xe9, 0xe0, 0xe3, 0xe5, 0xe2, 0xee, 0xef, 0xe9, + 0xff, 0xf3, 0xf1, 0xfd, 0xf8, 0xf4, 0xfe, 0xf7, + 0xf6, 0xff, 0xfb, 0xf2, 0xf3, 0xf8, 0xf4, 0xfe, + 0xf9, 0xfc, 0xf7, 0xf0, 0xf2, 0xf1, 0xfd, 0xfa, + 0xfc, 0xf6, 0xf0, 0xf9, 0xf5, 0xfb, 0xfa, 0xf5, + 0xf0, 0xfd, 0xfe, 0xf8, 0xf7, 0xfa, 0xfb, 0xf1, + 0xfa, 0xf3, 0xf4, 0xff, 0xfd, 0xf4, 0xf1, 0xf2, + 0xf5, 0xfb, 0xf8, 0xf6, 0xfc, 0xf7, 0xf6, 0xfc, + 0xf9, 0xf0, 0xf3, 0xf5, 0xf2, 0xfe, 0xff, 0xf9, + 0x2f, 0x23, 0x21, 0x2d, 0x28, 0x24, 0x2e, 0x27, + 0x26, 0x2f, 0x2b, 0x22, 0x23, 0x28, 0x24, 0x2e, + 0x29, 0x2c, 0x27, 0x20, 0x22, 0x21, 0x2d, 0x2a, + 0x2c, 0x26, 0x20, 0x29, 0x25, 0x2b, 0x2a, 0x25, + 0x20, 0x2d, 0x2e, 0x28, 0x27, 0x2a, 0x2b, 0x21, + 0x2a, 0x23, 0x24, 0x2f, 0x2d, 0x24, 0x21, 0x22, + 0x25, 0x2b, 0x28, 0x26, 0x2c, 0x27, 0x26, 0x2c, + 0x29, 0x20, 0x23, 0x25, 0x22, 0x2e, 0x2f, 0x29, + 0xbf, 0xb3, 0xb1, 0xbd, 0xb8, 0xb4, 0xbe, 0xb7, + 0xb6, 0xbf, 0xbb, 0xb2, 0xb3, 0xb8, 0xb4, 0xbe, + 0xb9, 0xbc, 0xb7, 0xb0, 0xb2, 0xb1, 0xbd, 0xba, + 0xbc, 0xb6, 0xb0, 0xb9, 0xb5, 0xbb, 0xba, 0xb5, + 0xb0, 0xbd, 0xbe, 0xb8, 0xb7, 0xba, 0xbb, 0xb1, + 0xba, 0xb3, 0xb4, 0xbf, 0xbd, 0xb4, 0xb1, 0xb2, + 0xb5, 0xbb, 0xb8, 0xb6, 0xbc, 0xb7, 0xb6, 0xbc, + 0xb9, 0xb0, 0xb3, 0xb5, 0xb2, 0xbe, 0xbf, 0xb9, + 0xdf, 0xd3, 0xd1, 0xdd, 0xd8, 0xd4, 0xde, 0xd7, + 0xd6, 0xdf, 0xdb, 0xd2, 0xd3, 0xd8, 0xd4, 0xde, + 0xd9, 0xdc, 0xd7, 0xd0, 0xd2, 0xd1, 0xdd, 0xda, + 0xdc, 0xd6, 0xd0, 0xd9, 0xd5, 0xdb, 0xda, 0xd5, + 0xd0, 0xdd, 0xde, 0xd8, 0xd7, 0xda, 0xdb, 0xd1, + 0xda, 0xd3, 0xd4, 0xdf, 0xdd, 0xd4, 0xd1, 0xd2, + 0xd5, 0xdb, 0xd8, 0xd6, 0xdc, 0xd7, 0xd6, 0xdc, + 0xd9, 0xd0, 0xd3, 0xd5, 0xd2, 0xde, 0xdf, 0xd9, + 0x8f, 0x83, 0x81, 0x8d, 0x88, 0x84, 0x8e, 0x87, + 0x86, 0x8f, 0x8b, 0x82, 0x83, 0x88, 0x84, 0x8e, + 0x89, 0x8c, 0x87, 0x80, 0x82, 0x81, 0x8d, 0x8a, + 0x8c, 0x86, 0x80, 0x89, 0x85, 0x8b, 0x8a, 0x85, + 0x80, 0x8d, 0x8e, 0x88, 0x87, 0x8a, 0x8b, 0x81, + 0x8a, 0x83, 0x84, 0x8f, 0x8d, 0x84, 0x81, 0x82, + 0x85, 0x8b, 0x88, 0x86, 0x8c, 0x87, 0x86, 0x8c, + 0x89, 0x80, 0x83, 0x85, 0x82, 0x8e, 0x8f, 0x89, + 0x1f, 0x13, 0x11, 0x1d, 0x18, 0x14, 0x1e, 0x17, + 0x16, 0x1f, 0x1b, 0x12, 0x13, 0x18, 0x14, 0x1e, + 0x19, 0x1c, 0x17, 0x10, 0x12, 0x11, 0x1d, 0x1a, + 0x1c, 0x16, 0x10, 0x19, 0x15, 0x1b, 0x1a, 0x15, + 0x10, 0x1d, 0x1e, 0x18, 0x17, 0x1a, 0x1b, 0x11, + 0x1a, 0x13, 0x14, 0x1f, 0x1d, 0x14, 0x11, 0x12, + 0x15, 0x1b, 0x18, 0x16, 0x1c, 0x17, 0x16, 0x1c, + 0x19, 0x10, 0x13, 0x15, 0x12, 0x1e, 0x1f, 0x19, + 0x3f, 0x33, 0x31, 0x3d, 0x38, 0x34, 0x3e, 0x37, + 0x36, 0x3f, 0x3b, 0x32, 0x33, 0x38, 0x34, 0x3e, + 0x39, 0x3c, 0x37, 0x30, 0x32, 0x31, 0x3d, 0x3a, + 0x3c, 0x36, 0x30, 0x39, 0x35, 0x3b, 0x3a, 0x35, + 0x30, 0x3d, 0x3e, 0x38, 0x37, 0x3a, 0x3b, 0x31, + 0x3a, 0x33, 0x34, 0x3f, 0x3d, 0x34, 0x31, 0x32, + 0x35, 0x3b, 0x38, 0x36, 0x3c, 0x37, 0x36, 0x3c, + 0x39, 0x30, 0x33, 0x35, 0x32, 0x3e, 0x3f, 0x39, + 0xaf, 0xa3, 0xa1, 0xad, 0xa8, 0xa4, 0xae, 0xa7, + 0xa6, 0xaf, 0xab, 0xa2, 0xa3, 0xa8, 0xa4, 0xae, + 0xa9, 0xac, 0xa7, 0xa0, 0xa2, 0xa1, 0xad, 0xaa, + 0xac, 0xa6, 0xa0, 0xa9, 0xa5, 0xab, 0xaa, 0xa5, + 0xa0, 0xad, 0xae, 0xa8, 0xa7, 0xaa, 0xab, 0xa1, + 0xaa, 0xa3, 0xa4, 0xaf, 0xad, 0xa4, 0xa1, 0xa2, + 0xa5, 0xab, 0xa8, 0xa6, 0xac, 0xa7, 0xa6, 0xac, + 0xa9, 0xa0, 0xa3, 0xa5, 0xa2, 0xae, 0xaf, 0xa9, + 0xaf, 0xa3, 0xa1, 0xad, 0xa8, 0xa4, 0xae, 0xa7, + 0xa6, 0xaf, 0xab, 0xa2, 0xa3, 0xa8, 0xa4, 0xae, + 0xa9, 0xac, 0xa7, 0xa0, 0xa2, 0xa1, 0xad, 0xaa, + 0xac, 0xa6, 0xa0, 0xa9, 0xa5, 0xab, 0xaa, 0xa5, + 0xa0, 0xad, 0xae, 0xa8, 0xa7, 0xaa, 0xab, 0xa1, + 0xaa, 0xa3, 0xa4, 0xaf, 0xad, 0xa4, 0xa1, 0xa2, + 0xa5, 0xab, 0xa8, 0xa6, 0xac, 0xa7, 0xa6, 0xac, + 0xa9, 0xa0, 0xa3, 0xa5, 0xa2, 0xae, 0xaf, 0xa9, + 0x6f, 0x63, 0x61, 0x6d, 0x68, 0x64, 0x6e, 0x67, + 0x66, 0x6f, 0x6b, 0x62, 0x63, 0x68, 0x64, 0x6e, + 0x69, 0x6c, 0x67, 0x60, 0x62, 0x61, 0x6d, 0x6a, + 0x6c, 0x66, 0x60, 0x69, 0x65, 0x6b, 0x6a, 0x65, + 0x60, 0x6d, 0x6e, 0x68, 0x67, 0x6a, 0x6b, 0x61, + 0x6a, 0x63, 0x64, 0x6f, 0x6d, 0x64, 0x61, 0x62, + 0x65, 0x6b, 0x68, 0x66, 0x6c, 0x67, 0x66, 0x6c, + 0x69, 0x60, 0x63, 0x65, 0x62, 0x6e, 0x6f, 0x69, + 0x6f, 0x63, 0x61, 0x6d, 0x68, 0x64, 0x6e, 0x67, + 0x66, 0x6f, 0x6b, 0x62, 0x63, 0x68, 0x64, 0x6e, + 0x69, 0x6c, 0x67, 0x60, 0x62, 0x61, 0x6d, 0x6a, + 0x6c, 0x66, 0x60, 0x69, 0x65, 0x6b, 0x6a, 0x65, + 0x60, 0x6d, 0x6e, 0x68, 0x67, 0x6a, 0x6b, 0x61, + 0x6a, 0x63, 0x64, 0x6f, 0x6d, 0x64, 0x61, 0x62, + 0x65, 0x6b, 0x68, 0x66, 0x6c, 0x67, 0x66, 0x6c, + 0x69, 0x60, 0x63, 0x65, 0x62, 0x6e, 0x6f, 0x69, + 0xcf, 0xc3, 0xc1, 0xcd, 0xc8, 0xc4, 0xce, 0xc7, + 0xc6, 0xcf, 0xcb, 0xc2, 0xc3, 0xc8, 0xc4, 0xce, + 0xc9, 0xcc, 0xc7, 0xc0, 0xc2, 0xc1, 0xcd, 0xca, + 0xcc, 0xc6, 0xc0, 0xc9, 0xc5, 0xcb, 0xca, 0xc5, + 0xc0, 0xcd, 0xce, 0xc8, 0xc7, 0xca, 0xcb, 0xc1, + 0xca, 0xc3, 0xc4, 0xcf, 0xcd, 0xc4, 0xc1, 0xc2, + 0xc5, 0xcb, 0xc8, 0xc6, 0xcc, 0xc7, 0xc6, 0xcc, + 0xc9, 0xc0, 0xc3, 0xc5, 0xc2, 0xce, 0xcf, 0xc9, + 0xcf, 0xc3, 0xc1, 0xcd, 0xc8, 0xc4, 0xce, 0xc7, + 0xc6, 0xcf, 0xcb, 0xc2, 0xc3, 0xc8, 0xc4, 0xce, + 0xc9, 0xcc, 0xc7, 0xc0, 0xc2, 0xc1, 0xcd, 0xca, + 0xcc, 0xc6, 0xc0, 0xc9, 0xc5, 0xcb, 0xca, 0xc5, + 0xc0, 0xcd, 0xce, 0xc8, 0xc7, 0xca, 0xcb, 0xc1, + 0xca, 0xc3, 0xc4, 0xcf, 0xcd, 0xc4, 0xc1, 0xc2, + 0xc5, 0xcb, 0xc8, 0xc6, 0xcc, 0xc7, 0xc6, 0xcc, + 0xc9, 0xc0, 0xc3, 0xc5, 0xc2, 0xce, 0xcf, 0xc9, + 0xbf, 0xb3, 0xb1, 0xbd, 0xb8, 0xb4, 0xbe, 0xb7, + 0xb6, 0xbf, 0xbb, 0xb2, 0xb3, 0xb8, 0xb4, 0xbe, + 0xb9, 0xbc, 0xb7, 0xb0, 0xb2, 0xb1, 0xbd, 0xba, + 0xbc, 0xb6, 0xb0, 0xb9, 0xb5, 0xbb, 0xba, 0xb5, + 0xb0, 0xbd, 0xbe, 0xb8, 0xb7, 0xba, 0xbb, 0xb1, + 0xba, 0xb3, 0xb4, 0xbf, 0xbd, 0xb4, 0xb1, 0xb2, + 0xb5, 0xbb, 0xb8, 0xb6, 0xbc, 0xb7, 0xb6, 0xbc, + 0xb9, 0xb0, 0xb3, 0xb5, 0xb2, 0xbe, 0xbf, 0xb9, + 0x5f, 0x53, 0x51, 0x5d, 0x58, 0x54, 0x5e, 0x57, + 0x56, 0x5f, 0x5b, 0x52, 0x53, 0x58, 0x54, 0x5e, + 0x59, 0x5c, 0x57, 0x50, 0x52, 0x51, 0x5d, 0x5a, + 0x5c, 0x56, 0x50, 0x59, 0x55, 0x5b, 0x5a, 0x55, + 0x50, 0x5d, 0x5e, 0x58, 0x57, 0x5a, 0x5b, 0x51, + 0x5a, 0x53, 0x54, 0x5f, 0x5d, 0x54, 0x51, 0x52, + 0x55, 0x5b, 0x58, 0x56, 0x5c, 0x57, 0x56, 0x5c, + 0x59, 0x50, 0x53, 0x55, 0x52, 0x5e, 0x5f, 0x59, + 0x9f, 0x93, 0x91, 0x9d, 0x98, 0x94, 0x9e, 0x97, + 0x96, 0x9f, 0x9b, 0x92, 0x93, 0x98, 0x94, 0x9e, + 0x99, 0x9c, 0x97, 0x90, 0x92, 0x91, 0x9d, 0x9a, + 0x9c, 0x96, 0x90, 0x99, 0x95, 0x9b, 0x9a, 0x95, + 0x90, 0x9d, 0x9e, 0x98, 0x97, 0x9a, 0x9b, 0x91, + 0x9a, 0x93, 0x94, 0x9f, 0x9d, 0x94, 0x91, 0x92, + 0x95, 0x9b, 0x98, 0x96, 0x9c, 0x97, 0x96, 0x9c, + 0x99, 0x90, 0x93, 0x95, 0x92, 0x9e, 0x9f, 0x99, + 0x9f, 0x93, 0x91, 0x9d, 0x98, 0x94, 0x9e, 0x97, + 0x96, 0x9f, 0x9b, 0x92, 0x93, 0x98, 0x94, 0x9e, + 0x99, 0x9c, 0x97, 0x90, 0x92, 0x91, 0x9d, 0x9a, + 0x9c, 0x96, 0x90, 0x99, 0x95, 0x9b, 0x9a, 0x95, + 0x90, 0x9d, 0x9e, 0x98, 0x97, 0x9a, 0x9b, 0x91, + 0x9a, 0x93, 0x94, 0x9f, 0x9d, 0x94, 0x91, 0x92, + 0x95, 0x9b, 0x98, 0x96, 0x9c, 0x97, 0x96, 0x9c, + 0x99, 0x90, 0x93, 0x95, 0x92, 0x9e, 0x9f, 0x99, + 0x5f, 0x53, 0x51, 0x5d, 0x58, 0x54, 0x5e, 0x57, + 0x56, 0x5f, 0x5b, 0x52, 0x53, 0x58, 0x54, 0x5e, + 0x59, 0x5c, 0x57, 0x50, 0x52, 0x51, 0x5d, 0x5a, + 0x5c, 0x56, 0x50, 0x59, 0x55, 0x5b, 0x5a, 0x55, + 0x50, 0x5d, 0x5e, 0x58, 0x57, 0x5a, 0x5b, 0x51, + 0x5a, 0x53, 0x54, 0x5f, 0x5d, 0x54, 0x51, 0x52, + 0x55, 0x5b, 0x58, 0x56, 0x5c, 0x57, 0x56, 0x5c, + 0x59, 0x50, 0x53, 0x55, 0x52, 0x5e, 0x5f, 0x59, + 0x0f, 0x03, 0x01, 0x0d, 0x08, 0x04, 0x0e, 0x07, + 0x06, 0x0f, 0x0b, 0x02, 0x03, 0x08, 0x04, 0x0e, + 0x09, 0x0c, 0x07, 0x00, 0x02, 0x01, 0x0d, 0x0a, + 0x0c, 0x06, 0x00, 0x09, 0x05, 0x0b, 0x0a, 0x05, + 0x00, 0x0d, 0x0e, 0x08, 0x07, 0x0a, 0x0b, 0x01, + 0x0a, 0x03, 0x04, 0x0f, 0x0d, 0x04, 0x01, 0x02, + 0x05, 0x0b, 0x08, 0x06, 0x0c, 0x07, 0x06, 0x0c, + 0x09, 0x00, 0x03, 0x05, 0x02, 0x0e, 0x0f, 0x09, + 0x3f, 0x33, 0x31, 0x3d, 0x38, 0x34, 0x3e, 0x37, + 0x36, 0x3f, 0x3b, 0x32, 0x33, 0x38, 0x34, 0x3e, + 0x39, 0x3c, 0x37, 0x30, 0x32, 0x31, 0x3d, 0x3a, + 0x3c, 0x36, 0x30, 0x39, 0x35, 0x3b, 0x3a, 0x35, + 0x30, 0x3d, 0x3e, 0x38, 0x37, 0x3a, 0x3b, 0x31, + 0x3a, 0x33, 0x34, 0x3f, 0x3d, 0x34, 0x31, 0x32, + 0x35, 0x3b, 0x38, 0x36, 0x3c, 0x37, 0x36, 0x3c, + 0x39, 0x30, 0x33, 0x35, 0x32, 0x3e, 0x3f, 0x39, + 0x7f, 0x73, 0x71, 0x7d, 0x78, 0x74, 0x7e, 0x77, + 0x76, 0x7f, 0x7b, 0x72, 0x73, 0x78, 0x74, 0x7e, + 0x79, 0x7c, 0x77, 0x70, 0x72, 0x71, 0x7d, 0x7a, + 0x7c, 0x76, 0x70, 0x79, 0x75, 0x7b, 0x7a, 0x75, + 0x70, 0x7d, 0x7e, 0x78, 0x77, 0x7a, 0x7b, 0x71, + 0x7a, 0x73, 0x74, 0x7f, 0x7d, 0x74, 0x71, 0x72, + 0x75, 0x7b, 0x78, 0x76, 0x7c, 0x77, 0x76, 0x7c, + 0x79, 0x70, 0x73, 0x75, 0x72, 0x7e, 0x7f, 0x79, + 0x8f, 0x83, 0x81, 0x8d, 0x88, 0x84, 0x8e, 0x87, + 0x86, 0x8f, 0x8b, 0x82, 0x83, 0x88, 0x84, 0x8e, + 0x89, 0x8c, 0x87, 0x80, 0x82, 0x81, 0x8d, 0x8a, + 0x8c, 0x86, 0x80, 0x89, 0x85, 0x8b, 0x8a, 0x85, + 0x80, 0x8d, 0x8e, 0x88, 0x87, 0x8a, 0x8b, 0x81, + 0x8a, 0x83, 0x84, 0x8f, 0x8d, 0x84, 0x81, 0x82, + 0x85, 0x8b, 0x88, 0x86, 0x8c, 0x87, 0x86, 0x8c, + 0x89, 0x80, 0x83, 0x85, 0x82, 0x8e, 0x8f, 0x89, + 0x4f, 0x43, 0x41, 0x4d, 0x48, 0x44, 0x4e, 0x47, + 0x46, 0x4f, 0x4b, 0x42, 0x43, 0x48, 0x44, 0x4e, + 0x49, 0x4c, 0x47, 0x40, 0x42, 0x41, 0x4d, 0x4a, + 0x4c, 0x46, 0x40, 0x49, 0x45, 0x4b, 0x4a, 0x45, + 0x40, 0x4d, 0x4e, 0x48, 0x47, 0x4a, 0x4b, 0x41, + 0x4a, 0x43, 0x44, 0x4f, 0x4d, 0x44, 0x41, 0x42, + 0x45, 0x4b, 0x48, 0x46, 0x4c, 0x47, 0x46, 0x4c, + 0x49, 0x40, 0x43, 0x45, 0x42, 0x4e, 0x4f, 0x49, + 0xff, 0xf3, 0xf1, 0xfd, 0xf8, 0xf4, 0xfe, 0xf7, + 0xf6, 0xff, 0xfb, 0xf2, 0xf3, 0xf8, 0xf4, 0xfe, + 0xf9, 0xfc, 0xf7, 0xf0, 0xf2, 0xf1, 0xfd, 0xfa, + 0xfc, 0xf6, 0xf0, 0xf9, 0xf5, 0xfb, 0xfa, 0xf5, + 0xf0, 0xfd, 0xfe, 0xf8, 0xf7, 0xfa, 0xfb, 0xf1, + 0xfa, 0xf3, 0xf4, 0xff, 0xfd, 0xf4, 0xf1, 0xf2, + 0xf5, 0xfb, 0xf8, 0xf6, 0xfc, 0xf7, 0xf6, 0xfc, + 0xf9, 0xf0, 0xf3, 0xf5, 0xf2, 0xfe, 0xff, 0xf9, + 0x1f, 0x13, 0x11, 0x1d, 0x18, 0x14, 0x1e, 0x17, + 0x16, 0x1f, 0x1b, 0x12, 0x13, 0x18, 0x14, 0x1e, + 0x19, 0x1c, 0x17, 0x10, 0x12, 0x11, 0x1d, 0x1a, + 0x1c, 0x16, 0x10, 0x19, 0x15, 0x1b, 0x1a, 0x15, + 0x10, 0x1d, 0x1e, 0x18, 0x17, 0x1a, 0x1b, 0x11, + 0x1a, 0x13, 0x14, 0x1f, 0x1d, 0x14, 0x11, 0x12, + 0x15, 0x1b, 0x18, 0x16, 0x1c, 0x17, 0x16, 0x1c, + 0x19, 0x10, 0x13, 0x15, 0x12, 0x1e, 0x1f, 0x19, + 0xcf, 0xc3, 0xc1, 0xcd, 0xc8, 0xc4, 0xce, 0xc7, + 0xc6, 0xcf, 0xcb, 0xc2, 0xc3, 0xc8, 0xc4, 0xce, + 0xc9, 0xcc, 0xc7, 0xc0, 0xc2, 0xc1, 0xcd, 0xca, + 0xcc, 0xc6, 0xc0, 0xc9, 0xc5, 0xcb, 0xca, 0xc5, + 0xc0, 0xcd, 0xce, 0xc8, 0xc7, 0xca, 0xcb, 0xc1, + 0xca, 0xc3, 0xc4, 0xcf, 0xcd, 0xc4, 0xc1, 0xc2, + 0xc5, 0xcb, 0xc8, 0xc6, 0xcc, 0xc7, 0xc6, 0xcc, + 0xc9, 0xc0, 0xc3, 0xc5, 0xc2, 0xce, 0xcf, 0xc9, + 0xef, 0xe3, 0xe1, 0xed, 0xe8, 0xe4, 0xee, 0xe7, + 0xe6, 0xef, 0xeb, 0xe2, 0xe3, 0xe8, 0xe4, 0xee, + 0xe9, 0xec, 0xe7, 0xe0, 0xe2, 0xe1, 0xed, 0xea, + 0xec, 0xe6, 0xe0, 0xe9, 0xe5, 0xeb, 0xea, 0xe5, + 0xe0, 0xed, 0xee, 0xe8, 0xe7, 0xea, 0xeb, 0xe1, + 0xea, 0xe3, 0xe4, 0xef, 0xed, 0xe4, 0xe1, 0xe2, + 0xe5, 0xeb, 0xe8, 0xe6, 0xec, 0xe7, 0xe6, 0xec, + 0xe9, 0xe0, 0xe3, 0xe5, 0xe2, 0xee, 0xef, 0xe9, + 0x8f, 0x83, 0x81, 0x8d, 0x88, 0x84, 0x8e, 0x87, + 0x86, 0x8f, 0x8b, 0x82, 0x83, 0x88, 0x84, 0x8e, + 0x89, 0x8c, 0x87, 0x80, 0x82, 0x81, 0x8d, 0x8a, + 0x8c, 0x86, 0x80, 0x89, 0x85, 0x8b, 0x8a, 0x85, + 0x80, 0x8d, 0x8e, 0x88, 0x87, 0x8a, 0x8b, 0x81, + 0x8a, 0x83, 0x84, 0x8f, 0x8d, 0x84, 0x81, 0x82, + 0x85, 0x8b, 0x88, 0x86, 0x8c, 0x87, 0x86, 0x8c, + 0x89, 0x80, 0x83, 0x85, 0x82, 0x8e, 0x8f, 0x89, + 0x8f, 0x83, 0x81, 0x8d, 0x88, 0x84, 0x8e, 0x87, + 0x86, 0x8f, 0x8b, 0x82, 0x83, 0x88, 0x84, 0x8e, + 0x89, 0x8c, 0x87, 0x80, 0x82, 0x81, 0x8d, 0x8a, + 0x8c, 0x86, 0x80, 0x89, 0x85, 0x8b, 0x8a, 0x85, + 0x80, 0x8d, 0x8e, 0x88, 0x87, 0x8a, 0x8b, 0x81, + 0x8a, 0x83, 0x84, 0x8f, 0x8d, 0x84, 0x81, 0x82, + 0x85, 0x8b, 0x88, 0x86, 0x8c, 0x87, 0x86, 0x8c, + 0x89, 0x80, 0x83, 0x85, 0x82, 0x8e, 0x8f, 0x89, + 0x2f, 0x23, 0x21, 0x2d, 0x28, 0x24, 0x2e, 0x27, + 0x26, 0x2f, 0x2b, 0x22, 0x23, 0x28, 0x24, 0x2e, + 0x29, 0x2c, 0x27, 0x20, 0x22, 0x21, 0x2d, 0x2a, + 0x2c, 0x26, 0x20, 0x29, 0x25, 0x2b, 0x2a, 0x25, + 0x20, 0x2d, 0x2e, 0x28, 0x27, 0x2a, 0x2b, 0x21, + 0x2a, 0x23, 0x24, 0x2f, 0x2d, 0x24, 0x21, 0x22, + 0x25, 0x2b, 0x28, 0x26, 0x2c, 0x27, 0x26, 0x2c, + 0x29, 0x20, 0x23, 0x25, 0x22, 0x2e, 0x2f, 0x29, + 0xdf, 0xd3, 0xd1, 0xdd, 0xd8, 0xd4, 0xde, 0xd7, + 0xd6, 0xdf, 0xdb, 0xd2, 0xd3, 0xd8, 0xd4, 0xde, + 0xd9, 0xdc, 0xd7, 0xd0, 0xd2, 0xd1, 0xdd, 0xda, + 0xdc, 0xd6, 0xd0, 0xd9, 0xd5, 0xdb, 0xda, 0xd5, + 0xd0, 0xdd, 0xde, 0xd8, 0xd7, 0xda, 0xdb, 0xd1, + 0xda, 0xd3, 0xd4, 0xdf, 0xdd, 0xd4, 0xd1, 0xd2, + 0xd5, 0xdb, 0xd8, 0xd6, 0xdc, 0xd7, 0xd6, 0xdc, + 0xd9, 0xd0, 0xd3, 0xd5, 0xd2, 0xde, 0xdf, 0xd9, + 0x4f, 0x43, 0x41, 0x4d, 0x48, 0x44, 0x4e, 0x47, + 0x46, 0x4f, 0x4b, 0x42, 0x43, 0x48, 0x44, 0x4e, + 0x49, 0x4c, 0x47, 0x40, 0x42, 0x41, 0x4d, 0x4a, + 0x4c, 0x46, 0x40, 0x49, 0x45, 0x4b, 0x4a, 0x45, + 0x40, 0x4d, 0x4e, 0x48, 0x47, 0x4a, 0x4b, 0x41, + 0x4a, 0x43, 0x44, 0x4f, 0x4d, 0x44, 0x41, 0x42, + 0x45, 0x4b, 0x48, 0x46, 0x4c, 0x47, 0x46, 0x4c, + 0x49, 0x40, 0x43, 0x45, 0x42, 0x4e, 0x4f, 0x49, + 0x6f, 0x63, 0x61, 0x6d, 0x68, 0x64, 0x6e, 0x67, + 0x66, 0x6f, 0x6b, 0x62, 0x63, 0x68, 0x64, 0x6e, + 0x69, 0x6c, 0x67, 0x60, 0x62, 0x61, 0x6d, 0x6a, + 0x6c, 0x66, 0x60, 0x69, 0x65, 0x6b, 0x6a, 0x65, + 0x60, 0x6d, 0x6e, 0x68, 0x67, 0x6a, 0x6b, 0x61, + 0x6a, 0x63, 0x64, 0x6f, 0x6d, 0x64, 0x61, 0x62, + 0x65, 0x6b, 0x68, 0x66, 0x6c, 0x67, 0x66, 0x6c, + 0x69, 0x60, 0x63, 0x65, 0x62, 0x6e, 0x6f, 0x69, + 0x9f, 0x93, 0x91, 0x9d, 0x98, 0x94, 0x9e, 0x97, + 0x96, 0x9f, 0x9b, 0x92, 0x93, 0x98, 0x94, 0x9e, + 0x99, 0x9c, 0x97, 0x90, 0x92, 0x91, 0x9d, 0x9a, + 0x9c, 0x96, 0x90, 0x99, 0x95, 0x9b, 0x9a, 0x95, + 0x90, 0x9d, 0x9e, 0x98, 0x97, 0x9a, 0x9b, 0x91, + 0x9a, 0x93, 0x94, 0x9f, 0x9d, 0x94, 0x91, 0x92, + 0x95, 0x9b, 0x98, 0x96, 0x9c, 0x97, 0x96, 0x9c, + 0x99, 0x90, 0x93, 0x95, 0x92, 0x9e, 0x9f, 0x99, + 0x2f, 0x23, 0x21, 0x2d, 0x28, 0x24, 0x2e, 0x27, + 0x26, 0x2f, 0x2b, 0x22, 0x23, 0x28, 0x24, 0x2e, + 0x29, 0x2c, 0x27, 0x20, 0x22, 0x21, 0x2d, 0x2a, + 0x2c, 0x26, 0x20, 0x29, 0x25, 0x2b, 0x2a, 0x25, + 0x20, 0x2d, 0x2e, 0x28, 0x27, 0x2a, 0x2b, 0x21, + 0x2a, 0x23, 0x24, 0x2f, 0x2d, 0x24, 0x21, 0x22, + 0x25, 0x2b, 0x28, 0x26, 0x2c, 0x27, 0x26, 0x2c, + 0x29, 0x20, 0x23, 0x25, 0x22, 0x2e, 0x2f, 0x29, + 0x1f, 0x13, 0x11, 0x1d, 0x18, 0x14, 0x1e, 0x17, + 0x16, 0x1f, 0x1b, 0x12, 0x13, 0x18, 0x14, 0x1e, + 0x19, 0x1c, 0x17, 0x10, 0x12, 0x11, 0x1d, 0x1a, + 0x1c, 0x16, 0x10, 0x19, 0x15, 0x1b, 0x1a, 0x15, + 0x10, 0x1d, 0x1e, 0x18, 0x17, 0x1a, 0x1b, 0x11, + 0x1a, 0x13, 0x14, 0x1f, 0x1d, 0x14, 0x11, 0x12, + 0x15, 0x1b, 0x18, 0x16, 0x1c, 0x17, 0x16, 0x1c, + 0x19, 0x10, 0x13, 0x15, 0x12, 0x1e, 0x1f, 0x19, + 0xbf, 0xb3, 0xb1, 0xbd, 0xb8, 0xb4, 0xbe, 0xb7, + 0xb6, 0xbf, 0xbb, 0xb2, 0xb3, 0xb8, 0xb4, 0xbe, + 0xb9, 0xbc, 0xb7, 0xb0, 0xb2, 0xb1, 0xbd, 0xba, + 0xbc, 0xb6, 0xb0, 0xb9, 0xb5, 0xbb, 0xba, 0xb5, + 0xb0, 0xbd, 0xbe, 0xb8, 0xb7, 0xba, 0xbb, 0xb1, + 0xba, 0xb3, 0xb4, 0xbf, 0xbd, 0xb4, 0xb1, 0xb2, + 0xb5, 0xbb, 0xb8, 0xb6, 0xbc, 0xb7, 0xb6, 0xbc, + 0xb9, 0xb0, 0xb3, 0xb5, 0xb2, 0xbe, 0xbf, 0xb9, + 0x7f, 0x73, 0x71, 0x7d, 0x78, 0x74, 0x7e, 0x77, + 0x76, 0x7f, 0x7b, 0x72, 0x73, 0x78, 0x74, 0x7e, + 0x79, 0x7c, 0x77, 0x70, 0x72, 0x71, 0x7d, 0x7a, + 0x7c, 0x76, 0x70, 0x79, 0x75, 0x7b, 0x7a, 0x75, + 0x70, 0x7d, 0x7e, 0x78, 0x77, 0x7a, 0x7b, 0x71, + 0x7a, 0x73, 0x74, 0x7f, 0x7d, 0x74, 0x71, 0x72, + 0x75, 0x7b, 0x78, 0x76, 0x7c, 0x77, 0x76, 0x7c, + 0x79, 0x70, 0x73, 0x75, 0x72, 0x7e, 0x7f, 0x79, + 0xff, 0xf3, 0xf1, 0xfd, 0xf8, 0xf4, 0xfe, 0xf7, + 0xf6, 0xff, 0xfb, 0xf2, 0xf3, 0xf8, 0xf4, 0xfe, + 0xf9, 0xfc, 0xf7, 0xf0, 0xf2, 0xf1, 0xfd, 0xfa, + 0xfc, 0xf6, 0xf0, 0xf9, 0xf5, 0xfb, 0xfa, 0xf5, + 0xf0, 0xfd, 0xfe, 0xf8, 0xf7, 0xfa, 0xfb, 0xf1, + 0xfa, 0xf3, 0xf4, 0xff, 0xfd, 0xf4, 0xf1, 0xf2, + 0xf5, 0xfb, 0xf8, 0xf6, 0xfc, 0xf7, 0xf6, 0xfc, + 0xf9, 0xf0, 0xf3, 0xf5, 0xf2, 0xfe, 0xff, 0xf9, + 0x5f, 0x53, 0x51, 0x5d, 0x58, 0x54, 0x5e, 0x57, + 0x56, 0x5f, 0x5b, 0x52, 0x53, 0x58, 0x54, 0x5e, + 0x59, 0x5c, 0x57, 0x50, 0x52, 0x51, 0x5d, 0x5a, + 0x5c, 0x56, 0x50, 0x59, 0x55, 0x5b, 0x5a, 0x55, + 0x50, 0x5d, 0x5e, 0x58, 0x57, 0x5a, 0x5b, 0x51, + 0x5a, 0x53, 0x54, 0x5f, 0x5d, 0x54, 0x51, 0x52, + 0x55, 0x5b, 0x58, 0x56, 0x5c, 0x57, 0x56, 0x5c, + 0x59, 0x50, 0x53, 0x55, 0x52, 0x5e, 0x5f, 0x59, + 0xcf, 0xc3, 0xc1, 0xcd, 0xc8, 0xc4, 0xce, 0xc7, + 0xc6, 0xcf, 0xcb, 0xc2, 0xc3, 0xc8, 0xc4, 0xce, + 0xc9, 0xcc, 0xc7, 0xc0, 0xc2, 0xc1, 0xcd, 0xca, + 0xcc, 0xc6, 0xc0, 0xc9, 0xc5, 0xcb, 0xca, 0xc5, + 0xc0, 0xcd, 0xce, 0xc8, 0xc7, 0xca, 0xcb, 0xc1, + 0xca, 0xc3, 0xc4, 0xcf, 0xcd, 0xc4, 0xc1, 0xc2, + 0xc5, 0xcb, 0xc8, 0xc6, 0xcc, 0xc7, 0xc6, 0xcc, + 0xc9, 0xc0, 0xc3, 0xc5, 0xc2, 0xce, 0xcf, 0xc9, + 0xbf, 0xb3, 0xb1, 0xbd, 0xb8, 0xb4, 0xbe, 0xb7, + 0xb6, 0xbf, 0xbb, 0xb2, 0xb3, 0xb8, 0xb4, 0xbe, + 0xb9, 0xbc, 0xb7, 0xb0, 0xb2, 0xb1, 0xbd, 0xba, + 0xbc, 0xb6, 0xb0, 0xb9, 0xb5, 0xbb, 0xba, 0xb5, + 0xb0, 0xbd, 0xbe, 0xb8, 0xb7, 0xba, 0xbb, 0xb1, + 0xba, 0xb3, 0xb4, 0xbf, 0xbd, 0xb4, 0xb1, 0xb2, + 0xb5, 0xbb, 0xb8, 0xb6, 0xbc, 0xb7, 0xb6, 0xbc, + 0xb9, 0xb0, 0xb3, 0xb5, 0xb2, 0xbe, 0xbf, 0xb9, + 0x9f, 0x93, 0x91, 0x9d, 0x98, 0x94, 0x9e, 0x97, + 0x96, 0x9f, 0x9b, 0x92, 0x93, 0x98, 0x94, 0x9e, + 0x99, 0x9c, 0x97, 0x90, 0x92, 0x91, 0x9d, 0x9a, + 0x9c, 0x96, 0x90, 0x99, 0x95, 0x9b, 0x9a, 0x95, + 0x90, 0x9d, 0x9e, 0x98, 0x97, 0x9a, 0x9b, 0x91, + 0x9a, 0x93, 0x94, 0x9f, 0x9d, 0x94, 0x91, 0x92, + 0x95, 0x9b, 0x98, 0x96, 0x9c, 0x97, 0x96, 0x9c, + 0x99, 0x90, 0x93, 0x95, 0x92, 0x9e, 0x9f, 0x99, + 0x3f, 0x33, 0x31, 0x3d, 0x38, 0x34, 0x3e, 0x37, + 0x36, 0x3f, 0x3b, 0x32, 0x33, 0x38, 0x34, 0x3e, + 0x39, 0x3c, 0x37, 0x30, 0x32, 0x31, 0x3d, 0x3a, + 0x3c, 0x36, 0x30, 0x39, 0x35, 0x3b, 0x3a, 0x35, + 0x30, 0x3d, 0x3e, 0x38, 0x37, 0x3a, 0x3b, 0x31, + 0x3a, 0x33, 0x34, 0x3f, 0x3d, 0x34, 0x31, 0x32, + 0x35, 0x3b, 0x38, 0x36, 0x3c, 0x37, 0x36, 0x3c, + 0x39, 0x30, 0x33, 0x35, 0x32, 0x3e, 0x3f, 0x39, + 0x7f, 0x73, 0x71, 0x7d, 0x78, 0x74, 0x7e, 0x77, + 0x76, 0x7f, 0x7b, 0x72, 0x73, 0x78, 0x74, 0x7e, + 0x79, 0x7c, 0x77, 0x70, 0x72, 0x71, 0x7d, 0x7a, + 0x7c, 0x76, 0x70, 0x79, 0x75, 0x7b, 0x7a, 0x75, + 0x70, 0x7d, 0x7e, 0x78, 0x77, 0x7a, 0x7b, 0x71, + 0x7a, 0x73, 0x74, 0x7f, 0x7d, 0x74, 0x71, 0x72, + 0x75, 0x7b, 0x78, 0x76, 0x7c, 0x77, 0x76, 0x7c, + 0x79, 0x70, 0x73, 0x75, 0x72, 0x7e, 0x7f, 0x79, + 0xef, 0xe3, 0xe1, 0xed, 0xe8, 0xe4, 0xee, 0xe7, + 0xe6, 0xef, 0xeb, 0xe2, 0xe3, 0xe8, 0xe4, 0xee, + 0xe9, 0xec, 0xe7, 0xe0, 0xe2, 0xe1, 0xed, 0xea, + 0xec, 0xe6, 0xe0, 0xe9, 0xe5, 0xeb, 0xea, 0xe5, + 0xe0, 0xed, 0xee, 0xe8, 0xe7, 0xea, 0xeb, 0xe1, + 0xea, 0xe3, 0xe4, 0xef, 0xed, 0xe4, 0xe1, 0xe2, + 0xe5, 0xeb, 0xe8, 0xe6, 0xec, 0xe7, 0xe6, 0xec, + 0xe9, 0xe0, 0xe3, 0xe5, 0xe2, 0xee, 0xef, 0xe9, + 0x3f, 0x33, 0x31, 0x3d, 0x38, 0x34, 0x3e, 0x37, + 0x36, 0x3f, 0x3b, 0x32, 0x33, 0x38, 0x34, 0x3e, + 0x39, 0x3c, 0x37, 0x30, 0x32, 0x31, 0x3d, 0x3a, + 0x3c, 0x36, 0x30, 0x39, 0x35, 0x3b, 0x3a, 0x35, + 0x30, 0x3d, 0x3e, 0x38, 0x37, 0x3a, 0x3b, 0x31, + 0x3a, 0x33, 0x34, 0x3f, 0x3d, 0x34, 0x31, 0x32, + 0x35, 0x3b, 0x38, 0x36, 0x3c, 0x37, 0x36, 0x3c, + 0x39, 0x30, 0x33, 0x35, 0x32, 0x3e, 0x3f, 0x39, + 0xaf, 0xa3, 0xa1, 0xad, 0xa8, 0xa4, 0xae, 0xa7, + 0xa6, 0xaf, 0xab, 0xa2, 0xa3, 0xa8, 0xa4, 0xae, + 0xa9, 0xac, 0xa7, 0xa0, 0xa2, 0xa1, 0xad, 0xaa, + 0xac, 0xa6, 0xa0, 0xa9, 0xa5, 0xab, 0xaa, 0xa5, + 0xa0, 0xad, 0xae, 0xa8, 0xa7, 0xaa, 0xab, 0xa1, + 0xaa, 0xa3, 0xa4, 0xaf, 0xad, 0xa4, 0xa1, 0xa2, + 0xa5, 0xab, 0xa8, 0xa6, 0xac, 0xa7, 0xa6, 0xac, + 0xa9, 0xa0, 0xa3, 0xa5, 0xa2, 0xae, 0xaf, 0xa9, + 0xaf, 0xa3, 0xa1, 0xad, 0xa8, 0xa4, 0xae, 0xa7, + 0xa6, 0xaf, 0xab, 0xa2, 0xa3, 0xa8, 0xa4, 0xae, + 0xa9, 0xac, 0xa7, 0xa0, 0xa2, 0xa1, 0xad, 0xaa, + 0xac, 0xa6, 0xa0, 0xa9, 0xa5, 0xab, 0xaa, 0xa5, + 0xa0, 0xad, 0xae, 0xa8, 0xa7, 0xaa, 0xab, 0xa1, + 0xaa, 0xa3, 0xa4, 0xaf, 0xad, 0xa4, 0xa1, 0xa2, + 0xa5, 0xab, 0xa8, 0xa6, 0xac, 0xa7, 0xa6, 0xac, + 0xa9, 0xa0, 0xa3, 0xa5, 0xa2, 0xae, 0xaf, 0xa9, + 0x0f, 0x03, 0x01, 0x0d, 0x08, 0x04, 0x0e, 0x07, + 0x06, 0x0f, 0x0b, 0x02, 0x03, 0x08, 0x04, 0x0e, + 0x09, 0x0c, 0x07, 0x00, 0x02, 0x01, 0x0d, 0x0a, + 0x0c, 0x06, 0x00, 0x09, 0x05, 0x0b, 0x0a, 0x05, + 0x00, 0x0d, 0x0e, 0x08, 0x07, 0x0a, 0x0b, 0x01, + 0x0a, 0x03, 0x04, 0x0f, 0x0d, 0x04, 0x01, 0x02, + 0x05, 0x0b, 0x08, 0x06, 0x0c, 0x07, 0x06, 0x0c, + 0x09, 0x00, 0x03, 0x05, 0x02, 0x0e, 0x0f, 0x09, + 0x5f, 0x53, 0x51, 0x5d, 0x58, 0x54, 0x5e, 0x57, + 0x56, 0x5f, 0x5b, 0x52, 0x53, 0x58, 0x54, 0x5e, + 0x59, 0x5c, 0x57, 0x50, 0x52, 0x51, 0x5d, 0x5a, + 0x5c, 0x56, 0x50, 0x59, 0x55, 0x5b, 0x5a, 0x55, + 0x50, 0x5d, 0x5e, 0x58, 0x57, 0x5a, 0x5b, 0x51, + 0x5a, 0x53, 0x54, 0x5f, 0x5d, 0x54, 0x51, 0x52, + 0x55, 0x5b, 0x58, 0x56, 0x5c, 0x57, 0x56, 0x5c, + 0x59, 0x50, 0x53, 0x55, 0x52, 0x5e, 0x5f, 0x59, + 0x6f, 0x63, 0x61, 0x6d, 0x68, 0x64, 0x6e, 0x67, + 0x66, 0x6f, 0x6b, 0x62, 0x63, 0x68, 0x64, 0x6e, + 0x69, 0x6c, 0x67, 0x60, 0x62, 0x61, 0x6d, 0x6a, + 0x6c, 0x66, 0x60, 0x69, 0x65, 0x6b, 0x6a, 0x65, + 0x60, 0x6d, 0x6e, 0x68, 0x67, 0x6a, 0x6b, 0x61, + 0x6a, 0x63, 0x64, 0x6f, 0x6d, 0x64, 0x61, 0x62, + 0x65, 0x6b, 0x68, 0x66, 0x6c, 0x67, 0x66, 0x6c, + 0x69, 0x60, 0x63, 0x65, 0x62, 0x6e, 0x6f, 0x69, + 0x0f, 0x03, 0x01, 0x0d, 0x08, 0x04, 0x0e, 0x07, + 0x06, 0x0f, 0x0b, 0x02, 0x03, 0x08, 0x04, 0x0e, + 0x09, 0x0c, 0x07, 0x00, 0x02, 0x01, 0x0d, 0x0a, + 0x0c, 0x06, 0x00, 0x09, 0x05, 0x0b, 0x0a, 0x05, + 0x00, 0x0d, 0x0e, 0x08, 0x07, 0x0a, 0x0b, 0x01, + 0x0a, 0x03, 0x04, 0x0f, 0x0d, 0x04, 0x01, 0x02, + 0x05, 0x0b, 0x08, 0x06, 0x0c, 0x07, 0x06, 0x0c, + 0x09, 0x00, 0x03, 0x05, 0x02, 0x0e, 0x0f, 0x09, + 0xdf, 0xd3, 0xd1, 0xdd, 0xd8, 0xd4, 0xde, 0xd7, + 0xd6, 0xdf, 0xdb, 0xd2, 0xd3, 0xd8, 0xd4, 0xde, + 0xd9, 0xdc, 0xd7, 0xd0, 0xd2, 0xd1, 0xdd, 0xda, + 0xdc, 0xd6, 0xd0, 0xd9, 0xd5, 0xdb, 0xda, 0xd5, + 0xd0, 0xdd, 0xde, 0xd8, 0xd7, 0xda, 0xdb, 0xd1, + 0xda, 0xd3, 0xd4, 0xdf, 0xdd, 0xd4, 0xd1, 0xd2, + 0xd5, 0xdb, 0xd8, 0xd6, 0xdc, 0xd7, 0xd6, 0xdc, + 0xd9, 0xd0, 0xd3, 0xd5, 0xd2, 0xde, 0xdf, 0xd9, + },{ + 0xa7, 0xad, 0xad, 0xa8, 0xae, 0xab, 0xa3, 0xa5, + 0xa0, 0xa6, 0xa6, 0xaf, 0xa9, 0xa0, 0xaa, 0xa3, + 0xa1, 0xa4, 0xa2, 0xa7, 0xa8, 0xa2, 0xa5, 0xac, + 0xab, 0xa1, 0xac, 0xaa, 0xa4, 0xae, 0xaf, 0xa9, + 0xaa, 0xa3, 0xa6, 0xaf, 0xa9, 0xa0, 0xa0, 0xa6, + 0xac, 0xaa, 0xab, 0xa1, 0xa7, 0xad, 0xad, 0xa8, + 0xaf, 0xa9, 0xa1, 0xa4, 0xa3, 0xa5, 0xae, 0xab, + 0xa5, 0xac, 0xa2, 0xa7, 0xa8, 0xa2, 0xa4, 0xae, + 0xd7, 0xdd, 0xdd, 0xd8, 0xde, 0xdb, 0xd3, 0xd5, + 0xd0, 0xd6, 0xd6, 0xdf, 0xd9, 0xd0, 0xda, 0xd3, + 0xd1, 0xd4, 0xd2, 0xd7, 0xd8, 0xd2, 0xd5, 0xdc, + 0xdb, 0xd1, 0xdc, 0xda, 0xd4, 0xde, 0xdf, 0xd9, + 0xda, 0xd3, 0xd6, 0xdf, 0xd9, 0xd0, 0xd0, 0xd6, + 0xdc, 0xda, 0xdb, 0xd1, 0xd7, 0xdd, 0xdd, 0xd8, + 0xdf, 0xd9, 0xd1, 0xd4, 0xd3, 0xd5, 0xde, 0xdb, + 0xd5, 0xdc, 0xd2, 0xd7, 0xd8, 0xd2, 0xd4, 0xde, + 0x07, 0x0d, 0x0d, 0x08, 0x0e, 0x0b, 0x03, 0x05, + 0x00, 0x06, 0x06, 0x0f, 0x09, 0x00, 0x0a, 0x03, + 0x01, 0x04, 0x02, 0x07, 0x08, 0x02, 0x05, 0x0c, + 0x0b, 0x01, 0x0c, 0x0a, 0x04, 0x0e, 0x0f, 0x09, + 0x0a, 0x03, 0x06, 0x0f, 0x09, 0x00, 0x00, 0x06, + 0x0c, 0x0a, 0x0b, 0x01, 0x07, 0x0d, 0x0d, 0x08, + 0x0f, 0x09, 0x01, 0x04, 0x03, 0x05, 0x0e, 0x0b, + 0x05, 0x0c, 0x02, 0x07, 0x08, 0x02, 0x04, 0x0e, + 0x77, 0x7d, 0x7d, 0x78, 0x7e, 0x7b, 0x73, 0x75, + 0x70, 0x76, 0x76, 0x7f, 0x79, 0x70, 0x7a, 0x73, + 0x71, 0x74, 0x72, 0x77, 0x78, 0x72, 0x75, 0x7c, + 0x7b, 0x71, 0x7c, 0x7a, 0x74, 0x7e, 0x7f, 0x79, + 0x7a, 0x73, 0x76, 0x7f, 0x79, 0x70, 0x70, 0x76, + 0x7c, 0x7a, 0x7b, 0x71, 0x77, 0x7d, 0x7d, 0x78, + 0x7f, 0x79, 0x71, 0x74, 0x73, 0x75, 0x7e, 0x7b, + 0x75, 0x7c, 0x72, 0x77, 0x78, 0x72, 0x74, 0x7e, + 0x97, 0x9d, 0x9d, 0x98, 0x9e, 0x9b, 0x93, 0x95, + 0x90, 0x96, 0x96, 0x9f, 0x99, 0x90, 0x9a, 0x93, + 0x91, 0x94, 0x92, 0x97, 0x98, 0x92, 0x95, 0x9c, + 0x9b, 0x91, 0x9c, 0x9a, 0x94, 0x9e, 0x9f, 0x99, + 0x9a, 0x93, 0x96, 0x9f, 0x99, 0x90, 0x90, 0x96, + 0x9c, 0x9a, 0x9b, 0x91, 0x97, 0x9d, 0x9d, 0x98, + 0x9f, 0x99, 0x91, 0x94, 0x93, 0x95, 0x9e, 0x9b, + 0x95, 0x9c, 0x92, 0x97, 0x98, 0x92, 0x94, 0x9e, + 0x07, 0x0d, 0x0d, 0x08, 0x0e, 0x0b, 0x03, 0x05, + 0x00, 0x06, 0x06, 0x0f, 0x09, 0x00, 0x0a, 0x03, + 0x01, 0x04, 0x02, 0x07, 0x08, 0x02, 0x05, 0x0c, + 0x0b, 0x01, 0x0c, 0x0a, 0x04, 0x0e, 0x0f, 0x09, + 0x0a, 0x03, 0x06, 0x0f, 0x09, 0x00, 0x00, 0x06, + 0x0c, 0x0a, 0x0b, 0x01, 0x07, 0x0d, 0x0d, 0x08, + 0x0f, 0x09, 0x01, 0x04, 0x03, 0x05, 0x0e, 0x0b, + 0x05, 0x0c, 0x02, 0x07, 0x08, 0x02, 0x04, 0x0e, + 0xe7, 0xed, 0xed, 0xe8, 0xee, 0xeb, 0xe3, 0xe5, + 0xe0, 0xe6, 0xe6, 0xef, 0xe9, 0xe0, 0xea, 0xe3, + 0xe1, 0xe4, 0xe2, 0xe7, 0xe8, 0xe2, 0xe5, 0xec, + 0xeb, 0xe1, 0xec, 0xea, 0xe4, 0xee, 0xef, 0xe9, + 0xea, 0xe3, 0xe6, 0xef, 0xe9, 0xe0, 0xe0, 0xe6, + 0xec, 0xea, 0xeb, 0xe1, 0xe7, 0xed, 0xed, 0xe8, + 0xef, 0xe9, 0xe1, 0xe4, 0xe3, 0xe5, 0xee, 0xeb, + 0xe5, 0xec, 0xe2, 0xe7, 0xe8, 0xe2, 0xe4, 0xee, + 0x97, 0x9d, 0x9d, 0x98, 0x9e, 0x9b, 0x93, 0x95, + 0x90, 0x96, 0x96, 0x9f, 0x99, 0x90, 0x9a, 0x93, + 0x91, 0x94, 0x92, 0x97, 0x98, 0x92, 0x95, 0x9c, + 0x9b, 0x91, 0x9c, 0x9a, 0x94, 0x9e, 0x9f, 0x99, + 0x9a, 0x93, 0x96, 0x9f, 0x99, 0x90, 0x90, 0x96, + 0x9c, 0x9a, 0x9b, 0x91, 0x97, 0x9d, 0x9d, 0x98, + 0x9f, 0x99, 0x91, 0x94, 0x93, 0x95, 0x9e, 0x9b, + 0x95, 0x9c, 0x92, 0x97, 0x98, 0x92, 0x94, 0x9e, + 0x67, 0x6d, 0x6d, 0x68, 0x6e, 0x6b, 0x63, 0x65, + 0x60, 0x66, 0x66, 0x6f, 0x69, 0x60, 0x6a, 0x63, + 0x61, 0x64, 0x62, 0x67, 0x68, 0x62, 0x65, 0x6c, + 0x6b, 0x61, 0x6c, 0x6a, 0x64, 0x6e, 0x6f, 0x69, + 0x6a, 0x63, 0x66, 0x6f, 0x69, 0x60, 0x60, 0x66, + 0x6c, 0x6a, 0x6b, 0x61, 0x67, 0x6d, 0x6d, 0x68, + 0x6f, 0x69, 0x61, 0x64, 0x63, 0x65, 0x6e, 0x6b, + 0x65, 0x6c, 0x62, 0x67, 0x68, 0x62, 0x64, 0x6e, + 0x37, 0x3d, 0x3d, 0x38, 0x3e, 0x3b, 0x33, 0x35, + 0x30, 0x36, 0x36, 0x3f, 0x39, 0x30, 0x3a, 0x33, + 0x31, 0x34, 0x32, 0x37, 0x38, 0x32, 0x35, 0x3c, + 0x3b, 0x31, 0x3c, 0x3a, 0x34, 0x3e, 0x3f, 0x39, + 0x3a, 0x33, 0x36, 0x3f, 0x39, 0x30, 0x30, 0x36, + 0x3c, 0x3a, 0x3b, 0x31, 0x37, 0x3d, 0x3d, 0x38, + 0x3f, 0x39, 0x31, 0x34, 0x33, 0x35, 0x3e, 0x3b, + 0x35, 0x3c, 0x32, 0x37, 0x38, 0x32, 0x34, 0x3e, + 0x37, 0x3d, 0x3d, 0x38, 0x3e, 0x3b, 0x33, 0x35, + 0x30, 0x36, 0x36, 0x3f, 0x39, 0x30, 0x3a, 0x33, + 0x31, 0x34, 0x32, 0x37, 0x38, 0x32, 0x35, 0x3c, + 0x3b, 0x31, 0x3c, 0x3a, 0x34, 0x3e, 0x3f, 0x39, + 0x3a, 0x33, 0x36, 0x3f, 0x39, 0x30, 0x30, 0x36, + 0x3c, 0x3a, 0x3b, 0x31, 0x37, 0x3d, 0x3d, 0x38, + 0x3f, 0x39, 0x31, 0x34, 0x33, 0x35, 0x3e, 0x3b, + 0x35, 0x3c, 0x32, 0x37, 0x38, 0x32, 0x34, 0x3e, + 0x47, 0x4d, 0x4d, 0x48, 0x4e, 0x4b, 0x43, 0x45, + 0x40, 0x46, 0x46, 0x4f, 0x49, 0x40, 0x4a, 0x43, + 0x41, 0x44, 0x42, 0x47, 0x48, 0x42, 0x45, 0x4c, + 0x4b, 0x41, 0x4c, 0x4a, 0x44, 0x4e, 0x4f, 0x49, + 0x4a, 0x43, 0x46, 0x4f, 0x49, 0x40, 0x40, 0x46, + 0x4c, 0x4a, 0x4b, 0x41, 0x47, 0x4d, 0x4d, 0x48, + 0x4f, 0x49, 0x41, 0x44, 0x43, 0x45, 0x4e, 0x4b, + 0x45, 0x4c, 0x42, 0x47, 0x48, 0x42, 0x44, 0x4e, + 0xf7, 0xfd, 0xfd, 0xf8, 0xfe, 0xfb, 0xf3, 0xf5, + 0xf0, 0xf6, 0xf6, 0xff, 0xf9, 0xf0, 0xfa, 0xf3, + 0xf1, 0xf4, 0xf2, 0xf7, 0xf8, 0xf2, 0xf5, 0xfc, + 0xfb, 0xf1, 0xfc, 0xfa, 0xf4, 0xfe, 0xff, 0xf9, + 0xfa, 0xf3, 0xf6, 0xff, 0xf9, 0xf0, 0xf0, 0xf6, + 0xfc, 0xfa, 0xfb, 0xf1, 0xf7, 0xfd, 0xfd, 0xf8, + 0xff, 0xf9, 0xf1, 0xf4, 0xf3, 0xf5, 0xfe, 0xfb, + 0xf5, 0xfc, 0xf2, 0xf7, 0xf8, 0xf2, 0xf4, 0xfe, + 0x67, 0x6d, 0x6d, 0x68, 0x6e, 0x6b, 0x63, 0x65, + 0x60, 0x66, 0x66, 0x6f, 0x69, 0x60, 0x6a, 0x63, + 0x61, 0x64, 0x62, 0x67, 0x68, 0x62, 0x65, 0x6c, + 0x6b, 0x61, 0x6c, 0x6a, 0x64, 0x6e, 0x6f, 0x69, + 0x6a, 0x63, 0x66, 0x6f, 0x69, 0x60, 0x60, 0x66, + 0x6c, 0x6a, 0x6b, 0x61, 0x67, 0x6d, 0x6d, 0x68, + 0x6f, 0x69, 0x61, 0x64, 0x63, 0x65, 0x6e, 0x6b, + 0x65, 0x6c, 0x62, 0x67, 0x68, 0x62, 0x64, 0x6e, + 0x57, 0x5d, 0x5d, 0x58, 0x5e, 0x5b, 0x53, 0x55, + 0x50, 0x56, 0x56, 0x5f, 0x59, 0x50, 0x5a, 0x53, + 0x51, 0x54, 0x52, 0x57, 0x58, 0x52, 0x55, 0x5c, + 0x5b, 0x51, 0x5c, 0x5a, 0x54, 0x5e, 0x5f, 0x59, + 0x5a, 0x53, 0x56, 0x5f, 0x59, 0x50, 0x50, 0x56, + 0x5c, 0x5a, 0x5b, 0x51, 0x57, 0x5d, 0x5d, 0x58, + 0x5f, 0x59, 0x51, 0x54, 0x53, 0x55, 0x5e, 0x5b, + 0x55, 0x5c, 0x52, 0x57, 0x58, 0x52, 0x54, 0x5e, + 0xa7, 0xad, 0xad, 0xa8, 0xae, 0xab, 0xa3, 0xa5, + 0xa0, 0xa6, 0xa6, 0xaf, 0xa9, 0xa0, 0xaa, 0xa3, + 0xa1, 0xa4, 0xa2, 0xa7, 0xa8, 0xa2, 0xa5, 0xac, + 0xab, 0xa1, 0xac, 0xaa, 0xa4, 0xae, 0xaf, 0xa9, + 0xaa, 0xa3, 0xa6, 0xaf, 0xa9, 0xa0, 0xa0, 0xa6, + 0xac, 0xaa, 0xab, 0xa1, 0xa7, 0xad, 0xad, 0xa8, + 0xaf, 0xa9, 0xa1, 0xa4, 0xa3, 0xa5, 0xae, 0xab, + 0xa5, 0xac, 0xa2, 0xa7, 0xa8, 0xa2, 0xa4, 0xae, + 0x17, 0x1d, 0x1d, 0x18, 0x1e, 0x1b, 0x13, 0x15, + 0x10, 0x16, 0x16, 0x1f, 0x19, 0x10, 0x1a, 0x13, + 0x11, 0x14, 0x12, 0x17, 0x18, 0x12, 0x15, 0x1c, + 0x1b, 0x11, 0x1c, 0x1a, 0x14, 0x1e, 0x1f, 0x19, + 0x1a, 0x13, 0x16, 0x1f, 0x19, 0x10, 0x10, 0x16, + 0x1c, 0x1a, 0x1b, 0x11, 0x17, 0x1d, 0x1d, 0x18, + 0x1f, 0x19, 0x11, 0x14, 0x13, 0x15, 0x1e, 0x1b, + 0x15, 0x1c, 0x12, 0x17, 0x18, 0x12, 0x14, 0x1e, + 0x27, 0x2d, 0x2d, 0x28, 0x2e, 0x2b, 0x23, 0x25, + 0x20, 0x26, 0x26, 0x2f, 0x29, 0x20, 0x2a, 0x23, + 0x21, 0x24, 0x22, 0x27, 0x28, 0x22, 0x25, 0x2c, + 0x2b, 0x21, 0x2c, 0x2a, 0x24, 0x2e, 0x2f, 0x29, + 0x2a, 0x23, 0x26, 0x2f, 0x29, 0x20, 0x20, 0x26, + 0x2c, 0x2a, 0x2b, 0x21, 0x27, 0x2d, 0x2d, 0x28, + 0x2f, 0x29, 0x21, 0x24, 0x23, 0x25, 0x2e, 0x2b, + 0x25, 0x2c, 0x22, 0x27, 0x28, 0x22, 0x24, 0x2e, + 0xd7, 0xdd, 0xdd, 0xd8, 0xde, 0xdb, 0xd3, 0xd5, + 0xd0, 0xd6, 0xd6, 0xdf, 0xd9, 0xd0, 0xda, 0xd3, + 0xd1, 0xd4, 0xd2, 0xd7, 0xd8, 0xd2, 0xd5, 0xdc, + 0xdb, 0xd1, 0xdc, 0xda, 0xd4, 0xde, 0xdf, 0xd9, + 0xda, 0xd3, 0xd6, 0xdf, 0xd9, 0xd0, 0xd0, 0xd6, + 0xdc, 0xda, 0xdb, 0xd1, 0xd7, 0xdd, 0xdd, 0xd8, + 0xdf, 0xd9, 0xd1, 0xd4, 0xd3, 0xd5, 0xde, 0xdb, + 0xd5, 0xdc, 0xd2, 0xd7, 0xd8, 0xd2, 0xd4, 0xde, + 0x87, 0x8d, 0x8d, 0x88, 0x8e, 0x8b, 0x83, 0x85, + 0x80, 0x86, 0x86, 0x8f, 0x89, 0x80, 0x8a, 0x83, + 0x81, 0x84, 0x82, 0x87, 0x88, 0x82, 0x85, 0x8c, + 0x8b, 0x81, 0x8c, 0x8a, 0x84, 0x8e, 0x8f, 0x89, + 0x8a, 0x83, 0x86, 0x8f, 0x89, 0x80, 0x80, 0x86, + 0x8c, 0x8a, 0x8b, 0x81, 0x87, 0x8d, 0x8d, 0x88, + 0x8f, 0x89, 0x81, 0x84, 0x83, 0x85, 0x8e, 0x8b, + 0x85, 0x8c, 0x82, 0x87, 0x88, 0x82, 0x84, 0x8e, + 0xc7, 0xcd, 0xcd, 0xc8, 0xce, 0xcb, 0xc3, 0xc5, + 0xc0, 0xc6, 0xc6, 0xcf, 0xc9, 0xc0, 0xca, 0xc3, + 0xc1, 0xc4, 0xc2, 0xc7, 0xc8, 0xc2, 0xc5, 0xcc, + 0xcb, 0xc1, 0xcc, 0xca, 0xc4, 0xce, 0xcf, 0xc9, + 0xca, 0xc3, 0xc6, 0xcf, 0xc9, 0xc0, 0xc0, 0xc6, + 0xcc, 0xca, 0xcb, 0xc1, 0xc7, 0xcd, 0xcd, 0xc8, + 0xcf, 0xc9, 0xc1, 0xc4, 0xc3, 0xc5, 0xce, 0xcb, + 0xc5, 0xcc, 0xc2, 0xc7, 0xc8, 0xc2, 0xc4, 0xce, + 0x57, 0x5d, 0x5d, 0x58, 0x5e, 0x5b, 0x53, 0x55, + 0x50, 0x56, 0x56, 0x5f, 0x59, 0x50, 0x5a, 0x53, + 0x51, 0x54, 0x52, 0x57, 0x58, 0x52, 0x55, 0x5c, + 0x5b, 0x51, 0x5c, 0x5a, 0x54, 0x5e, 0x5f, 0x59, + 0x5a, 0x53, 0x56, 0x5f, 0x59, 0x50, 0x50, 0x56, + 0x5c, 0x5a, 0x5b, 0x51, 0x57, 0x5d, 0x5d, 0x58, + 0x5f, 0x59, 0x51, 0x54, 0x53, 0x55, 0x5e, 0x5b, + 0x55, 0x5c, 0x52, 0x57, 0x58, 0x52, 0x54, 0x5e, + 0x77, 0x7d, 0x7d, 0x78, 0x7e, 0x7b, 0x73, 0x75, + 0x70, 0x76, 0x76, 0x7f, 0x79, 0x70, 0x7a, 0x73, + 0x71, 0x74, 0x72, 0x77, 0x78, 0x72, 0x75, 0x7c, + 0x7b, 0x71, 0x7c, 0x7a, 0x74, 0x7e, 0x7f, 0x79, + 0x7a, 0x73, 0x76, 0x7f, 0x79, 0x70, 0x70, 0x76, + 0x7c, 0x7a, 0x7b, 0x71, 0x77, 0x7d, 0x7d, 0x78, + 0x7f, 0x79, 0x71, 0x74, 0x73, 0x75, 0x7e, 0x7b, + 0x75, 0x7c, 0x72, 0x77, 0x78, 0x72, 0x74, 0x7e, + 0xe7, 0xed, 0xed, 0xe8, 0xee, 0xeb, 0xe3, 0xe5, + 0xe0, 0xe6, 0xe6, 0xef, 0xe9, 0xe0, 0xea, 0xe3, + 0xe1, 0xe4, 0xe2, 0xe7, 0xe8, 0xe2, 0xe5, 0xec, + 0xeb, 0xe1, 0xec, 0xea, 0xe4, 0xee, 0xef, 0xe9, + 0xea, 0xe3, 0xe6, 0xef, 0xe9, 0xe0, 0xe0, 0xe6, + 0xec, 0xea, 0xeb, 0xe1, 0xe7, 0xed, 0xed, 0xe8, + 0xef, 0xe9, 0xe1, 0xe4, 0xe3, 0xe5, 0xee, 0xeb, + 0xe5, 0xec, 0xe2, 0xe7, 0xe8, 0xe2, 0xe4, 0xee, + 0xb7, 0xbd, 0xbd, 0xb8, 0xbe, 0xbb, 0xb3, 0xb5, + 0xb0, 0xb6, 0xb6, 0xbf, 0xb9, 0xb0, 0xba, 0xb3, + 0xb1, 0xb4, 0xb2, 0xb7, 0xb8, 0xb2, 0xb5, 0xbc, + 0xbb, 0xb1, 0xbc, 0xba, 0xb4, 0xbe, 0xbf, 0xb9, + 0xba, 0xb3, 0xb6, 0xbf, 0xb9, 0xb0, 0xb0, 0xb6, + 0xbc, 0xba, 0xbb, 0xb1, 0xb7, 0xbd, 0xbd, 0xb8, + 0xbf, 0xb9, 0xb1, 0xb4, 0xb3, 0xb5, 0xbe, 0xbb, + 0xb5, 0xbc, 0xb2, 0xb7, 0xb8, 0xb2, 0xb4, 0xbe, + 0xc7, 0xcd, 0xcd, 0xc8, 0xce, 0xcb, 0xc3, 0xc5, + 0xc0, 0xc6, 0xc6, 0xcf, 0xc9, 0xc0, 0xca, 0xc3, + 0xc1, 0xc4, 0xc2, 0xc7, 0xc8, 0xc2, 0xc5, 0xcc, + 0xcb, 0xc1, 0xcc, 0xca, 0xc4, 0xce, 0xcf, 0xc9, + 0xca, 0xc3, 0xc6, 0xcf, 0xc9, 0xc0, 0xc0, 0xc6, + 0xcc, 0xca, 0xcb, 0xc1, 0xc7, 0xcd, 0xcd, 0xc8, + 0xcf, 0xc9, 0xc1, 0xc4, 0xc3, 0xc5, 0xce, 0xcb, + 0xc5, 0xcc, 0xc2, 0xc7, 0xc8, 0xc2, 0xc4, 0xce, + 0x47, 0x4d, 0x4d, 0x48, 0x4e, 0x4b, 0x43, 0x45, + 0x40, 0x46, 0x46, 0x4f, 0x49, 0x40, 0x4a, 0x43, + 0x41, 0x44, 0x42, 0x47, 0x48, 0x42, 0x45, 0x4c, + 0x4b, 0x41, 0x4c, 0x4a, 0x44, 0x4e, 0x4f, 0x49, + 0x4a, 0x43, 0x46, 0x4f, 0x49, 0x40, 0x40, 0x46, + 0x4c, 0x4a, 0x4b, 0x41, 0x47, 0x4d, 0x4d, 0x48, + 0x4f, 0x49, 0x41, 0x44, 0x43, 0x45, 0x4e, 0x4b, + 0x45, 0x4c, 0x42, 0x47, 0x48, 0x42, 0x44, 0x4e, + 0xb7, 0xbd, 0xbd, 0xb8, 0xbe, 0xbb, 0xb3, 0xb5, + 0xb0, 0xb6, 0xb6, 0xbf, 0xb9, 0xb0, 0xba, 0xb3, + 0xb1, 0xb4, 0xb2, 0xb7, 0xb8, 0xb2, 0xb5, 0xbc, + 0xbb, 0xb1, 0xbc, 0xba, 0xb4, 0xbe, 0xbf, 0xb9, + 0xba, 0xb3, 0xb6, 0xbf, 0xb9, 0xb0, 0xb0, 0xb6, + 0xbc, 0xba, 0xbb, 0xb1, 0xb7, 0xbd, 0xbd, 0xb8, + 0xbf, 0xb9, 0xb1, 0xb4, 0xb3, 0xb5, 0xbe, 0xbb, + 0xb5, 0xbc, 0xb2, 0xb7, 0xb8, 0xb2, 0xb4, 0xbe, + 0x27, 0x2d, 0x2d, 0x28, 0x2e, 0x2b, 0x23, 0x25, + 0x20, 0x26, 0x26, 0x2f, 0x29, 0x20, 0x2a, 0x23, + 0x21, 0x24, 0x22, 0x27, 0x28, 0x22, 0x25, 0x2c, + 0x2b, 0x21, 0x2c, 0x2a, 0x24, 0x2e, 0x2f, 0x29, + 0x2a, 0x23, 0x26, 0x2f, 0x29, 0x20, 0x20, 0x26, + 0x2c, 0x2a, 0x2b, 0x21, 0x27, 0x2d, 0x2d, 0x28, + 0x2f, 0x29, 0x21, 0x24, 0x23, 0x25, 0x2e, 0x2b, + 0x25, 0x2c, 0x22, 0x27, 0x28, 0x22, 0x24, 0x2e, + 0xf7, 0xfd, 0xfd, 0xf8, 0xfe, 0xfb, 0xf3, 0xf5, + 0xf0, 0xf6, 0xf6, 0xff, 0xf9, 0xf0, 0xfa, 0xf3, + 0xf1, 0xf4, 0xf2, 0xf7, 0xf8, 0xf2, 0xf5, 0xfc, + 0xfb, 0xf1, 0xfc, 0xfa, 0xf4, 0xfe, 0xff, 0xf9, + 0xfa, 0xf3, 0xf6, 0xff, 0xf9, 0xf0, 0xf0, 0xf6, + 0xfc, 0xfa, 0xfb, 0xf1, 0xf7, 0xfd, 0xfd, 0xf8, + 0xff, 0xf9, 0xf1, 0xf4, 0xf3, 0xf5, 0xfe, 0xfb, + 0xf5, 0xfc, 0xf2, 0xf7, 0xf8, 0xf2, 0xf4, 0xfe, + 0x87, 0x8d, 0x8d, 0x88, 0x8e, 0x8b, 0x83, 0x85, + 0x80, 0x86, 0x86, 0x8f, 0x89, 0x80, 0x8a, 0x83, + 0x81, 0x84, 0x82, 0x87, 0x88, 0x82, 0x85, 0x8c, + 0x8b, 0x81, 0x8c, 0x8a, 0x84, 0x8e, 0x8f, 0x89, + 0x8a, 0x83, 0x86, 0x8f, 0x89, 0x80, 0x80, 0x86, + 0x8c, 0x8a, 0x8b, 0x81, 0x87, 0x8d, 0x8d, 0x88, + 0x8f, 0x89, 0x81, 0x84, 0x83, 0x85, 0x8e, 0x8b, + 0x85, 0x8c, 0x82, 0x87, 0x88, 0x82, 0x84, 0x8e, + 0x17, 0x1d, 0x1d, 0x18, 0x1e, 0x1b, 0x13, 0x15, + 0x10, 0x16, 0x16, 0x1f, 0x19, 0x10, 0x1a, 0x13, + 0x11, 0x14, 0x12, 0x17, 0x18, 0x12, 0x15, 0x1c, + 0x1b, 0x11, 0x1c, 0x1a, 0x14, 0x1e, 0x1f, 0x19, + 0x1a, 0x13, 0x16, 0x1f, 0x19, 0x10, 0x10, 0x16, + 0x1c, 0x1a, 0x1b, 0x11, 0x17, 0x1d, 0x1d, 0x18, + 0x1f, 0x19, 0x11, 0x14, 0x13, 0x15, 0x1e, 0x1b, + 0x15, 0x1c, 0x12, 0x17, 0x18, 0x12, 0x14, 0x1e, + 0xd7, 0xdd, 0xdd, 0xd8, 0xde, 0xdb, 0xd3, 0xd5, + 0xd0, 0xd6, 0xd6, 0xdf, 0xd9, 0xd0, 0xda, 0xd3, + 0xd1, 0xd4, 0xd2, 0xd7, 0xd8, 0xd2, 0xd5, 0xdc, + 0xdb, 0xd1, 0xdc, 0xda, 0xd4, 0xde, 0xdf, 0xd9, + 0xda, 0xd3, 0xd6, 0xdf, 0xd9, 0xd0, 0xd0, 0xd6, + 0xdc, 0xda, 0xdb, 0xd1, 0xd7, 0xdd, 0xdd, 0xd8, + 0xdf, 0xd9, 0xd1, 0xd4, 0xd3, 0xd5, 0xde, 0xdb, + 0xd5, 0xdc, 0xd2, 0xd7, 0xd8, 0xd2, 0xd4, 0xde, + 0x17, 0x1d, 0x1d, 0x18, 0x1e, 0x1b, 0x13, 0x15, + 0x10, 0x16, 0x16, 0x1f, 0x19, 0x10, 0x1a, 0x13, + 0x11, 0x14, 0x12, 0x17, 0x18, 0x12, 0x15, 0x1c, + 0x1b, 0x11, 0x1c, 0x1a, 0x14, 0x1e, 0x1f, 0x19, + 0x1a, 0x13, 0x16, 0x1f, 0x19, 0x10, 0x10, 0x16, + 0x1c, 0x1a, 0x1b, 0x11, 0x17, 0x1d, 0x1d, 0x18, + 0x1f, 0x19, 0x11, 0x14, 0x13, 0x15, 0x1e, 0x1b, + 0x15, 0x1c, 0x12, 0x17, 0x18, 0x12, 0x14, 0x1e, + 0x67, 0x6d, 0x6d, 0x68, 0x6e, 0x6b, 0x63, 0x65, + 0x60, 0x66, 0x66, 0x6f, 0x69, 0x60, 0x6a, 0x63, + 0x61, 0x64, 0x62, 0x67, 0x68, 0x62, 0x65, 0x6c, + 0x6b, 0x61, 0x6c, 0x6a, 0x64, 0x6e, 0x6f, 0x69, + 0x6a, 0x63, 0x66, 0x6f, 0x69, 0x60, 0x60, 0x66, + 0x6c, 0x6a, 0x6b, 0x61, 0x67, 0x6d, 0x6d, 0x68, + 0x6f, 0x69, 0x61, 0x64, 0x63, 0x65, 0x6e, 0x6b, + 0x65, 0x6c, 0x62, 0x67, 0x68, 0x62, 0x64, 0x6e, + 0xa7, 0xad, 0xad, 0xa8, 0xae, 0xab, 0xa3, 0xa5, + 0xa0, 0xa6, 0xa6, 0xaf, 0xa9, 0xa0, 0xaa, 0xa3, + 0xa1, 0xa4, 0xa2, 0xa7, 0xa8, 0xa2, 0xa5, 0xac, + 0xab, 0xa1, 0xac, 0xaa, 0xa4, 0xae, 0xaf, 0xa9, + 0xaa, 0xa3, 0xa6, 0xaf, 0xa9, 0xa0, 0xa0, 0xa6, + 0xac, 0xaa, 0xab, 0xa1, 0xa7, 0xad, 0xad, 0xa8, + 0xaf, 0xa9, 0xa1, 0xa4, 0xa3, 0xa5, 0xae, 0xab, + 0xa5, 0xac, 0xa2, 0xa7, 0xa8, 0xa2, 0xa4, 0xae, + 0x47, 0x4d, 0x4d, 0x48, 0x4e, 0x4b, 0x43, 0x45, + 0x40, 0x46, 0x46, 0x4f, 0x49, 0x40, 0x4a, 0x43, + 0x41, 0x44, 0x42, 0x47, 0x48, 0x42, 0x45, 0x4c, + 0x4b, 0x41, 0x4c, 0x4a, 0x44, 0x4e, 0x4f, 0x49, + 0x4a, 0x43, 0x46, 0x4f, 0x49, 0x40, 0x40, 0x46, + 0x4c, 0x4a, 0x4b, 0x41, 0x47, 0x4d, 0x4d, 0x48, + 0x4f, 0x49, 0x41, 0x44, 0x43, 0x45, 0x4e, 0x4b, + 0x45, 0x4c, 0x42, 0x47, 0x48, 0x42, 0x44, 0x4e, + 0xd7, 0xdd, 0xdd, 0xd8, 0xde, 0xdb, 0xd3, 0xd5, + 0xd0, 0xd6, 0xd6, 0xdf, 0xd9, 0xd0, 0xda, 0xd3, + 0xd1, 0xd4, 0xd2, 0xd7, 0xd8, 0xd2, 0xd5, 0xdc, + 0xdb, 0xd1, 0xdc, 0xda, 0xd4, 0xde, 0xdf, 0xd9, + 0xda, 0xd3, 0xd6, 0xdf, 0xd9, 0xd0, 0xd0, 0xd6, + 0xdc, 0xda, 0xdb, 0xd1, 0xd7, 0xdd, 0xdd, 0xd8, + 0xdf, 0xd9, 0xd1, 0xd4, 0xd3, 0xd5, 0xde, 0xdb, + 0xd5, 0xdc, 0xd2, 0xd7, 0xd8, 0xd2, 0xd4, 0xde, + 0x97, 0x9d, 0x9d, 0x98, 0x9e, 0x9b, 0x93, 0x95, + 0x90, 0x96, 0x96, 0x9f, 0x99, 0x90, 0x9a, 0x93, + 0x91, 0x94, 0x92, 0x97, 0x98, 0x92, 0x95, 0x9c, + 0x9b, 0x91, 0x9c, 0x9a, 0x94, 0x9e, 0x9f, 0x99, + 0x9a, 0x93, 0x96, 0x9f, 0x99, 0x90, 0x90, 0x96, + 0x9c, 0x9a, 0x9b, 0x91, 0x97, 0x9d, 0x9d, 0x98, + 0x9f, 0x99, 0x91, 0x94, 0x93, 0x95, 0x9e, 0x9b, + 0x95, 0x9c, 0x92, 0x97, 0x98, 0x92, 0x94, 0x9e, + 0x07, 0x0d, 0x0d, 0x08, 0x0e, 0x0b, 0x03, 0x05, + 0x00, 0x06, 0x06, 0x0f, 0x09, 0x00, 0x0a, 0x03, + 0x01, 0x04, 0x02, 0x07, 0x08, 0x02, 0x05, 0x0c, + 0x0b, 0x01, 0x0c, 0x0a, 0x04, 0x0e, 0x0f, 0x09, + 0x0a, 0x03, 0x06, 0x0f, 0x09, 0x00, 0x00, 0x06, + 0x0c, 0x0a, 0x0b, 0x01, 0x07, 0x0d, 0x0d, 0x08, + 0x0f, 0x09, 0x01, 0x04, 0x03, 0x05, 0x0e, 0x0b, + 0x05, 0x0c, 0x02, 0x07, 0x08, 0x02, 0x04, 0x0e, + 0x87, 0x8d, 0x8d, 0x88, 0x8e, 0x8b, 0x83, 0x85, + 0x80, 0x86, 0x86, 0x8f, 0x89, 0x80, 0x8a, 0x83, + 0x81, 0x84, 0x82, 0x87, 0x88, 0x82, 0x85, 0x8c, + 0x8b, 0x81, 0x8c, 0x8a, 0x84, 0x8e, 0x8f, 0x89, + 0x8a, 0x83, 0x86, 0x8f, 0x89, 0x80, 0x80, 0x86, + 0x8c, 0x8a, 0x8b, 0x81, 0x87, 0x8d, 0x8d, 0x88, + 0x8f, 0x89, 0x81, 0x84, 0x83, 0x85, 0x8e, 0x8b, + 0x85, 0x8c, 0x82, 0x87, 0x88, 0x82, 0x84, 0x8e, + 0x67, 0x6d, 0x6d, 0x68, 0x6e, 0x6b, 0x63, 0x65, + 0x60, 0x66, 0x66, 0x6f, 0x69, 0x60, 0x6a, 0x63, + 0x61, 0x64, 0x62, 0x67, 0x68, 0x62, 0x65, 0x6c, + 0x6b, 0x61, 0x6c, 0x6a, 0x64, 0x6e, 0x6f, 0x69, + 0x6a, 0x63, 0x66, 0x6f, 0x69, 0x60, 0x60, 0x66, + 0x6c, 0x6a, 0x6b, 0x61, 0x67, 0x6d, 0x6d, 0x68, + 0x6f, 0x69, 0x61, 0x64, 0x63, 0x65, 0x6e, 0x6b, + 0x65, 0x6c, 0x62, 0x67, 0x68, 0x62, 0x64, 0x6e, + 0xf7, 0xfd, 0xfd, 0xf8, 0xfe, 0xfb, 0xf3, 0xf5, + 0xf0, 0xf6, 0xf6, 0xff, 0xf9, 0xf0, 0xfa, 0xf3, + 0xf1, 0xf4, 0xf2, 0xf7, 0xf8, 0xf2, 0xf5, 0xfc, + 0xfb, 0xf1, 0xfc, 0xfa, 0xf4, 0xfe, 0xff, 0xf9, + 0xfa, 0xf3, 0xf6, 0xff, 0xf9, 0xf0, 0xf0, 0xf6, + 0xfc, 0xfa, 0xfb, 0xf1, 0xf7, 0xfd, 0xfd, 0xf8, + 0xff, 0xf9, 0xf1, 0xf4, 0xf3, 0xf5, 0xfe, 0xfb, + 0xf5, 0xfc, 0xf2, 0xf7, 0xf8, 0xf2, 0xf4, 0xfe, + 0x97, 0x9d, 0x9d, 0x98, 0x9e, 0x9b, 0x93, 0x95, + 0x90, 0x96, 0x96, 0x9f, 0x99, 0x90, 0x9a, 0x93, + 0x91, 0x94, 0x92, 0x97, 0x98, 0x92, 0x95, 0x9c, + 0x9b, 0x91, 0x9c, 0x9a, 0x94, 0x9e, 0x9f, 0x99, + 0x9a, 0x93, 0x96, 0x9f, 0x99, 0x90, 0x90, 0x96, + 0x9c, 0x9a, 0x9b, 0x91, 0x97, 0x9d, 0x9d, 0x98, + 0x9f, 0x99, 0x91, 0x94, 0x93, 0x95, 0x9e, 0x9b, + 0x95, 0x9c, 0x92, 0x97, 0x98, 0x92, 0x94, 0x9e, + 0x37, 0x3d, 0x3d, 0x38, 0x3e, 0x3b, 0x33, 0x35, + 0x30, 0x36, 0x36, 0x3f, 0x39, 0x30, 0x3a, 0x33, + 0x31, 0x34, 0x32, 0x37, 0x38, 0x32, 0x35, 0x3c, + 0x3b, 0x31, 0x3c, 0x3a, 0x34, 0x3e, 0x3f, 0x39, + 0x3a, 0x33, 0x36, 0x3f, 0x39, 0x30, 0x30, 0x36, + 0x3c, 0x3a, 0x3b, 0x31, 0x37, 0x3d, 0x3d, 0x38, + 0x3f, 0x39, 0x31, 0x34, 0x33, 0x35, 0x3e, 0x3b, + 0x35, 0x3c, 0x32, 0x37, 0x38, 0x32, 0x34, 0x3e, + 0x87, 0x8d, 0x8d, 0x88, 0x8e, 0x8b, 0x83, 0x85, + 0x80, 0x86, 0x86, 0x8f, 0x89, 0x80, 0x8a, 0x83, + 0x81, 0x84, 0x82, 0x87, 0x88, 0x82, 0x85, 0x8c, + 0x8b, 0x81, 0x8c, 0x8a, 0x84, 0x8e, 0x8f, 0x89, + 0x8a, 0x83, 0x86, 0x8f, 0x89, 0x80, 0x80, 0x86, + 0x8c, 0x8a, 0x8b, 0x81, 0x87, 0x8d, 0x8d, 0x88, + 0x8f, 0x89, 0x81, 0x84, 0x83, 0x85, 0x8e, 0x8b, + 0x85, 0x8c, 0x82, 0x87, 0x88, 0x82, 0x84, 0x8e, + 0x07, 0x0d, 0x0d, 0x08, 0x0e, 0x0b, 0x03, 0x05, + 0x00, 0x06, 0x06, 0x0f, 0x09, 0x00, 0x0a, 0x03, + 0x01, 0x04, 0x02, 0x07, 0x08, 0x02, 0x05, 0x0c, + 0x0b, 0x01, 0x0c, 0x0a, 0x04, 0x0e, 0x0f, 0x09, + 0x0a, 0x03, 0x06, 0x0f, 0x09, 0x00, 0x00, 0x06, + 0x0c, 0x0a, 0x0b, 0x01, 0x07, 0x0d, 0x0d, 0x08, + 0x0f, 0x09, 0x01, 0x04, 0x03, 0x05, 0x0e, 0x0b, + 0x05, 0x0c, 0x02, 0x07, 0x08, 0x02, 0x04, 0x0e, + 0x77, 0x7d, 0x7d, 0x78, 0x7e, 0x7b, 0x73, 0x75, + 0x70, 0x76, 0x76, 0x7f, 0x79, 0x70, 0x7a, 0x73, + 0x71, 0x74, 0x72, 0x77, 0x78, 0x72, 0x75, 0x7c, + 0x7b, 0x71, 0x7c, 0x7a, 0x74, 0x7e, 0x7f, 0x79, + 0x7a, 0x73, 0x76, 0x7f, 0x79, 0x70, 0x70, 0x76, + 0x7c, 0x7a, 0x7b, 0x71, 0x77, 0x7d, 0x7d, 0x78, + 0x7f, 0x79, 0x71, 0x74, 0x73, 0x75, 0x7e, 0x7b, + 0x75, 0x7c, 0x72, 0x77, 0x78, 0x72, 0x74, 0x7e, + 0xb7, 0xbd, 0xbd, 0xb8, 0xbe, 0xbb, 0xb3, 0xb5, + 0xb0, 0xb6, 0xb6, 0xbf, 0xb9, 0xb0, 0xba, 0xb3, + 0xb1, 0xb4, 0xb2, 0xb7, 0xb8, 0xb2, 0xb5, 0xbc, + 0xbb, 0xb1, 0xbc, 0xba, 0xb4, 0xbe, 0xbf, 0xb9, + 0xba, 0xb3, 0xb6, 0xbf, 0xb9, 0xb0, 0xb0, 0xb6, + 0xbc, 0xba, 0xbb, 0xb1, 0xb7, 0xbd, 0xbd, 0xb8, + 0xbf, 0xb9, 0xb1, 0xb4, 0xb3, 0xb5, 0xbe, 0xbb, + 0xb5, 0xbc, 0xb2, 0xb7, 0xb8, 0xb2, 0xb4, 0xbe, + 0x47, 0x4d, 0x4d, 0x48, 0x4e, 0x4b, 0x43, 0x45, + 0x40, 0x46, 0x46, 0x4f, 0x49, 0x40, 0x4a, 0x43, + 0x41, 0x44, 0x42, 0x47, 0x48, 0x42, 0x45, 0x4c, + 0x4b, 0x41, 0x4c, 0x4a, 0x44, 0x4e, 0x4f, 0x49, + 0x4a, 0x43, 0x46, 0x4f, 0x49, 0x40, 0x40, 0x46, + 0x4c, 0x4a, 0x4b, 0x41, 0x47, 0x4d, 0x4d, 0x48, + 0x4f, 0x49, 0x41, 0x44, 0x43, 0x45, 0x4e, 0x4b, + 0x45, 0x4c, 0x42, 0x47, 0x48, 0x42, 0x44, 0x4e, + 0x17, 0x1d, 0x1d, 0x18, 0x1e, 0x1b, 0x13, 0x15, + 0x10, 0x16, 0x16, 0x1f, 0x19, 0x10, 0x1a, 0x13, + 0x11, 0x14, 0x12, 0x17, 0x18, 0x12, 0x15, 0x1c, + 0x1b, 0x11, 0x1c, 0x1a, 0x14, 0x1e, 0x1f, 0x19, + 0x1a, 0x13, 0x16, 0x1f, 0x19, 0x10, 0x10, 0x16, + 0x1c, 0x1a, 0x1b, 0x11, 0x17, 0x1d, 0x1d, 0x18, + 0x1f, 0x19, 0x11, 0x14, 0x13, 0x15, 0x1e, 0x1b, + 0x15, 0x1c, 0x12, 0x17, 0x18, 0x12, 0x14, 0x1e, + 0xf7, 0xfd, 0xfd, 0xf8, 0xfe, 0xfb, 0xf3, 0xf5, + 0xf0, 0xf6, 0xf6, 0xff, 0xf9, 0xf0, 0xfa, 0xf3, + 0xf1, 0xf4, 0xf2, 0xf7, 0xf8, 0xf2, 0xf5, 0xfc, + 0xfb, 0xf1, 0xfc, 0xfa, 0xf4, 0xfe, 0xff, 0xf9, + 0xfa, 0xf3, 0xf6, 0xff, 0xf9, 0xf0, 0xf0, 0xf6, + 0xfc, 0xfa, 0xfb, 0xf1, 0xf7, 0xfd, 0xfd, 0xf8, + 0xff, 0xf9, 0xf1, 0xf4, 0xf3, 0xf5, 0xfe, 0xfb, + 0xf5, 0xfc, 0xf2, 0xf7, 0xf8, 0xf2, 0xf4, 0xfe, + 0x27, 0x2d, 0x2d, 0x28, 0x2e, 0x2b, 0x23, 0x25, + 0x20, 0x26, 0x26, 0x2f, 0x29, 0x20, 0x2a, 0x23, + 0x21, 0x24, 0x22, 0x27, 0x28, 0x22, 0x25, 0x2c, + 0x2b, 0x21, 0x2c, 0x2a, 0x24, 0x2e, 0x2f, 0x29, + 0x2a, 0x23, 0x26, 0x2f, 0x29, 0x20, 0x20, 0x26, + 0x2c, 0x2a, 0x2b, 0x21, 0x27, 0x2d, 0x2d, 0x28, + 0x2f, 0x29, 0x21, 0x24, 0x23, 0x25, 0x2e, 0x2b, + 0x25, 0x2c, 0x22, 0x27, 0x28, 0x22, 0x24, 0x2e, + 0xe7, 0xed, 0xed, 0xe8, 0xee, 0xeb, 0xe3, 0xe5, + 0xe0, 0xe6, 0xe6, 0xef, 0xe9, 0xe0, 0xea, 0xe3, + 0xe1, 0xe4, 0xe2, 0xe7, 0xe8, 0xe2, 0xe5, 0xec, + 0xeb, 0xe1, 0xec, 0xea, 0xe4, 0xee, 0xef, 0xe9, + 0xea, 0xe3, 0xe6, 0xef, 0xe9, 0xe0, 0xe0, 0xe6, + 0xec, 0xea, 0xeb, 0xe1, 0xe7, 0xed, 0xed, 0xe8, + 0xef, 0xe9, 0xe1, 0xe4, 0xe3, 0xe5, 0xee, 0xeb, + 0xe5, 0xec, 0xe2, 0xe7, 0xe8, 0xe2, 0xe4, 0xee, + 0xc7, 0xcd, 0xcd, 0xc8, 0xce, 0xcb, 0xc3, 0xc5, + 0xc0, 0xc6, 0xc6, 0xcf, 0xc9, 0xc0, 0xca, 0xc3, + 0xc1, 0xc4, 0xc2, 0xc7, 0xc8, 0xc2, 0xc5, 0xcc, + 0xcb, 0xc1, 0xcc, 0xca, 0xc4, 0xce, 0xcf, 0xc9, + 0xca, 0xc3, 0xc6, 0xcf, 0xc9, 0xc0, 0xc0, 0xc6, + 0xcc, 0xca, 0xcb, 0xc1, 0xc7, 0xcd, 0xcd, 0xc8, + 0xcf, 0xc9, 0xc1, 0xc4, 0xc3, 0xc5, 0xce, 0xcb, + 0xc5, 0xcc, 0xc2, 0xc7, 0xc8, 0xc2, 0xc4, 0xce, + 0x37, 0x3d, 0x3d, 0x38, 0x3e, 0x3b, 0x33, 0x35, + 0x30, 0x36, 0x36, 0x3f, 0x39, 0x30, 0x3a, 0x33, + 0x31, 0x34, 0x32, 0x37, 0x38, 0x32, 0x35, 0x3c, + 0x3b, 0x31, 0x3c, 0x3a, 0x34, 0x3e, 0x3f, 0x39, + 0x3a, 0x33, 0x36, 0x3f, 0x39, 0x30, 0x30, 0x36, + 0x3c, 0x3a, 0x3b, 0x31, 0x37, 0x3d, 0x3d, 0x38, + 0x3f, 0x39, 0x31, 0x34, 0x33, 0x35, 0x3e, 0x3b, + 0x35, 0x3c, 0x32, 0x37, 0x38, 0x32, 0x34, 0x3e, + 0x57, 0x5d, 0x5d, 0x58, 0x5e, 0x5b, 0x53, 0x55, + 0x50, 0x56, 0x56, 0x5f, 0x59, 0x50, 0x5a, 0x53, + 0x51, 0x54, 0x52, 0x57, 0x58, 0x52, 0x55, 0x5c, + 0x5b, 0x51, 0x5c, 0x5a, 0x54, 0x5e, 0x5f, 0x59, + 0x5a, 0x53, 0x56, 0x5f, 0x59, 0x50, 0x50, 0x56, + 0x5c, 0x5a, 0x5b, 0x51, 0x57, 0x5d, 0x5d, 0x58, + 0x5f, 0x59, 0x51, 0x54, 0x53, 0x55, 0x5e, 0x5b, + 0x55, 0x5c, 0x52, 0x57, 0x58, 0x52, 0x54, 0x5e, + 0xb7, 0xbd, 0xbd, 0xb8, 0xbe, 0xbb, 0xb3, 0xb5, + 0xb0, 0xb6, 0xb6, 0xbf, 0xb9, 0xb0, 0xba, 0xb3, + 0xb1, 0xb4, 0xb2, 0xb7, 0xb8, 0xb2, 0xb5, 0xbc, + 0xbb, 0xb1, 0xbc, 0xba, 0xb4, 0xbe, 0xbf, 0xb9, + 0xba, 0xb3, 0xb6, 0xbf, 0xb9, 0xb0, 0xb0, 0xb6, + 0xbc, 0xba, 0xbb, 0xb1, 0xb7, 0xbd, 0xbd, 0xb8, + 0xbf, 0xb9, 0xb1, 0xb4, 0xb3, 0xb5, 0xbe, 0xbb, + 0xb5, 0xbc, 0xb2, 0xb7, 0xb8, 0xb2, 0xb4, 0xbe, + 0xa7, 0xad, 0xad, 0xa8, 0xae, 0xab, 0xa3, 0xa5, + 0xa0, 0xa6, 0xa6, 0xaf, 0xa9, 0xa0, 0xaa, 0xa3, + 0xa1, 0xa4, 0xa2, 0xa7, 0xa8, 0xa2, 0xa5, 0xac, + 0xab, 0xa1, 0xac, 0xaa, 0xa4, 0xae, 0xaf, 0xa9, + 0xaa, 0xa3, 0xa6, 0xaf, 0xa9, 0xa0, 0xa0, 0xa6, + 0xac, 0xaa, 0xab, 0xa1, 0xa7, 0xad, 0xad, 0xa8, + 0xaf, 0xa9, 0xa1, 0xa4, 0xa3, 0xa5, 0xae, 0xab, + 0xa5, 0xac, 0xa2, 0xa7, 0xa8, 0xa2, 0xa4, 0xae, + 0x57, 0x5d, 0x5d, 0x58, 0x5e, 0x5b, 0x53, 0x55, + 0x50, 0x56, 0x56, 0x5f, 0x59, 0x50, 0x5a, 0x53, + 0x51, 0x54, 0x52, 0x57, 0x58, 0x52, 0x55, 0x5c, + 0x5b, 0x51, 0x5c, 0x5a, 0x54, 0x5e, 0x5f, 0x59, + 0x5a, 0x53, 0x56, 0x5f, 0x59, 0x50, 0x50, 0x56, + 0x5c, 0x5a, 0x5b, 0x51, 0x57, 0x5d, 0x5d, 0x58, + 0x5f, 0x59, 0x51, 0x54, 0x53, 0x55, 0x5e, 0x5b, + 0x55, 0x5c, 0x52, 0x57, 0x58, 0x52, 0x54, 0x5e, + 0xe7, 0xed, 0xed, 0xe8, 0xee, 0xeb, 0xe3, 0xe5, + 0xe0, 0xe6, 0xe6, 0xef, 0xe9, 0xe0, 0xea, 0xe3, + 0xe1, 0xe4, 0xe2, 0xe7, 0xe8, 0xe2, 0xe5, 0xec, + 0xeb, 0xe1, 0xec, 0xea, 0xe4, 0xee, 0xef, 0xe9, + 0xea, 0xe3, 0xe6, 0xef, 0xe9, 0xe0, 0xe0, 0xe6, + 0xec, 0xea, 0xeb, 0xe1, 0xe7, 0xed, 0xed, 0xe8, + 0xef, 0xe9, 0xe1, 0xe4, 0xe3, 0xe5, 0xee, 0xeb, + 0xe5, 0xec, 0xe2, 0xe7, 0xe8, 0xe2, 0xe4, 0xee, + 0x27, 0x2d, 0x2d, 0x28, 0x2e, 0x2b, 0x23, 0x25, + 0x20, 0x26, 0x26, 0x2f, 0x29, 0x20, 0x2a, 0x23, + 0x21, 0x24, 0x22, 0x27, 0x28, 0x22, 0x25, 0x2c, + 0x2b, 0x21, 0x2c, 0x2a, 0x24, 0x2e, 0x2f, 0x29, + 0x2a, 0x23, 0x26, 0x2f, 0x29, 0x20, 0x20, 0x26, + 0x2c, 0x2a, 0x2b, 0x21, 0x27, 0x2d, 0x2d, 0x28, + 0x2f, 0x29, 0x21, 0x24, 0x23, 0x25, 0x2e, 0x2b, + 0x25, 0x2c, 0x22, 0x27, 0x28, 0x22, 0x24, 0x2e, + 0x77, 0x7d, 0x7d, 0x78, 0x7e, 0x7b, 0x73, 0x75, + 0x70, 0x76, 0x76, 0x7f, 0x79, 0x70, 0x7a, 0x73, + 0x71, 0x74, 0x72, 0x77, 0x78, 0x72, 0x75, 0x7c, + 0x7b, 0x71, 0x7c, 0x7a, 0x74, 0x7e, 0x7f, 0x79, + 0x7a, 0x73, 0x76, 0x7f, 0x79, 0x70, 0x70, 0x76, + 0x7c, 0x7a, 0x7b, 0x71, 0x77, 0x7d, 0x7d, 0x78, + 0x7f, 0x79, 0x71, 0x74, 0x73, 0x75, 0x7e, 0x7b, + 0x75, 0x7c, 0x72, 0x77, 0x78, 0x72, 0x74, 0x7e, + 0xc7, 0xcd, 0xcd, 0xc8, 0xce, 0xcb, 0xc3, 0xc5, + 0xc0, 0xc6, 0xc6, 0xcf, 0xc9, 0xc0, 0xca, 0xc3, + 0xc1, 0xc4, 0xc2, 0xc7, 0xc8, 0xc2, 0xc5, 0xcc, + 0xcb, 0xc1, 0xcc, 0xca, 0xc4, 0xce, 0xcf, 0xc9, + 0xca, 0xc3, 0xc6, 0xcf, 0xc9, 0xc0, 0xc0, 0xc6, + 0xcc, 0xca, 0xcb, 0xc1, 0xc7, 0xcd, 0xcd, 0xc8, + 0xcf, 0xc9, 0xc1, 0xc4, 0xc3, 0xc5, 0xce, 0xcb, + 0xc5, 0xcc, 0xc2, 0xc7, 0xc8, 0xc2, 0xc4, 0xce, + },{ + 0x2c, 0x2a, 0x21, 0x2f, 0x2a, 0x24, 0x2f, 0x22, + 0x29, 0x27, 0x22, 0x2c, 0x26, 0x29, 0x28, 0x25, + 0x20, 0x26, 0x2d, 0x21, 0x23, 0x2d, 0x24, 0x2e, + 0x2e, 0x20, 0x27, 0x2b, 0x25, 0x23, 0x2b, 0x28, + 0x29, 0x24, 0x2e, 0x23, 0x2f, 0x22, 0x25, 0x2c, + 0x22, 0x29, 0x28, 0x25, 0x2c, 0x2f, 0x23, 0x2a, + 0x27, 0x2b, 0x20, 0x2e, 0x24, 0x21, 0x2a, 0x27, + 0x21, 0x26, 0x2d, 0x20, 0x2b, 0x28, 0x26, 0x2d, + 0xec, 0xea, 0xe1, 0xef, 0xea, 0xe4, 0xef, 0xe2, + 0xe9, 0xe7, 0xe2, 0xec, 0xe6, 0xe9, 0xe8, 0xe5, + 0xe0, 0xe6, 0xed, 0xe1, 0xe3, 0xed, 0xe4, 0xee, + 0xee, 0xe0, 0xe7, 0xeb, 0xe5, 0xe3, 0xeb, 0xe8, + 0xe9, 0xe4, 0xee, 0xe3, 0xef, 0xe2, 0xe5, 0xec, + 0xe2, 0xe9, 0xe8, 0xe5, 0xec, 0xef, 0xe3, 0xea, + 0xe7, 0xeb, 0xe0, 0xee, 0xe4, 0xe1, 0xea, 0xe7, + 0xe1, 0xe6, 0xed, 0xe0, 0xeb, 0xe8, 0xe6, 0xed, + 0xcc, 0xca, 0xc1, 0xcf, 0xca, 0xc4, 0xcf, 0xc2, + 0xc9, 0xc7, 0xc2, 0xcc, 0xc6, 0xc9, 0xc8, 0xc5, + 0xc0, 0xc6, 0xcd, 0xc1, 0xc3, 0xcd, 0xc4, 0xce, + 0xce, 0xc0, 0xc7, 0xcb, 0xc5, 0xc3, 0xcb, 0xc8, + 0xc9, 0xc4, 0xce, 0xc3, 0xcf, 0xc2, 0xc5, 0xcc, + 0xc2, 0xc9, 0xc8, 0xc5, 0xcc, 0xcf, 0xc3, 0xca, + 0xc7, 0xcb, 0xc0, 0xce, 0xc4, 0xc1, 0xca, 0xc7, + 0xc1, 0xc6, 0xcd, 0xc0, 0xcb, 0xc8, 0xc6, 0xcd, + 0xbc, 0xba, 0xb1, 0xbf, 0xba, 0xb4, 0xbf, 0xb2, + 0xb9, 0xb7, 0xb2, 0xbc, 0xb6, 0xb9, 0xb8, 0xb5, + 0xb0, 0xb6, 0xbd, 0xb1, 0xb3, 0xbd, 0xb4, 0xbe, + 0xbe, 0xb0, 0xb7, 0xbb, 0xb5, 0xb3, 0xbb, 0xb8, + 0xb9, 0xb4, 0xbe, 0xb3, 0xbf, 0xb2, 0xb5, 0xbc, + 0xb2, 0xb9, 0xb8, 0xb5, 0xbc, 0xbf, 0xb3, 0xba, + 0xb7, 0xbb, 0xb0, 0xbe, 0xb4, 0xb1, 0xba, 0xb7, + 0xb1, 0xb6, 0xbd, 0xb0, 0xbb, 0xb8, 0xb6, 0xbd, + 0x4c, 0x4a, 0x41, 0x4f, 0x4a, 0x44, 0x4f, 0x42, + 0x49, 0x47, 0x42, 0x4c, 0x46, 0x49, 0x48, 0x45, + 0x40, 0x46, 0x4d, 0x41, 0x43, 0x4d, 0x44, 0x4e, + 0x4e, 0x40, 0x47, 0x4b, 0x45, 0x43, 0x4b, 0x48, + 0x49, 0x44, 0x4e, 0x43, 0x4f, 0x42, 0x45, 0x4c, + 0x42, 0x49, 0x48, 0x45, 0x4c, 0x4f, 0x43, 0x4a, + 0x47, 0x4b, 0x40, 0x4e, 0x44, 0x41, 0x4a, 0x47, + 0x41, 0x46, 0x4d, 0x40, 0x4b, 0x48, 0x46, 0x4d, + 0x2c, 0x2a, 0x21, 0x2f, 0x2a, 0x24, 0x2f, 0x22, + 0x29, 0x27, 0x22, 0x2c, 0x26, 0x29, 0x28, 0x25, + 0x20, 0x26, 0x2d, 0x21, 0x23, 0x2d, 0x24, 0x2e, + 0x2e, 0x20, 0x27, 0x2b, 0x25, 0x23, 0x2b, 0x28, + 0x29, 0x24, 0x2e, 0x23, 0x2f, 0x22, 0x25, 0x2c, + 0x22, 0x29, 0x28, 0x25, 0x2c, 0x2f, 0x23, 0x2a, + 0x27, 0x2b, 0x20, 0x2e, 0x24, 0x21, 0x2a, 0x27, + 0x21, 0x26, 0x2d, 0x20, 0x2b, 0x28, 0x26, 0x2d, + 0x1c, 0x1a, 0x11, 0x1f, 0x1a, 0x14, 0x1f, 0x12, + 0x19, 0x17, 0x12, 0x1c, 0x16, 0x19, 0x18, 0x15, + 0x10, 0x16, 0x1d, 0x11, 0x13, 0x1d, 0x14, 0x1e, + 0x1e, 0x10, 0x17, 0x1b, 0x15, 0x13, 0x1b, 0x18, + 0x19, 0x14, 0x1e, 0x13, 0x1f, 0x12, 0x15, 0x1c, + 0x12, 0x19, 0x18, 0x15, 0x1c, 0x1f, 0x13, 0x1a, + 0x17, 0x1b, 0x10, 0x1e, 0x14, 0x11, 0x1a, 0x17, + 0x11, 0x16, 0x1d, 0x10, 0x1b, 0x18, 0x16, 0x1d, + 0xcc, 0xca, 0xc1, 0xcf, 0xca, 0xc4, 0xcf, 0xc2, + 0xc9, 0xc7, 0xc2, 0xcc, 0xc6, 0xc9, 0xc8, 0xc5, + 0xc0, 0xc6, 0xcd, 0xc1, 0xc3, 0xcd, 0xc4, 0xce, + 0xce, 0xc0, 0xc7, 0xcb, 0xc5, 0xc3, 0xcb, 0xc8, + 0xc9, 0xc4, 0xce, 0xc3, 0xcf, 0xc2, 0xc5, 0xcc, + 0xc2, 0xc9, 0xc8, 0xc5, 0xcc, 0xcf, 0xc3, 0xca, + 0xc7, 0xcb, 0xc0, 0xce, 0xc4, 0xc1, 0xca, 0xc7, + 0xc1, 0xc6, 0xcd, 0xc0, 0xcb, 0xc8, 0xc6, 0xcd, + 0x7c, 0x7a, 0x71, 0x7f, 0x7a, 0x74, 0x7f, 0x72, + 0x79, 0x77, 0x72, 0x7c, 0x76, 0x79, 0x78, 0x75, + 0x70, 0x76, 0x7d, 0x71, 0x73, 0x7d, 0x74, 0x7e, + 0x7e, 0x70, 0x77, 0x7b, 0x75, 0x73, 0x7b, 0x78, + 0x79, 0x74, 0x7e, 0x73, 0x7f, 0x72, 0x75, 0x7c, + 0x72, 0x79, 0x78, 0x75, 0x7c, 0x7f, 0x73, 0x7a, + 0x77, 0x7b, 0x70, 0x7e, 0x74, 0x71, 0x7a, 0x77, + 0x71, 0x76, 0x7d, 0x70, 0x7b, 0x78, 0x76, 0x7d, + 0x4c, 0x4a, 0x41, 0x4f, 0x4a, 0x44, 0x4f, 0x42, + 0x49, 0x47, 0x42, 0x4c, 0x46, 0x49, 0x48, 0x45, + 0x40, 0x46, 0x4d, 0x41, 0x43, 0x4d, 0x44, 0x4e, + 0x4e, 0x40, 0x47, 0x4b, 0x45, 0x43, 0x4b, 0x48, + 0x49, 0x44, 0x4e, 0x43, 0x4f, 0x42, 0x45, 0x4c, + 0x42, 0x49, 0x48, 0x45, 0x4c, 0x4f, 0x43, 0x4a, + 0x47, 0x4b, 0x40, 0x4e, 0x44, 0x41, 0x4a, 0x47, + 0x41, 0x46, 0x4d, 0x40, 0x4b, 0x48, 0x46, 0x4d, + 0xac, 0xaa, 0xa1, 0xaf, 0xaa, 0xa4, 0xaf, 0xa2, + 0xa9, 0xa7, 0xa2, 0xac, 0xa6, 0xa9, 0xa8, 0xa5, + 0xa0, 0xa6, 0xad, 0xa1, 0xa3, 0xad, 0xa4, 0xae, + 0xae, 0xa0, 0xa7, 0xab, 0xa5, 0xa3, 0xab, 0xa8, + 0xa9, 0xa4, 0xae, 0xa3, 0xaf, 0xa2, 0xa5, 0xac, + 0xa2, 0xa9, 0xa8, 0xa5, 0xac, 0xaf, 0xa3, 0xaa, + 0xa7, 0xab, 0xa0, 0xae, 0xa4, 0xa1, 0xaa, 0xa7, + 0xa1, 0xa6, 0xad, 0xa0, 0xab, 0xa8, 0xa6, 0xad, + 0x7c, 0x7a, 0x71, 0x7f, 0x7a, 0x74, 0x7f, 0x72, + 0x79, 0x77, 0x72, 0x7c, 0x76, 0x79, 0x78, 0x75, + 0x70, 0x76, 0x7d, 0x71, 0x73, 0x7d, 0x74, 0x7e, + 0x7e, 0x70, 0x77, 0x7b, 0x75, 0x73, 0x7b, 0x78, + 0x79, 0x74, 0x7e, 0x73, 0x7f, 0x72, 0x75, 0x7c, + 0x72, 0x79, 0x78, 0x75, 0x7c, 0x7f, 0x73, 0x7a, + 0x77, 0x7b, 0x70, 0x7e, 0x74, 0x71, 0x7a, 0x77, + 0x71, 0x76, 0x7d, 0x70, 0x7b, 0x78, 0x76, 0x7d, + 0xbc, 0xba, 0xb1, 0xbf, 0xba, 0xb4, 0xbf, 0xb2, + 0xb9, 0xb7, 0xb2, 0xbc, 0xb6, 0xb9, 0xb8, 0xb5, + 0xb0, 0xb6, 0xbd, 0xb1, 0xb3, 0xbd, 0xb4, 0xbe, + 0xbe, 0xb0, 0xb7, 0xbb, 0xb5, 0xb3, 0xbb, 0xb8, + 0xb9, 0xb4, 0xbe, 0xb3, 0xbf, 0xb2, 0xb5, 0xbc, + 0xb2, 0xb9, 0xb8, 0xb5, 0xbc, 0xbf, 0xb3, 0xba, + 0xb7, 0xbb, 0xb0, 0xbe, 0xb4, 0xb1, 0xba, 0xb7, + 0xb1, 0xb6, 0xbd, 0xb0, 0xbb, 0xb8, 0xb6, 0xbd, + 0xdc, 0xda, 0xd1, 0xdf, 0xda, 0xd4, 0xdf, 0xd2, + 0xd9, 0xd7, 0xd2, 0xdc, 0xd6, 0xd9, 0xd8, 0xd5, + 0xd0, 0xd6, 0xdd, 0xd1, 0xd3, 0xdd, 0xd4, 0xde, + 0xde, 0xd0, 0xd7, 0xdb, 0xd5, 0xd3, 0xdb, 0xd8, + 0xd9, 0xd4, 0xde, 0xd3, 0xdf, 0xd2, 0xd5, 0xdc, + 0xd2, 0xd9, 0xd8, 0xd5, 0xdc, 0xdf, 0xd3, 0xda, + 0xd7, 0xdb, 0xd0, 0xde, 0xd4, 0xd1, 0xda, 0xd7, + 0xd1, 0xd6, 0xdd, 0xd0, 0xdb, 0xd8, 0xd6, 0xdd, + 0x6c, 0x6a, 0x61, 0x6f, 0x6a, 0x64, 0x6f, 0x62, + 0x69, 0x67, 0x62, 0x6c, 0x66, 0x69, 0x68, 0x65, + 0x60, 0x66, 0x6d, 0x61, 0x63, 0x6d, 0x64, 0x6e, + 0x6e, 0x60, 0x67, 0x6b, 0x65, 0x63, 0x6b, 0x68, + 0x69, 0x64, 0x6e, 0x63, 0x6f, 0x62, 0x65, 0x6c, + 0x62, 0x69, 0x68, 0x65, 0x6c, 0x6f, 0x63, 0x6a, + 0x67, 0x6b, 0x60, 0x6e, 0x64, 0x61, 0x6a, 0x67, + 0x61, 0x66, 0x6d, 0x60, 0x6b, 0x68, 0x66, 0x6d, + 0x1c, 0x1a, 0x11, 0x1f, 0x1a, 0x14, 0x1f, 0x12, + 0x19, 0x17, 0x12, 0x1c, 0x16, 0x19, 0x18, 0x15, + 0x10, 0x16, 0x1d, 0x11, 0x13, 0x1d, 0x14, 0x1e, + 0x1e, 0x10, 0x17, 0x1b, 0x15, 0x13, 0x1b, 0x18, + 0x19, 0x14, 0x1e, 0x13, 0x1f, 0x12, 0x15, 0x1c, + 0x12, 0x19, 0x18, 0x15, 0x1c, 0x1f, 0x13, 0x1a, + 0x17, 0x1b, 0x10, 0x1e, 0x14, 0x11, 0x1a, 0x17, + 0x11, 0x16, 0x1d, 0x10, 0x1b, 0x18, 0x16, 0x1d, + 0x8c, 0x8a, 0x81, 0x8f, 0x8a, 0x84, 0x8f, 0x82, + 0x89, 0x87, 0x82, 0x8c, 0x86, 0x89, 0x88, 0x85, + 0x80, 0x86, 0x8d, 0x81, 0x83, 0x8d, 0x84, 0x8e, + 0x8e, 0x80, 0x87, 0x8b, 0x85, 0x83, 0x8b, 0x88, + 0x89, 0x84, 0x8e, 0x83, 0x8f, 0x82, 0x85, 0x8c, + 0x82, 0x89, 0x88, 0x85, 0x8c, 0x8f, 0x83, 0x8a, + 0x87, 0x8b, 0x80, 0x8e, 0x84, 0x81, 0x8a, 0x87, + 0x81, 0x86, 0x8d, 0x80, 0x8b, 0x88, 0x86, 0x8d, + 0x5c, 0x5a, 0x51, 0x5f, 0x5a, 0x54, 0x5f, 0x52, + 0x59, 0x57, 0x52, 0x5c, 0x56, 0x59, 0x58, 0x55, + 0x50, 0x56, 0x5d, 0x51, 0x53, 0x5d, 0x54, 0x5e, + 0x5e, 0x50, 0x57, 0x5b, 0x55, 0x53, 0x5b, 0x58, + 0x59, 0x54, 0x5e, 0x53, 0x5f, 0x52, 0x55, 0x5c, + 0x52, 0x59, 0x58, 0x55, 0x5c, 0x5f, 0x53, 0x5a, + 0x57, 0x5b, 0x50, 0x5e, 0x54, 0x51, 0x5a, 0x57, + 0x51, 0x56, 0x5d, 0x50, 0x5b, 0x58, 0x56, 0x5d, + 0x5c, 0x5a, 0x51, 0x5f, 0x5a, 0x54, 0x5f, 0x52, + 0x59, 0x57, 0x52, 0x5c, 0x56, 0x59, 0x58, 0x55, + 0x50, 0x56, 0x5d, 0x51, 0x53, 0x5d, 0x54, 0x5e, + 0x5e, 0x50, 0x57, 0x5b, 0x55, 0x53, 0x5b, 0x58, + 0x59, 0x54, 0x5e, 0x53, 0x5f, 0x52, 0x55, 0x5c, + 0x52, 0x59, 0x58, 0x55, 0x5c, 0x5f, 0x53, 0x5a, + 0x57, 0x5b, 0x50, 0x5e, 0x54, 0x51, 0x5a, 0x57, + 0x51, 0x56, 0x5d, 0x50, 0x5b, 0x58, 0x56, 0x5d, + 0x0c, 0x0a, 0x01, 0x0f, 0x0a, 0x04, 0x0f, 0x02, + 0x09, 0x07, 0x02, 0x0c, 0x06, 0x09, 0x08, 0x05, + 0x00, 0x06, 0x0d, 0x01, 0x03, 0x0d, 0x04, 0x0e, + 0x0e, 0x00, 0x07, 0x0b, 0x05, 0x03, 0x0b, 0x08, + 0x09, 0x04, 0x0e, 0x03, 0x0f, 0x02, 0x05, 0x0c, + 0x02, 0x09, 0x08, 0x05, 0x0c, 0x0f, 0x03, 0x0a, + 0x07, 0x0b, 0x00, 0x0e, 0x04, 0x01, 0x0a, 0x07, + 0x01, 0x06, 0x0d, 0x00, 0x0b, 0x08, 0x06, 0x0d, + 0x3c, 0x3a, 0x31, 0x3f, 0x3a, 0x34, 0x3f, 0x32, + 0x39, 0x37, 0x32, 0x3c, 0x36, 0x39, 0x38, 0x35, + 0x30, 0x36, 0x3d, 0x31, 0x33, 0x3d, 0x34, 0x3e, + 0x3e, 0x30, 0x37, 0x3b, 0x35, 0x33, 0x3b, 0x38, + 0x39, 0x34, 0x3e, 0x33, 0x3f, 0x32, 0x35, 0x3c, + 0x32, 0x39, 0x38, 0x35, 0x3c, 0x3f, 0x33, 0x3a, + 0x37, 0x3b, 0x30, 0x3e, 0x34, 0x31, 0x3a, 0x37, + 0x31, 0x36, 0x3d, 0x30, 0x3b, 0x38, 0x36, 0x3d, + 0xfc, 0xfa, 0xf1, 0xff, 0xfa, 0xf4, 0xff, 0xf2, + 0xf9, 0xf7, 0xf2, 0xfc, 0xf6, 0xf9, 0xf8, 0xf5, + 0xf0, 0xf6, 0xfd, 0xf1, 0xf3, 0xfd, 0xf4, 0xfe, + 0xfe, 0xf0, 0xf7, 0xfb, 0xf5, 0xf3, 0xfb, 0xf8, + 0xf9, 0xf4, 0xfe, 0xf3, 0xff, 0xf2, 0xf5, 0xfc, + 0xf2, 0xf9, 0xf8, 0xf5, 0xfc, 0xff, 0xf3, 0xfa, + 0xf7, 0xfb, 0xf0, 0xfe, 0xf4, 0xf1, 0xfa, 0xf7, + 0xf1, 0xf6, 0xfd, 0xf0, 0xfb, 0xf8, 0xf6, 0xfd, + 0xfc, 0xfa, 0xf1, 0xff, 0xfa, 0xf4, 0xff, 0xf2, + 0xf9, 0xf7, 0xf2, 0xfc, 0xf6, 0xf9, 0xf8, 0xf5, + 0xf0, 0xf6, 0xfd, 0xf1, 0xf3, 0xfd, 0xf4, 0xfe, + 0xfe, 0xf0, 0xf7, 0xfb, 0xf5, 0xf3, 0xfb, 0xf8, + 0xf9, 0xf4, 0xfe, 0xf3, 0xff, 0xf2, 0xf5, 0xfc, + 0xf2, 0xf9, 0xf8, 0xf5, 0xfc, 0xff, 0xf3, 0xfa, + 0xf7, 0xfb, 0xf0, 0xfe, 0xf4, 0xf1, 0xfa, 0xf7, + 0xf1, 0xf6, 0xfd, 0xf0, 0xfb, 0xf8, 0xf6, 0xfd, + 0xac, 0xaa, 0xa1, 0xaf, 0xaa, 0xa4, 0xaf, 0xa2, + 0xa9, 0xa7, 0xa2, 0xac, 0xa6, 0xa9, 0xa8, 0xa5, + 0xa0, 0xa6, 0xad, 0xa1, 0xa3, 0xad, 0xa4, 0xae, + 0xae, 0xa0, 0xa7, 0xab, 0xa5, 0xa3, 0xab, 0xa8, + 0xa9, 0xa4, 0xae, 0xa3, 0xaf, 0xa2, 0xa5, 0xac, + 0xa2, 0xa9, 0xa8, 0xa5, 0xac, 0xaf, 0xa3, 0xaa, + 0xa7, 0xab, 0xa0, 0xae, 0xa4, 0xa1, 0xaa, 0xa7, + 0xa1, 0xa6, 0xad, 0xa0, 0xab, 0xa8, 0xa6, 0xad, + 0xdc, 0xda, 0xd1, 0xdf, 0xda, 0xd4, 0xdf, 0xd2, + 0xd9, 0xd7, 0xd2, 0xdc, 0xd6, 0xd9, 0xd8, 0xd5, + 0xd0, 0xd6, 0xdd, 0xd1, 0xd3, 0xdd, 0xd4, 0xde, + 0xde, 0xd0, 0xd7, 0xdb, 0xd5, 0xd3, 0xdb, 0xd8, + 0xd9, 0xd4, 0xde, 0xd3, 0xdf, 0xd2, 0xd5, 0xdc, + 0xd2, 0xd9, 0xd8, 0xd5, 0xdc, 0xdf, 0xd3, 0xda, + 0xd7, 0xdb, 0xd0, 0xde, 0xd4, 0xd1, 0xda, 0xd7, + 0xd1, 0xd6, 0xdd, 0xd0, 0xdb, 0xd8, 0xd6, 0xdd, + 0x3c, 0x3a, 0x31, 0x3f, 0x3a, 0x34, 0x3f, 0x32, + 0x39, 0x37, 0x32, 0x3c, 0x36, 0x39, 0x38, 0x35, + 0x30, 0x36, 0x3d, 0x31, 0x33, 0x3d, 0x34, 0x3e, + 0x3e, 0x30, 0x37, 0x3b, 0x35, 0x33, 0x3b, 0x38, + 0x39, 0x34, 0x3e, 0x33, 0x3f, 0x32, 0x35, 0x3c, + 0x32, 0x39, 0x38, 0x35, 0x3c, 0x3f, 0x33, 0x3a, + 0x37, 0x3b, 0x30, 0x3e, 0x34, 0x31, 0x3a, 0x37, + 0x31, 0x36, 0x3d, 0x30, 0x3b, 0x38, 0x36, 0x3d, + 0x0c, 0x0a, 0x01, 0x0f, 0x0a, 0x04, 0x0f, 0x02, + 0x09, 0x07, 0x02, 0x0c, 0x06, 0x09, 0x08, 0x05, + 0x00, 0x06, 0x0d, 0x01, 0x03, 0x0d, 0x04, 0x0e, + 0x0e, 0x00, 0x07, 0x0b, 0x05, 0x03, 0x0b, 0x08, + 0x09, 0x04, 0x0e, 0x03, 0x0f, 0x02, 0x05, 0x0c, + 0x02, 0x09, 0x08, 0x05, 0x0c, 0x0f, 0x03, 0x0a, + 0x07, 0x0b, 0x00, 0x0e, 0x04, 0x01, 0x0a, 0x07, + 0x01, 0x06, 0x0d, 0x00, 0x0b, 0x08, 0x06, 0x0d, + 0x9c, 0x9a, 0x91, 0x9f, 0x9a, 0x94, 0x9f, 0x92, + 0x99, 0x97, 0x92, 0x9c, 0x96, 0x99, 0x98, 0x95, + 0x90, 0x96, 0x9d, 0x91, 0x93, 0x9d, 0x94, 0x9e, + 0x9e, 0x90, 0x97, 0x9b, 0x95, 0x93, 0x9b, 0x98, + 0x99, 0x94, 0x9e, 0x93, 0x9f, 0x92, 0x95, 0x9c, + 0x92, 0x99, 0x98, 0x95, 0x9c, 0x9f, 0x93, 0x9a, + 0x97, 0x9b, 0x90, 0x9e, 0x94, 0x91, 0x9a, 0x97, + 0x91, 0x96, 0x9d, 0x90, 0x9b, 0x98, 0x96, 0x9d, + 0xec, 0xea, 0xe1, 0xef, 0xea, 0xe4, 0xef, 0xe2, + 0xe9, 0xe7, 0xe2, 0xec, 0xe6, 0xe9, 0xe8, 0xe5, + 0xe0, 0xe6, 0xed, 0xe1, 0xe3, 0xed, 0xe4, 0xee, + 0xee, 0xe0, 0xe7, 0xeb, 0xe5, 0xe3, 0xeb, 0xe8, + 0xe9, 0xe4, 0xee, 0xe3, 0xef, 0xe2, 0xe5, 0xec, + 0xe2, 0xe9, 0xe8, 0xe5, 0xec, 0xef, 0xe3, 0xea, + 0xe7, 0xeb, 0xe0, 0xee, 0xe4, 0xe1, 0xea, 0xe7, + 0xe1, 0xe6, 0xed, 0xe0, 0xeb, 0xe8, 0xe6, 0xed, + 0x8c, 0x8a, 0x81, 0x8f, 0x8a, 0x84, 0x8f, 0x82, + 0x89, 0x87, 0x82, 0x8c, 0x86, 0x89, 0x88, 0x85, + 0x80, 0x86, 0x8d, 0x81, 0x83, 0x8d, 0x84, 0x8e, + 0x8e, 0x80, 0x87, 0x8b, 0x85, 0x83, 0x8b, 0x88, + 0x89, 0x84, 0x8e, 0x83, 0x8f, 0x82, 0x85, 0x8c, + 0x82, 0x89, 0x88, 0x85, 0x8c, 0x8f, 0x83, 0x8a, + 0x87, 0x8b, 0x80, 0x8e, 0x84, 0x81, 0x8a, 0x87, + 0x81, 0x86, 0x8d, 0x80, 0x8b, 0x88, 0x86, 0x8d, + 0x9c, 0x9a, 0x91, 0x9f, 0x9a, 0x94, 0x9f, 0x92, + 0x99, 0x97, 0x92, 0x9c, 0x96, 0x99, 0x98, 0x95, + 0x90, 0x96, 0x9d, 0x91, 0x93, 0x9d, 0x94, 0x9e, + 0x9e, 0x90, 0x97, 0x9b, 0x95, 0x93, 0x9b, 0x98, + 0x99, 0x94, 0x9e, 0x93, 0x9f, 0x92, 0x95, 0x9c, + 0x92, 0x99, 0x98, 0x95, 0x9c, 0x9f, 0x93, 0x9a, + 0x97, 0x9b, 0x90, 0x9e, 0x94, 0x91, 0x9a, 0x97, + 0x91, 0x96, 0x9d, 0x90, 0x9b, 0x98, 0x96, 0x9d, + 0x6c, 0x6a, 0x61, 0x6f, 0x6a, 0x64, 0x6f, 0x62, + 0x69, 0x67, 0x62, 0x6c, 0x66, 0x69, 0x68, 0x65, + 0x60, 0x66, 0x6d, 0x61, 0x63, 0x6d, 0x64, 0x6e, + 0x6e, 0x60, 0x67, 0x6b, 0x65, 0x63, 0x6b, 0x68, + 0x69, 0x64, 0x6e, 0x63, 0x6f, 0x62, 0x65, 0x6c, + 0x62, 0x69, 0x68, 0x65, 0x6c, 0x6f, 0x63, 0x6a, + 0x67, 0x6b, 0x60, 0x6e, 0x64, 0x61, 0x6a, 0x67, + 0x61, 0x66, 0x6d, 0x60, 0x6b, 0x68, 0x66, 0x6d, + 0x4c, 0x4a, 0x41, 0x4f, 0x4a, 0x44, 0x4f, 0x42, + 0x49, 0x47, 0x42, 0x4c, 0x46, 0x49, 0x48, 0x45, + 0x40, 0x46, 0x4d, 0x41, 0x43, 0x4d, 0x44, 0x4e, + 0x4e, 0x40, 0x47, 0x4b, 0x45, 0x43, 0x4b, 0x48, + 0x49, 0x44, 0x4e, 0x43, 0x4f, 0x42, 0x45, 0x4c, + 0x42, 0x49, 0x48, 0x45, 0x4c, 0x4f, 0x43, 0x4a, + 0x47, 0x4b, 0x40, 0x4e, 0x44, 0x41, 0x4a, 0x47, + 0x41, 0x46, 0x4d, 0x40, 0x4b, 0x48, 0x46, 0x4d, + 0xbc, 0xba, 0xb1, 0xbf, 0xba, 0xb4, 0xbf, 0xb2, + 0xb9, 0xb7, 0xb2, 0xbc, 0xb6, 0xb9, 0xb8, 0xb5, + 0xb0, 0xb6, 0xbd, 0xb1, 0xb3, 0xbd, 0xb4, 0xbe, + 0xbe, 0xb0, 0xb7, 0xbb, 0xb5, 0xb3, 0xbb, 0xb8, + 0xb9, 0xb4, 0xbe, 0xb3, 0xbf, 0xb2, 0xb5, 0xbc, + 0xb2, 0xb9, 0xb8, 0xb5, 0xbc, 0xbf, 0xb3, 0xba, + 0xb7, 0xbb, 0xb0, 0xbe, 0xb4, 0xb1, 0xba, 0xb7, + 0xb1, 0xb6, 0xbd, 0xb0, 0xbb, 0xb8, 0xb6, 0xbd, + 0x2c, 0x2a, 0x21, 0x2f, 0x2a, 0x24, 0x2f, 0x22, + 0x29, 0x27, 0x22, 0x2c, 0x26, 0x29, 0x28, 0x25, + 0x20, 0x26, 0x2d, 0x21, 0x23, 0x2d, 0x24, 0x2e, + 0x2e, 0x20, 0x27, 0x2b, 0x25, 0x23, 0x2b, 0x28, + 0x29, 0x24, 0x2e, 0x23, 0x2f, 0x22, 0x25, 0x2c, + 0x22, 0x29, 0x28, 0x25, 0x2c, 0x2f, 0x23, 0x2a, + 0x27, 0x2b, 0x20, 0x2e, 0x24, 0x21, 0x2a, 0x27, + 0x21, 0x26, 0x2d, 0x20, 0x2b, 0x28, 0x26, 0x2d, + 0x8c, 0x8a, 0x81, 0x8f, 0x8a, 0x84, 0x8f, 0x82, + 0x89, 0x87, 0x82, 0x8c, 0x86, 0x89, 0x88, 0x85, + 0x80, 0x86, 0x8d, 0x81, 0x83, 0x8d, 0x84, 0x8e, + 0x8e, 0x80, 0x87, 0x8b, 0x85, 0x83, 0x8b, 0x88, + 0x89, 0x84, 0x8e, 0x83, 0x8f, 0x82, 0x85, 0x8c, + 0x82, 0x89, 0x88, 0x85, 0x8c, 0x8f, 0x83, 0x8a, + 0x87, 0x8b, 0x80, 0x8e, 0x84, 0x81, 0x8a, 0x87, + 0x81, 0x86, 0x8d, 0x80, 0x8b, 0x88, 0x86, 0x8d, + 0x1c, 0x1a, 0x11, 0x1f, 0x1a, 0x14, 0x1f, 0x12, + 0x19, 0x17, 0x12, 0x1c, 0x16, 0x19, 0x18, 0x15, + 0x10, 0x16, 0x1d, 0x11, 0x13, 0x1d, 0x14, 0x1e, + 0x1e, 0x10, 0x17, 0x1b, 0x15, 0x13, 0x1b, 0x18, + 0x19, 0x14, 0x1e, 0x13, 0x1f, 0x12, 0x15, 0x1c, + 0x12, 0x19, 0x18, 0x15, 0x1c, 0x1f, 0x13, 0x1a, + 0x17, 0x1b, 0x10, 0x1e, 0x14, 0x11, 0x1a, 0x17, + 0x11, 0x16, 0x1d, 0x10, 0x1b, 0x18, 0x16, 0x1d, + 0xcc, 0xca, 0xc1, 0xcf, 0xca, 0xc4, 0xcf, 0xc2, + 0xc9, 0xc7, 0xc2, 0xcc, 0xc6, 0xc9, 0xc8, 0xc5, + 0xc0, 0xc6, 0xcd, 0xc1, 0xc3, 0xcd, 0xc4, 0xce, + 0xce, 0xc0, 0xc7, 0xcb, 0xc5, 0xc3, 0xcb, 0xc8, + 0xc9, 0xc4, 0xce, 0xc3, 0xcf, 0xc2, 0xc5, 0xcc, + 0xc2, 0xc9, 0xc8, 0xc5, 0xcc, 0xcf, 0xc3, 0xca, + 0xc7, 0xcb, 0xc0, 0xce, 0xc4, 0xc1, 0xca, 0xc7, + 0xc1, 0xc6, 0xcd, 0xc0, 0xcb, 0xc8, 0xc6, 0xcd, + 0xbc, 0xba, 0xb1, 0xbf, 0xba, 0xb4, 0xbf, 0xb2, + 0xb9, 0xb7, 0xb2, 0xbc, 0xb6, 0xb9, 0xb8, 0xb5, + 0xb0, 0xb6, 0xbd, 0xb1, 0xb3, 0xbd, 0xb4, 0xbe, + 0xbe, 0xb0, 0xb7, 0xbb, 0xb5, 0xb3, 0xbb, 0xb8, + 0xb9, 0xb4, 0xbe, 0xb3, 0xbf, 0xb2, 0xb5, 0xbc, + 0xb2, 0xb9, 0xb8, 0xb5, 0xbc, 0xbf, 0xb3, 0xba, + 0xb7, 0xbb, 0xb0, 0xbe, 0xb4, 0xb1, 0xba, 0xb7, + 0xb1, 0xb6, 0xbd, 0xb0, 0xbb, 0xb8, 0xb6, 0xbd, + 0x7c, 0x7a, 0x71, 0x7f, 0x7a, 0x74, 0x7f, 0x72, + 0x79, 0x77, 0x72, 0x7c, 0x76, 0x79, 0x78, 0x75, + 0x70, 0x76, 0x7d, 0x71, 0x73, 0x7d, 0x74, 0x7e, + 0x7e, 0x70, 0x77, 0x7b, 0x75, 0x73, 0x7b, 0x78, + 0x79, 0x74, 0x7e, 0x73, 0x7f, 0x72, 0x75, 0x7c, + 0x72, 0x79, 0x78, 0x75, 0x7c, 0x7f, 0x73, 0x7a, + 0x77, 0x7b, 0x70, 0x7e, 0x74, 0x71, 0x7a, 0x77, + 0x71, 0x76, 0x7d, 0x70, 0x7b, 0x78, 0x76, 0x7d, + 0xac, 0xaa, 0xa1, 0xaf, 0xaa, 0xa4, 0xaf, 0xa2, + 0xa9, 0xa7, 0xa2, 0xac, 0xa6, 0xa9, 0xa8, 0xa5, + 0xa0, 0xa6, 0xad, 0xa1, 0xa3, 0xad, 0xa4, 0xae, + 0xae, 0xa0, 0xa7, 0xab, 0xa5, 0xa3, 0xab, 0xa8, + 0xa9, 0xa4, 0xae, 0xa3, 0xaf, 0xa2, 0xa5, 0xac, + 0xa2, 0xa9, 0xa8, 0xa5, 0xac, 0xaf, 0xa3, 0xaa, + 0xa7, 0xab, 0xa0, 0xae, 0xa4, 0xa1, 0xaa, 0xa7, + 0xa1, 0xa6, 0xad, 0xa0, 0xab, 0xa8, 0xa6, 0xad, + 0x1c, 0x1a, 0x11, 0x1f, 0x1a, 0x14, 0x1f, 0x12, + 0x19, 0x17, 0x12, 0x1c, 0x16, 0x19, 0x18, 0x15, + 0x10, 0x16, 0x1d, 0x11, 0x13, 0x1d, 0x14, 0x1e, + 0x1e, 0x10, 0x17, 0x1b, 0x15, 0x13, 0x1b, 0x18, + 0x19, 0x14, 0x1e, 0x13, 0x1f, 0x12, 0x15, 0x1c, + 0x12, 0x19, 0x18, 0x15, 0x1c, 0x1f, 0x13, 0x1a, + 0x17, 0x1b, 0x10, 0x1e, 0x14, 0x11, 0x1a, 0x17, + 0x11, 0x16, 0x1d, 0x10, 0x1b, 0x18, 0x16, 0x1d, + 0xdc, 0xda, 0xd1, 0xdf, 0xda, 0xd4, 0xdf, 0xd2, + 0xd9, 0xd7, 0xd2, 0xdc, 0xd6, 0xd9, 0xd8, 0xd5, + 0xd0, 0xd6, 0xdd, 0xd1, 0xd3, 0xdd, 0xd4, 0xde, + 0xde, 0xd0, 0xd7, 0xdb, 0xd5, 0xd3, 0xdb, 0xd8, + 0xd9, 0xd4, 0xde, 0xd3, 0xdf, 0xd2, 0xd5, 0xdc, + 0xd2, 0xd9, 0xd8, 0xd5, 0xdc, 0xdf, 0xd3, 0xda, + 0xd7, 0xdb, 0xd0, 0xde, 0xd4, 0xd1, 0xda, 0xd7, + 0xd1, 0xd6, 0xdd, 0xd0, 0xdb, 0xd8, 0xd6, 0xdd, + 0xec, 0xea, 0xe1, 0xef, 0xea, 0xe4, 0xef, 0xe2, + 0xe9, 0xe7, 0xe2, 0xec, 0xe6, 0xe9, 0xe8, 0xe5, + 0xe0, 0xe6, 0xed, 0xe1, 0xe3, 0xed, 0xe4, 0xee, + 0xee, 0xe0, 0xe7, 0xeb, 0xe5, 0xe3, 0xeb, 0xe8, + 0xe9, 0xe4, 0xee, 0xe3, 0xef, 0xe2, 0xe5, 0xec, + 0xe2, 0xe9, 0xe8, 0xe5, 0xec, 0xef, 0xe3, 0xea, + 0xe7, 0xeb, 0xe0, 0xee, 0xe4, 0xe1, 0xea, 0xe7, + 0xe1, 0xe6, 0xed, 0xe0, 0xeb, 0xe8, 0xe6, 0xed, + 0x7c, 0x7a, 0x71, 0x7f, 0x7a, 0x74, 0x7f, 0x72, + 0x79, 0x77, 0x72, 0x7c, 0x76, 0x79, 0x78, 0x75, + 0x70, 0x76, 0x7d, 0x71, 0x73, 0x7d, 0x74, 0x7e, + 0x7e, 0x70, 0x77, 0x7b, 0x75, 0x73, 0x7b, 0x78, + 0x79, 0x74, 0x7e, 0x73, 0x7f, 0x72, 0x75, 0x7c, + 0x72, 0x79, 0x78, 0x75, 0x7c, 0x7f, 0x73, 0x7a, + 0x77, 0x7b, 0x70, 0x7e, 0x74, 0x71, 0x7a, 0x77, + 0x71, 0x76, 0x7d, 0x70, 0x7b, 0x78, 0x76, 0x7d, + 0x2c, 0x2a, 0x21, 0x2f, 0x2a, 0x24, 0x2f, 0x22, + 0x29, 0x27, 0x22, 0x2c, 0x26, 0x29, 0x28, 0x25, + 0x20, 0x26, 0x2d, 0x21, 0x23, 0x2d, 0x24, 0x2e, + 0x2e, 0x20, 0x27, 0x2b, 0x25, 0x23, 0x2b, 0x28, + 0x29, 0x24, 0x2e, 0x23, 0x2f, 0x22, 0x25, 0x2c, + 0x22, 0x29, 0x28, 0x25, 0x2c, 0x2f, 0x23, 0x2a, + 0x27, 0x2b, 0x20, 0x2e, 0x24, 0x21, 0x2a, 0x27, + 0x21, 0x26, 0x2d, 0x20, 0x2b, 0x28, 0x26, 0x2d, + 0x8c, 0x8a, 0x81, 0x8f, 0x8a, 0x84, 0x8f, 0x82, + 0x89, 0x87, 0x82, 0x8c, 0x86, 0x89, 0x88, 0x85, + 0x80, 0x86, 0x8d, 0x81, 0x83, 0x8d, 0x84, 0x8e, + 0x8e, 0x80, 0x87, 0x8b, 0x85, 0x83, 0x8b, 0x88, + 0x89, 0x84, 0x8e, 0x83, 0x8f, 0x82, 0x85, 0x8c, + 0x82, 0x89, 0x88, 0x85, 0x8c, 0x8f, 0x83, 0x8a, + 0x87, 0x8b, 0x80, 0x8e, 0x84, 0x81, 0x8a, 0x87, + 0x81, 0x86, 0x8d, 0x80, 0x8b, 0x88, 0x86, 0x8d, + 0xdc, 0xda, 0xd1, 0xdf, 0xda, 0xd4, 0xdf, 0xd2, + 0xd9, 0xd7, 0xd2, 0xdc, 0xd6, 0xd9, 0xd8, 0xd5, + 0xd0, 0xd6, 0xdd, 0xd1, 0xd3, 0xdd, 0xd4, 0xde, + 0xde, 0xd0, 0xd7, 0xdb, 0xd5, 0xd3, 0xdb, 0xd8, + 0xd9, 0xd4, 0xde, 0xd3, 0xdf, 0xd2, 0xd5, 0xdc, + 0xd2, 0xd9, 0xd8, 0xd5, 0xdc, 0xdf, 0xd3, 0xda, + 0xd7, 0xdb, 0xd0, 0xde, 0xd4, 0xd1, 0xda, 0xd7, + 0xd1, 0xd6, 0xdd, 0xd0, 0xdb, 0xd8, 0xd6, 0xdd, + 0xfc, 0xfa, 0xf1, 0xff, 0xfa, 0xf4, 0xff, 0xf2, + 0xf9, 0xf7, 0xf2, 0xfc, 0xf6, 0xf9, 0xf8, 0xf5, + 0xf0, 0xf6, 0xfd, 0xf1, 0xf3, 0xfd, 0xf4, 0xfe, + 0xfe, 0xf0, 0xf7, 0xfb, 0xf5, 0xf3, 0xfb, 0xf8, + 0xf9, 0xf4, 0xfe, 0xf3, 0xff, 0xf2, 0xf5, 0xfc, + 0xf2, 0xf9, 0xf8, 0xf5, 0xfc, 0xff, 0xf3, 0xfa, + 0xf7, 0xfb, 0xf0, 0xfe, 0xf4, 0xf1, 0xfa, 0xf7, + 0xf1, 0xf6, 0xfd, 0xf0, 0xfb, 0xf8, 0xf6, 0xfd, + 0x6c, 0x6a, 0x61, 0x6f, 0x6a, 0x64, 0x6f, 0x62, + 0x69, 0x67, 0x62, 0x6c, 0x66, 0x69, 0x68, 0x65, + 0x60, 0x66, 0x6d, 0x61, 0x63, 0x6d, 0x64, 0x6e, + 0x6e, 0x60, 0x67, 0x6b, 0x65, 0x63, 0x6b, 0x68, + 0x69, 0x64, 0x6e, 0x63, 0x6f, 0x62, 0x65, 0x6c, + 0x62, 0x69, 0x68, 0x65, 0x6c, 0x6f, 0x63, 0x6a, + 0x67, 0x6b, 0x60, 0x6e, 0x64, 0x61, 0x6a, 0x67, + 0x61, 0x66, 0x6d, 0x60, 0x6b, 0x68, 0x66, 0x6d, + 0x9c, 0x9a, 0x91, 0x9f, 0x9a, 0x94, 0x9f, 0x92, + 0x99, 0x97, 0x92, 0x9c, 0x96, 0x99, 0x98, 0x95, + 0x90, 0x96, 0x9d, 0x91, 0x93, 0x9d, 0x94, 0x9e, + 0x9e, 0x90, 0x97, 0x9b, 0x95, 0x93, 0x9b, 0x98, + 0x99, 0x94, 0x9e, 0x93, 0x9f, 0x92, 0x95, 0x9c, + 0x92, 0x99, 0x98, 0x95, 0x9c, 0x9f, 0x93, 0x9a, + 0x97, 0x9b, 0x90, 0x9e, 0x94, 0x91, 0x9a, 0x97, + 0x91, 0x96, 0x9d, 0x90, 0x9b, 0x98, 0x96, 0x9d, + 0xfc, 0xfa, 0xf1, 0xff, 0xfa, 0xf4, 0xff, 0xf2, + 0xf9, 0xf7, 0xf2, 0xfc, 0xf6, 0xf9, 0xf8, 0xf5, + 0xf0, 0xf6, 0xfd, 0xf1, 0xf3, 0xfd, 0xf4, 0xfe, + 0xfe, 0xf0, 0xf7, 0xfb, 0xf5, 0xf3, 0xfb, 0xf8, + 0xf9, 0xf4, 0xfe, 0xf3, 0xff, 0xf2, 0xf5, 0xfc, + 0xf2, 0xf9, 0xf8, 0xf5, 0xfc, 0xff, 0xf3, 0xfa, + 0xf7, 0xfb, 0xf0, 0xfe, 0xf4, 0xf1, 0xfa, 0xf7, + 0xf1, 0xf6, 0xfd, 0xf0, 0xfb, 0xf8, 0xf6, 0xfd, + 0xcc, 0xca, 0xc1, 0xcf, 0xca, 0xc4, 0xcf, 0xc2, + 0xc9, 0xc7, 0xc2, 0xcc, 0xc6, 0xc9, 0xc8, 0xc5, + 0xc0, 0xc6, 0xcd, 0xc1, 0xc3, 0xcd, 0xc4, 0xce, + 0xce, 0xc0, 0xc7, 0xcb, 0xc5, 0xc3, 0xcb, 0xc8, + 0xc9, 0xc4, 0xce, 0xc3, 0xcf, 0xc2, 0xc5, 0xcc, + 0xc2, 0xc9, 0xc8, 0xc5, 0xcc, 0xcf, 0xc3, 0xca, + 0xc7, 0xcb, 0xc0, 0xce, 0xc4, 0xc1, 0xca, 0xc7, + 0xc1, 0xc6, 0xcd, 0xc0, 0xcb, 0xc8, 0xc6, 0xcd, + 0x0c, 0x0a, 0x01, 0x0f, 0x0a, 0x04, 0x0f, 0x02, + 0x09, 0x07, 0x02, 0x0c, 0x06, 0x09, 0x08, 0x05, + 0x00, 0x06, 0x0d, 0x01, 0x03, 0x0d, 0x04, 0x0e, + 0x0e, 0x00, 0x07, 0x0b, 0x05, 0x03, 0x0b, 0x08, + 0x09, 0x04, 0x0e, 0x03, 0x0f, 0x02, 0x05, 0x0c, + 0x02, 0x09, 0x08, 0x05, 0x0c, 0x0f, 0x03, 0x0a, + 0x07, 0x0b, 0x00, 0x0e, 0x04, 0x01, 0x0a, 0x07, + 0x01, 0x06, 0x0d, 0x00, 0x0b, 0x08, 0x06, 0x0d, + 0x5c, 0x5a, 0x51, 0x5f, 0x5a, 0x54, 0x5f, 0x52, + 0x59, 0x57, 0x52, 0x5c, 0x56, 0x59, 0x58, 0x55, + 0x50, 0x56, 0x5d, 0x51, 0x53, 0x5d, 0x54, 0x5e, + 0x5e, 0x50, 0x57, 0x5b, 0x55, 0x53, 0x5b, 0x58, + 0x59, 0x54, 0x5e, 0x53, 0x5f, 0x52, 0x55, 0x5c, + 0x52, 0x59, 0x58, 0x55, 0x5c, 0x5f, 0x53, 0x5a, + 0x57, 0x5b, 0x50, 0x5e, 0x54, 0x51, 0x5a, 0x57, + 0x51, 0x56, 0x5d, 0x50, 0x5b, 0x58, 0x56, 0x5d, + 0x9c, 0x9a, 0x91, 0x9f, 0x9a, 0x94, 0x9f, 0x92, + 0x99, 0x97, 0x92, 0x9c, 0x96, 0x99, 0x98, 0x95, + 0x90, 0x96, 0x9d, 0x91, 0x93, 0x9d, 0x94, 0x9e, + 0x9e, 0x90, 0x97, 0x9b, 0x95, 0x93, 0x9b, 0x98, + 0x99, 0x94, 0x9e, 0x93, 0x9f, 0x92, 0x95, 0x9c, + 0x92, 0x99, 0x98, 0x95, 0x9c, 0x9f, 0x93, 0x9a, + 0x97, 0x9b, 0x90, 0x9e, 0x94, 0x91, 0x9a, 0x97, + 0x91, 0x96, 0x9d, 0x90, 0x9b, 0x98, 0x96, 0x9d, + 0x6c, 0x6a, 0x61, 0x6f, 0x6a, 0x64, 0x6f, 0x62, + 0x69, 0x67, 0x62, 0x6c, 0x66, 0x69, 0x68, 0x65, + 0x60, 0x66, 0x6d, 0x61, 0x63, 0x6d, 0x64, 0x6e, + 0x6e, 0x60, 0x67, 0x6b, 0x65, 0x63, 0x6b, 0x68, + 0x69, 0x64, 0x6e, 0x63, 0x6f, 0x62, 0x65, 0x6c, + 0x62, 0x69, 0x68, 0x65, 0x6c, 0x6f, 0x63, 0x6a, + 0x67, 0x6b, 0x60, 0x6e, 0x64, 0x61, 0x6a, 0x67, + 0x61, 0x66, 0x6d, 0x60, 0x6b, 0x68, 0x66, 0x6d, + 0xac, 0xaa, 0xa1, 0xaf, 0xaa, 0xa4, 0xaf, 0xa2, + 0xa9, 0xa7, 0xa2, 0xac, 0xa6, 0xa9, 0xa8, 0xa5, + 0xa0, 0xa6, 0xad, 0xa1, 0xa3, 0xad, 0xa4, 0xae, + 0xae, 0xa0, 0xa7, 0xab, 0xa5, 0xa3, 0xab, 0xa8, + 0xa9, 0xa4, 0xae, 0xa3, 0xaf, 0xa2, 0xa5, 0xac, + 0xa2, 0xa9, 0xa8, 0xa5, 0xac, 0xaf, 0xa3, 0xaa, + 0xa7, 0xab, 0xa0, 0xae, 0xa4, 0xa1, 0xaa, 0xa7, + 0xa1, 0xa6, 0xad, 0xa0, 0xab, 0xa8, 0xa6, 0xad, + 0x3c, 0x3a, 0x31, 0x3f, 0x3a, 0x34, 0x3f, 0x32, + 0x39, 0x37, 0x32, 0x3c, 0x36, 0x39, 0x38, 0x35, + 0x30, 0x36, 0x3d, 0x31, 0x33, 0x3d, 0x34, 0x3e, + 0x3e, 0x30, 0x37, 0x3b, 0x35, 0x33, 0x3b, 0x38, + 0x39, 0x34, 0x3e, 0x33, 0x3f, 0x32, 0x35, 0x3c, + 0x32, 0x39, 0x38, 0x35, 0x3c, 0x3f, 0x33, 0x3a, + 0x37, 0x3b, 0x30, 0x3e, 0x34, 0x31, 0x3a, 0x37, + 0x31, 0x36, 0x3d, 0x30, 0x3b, 0x38, 0x36, 0x3d, + 0x4c, 0x4a, 0x41, 0x4f, 0x4a, 0x44, 0x4f, 0x42, + 0x49, 0x47, 0x42, 0x4c, 0x46, 0x49, 0x48, 0x45, + 0x40, 0x46, 0x4d, 0x41, 0x43, 0x4d, 0x44, 0x4e, + 0x4e, 0x40, 0x47, 0x4b, 0x45, 0x43, 0x4b, 0x48, + 0x49, 0x44, 0x4e, 0x43, 0x4f, 0x42, 0x45, 0x4c, + 0x42, 0x49, 0x48, 0x45, 0x4c, 0x4f, 0x43, 0x4a, + 0x47, 0x4b, 0x40, 0x4e, 0x44, 0x41, 0x4a, 0x47, + 0x41, 0x46, 0x4d, 0x40, 0x4b, 0x48, 0x46, 0x4d, + 0x0c, 0x0a, 0x01, 0x0f, 0x0a, 0x04, 0x0f, 0x02, + 0x09, 0x07, 0x02, 0x0c, 0x06, 0x09, 0x08, 0x05, + 0x00, 0x06, 0x0d, 0x01, 0x03, 0x0d, 0x04, 0x0e, + 0x0e, 0x00, 0x07, 0x0b, 0x05, 0x03, 0x0b, 0x08, + 0x09, 0x04, 0x0e, 0x03, 0x0f, 0x02, 0x05, 0x0c, + 0x02, 0x09, 0x08, 0x05, 0x0c, 0x0f, 0x03, 0x0a, + 0x07, 0x0b, 0x00, 0x0e, 0x04, 0x01, 0x0a, 0x07, + 0x01, 0x06, 0x0d, 0x00, 0x0b, 0x08, 0x06, 0x0d, + 0x5c, 0x5a, 0x51, 0x5f, 0x5a, 0x54, 0x5f, 0x52, + 0x59, 0x57, 0x52, 0x5c, 0x56, 0x59, 0x58, 0x55, + 0x50, 0x56, 0x5d, 0x51, 0x53, 0x5d, 0x54, 0x5e, + 0x5e, 0x50, 0x57, 0x5b, 0x55, 0x53, 0x5b, 0x58, + 0x59, 0x54, 0x5e, 0x53, 0x5f, 0x52, 0x55, 0x5c, + 0x52, 0x59, 0x58, 0x55, 0x5c, 0x5f, 0x53, 0x5a, + 0x57, 0x5b, 0x50, 0x5e, 0x54, 0x51, 0x5a, 0x57, + 0x51, 0x56, 0x5d, 0x50, 0x5b, 0x58, 0x56, 0x5d, + 0xec, 0xea, 0xe1, 0xef, 0xea, 0xe4, 0xef, 0xe2, + 0xe9, 0xe7, 0xe2, 0xec, 0xe6, 0xe9, 0xe8, 0xe5, + 0xe0, 0xe6, 0xed, 0xe1, 0xe3, 0xed, 0xe4, 0xee, + 0xee, 0xe0, 0xe7, 0xeb, 0xe5, 0xe3, 0xeb, 0xe8, + 0xe9, 0xe4, 0xee, 0xe3, 0xef, 0xe2, 0xe5, 0xec, + 0xe2, 0xe9, 0xe8, 0xe5, 0xec, 0xef, 0xe3, 0xea, + 0xe7, 0xeb, 0xe0, 0xee, 0xe4, 0xe1, 0xea, 0xe7, + 0xe1, 0xe6, 0xed, 0xe0, 0xeb, 0xe8, 0xe6, 0xed, + 0x3c, 0x3a, 0x31, 0x3f, 0x3a, 0x34, 0x3f, 0x32, + 0x39, 0x37, 0x32, 0x3c, 0x36, 0x39, 0x38, 0x35, + 0x30, 0x36, 0x3d, 0x31, 0x33, 0x3d, 0x34, 0x3e, + 0x3e, 0x30, 0x37, 0x3b, 0x35, 0x33, 0x3b, 0x38, + 0x39, 0x34, 0x3e, 0x33, 0x3f, 0x32, 0x35, 0x3c, + 0x32, 0x39, 0x38, 0x35, 0x3c, 0x3f, 0x33, 0x3a, + 0x37, 0x3b, 0x30, 0x3e, 0x34, 0x31, 0x3a, 0x37, + 0x31, 0x36, 0x3d, 0x30, 0x3b, 0x38, 0x36, 0x3d, + },{ + 0x4d, 0x41, 0x42, 0x4f, 0x48, 0x4d, 0x44, 0x48, + 0x46, 0x4a, 0x4f, 0x43, 0x4b, 0x47, 0x41, 0x44, + 0x4a, 0x4c, 0x49, 0x45, 0x43, 0x46, 0x4e, 0x4b, + 0x45, 0x40, 0x40, 0x4e, 0x4c, 0x49, 0x47, 0x42, + 0x47, 0x42, 0x4b, 0x41, 0x44, 0x4e, 0x41, 0x47, + 0x49, 0x44, 0x4c, 0x4a, 0x4e, 0x48, 0x42, 0x4d, + 0x40, 0x4f, 0x46, 0x4c, 0x4a, 0x49, 0x4d, 0x40, + 0x4f, 0x43, 0x43, 0x45, 0x45, 0x46, 0x48, 0x4b, + 0xdd, 0xd1, 0xd2, 0xdf, 0xd8, 0xdd, 0xd4, 0xd8, + 0xd6, 0xda, 0xdf, 0xd3, 0xdb, 0xd7, 0xd1, 0xd4, + 0xda, 0xdc, 0xd9, 0xd5, 0xd3, 0xd6, 0xde, 0xdb, + 0xd5, 0xd0, 0xd0, 0xde, 0xdc, 0xd9, 0xd7, 0xd2, + 0xd7, 0xd2, 0xdb, 0xd1, 0xd4, 0xde, 0xd1, 0xd7, + 0xd9, 0xd4, 0xdc, 0xda, 0xde, 0xd8, 0xd2, 0xdd, + 0xd0, 0xdf, 0xd6, 0xdc, 0xda, 0xd9, 0xdd, 0xd0, + 0xdf, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd8, 0xdb, + 0xbd, 0xb1, 0xb2, 0xbf, 0xb8, 0xbd, 0xb4, 0xb8, + 0xb6, 0xba, 0xbf, 0xb3, 0xbb, 0xb7, 0xb1, 0xb4, + 0xba, 0xbc, 0xb9, 0xb5, 0xb3, 0xb6, 0xbe, 0xbb, + 0xb5, 0xb0, 0xb0, 0xbe, 0xbc, 0xb9, 0xb7, 0xb2, + 0xb7, 0xb2, 0xbb, 0xb1, 0xb4, 0xbe, 0xb1, 0xb7, + 0xb9, 0xb4, 0xbc, 0xba, 0xbe, 0xb8, 0xb2, 0xbd, + 0xb0, 0xbf, 0xb6, 0xbc, 0xba, 0xb9, 0xbd, 0xb0, + 0xbf, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb8, 0xbb, + 0x0d, 0x01, 0x02, 0x0f, 0x08, 0x0d, 0x04, 0x08, + 0x06, 0x0a, 0x0f, 0x03, 0x0b, 0x07, 0x01, 0x04, + 0x0a, 0x0c, 0x09, 0x05, 0x03, 0x06, 0x0e, 0x0b, + 0x05, 0x00, 0x00, 0x0e, 0x0c, 0x09, 0x07, 0x02, + 0x07, 0x02, 0x0b, 0x01, 0x04, 0x0e, 0x01, 0x07, + 0x09, 0x04, 0x0c, 0x0a, 0x0e, 0x08, 0x02, 0x0d, + 0x00, 0x0f, 0x06, 0x0c, 0x0a, 0x09, 0x0d, 0x00, + 0x0f, 0x03, 0x03, 0x05, 0x05, 0x06, 0x08, 0x0b, + 0x2d, 0x21, 0x22, 0x2f, 0x28, 0x2d, 0x24, 0x28, + 0x26, 0x2a, 0x2f, 0x23, 0x2b, 0x27, 0x21, 0x24, + 0x2a, 0x2c, 0x29, 0x25, 0x23, 0x26, 0x2e, 0x2b, + 0x25, 0x20, 0x20, 0x2e, 0x2c, 0x29, 0x27, 0x22, + 0x27, 0x22, 0x2b, 0x21, 0x24, 0x2e, 0x21, 0x27, + 0x29, 0x24, 0x2c, 0x2a, 0x2e, 0x28, 0x22, 0x2d, + 0x20, 0x2f, 0x26, 0x2c, 0x2a, 0x29, 0x2d, 0x20, + 0x2f, 0x23, 0x23, 0x25, 0x25, 0x26, 0x28, 0x2b, + 0xbd, 0xb1, 0xb2, 0xbf, 0xb8, 0xbd, 0xb4, 0xb8, + 0xb6, 0xba, 0xbf, 0xb3, 0xbb, 0xb7, 0xb1, 0xb4, + 0xba, 0xbc, 0xb9, 0xb5, 0xb3, 0xb6, 0xbe, 0xbb, + 0xb5, 0xb0, 0xb0, 0xbe, 0xbc, 0xb9, 0xb7, 0xb2, + 0xb7, 0xb2, 0xbb, 0xb1, 0xb4, 0xbe, 0xb1, 0xb7, + 0xb9, 0xb4, 0xbc, 0xba, 0xbe, 0xb8, 0xb2, 0xbd, + 0xb0, 0xbf, 0xb6, 0xbc, 0xba, 0xb9, 0xbd, 0xb0, + 0xbf, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb8, 0xbb, + 0xed, 0xe1, 0xe2, 0xef, 0xe8, 0xed, 0xe4, 0xe8, + 0xe6, 0xea, 0xef, 0xe3, 0xeb, 0xe7, 0xe1, 0xe4, + 0xea, 0xec, 0xe9, 0xe5, 0xe3, 0xe6, 0xee, 0xeb, + 0xe5, 0xe0, 0xe0, 0xee, 0xec, 0xe9, 0xe7, 0xe2, + 0xe7, 0xe2, 0xeb, 0xe1, 0xe4, 0xee, 0xe1, 0xe7, + 0xe9, 0xe4, 0xec, 0xea, 0xee, 0xe8, 0xe2, 0xed, + 0xe0, 0xef, 0xe6, 0xec, 0xea, 0xe9, 0xed, 0xe0, + 0xef, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe8, 0xeb, + 0x7d, 0x71, 0x72, 0x7f, 0x78, 0x7d, 0x74, 0x78, + 0x76, 0x7a, 0x7f, 0x73, 0x7b, 0x77, 0x71, 0x74, + 0x7a, 0x7c, 0x79, 0x75, 0x73, 0x76, 0x7e, 0x7b, + 0x75, 0x70, 0x70, 0x7e, 0x7c, 0x79, 0x77, 0x72, + 0x77, 0x72, 0x7b, 0x71, 0x74, 0x7e, 0x71, 0x77, + 0x79, 0x74, 0x7c, 0x7a, 0x7e, 0x78, 0x72, 0x7d, + 0x70, 0x7f, 0x76, 0x7c, 0x7a, 0x79, 0x7d, 0x70, + 0x7f, 0x73, 0x73, 0x75, 0x75, 0x76, 0x78, 0x7b, + 0xfd, 0xf1, 0xf2, 0xff, 0xf8, 0xfd, 0xf4, 0xf8, + 0xf6, 0xfa, 0xff, 0xf3, 0xfb, 0xf7, 0xf1, 0xf4, + 0xfa, 0xfc, 0xf9, 0xf5, 0xf3, 0xf6, 0xfe, 0xfb, + 0xf5, 0xf0, 0xf0, 0xfe, 0xfc, 0xf9, 0xf7, 0xf2, + 0xf7, 0xf2, 0xfb, 0xf1, 0xf4, 0xfe, 0xf1, 0xf7, + 0xf9, 0xf4, 0xfc, 0xfa, 0xfe, 0xf8, 0xf2, 0xfd, + 0xf0, 0xff, 0xf6, 0xfc, 0xfa, 0xf9, 0xfd, 0xf0, + 0xff, 0xf3, 0xf3, 0xf5, 0xf5, 0xf6, 0xf8, 0xfb, + 0x4d, 0x41, 0x42, 0x4f, 0x48, 0x4d, 0x44, 0x48, + 0x46, 0x4a, 0x4f, 0x43, 0x4b, 0x47, 0x41, 0x44, + 0x4a, 0x4c, 0x49, 0x45, 0x43, 0x46, 0x4e, 0x4b, + 0x45, 0x40, 0x40, 0x4e, 0x4c, 0x49, 0x47, 0x42, + 0x47, 0x42, 0x4b, 0x41, 0x44, 0x4e, 0x41, 0x47, + 0x49, 0x44, 0x4c, 0x4a, 0x4e, 0x48, 0x42, 0x4d, + 0x40, 0x4f, 0x46, 0x4c, 0x4a, 0x49, 0x4d, 0x40, + 0x4f, 0x43, 0x43, 0x45, 0x45, 0x46, 0x48, 0x4b, + 0x0d, 0x01, 0x02, 0x0f, 0x08, 0x0d, 0x04, 0x08, + 0x06, 0x0a, 0x0f, 0x03, 0x0b, 0x07, 0x01, 0x04, + 0x0a, 0x0c, 0x09, 0x05, 0x03, 0x06, 0x0e, 0x0b, + 0x05, 0x00, 0x00, 0x0e, 0x0c, 0x09, 0x07, 0x02, + 0x07, 0x02, 0x0b, 0x01, 0x04, 0x0e, 0x01, 0x07, + 0x09, 0x04, 0x0c, 0x0a, 0x0e, 0x08, 0x02, 0x0d, + 0x00, 0x0f, 0x06, 0x0c, 0x0a, 0x09, 0x0d, 0x00, + 0x0f, 0x03, 0x03, 0x05, 0x05, 0x06, 0x08, 0x0b, + 0x9d, 0x91, 0x92, 0x9f, 0x98, 0x9d, 0x94, 0x98, + 0x96, 0x9a, 0x9f, 0x93, 0x9b, 0x97, 0x91, 0x94, + 0x9a, 0x9c, 0x99, 0x95, 0x93, 0x96, 0x9e, 0x9b, + 0x95, 0x90, 0x90, 0x9e, 0x9c, 0x99, 0x97, 0x92, + 0x97, 0x92, 0x9b, 0x91, 0x94, 0x9e, 0x91, 0x97, + 0x99, 0x94, 0x9c, 0x9a, 0x9e, 0x98, 0x92, 0x9d, + 0x90, 0x9f, 0x96, 0x9c, 0x9a, 0x99, 0x9d, 0x90, + 0x9f, 0x93, 0x93, 0x95, 0x95, 0x96, 0x98, 0x9b, + 0x8d, 0x81, 0x82, 0x8f, 0x88, 0x8d, 0x84, 0x88, + 0x86, 0x8a, 0x8f, 0x83, 0x8b, 0x87, 0x81, 0x84, + 0x8a, 0x8c, 0x89, 0x85, 0x83, 0x86, 0x8e, 0x8b, + 0x85, 0x80, 0x80, 0x8e, 0x8c, 0x89, 0x87, 0x82, + 0x87, 0x82, 0x8b, 0x81, 0x84, 0x8e, 0x81, 0x87, + 0x89, 0x84, 0x8c, 0x8a, 0x8e, 0x88, 0x82, 0x8d, + 0x80, 0x8f, 0x86, 0x8c, 0x8a, 0x89, 0x8d, 0x80, + 0x8f, 0x83, 0x83, 0x85, 0x85, 0x86, 0x88, 0x8b, + 0x1d, 0x11, 0x12, 0x1f, 0x18, 0x1d, 0x14, 0x18, + 0x16, 0x1a, 0x1f, 0x13, 0x1b, 0x17, 0x11, 0x14, + 0x1a, 0x1c, 0x19, 0x15, 0x13, 0x16, 0x1e, 0x1b, + 0x15, 0x10, 0x10, 0x1e, 0x1c, 0x19, 0x17, 0x12, + 0x17, 0x12, 0x1b, 0x11, 0x14, 0x1e, 0x11, 0x17, + 0x19, 0x14, 0x1c, 0x1a, 0x1e, 0x18, 0x12, 0x1d, + 0x10, 0x1f, 0x16, 0x1c, 0x1a, 0x19, 0x1d, 0x10, + 0x1f, 0x13, 0x13, 0x15, 0x15, 0x16, 0x18, 0x1b, + 0xdd, 0xd1, 0xd2, 0xdf, 0xd8, 0xdd, 0xd4, 0xd8, + 0xd6, 0xda, 0xdf, 0xd3, 0xdb, 0xd7, 0xd1, 0xd4, + 0xda, 0xdc, 0xd9, 0xd5, 0xd3, 0xd6, 0xde, 0xdb, + 0xd5, 0xd0, 0xd0, 0xde, 0xdc, 0xd9, 0xd7, 0xd2, + 0xd7, 0xd2, 0xdb, 0xd1, 0xd4, 0xde, 0xd1, 0xd7, + 0xd9, 0xd4, 0xdc, 0xda, 0xde, 0xd8, 0xd2, 0xdd, + 0xd0, 0xdf, 0xd6, 0xdc, 0xda, 0xd9, 0xdd, 0xd0, + 0xdf, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd8, 0xdb, + 0xad, 0xa1, 0xa2, 0xaf, 0xa8, 0xad, 0xa4, 0xa8, + 0xa6, 0xaa, 0xaf, 0xa3, 0xab, 0xa7, 0xa1, 0xa4, + 0xaa, 0xac, 0xa9, 0xa5, 0xa3, 0xa6, 0xae, 0xab, + 0xa5, 0xa0, 0xa0, 0xae, 0xac, 0xa9, 0xa7, 0xa2, + 0xa7, 0xa2, 0xab, 0xa1, 0xa4, 0xae, 0xa1, 0xa7, + 0xa9, 0xa4, 0xac, 0xaa, 0xae, 0xa8, 0xa2, 0xad, + 0xa0, 0xaf, 0xa6, 0xac, 0xaa, 0xa9, 0xad, 0xa0, + 0xaf, 0xa3, 0xa3, 0xa5, 0xa5, 0xa6, 0xa8, 0xab, + 0x3d, 0x31, 0x32, 0x3f, 0x38, 0x3d, 0x34, 0x38, + 0x36, 0x3a, 0x3f, 0x33, 0x3b, 0x37, 0x31, 0x34, + 0x3a, 0x3c, 0x39, 0x35, 0x33, 0x36, 0x3e, 0x3b, + 0x35, 0x30, 0x30, 0x3e, 0x3c, 0x39, 0x37, 0x32, + 0x37, 0x32, 0x3b, 0x31, 0x34, 0x3e, 0x31, 0x37, + 0x39, 0x34, 0x3c, 0x3a, 0x3e, 0x38, 0x32, 0x3d, + 0x30, 0x3f, 0x36, 0x3c, 0x3a, 0x39, 0x3d, 0x30, + 0x3f, 0x33, 0x33, 0x35, 0x35, 0x36, 0x38, 0x3b, + 0xed, 0xe1, 0xe2, 0xef, 0xe8, 0xed, 0xe4, 0xe8, + 0xe6, 0xea, 0xef, 0xe3, 0xeb, 0xe7, 0xe1, 0xe4, + 0xea, 0xec, 0xe9, 0xe5, 0xe3, 0xe6, 0xee, 0xeb, + 0xe5, 0xe0, 0xe0, 0xee, 0xec, 0xe9, 0xe7, 0xe2, + 0xe7, 0xe2, 0xeb, 0xe1, 0xe4, 0xee, 0xe1, 0xe7, + 0xe9, 0xe4, 0xec, 0xea, 0xee, 0xe8, 0xe2, 0xed, + 0xe0, 0xef, 0xe6, 0xec, 0xea, 0xe9, 0xed, 0xe0, + 0xef, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe8, 0xeb, + 0xcd, 0xc1, 0xc2, 0xcf, 0xc8, 0xcd, 0xc4, 0xc8, + 0xc6, 0xca, 0xcf, 0xc3, 0xcb, 0xc7, 0xc1, 0xc4, + 0xca, 0xcc, 0xc9, 0xc5, 0xc3, 0xc6, 0xce, 0xcb, + 0xc5, 0xc0, 0xc0, 0xce, 0xcc, 0xc9, 0xc7, 0xc2, + 0xc7, 0xc2, 0xcb, 0xc1, 0xc4, 0xce, 0xc1, 0xc7, + 0xc9, 0xc4, 0xcc, 0xca, 0xce, 0xc8, 0xc2, 0xcd, + 0xc0, 0xcf, 0xc6, 0xcc, 0xca, 0xc9, 0xcd, 0xc0, + 0xcf, 0xc3, 0xc3, 0xc5, 0xc5, 0xc6, 0xc8, 0xcb, + 0x3d, 0x31, 0x32, 0x3f, 0x38, 0x3d, 0x34, 0x38, + 0x36, 0x3a, 0x3f, 0x33, 0x3b, 0x37, 0x31, 0x34, + 0x3a, 0x3c, 0x39, 0x35, 0x33, 0x36, 0x3e, 0x3b, + 0x35, 0x30, 0x30, 0x3e, 0x3c, 0x39, 0x37, 0x32, + 0x37, 0x32, 0x3b, 0x31, 0x34, 0x3e, 0x31, 0x37, + 0x39, 0x34, 0x3c, 0x3a, 0x3e, 0x38, 0x32, 0x3d, + 0x30, 0x3f, 0x36, 0x3c, 0x3a, 0x39, 0x3d, 0x30, + 0x3f, 0x33, 0x33, 0x35, 0x35, 0x36, 0x38, 0x3b, + 0x9d, 0x91, 0x92, 0x9f, 0x98, 0x9d, 0x94, 0x98, + 0x96, 0x9a, 0x9f, 0x93, 0x9b, 0x97, 0x91, 0x94, + 0x9a, 0x9c, 0x99, 0x95, 0x93, 0x96, 0x9e, 0x9b, + 0x95, 0x90, 0x90, 0x9e, 0x9c, 0x99, 0x97, 0x92, + 0x97, 0x92, 0x9b, 0x91, 0x94, 0x9e, 0x91, 0x97, + 0x99, 0x94, 0x9c, 0x9a, 0x9e, 0x98, 0x92, 0x9d, + 0x90, 0x9f, 0x96, 0x9c, 0x9a, 0x99, 0x9d, 0x90, + 0x9f, 0x93, 0x93, 0x95, 0x95, 0x96, 0x98, 0x9b, + 0x5d, 0x51, 0x52, 0x5f, 0x58, 0x5d, 0x54, 0x58, + 0x56, 0x5a, 0x5f, 0x53, 0x5b, 0x57, 0x51, 0x54, + 0x5a, 0x5c, 0x59, 0x55, 0x53, 0x56, 0x5e, 0x5b, + 0x55, 0x50, 0x50, 0x5e, 0x5c, 0x59, 0x57, 0x52, + 0x57, 0x52, 0x5b, 0x51, 0x54, 0x5e, 0x51, 0x57, + 0x59, 0x54, 0x5c, 0x5a, 0x5e, 0x58, 0x52, 0x5d, + 0x50, 0x5f, 0x56, 0x5c, 0x5a, 0x59, 0x5d, 0x50, + 0x5f, 0x53, 0x53, 0x55, 0x55, 0x56, 0x58, 0x5b, + 0x7d, 0x71, 0x72, 0x7f, 0x78, 0x7d, 0x74, 0x78, + 0x76, 0x7a, 0x7f, 0x73, 0x7b, 0x77, 0x71, 0x74, + 0x7a, 0x7c, 0x79, 0x75, 0x73, 0x76, 0x7e, 0x7b, + 0x75, 0x70, 0x70, 0x7e, 0x7c, 0x79, 0x77, 0x72, + 0x77, 0x72, 0x7b, 0x71, 0x74, 0x7e, 0x71, 0x77, + 0x79, 0x74, 0x7c, 0x7a, 0x7e, 0x78, 0x72, 0x7d, + 0x70, 0x7f, 0x76, 0x7c, 0x7a, 0x79, 0x7d, 0x70, + 0x7f, 0x73, 0x73, 0x75, 0x75, 0x76, 0x78, 0x7b, + 0xcd, 0xc1, 0xc2, 0xcf, 0xc8, 0xcd, 0xc4, 0xc8, + 0xc6, 0xca, 0xcf, 0xc3, 0xcb, 0xc7, 0xc1, 0xc4, + 0xca, 0xcc, 0xc9, 0xc5, 0xc3, 0xc6, 0xce, 0xcb, + 0xc5, 0xc0, 0xc0, 0xce, 0xcc, 0xc9, 0xc7, 0xc2, + 0xc7, 0xc2, 0xcb, 0xc1, 0xc4, 0xce, 0xc1, 0xc7, + 0xc9, 0xc4, 0xcc, 0xca, 0xce, 0xc8, 0xc2, 0xcd, + 0xc0, 0xcf, 0xc6, 0xcc, 0xca, 0xc9, 0xcd, 0xc0, + 0xcf, 0xc3, 0xc3, 0xc5, 0xc5, 0xc6, 0xc8, 0xcb, + 0x5d, 0x51, 0x52, 0x5f, 0x58, 0x5d, 0x54, 0x58, + 0x56, 0x5a, 0x5f, 0x53, 0x5b, 0x57, 0x51, 0x54, + 0x5a, 0x5c, 0x59, 0x55, 0x53, 0x56, 0x5e, 0x5b, + 0x55, 0x50, 0x50, 0x5e, 0x5c, 0x59, 0x57, 0x52, + 0x57, 0x52, 0x5b, 0x51, 0x54, 0x5e, 0x51, 0x57, + 0x59, 0x54, 0x5c, 0x5a, 0x5e, 0x58, 0x52, 0x5d, + 0x50, 0x5f, 0x56, 0x5c, 0x5a, 0x59, 0x5d, 0x50, + 0x5f, 0x53, 0x53, 0x55, 0x55, 0x56, 0x58, 0x5b, + 0x2d, 0x21, 0x22, 0x2f, 0x28, 0x2d, 0x24, 0x28, + 0x26, 0x2a, 0x2f, 0x23, 0x2b, 0x27, 0x21, 0x24, + 0x2a, 0x2c, 0x29, 0x25, 0x23, 0x26, 0x2e, 0x2b, + 0x25, 0x20, 0x20, 0x2e, 0x2c, 0x29, 0x27, 0x22, + 0x27, 0x22, 0x2b, 0x21, 0x24, 0x2e, 0x21, 0x27, + 0x29, 0x24, 0x2c, 0x2a, 0x2e, 0x28, 0x22, 0x2d, + 0x20, 0x2f, 0x26, 0x2c, 0x2a, 0x29, 0x2d, 0x20, + 0x2f, 0x23, 0x23, 0x25, 0x25, 0x26, 0x28, 0x2b, + 0xad, 0xa1, 0xa2, 0xaf, 0xa8, 0xad, 0xa4, 0xa8, + 0xa6, 0xaa, 0xaf, 0xa3, 0xab, 0xa7, 0xa1, 0xa4, + 0xaa, 0xac, 0xa9, 0xa5, 0xa3, 0xa6, 0xae, 0xab, + 0xa5, 0xa0, 0xa0, 0xae, 0xac, 0xa9, 0xa7, 0xa2, + 0xa7, 0xa2, 0xab, 0xa1, 0xa4, 0xae, 0xa1, 0xa7, + 0xa9, 0xa4, 0xac, 0xaa, 0xae, 0xa8, 0xa2, 0xad, + 0xa0, 0xaf, 0xa6, 0xac, 0xaa, 0xa9, 0xad, 0xa0, + 0xaf, 0xa3, 0xa3, 0xa5, 0xa5, 0xa6, 0xa8, 0xab, + 0xfd, 0xf1, 0xf2, 0xff, 0xf8, 0xfd, 0xf4, 0xf8, + 0xf6, 0xfa, 0xff, 0xf3, 0xfb, 0xf7, 0xf1, 0xf4, + 0xfa, 0xfc, 0xf9, 0xf5, 0xf3, 0xf6, 0xfe, 0xfb, + 0xf5, 0xf0, 0xf0, 0xfe, 0xfc, 0xf9, 0xf7, 0xf2, + 0xf7, 0xf2, 0xfb, 0xf1, 0xf4, 0xfe, 0xf1, 0xf7, + 0xf9, 0xf4, 0xfc, 0xfa, 0xfe, 0xf8, 0xf2, 0xfd, + 0xf0, 0xff, 0xf6, 0xfc, 0xfa, 0xf9, 0xfd, 0xf0, + 0xff, 0xf3, 0xf3, 0xf5, 0xf5, 0xf6, 0xf8, 0xfb, + 0x6d, 0x61, 0x62, 0x6f, 0x68, 0x6d, 0x64, 0x68, + 0x66, 0x6a, 0x6f, 0x63, 0x6b, 0x67, 0x61, 0x64, + 0x6a, 0x6c, 0x69, 0x65, 0x63, 0x66, 0x6e, 0x6b, + 0x65, 0x60, 0x60, 0x6e, 0x6c, 0x69, 0x67, 0x62, + 0x67, 0x62, 0x6b, 0x61, 0x64, 0x6e, 0x61, 0x67, + 0x69, 0x64, 0x6c, 0x6a, 0x6e, 0x68, 0x62, 0x6d, + 0x60, 0x6f, 0x66, 0x6c, 0x6a, 0x69, 0x6d, 0x60, + 0x6f, 0x63, 0x63, 0x65, 0x65, 0x66, 0x68, 0x6b, + 0x8d, 0x81, 0x82, 0x8f, 0x88, 0x8d, 0x84, 0x88, + 0x86, 0x8a, 0x8f, 0x83, 0x8b, 0x87, 0x81, 0x84, + 0x8a, 0x8c, 0x89, 0x85, 0x83, 0x86, 0x8e, 0x8b, + 0x85, 0x80, 0x80, 0x8e, 0x8c, 0x89, 0x87, 0x82, + 0x87, 0x82, 0x8b, 0x81, 0x84, 0x8e, 0x81, 0x87, + 0x89, 0x84, 0x8c, 0x8a, 0x8e, 0x88, 0x82, 0x8d, + 0x80, 0x8f, 0x86, 0x8c, 0x8a, 0x89, 0x8d, 0x80, + 0x8f, 0x83, 0x83, 0x85, 0x85, 0x86, 0x88, 0x8b, + 0x1d, 0x11, 0x12, 0x1f, 0x18, 0x1d, 0x14, 0x18, + 0x16, 0x1a, 0x1f, 0x13, 0x1b, 0x17, 0x11, 0x14, + 0x1a, 0x1c, 0x19, 0x15, 0x13, 0x16, 0x1e, 0x1b, + 0x15, 0x10, 0x10, 0x1e, 0x1c, 0x19, 0x17, 0x12, + 0x17, 0x12, 0x1b, 0x11, 0x14, 0x1e, 0x11, 0x17, + 0x19, 0x14, 0x1c, 0x1a, 0x1e, 0x18, 0x12, 0x1d, + 0x10, 0x1f, 0x16, 0x1c, 0x1a, 0x19, 0x1d, 0x10, + 0x1f, 0x13, 0x13, 0x15, 0x15, 0x16, 0x18, 0x1b, + 0x6d, 0x61, 0x62, 0x6f, 0x68, 0x6d, 0x64, 0x68, + 0x66, 0x6a, 0x6f, 0x63, 0x6b, 0x67, 0x61, 0x64, + 0x6a, 0x6c, 0x69, 0x65, 0x63, 0x66, 0x6e, 0x6b, + 0x65, 0x60, 0x60, 0x6e, 0x6c, 0x69, 0x67, 0x62, + 0x67, 0x62, 0x6b, 0x61, 0x64, 0x6e, 0x61, 0x67, + 0x69, 0x64, 0x6c, 0x6a, 0x6e, 0x68, 0x62, 0x6d, + 0x60, 0x6f, 0x66, 0x6c, 0x6a, 0x69, 0x6d, 0x60, + 0x6f, 0x63, 0x63, 0x65, 0x65, 0x66, 0x68, 0x6b, + 0x1d, 0x11, 0x12, 0x1f, 0x18, 0x1d, 0x14, 0x18, + 0x16, 0x1a, 0x1f, 0x13, 0x1b, 0x17, 0x11, 0x14, + 0x1a, 0x1c, 0x19, 0x15, 0x13, 0x16, 0x1e, 0x1b, + 0x15, 0x10, 0x10, 0x1e, 0x1c, 0x19, 0x17, 0x12, + 0x17, 0x12, 0x1b, 0x11, 0x14, 0x1e, 0x11, 0x17, + 0x19, 0x14, 0x1c, 0x1a, 0x1e, 0x18, 0x12, 0x1d, + 0x10, 0x1f, 0x16, 0x1c, 0x1a, 0x19, 0x1d, 0x10, + 0x1f, 0x13, 0x13, 0x15, 0x15, 0x16, 0x18, 0x1b, + 0x6d, 0x61, 0x62, 0x6f, 0x68, 0x6d, 0x64, 0x68, + 0x66, 0x6a, 0x6f, 0x63, 0x6b, 0x67, 0x61, 0x64, + 0x6a, 0x6c, 0x69, 0x65, 0x63, 0x66, 0x6e, 0x6b, + 0x65, 0x60, 0x60, 0x6e, 0x6c, 0x69, 0x67, 0x62, + 0x67, 0x62, 0x6b, 0x61, 0x64, 0x6e, 0x61, 0x67, + 0x69, 0x64, 0x6c, 0x6a, 0x6e, 0x68, 0x62, 0x6d, + 0x60, 0x6f, 0x66, 0x6c, 0x6a, 0x69, 0x6d, 0x60, + 0x6f, 0x63, 0x63, 0x65, 0x65, 0x66, 0x68, 0x6b, + 0x4d, 0x41, 0x42, 0x4f, 0x48, 0x4d, 0x44, 0x48, + 0x46, 0x4a, 0x4f, 0x43, 0x4b, 0x47, 0x41, 0x44, + 0x4a, 0x4c, 0x49, 0x45, 0x43, 0x46, 0x4e, 0x4b, + 0x45, 0x40, 0x40, 0x4e, 0x4c, 0x49, 0x47, 0x42, + 0x47, 0x42, 0x4b, 0x41, 0x44, 0x4e, 0x41, 0x47, + 0x49, 0x44, 0x4c, 0x4a, 0x4e, 0x48, 0x42, 0x4d, + 0x40, 0x4f, 0x46, 0x4c, 0x4a, 0x49, 0x4d, 0x40, + 0x4f, 0x43, 0x43, 0x45, 0x45, 0x46, 0x48, 0x4b, + 0xbd, 0xb1, 0xb2, 0xbf, 0xb8, 0xbd, 0xb4, 0xb8, + 0xb6, 0xba, 0xbf, 0xb3, 0xbb, 0xb7, 0xb1, 0xb4, + 0xba, 0xbc, 0xb9, 0xb5, 0xb3, 0xb6, 0xbe, 0xbb, + 0xb5, 0xb0, 0xb0, 0xbe, 0xbc, 0xb9, 0xb7, 0xb2, + 0xb7, 0xb2, 0xbb, 0xb1, 0xb4, 0xbe, 0xb1, 0xb7, + 0xb9, 0xb4, 0xbc, 0xba, 0xbe, 0xb8, 0xb2, 0xbd, + 0xb0, 0xbf, 0xb6, 0xbc, 0xba, 0xb9, 0xbd, 0xb0, + 0xbf, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb8, 0xbb, + 0xbd, 0xb1, 0xb2, 0xbf, 0xb8, 0xbd, 0xb4, 0xb8, + 0xb6, 0xba, 0xbf, 0xb3, 0xbb, 0xb7, 0xb1, 0xb4, + 0xba, 0xbc, 0xb9, 0xb5, 0xb3, 0xb6, 0xbe, 0xbb, + 0xb5, 0xb0, 0xb0, 0xbe, 0xbc, 0xb9, 0xb7, 0xb2, + 0xb7, 0xb2, 0xbb, 0xb1, 0xb4, 0xbe, 0xb1, 0xb7, + 0xb9, 0xb4, 0xbc, 0xba, 0xbe, 0xb8, 0xb2, 0xbd, + 0xb0, 0xbf, 0xb6, 0xbc, 0xba, 0xb9, 0xbd, 0xb0, + 0xbf, 0xb3, 0xb3, 0xb5, 0xb5, 0xb6, 0xb8, 0xbb, + 0xdd, 0xd1, 0xd2, 0xdf, 0xd8, 0xdd, 0xd4, 0xd8, + 0xd6, 0xda, 0xdf, 0xd3, 0xdb, 0xd7, 0xd1, 0xd4, + 0xda, 0xdc, 0xd9, 0xd5, 0xd3, 0xd6, 0xde, 0xdb, + 0xd5, 0xd0, 0xd0, 0xde, 0xdc, 0xd9, 0xd7, 0xd2, + 0xd7, 0xd2, 0xdb, 0xd1, 0xd4, 0xde, 0xd1, 0xd7, + 0xd9, 0xd4, 0xdc, 0xda, 0xde, 0xd8, 0xd2, 0xdd, + 0xd0, 0xdf, 0xd6, 0xdc, 0xda, 0xd9, 0xdd, 0xd0, + 0xdf, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd8, 0xdb, + 0xdd, 0xd1, 0xd2, 0xdf, 0xd8, 0xdd, 0xd4, 0xd8, + 0xd6, 0xda, 0xdf, 0xd3, 0xdb, 0xd7, 0xd1, 0xd4, + 0xda, 0xdc, 0xd9, 0xd5, 0xd3, 0xd6, 0xde, 0xdb, + 0xd5, 0xd0, 0xd0, 0xde, 0xdc, 0xd9, 0xd7, 0xd2, + 0xd7, 0xd2, 0xdb, 0xd1, 0xd4, 0xde, 0xd1, 0xd7, + 0xd9, 0xd4, 0xdc, 0xda, 0xde, 0xd8, 0xd2, 0xdd, + 0xd0, 0xdf, 0xd6, 0xdc, 0xda, 0xd9, 0xdd, 0xd0, + 0xdf, 0xd3, 0xd3, 0xd5, 0xd5, 0xd6, 0xd8, 0xdb, + 0x8d, 0x81, 0x82, 0x8f, 0x88, 0x8d, 0x84, 0x88, + 0x86, 0x8a, 0x8f, 0x83, 0x8b, 0x87, 0x81, 0x84, + 0x8a, 0x8c, 0x89, 0x85, 0x83, 0x86, 0x8e, 0x8b, + 0x85, 0x80, 0x80, 0x8e, 0x8c, 0x89, 0x87, 0x82, + 0x87, 0x82, 0x8b, 0x81, 0x84, 0x8e, 0x81, 0x87, + 0x89, 0x84, 0x8c, 0x8a, 0x8e, 0x88, 0x82, 0x8d, + 0x80, 0x8f, 0x86, 0x8c, 0x8a, 0x89, 0x8d, 0x80, + 0x8f, 0x83, 0x83, 0x85, 0x85, 0x86, 0x88, 0x8b, + 0xcd, 0xc1, 0xc2, 0xcf, 0xc8, 0xcd, 0xc4, 0xc8, + 0xc6, 0xca, 0xcf, 0xc3, 0xcb, 0xc7, 0xc1, 0xc4, + 0xca, 0xcc, 0xc9, 0xc5, 0xc3, 0xc6, 0xce, 0xcb, + 0xc5, 0xc0, 0xc0, 0xce, 0xcc, 0xc9, 0xc7, 0xc2, + 0xc7, 0xc2, 0xcb, 0xc1, 0xc4, 0xce, 0xc1, 0xc7, + 0xc9, 0xc4, 0xcc, 0xca, 0xce, 0xc8, 0xc2, 0xcd, + 0xc0, 0xcf, 0xc6, 0xcc, 0xca, 0xc9, 0xcd, 0xc0, + 0xcf, 0xc3, 0xc3, 0xc5, 0xc5, 0xc6, 0xc8, 0xcb, + 0x1d, 0x11, 0x12, 0x1f, 0x18, 0x1d, 0x14, 0x18, + 0x16, 0x1a, 0x1f, 0x13, 0x1b, 0x17, 0x11, 0x14, + 0x1a, 0x1c, 0x19, 0x15, 0x13, 0x16, 0x1e, 0x1b, + 0x15, 0x10, 0x10, 0x1e, 0x1c, 0x19, 0x17, 0x12, + 0x17, 0x12, 0x1b, 0x11, 0x14, 0x1e, 0x11, 0x17, + 0x19, 0x14, 0x1c, 0x1a, 0x1e, 0x18, 0x12, 0x1d, + 0x10, 0x1f, 0x16, 0x1c, 0x1a, 0x19, 0x1d, 0x10, + 0x1f, 0x13, 0x13, 0x15, 0x15, 0x16, 0x18, 0x1b, + 0x3d, 0x31, 0x32, 0x3f, 0x38, 0x3d, 0x34, 0x38, + 0x36, 0x3a, 0x3f, 0x33, 0x3b, 0x37, 0x31, 0x34, + 0x3a, 0x3c, 0x39, 0x35, 0x33, 0x36, 0x3e, 0x3b, + 0x35, 0x30, 0x30, 0x3e, 0x3c, 0x39, 0x37, 0x32, + 0x37, 0x32, 0x3b, 0x31, 0x34, 0x3e, 0x31, 0x37, + 0x39, 0x34, 0x3c, 0x3a, 0x3e, 0x38, 0x32, 0x3d, + 0x30, 0x3f, 0x36, 0x3c, 0x3a, 0x39, 0x3d, 0x30, + 0x3f, 0x33, 0x33, 0x35, 0x35, 0x36, 0x38, 0x3b, + 0x4d, 0x41, 0x42, 0x4f, 0x48, 0x4d, 0x44, 0x48, + 0x46, 0x4a, 0x4f, 0x43, 0x4b, 0x47, 0x41, 0x44, + 0x4a, 0x4c, 0x49, 0x45, 0x43, 0x46, 0x4e, 0x4b, + 0x45, 0x40, 0x40, 0x4e, 0x4c, 0x49, 0x47, 0x42, + 0x47, 0x42, 0x4b, 0x41, 0x44, 0x4e, 0x41, 0x47, + 0x49, 0x44, 0x4c, 0x4a, 0x4e, 0x48, 0x42, 0x4d, + 0x40, 0x4f, 0x46, 0x4c, 0x4a, 0x49, 0x4d, 0x40, + 0x4f, 0x43, 0x43, 0x45, 0x45, 0x46, 0x48, 0x4b, + 0x7d, 0x71, 0x72, 0x7f, 0x78, 0x7d, 0x74, 0x78, + 0x76, 0x7a, 0x7f, 0x73, 0x7b, 0x77, 0x71, 0x74, + 0x7a, 0x7c, 0x79, 0x75, 0x73, 0x76, 0x7e, 0x7b, + 0x75, 0x70, 0x70, 0x7e, 0x7c, 0x79, 0x77, 0x72, + 0x77, 0x72, 0x7b, 0x71, 0x74, 0x7e, 0x71, 0x77, + 0x79, 0x74, 0x7c, 0x7a, 0x7e, 0x78, 0x72, 0x7d, + 0x70, 0x7f, 0x76, 0x7c, 0x7a, 0x79, 0x7d, 0x70, + 0x7f, 0x73, 0x73, 0x75, 0x75, 0x76, 0x78, 0x7b, + 0xad, 0xa1, 0xa2, 0xaf, 0xa8, 0xad, 0xa4, 0xa8, + 0xa6, 0xaa, 0xaf, 0xa3, 0xab, 0xa7, 0xa1, 0xa4, + 0xaa, 0xac, 0xa9, 0xa5, 0xa3, 0xa6, 0xae, 0xab, + 0xa5, 0xa0, 0xa0, 0xae, 0xac, 0xa9, 0xa7, 0xa2, + 0xa7, 0xa2, 0xab, 0xa1, 0xa4, 0xae, 0xa1, 0xa7, + 0xa9, 0xa4, 0xac, 0xaa, 0xae, 0xa8, 0xa2, 0xad, + 0xa0, 0xaf, 0xa6, 0xac, 0xaa, 0xa9, 0xad, 0xa0, + 0xaf, 0xa3, 0xa3, 0xa5, 0xa5, 0xa6, 0xa8, 0xab, + 0xed, 0xe1, 0xe2, 0xef, 0xe8, 0xed, 0xe4, 0xe8, + 0xe6, 0xea, 0xef, 0xe3, 0xeb, 0xe7, 0xe1, 0xe4, + 0xea, 0xec, 0xe9, 0xe5, 0xe3, 0xe6, 0xee, 0xeb, + 0xe5, 0xe0, 0xe0, 0xee, 0xec, 0xe9, 0xe7, 0xe2, + 0xe7, 0xe2, 0xeb, 0xe1, 0xe4, 0xee, 0xe1, 0xe7, + 0xe9, 0xe4, 0xec, 0xea, 0xee, 0xe8, 0xe2, 0xed, + 0xe0, 0xef, 0xe6, 0xec, 0xea, 0xe9, 0xed, 0xe0, + 0xef, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe8, 0xeb, + 0x7d, 0x71, 0x72, 0x7f, 0x78, 0x7d, 0x74, 0x78, + 0x76, 0x7a, 0x7f, 0x73, 0x7b, 0x77, 0x71, 0x74, + 0x7a, 0x7c, 0x79, 0x75, 0x73, 0x76, 0x7e, 0x7b, + 0x75, 0x70, 0x70, 0x7e, 0x7c, 0x79, 0x77, 0x72, + 0x77, 0x72, 0x7b, 0x71, 0x74, 0x7e, 0x71, 0x77, + 0x79, 0x74, 0x7c, 0x7a, 0x7e, 0x78, 0x72, 0x7d, + 0x70, 0x7f, 0x76, 0x7c, 0x7a, 0x79, 0x7d, 0x70, + 0x7f, 0x73, 0x73, 0x75, 0x75, 0x76, 0x78, 0x7b, + 0xad, 0xa1, 0xa2, 0xaf, 0xa8, 0xad, 0xa4, 0xa8, + 0xa6, 0xaa, 0xaf, 0xa3, 0xab, 0xa7, 0xa1, 0xa4, + 0xaa, 0xac, 0xa9, 0xa5, 0xa3, 0xa6, 0xae, 0xab, + 0xa5, 0xa0, 0xa0, 0xae, 0xac, 0xa9, 0xa7, 0xa2, + 0xa7, 0xa2, 0xab, 0xa1, 0xa4, 0xae, 0xa1, 0xa7, + 0xa9, 0xa4, 0xac, 0xaa, 0xae, 0xa8, 0xa2, 0xad, + 0xa0, 0xaf, 0xa6, 0xac, 0xaa, 0xa9, 0xad, 0xa0, + 0xaf, 0xa3, 0xa3, 0xa5, 0xa5, 0xa6, 0xa8, 0xab, + 0x9d, 0x91, 0x92, 0x9f, 0x98, 0x9d, 0x94, 0x98, + 0x96, 0x9a, 0x9f, 0x93, 0x9b, 0x97, 0x91, 0x94, + 0x9a, 0x9c, 0x99, 0x95, 0x93, 0x96, 0x9e, 0x9b, + 0x95, 0x90, 0x90, 0x9e, 0x9c, 0x99, 0x97, 0x92, + 0x97, 0x92, 0x9b, 0x91, 0x94, 0x9e, 0x91, 0x97, + 0x99, 0x94, 0x9c, 0x9a, 0x9e, 0x98, 0x92, 0x9d, + 0x90, 0x9f, 0x96, 0x9c, 0x9a, 0x99, 0x9d, 0x90, + 0x9f, 0x93, 0x93, 0x95, 0x95, 0x96, 0x98, 0x9b, + 0xfd, 0xf1, 0xf2, 0xff, 0xf8, 0xfd, 0xf4, 0xf8, + 0xf6, 0xfa, 0xff, 0xf3, 0xfb, 0xf7, 0xf1, 0xf4, + 0xfa, 0xfc, 0xf9, 0xf5, 0xf3, 0xf6, 0xfe, 0xfb, + 0xf5, 0xf0, 0xf0, 0xfe, 0xfc, 0xf9, 0xf7, 0xf2, + 0xf7, 0xf2, 0xfb, 0xf1, 0xf4, 0xfe, 0xf1, 0xf7, + 0xf9, 0xf4, 0xfc, 0xfa, 0xfe, 0xf8, 0xf2, 0xfd, + 0xf0, 0xff, 0xf6, 0xfc, 0xfa, 0xf9, 0xfd, 0xf0, + 0xff, 0xf3, 0xf3, 0xf5, 0xf5, 0xf6, 0xf8, 0xfb, + 0x5d, 0x51, 0x52, 0x5f, 0x58, 0x5d, 0x54, 0x58, + 0x56, 0x5a, 0x5f, 0x53, 0x5b, 0x57, 0x51, 0x54, + 0x5a, 0x5c, 0x59, 0x55, 0x53, 0x56, 0x5e, 0x5b, + 0x55, 0x50, 0x50, 0x5e, 0x5c, 0x59, 0x57, 0x52, + 0x57, 0x52, 0x5b, 0x51, 0x54, 0x5e, 0x51, 0x57, + 0x59, 0x54, 0x5c, 0x5a, 0x5e, 0x58, 0x52, 0x5d, + 0x50, 0x5f, 0x56, 0x5c, 0x5a, 0x59, 0x5d, 0x50, + 0x5f, 0x53, 0x53, 0x55, 0x55, 0x56, 0x58, 0x5b, + 0x6d, 0x61, 0x62, 0x6f, 0x68, 0x6d, 0x64, 0x68, + 0x66, 0x6a, 0x6f, 0x63, 0x6b, 0x67, 0x61, 0x64, + 0x6a, 0x6c, 0x69, 0x65, 0x63, 0x66, 0x6e, 0x6b, + 0x65, 0x60, 0x60, 0x6e, 0x6c, 0x69, 0x67, 0x62, + 0x67, 0x62, 0x6b, 0x61, 0x64, 0x6e, 0x61, 0x67, + 0x69, 0x64, 0x6c, 0x6a, 0x6e, 0x68, 0x62, 0x6d, + 0x60, 0x6f, 0x66, 0x6c, 0x6a, 0x69, 0x6d, 0x60, + 0x6f, 0x63, 0x63, 0x65, 0x65, 0x66, 0x68, 0x6b, + 0x0d, 0x01, 0x02, 0x0f, 0x08, 0x0d, 0x04, 0x08, + 0x06, 0x0a, 0x0f, 0x03, 0x0b, 0x07, 0x01, 0x04, + 0x0a, 0x0c, 0x09, 0x05, 0x03, 0x06, 0x0e, 0x0b, + 0x05, 0x00, 0x00, 0x0e, 0x0c, 0x09, 0x07, 0x02, + 0x07, 0x02, 0x0b, 0x01, 0x04, 0x0e, 0x01, 0x07, + 0x09, 0x04, 0x0c, 0x0a, 0x0e, 0x08, 0x02, 0x0d, + 0x00, 0x0f, 0x06, 0x0c, 0x0a, 0x09, 0x0d, 0x00, + 0x0f, 0x03, 0x03, 0x05, 0x05, 0x06, 0x08, 0x0b, + 0x8d, 0x81, 0x82, 0x8f, 0x88, 0x8d, 0x84, 0x88, + 0x86, 0x8a, 0x8f, 0x83, 0x8b, 0x87, 0x81, 0x84, + 0x8a, 0x8c, 0x89, 0x85, 0x83, 0x86, 0x8e, 0x8b, + 0x85, 0x80, 0x80, 0x8e, 0x8c, 0x89, 0x87, 0x82, + 0x87, 0x82, 0x8b, 0x81, 0x84, 0x8e, 0x81, 0x87, + 0x89, 0x84, 0x8c, 0x8a, 0x8e, 0x88, 0x82, 0x8d, + 0x80, 0x8f, 0x86, 0x8c, 0x8a, 0x89, 0x8d, 0x80, + 0x8f, 0x83, 0x83, 0x85, 0x85, 0x86, 0x88, 0x8b, + 0xfd, 0xf1, 0xf2, 0xff, 0xf8, 0xfd, 0xf4, 0xf8, + 0xf6, 0xfa, 0xff, 0xf3, 0xfb, 0xf7, 0xf1, 0xf4, + 0xfa, 0xfc, 0xf9, 0xf5, 0xf3, 0xf6, 0xfe, 0xfb, + 0xf5, 0xf0, 0xf0, 0xfe, 0xfc, 0xf9, 0xf7, 0xf2, + 0xf7, 0xf2, 0xfb, 0xf1, 0xf4, 0xfe, 0xf1, 0xf7, + 0xf9, 0xf4, 0xfc, 0xfa, 0xfe, 0xf8, 0xf2, 0xfd, + 0xf0, 0xff, 0xf6, 0xfc, 0xfa, 0xf9, 0xfd, 0xf0, + 0xff, 0xf3, 0xf3, 0xf5, 0xf5, 0xf6, 0xf8, 0xfb, + 0x0d, 0x01, 0x02, 0x0f, 0x08, 0x0d, 0x04, 0x08, + 0x06, 0x0a, 0x0f, 0x03, 0x0b, 0x07, 0x01, 0x04, + 0x0a, 0x0c, 0x09, 0x05, 0x03, 0x06, 0x0e, 0x0b, + 0x05, 0x00, 0x00, 0x0e, 0x0c, 0x09, 0x07, 0x02, + 0x07, 0x02, 0x0b, 0x01, 0x04, 0x0e, 0x01, 0x07, + 0x09, 0x04, 0x0c, 0x0a, 0x0e, 0x08, 0x02, 0x0d, + 0x00, 0x0f, 0x06, 0x0c, 0x0a, 0x09, 0x0d, 0x00, + 0x0f, 0x03, 0x03, 0x05, 0x05, 0x06, 0x08, 0x0b, + 0xed, 0xe1, 0xe2, 0xef, 0xe8, 0xed, 0xe4, 0xe8, + 0xe6, 0xea, 0xef, 0xe3, 0xeb, 0xe7, 0xe1, 0xe4, + 0xea, 0xec, 0xe9, 0xe5, 0xe3, 0xe6, 0xee, 0xeb, + 0xe5, 0xe0, 0xe0, 0xee, 0xec, 0xe9, 0xe7, 0xe2, + 0xe7, 0xe2, 0xeb, 0xe1, 0xe4, 0xee, 0xe1, 0xe7, + 0xe9, 0xe4, 0xec, 0xea, 0xee, 0xe8, 0xe2, 0xed, + 0xe0, 0xef, 0xe6, 0xec, 0xea, 0xe9, 0xed, 0xe0, + 0xef, 0xe3, 0xe3, 0xe5, 0xe5, 0xe6, 0xe8, 0xeb, + 0x5d, 0x51, 0x52, 0x5f, 0x58, 0x5d, 0x54, 0x58, + 0x56, 0x5a, 0x5f, 0x53, 0x5b, 0x57, 0x51, 0x54, + 0x5a, 0x5c, 0x59, 0x55, 0x53, 0x56, 0x5e, 0x5b, + 0x55, 0x50, 0x50, 0x5e, 0x5c, 0x59, 0x57, 0x52, + 0x57, 0x52, 0x5b, 0x51, 0x54, 0x5e, 0x51, 0x57, + 0x59, 0x54, 0x5c, 0x5a, 0x5e, 0x58, 0x52, 0x5d, + 0x50, 0x5f, 0x56, 0x5c, 0x5a, 0x59, 0x5d, 0x50, + 0x5f, 0x53, 0x53, 0x55, 0x55, 0x56, 0x58, 0x5b, + 0x2d, 0x21, 0x22, 0x2f, 0x28, 0x2d, 0x24, 0x28, + 0x26, 0x2a, 0x2f, 0x23, 0x2b, 0x27, 0x21, 0x24, + 0x2a, 0x2c, 0x29, 0x25, 0x23, 0x26, 0x2e, 0x2b, + 0x25, 0x20, 0x20, 0x2e, 0x2c, 0x29, 0x27, 0x22, + 0x27, 0x22, 0x2b, 0x21, 0x24, 0x2e, 0x21, 0x27, + 0x29, 0x24, 0x2c, 0x2a, 0x2e, 0x28, 0x22, 0x2d, + 0x20, 0x2f, 0x26, 0x2c, 0x2a, 0x29, 0x2d, 0x20, + 0x2f, 0x23, 0x23, 0x25, 0x25, 0x26, 0x28, 0x2b, + 0x9d, 0x91, 0x92, 0x9f, 0x98, 0x9d, 0x94, 0x98, + 0x96, 0x9a, 0x9f, 0x93, 0x9b, 0x97, 0x91, 0x94, + 0x9a, 0x9c, 0x99, 0x95, 0x93, 0x96, 0x9e, 0x9b, + 0x95, 0x90, 0x90, 0x9e, 0x9c, 0x99, 0x97, 0x92, + 0x97, 0x92, 0x9b, 0x91, 0x94, 0x9e, 0x91, 0x97, + 0x99, 0x94, 0x9c, 0x9a, 0x9e, 0x98, 0x92, 0x9d, + 0x90, 0x9f, 0x96, 0x9c, 0x9a, 0x99, 0x9d, 0x90, + 0x9f, 0x93, 0x93, 0x95, 0x95, 0x96, 0x98, 0x9b, + 0x3d, 0x31, 0x32, 0x3f, 0x38, 0x3d, 0x34, 0x38, + 0x36, 0x3a, 0x3f, 0x33, 0x3b, 0x37, 0x31, 0x34, + 0x3a, 0x3c, 0x39, 0x35, 0x33, 0x36, 0x3e, 0x3b, + 0x35, 0x30, 0x30, 0x3e, 0x3c, 0x39, 0x37, 0x32, + 0x37, 0x32, 0x3b, 0x31, 0x34, 0x3e, 0x31, 0x37, + 0x39, 0x34, 0x3c, 0x3a, 0x3e, 0x38, 0x32, 0x3d, + 0x30, 0x3f, 0x36, 0x3c, 0x3a, 0x39, 0x3d, 0x30, + 0x3f, 0x33, 0x33, 0x35, 0x35, 0x36, 0x38, 0x3b, + 0x2d, 0x21, 0x22, 0x2f, 0x28, 0x2d, 0x24, 0x28, + 0x26, 0x2a, 0x2f, 0x23, 0x2b, 0x27, 0x21, 0x24, + 0x2a, 0x2c, 0x29, 0x25, 0x23, 0x26, 0x2e, 0x2b, + 0x25, 0x20, 0x20, 0x2e, 0x2c, 0x29, 0x27, 0x22, + 0x27, 0x22, 0x2b, 0x21, 0x24, 0x2e, 0x21, 0x27, + 0x29, 0x24, 0x2c, 0x2a, 0x2e, 0x28, 0x22, 0x2d, + 0x20, 0x2f, 0x26, 0x2c, 0x2a, 0x29, 0x2d, 0x20, + 0x2f, 0x23, 0x23, 0x25, 0x25, 0x26, 0x28, 0x2b, + 0xcd, 0xc1, 0xc2, 0xcf, 0xc8, 0xcd, 0xc4, 0xc8, + 0xc6, 0xca, 0xcf, 0xc3, 0xcb, 0xc7, 0xc1, 0xc4, + 0xca, 0xcc, 0xc9, 0xc5, 0xc3, 0xc6, 0xce, 0xcb, + 0xc5, 0xc0, 0xc0, 0xce, 0xcc, 0xc9, 0xc7, 0xc2, + 0xc7, 0xc2, 0xcb, 0xc1, 0xc4, 0xce, 0xc1, 0xc7, + 0xc9, 0xc4, 0xcc, 0xca, 0xce, 0xc8, 0xc2, 0xcd, + 0xc0, 0xcf, 0xc6, 0xcc, 0xca, 0xc9, 0xcd, 0xc0, + 0xcf, 0xc3, 0xc3, 0xc5, 0xc5, 0xc6, 0xc8, 0xcb, + }, +}; + +static const uint8_t key_perm[56] = { + 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, + 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 +}; + +static const uint8_t key_shifts[16] = { + 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 +}; + +static const uint8_t comp_perm[48] = { + 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 +}; + +static const uint8_t pbox[32] = { + 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 +}; + +static int +ascii_to_bin(char ch) +{ + if (ch > 'z') + return 0; + if (ch >= 'a') + return (ch - 'a' + 38); + if (ch > 'Z') + return 0; + if (ch >= 'A') + return (ch - 'A' + 12); + if (ch > '9') + return 0; + if (ch >= '.') + return (ch - '.'); + return 0; +} + + + +struct des_ctx { + uint32_t saltbits; /* referenced 5 times */ + uint8_t un_pbox[32]; /* 2 times */ + uint8_t inv_comp_perm[56]; /* 3 times */ + uint8_t inv_key_perm[64]; /* 3 times */ + uint32_t en_keysl[16], en_keysr[16]; /* 2 times each */ + uint32_t fp_maskl[8][256], fp_maskr[8][256]; /* 9 times each */ + uint32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; /* 9 times */ + uint32_t comp_maskl[8][128], comp_maskr[8][128]; /* 9 times each */ + uint32_t psbox[4][256]; /* 5 times */ +}; +#define D (*ctx) +#define saltbits (D.saltbits ) +#define old_salt (D.old_salt ) +#define old_rawkey0 (D.old_rawkey0 ) +#define old_rawkey1 (D.old_rawkey1 ) +#define un_pbox (D.un_pbox ) +#define inv_comp_perm (D.inv_comp_perm ) +#define inv_key_perm (D.inv_key_perm ) +#define en_keysl (D.en_keysl ) +#define en_keysr (D.en_keysr ) +#define de_keysl (D.de_keysl ) +#define de_keysr (D.de_keysr ) +#define ip_maskl (D.ip_maskl ) +#define ip_maskr (D.ip_maskr ) +#define fp_maskl (D.fp_maskl ) +#define fp_maskr (D.fp_maskr ) +#define key_perm_maskl (D.key_perm_maskl ) +#define key_perm_maskr (D.key_perm_maskr ) +#define comp_maskl (D.comp_maskl ) +#define comp_maskr (D.comp_maskr ) +#define psbox (D.psbox ) + +static struct des_ctx *des_init(struct des_ctx *ctx) +{ + int i, j, b, k, inbit, obit; + uint32_t p; + + saltbits = 0; + + /* Initialise the inverted key permutation. */ + for (i = 0; i < 64; i++) { + inv_key_perm[i] = 255; + } + + /* + * Invert the key permutation and initialise the inverted key + * compression permutation. + */ + for (i = 0; i < 56; i++) { + inv_key_perm[key_perm[i] - 1] = (uint8_t)i; + inv_comp_perm[i] = 255; + } + + /* Invert the key compression permutation. */ + for (i = 0; i < 48; i++) { + inv_comp_perm[comp_perm[i] - 1] = (uint8_t)i; + } + + /* + * Set up the OR-mask arrays for the initial and final permutations, + * and for the key initial and compression permutations. + */ + for (k = 0; k < 8; k++) { + uint32_t il, ir; + uint32_t fl, fr; + for (i = 0; i < 256; i++) { + fl = 0; + fr = 0; + for (j = 0; j < 8; j++) { + inbit = 8 * k + j; + if (i & (0x80>>j)) { + obit = final_perm[inbit]; + if (obit < 32) + fl |= 0x80000000UL >> obit; + else + fr |= 0x80000000UL >> obit-32; + } + } + fp_maskl[k][i] = fl; + fp_maskr[k][i] = fr; + } + for (i = 0; i < 128; i++) { + il = 0; + ir = 0; + for (j = 0; j < 7; j++) { + inbit = 8 * k + j; + if (i & (0x80 >> j + 1)) { + obit = inv_key_perm[inbit]; + if (obit == 255) + continue; + if (obit < 28) + il |= 0x8000000UL >> obit; + else + ir |= 0x8000000UL >> obit-28; + } + } + key_perm_maskl[k][i] = il; + key_perm_maskr[k][i] = ir; + il = 0; + ir = 0; + for (j = 0; j < 7; j++) { + inbit = 7 * k + j; + if (i & (0x80 >> j + 1)) { + obit = inv_comp_perm[inbit]; + if (obit == 255) + continue; + if (obit < 24) + il |= 0x800000UL >> obit; + else + ir |= 0x800000UL >> obit-24; + } + } + comp_maskl[k][i] = il; + comp_maskr[k][i] = ir; + } + } + + /* + * Invert the P-box permutation, and convert into OR-masks for + * handling the output of the S-box arrays setup above. + */ + for (i = 0; i < 32; i++) + un_pbox[pbox[i] - 1] = (uint8_t)i; + + for (b = 0; b < 4; b++) { + for (i = 0; i < 256; i++) { + p = 0; + for (j = 0; j < 8; j++) { + if (i & (0x80 >> j)) + p |= 0x80000000>>un_pbox[8 * b + j]; + } + psbox[b][i] = p; + } + } + + return ctx; +} + + +static void setup_salt(struct des_ctx *ctx, uint32_t salt) +{ + uint32_t obit, saltbit; + int i; + + saltbits = 0; + saltbit = 1; + obit = 0x800000; + for (i = 0; i < 24; i++) { + if (salt & saltbit) + saltbits |= obit; + saltbit <<= 1; + obit >>= 1; + } +} + +static void des_setkey(struct des_ctx *ctx, const unsigned char *key) +{ + uint32_t k0, k1, rawkey0, rawkey1; + int shifts, round; + + rawkey0 = key[0]<<24 | key[1]<<16 | key[2]<<8 | key[3]; + rawkey1 = key[4]<<24 | key[5]<<16 | key[6]<<8 | key[7]; + + /* + * Do key permutation and split into two 28-bit subkeys. + */ + k0 = key_perm_maskl[0][rawkey0 >> 25] + | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskl[4][rawkey1 >> 25] + | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; + k1 = key_perm_maskr[0][rawkey0 >> 25] + | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskr[4][rawkey1 >> 25] + | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; + /* + * Rotate subkeys and do compression permutation. + */ + shifts = 0; + for (round = 0; round < 16; round++) { + uint32_t t0, t1; + + shifts += key_shifts[round]; + + t0 = (k0 << shifts) | (k0 >> (28 - shifts)); + t1 = (k1 << shifts) | (k1 >> (28 - shifts)); + + en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] + | comp_maskl[1][(t0 >> 14) & 0x7f] + | comp_maskl[2][(t0 >> 7) & 0x7f] + | comp_maskl[3][t0 & 0x7f] + | comp_maskl[4][(t1 >> 21) & 0x7f] + | comp_maskl[5][(t1 >> 14) & 0x7f] + | comp_maskl[6][(t1 >> 7) & 0x7f] + | comp_maskl[7][t1 & 0x7f]; + + en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] + | comp_maskr[1][(t0 >> 14) & 0x7f] + | comp_maskr[2][(t0 >> 7) & 0x7f] + | comp_maskr[3][t0 & 0x7f] + | comp_maskr[4][(t1 >> 21) & 0x7f] + | comp_maskr[5][(t1 >> 14) & 0x7f] + | comp_maskr[6][(t1 >> 7) & 0x7f] + | comp_maskr[7][t1 & 0x7f]; + } +} + + +static void do_des(struct des_ctx *ctx, uint32_t *l_out, uint32_t *r_out, int count) +{ + uint32_t l, r, *kl, *kr; + uint32_t f = f; /* silence gcc */ + uint32_t r48l, r48r; + int round; + + /* Do initial permutation (IP). */ + l = r = 0; + + do { + /* Do each round. */ + kl = en_keysl; + kr = en_keysr; + round = 16; + do { + /* Expand R to 48 bits (simulate the E-box). */ + r48l = ((r & 0x00000001) << 23) + | ((r & 0xf8000000) >> 9) + | ((r & 0x1f800000) >> 11) + | ((r & 0x01f80000) >> 13) + | ((r & 0x001f8000) >> 15); + + r48r = ((r & 0x0001f800) << 7) + | ((r & 0x00001f80) << 5) + | ((r & 0x000001f8) << 3) + | ((r & 0x0000001f) << 1) + | ((r & 0x80000000) >> 31); + /* + * Do salting for crypt() and friends, and + * XOR with the permuted key. + */ + f = (r48l ^ r48r) & saltbits; + r48l ^= f ^ *kl++; + r48r ^= f ^ *kr++; + /* + * Do sbox lookups (which shrink it back to 32 bits) + * and do the pbox permutation at the same time. + */ + f = psbox[0][m_sbox[0][r48l >> 12]] + | psbox[1][m_sbox[1][r48l & 0xfff]] + | psbox[2][m_sbox[2][r48r >> 12]] + | psbox[3][m_sbox[3][r48r & 0xfff]]; + /* Now that we've permuted things, complete f(). */ + f ^= l; + l = r; + r = f; + } while (--round); + r = l; + l = f; + } while (--count); + + /* Do final permutation (inverse of IP). */ + *l_out = fp_maskl[0][l >> 24] + | fp_maskl[1][(l >> 16) & 0xff] + | fp_maskl[2][(l >> 8) & 0xff] + | fp_maskl[3][l & 0xff] + | fp_maskl[4][r >> 24] + | fp_maskl[5][(r >> 16) & 0xff] + | fp_maskl[6][(r >> 8) & 0xff] + | fp_maskl[7][r & 0xff]; + *r_out = fp_maskr[0][l >> 24] + | fp_maskr[1][(l >> 16) & 0xff] + | fp_maskr[2][(l >> 8) & 0xff] + | fp_maskr[3][l & 0xff] + | fp_maskr[4][r >> 24] + | fp_maskr[5][(r >> 16) & 0xff] + | fp_maskr[6][(r >> 8) & 0xff] + | fp_maskr[7][r & 0xff]; +} + +#define DES_OUT_BUFSIZE 21 + +static void +to64_msb_first(char *s, unsigned v) +{ + *s++ = i64c(v >> 18); /* bits 23..18 */ + *s++ = i64c(v >> 12); /* bits 17..12 */ + *s++ = i64c(v >> 6); /* bits 11..6 */ + *s = i64c(v); /* bits 5..0 */ +} + +static char * +des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE], + const unsigned char *key, const unsigned char *setting) +{ + uint32_t salt, r0, r1, keybuf[2]; + uint8_t *q; + + /* + * Copy the key, shifting each character up by one bit + * and padding with zeros. + */ + q = (uint8_t *)keybuf; + while (q - (uint8_t *)keybuf != 8) { + *q = *key << 1; + if (*q) + key++; + q++; + } + des_setkey(ctx, (char *)keybuf); + + /* + * setting - 2 bytes of salt + * key - up to 8 characters + */ + salt = (ascii_to_bin(setting[1]) << 6) + | ascii_to_bin(setting[0]); + + output[0] = setting[0]; + /* + * If the encrypted password that the salt was extracted from + * is only 1 character long, the salt will be corrupted. We + * need to ensure that the output string doesn't have an extra + * NUL in it! + */ + output[1] = setting[1] ? setting[1] : output[0]; + + setup_salt(ctx, salt); + /* Do it. */ + do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */); + + /* Now encode the result. */ + /* Each call takes low-order 24 bits and stores 4 chars */ + /* bits 31..8 of r0 */ + to64_msb_first(output + 2, (r0 >> 8)); + /* bits 7..0 of r0 and 31..16 of r1 */ + to64_msb_first(output + 6, (r0 << 16) | (r1 >> 16)); + /* bits 15..0 of r1 and two zero bits (plus extra zero byte) */ + to64_msb_first(output + 10, (r1 << 8)); + /* extra zero byte is encoded as '.', fixing it */ + output[13] = '\0'; + + return output; +} + +#undef C +#undef init_perm +#undef final_perm +#undef m_sbox +#undef D +#undef const_ctx +#undef saltbits +#undef old_salt +#undef old_rawkey0 +#undef old_rawkey1 +#undef un_pbox +#undef inv_comp_perm +#undef inv_key_perm +#undef en_keysl +#undef en_keysr +#undef de_keysl +#undef de_keysr +#undef ip_maskl +#undef ip_maskr +#undef fp_maskl +#undef fp_maskr +#undef key_perm_maskl +#undef key_perm_maskr +#undef comp_maskl +#undef comp_maskr +#undef psbox + +struct crypt_data; + +char *__crypt_r(const char *clear, const char *salt, struct crypt_data *data) +{ + struct des_ctx des_ctx = { 0 }; + +#if 0 + /* MD5 or SHA? */ + if (salt[0] == '$' && salt[1] && salt[2] == '$') { + if (salt[1] == '1') + return md5_crypt((char *)data, clear, salt); + } +#endif + + des_init(&des_ctx); + return des_crypt(&des_ctx, (char *)data, clear, salt); +} + +weak_alias(__crypt_r, crypt_r); + +char *crypt(const char *clear, const char *salt) +{ + static char buf[128]; + return __crypt_r(clear, salt, (void *)buf); +} diff --git a/05/musl-final/src/misc/cuserid.c b/05/musl-final/src/misc/cuserid.c new file mode 100644 index 0000000..4e78798 --- /dev/null +++ b/05/musl-final/src/misc/cuserid.c @@ -0,0 +1,14 @@ +#define _GNU_SOURCE +#include +#include +#include + +char *cuserid(char *buf) +{ + struct passwd pw, *ppw; + long pwb[256]; + if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw)) + return 0; + snprintf(buf, L_cuserid, "%s", pw.pw_name); + return buf; +} diff --git a/05/musl-final/src/misc/dirname.c b/05/musl-final/src/misc/dirname.c new file mode 100644 index 0000000..8f70dbb --- /dev/null +++ b/05/musl-final/src/misc/dirname.c @@ -0,0 +1,15 @@ +#include +#include + +char *dirname(char *s) +{ + size_t i; + if (!s || !*s || !strchr(s, '/')) return "."; + i = strlen(s)-1; + for (; i&&s[i]=='/'; i--); + for (; i&&s[i-1]!='/'; i--); + for (; i&&s[i-1]=='/'; i--); + if (!i && *s=='/') i++; + s[i] = 0; + return s; +} diff --git a/05/musl-final/src/misc/ffs.c b/05/musl-final/src/misc/ffs.c new file mode 100644 index 0000000..2f7cb32 --- /dev/null +++ b/05/musl-final/src/misc/ffs.c @@ -0,0 +1,9 @@ +#include + +int ffs(int i) +{ + unsigned int j = i; + for (i=1; j && !(j&1); j>>=1, i++); + if (j) return i; + return 0; +} diff --git a/05/musl-final/src/misc/ftw.c b/05/musl-final/src/misc/ftw.c new file mode 100644 index 0000000..de01e39 --- /dev/null +++ b/05/musl-final/src/misc/ftw.c @@ -0,0 +1,9 @@ +#include +#include "libc.h" + +int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int fd_limit) +{ + return nftw(path, (void *)fn, fd_limit, FTW_PHYS); +} + +LFS64(ftw); diff --git a/05/musl-final/src/misc/getdomainname.c b/05/musl-final/src/misc/getdomainname.c new file mode 100644 index 0000000..7eb113d --- /dev/null +++ b/05/musl-final/src/misc/getdomainname.c @@ -0,0 +1,9 @@ +#include +#include +#include + +int getdomainname(char *name, size_t len) +{ + *name = 0; + return 0; +} diff --git a/05/musl-final/src/misc/getgrouplist.c b/05/musl-final/src/misc/getgrouplist.c new file mode 100644 index 0000000..88f273d --- /dev/null +++ b/05/musl-final/src/misc/getgrouplist.c @@ -0,0 +1,11 @@ +#include + +/* FIXME */ + +int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) +{ + if (*ngroups<1) return -1; + *groups = gid; + *ngroups = 1; + return 0; +} diff --git a/05/musl-final/src/misc/getopt.c b/05/musl-final/src/misc/getopt.c new file mode 100644 index 0000000..abf0e84 --- /dev/null +++ b/05/musl-final/src/misc/getopt.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +char *optarg; +int optind=1, opterr=1, optopt; +static int optpos; + +int getopt(int argc, char * const argv[], const char *optstring) +{ + int i; + wchar_t c, d; + int k, l; + char *optchar; + + if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1]) + return -1; + if (argv[optind][1] == '-' && !argv[optind][2]) + return optind++, -1; + + if (!optpos) optpos++; + if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) { + k = 1; + c = 0xfffd; /* replacement char */ + } + optchar = argv[optind]+optpos; + optopt = c; + optpos += k; + + if (!argv[optind][optpos]) { + optind++; + optpos = 0; + } + + for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1); + + if (d != c) { + if (optstring[0] != ':' && opterr) { + write(2, argv[0], strlen(argv[0])); + write(2, ": illegal option: ", 18); + write(2, optchar, k); + write(2, "\n", 1); + } + return '?'; + } + if (optstring[i+1] == ':') { + if (optind >= argc) { + if (optstring[0] == ':') return ':'; + if (opterr) { + write(2, argv[0], strlen(argv[0])); + write(2, ": option requires an argument: ", 31); + write(2, optchar, k); + write(2, "\n", 1); + } + return '?'; + } + optarg = argv[optind++] + optpos; + optpos = 0; + } + return c; +} diff --git a/05/musl-final/src/misc/getpriority.c b/05/musl-final/src/misc/getpriority.c new file mode 100644 index 0000000..2fb26b2 --- /dev/null +++ b/05/musl-final/src/misc/getpriority.c @@ -0,0 +1,9 @@ +#include +#include "syscall.h" + +int getpriority(int which, id_t who) +{ + int ret = syscall2(__NR_getpriority, which, who); + if (ret < 0) return ret; + return 20-ret; +} diff --git a/05/musl-final/src/misc/getrlimit.c b/05/musl-final/src/misc/getrlimit.c new file mode 100644 index 0000000..84a659d --- /dev/null +++ b/05/musl-final/src/misc/getrlimit.c @@ -0,0 +1,15 @@ +#include +#include "syscall.h" +#include "libc.h" + +int getrlimit(int resource, struct rlimit *rlim) +{ + long k_rlim[2]; + if (syscall2(__NR_getrlimit, resource, (long)k_rlim) < 0) + return -1; + rlim->rlim_cur = k_rlim[0] == -1 ? -1 : (unsigned long)k_rlim[0]; + rlim->rlim_max = k_rlim[1] == -1 ? -1 : (unsigned long)k_rlim[1]; + return 0; +} + +LFS64(getrlimit); diff --git a/05/musl-final/src/misc/getrusage.c b/05/musl-final/src/misc/getrusage.c new file mode 100644 index 0000000..1b8850f --- /dev/null +++ b/05/musl-final/src/misc/getrusage.c @@ -0,0 +1,20 @@ +#include +#include +#include "syscall.h" + +/* this is a huge hack to make up for the kernel's stupid 32bit time_t + * without having to recopy the whole rusage structure ourselves.. */ + +int getrusage(int who, struct rusage *ru) +{ + struct { long tv_sec, tv_usec; } ktv[2]; + char *fakeaddr = ((char *)ru + sizeof(struct timeval [2]) - sizeof ktv); + if (syscall2(__NR_getrusage, who, (long)fakeaddr) < 0) + return -1; + memcpy(ktv, fakeaddr, sizeof ktv); + ru->ru_utime.tv_sec = ktv[0].tv_sec; + ru->ru_utime.tv_usec = ktv[0].tv_usec; + ru->ru_stime.tv_sec = ktv[1].tv_sec; + ru->ru_stime.tv_usec = ktv[1].tv_usec; + return 0; +} diff --git a/05/musl-final/src/misc/getsubopt.c b/05/musl-final/src/misc/getsubopt.c new file mode 100644 index 0000000..dac9bf9 --- /dev/null +++ b/05/musl-final/src/misc/getsubopt.c @@ -0,0 +1,23 @@ +#include +#include + +int getsubopt(char **opt, char *const *keys, char **val) +{ + char *s = *opt; + int i; + + *val = NULL; + *opt = strchr(s, ','); + if (*opt) *(*opt)++ = 0; + else *opt = s + strlen(s); + + for (i=0; keys[i]; i++) { + size_t l = strlen(keys[i]); + if (strncmp(keys[i], s, l)) continue; + if (s[l] == '=') + *val = s + l; + else if (s[l]) continue; + return i; + } + return -1; +} diff --git a/05/musl-final/src/misc/ioctl.c b/05/musl-final/src/misc/ioctl.c new file mode 100644 index 0000000..808b7c9 --- /dev/null +++ b/05/musl-final/src/misc/ioctl.c @@ -0,0 +1,13 @@ +#include +#include +#include "syscall.h" + +int ioctl(int fd, int req, ...) +{ + void *arg; + va_list ap; + va_start(ap, req); + arg = va_arg(ap, void *); + va_end(ap); + return syscall3(__NR_ioctl, fd, req, (long)arg); +} diff --git a/05/musl-final/src/misc/lockf.c b/05/musl-final/src/misc/lockf.c new file mode 100644 index 0000000..d8f82ef --- /dev/null +++ b/05/musl-final/src/misc/lockf.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include "libc.h" + +int lockf(int fd, int op, off_t size) +{ + struct flock l = { + .l_type = F_WRLCK, + .l_whence = SEEK_CUR, + .l_len = size, + }; + switch (op) { + case F_TEST: + l.l_type = F_RDLCK; + if (fcntl(fd, F_GETLK, &l) < 0) + return -1; + if (l.l_type == F_UNLCK || l.l_pid == getpid()) + return 0; + errno = EACCES; + return -1; + case F_ULOCK: + l.l_type = F_UNLCK; + case F_TLOCK: + return fcntl(fd, F_SETLK, &l); + case F_LOCK: + return fcntl(fd, F_SETLKW, &l); + } + errno = EINVAL; + return -1; +} + +LFS64(lockf); diff --git a/05/musl-final/src/misc/nftw.c b/05/musl-final/src/misc/nftw.c new file mode 100644 index 0000000..1b94ac1 --- /dev/null +++ b/05/musl-final/src/misc/nftw.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +struct history +{ + struct history *chain; + dev_t dev; + ino_t ino; + int level; + int base; +}; + +#undef dirfd +#define dirfd(d) (*(int *)d) + +static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h) +{ + size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l; + struct stat st; + struct history new; + int type; + int r; + struct FTW lev; + char *name; + + if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) { + if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st)) + type = FTW_SLN; + else if (errno != EACCES) return -1; + else type = FTW_NS; + } else if (S_ISDIR(st.st_mode)) { + if (access(path, R_OK) < 0) type = FTW_DNR; + else if (flags & FTW_DEPTH) type = FTW_DP; + else type = FTW_D; + } else if (S_ISLNK(st.st_mode)) { + if (flags & FTW_PHYS) type = FTW_SL; + else type = FTW_SLN; + } else { + type = FTW_F; + } + + if ((flags & FTW_MOUNT) && h + && (st.st_dev != h->dev || st.st_ino != h->ino)) + return 0; + + new.chain = h; + new.dev = st.st_dev; + new.ino = st.st_ino; + new.level = h ? h->level+1 : 0; + new.base = l+1; + + lev.level = new.level; + lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0; + + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) + return r; + + for (; h; h = h->chain) + if (h->dev == st.st_dev && h->ino == st.st_ino) + return 0; + + if ((type == FTW_D || type == FTW_DP) && fd_limit) { + DIR *d = opendir(path); + if (d) { + struct dirent *de; + while ((de = readdir(d))) { + if (de->d_name[0] == '.' + && (!de->d_name[1] + || (de->d_name[1]=='.' + && !de->d_name[2]))) continue; + if (strlen(de->d_name) >= PATH_MAX-l) { + errno = ENAMETOOLONG; + closedir(d); + return -1; + } + path[j]='/'; + strcpy(path+j+1, de->d_name); + if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) { + closedir(d); + return r; + } + } + closedir(d); + } else if (errno != EACCES) { + return -1; + } + } + + path[l] = 0; + if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) + return r; + + return 0; +} + +int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) +{ + size_t l; + char pathbuf[PATH_MAX+1]; + + if (fd_limit <= 0) return 0; + + l = strlen(path); + if (l > PATH_MAX) { + errno = ENAMETOOLONG; + return -1; + } + memcpy(pathbuf, path, l+1); + + return do_nftw(pathbuf, fn, fd_limit, flags, NULL); +} + +LFS64(nftw); diff --git a/05/musl-final/src/misc/openpty.c b/05/musl-final/src/misc/openpty.c new file mode 100644 index 0000000..0b4eb22 --- /dev/null +++ b/05/musl-final/src/misc/openpty.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +/* Nonstandard, but vastly superior to the standard functions */ + +int openpty(int *m, int *s, char *name, const struct termios *tio, const struct winsize *ws) +{ + int n=0; + char buf[20]; + + *m = open("/dev/ptmx", O_RDWR|O_NOCTTY); + if (!*m) return -1; + + if (ioctl(*m, TIOCSPTLCK, &n) || ioctl (*m, TIOCGPTN, &n)) { + close(*m); + return -1; + } + + if (!name) name = buf; + snprintf(name, sizeof buf, "/dev/pts/%d", n); + if ((*s = open(name, O_RDWR|O_NOCTTY)) < 0) { + close(*m); + return -1; + } + + if (tio) tcsetattr(*s, TCSANOW, tio); + if (ws) ioctl(*s, TIOCSWINSZ, ws); + + return 0; +} diff --git a/05/musl-final/src/misc/pty.c b/05/musl-final/src/misc/pty.c new file mode 100644 index 0000000..0d25a83 --- /dev/null +++ b/05/musl-final/src/misc/pty.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +int posix_openpt(int flags) +{ + return open("/dev/ptmx", flags); +} + +int grantpt(int fd) +{ + return 0; +} + +int unlockpt(int fd) +{ + int unlock = 0; + return ioctl(fd, TIOCSPTLCK, &unlock); +} + +char *ptsname(int fd) +{ + static char buf[9 + sizeof(int)*3 + 1]; + char *s = buf+sizeof(buf)-1; + int pty; + if (ioctl (fd, TIOCGPTN, &pty)) + return NULL; + if (pty) for (; pty; pty/=10) *--s = '0' + pty%10; + else *--s = '0'; + s -= 9; + s[0] = '/'; s[1] = 'd'; s[2] = 'e'; s[3] = 'v'; + s[4] = '/'; s[5] = 'p'; s[6] = 't'; s[7] = 's'; s[8] = '/'; + return s; +} diff --git a/05/musl-final/src/misc/realpath.c b/05/musl-final/src/misc/realpath.c new file mode 100644 index 0000000..f6b5549 --- /dev/null +++ b/05/musl-final/src/misc/realpath.c @@ -0,0 +1,6 @@ +#include + +char *realpath(const char *filename, char *resolved) +{ + return 0; +} diff --git a/05/musl-final/src/misc/sched_yield.c b/05/musl-final/src/misc/sched_yield.c new file mode 100644 index 0000000..8a68519 --- /dev/null +++ b/05/musl-final/src/misc/sched_yield.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int __yield() +{ + return syscall0(__NR_sched_yield); +} + +weak_alias(__yield, sched_yield); diff --git a/05/musl-final/src/misc/setpriority.c b/05/musl-final/src/misc/setpriority.c new file mode 100644 index 0000000..26da4b8 --- /dev/null +++ b/05/musl-final/src/misc/setpriority.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int setpriority(int which, id_t who, int prio) +{ + return syscall3(__NR_getpriority, which, who, prio); +} diff --git a/05/musl-final/src/misc/setrlimit.c b/05/musl-final/src/misc/setrlimit.c new file mode 100644 index 0000000..7fdfc4e --- /dev/null +++ b/05/musl-final/src/misc/setrlimit.c @@ -0,0 +1,11 @@ +#include +#include "syscall.h" +#include "libc.h" + +int setrlimit(int resource, const struct rlimit *rlim) +{ + long k_rlim[2] = { rlim->rlim_cur, rlim->rlim_max }; + return syscall2(__NR_setrlimit, resource, (long)k_rlim); +} + +LFS64(setrlimit); diff --git a/05/musl-final/src/misc/syslog.c b/05/musl-final/src/misc/syslog.c new file mode 100644 index 0000000..4809d2d --- /dev/null +++ b/05/musl-final/src/misc/syslog.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +static int lock; +static const char *log_ident; +static int log_opt; +static int log_facility = LOG_USER; +static int log_mask = 0xff; +static FILE *log_f; + +int setlogmask(int maskpri) +{ + int old = log_mask; + if (maskpri) log_mask = maskpri; + return old; +} + +static const struct { + short sun_family; + char sun_path[9]; +} log_addr = { + AF_UNIX, + "/dev/log" +}; + +void closelog(void) +{ + LOCK(&lock); + if (log_f) fclose(log_f); + log_f = NULL; + UNLOCK(&lock); +} + +static void __openlog(const char *ident, int opt, int facility) +{ + int fd; + + log_ident = ident; + log_opt = opt; + log_facility = facility; + + if (!(opt & LOG_NDELAY) || log_f) return; + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + fcntl(fd, F_SETFD, FD_CLOEXEC); + if (connect(fd, (void *)&log_addr, sizeof(short) + sizeof "/dev/log") < 0) + close(fd); + else log_f = fdopen(fd, "wb"); +} + +void openlog(const char *ident, int opt, int facility) +{ + LOCK(&lock); + __openlog(ident, opt, facility); + UNLOCK(&lock); +} + +void syslog(int priority, const char *message, ...) +{ + struct sigaction sa; + va_list ap; + char timebuf[16]; + time_t now; + struct tm tm; + //const char *fmt, *ident, *sep; + //int i; + + if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return; + + LOCK(&lock); + + if (!log_f) __openlog(log_ident, log_opt | LOG_NDELAY, log_facility); + if (!log_f) { + UNLOCK(&lock); + return; + } + + memset(&sa, 0, sizeof sa); + sa.sa_handler = SIG_IGN; + if (sigaction(SIGPIPE, &sa, &sa) < 0) { + // we must abandon logging or we might cause SIGPIPE + UNLOCK(&lock); + return; + } + + now = time(NULL); + gmtime_r(&now, &tm); + strftime(timebuf, sizeof timebuf, "%b %e %T", &tm); + + fprintf(log_f, "<%d>%s ", priority, timebuf); + if (log_ident) fprintf(log_f, "%s", log_ident); + if (log_opt & LOG_PID) fprintf(log_f, "[%d]", getpid()); + if (log_ident) fprintf(log_f, ": "); + + va_start(ap, message); + vfprintf(log_f, message, ap); + va_end(ap); + fputc(0, log_f); + fflush(log_f); + + // Note: LOG_CONS is not supported because it is annoying!! + // syslogd will send messages to console if it deems them appropriate! + + sigaction(SIGPIPE, &sa, NULL); + + UNLOCK(&lock); +} diff --git a/05/musl-final/src/misc/uname.c b/05/musl-final/src/misc/uname.c new file mode 100644 index 0000000..fbe8664 --- /dev/null +++ b/05/musl-final/src/misc/uname.c @@ -0,0 +1,8 @@ +#include +#include +#include "syscall.h" + +int uname(struct utsname *uts) +{ + return syscall1(__NR_uname, (long)uts); +} diff --git a/05/musl-final/src/mman/madvise.c b/05/musl-final/src/mman/madvise.c new file mode 100644 index 0000000..c4ef799 --- /dev/null +++ b/05/musl-final/src/mman/madvise.c @@ -0,0 +1,13 @@ +#include +#include "syscall.h" +#include "libc.h" + +int __madvise(void *addr, size_t len, int advice) +{ + return syscall3(__NR_madvise, (long)addr, len, advice); +} + +int madvise(void *addr, size_t len, int advice) +{ + return syscall3(__NR_madvise, (long)addr, len, advice); +} diff --git a/05/musl-final/src/mman/mlock.c b/05/musl-final/src/mman/mlock.c new file mode 100644 index 0000000..3c7c653 --- /dev/null +++ b/05/musl-final/src/mman/mlock.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mlock(const void *addr, size_t len) +{ + return syscall2(__NR_mlock, (long)addr, len); +} diff --git a/05/musl-final/src/mman/mlockall.c b/05/musl-final/src/mman/mlockall.c new file mode 100644 index 0000000..782fc9d --- /dev/null +++ b/05/musl-final/src/mman/mlockall.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mlockall(int flags) +{ + return syscall1(__NR_mlockall, flags); +} diff --git a/05/musl-final/src/mman/mmap.c b/05/musl-final/src/mman/mmap.c new file mode 100644 index 0000000..665e2ec --- /dev/null +++ b/05/musl-final/src/mman/mmap.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include "syscall.h" +#include "libc.h" + +void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off) +{ + if (sizeof(off_t) > sizeof(long)) + if (((long)off & 0xfff) | ((long)((unsigned long long)off>>(12 + 8*(sizeof(off_t)-sizeof(long)))))) + start = (void *)-1; +#ifdef __NR_mmap2 + return (void *)syscall6(__NR_mmap2, (long)start, len, prot, flags, fd, off>>12); +#else + return (void *)syscall6(__NR_mmap, (long)start, len, prot, flags, fd, off); +#endif +} + +void *mmap(void *start, size_t len, int prot, int flags, int fd, off_t off) +{ + if (sizeof(off_t) > sizeof(long)) + if (((long)off & 0xfff) | ((long)((unsigned long long)off>>(12 + 8*(sizeof(off_t)-sizeof(long)))))) + start = (void *)-1; +#ifdef __NR_mmap2 + return (void *)syscall6(__NR_mmap2, (long)start, len, prot, flags, fd, off>>12); +#else + return (void *)syscall6(__NR_mmap, (long)start, len, prot, flags, fd, off); +#endif +} + +LFS64(mmap); diff --git a/05/musl-final/src/mman/mprotect.c b/05/musl-final/src/mman/mprotect.c new file mode 100644 index 0000000..11d5e23 --- /dev/null +++ b/05/musl-final/src/mman/mprotect.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mprotect(void *addr, size_t len, int prot) +{ + return syscall3(__NR_mprotect, (long)addr, len, prot); +} diff --git a/05/musl-final/src/mman/mremap.c b/05/musl-final/src/mman/mremap.c new file mode 100644 index 0000000..78491ef --- /dev/null +++ b/05/musl-final/src/mman/mremap.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include "syscall.h" +#include "libc.h" + +void *__mremap(void *old_addr, size_t old_len, size_t new_len, int flags, ...) +{ + va_list ap; + void *new_addr; + + va_start(ap, flags); + new_addr = va_arg(ap, void *); + va_end(ap); + + return (void *)syscall5(__NR_mremap, (long)old_addr, old_len, new_len, flags, (long)new_addr); +} + +weak_alias(__mremap, mremap); diff --git a/05/musl-final/src/mman/msync.c b/05/musl-final/src/mman/msync.c new file mode 100644 index 0000000..e092647 --- /dev/null +++ b/05/musl-final/src/mman/msync.c @@ -0,0 +1,8 @@ +#include +#include +#include "syscall.h" + +int msync(void *start, size_t len, int flags) +{ + return syscall3(__NR_msync, (long)start, len, flags); +} diff --git a/05/musl-final/src/mman/munlock.c b/05/musl-final/src/mman/munlock.c new file mode 100644 index 0000000..0db5981 --- /dev/null +++ b/05/musl-final/src/mman/munlock.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int munlock(const void *addr, size_t len) +{ + return syscall2(__NR_munlock, (long)addr, len); +} diff --git a/05/musl-final/src/mman/munlockall.c b/05/musl-final/src/mman/munlockall.c new file mode 100644 index 0000000..ce3e86c --- /dev/null +++ b/05/musl-final/src/mman/munlockall.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int munlockall(void) +{ + return syscall0(__NR_munlockall); +} diff --git a/05/musl-final/src/mman/munmap.c b/05/musl-final/src/mman/munmap.c new file mode 100644 index 0000000..d1777e3 --- /dev/null +++ b/05/musl-final/src/mman/munmap.c @@ -0,0 +1,14 @@ +#include +#include +#include "syscall.h" +#include "libc.h" + +int __munmap(void *start, size_t len) +{ + return syscall2(__NR_munmap, (long)start, len); +} + +int munmap(void *start, size_t len) +{ + return syscall2(__NR_munmap, (long)start, len); +} diff --git a/05/musl-final/src/mman/posix_madvise.c b/05/musl-final/src/mman/posix_madvise.c new file mode 100644 index 0000000..4727ad7 --- /dev/null +++ b/05/musl-final/src/mman/posix_madvise.c @@ -0,0 +1,6 @@ +#include + +int posix_madvise(void *addr, size_t len, int advice) +{ + return 0; +} diff --git a/05/musl-final/src/multibyte/btowc.c b/05/musl-final/src/multibyte/btowc.c new file mode 100644 index 0000000..9d2c3b1 --- /dev/null +++ b/05/musl-final/src/multibyte/btowc.c @@ -0,0 +1,7 @@ +#include +#include + +wint_t btowc(int c) +{ + return c<128U ? c : EOF; +} diff --git a/05/musl-final/src/multibyte/decode.c b/05/musl-final/src/multibyte/decode.c new file mode 100644 index 0000000..8d3d3c0 --- /dev/null +++ b/05/musl-final/src/multibyte/decode.c @@ -0,0 +1,47 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +/* Decodes UTF-8 byte-by-byte. The c argument must be initialized to 0 + * to begin decoding; when finished it will contain the Unicode scalar + * value decoded. Return value is 1 if finished, 0 if in-progress, and + * -1 if an invalid sequence was encountered. After an invalid sequence, + * the state (in c) automatically resets to 0 if a continuation byte was + * expected to facilitate a calling idiom of immediately retrying a + * failed decode call after processing the invalid sequence. If the + * second try fails, the byte is invalid as a starter as well. + * + * A trivial usage idiom is: + * while (src=0) 1[dst+=n]=0, src++; + */ + +int decode(unsigned *c, unsigned b) +{ + if (!*c) { + if (b < 0x80) { + *c = b; + return 1; + } else if (b-SA >= SB-SA) { + *c = FAILSTATE; + return -1; + } + *c = bittab[b-SA]; + return 0; + } + + if (OOB(*c,b)) { + *c = 0; + return -1; + } + *c = *c<<6 | b-0x80; + return !(*c&(1U<<31)); +} diff --git a/05/musl-final/src/multibyte/internal.c b/05/musl-final/src/multibyte/internal.c new file mode 100644 index 0000000..e9b938d --- /dev/null +++ b/05/musl-final/src/multibyte/internal.c @@ -0,0 +1,60 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include + +#include "internal.h" + +#define C(x) ( x<2 ? -1 : ( R(0x80,0xc0) | x ) ) +#define D(x) C((x+16)) +#define E(x) ( ( x==0 ? R(0xa0,0xc0) : \ + x==0xd ? R(0x80,0xa0) : \ + R(0x80,0xc0) ) \ + | ( R(0x80,0xc0) >> 6 ) \ + | x ) +#ifdef I_FAILED_TO_RTFM_RFC3629 +#define F0(x) (( x==0 ? R(0x90,0xc0) : \ + R(0x80,0xc0) ) \ + | ( R(0x80,0xc0) >> 6 ) \ + | ( R(0x80,0xc0) >> 12 ) \ + | x ) +#define F8(x) (( x==0 ? R(0xa0,0xc0) : \ + R(0x80,0xc0) ) \ + | ( R(0x80,0xc0) >> 6 ) \ + | ( R(0x80,0xc0) >> 12 ) \ + | ( R(0x80,0xc0) >> 18 ) \ + | x ) +#define FC(x) (( x==0 ? R(0x88,0xc0) : \ + R(0x80,0xc0) ) \ + | ( R(0x80,0xc0) >> 6 ) \ + | ( R(0x80,0xc0) >> 12 ) \ + | ( R(0x80,0xc0) >> 18 ) \ + | ( R(0x80,0xc0) >> 24 ) \ + | x ) +#define F(x) ( x<8 ? F0(x) : x<12 ? F8((x&3)) : x<14 ? FC((x&1)) : -1 ) +#else +#define F(x) ( ( x>=5 ? 0 : \ + x==0 ? R(0x90,0xc0) : \ + x==4 ? R(0x80,0xa0) : \ + R(0x80,0xc0) ) \ + | ( R(0x80,0xc0) >> 6 ) \ + | ( R(0x80,0xc0) >> 12 ) \ + | x ) +#endif + +const uint32_t bittab[] = { + C(0x2),C(0x3),C(0x4),C(0x5),C(0x6),C(0x7), + C(0x8),C(0x9),C(0xa),C(0xb),C(0xc),C(0xd),C(0xe),C(0xf), + D(0x0),D(0x1),D(0x2),D(0x3),D(0x4),D(0x5),D(0x6),D(0x7), + D(0x8),D(0x9),D(0xa),D(0xb),D(0xc),D(0xd),D(0xe),D(0xf), + E(0x0),E(0x1),E(0x2),E(0x3),E(0x4),E(0x5),E(0x6),E(0x7), + E(0x8),E(0x9),E(0xa),E(0xb),E(0xc),E(0xd),E(0xe),E(0xf), + F(0x0),F(0x1),F(0x2),F(0x3),F(0x4), +#ifdef I_FAILED_TO_RTFM_RFC3629 + F(0x5),F(0x6),F(0x7), + F(0x8),F(0x9),F(0xa),F(0xb),F(0xc),F(0xd) +#endif +}; diff --git a/05/musl-final/src/multibyte/internal.h b/05/musl-final/src/multibyte/internal.h new file mode 100644 index 0000000..ec52bdf --- /dev/null +++ b/05/musl-final/src/multibyte/internal.h @@ -0,0 +1,61 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#define LIBC +#ifndef LIBC +/* rename functions not to conflict with libc */ +#ifndef myprefix +#define myprefix fsmu8_ +#endif +#define concat2(a,b) a ## b +#define concat(a,b) concat2(a,b) +#define prefix(b) concat(myprefix,b) + +#undef mblen +#undef mbrlen +#undef mbrtowc +#undef mbsinit +#undef mbsnrtowcs +#undef mbsrtowcs +#undef wcrtomb +#undef wcsrtombs +#undef wcstombs +#undef wctomb +#define mblen prefix(mblen) +#define mbrlen prefix(mbrlen) +#define mbrtowc prefix(mbrtowc) +#define mbsinit prefix(mbsinit) +#define mbsnrtowcs prefix(mbsnrtowcs) +#define mbsrtowcs prefix(mbsrtowcs) +#define mbstowcs prefix(mbstowcs) +#define wcrtomb prefix(wcrtomb) +#define wcsnrtombs prefix(wcsnrtombs) +#define wcsrtombs prefix(wcsrtombs) +#define wcstombs prefix(wcstombs) +#define wctomb prefix(wctomb) + +#define bittab prefix(bittab) +#else +#define bittab __fsmu8 +#endif + +extern const uint32_t bittab[]; + +/* Upper 6 state bits are a negative integer offset to bound-check next byte */ +/* equivalent to: ( (b-0x80) | (b+offset) ) & ~0x3f */ +#define OOB(c,b) (((((b)>>3)-0x10)|(((b)>>3)+((int32_t)(c)>>26))) & ~7) + +/* Interval [a,b). Either a must be 80 or b must be c0, lower 3 bits clear. */ +#define R(a,b) ((uint32_t)((a==0x80 ? 0x40-b : -a) << 23)) +#define FAILSTATE R(0x80,0x80) + +#ifdef I_FAILED_TO_RTFM_RFC3629 +#define SA 0xc2u +#define SB 0xfeu +#else +#define SA 0xc2u +#define SB 0xf5u +#endif diff --git a/05/musl-final/src/multibyte/mblen.c b/05/musl-final/src/multibyte/mblen.c new file mode 100644 index 0000000..26d3564 --- /dev/null +++ b/05/musl-final/src/multibyte/mblen.c @@ -0,0 +1,17 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +int mblen(const char *s, size_t n) +{ + return mbtowc(0, s, n); +} diff --git a/05/musl-final/src/multibyte/mbrlen.c b/05/musl-final/src/multibyte/mbrlen.c new file mode 100644 index 0000000..c9a9f03 --- /dev/null +++ b/05/musl-final/src/multibyte/mbrlen.c @@ -0,0 +1,18 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t mbrlen(const char *s, size_t n, mbstate_t *st) +{ + static unsigned internal; + return mbrtowc(0, s, n, st ? st : (mbstate_t *)&internal); +} diff --git a/05/musl-final/src/multibyte/mbrtowc.c b/05/musl-final/src/multibyte/mbrtowc.c new file mode 100644 index 0000000..a354573 --- /dev/null +++ b/05/musl-final/src/multibyte/mbrtowc.c @@ -0,0 +1,58 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t mbrtowc(wchar_t *wc, const char *src, size_t n, mbstate_t *st) +{ + static unsigned internal_state; + unsigned c; + const unsigned char *s = (const void *)src; + const unsigned N = n; + + if (!st) st = (void *)&internal_state; + c = *(unsigned *)st; + + if (!s) { + s = ""; + wc = (void *)&wc; + n = 1; + } else if (!wc) wc = (void *)&wc; + + if (!n) return -2; + if (!c) { + if (*s < 0x80) return !!(*wc = *s); + if (*s-SA > SB-SA) goto ilseq; + c = bittab[*s++-SA]; n--; + } + + if (n) { + if (OOB(c,*s)) goto ilseq; +loop: + c = c<<6 | *s++-0x80; n--; + if (!(c&(1U<<31))) { + *(unsigned *)st = 0; + *wc = c; + return N-n; + } + if (n) { + if (*s-0x80u >= 0x40) goto ilseq; + goto loop; + } + } + + *(unsigned *)st = c; + return -2; +ilseq: + *(unsigned *)st = FAILSTATE; + errno = EILSEQ; + return -1; +} diff --git a/05/musl-final/src/multibyte/mbsinit.c b/05/musl-final/src/multibyte/mbsinit.c new file mode 100644 index 0000000..d307e5a --- /dev/null +++ b/05/musl-final/src/multibyte/mbsinit.c @@ -0,0 +1,17 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +int mbsinit(const mbstate_t *st) +{ + return !*(unsigned *)st; +} diff --git a/05/musl-final/src/multibyte/mbsnrtowcs.c b/05/musl-final/src/multibyte/mbsnrtowcs.c new file mode 100644 index 0000000..c6f0207 --- /dev/null +++ b/05/musl-final/src/multibyte/mbsnrtowcs.c @@ -0,0 +1,61 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include +#include + +#include "internal.h" + +size_t mbsnrtowcs(wchar_t *wcs, const char **src, size_t n, size_t wn, mbstate_t *st) +{ + size_t l, cnt=0, n2; + wchar_t *ws, wbuf[256]; + const char *s = *src; + + if (!wcs) ws = wbuf, wn = sizeof wbuf / sizeof *wbuf; + else ws = wcs; + + /* making sure output buffer size is at most n/4 will ensure + * that mbsrtowcs never reads more than n input bytes. thus + * we can use mbsrtowcs as long as it's practical.. */ + + while ( s && wn && ( (n2=n/4)>=wn || n2>32 ) ) { + if (n2>=wn) n2=wn; + n -= n2; + l = mbsrtowcs(ws, &s, n2, st); + if (!(l+1)) { + cnt = l; + wn = 0; + break; + } + if (ws != wbuf) { + ws += l; + wn -= l; + } + cnt += l; + } + if (s) while (wn && n) { + l = mbrtowc(ws, s, n, st); + if (l+2<=2) { + if (!(l+1)) { + cnt = l; + break; + } + /* have to roll back partial character */ + *(unsigned *)st = 0; + break; + } + s += l; n -= l; + /* safe - this loop runs fewer than sizeof(wbuf)/8 times */ + ws++; wn--; + cnt++; + } + if (wcs) *src = s; + return cnt; +} diff --git a/05/musl-final/src/multibyte/mbsrtowcs.c b/05/musl-final/src/multibyte/mbsrtowcs.c new file mode 100644 index 0000000..64399cf --- /dev/null +++ b/05/musl-final/src/multibyte/mbsrtowcs.c @@ -0,0 +1,121 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t mbsrtowcs(wchar_t *ws, const char **src, size_t wn, mbstate_t *st) +{ + unsigned c; + const unsigned char *s = (const void *)*src; + const wchar_t *wsorig = ws; + + if (!st) st = (void *)&c, c = 0; + else c = *(unsigned *)st; + + if (c) { + *(unsigned *)st = 0; + if (!ws) { + wn = 0; + goto resume0; + } + goto resume; + } + + if (!ws) for (wn=0;;) { + if (*s-SA >= SB-SA) { + while (((uintptr_t)s&3) && *s-1u<0x7f) s++, wn++; + while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) s+=4, wn+=4; + while (*s-1u<0x7f) s++, wn++; + if (!*s) return wn; + if (*s-SA >= SB-SA) goto ilseq2; + } + c = bittab[*s++-SA]; + do { +resume0: + if (OOB(c,*s)) goto ilseq2; s++; + c <<= 6; if (!(c&(1U<<31))) break; +#ifdef I_FAILED_TO_RTFM_RFC3629 + if (*s++-0x80u >= 0x40) goto ilseq2; + c <<= 6; if (!(c&(1U<<31))) break; + if (*s++-0x80u >= 0x40) goto ilseq2; + c <<= 6; if (!(c&(1U<<31))) break; +#endif + if (*s++-0x80u >= 0x40) goto ilseq2; + c <<= 6; if (!(c&(1U<<31))) break; + if (*s++-0x80u >= 0x40) goto ilseq2; + } while (0); + wn++; c = 0; + } + + while (wn) { + if (*s-SA >= SB-SA) { + if (wn >= 7) { + while (((uintptr_t)s&3) && *s-1u<0x7f) { + *ws++ = *s++; + wn--; + } + while (wn>=4 && !(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) { + *ws++ = *s++; + *ws++ = *s++; + *ws++ = *s++; + *ws++ = *s++; + wn -= 4; + } + } + while (wn && *s-1u<0x7f) { + *ws++ = *s++; + wn--; + } + if (!wn) break; + if (!*s) { + *ws = 0; + *src = 0; + return ws-wsorig; + } + if (*s-SA >= SB-SA) goto ilseq; + } + c = bittab[*s++-SA]; + do { +resume: + if (OOB(c,*s)) goto ilseq; + c = (c<<6) | *s++-0x80; + if (!(c&(1U<<31))) break; + +#ifdef I_FAILED_TO_RTFM_RFC3629 + if (*s-0x80u >= 0x40) goto ilseq; + c = (c<<6) | *s++-0x80; + if (!(c&(1U<<31))) break; + + if (*s-0x80u >= 0x40) goto ilseq; + c = (c<<6) | *s++-0x80; + if (!(c&(1U<<31))) break; +#endif + + if (*s-0x80u >= 0x40) goto ilseq; + c = (c<<6) | *s++-0x80; + if (!(c&(1U<<31))) break; + + if (*s-0x80u >= 0x40) goto ilseq; + c = (c<<6) | *s++-0x80; + } while (0); + + *ws++ = c; wn--; c = 0; + } + *src = (const void *)s; + return ws-wsorig; +ilseq: + *src = (const void *)s; +ilseq2: + /* enter permanently failing state */ + *(unsigned *)st = FAILSTATE; + errno = EILSEQ; + return -1; +} diff --git a/05/musl-final/src/multibyte/mbstowcs.c b/05/musl-final/src/multibyte/mbstowcs.c new file mode 100644 index 0000000..23e1d92 --- /dev/null +++ b/05/musl-final/src/multibyte/mbstowcs.c @@ -0,0 +1,18 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t mbstowcs(wchar_t *ws, const char *s, size_t wn) +{ + mbstate_t st = { 0 }; + return mbsrtowcs(ws, (void*)&s, wn, &st); +} diff --git a/05/musl-final/src/multibyte/mbtowc.c b/05/musl-final/src/multibyte/mbtowc.c new file mode 100644 index 0000000..bdcaeb3 --- /dev/null +++ b/05/musl-final/src/multibyte/mbtowc.c @@ -0,0 +1,19 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +int mbtowc(wchar_t *wc, const char *s, size_t n) +{ + mbstate_t st = { 0 }; + n = mbrtowc(wc, s, n, &st); + return n+2 ? n : -1; +} diff --git a/05/musl-final/src/multibyte/wcrtomb.c b/05/musl-final/src/multibyte/wcrtomb.c new file mode 100644 index 0000000..36180c8 --- /dev/null +++ b/05/musl-final/src/multibyte/wcrtomb.c @@ -0,0 +1,38 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st) +{ + if (!s) return 1; + if ((unsigned)wc < 0x80) { + *s = wc; + return 1; + } else if ((unsigned)wc < 0x800) { + *s++ = 0xc0 | (wc>>6); + *s = 0x80 | (wc&0x3f); + return 2; + } else if ((unsigned)wc < 0xd800 || (unsigned)wc-0xe000 < 0x2000) { + *s++ = 0xe0 | (wc>>12); + *s++ = 0x80 | ((wc>>6)&0x3f); + *s = 0x80 | (wc&0x3f); + return 3; + } else if ((unsigned)wc-0x10000 < 0x100000) { + *s++ = 0xf0 | (wc>>18); + *s++ = 0x80 | ((wc>>12)&0x3f); + *s++ = 0x80 | ((wc>>6)&0x3f); + *s = 0x80 | (wc&0x3f); + return 4; + } + errno = EILSEQ; + return -1; +} diff --git a/05/musl-final/src/multibyte/wcsnrtombs.c b/05/musl-final/src/multibyte/wcsnrtombs.c new file mode 100644 index 0000000..666f6f3 --- /dev/null +++ b/05/musl-final/src/multibyte/wcsnrtombs.c @@ -0,0 +1,51 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t wcsnrtombs(char *dst, const wchar_t **wcs, size_t wn, size_t n, mbstate_t *st) +{ + size_t l, cnt=0, n2; + char *s, buf[256]; + const wchar_t *ws = *wcs; + + if (!dst) s = buf, n = sizeof buf; + else s = dst; + + while ( n && ( (n2=wn)>=n || n2>32 ) ) { + if (n2>=n) n2=n; + wn -= n2; + l = wcsrtombs(s, &ws, n2, 0); + if (!(l+1)) { + cnt = l; + n = 0; + break; + } + if (s != buf) { + s += l; + n -= l; + } + cnt += l; + } + while (n && wn) { + l = wcrtomb(s, *ws, 0); + if (!(l+1)) { + cnt = l; + break; + } + ws++; wn--; + /* safe - this loop runs fewer than sizeof(buf) times */ + s+=l; n-=l; + cnt++; + } + if (dst) *wcs = ws; + return cnt; +} diff --git a/05/musl-final/src/multibyte/wcsrtombs.c b/05/musl-final/src/multibyte/wcsrtombs.c new file mode 100644 index 0000000..3c48d65 --- /dev/null +++ b/05/musl-final/src/multibyte/wcsrtombs.c @@ -0,0 +1,58 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t wcsrtombs(char *s, const wchar_t **ws, size_t n, mbstate_t *st) +{ + const wchar_t *ws2; + char buf[4]; + size_t N = n, l; + if (!s) { + for (n=0, ws2=*ws; *ws2; ws2++) { + if (*ws2 >= 0x80) { + l = wcrtomb(buf, *ws2, 0); + if (!(l+1)) return -1; + n += l; + } else n++; + } + return n; + } + while (n>=4 && **ws) { + if (**ws >= 0x80) { + l = wcrtomb(s, **ws, 0); + if (!(l+1)) return -1; + s += l; + n -= l; + } else { + *s++ = **ws; + n--; + } + (*ws)++; + } + while (n && **ws) { + if (**ws >= 0x80) { + l = wcrtomb(buf, **ws, 0); + if (!(l+1)) return -1; + if (l>n) return N-n; + wcrtomb(s, **ws, 0); + s += l; + n -= l; + } else { + *s++ = **ws; + n--; + } + (*ws)++; + } + if (n) *s = 0; + *ws = 0; + return N-n; +} diff --git a/05/musl-final/src/multibyte/wcstombs.c b/05/musl-final/src/multibyte/wcstombs.c new file mode 100644 index 0000000..b9c1b18 --- /dev/null +++ b/05/musl-final/src/multibyte/wcstombs.c @@ -0,0 +1,17 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +size_t wcstombs(char *s, const wchar_t *ws, size_t n) +{ + return wcsrtombs(s, &ws, n, 0); +} diff --git a/05/musl-final/src/multibyte/wctob.c b/05/musl-final/src/multibyte/wctob.c new file mode 100644 index 0000000..d6353ee --- /dev/null +++ b/05/musl-final/src/multibyte/wctob.c @@ -0,0 +1,8 @@ +#include +#include + +int wctob(wint_t c) +{ + if (c < 128U) return c; + return EOF; +} diff --git a/05/musl-final/src/multibyte/wctomb.c b/05/musl-final/src/multibyte/wctomb.c new file mode 100644 index 0000000..6910ef3 --- /dev/null +++ b/05/musl-final/src/multibyte/wctomb.c @@ -0,0 +1,18 @@ +/* + * This code was written by Rich Felker in 2010; no copyright is claimed. + * This code is in the public domain. Attribution is appreciated but + * unnecessary. + */ + +#include +#include +#include +#include + +#include "internal.h" + +int wctomb(char *s, wchar_t wc) +{ + if (!s) return 0; + return wcrtomb(s, wc, 0); +} diff --git a/05/musl-final/src/network/__dns.c b/05/musl-final/src/network/__dns.c new file mode 100644 index 0000000..b8547bb --- /dev/null +++ b/05/musl-final/src/network/__dns.c @@ -0,0 +1,267 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "__dns.h" +#include "stdio_impl.h" + +#define TIMEOUT 5 +#define RETRY 1 +#define PACKET_MAX 512 +#define PTR_MAX (64 + sizeof ".in-addr.arpa") + +int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt) +{ + time_t t0 = time(0); + int fd; + FILE *f, _f; + unsigned char _buf[64]; + char line[64], *s, *z; + union { + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + } sa = {0}, ns[3] = {{0}}; + socklen_t sl; + int nns; + int family; + unsigned char q[280] = "", *r = dest; + int ql; + int rlen; + int got = 0, failed = 0; + int errcode = EAI_AGAIN; + int i, j; + struct timeval tv; + fd_set fds; + int id; + + /* Construct query template - RR and ID will be filled later */ + if (strlen(name)-1 >= 254U) return -1; + q[2] = q[5] = 1; + strcpy((char *)q+13, name); + for (i=13; q[i]; i=j+1) { + for (j=i; q[j] && q[j] != '.'; j++); + if (j-i-1u > 62u) return -1; + q[i-1] = j-i; + } + q[i+3] = 1; + ql = i+4; + + /* Make a reasonably unpredictable id */ + gettimeofday(&tv, 0); + id = tv.tv_usec + tv.tv_usec/256 & 0xffff; + + /* Get nameservers from resolv.conf, fallback to localhost */ + f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf); + if (f) for (nns=0; nns<3 && fgets(line, sizeof line, f); ) { + if (strncmp(line, "nameserver", 10) || !isspace(line[10])) + continue; + for (s=line+11; isspace(*s); s++); + for (z=s; *z && !isspace(*z); z++); + *z=0; + if (__ipparse(ns+nns, family, s) < 0) continue; + ns[nns].sin.sin_port = htons(53); + family = ns[nns++].sin.sin_family; + sl = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin; + } + if (f) __fclose_ca(f); + if (!nns) { + ns[0].sin.sin_family = AF_INET; + ns[0].sin.sin_port = htons(53); + nns=1; + sl = sizeof sa.sin; + } + + /* Get local address and open/bind a socket */ + sa.sin.sin_family = family; + fd = socket(family, SOCK_DGRAM, 0); + if (bind(fd, (void *)&sa, sl) < 0) { + close(fd); + return -1; + } + /* Nonblocking to work around Linux UDP select bug */ + fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); + + /* Loop until we timeout; break early on success */ + for (; time(0)-t0 < TIMEOUT; ) { + + /* Query all configured namservers in parallel */ + for (i=0; i> 8; + q[1] = id+i; + q[ql-3] = rr[i]; + sendto(fd, q, ql, MSG_NOSIGNAL, (void *)&ns[j], sl); + } + + /* Wait for a response, or until time to retry */ + FD_ZERO(&fds); + FD_SET(fd, &fds); + tv.tv_sec = RETRY; + tv.tv_usec = 0; + if (select(fd+1, &fds, 0, 0, &tv) <= 0) continue; + + /* Process any and all replies */ + while (got+failed < rrcnt && (rlen = recvfrom(fd, r, 512, 0, + (void *)&sa, (socklen_t[1]){sl})) >= 2) + { + /* Ignore replies from addresses we didn't send to */ + for (i=0; i= rrcnt || !rr[i]) continue; + + /* Interpret the result code */ + switch (r[3] & 15) { + case 0: + got++; + break; + case 3: + if (1) errcode = EAI_NONAME; else + default: + errcode = EAI_FAIL; + failed++; + } + + /* Mark this record as answered */ + rr[i] = 0; + r += 512; + } + + /* Check to see if we have answers to all queries */ + if (got+failed == rrcnt) break; + } + close(fd); + + /* Return the number of results, or an error code if none */ + if (got) return got; + return errcode; +} + +static void mkptr4(char *s, const unsigned char *ip) +{ + sprintf(s, "%d.%d.%d.%d.in-addr.arpa", + ip[3], ip[2], ip[1], ip[0]); +} + +static void mkptr6(char *s, const unsigned char *ip) +{ + static const char xdigits[] = "0123456789abcdef"; + int i; + for (i=15; i>=0; i--) { + *s++ = xdigits[ip[i]&15]; *s++ = '.'; + *s++ = xdigits[ip[i]>>4]; *s++ = '.'; + } + strcpy(s, "ip6.arpa"); +} + +int __dns_query(unsigned char *r, const void *a, int family, int ptr) +{ + char buf[PTR_MAX]; + int rr[2], rrcnt = 1; + + if (ptr) { + if (family == AF_INET6) mkptr6(buf, a); + else mkptr4(buf, a); + rr[0] = RR_PTR; + a = buf; + } else if (family == AF_INET6) { + rr[0] = RR_AAAA; + } else { + rr[0] = RR_A; + if (family != AF_INET) rr[rrcnt++] = RR_AAAA; + } + + return __dns_doqueries(r, a, rr, rrcnt); +} + + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +static int decname(char *s, const unsigned char *b, const unsigned char *p) +{ + /* Remember jump destinations to detect loops and abort */ + size_t seen[PACKET_MAX/8/sizeof(size_t)] = { 0 }; + char *sz = s + HOST_NAME_MAX; + const unsigned char *pz = b+512; + for (;;) { + if (p>=pz) return -1; + else if (*p&0xc0) { + int j = (p[0]&1) | p[1]; + if (BITOP(seen, j, &)) return -1; + BITOP(seen, j, |=); + p = b + j; + } else if (*p) { + if (p+*p+1>=pz || s+*p>=sz) return -1; + memcpy(s, p+1, *p); + s += *p+1; + p += *p+1; + s[-1] = *p ? '.' : 0; + } else return 0; + } +} + +int __dns_get_rr(void *dest, size_t stride, size_t maxlen, size_t limit, const unsigned char *r, int rr, int dec) +{ + int qdcount, ancount; + const unsigned char *p; + char tmp[256]; + int found = 0; + int len; + + if ((r[3]&15)) return 0; + p = r+12; + qdcount = r[4]*256 + r[5]; + ancount = r[6]*256 + r[7]; + if (qdcount+ancount > 64) return -1; + while (qdcount--) { + while (p-r < 512 && *p-1U < 127) p++; + if (*p>193 || (*p==193 && p[1]>254) || p>r+506) + return -1; + p += 5 + !!*p; + } + while (ancount--) { + while (p-r < 512 && *p-1U < 127) p++; + if (*p>193 || (*p==193 && p[1]>254) || p>r+506) + return -1; + p += 1 + !!*p; + len = p[8]*256 + p[9]; + if (p+len > r+512) return -1; + if (p[1]==rr && len <= maxlen) { + if (dec && decname(tmp, r, p+10)<0) return -1; + if (dest && limit) { + if (dec) strcpy(dest, tmp); + else memcpy(dest, p+10, len); + dest = (char *)dest + stride; + limit--; + } + found++; + } + p += 10 + len; + } + return found; +} + +int __dns_count_addrs(const unsigned char *r, int cnt) +{ + int found=0, res, i; + static const int p[2][2] = { { 4, RR_A }, { 16, RR_AAAA } }; + + while (cnt--) for (i=0; i<2; i++) { + res = __dns_get_rr(0, 0, p[i][0], -1, r, p[i][1], 0); + if (res < 0) return res; + found += res; + r += 512; + } + return found; +} diff --git a/05/musl-final/src/network/__dns.h b/05/musl-final/src/network/__dns.h new file mode 100644 index 0000000..9a3f740 --- /dev/null +++ b/05/musl-final/src/network/__dns.h @@ -0,0 +1,14 @@ +#include + +#define RR_A 1 +#define RR_CNAME 5 +#define RR_PTR 12 +#define RR_AAAA 28 + +int __dns_count_addrs(const unsigned char *, int); +int __dns_get_rr(void *, size_t, size_t, size_t, const unsigned char *, int, int); + +int __dns_query(unsigned char *, const void *, int, int); +int __ipparse(void *, int, const char *); + +int __dns_doqueries(unsigned char *, const char *, int *, int); diff --git a/05/musl-final/src/network/__ipparse.c b/05/musl-final/src/network/__ipparse.c new file mode 100644 index 0000000..ca9e589 --- /dev/null +++ b/05/musl-final/src/network/__ipparse.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include "__dns.h" +#include + +int __ipparse(void *dest, int family, const char *s) +{ + unsigned char *d = dest; + unsigned long a[16] = { 0 }; + const char *z; + int i; + + if (family == AF_INET6) goto not_v4; + + for (i=0; i<4 && *s; i++) { + a[i] = strtoul(s, (char **)&z, 0); + if (z==s || (*z && *z != '.')) goto not_v4; + s=z+1; + } + switch (i) { + case 0: + a[1] = a[0] & 0xffffff; + a[0] >>= 24; + case 1: + a[2] = a[1] & 0xffff; + a[1] >>= 16; + case 2: + a[3] = a[2] & 0xff; + a[2] >>= 8; + } + ((struct sockaddr_in *)d)->sin_family = AF_INET; + d = (void *)&((struct sockaddr_in *)d)->sin_addr; + for (i=0; i<4; i++) d[i] = a[i]; + return 0; + +not_v4: + return -1; +} diff --git a/05/musl-final/src/network/accept.c b/05/musl-final/src/network/accept.c new file mode 100644 index 0000000..46adff5 --- /dev/null +++ b/05/musl-final/src/network/accept.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +int accept(int fd, struct sockaddr *addr, socklen_t *len) +{ + int ret; + CANCELPT_BEGIN; + ret = socketcall(accept, fd, addr, len, 0, 0, 0); + CANCELPT_END; + return ret; +} diff --git a/05/musl-final/src/network/bind.c b/05/musl-final/src/network/bind.c new file mode 100644 index 0000000..07bb669 --- /dev/null +++ b/05/musl-final/src/network/bind.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int bind(int fd, const struct sockaddr *addr, socklen_t len) +{ + return socketcall(bind, fd, addr, len, 0, 0, 0); +} diff --git a/05/musl-final/src/network/connect.c b/05/musl-final/src/network/connect.c new file mode 100644 index 0000000..29bffbc --- /dev/null +++ b/05/musl-final/src/network/connect.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +int connect(int fd, const struct sockaddr *addr, socklen_t len) +{ + int ret; + CANCELPT_BEGIN; + ret = socketcall(connect, fd, addr, len, 0, 0, 0); + CANCELPT_END; + return ret; +} diff --git a/05/musl-final/src/network/dn_expand.c b/05/musl-final/src/network/dn_expand.c new file mode 100644 index 0000000..01b449b --- /dev/null +++ b/05/musl-final/src/network/dn_expand.c @@ -0,0 +1,28 @@ +#include +#include + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +int dn_expand(unsigned char *b, unsigned char *pz, unsigned char *p, unsigned char *s, int outlen) +{ + /* Remember jump destinations to detect loops and abort */ + size_t seen[512/8/sizeof(size_t)] = { 0 }; + unsigned char *sz = s + outlen; + if (pz-b > 512) return -1; + for (;;) { + if (p>=pz) return -1; + else if (*p&0xc0) { + int j = (p[0]&1) | p[1]; + if (BITOP(seen, j, &)) return -1; + BITOP(seen, j, |=); + p = b + j; + } else if (*p) { + if (p+*p+1>=pz || s+*p>=sz) return -1; + memcpy(s, p+1, *p); + s += *p+1; + p += *p+1; + s[-1] = *p ? '.' : 0; + } else return 0; + } +} diff --git a/05/musl-final/src/network/ent.c b/05/musl-final/src/network/ent.c new file mode 100644 index 0000000..4c2f24b --- /dev/null +++ b/05/musl-final/src/network/ent.c @@ -0,0 +1,26 @@ +#include "libc.h" + +void sethostent(int x) +{ +} + +void *gethostent() +{ + return 0; +} + +void endhostent(void) +{ +} + +weak_alias(sethostent, setnetent); +weak_alias(gethostent, getnetent); +weak_alias(endhostent, endnetent); + +weak_alias(sethostent, setservent); +weak_alias(gethostent, getservent); +weak_alias(endhostent, endservent); + +weak_alias(sethostent, setprotoent); +weak_alias(gethostent, getprotoent); +weak_alias(endhostent, endprotoent); diff --git a/05/musl-final/src/network/freeaddrinfo.c b/05/musl-final/src/network/freeaddrinfo.c new file mode 100644 index 0000000..df3798a --- /dev/null +++ b/05/musl-final/src/network/freeaddrinfo.c @@ -0,0 +1,7 @@ +#include +#include + +void freeaddrinfo(struct addrinfo *p) +{ + free(p); +} diff --git a/05/musl-final/src/network/gai_strerror.c b/05/musl-final/src/network/gai_strerror.c new file mode 100644 index 0000000..ea00bed --- /dev/null +++ b/05/musl-final/src/network/gai_strerror.c @@ -0,0 +1,21 @@ +#include + +static const char msgs[] = + "Invalid flags\0" + "Name does not resolve\0" + "Try again\0" + "Non-recoverable error\0" + "Unrecognized address family or invalid length\0" + "Unrecognized socket type\0" + "Unrecognized service\0" + "Out of memory\0" + "System error\0" + "Overflow\0" + "\0Unknown error"; + +const char *gai_strerror(int ecode) +{ + const char *s; + for (s=msgs, ecode++; ecode && *s; ecode++, s++) for (; *s; s++); + return *s ? s : s+1; +} diff --git a/05/musl-final/src/network/getaddrinfo.c b/05/musl-final/src/network/getaddrinfo.c new file mode 100644 index 0000000..90e85f6 --- /dev/null +++ b/05/musl-final/src/network/getaddrinfo.c @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "__dns.h" +#include "stdio_impl.h" + +static int is_valid(const char *host) +{ + const unsigned char *s; + if (strlen(host)-1 > 254 || mbstowcs(0, host, 0) > 255) return 0; + for (s=host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++); + return !*s; +} + +#if 0 +static int have_af(int family) +{ + struct sockaddr_in6 sin6 = { .sin6_family = family }; + socklen_t sl = family == AF_INET + ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6); + int sock = socket(family, SOCK_STREAM, 0); + int have = !bind(sock, (void *)&sin6, sl); + close(sock); + return have; +} +#endif + +#include +#include + +union sa { + struct sockaddr_in sin; + struct sockaddr_in6 sin6; +}; + +struct aibuf { + struct addrinfo ai; + union sa sa; +}; + +/* Extra slots needed for storing canonical name */ +#define EXTRA ((256+sizeof(struct aibuf)-1)/sizeof(struct aibuf)) + +int getaddrinfo(const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res) +{ + int flags = hint ? hint->ai_flags : 0; + int family = hint ? hint->ai_family : AF_UNSPEC; + int type = hint ? hint->ai_socktype : 0; + int proto = hint ? hint->ai_protocol : 0; + unsigned long port = 0; + struct aibuf *buf; + union sa sa = {{0}}; + unsigned char reply[1024]; + int i, j; + //char hostbuf[256]; + char line[512]; + FILE *f, _f; + unsigned char _buf[64]; + char *z; + int result; + int cnt; + + if (host && strlen(host)>255) return EAI_NONAME; + if (serv && strlen(serv)>32) return EAI_SERVICE; + + if (type && !proto) + proto = type==SOCK_DGRAM ? IPPROTO_UDP : IPPROTO_TCP; + if (!type && proto) + type = proto==IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM; + + if (serv) { + port = strtoul(serv, &z, 0); + if (!*z && port > 65535) return EAI_SERVICE; + if (!port) { + if (flags & AI_NUMERICSERV) return EAI_SERVICE; + + //f = fopen("/etc/services", "rb"); + return EAI_SERVICE; + } + port = htons(port); + } + + if (!host) { + if (family == AF_UNSPEC) family = AF_INET; + buf = calloc(sizeof *buf, 1+EXTRA); + if (!buf) return EAI_MEMORY; + buf->ai.ai_protocol = proto; + buf->ai.ai_socktype = type; + buf->ai.ai_addr = (void *)&buf->sa; + buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin; + buf->ai.ai_family = family; + buf->sa.sin.sin_family = family; + buf->sa.sin.sin_port = port; + if (!(flags & AI_PASSIVE)) { + if (family == AF_INET) { + 0[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=127; + 3[(uint8_t*)&buf->sa.sin.sin_addr.s_addr]=1; + } else buf[0].sa.sin6.sin6_addr.s6_addr[15] = 1; + } + *res = &buf->ai; + return 0; + } + + /* Try as a numeric address */ + if (__ipparse(&sa, family, host) >= 0) { + buf = calloc(sizeof *buf, 1+EXTRA); + if (!buf) return EAI_MEMORY; + family = sa.sin.sin_family; + buf->ai.ai_protocol = proto; + buf->ai.ai_socktype = type; + buf->ai.ai_addr = (void *)&buf->sa; + buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin; + buf->ai.ai_family = family; + buf->sa = sa; + buf->sa.sin.sin_port = port; + *res = &buf->ai; + return 0; + } + + if (flags & AI_NUMERICHOST) return EAI_NONAME; + + f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf); + if (f) while (fgets(line, sizeof line, f)) { + char *p; + size_t l = strlen(host); + + if ((p=strchr(line, '#'))) *p++='\n', *p=0; + for(p=line+1; (p=strstr(p, host)) && + (!isspace(p[-1]) || !isspace(p[l])); p++); + if (!p) continue; + __fclose_ca(f); + + /* Isolate IP address to parse */ + for (p=line; *p && !isspace(*p); p++); + *p++ = 0; + if (__ipparse(&sa, family, line) < 0) return EAI_NONAME; + + /* Allocate and fill result buffer */ + buf = calloc(sizeof *buf, 1+EXTRA); + if (!buf) return EAI_MEMORY; + family = sa.sin.sin_family; + buf->ai.ai_protocol = proto; + buf->ai.ai_socktype = type; + buf->ai.ai_addr = (void *)&buf->sa; + buf->ai.ai_addrlen = family==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin; + buf->ai.ai_family = family; + buf->sa = sa; + buf->sa.sin.sin_port = port; + + /* Extract first name as canonical name */ + for (; *p && isspace(*p); p++); + buf->ai.ai_canonname = (void *)(buf+1); + snprintf(buf->ai.ai_canonname, 256, "%s", p); + for (p=buf->ai.ai_canonname; *p && !isspace(*p); p++); + *p = 0; + if (!is_valid(buf->ai.ai_canonname)) + buf->ai.ai_canonname = 0; + + *res = &buf->ai; + return 0; + } + if (f) __fclose_ca(f); + +#if 0 + f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf); + if (f) while (fgets(line, sizeof line, f)) { + if (!isspace(line[10]) || (strncmp(line, "search", 6) + && strncmp(line, "domain", 6))) continue; + } + if (f) __fclose_ca(f); +#endif + + /* Perform one or more DNS queries for host */ + memset(reply, 0, sizeof reply); + result = __dns_query(reply, host, family, 0); + if (result < 0) return result; + + cnt = __dns_count_addrs(reply, result); + if (cnt <= 0) return EAI_NONAME; + + buf = calloc(sizeof *buf, cnt+EXTRA); + if (!buf) return EAI_MEMORY; + + i = 0; + if (family != AF_INET6) { + j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply, RR_A, 0); + while (j--) buf[i++].sa.sin.sin_family = AF_INET; + } + if (family != AF_INET) { + j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply, RR_AAAA, 0); + while (j--) buf[i++].sa.sin.sin_family = AF_INET6; + } + if (result>1) { + j = __dns_get_rr(&buf[i].sa.sin.sin_addr, sizeof *buf, 4, cnt-i, reply+512, RR_A, 0); + while (j--) buf[i++].sa.sin.sin_family = AF_INET; + j = __dns_get_rr(&buf[i].sa.sin6.sin6_addr, sizeof *buf, 16, cnt-i, reply+512, RR_AAAA, 0); + while (j--) buf[i++].sa.sin.sin_family = AF_INET6; + } + + if (__dns_get_rr((void *)&buf[cnt], 0, 256, 1, reply, RR_CNAME, 1) < 0) + strcpy((void *)&buf[cnt], host); + + for (i=0; iai; + + return 0; +} diff --git a/05/musl-final/src/network/gethostbyaddr.c b/05/musl-final/src/network/gethostbyaddr.c new file mode 100644 index 0000000..51e1c56 --- /dev/null +++ b/05/musl-final/src/network/gethostbyaddr.c @@ -0,0 +1,15 @@ +#define _GNU_SOURCE + +#include +#include +#include + +struct hostent *gethostbyaddr(const void *a, socklen_t l, int af) +{ + static struct hostent h; + static long buf[512/sizeof(long)]; + struct hostent *res; + if (gethostbyaddr_r(a, l, af, &h, + (void *)buf, sizeof buf, &res, &h_errno)) return 0; + return &h; +} diff --git a/05/musl-final/src/network/gethostbyaddr_r.c b/05/musl-final/src/network/gethostbyaddr_r.c new file mode 100644 index 0000000..cdb1d50 --- /dev/null +++ b/05/musl-final/src/network/gethostbyaddr_r.c @@ -0,0 +1,71 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +int gethostbyaddr_r(const void *a, socklen_t l, int af, + struct hostent *h, char *buf, size_t buflen, + struct hostent **res, int *err) +{ + union { + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + } sa = { .sin.sin_family = af }; + socklen_t sl = af==AF_INET6 ? sizeof sa.sin6 : sizeof sa.sin; + int i; + + /* Load address argument into sockaddr structure */ + if (af==AF_INET6 && l==16) memcpy(&sa.sin6.sin6_addr, a, 16); + else if (af==AF_INET && l==4) memcpy(&sa.sin.sin_addr, a, 4); + else { + *err = NO_RECOVERY; + return -1; + } + + /* Align buffer and check for space for pointers and ip address */ + i = (uintptr_t)buf & sizeof(char *)-1; + if (!i) i = sizeof(char *); + if (buflen <= 5*sizeof(char *)-i + l) { + errno = ERANGE; + return -1; + } + buf += sizeof(char *)-i; + buflen -= 5*sizeof(char *)-i + l; + + h->h_addr_list = (void *)buf; + buf += 2*sizeof(char *); + h->h_aliases = (void *)buf; + buf += 2*sizeof(char *); + + h->h_addr_list[0] = buf; + memcpy(h->h_addr_list[0], a, l); + buf += l; + h->h_addr_list[1] = 0; + h->h_aliases[0] = buf; + h->h_aliases[1] = 0; + + switch (getnameinfo((void *)&sa, sl, buf, buflen, 0, 0, 0)) { + case EAI_AGAIN: + *err = TRY_AGAIN; + return -1; + case EAI_OVERFLOW: + errno = ERANGE; + default: + case EAI_MEMORY: + case EAI_SYSTEM: + case EAI_FAIL: + *err = NO_RECOVERY; + return -1; + case 0: + break; + } + + h->h_addrtype = af; + h->h_name = h->h_aliases[0]; + *res = h; + return 0; +} diff --git a/05/musl-final/src/network/gethostbyname.c b/05/musl-final/src/network/gethostbyname.c new file mode 100644 index 0000000..5088a51 --- /dev/null +++ b/05/musl-final/src/network/gethostbyname.c @@ -0,0 +1,63 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include + +struct hostent *gethostbyname(const char *name) +{ + return gethostbyname2(name, AF_INET); +} + +#if 0 +struct hostent *gethostbyname(const char *name) +{ + static struct hostent h; + static char *h_aliases[3]; + static char h_canon[256]; + static char *h_addr_list[10]; + static char h_addr_data[10][4]; + static const struct addrinfo hint = { + .ai_family = AF_INET, .ai_flags = AI_CANONNAME + }; + struct addrinfo *ai, *p; + int i; + + switch (getaddrinfo(name, 0, &hint, &ai)) { + case EAI_NONAME: + h_errno = HOST_NOT_FOUND; + break; + case EAI_AGAIN: + h_errno = TRY_AGAIN; + break; + case EAI_FAIL: + h_errno = NO_RECOVERY; + break; + default: + case EAI_MEMORY: + case EAI_SYSTEM: + h_errno = NO_DATA; + break; + case 0: + break; + } + + strcpy(h_canon, ai->ai_canonname); + h.h_name = h_canon; + h.h_aliases = h_aliases; + h.h_aliases[0] = h_canon; + h.h_aliases[1] = strcmp(h_canon, name) ? (char *)name : 0; + h.h_length = 4; + h.h_addr_list = h_addr_list; + for (i=0, p=ai; iai_next) { + h.h_addr_list[i] = h_addr_data[i]; + memcpy(h.h_addr_list[i], + &((struct sockaddr_in *)p->ai_addr)->sin_addr, 4); + } + h.h_addr_list[i] = 0; + h.h_addrtype = AF_INET; + freeaddrinfo(ai); + return &h; +} +#endif diff --git a/05/musl-final/src/network/gethostbyname2.c b/05/musl-final/src/network/gethostbyname2.c new file mode 100644 index 0000000..9fbe264 --- /dev/null +++ b/05/musl-final/src/network/gethostbyname2.c @@ -0,0 +1,16 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include + +struct hostent *gethostbyname2(const char *name, int af) +{ + static struct hostent h; + static long buf[512/sizeof(long)]; + struct hostent *res; + if (gethostbyname2_r(name, af, &h, + (void *)buf, sizeof buf, &res, &h_errno)) return 0; + return &h; +} diff --git a/05/musl-final/src/network/gethostbyname2_r.c b/05/musl-final/src/network/gethostbyname2_r.c new file mode 100644 index 0000000..c2ed75b --- /dev/null +++ b/05/musl-final/src/network/gethostbyname2_r.c @@ -0,0 +1,99 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +int gethostbyname2_r(const char *name, int af, + struct hostent *h, char *buf, size_t buflen, + struct hostent **res, int *err) +{ + struct addrinfo hint = { + .ai_family = af==AF_INET6 ? af : AF_INET, + .ai_flags = AI_CANONNAME + }; + struct addrinfo *ai, *p; + int i; + size_t need; + const char *canon; + + af = hint.ai_family; + + /* Align buffer */ + i = (uintptr_t)buf & sizeof(char *)-1; + if (i) { + if (buflen < sizeof(char *)-i) { + errno = ERANGE; + return -1; + } + buf += sizeof(char *)-i; + buflen -= sizeof(char *)-i; + } + + getaddrinfo(name, 0, &hint, &ai); + switch (getaddrinfo(name, 0, &hint, &ai)) { + case EAI_NONAME: + *err = HOST_NOT_FOUND; + return -1; + case EAI_AGAIN: + *err = TRY_AGAIN; + return -1; + default: + case EAI_MEMORY: + case EAI_SYSTEM: + case EAI_FAIL: + *err = NO_RECOVERY; + return -1; + case 0: + break; + } + + h->h_addrtype = af; + h->h_length = af==AF_INET6 ? 16 : 4; + + canon = ai->ai_canonname ? ai->ai_canonname : name; + need = 4*sizeof(char *); + for (i=0, p=ai; p; i++, p=p->ai_next) + need += sizeof(char *) + h->h_length; + need += strlen(name)+1; + need += strlen(canon)+1; + + if (need > buflen) { + freeaddrinfo(ai); + errno = ERANGE; + return -1; + } + + h->h_aliases = (void *)buf; + buf += 3*sizeof(char *); + h->h_addr_list = (void *)buf; + buf += (i+1)*sizeof(char *); + + h->h_name = h->h_aliases[0] = buf; + strcpy(h->h_name, canon); + buf += strlen(h->h_name)+1; + + if (strcmp(h->h_name, name)) { + h->h_aliases[1] = buf; + strcpy(h->h_aliases[1], name); + buf += strlen(h->h_aliases[1])+1; + } else h->h_aliases[1] = 0; + + h->h_aliases[2] = 0; + + for (i=0, p=ai; p; i++, p=p->ai_next) { + h->h_addr_list[i] = (void *)buf; + buf += h->h_length; + memcpy(h->h_addr_list[i], + &((struct sockaddr_in *)p->ai_addr)->sin_addr, + h->h_length); + } + h->h_addr_list[i] = 0; + + *res = h; + freeaddrinfo(ai); + return 0; +} diff --git a/05/musl-final/src/network/gethostbyname_r.c b/05/musl-final/src/network/gethostbyname_r.c new file mode 100644 index 0000000..cd87254 --- /dev/null +++ b/05/musl-final/src/network/gethostbyname_r.c @@ -0,0 +1,11 @@ +#define _GNU_SOURCE + +#include +#include + +int gethostbyname_r(const char *name, + struct hostent *h, char *buf, size_t buflen, + struct hostent **res, int *err) +{ + return gethostbyname2_r(name, AF_INET, h, buf, buflen, res, err); +} diff --git a/05/musl-final/src/network/getnameinfo.c b/05/musl-final/src/network/getnameinfo.c new file mode 100644 index 0000000..0763ca8 --- /dev/null +++ b/05/musl-final/src/network/getnameinfo.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "__dns.h" + +int getnameinfo(const struct sockaddr *sa, socklen_t sl, + char *node, socklen_t nodelen, + char *serv, socklen_t servlen, + int flags) +{ + char buf[256]; + unsigned char reply[512]; + int af = sa->sa_family; + unsigned char *a; + + switch (af) { + case AF_INET: + a = (void *)&((struct sockaddr_in *)sa)->sin_addr; + if (sl != sizeof(struct sockaddr_in)) return EAI_FAMILY; + break; + case AF_INET6: + a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr; + if (sl != sizeof(struct sockaddr_in6)) return EAI_FAMILY; + break; + default: + return EAI_FAMILY; + } + + if (node && nodelen) { + if ((flags & NI_NUMERICHOST) + || __dns_query(reply, a, af, 1) <= 0 + || __dns_get_rr(buf, 0, 256, 1, reply, RR_PTR, 1) <= 0) + { + if (flags & NI_NAMEREQD) return EAI_NONAME; + inet_ntop(af, a, buf, sizeof buf); + } + if (strlen(buf) >= nodelen) return EAI_OVERFLOW; + strcpy(node, buf); + } + + if (serv && servlen) { + if (snprintf(buf, sizeof buf, "%d", + ntohs(((struct sockaddr_in *)sa)->sin_port))>=servlen) + return EAI_OVERFLOW; + strcpy(serv, buf); + } + + return 0; +} diff --git a/05/musl-final/src/network/getpeername.c b/05/musl-final/src/network/getpeername.c new file mode 100644 index 0000000..22effda --- /dev/null +++ b/05/musl-final/src/network/getpeername.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int getpeername(int fd, struct sockaddr *addr, socklen_t *len) +{ + return socketcall(getpeername, fd, addr, len, 0, 0, 0); +} diff --git a/05/musl-final/src/network/getservbyname.c b/05/musl-final/src/network/getservbyname.c new file mode 100644 index 0000000..0b00ce1 --- /dev/null +++ b/05/musl-final/src/network/getservbyname.c @@ -0,0 +1,12 @@ +#define _GNU_SOURCE +#include + +struct servent *getservbyname(const char *name, const char *prots) +{ + static struct servent se; + static long buf[32/sizeof(long)]; + struct servent *res; + if (getservbyname_r(name, prots, &se, (void *)buf, sizeof buf, &res)) + return 0; + return &se; +} diff --git a/05/musl-final/src/network/getservbyname_r.c b/05/musl-final/src/network/getservbyname_r.c new file mode 100644 index 0000000..5c02515 --- /dev/null +++ b/05/musl-final/src/network/getservbyname_r.c @@ -0,0 +1,41 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +int getservbyname_r(const char *name, const char *prots, + struct servent *se, char *buf, size_t buflen, struct servent **res) +{ + struct addrinfo *ai, hint = { .ai_family = AF_INET }; + int i; + + /* Align buffer */ + i = (uintptr_t)buf & sizeof(char *)-1; + if (!i) i = sizeof(char *); + if (buflen < 3*sizeof(char *)-i) { + errno = ERANGE; + return -1; + } + buf += sizeof(char *)-i; + buflen -= sizeof(char *)-i; + + if (!strcmp(prots, "tcp")) hint.ai_protocol = IPPROTO_TCP; + else if (!strcmp(prots, "udp")) hint.ai_protocol = IPPROTO_UDP; + else return -1; + + if (getaddrinfo(0, name, &hint, &ai) < 0) return -1; + + se->s_name = (char *)name; + se->s_aliases = (void *)buf; + se->s_aliases[0] = se->s_name; + se->s_aliases[1] = 0; + se->s_port = ((struct sockaddr_in *)ai->ai_addr)->sin_port; + se->s_proto = (char *)prots; + + freeaddrinfo(ai); + *res = se; + return 0; +} diff --git a/05/musl-final/src/network/getservbyport.c b/05/musl-final/src/network/getservbyport.c new file mode 100644 index 0000000..c9ecbb1 --- /dev/null +++ b/05/musl-final/src/network/getservbyport.c @@ -0,0 +1,12 @@ +#define _GNU_SOURCE +#include + +struct servent *getservbyport(int port, const char *prots) +{ + static struct servent se; + static long buf[32/sizeof(long)]; + struct servent *res; + if (getservbyport_r(port, prots, &se, (void *)buf, sizeof buf, &res)) + return 0; + return &se; +} diff --git a/05/musl-final/src/network/getservbyport_r.c b/05/musl-final/src/network/getservbyport_r.c new file mode 100644 index 0000000..004a616 --- /dev/null +++ b/05/musl-final/src/network/getservbyport_r.c @@ -0,0 +1,43 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +int getservbyport_r(int port, const char *prots, + struct servent *se, char *buf, size_t buflen, struct servent **res) +{ + int i; + struct sockaddr_in sin = { + .sin_family = AF_INET, + .sin_port = port, + }; + + /* Align buffer */ + i = (uintptr_t)buf & sizeof(char *)-1; + if (!i) i = sizeof(char *); + if (buflen < 3*sizeof(char *)-i) { + errno = ERANGE; + return -1; + } + buf += sizeof(char *)-i; + buflen -= sizeof(char *)-i; + + if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return -1; + + se->s_port = port; + se->s_proto = (char *)prots; + se->s_aliases = (void *)buf; + buf += 2*sizeof(char *); + buflen -= 2*sizeof(char *); + se->s_aliases[1] = 0; + se->s_aliases[0] = se->s_name = buf; + + if (getnameinfo((void *)&sin, sizeof sin, 0, 0, buf, buflen, + strcmp(prots, "udp") ? 0 : NI_DGRAM) < 0) return -1; + + *res = se; + return 0; +} diff --git a/05/musl-final/src/network/getsockname.c b/05/musl-final/src/network/getsockname.c new file mode 100644 index 0000000..271e3b4 --- /dev/null +++ b/05/musl-final/src/network/getsockname.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int getsockname(int fd, struct sockaddr *addr, socklen_t *len) +{ + return socketcall(getsockname, fd, addr, len, 0, 0, 0); +} diff --git a/05/musl-final/src/network/getsockopt.c b/05/musl-final/src/network/getsockopt.c new file mode 100644 index 0000000..a9e0a72 --- /dev/null +++ b/05/musl-final/src/network/getsockopt.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen) +{ + return socketcall(getsockopt, fd, level, optname, optval, optlen, 0); +} diff --git a/05/musl-final/src/network/h_errno.c b/05/musl-final/src/network/h_errno.c new file mode 100644 index 0000000..73ead04 --- /dev/null +++ b/05/musl-final/src/network/h_errno.c @@ -0,0 +1 @@ +int h_errno; diff --git a/05/musl-final/src/network/hstrerror.c b/05/musl-final/src/network/hstrerror.c new file mode 100644 index 0000000..b7a6ab6 --- /dev/null +++ b/05/musl-final/src/network/hstrerror.c @@ -0,0 +1,16 @@ +#define _GNU_SOURCE +#include + +static const char msgs[] = + "Host not found\0" + "Try again\0" + "Non-recoverable error\0" + "Address not available\0" + "\0Unknown error"; + +const char *hstrerror(int ecode) +{ + const char *s; + for (s=msgs, ecode--; ecode && *s; ecode--, s++) for (; *s; s++); + return *s ? s : s+1; +} diff --git a/05/musl-final/src/network/htonl.c b/05/musl-final/src/network/htonl.c new file mode 100644 index 0000000..b21dace --- /dev/null +++ b/05/musl-final/src/network/htonl.c @@ -0,0 +1,10 @@ +#include + +uint32_t htonl(uint32_t n) +{ + union { + uint8_t b[4]; + uint32_t i; + } u = { { n>>24, n>>16, n>>8, n } }; + return u.i; +} diff --git a/05/musl-final/src/network/htons.c b/05/musl-final/src/network/htons.c new file mode 100644 index 0000000..522504a --- /dev/null +++ b/05/musl-final/src/network/htons.c @@ -0,0 +1,10 @@ +#include + +uint16_t htons(uint16_t n) +{ + union { + uint8_t b[2]; + uint16_t s; + } u = { { n>>8, n } }; + return u.s; +} diff --git a/05/musl-final/src/network/in6addr_any.c b/05/musl-final/src/network/in6addr_any.c new file mode 100644 index 0000000..995387f --- /dev/null +++ b/05/musl-final/src/network/in6addr_any.c @@ -0,0 +1,3 @@ +#include + +const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; diff --git a/05/musl-final/src/network/in6addr_loopback.c b/05/musl-final/src/network/in6addr_loopback.c new file mode 100644 index 0000000..b96005b --- /dev/null +++ b/05/musl-final/src/network/in6addr_loopback.c @@ -0,0 +1,3 @@ +#include + +const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; diff --git a/05/musl-final/src/network/inet_addr.c b/05/musl-final/src/network/inet_addr.c new file mode 100644 index 0000000..8413728 --- /dev/null +++ b/05/musl-final/src/network/inet_addr.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include "__dns.h" + +in_addr_t inet_addr(const char *p) +{ + struct sockaddr_in sin; + if (__ipparse(&sin, AF_INET, p)) return -1; + return sin.sin_addr.s_addr; +} diff --git a/05/musl-final/src/network/inet_aton.c b/05/musl-final/src/network/inet_aton.c new file mode 100644 index 0000000..ea4ee16 --- /dev/null +++ b/05/musl-final/src/network/inet_aton.c @@ -0,0 +1,7 @@ +#include +#include + +int inet_aton(const char *cp, struct in_addr *inp) +{ + return inet_pton(AF_INET, cp, (void *)inp) > 0; +} diff --git a/05/musl-final/src/network/inet_ntoa.c b/05/musl-final/src/network/inet_ntoa.c new file mode 100644 index 0000000..71411e0 --- /dev/null +++ b/05/musl-final/src/network/inet_ntoa.c @@ -0,0 +1,10 @@ +#include +#include + +char *inet_ntoa(struct in_addr in) +{ + static char buf[16]; + unsigned char *a = (void *)∈ + snprintf(buf, sizeof buf, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); + return buf; +} diff --git a/05/musl-final/src/network/inet_ntop.c b/05/musl-final/src/network/inet_ntop.c new file mode 100644 index 0000000..3e8a6db --- /dev/null +++ b/05/musl-final/src/network/inet_ntop.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include +#include + +const char *inet_ntop(int af, const void *a0, char *s, socklen_t l) +{ + const unsigned char *a = a0; + int i, j, max, best; + char buf[100]; + + switch (af) { + case AF_INET: + if (snprintf(s, l, "%d.%d.%d.%d", a[0],a[1],a[2],a[3]) < l) + return s; + break; + case AF_INET6: + memset(buf, 'x', sizeof buf); + buf[sizeof buf-1]=0; + snprintf(buf, sizeof buf, + "%.0x%x:%.0x%x:%.0x%x:%.0x%x:%.0x%x:%.0x%x:%.0x%x:%.0x%x", + a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7], + a[8],a[9],a[10],a[11],a[12],a[13],a[14],a[15]); + /* Replace longest /(^0|:)[:0]{2,}/ with "::" */ + for (i=best=0, max=2; buf[i]; i++) { + if (i && buf[i] != ':') continue; + j = strspn(buf+i, ":0"); + if (j>max) best=i, max=j; + } + if (max>2) { + buf[best] = buf[best+1] = ':'; + strcpy(buf+best+2, buf+best+max); + } + if (strlen(buf) < l) { + strcpy(s, buf); + return s; + } + break; + default: + errno = EAFNOSUPPORT; + return 0; + } + errno = ENOSPC; + return 0; +} diff --git a/05/musl-final/src/network/inet_pton.c b/05/musl-final/src/network/inet_pton.c new file mode 100644 index 0000000..349c402 --- /dev/null +++ b/05/musl-final/src/network/inet_pton.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include "__dns.h" + +int inet_pton(int af, const char *s, void *a0) +{ + unsigned char *a = a0; + const char *z; + unsigned long x; + int i; + + /* Reimplement this because inet_pton cannot accept special v4 forms */ + if (af==AF_INET) { + for (i=0; i<4 && *s; i++) { + a[i] = x = strtoul(s, (char **)&z, 10); + if (!isdigit(*s) || z==s || (*z && *z != '.') || x>255) + return 0; + s=z+1; + } + return 0; + } else if (af==AF_INET6) { + return !__ipparse(a, AF_INET6, s); + } + + errno = EAFNOSUPPORT; + return 0; +} diff --git a/05/musl-final/src/network/listen.c b/05/musl-final/src/network/listen.c new file mode 100644 index 0000000..f84ad03 --- /dev/null +++ b/05/musl-final/src/network/listen.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int listen(int fd, int backlog) +{ + return socketcall(listen, fd, backlog, 0, 0, 0, 0); +} diff --git a/05/musl-final/src/network/ntohl.c b/05/musl-final/src/network/ntohl.c new file mode 100644 index 0000000..6437919 --- /dev/null +++ b/05/musl-final/src/network/ntohl.c @@ -0,0 +1,10 @@ +#include + +uint32_t ntohl(uint32_t n) +{ + union { + uint32_t i; + uint8_t b[4]; + } u = { n }; + return (u.b[0]<<24) | (u.b[1]<<16) | (u.b[2]<<8) | u.b[3]; +} diff --git a/05/musl-final/src/network/ntohs.c b/05/musl-final/src/network/ntohs.c new file mode 100644 index 0000000..3544a47 --- /dev/null +++ b/05/musl-final/src/network/ntohs.c @@ -0,0 +1,10 @@ +#include + +uint16_t ntohs(uint16_t n) +{ + union { + uint16_t s; + uint8_t b[2]; + } u = { n }; + return (u.b[0]<<8) | u.b[1]; +} diff --git a/05/musl-final/src/network/proto.c b/05/musl-final/src/network/proto.c new file mode 100644 index 0000000..8c25c53 --- /dev/null +++ b/05/musl-final/src/network/proto.c @@ -0,0 +1,58 @@ +#include +#include +#include + +/* do we really need all these?? */ + +static int idx; +static const unsigned char protos[][6] = { + "\000ip", + "\001icmp", + "\002igmp", + "\003ggp", + "\006tcp", + "\014pup", + "\021udp", + "\026idp", + "\377raw" + "\0\0" +}; + +void endprotoent(void) +{ + idx = 0; +} + +void setprotoent(int stayopen) +{ + idx = 0; +} + +struct protoent *getprotoent(void) +{ + static struct protoent p; + static const char *aliases; + if (!protos[idx][1]) return NULL; + p.p_proto = protos[idx][0]; + p.p_name = (char *)protos[idx++]+1; + p.p_aliases = (char **)&aliases; + return &p; +} + +struct protoent *getprotobyname(const char *name) +{ + struct protoent *p; + endprotoent(); + do p = getprotoent(); + while (p && strcmp(name, p->p_name)); + return p; +} + +struct protoent *getprotobynumber(int num) +{ + struct protoent *p; + endprotoent(); + do p = getprotoent(); + while (p && p->p_proto != num); + return p; +} diff --git a/05/musl-final/src/network/recv.c b/05/musl-final/src/network/recv.c new file mode 100644 index 0000000..d04a54a --- /dev/null +++ b/05/musl-final/src/network/recv.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t recv(int fd, void *buf, size_t len, int flags) +{ + return recvfrom(fd, buf, len, flags, 0, 0); +} diff --git a/05/musl-final/src/network/recvfrom.c b/05/musl-final/src/network/recvfrom.c new file mode 100644 index 0000000..d522293 --- /dev/null +++ b/05/musl-final/src/network/recvfrom.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *alen) +{ + ssize_t r; + CANCELPT_BEGIN; + r = socketcall(recvfrom, fd, buf, len, flags, addr, alen); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/network/recvmsg.c b/05/musl-final/src/network/recvmsg.c new file mode 100644 index 0000000..edc5f24 --- /dev/null +++ b/05/musl-final/src/network/recvmsg.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t recvmsg(int fd, struct msghdr *msg, int flags) +{ + ssize_t r; + CANCELPT_BEGIN; + r = socketcall(recvmsg, fd, msg, flags, 0, 0, 0); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/network/res_init.c b/05/musl-final/src/network/res_init.c new file mode 100644 index 0000000..cbd5b15 --- /dev/null +++ b/05/musl-final/src/network/res_init.c @@ -0,0 +1,4 @@ +int res_init() +{ + return 0; +} diff --git a/05/musl-final/src/network/res_query.c b/05/musl-final/src/network/res_query.c new file mode 100644 index 0000000..4ebeb10 --- /dev/null +++ b/05/musl-final/src/network/res_query.c @@ -0,0 +1,20 @@ +#include +#include "__dns.h" + +int res_query(const char *name, int class, int type, unsigned char *dest, int len) +{ + if (class != 1 || len < 512) + return -1; + switch(__dns_doqueries(dest, name, &type, 1)) { + case EAI_NONAME: + h_errno = HOST_NOT_FOUND; + return -1; + case EAI_AGAIN: + h_errno = TRY_AGAIN; + return -1; + case EAI_FAIL: + h_errno = NO_RECOVERY; + return -1; + } + return 512; +} diff --git a/05/musl-final/src/network/send.c b/05/musl-final/src/network/send.c new file mode 100644 index 0000000..b6ec310 --- /dev/null +++ b/05/musl-final/src/network/send.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t send(int fd, const void *buf, size_t len, int flags) +{ + return sendto(fd, buf, len, flags, 0, 0); +} diff --git a/05/musl-final/src/network/sendmsg.c b/05/musl-final/src/network/sendmsg.c new file mode 100644 index 0000000..5d1123f --- /dev/null +++ b/05/musl-final/src/network/sendmsg.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) +{ + ssize_t r; + CANCELPT_BEGIN; + r = socketcall(sendmsg, fd, msg, flags, 0, 0, 0); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/network/sendto.c b/05/musl-final/src/network/sendto.c new file mode 100644 index 0000000..1cfc621 --- /dev/null +++ b/05/musl-final/src/network/sendto.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen) +{ + ssize_t r; + CANCELPT_BEGIN; + r = socketcall(sendto, fd, buf, len, flags, addr, alen); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/network/serv.c b/05/musl-final/src/network/serv.c new file mode 100644 index 0000000..5ade6ad --- /dev/null +++ b/05/musl-final/src/network/serv.c @@ -0,0 +1,16 @@ +#include +#include +#include + +void endservent(void) +{ +} + +void setservent(int stayopen) +{ +} + +struct servent *getservent(void) +{ + return 0; +} diff --git a/05/musl-final/src/network/setsockopt.c b/05/musl-final/src/network/setsockopt.c new file mode 100644 index 0000000..c960c9c --- /dev/null +++ b/05/musl-final/src/network/setsockopt.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) +{ + return socketcall(setsockopt, fd, level, optname, optval, optlen, 0); +} diff --git a/05/musl-final/src/network/shutdown.c b/05/musl-final/src/network/shutdown.c new file mode 100644 index 0000000..10ca21a --- /dev/null +++ b/05/musl-final/src/network/shutdown.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int shutdown(int fd, int how) +{ + return socketcall(shutdown, fd, how, 0, 0, 0, 0); +} diff --git a/05/musl-final/src/network/sockatmark.c b/05/musl-final/src/network/sockatmark.c new file mode 100644 index 0000000..f474551 --- /dev/null +++ b/05/musl-final/src/network/sockatmark.c @@ -0,0 +1,10 @@ +#include +#include + +int sockatmark(int s) +{ + int ret; + if (ioctl(s, SIOCATMARK, &ret) < 0) + return -1; + return ret; +} diff --git a/05/musl-final/src/network/socket.c b/05/musl-final/src/network/socket.c new file mode 100644 index 0000000..4f1e86d --- /dev/null +++ b/05/musl-final/src/network/socket.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int socket(int domain, int type, int protocol) +{ + return socketcall(socket, domain, type, protocol, 0, 0, 0); +} diff --git a/05/musl-final/src/network/socketpair.c b/05/musl-final/src/network/socketpair.c new file mode 100644 index 0000000..b15f846 --- /dev/null +++ b/05/musl-final/src/network/socketpair.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int socketpair(int domain, int type, int protocol, int fd[2]) +{ + return socketcall(socketpair, domain, type, protocol, fd, 0, 0); +} diff --git a/05/musl-final/src/passwd/getgr_r.c b/05/musl-final/src/passwd/getgr_r.c new file mode 100644 index 0000000..5b1333e --- /dev/null +++ b/05/musl-final/src/passwd/getgr_r.c @@ -0,0 +1,53 @@ +#include "pwf.h" + +#define FIX(x) (gr->gr_##x = gr->gr_##x-line+buf) + +static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, size_t size, struct group **res) +{ + FILE *f; + char *line = 0; + size_t len = 0; + char **mem = 0; + size_t nmem = 0; + int rv = 0; + size_t i; + + f = fopen("/etc/group", "rb"); + if (!f) return errno; + + *res = 0; + while (__getgrent_a(f, gr, &line, &len, &mem, &nmem)) { + if (name && !strcmp(name, gr->gr_name) + || !name && gr->gr_gid == gid) { + if (size < len + nmem*sizeof(char *) + 32) { + rv = ERANGE; + break; + } + *res = gr; + buf += (16-(uintptr_t)buf)%16; + gr->gr_mem = (void *)buf; + buf += nmem*sizeof(char *); + memcpy(buf, line, len); + FIX(name); + FIX(passwd); + for (i=0; mem[i]; i++) + gr->gr_mem[i] = mem[i]-line+buf; + gr->gr_mem[i] = 0; + break; + } + } + free(mem); + free(line); + fclose(f); + return rv; +} + +int getgrnam_r(const char *name, struct group *gr, char *buf, size_t size, struct group **res) +{ + return getgr_r(name, 0, gr, buf, size, res); +} + +int getgruid_r(gid_t gid, struct group *gr, char *buf, size_t size, struct group **res) +{ + return getgr_r(0, gid, gr, buf, size, res); +} diff --git a/05/musl-final/src/passwd/getgrent.c b/05/musl-final/src/passwd/getgrent.c new file mode 100644 index 0000000..e9d25eb --- /dev/null +++ b/05/musl-final/src/passwd/getgrent.c @@ -0,0 +1,39 @@ +#include "pwf.h" + +static FILE *f; + +void setgrent() +{ + if (f) fclose(f); + f = 0; +} + +weak_alias(setgrent, endgrent); + +struct group *getgrent() +{ + static char *line, **mem; + static struct group gr; + size_t size=0, nmem=0; + if (!f) f = fopen("/etc/group", "rb"); + if (!f) return 0; + return __getgrent_a(f, &gr, &line, &size, &mem, &nmem); +} + +struct group *getgrgid(gid_t gid) +{ + struct group *gr; + setgrent(); + while ((gr=getgrent()) && gr->gr_gid != gid); + endgrent(); + return gr; +} + +struct group *getgrnam(const char *name) +{ + struct group *gr; + setgrent(); + while ((gr=getgrent()) && strcmp(gr->gr_name, name)); + endgrent(); + return gr; +} diff --git a/05/musl-final/src/passwd/getgrent_a.c b/05/musl-final/src/passwd/getgrent_a.c new file mode 100644 index 0000000..ccb51d5 --- /dev/null +++ b/05/musl-final/src/passwd/getgrent_a.c @@ -0,0 +1,46 @@ +#include "pwf.h" + +struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem) +{ + ssize_t l; + char *s, *mems; + size_t i; + + for (;;) { + if ((l=getline(line, size, f)) < 0) { + free(*line); + *line = 0; + return 0; + } + line[0][l-1] = 0; + + s = line[0]; + gr->gr_name = s++; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; gr->gr_passwd = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; gr->gr_gid = atoi(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; mems = s; + break; + } + + for (*nmem=!!*s; *s; s++) + if (*s==',') ++*nmem; + free(*mem); + *mem = calloc(sizeof(char *), *nmem+1); + if (!*mem) { + free(*line); + *line = 0; + return 0; + } + mem[0][0] = mems; + for (s=mems, i=0; *s; s++) + if (*s==',') *s++ = 0, mem[0][++i] = s; + mem[0][++i] = 0; + gr->gr_mem = *mem; + return gr; +} diff --git a/05/musl-final/src/passwd/getpw_r.c b/05/musl-final/src/passwd/getpw_r.c new file mode 100644 index 0000000..7b331e8 --- /dev/null +++ b/05/musl-final/src/passwd/getpw_r.c @@ -0,0 +1,46 @@ +#include "pwf.h" + +#define FIX(x) (pw->pw_##x = pw->pw_##x-line+buf) + +static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res) +{ + FILE *f; + char *line = 0; + size_t len = 0; + int rv = 0; + + f = fopen("/etc/passwd", "rb"); + if (!f) return errno; + + *res = 0; + while (__getpwent_a(f, pw, &line, &len)) { + if (name && !strcmp(name, pw->pw_name) + || !name && pw->pw_uid == uid) { + if (size < len) { + rv = ERANGE; + break; + } + *res = pw; + memcpy(buf, line, len); + FIX(name); + FIX(passwd); + FIX(gecos); + FIX(dir); + FIX(shell); + break; + } + } + free(line); + fclose(f); + return rv; +} + +int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size, struct passwd **res) +{ + return getpw_r(name, 0, pw, buf, size, res); +} + +int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res) +{ + return getpw_r(0, uid, pw, buf, size, res); +} diff --git a/05/musl-final/src/passwd/getpwent.c b/05/musl-final/src/passwd/getpwent.c new file mode 100644 index 0000000..dabd411 --- /dev/null +++ b/05/musl-final/src/passwd/getpwent.c @@ -0,0 +1,39 @@ +#include "pwf.h" + +static FILE *f; + +void setpwent() +{ + if (f) fclose(f); + f = 0; +} + +weak_alias(setpwent, endpwent); + +struct passwd *getpwent() +{ + static char *line; + static struct passwd pw; + size_t size=0; + if (!f) f = fopen("/etc/passwd", "rb"); + if (!f) return 0; + return __getpwent_a(f, &pw, &line, &size); +} + +struct passwd *getpwuid(uid_t uid) +{ + struct passwd *pw; + setpwent(); + while ((pw=getpwent()) && pw->pw_uid != uid); + endpwent(); + return pw; +} + +struct passwd *getpwnam(const char *name) +{ + struct passwd *pw; + setpwent(); + while ((pw=getpwent()) && strcmp(pw->pw_name, name)); + endpwent(); + return pw; +} diff --git a/05/musl-final/src/passwd/getpwent_a.c b/05/musl-final/src/passwd/getpwent_a.c new file mode 100644 index 0000000..aaf84ed --- /dev/null +++ b/05/musl-final/src/passwd/getpwent_a.c @@ -0,0 +1,37 @@ +#include "pwf.h" + +struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size) +{ + ssize_t l; + char *s; + for (;;) { + if ((l=getline(line, size, f)) < 0) { + free(*line); + *line = 0; + return 0; + } + line[0][l-1] = 0; + + s = line[0]; + pw->pw_name = s++; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_passwd = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_uid = atoi(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_gid = atoi(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_gecos = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_dir = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_shell = s; + return pw; + } +} diff --git a/05/musl-final/src/passwd/getspent.c b/05/musl-final/src/passwd/getspent.c new file mode 100644 index 0000000..8574a48 --- /dev/null +++ b/05/musl-final/src/passwd/getspent.c @@ -0,0 +1,14 @@ +#include "pwf.h" + +void setspent() +{ +} + +void endspent() +{ +} + +struct spwd *getspent() +{ + return 0; +} diff --git a/05/musl-final/src/passwd/getspnam.c b/05/musl-final/src/passwd/getspnam.c new file mode 100644 index 0000000..041f896 --- /dev/null +++ b/05/musl-final/src/passwd/getspnam.c @@ -0,0 +1,17 @@ +#include "pwf.h" + +#define LINE_LIM 256 + +struct spwd *getspnam(const char *name) +{ + static struct spwd sp; + static char *line; + struct spwd *res; + int e; + + if (!line) line = malloc(LINE_LIM); + if (!line) return 0; + e = getspnam_r(name, &sp, line, LINE_LIM, &res); + if (e) errno = e; + return res; +} diff --git a/05/musl-final/src/passwd/getspnam_r.c b/05/musl-final/src/passwd/getspnam_r.c new file mode 100644 index 0000000..d21ca81 --- /dev/null +++ b/05/musl-final/src/passwd/getspnam_r.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include "pwf.h" + +/* This implementation support Openwall-style TCB passwords in place of + * traditional shadow, if the appropriate directories and files exist. + * Thus, it is careful to avoid following symlinks or blocking on fifos + * which a malicious user might create in place of his or her TCB shadow + * file. It also avoids any allocation to prevent memory-exhaustion + * attacks via huge TCB shadow files. */ + +int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct spwd **res) +{ + char path[20+NAME_MAX]; + FILE *f = 0; + int rv = 0; + int fd; + size_t k, l = strlen(name); + char *s; + int skip = 0; + + *res = 0; + + /* Disallow potentially-malicious user names */ + if (*name=='.' || strchr(name, '/') || !l) + return EINVAL; + + /* Buffer size must at least be able to hold name, plus some.. */ + if (size < l+100) return ERANGE; + + /* Protect against truncation */ + if (snprintf(path, sizeof path, "/etc/tcb/%s/shadow", name) >= sizeof path) + return EINVAL; + + fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK); + if (fd >= 0) { + struct stat st = { 0 }; + errno = EINVAL; + if (fstat(fd, &st) || !S_ISREG(st.st_mode) || !(f = fdopen(fd, "rb"))) { + close(fd); + return errno; + } + } else { + f = fopen("/etc/shadow", "rb"); + if (!f) return errno; + } + + while (fgets(buf, size, f) && (k=strlen(buf))>0) { + if (skip || strncmp(name, buf, l)) { + skip = buf[k-1] != '\n'; + continue; + } + if (buf[k-1] != '\n') { + rv = ERANGE; + break; + } + buf[k-1] = 0; + + s = buf; + sp->sp_namp = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_pwdp = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_lstchg = atol(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_min = atol(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_max = atol(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_warn = atol(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_inact = atol(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_expire = atol(s); + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; sp->sp_flag = atol(s); + *res = sp; + break; + } + fclose(f); + return rv; +} diff --git a/05/musl-final/src/passwd/lckpwdf.c b/05/musl-final/src/passwd/lckpwdf.c new file mode 100644 index 0000000..2feda61 --- /dev/null +++ b/05/musl-final/src/passwd/lckpwdf.c @@ -0,0 +1,11 @@ +#include + +int lckpwdf() +{ + return 0; +} + +int ulckpwdf() +{ + return 0; +} diff --git a/05/musl-final/src/passwd/pwf.h b/05/musl-final/src/passwd/pwf.h new file mode 100644 index 0000000..0a76ef8 --- /dev/null +++ b/05/musl-final/src/passwd/pwf.h @@ -0,0 +1,13 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size); +struct spwd *__getspent_a(FILE *f, struct spwd *sp, char **line, size_t *size); +struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem); diff --git a/05/musl-final/src/prng/__rand48_step.c b/05/musl-final/src/prng/__rand48_step.c new file mode 100644 index 0000000..755b4f2 --- /dev/null +++ b/05/musl-final/src/prng/__rand48_step.c @@ -0,0 +1,14 @@ +#include +#include + +uint64_t __rand48_step(unsigned short *xi, unsigned short *lc) +{ + uint64_t a, x; + x = xi[0] | xi[1]<<16 | xi[2]+0ULL<<32; + a = lc[0] | lc[1]<<16 | lc[2]+0ULL<<32; + x = a*x + lc[3]; + xi[0] = x; + xi[1] = x>>16; + xi[2] = x>>32; + return x & 0xffffffffffffull; +} diff --git a/05/musl-final/src/prng/__seed48.c b/05/musl-final/src/prng/__seed48.c new file mode 100644 index 0000000..05a4539 --- /dev/null +++ b/05/musl-final/src/prng/__seed48.c @@ -0,0 +1 @@ +unsigned short __seed48[7] = { 0, 0, 0, 0xe66d, 0xdeec, 0x5, 0xb }; diff --git a/05/musl-final/src/prng/drand48.c b/05/musl-final/src/prng/drand48.c new file mode 100644 index 0000000..d808353 --- /dev/null +++ b/05/musl-final/src/prng/drand48.c @@ -0,0 +1,19 @@ +#include +#include + +uint64_t __rand48_step(unsigned short *xi, unsigned short *lc); +extern unsigned short __seed48[7]; + +double erand48(unsigned short s[3]) +{ + union { + uint64_t u; + double f; + } x = { 0x3ff0000000000000ULL | __rand48_step(s, __seed48+3)<<4 }; + return x.f - 1.0; +} + +double drand48(void) +{ + return erand48(__seed48); +} diff --git a/05/musl-final/src/prng/lcong48.c b/05/musl-final/src/prng/lcong48.c new file mode 100644 index 0000000..32b27d4 --- /dev/null +++ b/05/musl-final/src/prng/lcong48.c @@ -0,0 +1,9 @@ +#include +#include + +extern unsigned short __seed48[7]; + +void lcong48(unsigned short p[7]) +{ + memcpy(__seed48, p, sizeof __seed48); +} diff --git a/05/musl-final/src/prng/lrand48.c b/05/musl-final/src/prng/lrand48.c new file mode 100644 index 0000000..a3c4e4e --- /dev/null +++ b/05/musl-final/src/prng/lrand48.c @@ -0,0 +1,15 @@ +#include +#include + +uint64_t __rand48_step(unsigned short *xi, unsigned short *lc); +extern unsigned short __seed48[7]; + +long nrand48(unsigned short s[3]) +{ + return __rand48_step(s, __seed48+3) >> 17; +} + +long lrand48(void) +{ + return nrand48(__seed48); +} diff --git a/05/musl-final/src/prng/mrand48.c b/05/musl-final/src/prng/mrand48.c new file mode 100644 index 0000000..ee650fc --- /dev/null +++ b/05/musl-final/src/prng/mrand48.c @@ -0,0 +1,15 @@ +#include +#include + +uint64_t __rand48_step(unsigned short *xi, unsigned short *lc); +extern unsigned short __seed48[7]; + +long jrand48(unsigned short s[3]) +{ + return __rand48_step(s, __seed48+3) >> 16; +} + +long mrand48(void) +{ + return jrand48(__seed48); +} diff --git a/05/musl-final/src/prng/rand.c b/05/musl-final/src/prng/rand.c new file mode 100644 index 0000000..e3ce634 --- /dev/null +++ b/05/musl-final/src/prng/rand.c @@ -0,0 +1,13 @@ +#include + +static unsigned seed; + +void srand(unsigned s) +{ + seed = s-1; +} + +int rand(void) +{ + return (seed = (seed+1) * 1103515245 + 12345 - 1)+1 & 0x7fffffff; +} diff --git a/05/musl-final/src/prng/rand_r.c b/05/musl-final/src/prng/rand_r.c new file mode 100644 index 0000000..e96cfba --- /dev/null +++ b/05/musl-final/src/prng/rand_r.c @@ -0,0 +1,6 @@ +#include + +int rand_r(unsigned *seed) +{ + return (*seed = *seed * 1103515245 + 12345) & 0x7fffffff; +} diff --git a/05/musl-final/src/prng/random.c b/05/musl-final/src/prng/random.c new file mode 100644 index 0000000..e6b7fd1 --- /dev/null +++ b/05/musl-final/src/prng/random.c @@ -0,0 +1,8 @@ +#include + +/* FIXME */ + +long random() +{ + return rand(); +} diff --git a/05/musl-final/src/prng/seed48.c b/05/musl-final/src/prng/seed48.c new file mode 100644 index 0000000..e0699c0 --- /dev/null +++ b/05/musl-final/src/prng/seed48.c @@ -0,0 +1,12 @@ +#include +#include + +extern unsigned short __seed48[7]; + +unsigned short *seed48(unsigned short *s) +{ + static unsigned short p[3]; + memcpy(p, __seed48, sizeof p); + memcpy(__seed48, s, sizeof p); + return p; +} diff --git a/05/musl-final/src/prng/srand48.c b/05/musl-final/src/prng/srand48.c new file mode 100644 index 0000000..0a56f6a --- /dev/null +++ b/05/musl-final/src/prng/srand48.c @@ -0,0 +1,6 @@ +#include + +void srand48(long seed) +{ + seed48((unsigned short [3]){ 0x330e, seed, seed>>16 }); +} diff --git a/05/musl-final/src/prng/srandom.c b/05/musl-final/src/prng/srandom.c new file mode 100644 index 0000000..77f4dcb --- /dev/null +++ b/05/musl-final/src/prng/srandom.c @@ -0,0 +1,8 @@ +#include + +/* FIXME */ + +void srandom(unsigned seed) +{ + return srand(seed); +} diff --git a/05/musl-final/src/process/execl.c b/05/musl-final/src/process/execl.c new file mode 100644 index 0000000..4c6eaa9 --- /dev/null +++ b/05/musl-final/src/process/execl.c @@ -0,0 +1,20 @@ +#include +#include + +int execl(const char *path, ...) +{ + int argc; + va_list ap; + va_start(ap, path); + for (argc=0; va_arg(ap, const char *); argc++); + va_end(ap); + { + int i; + char *argv[argc+1]; + va_start(ap, path); + for (i=0; i +#include + +int execle(const char *path, ...) +{ + int argc; + va_list ap; + va_start(ap, path); + for (argc=0; va_arg(ap, const char *); argc++); + va_end(ap); + { + int i; + char *argv[argc+1]; + char **envp; + va_start(ap, path); + for (i=0; i +#include + +int execlp(const char *file, ...) +{ + int argc; + va_list ap; + va_start(ap, file); + for (argc=0; va_arg(ap, const char *); argc++); + va_end(ap); + { + int i; + char *argv[argc+1]; + va_start(ap, file); + for (i=0; i + +extern char **__environ; + +int execv(const char *path, char *const argv[]) +{ + return execve(path, argv, __environ); +} diff --git a/05/musl-final/src/process/execve.c b/05/musl-final/src/process/execve.c new file mode 100644 index 0000000..2a0b62d --- /dev/null +++ b/05/musl-final/src/process/execve.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" + +int execve(const char *path, char *const argv[], char *const envp[]) +{ + /* do we need to use environ if envp is null? */ + return syscall3(__NR_execve, (long)path, (long)argv, (long)envp); +} diff --git a/05/musl-final/src/process/execvp.c b/05/musl-final/src/process/execvp.c new file mode 100644 index 0000000..d799dda --- /dev/null +++ b/05/musl-final/src/process/execvp.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +extern char **__environ; + +int execvp(const char *file, char *const argv[]) +{ + const char *p, *z, *path = getenv("PATH"); + int l; + + if (strchr(file, '/')) + return execve(file, argv, __environ); + + /* FIXME: integer overflows */ + if (!path) path = "/usr/local/bin:/bin:/usr/bin"; + l = strlen(file) + strlen(path) + 2; + + for(p=path; p && *p; p=z) { + char b[l]; + z = strchr(p, ':'); + if (z) { + memcpy(b, p, z-p); + b[z++-p] = 0; + } else strcpy(b, p); + strcat(b, "/"); + strcat(b, file); + if (!access(b, X_OK)) + return execve(b, argv, __environ); + } + errno = ENOENT; + return -1; +} diff --git a/05/musl-final/src/process/fork.c b/05/musl-final/src/process/fork.c new file mode 100644 index 0000000..1213f0f --- /dev/null +++ b/05/musl-final/src/process/fork.c @@ -0,0 +1,9 @@ +#include +#include "syscall.h" + +/* FIXME: add support for atfork stupidity */ + +pid_t fork(void) +{ + return syscall0(__NR_fork); +} diff --git a/05/musl-final/src/process/system.c b/05/musl-final/src/process/system.c new file mode 100644 index 0000000..0f1c07b --- /dev/null +++ b/05/musl-final/src/process/system.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +int system(const char *cmd) +{ + pid_t pid; + sigset_t old, new; + struct sigaction sa, oldint, oldquit; + int status; + + if (!cmd) return 1; + + sa.sa_handler = SIG_IGN; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + + sigaction(SIGINT, &sa, &oldint); + sigaction(SIGQUIT, &sa, &oldquit); + sigaddset(&sa.sa_mask, SIGCHLD); + sigprocmask(SIG_BLOCK, &new, &old); + + pid = fork(); + if (pid <= 0) { + sigaction(SIGINT, &oldint, NULL); + sigaction(SIGQUIT, &oldquit, NULL); + sigprocmask(SIG_SETMASK, &old, NULL); + if (pid == 0) { + execl("/bin/sh", "sh", "-c", cmd, (char *)0); + _exit(127); + } + return -1; + } + while (waitpid(pid, &status, 0) == -1) + if (errno != EINTR) { + status = -1; + break; + } + sigaction(SIGINT, &oldint, NULL); + sigaction(SIGQUIT, &oldquit, NULL); + sigprocmask(SIG_SETMASK, &old, NULL); + return status; +} diff --git a/05/musl-final/src/process/vfork.c b/05/musl-final/src/process/vfork.c new file mode 100644 index 0000000..32a7a6e --- /dev/null +++ b/05/musl-final/src/process/vfork.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" + +pid_t vfork(void) +{ + /* vfork syscall cannot be made from C code */ + return syscall0(__NR_fork); +} diff --git a/05/musl-final/src/process/wait.c b/05/musl-final/src/process/wait.c new file mode 100644 index 0000000..34da102 --- /dev/null +++ b/05/musl-final/src/process/wait.c @@ -0,0 +1,6 @@ +#include + +pid_t wait(int *status) +{ + return waitpid((pid_t)-1, status, 0); +} diff --git a/05/musl-final/src/process/waitid.c b/05/musl-final/src/process/waitid.c new file mode 100644 index 0000000..0ec0d55 --- /dev/null +++ b/05/musl-final/src/process/waitid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int waitid(idtype_t type, id_t id, siginfo_t *info, int options) +{ + return syscall5(__NR_waitid, type, id, (long)info, options, 0); +} diff --git a/05/musl-final/src/process/waitpid.c b/05/musl-final/src/process/waitpid.c new file mode 100644 index 0000000..0ddcd15 --- /dev/null +++ b/05/musl-final/src/process/waitpid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t waitpid(pid_t pid, int *status, int options) +{ + return syscall4(__NR_wait4, pid, (long)status, options, 0); +} diff --git a/05/musl-final/src/regex/fnmatch.c b/05/musl-final/src/regex/fnmatch.c new file mode 100644 index 0000000..5f2fccd --- /dev/null +++ b/05/musl-final/src/regex/fnmatch.c @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include + +static int next(const char **s) +{ + wchar_t c; + int l = mbtowc(&c, *s, MB_LEN_MAX); + /* hack to allow literal matches of invalid byte sequences */ + if (l < 0) return (unsigned char)*(*s)++ - 0x100; + *s += l; + return c; +} + +#define BRACKET_ERROR -0x100 +#define BRACKET_NOCHAR -0x101 + +static int bracket_next(const char **s) +{ + int c; + int type; + if (**s == '[') { + type = *(*s+1); + if (type == '.' || type == '=') { + *s += 2; + c = next(s); + if (c <= 0) return BRACKET_ERROR; + if (**s == type && *(*s+1) == ']') { + *s += 2; + return c; + } + for (; **s && (**s != type || *(*s+1) != ']'); (*s)++); + if (!**s) return BRACKET_ERROR; + *s += 2; + return BRACKET_NOCHAR; + } + } + c = next(s); + if (c <= 0) return BRACKET_ERROR; + return c; +} + +#define __FNM_CONT 0x8000 + +int fnmatch(const char *p, const char *s, int flags) +{ + int c, d, k; + int not; + int match; + int first; + int no_slash = (flags & FNM_PATHNAME) ? '/' : 0; + int no_period = (flags & FNM_PERIOD) && !(flags & __FNM_CONT) ? '.' : 0x100; + + flags |= __FNM_CONT; + + while ((c = *p++)) { + switch (c) { + case '?': + k = next(&s); + if (!k || k == no_period || k == no_slash) + return FNM_NOMATCH; + break; + case '\\': + if (!(flags & FNM_NOESCAPE)) { + c = *p++; + goto literal; + } + if (*s++ != c) return FNM_NOMATCH; + break; + case '*': + for (; *p == '*'; p++); + if (*p && !*s) return FNM_NOMATCH; + if (*s == no_period) + return FNM_NOMATCH; + if (!*p && (!no_slash || !strchr(s, no_slash))) + return 0; + for (; *s; s++) + if (!fnmatch(p, s, flags)) + return 0; + else if (*s == no_slash) + break; + return FNM_NOMATCH; + case '[': + not = (*p == '!' || *p == '^'); + if (not) p++; + k = next(&s); + if (!k || k == no_slash || k == no_period) + return FNM_NOMATCH; + match = 0; + first = 1; + for (;;) { + if (!*p) return FNM_NOMATCH; + if (*p == ']' && !first) break; + first = 0; + if (*p == '[' && *(p+1) == ':') { + const char *z; + p += 2; + for (z=p; *z && (*z != ':' || *(z+1) != ']'); z++); + if (!*z || z-p > 32) { /* FIXME: symbolic const? */ + return FNM_NOMATCH; + } else { + char class[z-p+1]; + memcpy(class, p, z-p); + class[z-p] = 0; + if (iswctype(k, wctype(class))) + match = 1; + } + p = z+2; + continue; + } + c = bracket_next(&p); + if (c == BRACKET_ERROR) + return FNM_NOMATCH; + if (c == BRACKET_NOCHAR) + continue; + if (*p == '-' && *(p+1) != ']') { + p++; + d = bracket_next(&p); + if (d == BRACKET_ERROR) + return FNM_NOMATCH; + if (d == BRACKET_NOCHAR) + continue; + if (k >= c && k <= d) + match = 1; + continue; + } + if (k == c) match = 1; + } + p++; + if (not == match) + return FNM_NOMATCH; + break; + default: + literal: + if (*s++ != c) + return FNM_NOMATCH; + if (c == no_slash && (flags & FNM_PERIOD)) { + no_period = '.'; + continue; + } + break; + } + no_period = 0x100; + } + if (*s) return FNM_NOMATCH; + return 0; +} diff --git a/05/musl-final/src/regex/glob.c b/05/musl-final/src/regex/glob.c new file mode 100644 index 0000000..9a70f0b --- /dev/null +++ b/05/musl-final/src/regex/glob.c @@ -0,0 +1,238 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +struct match +{ + struct match *next; + char name[1]; +}; + +static int is_literal(const char *p, int useesc) +{ + int bracket = 0; + for (; *p; p++) { + switch (*p) { + case '\\': + if (!useesc) break; + case '?': + case '*': + return 0; + case '[': + bracket = 1; + break; + case ']': + if (bracket) return 0; + break; + } + } + return 1; +} + +static int append(struct match **tail, const char *name, size_t len, int mark) +{ + struct match *new = malloc(sizeof(struct match) + len + 1); + if (!new) return -1; + (*tail)->next = new; + new->next = NULL; + strcpy(new->name, name); + if (mark) strcat(new->name, "/"); + *tail = new; + return 0; +} + +static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail) +{ + DIR *dir; + long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)]; + struct dirent *de; + char pat[strlen(p)+1]; + char *p2; + size_t l = strlen(d); + int literal; + int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD; + int error; + + if ((p2 = strchr(p, '/'))) { + strcpy(pat, p); + pat[p2-p] = 0; + for (; *p2 == '/'; p2++); + p = pat; + } + literal = is_literal(p, !(flags & GLOB_NOESCAPE)); + if (*d == '/' && !*(d+1)) l = 0; + + /* rely on opendir failing for nondirectory objects */ + dir = opendir(*d ? d : "."); + error = errno; + if (!dir) { + /* this is not an error -- we let opendir call stat for us */ + if (error == ENOTDIR) return 0; + if (error == EACCES && !*p) { + struct stat st; + if (!stat(d, &st) && S_ISDIR(st.st_mode)) { + if (append(tail, d, l, l)) + return GLOB_NOSPACE; + return 0; + } + } + if (errfunc(d, error) || (flags & GLOB_ERR)) + return GLOB_ABORTED; + return 0; + } + if (!*p) { + error = append(tail, d, l, l) ? GLOB_NOSPACE : 0; + closedir(dir); + return error; + } + while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) { + char namebuf[l+de->d_reclen+2], *name = namebuf; + if (!literal && fnmatch(p, de->d_name, fnm_flags)) + continue; + if (literal && strcmp(p, de->d_name)) + continue; + if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12)) + continue; + if (*d) { + memcpy(name, d, l); + name[l] = '/'; + strcpy(name+l+1, de->d_name); + } else { + name = de->d_name; + } + if (p2) { + if ((error = match_in_dir(name, p2, flags, errfunc, tail))) { + closedir(dir); + return error; + } + } else { + int mark = 0; + if (flags & GLOB_MARK) { + if (de->d_type) + mark = S_ISDIR(de->d_type<<12); + else { + struct stat st; + stat(name, &st); + mark = S_ISDIR(st.st_mode); + } + } + if (append(tail, name, l+de->d_reclen+1, mark)) { + closedir(dir); + return GLOB_NOSPACE; + } + } + } + closedir(dir); + if (error && (errfunc(d, error) || (flags & GLOB_ERR))) + return GLOB_ABORTED; + return 0; +} + +static int ignore_err(const char *path, int err) +{ + return 0; +} + +static void freelist(struct match *head) +{ + struct match *match, *next; + for (match=head->next; match; match=next) { + next = match->next; + free(match); + } +} + +static int sort(const void *a, const void *b) +{ + return strcmp(*(const char **)a, *(const char **)b); +} + +int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g) +{ + const char *p=pat, *d; + struct match head = { .next = NULL }, *tail = &head; + size_t cnt, i; + size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0; + int error = 0; + + if (*p == '/') { + for (; *p == '/'; p++); + d = "/"; + } else { + d = ""; + } + + if (!errfunc) errfunc = ignore_err; + + if (!(flags & GLOB_APPEND)) { + g->gl_offs = offs; + g->gl_pathc = 0; + g->gl_pathv = NULL; + } + + if (*p) error = match_in_dir(d, p, flags, errfunc, &tail); + if (error == GLOB_NOSPACE) { + freelist(&head); + return error; + } + + for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++); + if (!cnt) { + if (flags & GLOB_NOCHECK) { + tail = &head; + if (append(&tail, pat, strlen(pat), 0)) + return GLOB_NOSPACE; + cnt++; + } else + return GLOB_NOMATCH; + } + + if (flags & GLOB_APPEND) { + char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)); + if (!pathv) { + freelist(&head); + return GLOB_NOSPACE; + } + g->gl_pathv = pathv; + offs += g->gl_pathc; + } else { + g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *)); + if (!g->gl_pathv) { + freelist(&head); + return GLOB_NOSPACE; + } + for (i=0; igl_pathv[i] = NULL; + } + for (i=0, tail=head.next; inext, i++) + g->gl_pathv[offs + i] = tail->name; + g->gl_pathv[offs + i] = NULL; + g->gl_pathc += cnt; + + if (!(flags & GLOB_NOSORT)) + qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort); + + return error; +} + +void globfree(glob_t *g) +{ + size_t i; + for (i=0; igl_pathc; i++) + free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name)); + free(g->gl_pathv); + g->gl_pathc = 0; + g->gl_pathv = NULL; +} + +LFS64(glob); +LFS64(globfree); diff --git a/05/musl-final/src/regex/regcomp.c b/05/musl-final/src/regex/regcomp.c new file mode 100644 index 0000000..3307942 --- /dev/null +++ b/05/musl-final/src/regex/regcomp.c @@ -0,0 +1,3362 @@ +/* + regcomp.c - TRE POSIX compatible regex compilation functions. + + Copyright (c) 2001-2006 Ville Laurikari + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#include +#include +#include +#include +#include +#include + +#include "tre.h" + +#include + +/*********************************************************************** + from tre-ast.c and tre-ast.h +***********************************************************************/ + +/* The different AST node types. */ +typedef enum { + LITERAL, + CATENATION, + ITERATION, + UNION +} tre_ast_type_t; + +/* Special subtypes of TRE_LITERAL. */ +#define EMPTY -1 /* Empty leaf (denotes empty string). */ +#define ASSERTION -2 /* Assertion leaf. */ +#define TAG -3 /* Tag leaf. */ +#define BACKREF -4 /* Back reference leaf. */ + +#define IS_SPECIAL(x) ((x)->code_min < 0) +#define IS_EMPTY(x) ((x)->code_min == EMPTY) +#define IS_ASSERTION(x) ((x)->code_min == ASSERTION) +#define IS_TAG(x) ((x)->code_min == TAG) +#define IS_BACKREF(x) ((x)->code_min == BACKREF) + +/* Taken from tre-compile.h */ +typedef struct { + int position; + int code_min; + int code_max; + int *tags; + int assertions; + tre_ctype_t class; + tre_ctype_t *neg_classes; + int backref; +} tre_pos_and_tags_t; + +/* A generic AST node. All AST nodes consist of this node on the top + level with `obj' pointing to the actual content. */ +typedef struct { + tre_ast_type_t type; /* Type of the node. */ + void *obj; /* Pointer to actual node. */ + int nullable; + int submatch_id; + int num_submatches; + int num_tags; + tre_pos_and_tags_t *firstpos; + tre_pos_and_tags_t *lastpos; +} tre_ast_node_t; + + +/* A "literal" node. These are created for assertions, back references, + tags, matching parameter settings, and all expressions that match one + character. */ +typedef struct { + long code_min; + long code_max; + int position; + tre_ctype_t class; + tre_ctype_t *neg_classes; +} tre_literal_t; + +/* A "catenation" node. These are created when two regexps are concatenated. + If there are more than one subexpressions in sequence, the `left' part + holds all but the last, and `right' part holds the last subexpression + (catenation is left associative). */ +typedef struct { + tre_ast_node_t *left; + tre_ast_node_t *right; +} tre_catenation_t; + +/* An "iteration" node. These are created for the "*", "+", "?", and "{m,n}" + operators. */ +typedef struct { + /* Subexpression to match. */ + tre_ast_node_t *arg; + /* Minimum number of consecutive matches. */ + int min; + /* Maximum number of consecutive matches. */ + int max; +} tre_iteration_t; + +/* An "union" node. These are created for the "|" operator. */ +typedef struct { + tre_ast_node_t *left; + tre_ast_node_t *right; +} tre_union_t; + +static tre_ast_node_t * +tre_ast_new_node(tre_mem_t mem, tre_ast_type_t type, size_t size) +{ + tre_ast_node_t *node; + + node = tre_mem_calloc(mem, sizeof(*node)); + if (!node) + return NULL; + node->obj = tre_mem_calloc(mem, size); + if (!node->obj) + return NULL; + node->type = type; + node->nullable = -1; + node->submatch_id = -1; + + return node; +} + +static tre_ast_node_t * +tre_ast_new_literal(tre_mem_t mem, int code_min, int code_max, int position) +{ + tre_ast_node_t *node; + tre_literal_t *lit; + + node = tre_ast_new_node(mem, LITERAL, sizeof(tre_literal_t)); + if (!node) + return NULL; + lit = node->obj; + lit->code_min = code_min; + lit->code_max = code_max; + lit->position = position; + + return node; +} + +static tre_ast_node_t * +tre_ast_new_iter(tre_mem_t mem, tre_ast_node_t *arg, int min, int max) +{ + tre_ast_node_t *node; + tre_iteration_t *iter; + + node = tre_ast_new_node(mem, ITERATION, sizeof(tre_iteration_t)); + if (!node) + return NULL; + iter = node->obj; + iter->arg = arg; + iter->min = min; + iter->max = max; + node->num_submatches = arg->num_submatches; + + return node; +} + +static tre_ast_node_t * +tre_ast_new_union(tre_mem_t mem, tre_ast_node_t *left, tre_ast_node_t *right) +{ + tre_ast_node_t *node; + + node = tre_ast_new_node(mem, UNION, sizeof(tre_union_t)); + if (node == NULL) + return NULL; + ((tre_union_t *)node->obj)->left = left; + ((tre_union_t *)node->obj)->right = right; + node->num_submatches = left->num_submatches + right->num_submatches; + + return node; +} + +static tre_ast_node_t * +tre_ast_new_catenation(tre_mem_t mem, tre_ast_node_t *left, + tre_ast_node_t *right) +{ + tre_ast_node_t *node; + + node = tre_ast_new_node(mem, CATENATION, sizeof(tre_catenation_t)); + if (node == NULL) + return NULL; + ((tre_catenation_t *)node->obj)->left = left; + ((tre_catenation_t *)node->obj)->right = right; + node->num_submatches = left->num_submatches + right->num_submatches; + + return node; +} + +/*********************************************************************** + from tre-stack.c and tre-stack.h +***********************************************************************/ + +/* Just to save some typing. */ +#define STACK_PUSH(s, value) \ + do \ + { \ + status = tre_stack_push(s, (void *)(value)); \ + } \ + while (0) + +#define STACK_PUSHX(s, value) \ + { \ + status = tre_stack_push(s, (void *)(value)); \ + if (status != REG_OK) \ + break; \ + } + +#define STACK_PUSHR(s, value) \ + { \ + reg_errcode_t status; \ + status = tre_stack_push(s, (void *)(value)); \ + if (status != REG_OK) \ + return status; \ + } + +typedef struct tre_stack_rec { + int size; + int max_size; + int increment; + int ptr; + void **stack; +} tre_stack_t; + + +static tre_stack_t * +tre_stack_new(int size, int max_size, int increment) +{ + tre_stack_t *s; + + s = xmalloc(sizeof(*s)); + if (s != NULL) + { + s->stack = xmalloc(sizeof(*s->stack) * size); + if (s->stack == NULL) + { + xfree(s); + return NULL; + } + s->size = size; + s->max_size = max_size; + s->increment = increment; + s->ptr = 0; + } + return s; +} + +static void +tre_stack_destroy(tre_stack_t *s) +{ + xfree(s->stack); + xfree(s); +} + +static int +tre_stack_num_objects(tre_stack_t *s) +{ + return s->ptr; +} + +static reg_errcode_t +tre_stack_push(tre_stack_t *s, void *value) +{ + if (s->ptr < s->size) + { + s->stack[s->ptr] = value; + s->ptr++; + } + else + { + if (s->size >= s->max_size) + { + DPRINT(("tre_stack_push: stack full\n")); + return REG_ESPACE; + } + else + { + void **new_buffer; + int new_size; + DPRINT(("tre_stack_push: trying to realloc more space\n")); + new_size = s->size + s->increment; + if (new_size > s->max_size) + new_size = s->max_size; + new_buffer = xrealloc(s->stack, sizeof(*new_buffer) * new_size); + if (new_buffer == NULL) + { + DPRINT(("tre_stack_push: realloc failed.\n")); + return REG_ESPACE; + } + DPRINT(("tre_stack_push: realloc succeeded.\n")); + assert(new_size > s->size); + s->size = new_size; + s->stack = new_buffer; + tre_stack_push(s, value); + } + } + return REG_OK; +} + +static void * +tre_stack_pop(tre_stack_t *s) +{ + return s->stack[--s->ptr]; +} + + +/*********************************************************************** + from tre-parse.c and tre-parse.h +***********************************************************************/ + +/* Parse context. */ +typedef struct { + /* Memory allocator. The AST is allocated using this. */ + tre_mem_t mem; + /* Stack used for keeping track of regexp syntax. */ + tre_stack_t *stack; + /* The parse result. */ + tre_ast_node_t *result; + /* The regexp to parse and its length. */ + const tre_char_t *re; + /* The first character of the entire regexp. */ + const tre_char_t *re_start; + /* The first character after the end of the regexp. */ + const tre_char_t *re_end; + int len; + /* Current submatch ID. */ + int submatch_id; + /* Current position (number of literal). */ + int position; + /* The highest back reference or -1 if none seen so far. */ + int max_backref; + /* Compilation flags. */ + int cflags; + /* If this flag is set the top-level submatch is not captured. */ + int nofirstsub; +} tre_parse_ctx_t; + +static reg_errcode_t +tre_new_item(tre_mem_t mem, int min, int max, int *i, int *max_i, + tre_ast_node_t ***items) +{ + reg_errcode_t status; + tre_ast_node_t **array = *items; + /* Allocate more space if necessary. */ + if (*i >= *max_i) + { + tre_ast_node_t **new_items; + DPRINT(("out of array space, i = %d\n", *i)); + /* If the array is already 1024 items large, give up -- there's + probably an error in the regexp (e.g. not a '\0' terminated + string and missing ']') */ + if (*max_i > 1024) + return REG_ESPACE; + *max_i *= 2; + new_items = xrealloc(array, sizeof(*items) * *max_i); + if (new_items == NULL) + return REG_ESPACE; + *items = array = new_items; + } + array[*i] = tre_ast_new_literal(mem, min, max, -1); + status = array[*i] == NULL ? REG_ESPACE : REG_OK; + (*i)++; + return status; +} + + +/* Expands a character class to character ranges. */ +static reg_errcode_t +tre_expand_ctype(tre_mem_t mem, tre_ctype_t class, tre_ast_node_t ***items, + int *i, int *max_i, int cflags) +{ + reg_errcode_t status = REG_OK; + tre_cint_t c; + int j, min = -1, max = 0; + assert(TRE_MB_CUR_MAX == 1); + + DPRINT((" expanding class to character ranges\n")); + for (j = 0; (j < 256) && (status == REG_OK); j++) + { + c = j; + if (tre_isctype(c, class) + || ((cflags & REG_ICASE) + && (tre_isctype(tre_tolower(c), class) + || tre_isctype(tre_toupper(c), class)))) +{ + if (min < 0) + min = c; + max = c; + } + else if (min >= 0) + { + DPRINT((" range %c (%d) to %c (%d)\n", min, min, max, max)); + status = tre_new_item(mem, min, max, i, max_i, items); + min = -1; + } + } + if (min >= 0 && status == REG_OK) + status = tre_new_item(mem, min, max, i, max_i, items); + return status; +} + + +static int +tre_compare_items(const void *a, const void *b) +{ + tre_ast_node_t *node_a = *(tre_ast_node_t **)a; + tre_ast_node_t *node_b = *(tre_ast_node_t **)b; + tre_literal_t *l_a = node_a->obj, *l_b = node_b->obj; + int a_min = l_a->code_min, b_min = l_b->code_min; + + if (a_min < b_min) + return -1; + else if (a_min > b_min) + return 1; + else + return 0; +} + +/* Maximum number of character classes that can occur in a negated bracket + expression. */ +#define MAX_NEG_CLASSES 64 + +/* Maximum length of character class names. */ +#define MAX_CLASS_NAME + +static reg_errcode_t +tre_parse_bracket_items(tre_parse_ctx_t *ctx, int negate, + tre_ctype_t neg_classes[], int *num_neg_classes, + tre_ast_node_t ***items, int *num_items, + int *items_size) +{ + const tre_char_t *re = ctx->re; + reg_errcode_t status = REG_OK; + tre_ctype_t class = (tre_ctype_t)0; + int i = *num_items; + int max_i = *items_size; + int skip; + + /* Build an array of the items in the bracket expression. */ + while (status == REG_OK) + { + skip = 0; + if (re == ctx->re_end) + { + status = REG_EBRACK; + } + else if (*re == ']' && re > ctx->re) + { + DPRINT(("tre_parse_bracket: done: '%.*" STRF "'\n", + ctx->re_end - re, re)); + re++; + break; + } + else + { + tre_cint_t min = 0, max = 0; + + class = (tre_ctype_t)0; + if (re + 2 < ctx->re_end + && *(re + 1) == '-' && *(re + 2) != ']') + { + DPRINT(("tre_parse_bracket: range: '%.*" STRF "'\n", + ctx->re_end - re, re)); + min = *re; + max = *(re + 2); + re += 3; + /* XXX - Should use collation order instead of encoding values + in character ranges. */ + if (min > max) + status = REG_ERANGE; + } + else if (re + 1 < ctx->re_end + && *re == '[' && *(re + 1) == '.') + status = REG_ECOLLATE; + else if (re + 1 < ctx->re_end + && *re == '[' && *(re + 1) == '=') + status = REG_ECOLLATE; + else if (re + 1 < ctx->re_end + && *re == '[' && *(re + 1) == ':') + { + char tmp_str[64]; + const tre_char_t *endptr = re + 2; + int len; + DPRINT(("tre_parse_bracket: class: '%.*" STRF "'\n", + ctx->re_end - re, re)); + while (endptr < ctx->re_end && *endptr != ':') + endptr++; + if (endptr != ctx->re_end) + { + len = MIN(endptr - re - 2, 63); +#ifdef TRE_WCHAR + { + tre_char_t tmp_wcs[64]; + wcsncpy(tmp_wcs, re + 2, len); + tmp_wcs[len] = '\0'; +#if defined HAVE_WCSRTOMBS + { + mbstate_t state; + const tre_char_t *src = tmp_wcs; + memset(&state, '\0', sizeof(state)); + len = wcsrtombs(tmp_str, &src, sizeof(tmp_str), &state); + } +#elif defined HAVE_WCSTOMBS + len = wcstombs(tmp_str, tmp_wcs, 63); +#endif /* defined HAVE_WCSTOMBS */ + } +#else /* !TRE_WCHAR */ + strncpy(tmp_str, re + 2, len); +#endif /* !TRE_WCHAR */ + tmp_str[len] = '\0'; + DPRINT((" class name: %s\n", tmp_str)); + class = tre_ctype(tmp_str); + if (!class) + status = REG_ECTYPE; + /* Optimize character classes for 8 bit character sets. */ + if (status == REG_OK && TRE_MB_CUR_MAX == 1) + { + status = tre_expand_ctype(ctx->mem, class, items, + &i, &max_i, ctx->cflags); + class = (tre_ctype_t)0; + skip = 1; + } + re = endptr + 2; + } + else + status = REG_ECTYPE; + min = 0; + max = TRE_CHAR_MAX; + } + else + { + DPRINT(("tre_parse_bracket: char: '%.*" STRF "'\n", + ctx->re_end - re, re)); + if (*re == '-' && *(re + 1) != ']' + && ctx->re != re) + /* Two ranges are not allowed to share and endpoint. */ + status = REG_ERANGE; + min = max = *re++; + } + + if (status != REG_OK) + break; + + if (class && negate) + if (*num_neg_classes >= MAX_NEG_CLASSES) + status = REG_ESPACE; + else + neg_classes[(*num_neg_classes)++] = class; + else if (!skip) + { + status = tre_new_item(ctx->mem, min, max, &i, &max_i, items); + if (status != REG_OK) + break; + ((tre_literal_t*)((*items)[i-1])->obj)->class = class; + } + + /* Add opposite-case counterpoints if REG_ICASE is present. + This is broken if there are more than two "same" characters. */ + if (ctx->cflags & REG_ICASE && !class && status == REG_OK && !skip) + { + int cmin, ccurr; + + DPRINT(("adding opposite-case counterpoints\n")); + while (min <= max) + { + if (tre_islower(min)) + { + cmin = ccurr = tre_toupper(min++); + while (tre_islower(min) && tre_toupper(min) == ccurr + 1 + && min <= max) + ccurr = tre_toupper(min++); + status = tre_new_item(ctx->mem, cmin, ccurr, + &i, &max_i, items); + } + else if (tre_isupper(min)) + { + cmin = ccurr = tre_tolower(min++); + while (tre_isupper(min) && tre_tolower(min) == ccurr + 1 + && min <= max) + ccurr = tre_tolower(min++); + status = tre_new_item(ctx->mem, cmin, ccurr, + &i, &max_i, items); + } + else min++; + if (status != REG_OK) + break; + } + if (status != REG_OK) + break; + } + } + } + *num_items = i; + *items_size = max_i; + ctx->re = re; + return status; +} + +static reg_errcode_t +tre_parse_bracket(tre_parse_ctx_t *ctx, tre_ast_node_t **result) +{ + tre_ast_node_t *node = NULL; + int negate = 0; + reg_errcode_t status = REG_OK; + tre_ast_node_t **items, *u, *n; + int i = 0, j, max_i = 32, curr_max, curr_min; + tre_ctype_t neg_classes[MAX_NEG_CLASSES]; + int num_neg_classes = 0; + + /* Start off with an array of `max_i' elements. */ + items = xmalloc(sizeof(*items) * max_i); + if (items == NULL) + return REG_ESPACE; + + if (*ctx->re == '^') + { + DPRINT(("tre_parse_bracket: negate: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + negate = 1; + ctx->re++; + } + + status = tre_parse_bracket_items(ctx, negate, neg_classes, &num_neg_classes, + &items, &i, &max_i); + + if (status != REG_OK) + goto parse_bracket_done; + + /* Sort the array if we need to negate it. */ + if (negate) + qsort(items, i, sizeof(*items), tre_compare_items); + + curr_max = curr_min = 0; + /* Build a union of the items in the array, negated if necessary. */ + for (j = 0; j < i && status == REG_OK; j++) + { + int min, max; + tre_literal_t *l = items[j]->obj; + min = l->code_min; + max = l->code_max; + + DPRINT(("item: %d - %d, class %ld, curr_max = %d\n", + (int)l->code_min, (int)l->code_max, (long)l->class, curr_max)); + + if (negate) + { + if (min < curr_max) + { + /* Overlap. */ + curr_max = MAX(max + 1, curr_max); + DPRINT(("overlap, curr_max = %d\n", curr_max)); + l = NULL; + } + else + { + /* No overlap. */ + curr_max = min - 1; + if (curr_max >= curr_min) + { + DPRINT(("no overlap\n")); + l->code_min = curr_min; + l->code_max = curr_max; + } + else + { + DPRINT(("no overlap, zero room\n")); + l = NULL; + } + curr_min = curr_max = max + 1; + } + } + + if (l != NULL) + { + int k; + DPRINT(("creating %d - %d\n", (int)l->code_min, (int)l->code_max)); + l->position = ctx->position; + if (num_neg_classes > 0) + { + l->neg_classes = tre_mem_alloc(ctx->mem, + (sizeof(l->neg_classes) + * (num_neg_classes + 1))); + if (l->neg_classes == NULL) + { + status = REG_ESPACE; + break; + } + for (k = 0; k < num_neg_classes; k++) + l->neg_classes[k] = neg_classes[k]; + l->neg_classes[k] = (tre_ctype_t)0; + } + else + l->neg_classes = NULL; + if (node == NULL) + node = items[j]; + else + { + u = tre_ast_new_union(ctx->mem, node, items[j]); + if (u == NULL) + status = REG_ESPACE; + node = u; + } + } + } + + if (status != REG_OK) + goto parse_bracket_done; + + if (negate) + { + int k; + DPRINT(("final: creating %d - %d\n", curr_min, (int)TRE_CHAR_MAX)); + n = tre_ast_new_literal(ctx->mem, curr_min, TRE_CHAR_MAX, ctx->position); + if (n == NULL) + status = REG_ESPACE; + else + { + tre_literal_t *l = n->obj; + if (num_neg_classes > 0) + { + l->neg_classes = tre_mem_alloc(ctx->mem, + (sizeof(l->neg_classes) + * (num_neg_classes + 1))); + if (l->neg_classes == NULL) + { + status = REG_ESPACE; + goto parse_bracket_done; + } + for (k = 0; k < num_neg_classes; k++) + l->neg_classes[k] = neg_classes[k]; + l->neg_classes[k] = (tre_ctype_t)0; + } + else + l->neg_classes = NULL; + if (node == NULL) + node = n; + else + { + u = tre_ast_new_union(ctx->mem, node, n); + if (u == NULL) + status = REG_ESPACE; + node = u; + } + } + } + + if (status != REG_OK) + goto parse_bracket_done; + +#ifdef TRE_DEBUG + tre_ast_print(node); +#endif /* TRE_DEBUG */ + + parse_bracket_done: + xfree(items); + ctx->position++; + *result = node; + return status; +} + + +/* Parses a positive decimal integer. Returns -1 if the string does not + contain a valid number. */ +static int +tre_parse_int(const tre_char_t **regex, const tre_char_t *regex_end) +{ + int num = -1; + const tre_char_t *r = *regex; + while (r < regex_end && *r >= '0' && *r <= '9') + { + if (num < 0) + num = 0; + num = num * 10 + *r - '0'; + r++; + } + *regex = r; + return num; +} + + +static reg_errcode_t +tre_parse_bound(tre_parse_ctx_t *ctx, tre_ast_node_t **result) +{ + int min, max; + const tre_char_t *r = ctx->re; + const tre_char_t *start; + int counts_set = 0; + + /* Parse number (minimum repetition count). */ + min = -1; + if (r < ctx->re_end && *r >= '0' && *r <= '9') { + DPRINT(("tre_parse: min count: '%.*" STRF "'\n", ctx->re_end - r, r)); + min = tre_parse_int(&r, ctx->re_end); + } + + /* Parse comma and second number (maximum repetition count). */ + max = min; + if (r < ctx->re_end && *r == ',') + { + r++; + DPRINT(("tre_parse: max count: '%.*" STRF "'\n", ctx->re_end - r, r)); + max = tre_parse_int(&r, ctx->re_end); + } + + /* Check that the repeat counts are sane. */ + if ((max >= 0 && min > max) || max > RE_DUP_MAX) + return REG_BADBR; + + + /* + '{' + optionally followed immediately by a number == minimum repcount + optionally followed by , then a number == maximum repcount + */ + + + do { + int done; + start = r; + + /* Parse count limit settings */ + done = 0; + if (!counts_set) + while (r + 1 < ctx->re_end && !done) + { + switch (*r) + { + case ',': + r++; + break; + case ' ': + r++; + break; + case '}': + done = 1; + break; + default: + done = 1; + break; + } + } + } while (start != r); + + /* Missing }. */ + if (r >= ctx->re_end) + return REG_EBRACE; + + /* Empty contents of {}. */ + if (r == ctx->re) + return REG_BADBR; + + /* Parse the ending '}' or '\}'.*/ + if (ctx->cflags & REG_EXTENDED) + { + if (r >= ctx->re_end || *r != '}') + return REG_BADBR; + r++; + } + else + { + if (r + 1 >= ctx->re_end + || *r != '\\' + || *(r + 1) != '}') + return REG_BADBR; + r += 2; + } + + + /* Create the AST node(s). */ + if (min == 0 && max == 0) + { + *result = tre_ast_new_literal(ctx->mem, EMPTY, -1, -1); + if (*result == NULL) + return REG_ESPACE; + } + else + { + if (min < 0 && max < 0) + /* Only approximate parameters set, no repetitions. */ + min = max = 1; + + *result = tre_ast_new_iter(ctx->mem, *result, min, max); + if (!*result) + return REG_ESPACE; + } + + ctx->re = r; + return REG_OK; +} + +typedef enum { + PARSE_RE = 0, + PARSE_ATOM, + PARSE_MARK_FOR_SUBMATCH, + PARSE_BRANCH, + PARSE_PIECE, + PARSE_CATENATION, + PARSE_POST_CATENATION, + PARSE_UNION, + PARSE_POST_UNION, + PARSE_POSTFIX, + PARSE_RESTORE_CFLAGS +} tre_parse_re_stack_symbol_t; + + +static reg_errcode_t +tre_parse(tre_parse_ctx_t *ctx) +{ + tre_ast_node_t *result = NULL; + tre_parse_re_stack_symbol_t symbol; + reg_errcode_t status = REG_OK; + tre_stack_t *stack = ctx->stack; + int bottom = tre_stack_num_objects(stack); + int depth = 0; + + DPRINT(("tre_parse: parsing '%.*" STRF "', len = %d\n", + ctx->len, ctx->re, ctx->len)); + + if (!ctx->nofirstsub) + { + STACK_PUSH(stack, ctx->re); + STACK_PUSH(stack, ctx->submatch_id); + STACK_PUSH(stack, PARSE_MARK_FOR_SUBMATCH); + ctx->submatch_id++; + } + STACK_PUSH(stack, PARSE_RE); + ctx->re_start = ctx->re; + ctx->re_end = ctx->re + ctx->len; + + + /* The following is basically just a recursive descent parser. I use + an explicit stack instead of recursive functions mostly because of + two reasons: compatibility with systems which have an overflowable + call stack, and efficiency (both in lines of code and speed). */ + while (tre_stack_num_objects(stack) > bottom && status == REG_OK) + { + if (status != REG_OK) + break; + symbol = (tre_parse_re_stack_symbol_t)tre_stack_pop(stack); + switch (symbol) + { + case PARSE_RE: + /* Parse a full regexp. A regexp is one or more branches, + separated by the union operator `|'. */ + if (ctx->cflags & REG_EXTENDED) + STACK_PUSHX(stack, PARSE_UNION); + STACK_PUSHX(stack, PARSE_BRANCH); + break; + + case PARSE_BRANCH: + /* Parse a branch. A branch is one or more pieces, concatenated. + A piece is an atom possibly followed by a postfix operator. */ + STACK_PUSHX(stack, PARSE_CATENATION); + STACK_PUSHX(stack, PARSE_PIECE); + break; + + case PARSE_PIECE: + /* Parse a piece. A piece is an atom possibly followed by one + or more postfix operators. */ + STACK_PUSHX(stack, PARSE_POSTFIX); + STACK_PUSHX(stack, PARSE_ATOM); + break; + + case PARSE_CATENATION: + /* If the expression has not ended, parse another piece. */ + { + tre_char_t c; + if (ctx->re >= ctx->re_end) + break; + c = *ctx->re; + if (ctx->cflags & REG_EXTENDED && c == '|') + break; + if ((ctx->cflags & REG_EXTENDED + && c == ')' && depth > 0) + || (!(ctx->cflags & REG_EXTENDED) + && (c == '\\' + && *(ctx->re + 1) == ')'))) + { + if (!(ctx->cflags & REG_EXTENDED) && depth == 0) + status = REG_EPAREN; + DPRINT(("tre_parse: group end: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + depth--; + if (!(ctx->cflags & REG_EXTENDED)) + ctx->re += 2; + break; + } + + /* Left associative concatenation. */ + STACK_PUSHX(stack, PARSE_CATENATION); + STACK_PUSHX(stack, result); + STACK_PUSHX(stack, PARSE_POST_CATENATION); + STACK_PUSHX(stack, PARSE_PIECE); + break; + } + + case PARSE_POST_CATENATION: + { + tre_ast_node_t *tree = tre_stack_pop(stack); + tre_ast_node_t *tmp_node; + tmp_node = tre_ast_new_catenation(ctx->mem, tree, result); + if (!tmp_node) + return REG_ESPACE; + result = tmp_node; + break; + } + + case PARSE_UNION: + if (ctx->re >= ctx->re_end) + break; + switch (*ctx->re) + { + case '|': + DPRINT(("tre_parse: union: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + STACK_PUSHX(stack, PARSE_UNION); + STACK_PUSHX(stack, result); + STACK_PUSHX(stack, PARSE_POST_UNION); + STACK_PUSHX(stack, PARSE_BRANCH); + ctx->re++; + break; + + case ')': + ctx->re++; + break; + + default: + break; + } + break; + + case PARSE_POST_UNION: + { + tre_ast_node_t *tmp_node; + tre_ast_node_t *tree = tre_stack_pop(stack); + tmp_node = tre_ast_new_union(ctx->mem, tree, result); + if (!tmp_node) + return REG_ESPACE; + result = tmp_node; + break; + } + + case PARSE_POSTFIX: + /* Parse postfix operators. */ + if (ctx->re >= ctx->re_end) + break; + switch (*ctx->re) + { + case '+': + case '?': + if (!(ctx->cflags & REG_EXTENDED)) + break; + case '*': + { + tre_ast_node_t *tmp_node; + int rep_min = 0; + int rep_max = -1; + if (*ctx->re == '+') + rep_min = 1; + if (*ctx->re == '?') + rep_max = 1; + + ctx->re++; + tmp_node = tre_ast_new_iter(ctx->mem, result, rep_min, rep_max); + if (tmp_node == NULL) + return REG_ESPACE; + result = tmp_node; + STACK_PUSHX(stack, PARSE_POSTFIX); + break; + } + + case '\\': + /* "\{" is special without REG_EXTENDED */ + if (!(ctx->cflags & REG_EXTENDED) + && ctx->re + 1 < ctx->re_end + && *(ctx->re + 1) == '{') + { + ctx->re++; + goto parse_brace; + } + else + break; + + case '{': + /* "{" is literal without REG_EXTENDED */ + if (!(ctx->cflags & REG_EXTENDED)) + break; + + parse_brace: + DPRINT(("tre_parse: bound: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + ctx->re++; + + status = tre_parse_bound(ctx, &result); + if (status != REG_OK) + return status; + STACK_PUSHX(stack, PARSE_POSTFIX); + break; + } + break; + + case PARSE_ATOM: + /* Parse an atom. An atom is a regular expression enclosed in `()', + an empty set of `()', a bracket expression, `.', `^', `$', + a `\' followed by a character, or a single character. */ + + /* End of regexp? (empty string). */ + if (ctx->re >= ctx->re_end) + goto parse_literal; + + switch (*ctx->re) + { + case '(': /* parenthesized subexpression */ + + if (ctx->cflags & REG_EXTENDED + || (ctx->re > ctx->re_start + && *(ctx->re - 1) == '\\')) + { + depth++; + { + DPRINT(("tre_parse: group begin: '%.*" STRF + "', submatch %d\n", + ctx->re_end - ctx->re, ctx->re, + ctx->submatch_id)); + ctx->re++; + /* First parse a whole RE, then mark the resulting tree + for submatching. */ + STACK_PUSHX(stack, ctx->submatch_id); + STACK_PUSHX(stack, PARSE_MARK_FOR_SUBMATCH); + STACK_PUSHX(stack, PARSE_RE); + ctx->submatch_id++; + } + } + else + goto parse_literal; + break; + + case ')': /* end of current subexpression */ + if ((ctx->cflags & REG_EXTENDED && depth > 0) + || (ctx->re > ctx->re_start + && *(ctx->re - 1) == '\\')) + { + DPRINT(("tre_parse: empty: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + /* We were expecting an atom, but instead the current + subexpression was closed. POSIX leaves the meaning of + this to be implementation-defined. We interpret this as + an empty expression (which matches an empty string). */ + result = tre_ast_new_literal(ctx->mem, EMPTY, -1, -1); + if (result == NULL) + return REG_ESPACE; + if (!(ctx->cflags & REG_EXTENDED)) + ctx->re--; + } + else + goto parse_literal; + break; + + case '[': /* bracket expression */ + DPRINT(("tre_parse: bracket: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + ctx->re++; + status = tre_parse_bracket(ctx, &result); + if (status != REG_OK) + return status; + break; + + case '\\': + /* If this is "\(" or "\)" chew off the backslash and + try again. */ + if (!(ctx->cflags & REG_EXTENDED) + && ctx->re + 1 < ctx->re_end + && (*(ctx->re + 1) == '(' + || *(ctx->re + 1) == ')')) + { + ctx->re++; + STACK_PUSHX(stack, PARSE_ATOM); + break; + } + + if (ctx->re + 1 >= ctx->re_end) + /* Trailing backslash. */ + return REG_EESCAPE; + + DPRINT(("tre_parse: bleep: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + ctx->re++; + switch (*ctx->re) + { + default: + if (!(ctx->cflags & REG_EXTENDED) && tre_isdigit(*ctx->re)) + { + /* Back reference. */ + int val = *ctx->re - '0'; + DPRINT(("tre_parse: backref: '%.*" STRF "'\n", + ctx->re_end - ctx->re + 1, ctx->re - 1)); + result = tre_ast_new_literal(ctx->mem, BACKREF, val, + ctx->position); + if (result == NULL) + return REG_ESPACE; + ctx->position++; + ctx->max_backref = MAX(val, ctx->max_backref); + ctx->re++; + } + else + { + /* Escaped character. */ + DPRINT(("tre_parse: escaped: '%.*" STRF "'\n", + ctx->re_end - ctx->re + 1, ctx->re - 1)); + result = tre_ast_new_literal(ctx->mem, *ctx->re, *ctx->re, + ctx->position); + ctx->position++; + ctx->re++; + } + break; + } + if (result == NULL) + return REG_ESPACE; + break; + + case '.': /* the any-symbol */ + DPRINT(("tre_parse: any: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + if (ctx->cflags & REG_NEWLINE) + { + tre_ast_node_t *tmp1; + tre_ast_node_t *tmp2; + tmp1 = tre_ast_new_literal(ctx->mem, 0, '\n' - 1, + ctx->position); + if (!tmp1) + return REG_ESPACE; + tmp2 = tre_ast_new_literal(ctx->mem, '\n' + 1, TRE_CHAR_MAX, + ctx->position + 1); + if (!tmp2) + return REG_ESPACE; + result = tre_ast_new_union(ctx->mem, tmp1, tmp2); + if (!result) + return REG_ESPACE; + ctx->position += 2; + } + else + { + result = tre_ast_new_literal(ctx->mem, 0, TRE_CHAR_MAX, + ctx->position); + if (!result) + return REG_ESPACE; + ctx->position++; + } + ctx->re++; + break; + + case '^': /* beginning of line assertion */ + /* '^' has a special meaning everywhere in EREs, and in the + beginning of the RE and after \( is BREs. */ + if (ctx->cflags & REG_EXTENDED + || (ctx->re - 2 >= ctx->re_start + && *(ctx->re - 2) == '\\' + && *(ctx->re - 1) == '(') + || ctx->re == ctx->re_start) + { + DPRINT(("tre_parse: BOL: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + result = tre_ast_new_literal(ctx->mem, ASSERTION, + ASSERT_AT_BOL, -1); + if (result == NULL) + return REG_ESPACE; + ctx->re++; + } + else + goto parse_literal; + break; + + case '$': /* end of line assertion. */ + /* '$' is special everywhere in EREs, and in the end of the + string and before \) is BREs. */ + if (ctx->cflags & REG_EXTENDED + || (ctx->re + 2 < ctx->re_end + && *(ctx->re + 1) == '\\' + && *(ctx->re + 2) == ')') + || ctx->re + 1 == ctx->re_end) + { + DPRINT(("tre_parse: EOL: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + result = tre_ast_new_literal(ctx->mem, ASSERTION, + ASSERT_AT_EOL, -1); + if (result == NULL) + return REG_ESPACE; + ctx->re++; + } + else + goto parse_literal; + break; + + default: + parse_literal: + + /* We are expecting an atom. If the subexpression (or the whole + regexp ends here, we interpret it as an empty expression + (which matches an empty string). */ + if ( + (ctx->re >= ctx->re_end + || *ctx->re == '*' + || (ctx->cflags & REG_EXTENDED + && (*ctx->re == '|' + || *ctx->re == '{' + || *ctx->re == '+' + || *ctx->re == '?')) + /* Test for "\)" in BRE mode. */ + || (!(ctx->cflags & REG_EXTENDED) + && ctx->re + 1 < ctx->re_end + && *ctx->re == '\\' + && *(ctx->re + 1) == '{'))) + { + DPRINT(("tre_parse: empty: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + result = tre_ast_new_literal(ctx->mem, EMPTY, -1, -1); + if (!result) + return REG_ESPACE; + break; + } + + DPRINT(("tre_parse: literal: '%.*" STRF "'\n", + ctx->re_end - ctx->re, ctx->re)); + /* Note that we can't use an tre_isalpha() test here, since there + may be characters which are alphabetic but neither upper or + lower case. */ + if (ctx->cflags & REG_ICASE + && (tre_isupper(*ctx->re) || tre_islower(*ctx->re))) + { + tre_ast_node_t *tmp1; + tre_ast_node_t *tmp2; + + /* XXX - Can there be more than one opposite-case + counterpoints for some character in some locale? Or + more than two characters which all should be regarded + the same character if case is ignored? If yes, there + does not seem to be a portable way to detect it. I guess + that at least for multi-character collating elements there + could be several opposite-case counterpoints, but they + cannot be supported portably anyway. */ + tmp1 = tre_ast_new_literal(ctx->mem, tre_toupper(*ctx->re), + tre_toupper(*ctx->re), + ctx->position); + if (!tmp1) + return REG_ESPACE; + tmp2 = tre_ast_new_literal(ctx->mem, tre_tolower(*ctx->re), + tre_tolower(*ctx->re), + ctx->position); + if (!tmp2) + return REG_ESPACE; + result = tre_ast_new_union(ctx->mem, tmp1, tmp2); + if (!result) + return REG_ESPACE; + } + else + { + result = tre_ast_new_literal(ctx->mem, *ctx->re, *ctx->re, + ctx->position); + if (!result) + return REG_ESPACE; + } + ctx->position++; + ctx->re++; + break; + } + break; + + case PARSE_MARK_FOR_SUBMATCH: + { + int submatch_id = (int)tre_stack_pop(stack); + + if (result->submatch_id >= 0) + { + tre_ast_node_t *n, *tmp_node; + n = tre_ast_new_literal(ctx->mem, EMPTY, -1, -1); + if (n == NULL) + return REG_ESPACE; + tmp_node = tre_ast_new_catenation(ctx->mem, n, result); + if (tmp_node == NULL) + return REG_ESPACE; + tmp_node->num_submatches = result->num_submatches; + result = tmp_node; + } + result->submatch_id = submatch_id; + result->num_submatches++; + break; + } + + case PARSE_RESTORE_CFLAGS: + ctx->cflags = (int)tre_stack_pop(stack); + break; + } + } + + /* Check for missing closing parentheses. */ + if (depth > 0) + return REG_EPAREN; + + if (status == REG_OK) + ctx->result = result; + + return status; +} + + +/*********************************************************************** + from tre-compile.c +***********************************************************************/ + +/* + Algorithms to setup tags so that submatch addressing can be done. +*/ + + +/* Inserts a catenation node to the root of the tree given in `node'. + As the left child a new tag with number `tag_id' to `node' is added, + and the right child is the old root. */ +/* OR */ +/* Inserts a catenation node to the root of the tree given in `node'. + As the right child a new tag with number `tag_id' to `node' is added, + and the left child is the old root. */ +static reg_errcode_t +tre_add_tag(tre_mem_t mem, tre_ast_node_t *node, int tag_id, int right) +{ + tre_catenation_t *c; + tre_ast_node_t *child_tag, *child_old; + + DPRINT(("add_tag_%s: tag %d\n", right ? "right" : "left", tag_id)); + + c = tre_mem_alloc(mem, sizeof(*c)); + if (c == NULL) + return REG_ESPACE; + child_tag = tre_ast_new_literal(mem, TAG, tag_id, -1); + if (child_tag == NULL) + return REG_ESPACE; + child_old = tre_mem_alloc(mem, sizeof(tre_ast_node_t)); + if (child_old == NULL) + return REG_ESPACE; + + child_old->obj = node->obj; + child_old->type = node->type; + child_old->nullable = -1; + child_old->submatch_id = -1; + child_old->firstpos = NULL; + child_old->lastpos = NULL; + child_old->num_tags = 0; + node->obj = c; + node->type = CATENATION; + + c->right = c->left = child_old; + if (right) c->right = child_tag; + else c->left = child_tag; + + return REG_OK; +} + +typedef enum { + ADDTAGS_RECURSE, + ADDTAGS_AFTER_ITERATION, + ADDTAGS_AFTER_UNION_LEFT, + ADDTAGS_AFTER_UNION_RIGHT, + ADDTAGS_AFTER_CAT_LEFT, + ADDTAGS_AFTER_CAT_RIGHT, + ADDTAGS_SET_SUBMATCH_END +} tre_addtags_symbol_t; + + +typedef struct { + int tag; + int next_tag; +} tre_tag_states_t; + +/* Adds tags to appropriate locations in the parse tree in `tree', so that + subexpressions marked for submatch addressing can be traced. */ +static reg_errcode_t +tre_add_tags(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *tree, + tre_tnfa_t *tnfa) +{ + reg_errcode_t status = REG_OK; + tre_addtags_symbol_t symbol; + tre_ast_node_t *node = tree; /* Tree node we are currently looking at. */ + int bottom = tre_stack_num_objects(stack); + /* True for first pass (counting number of needed tags) */ + int first_pass = (mem == NULL || tnfa == NULL); + int *regset, *orig_regset; + int num_tags = 0; /* Total number of tags. */ + int tag = 0; /* The tag that is to be added next. */ + int next_tag = 1; /* Next tag to use after this one. */ + int *parents; /* Stack of submatches the current submatch is + contained in. */ + tre_tag_states_t *saved_states; + + tre_tag_direction_t direction = TRE_TAG_MINIMIZE; + if (!first_pass) + tnfa->end_tag = 0; + + regset = xmalloc(sizeof(*regset) * ((tnfa->num_submatches + 1) * 2)); + if (regset == NULL) + return REG_ESPACE; + regset[0] = -1; + orig_regset = regset; + + parents = xmalloc(sizeof(*parents) * (tnfa->num_submatches + 1)); + if (parents == NULL) + { + xfree(regset); + return REG_ESPACE; + } + parents[0] = -1; + + saved_states = xmalloc(sizeof(*saved_states) * (tnfa->num_submatches + 1)); + if (saved_states == NULL) + { + xfree(regset); + xfree(parents); + return REG_ESPACE; + } + else + { + unsigned int i; + for (i = 0; i <= tnfa->num_submatches; i++) + saved_states[i].tag = -1; + } + + STACK_PUSH(stack, node); + STACK_PUSH(stack, ADDTAGS_RECURSE); + + while (tre_stack_num_objects(stack) > bottom) + { + if (status != REG_OK) + break; + + symbol = (tre_addtags_symbol_t)tre_stack_pop(stack); + switch (symbol) + { + + case ADDTAGS_SET_SUBMATCH_END: + { + int id = (int)tre_stack_pop(stack); + int i; + + /* Add end of this submatch to regset. */ + for (i = 0; regset[i] >= 0; i++); + regset[i] = id * 2 + 1; + regset[i + 1] = -1; + + /* Pop this submatch from the parents stack. */ + for (i = 0; parents[i] >= 0; i++); + parents[i - 1] = -1; + break; + } + + case ADDTAGS_RECURSE: + node = tre_stack_pop(stack); + + if (node->submatch_id >= 0) + { + int id = node->submatch_id; + int i; + + + /* Add start of this submatch to regset. */ + for (i = 0; regset[i] >= 0; i++); + regset[i] = id * 2; + regset[i + 1] = -1; + + if (!first_pass) + { + for (i = 0; parents[i] >= 0; i++); + tnfa->submatch_data[id].parents = NULL; + if (i > 0) + { + int *p = xmalloc(sizeof(*p) * (i + 1)); + if (p == NULL) + { + status = REG_ESPACE; + break; + } + assert(tnfa->submatch_data[id].parents == NULL); + tnfa->submatch_data[id].parents = p; + for (i = 0; parents[i] >= 0; i++) + p[i] = parents[i]; + p[i] = -1; + } + } + + /* Add end of this submatch to regset after processing this + node. */ + STACK_PUSHX(stack, node->submatch_id); + STACK_PUSHX(stack, ADDTAGS_SET_SUBMATCH_END); + } + + switch (node->type) + { + case LITERAL: + { + tre_literal_t *lit = node->obj; + + if (!IS_SPECIAL(lit) || IS_BACKREF(lit)) + { + int i; + DPRINT(("Literal %d-%d\n", + (int)lit->code_min, (int)lit->code_max)); + if (regset[0] >= 0) + { + /* Regset is not empty, so add a tag before the + literal or backref. */ + if (!first_pass) + { + status = tre_add_tag(mem, node, tag, 0 /*left*/); + tnfa->tag_directions[tag] = direction; + /* Go through the regset and set submatch data for + submatches that are using this tag. */ + for (i = 0; regset[i] >= 0; i++) + { + int id = regset[i] >> 1; + int start = !(regset[i] & 1); + DPRINT((" Using tag %d for %s offset of " + "submatch %d\n", tag, + start ? "start" : "end", id)); + if (start) + tnfa->submatch_data[id].so_tag = tag; + else + tnfa->submatch_data[id].eo_tag = tag; + } + } + else + { + DPRINT((" num_tags = 1\n")); + node->num_tags = 1; + } + + DPRINT((" num_tags++\n")); + regset[0] = -1; + tag = next_tag; + num_tags++; + next_tag++; + } + } + else + { + assert(!IS_TAG(lit)); + } + break; + } + case CATENATION: + { + tre_catenation_t *cat = node->obj; + tre_ast_node_t *left = cat->left; + tre_ast_node_t *right = cat->right; + int reserved_tag = -1; + DPRINT(("Catenation, next_tag = %d\n", next_tag)); + + + /* After processing right child. */ + STACK_PUSHX(stack, node); + STACK_PUSHX(stack, ADDTAGS_AFTER_CAT_RIGHT); + + /* Process right child. */ + STACK_PUSHX(stack, right); + STACK_PUSHX(stack, ADDTAGS_RECURSE); + + /* After processing left child. */ + STACK_PUSHX(stack, next_tag + left->num_tags); + DPRINT((" Pushing %d for after left\n", + next_tag + left->num_tags)); + if (left->num_tags > 0 && right->num_tags > 0) + { + /* Reserve the next tag to the right child. */ + DPRINT((" Reserving next_tag %d to right child\n", + next_tag)); + reserved_tag = next_tag; + next_tag++; + } + STACK_PUSHX(stack, reserved_tag); + STACK_PUSHX(stack, ADDTAGS_AFTER_CAT_LEFT); + + /* Process left child. */ + STACK_PUSHX(stack, left); + STACK_PUSHX(stack, ADDTAGS_RECURSE); + + } + break; + case ITERATION: + { + tre_iteration_t *iter = node->obj; + DPRINT(("Iteration\n")); + + if (first_pass) + { + STACK_PUSHX(stack, regset[0] >= 0); + } + else + { + STACK_PUSHX(stack, tag); + } + STACK_PUSHX(stack, node); + STACK_PUSHX(stack, ADDTAGS_AFTER_ITERATION); + + STACK_PUSHX(stack, iter->arg); + STACK_PUSHX(stack, ADDTAGS_RECURSE); + + /* Regset is not empty, so add a tag here. */ + if (regset[0] >= 0) + { + if (!first_pass) + { + int i; + status = tre_add_tag(mem, node, tag, 0 /*left*/); + tnfa->tag_directions[tag] = direction; + /* Go through the regset and set submatch data for + submatches that are using this tag. */ + for (i = 0; regset[i] >= 0; i++) + { + int id = regset[i] >> 1; + int start = !(regset[i] & 1); + DPRINT((" Using tag %d for %s offset of " + "submatch %d\n", tag, + start ? "start" : "end", id)); + if (start) + tnfa->submatch_data[id].so_tag = tag; + else + tnfa->submatch_data[id].eo_tag = tag; + } + } + + DPRINT((" num_tags++\n")); + regset[0] = -1; + tag = next_tag; + num_tags++; + next_tag++; + } + direction = TRE_TAG_MINIMIZE; + } + break; + case UNION: + { + tre_union_t *uni = node->obj; + tre_ast_node_t *left = uni->left; + tre_ast_node_t *right = uni->right; + int left_tag; + int right_tag; + + if (regset[0] >= 0) + { + left_tag = next_tag; + right_tag = next_tag + 1; + } + else + { + left_tag = tag; + right_tag = next_tag; + } + + DPRINT(("Union\n")); + + /* After processing right child. */ + STACK_PUSHX(stack, right_tag); + STACK_PUSHX(stack, left_tag); + STACK_PUSHX(stack, regset); + STACK_PUSHX(stack, regset[0] >= 0); + STACK_PUSHX(stack, node); + STACK_PUSHX(stack, right); + STACK_PUSHX(stack, left); + STACK_PUSHX(stack, ADDTAGS_AFTER_UNION_RIGHT); + + /* Process right child. */ + STACK_PUSHX(stack, right); + STACK_PUSHX(stack, ADDTAGS_RECURSE); + + /* After processing left child. */ + STACK_PUSHX(stack, ADDTAGS_AFTER_UNION_LEFT); + + /* Process left child. */ + STACK_PUSHX(stack, left); + STACK_PUSHX(stack, ADDTAGS_RECURSE); + + /* Regset is not empty, so add a tag here. */ + if (regset[0] >= 0) + { + if (!first_pass) + { + int i; + status = tre_add_tag(mem, node, tag, 0 /*left*/); + tnfa->tag_directions[tag] = direction; + /* Go through the regset and set submatch data for + submatches that are using this tag. */ + for (i = 0; regset[i] >= 0; i++) + { + int id = regset[i] >> 1; + int start = !(regset[i] & 1); + DPRINT((" Using tag %d for %s offset of " + "submatch %d\n", tag, + start ? "start" : "end", id)); + if (start) + tnfa->submatch_data[id].so_tag = tag; + else + tnfa->submatch_data[id].eo_tag = tag; + } + } + + DPRINT((" num_tags++\n")); + regset[0] = -1; + tag = next_tag; + num_tags++; + next_tag++; + } + + if (node->num_submatches > 0) + { + /* The next two tags are reserved for markers. */ + next_tag++; + tag = next_tag; + next_tag++; + } + + break; + } + } + + if (node->submatch_id >= 0) + { + int i; + /* Push this submatch on the parents stack. */ + for (i = 0; parents[i] >= 0; i++); + parents[i] = node->submatch_id; + parents[i + 1] = -1; + } + + break; /* end case: ADDTAGS_RECURSE */ + + case ADDTAGS_AFTER_ITERATION: + { + int enter_tag; + node = tre_stack_pop(stack); + if (first_pass) + node->num_tags = ((tre_iteration_t *)node->obj)->arg->num_tags + + (int)tre_stack_pop(stack); + else + enter_tag = (int)tre_stack_pop(stack); + + DPRINT(("After iteration\n")); + direction = TRE_TAG_MAXIMIZE; + break; + } + + case ADDTAGS_AFTER_CAT_LEFT: + { + int new_tag = (int)tre_stack_pop(stack); + next_tag = (int)tre_stack_pop(stack); + DPRINT(("After cat left, tag = %d, next_tag = %d\n", + tag, next_tag)); + if (new_tag >= 0) + { + DPRINT((" Setting tag to %d\n", new_tag)); + tag = new_tag; + } + break; + } + + case ADDTAGS_AFTER_CAT_RIGHT: + DPRINT(("After cat right\n")); + node = tre_stack_pop(stack); + if (first_pass) + node->num_tags = ((tre_catenation_t *)node->obj)->left->num_tags + + ((tre_catenation_t *)node->obj)->right->num_tags; + break; + + case ADDTAGS_AFTER_UNION_LEFT: + DPRINT(("After union left\n")); + /* Lift the bottom of the `regset' array so that when processing + the right operand the items currently in the array are + invisible. The original bottom was saved at ADDTAGS_UNION and + will be restored at ADDTAGS_AFTER_UNION_RIGHT below. */ + while (*regset >= 0) + regset++; + break; + + case ADDTAGS_AFTER_UNION_RIGHT: + { + int added_tags, tag_left, tag_right; + tre_ast_node_t *left = tre_stack_pop(stack); + tre_ast_node_t *right = tre_stack_pop(stack); + DPRINT(("After union right\n")); + node = tre_stack_pop(stack); + added_tags = (int)tre_stack_pop(stack); + if (first_pass) + { + node->num_tags = ((tre_union_t *)node->obj)->left->num_tags + + ((tre_union_t *)node->obj)->right->num_tags + added_tags + + ((node->num_submatches > 0) ? 2 : 0); + } + regset = tre_stack_pop(stack); + tag_left = (int)tre_stack_pop(stack); + tag_right = (int)tre_stack_pop(stack); + + /* Add tags after both children, the left child gets a smaller + tag than the right child. This guarantees that we prefer + the left child over the right child. */ + /* XXX - This is not always necessary (if the children have + tags which must be seen for every match of that child). */ + /* XXX - Check if this is the only place where tre_add_tag_right + is used. If so, use tre_add_tag_left (putting the tag before + the child as opposed after the child) and throw away + tre_add_tag_right. */ + if (node->num_submatches > 0) + { + if (!first_pass) + { + status = tre_add_tag(mem, left, tag_left, 1 /*right*/); + tnfa->tag_directions[tag] = TRE_TAG_MAXIMIZE; + status = tre_add_tag(mem, right, tag_right, 1 /*right*/); + tnfa->tag_directions[tag] = TRE_TAG_MAXIMIZE; + } + DPRINT((" num_tags += 2\n")); + num_tags += 2; + } + direction = TRE_TAG_MAXIMIZE; + break; + } + + default: + assert(0); + break; + + } /* end switch(symbol) */ + } /* end while(tre_stack_num_objects(stack) > bottom) */ + + if (!first_pass) + { + int i; + /* Go through the regset and set submatch data for + submatches that are using this tag. */ + for (i = 0; regset[i] >= 0; i++) + { + int id = regset[i] >> 1; + int start = !(regset[i] & 1); + DPRINT((" Using tag %d for %s offset of " + "submatch %d\n", num_tags, + start ? "start" : "end", id)); + if (start) + tnfa->submatch_data[id].so_tag = num_tags; + else + tnfa->submatch_data[id].eo_tag = num_tags; + } + } + + DPRINT(("tre_add_tags: %s complete. Number of tags %d.\n", + first_pass? "First pass" : "Second pass", num_tags)); + + assert(tree->num_tags == num_tags); + tnfa->end_tag = num_tags; + tnfa->num_tags = num_tags; + xfree(orig_regset); + xfree(parents); + xfree(saved_states); + return status; +} + + + +/* + AST to TNFA compilation routines. +*/ + +typedef enum { + COPY_RECURSE, + COPY_SET_RESULT_PTR +} tre_copyast_symbol_t; + +/* Flags for tre_copy_ast(). */ +#define COPY_REMOVE_TAGS 1 +#define COPY_MAXIMIZE_FIRST_TAG 2 + +static reg_errcode_t +tre_copy_ast(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *ast, + int flags, int *pos_add, tre_tag_direction_t *tag_directions, + tre_ast_node_t **copy, int *max_pos) +{ + reg_errcode_t status = REG_OK; + int bottom = tre_stack_num_objects(stack); + int num_copied = 0; + int first_tag = 1; + tre_ast_node_t **result = copy; + tre_copyast_symbol_t symbol; + + STACK_PUSH(stack, ast); + STACK_PUSH(stack, COPY_RECURSE); + + while (status == REG_OK && tre_stack_num_objects(stack) > bottom) + { + tre_ast_node_t *node; + if (status != REG_OK) + break; + + symbol = (tre_copyast_symbol_t)tre_stack_pop(stack); + switch (symbol) + { + case COPY_SET_RESULT_PTR: + result = tre_stack_pop(stack); + break; + case COPY_RECURSE: + node = tre_stack_pop(stack); + switch (node->type) + { + case LITERAL: + { + tre_literal_t *lit = node->obj; + int pos = lit->position; + int min = lit->code_min; + int max = lit->code_max; + if (!IS_SPECIAL(lit) || IS_BACKREF(lit)) + { + /* XXX - e.g. [ab] has only one position but two + nodes, so we are creating holes in the state space + here. Not fatal, just wastes memory. */ + pos += *pos_add; + num_copied++; + } + else if (IS_TAG(lit) && (flags & COPY_REMOVE_TAGS)) + { + /* Change this tag to empty. */ + min = EMPTY; + max = pos = -1; + } + else if (IS_TAG(lit) && (flags & COPY_MAXIMIZE_FIRST_TAG) + && first_tag) + { + /* Maximize the first tag. */ + tag_directions[max] = TRE_TAG_MAXIMIZE; + first_tag = 0; + } + *result = tre_ast_new_literal(mem, min, max, pos); + if (*result == NULL) + status = REG_ESPACE; + + if (pos > *max_pos) + *max_pos = pos; + break; + } + case UNION: + { + tre_union_t *uni = node->obj; + tre_union_t *copy; + *result = tre_ast_new_union(mem, uni->left, uni->right); + if (*result == NULL) + { + status = REG_ESPACE; + break; + } + copy = (*result)->obj; + result = ©->left; + STACK_PUSHX(stack, uni->right); + STACK_PUSHX(stack, COPY_RECURSE); + STACK_PUSHX(stack, ©->right); + STACK_PUSHX(stack, COPY_SET_RESULT_PTR); + STACK_PUSHX(stack, uni->left); + STACK_PUSHX(stack, COPY_RECURSE); + break; + } + case CATENATION: + { + tre_catenation_t *cat = node->obj; + tre_catenation_t *copy; + *result = tre_ast_new_catenation(mem, cat->left, cat->right); + if (*result == NULL) + { + status = REG_ESPACE; + break; + } + copy = (*result)->obj; + copy->left = NULL; + copy->right = NULL; + result = ©->left; + + STACK_PUSHX(stack, cat->right); + STACK_PUSHX(stack, COPY_RECURSE); + STACK_PUSHX(stack, ©->right); + STACK_PUSHX(stack, COPY_SET_RESULT_PTR); + STACK_PUSHX(stack, cat->left); + STACK_PUSHX(stack, COPY_RECURSE); + break; + } + case ITERATION: + { + tre_iteration_t *iter = node->obj; + STACK_PUSHX(stack, iter->arg); + STACK_PUSHX(stack, COPY_RECURSE); + *result = tre_ast_new_iter(mem, iter->arg, iter->min, iter->max); + if (*result == NULL) + { + status = REG_ESPACE; + break; + } + iter = (*result)->obj; + result = &iter->arg; + break; + } + default: + assert(0); + break; + } + break; + } + } + *pos_add += num_copied; + return status; +} + +typedef enum { + EXPAND_RECURSE, + EXPAND_AFTER_ITER +} tre_expand_ast_symbol_t; + +/* Expands each iteration node that has a finite nonzero minimum or maximum + iteration count to a catenated sequence of copies of the node. */ +static reg_errcode_t +tre_expand_ast(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *ast, + int *position, tre_tag_direction_t *tag_directions, + int *max_depth) +{ + reg_errcode_t status = REG_OK; + int bottom = tre_stack_num_objects(stack); + int pos_add = 0; + int pos_add_total = 0; + int max_pos = 0; + /* Approximate parameter nesting level. */ + int iter_depth = 0; + + STACK_PUSHR(stack, ast); + STACK_PUSHR(stack, EXPAND_RECURSE); + while (status == REG_OK && tre_stack_num_objects(stack) > bottom) + { + tre_ast_node_t *node; + tre_expand_ast_symbol_t symbol; + + if (status != REG_OK) + break; + + DPRINT(("pos_add %d\n", pos_add)); + + symbol = (tre_expand_ast_symbol_t)tre_stack_pop(stack); + node = tre_stack_pop(stack); + switch (symbol) + { + case EXPAND_RECURSE: + switch (node->type) + { + case LITERAL: + { + tre_literal_t *lit= node->obj; + if (!IS_SPECIAL(lit) || IS_BACKREF(lit)) + { + lit->position += pos_add; + if (lit->position > max_pos) + max_pos = lit->position; + } + break; + } + case UNION: + { + tre_union_t *uni = node->obj; + STACK_PUSHX(stack, uni->right); + STACK_PUSHX(stack, EXPAND_RECURSE); + STACK_PUSHX(stack, uni->left); + STACK_PUSHX(stack, EXPAND_RECURSE); + break; + } + case CATENATION: + { + tre_catenation_t *cat = node->obj; + STACK_PUSHX(stack, cat->right); + STACK_PUSHX(stack, EXPAND_RECURSE); + STACK_PUSHX(stack, cat->left); + STACK_PUSHX(stack, EXPAND_RECURSE); + break; + } + case ITERATION: + { + tre_iteration_t *iter = node->obj; + STACK_PUSHX(stack, pos_add); + STACK_PUSHX(stack, node); + STACK_PUSHX(stack, EXPAND_AFTER_ITER); + STACK_PUSHX(stack, iter->arg); + STACK_PUSHX(stack, EXPAND_RECURSE); + /* If we are going to expand this node at EXPAND_AFTER_ITER + then don't increase the `pos' fields of the nodes now, it + will get done when expanding. */ + if (iter->min > 1 || iter->max > 1) + pos_add = 0; + iter_depth++; + DPRINT(("iter\n")); + break; + } + default: + assert(0); + break; + } + break; + case EXPAND_AFTER_ITER: + { + tre_iteration_t *iter = node->obj; + int pos_add_last; + pos_add = (int)tre_stack_pop(stack); + pos_add_last = pos_add; + if (iter->min > 1 || iter->max > 1) + { + tre_ast_node_t *seq1 = NULL, *seq2 = NULL; + int i; + int pos_add_save = pos_add; + + /* Create a catenated sequence of copies of the node. */ + for (i = 0; i < iter->min; i++) + { + tre_ast_node_t *copy; + /* Remove tags from all but the last copy. */ + int flags = ((i + 1 < iter->min) + ? COPY_REMOVE_TAGS + : COPY_MAXIMIZE_FIRST_TAG); + DPRINT((" pos_add %d\n", pos_add)); + pos_add_save = pos_add; + status = tre_copy_ast(mem, stack, iter->arg, flags, + &pos_add, tag_directions, ©, + &max_pos); + if (status != REG_OK) + return status; + if (seq1 != NULL) + seq1 = tre_ast_new_catenation(mem, seq1, copy); + else + seq1 = copy; + if (seq1 == NULL) + return REG_ESPACE; + } + + if (iter->max == -1) + { + /* No upper limit. */ + pos_add_save = pos_add; + status = tre_copy_ast(mem, stack, iter->arg, 0, + &pos_add, NULL, &seq2, &max_pos); + if (status != REG_OK) + return status; + seq2 = tre_ast_new_iter(mem, seq2, 0, -1); + if (seq2 == NULL) + return REG_ESPACE; + } + else + { + for (i = iter->min; i < iter->max; i++) + { + tre_ast_node_t *tmp, *copy; + pos_add_save = pos_add; + status = tre_copy_ast(mem, stack, iter->arg, 0, + &pos_add, NULL, ©, &max_pos); + if (status != REG_OK) + return status; + if (seq2 != NULL) + seq2 = tre_ast_new_catenation(mem, copy, seq2); + else + seq2 = copy; + if (seq2 == NULL) + return REG_ESPACE; + tmp = tre_ast_new_literal(mem, EMPTY, -1, -1); + if (tmp == NULL) + return REG_ESPACE; + seq2 = tre_ast_new_union(mem, tmp, seq2); + if (seq2 == NULL) + return REG_ESPACE; + } + } + + pos_add = pos_add_save; + if (seq1 == NULL) + seq1 = seq2; + else if (seq2 != NULL) + seq1 = tre_ast_new_catenation(mem, seq1, seq2); + if (seq1 == NULL) + return REG_ESPACE; + node->obj = seq1->obj; + node->type = seq1->type; + } + + iter_depth--; + pos_add_total += pos_add - pos_add_last; + if (iter_depth == 0) + pos_add = pos_add_total; + + break; + } + default: + assert(0); + break; + } + } + + *position += pos_add_total; + + /* `max_pos' should never be larger than `*position' if the above + code works, but just an extra safeguard let's make sure + `*position' is set large enough so enough memory will be + allocated for the transition table. */ + if (max_pos > *position) + *position = max_pos; + +#ifdef TRE_DEBUG + DPRINT(("Expanded AST:\n")); + tre_ast_print(ast); + DPRINT(("*position %d, max_pos %d\n", *position, max_pos)); +#endif + + return status; +} + +static tre_pos_and_tags_t * +tre_set_empty(tre_mem_t mem) +{ + tre_pos_and_tags_t *new_set; + + new_set = tre_mem_calloc(mem, sizeof(*new_set)); + if (new_set == NULL) + return NULL; + + new_set[0].position = -1; + new_set[0].code_min = -1; + new_set[0].code_max = -1; + + return new_set; +} + +static tre_pos_and_tags_t * +tre_set_one(tre_mem_t mem, int position, int code_min, int code_max, + tre_ctype_t class, tre_ctype_t *neg_classes, int backref) +{ + tre_pos_and_tags_t *new_set; + + new_set = tre_mem_calloc(mem, sizeof(*new_set) * 2); + if (new_set == NULL) + return NULL; + + new_set[0].position = position; + new_set[0].code_min = code_min; + new_set[0].code_max = code_max; + new_set[0].class = class; + new_set[0].neg_classes = neg_classes; + new_set[0].backref = backref; + new_set[1].position = -1; + new_set[1].code_min = -1; + new_set[1].code_max = -1; + + return new_set; +} + +static tre_pos_and_tags_t * +tre_set_union(tre_mem_t mem, tre_pos_and_tags_t *set1, tre_pos_and_tags_t *set2, + int *tags, int assertions) +{ + int s1, s2, i, j; + tre_pos_and_tags_t *new_set; + int *new_tags; + int num_tags; + + for (num_tags = 0; tags != NULL && tags[num_tags] >= 0; num_tags++); + for (s1 = 0; set1[s1].position >= 0; s1++); + for (s2 = 0; set2[s2].position >= 0; s2++); + new_set = tre_mem_calloc(mem, sizeof(*new_set) * (s1 + s2 + 1)); + if (!new_set ) + return NULL; + + for (s1 = 0; set1[s1].position >= 0; s1++) + { + new_set[s1].position = set1[s1].position; + new_set[s1].code_min = set1[s1].code_min; + new_set[s1].code_max = set1[s1].code_max; + new_set[s1].assertions = set1[s1].assertions | assertions; + new_set[s1].class = set1[s1].class; + new_set[s1].neg_classes = set1[s1].neg_classes; + new_set[s1].backref = set1[s1].backref; + if (set1[s1].tags == NULL && tags == NULL) + new_set[s1].tags = NULL; + else + { + for (i = 0; set1[s1].tags != NULL && set1[s1].tags[i] >= 0; i++); + new_tags = tre_mem_alloc(mem, (sizeof(*new_tags) + * (i + num_tags + 1))); + if (new_tags == NULL) + return NULL; + for (j = 0; j < i; j++) + new_tags[j] = set1[s1].tags[j]; + for (i = 0; i < num_tags; i++) + new_tags[j + i] = tags[i]; + new_tags[j + i] = -1; + new_set[s1].tags = new_tags; + } + } + + for (s2 = 0; set2[s2].position >= 0; s2++) + { + new_set[s1 + s2].position = set2[s2].position; + new_set[s1 + s2].code_min = set2[s2].code_min; + new_set[s1 + s2].code_max = set2[s2].code_max; + /* XXX - why not | assertions here as well? */ + new_set[s1 + s2].assertions = set2[s2].assertions; + new_set[s1 + s2].class = set2[s2].class; + new_set[s1 + s2].neg_classes = set2[s2].neg_classes; + new_set[s1 + s2].backref = set2[s2].backref; + if (set2[s2].tags == NULL) + new_set[s1 + s2].tags = NULL; + else + { + for (i = 0; set2[s2].tags[i] >= 0; i++); + new_tags = tre_mem_alloc(mem, sizeof(*new_tags) * (i + 1)); + if (new_tags == NULL) + return NULL; + for (j = 0; j < i; j++) + new_tags[j] = set2[s2].tags[j]; + new_tags[j] = -1; + new_set[s1 + s2].tags = new_tags; + } + } + new_set[s1 + s2].position = -1; + return new_set; +} + +/* Finds the empty path through `node' which is the one that should be + taken according to POSIX.2 rules, and adds the tags on that path to + `tags'. `tags' may be NULL. If `num_tags_seen' is not NULL, it is + set to the number of tags seen on the path. */ +static reg_errcode_t +tre_match_empty(tre_stack_t *stack, tre_ast_node_t *node, int *tags, + int *assertions, int *num_tags_seen) +{ + tre_literal_t *lit; + tre_union_t *uni; + tre_catenation_t *cat; + tre_iteration_t *iter; + int i; + int bottom = tre_stack_num_objects(stack); + reg_errcode_t status = REG_OK; + if (num_tags_seen) + *num_tags_seen = 0; + + status = tre_stack_push(stack, node); + + /* Walk through the tree recursively. */ + while (status == REG_OK && tre_stack_num_objects(stack) > bottom) + { + node = tre_stack_pop(stack); + + switch (node->type) + { + case LITERAL: + lit = (tre_literal_t *)node->obj; + switch (lit->code_min) + { + case TAG: + if (lit->code_max >= 0) + { + if (tags != NULL) + { + /* Add the tag to `tags'. */ + for (i = 0; tags[i] >= 0; i++) + if (tags[i] == lit->code_max) + break; + if (tags[i] < 0) + { + tags[i] = lit->code_max; + tags[i + 1] = -1; + } + } + if (num_tags_seen) + (*num_tags_seen)++; + } + break; + case ASSERTION: + assert(lit->code_max >= 1 + || lit->code_max <= ASSERT_LAST); + if (assertions != NULL) + *assertions |= lit->code_max; + break; + case EMPTY: + break; + default: + assert(0); + break; + } + break; + + case UNION: + /* Subexpressions starting earlier take priority over ones + starting later, so we prefer the left subexpression over the + right subexpression. */ + uni = (tre_union_t *)node->obj; + if (uni->left->nullable) + STACK_PUSHX(stack, uni->left) + else if (uni->right->nullable) + STACK_PUSHX(stack, uni->right) + else + assert(0); + break; + + case CATENATION: + /* The path must go through both children. */ + cat = (tre_catenation_t *)node->obj; + assert(cat->left->nullable); + assert(cat->right->nullable); + STACK_PUSHX(stack, cat->left); + STACK_PUSHX(stack, cat->right); + break; + + case ITERATION: + /* A match with an empty string is preferred over no match at + all, so we go through the argument if possible. */ + iter = (tre_iteration_t *)node->obj; + if (iter->arg->nullable) + STACK_PUSHX(stack, iter->arg); + break; + + default: + assert(0); + break; + } + } + + return status; +} + + +typedef enum { + NFL_RECURSE, + NFL_POST_UNION, + NFL_POST_CATENATION, + NFL_POST_ITERATION +} tre_nfl_stack_symbol_t; + + +/* Computes and fills in the fields `nullable', `firstpos', and `lastpos' for + the nodes of the AST `tree'. */ +static reg_errcode_t +tre_compute_nfl(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *tree) +{ + int bottom = tre_stack_num_objects(stack); + + STACK_PUSHR(stack, tree); + STACK_PUSHR(stack, NFL_RECURSE); + + while (tre_stack_num_objects(stack) > bottom) + { + tre_nfl_stack_symbol_t symbol; + tre_ast_node_t *node; + + symbol = (tre_nfl_stack_symbol_t) tre_stack_pop(stack); + node = tre_stack_pop(stack); + switch (symbol) + { + case NFL_RECURSE: + switch (node->type) + { + case LITERAL: + { + tre_literal_t *lit = (tre_literal_t *)node->obj; + if (IS_BACKREF(lit)) + { + /* Back references: nullable = false, firstpos = {i}, + lastpos = {i}. */ + node->nullable = 0; + node->firstpos = tre_set_one(mem, lit->position, 0, + TRE_CHAR_MAX, 0, NULL, -1); + if (!node->firstpos) + return REG_ESPACE; + node->lastpos = tre_set_one(mem, lit->position, 0, + TRE_CHAR_MAX, 0, NULL, + lit->code_max); + if (!node->lastpos) + return REG_ESPACE; + } + else if (lit->code_min < 0) + { + /* Tags, empty strings and zero width assertions: + nullable = true, firstpos = {}, and lastpos = {}. */ + node->nullable = 1; + node->firstpos = tre_set_empty(mem); + if (!node->firstpos) + return REG_ESPACE; + node->lastpos = tre_set_empty(mem); + if (!node->lastpos) + return REG_ESPACE; + } + else + { + /* Literal at position i: nullable = false, firstpos = {i}, + lastpos = {i}. */ + node->nullable = 0; + node->firstpos = + tre_set_one(mem, lit->position, lit->code_min, + lit->code_max, 0, NULL, -1); + if (!node->firstpos) + return REG_ESPACE; + node->lastpos = tre_set_one(mem, lit->position, + lit->code_min, lit->code_max, + lit->class, lit->neg_classes, + -1); + if (!node->lastpos) + return REG_ESPACE; + } + break; + } + + case UNION: + /* Compute the attributes for the two subtrees, and after that + for this node. */ + STACK_PUSHR(stack, node); + STACK_PUSHR(stack, NFL_POST_UNION); + STACK_PUSHR(stack, ((tre_union_t *)node->obj)->right); + STACK_PUSHR(stack, NFL_RECURSE); + STACK_PUSHR(stack, ((tre_union_t *)node->obj)->left); + STACK_PUSHR(stack, NFL_RECURSE); + break; + + case CATENATION: + /* Compute the attributes for the two subtrees, and after that + for this node. */ + STACK_PUSHR(stack, node); + STACK_PUSHR(stack, NFL_POST_CATENATION); + STACK_PUSHR(stack, ((tre_catenation_t *)node->obj)->right); + STACK_PUSHR(stack, NFL_RECURSE); + STACK_PUSHR(stack, ((tre_catenation_t *)node->obj)->left); + STACK_PUSHR(stack, NFL_RECURSE); + break; + + case ITERATION: + /* Compute the attributes for the subtree, and after that for + this node. */ + STACK_PUSHR(stack, node); + STACK_PUSHR(stack, NFL_POST_ITERATION); + STACK_PUSHR(stack, ((tre_iteration_t *)node->obj)->arg); + STACK_PUSHR(stack, NFL_RECURSE); + break; + } + break; /* end case: NFL_RECURSE */ + + case NFL_POST_UNION: + { + tre_union_t *uni = (tre_union_t *)node->obj; + node->nullable = uni->left->nullable || uni->right->nullable; + node->firstpos = tre_set_union(mem, uni->left->firstpos, + uni->right->firstpos, NULL, 0); + if (!node->firstpos) + return REG_ESPACE; + node->lastpos = tre_set_union(mem, uni->left->lastpos, + uni->right->lastpos, NULL, 0); + if (!node->lastpos) + return REG_ESPACE; + break; + } + + case NFL_POST_ITERATION: + { + tre_iteration_t *iter = (tre_iteration_t *)node->obj; + + if (iter->min == 0 || iter->arg->nullable) + node->nullable = 1; + else + node->nullable = 0; + node->firstpos = iter->arg->firstpos; + node->lastpos = iter->arg->lastpos; + break; + } + + case NFL_POST_CATENATION: + { + int num_tags, *tags, assertions; + reg_errcode_t status; + tre_catenation_t *cat = node->obj; + node->nullable = cat->left->nullable && cat->right->nullable; + + /* Compute firstpos. */ + if (cat->left->nullable) + { + /* The left side matches the empty string. Make a first pass + with tre_match_empty() to get the number of tags. */ + status = tre_match_empty(stack, cat->left, + NULL, NULL, &num_tags); + if (status != REG_OK) + return status; + /* Allocate arrays for the tags and parameters. */ + tags = xmalloc(sizeof(*tags) * (num_tags + 1)); + if (!tags) + return REG_ESPACE; + tags[0] = -1; + assertions = 0; + /* Second pass with tre_mach_empty() to get the list of + tags. */ + status = tre_match_empty(stack, cat->left, tags, + &assertions, NULL); + if (status != REG_OK) + { + xfree(tags); + return status; + } + node->firstpos = + tre_set_union(mem, cat->right->firstpos, cat->left->firstpos, + tags, assertions); + xfree(tags); + if (!node->firstpos) + return REG_ESPACE; + } + else + { + node->firstpos = cat->left->firstpos; + } + + /* Compute lastpos. */ + if (cat->right->nullable) + { + /* The right side matches the empty string. Make a first pass + with tre_match_empty() to get the number of tags. */ + status = tre_match_empty(stack, cat->right, + NULL, NULL, &num_tags); + if (status != REG_OK) + return status; + /* Allocate arrays for the tags and parameters. */ + tags = xmalloc(sizeof(int) * (num_tags + 1)); + if (!tags) + return REG_ESPACE; + tags[0] = -1; + assertions = 0; + /* Second pass with tre_mach_empty() to get the list of + tags. */ + status = tre_match_empty(stack, cat->right, tags, + &assertions, NULL); + if (status != REG_OK) + { + xfree(tags); + return status; + } + node->lastpos = + tre_set_union(mem, cat->left->lastpos, cat->right->lastpos, + tags, assertions); + xfree(tags); + if (!node->lastpos) + return REG_ESPACE; + } + else + { + node->lastpos = cat->right->lastpos; + } + break; + } + + default: + assert(0); + break; + } + } + + return REG_OK; +} + + +/* Adds a transition from each position in `p1' to each position in `p2'. */ +static reg_errcode_t +tre_make_trans(tre_pos_and_tags_t *p1, tre_pos_and_tags_t *p2, + tre_tnfa_transition_t *transitions, + int *counts, int *offs) +{ + tre_pos_and_tags_t *orig_p2 = p2; + tre_tnfa_transition_t *trans; + int i, j, k, l, dup, prev_p2_pos; + + if (transitions != NULL) + while (p1->position >= 0) + { + p2 = orig_p2; + prev_p2_pos = -1; + while (p2->position >= 0) + { + /* Optimization: if this position was already handled, skip it. */ + if (p2->position == prev_p2_pos) + { + p2++; + continue; + } + prev_p2_pos = p2->position; + /* Set `trans' to point to the next unused transition from + position `p1->position'. */ + trans = transitions + offs[p1->position]; + while (trans->state != NULL) + { +#if 0 + /* If we find a previous transition from `p1->position' to + `p2->position', it is overwritten. This can happen only + if there are nested loops in the regexp, like in "((a)*)*". + In POSIX.2 repetition using the outer loop is always + preferred over using the inner loop. Therefore the + transition for the inner loop is useless and can be thrown + away. */ + /* XXX - The same position is used for all nodes in a bracket + expression, so this optimization cannot be used (it will + break bracket expressions) unless I figure out a way to + detect it here. */ + if (trans->state_id == p2->position) + { + DPRINT(("*")); + break; + } +#endif + trans++; + } + + if (trans->state == NULL) + (trans + 1)->state = NULL; + /* Use the character ranges, assertions, etc. from `p1' for + the transition from `p1' to `p2'. */ + trans->code_min = p1->code_min; + trans->code_max = p1->code_max; + trans->state = transitions + offs[p2->position]; + trans->state_id = p2->position; + trans->assertions = p1->assertions | p2->assertions + | (p1->class ? ASSERT_CHAR_CLASS : 0) + | (p1->neg_classes != NULL ? ASSERT_CHAR_CLASS_NEG : 0); + if (p1->backref >= 0) + { + assert((trans->assertions & ASSERT_CHAR_CLASS) == 0); + assert(p2->backref < 0); + trans->u.backref = p1->backref; + trans->assertions |= ASSERT_BACKREF; + } + else + trans->u.class = p1->class; + if (p1->neg_classes != NULL) + { + for (i = 0; p1->neg_classes[i] != (tre_ctype_t)0; i++); + trans->neg_classes = + xmalloc(sizeof(*trans->neg_classes) * (i + 1)); + if (trans->neg_classes == NULL) + return REG_ESPACE; + for (i = 0; p1->neg_classes[i] != (tre_ctype_t)0; i++) + trans->neg_classes[i] = p1->neg_classes[i]; + trans->neg_classes[i] = (tre_ctype_t)0; + } + else + trans->neg_classes = NULL; + + /* Find out how many tags this transition has. */ + i = 0; + if (p1->tags != NULL) + while(p1->tags[i] >= 0) + i++; + j = 0; + if (p2->tags != NULL) + while(p2->tags[j] >= 0) + j++; + + /* If we are overwriting a transition, free the old tag array. */ + if (trans->tags != NULL) + xfree(trans->tags); + trans->tags = NULL; + + /* If there were any tags, allocate an array and fill it. */ + if (i + j > 0) + { + trans->tags = xmalloc(sizeof(*trans->tags) * (i + j + 1)); + if (!trans->tags) + return REG_ESPACE; + i = 0; + if (p1->tags != NULL) + while(p1->tags[i] >= 0) + { + trans->tags[i] = p1->tags[i]; + i++; + } + l = i; + j = 0; + if (p2->tags != NULL) + while (p2->tags[j] >= 0) + { + /* Don't add duplicates. */ + dup = 0; + for (k = 0; k < i; k++) + if (trans->tags[k] == p2->tags[j]) + { + dup = 1; + break; + } + if (!dup) + trans->tags[l++] = p2->tags[j]; + j++; + } + trans->tags[l] = -1; + } + + +#ifdef TRE_DEBUG + { + int *tags; + + DPRINT((" %2d -> %2d on %3d", p1->position, p2->position, + p1->code_min)); + if (p1->code_max != p1->code_min) + DPRINT(("-%3d", p1->code_max)); + tags = trans->tags; + if (tags) + { + DPRINT((", tags [")); + while (*tags >= 0) + { + DPRINT(("%d", *tags)); + tags++; + if (*tags >= 0) + DPRINT((",")); + } + DPRINT(("]")); + } + if (trans->assertions) + DPRINT((", assert %d", trans->assertions)); + if (trans->assertions & ASSERT_BACKREF) + DPRINT((", backref %d", trans->u.backref)); + else if (trans->class) + DPRINT((", class %ld", (long)trans->class)); + if (trans->neg_classes) + DPRINT((", neg_classes %p", trans->neg_classes)); + DPRINT(("\n")); + } +#endif /* TRE_DEBUG */ + p2++; + } + p1++; + } + else + /* Compute a maximum limit for the number of transitions leaving + from each state. */ + while (p1->position >= 0) + { + p2 = orig_p2; + while (p2->position >= 0) + { + counts[p1->position]++; + p2++; + } + p1++; + } + return REG_OK; +} + +/* Converts the syntax tree to a TNFA. All the transitions in the TNFA are + labelled with one character range (there are no transitions on empty + strings). The TNFA takes O(n^2) space in the worst case, `n' is size of + the regexp. */ +static reg_errcode_t +tre_ast_to_tnfa(tre_ast_node_t *node, tre_tnfa_transition_t *transitions, + int *counts, int *offs) +{ + tre_union_t *uni; + tre_catenation_t *cat; + tre_iteration_t *iter; + reg_errcode_t errcode = REG_OK; + + /* XXX - recurse using a stack!. */ + switch (node->type) + { + case LITERAL: + break; + case UNION: + uni = (tre_union_t *)node->obj; + errcode = tre_ast_to_tnfa(uni->left, transitions, counts, offs); + if (errcode != REG_OK) + return errcode; + errcode = tre_ast_to_tnfa(uni->right, transitions, counts, offs); + break; + + case CATENATION: + cat = (tre_catenation_t *)node->obj; + /* Add a transition from each position in cat->left->lastpos + to each position in cat->right->firstpos. */ + errcode = tre_make_trans(cat->left->lastpos, cat->right->firstpos, + transitions, counts, offs); + if (errcode != REG_OK) + return errcode; + errcode = tre_ast_to_tnfa(cat->left, transitions, counts, offs); + if (errcode != REG_OK) + return errcode; + errcode = tre_ast_to_tnfa(cat->right, transitions, counts, offs); + break; + + case ITERATION: + iter = (tre_iteration_t *)node->obj; + assert(iter->max == -1 || iter->max == 1); + + if (iter->max == -1) + { + assert(iter->min == 0 || iter->min == 1); + /* Add a transition from each last position in the iterated + expression to each first position. */ + errcode = tre_make_trans(iter->arg->lastpos, iter->arg->firstpos, + transitions, counts, offs); + if (errcode != REG_OK) + return errcode; + } + errcode = tre_ast_to_tnfa(iter->arg, transitions, counts, offs); + break; + } + return errcode; +} + + +static void +tre_free(regex_t *preg) +{ + tre_tnfa_t *tnfa; + unsigned int i; + tre_tnfa_transition_t *trans; + + tnfa = (void *)preg->TRE_REGEX_T_FIELD; + if (!tnfa) + return; + + for (i = 0; i < tnfa->num_transitions; i++) + if (tnfa->transitions[i].state) + { + if (tnfa->transitions[i].tags) + xfree(tnfa->transitions[i].tags); + if (tnfa->transitions[i].neg_classes) + xfree(tnfa->transitions[i].neg_classes); + } + if (tnfa->transitions) + xfree(tnfa->transitions); + + if (tnfa->initial) + { + for (trans = tnfa->initial; trans->state; trans++) + { + if (trans->tags) + xfree(trans->tags); + } + xfree(tnfa->initial); + } + + if (tnfa->submatch_data) + { + for (i = 0; i < tnfa->num_submatches; i++) + if (tnfa->submatch_data[i].parents) + xfree(tnfa->submatch_data[i].parents); + xfree(tnfa->submatch_data); + } + + if (tnfa->tag_directions) + xfree(tnfa->tag_directions); + xfree(tnfa); +} + + +#define ERROR_EXIT(err) \ + do \ + { \ + errcode = err; \ + if (1) goto error_exit; \ + } \ + while (0) + + +static int +tre_compile(regex_t *preg, const tre_char_t *regex, size_t n, int cflags) +{ + tre_stack_t *stack; + tre_ast_node_t *tree, *tmp_ast_l, *tmp_ast_r; + tre_pos_and_tags_t *p; + int *counts = NULL, *offs = NULL; + int i, add = 0; + tre_tnfa_transition_t *transitions, *initial; + tre_tnfa_t *tnfa = NULL; + tre_submatch_data_t *submatch_data; + tre_tag_direction_t *tag_directions = NULL; + reg_errcode_t errcode; + tre_mem_t mem; + + /* Parse context. */ + tre_parse_ctx_t parse_ctx; + + /* Allocate a stack used throughout the compilation process for various + purposes. */ + stack = tre_stack_new(512, 10240, 128); + if (!stack) + return REG_ESPACE; + /* Allocate a fast memory allocator. */ + mem = tre_mem_new(); + if (!mem) + { + tre_stack_destroy(stack); + return REG_ESPACE; + } + + /* Parse the regexp. */ + memset(&parse_ctx, 0, sizeof(parse_ctx)); + parse_ctx.mem = mem; + parse_ctx.stack = stack; + parse_ctx.re = regex; + parse_ctx.len = n; + parse_ctx.cflags = cflags; + parse_ctx.max_backref = -1; + DPRINT(("tre_compile: parsing '%.*" STRF "'\n", n, regex)); + errcode = tre_parse(&parse_ctx); + if (errcode != REG_OK) + ERROR_EXIT(errcode); + preg->re_nsub = parse_ctx.submatch_id - 1; + tree = parse_ctx.result; + +#ifdef TRE_DEBUG + tre_ast_print(tree); +#endif /* TRE_DEBUG */ + + /* Referring to nonexistent subexpressions is illegal. */ + if (parse_ctx.max_backref > (int)preg->re_nsub) + ERROR_EXIT(REG_ESUBREG); + + /* Allocate the TNFA struct. */ + tnfa = xcalloc(1, sizeof(tre_tnfa_t)); + if (tnfa == NULL) + ERROR_EXIT(REG_ESPACE); + tnfa->have_backrefs = parse_ctx.max_backref >= 0; + tnfa->num_submatches = parse_ctx.submatch_id; + + /* Set up tags for submatch addressing. If REG_NOSUB is set and the + regexp does not have back references, this can be skipped. */ + if (tnfa->have_backrefs || !(cflags & REG_NOSUB)) + { + DPRINT(("tre_compile: setting up tags\n")); + + /* Figure out how many tags we will need. */ + errcode = tre_add_tags(NULL, stack, tree, tnfa); + if (errcode != REG_OK) + ERROR_EXIT(errcode); +#ifdef TRE_DEBUG + tre_ast_print(tree); +#endif /* TRE_DEBUG */ + + if (tnfa->num_tags > 0) + { + tag_directions = xmalloc(sizeof(*tag_directions) + * (tnfa->num_tags + 1)); + if (tag_directions == NULL) + ERROR_EXIT(REG_ESPACE); + tnfa->tag_directions = tag_directions; + memset(tag_directions, -1, + sizeof(*tag_directions) * (tnfa->num_tags + 1)); + } + + submatch_data = xcalloc(parse_ctx.submatch_id, sizeof(*submatch_data)); + if (submatch_data == NULL) + ERROR_EXIT(REG_ESPACE); + tnfa->submatch_data = submatch_data; + + errcode = tre_add_tags(mem, stack, tree, tnfa); + if (errcode != REG_OK) + ERROR_EXIT(errcode); + +#ifdef TRE_DEBUG + for (i = 0; i < parse_ctx.submatch_id; i++) + DPRINT(("pmatch[%d] = {t%d, t%d}\n", + i, submatch_data[i].so_tag, submatch_data[i].eo_tag)); + for (i = 0; i < tnfa->num_tags; i++) + DPRINT(("t%d is %s\n", i, + tag_directions[i] == TRE_TAG_MINIMIZE ? + "minimized" : "maximized")); +#endif /* TRE_DEBUG */ + } + + /* Expand iteration nodes. */ + errcode = tre_expand_ast(mem, stack, tree, &parse_ctx.position, + tag_directions, NULL); + if (errcode != REG_OK) + ERROR_EXIT(errcode); + + /* Add a dummy node for the final state. + XXX - For certain patterns this dummy node can be optimized away, + for example "a*" or "ab*". Figure out a simple way to detect + this possibility. */ + tmp_ast_l = tree; + tmp_ast_r = tre_ast_new_literal(mem, 0, 0, parse_ctx.position++); + if (tmp_ast_r == NULL) + ERROR_EXIT(REG_ESPACE); + + tree = tre_ast_new_catenation(mem, tmp_ast_l, tmp_ast_r); + if (tree == NULL) + ERROR_EXIT(REG_ESPACE); + +#ifdef TRE_DEBUG + tre_ast_print(tree); + DPRINT(("Number of states: %d\n", parse_ctx.position)); +#endif /* TRE_DEBUG */ + + errcode = tre_compute_nfl(mem, stack, tree); + if (errcode != REG_OK) + ERROR_EXIT(errcode); + + counts = xmalloc(sizeof(int) * parse_ctx.position); + if (counts == NULL) + ERROR_EXIT(REG_ESPACE); + + offs = xmalloc(sizeof(int) * parse_ctx.position); + if (offs == NULL) + ERROR_EXIT(REG_ESPACE); + + for (i = 0; i < parse_ctx.position; i++) + counts[i] = 0; + tre_ast_to_tnfa(tree, NULL, counts, NULL); + + add = 0; + for (i = 0; i < parse_ctx.position; i++) + { + offs[i] = add; + add += counts[i] + 1; + counts[i] = 0; + } + transitions = xcalloc(add + 1, sizeof(*transitions)); + if (transitions == NULL) + ERROR_EXIT(REG_ESPACE); + tnfa->transitions = transitions; + tnfa->num_transitions = add; + + DPRINT(("Converting to TNFA:\n")); + errcode = tre_ast_to_tnfa(tree, transitions, counts, offs); + if (errcode != REG_OK) + ERROR_EXIT(errcode); + + p = tree->firstpos; + i = 0; + while (p->position >= 0) + { + i++; + +#ifdef TRE_DEBUG + { + int *tags; + DPRINT(("initial: %d", p->position)); + tags = p->tags; + if (tags != NULL) + { + if (*tags >= 0) + DPRINT(("/")); + while (*tags >= 0) + { + DPRINT(("%d", *tags)); + tags++; + if (*tags >= 0) + DPRINT((",")); + } + } + DPRINT((", assert %d", p->assertions)); + DPRINT(("\n")); + } +#endif /* TRE_DEBUG */ + + p++; + } + + initial = xcalloc(i + 1, sizeof(tre_tnfa_transition_t)); + if (initial == NULL) + ERROR_EXIT(REG_ESPACE); + tnfa->initial = initial; + + i = 0; + for (p = tree->firstpos; p->position >= 0; p++) + { + initial[i].state = transitions + offs[p->position]; + initial[i].state_id = p->position; + initial[i].tags = NULL; + /* Copy the arrays p->tags, they are allocated + from a tre_mem object. */ + if (p->tags) + { + int j; + for (j = 0; p->tags[j] >= 0; j++); + initial[i].tags = xmalloc(sizeof(*p->tags) * (j + 1)); + if (!initial[i].tags) + ERROR_EXIT(REG_ESPACE); + memcpy(initial[i].tags, p->tags, sizeof(*p->tags) * (j + 1)); + } + initial[i].assertions = p->assertions; + i++; + } + initial[i].state = NULL; + + tnfa->num_transitions = add; + tnfa->final = transitions + offs[tree->lastpos[0].position]; + tnfa->num_states = parse_ctx.position; + tnfa->cflags = cflags; + + DPRINT(("final state %p\n", (void *)tnfa->final)); + + tre_mem_destroy(mem); + tre_stack_destroy(stack); + xfree(counts); + xfree(offs); + + preg->TRE_REGEX_T_FIELD = (void *)tnfa; + return REG_OK; + + error_exit: + /* Free everything that was allocated and return the error code. */ + tre_mem_destroy(mem); + if (stack != NULL) + tre_stack_destroy(stack); + if (counts != NULL) + xfree(counts); + if (offs != NULL) + xfree(offs); + preg->TRE_REGEX_T_FIELD = (void *)tnfa; + tre_free(preg); + return errcode; +} + + +/*********************************************************************** + from regcomp.c +***********************************************************************/ + +int +regcomp(regex_t *preg, const char *regex, int cflags) +{ + int ret; + tre_char_t *wregex; + size_t n = strlen(regex); + + if (n+1 > SIZE_MAX/sizeof(tre_char_t)) + return REG_ESPACE; + wregex = xmalloc(sizeof(tre_char_t) * (n + 1)); + if (wregex == NULL) + return REG_ESPACE; + + n = mbstowcs(wregex, regex, n+1); + if (n == (size_t)-1) { + xfree(wregex); + return REG_BADPAT; + } + + ret = tre_compile(preg, wregex, n, cflags); + xfree(wregex); + + return ret; +} + +void +regfree(regex_t *preg) +{ + tre_free(preg); +} + +/* EOF */ diff --git a/05/musl-final/src/regex/regerror.c b/05/musl-final/src/regex/regerror.c new file mode 100644 index 0000000..39d70b2 --- /dev/null +++ b/05/musl-final/src/regex/regerror.c @@ -0,0 +1,75 @@ +/* + regerror.c - POSIX regerror() implementation for TRE. + + Copyright (c) 2001-2006 Ville Laurikari . + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#include +#include + +/* Error message strings for error codes listed in `regex.h'. This list + needs to be in sync with the codes listed there, naturally. */ + +/* Converted to single string by Rich Felker to remove the need for + * data relocations at runtime, 27 Feb 2006. */ + +static const char tre_error_messages[] = { + "No error\0" + "No match\0" + "Invalid regexp\0" + "Unknown collating element\0" + "Unknown character class name\0" + "Trailing backslash\0" + "Invalid back reference\0" + "Missing ']'\0" + "Missing ')'\0" + "Missing '}'\0" + "Invalid contents of {}\0" + "Invalid character range\0" + "Out of memory\0" + "XXX\0" +}; + +size_t +regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) +{ + const char *err; + size_t err_len; + + if (errcode >= 0 && errcode <= REG_BADRPT) + for (err=tre_error_messages; errcode; errcode--, err+=strlen(err)+1); + else + err = "Unknown error"; + + err_len = strlen(err) + 1; + if (errbuf_size > 0 && errbuf != NULL) + { + if (err_len > errbuf_size) + { + memcpy(errbuf, err, errbuf_size - 1); + errbuf[errbuf_size - 1] = '\0'; + } + else + { + strcpy(errbuf, err); + } + } + return err_len; +} + +/* EOF */ diff --git a/05/musl-final/src/regex/regexec.c b/05/musl-final/src/regex/regexec.c new file mode 100644 index 0000000..0c3d283 --- /dev/null +++ b/05/musl-final/src/regex/regexec.c @@ -0,0 +1,1107 @@ +/* + regexec.c - TRE POSIX compatible matching functions (and more). + + Copyright (c) 2001-2006 Ville Laurikari . + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#include +#include +#include +#include +#include + +#include + +#include "tre.h" + +#include + +static void +tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags, + const tre_tnfa_t *tnfa, int *tags, int match_eo); + +/*********************************************************************** + from tre-match-utils.h +***********************************************************************/ + +#define GET_NEXT_WCHAR() do { \ + prev_c = next_c; pos += pos_add_next; \ + if ((pos_add_next = mbtowc(&next_c, str_byte, MB_LEN_MAX)) <= 0) { \ + if (pos_add_next < 0) return REG_NOMATCH; \ + else pos_add_next++; \ + } \ + str_byte += pos_add_next; \ + } while (0) + +#define CHECK_ASSERTIONS(assertions) \ + (((assertions & ASSERT_AT_BOL) \ + && (pos > 0 || reg_notbol) \ + && (prev_c != L'\n' || !reg_newline)) \ + || ((assertions & ASSERT_AT_EOL) \ + && (next_c != L'\0' || reg_noteol) \ + && (next_c != L'\n' || !reg_newline))) + +/* Returns 1 if `t1' wins `t2', 0 otherwise. */ +static int +tre_tag_order(int num_tags, tre_tag_direction_t *tag_directions, + int *t1, int *t2) +{ + int i; + for (i = 0; i < num_tags; i++) + { + if (tag_directions[i] == TRE_TAG_MINIMIZE) + { + if (t1[i] < t2[i]) + return 1; + if (t1[i] > t2[i]) + return 0; + } + else + { + if (t1[i] > t2[i]) + return 1; + if (t1[i] < t2[i]) + return 0; + } + } + /* assert(0);*/ + return 0; +} + +static int +tre_neg_char_classes_match(tre_ctype_t *classes, tre_cint_t wc, int icase) +{ + DPRINT(("neg_char_classes_test: %p, %d, %d\n", classes, wc, icase)); + while (*classes != (tre_ctype_t)0) + if ((!icase && tre_isctype(wc, *classes)) + || (icase && (tre_isctype(tre_toupper(wc), *classes) + || tre_isctype(tre_tolower(wc), *classes)))) + return 1; /* Match. */ + else + classes++; + return 0; /* No match. */ +} + + +/*********************************************************************** + from tre-match-parallel.c +***********************************************************************/ + +/* + This algorithm searches for matches basically by reading characters + in the searched string one by one, starting at the beginning. All + matching paths in the TNFA are traversed in parallel. When two or + more paths reach the same state, exactly one is chosen according to + tag ordering rules; if returning submatches is not required it does + not matter which path is chosen. + + The worst case time required for finding the leftmost and longest + match, or determining that there is no match, is always linearly + dependent on the length of the text being searched. + + This algorithm cannot handle TNFAs with back referencing nodes. + See `tre-match-backtrack.c'. +*/ + + +typedef struct { + tre_tnfa_transition_t *state; + int *tags; +} tre_tnfa_reach_t; + +typedef struct { + int pos; + int **tags; +} tre_reach_pos_t; + + +#ifdef TRE_DEBUG +static void +tre_print_reach(const tre_tnfa_t *tnfa, tre_tnfa_reach_t *reach, int num_tags) +{ + int i; + + while (reach->state != NULL) + { + DPRINT((" %p", (void *)reach->state)); + if (num_tags > 0) + { + DPRINT(("/")); + for (i = 0; i < num_tags; i++) + { + DPRINT(("%d:%d", i, reach->tags[i])); + if (i < (num_tags-1)) + DPRINT((",")); + } + } + reach++; + } + DPRINT(("\n")); + +} +#endif /* TRE_DEBUG */ + +static reg_errcode_t +tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len, + int *match_tags, int eflags, int *match_end_ofs) +{ + /* State variables required by GET_NEXT_WCHAR. */ + tre_char_t prev_c = 0, next_c = 0; + const char *str_byte = string; + int pos = -1; + int pos_add_next = 1; +#ifdef TRE_MBSTATE + mbstate_t mbstate; +#endif /* TRE_MBSTATE */ + int reg_notbol = eflags & REG_NOTBOL; + int reg_noteol = eflags & REG_NOTEOL; + int reg_newline = tnfa->cflags & REG_NEWLINE; + + char *buf; + tre_tnfa_transition_t *trans_i; + tre_tnfa_reach_t *reach, *reach_next, *reach_i, *reach_next_i; + tre_reach_pos_t *reach_pos; + int *tag_i; + int num_tags, i; + + int match_eo = -1; /* end offset of match (-1 if no match found yet) */ + int new_match = 0; + int *tmp_tags = NULL; + int *tmp_iptr; + +#ifdef TRE_MBSTATE + memset(&mbstate, '\0', sizeof(mbstate)); +#endif /* TRE_MBSTATE */ + + if (!match_tags) + num_tags = 0; + else + num_tags = tnfa->num_tags; + + /* Allocate memory for temporary data required for matching. This needs to + be done for every matching operation to be thread safe. This allocates + everything in a single large block from the stack frame using alloca() + or with malloc() if alloca is unavailable. */ + { + int tbytes, rbytes, pbytes, xbytes, total_bytes; + char *tmp_buf; + /* Compute the length of the block we need. */ + tbytes = sizeof(*tmp_tags) * num_tags; + rbytes = sizeof(*reach_next) * (tnfa->num_states + 1); + pbytes = sizeof(*reach_pos) * tnfa->num_states; + xbytes = sizeof(int) * num_tags; + total_bytes = + (sizeof(long) - 1) * 4 /* for alignment paddings */ + + (rbytes + xbytes * tnfa->num_states) * 2 + tbytes + pbytes; + + /* Allocate the memory. */ +#ifdef TRE_USE_ALLOCA + buf = alloca(total_bytes); +#else /* !TRE_USE_ALLOCA */ + buf = xmalloc(total_bytes); +#endif /* !TRE_USE_ALLOCA */ + if (buf == NULL) + return REG_ESPACE; + memset(buf, 0, total_bytes); + + /* Get the various pointers within tmp_buf (properly aligned). */ + tmp_tags = (void *)buf; + tmp_buf = buf + tbytes; + tmp_buf += ALIGN(tmp_buf, long); + reach_next = (void *)tmp_buf; + tmp_buf += rbytes; + tmp_buf += ALIGN(tmp_buf, long); + reach = (void *)tmp_buf; + tmp_buf += rbytes; + tmp_buf += ALIGN(tmp_buf, long); + reach_pos = (void *)tmp_buf; + tmp_buf += pbytes; + tmp_buf += ALIGN(tmp_buf, long); + for (i = 0; i < tnfa->num_states; i++) + { + reach[i].tags = (void *)tmp_buf; + tmp_buf += xbytes; + reach_next[i].tags = (void *)tmp_buf; + tmp_buf += xbytes; + } + } + + for (i = 0; i < tnfa->num_states; i++) + reach_pos[i].pos = -1; + + GET_NEXT_WCHAR(); + pos = 0; + + DPRINT(("length: %d\n", len)); + DPRINT(("pos:chr/code | states and tags\n")); + DPRINT(("-------------+------------------------------------------------\n")); + + reach_next_i = reach_next; + while (1) + { + /* If no match found yet, add the initial states to `reach_next'. */ + if (match_eo < 0) + { + DPRINT((" init >")); + trans_i = tnfa->initial; + while (trans_i->state != NULL) + { + if (reach_pos[trans_i->state_id].pos < pos) + { + if (trans_i->assertions + && CHECK_ASSERTIONS(trans_i->assertions)) + { + DPRINT(("assertion failed\n")); + trans_i++; + continue; + } + + DPRINT((" %p", (void *)trans_i->state)); + reach_next_i->state = trans_i->state; + for (i = 0; i < num_tags; i++) + reach_next_i->tags[i] = -1; + tag_i = trans_i->tags; + if (tag_i) + while (*tag_i >= 0) + { + if (*tag_i < num_tags) + reach_next_i->tags[*tag_i] = pos; + tag_i++; + } + if (reach_next_i->state == tnfa->final) + { + DPRINT((" found empty match\n")); + match_eo = pos; + new_match = 1; + for (i = 0; i < num_tags; i++) + match_tags[i] = reach_next_i->tags[i]; + } + reach_pos[trans_i->state_id].pos = pos; + reach_pos[trans_i->state_id].tags = &reach_next_i->tags; + reach_next_i++; + } + trans_i++; + } + DPRINT(("\n")); + reach_next_i->state = NULL; + } + else + { + if (num_tags == 0 || reach_next_i == reach_next) + /* We have found a match. */ + break; + } + + /* Check for end of string. */ + if (!next_c) break; + + GET_NEXT_WCHAR(); + +#ifdef TRE_DEBUG + DPRINT(("%3d:%2lc/%05d |", pos - 1, (tre_cint_t)prev_c, (int)prev_c)); + tre_print_reach(tnfa, reach_next, num_tags); + DPRINT(("%3d:%2lc/%05d |", pos, (tre_cint_t)next_c, (int)next_c)); + tre_print_reach(tnfa, reach_next, num_tags); +#endif /* TRE_DEBUG */ + + /* Swap `reach' and `reach_next'. */ + reach_i = reach; + reach = reach_next; + reach_next = reach_i; + + /* For each state in `reach' see if there is a transition leaving with + the current input symbol to a state not yet in `reach_next', and + add the destination states to `reach_next'. */ + reach_next_i = reach_next; + for (reach_i = reach; reach_i->state; reach_i++) + { + for (trans_i = reach_i->state; trans_i->state; trans_i++) + { + /* Does this transition match the input symbol? */ + if (trans_i->code_min <= prev_c && + trans_i->code_max >= prev_c) + { + if (trans_i->assertions + && (CHECK_ASSERTIONS(trans_i->assertions) + /* Handle character class transitions. */ + || ((trans_i->assertions & ASSERT_CHAR_CLASS) + && !(tnfa->cflags & REG_ICASE) + && !tre_isctype((tre_cint_t)prev_c, + trans_i->u.class)) + || ((trans_i->assertions & ASSERT_CHAR_CLASS) + && (tnfa->cflags & REG_ICASE) + && (!tre_isctype(tre_tolower((tre_cint_t)prev_c), + trans_i->u.class) + && !tre_isctype(tre_toupper((tre_cint_t)prev_c), + trans_i->u.class))) + || ((trans_i->assertions & ASSERT_CHAR_CLASS_NEG) + && tre_neg_char_classes_match(trans_i->neg_classes, + (tre_cint_t)prev_c, + tnfa->cflags & REG_ICASE)))) + { + DPRINT(("assertion failed\n")); + continue; + } + + /* Compute the tags after this transition. */ + for (i = 0; i < num_tags; i++) + tmp_tags[i] = reach_i->tags[i]; + tag_i = trans_i->tags; + if (tag_i != NULL) + while (*tag_i >= 0) + { + if (*tag_i < num_tags) + tmp_tags[*tag_i] = pos; + tag_i++; + } + + if (reach_pos[trans_i->state_id].pos < pos) + { + /* Found an unvisited node. */ + reach_next_i->state = trans_i->state; + tmp_iptr = reach_next_i->tags; + reach_next_i->tags = tmp_tags; + tmp_tags = tmp_iptr; + reach_pos[trans_i->state_id].pos = pos; + reach_pos[trans_i->state_id].tags = &reach_next_i->tags; + + if (reach_next_i->state == tnfa->final + && (match_eo == -1 + || (num_tags > 0 + && reach_next_i->tags[0] <= match_tags[0]))) + { + DPRINT((" found match %p\n", trans_i->state)); + match_eo = pos; + new_match = 1; + for (i = 0; i < num_tags; i++) + match_tags[i] = reach_next_i->tags[i]; + } + reach_next_i++; + + } + else + { + assert(reach_pos[trans_i->state_id].pos == pos); + /* Another path has also reached this state. We choose + the winner by examining the tag values for both + paths. */ + if (tre_tag_order(num_tags, tnfa->tag_directions, + tmp_tags, + *reach_pos[trans_i->state_id].tags)) + { + /* The new path wins. */ + tmp_iptr = *reach_pos[trans_i->state_id].tags; + *reach_pos[trans_i->state_id].tags = tmp_tags; + if (trans_i->state == tnfa->final) + { + DPRINT((" found better match\n")); + match_eo = pos; + new_match = 1; + for (i = 0; i < num_tags; i++) + match_tags[i] = tmp_tags[i]; + } + tmp_tags = tmp_iptr; + } + } + } + } + } + reach_next_i->state = NULL; + } + + DPRINT(("match end offset = %d\n", match_eo)); + +#ifndef TRE_USE_ALLOCA + if (buf) + xfree(buf); +#endif /* !TRE_USE_ALLOCA */ + + *match_end_ofs = match_eo; + return match_eo >= 0 ? REG_OK : REG_NOMATCH; +} + + +/*********************************************************************** + from tre-match-backtrack.c +***********************************************************************/ + +/* + This matcher is for regexps that use back referencing. Regexp matching + with back referencing is an NP-complete problem on the number of back + references. The easiest way to match them is to use a backtracking + routine which basically goes through all possible paths in the TNFA + and chooses the one which results in the best (leftmost and longest) + match. This can be spectacularly expensive and may run out of stack + space, but there really is no better known generic algorithm. Quoting + Henry Spencer from comp.compilers: + + + POSIX.2 REs require longest match, which is really exciting to + implement since the obsolete ("basic") variant also includes + \. I haven't found a better way of tackling this than doing + a preliminary match using a DFA (or simulation) on a modified RE + that just replicates subREs for \, and then doing a + backtracking match to determine whether the subRE matches were + right. This can be rather slow, but I console myself with the + thought that people who use \ deserve very slow execution. + (Pun unintentional but very appropriate.) + +*/ + +typedef struct { + int pos; + const char *str_byte; + tre_tnfa_transition_t *state; + int state_id; + int next_c; + int *tags; +#ifdef TRE_MBSTATE + mbstate_t mbstate; +#endif /* TRE_MBSTATE */ +} tre_backtrack_item_t; + +typedef struct tre_backtrack_struct { + tre_backtrack_item_t item; + struct tre_backtrack_struct *prev; + struct tre_backtrack_struct *next; +} *tre_backtrack_t; + +#ifdef TRE_MBSTATE +#define BT_STACK_MBSTATE_IN stack->item.mbstate = (mbstate) +#define BT_STACK_MBSTATE_OUT (mbstate) = stack->item.mbstate +#else /* !TRE_MBSTATE */ +#define BT_STACK_MBSTATE_IN +#define BT_STACK_MBSTATE_OUT +#endif /* !TRE_MBSTATE */ + + +#ifdef TRE_USE_ALLOCA +#define tre_bt_mem_new tre_mem_newa +#define tre_bt_mem_alloc tre_mem_alloca +#define tre_bt_mem_destroy(obj) do { } while (0) +#else /* !TRE_USE_ALLOCA */ +#define tre_bt_mem_new tre_mem_new +#define tre_bt_mem_alloc tre_mem_alloc +#define tre_bt_mem_destroy tre_mem_destroy +#endif /* !TRE_USE_ALLOCA */ + + +#define BT_STACK_PUSH(_pos, _str_byte, _str_wide, _state, _state_id, _next_c, _tags, _mbstate) \ + do \ + { \ + int i; \ + if (!stack->next) \ + { \ + tre_backtrack_t s; \ + s = tre_bt_mem_alloc(mem, sizeof(*s)); \ + if (!s) \ + { \ + tre_bt_mem_destroy(mem); \ + if (tags) \ + xfree(tags); \ + if (pmatch) \ + xfree(pmatch); \ + if (states_seen) \ + xfree(states_seen); \ + return REG_ESPACE; \ + } \ + s->prev = stack; \ + s->next = NULL; \ + s->item.tags = tre_bt_mem_alloc(mem, \ + sizeof(*tags) * tnfa->num_tags); \ + if (!s->item.tags) \ + { \ + tre_bt_mem_destroy(mem); \ + if (tags) \ + xfree(tags); \ + if (pmatch) \ + xfree(pmatch); \ + if (states_seen) \ + xfree(states_seen); \ + return REG_ESPACE; \ + } \ + stack->next = s; \ + stack = s; \ + } \ + else \ + stack = stack->next; \ + stack->item.pos = (_pos); \ + stack->item.str_byte = (_str_byte); \ + stack->item.state = (_state); \ + stack->item.state_id = (_state_id); \ + stack->item.next_c = (_next_c); \ + for (i = 0; i < tnfa->num_tags; i++) \ + stack->item.tags[i] = (_tags)[i]; \ + BT_STACK_MBSTATE_IN; \ + } \ + while (0) + +#define BT_STACK_POP() \ + do \ + { \ + int i; \ + assert(stack->prev); \ + pos = stack->item.pos; \ + str_byte = stack->item.str_byte; \ + state = stack->item.state; \ + next_c = stack->item.next_c; \ + for (i = 0; i < tnfa->num_tags; i++) \ + tags[i] = stack->item.tags[i]; \ + BT_STACK_MBSTATE_OUT; \ + stack = stack->prev; \ + } \ + while (0) + +#undef MIN +#define MIN(a, b) ((a) <= (b) ? (a) : (b)) + +static reg_errcode_t +tre_tnfa_run_backtrack(const tre_tnfa_t *tnfa, const void *string, + int len, int *match_tags, + int eflags, int *match_end_ofs) +{ + /* State variables required by GET_NEXT_WCHAR. */ + tre_char_t prev_c = 0, next_c = 0; + const char *str_byte = string; + int pos = 0; + int pos_add_next = 1; +#ifdef TRE_MBSTATE + mbstate_t mbstate; +#endif /* TRE_MBSTATE */ + int reg_notbol = eflags & REG_NOTBOL; + int reg_noteol = eflags & REG_NOTEOL; + int reg_newline = tnfa->cflags & REG_NEWLINE; + + /* These are used to remember the necessary values of the above + variables to return to the position where the current search + started from. */ + int next_c_start; + const char *str_byte_start; + int pos_start = -1; +#ifdef TRE_MBSTATE + mbstate_t mbstate_start; +#endif /* TRE_MBSTATE */ + + /* Compilation flags for this regexp. */ + int cflags = tnfa->cflags; + + /* End offset of best match so far, or -1 if no match found yet. */ + int match_eo = -1; + /* Tag arrays. */ + int *next_tags, *tags = NULL; + /* Current TNFA state. */ + tre_tnfa_transition_t *state; + int *states_seen = NULL; + + /* Memory allocator to for allocating the backtracking stack. */ + tre_mem_t mem = tre_bt_mem_new(); + + /* The backtracking stack. */ + tre_backtrack_t stack; + + tre_tnfa_transition_t *trans_i; + regmatch_t *pmatch = NULL; + int ret; + +#ifdef TRE_MBSTATE + memset(&mbstate, '\0', sizeof(mbstate)); +#endif /* TRE_MBSTATE */ + + if (!mem) + return REG_ESPACE; + stack = tre_bt_mem_alloc(mem, sizeof(*stack)); + if (!stack) + { + ret = REG_ESPACE; + goto error_exit; + } + stack->prev = NULL; + stack->next = NULL; + +#ifdef TRE_USE_ALLOCA + tags = alloca(sizeof(*tags) * tnfa->num_tags); + pmatch = alloca(sizeof(*pmatch) * tnfa->num_submatches); + states_seen = alloca(sizeof(*states_seen) * tnfa->num_states); +#else /* !TRE_USE_ALLOCA */ + tags = xmalloc(sizeof(*tags) * tnfa->num_tags); + if (!tags) + { + ret = REG_ESPACE; + goto error_exit; + } + pmatch = xmalloc(sizeof(*pmatch) * tnfa->num_submatches); + if (!pmatch) + { + ret = REG_ESPACE; + goto error_exit; + } + states_seen = xmalloc(sizeof(*states_seen) * tnfa->num_states); + if (!states_seen) + { + ret = REG_ESPACE; + goto error_exit; + } +#endif /* !TRE_USE_ALLOCA */ + + retry: + { + int i; + for (i = 0; i < tnfa->num_tags; i++) + { + tags[i] = -1; + if (match_tags) + match_tags[i] = -1; + } + for (i = 0; i < tnfa->num_states; i++) + states_seen[i] = 0; + } + + state = NULL; + pos = pos_start; + GET_NEXT_WCHAR(); + pos_start = pos; + next_c_start = next_c; + str_byte_start = str_byte; +#ifdef TRE_MBSTATE + mbstate_start = mbstate; +#endif /* TRE_MBSTATE */ + + /* Handle initial states. */ + next_tags = NULL; + for (trans_i = tnfa->initial; trans_i->state; trans_i++) + { + DPRINT(("> init %p, prev_c %lc\n", trans_i->state, (tre_cint_t)prev_c)); + if (trans_i->assertions && CHECK_ASSERTIONS(trans_i->assertions)) + { + DPRINT(("assert failed\n")); + continue; + } + if (state == NULL) + { + /* Start from this state. */ + state = trans_i->state; + next_tags = trans_i->tags; + } + else + { + /* Backtrack to this state. */ + DPRINT(("saving state %d for backtracking\n", trans_i->state_id)); + BT_STACK_PUSH(pos, str_byte, str_wide, trans_i->state, + trans_i->state_id, next_c, tags, mbstate); + { + int *tmp = trans_i->tags; + if (tmp) + while (*tmp >= 0) + stack->item.tags[*tmp++] = pos; + } + } + } + + if (next_tags) + for (; *next_tags >= 0; next_tags++) + tags[*next_tags] = pos; + + + DPRINT(("entering match loop, pos %d, str_byte %p\n", pos, str_byte)); + DPRINT(("pos:chr/code | state and tags\n")); + DPRINT(("-------------+------------------------------------------------\n")); + + if (state == NULL) + goto backtrack; + + while (1) + { + tre_tnfa_transition_t *trans_i, *next_state; + int empty_br_match; + + DPRINT(("start loop\n")); + if (state == tnfa->final) + { + DPRINT((" match found, %d %d\n", match_eo, pos)); + if (match_eo < pos + || (match_eo == pos + && match_tags + && tre_tag_order(tnfa->num_tags, tnfa->tag_directions, + tags, match_tags))) + { + int i; + /* This match wins the previous match. */ + DPRINT((" win previous\n")); + match_eo = pos; + if (match_tags) + for (i = 0; i < tnfa->num_tags; i++) + match_tags[i] = tags[i]; + } + /* Our TNFAs never have transitions leaving from the final state, + so we jump right to backtracking. */ + goto backtrack; + } + +#ifdef TRE_DEBUG + DPRINT(("%3d:%2lc/%05d | %p ", pos, (tre_cint_t)next_c, (int)next_c, + state)); + { + int i; + for (i = 0; i < tnfa->num_tags; i++) + DPRINT(("%d%s", tags[i], i < tnfa->num_tags - 1 ? ", " : "")); + DPRINT(("\n")); + } +#endif /* TRE_DEBUG */ + + /* Go to the next character in the input string. */ + empty_br_match = 0; + trans_i = state; + if (trans_i->state && trans_i->assertions & ASSERT_BACKREF) + { + /* This is a back reference state. All transitions leaving from + this state have the same back reference "assertion". Instead + of reading the next character, we match the back reference. */ + int so, eo, bt = trans_i->u.backref; + int bt_len; + int result; + + DPRINT((" should match back reference %d\n", bt)); + /* Get the substring we need to match against. Remember to + turn off REG_NOSUB temporarily. */ + tre_fill_pmatch(bt + 1, pmatch, tnfa->cflags & !REG_NOSUB, + tnfa, tags, pos); + so = pmatch[bt].rm_so; + eo = pmatch[bt].rm_eo; + bt_len = eo - so; + + if (len < 0) + { + result = strncmp((char*)string + so, str_byte - 1, bt_len); + } + else if (len - pos < bt_len) + result = 1; + else + result = memcmp((char*)string + so, str_byte - 1, bt_len); + + /* We can ignore multibyte characters here because the backref + string is already aligned at character boundaries. */ + if (result == 0) + { + /* Back reference matched. Check for infinite loop. */ + if (bt_len == 0) + empty_br_match = 1; + if (empty_br_match && states_seen[trans_i->state_id]) + { + DPRINT((" avoid loop\n")); + goto backtrack; + } + + states_seen[trans_i->state_id] = empty_br_match; + + /* Advance in input string and resync `prev_c', `next_c' + and pos. */ + DPRINT((" back reference matched\n")); + str_byte += bt_len - 1; + pos += bt_len - 1; + GET_NEXT_WCHAR(); + DPRINT((" pos now %d\n", pos)); + } + else + { + DPRINT((" back reference did not match\n")); + goto backtrack; + } + } + else + { + /* Check for end of string. */ + if (len < 0) + { + if (next_c == L'\0') + goto backtrack; + } + else + { + if (pos >= len) + goto backtrack; + } + + /* Read the next character. */ + GET_NEXT_WCHAR(); + } + + next_state = NULL; + for (trans_i = state; trans_i->state; trans_i++) + { + DPRINT((" transition %d-%d (%c-%c) %d to %d\n", + trans_i->code_min, trans_i->code_max, + trans_i->code_min, trans_i->code_max, + trans_i->assertions, trans_i->state_id)); + if (trans_i->code_min <= prev_c && trans_i->code_max >= prev_c) + { + if (trans_i->assertions + && (CHECK_ASSERTIONS(trans_i->assertions) + /* Handle character class transitions. */ + || ((trans_i->assertions & ASSERT_CHAR_CLASS) + && !(cflags & REG_ICASE) + && !tre_isctype((tre_cint_t)prev_c, trans_i->u.class)) + || ((trans_i->assertions & ASSERT_CHAR_CLASS) + && (cflags & REG_ICASE) + && (!tre_isctype(tre_tolower((tre_cint_t)prev_c), + trans_i->u.class) + && !tre_isctype(tre_toupper((tre_cint_t)prev_c), + trans_i->u.class))) + || ((trans_i->assertions & ASSERT_CHAR_CLASS_NEG) + && tre_neg_char_classes_match(trans_i->neg_classes, + (tre_cint_t)prev_c, + cflags & REG_ICASE)))) + { + DPRINT((" assertion failed\n")); + continue; + } + + if (next_state == NULL) + { + /* First matching transition. */ + DPRINT((" Next state is %d\n", trans_i->state_id)); + next_state = trans_i->state; + next_tags = trans_i->tags; + } + else + { + /* Second mathing transition. We may need to backtrack here + to take this transition instead of the first one, so we + push this transition in the backtracking stack so we can + jump back here if needed. */ + DPRINT((" saving state %d for backtracking\n", + trans_i->state_id)); + BT_STACK_PUSH(pos, str_byte, str_wide, trans_i->state, + trans_i->state_id, next_c, tags, mbstate); + { + int *tmp; + for (tmp = trans_i->tags; tmp && *tmp >= 0; tmp++) + stack->item.tags[*tmp] = pos; + } +#if 0 /* XXX - it's important not to look at all transitions here to keep + the stack small! */ + break; +#endif + } + } + } + + if (next_state != NULL) + { + /* Matching transitions were found. Take the first one. */ + state = next_state; + + /* Update the tag values. */ + if (next_tags) + while (*next_tags >= 0) + tags[*next_tags++] = pos; + } + else + { + backtrack: + /* A matching transition was not found. Try to backtrack. */ + if (stack->prev) + { + DPRINT((" backtracking\n")); + if (stack->item.state->assertions && ASSERT_BACKREF) + { + DPRINT((" states_seen[%d] = 0\n", + stack->item.state_id)); + states_seen[stack->item.state_id] = 0; + } + + BT_STACK_POP(); + } + else if (match_eo < 0) + { + /* Try starting from a later position in the input string. */ + /* Check for end of string. */ + if (len < 0) + { + if (next_c == L'\0') + { + DPRINT(("end of string.\n")); + break; + } + } + else + { + if (pos >= len) + { + DPRINT(("end of string.\n")); + break; + } + } + DPRINT(("restarting from next start position\n")); + next_c = next_c_start; +#ifdef TRE_MBSTATE + mbstate = mbstate_start; +#endif /* TRE_MBSTATE */ + str_byte = str_byte_start; + goto retry; + } + else + { + DPRINT(("finished\n")); + break; + } + } + } + + ret = match_eo >= 0 ? REG_OK : REG_NOMATCH; + *match_end_ofs = match_eo; + + error_exit: + tre_bt_mem_destroy(mem); +#ifndef TRE_USE_ALLOCA + if (tags) + xfree(tags); + if (pmatch) + xfree(pmatch); + if (states_seen) + xfree(states_seen); +#endif /* !TRE_USE_ALLOCA */ + + return ret; +} + + +/*********************************************************************** + from regexec.c +***********************************************************************/ + +/* Fills the POSIX.2 regmatch_t array according to the TNFA tag and match + endpoint values. */ +static void +tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags, + const tre_tnfa_t *tnfa, int *tags, int match_eo) +{ + tre_submatch_data_t *submatch_data; + unsigned int i, j; + int *parents; + + i = 0; + if (match_eo >= 0 && !(cflags & REG_NOSUB)) + { + /* Construct submatch offsets from the tags. */ + DPRINT(("end tag = t%d = %d\n", tnfa->end_tag, match_eo)); + submatch_data = tnfa->submatch_data; + while (i < tnfa->num_submatches && i < nmatch) + { + if (submatch_data[i].so_tag == tnfa->end_tag) + pmatch[i].rm_so = match_eo; + else + pmatch[i].rm_so = tags[submatch_data[i].so_tag]; + + if (submatch_data[i].eo_tag == tnfa->end_tag) + pmatch[i].rm_eo = match_eo; + else + pmatch[i].rm_eo = tags[submatch_data[i].eo_tag]; + + /* If either of the endpoints were not used, this submatch + was not part of the match. */ + if (pmatch[i].rm_so == -1 || pmatch[i].rm_eo == -1) + pmatch[i].rm_so = pmatch[i].rm_eo = -1; + + DPRINT(("pmatch[%d] = {t%d = %d, t%d = %d}\n", i, + submatch_data[i].so_tag, pmatch[i].rm_so, + submatch_data[i].eo_tag, pmatch[i].rm_eo)); + i++; + } + /* Reset all submatches that are not within all of their parent + submatches. */ + i = 0; + while (i < tnfa->num_submatches && i < nmatch) + { + if (pmatch[i].rm_eo == -1) + assert(pmatch[i].rm_so == -1); + assert(pmatch[i].rm_so <= pmatch[i].rm_eo); + + parents = submatch_data[i].parents; + if (parents != NULL) + for (j = 0; parents[j] >= 0; j++) + { + DPRINT(("pmatch[%d] parent %d\n", i, parents[j])); + if (pmatch[i].rm_so < pmatch[parents[j]].rm_so + || pmatch[i].rm_eo > pmatch[parents[j]].rm_eo) + pmatch[i].rm_so = pmatch[i].rm_eo = -1; + } + i++; + } + } + + while (i < nmatch) + { + pmatch[i].rm_so = -1; + pmatch[i].rm_eo = -1; + i++; + } +} + + +/* + Wrapper functions for POSIX compatible regexp matching. +*/ + +static int +tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len, + size_t nmatch, regmatch_t pmatch[], int eflags) +{ + reg_errcode_t status; + int *tags = NULL, eo; + if (tnfa->num_tags > 0 && nmatch > 0) + { +#ifdef TRE_USE_ALLOCA + tags = alloca(sizeof(*tags) * tnfa->num_tags); +#else /* !TRE_USE_ALLOCA */ + tags = xmalloc(sizeof(*tags) * tnfa->num_tags); +#endif /* !TRE_USE_ALLOCA */ + if (tags == NULL) + return REG_ESPACE; + } + + /* Dispatch to the appropriate matcher. */ + if (tnfa->have_backrefs) + { + /* The regex has back references, use the backtracking matcher. */ + status = tre_tnfa_run_backtrack(tnfa, string, len, tags, eflags, &eo); + } + else + { + /* Exact matching, no back references, use the parallel matcher. */ + status = tre_tnfa_run_parallel(tnfa, string, len, tags, eflags, &eo); + } + + if (status == REG_OK) + /* A match was found, so fill the submatch registers. */ + tre_fill_pmatch(nmatch, pmatch, tnfa->cflags, tnfa, tags, eo); +#ifndef TRE_USE_ALLOCA + if (tags) + xfree(tags); +#endif /* !TRE_USE_ALLOCA */ + return status; +} + +int +regexec(const regex_t *preg, const char *str, + size_t nmatch, regmatch_t pmatch[], int eflags) +{ + return tre_match((void *)preg->TRE_REGEX_T_FIELD, str, -1, + nmatch, pmatch, eflags); +} + +/* EOF */ diff --git a/05/musl-final/src/regex/tre-mem.c b/05/musl-final/src/regex/tre-mem.c new file mode 100644 index 0000000..d7bdd3d --- /dev/null +++ b/05/musl-final/src/regex/tre-mem.c @@ -0,0 +1,163 @@ +/* + tre-mem.c - TRE memory allocator + + Copyright (c) 2001-2006 Ville Laurikari + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/* + This memory allocator is for allocating small memory blocks efficiently + in terms of memory overhead and execution speed. The allocated blocks + cannot be freed individually, only all at once. There can be multiple + allocators, though. +*/ + +#include +#include + +#include "tre.h" + + +/* Returns a new memory allocator or NULL if out of memory. */ +tre_mem_t +tre_mem_new_impl(int provided, void *provided_block) +{ + tre_mem_t mem; + if (provided) + { + mem = provided_block; + memset(mem, 0, sizeof(*mem)); + } + else + mem = xcalloc(1, sizeof(*mem)); + if (mem == NULL) + return NULL; + return mem; +} + + +/* Frees the memory allocator and all memory allocated with it. */ +void +tre_mem_destroy(tre_mem_t mem) +{ + tre_list_t *tmp, *l = mem->blocks; + + while (l != NULL) + { + xfree(l->data); + tmp = l->next; + xfree(l); + l = tmp; + } + xfree(mem); +} + + +/* Allocates a block of `size' bytes from `mem'. Returns a pointer to the + allocated block or NULL if an underlying malloc() failed. */ +void * +tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, + int zero, size_t size) +{ + void *ptr; + + if (mem->failed) + { + DPRINT(("tre_mem_alloc: oops, called after failure?!\n")); + return NULL; + } + +#ifdef MALLOC_DEBUGGING + if (!provided) + { + ptr = xmalloc(1); + if (ptr == NULL) + { + DPRINT(("tre_mem_alloc: xmalloc forced failure\n")); + mem->failed = 1; + return NULL; + } + xfree(ptr); + } +#endif /* MALLOC_DEBUGGING */ + + if (mem->n < size) + { + /* We need more memory than is available in the current block. + Allocate a new block. */ + tre_list_t *l; + if (provided) + { + DPRINT(("tre_mem_alloc: using provided block\n")); + if (provided_block == NULL) + { + DPRINT(("tre_mem_alloc: provided block was NULL\n")); + mem->failed = 1; + return NULL; + } + mem->ptr = provided_block; + mem->n = TRE_MEM_BLOCK_SIZE; + } + else + { + int block_size; + if (size * 8 > TRE_MEM_BLOCK_SIZE) + block_size = size * 8; + else + block_size = TRE_MEM_BLOCK_SIZE; + DPRINT(("tre_mem_alloc: allocating new %d byte block\n", + block_size)); + l = xmalloc(sizeof(*l)); + if (l == NULL) + { + mem->failed = 1; + return NULL; + } + l->data = xmalloc(block_size); + if (l->data == NULL) + { + xfree(l); + mem->failed = 1; + return NULL; + } + l->next = NULL; + if (mem->current != NULL) + mem->current->next = l; + if (mem->blocks == NULL) + mem->blocks = l; + mem->current = l; + mem->ptr = l->data; + mem->n = block_size; + } + } + + /* Make sure the next pointer will be aligned. */ + size += ALIGN(mem->ptr + size, long); + + /* Allocate from current block. */ + ptr = mem->ptr; + mem->ptr += size; + mem->n -= size; + + /* Set to zero if needed. */ + if (zero) + memset(ptr, 0, size); + + return ptr; +} + +/* EOF */ diff --git a/05/musl-final/src/regex/tre.h b/05/musl-final/src/regex/tre.h new file mode 100644 index 0000000..bfd171f --- /dev/null +++ b/05/musl-final/src/regex/tre.h @@ -0,0 +1,269 @@ +/* + tre-internal.h - TRE internal definitions + + Copyright (c) 2001-2006 Ville Laurikari . + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +#include +#include +#include + +#define TRE_MULTIBYTE 1 +#undef TRE_MBSTATE +#define TRE_WCHAR 1 +#define TRE_USE_SYSTEM_WCTYPE 1 +#define HAVE_WCSTOMBS 1 +#define TRE_MB_CUR_MAX MB_CUR_MAX + +#define NDEBUG + +#define TRE_REGEX_T_FIELD __opaque +typedef int reg_errcode_t; + +typedef wchar_t tre_char_t; + + +#ifdef TRE_DEBUG +#include +#define DPRINT(msg) do {printf msg; fflush(stdout);} while(0) +#else /* !TRE_DEBUG */ +#define DPRINT(msg) do { } while(0) +#endif /* !TRE_DEBUG */ + +#define elementsof(x) ( sizeof(x) / sizeof(x[0]) ) + +#if 1 +int __mbtowc(wchar_t *, const char *); +#define tre_mbrtowc(pwc, s, n, ps) (__mbtowc((pwc), (s))) +#else +#define tre_mbrtowc(pwc, s, n, ps) (mbtowc((pwc), (s), (n))) +#endif + +/* Wide characters. */ +typedef wint_t tre_cint_t; +#define TRE_CHAR_MAX WCHAR_MAX + +#ifdef TRE_MULTIBYTE +#define TRE_MB_CUR_MAX MB_CUR_MAX +#else /* !TRE_MULTIBYTE */ +#define TRE_MB_CUR_MAX 1 +#endif /* !TRE_MULTIBYTE */ + +#define tre_isalnum iswalnum +#define tre_isalpha iswalpha +#define tre_isblank iswblank +#define tre_iscntrl iswcntrl +#define tre_isdigit iswdigit +#define tre_isgraph iswgraph +#define tre_islower iswlower +#define tre_isprint iswprint +#define tre_ispunct iswpunct +#define tre_isspace iswspace +#define tre_isupper iswupper +#define tre_isxdigit iswxdigit + +#define tre_tolower towlower +#define tre_toupper towupper +#define tre_strlen wcslen + +/* Use system provided iswctype() and wctype(). */ +typedef wctype_t tre_ctype_t; +#define tre_isctype iswctype +#define tre_ctype wctype + +/* Returns number of bytes to add to (char *)ptr to make it + properly aligned for the type. */ +#define ALIGN(ptr, type) \ + ((((long)ptr) % sizeof(type)) \ + ? (sizeof(type) - (((long)ptr) % sizeof(type))) \ + : 0) + +#undef MAX +#undef MIN +#define MAX(a, b) (((a) >= (b)) ? (a) : (b)) +#define MIN(a, b) (((a) <= (b)) ? (a) : (b)) + +/* Define STRF to the correct printf formatter for strings. */ +#define STRF "ls" + +/* TNFA transition type. A TNFA state is an array of transitions, + the terminator is a transition with NULL `state'. */ +typedef struct tnfa_transition tre_tnfa_transition_t; + +struct tnfa_transition { + /* Range of accepted characters. */ + tre_cint_t code_min; + tre_cint_t code_max; + /* Pointer to the destination state. */ + tre_tnfa_transition_t *state; + /* ID number of the destination state. */ + int state_id; + /* -1 terminated array of tags (or NULL). */ + int *tags; + /* Assertion bitmap. */ + int assertions; + /* Assertion parameters. */ + union { + /* Character class assertion. */ + tre_ctype_t class; + /* Back reference assertion. */ + int backref; + } u; + /* Negative character class assertions. */ + tre_ctype_t *neg_classes; +}; + + +/* Assertions. */ +#define ASSERT_AT_BOL 1 /* Beginning of line. */ +#define ASSERT_AT_EOL 2 /* End of line. */ +#define ASSERT_CHAR_CLASS 4 /* Character class in `class'. */ +#define ASSERT_CHAR_CLASS_NEG 8 /* Character classes in `neg_classes'. */ +#define ASSERT_AT_BOW 16 /* Beginning of word. */ +#define ASSERT_AT_EOW 32 /* End of word. */ +#define ASSERT_AT_WB 64 /* Word boundary. */ +#define ASSERT_AT_WB_NEG 128 /* Not a word boundary. */ +#define ASSERT_BACKREF 256 /* A back reference in `backref'. */ +#define ASSERT_LAST 256 + +/* Tag directions. */ +typedef enum { + TRE_TAG_MINIMIZE = 0, + TRE_TAG_MAXIMIZE = 1 +} tre_tag_direction_t; + +/* Instructions to compute submatch register values from tag values + after a successful match. */ +struct tre_submatch_data { + /* Tag that gives the value for rm_so (submatch start offset). */ + int so_tag; + /* Tag that gives the value for rm_eo (submatch end offset). */ + int eo_tag; + /* List of submatches this submatch is contained in. */ + int *parents; +}; + +typedef struct tre_submatch_data tre_submatch_data_t; + + +/* TNFA definition. */ +typedef struct tnfa tre_tnfa_t; + +struct tnfa { + tre_tnfa_transition_t *transitions; + unsigned int num_transitions; + tre_tnfa_transition_t *initial; + tre_tnfa_transition_t *final; + tre_submatch_data_t *submatch_data; + unsigned int num_submatches; + tre_tag_direction_t *tag_directions; + int num_tags; + int end_tag; + int num_states; + int cflags; + int have_backrefs; +}; + +#if 0 +static int +tre_compile(regex_t *preg, const tre_char_t *regex, size_t n, int cflags); + +static void +tre_free(regex_t *preg); + +static void +tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags, + const tre_tnfa_t *tnfa, int *tags, int match_eo); + +static reg_errcode_t +tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len, + tre_str_type_t type, int *match_tags, int eflags, + int *match_end_ofs); + +static reg_errcode_t +tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len, + tre_str_type_t type, int *match_tags, int eflags, + int *match_end_ofs); + +static reg_errcode_t +tre_tnfa_run_backtrack(const tre_tnfa_t *tnfa, const void *string, + int len, tre_str_type_t type, int *match_tags, + int eflags, int *match_end_ofs); +#endif + +/* from tre-mem.h: */ + +#define TRE_MEM_BLOCK_SIZE 1024 + +typedef struct tre_list { + void *data; + struct tre_list *next; +} tre_list_t; + +typedef struct tre_mem_struct { + tre_list_t *blocks; + tre_list_t *current; + char *ptr; + size_t n; + int failed; + void **provided; +} *tre_mem_t; + +#define tre_mem_new_impl __tre_mem_new_impl +#define tre_mem_alloc_impl __tre_mem_alloc_impl +#define tre_mem_destroy __tre_mem_destroy + +tre_mem_t tre_mem_new_impl(int provided, void *provided_block); +void *tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, + int zero, size_t size); + +/* Returns a new memory allocator or NULL if out of memory. */ +#define tre_mem_new() tre_mem_new_impl(0, NULL) + +/* Allocates a block of `size' bytes from `mem'. Returns a pointer to the + allocated block or NULL if an underlying malloc() failed. */ +#define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size) + +/* Allocates a block of `size' bytes from `mem'. Returns a pointer to the + allocated block or NULL if an underlying malloc() failed. The memory + is set to zero. */ +#define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size) + +#ifdef TRE_USE_ALLOCA +/* alloca() versions. Like above, but memory is allocated with alloca() + instead of malloc(). */ + +#define tre_mem_newa() \ + tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct))) + +#define tre_mem_alloca(mem, size) \ + ((mem)->n >= (size) \ + ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size)) \ + : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size))) +#endif /* TRE_USE_ALLOCA */ + + +/* Frees the memory allocator and all memory allocated with it. */ +void tre_mem_destroy(tre_mem_t mem); + +#define xmalloc malloc +#define xcalloc calloc +#define xfree free +#define xrealloc realloc + +/* EOF */ diff --git a/05/musl-final/src/select/poll.c b/05/musl-final/src/select/poll.c new file mode 100644 index 0000000..e92943e --- /dev/null +++ b/05/musl-final/src/select/poll.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +int poll(struct pollfd *fds, nfds_t n, int timeout) +{ + int r; + CANCELPT_BEGIN; + r = syscall3(__NR_poll, (long)fds, n, timeout); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/select/pselect.c b/05/musl-final/src/select/pselect.c new file mode 100644 index 0000000..795c5b0 --- /dev/null +++ b/05/musl-final/src/select/pselect.c @@ -0,0 +1,15 @@ +#include +#include "syscall.h" +#include "libc.h" + +int pselect(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, const struct timespec *ts, const sigset_t *mask) +{ + int r; + long data[2] = { (long)mask, 8 }; + struct timespec ts_tmp; + if (ts) ts_tmp = *ts; + CANCELPT_BEGIN; + r = syscall6(__NR_pselect6, n, (long)rfds, (long)wfds, (long)efds, ts ? (long)&ts_tmp : 0, (long)data); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/select/select.c b/05/musl-final/src/select/select.c new file mode 100644 index 0000000..b394663 --- /dev/null +++ b/05/musl-final/src/select/select.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +int select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv) +{ + int r; + CANCELPT_BEGIN; + r = syscall5(__NR_select, n, (long)rfds, (long)wfds, (long)efds, (long)tv); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/setjmp/i386/longjmp.s b/05/musl-final/src/setjmp/i386/longjmp.s new file mode 100644 index 0000000..c1a956c --- /dev/null +++ b/05/musl-final/src/setjmp/i386/longjmp.s @@ -0,0 +1,22 @@ +.global _longjmp +.global longjmp +.type _longjmp,%function +.type longjmp,%function +_longjmp: +longjmp: + movl 4(%esp),%edx + movl 8(%esp),%eax + testl %eax,%eax + jnz .L0 + incl %eax +.L0: + movl (%edx),%ebx + movl 4(%edx),%esi + movl 8(%edx),%edi + movl 12(%edx),%ebp + movl 16(%edx),%ecx + movl %ecx,%esp + movl 20(%edx),%ecx + jmp *%ecx +.size _longjmp,.-_longjmp +.size longjmp,.-longjmp diff --git a/05/musl-final/src/setjmp/i386/setjmp.s b/05/musl-final/src/setjmp/i386/setjmp.s new file mode 100644 index 0000000..6c078b1 --- /dev/null +++ b/05/musl-final/src/setjmp/i386/setjmp.s @@ -0,0 +1,23 @@ +.global __setjmp +.global _setjmp +.global setjmp +.type __setjmp,%function +.type _setjmp,%function +.type setjmp,%function +__setjmp: +_setjmp: +setjmp: + movl 4(%esp), %eax + movl %ebx, (%eax) + movl %esi, 4(%eax) + movl %edi, 8(%eax) + movl %ebp, 12(%eax) + leal 4(%esp), %ecx + movl %ecx, 16(%eax) + movl (%esp), %ecx + movl %ecx, 20(%eax) + xorl %eax, %eax + ret +.size __setjmp,.-__setjmp +.size _setjmp,.-_setjmp +.size setjmp,.-setjmp diff --git a/05/musl-final/src/setjmp/longjmp.c b/05/musl-final/src/setjmp/longjmp.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/setjmp/setjmp.c b/05/musl-final/src/setjmp/setjmp.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/setjmp/x86_64/longjmp.s b/05/musl-final/src/setjmp/x86_64/longjmp.s new file mode 100644 index 0000000..c63b0c9 --- /dev/null +++ b/05/musl-final/src/setjmp/x86_64/longjmp.s @@ -0,0 +1,24 @@ +/* Copyright 2011 Nicholas J. Kain, licensed GNU LGPL 2.1 or later */ +.global _longjmp +.global longjmp +.type _longjmp,%function +.type longjmp,%function +_longjmp: +longjmp: + mov %rsi,%rax /* val will be longjmp return */ + test %rax,%rax + jnz .L0 + inc %rax /* if val==0, val=1 per longjmp semantics */ +.L0: + movq (%rdi),%rbx /* rdi is the jmp_buf, restore regs from it */ + movq 8(%rdi),%rbp + movq 16(%rdi),%r12 + movq 24(%rdi),%r13 + movq 32(%rdi),%r14 + movq 40(%rdi),%r15 + movq 48(%rdi),%rdx /* this ends up being the stack pointer */ + mov %rdx,%rsp + movq 56(%rdi),%rdx /* this is the instruction pointer */ + jmp *%rdx /* goto saved address without altering rsp */ +.size _longjmp,.-_longjmp +.size longjmp,.-longjmp diff --git a/05/musl-final/src/setjmp/x86_64/setjmp.s b/05/musl-final/src/setjmp/x86_64/setjmp.s new file mode 100644 index 0000000..8f29fa8 --- /dev/null +++ b/05/musl-final/src/setjmp/x86_64/setjmp.s @@ -0,0 +1,25 @@ +/* Copyright 2011 Nicholas J. Kain, licensed GNU LGPL 2.1 or later */ +.global __setjmp +.global _setjmp +.global setjmp +.type __setjmp,%function +.type _setjmp,%function +.type setjmp,%function +__setjmp: +_setjmp: +setjmp: + mov %rbx,(%rdi) /* rdi is jmp_buf, move registers onto it */ + mov %rbp,8(%rdi) + mov %r12,16(%rdi) + mov %r13,24(%rdi) + mov %r14,32(%rdi) + mov %r15,40(%rdi) + leaq 8(%rsp),%rdx /* this is our rsp WITHOUT current ret addr */ + mov %rdx,48(%rdi) + movq (%rsp),%rdx /* save return addr ptr for new rip */ + mov %rdx,56(%rdi) + xor %rax,%rax /* always return 0 */ + ret +.size __setjmp,.-__setjmp +.size _setjmp,.-_setjmp +.size setjmp,.-setjmp diff --git a/05/musl-final/src/signal/getitimer.c b/05/musl-final/src/signal/getitimer.c new file mode 100644 index 0000000..222d113 --- /dev/null +++ b/05/musl-final/src/signal/getitimer.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" + +int getitimer(int which, struct itimerval *old) +{ + int ret; + long kold[4]; + + if (!(ret = syscall2(__NR_getitimer, which, (long)&kold))) + *old = (struct itimerval){ { kold[0], kold[1] }, { kold[2], kold[3] } }; + return ret; +} diff --git a/05/musl-final/src/signal/i386/restore.s b/05/musl-final/src/signal/i386/restore.s new file mode 100644 index 0000000..e0c4ab6 --- /dev/null +++ b/05/musl-final/src/signal/i386/restore.s @@ -0,0 +1,15 @@ +.global __restore +.type __restore,%function +__restore: + popl %eax + movl $119, %eax + int $0x80 +.size __restore,.-__restore + +.global __restore_rt +.type __restore_rt,%function +__restore_rt: + movl $173, %eax + int $0x80 +.size __restore_rt,.-__restore_rt + diff --git a/05/musl-final/src/signal/i386/sigsetjmp.s b/05/musl-final/src/signal/i386/sigsetjmp.s new file mode 100644 index 0000000..0e7eefb --- /dev/null +++ b/05/musl-final/src/signal/i386/sigsetjmp.s @@ -0,0 +1,13 @@ +.global sigsetjmp +sigsetjmp: + mov 4(%esp),%eax + mov 8(%esp),%ecx + mov %ecx,24(%eax) + jecxz 1f + add $28,%eax + push %eax + push $0 + push $2 + call sigprocmask + add $12,%esp +1: jmp setjmp diff --git a/05/musl-final/src/signal/kill.c b/05/musl-final/src/signal/kill.c new file mode 100644 index 0000000..cc4b51e --- /dev/null +++ b/05/musl-final/src/signal/kill.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int kill(pid_t pid, int sig) +{ + return syscall2(__NR_kill, pid, sig); +} diff --git a/05/musl-final/src/signal/killpg.c b/05/musl-final/src/signal/killpg.c new file mode 100644 index 0000000..315ed44 --- /dev/null +++ b/05/musl-final/src/signal/killpg.c @@ -0,0 +1,11 @@ +#include +#include + +int killpg(pid_t pgid, int sig) +{ + if (pgid < 0) { + errno = EINVAL; + return -1; + } + return kill(-pgid, sig); +} diff --git a/05/musl-final/src/signal/raise.c b/05/musl-final/src/signal/raise.c new file mode 100644 index 0000000..52f8b42 --- /dev/null +++ b/05/musl-final/src/signal/raise.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int raise(int sig) +{ + return __syscall_kill(__syscall_getpid(), sig); +} diff --git a/05/musl-final/src/signal/restore.c b/05/musl-final/src/signal/restore.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/signal/setitimer.c b/05/musl-final/src/signal/setitimer.c new file mode 100644 index 0000000..cacab03 --- /dev/null +++ b/05/musl-final/src/signal/setitimer.c @@ -0,0 +1,15 @@ +#include +#include "syscall.h" + +int setitimer(int which, const struct itimerval *new, struct itimerval *old) +{ + int ret; + long knew[4] = { + new->it_interval.tv_sec, new->it_interval.tv_usec, + new->it_value.tv_sec, new->it_value.tv_usec + }, kold[4]; + + if (!(ret = syscall3(__NR_setitimer, which, (long)&knew, old ? (long)&kold : 0)) && old) + *old = (struct itimerval){ { kold[0], kold[1] }, { kold[2], kold[3] } }; + return ret; +} diff --git a/05/musl-final/src/signal/sigaction.c b/05/musl-final/src/signal/sigaction.c new file mode 100644 index 0000000..1c35e34 --- /dev/null +++ b/05/musl-final/src/signal/sigaction.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include "syscall.h" +#include "pthread_impl.h" + +void __restore(), __restore_rt(); + +int __libc_sigaction(int sig, const struct sigaction *sa, struct sigaction *old) +{ + struct { + void *handler; + unsigned long flags; + void (*restorer)(void); + sigset_t mask; + } ksa, kold; + long pksa=0, pkold=0; + if (sa) { + ksa.handler = sa->sa_handler; + ksa.flags = sa->sa_flags | SA_RESTORER; + ksa.restorer = (sa->sa_flags & SA_SIGINFO) ? __restore_rt : __restore; + ksa.mask = sa->sa_mask; + pksa = (long)&ksa; + } + if (old) pkold = (long)&kold; + if (syscall4(__NR_rt_sigaction, sig, pksa, pkold, 8)) + return -1; + if (old) { + old->sa_handler = kold.handler; + old->sa_flags = kold.flags; + old->sa_mask = kold.mask; + } + return 0; +} + +int __sigaction(int sig, const struct sigaction *sa, struct sigaction *old) +{ + if (sig == SIGCANCEL || sig == SIGSYSCALL) { + errno = EINVAL; + return -1; + } + return __libc_sigaction(sig, sa, old); +} + +int sigaction(int sig, const struct sigaction *sa, struct sigaction *old) +{ + if (sig == SIGCANCEL || sig == SIGSYSCALL) { + errno = EINVAL; + return -1; + } + return __libc_sigaction(sig, sa, old); +} + diff --git a/05/musl-final/src/signal/sigaddset.c b/05/musl-final/src/signal/sigaddset.c new file mode 100644 index 0000000..23e655d --- /dev/null +++ b/05/musl-final/src/signal/sigaddset.c @@ -0,0 +1,13 @@ +#include +#include + +int sigaddset(sigset_t *set, int sig) +{ + unsigned s = sig-1; + if (s >= 8*sizeof(sigset_t)) { + errno = EINVAL; + return -1; + } + set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1); + return 0; +} diff --git a/05/musl-final/src/signal/sigaltstack.c b/05/musl-final/src/signal/sigaltstack.c new file mode 100644 index 0000000..3cc2d45 --- /dev/null +++ b/05/musl-final/src/signal/sigaltstack.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" + +int sigaltstack(const stack_t *ss, stack_t *old) +{ + /* depends on kernel struct matching */ + return syscall2(__NR_sigaltstack, (long)ss, (long)old); +} diff --git a/05/musl-final/src/signal/sigdelset.c b/05/musl-final/src/signal/sigdelset.c new file mode 100644 index 0000000..14042fb --- /dev/null +++ b/05/musl-final/src/signal/sigdelset.c @@ -0,0 +1,13 @@ +#include +#include + +int sigdelset(sigset_t *set, int sig) +{ + unsigned s = sig-1; + if (s >= 8*sizeof(sigset_t)) { + errno = EINVAL; + return -1; + } + set->__bits[s/8/sizeof *set->__bits] &=~(1UL<<(s&8*sizeof *set->__bits-1)); + return 0; +} diff --git a/05/musl-final/src/signal/sigemptyset.c b/05/musl-final/src/signal/sigemptyset.c new file mode 100644 index 0000000..91f77ad --- /dev/null +++ b/05/musl-final/src/signal/sigemptyset.c @@ -0,0 +1,8 @@ +#include +#include + +int sigemptyset(sigset_t *set) +{ + memset(set, 0, sizeof *set); + return 0; +} diff --git a/05/musl-final/src/signal/sigfillset.c b/05/musl-final/src/signal/sigfillset.c new file mode 100644 index 0000000..fab50a5 --- /dev/null +++ b/05/musl-final/src/signal/sigfillset.c @@ -0,0 +1,8 @@ +#include +#include + +int sigfillset(sigset_t *set) +{ + memset(set, -1, sizeof *set); + return 0; +} diff --git a/05/musl-final/src/signal/sighold.c b/05/musl-final/src/signal/sighold.c new file mode 100644 index 0000000..5b0f6b1 --- /dev/null +++ b/05/musl-final/src/signal/sighold.c @@ -0,0 +1,11 @@ +#include +#include + +int sighold(int sig) +{ + sigset_t mask; + + sigemptyset(&mask); + if (sigaddset(&mask, sig) < 0) return -1; + return sigprocmask(SIG_BLOCK, &mask, NULL); +} diff --git a/05/musl-final/src/signal/sigignore.c b/05/musl-final/src/signal/sigignore.c new file mode 100644 index 0000000..98dff61 --- /dev/null +++ b/05/musl-final/src/signal/sigignore.c @@ -0,0 +1,12 @@ +#include +#include + +int sigignore(int sig) +{ + struct sigaction sa; + + sigemptyset(&sa.sa_mask); + sa.sa_handler = SIG_IGN; + sa.sa_flags = 0; + return sigaction(sig, &sa, NULL); +} diff --git a/05/musl-final/src/signal/siginterrupt.c b/05/musl-final/src/signal/siginterrupt.c new file mode 100644 index 0000000..60b3405 --- /dev/null +++ b/05/musl-final/src/signal/siginterrupt.c @@ -0,0 +1,13 @@ +#include +#include + +int siginterrupt(int sig, int flag) +{ + struct sigaction sa; + + sigaction(sig, NULL, &sa); + if (flag) sa.sa_flags &= ~SA_RESTART; + else sa.sa_flags |= SA_RESTART; + + return sigaction(sig, &sa, NULL); +} diff --git a/05/musl-final/src/signal/sigismember.c b/05/musl-final/src/signal/sigismember.c new file mode 100644 index 0000000..afd29e5 --- /dev/null +++ b/05/musl-final/src/signal/sigismember.c @@ -0,0 +1,12 @@ +#include +#include + +int sigismember(const sigset_t *set, int sig) +{ + unsigned s = sig-1; + if (s >= 8*sizeof(sigset_t)) { + errno = EINVAL; + return -1; + } + return !!(set->__bits[s/8/sizeof *set->__bits] & 1UL<<(s&8*sizeof *set->__bits-1)); +} diff --git a/05/musl-final/src/signal/siglongjmp.c b/05/musl-final/src/signal/siglongjmp.c new file mode 100644 index 0000000..33ac30e --- /dev/null +++ b/05/musl-final/src/signal/siglongjmp.c @@ -0,0 +1,12 @@ +#include +#include +#include + +void siglongjmp(sigjmp_buf buf, int ret) +{ + long *flag = buf + sizeof(jmp_buf)/sizeof(long); + sigset_t *mask = (void *)(flag + 1); + if (*flag) + sigprocmask (SIG_SETMASK, mask, NULL); + longjmp((void *)buf, ret); +} diff --git a/05/musl-final/src/signal/signal.c b/05/musl-final/src/signal/signal.c new file mode 100644 index 0000000..07346a8 --- /dev/null +++ b/05/musl-final/src/signal/signal.c @@ -0,0 +1,16 @@ +#include +#include +#include "syscall.h" +#include "libc.h" + +int __sigaction(int, const struct sigaction *, struct sigaction *); + +void (*signal(int sig, void (*func)(int)))(int) +{ + struct sigaction sa = { .sa_handler = func, .sa_flags = SA_RESTART }; + if (__sigaction(sig, &sa, &sa) < 0) + return SIG_ERR; + return sa.sa_handler; +} + +weak_alias(signal, bsd_signal); diff --git a/05/musl-final/src/signal/sigpause.c b/05/musl-final/src/signal/sigpause.c new file mode 100644 index 0000000..263c00f --- /dev/null +++ b/05/musl-final/src/signal/sigpause.c @@ -0,0 +1,11 @@ +#include +#include + +int sigpause(int sig) +{ + sigset_t mask; + + if (sigprocmask(0, NULL, &mask) < 0 || sigdelset(&mask, sig) < 0) + return -1; + return sigsuspend(&mask); +} diff --git a/05/musl-final/src/signal/sigpending.c b/05/musl-final/src/signal/sigpending.c new file mode 100644 index 0000000..7deda25 --- /dev/null +++ b/05/musl-final/src/signal/sigpending.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int sigpending(sigset_t *set) +{ + return syscall2(__NR_rt_sigpending, (long)set, SYSCALL_SIGSET_SIZE); +} diff --git a/05/musl-final/src/signal/sigprocmask.c b/05/musl-final/src/signal/sigprocmask.c new file mode 100644 index 0000000..a8a4a87 --- /dev/null +++ b/05/musl-final/src/signal/sigprocmask.c @@ -0,0 +1,44 @@ +#include +#include "syscall.h" +#include "libc.h" + +int __libc_sigprocmask(int how, const sigset_t *set, sigset_t *old) +{ + return syscall4(__NR_rt_sigprocmask, how, (long)set, (long)old, 8); +} + +int __sigprocmask(int how, const sigset_t *set, sigset_t *old) +{ + sigset_t tmp; + /* Quickly mask out bits 32 and 33 (thread control signals) */ + if (0 && how != SIG_UNBLOCK && (set->__bits[4/sizeof *set->__bits] & 3UL<<(32&8*sizeof *set->__bits-1))) { + tmp = *set; + set = &tmp; + tmp.__bits[4/sizeof *set->__bits] &= ~(3UL<<(32&8*sizeof *set->__bits-1)); + } + return __libc_sigprocmask(how, set, old); +} + +int sigprocmask(int how, const sigset_t *set, sigset_t *old) +{ + sigset_t tmp; + /* Quickly mask out bits 32 and 33 (thread control signals) */ + if (0 && how != SIG_UNBLOCK && (set->__bits[4/sizeof *set->__bits] & 3UL<<(32&8*sizeof *set->__bits-1))) { + tmp = *set; + set = &tmp; + tmp.__bits[4/sizeof *set->__bits] &= ~(3UL<<(32&8*sizeof *set->__bits-1)); + } + return __libc_sigprocmask(how, set, old); +} + +int pthread_sigmask(int how, const sigset_t *set, sigset_t *old) +{ + sigset_t tmp; + /* Quickly mask out bits 32 and 33 (thread control signals) */ + if (0 && how != SIG_UNBLOCK && (set->__bits[4/sizeof *set->__bits] & 3UL<<(32&8*sizeof *set->__bits-1))) { + tmp = *set; + set = &tmp; + tmp.__bits[4/sizeof *set->__bits] &= ~(3UL<<(32&8*sizeof *set->__bits-1)); + } + return __libc_sigprocmask(how, set, old); +} diff --git a/05/musl-final/src/signal/sigqueue.c b/05/musl-final/src/signal/sigqueue.c new file mode 100644 index 0000000..ce3abf6 --- /dev/null +++ b/05/musl-final/src/signal/sigqueue.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include "syscall.h" + +int sigqueue(pid_t pid, int sig, const union sigval value) +{ + siginfo_t si = { + .si_signo = sig, + .si_code = -1, + .si_value = value, + }; + return syscall3(__NR_rt_sigqueueinfo, pid, sig, (long)&si); +} diff --git a/05/musl-final/src/signal/sigrelse.c b/05/musl-final/src/signal/sigrelse.c new file mode 100644 index 0000000..b0b3024 --- /dev/null +++ b/05/musl-final/src/signal/sigrelse.c @@ -0,0 +1,11 @@ +#include +#include + +int sigrelse(int sig) +{ + sigset_t mask; + + sigemptyset(&mask); + if (sigaddset(&mask, sig) < 0) return -1; + return sigprocmask(SIG_UNBLOCK, &mask, NULL); +} diff --git a/05/musl-final/src/signal/sigrtmax.c b/05/musl-final/src/signal/sigrtmax.c new file mode 100644 index 0000000..0ef2987 --- /dev/null +++ b/05/musl-final/src/signal/sigrtmax.c @@ -0,0 +1,4 @@ +int __libc_current_sigrtmax() +{ + return 64; +} diff --git a/05/musl-final/src/signal/sigrtmin.c b/05/musl-final/src/signal/sigrtmin.c new file mode 100644 index 0000000..7ad06d2 --- /dev/null +++ b/05/musl-final/src/signal/sigrtmin.c @@ -0,0 +1,4 @@ +int __libc_current_sigrtmin() +{ + return 34; +} diff --git a/05/musl-final/src/signal/sigset.c b/05/musl-final/src/signal/sigset.c new file mode 100644 index 0000000..1b6b38f --- /dev/null +++ b/05/musl-final/src/signal/sigset.c @@ -0,0 +1,28 @@ +#include +#include + +void (*sigset(int sig, void (*handler)(int)))(int) +{ + struct sigaction sa, sa_old; + sigset_t mask; + + sigemptyset(&mask); + if (sigaddset(&mask, sig) < 0) + return SIG_ERR; + + if (handler == SIG_HOLD) { + if (sigaction(sig, NULL, &sa_old) < 0) + return SIG_ERR; + if (sigprocmask(SIG_BLOCK, &mask, &mask) < 0) + return SIG_ERR; + } else { + sa.sa_handler = handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + if (sigaction(sig, &sa, &sa_old) < 0) + return SIG_ERR; + if (sigprocmask(SIG_UNBLOCK, &mask, &mask) < 0) + return SIG_ERR; + } + return sigismember(&mask, sig) ? SIG_HOLD : sa_old.sa_handler; +} diff --git a/05/musl-final/src/signal/sigsetjmp.c b/05/musl-final/src/signal/sigsetjmp.c new file mode 100644 index 0000000..a6667a2 --- /dev/null +++ b/05/musl-final/src/signal/sigsetjmp.c @@ -0,0 +1,17 @@ +#include +#include +#include + +/* !!! This function will not work unless the compiler performs + * tail call optimization. Machine-specific asm versions should + * be created instead even though the workaround (tail call) + * is entirely non-machine-specific... */ + +int sigsetjmp(sigjmp_buf buf, int save) +{ + long *flag = buf + sizeof(jmp_buf)/sizeof(long); + sigset_t *mask = (void *)(flag + 1); + if ((*flag = save)) + sigprocmask (SIG_SETMASK, NULL, mask); + return setjmp((void *)buf); +} diff --git a/05/musl-final/src/signal/sigsuspend.c b/05/musl-final/src/signal/sigsuspend.c new file mode 100644 index 0000000..1acdab0 --- /dev/null +++ b/05/musl-final/src/signal/sigsuspend.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int sigsuspend(const sigset_t *mask) +{ + return syscall2(__NR_rt_sigsuspend, (long)mask, sizeof(sigset_t)); +} diff --git a/05/musl-final/src/signal/sigtimedwait.c b/05/musl-final/src/signal/sigtimedwait.c new file mode 100644 index 0000000..155185d --- /dev/null +++ b/05/musl-final/src/signal/sigtimedwait.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" + +int sigtimedwait(const sigset_t *mask, siginfo_t *si, const struct timespec *timeout) +{ + long k_timeout[2]; + if (timeout) { + k_timeout[0] = timeout->tv_sec; + k_timeout[1] = timeout->tv_nsec; + } + return syscall4(__NR_rt_sigtimedwait, (long)mask, (long)si, timeout ? (long)k_timeout : 0, SYSCALL_SIGSET_SIZE); +} diff --git a/05/musl-final/src/signal/sigwait.c b/05/musl-final/src/signal/sigwait.c new file mode 100644 index 0000000..9569d6b --- /dev/null +++ b/05/musl-final/src/signal/sigwait.c @@ -0,0 +1,11 @@ +#include +#include + +int sigwait(const sigset_t *mask, int *sig) +{ + siginfo_t si; + if (sigtimedwait(mask, &si, NULL) < 0) + return -1; + *sig = si.si_signo; + return 0; +} diff --git a/05/musl-final/src/signal/sigwaitinfo.c b/05/musl-final/src/signal/sigwaitinfo.c new file mode 100644 index 0000000..e79feb9 --- /dev/null +++ b/05/musl-final/src/signal/sigwaitinfo.c @@ -0,0 +1,7 @@ +#include +#include + +int sigwaitinfo(const sigset_t *mask, siginfo_t *si) +{ + return sigtimedwait(mask, si, NULL); +} diff --git a/05/musl-final/src/signal/x86_64/restore.s b/05/musl-final/src/signal/x86_64/restore.s new file mode 100644 index 0000000..bc5e0d7 --- /dev/null +++ b/05/musl-final/src/signal/x86_64/restore.s @@ -0,0 +1,11 @@ +.global __restore_rt +.global __restore +.type __restore_rt,%function +.type __restore,%function +__restore_rt: +__restore: + movl $15, %eax + syscall +.size __restore_rt,.-__restore_rt +.size __restore,.-__restore + diff --git a/05/musl-final/src/signal/x86_64/sigsetjmp.s b/05/musl-final/src/signal/x86_64/sigsetjmp.s new file mode 100644 index 0000000..0a45078 --- /dev/null +++ b/05/musl-final/src/signal/x86_64/sigsetjmp.s @@ -0,0 +1,11 @@ +/* Copyright 2011 Nicholas J. Kain, licensed GNU LGPL 2.1 or later */ +.global sigsetjmp +sigsetjmp: + test %rsi,%rsi + jz 1f /* if save == 0, just goto setjmp */ + movq %rsi,64(%rdi) /* move save -> jmp_buf[8] */ + addq $72,%rdi /* add sizeof(jmp_buf) to rdi */ + movl $0,%esi /* arg2 = 0 */ + movl $2,%edx /* arg3 = 2 */ + call sigprocmask /* sigprocmask(jmp_buf, 0, 2) */ +1: jmp setjmp diff --git a/05/musl-final/src/stat/chmod.c b/05/musl-final/src/stat/chmod.c new file mode 100644 index 0000000..cb310fe --- /dev/null +++ b/05/musl-final/src/stat/chmod.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int chmod(const char *path, mode_t mode) +{ + return syscall2(__NR_chmod, (long)path, mode); +} diff --git a/05/musl-final/src/stat/fchmod.c b/05/musl-final/src/stat/fchmod.c new file mode 100644 index 0000000..9189738 --- /dev/null +++ b/05/musl-final/src/stat/fchmod.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int fchmod(int fd, mode_t mode) +{ + return syscall2(__NR_fchmod, fd, mode); +} diff --git a/05/musl-final/src/stat/fchmodat.c b/05/musl-final/src/stat/fchmodat.c new file mode 100644 index 0000000..f4f22b2 --- /dev/null +++ b/05/musl-final/src/stat/fchmodat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int fchmodat(int fd, const char *path, mode_t mode, int flag) +{ + return syscall4(__NR_fchmodat, fd, (long)path, mode, flag); +} diff --git a/05/musl-final/src/stat/fstat.c b/05/musl-final/src/stat/fstat.c new file mode 100644 index 0000000..88ac6f3 --- /dev/null +++ b/05/musl-final/src/stat/fstat.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int fstat(int fd, struct stat *buf) +{ + return syscall2(__NR_fstat, fd, (long)buf); +} + +LFS64(fstat); diff --git a/05/musl-final/src/stat/fstatat.c b/05/musl-final/src/stat/fstatat.c new file mode 100644 index 0000000..e39f833 --- /dev/null +++ b/05/musl-final/src/stat/fstatat.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int fstatat(int fd, const char *path, struct stat *buf, int flag) +{ + return syscall4(__NR_fstatat, fd, (long)path, (long)buf, flag); +} + +LFS64(fstatat); diff --git a/05/musl-final/src/stat/fstatvfs.c b/05/musl-final/src/stat/fstatvfs.c new file mode 100644 index 0000000..4a8bfe2 --- /dev/null +++ b/05/musl-final/src/stat/fstatvfs.c @@ -0,0 +1,13 @@ +#include +#include "syscall.h" +#include "libc.h" + +int fstatvfs(int fd, struct statvfs *buf) +{ + return syscall2(__NR_fstatfs, fd, (long)buf); +} + +weak_alias(fstatvfs, fstatfs); + +LFS64(fstatvfs); +LFS64(fstatfs); diff --git a/05/musl-final/src/stat/lstat.c b/05/musl-final/src/stat/lstat.c new file mode 100644 index 0000000..3b22e62 --- /dev/null +++ b/05/musl-final/src/stat/lstat.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int lstat(const char *path, struct stat *buf) +{ + return syscall2(__NR_lstat, (long)path, (long)buf); +} + +LFS64(lstat); diff --git a/05/musl-final/src/stat/mkdir.c b/05/musl-final/src/stat/mkdir.c new file mode 100644 index 0000000..8295cad --- /dev/null +++ b/05/musl-final/src/stat/mkdir.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mkdir(const char *path, mode_t mode) +{ + return syscall2(__NR_mkdir, (long)path, mode); +} diff --git a/05/musl-final/src/stat/mkdirat.c b/05/musl-final/src/stat/mkdirat.c new file mode 100644 index 0000000..1fb3825 --- /dev/null +++ b/05/musl-final/src/stat/mkdirat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mkdirat(int fd, const char *path, mode_t mode) +{ + return syscall3(__NR_mkdirat, fd, (long)path, mode); +} diff --git a/05/musl-final/src/stat/mkfifo.c b/05/musl-final/src/stat/mkfifo.c new file mode 100644 index 0000000..60efcf7 --- /dev/null +++ b/05/musl-final/src/stat/mkfifo.c @@ -0,0 +1,6 @@ +#include + +int mkfifo(const char *path, mode_t mode) +{ + return mknod(path, mode | S_IFIFO, 0); +} diff --git a/05/musl-final/src/stat/mkfifoat.c b/05/musl-final/src/stat/mkfifoat.c new file mode 100644 index 0000000..d3a1f97 --- /dev/null +++ b/05/musl-final/src/stat/mkfifoat.c @@ -0,0 +1,6 @@ +#include + +int mkfifoat(int fd, const char *path, mode_t mode) +{ + return mknodat(fd, path, mode | S_IFIFO, 0); +} diff --git a/05/musl-final/src/stat/mknod.c b/05/musl-final/src/stat/mknod.c new file mode 100644 index 0000000..0123eee --- /dev/null +++ b/05/musl-final/src/stat/mknod.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" + +int mknod(const char *path, mode_t mode, dev_t dev) +{ + /* since dev_t is system-specific anyway we defer to the idiotic + * legacy-compatible bitfield mapping of the type.. at least we've + * made it large enough to leave space for future expansion.. */ + return syscall3(__NR_mknod, (long)path, mode, dev & 0xffff); +} diff --git a/05/musl-final/src/stat/mknodat.c b/05/musl-final/src/stat/mknodat.c new file mode 100644 index 0000000..b5687e4 --- /dev/null +++ b/05/musl-final/src/stat/mknodat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int mknodat(int fd, const char *path, mode_t mode, dev_t dev) +{ + return syscall4(__NR_mknodat, fd, (long)path, mode, dev & 0xffff); +} diff --git a/05/musl-final/src/stat/stat.c b/05/musl-final/src/stat/stat.c new file mode 100644 index 0000000..9847552 --- /dev/null +++ b/05/musl-final/src/stat/stat.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int stat(const char *path, struct stat *buf) +{ + return syscall2(__NR_stat, (long)path, (long)buf); +} + +LFS64(stat); diff --git a/05/musl-final/src/stat/statvfs.c b/05/musl-final/src/stat/statvfs.c new file mode 100644 index 0000000..ebf14b4 --- /dev/null +++ b/05/musl-final/src/stat/statvfs.c @@ -0,0 +1,13 @@ +#include +#include "syscall.h" +#include "libc.h" + +int statvfs(const char *path, struct statvfs *buf) +{ + return syscall2(__NR_statfs, (long)path, (long)buf); +} + +weak_alias(statvfs, statfs); + +LFS64(statvfs); +LFS64(statfs); diff --git a/05/musl-final/src/stat/umask.c b/05/musl-final/src/stat/umask.c new file mode 100644 index 0000000..49cb48a --- /dev/null +++ b/05/musl-final/src/stat/umask.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +mode_t umask(mode_t mode) +{ + return syscall1(__NR_umask, mode); +} diff --git a/05/musl-final/src/stdio/__fclose_ca.c b/05/musl-final/src/stdio/__fclose_ca.c new file mode 100644 index 0000000..e0b12a1 --- /dev/null +++ b/05/musl-final/src/stdio/__fclose_ca.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +int __fclose_ca(FILE *f) +{ + return f->close(f); +} diff --git a/05/musl-final/src/stdio/__fdopen.c b/05/musl-final/src/stdio/__fdopen.c new file mode 100644 index 0000000..6ad7c57 --- /dev/null +++ b/05/musl-final/src/stdio/__fdopen.c @@ -0,0 +1,52 @@ +#include "stdio_impl.h" + +FILE *__fdopen(int fd, const char *mode) +{ + FILE *f; + struct termios tio; + int plus = !!strchr(mode, '+'); + + /* Check for valid initial mode character */ + if (!strchr("rwa", *mode)) return 0; + + /* Allocate FILE+buffer or fail */ + if (!(f=malloc(sizeof *f + UNGET + BUFSIZ))) return 0; + + /* Zero-fill only the struct, not the buffer */ + memset(f, 0, sizeof *f); + + /* Impose mode restrictions */ + if (!plus) f->flags = (*mode == 'r') ? F_NOWR : F_NORD; + + /* Set append mode on fd if opened for append */ + if (*mode == 'a') { + int flags = __syscall_fcntl(fd, F_GETFL, 0); + __syscall_fcntl(fd, F_SETFL, flags | O_APPEND); + } + + f->fd = fd; + f->buf = (unsigned char *)f + sizeof *f + UNGET; + f->buf_size = BUFSIZ; + + /* Activate line buffered mode for terminals */ + f->lbf = EOF; + if (!(f->flags & F_NOWR) && !__syscall_ioctl(fd, TCGETS, &tio)) + f->lbf = '\n'; + + /* Initialize op ptrs. No problem if some are unneeded. */ + f->read = __stdio_read; + f->write = __stdio_write; + f->seek = __stdio_seek; + f->close = __stdio_close; + + /* Add new FILE to open file list */ + OFLLOCK(); + f->next = ofl_head; + if (ofl_head) ofl_head->prev = f; + ofl_head = f; + OFLUNLOCK(); + + return f; +} + +weak_alias(__fdopen, fdopen); diff --git a/05/musl-final/src/stdio/__fopen_rb_ca.c b/05/musl-final/src/stdio/__fopen_rb_ca.c new file mode 100644 index 0000000..57d9b73 --- /dev/null +++ b/05/musl-final/src/stdio/__fopen_rb_ca.c @@ -0,0 +1,18 @@ +#include "stdio_impl.h" + +FILE *__fopen_rb_ca(const char *filename, FILE *f, unsigned char *buf, size_t len) +{ + memset(f, 0, sizeof *f); + + f->fd = __syscall_open(filename, O_RDONLY, 0); + if (f->fd < 0) return 0; + + f->flags = F_NOWR | F_PERM; + f->buf = buf + UNGET; + f->buf_size = len - UNGET; + f->read = __stdio_read; + f->seek = __stdio_seek; + f->close = __stdio_close; + + return f; +} diff --git a/05/musl-final/src/stdio/__fpending.c b/05/musl-final/src/stdio/__fpending.c new file mode 100644 index 0000000..a4334e2 --- /dev/null +++ b/05/musl-final/src/stdio/__fpending.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +size_t __fpending(FILE *f) +{ + return f->wend ? f->wpos - f->wbase : 0; +} diff --git a/05/musl-final/src/stdio/__ofl.c b/05/musl-final/src/stdio/__ofl.c new file mode 100644 index 0000000..7d9652c --- /dev/null +++ b/05/musl-final/src/stdio/__ofl.c @@ -0,0 +1,3 @@ +#include "stdio_impl.h" + +struct ofl __ofl; diff --git a/05/musl-final/src/stdio/__overflow.c b/05/musl-final/src/stdio/__overflow.c new file mode 100644 index 0000000..e35104d --- /dev/null +++ b/05/musl-final/src/stdio/__overflow.c @@ -0,0 +1,52 @@ +#include "stdio_impl.h" + +static int overflow(FILE *f, int c) +{ + /* Initialize if we're not already writing */ + if (!f->wend) { + /* Fail if we're in error state or unwritable. */ + if (f->flags & (F_ERR|F_NOWR)) return EOF; + + /* Set byte orientation -1,0=>-1; 1=>1 */ + f->mode |= f->mode-1; + + /* Clear read buffer (easier than summoning nasal demons) */ + f->rpos = f->rend = f->rstop = 0; + + /* Activate write through the buffer */ + f->wpos = f->wbase = f->buf; + f->wend = f->buf + f->buf_size; + f->wstop = (f->lbf < 0) ? f->wend - 1 : 0; + } + + /* Buffer can always hold at least 1 byte... */ + if (c != EOF) { + *f->wpos++ = c; + if (f->wpos <= f->wstop && c != f->lbf) return c; + } + /* ...since if the next call fails, buffer is empty. */ + if (f->write(f, f->wbase, f->wpos - f->wbase) < 0) { + f->flags |= F_ERR; + f->wpos = f->wbase = f->wend = f->wstop = 0; + return EOF; + } + + /* Buffer is empty so reset position to beginning */ + f->wpos = f->wbase; + + return c; +} + +int __overflow(FILE *f, int c) +{ + return overflow(f, c & 0xff); +} + +int __oflow(FILE *f) +{ + overflow(f, EOF); + return (f->flags & F_ERR) ? EOF : 0; +} + +/* Link flush-on-exit code iff any stdio write functions are linked. */ +int (*const __fflush_on_exit)(FILE *) = fflush; diff --git a/05/musl-final/src/stdio/__scanf.c b/05/musl-final/src/stdio/__scanf.c new file mode 100644 index 0000000..185615d --- /dev/null +++ b/05/musl-final/src/stdio/__scanf.c @@ -0,0 +1,487 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "__scanf.h" + +static int read(rctx_t *r) +{ + if (--r->w < 0) return r->w = -1; + if (r->u) r->u = 0; + else r->read(r); + return r->c; +} + +static void unread(rctx_t *r) +{ + //if (r->u || r->w < 0) return; + if (r->w < 0) return; + r->w++; + r->u = 1; +} + +#define SIZE_hh -2 +#define SIZE_h -1 +#define SIZE_def 0 +#define SIZE_l 1 +#define SIZE_ll 2 +#define SIZE_L 3 + +static void store_int(void *dest, int size, int neg, unsigned long long i) +{ + if (!dest) return; + if (neg) i = -i; + switch (size) { + case SIZE_hh: + *(char *)dest = i; + break; + case SIZE_h: + *(short *)dest = i; + break; + case SIZE_def: + *(int *)dest = i; + break; + case SIZE_l: + *(long *)dest = i; + break; + case SIZE_ll: + *(long long *)dest = i; + break; + } +} + +static void *arg_n(va_list ap, unsigned int n) +{ + void *p; + unsigned int i; + va_list ap2; + va_copy(ap2, ap); + for (i=n; i>1; i--) va_arg(ap2, void *); + p = va_arg(ap2, void *); + va_end(ap2); + return p; +} + +int __scanf(rctx_t *r, const wchar_t *fmt, va_list ap) +{ + int mode=0; + int width; + int size; + const wchar_t *p, *z; + int c, l, t, m; + long long dummy; + char *s; + wchar_t *wcs; + mbstate_t st; + int wide = r->wide; + void *dest=NULL; + int invert; + unsigned long long i=0; + int neg=0; + int matches=0; + long double f; + int (*is_space)(int) = r->is_space; + + for (p=fmt; *p; ) { + if (is_space(*p)) { + do p++; while (is_space(*p)); + do r->w=1; while (is_space(read(r))); + unread(r); + continue; + } else if (*p != '%' || p[1] == '%') { + if (*p == '%') p++; + r->w = 1; + if (*p++ != read(r)) + goto match_fail; + continue; + } + p++; + if (mode != 1) { + for (z=p; isdigit(*z); z++); + if (*z != '$' && *z != '*') { + if (mode == 0) mode = 1; + else goto fmt_fail; + } else if (*z != '*') { + int pos = 0; + mode = 2; + for (; pw = width; + + if (t != 'n') { + if (read(r) < 0) goto input_fail; + unread(r); + } + + switch (t) { + case 'n': + store_int(dest, size, 0, r->l - r->u); + /* do not increment match count, etc! */ + continue; + case 'C': + wcs = dest ? dest : (void *)&dummy; + st = (mbstate_t){ 0 }; + while ((c=read(r)) >= 0) { + if (wide) { + if (dest) *wcs++ = c; + } else { + char ch = c; + switch (mbrtowc(wcs, &ch, 1, &st)) { + case -1: + goto enc_fail; + case -2: + break; + default: + if (dest) wcs++; + } + } + } + if (r->w > 0) goto match_fail; + break; + case 'c': + s = dest ? dest : (void *)&dummy; + while ((c=read(r)) >= 0) { + if (wide) { + if ((l=wctomb(s, c)) < 0) + goto enc_fail; + if (dest) s += l; + } else { + if (dest) *s++ = c; + } + } + if (r->w > 0) goto match_fail; + break; + case '[': + wcs = dest ? dest : (void *)&dummy; + s = dest ? dest : (void *)&dummy; + if (!wide && size == SIZE_l) st = (mbstate_t){ 0 }; + + if (*p == '^') p++, invert = 1; + else invert = 0; + + if (wide) { + for (m=0; (c=read(r)) >= 0; m=1) { + for (z=p; *z && *z != c && (*z != ']' || z==p); z++); + if (!*z) goto fmt_fail; + if (*z == c && (*z != ']' || z==p)) { + if (invert) break; + } else { + if (!invert) break; + } + if (size == SIZE_l) { + if (dest) *wcs++ = c; + } else { + if ((l=wctomb(s, c)) < 0) + goto enc_fail; + if (dest) s += l; + } + } + for (p++; *p && *p != ']'; p++); + p++; + } else { + unsigned char scanset[257]; + memset(scanset, invert, sizeof scanset); + scanset[0] = 0; + for (z=p; *z && (*z != ']' || z==p); z++) + scanset[1+*z] = 1-invert; + if (!*z) goto fmt_fail; + p=z+1; + c=0; + for (m=0; scanset[(c=read(r))+1]; m=1) { + if (size == SIZE_l) { + char ch = c; + switch (mbrtowc(wcs, &ch, 1, &st)) { + case -1: + goto enc_fail; + case -2: + break; + default: + if (dest) wcs++; + } + } else { + if (dest) *s++ = c; + } + } + } + if (!m) goto match_fail; + if (dest) { + if (size == SIZE_l) *wcs++ = 0; + else *s++ = 0; + } + break; + default: + /* read unlimited number of spaces, then reset width */ + do r->w = 1; while (is_space(c = read(r))); + if (c < 0) goto input_fail; + unread(r); + r->w = width; + } + + switch (t) { + case 'p': + case 'X': + t = 'x'; + case 'd': + case 'i': + case 'o': + case 'u': + case 'x': + i = m = neg = 0; + if ((c=read(r)) == '-') neg=1; + else if (c != '+') unread(r); + switch (t) { + case 'i': + case 'x': + if ((c=read(r)) != '0') { + if (t == 'i') t = 'd'; + unread(r); + break; + } + if (((c=read(r))|0x20) != 'x') { + if (t == 'i') { + t = 'o'; + /* lone 0 is valid octal */ + if ((unsigned)(c-'0') >= 8) { + m = 1; + goto int_finish; + } + } + unread(r); + break; + } + t = 'x'; + } + } + + switch (t) { + case 'd': + case 'u': + for (m=0; isdigit(c=read(r)); m=1) + i = 10*i + c-'0'; + goto int_finish; + case 'o': + for (m=0; (unsigned)(c=read(r))-'0' < 8; m=1) + i = (i<<3) + c-'0'; + goto int_finish; + case 'x': + for (m=0; ; m=1) { + if (isdigit(c=read(r))) { + i = (i<<4) + c-'0'; + } else if ((unsigned)(c|0x20)-'a' < 6) { + i = (i<<4) + (c|0x20)-'a'+10; + } else break; + } + int_finish: + if (!m) goto match_fail; + store_int(dest, size, neg, i); + break; + case 'a': + case 'e': + case 'f': + case 'g': + f = 0.0; + neg = m = 0; + if ((c=read(r)) == '-') neg=1; + else if (c != '+') unread(r); + /* FIXME: check for INF/NAN strings here */ + if (read(r)=='0' && (m=1, (read(r)|0x20) == 'x')) + goto hexfloat; + else unread(r); + for (; isdigit(c=read(r)); m=1) + f = 10.0 * f + (c-'0'); + if (c=='.') { + double mag = 10.0; + for (; isdigit(c=read(r)); mag*=10.0) + f += (c-'0')/mag; + } + if ((c|0x20)=='e') { + int ex=0, en=0; + m = 0; + if ((c=read(r))=='-') en=1; + else if (c!='+') unread(r); + for (; isdigit(c=read(r)); m=1) + if (ex < LDBL_MAX_10_EXP) + ex = 10 * ex + (c-'0'); + if (ex > LDBL_MAX_10_EXP) + f = en ? 0 : INFINITY; + else { + if (en) while (ex--) f/=10.0; + else while (ex--) f*=10.0; + } + } + goto writefloat; +hexfloat: + m = 0; + for (; isxdigit(c=read(r)); m=1) + if (isdigit(c)) f = 16.0*f + (c-'0'); + else f = 16.0*f + ((c|32)-'a'+10); + if (c=='.') { + double mag = 1/16.0; + for (; isxdigit(c=read(r)); mag*=1/16.0) + if (isdigit(c)) f += (c-'0')*mag; + else f += ((c|32)-'a'+10)*mag; + } + if ((c|0x20)=='p') { + int ex=0, en=0; + m = 0; + if ((c=read(r))=='-') en=1; + else if (c!='+') unread(r); + for (; isdigit(c=read(r)); m=1) + if (ex < LDBL_MAX_EXP) + ex = 10 * ex + (c-'0'); + if (ex > LDBL_MAX_EXP) + f = en ? 0 : INFINITY; + else { + if (en) while (ex--) f*=0.5; + else while (ex--) f*=2.0; + } + } +writefloat: + if (!m) goto match_fail; + if (neg) f *= -1.0; + if (dest) switch (size) { + case SIZE_def: + *(float *)dest = f; + break; + case SIZE_l: + *(double *)dest = f; + break; + case SIZE_L: + *(long double *)dest = f; + break; + } + break; + case 'S': + wcs = dest ? dest : (void *)&dummy; + st = (mbstate_t){ 0 }; + while((c=read(r)) >= 0) { + if (wide) { + if (is_space(c)) break; + if (dest) *wcs++ = c; + } else { + char ch = c; + if (is_space(c)) break; + switch (mbrtowc(wcs, &ch, 1, &st)) { + case -1: + goto enc_fail; + case -2: + break; + default: + if (dest) wcs++; + } + } + } + if (dest) *wcs++ = 0; + break; + case 's': + s = dest ? dest : (void *)&dummy; + while((c=read(r)) >= 0) { + if (wide) { + if (is_space(c)) break; + if ((l=wctomb(s, c)) < 0) + goto enc_fail; + if (dest) s += l; + } else { + if (is_space(c)) break; + if (dest) *s++ = c; + } + } + if (dest) *s++ = 0; + break; + } + + /* unread will do nothing if field width was exhausted */ + unread(r); + if (dest) matches++; + } + return matches; +enc_fail: + errno = EILSEQ; +fmt_fail: +input_fail: + if (!matches) matches--; +match_fail: + unread(r); + return matches; +} diff --git a/05/musl-final/src/stdio/__scanf.h b/05/musl-final/src/stdio/__scanf.h new file mode 100644 index 0000000..e549b97 --- /dev/null +++ b/05/musl-final/src/stdio/__scanf.h @@ -0,0 +1,16 @@ +#include + +typedef struct rctx +{ + void (*read)(struct rctx *); + void *opaque; + int wide; + int (*is_space)(); + int l; + int e; + int c; + int u; + int w; +} rctx_t; + +int __scanf(rctx_t *, const wchar_t *, va_list); diff --git a/05/musl-final/src/stdio/__stdio_close.c b/05/musl-final/src/stdio/__stdio_close.c new file mode 100644 index 0000000..24fef33 --- /dev/null +++ b/05/musl-final/src/stdio/__stdio_close.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +int __stdio_close(FILE *f) +{ + return __syscall_close(f->fd); +} diff --git a/05/musl-final/src/stdio/__stdio_read.c b/05/musl-final/src/stdio/__stdio_read.c new file mode 100644 index 0000000..ee7e125 --- /dev/null +++ b/05/musl-final/src/stdio/__stdio_read.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +size_t __stdio_read(FILE *f, unsigned char *buf, size_t len) +{ + return __syscall_read(f->fd, buf, len); +} diff --git a/05/musl-final/src/stdio/__stdio_seek.c b/05/musl-final/src/stdio/__stdio_seek.c new file mode 100644 index 0000000..c7a5b73 --- /dev/null +++ b/05/musl-final/src/stdio/__stdio_seek.c @@ -0,0 +1,21 @@ +#include "stdio_impl.h" + +static off_t retneg1(FILE *f, off_t off, int whence) +{ + return -1; +} + +off_t __stdio_seek(FILE *f, off_t off, int whence) +{ + off_t ret; +#ifdef __NR__llseek + if (syscall5(__NR__llseek, f->fd, off>>32, off, (long)&ret, whence)<0) + ret = -1; +#else + ret = syscall3(__NR_lseek, f->fd, off, whence); +#endif + /* Detect unseekable files and optimize future failures out */ + if (ret < 0 && off == 0 && whence == SEEK_CUR) + f->seek = retneg1; + return ret; +} diff --git a/05/musl-final/src/stdio/__stdio_write.c b/05/musl-final/src/stdio/__stdio_write.c new file mode 100644 index 0000000..7854562 --- /dev/null +++ b/05/musl-final/src/stdio/__stdio_write.c @@ -0,0 +1,9 @@ +#include "stdio_impl.h" + +size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) +{ + const unsigned char *stop = buf+len; + ssize_t cnt = 1; + for (; buffd, buf, len))>0; buf+=cnt); + return len-(stop-buf); +} diff --git a/05/musl-final/src/stdio/__uflow.c b/05/musl-final/src/stdio/__uflow.c new file mode 100644 index 0000000..5a51d61 --- /dev/null +++ b/05/musl-final/src/stdio/__uflow.c @@ -0,0 +1,7 @@ +#include "stdio_impl.h" + +int __uflow(FILE *f) +{ + if (__underflow(f) < 0) return EOF; + else return *f->rpos++; +} diff --git a/05/musl-final/src/stdio/__underflow.c b/05/musl-final/src/stdio/__underflow.c new file mode 100644 index 0000000..b769f4e --- /dev/null +++ b/05/musl-final/src/stdio/__underflow.c @@ -0,0 +1,38 @@ +#include "stdio_impl.h" + +int __underflow(FILE *f) +{ + ssize_t cnt; + + /* Read from buffer (Do we ever get called when this is true??) */ + if (f->rpos < f->rstop) return *f->rpos; + + /* Initialize if we're not already reading */ + if (!f->rstop) { + /* Fail immediately if unreadable, eof, or error state. */ + if (f->flags & (F_EOF|F_ERR|F_NORD)) return EOF; + + /* Set byte orientation -1,0=>-1; 1=>1 */ + f->mode |= f->mode-1; + + /* Flush any unwritten output; fail on error. */ + if (f->wpos > f->buf && __oflow(f)) return EOF; + + /* Disallow writes to buffer. */ + f->wstop = 0; + } + + /* Perform the underlying read operation */ + if ((cnt=f->read(f, f->buf, f->buf_size)) + 1 <= 1) { + /* Set flags and leave read mode */ + f->flags |= F_EOF | (cnt & F_ERR); + f->rpos = f->rend = f->rstop = 0; + return EOF; + } + + /* Setup buffer pointers for reading from buffer */ + f->rpos = f->buf; + f->rend = f->rstop = f->buf + cnt; + + return *f->rpos; +} diff --git a/05/musl-final/src/stdio/asprintf.c b/05/musl-final/src/stdio/asprintf.c new file mode 100644 index 0000000..79e59c2 --- /dev/null +++ b/05/musl-final/src/stdio/asprintf.c @@ -0,0 +1,14 @@ +#include +#include + +int vasprintf(char **, const char *, va_list); + +int asprintf(char **s, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vasprintf(s, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/clearerr.c b/05/musl-final/src/stdio/clearerr.c new file mode 100644 index 0000000..a409120 --- /dev/null +++ b/05/musl-final/src/stdio/clearerr.c @@ -0,0 +1,16 @@ +#include "stdio_impl.h" + +void clearerr(FILE *f) +{ + FLOCK(f); + f->flags &= ~(F_EOF|F_ERR); + FUNLOCK(f); +} + +void clearerr_unlocked(FILE *f) +{ + FLOCK(f); + f->flags &= ~(F_EOF|F_ERR); + FUNLOCK(f); +} + diff --git a/05/musl-final/src/stdio/dprintf.c b/05/musl-final/src/stdio/dprintf.c new file mode 100644 index 0000000..fa28322 --- /dev/null +++ b/05/musl-final/src/stdio/dprintf.c @@ -0,0 +1,12 @@ +#include +#include + +int dprintf(int fd, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vdprintf(fd, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/fclose.c b/05/musl-final/src/stdio/fclose.c new file mode 100644 index 0000000..26bc37e --- /dev/null +++ b/05/musl-final/src/stdio/fclose.c @@ -0,0 +1,21 @@ +#include "stdio_impl.h" + +int fclose(FILE *f) +{ + int r; + int perm = f->flags & F_PERM; + + if (!perm) { + OFLLOCK(); + if (f->prev) f->prev->next = f->next; + if (f->next) f->next->prev = f->prev; + if (ofl_head == f) ofl_head = f->next; + OFLUNLOCK(); + } + + r = fflush(f) | f->close(f); + + if (!perm) free(f); + + return r; +} diff --git a/05/musl-final/src/stdio/feof.c b/05/musl-final/src/stdio/feof.c new file mode 100644 index 0000000..9038a7c --- /dev/null +++ b/05/musl-final/src/stdio/feof.c @@ -0,0 +1,13 @@ +#include "stdio_impl.h" + +#undef feof + +int feof(FILE *f) +{ + return !!(f->flags & F_EOF); +} + +int feof_unlocked(FILE *f) +{ + return !!(f->flags & F_EOF); +} diff --git a/05/musl-final/src/stdio/ferror.c b/05/musl-final/src/stdio/ferror.c new file mode 100644 index 0000000..2c59172 --- /dev/null +++ b/05/musl-final/src/stdio/ferror.c @@ -0,0 +1,13 @@ +#include "stdio_impl.h" + +#undef ferror + +int ferror(FILE *f) +{ + return !!(f->flags & F_ERR); +} + +int ferror_unlocked(FILE *f) +{ + return !!(f->flags & F_ERR); +} diff --git a/05/musl-final/src/stdio/fflush.c b/05/musl-final/src/stdio/fflush.c new file mode 100644 index 0000000..5b79fa8 --- /dev/null +++ b/05/musl-final/src/stdio/fflush.c @@ -0,0 +1,54 @@ +#include "stdio_impl.h" + +static int __fflush_unlocked(FILE *f) +{ + /* If writing, flush output. */ + if (f->wpos > f->buf && __oflow(f)) return -1; + + /* If reading, sync position, per POSIX */ + if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); + f->rpos = f->rend; + + /* Hook for special behavior on flush */ + if (f->flush) f->flush(f); + + return (f->flags & F_ERR) ? EOF : 0; +} + +#if 0 +/* stdout.c will override this if linked */ +static FILE *const __dummy = 0; +weak_alias(__dummy, __stdout_to_flush); +#else +extern FILE *const __stdout_to_flush; +#endif + +int fflush(FILE *f) +{ + int r; + FILE *next; + + if (f) { + FLOCK(f); + r = __fflush_unlocked(f); + FUNLOCK(f); + return r; + } + + r = __stdout_to_flush ? fflush(__stdout_to_flush) : 0; + + OFLLOCK(); + for (f=ofl_head; f; f=next) { + FLOCK(f); + OFLUNLOCK(); + r |= __fflush_unlocked(f); + OFLLOCK(); + next = f->next; + FUNLOCK(f); + } + OFLUNLOCK(); + + return r; +} + +weak_alias(__fflush_unlocked, fflush_unlocked); diff --git a/05/musl-final/src/stdio/fgetc.c b/05/musl-final/src/stdio/fgetc.c new file mode 100644 index 0000000..8810f35 --- /dev/null +++ b/05/musl-final/src/stdio/fgetc.c @@ -0,0 +1,19 @@ +#include "stdio_impl.h" + +int fgetc(FILE *f) +{ + int c; + FLOCK(f); + c = f->rpos < f->rstop ? *f->rpos++ : __uflow(f); + FUNLOCK(f); + return c; +} + +int fgetc_unlocked(FILE *f) +{ + int c; + FLOCK(f); + c = f->rpos < f->rstop ? *f->rpos++ : __uflow(f); + FUNLOCK(f); + return c; +} diff --git a/05/musl-final/src/stdio/fgetpos.c b/05/musl-final/src/stdio/fgetpos.c new file mode 100644 index 0000000..5b663d1 --- /dev/null +++ b/05/musl-final/src/stdio/fgetpos.c @@ -0,0 +1,11 @@ +#include "stdio_impl.h" + +int fgetpos(FILE *f, fpos_t *pos) +{ + off_t off = __ftello(f); + if (off < 0) return -1; + *(off_t *)pos = off; + return 0; +} + +LFS64(fgetpos); diff --git a/05/musl-final/src/stdio/fgets.c b/05/musl-final/src/stdio/fgets.c new file mode 100644 index 0000000..22dab95 --- /dev/null +++ b/05/musl-final/src/stdio/fgets.c @@ -0,0 +1,37 @@ +#include "stdio_impl.h" + +#define MIN(a,b) ((a)<(b) ? (a) : (b)) + +char *fgets(char *s, int n, FILE *f) +{ + char *p = s; + unsigned char *z; + size_t k; + + if (!n--) return 0; + + FLOCK(f); + + while (n && !feof(f)) { + z = memchr(f->rpos, '\n', f->rend - f->rpos); + k = z ? z - f->rpos + 1 : f->rend - f->rpos; + k = MIN(k, n); + memcpy(p, f->rpos, k); + f->rpos += k; + p += k; + n -= k; + if (z) break; + __underflow(f); + } + *p = 0; + if (ferror(f)) p = s; + + FUNLOCK(f); + + return (p == s) ? 0 : s; +} + + +char *fgets_unlocked(char *s, int n, FILE *f) { + return fgets(s, n, f); +} diff --git a/05/musl-final/src/stdio/fgetwc.c b/05/musl-final/src/stdio/fgetwc.c new file mode 100644 index 0000000..77b30fd --- /dev/null +++ b/05/musl-final/src/stdio/fgetwc.c @@ -0,0 +1,51 @@ +#include "stdio_impl.h" + +wint_t __fgetwc_unlocked(FILE *f) +{ + mbstate_t st = { 0 }; + wchar_t wc; + int c; + unsigned char b; + size_t l; + + f->mode |= f->mode+1; + + /* Convert character from buffer if possible */ + if (f->rpos < f->rend) { + l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st); + if (l+2 >= 2) { + f->rpos += l + !l; /* l==0 means 1 byte, null */ + return wc; + } + if (l == -1) { + f->rpos++; + return WEOF; + } + } else l = -2; + + /* Convert character byte-by-byte from __uflow */ + while (l == -2) { + b = c = __uflow(f); + if (c < 0) { + if (!mbsinit(&st)) errno = EILSEQ; + return WEOF; + } + l = mbrtowc(&wc, (void *)&b, 1, &st); + if (l == -1) return WEOF; + } + + FUNLOCK(f); + return wc; +} + +wint_t fgetwc(FILE *f) +{ + wint_t c; + FLOCK(f); + c = __fgetwc_unlocked(f); + FUNLOCK(f); + return c; +} + +weak_alias(__fgetwc_unlocked, fgetwc_unlocked); +weak_alias(__fgetwc_unlocked, getwc_unlocked); diff --git a/05/musl-final/src/stdio/fgetws.c b/05/musl-final/src/stdio/fgetws.c new file mode 100644 index 0000000..2e76b56 --- /dev/null +++ b/05/musl-final/src/stdio/fgetws.c @@ -0,0 +1,27 @@ +#include "stdio_impl.h" + +wint_t __fgetwc_unlocked(FILE *); + +wchar_t *fgetws(wchar_t *s, int n, FILE *f) +{ + wchar_t *p = s; + + if (!n--) return s; + + FLOCK(f); + + for (; n; n--) { + wint_t c = __fgetwc_unlocked(f); + if (c == WEOF) break; + *p++ = c; + if (c == '\n') break; + } + *p = 0; + if (ferror(f)) p = s; + + FUNLOCK(f); + + return (p == s) ? NULL : s; +} + +weak_alias(fgetws, fgetws_unlocked); diff --git a/05/musl-final/src/stdio/fileno.c b/05/musl-final/src/stdio/fileno.c new file mode 100644 index 0000000..38bc8e8 --- /dev/null +++ b/05/musl-final/src/stdio/fileno.c @@ -0,0 +1,11 @@ +#include "stdio_impl.h" + +int fileno(FILE *f) +{ + return f->fd; +} + +int fileno_unlocked(FILE *f) +{ + return f->fd; +} diff --git a/05/musl-final/src/stdio/fmemopen.c b/05/musl-final/src/stdio/fmemopen.c new file mode 100644 index 0000000..77a6074 --- /dev/null +++ b/05/musl-final/src/stdio/fmemopen.c @@ -0,0 +1,16 @@ +#if 0 +#include "stdio_impl.h" + +static ssize_t mread(FILE *f, unsigned char *buf, size_t len) +{ + memcpy(buf, +} + +FILE *fmemopen(void *buf, size_t size, const char *mode) +{ + FILE *f = calloc(sizeof(FILE), 1); + if (!f) return 0; + + // +} +#endif diff --git a/05/musl-final/src/stdio/fopen.c b/05/musl-final/src/stdio/fopen.c new file mode 100644 index 0000000..670cf5f --- /dev/null +++ b/05/musl-final/src/stdio/fopen.c @@ -0,0 +1,34 @@ +#include "stdio_impl.h" + +FILE *fopen(const char *filename, const char *mode) +{ + FILE *f; + int fd; + int flags; + int plus = !!strchr(mode, '+'); + + /* Check for valid initial mode character */ + if (!strchr("rwa", *mode)) { + errno = EINVAL; + return 0; + } + + /* Compute the flags to pass to open() */ + if (plus) flags = O_RDWR; + else if (*mode == 'r') flags = O_RDONLY; + else flags = O_WRONLY; + if (*mode != 'r') flags |= O_CREAT; + if (*mode == 'w') flags |= O_TRUNC; + if (*mode == 'a') flags |= O_APPEND; + + fd = __syscall_open(filename, flags, 0666); + if (fd < 0) return 0; + + f = __fdopen(fd, mode); + if (f) return f; + + __syscall_close(fd); + return 0; +} + +LFS64(fopen); diff --git a/05/musl-final/src/stdio/fprintf.c b/05/musl-final/src/stdio/fprintf.c new file mode 100644 index 0000000..a220cc1 --- /dev/null +++ b/05/musl-final/src/stdio/fprintf.c @@ -0,0 +1,12 @@ +#include +#include + +int fprintf(FILE *f, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vfprintf(f, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/fputc.c b/05/musl-final/src/stdio/fputc.c new file mode 100644 index 0000000..3e0f738 --- /dev/null +++ b/05/musl-final/src/stdio/fputc.c @@ -0,0 +1,19 @@ +#include "stdio_impl.h" + +int fputc(int c, FILE *f) +{ + FLOCK(f); + if (c != f->lbf && f->wpos + 1 < f->wend) *f->wpos++ = c; + else c = __overflow(f, c); + FUNLOCK(f); + return c; +} + +int fputc_unlocked(int c, FILE *f) +{ + FLOCK(f); + if (c != f->lbf && f->wpos + 1 < f->wend) *f->wpos++ = c; + else c = __overflow(f, c); + FUNLOCK(f); + return c; +} diff --git a/05/musl-final/src/stdio/fputs.c b/05/musl-final/src/stdio/fputs.c new file mode 100644 index 0000000..5f78ca7 --- /dev/null +++ b/05/musl-final/src/stdio/fputs.c @@ -0,0 +1,15 @@ +#include "stdio_impl.h" + +int fputs(const char *s, FILE *f) +{ + size_t l = strlen(s); + if (!l) return 0; + return (int)fwrite(s, l, 1, f) - 1; +} + +int fputs_unlocked(const char *s, FILE *f) +{ + size_t l = strlen(s); + if (!l) return 0; + return (int)fwrite(s, l, 1, f) - 1; +} diff --git a/05/musl-final/src/stdio/fputwc.c b/05/musl-final/src/stdio/fputwc.c new file mode 100644 index 0000000..b48bb74 --- /dev/null +++ b/05/musl-final/src/stdio/fputwc.c @@ -0,0 +1,33 @@ +#include "stdio_impl.h" + +wint_t __fputwc_unlocked(wchar_t c, FILE *f) +{ + char mbc[MB_LEN_MAX]; + int l; + + f->mode |= f->mode+1; + + if (isascii(c)) { + if (c != f->lbf && f->wpos + 1 < f->wend) *f->wpos++ = c; + else c = __overflow(f, c); + } else if (f->wpos + MB_LEN_MAX < f->wend) { + l = wctomb(f->wpos, c); + if (l < 0) c = WEOF; + else f->wpos += l; + } else { + l = wctomb(mbc, c); + if (l < 0 || __fwritex(mbc, l, f) < l) c = WEOF; + } + return c; +} + +wint_t fputwc(wchar_t c, FILE *f) +{ + FLOCK(f); + c = __fputwc_unlocked(c, f); + FUNLOCK(f); + return 0; +} + +weak_alias(__fputwc_unlocked, fputwc_unlocked); +weak_alias(__fputwc_unlocked, putwc_unlocked); diff --git a/05/musl-final/src/stdio/fputws.c b/05/musl-final/src/stdio/fputws.c new file mode 100644 index 0000000..b75f95b --- /dev/null +++ b/05/musl-final/src/stdio/fputws.c @@ -0,0 +1,23 @@ +#include "stdio_impl.h" + +int fputws(const wchar_t *ws, FILE *f) +{ + unsigned char buf[BUFSIZ]; + size_t l=0; + + FLOCK(f); + + f->mode |= f->mode+1; + + while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1) + if (__fwritex(buf, l, f) < l) { + FUNLOCK(f); + return -1; + } + + FUNLOCK(f); + + return l; /* 0 or -1 */ +} + +weak_alias(fputws, fputws_unlocked); diff --git a/05/musl-final/src/stdio/fread.c b/05/musl-final/src/stdio/fread.c new file mode 100644 index 0000000..8b9ca55 --- /dev/null +++ b/05/musl-final/src/stdio/fread.c @@ -0,0 +1,51 @@ +#include "stdio_impl.h" + +#define MIN(a,b) ((a)<(b) ? (a) : (b)) + +size_t fread(void *destv, size_t size, size_t nmemb, FILE *f) +{ + unsigned char *dest = destv; + size_t len = size*nmemb, l = len, k; + + /* Never touch the file if length is zero.. */ + if (!l) return 0; + + FLOCK(f); + + for (;;) { + /* First exhaust the buffer. */ + k = MIN(f->rend - f->rpos, l); + memcpy(dest, f->rpos, k); + f->rpos += k; + dest += k; + l -= k; + + /* Stop on EOF or errors */ + if (f->flags & (F_EOF|F_ERR|F_NORD)) goto eof; + + /* Done? Or going unbuffered? */ + if (!l || l > f->buf_size/2) break; + + /* Otherwise, refill & read thru buffer. */ + __underflow(f); + } + + /* Read the remainder directly */ + for (; l; l-=k, dest+=k) { + k = f->read(f, dest, l); + if (k+1<=1) { + f->flags |= F_EOF | (F_ERR & k); + goto eof; + } + } + + FUNLOCK(f); + return nmemb; +eof: + FUNLOCK(f); + return (len-l)/size; +} + +size_t fread_unlocked(void *destv, size_t size, size_t nmemb, FILE *f) { + return fread(destv, size, nmemb, f); +} diff --git a/05/musl-final/src/stdio/freopen.c b/05/musl-final/src/stdio/freopen.c new file mode 100644 index 0000000..8d3af9f --- /dev/null +++ b/05/musl-final/src/stdio/freopen.c @@ -0,0 +1,47 @@ +#include "stdio_impl.h" + +/* The basic idea of this implementation is to open a new FILE, + * hack the necessary parts of the new FILE into the old one, then + * close the new FILE. */ + +/* Locking is not necessary because, in the event of failure, the stream + * passed to freopen is invalid as soon as freopen is called. */ + +FILE *freopen(const char *filename, const char *mode, FILE *f) +{ + int fl; + FILE *f2; + + fflush(f); + + if (!filename) { + f2 = fopen("/dev/null", mode); + if (!f2) goto fail; + fl = __syscall_fcntl(f2->fd, F_GETFL, 0); + if (fl < 0 || __syscall_fcntl(f->fd, F_SETFL, fl) < 0) + goto fail2; + } else { + f2 = fopen(filename, mode); + if (!f2) goto fail; + if (__syscall_dup2(f2->fd, f->fd) < 0) + goto fail2; + } + + f->flags = (f->flags & F_PERM) | f2->flags; + f->read = f2->read; + f->write = f2->write; + f->seek = f2->seek; + f->close = f2->close; + f->flush = f2->flush; + + fclose(f2); + return f; + +fail2: + fclose(f2); +fail: + fclose(f); + return NULL; +} + +LFS64(freopen); diff --git a/05/musl-final/src/stdio/fscanf.c b/05/musl-final/src/stdio/fscanf.c new file mode 100644 index 0000000..51fc9b3 --- /dev/null +++ b/05/musl-final/src/stdio/fscanf.c @@ -0,0 +1,12 @@ +#include +#include + +int fscanf(FILE *f, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vfscanf(f, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/fseek.c b/05/musl-final/src/stdio/fseek.c new file mode 100644 index 0000000..bfaad37 --- /dev/null +++ b/05/musl-final/src/stdio/fseek.c @@ -0,0 +1,38 @@ +#include "stdio_impl.h" + +int __fseeko_unlocked(FILE *f, off_t off, int whence) +{ + /* Adjust relative offset for unread data in buffer, if any. */ + if (whence == SEEK_CUR) off -= f->rend - f->rpos; + + /* If writing, flush output. */ + if (f->wpos > f->buf && __oflow(f)) return -1; + + /* Perform the underlying seek operation. */ + if (f->seek(f, off, whence) < 0) return -1; + + /* If seek succeeded, file is seekable and we discard read buffer. */ + f->rpos = f->rend = f->rstop = 0; + f->flags &= ~F_EOF; + + FUNLOCK(f); + return 0; +} + +int __fseeko(FILE *f, off_t off, int whence) +{ + int result; + FLOCK(f); + result = __fseeko_unlocked(f, off, whence); + FUNLOCK(f); + return result; +} + +int fseek(FILE *f, long off, int whence) +{ + return __fseeko(f, off, whence); +} + +weak_alias(__fseeko, fseeko); + +LFS64(fseeko); diff --git a/05/musl-final/src/stdio/fsetpos.c b/05/musl-final/src/stdio/fsetpos.c new file mode 100644 index 0000000..5d76c8c --- /dev/null +++ b/05/musl-final/src/stdio/fsetpos.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +int fsetpos(FILE *f, const fpos_t *pos) +{ + return __fseeko(f, *(const off_t *)pos, SEEK_SET); +} + +LFS64(fsetpos); diff --git a/05/musl-final/src/stdio/ftell.c b/05/musl-final/src/stdio/ftell.c new file mode 100644 index 0000000..aa1f538 --- /dev/null +++ b/05/musl-final/src/stdio/ftell.c @@ -0,0 +1,35 @@ +#include "stdio_impl.h" + +off_t __ftello_unlocked(FILE *f) +{ + off_t pos = f->seek(f, 0, SEEK_CUR); + if (pos < 0) { + FUNLOCK(f); + return pos; + } + /* Adjust for data in buffer. */ + return pos - (f->rend - f->rpos) + (f->wpos - f->wbase); +} + +off_t __ftello(FILE *f) +{ + off_t pos; + FLOCK(f); + pos = __ftello_unlocked(f); + FUNLOCK(f); + return pos; +} + +long ftell(FILE *f) +{ + off_t pos = __ftello(f); + if (pos > LONG_MAX) { + errno = EOVERFLOW; + return -1; + } + return pos; +} + +weak_alias(__ftello, ftello); + +LFS64(ftello); diff --git a/05/musl-final/src/stdio/fwide.c b/05/musl-final/src/stdio/fwide.c new file mode 100644 index 0000000..f4da47f --- /dev/null +++ b/05/musl-final/src/stdio/fwide.c @@ -0,0 +1,10 @@ +#include "stdio_impl.h" + +#define SH (8*sizeof(int)-1) +#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH)) + +int fwide(FILE *f, int mode) +{ + if (!f->mode) f->mode = NORMALIZE(mode); + return f->mode; +} diff --git a/05/musl-final/src/stdio/fwrite.c b/05/musl-final/src/stdio/fwrite.c new file mode 100644 index 0000000..d6bf314 --- /dev/null +++ b/05/musl-final/src/stdio/fwrite.c @@ -0,0 +1,59 @@ +#include "stdio_impl.h" + +size_t __fwritex(const unsigned char *s, size_t l, FILE *f) +{ + size_t i = 0; + size_t k = f->wend - f->wpos; + + /* Handle line-buffered mode by breaking into 2 parts */ + if (f->lbf >= 0) { + /* Match /^(.*\n|)/ */ + for (i=l; i && s[i-1] != '\n'; i--); + if (i) { + f->lbf = EOF; + __fwritex(s, i, f); + f->lbf = '\n'; + __oflow(f); + return ferror(f) ? 0 : i + __fwritex(s+i, l-i, f); + } + } + + /* Buffer initial segment */ + if (k > l) k = l; + memcpy(f->wpos, s, k); + f->wpos += k; + if (f->wpos < f->wend) return l; + + /* If there's work left to do, flush buffer */ + __oflow(f); + if (ferror(f)) return 0; + + /* If the remainder will not fit in buffer, write it directly */ + if (l - k >= f->wend - f->wpos) + return k + f->write(f, s+k, l-k); + + /* Otherwise, buffer the remainder */ + memcpy(f->wpos, s+k, l-k); + f->wpos += l-k; + return l; +} + +size_t fwrite(const void *src, size_t size, size_t nmemb, FILE *f) +{ + size_t l = size*nmemb; + if (!l) return l; + FLOCK(f); + l = __fwritex(src, l, f); + FUNLOCK(f); + return l/size; +} + +size_t fwrite_unlocked(const void *src, size_t size, size_t nmemb, FILE *f) +{ + size_t l = size*nmemb; + if (!l) return l; + FLOCK(f); + l = __fwritex(src, l, f); + FUNLOCK(f); + return l/size; +} diff --git a/05/musl-final/src/stdio/fwscanf.c b/05/musl-final/src/stdio/fwscanf.c new file mode 100644 index 0000000..a6892cf --- /dev/null +++ b/05/musl-final/src/stdio/fwscanf.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int fwscanf(FILE *f, const wchar_t *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vfwscanf(f, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/getc.c b/05/musl-final/src/stdio/getc.c new file mode 100644 index 0000000..b739b0a --- /dev/null +++ b/05/musl-final/src/stdio/getc.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +int getc(FILE *f) +{ + return fgetc(f); +} diff --git a/05/musl-final/src/stdio/getc_unlocked.c b/05/musl-final/src/stdio/getc_unlocked.c new file mode 100644 index 0000000..629223e --- /dev/null +++ b/05/musl-final/src/stdio/getc_unlocked.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +int getc_unlocked(FILE *f) +{ + return f->rpos < f->rstop ? *f->rpos++ : __uflow(f); +} + +weak_alias (getc_unlocked, fgetc_unlocked); diff --git a/05/musl-final/src/stdio/getchar.c b/05/musl-final/src/stdio/getchar.c new file mode 100644 index 0000000..c101265 --- /dev/null +++ b/05/musl-final/src/stdio/getchar.c @@ -0,0 +1,6 @@ +#include + +int getchar(void) +{ + return fgetc(stdin); +} diff --git a/05/musl-final/src/stdio/getchar_unlocked.c b/05/musl-final/src/stdio/getchar_unlocked.c new file mode 100644 index 0000000..299cb95 --- /dev/null +++ b/05/musl-final/src/stdio/getchar_unlocked.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +int getchar_unlocked(void) +{ + return stdin->rpos < stdin->rstop ? *stdin->rpos++ : __uflow(stdin); +} diff --git a/05/musl-final/src/stdio/getdelim.c b/05/musl-final/src/stdio/getdelim.c new file mode 100644 index 0000000..f770d20 --- /dev/null +++ b/05/musl-final/src/stdio/getdelim.c @@ -0,0 +1,59 @@ +#include "stdio_impl.h" + +#define MIN(a,b) ((a)<(b) ? (a) : (b)) + +ssize_t getdelim(char **s, size_t *n, int delim, FILE *f) +{ + char *tmp; + unsigned char *z; + size_t k; + size_t i=0; + + if (!n || !s) { + errno = EINVAL; + return -1; + } + + if (!*s) *n=0; + + FLOCK(f); + + while (!feof(f)) { + z = memchr(f->rpos, delim, f->rend - f->rpos); + k = z ? z - f->rpos + 1 : f->rend - f->rpos; + if (i+k >= *n) { + if (k >= SIZE_MAX-i) goto oom; + *n = i+k+1; + if (*n < SIZE_MAX/2) *n *= 2; + tmp = realloc(*s, *n); + if (!tmp) { + *n = i+k+1; + tmp = realloc(*s, *n); + if (!tmp) goto oom; + } + *s = tmp; + } + memcpy(*s+i, f->rpos, k); + f->rpos += k; + i += k; + if (z) break; + __underflow(f); + } + (*s)[i] = 0; + if (feof(f) || ferror(f)) { + FUNLOCK(f); + return -1; + } + + FUNLOCK(f); + + if (i > SSIZE_MAX) { + errno = EOVERFLOW; + return -1; + } + + return i; +oom: + errno = ENOMEM; + return -1; +} diff --git a/05/musl-final/src/stdio/getline.c b/05/musl-final/src/stdio/getline.c new file mode 100644 index 0000000..a3a6651 --- /dev/null +++ b/05/musl-final/src/stdio/getline.c @@ -0,0 +1,6 @@ +#include + +ssize_t getline(char **s, size_t *n, FILE *f) +{ + return getdelim(s, n, '\n', f); +} diff --git a/05/musl-final/src/stdio/gets.c b/05/musl-final/src/stdio/gets.c new file mode 100644 index 0000000..24319eb --- /dev/null +++ b/05/musl-final/src/stdio/gets.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +char *gets(char *s) +{ + char *ret = fgets(s, INT_MAX, stdin); + if (ret && s[strlen(s)-1] == '\n') s[strlen(s)-1] = 0; + return ret; +} diff --git a/05/musl-final/src/stdio/getw.c b/05/musl-final/src/stdio/getw.c new file mode 100644 index 0000000..de9e985 --- /dev/null +++ b/05/musl-final/src/stdio/getw.c @@ -0,0 +1,7 @@ +#include + +int getw(FILE *f) +{ + int x; + return fread(&x, sizeof x, 1, f) ? x : EOF; +} diff --git a/05/musl-final/src/stdio/getwc.c b/05/musl-final/src/stdio/getwc.c new file mode 100644 index 0000000..a2818bc --- /dev/null +++ b/05/musl-final/src/stdio/getwc.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +wint_t getwc(FILE *f) +{ + return fgetwc(f); +} diff --git a/05/musl-final/src/stdio/getwchar.c b/05/musl-final/src/stdio/getwchar.c new file mode 100644 index 0000000..2295bd4 --- /dev/null +++ b/05/musl-final/src/stdio/getwchar.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +wint_t getwchar(void) +{ + return fgetwc(stdin); +} + +weak_alias(getwchar, getwchar_unlocked); diff --git a/05/musl-final/src/stdio/pclose.c b/05/musl-final/src/stdio/pclose.c new file mode 100644 index 0000000..c2fe7a2 --- /dev/null +++ b/05/musl-final/src/stdio/pclose.c @@ -0,0 +1,10 @@ +#include "stdio_impl.h" + +int pclose(FILE *f) +{ + int status; + fclose(f); + while (waitpid(f->pipe_pid, &status, 0) == -1) + if (errno != EINTR) return -1; + return status; +} diff --git a/05/musl-final/src/stdio/perror.c b/05/musl-final/src/stdio/perror.c new file mode 100644 index 0000000..e4637c8 --- /dev/null +++ b/05/musl-final/src/stdio/perror.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include "stdio_impl.h" + +void perror(const char *msg) +{ +#if 1 + if (msg) fprintf(stderr, "%s: %m\n", msg, strerror(errno)); + else fprintf(stderr, "%m\n"); +#else + FILE *f = stderr; + char *errstr = strerror(errno); + + FLOCK(f); + + if (msg) { + __fwritex(msg, strlen(msg), f); + __putc_unlocked(':', f); + __putc_unlocked(' ', f); + } + __fwritex(errstr, strlen(errstr), f); + __putc_unlocked('\n', f); + + FUNLOCK(f); +#endif +} diff --git a/05/musl-final/src/stdio/popen.c b/05/musl-final/src/stdio/popen.c new file mode 100644 index 0000000..1d33e9d --- /dev/null +++ b/05/musl-final/src/stdio/popen.c @@ -0,0 +1,43 @@ +#include "stdio_impl.h" + +FILE *popen(const char *cmd, const char *mode) +{ + int p[2]; + int op; + pid_t pid; + FILE *f; + const char *modes = "rw", *mi = strchr(modes, *mode); + + if (mi) { + op = mi-modes; + } else { + errno = EINVAL; + return 0; + } + + if (pipe(p)) return NULL; + f = fdopen(p[op], mode); + if (!f) { + close(p[0]); + close(p[1]); + return NULL; + } + + pid = fork(); + switch (pid) { + case -1: + fclose(f); + close(p[0]); + close(p[1]); + return NULL; + case 0: + dup2(p[1-op], 1-op); + close(p[0]); + close(p[1]); + execl("/bin/sh", "sh", "-c", cmd, (char *)0); + _exit(127); + } + close(p[1-op]); + f->pipe_pid = pid; + return f; +} diff --git a/05/musl-final/src/stdio/printf.c b/05/musl-final/src/stdio/printf.c new file mode 100644 index 0000000..efeb8b3 --- /dev/null +++ b/05/musl-final/src/stdio/printf.c @@ -0,0 +1,12 @@ +#include +#include + +int printf(const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vprintf(fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/putc.c b/05/musl-final/src/stdio/putc.c new file mode 100644 index 0000000..3c9dc11 --- /dev/null +++ b/05/musl-final/src/stdio/putc.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +int putc(int c, FILE *f) +{ + return fputc(c, f); +} + +weak_alias(putc, _IO_putc); diff --git a/05/musl-final/src/stdio/putc_unlocked.c b/05/musl-final/src/stdio/putc_unlocked.c new file mode 100644 index 0000000..f01da71 --- /dev/null +++ b/05/musl-final/src/stdio/putc_unlocked.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +int putc_unlocked(int c, FILE *f) +{ + return f->wpos < f->wstop ? (*f->wpos++ = c) : __overflow(f, c); +} + +weak_alias(putc_unlocked, fputc_unlocked); diff --git a/05/musl-final/src/stdio/putchar.c b/05/musl-final/src/stdio/putchar.c new file mode 100644 index 0000000..945636d --- /dev/null +++ b/05/musl-final/src/stdio/putchar.c @@ -0,0 +1,6 @@ +#include + +int putchar(int c) +{ + return fputc(c, stdout); +} diff --git a/05/musl-final/src/stdio/putchar_unlocked.c b/05/musl-final/src/stdio/putchar_unlocked.c new file mode 100644 index 0000000..72d47d1 --- /dev/null +++ b/05/musl-final/src/stdio/putchar_unlocked.c @@ -0,0 +1,7 @@ +#include "stdio_impl.h" + +int putchar_unlocked(int c) +{ + return stdout->wpos < stdout->wstop ? + (*stdout->wpos++ = c) : __overflow(stdout, c); +} diff --git a/05/musl-final/src/stdio/puts.c b/05/musl-final/src/stdio/puts.c new file mode 100644 index 0000000..eb70efd --- /dev/null +++ b/05/musl-final/src/stdio/puts.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +int puts(const char *s) +{ + return -(fputs(s, stdout) < 0 || putchar('\n') < 0); +} diff --git a/05/musl-final/src/stdio/putw.c b/05/musl-final/src/stdio/putw.c new file mode 100644 index 0000000..137832e --- /dev/null +++ b/05/musl-final/src/stdio/putw.c @@ -0,0 +1,6 @@ +#include + +int putw(int x, FILE *f) +{ + return fwrite(&x, sizeof x, 1, f) ? x : EOF; +} diff --git a/05/musl-final/src/stdio/putwc.c b/05/musl-final/src/stdio/putwc.c new file mode 100644 index 0000000..80b54a4 --- /dev/null +++ b/05/musl-final/src/stdio/putwc.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +wint_t putwc(wchar_t c, FILE *f) +{ + return fputwc(c, f); +} diff --git a/05/musl-final/src/stdio/putwchar.c b/05/musl-final/src/stdio/putwchar.c new file mode 100644 index 0000000..3aacc1c --- /dev/null +++ b/05/musl-final/src/stdio/putwchar.c @@ -0,0 +1,8 @@ +#include "stdio_impl.h" + +wint_t putwchar(wchar_t c) +{ + return fputwc(c, stdout); +} + +weak_alias(putwchar, putwchar_unlocked); diff --git a/05/musl-final/src/stdio/remove.c b/05/musl-final/src/stdio/remove.c new file mode 100644 index 0000000..8e33827 --- /dev/null +++ b/05/musl-final/src/stdio/remove.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int remove(const char *path) +{ + return __syscall_unlink(path); +} diff --git a/05/musl-final/src/stdio/rename.c b/05/musl-final/src/stdio/rename.c new file mode 100644 index 0000000..4eced08 --- /dev/null +++ b/05/musl-final/src/stdio/rename.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int rename(const char *old, const char *new) +{ + return syscall2(__NR_rename, (long)old, (long)new); +} diff --git a/05/musl-final/src/stdio/rewind.c b/05/musl-final/src/stdio/rewind.c new file mode 100644 index 0000000..7944b43 --- /dev/null +++ b/05/musl-final/src/stdio/rewind.c @@ -0,0 +1,6 @@ +#include + +void rewind(FILE *f) +{ + fseek(f, 0, SEEK_SET); +} diff --git a/05/musl-final/src/stdio/scanf.c b/05/musl-final/src/stdio/scanf.c new file mode 100644 index 0000000..a04a440 --- /dev/null +++ b/05/musl-final/src/stdio/scanf.c @@ -0,0 +1,12 @@ +#include +#include + +int scanf(const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vscanf(fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/setbuf.c b/05/musl-final/src/stdio/setbuf.c new file mode 100644 index 0000000..205afcf --- /dev/null +++ b/05/musl-final/src/stdio/setbuf.c @@ -0,0 +1,6 @@ +#include "stdio_impl.h" + +void setbuf(FILE *f, char *buf) +{ + setvbuf(f, buf, buf ? _IOFBF : _IONBF, BUFSIZ); +} diff --git a/05/musl-final/src/stdio/setvbuf.c b/05/musl-final/src/stdio/setvbuf.c new file mode 100644 index 0000000..2985d3f --- /dev/null +++ b/05/musl-final/src/stdio/setvbuf.c @@ -0,0 +1,22 @@ +#include "stdio_impl.h" + +/* This function makes no attempt to protect the user from his/her own + * stupidity. If called any time but when then ISO C standard specifically + * allows it, all hell can and will break loose, especially with threads! + * + * This implementation ignores all arguments except the buffering type, + * and uses the existing buffer allocated alongside the FILE object. + * In the case of stderr where the preexisting buffer is length 1, it + * is not possible to set line buffering or full buffering. */ + +int setvbuf(FILE *f, char *buf, int type, size_t size) +{ + f->lbf = EOF; + + if (type == _IONBF) + f->buf_size = 1; + else if (type == _IOLBF) + f->lbf = '\n'; + + return 0; +} diff --git a/05/musl-final/src/stdio/snprintf.c b/05/musl-final/src/stdio/snprintf.c new file mode 100644 index 0000000..4071c2f --- /dev/null +++ b/05/musl-final/src/stdio/snprintf.c @@ -0,0 +1,13 @@ +#include +#include + +int snprintf(char *s, size_t n, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsnprintf(s, n, fmt, ap); + va_end(ap); + return ret; +} + diff --git a/05/musl-final/src/stdio/sprintf.c b/05/musl-final/src/stdio/sprintf.c new file mode 100644 index 0000000..6b22540 --- /dev/null +++ b/05/musl-final/src/stdio/sprintf.c @@ -0,0 +1,12 @@ +#include +#include + +int sprintf(char *s, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsprintf(s, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/sscanf.c b/05/musl-final/src/stdio/sscanf.c new file mode 100644 index 0000000..a1cea69 --- /dev/null +++ b/05/musl-final/src/stdio/sscanf.c @@ -0,0 +1,12 @@ +#include +#include + +int sscanf(const char *s, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsscanf(s, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/stderr.c b/05/musl-final/src/stdio/stderr.c new file mode 100644 index 0000000..4a79d4e --- /dev/null +++ b/05/musl-final/src/stdio/stderr.c @@ -0,0 +1,13 @@ +#include "stdio_impl.h" + +static unsigned char buf[1+UNGET]; +static FILE f = { + .buf = buf+UNGET, + .buf_size = 1, + .fd = 2, + .flags = F_PERM | F_NORD, + .write = __stdio_write, + .seek = __stdio_seek, + .close = __stdio_close, +}; +FILE *const stderr = &f; diff --git a/05/musl-final/src/stdio/stdin.c b/05/musl-final/src/stdio/stdin.c new file mode 100644 index 0000000..51c9923 --- /dev/null +++ b/05/musl-final/src/stdio/stdin.c @@ -0,0 +1,13 @@ +#include "stdio_impl.h" + +static unsigned char buf[BUFSIZ+UNGET]; +static FILE f = { + .buf = buf+UNGET, + .buf_size = sizeof buf-UNGET, + .fd = 0, + .flags = F_PERM | F_NOWR, + .read = __stdio_read, + .seek = __stdio_seek, + .close = __stdio_close, +}; +FILE *const stdin = &f; diff --git a/05/musl-final/src/stdio/stdout.c b/05/musl-final/src/stdio/stdout.c new file mode 100644 index 0000000..bf6eea6 --- /dev/null +++ b/05/musl-final/src/stdio/stdout.c @@ -0,0 +1,17 @@ +#include "stdio_impl.h" + +static unsigned char buf[BUFSIZ+UNGET]; +static FILE f = { + .buf = buf+UNGET, + .buf_size = sizeof buf-UNGET, + .fd = 1, + .flags = F_PERM | F_NORD, + .lbf = '\n', + .write = __stdio_write, + .seek = __stdio_seek, + .close = __stdio_close, +}; +FILE *const stdout = &f; + +/* overrides symbol in fflush.c, used for flushing NULL */ +FILE *const __stdout_to_flush = &f; diff --git a/05/musl-final/src/stdio/swscanf.c b/05/musl-final/src/stdio/swscanf.c new file mode 100644 index 0000000..b66ad03 --- /dev/null +++ b/05/musl-final/src/stdio/swscanf.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int swscanf(const wchar_t *s, const wchar_t *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vswscanf(s, fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdio/tempnam.c b/05/musl-final/src/stdio/tempnam.c new file mode 100644 index 0000000..2cbcb86 --- /dev/null +++ b/05/musl-final/src/stdio/tempnam.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +char *tempnam(const char *dir, const char *pfx) +{ + static int lock; + static int index; + char *s; + int pid = getpid(); + int l; + + if (!dir) dir = P_tmpdir; + if (!pfx) pfx = "temp"; + + if (access(dir, R_OK|W_OK|X_OK) != 0) + return NULL; + + l = strlen(dir) + 1 + strlen(pfx) + 2 + sizeof(int)*3*2 + 1; + s = malloc(l); + if (!s) { + errno = ENOMEM; + return NULL; + } + + LOCK(&lock); + for (; index < TMP_MAX; index++) { + snprintf(s, l, "%s/%s-%d-%d", dir, pfx, pid, index); + if (access(s, F_OK) != 0) { + UNLOCK(&lock); + return s; + } + } + UNLOCK(&lock); + free(s); + return NULL; +} diff --git a/05/musl-final/src/stdio/tmpfile.c b/05/musl-final/src/stdio/tmpfile.c new file mode 100644 index 0000000..185025f --- /dev/null +++ b/05/musl-final/src/stdio/tmpfile.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include "stdio_impl.h" + +FILE *tmpfile(void) +{ + char buf[L_tmpnam], *s; + int fd; + FILE *f; + for (;;) { + s = tmpnam(buf); + if (!s) return NULL; + fd = __syscall_open(s, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + f = __fdopen(fd, "w+"); + remove(s); + return f; + } + } +} + +LFS64(tmpfile); diff --git a/05/musl-final/src/stdio/tmpnam.c b/05/musl-final/src/stdio/tmpnam.c new file mode 100644 index 0000000..14d5922 --- /dev/null +++ b/05/musl-final/src/stdio/tmpnam.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include "libc.h" + +char *tmpnam(char *s) +{ + static int lock; + static int index; + static char *s2; + int pid = getpid(); + char *dir = getenv("TMPDIR"); + + if (!s) { + if (!s2) s2 = malloc(L_tmpnam); + s = s2; + } + + /* this interface is insecure anyway but at least we can try.. */ + if (!dir || strlen(dir) > L_tmpnam-32) + dir = P_tmpdir; + + if (access(dir, R_OK|W_OK|X_OK) != 0) + return NULL; + + LOCK(&lock); + for (index++; index < TMP_MAX; index++) { + snprintf(s, L_tmpnam, "%s/temp%d-%d", dir, pid, index); + if (access(s, F_OK) != 0) { + UNLOCK(&lock); + return s; + } + } + UNLOCK(&lock); + return NULL; +} diff --git a/05/musl-final/src/stdio/ungetc.c b/05/musl-final/src/stdio/ungetc.c new file mode 100644 index 0000000..0718168 --- /dev/null +++ b/05/musl-final/src/stdio/ungetc.c @@ -0,0 +1,33 @@ +#include "stdio_impl.h" + +int ungetc(int c, FILE *f) +{ + if (c == EOF) return c; + + FLOCK(f); + + /* Fail if unreadable or writing and unable to flush */ + if ((f->flags & (F_ERR|F_NORD)) || (f->wpos && __oflow(f))) { + FUNLOCK(f); + return EOF; + } + + /* Clear write mode */ + f->wbase = f->wpos = f->wstop = f->wend = 0; + + /* Put the file in read mode */ + if (!f->rpos) f->rpos = f->rend = f->buf; + + /* If unget buffer is already full, fail. */ + if (f->rpos <= f->buf - UNGET) { + FUNLOCK(f); + return EOF; + } + + /* Put a byte back into the buffer */ + *--f->rpos = c; + f->flags &= ~F_EOF; + + FUNLOCK(f); + return c; +} diff --git a/05/musl-final/src/stdio/ungetwc.c b/05/musl-final/src/stdio/ungetwc.c new file mode 100644 index 0000000..f7cde2e --- /dev/null +++ b/05/musl-final/src/stdio/ungetwc.c @@ -0,0 +1,45 @@ +#include "stdio_impl.h" + +wint_t ungetwc(wint_t c, FILE *f) +{ + unsigned char mbc[MB_LEN_MAX]; + int l=1; + + if (c == WEOF) return c; + + /* Try conversion early so we can fail without locking if invalid */ + if (!isascii(c) && (l = wctomb(mbc, c)) < 0) + return WEOF; + + FLOCK(f); + + f->mode |= f->mode+1; + + /* Fail if unreadable or writing and unable to flush */ + if ((f->flags & (F_ERR|F_NORD)) || (f->wpos && __oflow(f))) { + FUNLOCK(f); + return EOF; + } + + /* Clear write mode */ + f->wpos = f->wstop = f->wend = 0; + + /* Put the file in read mode */ + if (!f->rpos) f->rpos = f->rend = f->buf; + + /* If unget buffer is nonempty, fail. */ + if (f->rpos < f->buf) { + FUNLOCK(f); + return WEOF; + } + + /* Put character back into the buffer */ + if (isascii(c)) *--f->rpos = c; + else memcpy(f->rpos -= l, mbc, l); + + /* Clear EOF */ + f->flags &= ~F_EOF; + + FUNLOCK(f); + return c; +} diff --git a/05/musl-final/src/stdio/vasprintf.c b/05/musl-final/src/stdio/vasprintf.c new file mode 100644 index 0000000..f2bbc7a --- /dev/null +++ b/05/musl-final/src/stdio/vasprintf.c @@ -0,0 +1,27 @@ +#include +#include +#include + +#define GUESS 240U + +int vasprintf(char **s, const char *fmt, va_list ap) +{ + va_list ap2; + char *a; + int l=GUESS; + + if (!(a=malloc(GUESS))) return -1; + + va_copy(ap2, ap); + l=vsnprintf(a, GUESS, fmt, ap2); + va_end(ap2); + + if (l(b) ? (a) : (b)) +#define MIN(a,b) ((a)<(b) ? (a) : (b)) +#define CONCAT2(x,y) x ## y +#define CONCAT(x,y) CONCAT2(x,y) + +/* Convenient bit representation for modifier flags, which all fall + * within 31 codepoints of the space character. */ + +#define ALT_FORM (1U<<'#'-' ') +#define ZERO_PAD (1U<<'0'-' ') +#define LEFT_ADJ (1U<<'-'-' ') +#define PAD_POS (1U<<' '-' ') +#define MARK_POS (1U<<'+'-' ') +#define GROUPED (1U<<'\''-' ') + +#define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED) + +#if UINT_MAX == ULONG_MAX +#define LONG_IS_INT +#endif + +#if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX +#define ODD_TYPES +#endif + +/* State machine to accept length modifiers + conversion specifiers. + * Result is 0 on failure, or an argument type to pop on success. */ + +enum { + BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE, + ZTPRE, JPRE, + STOP, + PTR, INT, UINT, ULLONG, +#ifndef LONG_IS_INT + LONG, ULONG, +#else +#define LONG INT +#define ULONG UINT +#endif + SHORT, USHORT, CHAR, UCHAR, +#ifdef ODD_TYPES + LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR, +#else +#define LLONG ULLONG +#define SIZET ULONG +#define IMAX LLONG +#define UMAX ULLONG +#define PDIFF LONG +#define UIPTR ULONG +#endif + DBL, LDBL, + NOARG, + MAXSTATE +}; + +#define S(x) [(x)-'A'] + +static const unsigned char states[]['z'-'A'+1] = { + { /* 0: bare types */ + S('d') = INT, S('i') = INT, + S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, + S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, + S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, + S('c') = CHAR, S('C') = INT, + S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR, + S('m') = NOARG, + S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE, + S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE, + }, { /* 1: l-prefixed */ + S('d') = LONG, S('i') = LONG, + S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, + S('c') = INT, S('s') = PTR, S('n') = PTR, + S('l') = LLPRE, + }, { /* 2: ll-prefixed */ + S('d') = LLONG, S('i') = LLONG, + S('o') = ULLONG, S('u') = ULLONG, + S('x') = ULLONG, S('X') = ULLONG, + S('n') = PTR, + }, { /* 3: h-prefixed */ + S('d') = SHORT, S('i') = SHORT, + S('o') = USHORT, S('u') = USHORT, + S('x') = USHORT, S('X') = USHORT, + S('n') = PTR, + S('h') = HHPRE, + }, { /* 4: hh-prefixed */ + S('d') = CHAR, S('i') = CHAR, + S('o') = UCHAR, S('u') = UCHAR, + S('x') = UCHAR, S('X') = UCHAR, + S('n') = PTR, + }, { /* 5: L-prefixed */ + S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL, + S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL, + S('n') = PTR, + }, { /* 6: z- or t-prefixed (assumed to be same size) */ + S('d') = PDIFF, S('i') = PDIFF, + S('o') = SIZET, S('u') = SIZET, + S('x') = SIZET, S('X') = SIZET, + S('n') = PTR, + }, { /* 7: j-prefixed */ + S('d') = IMAX, S('i') = IMAX, + S('o') = UMAX, S('u') = UMAX, + S('x') = UMAX, S('X') = UMAX, + S('n') = PTR, + } +}; + +#define OOB(x) ((unsigned)(x)-'A' > 'z'-'A') + +union arg +{ + uintmax_t i; + long double f; + void *p; +}; + +static void pop_arg(union arg *arg, int type, va_list *ap) +{ + /* Give the compiler a hint for optimizing the switch. */ + if ((unsigned)type > MAXSTATE) return; + switch (type) { + case PTR: arg->p = va_arg(*ap, void *); + break; case INT: arg->i = va_arg(*ap, int); + break; case UINT: arg->i = va_arg(*ap, unsigned int); +#ifndef LONG_IS_INT + break; case LONG: arg->i = va_arg(*ap, long); + break; case ULONG: arg->i = va_arg(*ap, unsigned long); +#endif + break; case ULLONG: arg->i = va_arg(*ap, unsigned long long); + break; case SHORT: arg->i = (short)va_arg(*ap, int); + break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int); + break; case CHAR: arg->i = (signed char)va_arg(*ap, int); + break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int); +#ifdef ODD_TYPES + break; case LLONG: arg->i = va_arg(*ap, long long); + break; case SIZET: arg->i = va_arg(*ap, size_t); + break; case IMAX: arg->i = va_arg(*ap, intmax_t); + break; case UMAX: arg->i = va_arg(*ap, uintmax_t); + break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t); + break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *); +#endif + break; case DBL: arg->f = va_arg(*ap, double); + break; case LDBL: arg->f = va_arg(*ap, long double); + } +} + +static void out(FILE *f, const char *s, size_t l) +{ + __fwritex(s, l, f); +} + +static void pad(FILE *f, char c, int w, int l, int fl) +{ + char pad[256]; + if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return; + l = w - l; + memset(pad, c, l>sizeof pad ? sizeof pad : l); + for (; l >= sizeof pad; l -= sizeof pad) + out(f, pad, sizeof pad); + out(f, pad, l); +} + +static const char xdigits[16] = { + "0123456789ABCDEF" +}; + +static char *fmt_x(uintmax_t x, char *s, int lower) +{ + for (; x; x>>=4) *--s = xdigits[(x&15)]|lower; + return s; +} + +static char *fmt_o(uintmax_t x, char *s) +{ + for (; x; x>>=3) *--s = '0' + (x&7); + return s; +} + +static char *fmt_u(uintmax_t x, char *s) +{ + unsigned long y; + for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10; + for (y=x; y; y/=10) *--s = '0' + y%10; + return s; +} + +static long double negate_ld(long double d) { + register unsigned long long *p = (unsigned long long *)&d; + p[1] ^= 1ul<<15; + return *(long double *)p; +} + +static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t) +{ + uint32_t big[(LDBL_MAX_EXP+LDBL_MANT_DIG)/9+1]; + uint32_t *a, *d, *r, *z; + int e2=0, e, i, j, l; + char buf[9+LDBL_MANT_DIG/4], *s; + const char *prefix="-+ "; + int pl; + char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr; + + pl=1; + if (y<0 || 1/y<0) { + y=negate_ld(y); + } else if (fl & MARK_POS) { + prefix++; + } else if (fl & PAD_POS) { + prefix+=2; + } else pl=0; + + if (!isfinite(y)) { + char *s = (t&32)?"inf":"INF"; + if (y!=y) s=(t&32)?"nan":"NAN", pl=0; + pad(f, ' ', w, 3+pl, fl&~ZERO_PAD); + out(f, prefix, pl); + out(f, s, 3); + pad(f, ' ', w, 3+pl, fl^LEFT_ADJ); + return MAX(w, 3+pl); + } + + y = frexpl(y, &e2) * 2; + if (y) e2--; + + if ((t|32)=='a') { + long double round = 8.0; + int re; + + if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0; + else re=LDBL_MANT_DIG/4-1-p; + + if (re) { + if (pl && *prefix=='-') y=negate_ld(y); + while (re--) round*=16; + y+=round; + y-=round; + if (y<0) y=negate_ld(y); + } + + estr=fmt_u(e2<0 ? -e2 : e2, ebuf); + if (estr==ebuf) *--estr='0'; + *--estr = (e2<0 ? '-' : '+'); + *--estr = t+('p'-'a'); + + s=buf; + *s++='0'; + *s++=t+('x'-'a'); + do { + int x=y; + *s++=xdigits[x]|(t&32); + y=16*(y-x); + if (s-buf==3 && (y||p>0||(fl&ALT_FORM))) *s++='.'; + } while (y); + + if (p<0) p = s-buf-4; + l = 1 + p + (p || (fl&ALT_FORM)) + ebuf-estr; + + pad(f, ' ', w, pl+l, fl); + out(f, prefix, pl); + pad(f, '0', w, pl+l, fl^ZERO_PAD); + out(f, buf, s-buf); + pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0); + out(f, estr, ebuf-estr); + pad(f, '0', w, pl+l, fl^LEFT_ADJ); + return MAX(w, pl+l); + } + if (p<0) p=6; + + y *= 0x1p28; e2-=28; + + if (e2<0) a=r=z=big; + else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1; + + do { + *z = y; + y = 1000000000*(y-*z++); + } while (y); + + while (e2>0) { + uint32_t carry=0; + int sh=MIN(29,e2); + for (d=z-1; d>=a; d--) { + uint64_t x = ((uint64_t)*d<a) z--; + if (carry) *--a = carry; + e2-=sh; + } + while (e2<0) { + uint32_t carry=0, *z2; + int sh=MIN(9,-e2); + for (d=a; d>sh) + carry; + carry = (1000000000>>sh) * rm; + } + if (!*a) a++; + if (carry) *z++ = carry; + /* Avoid (slow!) computation past requested precision */ + z2 = ((t|32)=='f' ? r : a) + 2 + p/9; + z = MIN(z, z2); + e2+=sh; + } + + if (a=i; i*=10, e++); + else e=0; + + /* Perform rounding: j is precision after the radix (possibly neg) */ + j = p - ((t|32)!='f')*e - ((t|32)=='g'); + if (j < 9*(z-r-1)) { + uint32_t x; + /* We avoid C's broken division of negative numbers */ + d = r + 1 + (j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP; + j += 9*LDBL_MAX_EXP; + j %= 9; + for (i=10, j++; j<9; i*=10, j++); + x = *d % i; + /* Are there any significant digits past j? */ + if (x || d+1!=z) { + long double round = CONCAT(0x1p,LDBL_MANT_DIG); + long double small; + if (x 999999999) { + *d--=0; + (*d)++; + } + if (d=i; i*=10, e++); + } + } + for (; !z[-1] && z>a; z--); + } + + if ((t|32)=='g') { + if (!p) p++; + if (p>e && e>=-4) { + t--; + p-=e+1; + } else { + t-=2; + p--; + } + if (!(fl&ALT_FORM)) { + /* Count trailing zeros in last place */ + if (z>a) for (i=10, j=0; z[-1]%i==0; i*=10, j++); + else j=9; + if ((t|32)=='f') + p = MIN(p,MAX(0,9*(z-r-1)-j)); + else + p = MIN(p,MAX(0,9*(z-r-1)+e-j)); + } + } + l = 1 + p + (p || (fl&ALT_FORM)); + if ((t|32)=='f') { + if (e>0) l+=e; + } else { + estr=fmt_u(e<0 ? -e : e, ebuf); + while(ebuf-estr<2) *--estr='0'; + *--estr = (e<0 ? '-' : '+'); + *--estr = t; + l += ebuf-estr; + } + + pad(f, ' ', w, pl+l, fl); + out(f, prefix, pl); + pad(f, '0', w, pl+l, fl^ZERO_PAD); + + if ((t|32)=='f') { + if (a>r) a=r; + for (d=a; d<=r; d++) { + char *s = fmt_u(*d, buf+9); + if (d!=a) while (s>buf) *--s='0'; + else if (s==buf+9) *--s='0'; + out(f, s, buf+9-s); + } + if (p || (fl&ALT_FORM)) out(f, ".", 1); + for (; d0; d++, p-=9) { + char *s = fmt_u(*d, buf+9); + while (s>buf) *--s='0'; + out(f, s, MIN(9,p)); + } + pad(f, '0', p+9, 9, 0); + } else { + if (z<=a) z=a+1; + for (d=a; d=0; d++) { + char *s = fmt_u(*d, buf+9); + if (s==buf+9) *--s='0'; + if (d!=a) while (s>buf) *--s='0'; + else { + out(f, s++, 1); + if (p>0||(fl&ALT_FORM)) out(f, ".", 1); + } + out(f, s, MIN(buf+9-s, p)); + p -= buf+9-s; + } + pad(f, '0', p+18, 18, 0); + out(f, estr, ebuf-estr); + } + + pad(f, ' ', w, pl+l, fl^LEFT_ADJ); + + return MAX(w, pl+l); +} + +static int getint(char **s) { + int i; + for (i=0; isdigit(**s); (*s)++) + i = 10*i + (**s-'0'); + return i; +} + +static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type) +{ + char *a, *z, *s=(char *)fmt; + unsigned l10n=0, litpct, fl; + int w, p; + union arg arg; + int argpos; + unsigned st, ps; + int cnt=0, l=0; + int i; + char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; + const char *prefix; + int t, pl; + wchar_t wc[2], *ws; + char mb[4]; + + for (;;) { + /* Update output count, end loop when fmt is exhausted */ + if (cnt >= 0) { + if (l > INT_MAX - cnt) { + if (!ferror(f)) errno = EOVERFLOW; + cnt = -1; + } else cnt += l; + } + if (!*s) break; + + /* Handle literal text and %% format specifiers */ + for (a=s; *s && *s!='%'; s++); + litpct = strspn(s, "%")/2; /* Optimize %%%% runs */ + z = s+litpct; + s += 2*litpct; + l = z-a; + if (f) out(f, a, l); + if (l) continue; + + if (isdigit(s[1]) && s[2]=='$') { + l10n=1; + argpos = s[1]-'0'; + s+=3; + } else { + argpos = -1; + s++; + } + + /* Read modifier flags */ + for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++) + fl |= 1U<<*s-' '; + + /* Read field width */ + if (*s=='*') { + if (isdigit(s[1]) && s[2]=='$') { + l10n=1; + nl_type[s[1]-'0'] = INT; + w = nl_arg[s[1]-'0'].i; + s+=3; + } else if (!l10n) { + w = f ? va_arg(*ap, int) : 0; + s++; + } else return -1; + if (w<0) fl|=LEFT_ADJ, w=-w; + } else if ((w=getint(&s))<0) return -1; + + /* Read precision */ + if (*s=='.' && s[1]=='*') { + if (isdigit(s[2]) && s[3]=='$') { + nl_type[s[2]-'0'] = INT; + p = nl_arg[s[2]-'0'].i; + s+=4; + } else if (!l10n) { + p = f ? va_arg(*ap, int) : 0; + s+=2; + } else return -1; + } else if (*s=='.') { + s++; + p = getint(&s); + } else p = -1; + + /* Format specifier state machine */ + st=0; + do { + if (OOB(*s)) return -1; + ps=st; + st=states[st]S(*s++); + } while (st-1=0) return -1; + else if (!f) continue; + } else { + if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos]; + else if (f) pop_arg(&arg, st, ap); + else return 0; + } + + if (!f) continue; + + z = buf + sizeof(buf); + prefix = "-+ 0X0x"; + pl = 0; + t = s[-1]; + + /* Transform ls,lc -> S,C */ + if (ps && (t&15)==3) t&=~32; + + /* - and 0 flags are mutually exclusive */ + if (fl & LEFT_ADJ) fl &= ~ZERO_PAD; + + switch(t) { + case 'n': + switch(ps) { + case BARE: *(int *)arg.p = l; break; + case LPRE: *(long *)arg.p = l; break; + case LLPRE: *(long long *)arg.p = l; break; + case HPRE: *(unsigned short *)arg.p = l; break; + case HHPRE: *(unsigned char *)arg.p = l; break; + case ZTPRE: *(size_t *)arg.p = l; break; + case JPRE: *(uintmax_t *)arg.p = l; break; + } + continue; + case 'p': + p = MAX(p, 2*sizeof(void*)); + t = 'x'; + fl |= ALT_FORM; + case 'x': case 'X': + a = fmt_x(arg.i, z, t&32); + if (fl & ALT_FORM) prefix+=(t>>4), pl=2; + if (0) { + case 'o': + a = fmt_o(arg.i, z); + if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1; + } if (0) { + case 'd': case 'i': + pl=1; + if (arg.i>INTMAX_MAX) { + arg.i=-arg.i; + } else if (fl & MARK_POS) { + prefix++; + } else if (fl & PAD_POS) { + prefix+=2; + } else pl=0; + case 'u': + a = fmt_u(arg.i, z); + } + if (!arg.i && !p) continue; + if (p>=0) fl &= ~ZERO_PAD; + p = MAX(p, z-a + !arg.i); + break; + case 'c': + *(a=z-(p=1))=arg.i; + fl &= ~ZERO_PAD; + break; + case 'm': + if (1) a = strerror(errno); else + case 's': + a = arg.p; + z = memchr(a, 0, p); + if (!z) z=a+p; + else p=z-a; + fl &= ~ZERO_PAD; + break; + case 'C': + wc[0] = arg.i; + wc[1] = 0; + arg.p = wc; + p = -1; + case 'S': + ws = arg.p; + for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l); + if (l<0) return -1; + p = i; + pad(f, ' ', w, p, fl); + ws = arg.p; + for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l) + out(f, mb, l); + pad(f, ' ', w, p, fl^LEFT_ADJ); + l = w>p ? w : p; + continue; + case 'e': case 'f': case 'g': case 'a': + case 'E': case 'F': case 'G': case 'A': + l = fmt_fp(f, arg.f, w, p, fl, t); + continue; + } + + if (p < z-a) p = z-a; + if (w < pl+p) w = pl+p; + + pad(f, ' ', w, pl+p, fl); + out(f, prefix, pl); + pad(f, '0', w, pl+p, fl^ZERO_PAD); + pad(f, '0', p, z-a, 0); + out(f, a, z-a); + pad(f, ' ', w, pl+p, fl^LEFT_ADJ); + + l = w; + } + + if (f) return cnt; + if (!l10n) return 0; + + for (i=1; i<=NL_ARGMAX && nl_type[i]; i++) + pop_arg(nl_arg+i, nl_type[i], ap); + for (; i<=NL_ARGMAX && !nl_type[i]; i++); + if (i<=NL_ARGMAX) return -1; + return 1; +} + +int vfprintf(FILE *f, const char *fmt, va_list ap) +{ + va_list ap2; + int nl_type[NL_ARGMAX] = {0}; + union arg nl_arg[NL_ARGMAX]; + int ret; + + va_copy(ap2, ap); + if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1; + + FLOCK(f); + ret = printf_core(f, fmt, &ap2, nl_arg, nl_type); + FUNLOCK(f); + va_end(ap2); + return ret; +} diff --git a/05/musl-final/src/stdio/vfscanf.c b/05/musl-final/src/stdio/vfscanf.c new file mode 100644 index 0000000..69f4508 --- /dev/null +++ b/05/musl-final/src/stdio/vfscanf.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +#include "stdio_impl.h" +#include "__scanf.h" + +static void f_read(rctx_t *r) +{ + FILE *f = r->opaque; + if ((r->c = __uflow(f)) >= 0) r->l++; +} + +int vfscanf(FILE *f, const char *fmt, va_list ap) +{ + size_t l = strlen(fmt), i, result; + rctx_t r = { f_read, (void *)f, 0, isspace }; + wchar_t fmt2[l+1]; + + if (l > 0x100000) { + errno = ENOMEM; + return -1; + } + for (i=0; i<=l; i++) fmt2[i] = (unsigned char)fmt[i]; + + FLOCK(f); + + result = __scanf(&r, fmt2, ap); + + if (r.u && r.c >= 0) { + /* This code takes care of the case where the caller performs + * a nonmatching scanf to leave a character in the unscan + * buffer, followed by an unget, followed by a scanf that + * matches zero characters. In this case the final 'unread' + * character must be returned to the unget buffer rather than + * the unscan buffer. */ + f->rpos--; + } + + FUNLOCK(f); + return result; +} diff --git a/05/musl-final/src/stdio/vfwscanf.c b/05/musl-final/src/stdio/vfwscanf.c new file mode 100644 index 0000000..491c140 --- /dev/null +++ b/05/musl-final/src/stdio/vfwscanf.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include + +#include "stdio_impl.h" +#include "__scanf.h" + +static void f_read(rctx_t *r) +{ + FILE *f = r->opaque; + if ((r->c = fgetwc(f)) >= 0) r->l++; +} + +int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap) +{ + rctx_t r = { f_read, (void *)f, 1, iswspace }; + int result; + + result = __scanf(&r, fmt, ap); + + if (r.u && r.c >= 0) { + ungetwc(r.c, f); + } + + return result; +} diff --git a/05/musl-final/src/stdio/vprintf.c b/05/musl-final/src/stdio/vprintf.c new file mode 100644 index 0000000..67b38da --- /dev/null +++ b/05/musl-final/src/stdio/vprintf.c @@ -0,0 +1,6 @@ +#include + +int vprintf(const char *fmt, va_list ap) +{ + return vfprintf(stdout, fmt, ap); +} diff --git a/05/musl-final/src/stdio/vscanf.c b/05/musl-final/src/stdio/vscanf.c new file mode 100644 index 0000000..6f55b1c --- /dev/null +++ b/05/musl-final/src/stdio/vscanf.c @@ -0,0 +1,7 @@ +#include +#include + +int vscanf(const char *fmt, va_list ap) +{ + return vfscanf(stdin, fmt, ap); +} diff --git a/05/musl-final/src/stdio/vsnprintf.c b/05/musl-final/src/stdio/vsnprintf.c new file mode 100644 index 0000000..bda6b49 --- /dev/null +++ b/05/musl-final/src/stdio/vsnprintf.c @@ -0,0 +1,33 @@ +#include "stdio_impl.h" + +static size_t sn_write(FILE *f, const unsigned char *s, size_t l) +{ + /* pretend to succeed, but discard data */ + return l; +} + +int vsnprintf(char *s, size_t n, const char *fmt, va_list ap) +{ + int r; + FILE f; + unsigned char buf[1]; + + memset(&f, 0, sizeof(FILE)); + f.lbf = EOF; + f.write = sn_write; + f.buf_size = 1; + f.buf = buf; + if (n > INT_MAX) { + errno = EOVERFLOW; + return -1; + } else if (n > 0) { + if (n > (char *)0+SIZE_MAX-s) n = (char *)0+SIZE_MAX-s; + f.wpos = s; + f.wbase = f.wend = s+n-1; + f.wstop = f.wend - 1; + } + r = vfprintf(&f, fmt, ap); + /* wpos points just after last byte written, or to s+n-1 (wbase) */ + *f.wpos = 0; + return r; +} diff --git a/05/musl-final/src/stdio/vsprintf.c b/05/musl-final/src/stdio/vsprintf.c new file mode 100644 index 0000000..7836ccb --- /dev/null +++ b/05/musl-final/src/stdio/vsprintf.c @@ -0,0 +1,7 @@ +#include +#include + +int vsprintf(char *s, const char *fmt, va_list ap) +{ + return vsnprintf(s, INT_MAX, fmt, ap); +} diff --git a/05/musl-final/src/stdio/vsscanf.c b/05/musl-final/src/stdio/vsscanf.c new file mode 100644 index 0000000..fd48f70 --- /dev/null +++ b/05/musl-final/src/stdio/vsscanf.c @@ -0,0 +1,21 @@ +#include +#include +#include + +#include "__scanf.h" + +static void s_read(rctx_t *r) +{ + unsigned char *s = r->opaque; + if (!s[r->l]) r->c = -1; + else r->c = s[r->l++]; +} + +int vsscanf(const char *s, const char *fmt, va_list ap) +{ + size_t l = strlen(fmt), i; + wchar_t fmt2[l+1]; + rctx_t r = { s_read, (void *)s, 0, isspace }; + for (i=0; i<=l; i++) fmt2[i] = (unsigned char)fmt[i]; + return __scanf(&r, fmt2, ap); +} diff --git a/05/musl-final/src/stdio/vswscanf.c b/05/musl-final/src/stdio/vswscanf.c new file mode 100644 index 0000000..2c4ffbe --- /dev/null +++ b/05/musl-final/src/stdio/vswscanf.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +#include "__scanf.h" + +static void s_read(rctx_t *r) +{ + wchar_t *s = r->opaque; + if (!s[r->l]) r->c = -1; + else r->c = s[r->l++]; +} + +int vswscanf(const wchar_t *s, const wchar_t *fmt, va_list ap) +{ + rctx_t r = { s_read, (void *)s, 1, iswspace }; + return __scanf(&r, fmt, ap); +} diff --git a/05/musl-final/src/stdio/vwscanf.c b/05/musl-final/src/stdio/vwscanf.c new file mode 100644 index 0000000..86da045 --- /dev/null +++ b/05/musl-final/src/stdio/vwscanf.c @@ -0,0 +1,8 @@ +#include +#include +#include + +int vwscanf(const wchar_t *fmt, va_list ap) +{ + return vfwscanf(stdin, fmt, ap); +} diff --git a/05/musl-final/src/stdio/wscanf.c b/05/musl-final/src/stdio/wscanf.c new file mode 100644 index 0000000..34b5884 --- /dev/null +++ b/05/musl-final/src/stdio/wscanf.c @@ -0,0 +1,13 @@ +#include +#include +#include + +int wscanf(const wchar_t *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vwscanf(fmt, ap); + va_end(ap); + return ret; +} diff --git a/05/musl-final/src/stdlib/abs.c b/05/musl-final/src/stdlib/abs.c new file mode 100644 index 0000000..4806d62 --- /dev/null +++ b/05/musl-final/src/stdlib/abs.c @@ -0,0 +1,4 @@ +int abs(int a) +{ + return a>0 ? a : -a; +} diff --git a/05/musl-final/src/stdlib/atof.c b/05/musl-final/src/stdlib/atof.c new file mode 100644 index 0000000..f7fcd82 --- /dev/null +++ b/05/musl-final/src/stdlib/atof.c @@ -0,0 +1,6 @@ +#include + +double atof(const char *s) +{ + return strtod(s, 0); +} diff --git a/05/musl-final/src/stdlib/atoi.c b/05/musl-final/src/stdlib/atoi.c new file mode 100644 index 0000000..648b154 --- /dev/null +++ b/05/musl-final/src/stdlib/atoi.c @@ -0,0 +1,15 @@ +#include +#include + +int atoi(const char *s) +{ + int n=0, neg=0; + while (isspace(*s)) s++; + switch (*s) { + case '-': neg=1; + case '+': s++; + } + while (isdigit(*s)) + n = 10*n + *s++ - '0'; + return neg ? -n : n; +} diff --git a/05/musl-final/src/stdlib/atol.c b/05/musl-final/src/stdlib/atol.c new file mode 100644 index 0000000..9c91bba --- /dev/null +++ b/05/musl-final/src/stdlib/atol.c @@ -0,0 +1,16 @@ +#include +#include + +long atol(const char *s) +{ + long n=0; + int neg=0; + while (isspace(*s)) s++; + switch (*s) { + case '-': neg=1; + case '+': s++; + } + while (isdigit(*s)) + n = 10*n + *s++ - '0'; + return neg ? -n : n; +} diff --git a/05/musl-final/src/stdlib/atoll.c b/05/musl-final/src/stdlib/atoll.c new file mode 100644 index 0000000..0e03e0a --- /dev/null +++ b/05/musl-final/src/stdlib/atoll.c @@ -0,0 +1,16 @@ +#include +#include + +long long atoll(const char *s) +{ + long long n=0; + int neg=0; + while (isspace(*s)) s++; + switch (*s) { + case '-': neg=1; + case '+': s++; + } + while (isdigit(*s)) + n = 10*n + *s++ - '0'; + return neg ? -n : n; +} diff --git a/05/musl-final/src/stdlib/bsearch.c b/05/musl-final/src/stdlib/bsearch.c new file mode 100644 index 0000000..61d8936 --- /dev/null +++ b/05/musl-final/src/stdlib/bsearch.c @@ -0,0 +1,20 @@ +#include + +void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) +{ + void *try; + int sign; + while (nel > 0) { + try = (char *)base + width*(nel/2); + sign = cmp(key, try); + if (!sign) return try; + else if (nel == 1) break; + else if (sign < 0) + nel /= 2; + else { + base = try; + nel -= nel/2; + } + } + return NULL; +} diff --git a/05/musl-final/src/stdlib/div.c b/05/musl-final/src/stdlib/div.c new file mode 100644 index 0000000..e42c1f1 --- /dev/null +++ b/05/musl-final/src/stdlib/div.c @@ -0,0 +1,6 @@ +#include + +div_t div(int num, int den) +{ + return (div_t){ num/den, num%den }; +} diff --git a/05/musl-final/src/stdlib/frexp.c b/05/musl-final/src/stdlib/frexp.c new file mode 100644 index 0000000..ae82cb3 --- /dev/null +++ b/05/musl-final/src/stdlib/frexp.c @@ -0,0 +1,23 @@ +#include +#include + +double frexp(double x, int *e) +{ + union { double d; uint64_t i; } y = { x }; + int ee = y.i>>52 & 0x7ff; + + if (!ee) { + if (x) { + x = frexp(x*0x1p64, e); + *e -= 64; + } else *e = 0; + return x; + } else if (ee == 0x7ff) { + return x; + } + + *e = ee - 0x3fe; + y.i &= 0x800fffffffffffffull; + y.i |= 0x3fe0000000000000ull; + return y.d; +} diff --git a/05/musl-final/src/stdlib/frexpf.c b/05/musl-final/src/stdlib/frexpf.c new file mode 100644 index 0000000..ee5e910 --- /dev/null +++ b/05/musl-final/src/stdlib/frexpf.c @@ -0,0 +1,23 @@ +#include +#include + +float frexpf(float x, int *e) +{ + union { float f; uint32_t i; } y = { x }; + int ee = y.i>>23 & 0xff; + + if (!ee) { + if (x) { + x = frexpf(x*0x1p64, e); + *e -= 64; + } else *e = 0; + return x; + } else if (ee == 0xff) { + return x; + } + + *e = ee - 0x7e; + y.i &= 0x807ffffful; + y.i |= 0x3f000000ul; + return y.f; +} diff --git a/05/musl-final/src/stdlib/frexpl.c b/05/musl-final/src/stdlib/frexpl.c new file mode 100644 index 0000000..ecfff00 --- /dev/null +++ b/05/musl-final/src/stdlib/frexpl.c @@ -0,0 +1,25 @@ +#include +#include + +/* This version is for 80-bit little endian long double */ + +long double frexpl(long double x, int *e) +{ + union { long double ld; uint16_t hw[5]; } y = { x }; + int ee = y.hw[4]&0x7fff; + + if (!ee) { + if (x) { + x = frexpl(x*0x1p64, e); + *e -= 64; + } else *e = 0; + return x; + } else if (ee == 0x7fff) { + return x; + } + + *e = ee - 0x3ffe; + y.hw[4] &= 0x8000; + y.hw[4] |= 0x3ffe; + return y.ld; +} diff --git a/05/musl-final/src/stdlib/imaxabs.c b/05/musl-final/src/stdlib/imaxabs.c new file mode 100644 index 0000000..8100181 --- /dev/null +++ b/05/musl-final/src/stdlib/imaxabs.c @@ -0,0 +1,6 @@ +#include + +intmax_t imaxabs(intmax_t a) +{ + return a>0 ? a : -a; +} diff --git a/05/musl-final/src/stdlib/imaxdiv.c b/05/musl-final/src/stdlib/imaxdiv.c new file mode 100644 index 0000000..b2ce821 --- /dev/null +++ b/05/musl-final/src/stdlib/imaxdiv.c @@ -0,0 +1,6 @@ +#include + +imaxdiv_t imaxdiv(intmax_t num, intmax_t den) +{ + return (imaxdiv_t){ num/den, num%den }; +} diff --git a/05/musl-final/src/stdlib/labs.c b/05/musl-final/src/stdlib/labs.c new file mode 100644 index 0000000..675b95b --- /dev/null +++ b/05/musl-final/src/stdlib/labs.c @@ -0,0 +1,4 @@ +long labs(long a) +{ + return a>0 ? a : -a; +} diff --git a/05/musl-final/src/stdlib/ldiv.c b/05/musl-final/src/stdlib/ldiv.c new file mode 100644 index 0000000..36eb960 --- /dev/null +++ b/05/musl-final/src/stdlib/ldiv.c @@ -0,0 +1,6 @@ +#include + +ldiv_t ldiv(long num, long den) +{ + return (ldiv_t){ num/den, num%den }; +} diff --git a/05/musl-final/src/stdlib/llabs.c b/05/musl-final/src/stdlib/llabs.c new file mode 100644 index 0000000..bec4a03 --- /dev/null +++ b/05/musl-final/src/stdlib/llabs.c @@ -0,0 +1,4 @@ +long long llabs(long long a) +{ + return a>0 ? a : -a; +} diff --git a/05/musl-final/src/stdlib/lldiv.c b/05/musl-final/src/stdlib/lldiv.c new file mode 100644 index 0000000..7aaf7a0 --- /dev/null +++ b/05/musl-final/src/stdlib/lldiv.c @@ -0,0 +1,6 @@ +#include + +lldiv_t lldiv(long long num, long long den) +{ + return (lldiv_t){ num/den, num%den }; +} diff --git a/05/musl-final/src/stdlib/qsort.c b/05/musl-final/src/stdlib/qsort.c new file mode 100644 index 0000000..91a3361 --- /dev/null +++ b/05/musl-final/src/stdlib/qsort.c @@ -0,0 +1,50 @@ +#include +#include + +/* A simple heap sort implementation.. only in-place O(nlogn) sort I know. */ + +#define MIN(a, b) ((a)<(b) ? (a) : (b)) + +static void swap(char *a, char *b, size_t len) +{ + char tmp[256]; + size_t l; + while (len) { + l = MIN(sizeof tmp, len); + memcpy(tmp, a, l); + memcpy(a, b, l); + memcpy(b, tmp, l); + a += l; + b += l; + len -= l; + } +} + +static void sift(char *base, size_t root, size_t nel, size_t width, int (*cmp)(const void *, const void *)) +{ + size_t max; + + while (2*root <= nel) { + max = 2*root; + if (max < nel && cmp(base+max*width, base+(max+1)*width) < 0) + max++; + if (max && cmp(base+root*width, base+max*width) < 0) { + swap(base+root*width, base+max*width, width); + root = max; + } else break; + } +} + +void qsort(void *_base, size_t nel, size_t width, int (*cmp)(const void *, const void *)) +{ + char *base = _base; + size_t i; + + if (!nel) return; + for (i=(nel+1)/2; i; i--) + sift(base, i-1, nel-1, width, cmp); + for (i=nel-1; i; i--) { + swap(base, base+i*width, width); + sift(base, 0, i-1, width, cmp); + } +} diff --git a/05/musl-final/src/stdlib/strtod.c b/05/musl-final/src/stdlib/strtod.c new file mode 100644 index 0000000..388058f --- /dev/null +++ b/05/musl-final/src/stdlib/strtod.c @@ -0,0 +1,6 @@ +#include + +double strtod(const char *s, char **p) +{ + return strtold(s, p); +} diff --git a/05/musl-final/src/stdlib/strtof.c b/05/musl-final/src/stdlib/strtof.c new file mode 100644 index 0000000..07b32df --- /dev/null +++ b/05/musl-final/src/stdlib/strtof.c @@ -0,0 +1,6 @@ +#include + +float strtof(const char *s, char **p) +{ + return strtold(s, p); +} diff --git a/05/musl-final/src/stdlib/strtoimax.c b/05/musl-final/src/stdlib/strtoimax.c new file mode 100644 index 0000000..aeb0397 --- /dev/null +++ b/05/musl-final/src/stdlib/strtoimax.c @@ -0,0 +1,25 @@ +#include +#include +#include + +intmax_t strtoimax(const char *s1, char **p, int base) +{ + const unsigned char *s = (const void *)s1; + int sign = 0; + uintmax_t x; + + /* Initial whitespace */ + for (; isspace(*s); s++); + + /* Optional sign */ + if (*s == '-') sign = *s++; + else if (*s == '+') s++; + + x = strtoumax((const void *)s, p, base); + if (x > INTMAX_MAX) { + if (!sign || -x != INTMAX_MIN) + errno = ERANGE; + return sign ? INTMAX_MIN : INTMAX_MAX; + } + return sign ? -x : x; +} diff --git a/05/musl-final/src/stdlib/strtol.c b/05/musl-final/src/stdlib/strtol.c new file mode 100644 index 0000000..ace820a --- /dev/null +++ b/05/musl-final/src/stdlib/strtol.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +long strtol(const char *s, char **p, int base) +{ + intmax_t x = strtoimax(s, p, base); + if (x > LONG_MAX) { + errno = ERANGE; + return LONG_MAX; + } else if (x < LONG_MIN) { + errno = ERANGE; + return LONG_MIN; + } + return x; +} diff --git a/05/musl-final/src/stdlib/strtold.c b/05/musl-final/src/stdlib/strtold.c new file mode 100644 index 0000000..a504147 --- /dev/null +++ b/05/musl-final/src/stdlib/strtold.c @@ -0,0 +1,99 @@ +#include +#include +#include + +static long double negate_ld(long double d) { + register unsigned long long *p = (unsigned long long *)&d; + p[1] ^= 1ul<<15; + return *(long double *)p; +} + +long double strtold(const char *s1, char **p) +{ + const unsigned char *s = s1; + long double x = 0; + long double frac; + int sign = 0; + int nonzero = 0; + int radix = '.'; + long e; + + if (!p) p = (char **)&s1; + + /* Initial whitespace */ + for (; isspace(*s); s++); + + /* Optional sign */ + if (*s == '-') sign = *s++; + else if (*s == '+') s++; + + /* Handle infinities and NaNs. */ + if ((s[0]|32)=='i' && (s[1]|32)=='n' && (s[2]|32)=='f') { + *p = (char *)s + 3; + return sign ? -1.0/0.0 : 1.0/0.0; + } else if ((s[0]|32)=='n' && (s[1]|32)=='a' && (s[2]|32)=='n') { + *p = (char *)s + 3; + return 0.0/0.0; + } + + /* Possible hex float */ + if (s[0]=='0' && (s[1]|32)=='x') { + /* Mantissa must be non-degenerate */ + if (!isxdigit(s[2]) && (s[2]!=radix || !isxdigit(s[3]))) { + /* Decimal float 0, 'x' extraneous */ + *p = (char *)++s; + return 0; + } + /* We have a real hex float */ + s += 2; + for (; isxdigit(*s); s++) { + x = 16*x + (isdigit(*s)?*s-'0':(*s|32)-'a'); + if (*s!='0') nonzero=1; + } + if (*s == radix) { + frac = 1.0/16.0; + for (s++; isxdigit(*s); s++) { + x += frac * (isdigit(*s)?*s-'0':(*s|32)-'a'); + frac *= 1.0/16.0; + if (*s!='0') nonzero=1; + } + } + if ((*s|32) == 'p') { + e = strtol(s+1, (void *)&s, 10); + for (; e>0; e--) x *= 2.0; + for (; e<0; e++) x *= 0.5; + } + if ((nonzero && !x) || !(1.0/x)) + errno = ERANGE; + *p = (char *)s; + return sign ? negate_ld(x) : x; + } + + /* Mantissa must be non-degenerate */ + if (!isdigit(s[0]) && (s[0]!=radix || !isdigit(s[1]))) { + *p = (char *)s1; + return 0; + } + + for (; isdigit(*s); s++) { + x = 10*x + *s-'0'; + if (*s!='0') nonzero=1; + } + if (*s == radix) { + frac = 10.0; + for (s++; isdigit(*s); s++) { + x += (*s-'0') / frac; + frac *= 10.0; + if (*s!='0') nonzero=1; + } + } + if ((*s|32)=='e') { + e = strtol(++s, (void *)&s, 10); + for (; e>0; e--) x *= 10.0; + for (; e<0; e++) x /= 10.0; + } + if ((nonzero && !x) || !(1.0/x)) + errno = ERANGE; + *p = (char*)s; + return sign ? negate_ld(x) : x; +} diff --git a/05/musl-final/src/stdlib/strtoll.c b/05/musl-final/src/stdlib/strtoll.c new file mode 100644 index 0000000..9ab66fd --- /dev/null +++ b/05/musl-final/src/stdlib/strtoll.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +long long strtoll(const char *s, char **p, int base) +{ + intmax_t x = strtoimax(s, p, base); + if (x > LLONG_MAX) { + errno = ERANGE; + return LLONG_MAX; + } else if (x < LLONG_MIN) { + errno = ERANGE; + return LLONG_MIN; + } + return x; +} diff --git a/05/musl-final/src/stdlib/strtoul.c b/05/musl-final/src/stdlib/strtoul.c new file mode 100644 index 0000000..951d5e8 --- /dev/null +++ b/05/musl-final/src/stdlib/strtoul.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +unsigned long strtoul(const char *s, char **p, int base) +{ + uintmax_t x = strtoumax(s, p, base); + if (x > ULONG_MAX) { + errno = ERANGE; + return ULONG_MAX; + } + return x; +} diff --git a/05/musl-final/src/stdlib/strtoull.c b/05/musl-final/src/stdlib/strtoull.c new file mode 100644 index 0000000..20aa7bd --- /dev/null +++ b/05/musl-final/src/stdlib/strtoull.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +unsigned long long strtoull(const char *s, char **p, int base) +{ + uintmax_t x = strtoumax(s, p, base); + if (x > ULLONG_MAX) { + errno = ERANGE; + return ULLONG_MAX; + } + return x; +} diff --git a/05/musl-final/src/stdlib/strtoumax.c b/05/musl-final/src/stdlib/strtoumax.c new file mode 100644 index 0000000..a529f6e --- /dev/null +++ b/05/musl-final/src/stdlib/strtoumax.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include + +/* Lookup table for digit values. -1==255>=36 -> invalid */ +static const unsigned char digits[] = { +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, +-1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, +25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, +-1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, +25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, +}; + +uintmax_t strtoumax(const char *s1, char **p, int base) +{ + const unsigned char *s = s1; + size_t x1, z1; + uintmax_t x, z=0; + int sign = 0; + int shift; + + if (!p) p = (char **)&s1; + + /* Initial whitespace */ + for (; isspace(*s); s++); + + /* Optional sign */ + if (*s == '-') sign = *s++; + else if (*s == '+') s++; + + /* Default base 8, 10, or 16 depending on prefix */ + if (base == 0) { + if (s[0] == '0') { + if ((s[1]|32) == 'x') base = 16; + else base = 8; + } else { + base = 10; + } + } + + if ((unsigned)base-2 > 36-2 || digits[*s]>=base) { + *p = (char *)s1; + errno = EINVAL; + return 0; + } + + /* Main loops. Only use big types if we have to. */ + if (base == 10) { + for (x1=0; isdigit(*s) && x1<=SIZE_MAX/10-10; s++) + x1 = 10*x1 + *s-'0'; + for (x=x1; isdigit(*s) && x<=UINTMAX_MAX/10-10; s++) + x = 10*x + *s-'0'; + if (isdigit(*s)) { + if (isdigit(s[1]) || 10*x>UINTMAX_MAX-(*s-'0')) + goto overflow; + x = 10*x + *s-'0'; + } + } else if (!(base & base/2)) { + if (base == 16) { + if (s[0]=='0' && (s[1]|32)=='x' && digits[s[2]]<16) + s+=2; + shift=4; + z1 = SIZE_MAX/16; + z = UINTMAX_MAX/16; + } else if (base == 8) { + shift=3; + z1 = SIZE_MAX/8; + z = UINTMAX_MAX/8; + } else if (base == 2) { + shift=1; + z1 = SIZE_MAX/2; + z = UINTMAX_MAX/2; + } else if (base == 4) { + shift=2; + z1 = SIZE_MAX/4; + z = UINTMAX_MAX/4; + } else /* if (base == 32) */ { + shift=5; + z1 = SIZE_MAX/32; + z = UINTMAX_MAX/32; + } + for (x1=0; digits[*s]UINTMAX_MAX-digits[*s]) + goto overflow; + x = x*base + digits[*s]; + } + } + + *p = (char *)s; + return sign ? -x : x; + +overflow: + for (; digits[*s] < base; s++); + *p = (char *)s; + errno = ERANGE; + return UINTMAX_MAX; +} diff --git a/05/musl-final/src/stdlib/wcstoimax.c b/05/musl-final/src/stdlib/wcstoimax.c new file mode 100644 index 0000000..59894f6 --- /dev/null +++ b/05/musl-final/src/stdlib/wcstoimax.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +intmax_t wcstoimax(const wchar_t *s, wchar_t **p, int base) +{ + int sign = 0; + uintmax_t x; + + /* Initial whitespace */ + for (; iswspace(*s); s++); + + /* Optional sign */ + if (*s == '-') sign = *s++; + else if (*s == '+') s++; + + x = wcstoumax(s, p, base); + if (x > INTMAX_MAX) { + if (!sign || -x != INTMAX_MIN) + errno = ERANGE; + return sign ? INTMAX_MIN : INTMAX_MAX; + } + return sign ? -x : x; +} diff --git a/05/musl-final/src/stdlib/wcstol.c b/05/musl-final/src/stdlib/wcstol.c new file mode 100644 index 0000000..aad62e5 --- /dev/null +++ b/05/musl-final/src/stdlib/wcstol.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include + +long wcstol(const wchar_t *s, wchar_t **p, int base) +{ + intmax_t x = wcstoimax(s, p, base); + if (x > LONG_MAX) { + errno = ERANGE; + return LONG_MAX; + } else if (x < LONG_MIN) { + errno = ERANGE; + return LONG_MIN; + } + return x; +} diff --git a/05/musl-final/src/stdlib/wcstoll.c b/05/musl-final/src/stdlib/wcstoll.c new file mode 100644 index 0000000..ddfea74 --- /dev/null +++ b/05/musl-final/src/stdlib/wcstoll.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include +#include + +long long wcstoll(const wchar_t *s, wchar_t **p, int base) +{ + intmax_t x = wcstoimax(s, p, base); + if (x > LLONG_MAX) { + errno = ERANGE; + return LLONG_MAX; + } else if (x < LLONG_MIN) { + errno = ERANGE; + return LLONG_MIN; + } + return x; +} diff --git a/05/musl-final/src/stdlib/wcstoul.c b/05/musl-final/src/stdlib/wcstoul.c new file mode 100644 index 0000000..e39faaf --- /dev/null +++ b/05/musl-final/src/stdlib/wcstoul.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include + +unsigned long wcstoul(const wchar_t *s, wchar_t **p, int base) +{ + uintmax_t x = wcstoumax(s, p, base); + if (x > ULONG_MAX) { + errno = ERANGE; + return ULONG_MAX; + } + return x; +} diff --git a/05/musl-final/src/stdlib/wcstoull.c b/05/musl-final/src/stdlib/wcstoull.c new file mode 100644 index 0000000..e324dfb --- /dev/null +++ b/05/musl-final/src/stdlib/wcstoull.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include + +unsigned long long wcstoull(const wchar_t *s, wchar_t **p, int base) +{ + uintmax_t x = wcstoumax(s, p, base); + if (x > ULLONG_MAX) { + errno = ERANGE; + return ULLONG_MAX; + } + return x; +} diff --git a/05/musl-final/src/stdlib/wcstoumax.c b/05/musl-final/src/stdlib/wcstoumax.c new file mode 100644 index 0000000..456dded --- /dev/null +++ b/05/musl-final/src/stdlib/wcstoumax.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +uintmax_t wcstoumax(const wchar_t *s, wchar_t **p, int base) +{ + /* Large enough for largest value in binary */ + char buf[sizeof(uintmax_t)*8+2]; + int sign = 0, skipped=0; + + if (!p) p = (wchar_t **)&s; + + if (base && (unsigned)base-2 > 36-2) { + *p = (wchar_t *)s; + errno = EINVAL; + return 0; + } + + /* Initial whitespace */ + for (; iswspace(*s); s++); + + /* Optional sign */ + if (*s == '-') sign = *s++; + else if (*s == '+') s++; + + /* Skip leading zeros but don't allow leading zeros before "0x". */ + for (; s[0]=='0' && s[1]=='0'; s++) skipped=1; + if (skipped && (base==0 || base==16) && (s[1]|32)=='x') { + *p = (wchar_t *)(s+1); + return 0; + } + + /* Convert to normal char string so we can use strtoumax */ + buf[0] = sign; + if (wcstombs(buf+!!sign, s, sizeof buf-1) < 0) return 0; + buf[sizeof buf-1]=0; + + /* Compute final position */ + if (p) { + if ((base==0 || base==16) && s[0]=='0' && (s[1]|32)=='x' && iswxdigit(s[2])) s+=2; + for(;*s&&((unsigned)*s-'0' +#include + +int bcmp(const void *s1, const void *s2, size_t n) +{ + return memcmp(s1, s2, n); +} diff --git a/05/musl-final/src/string/bcopy.c b/05/musl-final/src/string/bcopy.c new file mode 100644 index 0000000..e76272f --- /dev/null +++ b/05/musl-final/src/string/bcopy.c @@ -0,0 +1,7 @@ +#include +#include + +void bcopy(const void *s1, void *s2, size_t n) +{ + memmove(s2, s1, n); +} diff --git a/05/musl-final/src/string/bzero.c b/05/musl-final/src/string/bzero.c new file mode 100644 index 0000000..0f98b4a --- /dev/null +++ b/05/musl-final/src/string/bzero.c @@ -0,0 +1,7 @@ +#include +#include + +void bzero(void *s, size_t n) +{ + memset(s, 0, n); +} diff --git a/05/musl-final/src/string/index.c b/05/musl-final/src/string/index.c new file mode 100644 index 0000000..dd61125 --- /dev/null +++ b/05/musl-final/src/string/index.c @@ -0,0 +1,7 @@ +#include +#include + +char *index(const char *s, int c) +{ + return strchr(s, c); +} diff --git a/05/musl-final/src/string/memchr.c b/05/musl-final/src/string/memchr.c new file mode 100644 index 0000000..a0472f7 --- /dev/null +++ b/05/musl-final/src/string/memchr.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +#define SS (sizeof(size_t)) +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +void *memchr(const void *src, int c, size_t n) +{ + const unsigned char *s = src; + c = (unsigned char)c; + for (; ((uintptr_t)s & ALIGN) && n && *s != c; s++, n--); + if (n && *s != c) { + const size_t *w; + size_t k = ONES * c; + for (w = (const void *)s; n>=SS && !HASZERO(*w^k); w++, n-=SS); + for (s = (const void *)w; n && *s != c; s++, n--); + } + return n ? (void *)s : 0; +} diff --git a/05/musl-final/src/string/memcmp.c b/05/musl-final/src/string/memcmp.c new file mode 100644 index 0000000..bdbce9f --- /dev/null +++ b/05/musl-final/src/string/memcmp.c @@ -0,0 +1,8 @@ +#include + +int memcmp(const void *vl, const void *vr, size_t n) +{ + const unsigned char *l=vl, *r=vr; + for (; n && *l == *r; n--, l++, r++); + return n ? *l-*r : 0; +} diff --git a/05/musl-final/src/string/memcpy.c b/05/musl-final/src/string/memcpy.c new file mode 100644 index 0000000..02cb469 --- /dev/null +++ b/05/musl-final/src/string/memcpy.c @@ -0,0 +1,29 @@ +#include +#include +#include + +#define SS (sizeof(size_t)) +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) + +void *memcpy(void *dest, const void *src, size_t n) +{ + unsigned char *d = dest; + const unsigned char *s = src; + + if (((uintptr_t)d & ALIGN) != ((uintptr_t)s & ALIGN)) + goto misaligned; + + for (; ((uintptr_t)d & ALIGN) && n; n--) *d++ = *s++; + if (n) { + size_t *wd = (void *)d; + const size_t *ws = (const void *)s; + + for (; n>=SS; n-=SS) *wd++ = *ws++; + d = (void *)wd; + s = (const void *)ws; +misaligned: + for (; n; n--) *d++ = *s++; + } + return dest; +} diff --git a/05/musl-final/src/string/memmove.c b/05/musl-final/src/string/memmove.c new file mode 100644 index 0000000..22bb4b3 --- /dev/null +++ b/05/musl-final/src/string/memmove.c @@ -0,0 +1,14 @@ +#include + +void *memmove(void *dest, const void *src, size_t n) +{ + char *d = dest; + const char *s = src; + if (d==s) return d; + if ((size_t)(d-s) < n) { + while (n--) d[n] = s[n]; + return dest; + } + /* Assumes memcpy is overlap-safe when dest < src */ + return memcpy(d, s, n); +} diff --git a/05/musl-final/src/string/mempcpy.c b/05/musl-final/src/string/mempcpy.c new file mode 100644 index 0000000..e54251c --- /dev/null +++ b/05/musl-final/src/string/mempcpy.c @@ -0,0 +1,7 @@ +#include + +void *mempcpy(void *dest, void *src, size_t n) +{ + memcpy(dest, src, n); + return (char *)dest + n; +} diff --git a/05/musl-final/src/string/memset.c b/05/musl-final/src/string/memset.c new file mode 100644 index 0000000..20e47c4 --- /dev/null +++ b/05/musl-final/src/string/memset.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +#define SS (sizeof(size_t)) +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) + +void *memset(void *dest, int c, size_t n) +{ + unsigned char *s = dest; + c = (unsigned char)c; + for (; ((uintptr_t)s & ALIGN) && n; n--) *s++ = c; + if (n) { + size_t *w, k = ONES * c; + for (w = (void *)s; n>=SS; n-=SS, w++) *w = k; + for (s = (void *)w; n; n--, s++) *s = c; + } + return dest; +} diff --git a/05/musl-final/src/string/rindex.c b/05/musl-final/src/string/rindex.c new file mode 100644 index 0000000..17df2bf --- /dev/null +++ b/05/musl-final/src/string/rindex.c @@ -0,0 +1,7 @@ +#include +#include + +char *rindex(const char *s, int c) +{ + return strrchr(s, c); +} diff --git a/05/musl-final/src/string/stpcpy.c b/05/musl-final/src/string/stpcpy.c new file mode 100644 index 0000000..10ca493 --- /dev/null +++ b/05/musl-final/src/string/stpcpy.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include "libc.h" + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *__stpcpy(char *d, const char *s) +{ + size_t *wd; + const size_t *ws; + + if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { + for (; (*d=*s) && ((uintptr_t)s & ALIGN); s++, d++); + if (!*s) return d; + wd=(void *)d; ws=(const void *)s; + for (; !HASZERO(*ws); *wd++ = *ws++); + d=(void *)wd; s=(const void *)ws; + } + for (; (*d=*s); s++, d++); + + return d; +} + +weak_alias(__stpcpy, stpcpy); diff --git a/05/musl-final/src/string/stpncpy.c b/05/musl-final/src/string/stpncpy.c new file mode 100644 index 0000000..a877f5f --- /dev/null +++ b/05/musl-final/src/string/stpncpy.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include "libc.h" + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *__stpncpy(char *d, const char *s, size_t n) +{ + size_t *wd; + const size_t *ws; + + if (((uintptr_t)s & ALIGN) != ((uintptr_t)d & ALIGN)) { + for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); + if (!n || !*s) goto tail; + wd=(void *)d; ws=(const void *)s; + for (; n>=sizeof(size_t) && !HASZERO(*ws); + n-=sizeof(size_t), ws++, *wd++) *wd = *ws; + d=(void *)wd; s=(const void *)ws; + } + for (; n && (*d=*s); n--, s++, d++); +tail: + memset(d, 0, n); + return d; +} + +weak_alias(__stpncpy, stpncpy); + diff --git a/05/musl-final/src/string/strcasecmp.c b/05/musl-final/src/string/strcasecmp.c new file mode 100644 index 0000000..dd87905 --- /dev/null +++ b/05/musl-final/src/string/strcasecmp.c @@ -0,0 +1,9 @@ +#include +#include + +int strcasecmp(const char *_l, const char *_r) +{ + const unsigned char *l=_l, *r=_r; + for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++); + return tolower(*l) - tolower(*r); +} diff --git a/05/musl-final/src/string/strcasestr.c b/05/musl-final/src/string/strcasestr.c new file mode 100644 index 0000000..f1cb0e8 --- /dev/null +++ b/05/musl-final/src/string/strcasestr.c @@ -0,0 +1,7 @@ +#include + +char *strcasestr(const char *h, const char *n) +{ + //FIXME! + return strstr(h, n); +} diff --git a/05/musl-final/src/string/strcat.c b/05/musl-final/src/string/strcat.c new file mode 100644 index 0000000..29fdb61 --- /dev/null +++ b/05/musl-final/src/string/strcat.c @@ -0,0 +1,7 @@ +#include + +char *strcat(char *dest, const char *src) +{ + strcpy(dest + strlen(dest), src); + return dest; +} diff --git a/05/musl-final/src/string/strchr.c b/05/musl-final/src/string/strchr.c new file mode 100644 index 0000000..e606f4f --- /dev/null +++ b/05/musl-final/src/string/strchr.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *strchr(const char *s, int c) +{ + c = (char)c; + if (!c) return (char *)s + strlen(s); + for (; ((uintptr_t)s & ALIGN) && *s && *s != c; s++); + if (*s && *s != c) { + const size_t *w; + size_t k = ONES * c; + for (w = (const void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); + for (s = (const void *)w; *s && *s != c; s++); + } + return *s ? (char *)s : 0; +} diff --git a/05/musl-final/src/string/strchrnul.c b/05/musl-final/src/string/strchrnul.c new file mode 100644 index 0000000..5e0c1a1 --- /dev/null +++ b/05/musl-final/src/string/strchrnul.c @@ -0,0 +1,7 @@ +#include + +char *strchrnul(const char *s, int c) +{ + char *p = strchr(s, c); + return p ? p : (char *)s + strlen(s); +} diff --git a/05/musl-final/src/string/strcmp.c b/05/musl-final/src/string/strcmp.c new file mode 100644 index 0000000..91eb740 --- /dev/null +++ b/05/musl-final/src/string/strcmp.c @@ -0,0 +1,7 @@ +#include + +int strcmp(const char *l, const char *r) +{ + for (; *l==*r && *l && *r; l++, r++); + return *(unsigned char *)l - *(unsigned char *)r; +} diff --git a/05/musl-final/src/string/strcpy.c b/05/musl-final/src/string/strcpy.c new file mode 100644 index 0000000..7675e9c --- /dev/null +++ b/05/musl-final/src/string/strcpy.c @@ -0,0 +1,16 @@ +#include + +char *__stpcpy(char *, const char *); + +char *strcpy(char *dest, const char *src) +{ +#if 1 + __stpcpy(dest, src); + return dest; +#else + const unsigned char *s = src; + unsigned char *d = dest; + while ((*d++ = *s++)); + return dest; +#endif +} diff --git a/05/musl-final/src/string/strcspn.c b/05/musl-final/src/string/strcspn.c new file mode 100644 index 0000000..439b7be --- /dev/null +++ b/05/musl-final/src/string/strcspn.c @@ -0,0 +1,20 @@ +#include + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +size_t strcspn(const char *_s, const char *_c) +{ + const unsigned char *s = _s; + const unsigned char *c = _c; + const unsigned char *a = s; + size_t byteset[32/sizeof(size_t)]; + + if (!c[0]) return strlen(s); + if (!c[1]) return (s=strchr(s, *c)) ? s-a : strlen(a); + + memset(byteset, 0, sizeof byteset); + for (; *c && BITOP(byteset, *c, |=); c++); + for (; *s && !BITOP(byteset, *s, &); s++); + return s-a; +} diff --git a/05/musl-final/src/string/strdup.c b/05/musl-final/src/string/strdup.c new file mode 100644 index 0000000..defd559 --- /dev/null +++ b/05/musl-final/src/string/strdup.c @@ -0,0 +1,19 @@ +#include +#include +#include "libc.h" + +char *__strdup(const char *s) +{ + size_t l = strlen(s); + char *d = malloc(l+1); + if (!d) return NULL; + return memcpy(d, s, l+1); +} + +char *strdup(const char *s) +{ + size_t l = strlen(s); + char *d = malloc(l+1); + if (!d) return NULL; + return memcpy(d, s, l+1); +} diff --git a/05/musl-final/src/string/strerror_r.c b/05/musl-final/src/string/strerror_r.c new file mode 100644 index 0000000..6fdd4ce --- /dev/null +++ b/05/musl-final/src/string/strerror_r.c @@ -0,0 +1,11 @@ +#include +#include + +int strerror_r(int err, char *buf, size_t buflen) +{ + char *msg = strerror(err); + if (strlen(msg) >= buflen) + return ERANGE; + strcpy(buf, msg); + return 0; +} diff --git a/05/musl-final/src/string/strlcat.c b/05/musl-final/src/string/strlcat.c new file mode 100644 index 0000000..ef81209 --- /dev/null +++ b/05/musl-final/src/string/strlcat.c @@ -0,0 +1,9 @@ +#define _BSD_SOURCE +#include + +size_t strlcat(char *d, const char *s, size_t n) +{ + size_t l = strnlen(d, n); + if (l == n) return l + strlen(s); + return l + strlcpy(d+l, s, n-l); +} diff --git a/05/musl-final/src/string/strlcpy.c b/05/musl-final/src/string/strlcpy.c new file mode 100644 index 0000000..bbebf1d --- /dev/null +++ b/05/musl-final/src/string/strlcpy.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include "libc.h" + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +size_t strlcpy(char *d, const char *s, size_t n) +{ + char *d0 = d; + size_t *wd; + const size_t *ws; + + if (!n--) goto finish; + if (((uintptr_t)s & ALIGN) != ((uintptr_t)d & ALIGN)) { + for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); + if (n && *s) { + wd=(void *)d; ws=(const void *)s; + for (; n>=sizeof(size_t) && !HASZERO(*ws); + n-=sizeof(size_t), ws++, *wd++) *wd = *ws; + d=(void *)wd; s=(const void *)ws; + } + } + for (; n && (*d=*s); n--, s++, d++); + *d = 0; +finish: + return d-d0 + strlen(s); +} diff --git a/05/musl-final/src/string/strlen.c b/05/musl-final/src/string/strlen.c new file mode 100644 index 0000000..936fb5c --- /dev/null +++ b/05/musl-final/src/string/strlen.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +size_t strlen(const char *s) +{ + const char *a = s; + const size_t *w; + for (; ((uintptr_t)s & ALIGN) && *s; s++); + if (*s) { + for (w = (const void *)s; !HASZERO(*w); w++); + for (s = (const void *)w; *s; s++); + } + return s-a; +} diff --git a/05/musl-final/src/string/strncasecmp.c b/05/musl-final/src/string/strncasecmp.c new file mode 100644 index 0000000..4f9230e --- /dev/null +++ b/05/musl-final/src/string/strncasecmp.c @@ -0,0 +1,10 @@ +#include +#include + +int strncasecmp(const char *_l, const char *_r, size_t n) +{ + const unsigned char *l=_l, *r=_r; + if (!n--) return 0; + for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--); + return tolower(*l) - tolower(*r); +} diff --git a/05/musl-final/src/string/strncat.c b/05/musl-final/src/string/strncat.c new file mode 100644 index 0000000..255b7a7 --- /dev/null +++ b/05/musl-final/src/string/strncat.c @@ -0,0 +1,10 @@ +#include + +char *strncat(char *d, const char *s, size_t n) +{ + char *a = d; + d += strlen(d); + while (n && (*d++ = *s++)) n--; + *d++ = 0; + return a; +} diff --git a/05/musl-final/src/string/strncmp.c b/05/musl-final/src/string/strncmp.c new file mode 100644 index 0000000..52ba032 --- /dev/null +++ b/05/musl-final/src/string/strncmp.c @@ -0,0 +1,9 @@ +#include + +int strncmp(const char *_l, const char *_r, size_t n) +{ + const unsigned char *l=_l, *r=_r; + if (!n--) return 0; + for (; *l && *r && n && *l == *r ; l++, r++, n--); + return *l - *r; +} diff --git a/05/musl-final/src/string/strncpy.c b/05/musl-final/src/string/strncpy.c new file mode 100644 index 0000000..c0cd797 --- /dev/null +++ b/05/musl-final/src/string/strncpy.c @@ -0,0 +1,9 @@ +#include + +char *__stpncpy(char *, const char *, size_t); + +char *strncpy(char *d, const char *s, size_t n) +{ + __stpncpy(d, s, n); + return d; +} diff --git a/05/musl-final/src/string/strndup.c b/05/musl-final/src/string/strndup.c new file mode 100644 index 0000000..617d27b --- /dev/null +++ b/05/musl-final/src/string/strndup.c @@ -0,0 +1,12 @@ +#include +#include + +char *strndup(const char *s, size_t n) +{ + size_t l = strnlen(s, n); + char *d = malloc(l+1); + if (!d) return NULL; + memcpy(d, s, l); + d[l] = 0; + return d; +} diff --git a/05/musl-final/src/string/strnlen.c b/05/musl-final/src/string/strnlen.c new file mode 100644 index 0000000..6442eb7 --- /dev/null +++ b/05/musl-final/src/string/strnlen.c @@ -0,0 +1,7 @@ +#include + +size_t strnlen(const char *s, size_t n) +{ + const char *p = memchr(s, 0, n); + return p ? p-s : n; +} diff --git a/05/musl-final/src/string/strpbrk.c b/05/musl-final/src/string/strpbrk.c new file mode 100644 index 0000000..55947c6 --- /dev/null +++ b/05/musl-final/src/string/strpbrk.c @@ -0,0 +1,7 @@ +#include + +char *strpbrk(const char *s, const char *b) +{ + s += strcspn(s, b); + return *s ? (char *)s : 0; +} diff --git a/05/musl-final/src/string/strrchr.c b/05/musl-final/src/string/strrchr.c new file mode 100644 index 0000000..31c8e0b --- /dev/null +++ b/05/musl-final/src/string/strrchr.c @@ -0,0 +1,9 @@ +#include + +char *strrchr(const char *s, int c) +{ + const char *p; + c = (char)c; + for (p=s+strlen(s); p>=s && *p!=c; p--); + return p>=s ? (char *)p : 0; +} diff --git a/05/musl-final/src/string/strsep.c b/05/musl-final/src/string/strsep.c new file mode 100644 index 0000000..1bfe1db --- /dev/null +++ b/05/musl-final/src/string/strsep.c @@ -0,0 +1,12 @@ +#include + +char *strsep(char **str, const char *sep) +{ + char *s = *str, *end; + if (!s) return NULL; + end = s + strcspn(s, sep); + if (*end) *end++ = 0; + else end = 0; + *str = end; + return s; +} diff --git a/05/musl-final/src/string/strsignal.c b/05/musl-final/src/string/strsignal.c new file mode 100644 index 0000000..72fba8d --- /dev/null +++ b/05/musl-final/src/string/strsignal.c @@ -0,0 +1,98 @@ +#include + +#if (SIGHUP == 1) && (SIGINT == 2) && (SIGQUIT == 3) && (SIGILL == 4) \ + && (SIGTRAP == 5) && (SIGABRT == 6) && (SIGBUS == 7) && (SIGFPE == 8) \ + && (SIGKILL == 9) && (SIGUSR1 == 10) && (SIGSEGV == 11) && (SIGUSR2 == 12) \ + && (SIGPIPE == 13) && (SIGALRM == 14) && (SIGTERM == 15) && (SIGSTKFLT == 16) \ + && (SIGCHLD == 17) && (SIGCONT == 18) && (SIGSTOP == 19) && (SIGTSTP == 20) \ + && (SIGTTIN == 21) && (SIGTTOU == 22) && (SIGURG == 23) && (SIGXCPU == 24) \ + && (SIGXFSZ == 25) && (SIGVTALRM == 26) && (SIGPROF == 27) && (SIGWINCH == 28) \ + && (SIGPOLL == 29) && (SIGPWR == 30) && (SIGSYS == 31) + +#define sigmap(x) x + +#else + +static const char map[] = { + [SIGHUP] = 1, + [SIGINT] = 2, + [SIGQUIT] = 3, + [SIGILL] = 4, + [SIGTRAP] = 5, + [SIGABRT] = 6, + [SIGBUS] = 7, + [SIGFPE] = 8, + [SIGKILL] = 9, + [SIGUSR1] = 10, + [SIGSEGV] = 11, + [SIGUSR2] = 12, + [SIGPIPE] = 13, + [SIGALRM] = 14, + [SIGTERM] = 15, + [SIGSTKFLT] = 16, + [SIGCHLD] = 17, + [SIGCONT] = 18, + [SIGSTOP] = 19, + [SIGTSTP] = 20, + [SIGTTIN] = 21, + [SIGTTOU] = 22, + [SIGURG] = 23, + [SIGXCPU] = 24, + [SIGXFSZ] = 25, + [SIGVTALRM] = 26, + [SIGPROF] = 27, + [SIGWINCH] = 28, + [SIGPOLL] = 29, + [SIGPWR] = 30, + [SIGSYS] = 31 +}; + +#define sigmap(x) ((unsigned)(x) > sizeof map ? 0 : map[(unsigned)(x)]) + +#endif + +static const char strings[] = + "Unknown signal\0" + "Hangup\0" + "Interrupt\0" + "Quit\0" + "Illegal instruction\0" + "Trace/breakpoint trap\0" + "Aborted\0" + "Bus error\0" + "Floating point exception\0" + "Killed\0" + "User defined signal 1\0" + "Segmentation fault\0" + "User defined signal 2\0" + "Broken pipe\0" + "Alarm clock\0" + "Terminated\0" + "Stack fault\0" + "Child exited\0" + "Continued\0" + "Stopped (signal)\0" + "Stopped\0" + "Stopped (tty input)\0" + "Stopped (tty output)\0" + "Urgent I/O condition\0" + "CPU time limit exceeded\0" + "File size limit exceeded\0" + "Virtual timer expired\0" + "Profiling timer expired\0" + "Window changed\0" + "I/O possible\0" + "Power failure\0" + "Bad system call"; + +char *strsignal(int signum) +{ + char *s = (char *)strings; + + signum = sigmap(signum); + if ((unsigned)signum - 1 > 31) signum = 0; + + for (; signum--; s++) for (; *s; s++); + + return s; +} diff --git a/05/musl-final/src/string/strspn.c b/05/musl-final/src/string/strspn.c new file mode 100644 index 0000000..59b063e --- /dev/null +++ b/05/musl-final/src/string/strspn.c @@ -0,0 +1,22 @@ +#include + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +size_t strspn(const char *_s, const char *_c) +{ + const unsigned char *s = _s; + const unsigned char *c = _c; + const unsigned char *a = s; + size_t byteset[32/sizeof(size_t)] = { 0 }; + + if (!c[0]) return 0; + if (!c[1]) { + for (; *s == *c; s++); + return s-a; + } + + for (; *c && BITOP(byteset, *c, |=); c++); + for (; *s && BITOP(byteset, *s, &); s++); + return s-a; +} diff --git a/05/musl-final/src/string/strstr.c b/05/musl-final/src/string/strstr.c new file mode 100644 index 0000000..4d536a7 --- /dev/null +++ b/05/musl-final/src/string/strstr.c @@ -0,0 +1,166 @@ +#include +#include +#include + +static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) +{ + uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; + for (h++; *h && hw != nw; hw = hw<<8 | *++h); + return *h ? (char *)h-1 : 0; +} + +static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) +{ + uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; + uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; + for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); + return *h ? (char *)h-2 : 0; +} + +static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) +{ + uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; + uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; + for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); + return *h ? (char *)h-3 : 0; +} + +#if 0 +static char *naive_strstr(const char *h, const char *n) +{ + size_t i; + for (i=0; n[i] && h[i]; i++) + for ( ; n[i] != h[i]; h++, i=0); + return n[i] ? 0 : (char *)h; +} +#endif + +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define MIN(a,b) ((a)<(b)?(a):(b)) + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +static char *twoway_strstr(const unsigned char *h, const unsigned char *n) +{ + const unsigned char *z; + size_t l, ip, jp, k, p, ms, p0, mem, mem0; + size_t byteset[32 / sizeof(size_t)] = { 0 }; + size_t shift[256]; + + /* Computing length of needle and fill shift table */ + for (l=0; n[l] && h[l]; l++) + BITOP(byteset, n[l], |=), shift[n[l]] = l+1; + if (n[l]) return 0; /* hit the end of h */ + + /* Compute maximal suffix */ + ip = -1; jp = 0; k = p = 1; + while (jp+k n[jp+k]) { + jp += k; + k = 1; + p = jp - ip; + } else { + ip = jp++; + k = p = 1; + } + } + ms = ip; + p0 = p; + + /* And with the opposite comparison */ + ip = -1; jp = 0; k = p = 1; + while (jp+k ms+1) ms = ip; + else p = p0; + + /* Periodic needle? */ + if (memcmp(n, n+p, ms+1)) { + mem0 = 0; + p = MAX(ms, l-ms-1) + 1; + } else mem0 = l-p; + mem = 0; + + /* Initialize incremental end-of-haystack pointer */ + z = h; + + /* Search loop */ + for (;;) { + /* Update incremental end-of-haystack pointer */ + if (z-h < l) { + /* Fast estimate for MIN(l,63) */ + size_t grow = l | 63; + const char *z2 = memchr(z, 0, grow); + if (z2) { + z = z2; + if (z-h < l) return 0; + } else z += grow; + } + + /* Check last byte first; advance by shift on mismatch */ + if (BITOP(byteset, h[l-1], &)) { + k = l-shift[h[l-1]]; + //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l); + if (k) { + if (mem0 && mem && k < p) k = l-p; + h += k; + mem = 0; + continue; + } + } else { + h += l; + mem = 0; + continue; + } + + /* Compare right half */ + for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); + if (n[k]) { + h += k-ms; + mem = 0; + continue; + } + /* Compare left half */ + for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); + if (k == mem) return (char *)h; + h += p; + mem = mem0; + } +} + +char *strstr(const char *h, const char *n) +{ + /* Return immediately on empty needle */ + if (!n[0]) return (char *)h; + + /* Use faster algorithms for short needles */ + h = strchr(h, *n); + if (!h || !n[1]) return (char *)h; + if (!h[1]) return 0; + if (!n[2]) return twobyte_strstr(h, n); + if (!h[2]) return 0; + if (!n[3]) return threebyte_strstr(h, n); + if (!h[3]) return 0; + if (!n[4]) return fourbyte_strstr(h, n); + + return twoway_strstr(h, n); +} diff --git a/05/musl-final/src/string/strtok.c b/05/musl-final/src/string/strtok.c new file mode 100644 index 0000000..1ba221c --- /dev/null +++ b/05/musl-final/src/string/strtok.c @@ -0,0 +1,13 @@ +#include + +char *strtok(char *s, const char *sep) +{ + static char *p; + if (!s && !(s = p)) return NULL; + s += strspn(s, sep); + if (!*s) return p = 0; + p = s + strcspn(s, sep); + if (*p) *p++ = 0; + else p = 0; + return s; +} diff --git a/05/musl-final/src/string/strtok_r.c b/05/musl-final/src/string/strtok_r.c new file mode 100644 index 0000000..c763897 --- /dev/null +++ b/05/musl-final/src/string/strtok_r.c @@ -0,0 +1,12 @@ +#include + +char *strtok_r(char *s, const char *sep, char **p) +{ + if (!s && !(s = *p)) return NULL; + s += strspn(s, sep); + if (!*s) return *p = 0; + *p = s + strcspn(s, sep); + if (**p) *(*p)++ = 0; + else *p = 0; + return s; +} diff --git a/05/musl-final/src/string/swab.c b/05/musl-final/src/string/swab.c new file mode 100644 index 0000000..b213288 --- /dev/null +++ b/05/musl-final/src/string/swab.c @@ -0,0 +1,13 @@ +#include + +void swab(const void *_src, void *_dest, ssize_t n) +{ + const char *src = _src; + char *dest = _dest; + for (; n>0; n-=2) { + dest[0] = src[1]; + dest[1] = src[0]; + dest += 2; + src += 2; + } +} diff --git a/05/musl-final/src/string/wcscat.c b/05/musl-final/src/string/wcscat.c new file mode 100644 index 0000000..946f16e --- /dev/null +++ b/05/musl-final/src/string/wcscat.c @@ -0,0 +1,7 @@ +#include + +wchar_t *wcscat(wchar_t *dest, const wchar_t *src) +{ + wcscpy(dest + wcslen(dest), src); + return dest; +} diff --git a/05/musl-final/src/string/wcschr.c b/05/musl-final/src/string/wcschr.c new file mode 100644 index 0000000..8dfc2f3 --- /dev/null +++ b/05/musl-final/src/string/wcschr.c @@ -0,0 +1,8 @@ +#include + +wchar_t *wcschr(const wchar_t *s, wchar_t c) +{ + if (!c) return (wchar_t *)s + wcslen(s); + for (; *s && *s != c; s++); + return *s ? (wchar_t *)s : 0; +} diff --git a/05/musl-final/src/string/wcscmp.c b/05/musl-final/src/string/wcscmp.c new file mode 100644 index 0000000..26eeee7 --- /dev/null +++ b/05/musl-final/src/string/wcscmp.c @@ -0,0 +1,7 @@ +#include + +int wcscmp(const wchar_t *l, const wchar_t *r) +{ + for (; *l==*r && *l && *r; l++, r++); + return *l - *r; +} diff --git a/05/musl-final/src/string/wcscpy.c b/05/musl-final/src/string/wcscpy.c new file mode 100644 index 0000000..e0ac194 --- /dev/null +++ b/05/musl-final/src/string/wcscpy.c @@ -0,0 +1,8 @@ +#include + +wchar_t *wcscpy(wchar_t *d, const wchar_t *s) +{ + wchar_t *a = d; + while ((*d++ = *s++)); + return a; +} diff --git a/05/musl-final/src/string/wcscspn.c b/05/musl-final/src/string/wcscspn.c new file mode 100644 index 0000000..c4e5272 --- /dev/null +++ b/05/musl-final/src/string/wcscspn.c @@ -0,0 +1,10 @@ +#include + +size_t wcscspn(const wchar_t *s, const wchar_t *c) +{ + const wchar_t *a; + if (!c[0]) return wcslen(s); + if (!c[1]) return (s=wcschr(a=s, *c)) ? s-a : wcslen(a); + for (a=s; *s && !wcschr(c, *s); s++); + return s-a; +} diff --git a/05/musl-final/src/string/wcslen.c b/05/musl-final/src/string/wcslen.c new file mode 100644 index 0000000..1b7b665 --- /dev/null +++ b/05/musl-final/src/string/wcslen.c @@ -0,0 +1,8 @@ +#include + +size_t wcslen(const wchar_t *s) +{ + const wchar_t *a; + for (a=s; *s; s++); + return s-a; +} diff --git a/05/musl-final/src/string/wcsncat.c b/05/musl-final/src/string/wcsncat.c new file mode 100644 index 0000000..b07abe4 --- /dev/null +++ b/05/musl-final/src/string/wcsncat.c @@ -0,0 +1,10 @@ +#include + +wchar_t *wcsncat(wchar_t *d, const wchar_t *s, size_t n) +{ + wchar_t *a = d; + d += wcslen(d); + while (n && (*d++ = *s++)) n--; + *d++ = 0; + return a; +} diff --git a/05/musl-final/src/string/wcsncmp.c b/05/musl-final/src/string/wcsncmp.c new file mode 100644 index 0000000..1b159f4 --- /dev/null +++ b/05/musl-final/src/string/wcsncmp.c @@ -0,0 +1,7 @@ +#include + +int wcsncmp(const wchar_t *l, const wchar_t *r, size_t n) +{ + for (; n && *l==*r && *l && *r; l++, r++); + return n ? *l - *r : 0; +} diff --git a/05/musl-final/src/string/wcsncpy.c b/05/musl-final/src/string/wcsncpy.c new file mode 100644 index 0000000..0164208 --- /dev/null +++ b/05/musl-final/src/string/wcsncpy.c @@ -0,0 +1,9 @@ +#include + +wchar_t *wcsncpy(wchar_t *d, const wchar_t *s, size_t n) +{ + wchar_t *a = d; + while (n && (*d++ = *s++)) n--; + wmemset(d, 0, n); + return a; +} diff --git a/05/musl-final/src/string/wcspbrk.c b/05/musl-final/src/string/wcspbrk.c new file mode 100644 index 0000000..0c72c19 --- /dev/null +++ b/05/musl-final/src/string/wcspbrk.c @@ -0,0 +1,7 @@ +#include + +wchar_t *wcspbrk(const wchar_t *s, const wchar_t *b) +{ + s += wcscspn(s, b); + return *s ? (wchar_t *)s : NULL; +} diff --git a/05/musl-final/src/string/wcsrchr.c b/05/musl-final/src/string/wcsrchr.c new file mode 100644 index 0000000..7503475 --- /dev/null +++ b/05/musl-final/src/string/wcsrchr.c @@ -0,0 +1,8 @@ +#include + +wchar_t *wcsrchr(const wchar_t *s, wint_t c) +{ + const wchar_t *p; + for (p=s+wcslen(s); p>=s && *p!=c; p--); + return p>=s ? (wchar_t *)p : 0; +} diff --git a/05/musl-final/src/string/wcsspn.c b/05/musl-final/src/string/wcsspn.c new file mode 100644 index 0000000..4320d8f --- /dev/null +++ b/05/musl-final/src/string/wcsspn.c @@ -0,0 +1,8 @@ +#include + +size_t wcsspn(const wchar_t *s, const wchar_t *c) +{ + const wchar_t *a; + for (a=s; *s && wcschr(c, *s); s++); + return s-a; +} diff --git a/05/musl-final/src/string/wcsstr.c b/05/musl-final/src/string/wcsstr.c new file mode 100644 index 0000000..966174f --- /dev/null +++ b/05/musl-final/src/string/wcsstr.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +static wchar_t *naive_wcsstr(const wchar_t *h, const wchar_t *n) +{ + size_t i; + for (i=0; n[i] && h[i]; i++) + for ( ; n[i] != h[i]; h++, i=0); + return n[i] ? 0 : (wchar_t *)h; +} + +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define MIN(a,b) ((a)<(b)?(a):(b)) + +static wchar_t *twoway_wcsstr(const wchar_t *h, const wchar_t *n) +{ + const wchar_t *z; + size_t l, ip, jp, k, p, ms, p0, mem, mem0; + + /* Computing length of needle */ + for (l=0; n[l] && h[l]; l++); + if (n[l]) return 0; /* hit the end of h */ + + /* Compute maximal suffix */ + ip = -1; jp = 0; k = p = 1; + while (jp+k n[jp+k]) { + jp += k; + k = 1; + p = jp - ip; + } else { + ip = jp++; + k = p = 1; + } + } + ms = ip; + p0 = p; + + /* And with the opposite comparison */ + ip = -1; jp = 0; k = p = 1; + while (jp+k ms+1) ms = ip; + else p = p0; + + /* Periodic needle? */ + if (wmemcmp(n, n+p, ms+1)) { + mem0 = 0; + p = MAX(ms, l-ms-1) + 1; + } else mem0 = l-p; + mem = 0; + + /* Initialize incremental end-of-haystack pointer */ + z = h; + + /* Search loop */ + for (;;) { + /* Update incremental end-of-haystack pointer */ + if (z-h < l) { + /* Fast estimate for MIN(l,63) */ + size_t grow = l | 63; + const wchar_t *z2 = wmemchr(z, 0, grow); + if (z2) { + z = z2; + if (z-h < l) return 0; + } else z += grow; + } + + /* Compare right half */ + for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); + if (n[k]) { + h += k-ms; + mem = 0; + continue; + } + /* Compare left half */ + for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); + if (k == mem) return (wchar_t *)h; + h += p; + mem = mem0; + } +} + +wchar_t *wcsstr(const wchar_t *h, const wchar_t *n) +{ + /* Return immediately on empty needle or haystack */ + if (!n[0]) return (wchar_t *)h; + if (!h[0]) return 0; + + /* Use faster algorithms for short needles */ + h = wcschr(h, *n); + if (!h || !n[1]) return (wchar_t *)h; + if (!h[1]) return 0; + if (!n[2] || !n[3] || !n[4]) return naive_wcsstr(h, n); + + return twoway_wcsstr(h, n); +} diff --git a/05/musl-final/src/string/wcswcs.c b/05/musl-final/src/string/wcswcs.c new file mode 100644 index 0000000..9cfe4ac --- /dev/null +++ b/05/musl-final/src/string/wcswcs.c @@ -0,0 +1,6 @@ +#include + +wchar_t *wcswcs(const wchar_t *haystack, const wchar_t *needle) +{ + return wcsstr(haystack, needle); +} diff --git a/05/musl-final/src/string/wmemchr.c b/05/musl-final/src/string/wmemchr.c new file mode 100644 index 0000000..a3ee0e6 --- /dev/null +++ b/05/musl-final/src/string/wmemchr.c @@ -0,0 +1,8 @@ +#include +#include + +wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n) +{ + for (; n && *s != c; s++); + return n ? (wchar_t *)s : 0; +} diff --git a/05/musl-final/src/string/wmemcmp.c b/05/musl-final/src/string/wmemcmp.c new file mode 100644 index 0000000..6788a38 --- /dev/null +++ b/05/musl-final/src/string/wmemcmp.c @@ -0,0 +1,8 @@ +#include +#include + +int wmemcmp(const wchar_t *l, const wchar_t *r, size_t n) +{ + for (; n && *l==*r; n--, l++, r++); + return n ? *l-*r : 0; +} diff --git a/05/musl-final/src/string/wmemcpy.c b/05/musl-final/src/string/wmemcpy.c new file mode 100644 index 0000000..330e37c --- /dev/null +++ b/05/musl-final/src/string/wmemcpy.c @@ -0,0 +1,9 @@ +#include +#include + +wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n) +{ + wchar_t *a = d; + while (n--) *d++ = *s++; + return a; +} diff --git a/05/musl-final/src/string/wmemmove.c b/05/musl-final/src/string/wmemmove.c new file mode 100644 index 0000000..49608ca --- /dev/null +++ b/05/musl-final/src/string/wmemmove.c @@ -0,0 +1,11 @@ +#include +#include + +wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n) +{ + if ((size_t)(d-s) < n) { + while (n--) d[n] = s[n]; + return d; + } + return wmemcpy(d, s, n); +} diff --git a/05/musl-final/src/string/wmemset.c b/05/musl-final/src/string/wmemset.c new file mode 100644 index 0000000..1a2a861 --- /dev/null +++ b/05/musl-final/src/string/wmemset.c @@ -0,0 +1,9 @@ +#include +#include + +wchar_t *wmemset(wchar_t *d, wchar_t c, size_t n) +{ + wchar_t *ret = d; + while (n--) *d++ = c; + return ret; +} diff --git a/05/musl-final/src/stub/utmpx.c b/05/musl-final/src/stub/utmpx.c new file mode 100644 index 0000000..3200396 --- /dev/null +++ b/05/musl-final/src/stub/utmpx.c @@ -0,0 +1,30 @@ +#include +#include + +void endutxent(void) +{ +} + +void setutxent(void) +{ +} + +struct utmpx *getutxent(void) +{ + return NULL; +} + +struct utmpx *getutxid(const struct utmpx *ut) +{ + return NULL; +} + +struct utmpx *getutxline(const struct utmpx *ut) +{ + return NULL; +} + +struct utmpx *pututxline(const struct utmpx *ut) +{ + return NULL; +} diff --git a/05/musl-final/src/syscall.s b/05/musl-final/src/syscall.s new file mode 100644 index 0000000..67af412 --- /dev/null +++ b/05/musl-final/src/syscall.s @@ -0,0 +1,30 @@ +# this file is necessary because tcc doesn't like musl's inline-assembly implementation +# of syscall +.global syscall0 +.global syscall1 +.global syscall2 +.global syscall3 +.global syscall4 +.global syscall5 +.global syscall6 + +syscall0: +syscall1: +syscall2: +syscall3: +syscall4: +syscall5: +syscall6: + # SysV calling convention: RDI, RSI, RDX, RCX, R8, R9, 8(%rsp) + # Linux syscall calling convention: RAX, RDI, RSI, RDX, R10, R8, R9 + mov %rdi, %rax + mov %rsi, %rdi + mov %rdx, %rsi + mov %rcx, %rdx + mov %r8, %r10 + mov %r9, %r8 + mov 8(%rsp), %r9 + syscall + mov %rax, %rdi + call __syscall_ret + ret diff --git a/05/musl-final/src/temp/mkdtemp.c b/05/musl-final/src/temp/mkdtemp.c new file mode 100644 index 0000000..162d98b --- /dev/null +++ b/05/musl-final/src/temp/mkdtemp.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +char *__mktemp(char *); + +char *mkdtemp(char *template) +{ + for (;;) { + if (!__mktemp(template)) return 0; + if (!mkdir(template, 0700)) return template; + if (errno != EEXIST) return 0; + /* this is safe because mktemp verified + * that we have a valid template string */ + strcpy(template+strlen(template)-6, "XXXXXX"); + } +} diff --git a/05/musl-final/src/temp/mkstemp.c b/05/musl-final/src/temp/mkstemp.c new file mode 100644 index 0000000..5e8bb93 --- /dev/null +++ b/05/musl-final/src/temp/mkstemp.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +char *__mktemp(char *); + +int mkstemp(char *template) +{ + int fd; +retry: + if (!__mktemp(template)) return -1; + fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) return fd; + if (errno == EEXIST) { + /* this is safe because mktemp verified + * that we have a valid template string */ + strcpy(template+strlen(template)-6, "XXXXXX"); + goto retry; + } + return -1; +} + +LFS64(mkstemp); diff --git a/05/musl-final/src/temp/mktemp.c b/05/musl-final/src/temp/mktemp.c new file mode 100644 index 0000000..1078b9d --- /dev/null +++ b/05/musl-final/src/temp/mktemp.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include +#include +#include "libc.h" + +char *__mktemp(char *template) +{ + static int lock; + static int index; + int l = strlen(template); + + if (l < 6 || strcmp(template+l-6, "XXXXXX")) { + errno = EINVAL; + return NULL; + } + LOCK(&lock); + for (; index < 1000000; index++) { + snprintf(template+l-6, 6, "%06d", index); + if (access(template, F_OK) != 0) { + UNLOCK(&lock); + return template; + } + } + UNLOCK(&lock); + return NULL; +} + +weak_alias(__mktemp, mktemp); diff --git a/05/musl-final/src/termios/cfgetospeed.c b/05/musl-final/src/termios/cfgetospeed.c new file mode 100644 index 0000000..0ebc198 --- /dev/null +++ b/05/musl-final/src/termios/cfgetospeed.c @@ -0,0 +1,12 @@ +#include +#include + +speed_t cfgetospeed(const struct termios *tio) +{ + return tio->c_cflag & CBAUD; +} + +speed_t cfgetispeed(const struct termios *tio) +{ + return cfgetospeed(tio); +} diff --git a/05/musl-final/src/termios/cfsetospeed.c b/05/musl-final/src/termios/cfsetospeed.c new file mode 100644 index 0000000..80c790f --- /dev/null +++ b/05/musl-final/src/termios/cfsetospeed.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include "libc.h" + +int cfsetospeed(struct termios *tio, speed_t speed) +{ + if (speed & ~CBAUD) { + errno = EINVAL; + return -1; + } + tio->c_cflag &= ~CBAUD; + tio->c_cflag |= speed; + return 0; +} + +int cfsetispeed(struct termios *tio, speed_t speed) +{ + return speed ? cfsetospeed(tio, speed) : 0; +} + +weak_alias(cfsetospeed, cfsetspeed); diff --git a/05/musl-final/src/termios/tcdrain.c b/05/musl-final/src/termios/tcdrain.c new file mode 100644 index 0000000..c51dd40 --- /dev/null +++ b/05/musl-final/src/termios/tcdrain.c @@ -0,0 +1,7 @@ +#include +#include + +int tcdrain(int fd) +{ + return ioctl(fd, TCSBRK, 1); +} diff --git a/05/musl-final/src/termios/tcflow.c b/05/musl-final/src/termios/tcflow.c new file mode 100644 index 0000000..c7fc3fe --- /dev/null +++ b/05/musl-final/src/termios/tcflow.c @@ -0,0 +1,7 @@ +#include +#include + +int tcflow(int fd, int action) +{ + return ioctl(fd, TCXONC, action); +} diff --git a/05/musl-final/src/termios/tcflush.c b/05/musl-final/src/termios/tcflush.c new file mode 100644 index 0000000..5022266 --- /dev/null +++ b/05/musl-final/src/termios/tcflush.c @@ -0,0 +1,7 @@ +#include +#include + +int tcflush(int fd, int queue) +{ + return ioctl(fd, TCFLSH, queue); +} diff --git a/05/musl-final/src/termios/tcgetattr.c b/05/musl-final/src/termios/tcgetattr.c new file mode 100644 index 0000000..d9ce786 --- /dev/null +++ b/05/musl-final/src/termios/tcgetattr.c @@ -0,0 +1,10 @@ +#include +#include +#include + +int tcgetattr(int fd, struct termios *tio) +{ + if (ioctl(fd, TCGETS, tio)) + return -1; + return 0; +} diff --git a/05/musl-final/src/termios/tcgetsid.c b/05/musl-final/src/termios/tcgetsid.c new file mode 100644 index 0000000..1053fd6 --- /dev/null +++ b/05/musl-final/src/termios/tcgetsid.c @@ -0,0 +1,10 @@ +#include +#include + +pid_t tcgetsid(int fd) +{ + int sid; + if (ioctl(fd, TIOCGSID, &sid) < 0) + return -1; + return sid; +} diff --git a/05/musl-final/src/termios/tcsendbreak.c b/05/musl-final/src/termios/tcsendbreak.c new file mode 100644 index 0000000..b6df0a2 --- /dev/null +++ b/05/musl-final/src/termios/tcsendbreak.c @@ -0,0 +1,8 @@ +#include +#include + +int tcsendbreak(int fd, int dur) +{ + /* nonzero duration is implementation-defined, so ignore it */ + return ioctl(fd, TCSBRK, 0); +} diff --git a/05/musl-final/src/termios/tcsetattr.c b/05/musl-final/src/termios/tcsetattr.c new file mode 100644 index 0000000..e9a168f --- /dev/null +++ b/05/musl-final/src/termios/tcsetattr.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +int tcsetattr(int fd, int act, const struct termios *tio) +{ + if (act < 0 || act > 2) { + errno = EINVAL; + return -1; + } + return ioctl(fd, TCSETS+act, tio); +} diff --git a/05/musl-final/src/thread/__futex.c b/05/musl-final/src/thread/__futex.c new file mode 100644 index 0000000..93352fa --- /dev/null +++ b/05/musl-final/src/thread/__futex.c @@ -0,0 +1,8 @@ +#include "futex.h" +#include "syscall.h" + +int __futex(volatile int *addr, int op, int val, void *ts) +{ + return syscall4(__NR_futex, (long)addr, op, val, (long)ts); +} + diff --git a/05/musl-final/src/thread/__lock.c b/05/musl-final/src/thread/__lock.c new file mode 100644 index 0000000..557c6a6 --- /dev/null +++ b/05/musl-final/src/thread/__lock.c @@ -0,0 +1,12 @@ +#define SYSCALL_RETURN_ERRNO +#include "pthread_impl.h" + +void __lock(volatile int *l) +{ + int spins=100000; + /* Do not use futexes because we insist that unlocking is a simple + * assignment to optimize non-pathological code with no contention. */ + while (a_xchg(l, 1)) + if (spins) spins--, a_spin(); + else syscall0(__NR_sched_yield); +} diff --git a/05/musl-final/src/thread/__set_thread_area.c b/05/musl-final/src/thread/__set_thread_area.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/thread/__timedwait.c b/05/musl-final/src/thread/__timedwait.c new file mode 100644 index 0000000..354def2 --- /dev/null +++ b/05/musl-final/src/thread/__timedwait.c @@ -0,0 +1,21 @@ +#include +#include +#include "futex.h" +#define SYSCALL_RETURN_ERRNO +#include "syscall.h" +#include +int __timedwait(volatile int *addr, int val, clockid_t clk, const struct timespec *at, int priv) +{ + struct timespec to; + if (at) { + clock_gettime(clk, &to); + to.tv_sec = at->tv_sec - to.tv_sec; + if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) { + to.tv_sec--; + to.tv_nsec += 1000000000; + } + if (to.tv_sec < 0) return ETIMEDOUT; + } + if (priv) priv = 128; priv=0; + return syscall4(__NR_futex, (long)addr, FUTEX_WAIT | priv, val, at ? (long)&to : 0); +} diff --git a/05/musl-final/src/thread/__unmapself.c b/05/musl-final/src/thread/__unmapself.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/thread/__wait.c b/05/musl-final/src/thread/__wait.c new file mode 100644 index 0000000..8c249cd --- /dev/null +++ b/05/musl-final/src/thread/__wait.c @@ -0,0 +1,16 @@ +#define SYSCALL_RETURN_ERRNO +#include "pthread_impl.h" + +void __wait(volatile int *addr, volatile int *waiters, int val, int priv) +{ + int spins=50000; + if (priv) priv = 128; priv=0; + while (spins--) { + if (*addr==val) a_spin(); + else return; + } + if (waiters) a_inc(waiters); + while (*addr==val) + syscall4(__NR_futex, (long)addr, FUTEX_WAIT|priv, val, 0); + if (waiters) a_dec(waiters); +} diff --git a/05/musl-final/src/thread/__wake.c b/05/musl-final/src/thread/__wake.c new file mode 100644 index 0000000..048ddcc --- /dev/null +++ b/05/musl-final/src/thread/__wake.c @@ -0,0 +1,9 @@ +#define SYSCALL_RETURN_ERRNO +#include "pthread_impl.h" + +void __wake(volatile int *addr, int cnt, int priv) +{ + if (priv) priv = 128; priv=0; + if (cnt<0) cnt = INT_MAX; + syscall3(__NR_futex, (long)addr, FUTEX_WAKE | priv, cnt); +} diff --git a/05/musl-final/src/thread/cancellation.c b/05/musl-final/src/thread/cancellation.c new file mode 100644 index 0000000..ac1af30 --- /dev/null +++ b/05/musl-final/src/thread/cancellation.c @@ -0,0 +1,14 @@ +#include "pthread_impl.h" + +void __pthread_register_cancel(struct __ptcb *cb) +{ + struct pthread *self = pthread_self(); + cb->__next = self->cancelbuf; + self->cancelbuf = cb; +} + +void __pthread_unregister_cancel(struct __ptcb *cb) +{ + struct pthread *self = __pthread_self(); + self->cancelbuf = self->cancelbuf->__next; +} diff --git a/05/musl-final/src/thread/clone.c b/05/musl-final/src/thread/clone.c new file mode 100644 index 0000000..e69de29 diff --git a/05/musl-final/src/thread/i386/__set_thread_area.s b/05/musl-final/src/thread/i386/__set_thread_area.s new file mode 100644 index 0000000..9bf698b --- /dev/null +++ b/05/musl-final/src/thread/i386/__set_thread_area.s @@ -0,0 +1,22 @@ +.text +.global __set_thread_area +.type __set_thread_area,%function +__set_thread_area: + pushl %ebx + movl 8(%esp),%ecx + movl $-1,4(%ecx) + movl %ecx,8(%ecx) + movl $0xfffff,12(%ecx) + movl $0x51,16(%ecx) + leal 4(%ecx),%ebx + movl $243,%eax + int $128 + popl %ebx + testl %eax,%eax + jnz 1f + movl 4(%ecx),%ecx + leal 3(,%ecx,8),%ecx + movw %cx,%gs +1: + ret +.size __set_thread_area,.-__set_thread_area diff --git a/05/musl-final/src/thread/i386/__unmapself.s b/05/musl-final/src/thread/i386/__unmapself.s new file mode 100644 index 0000000..5c67496 --- /dev/null +++ b/05/musl-final/src/thread/i386/__unmapself.s @@ -0,0 +1,22 @@ +.text +.global __unmapself +.type __unmapself,%function +__unmapself: + call 1f + .long -1 + .long -1 +1: popl %ecx + xorl %ebx,%ebx + xorl %edx,%edx + movl $8,%esi + movl $175,%eax + int $128 + movl $91,%eax + movl 4(%esp),%ebx + movl 8(%esp),%ecx + int $128 + xorl %ebx,%ebx + movl $1,%eax + int $128 + +.size __unmapself,.-__unmapself diff --git a/05/musl-final/src/thread/i386/clone.s b/05/musl-final/src/thread/i386/clone.s new file mode 100644 index 0000000..53405cf --- /dev/null +++ b/05/musl-final/src/thread/i386/clone.s @@ -0,0 +1,27 @@ +.text +.global __uniclone +.type __uniclone,%function +__uniclone: + movl 4(%esp),%ecx + subl $24,%ecx + movl 8(%esp),%eax + movl %eax,16(%ecx) + movl 12(%esp),%eax + movl %eax,24(%ecx) + pushl %ebx + pushl %esi + pushl %edi + pushl %ebp + movl %eax,8(%eax) + leal 20(%eax),%edx + leal 4(%eax),%esi + movl %edx,%edi + movl $0x7d0f00,%ebx + movl $120,%eax + int $128 + popl %ebp + popl %edi + popl %esi + popl %ebx + ret +.size __uniclone,.-__uniclone diff --git a/05/musl-final/src/thread/pthread_attr_destroy.c b/05/musl-final/src/thread/pthread_attr_destroy.c new file mode 100644 index 0000000..b5845dd --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_destroy.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_attr_destroy(pthread_attr_t *a) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_getdetachstate.c b/05/musl-final/src/thread/pthread_attr_getdetachstate.c new file mode 100644 index 0000000..de65c25 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_getdetachstate.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_attr_getdetachstate(pthread_attr_t *a, int *state) +{ + *state = a->_a_detach; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_getguardsize.c b/05/musl-final/src/thread/pthread_attr_getguardsize.c new file mode 100644 index 0000000..e6b9ee2 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_getguardsize.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_attr_getguardsize(pthread_attr_t *a, size_t *size) +{ + *size = a->_a_guardsize + DEFAULT_GUARD_SIZE; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_getscope.c b/05/musl-final/src/thread/pthread_attr_getscope.c new file mode 100644 index 0000000..0cb224d --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_getscope.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_attr_getscope(pthread_attr_t *a, int *scope) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_getstacksize.c b/05/musl-final/src/thread/pthread_attr_getstacksize.c new file mode 100644 index 0000000..900dc9f --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_getstacksize.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_attr_getstacksize(pthread_attr_t *a, size_t *size) +{ + *size = a->_a_stacksize + DEFAULT_STACK_SIZE; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_init.c b/05/musl-final/src/thread/pthread_attr_init.c new file mode 100644 index 0000000..d91bf15 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_init.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_attr_init(pthread_attr_t *a) +{ + memset(a, 0, sizeof *a); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_setdetachstate.c b/05/musl-final/src/thread/pthread_attr_setdetachstate.c new file mode 100644 index 0000000..bfffba8 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_setdetachstate.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_attr_setdetachstate(pthread_attr_t *a, int state) +{ + a->_a_detach = state; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_setguardsize.c b/05/musl-final/src/thread/pthread_attr_setguardsize.c new file mode 100644 index 0000000..9f21d24 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_setguardsize.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_attr_setguardsize(pthread_attr_t *a, size_t size) +{ + if (size > SIZE_MAX/8) return EINVAL; + a->_a_guardsize = size - DEFAULT_GUARD_SIZE; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_setscope.c b/05/musl-final/src/thread/pthread_attr_setscope.c new file mode 100644 index 0000000..a970a81 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_setscope.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_attr_setscope(pthread_attr_t *a, int scope) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_attr_setstacksize.c b/05/musl-final/src/thread/pthread_attr_setstacksize.c new file mode 100644 index 0000000..df16b98 --- /dev/null +++ b/05/musl-final/src/thread/pthread_attr_setstacksize.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_attr_setstacksize(pthread_attr_t *a, size_t size) +{ + if (size-PAGE_SIZE > SIZE_MAX/4) return EINVAL; + a->_a_stacksize = size - DEFAULT_STACK_SIZE; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_barrier_destroy.c b/05/musl-final/src/thread/pthread_barrier_destroy.c new file mode 100644 index 0000000..2898c41 --- /dev/null +++ b/05/musl-final/src/thread/pthread_barrier_destroy.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_barrier_destroy(pthread_barrier_t *b) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_barrier_init.c b/05/musl-final/src/thread/pthread_barrier_init.c new file mode 100644 index 0000000..736d101 --- /dev/null +++ b/05/musl-final/src/thread/pthread_barrier_init.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *a, unsigned count) +{ + if (!count) return EINVAL; + *b = (pthread_barrier_t){ ._b_limit = count-1 }; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_barrier_wait.c b/05/musl-final/src/thread/pthread_barrier_wait.c new file mode 100644 index 0000000..7bfadb9 --- /dev/null +++ b/05/musl-final/src/thread/pthread_barrier_wait.c @@ -0,0 +1,31 @@ +#include "pthread_impl.h" + +int pthread_barrier_wait(pthread_barrier_t *b) +{ + int cur; + + /* Trivial case: count was set at 1 */ + if (!b->_b_limit) return PTHREAD_BARRIER_SERIAL_THREAD; + + /* Wait for anyone still suspended at previous use of barrier */ + while ((cur=b->_b_left)) + __wait(&b->_b_left, &b->_b_waiters, cur, 0); + + /* If we are the last to reach barrier, reset it and wake others */ + if (a_fetch_add(&b->_b_count, 1) == b->_b_limit) { + b->_b_left = b->_b_limit; + b->_b_count = 0; + __wake(&b->_b_count, -1, 0); + return PTHREAD_BARRIER_SERIAL_THREAD; + } + + /* Wait for our peers to reach the barrier */ + while ((cur=b->_b_count)) + __wait(&b->_b_count, 0, cur, 0); + + /* If we're the last to wake up and barrier is awaiting reuse */ + if (a_fetch_add(&b->_b_left, -1) == 1 && b->_b_waiters) + __wake(&b->_b_left, -1, 0); + + return 0; +} diff --git a/05/musl-final/src/thread/pthread_cancel.c b/05/musl-final/src/thread/pthread_cancel.c new file mode 100644 index 0000000..9397ffe --- /dev/null +++ b/05/musl-final/src/thread/pthread_cancel.c @@ -0,0 +1,7 @@ +#define SYSCALL_RETURN_ERRNO +#include "pthread_impl.h" + +int pthread_cancel(pthread_t t) +{ + return syscall3(__NR_tgkill, t->pid, t->tid, SIGCANCEL); +} diff --git a/05/musl-final/src/thread/pthread_cond_broadcast.c b/05/musl-final/src/thread/pthread_cond_broadcast.c new file mode 100644 index 0000000..1d378c0 --- /dev/null +++ b/05/musl-final/src/thread/pthread_cond_broadcast.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_cond_broadcast(pthread_cond_t *c) +{ + c->_c_block = 0; + __wake(&c->_c_block, -1, 0); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_cond_destroy.c b/05/musl-final/src/thread/pthread_cond_destroy.c new file mode 100644 index 0000000..1d21a5a --- /dev/null +++ b/05/musl-final/src/thread/pthread_cond_destroy.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_cond_destroy(pthread_cond_t *c) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_cond_init.c b/05/musl-final/src/thread/pthread_cond_init.c new file mode 100644 index 0000000..3394860 --- /dev/null +++ b/05/musl-final/src/thread/pthread_cond_init.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a) +{ + memset(c, 0, sizeof *c); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_cond_signal.c b/05/musl-final/src/thread/pthread_cond_signal.c new file mode 100644 index 0000000..574f5c7 --- /dev/null +++ b/05/musl-final/src/thread/pthread_cond_signal.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_cond_signal(pthread_cond_t *c) +{ + c->_c_block = 0; + __wake(&c->_c_block, 1, 0); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_cond_timedwait.c b/05/musl-final/src/thread/pthread_cond_timedwait.c new file mode 100644 index 0000000..b0214b4 --- /dev/null +++ b/05/musl-final/src/thread/pthread_cond_timedwait.c @@ -0,0 +1,26 @@ +#include "pthread_impl.h" + +static void relock(void *m) +{ + pthread_mutex_lock(m); +} + +int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *ts) +{ + int r, e=0; + CANCELPT(0); + + pthread_cleanup_push(relock, m); + c->_c_block = 1; + if ((r=pthread_mutex_unlock(m))) return r; + + CANCELPT(1); + e = __timedwait(&c->_c_block, 1, CLOCK_REALTIME, ts, 0); + CANCELPT(0); + + pthread_cleanup_pop(0); + if ((r=pthread_mutex_lock(m))) return r; + + CANCELPT(0); + return e; +} diff --git a/05/musl-final/src/thread/pthread_cond_wait.c b/05/musl-final/src/thread/pthread_cond_wait.c new file mode 100644 index 0000000..eb70e5f --- /dev/null +++ b/05/musl-final/src/thread/pthread_cond_wait.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m) +{ + return pthread_cond_timedwait(c, m, 0); +} diff --git a/05/musl-final/src/thread/pthread_create.c b/05/musl-final/src/thread/pthread_create.c new file mode 100644 index 0000000..06d554b --- /dev/null +++ b/05/musl-final/src/thread/pthread_create.c @@ -0,0 +1,228 @@ +#include "pthread_impl.h" + +void __pthread_unwind_next(struct __ptcb *cb) +{ + int i, j, not_finished; + pthread_t self; + + if (cb->__next) longjmp((void *)cb->__next->__jb, 1); + + self = pthread_self(); + if (self->cancel) self->result = PTHREAD_CANCELLED; + + if (!a_fetch_add(&libc.threads_minus_1, -1)) + exit(0); + + LOCK(&self->exitlock); + + not_finished = self->tsd_used; + for (j=0; not_finished && jtsd[i] && libc.tsd_keys[i]) { + void *tmp = self->tsd[i]; + self->tsd[i] = 0; + libc.tsd_keys[i](tmp); + not_finished = 1; + } + } + } + + if (self->detached && self->map_base) + __unmapself(self->map_base, self->map_size); + + __syscall_exit(0); +} + +static void docancel(struct pthread *self) +{ + struct __ptcb cb = { .__next = self->cancelbuf }; + __pthread_unwind_next(&cb); +} + +static void cancel_handler(int sig, siginfo_t *si, void *ctx) +{ + struct pthread *self = __pthread_self(); + self->cancel = 1; + if (self->canceldisable || (!self->cancelasync && !self->cancelpoint)) + return; + docancel(self); +} + +static void cancelpt(int x) +{ + struct pthread *self = __pthread_self(); + if (self->canceldisable) return; + self->cancelpoint = x; + if (self->cancel) docancel(self); +} + +/* "rsyscall" is a mechanism by which a thread can synchronously force all + * other threads to perform an arbitrary syscall. It is necessary to work + * around the non-conformant implementation of setuid() et al on Linux, + * which affect only the calling thread and not the whole process. This + * implementation performs some tricks with signal delivery to work around + * the fact that it does not keep any list of threads in userspace. */ + +static struct { + volatile int lock, hold, blocks, cnt; + unsigned long arg[6]; + int nr; + int err; +} rs; + +static void rsyscall_handler(int sig, siginfo_t *si, void *ctx) +{ + if (rs.cnt == libc.threads_minus_1) return; + + if (syscall6(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2], + rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno; + + a_inc(&rs.cnt); + __wake(&rs.cnt, 1, 1); + while(rs.hold) + __wait(&rs.hold, 0, 1, 1); + a_dec(&rs.cnt); + if (!rs.cnt) __wake(&rs.cnt, 1, 1); +} + +static int rsyscall(int nr, long a, long b, long c, long d, long e, long f) +{ + int i, ret; + sigset_t set = { 0 }; + struct pthread *self = __pthread_self(); + sigaddset(&set, SIGSYSCALL); + + LOCK(&rs.lock); + while ((i=rs.blocks)) + __wait(&rs.blocks, 0, i, 1); + + __libc_sigprocmask(SIG_BLOCK, &set, 0); + + rs.nr = nr; + rs.arg[0] = a; rs.arg[1] = b; + rs.arg[2] = c; rs.arg[3] = d; + rs.arg[4] = d; rs.arg[5] = f; + rs.hold = 1; + rs.err = 0; + rs.cnt = 0; + + /* Dispatch signals until all threads respond */ + for (i=libc.threads_minus_1; i; i--) + sigqueue(self->pid, SIGSYSCALL, (union sigval){0}); + while ((i=rs.cnt) < libc.threads_minus_1) { + sigqueue(self->pid, SIGSYSCALL, (union sigval){0}); + __wait(&rs.cnt, 0, i, 1); + } + + /* Handle any lingering signals with no-op */ + __libc_sigprocmask(SIG_UNBLOCK, &set, 0); + + /* Resume other threads' signal handlers and wait for them */ + rs.hold = 0; + __wake(&rs.hold, -1, 0); + while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1); + + if (rs.err) errno = rs.err, ret = -1; + else ret = syscall6(nr, a, b, c, d, e, f); + + UNLOCK(&rs.lock); + return ret; +} + +static void init_threads() +{ + struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART }; + libc.lock = __lock; + libc.cancelpt = cancelpt; + libc.rsyscall = rsyscall; + sa.sa_sigaction = cancel_handler; + __libc_sigaction(SIGCANCEL, &sa, 0); + sigaddset(&sa.sa_mask, SIGSYSCALL); + sigaddset(&sa.sa_mask, SIGCANCEL); + sa.sa_sigaction = rsyscall_handler; + __libc_sigaction(SIGSYSCALL, &sa, 0); + sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0); +} + +static int start(void *p) +{ + struct pthread *self = p; + pthread_exit(self->start(self->start_arg)); + return 0; +} + +int __uniclone(void *, int (*)(), void *); + +#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE) + +#if 0 +/* pthread_key_create.c overrides this */ +static const size_t dummy = 0; +weak_alias(dummy, __pthread_tsd_size); +#else +extern size_t __pthread_tsd_size; +#endif + +int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg) +{ + static int init; + int ret; + size_t size, guard; + struct pthread *self = pthread_self(), *new; + unsigned char *map, *stack, *tsd; + static const pthread_attr_t default_attr; + + if (!self) return errno = ENOSYS; + if (!init && ++init) init_threads(); + + if (!attr) attr = &default_attr; + guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE); + size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE); + size += __pthread_tsd_size; + map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0); + if (!map) return EAGAIN; + mprotect(map, guard, PROT_NONE); + + tsd = map + size - __pthread_tsd_size; + new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new); + new->map_base = map; + new->map_size = size; + new->pid = self->pid; + new->errno_ptr = &new->errno_val; + new->start = entry; + new->start_arg = arg; + new->self = new; + new->tsd = (void *)tsd; + new->detached = attr->_a_detach; + new->attr = *attr; + memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc); + new->tlsdesc[1] = (uintptr_t)new; + stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15); + + /* We must synchronize new thread creation with rsyscall + * delivery. This looks to be the least expensive way: */ + a_inc(&rs.blocks); + while (rs.lock) __wait(&rs.lock, 0, 1, 1); + + a_inc(&libc.threads_minus_1); + ret = __uniclone(stack, start, new); + + a_dec(&rs.blocks); + if (rs.lock) __wake(&rs.blocks, 1, 1); + + if (ret < 0) { + a_dec(&libc.threads_minus_1); + munmap(map, size); + return EAGAIN; + } + *res = new; + return 0; +} + +void pthread_exit(void *result) +{ + struct pthread *self = pthread_self(); + self->result = result; + docancel(self); +} diff --git a/05/musl-final/src/thread/pthread_detach.c b/05/musl-final/src/thread/pthread_detach.c new file mode 100644 index 0000000..f0eae3e --- /dev/null +++ b/05/musl-final/src/thread/pthread_detach.c @@ -0,0 +1,11 @@ +#include "pthread_impl.h" + +int pthread_detach(pthread_t t) +{ + /* Cannot detach a thread that's already exiting */ + if (a_xchg(&t->exitlock, 1)) + return pthread_join(t, 0); + t->detached = 1; + t->exitlock = 0; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_equal.c b/05/musl-final/src/thread/pthread_equal.c new file mode 100644 index 0000000..a55d280 --- /dev/null +++ b/05/musl-final/src/thread/pthread_equal.c @@ -0,0 +1,6 @@ +#include + +int pthread_equal(pthread_t a, pthread_t b) +{ + return a==b; +} diff --git a/05/musl-final/src/thread/pthread_getspecific.c b/05/musl-final/src/thread/pthread_getspecific.c new file mode 100644 index 0000000..a6ca27d --- /dev/null +++ b/05/musl-final/src/thread/pthread_getspecific.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +void *pthread_getspecific(pthread_key_t k) +{ + struct pthread *self = pthread_self(); + if (!self->tsd) return 0; + return self->tsd[k]; +} diff --git a/05/musl-final/src/thread/pthread_join.c b/05/musl-final/src/thread/pthread_join.c new file mode 100644 index 0000000..5210ed4 --- /dev/null +++ b/05/musl-final/src/thread/pthread_join.c @@ -0,0 +1,12 @@ +#include "pthread_impl.h" + +int pthread_join(pthread_t t, void **res) +{ + int tmp = t->tid; + CANCELPT_BEGIN; + if (tmp) __wait(&t->tid, 0, tmp, 1); + CANCELPT_END; + if (res) *res = t->result; + if (t->map_base) munmap(t->map_base, t->map_size); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_key_create.c b/05/musl-final/src/thread/pthread_key_create.c new file mode 100644 index 0000000..efc3804 --- /dev/null +++ b/05/musl-final/src/thread/pthread_key_create.c @@ -0,0 +1,25 @@ +#include "pthread_impl.h" + +const size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX; + +static void nodtor(void *dummy) +{ +} + +int pthread_key_create(pthread_key_t *k, void (*dtor)(void *)) +{ + static void (*keys[PTHREAD_KEYS_MAX])(void *); + int i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX; + int j = i; + + libc.tsd_keys = keys; + if (!dtor) dtor = nodtor; + /* Cheap trick - &k cannot match any destructor pointer */ + while (a_cas_p(keys+j, 0, &k) + && (j=(j+1)%PTHREAD_KEYS_MAX) != i); + if (keys[j] != (void (*)(void *))&k) + return EAGAIN; + keys[j] = dtor; + *k = j; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_key_delete.c b/05/musl-final/src/thread/pthread_key_delete.c new file mode 100644 index 0000000..4914ebb --- /dev/null +++ b/05/musl-final/src/thread/pthread_key_delete.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_key_delete(pthread_key_t k) +{ + if (libc.tsd_keys) libc.tsd_keys[k] = 0; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_kill.c b/05/musl-final/src/thread/pthread_kill.c new file mode 100644 index 0000000..9d85fa5 --- /dev/null +++ b/05/musl-final/src/thread/pthread_kill.c @@ -0,0 +1,7 @@ +#define SYSCALL_RETURN_ERRNO +#include "pthread_impl.h" + +int pthread_kill(pthread_t t, int sig) +{ + return syscall3(__NR_tgkill, t->pid, t->tid, sig); +} diff --git a/05/musl-final/src/thread/pthread_mutex_destroy.c b/05/musl-final/src/thread/pthread_mutex_destroy.c new file mode 100644 index 0000000..6d49e68 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutex_destroy.c @@ -0,0 +1,6 @@ +#include + +int pthread_mutex_destroy(pthread_mutex_t *mutex) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutex_init.c b/05/musl-final/src/thread/pthread_mutex_init.c new file mode 100644 index 0000000..d453543 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutex_init.c @@ -0,0 +1,9 @@ +#include "pthread_impl.h" + +int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a) +{ + memset(m, 0, sizeof *m); + if (a) { + } + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutex_lock.c b/05/musl-final/src/thread/pthread_mutex_lock.c new file mode 100644 index 0000000..15ede3f --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutex_lock.c @@ -0,0 +1,9 @@ +#include "pthread_impl.h" + +int pthread_mutex_lock(pthread_mutex_t *m) +{ + int r; + while ((r=pthread_mutex_trylock(m)) == EBUSY) + __wait(&m->_m_lock, &m->_m_waiters, 1, 0); + return r; +} diff --git a/05/musl-final/src/thread/pthread_mutex_timedlock.c b/05/musl-final/src/thread/pthread_mutex_timedlock.c new file mode 100644 index 0000000..20ce012 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutex_timedlock.c @@ -0,0 +1,15 @@ +#include "pthread_impl.h" + +int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *at) +{ + int r, w=0; + while ((r=pthread_mutex_trylock(m)) == EBUSY) { + if (!w) a_inc(&m->_m_waiters), w++; + if (__timedwait(&m->_m_lock, 1, CLOCK_REALTIME, at, 0) == ETIMEDOUT) { + if (w) a_dec(&m->_m_waiters); + return ETIMEDOUT; + } + } + if (w) a_dec(&m->_m_waiters); + return r; +} diff --git a/05/musl-final/src/thread/pthread_mutex_trylock.c b/05/musl-final/src/thread/pthread_mutex_trylock.c new file mode 100644 index 0000000..29268fd --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutex_trylock.c @@ -0,0 +1,28 @@ +#include "pthread_impl.h" + +int pthread_mutex_trylock(pthread_mutex_t *m) +{ + if (m->_m_type == PTHREAD_MUTEX_RECURSIVE) { + pthread_t self = pthread_self(); + if (m->_m_owner == self->tid) { + if ((unsigned)m->_m_lock >= INT_MAX) return EAGAIN; + a_inc(&m->_m_lock); + return 0; + } + if (a_fetch_add(&m->_m_lock, 1)) { + if (a_fetch_add(&m->_m_lock, -1)==1 && m->_m_waiters) + __wake(&m->_m_lock, 1, 0); + return EBUSY; + } + m->_m_owner = self->tid; + return 0; + } + + if (a_xchg(&m->_m_lock, 1)) + if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK + && m->_m_owner == pthread_self()->tid) return EDEADLK; + else return EBUSY; + if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK) + m->_m_owner = pthread_self()->tid; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutex_unlock.c b/05/musl-final/src/thread/pthread_mutex_unlock.c new file mode 100644 index 0000000..0275eb5 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutex_unlock.c @@ -0,0 +1,19 @@ +#include "pthread_impl.h" + +int pthread_mutex_unlock(pthread_mutex_t *m) +{ + if (m->_m_type == PTHREAD_MUTEX_RECURSIVE) { + if (a_fetch_add(&m->_m_lock, -1)==1 && m->_m_waiters) + __wake(&m->_m_lock, 1, 0); + return 0; + } + + if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK + && m->_m_owner != pthread_self()->tid) + return EPERM; + + m->_m_owner = 0; + m->_m_lock = 0; + if (m->_m_waiters) __wake(&m->_m_lock, 1, 0); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutexattr_destroy.c b/05/musl-final/src/thread/pthread_mutexattr_destroy.c new file mode 100644 index 0000000..9fd6974 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutexattr_destroy.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_mutexattr_destroy(pthread_mutexattr_t *a) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutexattr_gettype.c b/05/musl-final/src/thread/pthread_mutexattr_gettype.c new file mode 100644 index 0000000..9edb16c --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutexattr_gettype.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_mutexattr_gettype(const pthread_mutexattr_t *a, int *type) +{ + *type = *a & 3; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutexattr_init.c b/05/musl-final/src/thread/pthread_mutexattr_init.c new file mode 100644 index 0000000..ea63106 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutexattr_init.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_mutexattr_init(pthread_mutexattr_t *a) +{ + memset(a, 0, sizeof *a); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_mutexattr_settype.c b/05/musl-final/src/thread/pthread_mutexattr_settype.c new file mode 100644 index 0000000..4e85950 --- /dev/null +++ b/05/musl-final/src/thread/pthread_mutexattr_settype.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_mutexattr_settype(pthread_mutexattr_t *a, int type) +{ + if ((unsigned)type > 2) return EINVAL; + *a = (*a & ~3) | type; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_once.c b/05/musl-final/src/thread/pthread_once.c new file mode 100644 index 0000000..7223005 --- /dev/null +++ b/05/musl-final/src/thread/pthread_once.c @@ -0,0 +1,38 @@ +#include "pthread_impl.h" + +static void undo(void *control) +{ + a_store(control, 0); + __wake(control, 1, 0); +} + +int pthread_once(pthread_once_t *control, void (*init)(void)) +{ + static int waiters; + + /* Return immediately if init finished before */ + if (*control == 2) return 0; + + /* Try to enter initializing state. Three possibilities: + * 0 - we're the first or the other cancelled; run init + * 1 - another thread is running init; wait + * 2 - another thread finished running init; just return */ + + for (;;) switch (a_swap(control, 1)) { + case 0: + break; + case 1: + __wait(control, &waiters, 1, 0); + continue; + case 2: + a_store(control, 2); + return 0; + } + + pthread_cleanup_push(undo, control); + init(); + pthread_cleanup_pop(0); + + if (waiters) __wake(control, -1, 0); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_destroy.c b/05/musl-final/src/thread/pthread_rwlock_destroy.c new file mode 100644 index 0000000..49ecfbd --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_destroy.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_rwlock_destroy(pthread_rwlock_t *rw) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_init.c b/05/musl-final/src/thread/pthread_rwlock_init.c new file mode 100644 index 0000000..f87d566 --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_init.c @@ -0,0 +1,9 @@ +#include "pthread_impl.h" + +int pthread_rwlock_init(pthread_rwlock_t *rw, const pthread_rwlockattr_t *a) +{ + memset(rw, 0, sizeof *rw); + if (a) { + } + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_rdlock.c b/05/musl-final/src/thread/pthread_rwlock_rdlock.c new file mode 100644 index 0000000..2986350 --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_rdlock.c @@ -0,0 +1,8 @@ +#include "pthread_impl.h" + +int pthread_rwlock_rdlock(pthread_rwlock_t *rw) +{ + while (pthread_rwlock_tryrdlock(rw)) + __wait(&rw->_rw_wrlock, &rw->_rw_waiters, 1, 0); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_timedrdlock.c b/05/musl-final/src/thread/pthread_rwlock_timedrdlock.c new file mode 100644 index 0000000..1e76a8e --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_timedrdlock.c @@ -0,0 +1,15 @@ +#include "pthread_impl.h" + +int pthread_rwlock_timedrdlock(pthread_rwlock_t *rw, const struct timespec *at) +{ + int w=0; + while (pthread_rwlock_tryrdlock(rw)) { + if (!w) a_inc(&rw->_rw_waiters), w++; + if (__timedwait(&rw->_rw_wrlock, 1, CLOCK_REALTIME, at, 0)==ETIMEDOUT) { + if (w) a_dec(&rw->_rw_waiters); + return ETIMEDOUT; + } + } + if (w) a_dec(&rw->_rw_waiters); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_timedwrlock.c b/05/musl-final/src/thread/pthread_rwlock_timedwrlock.c new file mode 100644 index 0000000..9682774 --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_timedwrlock.c @@ -0,0 +1,17 @@ +#include "pthread_impl.h" + +int pthread_rwlock_timedwrlock(pthread_rwlock_t *rw, const struct timespec *at) +{ + int nr, *p, w=0; + while (pthread_rwlock_trywrlock(rw)==EAGAIN) { + if (!w) a_inc(&rw->_rw_waiters), w++; + if ((nr=rw->_rw_readers)) p = &rw->_rw_readers; + else nr=1, p = &rw->_rw_wrlock; + if (__timedwait(p, nr, CLOCK_REALTIME, at, 0)==ETIMEDOUT) { + if (w) a_dec(&rw->_rw_waiters); + return ETIMEDOUT; + } + } + if (w) a_dec(&rw->_rw_waiters); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_tryrdlock.c b/05/musl-final/src/thread/pthread_rwlock_tryrdlock.c new file mode 100644 index 0000000..fc1d532 --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_tryrdlock.c @@ -0,0 +1,13 @@ +#include "pthread_impl.h" + +int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw) +{ + a_inc(&rw->_rw_readers); + if (rw->_rw_wrlock) { + a_dec(&rw->_rw_readers); + if (rw->_rw_waiters && !rw->_rw_readers) + __wake(&rw->_rw_readers, 1, 0); + return EAGAIN; + } + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_trywrlock.c b/05/musl-final/src/thread/pthread_rwlock_trywrlock.c new file mode 100644 index 0000000..1bcf7c9 --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_trywrlock.c @@ -0,0 +1,13 @@ +#include "pthread_impl.h" + +int pthread_rwlock_trywrlock(pthread_rwlock_t *rw) +{ + if (a_xchg(&rw->_rw_wrlock, 1)) + return EAGAIN; + if (rw->_rw_readers) { + a_store(&rw->_rw_wrlock, 0); + return EAGAIN; + } + rw->_rw_owner = pthread_self()->tid; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_unlock.c b/05/musl-final/src/thread/pthread_rwlock_unlock.c new file mode 100644 index 0000000..060e3fe --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_unlock.c @@ -0,0 +1,17 @@ +#include "pthread_impl.h" + +int pthread_rwlock_unlock(pthread_rwlock_t *rw) +{ + struct pthread *self = pthread_self(); + if (rw->_rw_owner == self->tid) { + rw->_rw_owner = 0; + a_store(&rw->_rw_wrlock, 0); + if (rw->_rw_waiters) + __wake(&rw->_rw_wrlock, -1, 0); + return 0; + } + a_dec(&rw->_rw_readers); + if (rw->_rw_waiters && !rw->_rw_readers) + __wake(&rw->_rw_readers, 1, 0); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_rwlock_wrlock.c b/05/musl-final/src/thread/pthread_rwlock_wrlock.c new file mode 100644 index 0000000..8fd9ad1 --- /dev/null +++ b/05/musl-final/src/thread/pthread_rwlock_wrlock.c @@ -0,0 +1,13 @@ +#include "pthread_impl.h" + +int pthread_rwlock_wrlock(pthread_rwlock_t *rw) +{ + int nr; + while (pthread_rwlock_trywrlock(rw)==EAGAIN) { + if ((nr=rw->_rw_readers)) + __wait(&rw->_rw_readers, &rw->_rw_waiters, nr, 0); + else + __wait(&rw->_rw_wrlock, &rw->_rw_waiters, 1, 0); + } + return 0; +} diff --git a/05/musl-final/src/thread/pthread_self.c b/05/musl-final/src/thread/pthread_self.c new file mode 100644 index 0000000..3a4d4c5 --- /dev/null +++ b/05/musl-final/src/thread/pthread_self.c @@ -0,0 +1,33 @@ +#include "pthread_impl.h" + +static struct pthread main_thread; + +#undef errno +static int *errno_location() +{ + return __pthread_self()->errno_ptr; +} + +static int init_main_thread() +{ + main_thread.self = &main_thread; + if (__set_thread_area(&main_thread) < 0) + return -1; + syscall1(__NR_set_tid_address, (long)&main_thread.tid); + main_thread.errno_ptr = __errno_location(); + libc.errno_location = errno_location; + main_thread.tid = main_thread.pid = getpid(); + return 0; +} + +pthread_t pthread_self() +{ + static int init, failed; + if (!init) { + if (failed) return 0; + if (init_main_thread() < 0) failed = 1; + if (failed) return 0; + init = 1; + } + return __pthread_self(); +} diff --git a/05/musl-final/src/thread/pthread_setcancelstate.c b/05/musl-final/src/thread/pthread_setcancelstate.c new file mode 100644 index 0000000..23c3885 --- /dev/null +++ b/05/musl-final/src/thread/pthread_setcancelstate.c @@ -0,0 +1,10 @@ +#include "pthread_impl.h" + +int pthread_setcancelstate(int new, int *old) +{ + struct pthread *self = pthread_self(); + if (old) *old = self->canceldisable; + if ((unsigned)new > 1) return EINVAL; + self->canceldisable = new; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_setcanceltype.c b/05/musl-final/src/thread/pthread_setcanceltype.c new file mode 100644 index 0000000..c73db22 --- /dev/null +++ b/05/musl-final/src/thread/pthread_setcanceltype.c @@ -0,0 +1,10 @@ +#include "pthread_impl.h" + +int pthread_setcanceltype(int new, int *old) +{ + struct pthread *self = pthread_self(); + if (old) *old = self->cancelasync; + if ((unsigned)new > 1) return EINVAL; + self->cancelasync = new; + return 0; +} diff --git a/05/musl-final/src/thread/pthread_setspecific.c b/05/musl-final/src/thread/pthread_setspecific.c new file mode 100644 index 0000000..171cef4 --- /dev/null +++ b/05/musl-final/src/thread/pthread_setspecific.c @@ -0,0 +1,18 @@ +#include "pthread_impl.h" + +int pthread_setspecific(pthread_key_t k, const void *x) +{ + struct pthread *self = pthread_self(); + /* Handle the case of the main thread */ + if (!self->tsd) { + if (!x) return 0; + if (!(self->tsd = calloc(sizeof(void *), PTHREAD_KEYS_MAX))) + return ENOMEM; + } + /* Avoid unnecessary COW */ + if (self->tsd[k] != x) { + self->tsd[k] = (void *)x; + self->tsd_used = 1; + } + return 0; +} diff --git a/05/musl-final/src/thread/pthread_spin_destroy.c b/05/musl-final/src/thread/pthread_spin_destroy.c new file mode 100644 index 0000000..e65a820 --- /dev/null +++ b/05/musl-final/src/thread/pthread_spin_destroy.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_spin_destroy(pthread_spinlock_t *s) +{ + return 0; +} diff --git a/05/musl-final/src/thread/pthread_spin_init.c b/05/musl-final/src/thread/pthread_spin_init.c new file mode 100644 index 0000000..681881c --- /dev/null +++ b/05/musl-final/src/thread/pthread_spin_init.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_spin_init(pthread_spinlock_t *s, int shared) +{ + return *s = 0; +} diff --git a/05/musl-final/src/thread/pthread_spin_lock.c b/05/musl-final/src/thread/pthread_spin_lock.c new file mode 100644 index 0000000..59fa6ea --- /dev/null +++ b/05/musl-final/src/thread/pthread_spin_lock.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +int pthread_spin_lock(pthread_spinlock_t *s) +{ + while (a_xchg(s, 1)); + return 0; +} diff --git a/05/musl-final/src/thread/pthread_spin_trylock.c b/05/musl-final/src/thread/pthread_spin_trylock.c new file mode 100644 index 0000000..c12696b --- /dev/null +++ b/05/musl-final/src/thread/pthread_spin_trylock.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_spin_trylock(pthread_spinlock_t *s) +{ + return -a_xchg(s, 1) & EBUSY; +} diff --git a/05/musl-final/src/thread/pthread_spin_unlock.c b/05/musl-final/src/thread/pthread_spin_unlock.c new file mode 100644 index 0000000..a7eab33 --- /dev/null +++ b/05/musl-final/src/thread/pthread_spin_unlock.c @@ -0,0 +1,6 @@ +#include "pthread_impl.h" + +int pthread_spin_unlock(pthread_spinlock_t *s) +{ + return *s = 0; +} diff --git a/05/musl-final/src/thread/pthread_testcancel.c b/05/musl-final/src/thread/pthread_testcancel.c new file mode 100644 index 0000000..774b706 --- /dev/null +++ b/05/musl-final/src/thread/pthread_testcancel.c @@ -0,0 +1,7 @@ +#include "pthread_impl.h" + +void pthread_testcancel() +{ + CANCELPT_BEGIN; + CANCELPT_END; +} diff --git a/05/musl-final/src/thread/x86_64/__set_thread_area.s b/05/musl-final/src/thread/x86_64/__set_thread_area.s new file mode 100644 index 0000000..faf73ec --- /dev/null +++ b/05/musl-final/src/thread/x86_64/__set_thread_area.s @@ -0,0 +1,11 @@ +/* Copyright 2011 Nicholas J. Kain, licensed GNU LGPL 2.1 or later */ +.text +.global __set_thread_area +.type __set_thread_area,%function +__set_thread_area: + mov %rdi,%rsi /* shift for syscall */ + movl $0x1002,%edi /* SET_FS register */ + movl $158,%eax /* set fs segment to */ + syscall /* arch_prctl(SET_FS, arg)*/ + ret +.size __set_thread_area,.-__set_thread_area diff --git a/05/musl-final/src/thread/x86_64/__unmapself.s b/05/musl-final/src/thread/x86_64/__unmapself.s new file mode 100644 index 0000000..59092ea --- /dev/null +++ b/05/musl-final/src/thread/x86_64/__unmapself.s @@ -0,0 +1,24 @@ +/* Copyright 2011 Nicholas J. Kain, licensed GNU LGPL 2.1 or later */ +.text +.global __unmapself +.type __unmapself,%function +__unmapself: + call 1f /* glibc ABI compat */ + .long -1 + .long -1 +1: push %rsi /* save arg2 for munmap */ + push %rdx /* save arg3 for munmap */ + mov %rdi,%rsi /* rt_sigprocmask() args: move arg1 to rsi */ + xor %rdi,%rdi + xor %rdx,%rdx + movq $8,%r10 + movl $14,%eax /* __NR_rt_sigprocmask */ + syscall /* call rt_sigprocmask(0,arg1,0,8) */ + pop %rsi /* munmap() args: reload from stack */ + pop %rdi + movl $11,%eax /* __NR_munmap */ + syscall /* munmap(arg2,arg3) */ + xor %rdi,%rdi /* exit() args: always return success */ + movl $60,%eax /* __NR_exit */ + syscall /* exit(0) */ +.size __unmapself,.-__unmapself diff --git a/05/musl-final/src/thread/x86_64/clone.s b/05/musl-final/src/thread/x86_64/clone.s new file mode 100644 index 0000000..3cefd93 --- /dev/null +++ b/05/musl-final/src/thread/x86_64/clone.s @@ -0,0 +1,22 @@ +/* Copyright 2011 Nicholas J. Kain, licensed GNU LGPL 2.1 or later */ +.text +.global __uniclone +.type __uniclone,%function +/* rdi = child_stack, rsi = start, rdx = pthread_struct */ +__uniclone: + subq $8,%rsp /* pad parent stack to prevent branch later */ + subq $16,%rdi /* grow child_stack */ + mov %rsi,8(%rdi) /* push start onto child_stack as return ptr */ + mov %rdx,0(%rdi) /* push pthread_struct onto child_stack */ + mov %rdx,%r8 /* r8 = tls */ + mov %rdi,%rsi /* rsi = child_stack */ + leaq 40(%rdx),%r10 /* r10 = child_id */ + movl $56,%eax /* clone syscall number */ + movl $0x7d0f00,%edi /* rdi = flags */ + mov %r10,%rdx /* rdx = parent_id */ + syscall /* clone(flags, child_stack, parent_id, + * child_id, tls) */ + pop %rdi /* child stack: restore pthread_struct + * parent stack: undo rsp displacement */ + ret +.size __uniclone,.-__uniclone diff --git a/05/musl-final/src/time/__asctime.c b/05/musl-final/src/time/__asctime.c new file mode 100644 index 0000000..1853580 --- /dev/null +++ b/05/musl-final/src/time/__asctime.c @@ -0,0 +1,27 @@ +#include +#include +#include + +const char *__langinfo(nl_item); + +char *__asctime(const struct tm *tm, char *buf) +{ + /* FIXME: change __langinfo to __C_langinfo once we have locales */ + if (snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", + __langinfo(ABDAY_1+tm->tm_wday), + __langinfo(ABMON_1+tm->tm_mon), + tm->tm_mday, tm->tm_hour, + tm->tm_min, tm->tm_sec, + 1900 + tm->tm_year) >= 26) + { + /* ISO C requires us to use the above format string, + * even if it will not fit in the buffer. Thus asctime_r + * is _supposed_ to crash if the fields in tm are too large. + * We follow this behavior and crash "gracefully" to warn + * application developers that they may not be so lucky + * on other implementations (e.g. stack smashing..). + */ + *(int*)0 = 0; + } + return buf; +} diff --git a/05/musl-final/src/time/__time.h b/05/musl-final/src/time/__time.h new file mode 100644 index 0000000..967e518 --- /dev/null +++ b/05/musl-final/src/time/__time.h @@ -0,0 +1,9 @@ +time_t __tm_to_time(struct tm *); +struct tm *__time_to_tm(time_t, struct tm *); +void __tzset(void); +struct tm *__dst_adjust(struct tm *tm); + +extern long __timezone; +extern int __daylight; +extern int __dst_offset; +extern char *__tzname[2]; diff --git a/05/musl-final/src/time/__time_to_tm.c b/05/musl-final/src/time/__time_to_tm.c new file mode 100644 index 0000000..a1ebc45 --- /dev/null +++ b/05/musl-final/src/time/__time_to_tm.c @@ -0,0 +1,81 @@ +#include + +/* C defines the rounding for division in a nonsensical way */ +#define Q(a,b) ((a)>0 ? (a)/(b) : -(((b)-(a)-1)/(b))) + +#define DAYS_PER_400Y (365*400 + 97) +#define DAYS_PER_100Y (365*100 + 24) +#define DAYS_PER_4Y (365*4 + 1) + +/* FIXME: use lldiv once it's fixed to compute quot,rem together */ +struct tm *__time_to_tm(time_t t, struct tm *tm) +{ + /* months are march-based */ + static const int days_thru_month[] = {31,61,92,122,153,184,214,245,275,306,337,366}; + long long bigday; + unsigned int day, year4, year100; + int year, year400; + int month; + int leap; + int hour, min, sec; + int wday, mday, yday; + + /* start from 2000-03-01 (multiple of 400 years) */ + t += -946684800 - 86400*(31+29); + + bigday = Q(t, 86400); + sec = t-bigday*86400; + + hour = sec/3600; + sec -= hour*3600; + min = sec/60; + sec -= min*60; + + /* 2000-03-01 was a wednesday */ + wday = (3+bigday)%7; + if (wday < 0) wday += 7; + + t = -946684800LL - 86400*(31+29) + 9000000; + + year400 = Q(bigday, DAYS_PER_400Y); + day = bigday-year400*DAYS_PER_400Y; + + year100 = day/DAYS_PER_100Y; + if (year100 == 4) year100--; + day -= year100*DAYS_PER_100Y; + + year4 = day/DAYS_PER_4Y; + if (year4 == 25) year4--; + day -= year4*DAYS_PER_4Y; + + year = day/365; + if (year == 4) year--; + day -= year*365; + + leap = !year && (year4 || !year100); + yday = day + 31+28 + leap; + if (yday >= 365+leap) yday -= 365+leap; + + year += 4*year4 + 100*year100 + 400*year400 + 2000-1900; + + for (month=0; days_thru_month[month] <= day; month++); + if (month) day -= days_thru_month[month-1]; + month += 2; + if (month >= 12) { + month -= 12; + year++; + } + + mday = day+1; + + tm->tm_sec = sec; + tm->tm_min = min; + tm->tm_hour= hour; + tm->tm_mday= mday; + tm->tm_mon = month; + tm->tm_year= year; + tm->tm_wday= wday; + tm->tm_yday= yday; + + return tm; +} diff --git a/05/musl-final/src/time/__tm_to_time.c b/05/musl-final/src/time/__tm_to_time.c new file mode 100644 index 0000000..3fa15fa --- /dev/null +++ b/05/musl-final/src/time/__tm_to_time.c @@ -0,0 +1,33 @@ +#include + +/* C defines the rounding for division in a nonsensical way */ +#define Q(a,b) ((a)>0 ? (a)/(b) : -(((b)-(a)-1)/(b))) + +time_t __tm_to_time(struct tm *tm) +{ + time_t year = tm->tm_year + -100; + int month = tm->tm_mon; + int day = tm->tm_mday; + int z4, z100, z400; + + /* normalize month */ + if (month >= 12) { + year += month/12; + month %= 12; + } else if (month < 0) { + year += month/12; + month %= 12; + if (month) { + month += 12; + year--; + } + } + z4 = Q(year - (month < 2), 4); + z100 = Q(z4, 25); + z400 = Q(z100, 4); + day += year*365 + z4 - z100 + z400 + + month[(int []){0,31,59,90,120,151,181,212,243,273,304,335}]; + return (long long)day*86400 + + tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec + - -946684800; /* the dawn of time :) */ +} diff --git a/05/musl-final/src/time/asctime.c b/05/musl-final/src/time/asctime.c new file mode 100644 index 0000000..3102eb8 --- /dev/null +++ b/05/musl-final/src/time/asctime.c @@ -0,0 +1,9 @@ +#include + +char *__asctime(const struct tm *, char *); + +char *asctime(const struct tm *tm) +{ + static char buf[26]; + return __asctime(tm, buf); +} diff --git a/05/musl-final/src/time/asctime_r.c b/05/musl-final/src/time/asctime_r.c new file mode 100644 index 0000000..e51b880 --- /dev/null +++ b/05/musl-final/src/time/asctime_r.c @@ -0,0 +1,8 @@ +#include + +char *__asctime(const struct tm *, char *); + +char *asctime_r(const struct tm *tm, char *buf) +{ + return __asctime(tm, buf); +} diff --git a/05/musl-final/src/time/clock.c b/05/musl-final/src/time/clock.c new file mode 100644 index 0000000..2feddb3 --- /dev/null +++ b/05/musl-final/src/time/clock.c @@ -0,0 +1,9 @@ +#include +#include + +/* this function assumes 100 hz linux and corrects for it */ +clock_t clock() +{ + struct tms tms; + return (unsigned long)times(&tms)*10000; +} diff --git a/05/musl-final/src/time/clock_gettime.c b/05/musl-final/src/time/clock_gettime.c new file mode 100644 index 0000000..67a05ba --- /dev/null +++ b/05/musl-final/src/time/clock_gettime.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" + +int clock_gettime(clockid_t clk, struct timespec *ts) +{ + return syscall2(__NR_clock_gettime, clk, (long)ts); +} + +int clock_settime(clockid_t clk, const struct timespec *ts) +{ + return syscall2(__NR_clock_settime, clk, (long)ts); +} diff --git a/05/musl-final/src/time/ctime.c b/05/musl-final/src/time/ctime.c new file mode 100644 index 0000000..185ec55 --- /dev/null +++ b/05/musl-final/src/time/ctime.c @@ -0,0 +1,6 @@ +#include + +char *ctime(const time_t *t) +{ + return asctime(localtime(t)); +} diff --git a/05/musl-final/src/time/ctime_r.c b/05/musl-final/src/time/ctime_r.c new file mode 100644 index 0000000..d2260a1 --- /dev/null +++ b/05/musl-final/src/time/ctime_r.c @@ -0,0 +1,8 @@ +#include + +char *ctime_r(const time_t *t, char *buf) +{ + struct tm tm; + localtime_r(t, &tm); + return asctime_r(&tm, buf); +} diff --git a/05/musl-final/src/time/difftime.c b/05/musl-final/src/time/difftime.c new file mode 100644 index 0000000..80a18cc --- /dev/null +++ b/05/musl-final/src/time/difftime.c @@ -0,0 +1,6 @@ +#include + +double difftime(time_t t1, time_t t0) +{ + return t1-t0; +} diff --git a/05/musl-final/src/time/gettimeofday.c b/05/musl-final/src/time/gettimeofday.c new file mode 100644 index 0000000..2b8a287 --- /dev/null +++ b/05/musl-final/src/time/gettimeofday.c @@ -0,0 +1,9 @@ +#define SYSCALL_RETURN_ERRNO +#include +#include "syscall.h" + +int gettimeofday(struct timeval *tv, void *tz) +{ + syscall2(__NR_gettimeofday, (long)tv, 0); + return 0; +} diff --git a/05/musl-final/src/time/gmtime.c b/05/musl-final/src/time/gmtime.c new file mode 100644 index 0000000..d4d5d1f --- /dev/null +++ b/05/musl-final/src/time/gmtime.c @@ -0,0 +1,11 @@ +#include + +#include "__time.h" + +struct tm *gmtime(const time_t *t) +{ + static struct tm tm; + __time_to_tm(*t, &tm); + tm.tm_isdst = 0; + return &tm; +} diff --git a/05/musl-final/src/time/gmtime_r.c b/05/musl-final/src/time/gmtime_r.c new file mode 100644 index 0000000..5b565a6 --- /dev/null +++ b/05/musl-final/src/time/gmtime_r.c @@ -0,0 +1,10 @@ +#include + +#include "__time.h" + +struct tm *gmtime_r(const time_t *t, struct tm *result) +{ + __time_to_tm(*t, result); + result->tm_isdst = 0; + return result; +} diff --git a/05/musl-final/src/time/localtime.c b/05/musl-final/src/time/localtime.c new file mode 100644 index 0000000..abd5e84 --- /dev/null +++ b/05/musl-final/src/time/localtime.c @@ -0,0 +1,12 @@ +#include + +#include "__time.h" + +struct tm *localtime(const time_t *t) +{ + static struct tm tm; + __tzset(); + __time_to_tm(*t - __timezone, &tm); + tm.tm_isdst = -1; + return __dst_adjust(&tm); +} diff --git a/05/musl-final/src/time/localtime_r.c b/05/musl-final/src/time/localtime_r.c new file mode 100644 index 0000000..2bf1037 --- /dev/null +++ b/05/musl-final/src/time/localtime_r.c @@ -0,0 +1,11 @@ +#include + +#include "__time.h" + +struct tm *localtime_r(const time_t *t, struct tm *result) +{ + __tzset(); + __time_to_tm(*t - __timezone, result); + result->tm_isdst = -1; + return __dst_adjust(result); +} diff --git a/05/musl-final/src/time/mktime.c b/05/musl-final/src/time/mktime.c new file mode 100644 index 0000000..858cd50 --- /dev/null +++ b/05/musl-final/src/time/mktime.c @@ -0,0 +1,24 @@ +#include + +#include "__time.h" + +time_t mktime(struct tm *tm) +{ + int isdst = tm->tm_isdst; + time_t t, lt; + + __tzset(); + + tm->tm_sec += __timezone; + if (isdst > 0) tm->tm_sec += __dst_offset; + + t = __tm_to_time(tm); + + lt = t - __timezone; + if (isdst > 0) lt -= __dst_offset; + __time_to_tm(lt, tm); + + __dst_adjust(tm); + + return t; +} diff --git a/05/musl-final/src/time/nanosleep.c b/05/musl-final/src/time/nanosleep.c new file mode 100644 index 0000000..5ac4c35 --- /dev/null +++ b/05/musl-final/src/time/nanosleep.c @@ -0,0 +1,13 @@ +#include +#include +#include "syscall.h" +#include "libc.h" + +int nanosleep(const struct timespec *req, struct timespec *rem) +{ + int ret; + CANCELPT_BEGIN; + ret = syscall2(__NR_nanosleep, (long)req, (long)rem); + CANCELPT_END; + return ret; +} diff --git a/05/musl-final/src/time/strftime.c b/05/musl-final/src/time/strftime.c new file mode 100644 index 0000000..f1b9463 --- /dev/null +++ b/05/musl-final/src/time/strftime.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include "__time.h" + +// FIXME: integer overflows + +const char *__langinfo(nl_item); + +size_t strftime(char *s, size_t n, const char *f, const struct tm *tm) +{ + nl_item item; + int val; + const char *fmt; + size_t l; + for (l=0; *f && ltm_wday; + goto nl_strcat; + case 'A': + item = DAY_1 + tm->tm_wday; + goto nl_strcat; + case 'h': + case 'b': + item = ABMON_1 + tm->tm_mon; + goto nl_strcat; + case 'B': + item = MON_1 + tm->tm_mon; + goto nl_strcat; + case 'c': + item = D_T_FMT; + goto nl_strftime; + case 'C': + val = (1900+tm->tm_year) / 100; + fmt = "%02d"; + goto number; + case 'd': + val = tm->tm_mday; + fmt = "%02d"; + goto number; + case 'D': + fmt = "%m/%d/%y"; + goto recu_strftime; + case 'e': + val = tm->tm_mday; + fmt = "%2d"; + goto number; + case 'F': + fmt = "%Y-%m-%d"; + goto recu_strftime; + case 'g': + // FIXME + val = 0; //week_based_year(tm)%100; + fmt = "%02d"; + goto number; + case 'G': + // FIXME + val = 0; //week_based_year(tm); + fmt = "%04d"; + goto number; + case 'H': + val = tm->tm_hour; + fmt = "%02d"; + goto number; + case 'I': + val = tm->tm_hour; + if (!val) val = 12; + else if (val > 12) val -= 12; + fmt = "%02d"; + goto number; + case 'j': + val = tm->tm_yday+1; + fmt = "%03d"; + goto number; + case 'm': + val = tm->tm_mon+1; + fmt = "%02d"; + goto number; + case 'M': + val = tm->tm_min; + fmt = "%02d"; + goto number; + case 'n': + s[l++] = '\n'; + continue; + case 'p': + item = tm->tm_hour >= 12 ? PM_STR : AM_STR; + goto nl_strcat; + case 'r': + item = T_FMT_AMPM; + goto nl_strftime; + case 'R': + fmt = "%H:%M"; + goto recu_strftime; + case 'S': + val = tm->tm_sec; + fmt = "%02d"; + goto number; + case 't': + s[l++] = '\t'; + continue; + case 'T': + fmt = "%H:%M:%S"; + goto recu_strftime; + case 'u': + val = tm->tm_wday ? tm->tm_wday : 7; + fmt = "%d"; + goto number; + case 'U': + case 'V': + case 'W': + // FIXME: week number mess.. + continue; + case 'w': + val = tm->tm_wday; + fmt = "%d"; + goto number; + case 'x': + item = D_FMT; + goto nl_strftime; + case 'X': + item = T_FMT; + goto nl_strftime; + case 'y': + val = tm->tm_year % 100; + fmt = "%02d"; + goto number; + case 'Y': + val = tm->tm_year + 1900; + fmt = "%04d"; + goto number; + case 'z': + if (tm->tm_isdst < 0) continue; + val = -__timezone - (tm->tm_isdst ? __dst_offset : 0); + l += snprintf(s+l, n-l, "%+.2d%.2d", val/3600, abs(val%3600)/60); + continue; + case 'Z': + if (tm->tm_isdst < 0 || !__tzname[0] || !__tzname[0][0]) + continue; + l += snprintf(s+l, n-l, "%s", __tzname[!!tm->tm_isdst]); + continue; + default: + return 0; + } + } +literal: + s[l++] = *f; + continue; +number: + l += snprintf(s+l, n-l, fmt, val); + continue; +nl_strcat: + l += snprintf(s+l, n-l, "%s", __langinfo(item)); + continue; +nl_strftime: + fmt = __langinfo(item); +recu_strftime: + l += strftime(s+l, n-l, fmt, tm); + } + if (l >= n) return 0; + s[l] = 0; + return l; +} diff --git a/05/musl-final/src/time/strptime.c b/05/musl-final/src/time/strptime.c new file mode 100644 index 0000000..db72e61 --- /dev/null +++ b/05/musl-final/src/time/strptime.c @@ -0,0 +1,178 @@ +#include +#include +#include +#include + +const char *__langinfo(nl_item); + +char *strptime(const char *s, const char *f, struct tm *tm) +{ + return NULL; +} + +#if 0 + +char *strptime(const char *s, const char *f, struct tm *tm) +{ + nl_item item; + int *dest; + const char *fmt; + for (; *f; f++) { + if (isspace(*f)) goto whitespace; + if (*f == '%') { +do_fmt: + switch (*++f) { + case '%': + goto literal; + case 'E': + case 'O': + goto do_fmt; + case 'a': + item = ABDAY_1 + tm->tm_wday; + goto nl_strcat; + case 'A': + item = DAY_1 + tm->tm_wday; + goto nl_strcat; + case 'h': + case 'b': + item = ABMON_1 + tm->tm_mon; + goto nl_strcat; + case 'B': + item = MON_1 + tm->tm_mon; + goto nl_strcat; + case 'c': + item = D_T_FMT; + goto nl_strftime; + case 'C': + val = (1900+tm->tm_year) / 100; + fmt = "%02d"; + goto number; + case 'd': + val = tm->tm_mday; + fmt = "%02d"; + goto number; + case 'D': + fmt = "%m/%d/%y"; + goto recu_strftime; + case 'e': + val = tm->tm_mday; + fmt = "%2d"; + goto number; + case 'F': + fmt = "%Y-%m-%d"; + goto recu_strftime; + case 'g': + // FIXME + val = 0; //week_based_year(tm)%100; + fmt = "%02d"; + goto number; + case 'G': + // FIXME + val = 0; //week_based_year(tm); + fmt = "%04d"; + goto number; + case 'H': + val = tm->tm_hour; + fmt = "%02d"; + goto number; + case 'I': + val = tm->tm_hour; + if (!val) val = 12; + else if (val > 12) val -= 12; + fmt = "%02d"; + goto number; + case 'j': + val = tm->tm_yday+1; + fmt = "%03d"; + goto number; + case 'm': + val = tm->tm_mon+1; + fmt = "%02d"; + goto number; + case 'M': + val = tm->tm_min; + fmt = "%02d"; + goto number; + case 'n': + case 't': + goto whitespace; + case 'p': + item = tm->tm_hour >= 12 ? PM_STR : AM_STR; + goto nl_strcat; + case 'r': + item = T_FMT_AMPM; + goto nl_strftime; + case 'R': + fmt = "%H:%M"; + goto recu_strftime; + case 'S': + val = tm->tm_sec; + fmt = "%02d"; + goto number; + case 'T': + fmt = "%H:%M:%S"; + goto recu_strftime; + case 'u': + val = tm->tm_wday ? tm->tm_wday : 7; + fmt = "%d"; + goto number; + case 'U': + case 'V': + case 'W': + // FIXME: week number mess.. + continue; + case 'w': + val = tm->tm_wday; + fmt = "%d"; + goto number; + case 'x': + item = D_FMT; + goto nl_strftime; + case 'X': + item = T_FMT; + goto nl_strftime; + case 'y': + val = tm->tm_year % 100; + fmt = "%02d"; + goto number; + case 'Y': + val = tm->tm_year + 1900; + fmt = "%04d"; + goto number; + case 'z': + if (tm->tm_isdst < 0) continue; + val = timezone + (tm->tm_isdst) ? __dst_offset : 0; + l += snprintf(s+l, n-l, "%+02d%02d", val/60, abs(val%60)); + continue; + case 'Z': + if (tm->tm_isdst < 0 || !tzname[0] || !tzname[0][0]) + continue; + l += snprintf(s+l, n-l, "%s", tzname[!!tm->tm_isdst]); + continue; + } + default: + return NULL; + } +literal: + if (*s++ != *f) return NULL; + continue; +whitespace: + while(isspace(*s)) s++; + continue; +number: + l += snprintf(s+l, n-l, fmt, val); + continue; +nl_strcat: + l += snprintf(s+l, n-l, "%s", __langinfo(item)); + continue; +nl_strftime: + fmt = __langinfo(item); +recu_strftime: + l += strftime(s+l, n-l, fmt, tm); + } + if (l >= n) return 0; + s[l] = 0; + return l; +} + +#endif diff --git a/05/musl-final/src/time/time.c b/05/musl-final/src/time/time.c new file mode 100644 index 0000000..3457dad --- /dev/null +++ b/05/musl-final/src/time/time.c @@ -0,0 +1,12 @@ +#define SYSCALL_RETURN_ERRNO +#include +#include +#include "syscall.h" + +time_t time(time_t *t) +{ + struct timeval tv; + syscall2(__NR_gettimeofday, (long)&tv, 0); + if (t) *t = tv.tv_sec; + return tv.tv_sec; +} diff --git a/05/musl-final/src/time/times.c b/05/musl-final/src/time/times.c new file mode 100644 index 0000000..e9b5a82 --- /dev/null +++ b/05/musl-final/src/time/times.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +clock_t times(struct tms *tms) +{ + return syscall1(__NR_times, (long)&tms); +} diff --git a/05/musl-final/src/time/timezone.s b/05/musl-final/src/time/timezone.s new file mode 100644 index 0000000..4e167b0 --- /dev/null +++ b/05/musl-final/src/time/timezone.s @@ -0,0 +1,27 @@ +.data + +.global timezone +.global __timezone +.global daylight +.global __daylight +.global tzname +.global __tzname + +__timezone: +timezone: + .long 0 +.size timezone,.-timezone +.size __timezone,.-__timezone + +__daylight: +daylight: + .long 0 +.size daylight,.-daylight +.size __daylight,.-__daylight + +__tzname: +tzname: + .long 0 + .long 0 +.size tzname,.-tzname +.size __tzname,.-__tzname diff --git a/05/musl-final/src/time/tzset.c b/05/musl-final/src/time/tzset.c new file mode 100644 index 0000000..6d69957 --- /dev/null +++ b/05/musl-final/src/time/tzset.c @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include +#include "libc.h" + +#include "__time.h" + +long __timezone = 0; +int __daylight = 0; +char *__tzname[2] = { 0, 0 }; +int __dst_offset = 0; + +weak_alias(__timezone, timezone); +weak_alias(__daylight, daylight); +weak_alias(__tzname, tzname); +weak_alias(__dst_offset, dst_offset); + +static char std_name[TZNAME_MAX+1]; +static char dst_name[TZNAME_MAX+1]; + +/* all elements are zero-based */ +static struct rule { + char month; + char week; + short day; + int time; +} __dst_start, __dst_end; + +static void zname(char *d, char **s) +{ + int i; + for (i=0; i= 0) { + if (**s == ':') ofs += strtol(*s+1, s, 10)*60; + if (**s == ':') ofs += strtol(*s+1, s, 10); + } else { + if (**s == ':') ofs -= strtol(*s+1, s, 10)*60; + if (**s == ':') ofs -= strtol(*s+1, s, 10); + } + return ofs; +} + +static int dstrule(struct rule *rule, char **s) +{ + if (**s != ',') return -1; + switch (*++*s) { + case 'J': + rule->month = 'J'; + rule->day = strtol(*s+1, s, 10)-1; + break; + case 'M': + rule->month = strtol(*s+1, s, 10)-1; + if (**s != '.' || rule->month < 0 || rule->month > 11) + return -1; + rule->week = strtol(*s+1, s, 10)-1; + if (**s != '.' || rule->week < 0 || rule->week > 4) + return -1; + rule->day = strtol(*s+1, s, 10); + if (rule->day < 0 || rule->day > 6) + return -1; + break; + default: + rule->month = 'L'; + rule->day = strtol(*s+1, s, 10); + break; + } + if (**s == '/') { + (*s)++; + rule->time = hhmmss(s); + } else rule->time = 7200; + return 0; +} + +void tzset(void) +{ + char *z, *a; + + strcpy(std_name, "GMT"); + strcpy(dst_name, "GMT"); + __tzname[0] = std_name; + __tzname[1] = dst_name; + __timezone = 0; + __daylight = 0; + + if (!(z = getenv("TZ")) || !isalpha(*z)) return; + + zname(std_name, &z); + __timezone = hhmmss(&z); + + zname(dst_name, &z); + if (dst_name[0]) __daylight=1; + a = z; + __dst_offset = hhmmss(&z) - __timezone; + if (z==a) __dst_offset = -3600; + + if (dstrule(&__dst_start, &z) || dstrule(&__dst_end, &z)) + __daylight = 0; +} + +void __tzset(void) +{ + static int lock, init; + if (init) return; + LOCK(&lock); + if (!init) tzset(); + init=1; + UNLOCK(&lock); +} + +static int is_leap(int year) +{ + year -= 100; + return !(year&3) && ((year%100) || !(year%400)); +} + +static int cutoff_yday(struct tm *tm, struct rule *rule) +{ + static const char days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31}; + static const int first_day[] = {0,31,59,90,120,151,181,212,243,273,304,335}; + int yday, mday, leap; + + switch (rule->month) { + case 'J': + return rule->day + (tm->tm_mon > 1 && is_leap(tm->tm_year)); + case 'L': + return rule->day; + default: + yday = first_day[rule->month]; + leap = is_leap(tm->tm_year); + if (rule->month > 1 && leap) yday++; + mday = (rule->day - (yday + tm->tm_wday - tm->tm_yday) + 1400)%7 + 7*rule->week; + if (mday >= days_in_month[rule->month] + (leap && rule->month == 1)) + mday -= 7; + return mday + yday; + } +} + +struct tm *__dst_adjust(struct tm *tm) +{ + time_t t; + int start, end, secs; + int after_start, before_end; + + if (tm->tm_isdst >= 0) return tm; + if (!__daylight) { + tm->tm_isdst = 0; + return tm; + } + + secs = tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec; + start = cutoff_yday(tm, &__dst_start); + end = cutoff_yday(tm, &__dst_end); + + after_start = (tm->tm_yday > start || (tm->tm_yday == start && secs >= __dst_start.time)); + before_end = (tm->tm_yday < end || (tm->tm_yday == end && secs < __dst_end.time)); + + if ((after_start && before_end) || ((end < start) && (after_start || before_end))) { + tm->tm_sec -= __dst_offset; + tm->tm_isdst = 1; + t = __tm_to_time(tm); + return __time_to_tm(t, tm); + } else tm->tm_isdst = 0; + + return tm; +} diff --git a/05/musl-final/src/time/utime.c b/05/musl-final/src/time/utime.c new file mode 100644 index 0000000..56e9e13 --- /dev/null +++ b/05/musl-final/src/time/utime.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" + +int utime(const char *path, const struct utimbuf *times) +{ + long ktimes[2]; + if (times) { + ktimes[0] = times->actime; + ktimes[1] = times->modtime; + } + return syscall2(__NR_utime, (long)path, times ? (long)ktimes : 0); +} diff --git a/05/musl-final/src/unistd/_exit.c b/05/musl-final/src/unistd/_exit.c new file mode 100644 index 0000000..d2e84c4 --- /dev/null +++ b/05/musl-final/src/unistd/_exit.c @@ -0,0 +1,7 @@ +#include +#include + +void _exit(int status) +{ + _Exit(status); +} diff --git a/05/musl-final/src/unistd/access.c b/05/musl-final/src/unistd/access.c new file mode 100644 index 0000000..2c10e58 --- /dev/null +++ b/05/musl-final/src/unistd/access.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int access(const char *filename, int amode) +{ + return syscall2(__NR_access, (long)filename, amode); +} diff --git a/05/musl-final/src/unistd/alarm.c b/05/musl-final/src/unistd/alarm.c new file mode 100644 index 0000000..bba444d --- /dev/null +++ b/05/musl-final/src/unistd/alarm.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +unsigned alarm(unsigned seconds) +{ + return syscall1(__NR_alarm, seconds); +} diff --git a/05/musl-final/src/unistd/chdir.c b/05/musl-final/src/unistd/chdir.c new file mode 100644 index 0000000..c89bda3 --- /dev/null +++ b/05/musl-final/src/unistd/chdir.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int chdir(const char *path) +{ + return syscall1(__NR_chdir, (long)path); +} diff --git a/05/musl-final/src/unistd/chown.c b/05/musl-final/src/unistd/chown.c new file mode 100644 index 0000000..b89b173 --- /dev/null +++ b/05/musl-final/src/unistd/chown.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int chown(const char *path, uid_t uid, gid_t gid) +{ + return syscall3(__NR_chown, (long)path, uid, gid); +} diff --git a/05/musl-final/src/unistd/close.c b/05/musl-final/src/unistd/close.c new file mode 100644 index 0000000..97302f6 --- /dev/null +++ b/05/musl-final/src/unistd/close.c @@ -0,0 +1,11 @@ +#include +#include "syscall.h" +#include "libc.h" + +int close(int fd) +{ + int ret = __syscall_close(fd); + CANCELPT_BEGIN; + CANCELPT_END; + return ret; +} diff --git a/05/musl-final/src/unistd/ctermid.c b/05/musl-final/src/unistd/ctermid.c new file mode 100644 index 0000000..21b44ec --- /dev/null +++ b/05/musl-final/src/unistd/ctermid.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include + +char *ctermid(char *s) +{ + static char *s2; + int fd; + if (!s) { + if (!s2) s2 = malloc(L_ctermid); + s = s2; + } + fd = open("/dev/tty", O_WRONLY | O_NOCTTY); + if (fd < 0) + return strcpy(s, ""); + if (ttyname_r(fd, s, L_ctermid)) + strcpy(s, ""); + close(fd); + return s; +} diff --git a/05/musl-final/src/unistd/dup.c b/05/musl-final/src/unistd/dup.c new file mode 100644 index 0000000..b11cd71 --- /dev/null +++ b/05/musl-final/src/unistd/dup.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int dup(int fd) +{ + return syscall1(__NR_dup, fd); +} diff --git a/05/musl-final/src/unistd/dup2.c b/05/musl-final/src/unistd/dup2.c new file mode 100644 index 0000000..9332544 --- /dev/null +++ b/05/musl-final/src/unistd/dup2.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int dup2(int old, int new) +{ + return __syscall_dup2(old, new); +} diff --git a/05/musl-final/src/unistd/faccessat.c b/05/musl-final/src/unistd/faccessat.c new file mode 100644 index 0000000..99a9378 --- /dev/null +++ b/05/musl-final/src/unistd/faccessat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int faccessat(int fd, const char *filename, int amode, int flag) +{ + return syscall4(__NR_faccessat, fd, (long)filename, amode, flag); +} diff --git a/05/musl-final/src/unistd/fchdir.c b/05/musl-final/src/unistd/fchdir.c new file mode 100644 index 0000000..b2acbc2 --- /dev/null +++ b/05/musl-final/src/unistd/fchdir.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int fchdir(int fd) +{ + return syscall1(__NR_fchdir, fd); +} diff --git a/05/musl-final/src/unistd/fchown.c b/05/musl-final/src/unistd/fchown.c new file mode 100644 index 0000000..6050b77 --- /dev/null +++ b/05/musl-final/src/unistd/fchown.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int fchown(int fd, uid_t uid, gid_t gid) +{ + return syscall3(__NR_fchown, fd, uid, gid); +} diff --git a/05/musl-final/src/unistd/fchownat.c b/05/musl-final/src/unistd/fchownat.c new file mode 100644 index 0000000..7062642 --- /dev/null +++ b/05/musl-final/src/unistd/fchownat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int fchownat(int fd, const char *path, uid_t uid, gid_t gid, int flag) +{ + return syscall5(__NR_fchownat, fd, (long)path, uid, gid, flag); +} diff --git a/05/musl-final/src/unistd/fdatasync.c b/05/musl-final/src/unistd/fdatasync.c new file mode 100644 index 0000000..ef7c9a9 --- /dev/null +++ b/05/musl-final/src/unistd/fdatasync.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int fdatasync(int fd) +{ + return 0; +} diff --git a/05/musl-final/src/unistd/fsync.c b/05/musl-final/src/unistd/fsync.c new file mode 100644 index 0000000..7cfedc9 --- /dev/null +++ b/05/musl-final/src/unistd/fsync.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" + +int fsync(int fd) +{ + //return syscall1(__NR_fsync, fd); + return 0; +} diff --git a/05/musl-final/src/unistd/ftruncate.c b/05/musl-final/src/unistd/ftruncate.c new file mode 100644 index 0000000..db5da84 --- /dev/null +++ b/05/musl-final/src/unistd/ftruncate.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int ftruncate(int fd, off_t length) +{ + return syscall3(__NR_ftruncate, fd, SYSCALL_LL(length)); +} + +LFS64(ftruncate); diff --git a/05/musl-final/src/unistd/getcwd.c b/05/musl-final/src/unistd/getcwd.c new file mode 100644 index 0000000..4910f42 --- /dev/null +++ b/05/musl-final/src/unistd/getcwd.c @@ -0,0 +1,8 @@ +#include +#include +#include "syscall.h" + +char *getcwd(char *buf, size_t size) +{ + return syscall2(__NR_getcwd, (long)buf, size) < 0 ? NULL : buf; +} diff --git a/05/musl-final/src/unistd/getegid.c b/05/musl-final/src/unistd/getegid.c new file mode 100644 index 0000000..33ee205 --- /dev/null +++ b/05/musl-final/src/unistd/getegid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +gid_t getegid(void) +{ + return syscall0(__NR_getegid); +} diff --git a/05/musl-final/src/unistd/geteuid.c b/05/musl-final/src/unistd/geteuid.c new file mode 100644 index 0000000..cdec631 --- /dev/null +++ b/05/musl-final/src/unistd/geteuid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +uid_t geteuid(void) +{ + return syscall0(__NR_geteuid); +} diff --git a/05/musl-final/src/unistd/getgid.c b/05/musl-final/src/unistd/getgid.c new file mode 100644 index 0000000..8a4590b --- /dev/null +++ b/05/musl-final/src/unistd/getgid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +gid_t getgid(void) +{ + return syscall0(__NR_getgid); +} diff --git a/05/musl-final/src/unistd/getgroups.c b/05/musl-final/src/unistd/getgroups.c new file mode 100644 index 0000000..37619b9 --- /dev/null +++ b/05/musl-final/src/unistd/getgroups.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int getgroups(int count, gid_t list[]) +{ + return syscall2(__NR_getgroups, count, (long)list); +} diff --git a/05/musl-final/src/unistd/gethostname.c b/05/musl-final/src/unistd/gethostname.c new file mode 100644 index 0000000..a406c4e --- /dev/null +++ b/05/musl-final/src/unistd/gethostname.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int gethostname(char *name, size_t len) +{ + size_t i; + struct utsname uts; + if (uname(&uts)) return -1; + if (len > sizeof uts.nodename) len = sizeof uts.nodename; + for (i=0; i +#include + +char *getlogin(void) +{ + return getenv("LOGNAME"); +} diff --git a/05/musl-final/src/unistd/getlogin_r.c b/05/musl-final/src/unistd/getlogin_r.c new file mode 100644 index 0000000..f04f71e --- /dev/null +++ b/05/musl-final/src/unistd/getlogin_r.c @@ -0,0 +1,13 @@ +#include +#include +#include +#include + +int getlogin_r(char *name, size_t size) +{ + char *logname = getlogin(); + if (!logname) return ENXIO; /* or...? */ + if (strlen(name) >= size) return ERANGE; + strcpy(name, logname); + return 0; +} diff --git a/05/musl-final/src/unistd/getpgid.c b/05/musl-final/src/unistd/getpgid.c new file mode 100644 index 0000000..50d716b --- /dev/null +++ b/05/musl-final/src/unistd/getpgid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t getpgid(pid_t pid) +{ + return syscall1(__NR_getpgid, pid); +} diff --git a/05/musl-final/src/unistd/getpgrp.c b/05/musl-final/src/unistd/getpgrp.c new file mode 100644 index 0000000..2004630 --- /dev/null +++ b/05/musl-final/src/unistd/getpgrp.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t getpgrp(void) +{ + return syscall0(__NR_getpgrp); +} diff --git a/05/musl-final/src/unistd/getpid.c b/05/musl-final/src/unistd/getpid.c new file mode 100644 index 0000000..31cbe1c --- /dev/null +++ b/05/musl-final/src/unistd/getpid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t getpid(void) +{ + return syscall0(__NR_getpid); +} diff --git a/05/musl-final/src/unistd/getppid.c b/05/musl-final/src/unistd/getppid.c new file mode 100644 index 0000000..a324182 --- /dev/null +++ b/05/musl-final/src/unistd/getppid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t getppid(void) +{ + return syscall0(__NR_getppid); +} diff --git a/05/musl-final/src/unistd/getsid.c b/05/musl-final/src/unistd/getsid.c new file mode 100644 index 0000000..064229c --- /dev/null +++ b/05/musl-final/src/unistd/getsid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t getsid(pid_t pid) +{ + return syscall1(__NR_getsid, pid); +} diff --git a/05/musl-final/src/unistd/getuid.c b/05/musl-final/src/unistd/getuid.c new file mode 100644 index 0000000..cd7233d --- /dev/null +++ b/05/musl-final/src/unistd/getuid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +uid_t getuid(void) +{ + return syscall0(__NR_getuid); +} diff --git a/05/musl-final/src/unistd/isatty.c b/05/musl-final/src/unistd/isatty.c new file mode 100644 index 0000000..cff6e9f --- /dev/null +++ b/05/musl-final/src/unistd/isatty.c @@ -0,0 +1,8 @@ +#include +#include + +int isatty(int fd) +{ + struct termios t; + return tcgetattr(fd, &t) == 0; +} diff --git a/05/musl-final/src/unistd/lchown.c b/05/musl-final/src/unistd/lchown.c new file mode 100644 index 0000000..a840213 --- /dev/null +++ b/05/musl-final/src/unistd/lchown.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int lchown(const char *path, uid_t uid, gid_t gid) +{ + return syscall3(__NR_lchown, (long)path, uid, gid); +} diff --git a/05/musl-final/src/unistd/link.c b/05/musl-final/src/unistd/link.c new file mode 100644 index 0000000..f121bb9 --- /dev/null +++ b/05/musl-final/src/unistd/link.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int link(const char *existing, const char *new) +{ + return syscall2(__NR_link, (long)existing, (long)new); +} diff --git a/05/musl-final/src/unistd/linkat.c b/05/musl-final/src/unistd/linkat.c new file mode 100644 index 0000000..0eb5122 --- /dev/null +++ b/05/musl-final/src/unistd/linkat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int linkat(int fd1, const char *existing, int fd2, const char *new, int flag) +{ + return syscall5(__NR_linkat, fd1, (long)existing, fd2, (long)new, flag); +} diff --git a/05/musl-final/src/unistd/lseek.c b/05/musl-final/src/unistd/lseek.c new file mode 100644 index 0000000..0152866 --- /dev/null +++ b/05/musl-final/src/unistd/lseek.c @@ -0,0 +1,15 @@ +#include +#include "syscall.h" +#include "libc.h" + +off_t lseek(int fd, off_t offset, int whence) +{ +#ifdef __NR__llseek + off_t result; + return syscall5(__NR__llseek, fd, offset>>32, offset, (long)&result, whence) ? -1 : result; +#else + return syscall3(__NR_lseek, fd, offset, whence); +#endif +} + +LFS64(lseek); diff --git a/05/musl-final/src/unistd/nice.c b/05/musl-final/src/unistd/nice.c new file mode 100644 index 0000000..f38db67 --- /dev/null +++ b/05/musl-final/src/unistd/nice.c @@ -0,0 +1,12 @@ +#include +#include +#include "syscall.h" + +int nice(int inc) +{ +#ifdef __NR_nice + return syscall1(__NR_nice, inc); +#else + return setpriority(PRIO_PROCESS, 0, getpriority(PRIO_PROCESS, 0)+inc); +#endif +} diff --git a/05/musl-final/src/unistd/pause.c b/05/musl-final/src/unistd/pause.c new file mode 100644 index 0000000..1472065 --- /dev/null +++ b/05/musl-final/src/unistd/pause.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +int pause(void) +{ + int r; + CANCELPT_BEGIN; + r = syscall0(__NR_pause); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/unistd/pipe.c b/05/musl-final/src/unistd/pipe.c new file mode 100644 index 0000000..2dfc9c9 --- /dev/null +++ b/05/musl-final/src/unistd/pipe.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int pipe(int fd[2]) +{ + return syscall1(__NR_pipe, (long)fd); +} diff --git a/05/musl-final/src/unistd/pread.c b/05/musl-final/src/unistd/pread.c new file mode 100644 index 0000000..534215a --- /dev/null +++ b/05/musl-final/src/unistd/pread.c @@ -0,0 +1,14 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t pread(int fd, void *buf, size_t size, off_t ofs) +{ + ssize_t r; + CANCELPT_BEGIN; + r = syscall5(__NR_pread, fd, (long)buf, size, SYSCALL_LL(ofs)); + CANCELPT_END; + return r; +} + +LFS64(pread); diff --git a/05/musl-final/src/unistd/pwrite.c b/05/musl-final/src/unistd/pwrite.c new file mode 100644 index 0000000..56ed6b4 --- /dev/null +++ b/05/musl-final/src/unistd/pwrite.c @@ -0,0 +1,14 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t pwrite(int fd, const void *buf, size_t size, off_t ofs) +{ + ssize_t r; + CANCELPT_BEGIN; + r = syscall5(__NR_pwrite, fd, (long)buf, size, SYSCALL_LL(ofs)); + CANCELPT_END; + return r; +} + +LFS64(pwrite); diff --git a/05/musl-final/src/unistd/read.c b/05/musl-final/src/unistd/read.c new file mode 100644 index 0000000..87ff1f1 --- /dev/null +++ b/05/musl-final/src/unistd/read.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t read(int fd, void *buf, size_t count) +{ + ssize_t r; + CANCELPT_BEGIN; + r = __syscall_read(fd, buf, count); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/unistd/readlink.c b/05/musl-final/src/unistd/readlink.c new file mode 100644 index 0000000..f6b1635 --- /dev/null +++ b/05/musl-final/src/unistd/readlink.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int readlink(const char *path, char *buf, size_t bufsize) +{ + return syscall3(__NR_readlink, (long)path, (long)buf, bufsize); +} diff --git a/05/musl-final/src/unistd/readlinkat.c b/05/musl-final/src/unistd/readlinkat.c new file mode 100644 index 0000000..8171050 --- /dev/null +++ b/05/musl-final/src/unistd/readlinkat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int readlinkat(int fd, const char *path, char *buf, size_t bufsize) +{ + return syscall4(__NR_readlinkat, fd, (long)path, (long)buf, bufsize); +} diff --git a/05/musl-final/src/unistd/readv.c b/05/musl-final/src/unistd/readv.c new file mode 100644 index 0000000..e311f9d --- /dev/null +++ b/05/musl-final/src/unistd/readv.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t readv(int fd, const struct iovec *iov, int count) +{ + ssize_t r; + CANCELPT_BEGIN; + r = syscall3(__NR_readv, fd, (long)iov, count); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/unistd/renameat.c b/05/musl-final/src/unistd/renameat.c new file mode 100644 index 0000000..0dae9f1 --- /dev/null +++ b/05/musl-final/src/unistd/renameat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int renameat(int oldfd, const char *old, int newfd, const char *new) +{ + return syscall4(__NR_renameat, oldfd, (long)old, newfd, (long)new); +} diff --git a/05/musl-final/src/unistd/rmdir.c b/05/musl-final/src/unistd/rmdir.c new file mode 100644 index 0000000..8e18c7a --- /dev/null +++ b/05/musl-final/src/unistd/rmdir.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int rmdir(const char *path) +{ + return syscall1(__NR_rmdir, (long)path); +} diff --git a/05/musl-final/src/unistd/setegid.c b/05/musl-final/src/unistd/setegid.c new file mode 100644 index 0000000..8534884 --- /dev/null +++ b/05/musl-final/src/unistd/setegid.c @@ -0,0 +1,6 @@ +#include + +int setegid(gid_t egid) +{ + return setregid(-1, egid); +} diff --git a/05/musl-final/src/unistd/seteuid.c b/05/musl-final/src/unistd/seteuid.c new file mode 100644 index 0000000..0aaa86e --- /dev/null +++ b/05/musl-final/src/unistd/seteuid.c @@ -0,0 +1,6 @@ +#include + +int seteuid(uid_t euid) +{ + return setreuid(-1, euid); +} diff --git a/05/musl-final/src/unistd/setgid.c b/05/musl-final/src/unistd/setgid.c new file mode 100644 index 0000000..42976d9 --- /dev/null +++ b/05/musl-final/src/unistd/setgid.c @@ -0,0 +1,9 @@ +#include +#include "syscall.h" +#include "libc.h" + +int setgid(gid_t gid) +{ + if (libc.rsyscall) return libc.rsyscall(__NR_setgid, gid, 0, 0, 0, 0, 0); + return syscall1(__NR_setgid, gid); +} diff --git a/05/musl-final/src/unistd/setpgid.c b/05/musl-final/src/unistd/setpgid.c new file mode 100644 index 0000000..748d290 --- /dev/null +++ b/05/musl-final/src/unistd/setpgid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t setpgid(pid_t pid, pid_t pgid) +{ + return syscall2(__NR_setpgid, pid, pgid); +} diff --git a/05/musl-final/src/unistd/setpgrp.c b/05/musl-final/src/unistd/setpgrp.c new file mode 100644 index 0000000..a2a37f6 --- /dev/null +++ b/05/musl-final/src/unistd/setpgrp.c @@ -0,0 +1,6 @@ +#include + +pid_t setpgrp(void) +{ + return setpgid(0, 0); +} diff --git a/05/musl-final/src/unistd/setregid.c b/05/musl-final/src/unistd/setregid.c new file mode 100644 index 0000000..158753b --- /dev/null +++ b/05/musl-final/src/unistd/setregid.c @@ -0,0 +1,9 @@ +#include +#include "syscall.h" +#include "libc.h" + +int setregid(gid_t rgid, gid_t egid) +{ + if (libc.rsyscall) return libc.rsyscall(__NR_setregid, rgid, egid, 0, 0, 0, 0); + return syscall2(__NR_setregid, rgid, egid); +} diff --git a/05/musl-final/src/unistd/setreuid.c b/05/musl-final/src/unistd/setreuid.c new file mode 100644 index 0000000..47c6730 --- /dev/null +++ b/05/musl-final/src/unistd/setreuid.c @@ -0,0 +1,9 @@ +#include +#include "syscall.h" +#include "libc.h" + +int setreuid(uid_t ruid, uid_t euid) +{ + if (libc.rsyscall) return libc.rsyscall(__NR_setreuid, ruid, euid, 0, 0, 0, 0); + return syscall2(__NR_setreuid, ruid, euid); +} diff --git a/05/musl-final/src/unistd/setsid.c b/05/musl-final/src/unistd/setsid.c new file mode 100644 index 0000000..e2c5690 --- /dev/null +++ b/05/musl-final/src/unistd/setsid.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +pid_t setsid(void) +{ + return syscall0(__NR_setsid); +} diff --git a/05/musl-final/src/unistd/setuid.c b/05/musl-final/src/unistd/setuid.c new file mode 100644 index 0000000..9e9da61 --- /dev/null +++ b/05/musl-final/src/unistd/setuid.c @@ -0,0 +1,9 @@ +#include +#include "syscall.h" +#include "libc.h" + +int setuid(uid_t uid) +{ + if (libc.rsyscall) return libc.rsyscall(__NR_setuid, uid, 0, 0, 0, 0, 0); + return syscall1(__NR_setuid, uid); +} diff --git a/05/musl-final/src/unistd/sleep.c b/05/musl-final/src/unistd/sleep.c new file mode 100644 index 0000000..d645094 --- /dev/null +++ b/05/musl-final/src/unistd/sleep.c @@ -0,0 +1,10 @@ +#include +#include + +unsigned sleep(unsigned seconds) +{ + struct timespec tv = { .tv_sec = seconds, .tv_nsec = 0 }; + if (nanosleep(&tv, &tv)) + return tv.tv_sec; + return 0; +} diff --git a/05/musl-final/src/unistd/symlink.c b/05/musl-final/src/unistd/symlink.c new file mode 100644 index 0000000..8d380d8 --- /dev/null +++ b/05/musl-final/src/unistd/symlink.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int symlink(const char *existing, const char *new) +{ + return syscall2(__NR_symlink, (long)existing, (long)new); +} diff --git a/05/musl-final/src/unistd/symlinkat.c b/05/musl-final/src/unistd/symlinkat.c new file mode 100644 index 0000000..9693b22 --- /dev/null +++ b/05/musl-final/src/unistd/symlinkat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int symlinkat(const char *existing, int fd, const char *new) +{ + return syscall3(__NR_symlinkat, (long)existing, fd, (long)new); +} diff --git a/05/musl-final/src/unistd/sync.c b/05/musl-final/src/unistd/sync.c new file mode 100644 index 0000000..a49808f --- /dev/null +++ b/05/musl-final/src/unistd/sync.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +void sync(void) +{ + syscall0(__NR_sync); +} diff --git a/05/musl-final/src/unistd/tcgetpgrp.c b/05/musl-final/src/unistd/tcgetpgrp.c new file mode 100644 index 0000000..50080c7 --- /dev/null +++ b/05/musl-final/src/unistd/tcgetpgrp.c @@ -0,0 +1,11 @@ +#include +#include +#include + +pid_t tcgetpgrp(int fd) +{ + int pgrp; + if (ioctl(fd, TIOCGPGRP, &pgrp) < 0) + return -1; + return pgrp; +} diff --git a/05/musl-final/src/unistd/tcsetpgrp.c b/05/musl-final/src/unistd/tcsetpgrp.c new file mode 100644 index 0000000..67c38cb --- /dev/null +++ b/05/musl-final/src/unistd/tcsetpgrp.c @@ -0,0 +1,9 @@ +#include +#include +#include + +int tcsetpgrp(int fd, pid_t pgrp) +{ + int pgrp_int = pgrp; + return ioctl(fd, TIOCSPGRP, &pgrp_int); +} diff --git a/05/musl-final/src/unistd/truncate.c b/05/musl-final/src/unistd/truncate.c new file mode 100644 index 0000000..322349d --- /dev/null +++ b/05/musl-final/src/unistd/truncate.c @@ -0,0 +1,10 @@ +#include +#include "syscall.h" +#include "libc.h" + +int truncate(const char *path, off_t length) +{ + return syscall3(__NR_truncate, (long)path, SYSCALL_LL(length)); +} + +LFS64(truncate); diff --git a/05/musl-final/src/unistd/ttyname.c b/05/musl-final/src/unistd/ttyname.c new file mode 100644 index 0000000..0f3e114 --- /dev/null +++ b/05/musl-final/src/unistd/ttyname.c @@ -0,0 +1,14 @@ +#include +#include +#include + +char *ttyname(int fd) +{ + static char buf[TTY_NAME_MAX]; + int result; + if ((result = ttyname_r(fd, buf, sizeof buf))) { + errno = result; + return NULL; + } + return buf; +} diff --git a/05/musl-final/src/unistd/ttyname_r.c b/05/musl-final/src/unistd/ttyname_r.c new file mode 100644 index 0000000..f86fbd9 --- /dev/null +++ b/05/musl-final/src/unistd/ttyname_r.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +int ttyname_r(int fd, char *name, size_t size) +{ + char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2]; + ssize_t l; + + if (!isatty(fd)) return ENOTTY; + + snprintf(procname, sizeof procname, "/proc/self/fd/%d", fd); + l = readlink(procname, name, size); + + if (l < 0) return errno; + else if (l == size) return ERANGE; + else return 0; +} diff --git a/05/musl-final/src/unistd/ualarm.c b/05/musl-final/src/unistd/ualarm.c new file mode 100644 index 0000000..be85303 --- /dev/null +++ b/05/musl-final/src/unistd/ualarm.c @@ -0,0 +1,8 @@ +#include +#include "syscall.h" + +/* FIXME: ?? */ +useconds_t ualarm(useconds_t useconds, useconds_t interval) +{ + return -1; +} diff --git a/05/musl-final/src/unistd/unlink.c b/05/musl-final/src/unistd/unlink.c new file mode 100644 index 0000000..fb57792 --- /dev/null +++ b/05/musl-final/src/unistd/unlink.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int unlink(const char *path) +{ + return __syscall_unlink(path); +} diff --git a/05/musl-final/src/unistd/unlinkat.c b/05/musl-final/src/unistd/unlinkat.c new file mode 100644 index 0000000..47fccc1 --- /dev/null +++ b/05/musl-final/src/unistd/unlinkat.c @@ -0,0 +1,7 @@ +#include +#include "syscall.h" + +int unlinkat(int fd, const char *path, int flag) +{ + return syscall3(__NR_unlinkat, fd, (long)path, flag); +} diff --git a/05/musl-final/src/unistd/usleep.c b/05/musl-final/src/unistd/usleep.c new file mode 100644 index 0000000..e386901 --- /dev/null +++ b/05/musl-final/src/unistd/usleep.c @@ -0,0 +1,11 @@ +#include +#include + +int usleep(useconds_t useconds) +{ + struct timespec tv = { + .tv_sec = useconds/1000000, + .tv_nsec = (useconds%1000000)*1000 + }; + return nanosleep(&tv, &tv); +} diff --git a/05/musl-final/src/unistd/write.c b/05/musl-final/src/unistd/write.c new file mode 100644 index 0000000..426cfc5 --- /dev/null +++ b/05/musl-final/src/unistd/write.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t write(int fd, const void *buf, size_t count) +{ + int r; + CANCELPT_BEGIN; + r = __syscall_write(fd, buf, count); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/src/unistd/writev.c b/05/musl-final/src/unistd/writev.c new file mode 100644 index 0000000..a6a118a --- /dev/null +++ b/05/musl-final/src/unistd/writev.c @@ -0,0 +1,12 @@ +#include +#include "syscall.h" +#include "libc.h" + +ssize_t writev(int fd, const struct iovec *iov, int count) +{ + ssize_t r; + CANCELPT_BEGIN; + r = syscall3(__NR_writev, fd, (long)iov, count); + CANCELPT_END; + return r; +} diff --git a/05/musl-final/tools/gen-musl-gcc.sh b/05/musl-final/tools/gen-musl-gcc.sh new file mode 100644 index 0000000..89f7f4d --- /dev/null +++ b/05/musl-final/tools/gen-musl-gcc.sh @@ -0,0 +1,30 @@ +#!/bin/sh + +printf '#!/bin/sh\n\nlibc_prefix="%s"\n' "$1" + +cat <<"EOF" +libc_lib=$libc_prefix/lib +libc_inc=$libc_prefix/include +libc_crt="$libc_lib/crt1.o" +libc_start="$libc_lib/crti.o" +libc_end="$libc_lib/crtn.o" + +gcc_inc=$libc_inc +libgcc="`gcc \"$@\" -print-file-name=libgcc.a`" + +gcc -wrapper sh,-c,' +x= ; y= ; z= ; s= ; for i ; do + [ "$z" ] || set -- ; z=1 + case "$i" in + -shared) s=1 ; set -- "$@" -shared ;; + -Lxxxxxx) x=1 ;; + -xxxxxx) x= ; [ "$s" ] || set -- "$@" "'"$libc_start"'" "'"$libc_crt"'" ;; + -l*) [ "$y" ] || set -- "$@" '"$libc_end"' ; set -- "$@" "$i" ; y=1 ;; + *) [ "$x" ] || set -- "$@" "$i" ;; + esac +done +exec "$0" "$@" +' -std=gnu99 -nostdinc -nostdlib \ + -isystem "$libc_inc" -isystem "$gcc_inc" \ + -Wl,-xxxxxx "$@" -L"$libc_lib" -lc "$libgcc" -Lxxxxxx -Wl,-nostdlib +EOF diff --git a/05/test.c b/05/test.c index 70c80b0..50e5cc8 100644 --- a/05/test.c +++ b/05/test.c @@ -1,5 +1,7 @@ -extern unsigned long __syscall(int, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long); +#include +#include + int main(void) { - __syscall(1, 1, (unsigned long)"Hello, world!\n", 14, 0, 0, 0); - return 42; + putc('a', stdout); + return 0; } diff --git a/06/Makefile b/06/Makefile index 6d58744..519cb7a 100644 --- a/06/Makefile +++ b/06/Makefile @@ -7,23 +7,30 @@ CFLAGS= -I $(MUSLINST)/include -I $(LUAINST)/include -I deps/lpeglabel SRCS = $(sort $(wildcard **/*.c)) OBJS = $(SRCS:.c=.o) -all: zsh +all: test.out #zsh -deps/lua-bootstrap/lib/liblua.a: - $(MAKE) -j8 -C deps/lua-5.4.6 - $(MAKE) -C deps/lua-5.4.6 install +test.out: test.s.o test.c.o + $(TCC) -static -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include ../musl-bootstrap/lib/*.[oa] test.s.o test.c.o -o test.out +test.s.o: $(TCC0) test.s + $(TCC) -static -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include ../musl-bootstrap/lib/*.[oa] -c test.s -o test.s.o +test.c.o: $(TCC0) test.c + $(TCC) -static -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include ../musl-bootstrap/lib/*.[oa] -c test.c -o test.c.o -deps/lpeglabel/lpeglabel.a: deps/lua-bootstrap/lib/liblua.a - $(MAKE) -C deps/lpeglabel +# deps/lua-bootstrap/lib/liblua.a: +# $(MAKE) -j8 -C deps/lua-5.4.6 +# $(MAKE) -C deps/lua-5.4.6 install -%.o: %.c deps/lua-bootstrap/lib/liblua.a - $(CC) $(CFLAGS) -c -o $@ $< +# deps/lpeglabel/lpeglabel.a: deps/lua-bootstrap/lib/liblua.a +# $(MAKE) -C deps/lpeglabel -zsh: $(OBJS) deps/lua-bootstrap/lib/liblua.a deps/lpeglabel/lpeglabel.a - $(CC) -nostdlib -B $(TCCINST) -o zsh $(OBJS) deps/lpeglabel/lpeglabel.a $(LUAINST)/lib/liblua.a $(MUSLINST)/lib/*.[oa] +# %.o: %.c deps/lua-bootstrap/lib/liblua.a +# $(CC) $(CFLAGS) -c -o $@ $< -run: zsh - ./zsh +# zsh: $(OBJS) deps/lua-bootstrap/lib/liblua.a deps/lpeglabel/lpeglabel.a +# $(CC) -nostdlib -B $(TCCINST) -o zsh $(OBJS) deps/lpeglabel/lpeglabel.a $(LUAINST)/lib/liblua.a $(MUSLINST)/lib/*.[oa] + +# run: zsh +# ./zsh c: rm -f zsh diff --git a/06/src/ast.lua b/06/src/ast.lua index 93b4c6d..844c4ca 100644 --- a/06/src/ast.lua +++ b/06/src/ast.lua @@ -1,32 +1,32 @@ util = require('src.util') local function parse_tree_to_ast(parsetree) - stack = {{i=1,node=parsetree}}; + local stack = {{i=1,node=parsetree}}; - while #stack > 0 do - current = table.remove(stack, 1) + while #stack > 0 do + local current = table.remove(stack, 1) for i = current.i, #current.node, 1 do - inner = current.node[i] + local inner = current.node[i] if type(inner) == "table" then current.i = i + 1; + print(current.node.rule) table.insert(stack, current) table.insert(stack, {i=1, node=inner}) break; else - print(inner) + print(string.rep(" ", #stack) .. inner) end end - end + end return {} end local function print_ast(ast) - end return { parse_tree_to_ast = parse_tree_to_ast, print = print_ast -} \ No newline at end of file +} diff --git a/06/src/main.lua b/06/src/main.lua index d1a6ab9..b2b52a9 100644 --- a/06/src/main.lua +++ b/06/src/main.lua @@ -12,13 +12,13 @@ raw = f:read("*all") f:close() local errors = 0 -local function printerror(desc,line,col,sfail,trec) +local function printerror(desc, line, col, sfail, trec) errors = errors+1 print("Error #"..errors..": "..desc.." on line "..line.."(col "..col..")") end -local result, errors = pg.parse(raw, grammar, printerror) +local result, _ = pg.parse(raw, grammar, printerror) -my_ast = ast.parse_tree_to_ast(result) -ast.print(my_ast) +print(util.dump(result)) +local my_ast = ast.parse_tree_to_ast(result) diff --git a/06/test.zig b/06/test.zig index 8ae7065..f4f43d0 100644 --- a/06/test.zig +++ b/06/test.zig @@ -1,4 +1 @@ -pub fn main() void { - const x = 0; - if (x) {} -} +const _x = 0; diff --git a/scripts/build.sh b/scripts/build.sh index a15896f..f6a82b4 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,3 +1,3 @@ #!/bin/bash -./bootstrap.sh +cd 06; make | less -R +F diff --git a/scripts/run.sh b/scripts/run.sh index 86b46b6..61b483b 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -cd 06 && ./zsh 2>&1 | less -R +F \ No newline at end of file +cd 06 && ./zsh 2>&1 | less -R +F From e299e0cd3ee752952aa1dedb9da9b11a15b8cfb4 Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Sat, 5 Apr 2025 10:58:32 +0100 Subject: [PATCH 05/10] fixing --- 05/Makefile | 1 + 05/test | Bin 0 -> 85112 bytes 2 files changed, 1 insertion(+) create mode 100755 05/test diff --git a/05/Makefile b/05/Makefile index ab43748..e6f0c16 100644 --- a/05/Makefile +++ b/05/Makefile @@ -22,6 +22,7 @@ $(TCCDIR)/lib/libtcc1.a: $(TCC0) $(TCCDIR)/lib/*.[cS] $(TCC0) -c $(TCCDIR)/lib/libtcc1.c -o $(TCCDIR)/lib/libtcc1.o $(TCC0) -ar $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/lib/*.o musl: tcc-files + mkdir -p musl-boostrap $(MAKE) -j8 -C musl-0.6.0 $(MAKE) -C musl-0.6.0 install tcc-files: $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/include/*.h diff --git a/05/test b/05/test new file mode 100755 index 0000000000000000000000000000000000000000..f135ccf835b39e5def416bc3d48f2d83b5b8afa0 GIT binary patch literal 85112 zcmeHw349gR+5Su*5Y_}l1vi33WDlSsh)d7}gM#9M+G-6U2@-(>lYqDa29y}FR;^WQ zTSaTJZdk2SMGa~Nm$q1~#i})~y%AJg+Qy~j|Ge*6=DEoo3F!C#zJ4F```tUwJoB9Q zyk|diX71dD1(PPEg<;^fwBYbSjv9kJrObB_j46yXdBK<<4d;6VS*lG}rTcOr%zo84 z!@GSDWGf-oI^g5@RpX{9@AeyPbXRp#8eiE8rWV${Tex_$;KKU!8HZ+L7Zlb#y}DSK zx^;;=GlF2XoPOp>`7Rv!!Nj^3&Md5ZtFZQ+EmKaOJhGv1yi~yQsOaZr+x{x~Yl6y2*``>Iybas++u_zFwe8p%G!%KC|QPrx+1d|I5VTQ{lhkCW=Z*EoLbixPk8v(0k0uArg5ph5Dh z3rtad*?h>72qYis(|f{)`b8r^Xaai4ndRyEU?q5I#~^6xEHbGKRnviDB6)tYrAcpEEHy18mE3m#t?fYSUMyD6*G9)m}Vvn=6YkL>gPsRR;ER;!t zGa#+7F4T%9I}I|>1{q_6oQ5Ech4oh0sCwtJ+CI$+r$M+;g!MfNL#Z(v3+uA1(Gk|m zdU%=nSdZE-g5LlInK!LPpbeTpEppC`rm!g!n#kLxc5+yUmN0n(+(2d2hH$#D-gmT> zXOZBrur4F>!GbNz;7(Gq;0c@x3&%c@d09Ufa&dz=RzG=5=3`l7U&vg7)*Zln)2or7 zL7^tt<;+0%4rkQ-7&Os_!url)eb5UySd9~_P1^tux1Uy}Yd!02{gi53>r#NyekR+O zUCV;VMs%xebR!VM%0kUU_yy}oHsUT=DX1Q@x{TFYS^B8kNjU)bd!S^HK3^fT)rRSi zz%OpxszgD;G<%H{)%dV~ZQbg_xW=pTM9E9^hDZWP)y6s!RKdL{7tv3lLVDd()MXtOyCryd;qLH=j$S~Mw0&AK}p>xMSX<^k(P`PGO-7GktRD%p#Nx)^R0SUli#QOd+XM^g3oN)M5q<7 zE~CESv!A-O+Xp%3rn7&7{!+YWcLgq-w#SPL7 z#n&>#5!3yDaUhh^yx^+V9?6W45Ki@=5^`$O+c0`Bq%_22Yg7nk(hk{1$kAu5OZ;8J ze|_HQ7=m%(d>F;>DxQwl5m;+z4%$Hd-t(%N&OFt+%W}C<(z)~U?Nu(*qI4R?X zxiUc1VMZXSG%njxXxa^#>->5D0QLt3q!W~u$Uxlk>?37J%_$cf*t!&)^7{}le z%B_BK0zO_AqH%IxB_=+-%Fes$ApK`y-OK3oC)YhOxel|TNjVvr=@Q3BsNp(0^YgB3 z1eInZS%^87wtCHl!qtBhv)Cly?Z{6rkqk#2db)7+p+=#ST1emm=U09DzYor~lZJ;O zZQ4!D)u$(ftMbKrdwbv`x=89qZ~+>$0g& zpEFXDvPgS=E%UwAapa~>q>jVs$@M>$)0p}+%`!d17_ijrdP$Jw>3^ z`t*B6qS5G-NG4(k_ycqe$ji>Ks`J_OWh%o=XK_Qbx?{WDS#qE?7XWdg1T)}~IartI zC*El_L-9tj$av}6(mf-xrBR!K^d~r>RiVDyZt8o69Kn-CP{9)oglezNAv<&VA*xS* zUwSeOO6UzzlqnUbX*j6tpKV&R39U1IRqBu!Z7{Y=DiC*9>!BPDKf|?2vy*C$yRO$}G_KNb3^+>LRg82ar@U zE#`t(84D=82K4+W^EW%1`E#{1EmMKoL|C{s(GKQhc#yH65qmpkPK2^4+sKitxYBCa zYL$)V5It@|kCfU-7Ym&^&vhJ0{x#(Rs-R0Y_G)FU6NWXhnj1$tK zYvDx35<}sH8b;ihnn=>6+j3F2@ELQX#KqZC3vJqgU9_W2>q(3pJxnyh#Q9bAj77=o-wVP3o6#2dR%=0|bk zp+F&yPTBlBYI zcal_WFeS_BKvn7!9LCe!0)Bbl|;<~SJrQv(7 z!uoE@(H+VResLmz-1kG;&Yssu)6W)(bUGMW<*Ive1D2Q2L!+Nq+!$Zwj06}fhe&$SEShAHLOwib`CT+0!0F}-BpZkI>9;^s+nkoUR0~!V zP4~kU-+Y&+FSKSdF-^myq_#QCTsjgu+tQ=XW_kK)Dt#JpA7cW^B@w!u_UP(2-DT$> zOZrJ*Oupc>Nc&p*ul4{9HfS0J2cp41lD6l5U}_ukv><958zudNO#0Og2`0LaeDk*`XmFeNx`NAGzH?*K=BFQ z#I!UFORL|&0FP~UqbYRgWOQrxXHWt=yiCU4?fOHH1QXZ#c4A;&^jbS{Mj$1}! zUu;+auTuVN$mYe8HHUdvEzQD%CtQjM$acT12g`?7=lZ0Ui&6e?HAjoo4i5q zy)N;{7kaI$7c%P-BRp4sV-^KQHeA$e*t<4u!2x~|da{YHUf2_W*j5H1?OcXi2I zmpA}q)9I*K$*$)~6s~I2nbm8|hiaP#W-gC9nVD$gaH_VUUH#-n9S=4DZUECev?g7KlS?hGRhlEzKQCvgDXb!5Yg z1yQ783H1jnkqEZM3=c91S&zf@RP#|0?IMq`sI$_=OC2T_8l*%kHZ01bs?`dMlQ{G& zQjKK#`i$r#^;41Z7tFA+!4zW)mV;@b`MN%WizX?55{Nm0-W47oN|eVX+Hu-$4i@2_ ztty%mQfszo0(CkDw@@f%SR;=?%Zh~Y*rx+MfKzxkN%}sHM zk%UMGzO=hAnqmOdK`B9dZi%k!FUJwgyo9DA0j|(AqSK+#rs-3ROd8DAtxFr^yx2)K z7if&esfl%KwJ)7m*RWcK5}Aj$)4j~YQFU>Ji9_Asg&i92GgmXnDakX%lt_CDBRanBp&>y?O|Yc0n8%1Q6cbS>CgK5jSB3YN%^-H@(dln38 zFtb*U;M_53BR7Uo`&}JeZ)Rtvw$x7*KO2k_&GaAgrBuJ!6lt@ozQgb6x z(}a11QD6~8jx*4k!y*|@yoW;2NKj3DAF&AGeE02}kG2$B?eeCcYesqW}OkS<%UG63K{-ip<1~QcW0!( ztSh(~!ER0}Hq=Ndw#04dk#G`uzAiD)Bx-%r-Uc9|6xSBw0=&Y_8&sX8ooqffH6dZz zv7wHLlP0!RrubL;N@7~(#khEbTWAXSXqPh9*n#oMt6*4VnO8T&wp6#s({zr>P|3m7 z=4ulKA%y`4>!&vOfk<*Zqv`-Ex?Y^D zmua5qpKJ|CHEF*Wz8AbR9OVlUdPloZ7@%N zVtuJLVNsH%()`!N=xe0yNfJ%JH205@Qp|tqGLT163E^;g`nnub277JtWsp%ndA%&J z^IG1sSj)S1A4o$y5AX!i@P3B5K`T>`te5~LJ^-q3=VbIaso_rLeBxD%iM6YV`8-KkseQcM6oYvfP2B`d&i@e&1_k zu4zEem2`tF+nsN5GflkQQrIu_hQ40dY1={=Y;}7s&M35Xun6CwP(7 zT|uK63b#-fyBaH$x|udFD^Fj%O*xZ|lYVMbH#M;X_I}CsrnIn!uFyZHrQ?%{Kq_j< zGxbw97MA>RiXBHb0LY|t>S`nf&w*gpUC@9l2kh{~l#KC736qI{Y*E7c!I8q+r?Px$ z{dYQZ?OVk2*_p}S_Oo-;CEzNoQq-O>aW4{#{Q^ETjkV{|;H z0mtj3|(h8&^09}d*r@@&~HFF=`~RW8j?a$TYc&zP;2juWM5_fP1U zog}k!`sq49M8C56_7(*Sqmcm&KqgeUSNf(wB&~9`?>=>I7 zpL8J@k!wp`G%jh##SKu%wWA14XShKV6d7@skRSY26NDKm5_4s5*lNNGxcDtI02}?5 zP0KfZ01C+lg=v+$c#7tw*%HXD@YSc!?~%=S@b#=X?H`;$;?Q$rBD3aoTr}FOa_2R% z{K%$;tVa}BKC{o;9VHQJzBddsW2zfsvt=TQNAhZYthGkMPx&=3OVe2OO3YnWC$e9W z30$+%BLROStx*CyC&E#=K$m6wH0<3SNUj z%`u&OFM<#r*=xSW`hwRG8L|#KoiW<^JD#OP7RXAHETpJ9@g?e zu0Bm;LW8`>;K;xf5R#^n25I)vOG~C;x~f)j660C}Y}rW)I#OBOL@hxN`mCJB7L7d@ z{snh7ewI3FSvG3j%*2)~o6DMCom@9z4^Osi-5zbUm?iGGhrs~tcrpTz^B z&l7M!&MnvSK$pJIECAN74>gbQFuvpwDgl}V`ccylV{ZC&*;0m{C*|`qroCadu8+)J zfm`-kQlA!P4sK|Y=V_V_w$zk2hUQ@Um+?x&s~0uCzVX#zwu34vS0R%FeA4*g9F5 zNES5D$gGer^B{s0Udtu*+Ykrxcx_^L^CWX^;^NvwZ<7;Xwk`}7uMdJns3QfY{0C)T z(M$YVjS8S!Q?dAZhT75kVa}r>$)NdSW7Bv!FD0h+rz!J_0l-?PAh3ffTD|lg(%fE5 z=sl~Z&QL{7Evdv@($eqkl#K~<1qR1lvyyjyyUc4Eu&S?hSeF%Kbr_QLWsrWFc?O>2 z5ZdQULQo>{l*}m!lenRN1q~}k=ZXxJGDJb%j|v-TFLI_ zUdY^zL9jUSZ_M{<69>ti2nqBXlc||{&%xe`$O;NONjKbxCGpzku3uLG>b)y~Msr7l z3jpVzPbz@TS^%caY5{C)a{(Z^>7z3wS1+E7WPfc5h_)xRpZF5+nbjz$F>&uVEP+?& zWJz=1^1Adrc2+9az?lyfG^eWg=i75AhlN@WM@Tv3xpHv1jmcq)>Q2=exw-Zm=4KY$ zz?D-LidUvzGEvWvY2D(F(df|Xa8S_v>Of!pMgQusKX;H0AnUKMywUiNr_!~GW?qYW zZyqn!dN037f&@R!(jXV+1g~#QT!z{uSISLtkOtJp znMJs^_VW>$mr4I@xv7aT`Vo;t+APjwz!zAQHJU+KK3?DSDMB=zVuQgwJSe#MbMY;+ z_JAZ7OJ}CkZ5oi4we6irlaTdIKSvO~OoOx9^=m$&?%1gct^}}_=YLZa<mZaGrV|@C3&RDb%xG{3vvU7SHd8?p*Llk^^DE6&nIx$Jomm4T!*TuK?r}9X)rdu@ z_HxD!k#fz8K1z7hSAW@H)=T$xC*9y@8hMAJ+Pdk4T+RZ-ryg{Rk zE=X{lBkK@8VfLW_q#Ze{d6Xe_eUpSk43jsYL*4YUIjd!0qZ}zlvhjseNtvr%kL0yp z)0oGbGasD1Wm(3im!L)8H?dl9(YLk#xB2V;j!x7CF$?{q=`VQ|ZR>X1~nl(@M*`-!0#4 zj|WaPy2O!Yzs2sG?S70suSR3O+TqvL@-t=LB>&q7A8%#OVbQzaZ97g@B~$5#s2~i| z!aOJSd$E(zf-l6`RCXj#D*Z{@sXucY`mD4(D5C8LfT{es1$-EE3>Mk);cBdYw#$o* z0KJ4yDmx-drBB4@r9jdo->m%~VAkL^vD3Rlo;%e2z9`64@g4+T>^S{M9J@{IEb5f! z4s~A{V@FPkeq{^&_ha-cz)QYuaQS94?snzV1J@24HCFSL%Ki&6_TL3B>FQ(K@lb1L zv*imcFUu&heHCLz&S^eay&TJS9P&F=e#*I2`ir)qZ_(cbSH|dX1~2*6IDVQ$g?6L6 zV5Q|<`_C|Ra5s3#2WS6v%RgcDuAgXGZ)bMPvr4o36ZBt~-qkJi|JsIrOZQ~^y)ju% zWq&AmNw4e22Fj!en>=eI>2h|K8ag=0@~ijDQ^uA1p_ZSQX?VH6F56L-f5+$gKH~Iiw^RS( zHuSv*C;R_FjJ^XVd^#>zeShZ=_L9$TKIQ1{(0^S%uN;yb@4#*77Y$3+Puzw+F+5rS zy%_yD;3Zurjxc)H|1Yro7rBOa{l>+XA7J&453or-%(T25%O)!dUzZQfE%evN=pO(d zV%ELt&}nKnOTTeZX!(W~{2I$|Xu&sFzR~h1Zrz@;{5vi9^_EXq-i;&ATYgImegk;P z&%{A_YIlS&ANS957Zxb9pTEcm({y@-TpY1 zEf*hoyHaD2Cx?C;4J7v6{Jg#GpJjcq2gVn7ed6pdwYcfI56KWce*{p0xGTRV$7=K%%5srqf^M`Hhm^gLw_wEQkehw#6% z{IQnb)AH`R_dv{JWIGzX_~Wj(T)f{=zGL97zg#^p0>3Zpx13imvi4nnxXG9g@OlN+ zw=9>HR`2@T2CIM2^6q+8@=>-YE$_yWafS{)1ux}$n#~_e2c40=RQxV6{9ZBqelh&; z82;cGJ`cQ+1ozS{b83wUW?E&Y7h>TkC5F6ZYV z7>^|XuNi9WxOVr3<$=ZXBo2#I|`p3WZ7}woy<_aV~}UBI~?Cl z`LtGc`p57GE8nh_{+Jm4bmiN((pSXr3&Homn*yExE+2xLvg(@QCBdBH@`|9krnGWi zO)z&}O-WEvRg=45%;=)g2j&8f99|s^pEM#Us;()nstJmUW|vPdF$YB&ROI-S!<2;; zfYntcxn&jeb6e?guBfO8r^>4;E3CPoY(aUA2?CHSN07~~ESV(&vzsxmqNKX0vZ6?} zMMX1a%$`?0Ga?F{iSmk?vZ{*W*}3AV^#sW)uY@m^^UJDc%&t5aw1bF&p_~njZFR_m zG=#ctb{CLUj;AUbdCK{0*a zj9`X|puDo^+^X`LGJjNERwkLF7K=(|S5}v)yEQYb%8E-R*Wn#Z6c<&WTRc~GbETBe zt1c;?J$r;nnK?l5T6&}dqZ~NEfdd^l$br!kxv@}GRb~rM%i9*MW|l8z2i#t(hm>`Z zlpD03D6X-^KYd>L>>4;d6A22c=2aA#GeuGnYRW4ss!atkY#`~XG-iASu>KiOs;Vj~ zC0B|itVTCSgXEf!5Se2>Wf*hH#%A)cVL@@}{Bm3K5T+t`jv+M1TAo4> z;U%OLw$ZCe&_zY_B{NMA16W*jR$$|c#2T&GF!9R?)1tJ+;RG|(GE$zA280vHr;4(( zQ1!`}399WaDX6wsFuHHqva8A~&dN1;Wdr%PD7g;KHo{`m0XZlwGY*q%RvBc&hv(Lm zlnfg&eC+U12TLa=A21_vZqo1r@Hr5lgYX%R&%yYN!DnoMWS}h+%`U~!Nc7iH@s>*Q z`odnIN97CVRa95cC@q&$Y~idy@La+_Ul#+0%-QuC1=bI`d4#nUS> zbVwGLNi#(Eq%($A~LQK;~y93bKtFsQ_AiYV0_J?Z!($4@Fc ze!_&43QjIMdHj)+3W{V7VW&GfoxoH*D8)yvg$EEG27C!VvQ5Ot;n{@c+zR=!TPnN^ z=UmuZ2}_Vv`lrF?V1n~ZIyyT@IEOpm)_ z{^#`Ff?YiB9>_xw#%5+9>*EGzVIFU=q@{hB7r1LC90qcoF1)Ni97(tb;p3g?KTP;#;7h~-Y;w=Y;jM)GAd4Me2Y!W-2aJcdK{JswrxRv7aagW%bCCOz zKcdOklEAaX7Waow2KNl)Aw`Gfdd%QmgEI+Bn=t%t!D7PS_qb1R8{uDhyqDHT6L#<5 zUzX3oUe=6+mvZcymKW5+XSZDeEPPg4UhoLv=YTJ84W!h}9l%3pzehB`%O@8C!_`ebU68I7; z%4{FueF+~*_(AAh`7%Df4Vu}cxtK89`8r$*KP6o^AgtSN1a|y0gd2ffy1pVT_e2~o z_d6VxH4T@pI6RK@#}Q`z%ov_EOIgD>#)n^#=C_2|CSzGYaq0Sua7U~UIG*+WVtCe! zZG+|{(v%TqJB$~S=4!&MxhsZe&6A}06JgeT6vMNo0|r%>KYa+ZhVe+!97UKljK517 zS?h3dv1VBe&zf6E^AKUyF#bJh8VR$8@fW1Y%t-C$pctO*96_2XgjvJ*Y|>mvm^F;A ziP6a4KcvcA4!af_nFmULBD|XiIxKS}gFEPa$zhqd8k`X@KEm@|bUte|-Gj3|-y`^m z$326mJ>Dhw#N*uqnG>0?y9aU~!{FY8`vf(f?-ShR@m|3Lg!fEyVfPLqyiYK|^L+zZ zr#JTd2lGANFOczH_#F5p&oKQ?^JTIOD<&qWl@`v4oZTI{GhA$sj-^h1N6dCjokevgOi{cWSk4bswFTq6UyMmPKcL9WG; z4{~2$cXa7@L7up6D6sHyFaC7Gvw<(MqGg0{B>Vv3!N4o5J`S%V{a*;P{<9dK zH9fjVzU2^R4dcT}a{^)3FrG!4MTD;*%=)`yc-H*34VpJd^D$wz!?+6?lPjZr2(xBP z49}Y5NmERiH5bJ2tht&rKPSu@#*dQb4}@96_&w5mL6|kYupsC1gn7opNOKfn)-e7q zX=W2<4dY8mBmWDL%Aa4y@NDNf(!55PHH`m3nhY$dq>7jIePc8S$M76>GBh&JmwrQd z*UvjF^L&HTb?z!y`l9_UFMZB05cU+GecO4mJ86azW;+vNc-EXjnhL_KVZ4+y*Ar$9<5i^jE#ZxX zS^r54&zjD?x1B#Zq&b){+hKe>X{Hlq4dV+*b2Z_g6K4J6F+6KtBh80|Srdmd@FU4o z?I=5jKRAXzE{5lLXF-!A&rxIkAbdWsYz||}XmKa0$`*Il^)#dD9OQYvOK`fyIoO|r z&pfq%tp3i<=#K^K>>R7}9K%l{JXz;F!i$}u_$*XAGXwcup3%<~Gah&GxLog*yRgoG z>C;X|*vnzZZS}yy%QN8*6aGE0%l~%>|C4YJyhYw=>cFqCMj7X{K{K8-ClO{lUx%Il z6$tCLN?^zTl<>V|{|Umc5uO9=(!%(oHfXwHnZV@gC_?``BO{yCxnLqyY$B4RiuBC zFza8B;aT$qX}aOTTIU;U81F}#afBxmW_=t!3;G;9bs_m9ntXi%Bk)+-j^TT1+j00{ z&+ig2Ugr5;!R3VS@OZc2Ws9ZkUdLyl`mk3Z&($0IAh0_$S_d4KYafIA!*7QN z1ai;X@B@NtJRYRa9vFU*{-vYALxT6bW=Qb4$3qDZ)8EP(eXc%ZV(^F{8|z309~cZL ze7?sA1s8gJa3H@?H2N{YFD;gQklzvrFLiZBH}pj&3vh%Ni2w^PziW7y@C(4Mp1e)C z4%pRO#$S-88y4hT*!>B!p79}V(3}8`j2GgkgmvSE!%~h0o3%=ZvptXBoO+yre{OgB z&cV?ZOI}U3Sn}l8h^qwgx~&vgc=;{HHH3cw?9%%%;g<+c2X^_!_-}2{wBIMPzZ+q; z!+02J@(8nr@pqw-ep1pUyz3_&#wC{JJLySK7; z+)A2X6J`zL=SlM>Vb**Z!?UJGc9cK+6CO*L^>O&582#DAFC%;tVYc}o@#_h*hVfgZ z`IIng7~_YWO3ANtTy>W+^`3Q~MJF zd7p#POaSX_P9U2{C*jC`A=#f8$h|gWXJT-Q#S+)K_?)G2O%CK;5Jn@Tm(x!s`^OPJ zUZ3MN`YFLlUVlPxnZ;sX-YZe9_NNB&P6?x*iZjmsRI+(W5{}Yxs!xm9EW&58+B`jw z_fZ&|r;ACCqx}D#w<%%KeFs-+*!h9Ho5F;hFF`$y!k+8H<_8M`bT?MBK%ZAJVb2Q= z^89&$l#=1k52jcw@z&t;V~zLXK;C^}G#7()`im*9g@hMTyo)H_#Xeq_CwHp-8v}WV zhOvJm*}su&{*>^|0jjUn-%S4BqR)yLJ1c@}uenX@r{Ql4e(L$#gIhiRMR2djD}yII z{$=o@$9D#Ad3;y!vB&qAXT7yet94%u;#-+HOv^G3tJOZYv#);9d#f@xl#(D|X^n*wPK2EQMyv{>SO z7N0jY-VXzLuZz)q2-fL8q%?kuX?s?S>kdFPDLYz6Cl-b#L^g;AfD7LN3qc7)}Z2`+4UD63q? zW_noSaR?Hu0b^Ww0xf= zEZ5OSzX$oeM|h;?_e{ca?O^nKg{OJEcX$@zg&yw{Ug>dP!u`UZc)nlwq{scks4eG& zZ+m_~_=(5+hZ(+o4Gj13cu;tt$AiOTJRTB8?RRJ>?;$h(4-1!iJUqPCV#!arPVdlP z+Rmu3xKEzdOAqM6jtY12cyx%Yx0=!6!5)tZ5A}F#c%sMS!eWmP4Q2c?{u~xw?(yMN z)_LLmmKWb7f4Zx0`Juc+z=X{w-}0$W9-V|`j5hkha0ua(JU%9zPPp3R_eD zDwio?8oV&}PYCsQHagav7`kyJKbRUG;5Da&`nw#}oEFaU{AuALkH16syJ6Jdo*v%l zHK&JndwfQ?hVTm>pBYNuYkVsTKlONeI3~^UrQu;7mxYr&o)Mnv@mb*_!t!o2V{>N6 zIHR58%fmf9K0DmcaztqF|p}dE|#5Et@JNxsg9$t`y z2YAheR1Yr0;`CYQwv|zBXLv@v^X;&(CF{tb-Xp>j+;Sp6dDf zu*Bo#;cSnu4CP%WChV2rk39Zg;AOQAbi8~SBJfQSzHtD@A0+aSdV`cPVo4q z@C?G2c>I$v!aohK_xvs4J%sP~ctsfDTM7T1@a^FfUVjJGlRLuqJbx$QyTZ>szbfqD z>&AWIzJ%p{btbQV73O<lhE2g0QmOI?!pcl7Tsb!l}d?*K7j&ELp49F1jVV%cMhc|irOn8gO&-yVdKX^Wrxq*rI_eogRU=9DraIi1mSE&BH z63Y6j(foz*tD*i@O#749!kfJ2_3%!Q{~F5sZ%o)X!ryw_7$!V^GyK5gx5BSHemCss z%jMlr-e+U%yhr$N;b_k%!UB(*!jnCIKa}6GnXn&(*ASL>F&h5EFv1^&4|#q|__W6# zhw`pOqyHpKNBJ83k1*Tge}+RmZl-?bi*TanzYNd!_^a@j9{($B@OW$Zvd2N%R*%!t z2DLZwN*$AT$_(i*b*y6`?=3X`bi^6wo4Jo;_)avB>KwTFiP*`;=U}yy8OVDAjb3V} zvy(}4)GUuBtjvv%(y+S*@~%GeQc_$y#n zUYYwvco5-7pmF8J_|P_JP9n`r!ffY~7@jrPljfI%S;KfOXk7q zco<=}b94;Pno`otC(N2Sd=>OLK_@L=(c}Zm<}ixi;?DY;Aj5YF2710L;Vh3~Dp-!s zjcR|_K;CU-!kYfd;mH1OWPf*WCqL*-crTBgeX&!Dc%Fs*TM?JrZUYux-i7rd;dg;u z{x=iuxqrm30PpH2}`79b-*J}|t+=L^8yXxntYvxf2hq!~+?HH=Rp%}m0qxg>^X&Gn?YgD`6tKS`QD z5oQhJ%`uwxqqdzVdq5+Tb2t+njKS%;{v$XCoUAJfmOf{f!Fj<@*m2u1 zVBzII2+klp57?E_4+!5%_))?`fL&h{hc}S^ZNjYoR}9aZJr0O`8%CHljE^SGcL}qG zaSdte2;W4Q^$*7ItoeN#GzrptL743@?sZ_45Bn2l&G;CeHK&p0Y{INr62r6R$E3N3 zFl!h;O`45_S;P1v(zH7$N;_-zj^SA|nlzILvxadAY331T4dd^V<~G8tc`Sx!%}bZzW9++*ok!mo*p4&2L@|pXtNq<4xWrUb9P3?{Tl-W{buC?G{U)_9pCCAuhL70}C(j?O#E7 z4Y13rjfCGP+#WZdo#q_yuCI&3yOVw}VbFk=@ErCh&`4h={f6+auXA{J^eF~+)Zgei{0q-#1b0|0eN=H182+O>kJ0 z56m;(oiu|9vxf1Jq&b-|YZ%WW%|gPgxgmyU&96wao-k_|zd@Rh3A2WAm&2pH$|lU3 zLt=Q=Od(A%Vb(CdfHcbqvxf0)F`6|oJcs=wG}1>&eT$~+*9Y( zM&C2I$Md@dj}d;us%$i9tJZnlxb1q@l{2+#B%}UZdN|-f&j^SDJ0co}pW)0)rj*RkZ5MkCZ9#5K6 z2$vIPeQgZSnjg18^J~(qC(L#jHkmegslSVHxP55CO8?dqxI@0A>pOw`x2e&5 z2WOo9@96ri;lJy#gdK`criMK;kpDk5nlmM+$C(}%wF=u?!_L(IS~X#3QrMaL+akl8 zcX|kxc%98s;9|tvA9mc911!A!@9If}&jP;0ik1?-f$;r=_XS>I^^Bh;%|^nkVf+bc z+F@{WKCp)I-fhr~Ce1{`Y^NlKXUzqqxsotzR>ttGd7Lya5oXPYF+6M9Pl)noH^Qu8 zJd8AXgjvJ*G}6o=yofOCZ;att^Q$&!o+r&4gxL<`f0Cy2(NUhThH(yQ4k0|5Fze5X z;aRhUG}jSkO&neoqhBAxzY)X#BZlXAcPTV|vb1;MU7zf*%sCD2pzF5|_a`2YQ&>&M z0Jl0k#+29M4E+t7gv|lB-14Q^{~ht}10USh7g%`tzs}PvJ{!DApW8Qhvp%QB2g`9DqLPrEd@>2Z`l9n&J5 zkrw4oMp~3Vod|bM!&0EN(}i%?v`L=tmL{W?#4GtT6Q7?b?wKb4<7xcqN&fUq%kp7& zC)_7(56|zBHo)V((r`P(+Sw=V7?1m=eb3|Uv~rL8rCsFlzG-zH_fPwY$2n|71}u-X}!CjU=r?2IHkBPr}?!eb~O z#-p(mp=z2cAEY(tcFheV)%xd(`6zX>VIB`S1}w z&#M2&q{;uNns|>P|BoS`ClQ{UHiPV31N>*Tb7GqO@2atLBH1}Ht&7#?2d5JLPTH=X z|85!{JFsETNRz(B*f}$8s>jo)J`_`Zm`-_8LiM36?Oq#J()$QLe^Wop)8zlwjs0@+ zvz&aJm9~qIceXDhu_JzdrgrA0$^Xe3J9Ej-T(VO|xH_%U+7vrCSzLVdG5|)0(@PqX|6$TH%jN9?U zf{Q)Q4Qf3ek%VvX{Kz1}qx84f#^!|33FJ+2em)uOAFA`LmbBk`IT%&LY@!+hSnh^T4kn{3Ni;hnER& zA-oXSxyS#xO&&zhf-=03u#Vf+jz`sqdmO`ksNj zr^4V~f&8vnu$1ozi=`Y_jmyJ#?##+LWx1ur=a!dN=T51rJiDx#H?IQ!hF$u# z>5GhOv~w$~%NHaC*mf4BEBZ)^v#!c5CNS#wdo*EiT`#deBNgr?_Tj z8{E_;%*?gJGMp=c*J?V|pyt~8Au~n>QOlQ35=YVr>~MdneMRn!8MEhA z&)nIQL=|*rPm-c3@9atRYT!S)Cn+j2ZM|q-#q7$GS!Jctr5NfP_91iU)s*b)L3Z{a z$(O@Xe(ao!L|3N&$vud)^KaNyVA4@JuV!aw5jBpT(~X_ejihD7sA~L2U%2eh3P;V% zs~UG%CcpVQV$PaXMEIaghN7M7XY z=&xEy5#rleQ;<3~XHId&_PEkY-+E0zU2aXMSY}XV3jV*NU#u>xIeX6B)>)B)$IXLn zFHWVppr_{jZ7!?nt%S(7=4#SvNZg_W^TjolbIQ$9dXnoi=GBy$6<$`m)+ikE%rsh= z9~2i=pIbavE-!%Q;?@Pw4s{1*y8e&v@~X=JfOY>JOQS6D7Zu=J`#V~OZTp&QiVjUH zK+9VgTzb*MlSaP7^Ez3x&n-s%Xq|xVS?X=wBY$hHBSq)ZveQYb?a235x0ANfysG9^ z6q!6InlZ1Uq^7*GqFU>}=F1M}E&9N+D!Co$3QKN%xf;^Cchyw2rnhUDh^tEOCQnh3 z^`)q)Y{&1_%t6;wS&}O&pOt3mz@-ZDfBW^`8E>68Fx}zE8Rn;{` zm_UjQilXw0(y|3f2SxLAu2i;QZgE9v(abV4bt=TOXrtYmnHXp+t;+?hAV%G(xy^RHR88@6w+GRa;oJU zu-vmx>g8Hi;v$r?=7SUh!EcbSbBgCCW$L#&1Bk{4w4(o6*N*=Yg^j||lD7MAP|Q_j z{|N>CA5kcpl{-F})81@e#m?f;hbF&yhp1Vpt;C}pY9}(u)F-B9%+?MuX{^_it=AHj z{@Na!dcYJbiQ97lv#p%ko-61!tVg*)Ddpr!(%oW0-g2ml60T>qE5y!+rL=9Ndh%%- z*_HjkiOMRn0!gL2Evn%b4bZ1|o6?SJ|vGe}q|Hk{1Sjd#gL6J82 z9iI8A4cxCZwyMj@X6>A3M6-mQeaFte<9}n{ft9t2va?3YN*Oo(Z>bkKx2n9xEQ;-% zg6y1v_@5U>-KtLWcRrE*zu<{%Gi}fY{|!5j89N`sP9AG_&OMUzV`rz~pPZ1zlU6_Q z+QuSGEB$}h55lyeOJ|`V`Y{KX9bEp9g~QxAWphgAo|kmjXuF;jKu^}{naXc-C3HuZ zCQZ6_xO4Rnq-y!M`sMkzb(cRy=KWv2@&7;Wrs99+qIrr3Pqm*)!MCZkCS6ecx8C(m zeu@T#?tcZ#_Y32mu4&U_8bw9YXr<{FKnFZhI!BpxNW~I1P+u1E7-9Ou| z#}BlAyQdAKYo49m0zLYfqWSOa7VwyCo4bWs`oz5wELzFK_I>5e4o|N1c~2~!>t25H zQ!EcoC(S{(`AF!uIHIMP!fLDfCXaR{CvC@bN*}VHURm}3;YR{)zLVkyN17dYaIp28 zR!sNfe$-Sfp6+mB?W55}MboRy-4yT9_B^2wSDLcqVjpwhnMO1N(Rg>*d6R3oRmH6y zY;>6ujcM`O8S8vxB(?Jz?~XgKKWdU|!8v&ENK!*=)AW)G%3KUuOW%S+81lXaf1E&1Dsm-pYgZLq!3+ z@vPrW`~cE7$M9YINAV6O{Z+)@O#JmWGVzn`uOfaZ>DR>Yr|oA%Vn^QP?zX3hXFIPE zKirA2PsH#KSm(t4Eu_DGe{&!_+gU}tyrbT2Yhw7DhZu#}Ilzf!Z=Dui{+GdRvxz_0 zk=Q>^{21c1hnWMhGnV)>h#yD%OT^=^H2rp_s zk0gHXNOK_ieByIPMSKDA8;PGl{J;YvJ^q@-Z%+|_t7q|^+tKDg;{7G@U)f9)ekJkK zY-S062=Ob4KZ^Kwh{xX*_$}LJuGq1z>A9K2Ti2AojrgQ`MLb^V?zi8~Yz74e@D&-T08$|QCslfHKhUt=qo z=E$zsE%{l&f1ND1z3}0v~tJt}l^h=0;fcOUD*}nWwmuzC^1V!fSoftmL z7Dh|`K;lm#I|s(_(`+V+ooh*d6Y*^40pjI9LEP35!*8~U5&Ne)G4=ybGzY?8L;N!0 z+5XMMpGNw-V)%`xME0*H{ZQ+K#LIT_iT^I?r^N6Vo*vo1iu9im|7+rBpAqTV{u1IX zRN+-Ie9iYFJ9m=(NoPhp+bJd9LKV)9;ZL3(*}0AE4=suKV&Z2KZ-eUD+lZe=`bOf} zpJw7M)KKk9O<*Z6<`ZW{{Lc;4?`bn5p7jfeFC+bu82&2aZzp|rd1U`V;#U#>5b=3u zNBY&o2eTsnH^dhb|1j|xvm^Z@#7`l94e`yyR}h~!C$jS>@ym$k{JELp38u2xxPsH#yonu5|zk&4c z5YKkLB7Q#UGprM0hxsdr{{`vS6VLia;uny9a|}PX+K43HCrE!6@oeWQ;?F1jh8RA_ zCSL46PWlIkXFJame#bhnyL*e}HAtF311XZbYj7ZGoV zM;(7Jwi?+TXR+un!N=M8k>$mHE%Eop@Xryyg!CWA@F#@kgv7g)^i`IZc-hV}(o5Q% zpH~roDe-rboy&-SJcfUs_+_O3Acp^fc=pr$LtPNG^uNC(6kAJvTnsfXa@a1|JO0oZ{-?y(lm0gy#7b*_-nP8N%lX;8qslej-2h#@qbx7!T2B0g82$vZ za|P+oCOzlpBI2(k{rAZZ`}xZl{hx?u`&(n|49-Ag*d$#XZ=U5Pe>fkGCq3IQBcAQl zkRA5_;TZkLq`wMryZrAf4q_93WU0pS#}J=Qd@=DqB>uO=-$eYsiNB8cew}fEP2y#K zH1UIQ&iOMbhCjvf5)JF;$LL>*;dhq~5}VlJ{2v{|Pm1BETVB#N1Rv-B*~D{sEhV1) z|1t3c$<7MPw@mL{q-Q&C6MqZY>ChDi*u>5de4L;ASYG^RJ3~p&{4p{5B{BR;vcvJd zLi!ct&#~QbfKB{i{vzUU#X0ByiPE`alXP)8F0{PZIS?PGf0X#!i2rkpo%Wd!VG}!} z@o{!W6aRDKkF$JBe-@B_6!g9Dk*%Ki190r@-%dR1{}RK06~phFrO~MWZ0C5(xAe0% zMt>dg2a%um6MqQtyY-0Ty`A{V82+cktDhZA_}hu+{CS9Y(Yt*6OAOzkXXFp-`w;&N zvNN4{)-SQVw$S&o{$EEt=iBYXiyf!Ghj`XMNBj}w=La$T--+k?-&-aD*rYz^ z;p6gcU*g&R2;#ZDOeUW7Q;FyHat`sF&r2-dGCyx1y)4l=KOZNa<6TEQ$NM|tIlUhc z&vu%L=XTxAHrSSa=38F!b0ztC67g*RbmH01V&d7)#l*9nD~M-5*Al;i{5gJC9AFba zN8w|;-{4f@j{@)V`3vG@$=2~1yWs$v*f|&<#}6m|NaDv5KZf|@Vt997Ozd;KD@f1r zzDGRA`!VqxZ-?Ds7@OE<{#oKV-UasNndrHEA18i1&bfRzb`Kn26a5|dIQ}=pA4+^9 z@mwzL_k;+W*f|^@XD5qzZWl)qUqJdNiI@8w&dv+ObAE0nUi`7uDcE%{9AFcFn9nDE z9L_oWHN;OK{#xRXB>rc_bGh6b!>=R$XtL98ZyaC~KY5-olz1-3qlxEo{Fr#QGjJa@ zulDZ*bm={ic((sj;_o8;F?}OD`NTg%KYfUoda{=t2m29!0NI~HJnK&*p2xuw;<=tw63_MjOX4}7f6_ln z7svZ^;yK>+#B=@sJcgf`lWgDhw-?3mONlR}{JDyFPVX=Gi|k)d`Y&Sm+5wUN$E45N zKQ+JSz=*$(^o7L#iuk*TzmfR&iT@Gt`wqeZwlsX0{~qzz;hd|V?-9@G%^56KjGa@l zclw8jXMK0M2*K6~pC$M>{TR!OKbI2!TjDP#{__|+{f9z?t)-n+#Iv1She!HpfX<(1 zh%Y97U~Z(pf%tslrxSm~h)B=*a1HTikv?r?q@PKA4e{l~UnGqIo22(_e4PI)iJwLM z)5NbP{@>CmU=usD@o{zrA1JKh=V0&nLgF7Lex5X1Y+|PpAE&>Y_zL2mBYq9><3{5E zo7i~_A7`hE_{WL=hjfD2MBjjq(~lm518gnp+i@{`De-F&&e>UFd9nWl@edHs?d5gi ze@Oc7G6}#YcDUU=L;RgM=i(KuY#}~ZC^BEuEEZm-4DIdldg8CPSautTzlQim;;$t> zLA)L6mDv)*HxqB0zB0WnFT>D6i)EK>vGBDP%g%OJ`pls84{o!~MfuAdiM`Ec<(Cnk zXR+8{PJAKpR}w#kc-z$}a~kos%TZ<;@wQ1RGn06!KYkmIw};mR!%K^6ii6?PtE+?I z)#uHrDV~mfO_kl7qvGjcg5gzVvx{*~IE5y>LSqE)gw^-T7FSgjpBD^gjQ>3@FS7hu zm55e@DXN}nFFp#<@AIW3cRt0%>uRC)AVWI3Y2n#$B9|15s3?*b=Wi<1wqzfx=XtMD2z+L zYwO)C=fW?t;TPHP4PA^K_wB;Fw!% z`k`bHZ7s4R*cazoaw4B%8{PHuO<1N%;Kk#t(C%8sB}1x96D!y3 zZIp=~hLw0blh&pGP~wczeBYL9!jIX_97}j%TwE^v(Zm_0`F^dygm18mfINPPVQWsHZvPL@F_=OC literal 0 HcmV?d00001 From 6bf4df85dae1c0c58217a8d6d473ab2cf3545052 Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Sat, 5 Apr 2025 10:59:56 +0100 Subject: [PATCH 06/10] update --- 05/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/05/.gitignore b/05/.gitignore index 022bae2..317ef82 100644 --- a/05/.gitignore +++ b/05/.gitignore @@ -4,3 +4,4 @@ address_map.txt *.d tcc-bootstrap musl-bootstrap +musl-bootstrap-final From 4715742aa85106c1f5f9cf6c763d22d7913220d1 Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Mon, 14 Apr 2025 07:44:54 +0100 Subject: [PATCH 07/10] remove 06 to start packaging tcc and musl --- .helix/ignore | 13 + 05/Makefile | 7 +- 05/test | Bin 85112 -> 125656 bytes 05/test.c | 2 +- 06/.gitignore | 3 - 06/Makefile | 44 - 06/README.md | 18 - 06/deps/lpeglabel/.gitignore | 3 - 06/deps/lpeglabel/HISTORY | 32 - 06/deps/lpeglabel/LICENSE | 22 - 06/deps/lpeglabel/README.md | 727 - 06/deps/lpeglabel/examples/expRec.lua | 128 - 06/deps/lpeglabel/examples/expRecAut.lua | 122 - 06/deps/lpeglabel/examples/farthest.lua | 14 - 06/deps/lpeglabel/examples/listId1.lua | 33 - 06/deps/lpeglabel/examples/listId2.lua | 39 - 06/deps/lpeglabel/examples/listId2Rec2.lua | 67 - 06/deps/lpeglabel/examples/listId2Rec2Cap.lua | 79 - 06/deps/lpeglabel/examples/listIdRe1.lua | 30 - 06/deps/lpeglabel/examples/listIdRe2.lua | 71 - 06/deps/lpeglabel/examples/tiny.lua | 151 - 06/deps/lpeglabel/examples/typedlua/test.lua | 2572 ---- .../lpeglabel/examples/typedlua/tlerror.lua | 66 - .../lpeglabel/examples/typedlua/tllexer.lua | 105 - 06/deps/lpeglabel/examples/typedlua/tlp.lua | 20 - .../lpeglabel/examples/typedlua/tlparser.lua | 245 - 06/deps/lpeglabel/lpcap.c | 537 - 06/deps/lpeglabel/lpcap.h | 43 - 06/deps/lpeglabel/lpcode.c | 1035 -- 06/deps/lpeglabel/lpcode.h | 42 - 06/deps/lpeglabel/lpeglabel-logo.png | Bin 14115 -> 0 bytes 06/deps/lpeglabel/lpprint.c | 263 - 06/deps/lpeglabel/lpprint.h | 36 - 06/deps/lpeglabel/lptree.c | 1363 -- 06/deps/lpeglabel/lptree.h | 82 - 06/deps/lpeglabel/lptypes.h | 171 - 06/deps/lpeglabel/lpvm.c | 429 - 06/deps/lpeglabel/lpvm.h | 60 - 06/deps/lpeglabel/makefile | 57 - 06/deps/lpeglabel/relabel.lua | 399 - .../rockspecs/lpeglabel-0.12.2-1.rockspec | 32 - .../rockspecs/lpeglabel-0.12.2-2.rockspec | 32 - .../rockspecs/lpeglabel-1.0.0-1.rockspec | 32 - .../rockspecs/lpeglabel-1.1.0-1.rockspec | 32 - .../rockspecs/lpeglabel-1.2.0-1.rockspec | 32 - 06/deps/lpeglabel/test.lua | 1449 -- 06/deps/lpeglabel/testlabel.lua | 1054 -- 06/deps/lpeglabel/testrelabelparser.lua | 549 - 06/deps/lua-5.4.6/.gitignore | 5 - 06/deps/lua-5.4.6/Makefile | 106 - 06/deps/lua-5.4.6/README | 6 - 06/deps/lua-5.4.6/doc/contents.html | 678 - 06/deps/lua-5.4.6/doc/index.css | 21 - 06/deps/lua-5.4.6/doc/logo.gif | Bin 9893 -> 0 bytes 06/deps/lua-5.4.6/doc/lua.1 | 155 - 06/deps/lua-5.4.6/doc/lua.css | 161 - 06/deps/lua-5.4.6/doc/luac.1 | 118 - 06/deps/lua-5.4.6/doc/manual.css | 21 - 06/deps/lua-5.4.6/doc/manual.html | 12046 ---------------- 06/deps/lua-5.4.6/doc/osi-certified-72x60.png | Bin 3774 -> 0 bytes 06/deps/lua-5.4.6/doc/readme.html | 337 - 06/deps/lua-5.4.6/src/Makefile | 225 - 06/deps/lua-5.4.6/src/lapi.c | 1463 -- 06/deps/lua-5.4.6/src/lapi.h | 52 - 06/deps/lua-5.4.6/src/lauxlib.c | 1112 -- 06/deps/lua-5.4.6/src/lauxlib.h | 301 - 06/deps/lua-5.4.6/src/lbaselib.c | 549 - 06/deps/lua-5.4.6/src/lcode.c | 1871 --- 06/deps/lua-5.4.6/src/lcode.h | 104 - 06/deps/lua-5.4.6/src/lcorolib.c | 210 - 06/deps/lua-5.4.6/src/lctype.c | 64 - 06/deps/lua-5.4.6/src/lctype.h | 101 - 06/deps/lua-5.4.6/src/ldblib.c | 483 - 06/deps/lua-5.4.6/src/ldebug.c | 924 -- 06/deps/lua-5.4.6/src/ldebug.h | 63 - 06/deps/lua-5.4.6/src/ldo.c | 1024 -- 06/deps/lua-5.4.6/src/ldo.h | 88 - 06/deps/lua-5.4.6/src/ldump.c | 230 - 06/deps/lua-5.4.6/src/lfunc.c | 294 - 06/deps/lua-5.4.6/src/lfunc.h | 64 - 06/deps/lua-5.4.6/src/lgc.c | 1739 --- 06/deps/lua-5.4.6/src/lgc.h | 202 - 06/deps/lua-5.4.6/src/linit.c | 65 - 06/deps/lua-5.4.6/src/liolib.c | 828 -- 06/deps/lua-5.4.6/src/ljumptab.h | 112 - 06/deps/lua-5.4.6/src/llex.c | 581 - 06/deps/lua-5.4.6/src/llex.h | 91 - 06/deps/lua-5.4.6/src/llimits.h | 380 - 06/deps/lua-5.4.6/src/lmathlib.c | 764 - 06/deps/lua-5.4.6/src/lmem.c | 215 - 06/deps/lua-5.4.6/src/lmem.h | 93 - 06/deps/lua-5.4.6/src/loadlib.c | 767 - 06/deps/lua-5.4.6/src/lobject.c | 602 - 06/deps/lua-5.4.6/src/lobject.h | 815 -- 06/deps/lua-5.4.6/src/lopcodes.c | 104 - 06/deps/lua-5.4.6/src/lopcodes.h | 405 - 06/deps/lua-5.4.6/src/lopnames.h | 103 - 06/deps/lua-5.4.6/src/loslib.c | 428 - 06/deps/lua-5.4.6/src/lparser.c | 1967 --- 06/deps/lua-5.4.6/src/lparser.h | 171 - 06/deps/lua-5.4.6/src/lprefix.h | 45 - 06/deps/lua-5.4.6/src/lstate.c | 445 - 06/deps/lua-5.4.6/src/lstate.h | 409 - 06/deps/lua-5.4.6/src/lstring.c | 273 - 06/deps/lua-5.4.6/src/lstring.h | 57 - 06/deps/lua-5.4.6/src/lstrlib.c | 1874 --- 06/deps/lua-5.4.6/src/ltable.c | 980 -- 06/deps/lua-5.4.6/src/ltable.h | 65 - 06/deps/lua-5.4.6/src/ltablib.c | 430 - 06/deps/lua-5.4.6/src/ltm.c | 271 - 06/deps/lua-5.4.6/src/ltm.h | 104 - 06/deps/lua-5.4.6/src/lua.c | 679 - 06/deps/lua-5.4.6/src/lua.h | 523 - 06/deps/lua-5.4.6/src/lua.hpp | 9 - 06/deps/lua-5.4.6/src/luac.c | 723 - 06/deps/lua-5.4.6/src/luaconf.h | 793 - 06/deps/lua-5.4.6/src/lualib.h | 52 - 06/deps/lua-5.4.6/src/lundump.c | 335 - 06/deps/lua-5.4.6/src/lundump.h | 36 - 06/deps/lua-5.4.6/src/lutf8lib.c | 291 - 06/deps/lua-5.4.6/src/lvm.c | 1901 --- 06/deps/lua-5.4.6/src/lvm.h | 141 - 06/deps/lua-5.4.6/src/lzio.c | 68 - 06/deps/lua-5.4.6/src/lzio.h | 66 - 06/deps/parser-gen/LICENSE | 20 - 06/deps/parser-gen/README.md | 397 - 06/deps/parser-gen/equals.lua | 32 - 06/deps/parser-gen/errorgen.lua | 401 - 06/deps/parser-gen/parser-gen-tests.lua | 149 - 06/deps/parser-gen/parser-gen.lua | 563 - .../parser-gen/parsers/lua-parser-tests.lua | 799 - 06/deps/parser-gen/parsers/lua-parser.lua | 291 - 06/deps/parser-gen/parsers/tiny-parser.lua | 55 - 06/deps/parser-gen/peg-parser-tests.lua | 260 - 06/deps/parser-gen/peg-parser.lua | 336 - .../rockspecs/parser-gen-1.0.rockspec | 27 - .../rockspecs/parser-gen-1.1.rockspec | 27 - .../rockspecs/parser-gen-1.2.rockspec | 27 - 06/deps/parser-gen/stack.lua | 66 - 06/src/ast.lua | 32 - 06/src/main.c | 24 - 06/src/main.lua | 24 - 06/src/util.lua | 34 - 06/test.zig | 1 - 06/zig.peg | 516 - Makefile | 2 - scripts/run.sh | 2 +- scripts/script1.sh | 6 + tofix/lua-5.3.4-tests/all.lua | 292 - tofix/lua-5.3.4-tests/api.lua | 1173 -- tofix/lua-5.3.4-tests/attrib.lua | 471 - tofix/lua-5.3.4-tests/big.lua | 82 - tofix/lua-5.3.4-tests/bitwise.lua | 328 - tofix/lua-5.3.4-tests/calls.lua | 401 - tofix/lua-5.3.4-tests/closure.lua | 247 - tofix/lua-5.3.4-tests/code.lua | 239 - tofix/lua-5.3.4-tests/constructs.lua | 313 - tofix/lua-5.3.4-tests/coroutine.lua | 874 -- tofix/lua-5.3.4-tests/db.lua | 857 -- tofix/lua-5.3.4-tests/errors.lua | 537 - tofix/lua-5.3.4-tests/events.lua | 456 - tofix/lua-5.3.4-tests/files.lua | 793 - tofix/lua-5.3.4-tests/gc.lua | 624 - tofix/lua-5.3.4-tests/goto.lua | 232 - tofix/lua-5.3.4-tests/literals.lua | 302 - tofix/lua-5.3.4-tests/locals.lua | 162 - tofix/lua-5.3.4-tests/main.lua | 381 - tofix/lua-5.3.4-tests/math.lua | 824 -- tofix/lua-5.3.4-tests/nextvar.lua | 631 - tofix/lua-5.3.4-tests/pm.lua | 374 - tofix/lua-5.3.4-tests/sort.lua | 310 - tofix/lua-5.3.4-tests/strings.lua | 379 - tofix/lua-5.3.4-tests/tpack.lua | 322 - tofix/lua-5.3.4-tests/utf8.lua | 210 - tofix/lua-5.3.4-tests/vararg.lua | 142 - tofix/lua-5.3.4-tests/verybig.lua | 152 - 176 files changed, 27 insertions(+), 72392 deletions(-) create mode 100644 .helix/ignore delete mode 100644 06/.gitignore delete mode 100644 06/Makefile delete mode 100644 06/README.md delete mode 100644 06/deps/lpeglabel/.gitignore delete mode 100644 06/deps/lpeglabel/HISTORY delete mode 100644 06/deps/lpeglabel/LICENSE delete mode 100644 06/deps/lpeglabel/README.md delete mode 100644 06/deps/lpeglabel/examples/expRec.lua delete mode 100644 06/deps/lpeglabel/examples/expRecAut.lua delete mode 100644 06/deps/lpeglabel/examples/farthest.lua delete mode 100644 06/deps/lpeglabel/examples/listId1.lua delete mode 100644 06/deps/lpeglabel/examples/listId2.lua delete mode 100644 06/deps/lpeglabel/examples/listId2Rec2.lua delete mode 100644 06/deps/lpeglabel/examples/listId2Rec2Cap.lua delete mode 100644 06/deps/lpeglabel/examples/listIdRe1.lua delete mode 100644 06/deps/lpeglabel/examples/listIdRe2.lua delete mode 100644 06/deps/lpeglabel/examples/tiny.lua delete mode 100755 06/deps/lpeglabel/examples/typedlua/test.lua delete mode 100644 06/deps/lpeglabel/examples/typedlua/tlerror.lua delete mode 100644 06/deps/lpeglabel/examples/typedlua/tllexer.lua delete mode 100644 06/deps/lpeglabel/examples/typedlua/tlp.lua delete mode 100644 06/deps/lpeglabel/examples/typedlua/tlparser.lua delete mode 100644 06/deps/lpeglabel/lpcap.c delete mode 100644 06/deps/lpeglabel/lpcap.h delete mode 100644 06/deps/lpeglabel/lpcode.c delete mode 100644 06/deps/lpeglabel/lpcode.h delete mode 100644 06/deps/lpeglabel/lpeglabel-logo.png delete mode 100644 06/deps/lpeglabel/lpprint.c delete mode 100644 06/deps/lpeglabel/lpprint.h delete mode 100644 06/deps/lpeglabel/lptree.c delete mode 100644 06/deps/lpeglabel/lptree.h delete mode 100644 06/deps/lpeglabel/lptypes.h delete mode 100644 06/deps/lpeglabel/lpvm.c delete mode 100644 06/deps/lpeglabel/lpvm.h delete mode 100644 06/deps/lpeglabel/makefile delete mode 100644 06/deps/lpeglabel/relabel.lua delete mode 100644 06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec delete mode 100644 06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec delete mode 100644 06/deps/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec delete mode 100644 06/deps/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec delete mode 100644 06/deps/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec delete mode 100755 06/deps/lpeglabel/test.lua delete mode 100644 06/deps/lpeglabel/testlabel.lua delete mode 100644 06/deps/lpeglabel/testrelabelparser.lua delete mode 100644 06/deps/lua-5.4.6/.gitignore delete mode 100644 06/deps/lua-5.4.6/Makefile delete mode 100644 06/deps/lua-5.4.6/README delete mode 100644 06/deps/lua-5.4.6/doc/contents.html delete mode 100644 06/deps/lua-5.4.6/doc/index.css delete mode 100644 06/deps/lua-5.4.6/doc/logo.gif delete mode 100644 06/deps/lua-5.4.6/doc/lua.1 delete mode 100644 06/deps/lua-5.4.6/doc/lua.css delete mode 100644 06/deps/lua-5.4.6/doc/luac.1 delete mode 100644 06/deps/lua-5.4.6/doc/manual.css delete mode 100644 06/deps/lua-5.4.6/doc/manual.html delete mode 100644 06/deps/lua-5.4.6/doc/osi-certified-72x60.png delete mode 100644 06/deps/lua-5.4.6/doc/readme.html delete mode 100644 06/deps/lua-5.4.6/src/Makefile delete mode 100644 06/deps/lua-5.4.6/src/lapi.c delete mode 100644 06/deps/lua-5.4.6/src/lapi.h delete mode 100644 06/deps/lua-5.4.6/src/lauxlib.c delete mode 100644 06/deps/lua-5.4.6/src/lauxlib.h delete mode 100644 06/deps/lua-5.4.6/src/lbaselib.c delete mode 100644 06/deps/lua-5.4.6/src/lcode.c delete mode 100644 06/deps/lua-5.4.6/src/lcode.h delete mode 100644 06/deps/lua-5.4.6/src/lcorolib.c delete mode 100644 06/deps/lua-5.4.6/src/lctype.c delete mode 100644 06/deps/lua-5.4.6/src/lctype.h delete mode 100644 06/deps/lua-5.4.6/src/ldblib.c delete mode 100644 06/deps/lua-5.4.6/src/ldebug.c delete mode 100644 06/deps/lua-5.4.6/src/ldebug.h delete mode 100644 06/deps/lua-5.4.6/src/ldo.c delete mode 100644 06/deps/lua-5.4.6/src/ldo.h delete mode 100644 06/deps/lua-5.4.6/src/ldump.c delete mode 100644 06/deps/lua-5.4.6/src/lfunc.c delete mode 100644 06/deps/lua-5.4.6/src/lfunc.h delete mode 100644 06/deps/lua-5.4.6/src/lgc.c delete mode 100644 06/deps/lua-5.4.6/src/lgc.h delete mode 100644 06/deps/lua-5.4.6/src/linit.c delete mode 100644 06/deps/lua-5.4.6/src/liolib.c delete mode 100644 06/deps/lua-5.4.6/src/ljumptab.h delete mode 100644 06/deps/lua-5.4.6/src/llex.c delete mode 100644 06/deps/lua-5.4.6/src/llex.h delete mode 100644 06/deps/lua-5.4.6/src/llimits.h delete mode 100644 06/deps/lua-5.4.6/src/lmathlib.c delete mode 100644 06/deps/lua-5.4.6/src/lmem.c delete mode 100644 06/deps/lua-5.4.6/src/lmem.h delete mode 100644 06/deps/lua-5.4.6/src/loadlib.c delete mode 100644 06/deps/lua-5.4.6/src/lobject.c delete mode 100644 06/deps/lua-5.4.6/src/lobject.h delete mode 100644 06/deps/lua-5.4.6/src/lopcodes.c delete mode 100644 06/deps/lua-5.4.6/src/lopcodes.h delete mode 100644 06/deps/lua-5.4.6/src/lopnames.h delete mode 100644 06/deps/lua-5.4.6/src/loslib.c delete mode 100644 06/deps/lua-5.4.6/src/lparser.c delete mode 100644 06/deps/lua-5.4.6/src/lparser.h delete mode 100644 06/deps/lua-5.4.6/src/lprefix.h delete mode 100644 06/deps/lua-5.4.6/src/lstate.c delete mode 100644 06/deps/lua-5.4.6/src/lstate.h delete mode 100644 06/deps/lua-5.4.6/src/lstring.c delete mode 100644 06/deps/lua-5.4.6/src/lstring.h delete mode 100644 06/deps/lua-5.4.6/src/lstrlib.c delete mode 100644 06/deps/lua-5.4.6/src/ltable.c delete mode 100644 06/deps/lua-5.4.6/src/ltable.h delete mode 100644 06/deps/lua-5.4.6/src/ltablib.c delete mode 100644 06/deps/lua-5.4.6/src/ltm.c delete mode 100644 06/deps/lua-5.4.6/src/ltm.h delete mode 100644 06/deps/lua-5.4.6/src/lua.c delete mode 100644 06/deps/lua-5.4.6/src/lua.h delete mode 100644 06/deps/lua-5.4.6/src/lua.hpp delete mode 100644 06/deps/lua-5.4.6/src/luac.c delete mode 100644 06/deps/lua-5.4.6/src/luaconf.h delete mode 100644 06/deps/lua-5.4.6/src/lualib.h delete mode 100644 06/deps/lua-5.4.6/src/lundump.c delete mode 100644 06/deps/lua-5.4.6/src/lundump.h delete mode 100644 06/deps/lua-5.4.6/src/lutf8lib.c delete mode 100644 06/deps/lua-5.4.6/src/lvm.c delete mode 100644 06/deps/lua-5.4.6/src/lvm.h delete mode 100644 06/deps/lua-5.4.6/src/lzio.c delete mode 100644 06/deps/lua-5.4.6/src/lzio.h delete mode 100644 06/deps/parser-gen/LICENSE delete mode 100644 06/deps/parser-gen/README.md delete mode 100644 06/deps/parser-gen/equals.lua delete mode 100644 06/deps/parser-gen/errorgen.lua delete mode 100644 06/deps/parser-gen/parser-gen-tests.lua delete mode 100644 06/deps/parser-gen/parser-gen.lua delete mode 100644 06/deps/parser-gen/parsers/lua-parser-tests.lua delete mode 100644 06/deps/parser-gen/parsers/lua-parser.lua delete mode 100644 06/deps/parser-gen/parsers/tiny-parser.lua delete mode 100644 06/deps/parser-gen/peg-parser-tests.lua delete mode 100644 06/deps/parser-gen/peg-parser.lua delete mode 100644 06/deps/parser-gen/rockspecs/parser-gen-1.0.rockspec delete mode 100644 06/deps/parser-gen/rockspecs/parser-gen-1.1.rockspec delete mode 100644 06/deps/parser-gen/rockspecs/parser-gen-1.2.rockspec delete mode 100644 06/deps/parser-gen/stack.lua delete mode 100644 06/src/ast.lua delete mode 100644 06/src/main.c delete mode 100644 06/src/main.lua delete mode 100644 06/src/util.lua delete mode 100644 06/test.zig delete mode 100644 06/zig.peg create mode 100755 scripts/script1.sh delete mode 100644 tofix/lua-5.3.4-tests/all.lua delete mode 100644 tofix/lua-5.3.4-tests/api.lua delete mode 100644 tofix/lua-5.3.4-tests/attrib.lua delete mode 100644 tofix/lua-5.3.4-tests/big.lua delete mode 100644 tofix/lua-5.3.4-tests/bitwise.lua delete mode 100644 tofix/lua-5.3.4-tests/calls.lua delete mode 100644 tofix/lua-5.3.4-tests/closure.lua delete mode 100644 tofix/lua-5.3.4-tests/code.lua delete mode 100644 tofix/lua-5.3.4-tests/constructs.lua delete mode 100644 tofix/lua-5.3.4-tests/coroutine.lua delete mode 100644 tofix/lua-5.3.4-tests/db.lua delete mode 100644 tofix/lua-5.3.4-tests/errors.lua delete mode 100644 tofix/lua-5.3.4-tests/events.lua delete mode 100644 tofix/lua-5.3.4-tests/files.lua delete mode 100644 tofix/lua-5.3.4-tests/gc.lua delete mode 100644 tofix/lua-5.3.4-tests/goto.lua delete mode 100644 tofix/lua-5.3.4-tests/literals.lua delete mode 100644 tofix/lua-5.3.4-tests/locals.lua delete mode 100644 tofix/lua-5.3.4-tests/main.lua delete mode 100644 tofix/lua-5.3.4-tests/math.lua delete mode 100644 tofix/lua-5.3.4-tests/nextvar.lua delete mode 100644 tofix/lua-5.3.4-tests/pm.lua delete mode 100644 tofix/lua-5.3.4-tests/sort.lua delete mode 100644 tofix/lua-5.3.4-tests/strings.lua delete mode 100644 tofix/lua-5.3.4-tests/tpack.lua delete mode 100644 tofix/lua-5.3.4-tests/utf8.lua delete mode 100644 tofix/lua-5.3.4-tests/vararg.lua delete mode 100644 tofix/lua-5.3.4-tests/verybig.lua diff --git a/.helix/ignore b/.helix/ignore new file mode 100644 index 0000000..369e75e --- /dev/null +++ b/.helix/ignore @@ -0,0 +1,13 @@ +00/* +01/* +02/* +03/* +04/* +05/* +tofix +04a +06/deps/* +06/zig +06/exercises +*.dot + diff --git a/05/Makefile b/05/Makefile index e6f0c16..f64768d 100644 --- a/05/Makefile +++ b/05/Makefile @@ -22,7 +22,9 @@ $(TCCDIR)/lib/libtcc1.a: $(TCC0) $(TCCDIR)/lib/*.[cS] $(TCC0) -c $(TCCDIR)/lib/libtcc1.c -o $(TCCDIR)/lib/libtcc1.o $(TCC0) -ar $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/lib/*.o musl: tcc-files - mkdir -p musl-boostrap + mkdir -p musl-boostrap/include + mkdir -p musl-boostrap/bin + mkdir -p musl-boostrap/lib $(MAKE) -j8 -C musl-0.6.0 $(MAKE) -C musl-0.6.0 install tcc-files: $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/include/*.h @@ -33,6 +35,9 @@ $(TCC): $(TCC0) musl cd $(TCCDIR) && ./tcc0 -nostdinc -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include tcc.c ../musl-bootstrap/lib/*.[oa] -o tcc tcc: $(TCC) musl-final: tcc + mkdir -p musl-boostrap-final/include + mkdir -p musl-boostrap-final/bin + mkdir -p musl-boostrap-final/lib $(MAKE) -j8 -C musl-final $(MAKE) -C musl-final install testapp: tcc musl-final diff --git a/05/test b/05/test index f135ccf835b39e5def416bc3d48f2d83b5b8afa0..3214e1008bcd2d40d0147e67f2af07d21409e1b7 100755 GIT binary patch literal 125656 zcmdqK3w%`7)joU<8VD#(@QM|6P*gyCvd+oK?zMM<0E}3)65a0LQXNY&a$D>VNv53sK=iTzxkW=i<^oHR4D6dGk z1;TV+E`|AB({AMM*Ylzxpthm-@8oOR=ZoC^_U$Z(-n!A|*QJ|Rn)pj;+q)E&rV3t* zMZMC*`VJnZ?=JK_K^q>&$!UL|op|oT(!`skvA6r?o>exjvvk^q()H`tq2Oue-1{%4 zvigbvOA}Z2mnIhXl_ut;OA}>1a}p(+<|N8CCQJHCD?3ZOdUhyHmZeMmuF_anAqd^2 z$%21gj{+pU(pdkNNc%2Sk}T<-lic@J$Y&>zpkyP?d6D)#%(;y?w}I!{9*;s|D4Vh< zn?SkCP*`_E=`oc3jF#@%iN~NCrF@cwORS%hcz#auf}Rt%yukbiKikiZmF?sS&RyM~UP?oPLLb_jY5I;J3en50J_WtI-wrok1d}QlJqBvfnFj?|Z<~E5`1(zY? zAPn!typjbqR)*YWp~*}D$0;wvFVR>qF}uL~-~>`0&XjGPg*-y)6e3=p@IS)kfuf4{ z&=~(fI^jcq|bO6Rj`lXFslqymVzx>H2sYnsCVD#T#+FDLh`(iR13@xOyFq z+4n8~f;Bi^8y?SFjpJLwcQ z=eNb?XC{AAC2)=>OH=XDB3z%|ldk9v5pLr`Lwm9*6Y;}AOUk#{gypv5F-@Sx<37{# z(UMS7u&P_>|GmV7!|RdI7&2xe<#b*h}qZr2x2;>Q^kj?O;v3@ zvyun-pte$mFov_D?^mG&%mSlgX)J?@1AGsmOOj=U#vWrDQ;#7oSr~{5G2Bpzk6{tv zQoI>~5|XyeI#x)u7Y3D_N|~D-+1WArajbJ#tfwEr83D}Up3L47emL5I;&;V9of^4z zIu#%v$mXT}a5BV9`&pPml!cK2^Xv(jfOKa*M2gHeZ8CJW9g~G^pR#O`*o-V`tg9%i z4xC%<=v11DbZ35wBpvKzC~O8l#*Q!uTB@{i14lLgDK}VXJI(7Ut$ZFvMj-M_eaz+4 zzVz|rydKOckaBD{s(=vX=4wjg1ZdotnTXud_u(rO#TVQ`Ty8T0c&y)#wC{^95bGZj zX}=ht_obj8lqTTppOhwE!sMwe@mN^`tBN_}3L^#d!3ahQ1`&L6{D^G{8_MX(mBEvx z9e*=Kao)Sw;T#2i%2*7_v5FD{1*Nchd7^`>C1zWQA)j+*3UxnC7Tm|cDHZ!~oXhOV z1j&M1C`}e3CJ&7v1}1ju{Zj{2pb@@EmV80_suK*eUedEUzMxUN-q@kkhhg>nn-L?k z3%-!l_0%tVAM8aBGpg952NK>uIf@3=4jW;#w{wTyr!rqh#ihptZXbn4Go}~pv4T}; zmGC0<+?VHQN?EXc+{We24|p0&jLaow1nz-!;0o$Gj0d#l*FJXxFxc5EsB%&y8(DgA z!GopOi3oeJ<$pt~ED8%|3JoiYw$6T1oV_mLfkZw4N=i5=n%xDd994NqtrL1-FtbUF@Sl z+Eg{C3A_lFAN4-c+lTg=KX^*a>M)0fa$4b$R#QSA(j&D$%K8;e)7HP`A6vFWRvZFO z`r(UF3+cN|BJ<1%3`9~#sppO&FXMuRI zD{>7-l=N)m_|VQk%mqL4$vlrASQGUGH}VUvwt(4J*QdY4qI{5+$ysx)HX70}F2(}% zkvE2!b!xLy%?O4_Roa!_0qQ_vKbMK7otw@EQ{oX$!K&EN=)?do%pP-ynN3Mg?=v{x z6Zn<*AqAls6NSBxP`wEX&^n+U3SPM?s~3nz%hy?;=Z?>Dgbs1ZqfA{m&)N}1=@a7Y z#N+mti&+V8whLnaBk^*2SOo&qJwgAK-fA{8=H)BIDJ%s3!|rZ5m_7`J5W~o@BMT;m zEthppnK?A?vOvv2qM}XqP+q$-tL~F=#)jXAMg#LBK9CyuT|`K3EhXSf)*F6_F(MDU z#`VJNhlV6hDRgsWWm zS2$fzc7OGXECYm%zZFv?YzE2{hr$$tG%I~~efss?l<~v7ak492ApO92Hhn)Dxt7xa zR4;QJjn8_v^?PG5uee6mt{jju2e?yiYw^+foPl2@F|I4UM_alNF?8?(Z8gvFxr(cm z;*EaHjtR+;?;*OHg|`hJ>(e*ZPz(Iw=x)0s)*^b5CYENj zFn%jB37Nz;aKA@f`61X!J|ZZ%fx2}PmMNffTuEPMMz~bLRU8RW-$Pknbc+lJDcGUL z%6AhxSoL9pL?KBQd;=s-t>V(KBxQb$q6FPHRWQY>iC~(KNeL%p@oC`Mo*o99R6(Io zv9>ArEG}i@)&oM$ch6_Yv2&32u}nM!UyLTHfgPS;kmAR&t#yx{M8V+;3z~)$s9FqlS;z(ix9okmvF@8C>oyQjdLDUX*32sWrYpxQK`X;a5t>^ zaWt?@%-PuN2Zqh4+5y{2-3ISz82UP1#{iGd)iUaG+L?Y~K``o?`W$HL14o9RVPU{6 z*VtpxQf$nY#@_6~tUo)CDEV%&!}t*EERwVmYyoAbGR^=NPO>-!c%i2>)))37@qpi8 z0AMShhcR4&jVQzD%)l+W@r#ulmO$=^Cy|TGI5^3X-gPQe1V?Z52u=F4k;wVLT0u>y z3<(nGy|Fh?>tItGwqmgmz1OE7{!I3iWFfphJ@r%RagvS%%xdC?fGwwkk#d6!QJBE3 zPkTr%t|e<`tECrOdRz0@*Hkf2$zrNxW3x(jbH6Q_o&LDeooG_9y^Jxed1U{vCnz@> ze0_RUSng63(UhB0Qd{mB5N6JUV`Ue;m{~a22-d+pvHtOqR2XCyqK~6cvCbWmWj(N4 zYK|xd?Sx2sNIQ|Cz_tvVGJ$0zcf06HjO6`Z>keMz+Au*85}5t>{Hbt#2TD3w$6nZp z1wgnCb(pchxP$T^?D!*DpSZb9Lb`F-tvnwNX*W|Th0ii((}$H595|&ocDGp&TP~$I z355wRlPN_Dp_+j|BRkf99%^p4BsN-7wp1sRMhkp>s&1Mr`=pU&8qpOcK$d8a6%sgY zn1e6po$Rx?EZ(Y+#t<=*t$XuW& z9H(X{x+Ip)PIPu~DB(PO2l?eZ9IlHMCJyC;QrMyKmu59%4TuY*@tpE-D-G#sU4&yb ziGbT;=Vr8EuQ+yx;j?x0#(8Z%NK$IXjIGUcR!dA+k9F0lQNfI=i3w(&Qzs^x{XfM4DcY;#~A0 zR}r$a7TS>O&eX{0QrN=IB z4L?=k)ILhbFV~x8bPM}`4T{!^Yc7sr)N9wDMhar#|Cz5NM^bJHI?wHEeO>{N5 z1LgzfvLu|=9j=AewH#AmyGZODRoetDMGqC9J>H#RyiAm=L9#Vj%?91F>plyE8cxFI z>(k>+rMfe`N@Fq_B%ZJc@DN-ih3mwJTRUblsF;Lu_z2UQ+}4@4gp1o~VtVFPSiGTF z=nDAHOU?14jUFg3TL;BLi`>wex2Nvt^1aj;losqT3vV<88v_MQ&cime9f)Z0!pzCe z{H_mzDR%BbBHDQ)Yjw?uMtcvfvJ{a+igB0|25C2%v}|gmx4jzBXv~05GqNlg1~=G+ zlp9-=D99MgLP-MOXBUu;|CX62nexZndDE`9fOHEBndCIj#3$hOfTQhJW8~UZ~5+zTA2w4znr9hvuZ;7R8 zE!{CVxso#jItC)F>liSUvoiVzrR$JJIa&IVVd(ESX61rblpfg=W^AA|Q#x7lv=p)} zs{ou76mg9(7W*h~qM)B)guXDO48HD$%c0R?X`FRS@ih?PKfF;Uu(x#3bB)o_a%|(GX$GCT&p0HK+ez79tl5(v*s|DtR8-A4&HtGk1CO+kB%S#$ zbDsukiuq5X5XBE$2#2YHrHf4)jOh*9pfFk1&Fyu)m-iI*@@6%FifdZ2$54lN3eB}G zPC<(DB-kxT0QVgcPQ6eJXps3kq6_EmqQyvAMv;mwVN=|()6H2|a^%DHD8Se*wb-1< zCdnlxN>)S1YI=04;990ZOMH`xjQ8D*ss>Sm1Ah=TB9C+;#AIDyjk@!^i%$k8SWtYl zq_3}ZtRE=aI&t{6iKzB~%e(oMf}4OwlntN3t}7(wrPHvc<)*J*uobMzq;2KPhC_+$!9N3GnDoVN*AN$1*eWFr8k9f^4z zs0!}4!mPWb6DtS&;Lb#j@mUq)M1Y^LVm=B|8vA`w&|3dr7N+G*R~S=FcYOoV4fkzC zA^iWreH)42OvPSDY2v2w@{@}4C%BoTP=xE#vtP%42m>ct(f)?FDWu|-X1((=!LB+l~4U>BbW#OTn$tzUjZN7>XPm#RhT+C>P# z?HE~}hUKVyjDqy7lKS6Iw?LaxXcw^f}lWG>XX43?xaY+y*hg*h@%6wEHhmQ5bUX+z76iR&YA!`*<>C=1 zKxUI7tLs9_i^@^}R|)QhpMruQfdny`!lkA#7nYf$e0(g9pjtNCx!r~{+hjxitkF*ISMk5y5(t{SYk5dyABNi?mpQ7RNF zaY1(RYf>T1d{O*wkA|)ccGjtoGYgx2UvH}S?-2Ef2-7tVIh=cj^W7dMQagvpIM%;s zzBkdAts-BdmFP427@Nt!Ewdtldi8)`og6 zhaQFza|3PW%xpz#py-yZD7qz7G(Av+M1`3V*@{*}(aNnTS{W+Jyy8lPM1`3yx1eX6 zCrVlo@Rq@i^IJ)7uM5@P3U$fCt~ZLhJ}qJwPf|r;=He{1=FU8GJKk0_FO>e{T(?*> zx4psp0Y$UbQkWT;t$e;Dle_!2qWqjJSV&wuJ2X+ zhkh!0xpVW(S2tz)m@eo(G7zCnIWlz=;S|R{^ZQtEi7P1sS6KchH*MLnc|6o|6til- zkF?V1Ba!QQ%`Vm-iKHIKgg@55bL9F7BH1~TS}2m?k@okoEfeb>5ow=>-KbdqPLcLz z*_IOG$VmIWBHks^{&$Lb?^CRQ*U0rz3sTE5%EtPOBJB@|#!o7$_m;edqcAIO67U`ePR@>FZY!wyU(3xbih$w~SEFPSY*pI99us{#` z3Bqzp0-t=nC-OAn^@MK3Jw`Fi6R?hhRysCliH#Rg;BEAuIg4Owh zLURy6nN#si6q{YE`(UW73?eJ!g1^~wG#RI<5e4aeDO64yI7Ku=zt#5yRQKW;97&mo zcbfw6Ctr#iac-;TmXW6YIMhgPW|Pdi(!2+zzxQ<-?Or)_dfih0l2io zf2oSj40o&y$Sge|GYsEIoXgD4Y?I9_d@4f>nJOt_Ye*@$&xPTs?R~G6CdNTY|K{;Q z1Gnz443A}*>ltRND?W}Z9o$%^|49A%~%Zmh=_;$ApJCzQ&> zI`NE>VX@!ZTC;)!N>DWm$xs4L4X?i+JrKbo))N(#Qtq9pH&qZt#WL8dBQ64l{S5r|^ei$aKv2?0*9g>7|e8XZrvvj9V z2^8C+zVD#`DC$a~rdsinjPqNHO6fL=Ix$;g-_czr1mB@T@Gi8hGf)N-DcHF$Gj!j9 zeg$t97snE%3T|RO?6-|52c2|&8J(0zuWhiNw|UZe6lOQ2L}#l>ZqXOa#_fiKR7B!s z*k9f}X&O?p56l46qqlK|gCq?2>hz9ucA25Ea9%U5T`<9Jz-h{zkwRzIYIjHizEVkIeR-pF} zP-?|p&OCu6<0O;5#nd`33|^qG<`#%8gBqIlz0I^QEiHm%il--ZXJot;d&{fWqQbNO+ALj}lWD(0CCh#GSA1e_#b5EEu;AWMFKrjy` z#!I0&ynTgnzT;4QndeU@0hK0GLVoqMhHVe?ndAJ_LmqzH4!4JxV;pc983Lht&}4V?BlR_qg;C?lwA|*@l^nGpPHd-3Kb|#ni?sO^sFxRH4#4 zV%R1}hL6#xf=$5tesw7r;l&Yq3uF*Xrbxz>8(9u)1m@mxO_DI5{i8E<~3nxA6)WTba3ZA!lJ&7syf4zu24m`e6(hMM1#mguxgvR-VFu!K0w)npO+71lxo{7osI=dY;BxLNwvu2>+v9J^I4kUC1j&T(epGhkq(#cPX#^5&H zirbJTW9N=?PQnhJ^bsZ>L=b?vb|SSFN|Pz8ng_VzluWUVazQY@&SYxT#MhW35kBNS zbt~7(ZW)5<8_x0}VUsB5ys3Dmma}7P^;qW@cq%x!f$}iwvZsKpogru5E*+lDmY_TCLETOi^?4CgK;~lvg5*eJ+t& zX|l7vz@OwOLw(_$WQuk2pW^MgXf!MCPf8-(zm$mce1@BCXaF&tY8Tr8rXsGR2uYGj z1JIvc+ljXYok=)+oq?HQ)`(>z-&J$qolRP5G^-J8_5h7@+n7? zuAU(nu6RPWtW&Fr0>_af*Q@m1D>fgVclp?8V!VlNVNByYC)tb*A8Qfh7_{2eC~*fT zOo{jymedy5IxS`lUHwB!rNrRKQz?mf)DXpPg-J=r`zNP>kQt9JBa%h5v$vo2fD$j?eMUe3#H+3dc6hjMV;a%?ZMb7dQx)Q0;)Ek&g%x#O!XBe`I zj_Cr)81H!h?X|Mm>l$cBbF!_=D;~nZW+7W5(LJ!SXOC;#x1{?!{$jlBE{x= zS$MOCGS%2T>5CI6XF-kJ6xlGXyvrQv&4U6zG+ENLI|37pFySdit#k--3>fW%G{k8eR*;I#^~9jCBxLhcDqe!9)cNI^ULE zCJ8OE{x2e{4iN4p;o@E;_$$K)LXL!wL|Iy17Qx)oLwMS%HTT83wT{X5784zE>#__v zD7)L(B|S2<3Bt!rI0ah^Ke`w4zOVQ;0_A}2{Zkhx&1lT?&R{i>_yXgUwxHV?{}3T% z`Hb3l2!fM}~Jn`>FRcs7!D8UA~{lM%d+L{dXgyZ)AT?YSGik-p6k@#F?CuaDW z)W2h48Rjs;_}$@XvrTKy`ge>mcn;fd`QQBKnQfyL{@<@oc8f4R$X3Brb}thww!KwA zvX2Ra>)8(lLT}Kxx;0= z2???;PH=ln1lS(?!(Ta`n|-MP>t;Txm@>DQ=k8qDEwbLHpi9=$;qIAHYDW*yi zW-$O=V@G8c_2Co4T1k-oRXo)_D166-13ivYIYM_MD^V`?csb~jELS;WT3 zgn^MeIEZ6hbjvzDCURd1g)!UKE%ygt9C80}f5}O5eLZM|j~O&gfL`^^*8N^Kx?x$d zeFuu-Jc|{LivYI2gN>btck+DF{G?L3o+@XJ?Fqw63DVsL_7mX`Y{J3S^gf$eX^bJ~ z#gll|(;TJNv1D03i%lfTvfAs%ubHO8h%@L;`(U2`tr@wVe*v^ODfC=4h&efeY z=aMIxY#<4!<}wZ@!Ihn6oWfO8V_5zeUo+KGQ9Av%SM%qJ1vw_!``CWnxhw3l2C4!_ zq#KDNQkvHhA7%~%M#4n=7E?I>n8FR*LH-(&=HEeHO$FFs!cQ{JZy5|I4F`iL8&@|L zcitxqqW0h1K@P6|b1lo;F2pE!y-g_xz+_?MQ9qWR%A0#)4Br{!HNWvIB#g9j z1-6Q}+;`yk>SS3fTr0(MSigVUj1&Xj9m%y?df|b1)c)&0_SKfS{ms6BsnsZ8QCm@n zPFbtFb)GEt_^u?LLGdj&N?)WIjj8GMyVO zXXTxC;*7jEQg@TiSLkHa5w2x_ijE>D;eBa&QRz+D}Q@#dba zcCM-iweEwVBAY5%c0XU9cz^Go+^J6|M&ifQ^Rfyp^}KYDdR~yXo?JP0y@CBv+|}xL zi_<4?nM0exP_TD}TdlC*Dl`vp(N@MZtDrnt@<`YKwgk3Gk2B@L-a?%ByKUt*N1hHR zsb8$aKq(VwC@@!jq=^(L2rwl74Twqkpp4sGB1i+r|s+2iISWFH#q#*t_v zUf$6bLyR(J2-~XEntUE^c1e`nli_U4_N#krM_TDrWKN8IdbE8vkhI8TWP+y}6D93@ zw(+5_A}v4mHBJ|dkK*D!W;kqAJORkXD_d_!EPW#9cz##p0Ys;jNQ+JDSbt$;#SKu) zL%56CkP0jG7{aWa+9ML5iA<6ut4Lb~2eU70Sp!C9H1iL_mxNdOilTm*AF6cc8q9*o z^$6VE?_;lhB``Ck3hDtuI#Z{b`D*fY8D8bQLB^Yat z_`m~{Ggt;`((i7}YSkWGHXN+z)x41;W$%?nq)rvgo(Rig;e}F|HZu41nkd1|;6&qWjL(?ug!@b5YfuQOv7L)zcrY!1j0-)zLF)V;|`H)4*~{g z{(;o^Y}S>plq?s3S4v(2ATJDw_3zk{i1iO`xjEK9tmUrEuztGSJ#2B@BP`~O=$mY5 zs#0ls0MBm8kdEHcu?(WXe6j9ASjw3-iU#82Jy?+9+YFXiCUUT}K9e5uBKvsBMi|=O zanjBq-)QLS-;rg)yd5PsPAKio%Me(3usYuWggT`)z1KjMhZ@#uR1dw%Ug#60vCabL zMSiGt9iSP5omZ_ceF)j{gLeyIMJ-or#ZsW61ZAtubGfVSy^?e!#1YBQUD-wAV8$Yj zYwwjr&9m;6v<2h6l7#wN8S9)=-E012EGR~m{G_v+8))tc1tQ%S522%_DP5K8-=fz{ zfsk{bnXy=t!Sl_fWqbdPX%^uyVu?636S)Ux>V9qXd0X zBF-q?EMJ5z>BpMpHv7pn#@s3-5F0a z@UHlA!dst?VTxjAz~Ur~XS>5V$sq6Y~1Cq(tRlf-gszOTq5_OE?QQuS0@x zOXf>BqDh130RwO)R&tLl!`9KrRBVi_b_ehLqxe0d#?~vnhKci3THTGGTWLPYWh!kd zDn5MlPhMX;SS@>FdRfqEe1$)Zbk3rBWpLNCu*z@P=zSbzP?-*}vBx24w!;iR zfC5Y3Sv8M%J89Gw|So%7N=YQO6s;fY zKQYo?FW_YSffR5u4@inrBJF0tIyKUM8r|d$5uX!j{{@DA-qqWFlq9Q+w0|bg!c;}t z&*g+>i4YrH)(w%=yON^TWnCLdO$9O5UlVEn5SEVhUleJ72f#iivnbO3s%Wgo(9jR@ zIMLV`X;0%Q*54dye@o6Uj#LLQx51`$xOQYl;K zI7*6nzB+I1on?sgTlPvQ4U54wah5{vm3;Y!ESsPptC2_ol&IiQ;BABNME>^x6)TYlHQFVOffbM5S}JGAW+F!wI@)ERW3%zIKTyL>U7tTl5a zdx2hIhReczaLUde7&T4k;Qbxf-vqOnLpGF}bDQy>uADK~p_mY18w&>9J_OwRkWS}j zA~pLI#38l~blE#wb2VT}*)$i%&5+z@00yW|Ndzh0H`gsqoRrV+fJkguTk z_r9aFg^L#^6srW=RD6M*-lj4`8731l27u0$h6w3uDTb$To$(>`v2yS-5B3RPdM}42 zfgPnqMS27iq!{UN*R?GB;foM#G;Y<$B*J9ICMP=e-FQ(6M?y7%{gi}c!Kj0y`gPv2 zbIVni`O^<5SHIZb{9vk4dk1369+Ea-i9ys~OWH>Hs6M!27D34p@tx@?VYUyk^t@e= zay&I{pZ_-f^~!$S;mHRDdS2ZWd9WmnITB_GiIP7jOa2^AUK*ic^P~^Q;6|CO`$603 z8F;?PoOo?R)`%iSKqXhPIN_5+6k|S;DaKl7CnV>wEh3a>z6OzOW1#?T{VYr*&JMgE|9ZC)e~iRaWe`1@UAIX+Ej|8$N#Fo(^f zSPvd%}Ye;6M3KzNf5Hz4}l~Twc=nJ4-Mjj+=oPe#g(MHdtVcgDBUtZ z&EFy6E-bqLL%#jk*M4&j+XEe&Y z`->^pkC58-0~Z}tOk~=Q?}_9K&#@*myCoZ~#;|2MB|$n{8SV$UElvcFu;Fkqo#uBt z%4}Ge$?e%}iaao6rOzwwPSHA-k=d~ZSC#?H1}#Siaua@tta)EYas(yjL3^{syG zPawq56^*4svzqt)Shq{N%_JJpj8@nRvu5pg&S_ta0fTbxj7rHk^%mxe1LIk4xlwHM zr?Cd~Z@Uj@?Kp|egPp3};z|Q+JIY@u^Lt&NM4RUfRwR8{cr5_*% ztQ2tzAnxeoS!sP++G$i0#TQPcmCxc(m({$UKk`6XAD+qOwmjO}#>4qc?D~`GOK1UU zV>&vNB2P8?;bO_4e|^U6lm?r9^=5{_Fd5!pVs?v=p?orzbChR6c^yOZ?8OgpipT>+ z*a*d6e2b4ps+^YbQMz>RpKyUk{6>5(GtR1YDqE3`+2C0AurovD&xWK>6FwPKh7+UA z-)>olKf_Y`P%-%Y*awhhAeQ6AbMnF}z7cMC>2StaBq%g~9L#&9LSd}PG-255eTU_9 z%=dtL ze-9cS>*g5C9Va=$MB&O@1#=;wn^bn%3)zg1*ft5>k~231OitSnM$NRXU#4wOAuT`Q z(jVo;7C{LC=UvGX8Z2%&qtcefWl+J{+g}w){KX!ay(727lZMFweXM4w7s0 zw6%E}X5+b(vCLMH7E8F#-DJ zPhhRG*P#18ER9U;%p40=#@t#|(q|tlDzx*+&66^_a;Dhxnmm+>nI-Vcco6bKc@Q## zqIhkBG3%gq2#!)18 zRV$b>DvE>pvi``FZqazz4v`yi5#U|%VVU3YJ`Yd|3PJraaz6g92E~D0(nRU>?>TNr ztyNNK6;X>6XbDsYcU0OLV))L5siF_Y##>ux?3WN-T!hIr_XFUF{p$EWDzaZ)4d?14!vz1K?FAe8aSE0Gxk1 zs{!7X1~7eA8er3QHvp&N3`U|(+gnqvtiMg-Q-~*Ig49kw}fV20p32>HWv2oWd zVFq-Be$^4O=kLfBJ*D89J)-z&9nT{v6II|3cGN?Tze7ej{|*^?0NHCGwbKGOE=74J zyfx`vxIUMtsgZRfp;@j{i{2UF1?T1l=P+(^0mjV@d&keaGF`x>?RR^BjX&a*Dp*Fc z>hg2QwG-n$GpFSY1|yJ3ikZf{z0cFR8>AlY4W^;TNxqWq0DOm`8xsKbNLf5#HY1Ctm!Q?OxUFw1 zleqN&=Ye6|<&*-W38w2&&VTT#pYiq;#uKo&h%H8*G=GlSw#=&CQ4!i7?Z>p3>T|Y7 zUol5`9FH?A>qdBqm2S!&)A$?@dV0HLl5Z9K)+lhfNv!6*=%);5@G=_`{S= z$QFv)7QzB5Hr&*xFEi1U0Lufsh$Eg``) zG3Q@#Vc0;O0$D=*!DhT?n6)-{MPU(w%b)Ys!k%kTyEUe+IAmQ}I8+pWcyXQkGt1Cn zF8yBYvfs&7GRt9mt5uf*X>qZ)@)4T3Y{&&(U9oyFL~Ncx<%X7>Cdt7~wwByNGu zz(15=+7H`KbaDv+*5sQq-;p90e1f4@{h`OGsYp+-Gb|fFbffgVM7SsH&gbq4qeGdB zZ88;eotkfgvxRX~;~26H*ioc_a_?YrGIyL{#UzGi3Ra@+RROvmr6 zs01;g+9_d7o5YTYoE#A~BBLFj+sIb-~U_cn^1G6;fpQhXK(1cmW+2Vv*R% zmK+XQ7`*$s`iHi4cj6SEp}}xo))#x@gtiT>lt3eg2YxbNrN;dOjhmox1#b^w25GMc zGoyYlN}>2wCC*S?ZeBaLb%Sd2$~`$W)^kEzcWZDyw=5i>_{rSIuN(1YGRHH`$Q-lZ zNbD*I2g`TYw(dThGIh+&cjugZbI)(9TpalLk<0DrF`wY}{j+cgZ|>j}ogWNgrPXzH z4TnX~ZfL5j8jB(D#9_ruczKGoK%v}s`H*bWu+itt@oHYT1k;iK-^4ZNBH3H{%7%|NA_ZYs@E}l1hC{FI= z4Rws1UohNf#7H814e^FkJlw;#I`4S)*G;i_C|JIR8!^5O$VnuJoUOSJyXubo@`f8R zzV&&>GrwGJx(}y@o_q~AVtnJ@?vU=jJHH42WStk|4bBUs+5*l(D6J9yrxuzzZTgJE zk2vzEnMa>^(#f+*PMLIQG{|@>pmhuP-Us>A*4KEmPdmk{uc-GsaX`Y|S|i4$m5Uz}_SMAv=uv`k0-IcHC*- zUH3a=VSPj6r3<13LnFKGzQ#J&=9cA`U4Df(yRNSKqKdlcg5@pM(dOz)7gyI;R(mHl zU9@;nb$v^;VR1`yZB=!&s$o$@ZM}C!b@Sr7mS}xLOSGxFv8lQloQef?)!u24R76`^ zmV0xnn-v~5n%cVRXhT!9 zsVpSE?Hb*P!noy+W(c1bbQh6se zHC$3%A8o9K*=A2UJ&Kw)GzCpyYXTEhnB?9`thBW!%6aKx)Ff<7YiHvRVT;a2wyJ^8 zs$SM&%pvwJSyo-ihFnz9VzZxJzoeqBwknDy7Do&{vJPHgn!lm`BJV8t!J>-#;sI8N(bB1%hDzu8& z);L!~W#!@~@07*$#v|Bwnin@VHZh ztDK)xMMbo^1ngpY^rTUFQ@x0fQ6e|Dq5<&Ghq}sJgjd+g+z<`NzEch z1)5uG>rD``_6}XI%o;*s0vk9e%hDzsid7^`SY^R!u}9DjS>n*rwq%Sd(pAGV*>Nq9 zGuq_lR8z6276Bl;4PAd6?4)#kYf|szih3z3qUuEq4G7YRN$4E5N1j&Qvb3S;lCbB8 z$BXOH*p&-SjJD#criR8wB(Ds^&4TL63d92Xl0+V~M@`Gpil%DBJw_JItylmTr28yb zTvG#wF>y9@BWo~(A)5ibBb2csq1@IC!w70qgP?0I7j0g+xJ9B^Xk-<(8k-s}VklDq zxxQg3Jt{bJW=jRejMGjz({$m574=mZrp`czKtv3~+&30CV92OmR#{zLWuh3uYwhAi z@R!B)Ro>a+KvpCJiwjH=C8IVn1Vi+oAbrz(k>Bzi^Sd@~e%IM~`x<*Z^LER#$Ne{( zWAA(Bx6uBUTAsI+&kk6!t;?CnaXqr5_x_gLWSpq_SR$;k<&hb-B~j0&96j8j8%)nc zN#lE;mZM1-x)#;=EHo8h8DX+`5g=DVC6p?a_VvXX*hNt z>RC3t*d5AmeI7kLN&e~)#lmx6-_67S8RWEEkF%RK+|NZ`;JNi`k5h!-!SuhHM?aI1 z-zP0nSR$jKdk(XQk z%T|7mmG5iiZ&~?W;|)J*E8hj#QNT5<B3AzFo`(Oj zmG1*N{bZ^gpIrM!t^DvD`FN}6XQPdtDqEhzt$e4wjr=Jq|EiTgY3nt?%1^fP7cKv0 zD=)M1OTJ?CJZ$A>Tlv9uqu~d({N+~OHOBCtvHU74|3^-~JWW4rwC!l=-V@+&&2Nh) z7CWl^_H2Vc_0VkoKexf}J}jHR3wHmuR-T!YviXN>gYWhorSSOr>c7dKvkm^nobuG< z;a?3o{nPOebOpjM+r{mhI`Or6^zc0S^QRO$s{H;a5B~wkIbT|5=V=?Q|NPdL$LaYQ z+KHbhtbEwv#d3V4eB-LCl^rRd`DZJQC9w^9QmGBz9~n(kCp#9M;^8E9xHeDJjlxXa^w@O+*>(c zO5*rath^{kez=uKbL2-``P3Zw@m5}(BR|>7=jO;ut$cotyv)iQbL3}Od25dRY%5=# zBR}8D*W}16tbARLyvE8qbL5v;`NkZ1qm}pM$QN6ApOs^}BA?5w{DU0%)sVBl&pW$V zc(sP^U1Q~sTKUg*H79PcdOph0^F1s7*vdy(J^W!D*my~?l6c+cPlNKAkn};v9@^@N{+|92pxAHwL-^o3EXoqvGoX7mM z<;j=l#BMqKoAU5~2|43iG^c%kW93tG9X>(a^z1~`8ihZ z=Gz;re14An8OUkRO_vpmUKi*8$MPF3-}x;`w8PrV4PO%B>o{aInE&%U`F%P3`FZ$@ zA*bD1ZNBqt2A5m;i&jp5av$pNyrS40%I}Ii`gu~ylf%Dh8~l}57K>i@b>BAlJvsbm zw!vSwB3u6_dH4|HqGY&lbFeKCuUk^?*6E|{JYh2AMUdz2&m3X-o?i@vwzK@@z@sof zcl%MykDqq@r`+vNm5LPC{_)Fv3x^boq+h;0@2B{s+^xGWweoxLI|}XV_P3n=N37iK zcR4xd*VN#c)SLcr%gACmc3&q#&T_gqKh^R}t=x?l z584ECt$e?ojh<%9KiA6LJ_GOR3#_;niD|hQw*N+~ya<{&o zYv|r4EAO08ED|U0v+~;~8o4XyFsxJ9uXFQ_LQZ>nw%@sYZyRdlF3#^`>vflv_u28~ zd$v6HTe+JzzGvkRJGq@N?Ert`=P{?pj+=ipV($-D?&gQ`S5a{8c`JA0l=HVgW1M99 z&t6h28eIFnW93g;`9iC|-^wR1HT=z1UVy+tJ;Rq7`3+VcvGN~S`9G|DjFn%v-0*L- z@`J5>-^+~L?YGRZ@9O+9TaEk`IqP|U zv+@_)vgMKCCT`r(ZseBk?J081-@Y^7iR5=*kq`6c-kvQ#K;%PkeWW;xKQoVfj>vZy zz%S1uZxs2C1NgjMw+J(@+y7oJ_T^vA5UMqltNTW`6v(7B0x7!9cX2(-g-d|tutgQ_ zn<#H-D3>BpVNIQv%cDVzGGk^@wxq(Eb&0io$TC{N!evvh#$;35P`x6f(O!X#Dp3+iT zIce(TqbE;4iYA?cGV`7aB*9%Vxa)*Jylnu5!YSN<;h8JGQWc{HE@Tx>b6~myGaNYF zfg>C^lJzti%A1f;=u_5xPzL2n&Zpe3(CJU*^bK$WffNverixlxjIK%^ZY(gw1&eFz zaQ}d}5Ek;97T1@XGvb51-KN?2rjdFyRD;nT_?|7cDPU7meFM!b_ajJlizLYulM*Cz ztWOzrsF3bzfj8sr7?DCqWRTec> zFF_QkZfUArc|#M4<>k~SKbO!`(9J4ZaK*f~nXbK{=@PG|shTGiEkaS?nNkdR zR%PRIN^xfjZdF+0ni*P9-yDqNQuZ3d@KZE9)w7FH6mGMAas^Y#ofy)ZB~k@g(&yl9F^6xcbCH7*|@V|*JRLpq z+E83OnNzh@BrIyaNEkL0N)W(#>cwq+3!zrEyE}oKNo#rM=@iq_5HH1?L$x_ZdE+=Z zWq|!D4+d<>K`D~-pqE!8{3AqF^DZ>};C7z!as)Gpixo{x70c~dohvoWL1;>3IJoPy z7QI%hQX=7;I_LD0PMlMI`YETJS#nnSStp(}r=;A@BJH$Rj<__a!hhbgbFjjbfLGu@ zKeO@Q;W~wR?oNK$&tP~Z&bhQdR+vc!<39=cILtlWJw&sB-G_K5doT<86Cobz?QiAd z@XLDe!+O2s6?<--L)k_v=ib#8g`WkUg#^NpD48^{m;}4U^7YwHm zs|zO|j;A3U9wCn^7|u>!B#`fn`L^SX^mYsIF5aF2j(EJzZqgQEU1>1u_yy|X*5o+! zc0&VG&V9I(6z2Y&lb@!X;B+OwO5rkvuT%V_lK;2D+&5ameDQf#;Vla94HG*~0`fZz z-P8E6?cmH-obwdcdh+4L;E$vAVIOj4NlW0d9*Yk|nGD|5J62(?_l1GP}6m9=X=ZYyN|!)toaGr&?jn(U{l0olaiU zc35-n$s^aC#}(&Ag*E3NdE}Zi6oaZO!`=#Oj>gjz=VXO7M`Ke?&g6GbB<*2P?;||%z3N9g`UPI1oE9^K5IC;cozrqQQqwV-qm|Dz`J=L z1b8oxb0U*=FYnv{k5+hZuO*Q0?eUchhCjyRBi;`0GsNM2y%6u`9URE__v!+CpvP-k zChb8U$A98+C>O6$@jCE0Z-QL6G@J=IfKWy9Q76LVT#K9gX}j?~Z^o$@_JHCwm_Tc#1bD|DX1^}s7Esa4@y6`lxur{(0s_kuGHdp^`p4)=ff zarjRGF7!e?+{{!DQq=zPu&2Pmxh$L5i1&RN^R zsaKpqVj>d}> zC!w(B{5X$Xa~@Hg=M~l*jWde#xx$*G@gCTkakiSMu;$FlBiEd7Db6JdYmUZo#rdAX znxpZ3c{oqzk?XX?j39KMHHk`+4M=Q@DF*lYJG|oTKu{ zHK$B*$`#fejh84+LSfC(_(zKKfWjLT*8I2f$TjEl?cnUOM`)k%3Tr(Y7c0)03TuwW zixelO@XZQq{<=JJ&3Q&~URPLi^5IYO@JHdIkn1}-?f5+M6Y|J)+VjET{EBgaayP$n z7(<|y5BK&8@CeyMFq{!yaUkExJI`XqnM?7%M)ZGOo`E*}uS4o|eqG{{kJZxlTXJw7vS_zNL*dKStxK_jmXFw?s7Gp4m5?d8zpK34!!&ikx?uJC%v>nkF^ z40x9J6-nYSy0mBTK2m0b$9P8t@_oJf0Pp8rXEDoj7yiE}`p0>^-`S+)8K-lc#}$T= zAEfZXViO}j#G4Ut4v~0nFR;?mZpgBV^^T6cb`8Zq!r){ZomRu3XbEp&n}sDH-d`2|G{A>>A)e&%JsT$N6uGa&;Hh3TkRR?%R`{|2AK_gQ;G;ag-@v4u>HU|* zw8JkgrmsF(g!^+~6ZhE&OgW#S*`n~C7z~}ijaT?taGc+2e1hVfp>UnTny>NI+rhaR z9GNenoJ{NbvcrsZ2Alm!hogafm>1$g8KVtngmG z8|rNEaG7^Gd{Q7E;e~i7@3cTZ(z_tQyT~4_NgI)V5`}`c3a=z>36@}jec6RfzsqFAR3f~RR zomP>?ledHOb;Y?*VXa5wWr~wjSaUS~vEn?Su;y&cBiEd?;(VsC=4d=>ztC0(E37%i zdE}b&O~t8G_$q}pKOep&5C0cR{*1zJD6DmUuH++e^P}?z&C&P}#hIzF=4gDzc5p6H zoU0VpdTz-h*PL~V^SHvA^J*Ts=6s|$!=u}_$$^SWEVRJdf|AGWzG?jMG0)>HKCE4(oDuP#5Yf$Nwjz)OnuAcUc*o=RxXpo~QI& z5MZW7=}=`{zOpNM}N`A@tb2J*YS9|!nna=)eFto0rXK#^Mt(nz;M=k&jj)f-dhTP z9N?$C5dXm&8PxX=-k1PC?HwQBKjQBxxpHpwE(q{*UQ>Wy@OTfnDZ`(Ezsg>Ok^jw`AMn#MKQ!`;$KGJ@JF?$J%zU51 z|AQsp_dLE6&T!s?)bZa_b$mYyPYO66sJ{83+<$KLe5Cy6BiUay@_#D)vCL_W{1fG4 zpUV8e$Upb4w|TM5H{<^dDf1SO@2fMMEs(l0Z&BqO;)ii;h(9gh?4aWuziTn=$#weI z$I^$V`+NtX$%_%t6^w;`)XRE;%`v(QM&Yu|IM*ph;Zt}}4W?S=pBNvRNA1?9#5-qlRmZ0=b=Mv?I zmuKOF1I`u753le~59C+*7b_h0*;dt0+f@9C`S%9=n7=;2EBze;d#?1k4`%9{Q209k z>_DFMD+8SJ>jM03{~CqA7vSstH344jhi&>D|MftAgFiZGiyQqz0{mV7=m3AuKPAAo z`R6OVBEYx%A^xF%OCbM|f1kp?4)C3Rh<~i`PZhq)e=OkNt^DL}|F41k=L+BBe;UZw z`9lNW_@#e;BYMaoqu4^ zzQ6O23~-l!jKW-P7(Gw;i2y(8-xlBv{*MCuR4``E@}BlNH!x|R&BEMwHS*{Ei9!3m zto-L?pZlwZ^NPZoeLf#%W3-GHx-*au`ulWxI_;vq@0QdOm0Keh?J-~1JUj+Cs z{;;53{^Ii;Pe$io75~R(X9f5jpUY#_2TIxs3kdY92L0uIExtJyHC>Li9wuCj&k7 zPNyfL=BPyhW?IgTS4!GFJiaH`H31IGP?c2%7q_dxy_jjC$9?__m~y^{>1BoA1zurE zoQycUkHTxgxzlnqp1d8LS$R0;D|wT`afNkWcjS?4&LfKRtiqa;52wNBJ~{0}j@u`9 zcrVo7;9+vl1~J=<`w9P)v^#lxmzCk{B-U~`OuO@dwB)c|_+BmvoRSjnd^|9ePgD3r z;1!m0mcsQ4^MR5(El1bb_OjobH71kV$r{&?y zR`PO%n-tc1R^^dv&fSXhu)>;?55EZhIQ(TjdG8wRFbh~d7URcb+F5wAJ;XbC;{*B5 z3KsYbQg|4G ztE-pRGd7Q0bBxe9AeK729w1nlbmw8DQ?cnPp;Q;k2}4o(p^5}f{n6xMo9 z$RpRBd3iXClsu;J%?fKh>+;Ao=NZL$U1811hd%|M^BmfloLRv9ILz_g;6fSSiP?5D z@xK6_bsYcH<|LOE7$3vmIpEL2-}Q8QTv~G2zI^9pk#u@V>Wu<+@?wS01a|FGqj0Oj z5n$Ku8sE4boS*06JPtYU2Vgm=)7=l?FlTcHoBIKXS;w=ioOM}+w9i29KF#jNb<&kU75sFi) zu;yqyUvZig)*Ou!igUZdn)6^Dx#nzCoWClpIU0YdI1wyFT;JCmjVCD1@d|6sIeFxo zQ?EEzE37#he_wHauCV54{8%2&D|zHPEwPIm?}P6?9{@Y~*hwKi2H4r{Y=vtTjw##+ zJuaT)!#6Ab-3n{|<9Xzo)1x>aDy%sgkDMIVC91IIX#7>hIbGoe3TyrqdE}b&-RnujP?zPKDyMD6BaeuTq>JDy%sg|3+~( zD6Bbe<&kU77mBm%)NR{mg5n&bu-2*ZJjJnI4vHio%+s@odF8Ut!JB zc!}a975<^Zn%|K}t~ndGgY%B!e4((`qw$_ILOV=QSaVL!BiEd7DNdcjnsZGax#rxi zIO`PF9F4mb=M{xDN8|Sur{M6g?wYfI9=YbsRGiZk)*Owi6=$i!nxpY|6z495HK#L= zTytJhoWCioIU4&%gf`htVa?HaVjj*(dE`3nIp8oBF>X-qVv)n#w=}pw_AQASi?~l! zDQSm${5u$iGaORKH_x>hc_c_X3vZ~e3^=>V-zG7f-MuwdPW^XT%$WB5$;Dm^@^YWW zz?Adva{NT$M}b|edqv@Q6&{LLp)C5;}u>9 zzKc=$@HZ5{N@2~vDvw-qexNw_DXcjfcPq|jg*8Xxe=5!hOlF-O_ElK(kIEz0oYS|1 zQ>!?aE3EZse3Rm=RakQ#%OlsEmlda1Va+MPOzPb2M&MoEsF@ z9F6bJ!|BQ+*J=L;oN?ZAi6@lL0_MkI#wdd?mpPTgoR1lNgf)*=SYq3S8h=qy^T31BC z1*K{Qt0*WcDkv%{T53fQ|L=R2cfL2dQ$pbJr~l?W&)oUm&;8DO&YYP!b7t-=e9e{9 z+eF{s@pUhT-{74pdX~r6w-`Rl7Kdk>(GW?M^O@}wu|2J>6)g1Fu8vMRo z<|jOUkFlBd38K#b1jKnNPN#`yg}w&cOuNE+K={+%cB?tBFKDQ%^IGNcyOE856;YSh zDt&)};cFnTXJy;rICP4~Yo+6?_4r-N#=n-shz^$TYx*7y<5};G6wi8nFNWb8K)m&_-$6GD> zU9YihM_assqTll-iGJT(Df&b2JJBC`y=43P$Qvnoo0kE)Nc0YG2I#v*f95?T`U`J0 z=+{K=^ib~-{-xKbx|^>ry{@9a^3D|fwa4#_H}k&Bi-NvZ^uN46i{9<|M>zj(ueIoZ zdnbzi&YL0ndvAg0J>GqyfAC%uz1RC#^pD;TqW62J*Km3Lg&bqK{Dgo?XLhD1K+H(KXdh(Y4foT8`INSBtKrJ`-J6CDGx$nYNyqCc1%I4f++) zjno^Uw~KC~c7v`~$Hi=_l0-LGX`)+zZmBL7-bOtIdY$NYs$N|evz;0#`e=2n==SO% z(H+$5psUq$(;ll@f$k(aN%a?foEi%{D*AZ!H0XVzyDEMrqN&4fYMAI`l?QsJ=oEDa z=%u2&tF@v}R2>_*m?x=h(LL2Qpl=7=prP}hqMD2DquPr;RgDHcPjp`u=zeOm@HF*> z=>F;_(4!l7s|K;h+md4^wx8eqQu& z73g!+cH!r!A4H!EdW34x#N|6m9S=H9^m*zW&@YOTI zRV$i1f36zT(&;>PBk1{}^A+kP!VA(Nok% zqD$3rtzDd{YK-VT7LiDxj4bj)B zrboJ%*Qt|4&s1YT|5@}6>UGdNM9)&y+PXM5stKZRQcFbtCQh#sK1Xc;-L9RB^IJ6> zbf)NA)K#Ew1HDZ2T=k0Rd7y7q^^bD#Z&fFVzD=Dc`uFPZqW_@25ItXYr4E@{SO284 zMc=8Ou{w$RQ2R-D=*xZ4h3bNf(yX8F5^>Wm)b~A^IEz5vqs|o1VwGt%$Bfd@O~+iS zR!PjI5Ob-bY;9go=zHQ!{HNmdIpSHNP-hE&TA{v5_)2{bg^Br$x?T8ceJ_~dtJPD& zpHr(q?-TvJ+AsPAb)@tU*Q(=1zo`0%eo37TdW`6G`hE(N?>aSK_{-`}(Cb9Mq5}P@ z+93QN>O;}5sc%KE2fac4ApA{LTl%FN)mYJ+6u+m)lxLH=Ui9WTyJHHNivCzV5BgovpQ!DiYfFFiQ-!*R z@Et19pQ)e^{JAO>&ll=O(EOemQ&&4xpubfA5&o5`C4JYgRZGyFMDJ38{zml_{;e7+ z`rm4l=dxd*`9qD6xeml|1 z?-Kh_V9eUkiH!aIUK&R-$? zIREdWJNe&${!VmfKhVd6?&8;%`c(e|(Wm*_M4t}2uU|`!*ZTR*M5p(TJW`k6vbFBVTT{ok44&Ac?)290j6&%--? zi}04-Jga&6K4mp8$Eo>g-mPgQZ2DcAS-#VWKL`3P(r)>F2D)1RfX^fDmT#DD0siAb zqkljMN6&ch6oW?3%^@5;_mU@x?uF#}WzP3Sa&nrNm(dOMv%8IM=%op7?41dEn&>9p zLeLM2ZlSkF6Q_m#ug&O|-Vw^>)k;72$?(?tKNq9h#A*IN&hWP0rB?Iu&9s`A?>1U# zm-i=O)5D~h?;ySv^uI~F!a>(l98 z!fSannf27t&o(!n+Htx_Jau&2Xgu}3`Id7%JYhA@@7;xIUVkc^OJ|T~zL5Ckpsy!A z!;EVXZ>*oUY;+U-Y&56&xk*Mh z^Y}R%ta6*SpzFc#2KrgGMmLPpuZX{q-gg^MQ~f+@r!S*=FuIwS2YR>Y z=H7Q!bG=pbji2l1jp8(~HO-4lZAdfUM0^P7OGvwVn+Ez8(0>KpinOak)GNXB8ff&O z-T|IHpwWZ6DK*BfoXAmk15ZED=ou5j(Q`R?W`ah~T_GGjkAdf9(CFC~!qM{sc)9hc@6z1w8;X#=j(l zqvs0n%mIy_F#VSh|EdtaF@*07;h1l2Iw<6OpLu&{?(Thdn*P$Uyr#Ym-|(7V3h+}z z*V4~*G@e@e`XZxi$LTEb*U`^JHJ*AN_nn-+QFMLp_o5rV%(P=g^R(S)Xsf5a)N4`VroEIWwPm|h`~&GJw0x}R z^5tne>S=SlGi2Hvp43ij-f?5{HSf4_)4DjBM;p1mO)J&lJ3^>Zv}^q}qop5dU;gE|X5SAf15H2Rl>aP+LMfM+v!c7nz@sH>kH z)I&?q=t12bJcB@w1&#h#2uII6@Z19$Jz@Hp5dX#yzB7bZ8y3_1T zz|2<%|3=Zr`ge*>^8YIOIDfs>T>f`ySfR_`#jjnOX5(~$@^^tUcLRNb&;1Qo54Vzj zNyq8#|4HI>hdA9~+Mb|$K|S>LJKLB8=?d~wM4#@TE4rV5x#%>Q_x}Do;b*|S5AYur zKG1(g^jZGLR&zakO~c!|{6qblrn-6<3gsUPWgZ54xStJien)znjx)+{cZG{H3gV3N z>stRn?;_A+{N}>P`l+JF`ETi;AMxmThDBEQ|A9tH|ELWBh$Ki6S;k+#r`n9IH`7MMvg%GC*bg^GxV{)7YR%g<9 zF~w;}FuojH?HN{c9ez$RdsBRu`jBS6i+B|D z)udhB&IR3zw5waxOTe=N^qZj3AErMI@$V1e&8cDF_m^?`Ii`DmnbQqv-i&Ulk13qy zc{aM4$J?*dcY|Ily1DmvtGPbcTg@?6Uzz50pfb30ENSLzh>rrDMcUP8Dd<~4A4A&J zE$VwK;CUv*vynLWdAK|r)Af0rZb)S{+B}1cHP2%g%XwZtr)fVR?$TD$%y$uQI3m!U zNxOMG74!w5-=}HaJfhC2fM+^*=7GjIsF#t4p9RY0WbU2?>NIahM%VW`fIdld18*#7 z-sc*BLvOCtTvvavn(JiNRcYQcl&?#xNHd>J{7cZ)M+SA(5_EUaE6MNb3iZGWcp~7L z4EjdU81tSGj-ID0;MoYCPeEfG)cjCT}x!+}UW4$k9&CB$B%X!&tp_u1Te3#B8&3r5It3l5r?dIhH(5pdj20fg#TfSj> zC-{E?jsDi>1^FUJ-4i@RK%)nB26!feMi1(_;JFv{lc3T6&k&BD&nw`mP7BJ_c?-}O z2X!iV27pG-B_SL=G4R{~8a)d`IC`D{&�EgZd-z>;{b<)OD%D=<4Ta(CFzM!qGDd zJUO7zgZdiq%ma-c)XTuL1~hu!3E}A31s-)_Fz@I=eI$5J0F55h14BIHLO4!aL>~SR zm)i~I?mt|o`S{)F+Pcr?^cdlF^?$X7(|?G<>*;pH@Os{I;q^f`(C>;eo`(AWaibgQ z_d^-o*!xkYZK413GQ5R%H68IA-BRCsXml&@5#g=9mq33ax~=!M=yu*-(Di*6v%Sa9 z(l;@W@kWYH@*Wc1(c^a_7|(HD^J+$OTYV(yYjoXq@%BznvmWljIDZ%YYzJ7YF9+Bk=479pEkv^B`G_RFLH`d2!PSfc~%bR$CZtBruw%i=27=JUbM0j)1ExcQWx76=DHvU$6 zU2^(E@wD~=-A3P!Y&=JLd~JZyZT0m4Mz`|@3UBA}eIkY*1^Q@uQ;p;8^)m#F=NRvC z;T=4_PsH#f?`_c?y^pQt?TgzD_VD_A_slf}Y-@Hzk^%kz10Pl~Nqb%#BV+&Pnv!geV1y zeyIhhY?TJT$TA9$Hc!bar3D;|#7cPK20xNtFSme{ja{zDm0EVX0;mWnTn;_c zmUR@xv6CTA68=N3#Uza?l#dNi*v_n?;ttX!ta3YIcOHH~0^4zNo6mu)9AD?6@i z;gP6)j^=aYS&?jM>AQ9~uyb|f$0`^>7v!L(n39nbDXL)f!?9UqXXMA?7i@Ut>0ioE zkQ>dTMy-6A6Y#8}qI}*z1=XozmdDfz=S2$=1ciNLnIMTKDV|!K5-rF`G>7GJ1~2UL zg-e(t2c8n4HniNlB(&?fEa%UQ#BwSafHuuYg3JOODay!kyJad|1{FhxPSj$#ZM{!+ zcQ4rTxsjxA+z3>pcLfsg@_nGHu2I>3V#2;v&Njc!IxRz;8UW6`{d_O*2x5*4mEI+0G* zRIt1{N`fg4$3YYy4dmrT@(-pc<@^}> zr$`mjxmr>O-8v;Fr^GTcPDoAeoqXacd`>fEa)i!(>NC^n>7?~3P|i+&qb^gWTOD&= zl|G|I1;2vxsRvz^ye#=#VoLg%Kg+pOTuFihdYyUYml2mgGNGX8XFT_tpvl#nVZxnC zd?T$(dWbVi+g$lG-3N2N&>ly?Uhc4+xlGVtmu<-7T98DI-vOGeqLTddh^~Y5?2`P9 zShOI&Shp#fRl06zzheF-acd_3f8x4d72c5@)UG!{e?wQ6`==m1-IgW2D68@({PSqF zEXYXV(`N-4Wsg29e!@Deuwnb23RhVL_L!#Zm0q;A2F&Q^tAPs4Sg?jwV8d}MBOjt4 z^6C1DFP=l`2p;OTo}7aFjIeHq>u{9&Lnl!WJG`YX{$K6uC+t%HmwGyIZy1-FYft%a zg}C*hLcK>m;N#ZaoQ-3}Bz=PEJsRr1Wk-vOW9if>XB#=vqxqRxrEy=3|I+F^SDIqa*@QctwUulQa_ua#lD9BvpB{1r#F@BZ^lr=_gxffoYEL#Wrh zNMT&19@ZW}uz#Sf=$Cao{;OEnv~cv2w&O!s%tcwhh6VkrSSY$GE8m&ZtyxKa)#A`s z-W_^_sH;+MC88>|otUHIl%hzv-ORz9(+u0mz%h5?m0agr>9tDlI{2EQ>|EWN$8}Wn zkL6D;A9!~I&A0pWE)E^O{ocVG{#RWt6n7@K{D1c0U0?l6`3Vl<;H|>PU5T#!IY7Y@ zO~U<#^4A6FK;`lO%;iFrYv8zhNo|B_4l;5JY7G#x}o|JBy`p+%7A8*^;;FvgC?g zz*^OIRJ9#)VkqdA2N&76^X+rZQDVo1@p5ecIVT4t{LG?|1h;8Q;MQ7 zb4D+&-KuIv;u=e?j;byQwux1TEB3a<@cpR% zun#nQymGfuRka(xvUa1epfH^;fTQjQUF^@J!ic$_AupD${}|0CQNTa2C}BbU6}q}6 zJy(3$`XAGYSuxs+>Ja)ihV%^km!xY?4y9S5-ckzvHCjsdW8zge2XQl5g5#c_?|g(Q z`XN*RpOH<;%gW2hDT=FrgSvtwK@|`>e5aTXy%Tl7_QO=c;i$;MssH~~B-c*i)Wc9a zd33krVW=J|4@Wu-1)@tf4nui@W4K@8de2v_oK=a-p}7ZVnh72i7nE++!#X*VS4H&UF2+UcmK>yEw$He?Q+o7&iU`U!LZQSE0=#?ik6Q*MZy) z>n`@cU%eE6H6$%`xg{RYNrhb{S)t4A($jgPzh+Ed?WE0=jQK{!B{Y=fmWtHpcC6~Kj zi`r1SgR49B_~>4Heh5fOerB{dn;RVfnPow#1JTlw{Nm#5%&46rS{nC-e$2!lHeXq zDcbjvD2PAcJzx#F7NkOZ?!avS6y^Fgx$_xSn}3de1Xdz!9hII@d*4ZV`h=p(;#LG& zI2Avd4qK8C1b6>Iw?OB62!)U3(CVCBn2{SPE{la*X4LhRpeRXY4 z5afX8eBz2!`ME2}jYTI+jb)`w$)KBCCzM@06+dkfws^uWQvbi$_Z`e%-3QnNc@xT$ z%A=XlH_g%R%+zD07YMHk1u5B8e`)GxWCokgFMV}69R1Yt7gUBfCHj~06CCi=Vo;+8 zSiu8hqD!O>P_RUkR9#h-s9QtLxZc)Fxy3LtP{Fg8Dtk&SXriXjlU1u4_=?}l{9hV) zU6HzlZ${5;;Kc9wphpQyYHw9pz8Lr_aeLlQ;24MBjme4QRM*OUGz#GxLii5gH6f09 z%bJOg+@t59bK-oFw*p>UJIqH?2u}^+gMo7%E}b93Yti%9IkERyt<1-A;238e@cQ6? zGlXxjmCG?+0RPM*%opYuX94g=;9nZT$JGeppg#t@3B;Km!tbvW#Bu8$lTCK!dBpD# za;aH8qnN+!AdSZX$9!YJn}dIP2*2OX49Dl^k-KzSW22bA;vkI&+RT_^{Bgirfj=jN zuLQmt{EeELFB~7^bOzoA{5?YWcsny32mPhMkAgTeL-;1(8^GVp7LMa%oNmC|gTGe@ zZ`;XASVK;D0rQPd~<(xcvNX2bb0X$2ePo zpWw_i-WkI6i=OFsj?aIhxir%*Z04UhNaH!S(wSrY#lTMj|MCz%tfMh;oDaeOByfzg z0r<(_-x9)?9v8&_6#R4SO2GMIoW;OT0srz4ep}Zdejo7f27Vgw@!bM{U*Kzi_XFO{ zRxXz_4fqJ)X8>OZd;svy$>s~k83;TzCE#ZQkEI5j-}&IuPT)fvq;aNQX*mAbz+0VY zzAzsK{8iw?fw%1u`015-lAZ+qnNWHkV^8yi_-S@y z;Q0R%x961t{~q|Gz%l--z(<3BQwSetH#W|9JNV}V$2iM@j|KnA5Wd51oE#tho?W?_ zkApb%LO8#Bk`w#i(8_$ofMc9Fz%K>={1E)$34A-Bvp1i*ecl&j5et5PrU`6poV({&km_FU%(buWo0Cc@FT|z$XFU0{k-I zsp;km$H@i00C*noZNT$^uZ)^69OowBjcg&9{|0!gnEAqdBk%>lvECj9{#)>`3E^`} zg7|yEUpP777-u%{Tfu)@2wyrSh_fI3BT55~adLp)2L4zGA3ZgQ^9}e@uLwBC84Ub( z@Sh*Tmjd4e{$A69_!wsd@cG~$7s3m#4C1^E{#I879OI+{|0DQM3*mEt?*spctAqF$ zCkOZf@W(><=ovwr&%vL1UBK@Ko(p^-@TI`-0=^A6mS-REKZC#4O!I}e7vwu<1^f%} zXU+~d`mY3j5BO(?@Y{gz1b@EnPEGL70gm;v82Ez_XL$&}%{DGPzdOLc131R z9Q^e{_!{6pg1^_?#BoLde-!-VLikqTUxL5wydXZt=>hz4@TZ0FEx>;O|6to_a=l@k z@xY%1e{Kk0_d8?aI6r~Et!*@!W1JqqSAah)gy-7Ei{tD8|GGQO7v^5t!=AYNm;OevC5geV`U>cgy z;CPP^emn4&!T)d#`a-E}Ip4LM<70i+sHyEb- zrNI9I{#g(Q%eg4T{}138|34vdI@F@fDDiw@zP&8x`oVe_1b&Qv32=;)3vsagkA(Oi zi;F+8-Q1@LtiNI&u`M;{7o$9INWb{ydUrmz$XDe7WlQm-vYiDIQJV} z{1dt1p~U%aqQUXU>oGO_9U41+M1765|3lz?fxinp62h+pz8(Cx15X8he+Ylva;{JG zzZK%|VH;?kU-VD0oa5kna(f8BKZHLA@lS;EzYHALtL?zCJbQtsKpdYNSxRN+x1Qx? z=W77)PiZ>09J3(~mh)=xV|nI)ANhSD{%s+=RwG?}U4G2Bx8+>^9Z=3?A^bhypMn38 z#zA~sjvrXg@lOSRyC%lZ{Bz)^SYCF0$OQlC;J+RC7r-A3iL(j(y~)q}3{GDF??d0+ z@~Y8P|E#w=^!Ewj<3jk15dM(mW#{+f5dU|;PlIwcwKKx?*&q01;5&h@58?ZP>-n|& zzZ%VHVoDqz>!&4f_PcuP6T-7Y_*CFuLY(J-WBhHFbG;$|s*JyxE&q4GvEFL5pa7J3 zzHojU07rjU;Ahb^ZhnV{@C$(B`dxb^?z1o&X%@cQ|m2Ga5K<*LjwgE$3qJe??_-<@_6PjQ=!n zEa$Vpv78?Q$2dEIV>yp$#kFJV2iw($fb;rhclX{Cz=sld@q2P2j^J{%+v7ynX`S5Bv?=(-%rye&n6nGd27i8oT+*XwTH} z&uHxUoc2r&$K|-By~a8|F2|9_1RUec2L26MT>RUBWBmQVzXkt&9fJ601Mk9}drCaN zIA7-i$MW0-9Lw`Ka4gU0BpqJI{})MD{-zxRz8iQZ@H2oH0mt=cDsWzZ+SV{}uLa%* z;w%S_es>)L*8}bcp9ep#59@*B`hW3pCV!5D_534n%=d^+fgkhj3>>$MF(Le~Ww_g} z-wWZ}fuBo7aLZ*EaGc*d#|Pzn5By_8_{YGvg1@j!5C{3Rt^r>U{-wY-1FzRD@P7b& zDDW-7XN2&3fxiX*w}4}u<|hRC;&JF*z_EU|03Q$W3zCC4=x>@5aGc+9A$&;)|2l;C zN=+R9+7P}Wgm>wlIL@pPzAc3JKQVEf#UXswNr8V4Nw?ni?Gf;|f&UixMBr;ec=MBk zI8pGw*)!k;z<&T<2)xNDf&X&goq!hszYusH@J+yf0PgjoFO;~QiP7Ndp&syJ-~)kA z20j9K3GhdO=K%i{IM!!1^AorK?^00GmQopr#Enna~JSj@IS|mI3rb`QHP-w}k}m#9z^?*6 z8~D}0$MMdO6355-yahPc|0lrFe+)P7lsFFh=Kx3lOTf{8R2oMqU!E(|G}iI2A?fC~ zNB@9N2R;P&wZO*=3j8yGUl05`;I9Lp3H%e_*8^`gn7&ZrdboiGSI$)6vw%Otog_-^ z$L0R_b2Zi+>un$KiDYr{&l{20{|#`A|M1Abk8wT+J{#ii1CIW@Q9&Gx@2YrwHQwJ%KU9}gV;b1n+}JD@xbM+bZk z@S}k51O7YUSpNL6L7dIt-wgaF;GbWd*gtGsz|ns<@GOWk?~=g33HTb|*}(SzzZv+N z^dQb8;7!K|{4(HvB;dF{be<4!tmib~KSG>qfbR!BG9!rd6Yx)g7E#_7*H8%petTfV^G zrr%uoyYND##C}{)BEWw`zd8Rj%WD&R9{4iwuLZsrIK~;nD-9*icPcqVtwC~=%6G`Ki<+!><8d?^i%&jvo*~t5Pyi z;yCxw;CLJEJW*o4j0VSh0q5&^9M9s;6D9UPK!f9V0>2;l)4=&UALp+zjZBovu2)Be z@E*WxL7efHb9o-2!NtEag#Q)z-@xDFO8P>H=3T&_M>Th4Lfwo~9(o`P!=$GOAuvT-&7 z$2gm~b4H2%J7{qA(}*D@MZ+svnU6xNnYX2(y8X^=6es4dYh^yRTFv~QR`Y0=)y&_p znn&MT&Agq}dc=TISB_(l{0POTc>pe-(Hd z@P7aw4E#;tBY?ZS7>x#gqSZVaZ#9>*x79q#v6{K76OEGTWx_EpIWrQAc*zrri@oIH zsd=%;1RBSR?AUyZOd#SV7iHx}=r?9sn(4JPLFg5c`Zbf0qN2!DFBx?ruIn zveQS%aVXRCaq6K=U(-%s&6afG%<)<+oN0i&*VF#&HFCm^r_z}7chk3@H(vkJex4qA zg=Qq5ZvNf2UY~wp=BA%(r=M%5UsG2m)xu5hXcH2y?j7T?o4!#SLwTRGiUyaj+ot(v zm$z}6&xO2Wq;wxo2#MzvQwRD|mhtZ+9y6}Rcbc)A4Ojkp278@poGAa@qfFEkxA9tk z*}S-&a`R*@dX%)xI7hTFl=H1agY&xiKMk01n$Os?X8LtJgOqrBCS|7|1dODl)3%xE z*I2^SA3;Oe>3LV^n6>KBK0Cc@e|UP{MxED{e>Sm1^;i89lmD$cTJT|L@csV*+y5_a literal 85112 zcmeHw349gR+5Su*5Y_}l1vi33WDlSsh)d7}gM#9M+G-6U2@-(>lYqDa29y}FR;^WQ zTSaTJZdk2SMGa~Nm$q1~#i})~y%AJg+Qy~j|Ge*6=DEoo3F!C#zJ4F```tUwJoB9Q zyk|diX71dD1(PPEg<;^fwBYbSjv9kJrObB_j46yXdBK<<4d;6VS*lG}rTcOr%zo84 z!@GSDWGf-oI^g5@RpX{9@AeyPbXRp#8eiE8rWV${Tex_$;KKU!8HZ+L7Zlb#y}DSK zx^;;=GlF2XoPOp>`7Rv!!Nj^3&Md5ZtFZQ+EmKaOJhGv1yi~yQsOaZr+x{x~Yl6y2*``>Iybas++u_zFwe8p%G!%KC|QPrx+1d|I5VTQ{lhkCW=Z*EoLbixPk8v(0k0uArg5ph5Dh z3rtad*?h>72qYis(|f{)`b8r^Xaai4ndRyEU?q5I#~^6xEHbGKRnviDB6)tYrAcpEEHy18mE3m#t?fYSUMyD6*G9)m}Vvn=6YkL>gPsRR;ER;!t zGa#+7F4T%9I}I|>1{q_6oQ5Ech4oh0sCwtJ+CI$+r$M+;g!MfNL#Z(v3+uA1(Gk|m zdU%=nSdZE-g5LlInK!LPpbeTpEppC`rm!g!n#kLxc5+yUmN0n(+(2d2hH$#D-gmT> zXOZBrur4F>!GbNz;7(Gq;0c@x3&%c@d09Ufa&dz=RzG=5=3`l7U&vg7)*Zln)2or7 zL7^tt<;+0%4rkQ-7&Os_!url)eb5UySd9~_P1^tux1Uy}Yd!02{gi53>r#NyekR+O zUCV;VMs%xebR!VM%0kUU_yy}oHsUT=DX1Q@x{TFYS^B8kNjU)bd!S^HK3^fT)rRSi zz%OpxszgD;G<%H{)%dV~ZQbg_xW=pTM9E9^hDZWP)y6s!RKdL{7tv3lLVDd()MXtOyCryd;qLH=j$S~Mw0&AK}p>xMSX<^k(P`PGO-7GktRD%p#Nx)^R0SUli#QOd+XM^g3oN)M5q<7 zE~CESv!A-O+Xp%3rn7&7{!+YWcLgq-w#SPL7 z#n&>#5!3yDaUhh^yx^+V9?6W45Ki@=5^`$O+c0`Bq%_22Yg7nk(hk{1$kAu5OZ;8J ze|_HQ7=m%(d>F;>DxQwl5m;+z4%$Hd-t(%N&OFt+%W}C<(z)~U?Nu(*qI4R?X zxiUc1VMZXSG%njxXxa^#>->5D0QLt3q!W~u$Uxlk>?37J%_$cf*t!&)^7{}le z%B_BK0zO_AqH%IxB_=+-%Fes$ApK`y-OK3oC)YhOxel|TNjVvr=@Q3BsNp(0^YgB3 z1eInZS%^87wtCHl!qtBhv)Cly?Z{6rkqk#2db)7+p+=#ST1emm=U09DzYor~lZJ;O zZQ4!D)u$(ftMbKrdwbv`x=89qZ~+>$0g& zpEFXDvPgS=E%UwAapa~>q>jVs$@M>$)0p}+%`!d17_ijrdP$Jw>3^ z`t*B6qS5G-NG4(k_ycqe$ji>Ks`J_OWh%o=XK_Qbx?{WDS#qE?7XWdg1T)}~IartI zC*El_L-9tj$av}6(mf-xrBR!K^d~r>RiVDyZt8o69Kn-CP{9)oglezNAv<&VA*xS* zUwSeOO6UzzlqnUbX*j6tpKV&R39U1IRqBu!Z7{Y=DiC*9>!BPDKf|?2vy*C$yRO$}G_KNb3^+>LRg82ar@U zE#`t(84D=82K4+W^EW%1`E#{1EmMKoL|C{s(GKQhc#yH65qmpkPK2^4+sKitxYBCa zYL$)V5It@|kCfU-7Ym&^&vhJ0{x#(Rs-R0Y_G)FU6NWXhnj1$tK zYvDx35<}sH8b;ihnn=>6+j3F2@ELQX#KqZC3vJqgU9_W2>q(3pJxnyh#Q9bAj77=o-wVP3o6#2dR%=0|bk zp+F&yPTBlBYI zcal_WFeS_BKvn7!9LCe!0)Bbl|;<~SJrQv(7 z!uoE@(H+VResLmz-1kG;&Yssu)6W)(bUGMW<*Ive1D2Q2L!+Nq+!$Zwj06}fhe&$SEShAHLOwib`CT+0!0F}-BpZkI>9;^s+nkoUR0~!V zP4~kU-+Y&+FSKSdF-^myq_#QCTsjgu+tQ=XW_kK)Dt#JpA7cW^B@w!u_UP(2-DT$> zOZrJ*Oupc>Nc&p*ul4{9HfS0J2cp41lD6l5U}_ukv><958zudNO#0Og2`0LaeDk*`XmFeNx`NAGzH?*K=BFQ z#I!UFORL|&0FP~UqbYRgWOQrxXHWt=yiCU4?fOHH1QXZ#c4A;&^jbS{Mj$1}! zUu;+auTuVN$mYe8HHUdvEzQD%CtQjM$acT12g`?7=lZ0Ui&6e?HAjoo4i5q zy)N;{7kaI$7c%P-BRp4sV-^KQHeA$e*t<4u!2x~|da{YHUf2_W*j5H1?OcXi2I zmpA}q)9I*K$*$)~6s~I2nbm8|hiaP#W-gC9nVD$gaH_VUUH#-n9S=4DZUECev?g7KlS?hGRhlEzKQCvgDXb!5Yg z1yQ783H1jnkqEZM3=c91S&zf@RP#|0?IMq`sI$_=OC2T_8l*%kHZ01bs?`dMlQ{G& zQjKK#`i$r#^;41Z7tFA+!4zW)mV;@b`MN%WizX?55{Nm0-W47oN|eVX+Hu-$4i@2_ ztty%mQfszo0(CkDw@@f%SR;=?%Zh~Y*rx+MfKzxkN%}sHM zk%UMGzO=hAnqmOdK`B9dZi%k!FUJwgyo9DA0j|(AqSK+#rs-3ROd8DAtxFr^yx2)K z7if&esfl%KwJ)7m*RWcK5}Aj$)4j~YQFU>Ji9_Asg&i92GgmXnDakX%lt_CDBRanBp&>y?O|Yc0n8%1Q6cbS>CgK5jSB3YN%^-H@(dln38 zFtb*U;M_53BR7Uo`&}JeZ)Rtvw$x7*KO2k_&GaAgrBuJ!6lt@ozQgb6x z(}a11QD6~8jx*4k!y*|@yoW;2NKj3DAF&AGeE02}kG2$B?eeCcYesqW}OkS<%UG63K{-ip<1~QcW0!( ztSh(~!ER0}Hq=Ndw#04dk#G`uzAiD)Bx-%r-Uc9|6xSBw0=&Y_8&sX8ooqffH6dZz zv7wHLlP0!RrubL;N@7~(#khEbTWAXSXqPh9*n#oMt6*4VnO8T&wp6#s({zr>P|3m7 z=4ulKA%y`4>!&vOfk<*Zqv`-Ex?Y^D zmua5qpKJ|CHEF*Wz8AbR9OVlUdPloZ7@%N zVtuJLVNsH%()`!N=xe0yNfJ%JH205@Qp|tqGLT163E^;g`nnub277JtWsp%ndA%&J z^IG1sSj)S1A4o$y5AX!i@P3B5K`T>`te5~LJ^-q3=VbIaso_rLeBxD%iM6YV`8-KkseQcM6oYvfP2B`d&i@e&1_k zu4zEem2`tF+nsN5GflkQQrIu_hQ40dY1={=Y;}7s&M35Xun6CwP(7 zT|uK63b#-fyBaH$x|udFD^Fj%O*xZ|lYVMbH#M;X_I}CsrnIn!uFyZHrQ?%{Kq_j< zGxbw97MA>RiXBHb0LY|t>S`nf&w*gpUC@9l2kh{~l#KC736qI{Y*E7c!I8q+r?Px$ z{dYQZ?OVk2*_p}S_Oo-;CEzNoQq-O>aW4{#{Q^ETjkV{|;H z0mtj3|(h8&^09}d*r@@&~HFF=`~RW8j?a$TYc&zP;2juWM5_fP1U zog}k!`sq49M8C56_7(*Sqmcm&KqgeUSNf(wB&~9`?>=>I7 zpL8J@k!wp`G%jh##SKu%wWA14XShKV6d7@skRSY26NDKm5_4s5*lNNGxcDtI02}?5 zP0KfZ01C+lg=v+$c#7tw*%HXD@YSc!?~%=S@b#=X?H`;$;?Q$rBD3aoTr}FOa_2R% z{K%$;tVa}BKC{o;9VHQJzBddsW2zfsvt=TQNAhZYthGkMPx&=3OVe2OO3YnWC$e9W z30$+%BLROStx*CyC&E#=K$m6wH0<3SNUj z%`u&OFM<#r*=xSW`hwRG8L|#KoiW<^JD#OP7RXAHETpJ9@g?e zu0Bm;LW8`>;K;xf5R#^n25I)vOG~C;x~f)j660C}Y}rW)I#OBOL@hxN`mCJB7L7d@ z{snh7ewI3FSvG3j%*2)~o6DMCom@9z4^Osi-5zbUm?iGGhrs~tcrpTz^B z&l7M!&MnvSK$pJIECAN74>gbQFuvpwDgl}V`ccylV{ZC&*;0m{C*|`qroCadu8+)J zfm`-kQlA!P4sK|Y=V_V_w$zk2hUQ@Um+?x&s~0uCzVX#zwu34vS0R%FeA4*g9F5 zNES5D$gGer^B{s0Udtu*+Ykrxcx_^L^CWX^;^NvwZ<7;Xwk`}7uMdJns3QfY{0C)T z(M$YVjS8S!Q?dAZhT75kVa}r>$)NdSW7Bv!FD0h+rz!J_0l-?PAh3ffTD|lg(%fE5 z=sl~Z&QL{7Evdv@($eqkl#K~<1qR1lvyyjyyUc4Eu&S?hSeF%Kbr_QLWsrWFc?O>2 z5ZdQULQo>{l*}m!lenRN1q~}k=ZXxJGDJb%j|v-TFLI_ zUdY^zL9jUSZ_M{<69>ti2nqBXlc||{&%xe`$O;NONjKbxCGpzku3uLG>b)y~Msr7l z3jpVzPbz@TS^%caY5{C)a{(Z^>7z3wS1+E7WPfc5h_)xRpZF5+nbjz$F>&uVEP+?& zWJz=1^1Adrc2+9az?lyfG^eWg=i75AhlN@WM@Tv3xpHv1jmcq)>Q2=exw-Zm=4KY$ zz?D-LidUvzGEvWvY2D(F(df|Xa8S_v>Of!pMgQusKX;H0AnUKMywUiNr_!~GW?qYW zZyqn!dN037f&@R!(jXV+1g~#QT!z{uSISLtkOtJp znMJs^_VW>$mr4I@xv7aT`Vo;t+APjwz!zAQHJU+KK3?DSDMB=zVuQgwJSe#MbMY;+ z_JAZ7OJ}CkZ5oi4we6irlaTdIKSvO~OoOx9^=m$&?%1gct^}}_=YLZa<mZaGrV|@C3&RDb%xG{3vvU7SHd8?p*Llk^^DE6&nIx$Jomm4T!*TuK?r}9X)rdu@ z_HxD!k#fz8K1z7hSAW@H)=T$xC*9y@8hMAJ+Pdk4T+RZ-ryg{Rk zE=X{lBkK@8VfLW_q#Ze{d6Xe_eUpSk43jsYL*4YUIjd!0qZ}zlvhjseNtvr%kL0yp z)0oGbGasD1Wm(3im!L)8H?dl9(YLk#xB2V;j!x7CF$?{q=`VQ|ZR>X1~nl(@M*`-!0#4 zj|WaPy2O!Yzs2sG?S70suSR3O+TqvL@-t=LB>&q7A8%#OVbQzaZ97g@B~$5#s2~i| z!aOJSd$E(zf-l6`RCXj#D*Z{@sXucY`mD4(D5C8LfT{es1$-EE3>Mk);cBdYw#$o* z0KJ4yDmx-drBB4@r9jdo->m%~VAkL^vD3Rlo;%e2z9`64@g4+T>^S{M9J@{IEb5f! z4s~A{V@FPkeq{^&_ha-cz)QYuaQS94?snzV1J@24HCFSL%Ki&6_TL3B>FQ(K@lb1L zv*imcFUu&heHCLz&S^eay&TJS9P&F=e#*I2`ir)qZ_(cbSH|dX1~2*6IDVQ$g?6L6 zV5Q|<`_C|Ra5s3#2WS6v%RgcDuAgXGZ)bMPvr4o36ZBt~-qkJi|JsIrOZQ~^y)ju% zWq&AmNw4e22Fj!en>=eI>2h|K8ag=0@~ijDQ^uA1p_ZSQX?VH6F56L-f5+$gKH~Iiw^RS( zHuSv*C;R_FjJ^XVd^#>zeShZ=_L9$TKIQ1{(0^S%uN;yb@4#*77Y$3+Puzw+F+5rS zy%_yD;3Zurjxc)H|1Yro7rBOa{l>+XA7J&453or-%(T25%O)!dUzZQfE%evN=pO(d zV%ELt&}nKnOTTeZX!(W~{2I$|Xu&sFzR~h1Zrz@;{5vi9^_EXq-i;&ATYgImegk;P z&%{A_YIlS&ANS957Zxb9pTEcm({y@-TpY1 zEf*hoyHaD2Cx?C;4J7v6{Jg#GpJjcq2gVn7ed6pdwYcfI56KWce*{p0xGTRV$7=K%%5srqf^M`Hhm^gLw_wEQkehw#6% z{IQnb)AH`R_dv{JWIGzX_~Wj(T)f{=zGL97zg#^p0>3Zpx13imvi4nnxXG9g@OlN+ zw=9>HR`2@T2CIM2^6q+8@=>-YE$_yWafS{)1ux}$n#~_e2c40=RQxV6{9ZBqelh&; z82;cGJ`cQ+1ozS{b83wUW?E&Y7h>TkC5F6ZYV z7>^|XuNi9WxOVr3<$=ZXBo2#I|`p3WZ7}woy<_aV~}UBI~?Cl z`LtGc`p57GE8nh_{+Jm4bmiN((pSXr3&Homn*yExE+2xLvg(@QCBdBH@`|9krnGWi zO)z&}O-WEvRg=45%;=)g2j&8f99|s^pEM#Us;()nstJmUW|vPdF$YB&ROI-S!<2;; zfYntcxn&jeb6e?guBfO8r^>4;E3CPoY(aUA2?CHSN07~~ESV(&vzsxmqNKX0vZ6?} zMMX1a%$`?0Ga?F{iSmk?vZ{*W*}3AV^#sW)uY@m^^UJDc%&t5aw1bF&p_~njZFR_m zG=#ctb{CLUj;AUbdCK{0*a zj9`X|puDo^+^X`LGJjNERwkLF7K=(|S5}v)yEQYb%8E-R*Wn#Z6c<&WTRc~GbETBe zt1c;?J$r;nnK?l5T6&}dqZ~NEfdd^l$br!kxv@}GRb~rM%i9*MW|l8z2i#t(hm>`Z zlpD03D6X-^KYd>L>>4;d6A22c=2aA#GeuGnYRW4ss!atkY#`~XG-iASu>KiOs;Vj~ zC0B|itVTCSgXEf!5Se2>Wf*hH#%A)cVL@@}{Bm3K5T+t`jv+M1TAo4> z;U%OLw$ZCe&_zY_B{NMA16W*jR$$|c#2T&GF!9R?)1tJ+;RG|(GE$zA280vHr;4(( zQ1!`}399WaDX6wsFuHHqva8A~&dN1;Wdr%PD7g;KHo{`m0XZlwGY*q%RvBc&hv(Lm zlnfg&eC+U12TLa=A21_vZqo1r@Hr5lgYX%R&%yYN!DnoMWS}h+%`U~!Nc7iH@s>*Q z`odnIN97CVRa95cC@q&$Y~idy@La+_Ul#+0%-QuC1=bI`d4#nUS> zbVwGLNi#(Eq%($A~LQK;~y93bKtFsQ_AiYV0_J?Z!($4@Fc ze!_&43QjIMdHj)+3W{V7VW&GfoxoH*D8)yvg$EEG27C!VvQ5Ot;n{@c+zR=!TPnN^ z=UmuZ2}_Vv`lrF?V1n~ZIyyT@IEOpm)_ z{^#`Ff?YiB9>_xw#%5+9>*EGzVIFU=q@{hB7r1LC90qcoF1)Ni97(tb;p3g?KTP;#;7h~-Y;w=Y;jM)GAd4Me2Y!W-2aJcdK{JswrxRv7aagW%bCCOz zKcdOklEAaX7Waow2KNl)Aw`Gfdd%QmgEI+Bn=t%t!D7PS_qb1R8{uDhyqDHT6L#<5 zUzX3oUe=6+mvZcymKW5+XSZDeEPPg4UhoLv=YTJ84W!h}9l%3pzehB`%O@8C!_`ebU68I7; z%4{FueF+~*_(AAh`7%Df4Vu}cxtK89`8r$*KP6o^AgtSN1a|y0gd2ffy1pVT_e2~o z_d6VxH4T@pI6RK@#}Q`z%ov_EOIgD>#)n^#=C_2|CSzGYaq0Sua7U~UIG*+WVtCe! zZG+|{(v%TqJB$~S=4!&MxhsZe&6A}06JgeT6vMNo0|r%>KYa+ZhVe+!97UKljK517 zS?h3dv1VBe&zf6E^AKUyF#bJh8VR$8@fW1Y%t-C$pctO*96_2XgjvJ*Y|>mvm^F;A ziP6a4KcvcA4!af_nFmULBD|XiIxKS}gFEPa$zhqd8k`X@KEm@|bUte|-Gj3|-y`^m z$326mJ>Dhw#N*uqnG>0?y9aU~!{FY8`vf(f?-ShR@m|3Lg!fEyVfPLqyiYK|^L+zZ zr#JTd2lGANFOczH_#F5p&oKQ?^JTIOD<&qWl@`v4oZTI{GhA$sj-^h1N6dCjokevgOi{cWSk4bswFTq6UyMmPKcL9WG; z4{~2$cXa7@L7up6D6sHyFaC7Gvw<(MqGg0{B>Vv3!N4o5J`S%V{a*;P{<9dK zH9fjVzU2^R4dcT}a{^)3FrG!4MTD;*%=)`yc-H*34VpJd^D$wz!?+6?lPjZr2(xBP z49}Y5NmERiH5bJ2tht&rKPSu@#*dQb4}@96_&w5mL6|kYupsC1gn7opNOKfn)-e7q zX=W2<4dY8mBmWDL%Aa4y@NDNf(!55PHH`m3nhY$dq>7jIePc8S$M76>GBh&JmwrQd z*UvjF^L&HTb?z!y`l9_UFMZB05cU+GecO4mJ86azW;+vNc-EXjnhL_KVZ4+y*Ar$9<5i^jE#ZxX zS^r54&zjD?x1B#Zq&b){+hKe>X{Hlq4dV+*b2Z_g6K4J6F+6KtBh80|Srdmd@FU4o z?I=5jKRAXzE{5lLXF-!A&rxIkAbdWsYz||}XmKa0$`*Il^)#dD9OQYvOK`fyIoO|r z&pfq%tp3i<=#K^K>>R7}9K%l{JXz;F!i$}u_$*XAGXwcup3%<~Gah&GxLog*yRgoG z>C;X|*vnzZZS}yy%QN8*6aGE0%l~%>|C4YJyhYw=>cFqCMj7X{K{K8-ClO{lUx%Il z6$tCLN?^zTl<>V|{|Umc5uO9=(!%(oHfXwHnZV@gC_?``BO{yCxnLqyY$B4RiuBC zFza8B;aT$qX}aOTTIU;U81F}#afBxmW_=t!3;G;9bs_m9ntXi%Bk)+-j^TT1+j00{ z&+ig2Ugr5;!R3VS@OZc2Ws9ZkUdLyl`mk3Z&($0IAh0_$S_d4KYafIA!*7QN z1ai;X@B@NtJRYRa9vFU*{-vYALxT6bW=Qb4$3qDZ)8EP(eXc%ZV(^F{8|z309~cZL ze7?sA1s8gJa3H@?H2N{YFD;gQklzvrFLiZBH}pj&3vh%Ni2w^PziW7y@C(4Mp1e)C z4%pRO#$S-88y4hT*!>B!p79}V(3}8`j2GgkgmvSE!%~h0o3%=ZvptXBoO+yre{OgB z&cV?ZOI}U3Sn}l8h^qwgx~&vgc=;{HHH3cw?9%%%;g<+c2X^_!_-}2{wBIMPzZ+q; z!+02J@(8nr@pqw-ep1pUyz3_&#wC{JJLySK7; z+)A2X6J`zL=SlM>Vb**Z!?UJGc9cK+6CO*L^>O&582#DAFC%;tVYc}o@#_h*hVfgZ z`IIng7~_YWO3ANtTy>W+^`3Q~MJF zd7p#POaSX_P9U2{C*jC`A=#f8$h|gWXJT-Q#S+)K_?)G2O%CK;5Jn@Tm(x!s`^OPJ zUZ3MN`YFLlUVlPxnZ;sX-YZe9_NNB&P6?x*iZjmsRI+(W5{}Yxs!xm9EW&58+B`jw z_fZ&|r;ACCqx}D#w<%%KeFs-+*!h9Ho5F;hFF`$y!k+8H<_8M`bT?MBK%ZAJVb2Q= z^89&$l#=1k52jcw@z&t;V~zLXK;C^}G#7()`im*9g@hMTyo)H_#Xeq_CwHp-8v}WV zhOvJm*}su&{*>^|0jjUn-%S4BqR)yLJ1c@}uenX@r{Ql4e(L$#gIhiRMR2djD}yII z{$=o@$9D#Ad3;y!vB&qAXT7yet94%u;#-+HOv^G3tJOZYv#);9d#f@xl#(D|X^n*wPK2EQMyv{>SO z7N0jY-VXzLuZz)q2-fL8q%?kuX?s?S>kdFPDLYz6Cl-b#L^g;AfD7LN3qc7)}Z2`+4UD63q? zW_noSaR?Hu0b^Ww0xf= zEZ5OSzX$oeM|h;?_e{ca?O^nKg{OJEcX$@zg&yw{Ug>dP!u`UZc)nlwq{scks4eG& zZ+m_~_=(5+hZ(+o4Gj13cu;tt$AiOTJRTB8?RRJ>?;$h(4-1!iJUqPCV#!arPVdlP z+Rmu3xKEzdOAqM6jtY12cyx%Yx0=!6!5)tZ5A}F#c%sMS!eWmP4Q2c?{u~xw?(yMN z)_LLmmKWb7f4Zx0`Juc+z=X{w-}0$W9-V|`j5hkha0ua(JU%9zPPp3R_eD zDwio?8oV&}PYCsQHagav7`kyJKbRUG;5Da&`nw#}oEFaU{AuALkH16syJ6Jdo*v%l zHK&JndwfQ?hVTm>pBYNuYkVsTKlONeI3~^UrQu;7mxYr&o)Mnv@mb*_!t!o2V{>N6 zIHR58%fmf9K0DmcaztqF|p}dE|#5Et@JNxsg9$t`y z2YAheR1Yr0;`CYQwv|zBXLv@v^X;&(CF{tb-Xp>j+;Sp6dDf zu*Bo#;cSnu4CP%WChV2rk39Zg;AOQAbi8~SBJfQSzHtD@A0+aSdV`cPVo4q z@C?G2c>I$v!aohK_xvs4J%sP~ctsfDTM7T1@a^FfUVjJGlRLuqJbx$QyTZ>szbfqD z>&AWIzJ%p{btbQV73O<lhE2g0QmOI?!pcl7Tsb!l}d?*K7j&ELp49F1jVV%cMhc|irOn8gO&-yVdKX^Wrxq*rI_eogRU=9DraIi1mSE&BH z63Y6j(foz*tD*i@O#749!kfJ2_3%!Q{~F5sZ%o)X!ryw_7$!V^GyK5gx5BSHemCss z%jMlr-e+U%yhr$N;b_k%!UB(*!jnCIKa}6GnXn&(*ASL>F&h5EFv1^&4|#q|__W6# zhw`pOqyHpKNBJ83k1*Tge}+RmZl-?bi*TanzYNd!_^a@j9{($B@OW$Zvd2N%R*%!t z2DLZwN*$AT$_(i*b*y6`?=3X`bi^6wo4Jo;_)avB>KwTFiP*`;=U}yy8OVDAjb3V} zvy(}4)GUuBtjvv%(y+S*@~%GeQc_$y#n zUYYwvco5-7pmF8J_|P_JP9n`r!ffY~7@jrPljfI%S;KfOXk7q zco<=}b94;Pno`otC(N2Sd=>OLK_@L=(c}Zm<}ixi;?DY;Aj5YF2710L;Vh3~Dp-!s zjcR|_K;CU-!kYfd;mH1OWPf*WCqL*-crTBgeX&!Dc%Fs*TM?JrZUYux-i7rd;dg;u z{x=iuxqrm30PpH2}`79b-*J}|t+=L^8yXxntYvxf2hq!~+?HH=Rp%}m0qxg>^X&Gn?YgD`6tKS`QD z5oQhJ%`uwxqqdzVdq5+Tb2t+njKS%;{v$XCoUAJfmOf{f!Fj<@*m2u1 zVBzII2+klp57?E_4+!5%_))?`fL&h{hc}S^ZNjYoR}9aZJr0O`8%CHljE^SGcL}qG zaSdte2;W4Q^$*7ItoeN#GzrptL743@?sZ_45Bn2l&G;CeHK&p0Y{INr62r6R$E3N3 zFl!h;O`45_S;P1v(zH7$N;_-zj^SA|nlzILvxadAY331T4dd^V<~G8tc`Sx!%}bZzW9++*ok!mo*p4&2L@|pXtNq<4xWrUb9P3?{Tl-W{buC?G{U)_9pCCAuhL70}C(j?O#E7 z4Y13rjfCGP+#WZdo#q_yuCI&3yOVw}VbFk=@ErCh&`4h={f6+auXA{J^eF~+)Zgei{0q-#1b0|0eN=H182+O>kJ0 z56m;(oiu|9vxf1Jq&b-|YZ%WW%|gPgxgmyU&96wao-k_|zd@Rh3A2WAm&2pH$|lU3 zLt=Q=Od(A%Vb(CdfHcbqvxf0)F`6|oJcs=wG}1>&eT$~+*9Y( zM&C2I$Md@dj}d;us%$i9tJZnlxb1q@l{2+#B%}UZdN|-f&j^SDJ0co}pW)0)rj*RkZ5MkCZ9#5K6 z2$vIPeQgZSnjg18^J~(qC(L#jHkmegslSVHxP55CO8?dqxI@0A>pOw`x2e&5 z2WOo9@96ri;lJy#gdK`criMK;kpDk5nlmM+$C(}%wF=u?!_L(IS~X#3QrMaL+akl8 zcX|kxc%98s;9|tvA9mc911!A!@9If}&jP;0ik1?-f$;r=_XS>I^^Bh;%|^nkVf+bc z+F@{WKCp)I-fhr~Ce1{`Y^NlKXUzqqxsotzR>ttGd7Lya5oXPYF+6M9Pl)noH^Qu8 zJd8AXgjvJ*G}6o=yofOCZ;att^Q$&!o+r&4gxL<`f0Cy2(NUhThH(yQ4k0|5Fze5X z;aRhUG}jSkO&neoqhBAxzY)X#BZlXAcPTV|vb1;MU7zf*%sCD2pzF5|_a`2YQ&>&M z0Jl0k#+29M4E+t7gv|lB-14Q^{~ht}10USh7g%`tzs}PvJ{!DApW8Qhvp%QB2g`9DqLPrEd@>2Z`l9n&J5 zkrw4oMp~3Vod|bM!&0EN(}i%?v`L=tmL{W?#4GtT6Q7?b?wKb4<7xcqN&fUq%kp7& zC)_7(56|zBHo)V((r`P(+Sw=V7?1m=eb3|Uv~rL8rCsFlzG-zH_fPwY$2n|71}u-X}!CjU=r?2IHkBPr}?!eb~O z#-p(mp=z2cAEY(tcFheV)%xd(`6zX>VIB`S1}w z&#M2&q{;uNns|>P|BoS`ClQ{UHiPV31N>*Tb7GqO@2atLBH1}Ht&7#?2d5JLPTH=X z|85!{JFsETNRz(B*f}$8s>jo)J`_`Zm`-_8LiM36?Oq#J()$QLe^Wop)8zlwjs0@+ zvz&aJm9~qIceXDhu_JzdrgrA0$^Xe3J9Ej-T(VO|xH_%U+7vrCSzLVdG5|)0(@PqX|6$TH%jN9?U zf{Q)Q4Qf3ek%VvX{Kz1}qx84f#^!|33FJ+2em)uOAFA`LmbBk`IT%&LY@!+hSnh^T4kn{3Ni;hnER& zA-oXSxyS#xO&&zhf-=03u#Vf+jz`sqdmO`ksNj zr^4V~f&8vnu$1ozi=`Y_jmyJ#?##+LWx1ur=a!dN=T51rJiDx#H?IQ!hF$u# z>5GhOv~w$~%NHaC*mf4BEBZ)^v#!c5CNS#wdo*EiT`#deBNgr?_Tj z8{E_;%*?gJGMp=c*J?V|pyt~8Au~n>QOlQ35=YVr>~MdneMRn!8MEhA z&)nIQL=|*rPm-c3@9atRYT!S)Cn+j2ZM|q-#q7$GS!Jctr5NfP_91iU)s*b)L3Z{a z$(O@Xe(ao!L|3N&$vud)^KaNyVA4@JuV!aw5jBpT(~X_ejihD7sA~L2U%2eh3P;V% zs~UG%CcpVQV$PaXMEIaghN7M7XY z=&xEy5#rleQ;<3~XHId&_PEkY-+E0zU2aXMSY}XV3jV*NU#u>xIeX6B)>)B)$IXLn zFHWVppr_{jZ7!?nt%S(7=4#SvNZg_W^TjolbIQ$9dXnoi=GBy$6<$`m)+ikE%rsh= z9~2i=pIbavE-!%Q;?@Pw4s{1*y8e&v@~X=JfOY>JOQS6D7Zu=J`#V~OZTp&QiVjUH zK+9VgTzb*MlSaP7^Ez3x&n-s%Xq|xVS?X=wBY$hHBSq)ZveQYb?a235x0ANfysG9^ z6q!6InlZ1Uq^7*GqFU>}=F1M}E&9N+D!Co$3QKN%xf;^Cchyw2rnhUDh^tEOCQnh3 z^`)q)Y{&1_%t6;wS&}O&pOt3mz@-ZDfBW^`8E>68Fx}zE8Rn;{` zm_UjQilXw0(y|3f2SxLAu2i;QZgE9v(abV4bt=TOXrtYmnHXp+t;+?hAV%G(xy^RHR88@6w+GRa;oJU zu-vmx>g8Hi;v$r?=7SUh!EcbSbBgCCW$L#&1Bk{4w4(o6*N*=Yg^j||lD7MAP|Q_j z{|N>CA5kcpl{-F})81@e#m?f;hbF&yhp1Vpt;C}pY9}(u)F-B9%+?MuX{^_it=AHj z{@Na!dcYJbiQ97lv#p%ko-61!tVg*)Ddpr!(%oW0-g2ml60T>qE5y!+rL=9Ndh%%- z*_HjkiOMRn0!gL2Evn%b4bZ1|o6?SJ|vGe}q|Hk{1Sjd#gL6J82 z9iI8A4cxCZwyMj@X6>A3M6-mQeaFte<9}n{ft9t2va?3YN*Oo(Z>bkKx2n9xEQ;-% zg6y1v_@5U>-KtLWcRrE*zu<{%Gi}fY{|!5j89N`sP9AG_&OMUzV`rz~pPZ1zlU6_Q z+QuSGEB$}h55lyeOJ|`V`Y{KX9bEp9g~QxAWphgAo|kmjXuF;jKu^}{naXc-C3HuZ zCQZ6_xO4Rnq-y!M`sMkzb(cRy=KWv2@&7;Wrs99+qIrr3Pqm*)!MCZkCS6ecx8C(m zeu@T#?tcZ#_Y32mu4&U_8bw9YXr<{FKnFZhI!BpxNW~I1P+u1E7-9Ou| z#}BlAyQdAKYo49m0zLYfqWSOa7VwyCo4bWs`oz5wELzFK_I>5e4o|N1c~2~!>t25H zQ!EcoC(S{(`AF!uIHIMP!fLDfCXaR{CvC@bN*}VHURm}3;YR{)zLVkyN17dYaIp28 zR!sNfe$-Sfp6+mB?W55}MboRy-4yT9_B^2wSDLcqVjpwhnMO1N(Rg>*d6R3oRmH6y zY;>6ujcM`O8S8vxB(?Jz?~XgKKWdU|!8v&ENK!*=)AW)G%3KUuOW%S+81lXaf1E&1Dsm-pYgZLq!3+ z@vPrW`~cE7$M9YINAV6O{Z+)@O#JmWGVzn`uOfaZ>DR>Yr|oA%Vn^QP?zX3hXFIPE zKirA2PsH#KSm(t4Eu_DGe{&!_+gU}tyrbT2Yhw7DhZu#}Ilzf!Z=Dui{+GdRvxz_0 zk=Q>^{21c1hnWMhGnV)>h#yD%OT^=^H2rp_s zk0gHXNOK_ieByIPMSKDA8;PGl{J;YvJ^q@-Z%+|_t7q|^+tKDg;{7G@U)f9)ekJkK zY-S062=Ob4KZ^Kwh{xX*_$}LJuGq1z>A9K2Ti2AojrgQ`MLb^V?zi8~Yz74e@D&-T08$|QCslfHKhUt=qo z=E$zsE%{l&f1ND1z3}0v~tJt}l^h=0;fcOUD*}nWwmuzC^1V!fSoftmL z7Dh|`K;lm#I|s(_(`+V+ooh*d6Y*^40pjI9LEP35!*8~U5&Ne)G4=ybGzY?8L;N!0 z+5XMMpGNw-V)%`xME0*H{ZQ+K#LIT_iT^I?r^N6Vo*vo1iu9im|7+rBpAqTV{u1IX zRN+-Ie9iYFJ9m=(NoPhp+bJd9LKV)9;ZL3(*}0AE4=suKV&Z2KZ-eUD+lZe=`bOf} zpJw7M)KKk9O<*Z6<`ZW{{Lc;4?`bn5p7jfeFC+bu82&2aZzp|rd1U`V;#U#>5b=3u zNBY&o2eTsnH^dhb|1j|xvm^Z@#7`l94e`yyR}h~!C$jS>@ym$k{JELp38u2xxPsH#yonu5|zk&4c z5YKkLB7Q#UGprM0hxsdr{{`vS6VLia;uny9a|}PX+K43HCrE!6@oeWQ;?F1jh8RA_ zCSL46PWlIkXFJame#bhnyL*e}HAtF311XZbYj7ZGoV zM;(7Jwi?+TXR+un!N=M8k>$mHE%Eop@Xryyg!CWA@F#@kgv7g)^i`IZc-hV}(o5Q% zpH~roDe-rboy&-SJcfUs_+_O3Acp^fc=pr$LtPNG^uNC(6kAJvTnsfXa@a1|JO0oZ{-?y(lm0gy#7b*_-nP8N%lX;8qslej-2h#@qbx7!T2B0g82$vZ za|P+oCOzlpBI2(k{rAZZ`}xZl{hx?u`&(n|49-Ag*d$#XZ=U5Pe>fkGCq3IQBcAQl zkRA5_;TZkLq`wMryZrAf4q_93WU0pS#}J=Qd@=DqB>uO=-$eYsiNB8cew}fEP2y#K zH1UIQ&iOMbhCjvf5)JF;$LL>*;dhq~5}VlJ{2v{|Pm1BETVB#N1Rv-B*~D{sEhV1) z|1t3c$<7MPw@mL{q-Q&C6MqZY>ChDi*u>5de4L;ASYG^RJ3~p&{4p{5B{BR;vcvJd zLi!ct&#~QbfKB{i{vzUU#X0ByiPE`alXP)8F0{PZIS?PGf0X#!i2rkpo%Wd!VG}!} z@o{!W6aRDKkF$JBe-@B_6!g9Dk*%Ki190r@-%dR1{}RK06~phFrO~MWZ0C5(xAe0% zMt>dg2a%um6MqQtyY-0Ty`A{V82+cktDhZA_}hu+{CS9Y(Yt*6OAOzkXXFp-`w;&N zvNN4{)-SQVw$S&o{$EEt=iBYXiyf!Ghj`XMNBj}w=La$T--+k?-&-aD*rYz^ z;p6gcU*g&R2;#ZDOeUW7Q;FyHat`sF&r2-dGCyx1y)4l=KOZNa<6TEQ$NM|tIlUhc z&vu%L=XTxAHrSSa=38F!b0ztC67g*RbmH01V&d7)#l*9nD~M-5*Al;i{5gJC9AFba zN8w|;-{4f@j{@)V`3vG@$=2~1yWs$v*f|&<#}6m|NaDv5KZf|@Vt997Ozd;KD@f1r zzDGRA`!VqxZ-?Ds7@OE<{#oKV-UasNndrHEA18i1&bfRzb`Kn26a5|dIQ}=pA4+^9 z@mwzL_k;+W*f|^@XD5qzZWl)qUqJdNiI@8w&dv+ObAE0nUi`7uDcE%{9AFcFn9nDE z9L_oWHN;OK{#xRXB>rc_bGh6b!>=R$XtL98ZyaC~KY5-olz1-3qlxEo{Fr#QGjJa@ zulDZ*bm={ic((sj;_o8;F?}OD`NTg%KYfUoda{=t2m29!0NI~HJnK&*p2xuw;<=tw63_MjOX4}7f6_ln z7svZ^;yK>+#B=@sJcgf`lWgDhw-?3mONlR}{JDyFPVX=Gi|k)d`Y&Sm+5wUN$E45N zKQ+JSz=*$(^o7L#iuk*TzmfR&iT@Gt`wqeZwlsX0{~qzz;hd|V?-9@G%^56KjGa@l zclw8jXMK0M2*K6~pC$M>{TR!OKbI2!TjDP#{__|+{f9z?t)-n+#Iv1She!HpfX<(1 zh%Y97U~Z(pf%tslrxSm~h)B=*a1HTikv?r?q@PKA4e{l~UnGqIo22(_e4PI)iJwLM z)5NbP{@>CmU=usD@o{zrA1JKh=V0&nLgF7Lex5X1Y+|PpAE&>Y_zL2mBYq9><3{5E zo7i~_A7`hE_{WL=hjfD2MBjjq(~lm518gnp+i@{`De-F&&e>UFd9nWl@edHs?d5gi ze@Oc7G6}#YcDUU=L;RgM=i(KuY#}~ZC^BEuEEZm-4DIdldg8CPSautTzlQim;;$t> zLA)L6mDv)*HxqB0zB0WnFT>D6i)EK>vGBDP%g%OJ`pls84{o!~MfuAdiM`Ec<(Cnk zXR+8{PJAKpR}w#kc-z$}a~kos%TZ<;@wQ1RGn06!KYkmIw};mR!%K^6ii6?PtE+?I z)#uHrDV~mfO_kl7qvGjcg5gzVvx{*~IE5y>LSqE)gw^-T7FSgjpBD^gjQ>3@FS7hu zm55e@DXN}nFFp#<@AIW3cRt0%>uRC)AVWI3Y2n#$B9|15s3?*b=Wi<1wqzfx=XtMD2z+L zYwO)C=fW?t;TPHP4PA^K_wB;Fw!% z`k`bHZ7s4R*cazoaw4B%8{PHuO<1N%;Kk#t(C%8sB}1x96D!y3 zZIp=~hLw0blh&pGP~wczeBYL9!jIX_97}j%TwE^v(Zm_0`F^dygm18mfINPPVQWsHZvPL@F_=OC diff --git a/05/test.c b/05/test.c index 50e5cc8..374f109 100644 --- a/05/test.c +++ b/05/test.c @@ -2,6 +2,6 @@ #include int main(void) { - putc('a', stdout); + printf("Hello, World!\n"); return 0; } diff --git a/06/.gitignore b/06/.gitignore deleted file mode 100644 index cf83954..0000000 --- a/06/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -lua-bootstrap -**/*.o -zsh \ No newline at end of file diff --git a/06/Makefile b/06/Makefile deleted file mode 100644 index 519cb7a..0000000 --- a/06/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC= ../05/tcc-0.9.27/tcc -TCCINST= ../05/tcc-bootstrap -MUSLINST= ../05/musl-bootstrap -LUAINST= deps/lua-bootstrap - -CFLAGS= -I $(MUSLINST)/include -I $(LUAINST)/include -I deps/lpeglabel -SRCS = $(sort $(wildcard **/*.c)) -OBJS = $(SRCS:.c=.o) - -all: test.out #zsh - -test.out: test.s.o test.c.o - $(TCC) -static -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include ../musl-bootstrap/lib/*.[oa] test.s.o test.c.o -o test.out -test.s.o: $(TCC0) test.s - $(TCC) -static -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include ../musl-bootstrap/lib/*.[oa] -c test.s -o test.s.o -test.c.o: $(TCC0) test.c - $(TCC) -static -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include ../musl-bootstrap/lib/*.[oa] -c test.c -o test.c.o - -# deps/lua-bootstrap/lib/liblua.a: -# $(MAKE) -j8 -C deps/lua-5.4.6 -# $(MAKE) -C deps/lua-5.4.6 install - -# deps/lpeglabel/lpeglabel.a: deps/lua-bootstrap/lib/liblua.a -# $(MAKE) -C deps/lpeglabel - -# %.o: %.c deps/lua-bootstrap/lib/liblua.a -# $(CC) $(CFLAGS) -c -o $@ $< - -# zsh: $(OBJS) deps/lua-bootstrap/lib/liblua.a deps/lpeglabel/lpeglabel.a -# $(CC) -nostdlib -B $(TCCINST) -o zsh $(OBJS) deps/lpeglabel/lpeglabel.a $(LUAINST)/lib/liblua.a $(MUSLINST)/lib/*.[oa] - -# run: zsh -# ./zsh - -c: - rm -f zsh - rm -f src/*.o - rm -f deps/lpeglabel/*.o - -clean: c - rm -rf deps/lua-bootstrap - $(MAKE) -C deps/lua-5.4.6 clean - $(MAKE) -C deps/lpeglabel clean - diff --git a/06/README.md b/06/README.md deleted file mode 100644 index 2922ed5..0000000 --- a/06/README.md +++ /dev/null @@ -1,18 +0,0 @@ -TODO: - -Fix 05 stage tcc compiler that causes some non-descript linker error whenever a -generated executable is executed. Causes this error message : "Inconsistency -detected by ld.so: dl-fini.c: 92: _dl_fini: Assertion `ns != LM_ID_BASE || i == -nloaded' failed!" - -Write rules into makefile to run tests -1. Create test runner if it doesn't exist for lpeglable and parser-gen -2. Update tests to lua 5.4 -2. Fix diagnostics in tofix -3. Write a 'make test' rule - -Implement backslashed escaped characters in grammar parser - -Implement single line comments starting with # in grammar parser - - diff --git a/06/deps/lpeglabel/.gitignore b/06/deps/lpeglabel/.gitignore deleted file mode 100644 index 0e425ae..0000000 --- a/06/deps/lpeglabel/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -lpeglabel.so -lpeglabel.a -*.o \ No newline at end of file diff --git a/06/deps/lpeglabel/HISTORY b/06/deps/lpeglabel/HISTORY deleted file mode 100644 index 13eb31c..0000000 --- a/06/deps/lpeglabel/HISTORY +++ /dev/null @@ -1,32 +0,0 @@ -HISTORY for LPegLabel 1.2.0-1 - -* Changes from version 1.1.0-1 to 1.2.0-1 - --------------------------------- - + fixes bug when reporting the error position of an ordinary failure - + reports the farthest failure position for ordinary failures - - -* Changes from version 1.0.0-1 to 1.1.0-1 - --------------------------------- - + new semantics of lpeglabel.Rec - - labeled ordered choice removed - + examples updated - - -* Changes from version 0.12.2-2 to 1.0.0-1 - --------------------------------- - + base code updated to LPeg 1.0 - + new functions: lpeglabel.Rec and relabel.calcline - + improved documentation - + new examples - + some bugs fixed - - -* Changes from version 0.12.2-1 to 0.12.2-2 - --------------------------------- - + in case of a failure, "match" also returns a suffix of the input - + improved error reporting for "relabel", thanks to Matthew Allen - + limit of labels increased from 32 to 64, thanks to André Maidl - + compiles with Lua 5.1, thanks to Matthew Allen - + the throw operator now throws only one label - + some bugs fixed diff --git a/06/deps/lpeglabel/LICENSE b/06/deps/lpeglabel/LICENSE deleted file mode 100644 index 27faf04..0000000 --- a/06/deps/lpeglabel/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2014-2016 Sérgio Medeiros - -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/06/deps/lpeglabel/README.md b/06/deps/lpeglabel/README.md deleted file mode 100644 index ddad3c8..0000000 --- a/06/deps/lpeglabel/README.md +++ /dev/null @@ -1,727 +0,0 @@ -

      LPegLabel

      - -## LPegLabel - Parsing Expression Grammars (with Labels) for Lua - ---- - -### Introduction - -LPegLabel is a conservative extension of the -[LPeg](http://www.inf.puc-rio.br/~roberto/lpeg) -library that provides an implementation of Parsing -Expression Grammars (PEGs) with labeled failures. -Labels can be used to signal different kinds of errors -and to specify which recovery pattern should handle a -given label. Labels can also be combined with the standard -patterns of LPeg. - -Besides that, LPegLabel also reports the farthest -failure position in case of an ordinary failure -(which is represented by label **0**). - -This document describes the new functions available -in LpegLabel and presents some examples of usage. - -With labeled failures it is possible to distinguish -between an ordinary failure and an error. Usually, an -ordinary failure is produced when the matching of a -character fails, and this failure is caught by ordered choice. -An error (a non-ordinary failure), by its turn, is produced -by the throw operator and may be caught by the recovery operator. - -In LPegLabel, the result of an unsuccessful matching -is a triple **nil, lab, sfail**, where **lab** -is the label associated with the failure, and -**sfail** is the suffix input being matched when -**lab** was thrown. - -When **lab** is an ordinary failure and no error was thrown before, -**sfail** is formed according to the farthest position where an -ordinary failure occurred. -In case **lab** is an ordinary failure and an error -was thrown before, **sfail** is the farthest suffix -where an ordinary failure occurred after the last error. - -Below there is a brief summary of the new functions provided by LpegLabel: - -
  • - - - - - - - - - - - - - -
    FunctionDescription
    lpeglabel.T (l)Throws a label l to signal an error
    lpeglabel.Rec (p1, p2, l1 [, l2, ..., ln])Specifies a recovery pattern p2 for p1, - when the matching of p1 gives one of the labels l1, ..., ln.
    %{l}Syntax of relabel module. Equivalent to lpeglabel.T(l) -
    p1 //{l1 [, l2, ..., ln} p2Syntax of relabel module. Equivalent to lpeglabel.Rec(p1, p2, l1, ..., ln) -
    relabel.calcline(subject, i)Calculates line and column information regarding position i of the subject -
    relabel.setlabels (tlabel)Allows to specicify a table with mnemonic labels. -
    - - -### Functions - - -#### lpeglabel.T(l) - -Returns a pattern that throws the label `l`. -A label must be an integer between 1 and 255. - -This pattern always causes a failure, whose associated -position will be used to set **sfail**, no matter -whether this is the farthest failure position or not. - - -#### lpeglabel.Rec(p1, p2, l1, ..., ln) - -Returns a *recovery pattern*. -If the matching of `p1` gives one of the labels `l1, ..., ln`, -then the matching of `p2` is tried from the failure position of `p1`. -Otherwise, the result of the matching of `p1` is the pattern's result. - - -#### %{l} - -Syntax of *relabel* module. Equivalent to `lpeg.T(l)`. - - -#### p1 //{l1, ..., ln} p2 - -Syntax of *relabel* module. Equivalent to `lpeglabel.Rec(p1, p2, l1, ..., ln)`. - -The `//{}` operator is left-associative. - - - -#### relabel.calcline (subject, i) - -Returns line and column information regarding position i of the subject. - - -#### relabel.setlabels (tlabel) - -Allows to specicify a table with labels. They keys of -`tlabel` must be integers between 1 and 255, -and the associated values should be strings. - - -### Examples - -Below there a few examples of usage of LPegLabel. -The code of these and of other examples is available -in the *examples* directory. - - -#### Reporting the farthest failure - -This example illustrates the new values returned -by the *match* function in case of an unsuccessful -matching. As no error is thrown, when the matching -fails *sfail* represents the farthest suffix where -an ordinary failure occurred. - -```lua -local m = require'lpeglabel' - -function matchPrint(p, s) - local r, lab, sfail = p:match(s) - print("r: ", r, "lab: ", lab, "sfail: ", sfail) -end - -local p = m.P"a"^0 * m.P"b" + m.P"c" -matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil -matchPrint(p, "c") --> r: 2 lab: nil sfail: nil -matchPrint(p, "aac") --> r: nil lab: 0 sfail: c -matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc -``` - -#### Matching a list of identifiers separated by commas - -The following example defines a grammar that matches -a list of identifiers separated by commas. A label -is thrown when there is an error matching an identifier -or a comma. - -We use function `newError` to store error messages in a -table and to return the index associated with each error message. - - -```lua -local m = require'lpeglabel' -local re = require'relabel' - -local terror = {} - -local function newError(s) - table.insert(terror, s) - return #terror -end - -local errUndef = newError("undefined") -local errId = newError("expecting an identifier") -local errComma = newError("expecting ','") - -local g = m.P{ - "S", - S = m.V"Id" * m.V"List", - List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List", - Id = m.V"Sp" * m.R'az'^1, - Comma = m.V"Sp" * ",", - Sp = m.S" \n\t"^0, -} - -function mymatch (g, s) - local r, e, sfail = g:match(s) - if not r then - local line, col = re.calcline(s, #s - #sfail) - local msg = "Error at line " .. line .. " (col " .. col .. "): " - return r, msg .. terror[e] .. " before '" .. sfail .. "'" - end - return r -end - -print(mymatch(g, "one,two")) --> 8 -print(mymatch(g, "one two")) --> nil Error at line 1 (col 3): expecting ',' before ' two' -print(mymatch(g, "one,\n two,\nthree,")) --> nil Error at line 3 (col 6): expecting an identifier before '' -``` - -In this example we could think about writing rule List as follows: -```lua -List = ((m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)))^0, -``` - -but when matching this expression against the end of input -we would get a failure whose associated label would be **errComma**, -and this would cause the failure of the *whole* repetition. - - - -#### Error Recovery - -By using the `Rec` function we can specify a recovery pattern that -should be matched when a label is thrown. After matching the recovery -pattern, and possibly recording the error, the parser will resume -the regular matching. For example, in the example below -we expect to match rule `A`, but when a failure occur the label 42 -is thrown and then we will try to match the recovery pattern `recp`: -```lua -local m = require'lpeglabel' - -local recp = m.P"oast" - -local g = m.P{ - "S", - S = m.Rec(m.V"A", recp, 42) * ".", - A = m.P"t" * (m.P"est" + m.T(42)) -} - -print(g:match("test.")) --> 6 -print(g:match("toast.")) --> 7 -print(g:match("oast.")) --> nil 0 oast. -print(g:match("toward.")) --> nil 0 ward. -``` -When trying to match subject 'toast.', in rule `A` the first -'t' is matched, then the matching of `m.P"est"` fails and label 42 -is thrown, with the associated inpux suffix 'oast.'. In rule -`S` label 42 is caught and the recovery pattern matches 'oast', -so pattern `'.'` matches the rest of the input. - -When matching subject 'oast.', pattern `m.P"t"` fails, and -the result of the matching is nil, 0, oast.. - -When matching 'toward.', label 42 is thrown after matching 't', -with the associated input suffix 'oward.'. As the matching of the -recovery pattern fails, the result is nil, 0, ward.. - -Usually, the recovery pattern is an expression that does not fail. -In the previous example, we could have used `(m.P(1) - m.P".")^0` -as the recovery pattern. - -Below we rewrite the grammar that describes a list of identifiers -to use a recovery strategy. Grammar `g` remains the same, but we add a -recovery grammar `grec` that handles the labels thrown by `g`. - -In grammar `grec` we use functions `record` and `sync`. -Function `record`, plus function `recorderror`, will help -us to save the input position where a label was thrown, -while function `sync` will give us a synchronization pattern, -that consumes the input while is not possible to match a given -pattern `p`. - -When the matching of an identifier fails, a defaul value ('NONE') -is provided. - -```lua -local m = require'lpeglabel' -local re = require'relabel' - -local terror = {} - -local function newError(s) - table.insert(terror, s) - return #terror -end - -local errUndef = newError("undefined") -local errId = newError("expecting an identifier") -local errComma = newError("expecting ','") - -local id = m.R'az'^1 - -local g = m.P{ - "S", - S = m.V"Id" * m.V"List", - List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", - Id = m.V"Sp" * id + m.T(errId), - Comma = m.V"Sp" * "," + m.T(errComma), - Sp = m.S" \n\t"^0, -} - -local subject, errors - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = terror[lab] }) -end - -function record (lab) - return (m.Cp() * m.Cc(lab)) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -local grec = m.P{ - "S", - S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), - ErrComma = record(errComma) * sync(id), - ErrId = record(errId) * sync(m.P",") -} - - -function mymatch (g, s) - errors = {} - subject = s - local r, e, sfail = g:match(s) - if #errors > 0 then - local out = {} - for i, err in ipairs(errors) do - local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg - table.insert(out, msg) - end - return nil, table.concat(out, "\n") .. "\n" - end - return r -end - -print(mymatch(grec, "one,two")) --- Captures (separated by ';'): one; two; --- Syntactic errors found: 0 - -print(mymatch(grec, "one two three")) --- Captures (separated by ';'): one; two; three; --- Syntactic errors found: 2 --- Error at line 1 (col 4): expecting ',' --- Error at line 1 (col 8): expecting ',' - -print(mymatch(grec, "1,\n two, \n3,")) --- Captures (separated by ';'): NONE; two; NONE; NONE; --- Syntactic errors found: 3 --- Error at line 1 (col 1): expecting an identifier --- Error at line 2 (col 6): expecting an identifier --- Error at line 3 (col 2): expecting an identifier - -print(mymatch(grec, "one\n two123, \nthree,")) --- Captures (separated by ';'): one; two; three; NONE; --- Syntactic errors found: 3 --- Error at line 2 (col 1): expecting ',' --- Error at line 2 (col 5): expecting ',' --- Error at line 3 (col 6): expecting an identifier -``` - -##### *relabel* syntax - -Below we describe again a grammar that matches a list of identifiers, -now using the syntax supported by *relabel*, where `//{}` is the -recovery operator, and `%{}` is the throw operator: - -```lua -local re = require 'relabel' - -local errinfo = { - {"errUndef", "undefined"}, - {"errId", "expecting an identifier"}, - {"errComma", "expecting ','"}, -} - -local errmsgs = {} -local labels = {} - -for i, err in ipairs(errinfo) do - errmsgs[i] = err[2] - labels[err[1]] = i -end - -re.setlabels(labels) - -local g = re.compile[[ - S <- Id List - List <- !. / Comma Id List - Id <- Sp {[a-z]+} / %{errId} - Comma <- Sp ',' / %{errComma} - Sp <- %s* -]] - -local errors - -function recorderror (subject, pos, label) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = errmsgs[labels[label]] }) - return true -end - -function sync (p) - return '( !(' .. p .. ') .)*' -end - -local grec = re.compile( - "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. - "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('[a-z]+') .. "\n" .. - "ErrId <- ('' -> 'errId' => recorderror) " .. sync('","') .. "-> default" - , {g = g, recorderror = recorderror, default = "NONE"} -) - -function mymatch (g, s) - errors = {} - subject = s - io.write("Input: ", s, "\n") - local r = { g:match(s) } - io.write("Captures (separated by ';'): ") - for k, v in pairs(r) do - io.write(v .. "; ") - end - io.write("\nSyntactic errors found: " .. #errors) - if #errors > 0 then - io.write("\n") - local out = {} - for i, err in ipairs(errors) do - local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg - table.insert(out, msg) - end - io.write(table.concat(out, "\n")) - end - print("\n") - return r -end - -print(mymatch(grec, "one,two")) --- Captures (separated by ';'): one; two; --- Syntactic errors found: 0 - -print(mymatch(grec, "one two three")) --- Captures (separated by ';'): one; two; three; --- Syntactic errors found: 2 --- Error at line 1 (col 4): expecting ',' --- Error at line 1 (col 8): expecting ',' - -print(mymatch(grec, "1,\n two, \n3,")) --- Captures (separated by ';'): NONE; two; NONE; NONE; --- Syntactic errors found: 3 --- Error at line 1 (col 1): expecting an identifier --- Error at line 2 (col 6): expecting an identifier --- Error at line 3 (col 2): expecting an identifier - -print(mymatch(grec, "one\n two123, \nthree,")) --- Captures (separated by ';'): one; two; three; NONE; --- Syntactic errors found: 3 --- Error at line 2 (col 1): expecting ',' --- Error at line 2 (col 5): expecting ',' --- Error at line 3 (col 6): expecting an identifier -``` - - -#### Arithmetic Expressions - -Here's an example of an LPegLabel grammar that matches an expression. -We have used a function `expect`, that takes a pattern `patt` and a label as -parameters and builds a new pattern that throws this label when `patt` -fails. - -When a subexpression is syntactically invalid, a default value of 1000 -is provided by the recovery pattern, so the evaluation of an expression -should always produce a numeric value. - -In this example, we can see that it may be a tedious and error prone -task to build manually the recovery grammar `grec`. In the next example -we will show how to build the recovery grammar in a more automatic way. - -```lua -local m = require"lpeglabel" -local re = require"relabel" - -local labels = { - {"ExpTermFirst", "expected an expression"}, - {"ExpTermOp", "expected a term after the operator"}, - {"MisClose", "missing a closing ')' after the expression"}, -} - -local function labelindex(labname) - for i, elem in ipairs(labels) do - if elem[1] == labname then - return i - end - end - error("could not find label: " .. labname) -end - -local errors, subject - -local function expect(patt, labname) - local i = labelindex(labname) - return patt + m.T(i) -end - - -local num = m.R("09")^1 / tonumber -local op = m.S("+-") - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - -local g = m.P { - "Exp", - Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, - OperandFirst = expect(m.V"Term", "ExpTermFirst"), - Operand = expect(m.V"Term", "ExpTermOp"), - Term = num + m.V"Group", - Group = "(" * m.V"Exp" * expect(")", "MisClose"), -} - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = labels[lab][2] }) -end - -function record (labname) - return (m.Cp() * m.Cc(labelindex(labname))) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue (p) - return p or m.Cc(1000) -end - -local grec = m.P { - "S", - S = m.Rec(m.V"A", m.V"ErrExpTermFirst", labelindex("ExpTermFirst")), - A = m.Rec(m.V"Sg", m.V"ErrExpTermOp", labelindex("ExpTermOp")), - Sg = m.Rec(g, m.V"ErrMisClose", labelindex("MisClose")), - ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), - ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), - ErrMisClose = record("MisClose") * sync(m.P")") * defaultValue(m.P""), -} - -local function eval(input) - errors = {} - io.write("Input: ", input, "\n") - subject = input - local result, label, suffix = grec:match(input) - io.write("Syntactic errors found: " .. #errors, "\n") - if #errors > 0 then - local out = {} - for i, err in ipairs(errors) do - local pos = err.col - local msg = err.msg - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - print(table.concat(out, "\n")) - end - io.write("Result = ") - return result -end - -print(eval "90-70-(5)+3") --- Syntactic errors found: 0 --- Result = 18 - -print(eval "15+") --- Syntactic errors found: 1 --- syntax error: expected a term after the operator (at index 3) --- Result = 1015 - -print(eval "-2") --- Syntactic errors found: 1 --- syntax error: expected an expression (at index 1) --- Result = 998 - -print(eval "1+()+") --- Syntactic errors found: 2 --- syntax error: expected an expression (at index 4) --- syntax error: expected a term after the operator (at index 5) --- Result = 2001 - -print(eval "1+(") --- Syntactic errors found: 2 --- syntax error: expected an expression (at index 3) --- syntax error: missing a closing ')' after the expression (at index 3) --- Result = 1001 - -print(eval "3)") --- Syntactic errors found: 0 --- Result = 3 -``` - -#### Automatically Building the Recovery Grammar - -Below we rewrite the previous example to automatically -build the recovery grammar based on information provided -by the user for each label (error message, recovery pattern, etc). -In the example below we also throw an error when the grammar -does not match the whole subject. - -```lua -local m = require"lpeglabel" -local re = require"relabel" - -local num = m.R("09")^1 / tonumber -local op = m.S("+-") - -local labels = {} -local nlabels = 0 - -local function newError(lab, msg, psync, pcap) - nlabels = nlabels + 1 - psync = psync or m.P(-1) - pcap = pcap or m.P"" - labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } -end - -newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) -newError("ExpTermOp", "expected a term after the operator", op + ")", m.Cc(1000)) -newError("MisClose", "missing a closing ')' after the expression", m.P")") -newError("Extra", "extra characters found after the expression") - -local errors, subject - -local function expect(patt, labname) - local i = labels[labname].id - return patt + m.T(i) -end - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - -local g = m.P { - "Exp", - Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, - OperandFirst = expect(m.V"Term", "ExpTermFirst"), - Operand = expect(m.V"Term", "ExpTermOp"), - Term = num + m.V"Group", - Group = "(" * m.V"Exp" * expect(")", "MisClose"), -} - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = labels[lab].msg }) -end - -function record (labname) - return (m.Cp() * m.Cc(labname)) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue (p) - return p or m.Cc(1000) -end - -local grec = g * expect(m.P(-1), "Extra") -for k, v in pairs(labels) do - grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id) -end - -local function eval(input) - errors = {} - io.write("Input: ", input, "\n") - subject = input - local result, label, suffix = grec:match(input) - io.write("Syntactic errors found: " .. #errors, "\n") - if #errors > 0 then - local out = {} - for i, err in ipairs(errors) do - local pos = err.col - local msg = err.msg - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - print(table.concat(out, "\n")) - end - io.write("Result = ") - return result -end - -print(eval "90-70-(5)+3") --- Syntactic errors found: 0 --- Result = 18 - -print(eval "15+") --- Syntactic errors found: 1 --- syntax error: expected a term after the operator (at index 3) --- Result = 1015 - -print(eval "-2") --- Syntactic errors found: 1 --- syntax error: expected an expression (at index 1) --- Result = 998 - -print(eval "1+()+") --- Syntactic errors found: 2 --- syntax error: expected an expression (at index 4) --- syntax error: expected a term after the operator (at index 5) --- Result = 2001 - -print(eval "1+(") --- Syntactic errors found: 2 --- syntax error: expected an expression (at index 3) --- syntax error: missing a closing ')' after the expression (at index 3) --- Result = 1001 - -print(eval "3)") --- Syntactic errors found: 1 --- syntax error: extra characters found after the expression (at index 2) --- Result = 3 -``` diff --git a/06/deps/lpeglabel/examples/expRec.lua b/06/deps/lpeglabel/examples/expRec.lua deleted file mode 100644 index f70b581..0000000 --- a/06/deps/lpeglabel/examples/expRec.lua +++ /dev/null @@ -1,128 +0,0 @@ -local m = require "lpeglabel" -local re = require "relabel" - -local labels = { - {"ExpTermFirst", "expected an expression"}, - {"ExpTermOp", "expected a term after the operator"}, - {"MisClose", "missing a closing ')' after the expression"}, -} - -local function labelindex(labname) - for i, elem in ipairs(labels) do - if elem[1] == labname then - return i - end - end - error("could not find label: " .. labname) -end - -local errors, subject - -local function expect(patt, labname) - local i = labelindex(labname) - return patt + m.T(i) -end - - -local num = m.R("09")^1 / tonumber -local op = m.S("+-") - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - -local g = m.P { - "Exp", - Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, - OperandFirst = expect(m.V"Term", "ExpTermFirst"), - Operand = expect(m.V"Term", "ExpTermOp"), - Term = num + m.V"Group", - Group = "(" * m.V"Exp" * expect(")", "MisClose"), -} - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = labels[lab][2] }) -end - -function record (labname) - return (m.Cp() * m.Cc(labelindex(labname))) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue (p) - return p or m.Cc(1000) -end - -local grec = m.P { - "S", - S = m.Rec(m.V"A", m.V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 - A = m.Rec(m.V"Sg", m.V"ErrExpTermOp", labelindex("ExpTermOp")), - Sg = m.Rec(g, m.V"ErrMisClose", labelindex("MisClose")), - ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), - ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), - ErrMisClose = record("MisClose") * sync(m.P")") * defaultValue(m.P""), -} - -local function eval(input) - errors = {} - io.write("Input: ", input, "\n") - subject = input - local result, label, suffix = grec:match(input) - io.write("Syntactic errors found: " .. #errors, "\n") - if #errors > 0 then - local out = {} - for i, err in ipairs(errors) do - local pos = err.col - local msg = err.msg - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - print(table.concat(out, "\n")) - end - io.write("Result = ") - return result -end - -print(eval "90-70-(5)+3") ---> 20 - -print(eval "15+") ---> 2 + 0 - -print(eval "-2") ---> 0 - 2 - -print(eval "1+3+-9") ---> 1 + 3 + [0] - 9 - -print(eval "1+()3+") ---> 1 + ([0]) [3 +] [0] - -print(eval "8-(2+)-5") ---> 8 - (2 + [0]) - 5 - -print(eval "()") - -print(eval "") - -print(eval "1+()+") - -print(eval "1+(") - -print(eval "3)") - -print(eval "11+())3") - diff --git a/06/deps/lpeglabel/examples/expRecAut.lua b/06/deps/lpeglabel/examples/expRecAut.lua deleted file mode 100644 index c3d7efb..0000000 --- a/06/deps/lpeglabel/examples/expRecAut.lua +++ /dev/null @@ -1,122 +0,0 @@ -local m = require "lpeglabel" -local re = require "relabel" - -local num = m.R("09")^1 / tonumber -local op = m.S("+-") - -local labels = {} -local nlabels = 0 - -local function newError(lab, msg, psync, pcap) - nlabels = nlabels + 1 - psync = psync or m.P(-1) - pcap = pcap or m.P"" - labels[lab] = { id = nlabels, msg = msg, psync = psync, pcap = pcap } -end - -newError("ExpTermFirst", "expected an expression", op + ")", m.Cc(1000)) -newError("ExpTermOp", "expected a term after the operator", op + ")", m.Cc(1000)) -newError("MisClose", "missing a closing ')' after the expression", m.P")") -newError("Extra", "extra characters found after the expression") - -local errors, subject - -local function expect(patt, labname) - local i = labels[labname].id - return patt + m.T(i) -end - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - -local g = m.P { - "Exp", - Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, - OperandFirst = expect(m.V"Term", "ExpTermFirst"), - Operand = expect(m.V"Term", "ExpTermOp"), - Term = num + m.V"Group", - Group = "(" * m.V"Exp" * expect(")", "MisClose"), -} - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = labels[lab].msg }) -end - -function record (labname) - return (m.Cp() * m.Cc(labname)) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue (p) - return p or m.Cc(1000) -end - -local grec = g * expect(m.P(-1), "Extra") -for k, v in pairs(labels) do - grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id) -end - -local function eval(input) - errors = {} - io.write("Input: ", input, "\n") - subject = input - local result, label, suffix = grec:match(input) - io.write("Syntactic errors found: " .. #errors, "\n") - if #errors > 0 then - local out = {} - for i, err in ipairs(errors) do - local pos = err.col - local msg = err.msg - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - print(table.concat(out, "\n")) - end - io.write("Result = ") - return result -end - -print(eval "90-70-(5)+3") ---> 18 - -print(eval "15+") ---> 2 + 0 - -print(eval "-2") ---> 0 - 2 - -print(eval "1+3+-9") ---> 1 + 3 + [0] - 9 - -print(eval "1+()3+") ---> 1 + ([0]) [+] 3 + [0] - -print(eval "8-(2+)-5") ---> 8 - (2 + [0]) - 5 - -print(eval "()") - -print(eval "") - -print(eval "1+()+") - -print(eval "1+(") - -print(eval "3)") - -print(eval "11+()3") ---> 1 + ([0]) [+] 3 + [0] diff --git a/06/deps/lpeglabel/examples/farthest.lua b/06/deps/lpeglabel/examples/farthest.lua deleted file mode 100644 index 692390f..0000000 --- a/06/deps/lpeglabel/examples/farthest.lua +++ /dev/null @@ -1,14 +0,0 @@ -local m = require'lpeglabel' - -function matchPrint(p, s) - local r, lab, sfail = p:match(s) - print("r: ", r, "lab: ", lab, "sfail: ", sfail) -end - -local p = m.P"a"^0 * m.P"b" + m.P"c" -matchPrint(p, "abc") --> r: 3 lab: nil sfail: nil -matchPrint(p, "c") --> r: 2 lab: nil sfail: nil -matchPrint(p, "aac") --> r: nil lab: 0 sfail: c -matchPrint(p, "xxc") --> r: nil lab: 0 sfail: xxc - - diff --git a/06/deps/lpeglabel/examples/listId1.lua b/06/deps/lpeglabel/examples/listId1.lua deleted file mode 100644 index b7943c1..0000000 --- a/06/deps/lpeglabel/examples/listId1.lua +++ /dev/null @@ -1,33 +0,0 @@ -local m = require'lpeglabel' -local re = require'relabel' - -local id = m.R'az'^1 - -local g = m.P{ - "S", - S = m.V"Id" * m.V"List", - List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", - Id = m.V"Sp" * id + m.T(1), - Comma = m.V"Sp" * "," + m.T(2), - Sp = m.S" \n\t"^0, -} - -function mymatch (g, s) - local r, e, sfail = g:match(s) - if not r then - local line, col = re.calcline(s, #s - #sfail) - local msg = "Error at line " .. line .. " (col " .. col .. ")" - if e == 1 then - return r, msg .. ": expecting an identifier before '" .. sfail .. "'" - elseif e == 2 then - return r, msg .. ": expecting ',' before '" .. sfail .. "'" - else - return r, msg - end - end - return r -end - -print(mymatch(g, "one,two")) -print(mymatch(g, "one two")) -print(mymatch(g, "one,\n two,\nthree,")) diff --git a/06/deps/lpeglabel/examples/listId2.lua b/06/deps/lpeglabel/examples/listId2.lua deleted file mode 100644 index 3ee89da..0000000 --- a/06/deps/lpeglabel/examples/listId2.lua +++ /dev/null @@ -1,39 +0,0 @@ -local m = require'lpeglabel' -local re = require'relabel' - -local terror = {} - -local function newError(s) - table.insert(terror, s) - return #terror -end - -local errUndef = newError("undefined") -local errId = newError("expecting an identifier") -local errComma = newError("expecting ','") - -local id = m.R'az'^1 - -local g = m.P{ - "S", - S = m.V"Id" * m.V"List", - List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", - Id = m.V"Sp" * id + m.T(errId), - Comma = m.V"Sp" * "," + m.T(errComma), - Sp = m.S" \n\t"^0, -} - - -function mymatch (g, s) - local r, e, sfail = g:match(s) - if not r then - local line, col = re.calcline(s, #s - #sfail) - local msg = "Error at line " .. line .. " (col " .. col .. "): " - return r, msg .. terror[e] .. " before '" .. sfail .. "'" - end - return r -end - -print(mymatch(g, "one,two")) -print(mymatch(g, "one two")) -print(mymatch(g, "one,\n two,\nthree,")) diff --git a/06/deps/lpeglabel/examples/listId2Rec2.lua b/06/deps/lpeglabel/examples/listId2Rec2.lua deleted file mode 100644 index 3506095..0000000 --- a/06/deps/lpeglabel/examples/listId2Rec2.lua +++ /dev/null @@ -1,67 +0,0 @@ -local m = require'lpeglabel' -local re = require'relabel' - -local terror = {} - -local function newError(s) - table.insert(terror, s) - return #terror -end - -local errUndef = newError("undefined") -local errId = newError("expecting an identifier") -local errComma = newError("expecting ','") - -local id = m.R'az'^1 - -local g = m.P{ - "S", - S = m.V"Id" * m.V"List", - List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", - Id = m.V"Sp" * id + m.T(errId), - Comma = m.V"Sp" * "," + m.T(errComma), - Sp = m.S" \n\t"^0, -} - -local subject, errors - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = terror[lab] }) -end - -function record (lab) - return (m.Cp() * m.Cc(lab)) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -local grec = m.P{ - "S", - S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), - ErrComma = record(errComma) * sync(id), - ErrId = record(errId) * sync(m.P",") -} - - -function mymatch (g, s) - errors = {} - subject = s - local r, e, sfail = g:match(s) - if #errors > 0 then - local out = {} - for i, err in ipairs(errors) do - local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg - table.insert(out, msg) - end - return nil, table.concat(out, "\n") .. "\n" - end - return r -end - -print(mymatch(grec, "one,two")) -print(mymatch(grec, "one two three")) -print(mymatch(grec, "1,\n two, \n3,")) -print(mymatch(grec, "one\n two123, \nthree,")) diff --git a/06/deps/lpeglabel/examples/listId2Rec2Cap.lua b/06/deps/lpeglabel/examples/listId2Rec2Cap.lua deleted file mode 100644 index 32a4113..0000000 --- a/06/deps/lpeglabel/examples/listId2Rec2Cap.lua +++ /dev/null @@ -1,79 +0,0 @@ -local m = require'lpeglabel' -local re = require'relabel' - -local terror = {} - -local function newError(s) - table.insert(terror, s) - return #terror -end - -local errUndef = newError("undefined") -local errId = newError("expecting an identifier") -local errComma = newError("expecting ','") - -local id = m.R'az'^1 - -local g = m.P{ - "S", - S = m.V"Id" * m.V"List", - List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", - Id = m.V"Sp" * m.C(id) + m.T(errId), - Comma = m.V"Sp" * "," + m.T(errComma), - Sp = m.S" \n\t"^0, -} - -local subject, errors - -function recorderror(pos, lab) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = terror[lab] }) -end - -function record (lab) - return (m.Cp() * m.Cc(lab)) / recorderror -end - -function sync (p) - return (-p * m.P(1))^0 -end - -function defaultValue () - return m.Cc"NONE" -end - -local grec = m.P{ - "S", - S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), - ErrComma = record(errComma) * sync(id), - ErrId = record(errId) * sync(m.P",") * defaultValue(), -} - - -function mymatch (g, s) - errors = {} - subject = s - io.write("Input: ", s, "\n") - local r = { g:match(s) } - io.write("Captures (separated by ';'): ") - for k, v in pairs(r) do - io.write(v .. "; ") - end - io.write("\nSyntactic errors found: " .. #errors) - if #errors > 0 then - io.write("\n") - local out = {} - for i, err in ipairs(errors) do - local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg - table.insert(out, msg) - end - io.write(table.concat(out, "\n")) - end - print("\n") - return r -end - -mymatch(grec, "one,two") -mymatch(grec, "one two three") -mymatch(grec, "1,\n two, \n3,") -mymatch(grec, "one\n two123, \nthree,") diff --git a/06/deps/lpeglabel/examples/listIdRe1.lua b/06/deps/lpeglabel/examples/listIdRe1.lua deleted file mode 100644 index 3098c66..0000000 --- a/06/deps/lpeglabel/examples/listIdRe1.lua +++ /dev/null @@ -1,30 +0,0 @@ -local re = require 'relabel' - -local g = re.compile[[ - S <- Id List - List <- !. / Comma Id List - Id <- Sp [a-z]+ / %{2} - Comma <- Sp ',' / %{3} - Sp <- %s* -]] - -function mymatch (g, s) - local r, e, sfail = g:match(s) - if not r then - local line, col = re.calcline(s, #s - #sfail) - local msg = "Error at line " .. line .. " (col " .. col .. ")" - if e == 1 then - return r, msg .. ": expecting an identifier before '" .. sfail .. "'" - elseif e == 2 then - return r, msg .. ": expecting ',' before '" .. sfail .. "'" - else - return r, msg - end - end - return r -end - -print(mymatch(g, "one,two")) -print(mymatch(g, "one two")) -print(mymatch(g, "one,\n two,\nthree,")) - diff --git a/06/deps/lpeglabel/examples/listIdRe2.lua b/06/deps/lpeglabel/examples/listIdRe2.lua deleted file mode 100644 index 58ddedd..0000000 --- a/06/deps/lpeglabel/examples/listIdRe2.lua +++ /dev/null @@ -1,71 +0,0 @@ -local re = require 'relabel' - -local errinfo = { - {"errUndef", "undefined"}, - {"errId", "expecting an identifier"}, - {"errComma", "expecting ','"}, -} - -local errmsgs = {} -local labels = {} - -for i, err in ipairs(errinfo) do - errmsgs[i] = err[2] - labels[err[1]] = i -end - -re.setlabels(labels) - -local g = re.compile[[ - S <- Id List - List <- !. / Comma Id List - Id <- Sp {[a-z]+} / %{errId} - Comma <- Sp ',' / %{errComma} - Sp <- %s* -]] - -local errors - -function recorderror (subject, pos, label) - local line, col = re.calcline(subject, pos) - table.insert(errors, { line = line, col = col, msg = errmsgs[labels[label]] }) - return true -end - -function sync (p) - return '( !(' .. p .. ') .)*' -end - -local grec = re.compile( - "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. - "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('[a-z]+') .. "\n" .. - "ErrId <- ('' -> 'errId' => recorderror) " .. sync('","') .. "-> default" - , {g = g, recorderror = recorderror, default = "NONE"}) - -function mymatch (g, s) - errors = {} - subject = s - io.write("Input: ", s, "\n") - local r = { g:match(s) } - io.write("Captures (separated by ';'): ") - for k, v in pairs(r) do - io.write(v .. "; ") - end - io.write("\nSyntactic errors found: " .. #errors) - if #errors > 0 then - io.write("\n") - local out = {} - for i, err in ipairs(errors) do - local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg - table.insert(out, msg) - end - io.write(table.concat(out, "\n")) - end - print("\n") - return r -end - -mymatch(grec, "one,two") -mymatch(grec, "one two three") -mymatch(grec, "1,\n two, \n3,") -mymatch(grec, "one\n two123, \nthree,") diff --git a/06/deps/lpeglabel/examples/tiny.lua b/06/deps/lpeglabel/examples/tiny.lua deleted file mode 100644 index 734536c..0000000 --- a/06/deps/lpeglabel/examples/tiny.lua +++ /dev/null @@ -1,151 +0,0 @@ -local re = require 'relabel' - -local terror = {} - -local function newError(l, msg) - table.insert(terror, { l = l, msg = msg} ) -end - -newError("errSemi", "Error: missing ';'") -newError("errExpIf", "Error: expected expression after 'if'") -newError("errThen", "Error: expected 'then' keyword") -newError("errCmdSeq1", "Error: expected at least a command after 'then'") -newError("errCmdSeq2", "Error: expected at least a command after 'else'") -newError("errEnd", "Error: expected 'end' keyword") -newError("errCmdSeqRep", "Error: expected at least a command after 'repeat'") -newError("errUntil", "Error: expected 'until' keyword") -newError("errExpRep", "Error: expected expression after 'until'") -newError("errAssignOp", "Error: expected ':=' in assigment") -newError("errExpAssign", "Error: expected expression after ':='") -newError("errReadName", "Error: expected an identifier after 'read'") -newError("errWriteExp", "Error: expected expression after 'write'") -newError("errSimpExp", "Error: expected '(', ID, or number after '<' or '='") -newError("errTerm", "Error: expected '(', ID, or number after '+' or '-'") -newError("errFactor", "Error: expected '(', ID, or number after '*' or '/'") -newError("errExpFac", "Error: expected expression after '('") -newError("errClosePar", "Error: expected ')' after expression") - - -local labelCode = {} -for k, v in ipairs(terror) do - labelCode[v.l] = k -end - -re.setlabels(labelCode) - -local g = re.compile[[ - Tiny <- CmdSeq - CmdSeq <- (Cmd (SEMICOLON / ErrSemi)) (Cmd (SEMICOLON / ErrSemi))* - Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd - IfCmd <- IF (Exp / ErrExpIf) (THEN / ErrThen) (CmdSeq / ErrCmdSeq1) (ELSE (CmdSeq / ErrCmdSeq2) / '') (END / ErrEnd) - RepeatCmd <- REPEAT (CmdSeq / ErrCmdSeqRep) (UNTIL / ErrUntil) (Exp / ErrExpRep) - AssignCmd <- NAME (ASSIGNMENT / ErrAssignOp) (Exp / ErrExpAssign) - ReadCmd <- READ (NAME / ErrReadName) - WriteCmd <- WRITE (Exp / ErrWriteExp) - Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / ErrSimpExp) / '') - SimpleExp <- Term ((ADD / SUB) (Term / ErrTerm))* - Term <- Factor ((MUL / DIV) (Factor / ErrFactor))* - Factor <- OPENPAR (Exp / ErrExpFac) (CLOSEPAR / ErrClosePar) / NUMBER / NAME - ErrSemi <- %{errSemi} - ErrExpIf <- %{errExpIf} - ErrThen <- %{errThen} - ErrCmdSeq1 <- %{errCmdSeq1} - ErrCmdSeq2 <- %{errCmdSeq2} - ErrEnd <- %{errEnd} - ErrCmdSeqRep <- %{errCmdSeqRep} - ErrUntil <- %{errUntil} - ErrExpRep <- %{errExpRep} - ErrAssignOp <- %{errAssignOp} - ErrExpAssign <- %{errExpAssign} - ErrReadName <- %{errReadName} - ErrWriteExp <- %{errWriteExp} - ErrSimpExp <- %{errSimpExp} - ErrTerm <- %{errTerm} - ErrFactor <- %{errFactor} - ErrExpFac <- %{errExpFac} - ErrClosePar <- %{errClosePar} - ADD <- Sp '+' - ASSIGNMENT <- Sp ':=' - CLOSEPAR <- Sp ')' - DIV <- Sp '/' - IF <- Sp 'if' - ELSE <- Sp 'else' - END <- Sp 'end' - EQUAL <- Sp '=' - LESS <- Sp '<' - MUL <- Sp '*' - NAME <- Sp !RESERVED [a-z]+ - NUMBER <- Sp [0-9]+ - OPENPAR <- Sp '(' - READ <- Sp 'read' - REPEAT <- Sp 'repeat' - SEMICOLON <- Sp ';' - SUB <- Sp '-' - THEN <- Sp 'then' - UNTIL <- Sp 'until' - WRITE <- Sp 'write' - RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+ - Sp <- %s* -]] - - -local function mymatch(g, s) - local r, e, sfail = g:match(s) - if not r then - local line, col = re.calcline(s, #s - #sfail) - local msg = "Error at line " .. line .. " (col " .. col .. "): " - return r, msg .. terror[e].msg - end - return r -end - -local s = [[ -n := 5; -f := 1; -repeat - f := f + n; - n := n - 1 -until (n < 1); -write f;]] -print(mymatch(g, s)) - -s = [[ -n := 5; -f := 1; -repeat - f := f + n; - n := n - 1; -until (n < 1); -read ;]] -print(mymatch(g, s)) - -s = [[ -if a < 1 then - b := 2; -else - b := 3;]] -print(mymatch(g, s)) - -s = [[ -n := 5; -f := 1; -repeat - f := f + n; - n := n - 1; -untill (n < 1); -]] -print(mymatch(g, s)) - -s = [[ -n := 5; -f := 1; -repeat - f := f + n; - n := n - 1; -3 (n < 1); -]] -print(mymatch(g, s)) - -print(mymatch(g, "a : 2")) -print(mymatch(g, "a := (2")) - diff --git a/06/deps/lpeglabel/examples/typedlua/test.lua b/06/deps/lpeglabel/examples/typedlua/test.lua deleted file mode 100755 index 95474ba..0000000 --- a/06/deps/lpeglabel/examples/typedlua/test.lua +++ /dev/null @@ -1,2572 +0,0 @@ -#!/usr/bin/env lua - -local tlparser = require "tlparser" - --- expected result, result, message, subject -local e, r, m, s - -local filename = "test.lua" - -local function parse (s) - local r, m = tlparser.parse(s,filename,false,false) - if not r then m = m .. "\n" end - return r, m -end - -print("> testing lexer...") - --- syntax ok - --- empty files - -s = [=[ -]=] ---[=[ -{ } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ --- testing empty file -]=] ---[=[ -{ } -]=] - -r, m = parse(s) -assert(r == true) - --- expressions - -s = [=[ -local _nil,_false,_true,_dots = nil,false,true,... -]=] ---[=[ -{ `Local{ { `Id "_nil", `Id "_false", `Id "_true", `Id "_dots" }, { `Nil, `False, `True, `Dots } } } -]=] - -r, m = parse(s) -assert(r == true) - --- floating points - -s = [=[ -local f1 = 1. -local f2 = 1.1 -]=] ---[=[ -{ `Local{ { `Id "f1" }, { `Number "1.0" } }, `Local{ { `Id "f2" }, { `Number "1.1" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f1 = 1.e-1 -local f2 = 1.e1 -]=] ---[=[ -{ `Local{ { `Id "f1" }, { `Number "0.1" } }, `Local{ { `Id "f2" }, { `Number "10.0" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f1 = 1.1e+1 -local f2 = 1.1e1 -]=] ---[=[ -{ `Local{ { `Id "f1" }, { `Number "11.0" } }, `Local{ { `Id "f2" }, { `Number "11.0" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f1 = .1 -local f2 = .1e1 -]=] ---[=[ -{ `Local{ { `Id "f1" }, { `Number "0.1" } }, `Local{ { `Id "f2" }, { `Number "1.0" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f1 = 1E1 -local f2 = 1e-1 -]=] ---[=[ -{ `Local{ { `Id "f1" }, { `Number "10.0" } }, `Local{ { `Id "f2" }, { `Number "0.1" } } } -]=] - -r, m = parse(s) -assert(r == true) - --- integers - -s = [=[ -local i = 1 -local h = 0xff -]=] ---[=[ -{ `Local{ { `Id "i" }, { `Number "1" } }, `Local{ { `Id "h" }, { `Number "255" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local h = 0x76c -local i = 4294967296 -- 2^32 -]=] ---[=[ -{ `Local{ { `Id "h" }, { `Number "1900" } }, `Local{ { `Id "i" }, { `Number "4294967296" } } } -]=] - -r, m = parse(s) -assert(r == true) - --- long comments - -s = [=[ ---[======[ -testing -long -comment -[==[ one ]==] -[===[ more ]===] -[====[ time ]====] -bye -]======] -]=] ---[=[ -{ } -]=] - -r, m = parse(s) -assert(r == true) - --- long strings - -s = [=[ ---[[ -testing long string1 begin -]] - -local ls1 = -[[ -testing long string -]] - ---[[ -testing long string1 end -]] -]=] ---[=[ -{ `Local{ { `Id "ls1" }, { `String "testing long string\n" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ ---[==[ -testing long string2 begin -]==] - -local ls2 = [==[ testing \n [[ long ]] \t [===[ string ]===] -\a ]==] - ---[==[ -[[ testing long string2 end ]] -]==] -]=] ---[=[ -{ `Local{ { `Id "ls2" }, { `String " testing \\n [[ long ]] \\t [===[ string ]===]\n\\a " } } } -]=] - -r, m = parse(s) -assert(r == true) - --- short strings - -s = [=[ --- short string test begin - -local ss1_a = "ola mundo\a" -local ss1_b = 'ola mundo\a' - --- short string test end -]=] ---[=[ -{ `Local{ { `Id "ss1_a" }, { `String "ola mundo\a" } }, `Local{ { `Id "ss1_b" }, { `String "ola mundo\a" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ --- short string test begin - -local ss2_a = "testando,\tteste\n1\n2\n3 --> \"tchau\"" -local ss2_b = 'testando,\tteste\n1\n2\n3 --> \'tchau\'' - --- short string test end -]=] ---[=[ -{ `Local{ { `Id "ss2_a" }, { `String "testando,\tteste\n1\n2\n3 --> \"tchau\"" } }, `Local{ { `Id "ss2_b" }, { `String "testando,\tteste\n1\n2\n3 --> 'tchau'" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ --- short string test begin - -local ss3_a = "ola \ -'mundo'!" - -local ss3_b = 'ola \ -"mundo"!' - --- short string test end -]=] ---[=[ -{ `Local{ { `Id "ss3_a" }, { `String "ola \n'mundo'!" } }, `Local{ { `Id "ss3_b" }, { `String "ola \n\"mundo\"!" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ --- short string test begin - -local ss4_a = "C:\\Temp/" - -local ss4_b = 'C:\\Temp/' - --- short string test end -]=] ---[=[ -{ `Local{ { `Id "ss4_a" }, { `String "C:\\Temp/" } }, `Local{ { `Id "ss4_b" }, { `String "C:\\Temp/" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ --- short string test begin - -local ss5_a = "ola \ -mundo \\ \ -cruel" - -local ss5_b = 'ola \ -mundo \\ \ -cruel' - --- short string test end -]=] ---[=[ -{ `Local{ { `Id "ss5_a" }, { `String "ola \nmundo \\ \ncruel" } }, `Local{ { `Id "ss5_b" }, { `String "ola \nmundo \\ \ncruel" } } } -]=] - -r, m = parse(s) -assert(r == true) - --- syntax error - --- floating points - -s = [=[ -local f = 9e -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting '=', ',', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:1:12: malformed -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local f = 5.e -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting '=', ',', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:1:13: malformed -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local f = .9e- -]=] ---[=[ -test.lua:1:14: syntax error, unexpected '-', expecting '=', ',', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:1:14: malformed -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local f = 5.9e+ -]=] ---[=[ -test.lua:1:15: syntax error, unexpected '+', expecting '=', ',', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:1:15: malformed -]=] - -r, m = parse(s) -assert(m == e) - --- integers - -s = [=[ --- invalid hexadecimal number - -local hex = 0xG -]=] ---[=[ -test.lua:4:1: syntax error, unexpected 'EOF', expecting '=', ',', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:3:14: malformed -]=] - -r, m = parse(s) -assert(m == e) - --- long strings - -s = [=[ ---[==[ -testing long string3 begin -]==] - -local ls3 = [===[ -testing -unfinised -long string -]==] - ---[==[ -[[ testing long string3 end ]] -]==] -]=] ---[=[ -test.lua:5:13: syntax error, unexpected '[', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:14:1: unfinished long string -]=] - -r, m = parse(s) -assert(m == e) - --- short strings - -s = [=[ --- short string test begin - -local ss6 = "testing unfinished string - --- short string test end -]=] ---[=[ -test.lua:3:13: syntax error, unexpected '"', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:6:1: malformed -]=] - -r, m = parse(s) -assert(m == e) - --- unfinished comments - -s = [=[ ---[[ - -testing -unfinished - -comment - ]=] ---[=[ -test.lua:3:1: syntax error, unexpected 'comment', expecting '=', ',', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:1:2: unfinished long comment -]=] - -r, m = parse(s) -assert(m == e) - -print("> testing parser...") - --- syntax ok - --- anonymous functions - -s = [=[ -local a,b,c = function () end -]=] ---[=[ -{ `Local{ { `Id "a", `Id "b", `Id "c" }, { `Function{ { }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local test = function ( a , b , ... ) end -]=] ---[=[ -{ `Local{ { `Id "test" }, { `Function{ { `Id "a", `Id "b", `Dots }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local test = function (...) return ...,0 end -]=] ---[=[ -{ `Local{ { `Id "test" }, { `Function{ { `Dots }, { `Return{ `Dots, `Number "0" } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- arithmetic expressions - -s = [=[ -local arithmetic = 1 - 2 * 3 + 4 -]=] ---[=[ -{ `Local{ { `Id "arithmetic" }, { `Op{ "add", `Op{ "sub", `Number "1", `Op{ "mul", `Number "2", `Number "3" } }, `Number "4" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local pow = -3^-2^2 -]=] ---[=[ -{ `Local{ { `Id "pow" }, { `Op{ "unm", `Op{ "pow", `Number "3", `Op{ "unm", `Op{ "pow", `Number "2", `Number "2" } } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -q, r, f = 3//2, 3%2, 3/2 -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "q" }, `Index{ `Id "_ENV", `String "r" }, `Index{ `Id "_ENV", `String "f" } }, { `Op{ "idiv", `Number "3", `Number "2" }, `Op{ "mod", `Number "3", `Number "2" }, `Op{ "div", `Number "3", `Number "2" } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- assignments - -s = [=[ -a = f()[1] -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "a" } }, { `Index{ `Call{ `Index{ `Id "_ENV", `String "f" } }, `Number "1" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -a()[1] = 1; -]=] ---[=[ -{ `Set{ { `Index{ `Call{ `Index{ `Id "_ENV", `String "a" } }, `Number "1" } }, { `Number "1" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -i = a.f(1) -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "i" } }, { `Call{ `Index{ `Index{ `Id "_ENV", `String "a" }, `String "f" }, `Number "1" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -i = a[f(1)] -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "i" } }, { `Index{ `Index{ `Id "_ENV", `String "a" }, `Call{ `Index{ `Id "_ENV", `String "f" }, `Number "1" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -a[f()] = sub -i = i + 1 -]=] ---[=[ -{ `Set{ { `Index{ `Index{ `Id "_ENV", `String "a" }, `Call{ `Index{ `Id "_ENV", `String "f" } } } }, { `Index{ `Id "_ENV", `String "sub" } } }, `Set{ { `Index{ `Id "_ENV", `String "i" } }, { `Op{ "add", `Index{ `Id "_ENV", `String "i" }, `Number "1" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -a:b(1)._ = some_value -]=] ---[=[ -{ `Set{ { `Index{ `Invoke{ `Index{ `Id "_ENV", `String "a" }, `String "b", `Number "1" }, `String "_" } }, { `Index{ `Id "_ENV", `String "some_value" } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- bitwise expressions - -s = [=[ -b = 1 & 0 | 1 ~ 1 -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "b" } }, { `Op{ "bor", `Op{ "band", `Number "1", `Number "0" }, `Op{ "bxor", `Number "1", `Number "1" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -b = 1 & 0 | 1 >> 1 ~ 1 -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "b" } }, { `Op{ "bor", `Op{ "band", `Number "1", `Number "0" }, `Op{ "bxor", `Op{ "shr", `Number "1", `Number "1" }, `Number "1" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- break - -s = [=[ -while 1 do - break -end -]=] ---[=[ -{ `While{ `Number "1", { `Break } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -while 1 do - while 1 do - break - end - break -end -]=] ---[=[ -{ `While{ `Number "1", { `While{ `Number "1", { `Break } }, `Break } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -repeat - if 2 > 1 then break end -until 1 -]=] ---[=[ -{ `Repeat{ { `If{ `Op{ "lt", `Number "1", `Number "2" }, { `Break } } }, `Number "1" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -for i=1,10 do - do - break - break - return - end -end -]=] ---[=[ -{ `Fornum{ `Id "i", `Number "1", `Number "10", { `Do{ `Break, `Break, `Return } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- block statements - -s = [=[ -do - local var = 2+2; - return -end -]=] ---[=[ -{ `Do{ `Local{ { `Id "var" }, { `Op{ "add", `Number "2", `Number "2" } } }, `Return } } -]=] - -r, m = parse(s) -assert(r == true) - --- calls - -s = [=[ -f() -t:m() -]=] ---[=[ -{ `Call{ `Index{ `Id "_ENV", `String "f" } }, `Invoke{ `Index{ `Id "_ENV", `String "t" }, `String "m" } } -]=] - -r, m = parse(s) -assert(r == true) - --- concatenation expressions - -s = [=[ -local concat1 = 1 .. 2^3 -]=] ---[=[ -{ `Local{ { `Id "concat1" }, { `Op{ "concat", `Number "1", `Op{ "pow", `Number "2", `Number "3" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- empty files - -s = [=[ -; -]=] ---[=[ -{ } -]=] - -r, m = parse(s) -assert(r == true) - --- for generic - -s = [=[ -for k,v in pairs(t) do print (k,v) end -]=] ---[=[ -{ `Forin{ { `Id "k", `Id "v" }, { `Call{ `Index{ `Id "_ENV", `String "pairs" }, `Index{ `Id "_ENV", `String "t" } } }, { `Call{ `Index{ `Id "_ENV", `String "print" }, `Id "k", `Id "v" } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- for numeric - -s = [=[ -for i = 1 , 10 , 2 do end -]=] ---[=[ -{ `Fornum{ `Id "i", `Number "1", `Number "10", `Number "2", { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -for i=1,10 do end -]=] ---[=[ -{ `Fornum{ `Id "i", `Number "1", `Number "10", { } } } -]=] - -r, m = parse(s) -assert(r == true) - --- global functions - -s = [=[ -function test(a , b , ...) end -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "test" } }, { `Function{ { `Id "a", `Id "b", `Dots }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -function test (...) end -]=] ---[=[ -{ `Set{ { `Index{ `Id "_ENV", `String "test" } }, { `Function{ { `Dots }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -function t.a:b() end -]=] ---[=[ -{ `Set{ { `Index{ `Index{ `Index{ `Id "_ENV", `String "t" }, `String "a" }, `String "b" } }, { `Function{ { `Id "self" }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -function t.a() end -]=] ---[=[ -{ `Set{ { `Index{ `Index{ `Id "_ENV", `String "t" }, `String "a" } }, { `Function{ { }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -function testando . funcao . com : espcacos ( e, com , parametros, ... ) end -]=] ---[=[ -{ `Set{ { `Index{ `Index{ `Index{ `Index{ `Id "_ENV", `String "testando" }, `String "funcao" }, `String "com" }, `String "espcacos" } }, { `Function{ { `Id "self", `Id "e", `Id "com", `Id "parametros", `Dots }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- goto - -s = [=[ -goto label -:: label :: return -]=] ---[=[ -{ `Goto{ "label" }, `Label{ "label" }, `Return } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -::label:: -goto label -]=] ---[=[ -{ `Label{ "label" }, `Goto{ "label" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -goto label -::label:: -]=] ---[=[ -{ `Goto{ "label" }, `Label{ "label" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -::label:: -do ::label:: goto label end -]=] ---[=[ -{ `Label{ "label" }, `Do{ `Label{ "label" }, `Goto{ "label" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -::label:: -do goto label ; ::label:: end -]=] ---[=[ -{ `Label{ "label" }, `Do{ `Goto{ "label" }, `Label{ "label" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -::label:: -do goto label end -]=] ---[=[ -{ `Label{ "label" }, `Do{ `Goto{ "label" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -do goto label end -::label:: -]=] ---[=[ -{ `Do{ `Goto{ "label" } }, `Label{ "label" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -do do do do do goto label end end end end end -::label:: -]=] ---[=[ -{ `Do{ `Do{ `Do{ `Do{ `Do{ `Goto{ "label" } } } } } }, `Label{ "label" } } -]=] - -r, m = parse(s) -assert(r == true) - --- if-else - -s = [=[ -if a then end -]=] ---[=[ -{ `If{ `Index{ `Id "_ENV", `String "a" }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -if a then return a else return end -]=] ---[=[ -{ `If{ `Index{ `Id "_ENV", `String "a" }, { `Return{ `Index{ `Id "_ENV", `String "a" } } }, { `Return } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -if a then - return a -else - local c = d - d = d + 1 - return d -end -]=] ---[=[ -{ `If{ `Index{ `Id "_ENV", `String "a" }, { `Return{ `Index{ `Id "_ENV", `String "a" } } }, { `Local{ { `Id "c" }, { `Index{ `Id "_ENV", `String "d" } } }, `Set{ { `Index{ `Id "_ENV", `String "d" } }, { `Op{ "add", `Index{ `Id "_ENV", `String "d" }, `Number "1" } } }, `Return{ `Index{ `Id "_ENV", `String "d" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -if a then - return a -elseif b then - return b -elseif c then - return c -end -]=] ---[=[ -{ `If{ `Index{ `Id "_ENV", `String "a" }, { `Return{ `Index{ `Id "_ENV", `String "a" } } }, `Index{ `Id "_ENV", `String "b" }, { `Return{ `Index{ `Id "_ENV", `String "b" } } }, `Index{ `Id "_ENV", `String "c" }, { `Return{ `Index{ `Id "_ENV", `String "c" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -if a then return a -elseif b then return -else ; -end -]=] ---[=[ -{ `If{ `Index{ `Id "_ENV", `String "a" }, { `Return{ `Index{ `Id "_ENV", `String "a" } } }, `Index{ `Id "_ENV", `String "b" }, { `Return }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -if a then - return -elseif c then -end -]=] ---[=[ -{ `If{ `Index{ `Id "_ENV", `String "a" }, { `Return }, `Index{ `Id "_ENV", `String "c" }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - --- interfaces - -s = [=[ -local interface Empty end -]=] ---[=[ -{ `Interface{ Empty, `TTable{ } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface X - x, y, z:number -end -]=] ---[=[ -{ `Interface{ X, `TTable{ `TLiteral x:`TBase number, `TLiteral y:`TBase number, `TLiteral z:`TBase number } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface Person - firstname:string - lastname:string -end -]=] ---[=[ -{ `Interface{ Person, `TTable{ `TLiteral firstname:`TBase string, `TLiteral lastname:`TBase string } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface Element - info:number - next:Element? -end -]=] ---[=[ -{ `Interface{ Element, `TRecursive{ Element, `TTable{ `TLiteral info:`TBase number, `TLiteral next:`TUnion{ `TVariable Element, `TNil } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- labels - -s = [=[ -::label:: -do ::label:: end -::other_label:: -]=] ---[=[ -{ `Label{ "label" }, `Do{ `Label{ "label" } }, `Label{ "other_label" } } -]=] - -r, m = parse(s) -assert(r == true) - --- locals - -s = [=[ -local a -]=] ---[=[ -{ `Local{ { `Id "a" }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local a,b,c -]=] ---[=[ -{ `Local{ { `Id "a", `Id "b", `Id "c" }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local a = 1 , 1 + 2, 5.1 -]=] ---[=[ -{ `Local{ { `Id "a" }, { `Number "1", `Op{ "add", `Number "1", `Number "2" }, `Number "5.1" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local a,b,c = 1.9 -]=] ---[=[ -{ `Local{ { `Id "a", `Id "b", `Id "c" }, { `Number "1.9" } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function test() end -]=] ---[=[ -{ `Localrec{ { `Id "test" }, { `Function{ { }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function test ( a , b , c , ... ) end -]=] ---[=[ -{ `Localrec{ { `Id "test" }, { `Function{ { `Id "a", `Id "b", `Id "c", `Dots }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function test(...) return ... end -]=] ---[=[ -{ `Localrec{ { `Id "test" }, { `Function{ { `Dots }, { `Return{ `Dots } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- relational expressions - -s = [=[ -local relational = 1 < 2 >= 3 == 4 ~= 5 < 6 <= 7 -]=] ---[=[ -{ `Local{ { `Id "relational" }, { `Op{ "le", `Op{ "lt", `Op{ "not", `Op{ "eq", `Op{ "eq", `Op{ "le", `Number "3", `Op{ "lt", `Number "1", `Number "2" } }, `Number "4" }, `Number "5" } }, `Number "6" }, `Number "7" } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- repeat - -s = [=[ -repeat - local a,b,c = 1+1,2+2,3+3 - break -until a < 1 -]=] ---[=[ -{ `Repeat{ { `Local{ { `Id "a", `Id "b", `Id "c" }, { `Op{ "add", `Number "1", `Number "1" }, `Op{ "add", `Number "2", `Number "2" }, `Op{ "add", `Number "3", `Number "3" } } }, `Break }, `Op{ "lt", `Index{ `Id "_ENV", `String "a" }, `Number "1" } } } -]=] - -r, m = parse(s) -assert(r == true) - --- return - -s = [=[ -return -]=] ---[=[ -{ `Return } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -return 1 -]=] ---[=[ -{ `Return{ `Number "1" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -return 1,1-2*3+4,"alo" -]=] ---[=[ -{ `Return{ `Number "1", `Op{ "add", `Op{ "sub", `Number "1", `Op{ "mul", `Number "2", `Number "3" } }, `Number "4" }, `String "alo" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -return; -]=] ---[=[ -{ `Return } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -return 1; -]=] ---[=[ -{ `Return{ `Number "1" } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -return 1,1-2*3+4,"alo"; -]=] ---[=[ -{ `Return{ `Number "1", `Op{ "add", `Op{ "sub", `Number "1", `Op{ "mul", `Number "2", `Number "3" } }, `Number "4" }, `String "alo" } } -]=] - -r, m = parse(s) -assert(r == true) - --- tables - -s = [=[ -local t = { [1] = "alo", alo = 1, 2; } -]=] ---[=[ -{ `Local{ { `Id "t" }, { `Table{ `Pair{ `Number "1", `String "alo" }, `Pair{ `String "alo", `Number "1" }, `Number "2" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local t = { 1.5 } -]=] ---[=[ -{ `Local{ { `Id "t" }, { `Table{ `Number "1.5" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local t = {1,2; -3, -4, - - - -5} -]=] ---[=[ -{ `Local{ { `Id "t" }, { `Table{ `Number "1", `Number "2", `Number "3", `Number "4", `Number "5" } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local t = {[1]=1,[2]=2; -[3]=3, -[4]=4, - - - -[5]=5} -]=] ---[=[ -{ `Local{ { `Id "t" }, { `Table{ `Pair{ `Number "1", `Number "1" }, `Pair{ `Number "2", `Number "2" }, `Pair{ `Number "3", `Number "3" }, `Pair{ `Number "4", `Number "4" }, `Pair{ `Number "5", `Number "5" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local t = {{{}}, {"alo"}} -]=] ---[=[ -{ `Local{ { `Id "t" }, { `Table{ `Table{ `Table }, `Table{ `String "alo" } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- vararg - -s = [=[ -local f = function (...) - return ... -end -]=] ---[=[ -{ `Local{ { `Id "f" }, { `Function{ { `Dots }, { `Return{ `Dots } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f = function () - local g = function (x, y, ...) - return ...,...,... - end -end -]=] ---[=[ -{ `Local{ { `Id "f" }, { `Function{ { }, { `Local{ { `Id "g" }, { `Function{ { `Id "x", `Id "y", `Dots }, { `Return{ `Dots, `Dots, `Dots } } } } } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x, ...) - return ... -end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x", `Dots }, { `Return{ `Dots } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f = function (x, ...) - return ... -end -]=] ---[=[ -{ `Local{ { `Id "f" }, { `Function{ { `Id "x", `Dots }, { `Return{ `Dots } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- while - -s = [=[ -local i = 0 -while (i < 10) -do - i = i + 1 -end -]=] ---[=[ -{ `Local{ { `Id "i" }, { `Number "0" } }, `While{ `Paren{ `Op{ "lt", `Id "i", `Number "10" } }, { `Set{ { `Id "i" }, { `Op{ "add", `Id "i", `Number "1" } } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- type annotations - -s = [=[ -local x:nil -]=] ---[=[ -{ `Local{ { `Id "x":`TNil }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:false, y:true -]=] ---[=[ -{ `Local{ { `Id "x":`TLiteral false, `Id "y":`TLiteral true }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:1, y:1.1 -]=] ---[=[ -{ `Local{ { `Id "x":`TLiteral 1, `Id "y":`TLiteral 1.1 }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:"hello", y:'world' -]=] ---[=[ -{ `Local{ { `Id "x":`TLiteral hello, `Id "y":`TLiteral world }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:boolean, y:number, z:string -]=] ---[=[ -{ `Local{ { `Id "x":`TBase boolean, `Id "y":`TBase number, `Id "z":`TBase string }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:any -]=] ---[=[ -{ `Local{ { `Id "x":`TAny }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:number? -]=] ---[=[ -{ `Local{ { `Id "x":`TUnion{ `TBase number, `TNil } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:number|nil -]=] ---[=[ -{ `Local{ { `Id "x":`TUnion{ `TBase number, `TNil } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:number|string|nil -]=] ---[=[ -{ `Local{ { `Id "x":`TUnion{ `TBase number, `TBase string, `TNil } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:number|nil|nil|nil|nil -]=] ---[=[ -{ `Local{ { `Id "x":`TUnion{ `TBase number, `TNil } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:number|nil|string|nil|number|boolean|string -]=] ---[=[ -{ `Local{ { `Id "x":`TUnion{ `TNil, `TBase number, `TBase boolean, `TBase string } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:number|string? -]=] ---[=[ -{ `Local{ { `Id "x":`TUnion{ `TBase number, `TBase string, `TNil } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:(number) -> (number) -]=] ---[=[ -{ `Local{ { `Id "x":`TFunction{ `TTuple{ `TBase number, `TVararg{ `TValue } }, `TTuple{ `TBase number, `TVararg{ `TNil } } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:(value*) -> (nil*) -]=] ---[=[ -{ `Local{ { `Id "x":`TFunction{ `TTuple{ `TVararg{ `TValue } }, `TTuple{ `TVararg{ `TNil } } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:(number,string,boolean) -> (string,number,boolean) -]=] ---[=[ -{ `Local{ { `Id "x":`TFunction{ `TTuple{ `TBase number, `TBase string, `TBase boolean, `TVararg{ `TValue } }, `TTuple{ `TBase string, `TBase number, `TBase boolean, `TVararg{ `TNil } } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:(number,string,value*) -> (string,number,nil*) -]=] ---[=[ -{ `Local{ { `Id "x":`TFunction{ `TTuple{ `TBase number, `TBase string, `TVararg{ `TValue } }, `TTuple{ `TBase string, `TBase number, `TVararg{ `TNil } } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{{{{{}}}}} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ `TBase number:`TUnion{ `TTable{ `TBase number:`TUnion{ `TTable{ `TBase number:`TUnion{ `TTable{ `TBase number:`TUnion{ `TTable{ }, `TNil } }, `TNil } }, `TNil } }, `TNil } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{string} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ `TBase number:`TUnion{ `TBase string, `TNil } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{string:number} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ `TBase string:`TUnion{ `TBase number, `TNil } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{'firstname':string, 'lastname':string} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ `TLiteral firstname:`TBase string, `TLiteral lastname:`TBase string } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{'tag':string, number:string} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ `TLiteral tag:`TBase string, `TBase number:`TUnion{ `TBase string, `TNil } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local x:{'f':(number) -> (number), 't':{number:number}} -]=] ---[=[ -{ `Local{ { `Id "x":`TTable{ `TLiteral f:`TFunction{ `TTuple{ `TBase number, `TVararg{ `TValue } }, `TTuple{ `TBase number, `TVararg{ `TNil } } }, `TLiteral t:`TTable{ `TBase number:`TUnion{ `TBase number, `TNil } } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -for k:number, v:string in ipairs({"hello", "world"}) do end -]=] ---[=[ -{ `Forin{ { `Id "k":`TBase number, `Id "v":`TBase string }, { `Call{ `Index{ `Id "_ENV", `String "ipairs" }, `Table{ `String "hello", `String "world" } } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -for k:string, v in pairs({}) do end -]=] ---[=[ -{ `Forin{ { `Id "k":`TBase string, `Id "v" }, { `Call{ `Index{ `Id "_ENV", `String "pairs" }, `Table } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -for k, v:boolean in pairs({}) do end -]=] ---[=[ -{ `Forin{ { `Id "k", `Id "v":`TBase boolean }, { `Call{ `Index{ `Id "_ENV", `String "pairs" }, `Table } }, { } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:any) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TAny }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:any):(any) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TAny }:`TTuple{ `TAny, `TVararg{ `TNil } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (...:any) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Dots:`TAny }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:any, ...:any) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TAny, `Dots:`TAny }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x, ...:any) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x", `Dots:`TAny }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:any, ...) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TAny, `Dots }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:any, ...:any):(any) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TAny, `Dots:`TAny }:`TTuple{ `TAny, `TVararg{ `TNil } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:(any) -> (any)):((any) -> (any)) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TFunction{ `TTuple{ `TAny, `TVararg{ `TValue } }, `TTuple{ `TAny, `TVararg{ `TNil } } } }:`TTuple{ `TFunction{ `TTuple{ `TAny, `TVararg{ `TValue } }, `TTuple{ `TAny, `TVararg{ `TNil } } }, `TVararg{ `TNil } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x:(number, number) -> (number, nil*)):(number*) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { `Id "x":`TFunction{ `TTuple{ `TBase number, `TBase number, `TVararg{ `TValue } }, `TTuple{ `TBase number, `TVararg{ `TNil } } } }:`TTuple{ `TVararg{ `TBase number } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f ():(number, nil*) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { }:`TTuple{ `TBase number, `TVararg{ `TNil } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f ():number end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { }:`TTuple{ `TBase number, `TVararg{ `TNil } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f ():number? end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { }:`TTuple{ `TUnion{ `TBase number, `TNil }, `TVararg{ `TNil } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f ():(number) | (nil,string) end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { }:`TUnionlist{ `TTuple{ `TBase number, `TVararg{ `TNil } }, `TTuple{ `TNil, `TBase string, `TVararg{ `TNil } } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f ():(number)? end -]=] ---[=[ -{ `Localrec{ { `Id "f" }, { `Function{ { }:`TUnionlist{ `TTuple{ `TBase number, `TVararg{ `TNil } }, `TTuple{ `TNil, `TBase string, `TVararg{ `TNil } } }, { } } } } } -]=] - -r, m = parse(s) -assert(r == true) - --- syntax error - --- anonymous functions - -s = [=[ -a = function (a,b,) end -]=] ---[=[ -test.lua:1:19: syntax error, unexpected ')', expecting '...', 'Name' -]=] -e = [=[ -test.lua:1:18: expecting '...' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -a = function (...,a) end -]=] ---[=[ -test.lua:1:18: syntax error, unexpected ',', expecting ')', ':' -]=] -e = [=[ -test.lua:1:17: missing ')' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local a = function (1) end -]=] ---[=[ -test.lua:1:21: syntax error, unexpected '1', expecting ')', '...', 'Name' -]=] -e = [=[ -test.lua:1:20: missing ')' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local test = function ( a , b , c , ... ) -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting 'end', 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';', ':' -]=] -e = [=[ -test.lua:2:1: missing 'end' to close function declaration -]=] - -r, m = parse(s) -assert(m == e) - --- arithmetic expressions - -s = [=[ -a = 3 / / 2 -]=] ---[=[ -test.lua:1:9: syntax error, unexpected '/', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:1:8: malformed multiplication expression -]=] - -r, m = parse(s) -assert(m == e) - --- bitwise expressions - -s = [=[ -b = 1 && 1 -]=] ---[=[ -test.lua:1:8: syntax error, unexpected '&', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:1:7: malformed '&' expression -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -b = 1 <> 0 -]=] ---[=[ -test.lua:1:8: syntax error, unexpected '>', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:1:7: malformed relational expression -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -b = 1 < < 0 -]=] ---[=[ -test.lua:1:9: syntax error, unexpected '<', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:1:8: malformed relational expression -]=] - -r, m = parse(s) -assert(m == e) - --- concatenation expressions - -s = [=[ -concat2 = 2^3..1 -]=] ---[=[ -test.lua:1:15: syntax error, unexpected '.1', expecting 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';', ',', 'or', 'and', '>', '<', '>=', '<=', '==', '~=', '|', '~', '&', '>>', '<<', '..', '-', '+', '%', '/', '//', '*', '^' -]=] -e = [=[ -test.lua:1:14: malformed -]=] - -r, m = parse(s) -assert(m == e) - --- for generic - -s = [=[ -for k;v in pairs(t) do end -]=] ---[=[ -test.lua:1:6: syntax error, unexpected ';', expecting 'in', ',', ':', '=' -]=] -e = [=[ -test.lua:1:5: expecting 'in' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -for k,v in pairs(t:any) do end -]=] ---[=[ -test.lua:1:23: syntax error, unexpected ')', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:1:22: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - --- for numeric - -s = [=[ -for i=1,10, do end -]=] ---[=[ -test.lua:1:13: syntax error, unexpected 'do', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:1:10: missing 'do' in for statement -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -for i=1,n:number do end -]=] ---[=[ -test.lua:1:18: syntax error, unexpected 'do', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:1:17: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - --- global functions - -s = [=[ -function func(a,b,c,) end -]=] ---[=[ -test.lua:1:21: syntax error, unexpected ')', expecting '...', 'Name' -]=] -e = [=[ -test.lua:1:20: expecting '...' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -function func(...,a) end -]=] ---[=[ -test.lua:1:18: syntax error, unexpected ',', expecting ')', ':' -]=] -e = [=[ -test.lua:1:17: missing ')' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -function a.b:c:d () end -]=] ---[=[ -test.lua:1:15: syntax error, unexpected ':', expecting '(' -]=] -e = [=[ -test.lua:1:14: missing '(' -]=] - -r, m = parse(s) -assert(m == e) - --- goto - -s = [=[ -:: label :: return -goto label -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'goto', expecting ';', '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:2:1: invalid statement after 'return' -]=] - -r, m = parse(s) -assert(m == e) - --- if-else - -s = [=[ -if a then -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting 'end', 'else', 'elseif', 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';' -]=] -e = [=[ -test.lua:2:1: missing 'end' to close if statement -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -if a then else -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting 'end', 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';' -]=] -e = [=[ -test.lua:2:1: missing 'end' to close if statement -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -if a then - return a -elseif b then - return b -elseif - -end -]=] ---[=[ -test.lua:7:1: syntax error, unexpected 'end', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:7:1: expecting after 'elseif' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -if a:any then else end -]=] ---[=[ -test.lua:1:10: syntax error, unexpected 'then', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:1:9: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - --- labels - -s = [=[ -:: blah :: -:: not :: -]=] ---[=[ -test.lua:2:4: syntax error, unexpected 'not', expecting 'Name' -]=] -e = [=[ -test.lua:2:3: expecting after '::' -]=] - -r, m = parse(s) -assert(m == e) - --- locals - -s = [=[ -local a = -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:2:1: expecting expression list after '=' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local function t.a() end -]=] ---[=[ -test.lua:1:17: syntax error, unexpected '.', expecting '(' -]=] -e = [=[ -test.lua:1:16: missing '(' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local function test (a,) end -]=] ---[=[ -test.lua:1:24: syntax error, unexpected ')', expecting '...', 'Name' -]=] -e = [=[ -test.lua:1:23: expecting '...' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local function test(...,a) end -]=] ---[=[ -test.lua:1:24: syntax error, unexpected ',', expecting ')', ':' -]=] -e = [=[ -test.lua:1:23: missing ')' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local function (a, b, c, ...) end -]=] ---[=[ -test.lua:1:16: syntax error, unexpected '(', expecting 'Name' -]=] -e = [=[ -test.lua:1:15: expecting in local function declaration -]=] - -r, m = parse(s) -assert(m == e) - --- repeat - -s = [=[ -repeat - a,b,c = 1+1,2+2,3+3 - break -]=] ---[=[ -test.lua:4:1: syntax error, unexpected 'EOF', expecting 'until', 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';' -]=] -e = [=[ -test.lua:4:1: missing 'until' in repeat statement -]=] - -r, m = parse(s) -assert(m == e) - --- return - -s = [=[ -return -return 1 -return 1,1-2*3+4,"alo" -return; -return 1; -return 1,1-2*3+4,"alo"; -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'return', expecting ';', '(', 'Name', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not' -]=] -e = [=[ -test.lua:2:1: invalid statement -]=] - -r, m = parse(s) -assert(m == e) - --- tables - -s = [=[ -t = { , } -]=] ---[=[ -test.lua:1:7: syntax error, unexpected ',', expecting '}', '(', '{', 'function', '...', 'true', 'false', 'nil', 'String', 'Number', '#', '~', '-', 'not', 'Name', '[', 'const' -]=] -e = [=[ -test.lua:1:6: missing '}' -]=] - -r, m = parse(s) -assert(m == e) - --- while - -s = [=[ -i = 0 -while (i < 10) - i = i + 1 -end -]=] ---[=[ -test.lua:3:3: syntax error, unexpected 'i', expecting 'do', 'or', 'and', '>', '<', '>=', '<=', '==', '~=', '|', '~', '&', '>>', '<<', '..', '-', '+', '%', '/', '//', '*', '^', 'String', '{', '(', ':', '[', '.' -]=] -e = [=[ -test.lua:3:2: missing 'do' in while statement -]=] - -r, m = parse(s) -assert(m == e) - --- type annotations - -s = [=[ -t[x:any] = 1 -]=] ---[=[ -test.lua:1:8: syntax error, unexpected ']', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:1:7: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -x:number, y, z:boolean = 1, nil, true -]=] ---[=[ -test.lua:1:9: syntax error, unexpected ',', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:1:8: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -x = x:any -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:2:1: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -x = ...:any -]=] ---[=[ -test.lua:1:8: syntax error, unexpected ':', expecting 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';', ',', 'or', 'and', '>', '<', '>=', '<=', '==', '~=', '|', '~', '&', '>>', '<<', '..', '-', '+', '%', '/', '//', '*', '^' -]=] -e = [=[ -test.lua:1:7: invalid statement -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -f(x:any) -]=] ---[=[ -test.lua:1:8: syntax error, unexpected ')', expecting 'String', '{', '(' -]=] -e = [=[ -test.lua:1:7: expecting '(' for method call -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -f(...:any) -]=] ---[=[ -test.lua:1:6: syntax error, unexpected ':', expecting ')', ',', 'or', 'and', '>', '<', '>=', '<=', '==', '~=', '|', '~', '&', '>>', '<<', '..', '-', '+', '%', '/', '//', '*', '^' -]=] -e = [=[ -test.lua:1:5: missing ')' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:number* -]=] ---[=[ -test.lua:1:15: syntax error, unexpected '*', expecting 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';', '=', ',', '?', '|' -]=] -e = [=[ -test.lua:1:6: invalid local declaration -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:number| -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting '{', '(', 'Type' -]=] -e = [=[ -test.lua:2:1: expecting after '|' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:number?|string? -]=] ---[=[ -test.lua:1:16: syntax error, unexpected '|', expecting 'return', '(', 'Name', 'typealias', 'interface', 'goto', 'break', '::', 'local', 'function', 'const', 'repeat', 'for', 'do', 'while', 'if', ';', '=', ',' -]=] -e = [=[ -test.lua:1:6: invalid local declaration -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:() -> number -]=] ---[=[ -test.lua:1:15: syntax error, unexpected 'number', expecting '(' -]=] -e = [=[ -test.lua:1:14: expecting after '->' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:() -> (number)? | (string)? -]=] ---[=[ -test.lua:1:35: syntax error, unexpected '?', expecting '->' -]=] -e = [=[ -test.lua:1:26: expecting after '|' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:{()->():string} -]=] ---[=[ -test.lua:1:16: syntax error, unexpected ':', expecting '}', '?', '|' -]=] -e = [=[ -test.lua:1:15: missing '}' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:{string:t 1} -]=] ---[=[ -test.lua:1:19: syntax error, unexpected '1', expecting '}', '?', '|' -]=] -e = [=[ -test.lua:1:18: missing '}' -]=] - -r, m = parse(s) -assert(m == e) - -s = [=[ -local x:{{{{{}}}} -]=] ---[=[ -test.lua:2:1: syntax error, unexpected 'EOF', expecting '}', '?', '|' -]=] -e = [=[ -test.lua:2:1: missing '}' -]=] - -r, m = parse(s) -assert(m == e) - --- syntax errors that depend on some semantic information - --- break - -s = [=[ -break -]=] ---[=[ -test.lua:1:1: syntax error, not inside a loop -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -function f (x) - if 1 then break end -end -]=] ---[=[ -test.lua:2:13: syntax error, not inside a loop -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -while 1 do -end -break -]=] ---[=[ -test.lua:3:1: syntax error, not inside a loop -]=] - -r, m = parse(s) -assert(r == true) - --- goto - -s = [=[ -goto label -]=] ---[=[ -test.lua:1:1: syntax error, no visible label 'label' for -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -goto label -::other_label:: -]=] ---[=[ -test.lua:1:1: syntax error, no visible label 'label' for -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -::other_label:: -do do do goto label end end end -]=] ---[=[ -test.lua:2:10: syntax error, no visible label 'label' for -]=] - -r, m = parse(s) -assert(r == true) - --- interfaces - -s = [=[ -local interface X - x:number - y:number - z:number - x:number -end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare field 'x' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface X - x, y, z, x:number -end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare field 'x' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface boolean end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'boolean' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface number end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'number' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface string end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'string' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface value end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'value' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface any end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'any' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface self end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'self' -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local interface const end -]=] ---[=[ -test.lua:1:7: syntax error, attempt to redeclare type 'const' -]=] - -r, m = parse(s) -assert(r == true) - --- labels - -s = [=[ -::label:: -::other_label:: -::label:: -]=] ---[=[ -test.lua:3:1: syntax error, label 'label' already defined -]=] - -r, m = parse(s) -assert(r == true) - --- vararg - -s = [=[ -function f () - return ... -end -]=] ---[=[ -test.lua:2:10: syntax error, cannot use '...' outside a vararg function -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -function f () - function g (x, y) - return ...,...,... - end -end -]=] ---[=[ -test.lua:3:12: syntax error, cannot use '...' outside a vararg function -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local function f (x) - return ... -end -]=] ---[=[ -test.lua:2:10: syntax error, cannot use '...' outside a vararg function -]=] - -r, m = parse(s) -assert(r == true) - -s = [=[ -local f = function (x) - return ... -end -]=] ---[=[ -test.lua:2:10: syntax error, cannot use '...' outside a vararg function -]=] - -r, m = parse(s) -assert(r == true) - -print("OK") diff --git a/06/deps/lpeglabel/examples/typedlua/tlerror.lua b/06/deps/lpeglabel/examples/typedlua/tlerror.lua deleted file mode 100644 index 94be024..0000000 --- a/06/deps/lpeglabel/examples/typedlua/tlerror.lua +++ /dev/null @@ -1,66 +0,0 @@ - -local errors = {} -local function new_error (label, msg) - table.insert(errors, { label = label, msg = msg }) -end - -new_error("Number", "malformed ") -new_error("String", "malformed ") -new_error("LongString", "unfinished long string") -new_error("LongComment", "unfinished long comment") -new_error("MissingOP", "missing '('") -new_error("MissingCP", "missing ')'") -new_error("MissingCC", "missing '}'") -new_error("MissingCB", "missing ']'") -new_error("UnionType", "expecting after '|'") -new_error("FunctionType", "expecting after '->'") -new_error("MethodType", "expecting after '=>'") -new_error("TupleType", "expecting after ','") -new_error("Type", "expecting after ':'") -new_error("TypeDecEnd", "missing 'end' in type declaration") -new_error("TypeAliasName", "expecting after 'typealias'") -new_error("MissingEqTypeAlias", "missing '=' in 'typealias'") -new_error("DotIndex", "expecting after '.'") -new_error("MethodName", "expecting after ':'") -new_error("Then", "missing 'then'") -new_error("IfEnd", "missing 'end' to close if statement") -new_error("WhileDo", "missing 'do' in while statement") -new_error("WhileEnd", "missing 'end' to close while statement") -new_error("BlockEnd", "missing 'end' to close block") -new_error("ForDo", "missing 'do' in for statement") -new_error("ForEnd", "missing 'end' to close for statement") -new_error("Until", "missing 'until' in repeat statement") -new_error("FuncEnd", "missing 'end' to close function declaration") -new_error("ParList", "expecting '...'") -new_error("MethodCall", "expecting '(' for method call") -new_error("Label1", "expecting after '::'") -new_error("Label2", "expecting '::' to close label declaration") -new_error("LocalAssign1", "expecting expression list after '='") -new_error("LocalAssign2", "invalid local declaration") -new_error("ForGen", "expecting 'in'") -new_error("LocalFunc", "expecting in local function declaration") -new_error("RetStat", "invalid statement after 'return'") -new_error("ElseIf", "expecting after 'elseif'") -new_error("SubExpr_1", "malformed 'or' expression") -new_error("SubExpr_2", "malformed 'and' expression") -new_error("SubExpr_3", "malformed relational expression") -new_error("SubExpr_4", "malformed '|' expression") -new_error("SubExpr_5", "malformed '~' expression") -new_error("SubExpr_6", "malformed '&' expression") -new_error("SubExpr_7", "malformed shift expression") -new_error("SubExpr_8", "malformed '..' expression") -new_error("SubExpr_9", "malformed addition expression") -new_error("SubExpr_10", "malformed multiplication expression") -new_error("SubExpr_11", "malformed unary expression") -new_error("SubExpr_12", "malformed '^' expression") -new_error("Stat", "invalid statement") - -local labels = {} -for k, v in ipairs(errors) do - labels[v.label] = k -end - -return { - errors = errors, - labels = labels, -} diff --git a/06/deps/lpeglabel/examples/typedlua/tllexer.lua b/06/deps/lpeglabel/examples/typedlua/tllexer.lua deleted file mode 100644 index 96f296d..0000000 --- a/06/deps/lpeglabel/examples/typedlua/tllexer.lua +++ /dev/null @@ -1,105 +0,0 @@ -local tllexer = {} - -local lpeg = require "lpeglabel" -lpeg.locale(lpeg) - -local tlerror = require "tlerror" - -function tllexer.try (pat, label) - return pat + lpeg.T(tlerror.labels[label]) -end - -local function setffp (s, i, t, n) - if not t.ffp or i > t.ffp then - t.ffp = i - t.list = {} - t.list[n] = true - t.expected = "'" .. n .. "'" - elseif i == t.ffp then - if not t.list[n] then - t.list[n] = true - t.expected = "'" .. n .. "', " .. t.expected - end - end - return false -end - -local function updateffp (name) - return lpeg.Cmt(lpeg.Carg(1) * lpeg.Cc(name), setffp) -end - -tllexer.Shebang = lpeg.P("#") * (lpeg.P(1) - lpeg.P("\n"))^0 * lpeg.P("\n") - -local Space = lpeg.space^1 - -local Equals = lpeg.P("=")^0 -local Open = "[" * lpeg.Cg(Equals, "init") * "[" * lpeg.P("\n")^-1 -local Close = "]" * lpeg.C(Equals) * "]" -local CloseEQ = lpeg.Cmt(Close * lpeg.Cb("init"), - function (s, i, a, b) return a == b end) - -local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * tllexer.try(Close, "LongString") / - function (s, o) return s end - -local LongStringCm1 = Open * (lpeg.P(1) - CloseEQ)^0 * Close / - function (s, o) return s end - -local Comment = lpeg.Rec(lpeg.P"--" * #Open * (LongStringCm1 / function() return end + lpeg.T(tlerror.labels["LongString"])), - lpeg.T(tlerror.labels["LongComment"]), tlerror.labels["LongString"]) + - lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0 - -tllexer.Skip = (Space + Comment)^0 - -local idStart = lpeg.alpha + lpeg.P("_") -local idRest = lpeg.alnum + lpeg.P("_") - -local Keywords = lpeg.P("and") + "break" + "do" + "elseif" + "else" + "end" + - "false" + "for" + "function" + "goto" + "if" + "in" + - "local" + "nil" + "not" + "or" + "repeat" + "return" + - "then" + "true" + "until" + "while" - -tllexer.Reserved = Keywords * -idRest - -local Identifier = idStart * idRest^0 - -tllexer.Name = -tllexer.Reserved * Identifier * -idRest - -function tllexer.token (pat, name) - return pat * tllexer.Skip + updateffp(name) * lpeg.P(false) -end - -function tllexer.symb (str) - return tllexer.token(lpeg.P(str), str) -end - -function tllexer.kw (str) - return tllexer.token(lpeg.P(str) * -idRest, str) -end - -local Hex = (lpeg.P("0x") + lpeg.P("0X")) * tllexer.try(lpeg.xdigit^1, "Number") -local Expo = lpeg.S("eE") * lpeg.S("+-")^-1 * tllexer.try(lpeg.digit^1, "Number") -local Float = (((lpeg.digit^1 * lpeg.P(".") * lpeg.digit^0 * tllexer.try(-lpeg.P("."), "Number")) + - (lpeg.P(".") * lpeg.digit^1)) * Expo^-1) + - (lpeg.digit^1 * Expo) -local Int = lpeg.digit^1 - -tllexer.Number = Hex + Float + Int - -local ShortString = lpeg.P('"') * - ((lpeg.P('\\') * lpeg.P(1)) + (lpeg.P(1) - lpeg.P('"')))^0 * - tllexer.try(lpeg.P('"'), "String") + - lpeg.P("'") * - ((lpeg.P("\\") * lpeg.P(1)) + (lpeg.P(1) - lpeg.P("'")))^0 * - tllexer.try(lpeg.P("'"), "String") - -tllexer.String = LongString + ShortString - --- for error reporting -tllexer.OneWord = tllexer.Name + - tllexer.Number + - tllexer.String + - tllexer.Reserved + - lpeg.P("...") + - lpeg.P(1) - -return tllexer diff --git a/06/deps/lpeglabel/examples/typedlua/tlp.lua b/06/deps/lpeglabel/examples/typedlua/tlp.lua deleted file mode 100644 index 2a4a736..0000000 --- a/06/deps/lpeglabel/examples/typedlua/tlp.lua +++ /dev/null @@ -1,20 +0,0 @@ -local tlparser = require "tlparser" - -local function getcontents(filename) - file = assert(io.open(filename, "r")) - contents = file:read("*a") - file:close() - return contents -end - -if #arg ~= 1 then - print ("Usage: lua tlp.lua ") - os.exit(1) -end - -local filename = arg[1] -local subject = getcontents(filename) -local r, msg = tlparser.parse(subject, filename, false, true) -if not r then print(msg) end - -os.exit(0) diff --git a/06/deps/lpeglabel/examples/typedlua/tlparser.lua b/06/deps/lpeglabel/examples/typedlua/tlparser.lua deleted file mode 100644 index a301fa6..0000000 --- a/06/deps/lpeglabel/examples/typedlua/tlparser.lua +++ /dev/null @@ -1,245 +0,0 @@ -local tlparser = {} - -local lpeg = require "lpeglabel" -lpeg.locale(lpeg) - -local tllexer = require "tllexer" -local tlerror = require "tlerror" - -local function chainl1 (pat, sep, label) - return pat * (sep * tllexer.try(pat, label))^0 -end - -local G = lpeg.P { "TypedLua"; - TypedLua = tllexer.Shebang^-1 * tllexer.Skip * lpeg.V("Chunk") * tllexer.try(-1, "Stat"); - -- type language - Type = lpeg.V("NilableType"); - NilableType = lpeg.V("UnionType") * tllexer.symb("?")^-1; - UnionType = lpeg.V("PrimaryType") * (tllexer.symb("|") * tllexer.try(lpeg.V("PrimaryType"), "UnionType"))^0; - PrimaryType = lpeg.V("LiteralType") + - lpeg.V("BaseType") + - lpeg.V("NilType") + - lpeg.V("ValueType") + - lpeg.V("AnyType") + - lpeg.V("SelfType") + - lpeg.V("FunctionType") + - lpeg.V("TableType") + - lpeg.V("VariableType"); - LiteralType = tllexer.token("false", "false") + - tllexer.token("true", "true") + - tllexer.token(tllexer.Number, "Number") + - tllexer.token(tllexer.String, "String"); - BaseType = tllexer.token("boolean", "boolean") + - tllexer.token("number", "number") + - tllexer.token("string", "string") + - tllexer.token("integer", "integer"); - NilType = tllexer.token("nil", "nil"); - ValueType = tllexer.token("value", "value"); - AnyType = tllexer.token("any", "any"); - SelfType = tllexer.token("self", "self"); - FunctionType = lpeg.V("InputType") * tllexer.symb("->") * tllexer.try(lpeg.V("NilableTuple"), "FunctionType"); - MethodType = lpeg.V("InputType") * tllexer.symb("=>") * tllexer.try(lpeg.V("NilableTuple"), "MethodType"); - InputType = tllexer.symb("(") * lpeg.V("TupleType")^-1 * tllexer.try(tllexer.symb(")"), "MissingCP"); - NilableTuple = lpeg.V("UnionlistType") * tllexer.symb("?")^-1; - UnionlistType = lpeg.V("OutputType") * (tllexer.symb("|") * tllexer.try(lpeg.V("OutputType"), "UnionType"))^0; - OutputType = tllexer.symb("(") * lpeg.V("TupleType")^-1 * tllexer.try(tllexer.symb(")"), "MissingCP"); - TupleType = lpeg.V("Type") * (tllexer.symb(",") * tllexer.try(lpeg.V("Type"), "TupleType"))^0 * tllexer.symb("*")^-1; - TableType = tllexer.symb("{") * lpeg.V("TableTypeBody")^-1 * tllexer.try(tllexer.symb("}"), "MissingCC"); - TableTypeBody = lpeg.V("RecordType") + - lpeg.V("HashType") + - lpeg.V("ArrayType"); - RecordType = lpeg.V("RecordField") * (tllexer.symb(",") * lpeg.V("RecordField"))^0 * - (tllexer.symb(",") * (lpeg.V("HashType") + lpeg.V("ArrayType")))^-1; - RecordField = tllexer.kw("const")^-1 * - lpeg.V("LiteralType") * tllexer.symb(":") * tllexer.try(lpeg.V("Type"), "Type"); - HashType = lpeg.V("KeyType") * tllexer.symb(":") * tllexer.try(lpeg.V("FieldType"), "Type"); - ArrayType = lpeg.V("FieldType"); - KeyType = lpeg.V("BaseType") + lpeg.V("ValueType") + lpeg.V("AnyType"); - FieldType = lpeg.V("Type"); - VariableType = tllexer.token(tllexer.Name, "Name"); - RetType = lpeg.V("NilableTuple") + - lpeg.V("Type"); - Id = tllexer.token(tllexer.Name, "Name"); - TypeDecId = (tllexer.kw("const") * lpeg.V("Id")) + - lpeg.V("Id"); - IdList = lpeg.V("TypeDecId") * (tllexer.symb(",") * tllexer.try(lpeg.V("TypeDecId"), "TupleType"))^0; - IdDec = lpeg.V("IdList") * tllexer.symb(":") * tllexer.try((lpeg.V("Type") + lpeg.V("MethodType")), "Type"); - IdDecList = (lpeg.V("IdDec")^1)^-1; - TypeDec = tllexer.token(tllexer.Name, "Name") * lpeg.V("IdDecList") * tllexer.try(tllexer.kw("end"), "TypeDecEnd"); - Interface = tllexer.kw("interface") * lpeg.V("TypeDec") + - tllexer.kw("typealias") * - tllexer.try(tllexer.token(tllexer.Name, "Name"), "TypeAliasName") * - tllexer.try(tllexer.symb("="), "MissingEqTypeAlias") * lpeg.V("Type"); - -- parser - Chunk = lpeg.V("Block"); - StatList = (tllexer.symb(";") + lpeg.V("Stat"))^0; - Var = lpeg.V("Id"); - TypedId = tllexer.token(tllexer.Name, "Name") * (tllexer.symb(":") * tllexer.try(lpeg.V("Type"), "Type"))^-1; - FunctionDef = tllexer.kw("function") * lpeg.V("FuncBody"); - FieldSep = tllexer.symb(",") + tllexer.symb(";"); - Field = ((tllexer.symb("[") * lpeg.V("Expr") * tllexer.try(tllexer.symb("]"), "MissingCB")) + - (tllexer.token(tllexer.Name, "Name"))) * - tllexer.symb("=") * lpeg.V("Expr") + - lpeg.V("Expr"); - TField = (tllexer.kw("const") * lpeg.V("Field")) + - lpeg.V("Field"); - FieldList = (lpeg.V("TField") * (lpeg.V("FieldSep") * lpeg.V("TField"))^0 * - lpeg.V("FieldSep")^-1)^-1; - Constructor = tllexer.symb("{") * lpeg.V("FieldList") * tllexer.try(tllexer.symb("}"), "MissingCC"); - NameList = lpeg.V("TypedId") * (tllexer.symb(",") * lpeg.V("TypedId"))^0; - ExpList = lpeg.V("Expr") * (tllexer.symb(",") * lpeg.V("Expr"))^0; - FuncArgs = tllexer.symb("(") * - (lpeg.V("Expr") * (tllexer.symb(",") * lpeg.V("Expr"))^0)^-1 * - tllexer.try(tllexer.symb(")"), "MissingCP") + - lpeg.V("Constructor") + - tllexer.token(tllexer.String, "String"); - OrOp = tllexer.kw("or"); - AndOp = tllexer.kw("and"); - RelOp = tllexer.symb("~=") + - tllexer.symb("==") + - tllexer.symb("<=") + - tllexer.symb(">=") + - tllexer.symb("<") + - tllexer.symb(">"); - BOrOp = tllexer.symb("|"); - BXorOp = tllexer.symb("~") * -lpeg.P("="); - BAndOp = tllexer.symb("&"); - ShiftOp = tllexer.symb("<<") + - tllexer.symb(">>"); - ConOp = tllexer.symb(".."); - AddOp = tllexer.symb("+") + - tllexer.symb("-"); - MulOp = tllexer.symb("*") + - tllexer.symb("//") + - tllexer.symb("/") + - tllexer.symb("%"); - UnOp = tllexer.kw("not") + - tllexer.symb("-") + - tllexer.symb("~") + - tllexer.symb("#"); - PowOp = tllexer.symb("^"); - Expr = lpeg.V("SubExpr_1"); - SubExpr_1 = chainl1(lpeg.V("SubExpr_2"), lpeg.V("OrOp"), "SubExpr_1"); - SubExpr_2 = chainl1(lpeg.V("SubExpr_3"), lpeg.V("AndOp"), "SubExpr_2"); - SubExpr_3 = chainl1(lpeg.V("SubExpr_4"), lpeg.V("RelOp"), "SubExpr_3"); - SubExpr_4 = chainl1(lpeg.V("SubExpr_5"), lpeg.V("BOrOp"), "SubExpr_4"); - SubExpr_5 = chainl1(lpeg.V("SubExpr_6"), lpeg.V("BXorOp"), "SubExpr_5"); - SubExpr_6 = chainl1(lpeg.V("SubExpr_7"), lpeg.V("BAndOp"), "SubExpr_6"); - SubExpr_7 = chainl1(lpeg.V("SubExpr_8"), lpeg.V("ShiftOp"), "SubExpr_7"); - SubExpr_8 = lpeg.V("SubExpr_9") * lpeg.V("ConOp") * tllexer.try(lpeg.V("SubExpr_8"), "SubExpr_8") + - lpeg.V("SubExpr_9"); - SubExpr_9 = chainl1(lpeg.V("SubExpr_10"), lpeg.V("AddOp"), "SubExpr_9"); - SubExpr_10 = chainl1(lpeg.V("SubExpr_11"), lpeg.V("MulOp"), "SubExpr_10"); - SubExpr_11 = lpeg.V("UnOp") * tllexer.try(lpeg.V("SubExpr_11"), "SubExpr_11") + - lpeg.V("SubExpr_12"); - SubExpr_12 = lpeg.V("SimpleExp") * (lpeg.V("PowOp") * tllexer.try(lpeg.V("SubExpr_11"), "SubExpr_12"))^-1; - SimpleExp = tllexer.token(tllexer.Number, "Number") + - tllexer.token(tllexer.String, "String") + - tllexer.kw("nil") + - tllexer.kw("false") + - tllexer.kw("true") + - tllexer.symb("...") + - lpeg.V("FunctionDef") + - lpeg.V("Constructor") + - lpeg.V("SuffixedExp"); - SuffixedExp = lpeg.V("PrimaryExp") * ( - (tllexer.symb(".") * tllexer.try(tllexer.token(tllexer.Name, "Name"), "DotIndex")) / "index" + - (tllexer.symb("[") * lpeg.V("Expr") * tllexer.try(tllexer.symb("]"), "MissingCB")) / "index" + - (tllexer.symb(":") * tllexer.try(tllexer.token(tllexer.Name, "Name"), "MethodName") * tllexer.try(lpeg.V("FuncArgs"), "MethodCall")) / "call" + - lpeg.V("FuncArgs") / "call")^0 / function (...) local l = {...}; return l[#l] end; - PrimaryExp = lpeg.V("Var") / "var" + - tllexer.symb("(") * lpeg.V("Expr") * tllexer.try(tllexer.symb(")"), "MissingCP"); - Block = lpeg.V("StatList") * lpeg.V("RetStat")^-1; - IfStat = tllexer.kw("if") * lpeg.V("Expr") * tllexer.try(tllexer.kw("then"), "Then") * lpeg.V("Block") * - (tllexer.kw("elseif") * tllexer.try(lpeg.V("Expr"), "ElseIf") * tllexer.try(tllexer.kw("then"), "Then") * lpeg.V("Block"))^0 * - (tllexer.kw("else") * lpeg.V("Block"))^-1 * - tllexer.try(tllexer.kw("end"), "IfEnd"); - WhileStat = tllexer.kw("while") * lpeg.V("Expr") * - tllexer.try(tllexer.kw("do"), "WhileDo") * lpeg.V("Block") * tllexer.try(tllexer.kw("end"), "WhileEnd"); - DoStat = tllexer.kw("do") * lpeg.V("Block") * tllexer.try(tllexer.kw("end"), "BlockEnd"); - ForBody = tllexer.try(tllexer.kw("do"), "ForDo") * lpeg.V("Block"); - ForNum = lpeg.V("Id") * tllexer.symb("=") * lpeg.V("Expr") * tllexer.symb(",") * - lpeg.V("Expr") * (tllexer.symb(",") * lpeg.V("Expr"))^-1 * - lpeg.V("ForBody"); - ForGen = lpeg.V("NameList") * tllexer.try(tllexer.kw("in"), "ForGen") * - lpeg.V("ExpList") * lpeg.V("ForBody"); - ForStat = tllexer.kw("for") * (lpeg.V("ForNum") + lpeg.V("ForGen")) * tllexer.try(tllexer.kw("end"), "ForEnd"); - RepeatStat = tllexer.kw("repeat") * lpeg.V("Block") * - tllexer.try(tllexer.kw("until"), "Until") * lpeg.V("Expr"); - FuncName = lpeg.V("Id") * (tllexer.symb(".") * - (tllexer.token(tllexer.Name, "Name")))^0 * - (tllexer.symb(":") * (tllexer.token(tllexer.Name, "Name")))^-1; - ParList = lpeg.V("NameList") * (tllexer.symb(",") * tllexer.try(lpeg.V("TypedVarArg"), "ParList"))^-1 + - lpeg.V("TypedVarArg"); - TypedVarArg = tllexer.symb("...") * (tllexer.symb(":") * tllexer.try(lpeg.V("Type"), "Type"))^-1; - FuncBody = tllexer.try(tllexer.symb("("), "MissingOP") * lpeg.V("ParList")^-1 * tllexer.try(tllexer.symb(")"), "MissingCP") * - (tllexer.symb(":") * tllexer.try(lpeg.V("RetType"), "Type"))^-1 * - lpeg.V("Block") * tllexer.try(tllexer.kw("end"), "FuncEnd"); - FuncStat = tllexer.kw("const")^-1 * - tllexer.kw("function") * lpeg.V("FuncName") * lpeg.V("FuncBody"); - LocalFunc = tllexer.kw("function") * - tllexer.try(lpeg.V("Id"), "LocalFunc") * lpeg.V("FuncBody"); - LocalAssign = lpeg.V("NameList") * tllexer.symb("=") * tllexer.try(lpeg.V("ExpList"), "LocalAssign1") + - lpeg.V("NameList") * (#(-tllexer.symb("=") * (lpeg.V("Stat") + -1)) * lpeg.P(true)) + lpeg.T(tlerror.labels["LocalAssign2"]); - LocalStat = tllexer.kw("local") * - (lpeg.V("LocalTypeDec") + lpeg.V("LocalFunc") + lpeg.V("LocalAssign")); - LabelStat = tllexer.symb("::") * tllexer.try(tllexer.token(tllexer.Name, "Name"), "Label1") * tllexer.try(tllexer.symb("::"), "Label2"); - BreakStat = tllexer.kw("break"); - GoToStat = tllexer.kw("goto") * tllexer.token(tllexer.Name, "Name"); - RetStat = tllexer.kw("return") * tllexer.try(-lpeg.V("Stat"), "RetStat") * - (lpeg.V("Expr") * (tllexer.symb(",") * lpeg.V("Expr"))^0)^-1 * - tllexer.symb(";")^-1; - TypeDecStat = lpeg.V("Interface"); - LocalTypeDec = lpeg.V("TypeDecStat"); - LVar = (tllexer.kw("const") * lpeg.V("SuffixedExp")) + - lpeg.V("SuffixedExp"); - ExprStat = lpeg.Cmt(lpeg.V("LVar") * lpeg.V("Assignment"), - function (s, i, ...) - local l = {...} - local i = 1 - while l[i] ~= "=" do - local se = l[i] - if se ~= "var" and se ~= "index" then return false end - i = i + 1 - end - return true - end) + - lpeg.Cmt(lpeg.V("SuffixedExp"), - function (s, i, se) - if se ~= "call" then return false end - return true - end); - Assignment = ((tllexer.symb(",") * lpeg.V("LVar"))^1)^-1 * (tllexer.symb("=") / "=") * lpeg.V("ExpList"); - Stat = lpeg.V("IfStat") + lpeg.V("WhileStat") + lpeg.V("DoStat") + lpeg.V("ForStat") + - lpeg.V("RepeatStat") + lpeg.V("FuncStat") + lpeg.V("LocalStat") + - lpeg.V("LabelStat") + lpeg.V("BreakStat") + lpeg.V("GoToStat") + - lpeg.V("TypeDecStat") + lpeg.V("ExprStat"); -} - -local function lineno (s, i) - if i == 1 then return 1, 1 end - local rest, num = s:sub(1,i):gsub("[^\n]*\n", "") - local r = #rest - return 1 + num, r ~= 0 and r or 1 -end - -function tlparser.parse (subject, filename, strict, integer) - local errorinfo = {} - lpeg.setmaxstack(1000) - local ast, label, suffix = lpeg.match(G, subject, nil, errorinfo, strict, integer) - if not ast then - local line, col = lineno(subject, string.len(subject) - string.len(suffix)) - local error_msg = string.format("%s:%d:%d: ", filename, line, col) - if label ~= 0 then - error_msg = error_msg .. tlerror.errors[label].msg - else - local u = lpeg.match(lpeg.C(tllexer.OneWord) + lpeg.Cc("EOF"), subject, errorinfo.ffp) - error_msg = error_msg .. string.format("unexpected '%s', expecting %s", u, errorinfo.expected) - end - return nil, error_msg - else - return true - end -end - -return tlparser diff --git a/06/deps/lpeglabel/lpcap.c b/06/deps/lpeglabel/lpcap.c deleted file mode 100644 index c9085de..0000000 --- a/06/deps/lpeglabel/lpcap.c +++ /dev/null @@ -1,537 +0,0 @@ -/* -** $Id: lpcap.c,v 1.6 2015/06/15 16:09:57 roberto Exp $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ - -#include "lua.h" -#include "lauxlib.h" - -#include "lpcap.h" -#include "lptypes.h" - - -#define captype(cap) ((cap)->kind) - -#define isclosecap(cap) (captype(cap) == Cclose) - -#define closeaddr(c) ((c)->s + (c)->siz - 1) - -#define isfullcap(cap) ((cap)->siz != 0) - -#define getfromktable(cs,v) lua_rawgeti((cs)->L, ktableidx((cs)->ptop), v) - -#define pushluaval(cs) getfromktable(cs, (cs)->cap->idx) - - - -/* -** Put at the cache for Lua values the value indexed by 'v' in ktable -** of the running pattern (if it is not there yet); returns its index. -*/ -static int updatecache (CapState *cs, int v) { - int idx = cs->ptop + 1; /* stack index of cache for Lua values */ - if (v != cs->valuecached) { /* not there? */ - getfromktable(cs, v); /* get value from 'ktable' */ - lua_replace(cs->L, idx); /* put it at reserved stack position */ - cs->valuecached = v; /* keep track of what is there */ - } - return idx; -} - - -static int pushcapture (CapState *cs); - - -/* -** Goes back in a list of captures looking for an open capture -** corresponding to a close -*/ -static Capture *findopen (Capture *cap) { - int n = 0; /* number of closes waiting an open */ - for (;;) { - cap--; - if (isclosecap(cap)) n++; /* one more open to skip */ - else if (!isfullcap(cap)) - if (n-- == 0) return cap; - } -} - - -/* -** Go to the next capture -*/ -static void nextcap (CapState *cs) { - Capture *cap = cs->cap; - if (!isfullcap(cap)) { /* not a single capture? */ - int n = 0; /* number of opens waiting a close */ - for (;;) { /* look for corresponding close */ - cap++; - if (isclosecap(cap)) { - if (n-- == 0) break; - } - else if (!isfullcap(cap)) n++; - } - } - cs->cap = cap + 1; /* + 1 to skip last close (or entire single capture) */ -} - - -/* -** Push on the Lua stack all values generated by nested captures inside -** the current capture. Returns number of values pushed. 'addextra' -** makes it push the entire match after all captured values. The -** entire match is pushed also if there are no other nested values, -** so the function never returns zero. -*/ -static int pushnestedvalues (CapState *cs, int addextra) { - Capture *co = cs->cap; - if (isfullcap(cs->cap++)) { /* no nested captures? */ - lua_pushlstring(cs->L, co->s, co->siz - 1); /* push whole match */ - return 1; /* that is it */ - } - else { - int n = 0; - while (!isclosecap(cs->cap)) /* repeat for all nested patterns */ - n += pushcapture(cs); - if (addextra || n == 0) { /* need extra? */ - lua_pushlstring(cs->L, co->s, cs->cap->s - co->s); /* push whole match */ - n++; - } - cs->cap++; /* skip close entry */ - return n; - } -} - - -/* -** Push only the first value generated by nested captures -*/ -static void pushonenestedvalue (CapState *cs) { - int n = pushnestedvalues(cs, 0); - if (n > 1) - lua_pop(cs->L, n - 1); /* pop extra values */ -} - - -/* -** Try to find a named group capture with the name given at the top of -** the stack; goes backward from 'cap'. -*/ -static Capture *findback (CapState *cs, Capture *cap) { - lua_State *L = cs->L; - while (cap-- > cs->ocap) { /* repeat until end of list */ - if (isclosecap(cap)) - cap = findopen(cap); /* skip nested captures */ - else if (!isfullcap(cap)) - continue; /* opening an enclosing capture: skip and get previous */ - if (captype(cap) == Cgroup) { - getfromktable(cs, cap->idx); /* get group name */ - if (lp_equal(L, -2, -1)) { /* right group? */ - lua_pop(L, 2); /* remove reference name and group name */ - return cap; - } - else lua_pop(L, 1); /* remove group name */ - } - } - luaL_error(L, "back reference '%s' not found", lua_tostring(L, -1)); - return NULL; /* to avoid warnings */ -} - - -/* -** Back-reference capture. Return number of values pushed. -*/ -static int backrefcap (CapState *cs) { - int n; - Capture *curr = cs->cap; - pushluaval(cs); /* reference name */ - cs->cap = findback(cs, curr); /* find corresponding group */ - n = pushnestedvalues(cs, 0); /* push group's values */ - cs->cap = curr + 1; - return n; -} - - -/* -** Table capture: creates a new table and populates it with nested -** captures. -*/ -static int tablecap (CapState *cs) { - lua_State *L = cs->L; - int n = 0; - lua_newtable(L); - if (isfullcap(cs->cap++)) - return 1; /* table is empty */ - while (!isclosecap(cs->cap)) { - if (captype(cs->cap) == Cgroup && cs->cap->idx != 0) { /* named group? */ - pushluaval(cs); /* push group name */ - pushonenestedvalue(cs); - lua_settable(L, -3); - } - else { /* not a named group */ - int i; - int k = pushcapture(cs); - for (i = k; i > 0; i--) /* store all values into table */ - lua_rawseti(L, -(i + 1), n + i); - n += k; - } - } - cs->cap++; /* skip close entry */ - return 1; /* number of values pushed (only the table) */ -} - - -/* -** Table-query capture -*/ -static int querycap (CapState *cs) { - int idx = cs->cap->idx; - pushonenestedvalue(cs); /* get nested capture */ - lua_gettable(cs->L, updatecache(cs, idx)); /* query cap. value at table */ - if (!lua_isnil(cs->L, -1)) - return 1; - else { /* no value */ - lua_pop(cs->L, 1); /* remove nil */ - return 0; - } -} - - -/* -** Fold capture -*/ -static int foldcap (CapState *cs) { - int n; - lua_State *L = cs->L; - int idx = cs->cap->idx; - if (isfullcap(cs->cap++) || /* no nested captures? */ - isclosecap(cs->cap) || /* no nested captures (large subject)? */ - (n = pushcapture(cs)) == 0) /* nested captures with no values? */ - return luaL_error(L, "no initial value for fold capture"); - if (n > 1) - lua_pop(L, n - 1); /* leave only one result for accumulator */ - while (!isclosecap(cs->cap)) { - lua_pushvalue(L, updatecache(cs, idx)); /* get folding function */ - lua_insert(L, -2); /* put it before accumulator */ - n = pushcapture(cs); /* get next capture's values */ - lua_call(L, n + 1, 1); /* call folding function */ - } - cs->cap++; /* skip close entry */ - return 1; /* only accumulator left on the stack */ -} - - -/* -** Function capture -*/ -static int functioncap (CapState *cs) { - int n; - int top = lua_gettop(cs->L); - pushluaval(cs); /* push function */ - n = pushnestedvalues(cs, 0); /* push nested captures */ - lua_call(cs->L, n, LUA_MULTRET); /* call function */ - return lua_gettop(cs->L) - top; /* return function's results */ -} - - -/* -** Select capture -*/ -static int numcap (CapState *cs) { - int idx = cs->cap->idx; /* value to select */ - if (idx == 0) { /* no values? */ - nextcap(cs); /* skip entire capture */ - return 0; /* no value produced */ - } - else { - int n = pushnestedvalues(cs, 0); - if (n < idx) /* invalid index? */ - return luaL_error(cs->L, "no capture '%d'", idx); - else { - lua_pushvalue(cs->L, -(n - idx + 1)); /* get selected capture */ - lua_replace(cs->L, -(n + 1)); /* put it in place of 1st capture */ - lua_pop(cs->L, n - 1); /* remove other captures */ - return 1; - } - } -} - - -/* -** Return the stack index of the first runtime capture in the given -** list of captures (or zero if no runtime captures) -*/ -int finddyncap (Capture *cap, Capture *last) { - for (; cap < last; cap++) { - if (cap->kind == Cruntime) - return cap->idx; /* stack position of first capture */ - } - return 0; /* no dynamic captures in this segment */ -} - - -/* -** Calls a runtime capture. Returns number of captures removed by -** the call, including the initial Cgroup. (Captures to be added are -** on the Lua stack.) -*/ -int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) { - int n, id; - lua_State *L = cs->L; - int otop = lua_gettop(L); - Capture *open = findopen(close); - assert(captype(open) == Cgroup); - id = finddyncap(open, close); /* get first dynamic capture argument */ - close->kind = Cclose; /* closes the group */ - close->s = s; - cs->cap = open; cs->valuecached = 0; /* prepare capture state */ - luaL_checkstack(L, 4, "too many runtime captures"); - pushluaval(cs); /* push function to be called */ - lua_pushvalue(L, SUBJIDX); /* push original subject */ - lua_pushinteger(L, s - cs->s + 1); /* push current position */ - n = pushnestedvalues(cs, 0); /* push nested captures */ - lua_call(L, n + 2, LUA_MULTRET); /* call dynamic function */ - if (id > 0) { /* are there old dynamic captures to be removed? */ - int i; - for (i = id; i <= otop; i++) - lua_remove(L, id); /* remove old dynamic captures */ - *rem = otop - id + 1; /* total number of dynamic captures removed */ - } - else - *rem = 0; /* no dynamic captures removed */ - return close - open; /* number of captures of all kinds removed */ -} - - -/* -** Auxiliary structure for substitution and string captures: keep -** information about nested captures for future use, avoiding to push -** string results into Lua -*/ -typedef struct StrAux { - int isstring; /* whether capture is a string */ - union { - Capture *cp; /* if not a string, respective capture */ - struct { /* if it is a string... */ - const char *s; /* ... starts here */ - const char *e; /* ... ends here */ - } s; - } u; -} StrAux; - -#define MAXSTRCAPS 10 - -/* -** Collect values from current capture into array 'cps'. Current -** capture must be Cstring (first call) or Csimple (recursive calls). -** (In first call, fills %0 with whole match for Cstring.) -** Returns number of elements in the array that were filled. -*/ -static int getstrcaps (CapState *cs, StrAux *cps, int n) { - int k = n++; - cps[k].isstring = 1; /* get string value */ - cps[k].u.s.s = cs->cap->s; /* starts here */ - if (!isfullcap(cs->cap++)) { /* nested captures? */ - while (!isclosecap(cs->cap)) { /* traverse them */ - if (n >= MAXSTRCAPS) /* too many captures? */ - nextcap(cs); /* skip extra captures (will not need them) */ - else if (captype(cs->cap) == Csimple) /* string? */ - n = getstrcaps(cs, cps, n); /* put info. into array */ - else { - cps[n].isstring = 0; /* not a string */ - cps[n].u.cp = cs->cap; /* keep original capture */ - nextcap(cs); - n++; - } - } - cs->cap++; /* skip close */ - } - cps[k].u.s.e = closeaddr(cs->cap - 1); /* ends here */ - return n; -} - - -/* -** add next capture value (which should be a string) to buffer 'b' -*/ -static int addonestring (luaL_Buffer *b, CapState *cs, const char *what); - - -/* -** String capture: add result to buffer 'b' (instead of pushing -** it into the stack) -*/ -static void stringcap (luaL_Buffer *b, CapState *cs) { - StrAux cps[MAXSTRCAPS]; - int n; - size_t len, i; - const char *fmt; /* format string */ - fmt = lua_tolstring(cs->L, updatecache(cs, cs->cap->idx), &len); - n = getstrcaps(cs, cps, 0) - 1; /* collect nested captures */ - for (i = 0; i < len; i++) { /* traverse them */ - if (fmt[i] != '%') /* not an escape? */ - luaL_addchar(b, fmt[i]); /* add it to buffer */ - else if (fmt[++i] < '0' || fmt[i] > '9') /* not followed by a digit? */ - luaL_addchar(b, fmt[i]); /* add to buffer */ - else { - int l = fmt[i] - '0'; /* capture index */ - if (l > n) - luaL_error(cs->L, "invalid capture index (%d)", l); - else if (cps[l].isstring) - luaL_addlstring(b, cps[l].u.s.s, cps[l].u.s.e - cps[l].u.s.s); - else { - Capture *curr = cs->cap; - cs->cap = cps[l].u.cp; /* go back to evaluate that nested capture */ - if (!addonestring(b, cs, "capture")) - luaL_error(cs->L, "no values in capture index %d", l); - cs->cap = curr; /* continue from where it stopped */ - } - } - } -} - - -/* -** Substitution capture: add result to buffer 'b' -*/ -static void substcap (luaL_Buffer *b, CapState *cs) { - const char *curr = cs->cap->s; - if (isfullcap(cs->cap)) /* no nested captures? */ - luaL_addlstring(b, curr, cs->cap->siz - 1); /* keep original text */ - else { - cs->cap++; /* skip open entry */ - while (!isclosecap(cs->cap)) { /* traverse nested captures */ - const char *next = cs->cap->s; - luaL_addlstring(b, curr, next - curr); /* add text up to capture */ - if (addonestring(b, cs, "replacement")) - curr = closeaddr(cs->cap - 1); /* continue after match */ - else /* no capture value */ - curr = next; /* keep original text in final result */ - } - luaL_addlstring(b, curr, cs->cap->s - curr); /* add last piece of text */ - } - cs->cap++; /* go to next capture */ -} - - -/* -** Evaluates a capture and adds its first value to buffer 'b'; returns -** whether there was a value -*/ -static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) { - switch (captype(cs->cap)) { - case Cstring: - stringcap(b, cs); /* add capture directly to buffer */ - return 1; - case Csubst: - substcap(b, cs); /* add capture directly to buffer */ - return 1; - default: { - lua_State *L = cs->L; - int n = pushcapture(cs); - if (n > 0) { - if (n > 1) lua_pop(L, n - 1); /* only one result */ - if (!lua_isstring(L, -1)) - luaL_error(L, "invalid %s value (a %s)", what, luaL_typename(L, -1)); - luaL_addvalue(b); - } - return n; - } - } -} - - -/* -** Push all values of the current capture into the stack; returns -** number of values pushed -*/ -static int pushcapture (CapState *cs) { - lua_State *L = cs->L; - luaL_checkstack(L, 4, "too many captures"); - switch (captype(cs->cap)) { - case Cposition: { - lua_pushinteger(L, cs->cap->s - cs->s + 1); - cs->cap++; - return 1; - } - case Cconst: { - pushluaval(cs); - cs->cap++; - return 1; - } - case Carg: { - int arg = (cs->cap++)->idx; - if (arg + FIXEDARGS > cs->ptop) - return luaL_error(L, "reference to absent extra argument #%d", arg); - lua_pushvalue(L, arg + FIXEDARGS); - return 1; - } - case Csimple: { - int k = pushnestedvalues(cs, 1); - lua_insert(L, -k); /* make whole match be first result */ - return k; - } - case Cruntime: { - lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */ - return 1; - } - case Cstring: { - luaL_Buffer b; - luaL_buffinit(L, &b); - stringcap(&b, cs); - luaL_pushresult(&b); - return 1; - } - case Csubst: { - luaL_Buffer b; - luaL_buffinit(L, &b); - substcap(&b, cs); - luaL_pushresult(&b); - return 1; - } - case Cgroup: { - if (cs->cap->idx == 0) /* anonymous group? */ - return pushnestedvalues(cs, 0); /* add all nested values */ - else { /* named group: add no values */ - nextcap(cs); /* skip capture */ - return 0; - } - } - case Cbackref: return backrefcap(cs); - case Ctable: return tablecap(cs); - case Cfunction: return functioncap(cs); - case Cnum: return numcap(cs); - case Cquery: return querycap(cs); - case Cfold: return foldcap(cs); - default: assert(0); return 0; - } -} - - -/* -** Prepare a CapState structure and traverse the entire list of -** captures in the stack pushing its results. 's' is the subject -** string, 'r' is the final position of the match, and 'ptop' -** the index in the stack where some useful values were pushed. -** Returns the number of results pushed. (If the list produces no -** results, push the final position of the match.) -*/ -int getcaptures (lua_State *L, const char *s, const char *r, int ptop) { - Capture *capture = (Capture *)lua_touserdata(L, caplistidx(ptop)); - int n = 0; - if (!isclosecap(capture)) { /* is there any capture? */ - CapState cs; - cs.ocap = cs.cap = capture; cs.L = L; - cs.s = s; cs.valuecached = 0; cs.ptop = ptop; - do { /* collect their values */ - n += pushcapture(&cs); - } while (!isclosecap(cs.cap)); - } - if (n == 0) { /* no capture values? */ - lua_pushinteger(L, r - s + 1); /* return only end position */ - n = 1; - } - return n; -} - - diff --git a/06/deps/lpeglabel/lpcap.h b/06/deps/lpeglabel/lpcap.h deleted file mode 100644 index d762fdc..0000000 --- a/06/deps/lpeglabel/lpcap.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -** $Id: lpcap.h,v 1.2 2015/02/27 17:13:17 roberto Exp $ -*/ - -#if !defined(lpcap_h) -#define lpcap_h - - -#include "lptypes.h" - - -/* kinds of captures */ -typedef enum CapKind { - Cclose, Cposition, Cconst, Cbackref, Carg, Csimple, Ctable, Cfunction, - Cquery, Cstring, Cnum, Csubst, Cfold, Cruntime, Cgroup -} CapKind; - - -typedef struct Capture { - const char *s; /* subject position */ - unsigned short idx; /* extra info (group name, arg index, etc.) */ - byte kind; /* kind of capture */ - byte siz; /* size of full capture + 1 (0 = not a full capture) */ -} Capture; - - -typedef struct CapState { - Capture *cap; /* current capture */ - Capture *ocap; /* (original) capture list */ - lua_State *L; - int ptop; /* index of last argument to 'match' */ - const char *s; /* original string */ - int valuecached; /* value stored in cache slot */ -} CapState; - - -int runtimecap (CapState *cs, Capture *close, const char *s, int *rem); -int getcaptures (lua_State *L, const char *s, const char *r, int ptop); -int finddyncap (Capture *cap, Capture *last); - -#endif - - diff --git a/06/deps/lpeglabel/lpcode.c b/06/deps/lpeglabel/lpcode.c deleted file mode 100644 index b2dbba2..0000000 --- a/06/deps/lpeglabel/lpcode.c +++ /dev/null @@ -1,1035 +0,0 @@ -/* -** $Id: lpcode.c,v 1.23 2015/06/12 18:36:47 roberto Exp $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ - -#include - - -#include "lua.h" -#include "lauxlib.h" - -#include "lptypes.h" -#include "lpcode.h" - - -/* signals a "no-instruction */ -#define NOINST -1 - - - -static const Charset fullset_ = - {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; - -static const Charset *fullset = &fullset_; - -/* -** {====================================================== -** Analysis and some optimizations -** ======================================================= -*/ - -/* -** Check whether a charset is empty (returns IFail), singleton (IChar), -** full (IAny), or none of those (ISet). When singleton, '*c' returns -** which character it is. (When generic set, the set was the input, -** so there is no need to return it.) -*/ -static Opcode charsettype (const byte *cs, int *c) { - int count = 0; /* number of characters in the set */ - int i; - int candidate = -1; /* candidate position for the singleton char */ - for (i = 0; i < CHARSETSIZE; i++) { /* for each byte */ - int b = cs[i]; - if (b == 0) { /* is byte empty? */ - if (count > 1) /* was set neither empty nor singleton? */ - return ISet; /* neither full nor empty nor singleton */ - /* else set is still empty or singleton */ - } - else if (b == 0xFF) { /* is byte full? */ - if (count < (i * BITSPERCHAR)) /* was set not full? */ - return ISet; /* neither full nor empty nor singleton */ - else count += BITSPERCHAR; /* set is still full */ - } - else if ((b & (b - 1)) == 0) { /* has byte only one bit? */ - if (count > 0) /* was set not empty? */ - return ISet; /* neither full nor empty nor singleton */ - else { /* set has only one char till now; track it */ - count++; - candidate = i; - } - } - else return ISet; /* byte is neither empty, full, nor singleton */ - } - switch (count) { - case 0: return IFail; /* empty set */ - case 1: { /* singleton; find character bit inside byte */ - int b = cs[candidate]; - *c = candidate * BITSPERCHAR; - if ((b & 0xF0) != 0) { *c += 4; b >>= 4; } - if ((b & 0x0C) != 0) { *c += 2; b >>= 2; } - if ((b & 0x02) != 0) { *c += 1; } - return IChar; - } - default: { - assert(count == CHARSETSIZE * BITSPERCHAR); /* full set */ - return IAny; - } - } -} - - -/* -** A few basic operations on Charsets -*/ -static void cs_complement (Charset *cs) { - loopset(i, cs->cs[i] = ~cs->cs[i]); -} - -static int cs_equal (const byte *cs1, const byte *cs2) { - loopset(i, if (cs1[i] != cs2[i]) return 0); - return 1; -} - -static int cs_disjoint (const Charset *cs1, const Charset *cs2) { - loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;) - return 1; -} - - -/* -** If 'tree' is a 'char' pattern (TSet, TChar, TAny), convert it into a -** charset and return 1; else return 0. -*/ -int tocharset (TTree *tree, Charset *cs) { - switch (tree->tag) { - case TSet: { /* copy set */ - loopset(i, cs->cs[i] = treebuffer(tree)[i]); - return 1; - } - case TChar: { /* only one char */ - assert(0 <= tree->u.n && tree->u.n <= UCHAR_MAX); - loopset(i, cs->cs[i] = 0); /* erase all chars */ - setchar(cs->cs, tree->u.n); /* add that one */ - return 1; - } - case TAny: { - loopset(i, cs->cs[i] = 0xFF); /* add all characters to the set */ - return 1; - } - default: return 0; - } -} - - -/* -** Check whether a pattern tree has captures -*/ -int hascaptures (TTree *tree) { - tailcall: - switch (tree->tag) { - case TCapture: case TRunTime: - return 1; - case TCall: - tree = sib2(tree); goto tailcall; /* return hascaptures(sib2(tree)); */ - case TOpenCall: assert(0); - default: { - switch (numsiblings[tree->tag]) { - case 1: /* return hascaptures(sib1(tree)); */ - tree = sib1(tree); goto tailcall; - case 2: - if (hascaptures(sib1(tree))) return 1; - /* else return hascaptures(sib2(tree)); */ - tree = sib2(tree); goto tailcall; - default: assert(numsiblings[tree->tag] == 0); return 0; - } - } - } -} - - -/* -** Checks how a pattern behaves regarding the empty string, -** in one of two different ways: -** A pattern is *nullable* if it can match without consuming any character; -** A pattern is *nofail* if it never fails for any string -** (including the empty string). -** The difference is only for predicates and run-time captures; -** for other patterns, the two properties are equivalent. -** (With predicates, &'a' is nullable but not nofail. Of course, -** nofail => nullable.) -** These functions are all convervative in the following way: -** p is nullable => nullable(p) -** nofail(p) => p cannot fail -** The function assumes that TOpenCall is not nullable; -** this will be checked again when the grammar is fixed. -** Run-time captures can do whatever they want, so the result -** is conservative. -*/ -int checkaux (TTree *tree, int pred) { - tailcall: - switch (tree->tag) { - case TChar: case TSet: case TAny: - case TFalse: case TOpenCall: case TThrow: /* labeled failure */ - return 0; /* not nullable */ - case TRep: case TTrue: - return 1; /* no fail */ - case TNot: case TBehind: /* can match empty, but can fail */ - if (pred == PEnofail) return 0; - else return 1; /* PEnullable */ - case TAnd: /* can match empty; fail iff body does */ - if (pred == PEnullable) return 1; - /* else return checkaux(sib1(tree), pred); */ - tree = sib1(tree); goto tailcall; - case TRunTime: /* can fail; match empty iff body does */ - if (pred == PEnofail) return 0; - /* else return checkaux(sib1(tree), pred); */ - tree = sib1(tree); goto tailcall; - case TSeq: - if (!checkaux(sib1(tree), pred)) return 0; - /* else return checkaux(sib2(tree), pred); */ - tree = sib2(tree); goto tailcall; - case TChoice: - if (checkaux(sib2(tree), pred)) return 1; - /* else return checkaux(sib1(tree), pred); */ - tree = sib1(tree); goto tailcall; - case TRecov: /* labeled failure */ - /* we do not know whether sib2 will be evaluated */ - tree = sib1(tree); goto tailcall; - case TCapture: case TGrammar: case TRule: - /* return checkaux(sib1(tree), pred); */ - tree = sib1(tree); goto tailcall; - case TCall: /* return checkaux(sib2(tree), pred); */ - tree = sib2(tree); goto tailcall; - default: assert(0); return 0; - } -} - - -/* -** number of characters to match a pattern (or -1 if variable) -** ('count' avoids infinite loops for grammars) -*/ -int fixedlenx (TTree *tree, int count, int len) { - tailcall: - switch (tree->tag) { - case TChar: case TSet: case TAny: - return len + 1; - case TFalse: case TTrue: case TNot: case TAnd: case TBehind: - return len; - case TRep: case TRunTime: case TOpenCall: - case TThrow: /* labeled failure */ - return -1; - case TCapture: case TRule: case TGrammar: - /* return fixedlenx(sib1(tree), count); */ - tree = sib1(tree); goto tailcall; - case TCall: - if (count++ >= MAXRULES) - return -1; /* may be a loop */ - /* else return fixedlenx(sib2(tree), count); */ - tree = sib2(tree); goto tailcall; - case TSeq: case TRecov: { /* labeled failure */ - len = fixedlenx(sib1(tree), count, len); - if (len < 0) return -1; - /* else return fixedlenx(sib2(tree), count, len); */ - tree = sib2(tree); goto tailcall; - } - case TChoice: { - int n1, n2; - n1 = fixedlenx(sib1(tree), count, len); - if (n1 < 0) return -1; - n2 = fixedlenx(sib2(tree), count, len); - if (n1 == n2) return n1; - else return -1; - } - default: assert(0); return 0; - }; -} - - -/* -** Computes the 'first set' of a pattern. -** The result is a conservative aproximation: -** match p ax -> x (for some x) ==> a belongs to first(p) -** or -** a not in first(p) ==> match p ax -> fail (for all x) -** -** The set 'follow' is the first set of what follows the -** pattern (full set if nothing follows it). -** -** The function returns 0 when this resulting set can be used for -** test instructions that avoid the pattern altogether. -** A non-zero return can happen for two reasons: -** 1) match p '' -> '' ==> return has bit 1 set -** (tests cannot be used because they would always fail for an empty input); -** 2) there is a match-time capture ==> return has bit 2 set -** (optimizations should not bypass match-time captures). -*/ -static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) { - tailcall: - switch (tree->tag) { - case TChar: case TSet: case TAny: { - tocharset(tree, firstset); - return 0; - } - case TTrue: { - loopset(i, firstset->cs[i] = follow->cs[i]); - return 1; /* accepts the empty string */ - } - case TFalse: { - loopset(i, firstset->cs[i] = 0); - return 0; - } - case TThrow: { /* labeled failure: must always throw the label */ - loopset(i, firstset->cs[i] = follow->cs[i]); /* follow = fullset(?) */ - return 1; - } - case TChoice: { - Charset csaux; - int e1 = getfirst(sib1(tree), follow, firstset); - int e2 = getfirst(sib2(tree), follow, &csaux); - loopset(i, firstset->cs[i] |= csaux.cs[i]); - return e1 | e2; - } - case TRecov: { /* labeled failure */ - /* when p1 is not nullable, p2 has nothing to contribute; - and when p1 is nullable, then p2 will not match - return getfirst(sib1(tree), fullset, firstset); */ - tree = sib1(tree); follow = fullset; goto tailcall; - } - case TSeq: { - if (!nullable(sib1(tree))) { - /* when p1 is not nullable, p2 has nothing to contribute; - return getfirst(sib1(tree), fullset, firstset); */ - tree = sib1(tree); follow = fullset; goto tailcall; - } - else { /* FIRST(p1 p2, fl) = FIRST(p1, FIRST(p2, fl)) */ - Charset csaux; - int e2 = getfirst(sib2(tree), follow, &csaux); - int e1 = getfirst(sib1(tree), &csaux, firstset); - if (e1 == 0) return 0; /* 'e1' ensures that first can be used */ - else if ((e1 | e2) & 2) /* one of the children has a matchtime? */ - return 2; /* pattern has a matchtime capture */ - else return e2; /* else depends on 'e2' */ - } - } - case TRep: { - getfirst(sib1(tree), follow, firstset); - loopset(i, firstset->cs[i] |= follow->cs[i]); - return 1; /* accept the empty string */ - } - case TCapture: case TGrammar: case TRule: { - /* return getfirst(sib1(tree), follow, firstset); */ - tree = sib1(tree); goto tailcall; - } - case TRunTime: { /* function invalidates any follow info. */ - int e = getfirst(sib1(tree), fullset, firstset); - if (e) return 2; /* function is not "protected"? */ - else return 0; /* pattern inside capture ensures first can be used */ - } - case TCall: { - /* return getfirst(sib2(tree), follow, firstset); */ - tree = sib2(tree); goto tailcall; - } - case TAnd: { - int e = getfirst(sib1(tree), follow, firstset); - loopset(i, firstset->cs[i] &= follow->cs[i]); - return e; - } - case TNot: { - if (tocharset(sib1(tree), firstset)) { - cs_complement(firstset); - return 1; - } - /* else go through */ - } - case TBehind: { /* instruction gives no new information */ - /* call 'getfirst' only to check for math-time captures */ - int e = getfirst(sib1(tree), follow, firstset); - loopset(i, firstset->cs[i] = follow->cs[i]); /* uses follow */ - return e | 1; /* always can accept the empty string */ - } - default: assert(0); return 0; - } -} - - -/* -** If 'headfail(tree)' true, then 'tree' can fail only depending on the -** next character of the subject. -*/ -static int headfail (TTree *tree) { - tailcall: - switch (tree->tag) { - case TChar: case TSet: case TAny: case TFalse: - return 1; - case TTrue: case TRep: case TRunTime: case TNot: - case TBehind: - case TThrow: /* labeled failure: must always throw the label */ - return 0; - case TCapture: case TGrammar: case TRule: case TAnd: - tree = sib1(tree); goto tailcall; /* return headfail(sib1(tree)); */ - case TCall: - tree = sib2(tree); goto tailcall; /* return headfail(sib2(tree)); */ - case TSeq: - if (!nofail(sib2(tree))) return 0; - /* else return headfail(sib1(tree)); */ - tree = sib1(tree); goto tailcall; - case TChoice: case TRecov: /* labeled failure */ - if (!headfail(sib1(tree))) return 0; - /* else return headfail(sib2(tree)); */ - tree = sib2(tree); goto tailcall; - default: assert(0); return 0; - } -} - - -/* -** Check whether the code generation for the given tree can benefit -** from a follow set (to avoid computing the follow set when it is -** not needed) -*/ -static int needfollow (TTree *tree) { - tailcall: - switch (tree->tag) { - case TChar: case TSet: case TAny: - case TFalse: case TTrue: case TAnd: case TNot: - case TRunTime: case TGrammar: case TCall: case TBehind: - case TThrow: case TRecov: /* (?)labeled failure */ - return 0; - case TChoice: case TRep: - return 1; - case TCapture: - tree = sib1(tree); goto tailcall; - case TSeq: - tree = sib2(tree); goto tailcall; - default: assert(0); return 0; - } -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** Code generation -** ======================================================= -*/ - - -/* -** size of an instruction -*/ -int sizei (const Instruction *i) { - switch((Opcode)i->i.code) { - case ISet: case ISpan: return CHARSETINSTSIZE; - case ITestSet: return CHARSETINSTSIZE + 1; - case ITestChar: case ITestAny: case IChoice: case IJmp: case ICall: - case IOpenCall: case ICommit: case IPartialCommit: case IBackCommit: - return 2; - case IThrow: /* labeled failure */ - return 1; - case IRecov: - return (CHARSETINSTSIZE - 1) + 2; /* labeled failure */ - default: return 1; - } -} - - -/* -** state for the compiler -*/ -typedef struct CompileState { - Pattern *p; /* pattern being compiled */ - int ncode; /* next position in p->code to be filled */ - lua_State *L; -} CompileState; - - -/* -** code generation is recursive; 'opt' indicates that the code is being -** generated as the last thing inside an optional pattern (so, if that -** code is optional too, it can reuse the 'IChoice' already in place for -** the outer pattern). 'tt' points to a previous test protecting this -** code (or NOINST). 'fl' is the follow set of the pattern. -*/ -static void codegen (CompileState *compst, TTree *tree, int opt, int tt, - const Charset *fl); - - -void realloccode (lua_State *L, Pattern *p, int nsize) { - void *ud; - lua_Alloc f = lua_getallocf(L, &ud); - void *newblock = f(ud, p->code, p->codesize * sizeof(Instruction), - nsize * sizeof(Instruction)); - if (newblock == NULL && nsize > 0) - luaL_error(L, "not enough memory"); - p->code = (Instruction *)newblock; - p->codesize = nsize; -} - - -static int nextinstruction (CompileState *compst) { - int size = compst->p->codesize; - if (compst->ncode >= size) - realloccode(compst->L, compst->p, size * 2); - return compst->ncode++; -} - - -#define getinstr(cs,i) ((cs)->p->code[i]) - - -static int addinstruction (CompileState *compst, Opcode op, int aux) { - int i = nextinstruction(compst); - getinstr(compst, i).i.code = op; - getinstr(compst, i).i.aux = aux; - return i; -} - - -/* -** Add an instruction followed by space for an offset (to be set later) -*/ -static int addoffsetinst (CompileState *compst, Opcode op) { - int i = addinstruction(compst, op, 0); /* instruction */ - addinstruction(compst, (Opcode)0, 0); /* open space for offset */ - assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2 || - op == IRecov); /* labeled failure */ - return i; -} - - -/* -** Set the offset of an instruction -*/ -static void setoffset (CompileState *compst, int instruction, int offset) { - getinstr(compst, instruction + 1).offset = offset; -} - - -/* -** Add a capture instruction: -** 'op' is the capture instruction; 'cap' the capture kind; -** 'key' the key into ktable; 'aux' is the optional capture offset -** -*/ -static int addinstcap (CompileState *compst, Opcode op, int cap, int key, - int aux) { - int i = addinstruction(compst, op, joinkindoff(cap, aux)); - getinstr(compst, i).i.key = key; - return i; -} - - -#define gethere(compst) ((compst)->ncode) - -#define target(code,i) ((i) + code[i + 1].offset) - - -/* -** Patch 'instruction' to jump to 'target' -*/ -static void jumptothere (CompileState *compst, int instruction, int target) { - if (instruction >= 0) - setoffset(compst, instruction, target - instruction); -} - - -/* -** Patch 'instruction' to jump to current position -*/ -static void jumptohere (CompileState *compst, int instruction) { - jumptothere(compst, instruction, gethere(compst)); -} - - -/* -** Code an IChar instruction, or IAny if there is an equivalent -** test dominating it -*/ -static void codechar (CompileState *compst, int c, int tt) { - if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && - getinstr(compst, tt).i.aux == c) - addinstruction(compst, IAny, 0); - else - addinstruction(compst, IChar, c); -} - - -/* -** Add a charset posfix to an instruction -*/ -static void addcharset (CompileState *compst, const byte *cs) { - int p = gethere(compst); - int i; - for (i = 0; i < (int)CHARSETINSTSIZE - 1; i++) - nextinstruction(compst); /* space for buffer */ - /* fill buffer with charset */ - loopset(j, getinstr(compst, p).buff[j] = cs[j]); -} - - -/* -** code a char set, optimizing unit sets for IChar, "complete" -** sets for IAny, and empty sets for IFail; also use an IAny -** when instruction is dominated by an equivalent test. -*/ -static void codecharset (CompileState *compst, const byte *cs, int tt) { - int c = 0; /* (=) to avoid warnings */ - Opcode op = charsettype(cs, &c); - switch (op) { - case IChar: codechar(compst, c, tt); break; - case ISet: { /* non-trivial set? */ - if (tt >= 0 && getinstr(compst, tt).i.code == ITestSet && - cs_equal(cs, getinstr(compst, tt + 2).buff)) - addinstruction(compst, IAny, 0); - else { - addinstruction(compst, ISet, 0); - addcharset(compst, cs); - } - break; - } - default: addinstruction(compst, op, c); break; - } -} - - -/* -** code a test set, optimizing unit sets for ITestChar, "complete" -** sets for ITestAny, and empty sets for IJmp (always fails). -** 'e' is true iff test should accept the empty string. (Test -** instructions in the current VM never accept the empty string.) -*/ -static int codetestset (CompileState *compst, Charset *cs, int e) { - if (e) return NOINST; /* no test */ - else { - int c = 0; - Opcode op = charsettype(cs->cs, &c); - switch (op) { - case IFail: return addoffsetinst(compst, IJmp); /* always jump */ - case IAny: return addoffsetinst(compst, ITestAny); - case IChar: { - int i = addoffsetinst(compst, ITestChar); - getinstr(compst, i).i.aux = c; - return i; - } - case ISet: { - int i = addoffsetinst(compst, ITestSet); - addcharset(compst, cs->cs); - return i; - } - default: assert(0); return 0; - } - } -} - - -/* -** Find the final destination of a sequence of jumps -*/ -static int finaltarget (Instruction *code, int i) { - while (code[i].i.code == IJmp) - i = target(code, i); - return i; -} - - -/* -** final label (after traversing any jumps) -*/ -static int finallabel (Instruction *code, int i) { - return finaltarget(code, target(code, i)); -} - - -/* -** == behind n;

    (where n = fixedlen(p)) -*/ -static void codebehind (CompileState *compst, TTree *tree) { - if (tree->u.n > 0) - addinstruction(compst, IBehind, tree->u.n); - codegen(compst, sib1(tree), 0, NOINST, fullset); -} - - -/* -** Choice; optimizations: -** - when p1 is headfail or -** when first(p1) and first(p2) are disjoint, than -** a character not in first(p1) cannot go to p1, and a character -** in first(p1) cannot go to p2 (at it is not in first(p2)). -** (The optimization is not valid if p1 accepts the empty string, -** as then there is no character at all...) -** - when p2 is empty and opt is true; a IPartialCommit can reuse -** the Choice already active in the stack. -*/ -static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt, - const Charset *fl) { - int emptyp2 = (p2->tag == TTrue); - Charset cs1, cs2; - int e1 = getfirst(p1, fullset, &cs1); - if (headfail(p1) || - (!e1 && (getfirst(p2, fl, &cs2), cs_disjoint(&cs1, &cs2)))) { - /* == test (fail(p1)) -> L1 ; p1 ; jmp L2; L1: p2; L2: */ - int test = codetestset(compst, &cs1, 0); - int jmp = NOINST; - codegen(compst, p1, 0, test, fl); - if (!emptyp2) - jmp = addoffsetinst(compst, IJmp); - jumptohere(compst, test); - codegen(compst, p2, opt, NOINST, fl); - jumptohere(compst, jmp); - } - else if (opt && emptyp2) { - /* p1? == IPartialCommit; p1 */ - jumptohere(compst, addoffsetinst(compst, IPartialCommit)); - codegen(compst, p1, 1, NOINST, fullset); - } - else { - /* == - test(first(p1)) -> L1; choice L1; ; commit L2; L1: ; L2: */ - int pcommit; - int test = codetestset(compst, &cs1, e1); - int pchoice = addoffsetinst(compst, IChoice); - codegen(compst, p1, emptyp2, test, fullset); - pcommit = addoffsetinst(compst, ICommit); - jumptohere(compst, pchoice); - jumptohere(compst, test); - codegen(compst, p2, opt, NOINST, fl); - jumptohere(compst, pcommit); - } -} - - -/* labeled failure begin */ -static void coderecovery (CompileState *compst, TTree *p1, TTree *p2, int opt, - const Charset *fl, const byte *cs) { - int emptyp2 = (p2->tag == TTrue); - int pcommit; - int test = NOINST; - int precovery = addoffsetinst(compst, IRecov); - addcharset(compst, cs); - codegen(compst, p1, emptyp2, test, fullset); - pcommit = addoffsetinst(compst, ICommit); - jumptohere(compst, precovery); - jumptohere(compst, test); - codegen(compst, p2, opt, NOINST, fl); - addinstruction(compst, IRet, 0); - jumptohere(compst, pcommit); -} -/* labeled failure end */ - - -/* -** And predicate -** optimization: fixedlen(p) = n ==> <&p> ==

    ; behind n -** (valid only when 'p' has no captures) -*/ -static void codeand (CompileState *compst, TTree *tree, int tt) { - int n = fixedlen(tree); - if (n >= 0 && n <= MAXBEHIND && !hascaptures(tree)) { - codegen(compst, tree, 0, tt, fullset); - if (n > 0) - addinstruction(compst, IBehind, n); - } - else { /* default: Choice L1; p1; BackCommit L2; L1: Fail; L2: */ - int pcommit; - int pchoice = addoffsetinst(compst, IChoice); - codegen(compst, tree, 0, tt, fullset); - pcommit = addoffsetinst(compst, IBackCommit); - jumptohere(compst, pchoice); - addinstruction(compst, IFail, 0); - jumptohere(compst, pcommit); - } -} - - -/* -** Captures: if pattern has fixed (and not too big) length, use -** a single IFullCapture instruction after the match; otherwise, -** enclose the pattern with OpenCapture - CloseCapture. -*/ -static void codecapture (CompileState *compst, TTree *tree, int tt, - const Charset *fl) { - int len = fixedlen(sib1(tree)); - if (len >= 0 && len <= MAXOFF && !hascaptures(sib1(tree))) { - codegen(compst, sib1(tree), 0, tt, fl); - addinstcap(compst, IFullCapture, tree->cap, tree->key, len); - } - else { - addinstcap(compst, IOpenCapture, tree->cap, tree->key, 0); - codegen(compst, sib1(tree), 0, tt, fl); - addinstcap(compst, ICloseCapture, Cclose, 0, 0); - } -} - - -static void coderuntime (CompileState *compst, TTree *tree, int tt) { - addinstcap(compst, IOpenCapture, Cgroup, tree->key, 0); - codegen(compst, sib1(tree), 0, tt, fullset); - addinstcap(compst, ICloseRunTime, Cclose, 0, 0); -} - - -/* -** Repetion; optimizations: -** When pattern is a charset, can use special instruction ISpan. -** When pattern is head fail, or if it starts with characters that -** are disjoint from what follows the repetions, a simple test -** is enough (a fail inside the repetition would backtrack to fail -** again in the following pattern, so there is no need for a choice). -** When 'opt' is true, the repetion can reuse the Choice already -** active in the stack. -*/ -static void coderep (CompileState *compst, TTree *tree, int opt, - const Charset *fl) { - Charset st; - if (tocharset(tree, &st)) { - addinstruction(compst, ISpan, 0); - addcharset(compst, st.cs); - } - else { - int e1 = getfirst(tree, fullset, &st); - if (headfail(tree) || (!e1 && cs_disjoint(&st, fl))) { - /* L1: test (fail(p1)) -> L2;

    ; jmp L1; L2: */ - int jmp; - int test = codetestset(compst, &st, 0); - codegen(compst, tree, 0, test, fullset); - jmp = addoffsetinst(compst, IJmp); - jumptohere(compst, test); - jumptothere(compst, jmp, test); - } - else { - /* test(fail(p1)) -> L2; choice L2; L1:

    ; partialcommit L1; L2: */ - /* or (if 'opt'): partialcommit L1; L1:

    ; partialcommit L1; */ - int commit, l2; - int test = codetestset(compst, &st, e1); - int pchoice = NOINST; - if (opt) - jumptohere(compst, addoffsetinst(compst, IPartialCommit)); - else - pchoice = addoffsetinst(compst, IChoice); - l2 = gethere(compst); - codegen(compst, tree, 0, NOINST, fullset); - commit = addoffsetinst(compst, IPartialCommit); - jumptothere(compst, commit, l2); - jumptohere(compst, pchoice); - jumptohere(compst, test); - } - } -} - - -/* -** Not predicate; optimizations: -** In any case, if first test fails, 'not' succeeds, so it can jump to -** the end. If pattern is headfail, that is all (it cannot fail -** in other parts); this case includes 'not' of simple sets. Otherwise, -** use the default code (a choice plus a failtwice). -*/ -static void codenot (CompileState *compst, TTree *tree) { - Charset st; - int e = getfirst(tree, fullset, &st); - int test = codetestset(compst, &st, e); - if (headfail(tree)) /* test (fail(p1)) -> L1; fail; L1: */ - addinstruction(compst, IFail, 0); - else { - /* test(fail(p))-> L1; choice L1;

    ; failtwice; L1: */ - int pchoice = addoffsetinst(compst, IChoice); - codegen(compst, tree, 0, NOINST, fullset); - addinstruction(compst, IFailTwice, 0); - jumptohere(compst, pchoice); - } - jumptohere(compst, test); -} - - -/* -** change open calls to calls, using list 'positions' to find -** correct offsets; also optimize tail calls -*/ -static void correctcalls (CompileState *compst, int *positions, - int from, int to) { - int i; - Instruction *code = compst->p->code; - for (i = from; i < to; i += sizei(&code[i])) { - if (code[i].i.code == IOpenCall) { - int n = code[i].i.key; /* rule number */ - int rule = positions[n]; /* rule position */ - assert(rule == from || code[rule - 1].i.code == IRet); - if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */ - code[i].i.code = IJmp; /* tail call */ - else - code[i].i.code = ICall; - jumptothere(compst, i, rule); /* call jumps to respective rule */ - } - } - assert(i == to); -} - - -/* -** Code for a grammar: -** call L1; jmp L2; L1: rule 1; ret; rule 2; ret; ...; L2: -*/ -static void codegrammar (CompileState *compst, TTree *grammar) { - int positions[MAXRULES]; - int rulenumber = 0; - TTree *rule; - int firstcall = addoffsetinst(compst, ICall); /* call initial rule */ - int jumptoend = addoffsetinst(compst, IJmp); /* jump to the end */ - int start = gethere(compst); /* here starts the initial rule */ - jumptohere(compst, firstcall); - for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { - positions[rulenumber++] = gethere(compst); /* save rule position */ - codegen(compst, sib1(rule), 0, NOINST, fullset); /* code rule */ - addinstruction(compst, IRet, 0); - } - assert(rule->tag == TTrue); - jumptohere(compst, jumptoend); - correctcalls(compst, positions, start, gethere(compst)); -} - - -static void codecall (CompileState *compst, TTree *call) { - int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */ - getinstr(compst, c).i.key = sib2(call)->cap; /* rule number */ - assert(sib2(call)->tag == TRule); -} - - -/* -** Code first child of a sequence -** (second child is called in-place to allow tail call) -** Return 'tt' for second child -*/ -static int codeseq1 (CompileState *compst, TTree *p1, TTree *p2, - int tt, const Charset *fl) { - if (needfollow(p1)) { - Charset fl1; - getfirst(p2, fl, &fl1); /* p1 follow is p2 first */ - codegen(compst, p1, 0, tt, &fl1); - } - else /* use 'fullset' as follow */ - codegen(compst, p1, 0, tt, fullset); - if (fixedlen(p1) != 0) /* can 'p1' consume anything? */ - return NOINST; /* invalidate test */ - else return tt; /* else 'tt' still protects sib2 */ -} - - -/* -** Main code-generation function: dispatch to auxiliar functions -** according to kind of tree. ('needfollow' should return true -** only for consructions that use 'fl'.) -*/ -static void codegen (CompileState *compst, TTree *tree, int opt, int tt, - const Charset *fl) { - tailcall: - switch (tree->tag) { - case TChar: codechar(compst, tree->u.n, tt); break; - case TAny: addinstruction(compst, IAny, 0); break; - case TSet: codecharset(compst, treebuffer(tree), tt); break; - case TTrue: break; - case TFalse: addinstruction(compst, IFail, 0); break; - case TChoice: codechoice(compst, sib1(tree), sib2(tree), opt, fl); break; - case TRep: coderep(compst, sib1(tree), opt, fl); break; - case TBehind: codebehind(compst, tree); break; - case TNot: codenot(compst, sib1(tree)); break; - case TAnd: codeand(compst, sib1(tree), tt); break; - case TCapture: codecapture(compst, tree, tt, fl); break; - case TRunTime: coderuntime(compst, tree, tt); break; - case TGrammar: codegrammar(compst, tree); break; - case TCall: codecall(compst, tree); break; - case TSeq: { - tt = codeseq1(compst, sib1(tree), sib2(tree), tt, fl); /* code 'p1' */ - /* codegen(compst, p2, opt, tt, fl); */ - tree = sib2(tree); goto tailcall; - } - case TThrow: { /* labeled failure */ - addinstruction(compst, IThrow, (byte) tree->u.label); - break; - } - case TRecov: { /* labeled failure */ - coderecovery(compst, sib1(tree), sib2(tree), opt, fl, treelabelset(tree)); - break; - } - default: assert(0); - } -} - - -/* -** Optimize jumps and other jump-like instructions. -** * Update labels of instructions with labels to their final -** destinations (e.g., choice L1; ... L1: jmp L2: becomes -** choice L2) -** * Jumps to other instructions that do jumps become those -** instructions (e.g., jump to return becomes a return; jump -** to commit becomes a commit) -*/ -static void peephole (CompileState *compst) { - Instruction *code = compst->p->code; - int i; - for (i = 0; i < compst->ncode; i += sizei(&code[i])) { - redo: - switch (code[i].i.code) { - case IChoice: case ICall: case ICommit: case IPartialCommit: - case IBackCommit: case ITestChar: case ITestSet: - case IRecov: /* labeled failure */ - case ITestAny: { /* instructions with labels */ - jumptothere(compst, i, finallabel(code, i)); /* optimize label */ - break; - } - case IJmp: { - int ft = finaltarget(code, i); - switch (code[ft].i.code) { /* jumping to what? */ - case IRet: case IFail: case IFailTwice: - case IEnd: { /* instructions with unconditional implicit jumps */ - code[i] = code[ft]; /* jump becomes that instruction */ - code[i + 1].i.code = IAny; /* 'no-op' for target position */ - break; - } - case ICommit: case IPartialCommit: - case IBackCommit: { /* inst. with unconditional explicit jumps */ - int fft = finallabel(code, ft); - code[i] = code[ft]; /* jump becomes that instruction... */ - jumptothere(compst, i, fft); /* but must correct its offset */ - goto redo; /* reoptimize its label */ - } - default: { - jumptothere(compst, i, ft); /* optimize label */ - break; - } - } - break; - } - default: break; - } - } - assert(code[i - 1].i.code == IEnd); -} - - -/* -** Compile a pattern -*/ -Instruction *compile (lua_State *L, Pattern *p) { - CompileState compst; - compst.p = p; compst.ncode = 0; compst.L = L; - realloccode(L, p, 2); /* minimum initial size */ - codegen(&compst, p->tree, 0, NOINST, fullset); - addinstruction(&compst, IEnd, 0); - realloccode(L, p, compst.ncode); /* set final size */ - peephole(&compst); - return p->code; -} - - -/* }====================================================== */ - diff --git a/06/deps/lpeglabel/lpcode.h b/06/deps/lpeglabel/lpcode.h deleted file mode 100644 index 896d3c7..0000000 --- a/06/deps/lpeglabel/lpcode.h +++ /dev/null @@ -1,42 +0,0 @@ -/* -** $Id: lpcode.h,v 1.7 2015/06/12 18:24:45 roberto Exp $ -*/ - -#if !defined(lpcode_h) -#define lpcode_h - -#include "lua.h" - -#include "lptypes.h" -#include "lptree.h" -#include "lpvm.h" - -int tocharset (TTree *tree, Charset *cs); -int checkaux (TTree *tree, int pred); -int fixedlenx (TTree *tree, int count, int len); -int hascaptures (TTree *tree); -int lp_gc (lua_State *L); -Instruction *compile (lua_State *L, Pattern *p); -void realloccode (lua_State *L, Pattern *p, int nsize); -int sizei (const Instruction *i); - - -#define PEnullable 0 -#define PEnofail 1 - -/* -** nofail(t) implies that 't' cannot fail with any input -*/ -#define nofail(t) checkaux(t, PEnofail) - -/* -** (not nullable(t)) implies 't' cannot match without consuming -** something -*/ -#define nullable(t) checkaux(t, PEnullable) - -#define fixedlen(t) fixedlenx(t, 0, 0) - - - -#endif diff --git a/06/deps/lpeglabel/lpeglabel-logo.png b/06/deps/lpeglabel/lpeglabel-logo.png deleted file mode 100644 index 46532af3b252ab1fd5ddf1b4186695bf63ee5da9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14115 zcmajGdpwlg_b}W^ijX2js1#9g7o`lN!=0$45-NwOgd}DR=Ato_4m#nE5Mp$q9FsJM z%ZxKQj*?TZYsUEy_Bf=A#_){$`+eT$`Msa_^LhW+pS}0m>sov5eeJ#1+H0--@bsx; zax$tiixw@Cv$FjA?4m_d$U<7OR028b_y&_?D^H(1cXZLB(CFys`T6+(LB|vF^E9;}uoy#}MZI#_DmTr@PHuxVs?|+2o?d|5{zvu|r?Xthy z)lHD6|FuoWtgKHv+?H2av}n^JtG^GO3;Z#Pp7{8N8NK|{lg%eCn_p_#dE4&8%Il3< z7n05_zv$**mmm3}DSE3HuI_04o zq;T9mX4(I&s3BdEa^Sx$|E*Ll|34p+{_o2Frt&{XepX={m?1Gt$l175eSs3vB@Zz@ z3hVzZYLuvpGXFPF()U7<(2XxMtLXn+qyQCp4|k?q@pC>VlrYFFLrZD5sL|Y?gZ%%Z zy1^L9Chw$VBoe?9gb~G;g+j5&3FuVjN)m>wkY%HEIpB-N8(=AyXYbuO--4nQSTU$Aq20oziw-_X5Znrf zg`ZUlpXC9Tnta2S@dgIR^#E0-I|*6z=CZ z3Rx*YIis=^xD98LRcJs`=;At^93lWZ{r2P#jbsiB^vgD;)d}a9LfY6Ud4u}_UL0{_j_v4WGovrVW!cm&^tKQU#k4k)ZjO8Yb8$3u$12<9y?m zzALe-KplXqgX!93L%F5NU&W!6VwCo%EZMh%i&Qy3ABj8|kzVwpESX0$H_#_V4TbDK z+A}<#%rj_{4_yMuV_ko}CBRueV)+ZrDKYX@i@HGE zo`IO<_&9Z&bWoD4qG_Hx7@CWT#FW#=mF%rak^w8M6kBD7B+s_ds{sdNE&#@_Y zv9tC8lYJNdDMec|Chq3mEw3Bf_Gb|9#1e*{r9VN};R{5-^5?`LH<%q75=JwjoS6O& zM1Go@4S&p7QO1H7`$1CyH8w_6==Z|L2nUCeHH>w}J&N_hrIS`AJ0x_I>A2W>`9U~|5?Mv+vzJf+|(81L~dh-L8A*$i&o zz{2tn#%_OBlPo?@G*Hp{Iuo9uymwS#BSTUK#Wbd$X6|_pO$_hPiuo7gLeU|V))m{% zX=tP!07ptVt{(d2IMPI|u5DmvTF~`mmL~`uTpk6BhuLrHL(=eXDA>;?K;iIN|-7OET zMQCZTFse(!qKb?=TO@vYp)%+ANY)tymnb?+(VQB<=fqMq|B|4_@Hx1?V$$BBk@E)N zqL&b?9{=_HT~ z9-6stKeW~@CCFcB)7M7Gem;6Wo(D_WI&k6|%(}g=++S_vi-^L1nZHJuJZWs)&*@dx zKK%w@zx&9F#O)$$8_;urCW@JCM#!d0ryv1c3U~MMhQp0_q0o)j`sebM3P{px^BI zkCQj5wgHrPst>%*;XYS^c#0;bc8`B@S2Joav$>Xxo`v$+%F#2FXtEwGhJAk7FD(Pe(~#_!y}<1 zt`X#vloA?#S9k=jqbs8t=p>%WqV6c+7>Z{|tPv)xJ2v;)QQ!V+87tQZSYLA$%Li)Wh7XxEdY^ulL{0vI{?)j1pu}x||qbEqzI~pFK zM7XQ@`yQ-S-XH)gymw158#3ja8v?AaU}(@-rtIn)C_$X%~~oy>YJ)NQ&@GB;IsDQz-S)+n{m<`+RCJ z>h%>AL3^wyly5%X@3vy1Lu5X|HRhW*qmx#Z&~aNkLqrnk2q33ZiX)m*0+n8_89YCE z%!(4Pe=PAf@kycj*4Jz-D(}bJ1Wof}iP6YjiMaSVH=gV$Mw7|`w$%DlOvrnwTtOF} ziZt)GEHR9H%D8h?x!_r4m3qbZ@f>q7@oOC>5g(Oi7JRvAyE^FChc!F3+#7V3$9IRE z;k|AlU+FK3uQsvJhKe?9jV}=$ScXWMPp}zF5Odi-V_D?IBlCsNOA!Of?isicez-8*e#c=`;|ct3)u!c9A(m_tFvQ_K8O1e*TRzW z%(f~0I@;R&-)jDhA@?fX{>fgr_QZ7~#8o~z>0QN#SLndw^zoJY*hi$QY_%>635r|k zLpkrtHb_rDyuv^N4nM-eNKq!1`rYAQ!Vu_A?~6k$!(BWl1EpmoLLG3F?@LJ%nW{=x z@2oL1#zgHTzS&!=KdTdxMOU~|e)31Qc6@A`DqUYBy1|s1{HFqM8v+zY$s*d|)#ZID z@uHsQmhgdqUnCE{?x-f3-{#TnxbAT;-kddW+*p_J43|V*TPq&d5IJl2r6h=YIQ(tS zXKkO0dd#X+b~~J0H!wFnmT3KL+unODbE(<5GpvDrpWx@mwEG$gW;JTXyE_MhYm#}K z$WIzGtH=EGk6gVEY!(;w?)lrjpHCB9qXhd)w84hC^_`CRzLck;!~U9`F86e?j_3xr z2a@ALHhV0d!nx@+QW~ z`(K_QHoVQ6MC2GVpH2KUP~>3?SWE+R9zXTxH=H~#%XG)xK3JNGye=;UK;YT(DTv| z=<$6wV%zI8xd(Iou(MPe*0P;PVEYR^7$8d5)mshiyv)RH#SRTUxRXPDU9blhJ#>8jIT z3u}`5+N60@qF{h8dkMPB>t=V!Li2YzZa8E(ScOu3K=8<^F&5i^8xi{ybxW+Pq%p}5 zF>7r)Ty{Dp6CT)G>Qw<32+6{&Z=Tqedvt{abXev#<>^o`lUS-+8>h%0k`1&QCtANE zI;mnVhncve!TMleI~_&ueCW`&mIr@YIBOMwzEw zNrVwKFXfv%YH7nc4cptY(DY7V9R$kSojdlm6l9bu1#9p)Vx_rO+UFZtw#zU!2@9pC z7PPT4^(tH9dgm^*Ph!ruEDl&Vr*%$}<_p#HIn)Lgh#Z_j~2U^7l{M-b>N% ziGC}N&G$9j=+ekU-sfLk2j~o^Iic(M!8^SdD>}BwJDA)T1x_z%W7#O8No0hl5;*HC z_;TlJ9XVZjRw*Z*B+z7<&(6>NS#;@-1XQ?Y1g}t4wlVwQwjHmIUT zYT6X0K;TX>IbA83x%c6tmq?Lwl#WYisD@fK?RY2u>QC+qO`i`2bM-Z5M_FrST+&3r z-0HcxyI<&Tm!n0`$~h0mKN`WJswUGofx=}7Cm%*lBpa&~T4H@Yo*Es(`F1;eiSsrx zv9u^B5v9!q-)QX9!mj`i+aiX?wx9+YYDO?CT@UKHoxZotp$4VFfj1h-Q`2jxuQC~K%elUD z`n2Ztz9#!)G?U7_9n&G2mS@8){XFER@;S)V+tDHJ6&^T#i@J7f_{%8wg^l|Dl>L)I zCgF`a*JV_d2ak!E3ZibICU-q&kQ_Ra^$k(jEB3D`p7d$jTFP135}KF_Y|^#OQY!Q8 z_k6wtb}?|Q0DOvm+Fd5&rRwPw81M6+_@SlIZFX|W0Wy@P#!qh2m&}@fKiaj2f2_-SYWKBX zR@6Ytfl%$5iC+B&VqL|sPjbgFaW^13qVPP;^u$?z2imGc-ExIK5`mKbHwdGBcW7NB z8<7xhd3rPV?~Py*+#N>AZCHUd!`n_t#Cf`3!X?{JFees7*_i&LY=|X%xPL8kBPjBo z`gE?vjGsZrS%nWaF63%kP`?#@)*kgqJF%X76Fn@7%9puVH>_;2#Y`tXcOv zjZc5ws>#!)KESMAP|MKs!<|6$SX|w>OLYfR&1>y<4oo(#e6nVczbD>Ot&(QyMPPfZ zHN>V4j8{o6?&SSlY|(lT@=%7t=||>#F*?xW89flp+wSgj=W<_^*y$OLfw9kq65^!sKszA4!z(!)yrLPeeT z)*Zh;|Loxtc9m;3Jg)01)_ZH=Ac2f>bI0%9+;M4J3#TtLX5U?r4Q|v4(a8gH0@;?$ zsDg(m8m;&!xc9L;5;DNXKmCSR3zc+@-NCoZeUVV^I1I*lLlogh?a*&^_|0=-~#9V{YQazWl=2?*wfxD%@Iva_c51uR> z2Lt$xI>!>>k>{+nwvuX4^6Z(!-&dDzLpWXU;%S2HULKgWyC=vz^0iU5 z^Ub;s%dFM;v}q@`1{3%5+ST{J(l+3wtB*LwQhSC6W8U=pnbTwPXv`*`U-~J@m*Wfi z{pbcMkDAR;`7IrR9xD>>*V9C%46LW`Y5sJ&m;=^~E*gh7JYm$wIuQ14c5^*xM1i@G&}oI!V;XBdLZSIu`QB* z5>bl^BhDZO8dt_dbc|Q0x7FRpi_-mQL$ru8-WZ9tc4DE}z49l#x2NRjBJz~dQX13n z=eK|`O_lO>V|T+^gTZ98Ee_9>UWVzaVU-1)juu#^^l>Ng?YwOFG7ud9;9?btz)m6_ z2IF)7NA|^$)0squ=f?o!YC5&he39M)VBXZC+vk01VPI&X;re5&CbUX0!AvSLJ;iwd<|sZiAq;%0ynue4Uaossg^5 zzN&(TGkZLAzU`N%c4lBGfBYO*_XtH3t1IhMEj@j%&^-PAgqsf20G*k@=E#bNRFnf_oCmCmCx_0?N55PJ)B;Ex1j+QP;Xy zE%bMLdp#|-<*b$RS9hWQCVoe1RTL9tWY#8DuIl0VzI#{>I7RTFU5-*jb1(M;Gxv&Z z|4j2+zxhCJN(>yGv?ofp%^jqe^3{tXt@mIG?&)H?_P&sLTGk|m;1(}Ba`_E;e!W(5 zi^sDua#83#?-mgAi(Is8TKlPp9JC&nA4o)sq6@ZScuC33#dQl7wVPbcWuV3&7!beDOS{l3V5LQWcOS`zkIEs z-kqGAn&H_Sv8-;+R92MsJ<-B;+}EdQ2pN3e(;5F4(09G6z1t6~_(aWDe@w0Vi<_HJ zUgz9+UTXHRcSszNcYRZ{&E1EaU=P$E<7%hzeQOMj4Vu&Pe6Fg$hOzYt)^&e5r5nfC zGG)xTxijT;))D7pagT9T>)%g2Oh`)g+{M;Qs2b!4<#2TZ80gTM5c9jW;t9Pj{Oixx z;h3U7G|!!<;`YH%NYBYr<00bfuVG?5L$H3rGxPUjW{e=c^(k!y;PCdpCO=9@itn(X z5cG+u@lRp#p01y*=RL^tubG&JYwt6=Iw$F1J&e8vBJn&Q6^cT>nVbLFWc39TeEF!U z)mqps?w2}7nP)PdaH#I%A(K+uLT~*#^sP{XwIfip`>fkKVaj4&Z-{=NC}w3^UAOp; za#QGO6^i%AI6F>xXE}lJGvD}TMW27RpRZmj z*JqL>J|g$0v?If#IG-hyn-rsa$bbK!uJfew4I zJgsqgAgpzM-|MRq>-p09(h(n6_)NIJ2hx}JzQrgEta{a%`2?jcPmJh5KXy*4d(8g( zNm2vRZtOsmVMzFkHZ)ttrEdi6ZEQ@j@EX68Wl{a2YQy}Y=w|e~z_aED>XDidG+B6c&(0Z8 zT4zBoE&W-~sw&R2vQKglbiE{g&_%~TeO>Tziagya5trTBx(3aB20n$^{A_qyrgyUF zH%TWVS_U9MZQHf9!YWH4&T2VnUN_MBqe!CIY4uX=-cXaji!(d>*ptO~)vq~)e81wP z>e!`G?5u{`y#(GDu?nMxtpNj8T~?{^Ix~yq{=_w~L3Gs@?H3ZejI$b&Ig7?xVl%!^ zoK6Jfo`s2FXh#@2wG(>M>q{t;kIn%dD=;NMR+!k$d0il5Vp)n_*Umf&JCJO;xN*ZS zC1xXg4n=~D6`1be!KDxmv-ludhHV0&4=KbjcI!%X;LybS_GQ+!&pdgzT8qq;Jk;W^A}iHR(MGTMlylgnf}u1Fm5vPes4U*zq*c#V4ZPVSGMYjp9%X z5vxz{MwJoW@IC~oGKSYmCnmI`A7GP`&cD5|g!&T#2GrP?VMmGmxWzGM-$6<{&1x^_lNMqqsqYw%6j z#G-1IqacxNyx*9@RTMF>Y2FvX)j=T}f+)72;}_ksyjd zde-CHdL+|<*?JCZ__<$T`|U<49+%uhy{X1d?r`ZOjx1vF+3mxOo67NK?r1*C>rb!iJbPp-fzX4McZ@2MF+Yo4Aw3y z5=8eMzMU!3?Zz>iq^HxR5+D8Y-u=RL(>2EXjo4^8zYM+dvKSSwAMaimh=U>$r8ut& zRdn;IOieA9X>ff|blEYOos=s&?B_rl2r%C&hj=4614tYk`QgpD+1RAnjS$&8qPQsP zu1aSSEZm|2Hp&|Hy1h9NgEm`&fSr(dA9P=^$B#)&U^<*Zc4WZ61Lhws47(I5C=$^n zb5(!%c0Z|=NH*rE)kHS!nP3t(jO5&>rI>aQ^D-l)fE6z*e%e2YhkgH_|}rnSKyisFGjlNw6S8?BN&{eIIuK=mq5&cF1|&OarSCRO5%nz zYf{%yo@R`(Y)GLjs(K$H z&nyXj)#3k=FtTUrcxHJ$6P|lod&Q;VU1v%;O~PxX3}zIu_mps-_?~`MpC!GLy$2=; z^r~lx@pv0d+uw1U;0Sp|yx|gbxN-axxrkbMj0NnbPsQ?`m3z1opZ`%|GfD(2kr~w; zNRv^^!&Wg!*e3-$yzJ2HQ&VoEWyq|YRt8o}B&U*9u?@*)%^0G2;}Z0AM1FD6$IzEP z8b@tEQP)mWWhEA&+|HCO*CcGNRF%TE9&*LSy`>QDu^n#LhEocMejb$UOYb(uVkj;( zp)+OcH9cnfip0I;HlgpS>U-Ry;mnM?sVgVE*#{aO3aM%nCuE`f{}1B%*Jmz=P)=j- zYs?qE8_e2)#3;=weW6jasmeRuI{|vZ&Jx4tQ8N~AZ@Zmjl1JUv!C84uGSj=JCG#q? zzZ;j%2eaSC6S9Sq#DVI(=#a?fOP@KKcqyWZ%BH9ML2k?d0Uqz3)@&IG32RB+agr zdm;hJraJ_R*Jj2}6)=nBu=4^`&ec zDI6}(zAtgUYgBW>{^NuHuMfRTzs&jHqG)JWiCHKn55iD~1%LYg%Pqg)w*UWt>XW?| zT0|00{C|D>5~MzO2h-g94hQFb2Ns`N9gZU}myi?L#@zi+*U}CWsTbh2-$fqrX7o^> zcWSt0CPQpG-i+Dt5w0{v%-YonYY}mQHq$)dL+q~|&yE=0rJlwrMGu-rXER0YF%dmBl6Qx?{(d|#* zgHxe#DPa=Ur?f*l8;1A@ccdjK)oK{uH^}8fPE0>GL*er-t1ze4Tu9jci;VD zkZ~KX=7hv~Cod0VcCggaPBNv*^c2jUg;uG+#`kZ(^=L&K<}M;@BiB--|C3 z&*&J*<4$>^cJ83&lfpBE1}EO0@HY-IU&`{5#)jMTPh&MDoPdZ24s12WE{#&!-f)X6 znB*4lW45+SBIoZ*_^h7&vy5rT40Zo9Z%eo69}LZ^>^?!Unr>R=mNa__K_9P0G0xlemHqsJE3PXfz} znI7OE#o93gC_O8YqcDSHP)oZ_-6NU4vkNQF>=nGQP~{;w`?jbSeW;>wjX4QpxxU0* zsMo(skb(G9fbB}KS8~)DlaU~sR7Jr5!KE(;8-c3iup3^5+ARihlGcvU&<+yFlKy-Y zHkK$sF2ftq))90sV#{QNOm0BXkUK)B;Ly%5XmW81i<(`=5^UVJN8IsNhvrSD0@#aGgK}Gg z&W4d3S-;jGrtwo$h3TmNFpb%|u&fq_GCZ+hwXi-r?=j`0%3O=e<%=%n!#t94Rh?2L zZPm}5Ubr<4S|m~DvORgc2F^WPJ<2Sr7xGKQ2i#lqp2PA#xsM>?6FXbnsgbM4KuaUkXfmN@n(8+l** z)Bqx`AT(|QOA%|<^S5yHD1XflMPvrm6`a}u{UmQiA`JFD5Up>cDUZ6?-gjY2Q|Rj@ zBU}mO`rdwoAw{I%8Wy)_;)g^Pg5;nkg}85cfj5L*4YUmxDCdtdZA?MpAU=>ueB-g-ib9NG&dUnB zxB=T(4vOg1NYANY_k@O0TAyz^{)ekZGOE&@Z?_e03h`S6iRCei7chmAjz95b_R6*e zN^-~vO1f!J*%RFoiM(Ef{r+S8eFS(1O89ICUdzFb7&x*QUuwq2jc?HGUyLt=@53jd ziXN9LvSz1#D=m7#(96BLP$zaa4D!GD>FjEJ*UDtAjYt62P&QEbGfCxJEM2M#KN$gCapIj1-7C4SkazR;{3I z@yEHr6W`dqeK4ZDIUsgaCU@Q7Df}G^9e<(~c+*9QrqbIu`)HY;;;AQ~Tq(AVgxH~; z2HLjf538|VOR>sY&gVci>vPzte?(U`z;{$LdS||$L{p1pwd=xf1Ck~@U$eY?pox9X zbHmLW*^m6M)T2dly{CR$c7DzDnv5r);-TX&Uo44w%3|ACupA-lAh)wfwXBgzB)q4A zQseH;$@9(Cf)yU39qGAtlOsOYYV5rwHw&c-aZl@P@FsUG_OJCB#jabMX`2U5?#KEU z_gC2^pyq^E&PlIWJSXspoSw41YdD9<{uv>1^}9BLc)Io?lT7SloT&cKpT9}1cjNm; zeYT%_RncX?5M|+ME{J-iLFG_>g{a)Wl*W$gMw+KK6Cc6*s-}8p^iS^C@dT6Z z&v@1$G2tBx&$K5ZNpSZ&@7nYb80Jn3|HcR{KmK@I2GGM;jQG$%RYmJN{a>f1wH#`K z52|TdAA9tXQj~EvL)5pkWsQtW1}xL>6H+ytjT?e&(w(~{VP^t4+_W1jlM2bGcpf;P zAyTd5q|JIOV6nY9gaMZsrtveP9-Ug1FDN&(Tn^;|)T>5ThuLc2Ph3eT=-78;=G8tY ziF91NglSWPsJWh=X6}flP9B!*@^~WZSeje&!&%BNcg0Fbq^Ek=Qc*G`Z^p|jqmkBQ z5|od_FVI#J$evkRH2-U5(ZkLLLvA>!wF^<36mTG7ee)7*7;;G}^1y)HX7vbAa0%0W zPCXiL8+TPX6CR&s*CA>>1ll0DoAy1CZ>81de9tepS~+vX;_1V|Q)(V{1KA$^E*nCJ z%O3v~dab`!VJn=w7RY?|`dt$Aephq$ zg&vVB;;b9ROZgL5g^uS{KM$3S5;>*7L#D5fVU=zFTx8gw6o&2-xUBi+uj~S+VC~=u zhECC`$oOZmgDxG}6^cItIO3rk&|g~;#>}-F38TN8Z`;3|84$1mupc>%{Ze4>rr{iy zW7(FNEL`2w@zMU*4)S$bD7l!H@k%>lV&=1{c&ys{#RZvw+~6M%J>x$GO`qI}h?GFa zqZLJe%D?w0o~yFkUFu_-17dOTj6M-_e&ijj(92y8&A-1$U;Z)$|Gk+Oj!S@ZYLK={ zh)EUdJtos1`Rha)I+{RJHz7H!_{2+n*!m^Xx&_^fa>A+p*nwo~)1}tlUHGkJ1K-rm zpPT0^Et%=Qnh9vUih_~%VGLA66cl`c>W8_jg~0v|PT8{uO6S|$qMh- z4%!{63C56*wl;yhdt81TY|^G^y8JkA%jQYwJw!#&z7~_WaQz~}c~|eVFhq(1Tn?1# zneR*ZYQG_h+9<6m2Qg*UJZ_)^40$7K3n1eiD-2%pSfUbnA`YMI^r6f{XJ2Qh`O-TZ zdVP#7<9hg4`*IrXSxENsYGv=e4<$N7JlwqL{a5=Yrp>j?lYnyP&0$CDZf`ftI1|2VYAuJLIBOy<@9Ua%w`O1bs(2E(S(7 zu@<5sotH+e>yq?){gd4(Z2V|M!P73E1D1NsNv0PLn>y+}mD>sa90lvNs@8v_eNGTe z60@=x$>aWvnc*gCH`=dcG5S|y+cWBK$qz`_e?Jwxt|t%(YlV;Noef7&1|JBEwD~HJ zV|T1(r+z{l}Fh+}rT4iZA-4hVwqdCw^oYLE`8@#zp3%E<8tzui^=Q zs*n3Xj6)H(JjnF7tYPd^vj+GYHQtUI9(kQN@u}H51-^Nt(9xwP=j-{PZram<9KmWK zWk9*YmtGSEJp~!FfebY*Emm${mB>A)E8e0wzZ^b^y`K(bn9_Df!+LnWM{QHU-X$qo z{Cn)-Z#^ja#lwEQvW76udv78fMqPsyjD5a-py6-7)_V-Q+RUhKmW>;u`9cP)XTtZc zN(cmAU&r-ZomJF@pLyJKZcwAxU_Eo|GjuAsSwGr5bPoi@-3LT>i&!Bei$9)9$W@!ACr7n*t^s2*4 z`4fEm{L%eALq4r1_Q*_EPEd&wOCz#NSoicyG2@5HwJmI3+{@4FRD~^A@3^1fMv~RO zL$)2ElcGTIc(gImy3y#j#BU|Kk$m0jLO=3jau6NB}9-LYlA)&>_?X~|Jb2a83?C9#OXvhVpBJ8x>&m-aFu(axOG&`?G zZh;h|GJ~LL$Zu8Ozt=wRP*ZQ84|=}2!?m&l^-g`NYGk4&!O%0hmKcnvt7YB{s+%V8 z23pQf!RZ_HvAhSVo@6p~BWoCTtrVbnc|W15Cq=NUtMYmyYkQCrVdv5~xC*VBy_42Z zN29C?oa%pmnMBw)Osh9{pF2LpW%a?IQhgsL=oe6h6AErp*25DG;djB0UHrFF5w@kj zby1J>K$U%^#y&*TxA0_D&P3z}l(lYS6ilt_L+y-PZ@H0M8|Z(Y1=Q_VP`Uwf=1KLe zOcNpV0}B@7Pj0AsG8t)-1dG!4fQ$xN+a*XqQV)w$Ve5MgpB#0#_$rWAInM|p@U;6+fZ zo5R!-XfZIUB@-qfhYfKe3CVm)>jp}*`8d9J6z^vIcSwckIYX34V2LW>71&)V;d#Ix zm)xZLTI@JI&iCPl8xMZ*y`sBw_UynUrlI5uEGolJ7wBb@F=FOfB*4B82<$7EnJ;Qk zbAAPL_}{#bS!FP7k^UGu0LEoqbe=$mfL;>WNV4kV)5fSTj?7c(AUi_Fw zjqjTs*j9v?VP_9F!@`<-gB!ku=9-@4ihlpG`@O2VV7dm+3*>!{`StASK!f7g<(RaV zZS4GI!Hq3H;)W!E%`q01H6LIJq_`zRHeg`o^d+mNxStJc$6j9a%6_~6IB4~{b|L@E zs%hOwgVNY$_e2Yokp|_lSw+I|D>PwAig|ZHJCJo~{Oee-BU4nl&L~S!Fj$JtFAJ`k z?3R&}Tz?FkwpPpJ)hof$zYJt>P}XkJ7##a)CC1FGmg`K}-2NdfqNTv<;QghT))fiWT5;u1 zThW#S$4$_c>hPc=8<<{mpO44$x@9+hp>+{6|DO5FP@?dQ)}>~j?L8u{jHI4kF?GDG zT-38)b4(KOhF>2}rU%^hepE2GH($8oK6&0T{?oX@emhmQUG5X_uved1wb}yNCkcyz zFC)U-y)#AoKj~Ctf+EmOv+SSJX6iiLz)W9AZ5QrQGoIh3CjQR^Urs73UxirhZYtwE z`9DoKw?IvDfaV0HmBofGz!-~7EaCl-HECGIVuzdJ_8%rR&#(tnjr>xdKkUIntmX9^ zf0oATePirxO1*H{nYpHnHq3Uq<37B^`JiGw?MpMQu7x%b zHRFX5DuYq@Qs%}k#_3ksicCzuwIZ)@AmIkiiwgvY48w4LOcsY}-xkIBdDOS5ABy=x zJLDme@Gm=Kn6LA-$%MuW|0&SF{#!`%JyxPaKIUQFGlyQ-ecXaVKQm^9_zh)+s`RJS zc5!ozux+x~wg#0wuS9Vbo7$BJ=F;CJce6In`YbBtND%1QRD_)HZ+$&Wj*7m=G$=pD zihnLt?UTonBsavASIA?>&||DJ@k>cSU;1{;68D{Ca~6(BH$!egt)!VJVPG9i(uXU2D-y+sKxPiMzsu*SW5P-nGvfUw+Y;#2wD*ik071euaWn{?{;aleUa zDr;q*YY~Fr2ff3F1|0L_7qQU-tC;Eif}QCF+!`9y>)RWAj#<&Cf;5UiIT(2}BY36wo>e(FZxz>R?l6iC2>UecPurjQ4vnee5 zlQCQqr;ht+CWmYB$Vnd|-GLwU3|>K(7LT+aV+!03TVlP^eo`>BSYqMPym{$^F8Qvg zXJq$%Ulmg1vvb&igO?k^8on!n1{s5)m1n=92MjMO`X5d`hppBUY#B5?IRk=OA6A$M z_e(?inMWIk_!pQsuZZ*l=>t0IvH^nqlBl@1$8XjgEdiRXTw<7hzix~3^pT<7oTXG2 z@0&s58fu~EB#F>V$kN943;J(9yE3$g>3)1{+%DZqEMIlH3mhnq;@zq7_)wNC6;k-= z9`)MvcnRfT!?L!V9K~DIoq-M#^Q()i9MW{x6p6VZi}d}v_z1pFeSqrTfqNZSDv#*@ zF;g1WpEXnZsXu3?G=#s-tb%ymW>838}@&ZT%bZ=i_O<1ZASM8QOTw7#y^wi%uhcAWwFIbbr AH~;_u diff --git a/06/deps/lpeglabel/lpprint.c b/06/deps/lpeglabel/lpprint.c deleted file mode 100644 index 8c488ea..0000000 --- a/06/deps/lpeglabel/lpprint.c +++ /dev/null @@ -1,263 +0,0 @@ -/* -** $Id: lpprint.c,v 1.9 2015/06/15 16:09:57 roberto Exp $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ - -#include -#include -#include - - -#include "lptypes.h" -#include "lpprint.h" -#include "lpcode.h" - - -#if defined(LPEG_DEBUG) - -/* -** {====================================================== -** Printing patterns (for debugging) -** ======================================================= -*/ - - -void printcharset (const byte *st) { - int i; - printf("["); - for (i = 0; i <= UCHAR_MAX; i++) { - int first = i; - while (testchar(st, i) && i <= UCHAR_MAX) i++; - if (i - 1 == first) /* unary range? */ - printf("(%02x)", first); - else if (i - 1 > first) /* non-empty range? */ - printf("(%02x-%02x)", first, i - 1); - } - printf("]"); -} - - -static void printcapkind (int kind) { - const char *const modes[] = { - "close", "position", "constant", "backref", - "argument", "simple", "table", "function", - "query", "string", "num", "substitution", "fold", - "runtime", "group"}; - printf("%s", modes[kind]); -} - - -static void printjmp (const Instruction *op, const Instruction *p) { - printf("-> %d", (int)(p + (p + 1)->offset - op)); -} - - -void printinst (const Instruction *op, const Instruction *p) { - const char *const names[] = { - "any", "char", "set", - "testany", "testchar", "testset", - "span", "behind", - "ret", "end", - "choice", "jmp", "call", "open_call", - "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", - "fullcapture", "opencapture", "closecapture", "closeruntime", - "throw", "recovery" /* labeled failure */ - }; - printf("%02ld: %s ", (long)(p - op), names[p->i.code]); - switch ((Opcode)p->i.code) { - case IChar: { - printf("'%c'", p->i.aux); - break; - } - case ITestChar: { - printf("'%c'", p->i.aux); printjmp(op, p); - break; - } - case IFullCapture: { - printcapkind(getkind(p)); - printf(" (size = %d) (idx = %d)", getoff(p), p->i.key); - break; - } - case IOpenCapture: { - printcapkind(getkind(p)); - printf(" (idx = %d)", p->i.key); - break; - } - case ISet: { - printcharset((p+1)->buff); - break; - } - case ITestSet: { - printcharset((p+2)->buff); printjmp(op, p); - break; - } - case ISpan: { - printcharset((p+1)->buff); - break; - } - case IOpenCall: { - printf("-> %d", (p + 1)->offset); - break; - } - case IBehind: { - printf("%d", p->i.aux); - break; - } - case IJmp: case ICall: case ICommit: case IChoice: - case IPartialCommit: case IBackCommit: case ITestAny: { - printjmp(op, p); - break; - } - case IThrow: { /* labeled failure */ - printf("%d", p->i.aux); - break; - } - case IRecov: { /* labeled failure */ - printjmp(op, p); - printcharset((p+2)->buff); - break; - } - default: break; - } - printf("\n"); -} - - -void printpatt (Instruction *p, int n) { - Instruction *op = p; - while (p < op + n) { - printinst(op, p); - p += sizei(p); - } -} - - -#if defined(LPEG_DEBUG) -static void printcap (Capture *cap) { - printcapkind(cap->kind); - printf(" (idx: %d - size: %d) -> %p\n", cap->idx, cap->siz, cap->s); -} - - -void printcaplist (Capture *cap, Capture *limit) { - printf(">======\n"); - for (; cap->s && (limit == NULL || cap < limit); cap++) - printcap(cap); - printf("=======\n"); -} -#endif - -/* }====================================================== */ - - -/* -** {====================================================== -** Printing trees (for debugging) -** ======================================================= -*/ - -static const char *tagnames[] = { - "char", "set", "any", - "true", "false", - "rep", - "seq", "choice", - "not", "and", - "call", "opencall", "rule", "grammar", - "behind", - "capture", "run-time", - "throw", "labeled-choice", "recov" /* labeled failure */ -}; - - -void printtree (TTree *tree, int ident) { - int i; - for (i = 0; i < ident; i++) printf(" "); - printf("%s", tagnames[tree->tag]); - switch (tree->tag) { - case TChar: { - int c = tree->u.n; - if (isprint(c)) - printf(" '%c'\n", c); - else - printf(" (%02X)\n", c); - break; - } - case TSet: { - printcharset(treebuffer(tree)); - printf("\n"); - break; - } - case TOpenCall: case TCall: { - printf(" key: %d\n", tree->key); - break; - } - case TBehind: { - printf(" %d\n", tree->u.n); - printtree(sib1(tree), ident + 2); - break; - } - case TCapture: { - printf(" cap: %d key: %d n: %d\n", tree->cap, tree->key, tree->u.n); - printtree(sib1(tree), ident + 2); - break; - } - case TRule: { - printf(" n: %d key: %d\n", tree->cap, tree->key); - printtree(sib1(tree), ident + 2); - break; /* do not print next rule as a sibling */ - } - case TGrammar: { - TTree *rule = sib1(tree); - printf(" %d\n", tree->u.n); /* number of rules */ - for (i = 0; i < tree->u.n; i++) { - printtree(rule, ident + 2); - rule = sib2(rule); - } - assert(rule->tag == TTrue); /* sentinel */ - break; - } - case TThrow: { /* labeled failure */ - printf(" labels: %d\n", tree->u.label); - break; - } - default: { - int sibs = numsiblings[tree->tag]; - printf("\n"); - if (tree->tag == TRecov) { /* labeled failure */ - printcharset(treelabelset(tree)); - printf("\n"); - } - if (sibs >= 1) { - printtree(sib1(tree), ident + 2); - if (sibs >= 2) - printtree(sib2(tree), ident + 2); - } - break; - } - } -} - - -void printktable (lua_State *L, int idx) { - int n, i; - lua_getuservalue(L, idx); - if (lua_isnil(L, -1)) /* no ktable? */ - return; - n = lua_rawlen(L, -1); - printf("["); - for (i = 1; i <= n; i++) { - printf("%d = ", i); - lua_rawgeti(L, -1, i); - if (lua_isstring(L, -1)) - printf("%s ", lua_tostring(L, -1)); - else - printf("%s ", lua_typename(L, lua_type(L, -1))); - lua_pop(L, 1); - } - printf("]\n"); - /* leave ktable at the stack */ -} - -/* }====================================================== */ - -#endif diff --git a/06/deps/lpeglabel/lpprint.h b/06/deps/lpeglabel/lpprint.h deleted file mode 100644 index 6329760..0000000 --- a/06/deps/lpeglabel/lpprint.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -** $Id: lpprint.h,v 1.2 2015/06/12 18:18:08 roberto Exp $ -*/ - - -#if !defined(lpprint_h) -#define lpprint_h - - -#include "lptree.h" -#include "lpvm.h" - - -#if defined(LPEG_DEBUG) - -void printpatt (Instruction *p, int n); -void printtree (TTree *tree, int ident); -void printktable (lua_State *L, int idx); -void printcharset (const byte *st); -void printcaplist (Capture *cap, Capture *limit); -void printinst (const Instruction *op, const Instruction *p); - -#else - -#define printktable(L,idx) \ - luaL_error(L, "function only implemented in debug mode") -#define printtree(tree,i) \ - luaL_error(L, "function only implemented in debug mode") -#define printpatt(p,n) \ - luaL_error(L, "function only implemented in debug mode") - -#endif - - -#endif - diff --git a/06/deps/lpeglabel/lptree.c b/06/deps/lpeglabel/lptree.c deleted file mode 100644 index 6633035..0000000 --- a/06/deps/lpeglabel/lptree.c +++ /dev/null @@ -1,1363 +0,0 @@ -/* -** $Id: lptree.c,v 1.21 2015/09/28 17:01:25 roberto Exp $ -** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ - -#include -#include -#include - - -#include "lua.h" -#include "lauxlib.h" - -#include "lptypes.h" -#include "lpcap.h" -#include "lpcode.h" -#include "lpprint.h" -#include "lptree.h" - - -/* number of siblings for each tree */ -const byte numsiblings[] = { - 0, 0, 0, /* char, set, any */ - 0, 0, /* true, false */ - 1, /* rep */ - 2, 2, /* seq, choice */ - 1, 1, /* not, and */ - 0, 0, 2, 1, /* call, opencall, rule, grammar */ - 1, /* behind */ - 1, 1, /* capture, runtime capture */ - 0, 2 /* labeled failure throw, recovery */ -}; - - -static TTree *newgrammar (lua_State *L, int arg); - - -/* -** returns a reasonable name for value at index 'idx' on the stack -*/ -static const char *val2str (lua_State *L, int idx) { - const char *k = lua_tostring(L, idx); - if (k != NULL) - return lua_pushfstring(L, "%s", k); - else - return lua_pushfstring(L, "(a %s)", luaL_typename(L, idx)); -} - - -/* -** Fix a TOpenCall into a TCall node, using table 'postable' to -** translate a key to its rule address in the tree. Raises an -** error if key does not exist. -*/ -static void fixonecall (lua_State *L, int postable, TTree *g, TTree *t) { - int n; - lua_rawgeti(L, -1, t->key); /* get rule's name */ - lua_gettable(L, postable); /* query name in position table */ - n = lua_tonumber(L, -1); /* get (absolute) position */ - lua_pop(L, 1); /* remove position */ - if (n == 0) { /* no position? */ - lua_rawgeti(L, -1, t->key); /* get rule's name again */ - luaL_error(L, "rule '%s' undefined in given grammar", val2str(L, -1)); - } - t->tag = TCall; - t->u.s.ps = n - (t - g); /* position relative to node */ - assert(sib2(t)->tag == TRule); - sib2(t)->key = t->key; -} - - -/* -** Transform left associative constructions into right -** associative ones, for sequence and choice; that is: -** (t11 + t12) + t2 => t11 + (t12 + t2) -** (t11 * t12) * t2 => t11 * (t12 * t2) -** (that is, Op (Op t11 t12) t2 => Op t11 (Op t12 t2)) -*/ -static void correctassociativity (TTree *tree) { - TTree *t1 = sib1(tree); - assert(tree->tag == TChoice || tree->tag == TSeq); - while (t1->tag == tree->tag) { - int n1size = tree->u.s.ps - 1; /* t1 == Op t11 t12 */ - int n11size = t1->u.s.ps - 1; - int n12size = n1size - n11size - 1; - memmove(sib1(tree), sib1(t1), n11size * sizeof(TTree)); /* move t11 */ - tree->u.s.ps = n11size + 1; - sib2(tree)->tag = tree->tag; - sib2(tree)->u.s.ps = n12size + 1; - } -} - - -/* -** Make final adjustments in a tree. Fix open calls in tree 't', -** making them refer to their respective rules or raising appropriate -** errors (if not inside a grammar). Correct associativity of associative -** constructions (making them right associative). Assume that tree's -** ktable is at the top of the stack (for error messages). -*/ -static void finalfix (lua_State *L, int postable, TTree *g, TTree *t) { - tailcall: - switch (t->tag) { - case TGrammar: /* subgrammars were already fixed */ - return; - case TOpenCall: { - if (g != NULL) /* inside a grammar? */ - fixonecall(L, postable, g, t); - else { /* open call outside grammar */ - lua_rawgeti(L, -1, t->key); - luaL_error(L, "rule '%s' used outside a grammar", val2str(L, -1)); - } - break; - } - case TSeq: case TChoice: - correctassociativity(t); - break; - } - switch (numsiblings[t->tag]) { - case 1: /* finalfix(L, postable, g, sib1(t)); */ - t = sib1(t); goto tailcall; - case 2: - finalfix(L, postable, g, sib1(t)); - t = sib2(t); goto tailcall; /* finalfix(L, postable, g, sib2(t)); */ - default: assert(numsiblings[t->tag] == 0); break; - } -} - - - -/* -** {=================================================================== -** KTable manipulation -** -** - The ktable of a pattern 'p' can be shared by other patterns that -** contain 'p' and no other constants. Because of this sharing, we -** should not add elements to a 'ktable' unless it was freshly created -** for the new pattern. -** -** - The maximum index in a ktable is USHRT_MAX, because trees and -** patterns use unsigned shorts to store those indices. -** ==================================================================== -*/ - -/* -** Create a new 'ktable' to the pattern at the top of the stack. -*/ -static void newktable (lua_State *L, int n) { - lua_createtable(L, n, 0); /* create a fresh table */ - lua_setuservalue(L, -2); /* set it as 'ktable' for pattern */ -} - - -/* -** Add element 'idx' to 'ktable' of pattern at the top of the stack; -** Return index of new element. -** If new element is nil, does not add it to table (as it would be -** useless) and returns 0, as ktable[0] is always nil. -*/ -static int addtoktable (lua_State *L, int idx) { - if (lua_isnil(L, idx)) /* nil value? */ - return 0; - else { - int n; - lua_getuservalue(L, -1); /* get ktable from pattern */ - n = lua_rawlen(L, -1); - if (n >= USHRT_MAX) - luaL_error(L, "too many Lua values in pattern"); - lua_pushvalue(L, idx); /* element to be added */ - lua_rawseti(L, -2, ++n); - lua_pop(L, 1); /* remove 'ktable' */ - return n; - } -} - - -/* -** Return the number of elements in the ktable at 'idx'. -** In Lua 5.2/5.3, default "environment" for patterns is nil, not -** a table. Treat it as an empty table. In Lua 5.1, assumes that -** the environment has no numeric indices (len == 0) -*/ -static int ktablelen (lua_State *L, int idx) { - if (!lua_istable(L, idx)) return 0; - else return lua_rawlen(L, idx); -} - - -/* -** Concatentate the contents of table 'idx1' into table 'idx2'. -** (Assume that both indices are negative.) -** Return the original length of table 'idx2' (or 0, if no -** element was added, as there is no need to correct any index). -*/ -static int concattable (lua_State *L, int idx1, int idx2) { - int i; - int n1 = ktablelen(L, idx1); - int n2 = ktablelen(L, idx2); - if (n1 + n2 > USHRT_MAX) - luaL_error(L, "too many Lua values in pattern"); - if (n1 == 0) return 0; /* nothing to correct */ - for (i = 1; i <= n1; i++) { - lua_rawgeti(L, idx1, i); - lua_rawseti(L, idx2 - 1, n2 + i); /* correct 'idx2' */ - } - return n2; -} - - -/* -** When joining 'ktables', constants from one of the subpatterns must -** be renumbered; 'correctkeys' corrects their indices (adding 'n' -** to each of them) -*/ -static void correctkeys (TTree *tree, int n) { - if (n == 0) return; /* no correction? */ - tailcall: - switch (tree->tag) { - case TOpenCall: case TCall: case TRunTime: case TRule: { - if (tree->key > 0) - tree->key += n; - break; - } - case TCapture: { - if (tree->key > 0 && tree->cap != Carg && tree->cap != Cnum) - tree->key += n; - break; - } - default: break; - } - switch (numsiblings[tree->tag]) { - case 1: /* correctkeys(sib1(tree), n); */ - tree = sib1(tree); goto tailcall; - case 2: - correctkeys(sib1(tree), n); - tree = sib2(tree); goto tailcall; /* correctkeys(sib2(tree), n); */ - default: assert(numsiblings[tree->tag] == 0); break; - } -} - - -/* -** Join the ktables from p1 and p2 the ktable for the new pattern at the -** top of the stack, reusing them when possible. -*/ -static void joinktables (lua_State *L, int p1, TTree *t2, int p2) { - int n1, n2; - lua_getuservalue(L, p1); /* get ktables */ - lua_getuservalue(L, p2); - n1 = ktablelen(L, -2); - n2 = ktablelen(L, -1); - if (n1 == 0 && n2 == 0) /* are both tables empty? */ - lua_pop(L, 2); /* nothing to be done; pop tables */ - else if (n2 == 0 || lp_equal(L, -2, -1)) { /* 2nd table empty or equal? */ - lua_pop(L, 1); /* pop 2nd table */ - lua_setuservalue(L, -2); /* set 1st ktable into new pattern */ - } - else if (n1 == 0) { /* first table is empty? */ - lua_setuservalue(L, -3); /* set 2nd table into new pattern */ - lua_pop(L, 1); /* pop 1st table */ - } - else { - lua_createtable(L, n1 + n2, 0); /* create ktable for new pattern */ - /* stack: new p; ktable p1; ktable p2; new ktable */ - concattable(L, -3, -1); /* from p1 into new ktable */ - concattable(L, -2, -1); /* from p2 into new ktable */ - lua_setuservalue(L, -4); /* new ktable becomes 'p' environment */ - lua_pop(L, 2); /* pop other ktables */ - correctkeys(t2, n1); /* correction for indices from p2 */ - } -} - - -/* -** copy 'ktable' of element 'idx' to new tree (on top of stack) -*/ -static void copyktable (lua_State *L, int idx) { - lua_getuservalue(L, idx); - lua_setuservalue(L, -2); -} - - -/* -** merge 'ktable' from 'stree' at stack index 'idx' into 'ktable' -** from tree at the top of the stack, and correct corresponding -** tree. -*/ -static void mergektable (lua_State *L, int idx, TTree *stree) { - int n; - lua_getuservalue(L, -1); /* get ktables */ - lua_getuservalue(L, idx); - n = concattable(L, -1, -2); - lua_pop(L, 2); /* remove both ktables */ - correctkeys(stree, n); -} - - -/* -** Create a new 'ktable' to the pattern at the top of the stack, adding -** all elements from pattern 'p' (if not 0) plus element 'idx' to it. -** Return index of new element. -*/ -static int addtonewktable (lua_State *L, int p, int idx) { - newktable(L, 1); - if (p) - mergektable(L, p, NULL); - return addtoktable(L, idx); -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Tree generation -** ======================================================= -*/ - -/* -** In 5.2, could use 'luaL_testudata'... -*/ -static int testpattern (lua_State *L, int idx) { - if (lua_touserdata(L, idx)) { /* value is a userdata? */ - if (lua_getmetatable(L, idx)) { /* does it have a metatable? */ - luaL_getmetatable(L, PATTERN_T); - if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ - lua_pop(L, 2); /* remove both metatables */ - return 1; - } - } - } - return 0; -} - - -static Pattern *getpattern (lua_State *L, int idx) { - return (Pattern *)luaL_checkudata(L, idx, PATTERN_T); -} - - -static int getsize (lua_State *L, int idx) { - return (lua_rawlen(L, idx) - sizeof(Pattern)) / sizeof(TTree) + 1; -} - - -static TTree *gettree (lua_State *L, int idx, int *len) { - Pattern *p = getpattern(L, idx); - if (len) - *len = getsize(L, idx); - return p->tree; -} - - -/* -** create a pattern. Set its uservalue (the 'ktable') equal to its -** metatable. (It could be any empty sequence; the metatable is at -** hand here, so we use it.) -*/ -static TTree *newtree (lua_State *L, int len) { - size_t size = (len - 1) * sizeof(TTree) + sizeof(Pattern); - Pattern *p = (Pattern *)lua_newuserdata(L, size); - luaL_getmetatable(L, PATTERN_T); - lua_pushvalue(L, -1); - lua_setuservalue(L, -3); - lua_setmetatable(L, -2); - p->code = NULL; p->codesize = 0; - return p->tree; -} - - -static TTree *newleaf (lua_State *L, int tag) { - TTree *tree = newtree(L, 1); - tree->tag = tag; - return tree; -} - - -static TTree *newcharset (lua_State *L) { - TTree *tree = newtree(L, bytes2slots(CHARSETSIZE) + 1); - tree->tag = TSet; - loopset(i, treebuffer(tree)[i] = 0); - return tree; -} - - -/* -** add to tree a sequence where first sibling is 'sib' (with size -** 'sibsize'); returns position for second sibling -*/ -static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { - tree->tag = TSeq; tree->u.s.ps = sibsize + 1; - memcpy(sib1(tree), sib, sibsize * sizeof(TTree)); - return sib2(tree); -} - - -/* -** Build a sequence of 'n' nodes, each with tag 'tag' and 'u.n' got -** from the array 's' (or 0 if array is NULL). (TSeq is binary, so it -** must build a sequence of sequence of sequence...) -*/ -static void fillseq (TTree *tree, int tag, int n, const char *s) { - int i; - for (i = 0; i < n - 1; i++) { /* initial n-1 copies of Seq tag; Seq ... */ - tree->tag = TSeq; tree->u.s.ps = 2; - sib1(tree)->tag = tag; - sib1(tree)->u.n = s ? (byte)s[i] : 0; - tree = sib2(tree); - } - tree->tag = tag; /* last one does not need TSeq */ - tree->u.n = s ? (byte)s[i] : 0; -} - - -/* -** Numbers as patterns: -** 0 == true (always match); n == TAny repeated 'n' times; -** -n == not (TAny repeated 'n' times) -*/ -static TTree *numtree (lua_State *L, int n) { - if (n == 0) - return newleaf(L, TTrue); - else { - TTree *tree, *nd; - if (n > 0) - tree = nd = newtree(L, 2 * n - 1); - else { /* negative: code it as !(-n) */ - n = -n; - tree = newtree(L, 2 * n); - tree->tag = TNot; - nd = sib1(tree); - } - fillseq(nd, TAny, n, NULL); /* sequence of 'n' any's */ - return tree; - } -} - - -/* -** Convert value at index 'idx' to a pattern -*/ -static TTree *getpatt (lua_State *L, int idx, int *len) { - TTree *tree; - switch (lua_type(L, idx)) { - case LUA_TSTRING: { - size_t slen; - const char *s = lua_tolstring(L, idx, &slen); /* get string */ - if (slen == 0) /* empty? */ - tree = newleaf(L, TTrue); /* always match */ - else { - tree = newtree(L, 2 * (slen - 1) + 1); - fillseq(tree, TChar, slen, s); /* sequence of 'slen' chars */ - } - break; - } - case LUA_TNUMBER: { - int n = lua_tointeger(L, idx); - tree = numtree(L, n); - break; - } - case LUA_TBOOLEAN: { - tree = (lua_toboolean(L, idx) ? newleaf(L, TTrue) : newleaf(L, TFalse)); - break; - } - case LUA_TTABLE: { - tree = newgrammar(L, idx); - break; - } - case LUA_TFUNCTION: { - tree = newtree(L, 2); - tree->tag = TRunTime; - tree->key = addtonewktable(L, 0, idx); - sib1(tree)->tag = TTrue; - break; - } - default: { - return gettree(L, idx, len); - } - } - lua_replace(L, idx); /* put new tree into 'idx' slot */ - if (len) - *len = getsize(L, idx); - return tree; -} - - -/* -** create a new tree, whith a new root and one sibling. -** Sibling must be on the Lua stack, at index 1. -*/ -static TTree *newroot1sib (lua_State *L, int tag) { - int s1; - TTree *tree1 = getpatt(L, 1, &s1); - TTree *tree = newtree(L, 1 + s1); /* create new tree */ - tree->tag = tag; - memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); - copyktable(L, 1); - return tree; -} - - -/* -** create a new tree, whith a new root and 2 siblings. -** Siblings must be on the Lua stack, first one at index 1. -*/ -static TTree *newroot2sib (lua_State *L, int tag) { - int s1, s2; - TTree *tree1 = getpatt(L, 1, &s1); - TTree *tree2 = getpatt(L, 2, &s2); - TTree *tree = newtree(L, 1 + s1 + s2); /* create new tree */ - tree->tag = tag; - tree->u.s.ps = 1 + s1; - memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); - memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); - joinktables(L, 1, sib2(tree), 2); - return tree; -} - - -/* labeled failure begin */ -static TTree *newthrowleaf (lua_State *L, int lab) { - TTree *tree = newtree(L, 1); - tree->tag = TThrow; - tree->u.label = lab; - return tree; -} - -static TTree *newrootlab2sib (lua_State *L, int tag) { - int s1, s2; - TTree *tree1 = getpatt(L, 1, &s1); - TTree *tree2 = getpatt(L, 2, &s2); - TTree *tree = newtree(L, bytes2slots(LABELSETSIZE) + 1 + s1 + s2); /* create new tree */ - tree->tag = tag; - tree->u.s.ps = 1 + s1; - tree->u.s.plab = 1 + s1 + s2; - memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); - memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); - loopset(i, treelabelset(tree)[i] = 0); - joinktables(L, 1, sib2(tree), 2); - return tree; -} -/* labeled failure end */ - - - -static int lp_P (lua_State *L) { - luaL_checkany(L, 1); - getpatt(L, 1, NULL); - lua_settop(L, 1); - return 1; -} - - -/* -** sequence operator; optimizations: -** false x => false, x true => x, true x => x -** (cannot do x . false => false because x may have runtime captures) -*/ -static int lp_seq (lua_State *L) { - TTree *tree1 = getpatt(L, 1, NULL); - TTree *tree2 = getpatt(L, 2, NULL); - if (tree1->tag == TFalse || tree2->tag == TTrue) - lua_pushvalue(L, 1); /* false . x == false, x . true = x */ - else if (tree1->tag == TTrue) - lua_pushvalue(L, 2); /* true . x = x */ - else - newroot2sib(L, TSeq); - return 1; -} - - -/* -** choice operator; optimizations: -** charset / charset => charset -** true / x => true, x / false => x, false / x => x -** (x / true is not equivalent to true) -*/ -static int lp_choice (lua_State *L) { - Charset st1, st2; - TTree *t1 = getpatt(L, 1, NULL); - TTree *t2 = getpatt(L, 2, NULL); - if (tocharset(t1, &st1) && tocharset(t2, &st2)) { - TTree *t = newcharset(L); - loopset(i, treebuffer(t)[i] = st1.cs[i] | st2.cs[i]); - } - else if (nofail(t1) || t2->tag == TFalse) - lua_pushvalue(L, 1); /* true / x => true, x / false => x */ - else if (t1->tag == TFalse) - lua_pushvalue(L, 2); /* false / x => x */ - else - newroot2sib(L, TChoice); - return 1; -} - - -/* -** p^n -*/ -static int lp_star (lua_State *L) { - int size1; - int n = (int)luaL_checkinteger(L, 2); - TTree *tree1 = getpatt(L, 1, &size1); - if (n >= 0) { /* seq tree1 (seq tree1 ... (seq tree1 (rep tree1))) */ - TTree *tree = newtree(L, (n + 1) * (size1 + 1)); - if (nullable(tree1)) - luaL_error(L, "loop body may accept empty string"); - while (n--) /* repeat 'n' times */ - tree = seqaux(tree, tree1, size1); - tree->tag = TRep; - memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); - } - else { /* choice (seq tree1 ... choice tree1 true ...) true */ - TTree *tree; - n = -n; - /* size = (choice + seq + tree1 + true) * n, but the last has no seq */ - tree = newtree(L, n * (size1 + 3) - 1); - for (; n > 1; n--) { /* repeat (n - 1) times */ - tree->tag = TChoice; tree->u.s.ps = n * (size1 + 3) - 2; - sib2(tree)->tag = TTrue; - tree = sib1(tree); - tree = seqaux(tree, tree1, size1); - } - tree->tag = TChoice; tree->u.s.ps = size1 + 1; - sib2(tree)->tag = TTrue; - memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); - } - copyktable(L, 1); - return 1; -} - - -/* -** #p == &p -*/ -static int lp_and (lua_State *L) { - newroot1sib(L, TAnd); - return 1; -} - - -/* -** -p == !p -*/ -static int lp_not (lua_State *L) { - newroot1sib(L, TNot); - return 1; -} - - -/* -** [t1 - t2] == Seq (Not t2) t1 -** If t1 and t2 are charsets, make their difference. -*/ -static int lp_sub (lua_State *L) { - Charset st1, st2; - int s1, s2; - TTree *t1 = getpatt(L, 1, &s1); - TTree *t2 = getpatt(L, 2, &s2); - if (tocharset(t1, &st1) && tocharset(t2, &st2)) { - TTree *t = newcharset(L); - loopset(i, treebuffer(t)[i] = st1.cs[i] & ~st2.cs[i]); - } - else { - TTree *tree = newtree(L, 2 + s1 + s2); - tree->tag = TSeq; /* sequence of... */ - tree->u.s.ps = 2 + s2; - sib1(tree)->tag = TNot; /* ...not... */ - memcpy(sib1(sib1(tree)), t2, s2 * sizeof(TTree)); /* ...t2 */ - memcpy(sib2(tree), t1, s1 * sizeof(TTree)); /* ... and t1 */ - joinktables(L, 1, sib1(tree), 2); - } - return 1; -} - - -static int lp_set (lua_State *L) { - size_t l; - const char *s = luaL_checklstring(L, 1, &l); - TTree *tree = newcharset(L); - while (l--) { - setchar(treebuffer(tree), (byte)(*s)); - s++; - } - return 1; -} - - -static int lp_range (lua_State *L) { - int arg; - int top = lua_gettop(L); - TTree *tree = newcharset(L); - for (arg = 1; arg <= top; arg++) { - int c; - size_t l; - const char *r = luaL_checklstring(L, arg, &l); - luaL_argcheck(L, l == 2, arg, "range must have two characters"); - for (c = (byte)r[0]; c <= (byte)r[1]; c++) - setchar(treebuffer(tree), c); - } - return 1; -} - - -/* -** Look-behind predicate -*/ -static int lp_behind (lua_State *L) { - TTree *tree; - TTree *tree1 = getpatt(L, 1, NULL); - int n = fixedlen(tree1); - luaL_argcheck(L, n >= 0, 1, "pattern may not have fixed length"); - luaL_argcheck(L, !hascaptures(tree1), 1, "pattern have captures"); - luaL_argcheck(L, n <= MAXBEHIND, 1, "pattern too long to look behind"); - tree = newroot1sib(L, TBehind); - tree->u.n = n; - return 1; -} - - -/* labeled failure begin */ -/* -** Throws a label -*/ -static int lp_throw (lua_State *L) { - int label = luaL_checkinteger(L, -1); - luaL_argcheck(L, label >= 1 && label < MAXLABELS, -1, "the number of a label must be between 1 and 255"); - newthrowleaf(L, label); - return 1; -} - -/* -** labeled recovery function -*/ -static int lp_recovery (lua_State *L) { - int n = lua_gettop(L); - TTree *tree = newrootlab2sib(L, TRecov); - luaL_argcheck(L, n >= 3, 3, "non-nil value expected"); - if (n == 2) { /* catches fail as default */ - /*setlabel(treelabelset(tree), LFAIL); recovery does not catch regular fail */ - } else { - int i; - for (i = 3; i <= n; i++) { - int d = luaL_checkinteger(L, i); - luaL_argcheck(L, d >= 1 && d < MAXLABELS, i, "the number of a label must be between 1 and 255"); - setlabel(treelabelset(tree), (byte)d); - } - } - return 1; -} - -/* labeled failure end */ - - -/* -** Create a non-terminal -*/ -static int lp_V (lua_State *L) { - TTree *tree = newleaf(L, TOpenCall); - luaL_argcheck(L, !lua_isnoneornil(L, 1), 1, "non-nil value expected"); - tree->key = addtonewktable(L, 0, 1); - return 1; -} - - -/* -** Create a tree for a non-empty capture, with a body and -** optionally with an associated Lua value (at index 'labelidx' in the -** stack) -*/ -static int capture_aux (lua_State *L, int cap, int labelidx) { - TTree *tree = newroot1sib(L, TCapture); - tree->cap = cap; - tree->key = (labelidx == 0) ? 0 : addtonewktable(L, 1, labelidx); - return 1; -} - - -/* -** Fill a tree with an empty capture, using an empty (TTrue) sibling. -*/ -static TTree *auxemptycap (TTree *tree, int cap) { - tree->tag = TCapture; - tree->cap = cap; - sib1(tree)->tag = TTrue; - return tree; -} - - -/* -** Create a tree for an empty capture -*/ -static TTree *newemptycap (lua_State *L, int cap) { - return auxemptycap(newtree(L, 2), cap); -} - - -/* -** Create a tree for an empty capture with an associated Lua value -*/ -static TTree *newemptycapkey (lua_State *L, int cap, int idx) { - TTree *tree = auxemptycap(newtree(L, 2), cap); - tree->key = addtonewktable(L, 0, idx); - return tree; -} - - -/* -** Captures with syntax p / v -** (function capture, query capture, string capture, or number capture) -*/ -static int lp_divcapture (lua_State *L) { - switch (lua_type(L, 2)) { - case LUA_TFUNCTION: return capture_aux(L, Cfunction, 2); - case LUA_TTABLE: return capture_aux(L, Cquery, 2); - case LUA_TSTRING: return capture_aux(L, Cstring, 2); - case LUA_TNUMBER: { - int n = lua_tointeger(L, 2); - TTree *tree = newroot1sib(L, TCapture); - luaL_argcheck(L, 0 <= n && n <= SHRT_MAX, 1, "invalid number"); - tree->cap = Cnum; - tree->key = n; - return 1; - } - default: return luaL_argerror(L, 2, "invalid replacement value"); - } -} - - -static int lp_substcapture (lua_State *L) { - return capture_aux(L, Csubst, 0); -} - - -static int lp_tablecapture (lua_State *L) { - return capture_aux(L, Ctable, 0); -} - - -static int lp_groupcapture (lua_State *L) { - if (lua_isnoneornil(L, 2)) - return capture_aux(L, Cgroup, 0); - else - return capture_aux(L, Cgroup, 2); -} - - -static int lp_foldcapture (lua_State *L) { - luaL_checktype(L, 2, LUA_TFUNCTION); - return capture_aux(L, Cfold, 2); -} - - -static int lp_simplecapture (lua_State *L) { - return capture_aux(L, Csimple, 0); -} - - -static int lp_poscapture (lua_State *L) { - newemptycap(L, Cposition); - return 1; -} - - -static int lp_argcapture (lua_State *L) { - int n = (int)luaL_checkinteger(L, 1); - TTree *tree = newemptycap(L, Carg); - tree->key = n; - luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index"); - return 1; -} - - -static int lp_backref (lua_State *L) { - luaL_checkany(L, 1); - newemptycapkey(L, Cbackref, 1); - return 1; -} - - -/* -** Constant capture -*/ -static int lp_constcapture (lua_State *L) { - int i; - int n = lua_gettop(L); /* number of values */ - if (n == 0) /* no values? */ - newleaf(L, TTrue); /* no capture */ - else if (n == 1) - newemptycapkey(L, Cconst, 1); /* single constant capture */ - else { /* create a group capture with all values */ - TTree *tree = newtree(L, 1 + 3 * (n - 1) + 2); - newktable(L, n); /* create a 'ktable' for new tree */ - tree->tag = TCapture; - tree->cap = Cgroup; - tree->key = 0; - tree = sib1(tree); - for (i = 1; i <= n - 1; i++) { - tree->tag = TSeq; - tree->u.s.ps = 3; /* skip TCapture and its sibling */ - auxemptycap(sib1(tree), Cconst); - sib1(tree)->key = addtoktable(L, i); - tree = sib2(tree); - } - auxemptycap(tree, Cconst); - tree->key = addtoktable(L, i); - } - return 1; -} - - -static int lp_matchtime (lua_State *L) { - TTree *tree; - luaL_checktype(L, 2, LUA_TFUNCTION); - tree = newroot1sib(L, TRunTime); - tree->key = addtonewktable(L, 1, 2); - return 1; -} - -/* }====================================================== */ - - -/* -** {====================================================== -** Grammar - Tree generation -** ======================================================= -*/ - -/* -** push on the stack the index and the pattern for the -** initial rule of grammar at index 'arg' in the stack; -** also add that index into position table. -*/ -static void getfirstrule (lua_State *L, int arg, int postab) { - lua_rawgeti(L, arg, 1); /* access first element */ - if (lua_isstring(L, -1)) { /* is it the name of initial rule? */ - lua_pushvalue(L, -1); /* duplicate it to use as key */ - lua_gettable(L, arg); /* get associated rule */ - } - else { - lua_pushinteger(L, 1); /* key for initial rule */ - lua_insert(L, -2); /* put it before rule */ - } - if (!testpattern(L, -1)) { /* initial rule not a pattern? */ - if (lua_isnil(L, -1)) - luaL_error(L, "grammar has no initial rule"); - else - luaL_error(L, "initial rule '%s' is not a pattern", lua_tostring(L, -2)); - } - lua_pushvalue(L, -2); /* push key */ - lua_pushinteger(L, 1); /* push rule position (after TGrammar) */ - lua_settable(L, postab); /* insert pair at position table */ -} - -/* -** traverse grammar at index 'arg', pushing all its keys and patterns -** into the stack. Create a new table (before all pairs key-pattern) to -** collect all keys and their associated positions in the final tree -** (the "position table"). -** Return the number of rules and (in 'totalsize') the total size -** for the new tree. -*/ -static int collectrules (lua_State *L, int arg, int *totalsize) { - int n = 1; /* to count number of rules */ - int postab = lua_gettop(L) + 1; /* index of position table */ - int size; /* accumulator for total size */ - lua_newtable(L); /* create position table */ - getfirstrule(L, arg, postab); - size = 2 + getsize(L, postab + 2); /* TGrammar + TRule + rule */ - lua_pushnil(L); /* prepare to traverse grammar table */ - while (lua_next(L, arg) != 0) { - if (lua_tonumber(L, -2) == 1 || - lp_equal(L, -2, postab + 1)) { /* initial rule? */ - lua_pop(L, 1); /* remove value (keep key for lua_next) */ - continue; - } - if (!testpattern(L, -1)) /* value is not a pattern? */ - luaL_error(L, "rule '%s' is not a pattern", val2str(L, -2)); - luaL_checkstack(L, LUA_MINSTACK, "grammar has too many rules"); - lua_pushvalue(L, -2); /* push key (to insert into position table) */ - lua_pushinteger(L, size); - lua_settable(L, postab); - size += 1 + getsize(L, -1); /* update size */ - lua_pushvalue(L, -2); /* push key (for next lua_next) */ - n++; - } - *totalsize = size + 1; /* TTrue to finish list of rules */ - return n; -} - - -static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { - int i; - TTree *nd = sib1(grammar); /* auxiliary pointer to traverse the tree */ - for (i = 0; i < n; i++) { /* add each rule into new tree */ - int ridx = frule + 2*i + 1; /* index of i-th rule */ - int rulesize; - TTree *rn = gettree(L, ridx, &rulesize); - nd->tag = TRule; - nd->key = 0; - nd->cap = i; /* rule number */ - nd->u.s.ps = rulesize + 1; /* point to next rule */ - memcpy(sib1(nd), rn, rulesize * sizeof(TTree)); /* copy rule */ - mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ - nd = sib2(nd); /* move to next rule */ - } - nd->tag = TTrue; /* finish list of rules */ -} - - -/* -** Check whether a tree has potential infinite loops -*/ -static int checkloops (TTree *tree) { - tailcall: - if (tree->tag == TRep && nullable(sib1(tree))) - return 1; - else if (tree->tag == TGrammar) - return 0; /* sub-grammars already checked */ - else { - switch (numsiblings[tree->tag]) { - case 1: /* return checkloops(sib1(tree)); */ - tree = sib1(tree); goto tailcall; - case 2: - if (checkloops(sib1(tree))) return 1; - /* else return checkloops(sib2(tree)); */ - tree = sib2(tree); goto tailcall; - default: assert(numsiblings[tree->tag] == 0); return 0; - } - } -} - - -static int verifyerror (lua_State *L, int *passed, int npassed) { - int i, j; - for (i = npassed - 1; i >= 0; i--) { /* search for a repetition */ - for (j = i - 1; j >= 0; j--) { - if (passed[i] == passed[j]) { - lua_rawgeti(L, -1, passed[i]); /* get rule's key */ - return luaL_error(L, "rule '%s' may be left recursive", val2str(L, -1)); - } - } - } - return luaL_error(L, "too many left calls in grammar"); -} - - -/* -** Check whether a rule can be left recursive; raise an error in that -** case; otherwise return 1 iff pattern is nullable. -** The return value is used to check sequences, where the second pattern -** is only relevant if the first is nullable. -** Parameter 'nb' works as an accumulator, to allow tail calls in -** choices. ('nb' true makes function returns true.) -** Assume ktable at the top of the stack. -*/ -static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed, - int nb) { - tailcall: - switch (tree->tag) { - case TChar: case TSet: case TAny: - case TFalse: case TThrow: /* labeled failure */ - return nb; /* cannot pass from here */ - case TTrue: - case TBehind: /* look-behind cannot have calls */ - return 1; - case TNot: case TAnd: case TRep: - /* return verifyrule(L, sib1(tree), passed, npassed, 1); */ - tree = sib1(tree); nb = 1; goto tailcall; - case TCapture: case TRunTime: - /* return verifyrule(L, sib1(tree), passed, npassed, nb); */ - tree = sib1(tree); goto tailcall; - case TCall: - /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ - tree = sib2(tree); goto tailcall; - case TSeq: /* only check 2nd child if first is nb */ - if (!verifyrule(L, sib1(tree), passed, npassed, 0)) - return nb; - /* else return verifyrule(L, sib2(tree), passed, npassed, nb); */ - tree = sib2(tree); goto tailcall; - case TChoice: case TRecov: /* must check both children */ /* labeled failure */ - nb = verifyrule(L, sib1(tree), passed, npassed, nb); - /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ - tree = sib2(tree); goto tailcall; - case TRule: - if (npassed >= MAXRULES) - return verifyerror(L, passed, npassed); - else { - passed[npassed++] = tree->key; - /* return verifyrule(L, sib1(tree), passed, npassed); */ - tree = sib1(tree); goto tailcall; - } - case TGrammar: - return nullable(tree); /* sub-grammar cannot be left recursive */ - default: assert(0); return 0; - } -} - - -static void verifygrammar (lua_State *L, TTree *grammar) { - int passed[MAXRULES]; - TTree *rule; - /* check left-recursive rules */ - for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { - if (rule->key == 0) continue; /* unused rule */ - verifyrule(L, sib1(rule), passed, 0, 0); - } - assert(rule->tag == TTrue); - /* check infinite loops inside rules */ - for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) { - if (rule->key == 0) continue; /* unused rule */ - if (checkloops(sib1(rule))) { - lua_rawgeti(L, -1, rule->key); /* get rule's key */ - luaL_error(L, "empty loop in rule '%s'", val2str(L, -1)); - } - } - assert(rule->tag == TTrue); -} - - -/* -** Give a name for the initial rule if it is not referenced -*/ -static void initialrulename (lua_State *L, TTree *grammar, int frule) { - if (sib1(grammar)->key == 0) { /* initial rule is not referenced? */ - int n = lua_rawlen(L, -1) + 1; /* index for name */ - lua_pushvalue(L, frule); /* rule's name */ - lua_rawseti(L, -2, n); /* ktable was on the top of the stack */ - sib1(grammar)->key = n; - } -} - - -static TTree *newgrammar (lua_State *L, int arg) { - int treesize; - int frule = lua_gettop(L) + 2; /* position of first rule's key */ - int n = collectrules(L, arg, &treesize); - TTree *g = newtree(L, treesize); - luaL_argcheck(L, n <= MAXRULES, arg, "grammar has too many rules"); - g->tag = TGrammar; g->u.n = n; - lua_newtable(L); /* create 'ktable' */ - lua_setuservalue(L, -2); - buildgrammar(L, g, frule, n); - lua_getuservalue(L, -1); /* get 'ktable' for new tree */ - finalfix(L, frule - 1, g, sib1(g)); - initialrulename(L, g, frule); - verifygrammar(L, g); - lua_pop(L, 1); /* remove 'ktable' */ - lua_insert(L, -(n * 2 + 2)); /* move new table to proper position */ - lua_pop(L, n * 2 + 1); /* remove position table + rule pairs */ - return g; /* new table at the top of the stack */ -} - -/* }====================================================== */ - - -static Instruction *prepcompile (lua_State *L, Pattern *p, int idx) { - lua_getuservalue(L, idx); /* push 'ktable' (may be used by 'finalfix') */ - finalfix(L, 0, NULL, p->tree); - lua_pop(L, 1); /* remove 'ktable' */ - return compile(L, p); -} - - -static int lp_printtree (lua_State *L) { - TTree *tree = getpatt(L, 1, NULL); - int c = lua_toboolean(L, 2); - if (c) { - lua_getuservalue(L, 1); /* push 'ktable' (may be used by 'finalfix') */ - finalfix(L, 0, NULL, tree); - lua_pop(L, 1); /* remove 'ktable' */ - } - printktable(L, 1); - printtree(tree, 0); - return 0; -} - - -static int lp_printcode (lua_State *L) { - Pattern *p = getpattern(L, 1); - printktable(L, 1); - if (p->code == NULL) /* not compiled yet? */ - prepcompile(L, p, 1); - printpatt(p->code, p->codesize); - return 0; -} - - -/* -** Get the initial position for the match, interpreting negative -** values from the end of the subject -*/ -static size_t initposition (lua_State *L, size_t len) { - lua_Integer ii = luaL_optinteger(L, 3, 1); - if (ii > 0) { /* positive index? */ - if ((size_t)ii <= len) /* inside the string? */ - return (size_t)ii - 1; /* return it (corrected to 0-base) */ - else return len; /* crop at the end */ - } - else { /* negative index */ - if ((size_t)(-ii) <= len) /* inside the string? */ - return len - ((size_t)(-ii)); /* return position from the end */ - else return 0; /* crop at the beginning */ - } -} - - -/* -** Main match function -*/ -static int lp_match (lua_State *L) { - Capture capture[INITCAPSIZE]; - const char *r; - size_t l; - Pattern *p = (getpatt(L, 1, NULL), getpattern(L, 1)); - Instruction *code = (p->code != NULL) ? p->code : prepcompile(L, p, 1); - const char *s = luaL_checklstring(L, SUBJIDX, &l); - size_t i = initposition(L, l); - int ptop = lua_gettop(L); - byte labelf; /* labeled failure */ - const char *sfail = NULL; /* labeled failure */ - lua_pushnil(L); /* initialize subscache */ - lua_pushlightuserdata(L, capture); /* initialize caplistidx */ - lua_getuservalue(L, 1); /* initialize penvidx */ - r = match(L, s, s + i, s + l, code, capture, ptop, &labelf, &sfail); /* labeled failure */ - if (r == NULL) { /* labeled failure begin */ - lua_pushnil(L); - lua_pushinteger(L, labelf); - lua_pushstring(L, sfail); /* Pushing the subject where the failure occurred */ - return 3; - } /* labeled failure end */ - return getcaptures(L, s, r, ptop); -} - - - -/* -** {====================================================== -** Library creation and functions not related to matching -** ======================================================= -*/ - -/* maximum limit for stack size */ -#define MAXLIM (INT_MAX / 100) - -static int lp_setmax (lua_State *L) { - lua_Integer lim = luaL_checkinteger(L, 1); - luaL_argcheck(L, 0 < lim && lim <= MAXLIM, 1, "out of range"); - lua_settop(L, 1); - lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); - return 0; -} - - -static int lp_version (lua_State *L) { - lua_pushstring(L, VERSION); - return 1; -} - - -static int lp_type (lua_State *L) { - if (testpattern(L, 1)) - lua_pushliteral(L, "pattern"); - else - lua_pushnil(L); - return 1; -} - - -int lp_gc (lua_State *L) { - Pattern *p = getpattern(L, 1); - realloccode(L, p, 0); /* delete code block */ - return 0; -} - - -static void createcat (lua_State *L, const char *catname, int (catf) (int)) { - TTree *t = newcharset(L); - int i; - for (i = 0; i <= UCHAR_MAX; i++) - if (catf(i)) setchar(treebuffer(t), i); - lua_setfield(L, -2, catname); -} - - -static int lp_locale (lua_State *L) { - if (lua_isnoneornil(L, 1)) { - lua_settop(L, 0); - lua_createtable(L, 0, 12); - } - else { - luaL_checktype(L, 1, LUA_TTABLE); - lua_settop(L, 1); - } - createcat(L, "alnum", isalnum); - createcat(L, "alpha", isalpha); - createcat(L, "cntrl", iscntrl); - createcat(L, "digit", isdigit); - createcat(L, "graph", isgraph); - createcat(L, "lower", islower); - createcat(L, "print", isprint); - createcat(L, "punct", ispunct); - createcat(L, "space", isspace); - createcat(L, "upper", isupper); - createcat(L, "xdigit", isxdigit); - return 1; -} - - -static struct luaL_Reg pattreg[] = { - {"ptree", lp_printtree}, - {"pcode", lp_printcode}, - {"match", lp_match}, - {"B", lp_behind}, - {"V", lp_V}, - {"C", lp_simplecapture}, - {"Cc", lp_constcapture}, - {"Cmt", lp_matchtime}, - {"Cb", lp_backref}, - {"Carg", lp_argcapture}, - {"Cp", lp_poscapture}, - {"Cs", lp_substcapture}, - {"Ct", lp_tablecapture}, - {"Cf", lp_foldcapture}, - {"Cg", lp_groupcapture}, - {"P", lp_P}, - {"S", lp_set}, - {"R", lp_range}, - {"locale", lp_locale}, - {"version", lp_version}, - {"setmaxstack", lp_setmax}, - {"type", lp_type}, - {"T", lp_throw}, /* labeled failure throw */ - {"Rec", lp_recovery}, /* labeled failure choice */ - {NULL, NULL} -}; - - -static struct luaL_Reg metareg[] = { - {"__mul", lp_seq}, - {"__add", lp_choice}, - {"__pow", lp_star}, - {"__gc", lp_gc}, - {"__len", lp_and}, - {"__div", lp_divcapture}, - {"__unm", lp_not}, - {"__sub", lp_sub}, - {NULL, NULL} -}; - - -int luaopen_lpeglabel (lua_State *L); /* labeled failure */ -int luaopen_lpeglabel (lua_State *L) { /* labeled failure */ - luaL_newmetatable(L, PATTERN_T); - lua_pushnumber(L, MAXBACK); /* initialize maximum backtracking */ - lua_setfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); - luaL_setfuncs(L, metareg, 0); - luaL_newlib(L, pattreg); - lua_pushvalue(L, -1); - lua_setfield(L, -3, "__index"); - return 1; -} - -/* }====================================================== */ diff --git a/06/deps/lpeglabel/lptree.h b/06/deps/lpeglabel/lptree.h deleted file mode 100644 index b75f323..0000000 --- a/06/deps/lpeglabel/lptree.h +++ /dev/null @@ -1,82 +0,0 @@ -/* -** $Id: lptree.h,v 1.2 2013/03/24 13:51:12 roberto Exp $ -*/ - -#if !defined(lptree_h) -#define lptree_h - - -#include "lptypes.h" - - -/* -** types of trees -*/ -typedef enum TTag { - TChar = 0, TSet, TAny, /* standard PEG elements */ - TTrue, TFalse, - TRep, - TSeq, TChoice, - TNot, TAnd, - TCall, - TOpenCall, - TRule, /* sib1 is rule's pattern, sib2 is 'next' rule */ - TGrammar, /* sib1 is initial (and first) rule */ - TBehind, /* match behind */ - TCapture, /* regular capture */ - TRunTime, /* run-time capture */ - TThrow, TRecov /* labeled failure */ -} TTag; - -/* number of siblings for each tree */ -extern const byte numsiblings[]; - - -/* -** Tree trees -** The first sibling of a tree (if there is one) is immediately after -** the tree. A reference to a second sibling (ps) is its position -** relative to the position of the tree itself. A key in ktable -** uses the (unique) address of the original tree that created that -** entry. NULL means no data. -*/ -typedef struct TTree { - byte tag; - byte cap; /* kind of capture (if it is a capture) */ - unsigned short key; /* key in ktable for Lua data (0 if no key) */ - union { - int n; /* occasional counter */ - int label; /* labeled failure */ - struct { /* labeled failure */ - int ps; /* occasional second sibling */ - int plab; /* occasional label set */ - } s; /* labeled failure */ - } u; -} TTree; - - -/* -** A complete pattern has its tree plus, if already compiled, -** its corresponding code -*/ -typedef struct Pattern { - union Instruction *code; - int codesize; - TTree tree[1]; -} Pattern; - - -/* number of siblings for each tree */ -extern const byte numsiblings[]; - -/* access to siblings */ -#define sib1(t) ((t) + 1) -#define sib2(t) ((t) + (t)->u.s.ps) - - - - - - -#endif - diff --git a/06/deps/lpeglabel/lptypes.h b/06/deps/lpeglabel/lptypes.h deleted file mode 100644 index 81f0fdc..0000000 --- a/06/deps/lpeglabel/lptypes.h +++ /dev/null @@ -1,171 +0,0 @@ -/* -** $Id: lptypes.h,v 1.14 2015/09/28 17:17:41 roberto Exp $ -** LPeg - PEG pattern matching for Lua -** Copyright 2007-2015, Lua.org & PUC-Rio (see 'lpeg.html' for license) -** written by Roberto Ierusalimschy -*/ - -#if !defined(lptypes_h) -#define lptypes_h - - -#if !defined(LPEG_DEBUG) -#define NDEBUG -#endif - -#include -#include - -#include "lua.h" - - -#define VERSION "1.0.0" - - -#define PATTERN_T "lpeg-pattern" -#define MAXSTACKIDX "lpeg-maxstack" - - -/* -** compatibility with Lua 5.1 -*/ -#if (LUA_VERSION_NUM == 501) - -#define lp_equal lua_equal - -#define lua_getuservalue lua_getfenv -#define lua_setuservalue lua_setfenv - -#define lua_rawlen lua_objlen - -#define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) -#define luaL_newlib(L,f) luaL_register(L,"lpeg",f) - -#endif - - -#if !defined(lp_equal) -#define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) -#endif - - -/* default maximum size for call/backtrack stack */ -#if !defined(MAXBACK) -#define MAXBACK 400 -#endif - - -/* maximum number of rules in a grammar */ -#if !defined(MAXRULES) -#define MAXRULES 1000 -#endif - - - -/* initial size for capture's list */ -#define INITCAPSIZE 32 - - -/* index, on Lua stack, for subject */ -#define SUBJIDX 2 - -/* number of fixed arguments to 'match' (before capture arguments) */ -#define FIXEDARGS 3 - -/* index, on Lua stack, for capture list */ -#define caplistidx(ptop) ((ptop) + 2) - -/* index, on Lua stack, for pattern's ktable */ -#define ktableidx(ptop) ((ptop) + 3) - -/* index, on Lua stack, for backtracking stack */ -#define stackidx(ptop) ((ptop) + 4) - - - -typedef unsigned char byte; - - -#define BITSPERCHAR 8 - -#define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1) - - - -typedef struct Charset { - byte cs[CHARSETSIZE]; -} Charset; - - - -#define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} } - -/* access to charset */ -#define treebuffer(t) ((byte *)((t) + 1)) - -/* number of slots needed for 'n' bytes */ -#define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1) - -/* set 'b' bit in charset 'cs' */ -#define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7))) - - -/* -** in capture instructions, 'kind' of capture and its offset are -** packed in field 'aux', 4 bits for each -*/ -#define getkind(op) ((op)->i.aux & 0xF) -#define getoff(op) (((op)->i.aux >> 4) & 0xF) -#define joinkindoff(k,o) ((k) | ((o) << 4)) - -#define MAXOFF 0xF -#define MAXAUX 0xFF - - -/* maximum number of bytes to look behind */ -#define MAXBEHIND MAXAUX - - -/* maximum size (in elements) for a pattern */ -#define MAXPATTSIZE (SHRT_MAX - 10) - - -/* size (in elements) for an instruction plus extra l bytes */ -#define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1) - - -/* size (in elements) for a ISet instruction */ -#define CHARSETINSTSIZE instsize(CHARSETSIZE) - -/* size (in elements) for a IFunc instruction */ -#define funcinstsize(p) ((p)->i.aux + 2) - - - -#define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7)))) - -/* labeled failure begin */ -#define MAXLABELS (UCHAR_MAX + 1) - -#define LABELSETSIZE CHARSETSIZE - -typedef Charset Labelset; - -#define setlabel setchar - -#define testlabel testchar - -/* access to labelset */ -#define treelabelset(t) ((byte *)((t) + (t)->u.s.plab)) - -#define IDXLFAIL 0 - -#define LFAIL 0 - -/* update the farthest failure */ -#define updatefarthest(s1,s2) { if ((s2) > (s1)) s1 = s2; } - -/* labeled failure end */ - -#endif - diff --git a/06/deps/lpeglabel/lpvm.c b/06/deps/lpeglabel/lpvm.c deleted file mode 100644 index c256083..0000000 --- a/06/deps/lpeglabel/lpvm.c +++ /dev/null @@ -1,429 +0,0 @@ -/* -** $Id: lpvm.c,v 1.6 2015/09/28 17:01:25 roberto Exp $ -** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) -*/ - -#include -#include - - -#include "lua.h" -#include "lauxlib.h" - -#include "lpcap.h" -#include "lptypes.h" -#include "lpvm.h" -#include "lpprint.h" - - -/* initial size for call/backtrack stack */ -#if !defined(INITBACK) -#define INITBACK MAXBACK -#endif - - -#define getoffset(p) (((p) + 1)->offset) - -static const Instruction giveup = {{IGiveup, 0, 0}}; - -/* labeled failure */ -static void setlabelfail(Labelset *ls) { - loopset(i, ls->cs[i] = 0); - ls->cs[IDXLFAIL] = 1; -} -/* labeled failure end */ - -/* -** {====================================================== -** Virtual Machine -** ======================================================= -*/ - - -typedef struct Stack { - const char *s; /* saved position (or NULL for calls) */ - const Instruction *p; /* next instruction */ - const Labelset *ls; /* labeled failure */ - int caplevel; -} Stack; - - -#define getstackbase(L, ptop) ((Stack *)lua_touserdata(L, stackidx(ptop))) - - -/* -** Double the size of the array of captures -*/ -static Capture *doublecap (lua_State *L, Capture *cap, int captop, int ptop) { - Capture *newc; - if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) - luaL_error(L, "too many captures"); - newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); - memcpy(newc, cap, captop * sizeof(Capture)); - lua_replace(L, caplistidx(ptop)); - return newc; -} - - -/* -** Double the size of the stack -*/ -static Stack *doublestack (lua_State *L, Stack **stacklimit, int ptop) { - Stack *stack = getstackbase(L, ptop); - Stack *newstack; - int n = *stacklimit - stack; /* current stack size */ - int max, newn; - lua_getfield(L, LUA_REGISTRYINDEX, MAXSTACKIDX); - max = lua_tointeger(L, -1); /* maximum allowed size */ - lua_pop(L, 1); - if (n >= max) /* already at maximum size? */ - luaL_error(L, "backtrack stack overflow (current limit is %d)", max); - newn = 2 * n; /* new size */ - if (newn > max) newn = max; - newstack = (Stack *)lua_newuserdata(L, newn * sizeof(Stack)); - memcpy(newstack, stack, n * sizeof(Stack)); - lua_replace(L, stackidx(ptop)); - *stacklimit = newstack + newn; - return newstack + n; /* return next position */ -} - - -/* -** Interpret the result of a dynamic capture: false -> fail; -** true -> keep current position; number -> next position. -** Return new subject position. 'fr' is stack index where -** is the result; 'curr' is current subject position; 'limit' -** is subject's size. -*/ -static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { - lua_Integer res; - if (!lua_toboolean(L, fr)) { /* false value? */ - lua_settop(L, fr - 1); /* remove results */ - return -1; /* and fail */ - } - else if (lua_isboolean(L, fr)) /* true? */ - res = curr; /* keep current position */ - else { - res = lua_tointeger(L, fr) - 1; /* new position */ - if (res < curr || res > limit) - luaL_error(L, "invalid position returned by match-time capture"); - } - lua_remove(L, fr); /* remove first result (offset) */ - return res; -} - - -/* -** Add capture values returned by a dynamic capture to the capture list -** 'base', nested inside a group capture. 'fd' indexes the first capture -** value, 'n' is the number of values (at least 1). -*/ -static void adddyncaptures (const char *s, Capture *base, int n, int fd) { - int i; - /* Cgroup capture is already there */ - assert(base[0].kind == Cgroup && base[0].siz == 0); - base[0].idx = 0; /* make it an anonymous group */ - for (i = 1; i <= n; i++) { /* add runtime captures */ - base[i].kind = Cruntime; - base[i].siz = 1; /* mark it as closed */ - base[i].idx = fd + i - 1; /* stack index of capture value */ - base[i].s = s; - } - base[i].kind = Cclose; /* close group */ - base[i].siz = 1; - base[i].s = s; -} - - -/* -** Remove dynamic captures from the Lua stack (called in case of failure) -*/ -static int removedyncap (lua_State *L, Capture *capture, - int level, int last) { - int id = finddyncap(capture + level, capture + last); /* index of 1st cap. */ - int top = lua_gettop(L); - if (id == 0) return 0; /* no dynamic captures? */ - lua_settop(L, id - 1); /* remove captures */ - return top - id + 1; /* number of values removed */ -} - - - - -/* -** Opcode interpreter -*/ -const char *match (lua_State *L, const char *o, const char *s, const char *e, - Instruction *op, Capture *capture, int ptop, byte *labelf, const char **sfail) { /* labeled failure */ - Stack stackbase[INITBACK]; - Stack *stacklimit = stackbase + INITBACK; - Stack *stack = stackbase; /* point to first empty slot in stack */ - int capsize = INITCAPSIZE; - int captop = 0; /* point to first empty slot in captures */ - int ndyncap = 0; /* number of dynamic captures (in Lua stack) */ - const Instruction *p = op; /* current instruction */ - const Instruction *pk = NULL; /* resume instruction */ - Labelset lsfail; - setlabelfail(&lsfail); - stack->p = &giveup; stack->s = s; stack->ls = &lsfail; stack->caplevel = 0; stack++; /* labeled failure */ - *sfail = s; /* labeled failure */ - lua_pushlightuserdata(L, stackbase); - for (;;) { -#if defined(DEBUG) - printinst(op, p); - printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", - s, stack - getstackbase(L, ptop), ndyncap, captop); - printcaplist(capture, capture + captop); -#endif - assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); - switch ((Opcode)p->i.code) { - case IEnd: { - assert(stack == getstackbase(L, ptop) + 1); - capture[captop].kind = Cclose; - capture[captop].s = NULL; - return s; - } - case IGiveup: { - assert(stack == getstackbase(L, ptop)); - return NULL; - } - case IRet: { - assert(stack > getstackbase(L, ptop) && (stack - 1)->s == NULL); - p = (--stack)->p; - continue; - } - case IAny: { - if (s < e) { p++; s++; } - else { - *labelf = LFAIL; /* labeled failure */ - pk = p + 1; - updatefarthest(*sfail, s); /*labeled failure */ - goto fail; - } - continue; - } - case ITestAny: { - if (s < e) p += 2; - else p += getoffset(p); - continue; - } - case IChar: { - if ((byte)*s == p->i.aux && s < e) { p++; s++; } - else { - *labelf = LFAIL; /* labeled failure */ - pk = p + 1; - updatefarthest(*sfail, s); /*labeled failure */ - goto fail; - } - continue; - } - case ITestChar: { - if ((byte)*s == p->i.aux && s < e) p += 2; - else p += getoffset(p); - continue; - } - case ISet: { - int c = (byte)*s; - if (testchar((p+1)->buff, c) && s < e) - { p += CHARSETINSTSIZE; s++; } - else { - *labelf = LFAIL; /* labeled failure */ - pk = p + CHARSETINSTSIZE; - updatefarthest(*sfail, s); /*labeled failure */ - goto fail; - } - continue; - } - case ITestSet: { - int c = (byte)*s; - if (testchar((p + 2)->buff, c) && s < e) - p += 1 + CHARSETINSTSIZE; - else p += getoffset(p); - continue; - } - case IBehind: { - int n = p->i.aux; - if (n > s - o) { - *labelf = LFAIL; /* labeled failure */ - pk = p + 1; - updatefarthest(*sfail, s); /*labeled failure */ - goto fail; - } - s -= n; p++; - continue; - } - case ISpan: { - for (; s < e; s++) { - int c = (byte)*s; - if (!testchar((p+1)->buff, c)) break; - } - p += CHARSETINSTSIZE; - continue; - } - case IJmp: { - p += getoffset(p); - continue; - } - case IChoice: { - if (stack == stacklimit) - stack = doublestack(L, &stacklimit, ptop); - stack->p = p + getoffset(p); - stack->s = s; - stack->ls = &lsfail; /* labeled failure */ - stack->caplevel = captop; - stack++; - p += 2; - continue; - } - case IRecov: { /* labeled failure */ - if (stack == stacklimit) - stack = doublestack(L, &stacklimit, ptop); - stack->p = p + getoffset(p); - stack->s = NULL; - stack->ls = (const Labelset *) ((p + 2)->buff); - stack->caplevel = captop; - stack++; - p += (CHARSETINSTSIZE - 1) + 2; - continue; - } - case ICall: { - if (stack == stacklimit) - stack = doublestack(L, &stacklimit, ptop); - stack->s = NULL; - stack->p = p + 2; /* save return address */ - stack->ls = NULL; - stack++; - p += getoffset(p); - continue; - } - case ICommit: { - assert(stack > getstackbase(L, ptop) && (stack - 1)->ls != NULL); /* labeled failure */ - /*assert((stack - 1)->s != NULL); labeled failure: IRecov does not push s onto the stack */ - stack--; - p += getoffset(p); - continue; - } - case IPartialCommit: { - assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); - (stack - 1)->s = s; - (stack - 1)->caplevel = captop; - p += getoffset(p); - continue; - } - case IBackCommit: { - assert(stack > getstackbase(L, ptop) && (stack - 1)->s != NULL); - s = (--stack)->s; - captop = stack->caplevel; - p += getoffset(p); - continue; - } - case IThrow: { /* labeled failure */ - *labelf = p->i.aux; - pk = p + 1; - *sfail = s; - goto fail; - } - case IFailTwice: - assert(stack > getstackbase(L, ptop)); - stack--; - /* go through */ - case IFail: - *labelf = LFAIL; /* labeled failure */ - pk = NULL; - updatefarthest(*sfail, s); /*labeled failure */ - fail: { /* pattern failed: try to backtrack */ - const Labelset *auxlab = NULL; - Stack *pstack = stack; - do { /* remove pending calls */ - assert(pstack > getstackbase(L, ptop)); - auxlab = (--pstack)->ls; - } while (auxlab == NULL || !(pstack->p == &giveup || testlabel(pstack->ls->cs, *labelf))); - if (pstack->s != NULL) { /* labeled failure: giveup or backtrack frame */ - stack = pstack; - s = stack->s; - if (ndyncap > 0) /* is there matchtime captures? */ - ndyncap -= removedyncap(L, capture, stack->caplevel, captop); - captop = stack->caplevel; - } else { /* labeled failure: recovery frame */ - if (stack == stacklimit) - stack = doublestack(L, &stacklimit, ptop); - stack->s = NULL; - stack->p = pk; /* save return address */ - stack->ls = NULL; - stack->caplevel = captop; /* TODO: really necessary?? */ - stack++; - } - p = pstack->p; - continue; - } - case ICloseRunTime: { - CapState cs; - int rem, res, n; - int fr = lua_gettop(L) + 1; /* stack index of first result */ - cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; - n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ - captop -= n; /* remove nested captures */ - fr -= rem; /* 'rem' items were popped from Lua stack */ - res = resdyncaptures(L, fr, s - o, e - o); /* get result */ - if (res == -1) { /* fail? */ - *labelf = LFAIL; /* labeled failure */ - pk = NULL; - updatefarthest(*sfail, s); /*labeled failure */ - goto fail; - } - s = o + res; /* else update current position */ - n = lua_gettop(L) - fr + 1; /* number of new captures */ - ndyncap += n - rem; /* update number of dynamic captures */ - if (n > 0) { /* any new capture? */ - if ((captop += n + 2) >= capsize) { - capture = doublecap(L, capture, captop, ptop); - capsize = 2 * captop; - } - /* add new captures to 'capture' list */ - adddyncaptures(s, capture + captop - n - 2, n, fr); - } - p++; - continue; - } - case ICloseCapture: { - const char *s1 = s; - assert(captop > 0); - /* if possible, turn capture into a full capture */ - if (capture[captop - 1].siz == 0 && - s1 - capture[captop - 1].s < UCHAR_MAX) { - capture[captop - 1].siz = s1 - capture[captop - 1].s + 1; - p++; - continue; - } - else { - capture[captop].siz = 1; /* mark entry as closed */ - capture[captop].s = s; - goto pushcapture; - } - } - case IOpenCapture: - capture[captop].siz = 0; /* mark entry as open */ - capture[captop].s = s; - goto pushcapture; - case IFullCapture: - capture[captop].siz = getoff(p) + 1; /* save capture size */ - capture[captop].s = s - getoff(p); - /* goto pushcapture; */ - pushcapture: { - capture[captop].idx = p->i.key; - capture[captop].kind = getkind(p); - if (++captop >= capsize) { - capture = doublecap(L, capture, captop, ptop); - capsize = 2 * captop; - } - p++; - continue; - } - default: assert(0); return NULL; - } - } -} - -/* }====================================================== */ - - diff --git a/06/deps/lpeglabel/lpvm.h b/06/deps/lpeglabel/lpvm.h deleted file mode 100644 index bcfc22f..0000000 --- a/06/deps/lpeglabel/lpvm.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -** $Id: lpvm.h,v 1.3 2014/02/21 13:06:41 roberto Exp $ -*/ - -#if !defined(lpvm_h) -#define lpvm_h - -#include "lpcap.h" - - -/* Virtual Machine's instructions */ -typedef enum Opcode { - IAny, /* if no char, fail */ - IChar, /* if char != aux, fail */ - ISet, /* if char not in buff, fail */ - ITestAny, /* in no char, jump to 'offset' */ - ITestChar, /* if char != aux, jump to 'offset' */ - ITestSet, /* if char not in buff, jump to 'offset' */ - ISpan, /* read a span of chars in buff */ - IBehind, /* walk back 'aux' characters (fail if not possible) */ - IRet, /* return from a rule */ - IEnd, /* end of pattern */ - IChoice, /* stack a choice; next fail will jump to 'offset' */ - IJmp, /* jump to 'offset' */ - ICall, /* call rule at 'offset' */ - IOpenCall, /* call rule number 'key' (must be closed to a ICall) */ - ICommit, /* pop choice and jump to 'offset' */ - IPartialCommit, /* update top choice to current position and jump */ - IBackCommit, /* "fails" but jump to its own 'offset' */ - IFailTwice, /* pop one choice and then fail */ - IFail, /* go back to saved state on choice and jump to saved offset */ - IGiveup, /* internal use */ - IFullCapture, /* complete capture of last 'off' chars */ - IOpenCapture, /* start a capture */ - ICloseCapture, - ICloseRunTime, - IThrow, /* "fails" with a specific label labeled failure */ - IRecov /* stack a recovery; next fail with label 'f' will jump to 'offset' */ -} Opcode; - - - -typedef union Instruction { - struct Inst { - byte code; - byte aux; - short key; - } i; - int offset; - byte buff[1]; -} Instruction; - - -void printpatt (Instruction *p, int n); -const char *match (lua_State *L, const char *o, const char *s, const char *e, - Instruction *op, Capture *capture, int ptop, byte *labelf, const char **sfail); /* labeled failure */ - - -#endif - diff --git a/06/deps/lpeglabel/makefile b/06/deps/lpeglabel/makefile deleted file mode 100644 index d0c67c9..0000000 --- a/06/deps/lpeglabel/makefile +++ /dev/null @@ -1,57 +0,0 @@ -LIBNAME = lpeglabel -LUADIR = ../lua-bootstrap/include/ - -COPT = -O2 -# COPT = -DLPEG_DEBUG -g - -CWARNS = -Wall -Wextra -pedantic \ - -Waggregate-return \ - -Wcast-align \ - -Wcast-qual \ - -Wdisabled-optimization \ - -Wpointer-arith \ - -Wshadow \ - -Wsign-compare \ - -Wundef \ - -Wwrite-strings \ - -Wbad-function-cast \ - -Wdeclaration-after-statement \ - -Wmissing-prototypes \ - -Wnested-externs \ - -Wstrict-prototypes \ -# -Wunreachable-code \ - - -CFLAGS = $(CWARNS) -nostdinc -B ../../../05/tcc-bootstrap -I ../../../05/musl-bootstrap/include -I$(LUADIR) -CC = ../../../05/tcc-0.9.27/tcc - -FILES = lpvm.o lpcap.o lptree.o lpcode.o lpprint.o - -# For Linux -linux: - make lpeglabel.a - -# # For Mac OS -# macosx: -# make lpeglabel.so "DLLFLAGS = -bundle -undefined dynamic_lookup" - -lpeglabel.a: $(FILES) - $(CC) -ar c lpeglabel.a $(FILES) - -$(FILES): makefile - -test: test.lua testlabel.lua testerrors.lua relabel.lua lpeglabel.so - lua test.lua - lua testlabel.lua - lua testerrors.lua - -clean: - rm -f $(FILES) lpeglabel.so - - -lpcap.o: lpcap.c lpcap.h lptypes.h -lpcode.o: lpcode.c lptypes.h lpcode.h lptree.h lpvm.h lpcap.h -lpprint.o: lpprint.c lptypes.h lpprint.h lptree.h lpvm.h lpcap.h -lptree.o: lptree.c lptypes.h lpcap.h lpcode.h lptree.h lpvm.h lpprint.h -lpvm.o: lpvm.c lpcap.h lptypes.h lpvm.h lpprint.h lptree.h - diff --git a/06/deps/lpeglabel/relabel.lua b/06/deps/lpeglabel/relabel.lua deleted file mode 100644 index 33a332c..0000000 --- a/06/deps/lpeglabel/relabel.lua +++ /dev/null @@ -1,399 +0,0 @@ --- $Id: re.lua,v 1.44 2013/03/26 20:11:40 roberto Exp $ - --- imported functions and modules -local tonumber, type, print, error, ipairs = tonumber, type, print, error, ipairs -local pcall = pcall -local setmetatable = setmetatable -local unpack, tinsert, concat = table.unpack, table.insert, table.concat -local rep = string.rep --- m is defined in main.c and is equivalent to require ('lpeglabel' -local m = m - --- 'm' will be used to parse expressions, and 'mm' will be used to --- create expressions; that is, 're' runs on 'm', creating patterns --- on 'mm' -local mm = m - --- pattern's metatable -local mt = getmetatable(mm.P(0)) - - - --- No more global accesses after this point -local version = _VERSION -if version == "Lua 5.2" then _ENV = nil end - - -local any = m.P(1) -local dummy = mm.P(false) - - -local errinfo = { - {"NoPatt", "no pattern found"}, - {"ExtraChars", "unexpected characters after the pattern"}, - - {"ExpPatt1", "expected a pattern after '/' or '//{...}'"}, - - {"ExpPatt2", "expected a pattern after '&'"}, - {"ExpPatt3", "expected a pattern after '!'"}, - - {"ExpPatt4", "expected a pattern after '('"}, - {"ExpPatt5", "expected a pattern after ':'"}, - {"ExpPatt6", "expected a pattern after '{~'"}, - {"ExpPatt7", "expected a pattern after '{|'"}, - - {"ExpPatt8", "expected a pattern after '<-'"}, - - {"ExpPattOrClose", "expected a pattern or closing '}' after '{'"}, - - {"ExpNum", "expected a number after '^', '+' or '-' (no space)"}, - {"ExpCap", "expected a string, number, '{}' or name after '->'"}, - - {"ExpName1", "expected the name of a rule after '=>'"}, - {"ExpName2", "expected the name of a rule after '=' (no space)"}, - {"ExpName3", "expected the name of a rule after '<' (no space)"}, - - {"ExpLab1", "expected at least one label after '{'"}, - {"ExpLab2", "expected a label after the comma"}, - - {"ExpNameOrLab", "expected a name or label after '%' (no space)"}, - - {"ExpItem", "expected at least one item after '[' or '^'"}, - - {"MisClose1", "missing closing ')'"}, - {"MisClose2", "missing closing ':}'"}, - {"MisClose3", "missing closing '~}'"}, - {"MisClose4", "missing closing '|}'"}, - {"MisClose5", "missing closing '}'"}, -- for the captures - - {"MisClose6", "missing closing '>'"}, - {"MisClose7", "missing closing '}'"}, -- for the labels - - {"MisClose8", "missing closing ']'"}, - - {"MisTerm1", "missing terminating single quote"}, - {"MisTerm2", "missing terminating double quote"}, -} - -local errmsgs = {} -local labels = {} - -for i, err in ipairs(errinfo) do - errmsgs[i] = err[2] - labels[err[1]] = i -end - -local function expect (pattern, labelname) - local label = labels[labelname] - return pattern + m.T(label) -end - - --- Pre-defined names -local Predef = { nl = m.P"\n" } -local tlabels = {} - - -local mem -local fmem -local gmem - - -local function updatelocale () - mm.locale(Predef) - Predef.a = Predef.alpha - Predef.c = Predef.cntrl - Predef.d = Predef.digit - Predef.g = Predef.graph - Predef.l = Predef.lower - Predef.p = Predef.punct - Predef.s = Predef.space - Predef.u = Predef.upper - Predef.w = Predef.alnum - Predef.x = Predef.xdigit - Predef.A = any - Predef.a - Predef.C = any - Predef.c - Predef.D = any - Predef.d - Predef.G = any - Predef.g - Predef.L = any - Predef.l - Predef.P = any - Predef.p - Predef.S = any - Predef.s - Predef.U = any - Predef.u - Predef.W = any - Predef.w - Predef.X = any - Predef.x - mem = {} -- restart memoization - fmem = {} - gmem = {} - local mt = {__mode = "v"} - setmetatable(mem, mt) - setmetatable(fmem, mt) - setmetatable(gmem, mt) -end - - -updatelocale() - - - -local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end) - - -local function getdef (id, defs) - local c = defs and defs[id] - if not c then - error("undefined name: " .. id) - end - return c -end - - -local function mult (p, n) - local np = mm.P(true) - while n >= 1 do - if n%2 >= 1 then np = np * p end - p = p * p - n = n/2 - end - return np -end - -local function equalcap (s, i, c) - if type(c) ~= "string" then return nil end - local e = #c + i - if s:sub(i, e - 1) == c then return e else return nil end -end - - -local S = (Predef.space + "--" * (any - Predef.nl)^0)^0 - -local name = m.C(m.R("AZ", "az", "__") * m.R("AZ", "az", "__", "09")^0) - -local arrow = S * "<-" - --- a defined name only have meaning in a given environment -local Def = name * m.Carg(1) - -local num = m.C(m.R"09"^1) * S / tonumber - -local String = "'" * m.C((any - "'" - m.P"\n")^0) * expect("'", "MisTerm1") - + '"' * m.C((any - '"' - m.P"\n")^0) * expect('"', "MisTerm2") - - -local defined = "%" * Def / function (c,Defs) - local cat = Defs and Defs[c] or Predef[c] - if not cat then - error("name '" .. c .. "' undefined") - end - return cat -end - -local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R - -local item = defined + Range + m.C(any - m.P"\n") - -local Class = - "[" - * (m.C(m.P"^"^-1)) -- optional complement symbol - * m.Cf(expect(item, "ExpItem") * (item - "]")^0, mt.__add) - / function (c, p) return c == "^" and any - p or p end - * expect("]", "MisClose8") - -local function adddef (t, k, exp) - if t[k] then - error("'"..k.."' already defined as a rule") - else - t[k] = exp - end - return t -end - -local function firstdef (n, r) return adddef({n}, n, r) end - - -local function NT (n, b) - if not b then - error("rule '"..n.."' used outside a grammar") - else return mm.V(n) - end -end - -local function choicerec (...) - local t = { ... } - local n = #t - local p = t[1] - local i = 2 - while i + 1 <= n do - -- t[i] == nil when there are no labels - p = t[i] and mm.Rec(p, t[i+1], unpack(t[i])) or mt.__add(p, t[i+1]) - i = i + 2 - end - - return p -end - -local exp = m.P{ "Exp", - Exp = S * ( m.V"Grammar" - + (m.V"Seq" * (S * (("//" * m.Ct(m.V"Labels")) + ("/" * m.Cc(nil))) - * expect(S * m.V"Seq", "ExpPatt1") - )^0 - ) / choicerec); - Labels = m.P"{" * expect(S * m.V"Label", "ExpLab1") - * (S * "," * expect(S * m.V"Label", "ExpLab2"))^0 - * expect(S * "}", "MisClose7"); - Seq = m.Cf(m.Cc(m.P"") * m.V"Prefix" * (S * m.V"Prefix")^0, mt.__mul); - Prefix = "&" * expect(S * m.V"Prefix", "ExpPatt2") / mt.__len - + "!" * expect(S * m.V"Prefix", "ExpPatt3") / mt.__unm - + m.V"Suffix"; - Suffix = m.Cf(m.V"Primary" * - ( S * ( m.P"+" * m.Cc(1, mt.__pow) - + m.P"*" * m.Cc(0, mt.__pow) - + m.P"?" * m.Cc(-1, mt.__pow) - + "^" * expect( m.Cg(num * m.Cc(mult)) - + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow) - ), - "ExpNum") - + "->" * expect(S * ( m.Cg((String + num) * m.Cc(mt.__div)) - + m.P"{}" * m.Cc(nil, m.Ct) - + m.Cg(Def / getdef * m.Cc(mt.__div)) - ), - "ExpCap") - + "=>" * expect(S * m.Cg(Def / getdef * m.Cc(m.Cmt)), - "ExpName1") - ) - )^0, function (a,b,f) return f(a,b) end ); - Primary = "(" * expect(m.V"Exp", "ExpPatt4") * expect(S * ")", "MisClose1") - + String / mm.P - + Class - + defined - + "%" * expect(m.P"{", "ExpNameOrLab") - * expect(S * m.V"Label", "ExpLab1") - * expect(S * "}", "MisClose7") / mm.T - + "{:" * (name * ":" + m.Cc(nil)) * expect(m.V"Exp", "ExpPatt5") - * expect(S * ":}", "MisClose2") - / function (n, p) return mm.Cg(p, n) end - + "=" * expect(name, "ExpName2") - / function (n) return mm.Cmt(mm.Cb(n), equalcap) end - + m.P"{}" / mm.Cp - + "{~" * expect(m.V"Exp", "ExpPatt6") - * expect(S * "~}", "MisClose3") / mm.Cs - + "{|" * expect(m.V"Exp", "ExpPatt7") - * expect(S * "|}", "MisClose4") / mm.Ct - + "{" * expect(m.V"Exp", "ExpPattOrClose") - * expect(S * "}", "MisClose5") / mm.C - + m.P"." * m.Cc(any) - + (name * -arrow + "<" * expect(name, "ExpName3") - * expect(">", "MisClose6")) * m.Cb("G") / NT; - Label = num + name / function (f) return tlabels[f] end; - Definition = name * arrow * expect(m.V"Exp", "ExpPatt8"); - Grammar = m.Cg(m.Cc(true), "G") - * m.Cf(m.V"Definition" / firstdef * (S * m.Cg(m.V"Definition"))^0, - adddef) / mm.P; -} - -local pattern = S * m.Cg(m.Cc(false), "G") * expect(exp, "NoPatt") / mm.P - * S * expect(-any, "ExtraChars") - -local function lineno (s, i) - if i == 1 then return 1, 1 end - local adjustment = 0 - -- report the current line if at end of line, not the next - if s:sub(i,i) == '\n' then - i = i-1 - adjustment = 1 - end - local rest, num = s:sub(1,i):gsub("[^\n]*\n", "") - local r = #rest - return 1 + num, (r ~= 0 and r or 1) + adjustment -end - -local function calcline (s, i) - if i == 1 then return 1, 1 end - local rest, line = s:sub(1,i):gsub("[^\n]*\n", "") - local col = #rest - return 1 + line, col ~= 0 and col or 1 -end - - -local function splitlines(str) - local t = {} - local function helper(line) tinsert(t, line) return "" end - helper((str:gsub("(.-)\r?\n", helper))) - return t -end - -local function compile (p, defs) - if mm.type(p) == "pattern" then return p end -- already compiled - p = p .. " " -- for better reporting of column numbers in errors when at EOF - local ok, cp, label, suffix = pcall(function() return pattern:match(p, 1, defs) end) - if not ok and cp then - if type(cp) == "string" then - cp = cp:gsub("^[^:]+:[^:]+: ", "") - end - error(cp, 3) - end - if not cp then - local lines = splitlines(p) - local line, col = lineno(p, #p - #suffix + 1) - --local line, col = calcline(p, #p - #suffix + 1) - local err = {} - tinsert(err, "L" .. line .. ":C" .. col .. ": " .. errmsgs[label]) - tinsert(err, lines[line]) - tinsert(err, rep(" ", col-1) .. "^") - error("syntax error(s) in pattern\n" .. concat(err, "\n"), 3) - end - return cp -end - -local function match (s, p, i) - local cp = mem[p] - if not cp then - cp = compile(p) - mem[p] = cp - end - return cp:match(s, i or 1) -end - -local function find (s, p, i) - local cp = fmem[p] - if not cp then - cp = compile(p) / 0 - cp = mm.P{ mm.Cp() * cp * mm.Cp() + 1 * mm.V(1) } - fmem[p] = cp - end - local i, e = cp:match(s, i or 1) - if i then return i, e - 1 - else return i - end -end - -local function gsub (s, p, rep) - local g = gmem[p] or {} -- ensure gmem[p] is not collected while here - gmem[p] = g - local cp = g[rep] - if not cp then - cp = compile(p) - cp = mm.Cs((cp / rep + 1)^0) - g[rep] = cp - end - return cp:match(s) -end - -local function setlabels (t) - tlabels = t -end - - --- exported names -local re = { - compile = compile, - match = match, - find = find, - gsub = gsub, - updatelocale = updatelocale, - setlabels = setlabels, - calcline = calcline -} - -if version == "Lua 5.1" then _G.re = re end - -return re diff --git a/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec deleted file mode 100644 index 63500ff..0000000 --- a/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-1.rockspec +++ /dev/null @@ -1,32 +0,0 @@ -package = "LPegLabel" -version = "0.12.2-1" -source = { - url = "https://github.com/sqmedeiros/lpeglabel/archive/v0.12.2.tar.gz", - tag = "v0.12.2", - dir = "lpeglabel-0.12.2", -} -description = { - summary = "Parsing Expression Grammars For Lua with Labeled Failures", - detailed = [[ - LPegLabel is a conservative extension of the LPeg library that provides - an implementation of Parsing Expression Grammars (PEGs) with labeled failures. - Labels can be used to signal different kinds of erros and to specify which - alternative in a labeled ordered choice should handle a given label. - Labels can also be combined with the standard patterns of LPeg. - ]], - homepage = "https://github.com/sqmedeiros/lpeglabel/", - maintainer = "Sergio Medeiros ", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.2", -} -build = { - type = "builtin", - modules = { - lpeglabel = { - "lpcap.c", "lpcode.c", "lpprint.c", "lptree.c", "lpvm.c" - }, - relabel = "relabel.lua" - } -} diff --git a/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec deleted file mode 100644 index e370f7d..0000000 --- a/06/deps/lpeglabel/rockspecs/lpeglabel-0.12.2-2.rockspec +++ /dev/null @@ -1,32 +0,0 @@ -package = "LPegLabel" -version = "0.12.2-2" -source = { - url = "https://github.com/sqmedeiros/lpeglabel/archive/v0.12.2-2.tar.gz", - tag = "v0.12.2-2", - dir = "lpeglabel-0.12.2-2", -} -description = { - summary = "Parsing Expression Grammars For Lua with Labeled Failures", - detailed = [[ - LPegLabel is a conservative extension of the LPeg library that provides - an implementation of Parsing Expression Grammars (PEGs) with labeled failures. - Labels can be used to signal different kinds of erros and to specify which - alternative in a labeled ordered choice should handle a given label. - Labels can also be combined with the standard patterns of LPeg. - ]], - homepage = "https://github.com/sqmedeiros/lpeglabel/", - maintainer = "Sergio Medeiros ", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1", -} -build = { - type = "builtin", - modules = { - lpeglabel = { - "lpcap.c", "lpcode.c", "lpprint.c", "lptree.c", "lpvm.c" - }, - relabel = "relabel.lua" - } -} diff --git a/06/deps/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec deleted file mode 100644 index d19b833..0000000 --- a/06/deps/lpeglabel/rockspecs/lpeglabel-1.0.0-1.rockspec +++ /dev/null @@ -1,32 +0,0 @@ -package = "LPegLabel" -version = "1.0.0-1" -source = { - url = "https://github.com/sqmedeiros/lpeglabel/archive/v1.0.0-1.tar.gz", - tag = "v1.0.0-1", - dir = "lpeglabel-1.0.0-1", -} -description = { - summary = "Parsing Expression Grammars For Lua with Labeled Failures", - detailed = [[ - LPegLabel is a conservative extension of the LPeg library that provides - an implementation of Parsing Expression Grammars (PEGs) with labeled failures. - Labels can be used to signal different kinds of erros and to specify which - alternative in a labeled ordered choice should handle a given label. - Labels can also be combined with the standard patterns of LPeg. - ]], - homepage = "https://github.com/sqmedeiros/lpeglabel/", - maintainer = "Sergio Medeiros ", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1", -} -build = { - type = "builtin", - modules = { - lpeglabel = { - "lpcap.c", "lpcode.c", "lpprint.c", "lptree.c", "lpvm.c" - }, - relabel = "relabel.lua" - } -} diff --git a/06/deps/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec deleted file mode 100644 index d7bbc95..0000000 --- a/06/deps/lpeglabel/rockspecs/lpeglabel-1.1.0-1.rockspec +++ /dev/null @@ -1,32 +0,0 @@ -package = "LPegLabel" -version = "1.1.0-1" -source = { - url = "https://github.com/sqmedeiros/lpeglabel/archive/v1.1.0-1.tar.gz", - tag = "v1.1.0-1", - dir = "lpeglabel-1.1.0-1", -} -description = { - summary = "Parsing Expression Grammars For Lua with Labeled Failures", - detailed = [[ - LPegLabel is a conservative extension of the LPeg library that provides - an implementation of Parsing Expression Grammars (PEGs) with labeled failures. - Labels can be used to signal different kinds of erros and to specify which - which recovery pattern should handle a given label. - Labels can also be combined with the standard patterns of LPeg. - ]], - homepage = "https://github.com/sqmedeiros/lpeglabel/", - maintainer = "Sergio Medeiros ", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1", -} -build = { - type = "builtin", - modules = { - lpeglabel = { - "lpcap.c", "lpcode.c", "lpprint.c", "lptree.c", "lpvm.c" - }, - relabel = "relabel.lua" - } -} diff --git a/06/deps/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec b/06/deps/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec deleted file mode 100644 index 2fd17b9..0000000 --- a/06/deps/lpeglabel/rockspecs/lpeglabel-1.2.0-1.rockspec +++ /dev/null @@ -1,32 +0,0 @@ -package = "LPegLabel" -version = "1.2.0-1" -source = { - url = "https://github.com/sqmedeiros/lpeglabel/archive/v1.2.0-1.tar.gz", - tag = "v1.2.0-1", - dir = "lpeglabel-1.2.0-1", -} -description = { - summary = "Parsing Expression Grammars For Lua with Labeled Failures", - detailed = [[ - LPegLabel is a conservative extension of the LPeg library that provides - an implementation of Parsing Expression Grammars (PEGs) with labeled failures. - Labels can be used to signal different kinds of erros and to specify which recovery - pattern should handle a given label. - LPegLabel also reports the farthest failure position in case of an ordinary failure. - ]], - homepage = "https://github.com/sqmedeiros/lpeglabel/", - maintainer = "Sergio Medeiros ", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1", -} -build = { - type = "builtin", - modules = { - lpeglabel = { - "lpcap.c", "lpcode.c", "lpprint.c", "lptree.c", "lpvm.c" - }, - relabel = "relabel.lua" - } -} diff --git a/06/deps/lpeglabel/test.lua b/06/deps/lpeglabel/test.lua deleted file mode 100755 index b985126..0000000 --- a/06/deps/lpeglabel/test.lua +++ /dev/null @@ -1,1449 +0,0 @@ -#!/usr/bin/env lua - --- $Id: test.lua,v 1.109 2015/09/28 17:01:25 roberto Exp $ - --- require"strict" -- just to be pedantic - -local m = require"lpeglabel" - - --- for general use -local a, b, c, d, e, f, g, p, t - - --- compatibility with Lua 5.2 -local unpack = rawget(table, "unpack") -local loadstring = rawget(_G, "loadstring") or load - - -local any = m.P(1) -local space = m.S" \t\n"^0 - -local function checkeq (x, y, p) -if p then print(x,y) end - if type(x) ~= "table" then assert(x == y) - else - for k,v in pairs(x) do checkeq(v, y[k], p) end - for k,v in pairs(y) do checkeq(v, x[k], p) end - end -end - - -local mt = getmetatable(m.P(1)) - - -local allchar = {} -for i=0,255 do allchar[i + 1] = i end -allchar = string.char(unpack(allchar)) -assert(#allchar == 256) - -local function cs2str (c) - return m.match(m.Cs((c + m.P(1)/"")^0), allchar) -end - -local function eqcharset (c1, c2) - assert(cs2str(c1) == cs2str(c2)) -end - - -print"General tests for LPeg library" - -assert(type(m.version()) == "string") -print("version " .. m.version()) -assert(m.type("alo") ~= "pattern") -assert(m.type(io.input) ~= "pattern") -assert(m.type(m.P"alo") == "pattern") - --- tests for some basic optimizations -assert(m.match(m.P(false) + "a", "a") == 2) -assert(m.match(m.P(true) + "a", "a") == 1) -assert(m.match("a" + m.P(false), "b") == nil) -assert(m.match("a" + m.P(true), "b") == 1) - -assert(m.match(m.P(false) * "a", "a") == nil) -assert(m.match(m.P(true) * "a", "a") == 2) -assert(m.match("a" * m.P(false), "a") == nil) -assert(m.match("a" * m.P(true), "a") == 2) - -assert(m.match(#m.P(false) * "a", "a") == nil) -assert(m.match(#m.P(true) * "a", "a") == 2) -assert(m.match("a" * #m.P(false), "a") == nil) -assert(m.match("a" * #m.P(true), "a") == 2) - - --- tests for locale -do - assert(m.locale(m) == m) - local t = {} - assert(m.locale(t, m) == t) - local x = m.locale() - for n,v in pairs(x) do - assert(type(n) == "string") - eqcharset(v, m[n]) - end -end - - -assert(m.match(3, "aaaa")) -assert(m.match(4, "aaaa")) -assert(not m.match(5, "aaaa")) -assert(m.match(-3, "aa")) -assert(not m.match(-3, "aaa")) -assert(not m.match(-3, "aaaa")) -assert(not m.match(-4, "aaaa")) -assert(m.P(-5):match"aaaa") - -assert(m.match("a", "alo") == 2) -assert(m.match("al", "alo") == 3) -assert(not m.match("alu", "alo")) -assert(m.match(true, "") == 1) - -local digit = m.S"0123456789" -local upper = m.S"ABCDEFGHIJKLMNOPQRSTUVWXYZ" -local lower = m.S"abcdefghijklmnopqrstuvwxyz" -local letter = m.S"" + upper + lower -local alpha = letter + digit + m.R() - -eqcharset(m.S"", m.P(false)) -eqcharset(upper, m.R("AZ")) -eqcharset(lower, m.R("az")) -eqcharset(upper + lower, m.R("AZ", "az")) -eqcharset(upper + lower, m.R("AZ", "cz", "aa", "bb", "90")) -eqcharset(digit, m.S"01234567" + "8" + "9") -eqcharset(upper, letter - lower) -eqcharset(m.S(""), m.R()) -assert(cs2str(m.S("")) == "") - -eqcharset(m.S"\0", "\0") -eqcharset(m.S"\1\0\2", m.R"\0\2") -eqcharset(m.S"\1\0\2", m.R"\1\2" + "\0") -eqcharset(m.S"\1\0\2" - "\0", m.R"\1\2") - -local word = alpha^1 * (1 - alpha)^0 - -assert((word^0 * -1):match"alo alo") -assert(m.match(word^1 * -1, "alo alo")) -assert(m.match(word^2 * -1, "alo alo")) -assert(not m.match(word^3 * -1, "alo alo")) - -assert(not m.match(word^-1 * -1, "alo alo")) -assert(m.match(word^-2 * -1, "alo alo")) -assert(m.match(word^-3 * -1, "alo alo")) - -local eos = m.P(-1) - -assert(m.match(digit^0 * letter * digit * eos, "1298a1")) -assert(not m.match(digit^0 * letter * eos, "1257a1")) - -b = { - [1] = "(" * (((1 - m.S"()") + #m.P"(" * m.V(1))^0) * ")" -} - -assert(m.match(b, "(al())()")) -assert(not m.match(b * eos, "(al())()")) -assert(m.match(b * eos, "((al())()(é))")) -assert(not m.match(b, "(al()()")) - -assert(not m.match(letter^1 - "for", "foreach")) -assert(m.match(letter^1 - ("for" * eos), "foreach")) -assert(not m.match(letter^1 - ("for" * eos), "for")) - -function basiclookfor (p) - return m.P { - [1] = p + (1 * m.V(1)) - } -end - -function caplookfor (p) - return basiclookfor(p:C()) -end - -assert(m.match(caplookfor(letter^1), " 4achou123...") == "achou") -a = {m.match(caplookfor(letter^1)^0, " two words, one more ")} -checkeq(a, {"two", "words", "one", "more"}) - -assert(m.match( basiclookfor((#m.P(b) * 1) * m.Cp()), " ( (a)") == 7) - -a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "123")} -checkeq(a, {"123", "d"}) - --- bug in LPeg 0.12 (nil value does not create a 'ktable') -assert(m.match(m.Cc(nil), "") == nil) - -a = {m.match(m.C(digit^1 * m.Cc"d") + m.C(letter^1 * m.Cc"l"), "abcd")} -checkeq(a, {"abcd", "l"}) - -a = {m.match(m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} -checkeq(a, {10,20,30,2}) -a = {m.match(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp(), 'aaa')} -checkeq(a, {1,10,20,30,2}) -a = m.match(m.Ct(m.Cp() * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') -checkeq(a, {1,10,20,30,2}) -a = m.match(m.Ct(m.Cp() * m.Cc(7,8) * m.Cc(10,20,30) * 'a' * m.Cp()), 'aaa') -checkeq(a, {1,7,8,10,20,30,2}) -a = {m.match(m.Cc() * m.Cc() * m.Cc(1) * m.Cc(2,3,4) * m.Cc() * 'a', 'aaa')} -checkeq(a, {1,2,3,4}) - -a = {m.match(m.Cp() * letter^1 * m.Cp(), "abcd")} -checkeq(a, {1, 5}) - - -t = {m.match({[1] = m.C(m.C(1) * m.V(1) + -1)}, "abc")} -checkeq(t, {"abc", "a", "bc", "b", "c", "c", ""}) - --- bug in 0.12 ('hascapture' did not check for captures inside a rule) -do - local pat = m.P{ - 'S'; - S1 = m.C('abc') + 3, - S = #m.V('S1') -- rule has capture, but '#' must ignore it - } - assert(pat:match'abc' == 1) -end - - --- test for small capture boundary -for i = 250,260 do - assert(#m.match(m.C(i), string.rep('a', i)) == i) - assert(#m.match(m.C(m.C(i)), string.rep('a', i)) == i) -end - --- tests for any*n and any*-n -for n = 1, 550, 13 do - local x_1 = string.rep('x', n - 1) - local x = x_1 .. 'a' - assert(not m.P(n):match(x_1)) - assert(m.P(n):match(x) == n + 1) - assert(n < 4 or m.match(m.P(n) + "xxx", x_1) == 4) - assert(m.C(n):match(x) == x) - assert(m.C(m.C(n)):match(x) == x) - assert(m.P(-n):match(x_1) == 1) - assert(not m.P(-n):match(x)) - assert(n < 13 or m.match(m.Cc(20) * ((n - 13) * m.P(10)) * 3, x) == 20) - local n3 = math.floor(n/3) - assert(m.match(n3 * m.Cp() * n3 * n3, x) == n3 + 1) -end - --- true values -assert(m.P(0):match("x") == 1) -assert(m.P(0):match("") == 1) -assert(m.C(0):match("x") == "") - -assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxu") == 1) -assert(m.match(m.Cc(0) * m.P(10) + m.Cc(1) * "xuxu", "xuxuxuxuxu") == 0) -assert(m.match(m.C(m.P(2)^1), "abcde") == "abcd") -p = m.Cc(0) * 1 + m.Cc(1) * 2 + m.Cc(2) * 3 + m.Cc(3) * 4 - - --- test for alternation optimization -assert(m.match(m.P"a"^1 + "ab" + m.P"x"^0, "ab") == 2) -assert(m.match((m.P"a"^1 + "ab" + m.P"x"^0 * 1)^0, "ab") == 3) -assert(m.match(m.P"ab" + "cd" + "" + "cy" + "ak", "98") == 1) -assert(m.match(m.P"ab" + "cd" + "ax" + "cy", "ax") == 3) -assert(m.match("a" * m.P"b"^0 * "c" + "cd" + "ax" + "cy", "ax") == 3) -assert(m.match((m.P"ab" + "cd" + "ax" + "cy")^0, "ax") == 3) -assert(m.match(m.P(1) * "x" + m.S"" * "xu" + "ay", "ay") == 3) -assert(m.match(m.P"abc" + "cde" + "aka", "aka") == 4) -assert(m.match(m.S"abc" * "x" + "cde" + "aka", "ax") == 3) -assert(m.match(m.S"abc" * "x" + "cde" + "aka", "aka") == 4) -assert(m.match(m.S"abc" * "x" + "cde" + "aka", "cde") == 4) -assert(m.match(m.S"abc" * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) -assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "ax") == 3) -assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "aka") == 4) -assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "cde" + "aka", "cde") == 4) -assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "aka") == 4) -assert(m.match("ab" + m.S"abc" * m.P"y"^0 * "x" + "ide" + m.S"ab" * "ka", "ax") == 3) -assert(m.match(m.P(1) * "x" + "cde" + m.S"ab" * "ka", "aka") == 4) -assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "aka") == 4) -assert(m.match(m.P(1) * "x" + "cde" + m.P(1) * "ka", "cde") == 4) -assert(m.match(m.P"eb" + "cd" + m.P"e"^0 + "x", "ee") == 3) -assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "abcd") == 3) -assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "eeex") == 4) -assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "cd") == 3) -assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x", "x") == 1) -assert(m.match(m.P"ab" + "cd" + m.P"e"^0 + "x" + "", "zee") == 1) -assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "abcd") == 3) -assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "eeex") == 4) -assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "cd") == 3) -assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x", "x") == 2) -assert(m.match(m.P"ab" + "cd" + m.P"e"^1 + "x" + "", "zee") == 1) -assert(not m.match(("aa" * m.P"bc"^-1 + "aab") * "e", "aabe")) - -assert(m.match("alo" * (m.P"\n" + -1), "alo") == 4) - - --- bug in 0.12 (rc1) -assert(m.match((m.P"\128\187\191" + m.S"abc")^0, "\128\187\191") == 4) - -assert(m.match(m.S"\0\128\255\127"^0, string.rep("\0\128\255\127", 10)) == - 4*10 + 1) - --- optimizations with optional parts -assert(m.match(("ab" * -m.P"c")^-1, "abc") == 1) -assert(m.match(("ab" * #m.P"c")^-1, "abd") == 1) -assert(m.match(("ab" * m.B"c")^-1, "ab") == 1) -assert(m.match(("ab" * m.P"cd"^0)^-1, "abcdcdc") == 7) - -assert(m.match(m.P"ab"^-1 - "c", "abcd") == 3) - -p = ('Aa' * ('Bb' * ('Cc' * m.P'Dd'^0)^0)^0)^-1 -assert(p:match("AaBbCcDdBbCcDdDdDdBb") == 21) - - --- bug in 0.12.2 --- p = { ('ab' ('c' 'ef'?)*)? } -p = m.C(('ab' * ('c' * m.P'ef'^-1)^0)^-1) -s = "abcefccefc" -assert(s == p:match(s)) - - -pi = "3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510" -assert(m.match(m.Cs((m.P"1" / "a" + m.P"5" / "b" + m.P"9" / "c" + 1)^0), pi) == - m.match(m.Cs((m.P(1) / {["1"] = "a", ["5"] = "b", ["9"] = "c"})^0), pi)) -print"+" - - --- tests for capture optimizations -assert(m.match((m.P(3) + 4 * m.Cp()) * "a", "abca") == 5) -t = {m.match(((m.P"a" + m.Cp()) * m.P"x")^0, "axxaxx")} -checkeq(t, {3, 6}) - - --- tests for numbered captures -p = m.C(1) -assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 3, "abcdefgh") == "a") -assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 1, "abcdefgh") == "abcdef") -assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 4, "abcdefgh") == "bc") -assert(m.match(m.C(m.C(p * m.C(2)) * m.C(3)) / 0, "abcdefgh") == 7) - -a, b, c = m.match(p * (m.C(p * m.C(2)) * m.C(3) / 4) * p, "abcdefgh") -assert(a == "a" and b == "efg" and c == "h") - --- test for table captures -t = m.match(m.Ct(letter^1), "alo") -checkeq(t, {}) - -t, n = m.match(m.Ct(m.C(letter)^1) * m.Cc"t", "alo") -assert(n == "t" and table.concat(t) == "alo") - -t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") -assert(table.concat(t, ";") == "alo;a;l;o") - -t = m.match(m.Ct(m.C(m.C(letter)^1)), "alo") -assert(table.concat(t, ";") == "alo;a;l;o") - -t = m.match(m.Ct(m.Ct((m.Cp() * letter * m.Cp())^1)), "alo") -assert(table.concat(t[1], ";") == "1;2;2;3;3;4") - -t = m.match(m.Ct(m.C(m.C(1) * 1 * m.C(1))), "alo") -checkeq(t, {"alo", "a", "o"}) - - --- tests for groups -p = m.Cg(1) -- no capture -assert(p:match('x') == 'x') -p = m.Cg(m.P(true)/function () end * 1) -- no value -assert(p:match('x') == 'x') -p = m.Cg(m.Cg(m.Cg(m.C(1)))) -assert(p:match('x') == 'x') -p = m.Cg(m.Cg(m.Cg(m.C(1))^0) * m.Cg(m.Cc(1) * m.Cc(2))) -t = {p:match'abc'} -checkeq(t, {'a', 'b', 'c', 1, 2}) - -p = m.Ct(m.Cg(m.Cc(10), "hi") * m.C(1)^0 * m.Cg(m.Cc(20), "ho")) -t = p:match'' -checkeq(t, {hi = 10, ho = 20}) -t = p:match'abc' -checkeq(t, {hi = 10, ho = 20, 'a', 'b', 'c'}) - --- non-string group names -p = m.Ct(m.Cg(1, print) * m.Cg(1, 23.5) * m.Cg(1, io)) -t = p:match('abcdefghij') -assert(t[print] == 'a' and t[23.5] == 'b' and t[io] == 'c') - - --- test for error messages -local function checkerr (msg, f, ...) - local st, err = pcall(f, ...) - assert(not st and m.match({ m.P(msg) + 1 * m.V(1) }, err)) -end - -checkerr("rule '1' may be left recursive", m.match, { m.V(1) * 'a' }, "a") -checkerr("rule '1' used outside a grammar", m.match, m.V(1), "") -checkerr("rule 'hiii' used outside a grammar", m.match, m.V('hiii'), "") -checkerr("rule 'hiii' undefined in given grammar", m.match, { m.V('hiii') }, "") -checkerr("undefined in given grammar", m.match, { m.V{} }, "") - -checkerr("rule 'A' is not a pattern", m.P, { m.P(1), A = {} }) -checkerr("grammar has no initial rule", m.P, { [print] = {} }) - --- grammar with a long call chain before left recursion -p = {'a', - a = m.V'b' * m.V'c' * m.V'd' * m.V'a', - b = m.V'c', - c = m.V'd', - d = m.V'e', - e = m.V'f', - f = m.V'g', - g = m.P'' -} -checkerr("rule 'a' may be left recursive", m.match, p, "a") - --- Bug in peephole optimization of LPeg 0.12 (IJmp -> ICommit) --- the next grammar has an original sequence IJmp -> ICommit -> IJmp L1 --- that is optimized to ICommit L1 - -p = m.P { (m.P {m.P'abc'} + 'ayz') * m.V'y'; y = m.P'x' } -assert(p:match('abcx') == 5 and p:match('ayzx') == 5 and not p:match'abc') - - -do - -- large dynamic Cc - local lim = 2^16 - 1 - local c = 0 - local function seq (n) - if n == 1 then c = c + 1; return m.Cc(c) - else - local m = math.floor(n / 2) - return seq(m) * seq(n - m) - end - end - p = m.Ct(seq(lim)) - t = p:match('') - assert(t[lim] == lim) - checkerr("too many", function () p = p / print end) - checkerr("too many", seq, lim + 1) -end - - --- tests for non-pattern as arguments to pattern functions - -p = { ('a' * m.V(1))^-1 } * m.P'b' * { 'a' * m.V(2); m.V(1)^-1 } -assert(m.match(p, "aaabaac") == 7) - -p = m.P'abc' * 2 * -5 * true * 'de' -- mix of numbers and strings and booleans - -assert(p:match("abc01de") == 8) -assert(p:match("abc01de3456") == nil) - -p = 'abc' * (2 * (-5 * (true * m.P'de'))) - -assert(p:match("abc01de") == 8) -assert(p:match("abc01de3456") == nil) - -p = { m.V(2), m.P"abc" } * - (m.P{ "xx", xx = m.P"xx" } + { "x", x = m.P"a" * m.V"x" + "" }) -assert(p:match("abcaaaxx") == 7) -assert(p:match("abcxx") == 6) - - --- a large table capture -t = m.match(m.Ct(m.C('a')^0), string.rep("a", 10000)) -assert(#t == 10000 and t[1] == 'a' and t[#t] == 'a') - -print('+') - - --- bug in 0.10 (rechecking a grammar, after tail-call optimization) -m.P{ m.P { (m.P(3) + "xuxu")^0 * m.V"xuxu", xuxu = m.P(1) } } - -local V = m.V - -local Space = m.S(" \n\t")^0 -local Number = m.C(m.R("09")^1) * Space -local FactorOp = m.C(m.S("+-")) * Space -local TermOp = m.C(m.S("*/")) * Space -local Open = "(" * Space -local Close = ")" * Space - - -local function f_factor (v1, op, v2, d) - assert(d == nil) - if op == "+" then return v1 + v2 - else return v1 - v2 - end -end - - -local function f_term (v1, op, v2, d) - assert(d == nil) - if op == "*" then return v1 * v2 - else return v1 / v2 - end -end - -G = m.P{ "Exp", - Exp = m.Cf(V"Factor" * m.Cg(FactorOp * V"Factor")^0, f_factor); - Factor = m.Cf(V"Term" * m.Cg(TermOp * V"Term")^0, f_term); - Term = Number / tonumber + Open * V"Exp" * Close; -} - -G = Space * G * -1 - -for _, s in ipairs{" 3 + 5*9 / (1+1) ", "3+4/2", "3+3-3- 9*2+3*9/1- 8"} do - assert(m.match(G, s) == loadstring("return "..s)()) -end - - --- test for grammars (errors deep in calling non-terminals) -g = m.P{ - [1] = m.V(2) + "a", - [2] = "a" * m.V(3) * "x", - [3] = "b" * m.V(3) + "c" -} - -assert(m.match(g, "abbbcx") == 7) -assert(m.match(g, "abbbbx") == 2) - - --- tests for \0 -assert(m.match(m.R("\0\1")^1, "\0\1\0") == 4) -assert(m.match(m.S("\0\1ab")^1, "\0\1\0a") == 5) -assert(m.match(m.P(1)^3, "\0\1\0a") == 5) -assert(not m.match(-4, "\0\1\0a")) -assert(m.match("\0\1\0a", "\0\1\0a") == 5) -assert(m.match("\0\0\0", "\0\0\0") == 4) -assert(not m.match("\0\0\0", "\0\0")) - - --- tests for predicates -assert(not m.match(-m.P("a") * 2, "alo")) -assert(m.match(- -m.P("a") * 2, "alo") == 3) -assert(m.match(#m.P("a") * 2, "alo") == 3) -assert(m.match(##m.P("a") * 2, "alo") == 3) -assert(not m.match(##m.P("c") * 2, "alo")) -assert(m.match(m.Cs((##m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") -assert(m.match(m.Cs((#((#m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") -assert(m.match(m.Cs((- -m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") -assert(m.match(m.Cs((-((-m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.") - -p = -m.P'a' * m.Cc(1) + -m.P'b' * m.Cc(2) + -m.P'c' * m.Cc(3) -assert(p:match('a') == 2 and p:match('') == 1 and p:match('b') == 1) - -p = -m.P'a' * m.Cc(10) + #m.P'a' * m.Cc(20) -assert(p:match('a') == 20 and p:match('') == 10 and p:match('b') == 10) - - - --- look-behind predicate -assert(not m.match(m.B'a', 'a')) -assert(m.match(1 * m.B'a', 'a') == 2) -assert(not m.match(m.B(1), 'a')) -assert(m.match(1 * m.B(1), 'a') == 2) -assert(m.match(-m.B(1), 'a') == 1) -assert(m.match(m.B(250), string.rep('a', 250)) == nil) -assert(m.match(250 * m.B(250), string.rep('a', 250)) == 251) - --- look-behind with an open call -checkerr("pattern may not have fixed length", m.B, m.V'S1') -checkerr("too long to look behind", m.B, 260) - -B = #letter * -m.B(letter) + -letter * m.B(letter) -x = m.Ct({ (B * m.Cp())^-1 * (1 * m.V(1) + m.P(true)) }) -checkeq(m.match(x, 'ar cal c'), {1,3,4,7,9,10}) -checkeq(m.match(x, ' ar cal '), {2,4,5,8}) -checkeq(m.match(x, ' '), {}) -checkeq(m.match(x, 'aloalo'), {1,7}) - -assert(m.match(B, "a") == 1) -assert(m.match(1 * B, "a") == 2) -assert(not m.B(1 - letter):match("")) -assert((-m.B(letter)):match("") == 1) - -assert((4 * m.B(letter, 4)):match("aaaaaaaa") == 5) -assert(not (4 * m.B(#letter * 5)):match("aaaaaaaa")) -assert((4 * -m.B(#letter * 5)):match("aaaaaaaa") == 5) - --- look-behind with grammars -assert(m.match('a' * m.B{'x', x = m.P(3)}, 'aaa') == nil) -assert(m.match('aa' * m.B{'x', x = m.P('aaa')}, 'aaaa') == nil) -assert(m.match('aaa' * m.B{'x', x = m.P('aaa')}, 'aaaaa') == 4) - - - --- bug in 0.9 -assert(m.match(('a' * #m.P'b'), "ab") == 2) -assert(not m.match(('a' * #m.P'b'), "a")) - -assert(not m.match(#m.S'567', "")) -assert(m.match(#m.S'567' * 1, "6") == 2) - - --- tests for Tail Calls - -p = m.P{ 'a' * m.V(1) + '' } -assert(p:match(string.rep('a', 1000)) == 1001) - --- create a grammar for a simple DFA for even number of 0s and 1s --- --- ->1 <---0---> 2 --- ^ ^ --- | | --- 1 1 --- | | --- V V --- 3 <---0---> 4 --- --- this grammar should keep no backtracking information - -p = m.P{ - [1] = '0' * m.V(2) + '1' * m.V(3) + -1, - [2] = '0' * m.V(1) + '1' * m.V(4), - [3] = '0' * m.V(4) + '1' * m.V(1), - [4] = '0' * m.V(3) + '1' * m.V(2), -} - -assert(p:match(string.rep("00", 10000))) -assert(p:match(string.rep("01", 10000))) -assert(p:match(string.rep("011", 10000))) -assert(not p:match(string.rep("011", 10000) .. "1")) -assert(not p:match(string.rep("011", 10001))) - - --- this grammar does need backtracking info. -local lim = 10000 -p = m.P{ '0' * m.V(1) + '0' } -checkerr("stack overflow", m.match, p, string.rep("0", lim)) -m.setmaxstack(2*lim) -checkerr("stack overflow", m.match, p, string.rep("0", lim)) -m.setmaxstack(2*lim + 4) -assert(m.match(p, string.rep("0", lim)) == lim + 1) - --- this repetition should not need stack space (only the call does) -p = m.P{ ('a' * m.V(1))^0 * 'b' + 'c' } -m.setmaxstack(200) -assert(p:match(string.rep('a', 180) .. 'c' .. string.rep('b', 180)) == 362) - -m.setmaxstack(100) -- restore low limit - --- tests for optional start position -assert(m.match("a", "abc", 1)) -assert(m.match("b", "abc", 2)) -assert(m.match("c", "abc", 3)) -assert(not m.match(1, "abc", 4)) -assert(m.match("a", "abc", -3)) -assert(m.match("b", "abc", -2)) -assert(m.match("c", "abc", -1)) -assert(m.match("abc", "abc", -4)) -- truncate to position 1 - -assert(m.match("", "abc", 10)) -- empty string is everywhere! -assert(m.match("", "", 10)) -assert(not m.match(1, "", 1)) -assert(not m.match(1, "", -1)) -assert(not m.match(1, "", 0)) - -print("+") - - --- tests for argument captures -checkerr("invalid argument", m.Carg, 0) -checkerr("invalid argument", m.Carg, -1) -checkerr("invalid argument", m.Carg, 2^18) -checkerr("absent extra argument #1", m.match, m.Carg(1), 'a', 1) -assert(m.match(m.Carg(1), 'a', 1, print) == print) -x = {m.match(m.Carg(1) * m.Carg(2), '', 1, 10, 20)} -checkeq(x, {10, 20}) - -assert(m.match(m.Cmt(m.Cg(m.Carg(3), "a") * - m.Cmt(m.Cb("a"), function (s,i,x) - assert(s == "a" and i == 1); - return i, x+1 - end) * - m.Carg(2), function (s,i,a,b,c) - assert(s == "a" and i == 1 and c == nil); - return i, 2*a + 3*b - end) * "a", - "a", 1, false, 100, 1000) == 2*1001 + 3*100) - - --- tests for Lua functions - -t = {} -s = "" -p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; return nil end) * false -s = "hi, this is a test" -assert(m.match(((p - m.P(-1)) + 2)^0, s) == string.len(s) + 1) -assert(#t == string.len(s)/2 and t[1] == 1 and t[2] == 3) - -assert(not m.match(p, s)) - -p = mt.__add(function (s, i) return i end, function (s, i) return nil end) -assert(m.match(p, "alo")) - -p = mt.__mul(function (s, i) return i end, function (s, i) return nil end) -assert(not m.match(p, "alo")) - - -t = {} -p = function (s1, i) assert(s == s1); t[#t + 1] = i; return i end -s = "hi, this is a test" -assert(m.match((m.P(1) * p)^0, s) == string.len(s) + 1) -assert(#t == string.len(s) and t[1] == 2 and t[2] == 3) - -t = {} -p = m.P(function (s1, i) assert(s == s1); t[#t + 1] = i; - return i <= s1:len() and i end) * 1 -s = "hi, this is a test" -assert(m.match(p^0, s) == string.len(s) + 1) -assert(#t == string.len(s) + 1 and t[1] == 1 and t[2] == 2) - -p = function (s1, i) return m.match(m.P"a"^1, s1, i) end -assert(m.match(p, "aaaa") == 5) -assert(m.match(p, "abaa") == 2) -assert(not m.match(p, "baaa")) - -checkerr("invalid position", m.match, function () return 2^20 end, s) -checkerr("invalid position", m.match, function () return 0 end, s) -checkerr("invalid position", m.match, function (s, i) return i - 1 end, s) -checkerr("invalid position", m.match, - m.P(1)^0 * function (_, i) return i - 1 end, s) -assert(m.match(m.P(1)^0 * function (_, i) return i end * -1, s)) -checkerr("invalid position", m.match, - m.P(1)^0 * function (_, i) return i + 1 end, s) -assert(m.match(m.P(function (s, i) return s:len() + 1 end) * -1, s)) -checkerr("invalid position", m.match, m.P(function (s, i) return s:len() + 2 end) * -1, s) -assert(not m.match(m.P(function (s, i) return s:len() end) * -1, s)) -assert(m.match(m.P(1)^0 * function (_, i) return true end, s) == - string.len(s) + 1) -for i = 1, string.len(s) + 1 do - assert(m.match(function (_, _) return i end, s) == i) -end - -p = (m.P(function (s, i) return i%2 == 0 and i end) * 1 - + m.P(function (s, i) return i%2 ~= 0 and i + 2 <= s:len() and i end) * 3)^0 - * -1 -assert(p:match(string.rep('a', 14000))) - --- tests for Function Replacements -f = function (a, ...) if a ~= "x" then return {a, ...} end end - -t = m.match(m.C(1)^0/f, "abc") -checkeq(t, {"a", "b", "c"}) - -t = m.match(m.C(1)^0/f/f, "abc") -checkeq(t, {{"a", "b", "c"}}) - -t = m.match(m.P(1)^0/f/f, "abc") -- no capture -checkeq(t, {{"abc"}}) - -t = m.match((m.P(1)^0/f * m.Cp())/f, "abc") -checkeq(t, {{"abc"}, 4}) - -t = m.match((m.C(1)^0/f * m.Cp())/f, "abc") -checkeq(t, {{"a", "b", "c"}, 4}) - -t = m.match((m.C(1)^0/f * m.Cp())/f, "xbc") -checkeq(t, {4}) - -t = m.match(m.C(m.C(1)^0)/f, "abc") -checkeq(t, {"abc", "a", "b", "c"}) - -g = function (...) return 1, ... end -t = {m.match(m.C(1)^0/g/g, "abc")} -checkeq(t, {1, 1, "a", "b", "c"}) - -t = {m.match(m.Cc(nil,nil,4) * m.Cc(nil,3) * m.Cc(nil, nil) / g / g, "")} -t1 = {1,1,nil,nil,4,nil,3,nil,nil} -for i=1,10 do assert(t[i] == t1[i]) end - --- bug in 0.12.2: ktable with only nil could be eliminated when joining --- with a pattern without ktable -assert((m.P"aaa" * m.Cc(nil)):match"aaa" == nil) - -t = {m.match((m.C(1) / function (x) return x, x.."x" end)^0, "abc")} -checkeq(t, {"a", "ax", "b", "bx", "c", "cx"}) - -t = m.match(m.Ct((m.C(1) / function (x,y) return y, x end * m.Cc(1))^0), "abc") -checkeq(t, {nil, "a", 1, nil, "b", 1, nil, "c", 1}) - --- tests for Query Replacements - -assert(m.match(m.C(m.C(1)^0)/{abc = 10}, "abc") == 10) -assert(m.match(m.C(1)^0/{a = 10}, "abc") == 10) -assert(m.match(m.S("ba")^0/{ab = 40}, "abc") == 40) -t = m.match(m.Ct((m.S("ba")/{a = 40})^0), "abc") -checkeq(t, {40}) - -assert(m.match(m.Cs((m.C(1)/{a=".", d=".."})^0), "abcdde") == ".bc....e") -assert(m.match(m.Cs((m.C(1)/{f="."})^0), "abcdde") == "abcdde") -assert(m.match(m.Cs((m.C(1)/{d="."})^0), "abcdde") == "abc..e") -assert(m.match(m.Cs((m.C(1)/{e="."})^0), "abcdde") == "abcdd.") -assert(m.match(m.Cs((m.C(1)/{e=".", f="+"})^0), "eefef") == "..+.+") -assert(m.match(m.Cs((m.C(1))^0), "abcdde") == "abcdde") -assert(m.match(m.Cs(m.C(m.C(1)^0)), "abcdde") == "abcdde") -assert(m.match(1 * m.Cs(m.P(1)^0), "abcdde") == "bcdde") -assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "abcdde") == "abcdde") -assert(m.match(m.Cs((m.C('0')/'x' + 1)^0), "0ab0b0") == "xabxbx") -assert(m.match(m.Cs((m.C('0')/'x' + m.P(1)/{b=3})^0), "b0a0b") == "3xax3") -assert(m.match(m.P(1)/'%0%0'/{aa = -3} * 'x', 'ax') == -3) -assert(m.match(m.C(1)/'%0%1'/{aa = 'z'}/{z = -3} * 'x', 'ax') == -3) - -assert(m.match(m.Cs(m.Cc(0) * (m.P(1)/"")), "4321") == "0") - -assert(m.match(m.Cs((m.P(1) / "%0")^0), "abcd") == "abcd") -assert(m.match(m.Cs((m.P(1) / "%0.%0")^0), "abcd") == "a.ab.bc.cd.d") -assert(m.match(m.Cs((m.P("a") / "%0.%0" + 1)^0), "abcad") == "a.abca.ad") -assert(m.match(m.C("a") / "%1%%%0", "a") == "a%a") -assert(m.match(m.Cs((m.P(1) / ".xx")^0), "abcd") == ".xx.xx.xx.xx") -assert(m.match(m.Cp() * m.P(3) * m.Cp()/"%2%1%1 - %0 ", "abcde") == - "411 - abc ") - -assert(m.match(m.P(1)/"%0", "abc") == "a") -checkerr("invalid capture index", m.match, m.P(1)/"%1", "abc") -checkerr("invalid capture index", m.match, m.P(1)/"%9", "abc") - -p = m.C(1) -p = p * p; p = p * p; p = p * p * m.C(1) / "%9 - %1" -assert(p:match("1234567890") == "9 - 1") - -assert(m.match(m.Cc(print), "") == print) - --- too many captures (just ignore extra ones) -p = m.C(1)^0 / "%2-%9-%0-%9" -assert(p:match"01234567890123456789" == "1-8-01234567890123456789-8") -s = string.rep("12345678901234567890", 20) -assert(m.match(m.C(1)^0 / "%9-%1-%0-%3", s) == "9-1-" .. s .. "-3") - --- string captures with non-string subcaptures -p = m.Cc('alo') * m.C(1) / "%1 - %2 - %1" -assert(p:match'x' == 'alo - x - alo') - -checkerr("invalid capture value (a boolean)", m.match, m.Cc(true) / "%1", "a") - --- long strings for string capture -l = 10000 -s = string.rep('a', l) .. string.rep('b', l) .. string.rep('c', l) - -p = (m.C(m.P'a'^1) * m.C(m.P'b'^1) * m.C(m.P'c'^1)) / '%3%2%1' - -assert(p:match(s) == string.rep('c', l) .. - string.rep('b', l) .. - string.rep('a', l)) - -print"+" - --- accumulator capture -function f (x) return x + 1 end -assert(m.match(m.Cf(m.Cc(0) * m.C(1)^0, f), "alo alo") == 7) - -t = {m.match(m.Cf(m.Cc(1,2,3), error), "")} -checkeq(t, {1}) -p = m.Cf(m.Ct(true) * m.Cg(m.C(m.R"az"^1) * "=" * m.C(m.R"az"^1) * ";")^0, - rawset) -t = p:match("a=b;c=du;xux=yuy;") -checkeq(t, {a="b", c="du", xux="yuy"}) - - --- errors in accumulator capture - --- no initial capture -checkerr("no initial value", m.match, m.Cf(m.P(5), print), 'aaaaaa') --- no initial capture (very long match forces fold to be a pair open-close) -checkerr("no initial value", m.match, m.Cf(m.P(500), print), - string.rep('a', 600)) - --- nested capture produces no initial value -checkerr("no initial value", m.match, m.Cf(m.P(1) / {}, print), "alo") - - --- tests for loop checker - -local function isnullable (p) - checkerr("may accept empty string", function (p) return p^0 end, m.P(p)) -end - -isnullable(m.P("x")^-4) -assert(m.match(((m.P(0) + 1) * m.S"al")^0, "alo") == 3) -assert(m.match((("x" + #m.P(1))^-4 * m.S"al")^0, "alo") == 3) -isnullable("") -isnullable(m.P("x")^0) -isnullable(m.P("x")^-1) -isnullable(m.P("x") + 1 + 2 + m.P("a")^-1) -isnullable(-m.P("ab")) -isnullable(- -m.P("ab")) -isnullable(# #(m.P("ab") + "xy")) -isnullable(- #m.P("ab")^0) -isnullable(# -m.P("ab")^1) -isnullable(#m.V(3)) -isnullable(m.V(3) + m.V(1) + m.P('a')^-1) -isnullable({[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(0)}) -assert(m.match(m.P{[1] = m.V(2) * m.V(3), [2] = m.V(3), [3] = m.P(1)}^0, "abc") - == 3) -assert(m.match(m.P""^-3, "a") == 1) - -local function find (p, s) - return m.match(basiclookfor(p), s) -end - - -local function badgrammar (g, expected) - local stat, msg = pcall(m.P, g) - assert(not stat) - if expected then assert(find(expected, msg)) end -end - -badgrammar({[1] = m.V(1)}, "rule '1'") -badgrammar({[1] = m.V(2)}, "rule '2'") -- invalid non-terminal -badgrammar({[1] = m.V"x"}, "rule 'x'") -- invalid non-terminal -badgrammar({[1] = m.V{}}, "rule '(a table)'") -- invalid non-terminal -badgrammar({[1] = #m.P("a") * m.V(1)}, "rule '1'") -- left-recursive -badgrammar({[1] = -m.P("a") * m.V(1)}, "rule '1'") -- left-recursive -badgrammar({[1] = -1 * m.V(1)}, "rule '1'") -- left-recursive -badgrammar({[1] = -1 + m.V(1)}, "rule '1'") -- left-recursive -badgrammar({[1] = 1 * m.V(2), [2] = m.V(2)}, "rule '2'") -- left-recursive -badgrammar({[1] = 1 * m.V(2)^0, [2] = m.P(0)}, "rule '1'") -- inf. loop -badgrammar({ m.V(2), m.V(3)^0, m.P"" }, "rule '2'") -- inf. loop -badgrammar({ m.V(2) * m.V(3)^0, m.V(3)^0, m.P"" }, "rule '1'") -- inf. loop -badgrammar({"x", x = #(m.V(1) * 'a') }, "rule '1'") -- inf. loop -badgrammar({ -(m.V(1) * 'a') }, "rule '1'") -- inf. loop -badgrammar({"x", x = m.P'a'^-1 * m.V"x"}, "rule 'x'") -- left recursive -badgrammar({"x", x = m.P'a' * m.V"y"^1, y = #m.P(1)}, "rule 'x'") - -assert(m.match({'a' * -m.V(1)}, "aaa") == 2) -assert(m.match({'a' * -m.V(1)}, "aaaa") == nil) - - --- good x bad grammars -m.P{ ('a' * m.V(1))^-1 } -m.P{ -('a' * m.V(1)) } -m.P{ ('abc' * m.V(1))^-1 } -m.P{ -('abc' * m.V(1)) } -badgrammar{ #m.P('abc') * m.V(1) } -badgrammar{ -('a' + m.V(1)) } -m.P{ #('a' * m.V(1)) } -badgrammar{ #('a' + m.V(1)) } -m.P{ m.B{ m.P'abc' } * 'a' * m.V(1) } -badgrammar{ m.B{ m.P'abc' } * m.V(1) } -badgrammar{ ('a' + m.P'bcd')^-1 * m.V(1) } - - --- simple tests for maximum sizes: -local p = m.P"a" -for i=1,14 do p = p * p end - -p = {} -for i=1,100 do p[i] = m.P"a" end -p = m.P(p) - - --- strange values for rule labels - -p = m.P{ "print", - print = m.V(print), - [print] = m.V(_G), - [_G] = m.P"a", - } - -assert(p:match("a")) - --- initial rule -g = {} -for i = 1, 10 do g["i"..i] = "a" * m.V("i"..i+1) end -g.i11 = m.P"" -for i = 1, 10 do - g[1] = "i"..i - local p = m.P(g) - assert(p:match("aaaaaaaaaaa") == 11 - i + 1) -end - -print"+" - - --- tests for back references -checkerr("back reference 'x' not found", m.match, m.Cb('x'), '') -checkerr("back reference 'b' not found", m.match, m.Cg(1, 'a') * m.Cb('b'), 'a') - -p = m.Cg(m.C(1) * m.C(1), "k") * m.Ct(m.Cb("k")) -t = p:match("ab") -checkeq(t, {"a", "b"}) - -p = m.P(true) -for i = 1, 10 do p = p * m.Cg(1, i) end -for i = 1, 10 do - local p = p * m.Cb(i) - assert(p:match('abcdefghij') == string.sub('abcdefghij', i, i)) -end - - -t = {} -function foo (p) t[#t + 1] = p; return p .. "x" end - -p = m.Cg(m.C(2) / foo, "x") * m.Cb"x" * - m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * - m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" * - m.Cg(m.Cb('x') / foo, "x") * m.Cb"x" -x = {p:match'ab'} -checkeq(x, {'abx', 'abxx', 'abxxx', 'abxxxx'}) -checkeq(t, {'ab', - 'ab', 'abx', - 'ab', 'abx', 'abxx', - 'ab', 'abx', 'abxx', 'abxxx'}) - - - --- tests for match-time captures - -p = m.P'a' * (function (s, i) return (s:sub(i, i) == 'b') and i + 1 end) - + 'acd' - -assert(p:match('abc') == 3) -assert(p:match('acd') == 4) - -local function id (s, i, ...) - return true, ... -end - -assert(m.Cmt(m.Cs((m.Cmt(m.S'abc' / { a = 'x', c = 'y' }, id) + - m.R'09'^1 / string.char + - m.P(1))^0), id):match"acb98+68c" == "xyb\98+\68y") - -p = m.P{'S', - S = m.V'atom' * space - + m.Cmt(m.Ct("(" * space * (m.Cmt(m.V'S'^1, id) + m.P(true)) * ")" * space), id), - atom = m.Cmt(m.C(m.R("AZ", "az", "09")^1), id) -} -x = p:match"(a g () ((b) c) (d (e)))" -checkeq(x, {'a', 'g', {}, {{'b'}, 'c'}, {'d', {'e'}}}); - -x = {(m.Cmt(1, id)^0):match(string.rep('a', 500))} -assert(#x == 500) - -local function id(s, i, x) - if x == 'a' then return i, 1, 3, 7 - else return nil, 2, 4, 6, 8 - end -end - -p = ((m.P(id) * 1 + m.Cmt(2, id) * 1 + m.Cmt(1, id) * 1))^0 -assert(table.concat{p:match('abababab')} == string.rep('137', 4)) - -local function ref (s, i, x) - return m.match(x, s, i - x:len()) -end - -assert(m.Cmt(m.P(1)^0, ref):match('alo') == 4) -assert((m.P(1) * m.Cmt(m.P(1)^0, ref)):match('alo') == 4) -assert(not (m.P(1) * m.Cmt(m.C(1)^0, ref)):match('alo')) - -ref = function (s,i,x) return i == tonumber(x) and i, 'xuxu' end - -assert(m.Cmt(1, ref):match'2') -assert(not m.Cmt(1, ref):match'1') -assert(m.Cmt(m.P(1)^0, ref):match'03') - -function ref (s, i, a, b) - if a == b then return i, a:upper() end -end - -p = m.Cmt(m.C(m.R"az"^1) * "-" * m.C(m.R"az"^1), ref) -p = (any - p)^0 * p * any^0 * -1 - -assert(p:match'abbbc-bc ddaa' == 'BC') - -do -- match-time captures cannot be optimized away - local touch = 0 - f = m.P(function () touch = touch + 1; return true end) - - local function check(n) n = n or 1; assert(touch == n); touch = 0 end - - assert(m.match(f * false + 'b', 'a') == nil); check() - assert(m.match(f * false + 'b', '') == nil); check() - assert(m.match( (f * 'a')^0 * 'b', 'b') == 2); check() - assert(m.match( (f * 'a')^0 * 'b', '') == nil); check() - assert(m.match( (f * 'a')^-1 * 'b', 'b') == 2); check() - assert(m.match( (f * 'a')^-1 * 'b', '') == nil); check() - assert(m.match( ('b' + f * 'a')^-1 * 'b', '') == nil); check() - assert(m.match( (m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); check() - assert(m.match( (-m.P(1) * m.P'b'^-1 * f * 'a')^-1 * 'b', '') == nil); - check() - assert(m.match( (f * 'a' + 'b')^-1 * 'b', '') == nil); check() - assert(m.match(f * 'a' + f * 'b', 'b') == 2); check(2) - assert(m.match(f * 'a' + f * 'b', 'a') == 2); check(1) - assert(m.match(-f * 'a' + 'b', 'b') == 2); check(1) - assert(m.match(-f * 'a' + 'b', '') == nil); check(1) -end - -c = '[' * m.Cg(m.P'='^0, "init") * '[' * - { m.Cmt(']' * m.C(m.P'='^0) * ']' * m.Cb("init"), function (_, _, s1, s2) - return s1 == s2 end) - + 1 * m.V(1) } / 0 - -assert(c:match'[==[]]====]]]]==]===[]' == 18) -assert(c:match'[[]=]====]=]]]==]===[]' == 14) -assert(not c:match'[[]=]====]=]=]==]===[]') - - --- old bug: optimization of concat with fail removed match-time capture -p = m.Cmt(0, function (s) p = s end) * m.P(false) -assert(not p:match('alo')) -assert(p == 'alo') - - --- ensure that failed match-time captures are not kept on Lua stack -do - local t = {__mode = "kv"}; setmetatable(t,t) - local c = 0 - - local function foo (s,i) - collectgarbage(); - assert(next(t) == "__mode" and next(t, "__mode") == nil) - local x = {} - t[x] = true - c = c + 1 - return i, x - end - - local p = m.P{ m.Cmt(0, foo) * m.P(false) + m.P(1) * m.V(1) + m.P"" } - p:match(string.rep('1', 10)) - assert(c == 11) -end - -p = (m.P(function () return true, "a" end) * 'a' - + m.P(function (s, i) return i, "aa", 20 end) * 'b' - + m.P(function (s,i) if i <= #s then return i, "aaa" end end) * 1)^0 - -t = {p:match('abacc')} -checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'}) - - -------------------------------------------------------------------- --- Tests for 're' module -------------------------------------------------------------------- - -local re = require "relabel" - -local match, compile = re.match, re.compile - - - -assert(match("a", ".") == 2) -assert(match("a", "''") == 1) -assert(match("", " ! . ") == 1) -assert(not match("a", " ! . ")) -assert(match("abcde", " ( . . ) * ") == 5) -assert(match("abbcde", " [a-c] +") == 5) -assert(match("0abbc1de", "'0' [a-c]+ '1'") == 7) -assert(match("0zz1dda", "'0' [^a-c]+ 'a'") == 8) -assert(match("abbc--", " [a-c] + +") == 5) -assert(match("abbc--", " [ac-] +") == 2) -assert(match("abbc--", " [-acb] + ") == 7) -assert(not match("abbcde", " [b-z] + ")) -assert(match("abb\"de", '"abb"["]"de"') == 7) -assert(match("abceeef", "'ac' ? 'ab' * 'c' { 'e' * } / 'abceeef' ") == "eee") -assert(match("abceeef", "'ac'? 'ab'* 'c' { 'f'+ } / 'abceeef' ") == 8) -local t = {match("abceefe", "( ( & 'e' {} ) ? . ) * ")} -checkeq(t, {4, 5, 7}) -local t = {match("abceefe", "((&&'e' {})? .)*")} -checkeq(t, {4, 5, 7}) -local t = {match("abceefe", "( ( ! ! 'e' {} ) ? . ) *")} -checkeq(t, {4, 5, 7}) -local t = {match("abceefe", "(( & ! & ! 'e' {})? .)*")} -checkeq(t, {4, 5, 7}) - -assert(match("cccx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 5) -assert(match("cdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 4) -assert(match("abcdcdx" , "'ab'? ('ccc' / ('cde' / 'cd'*)? / 'ccc') 'x'+") == 8) - -assert(match("abc", "a <- (. a)?") == 4) -b = "balanced <- '(' ([^()] / balanced)* ')'" -assert(match("(abc)", b)) -assert(match("(a(b)((c) (d)))", b)) -assert(not match("(a(b ((c) (d)))", b)) - -b = compile[[ balanced <- "(" ([^()] / balanced)* ")" ]] -assert(b == m.P(b)) -assert(b:match"((((a))(b)))") - -local g = [[ - S <- "0" B / "1" A / "" -- balanced strings - A <- "0" S / "1" A A -- one more 0 - B <- "1" S / "0" B B -- one more 1 -]] -assert(match("00011011", g) == 9) - -local g = [[ - S <- ("0" B / "1" A)* - A <- "0" / "1" A A - B <- "1" / "0" B B -]] -assert(match("00011011", g) == 9) -assert(match("000110110", g) == 9) -assert(match("011110110", g) == 3) -assert(match("000110010", g) == 1) - -s = "aaaaaaaaaaaaaaaaaaaaaaaa" -assert(match(s, "'a'^3") == 4) -assert(match(s, "'a'^0") == 1) -assert(match(s, "'a'^+3") == s:len() + 1) -assert(not match(s, "'a'^+30")) -assert(match(s, "'a'^-30") == s:len() + 1) -assert(match(s, "'a'^-5") == 6) -for i = 1, s:len() do - assert(match(s, string.format("'a'^+%d", i)) >= i + 1) - assert(match(s, string.format("'a'^-%d", i)) <= i + 1) - assert(match(s, string.format("'a'^%d", i)) == i + 1) -end -assert(match("01234567890123456789", "[0-9]^3+") == 19) - - -assert(match("01234567890123456789", "({....}{...}) -> '%2%1'") == "4560123") -t = match("0123456789", "{| {.}* |}") -checkeq(t, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}) -assert(match("012345", "{| (..) -> '%0%0' |}")[1] == "0101") - -assert(match("abcdef", "( {.} {.} {.} {.} {.} ) -> 3") == "c") -assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 3") == "d") -assert(match("abcdef", "( {:x: . :} {.} {.} {.} {.} ) -> 0") == 6) - -assert(not match("abcdef", "{:x: ({.} {.} {.}) -> 2 :} =x")) -assert(match("abcbef", "{:x: ({.} {.} {.}) -> 2 :} =x")) - -eqcharset(compile"[]]", "]") -eqcharset(compile"[][]", m.S"[]") -eqcharset(compile"[]-]", m.S"-]") -eqcharset(compile"[-]", m.S"-") -eqcharset(compile"[az-]", m.S"a-z") -eqcharset(compile"[-az]", m.S"a-z") -eqcharset(compile"[a-z]", m.R"az") -eqcharset(compile"[]['\"]", m.S[[]['"]]) - -eqcharset(compile"[^]]", any - "]") -eqcharset(compile"[^][]", any - m.S"[]") -eqcharset(compile"[^]-]", any - m.S"-]") -eqcharset(compile"[^]-]", any - m.S"-]") -eqcharset(compile"[^-]", any - m.S"-") -eqcharset(compile"[^az-]", any - m.S"a-z") -eqcharset(compile"[^-az]", any - m.S"a-z") -eqcharset(compile"[^a-z]", any - m.R"az") -eqcharset(compile"[^]['\"]", any - m.S[[]['"]]) - --- tests for comments in 're' -e = compile[[ -A <- _B -- \t \n %nl .<> <- -> -- -_B <- 'x' --]] -assert(e:match'xy' == 2) - --- tests for 're' with pre-definitions -defs = {digits = m.R"09", letters = m.R"az", _=m.P"__"} -e = compile("%letters (%letters / %digits)*", defs) -assert(e:match"x123" == 5) -e = compile("%_", defs) -assert(e:match"__" == 3) - -e = compile([[ - S <- A+ - A <- %letters+ B - B <- %digits+ -]], defs) - -e = compile("{[0-9]+'.'?[0-9]*} -> sin", math) -assert(e:match("2.34") == math.sin(2.34)) - - -function eq (_, _, a, b) return a == b end - -c = re.compile([[ - longstring <- '[' {:init: '='* :} '[' close - close <- ']' =init ']' / . close -]]) - -assert(c:match'[==[]]===]]]]==]===[]' == 17) -assert(c:match'[[]=]====]=]]]==]===[]' == 14) -assert(not c:match'[[]=]====]=]=]==]===[]') - -c = re.compile" '[' {:init: '='* :} '[' (!(']' =init ']') .)* ']' =init ']' !. " - -assert(c:match'[==[]]===]]]]==]') -assert(c:match'[[]=]====]=][]==]===[]]') -assert(not c:match'[[]=]====]=]=]==]===[]') - -assert(re.find("hi alalo", "{:x:..:} =x") == 4) -assert(re.find("hi alalo", "{:x:..:} =x", 4) == 4) -assert(not re.find("hi alalo", "{:x:..:} =x", 5)) -assert(re.find("hi alalo", "{'al'}", 5) == 6) -assert(re.find("hi aloalolo", "{:x:..:} =x") == 8) -assert(re.find("alo alohi x x", "{:word:%w+:}%W*(=word)!%w") == 11) - --- re.find discards any captures -local a,b,c = re.find("alo", "{.}{'o'}") -assert(a == 2 and b == 3 and c == nil) - -local function match (s,p) - local i,e = re.find(s,p) - if i then return s:sub(i, e) end -end -assert(match("alo alo", '[a-z]+') == "alo") -assert(match("alo alo", '{:x: [a-z]+ :} =x') == nil) -assert(match("alo alo", "{:x: [a-z]+ :} ' ' =x") == "alo alo") - -assert(re.gsub("alo alo", "[abc]", "x") == "xlo xlo") -assert(re.gsub("alo alo", "%w+", ".") == ". .") -assert(re.gsub("hi, how are you", "[aeiou]", string.upper) == - "hI, hOw ArE yOU") - -s = 'hi [[a comment[=]=] ending here]] and [=[another]]=]]' -c = re.compile" '[' {:i: '='* :} '[' (!(']' =i ']') .)* ']' { =i } ']' " -assert(re.gsub(s, c, "%2") == 'hi and =]') -assert(re.gsub(s, c, "%0") == s) -assert(re.gsub('[=[hi]=]', c, "%2") == '=') - -assert(re.find("", "!.") == 1) -assert(re.find("alo", "!.") == 4) - -function addtag (s, i, t, tag) t.tag = tag; return i, t end - -c = re.compile([[ - doc <- block !. - block <- (start {| (block / { [^<]+ })* |} end?) => addtag - start <- '<' {:tag: [a-z]+ :} '>' - end <- '' -]], {addtag = addtag}) - -x = c:match[[ -hihellobuttotheend]] -checkeq(x, {tag='x', 'hi', {tag = 'b', 'hello'}, 'but', - {'totheend'}}) - - --- tests for look-ahead captures -x = {re.match("alo", "&(&{.}) !{'b'} {&(...)} &{..} {...} {!.}")} -checkeq(x, {"", "alo", ""}) - -assert(re.match("aloalo", - "{~ (((&'al' {.}) -> 'A%1' / (&%l {.}) -> '%1%1') / .)* ~}") - == "AallooAalloo") - --- bug in 0.9 (and older versions), due to captures in look-aheads -x = re.compile[[ {~ (&(. ([a-z]* -> '*')) ([a-z]+ -> '+') ' '*)* ~} ]] -assert(x:match"alo alo" == "+ +") - --- valid capture in look-ahead (used inside the look-ahead itself) -x = re.compile[[ - S <- &({:two: .. :} . =two) {[a-z]+} / . S -]] -assert(x:match("hello aloaLo aloalo xuxu") == "aloalo") - - -p = re.compile[[ - block <- {| {:ident:space*:} line - ((=ident !space line) / &(=ident space) block)* |} - line <- {[^%nl]*} %nl - space <- '_' -- should be ' ', but '_' is simpler for editors -]] - -t= p:match[[ -1 -__1.1 -__1.2 -____1.2.1 -____ -2 -__2.1 -]] -checkeq(t, {"1", {"1.1", "1.2", {"1.2.1", "", ident = "____"}, ident = "__"}, - "2", {"2.1", ident = "__"}, ident = ""}) - - --- nested grammars -p = re.compile[[ - s <- a b !. - b <- ( x <- ('b' x)? ) - a <- ( x <- 'a' x? ) -]] - -assert(p:match'aaabbb') -assert(p:match'aaa') -assert(not p:match'bbb') -assert(not p:match'aaabbba') - --- testing groups -t = {re.match("abc", "{:S <- {:.:} {S} / '':}")} -checkeq(t, {"a", "bc", "b", "c", "c", ""}) - -t = re.match("1234", "{| {:a:.:} {:b:.:} {:c:.{.}:} |}") -checkeq(t, {a="1", b="2", c="4"}) -t = re.match("1234", "{|{:a:.:} {:b:{.}{.}:} {:c:{.}:}|}") -checkeq(t, {a="1", b="2", c="4"}) -t = re.match("12345", "{| {:.:} {:b:{.}{.}:} {:{.}{.}:} |}") -checkeq(t, {"1", b="2", "4", "5"}) -t = re.match("12345", "{| {:.:} {:{:b:{.}{.}:}:} {:{.}{.}:} |}") -checkeq(t, {"1", "23", "4", "5"}) -t = re.match("12345", "{| {:.:} {{:b:{.}{.}:}} {:{.}{.}:} |}") -checkeq(t, {"1", "23", "4", "5"}) - - --- testing pre-defined names -assert(os.setlocale("C") == "C") - -function eqlpeggsub (p1, p2) - local s1 = cs2str(re.compile(p1)) - local s2 = string.gsub(allchar, "[^" .. p2 .. "]", "") - -- if s1 ~= s2 then print(#s1,#s2) end - assert(s1 == s2) -end - - -eqlpeggsub("%w", "%w") -eqlpeggsub("%a", "%a") -eqlpeggsub("%l", "%l") -eqlpeggsub("%u", "%u") -eqlpeggsub("%p", "%p") -eqlpeggsub("%d", "%d") -eqlpeggsub("%x", "%x") -eqlpeggsub("%s", "%s") -eqlpeggsub("%c", "%c") - -eqlpeggsub("%W", "%W") -eqlpeggsub("%A", "%A") -eqlpeggsub("%L", "%L") -eqlpeggsub("%U", "%U") -eqlpeggsub("%P", "%P") -eqlpeggsub("%D", "%D") -eqlpeggsub("%X", "%X") -eqlpeggsub("%S", "%S") -eqlpeggsub("%C", "%C") - -eqlpeggsub("[%w]", "%w") -eqlpeggsub("[_%w]", "_%w") -eqlpeggsub("[^%w]", "%W") -eqlpeggsub("[%W%S]", "%W%S") - -re.updatelocale() - - --- testing nested substitutions x string captures - -p = re.compile[[ - text <- {~ item* ~} - item <- macro / [^()] / '(' item* ')' - arg <- ' '* {~ (!',' item)* ~} - args <- '(' arg (',' arg)* ')' - macro <- ('apply' args) -> '%1(%2)' - / ('add' args) -> '%1 + %2' - / ('mul' args) -> '%1 * %2' -]] - -assert(p:match"add(mul(a,b), apply(f,x))" == "a * b + f(x)") - -rev = re.compile[[ R <- (!.) -> '' / ({.} R) -> '%2%1']] - -assert(rev:match"0123456789" == "9876543210") - - --- testing error messages in re - -local function errmsg (p, err) - checkerr(err, re.compile, p) -end - -errmsg('aaaa', "rule 'aaaa'") -errmsg('a', 'outside') -errmsg('b <- a', 'undefined') -errmsg("x <- 'a' x <- 'b'", 'already defined') --- error message is different because Matthew rewrote 're.lua' ---errmsg("'a' -", "near '-'") - - -print"OK" - - diff --git a/06/deps/lpeglabel/testlabel.lua b/06/deps/lpeglabel/testlabel.lua deleted file mode 100644 index e2833ea..0000000 --- a/06/deps/lpeglabel/testlabel.lua +++ /dev/null @@ -1,1054 +0,0 @@ -local m = require 'lpeglabel' - -local p, r, l, s, serror - -local function checklabeq (x, ...) - y = { ... } - assert(type(x) == "table") - assert(#x == #y) - for i = 1, 3 do - assert(x[i] == y[i]) - end -end - -local function checkeq (x, y, p) - if p then print(x,y) end - if type(x) ~= "table" then assert(x == y) - else - for k,v in pairs(x) do checkeq(v, y[k], p) end - for k,v in pairs(y) do checkeq(v, x[k], p) end - end -end - - --- tests related to reporting the farthest failure position --- when a label is not thrown - -p = m.P"a"^0 * m.P"b" + m.P"c" -checklabeq({4, nil, nil}, p:match("aabk")) -checklabeq({2, nil, nil}, p:match("ck")) -checklabeq({nil, 0, "dk"}, p:match("dk")) -checklabeq({nil, 0, "k"}, p:match("aak")) - -p = (m.P"a" + m.P"c")^0 * m.P"b" + m.P"c" -checklabeq({4, nil, nil}, p:match("aabk")) -checklabeq({2, nil, nil}, p:match("ck")) -checklabeq({nil, 0, "dk"}, p:match("dk")) -checklabeq({nil, 0, "k"}, p:match("aak")) - -p = m.P"a"^0 * m.P"b" + m.P(1)^0 * m.P(1) -checklabeq({4, nil, nil}, p:match("aabk")) -checklabeq({nil, 0, ""}, p:match("ck")) -checklabeq({nil, 0, ""}, p:match("aak")) - -p = m.P(1) * m.P"a" + m.P"c" -checklabeq({3, nil, nil}, p:match("bac")) -checklabeq({2, nil, nil}, p:match("c")) -checklabeq({nil, 0, ""}, p:match("x")) -checklabeq({nil, 0, "x"}, p:match("kx")) - -p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" -checklabeq({5, nil, nil}, p:match("aabc")) -checklabeq({4, nil, nil}, p:match("aac")) -checklabeq({nil, 0, ""}, p:match("aak")) -checklabeq({nil, 0, ""}, p:match("x")) - -p = m.P"a"^0 * m.P(1) * m.P(1) + m.P"a"^0 * m.P"c" -checklabeq({5, nil, nil}, p:match("aabc")) -checklabeq({4, nil, nil}, p:match("aac")) -checklabeq({nil, 0, ""}, p:match("aak")) -checklabeq({nil, 0, ""}, p:match("x")) - -p = m.Cmt(m.P"a"^0, function() return nil end) + m.P"x" -checklabeq({2, nil, nil}, p:match("xabc")) -checklabeq({nil, 0, "c"}, p:match("aac")) -checklabeq({nil, 0, "kx"}, p:match("kx")) - -p = m.P"b" * -m.P"a" + m.P"c" -checklabeq({nil, 0, "a"}, p:match("ba")) -checklabeq({nil, 0, "kx"}, p:match("kx")) - -p = (m.P"c" + m.P"a") * m.P("b" + m.P"d") + m.P"xxx" -checklabeq({nil, 0, "kk"}, p:match("kk")) -checklabeq({nil, 0, "k"}, p:match("ak")) -checklabeq({nil, 0, "y"}, p:match("xxy")) -checklabeq({nil, 0, "yz"}, p:match("xyz")) - -print"+" - - --- throws a label -p = m.T(1) -s = "abc" -r, l, serror = p:match(s) -assert(r == nil and l == 1 and serror == "abc") - --- throws a label, choice does not catch labels -p = m.T(1) + m.P"a" -r, l, serror = p:match(s) -assert(r == nil and l == 1 and serror == "abc") - --- again throws a label that is not caught by choice -local g = m.P{ - "S", - S = m.V"A" + m.V"B", - A = m.T(1), - B = m.P"a" -} -r, l, serror = g:match(s) -assert(r == nil and l == 1 and serror == "abc") - --- throws a label in a position that is not the farthest one --- but it is the position that should be reported -p = m.P(1) * m.P"a" + m.T(11) -checklabeq({3, nil, nil}, p:match("bac")) -checklabeq({nil, 11, "c"}, p:match("c")) -checklabeq({nil, 11, "x"}, p:match("x")) -checklabeq({nil, 11, "kx"}, p:match("kx")) - - --- throws a label that is not caught by the recovery operator -p = m.Rec(m.T(2), m.P"a", 1, 3) -r, l, serror = p:match(s) -assert(r == nil and l == 2 and serror == "abc") - --- wraps the previous pattern with a recovery that catches label "2" -p = m.Rec(p, m.P"a", 2) -assert(p:match(s) == 2) - --- throws a label that is caught by recovery -p = m.Rec(m.T(25), m.P"a", 25) -assert(p:match(s) == 2) - --- "fail" is label "0" --- throws the "fail" label after the recovery -s = "bola" -r, l, serror = p:match("bola") -assert(r == nil and l == 0 and serror == "bola") - --- Recovery does not catch "fail" by default -p = m.Rec(m.P"b", m.P"a", 1) - -r, l, serror = p:match("abc") -assert(r == nil and l == 0 and serror == "abc") - -assert(p:match("bola") == 2) - - --- recovery operator catches "1" or "3" -p = m.Rec((m.P"a" + m.T(1)) * m.T(3), (m.P"a" + m.P"b"), 1, 3) -assert(p:match("aac") == 3) -assert(p:match("abc") == 3) -r, l, serror = p:match("acc") -assert(r == nil and l == 0 and serror == "cc") - ---throws 1, recovery pattern matches 'b', throw 3, and rec pat mathces 'a' -assert(p:match("bac") == 3) - -r, l, serror = p:match("cab") -assert(r == nil and l == 0 and serror == "cab") - - --- associativity --- (p1 / %1) //{1} (p2 / %2) //{2} p3 --- left-associativity --- ("a" //{1} "b") //{2} "c" -p = m.Rec(m.Rec(m.P"a" + m.T(1), m.P"b" + m.T(2), 1), m.P"c", 2) -assert(p:match("abc") == 2) -assert(p:match("bac") == 2) -assert(p:match("cab") == 2) -r, l, serror = p:match("dab") -assert(r == nil and l == 0 and serror == "dab") - - --- righ-associativity --- "a" //{1} ("b" //{2} "c") -p = m.Rec(m.P"a" + m.T(1), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1) -assert(p:match("abc") == 2) -assert(p:match("bac") == 2) -assert(p:match("cab") == 2) -r, l, serror = p:match("dab") -assert(r == nil and l == 0 and serror == "dab") - - --- associativity -> in this case the error thrown by p1 is only --- recovered when we have a left-associative operator --- (p1 / %2) //{1} (p2 / %2) //{2} p3 --- left-associativity --- ("a" //{1} "b") //{2} "c" -p = m.Rec(m.Rec(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) -assert(p:match("abc") == 2) -r, l, serror = p:match("bac") -assert(r == nil and l == 0 and serror == "bac") -assert(p:match("cab") == 2) -r, l, serror = p:match("dab") -assert(r == nil and l == 0 and serror == "dab") - - --- righ-associativity --- "a" //{1} ("b" //{2} "c") -p = m.Rec(m.P"a" + m.T(2), m.Rec(m.P"b" + m.T(2), m.P"c", 2), 1) -assert(p:match("abc") == 2) -r, l, serror = p:match("bac") -assert(r == nil and l == 2 and serror == "bac") -r, l, serror = p:match("cab") -assert(r == nil and l == 2 and serror == "cab") -r, l, serror = p:match("dab") -assert(r == nil and l == 2 and serror == "dab") - - - --- tests related to predicates -p = #m.T(1) + m.P"a" -r, l, serror = p:match("abc") -assert(r == nil and l == 1 and serror == "abc") - -p = ##m.T(1) + m.P"a" -r, l, serror = p:match("abc") -assert(r == nil and l == 1 and serror == "abc") - -p = -m.T(1) * m.P"a" -r, l, serror = p:match("abc") -assert(r == nil and l == 1 and serror == "abc") - -p = -m.T(1) * m.P"a" -r, l, serror = p:match("bbc") -assert(r == nil and l == 1 and serror == "bbc") - -p = -(-m.T(1)) * m.P"a" -r, l, serror = p:match("abc") -assert(r == nil and l == 1 and serror == "abc") - -p = m.Rec(-m.T(22), m.P"a", 22) -r, l, serror = p:match("abc") -assert(r == nil and l == 0 and serror == "bc") - -assert(p:match("bbc") == 1) - -p = m.Rec(#m.T(22), m.P"a", 22) -assert(p:match("abc") == 1) - -p = #m.Rec(m.T(22), m.P"a", 22) -assert(p:match("abc") == 1) - -p = m.Rec(m.T(22), #m.P"a", 22) -assert(p:match("abc") == 1) - -p = m.Rec(#m.T(22), m.P"a", 22) -r, l, serror = p:match("bbc") -assert(r == nil and l == 0 and serror == "bbc") - -p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22) -r, l, serror = p:match("abc") -assert(r == nil and l == 15 and serror == "abc") - -p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22) -r, l, serror = p:match("abc") -assert(r == nil and l == 15 and serror == "bc") - - - --- tests related to repetition -p = m.T(1)^0 -r, l, serror = p:match("ab") -assert(r == nil and l == 1 and serror == "ab") - -p = (m.P"a" + m.T(1))^0 -r, l, serror = p:match("aa") -assert(r == nil and l == 1 and serror == "") - - --- Bug reported by Matthew Allen --- some optmizations performed by LPeg should not be --- applied in case of labeled choices -p = m.Rec(m.P"A", m.P(true), 1) + m.P("B") -assert(p:match("B") == 2) - -p = m.Rec(m.P"A", m.P(false), 1) + m.P("B") -assert(p:match("B") == 2) - - ---[[ -S -> A //{1} 'a' -A -> B -B -> %1 -]] -g = m.P{ - "S", - S = m.Rec(m.V"A", m.P"a", 1), - A = m.V"B", - B = m.T(1), -} -assert(g:match("ab") == 2) -r, l, serror = g:match("bc") -assert(r == nil and l == 0 and serror == "bc") - - ---[[ -S -> A -A -> (B (';' / %{1}))* -B -> 'a' -]] -g = m.P{ - "S", - S = m.V"A", - A = m.P(m.V"B" * (";" + m.T(1)))^0, - B = m.P'a', -} -assert(g:match("a;a;") == 5) - -r, l, serror = g:match("a;a") -assert(r == nil and l == 1 and serror == "") - - --- %1 //{1,3} %2 //{2} 'a' -p = m.Rec(m.Rec(m.T(1), m.T(2), 1, 3), m.P"a", 2) -assert(p:match("abc") == 2) - -r, l, serror = p:match("") -assert(r == nil and l == 0 and serror == "") - -p = m.Rec(m.T(1), m.Rec(m.T(2), m.P"a", 2), 1, 3) -assert(p:match("abc") == 2) - -r, l, serror = p:match("") -assert(r == nil and l == 0 and serror == "") - - --- Infinte Loop TODO: check the semantics --- %1 //{1} %1 -p = m.Rec(m.T(1), m.T(1), 1) ---r, l, serror = p:match("ab") ---assert(r == nil and l == 1 and serror == "ab") - --- %1 //{1} 'a' (!. / %1) -p = m.Rec(m.T(1), m.P"a" * (-m.P(1) + m.T(1)), 1) -r, l, serror = p:match("ab") -assert(r == nil and l == 0 and serror == "b") - -r, l, serror = p:match("cd") -assert(r == nil and l == 0 and serror == "cd") - --- %1 //{1} . (!. / %1) -p = m.Rec(m.T(1), m.P(1) * (-m.P(1) + m.T(1)), 1) -assert(p:match("abc") == 4) - - --- testing the limit of labels --- can only throw labels between 1 and 255 -local r = pcall(m.Rec, m.P"b", m.P"a", 0) -assert(r == false) - -local r = pcall(m.Rec, m.P"b", m.P"a", 256) -assert(r == false) - -local r = pcall(m.Rec, m.P"b", m.P"a", -1) -assert(r == false) - -local r = pcall(m.T, 0) -assert(r == false) - -local r = pcall(m.T, 256) -assert(r == false) - -local r = pcall(m.T, -1) -assert(r == false) - - -local r = m.Rec(m.P"b", m.P"a", 255) -assert(p:match("a") == 2) - -p = m.T(255) -s = "abc" -r, l, serror = p:match(s) -assert(r == nil and l == 255 and serror == "abc") - - - -print("+") - ---[[ grammar based on Figure 8 of paper submitted to SCP -S -> S0 //{1} ID //{2} ID '=' Exp //{3} 'unsigned'* 'int' ID //{4} 'unsigned'* ID ID / %error -S0 -> S1 / S2 / &'int' %3 -S1 -> &(ID '=') %2 / &(ID !.) %1 / &ID %4 -S2 -> &('unsigned'+ ID) %4 / & ('unsigned'+ 'int') %3 -]] -local sp = m.S" \t\n"^0 -local eq = sp * m.P"=" - -g = m.P{ - "S", - S = m.Rec( - m.Rec( - m.Rec( - m.Rec(m.V"S0", m.V"ID", 1), - m.V"ID" * eq * m.V"Exp", 2 - ), - m.V"U"^0 * m.V"I" * m.V"ID", 3 - ), - m.V"U"^0 * m.V"ID" * m.V"ID", 4) - + m.T(5), -- error - S0 = m.V"S1" + m.V"S2" + #m.V"I" * m.T(3), - S1 = #(m.V"ID" * eq) * m.T(2) + sp * #(m.V"ID" * -m.P(1)) * m.T(1) + #m.V"ID" * m.T(4), - S2 = #(m.V"U"^1 * m.V"ID") * m.T(4) + #(m.V"U"^1 * m.V"I") * m.T(3), - ID = sp * m.P"a", - U = sp * m.P"unsigned", - I = sp * m.P"int", - Exp = sp * m.P"E", -} - -local s = "a" -assert(g:match(s) == #s + 1) --1 -s = "a = E" -assert(g:match(s) == #s + 1) --2 -s = "int a" -assert(g:match(s) == #s + 1) --3 -s = "unsigned int a" -assert(g:match(s) == #s + 1) --3 -s = "unsigned a a" -assert(g:match(s) == #s + 1) --4 - -s = "b" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == "b") - -s = "unsigned" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) - -s = "unsigned a" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) - -s = "unsigned int" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) - - -print("+") - - -local re = require 'relabel' - -g = re.compile[['a' //{4,9} [a-z] -]] -assert(g:match("a") == 2) -r, l, serror = g:match("b") -assert(r == nil and l == 0 and serror == "b") - -g = re.compile[['a' //{4,9} [a-f] //{5, 7} [a-z] -]] -assert(g:match("a") == 2) -r, l, serror = g:match("b") -assert(r == nil and l == 0 and serror == "b") - -g = re.compile[[%{1} //{4,9} [a-z] -]] -r, l, serror = g:match("a") -assert(r == nil and l == 1 and serror == "a") - - -g = re.compile[[%{1} //{4,1} [a-f] -]] -assert(g:match("a") == 2) -r, l, serror = g:match("h") -assert(r == nil and l == 0 and serror == "h") - -g = re.compile[[[a-f]%{9} //{4,9} [a-c]%{7} //{5, 7} [a-z] ]] -r, l, serror = g:match("a") -assert(r == nil and l == 0 and serror == "") -r, l, serror = g:match("aa") -assert(r == nil and l == 0 and serror == "") -assert(g:match("aaa") == 4) - -r, l, serror = g:match("ad") -assert(r == nil and l == 0 and serror == "d") - -r, l, serror = g:match("g") -assert(r == nil and l == 0 and serror == "g") - - ---[[ grammar based on Figure 8 of paper submitted to SCP -S -> S0 //{1} ID //{2} ID '=' Exp //{3} 'unsigned'* 'int' ID //{4} 'unsigned'* ID ID / %error -S0 -> S1 / S2 / &'int' %3 -S1 -> &(ID '=') %2 / &(ID !.) %1 / &ID %4 -S2 -> &('unsigned'+ ID) %4 / & ('unsigned'+ 'int') %3 -]] - -g = re.compile([[ - S <- S0 //{1} ID //{2} ID %s* '=' Exp //{3} U* Int ID //{4} U ID ID / %{5} - S0 <- S1 / S2 / &Int %{3} - S1 <- &(ID %s* '=') %{2} / &(ID !.) %{1} / &ID %{4} - S2 <- &(U+ ID) %{4} / &(U+ Int) %{3} - ID <- %s* 'a' - U <- %s* 'unsigned' - Int <- %s* 'int' - Exp <- %s* 'E' -]]) - -local s = "a" -assert(g:match(s) == #s + 1) --1 -s = "a = E" -assert(g:match(s) == #s + 1) --2 -s = "int a" -assert(g:match(s) == #s + 1) --3 -s = "unsigned int a" -assert(g:match(s) == #s + 1) --3 -s = "unsigned a a" -assert(g:match(s) == #s + 1) --4 -s = "b" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) -s = "unsigned" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) -s = "unsigned a" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) -s = "unsigned int" -r, l, serror = g:match(s) -assert(r == nil and l == 5 and serror == s) - - - -local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq", - ['ifExp'] = "Error in expresion of 'if'", - ['ifThen'] = "Error matching 'then' keyword", - ['ifThenCmdSeq'] = "Error matching CmdSeq of 'then' branch", - ['ifElseCmdSeq'] = "Error matching CmdSeq of 'else' branch", - ['ifEnd'] = "Error matching 'end' keyword of 'if'", - ['repeatCmdSeq'] = "Error matching CmdSeq of 'repeat'", - ['repeatUntil'] = "Error matching 'until' keyword", - ['repeatExp'] = "Error matching expression of 'until'", - ['assignOp'] = "Error matching ':='", - ['assignExp'] = "Error matching expression of assignment", - ['readName'] = "Error matching 'NAME' after 'read'", - ['writeExp'] = "Error matching expression after 'write'", - ['simpleExp'] = "Error matching 'SimpleExp'", - ['term'] = "Error matching 'Term'", - ['factor'] = "Error matching 'Factor'", - ['openParExp'] = "Error matching expression after '('", - ['closePar'] = "Error matching ')'", - ['undefined'] = "Undefined Error"} - -g = re.compile([[ - Tiny <- CmdSeq //{1} '' -> cmdSeq //{2} '' -> ifExp //{3} '' -> ifThen //{4} '' -> ifThenCmdSeq - //{5} '' -> ifElseCmdSeq //{6} '' -> ifEnd //{7} '' -> repeatCmdSeq - //{8} '' -> repeatUntil //{9} '' -> repeatExp //{10} '' -> assignOp - //{11} '' -> assignExp //{12} '' -> readName //{13} '' -> writeExp - //{14} '' -> simpleExp //{15} '' -> term //{16} '' -> factor - //{17} '' -> openParExp //{18} '' -> closePar / '' -> undefined - CmdSeq <- (Cmd (SEMICOLON / %{1})) (Cmd (SEMICOLON / %{1}))* - Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd - IfCmd <- IF (Exp / %{2}) (THEN / %{3}) (CmdSeq / %{4}) (ELSE (CmdSeq / %{5}) / '') (END / %{6}) - RepeatCmd <- REPEAT (CmdSeq / %{7}) (UNTIL / %{8}) (Exp / %{9}) - AssignCmd <- NAME (ASSIGNMENT / %{10}) (Exp / %{11}) - ReadCmd <- READ (NAME / %{12}) - WriteCmd <- WRITE (Exp / %{13}) - Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / %{14}) / '') - SimpleExp <- Term ((ADD / SUB) (Term / %{15}))* - Term <- Factor ((MUL / DIV) (Factor / %{16}))* - Factor <- OPENPAR (Exp / %{17}) (CLOSEPAR / %{18}) / NUMBER / NAME - ADD <- Sp '+' - ASSIGNMENT <- Sp ':=' - CLOSEPAR <- Sp ')' - DIV <- Sp '/' - IF <- Sp 'if' - ELSE <- Sp 'else' - END <- Sp 'end' - EQUAL <- Sp '=' - LESS <- Sp '<' - MUL <- Sp '*' - NAME <- !RESERVED Sp [a-z]+ - NUMBER <- Sp [0-9]+ - OPENPAR <- Sp '(' - READ <- Sp 'read' - REPEAT <- Sp 'repeat' - SEMICOLON <- Sp ';' - SUB <- Sp '-' - THEN <- Sp 'then' - UNTIL <- Sp 'until' - WRITE <- Sp 'write' - RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+ - Sp <- (%s / %nl)* -]], terror) - -s = [[ -n := 5;]] -assert(g:match(s) == #s + 1) - -s = [[ -n := 5; -f := 1; -repeat - f := f * n; - n := n - 1; -until (n < 1); -write f;]] -assert(g:match(s) == #s + 1) - --- a ';' is missing in 'read a' -s = [[ -read a]] -assert(g:match(s) == terror['cmdSeq']) - - --- a ';' is missing in 'n := n - 1' -s = [[ -n := 5; -f := 1; -repeat - f := f * n; - n := n - 1 -until (n < 1); -write f;]] -assert(g:match(s) == terror['cmdSeq']) - - --- IF expression -s = [[ -if a then a := a + 1; end;]] -assert(g:match(s) == #s + 1) - --- IF expression -s = [[ -if a then a := a + 1; else write 2; end;]] -assert(g:match(s) == #s + 1) - --- Error in expression of 'if'. 'A' is not a valida name -s = [[ -if A then a := a + 1; else write 2; end;]] -assert(g:match(s) == terror['ifExp']) - --- Error matching the 'then' keyword -s = [[ -if a a := a + 1; else write 2; end;]] -assert(g:match(s) == terror['ifThen']) - --- Error matching the CmdSeq inside of 'then' branch -s = [[ -if a then 3 := 2; else write 2; end;]] -assert(g:match(s) == terror['ifThenCmdSeq']) - --- Error matching the CmdSeq inside of 'else' branch -s = [[ -if a then b := 2; else A := 2; end;]] -assert(g:match(s) == terror['ifElseCmdSeq']) - --- Error matching 'end' of 'if' -s = [[ -if a then b := 2; else a := 2; 77;]] -assert(g:match(s) == terror['ifEnd']) - --- Error matching the CmdSeq of 'repeat' -s = [[repeat - F := f * n; - n := n - 1; -until (n < 1);]] -assert(g:match(s) == terror['repeatCmdSeq']) - --- Error matching 'until' -s = [[repeat - f := f * n; - n := n - 1; -88 (n < 1);]] -assert(g:match(s) == terror['repeatUntil']) - --- Error matching expression of 'until' -s = [[repeat - f := f * n; - n := n - 1; -until ; (n < 1);]] -assert(g:match(s) == terror['repeatExp']) - --- Error matching ':=' -s = [[ -f = f * n;]] -assert(g:match(s) == terror['assignOp']) - --- Error matching expression of assignment -s = [[ -f := A * n;]] -assert(g:match(s) == terror['assignExp']) - --- Error matching 'name' -s = [[ -read 2;]] -assert(g:match(s) == terror['readName']) - --- Error matching expression after 'write' -s = [[ -write [a] := 2;]] -assert(g:match(s) == terror['writeExp']) - --- Error matching 'SimpleExp' -s = [[ -a := a < A;]] -assert(g:match(s) == terror['simpleExp']) - --- Error matching 'Term' -s = [[ -a := a + A;]] -assert(g:match(s) == terror['term']) - --- Error matching 'Factor' -s = [[ -a := a * A;]] -assert(g:match(s) == terror['factor']) - --- Error matching expression after '(' -s = [[ -a := (A);]] -assert(g:match(s) == terror['openParExp']) - --- Error matching ')' -s = [[ -a := (a];]] -assert(g:match(s) == terror['closePar']) - --- Error undefined -s = [[ -A := a;]] -assert(g:match(s) == terror['undefined']) - - -print("+") - - -p = m.Rec("a", "b", 3) -assert(p:match("a") == 2) -checklabeq({nil, 0, "b"}, p:match("b")) -checklabeq({nil, 0, "c"}, p:match("c")) - -p = m.Rec(m.T(3), "b", 1) -checklabeq({nil, 3, "a"}, p:match("a")) -checklabeq({nil, 3, "b"}, p:match("b")) - -p = m.Rec(m.T(3), "b", 3) -checklabeq({nil, 0, "a"}, p:match("a")) -assert(p:match("b") == 2) - ---[[ -S -> (A //{128} (!c .)*) C -A -> a*b / %128 -C -> c+ -]] -g = m.P{ - "S", - S = m.Rec(m.V"A", (-m.P"c" * m.P(1))^0, 128) * m.V"C", - A = m.P"a"^0 * "b" + m.T(128), - C = m.P"c"^1, -} - -assert(g:match("abc") == 4) -assert(g:match("aabc") == 5) -assert(g:match("aadc") == 5) -assert(g:match("dc") == 3) -checklabeq({nil, 0, "bc"}, g:match("bbc")) -assert(g:match("xxc") == 4) -assert(g:match("c") == 2) -checklabeq({nil, 0, ""}, g:match("fail")) -checklabeq({nil, 0, ""}, g:match("aaxx")) - - ---[[ -S -> (A //{99} (!c .)*) C -A -> a+ (b / ^99) -C -> c+ -]] -g = m.P{ - "S", - S = m.Rec(m.V"A", (-m.P"c" * m.P(1))^0, 99) * m.V"C", - A = m.P"a"^1 * ("b" + m.T(99)), - C = m.P"c"^1, -} - -assert(g:match("abc") == 4) -assert(g:match("aabc") == 5) -assert(g:match("aadc") == 5) -checklabeq({nil, 0, "bc"}, g:match("bc")) -checklabeq({nil, 0, "bbc"}, g:match("bbc")) -checklabeq({nil, 0, "b"}, g:match("abb")) -checklabeq({nil, 0, ""}, g:match("axx")) -assert(g:match("accc") == 5) -assert(g:match("axxc") == 5) -checklabeq({nil, 0, "c"}, g:match("c")) -checklabeq({nil, 0, "fail"}, g:match("fail")) - - - --- Matthew's recovery example -lpeg = m - -local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V -local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt -local T, Lc = lpeg.T, lpeg.Lc - -local labels = { - {"NoExp", "no expression found"}, - {"Extra", "extra characters found after the expression"}, - {"ExpTerm", "expected a term after the operator"}, - {"ExpExp", "expected an expression after the parenthesis"}, - {"MisClose", "missing a closing ')' after the expression"}, -} - -local function labelindex(labname) - for i, elem in ipairs(labels) do - if elem[1] == labname then - return i - end - end - error("could not find label: " .. labname) -end - -local errors = {} - -local function expect(patt, labname) - local i = labelindex(labname) - function recorderror(input, pos) - table.insert(errors, {i, pos}) - return true - end - return patt + Cmt("", recorderror) * T(i) -end - -local num = R("09")^1 / tonumber -local op = S("+-*/") - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - elseif tokens[i] == '*' then - result = result * tokens[i+1] - elseif tokens[i] == '/' then - result = result / tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - -local g = P { - "Exp", - Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; - OpRecov = m.Rec(V"Operand", Cc(0), labelindex("ExpTerm")); - Operand = expect(V"Term", "ExpTerm"); - Term = num + V"Group"; - Group = "(" * V"InnerExp" * m.Rec(expect(")", "MisClose"), P"", labelindex("MisClose")); - InnerExp = m.Rec(expect(V"Exp", "ExpExp"), (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")); -} - -g = expect(g, "NoExp") * expect(-P(1), "Extra") - -local function eval(input) - local result, label, suffix = g:match(input) - if #errors == 0 then - return result - else - local out = {} - for i, err in ipairs(errors) do - local pos = err[2] - local msg = labels[err[1]][2] - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - errors = {} - return nil, table.concat(out, "\n") - end -end - -print(eval "98-76*(54/32)") ---> 37.125 - -print(eval "(1+1-1*2/2") ---> syntax error: missing a closing ')' after the expression (at index 11) - -print(eval "(1+)-1*(2/2)") ---> syntax error: expected a term after the operator (at index 4) - -print(eval "(1+1)-1*(/2)") ---> syntax error: expected an expression after the parenthesis (at index 10) - -print(eval "1+(1-(1*2))/2x") ---> syntax error: extra chracters found after the expression (at index 14) - -print(eval "-1+(1-(1*2))/2") ---> syntax error: no expression found (at index 1) - -print(eval "(1+1-1*(2/2+)-():") ---> syntax error: expected a term after the operator (at index 13) ---> syntax error: expected an expression after the parenthesis (at index 16) ---> syntax error: missing a closing ')' after the expression (at index 17) ---> syntax error: extra characters found after the expression (at index - - -print("+") - -local g = m.P{ - "S", - S = V"End" + V'A' * V'S', - A = P'a' + T(1), - End = P"." * (-P(1) + T(2)), -} - -assert(g:match("a.") == 3) -assert(g:match("aa.") == 4) -assert(g:match(".") == 2) -checklabeq({nil, 1, "ba."}, g:match("ba.")) -checklabeq({nil, 1, "ba."}, g:match("aba.")) -checklabeq({nil, 1, "cba."}, g:match("cba.")) -checklabeq({nil, 2, "a"}, g:match("a.a")) - - -local g2 = m.P{ - "S", - S = m.Rec(g, V"B", 1), - B = P'b'^1 + T(3) -} - -assert(g2:match("a.") == 3) -assert(g2:match("aa.") == 4) -assert(g2:match(".") == 2) -assert(g2:match("ba.") == 4) -assert(g2:match("aba.") == 5) -checklabeq({nil, 3, "cba."}, g2:match("cba.")) -checklabeq({nil, 2, "a"}, g2:match("a.a")) - -local g3 = m.P{ - "S", - S = m.Rec(g2, V"C", 2, 3), - C = P'c'^1 + T(4) -} - -assert(g3:match("a.") == 3) -assert(g3:match("aa.") == 4) -assert(g3:match(".") == 2) -assert(g3:match("ba.") == 4) -assert(g3:match("aba.") == 5) -assert(g3:match("cba.") == 5) -checklabeq({nil, 4, "a"}, g3:match("a.a")) -checklabeq({nil, 4, "dc"}, g3:match("dc")) -checklabeq({nil, 4, "d"}, g3:match(".d")) - - --- testing more captures -local g = re.compile[[ - S <- ( %s* &. {A} )* - A <- [0-9]+ / %{5} -]] - -checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) -checkeq({nil, 5, "a 123"}, {g:match("44 a 123")}) - -local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) - -checkeq({"523", "624", "346", "888"} , {g2:match("523 624 346\n888")}) -checkeq({"44", "a ", "58", "123"}, {g2:match("44 a 123")}) - - -local g = re.compile[[ - S <- ( %s* &. A )* - A <- {[0-9]+} / %{5} -]] - -checkeq({"523", "624", "346", "888"} , {g:match("523 624 346\n888")}) -checkeq({nil, 5, "a 123"}, {g:match("44 a 123")}) - -local g2 = m.Rec(g, ((-m.R("09") * m.P(1))^0) / "58", 5) - -checkeq({"523", "624", "346", "888"} , {g2:match("523 624 346\n888")}) -checkeq({"44", "58", "123"}, {g2:match("44 a 123")}) - - -local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V -local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt -local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec - -local labels = { - {"NoExp", "no expression found"}, - {"Extra", "extra characters found after the expression"}, - {"ExpTerm", "expected a term after the operator"}, - {"ExpExp", "expected an expression after the parenthesis"}, - {"MisClose", "missing a closing ')' after the expression"}, -} - -local function labelindex(labname) - for i, elem in ipairs(labels) do - if elem[1] == labname then - return i - end - end - error("could not find label: " .. labname) -end - -local errors = {} - -local function expect(patt, labname, recpatt) - local i = labelindex(labname) - function recorderror(input, pos) - table.insert(errors, {i, pos}) - return true - end - if not recpatt then recpatt = P"" end - --return Rec(patt, Cmt("", recorderror) * recpatt) - return patt + T(i) -end - -local num = R("09")^1 / tonumber -local op = S("+-*/") - -local function compute(tokens) - local result = tokens[1] - for i = 2, #tokens, 2 do - if tokens[i] == '+' then - result = result + tokens[i+1] - elseif tokens[i] == '-' then - result = result - tokens[i+1] - elseif tokens[i] == '*' then - result = result * tokens[i+1] - elseif tokens[i] == '/' then - result = result / tokens[i+1] - else - error('unknown operation: ' .. tokens[i]) - end - end - return result -end - - -local g = P { -"Exp", -Exp = Ct(V"Term" * (C(op) * V"Operand")^0) / compute, -Operand = expect(V"Term", "ExpTerm"), -Term = num, -} -local rg = Rec(g, Cc(3), labelindex("ExpTerm")) - -local function eval(input) - local result, label, suffix = rg:match(input) - if #errors == 0 then - return result - else - local out = {} - for i, err in ipairs(errors) do - local pos = err[2] - local msg = labels[err[1]][2] - table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") - end - errors = {} - return nil, table.concat(out, "\n") - end -end - -assert(eval("98-76*54/32") == 37.125) ---> 37.125 - -assert(eval("1+") == 4) ---> syntax error: expected a term after the operator (at index 3) - - -print("OK") diff --git a/06/deps/lpeglabel/testrelabelparser.lua b/06/deps/lpeglabel/testrelabelparser.lua deleted file mode 100644 index 5a43621..0000000 --- a/06/deps/lpeglabel/testrelabelparser.lua +++ /dev/null @@ -1,549 +0,0 @@ -local re = require 'relabel' - -function testerror(repatt, msg) - msg = msg:match("^%s*(.-)%s*$") -- trim - local ok, err = pcall(function () re.compile(repatt) end) - assert(not ok) - if msg:match("^[^\n]*\n(.-)$") then - -- expecting a syntax error - err = err:match("^[^\n]*\n(.-)$") -- remove first line (filename) - err = err:gsub("[ \t]*\n", "\n") -- remove trailing spaces - -- if err ~= msg then - -- print(#err, #msg) - -- print('--') - -- print(err) - -- print('--') - -- print(msg) - -- print('--') - -- end - assert(err == msg) - else - -- expecting a non-syntax error - assert(err:match(msg)) - end -end - --- testing NoPatt - -testerror([[~]], [[ -L1:C1: no pattern found -~ -^ -]]) - -testerror([[???]], [[ -L1:C1: no pattern found -??? -^ -]]) - --- testing ExtraChars - -testerror([['p'~]], [[ -L1:C4: unexpected characters after the pattern -'p'~ - ^ -]]) - -testerror([['p'?$?]], [[ -L1:C5: unexpected characters after the pattern -'p'?$? - ^ -]]) - --- testing ExpPatt1 - -testerror([['p' //{1}]], [[ -L1:C10: expected a pattern after '/' or '//{...}' -'p' //{1} - ^ -]]) - -testerror([['p' //{1} //{2} 'q']], [[ -L1:C10: expected a pattern after '/' or '//{...}' -'p' //{1} //{2} 'q' - ^ -]]) - -testerror([['p' /]], [[ -L1:C6: expected a pattern after '/' or '//{...}' -'p' / - ^ -]]) - -testerror([['p' / / 'q']], [[ -L1:C6: expected a pattern after '/' or '//{...}' -'p' / / 'q' - ^ -]]) - --- testing ExpPatt2 - -testerror([[&]], [[ -L1:C2: expected a pattern after '&' -& - ^ -]]) - -testerror([[& / 'p']], [[ -L1:C2: expected a pattern after '&' -& / 'p' - ^ -]]) - -testerror([['p' &]], [[ -L1:C6: expected a pattern after '&' -'p' & - ^ -]]) - -testerror([['p' / & / 'q']], [[ -L1:C8: expected a pattern after '&' -'p' / & / 'q' - ^ -]]) - -testerror([[&&]], [[ -L1:C3: expected a pattern after '&' -&& - ^ -]]) - -testerror([[!&]], [[ -L1:C3: expected a pattern after '&' -!& - ^ -]]) - --- testing ExpPatt3 - -testerror([[!]], [[ -L1:C2: expected a pattern after '!' -! - ^ -]]) - -testerror([[! / 'p']], [[ -L1:C2: expected a pattern after '!' -! / 'p' - ^ -]]) - -testerror([['p' !]], [[ -L1:C6: expected a pattern after '!' -'p' ! - ^ -]]) - -testerror([['p' / ! / 'q']], [[ -L1:C8: expected a pattern after '!' -'p' / ! / 'q' - ^ -]]) - -testerror([[!!]], [[ -L1:C3: expected a pattern after '!' -!! - ^ -]]) - -testerror([[&!]], [[ -L1:C3: expected a pattern after '!' -&! - ^ -]]) - --- testing ExpPatt4 - -testerror([[()]], [[ -L1:C2: expected a pattern after '(' -() - ^ -]]) - -testerror([[($$$)]], [[ -L1:C2: expected a pattern after '(' -($$$) - ^ -]]) - --- testing ExpPatt5 - -testerror([[{: *** :}]], [[ -L1:C3: expected a pattern after ':' -{: *** :} - ^ -]]) - -testerror([[{:group: *** :}]], [[ -L1:C9: expected a pattern after ':' -{:group: *** :} - ^ -]]) - -testerror([[x <- {:x:}]], [[ -L1:C10: expected a pattern after ':' -x <- {:x:} - ^ -]]) - --- testing ExpPatt6 - -testerror([[{~~}]], [[ -L1:C3: expected a pattern after '{~' -{~~} - ^ -]]) - -testerror([[{ {~ } ~}]], [[ -L1:C5: expected a pattern after '{~' -{ {~ } ~} - ^ -]]) - -testerror([[{~ ^_^ ~}]], [[ -L1:C3: expected a pattern after '{~' -{~ ^_^ ~} - ^ -]]) - --- testing ExpPatt7 - -testerror([[{||}]], [[ -L1:C3: expected a pattern after '{|' -{||} - ^ -]]) - -testerror([[{|@|}]], [[ -L1:C3: expected a pattern after '{|' -{|@|} - ^ -]]) - --- testing ExpPatt8 - -testerror([[S <-]], [[ -L1:C5: expected a pattern after '<-' -S <- - ^ -]]) - -testerror([[S <- 'p' T <-]], [[ -L1:C14: expected a pattern after '<-' -S <- 'p' T <- - ^ -]]) - --- testing ExpPattOrClose - -testerror([[{0}]], [[ -L1:C2: expected a pattern or closing '}' after '{' -{0} - ^ -]]) - -testerror([[{ :'p': }]], [[ -L1:C2: expected a pattern or closing '}' after '{' -{ :'p': } - ^ -]]) - --- testing ExpNum - -testerror([['p' ^ n]], [[ -L1:C6: expected a number after '^', '+' or '-' (no space) -'p' ^ n - ^ -]]) - -testerror([['p'^+(+1)]], [[ -L1:C5: expected a number after '^', '+' or '-' (no space) -'p'^+(+1) - ^ -]]) - -testerror([['p'^-/'q']], [[ -L1:C5: expected a number after '^', '+' or '-' (no space) -'p'^-/'q' - ^ -]]) - --- testing ExpCap - -testerror([['p' -> {]], [[ -L1:C7: expected a string, number, '{}' or name after '->' -'p' -> { - ^ -]]) - -testerror([['p' -> {'q'}]], [[ -L1:C7: expected a string, number, '{}' or name after '->' -'p' -> {'q'} - ^ -]]) - -testerror([['p' -> / 'q']], [[ -L1:C7: expected a string, number, '{}' or name after '->' -'p' -> / 'q' - ^ -]]) - -testerror([['p' -> [0-9] ]], [[ -L1:C7: expected a string, number, '{}' or name after '->' -'p' -> [0-9] - ^ -]]) - --- testing ExpName1 - -testerror([['p' =>]], [[ -L1:C7: expected the name of a rule after '=>' -'p' => - ^ -]]) - -testerror([['p' => 'q']], [[ -L1:C7: expected the name of a rule after '=>' -'p' => 'q' - ^ -]]) - --- testing ExpName2 - -testerror([['<' {:tag: [a-z]+ :} '>' '<' = '>']], [[ -L1:C31: expected the name of a rule after '=' (no space) -'<' {:tag: [a-z]+ :} '>' '<' = '>' - ^ -]]) - -testerror([['<' {:tag: [a-z]+ :} '>' '<' = tag '>']], [[ -L1:C31: expected the name of a rule after '=' (no space) -'<' {:tag: [a-z]+ :} '>' '<' = tag '>' - ^ -]]) - --- testing ExpName3 - -testerror([[<>]], [[ -L1:C2: expected the name of a rule after '<' (no space) -<> - ^ -]]) - -testerror([[<123>]], [[ -L1:C2: expected the name of a rule after '<' (no space) -<123> - ^ -]]) - -testerror([[< hello >]], [[ -L1:C2: expected the name of a rule after '<' (no space) -< hello > - ^ -]]) - -testerror([[<>]], [[ -L1:C2: expected the name of a rule after '<' (no space) -<> - ^ -]]) - --- testing ExpLab1 - -testerror([['p' //{} 'q']], [[ -L1:C8: expected at least one label after '{' -'p' //{} 'q' - ^ -]]) - -testerror([[%{ 'label' }]], [[ -L1:C3: expected at least one label after '{' -%{ 'label' } - ^ -]]) - --- testing ExpLab2 - -testerror([['p' //{1,2,3,} 'q']], [[ -L1:C14: expected a label after the comma -'p' //{1,2,3,} 'q' - ^ -]]) - --- testing ExpNameOrLab - -testerror([[% s]], [[ -L1:C2: expected a name or label after '%' (no space) -% s - ^ -]]) - -testerror([[% {1}]], [[ -L1:C2: expected a name or label after '%' (no space) -% {1} - ^ -]]) - --- testing ExpItem - -testerror([[ -"p" [ -abc -] "q" -]], [[ -L1:C6: expected at least one item after '[' or '^' -"p" [ - ^ -]]) - --- testing MisClose1 - -testerror([[('p' ('q' / 'r')]], [[ -L1:C17: missing closing ')' -('p' ('q' / 'r') - ^ -]]) - --- testing MisClose2 - --- two errors are reported due to the ignore strategy -testerror([[{: group: 'p' :}]], [[ -L1:C9: missing closing ':}' -{: group: 'p' :} - ^ -]]) - -testerror([[S <- {: 'p' T <- 'q']], [[ -L1:C12: missing closing ':}' -S <- {: 'p' T <- 'q' - ^ -]]) - --- testing MisClose3 - -testerror([['p' {~ ('q' 'r') / 's']], [[ -L1:C23: missing closing '~}' -'p' {~ ('q' 'r') / 's' - ^ -]]) - --- testing MisClose4 - --- two errors are reported due to the ignore strategy -testerror([['p' {| 'q' / 'r' }]], [[ -L1:C17: missing closing '|}' -'p' {| 'q' / 'r' } - ^ -]]) - --- testing MisClose5 - -testerror([[{ 'p' ]], [[ -L1:C6: missing closing '}' -{ 'p' - ^ -]]) - --- testing MisClose6 - -testerror([[' -]], [[ -L1:C8: missing closing '>' - - ^ -]]) - --- testing MisClose7 - -testerror([['{' %{ a, b '}']], [[ -L1:C9: missing closing '}' -'{' %{ a, b '}' - ^ -]]) - --- testing MisClose8 - -testerror([[[]], [[ -L1:C2: missing closing ']' -[ - ^ -]]) - -testerror([[[^]], [[ -L1:C3: missing closing ']' -[^ - ^ -]]) - -testerror([[[] ]], [[ -L1:C4: missing closing ']' -[] - ^ -]]) - -testerror([[[^] ]], [[ -L1:C6: missing closing ']' -[^] - ^ -]]) - -testerror([[[_-___-_|]], [[ -L1:C10: missing closing ']' -[_-___-_| - ^ -]]) - --- testing MisTerm1 - -testerror([['That is the question...]], [[ -L1:C25: missing terminating single quote -'That is the question... - ^ -]]) - --- testing MisTerm2 - -testerror([[Q <- "To be or not to be...]], [[ -L1:C28: missing terminating double quote -Q <- "To be or not to be... - ^ -]]) - --- testing non-syntax errors - -testerror([[ - A <- %nosuch %def - A <- 'A again' - A <- 'and again' -]], [[ -name 'nosuch' undefined -]]) - -testerror([[names not in grammar]], [[ -rule 'names' used outside a grammar -]]) - -testerror([[ - A <- %nosuch %def - A <- 'A again' - A <- 'and again' -]], [[ -name 'nosuch' undefined -]]) - --- the non-syntax error should not be reported --- since there is a syntax error -testerror([[ A <- %nosuch ('error' ]], [[ -L1:C23: missing closing ')' - A <- %nosuch ('error' - ^ -]]) - - -print 'OK' diff --git a/06/deps/lua-5.4.6/.gitignore b/06/deps/lua-5.4.6/.gitignore deleted file mode 100644 index d27f61e..0000000 --- a/06/deps/lua-5.4.6/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -**/*.o -**/*.d -src/liblua.a -src/lua -src/luac \ No newline at end of file diff --git a/06/deps/lua-5.4.6/Makefile b/06/deps/lua-5.4.6/Makefile deleted file mode 100644 index 03cca3b..0000000 --- a/06/deps/lua-5.4.6/Makefile +++ /dev/null @@ -1,106 +0,0 @@ -# Makefile for installing Lua -# See doc/readme.html for installation and customization instructions. - -# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= - -# Your platform. See PLATS for possible values. -PLAT= generic - -# Where to install. The installation starts in the src and doc directories, -# so take care if INSTALL_TOP is not an absolute path. See the local target. -# You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with -# LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h. -INSTALL_TOP= ../../lua-bootstrap -INSTALL_BIN= $(INSTALL_TOP)/bin -INSTALL_INC= $(INSTALL_TOP)/include -INSTALL_LIB= $(INSTALL_TOP)/lib -INSTALL_MAN= $(INSTALL_TOP)/man/man1 -INSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V -INSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V - -# How to install. If your install program does not support "-p", then -# you may have to run ranlib on the installed liblua.a. -INSTALL= install -p -INSTALL_EXEC= $(INSTALL) -m 0755 -INSTALL_DATA= $(INSTALL) -m 0644 -# -# If you don't have "install" you can use "cp" instead. -# INSTALL= cp -p -# INSTALL_EXEC= $(INSTALL) -# INSTALL_DATA= $(INSTALL) - -# Other utilities. -MKDIR= mkdir -p -RM= rm -f - -# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= - -# Convenience platforms targets. -PLATS= guess aix bsd c89 freebsd generic ios linux linux-readline macosx mingw posix solaris - -# What to install. -TO_BIN= lua luac -TO_INC= lua.h luaconf.h lualib.h lauxlib.h lua.hpp -TO_LIB= liblua.a -TO_MAN= lua.1 luac.1 - -# Lua version and release. -V= 5.4 -R= $V.6 - -# Targets start here. -all: $(PLAT) - -$(PLATS) help test clean: - @cd src && $(MAKE) $@ - -install: dummy - cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD) - cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN) - cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC) - cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB) - cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN) - -uninstall: - cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN) - cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC) - cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB) - cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN) - -local: - $(MAKE) install INSTALL_TOP=../install - -# make may get confused with install/ if it does not support .PHONY. -dummy: - -# Echo config parameters. -echo: - @cd src && $(MAKE) -s echo - @echo "PLAT= $(PLAT)" - @echo "V= $V" - @echo "R= $R" - @echo "TO_BIN= $(TO_BIN)" - @echo "TO_INC= $(TO_INC)" - @echo "TO_LIB= $(TO_LIB)" - @echo "TO_MAN= $(TO_MAN)" - @echo "INSTALL_TOP= $(INSTALL_TOP)" - @echo "INSTALL_BIN= $(INSTALL_BIN)" - @echo "INSTALL_INC= $(INSTALL_INC)" - @echo "INSTALL_LIB= $(INSTALL_LIB)" - @echo "INSTALL_MAN= $(INSTALL_MAN)" - @echo "INSTALL_LMOD= $(INSTALL_LMOD)" - @echo "INSTALL_CMOD= $(INSTALL_CMOD)" - @echo "INSTALL_EXEC= $(INSTALL_EXEC)" - @echo "INSTALL_DATA= $(INSTALL_DATA)" - -# Echo pkg-config data. -pc: - @echo "version=$R" - @echo "prefix=$(INSTALL_TOP)" - @echo "libdir=$(INSTALL_LIB)" - @echo "includedir=$(INSTALL_INC)" - -# Targets that do not create files (not all makes understand .PHONY). -.PHONY: all $(PLATS) help test clean install uninstall local dummy echo pc - -# (end of Makefile) diff --git a/06/deps/lua-5.4.6/README b/06/deps/lua-5.4.6/README deleted file mode 100644 index 1ae9716..0000000 --- a/06/deps/lua-5.4.6/README +++ /dev/null @@ -1,6 +0,0 @@ - -This is Lua 5.4.6, released on 02 May 2023. - -For installation instructions, license details, and -further information about Lua, see doc/readme.html. - diff --git a/06/deps/lua-5.4.6/doc/contents.html b/06/deps/lua-5.4.6/doc/contents.html deleted file mode 100644 index 1231e6d..0000000 --- a/06/deps/lua-5.4.6/doc/contents.html +++ /dev/null @@ -1,678 +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–2023 Lua.org, PUC-Rio. -Freely available under the terms of the -Lua license. - - -

    Contents

    - - -

    Index

    - - - - - - - - - - - - - - diff --git a/06/deps/lua-5.4.6/doc/index.css b/06/deps/lua-5.4.6/doc/index.css deleted file mode 100644 index c961835..0000000 --- a/06/deps/lua-5.4.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/06/deps/lua-5.4.6/doc/logo.gif b/06/deps/lua-5.4.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/06/deps/lua-5.4.6/doc/lua.1 b/06/deps/lua-5.4.6/doc/lua.1 deleted file mode 100644 index 3f472fd..0000000 --- a/06/deps/lua-5.4.6/doc/lua.1 +++ /dev/null @@ -1,155 +0,0 @@ -.\" $Id: lua.man,v 1.14 2022/09/23 09:06:36 lhf Exp $ -.TH LUA 1 "$Date: 2022/09/23 09:06:36 $" -.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 " mod" -require library -.I mod -into global -.IR mod . -.TP -.BI \-l " g=mod" -require library -.I mod -into global -.IR g . -.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/06/deps/lua-5.4.6/doc/lua.css b/06/deps/lua-5.4.6/doc/lua.css deleted file mode 100644 index cbd0799..0000000 --- a/06/deps/lua-5.4.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/06/deps/lua-5.4.6/doc/luac.1 b/06/deps/lua-5.4.6/doc/luac.1 deleted file mode 100644 index 33a4ed0..0000000 --- a/06/deps/lua-5.4.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/06/deps/lua-5.4.6/doc/manual.css b/06/deps/lua-5.4.6/doc/manual.css deleted file mode 100644 index aa0e677..0000000 --- a/06/deps/lua-5.4.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/06/deps/lua-5.4.6/doc/manual.html b/06/deps/lua-5.4.6/doc/manual.html deleted file mode 100644 index 0af688b..0000000 --- a/06/deps/lua-5.4.6/doc/manual.html +++ /dev/null @@ -1,12046 +0,0 @@ - - - -Lua 5.4 Reference Manual - - - - - - - -

    -Lua -Lua 5.4 Reference Manual -

    - -

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

    - -Copyright © 2020–2023 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' and written in decimal. -(For instance, 0x1.fp10 denotes 1984, -which is 0x1f / 16 multiplied by 210.) - - -

    -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 (see §3.4.12). - - -

    -If a variable is both assigned and read -inside a multiple assignment, -Lua ensures that 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. - - -

    -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 variadic 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). - - - - - -

    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. - - -

    -It is always a good practice not to rely on the -implicit coercions from strings to numbers, -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 multires expression, -then all values returned by this expression enter the list consecutively -(see §3.4.12). - - -

    -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 (see §3.4.12), -unless the function is a variadic function, -which is indicated by three dots ('...') -at the end of its parameter list. -A variadic 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 (see §3.4.12). - - -

    -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.4.12 – Lists of expressions, multiple results, -and adjustment

    - -

    -Both function calls and vararg expressions can result in multiple values. -These expressions are called multires expressions. - - -

    -When a multires expression is used as the last element -of a list of expressions, -all results from the expression are added to the -list of values produced by the list of expressions. -Note that a single expression -in a place that expects a list of expressions -is the last expression in that (singleton) list. - - -

    -These are the places where Lua expects a list of expressions: - -

      - -
    • A return statement, -for instance return e1, e2, e3 (see §3.3.4).
    • - -
    • A table constructor, -for instance {e1, e2, e3} (see §3.4.9).
    • - -
    • The arguments of a function call, -for instance foo(e1, e2, e3) (see §3.4.10).
    • - -
    • A multiple assignment, -for instance a , b, c = e1, e2, e3 (see §3.3.3).
    • - -
    • A local declaration, -for instance local a , b, c = e1, e2, e3 (see §3.3.7).
    • - -
    • The initial values in a generic for loop, -for instance for k in e1, e2, e3 do ... end (see §3.3.5).
    • - -

    -In the last four cases, -the list of values from the list of expressions -must be adjusted to a specific length: -the number of parameters in a call to a non-variadic function -(see §3.4.11), -the number of variables in a multiple assignment or -a local declaration, -and exactly four values for a generic for loop. -The adjustment follows these rules: -If there are more values than needed, -the extra values are thrown away; -if there are fewer values than needed, -the list is extended with nil's. -When the list of expressions ends with a multires expression, -all results from that expression enter the list of values -before the adjustment. - - -

    -When a multires expression is used -in a list of expressions without being the last element, -or in a place where the syntax expects a single expression, -Lua adjusts the result list of that expression to one element. -As a particular case, -the syntax expects a single expression inside a parenthesized expression; -therefore, adding parentheses around a multires expression -forces it to produce exactly one result. - - -

    -We seldom need to use a vararg expression in a place -where the syntax expects a single expression. -(Usually it is simpler to add a regular parameter before -the variadic part and use that parameter.) -When there is such a need, -we recommend assigning the vararg expression -to a single variable and using that variable -in its place. - - -

    -Here are some examples of uses of mutlres expressions. -In all cases, when the construction needs -"the n-th result" and there is no such result, -it uses a nil. - -

    -     print(x, f())      -- prints x and all results from f().
    -     print(x, (f()))    -- prints x and the first result from f().
    -     print(f(), x)      -- prints the first result from f() and x.
    -     print(1 + f())     -- prints 1 added to the first result from f().
    -     local x = ...      -- x gets the first vararg argument.
    -     x,y = ...          -- x gets the first vararg argument,
    -                        -- y gets the second vararg argument.
    -     x,y,z = w, f()     -- x gets w, y gets the first result from f(),
    -                        -- z gets the second result from f().
    -     x,y,z = f()        -- x gets the first result from f(),
    -                        -- y gets the second result from f(),
    -                        -- z gets the third result from f().
    -     x,y,z = f(), g()   -- x gets the first result from f(),
    -                        -- y gets the first result from g(),
    -                        -- z gets the second result from g().
    -     x,y,z = (f())      -- x gets the first result from f(), y and z get nil.
    -     return f()         -- returns all results from f().
    -     return x, ...      -- returns x and 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(), 5}           -- creates a list with the first result from f() and 5.
    -
    - - - - - - -

    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. -(Void statements are labels and empty statements.) -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 ISO 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. - - -

    -(This function was introduced in release 5.4.3.) - - - - - -


    lua_closethread

    -[-0, +?, –] -

    int lua_closethread (lua_State *L, lua_State *from);
    - -

    -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. - - -

    -The parameter from represents the coroutine that is resetting L. -If there is no such coroutine, -this parameter can be NULL. - - -

    -(This function was introduced in release 5.4.6.) - - - - - -


    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). -The value at index must be a table. - - - - - -


    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). -The value at index must be a table. - - - - - -


    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);
    - -

    -This function is deprecated; -it is equivalent to lua_closethread with -from being NULL. - - - - - -


    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 variadic 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 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 0 (LUA_OK) if there are no errors, -or 1 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 0 (LUA_OK) 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_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 ISO 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] -

    int 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. -The real table is stored in the C registry (see §4.3), -indexed by the key LUA_LOADED_TABLE, a string. - - - - -

    -


    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 functionality is not supported by ISO 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. -The real table is stored in the C registry (see §4.3), -indexed by the key LUA_PRELOAD_TABLE, a string. - - - - -

    -


    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 length 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), -using 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, -the function closes the Lua state before exiting (see lua_close). - - - - -

    -


    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;
    • -
    • -l g=mod: "require" mod and assign the - result to global g;
    • -
    • -v: print version information;
    • -
    • -E: ignore environment variables;
    • -
    • -W: turn warnings on;
    • -
    • --: stop handling options;
    • -
    • -: execute stdin as a file and stop handling options.
    • -

    -(The form -l g=mod was introduced in release 5.4.4.) - - -

    -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 variadic 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/06/deps/lua-5.4.6/doc/osi-certified-72x60.png b/06/deps/lua-5.4.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.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. - -

    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.6. -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 ios 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–2023 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/06/deps/lua-5.4.6/src/Makefile b/06/deps/lua-5.4.6/src/Makefile deleted file mode 100644 index bc879e5..0000000 --- a/06/deps/lua-5.4.6/src/Makefile +++ /dev/null @@ -1,225 +0,0 @@ -# Makefile for building Lua -# See ../doc/readme.html for installation and customization instructions. - -# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= - -# Your platform. See PLATS for possible values. -PLAT= linux - -CC= ../../../../05/tcc-0.9.27/tcc -CFLAGS= -nostdinc -B ../../../../05/tcc-boostrap -I ../../../../05/musl-bootstrap/include $(SYSCFLAGS) $(MYCFLAGS) -LDFLAGS= -nostdlib $(SYSLDFLAGS) $(MYLDFLAGS) -LIBS= $(SYSLIBS) $(MYLIBS) - -AR= ../../../../05/tcc-0.9.27/tcc -ar rc -RANLIB= ranlib -RM= rm -f -UNAME= uname - -SYSCFLAGS= -SYSLDFLAGS= -SYSLIBS= - -MYCFLAGS= -MYLDFLAGS= -MYLIBS= -MYOBJS= - -# Special flags for compiler modules; -Os reduces code size. -CMCFLAGS= - -# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= - -PLATS= guess aix bsd c89 freebsd generic ios linux linux-readline macosx mingw posix solaris - -LUA_A= liblua.a -CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o -LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o linit.o -BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS) - -LUA_T= lua -LUA_O= lua.o - -LUAC_T= luac -LUAC_O= luac.o - -ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O) -ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) -ALL_A= $(LUA_A) - -# Targets start here. -default: $(PLAT) - -all: $(ALL_T) - -o: $(ALL_O) - -a: $(ALL_A) - -$(LUA_A): $(BASE_O) - $(AR) $@ $(BASE_O) - $(RANLIB) $@ - -$(LUA_T): $(LUA_O) $(LUA_A) - $(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) ../../../../05/musl-bootstrap/lib/*.[oa] - -$(LUAC_T): $(LUAC_O) $(LUA_A) - $(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) ../../../../05/musl-bootstrap/lib/*.[oa] - -test: - ./$(LUA_T) -v - -clean: - $(RM) $(ALL_T) $(ALL_O) - -depend: - @$(CC) $(CFLAGS) -MM l*.c - -echo: - @echo "PLAT= $(PLAT)" - @echo "CC= $(CC)" - @echo "CFLAGS= $(CFLAGS)" - @echo "LDFLAGS= $(LDFLAGS)" - @echo "LIBS= $(LIBS)" - @echo "AR= $(AR)" - @echo "RANLIB= $(RANLIB)" - @echo "RM= $(RM)" - @echo "UNAME= $(UNAME)" - -# Convenience targets for popular platforms. -ALL= all - -help: - @echo "Do 'make PLATFORM' where PLATFORM is one of these:" - @echo " $(PLATS)" - @echo "See doc/readme.html for complete instructions." - -guess: - @echo Guessing `$(UNAME)` - @$(MAKE) `$(UNAME)` - -AIX aix: - $(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall" - -bsd: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E" - -c89: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" - @echo '' - @echo '*** C89 does not guarantee 64-bit integers for Lua.' - @echo '*** Make sure to compile all external Lua libraries' - @echo '*** with LUA_USE_C89 to ensure consistency' - @echo '' - -FreeBSD NetBSD OpenBSD freebsd: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc" - -generic: $(ALL) - -ios: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_IOS" - -Linux linux: linux-noreadline - -linux-noreadline: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl" - -linux-readline: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline" - -Darwin macos macosx: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline" - -mingw: - $(MAKE) "LUA_A=lua54.dll" "LUA_T=lua.exe" \ - "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ - "SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe - $(MAKE) "LUAC_T=luac.exe" luac.exe - -posix: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX" - -SunOS solaris: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl" - -# Targets that do not create files (not all makes understand .PHONY). -.PHONY: all $(PLATS) help test clean default o a depend echo - -# Compiler modules may use special flags. -llex.o: - $(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c - -lparser.o: - $(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c - -lcode.o: - $(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c - -# DO NOT DELETE - -lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ - ltable.h lundump.h lvm.h -lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h -lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ - llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ - ldo.h lgc.h lstring.h ltable.h lvm.h -lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h -ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ - ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h -ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ - lparser.h lstring.h ltable.h lundump.h lvm.h -ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ - ltm.h lzio.h lmem.h lundump.h -lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h -lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h -linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h -liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ - lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ - lstring.h ltable.h -lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h -loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ - ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ - lvm.h -lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h -loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ - llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ - ldo.h lfunc.h lstring.h lgc.h ltable.h -lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ - lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \ - lstring.h ltable.h -lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ - lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h -lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h ldebug.h lstate.h \ - lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.h -lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ - lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ - lundump.h -lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ - ltable.h lvm.h ljumptab.h -lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ - lobject.h ltm.h lzio.h - -# (end of Makefile) diff --git a/06/deps/lua-5.4.6/src/lapi.c b/06/deps/lua-5.4.6/src/lapi.c deleted file mode 100644 index 34e64af..0000000 --- a/06/deps/lua-5.4.6/src/lapi.c +++ /dev/null @@ -1,1463 +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.p + idx; - api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); - if (o >= L->top.p) return &G(L)->nilvalue; - else return s2v(o); - } - else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), - "invalid index"); - return s2v(L->top.p + 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.p))) { /* C closure? */ - CClosure *func = clCvalue(s2v(ci->func.p)); - 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.p)), "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.p + idx; - api_check(L, o < L->top.p, "invalid index"); - return o; - } - else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), - "invalid index"); - api_check(L, !ispseudo(idx), "invalid index"); - return L->top.p + 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.p - L->top.p > n) /* stack large enough? */ - res = 1; /* yes; check is OK */ - else /* need to grow stack */ - res = luaD_growstack(L, n, 0); - if (res && ci->top.p < L->top.p + n) - ci->top.p = L->top.p + 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.p - to->top.p >= n, "stack overflow"); - from->top.p -= n; - for (i = 0; i < n; i++) { - setobjs2s(to, to->top.p, from->top.p + i); - to->top.p++; /* 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.p - L->ci->func.p) + idx; -} - - -LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top.p - (L->ci->func.p + 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.p; - if (idx >= 0) { - api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); - diff = ((func + 1) + idx) - L->top.p; - for (; diff > 0; diff--) - setnilvalue(s2v(L->top.p++)); /* clear new slots */ - } - else { - api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); - diff = idx + 1; /* will "subtract" index (as it is negative) */ - } - api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot"); - newtop = L->top.p + diff; - if (diff < 0 && L->tbclist.p >= newtop) { - lua_assert(hastocloseCfunc(ci->nresults)); - newtop = luaF_close(L, newtop, CLOSEKTOP, 0); - } - L->top.p = 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.p == level, - "no variable to close at given level"); - level = luaF_close(L, level, CLOSEKTOP, 0); - 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.p - 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.p)), 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.p, 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.p, L->top.p - 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.p - 2), s2v(L->top.p - 1), L->top.p - 2); - L->top.p--; /* 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.p)); - 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.p)); - 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.p), 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.p), 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.p, 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.p)); - else { - TString *ts; - ts = luaS_new(L, s); - setsvalue2s(L, L->top.p, 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.p), 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.p -= n; - while (n--) { - setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n)); - /* does not need barrier because closure is white */ - lua_assert(iswhite(cl)); - } - setclCvalue(L, s2v(L->top.p), 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.p)); - else - setbfvalue(s2v(L->top.p)); - 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), p); - api_incr_top(L); - lua_unlock(L); -} - - -LUA_API int lua_pushthread (lua_State *L) { - lua_lock(L); - setthvalue(L, s2v(L->top.p), 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.p, slot); - api_incr_top(L); - } - else { - setsvalue2s(L, L->top.p, str); - api_incr_top(L); - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); - } - lua_unlock(L); - return ttype(s2v(L->top.p - 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.p - 1), slot, luaH_get)) { - setobj2s(L, L->top.p - 1, slot); - } - else - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); - lua_unlock(L); - return ttype(s2v(L->top.p - 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.p, slot); - } - else { - TValue aux; - setivalue(&aux, n); - luaV_finishget(L, t, &aux, L->top.p, slot); - } - api_incr_top(L); - lua_unlock(L); - return ttype(s2v(L->top.p - 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.p)); - else - setobj2s(L, L->top.p, val); - api_incr_top(L); - lua_unlock(L); - return ttype(s2v(L->top.p - 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.p - 1)); - L->top.p--; /* 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.p, 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.p, 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.p)); - t = LUA_TNONE; - } - else { - setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv); - t = ttype(s2v(L->top.p)); - } - 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.p - 1)); - L->top.p--; /* pop value */ - } - else { - setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ - api_incr_top(L); - luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); - L->top.p -= 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.p - 2), slot, luaH_get)) { - luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); - } - else - luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot); - L->top.p -= 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.p - 1)); - } - else { - TValue aux; - setivalue(&aux, n); - luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot); - } - L->top.p--; /* 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.p - 1)); - invalidateTMcache(t); - luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); - L->top.p -= n; - lua_unlock(L); -} - - -LUA_API void lua_rawset (lua_State *L, int idx) { - aux_rawset(L, idx, s2v(L->top.p - 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.p - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); - L->top.p--; - 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.p - 1))) - mt = NULL; - else { - api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); - mt = hvalue(s2v(L->top.p - 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.p--; - 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.p - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1)); - res = 1; - } - L->top.p--; - 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.p - L->top.p >= (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.p - (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.p - (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.p - 1)); /* get new 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.p, 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.p - 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.p - 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.p - 1); - if (more) { - api_incr_top(L); - } - else /* no more elements */ - L->top.p -= 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.p < 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.p, 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.p, 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.p), 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.p; - 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.p, 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.p--; - setobj(L, val, s2v(L->top.p)); - 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/06/deps/lua-5.4.6/src/lapi.h b/06/deps/lua-5.4.6/src/lapi.h deleted file mode 100644 index a742427..0000000 --- a/06/deps/lua-5.4.6/src/lapi.h +++ /dev/null @@ -1,52 +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.p', checking for stack overflows */ -#define api_incr_top(L) {L->top.p++; \ - api_check(L, L->top.p <= L->ci->top.p, \ - "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.p'). -*/ -#define adjustresults(L,nres) \ - { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \ - L->ci->top.p = L->top.p; } - - -/* Ensure the stack has at least 'n' elements */ -#define api_checknelems(L,n) \ - api_check(L, (n) < (L->top.p - L->ci->func.p), \ - "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/06/deps/lua-5.4.6/src/lauxlib.c b/06/deps/lua-5.4.6/src/lauxlib.c deleted file mode 100644 index 4ca6c65..0000000 --- a/06/deps/lua-5.4.6/src/lauxlib.c +++ /dev/null @@ -1,1112 +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. (The test for "not big enough" also gets the case when the -** computation of 'newsize' overflows.) -*/ -static size_t newbuffsize (luaL_Buffer *B, size_t sz) { - size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ - 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) /* 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) below 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; -} - - -/* -** Skip an optional BOM at the start of a stream. If there is an -** incomplete BOM (the first character is correct but the rest is -** not), returns the first character anyway to force an error -** (as no chunk can start with 0xEF). -*/ -static int skipBOM (FILE *f) { - int c = getc(f); /* read first character */ - if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ - return getc(f); /* ignore BOM and return next char */ - else /* no (valid) BOM */ - return c; /* return first 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 (FILE *f, int *cp) { - int c = *cp = skipBOM(f); - if (c == '#') { /* first line is a comment (Unix exec. file)? */ - do { /* skip first line */ - c = getc(f); - } while (c != EOF && c != '\n'); - *cp = getc(f); /* next character after comment, 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); - } - lf.n = 0; - if (skipcomment(lf.f, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ - if (c == LUA_SIGNATURE[0]) { /* binary file? */ - lf.n = 0; /* remove possible newline */ - if (filename) { /* "real" file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(lf.f, &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/06/deps/lua-5.4.6/src/lauxlib.h b/06/deps/lua-5.4.6/src/lauxlib.h deleted file mode 100644 index 5b977e2..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lbaselib.c b/06/deps/lua-5.4.6/src/lbaselib.c deleted file mode 100644 index 1d60c9d..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lcode.c b/06/deps/lua-5.4.6/src/lcode.c deleted file mode 100644 index 1a371ca..0000000 --- a/06/deps/lua-5.4.6/src/lcode.c +++ /dev/null @@ -1,1871 +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; -} - - -/* -** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP) -*/ -l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) { - lua_assert(baser <= opr && - ((baser == OPR_ADD && opr <= OPR_SHR) || - (baser == OPR_LT && opr <= OPR_LE))); - return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base)); -} - - -/* -** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP) -*/ -l_sinline OpCode unopr2op (UnOpr opr) { - return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) + - cast_int(OP_UNM)); -} - - -/* -** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM) -*/ -l_sinline TMS binopr2TM (BinOpr opr) { - lua_assert(OPR_ADD <= opr && opr <= OPR_SHR); - return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD)); -} - - -/* -** 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, BinOpr opr, - expdesc *e1, expdesc *e2, int line) { - OpCode op = binopr2op(opr, OPR_ADD, OP_ADD); - int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ - /* 'e1' must be already in a register or it is a constant */ - lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || - e1->k == VNONRELOC || e1->k == VRELOC); - lua_assert(OP_ADD <= op && op <= OP_SHR); - finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr)); -} - - -/* -** 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); -} - - -/* -** Code binary operators with K operand. -*/ -static void codebinK (FuncState *fs, BinOpr opr, - expdesc *e1, expdesc *e2, int flip, int line) { - TMS event = binopr2TM(opr); - int v2 = e2->u.info; /* K index */ - OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, 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 binary operators with no constant operand. -*/ -static void codebinNoK (FuncState *fs, BinOpr opr, - expdesc *e1, expdesc *e2, int flip, int line) { - if (flip) - swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, opr, e1, e2, line); /* use standard operators */ -} - - -/* -** 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) { - if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */ - codebinK(fs, opr, e1, e2, flip, line); - else /* 'e2' is neither an immediate nor a K operand */ - codebinNoK(fs, opr, e1, e2, flip, line); -} - - -/* -** 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, OP_ADDI, e1, e2, flip, line, TM_ADD); - else - codearith(fs, op, e1, e2, flip, line); -} - - -/* -** Code bitwise operations; they are all commutative, 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; - if (e1->k == VKINT) { - swapexps(e1, e2); /* 'e2' will be the constant operand */ - flip = 1; - } - if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */ - codebinK(fs, opr, e1, e2, flip, line); - else /* no constants */ - codebinNoK(fs, opr, e1, e2, flip, line); -} - - -/* -** Emit code for order comparisons. When using an immediate operand, -** 'isfloat' tells whether the original value was a float. -*/ -static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { - int r1, r2; - int im; - int isfloat = 0; - OpCode op; - if (isSCnumber(e2, &im, &isfloat)) { - /* use immediate operand */ - r1 = luaK_exp2anyreg(fs, e1); - r2 = im; - op = binopr2op(opr, OPR_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 = binopr2op(opr, OPR_LT, OP_GTI); - } - else { /* regular case, compare two registers */ - r1 = luaK_exp2anyreg(fs, e1); - r2 = luaK_exp2anyreg(fs, e2); - op = binopr2op(opr, OPR_LT, OP_LT); - } - 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)) { /* 2nd 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 opr, expdesc *e, int line) { - static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; - luaK_dischargevars(fs, e); - switch (opr) { - case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, opr + LUA_OPUNM, e, &ef)) - break; - /* else */ /* FALLTHROUGH */ - case OPR_LEN: - codeunexpval(fs, unopr2op(opr), 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 or used as an immediate - 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, opr, 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, opr, e1, e2, line); - break; - } - case OPR_EQ: case OPR_NE: { - codeeq(fs, opr, e1, e2); - break; - } - case OPR_GT: case OPR_GE: { - /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ - swapexps(e1, e2); - opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT); - } /* FALLTHROUGH */ - case OPR_LT: case OPR_LE: { - codeorder(fs, opr, 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/06/deps/lua-5.4.6/src/lcode.h b/06/deps/lua-5.4.6/src/lcode.h deleted file mode 100644 index 3265824..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lcorolib.c b/06/deps/lua-5.4.6/src/lcorolib.c deleted file mode 100644 index c64adf0..0000000 --- a/06/deps/lua-5.4.6/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_closethread(co, L); /* 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_closethread(co, L); - 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/06/deps/lua-5.4.6/src/lctype.c b/06/deps/lua-5.4.6/src/lctype.c deleted file mode 100644 index 9542280..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lctype.h b/06/deps/lua-5.4.6/src/lctype.h deleted file mode 100644 index 864e190..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/ldblib.c b/06/deps/lua-5.4.6/src/ldblib.c deleted file mode 100644 index 6dcbaa9..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/ldebug.c b/06/deps/lua-5.4.6/src/ldebug.c deleted file mode 100644 index 28b1caa..0000000 --- a/06/deps/lua-5.4.6/src/ldebug.c +++ /dev/null @@ -1,924 +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))->p->is_vararg) { - int nextra = ci->u.l.nextraargs; - if (n >= -nextra) { /* 'n' is negative */ - *pos = ci->func.p - 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.p + 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.p : ci->next->func.p; - 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.p - 1))) /* not a Lua function? */ - name = NULL; - else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(s2v(L->top.p - 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.p, 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.p - 1); - L->top.p--; /* 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.p)); - 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.p, 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.p - 1); - api_check(L, ttisfunction(func), "function expected"); - what++; /* skip the '>' */ - L->top.p--; /* pop function */ - } - else { - ci = ar->i_ci; - func = s2v(ci->func.p); - 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.p, 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 and, if so, returns its index. Because 'o' may -** not point to a value in this stack, we cannot compare it with the -** region boundaries (undefined behavior in ISO C). -*/ -static int instack (CallInfo *ci, const TValue *o) { - int pos; - StkId base = ci->func.p + 1; - for (pos = 0; base + pos < ci->top.p; pos++) { - if (o == s2v(base + pos)) - return pos; - } - return -1; /* 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.p == 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) { /* not an upvalue? */ - int reg = instack(ci, o); /* try a register */ - if (reg >= 0) /* is 'o' a register? */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &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.p, L->top.p - 1); /* move argument */ - setobjs2s(L, L->top.p - 1, errfunc); /* push function */ - L->top.p++; /* assume EXTRA_STACK */ - luaD_callnoyield(L, L->top.p - 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)); - setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ - L->top.p--; - } - 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 difference */ - 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.p' 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.p = ci->top.p; /* 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/06/deps/lua-5.4.6/src/ldebug.h b/06/deps/lua-5.4.6/src/ldebug.h deleted file mode 100644 index 2c3074c..0000000 --- a/06/deps/lua-5.4.6/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.p))) - - -#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/06/deps/lua-5.4.6/src/ldo.c b/06/deps/lua-5.4.6/src/ldo.c deleted file mode 100644 index 2a0017c..0000000 --- a/06/deps/lua-5.4.6/src/ldo.c +++ /dev/null @@ -1,1024 +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.p - 1); /* error message on current top */ - break; - } - } - L->top.p = 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.p++, L->top.p - 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 -** =================================================================== -*/ - - -/* -** Change all pointers to the stack into offsets. -*/ -static void relstack (lua_State *L) { - CallInfo *ci; - UpVal *up; - L->top.offset = savestack(L, L->top.p); - L->tbclist.offset = savestack(L, L->tbclist.p); - for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v.offset = savestack(L, uplevel(up)); - for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top.offset = savestack(L, ci->top.p); - ci->func.offset = savestack(L, ci->func.p); - } -} - - -/* -** Change back all offsets into pointers. -*/ -static void correctstack (lua_State *L) { - CallInfo *ci; - UpVal *up; - L->top.p = restorestack(L, L->top.offset); - L->tbclist.p = restorestack(L, L->tbclist.offset); - for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v.p = s2v(restorestack(L, up->v.offset)); - for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top.p = restorestack(L, ci->top.offset); - ci->func.p = restorestack(L, ci->func.offset); - 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. -** In ISO C, any pointer use after the pointer has been deallocated is -** undefined behavior. So, before the reallocation, all pointers are -** changed to offsets, and after the reallocation they are changed back -** to pointers. As during the reallocation the pointers are invalid, the -** reallocation cannot run emergency collections. -** -** 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; - int oldgcstop = G(L)->gcstopem; - lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - relstack(L); /* change pointers to offsets */ - G(L)->gcstopem = 1; /* stop emergency collection */ - newstack = luaM_reallocvector(L, L->stack.p, oldsize + EXTRA_STACK, - newsize + EXTRA_STACK, StackValue); - G(L)->gcstopem = oldgcstop; /* restore emergency collection */ - if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ - correctstack(L); /* change offsets back to pointers */ - if (raiseerror) - luaM_error(L); - else return 0; /* do not raise an error */ - } - L->stack.p = newstack; - correctstack(L); /* change offsets back to pointers */ - L->stack_last.p = L->stack.p + newsize; - for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) - setnilvalue(s2v(newstack + i)); /* erase new segment */ - 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 if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ - int newsize = 2 * size; /* tentative new size */ - int needed = cast_int(L->top.p - L->stack.p) + 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; -} - - -/* -** Compute how much of the stack is being used, by computing the -** maximum top of all call frames in the stack and the current top. -*/ -static int stackinuse (lua_State *L) { - CallInfo *ci; - int res; - StkId lim = L->top.p; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top.p) lim = ci->top.p; - } - lua_assert(lim <= L->stack_last.p + EXTRA_STACK); - res = cast_int(lim - L->stack.p) + 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 max = (inuse > LUAI_MAXSTACK / 3) ? LUAI_MAXSTACK : inuse * 3; - /* 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) { - int nsize = (inuse > LUAI_MAXSTACK / 2) ? LUAI_MAXSTACK : inuse * 2; - 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.p++; -} - -/* }================================================================== */ - - -/* -** 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.p); /* preserve original 'top' */ - ptrdiff_t ci_top = savestack(L, ci->top.p); /* 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.p < ci->top.p) - L->top.p = ci->top.p; /* protect entire activation register */ - luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (ci->top.p < L->top.p + LUA_MINSTACK) - ci->top.p = L->top.p + 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.p = restorestack(L, ci_top); - L->top.p = 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.p - 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.p += delta; /* if vararg, back to virtual 'func' */ - ftransfer = cast(unsigned short, firstres - ci->func.p); - luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ - ci->func.p -= 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; p > func; p--) /* open space for metamethod */ - setobjs2s(L, p, p-1); - L->top.p++; /* 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.p = 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.p - nres); /* move it to proper place */ - L->top.p = 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? */ - L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ - L->ci->u2.nres = nres; - res = luaF_close(L, res, CLOSEKTOP, 1); - L->ci->callstatus &= ~CIST_CLSRET; - if (L->hookmask) { /* if needed, call hook after '__close's */ - ptrdiff_t savedres = savestack(L, res); - rethook(L, L->ci, nres); - res = restorestack(L, savedres); /* hook can move stack */ - } - wanted = decodeNresults(wanted); - if (wanted == LUA_MULTRET) - wanted = nres; /* we want all results */ - } - break; - } - /* generic case */ - firstresult = L->top.p - 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.p = 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.p, 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.p = func; - ci->nresults = nret; - ci->callstatus = mask; - ci->top.p = 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.p + LUA_MINSTACK); - lua_assert(ci->top.p <= L->stack_last.p); - if (l_unlikely(L->hookmask & LUA_MASKCALL)) { - int narg = cast_int(L->top.p - 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.p -= delta; /* restore 'func' (if vararg) */ - for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func.p + i, func + i); - func = ci->func.p; /* moved-down function */ - for (; narg1 <= nfixparams; narg1++) - setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top.p = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top.p <= L->stack_last.p); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus |= CIST_TAIL; - L->top.p = 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.p - 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.p++)); /* complete missing arguments */ - lua_assert(ci->top.p <= L->stack_last.p); - 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). -** This function can be called with some use of EXTRA_STACK, so it should -** check the stack before doing anything else. 'luaD_precall' already -** does that. -*/ -l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { - CallInfo *ci; - L->nCcalls += inc; - if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { - checkstackp(L, 0, func); /* free any use of EXTRA_STACK */ - 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' */ - func = luaF_close(L, func, status, 1); /* can yield or raise an error */ - 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.p -= narg; /* remove args from the stack */ - setsvalue2s(L, L->top.p, 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.p - 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.p = 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.p - (L->ci->func.p + 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.p); /* push error message */ - L->ci->top.p = L->top.p; - } - *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield - : cast_int(L->top.p - (L->ci->func.p + 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.p), 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/06/deps/lua-5.4.6/src/ldo.h b/06/deps/lua-5.4.6/src/ldo.h deleted file mode 100644 index 1aa446a..0000000 --- a/06/deps/lua-5.4.6/src/ldo.h +++ /dev/null @@ -1,88 +0,0 @@ -/* -** $Id: ldo.h $ -** Stack and Call structure of Lua -** See Copyright Notice in lua.h -*/ - -#ifndef ldo_h -#define ldo_h - - -#include "llimits.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.p - L->top.p <= (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,pt) (cast_charp(pt) - cast_charp(L->stack.p)) -#define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n)) - - -/* macro to check stack size, preserving 'p' */ -#define checkstackp(L,n,p) \ - luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \ - p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ - - -/* macro to check stack size and GC, 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/06/deps/lua-5.4.6/src/ldump.c b/06/deps/lua-5.4.6/src/ldump.c deleted file mode 100644 index f231691..0000000 --- a/06/deps/lua-5.4.6/src/ldump.c +++ /dev/null @@ -1,230 +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 - -#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); -} - - -/* -** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6" -** rounds up the division.) -*/ -#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) - -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/06/deps/lua-5.4.6/src/lfunc.c b/06/deps/lua-5.4.6/src/lfunc.c deleted file mode 100644 index 0945f24..0000000 --- a/06/deps/lua-5.4.6/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.p = &uv->u.value; /* make it closed */ - setnilvalue(uv->v.p); - 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, StkId level, UpVal **prev) { - GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); - UpVal *uv = gco2upv(o); - UpVal *next = *prev; - uv->v.p = s2v(level); /* current value lives in the stack */ - 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, 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.p; - 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.p = 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.p); /* 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.p->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.p); - 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.p) > MAXDELTA) { - L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */ - L->tbclist.p->tbclist.delta = 0; - } - level->tbclist.delta = cast(unsigned short, level - L->tbclist.p); - L->tbclist.p = 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.p); - luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ - setobj(L, slot, uv->v.p); /* move value to upvalue slot */ - uv->v.p = 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 first element from the tbclist plus its dummy nodes. -*/ -static void poptbclist (lua_State *L) { - StkId tbc = L->tbclist.p; - lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ - tbc -= tbc->tbclist.delta; - while (tbc > L->stack.p && tbc->tbclist.delta == 0) - tbc -= MAXDELTA; /* remove dummy nodes */ - L->tbclist.p = tbc; -} - - -/* -** Close all upvalues and to-be-closed variables up to the given stack -** level. Return restored 'level'. -*/ -StkId 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.p >= level) { /* traverse tbc's down to that level */ - StkId tbc = L->tbclist.p; /* get variable index */ - poptbclist(L); /* remove it from list */ - prepcallclosemth(L, tbc, status, yy); /* close variable */ - level = restorestack(L, levelrel); - } - return level; -} - - -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/06/deps/lua-5.4.6/src/lfunc.h b/06/deps/lua-5.4.6/src/lfunc.h deleted file mode 100644 index 3be265e..0000000 --- a/06/deps/lua-5.4.6/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.p != &(up)->u.value) - - -#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p)) - - -/* -** 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 StkId 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/06/deps/lua-5.4.6/src/lgc.c b/06/deps/lua-5.4.6/src/lgc.c deleted file mode 100644 index a3094ff..0000000 --- a/06/deps/lua-5.4.6/src/lgc.c +++ /dev/null @@ -1,1739 +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, size, and offset) -** and link it to 'allgc' list. -*/ -GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { - global_State *g = G(L); - char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); - GCObject *o = cast(GCObject *, p + offset); - o->marked = luaC_white(g); - o->tt = tt; - o->next = g->allgc; - g->allgc = o; - return o; -} - - -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { - return luaC_newobjdt(L, tt, sz, 0); -} - -/* }====================================================== */ - - - -/* -** {====================================================== -** 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.p); /* 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.p); /* 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.p; - 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.p; 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.p + 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.p - 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.p++, tm); /* push finalizer... */ - setobj2s(L, L->top.p++, &v); /* ... and its argument */ - L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 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.p--; /* 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 -** ======================================================= -*/ - - -/* -** 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); -} - - -/* -** 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); -} - - -/* -** 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)); -} - - -/* -** 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); - setminordebt(g); /* set debt assuming next cycle will be minor */ - 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); -} - - -/* -** 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. */ - lua_assert(g->lastatomic == 0); - } - 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 -** ======================================================= -*/ - - -/* -** 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. (If collector is -** not running, set a reasonable debt to avoid it being called at -** every single check.) -*/ -void luaC_step (lua_State *L) { - global_State *g = G(L); - if (!gcrunning(g)) /* not running? */ - luaE_setdebt(g, -2000); - else { - 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/06/deps/lua-5.4.6/src/lgc.h b/06/deps/lua-5.4.6/src/lgc.h deleted file mode 100644 index 538f6ed..0000000 --- a/06/deps/lua-5.4.6/src/lgc.h +++ /dev/null @@ -1,202 +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_objbarrier(L,p,o) ( \ - (isblack(p) && iswhite(o)) ? \ - luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) - -#define luaC_barrier(L,p,v) ( \ - iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0)) - -#define luaC_objbarrierback(L,p,o) ( \ - (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0)) - -#define luaC_barrierback(L,p,v) ( \ - iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : 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 GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, - size_t 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_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); - - -#endif diff --git a/06/deps/lua-5.4.6/src/linit.c b/06/deps/lua-5.4.6/src/linit.c deleted file mode 100644 index 69808f8..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/liolib.c b/06/deps/lua-5.4.6/src/liolib.c deleted file mode 100644 index b08397d..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/ljumptab.h b/06/deps/lua-5.4.6/src/ljumptab.h deleted file mode 100644 index 8306f25..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/llex.c b/06/deps/lua-5.4.6/src/llex.c deleted file mode 100644 index 5fc39a5..0000000 --- a/06/deps/lua-5.4.6/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 readily 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.p++); /* 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.p--; /* 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/06/deps/lua-5.4.6/src/llex.h b/06/deps/lua-5.4.6/src/llex.h deleted file mode 100644 index 389d2f8..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/llimits.h b/06/deps/lua-5.4.6/src/llimits.h deleted file mode 100644 index 1c826f7..0000000 --- a/06/deps/lua-5.4.6/src/llimits.h +++ /dev/null @@ -1,380 +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. (In strict ISO C this may cause undefined behavior, but no -** actual machine seems to bother.) -*/ -#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ - __STDC_VERSION__ >= 199901L -#include -#if defined(UINTPTR_MAX) /* even in C99 this type is optional */ -#define L_P2I uintptr_t -#else /* no 'intptr'? */ -#define L_P2I uintmax_t /* use the largest available integer */ -#endif -#else /* C89 option */ -#define L_P2I size_t -#endif - -#define point2uint(p) ((unsigned int)((L_P2I)(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/06/deps/lua-5.4.6/src/lmathlib.c b/06/deps/lua-5.4.6/src/lmathlib.c deleted file mode 100644 index d0b1e1e..0000000 --- a/06/deps/lua-5.4.6/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_Unsigned' 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/06/deps/lua-5.4.6/src/lmem.c b/06/deps/lua-5.4.6/src/lmem.c deleted file mode 100644 index 9800a86..0000000 --- a/06/deps/lua-5.4.6/src/lmem.c +++ /dev/null @@ -1,215 +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" - - - -/* -** 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. -*/ - - -/* -** Macro to call the allocation function. -*/ -#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) - - -/* -** When an allocation fails, it will try again after an emergency -** collection, except when it cannot run a collection. The GC should -** not be called while the 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. -*/ -#define cantryagain(g) (completestate(g) && !g->gcstopem) - - - - -#if defined(EMERGENCYGCTESTS) -/* -** First allocation will fail except when freeing a block (frees never -** fail) and when it cannot try again; 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 (ns > 0 && cantryagain(g)) - return NULL; /* fail */ - else /* normal allocation */ - return callfrealloc(g, block, os, ns); -} -#else -#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns) -#endif - - - - - -/* -** {================================================================== -** 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)); - callfrealloc(g, 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. -*/ -static void *tryagain (lua_State *L, void *block, - size_t osize, size_t nsize) { - global_State *g = G(L); - if (cantryagain(g)) { - luaC_fullgc(L, 1); /* try to free some memory... */ - return callfrealloc(g, block, osize, nsize); /* try again */ - } - else return NULL; /* cannot run an emergency collection */ -} - - -/* -** 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/06/deps/lua-5.4.6/src/lmem.h b/06/deps/lua-5.4.6/src/lmem.h deleted file mode 100644 index 8c75a44..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/loadlib.c b/06/deps/lua-5.4.6/src/loadlib.c deleted file mode 100644 index d792dff..0000000 --- a/06/deps/lua-5.4.6/src/loadlib.c +++ /dev/null @@ -1,767 +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/06/deps/lua-5.4.6/src/lobject.c b/06/deps/lua-5.4.6/src/lobject.c deleted file mode 100644 index f73ffc6..0000000 --- a/06/deps/lua-5.4.6/src/lobject.c +++ /dev/null @@ -1,602 +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_shiftr(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'. It should be -** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, -** so that 'luaG_addinfo' can work directly on the buffer. -*/ -#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95) - -/* buffer used by 'luaO_pushvfstring' */ -typedef struct BuffFS { - lua_State *L; - int pushed; /* true if there is a part of the result 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 result, and -** join it to previous partial result if there is one. -** It may call 'luaV_concat' while using one slot from EXTRA_STACK. -** This call cannot invoke metamethods, as both operands must be -** strings. It can, however, raise an error if the result is too -** long. In that case, 'luaV_concat' frees the extra slot before -** raising the error. -*/ -static void pushstr (BuffFS *buff, const char *str, size_t lstr) { - lua_State *L = buff->L; - setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr)); - L->top.p++; /* may use one slot from EXTRA_STACK */ - if (!buff->pushed) /* no previous string on the stack? */ - buff->pushed = 1; /* now there is one */ - else /* join previous string with new one */ - luaV_concat(L, 2); -} - - -/* -** 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 numeral 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.p - 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/06/deps/lua-5.4.6/src/lobject.h b/06/deps/lua-5.4.6/src/lobject.h deleted file mode 100644 index 556608e..0000000 --- a/06/deps/lua-5.4.6/src/lobject.h +++ /dev/null @@ -1,815 +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 */ - /* not used, but may avoid warnings for uninitialized value */ - lu_byte ub; -} 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; - - -/* -** When reallocating the stack, change all pointers to the stack into -** proper offsets. -*/ -typedef union { - StkId p; /* actual pointer */ - ptrdiff_t offset; /* used while the stack is being reallocated */ -} StkIdRel; - - -/* 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; - union { - TValue *p; /* points to stack or to its own value */ - ptrdiff_t offset; /* used while the stack is being reallocated */ - } v; - 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/06/deps/lua-5.4.6/src/lopcodes.c b/06/deps/lua-5.4.6/src/lopcodes.c deleted file mode 100644 index c67aa22..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lopcodes.h b/06/deps/lua-5.4.6/src/lopcodes.h deleted file mode 100644 index 4c55145..0000000 --- a/06/deps/lua-5.4.6/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 (signed)(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/06/deps/lua-5.4.6/src/lopnames.h b/06/deps/lua-5.4.6/src/lopnames.h deleted file mode 100644 index 965cec9..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/loslib.c b/06/deps/lua-5.4.6/src/loslib.c deleted file mode 100644 index ad5a927..0000000 --- a/06/deps/lua-5.4.6/src/loslib.c +++ /dev/null @@ -1,428 +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) /* { */ - -#if defined(LUA_USE_WINDOWS) -#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ -#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */ -#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%" -#else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ - "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ -#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 /* } */ -/* }================================================================== */ - - -#if !defined(l_system) -#if defined(LUA_USE_IOS) -/* Despite claiming to be ISO C, iOS does not implement 'system'. */ -#define l_system(cmd) ((cmd) == NULL ? 0 : -1) -#else -#define l_system(cmd) system(cmd) /* default definition */ -#endif -#endif - - -static int os_execute (lua_State *L) { - const char *cmd = luaL_optstring(L, 1, NULL); - int stat; - errno = 0; - stat = l_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 { - if (!(res >= 0 ? res - delta <= INT_MAX : 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/06/deps/lua-5.4.6/src/lparser.c b/06/deps/lua-5.4.6/src/lparser.c deleted file mode 100644 index b745f23..0000000 --- a/06/deps/lua-5.4.6/src/lparser.c +++ /dev/null @@ -1,1967 +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 */ - luaK_exp2anyregup(fs, var); /* but could be a constant */ - 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 gotos. -** 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 gotos */ - 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 gotos 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 gotos 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 */ - removevars(fs, bl->nactvar); /* remove block locals */ - lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ - if (bl->isloop) /* has to fix pending breaks? */ - hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); - if (!hasclose && bl->previous && bl->upval) /* still need a 'close'? */ - luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); - fs->freereg = stklevel; /* free registers */ - ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - fs->bl = bl->previous; /* current block now is previous one */ - if (bl->previous) /* was it a nested block? */ - movegotosout(fs, bl); /* update pending gotos to enclosing block */ - else { - if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ - 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.p, cl); /* anchor it (to avoid being collected) */ - luaD_inctop(L); - lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue2s(L, L->top.p, 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.p--; /* remove scanner's table */ - return cl; /* closure is on the stack, too */ -} - diff --git a/06/deps/lua-5.4.6/src/lparser.h b/06/deps/lua-5.4.6/src/lparser.h deleted file mode 100644 index 5e4500f..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lprefix.h b/06/deps/lua-5.4.6/src/lprefix.h deleted file mode 100644 index 484f2ad..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lstate.c b/06/deps/lua-5.4.6/src/lstate.c deleted file mode 100644 index 1e925e5..0000000 --- a/06/deps/lua-5.4.6/src/lstate.c +++ /dev/null @@ -1,445 +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.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - L1->tbclist.p = L1->stack.p; - for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) - setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ - L1->top.p = L1->stack.p; - L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; - /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - ci->callstatus = CIST_C; - ci->func.p = L1->top.p; - ci->u.c.k = NULL; - ci->nresults = 0; - setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ - L1->top.p++; - ci->top.p = L1->top.p + LUA_MINSTACK; - L1->ci = ci; -} - - -static void freestack (lua_State *L) { - if (L->stack.p == 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.p, 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.p = 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 = G(L); - GCObject *o; - lua_State *L1; - lua_lock(L); - luaC_checkGC(L); - /* create new thread */ - o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); - L1 = gco2th(o); - /* anchor it on L stack */ - setthvalue2s(L, L->top.p, 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.p); /* 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.p)); /* 'function' entry for basic 'ci' */ - ci->func.p = L->stack.p; - 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.p + 1); - else - L->top.p = L->stack.p + 1; - ci->top.p = L->top.p + LUA_MINSTACK; - luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0); - return status; -} - - -LUA_API int lua_closethread (lua_State *L, lua_State *from) { - int status; - lua_lock(L); - L->nCcalls = (from) ? getCcalls(from) : 0; - status = luaE_resetthread(L, L->status); - lua_unlock(L); - return status; -} - - -/* -** Deprecated! Use 'lua_closethread' instead. -*/ -LUA_API int lua_resetthread (lua_State *L) { - return lua_closethread(L, NULL); -} - - -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.p - 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/06/deps/lua-5.4.6/src/lstate.h b/06/deps/lua-5.4.6/src/lstate.h deleted file mode 100644 index 8bf6600..0000000 --- a/06/deps/lua-5.4.6/src/lstate.h +++ /dev/null @@ -1,409 +0,0 @@ -/* -** $Id: lstate.h $ -** Global State -** See Copyright Notice in lua.h -*/ - -#ifndef lstate_h -#define lstate_h - -#include "lua.h" - - -/* Some header files included here need this definition */ -typedef struct CallInfo CallInfo; - - -#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.p - (th)->stack.p) - - -/* 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. -*/ -struct CallInfo { - StkIdRel func; /* function index in the stack */ - StkIdRel 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; -}; - - -/* -** 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_NUMTYPES]; /* 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 */ - StkIdRel top; /* first free slot in the stack */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - StkIdRel stack_last; /* end of stack (last element + 1) */ - StkIdRel stack; /* stack base */ - UpVal *openupval; /* list of open upvalues in this stack */ - StkIdRel 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/06/deps/lua-5.4.6/src/lstring.c b/06/deps/lua-5.4.6/src/lstring.c deleted file mode 100644 index 13dcaf4..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lstring.h b/06/deps/lua-5.4.6/src/lstring.h deleted file mode 100644 index 450c239..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lstrlib.c b/06/deps/lua-5.4.6/src/lstrlib.c deleted file mode 100644 index 0316716..0000000 --- a/06/deps/lua-5.4.6/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 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/06/deps/lua-5.4.6/src/ltable.c b/06/deps/lua-5.4.6/src/ltable.c deleted file mode 100644 index 3c690c5..0000000 --- a/06/deps/lua-5.4.6/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 <= cast_uint(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); -#if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */ - size |= (size >> 16); -#if (UINT_MAX >> 30) > 3 - size |= (size >> 32); /* unsigned int has more than 32 bits */ -#endif -#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 < cast_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); -} - -#endif diff --git a/06/deps/lua-5.4.6/src/ltable.h b/06/deps/lua-5.4.6/src/ltable.h deleted file mode 100644 index 75dd9e2..0000000 --- a/06/deps/lua-5.4.6/src/ltable.h +++ /dev/null @@ -1,65 +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); -#endif - - -#endif diff --git a/06/deps/lua-5.4.6/src/ltablib.c b/06/deps/lua-5.4.6/src/ltablib.c deleted file mode 100644 index e6bc4d0..0000000 --- a/06/deps/lua-5.4.6/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, 2, - "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/06/deps/lua-5.4.6/src/ltm.c b/06/deps/lua-5.4.6/src/ltm.c deleted file mode 100644 index 07a0608..0000000 --- a/06/deps/lua-5.4.6/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.p; - 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.p = 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.p; - 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.p += 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.p); /* 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.p; - 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.p, event)) /* try original event */ - return !l_isfalse(s2v(L->top.p)); -#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.p, TM_LT)) { - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - return l_isfalse(s2v(L->top.p)); - } - /* 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.p - ci->func.p) - 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.p++, ci->func.p); - /* move fixed parameters to the top of the stack */ - for (i = 1; i <= nfixparams; i++) { - setobjs2s(L, L->top.p++, ci->func.p + i); - setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ - } - ci->func.p += actual + 1; - ci->top.p += actual + 1; - lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); -} - - -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.p = where + nextra; /* next instruction will need top */ - } - for (i = 0; i < wanted && i < nextra; i++) - setobjs2s(L, where + i, ci->func.p - nextra + i); - for (; i < wanted; i++) /* complete required results with nil */ - setnilvalue(s2v(where + i)); -} - diff --git a/06/deps/lua-5.4.6/src/ltm.h b/06/deps/lua-5.4.6/src/ltm.h deleted file mode 100644 index c309e2a..0000000 --- a/06/deps/lua-5.4.6/src/ltm.h +++ /dev/null @@ -1,104 +0,0 @@ -/* -** $Id: ltm.h $ -** Tag methods -** See Copyright Notice in lua.h -*/ - -#ifndef ltm_h -#define ltm_h - - -#include "lobject.h" -#include "lstate.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, - CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci, - StkId where, int wanted); - - -#endif diff --git a/06/deps/lua-5.4.6/src/lua.c b/06/deps/lua-5.4.6/src/lua.c deleted file mode 100644 index 0ff8845..0000000 --- a/06/deps/lua-5.4.6/src/lua.c +++ /dev/null @@ -1,679 +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. -** (If there is no interpreter's name either, 'script' is -1, so -** table sizes are zero.) -*/ -static void createargtable (lua_State *L, char **argv, int argc, int script) { - int i, narg; - 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. In case of error, 'first' is the index of the bad -** argument. Otherwise, 'first' is -1 if there is no program name, -** 0 if there is no script name, or the index of the script name. -*/ -static int collectargs (char **argv, int *first) { - int args = 0; - int i; - if (argv[0] != NULL) { /* is there a program name? */ - if (argv[0][0]) /* not empty? */ - progname = argv[0]; /* save it */ - } - else { /* no program name */ - *first = -1; - return 0; - } - for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ - *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 = 0; /* 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); - int optlim = (script > 0) ? script : argc; /* first argv not an option */ - luaL_checkversion(L); /* check that interpreter has correct version */ - 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_GCRESTART); /* start GC... */ - lua_gc(L, LUA_GCGEN, 0, 0); /* ...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, optlim)) /* execute arguments -e and -l */ - return 0; /* something failed */ - if (script > 0) { /* execute main script (if there is one) */ - if (handle_script(L, argv + script) != LUA_OK) - return 0; /* interrupt in case of error */ - } - if (args & has_i) /* -i option? */ - doREPL(L); /* do read-eval-print loop */ - else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ - 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_gc(L, LUA_GCSTOP); /* stop GC while building state */ - 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/06/deps/lua-5.4.6/src/lua.h b/06/deps/lua-5.4.6/src/lua.h deleted file mode 100644 index fd16cf8..0000000 --- a/06/deps/lua-5.4.6/src/lua.h +++ /dev/null @@ -1,523 +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 "6" - -#define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 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-2023 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); - - -/* -** Type used by the debug API to collect debug information -*/ -typedef struct lua_Debug lua_Debug; - - -/* -** Functions to be called by the debugger in specific events -*/ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - - -/* -** 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_closethread) (lua_State *L, lua_State *from); -LUA_API int (lua_resetthread) (lua_State *L); /* Deprecated! */ - -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) - - -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-2023 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/06/deps/lua-5.4.6/src/lua.hpp b/06/deps/lua-5.4.6/src/lua.hpp deleted file mode 100644 index ec417f5..0000000 --- a/06/deps/lua-5.4.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/06/deps/lua-5.4.6/src/luac.c b/06/deps/lua-5.4.6/src/luac.c deleted file mode 100644 index 5f4a141..0000000 --- a/06/deps/lua-5.4.6/src/luac.c +++ /dev/null @@ -1,723 +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.p+(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; - } - 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/06/deps/lua-5.4.6/src/luaconf.h b/06/deps/lua-5.4.6/src/luaconf.h deleted file mode 100644 index 137103e..0000000 --- a/06/deps/lua-5.4.6/src/luaconf.h +++ /dev/null @@ -1,793 +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 - - -#if defined(LUA_USE_IOS) -#define LUA_USE_POSIX -#define LUA_USE_DLOPEN -#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 and max(int)/2.) -*/ -#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 initial 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/06/deps/lua-5.4.6/src/lualib.h b/06/deps/lua-5.4.6/src/lualib.h deleted file mode 100644 index 2625529..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lundump.c b/06/deps/lua-5.4.6/src/lundump.c deleted file mode 100644 index 02aed64..0000000 --- a/06/deps/lua-5.4.6/src/lundump.c +++ /dev/null @@ -1,335 +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.p, ts); /* anchor it ('loadVector' can GC) */ - luaD_inctop(L); - loadVector(S, getstr(ts), size); /* load directly in final place */ - L->top.p--; /* 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); - if (n != 0) /* does it have debug information? */ - n = f->sizeupvalues; /* must be this many */ - 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.p, 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/06/deps/lua-5.4.6/src/lundump.h b/06/deps/lua-5.4.6/src/lundump.h deleted file mode 100644 index f3748a9..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lutf8lib.c b/06/deps/lua-5.4.6/src/lutf8lib.c deleted file mode 100644 index 3a5b9bc..0000000 --- a/06/deps/lua-5.4.6/src/lutf8lib.c +++ /dev/null @@ -1,291 +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 - - -#define MSGInvalid "invalid UTF-8 code" - -/* -** 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(c) (((c) & 0xC0) == 0x80) -#define iscontp(p) iscont(*(p)) - - -/* 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 (!iscont(cc)) /* 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, MSGInvalid); - 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 && iscontp(s + posi)) posi--; - } - else { - if (iscontp(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 && iscontp(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 (iscontp(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 (iscontp(s + n)) n++; /* go to next character */ - } - 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 || iscontp(next)) - return luaL_error(L, MSGInvalid); - 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); - const char *s = luaL_checkstring(L, 1); - luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); - 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/06/deps/lua-5.4.6/src/lvm.c b/06/deps/lua-5.4.6/src/lvm.c deleted file mode 100644 index 8493a77..0000000 --- a/06/deps/lua-5.4.6/src/lvm.c +++ /dev/null @@ -1,1901 +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.p); /* call TM */ - return !l_isfalse(s2v(L->top.p)); - } -} - - -/* 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.p - total' up to 'L->top.p - 1'. -*/ -void luaV_concat (lua_State *L, int total) { - if (total == 1) - return; /* "all" values already concatenated */ - do { - StkId top = L->top.p; - 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); /* may invalidate 'top' */ - 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)) { - L->top.p = top - total; /* pop strings to avoid wasting stack */ - 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 one new */ - L->top.p -= 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'.) -*/ -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.p + 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.p); - 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.p); - 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.p - 1)); - L->top.p--; -#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.p - 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.p = 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.p = 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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) { \ - StkId ra = RA(i); \ - 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.p + 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.p = ci->top.p) - - -/* -** 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.p = (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++); \ -} - -#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.p)); - 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.p + 1; - /* main loop of interpreter */ - for (;;) { - Instruction i; /* instruction being executed */ - 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.p + 1); - lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); - /* invalidate top for instructions not expecting it */ - lua_assert(isIT(i) || (cast_void(L->top.p = base), 1)); - vmdispatch (GET_OPCODE(i)) { - vmcase(OP_MOVE) { - StkId ra = RA(i); - setobjs2s(L, ra, RB(i)); - vmbreak; - } - vmcase(OP_LOADI) { - StkId ra = RA(i); - lua_Integer b = GETARG_sBx(i); - setivalue(s2v(ra), b); - vmbreak; - } - vmcase(OP_LOADF) { - StkId ra = RA(i); - int b = GETARG_sBx(i); - setfltvalue(s2v(ra), cast_num(b)); - vmbreak; - } - vmcase(OP_LOADK) { - StkId ra = RA(i); - TValue *rb = k + GETARG_Bx(i); - setobj2s(L, ra, rb); - vmbreak; - } - vmcase(OP_LOADKX) { - StkId ra = RA(i); - TValue *rb; - rb = k + GETARG_Ax(*pc); pc++; - setobj2s(L, ra, rb); - vmbreak; - } - vmcase(OP_LOADFALSE) { - StkId ra = RA(i); - setbfvalue(s2v(ra)); - vmbreak; - } - vmcase(OP_LFALSESKIP) { - StkId ra = RA(i); - setbfvalue(s2v(ra)); - pc++; /* skip next instruction */ - vmbreak; - } - vmcase(OP_LOADTRUE) { - StkId ra = RA(i); - setbtvalue(s2v(ra)); - vmbreak; - } - vmcase(OP_LOADNIL) { - StkId ra = RA(i); - int b = GETARG_B(i); - do { - setnilvalue(s2v(ra++)); - } while (b--); - vmbreak; - } - vmcase(OP_GETUPVAL) { - StkId ra = RA(i); - int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v.p); - vmbreak; - } - vmcase(OP_SETUPVAL) { - StkId ra = RA(i); - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v.p, s2v(ra)); - luaC_barrier(L, uv, s2v(ra)); - vmbreak; - } - vmcase(OP_GETTABUP) { - StkId ra = RA(i); - const TValue *slot; - TValue *upval = cl->upvals[GETARG_B(i)]->v.p; - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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.p; - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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.p = 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) { - StkId ra = RA(i); - 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) { - savestate(L, ci); /* in case of division by 0 */ - 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) { - savestate(L, ci); /* in case of division by 0 */ - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - savestate(L, ci); /* in case of division by 0 */ - 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 */ - savestate(L, ci); /* in case of division by 0 */ - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - TValue *rb = vRB(i); - if (l_isfalse(rb)) - setbtvalue(s2v(ra)); - else - setbfvalue(s2v(ra)); - vmbreak; - } - vmcase(OP_LEN) { - StkId ra = RA(i); - Protect(luaV_objlen(L, ra, vRB(i))); - vmbreak; - } - vmcase(OP_CONCAT) { - StkId ra = RA(i); - int n = GETARG_B(i); /* number of elements to concatenate */ - L->top.p = ra + n; /* mark the end of concat operands */ - ProtectNT(luaV_concat(L, n)); - checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ - vmbreak; - } - vmcase(OP_CLOSE) { - StkId ra = RA(i); - Protect(luaF_close(L, ra, LUA_OK, 1)); - vmbreak; - } - vmcase(OP_TBC) { - StkId ra = RA(i); - /* create new to-be-closed upvalue */ - halfProtect(luaF_newtbcupval(L, ra)); - vmbreak; - } - vmcase(OP_JMP) { - dojump(ci, i, 0); - vmbreak; - } - vmcase(OP_EQ) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - int cond = !l_isfalse(s2v(ra)); - docondjump(); - vmbreak; - } - vmcase(OP_TESTSET) { - StkId ra = RA(i); - TValue *rb = vRB(i); - if (l_isfalse(rb) == GETARG_k(i)) - pc++; - else { - setobj2s(L, ra, rb); - donextjump(ci); - } - vmbreak; - } - vmcase(OP_CALL) { - StkId ra = RA(i); - CallInfo *newci; - int b = GETARG_B(i); - int nresults = GETARG_C(i) - 1; - if (b != 0) /* fixed number of arguments? */ - L->top.p = 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) { - StkId ra = RA(i); - 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.p = ra + b; - else /* previous instruction set top */ - b = cast_int(L->top.p - 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.p < base); /* no pending tbc variables */ - lua_assert(base == ci->func.p + 1); - } - if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ - goto startfunc; /* execute the callee */ - else { /* C function? */ - ci->func.p -= 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) { - StkId ra = RA(i); - int n = GETARG_B(i) - 1; /* number of results */ - int nparams1 = GETARG_C(i); - if (n < 0) /* not fixed? */ - n = cast_int(L->top.p - 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.p < ci->top.p) - L->top.p = ci->top.p; - luaF_close(L, base, CLOSEKTOP, 1); - updatetrap(ci); - updatestack(ci); - } - if (nparams1) /* vararg function? */ - ci->func.p -= ci->u.l.nextraargs + nparams1; - L->top.p = 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)) { - StkId ra = RA(i); - L->top.p = 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.p = base - 1; - for (nres = ci->nresults; l_unlikely(nres > 0); nres--) - setnilvalue(s2v(L->top.p++)); /* all results are nil */ - } - goto ret; - } - vmcase(OP_RETURN1) { - if (l_unlikely(L->hookmask)) { - StkId ra = RA(i); - L->top.p = 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.p = base - 1; /* asked for no results */ - else { - StkId ra = RA(i); - setobjs2s(L, base - 1, ra); /* at least this result */ - L->top.p = base; - for (; l_unlikely(nres > 1); nres--) - setnilvalue(s2v(L->top.p++)); /* 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) { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - savestate(L, ci); /* in case of errors */ - if (forprep(L, ra)) - pc += GETARG_Bx(i) + 1; /* skip the loop */ - vmbreak; - } - vmcase(OP_TFORPREP) { - StkId ra = RA(i); - /* 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: { - StkId ra = RA(i); - /* '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.p = 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: { - StkId ra = RA(i); - 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) { - StkId ra = RA(i); - int n = GETARG_B(i); - unsigned int last = GETARG_C(i); - Table *h = hvalue(s2v(ra)); - if (n == 0) - n = cast_int(L->top.p - ra) - 1; /* get up to the top */ - else - L->top.p = ci->top.p; /* 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) { - StkId ra = RA(i); - Proto *p = cl->p->p[GETARG_Bx(i)]; - halfProtect(pushclosure(L, p, cl->upvals, base, ra)); - checkGC(L, ra + 1); - vmbreak; - } - vmcase(OP_VARARG) { - StkId ra = RA(i); - 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/06/deps/lua-5.4.6/src/lvm.h b/06/deps/lua-5.4.6/src/lvm.h deleted file mode 100644 index dba1ad2..0000000 --- a/06/deps/lua-5.4.6/src/lvm.h +++ /dev/null @@ -1,141 +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); } - - -/* -** Shift right is the same as shift left with a negative 'y' -*/ -#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) - - - -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/06/deps/lua-5.4.6/src/lzio.c b/06/deps/lua-5.4.6/src/lzio.c deleted file mode 100644 index cd0a02d..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/lua-5.4.6/src/lzio.h b/06/deps/lua-5.4.6/src/lzio.h deleted file mode 100644 index 38f397f..0000000 --- a/06/deps/lua-5.4.6/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/06/deps/parser-gen/LICENSE b/06/deps/parser-gen/LICENSE deleted file mode 100644 index e812781..0000000 --- a/06/deps/parser-gen/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2017 Benas Vaitkevicius - -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. \ No newline at end of file diff --git a/06/deps/parser-gen/README.md b/06/deps/parser-gen/README.md deleted file mode 100644 index 092c79c..0000000 --- a/06/deps/parser-gen/README.md +++ /dev/null @@ -1,397 +0,0 @@ -# parser-gen - -A Lua parser generator that makes it possible to describe grammars in a [PEG](https://en.wikipedia.org/wiki/Parsing_expression_grammar) syntax. The tool will parse a given input using a provided grammar and if the matching is successful produce an AST as an output with the captured values using [Lpeg](http://www.inf.puc-rio.br/~roberto/lpeg/). If the matching fails, labelled errors can be used in the grammar to indicate failure position, and recovery grammars are generated to continue parsing the input using [LpegLabel](https://github.com/sqmedeiros/lpeglabel). The tool can also automatically generate error labels and recovery grammars for LL(1) grammars. - -parser-gen is a [GSoC 2017](https://developers.google.com/open-source/gsoc/) project, and was completed with the help of my mentor [@sqmedeiros](https://github.com/sqmedeiros) from [LabLua](http://www.lua.inf.puc-rio.br/). A blog documenting the progress of the project can be found [here](https://parsergen.blogspot.com/2017/08/parser-generator-based-on-lpeglabel.html). - ---- -# Table of contents - -* [Requirements](#requirements) - -* [Syntax](#syntax) - -* [Grammar Syntax](#grammar-syntax) - -* [Example: Tiny Parser](#example-tiny-parser) - -# Requirements -``` -lua >= 5.1 -lpeglabel >= 1.2.0 -``` -# Syntax - -### compile - -This function generates a PEG parser from the grammar description. - -```lua -local pg = require "parser-gen" -grammar = pg.compile(input,definitions [, errorgen, noast]) -``` -*Arguments*: - -`input` - A string containing a PEG grammar description. For complete PEG syntax see the grammar section of this document. - -`definitions` - table of custom functions and definitions used inside the grammar, for example {equals=equals}, where equals is a function. - -`errorgen` - **EXPERIMENTAL** optional boolean parameter(default:false), when enabled generates error labels automatically. Works well only on LL(1) grammars. Custom error labels have precedence over automatically generated ones. - -`noast` - optional boolean parameter(default:false), when enabled does not generate an AST for the parse. - -*Output*: - -`grammar` - a compiled grammar on success, throws error on failure. - -### setlabels - -If custom error labels are used, the function *setlabels* allows setting their description (and custom recovery pattern): -```lua -pg.setlabels(t) -``` -Example table of a simple error and one with a custom recovery expression: -```lua --- grammar rule: " ifexp <- 'if' exp 'then'^missingThen stmt 'end'^missingEnd " -local t = { - missingEnd = "Missing 'end' in if expression", - missingThen = {"Missing 'then' in if expression", " (!stmt .)* "} -- a custom recovery pattern -} -pg.setlabels(t) -``` -If the recovery pattern is not set, then the one specified by the rule SYNC will be used. It is by default set to: -```lua -SKIP <- %s / %nl -- a space ' ' or newline '\n' character -SYNC <- .? (!SKIP .)* -``` -Learn more about special rules in the grammar section. - -### parse - -This operation attempts to match a grammar to the given input. - -```lua -result, errors = pg.parse(input, grammar [, errorfunction]) -``` -*Arguments*: - -`input` - an input string that the tool will attempt to parse. - -`grammar` - a compiled grammar. - -`errorfunction` - an optional function that will be called if an error is encountered, with the arguments `desc` for the error description set using `setlabels()`; location indicators `line` and `col`; the remaining string before failure `sfail` and a custom recovery expression `trec` if available. -Example: -```lua -local errs = 0 -local function printerror(desc,line,col,sfail,trec) - errs = errs+1 - print("Error #"..errs..": "..desc.." before '"..sfail.."' on line "..line.."(col "..col..")") -end - -result, errors = pg.parse(input,grammar,printerror) -``` -*Output*: - -If the parse is succesful, the function returns an abstract syntax tree containing the captures `result` and a table of any encountered `errors`. If the parse was unsuccessful, `result` is going to be **nil**. -Also, if the `noast` option is enabled when compiling the grammar, the function will then produce the longest match length or any custom captures used. - -### calcline - -Calculates line and column information regarding position i of the subject (exported from the relabel module). - -```lua -line, col = pg.calcline(subject, position) -``` -*Arguments*: - -`subject` - subject string - -`position` - position inside the string, for example, the one given by automatic AST generation. - -### usenodes - -When AST generation is enabled, this function will enable the "node" mode, where only rules tagged with a `node` prefix will generate AST entries. Must be used before compiling the grammar. - -```lua -pg.usenodes(value) -``` -*Arguments*: - -`value` - a boolean value that enables or disables this function - -# Grammar Syntax - -The grammar used for this tool is described using a PEG-like syntax, that is identical to the one provided by the [re](http://www.inf.puc-rio.br/~roberto/lpeg/re.html) module, with an extension of labelled failures provided by [relabel](https://github.com/sqmedeiros/lpeglabel) module (except numbered labels). That is, all grammars that work with relabel should work with parser-gen as long as numbered error labels are not used, as they are not supported by parser-gen. - -Since a parser generated with parser-gen automatically consumes space characters, builds ASTs and generates errors, additional extensions have been added based on the [ANTLR](http://www.antlr.org/) syntax. - -### Basic syntax - -The syntax of parser-gen grammars is somewhat similar to regex syntax. The next table summarizes the tools syntax. A p represents an arbitrary pattern; num represents a number (`[0-9]+`); name represents an identifier (`[a-zA-Z][a-zA-Z0-9_]*`).`defs` is the definitions table provided when compiling the grammar. Note that error names must be set using `setlabels()` before compiling the grammar. Constructions are listed in order of decreasing precedence. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    SyntaxDescription
    ( p ) grouping
    'string' literal string
    "string" literal string
    [class] character class
    . any character
    %namepattern defs[name] or a pre-defined pattern
    namenon terminal
    <name>non terminal
    %{name} error label
    {} position capture
    { p } simple capture
    {: p :} anonymous group capture
    {:name: p :} named group capture
    {~ p ~} substitution capture
    {| p |} table capture
    =name back reference -
    p ? optional match
    p * zero or more repetitions
    p + one or more repetitions
    p^num exactly n repetitions
    p^+numat least n repetitions
    p^-numat most n repetitions
    p^name match p or throw error label name.
    p -> 'string' string capture
    p -> "string" string capture
    p -> num numbered capture
    p -> name function/query/string capture -equivalent to p / defs[name]
    p => name match-time capture -equivalent to lpeg.Cmt(p, defs[name])
    & p and predicate
    ! p not predicate
    p1 p2 concatenation
    p1 //{name [, name, ...]} p2 specifies recovery pattern p2 for p1 -when one of the labels is thrown
    p1 / p2 ordered choice
    (name <- p)+ grammar
    - - -The grammar below is used to match balanced parenthesis - -```lua -balanced <- "(" ([^()] / balanced)* ")" -``` -For more examples check out the [re](http://www.inf.puc-rio.br/~roberto/lpeg/re.html) page, see the Tiny parser below or the [Lua parser](https://github.com/vsbenas/parser-gen/blob/master/parsers/lua-parser.lua) writen with this tool. - -### Error labels - -Error labels are provided by the relabel function %{errorname} (errorname must follow `[A-Za-z][A-Za-z0-9_]*` format). Usually we use error labels in a syntax like `'a' ('b' / %{errB}) 'c'`, which throws an error label if `'b'` is not matched. This syntax is quite complicated so an additional syntax is allowed `'a' 'b'^errB 'c'`, which allows cleaner description of grammars. Note: all errors must be defined in a table using parser-gen.setlabels() before compiling and parsing the grammar. - -### Tokens - -Non-terminals with names in all capital letters, i.e. `[A-Z]+`, are considered tokens and are treated as a single object in parsing. That is, the whole string matched by a token is captured in a single AST entry and space characters are not consumed. Consider two examples: -```lua --- a token non-terminal -grammar = pg.compile [[ - WORD <- [A-Z]+ -]] -res, _ = pg.parse("AA A", grammar) -- outputs {rule="WORD", "AA"} -``` -```lua --- a non-token non-terminal -grammar = pg.compile [[ - word <- [A-Z]+ -]] -res, _ = pg.parse("AA A", grammar) -- outputs {rule="word", "A", "A", "A"} -``` - -### Fragments - -If a token definition is followed by a `fragment` keyword, then the parser does not build an AST entry for that token. Essentially, these rules are used to simplify grammars without building unnecessarily complicated ASTS. Example of `fragment` usage: -```lua -grammar = pg.compile [[ - WORD <- LETTER+ - fragment LETTER <- [A-Z] -]] -res, _ = pg.parse("AA A", grammar) -- outputs {rule="WORD", "AA"} -``` -Without using `fragment`: -```lua -grammar = pg.compile [[ - WORD <- LETTER+ - LETTER <- [A-Z] -]] -res, _ = pg.parse("AA A", grammar) -- outputs {rule="WORD", {rule="LETTER", "A"}, {rule="LETTER", "A"}} - -``` - -### Nodes - -When node mode is enabled using `pg.usenodes(true)` only rules prefixed with a `node` keyword will generate AST entries: -```lua -grammar = pg.compile [[ - node WORD <- LETTER+ - LETTER <- [A-Z] -]] -res, _ = pg.parse("AA A", grammar) -- outputs {rule="WORD", "AA"} -``` -### Special rules - -There are two special rules used by the grammar: - -#### SKIP - -The `SKIP` rule identifies which characters to skip in a grammar. For example, most programming languages do not take into acount any space or newline characters. By default, SKIP is set to: -```lua -SKIP <- %s / %nl -``` -This rule can be extended to contain semicolons `';'`, comments, or any other patterns that the parser can safely ignore. - -Character skipping can be disabled by using: -```lua -SKIP <- '' -``` - -#### SYNC - -This rule specifies the general recovery expression both for custom errors and automatically generated ones. By default: -```lua -SYNC <- .? (!SKIP .)* -``` -The default SYNC rule consumes any characters until the next character matched by SKIP, usually a space or a newline. That means, if some statement in a program is invalid, the parser will continue parsing after a space or a newline character. - -For some programming languages it might be useful to skip to a semicolon or a keyword, since they usually indicate the end of a statement, so SYNC could be something like: -```lua -HELPER <- ';' / 'end' / SKIP -- etc -SYNC <- (!HELPER .)* SKIP* -- we can consume the spaces after syncing with them as well -``` - -Recovery grammars can be disabled by using: -```lua -SYNC <- '' -``` -# Example: Tiny parser - -Below is the full code from *parsers/tiny-parser.lua*: -```lua -local pg = require "parser-gen" -local peg = require "peg-parser" -local errs = {errMissingThen = "Missing Then"} -- one custom error -pg.setlabels(errs) - ---warning: experimental error generation function is enabled. If the grammar isn't LL(1), set errorgen to false -local errorgen = true - -local grammar = pg.compile([[ - - program <- stmtsequence !. - stmtsequence <- statement (';' statement)* - statement <- ifstmt / repeatstmt / assignstmt / readstmt / writestmt - ifstmt <- 'if' exp 'then'^errMissingThen stmtsequence elsestmt? 'end' - elsestmt <- ('else' stmtsequence) - repeatstmt <- 'repeat' stmtsequence 'until' exp - assignstmt <- IDENTIFIER ':=' exp - readstmt <- 'read' IDENTIFIER - writestmt <- 'write' exp - exp <- simpleexp (COMPARISONOP simpleexp)* - COMPARISONOP <- '<' / '=' - simpleexp <- term (ADDOP term)* - ADDOP <- [+-] - term <- factor (MULOP factor)* - MULOP <- [*/] - factor <- '(' exp ')' / NUMBER / IDENTIFIER - - NUMBER <- '-'? [0-9]+ - KEYWORDS <- 'if' / 'repeat' / 'read' / 'write' / 'then' / 'else' / 'end' / 'until' - RESERVED <- KEYWORDS ![a-zA-Z] - IDENTIFIER <- !RESERVED [a-zA-Z]+ - HELPER <- ';' / %nl / %s / KEYWORDS / !. - SYNC <- (!HELPER .)* - -]], _, errorgen) - -local errors = 0 -local function printerror(desc,line,col,sfail,trec) - errors = errors+1 - print("Error #"..errors..": "..desc.." on line "..line.."(col "..col..")") -end - - -local function parse(input) - errors = 0 - result, errors = pg.parse(input,grammar,printerror) - return result, errors -end - -if arg[1] then - -- argument must be in quotes if it contains spaces - res, errs = parse(arg[1]) - peg.print_t(res) - peg.print_r(errs) -end -local ret = {parse=parse} -return ret -``` -For input: `lua tiny-parser-nocap.lua "if a b:=1"` we get: -```lua -Error #1: Missing Then on line 1(col 6) -Error #2: Expected stmtsequence on line 1(col 9) -Error #3: Expected 'end' on line 1(col 9) --- ast: -rule='program', -pos=1, -{ - rule='stmtsequence', - pos=1, - { - rule='statement', - pos=1, - { - rule='ifstmt', - pos=1, - 'if', - { - rule='exp', - pos=4, - { - rule='simpleexp', - pos=4, - { - rule='term', - pos=4, - { - rule='factor', - pos=4, - { - rule='IDENTIFIER', - pos=4, - 'a', - }, - }, - }, - }, - }, - }, - }, -}, --- error table: -[1] => { - [msg] => 'Missing Then' -- custom error is used over the automatically generated one - [line] => '1' - [col] => '6' - [label] => 'errMissingThen' - } -[2] => { - [msg] => 'Expected stmtsequence' -- automatically generated errors - [line] => '1' - [col] => '9' - [label] => 'errorgen6' - } -[3] => { - [msg] => 'Expected 'end'' - [line] => '1' - [col] => '9' - [label] => 'errorgen4' - } -``` - - diff --git a/06/deps/parser-gen/equals.lua b/06/deps/parser-gen/equals.lua deleted file mode 100644 index 59f8eb9..0000000 --- a/06/deps/parser-gen/equals.lua +++ /dev/null @@ -1,32 +0,0 @@ --- this function compares if two tables are equal -local function equals(o1, o2, ignore_mt) - if o1 == o2 then return true end - local o1Type = type(o1) - local o2Type = type(o2) - if o1Type ~= o2Type then return false end - if o1Type ~= 'table' then return false end - - if not ignore_mt then - local mt1 = getmetatable(o1) - if mt1 and mt1.__eq then - --compare using built in method - return o1 == o2 - end - end - - local keySet = {} - - for key1, value1 in pairs(o1) do - local value2 = o2[key1] - if value2 == nil or equals(value1, value2, ignore_mt) == false then - return false - end - keySet[key1] = true - end - - for key2, _ in pairs(o2) do - if not keySet[key2] then return false end - end - return true -end -return {equals=equals} \ No newline at end of file diff --git a/06/deps/parser-gen/errorgen.lua b/06/deps/parser-gen/errorgen.lua deleted file mode 100644 index c502c0d..0000000 --- a/06/deps/parser-gen/errorgen.lua +++ /dev/null @@ -1,401 +0,0 @@ --- Error generation code for LL(1) grammars --- AST funcs: - -local function isfinal(t) - return t["t"] or t["nt"] or t["func"] or t["s"] or t["num"] -end - -local function isaction(t) - return t["action"] -end - - -local function isrule(t) - return t and t["rulename"] -end - -local function isgrammar(t) - if type(t) == "table" and not(t["action"]) then - return isrule(t[1]) - end - return false -end -local function istoken (t) - return t["token"] == "1" -end - -local function finalNode (t) - if t["t"] then - return"t",t["t"] -- terminal - elseif t["nt"] then - return "nt", t["nt"], istoken(t) -- nonterminal - elseif t["func"] then - return "func", t["func"] -- function - elseif t["s"] then - return "s", t["s"] - elseif t["num"] then - return "num", t["num"] - end - return nil -end - ---[[ - -function rightleaf: - -returns the right-most concatenation in the AST. -used for followset keys - -input: ((A B) C) -output: {"nt_C"} - -input: (A / B / C) (D / 'e') -output: {"nt_D","t_e"} - -input: A* -output: {'',"nt_A"} - -input: !A -output: {"not_nt_A"} -]] -local function addnot(t) - local ret = {} - for k,v in pairs(t) do - ret[k] = "not_"..v - end - return ret -end -local function addepsilon(t) - local ret = t - table.insert(ret, '') - return ret -end -local function mergetables(first,second) - local ret = first - for k,v in pairs(second) do - table.insert(ret, v) - end - return ret -end - -local function rightleaf(t) - local action = t.action - local op1 = t.op1 - local op2 = t.op2 - - if isfinal(t) then - - -- todo: replace nt_A with FIRST(A) - local typefn, fn, tok = finalNode(t) - local ret = typefn .. "_" .. fn -- terminals: t_if, nonterminals: nt_if - return {ret} - - end - - - if action == "or" then - - return mergetables(rightleaf(op1), rightleaf(op2)) - - elseif action == "and" then -- consider only RHS - - return rightleaf(op2) - - elseif action == "&" then - - return rightleaf(op1) - - elseif action == "!" then - - return addnot(rightleaf(op1)) - - elseif action == "+" then - - return rightleaf(op1) - - elseif action == "*" or action == "?" then - - return addepsilon(rightleaf(op1)) - - elseif action == "^" then - - op2 = op2["num"] -- second operand is number - if op2 >= 1 then - return rightleaf(op1) - else - return addepsilon(rightleaf(op1)) - end - - elseif action == "^LABEL" or action == "->" or action == "=>" or action == "tcap" or action == "gcap" or action == "subcap" or action == "scap" then - - return rightleaf(op1) - - elseif action == "bref" or action == "poscap" then - - return addepsilon({}) -- only empty string - - elseif action == "anychar" then - - return {"_anychar"} - - elseif action == "label" then - - return addepsilon({}) - - elseif action == "%" then - - return addepsilon({}) - - elseif action == "invert" then - - return addnot(rightleaf(op1)) - - elseif action == "range" then - - return {"_anychar"} - - else - error("Unsupported action '"..action.."'") - end - -end - - -local FOLLOW = {} - -local function follow_aux(t, dontsplit) - - local action = t.action - local op1 = t.op1 - local op2 = t.op2 - - - if isfinal(t) then - - return {t} - - end - - if action == "or" then - - if dontsplit then -- do not split "(B / C)" in "A (B / C)" - return {t} - else -- return both - return mergetables(follow_aux(op1), follow_aux(op2)) - end - - elseif action == "and" then -- magic happens here - - -- (A (B / D)) (!E C / D) - - -- 1) FOLLOW(B) = FOLLOW(D) = {(!E C / D)} - local rightset = rightleaf(op1) - local rhs = follow_aux(op2) - for k,v in pairs(rightset) do - if not FOLLOW[v] then - FOLLOW[v] = {} - end - -- TODO: check if rhs already exists in FOLLOW(v) - table.insert(FOLLOW[v],rhs) - - end - - -- 2) FOLLOW(A) = {(B / D)} - - return follow_aux(op1) - - - elseif action == "&" then - - return follow_aux(op1) - - elseif action == "!" then - - return {action="!", op1=follow_aux(op1)} - - elseif action == "+" then - - return follow_aux(op1) - - elseif action == "*" then - - return addepsilon(follow_aux(op1)) - - elseif action == "?" then - - return addepsilon(follow_aux(op1)) - - elseif action == "^" then - - op2 = op2["num"] - - if op2 >= 1 then - return follow_aux(op1) - else - return addepsilon(follow_aux(op1)) - end - - elseif action == "^LABEL" or action == "->" or action == "=>" or action == "tcap" or action == "gcap" or action == "subcap" or action == "scap" then - - return follow_aux(op1) - - elseif action == "bref" or action == "poscap" then - - return addepsilon({}) -- only empty string - - elseif action == "anychar" then - - return {"_anychar"} - - elseif action == "label" then - - return addepsilon({}) - - elseif action == "%" then - - return addepsilon({}) - - elseif action == "invert" then - - return {t} -- whole table - - elseif action == "range" then - - return {"_anychar"} - - else - error("Unsupported action '"..action.."'") - end -end - --- function: follow --- finds follow set for the whole AST, with key (rule, term) -local function follow (t) - local followset = {} - if isgrammar(t) then - for pos,val in pairs(t) do - local rule = val.rulename - FOLLOW = {} -- reset for each rule - follow_aux(val.rule) -- apply recursive function - followset[rule] = FOLLOW - end - else - FOLLOW = {} - follow_aux(t) - followset[''] = FOLLOW - end - return followset -end - --- functions to add errors --- find size of table -local function getn (t) - local size = 0 - for _, _ in pairs(t) do - size = size+1 - end - return size -end --- generate error message by traversing table to the left -local function printexpect(op) - --peg.print_r(op) - if isfinal(op) then - if op["t"] then - return "'"..op["t"].."'" - end - return op["nt"] or op["func"] or op["s"] or op["num"] - else - local test = op.op1 - if not test then - return op.action - else - return printexpect(test) - end - end -end -local GENERATED_ERRORS = 0 -local TERRS = {} -local function generateerror(op, after) - - local desc = "Expected "..printexpect(op) - - local err = GENERATED_ERRORS+1 - if err >= 255 then - error("Error label limit reached(255)") - end - local name = "errorgen"..err - TERRS[name] = desc - GENERATED_ERRORS = GENERATED_ERRORS+1 - return name -end - - -local function tryadderror(op, after) - - if FOLLOW then - - local rhs = rightleaf(after) - -- (A / B) C - -- generate error iff #FOLLOW(A) OR #FOLLOW(B) = 1 - local generate = false - for k,v in pairs(rhs) do - if FOLLOW[v] then - local n = getn(FOLLOW[v]) - generate = generate or n==1 - end - end - if generate then - local lab = generateerror(op, after) - return {action="^LABEL",op1=op,op2={s=lab}} - end - end - return op -end - - --- function: adderrors --- traverses the AST and adds error labels where possible - -local function adderrors_aux(ast,tokenrule) - - if not ast then - return nil - end - - if isaction(ast) then - - local act, op1, op2 - act = ast["action"] - op1 = ast["op1"] - op2 = ast["op2"] - - if act == "and" and not tokenrule then - - op2 = tryadderror(op2, op1) - - end - - ast["op1"] = adderrors_aux(op1,tokenrule) - ast["op2"] = adderrors_aux(op2,tokenrule) - end - return ast -end -local function adderrors(t, followset) - GENERATED_ERRORS = 0 - TERRS = {} - if isgrammar(t) then - for pos,val in pairs(t) do - local currentrule = val.rulename - FOLLOW = followset[currentrule] - local rule = val.rule - local istokenrule = val.token == "1" - adderrors_aux(rule,istokenrule) - end - else - FOLLOW = followset[''] - adderrors_aux(t,false) - end - return TERRS -end - -return {follow=follow,adderrors=adderrors} diff --git a/06/deps/parser-gen/parser-gen-tests.lua b/06/deps/parser-gen/parser-gen-tests.lua deleted file mode 100644 index a1ae62d..0000000 --- a/06/deps/parser-gen/parser-gen-tests.lua +++ /dev/null @@ -1,149 +0,0 @@ -local pg = require("parser-gen") -local equals = require("equals").equals - --- terminals --- space allowed -rule = pg.compile [[ -rule <- 'a' -]] -str = "a a aa " -res = pg.parse(str,rule) -assert(res) - --- space not allowed -rule = pg.compile [[ -RULE <- 'a' 'b' -]] -str = "a b" -res = pg.parse(str,rule) -assert(not res) - --- space not allowed 2 -rule = pg.compile [[ -rule <- 'a' 'b' -SKIP <- '' -SYNC <- '' -]] -str = "a b" -res = pg.parse(str,rule) -assert(not res) - --- custom space -rule = pg.compile [[ -rule <- 'a' 'b' -SKIP <- DOT -DOT <- '.' -]] -str = "a...b" -res = pg.parse(str,rule) -assert(res) - --- non terminals --- space allowed -rule = pg.compile [[ -rule <- A B -A <- 'a' -B <- 'b' -]] -str = "a b" -res, err = pg.parse(str,rule) -assert(res) --- no spaces allowed -rule = pg.compile [[ -RULE <- A B -A <- 'a' -B <- 'b' -]] -str = "a b" -res = pg.parse(str,rule) -assert(not res) - --- space in the beginning and end of string -rule = pg.compile [[ -rule <- A B -A <- 'a' -B <- 'b' -]] -str = " a b " -res = pg.parse(str,rule) -assert(res) - - - --- TESTING CAPTURES - -r = pg.compile([[ rule <- {| {:'a' 'b':}* |} - - ]],_,_,true) -res = pg.parse("ababab", r) - -assert(equals(res,{"ab","ab","ab"})) --- space in capture - -rule = pg.compile([[ rule <- {| {: 'a' :}* |} -]],_,_,true) -str = " a a a " -res = pg.parse(str,rule) - -assert(equals(res,{"a","a","a"})) -- fails - --- TESTING ERROR LABELS -local labs = {errName = "Error number 1",errName2 = "Error number 2"} -pg.setlabels(labs) -rule = pg.compile [[ rule <- 'a' / %{errName} - SYNC <- '' - ]] -local errorcalled = false -local function err(desc, line, col, sfail, recexp) - errorcalled = true - assert(desc == "Error number 1") -end -res = pg.parse("b",rule,err) -assert(errorcalled) - --- TESTING ERROR RECOVERY - -local labs = {errName = "Error number 1",errName2 = "Error number 2"} -pg.setlabels(labs) - -rule = pg.compile [[ -rule <- As //{errName,errName2} Bs -As <- 'a'* / %{errName2} -Bs <- 'b'* -]] -res1 = pg.parse(" a a a",rule) -res2 = pg.parse("b b b ",rule) -assert(res1 and res2) - --- TESTING ERROR GENERATION - -pg.setlabels({}) -rule = pg.compile([[ -rule <- A B C -A <- 'a' -B <- 'b' -C <- 'c' - -]],_,true) -res1, errs = pg.parse("ab",rule) -assert(errs[1]["msg"] == "Expected C") - --- TESTING RECOVERY GENERATION - - --- SELF-DESCRIPTION -pg.setlabels(pg.labels) -gram = pg.compile(pg.gram, pg.defs,_,true) -res1, errs = pg.parse(pg.gram,gram) -assert(res1) -- parse succesful - ---[[ this test is invalid since tool added ^LABEL syntax -r = re.compile(pg.gram,pg.defs) -res2 = r:match(pg.gram) - ---pg.print_r(res2) - -assert(equals(res1, res2)) -]]-- - -print("all tests succesful") diff --git a/06/deps/parser-gen/parser-gen.lua b/06/deps/parser-gen/parser-gen.lua deleted file mode 100644 index 128de68..0000000 --- a/06/deps/parser-gen/parser-gen.lua +++ /dev/null @@ -1,563 +0,0 @@ -local f = (...):match("(.-)[^%.]+$") -local peg = require(f .. "peg-parser") -local eg = require(f .. "errorgen") -local s = require(f .. "stack") - --- Create stack for tokens inside captures. --- nil - not inside capture, --- 0 - inside capture, --- 1 - token found inside capture -local tokenstack = s.Stack:Create() -local subject, errors, errorfunc -local unpack = table.unpack; - -local Predef = { nl = m.P"\n", cr = m.P"\r", tab = m.P"\t" } -local mem = {} -- for compiled grammars - -local function updatelocale() - m.locale(Predef) - local any = m.P(1) - Predef.a = Predef.alpha - Predef.c = Predef.cntrl - Predef.d = Predef.digit - Predef.g = Predef.graph - Predef.l = Predef.lower - Predef.p = Predef.punct - Predef.s = Predef.space - Predef.u = Predef.upper - Predef.w = Predef.alnum - Predef.x = Predef.xdigit - Predef.A = any - Predef.a - Predef.C = any - Predef.c - Predef.D = any - Predef.d - Predef.G = any - Predef.g - Predef.L = any - Predef.l - Predef.P = any - Predef.p - Predef.S = any - Predef.s - Predef.U = any - Predef.u - Predef.W = any - Predef.w - Predef.X = any - Predef.x - mem = {} -end - -updatelocale() - -local definitions = {} -local tlabels = {} -local totallabels = 0 -local tlabelnames = {} -- reverse table -local tdescs = {} -local trecs = {} -- recovery for each error - - -local function defaultsync(patt) - return (m.P(1)^-1) * (-patt * m.P(1))^0 -end - --- TODO: store these variables for each grammar -local SKIP = (Predef.space + Predef.nl) -local SYNC = defaultsync(SKIP) - - -local recovery = true -local skipspaces = true -local buildast = true - -local function sync (patt) - return patt --(-patt * m.P(1))^0 * patt^0 -- skip until we find the pattern and consume it(if we do) -end - - -local function pattspaces (patt) - if skipspaces then - return patt * SKIP^0 - else - return patt - end -end - -local function token (patt) - local incapture = tokenstack:pop() -- returns nil if not in capture - if not incapture then - return pattspaces(patt) - end - tokenstack:push(1) - return patt -end - - - - --- functions used by the tool - -local function iscompiled (gr) - return m.type(gr) == "pattern" -end - -local function istoken (t) - return t["token"] == "1" -end - -local function isfinal(t) - return t["t"] or t["nt"] or t["func"] or t["s"] or t["num"] -end - -local function isaction(t) - return t["action"] -end - - -local function isrule(t) - return t and t["rulename"] -end -local function isgrammar(t) - if type(t) == "table" and not(t["action"]) then - return isrule(t[1]) - end - return false -end - -local function iscapture (action) - return action == "=>" or action == "gcap" or action == "scap" or action == "subcap" or action == "poscap" -end - -local function finalNode (t) - if t["t"] then - return"t",t["t"] -- terminal - elseif t["nt"] then - return "nt", t["nt"], istoken(t) -- nonterminal - elseif t["func"] then - return "func", t["func"] -- function - elseif t["s"] then - return "s", t["s"] - elseif t["num"] then - return "num", t["num"] - end - return nil -end -local bg = {} -- local variable to keep global function buildgrammar - - -local function addspaces (caps) - local hastoken = tokenstack:pop() - if hastoken == 1 then - return pattspaces(caps) - end - return caps -end - -local function applyaction(action, op1, op2, labels,tokenrule) - if action == "or" then - if labels then -- labels = {{s="errName"},{s="errName2"}} - for i, v in ipairs(labels) do - local labname = v["s"] - local lab = tlabels[labname] - if not lab then - error("Label '"..labname.."' undefined") - end - labels[i] = lab - end - return m.Rec(op1,op2,unpack(labels)) - end - return op1 + op2 - elseif action == "and" then - - return op1 * op2 - elseif action == "&" then - return #op1 - elseif action == "!" then - return -op1 - elseif action == "+" then - return op1^1 - elseif action == "*" then - return op1^0 - elseif action == "?" then - return op1^-1 - elseif action == "^" then - return op1^op2 - elseif action == "^LABEL" then - local lab = tlabels[op2] - if not lab then - error("Label '"..op2.."' unspecified using setlabels()") - end - return op1 + m.T(lab) - elseif action == "->" then - return op1 / op2 - -- in captures we add SPACES^0 - elseif action == "=>" then - return addspaces(m.Cmt(op1,op2)) - elseif action == "tcap" then - return m.Ct(op1) -- nospaces - elseif action == "gcap" then - return addspaces(m.Cg(op1, op2)) - elseif action == "bref" then - return m.Cb(op1) --m.Cmt(m.Cb(op1), equalcap) -- do we need to add spaces to bcap? - elseif action == "poscap" then - return addspaces(m.Cp()) - elseif action == "subcap" then - return addspaces(m.Cs(op1)) - elseif action == "scap" then - return addspaces(m.C(op1)) - elseif action == "anychar" then - if buildast and not tokenrule then - return m.C(m.P(1)) - end - return m.P(1) - elseif action == "label" then - local lab = tlabels[op1] - if not lab then - error("Label '"..op1.."' unspecified using setlabels()") - end - return m.T(lab) -- lpeglabel - elseif action == "%" then - if definitions[op1] then - return definitions[op1] - elseif Predef[op1] then - return Predef[op1] - else - error("Definition for '%"..op1.."' unspecified(use second parameter of parser-gen.compile())") - end - elseif action == "invert" then - return m.P(1) - op1 - elseif action == "range" then - local res = m.R(op1) - if not tokenrule then - if buildast then - res = m.C(res) - end - res = token(res) - end - return res - else - error("Unsupported action '"..action.."'") - end -end - -local function applyfinal(action, term, tokenterm, tokenrule) - - if action == "t" then - local res = m.P(term) - if not tokenrule then - if buildast then - res = m.C(res) - end - if skipspaces then - res = token(res) - end - end - return res - elseif action == "nt" then - if skipspaces and tokenterm and (not tokenrule) then - return token(m.V(term)) - else - return m.V(term) - end - elseif action == "func" then - if definitions[term] then - return definitions[term] - else - error("Definition for function '"..term.."' unspecified (use second parameter of parser-gen.compile())") - end - elseif action == "s" then -- simple string - return term - elseif action == "num" then -- numbered string - return tonumber(term) - end -end - - -local function applygrammar(gram) - return m.P(gram) -end - -local function traverse (ast, tokenrule) - if not ast then - return nil - end - - if isfinal(ast) then - local typefn, fn, tok = finalNode(ast) - return applyfinal(typefn, fn, tok, tokenrule) - - elseif isaction(ast) then - - local act, op1, op2, labs, ret1, ret2 - act = ast["action"] - op1 = ast["op1"] - op2 = ast["op2"] - labs = ast["condition"] -- recovery operations - - -- post-order traversal - if iscapture(act) then - tokenstack:push(0) -- not found any tokens yet - end - - ret1 = traverse(op1, tokenrule) - ret2 = traverse(op2, tokenrule) - - - return applyaction(act, ret1, ret2, labs, tokenrule) - - elseif isgrammar(ast) then - -- - local g = bg.buildgrammar (ast) - return applygrammar (g) - - else - peg.print_r(ast) - error("Unsupported AST") - end - -end - -local function specialrules(ast, builder) - -- initialize values - SKIP = (Predef.space + Predef.nl) - skipspaces = true - SYNC = nil - recovery = true - -- find SPACE and SYNC rules - for i, v in ipairs(ast) do - local name = v["rulename"] - local rule - if name == "SKIP" then - rule = traverse(v["rule"], true) - if v["rule"]["t"] == '' then - skipspaces = false - else - skipspaces = true - SKIP = rule - end - builder[name] = rule - elseif name == "SYNC" then - rule = traverse(v["rule"], true) - if v["rule"]["t"] == '' then-- SYNC <- '' - recovery=false - else - recovery= true - SYNC = rule - end - builder[name] = rule - end - end - if not SYNC and recovery then - SYNC = defaultsync(SKIP) - end -end - -local function recorderror(position,label) - -- call error function here - local line, col = peg.calcline(subject, position) - local desc - if label == 0 then - desc = "Syntax error" - else - desc = tdescs[label] - end - if errorfunc then - local temp = string.sub(subject,position) - local strend = string.find(temp, "\n") - local sfail = string.sub(temp, 1, strend) - errorfunc(desc,line,col,sfail,trecs[label]) - end - - local err = { line = line, col = col, label=tlabelnames[label], msg = desc } - table.insert(errors, err) - -end -local function record(label) - return (m.Cp() * m.Cc(label)) / recorderror -end - -local function buildrecovery(grammar) - - local synctoken = pattspaces(sync(SYNC)) - local grec = grammar - - for k,v in pairs(tlabels) do - - if trecs[v] then -- custom sync token - grec = m.Rec(grec,record(v) * pattspaces(trecs[v]), v) - else -- use global sync token - grec = m.Rec(grec,record(v) * synctoken, v) - end - end - return grec - -end -local usenode = false - -local function usenodes(val) - usenode = val -end - - -function bg.buildgrammar (ast) - local builder = {} - specialrules(ast, builder) - local initialrule - for i, v in ipairs(ast) do - local istokenrule = v["token"] == "1" - local isfragment = v["fragment"] == "1" - local isnode = v["node"] == "1" - - if isnode and not usenodes then - error("Node mode disabled - please use parser-gen.usenodes(true) before compiling the grammar") - end - - local name = v["rulename"] - local isspecial = name == "SKIP" or name == "SYNC" - local rule = v["rule"] - if i == 1 then - initialrule = name - table.insert(builder, name) -- lpeg syntax - builder[name] = traverse(rule, istokenrule) - else - if not builder[name] then -- dont traverse rules for SKIP and SYNC twice - builder[name] = traverse(rule, istokenrule) - end - end - if buildast and not isfragment and not isspecial and ((not usenode) or (usenode and isnode)) then - if istokenrule then - builder[name] = m.C(builder[name]) - end - builder[name] = m.Ct(m.Cg(m.Cc(name),"rule") * m.Cg(m.Cp(),"pos") * builder[name]) - end - end - - if skipspaces then - builder[initialrule] = SKIP^0 * builder[initialrule] -- skip spaces at the beginning of the input - end - if recovery then - builder[initialrule] = buildrecovery(builder[initialrule]) -- build recovery on top of initial rule - end - return builder -end - - - - -local function build(ast, defs) - if defs then - definitions = defs - end - if isgrammar(ast) then - return traverse(ast) - else - SKIP = (Predef.space + Predef.nl) - skipspaces = true - SYNC = nil - recovery = true - SYNC = defaultsync(SKIP) - local res = SKIP ^0 * traverse(ast) - if buildast then - res = m.Ct(res) - end - return res -- input is not a grammar - skip spaces and sync by default - end -end - - - --- recovery grammar - - - - --- end - - - --- t = {errName="Error description",...} -local function setlabels (t, errorgen) - local index - if errorgen then - index = totallabels + 1 - else - -- reset error tables - index = 1 - tlabels = {} - - tdescs = {} - trecs = {} - end - for key,value in pairs(t) do - if index >= 255 then - error("Error label limit reached(255)") - end - if type(value) == "table" then -- we have a recovery expression - tdescs[index] = value[1] - - trecs[index] = traverse(peg.pegToAST(value[2]), true)-- PEG to LPEG - else - tdescs[index] = value - end - tlabels[key] = index - tlabelnames[index] = key -- reverse table - index = index + 1 - end - totallabels = index-1 -end - - -local function compile (input, defs, generrors, nocaptures) - if iscompiled(input) then - return input - end - if not mem[input] then - buildast = true - if nocaptures then - buildast=false - end - --re.setlabels(tlabels) - --re.compile(input,defs) - -- build ast - local ast = peg.pegToAST(input) - if generrors then - local follow = eg.follow(ast) - local errors = eg.adderrors(ast, follow) - setlabels (errors, true) -- add errors generated by errorgen - end - local gram = build(ast,defs) - if not gram then - -- find error using relabel module - - end - mem[input] = gram-- store if the user forgets to compile it - end - return mem[input] -end - - - - - - -local function parse (input, grammar, errorfunction) - if not iscompiled(grammar) then - - local cp = compile(grammar) - grammar = cp - end - -- set up recovery table - errorfunc = errorfunction - subject = input - errors = {} - -- end - local r, e, sfail = m.match(grammar,input) - if not r then - recorderror(#input - #sfail, e) - end - if #errors == 0 then errors=nil end - return r, errors -end - - - - --- could be wrong -local follow = eg.follow - -local pg = {compile=compile, setlabels=setlabels, parse=parse,follow=follow, calcline = peg.calcline, usenodes = usenodes} - -return pg diff --git a/06/deps/parser-gen/parsers/lua-parser-tests.lua b/06/deps/parser-gen/parsers/lua-parser-tests.lua deleted file mode 100644 index d808518..0000000 --- a/06/deps/parser-gen/parsers/lua-parser-tests.lua +++ /dev/null @@ -1,799 +0,0 @@ -local lua = require "lua-parser" -local peg = require "peg-parser" - -local eq = require "equals" -local equals = eq.equals -print("\n\n [[ PARSING LUA TEST SUITE FILES ]] \n\n") -local filenames = { -'all.lua', -'main.lua', -'gc.lua', -'db.lua', -'calls.lua', -'strings.lua', -'literals.lua', -'tpack.lua', -'attrib.lua', -'locals.lua', -'constructs.lua', -'code.lua', -'big.lua', -'nextvar.lua', -'pm.lua', -'utf8.lua', -'api.lua', -'events.lua', -'vararg.lua', -'closure.lua', -'coroutine.lua', -'goto.lua', -'errors.lua', -'math.lua', -'sort.lua', -'bitwise.lua', -'verybig.lua', -'files.lua', -} -local errs = 0 -for k,v in ipairs(filenames) do - local filename = "lua-5.3.4-tests/"..v - local f = assert(io.open(filename, "r")) - - local t = f:read("*all") - - local res, err = lua.parse(t) - local s = "OK" - if not res then s = "FAIL" end -- only check if succesful since grammar ensures whole file is read - print("Testing file '"..v.."': ["..s.."]") - if not res then - errs = errs + 1 - print("Error: "..err[1]["msg"]) - end - f:close() -end -assert(errs == 0) - -print("\n\n Test suite files compiled successfully") - - -print("\n\n [[ TESTING ERROR LABELS ]] ") -local pr = peg.print_r --- test errors -local s,res, err -local ErrExtra="unexpected character(s), expected EOF" -s = [[ return; ! ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExtra) - -local ErrInvalidStat="unexpected token, invalid start of statement" -s = [[ ! ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrInvalidStat) - - -local ErrEndIf="expected 'end' to close the if statement" - -s = [[ if c then b=1 ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEndIf) - -local ErrExprIf="expected a condition after 'if'" - -s = [[ if then b=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprIf) - -local ErrThenIf="expected 'then' after the condition" - -s = [[ if c b=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrThenIf) - -local ErrExprEIf="expected a condition after 'elseif'" - -s = [[ if a then b=1 elseif then d=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprEIf) - -local ErrThenEIf="expected 'then' after the condition" - -s = [[ if a b=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrThenEIf) - -local ErrEndDo="expected 'end' to close the do block" - -s = [[ do x=1 ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEndDo) - -local ErrExprWhile="expected a condition after 'while'" - -s = [[ while do c=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprWhile) - -local ErrDoWhile="expected 'do' after the condition" - -s = [[ while a c=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrDoWhile) - -local ErrEndWhile="expected 'end' to close the while loop" - -s = [[ while a do b=1]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEndWhile) - -local ErrUntilRep="expected 'until' at the end of the repeat loop" - -s = [[ repeat c=1 ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrUntilRep) - -local ErrExprRep="expected a conditions after 'until'" - -s = [[ repeat c=1 until ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprRep) - -local ErrForRange="expected a numeric or generic range after 'for'" - -s = [[ for 3,4 do x=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrForRange) - -local ErrEndFor="expected 'end' to close the for loop" - -s = [[ for c=1,3 do a=1 ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEndFor) - -local ErrExprFor1="expected a starting expression for the numeric range" - -s = [[ for a=,4 do a=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprFor1) - -local ErrCommaFor="expected ',' to split the start and end of the range" - -s = [[ for a=4 5 do a=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCommaFor) - -local ErrExprFor2="expected an ending expression for the numeric range" - -s = [[ for a=4, do a=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprFor2) - -local ErrExprFor3="expected a step expression for the numeric range after ','" - -s = [[ for a=1,2, do a=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprFor3) - -local ErrInFor="expected '=' or 'in' after the variable(s)" - -s = [[ for a of 1 do a=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrInFor) - -local ErrEListFor="expected one or more expressions after 'in'" - -s = [[ for a in do a=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEListFor) - -local ErrDoFor="expected 'do' after the range of the for loop" - -s = [[ for a=1,2 a=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrDoFor) - -local ErrDefLocal="expected a function definition or assignment after local" - -s = [[ local return c ]] - -print("Parsing '"..s.."'") -res, err = lua.parse(s) - -assert(err[1]["msg"] == ErrDefLocal) - - -local ErrNameLFunc="expected a function name after 'function'" - -s = [[ local function() c=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrNameLFunc) - - -local ErrEListLAssign="expected one or more expressions after '='" - -s = [[ local a = return b ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEListLAssign) - -local ErrEListAssign="expected one or more expressions after '='" - -s = [[ a = return b ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEListAssign) - - -local ErrFuncName="expected a function name after 'function'" - -s = [[ function () a=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrFuncName) - -local ErrNameFunc1="expected a function name after '.'" - -s = [[ function a.() a=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrNameFunc1) - -local ErrNameFunc2="expected a method name after ':'" - -s = [[ function a:() a=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrNameFunc2) - -local ErrOParenPList="expected '(' for the parameter list" - -s = [[ function a b=1 end]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrOParenPList) - -local ErrCParenPList="expected ')' to close the parameter list" - -s = [[ -function a( - b=1 - -end -]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCParenPList) - -local ErrEndFunc="expected 'end' to close the function body" - -s = [[ function a() b=1 ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEndFunc) - -local ErrParList="expected a variable name or '...' after ','" - -s = [[ function a(b, ) b=1 end ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrParList) - - -local ErrLabel="expected a label name after '::'" - -s = [[ :: return b ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrLabel) - -local ErrCloseLabel="expected '::' after the label" - -s = [[ :: abc return a]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCloseLabel) - -local ErrGoto="expected a label after 'goto'" - -s = [[ goto return c]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrGoto) - - - -local ErrVarList="expected a variable name after ','" - -s = [[ abc, - = 3]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) - -assert(err[1]["msg"] == ErrVarList) - -local ErrExprList="expected an expression after ','" - -s = [[ return a,;]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprList) - - -local ErrOrExpr="expected an expression after 'or'" - -s = [[ return a or; ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrOrExpr) - -local ErrAndExpr="expected an expression after 'and'" - -s = [[ return a and;]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrAndExpr) - -local ErrRelExpr="expected an expression after the relational operator" - -s = [[ return a >;]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrRelExpr) - - -local ErrBitwiseExpr="expected an expression after bitwise operator" - -s = [[ return b & ; ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrBitwiseExpr) - -local ErrConcatExpr="expected an expression after '..'" - -s = [[ print(a..) ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrConcatExpr) - -local ErrAddExpr="expected an expression after the additive operator" - -s = [[ return a - ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrAddExpr) - -local ErrMulExpr="expected an expression after the multiplicative operator" - -s = [[ return a/ ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrMulExpr) - -local ErrUnaryExpr="expected an expression after the unary operator" - -s = [[ return # ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrUnaryExpr) - -local ErrPowExpr="expected an expression after '^'" - -s = [[ return a^ ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrPowExpr) - - -local ErrExprParen="expected an expression after '('" - -s = [[ return a + () ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprParen) - -local ErrCParenExpr="expected ')' to close the expression" - -s = [[ return a + (a ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCParenExpr) - -local ErrNameIndex="expected a field name after '.'" - -s = [[ return a. ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrNameIndex) - -local ErrExprIndex="expected an expression after '['" - -s = [[ return a [ ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprIndex) - -local ErrCBracketIndex="expected ']' to close the indexing expression" - -s = [[ return a[1 ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCBracketIndex) - -local ErrNameMeth="expected a method name after ':'" - -s = [[ return a: ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrNameMeth) - -local ErrMethArgs="expected some arguments for the method call (or '()')" -s = [[ a:b ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrMethArgs) - - - -local ErrCParenArgs="expected ')' to close the argument list" - -s = [[ return a(c ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCParenArgs) - - -local ErrCBraceTable="expected '}' to close the table constructor" - -s = [[ return { ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCBraceTable) - -local ErrEqField="expected '=' after the table key" - -s = [[ a = {[b] b} ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEqField) - -local ErrExprField="expected an expression after '='" - -s = [[ a = {[a] = } ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprField) - -local ErrExprFKey="expected an expression after '[' for the table key" - -s = [[ a = {[ = b} ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrExprFKey) - -local ErrCBracketFKey="expected ']' to close the table key" - -s = [[ a = {[a = b} ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCBracketFKey) - - -local ErrDigitHex="expected one or more hexadecimal digits after '0x'" - -s = [[ a = 0x ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrDigitHex) - -local ErrDigitDeci="expected one or more digits after the decimal point" - -s = [[ a = . ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrDigitDeci) - -local ErrDigitExpo="expected one or more digits for the exponent" - - -s = [[ a = 1.0e ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrDigitExpo) - -local ErrQuote="unclosed string" - -s = [[ a = ";]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrQuote) - -local ErrHexEsc="expected exactly two hexadecimal digits after '\\x'" - -s = [[ a = "a\x1" ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrHexEsc) - -local ErrOBraceUEsc="expected '{' after '\\u'" - -s = [[ a = "a\u" ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrOBraceUEsc) - -local ErrDigitUEsc="expected one or more hexadecimal digits for the UTF-8 code point" - -s = [[ a = "\u{}"]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrDigitUEsc) - -local ErrCBraceUEsc="expected '}' after the code point" - -s = [[ a = "\u{12" ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCBraceUEsc) - -local ErrEscSeq="invalid escape sequence" - -s = [[ a = "\;" ]] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrEscSeq) - -local ErrCloseLStr="unclosed long string" - - -s = [==[ a = [[ abc return; ]==] -print("Parsing '"..s.."'") -res, err = lua.parse(s) -assert(err[1]["msg"] == ErrCloseLStr) - -print("\n\n All error labels generated successfully") - -print("\n\n [[ TESTING AST GENERATION ]]\n\n ") - --- TODO: AST - -s = [[ -if abc > 123 then - abc=123 -end]] -rez = { - rule='chunk', - pos=3, - { - rule='block', - pos=3, - { - rule='stat', - pos=3, - 'if', - { - rule='exp', - pos=6, - { - rule='exp', - { - rule='exp', - { - rule='expTokens', - pos=6, - { - rule='prefixexp', - pos=6, - { - rule='varOrExp', - pos=6, - { - rule='var', - pos=6, - { - rule='NAME', - pos=6, - 'abc', - }, - }, - }, - }, - }, - }, - { - rule='operatorComparison', - pos=10, - '>', - }, - { - rule='expTokens', - pos=12, - { - rule='number', - pos=12, - { - rule='INT', - pos=12, - '123', - }, - }, - }, - }, - }, - 'then', - { - rule='block', - pos=23, - { - rule='stat', - pos=23, - { - rule='varlist', - pos=23, - { - rule='var', - pos=23, - { - rule='NAME', - pos=23, - 'abc', - }, - }, - }, - '=', - { - rule='explist', - pos=27, - { - rule='exp', - pos=27, - { - rule='expTokens', - pos=27, - { - rule='number', - pos=27, - { - rule='INT', - pos=27, - '123', - }, - }, - }, - }, - }, - }, - }, - 'end', - }, - }, - - -} - - -print("Parsing '"..s.."'") -res, err = lua.parse(s) -peg.print_t(res) -assert(equals(res,rez)) - -s = [[ -local a = [=[ long string ]=] - --- aaa -return a ---[==[ hi - -]==] -]] -rez = { - - rule='chunk', - pos=3, - { - rule='block', - pos=3, - { - rule='stat', - pos=3, - 'local', - { - rule='localAssign', - pos=9, - { - rule='namelist', - pos=9, - { - rule='NAME', - pos=9, - 'a', - }, - }, - '=', - { - rule='explist', - pos=13, - { - rule='exp', - pos=13, - { - rule='expTokens', - pos=13, - { - rule='string', - pos=13, - ' long string ', - }, - }, - }, - }, - }, - }, - { - rule='retstat', - pos=41, - 'return', - { - rule='explist', - pos=48, - { - rule='exp', - pos=48, - { - rule='expTokens', - pos=48, - { - rule='prefixexp', - pos=48, - { - rule='varOrExp', - pos=48, - { - rule='var', - pos=48, - { - rule='NAME', - pos=48, - 'a', - }, - }, - }, - }, - }, - }, - }, - }, - }, - - - -} -print("Parsing '"..s.."'") -res, err = lua.parse(s) -peg.print_t(res) -assert(equals(res,rez)) - -print("\n\n All AST's generated successfully") - -print("\n\nAll tests passed!") \ No newline at end of file diff --git a/06/deps/parser-gen/parsers/lua-parser.lua b/06/deps/parser-gen/parsers/lua-parser.lua deleted file mode 100644 index c87a3a5..0000000 --- a/06/deps/parser-gen/parsers/lua-parser.lua +++ /dev/null @@ -1,291 +0,0 @@ ---[==[ -Parser for Lua 5.3 -Based on https://github.com/antlr/grammars-v4/blob/master/lua/Lua.g4 and https://github.com/andremm/lua-parser/blob/master/lua-parser/parser.lua -]==] -package.path = package.path .. ";../?.lua" -local pg = require "parser-gen" -function equals(s,i,a,b) return #a == #b end -function fixexp (...) - local exp = {...} - local len = #exp - if len > 1 then - exp.rule = "exp" - exp[len].rule = "exp" - return exp - elseif exp[1] then - if exp[1].rule == "expTokens" then - return exp[1] - else - return exp[1][1] - end - end -end -function fold (...) - local exp = {...} - local len = #exp - if len > 1 then - local folded = { rule = "exp", fixexp(exp[1]) } - for i = 2, len, 2 do - folded = { rule = "exp", folded, exp[i], fixexp(exp[i+1]) } - end - return folded - elseif exp[1] then - return exp[1][1] - end -end --- from https://github.com/andremm/lua-parser/blob/master/lua-parser/parser.lua -local labels = { - ErrExtra="unexpected character(s), expected EOF", - ErrInvalidStat={"unexpected token, invalid start of statement",[[ (!%nl .)* ]]}, - - ErrEndIf="expected 'end' to close the if statement", - ErrExprIf="expected a condition after 'if'", - ErrThenIf="expected 'then' after the condition", - ErrExprEIf="expected a condition after 'elseif'", - ErrThenEIf="expected 'then' after the condition", - - ErrEndDo="expected 'end' to close the do block", - ErrExprWhile="expected a condition after 'while'", - ErrDoWhile="expected 'do' after the condition", - ErrEndWhile="expected 'end' to close the while loop", - ErrUntilRep="expected 'until' at the end of the repeat loop", - ErrExprRep="expected a conditions after 'until'", - - ErrForRange="expected a numeric or generic range after 'for'", - ErrEndFor="expected 'end' to close the for loop", - ErrExprFor1="expected a starting expression for the numeric range", - ErrCommaFor="expected ',' to split the start and end of the range", - ErrExprFor2="expected an ending expression for the numeric range", - ErrExprFor3={"expected a step expression for the numeric range after ','",[[ (!'do' !%nl .)* ]]}, - ErrInFor="expected '=' or 'in' after the variable(s)", - ErrEListFor="expected one or more expressions after 'in'", - ErrDoFor="expected 'do' after the range of the for loop", - - ErrDefLocal="expected a function definition or assignment after local", - ErrNameLFunc="expected a function name after 'function'", - ErrEListLAssign="expected one or more expressions after '='", - ErrEListAssign="expected one or more expressions after '='", - - ErrFuncName="expected a function name after 'function'", - ErrNameFunc1="expected a function name after '.'", - ErrNameFunc2="expected a method name after ':'", - ErrOParenPList="expected '(' for the parameter list", - ErrCParenPList="expected ')' to close the parameter list", - ErrEndFunc="expected 'end' to close the function body", - ErrParList="expected a variable name or '...' after ','", - - ErrLabel="expected a label name after '::'", - ErrCloseLabel="expected '::' after the label", - ErrGoto="expected a label after 'goto'", - - ErrVarList={"expected a variable name after ','",[[ (!'=' !%nl .)* ]]}, - ErrExprList="expected an expression after ','", - - ErrOrExpr="expected an expression after 'or'", - ErrAndExpr="expected an expression after 'and'", - ErrRelExpr="expected an expression after the relational operator", - - ErrBitwiseExpr="expected an expression after bitwise operator", - - ErrConcatExpr="expected an expression after '..'", - ErrAddExpr="expected an expression after the additive operator", - ErrMulExpr="expected an expression after the multiplicative operator", - ErrUnaryExpr="expected an expression after the unary operator", - ErrPowExpr="expected an expression after '^'", - - ErrExprParen="expected an expression after '('", - ErrCParenExpr="expected ')' to close the expression", - ErrNameIndex="expected a field name after '.'", - ErrExprIndex="expected an expression after '['", - ErrCBracketIndex="expected ']' to close the indexing expression", - ErrNameMeth="expected a method name after ':'", - ErrMethArgs="expected some arguments for the method call (or '()')", - - - ErrCParenArgs="expected ')' to close the argument list", - - ErrCBraceTable="expected '}' to close the table constructor", - ErrEqField="expected '=' after the table key", - ErrExprField="expected an expression after '='", - ErrExprFKey={"expected an expression after '[' for the table key",[[ (!']' !%nl .)* ]] }, - ErrCBracketFKey={"expected ']' to close the table key",[[ (!'=' !%nl .)* ]]}, - - ErrDigitHex="expected one or more hexadecimal digits after '0x'", - ErrDigitDeci="expected one or more digits after the decimal point", - ErrDigitExpo="expected one or more digits for the exponent", - - ErrQuote="unclosed string", - ErrHexEsc={"expected exactly two hexadecimal digits after '\\x'",[[ (!('"' / "'" / %nl) .)* ]]}, - ErrOBraceUEsc="expected '{' after '\\u'", - ErrDigitUEsc={"expected one or more hexadecimal digits for the UTF-8 code point",[[ (!'}' !%nl .)* ]]}, - ErrCBraceUEsc={"expected '}' after the code point",[[ (!('"' / "'") .)* ]]}, - ErrEscSeq={"invalid escape sequence",[[ (!('"' / "'" / %nl) .)* ]]}, - ErrCloseLStr="unclosed long string", - ErrEqAssign="expected '=' after variable list in assign statement" -} -pg.setlabels(labels) -local grammar = pg.compile([==[ - chunk <- block (!.)^ErrExtra - block <- stat* retstat? - stat <- ';' / - functioncall / - varlist '='^ErrEqAssign explist^ErrEListAssign / - 'break' / - 'goto' NAME^ErrGoto / - 'do' block 'end'^ErrEndDo / - 'while' exp^ErrExprWhile 'do'^ErrDoWhile block 'end'^ErrEndWhile / - 'repeat' block 'until'^ErrUntilRep exp^ErrExprRep / - 'if' exp^ErrExprIf 'then'^ErrThenIf block ('elseif' exp^ErrExprEIf 'then'^ErrThenEIf block)* ('else' block)? 'end'^ErrEndIf / - 'for' (forNum / forIn)^ErrForRange 'do'^ErrDoFor block 'end'^ErrEndFor / - - 'function' funcname^ErrFuncName funcbody / - 'local' (localAssign / localFunc)^ErrDefLocal / - label / - !blockEnd %{ErrInvalidStat} - blockEnd <- 'return' / 'end' / 'elseif' / 'else' / 'until' / !. - retstat <- 'return' explist? ';'? - forNum <- NAME '=' exp^ErrExprFor1 ','^ErrCommaFor exp^ErrExprFor2 (',' exp^ErrExprFor3)? - forIn <- namelist 'in'^ErrInFor explist^ErrEListFor - localFunc <- 'function' NAME^ErrNameLFunc funcbody - localAssign <- namelist ('=' explist^ErrEListLAssign)? - label <- '::' NAME^ErrLabel '::'^ErrCloseLabel - funcname <- NAME ('.' NAME^ErrNameFunc1)* (':' NAME^ErrNameFunc2)? - varlist <- var (',' var^ErrVarList)* - namelist <- NAME (',' NAME)* - explist <- exp (',' exp^ErrExprList )* - - exp <- expOR -> fixexp - expOR <- (expAND (operatorOr expAND^ErrOrExpr)*) -> fold - expAND <- (expREL (operatorAnd expREL^ErrAndExpr)*) -> fold - expREL <- (expBIT (operatorComparison expBIT^ErrRelExpr)*) -> fold - expBIT <- (expCAT (operatorBitwise expCAT^ErrBitwiseExpr)*) -> fold - expCAT <- (expADD (operatorStrcat expCAT^ErrConcatExpr)?) -> fixexp - expADD <- (expMUL (operatorAddSub expMUL^ErrAddExpr)*) -> fold - expMUL <- (expUNA (operatorMulDivMod expUNA^ErrMulExpr)*) -> fold - expUNA <- ((operatorUnary expUNA^ErrUnaryExpr) / expPOW) -> fixexp - expPOW <- (expTokens (operatorPower expUNA^ErrPowExpr)?) -> fixexp - - expTokens <- 'nil' / 'false' / 'true' / - number / - string / - '...' / - 'function' funcbody / - tableconstructor / - prefixexp - - prefixexp <- varOrExp nameAndArgs* - functioncall <- varOrExp nameAndArgs+ - varOrExp <- var / brackexp - brackexp <- '(' exp^ErrExprParen ')'^ErrCParenExpr - var <- (NAME / brackexp varSuffix) varSuffix* - varSuffix <- nameAndArgs* ('[' exp^ErrExprIndex ']'^ErrCBracketIndex / '.' !'.' NAME^ErrNameIndex) - nameAndArgs <- (':' !':' NAME^ErrNameMeth args^ErrMethArgs) / - args - args <- '(' explist? ')'^ErrCParenArgs / tableconstructor / string - funcbody <- '('^ErrOParenPList parlist? ')'^ErrCParenPList block 'end'^ErrEndFunc - parlist <- namelist (',' '...'^ErrParList)? / '...' - tableconstructor<- '{' fieldlist? '}'^ErrCBraceTable - fieldlist <- field (fieldsep field)* fieldsep? - field <- !OPEN '[' exp^ErrExprFKey ']'^ErrCBracketFKey '='^ErrEqField exp^ErrExprField / - NAME '=' exp / - exp - fieldsep <- ',' / ';' - operatorOr <- 'or' - operatorAnd <- 'and' - operatorComparison<- '<=' / '>=' / '~=' / '==' / '<' !'<' / '>' !'>' - operatorStrcat <- !'...' '..' - operatorAddSub <- '+' / '-' - operatorMulDivMod<- '*' / '%' / '//' / '/' - operatorBitwise <- '&' / '|' / !'~=' '~' / '<<' / '>>' - operatorUnary <- 'not' / '#' / '-' / !'~=' '~' - operatorPower <- '^' - number <- FLOAT / HEX_FLOAT / HEX / INT - string <- NORMALSTRING / CHARSTRING / LONGSTRING - -- lexer - fragment - RESERVED <- KEYWORDS !IDREST - fragment - IDREST <- [a-zA-Z_0-9] - fragment - KEYWORDS <- 'and' / 'break' / 'do' / 'elseif' / 'else' / 'end' / - 'false' / 'for' / 'function' / 'goto' / 'if' / 'in' / - 'local' / 'nil' / 'not' / 'or' / 'repeat' / 'return' / - 'then' / 'true' / 'until' / 'while' - NAME <- !RESERVED [a-zA-Z_] [a-zA-Z_0-9]* - fragment - NORMALSTRING <- '"' {( ESC / [^"\] )*} '"'^ErrQuote - fragment - CHARSTRING <- "'" {( ESC / [^\'] )*} "'"^ErrQuote - fragment - LONGSTRING <- (OPEN {(!CLOSEEQ .)*} CLOSE^ErrCloseLStr) -> 1 -- capture only the string - - fragment - OPEN <- '[' {:openEq: EQUALS :} '[' %nl? - fragment - CLOSE <- ']' {EQUALS} ']' - fragment - EQUALS <- '='* - fragment - CLOSEEQ <- (CLOSE =openEq) => equals - - INT <- DIGIT+ - HEX <- '0' [xX] HEXDIGIT+^ErrDigitHex - FLOAT <- DIGIT+ '.' DIGIT* ExponentPart? / - '.' !'.' DIGIT+^ErrDigitDeci ExponentPart? / - DIGIT+ ExponentPart - HEX_FLOAT <- '0' [xX] HEXDIGIT+ '.' HEXDIGIT* HexExponentPart? / - '0' [xX] '.' HEXDIGIT+ HexExponentPart? / - '0' [xX] HEXDIGIT+^ErrDigitHex HexExponentPart - fragment - ExponentPart <- [eE] [+-]? DIGIT+^ErrDigitExpo - fragment - HexExponentPart <- [pP] [+-]? DIGIT+^ErrDigitExpo - fragment - ESC <- '\' [abfnrtvz"'\] / - '\' %nl / - DECESC / - HEXESC/ - UTFESC/ - '\' %{ErrEscSeq} - fragment - DECESC <- '\' ( DIGIT DIGIT? / [0-2] DIGIT DIGIT) - fragment - HEXESC <- '\' 'x' (HEXDIGIT HEXDIGIT)^ErrHexEsc - fragment - UTFESC <- '\' 'u' '{'^ErrOBraceUEsc HEXDIGIT+^ErrDigitUEsc '}'^ErrCBraceUEsc - fragment - DIGIT <- [0-9] - fragment - HEXDIGIT <- [0-9a-fA-F] - - - fragment - COMMENT <- '--' LONGSTRING -> 0 -- skip this - fragment - LINE_COMMENT <- '--' COM_TYPES ( %nl / !.) - fragment - COM_TYPES <- '[' '='* [^[=%nl] [^%nl]* / - '[' '='* / - [^[%nl] [^%nl]* / - '' - fragment - SHEBANG <- '#' '!' [^%nl]* - - - SKIP <- %nl / %s / COMMENT / LINE_COMMENT / SHEBANG - fragment - HELPER <- RESERVED / '(' / ')' -- for sync expression - SYNC <- ((!HELPER !SKIP .)+ / .?) SKIP* -- either sync to reserved keyword or skip characters and consume them - -]==],{ equals = equals, fixexp = fixexp, fold = fold }) -local errnr = 1 -local function err (desc, line, col, sfail, recexp) - print("Syntax error #"..errnr..": "..desc.." at line "..line.."(col "..col..")") - errnr = errnr+1 -end -local function parse (input) - errnr = 1 - local ast, errs = pg.parse(input,grammar,err) - return ast, errs -end -return {parse=parse} diff --git a/06/deps/parser-gen/parsers/tiny-parser.lua b/06/deps/parser-gen/parsers/tiny-parser.lua deleted file mode 100644 index 3a696e5..0000000 --- a/06/deps/parser-gen/parsers/tiny-parser.lua +++ /dev/null @@ -1,55 +0,0 @@ -package.path = package.path .. ";../?.lua" -local pg = require "parser-gen" -local peg = require "peg-parser" -local errs = {errMissingThen = "Missing Then"} -pg.setlabels(errs) - - -local grammar = pg.compile([[ - - program <- stmtsequence !. - stmtsequence <- statement (';' statement)* - statement <- ifstmt / repeatstmt / assignstmt / readstmt / writestmt - ifstmt <- 'if' exp 'then'^errMissingThen stmtsequence elsestmt? 'end' - elsestmt <- ('else' stmtsequence) - repeatstmt <- 'repeat' stmtsequence 'until' exp - assignstmt <- IDENTIFIER ':=' exp - readstmt <- 'read' IDENTIFIER - writestmt <- 'write' exp - exp <- simpleexp (COMPARISONOP simpleexp)* - COMPARISONOP <- '<' / '=' - simpleexp <- term (ADDOP term)* - ADDOP <- [+-] - term <- factor (MULOP factor)* - MULOP <- [*/] - factor <- '(' exp ')' / NUMBER / IDENTIFIER - - NUMBER <- '-'? [0-9]+ - KEYWORDS <- 'if' / 'repeat' / 'read' / 'write' / 'then' / 'else' / 'end' / 'until' - RESERVED <- KEYWORDS ![a-zA-Z] - IDENTIFIER <- !RESERVED [a-zA-Z]+ - HELPER <- ';' / %nl / %s / KEYWORDS / !. - SYNC <- (!HELPER .)* - -]], _, true) -local errors = 0 -local function printerror(desc,line,col,sfail,trec) - errors = errors+1 - print("Error #"..errors..": "..desc.." on line "..line.."(col "..col..")") -end - - -local function parse(input) - errors = 0 - result, errors = pg.parse(input,grammar,printerror) - return result, errors -end - -if arg[1] then - -- argument must be in quotes if it contains spaces - res, errs = parse(arg[1]) - peg.print_t(res) - peg.print_r(errs) -end -local ret = {parse=parse} -return ret diff --git a/06/deps/parser-gen/peg-parser-tests.lua b/06/deps/parser-gen/peg-parser-tests.lua deleted file mode 100644 index 2d072d1..0000000 --- a/06/deps/parser-gen/peg-parser-tests.lua +++ /dev/null @@ -1,260 +0,0 @@ -local peg = require("peg-parser") -local f = peg.pegToAST - -local eq = require "equals" -local equals = eq.equals - - --- self-description of peg-parser: - ---assert(f(peg.gram)) - --- ( p ) grouping -e = f("('a')") -res = {t="a"} - -assert(equals(e,res)) - --- 'string' literal string - -e = f("'string'") -res = {t="string"} -assert(equals(e,res)) - --- "string" literal string -e = f('"string"') -res = {t="string"} - -assert(equals(e,res)) ---[class] character class -e = f("[^a-zA-Z01]") -res = { - action = "invert", - op1 = { - action = "or", - op1 = { - action = "or", - op1 = { - action = "or", - op1 = { - action = "range", - op1 = { - s = "az" - } - }, - op2 = { - action = "range", - op1 = { - s = "AZ" - } - } - }, - op2 = { - t = "0" - } - }, - op2 = { - t = "1" - } - } -} - - -assert(equals(e,res)) - ---. any character -e = f(".") -res = {action="anychar"} - -assert(equals(e,res)) ---%name pattern defs[name] or a pre-defined pattern -e = f("%name") -res = {action="%", op1={s="name"}} - -assert(equals(e,res)) ---name non terminal -e = f("name") -res = {nt="name"} - -assert(equals(e,res)) - --- non terminal -e = f("") -res = {nt="name"} - -assert(equals(e,res)) - ---{} position capture -e = f("{}") - -res = {action="poscap"} - -assert(equals(e,res)) - ---{ p } simple capture -e = f("{name}") -res = {action="scap", op1= {nt="name"}} - -assert(equals(e,res)) - ---{: p :} anonymous group capture -e = f("{:name:}") -res = {action="gcap", op1= {nt="name"}} - -assert(equals(e,res)) - ---{:name: p :} named group capture -e = f("{:g: name:}") -res = {action="gcap", op1= {nt="name"} , op2={s="g"}} - -assert(equals(e,res)) ---{~ p ~} substitution capture -e = f("{~ name ~}") - -res = {action="subcap", op1= {nt="name"}} - -assert(equals(e,res)) - ---{| p |} table capture -e = f("{| name |}") -res = {action="tcap", op1= {nt="name"}} -assert(equals(e,res)) - ---=name back reference -e = f("=name") -res = {action="bref", op1= {s="name"}} -assert(equals(e,res)) - ---p ? optional match -e = f("name?") -res = {action="?", op1= {nt="name"}} -assert(equals(e,res)) - ---p * zero or more repetitions -e = f("name*") -res = {action="*", op1= {nt="name"}} -assert(equals(e,res)) - ---p + one or more repetitions -e = f("name+") -res = {action="+", op1= {nt="name"}} -assert(equals(e,res)) - ---p^num exactly n repetitions -e = f("name^3") -res = {action="^", op1= {nt="name"}, op2 = {num="3"}} -assert(equals(e,res)) - ---p^+num at least n repetitions -e = f("name^+3") -res = {action="^", op1= {nt="name"}, op2 = {num="+3"}} -assert(equals(e,res)) - ---p^-num at most n repetitions -e = f("name^-3") -res = {action="^", op1= {nt="name"}, op2 = {num="-3"}} -assert(equals(e,res)) - ---p^LABEL error label -e = f("name^err") -res = {action = "^LABEL", op1= {nt="name"}, op2 = {s="err"}} -assert(equals(e,res)) - ---p -> 'string' string capture -e = f("name -> 'a'") -res = {action="->", op1= {nt="name"}, op2 = {s="a"}} -assert(equals(e,res)) - ---p -> "string" string capture -e = f('name -> "a"') -res = {action="->", op1= {nt="name"}, op2 = {s="a"}} -assert(equals(e,res)) - ---p -> num numbered capture - -e = f('name -> 3') - -res = {action="->", op1= {nt="name"}, op2 = {num="3"}} - -assert(equals(e,res)) - ---p -> name function/query/string capture equivalent to p / defs[name] - -e = f('name -> func') -res = {action="->", op1= {nt="name"}, op2 = {func="func"}} - -assert(equals(e,res)) - - - ---p => name match-time capture equivalent to lpeg.Cmt(p, defs[name]) - -e = f('name => func') -res = {action="=>", op1= {nt="name"}, op2 = {func="func"}} - -assert(equals(e,res)) - - ---& p and predicate - -e = f('&name') -res = {action="&", op1= {nt="name"}} - -assert(equals(e,res)) - - ---! p not predicate - - -e = f('!name') -res = {action="!", op1= {nt="name"}} - -assert(equals(e,res)) - - ---p1 p2 p3 concatenation with left association - -e = f('name name2 name3') -res = {action="and", op1= {action = "and", op1={nt="name"}, op2={nt="name2"}}, op2={nt="name3"}} - -assert(equals(e,res)) - ---p1 / p2 / p3 ordered choice with left association - -e = f('name / name2 / name3') -res = {action="or", op1= {action = "or", op1={nt="name"}, op2={nt="name2"}}, op2={nt="name3"}} - -assert(equals(e,res)) - - ---(name <- p)+ grammar - -e = f('a <- b b <- c') -res = { - {rulename = "a", rule = {nt="b"}}, - {rulename = "b", rule = {nt="c"}} -} - -assert(equals(e,res)) - --- error labels --- %{errName} - ---peg.setlabels({errName=1}) -e = f('%{errName}') - -res = {action="label", op1={s="errName"}} - -assert(equals(e,res)) - --- a //{errName,errName2} b - ---peg.setlabels({errName=1, errName2=2}) -e = f('a //{errName,errName2} b') - -res = {action="or", condition={{s="errName"},{s="errName2"}}, op1={nt="a"}, op2={nt="b"}} - - -assert(equals(e,res)) - -print("all tests succesful") \ No newline at end of file diff --git a/06/deps/parser-gen/peg-parser.lua b/06/deps/parser-gen/peg-parser.lua deleted file mode 100644 index 1448511..0000000 --- a/06/deps/parser-gen/peg-parser.lua +++ /dev/null @@ -1,336 +0,0 @@ -local re = require("deps.lpeglabel.relabel") -local util = require("src.util") - -local peg = {} - --- from relabel.lua - -local errinfo = { - {"NoPatt", "no pattern found"}, - {"ExtraChars", "unexpected characters after the pattern"}, - - {"ExpPatt1", "expected a pattern after '/' or '//{...}'"}, - - {"ExpPatt2", "expected a pattern after '&'"}, - {"ExpPatt3", "expected a pattern after '!'"}, - - {"ExpPatt4", "expected a pattern after '('"}, - {"ExpPatt5", "expected a pattern after ':'"}, - {"ExpPatt6", "expected a pattern after '{~'"}, - {"ExpPatt7", "expected a pattern after '{|'"}, - - {"ExpPatt8", "expected a pattern after '<-'"}, - - {"ExpPattOrClose", "expected a pattern or closing '}' after '{'"}, - - {"ExpNum", "expected a number after '^', '+' or '-' (no space)"}, - {"ExpNumOrLab", "expected a number or a label after ^"}, - {"ExpCap", "expected a string, number, '{}' or name after '->'"}, - - {"ExpName1", "expected the name of a rule after '=>'"}, - {"ExpName2", "expected the name of a rule after '=' (no space)"}, - {"ExpName3", "expected the name of a rule after '<' (no space)"}, - - {"ExpLab1", "expected at least one label after '{'"}, - {"ExpLab2", "expected a label after the comma"}, - - {"ExpNameOrLab", "expected a name or label after '%' (no space)"}, - - {"ExpItem", "expected at least one item after '[' or '^'"}, - - {"MisClose1", "missing closing ')'"}, - {"MisClose2", "missing closing ':}'"}, - {"MisClose3", "missing closing '~}'"}, - {"MisClose4", "missing closing '|}'"}, - {"MisClose5", "missing closing '}'"}, -- for the captures - - {"MisClose6", "missing closing '>'"}, - {"MisClose7", "missing closing '}'"}, -- for the labels - - {"MisClose8", "missing closing ']'"}, - - {"MisTerm1", "missing terminating single quote"}, - {"MisTerm2", "missing terminating double quote"}, -} - -local errmsgs = {} -local labels = {} - -for i, err in ipairs(errinfo) do - errmsgs[i] = err[2] - labels[err[1]] = i -end - -re.setlabels(labels) - -local function concat(a,b) - return a..b -end -local function foldtable(action,t) - local re - local first = true - for key,value in pairs(t) do - if first then - re = value - first = false - else - - local temp = re - if action == "suf" then -- suffix actions - local act = value[1] - if act == "*" or act == "?" or act == "+" then - re = {action=act, op1=temp} - else - re = {action=act, op1=temp, op2=value[2]} - end - elseif action == "or" and #value == 2 then -- recovery expression - local labels = value[1] - local op2 = value[2] - re = {action=action, op1=temp, op2=op2, condition=labels} - else - re = {action=action, op1=temp, op2=value} - end - end - end - return re -end - - -local gram = [=[ - - pattern <- (exp / %{NoPatt}) (!. / %{ExtraChars}) - exp <- S (grammar / alternative) - - labels <- {| '{' {: (label / %{ExpLab1}) :} (',' {: (label / %{ExpLab2}) :})* ('}' / %{MisClose7}) |} - - - alternative <- ( {:''->'or':} {| {: seq :} ('/' (('/' {| {: labels :} S {: (seq / %{ExpPatt1}) :} |}) / (S {: (seq / %{ExpPatt1}) :} ) ) )* |} ) -> foldtable - - - seq <- ( {:''->'and':} {| {: prefix :}+ |} ) -> foldtable - - - prefix <- {| {:action: '&' :} S {:op1: (prefix / %{ExpPatt2}) :} |} - / {| {:action: '!' :} S {:op1: (prefix / %{ExpPatt3}) :} |} - / suffix - - suffix <- ( {:''->'suf':} {| primary S {| suffixaction S |}* |} ) -> foldtable - - - suffixaction <- {[+*?]} - / {'^'} {| {:num: [+-]? NUM:} |} - / '^'->'^LABEL' (label / %{ExpNumOrLab}) - / {'->'} S ((string / {| {:action:'{}'->'poscap':} |} / funcname / {|{:num: NUM :} |}) / %{ExpCap}) - / {'=>'} S (funcname / %{ExpName1}) - - - - - primary <- '(' (exp / %{ExpPatt4}) (')' / %{MisClose1}) - / term - / class - / defined - / {| {:action: '%'->'label':} ('{' / %{ExpNameOrLab}) S ({:op1: label:} / %{ExpLab1}) S ('}' / %{MisClose7}) |} - / {| {:action: '{:'->'gcap':} {:op2: defname:} ':' !'}' ({:op1:exp:} / %{ExpPatt5}) (':}' / %{MisClose2}) |} - / {| {:action: '{:'->'gcap':} ({:op1:exp:} / %{ExpPatt5}) (':}' / %{MisClose2}) |} - / {| {:action: '='->'bref':} ({:op1: defname:} / %{ExpName2}) |} - / {| {:action: '{}'->'poscap':} |} - / {| {:action: '{~'->'subcap':} ({:op1: exp:} / %{ExpPatt6}) ('~}' / %{MisClose3}) |} - / {| {:action: '{|'->'tcap':} ({:op1: exp:} / %{ExpPatt7}) ('|}' / %{MisClose4}) |} - / {| {:action: '{'->'scap':} ({:op1: exp:} / %{ExpPattOrClose}) ('}' / %{MisClose5}) |} - / {| {:action: '.'->'anychar':} |} - / !frag !nodee name S !ARROW - / '<' (name / %{ExpName3}) ('>' / %{MisClose6}) -- old-style non terminals - - grammar <- {| definition+ |} - definition <- {| (frag / nodee)? (token / nontoken) S ARROW ({:rule: exp :} / %{ExpPatt8}) |} - - label <- {| {:s: ERRORNAME :} |} - - frag <- {:fragment: 'fragment'->'1' :} ![0-9_a-z] S !ARROW - nodee <- {:node: 'node'->'1' :} ![0-9_a-z] S !ARROW - token <- {:rulename: TOKENNAME :} {:token:''->'1':} - nontoken <- {:rulename: NAMESTRING :} - - class <- '[' ( ('^' {| {:action:''->'invert':} {:op1: classset :} |} ) / classset ) (']' / %{MisClose8}) - classset <- ( {:''->'or':} {| {: (item / %{ExpItem}) :} (!']' {: (item / %{ExpItem}) :})* |} ) -> foldtable - item <- defined / range / {| {:t: . :} |} - range <- {| {:action:''->'range':} {:op1: {| {:s: ({: . :} ('-') {: [^]] :} ) -> concat :} |} :} |} - - S <- (%s / '--' [^%nl]*)* -- spaces and comments - name <- {| {:nt: TOKENNAME :} {:token:''->'1':} / {:nt: NAMESTRING :} |} - - funcname <- {| {:func: NAMESTRING :} |} - ERRORNAME <- NAMESTRING - NAMESTRING <- [A-Za-z][A-Za-z0-9_]* - TOKENNAME <- [A-Z_]+ ![0-9a-z] - defname <- {| {:s: NAMESTRING :} |} - ARROW <- '<-' - NUM <- [0-9]+ - term <- {| '"' {:t: [^"]* :} ('"' / %{MisTerm2}) / "'" {:t: [^']* :} ("'" / %{MisTerm1}) |} - string <- {| '"' {:s: [^"]* :} ('"' / %{MisTerm2}) / "'" {:s: [^']* :} ("'" / %{MisTerm1}) |} - defined <- {| {:action: '%':} {:op1: defname :} |} -]=] - -local defs = {foldtable=foldtable, concat=concat} -peg.gram = gram -peg.defs = defs -peg.labels = labels -local p = re.compile ( gram, defs) - - - - ---[[ -Function: pegToAST(input) - -Input: a grammar in PEG format, described in https://github.com/vsbenas/parser-gen - -Output: if parsing successful - a table of grammar rules, else - runtime error - -Example input: " - - Program <- stmt* / SPACE - stmt <- ('a' / 'b')+ - SPACE <- '' - -" - -Example output: { - {rulename = "Program", rule = {action = "or", op1 = {action = "*", op1 = {nt = "stmt"}}, op2 = {nt = "SPACE", token="1"}}}, - {rulename = "stmt", rule = {action = "+", op1 = {action="or", op1 = {t = "a"}, op2 = {t = "b"}}}}, - {rulename = "SPACE", rule = {t=""}, token=1}, -} - -The rules are further processed and turned into lpeg compatible format in parser-gen.lua - -Action names: -or (has parameter condition for recovery expresions) -and -& -! -+ -* -? -^num (num is a number with an optional plus or minus sign) -^label (label is an error label set with setlabels) --> -=> -tcap -gcap (op2= name, anonymous otherwise) -bref -poscap -subcap -scap -anychar -label -% -range - -Final token actions: -t - terminal -nt - non terminal -func - function definition -s - literal string -num - literal number -]]-- -local function splitlines(str) - local t = {} - local function helper(line) table.insert(t, line) return "" end - helper((str:gsub("(.-)\r?\n", helper))) - return t -end -function peg.pegToAST(input, defs) - local r, e, sfail = p:match(input, defs) - if not r then - local lab - if e == 0 then - lab = "Syntax error" - else - lab = errmsgs[e] - end - local lines = splitlines(input) - local line, col = re.calcline(input, #input - #sfail + 1) - local err = {} - table.insert(err, "L" .. line .. ":C" .. col .. ": " .. lab) - table.insert(err, lines[line]) - table.insert(err, string.rep(" ", col-1) .. "^") - error("syntax error(s) in pattern\n" .. table.concat(err, "\n"), 3) - end - - return r -end - - -function peg.print_r ( t ) -- for debugging - local print_r_cache={} - local function sub_print_r(t,indent) - if (print_r_cache[tostring(t)]) then - print(indent.."*"..tostring(t)) - else - print_r_cache[tostring(t)]=true - if (type(t)=="table") then - for pos,val in pairs(t) do - if (type(val)=="table") then - print(indent.."["..pos.."] => {") - sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) - print(indent..string.rep(" ",string.len(pos)+6).."}") - else - print(indent.."["..pos.."] => '"..tostring(val).."'") - end - end - else - print(indent..tostring(t)) - end - end - end - sub_print_r(t,"") -end -function peg.print_t ( t ) -- for debugging - local print_r_cache={} - local function sub_print_r (t,indent) - if (print_r_cache[tostring(t)]) then - print(indent.."*"..tostring(t)) - else - print_r_cache[tostring(t)]=true - if (type(t)=="table") then - local function subprint (pos,val,indent) - if (type(val)=="table") then - print(indent.."{") - sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) - print(indent..string.rep(" ",string.len(pos)-1).."},") - else - if type(val) ~= "number" then - val = "'"..tostring(val).."'" - end - - if tonumber(pos) then - print(indent..val..",") - else - print(indent..pos.."="..val..",") - end - end - end - if t["rule"] then - subprint("rule",t["rule"],indent) - end - if t["pos"] then - subprint("pos",t["pos"],indent) - end - for pos,val in pairs(t) do - if pos ~= "rule" and pos ~= "pos" then - subprint(pos,val,indent) - end - end - else - print(indent..tostring(t)) - end - end - end - sub_print_r(t,"") -end - -function peg.calcline(subject, pos) - return re.calcline(subject,pos) -end -return peg \ No newline at end of file diff --git a/06/deps/parser-gen/rockspecs/parser-gen-1.0.rockspec b/06/deps/parser-gen/rockspecs/parser-gen-1.0.rockspec deleted file mode 100644 index 8d3c886..0000000 --- a/06/deps/parser-gen/rockspecs/parser-gen-1.0.rockspec +++ /dev/null @@ -1,27 +0,0 @@ -package = "parser-gen" -version = "1.0-7" -source = { - url = "git://github.com/vsbenas/parser-gen", - tag = "v1.0" -} -description = { - summary = "A PEG parser generator that handles space characters, generates ASTs and adds error labels automatically.", - homepage = "https://github.com/vsbenas/parser-gen", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1, < 5.4", - "lpeglabel >= 0.12.2" -} -build = { - type = "builtin", - modules = { - ["parser-gen"] = "parser-gen.lua", - ["peg-parser"] = "peg-parser.lua", - ["stack"] = "stack.lua", - ["equals"] = "equals.lua", - ["errorgen"] = "errorgen.lua", - - - } -} \ No newline at end of file diff --git a/06/deps/parser-gen/rockspecs/parser-gen-1.1.rockspec b/06/deps/parser-gen/rockspecs/parser-gen-1.1.rockspec deleted file mode 100644 index ff67135..0000000 --- a/06/deps/parser-gen/rockspecs/parser-gen-1.1.rockspec +++ /dev/null @@ -1,27 +0,0 @@ -package = "parser-gen" -version = "1.1-0" -source = { - url = "git://github.com/vsbenas/parser-gen", - tag = "v1.1" -} -description = { - summary = "A PEG parser generator that handles space characters, generates ASTs and adds error labels automatically.", - homepage = "https://github.com/vsbenas/parser-gen", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1, < 5.4", - "lpeglabel >= 0.12.2" -} -build = { - type = "builtin", - modules = { - ["parser-gen"] = "parser-gen.lua", - ["peg-parser"] = "peg-parser.lua", - ["stack"] = "stack.lua", - ["equals"] = "equals.lua", - ["errorgen"] = "errorgen.lua", - - - } -} \ No newline at end of file diff --git a/06/deps/parser-gen/rockspecs/parser-gen-1.2.rockspec b/06/deps/parser-gen/rockspecs/parser-gen-1.2.rockspec deleted file mode 100644 index fec828a..0000000 --- a/06/deps/parser-gen/rockspecs/parser-gen-1.2.rockspec +++ /dev/null @@ -1,27 +0,0 @@ -package = "parser-gen" -version = "1.2-0" -source = { - url = "git://github.com/vsbenas/parser-gen", - tag = "v1.2" -} -description = { - summary = "A PEG parser generator that handles space characters, generates ASTs and adds error labels automatically.", - homepage = "https://github.com/vsbenas/parser-gen", - license = "MIT/X11" -} -dependencies = { - "lua >= 5.1, < 5.4", - "lpeglabel >= 0.12.2" -} -build = { - type = "builtin", - modules = { - ["parser-gen"] = "parser-gen.lua", - ["peg-parser"] = "peg-parser.lua", - ["stack"] = "stack.lua", - ["equals"] = "equals.lua", - ["errorgen"] = "errorgen.lua", - - - } -} \ No newline at end of file diff --git a/06/deps/parser-gen/stack.lua b/06/deps/parser-gen/stack.lua deleted file mode 100644 index b2ca5bf..0000000 --- a/06/deps/parser-gen/stack.lua +++ /dev/null @@ -1,66 +0,0 @@ --- Stack Table --- Uses a table as stack, use :push(value) and
    :pop() -local unpack = table.unpack --- GLOBAL -local Stack = {} - --- Create a Table with stack functions -function Stack:Create() - - -- stack table - local t = {} - -- entry table - t._et = {} - - -- push a value on to the stack - function t:push(...) - if ... then - local targs = {...} - -- add values - for _,v in ipairs(targs) do - table.insert(self._et, v) - end - end - end - - -- pop a value from the stack - function t:pop(num) - - -- get num values from stack - local num = num or 1 - - -- return table - local entries = {} - - -- get values into entries - for i = 1, num do - -- get last entry - if #self._et ~= 0 then - table.insert(entries, self._et[#self._et]) - -- remove last value - table.remove(self._et) - else - break - end - end - -- return unpacked entries - return unpack(entries) - end - - -- get entries - function t:getn() - return #self._et - end - - -- list values - function t:list() - for i,v in pairs(self._et) do - print(i, v) - end - end - return t -end - -return {Stack=Stack} - --- CHILLCODE™ \ No newline at end of file diff --git a/06/src/ast.lua b/06/src/ast.lua deleted file mode 100644 index 844c4ca..0000000 --- a/06/src/ast.lua +++ /dev/null @@ -1,32 +0,0 @@ -util = require('src.util') - -local function parse_tree_to_ast(parsetree) - local stack = {{i=1,node=parsetree}}; - - while #stack > 0 do - local current = table.remove(stack, 1) - - for i = current.i, #current.node, 1 do - local inner = current.node[i] - if type(inner) == "table" then - current.i = i + 1; - print(current.node.rule) - table.insert(stack, current) - table.insert(stack, {i=1, node=inner}) - break; - else - print(string.rep(" ", #stack) .. inner) - end - end - end - - return {} -end - -local function print_ast(ast) -end - -return { - parse_tree_to_ast = parse_tree_to_ast, - print = print_ast -} diff --git a/06/src/main.c b/06/src/main.c deleted file mode 100644 index 5530ca7..0000000 --- a/06/src/main.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include -#include "lptree.h" - -int main (void) { - int error; - lua_State *L = luaL_newstate(); - luaL_openlibs(L); - - luaopen_lpeglabel(L); - lua_setglobal(L, "m"); - luaL_loadfile(L, "src/main.lua"); - error = lua_pcall(L, 0, LUA_MULTRET, 0); - if (error) { - fprintf(stderr, "%s", lua_tostring(L, -1)); - lua_pop(L, 1); /* pop error message from the stack */ - } - printf("\n"); - lua_close(L); - return 0; -} \ No newline at end of file diff --git a/06/src/main.lua b/06/src/main.lua deleted file mode 100644 index b2b52a9..0000000 --- a/06/src/main.lua +++ /dev/null @@ -1,24 +0,0 @@ -local pg = require("deps.parser-gen.parser-gen") -local util = require("src.util") -local ast = require("src.ast") - -local f = assert(io.open("zig.peg", "r")) -local raw = f:read("*all") -f:close() -local grammar = pg.compile(raw) - -f = assert(io.open("test.zig", "r")) -raw = f:read("*all") -f:close() - -local errors = 0 -local function printerror(desc, line, col, sfail, trec) - errors = errors+1 - print("Error #"..errors..": "..desc.." on line "..line.."(col "..col..")") -end - -local result, _ = pg.parse(raw, grammar, printerror) - -print(util.dump(result)) -local my_ast = ast.parse_tree_to_ast(result) - diff --git a/06/src/util.lua b/06/src/util.lua deleted file mode 100644 index 04a3ff8..0000000 --- a/06/src/util.lua +++ /dev/null @@ -1,34 +0,0 @@ -local function dump(value, call_indent) - if not call_indent then - call_indent = "" - end - - local indent = call_indent .. " " - - local output = "" - - if type(value) == "table" then - output = output .. "{" - local first = true - for inner_key, inner_value in pairs ( value ) do - if not first then - output = output .. ", " - else - first = false - end - output = output .. "\n" .. indent - output = output .. inner_key .. " = " .. dump ( inner_value, indent ) - end - output = output .. "\n" .. call_indent .. "}" - - elseif type (value) == "userdata" then - output = "userdata" - else - output = value - end - return output -end - -return { - dump = dump -} \ No newline at end of file diff --git a/06/test.zig b/06/test.zig deleted file mode 100644 index f4f43d0..0000000 --- a/06/test.zig +++ /dev/null @@ -1 +0,0 @@ -const _x = 0; diff --git a/06/zig.peg b/06/zig.peg deleted file mode 100644 index b4976d9..0000000 --- a/06/zig.peg +++ /dev/null @@ -1,516 +0,0 @@ -Root <- container_doc_comment? container_members eof - -container_members <- container_declarations (container_field COMMA)* (container_field / container_declarations) - -container_declarations - <- test_decl container_declarations - / top_level_comptime container_declarations - / doc_comment? KEYWORD_PUB? top_level_decl container_declarations - / end_of_word - -test_decl <- doc_comment? KEYWORD_TEST block - -top_level_comptime <- doc_comment? KEYWORD_COMPTIME block_expr - -top_level_decl - <- (KEYWORD_EXPORT / KEYWORD_EXTERN / (KEYWORD_INLINE / KEYWORD_NOINLINE))? fn_proto (SEMICOLON / block) - / (KEYWORD_EXPORT / KEYWORD_EXTERN )? KEYWORD_THREADLOCAL? var_decl - / KEYWORD_USINGNAMESPACE expr SEMICOLON - -fn_proto <- KEYWORD_FN IDENTIFIER? LPAREN param_decl_list RPAREN byte_align? link_section? call_conv? EXCLAMATIONMARK? type_expr - -var_decl <- (KEYWORD_CONST / KEYWORD_VAR) IDENTIFIER (COLON type_expr)? byte_align? link_section? (EQUAL expr)? SEMICOLON - -container_field <- doc_comment? KEYWORD_COMPTIME? IDENTIFIER (COLON (KEYWORD_ANYTYPE / type_expr) byte_align?)? (EQUAL expr)? - -statement - <- KEYWORD_COMPTIME? var_decl - / KEYWORD_COMPTIME block_expr_satement - / KEYWORD_NOSUSPEND block_expr_satement - / KEYWORD_SUSPEND block_expr_satement - / KEYWORD_DEFER block_expr_satement - / KEYWORD_ERRDEFER payload? block_expr_satement - / if_statement - / labeled_statement - / switch_expr - / assign_expr SEMICOLON - -if_statement - <- if_prefix block_expr ( KEYWORD_ELSE payload? statement )? - / if_prefix assign_expr ( SEMICOLON / KEYWORD_ELSE payload? statement ) - -labeled_statement <- block_label? (block / loop_statement) - -loop_statement <- KEYWORD_INLINE? (for_statement / while_statement) - -for_statement - <- for_prefix block_expr ( KEYWORD_ELSE statement )? - / for_prefix assign_expr ( SEMICOLON / KEYWORD_ELSE statement ) - -while_statement - <- WhilePrefix block_expr ( KEYWORD_ELSE payload? statement )? - / WhilePrefix assign_expr ( SEMICOLON / KEYWORD_ELSE payload? statement ) - -block_expr_satement - <- block_expr - / assign_expr SEMICOLON - -block_expr <- block_label? block - -assign_expr <- expr (AssignOp expr)? - -expr <- bool_or_expr - -bool_or_expr <- BoolAndExpr (KEYWORD_OR BoolAndExpr)* - -BoolAndExpr <- CompareExpr (KEYWORD_AND CompareExpr)* - -CompareExpr <- BitwiseExpr (CompareOp BitwiseExpr)? - -BitwiseExpr <- BitShiftExpr (BitwiseOp BitShiftExpr)* - -BitShiftExpr <- AdditionExpr (BitShiftOp AdditionExpr)* - -AdditionExpr <- MultiplyExpr (AdditionOp MultiplyExpr)* - -MultiplyExpr <- PrefixExpr (MultiplyOp PrefixExpr)* - -PrefixExpr <- PrefixOp* PrimaryExpr - -PrimaryExpr - <- AsmExpr - / IfExpr - / KEYWORD_BREAK BreakLabel? expr? - / KEYWORD_COMPTIME expr - / KEYWORD_NOSUSPEND expr - / KEYWORD_CONTINUE BreakLabel? - / KEYWORD_RESUME expr - / KEYWORD_RETURN expr? - / block_label? LoopExpr - / block - / CurlySuffixExpr - -IfExpr <- if_prefix expr (KEYWORD_ELSE payload? expr)? - -block <- LBRACE statement* RBRACE - -LoopExpr <- KEYWORD_INLINE? (ForExpr / WhileExpr) - -ForExpr <- for_prefix expr (KEYWORD_ELSE expr)? - -WhileExpr <- WhilePrefix expr (KEYWORD_ELSE payload? expr)? - -CurlySuffixExpr <- type_expr InitList? - -InitList - <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE - / LBRACE expr (COMMA expr)* COMMA? RBRACE - / LBRACE RBRACE - -type_expr <- PrefixTypeOp* ErrorUnionExpr - -ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK type_expr)? - -SuffixExpr - <- KEYWORD_ASYNC PrimaryTypeExpr SuffixOp* FnCallArguments - / PrimaryTypeExpr (SuffixOp / FnCallArguments)* - -PrimaryTypeExpr - <- BUILTINIDENTIFIER FnCallArguments - / CHAR_LITERAL - / ContainerDecl - / DOT IDENTIFIER - / DOT InitList - / ErrorSetDecl - / FLOAT - / fn_proto - / GroupedExpr - / LabeledTypeExpr - / IDENTIFIER - / IfTypeExpr - / INTEGER - / KEYWORD_COMPTIME type_expr - / KEYWORD_ERROR DOT IDENTIFIER - / KEYWORD_ANYFRAME - / KEYWORD_UNREACHABLE - / STRINGLITERAL - / switch_expr - -ContainerDecl <- (KEYWORD_EXTERN / KEYWORD_PACKED)? ContainerDeclAuto - -ErrorSetDecl <- KEYWORD_ERROR LBRACE IdentifierList RBRACE - -GroupedExpr <- LPAREN expr RPAREN - -IfTypeExpr <- if_prefix type_expr (KEYWORD_ELSE payload? type_expr)? - -LabeledTypeExpr - <- block_label block - / block_label? LoopTypeExpr - -LoopTypeExpr <- KEYWORD_INLINE? (ForTypeExpr / WhileTypeExpr) - -ForTypeExpr <- for_prefix type_expr (KEYWORD_ELSE type_expr)? - -WhileTypeExpr <- WhilePrefix type_expr (KEYWORD_ELSE payload? type_expr)? - -switch_expr <- KEYWORD_SWITCH LPAREN expr RPAREN LBRACE switch_prong_list RBRACE - -AsmExpr <- KEYWORD_ASM KEYWORD_VOLATILE? LPAREN expr AsmOutput? RPAREN - -AsmOutput <- COLON AsmOutputList AsmInput? - -AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW type_expr / IDENTIFIER) RPAREN - -AsmInput <- COLON AsmInputList AsmClobbers? - -AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN expr RPAREN - -AsmClobbers <- COLON StringList - -BreakLabel <- COLON IDENTIFIER - -block_label <- IDENTIFIER COLON - -FieldInit <- DOT IDENTIFIER EQUAL expr - -while_continue_expr <- COLON LPAREN assign_expr RPAREN - -link_section <- KEYWORD_LINKSECTION LPAREN expr RPAREN - -call_conv <- KEYWORD_CALLCONV LPAREN expr RPAREN - -param_decl - <- doc_comment? (KEYWORD_NOALIAS / KEYWORD_COMPTIME)? (IDENTIFIER COLON)? param_type - / DOT3 - -param_type - <- KEYWORD_ANYTYPE - / type_expr - -if_prefix <- KEYWORD_IF LPAREN expr RPAREN PtrPayload? - -WhilePrefix <- KEYWORD_WHILE LPAREN expr RPAREN PtrPayload? while_continue_expr? - -for_prefix <- KEYWORD_FOR LPAREN expr RPAREN ptr_index_payload - -payload <- PIPE IDENTIFIER PIPE - -PtrPayload <- PIPE ASTERISK? IDENTIFIER PIPE - -ptr_index_payload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE - - -SwitchProng <- SwitchCase EQUALRARROW PtrPayload? assign_expr - -SwitchCase - <- SwitchItem (COMMA SwitchItem)* COMMA? - / KEYWORD_ELSE - -SwitchItem <- expr (DOT3 expr)? - -AssignOp - <- ASTERISKEQUAL - / SLASHEQUAL - / PERCENTEQUAL - / PLUSEQUAL - / MINUSEQUAL - / LARROW2EQUAL - / RARROW2EQUAL - / AMPERSANDEQUAL - / CARETEQUAL - / PIPEEQUAL - / ASTERISKPERCENTEQUAL - / PLUSPERCENTEQUAL - / MINUSPERCENTEQUAL - / EQUAL - -CompareOp - <- EQUALEQUAL - / EXCLAMATIONMARKEQUAL - / LARROW - / RARROW - / LARROWEQUAL - / RARROWEQUAL - -BitwiseOp - <- AMPERSAND - / CARET - / PIPE - / KEYWORD_ORELSE - / KEYWORD_CATCH payload? - -BitShiftOp - <- LARROW2 - / RARROW2 - -AdditionOp - <- PLUS - / MINUS - / PLUS2 - / PLUSPERCENT - / MINUSPERCENT - -MultiplyOp - <- PIPE2 - / ASTERISK - / SLASH - / PERCENT - / ASTERISK2 - / ASTERISKPERCENT - -PrefixOp - <- EXCLAMATIONMARK - / MINUS - / TILDE - / MINUSPERCENT - / AMPERSAND - / KEYWORD_TRY - / KEYWORD_AWAIT - -PrefixTypeOp - <- QUESTIONMARK - / KEYWORD_ANYFRAME MINUSRARROW - / SliceTypeStart (byte_align / KEYWORD_CONST / KEYWORD_VOLATILE / KEYWORD_ALLOWZERO)* - / PtrTypeStart (KEYWORD_ALIGN LPAREN expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_CONST / KEYWORD_VOLATILE / KEYWORD_ALLOWZERO)* - / ArrayTypeStart - -SuffixOp - <- LBRACKET expr (DOT2 (expr? (COLON expr)?)?)? RBRACKET - / DOT IDENTIFIER - / DOTASTERISK - / DOTQUESTIONMARK - -FnCallArguments <- LPAREN expr_list RPAREN - -SliceTypeStart <- LBRACKET (COLON expr)? RBRACKET - -PtrTypeStart - <- ASTERISK - / ASTERISK2 - / LBRACKET ASTERISK (LETTERC / COLON expr)? RBRACKET - -ArrayTypeStart <- LBRACKET expr (COLON expr)? RBRACKET - -ContainerDeclAuto <- ContainerDeclType LBRACE container_doc_comment? container_members RBRACE - -ContainerDeclType - <- KEYWORD_STRUCT - / KEYWORD_OPAQUE - / KEYWORD_ENUM (LPAREN expr RPAREN)? - / KEYWORD_UNION (LPAREN (KEYWORD_ENUM (LPAREN expr RPAREN)? / expr) RPAREN)? - -byte_align <- KEYWORD_ALIGN LPAREN expr RPAREN - -IdentifierList <- (doc_comment? IDENTIFIER COMMA)* (doc_comment? IDENTIFIER)? - -switch_prong_list <- (SwitchProng COMMA)* SwitchProng? - -AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem? - -AsmInputList <- (AsmInputItem COMMA)* AsmInputItem? - -StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL? - -param_decl_list <- (param_decl COMMA)* param_decl? - -expr_list <- (expr COMMA)* expr? - -eof <- !. -bin <- [01] -bin_ <- '_'? bin -oct <- [0-7] -oct_ <- '_'? oct -hex <- [0-9a-fA-F] -hex_ <- '_'? hex -dec <- [0-9] -dec_ <- '_'? dec - -bin_int <- bin bin_* -oct_int <- oct oct_* -dec_int <- dec dec_* -hex_int <- hex hex_* - -ox80_oxBF <- [\200-\277] -oxF4 <- '\364' -ox80_ox8F <- [\200-\217] -oxF1_oxF3 <- [\361-\363] -oxF0 <- '\360' -ox90_0xBF <- [\220-\277] -oxEE_oxEF <- [\356-\357] -oxED <- '\355' -ox80_ox9F <- [\200-\237] -oxE1_oxEC <- [\341-\354] -oxE0 <- '\340' -oxA0_oxBF <- [\240-\277] -oxC2_oxDF <- [\302-\337] - -mb_utf8_literal <- - oxF4 ox80_ox8F ox80_oxBF ox80_oxBF - / oxF1_oxF3 ox80_oxBF ox80_oxBF ox80_oxBF - / oxF0 ox90_0xBF ox80_oxBF ox80_oxBF - / oxEE_oxEF ox80_oxBF ox80_oxBF - / oxED ox80_ox9F ox80_oxBF - / oxE1_oxEC ox80_oxBF ox80_oxBF - / oxE0 oxA0_oxBF ox80_oxBF - / oxC2_oxDF ox80_oxBF - -ascii_char_not_nl_slash_squote <- [\000-\011\013-\046-\050-\133\135-\177] - -char_escape - <- "\\x" hex hex - / "\\u{" hex+ "}" - / "\\" [nr\\t'"] -char_char - <- mb_utf8_literal - / char_escape - / ascii_char_not_nl_slash_squote - -string_char - <- char_escape - / [^\\"\n] - -container_doc_comment <- ('//!' [^\n]* [ \n]*)+ -doc_comment <- ('///' [^\n]* [ \n]*)+ -line_comment <- '//' ![!/][^\n]* / '////' [^\n]* -line_string <- ("\\\\" [^\n]* [ \n]*)+ - -CHAR_LITERAL <- "'" char_char "'" -FLOAT - <- "0x" hex_int "." hex_int ([pP] [-+]? dec_int)? - / dec_int "." dec_int ([eE] [-+]? dec_int)? - / "0x" hex_int [pP] [-+]? dec_int - / dec_int [eE] [-+]? dec_int -INTEGER - <- "0b" bin_int - / "0o" oct_int - / "0x" hex_int - / dec_int -STRINGLITERALSINGLE <- '"' string_char* '"' -STRINGLITERAL - <- STRINGLITERALSINGLE - / (line_string)+ -IDENTIFIER - <- !KEYWORD [A-Za-z_] [A-Za-z0-9_]* -BUILTINIDENTIFIER <- "@"[A-Za-z_][A-Za-z0-9_]* - - -AMPERSAND <- '&' ![=] -AMPERSANDEQUAL <- '&=' -ASTERISK <- '*' ![*%=] -ASTERISK2 <- '**' -ASTERISKEQUAL <- '*=' -ASTERISKPERCENT <- '*%' ![=] -ASTERISKPERCENTEQUAL <- '*%=' -CARET <- '^' ![=] -CARETEQUAL <- '^=' -COLON <- ':' -COMMA <- ',' -DOT <- '.' ![*.?] -DOT2 <- '..' ![.] -DOT3 <- '...' -DOTASTERISK <- '.*' -DOTQUESTIONMARK <- '.?' -EQUAL <- '=' ![>=] -EQUALEQUAL <- '==' -EQUALRARROW <- '=>' -EXCLAMATIONMARK <- '!' ![=] -EXCLAMATIONMARKEQUAL <- '!=' -LARROW <- '<' ![<=] -LARROW2 <- '<<' ![=] -LARROW2EQUAL <- '<<=' -LARROWEQUAL <- '<=' -LBRACE <- '{' -LBRACKET <- '[' -LPAREN <- '(' -MINUS <- '-' ![%=>] -MINUSEQUAL <- '-=' -MINUSPERCENT <- '-%' ![=] -MINUSPERCENTEQUAL <- '-%=' -MINUSRARROW <- '->' -PERCENT <- '%' ![=] -PERCENTEQUAL <- '%=' -PIPE <- '|' ![|=] -PIPE2 <- '||' -PIPEEQUAL <- '|=' -PLUS <- '+' ![%+=] -PLUS2 <- '++' -PLUSEQUAL <- '+=' -PLUSPERCENT <- '+%' ![=] -PLUSPERCENTEQUAL <- '+%=' -LETTERC <- 'c' -QUESTIONMARK <- '?' -RARROW <- '>' ![>=] -RARROW2 <- '>>' ![=] -RARROW2EQUAL <- '>>=' -RARROWEQUAL <- '>=' -RBRACE <- '}' -RBRACKET <- ']' -RPAREN <- ')' -SEMICOLON <- ';' -SLASH <- '/' ![=] -SLASHEQUAL <- '/=' -TILDE <- '~' - -fragment end_of_word <- ![a-zA-Z0-9_] -KEYWORD_ALIGN <- 'align' end_of_word -KEYWORD_ALLOWZERO <- 'allowzero' end_of_word -KEYWORD_AND <- 'and' end_of_word -KEYWORD_ANYFRAME <- 'anyframe' end_of_word -KEYWORD_ANYTYPE <- 'anytype' end_of_word -KEYWORD_ASM <- 'asm' end_of_word -KEYWORD_ASYNC <- 'async' end_of_word -KEYWORD_AWAIT <- 'await' end_of_word -KEYWORD_BREAK <- 'break' end_of_word -KEYWORD_CALLCONV <- 'callconv' end_of_word -KEYWORD_CATCH <- 'catch' end_of_word -KEYWORD_COMPTIME <- 'comptime' end_of_word -KEYWORD_CONST <- 'const' end_of_word -KEYWORD_CONTINUE <- 'continue' end_of_word -KEYWORD_DEFER <- 'defer' end_of_word -KEYWORD_ELSE <- 'else' end_of_word -KEYWORD_ENUM <- 'enum' end_of_word -KEYWORD_ERRDEFER <- 'errdefer' end_of_word -KEYWORD_ERROR <- 'error' end_of_word -KEYWORD_EXPORT <- 'export' end_of_word -KEYWORD_EXTERN <- 'extern' end_of_word -KEYWORD_FN <- 'fn' end_of_word -KEYWORD_FOR <- 'for' end_of_word -KEYWORD_IF <- 'if' end_of_word -KEYWORD_INLINE <- 'inline' end_of_word -KEYWORD_NOALIAS <- 'noalias' end_of_word -KEYWORD_NOSUSPEND <- 'nosuspend' end_of_word -KEYWORD_NOINLINE <- 'noinline' end_of_word -KEYWORD_OPAQUE <- 'opaque' end_of_word -KEYWORD_OR <- 'or' end_of_word -KEYWORD_ORELSE <- 'orelse' end_of_word -KEYWORD_PACKED <- 'packed' end_of_word -KEYWORD_PUB <- 'pub' end_of_word -KEYWORD_RESUME <- 'resume' end_of_word -KEYWORD_RETURN <- 'return' end_of_word -KEYWORD_LINKSECTION <- 'linksection' end_of_word -KEYWORD_STRUCT <- 'struct' end_of_word -KEYWORD_SUSPEND <- 'suspend' end_of_word -KEYWORD_SWITCH <- 'switch' end_of_word -KEYWORD_TEST <- 'test' end_of_word -KEYWORD_THREADLOCAL <- 'threadlocal' end_of_word -KEYWORD_TRY <- 'try' end_of_word -KEYWORD_UNION <- 'union' end_of_word -KEYWORD_UNREACHABLE <- 'unreachable' end_of_word -KEYWORD_USINGNAMESPACE <- 'usingnamespace' end_of_word -KEYWORD_VAR <- 'var' end_of_word -KEYWORD_VOLATILE <- 'volatile' end_of_word -KEYWORD_WHILE <- 'while' end_of_word - -KEYWORD <- KEYWORD_ALIGN / KEYWORD_ALLOWZERO / KEYWORD_AND / KEYWORD_ANYFRAME - / KEYWORD_ANYTYPE / KEYWORD_ASM / KEYWORD_ASYNC / KEYWORD_AWAIT - / KEYWORD_BREAK / KEYWORD_CALLCONV / KEYWORD_CATCH / KEYWORD_COMPTIME - / KEYWORD_CONST / KEYWORD_CONTINUE / KEYWORD_DEFER / KEYWORD_ELSE - / KEYWORD_ENUM / KEYWORD_ERRDEFER / KEYWORD_ERROR / KEYWORD_EXPORT - / KEYWORD_EXTERN / KEYWORD_FN / KEYWORD_FOR / KEYWORD_IF - / KEYWORD_INLINE / KEYWORD_NOALIAS / KEYWORD_NOSUSPEND / KEYWORD_NOINLINE - / KEYWORD_OPAQUE / KEYWORD_OR / KEYWORD_ORELSE / KEYWORD_PACKED - / KEYWORD_PUB / KEYWORD_RESUME / KEYWORD_RETURN / KEYWORD_LINKSECTION - / KEYWORD_STRUCT / KEYWORD_SUSPEND / KEYWORD_SWITCH / KEYWORD_TEST - / KEYWORD_THREADLOCAL / KEYWORD_TRY / KEYWORD_UNION / KEYWORD_UNREACHABLE - / KEYWORD_USINGNAMESPACE / KEYWORD_VAR / KEYWORD_VOLATILE / KEYWORD_WHILE \ No newline at end of file diff --git a/Makefile b/Makefile index 07f2436..dde1c92 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,6 @@ all: $(MAKE) -C 05 tcc $(MAKE) -C 05 tcc-files $(MAKE) -C 05 musl - $(MAKE) -C 06 clean: $(MAKE) -C 00 clean $(MAKE) -C 01 clean @@ -19,6 +18,5 @@ clean: $(MAKE) -C 04 clean $(MAKE) -C 04a clean $(MAKE) -C 05 clean - $(MAKE) -C 06 clean rm -f markdown rm -f README.html diff --git a/scripts/run.sh b/scripts/run.sh index 61b483b..077b1bc 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -cd 06 && ./zsh 2>&1 | less -R +F +cd 06 && ./zigsh 2>&1 | less -R +F diff --git a/scripts/script1.sh b/scripts/script1.sh new file mode 100755 index 0000000..4c19361 --- /dev/null +++ b/scripts/script1.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +cat 06/ast.dot | + dot -Grankdir=LR -Gmargin=0.7 '-Gbgcolor=#ffffff00' -Gfontname=MonocraftNerdFont -Gcolor=#d5c4a1 -Gfontcolor=#d5c4a1 -Ecolor=#d5c4a1 -Efontcolor=#d5c4a1 -Ncolor=#d5c4a1 -Nfontcolor=#d5c4a1 -Ecolor=white -T png | + kitty +kitten icat && read -n 1 -s -r -p "Press any key to continue" + diff --git a/tofix/lua-5.3.4-tests/all.lua b/tofix/lua-5.3.4-tests/all.lua deleted file mode 100644 index 23f3e1c..0000000 --- a/tofix/lua-5.3.4-tests/all.lua +++ /dev/null @@ -1,292 +0,0 @@ -#!../lua --- $Id: all.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice at the end of this file - - - -local version = "Lua 5.3" -if _VERSION ~= version then - io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION, - "\nExiting tests\n") - return -end - - -_G._ARG = arg -- save arg for other tests - - --- next variables control the execution of some tests --- true means no test (so an undefined variable does not skip a test) --- defaults are for Linux; test everything. --- Make true to avoid long or memory consuming tests -_soft = rawget(_G, "_soft") or false --- Make true to avoid non-portable tests -_port = rawget(_G, "_port") or false --- Make true to avoid messages about tests not performed -_nomsg = rawget(_G, "_nomsg") or false - - -local usertests = rawget(_G, "_U") - -if usertests then - -- tests for sissies ;) Avoid problems - _soft = true - _port = true - _nomsg = true -end - --- tests should require debug when needed -debug = nil - -if usertests then - T = nil -- no "internal" tests for user tests -else - T = rawget(_G, "T") -- avoid problems with 'strict' module -end - -math.randomseed(0) - ---[=[ - example of a long [comment], - [[spanning several [lines]]] - -]=] - -print("current path:\n****" .. package.path .. "****\n") - - -local initclock = os.clock() -local lastclock = initclock -local walltime = os.time() - -local collectgarbage = collectgarbage - -do -- ( - --- track messages for tests not performed -local msgs = {} -function Message (m) - if not _nomsg then - print(m) - msgs[#msgs+1] = string.sub(m, 3, -3) - end -end - -assert(os.setlocale"C") - -local T,print,format,write,assert,type,unpack,floor = - T,print,string.format,io.write,assert,type,table.unpack,math.floor - --- use K for 1000 and M for 1000000 (not 2^10 -- 2^20) -local function F (m) - local function round (m) - m = m + 0.04999 - return format("%.1f", m) -- keep one decimal digit - end - if m < 1000 then return m - else - m = m / 1000 - if m < 1000 then return round(m).."K" - else - return round(m/1000).."M" - end - end -end - -local showmem -if not T then - local max = 0 - showmem = function () - local m = collectgarbage("count") * 1024 - max = (m > max) and m or max - print(format(" ---- total memory: %s, max memory: %s ----\n", - F(m), F(max))) - end -else - showmem = function () - T.checkmemory() - local total, numblocks, maxmem = T.totalmem() - local count = collectgarbage("count") - print(format( - "\n ---- total memory: %s (%.0fK), max use: %s, blocks: %d\n", - F(total), count, F(maxmem), numblocks)) - print(format("\t(strings: %d, tables: %d, functions: %d, ".. - "\n\tudata: %d, threads: %d)", - T.totalmem"string", T.totalmem"table", T.totalmem"function", - T.totalmem"userdata", T.totalmem"thread")) - end -end - - --- --- redefine dofile to run files through dump/undump --- -local function report (n) print("\n***** FILE '"..n.."'*****") end -local olddofile = dofile -local dofile = function (n, strip) - showmem() - local c = os.clock() - print(string.format("time: %g (+%g)", c - initclock, c - lastclock)) - lastclock = c - report(n) - local f = assert(loadfile(n)) - local b = string.dump(f, strip) - f = assert(load(b)) - return f() -end - -dofile('main.lua') - -do - local next, setmetatable, stderr = next, setmetatable, io.stderr - -- track collections - local mt = {} - -- each time a table is collected, remark it for finalization - -- on next cycle - mt.__gc = function (o) - stderr:write'.' -- mark progress - local n = setmetatable(o, mt) -- remark it - end - local n = setmetatable({}, mt) -- create object -end - -report"gc.lua" -local f = assert(loadfile('gc.lua')) -f() - -dofile('db.lua') -assert(dofile('calls.lua') == deep and deep) -olddofile('strings.lua') -olddofile('literals.lua') -dofile('tpack.lua') -assert(dofile('attrib.lua') == 27) - -assert(dofile('locals.lua') == 5) -dofile('constructs.lua') -dofile('code.lua', true) -if not _G._soft then - report('big.lua') - local f = coroutine.wrap(assert(loadfile('big.lua'))) - assert(f() == 'b') - assert(f() == 'a') -end -dofile('nextvar.lua') -dofile('pm.lua') -dofile('utf8.lua') -dofile('api.lua') -assert(dofile('events.lua') == 12) -dofile('vararg.lua') -dofile('closure.lua') -dofile('coroutine.lua') -dofile('goto.lua', true) -dofile('errors.lua') -dofile('math.lua') -dofile('sort.lua', true) -dofile('bitwise.lua') -assert(dofile('verybig.lua', true) == 10); collectgarbage() -dofile('files.lua') - -if #msgs > 0 then - print("\ntests not performed:") - for i=1,#msgs do - print(msgs[i]) - end - print() -end - --- no test module should define 'debug' -assert(debug == nil) - -local debug = require "debug" - -print(string.format("%d-bit integers, %d-bit floats", - string.packsize("j") * 8, string.packsize("n") * 8)) - -debug.sethook(function (a) assert(type(a) == 'string') end, "cr") - --- to survive outside block -_G.showmem = showmem - -end --) - -local _G, showmem, print, format, clock, time, difftime, assert, open = - _G, showmem, print, string.format, os.clock, os.time, os.difftime, - assert, io.open - --- file with time of last performed test -local fname = T and "time-debug.txt" or "time.txt" -local lasttime - -if not usertests then - -- open file with time of last performed test - local f = io.open(fname) - if f then - lasttime = assert(tonumber(f:read'a')) - f:close(); - else -- no such file; assume it is recording time for first time - lasttime = nil - end -end - --- erase (almost) all globals -print('cleaning all!!!!') -for n in pairs(_G) do - if not ({___Glob = 1, tostring = 1})[n] then - _G[n] = nil - end -end - - -collectgarbage() -collectgarbage() -collectgarbage() -collectgarbage() -collectgarbage() -collectgarbage();showmem() - -local clocktime = clock() - initclock -walltime = difftime(time(), walltime) - -print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime)) - -if not usertests then - lasttime = lasttime or clocktime -- if no last time, ignore difference - -- check whether current test time differs more than 5% from last time - local diff = (clocktime - lasttime) / lasttime - local tolerance = 0.05 -- 5% - if (diff >= tolerance or diff <= -tolerance) then - print(format("WARNING: time difference from previous test: %+.1f%%", - diff * 100)) - end - assert(open(fname, "w")):write(clocktime):close() -end - -print("final OK !!!") - - - ---[[ -***************************************************************************** -* Copyright (C) 1994-2016 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/tofix/lua-5.3.4-tests/api.lua b/tofix/lua-5.3.4-tests/api.lua deleted file mode 100644 index d509248..0000000 --- a/tofix/lua-5.3.4-tests/api.lua +++ /dev/null @@ -1,1173 +0,0 @@ ----@diagnostic disable: * --- $Id: api.lua,v 1.147 2016/11/07 13:06:25 roberto Exp $ --- See Copyright Notice in file all.lua - -if T==nil then - (Message or print)('\n >>> testC not active: skipping API tests <<<\n') - return -end - -local debug = require "debug" - -local pack = table.pack - - -function tcheck (t1, t2) - assert(t1.n == (t2.n or #t2) + 1) - for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end -end - - -local function checkerr (msg, f, ...) - local stat, err = pcall(f, ...) - assert(not stat and string.find(err, msg)) -end - - -print('testing C API') - -a = T.testC("pushvalue R; return 1") -assert(a == debug.getregistry()) - - --- absindex -assert(T.testC("settop 10; absindex -1; return 1") == 10) -assert(T.testC("settop 5; absindex -5; return 1") == 1) -assert(T.testC("settop 10; absindex 1; return 1") == 1) -assert(T.testC("settop 10; absindex R; return 1") < -10) - --- testing alignment -a = T.d2s(12458954321123.0) -assert(a == string.pack("d", 12458954321123.0)) -assert(T.s2d(a) == 12458954321123.0) - -a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") -assert(a == 2 and b == 3 and not c) - -f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") -a,b,c = f() -assert(a == 2 and b == 3 and not c) - --- test that all trues are equal -a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3") -assert(a == b and a == true and c == false) -a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\ - tobool -3; tobool -3; tobool -3; return 3" -assert(a==false and b==true and c==false) - - -a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) -assert(a == 40 and b == 5 and not c) - -t = pack(T.testC("settop 5; return *", 2, 3)) -tcheck(t, {n=4,2,3}) - -t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) -assert(t.n == 10 and t[1] == nil and t[10] == nil) - -t = pack(T.testC("remove -2; return *", 2, 3, 4)) -tcheck(t, {n=2,2,4}) - -t = pack(T.testC("insert -1; return *", 2, 3)) -tcheck(t, {n=2,2,3}) - -t = pack(T.testC("insert 3; return *", 2, 3, 4, 5)) -tcheck(t, {n=4,2,5,3,4}) - -t = pack(T.testC("replace 2; return *", 2, 3, 4, 5)) -tcheck(t, {n=3,5,3,4}) - -t = pack(T.testC("replace -2; return *", 2, 3, 4, 5)) -tcheck(t, {n=3,2,3,5}) - -t = pack(T.testC("remove 3; return *", 2, 3, 4, 5)) -tcheck(t, {n=3,2,4,5}) - -t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5)) -tcheck(t, {n=4,2,3,3,5}) - -t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5)) -tcheck(t, {n=4,2,3,4,3}) - -do -- testing 'rotate' - local t = {10, 20, 30, 40, 50, 60} - for i = -6, 6 do - local s = string.format("rotate 2 %d; return 7", i) - local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60)) - tcheck(t1, t) - table.insert(t, 1, table.remove(t)) - end - - t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40)) - tcheck(t, {10, 20, 40, 30}) - t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40)) - tcheck(t, {10, 20, 40, 30}) - - -- some corner cases - t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40)) - tcheck(t, {10, 20, 30, 40}) - t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40)) - tcheck(t, {10, 20, 30, 40}) - t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40)) - tcheck(t, {10, 20, 30, 40}) -end - --- testing non-function message handlers -do - local f = T.makeCfunc[[ - getglobal error - pushstring bola - pcall 1 1 1 # call 'error' with given handler - pushstatus - return 2 # return error message and status - ]] - - local msg, st = f({}) -- invalid handler - assert(st == "ERRERR" and string.find(msg, "error handling")) - local msg, st = f(nil) -- invalid handler - assert(st == "ERRERR" and string.find(msg, "error handling")) - - local a = setmetatable({}, {__call = function (_, x) return x:upper() end}) - local msg, st = f(a) -- callable handler - assert(st == "ERRRUN" and msg == "BOLA") -end - -t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \ - insert 2; pushvalue 1; remove 1; insert 1; \ - insert -2; pushvalue -2; remove -3; return *", - 2, 3, 4, 5, 10, 40, 90)) -tcheck(t, {n=7,2,3,4,5,10,40,90}) - -t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12)) -tcheck(t, {n=1,"alo23joao12"}) - --- testing MULTRET -t = pack(T.testC("call 2,-1; return *", - function (a,b) return 1,2,3,4,a,b end, "alo", "joao")) -tcheck(t, {n=6,1,2,3,4,"alo", "joao"}) - -do -- test returning more results than fit in the caller stack - local a = {} - for i=1,1000 do a[i] = true end; a[999] = 10 - local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]], - table.unpack, a) - assert(b == "10") -end - - --- testing globals -_G.a = 14; _G.b = "a31" -local a = {T.testC[[ - getglobal a; - getglobal b; - getglobal b; - setglobal a; - return * -]]} -assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") - - --- testing arith -assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) -assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10) -assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200) -assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000) -assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5) -assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10) -assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200) -assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000) -assert(T.testC("arith /; return 1", 2, 0) == 10.0/0) -a = T.testC("pushnum 10; pushint 3; arith \\; return 1") -assert(a == 3.0 and math.type(a) == "float") -a = T.testC("pushint 10; pushint 3; arith \\; return 1") -assert(a == 3 and math.type(a) == "integer") -a = assert(T.testC("pushint 10; pushint 3; arith +; return 1")) -assert(a == 13 and math.type(a) == "integer") -a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1")) -assert(a == 13 and math.type(a) == "float") -a,b,c = T.testC([[pushnum 1; - pushstring 10; arith _; - pushstring 5; return 3]]) -assert(a == 1 and b == -10 and c == "5") -mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, - __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, - __unm = function (a) return setmetatable({a[1]* 2}, mt) end} -a,b,c = setmetatable({4}, mt), - setmetatable({8}, mt), - setmetatable({-3}, mt) -x,y,z = T.testC("arith +; return 2", 10, a, b) -assert(x == 10 and y[1] == 12 and z == nil) -assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) -assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == - 8 % (4 + (-3)*2)) - --- errors in arithmetic -checkerr("divide by zero", T.testC, "arith \\", 10, 0) -checkerr("%%0", T.testC, "arith %", 10, 0) - - --- testing lessthan and lessequal -assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2)) -assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2)) -assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2)) -assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2)) -assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2)) -assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2")) -assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2")) - --- non-valid indices produce false -assert(not T.testC("compare LT 1 4, return 1")) -assert(not T.testC("compare LE 9 1, return 1")) -assert(not T.testC("compare EQ 9 9, return 1")) - -local b = {__lt = function (a,b) return a[1] < b[1] end} -local a1,a3,a4 = setmetatable({1}, b), - setmetatable({3}, b), - setmetatable({4}, b) -assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2)) -assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2)) -assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2)) -a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20) -assert(a == 20 and b == false) -a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20) -assert(a == 20 and b == false) -a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) -assert(a == 20 and b == true) - --- testing length -local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) -a,b,c = T.testC([[ - len 2; - Llen 2; - objsize 2; - return 3 -]], t) -assert(a == 20 and b == 20 and c == 0) - -t.x = "234"; t[1] = 20 -a,b,c = T.testC([[ - len 2; - Llen 2; - objsize 2; - return 3 -]], t) -assert(a == "234" and b == 234 and c == 1) - -t.x = print; t[1] = 20 -a,c = T.testC([[ - len 2; - objsize 2; - return 2 -]], t) -assert(a == print and c == 1) - - --- testing __concat - -a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end}) -x,y = T.testC([[ - pushnum 5 - pushvalue 2; - pushvalue 2; - concat 2; - pushvalue -2; - return 2; -]], a, a) -assert(x == a..a and y == 5) - --- concat with 0 elements -assert(T.testC("concat 0; return 1") == "") - --- concat with 1 element -assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") - - - --- testing lua_is - -function B(x) return x and 1 or 0 end - -function count (x, n) - n = n or 2 - local prog = [[ - isnumber %d; - isstring %d; - isfunction %d; - iscfunction %d; - istable %d; - isuserdata %d; - isnil %d; - isnull %d; - return 8 - ]] - prog = string.format(prog, n, n, n, n, n, n, n, n) - local a,b,c,d,e,f,g,h = T.testC(prog, x) - return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h)) -end - -assert(count(3) == 2) -assert(count('alo') == 1) -assert(count('32') == 2) -assert(count({}) == 1) -assert(count(print) == 2) -assert(count(function () end) == 1) -assert(count(nil) == 1) -assert(count(io.stdin) == 1) -assert(count(nil, 15) == 100) - - --- testing lua_to... - -function to (s, x, n) - n = n or 2 - return T.testC(string.format("%s %d; return 1", s, n), x) -end - -local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues) -assert(debug.getupvalue(hfunc, 1)) -assert(to("tostring", {}) == nil) -assert(to("tostring", "alo") == "alo") -assert(to("tostring", 12) == "12") -assert(to("tostring", 12, 3) == nil) -assert(to("objsize", {}) == 0) -assert(to("objsize", {1,2,3}) == 3) -assert(to("objsize", "alo\0\0a") == 6) -assert(to("objsize", T.newuserdata(0)) == 0) -assert(to("objsize", T.newuserdata(101)) == 101) -assert(to("objsize", 124) == 0) -assert(to("objsize", true) == 0) -assert(to("tonumber", {}) == 0) -assert(to("tonumber", "12") == 12) -assert(to("tonumber", "s2") == 0) -assert(to("tonumber", 1, 20) == 0) -assert(to("topointer", 10) == 0) -assert(to("topointer", true) == 0) -assert(to("topointer", T.pushuserdata(20)) == 20) -assert(to("topointer", io.read) ~= 0) -- light C function -assert(to("topointer", hfunc) ~= 0) -- "heavy" C function -assert(to("topointer", function () end) ~= 0) -- Lua function -assert(to("topointer", io.stdin) ~= 0) -- full userdata -assert(to("func2num", 20) == 0) -assert(to("func2num", T.pushuserdata(10)) == 0) -assert(to("func2num", io.read) ~= 0) -- light C function -assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue) -a = to("tocfunction", math.deg) -assert(a(3) == math.deg(3) and a == math.deg) - - -print("testing panic function") -do - -- trivial error - assert(T.checkpanic("pushstring hi; error") == "hi") - - -- using the stack inside panic - assert(T.checkpanic("pushstring hi; error;", - [[checkstack 5 XX - pushstring ' alo' - pushstring ' mundo' - concat 3]]) == "hi alo mundo") - - -- "argerror" without frames - assert(T.checkpanic("loadstring 4") == - "bad argument #4 (string expected, got no value)") - - - -- memory error - T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) - assert(T.checkpanic("newuserdata 20000") == "not enough memory") - T.totalmem(0) -- restore high limit - - -- stack error - if not _soft then - local msg = T.checkpanic[[ - pushstring "function f() f() end" - loadstring -1; call 0 0 - getglobal f; call 0 0 - ]] - assert(string.find(msg, "stack overflow")) - end - -end - --- testing deep C stack -if not _soft then - print("testing stack overflow") - collectgarbage("stop") - checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep - -- too deep (with no message) - checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''") - local s = string.rep("pushnil;checkstack 1 XX;", 1000000) - checkerr("overflow", T.testC, s) - collectgarbage("restart") - print'+' -end - -local lim = _soft and 500 or 12000 -local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"} -for i = 1,lim do - prog[#prog + 1] = "pushnum " .. i - prog[#prog + 1] = "pushnum " .. i * 10 -end - -prog[#prog + 1] = "rawgeti R 2" -- get global table in registry -prog[#prog + 1] = "insert " .. -(2*lim + 2) - -for i = 1,lim do - prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1) -end - -prog[#prog + 1] = "return 2" - -prog = table.concat(prog, ";") -local g, t = T.testC(prog) -assert(g == _G) -for i = 1,lim do assert(t[i] == i*10); t[i] = nil end -assert(next(t) == nil) -prog, g, t = nil - --- testing errors - -a = T.testC([[ - loadstring 2; pcall 0 1 0; - pushvalue 3; insert -2; pcall 1 1 0; - pcall 0 0 0; - return 1 -]], "x=150", function (a) assert(a==nil); return 3 end) - -assert(type(a) == 'string' and x == 150) - -function check3(p, ...) - local arg = {...} - assert(#arg == 3) - assert(string.find(arg[3], p)) -end -check3(":1:", T.testC("loadstring 2; return *", "x=")) -check3("%.", T.testC("loadfile 2; return *", ".")) -check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) - --- test errors in non protected threads -function checkerrnopro (code, msg) - local th = coroutine.create(function () end) -- create new thread - local stt, err = pcall(T.testC, th, code) -- run code there - assert(not stt and string.find(err, msg)) -end - -if not _soft then - checkerrnopro("pushnum 3; call 0 0", "attempt to call") - print"testing stack overflow in unprotected thread" - function f () f() end - checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") -end -print"+" - - --- testing table access - -do -- getp/setp - local a = {} - T.testC("rawsetp 2 1", a, 20) - assert(a[T.pushuserdata(1)] == 20) - assert(T.testC("rawgetp 2 1; return 1", a) == 20) -end - -a = {x=0, y=12} -x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2", - a, 3, "y", 4, "x") -assert(x == 0 and y == 12) -T.testC("settable -5", a, 3, 4, "x", 15) -assert(a.x == 15) -a[a] = print -x = T.testC("gettable 2; return 1", a) -- table and key are the same object! -assert(x == print) -T.testC("settable 2", a, "x") -- table and key are the same object! -assert(a[a] == "x") - -b = setmetatable({p = a}, {}) -getmetatable(b).__index = function (t, i) return t.p[i] end -k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") -assert(x == 15 and k == 35) -k = T.testC("getfield 2 y, return 1", b) -assert(k == 12) -getmetatable(b).__index = function (t, i) return a[i] end -getmetatable(b).__newindex = function (t, i,v ) a[i] = v end -y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b) -assert(y == 12) -k = T.testC("settable -5, return 1", b, 3, 4, "x", 16) -assert(a.x == 16 and k == 4) -a[b] = 'xuxu' -y = T.testC("gettable 2, return 1", b) -assert(y == 'xuxu') -T.testC("settable 2", b, 19) -assert(a[b] == 19) - --- -do -- testing getfield/setfield with long keys - local t = {_012345678901234567890123456789012345678901234567890123456789 = 32} - local a = T.testC([[ - getfield 2 _012345678901234567890123456789012345678901234567890123456789 - return 1 - ]], t) - assert(a == 32) - local a = T.testC([[ - pushnum 33 - setglobal _012345678901234567890123456789012345678901234567890123456789 - ]]) - assert(_012345678901234567890123456789012345678901234567890123456789 == 33) - _012345678901234567890123456789012345678901234567890123456789 = nil -end - --- testing next -a = {} -t = pack(T.testC("next; return *", a, nil)) -tcheck(t, {n=1,a}) -a = {a=3} -t = pack(T.testC("next; return *", a, nil)) -tcheck(t, {n=3,a,'a',3}) -t = pack(T.testC("next; pop 1; next; return *", a, nil)) -tcheck(t, {n=1,a}) - - - --- testing upvalues - -do - local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] - t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]]) - assert(b == 10 and c == 20 and type(t) == 'table') - a, b = A([[tostring U3; tonumber U4; return 2]]) - assert(a == nil and b == 0) - A([[pushnum 100; pushnum 200; replace U2; replace U1]]) - b, c = A([[pushvalue U1; pushvalue U2; return 2]]) - assert(b == 100 and c == 200) - A([[replace U2; replace U1]], {x=1}, {x=2}) - b, c = A([[pushvalue U1; pushvalue U2; return 2]]) - assert(b.x == 1 and c.x == 2) - T.checkmemory() -end - - --- testing absent upvalues from C-function pointers -assert(T.testC[[isnull U1; return 1]] == true) -assert(T.testC[[isnull U100; return 1]] == true) -assert(T.testC[[pushvalue U1; return 1]] == nil) - -local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] -assert(T.upvalue(f, 1) == 10 and - T.upvalue(f, 2) == 20 and - T.upvalue(f, 3) == nil) -T.upvalue(f, 2, "xuxu") -assert(T.upvalue(f, 2) == "xuxu") - - --- large closures -do - local A = "checkstack 300 msg;" .. - string.rep("pushnum 10;", 255) .. - "pushcclosure 255; return 1" - A = T.testC(A) - for i=1,255 do - assert(A(("pushvalue U%d; return 1"):format(i)) == 10) - end - assert(A("isnull U256; return 1")) - assert(not A("isnil U256; return 1")) -end - - - --- testing get/setuservalue --- bug in 5.1.2 -checkerr("got number", debug.setuservalue, 3, {}) -checkerr("got nil", debug.setuservalue, nil, {}) -checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {}) - -local b = T.newuserdata(0) -assert(debug.getuservalue(b) == nil) -for _, v in pairs{true, false, 4.56, print, {}, b, "XYZ"} do - assert(debug.setuservalue(b, v) == b) - assert(debug.getuservalue(b) == v) -end - -assert(debug.getuservalue(4) == nil) - -debug.setuservalue(b, function () return 10 end) -collectgarbage() -- function should not be collected -assert(debug.getuservalue(b)() == 10) - -debug.setuservalue(b, 134) -collectgarbage() -- number should not be a problem for collector -assert(debug.getuservalue(b) == 134) - --- test barrier for uservalues -T.gcstate("atomic") -assert(T.gccolor(b) == "black") -debug.setuservalue(b, {x = 100}) -T.gcstate("pause") -- complete collection -assert(debug.getuservalue(b).x == 100) -- uvalue should be there - --- long chain of userdata -for i = 1, 1000 do - local bb = T.newuserdata(0) - debug.setuservalue(bb, b) - b = bb -end -collectgarbage() -- nothing should not be collected -for i = 1, 1000 do - b = debug.getuservalue(b) -end -assert(debug.getuservalue(b).x == 100) -b = nil - - --- testing locks (refs) - --- reuse of references -local i = T.ref{} -T.unref(i) -assert(T.ref{} == i) - -Arr = {} -Lim = 100 -for i=1,Lim do -- lock many objects - Arr[i] = T.ref({}) -end - -assert(T.ref(nil) == -1 and T.getref(-1) == nil) -T.unref(-1); T.unref(-1) - -for i=1,Lim do -- unlock all them - T.unref(Arr[i]) -end - -function printlocks () - local f = T.makeCfunc("gettable R; return 1") - local n = f("n") - print("n", n) - for i=0,n do - print(i, f(i)) - end -end - - -for i=1,Lim do -- lock many objects - Arr[i] = T.ref({}) -end - -for i=1,Lim,2 do -- unlock half of them - T.unref(Arr[i]) -end - -assert(type(T.getref(Arr[2])) == 'table') - - -assert(T.getref(-1) == nil) - - -a = T.ref({}) - -collectgarbage() - -assert(type(T.getref(a)) == 'table') - - --- colect in cl the `val' of all collected userdata -tt = {} -cl = {n=0} -A = nil; B = nil -local F -F = function (x) - local udval = T.udataval(x) - table.insert(cl, udval) - local d = T.newuserdata(100) -- cria lixo - d = nil - assert(debug.getmetatable(x).__gc == F) - assert(load("table.insert({}, {})"))() -- cria mais lixo - collectgarbage() -- forca coleta de lixo durante coleta! - assert(debug.getmetatable(x).__gc == F) -- coleta anterior nao melou isso? - local dummy = {} -- cria lixo durante coleta - if A ~= nil then - assert(type(A) == "userdata") - assert(T.udataval(A) == B) - debug.getmetatable(A) -- just acess it - end - A = x -- ressucita userdata - B = udval - return 1,2,3 -end -tt.__gc = F - --- test whether udate collection frees memory in the right time -do - collectgarbage(); - collectgarbage(); - local x = collectgarbage("count"); - local a = T.newuserdata(5001) - assert(T.testC("objsize 2; return 1", a) == 5001) - assert(collectgarbage("count") >= x+4) - a = nil - collectgarbage(); - assert(collectgarbage("count") <= x+1) - -- udata without finalizer - x = collectgarbage("count") - collectgarbage("stop") - for i=1,1000 do T.newuserdata(0) end - assert(collectgarbage("count") > x+10) - collectgarbage() - assert(collectgarbage("count") <= x+1) - -- udata with finalizer - collectgarbage() - x = collectgarbage("count") - collectgarbage("stop") - a = {__gc = function () end} - for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end - assert(collectgarbage("count") >= x+10) - collectgarbage() -- this collection only calls TM, without freeing memory - assert(collectgarbage("count") >= x+10) - collectgarbage() -- now frees memory - assert(collectgarbage("count") <= x+1) - collectgarbage("restart") -end - - -collectgarbage("stop") - --- create 3 userdatas with tag `tt' -a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) -b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) -c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) - --- create userdata without meta table -x = T.newuserdata(4) -y = T.newuserdata(0) - -checkerr("FILE%* expected, got userdata", io.input, a) -checkerr("FILE%* expected, got userdata", io.input, x) - -assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) - -d=T.ref(a); -e=T.ref(b); -f=T.ref(c); -t = {T.getref(d), T.getref(e), T.getref(f)} -assert(t[1] == a and t[2] == b and t[3] == c) - -t=nil; a=nil; c=nil; -T.unref(e); T.unref(f) - -collectgarbage() - --- check that unref objects have been collected -assert(#cl == 1 and cl[1] == nc) - -x = T.getref(d) -assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) -x =nil -tt.b = b -- create cycle -tt=nil -- frees tt for GC -A = nil -b = nil -T.unref(d); -n5 = T.newuserdata(0) -debug.setmetatable(n5, {__gc=F}) -n5 = T.udataval(n5) -collectgarbage() -assert(#cl == 4) --- check order of collection -assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) - -collectgarbage"restart" - - -a, na = {}, {} -for i=30,1,-1 do - a[i] = T.newuserdata(0) - debug.setmetatable(a[i], {__gc=F}) - na[i] = T.udataval(a[i]) -end -cl = {} -a = nil; collectgarbage() -assert(#cl == 30) -for i=1,30 do assert(cl[i] == na[i]) end -na = nil - - -for i=2,Lim,2 do -- unlock the other half - T.unref(Arr[i]) -end - -x = T.newuserdata(41); debug.setmetatable(x, {__gc=F}) -assert(T.testC("objsize 2; return 1", x) == 41) -cl = {} -a = {[x] = 1} -x = T.udataval(x) -collectgarbage() --- old `x' cannot be collected (`a' still uses it) -assert(#cl == 0) -for n in pairs(a) do a[n] = nil end -collectgarbage() -assert(#cl == 1 and cl[1] == x) -- old `x' must be collected - --- testing lua_equal -assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20)) -assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo")) -assert(T.testC("compare EQ 2 3; return 1", nil, nil)) -assert(not T.testC("compare EQ 2 3; return 1", {}, {})) -assert(not T.testC("compare EQ 2 3; return 1")) -assert(not T.testC("compare EQ 2 3; return 1", 3)) - --- testing lua_equal with fallbacks -do - local map = {} - local t = {__eq = function (a,b) return map[a] == map[b] end} - local function f(x) - local u = T.newuserdata(0) - debug.setmetatable(u, t) - map[u] = x - return u - end - assert(f(10) == f(10)) - assert(f(10) ~= f(11)) - assert(T.testC("compare EQ 2 3; return 1", f(10), f(10))) - assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20))) - t.__eq = nil - assert(f(10) ~= f(10)) -end - -print'+' - - - --- testing changing hooks during hooks -_G.t = {} -T.sethook([[ - # set a line hook after 3 count hooks - sethook 4 0 ' - getglobal t; - pushvalue -3; append -2 - pushvalue -2; append -2 - ']], "c", 3) -local a = 1 -- counting -a = 1 -- counting -a = 1 -- count hook (set line hook) -a = 1 -- line hook -a = 1 -- line hook -debug.sethook() -t = _G.t -assert(t[1] == "line") -line = t[2] -assert(t[3] == "line" and t[4] == line + 1) -assert(t[5] == "line" and t[6] == line + 2) -assert(t[7] == nil) - - -------------------------------------------------------------------------- -do -- testing errors during GC - local a = {} - for i=1,20 do - a[i] = T.newuserdata(i) -- creates several udata - end - for i=1,20,2 do -- mark half of them to raise errors during GC - debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end}) - end - for i=2,20,2 do -- mark the other half to count and to create more garbage - debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end}) - end - _G.A = 0 - a = 0 - while 1 do - local stat, msg = pcall(collectgarbage) - if stat then - break -- stop when no more errors - else - a = a + 1 - assert(string.find(msg, "__gc")) - end - end - assert(a == 10) -- number of errors - - assert(A == 10) -- number of normal collections -end -------------------------------------------------------------------------- --- test for userdata vals -do - local a = {}; local lim = 30 - for i=0,lim do a[i] = T.pushuserdata(i) end - for i=0,lim do assert(T.udataval(a[i]) == i) end - for i=0,lim do assert(T.pushuserdata(i) == a[i]) end - for i=0,lim do a[a[i]] = i end - for i=0,lim do a[T.pushuserdata(i)] = i end - assert(type(tostring(a[1])) == "string") -end - - -------------------------------------------------------------------------- --- testing multiple states -T.closestate(T.newstate()); -L1 = T.newstate() -assert(L1) - -assert(T.doremote(L1, "X='a'; return 'a'") == 'a') - - -assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0) - -a, b = T.doremote(L1, "return f()") -assert(a == 'alo' and b == '3') - -T.doremote(L1, "_ERRORMESSAGE = nil") --- error: `sin' is not defined -a, _, b = T.doremote(L1, "return sin(1)") -assert(a == nil and b == 2) -- 2 == run-time error - --- error: syntax error -a, b, c = T.doremote(L1, "return a+") -assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error - -T.loadlib(L1) -a, b, c = T.doremote(L1, [[ - string = require'string' - a = require'_G'; assert(a == _G and require("_G") == a) - io = require'io'; assert(type(io.read) == "function") - assert(require("io") == io) - a = require'table'; assert(type(a.insert) == "function") - a = require'debug'; assert(type(a.getlocal) == "function") - a = require'math'; assert(type(a.sin) == "function") - return string.sub('okinama', 1, 2) -]]) -assert(a == "ok") - -T.closestate(L1); - - -L1 = T.newstate() -T.loadlib(L1) -T.doremote(L1, "a = {}") -T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; - settable -3]]) -assert(T.doremote(L1, "return a.x") == "1") - -T.closestate(L1) - -L1 = nil - -print('+') - -------------------------------------------------------------------------- --- testing memory limits -------------------------------------------------------------------------- -checkerr("block too big", T.newuserdata, math.maxinteger) -collectgarbage() -T.totalmem(T.totalmem()+5000) -- set low memory limit (+5k) -checkerr("not enough memory", load"local a={}; for i=1,100000 do a[i]=i end") -T.totalmem(0) -- restore high limit - --- test memory errors; increase memory limit in small steps, so that --- we get memory errors in different parts of a given task, up to there --- is enough memory to complete the task without errors -function testamem (s, f) - collectgarbage(); collectgarbage() - local M = T.totalmem() - local oldM = M - local a,b = nil - while 1 do - M = M+7 -- increase memory limit in small steps - T.totalmem(M) - a, b = pcall(f) - T.totalmem(0) -- restore high limit - if a and b then break end -- stop when no more errors - collectgarbage() - if not a and not -- `real' error? - (string.find(b, "memory") or string.find(b, "overflow")) then - error(b, 0) -- propagate it - end - end - print("\nlimit for " .. s .. ": " .. M-oldM) - return b -end - - --- testing memory errors when creating a new state - -b = testamem("state creation", T.newstate) -T.closestate(b); -- close new state - - --- testing threads - --- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) -mt = T.testC("rawgeti R 1; return 1") -assert(type(mt) == "thread" and coroutine.running() == mt) - - - -function expand (n,s) - if n==0 then return "" end - local e = string.rep("=", n) - return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", - e, s, expand(n-1,s), e) -end - -G=0; collectgarbage(); a =collectgarbage("count") -load(expand(20,"G=G+1"))() -assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) - -testamem("thread creation", function () - return T.doonnewstack("x=1") == 0 -- try to create thread -end) - - --- testing memory x compiler - -testamem("loadstring", function () - return load("x=1") -- try to do load a string -end) - - -local testprog = [[ -local function foo () return end -local t = {"x"} -a = "aaa" -for i = 1, #t do a=a..t[i] end -return true -]] - --- testing memory x dofile -_G.a = nil -local t =os.tmpname() -local f = assert(io.open(t, "w")) -f:write(testprog) -f:close() -testamem("dofile", function () - local a = loadfile(t) - return a and a() -end) -assert(os.remove(t)) -assert(_G.a == "aaax") - - --- other generic tests - -testamem("string creation", function () - local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) - return (a == 'ablo ablo') -end) - -testamem("dump/undump", function () - local a = load(testprog) - local b = a and string.dump(a) - a = b and load(b) - return a and a() -end) - -local t = os.tmpname() -testamem("file creation", function () - local f = assert(io.open(t, 'w')) - assert (not io.open"nomenaoexistente") - io.close(f); - return not loadfile'nomenaoexistente' -end) -assert(os.remove(t)) - -testamem("table creation", function () - local a, lim = {}, 10 - for i=1,lim do a[i] = i; a[i..'a'] = {} end - return (type(a[lim..'a']) == 'table' and a[lim] == lim) -end) - -testamem("constructors", function () - local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} - return (type(a) == 'table' and a.e == 5) -end) - -local a = 1 -close = nil -testamem("closure creation", function () - function close (b,c) - return function (x) return a+b+c+x end - end - return (close(2,3)(4) == 10) -end) - -testamem("coroutines", function () - local a = coroutine.wrap(function () - coroutine.yield(string.rep("a", 10)) - return {} - end) - assert(string.len(a()) == 10) - return a() -end) - -do -- auxiliary buffer - local lim = 100 - local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end - testamem("auxiliary buffer", function () - return (#table.concat(a, ",") == 20*lim + lim - 1) - end) -end - -print'+' - --- testing some auxlib functions -local function gsub (a, b, c) - a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c) - assert(b == 5) - return a -end - -assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//") -assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.") -assert(gsub("", "alo", "//") == "") -assert(gsub("...", ".", "/.") == "/././.") -assert(gsub("...", "...", "") == "") - - --- testing luaL_newmetatable -local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3") -assert(type(mt_xuxu) == "table" and res and top == 3) -local d, res, top = T.testC("newmetatable xuxu; gettop; return 3") -assert(mt_xuxu == d and not res and top == 3) -d, res, top = T.testC("newmetatable xuxu1; gettop; return 3") -assert(mt_xuxu ~= d and res and top == 3) - -x = T.newuserdata(0); -y = T.newuserdata(0); -T.testC("pushstring xuxu; gettable R; setmetatable 2", x) -assert(getmetatable(x) == mt_xuxu) - --- testing luaL_testudata --- correct metatable -local res1, res2, top = T.testC([[testudata -1 xuxu - testudata 2 xuxu - gettop - return 3]], x) -assert(res1 and res2 and top == 4) - --- wrong metatable -res1, res2, top = T.testC([[testudata -1 xuxu1 - testudata 2 xuxu1 - gettop - return 3]], x) -assert(not res1 and not res2 and top == 4) - --- non-existent type -res1, res2, top = T.testC([[testudata -1 xuxu2 - testudata 2 xuxu2 - gettop - return 3]], x) -assert(not res1 and not res2 and top == 4) - --- userdata has no metatable -res1, res2, top = T.testC([[testudata -1 xuxu - testudata 2 xuxu - gettop - return 3]], y) -assert(not res1 and not res2 and top == 4) - --- erase metatables -do - local r = debug.getregistry() - assert(r.xuxu == mt_xuxu and r.xuxu1 == d) - r.xuxu = nil; r.xuxu1 = nil -end - -print'OK' - diff --git a/tofix/lua-5.3.4-tests/attrib.lua b/tofix/lua-5.3.4-tests/attrib.lua deleted file mode 100644 index 041f1aa..0000000 --- a/tofix/lua-5.3.4-tests/attrib.lua +++ /dev/null @@ -1,471 +0,0 @@ ----@diagnostic disable: unbalanced-assignments --- $Id: attrib.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print "testing require" - -assert(require"string" == string) -assert(require"math" == math) -assert(require"table" == table) -assert(require"io" == io) -assert(require"os" == os) -assert(require"coroutine" == coroutine) - -assert(type(package.path) == "string") -assert(type(package.cpath) == "string") -assert(type(package.loaded) == "table") -assert(type(package.preload) == "table") - -assert(type(package.config) == "string") -print("package config: "..string.gsub(package.config, "\n", "|")) - -do - -- create a path with 'max' templates, - -- each with 1-10 repetitions of '?' - local max = _soft and 100 or 2000 - local t = {} - for i = 1,max do t[i] = string.rep("?", i%10 + 1) end - t[#t + 1] = ";" -- empty template - local path = table.concat(t, ";") - -- use that path in a search - local s, err = package.searchpath("xuxu", path) - -- search fails; check that message has an occurence of - -- '??????????' with ? replaced by xuxu and at least 'max' lines - assert(not s and - string.find(err, string.rep("xuxu", 10)) and - #string.gsub(err, "[^\n]", "") >= max) - -- path with one very long template - local path = string.rep("?", max) - local s, err = package.searchpath("xuxu", path) - assert(not s and string.find(err, string.rep('xuxu', max))) -end - -do - local oldpath = package.path - package.path = {} - local s, err = pcall(require, "no-such-file") - assert(not s and string.find(err, "package.path")) - package.path = oldpath -end - -print('+') - - --- The next tests for 'require' assume some specific directories and --- libraries. - -if not _port then --[ - -local dirsep = string.match(package.config, "^([^\n]+)\n") - --- auxiliary directory with C modules and temporary files -local DIR = "libs" .. dirsep - --- prepend DIR to a name and correct directory separators -local function D (x) - x = string.gsub(x, "/", dirsep) - return DIR .. x -end - --- prepend DIR and pospend proper C lib. extension to a name -local function DC (x) - local ext = (dirsep == '\\') and ".dll" or ".so" - return D(x .. ext) -end - - -local function createfiles (files, preextras, posextras) - for n,c in pairs(files) do - io.output(D(n)) - io.write(string.format(preextras, n)) - io.write(c) - io.write(string.format(posextras, n)) - io.close(io.output()) - end -end - -function removefiles (files) - for n in pairs(files) do - os.remove(D(n)) - end -end - -local files = { - ["names.lua"] = "do return {...} end\n", - ["err.lua"] = "B = 15; a = a + 1;", - ["synerr.lua"] = "B =", - ["A.lua"] = "", - ["B.lua"] = "assert(...=='B');require 'A'", - ["A.lc"] = "", - ["A"] = "", - ["L"] = "", - ["XXxX"] = "", - ["C.lua"] = "package.loaded[...] = 25; require'C'", -} - -AA = nil -local extras = [[ -NAME = '%s' -REQUIRED = ... -return AA]] - -createfiles(files, "", extras) - --- testing explicit "dir" separator in 'searchpath' -assert(package.searchpath("C.lua", D"?", "", "") == D"C.lua") -assert(package.searchpath("C.lua", D"?", ".", ".") == D"C.lua") -assert(package.searchpath("--x-", D"?", "-", "X") == D"XXxX") -assert(package.searchpath("---xX", D"?", "---", "XX") == D"XXxX") -assert(package.searchpath(D"C.lua", "?", dirsep) == D"C.lua") -assert(package.searchpath(".\\C.lua", D"?", "\\") == D"./C.lua") - -local oldpath = package.path - -package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) - -local try = function (p, n, r) - NAME = nil - local rr = require(p) - assert(NAME == n) - assert(REQUIRED == p) - assert(rr == r) -end - -a = require"names" -assert(a[1] == "names" and a[2] == D"names.lua") - -_G.a = nil -local st, msg = pcall(require, "err") -assert(not st and string.find(msg, "arithmetic") and B == 15) -st, msg = pcall(require, "synerr") -assert(not st and string.find(msg, "error loading module")) - -assert(package.searchpath("C", package.path) == D"C.lua") -assert(require"C" == 25) -assert(require"C" == 25) -AA = nil -try('B', 'B.lua', true) -assert(package.loaded.B) -assert(require"B" == true) -assert(package.loaded.A) -assert(require"C" == 25) -package.loaded.A = nil -try('B', nil, true) -- should not reload package -try('A', 'A.lua', true) -package.loaded.A = nil -os.remove(D'A.lua') -AA = {} -try('A', 'A.lc', AA) -- now must find second option -assert(package.searchpath("A", package.path) == D"A.lc") -assert(require("A") == AA) -AA = false -try('K', 'L', false) -- default option -try('K', 'L', false) -- default option (should reload it) -assert(rawget(_G, "_REQUIREDNAME") == nil) - -AA = "x" -try("X", "XXxX", AA) - - -removefiles(files) - - --- testing require of sub-packages - -local _G = _G - -package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR) - -files = { - ["P1/init.lua"] = "AA = 10", - ["P1/xuxu.lua"] = "AA = 20", -} - -createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") -AA = 0 - -local m = assert(require"P1") -assert(AA == 0 and m.AA == 10) -assert(require"P1" == m) -assert(require"P1" == m) - -assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") -m.xuxu = assert(require"P1.xuxu") -assert(AA == 0 and m.xuxu.AA == 20) -assert(require"P1.xuxu" == m.xuxu) -assert(require"P1.xuxu" == m.xuxu) -assert(require"P1" == m and m.AA == 10) - - -removefiles(files) - - -package.path = "" -assert(not pcall(require, "file_does_not_exist")) -package.path = "??\0?" -assert(not pcall(require, "file_does_not_exist1")) - -package.path = oldpath - --- check 'require' error message -local fname = "file_does_not_exist2" -local m, err = pcall(require, fname) -for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do - t = string.gsub(t, "?", fname) - assert(string.find(err, t, 1, true)) -end - -do -- testing 'package.searchers' not being a table - local searchers = package.searchers - package.searchers = 3 - local st, msg = pcall(require, 'a') - assert(not st and string.find(msg, "must be a table")) - package.searchers = searchers -end - -local function import(...) - local f = {...} - return function (m) - for i=1, #f do m[f[i]] = _G[f[i]] end - end -end - --- cannot change environment of a C function -assert(not pcall(module, 'XUXU')) - - - --- testing require of C libraries - - -local p = "" -- On Mac OS X, redefine this to "_" - --- check whether loadlib works in this system -local st, err, when = package.loadlib(DC"lib1", "*") -if not st then - local f, err, when = package.loadlib("donotexist", p.."xuxu") - assert(not f and type(err) == "string" and when == "absent") - ;(Message or print)('\n >>> cannot load dynamic library <<<\n') - print(err, when) -else - -- tests for loadlib - local f = assert(package.loadlib(DC"lib1", p.."onefunction")) - local a, b = f(15, 25) - assert(a == 25 and b == 15) - - f = assert(package.loadlib(DC"lib1", p.."anotherfunc")) - assert(f(10, 20) == "10%20\n") - - -- check error messages - local f, err, when = package.loadlib(DC"lib1", p.."xuxu") - assert(not f and type(err) == "string" and when == "init") - f, err, when = package.loadlib("donotexist", p.."xuxu") - assert(not f and type(err) == "string" and when == "open") - - -- symbols from 'lib1' must be visible to other libraries - f = assert(package.loadlib(DC"lib11", p.."luaopen_lib11")) - assert(f() == "exported") - - -- test C modules with prefixes in names - package.cpath = DC"?" - local lib2 = require"lib2-v2" - -- check correct access to global environment and correct - -- parameters - assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") - assert(lib2.id("x") == "x") - - -- test C submodules - local fs = require"lib1.sub" - assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") - assert(fs.id(45) == 45) -end - -_ENV = _G - - --- testing preload - -do - local p = package - package = {} - p.preload.pl = function (...) - local _ENV = {...} - function xuxu (x) return x+20 end - return _ENV - end - - local pl = require"pl" - assert(require"pl" == pl) - assert(pl.xuxu(10) == 30) - assert(pl[1] == "pl" and pl[2] == nil) - - package = p - assert(type(package.path) == "string") -end - -print('+') - -end --] - -print("testing assignments, logical operators, and constructors") - -local res, res2 = 27 - -a, b = 1, 2+3 -assert(a==1 and b==5) -a={} -function f() return 10, 11, 12 end -a.x, b, a[1] = 1, 2, f() -assert(a.x==1 and b==2 and a[1]==10) -a[f()], b, a[f()+3] = f(), a, 'x' -assert(a[10] == 10 and b == a and a[13] == 'x') - -do - local f = function (n) local x = {}; for i=1,n do x[i]=i end; - return table.unpack(x) end; - local a,b,c - a,b = 0, f(1) - assert(a == 0 and b == 1) - A,b = 0, f(1) - assert(A == 0 and b == 1) - a,b,c = 0,5,f(4) - assert(a==0 and b==5 and c==1) - a,b,c = 0,5,f(0) - assert(a==0 and b==5 and c==nil) -end - -a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 -assert(not a and b and c and d==6) - -d = 20 -a, b, c, d = f() -assert(a==10 and b==11 and c==12 and d==nil) -a,b = f(), 1, 2, 3, f() -assert(a==10 and b==1) - -assert(ab == true) -assert((10 and 2) == 2) -assert((10 or 2) == 10) -assert((10 or assert(nil)) == 10) -assert(not (nil and assert(nil))) -assert((nil or "alo") == "alo") -assert((nil and 10) == nil) -assert((false and 10) == false) -assert((true or 10) == true) -assert((false or 10) == 10) -assert(false ~= nil) -assert(nil ~= false) -assert(not nil == true) -assert(not not nil == false) -assert(not not 1 == true) -assert(not not a == true) -assert(not not (6 or nil) == true) -assert(not not (nil and 56) == false) -assert(not not (nil and true) == false) -assert(not 10 == false) -assert(not {} == false) -assert(not 0.5 == false) -assert(not "x" == false) - -assert({} ~= {}) -print('+') - -a = {} -a[true] = 20 -a[false] = 10 -assert(a[1<2] == 20 and a[1>2] == 10) - -function f(a) return a end - -local a = {} -for i=3000,-3000,-1 do a[i + 0.0] = i; end -a[10e30] = "alo"; a[true] = 10; a[false] = 20 -assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10) -for i=3000,-3000,-1 do assert(a[i] == i); end -a[print] = assert -a[f] = print -a[a] = a -assert(a[a][a][a][a][print] == assert) -a[print](a[a[f]] == a[print]) -assert(not pcall(function () local a = {}; a[nil] = 10 end)) -assert(not pcall(function () local a = {[nil] = 10} end)) -assert(a[nil] == nil) -a = nil - -a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} -a, a.x, a.y = a, a[-3] -assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) -a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2 -a[1].alo(a[2]==10 and b==10 and c==print) - - --- test of large float/integer indices - --- compute maximum integer where all bits fit in a float -local maxint = math.maxinteger - -while maxint - 1.0 == maxint - 0.0 do -- trim (if needed) to fit in a float - maxint = maxint // 2 -end - -maxintF = maxint + 0.0 -- float version - -assert(math.type(maxintF) == "float" and maxintF >= 2.0^14) - --- floats and integers must index the same places -a[maxintF] = 10; a[maxintF - 1.0] = 11; -a[-maxintF] = 12; a[-maxintF + 1.0] = 13; - -assert(a[maxint] == 10 and a[maxint - 1] == 11 and - a[-maxint] == 12 and a[-maxint + 1] == 13) - -a[maxint] = 20 -a[-maxint] = 22 - -assert(a[maxintF] == 20 and a[maxintF - 1.0] == 11 and - a[-maxintF] == 22 and a[-maxintF + 1.0] == 13) - -a = nil - - --- test conflicts in multiple assignment -do - local a,i,j,b - a = {'a', 'b'}; i=1; j=2; b=a - i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i - assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and - b[3] == 1) -end - --- repeat test with upvalues -do - local a,i,j,b - a = {'a', 'b'}; i=1; j=2; b=a - local function foo () - i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i - end - foo() - assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and - b[3] == 1) - local t = {} - (function (a) t[a], a = 10, 20 end)(1); - assert(t[1] == 10) -end - --- bug in 5.2 beta -local function foo () - local a - return function () - local b - a, b = 3, 14 -- local and upvalue have same index - return a, b - end -end - -local a, b = foo()() -assert(a == 3 and b == 14) - -print('OK') - -return res - diff --git a/tofix/lua-5.3.4-tests/big.lua b/tofix/lua-5.3.4-tests/big.lua deleted file mode 100644 index 1a1fa78..0000000 --- a/tofix/lua-5.3.4-tests/big.lua +++ /dev/null @@ -1,82 +0,0 @@ --- $Id: big.lua,v 1.32 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -if _soft then - return 'a' -end - -print "testing large tables" - -local debug = require"debug" - -local lim = 2^18 + 1000 -local prog = { "local y = {0" } -for i = 1, lim do prog[#prog + 1] = i end -prog[#prog + 1] = "}\n" -prog[#prog + 1] = "X = y\n" -prog[#prog + 1] = ("assert(X[%d] == %d)"):format(lim - 1, lim - 2) -prog[#prog + 1] = "return 0" -prog = table.concat(prog, ";") - -local env = {string = string, assert = assert} -local f = assert(load(prog, nil, nil, env)) - -f() -assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim) -for k in pairs(env) do env[k] = nil end - --- yields during accesses larger than K (in RK) -setmetatable(env, { - __index = function (t, n) coroutine.yield('g'); return _G[n] end, - __newindex = function (t, n, v) coroutine.yield('s'); _G[n] = v end, -}) - -X = nil -co = coroutine.wrap(f) -assert(co() == 's') -assert(co() == 'g') -assert(co() == 'g') -assert(co() == 0) - -assert(X[lim] == lim - 1 and X[lim + 1] == lim) - --- errors in accesses larger than K (in RK) -getmetatable(env).__index = function () end -getmetatable(env).__newindex = function () end -local e, m = pcall(f) -assert(not e and m:find("global 'X'")) - --- errors in metamethods -getmetatable(env).__newindex = function () error("hi") end -local e, m = xpcall(f, debug.traceback) -assert(not e and m:find("'__newindex'")) - -f, X = nil - -coroutine.yield'b' - -if 2^32 == 0 then -- (small integers) { - -print "testing string length overflow" - -local repstrings = 192 -- number of strings to be concatenated -local ssize = math.ceil(2.0^32 / repstrings) + 1 -- size of each string - -assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size - -local longs = string.rep("\0", ssize) -- create one long string - --- create function to concatentate 'repstrings' copies of its argument -local rep = assert(load( - "local a = ...; return " .. string.rep("a", repstrings, ".."))) - -local a, b = pcall(rep, longs) -- call that function - --- it should fail without creating string (result would be too large) -assert(not a and string.find(b, "overflow")) - -end -- } - -print'OK' - -return 'a' diff --git a/tofix/lua-5.3.4-tests/bitwise.lua b/tofix/lua-5.3.4-tests/bitwise.lua deleted file mode 100644 index a1d6ff4..0000000 --- a/tofix/lua-5.3.4-tests/bitwise.lua +++ /dev/null @@ -1,328 +0,0 @@ --- $Id: bitwise.lua,v 1.26 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print("testing bitwise operations") - -local numbits = string.packsize('j') * 8 - -assert(~0 == -1) - -assert((1 << (numbits - 1)) == math.mininteger) - --- basic tests for bitwise operators; --- use variables to avoid constant folding -local a, b, c, d -a = 0xFFFFFFFFFFFFFFFF -assert(a == -1 and a & -1 == a and a & 35 == 35) -a = 0xF0F0F0F0F0F0F0F0 -assert(a | -1 == -1) -assert(a ~ a == 0 and a ~ 0 == a and a ~ ~a == -1) -assert(a >> 4 == ~a) -a = 0xF0; b = 0xCC; c = 0xAA; d = 0xFD -assert(a | b ~ c & d == 0xF4) - -a = 0xF0.0; b = 0xCC.0; c = "0xAA.0"; d = "0xFD.0" -assert(a | b ~ c & d == 0xF4) - -a = 0xF0000000; b = 0xCC000000; -c = 0xAA000000; d = 0xFD000000 -assert(a | b ~ c & d == 0xF4000000) -assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) - -a = a << 32 -b = b << 32 -c = c << 32 -d = d << 32 -assert(a | b ~ c & d == 0xF4000000 << 32) -assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) - -assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000) -assert(-1 >> (numbits - 1) == 1) -assert(-1 >> numbits == 0 and - -1 >> -numbits == 0 and - -1 << numbits == 0 and - -1 << -numbits == 0) - -assert((2^30 - 1) << 2^30 == 0) -assert((2^30 - 1) >> 2^30 == 0) - -assert(1 >> -3 == 1 << 3 and 1000 >> 5 == 1000 << -5) - - --- coercion from strings to integers -assert("0xffffffffffffffff" | 0 == -1) -assert("0xfffffffffffffffe" & "-1" == -2) -assert(" \t-0xfffffffffffffffe\n\t" & "-1" == 2) -assert(" \n -45 \t " >> " -2 " == -45 * 4) - --- out of range number -assert(not pcall(function () return "0xffffffffffffffff.0" | 0 end)) - --- embedded zeros -assert(not pcall(function () return "0xffffffffffffffff\0" | 0 end)) - -print'+' - - -package.preload.bit32 = function () --{ - --- no built-in 'bit32' library: implement it using bitwise operators - -local bit = {} - -function bit.bnot (a) - return ~a & 0xFFFFFFFF -end - - --- --- in all vararg functions, avoid creating 'arg' table when there are --- only 2 (or less) parameters, as 2 parameters is the common case --- - -function bit.band (x, y, z, ...) - if not z then - - else - local arg = {...} - local res = x & y & z - for i = 1, #arg do res = res & arg[i] end - return res & 0xFFFFFFFF - end -end - -function bit.bor (x, y, z, ...) - if not z then - return ((x or 0) | (y or 0)) & 0xFFFFFFFF - else - local arg = {...} - local res = x | y | z - for i = 1, #arg do res = res | arg[i] end - return res & 0xFFFFFFFF - end -end - -function bit.bxor (x, y, z, ...) - if not z then - return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF - else - local arg = {...} - local res = x ~ y ~ z - for i = 1, #arg do res = res ~ arg[i] end - return res & 0xFFFFFFFF - end -end - -function bit.btest (...) - return bit.band(...) ~= 0 -end - -function bit.lshift (a, b) - return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF -end - -function bit.rshift (a, b) - return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF -end - -function bit.arshift (a, b) - a = a & 0xFFFFFFFF - if b <= 0 or (a & 0x80000000) == 0 then - return (a >> b) & 0xFFFFFFFF - else - return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF - end -end - -function bit.lrotate (a ,b) - b = b & 31 - a = a & 0xFFFFFFFF - a = (a << b) | (a >> (32 - b)) - return a & 0xFFFFFFFF -end - -function bit.rrotate (a, b) - return bit.lrotate(a, -b) -end - -local function checkfield (f, w) - w = w or 1 - assert(f >= 0, "field cannot be negative") - assert(w > 0, "width must be positive") - assert(f + w <= 32, "trying to access non-existent bits") - return f, ~(-1 << w) -end - -function bit.extract (a, f, w) - local f, mask = checkfield(f, w) - return (a >> f) & mask -end - -function bit.replace (a, v, f, w) - local f, mask = checkfield(f, w) - v = v & mask - a = (a & ~(mask << f)) | (v << f) - return a & 0xFFFFFFFF -end - -return bit - -end --} - - -print("testing bitwise library") - -local bit32 = require'bit32' - -assert(bit32.band() == bit32.bnot(0)) -assert(bit32.btest() == true) -assert(bit32.bor() == 0) -assert(bit32.bxor() == 0) - -assert(bit32.band() == bit32.band(0xffffffff)) -assert(bit32.band(1,2) == 0) - - --- out-of-range numbers -assert(bit32.band(-1) == 0xffffffff) -assert(bit32.band((1 << 33) - 1) == 0xffffffff) -assert(bit32.band(-(1 << 33) - 1) == 0xffffffff) -assert(bit32.band((1 << 33) + 1) == 1) -assert(bit32.band(-(1 << 33) + 1) == 1) -assert(bit32.band(-(1 << 40)) == 0) -assert(bit32.band(1 << 40) == 0) -assert(bit32.band(-(1 << 40) - 2) == 0xfffffffe) -assert(bit32.band((1 << 40) - 4) == 0xfffffffc) - -assert(bit32.lrotate(0, -1) == 0) -assert(bit32.lrotate(0, 7) == 0) -assert(bit32.lrotate(0x12345678, 0) == 0x12345678) -assert(bit32.lrotate(0x12345678, 32) == 0x12345678) -assert(bit32.lrotate(0x12345678, 4) == 0x23456781) -assert(bit32.rrotate(0x12345678, -4) == 0x23456781) -assert(bit32.lrotate(0x12345678, -8) == 0x78123456) -assert(bit32.rrotate(0x12345678, 8) == 0x78123456) -assert(bit32.lrotate(0xaaaaaaaa, 2) == 0xaaaaaaaa) -assert(bit32.lrotate(0xaaaaaaaa, -2) == 0xaaaaaaaa) -for i = -50, 50 do - assert(bit32.lrotate(0x89abcdef, i) == bit32.lrotate(0x89abcdef, i%32)) -end - -assert(bit32.lshift(0x12345678, 4) == 0x23456780) -assert(bit32.lshift(0x12345678, 8) == 0x34567800) -assert(bit32.lshift(0x12345678, -4) == 0x01234567) -assert(bit32.lshift(0x12345678, -8) == 0x00123456) -assert(bit32.lshift(0x12345678, 32) == 0) -assert(bit32.lshift(0x12345678, -32) == 0) -assert(bit32.rshift(0x12345678, 4) == 0x01234567) -assert(bit32.rshift(0x12345678, 8) == 0x00123456) -assert(bit32.rshift(0x12345678, 32) == 0) -assert(bit32.rshift(0x12345678, -32) == 0) -assert(bit32.arshift(0x12345678, 0) == 0x12345678) -assert(bit32.arshift(0x12345678, 1) == 0x12345678 // 2) -assert(bit32.arshift(0x12345678, -1) == 0x12345678 * 2) -assert(bit32.arshift(-1, 1) == 0xffffffff) -assert(bit32.arshift(-1, 24) == 0xffffffff) -assert(bit32.arshift(-1, 32) == 0xffffffff) -assert(bit32.arshift(-1, -1) == bit32.band(-1 * 2, 0xffffffff)) - -assert(0x12345678 << 4 == 0x123456780) -assert(0x12345678 << 8 == 0x1234567800) -assert(0x12345678 << -4 == 0x01234567) -assert(0x12345678 << -8 == 0x00123456) -assert(0x12345678 << 32 == 0x1234567800000000) -assert(0x12345678 << -32 == 0) -assert(0x12345678 >> 4 == 0x01234567) -assert(0x12345678 >> 8 == 0x00123456) -assert(0x12345678 >> 32 == 0) -assert(0x12345678 >> -32 == 0x1234567800000000) - -print("+") --- some special cases -local c = {0, 1, 2, 3, 10, 0x80000000, 0xaaaaaaaa, 0x55555555, - 0xffffffff, 0x7fffffff} - -for _, b in pairs(c) do - assert(bit32.band(b) == b) - assert(bit32.band(b, b) == b) - assert(bit32.band(b, b, b, b) == b) - assert(bit32.btest(b, b) == (b ~= 0)) - assert(bit32.band(b, b, b) == b) - assert(bit32.band(b, b, b, ~b) == 0) - assert(bit32.btest(b, b, b) == (b ~= 0)) - assert(bit32.band(b, bit32.bnot(b)) == 0) - assert(bit32.bor(b, bit32.bnot(b)) == bit32.bnot(0)) - assert(bit32.bor(b) == b) - assert(bit32.bor(b, b) == b) - assert(bit32.bor(b, b, b) == b) - assert(bit32.bor(b, b, 0, ~b) == 0xffffffff) - assert(bit32.bxor(b) == b) - assert(bit32.bxor(b, b) == 0) - assert(bit32.bxor(b, b, b) == b) - assert(bit32.bxor(b, b, b, b) == 0) - assert(bit32.bxor(b, 0) == b) - assert(bit32.bnot(b) ~= b) - assert(bit32.bnot(bit32.bnot(b)) == b) - assert(bit32.bnot(b) == (1 << 32) - 1 - b) - assert(bit32.lrotate(b, 32) == b) - assert(bit32.rrotate(b, 32) == b) - assert(bit32.lshift(bit32.lshift(b, -4), 4) == bit32.band(b, bit32.bnot(0xf))) - assert(bit32.rshift(bit32.rshift(b, 4), -4) == bit32.band(b, bit32.bnot(0xf))) -end - --- for this test, use at most 24 bits (mantissa of a single float) -c = {0, 1, 2, 3, 10, 0x800000, 0xaaaaaa, 0x555555, 0xffffff, 0x7fffff} -for _, b in pairs(c) do - for i = -40, 40 do - local x = bit32.lshift(b, i) - local y = math.floor(math.fmod(b * 2.0^i, 2.0^32)) - assert(math.fmod(x - y, 2.0^32) == 0) - end -end - -assert(not pcall(bit32.band, {})) -assert(not pcall(bit32.bnot, "a")) -assert(not pcall(bit32.lshift, 45)) -assert(not pcall(bit32.lshift, 45, print)) -assert(not pcall(bit32.rshift, 45, print)) - -print("+") - - --- testing extract/replace - -assert(bit32.extract(0x12345678, 0, 4) == 8) -assert(bit32.extract(0x12345678, 4, 4) == 7) -assert(bit32.extract(0xa0001111, 28, 4) == 0xa) -assert(bit32.extract(0xa0001111, 31, 1) == 1) -assert(bit32.extract(0x50000111, 31, 1) == 0) -assert(bit32.extract(0xf2345679, 0, 32) == 0xf2345679) - -assert(not pcall(bit32.extract, 0, -1)) -assert(not pcall(bit32.extract, 0, 32)) -assert(not pcall(bit32.extract, 0, 0, 33)) -assert(not pcall(bit32.extract, 0, 31, 2)) - -assert(bit32.replace(0x12345678, 5, 28, 4) == 0x52345678) -assert(bit32.replace(0x12345678, 0x87654321, 0, 32) == 0x87654321) -assert(bit32.replace(0, 1, 2) == 2^2) -assert(bit32.replace(0, -1, 4) == 2^4) -assert(bit32.replace(-1, 0, 31) == (1 << 31) - 1) -assert(bit32.replace(-1, 0, 1, 2) == (1 << 32) - 7) - - --- testing conversion of floats - -assert(bit32.bor(3.0) == 3) -assert(bit32.bor(-4.0) == 0xfffffffc) - --- large floats and large-enough integers? -if 2.0^50 < 2.0^50 + 1.0 and 2.0^50 < (-1 >> 1) then - assert(bit32.bor(2.0^32 - 5.0) == 0xfffffffb) - assert(bit32.bor(-2.0^32 - 6.0) == 0xfffffffa) - assert(bit32.bor(2.0^48 - 5.0) == 0xfffffffb) - assert(bit32.bor(-2.0^48 - 6.0) == 0xfffffffa) -end - -print'OK' - diff --git a/tofix/lua-5.3.4-tests/calls.lua b/tofix/lua-5.3.4-tests/calls.lua deleted file mode 100644 index 6d6fb7b..0000000 --- a/tofix/lua-5.3.4-tests/calls.lua +++ /dev/null @@ -1,401 +0,0 @@ --- $Id: calls.lua,v 1.60 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print("testing functions and calls") - -local debug = require "debug" - --- get the opportunity to test 'type' too ;) - -assert(type(1<2) == 'boolean') -assert(type(true) == 'boolean' and type(false) == 'boolean') -assert(type(nil) == 'nil' - and type(-3) == 'number' - and type'x' == 'string' - and type{} == 'table' - and type(type) == 'function') - -assert(type(assert) == type(print)) -function f (x) return a:x (x) end -assert(type(f) == 'function') -assert(not pcall(type)) - - -do -- test error in 'print' too... - local tostring = _ENV.tostring - - _ENV.tostring = nil - local st, msg = pcall(print, 1) - assert(st == false and string.find(msg, "attempt to call a nil value")) - - _ENV.tostring = function () return {} end - local st, msg = pcall(print, 1) - assert(st == false and string.find(msg, "must return a string")) - - _ENV.tostring = tostring -end - - --- testing local-function recursion -fact = false -do - local res = 1 - local function fact (n) - if n==0 then return res - else return n*fact(n-1) - end - end - assert(fact(5) == 120) -end -assert(fact == false) - --- testing declarations -a = {i = 10} -self = 20 -function a:x (x) return x+self.i end -function a.y (x) return x+self end - -assert(a:x(1)+10 == a.y(1)) - -a.t = {i=-100} -a["t"].x = function (self, a,b) return self.i+a+b end - -assert(a.t:x(2,3) == -95) - -do - local a = {x=0} - function a:add (x) self.x, a.y = self.x+x, 20; return self end - assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) -end - -local a = {b={c={}}} - -function a.b.c.f1 (x) return x+1 end -function a.b.c:f2 (x,y) self[x] = y end -assert(a.b.c.f1(4) == 5) -a.b.c:f2('k', 12); assert(a.b.c.k == 12) - -print('+') - -t = nil -- 'declare' t -function f(a,b,c) local d = 'a'; t={a,b,c,d} end - -f( -- this line change must be valid - 1,2) -assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') -f(1,2, -- this one too - 3,4) -assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') - -function fat(x) - if x <= 1 then return 1 - else return x*load("return fat(" .. x-1 .. ")", "")() - end -end - -assert(load "load 'assert(fat(6)==720)' () ")() -a = load('return fat(5), 3') -a,b = a() -assert(a == 120 and b == 3) -print('+') - -function err_on_n (n) - if n==0 then error(); exit(1); - else err_on_n (n-1); exit(1); - end -end - -do - function dummy (n) - if n > 0 then - assert(not pcall(err_on_n, n)) - dummy(n-1) - end - end -end - -dummy(10) - -function deep (n) - if n>0 then deep(n-1) end -end -deep(10) -deep(200) - --- testing tail call -function deep (n) if n>0 then return deep(n-1) else return 101 end end -assert(deep(30000) == 101) -a = {} -function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end -assert(a:deep(30000) == 101) - -print('+') - - -a = nil -(function (x) a=x end)(23) -assert(a == 23 and (function (x) return x*2 end)(20) == 40) - - --- testing closures - --- fixed-point operator -Z = function (le) - local function a (f) - return le(function (x) return f(f)(x) end) - end - return a(a) - end - - --- non-recursive factorial - -F = function (f) - return function (n) - if n == 0 then return 1 - else return n*f(n-1) end - end - end - -fat = Z(F) - -assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) - -local function g (z) - local function f (a,b,c,d) - return function (x,y) return a+b+c+d+a+x+y+z end - end - return f(z,z+1,z+2,z+3) -end - -f = g(10) -assert(f(9, 16) == 10+11+12+13+10+9+16+10) - -Z, F, f = nil -print('+') - --- testing multiple returns - -function unlpack (t, i) - i = i or 1 - if (i <= #t) then - return t[i], unlpack(t, i+1) - end -end - -function equaltab (t1, t2) - assert(#t1 == #t2) - for i = 1, #t1 do - assert(t1[i] == t2[i]) - end -end - -local pack = function (...) return (table.pack(...)) end - -function f() return 1,2,30,4 end -function ret2 (a,b) return a,b end - -local a,b,c,d = unlpack{1,2,3} -assert(a==1 and b==2 and c==3 and d==nil) -a = {1,2,3,4,false,10,'alo',false,assert} -equaltab(pack(unlpack(a)), a) -equaltab(pack(unlpack(a), -1), {1,-1}) -a,b,c,d = ret2(f()), ret2(f()) -assert(a==1 and b==1 and c==2 and d==nil) -a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) -assert(a==1 and b==1 and c==2 and d==nil) -a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) -assert(a==1 and b==1 and c==nil and d==nil) - -a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} -assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") - - --- testing calls with 'incorrect' arguments -rawget({}, "x", 1) -rawset({}, "x", 1, 2) -assert(math.sin(1,2) == math.sin(1)) -table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a 10 or a[i]() ~= x -assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4) - - --- testing closures created in 'then' and 'else' parts of 'if's -a = {} -for i = 1, 10 do - if i % 3 == 0 then - local y = 0 - a[i] = function (x) local t = y; y = x; return t end - elseif i % 3 == 1 then - goto L1 - error'not here' - ::L1:: - local y = 1 - a[i] = function (x) local t = y; y = x; return t end - elseif i % 3 == 2 then - local t - goto l4 - ::l4a:: a[i] = t; goto l4b - error("should never be here!") - ::l4:: - local y = 2 - t = function (x) local t = y; y = x; return t end - goto l4a - error("should never be here!") - ::l4b:: - end -end - -for i = 1, 10 do - assert(a[i](i * 10) == i % 3 and a[i]() == i * 10) -end - -print'+' - - --- test for correctly closing upvalues in tail calls of vararg functions -local function t () - local function c(a,b) assert(a=="test" and b=="OK") end - local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end - local x = 1 - return v(function() return x end) -end -t() - - --- test for debug manipulation of upvalues -local debug = require'debug' - -do - local a , b, c = 3, 5, 7 - foo1 = function () return a+b end; - foo2 = function () return b+a end; - do - local a = 10 - foo3 = function () return a+b end; - end -end - -assert(debug.upvalueid(foo1, 1)) -assert(debug.upvalueid(foo1, 2)) -assert(not pcall(debug.upvalueid, foo1, 3)) -assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2)) -assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1)) -assert(debug.upvalueid(foo3, 1)) -assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1)) -assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2)) - -assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil) - -assert(foo1() == 3 + 5 and foo2() == 5 + 3) -debug.upvaluejoin(foo1, 2, foo2, 2) -assert(foo1() == 3 + 3 and foo2() == 5 + 3) -assert(foo3() == 10 + 5) -debug.upvaluejoin(foo3, 2, foo2, 1) -assert(foo3() == 10 + 5) -debug.upvaluejoin(foo3, 2, foo2, 2) -assert(foo3() == 10 + 3) - -assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1)) -assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3)) -assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1)) -assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1)) -assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1)) -assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1)) - -print'OK' diff --git a/tofix/lua-5.3.4-tests/code.lua b/tofix/lua-5.3.4-tests/code.lua deleted file mode 100644 index 0b9d142..0000000 --- a/tofix/lua-5.3.4-tests/code.lua +++ /dev/null @@ -1,239 +0,0 @@ --- $Id: code.lua,v 1.42 2016/11/07 13:04:32 roberto Exp $ --- See Copyright Notice in file all.lua - -if T==nil then - (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') - return -end -print "testing code generation and optimizations" - - --- this code gave an error for the code checker -do - local function f (a) - for k,v,w in a do end - end -end - - --- testing reuse in constant table -local function checkKlist (func, list) - local k = T.listk(func) - assert(#k == #list) - for i = 1, #k do - assert(k[i] == list[i] and math.type(k[i]) == math.type(list[i])) - end -end - -local function foo () - local a - a = 3; - a = 0; a = 0.0; a = -7 + 7 - a = 3.78/4; a = 3.78/4 - a = -3.78/4; a = 3.78/4; a = -3.78/4 - a = -3.79/4; a = 0.0; a = -0; - a = 3; a = 3.0; a = 3; a = 3.0 -end - -checkKlist(foo, {3, 0, 0.0, 3.78/4, -3.78/4, -3.79/4, 3.0}) - - --- testing opcodes - -function check (f, ...) - local arg = {...} - local c = T.listcode(f) - for i=1, #arg do - -- print(arg[i], c[i]) - assert(string.find(c[i], '- '..arg[i]..' *%d')) - end - assert(c[#arg+2] == nil) -end - - -function checkequal (a, b) - a = T.listcode(a) - b = T.listcode(b) - for i = 1, #a do - a[i] = string.gsub(a[i], '%b()', '') -- remove line number - b[i] = string.gsub(b[i], '%b()', '') -- remove line number - assert(a[i] == b[i]) - end -end - - --- some basic instructions -check(function () - (function () end){f()} -end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') - - --- sequence of LOADNILs -check(function () - local a,b,c - local d; local e; - local f,g,h; - d = nil; d=nil; b=nil; a=nil; c=nil; -end, 'LOADNIL', 'RETURN') - -check(function () - local a,b,c,d = 1,1,1,1 - d=nil;c=nil;b=nil;a=nil -end, 'LOADK', 'LOADK', 'LOADK', 'LOADK', 'LOADNIL', 'RETURN') - -do - local a,b,c,d = 1,1,1,1 - d=nil;c=nil;b=nil;a=nil - assert(a == nil and b == nil and c == nil and d == nil) -end - - --- single return -check (function (a,b,c) return a end, 'RETURN') - - --- infinite loops -check(function () while true do local a = -1 end end, -'LOADK', 'JMP', 'RETURN') - -check(function () while 1 do local a = -1 end end, -'LOADK', 'JMP', 'RETURN') - -check(function () repeat local x = 1 until true end, -'LOADK', 'RETURN') - - --- concat optimization -check(function (a,b,c,d) return a..b..c..d end, - 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN') - --- not -check(function () return not not nil end, 'LOADBOOL', 'RETURN') -check(function () return not not false end, 'LOADBOOL', 'RETURN') -check(function () return not not true end, 'LOADBOOL', 'RETURN') -check(function () return not not 1 end, 'LOADBOOL', 'RETURN') - --- direct access to locals -check(function () - local a,b,c,d - a = b*2 - c[2], a[b] = -((a + d/2 - a[b]) ^ a.x), b -end, - 'LOADNIL', - 'MUL', - 'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETTABLE', 'POW', - 'UNM', 'SETTABLE', 'SETTABLE', 'RETURN') - - --- direct access to constants -check(function () - local a,b - a.x = 3.2 - a.x = b - a[b] = 'x' -end, - 'LOADNIL', 'SETTABLE', 'SETTABLE', 'SETTABLE', 'RETURN') - -check(function () - local a,b - a = 1 - a - b = 1/a - b = 5-4 -end, - 'LOADNIL', 'SUB', 'DIV', 'LOADK', 'RETURN') - -check(function () - local a,b - a[true] = false -end, - 'LOADNIL', 'SETTABLE', 'RETURN') - - --- constant folding -local function checkK (func, val) - check(func, 'LOADK', 'RETURN') - local k = T.listk(func) - assert(#k == 1 and k[1] == val and math.type(k[1]) == math.type(val)) - assert(func() == val) -end -checkK(function () return 0.0 end, 0.0) -checkK(function () return 0 end, 0) -checkK(function () return -0//1 end, 0) -checkK(function () return 3^-1 end, 1/3) -checkK(function () return (1 + 1)^(50 + 50) end, 2^100) -checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) -checkK(function () return (-3^0 + 5) // 3.0 end, 1.0) -checkK(function () return -3 % 5 end, 2) -checkK(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) -checkK(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) -checkK(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) -checkK(function () return ~(~0xFF0 | 0xFF0) end, 0) -checkK(function () return ~~-100024.0 end, -100024) -checkK(function () return ((100 << 6) << -4) >> 2 end, 100) - - --- no foldings -check(function () return -0.0 end, 'LOADK', 'UNM', 'RETURN') -check(function () return 3/0 end, 'DIV', 'RETURN') -check(function () return 0%0 end, 'MOD', 'RETURN') -check(function () return -4//0 end, 'IDIV', 'RETURN') - --- bug in constant folding for 5.1 -check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN') - - -check(function () - local a,b,c - b[c], a = c, b - b[a], a = c, b - a, b = c, a - a = a -end, - 'LOADNIL', - 'MOVE', 'MOVE', 'SETTABLE', - 'MOVE', 'MOVE', 'MOVE', 'SETTABLE', - 'MOVE', 'MOVE', 'MOVE', - -- no code for a = a - 'RETURN') - - --- x == nil , x ~= nil -checkequal(function () if (a==nil) then a=1 end; if a~=nil then a=1 end end, - function () if (a==9) then a=1 end; if a~=9 then a=1 end end) - -check(function () if a==nil then a='a' end end, -'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN') - --- de morgan -checkequal(function () local a; if not (a or b) then b=a end end, - function () local a; if (not a and not b) then b=a end end) - -checkequal(function (l) local a; return 0 <= a and a <= l end, - function (l) local a; return not (not(a >= 0) or not(a <= l)) end) - - --- if-goto optimizations -check(function (a, b, c, d, e) - if a == b then goto l1 - elseif a == c then goto l2 - elseif a == d then goto l2 - else if a == e then goto l3 - else goto l3 - end - end - ::l1:: ::l2:: ::l3:: ::l4:: -end, 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'JMP', 'RETURN') - -checkequal( -function (a) while a < 10 do a = a + 1 end end, -function (a) ::L2:: if not(a < 10) then goto L1 end; a = a + 1; - goto L2; ::L1:: end -) - -checkequal( -function (a) while a < 10 do a = a + 1 end end, -function (a) while true do if not(a < 10) then break end; a = a + 1; end end -) - -print 'OK' - diff --git a/tofix/lua-5.3.4-tests/constructs.lua b/tofix/lua-5.3.4-tests/constructs.lua deleted file mode 100644 index cebd257..0000000 --- a/tofix/lua-5.3.4-tests/constructs.lua +++ /dev/null @@ -1,313 +0,0 @@ --- $Id: constructs.lua,v 1.41 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -;;print "testing syntax";; - -local debug = require "debug" - - -local function checkload (s, msg) - assert(string.find(select(2, load(s)), msg)) -end - --- testing semicollons -do ;;; end -; do ; a = 3; assert(a == 3) end; -; - - --- invalid operations should not raise errors when not executed -if false then a = 3 // 0; a = 0 % 0 end - - --- testing priorities - -assert(2^3^2 == 2^(3^2)); -assert(2^3*4 == (2^3)*4); -assert(2.0^-2 == 1/4 and -2^- -2 == - - -4); -assert(not nil and 2 and not(2>3 or 3<2)); -assert(-3-1-5 == 0+0-9); -assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0); -assert(-3%5 == 2 and -3+5 == 2) -assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33"); -assert(not(2+1 > 3*1) and "a".."b" > "a"); - -assert("7" .. 3 << 1 == 146) -assert(10 >> 1 .. "9" == 0) -assert(10 | 1 .. "9" == 27) - -assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4) -assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4) -assert(0xF0 & 0x0F + 1 == 0x10) - -assert(3^4//2^3//5 == 2) - -assert(-3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)) - -assert(not ((true or false) and nil)) -assert( true or false and nil) - --- old bug -assert((((1 or false) and true) or false) == true) -assert((((nil and true) or false) and true) == false) - -local a,b = 1,nil; -assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); -x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); -x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); - -x,y=1,2; -assert((x>y) and x or y == 2); -x,y=2,1; -assert((x>y) and x or y == 2); - -assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) - - --- silly loops -repeat until 1; repeat until true; -while false do end; while nil do end; - -do -- test old bug (first name could not be an `upvalue') - local a; function f(x) x={a=1}; x={x=1}; x={G=1} end -end - -function f (i) - if type(i) ~= 'number' then return i,'jojo'; end; - if i > 0 then return i, f(i-1); end; -end - -x = {f(3), f(5), f(10);}; -assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1); -assert(x[nil] == nil) -x = {f'alo', f'xixi', nil}; -assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil); -x = {f'alo'..'xixi'}; -assert(x[1] == 'aloxixi') -x = {f{}} -assert(x[2] == 'jojo' and type(x[1]) == 'table') - - -local f = function (i) - if i < 10 then return 'a'; - elseif i < 20 then return 'b'; - elseif i < 30 then return 'c'; - end; -end - -assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) - -for i=1,1000 do break; end; -n=100; -i=3; -t = {}; -a=nil -while not a do - a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; -end -assert(a == n*(n+1)/2 and i==3); -assert(t[1] and t[n] and not t[0] and not t[n+1]) - -function f(b) - local x = 1; - repeat - local a; - if b==1 then local b=1; x=10; break - elseif b==2 then x=20; break; - elseif b==3 then x=30; - else local a,b,c,d=math.sin(1); x=x+1; - end - until x>=12; - return x; -end; - -assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12) - - -local f = function (i) - if i < 10 then return 'a' - elseif i < 20 then return 'b' - elseif i < 30 then return 'c' - else return 8 - end -end - -assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8) - -local a, b = nil, 23 -x = {f(100)*2+3 or a, a or b+2} -assert(x[1] == 19 and x[2] == 25) -x = {f=2+3 or a, a = b+2} -assert(x.f == 5 and x.a == 25) - -a={y=1} -x = {a.y} -assert(x[1] == 1) - -function f(i) - while 1 do - if i>0 then i=i-1; - else return; end; - end; -end; - -function g(i) - while 1 do - if i>0 then i=i-1 - else return end - end -end - -f(10); g(10); - -do - function f () return 1,2,3; end - local a, b, c = f(); - assert(a==1 and b==2 and c==3) - a, b, c = (f()); - assert(a==1 and b==nil and c==nil) -end - -local a,b = 3 and f(); -assert(a==1 and b==nil) - -function g() f(); return; end; -assert(g() == nil) -function g() return nil or f() end -a,b = g() -assert(a==1 and b==nil) - -print'+'; - - -f = [[ -return function ( a , b , c , d , e ) - local x = a >= b or c or ( d and e ) or nil - return x -end , { a = 1 , b = 2 >= 1 , } or { 1 }; -]] -f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes -f,a = load(f)(); -assert(a.a == 1 and a.b) - -function g (a,b,c,d,e) - if not (a>=b or c or d and e or nil) then return 0; else return 1; end; -end - -function h (a,b,c,d,e) - while (a>=b or c or (d and e) or nil) do return 1; end; - return 0; -end; - -assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1) -assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) -assert(f(1,2,'a') -~= -- force SETLINE before nil -nil, "") -assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) -assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and - h(1,2,nil,1,'x') == 1) -assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and - h(1,2,nil,nil,'x') == 0) -assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and - h(1,2,nil,1,nil) == 0) - -assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true) -x = 2<3 and not 3; assert(x==false) -x = 2<1 or (2>1 and 'a'); assert(x=='a') - - -do - local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2 - assert(a==2) -end - -function F(a) - assert(debug.getinfo(1, "n").name == 'F') - return a,2,3 -end - -a,b = F(1)~=nil; assert(a == true and b == nil); -a,b = F(nil)==nil; assert(a == true and b == nil) - ----------------------------------------------------------------- ------------------------------------------------------------------- - --- sometimes will be 0, sometimes will not... -_ENV.GLOB1 = math.floor(os.time()) % 2 - --- basic expressions with their respective values -local basiccases = { - {"nil", nil}, - {"false", false}, - {"true", true}, - {"10", 10}, - {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, -} - -print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') - - --- operators with their respective values -local binops = { - {" and ", function (a,b) if not a then return a else return b end end}, - {" or ", function (a,b) if a then return a else return b end end}, -} - -local cases = {} - --- creates all combinations of '(cases[i] op cases[n-i])' plus --- 'not(cases[i] op cases[n-i])' (syntax + value) -local function createcases (n) - local res = {} - for i = 1, n - 1 do - for _, v1 in ipairs(cases[i]) do - for _, v2 in ipairs(cases[n - i]) do - for _, op in ipairs(binops) do - local t = { - "(" .. v1[1] .. op[1] .. v2[1] .. ")", - op[2](v1[2], v2[2]) - } - res[#res + 1] = t - res[#res + 1] = {"not" .. t[1], not t[2]} - end - end - end - end - return res -end - --- do not do too many combinations for soft tests -local level = _soft and 3 or 4 - -cases[1] = basiccases -for i = 2, level do cases[i] = createcases(i) end -print("+") - -local prog = [[if %s then IX = true end; return %s]] - -local i = 0 -for n = 1, level do - for _, v in pairs(cases[n]) do - local s = v[1] - local p = load(string.format(prog, s, s), "") - IX = false - assert(p() == v[2] and IX == not not v[2]) - i = i + 1 - if i % 60000 == 0 then print('+') end - end -end ------------------------------------------------------------------- - --- testing some syntax errors (chosen through 'gcov') -checkload("for x do", "expected") -checkload("x:call", "expected") - -if not _soft then - -- control structure too long - local s = string.rep("a = a + 1\n", 2^18) - s = "while true do " .. s .. "end" - checkload(s, "too long") -end - -print'OK' diff --git a/tofix/lua-5.3.4-tests/coroutine.lua b/tofix/lua-5.3.4-tests/coroutine.lua deleted file mode 100644 index c8fc857..0000000 --- a/tofix/lua-5.3.4-tests/coroutine.lua +++ /dev/null @@ -1,874 +0,0 @@ --- $Id: coroutine.lua,v 1.42 2016/11/07 13:03:20 roberto Exp $ --- See Copyright Notice in file all.lua - -print "testing coroutines" - -local debug = require'debug' - -local f - -local main, ismain = coroutine.running() -assert(type(main) == "thread" and ismain) -assert(not coroutine.resume(main)) -assert(not coroutine.isyieldable()) -assert(not pcall(coroutine.yield)) - - --- trivial errors -assert(not pcall(coroutine.resume, 0)) -assert(not pcall(coroutine.status, 0)) - - --- tests for multiple yield/resume arguments - -local function eqtab (t1, t2) - assert(#t1 == #t2) - for i = 1, #t1 do - local v = t1[i] - assert(t2[i] == v) - end -end - -_G.x = nil -- declare x -function foo (a, ...) - local x, y = coroutine.running() - assert(x == f and y == false) - -- next call should not corrupt coroutine (but must fail, - -- as it attempts to resume the running coroutine) - assert(coroutine.resume(f) == false) - assert(coroutine.status(f) == "running") - local arg = {...} - assert(coroutine.isyieldable()) - for i=1,#arg do - _G.x = {coroutine.yield(table.unpack(arg[i]))} - end - return table.unpack(a) -end - -f = coroutine.create(foo) -assert(type(f) == "thread" and coroutine.status(f) == "suspended") -assert(string.find(tostring(f), "thread")) -local s,a,b,c,d -s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) -assert(s and a == nil and coroutine.status(f) == "suspended") -s,a,b,c,d = coroutine.resume(f) -eqtab(_G.x, {}) -assert(s and a == 1 and b == nil) -s,a,b,c,d = coroutine.resume(f, 1, 2, 3) -eqtab(_G.x, {1, 2, 3}) -assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) -s,a,b,c,d = coroutine.resume(f, "xuxu") -eqtab(_G.x, {"xuxu"}) -assert(s and a == 1 and b == 2 and c == 3 and d == nil) -assert(coroutine.status(f) == "dead") -s, a = coroutine.resume(f, "xuxu") -assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") - - --- yields in tail calls -local function foo (i) return coroutine.yield(i) end -f = coroutine.wrap(function () - for i=1,10 do - assert(foo(i) == _G.x) - end - return 'a' -end) -for i=1,10 do _G.x = i; assert(f(i) == i) end -_G.x = 'xuxu'; assert(f('xuxu') == 'a') - --- recursive -function pf (n, i) - coroutine.yield(n) - pf(n*i, i+1) -end - -f = coroutine.wrap(pf) -local s=1 -for i=1,10 do - assert(f(1, 1) == s) - s = s*i -end - --- sieve -function gen (n) - return coroutine.wrap(function () - for i=2,n do coroutine.yield(i) end - end) -end - - -function filter (p, g) - return coroutine.wrap(function () - while 1 do - local n = g() - if n == nil then return end - if math.fmod(n, p) ~= 0 then coroutine.yield(n) end - end - end) -end - -local x = gen(100) -local a = {} -while 1 do - local n = x() - if n == nil then break end - table.insert(a, n) - x = filter(n, x) -end - -assert(#a == 25 and a[#a] == 97) -x, a = nil - --- yielding across C boundaries - -co = coroutine.wrap(function() - assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) - assert(coroutine.isyieldable()) - coroutine.yield(20) - return 30 - end) - -assert(co() == 20) -assert(co() == 30) - - -local f = function (s, i) return coroutine.yield(i) end - -local f1 = coroutine.wrap(function () - return xpcall(pcall, function (...) return ... end, - function () - local s = 0 - for i in f, nil, 1 do pcall(function () s = s + i end) end - error({s}) - end) - end) - -f1() -for i = 1, 10 do assert(f1(i) == i) end -local r1, r2, v = f1(nil) -assert(r1 and not r2 and v[1] == (10 + 1)*10/2) - - -function f (a, b) a = coroutine.yield(a); error{a + b} end -function g(x) return x[1]*2 end - -co = coroutine.wrap(function () - coroutine.yield(xpcall(f, g, 10, 20)) - end) - -assert(co() == 10) -r, msg = co(100) -assert(not r and msg == 240) - - --- unyieldable C call -do - local function f (c) - assert(not coroutine.isyieldable()) - return c .. c - end - - local co = coroutine.wrap(function (c) - assert(coroutine.isyieldable()) - local s = string.gsub("a", ".", f) - return s - end) - assert(co() == "aa") -end - - --- errors in coroutines -function foo () - assert(debug.getinfo(1).currentline == debug.getinfo(foo).linedefined + 1) - assert(debug.getinfo(2).currentline == debug.getinfo(goo).linedefined) - coroutine.yield(3) - error(foo) -end - -function goo() foo() end -x = coroutine.wrap(goo) -assert(x() == 3) -local a,b = pcall(x) -assert(not a and b == foo) - -x = coroutine.create(goo) -a,b = coroutine.resume(x) -assert(a and b == 3) -a,b = coroutine.resume(x) -assert(not a and b == foo and coroutine.status(x) == "dead") -a,b = coroutine.resume(x) -assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") - - --- co-routines x for loop -function all (a, n, k) - if k == 0 then coroutine.yield(a) - else - for i=1,n do - a[k] = i - all(a, n, k-1) - end - end -end - -local a = 0 -for t in coroutine.wrap(function () all({}, 5, 4) end) do - a = a+1 -end -assert(a == 5^4) - - --- access to locals of collected corroutines -local C = {}; setmetatable(C, {__mode = "kv"}) -local x = coroutine.wrap (function () - local a = 10 - local function f () a = a+10; return a end - while true do - a = a+1 - coroutine.yield(f) - end - end) - -C[1] = x; - -local f = x() -assert(f() == 21 and x()() == 32 and x() == f) -x = nil -collectgarbage() -assert(C[1] == nil) -assert(f() == 43 and f() == 53) - - --- old bug: attempt to resume itself - -function co_func (current_co) - assert(coroutine.running() == current_co) - assert(coroutine.resume(current_co) == false) - coroutine.yield(10, 20) - assert(coroutine.resume(current_co) == false) - coroutine.yield(23) - return 10 -end - -local co = coroutine.create(co_func) -local a,b,c = coroutine.resume(co, co) -assert(a == true and b == 10 and c == 20) -a,b = coroutine.resume(co, co) -assert(a == true and b == 23) -a,b = coroutine.resume(co, co) -assert(a == true and b == 10) -assert(coroutine.resume(co, co) == false) -assert(coroutine.resume(co, co) == false) - - --- other old bug when attempting to resume itself --- (trigger C-code assertions) -do - local A = coroutine.running() - local B = coroutine.create(function() return coroutine.resume(A) end) - local st, res = coroutine.resume(B) - assert(st == true and res == false) - - A = coroutine.wrap(function() return pcall(A, 1) end) - st, res = A() - assert(not st and string.find(res, "non%-suspended")) -end - - --- attempt to resume 'normal' coroutine -local co1, co2 -co1 = coroutine.create(function () return co2() end) -co2 = coroutine.wrap(function () - assert(coroutine.status(co1) == 'normal') - assert(not coroutine.resume(co1)) - coroutine.yield(3) - end) - -a,b = coroutine.resume(co1) -assert(a and b == 3) -assert(coroutine.status(co1) == 'dead') - --- infinite recursion of coroutines -a = function(a) coroutine.wrap(a)(a) end -assert(not pcall(a, a)) -a = nil - - --- access to locals of erroneous coroutines -local x = coroutine.create (function () - local a = 10 - _G.f = function () a=a+1; return a end - error('x') - end) - -assert(not coroutine.resume(x)) --- overwrite previous position of local `a' -assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) -assert(_G.f() == 11) -assert(_G.f() == 12) - - -if not T then - (Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n') -else - print "testing yields inside hooks" - - local turn - - function fact (t, x) - assert(turn == t) - if x == 0 then return 1 - else return x*fact(t, x-1) - end - end - - local A, B = 0, 0 - - local x = coroutine.create(function () - T.sethook("yield 0", "", 2) - A = fact("A", 6) - end) - - local y = coroutine.create(function () - T.sethook("yield 0", "", 3) - B = fact("B", 7) - end) - - while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') - if A==0 then turn = "A"; assert(T.resume(x)) end - if B==0 then turn = "B"; assert(T.resume(y)) end - end - - assert(B // A == 7) -- fact(7) // fact(6) - - local line = debug.getinfo(1, "l").currentline + 2 -- get line number - local function foo () - local x = 10 --<< this line is 'line' - x = x + 10 - _G.XX = x - end - - -- testing yields in line hook - local co = coroutine.wrap(function () - T.sethook("setglobal X; yield 0", "l", 0); foo(); return 10 end) - - _G.XX = nil; - _G.X = nil; co(); assert(_G.X == line) - _G.X = nil; co(); assert(_G.X == line + 1) - _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) - _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) - assert(co() == 10) - - -- testing yields in count hook - co = coroutine.wrap(function () - T.sethook("yield 0", "", 1); foo(); return 10 end) - - _G.XX = nil; - local c = 0 - repeat c = c + 1; local a = co() until a == 10 - assert(_G.XX == 20 and c >= 5) - - co = coroutine.wrap(function () - T.sethook("yield 0", "", 2); foo(); return 10 end) - - _G.XX = nil; - local c = 0 - repeat c = c + 1; local a = co() until a == 10 - assert(_G.XX == 20 and c >= 5) - _G.X = nil; _G.XX = nil - - do - -- testing debug library on a coroutine suspended inside a hook - -- (bug in 5.2/5.3) - c = coroutine.create(function (a, ...) - T.sethook("yield 0", "l") -- will yield on next two lines - assert(a == 10) - return ... - end) - - assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine - local n,v = debug.getlocal(c, 0, 1) -- check its local - assert(n == "a" and v == 1) - n,v = debug.getlocal(c, 0, -1) -- check varargs - assert(v == 2) - n,v = debug.getlocal(c, 0, -2) - assert(v == 3) - assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal' - assert(debug.setlocal(c, 0, -2, 20)) - local t = debug.getinfo(c, 0) -- test 'getinfo' - assert(t.currentline == t.linedefined + 1) - assert(not debug.getinfo(c, 1)) -- no other level - assert(coroutine.resume(c)) -- run next line - v = {coroutine.resume(c)} -- finish coroutine - assert(v[1] == true and v[2] == 2 and v[3] == 20 and v[4] == nil) - assert(not coroutine.resume(c)) - end - - do - -- testing debug library on last function in a suspended coroutine - -- (bug in 5.2/5.3) - local c = coroutine.create(function () T.testC("yield 1", 10, 20) end) - local a, b = coroutine.resume(c) - assert(a and b == 20) - assert(debug.getinfo(c, 0).linedefined == -1) - a, b = debug.getlocal(c, 0, 2) - assert(b == 10) - end - - - print "testing coroutine API" - - -- reusing a thread - assert(T.testC([[ - newthread # create thread - pushvalue 2 # push body - pushstring 'a a a' # push argument - xmove 0 3 2 # move values to new thread - resume -1, 1 # call it first time - pushstatus - xmove 3 0 0 # move results back to stack - setglobal X # result - setglobal Y # status - pushvalue 2 # push body (to call it again) - pushstring 'b b b' - xmove 0 3 2 - resume -1, 1 # call it again - pushstatus - xmove 3 0 0 - return 1 # return result - ]], function (...) return ... end) == 'b b b') - - assert(X == 'a a a' and Y == 'OK') - - - -- resuming running coroutine - C = coroutine.create(function () - return T.testC([[ - pushnum 10; - pushnum 20; - resume -3 2; - pushstatus - gettop; - return 3]], C) - end) - local a, b, c, d = coroutine.resume(C) - assert(a == true and string.find(b, "non%-suspended") and - c == "ERRRUN" and d == 4) - - a, b, c, d = T.testC([[ - rawgeti R 1 # get main thread - pushnum 10; - pushnum 20; - resume -3 2; - pushstatus - gettop; - return 4]]) - assert(a == coroutine.running() and string.find(b, "non%-suspended") and - c == "ERRRUN" and d == 4) - - - -- using a main thread as a coroutine - local state = T.newstate() - T.loadlib(state) - - assert(T.doremote(state, [[ - coroutine = require'coroutine'; - X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; - return 'ok']])) - - t = table.pack(T.testC(state, [[ - rawgeti R 1 # get main thread - pushstring 'XX' - getglobal X # get function for body - pushstring AA # arg - resume 1 1 # 'resume' shadows previous stack! - gettop - setglobal T # top - setglobal B # second yielded value - setglobal A # fist yielded value - rawgeti R 1 # get main thread - pushnum 5 # arg (noise) - resume 1 1 # after coroutine ends, previous stack is back - pushstatus - return * - ]])) - assert(t.n == 4 and t[2] == 'XX' and t[3] == 'CC' and t[4] == 'OK') - assert(T.doremote(state, "return T") == '2') - assert(T.doremote(state, "return A") == 'AA') - assert(T.doremote(state, "return B") == 'BB') - - T.closestate(state) - - print'+' - -end - - --- leaving a pending coroutine open -_X = coroutine.wrap(function () - local a = 10 - local x = function () a = a+1 end - coroutine.yield() - end) - -_X() - - -if not _soft then - -- bug (stack overflow) - local j = 2^9 - local lim = 1000000 -- (C stack limit; assume 32-bit machine) - local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1} - for i = 1, #t do - local j = t[i] - co = coroutine.create(function() - local t = {} - for i = 1, j do t[i] = i end - return table.unpack(t) - end) - local r, msg = coroutine.resume(co) - assert(not r) - end - co = nil -end - - -assert(coroutine.running() == main) - -print"+" - - -print"testing yields inside metamethods" - -local mt = { - __eq = function(a,b) coroutine.yield(nil, "eq"); return a.x == b.x end, - __lt = function(a,b) coroutine.yield(nil, "lt"); return a.x < b.x end, - __le = function(a,b) coroutine.yield(nil, "le"); return a - b <= 0 end, - __add = function(a,b) coroutine.yield(nil, "add"); return a.x + b.x end, - __sub = function(a,b) coroutine.yield(nil, "sub"); return a.x - b.x end, - __mod = function(a,b) coroutine.yield(nil, "mod"); return a.x % b.x end, - __unm = function(a,b) coroutine.yield(nil, "unm"); return -a.x end, - __bnot = function(a,b) coroutine.yield(nil, "bnot"); return ~a.x end, - __shl = function(a,b) coroutine.yield(nil, "shl"); return a.x << b.x end, - __shr = function(a,b) coroutine.yield(nil, "shr"); return a.x >> b.x end, - __band = function(a,b) - a = type(a) == "table" and a.x or a - b = type(b) == "table" and b.x or b - coroutine.yield(nil, "band") - return a & b - end, - __bor = function(a,b) coroutine.yield(nil, "bor"); return a.x | b.x end, - __bxor = function(a,b) coroutine.yield(nil, "bxor"); return a.x ~ b.x end, - - __concat = function(a,b) - coroutine.yield(nil, "concat"); - a = type(a) == "table" and a.x or a - b = type(b) == "table" and b.x or b - return a .. b - end, - __index = function (t,k) coroutine.yield(nil, "idx"); return t.k[k] end, - __newindex = function (t,k,v) coroutine.yield(nil, "nidx"); t.k[k] = v end, -} - - -local function new (x) - return setmetatable({x = x, k = {}}, mt) -end - - -local a = new(10) -local b = new(12) -local c = new"hello" - -local function run (f, t) - local i = 1 - local c = coroutine.wrap(f) - while true do - local res, stat = c() - if res then assert(t[i] == nil); return res, t end - assert(stat == t[i]) - i = i + 1 - end -end - - -assert(run(function () if (a>=b) then return '>=' else return '<' end end, - {"le", "sub"}) == "<") --- '<=' using '<' -mt.__le = nil -assert(run(function () if (a<=b) then return '<=' else return '>' end end, - {"lt"}) == "<=") -assert(run(function () if (a==b) then return '==' else return '~=' end end, - {"eq"}) == "~=") - -assert(run(function () return a & b + a end, {"add", "band"}) == 2) - -assert(run(function () return a % b end, {"mod"}) == 10) - -assert(run(function () return ~a & b end, {"bnot", "band"}) == ~10 & 12) -assert(run(function () return a | b end, {"bor"}) == 10 | 12) -assert(run(function () return a ~ b end, {"bxor"}) == 10 ~ 12) -assert(run(function () return a << b end, {"shl"}) == 10 << 12) -assert(run(function () return a >> b end, {"shr"}) == 10 >> 12) - -assert(run(function () return a..b end, {"concat"}) == "1012") - -assert(run(function() return a .. b .. c .. a end, - {"concat", "concat", "concat"}) == "1012hello10") - -assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end, - {"concat", "concat", "concat"}) == "ab10chello12x") - - -do -- a few more tests for comparsion operators - local mt1 = { - __le = function (a,b) - coroutine.yield(10) - return - (type(a) == "table" and a.x or a) <= (type(b) == "table" and b.x or b) - end, - __lt = function (a,b) - coroutine.yield(10) - return - (type(a) == "table" and a.x or a) < (type(b) == "table" and b.x or b) - end, - } - local mt2 = { __lt = mt1.__lt } -- no __le - - local function run (f) - local co = coroutine.wrap(f) - local res - repeat - res = co() - until res ~= 10 - return res - end - - local function test () - local a1 = setmetatable({x=1}, mt1) - local a2 = setmetatable({x=2}, mt2) - assert(a1 < a2) - assert(a1 <= a2) - assert(1 < a2) - assert(1 <= a2) - assert(2 > a1) - assert(2 >= a2) - return true - end - - run(test) - -end - -assert(run(function () - a.BB = print - return a.BB - end, {"nidx", "idx"}) == print) - --- getuptable & setuptable -do local _ENV = _ENV - f = function () AAA = BBB + 1; return AAA end -end -g = new(10); g.k.BBB = 10; -debug.setupvalue(f, 1, g) -assert(run(f, {"idx", "nidx", "idx"}) == 11) -assert(g.k.AAA == 11) - -print"+" - -print"testing yields inside 'for' iterators" - -local f = function (s, i) - if i%2 == 0 then coroutine.yield(nil, "for") end - if i < s then return i + 1 end - end - -assert(run(function () - local s = 0 - for i in f, 4, 0 do s = s + i end - return s - end, {"for", "for", "for"}) == 10) - - - --- tests for coroutine API -if T==nil then - (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') - return -end - -print('testing coroutine API') - -local function apico (...) - local x = {...} - return coroutine.wrap(function () - return T.testC(table.unpack(x)) - end) -end - -local a = {apico( -[[ - pushstring errorcode - pcallk 1 0 2; - invalid command (should not arrive here) -]], -[[return *]], -"stackmark", -error -)()} -assert(#a == 4 and - a[3] == "stackmark" and - a[4] == "errorcode" and - _G.status == "ERRRUN" and - _G.ctx == 2) -- 'ctx' to pcallk - -local co = apico( - "pushvalue 2; pushnum 10; pcallk 1 2 3; invalid command;", - coroutine.yield, - "getglobal status; getglobal ctx; pushvalue 2; pushstring a; pcallk 1 0 4; invalid command", - "getglobal status; getglobal ctx; return *") - -assert(co() == 10) -assert(co(20, 30) == 'a') -a = {co()} -assert(#a == 10 and - a[2] == coroutine.yield and - a[5] == 20 and a[6] == 30 and - a[7] == "YIELD" and a[8] == 3 and - a[9] == "YIELD" and a[10] == 4) -assert(not pcall(co)) -- coroutine is dead now - - -f = T.makeCfunc("pushnum 3; pushnum 5; yield 1;") -co = coroutine.wrap(function () - assert(f() == 23); assert(f() == 23); return 10 -end) -assert(co(23,16) == 5) -assert(co(23,16) == 5) -assert(co(23,16) == 10) - - --- testing coroutines with C bodies -f = T.makeCfunc([[ - pushnum 102 - yieldk 1 U2 - cannot be here! -]], -[[ # continuation - pushvalue U3 # accessing upvalues inside a continuation - pushvalue U4 - return * -]], 23, "huu") - -x = coroutine.wrap(f) -assert(x() == 102) -eqtab({x()}, {23, "huu"}) - - -f = T.makeCfunc[[pushstring 'a'; pushnum 102; yield 2; ]] - -a, b, c, d = T.testC([[newthread; pushvalue 2; xmove 0 3 1; resume 3 0; - pushstatus; xmove 3 0 0; resume 3 0; pushstatus; - return 4; ]], f) - -assert(a == 'YIELD' and b == 'a' and c == 102 and d == 'OK') - - --- testing chain of suspendable C calls - -local count = 3 -- number of levels - -f = T.makeCfunc([[ - remove 1; # remove argument - pushvalue U3; # get selection function - call 0 1; # call it (result is 'f' or 'yield') - pushstring hello # single argument for selected function - pushupvalueindex 2; # index of continuation program - callk 1 -1 .; # call selected function - errorerror # should never arrive here -]], -[[ - # continuation program - pushnum 34 # return value - return * # return all results -]], -function () -- selection function - count = count - 1 - if count == 0 then return coroutine.yield - else return f - end -end -) - -co = coroutine.wrap(function () return f(nil) end) -assert(co() == "hello") -- argument to 'yield' -a = {co()} --- three '34's (one from each pending C call) -assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34) - - --- testing yields with continuations - -co = coroutine.wrap(function (...) return - T.testC([[ # initial function - yieldk 1 2 - cannot be here! - ]], - [[ # 1st continuation - yieldk 0 3 - cannot be here! - ]], - [[ # 2nd continuation - yieldk 0 4 - cannot be here! - ]], - [[ # 3th continuation - pushvalue 6 # function which is last arg. to 'testC' here - pushnum 10; pushnum 20; - pcall 2 0 0 # call should throw an error and return to next line - pop 1 # remove error message - pushvalue 6 - getglobal status; getglobal ctx - pcallk 2 2 5 # call should throw an error and jump to continuation - cannot be here! - ]], - [[ # 4th (and last) continuation - return * - ]], - -- function called by 3th continuation - function (a,b) x=a; y=b; error("errmsg") end, - ... -) -end) - -local a = {co(3,4,6)} -assert(a[1] == 6 and a[2] == nil) -a = {co()}; assert(a[1] == nil and _G.status == "YIELD" and _G.ctx == 2) -a = {co()}; assert(a[1] == nil and _G.status == "YIELD" and _G.ctx == 3) -a = {co(7,8)}; --- original arguments -assert(type(a[1]) == 'string' and type(a[2]) == 'string' and - type(a[3]) == 'string' and type(a[4]) == 'string' and - type(a[5]) == 'string' and type(a[6]) == 'function') --- arguments left from fist resume -assert(a[7] == 3 and a[8] == 4) --- arguments to last resume -assert(a[9] == 7 and a[10] == 8) --- error message and nothing more -assert(a[11]:find("errmsg") and #a == 11) --- check arguments to pcallk -assert(x == "YIELD" and y == 4) - -assert(not pcall(co)) -- coroutine should be dead - - --- bug in nCcalls -local co = coroutine.wrap(function () - local a = {pcall(pcall,pcall,pcall,pcall,pcall,pcall,pcall,error,"hi")} - return pcall(assert, table.unpack(a)) -end) - -local a = {co()} -assert(a[10] == "hi") - -print'OK' diff --git a/tofix/lua-5.3.4-tests/db.lua b/tofix/lua-5.3.4-tests/db.lua deleted file mode 100644 index 004f57a..0000000 --- a/tofix/lua-5.3.4-tests/db.lua +++ /dev/null @@ -1,857 +0,0 @@ --- $Id: db.lua,v 1.79 2016/11/07 13:02:34 roberto Exp $ --- See Copyright Notice in file all.lua - --- testing debug library - -local debug = require "debug" - -local function dostring(s) return assert(load(s))() end - -print"testing debug library and debug information" - -do -local a=1 -end - -assert(not debug.gethook()) - -local testline = 19 -- line where 'test' is defined -function test (s, l, p) -- this must be line 19 - collectgarbage() -- avoid gc during trace - local function f (event, line) - assert(event == 'line') - local l = table.remove(l, 1) - if p then print(l, line) end - assert(l == line, "wrong trace!!") - end - debug.sethook(f,"l"); load(s)(); debug.sethook() - assert(#l == 0) -end - - -do - assert(not pcall(debug.getinfo, print, "X")) -- invalid option - assert(not debug.getinfo(1000)) -- out of range level - assert(not debug.getinfo(-1)) -- out of range level - local a = debug.getinfo(print) - assert(a.what == "C" and a.short_src == "[C]") - a = debug.getinfo(print, "L") - assert(a.activelines == nil) - local b = debug.getinfo(test, "SfL") - assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and - b.lastlinedefined == b.linedefined + 10 and - b.func == test and not string.find(b.short_src, "%[")) - assert(b.activelines[b.linedefined + 1] and - b.activelines[b.lastlinedefined]) - assert(not b.activelines[b.linedefined] and - not b.activelines[b.lastlinedefined + 1]) -end - - --- test file and string names truncation -a = "function f () end" -local function dostring (s, x) return load(s, x)() end -dostring(a) -assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) -dostring(a..string.format("; %s\n=1", string.rep('p', 400))) -assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) -dostring(a..string.format("; %s=1", string.rep('p', 400))) -assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) -dostring("\n"..a) -assert(debug.getinfo(f).short_src == '[string "..."]') -dostring(a, "") -assert(debug.getinfo(f).short_src == '[string ""]') -dostring(a, "@xuxu") -assert(debug.getinfo(f).short_src == "xuxu") -dostring(a, "@"..string.rep('p', 1000)..'t') -assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$")) -dostring(a, "=xuxu") -assert(debug.getinfo(f).short_src == "xuxu") -dostring(a, string.format("=%s", string.rep('x', 500))) -assert(string.find(debug.getinfo(f).short_src, "^x*$")) -dostring(a, "=") -assert(debug.getinfo(f).short_src == "") -a = nil; f = nil; - - -repeat - local g = {x = function () - local a = debug.getinfo(2) - assert(a.name == 'f' and a.namewhat == 'local') - a = debug.getinfo(1) - assert(a.name == 'x' and a.namewhat == 'field') - return 'xixi' - end} - local f = function () return 1+1 and (not 1 or g.x()) end - assert(f() == 'xixi') - g = debug.getinfo(f) - assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name) - - function f (x, name) -- local! - name = name or 'f' - local a = debug.getinfo(1) - assert(a.name == name and a.namewhat == 'local') - return x - end - - -- breaks in different conditions - if 3>4 then break end; f() - if 3<4 then a=1 else break end; f() - while 1 do local x=10; break end; f() - local b = 1 - if 3>4 then return math.sin(1) end; f() - a = 3<4; f() - a = 3<4 or 1; f() - repeat local x=20; if 4>3 then f() else break end; f() until 1 - g = {} - f(g).x = f(2) and f(10)+f(9) - assert(g.x == f(19)) - function g(x) if not x then return 3 end return (x('a', 'x')) end - assert(g(f) == 'a') -until 1 - -test([[if -math.sin(1) -then - a=1 -else - a=2 -end -]], {2,3,4,7}) - -test([[-- -if nil then - a=1 -else - a=2 -end -]], {2,5,6}) - -test([[a=1 -repeat - a=a+1 -until a==3 -]], {1,3,4,3,4}) - -test([[ do - return -end -]], {2}) - -test([[local a -a=1 -while a<=3 do - a=a+1 -end -]], {1,2,3,4,3,4,3,4,3,5}) - -test([[while math.sin(1) do - if math.sin(1) - then break - end -end -a=1]], {1,2,3,6}) - -test([[for i=1,3 do - a=i -end -]], {1,2,1,2,1,2,1,3}) - -test([[for i,v in pairs{'a','b'} do - a=tostring(i) .. v -end -]], {1,2,1,2,1,3}) - -test([[for i=1,4 do a=1 end]], {1,1,1,1,1}) - - - -print'+' - --- invalid levels in [gs]etlocal -assert(not pcall(debug.getlocal, 20, 1)) -assert(not pcall(debug.setlocal, -1, 1, 10)) - - --- parameter names -local function foo (a,b,...) local d, e end -local co = coroutine.create(foo) - -assert(debug.getlocal(foo, 1) == 'a') -assert(debug.getlocal(foo, 2) == 'b') -assert(not debug.getlocal(foo, 3)) -assert(debug.getlocal(co, foo, 1) == 'a') -assert(debug.getlocal(co, foo, 2) == 'b') -assert(not debug.getlocal(co, foo, 3)) - -assert(not debug.getlocal(print, 1)) - - --- varargs -local function foo (a, ...) - local t = table.pack(...) - for i = 1, t.n do - local n, v = debug.getlocal(1, -i) - assert(n == "(*vararg)" and v == t[i]) - end - assert(not debug.getlocal(1, -(t.n + 1))) - assert(not debug.setlocal(1, -(t.n + 1), 30)) - if t.n > 0 then - (function (x) - assert(debug.setlocal(2, -1, x) == "(*vararg)") - assert(debug.setlocal(2, -t.n, x) == "(*vararg)") - end)(430) - assert(... == 430) - end -end - -foo() -foo(print) -foo(200, 3, 4) -local a = {} -for i = 1, (_soft and 100 or 1000) do a[i] = i end -foo(table.unpack(a)) -a = nil - --- access to vararg in non-vararg function -local function foo () return debug.getlocal(1, -1) end -assert(not foo(10)) - - -do -- test hook presence in debug info - assert(not debug.gethook()) - local count = 0 - local function f () - assert(debug.getinfo(1).namewhat == "hook") - local sndline = string.match(debug.traceback(), "\n(.-)\n") - assert(string.find(sndline, "hook")) - count = count + 1 - end - debug.sethook(f, "l") - local a = 0 - _ENV.a = a - a = 1 - debug.sethook() - assert(count == 4) -end - - -a = {}; L = nil -local glob = 1 -local oldglob = glob -debug.sethook(function (e,l) - collectgarbage() -- force GC during a hook - local f, m, c = debug.gethook() - assert(m == 'crl' and c == 0) - if e == "line" then - if glob ~= oldglob then - L = l-1 -- get the first line where "glob" has changed - oldglob = glob - end - elseif e == "call" then - local f = debug.getinfo(2, "f").func - a[f] = 1 - else assert(e == "return") - end -end, "crl") - - -function f(a,b) - collectgarbage() - local _, x = debug.getlocal(1, 1) - local _, y = debug.getlocal(1, 2) - assert(x == a and y == b) - assert(debug.setlocal(2, 3, "pera") == "AA".."AA") - assert(debug.setlocal(2, 4, "ma") == "B") - x = debug.getinfo(2) - assert(x.func == g and x.what == "Lua" and x.name == 'g' and - x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) - glob = glob+1 - assert(debug.getinfo(1, "l").currentline == L+1) - assert(debug.getinfo(1, "l").currentline == L+2) -end - -function foo() - glob = glob+1 - assert(debug.getinfo(1, "l").currentline == L+1) -end; foo() -- set L --- check line counting inside strings and empty lines - -_ = 'alo\ -alo' .. [[ - -]] ---[[ -]] -assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines - - -function g(...) - local arg = {...} - do local a,b,c; a=math.sin(40); end - local feijao - local AAAA,B = "xuxu", "mamo" - f(AAAA,B) - assert(AAAA == "pera" and B == "ma") - do - local B = 13 - local x,y = debug.getlocal(1,5) - assert(x == 'B' and y == 13) - end -end - -g() - - -assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print]) - - --- tests for manipulating non-registered locals (C and Lua temporaries) - -local n, v = debug.getlocal(0, 1) -assert(v == 0 and n == "(*temporary)") -local n, v = debug.getlocal(0, 2) -assert(v == 2 and n == "(*temporary)") -assert(not debug.getlocal(0, 3)) -assert(not debug.getlocal(0, 0)) - -function f() - assert(select(2, debug.getlocal(2,3)) == 1) - assert(not debug.getlocal(2,4)) - debug.setlocal(2, 3, 10) - return 20 -end - -function g(a,b) return (a+1) + f() end - -assert(g(0,0) == 30) - - -debug.sethook(nil); -assert(debug.gethook() == nil) - - --- testing access to function arguments - -local function collectlocals (level) - local tab = {} - for i = 1, math.huge do - local n, v = debug.getlocal(level + 1, i) - if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then - break -- consider only real variables - end - tab[n] = v - end - return tab -end - - -X = nil -a = {} -function a:f (a, b, ...) local arg = {...}; local c = 13 end -debug.sethook(function (e) - assert(e == "call") - dostring("XX = 12") -- test dostring inside hooks - -- testing errors inside hooks - assert(not pcall(load("a='joao'+1"))) - debug.sethook(function (e, l) - assert(debug.getinfo(2, "l").currentline == l) - local f,m,c = debug.gethook() - assert(e == "line") - assert(m == 'l' and c == 0) - debug.sethook(nil) -- hook is called only once - assert(not X) -- check that - X = collectlocals(2) - end, "l") -end, "c") - -a:f(1,2,3,4,5) -assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) -assert(XX == 12) -assert(debug.gethook() == nil) - - --- testing access to local variables in return hook (bug in 5.2) -do - local function foo (a, b) - do local x,y,z end - local c, d = 10, 20 - return - end - - local function aux () - if debug.getinfo(2).name == "foo" then - foo = nil -- to signal that it found 'foo' - local tab = {a = 100, b = 200, c = 10, d = 20} - for n, v in pairs(collectlocals(2)) do - assert(tab[n] == v) - tab[n] = nil - end - assert(next(tab) == nil) -- 'tab' must be empty - end - end - - debug.sethook(aux, "r"); foo(100, 200); debug.sethook() - assert(foo == nil) -end - --- testing upvalue access -local function getupvalues (f) - local t = {} - local i = 1 - while true do - local name, value = debug.getupvalue(f, i) - if not name then break end - assert(not t[name]) - t[name] = value - i = i + 1 - end - return t -end - -local a,b,c = 1,2,3 -local function foo1 (a) b = a; return c end -local function foo2 (x) a = x; return c+b end -assert(not debug.getupvalue(foo1, 3)) -assert(not debug.getupvalue(foo1, 0)) -assert(not debug.setupvalue(foo1, 3, "xuxu")) -local t = getupvalues(foo1) -assert(t.a == nil and t.b == 2 and t.c == 3) -t = getupvalues(foo2) -assert(t.a == 1 and t.b == 2 and t.c == 3) -assert(debug.setupvalue(foo1, 1, "xuxu") == "b") -assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu") --- upvalues of C functions are allways "called" "" (the empty string) -assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "") - - --- testing count hooks -local a=0 -debug.sethook(function (e) a=a+1 end, "", 1) -a=0; for i=1,1000 do end; assert(1000 < a and a < 1012) -debug.sethook(function (e) a=a+1 end, "", 4) -a=0; for i=1,1000 do end; assert(250 < a and a < 255) -local f,m,c = debug.gethook() -assert(m == "" and c == 4) -debug.sethook(function (e) a=a+1 end, "", 4000) -a=0; for i=1,1000 do end; assert(a == 0) - -do - debug.sethook(print, "", 2^24 - 1) -- count upperbound - local f,m,c = debug.gethook() - assert(({debug.gethook()})[3] == 2^24 - 1) -end - -debug.sethook() - - --- tests for tail calls -local function f (x) - if x then - assert(debug.getinfo(1, "S").what == "Lua") - assert(debug.getinfo(1, "t").istailcall == true) - local tail = debug.getinfo(2) - assert(tail.func == g1 and tail.istailcall == true) - assert(debug.getinfo(3, "S").what == "main") - print"+" - end -end - -function g(x) return f(x) end - -function g1(x) g(x) end - -local function h (x) local f=g1; return f(x) end - -h(true) - -local b = {} -debug.sethook(function (e) table.insert(b, e) end, "cr") -h(false) -debug.sethook() -local res = {"return", -- first return (from sethook) - "call", "tail call", "call", "tail call", - "return", "return", - "call", -- last call (to sethook) -} -for i = 1, #res do assert(res[i] == table.remove(b, 1)) end - -b = 0 -debug.sethook(function (e) - if e == "tail call" then - b = b + 1 - assert(debug.getinfo(2, "t").istailcall == true) - else - assert(debug.getinfo(2, "t").istailcall == false) - end - end, "c") -h(false) -debug.sethook() -assert(b == 2) -- two tail calls - -lim = _soft and 3000 or 30000 -local function foo (x) - if x==0 then - assert(debug.getinfo(2).what == "main") - local info = debug.getinfo(1) - assert(info.istailcall == true and info.func == foo) - else return foo(x-1) - end -end - -foo(lim) - - -print"+" - - --- testing local function information -co = load[[ - local A = function () - return x - end - return -]] - -local a = 0 --- 'A' should be visible to debugger only after its complete definition -debug.sethook(function (e, l) - if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(*temporary)") - elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A") - end -end, "l") -co() -- run local function definition -debug.sethook() -- turn off hook -assert(a == 2) -- ensure all two lines where hooked - --- testing traceback - -assert(debug.traceback(print) == print) -assert(debug.traceback(print, 4) == print) -assert(string.find(debug.traceback("hi", 4), "^hi\n")) -assert(string.find(debug.traceback("hi"), "^hi\n")) -assert(not string.find(debug.traceback("hi"), "'debug.traceback'")) -assert(string.find(debug.traceback("hi", 0), "'debug.traceback'")) -assert(string.find(debug.traceback(), "^stack traceback:\n")) - -do -- C-function names in traceback - local st, msg = (function () return pcall end)()(debug.traceback) - assert(st == true and string.find(msg, "pcall")) -end - - --- testing nparams, nups e isvararg -local t = debug.getinfo(print, "u") -assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) - -t = debug.getinfo(function (a,b,c) end, "u") -assert(t.isvararg == false and t.nparams == 3 and t.nups == 0) - -t = debug.getinfo(function (a,b,...) return t[a] end, "u") -assert(t.isvararg == true and t.nparams == 2 and t.nups == 1) - -t = debug.getinfo(1) -- main -assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and - debug.getupvalue(t.func, 1) == "_ENV") - - - - --- testing debugging of coroutines - -local function checktraceback (co, p, level) - local tb = debug.traceback(co, nil, level) - local i = 0 - for l in string.gmatch(tb, "[^\n]+\n?") do - assert(i == 0 or string.find(l, p[i])) - i = i+1 - end - assert(p[i] == nil) -end - - -local function f (n) - if n > 0 then f(n-1) - else coroutine.yield() end -end - -local co = coroutine.create(f) -coroutine.resume(co, 3) -checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"}) -checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1) -checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2) -checktraceback(co, {"db.lua"}, 4) -checktraceback(co, {}, 40) - - -co = coroutine.create(function (x) - local a = 1 - coroutine.yield(debug.getinfo(1, "l")) - coroutine.yield(debug.getinfo(1, "l").currentline) - return a - end) - -local tr = {} -local foo = function (e, l) if l then table.insert(tr, l) end end -debug.sethook(co, foo, "lcr") - -local _, l = coroutine.resume(co, 10) -local x = debug.getinfo(co, 1, "lfLS") -assert(x.currentline == l.currentline and x.activelines[x.currentline]) -assert(type(x.func) == "function") -for i=x.linedefined + 1, x.lastlinedefined do - assert(x.activelines[i]) - x.activelines[i] = nil -end -assert(next(x.activelines) == nil) -- no 'extra' elements -assert(not debug.getinfo(co, 2)) -local a,b = debug.getlocal(co, 1, 1) -assert(a == "x" and b == 10) -a,b = debug.getlocal(co, 1, 2) -assert(a == "a" and b == 1) -debug.setlocal(co, 1, 2, "hi") -assert(debug.gethook(co) == foo) -assert(#tr == 2 and - tr[1] == l.currentline-1 and tr[2] == l.currentline) - -a,b,c = pcall(coroutine.resume, co) -assert(a and b and c == l.currentline+1) -checktraceback(co, {"yield", "in function <"}) - -a,b = coroutine.resume(co) -assert(a and b == "hi") -assert(#tr == 4 and tr[4] == l.currentline+2) -assert(debug.gethook(co) == foo) -assert(not debug.gethook()) -checktraceback(co, {}) - - --- check get/setlocal in coroutines -co = coroutine.create(function (x) - local a, b = coroutine.yield(x) - assert(a == 100 and b == nil) - return x -end) -a, b = coroutine.resume(co, 10) -assert(a and b == 10) -a, b = debug.getlocal(co, 1, 1) -assert(a == "x" and b == 10) -assert(not debug.getlocal(co, 1, 5)) -assert(debug.setlocal(co, 1, 1, 30) == "x") -assert(not debug.setlocal(co, 1, 5, 40)) -a, b = coroutine.resume(co, 100) -assert(a and b == 30) - - --- check traceback of suspended (or dead with error) coroutines - -function f(i) if i==0 then error(i) else coroutine.yield(); f(i-1) end end - -co = coroutine.create(function (x) f(x) end) -a, b = coroutine.resume(co, 3) -t = {"'coroutine.yield'", "'f'", "in function <"} -while coroutine.status(co) == "suspended" do - checktraceback(co, t) - a, b = coroutine.resume(co) - table.insert(t, 2, "'f'") -- one more recursive call to 'f' -end -t[1] = "'error'" -checktraceback(co, t) - - --- test acessing line numbers of a coroutine from a resume inside --- a C function (this is a known bug in Lua 5.0) - -local function g(x) - coroutine.yield(x) -end - -local function f (i) - debug.sethook(function () end, "l") - for j=1,1000 do - g(i+j) - end -end - -local co = coroutine.wrap(f) -co(10) -pcall(co) -pcall(co) - - -assert(type(debug.getregistry()) == "table") - - --- test tagmethod information -local a = {} -local function f (t) - local info = debug.getinfo(1); - assert(info.namewhat == "metamethod") - a.op = info.name - return info.name -end -setmetatable(a, { - __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f; - __mul = f; __idiv = f; __unm = f; __len = f; __sub = f; - __shl = f; __shr = f; __bor = f; __bxor = f; - __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f; - __bnot = f; -}) - -local b = setmetatable({}, getmetatable(a)) - -assert(a[3] == "__index" and a^3 == "__pow" and a..a == "__concat") -assert(a/3 == "__div" and 3%a == "__mod") -assert(a+3 == "__add" and 3-a == "__sub" and a*3 == "__mul" and - -a == "__unm" and #a == "__len" and a&3 == "__band") -assert(a|3 == "__bor" and 3~a == "__bxor" and a<<3 == "__shl" and - a>>1 == "__shr") -assert (a==b and a.op == "__eq") -assert (a>=b and a.op == "__le") -assert (a>b and a.op == "__lt") -assert(~a == "__bnot") - -do -- testing for-iterator name - local function f() - assert(debug.getinfo(1).name == "for iterator") - end - - for i in f do end -end - - -do -- testing debug info for finalizers - local name = nil - - -- create a piece of garbage with a finalizer - setmetatable({}, {__gc = function () - local t = debug.getinfo(2) -- get callee information - assert(t.namewhat == "metamethod") - name = t.name - end}) - - -- repeat until previous finalizer runs (setting 'name') - repeat local a = {} until name - assert(name == "__gc") -end - - -do - print("testing traceback sizes") - - local function countlines (s) - return select(2, string.gsub(s, "\n", "")) - end - - local function deep (lvl, n) - if lvl == 0 then - return (debug.traceback("message", n)) - else - return (deep(lvl-1, n)) - end - end - - local function checkdeep (total, start) - local s = deep(total, start) - local rest = string.match(s, "^message\nstack traceback:\n(.*)$") - local cl = countlines(rest) - -- at most 10 lines in first part, 11 in second, plus '...' - assert(cl <= 10 + 11 + 1) - local brk = string.find(rest, "%.%.%.") - if brk then -- does message have '...'? - local rest1 = string.sub(rest, 1, brk) - local rest2 = string.sub(rest, brk, #rest) - assert(countlines(rest1) == 10 and countlines(rest2) == 11) - else - assert(cl == total - start + 2) - end - end - - for d = 1, 51, 10 do - for l = 1, d do - -- use coroutines to ensure complete control of the stack - coroutine.wrap(checkdeep)(d, l) - end - end - -end - - -print("testing debug functions on chunk without debug info") -prog = [[-- program to be loaded without debug information -local debug = require'debug' -local a = 12 -- a local variable - -local n, v = debug.getlocal(1, 1) -assert(n == "(*temporary)" and v == debug) -- unkown name but known value -n, v = debug.getlocal(1, 2) -assert(n == "(*temporary)" and v == 12) -- unkown name but known value - --- a function with an upvalue -local f = function () local x; return a end -n, v = debug.getupvalue(f, 1) -assert(n == "(*no name)" and v == 12) -assert(debug.setupvalue(f, 1, 13) == "(*no name)") -assert(a == 13) - -local t = debug.getinfo(f) -assert(t.name == nil and t.linedefined > 0 and - t.lastlinedefined == t.linedefined and - t.short_src == "?") -assert(debug.getinfo(1).currentline == -1) - -t = debug.getinfo(f, "L").activelines -assert(next(t) == nil) -- active lines are empty - --- dump/load a function without debug info -f = load(string.dump(f)) - -t = debug.getinfo(f) -assert(t.name == nil and t.linedefined > 0 and - t.lastlinedefined == t.linedefined and - t.short_src == "?") -assert(debug.getinfo(1).currentline == -1) - -return a -]] - - --- load 'prog' without debug info -local f = assert(load(string.dump(load(prog), true))) - -assert(f() == 13) - -do -- tests for 'source' in binary dumps - local prog = [[ - return function (x) - return function (y) - return x + y - end - end - ]] - local name = string.rep("x", 1000) - local p = assert(load(prog, name)) - -- load 'p' as a binary chunk with debug information - local c = string.dump(p) - assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump - local f = assert(load(c)) - local g = f() - local h = g(3) - assert(h(5) == 8) - assert(debug.getinfo(f).source == name and -- all functions have 'source' - debug.getinfo(g).source == name and - debug.getinfo(h).source == name) - -- again, without debug info - local c = string.dump(p, true) - assert(#c < 500) -- no 'source' in dump - local f = assert(load(c)) - local g = f() - local h = g(30) - assert(h(50) == 80) - assert(debug.getinfo(f).source == '=?' and -- no function has 'source' - debug.getinfo(g).source == '=?' and - debug.getinfo(h).source == '=?') -end - -print"OK" - diff --git a/tofix/lua-5.3.4-tests/errors.lua b/tofix/lua-5.3.4-tests/errors.lua deleted file mode 100644 index 79d746c..0000000 --- a/tofix/lua-5.3.4-tests/errors.lua +++ /dev/null @@ -1,537 +0,0 @@ --- $Id: errors.lua,v 1.94 2016/12/21 19:23:02 roberto Exp $ --- See Copyright Notice in file all.lua - -print("testing errors") - -local debug = require"debug" - --- avoid problems with 'strict' module (which may generate other error messages) -local mt = getmetatable(_G) or {} -local oldmm = mt.__index -mt.__index = nil - -local function checkerr (msg, f, ...) - local st, err = pcall(f, ...) - assert(not st and string.find(err, msg)) -end - - -local function doit (s) - local f, msg = load(s) - if f == nil then return msg end - local cond, msg = pcall(f) - return (not cond) and msg -end - - -local function checkmessage (prog, msg) - local m = doit(prog) - assert(string.find(m, msg, 1, true)) -end - -local function checksyntax (prog, extra, token, line) - local msg = doit(prog) - if not string.find(token, "^<%a") and not string.find(token, "^char%(") - then token = "'"..token.."'" end - token = string.gsub(token, "(%p)", "%%%1") - local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]], - line, token) - assert(string.find(msg, pt)) - assert(string.find(msg, msg, 1, true)) -end - - --- test error message with no extra info -assert(doit("error('hi', 0)") == 'hi') - --- test error message with no info -assert(doit("error()") == nil) - - --- test common errors/errors that crashed in the past -assert(doit("table.unpack({}, 1, n=2^30)")) -assert(doit("a=math.sin()")) -assert(not doit("tostring(1)") and doit("tostring()")) -assert(doit"tonumber()") -assert(doit"repeat until 1; a") -assert(doit"return;;") -assert(doit"assert(false)") -assert(doit"assert(nil)") -assert(doit("function a (... , ...) end")) -assert(doit("function a (, ...) end")) -assert(doit("local t={}; t = t[#t] + 1")) - -checksyntax([[ - local a = {4 - -]], "'}' expected (to close '{' at line 1)", "", 3) - - --- tests for better error messages - -checkmessage("a = {} + 1", "arithmetic") -checkmessage("a = {} | 1", "bitwise operation") -checkmessage("a = {} < 1", "attempt to compare") -checkmessage("a = {} <= 1", "attempt to compare") - -checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") -checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") -checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") -assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) -checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") -checkmessage("a=(1)..{}", "a table value") - -checkmessage("a = #print", "length of a function value") -checkmessage("a = #3", "length of a number value") - -aaa = nil -checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") -checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") -checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") -checkmessage("local a,b,c; (function () a = b+1 end)()", "upvalue 'b'") -assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)") - --- upvalues being indexed do not go to the stack -checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'") -checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'") - -checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") - -checkmessage("b=1; local aaa='a'; x=aaa+b", "local 'aaa'") -checkmessage("aaa={}; x=3/aaa", "global 'aaa'") -checkmessage("aaa='2'; b=nil;x=aaa*b", "global 'b'") -checkmessage("aaa={}; x=-aaa", "global 'aaa'") - --- short circuit -checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", - "local 'bbbb'") -checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") -checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") -checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") -assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) -assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'")) - -checkmessage("print(print < 10)", "function with number") -checkmessage("print(print < print)", "two function values") -checkmessage("print('10' < 10)", "string with number") -checkmessage("print(10 < '23')", "number with string") - --- float->integer conversions -checkmessage("local a = 2.0^100; x = a << 2", "local a") -checkmessage("local a = 1 >> 2.0^100", "has no integer representation") -checkmessage("local a = '10' << 2.0^100", "has no integer representation") -checkmessage("local a = 2.0^100 & 1", "has no integer representation") -checkmessage("local a = 2.0^100 & '1'", "has no integer representation") -checkmessage("local a = 2.0 | 1e40", "has no integer representation") -checkmessage("local a = 2e100 ~ 1", "has no integer representation") -checkmessage("string.sub('a', 2.0^100)", "has no integer representation") -checkmessage("string.rep('a', 3.3)", "has no integer representation") -checkmessage("return 6e40 & 7", "has no integer representation") -checkmessage("return 34 << 7e30", "has no integer representation") -checkmessage("return ~-3e40", "has no integer representation") -checkmessage("return ~-3.009", "has no integer representation") -checkmessage("return 3.009 & 1", "has no integer representation") -checkmessage("return 34 >> {}", "table value") -checkmessage("a = 24 // 0", "divide by zero") -checkmessage("a = 1 % 0", "'n%0'") - - --- passing light userdata instead of full userdata -_G.D = debug -checkmessage([[ - -- create light udata - local x = D.upvalueid(function () return debug end, 1) - D.setuservalue(x, {}) -]], "light userdata") -_G.D = nil - -do -- named objects (field '__name') - checkmessage("math.sin(io.input())", "(number expected, got FILE*)") - _G.XX = setmetatable({}, {__name = "My Type"}) - assert(string.find(tostring(XX), "^My Type")) - checkmessage("io.input(XX)", "(FILE* expected, got My Type)") - checkmessage("return XX + 1", "on a My Type value") - checkmessage("return ~io.stdin", "on a FILE* value") - checkmessage("return XX < XX", "two My Type values") - checkmessage("return {} < XX", "table with My Type") - checkmessage("return XX < io.stdin", "My Type with FILE*") - _G.XX = nil -end - --- global functions -checkmessage("(io.write or print){}", "io.write") -checkmessage("(collectgarbage or print){}", "collectgarbage") - --- errors in functions without debug info -do - local f = function (a) return a + 1 end - f = assert(load(string.dump(f, true))) - assert(f(3) == 4) - checkerr("^%?:%-1:", f, {}) - - -- code with a move to a local var ('OP_MOV A B' with A3+1, - {d = x and aaa[x or y]}} -]], "global 'aaa'") - -checkmessage([[ -local x,y = {},1 -if math.sin(1) == 0 then return 3 end -- return -x.a()]], "field 'a'") - -checkmessage([[ -prefix = nil -insert = nil -while 1 do - local a - if nil then break end - insert(prefix, a) -end]], "global 'insert'") - -checkmessage([[ -- tail call - return math.sin("a") -]], "'sin'") - -checkmessage([[collectgarbage("nooption")]], "invalid option") - -checkmessage([[x = print .. "a"]], "concatenate") -checkmessage([[x = "a" .. false]], "concatenate") -checkmessage([[x = {} .. 2]], "concatenate") - -checkmessage("getmetatable(io.stdin).__gc()", "no value") - -checkmessage([[ -local Var -local function main() - NoSuchName (function() Var=0 end) -end -main() -]], "global 'NoSuchName'") -print'+' - -a = {}; setmetatable(a, {__index = string}) -checkmessage("a:sub()", "bad self") -checkmessage("string.sub('a', {})", "#2") -checkmessage("('a'):sub{}", "#1") - -checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") -checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") - --- tests for errors in coroutines - -local function f (n) - local c = coroutine.create(f) - local a,b = coroutine.resume(c) - return b -end -assert(string.find(f(), "C stack overflow")) - -checkmessage("coroutine.yield()", "outside a coroutine") - -f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end) -checkerr("yield across", f) - - --- testing size of 'source' info; size of buffer for that info is --- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. -idsize = 60 - 1 -local function checksize (source) - -- syntax error - local _, msg = load("x", source) - msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':') - assert(msg:len() <= idsize) -end - -for i = 60 - 10, 60 + 10 do -- check border cases around 60 - checksize("@" .. string.rep("x", i)) -- file names - checksize(string.rep("x", i - 10)) -- string sources - checksize("=" .. string.rep("x", i)) -- exact sources -end - - --- testing line error - -local function lineerror (s, l) - local err,msg = pcall(load(s)) - local line = string.match(msg, ":(%d+):") - assert((line and line+0) == l) -end - -lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) -lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3) -lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4) -lineerror("function a.x.y ()\na=a+1\nend", 1) - -lineerror("a = \na\n+\n{}", 3) -lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6) -lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3) - -lineerror("a\n=\n-\n\nprint\n;", 3) - -lineerror([[ -a -( -23) -]], 1) - -lineerror([[ -local a = {x = 13} -a -. -x -( -23 -) -]], 2) - -lineerror([[ -local a = {x = 13} -a -. -x -( -23 + a -) -]], 6) - -local p = [[ - function g() f() end - function f(x) error('a', X) end -g() -]] -X=3;lineerror((p), 3) -X=0;lineerror((p), nil) -X=1;lineerror((p), 2) -X=2;lineerror((p), 1) - - -if not _soft then - -- several tests that exaust the Lua stack - collectgarbage() - print"testing stack overflow" - C = 0 - local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end - - local function checkstackmessage (m) - return (string.find(m, "^.-:%d+: stack overflow")) - end - -- repeated stack overflows (to check stack recovery) - assert(checkstackmessage(doit('y()'))) - print('+') - assert(checkstackmessage(doit('y()'))) - print('+') - assert(checkstackmessage(doit('y()'))) - print('+') - - - -- error lines in stack overflow - C = 0 - local l1 - local function g(x) - l1 = debug.getinfo(x, "l").currentline; y() - end - local _, stackmsg = xpcall(g, debug.traceback, 1) - print('+') - local stack = {} - for line in string.gmatch(stackmsg, "[^\n]*") do - local curr = string.match(line, ":(%d+):") - if curr then table.insert(stack, tonumber(curr)) end - end - local i=1 - while stack[i] ~= l1 do - assert(stack[i] == l) - i = i+1 - end - assert(i > 15) - - - -- error in error handling - local res, msg = xpcall(error, error) - assert(not res and type(msg) == 'string') - print('+') - - local function f (x) - if x==0 then error('a\n') - else - local aux = function () return f(x-1) end - local a,b = xpcall(aux, aux) - return a,b - end - end - f(3) - - local function loop (x,y,z) return 1 + loop(x, y, z) end - - local res, msg = xpcall(loop, function (m) - assert(string.find(m, "stack overflow")) - checkerr("error handling", loop) - assert(math.sin(0) == 0) - return 15 - end) - assert(msg == 15) - - local f = function () - for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end - end - checkerr("too many results", f) - -end - - -do - -- non string messages - local t = {} - local res, msg = pcall(function () error(t) end) - assert(not res and msg == t) - - res, msg = pcall(function () error(nil) end) - assert(not res and msg == nil) - - local function f() error{msg='x'} end - res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end) - assert(msg.msg == 'xy') - - -- 'assert' with extra arguments - res, msg = pcall(assert, false, "X", t) - assert(not res and msg == "X") - - -- 'assert' with no message - res, msg = pcall(function () assert(false) end) - local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$") - assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2) - - -- 'assert' with non-string messages - res, msg = pcall(assert, false, t) - assert(not res and msg == t) - - res, msg = pcall(assert, nil, nil) - assert(not res and msg == nil) - - -- 'assert' without arguments - res, msg = pcall(assert) - assert(not res and string.find(msg, "value expected")) -end - --- xpcall with arguments -a, b, c = xpcall(string.find, error, "alo", "al") -assert(a and b == 1 and c == 2) -a, b, c = xpcall(string.find, function (x) return {} end, true, "al") -assert(not a and type(b) == "table" and c == nil) - - -print("testing tokens in error messages") -checksyntax("syntax error", "", "error", 1) -checksyntax("1.000", "", "1.000", 1) -checksyntax("[[a]]", "", "[[a]]", 1) -checksyntax("'aa'", "", "'aa'", 1) -checksyntax("while << do end", "", "<<", 1) -checksyntax("for >> do end", "", ">>", 1) - --- test invalid non-printable char in a chunk -checksyntax("a\1a = 1", "", "<\\1>", 1) - --- test 255 as first char in a chunk -checksyntax("\255a = 1", "", "<\\255>", 1) - -doit('I = load("a=9+"); a=3') -assert(a==3 and I == nil) -print('+') - -lim = 1000 -if _soft then lim = 100 end -for i=1,lim do - doit('a = ') - doit('a = 4+nil') -end - - --- testing syntax limits - -local maxClevel = 200 -- LUAI_MAXCCALLS (in llimits.h) - -local function testrep (init, rep, close, repc) - local s = init .. string.rep(rep, maxClevel - 10) .. close .. - string.rep(repc, maxClevel - 10) - assert(load(s)) -- 190 levels is OK - s = init .. string.rep(rep, maxClevel + 1) - checkmessage(s, "too many C levels") -end - -testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment -testrep("local a; a=", "{", "0", "}") -testrep("local a; a=", "(", "2", ")") -testrep("local a; ", "a(", "2", ")") -testrep("", "do ", "", " end") -testrep("", "while a do ", "", " end") -testrep("local a; ", "if a then else ", "", " end") -testrep("", "function foo () ", "", " end") -testrep("local a; a=", "a..", "a", "") -testrep("local a; a=", "a^", "a", "") - -checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers") - - --- testing other limits - --- upvalues -local lim = 127 -local s = "local function fooA ()\n local " -for j = 1,lim do - s = s.."a"..j..", " -end -s = s.."b,c\n" -s = s.."local function fooB ()\n local " -for j = 1,lim do - s = s.."b"..j..", " -end -s = s.."b\n" -s = s.."function fooC () return b+c" -local c = 1+2 -for j = 1,lim do - s = s.."+a"..j.."+b"..j - c = c + 2 -end -s = s.."\nend end end" -local a,b = load(s) -assert(c > 255 and string.find(b, "too many upvalues") and - string.find(b, "line 5")) - --- local variables -s = "\nfunction foo ()\n local " -for j = 1,300 do - s = s.."a"..j..", " -end -s = s.."b\n" -local a,b = load(s) -assert(string.find(b, "line 2") and string.find(b, "too many local variables")) - -mt.__index = oldmm - -print('OK') diff --git a/tofix/lua-5.3.4-tests/events.lua b/tofix/lua-5.3.4-tests/events.lua deleted file mode 100644 index 9136f99..0000000 --- a/tofix/lua-5.3.4-tests/events.lua +++ /dev/null @@ -1,456 +0,0 @@ --- $Id: events.lua,v 1.45 2016/12/21 19:23:02 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing metatables') - -local debug = require'debug' - -X = 20; B = 30 - -_ENV = setmetatable({}, {__index=_G}) - -collectgarbage() - -X = X+10 -assert(X == 30 and _G.X == 20) -B = false -assert(B == false) -B = nil -assert(B == 30) - -assert(getmetatable{} == nil) -assert(getmetatable(4) == nil) -assert(getmetatable(nil) == nil) -a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu", - __tostring=function(x) return x.name end}) -assert(getmetatable(a) == "xuxu") -assert(tostring(a) == "NAME") --- cannot change a protected metatable -assert(pcall(setmetatable, a, {}) == false) -a.name = "gororoba" -assert(tostring(a) == "gororoba") - -local a, t = {10,20,30; x="10", y="20"}, {} -assert(setmetatable(a,t) == a) -assert(getmetatable(a) == t) -assert(setmetatable(a,nil) == a) -assert(getmetatable(a) == nil) -assert(setmetatable(a,t) == a) - - -function f (t, i, e) - assert(not e) - local p = rawget(t, "parent") - return (p and p[i]+3), "dummy return" -end - -t.__index = f - -a.parent = {z=25, x=12, [4] = 24} -assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10") - -collectgarbage() - -a = setmetatable({}, t) -function f(t, i, v) rawset(t, i, v-3) end -setmetatable(t, t) -- causes a bug in 5.1 ! -t.__newindex = f -a[1] = 30; a.x = "101"; a[5] = 200 -assert(a[1] == 27 and a.x == 98 and a[5] == 197) - -do -- bug in Lua 5.3.2 - local mt = {} - mt.__newindex = mt - local t = setmetatable({}, mt) - t[1] = 10 -- will segfault on some machines - assert(mt[1] == 10) -end - - -local c = {} -a = setmetatable({}, t) -t.__newindex = c -a[1] = 10; a[2] = 20; a[3] = 90 -assert(c[1] == 10 and c[2] == 20 and c[3] == 90) - - -do - local a; - a = setmetatable({}, {__index = setmetatable({}, - {__index = setmetatable({}, - {__index = function (_,n) return a[n-3]+4, "lixo" end})})}) - a[0] = 20 - for i=0,10 do - assert(a[i*3] == 20 + i*4) - end -end - - -do -- newindex - local foi - local a = {} - for i=1,10 do a[i] = 0; a['a'..i] = 0; end - setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end}) - foi = false; a[1]=0; assert(not foi) - foi = false; a['a1']=0; assert(not foi) - foi = false; a['a11']=0; assert(foi) - foi = false; a[11]=0; assert(foi) - foi = false; a[1]=nil; assert(not foi) - foi = false; a[1]=nil; assert(foi) -end - - -setmetatable(t, nil) -function f (t, ...) return t, {...} end -t.__call = f - -do - local x,y = a(table.unpack{'a', 1}) - assert(x==a and y[1]=='a' and y[2]==1 and y[3]==nil) - x,y = a() - assert(x==a and y[1]==nil) -end - - -local b = setmetatable({}, t) -setmetatable(b,t) - -function f(op) - return function (...) cap = {[0] = op, ...} ; return (...) end -end -t.__add = f("add") -t.__sub = f("sub") -t.__mul = f("mul") -t.__div = f("div") -t.__idiv = f("idiv") -t.__mod = f("mod") -t.__unm = f("unm") -t.__pow = f("pow") -t.__len = f("len") -t.__band = f("band") -t.__bor = f("bor") -t.__bxor = f("bxor") -t.__shl = f("shl") -t.__shr = f("shr") -t.__bnot = f("bnot") - -assert(b+5 == b) -assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==nil) -assert(b+'5' == b) -assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==nil) -assert(5+b == 5) -assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==nil) -assert('5'+b == '5') -assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==nil) -b=b-3; assert(getmetatable(b) == t) -assert(5-a == 5) -assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==nil) -assert('5'-a == '5') -assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==nil) -assert(a*a == a) -assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==nil) -assert(a/0 == a) -assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==nil) -assert(a%2 == a) -assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==nil) -assert(a // (1/0) == a) -assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==nil) -assert(a & "hi" == a) -assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==nil) -assert(a | "hi" == a) -assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==nil) -assert("hi" ~ a == "hi") -assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==nil) -assert(-a == a) -assert(cap[0] == "unm" and cap[1] == a) -assert(a^4 == a) -assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==nil) -assert(a^'4' == a) -assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==nil) -assert(4^a == 4) -assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==nil) -assert('4'^a == '4') -assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==nil) -assert(#a == a) -assert(cap[0] == "len" and cap[1] == a) -assert(~a == a) -assert(cap[0] == "bnot" and cap[1] == a) -assert(a << 3 == a) -assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3) -assert(1.5 >> a == 1.5) -assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a) - - --- test for rawlen -t = setmetatable({1,2,3}, {__len = function () return 10 end}) -assert(#t == 10 and rawlen(t) == 3) -assert(rawlen"abc" == 3) -assert(not pcall(rawlen, io.stdin)) -assert(not pcall(rawlen, 34)) -assert(not pcall(rawlen)) - --- rawlen for long strings -assert(rawlen(string.rep('a', 1000)) == 1000) - - -t = {} -t.__lt = function (a,b,c) - collectgarbage() - assert(c == nil) - if type(a) == 'table' then a = a.x end - if type(b) == 'table' then b = b.x end - return aOp(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1))) - assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a'))) - assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1))) - assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) - assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) - assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) -end - -test() - -t.__le = function (a,b,c) - assert(c == nil) - if type(a) == 'table' then a = a.x end - if type(b) == 'table' then b = b.x end - return a<=b, "dummy" -end - -test() -- retest comparisons, now using both `lt' and `le' - - --- test `partial order' - -local function rawSet(x) - local y = {} - for _,k in pairs(x) do y[k] = 1 end - return y -end - -local function Set(x) - return setmetatable(rawSet(x), t) -end - -t.__lt = function (a,b) - for k in pairs(a) do - if not b[k] then return false end - b[k] = nil - end - return next(b) ~= nil -end - -t.__le = nil - -assert(Set{1,2,3} < Set{1,2,3,4}) -assert(not(Set{1,2,3,4} < Set{1,2,3,4})) -assert((Set{1,2,3,4} <= Set{1,2,3,4})) -assert((Set{1,2,3,4} >= Set{1,2,3,4})) -assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-) - -t.__le = function (a,b) - for k in pairs(a) do - if not b[k] then return false end - end - return true -end - -assert(not (Set{1,3} <= Set{3,5})) -- now its OK! -assert(not(Set{1,3} <= Set{3,5})) -assert(not(Set{1,3} >= Set{3,5})) - -t.__eq = function (a,b) - for k in pairs(a) do - if not b[k] then return false end - b[k] = nil - end - return next(b) == nil -end - -local s = Set{1,3,5} -assert(s == Set{3,5,1}) -assert(not rawequal(s, Set{3,5,1})) -assert(rawequal(s, s)) -assert(Set{1,3,5,1} == rawSet{3,5,1}) -assert(rawSet{1,3,5,1} == Set{3,5,1}) -assert(Set{1,3,5} ~= Set{3,5,1,6}) - --- '__eq' is not used for table accesses -t[Set{1,3,5}] = 1 -assert(t[Set{1,3,5}] == nil) - - -if not T then - (Message or print)('\n >>> testC not active: skipping tests for \z -userdata equality <<<\n') -else - local u1 = T.newuserdata(0) - local u2 = T.newuserdata(0) - local u3 = T.newuserdata(0) - assert(u1 ~= u2 and u1 ~= u3) - debug.setuservalue(u1, 1); - debug.setuservalue(u2, 2); - debug.setuservalue(u3, 1); - debug.setmetatable(u1, {__eq = function (a, b) - return debug.getuservalue(a) == debug.getuservalue(b) - end}) - debug.setmetatable(u2, {__eq = function (a, b) - return true - end}) - assert(u1 == u3 and u3 == u1 and u1 ~= u2) - assert(u2 == u1 and u2 == u3 and u3 == u2) - assert(u2 ~= {}) -- different types cannot be equal -end - - -t.__concat = function (a,b,c) - assert(c == nil) - if type(a) == 'table' then a = a.val end - if type(b) == 'table' then b = b.val end - if A then return a..b - else - return setmetatable({val=a..b}, t) - end -end - -c = {val="c"}; setmetatable(c, t) -d = {val="d"}; setmetatable(d, t) - -A = true -assert(c..d == 'cd') -assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g") - -A = false -assert((c..d..c..d).val == 'cdcd') -x = c..d -assert(getmetatable(x) == t and x.val == 'cd') -x = 0 .."a".."b"..c..d.."e".."f".."g" -assert(x.val == "0abcdefg") - - --- concat metamethod x numbers (bug in 5.1.1) -c = {} -local x -setmetatable(c, {__concat = function (a,b) - assert(type(a) == "number" and b == c or type(b) == "number" and a == c) - return c -end}) -assert(c..5 == c and 5 .. c == c) -assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c) - - --- test comparison compatibilities -local t1, t2, c, d -t1 = {}; c = {}; setmetatable(c, t1) -d = {} -t1.__eq = function () return true end -t1.__lt = function () return true end -setmetatable(d, t1) -assert(c == d and c < d and not(d <= c)) -t2 = {} -t2.__eq = t1.__eq -t2.__lt = t1.__lt -setmetatable(d, t2) -assert(c == d and c < d and not(d <= c)) - - - --- test for several levels of calls -local i -local tt = { - __call = function (t, ...) - i = i+1 - if t.f then return t.f(...) - else return {...} - end - end -} - -local a = setmetatable({}, tt) -local b = setmetatable({f=a}, tt) -local c = setmetatable({f=b}, tt) - -i = 0 -x = c(3,4,5) -assert(i == 3 and x[1] == 3 and x[3] == 5) - - -assert(_G.X == 20) - -print'+' - -local _g = _G -_ENV = setmetatable({}, {__index=function (_,k) return _g[k] end}) - - -a = {} -rawset(a, "x", 1, 2, 3) -assert(a.x == 1 and rawget(a, "x", 3) == 1) - -print '+' - --- testing metatables for basic types -mt = {__index = function (a,b) return a+b end, - __len = function (x) return math.floor(x) end} -debug.setmetatable(10, mt) -assert(getmetatable(-2) == mt) -assert((10)[3] == 13) -assert((10)["3"] == 13) -assert(#3.45 == 3) -debug.setmetatable(23, nil) -assert(getmetatable(-2) == nil) - -debug.setmetatable(true, mt) -assert(getmetatable(false) == mt) -mt.__index = function (a,b) return a or b end -assert((true)[false] == true) -assert((false)[false] == false) -debug.setmetatable(false, nil) -assert(getmetatable(true) == nil) - -debug.setmetatable(nil, mt) -assert(getmetatable(nil) == mt) -mt.__add = function (a,b) return (a or 0) + (b or 0) end -assert(10 + nil == 10) -assert(nil + 23 == 23) -assert(nil + nil == 0) -debug.setmetatable(nil, nil) -assert(getmetatable(nil) == nil) - -debug.setmetatable(nil, {}) - - --- loops in delegation -a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a -assert(not pcall(function (a,b) return a[b] end, a, 10)) -assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true)) - --- bug in 5.1 -T, K, V = nil -grandparent = {} -grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end - -parent = {} -parent.__newindex = parent -setmetatable(parent, grandparent) - -child = setmetatable({}, parent) -child.foo = 10 --> CRASH (on some machines) -assert(T == parent and K == "foo" and V == 10) - -print 'OK' - -return 12 - - diff --git a/tofix/lua-5.3.4-tests/files.lua b/tofix/lua-5.3.4-tests/files.lua deleted file mode 100644 index 3cfe12d..0000000 --- a/tofix/lua-5.3.4-tests/files.lua +++ /dev/null @@ -1,793 +0,0 @@ --- $Id: files.lua,v 1.95 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -local debug = require "debug" - -local maxint = math.maxinteger - -assert(type(os.getenv"PATH") == "string") - -assert(io.input(io.stdin) == io.stdin) -assert(not pcall(io.input, "non-existent-file")) -assert(io.output(io.stdout) == io.stdout) - - -local function testerr (msg, f, ...) - local stat, err = pcall(f, ...) - return (not stat and string.find(err, msg, 1, true)) -end - - -local function checkerr (msg, f, ...) - assert(testerr(msg, f, ...)) -end - - --- cannot close standard files -assert(not io.close(io.stdin) and - not io.stdout:close() and - not io.stderr:close()) - - -assert(type(io.input()) == "userdata" and io.type(io.output()) == "file") -assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file") -assert(not io.type(8)) -local a = {}; setmetatable(a, {}) -assert(not io.type(a)) - -assert(getmetatable(io.input()).__name == "FILE*") - -local a,b,c = io.open('xuxu_nao_existe') -assert(not a and type(b) == "string" and type(c) == "number") - -a,b,c = io.open('/a/b/c/d', 'w') -assert(not a and type(b) == "string" and type(c) == "number") - -local file = os.tmpname() -local f, msg = io.open(file, "w") -if not f then - (Message or print)("'os.tmpname' file cannot be open; skipping file tests") - -else --{ most tests here need tmpname -f:close() - -print('testing i/o') - -local otherfile = os.tmpname() - -checkerr("invalid mode", io.open, file, "rw") -checkerr("invalid mode", io.open, file, "rb+") -checkerr("invalid mode", io.open, file, "r+bk") -checkerr("invalid mode", io.open, file, "") -checkerr("invalid mode", io.open, file, "+") -checkerr("invalid mode", io.open, file, "b") -assert(io.open(file, "r+b")):close() -assert(io.open(file, "r+")):close() -assert(io.open(file, "rb")):close() - -assert(os.setlocale('C', 'all')) - -io.input(io.stdin); io.output(io.stdout); - -os.remove(file) -assert(not loadfile(file)) -checkerr("", dofile, file) -assert(not io.open(file)) -io.output(file) -assert(io.output() ~= io.stdout) - -if not _port then -- invalid seek - local status, msg, code = io.stdin:seek("set", 1000) - assert(not status and type(msg) == "string" and type(code) == "number") -end - -assert(io.output():seek() == 0) -assert(io.write("alo alo"):seek() == string.len("alo alo")) -assert(io.output():seek("cur", -3) == string.len("alo alo")-3) -assert(io.write("joao")) -assert(io.output():seek("end") == string.len("alo joao")) - -assert(io.output():seek("set") == 0) - -assert(io.write('"lo"', "{a}\n", "second line\n", "third line \n")) -assert(io.write('fourth_line')) -io.output(io.stdout) -collectgarbage() -- file should be closed by GC -assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) -print('+') - --- test GC for files -collectgarbage() -for i=1,120 do - for i=1,5 do - io.input(file) - assert(io.open(file, 'r')) - io.lines(file) - end - collectgarbage() -end - -io.input():close() -io.close() - -assert(os.rename(file, otherfile)) -assert(not os.rename(file, otherfile)) - -io.output(io.open(otherfile, "ab")) -assert(io.write("\n\n\t\t ", 3450, "\n")); -io.close() - --- test writing/reading numbers -f = assert(io.open(file, "w")) -f:write(maxint, '\n') -f:write(string.format("0X%x\n", maxint)) -f:write("0xABCp-3", '\n') -f:write(0, '\n') -f:write(-maxint, '\n') -f:write(string.format("0x%X\n", -maxint)) -f:write("-0xABCp-3", '\n') -assert(f:close()) -f = assert(io.open(file, "r")) -assert(f:read("n") == maxint) -assert(f:read("n") == maxint) -assert(f:read("n") == 0xABCp-3) -assert(f:read("n") == 0) -assert(f:read("*n") == -maxint) -- test old format (with '*') -assert(f:read("n") == -maxint) -assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') -assert(f:close()) -assert(os.remove(file)) - --- test yielding during 'dofile' -f = assert(io.open(file, "w")) -f:write[[ -local x, z = coroutine.yield(10) -local y = coroutine.yield(20) -return x + y * z -]] -assert(f:close()) -f = coroutine.wrap(dofile) -assert(f(file) == 10) -print(f(100, 101) == 20) -assert(f(200) == 100 + 200 * 101) -assert(os.remove(file)) - - -f = assert(io.open(file, "w")) --- test number termination -f:write[[ --12.3- -0xffff+ .3|5.E-3X +234e+13E 0xDEADBEEFDEADBEEFx -0x1.13Ap+3e -]] --- very long number -f:write("1234"); for i = 1, 1000 do f:write("0") end; f:write("\n") --- invalid sequences (must read and discard valid prefixes) -f:write[[ -.e+ 0.e; --; 0xX; -]] -assert(f:close()) -f = assert(io.open(file, "r")) -assert(f:read("n") == -12.3); assert(f:read(1) == "-") -assert(f:read("n") == -0xffff); assert(f:read(2) == "+ ") -assert(f:read("n") == 0.3); assert(f:read(1) == "|") -assert(f:read("n") == 5e-3); assert(f:read(1) == "X") -assert(f:read("n") == 234e13); assert(f:read(1) == "E") -assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") -assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") - -do -- attempt to read too long number - assert(f:read("n") == nil) -- fails - local s = f:read("L") -- read rest of line - assert(string.find(s, "^00*\n$")) -- lots of 0's left -end - -assert(not f:read("n")); assert(f:read(2) == "e+") -assert(not f:read("n")); assert(f:read(1) == ";") -assert(not f:read("n")); assert(f:read(2) == "-;") -assert(not f:read("n")); assert(f:read(1) == "X") -assert(not f:read("n")); assert(f:read(1) == ";") -assert(not f:read("n")); assert(not f:read(0)) -- end of file -assert(f:close()) -assert(os.remove(file)) - - --- test line generators -assert(not pcall(io.lines, "non-existent-file")) -assert(os.rename(otherfile, file)) -io.output(otherfile) -local n = 0 -local f = io.lines(file) -while f() do n = n + 1 end; -assert(n == 6) -- number of lines in the file -checkerr("file is already closed", f) -checkerr("file is already closed", f) --- copy from file to otherfile -n = 0 -for l in io.lines(file) do io.write(l, "\n"); n = n + 1 end -io.close() -assert(n == 6) --- copy from otherfile back to file -local f = assert(io.open(otherfile)) -assert(io.type(f) == "file") -io.output(file) -assert(not io.output():read()) -n = 0 -for l in f:lines() do io.write(l, "\n"); n = n + 1 end -assert(tostring(f):sub(1, 5) == "file ") -assert(f:close()); io.close() -assert(n == 6) -checkerr("closed file", io.close, f) -assert(tostring(f) == "file (closed)") -assert(io.type(f) == "closed file") -io.input(file) -f = io.open(otherfile):lines() -n = 0 -for l in io.lines() do assert(l == f()); n = n + 1 end -f = nil; collectgarbage() -assert(n == 6) -assert(os.remove(otherfile)) - -do -- bug in 5.3.1 - io.output(otherfile) - io.write(string.rep("a", 300), "\n") - io.close() - local t ={}; for i = 1, 250 do t[i] = 1 end - t = {io.lines(otherfile, table.unpack(t))()} - -- everything ok here - assert(#t == 250 and t[1] == 'a' and t[#t] == 'a') - t[#t + 1] = 1 -- one too many - checkerr("too many arguments", io.lines, otherfile, table.unpack(t)) - collectgarbage() -- ensure 'otherfile' is closed - assert(os.remove(otherfile)) -end - -io.input(file) -do -- test error returns - local a,b,c = io.input():write("xuxu") - assert(not a and type(b) == "string" and type(c) == "number") -end -checkerr("invalid format", io.read, "x") -assert(io.read(0) == "") -- not eof -assert(io.read(5, 'l') == '"lo"') -assert(io.read(0) == "") -assert(io.read() == "second line") -local x = io.input():seek() -assert(io.read() == "third line ") -assert(io.input():seek("set", x)) -assert(io.read('L') == "third line \n") -assert(io.read(1) == "") -assert(io.read(string.len"fourth_line") == "fourth_line") -assert(io.input():seek("cur", -string.len"fourth_line")) -assert(io.read() == "fourth_line") -assert(io.read() == "") -- empty line -assert(io.read('n') == 3450) -assert(io.read(1) == '\n') -assert(io.read(0) == nil) -- end of file -assert(io.read(1) == nil) -- end of file -assert(io.read(30000) == nil) -- end of file -assert(({io.read(1)})[2] == nil) -assert(io.read() == nil) -- end of file -assert(({io.read()})[2] == nil) -assert(io.read('n') == nil) -- end of file -assert(({io.read('n')})[2] == nil) -assert(io.read('a') == '') -- end of file (OK for 'a') -assert(io.read('a') == '') -- end of file (OK for 'a') -collectgarbage() -print('+') -io.close(io.input()) -checkerr(" input file is closed", io.read) - -assert(os.remove(file)) - -local t = '0123456789' -for i=1,10 do t = t..t; end -assert(string.len(t) == 10*2^10) - -io.output(file) -io.write("alo"):write("\n") -io.close() -checkerr(" output file is closed", io.write) -local f = io.open(file, "a+b") -io.output(f) -collectgarbage() - -assert(io.write(' ' .. t .. ' ')) -assert(io.write(';', 'end of file\n')) -f:flush(); io.flush() -f:close() -print('+') - -io.input(file) -assert(io.read() == "alo") -assert(io.read(1) == ' ') -assert(io.read(string.len(t)) == t) -assert(io.read(1) == ' ') -assert(io.read(0)) -assert(io.read('a') == ';end of file\n') -assert(io.read(0) == nil) -assert(io.close(io.input())) - - --- test errors in read/write -do - local function ismsg (m) - -- error message is not a code number - return (type(m) == "string" and tonumber(m) == nil) - end - - -- read - local f = io.open(file, "w") - local r, m, c = f:read() - assert(not r and ismsg(m) and type(c) == "number") - assert(f:close()) - -- write - f = io.open(file, "r") - r, m, c = f:write("whatever") - assert(not r and ismsg(m) and type(c) == "number") - assert(f:close()) - -- lines - f = io.open(file, "w") - r, m = pcall(f:lines()) - assert(r == false and ismsg(m)) - assert(f:close()) -end - -assert(os.remove(file)) - --- test for L format -io.output(file); io.write"\n\nline\nother":close() -io.input(file) -assert(io.read"L" == "\n") -assert(io.read"L" == "\n") -assert(io.read"L" == "line\n") -assert(io.read"L" == "other") -assert(io.read"L" == nil) -io.input():close() - -local f = assert(io.open(file)) -local s = "" -for l in f:lines("L") do s = s .. l end -assert(s == "\n\nline\nother") -f:close() - -io.input(file) -s = "" -for l in io.lines(nil, "L") do s = s .. l end -assert(s == "\n\nline\nother") -io.input():close() - -s = "" -for l in io.lines(file, "L") do s = s .. l end -assert(s == "\n\nline\nother") - -s = "" -for l in io.lines(file, "l") do s = s .. l end -assert(s == "lineother") - -io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close() -local t = {} -load(io.lines(file, "L"), nil, nil, t)() -assert(t.a == -((10 + 34) * 2)) - - --- test for multipe arguments in 'lines' -io.output(file); io.write"0123456789\n":close() -for a,b in io.lines(file, 1, 1) do - if a == "\n" then assert(b == nil) - else assert(tonumber(a) == tonumber(b) - 1) - end -end - -for a,b,c in io.lines(file, 1, 2, "a") do - assert(a == "0" and b == "12" and c == "3456789\n") -end - -for a,b,c in io.lines(file, "a", 0, 1) do - if a == "" then break end - assert(a == "0123456789\n" and b == nil and c == nil) -end -collectgarbage() -- to close file in previous iteration - -io.output(file); io.write"00\n10\n20\n30\n40\n":close() -for a, b in io.lines(file, "n", "n") do - if a == 40 then assert(b == nil) - else assert(a == b - 10) - end -end - - --- test load x lines -io.output(file); -io.write[[ -local y -= X -X = -X * -2 + -X; -X = -X -- y; -]]:close() -_G.X = 1 -assert(not load(io.lines(file))) -collectgarbage() -- to close file in previous iteration -load(io.lines(file, "L"))() -assert(_G.X == 2) -load(io.lines(file, 1))() -assert(_G.X == 4) -load(io.lines(file, 3))() -assert(_G.X == 8) - -print('+') - -local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" -io.output(file) -assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) -io.close() -assert(loadfile(file))() -assert(x1 == x2) -print('+') -assert(os.remove(file)) -assert(not os.remove(file)) -assert(not os.remove(otherfile)) - --- testing loadfile -local function testloadfile (s, expres) - io.output(file) - if s then io.write(s) end - io.close() - local res = assert(loadfile(file))() - assert(os.remove(file)) - assert(res == expres) -end - --- loading empty file -testloadfile(nil, nil) - --- loading file with initial comment without end of line -testloadfile("# a non-ending comment", nil) - - --- checking Unicode BOM in files -testloadfile("\xEF\xBB\xBF# some comment\nreturn 234", 234) -testloadfile("\xEF\xBB\xBFreturn 239", 239) -testloadfile("\xEF\xBB\xBF", nil) -- empty file with a BOM - - --- checking line numbers in files with initial comments -testloadfile("# a comment\nreturn require'debug'.getinfo(1).currentline", 2) - - --- loading binary file -io.output(io.open(file, "wb")) -assert(io.write(string.dump(function () return 10, '\0alo\255', 'hi' end))) -io.close() -a, b, c = assert(loadfile(file))() -assert(a == 10 and b == "\0alo\255" and c == "hi") -assert(os.remove(file)) - --- bug in 5.2.1 -do - io.output(io.open(file, "wb")) - -- save function with no upvalues - assert(io.write(string.dump(function () return 1 end))) - io.close() - f = assert(loadfile(file, "b", {})) - assert(type(f) == "function" and f() == 1) - assert(os.remove(file)) -end - --- loading binary file with initial comment -io.output(io.open(file, "wb")) -assert(io.write("#this is a comment for a binary file\0\n", - string.dump(function () return 20, '\0\0\0' end))) -io.close() -a, b, c = assert(loadfile(file))() -assert(a == 20 and b == "\0\0\0" and c == nil) -assert(os.remove(file)) - - --- 'loadfile' with 'env' -do - local f = io.open(file, 'w') - f:write[[ - if (...) then a = 15; return b, c, d - else return _ENV - end - ]] - f:close() - local t = {b = 12, c = "xuxu", d = print} - local f = assert(loadfile(file, 't', t)) - local b, c, d = f(1) - assert(t.a == 15 and b == 12 and c == t.c and d == print) - assert(f() == t) - f = assert(loadfile(file, 't', nil)) - assert(f() == nil) - f = assert(loadfile(file)) - assert(f() == _G) - assert(os.remove(file)) -end - - --- 'loadfile' x modes -do - io.open(file, 'w'):write("return 10"):close() - local s, m = loadfile(file, 'b') - assert(not s and string.find(m, "a text chunk")) - io.open(file, 'w'):write("\27 return 10"):close() - local s, m = loadfile(file, 't') - assert(not s and string.find(m, "a binary chunk")) - assert(os.remove(file)) -end - - -io.output(file) -assert(io.write("qualquer coisa\n")) -assert(io.write("mais qualquer coisa")) -io.close() -assert(io.output(assert(io.open(otherfile, 'wb'))) - :write("outra coisa\0\1\3\0\0\0\0\255\0") - :close()) - -local filehandle = assert(io.open(file, 'r+')) -local otherfilehandle = assert(io.open(otherfile, 'rb')) -assert(filehandle ~= otherfilehandle) -assert(type(filehandle) == "userdata") -assert(filehandle:read('l') == "qualquer coisa") -io.input(otherfilehandle) -assert(io.read(string.len"outra coisa") == "outra coisa") -assert(filehandle:read('l') == "mais qualquer coisa") -filehandle:close(); -assert(type(filehandle) == "userdata") -io.input(otherfilehandle) -assert(io.read(4) == "\0\1\3\0") -assert(io.read(3) == "\0\0\0") -assert(io.read(0) == "") -- 255 is not eof -assert(io.read(1) == "\255") -assert(io.read('a') == "\0") -assert(not io.read(0)) -assert(otherfilehandle == io.input()) -otherfilehandle:close() -assert(os.remove(file)) -assert(os.remove(otherfile)) -collectgarbage() - -io.output(file) - :write[[ - 123.4 -56e-2 not a number -second line -third line - -and the rest of the file -]] - :close() -io.input(file) -local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) -assert(io.close(io.input())) -assert(_ == ' ' and __ == nil) -assert(type(a) == 'number' and a==123.4 and b==-56e-2) -assert(d=='second line' and e=='third line') -assert(h==[[ - -and the rest of the file -]]) -assert(os.remove(file)) -collectgarbage() - --- testing buffers -do - local f = assert(io.open(file, "w")) - local fr = assert(io.open(file, "r")) - assert(f:setvbuf("full", 2000)) - f:write("x") - assert(fr:read("all") == "") -- full buffer; output not written yet - f:close() - fr:seek("set") - assert(fr:read("all") == "x") -- `close' flushes it - f = assert(io.open(file), "w") - assert(f:setvbuf("no")) - f:write("x") - fr:seek("set") - assert(fr:read("all") == "x") -- no buffer; output is ready - f:close() - f = assert(io.open(file, "a")) - assert(f:setvbuf("line")) - f:write("x") - fr:seek("set", 1) - assert(fr:read("all") == "") -- line buffer; no output without `\n' - f:write("a\n"):seek("set", 1) - assert(fr:read("all") == "xa\n") -- now we have a whole line - f:close(); fr:close() - assert(os.remove(file)) -end - - -if not _soft then - print("testing large files (> BUFSIZ)") - io.output(file) - for i=1,5001 do io.write('0123456789123') end - io.write('\n12346'):close() - io.input(file) - local x = io.read('a') - io.input():seek('set', 0) - local y = io.read(30001)..io.read(1005)..io.read(0).. - io.read(1)..io.read(100003) - assert(x == y and string.len(x) == 5001*13 + 6) - io.input():seek('set', 0) - y = io.read() -- huge line - assert(x == y..'\n'..io.read()) - assert(io.read() == nil) - io.close(io.input()) - assert(os.remove(file)) - x = nil; y = nil -end - -if not _port then - local progname - do -- get name of running executable - local arg = arg or _ARG - local i = 0 - while arg[i] do i = i - 1 end - progname = '"' .. arg[i + 1] .. '"' - end - print("testing popen/pclose and execute") - local tests = { - -- command, what, code - {"ls > /dev/null", "ok"}, - {"not-to-be-found-command", "exit"}, - {"exit 3", "exit", 3}, - {"exit 129", "exit", 129}, - {"kill -s HUP $$", "signal", 1}, - {"kill -s KILL $$", "signal", 9}, - {"sh -c 'kill -s HUP $$'", "exit"}, - {progname .. ' -e " "', "ok"}, - {progname .. ' -e "os.exit(0, true)"', "ok"}, - {progname .. ' -e "os.exit(20, true)"', "exit", 20}, - } - print("\n(some error messages are expected now)") - for _, v in ipairs(tests) do - local x, y, z = io.popen(v[1]):close() - local x1, y1, z1 = os.execute(v[1]) - assert(x == x1 and y == y1 and z == z1) - if v[2] == "ok" then - assert(x and y == 'exit' and z == 0) - else - assert(not x and y == v[2]) -- correct status and 'what' - -- correct code if known (but always different from 0) - assert((v[3] == nil and z > 0) or v[3] == z) - end - end -end - - --- testing tmpfile -f = io.tmpfile() -assert(io.type(f) == "file") -f:write("alo") -f:seek("set") -assert(f:read"a" == "alo") - -end --} - -print'+' - -print("testing date/time") - -assert(os.date("") == "") -assert(os.date("!") == "") -assert(os.date("\0\0") == "\0\0") -assert(os.date("!\0\0") == "\0\0") -local x = string.rep("a", 10000) -assert(os.date(x) == x) -local t = os.time() -D = os.date("*t", t) -assert(os.date(string.rep("%d", 1000), t) == - string.rep(os.date("%d", t), 1000)) -assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) - -local t = os.time() -D = os.date("*t", t) -load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and - D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() - -checkerr("invalid conversion specifier", os.date, "%") -checkerr("invalid conversion specifier", os.date, "%9") -checkerr("invalid conversion specifier", os.date, "%") -checkerr("invalid conversion specifier", os.date, "%O") -checkerr("invalid conversion specifier", os.date, "%E") -checkerr("invalid conversion specifier", os.date, "%Ea") - -checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour='x'}) -checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5}) - -checkerr("missing", os.time, {hour = 12}) -- missing date - -if not _port then - -- test Posix-specific modifiers - assert(type(os.date("%Ex")) == 'string') - assert(type(os.date("%Oy")) == 'string') - - - -- test out-of-range dates (at least for Unix) - if maxint >= 2^62 then -- cannot do these tests in Small Lua - -- no arith overflows - checkerr("out-of-bound", os.time, {year = -maxint, month = 1, day = 1}) - if string.packsize("i") == 4 then -- 4-byte ints - if testerr("out-of-bound", os.date, "%Y", 2^40) then - -- time_t has 4 bytes and therefore cannot represent year 4000 - print(" 4-byte time_t") - checkerr("cannot be represented", os.time, {year=4000, month=1, day=1}) - else - -- time_t has 8 bytes; an int year cannot represent a huge time - print(" 8-byte time_t") - checkerr("cannot be represented", os.date, "%Y", 2^60) - -- it should have no problems with year 4000 - assert(tonumber(os.time{year=4000, month=1, day=1})) - end - else -- 8-byte ints - -- assume time_t has 8 bytes too - print(" 8-byte time_t") - assert(tonumber(os.date("%Y", 2^60))) - -- but still cannot represent a huge year - checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1}) - end - end -end - - -D = os.date("!*t", t) -load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and - D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() - -do - local D = os.date("*t") - local t = os.time(D) - assert(type(D.isdst) == 'boolean') - D.isdst = nil - local t1 = os.time(D) - assert(t == t1) -- if isdst is absent uses correct default -end - -t = os.time(D) -D.year = D.year-1; -local t1 = os.time(D) --- allow for leap years -assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2) - --- should not take more than 1 second to execute these two lines -t = os.time() -t1 = os.time(os.date("*t")) -local diff = os.difftime(t1,t) -assert(0 <= diff and diff <= 1) -diff = os.difftime(t,t1) -assert(-1 <= diff and diff <= 0) - -local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12} -local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19} -assert(os.difftime(t1,t2) == 60*2-19) - --- since 5.3.3, 'os.time' normalizes table fields -t1 = {year = 2005, month = 1, day = 1, hour = 1, min = 0, sec = -3602} -os.time(t1) -assert(t1.day == 31 and t1.month == 12 and t1.year == 2004 and - t1.hour == 23 and t1.min == 59 and t1.sec == 58 and - t1.yday == 366) - -io.output(io.stdout) -local t = os.date('%d %m %Y %H %M %S') -local d, m, a, h, min, s = string.match(t, - "(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)") -d = tonumber(d) -m = tonumber(m) -a = tonumber(a) -h = tonumber(h) -min = tonumber(min) -s = tonumber(s) -io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a)) -io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s)) -io.write(string.format('%s\n', _VERSION)) - - diff --git a/tofix/lua-5.3.4-tests/gc.lua b/tofix/lua-5.3.4-tests/gc.lua deleted file mode 100644 index 93fd6d6..0000000 --- a/tofix/lua-5.3.4-tests/gc.lua +++ /dev/null @@ -1,624 +0,0 @@ --- $Id: gc.lua,v 1.72 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing garbage collection') - -local debug = require"debug" - -collectgarbage() - -assert(collectgarbage("isrunning")) - -local function gcinfo () return collectgarbage"count" * 1024 end - - --- test weird parameters -do - -- save original parameters - local a = collectgarbage("setpause", 200) - local b = collectgarbage("setstepmul", 200) - local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} - for i = 1, #t do - local p = t[i] - for j = 1, #t do - local m = t[j] - collectgarbage("setpause", p) - collectgarbage("setstepmul", m) - collectgarbage("step", 0) - collectgarbage("step", 10000) - end - end - -- restore original parameters - collectgarbage("setpause", a) - collectgarbage("setstepmul", b) - collectgarbage() -end - - -_G["while"] = 234 - -limit = 5000 - - -local function GC1 () - local u - local b -- must be declared after 'u' (to be above it in the stack) - local finish = false - u = setmetatable({}, {__gc = function () finish = true end}) - b = {34} - repeat u = {} until finish - assert(b[1] == 34) -- 'u' was collected, but 'b' was not - - finish = false; local i = 1 - u = setmetatable({}, {__gc = function () finish = true end}) - repeat i = i + 1; u = tostring(i) .. tostring(i) until finish - assert(b[1] == 34) -- 'u' was collected, but 'b' was not - - finish = false - u = setmetatable({}, {__gc = function () finish = true end}) - repeat local i; u = function () return i end until finish - assert(b[1] == 34) -- 'u' was collected, but 'b' was not -end - -local function GC2 () - local u - local finish = false - u = {setmetatable({}, {__gc = function () finish = true end})} - b = {34} - repeat u = {{}} until finish - assert(b[1] == 34) -- 'u' was collected, but 'b' was not - - finish = false; local i = 1 - u = {setmetatable({}, {__gc = function () finish = true end})} - repeat i = i + 1; u = {tostring(i) .. tostring(i)} until finish - assert(b[1] == 34) -- 'u' was collected, but 'b' was not - - finish = false - u = {setmetatable({}, {__gc = function () finish = true end})} - repeat local i; u = {function () return i end} until finish - assert(b[1] == 34) -- 'u' was collected, but 'b' was not -end - -local function GC() GC1(); GC2() end - - -contCreate = 0 - -print('tables') -while contCreate <= limit do - local a = {}; a = nil - contCreate = contCreate+1 -end - -a = "a" - -contCreate = 0 -print('strings') -while contCreate <= limit do - a = contCreate .. "b"; - a = string.gsub(a, '(%d%d*)', string.upper) - a = "a" - contCreate = contCreate+1 -end - - -contCreate = 0 - -a = {} - -print('functions') -function a:test () - while contCreate <= limit do - load(string.format("function temp(a) return 'a%d' end", contCreate), "")() - assert(temp() == string.format('a%d', contCreate)) - contCreate = contCreate+1 - end -end - -a:test() - --- collection of functions without locals, globals, etc. -do local f = function () end end - - -print("functions with errors") -prog = [[ -do - a = 10; - function foo(x,y) - a = sin(a+0.456-0.23e-12); - return function (z) return sin(%x+z) end - end - local x = function (w) a=a+w; end -end -]] -do - local step = 1 - if _soft then step = 13 end - for i=1, string.len(prog), step do - for j=i, string.len(prog), step do - pcall(load(string.sub(prog, i, j), "")) - end - end -end - -foo = nil -print('long strings') -x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" -assert(string.len(x)==80) -s = '' -n = 0 -k = math.min(300, (math.maxinteger // 80) // 2) -while n < k do s = s..x; n=n+1; j=tostring(n) end -assert(string.len(s) == k*80) -s = string.sub(s, 1, 10000) -s, i = string.gsub(s, '(%d%d%d%d)', '') -assert(i==10000 // 4) -s = nil -x = nil - -assert(_G["while"] == 234) - - -print("steps") - -print("steps (2)") - -local function dosteps (siz) - assert(not collectgarbage("isrunning")) - collectgarbage() - assert(not collectgarbage("isrunning")) - local a = {} - for i=1,100 do a[i] = {{}}; local b = {} end - local x = gcinfo() - local i = 0 - repeat -- do steps until it completes a collection cycle - i = i+1 - until collectgarbage("step", siz) - assert(gcinfo() < x) - return i -end - -collectgarbage"stop" - -if not _port then - -- test the "size" of basic GC steps (whatever they mean...) - assert(dosteps(0) > 10) - assert(dosteps(10) < dosteps(2)) -end - --- collector should do a full collection with so many steps -assert(dosteps(20000) == 1) -assert(collectgarbage("step", 20000) == true) -assert(collectgarbage("step", 20000) == true) - -assert(not collectgarbage("isrunning")) -collectgarbage"restart" -assert(collectgarbage("isrunning")) - - -if not _port then - -- test the pace of the collector - collectgarbage(); collectgarbage() - local x = gcinfo() - collectgarbage"stop" - assert(not collectgarbage("isrunning")) - repeat - local a = {} - until gcinfo() > 3 * x - collectgarbage"restart" - assert(collectgarbage("isrunning")) - repeat - local a = {} - until gcinfo() <= x * 2 -end - - -print("clearing tables") -lim = 15 -a = {} --- fill a with `collectable' indices -for i=1,lim do a[{}] = i end -b = {} -for k,v in pairs(a) do b[k]=v end --- remove all indices and collect them -for n in pairs(b) do - a[n] = nil - assert(type(n) == 'table' and next(n) == nil) - collectgarbage() -end -b = nil -collectgarbage() -for n in pairs(a) do error'cannot be here' end -for i=1,lim do a[i] = i end -for i=1,lim do assert(a[i] == i) end - - -print('weak tables') -a = {}; setmetatable(a, {__mode = 'k'}); --- fill a with some `collectable' indices -for i=1,lim do a[{}] = i end --- and some non-collectable ones -for i=1,lim do a[i] = i end -for i=1,lim do local s=string.rep('@', i); a[s] = s..'#' end -collectgarbage() -local i = 0 -for k,v in pairs(a) do assert(k==v or k..'#'==v); i=i+1 end -assert(i == 2*lim) - -a = {}; setmetatable(a, {__mode = 'v'}); -a[1] = string.rep('b', 21) -collectgarbage() -assert(a[1]) -- strings are *values* -a[1] = nil --- fill a with some `collectable' values (in both parts of the table) -for i=1,lim do a[i] = {} end -for i=1,lim do a[i..'x'] = {} end --- and some non-collectable ones -for i=1,lim do local t={}; a[t]=t end -for i=1,lim do a[i+lim]=i..'x' end -collectgarbage() -local i = 0 -for k,v in pairs(a) do assert(k==v or k-lim..'x' == v); i=i+1 end -assert(i == 2*lim) - -a = {}; setmetatable(a, {__mode = 'vk'}); -local x, y, z = {}, {}, {} --- keep only some items -a[1], a[2], a[3] = x, y, z -a[string.rep('$', 11)] = string.rep('$', 11) --- fill a with some `collectable' values -for i=4,lim do a[i] = {} end -for i=1,lim do a[{}] = i end -for i=1,lim do local t={}; a[t]=t end -collectgarbage() -assert(next(a) ~= nil) -local i = 0 -for k,v in pairs(a) do - assert((k == 1 and v == x) or - (k == 2 and v == y) or - (k == 3 and v == z) or k==v); - i = i+1 -end -assert(i == 4) -x,y,z=nil -collectgarbage() -assert(next(a) == string.rep('$', 11)) - - --- 'bug' in 5.1 -a = {} -local t = {x = 10} -local C = setmetatable({key = t}, {__mode = 'v'}) -local C1 = setmetatable({[t] = 1}, {__mode = 'k'}) -a.x = t -- this should not prevent 't' from being removed from - -- weak table 'C' by the time 'a' is finalized - -setmetatable(a, {__gc = function (u) - assert(C.key == nil) - assert(type(next(C1)) == 'table') - end}) - -a, t = nil -collectgarbage() -collectgarbage() -assert(next(C) == nil and next(C1) == nil) -C, C1 = nil - - --- ephemerons -local mt = {__mode = 'k'} -a = {{10},{20},{30},{40}}; setmetatable(a, mt) -x = nil -for i = 1, 100 do local n = {}; a[n] = {k = {x}}; x = n end -GC() -local n = x -local i = 0 -while n do n = a[n].k[1]; i = i + 1 end -assert(i == 100) -x = nil -GC() -for i = 1, 4 do assert(a[i][1] == i * 10); a[i] = nil end -assert(next(a) == nil) - -local K = {} -a[K] = {} -for i=1,10 do a[K][i] = {}; a[a[K][i]] = setmetatable({}, mt) end -x = nil -local k = 1 -for j = 1,100 do - local n = {}; local nk = k%10 + 1 - a[a[K][nk]][n] = {x, k = k}; x = n; k = nk -end -GC() -local n = x -local i = 0 -while n do local t = a[a[K][k]][n]; n = t[1]; k = t.k; i = i + 1 end -assert(i == 100) -K = nil -GC() --- assert(next(a) == nil) - - --- testing errors during GC -do -collectgarbage("stop") -- stop collection -local u = {} -local s = {}; setmetatable(s, {__mode = 'k'}) -setmetatable(u, {__gc = function (o) - local i = s[o] - s[i] = true - assert(not s[i - 1]) -- check proper finalization order - if i == 8 then error("here") end -- error during GC -end}) - -for i = 6, 10 do - local n = setmetatable({}, getmetatable(u)) - s[n] = i -end - -assert(not pcall(collectgarbage)) -for i = 8, 10 do assert(s[i]) end - -for i = 1, 5 do - local n = setmetatable({}, getmetatable(u)) - s[n] = i -end - -collectgarbage() -for i = 1, 10 do assert(s[i]) end - -getmetatable(u).__gc = false - - --- __gc errors with non-string messages -setmetatable({}, {__gc = function () error{} end}) -local a, b = pcall(collectgarbage) -assert(not a and type(b) == "string" and string.find(b, "error in __gc")) - -end -print '+' - - --- testing userdata -if T==nil then - (Message or print)('\n >>> testC not active: skipping userdata GC tests <<<\n') - -else - - local function newproxy(u) - return debug.setmetatable(T.newuserdata(0), debug.getmetatable(u)) - end - - collectgarbage("stop") -- stop collection - local u = newproxy(nil) - debug.setmetatable(u, {__gc = true}) - local s = 0 - local a = {[u] = 0}; setmetatable(a, {__mode = 'vk'}) - for i=1,10 do a[newproxy(u)] = i end - for k in pairs(a) do assert(getmetatable(k) == getmetatable(u)) end - local a1 = {}; for k,v in pairs(a) do a1[k] = v end - for k,v in pairs(a1) do a[v] = k end - for i =1,10 do assert(a[i]) end - getmetatable(u).a = a1 - getmetatable(u).u = u - do - local u = u - getmetatable(u).__gc = function (o) - assert(a[o] == 10-s) - assert(a[10-s] == nil) -- udata already removed from weak table - assert(getmetatable(o) == getmetatable(u)) - assert(getmetatable(o).a[o] == 10-s) - s=s+1 - end - end - a1, u = nil - assert(next(a) ~= nil) - collectgarbage() - assert(s==11) - collectgarbage() - assert(next(a) == nil) -- finalized keys are removed in two cycles -end - - --- __gc x weak tables -local u = setmetatable({}, {__gc = true}) --- __gc metamethod should be collected before running -setmetatable(getmetatable(u), {__mode = "v"}) -getmetatable(u).__gc = function (o) os.exit(1) end -- cannot happen -u = nil -collectgarbage() - -local u = setmetatable({}, {__gc = true}) -local m = getmetatable(u) -m.x = {[{0}] = 1; [0] = {1}}; setmetatable(m.x, {__mode = "kv"}); -m.__gc = function (o) - assert(next(getmetatable(o).x) == nil) - m = 10 -end -u, m = nil -collectgarbage() -assert(m==10) - - --- errors during collection -u = setmetatable({}, {__gc = function () error "!!!" end}) -u = nil -assert(not pcall(collectgarbage)) - - -if not _soft then - print("deep structures") - local a = {} - for i = 1,200000 do - a = {next = a} - end - collectgarbage() -end - --- create many threads with self-references and open upvalues -print("self-referenced threads") -local thread_id = 0 -local threads = {} - -local function fn (thread) - local x = {} - threads[thread_id] = function() - thread = x - end - coroutine.yield() -end - -while thread_id < 1000 do - local thread = coroutine.create(fn) - coroutine.resume(thread, thread) - thread_id = thread_id + 1 -end - - --- Create a closure (function inside 'f') with an upvalue ('param') that --- points (through a table) to the closure itself and to the thread --- ('co' and the initial value of 'param') where closure is running. --- Then, assert that table (and therefore everything else) will be --- collected. -do - local collected = false -- to detect collection - collectgarbage(); collectgarbage("stop") - do - local function f (param) - ;(function () - assert(type(f) == 'function' and type(param) == 'thread') - param = {param, f} - setmetatable(param, {__gc = function () collected = true end}) - coroutine.yield(100) - end)() - end - local co = coroutine.create(f) - assert(coroutine.resume(co, co)) - end - -- Now, thread and closure are not reacheable any more; - -- two collections are needed to break cycle - collectgarbage() - assert(not collected) - collectgarbage() - assert(collected) - collectgarbage("restart") -end - - -do - collectgarbage() - collectgarbage"stop" - local x = gcinfo() - repeat - for i=1,1000 do _ENV.a = {} end - collectgarbage("step", 0) -- steps should not unblock the collector - until gcinfo() > 2 * x - collectgarbage"restart" -end - - -if T then -- tests for weird cases collecting upvalues - - local function foo () - local a = {x = 20} - coroutine.yield(function () return a.x end) -- will run collector - assert(a.x == 20) -- 'a' is 'ok' - a = {x = 30} -- create a new object - assert(T.gccolor(a) == "white") -- of course it is new... - coroutine.yield(100) -- 'a' is still local to this thread - end - - local t = setmetatable({}, {__mode = "kv"}) - collectgarbage(); collectgarbage('stop') - -- create coroutine in a weak table, so it will never be marked - t.co = coroutine.wrap(foo) - local f = t.co() -- create function to access local 'a' - T.gcstate("atomic") -- ensure all objects are traversed - assert(T.gcstate() == "atomic") - assert(t.co() == 100) -- resume coroutine, creating new table for 'a' - assert(T.gccolor(t.co) == "white") -- thread was not traversed - T.gcstate("pause") -- collect thread, but should mark 'a' before that - assert(t.co == nil and f() == 30) -- ensure correct access to 'a' - - collectgarbage("restart") - - -- test barrier in sweep phase (advance cleaning of upvalue to white) - local u = T.newuserdata(0) -- create a userdata - collectgarbage() - collectgarbage"stop" - T.gcstate"atomic" - T.gcstate"sweepallgc" - local x = {} - assert(T.gccolor(u) == "black") -- upvalue is "old" (black) - assert(T.gccolor(x) == "white") -- table is "new" (white) - debug.setuservalue(u, x) -- trigger barrier - assert(T.gccolor(u) == "white") -- upvalue changed to white - collectgarbage"restart" - - print"+" -end - - -if T then - local debug = require "debug" - collectgarbage("stop") - local x = T.newuserdata(0) - local y = T.newuserdata(0) - debug.setmetatable(y, {__gc = true}) -- bless the new udata before... - debug.setmetatable(x, {__gc = true}) -- ...the old one - assert(T.gccolor(y) == "white") - T.checkmemory() - collectgarbage("restart") -end - - -if T then - print("emergency collections") - collectgarbage() - collectgarbage() - T.totalmem(T.totalmem() + 200) - for i=1,200 do local a = {} end - T.totalmem(0) - collectgarbage() - local t = T.totalmem("table") - local a = {{}, {}, {}} -- create 4 new tables - assert(T.totalmem("table") == t + 4) - t = T.totalmem("function") - a = function () end -- create 1 new closure - assert(T.totalmem("function") == t + 1) - t = T.totalmem("thread") - a = coroutine.create(function () end) -- create 1 new coroutine - assert(T.totalmem("thread") == t + 1) -end - --- create an object to be collected when state is closed -do - local setmetatable,assert,type,print,getmetatable = - setmetatable,assert,type,print,getmetatable - local tt = {} - tt.__gc = function (o) - assert(getmetatable(o) == tt) - -- create new objects during GC - local a = 'xuxu'..(10+3)..'joao', {} - ___Glob = o -- ressurect object! - setmetatable({}, tt) -- creates a new one with same metatable - print(">>> closing state " .. "<<<\n") - end - local u = setmetatable({}, tt) - ___Glob = {u} -- avoid object being collected before program end -end - --- create several objects to raise errors when collected while closing state -do - local mt = {__gc = function (o) return o + 1 end} - for i = 1,10 do - -- create object and preserve it until the end - table.insert(___Glob, setmetatable({}, mt)) - end -end - --- just to make sure -assert(collectgarbage'isrunning') - -print('OK') diff --git a/tofix/lua-5.3.4-tests/goto.lua b/tofix/lua-5.3.4-tests/goto.lua deleted file mode 100644 index 69bc154..0000000 --- a/tofix/lua-5.3.4-tests/goto.lua +++ /dev/null @@ -1,232 +0,0 @@ --- $Id: goto.lua,v 1.13 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -collectgarbage() - -local function errmsg (code, m) - local st, msg = load(code) - assert(not st and string.find(msg, m)) -end - --- cannot see label inside block -errmsg([[ goto l1; do ::l1:: end ]], "label 'l1'") -errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'") - --- repeated label -errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") - - --- undefined label -errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'") - --- jumping over variable definition -errmsg([[ -do local bb, cc; goto l1; end -local aa -::l1:: print(3) -]], "local 'aa'") - --- jumping into a block -errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") -errmsg([[ goto l1 do ::l1:: end ]], "label 'l1'") - --- cannot continue a repeat-until with variables -errmsg([[ - repeat - if x then goto cont end - local xuxu = 10 - ::cont:: - until xuxu < x -]], "local 'xuxu'") - --- simple gotos -local x -do - local y = 12 - goto l1 - ::l2:: x = x + 1; goto l3 - ::l1:: x = y; goto l2 -end -::l3:: ::l3_1:: assert(x == 13) - - --- long labels -do - local prog = [[ - do - local a = 1 - goto l%sa; a = a + 1 - ::l%sa:: a = a + 10 - goto l%sb; a = a + 2 - ::l%sb:: a = a + 20 - return a - end - ]] - local label = string.rep("0123456789", 40) - prog = string.format(prog, label, label, label, label) - assert(assert(load(prog))() == 31) -end - --- goto to correct label when nested --- do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3' - --- ok to jump over local dec. to end of block -do - goto l1 - local a = 23 - x = a - ::l1::; -end - -while true do - goto l4 - goto l1 -- ok to jump over local dec. to end of block - goto l1 -- multiple uses of same label - local x = 45 - ::l1:: ;;; -end -::l4:: assert(x == 13) - -if print then - goto l1 -- ok to jump over local dec. to end of block - error("should not be here") - goto l2 -- ok to jump over local dec. to end of block - local x - ::l1:: ; ::l2:: ;; -else end - --- to repeat a label in a different function is OK -local function foo () - local a = {} - goto l3 - ::l1:: a[#a + 1] = 1; goto l2; - ::l2:: a[#a + 1] = 2; goto l5; - ::l3:: - ::l3a:: a[#a + 1] = 3; goto l1; - ::l4:: a[#a + 1] = 4; goto l6; - ::l5:: a[#a + 1] = 5; goto l4; - ::l6:: assert(a[1] == 3 and a[2] == 1 and a[3] == 2 and - a[4] == 5 and a[5] == 4) - if not a[6] then a[6] = true; goto l3a end -- do it twice -end - -::l6:: foo() - - -do -- bug in 5.2 -> 5.3.2 - local x - ::L1:: - local y -- cannot join this SETNIL with previous one - assert(y == nil) - y = true - if x == nil then - x = 1 - goto L1 - else - x = x + 1 - end - assert(x == 2 and y == true) -end - --------------------------------------------------------------------------------- --- testing closing of upvalues - -local debug = require 'debug' - -local function foo () - local t = {} - do - local i = 1 - local a, b, c, d - t[1] = function () return a, b, c, d end - ::l1:: - local b - do - local c - t[#t + 1] = function () return a, b, c, d end -- t[2], t[4], t[6] - if i > 2 then goto l2 end - do - local d - t[#t + 1] = function () return a, b, c, d end -- t[3], t[5] - i = i + 1 - local a - goto l1 - end - end - end - ::l2:: return t -end - -local a = foo() -assert(#a == 6) - --- all functions share same 'a' -for i = 2, 6 do - assert(debug.upvalueid(a[1], 1) == debug.upvalueid(a[i], 1)) -end - --- 'b' and 'c' are shared among some of them -for i = 2, 6 do - -- only a[1] uses external 'b'/'b' - assert(debug.upvalueid(a[1], 2) ~= debug.upvalueid(a[i], 2)) - assert(debug.upvalueid(a[1], 3) ~= debug.upvalueid(a[i], 3)) -end - -for i = 3, 5, 2 do - -- inner functions share 'b'/'c' with previous ones - assert(debug.upvalueid(a[i], 2) == debug.upvalueid(a[i - 1], 2)) - assert(debug.upvalueid(a[i], 3) == debug.upvalueid(a[i - 1], 3)) - -- but not with next ones - assert(debug.upvalueid(a[i], 2) ~= debug.upvalueid(a[i + 1], 2)) - assert(debug.upvalueid(a[i], 3) ~= debug.upvalueid(a[i + 1], 3)) -end - --- only external 'd' is shared -for i = 2, 6, 2 do - assert(debug.upvalueid(a[1], 4) == debug.upvalueid(a[i], 4)) -end - --- internal 'd's are all different -for i = 3, 5, 2 do - for j = 1, 6 do - assert((debug.upvalueid(a[i], 4) == debug.upvalueid(a[j], 4)) - == (i == j)) - end -end - --------------------------------------------------------------------------------- --- testing if x goto optimizations - -local function testG (a) - if a == 1 then - goto l1 - error("should never be here!") - elseif a == 2 then goto l2 - elseif a == 3 then goto l3 - elseif a == 4 then - goto l1 -- go to inside the block - error("should never be here!") - ::l1:: a = a + 1 -- must go to 'if' end - else - goto l4 - ::l4a:: a = a * 2; goto l4b - error("should never be here!") - ::l4:: goto l4a - error("should never be here!") - ::l4b:: - end - do return a end - ::l2:: do return "2" end - ::l3:: do return "3" end - ::l1:: return "1" -end - -assert(testG(1) == "1") -assert(testG(2) == "2") -assert(testG(3) == "3") -assert(testG(4) == 5) -assert(testG(5) == 10) --------------------------------------------------------------------------------- - - -print'OK' diff --git a/tofix/lua-5.3.4-tests/literals.lua b/tofix/lua-5.3.4-tests/literals.lua deleted file mode 100644 index 3922b3f..0000000 --- a/tofix/lua-5.3.4-tests/literals.lua +++ /dev/null @@ -1,302 +0,0 @@ --- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing scanner') - -local debug = require "debug" - - -local function dostring (x) return assert(load(x), "")() end - -dostring("x \v\f = \t\r 'a\0a' \v\f\f") -assert(x == 'a\0a' and string.len(x) == 3) - --- escape sequences -assert('\n\"\'\\' == [[ - -"'\]]) - -assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$")) - --- assume ASCII just for tests: -assert("\09912" == 'c12') -assert("\99ab" == 'cab') -assert("\099" == '\99') -assert("\099\n" == 'c\10') -assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo') - -assert(010 .. 020 .. -030 == "1020-30") - --- hexadecimal escapes -assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232") - -local function lexstring (x, y, n) - local f = assert(load('return ' .. x .. - ', require"debug".getinfo(1).currentline', '')) - local s, l = f() - assert(s == y and l == n) -end - -lexstring("'abc\\z \n efg'", "abcefg", 2) -lexstring("'abc\\z \n\n\n'", "abc", 4) -lexstring("'\\z \n\t\f\v\n'", "", 3) -lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5) -lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5) -lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4) -lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4) -lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2) - -assert("abc\z - def\z - ghi\z - " == 'abcdefghi') - - --- UTF-8 sequences -assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0)) - --- limits for 1-byte sequences -assert("\u{0}\u{7F}" == "\x00\z\x7F") - --- limits for 2-byte sequences -assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF") - --- limits for 3-byte sequences -assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF") - --- limits for 4-byte sequences -assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF") - - --- Error in escape sequences -local function lexerror (s, err) - local st, msg = load('return ' .. s, '') - if err ~= '' then err = err .. "'" end - assert(not st and string.find(msg, "near .-" .. err)) -end - -lexerror([["abc\x"]], [[\x"]]) -lexerror([["abc\x]], [[\x]]) -lexerror([["\x]], [[\x]]) -lexerror([["\x5"]], [[\x5"]]) -lexerror([["\x5]], [[\x5]]) -lexerror([["\xr"]], [[\xr]]) -lexerror([["\xr]], [[\xr]]) -lexerror([["\x.]], [[\x.]]) -lexerror([["\x8%"]], [[\x8%%]]) -lexerror([["\xAG]], [[\xAG]]) -lexerror([["\g"]], [[\g]]) -lexerror([["\g]], [[\g]]) -lexerror([["\."]], [[\%.]]) - -lexerror([["\999"]], [[\999"]]) -lexerror([["xyz\300"]], [[\300"]]) -lexerror([[" \256"]], [[\256"]]) - --- errors in UTF-8 sequences -lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large -lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{' -lexerror([["abc\u"]], [[abc\u"]]) -- missing '{' -lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}' -lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}' -lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}' -lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits - --- unfinished strings -lexerror("[=[alo]]", "") -lexerror("[=[alo]=", "") -lexerror("[=[alo]", "") -lexerror("'alo", "") -lexerror("'alo \\z \n\n", "") -lexerror("'alo \\z", "") -lexerror([['alo \98]], "") - --- valid characters in variable names -for i = 0, 255 do - local s = string.char(i) - assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", "")) - assert(not string.find(s, "[a-zA-Z_0-9]") == - not load("a" .. s .. "1 = 1", "")) -end - - --- long variable names - -var1 = string.rep('a', 15000) .. '1' -var2 = string.rep('a', 15000) .. '2' -prog = string.format([[ - %s = 5 - %s = %s + 1 - return function () return %s - %s end -]], var1, var2, var1, var1, var2) -local f = dostring(prog) -assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) -var1, var2, f = nil -print('+') - --- escapes -- -assert("\n\t" == [[ - - ]]) -assert([[ - - $debug]] == "\n $debug") -assert([[ [ ]] ~= [[ ] ]]) --- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" -assert(string.len(b) == 960) -prog = [=[ -print('+') - -a1 = [["this is a 'string' with several 'quotes'"]] -a2 = "'quotes'" - -assert(string.find(a1, a2) == 34) -print('+') - -a1 = [==[temp = [[an arbitrary value]]; ]==] -assert(load(a1))() -assert(temp == 'an arbitrary value') --- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" -assert(string.len(b) == 960) -print('+') - -a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -00123456789012345678901234567890123456789123456789012345678901234567890123456789 -]] -assert(string.len(a) == 1863) -assert(string.sub(a, 1, 40) == string.sub(b, 1, 40)) -x = 1 -]=] - -print('+') -x = nil -dostring(prog) -assert(x) - -prog = nil -a = nil -b = nil - - --- testing line ends -prog = [[ -a = 1 -- a comment -b = 2 - - -x = [=[ -hi -]=] -y = "\ -hello\r\n\ -" -return require"debug".getinfo(1).currentline -]] - -for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do - local prog, nn = string.gsub(prog, "\n", n) - assert(dostring(prog) == nn) - assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") -end - - --- testing comments and strings with long brackets -a = [==[]=]==] -assert(a == "]=") - -a = [==[[===[[=[]]=][====[]]===]===]==] -assert(a == "[===[[=[]]=][====[]]===]===") - -a = [====[[===[[=[]]=][====[]]===]===]====] -assert(a == "[===[[=[]]=][====[]]===]===") - -a = [=[]]]]]]]]]=] -assert(a == "]]]]]]]]") - - ---[===[ -x y z [==[ blu foo -]== -] -]=]==] -error error]=]===] - --- generate all strings of four of these chars -local x = {"=", "[", "]", "\n"} -local len = 4 -local function gen (c, n) - if n==0 then coroutine.yield(c) - else - for _, a in pairs(x) do - gen(c..a, n-1) - end - end -end - -for s in coroutine.wrap(function () gen("", len) end) do - assert(s == load("return [====[\n"..s.."]====]", "")()) -end - - --- testing decimal point locale -if os.setlocale("pt_BR") or os.setlocale("ptb") then - assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4) - assert(tonumber(" -.4 ") == -0.4) - assert(tonumber(" +0x.41 ") == 0X0.41) - assert(not load("a = (3,4)")) - assert(assert(load("return 3.4"))() == 3.4) - assert(assert(load("return .4,3"))() == .4) - assert(assert(load("return 4."))() == 4.) - assert(assert(load("return 4.+.5"))() == 4.5) - - assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) - - assert(tonumber"inf" == nil and tonumber"NAN" == nil) - - assert(assert(load(string.format("return %q", 4.51)))() == 4.51) - - local a,b = load("return 4.5.") - assert(string.find(b, "'4%.5%.'")) - - assert(os.setlocale("C")) -else - (Message or print)( - '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n') -end - - --- testing %q x line ends -local s = "a string with \r and \n and \r\n and \n\r" -local c = string.format("return %q", s) -assert(assert(load(c))() == s) - --- testing errors -assert(not load"a = 'non-ending string") -assert(not load"a = 'non-ending string\n'") -assert(not load"a = '\\345'") -assert(not load"a = [=x]") - -print('OK') diff --git a/tofix/lua-5.3.4-tests/locals.lua b/tofix/lua-5.3.4-tests/locals.lua deleted file mode 100644 index f66f6f7..0000000 --- a/tofix/lua-5.3.4-tests/locals.lua +++ /dev/null @@ -1,162 +0,0 @@ --- $Id: locals.lua,v 1.37 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing local variables and environments') - -local debug = require"debug" - - --- bug in 5.1: - -local function f(x) x = nil; return x end -assert(f(10) == nil) - -local function f() local x; return x end -assert(f(10) == nil) - -local function f(x) x = nil; local y; return x, y end -assert(f(10) == nil and select(2, f(20)) == nil) - -do - local i = 10 - do local i = 100; assert(i==100) end - do local i = 1000; assert(i==1000) end - assert(i == 10) - if i ~= 10 then - local i = 20 - else - local i = 30 - assert(i == 30) - end -end - - - -f = nil - -local f -x = 1 - -a = nil -load('local a = {}')() -assert(a == nil) - -function f (a) - local _1, _2, _3, _4, _5 - local _6, _7, _8, _9, _10 - local x = 3 - local b = a - local c,d = a,b - if (d == b) then - local x = 'q' - x = b - assert(x == 2) - else - assert(nil) - end - assert(x == 3) - local f = 10 -end - -local b=10 -local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 - - -assert(x == 1) - -f(2) -assert(type(f) == 'function') - - -local function getenv (f) - local a,b = debug.getupvalue(f, 1) - assert(a == '_ENV') - return b -end - --- test for global table of loaded chunks -assert(getenv(load"a=3") == _G) -local c = {}; local f = load("a = 3", nil, nil, c) -assert(getenv(f) == c) -assert(c.a == nil) -f() -assert(c.a == 3) - --- old test for limits for special instructions (now just a generic test) -do - local i = 2 - local p = 4 -- p == 2^i - repeat - for j=-3,3 do - assert(load(string.format([[local a=%s; - a=a+%s; - assert(a ==2^%s)]], j, p-j, i), '')) () - assert(load(string.format([[local a=%s; - a=a-%s; - assert(a==-2^%s)]], -j, p-j, i), '')) () - assert(load(string.format([[local a,b=0,%s; - a=b-%s; - assert(a==-2^%s)]], -j, p-j, i), '')) () - end - p = 2 * p; i = i + 1 - until p <= 0 -end - -print'+' - - -if rawget(_G, "querytab") then - -- testing clearing of dead elements from tables - collectgarbage("stop") -- stop GC - local a = {[{}] = 4, [3] = 0, alo = 1, - a1234567890123456789012345678901234567890 = 10} - - local t = querytab(a) - - for k,_ in pairs(a) do a[k] = nil end - collectgarbage() -- restore GC and collect dead fiels in `a' - for i=0,t-1 do - local k = querytab(a, i) - assert(k == nil or type(k) == 'number' or k == 'alo') - end -end - - --- testing lexical environments - -assert(_ENV == _G) - -do -local dummy -local _ENV = (function (...) return ... end)(_G, dummy) -- { - -do local _ENV = {assert=assert}; assert(true) end -mt = {_G = _G} -local foo,x -A = false -- "declare" A -do local _ENV = mt - function foo (x) - A = x - do local _ENV = _G; A = 1000 end - return function (x) return A .. x end - end -end -assert(getenv(foo) == mt) -x = foo('hi'); assert(mt.A == 'hi' and A == 1000) -assert(x('*') == mt.A .. '*') - -do local _ENV = {assert=assert, A=10}; - do local _ENV = {assert=assert, A=20}; - assert(A==20);x=A - end - assert(A==10 and x==20) -end -assert(x==20) - - -print('OK') - -return 5,f - -end -- } - diff --git a/tofix/lua-5.3.4-tests/main.lua b/tofix/lua-5.3.4-tests/main.lua deleted file mode 100644 index a1809ce..0000000 --- a/tofix/lua-5.3.4-tests/main.lua +++ /dev/null @@ -1,381 +0,0 @@ -#! testing special comment on first line --- $Id: main.lua,v 1.65 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - --- most (all?) tests here assume a reasonable "Unix-like" shell -if _port then return end - --- use only "double quotes" inside shell scripts (better change to --- run on Windows) - - -print ("testing stand-alone interpreter") - -assert(os.execute()) -- machine has a system command - -local arg = arg or _ARG - -local prog = os.tmpname() -local otherprog = os.tmpname() -local out = os.tmpname() - -local progname -do - local i = 0 - while arg[i] do i=i-1 end - progname = arg[i+1] -end -print("progname: "..progname) - -local prepfile = function (s, p) - p = p or prog - io.output(p) - io.write(s) - assert(io.close()) -end - -local function getoutput () - io.input(out) - local t = io.read("a") - io.input():close() - assert(os.remove(out)) - return t -end - -local function checkprogout (s) - local t = getoutput() - for line in string.gmatch(s, ".-\n") do - assert(string.find(t, line, 1, true)) - end -end - -local function checkout (s) - local t = getoutput() - if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end - assert(s == t) - return t -end - - -local function RUN (p, ...) - p = string.gsub(p, "lua", '"'..progname..'"', 1) - local s = string.format(p, ...) - assert(os.execute(s)) -end - -local function NoRun (msg, p, ...) - p = string.gsub(p, "lua", '"'..progname..'"', 1) - local s = string.format(p, ...) - s = string.format("%s 2> %s", s, out) -- will send error to 'out' - assert(not os.execute(s)) - assert(string.find(getoutput(), msg, 1, true)) -- check error message -end - -RUN('lua -v') - -print(string.format("(temporary program file used in these tests: %s)", prog)) - --- running stdin as a file -prepfile"" -RUN('lua - < %s > %s', prog, out) -checkout("") - -prepfile[[ - print( -1, a -) -]] -RUN('lua - < %s > %s', prog, out) -checkout("1\tnil\n") - -RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) -checkout("10\n2\n") - - --- test option '-' -RUN('echo "print(arg[1])" | lua - -h > %s', out) -checkout("-h\n") - --- test environment variables used by Lua - -prepfile("print(package.path)") - --- test LUA_PATH -RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) -checkout("x\n") - --- test LUA_PATH_version -RUN('env LUA_INIT= LUA_PATH_5_3=y LUA_PATH=x lua %s > %s', prog, out) -checkout("y\n") - --- test LUA_CPATH -prepfile("print(package.cpath)") -RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out) -checkout("xuxu\n") - --- test LUA_CPATH_version -RUN('env LUA_INIT= LUA_CPATH_5_3=yacc LUA_CPATH=x lua %s > %s', prog, out) -checkout("yacc\n") - --- test LUA_INIT (and its access to 'arg' table) -prepfile("print(X)") -RUN('env LUA_INIT="X=tonumber(arg[1])" lua %s 3.2 > %s', prog, out) -checkout("3.2\n") - --- test LUA_INIT_version -prepfile("print(X)") -RUN('env LUA_INIT_5_3="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) -checkout("10\n") - --- test LUA_INIT for files -prepfile("x = x or 10; print(x); x = x + 1") -RUN('env LUA_INIT="@%s" lua %s > %s', prog, prog, out) -checkout("10\n11\n") - --- test errors in LUA_INIT -NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') - --- test option '-E' -local defaultpath, defaultCpath - -do - prepfile("print(package.path, package.cpath)") - RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', - prog, out) - local out = getoutput() - defaultpath = string.match(out, "^(.-)\t") - defaultCpath = string.match(out, "\t(.-)$") -end - --- paths did not changed -assert(not string.find(defaultpath, "xxx") and - string.find(defaultpath, "lua") and - not string.find(defaultCpath, "xxx") and - string.find(defaultCpath, "lua")) - - --- test replacement of ';;' to default path -local function convert (p) - prepfile("print(package.path)") - RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) - local expected = getoutput() - expected = string.sub(expected, 1, -2) -- cut final end of line - assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) -end - -convert(";") -convert(";;") -convert(";;;") -convert(";;;;") -convert(";;;;;") -convert(";;a;;;bc") - - --- test -l over multiple libraries -prepfile("print(1); a=2; return {x=15}") -prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) -RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) -checkout("1\n2\n15\n2\n15\n") - --- test 'arg' table -local a = [[ - assert(#arg == 3 and arg[1] == 'a' and - arg[2] == 'b' and arg[3] == 'c') - assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s') - assert(arg[4] == nil and arg[-4] == nil) - local a, b, c = ... - assert(... == 'a' and a == 'a' and b == 'b' and c == 'c') -]] -a = string.format(a, progname) -prepfile(a) -RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command - --- test 'arg' availability in libraries -prepfile"assert(arg)" -prepfile("assert(arg)", otherprog) -RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) - --- test messing up the 'arg' table -RUN('echo "print(...)" | lua -e "arg[1] = 100" - > %s', out) -checkout("100\n") -NoRun("'arg' is not a table", 'echo "" | lua -e "arg = 1" -') - --- test error in 'print' -RUN('echo 10 | lua -e "print=nil" -i > /dev/null 2> %s', out) -assert(string.find(getoutput(), "error calling 'print'")) - --- test 'debug.debug' -RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) -checkout("lua_debug> 1000lua_debug> ") - --- test many arguments -prepfile[[print(({...})[30])]] -RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) -checkout("a\n") - -RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out) -checkout("1\n3\n") - --- test iteractive mode -prepfile[[ -(6*2-6) -- === -a = -10 -print(a) -a]] -RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) -checkprogout("6\n10\n10\n\n") - -prepfile("a = [[b\nc\nd\ne]]\n=a") -RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) -checkprogout("b\nc\nd\ne\n\n") - -prompt = "alo" -prepfile[[ -- -a = 2 -]] -RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out) -local t = getoutput() -assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) - --- test for error objects -prepfile[[ -debug = require "debug" -m = {x=0} -setmetatable(m, {__tostring = function(x) - return tostring(debug.getinfo(4).currentline + x.x) -end}) -error(m) -]] -NoRun(progname .. ": 6\n", [[lua %s]], prog) - -prepfile("error{}") -NoRun("error object is a table value", [[lua %s]], prog) - - --- chunk broken in many lines -s = [=[ -- -function f ( x ) - local a = [[ -xuxu -]] - local b = "\ -xuxu\n" - if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] - return x + 1 - --\\ -end -return( f( 100 ) ) -assert( a == b ) -do return f( 11 ) end ]=] -s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines -prepfile(s) -RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) -checkprogout("101\n13\t22\n\n") - -prepfile[[#comment in 1st line without \n at the end]] -RUN('lua %s', prog) - -prepfile[[#test line number when file starts with comment line -debug = require"debug" -print(debug.getinfo(1).currentline) -]] -RUN('lua %s > %s', prog, out) -checkprogout('3') - --- close Lua with an open file -prepfile(string.format([[io.output(%q); io.write('alo')]], out)) -RUN('lua %s', prog) -checkout('alo') - --- bug in 5.2 beta (extra \0 after version line) -RUN([[lua -v -e"print'hello'" > %s]], out) -t = getoutput() -assert(string.find(t, "PUC%-Rio\nhello")) - - --- testing os.exit -prepfile("os.exit(nil, true)") -RUN('lua %s', prog) -prepfile("os.exit(0, true)") -RUN('lua %s', prog) -prepfile("os.exit(true, true)") -RUN('lua %s', prog) -prepfile("os.exit(1, true)") -NoRun("", "lua %s", prog) -- no message -prepfile("os.exit(false, true)") -NoRun("", "lua %s", prog) -- no message - --- remove temporary files -assert(os.remove(prog)) -assert(os.remove(otherprog)) -assert(not os.remove(out)) - --- invalid options -NoRun("unrecognized option '-h'", "lua -h") -NoRun("unrecognized option '---'", "lua ---") -NoRun("unrecognized option '-Ex'", "lua -Ex") -NoRun("unrecognized option '-vv'", "lua -vv") -NoRun("unrecognized option '-iv'", "lua -iv") -NoRun("'-e' needs argument", "lua -e") -NoRun("syntax error", "lua -e a") -NoRun("'-l' needs argument", "lua -l") - - -if T then -- auxiliary library? - print("testing 'not enough memory' to create a state") - NoRun("not enough memory", "env MEMLIMIT=100 lua") -end -print('+') - -print('testing Ctrl C') -do - -- interrupt a script - local function kill (pid) - return os.execute(string.format('kill -INT %d 2> /dev/null', pid)) - end - - -- function to run a script in background, returning its output file - -- descriptor and its pid - local function runback (luaprg) - -- shell script to run 'luaprg' in background and echo its pid - local shellprg = string.format('%s -e "%s" & echo $!', progname, luaprg) - local f = io.popen(shellprg, "r") -- run shell script - local pid = f:read() -- get pid for Lua script - print("(if test fails now, it may leave a Lua script running in \z - background, pid " .. pid .. ")") - return f, pid - end - - -- Lua script that runs protected infinite loop and then prints '42' - local f, pid = runback[[ - pcall(function () print(12); while true do end end); print(42)]] - -- wait until script is inside 'pcall' - assert(f:read() == "12") - kill(pid) -- send INT signal to Lua script - -- check that 'pcall' captured the exception and script continued running - assert(f:read() == "42") -- expected output - assert(f:close()) - print("done") - - -- Lua script in a long unbreakable search - local f, pid = runback[[ - print(15); string.find(string.rep('a', 100000), '.*b')]] - -- wait (so script can reach the loop) - assert(f:read() == "15") - assert(os.execute("sleep 1")) - -- must send at least two INT signals to stop this Lua script - local n = 100 - for i = 0, 100 do -- keep sending signals - if not kill(pid) then -- until it fails - n = i -- number of non-failed kills - break - end - end - assert(f:close()) - assert(n >= 2) - print(string.format("done (with %d kills)", n)) - -end - -print("OK") diff --git a/tofix/lua-5.3.4-tests/math.lua b/tofix/lua-5.3.4-tests/math.lua deleted file mode 100644 index 53ce9b5..0000000 --- a/tofix/lua-5.3.4-tests/math.lua +++ /dev/null @@ -1,824 +0,0 @@ --- $Id: math.lua,v 1.78 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print("testing numbers and math lib") - -local minint = math.mininteger -local maxint = math.maxinteger - -local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 -assert((1 << intbits) == 0) - -assert(minint == 1 << (intbits - 1)) -assert(maxint == minint - 1) - --- number of bits in the mantissa of a floating-point number -local floatbits = 24 -do - local p = 2.0^floatbits - while p < p + 1.0 do - p = p * 2.0 - floatbits = floatbits + 1 - end -end - -local function isNaN (x) - return (x ~= x) -end - -assert(isNaN(0/0)) -assert(not isNaN(1/0)) - - -do - local x = 2.0^floatbits - assert(x > x - 1.0 and x == x + 1.0) - - print(string.format("%d-bit integers, %d-bit (mantissa) floats", - intbits, floatbits)) -end - -assert(math.type(0) == "integer" and math.type(0.0) == "float" - and math.type("10") == nil) - - -local function checkerror (msg, f, ...) - local s, err = pcall(f, ...) - assert(not s and string.find(err, msg)) -end - -local msgf2i = "number.* has no integer representation" - --- float equality -function eq (a,b,limit) - if not limit then - if floatbits >= 50 then limit = 1E-11 - else limit = 1E-5 - end - end - -- a == b needed for +inf/-inf - return a == b or math.abs(a-b) <= limit -end - - --- equality with types -function eqT (a,b) - return a == b and math.type(a) == math.type(b) -end - - --- basic float notation -assert(0e12 == 0 and .0 == 0 and 0. == 0 and .2e2 == 20 and 2.E-1 == 0.2) - -do - local a,b,c = "2", " 3e0 ", " 10 " - assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0) - assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string') - assert(a == "2" and b == " 3e0 " and c == " 10 " and -c == -" 10 ") - assert(c%a == 0 and a^b == 08) - a = 0 - assert(a == -a and 0 == -0) -end - -do - local x = -1 - local mz = 0/x -- minus zero - t = {[0] = 10, 20, 30, 40, 50} - assert(t[mz] == t[0] and t[-0] == t[0]) -end - -do -- tests for 'modf' - local a,b = math.modf(3.5) - assert(a == 3.0 and b == 0.5) - a,b = math.modf(-2.5) - assert(a == -2.0 and b == -0.5) - a,b = math.modf(-3e23) - assert(a == -3e23 and b == 0.0) - a,b = math.modf(3e35) - assert(a == 3e35 and b == 0.0) - a,b = math.modf(-1/0) -- -inf - assert(a == -1/0 and b == 0.0) - a,b = math.modf(1/0) -- inf - assert(a == 1/0 and b == 0.0) - a,b = math.modf(0/0) -- NaN - assert(isNaN(a) and isNaN(b)) - a,b = math.modf(3) -- integer argument - assert(eqT(a, 3) and eqT(b, 0.0)) - a,b = math.modf(minint) - assert(eqT(a, minint) and eqT(b, 0.0)) -end - -assert(math.huge > 10e30) -assert(-math.huge < -10e30) - - --- integer arithmetic -assert(minint < minint + 1) -assert(maxint - 1 < maxint) -assert(0 - minint == minint) -assert(minint * minint == 0) -assert(maxint * maxint * maxint == maxint) - - --- testing floor division and conversions - -for _, i in pairs{-16, -15, -3, -2, -1, 0, 1, 2, 3, 15} do - for _, j in pairs{-16, -15, -3, -2, -1, 1, 2, 3, 15} do - for _, ti in pairs{0, 0.0} do -- try 'i' as integer and as float - for _, tj in pairs{0, 0.0} do -- try 'j' as integer and as float - local x = i + ti - local y = j + tj - assert(i//j == math.floor(i/j)) - end - end - end -end - -assert(1//0.0 == 1/0) -assert(-1 // 0.0 == -1/0) -assert(eqT(3.5 // 1.5, 2.0)) -assert(eqT(3.5 // -1.5, -3.0)) - -assert(maxint // maxint == 1) -assert(maxint // 1 == maxint) -assert((maxint - 1) // maxint == 0) -assert(maxint // (maxint - 1) == 1) -assert(minint // minint == 1) -assert(minint // minint == 1) -assert((minint + 1) // minint == 0) -assert(minint // (minint + 1) == 1) -assert(minint // 1 == minint) - -assert(minint // -1 == -minint) -assert(minint // -2 == 2^(intbits - 2)) -assert(maxint // -1 == -maxint) - - --- negative exponents -do - assert(2^-3 == 1 / 2^3) - assert(eq((-3)^-3, 1 / (-3)^3)) - for i = -3, 3 do -- variables avoid constant folding - for j = -3, 3 do - -- domain errors (0^(-n)) are not portable - if not _port or i ~= 0 or j > 0 then - assert(eq(i^j, 1 / i^(-j))) - end - end - end -end - --- comparison between floats and integers (border cases) -if floatbits < intbits then - assert(2.0^floatbits == (1 << floatbits)) - assert(2.0^floatbits - 1.0 == (1 << floatbits) - 1.0) - assert(2.0^floatbits - 1.0 ~= (1 << floatbits)) - -- float is rounded, int is not - assert(2.0^floatbits + 1.0 ~= (1 << floatbits) + 1) -else -- floats can express all integers with full accuracy - assert(maxint == maxint + 0.0) - assert(maxint - 1 == maxint - 1.0) - assert(minint + 1 == minint + 1.0) - assert(maxint ~= maxint - 1.0) -end -assert(maxint + 0.0 == 2.0^(intbits - 1) - 1.0) -assert(minint + 0.0 == minint) -assert(minint + 0.0 == -2.0^(intbits - 1)) - - --- order between floats and integers -assert(1 < 1.1); assert(not (1 < 0.9)) -assert(1 <= 1.1); assert(not (1 <= 0.9)) -assert(-1 < -0.9); assert(not (-1 < -1.1)) -assert(1 <= 1.1); assert(not (-1 <= -1.1)) -assert(-1 < -0.9); assert(not (-1 < -1.1)) -assert(-1 <= -0.9); assert(not (-1 <= -1.1)) -assert(minint <= minint + 0.0) -assert(minint + 0.0 <= minint) -assert(not (minint < minint + 0.0)) -assert(not (minint + 0.0 < minint)) -assert(maxint < minint * -1.0) -assert(maxint <= minint * -1.0) - -do - local fmaxi1 = 2^(intbits - 1) - assert(maxint < fmaxi1) - assert(maxint <= fmaxi1) - assert(not (fmaxi1 <= maxint)) - assert(minint <= -2^(intbits - 1)) - assert(-2^(intbits - 1) <= minint) -end - -if floatbits < intbits then - print("testing order (floats cannot represent all integers)") - local fmax = 2^floatbits - local ifmax = fmax | 0 - assert(fmax < ifmax + 1) - assert(fmax - 1 < ifmax) - assert(-(fmax - 1) > -ifmax) - assert(not (fmax <= ifmax - 1)) - assert(-fmax > -(ifmax + 1)) - assert(not (-fmax >= -(ifmax - 1))) - - assert(fmax/2 - 0.5 < ifmax//2) - assert(-(fmax/2 - 0.5) > -ifmax//2) - - assert(maxint < 2^intbits) - assert(minint > -2^intbits) - assert(maxint <= 2^intbits) - assert(minint >= -2^intbits) -else - print("testing order (floats can represent all integers)") - assert(maxint < maxint + 1.0) - assert(maxint < maxint + 0.5) - assert(maxint - 1.0 < maxint) - assert(maxint - 0.5 < maxint) - assert(not (maxint + 0.0 < maxint)) - assert(maxint + 0.0 <= maxint) - assert(not (maxint < maxint + 0.0)) - assert(maxint + 0.0 <= maxint) - assert(maxint <= maxint + 0.0) - assert(not (maxint + 1.0 <= maxint)) - assert(not (maxint + 0.5 <= maxint)) - assert(not (maxint <= maxint - 1.0)) - assert(not (maxint <= maxint - 0.5)) - - assert(minint < minint + 1.0) - assert(minint < minint + 0.5) - assert(minint <= minint + 0.5) - assert(minint - 1.0 < minint) - assert(minint - 1.0 <= minint) - assert(not (minint + 0.0 < minint)) - assert(not (minint + 0.5 < minint)) - assert(not (minint < minint + 0.0)) - assert(minint + 0.0 <= minint) - assert(minint <= minint + 0.0) - assert(not (minint + 1.0 <= minint)) - assert(not (minint + 0.5 <= minint)) - assert(not (minint <= minint - 1.0)) -end - -do - local NaN = 0/0 - assert(not (NaN < 0)) - assert(not (NaN > minint)) - assert(not (NaN <= -9)) - assert(not (NaN <= maxint)) - assert(not (NaN < maxint)) - assert(not (minint <= NaN)) - assert(not (minint < NaN)) -end - - --- avoiding errors at compile time -local function checkcompt (msg, code) - checkerror(msg, assert(load(code))) -end -checkcompt("divide by zero", "return 2 // 0") -checkcompt(msgf2i, "return 2.3 >> 0") -checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1)) -checkcompt("field 'huge'", "return math.huge << 1") -checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1)) -checkcompt(msgf2i, "return 2.3 ~ '0.0'") - - --- testing overflow errors when converting from float to integer (runtime) -local function f2i (x) return x | x end -checkerror(msgf2i, f2i, math.huge) -- +inf -checkerror(msgf2i, f2i, -math.huge) -- -inf -checkerror(msgf2i, f2i, 0/0) -- NaN - -if floatbits < intbits then - -- conversion tests when float cannot represent all integers - assert(maxint + 1.0 == maxint + 0.0) - assert(minint - 1.0 == minint + 0.0) - checkerror(msgf2i, f2i, maxint + 0.0) - assert(f2i(2.0^(intbits - 2)) == 1 << (intbits - 2)) - assert(f2i(-2.0^(intbits - 2)) == -(1 << (intbits - 2))) - assert((2.0^(floatbits - 1) + 1.0) // 1 == (1 << (floatbits - 1)) + 1) - -- maximum integer representable as a float - local mf = maxint - (1 << (floatbits - intbits)) + 1 - assert(f2i(mf + 0.0) == mf) -- OK up to here - mf = mf + 1 - assert(f2i(mf + 0.0) ~= mf) -- no more representable -else - -- conversion tests when float can represent all integers - assert(maxint + 1.0 > maxint) - assert(minint - 1.0 < minint) - assert(f2i(maxint + 0.0) == maxint) - checkerror("no integer rep", f2i, maxint + 1.0) - checkerror("no integer rep", f2i, minint - 1.0) -end - --- 'minint' should be representable as a float no matter the precision -assert(f2i(minint + 0.0) == minint) - - --- testing numeric strings - -assert("2" + 1 == 3) -assert("2 " + 1 == 3) -assert(" -2 " + 1 == -1) -assert(" -0xa " + 1 == -9) - - --- Literal integer Overflows (new behavior in 5.3.3) -do - -- no overflows - assert(eqT(tonumber(tostring(maxint)), maxint)) - assert(eqT(tonumber(tostring(minint)), minint)) - - -- add 1 to last digit as a string (it cannot be 9...) - local function incd (n) - local s = string.format("%d", n) - s = string.gsub(s, "%d$", function (d) - assert(d ~= '9') - return string.char(string.byte(d) + 1) - end) - return s - end - - -- 'tonumber' with overflow by 1 - assert(eqT(tonumber(incd(maxint)), maxint + 1.0)) - assert(eqT(tonumber(incd(minint)), minint - 1.0)) - - -- large numbers - assert(eqT(tonumber("1"..string.rep("0", 30)), 1e30)) - assert(eqT(tonumber("-1"..string.rep("0", 30)), -1e30)) - - -- hexa format still wraps around - assert(eqT(tonumber("0x1"..string.rep("0", 30)), 0)) - - -- lexer in the limits - assert(minint == load("return " .. minint)()) - assert(eqT(maxint, load("return " .. maxint)())) - - assert(eqT(10000000000000000000000.0, 10000000000000000000000)) - assert(eqT(-10000000000000000000000.0, -10000000000000000000000)) -end - - --- testing 'tonumber' - --- 'tonumber' with numbers -assert(tonumber(3.4) == 3.4) -assert(eqT(tonumber(3), 3)) -assert(eqT(tonumber(maxint), maxint) and eqT(tonumber(minint), minint)) -assert(tonumber(1/0) == 1/0) - --- 'tonumber' with strings -assert(tonumber("0") == 0) -assert(tonumber("") == nil) -assert(tonumber(" ") == nil) -assert(tonumber("-") == nil) -assert(tonumber(" -0x ") == nil) -assert(tonumber{} == nil) -assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and - tonumber'.01' == 0.01 and tonumber'-1.' == -1 and - tonumber'+1.' == 1) -assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and - tonumber'1e' == nil and tonumber'1.0e+' == nil and - tonumber'.' == nil) -assert(tonumber('-012') == -010-2) -assert(tonumber('-1.2e2') == - - -120) - -assert(tonumber("0xffffffffffff") == (1 << (4*12)) - 1) -assert(tonumber("0x"..string.rep("f", (intbits//4))) == -1) -assert(tonumber("-0x"..string.rep("f", (intbits//4))) == 1) - --- testing 'tonumber' with base -assert(tonumber(' 001010 ', 2) == 10) -assert(tonumber(' 001010 ', 10) == 001010) -assert(tonumber(' -1010 ', 2) == -10) -assert(tonumber('10', 36) == 36) -assert(tonumber(' -10 ', 36) == -36) -assert(tonumber(' +1Z ', 36) == 36 + 35) -assert(tonumber(' -1z ', 36) == -36 + -35) -assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15))))))) -assert(tonumber(string.rep('1', (intbits - 2)), 2) + 1 == 2^(intbits - 2)) -assert(tonumber('ffffFFFF', 16)+1 == (1 << 32)) -assert(tonumber('0ffffFFFF', 16)+1 == (1 << 32)) -assert(tonumber('-0ffffffFFFF', 16) - 1 == -(1 << 40)) -for i = 2,36 do - local i2 = i * i - local i10 = i2 * i2 * i2 * i2 * i2 -- i^10 - assert(tonumber('\t10000000000\t', i) == i10) -end - -if not _soft then - -- tests with very long numerals - assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) - assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) - assert(tonumber("0x"..string.rep("f", 300)..".0") == 2.0^(4*300) - 1) - assert(tonumber("0x"..string.rep("f", 500)..".0") == 2.0^(4*500) - 1) - assert(tonumber('0x3.' .. string.rep('0', 1000)) == 3) - assert(tonumber('0x' .. string.rep('0', 1000) .. 'a') == 10) - assert(tonumber('0x0.' .. string.rep('0', 13).."1") == 2.0^(-4*14)) - assert(tonumber('0x0.' .. string.rep('0', 150).."1") == 2.0^(-4*151)) - assert(tonumber('0x0.' .. string.rep('0', 300).."1") == 2.0^(-4*301)) - assert(tonumber('0x0.' .. string.rep('0', 500).."1") == 2.0^(-4*501)) - - assert(tonumber('0xe03' .. string.rep('0', 1000) .. 'p-4000') == 3587.0) - assert(tonumber('0x.' .. string.rep('0', 1000) .. '74p4004') == 0x7.4) -end - --- testing 'tonumber' for invalid formats - -local function f (...) - if select('#', ...) == 1 then - return (...) - else - return "***" - end -end - -assert(f(tonumber('fFfa', 15)) == nil) -assert(f(tonumber('099', 8)) == nil) -assert(f(tonumber('1\0', 2)) == nil) -assert(f(tonumber('', 8)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber('0xf', 10)) == nil) - -assert(f(tonumber('inf')) == nil) -assert(f(tonumber(' INF ')) == nil) -assert(f(tonumber('Nan')) == nil) -assert(f(tonumber('nan')) == nil) - -assert(f(tonumber(' ')) == nil) -assert(f(tonumber('')) == nil) -assert(f(tonumber('1 a')) == nil) -assert(f(tonumber('1 a', 2)) == nil) -assert(f(tonumber('1\0')) == nil) -assert(f(tonumber('1 \0')) == nil) -assert(f(tonumber('1\0 ')) == nil) -assert(f(tonumber('e1')) == nil) -assert(f(tonumber('e 1')) == nil) -assert(f(tonumber(' 3.4.5 ')) == nil) - - --- testing 'tonumber' for invalid hexadecimal formats - -assert(tonumber('0x') == nil) -assert(tonumber('x') == nil) -assert(tonumber('x3') == nil) -assert(tonumber('0x3.3.3') == nil) -- two decimal points -assert(tonumber('00x2') == nil) -assert(tonumber('0x 2') == nil) -assert(tonumber('0 x2') == nil) -assert(tonumber('23x') == nil) -assert(tonumber('- 0xaa') == nil) -assert(tonumber('-0xaaP ') == nil) -- no exponent -assert(tonumber('0x0.51p') == nil) -assert(tonumber('0x5p+-2') == nil) - - --- testing hexadecimal numerals - -assert(0x10 == 16 and 0xfff == 2^12 - 1 and 0XFB == 251) -assert(0x0p12 == 0 and 0x.0p-3 == 0) -assert(0xFFFFFFFF == (1 << 32) - 1) -assert(tonumber('+0x2') == 2) -assert(tonumber('-0xaA') == -170) -assert(tonumber('-0xffFFFfff') == -(1 << 32) + 1) - --- possible confusion with decimal exponent -assert(0E+1 == 0 and 0xE+1 == 15 and 0xe-1 == 13) - - --- floating hexas - -assert(tonumber(' 0x2.5 ') == 0x25/16) -assert(tonumber(' -0x2.5 ') == -0x25/16) -assert(tonumber(' +0x0.51p+8 ') == 0x51) -assert(0x.FfffFFFF == 1 - '0x.00000001') -assert('0xA.a' + 0 == 10 + 10/16) -assert(0xa.aP4 == 0XAA) -assert(0x4P-2 == 1) -assert(0x1.1 == '0x1.' + '+0x.1') -assert(0Xabcdef.0 == 0x.ABCDEFp+24) - - -assert(1.1 == 1.+.1) -assert(100.0 == 1E2 and .01 == 1e-2) -assert(1111111111 - 1111111110 == 1000.00e-03) -assert(1.1 == '1.'+'.1') -assert(tonumber'1111111111' - tonumber'1111111110' == - tonumber" +0.001e+3 \n\t") - -assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31) - -assert(0.123456 > 0.123455) - -assert(tonumber('+1.23E18') == 1.23*10.0^18) - --- testing order operators -assert(not(1<1) and (1<2) and not(2<1)) -assert(not('a'<'a') and ('a'<'b') and not('b'<'a')) -assert((1<=1) and (1<=2) and not(2<=1)) -assert(('a'<='a') and ('a'<='b') and not('b'<='a')) -assert(not(1>1) and not(1>2) and (2>1)) -assert(not('a'>'a') and not('a'>'b') and ('b'>'a')) -assert((1>=1) and not(1>=2) and (2>=1)) -assert(('a'>='a') and not('a'>='b') and ('b'>='a')) -assert(1.3 < 1.4 and 1.3 <= 1.4 and not (1.3 < 1.3) and 1.3 <= 1.3) - --- testing mod operator -assert(eqT(-4 % 3, 2)) -assert(eqT(4 % -3, -2)) -assert(eqT(-4.0 % 3, 2.0)) -assert(eqT(4 % -3.0, -2.0)) -assert(math.pi - math.pi % 1 == 3) -assert(math.pi - math.pi % 0.001 == 3.141) - -assert(eqT(minint % minint, 0)) -assert(eqT(maxint % maxint, 0)) -assert((minint + 1) % minint == minint + 1) -assert((maxint - 1) % maxint == maxint - 1) -assert(minint % maxint == maxint - 1) - -assert(minint % -1 == 0) -assert(minint % -2 == 0) -assert(maxint % -2 == -1) - --- non-portable tests because Windows C library cannot compute --- fmod(1, huge) correctly -if not _port then - local function anan (x) assert(isNaN(x)) end -- assert Not a Number - anan(0.0 % 0) - anan(1.3 % 0) - anan(math.huge % 1) - anan(math.huge % 1e30) - anan(-math.huge % 1e30) - anan(-math.huge % -1e30) - assert(1 % math.huge == 1) - assert(1e30 % math.huge == 1e30) - assert(1e30 % -math.huge == -math.huge) - assert(-1 % math.huge == math.huge) - assert(-1 % -math.huge == -1) -end - - --- testing unsigned comparisons -assert(math.ult(3, 4)) -assert(not math.ult(4, 4)) -assert(math.ult(-2, -1)) -assert(math.ult(2, -1)) -assert(not math.ult(-2, -2)) -assert(math.ult(maxint, minint)) -assert(not math.ult(minint, maxint)) - - -assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1)) -assert(eq(math.tan(math.pi/4), 1)) -assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0)) -assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and - eq(math.asin(1), math.pi/2)) -assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2)) -assert(math.abs(-10.43) == 10.43) -assert(eqT(math.abs(minint), minint)) -assert(eqT(math.abs(maxint), maxint)) -assert(eqT(math.abs(-maxint), maxint)) -assert(eq(math.atan(1,0), math.pi/2)) -assert(math.fmod(10,3) == 1) -assert(eq(math.sqrt(10)^2, 10)) -assert(eq(math.log(2, 10), math.log(2)/math.log(10))) -assert(eq(math.log(2, 2), 1)) -assert(eq(math.log(9, 3), 2)) -assert(eq(math.exp(0), 1)) -assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) - - -assert(tonumber(' 1.3e-2 ') == 1.3e-2) -assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) - --- testing constant limits --- 2^23 = 8388608 -assert(8388609 + -8388609 == 0) -assert(8388608 + -8388608 == 0) -assert(8388607 + -8388607 == 0) - - - -do -- testing floor & ceil - assert(eqT(math.floor(3.4), 3)) - assert(eqT(math.ceil(3.4), 4)) - assert(eqT(math.floor(-3.4), -4)) - assert(eqT(math.ceil(-3.4), -3)) - assert(eqT(math.floor(maxint), maxint)) - assert(eqT(math.ceil(maxint), maxint)) - assert(eqT(math.floor(minint), minint)) - assert(eqT(math.floor(minint + 0.0), minint)) - assert(eqT(math.ceil(minint), minint)) - assert(eqT(math.ceil(minint + 0.0), minint)) - assert(math.floor(1e50) == 1e50) - assert(math.ceil(1e50) == 1e50) - assert(math.floor(-1e50) == -1e50) - assert(math.ceil(-1e50) == -1e50) - for _, p in pairs{31,32,63,64} do - assert(math.floor(2^p) == 2^p) - assert(math.floor(2^p + 0.5) == 2^p) - assert(math.ceil(2^p) == 2^p) - assert(math.ceil(2^p - 0.5) == 2^p) - end - checkerror("number expected", math.floor, {}) - checkerror("number expected", math.ceil, print) - assert(eqT(math.tointeger(minint), minint)) - assert(eqT(math.tointeger(minint .. ""), minint)) - assert(eqT(math.tointeger(maxint), maxint)) - assert(eqT(math.tointeger(maxint .. ""), maxint)) - assert(eqT(math.tointeger(minint + 0.0), minint)) - assert(math.tointeger(0.0 - minint) == nil) - assert(math.tointeger(math.pi) == nil) - assert(math.tointeger(-math.pi) == nil) - assert(math.floor(math.huge) == math.huge) - assert(math.ceil(math.huge) == math.huge) - assert(math.tointeger(math.huge) == nil) - assert(math.floor(-math.huge) == -math.huge) - assert(math.ceil(-math.huge) == -math.huge) - assert(math.tointeger(-math.huge) == nil) - assert(math.tointeger("34.0") == 34) - assert(math.tointeger("34.3") == nil) - assert(math.tointeger({}) == nil) - assert(math.tointeger(0/0) == nil) -- NaN -end - - --- testing fmod for integers -for i = -6, 6 do - for j = -6, 6 do - if j ~= 0 then - local mi = math.fmod(i, j) - local mf = math.fmod(i + 0.0, j) - assert(mi == mf) - assert(math.type(mi) == 'integer' and math.type(mf) == 'float') - if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then - assert(eqT(mi, i % j)) - end - end - end -end -assert(eqT(math.fmod(minint, minint), 0)) -assert(eqT(math.fmod(maxint, maxint), 0)) -assert(eqT(math.fmod(minint + 1, minint), minint + 1)) -assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1)) - -checkerror("zero", math.fmod, 3, 0) - - -do -- testing max/min - checkerror("value expected", math.max) - checkerror("value expected", math.min) - assert(eqT(math.max(3), 3)) - assert(eqT(math.max(3, 5, 9, 1), 9)) - assert(math.max(maxint, 10e60) == 10e60) - assert(eqT(math.max(minint, minint + 1), minint + 1)) - assert(eqT(math.min(3), 3)) - assert(eqT(math.min(3, 5, 9, 1), 1)) - assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2) - assert(math.min(1.9, 1.7, 1.72) == 1.7) - assert(math.min(-10e60, minint) == -10e60) - assert(eqT(math.min(maxint, maxint - 1), maxint - 1)) - assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)) -end --- testing implicit convertions - -local a,b = '10', '20' -assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20) -assert(a == '10' and b == '20') - - -do - print("testing -0 and NaN") - local mz, z = -0.0, 0.0 - assert(mz == z) - assert(1/mz < 0 and 0 < 1/z) - local a = {[mz] = 1} - assert(a[z] == 1 and a[mz] == 1) - a[z] = 2 - assert(a[z] == 2 and a[mz] == 2) - local inf = math.huge * 2 + 1 - mz, z = -1/inf, 1/inf - assert(mz == z) - assert(1/mz < 0 and 0 < 1/z) - local NaN = inf - inf - assert(NaN ~= NaN) - assert(not (NaN < NaN)) - assert(not (NaN <= NaN)) - assert(not (NaN > NaN)) - assert(not (NaN >= NaN)) - assert(not (0 < NaN) and not (NaN < 0)) - local NaN1 = 0/0 - assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) - local a = {} - assert(not pcall(rawset, a, NaN, 1)) - assert(a[NaN] == nil) - a[1] = 1 - assert(not pcall(rawset, a, NaN, 1)) - assert(a[NaN] == nil) - -- strings with same binary representation as 0.0 (might create problems - -- for constant manipulation in the pre-compiler) - local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0" - assert(a1 == a2 and a2 == a4 and a1 ~= a3) - assert(a3 == a5) -end - - -print("testing 'math.random'") -math.randomseed(0) - -do -- test random for floats - local max = -math.huge - local min = math.huge - for i = 0, 20000 do - local t = math.random() - assert(0 <= t and t < 1) - max = math.max(max, t) - min = math.min(min, t) - if eq(max, 1, 0.001) and eq(min, 0, 0.001) then - goto ok - end - end - -- loop ended without satisfing condition - assert(false) - ::ok:: -end - -do - local function aux (p, lim) -- test random for small intervals - local x1, x2 - if #p == 1 then x1 = 1; x2 = p[1] - else x1 = p[1]; x2 = p[2] - end - local mark = {}; local count = 0 -- to check that all values appeared - for i = 0, lim or 2000 do - local t = math.random(table.unpack(p)) - assert(x1 <= t and t <= x2) - if not mark[t] then -- new value - mark[t] = true - count = count + 1 - end - if count == x2 - x1 + 1 then -- all values appeared; OK - goto ok - end - end - -- loop ended without satisfing condition - assert(false) - ::ok:: - end - - aux({-10,0}) - aux({6}) - aux({-10, 10}) - aux({minint, minint}) - aux({maxint, maxint}) - aux({minint, minint + 9}) - aux({maxint - 3, maxint}) -end - -do - local function aux(p1, p2) -- test random for large intervals - local max = minint - local min = maxint - local n = 200 - local mark = {}; local count = 0 -- to count how many different values - for _ = 1, n do - local t = math.random(p1, p2) - max = math.max(max, t) - min = math.min(min, t) - if not mark[t] then -- new value - mark[t] = true - count = count + 1 - end - end - -- at least 80% of values are different - assert(count >= n * 0.8) - -- min and max not too far from formal min and max - local diff = (p2 - p1) // 8 - assert(min < p1 + diff and max > p2 - diff) - end - aux(0, maxint) - aux(1, maxint) - aux(minint, -1) - aux(minint // 2, maxint // 2) -end - -for i=1,100 do - assert(math.random(maxint) > 0) - assert(math.random(minint, -1) < 0) -end - -assert(not pcall(math.random, 1, 2, 3)) -- too many arguments - --- empty interval -assert(not pcall(math.random, minint + 1, minint)) -assert(not pcall(math.random, maxint, maxint - 1)) -assert(not pcall(math.random, maxint, minint)) - --- interval too large -assert(not pcall(math.random, minint, 0)) -assert(not pcall(math.random, -1, maxint)) -assert(not pcall(math.random, minint // 2, maxint // 2 + 1)) - - -print('OK') diff --git a/tofix/lua-5.3.4-tests/nextvar.lua b/tofix/lua-5.3.4-tests/nextvar.lua deleted file mode 100644 index ce7312e..0000000 --- a/tofix/lua-5.3.4-tests/nextvar.lua +++ /dev/null @@ -1,631 +0,0 @@ --- $Id: nextvar.lua,v 1.79 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing tables, next, and for') - -local function checkerror (msg, f, ...) - local s, err = pcall(f, ...) - assert(not s and string.find(err, msg)) -end - - -local a = {} - --- make sure table has lots of space in hash part -for i=1,100 do a[i.."+"] = true end -for i=1,100 do a[i.."+"] = nil end --- fill hash part with numeric indices testing size operator -for i=1,100 do - a[i] = true - assert(#a == i) -end - --- testing ipairs -local x = 0 -for k,v in ipairs{10,20,30;x=12} do - x = x + 1 - assert(k == x and v == x * 10) -end - -for _ in ipairs{x=12, y=24} do assert(nil) end - --- test for 'false' x ipair -x = false -local i = 0 -for k,v in ipairs{true,false,true,false} do - i = i + 1 - x = not x - assert(x == v) -end -assert(i == 4) - --- iterator function is always the same -assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) - - -if not T then - (Message or print) - ('\n >>> testC not active: skipping tests for table sizes <<<\n') -else --[ --- testing table sizes - -local function log2 (x) return math.log(x, 2) end - -local function mp2 (n) -- minimum power of 2 >= n - local mp = 2^math.ceil(log2(n)) - assert(n == 0 or (mp/2 < n and n <= mp)) - return mp -end - -local function fb (n) - local r, nn = T.int2fb(n) - assert(r < 256) - return nn -end - --- test fb function -for a = 1, 10000 do -- all numbers up to 10^4 - local n = fb(a) - assert(a <= n and n <= a*1.125) -end -local a = 1024 -- plus a few up to 2 ^30 -local lim = 2^30 -while a < lim do - local n = fb(a) - assert(a <= n and n <= a*1.125) - a = math.ceil(a*1.3) -end - - -local function check (t, na, nh) - local a, h = T.querytab(t) - if a ~= na or h ~= nh then - print(na, nh, a, h) - assert(nil) - end -end - - --- testing C library sizes -do - local s = 0 - for _ in pairs(math) do s = s + 1 end - check(math, 0, mp2(s)) -end - - --- testing constructor sizes -local lim = 40 -local s = 'return {' -for i=1,lim do - s = s..i..',' - local s = s - for k=0,lim do - local t = load(s..'}', '')() - assert(#t == i) - check(t, fb(i), mp2(k)) - s = string.format('%sa%d=%d,', s, k, k) - end -end - - --- tests with unknown number of elements -local a = {} -for i=1,lim do a[i] = i end -- build auxiliary table -for k=0,lim do - local a = {table.unpack(a,1,k)} - assert(#a == k) - check(a, k, 0) - a = {1,2,3,table.unpack(a,1,k)} - check(a, k+3, 0) - assert(#a == k + 3) -end - - --- testing tables dynamically built -local lim = 130 -local a = {}; a[2] = 1; check(a, 0, 1) -a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2) -a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1) -a = {} -for i = 1,lim do - a[i] = 1 - assert(#a == i) - check(a, mp2(i), 0) -end - -a = {} -for i = 1,lim do - a['a'..i] = 1 - assert(#a == 0) - check(a, 0, mp2(i)) -end - -a = {} -for i=1,16 do a[i] = i end -check(a, 16, 0) -do - for i=1,11 do a[i] = nil end - for i=30,50 do a[i] = nil end -- force a rehash (?) - check(a, 0, 8) -- only 5 elements in the table - a[10] = 1 - for i=30,50 do a[i] = nil end -- force a rehash (?) - check(a, 0, 8) -- only 6 elements in the table - for i=1,14 do a[i] = nil end - for i=18,50 do a[i] = nil end -- force a rehash (?) - check(a, 0, 4) -- only 2 elements ([15] and [16]) -end - --- reverse filling -for i=1,lim do - local a = {} - for i=i,1,-1 do a[i] = i end -- fill in reverse - check(a, mp2(i), 0) -end - --- size tests for vararg -lim = 35 -function foo (n, ...) - local arg = {...} - check(arg, n, 0) - assert(select('#', ...) == n) - arg[n+1] = true - check(arg, mp2(n+1), 0) - arg.x = true - check(arg, mp2(n+1), 1) -end -local a = {} -for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end - -end --] - - --- test size operation on empty tables -assert(#{} == 0) -assert(#{nil} == 0) -assert(#{nil, nil} == 0) -assert(#{nil, nil, nil} == 0) -assert(#{nil, nil, nil, nil} == 0) -print'+' - - -local nofind = {} - -a,b,c = 1,2,3 -a,b,c = nil - - --- next uses always the same iteraction function -assert(next{} == next{}) - -local function find (name) - local n,v - while 1 do - n,v = next(_G, n) - if not n then return nofind end - assert(v ~= nil) - if n == name then return v end - end -end - -local function find1 (name) - for n,v in pairs(_G) do - if n==name then return v end - end - return nil -- not found -end - - -assert(print==find("print") and print == find1("print")) -assert(_G["print"]==find("print")) -assert(assert==find1("assert")) -assert(nofind==find("return")) -assert(not find1("return")) -_G["ret" .. "urn"] = nil -assert(nofind==find("return")) -_G["xxx"] = 1 -assert(xxx==find("xxx")) - --- invalid key to 'next' -checkerror("invalid key", next, {10,20}, 3) - --- both 'pairs' and 'ipairs' need an argument -checkerror("bad argument", pairs) -checkerror("bad argument", ipairs) - -print('+') - -a = {} -for i=0,10000 do - if math.fmod(i,10) ~= 0 then - a['x'..i] = i - end -end - -n = {n=0} -for i,v in pairs(a) do - n.n = n.n+1 - assert(i and v and a[i] == v) -end -assert(n.n == 9000) -a = nil - -do -- clear global table - local a = {} - for n,v in pairs(_G) do a[n]=v end - for n,v in pairs(a) do - if not package.loaded[n] and type(v) ~= "function" and - not string.find(n, "^[%u_]") then - _G[n] = nil - end - collectgarbage() - end -end - - --- - -local function checknext (a) - local b = {} - do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end - for k,v in pairs(b) do assert(a[k] == v) end - for k,v in pairs(a) do assert(b[k] == v) end -end - -checknext{1,x=1,y=2,z=3} -checknext{1,2,x=1,y=2,z=3} -checknext{1,2,3,x=1,y=2,z=3} -checknext{1,2,3,4,x=1,y=2,z=3} -checknext{1,2,3,4,5,x=1,y=2,z=3} - -assert(#{} == 0) -assert(#{[-1] = 2} == 0) -assert(#{1,2,3,nil,nil} == 3) -for i=0,40 do - local a = {} - for j=1,i do a[j]=j end - assert(#a == i) -end - --- 'maxn' is now deprecated, but it is easily defined in Lua -function table.maxn (t) - local max = 0 - for k in pairs(t) do - max = (type(k) == 'number') and math.max(max, k) or max - end - return max -end - -assert(table.maxn{} == 0) -assert(table.maxn{["1000"] = true} == 0) -assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5) -assert(table.maxn{[1000] = true} == 1000) -assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi) - -table.maxn = nil - --- int overflow -a = {} -for i=0,50 do a[2^i] = true end -assert(a[#a]) - -print('+') - - --- erasing values -local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3, - [100.3] = 4, [4] = 5} - -local n = 0 -for k, v in pairs( t ) do - n = n+1 - assert(t[k] == v) - t[k] = nil - collectgarbage() - assert(t[k] == nil) -end -assert(n == 5) - - -local function test (a) - assert(not pcall(table.insert, a, 2, 20)); - table.insert(a, 10); table.insert(a, 2, 20); - table.insert(a, 1, -1); table.insert(a, 40); - table.insert(a, #a+1, 50) - table.insert(a, 2, -2) - assert(not pcall(table.insert, a, 0, 20)); - assert(not pcall(table.insert, a, #a + 2, 20)); - assert(table.remove(a,1) == -1) - assert(table.remove(a,1) == -2) - assert(table.remove(a,1) == 10) - assert(table.remove(a,1) == 20) - assert(table.remove(a,1) == 40) - assert(table.remove(a,1) == 50) - assert(table.remove(a,1) == nil) - assert(table.remove(a) == nil) - assert(table.remove(a, #a) == nil) -end - -a = {n=0, [-7] = "ban"} -test(a) -assert(a.n == 0 and a[-7] == "ban") - -a = {[-7] = "ban"}; -test(a) -assert(a.n == nil and #a == 0 and a[-7] == "ban") - -a = {[-1] = "ban"} -test(a) -assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban") - -a = {[0] = "ban"} -assert(#a == 0 and table.remove(a) == "ban" and a[0] == nil) - -table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1) -assert(table.remove(a) == 10) -assert(table.remove(a) == 20) -assert(table.remove(a) == -1) -assert(table.remove(a) == nil) - -a = {'c', 'd'} -table.insert(a, 3, 'a') -table.insert(a, 'b') -assert(table.remove(a, 1) == 'c') -assert(table.remove(a, 1) == 'd') -assert(table.remove(a, 1) == 'a') -assert(table.remove(a, 1) == 'b') -assert(table.remove(a, 1) == nil) -assert(#a == 0 and a.n == nil) - -a = {10,20,30,40} -assert(table.remove(a, #a + 1) == nil) -assert(not pcall(table.remove, a, 0)) -assert(a[#a] == 40) -assert(table.remove(a, #a) == 40) -assert(a[#a] == 30) -assert(table.remove(a, 2) == 20) -assert(a[#a] == 30 and #a == 2) - -do -- testing table library with metamethods - local function test (proxy, t) - for i = 1, 10 do - table.insert(proxy, 1, i) - end - assert(#proxy == 10 and #t == 10) - for i = 1, 10 do - assert(t[i] == 11 - i) - end - table.sort(proxy) - for i = 1, 10 do - assert(t[i] == i and proxy[i] == i) - end - assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10") - for i = 1, 8 do - assert(table.remove(proxy, 1) == i) - end - assert(#proxy == 2 and #t == 2) - local a, b, c = table.unpack(proxy) - assert(a == 9 and b == 10 and c == nil) - end - - -- all virtual - local t = {} - local proxy = setmetatable({}, { - __len = function () return #t end, - __index = t, - __newindex = t, - }) - test(proxy, t) - - -- only __newindex - local count = 0 - t = setmetatable({}, { - __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end}) - test(t, t) - assert(count == 10) -- after first 10, all other sets are not new - - -- no __newindex - t = setmetatable({}, { - __index = function (_,k) return k + 1 end, - __len = function (_) return 5 end}) - assert(table.concat(t, ";") == "2;3;4;5;6") - -end - - -if not T then - (Message or print) - ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') -else --[ - local debug = require'debug' - local tab = {10, 20, 30} - local mt = {} - local u = T.newuserdata(0) - checkerror("table expected", table.insert, u, 40) - checkerror("table expected", table.remove, u) - debug.setmetatable(u, mt) - checkerror("table expected", table.insert, u, 40) - checkerror("table expected", table.remove, u) - mt.__index = tab - checkerror("table expected", table.insert, u, 40) - checkerror("table expected", table.remove, u) - mt.__newindex = tab - checkerror("table expected", table.insert, u, 40) - checkerror("table expected", table.remove, u) - mt.__len = function () return #tab end - table.insert(u, 40) - assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40) - assert(table.remove(u) == 40) - table.insert(u, 1, 50) - assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50) - - mt.__newindex = nil - mt.__len = nil - local tab2 = {} - local u2 = T.newuserdata(0) - debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end}) - table.move(u, 1, 4, 1, u2) - assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4]) - -end -- ] - -print('+') - -a = {} -for i=1,1000 do - a[i] = i; a[i-1] = nil -end -assert(next(a,nil) == 1000 and next(a,1000) == nil) - -assert(next({}) == nil) -assert(next({}, nil) == nil) - -for a,b in pairs{} do error"not here" end -for i=1,0 do error'not here' end -for i=0,1,-1 do error'not here' end -a = nil; for i=1,1 do assert(not a); a=1 end; assert(a) -a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a) - -do - print("testing floats in numeric for") - local a - -- integer count - a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1) - a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1) - a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0) - a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0) - a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1) - - -- float count - a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10) - a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1) - a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1) - a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1) - a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0) - a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0) - a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) -end - --- conversion -a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5) - -do -- checking types - local c - local function checkfloat (i) - assert(math.type(i) == "float") - c = c + 1 - end - - c = 0; for i = 1.0, 10 do checkfloat(i) end - assert(c == 10) - - c = 0; for i = -1, -10, -1.0 do checkfloat(i) end - assert(c == 10) - - local function checkint (i) - assert(math.type(i) == "integer") - c = c + 1 - end - - local m = math.maxinteger - c = 0; for i = m, m - 10, -1 do checkint(i) end - assert(c == 11) - - c = 0; for i = 1, 10.9 do checkint(i) end - assert(c == 10) - - c = 0; for i = 10, 0.001, -1 do checkint(i) end - assert(c == 10) - - c = 0; for i = 1, "10.8" do checkint(i) end - assert(c == 10) - - c = 0; for i = 9, "3.4", -1 do checkint(i) end - assert(c == 6) - - c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end - assert(c == 4) - - c = 0; for i = 100, "96.3", -2 do checkint(i) end - assert(c == 2) - - c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end - assert(c == 10) - - c = 0; for i = -1, -math.huge, -1 do - if i < -10 then break end; checkint(i) - end - assert(c == 10) - - - for i = math.mininteger, -10e100 do assert(false) end - for i = math.maxinteger, 10e100, -1 do assert(false) end - -end - -collectgarbage() - - --- testing generic 'for' - -local function f (n, p) - local t = {}; for i=1,p do t[i] = i*10 end - return function (_,n) - if n > 0 then - n = n-1 - return n, table.unpack(t) - end - end, nil, n -end - -local x = 0 -for n,a,b,c,d in f(5,3) do - x = x+1 - assert(a == 10 and b == 20 and c == 30 and d == nil) -end -assert(x == 5) - - - --- testing __pairs and __ipairs metamethod -a = {} -do - local x,y,z = pairs(a) - assert(type(x) == 'function' and y == a and z == nil) -end - -local function foo (e,i) - assert(e == a) - if i <= 10 then return i+1, i+2 end -end - -local function foo1 (e,i) - i = i + 1 - assert(e == a) - if i <= e.n then return i,a[i] end -end - -setmetatable(a, {__pairs = function (x) return foo, x, 0 end}) - -local i = 0 -for k,v in pairs(a) do - i = i + 1 - assert(k == i and v == k+1) -end - -a.n = 5 -a[3] = 30 - --- testing ipairs with metamethods -a = {n=10} -setmetatable(a, { __index = function (t,k) - if k <= t.n then return k * 10 end - end}) -i = 0 -for k,v in ipairs(a) do - i = i + 1 - assert(k == i and v == i * 10) -end -assert(i == a.n) - -print"OK" diff --git a/tofix/lua-5.3.4-tests/pm.lua b/tofix/lua-5.3.4-tests/pm.lua deleted file mode 100644 index fde39ff..0000000 --- a/tofix/lua-5.3.4-tests/pm.lua +++ /dev/null @@ -1,374 +0,0 @@ --- $Id: pm.lua,v 1.48 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing pattern matching') - -local function checkerror (msg, f, ...) - local s, err = pcall(f, ...) - assert(not s and string.find(err, msg)) -end - - -function f(s, p) - local i,e = string.find(s, p) - if i then return string.sub(s, i, e) end -end - -a,b = string.find('', '') -- empty patterns are tricky -assert(a == 1 and b == 0); -a,b = string.find('alo', '') -assert(a == 1 and b == 0) -a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position -assert(a == 1 and b == 1) -a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle -assert(a == 5 and b == 7) -a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle -assert(a == 9 and b == 11) -a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end -assert(a == 9 and b == 11); -a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position -assert(a == 11 and b == 11) -assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending -assert(string.find('', '\0') == nil) -assert(string.find('alo123alo', '12') == 4) -assert(string.find('alo123alo', '^12') == nil) - -assert(string.match("aaab", ".*b") == "aaab") -assert(string.match("aaa", ".*a") == "aaa") -assert(string.match("b", ".*b") == "b") - -assert(string.match("aaab", ".+b") == "aaab") -assert(string.match("aaa", ".+a") == "aaa") -assert(not string.match("b", ".+b")) - -assert(string.match("aaab", ".?b") == "ab") -assert(string.match("aaa", ".?a") == "aa") -assert(string.match("b", ".?b") == "b") - -assert(f('aloALO', '%l*') == 'alo') -assert(f('aLo_ALO', '%a*') == 'aLo') - -assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") - -assert(f('aaab', 'a*') == 'aaa'); -assert(f('aaa', '^.*$') == 'aaa'); -assert(f('aaa', 'b*') == ''); -assert(f('aaa', 'ab*a') == 'aa') -assert(f('aba', 'ab*a') == 'aba') -assert(f('aaab', 'a+') == 'aaa') -assert(f('aaa', '^.+$') == 'aaa') -assert(f('aaa', 'b+') == nil) -assert(f('aaa', 'ab+a') == nil) -assert(f('aba', 'ab+a') == 'aba') -assert(f('a$a', '.$') == 'a') -assert(f('a$a', '.%$') == 'a$') -assert(f('a$a', '.$.') == 'a$a') -assert(f('a$a', '$$') == nil) -assert(f('a$b', 'a$') == nil) -assert(f('a$a', '$') == '') -assert(f('', 'b*') == '') -assert(f('aaa', 'bb*') == nil) -assert(f('aaab', 'a-') == '') -assert(f('aaa', '^.-$') == 'aaa') -assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') -assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') -assert(f('alo xo', '.o$') == 'xo') -assert(f(' \n isto assim', '%S%S*') == 'isto') -assert(f(' \n isto assim', '%S*$') == 'assim') -assert(f(' \n isto assim', '[a-z]*$') == 'assim') -assert(f('um caracter ? extra', '[^%sa-z]') == '?') -assert(f('', 'a?') == '') -assert(f('', '?') == '') -assert(f('bl', '?b?l?') == 'bl') -assert(f(' bl', '?b?l?') == '') -assert(f('aa', '^aa?a?a') == 'aa') -assert(f(']]]b', '[^]]') == '') -assert(f("0alo alo", "%x*") == "0a") -assert(f("alo alo", "%C+") == "alo alo") -print('+') - - -function f1(s, p) - p = string.gsub(p, "%%([0-9])", function (s) - return "%" .. (tonumber(s)+1) - end) - p = string.gsub(p, "^(^?)", "%1()", 1) - p = string.gsub(p, "($?)$", "()%1", 1) - local t = {string.match(s, p)} - return string.sub(s, t[1], t[#t] - 1) -end - -assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") -assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') -assert(f1('=======', '^(=*)=%1$') == '=======') -assert(string.match('==========', '^([=]*)=%1$') == nil) - -local function range (i, j) - if i <= j then - return i, range(i+1, j) - end -end - -local abc = string.char(range(0, 255)); - -assert(string.len(abc) == 256) - -function strset (p) - local res = {s=''} - string.gsub(abc, p, function (c) res.s = res.s .. c end) - return res.s -end; - -assert(string.len(strset('[\200-\210]')) == 11) - -assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz") -assert(strset('[a-z%d]') == strset('[%da-uu-z]')) -assert(strset('[a-]') == "-a") -assert(strset('[^%W]') == strset('[%w]')) -assert(strset('[]%%]') == '%]') -assert(strset('[a%-z]') == '-az') -assert(strset('[%^%[%-a%]%-b]') == '-[]^ab') -assert(strset('%Z') == strset('[\1-\255]')) -assert(strset('.') == strset('[\1-\255%z]')) -print('+'); - -assert(string.match("alo xyzK", "(%w+)K") == "xyz") -assert(string.match("254 K", "(%d*)K") == "") -assert(string.match("alo ", "(%w*)$") == "") -assert(string.match("alo ", "(%w+)$") == nil) -assert(string.find("(lo)", "%(") == 1) -local a, b, c, d, e = string.match("lo alo", "^(((.).).* (%w*))$") -assert(a == 'lo alo' and b == 'l' and c == '' and d == 'alo' and e == nil) -a, b, c, d = string.match('0123456789', '(.+(.?)())') -assert(a == '0123456789' and b == '' and c == 11 and d == nil) -print('+') - -assert(string.gsub('lo lo', '', 'x') == 'xlo xlo') -assert(string.gsub('alo lo ', ' +$', '') == 'alo lo') -- trim -assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim -assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') -t = "ab d" -a, b = string.gsub(t, '(.)', '%1@') -assert('@'..a == string.gsub(t, '', '@') and b == 5) -a, b = string.gsub('abd', '(.)', '%0@', 2) -assert(a == 'a@b@d' and b == 2) -assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') -assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == - "xyz=abc-abc=xyz") -assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") -assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") -assert(string.gsub('', '$', '\0') == '\0') -assert(string.gsub('', '^', 'r') == 'r') -assert(string.gsub('', '$', 'r') == 'r') -print('+') - - -do -- new (5.3.3) semantics for empty matches - assert(string.gsub("a b cd", " *", "-") == "-a-b-c-d-") - - local res = "" - local sub = "a \nbc\t\td" - local i = 1 - for p, e in string.gmatch(sub, "()%s*()") do - res = res .. string.sub(sub, i, p - 1) .. "-" - i = e - end - assert(res == "-a-b-c-d-") -end - - -assert(string.gsub("um (dois) tres (quatro)", "(%(%w+%))", string.upper) == - "um (DOIS) tres (QUATRO)") - -do - local function setglobal (n,v) rawset(_G, n, v) end - string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) - assert(_G.a=="roberto" and _G.roberto=="a") -end - -function f(a,b) return string.gsub(a,'.',b) end -assert(string.gsub("trocar tudo em |teste|b| |beleza|al|", "|([^|]*)|([^|]*)|", f) == - "trocar tudo em bbbbb alalalalalal") - -local function dostring (s) return load(s, "")() or "" end -assert(string.gsub("alo $a='x'$ novamente $return a$", - "$([^$]*)%$", - dostring) == "alo novamente x") - -x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", - "$([^$]*)%$", dostring) -assert(x == ' assim vai para ALO') - -t = {} -s = 'a alo jose joao' -r = string.gsub(s, '()(%w+)()', function (a,w,b) - assert(string.len(w) == b-a); - t[a] = b-a; - end) -assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) - - -function isbalanced (s) - return string.find(string.gsub(s, "%b()", ""), "[()]") == nil -end - -assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) -assert(not isbalanced("(9 ((8) 7) a b (\0 c) a")) -assert(string.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo') - - -local t = {"apple", "orange", "lime"; n=0} -assert(string.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end) - == "apple and orange and lime") - -t = {n=0} -string.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end) -assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3) - -t = {n=0} -assert(string.gsub("first second word", "%w+", - function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word") -assert(t[1] == "first" and t[2] == "second" and t[3] == nil) - -checkerror("invalid replacement value %(a table%)", - string.gsub, "alo", ".", {a = {}}) -checkerror("invalid capture index %%2", string.gsub, "alo", ".", "%2") -checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a") -checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a") -checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x") - --- bug since 2.5 (C-stack overflow) -do - local function f (size) - local s = string.rep("a", size) - local p = string.rep(".?", size) - return pcall(string.match, s, p) - end - local r, m = f(80) - assert(r and #m == 80) - r, m = f(200000) - assert(not r and string.find(m, "too complex")) -end - -if not _soft then - print("big strings") - local a = string.rep('a', 300000) - assert(string.find(a, '^a*.?$')) - assert(not string.find(a, '^a*.?b$')) - assert(string.find(a, '^a-.?$')) - - -- bug in 5.1.2 - a = string.rep('a', 10000) .. string.rep('b', 10000) - assert(not pcall(string.gsub, a, 'b')) -end - --- recursive nest of gsubs -function rev (s) - return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) -end - -local x = "abcdef" -assert(rev(rev(x)) == x) - - --- gsub with tables -assert(string.gsub("alo alo", ".", {}) == "alo alo") -assert(string.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo") -assert(string.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo") -assert(string.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo") - -assert(string.gsub("alo alo", "().", {'x','yy','zzz'}) == "xyyzzz alo") - -t = {}; setmetatable(t, {__index = function (t,s) return string.upper(s) end}) -assert(string.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI") - - --- tests for gmatch -local a = 0 -for i in string.gmatch('abcde', '()') do assert(i == a+1); a=i end -assert(a==6) - -t = {n=0} -for w in string.gmatch("first second word", "%w+") do - t.n=t.n+1; t[t.n] = w -end -assert(t[1] == "first" and t[2] == "second" and t[3] == "word") - -t = {3, 6, 9} -for i in string.gmatch ("xuxx uu ppar r", "()(.)%2") do - assert(i == table.remove(t, 1)) -end -assert(#t == 0) - -t = {} -for i,j in string.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do - t[tonumber(i)] = tonumber(j) -end -a = 0 -for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end -assert(a == 3) - - --- tests for `%f' (`frontiers') - -assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") -assert(string.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[") -assert(string.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3") -assert(string.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.") -assert(string.gsub("function", "%f[\1-\255]%w", ".") == ".unction") -assert(string.gsub("function", "%f[^\1-\255]", ".") == "function.") - -assert(string.find("a", "%f[a]") == 1) -assert(string.find("a", "%f[^%z]") == 1) -assert(string.find("a", "%f[^%l]") == 2) -assert(string.find("aba", "%f[a%z]") == 3) -assert(string.find("aba", "%f[%z]") == 4) -assert(not string.find("aba", "%f[%l%z]")) -assert(not string.find("aba", "%f[^%l%z]")) - -local i, e = string.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]") -assert(i == 2 and e == 5) -local k = string.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])") -assert(k == 'alo ') - -local a = {1, 5, 9, 14, 17,} -for k in string.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do - assert(table.remove(a, 1) == k) -end -assert(#a == 0) - - --- malformed patterns -local function malform (p, m) - m = m or "malformed" - local r, msg = pcall(string.find, "a", p) - assert(not r and string.find(msg, m)) -end - -malform("(.", "unfinished capture") -malform(".)", "invalid pattern capture") -malform("[a") -malform("[]") -malform("[^]") -malform("[a%]") -malform("[a%") -malform("%b") -malform("%ba") -malform("%") -malform("%f", "missing") - --- \0 in patterns -assert(string.match("ab\0\1\2c", "[\0-\2]+") == "\0\1\2") -assert(string.match("ab\0\1\2c", "[\0-\0]+") == "\0") -assert(string.find("b$a", "$\0?") == 2) -assert(string.find("abc\0efg", "%\0") == 4) -assert(string.match("abc\0efg\0\1e\1g", "%b\0\1") == "\0efg\0\1e\1") -assert(string.match("abc\0\0\0", "%\0+") == "\0\0\0") -assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") - --- magic char after \0 -assert(string.find("abc\0\0","\0.") == 4) -assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) - -print('OK') - diff --git a/tofix/lua-5.3.4-tests/sort.lua b/tofix/lua-5.3.4-tests/sort.lua deleted file mode 100644 index d52feee..0000000 --- a/tofix/lua-5.3.4-tests/sort.lua +++ /dev/null @@ -1,310 +0,0 @@ --- $Id: sort.lua,v 1.38 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print "testing (parts of) table library" - -print "testing unpack" - -local unpack = table.unpack - -local maxI = math.maxinteger -local minI = math.mininteger - - -local function checkerror (msg, f, ...) - local s, err = pcall(f, ...) - assert(not s and string.find(err, msg)) -end - - -checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) - -local x,y,z,a,n -a = {}; lim = _soft and 200 or 2000 -for i=1, lim do a[i]=i end -assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) -x = unpack(a) -assert(x == 1) -x = {unpack(a)} -assert(#x == lim and x[1] == 1 and x[lim] == lim) -x = {unpack(a, lim-2)} -assert(#x == 3 and x[1] == lim-2 and x[3] == lim) -x = {unpack(a, 10, 6)} -assert(next(x) == nil) -- no elements -x = {unpack(a, 11, 10)} -assert(next(x) == nil) -- no elements -x,y = unpack(a, 10, 10) -assert(x == 10 and y == nil) -x,y,z = unpack(a, 10, 11) -assert(x == 10 and y == 11 and z == nil) -a,x = unpack{1} -assert(a==1 and x==nil) -a,x = unpack({1,2}, 1, 1) -assert(a==1 and x==nil) - -do - local maxi = (1 << 31) - 1 -- maximum value for an int (usually) - local mini = -(1 << 31) -- minimum value for an int (usually) - checkerror("too many results", unpack, {}, 0, maxi) - checkerror("too many results", unpack, {}, 1, maxi) - checkerror("too many results", unpack, {}, 0, maxI) - checkerror("too many results", unpack, {}, 1, maxI) - checkerror("too many results", unpack, {}, mini, maxi) - checkerror("too many results", unpack, {}, -maxi, maxi) - checkerror("too many results", unpack, {}, minI, maxI) - unpack({}, maxi, 0) - unpack({}, maxi, 1) - unpack({}, maxI, minI) - pcall(unpack, {}, 1, maxi + 1) - local a, b = unpack({[maxi] = 20}, maxi, maxi) - assert(a == 20 and b == nil) - a, b = unpack({[maxi] = 20}, maxi - 1, maxi) - assert(a == nil and b == 20) - local t = {[maxI - 1] = 12, [maxI] = 23} - a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23) - a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil) - a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil) - t = {[minI] = 12.3, [minI + 1] = 23.5} - a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5) - a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil) - a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil) -end - -do -- length is not an integer - local t = setmetatable({}, {__len = function () return 'abc' end}) - assert(#t == 'abc') - checkerror("object length is not an integer", table.insert, t, 1) -end - -print "testing pack" - -a = table.pack() -assert(a[1] == nil and a.n == 0) - -a = table.pack(table) -assert(a[1] == table and a.n == 1) - -a = table.pack(nil, nil, nil, nil) -assert(a[1] == nil and a.n == 4) - - --- testing move -do - - checkerror("table expected", table.move, 1, 2, 3, 4) - - local function eqT (a, b) - for k, v in pairs(a) do assert(b[k] == v) end - for k, v in pairs(b) do assert(a[k] == v) end - end - - local a = table.move({10,20,30}, 1, 3, 2) -- move forward - eqT(a, {10,10,20,30}) - - -- move forward with overlap of 1 - a = table.move({10, 20, 30}, 1, 3, 3) - eqT(a, {10, 20, 10, 20, 30}) - - -- moving to the same table (not being explicit about it) - a = {10, 20, 30, 40} - table.move(a, 1, 4, 2, a) - eqT(a, {10, 10, 20, 30, 40}) - - a = table.move({10,20,30}, 2, 3, 1) -- move backward - eqT(a, {20,30,30}) - - a = {} -- move to new table - assert(table.move({10,20,30}, 1, 3, 1, a) == a) - eqT(a, {10,20,30}) - - a = {} - assert(table.move({10,20,30}, 1, 0, 3, a) == a) -- empty move (no move) - eqT(a, {}) - - a = table.move({10,20,30}, 1, 10, 1) -- move to the same place - eqT(a, {10,20,30}) - - -- moving on the fringes - a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3}, - maxI - 2, maxI, -10, {}) - eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) - - a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3}, - minI, minI + 2, -10, {}) - eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) - - a = table.move({45}, 1, 1, maxI) - eqT(a, {45, [maxI] = 45}) - - a = table.move({[maxI] = 100}, maxI, maxI, minI) - eqT(a, {[minI] = 100, [maxI] = 100}) - - a = table.move({[minI] = 100}, minI, minI, maxI) - eqT(a, {[minI] = 100, [maxI] = 100}) - - a = setmetatable({}, { - __index = function (_,k) return k * 10 end, - __newindex = error}) - local b = table.move(a, 1, 10, 3, {}) - eqT(a, {}) - eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100}) - - b = setmetatable({""}, { - __index = error, - __newindex = function (t,k,v) - t[1] = string.format("%s(%d,%d)", t[1], k, v) - end}) - table.move(a, 10, 13, 3, b) - assert(b[1] == "(3,100)(4,110)(5,120)(6,130)") - local stat, msg = pcall(table.move, b, 10, 13, 3, b) - assert(not stat and msg == b) -end - -do - -- for very long moves, just check initial accesses and interrupt - -- move with an error - local function checkmove (f, e, t, x, y) - local pos1, pos2 - local a = setmetatable({}, { - __index = function (_,k) pos1 = k end, - __newindex = function (_,k) pos2 = k; error() end, }) - local st, msg = pcall(table.move, a, f, e, t) - assert(not st and not msg and pos1 == x and pos2 == y) - end - checkmove(1, maxI, 0, 1, 0) - checkmove(0, maxI - 1, 1, maxI - 1, maxI) - checkmove(minI, -2, -5, -2, maxI - 6) - checkmove(minI + 1, -1, -2, -1, maxI - 3) - checkmove(minI, -2, 0, minI, 0) -- non overlapping - checkmove(minI + 1, -1, 1, minI + 1, 1) -- non overlapping -end - -checkerror("too many", table.move, {}, 0, maxI, 1) -checkerror("too many", table.move, {}, -1, maxI - 1, 1) -checkerror("too many", table.move, {}, minI, -1, 1) -checkerror("too many", table.move, {}, minI, maxI, 1) -checkerror("wrap around", table.move, {}, 1, maxI, 2) -checkerror("wrap around", table.move, {}, 1, 2, maxI) -checkerror("wrap around", table.move, {}, minI, -2, 2) - - -print"testing sort" - - --- strange lengths -local a = setmetatable({}, {__len = function () return -1 end}) -assert(#a == -1) -table.sort(a, error) -- should not compare anything -a = setmetatable({}, {__len = function () return maxI end}) -checkerror("too big", table.sort, a) - --- test checks for invalid order functions -local function check (t) - local function f(a, b) assert(a and b); return true end - checkerror("invalid order function", table.sort, t, f) -end - -check{1,2,3,4} -check{1,2,3,4,5} -check{1,2,3,4,5,6} - - -function check (a, f) - f = f or function (x,y) return x 'alo\0alo\0') -assert('alo' < 'alo\0') -assert('alo\0' > 'alo') -assert('\0' < '\1') -assert('\0\0' < '\0\1') -assert('\1\0a\0a' <= '\1\0a\0a') -assert(not ('\1\0a\0b' <= '\1\0a\0a')) -assert('\0\0\0' < '\0\0\0\0') -assert(not('\0\0\0\0' < '\0\0\0')) -assert('\0\0\0' <= '\0\0\0\0') -assert(not('\0\0\0\0' <= '\0\0\0')) -assert('\0\0\0' <= '\0\0\0') -assert('\0\0\0' >= '\0\0\0') -assert(not ('\0\0b' < '\0\0a\0')) - --- testing string.sub -assert(string.sub("123456789",2,4) == "234") -assert(string.sub("123456789",7) == "789") -assert(string.sub("123456789",7,6) == "") -assert(string.sub("123456789",7,7) == "7") -assert(string.sub("123456789",0,0) == "") -assert(string.sub("123456789",-10,10) == "123456789") -assert(string.sub("123456789",1,9) == "123456789") -assert(string.sub("123456789",-10,-20) == "") -assert(string.sub("123456789",-1) == "9") -assert(string.sub("123456789",-4) == "6789") -assert(string.sub("123456789",-6, -4) == "456") -assert(string.sub("123456789", mini, -4) == "123456") -assert(string.sub("123456789", mini, maxi) == "123456789") -assert(string.sub("123456789", mini, mini) == "") -assert(string.sub("\000123456789",3,5) == "234") -assert(("\000123456789"):sub(8) == "789") - --- testing string.find -assert(string.find("123456789", "345") == 3) -a,b = string.find("123456789", "345") -assert(string.sub("123456789", a, b) == "345") -assert(string.find("1234567890123456789", "345", 3) == 3) -assert(string.find("1234567890123456789", "345", 4) == 13) -assert(string.find("1234567890123456789", "346", 4) == nil) -assert(string.find("1234567890123456789", ".45", -9) == 13) -assert(string.find("abcdefg", "\0", 5, 1) == nil) -assert(string.find("", "") == 1) -assert(string.find("", "", 1) == 1) -assert(not string.find("", "", 2)) -assert(string.find('', 'aaa', 1) == nil) -assert(('alo(.)alo'):find('(.)', 1, 1) == 4) - -assert(string.len("") == 0) -assert(string.len("\0\0\0") == 3) -assert(string.len("1234567890") == 10) - -assert(#"" == 0) -assert(#"\0\0\0" == 3) -assert(#"1234567890" == 10) - --- testing string.byte/string.char -assert(string.byte("a") == 97) -assert(string.byte("\xe4") > 127) -assert(string.byte(string.char(255)) == 255) -assert(string.byte(string.char(0)) == 0) -assert(string.byte("\0") == 0) -assert(string.byte("\0\0alo\0x", -1) == string.byte('x')) -assert(string.byte("ba", 2) == 97) -assert(string.byte("\n\n", 2, -1) == 10) -assert(string.byte("\n\n", 2, 2) == 10) -assert(string.byte("") == nil) -assert(string.byte("hi", -3) == nil) -assert(string.byte("hi", 3) == nil) -assert(string.byte("hi", 9, 10) == nil) -assert(string.byte("hi", 2, 1) == nil) -assert(string.char() == "") -assert(string.char(0, 255, 0) == "\0\255\0") -assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0") -assert(string.char(string.byte("\xe4l\0u", 1, -1)) == "\xe4l\0u") -assert(string.char(string.byte("\xe4l\0u", 1, 0)) == "") -assert(string.char(string.byte("\xe4l\0u", -10, 100)) == "\xe4l\0u") - -assert(string.upper("ab\0c") == "AB\0C") -assert(string.lower("\0ABCc%$") == "\0abcc%$") -assert(string.rep('teste', 0) == '') -assert(string.rep('ts\00t', 2) == 'ts\0tts\000t') -assert(string.rep('', 10) == '') - -if string.packsize("i") == 4 then - -- result length would be 2^31 (int overflow) - checkerror("too large", string.rep, 'aa', (1 << 30)) - checkerror("too large", string.rep, 'a', (1 << 30), ',') -end - --- repetitions with separator -assert(string.rep('teste', 0, 'xuxu') == '') -assert(string.rep('teste', 1, 'xuxu') == 'teste') -assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1') -assert(string.rep('', 10, '.') == string.rep('.', 9)) -assert(not pcall(string.rep, "aa", maxi // 2 + 10)) -assert(not pcall(string.rep, "", maxi // 2 + 10, "aa")) - -assert(string.reverse"" == "") -assert(string.reverse"\0\1\2\3" == "\3\2\1\0") -assert(string.reverse"\0001234" == "4321\0") - -for i=0,30 do assert(string.len(string.rep('a', i)) == i) end - -assert(type(tostring(nil)) == 'string') -assert(type(tostring(12)) == 'string') -assert(string.find(tostring{}, 'table:')) -assert(string.find(tostring(print), 'function:')) -assert(#tostring('\0') == 1) -assert(tostring(true) == "true") -assert(tostring(false) == "false") -assert(tostring(-1203) == "-1203") -assert(tostring(1203.125) == "1203.125") -assert(tostring(-0.5) == "-0.5") -assert(tostring(-32767) == "-32767") -if math.tointeger(2147483647) then -- no overflow? (32 bits) - assert(tostring(-2147483647) == "-2147483647") -end -if math.tointeger(4611686018427387904) then -- no overflow? (64 bits) - assert(tostring(4611686018427387904) == "4611686018427387904") - assert(tostring(-4611686018427387904) == "-4611686018427387904") -end - -if tostring(0.0) == "0.0" then -- "standard" coercion float->string - assert('' .. 12 == '12' and 12.0 .. '' == '12.0') - assert(tostring(-1203 + 0.0) == "-1203.0") -else -- compatible coercion - assert(tostring(0.0) == "0") - assert('' .. 12 == '12' and 12.0 .. '' == '12') - assert(tostring(-1203 + 0.0) == "-1203") -end - - -x = '"lo"\n\\' -assert(string.format('%q%s', x, x) == '"\\"lo\\"\\\n\\\\""lo"\n\\') -assert(string.format('%q', "\0") == [["\0"]]) -assert(load(string.format('return %q', x))() == x) -x = "\0\1\0023\5\0009" -assert(load(string.format('return %q', x))() == x) -assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) == - "\0\xe4\0b8c\0") -assert(string.format('') == "") -assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == - string.format("%c%c%c%c", 34, 48, 90, 100)) -assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be') -assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") -assert(tonumber(string.format("%f", 10.3)) == 10.3) -x = string.format('"%-50s"', 'a') -assert(#x == 52) -assert(string.sub(x, 1, 4) == '"a ') - -assert(string.format("-%.20s.20s", string.rep("%", 2000)) == - "-"..string.rep("%", 20)..".20s") -assert(string.format('"-%20s.20s"', string.rep("%", 2000)) == - string.format("%q", "-"..string.rep("%", 2000)..".20s")) - -do - local function checkQ (v) - local s = string.format("%q", v) - local nv = load("return " .. s)() - assert(v == nv and math.type(v) == math.type(nv)) - end - checkQ("\0\0\1\255\u{234}") - checkQ(math.maxinteger) - checkQ(math.mininteger) - checkQ(math.pi) - checkQ(0.1) - checkQ(true) - checkQ(nil) - checkQ(false) - checkerror("no literal", string.format, "%q", {}) -end - -assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") -checkerror("contains zeros", string.format, "%10s", "\0") - --- format x tostring -assert(string.format("%s %s", nil, true) == "nil true") -assert(string.format("%s %.4s", false, true) == "false true") -assert(string.format("%.3s %.3s", false, true) == "fal tru") -local m = setmetatable({}, {__tostring = function () return "hello" end, - __name = "hi"}) -assert(string.format("%s %.10s", m, m) == "hello hello") -getmetatable(m).__tostring = nil -- will use '__name' from now on -assert(string.format("%.4s", m) == "hi: ") - -getmetatable(m).__tostring = function () return {} end -checkerror("'__tostring' must return a string", tostring, m) - - -assert(string.format("%x", 0.0) == "0") -assert(string.format("%02x", 0.0) == "00") -assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF") -assert(string.format("%+08d", 31501) == "+0031501") -assert(string.format("%+08d", -30927) == "-0030927") - - -do -- longest number that can be formatted - local i = 1 - local j = 10000 - while i + 1 < j do -- binary search for maximum finite float - local m = (i + j) // 2 - if 10^m < math.huge then i = m else j = m end - end - assert(10^i < math.huge and 10^j == math.huge) - local s = string.format('%.99f', -(10^i)) - assert(string.len(s) >= i + 101) - assert(tonumber(s) == -(10^i)) -end - - --- testing large numbers for format -do -- assume at least 32 bits - local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits - assert(string.sub(string.format("%8x", -1), -8) == "ffffffff") - assert(string.format("%x", max) == "7fffffff") - assert(string.sub(string.format("%x", min), -8) == "80000000") - assert(string.format("%d", max) == "2147483647") - assert(string.format("%d", min) == "-2147483648") - assert(string.format("%u", 0xffffffff) == "4294967295") - assert(string.format("%o", 0xABCD) == "125715") - - max, min = 0x7fffffffffffffff, -0x8000000000000000 - if max > 2.0^53 then -- only for 64 bits - assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff") - assert(string.format("0x%8X", 0x8f000003) == "0x8F000003") - assert(string.format("%d", 2^53) == "9007199254740992") - assert(string.format("%i", -2^53) == "-9007199254740992") - assert(string.format("%x", max) == "7fffffffffffffff") - assert(string.format("%x", min) == "8000000000000000") - assert(string.format("%d", max) == "9223372036854775807") - assert(string.format("%d", min) == "-9223372036854775808") - assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615") - assert(tostring(1234567890123) == '1234567890123') - end -end - - -do print("testing 'format %a %A'") - local function matchhexa (n) - local s = string.format("%a", n) - -- result matches ISO C requirements - assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$")) - assert(tonumber(s) == n) -- and has full precision - s = string.format("%A", n) - assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$")) - assert(tonumber(s) == n) - end - for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30, - -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do - matchhexa(n) - end - - assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) - assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) - - if not _port then -- test inf, -inf, NaN, and -0.0 - assert(string.find(string.format("%a", 1/0), "^inf")) - assert(string.find(string.format("%A", -1/0), "^%-INF")) - assert(string.find(string.format("%a", 0/0), "^%-?nan")) - assert(string.find(string.format("%a", -0.0), "^%-0x0")) - end - - if not pcall(string.format, "%.3a", 0) then - (Message or print)("\n >>> modifiers for format '%a' not available <<<\n") - else - assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) - assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) - end -end - - --- errors in format - -local function check (fmt, msg) - checkerror(msg, string.format, fmt, 10) -end - -local aux = string.rep('0', 600) -check("%100.3d", "too long") -check("%1"..aux..".3d", "too long") -check("%1.100d", "too long") -check("%10.1"..aux.."004d", "too long") -check("%t", "invalid option") -check("%"..aux.."d", "repeated flags") -check("%d %d", "no value") - - -assert(load("return 1\n--comment without ending EOL")() == 1) - - -checkerror("table expected", table.concat, 3) -assert(table.concat{} == "") -assert(table.concat({}, 'x') == "") -assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2") -local a = {}; for i=1,300 do a[i] = "xuxu" end -assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300)) -assert(table.concat(a, "b", 20, 20) == "xuxu") -assert(table.concat(a, "", 20, 21) == "xuxuxuxu") -assert(table.concat(a, "x", 22, 21) == "") -assert(table.concat(a, "3", 299) == "xuxu3xuxu") -assert(table.concat({}, "x", maxi, maxi - 1) == "") -assert(table.concat({}, "x", mini + 1, mini) == "") -assert(table.concat({}, "x", maxi, mini) == "") -assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo") -assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi) - == "y-alo") - -assert(not pcall(table.concat, {"a", "b", {}})) - -a = {"a","b","c"} -assert(table.concat(a, ",", 1, 0) == "") -assert(table.concat(a, ",", 1, 1) == "a") -assert(table.concat(a, ",", 1, 2) == "a,b") -assert(table.concat(a, ",", 2) == "b,c") -assert(table.concat(a, ",", 3) == "c") -assert(table.concat(a, ",", 4) == "") - -if not _port then - - local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" } - local function trylocale (w) - for i = 1, #locales do - if os.setlocale(locales[i], w) then - print(string.format("'%s' locale set to '%s'", w, locales[i])) - return locales[i] - end - end - print(string.format("'%s' locale not found", w)) - return false - end - - if trylocale("collate") then - assert("alo" < "lo" and "lo" < "amo") - end - - if trylocale("ctype") then - assert(string.gsub("", "%a", "x") == "xxxxx") - assert(string.gsub("", "%l", "x") == "xx") - assert(string.gsub("", "%u", "x") == "xx") - assert(string.upper"{xuxu}o" == "{XUXU}O") - end - - os.setlocale("C") - assert(os.setlocale() == 'C') - assert(os.setlocale(nil, "numeric") == 'C') - -end - - --- bug in Lua 5.3.2 --- 'gmatch' iterator does not work across coroutines -do - local f = string.gmatch("1 2 3 4 5", "%d+") - assert(f() == "1") - co = coroutine.wrap(f) - assert(co() == "2") -end - -print('OK') - diff --git a/tofix/lua-5.3.4-tests/tpack.lua b/tofix/lua-5.3.4-tests/tpack.lua deleted file mode 100644 index 96bb4da..0000000 --- a/tofix/lua-5.3.4-tests/tpack.lua +++ /dev/null @@ -1,322 +0,0 @@ --- $Id: tpack.lua,v 1.13 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -local pack = string.pack -local packsize = string.packsize -local unpack = string.unpack - -print "testing pack/unpack" - --- maximum size for integers -local NB = 16 - -local sizeshort = packsize("h") -local sizeint = packsize("i") -local sizelong = packsize("l") -local sizesize_t = packsize("T") -local sizeLI = packsize("j") -local sizefloat = packsize("f") -local sizedouble = packsize("d") -local sizenumber = packsize("n") -local little = (pack("i2", 1) == "\1\0") -local align = packsize("!xXi16") - -assert(1 <= sizeshort and sizeshort <= sizeint and sizeint <= sizelong and - sizefloat <= sizedouble) - -print("platform:") -print(string.format( - "\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\n\z - \tlua Integer %d, lua Number %d", - sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, - sizeLI, sizenumber)) -print("\t" .. (little and "little" or "big") .. " endian") -print("\talignment: " .. align) - - --- check errors in arguments -function checkerror (msg, f, ...) - local status, err = pcall(f, ...) - -- print(status, err, msg) - assert(not status and string.find(err, msg)) -end - --- minimum behavior for integer formats -assert(unpack("B", pack("B", 0xff)) == 0xff) -assert(unpack("b", pack("b", 0x7f)) == 0x7f) -assert(unpack("b", pack("b", -0x80)) == -0x80) - -assert(unpack("H", pack("H", 0xffff)) == 0xffff) -assert(unpack("h", pack("h", 0x7fff)) == 0x7fff) -assert(unpack("h", pack("h", -0x8000)) == -0x8000) - -assert(unpack("L", pack("L", 0xffffffff)) == 0xffffffff) -assert(unpack("l", pack("l", 0x7fffffff)) == 0x7fffffff) -assert(unpack("l", pack("l", -0x80000000)) == -0x80000000) - - -for i = 1, NB do - -- small numbers with signal extension ("\xFF...") - local s = string.rep("\xff", i) - assert(pack("i" .. i, -1) == s) - assert(packsize("i" .. i) == #s) - assert(unpack("i" .. i, s) == -1) - - -- small unsigned number ("\0...\xAA") - s = "\xAA" .. string.rep("\0", i - 1) - assert(pack("I" .. i, 0xAA) == s:reverse()) - assert(unpack(">I" .. i, s:reverse()) == 0xAA) -end - -do - local lnum = 0x13121110090807060504030201 - local s = pack("i" .. i, ("\xFF"):rep(i - sizeLI) .. s:reverse()) == -lnum) - assert(unpack("i" .. i, "\1" .. ("\x00"):rep(i - 1)) - end -end - -for i = 1, sizeLI do - local lstr = "\1\2\3\4\5\6\7\8\9\10\11\12\13" - local lnum = 0x13121110090807060504030201 - local n = lnum & (~(-1 << (i * 8))) - local s = string.sub(lstr, 1, i) - assert(pack("i" .. i, n) == s:reverse()) - assert(unpack(">i" .. i, s:reverse()) == n) -end - --- sign extension -do - local u = 0xf0 - for i = 1, sizeLI - 1 do - assert(unpack("I"..i, "\xf0"..("\xff"):rep(i - 1)) == u) - u = u * 256 + 0xff - end -end - --- mixed endianness -do - assert(pack(">i2 i2", "\10\0\0\20") - assert(a == 10 and b == 20) - assert(pack("=i4", 2001) == pack("i4", 2001)) -end - -print("testing invalid formats") - -checkerror("out of limits", pack, "i0", 0) -checkerror("out of limits", pack, "i" .. NB + 1, 0) -checkerror("out of limits", pack, "!" .. NB + 1, 0) -checkerror("%(17%) out of limits %[1,16%]", pack, "Xi" .. NB + 1) -checkerror("invalid format option 'r'", pack, "i3r", 0) -checkerror("16%-byte integer", unpack, "i16", string.rep('\3', 16)) -checkerror("not power of 2", pack, "!4i3", 0); -checkerror("missing size", pack, "c", "") -checkerror("variable%-length format", packsize, "s") -checkerror("variable%-length format", packsize, "z") - --- overflow in option size (error will be in digit after limit) -checkerror("invalid format", packsize, "c1" .. string.rep("0", 40)) - -if packsize("i") == 4 then - -- result would be 2^31 (2^3 repetitions of 2^28 strings) - local s = string.rep("c268435456", 2^3) - checkerror("too large", packsize, s) - -- one less is OK - s = string.rep("c268435456", 2^3 - 1) .. "c268435455" - assert(packsize(s) == 0x7fffffff) -end - --- overflow in packing -for i = 1, sizeLI - 1 do - local umax = (1 << (i * 8)) - 1 - local max = umax >> 1 - local min = ~max - checkerror("overflow", pack, "I" .. i, umax + 1) - - checkerror("overflow", pack, ">i" .. i, umax) - checkerror("overflow", pack, ">i" .. i, max + 1) - checkerror("overflow", pack, "i" .. i, pack(">i" .. i, max)) == max) - assert(unpack("I" .. i, pack(">I" .. i, umax)) == umax) -end - --- Lua integer size -assert(unpack(">j", pack(">j", math.maxinteger)) == math.maxinteger) -assert(unpack("f", 24)) -end - -print "testing pack/unpack of floating-point numbers" - -for _, n in ipairs{0, -1.1, 1.9, 1/0, -1/0, 1e20, -1e20, 0.1, 2000.7} do - assert(unpack("n", pack("n", n)) == n) - assert(unpack("n", pack(">n", n)) == n) - assert(pack("f", n):reverse()) - assert(pack(">d", n) == pack("f", pack(">f", n)) == n) - assert(unpack("d", pack(">d", n)) == n) -end - -print "testing pack/unpack of strings" -do - local s = string.rep("abc", 1000) - assert(pack("zB", s, 247) == s .. "\0\xF7") - local s1, b = unpack("zB", s .. "\0\xF9") - assert(b == 249 and s1 == s) - s1 = pack("s", s) - assert(unpack("s", s1) == s) - - checkerror("does not fit", pack, "s1", s) - - checkerror("contains zeros", pack, "z", "alo\0"); - - for i = 2, NB do - local s1 = pack("s" .. i, s) - assert(unpack("s" .. i, s1) == s and #s1 == #s + i) - end -end - -do - local x = pack("s", "alo") - checkerror("too short", unpack, "s", x:sub(1, -2)) - checkerror("too short", unpack, "c5", "abcd") - checkerror("out of limits", pack, "s100", "alo") -end - -do - assert(pack("c0", "") == "") - assert(packsize("c0") == 0) - assert(unpack("c0", "") == "") - assert(pack("!4 c6", "abcdef") == "abcdef") - assert(pack("c3", "123") == "123") - assert(pack("c0", "") == "") - assert(pack("c8", "123456") == "123456\0\0") - assert(pack("c88", "") == string.rep("\0", 88)) - assert(pack("c188", "ab") == "ab" .. string.rep("\0", 188 - 2)) - local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz") - assert(a == "abcdefghi" and b == "xyz" and c == 14) - checkerror("longer than", pack, "c3", "1234") -end - - --- testing multiple types and sequence -do - local x = pack("!8 b Xh i4 i8 c1 Xi8", -12, 100, 200, "\xEC") - assert(#x == packsize(">!8 b Xh i4 i8 c1 Xi8")) - assert(x == "\xf4" .. "\0\0\0" .. - "\0\0\0\100" .. - "\0\0\0\0\0\0\0\xC8" .. - "\xEC" .. "\0\0\0\0\0\0\0") - local a, b, c, d, pos = unpack(">!8 c1 Xh i4 i8 b Xi8 XI XH", x) - assert(a == "\xF4" and b == 100 and c == 200 and d == -20 and (pos - 1) == #x) - - x = pack(">!4 c3 c4 c2 z i4 c5 c2 Xi4", - "abc", "abcd", "xz", "hello", 5, "world", "xy") - assert(x == "abcabcdxzhello\0\0\0\0\0\5worldxy\0") - local a, b, c, d, e, f, g, pos = unpack(">!4 c3 c4 c2 z i4 c5 c2 Xh Xi4", x) - assert(a == "abc" and b == "abcd" and c == "xz" and d == "hello" and - e == 5 and f == "world" and g == "xy" and (pos - 1) % 4 == 0) - - x = pack(" b b Xd b Xb x", 1, 2, 3) - assert(packsize(" b b Xd b Xb x") == 4) - assert(x == "\1\2\3\0") - a, b, c, pos = unpack("bbXdb", x) - assert(a == 1 and b == 2 and c == 3 and pos == #x) - - -- only alignment - assert(packsize("!8 xXi8") == 8) - local pos = unpack("!8 xXi8", "0123456701234567"); assert(pos == 9) - assert(packsize("!8 xXi2") == 2) - local pos = unpack("!8 xXi2", "0123456701234567"); assert(pos == 3) - assert(packsize("!2 xXi2") == 2) - local pos = unpack("!2 xXi2", "0123456701234567"); assert(pos == 3) - assert(packsize("!2 xXi8") == 2) - local pos = unpack("!2 xXi8", "0123456701234567"); assert(pos == 3) - assert(packsize("!16 xXi16") == 16) - local pos = unpack("!16 xXi16", "0123456701234567"); assert(pos == 17) - - checkerror("invalid next option", pack, "X") - checkerror("invalid next option", unpack, "XXi", "") - checkerror("invalid next option", unpack, "X i", "") - checkerror("invalid next option", pack, "Xc1") -end - -do -- testing initial position - local x = pack("i4i4i4i4", 1, 2, 3, 4) - for pos = 1, 16, 4 do - local i, p = unpack("i4", x, pos) - assert(i == pos//4 + 1 and p == pos + 4) - end - - -- with alignment - for pos = 0, 12 do -- will always round position to power of 2 - local i, p = unpack("!4 i4", x, pos + 1) - assert(i == (pos + 3)//4 + 1 and p == i*4 + 1) - end - - -- negative indices - local i, p = unpack("!4 i4", x, -4) - assert(i == 4 and p == 17) - local i, p = unpack("!4 i4", x, -7) - assert(i == 4 and p == 17) - local i, p = unpack("!4 i4", x, -#x) - assert(i == 1 and p == 5) - - -- limits - for i = 1, #x + 1 do - assert(unpack("c0", x, i) == "") - end - checkerror("out of string", unpack, "c0", x, 0) - checkerror("out of string", unpack, "c0", x, #x + 2) - checkerror("out of string", unpack, "c0", x, -(#x + 1)) - -end - -print "OK" - diff --git a/tofix/lua-5.3.4-tests/utf8.lua b/tofix/lua-5.3.4-tests/utf8.lua deleted file mode 100644 index ebc190b..0000000 --- a/tofix/lua-5.3.4-tests/utf8.lua +++ /dev/null @@ -1,210 +0,0 @@ --- $Id: utf8.lua,v 1.12 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print "testing UTF-8 library" - -local utf8 = require'utf8' - - -local function checkerror (msg, f, ...) - local s, err = pcall(f, ...) - assert(not s and string.find(err, msg)) -end - - -local function len (s) - return #string.gsub(s, "[\x80-\xBF]", "") -end - - -local justone = "^" .. utf8.charpattern .. "$" - --- 't' is the list of codepoints of 's' -local function checksyntax (s, t) - local ts = {"return '"} - for i = 1, #t do ts[i + 1] = string.format("\\u{%x}", t[i]) end - ts[#t + 2] = "'" - ts = table.concat(ts) - assert(assert(load(ts))() == s) -end - -assert(utf8.offset("alo", 5) == nil) -assert(utf8.offset("alo", -4) == nil) - --- 't' is the list of codepoints of 's' -local function check (s, t) - local l = utf8.len(s) - assert(#t == l and len(s) == l) - assert(utf8.char(table.unpack(t)) == s) - - assert(utf8.offset(s, 0) == 1) - - checksyntax(s, t) - - local t1 = {utf8.codepoint(s, 1, -1)} - assert(#t == #t1) - for i = 1, #t do assert(t[i] == t1[i]) end - - for i = 1, l do - local pi = utf8.offset(s, i) -- position of i-th char - local pi1 = utf8.offset(s, 2, pi) -- position of next char - assert(string.find(string.sub(s, pi, pi1 - 1), justone)) - assert(utf8.offset(s, -1, pi1) == pi) - assert(utf8.offset(s, i - l - 1) == pi) - assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi))) - for j = pi, pi1 - 1 do - assert(utf8.offset(s, 0, j) == pi) - end - for j = pi + 1, pi1 - 1 do - assert(not utf8.len(s, j)) - end - assert(utf8.len(s, pi, pi) == 1) - assert(utf8.len(s, pi, pi1 - 1) == 1) - assert(utf8.len(s, pi) == l - i + 1) - assert(utf8.len(s, pi1) == l - i) - assert(utf8.len(s, 1, pi) == i) - end - - local i = 0 - for p, c in utf8.codes(s) do - i = i + 1 - assert(c == t[i] and p == utf8.offset(s, i)) - assert(utf8.codepoint(s, p) == c) - end - assert(i == #t) - - i = 0 - for p, c in utf8.codes(s) do - i = i + 1 - assert(c == t[i] and p == utf8.offset(s, i)) - end - assert(i == #t) - - i = 0 - for c in string.gmatch(s, utf8.charpattern) do - i = i + 1 - assert(c == utf8.char(t[i])) - end - assert(i == #t) - - for i = 1, l do - assert(utf8.offset(s, i) == utf8.offset(s, i - l - 1, #s + 1)) - end - -end - - -do -- error indication in utf8.len - local function check (s, p) - local a, b = utf8.len(s) - assert(not a and b == p) - end - check("abc\xE3def", 4) - check("汉字\x80", #("汉字") + 1) - check("\xF4\x9F\xBF", 1) - check("\xF4\x9F\xBF\xBF", 1) -end - --- error in utf8.codes -checkerror("invalid UTF%-8 code", - function () - local s = "ab\xff" - for c in utf8.codes(s) do assert(c) end - end) - - --- error in initial position for offset -checkerror("position out of range", utf8.offset, "abc", 1, 5) -checkerror("position out of range", utf8.offset, "abc", 1, -4) -checkerror("position out of range", utf8.offset, "", 1, 2) -checkerror("position out of range", utf8.offset, "", 1, -1) -checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) -checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) -checkerror("continuation byte", utf8.offset, "\x80", 1) - - - -local s = "hello World" -local t = {string.byte(s, 1, -1)} -for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end -check(s, t) - -check("汉字/漢字", {27721, 23383, 47, 28450, 23383,}) - -do - local s = "áéí\128" - local t = {utf8.codepoint(s,1,#s - 1)} - assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237) - checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s) - checkerror("out of range", utf8.codepoint, s, #s + 1) - t = {utf8.codepoint(s, 4, 3)} - assert(#t == 0) - checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1) - checkerror("out of range", utf8.codepoint, s, 1, #s + 1) -end - -assert(utf8.char() == "") -assert(utf8.char(97, 98, 99) == "abc") - -assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF) - -checkerror("value out of range", utf8.char, 0x10FFFF + 1) - -local function invalid (s) - checkerror("invalid UTF%-8 code", utf8.codepoint, s) - assert(not utf8.len(s)) -end - --- UTF-8 representation for 0x11ffff (value out of valid range) -invalid("\xF4\x9F\xBF\xBF") - --- overlong sequences -invalid("\xC0\x80") -- zero -invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte) -invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes) -invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes) - - --- invalid bytes -invalid("\x80") -- continuation byte -invalid("\xBF") -- continuation byte -invalid("\xFE") -- invalid byte -invalid("\xFF") -- invalid byte - - --- empty string -check("", {}) - --- minimum and maximum values for each sequence size -s = "\0 \x7F\z - \xC2\x80 \xDF\xBF\z - \xE0\xA0\x80 \xEF\xBF\xBF\z - \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF" -s = string.gsub(s, " ", "") -check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF}) - -x = "日本語a-4\0éó" -check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) - - --- Supplementary Characters -check("𣲷𠜎𠱓𡁻𠵼ab𠺢", - {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,}) - -check("𨳊𩶘𦧺𨳒𥄫𤓓\xF4\x8F\xBF\xBF", - {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff}) - - -local i = 0 -for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do - i = i + 1 - assert(utf8.offset(x, i) == p) - assert(utf8.len(x, p) == utf8.len(x) - i + 1) - assert(utf8.len(c) == 1) - for j = 1, #c - 1 do - assert(utf8.offset(x, 0, p + j - 1) == p) - end -end - -print'ok' - diff --git a/tofix/lua-5.3.4-tests/vararg.lua b/tofix/lua-5.3.4-tests/vararg.lua deleted file mode 100644 index d617a98..0000000 --- a/tofix/lua-5.3.4-tests/vararg.lua +++ /dev/null @@ -1,142 +0,0 @@ --- $Id: vararg.lua,v 1.25 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print('testing vararg') - -function f(a, ...) - local arg = {n = select('#', ...), ...} - for i=1,arg.n do assert(a[i]==arg[i]) end - return arg.n -end - -function c12 (...) - assert(arg == _G.arg) -- no local 'arg' - local x = {...}; x.n = #x - local res = (x.n==2 and x[1] == 1 and x[2] == 2) - if res then res = 55 end - return res, 2 -end - -function vararg (...) return {n = select('#', ...), ...} end - -local call = function (f, args) return f(table.unpack(args, 1, args.n)) end - -assert(f() == 0) -assert(f({1,2,3}, 1, 2, 3) == 3) -assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5) - -assert(c12(1,2)==55) -a,b = assert(call(c12, {1,2})) -assert(a == 55 and b == 2) -a = call(c12, {1,2;n=2}) -assert(a == 55 and b == 2) -a = call(c12, {1,2;n=1}) -assert(not a) -assert(c12(1,2,3) == false) -local a = vararg(call(next, {_G,nil;n=2})) -local b,c = next(_G) -assert(a[1] == b and a[2] == c and a.n == 2) -a = vararg(call(call, {c12, {1,2}})) -assert(a.n == 2 and a[1] == 55 and a[2] == 2) -a = call(print, {'+'}) -assert(a == nil) - -local t = {1, 10} -function t:f (...) local arg = {...}; return self[...]+#arg end -assert(t:f(1,4) == 3 and t:f(2) == 11) -print('+') - -lim = 20 -local i, a = 1, {} -while i <= lim do a[i] = i+0.3; i=i+1 end - -function f(a, b, c, d, ...) - local more = {...} - assert(a == 1.3 and more[1] == 5.3 and - more[lim-4] == lim+0.3 and not more[lim-3]) -end - -function g(a,b,c) - assert(a == 1.3 and b == 2.3 and c == 3.3) -end - -call(f, a) -call(g, a) - -a = {} -i = 1 -while i <= lim do a[i] = i; i=i+1 end -assert(call(math.max, a) == lim) - -print("+") - - --- new-style varargs - -function oneless (a, ...) return ... end - -function f (n, a, ...) - local b - assert(arg == _G.arg) -- no local 'arg' - if n == 0 then - local b, c, d = ... - return a, b, c, d, oneless(oneless(oneless(...))) - else - n, b, a = n-1, ..., a - assert(b == ...) - return f(n, a, ...) - end -end - -a,b,c,d,e = assert(f(10,5,4,3,2,1)) -assert(a==5 and b==4 and c==3 and d==2 and e==1) - -a,b,c,d,e = f(4) -assert(a==nil and b==nil and c==nil and d==nil and e==nil) - - --- varargs for main chunks -f = load[[ return {...} ]] -x = f(2,3) -assert(x[1] == 2 and x[2] == 3 and x[3] == nil) - - -f = load[[ - local x = {...} - for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end - assert(x[select('#', ...)+1] == nil) - return true -]] - -assert(f("a", "b", nil, {}, assert)) -assert(f()) - -a = {select(3, table.unpack{10,20,30,40})} -assert(#a == 2 and a[1] == 30 and a[2] == 40) -a = {select(1)} -assert(next(a) == nil) -a = {select(-1, 3, 5, 7)} -assert(a[1] == 7 and a[2] == nil) -a = {select(-2, 3, 5, 7)} -assert(a[1] == 5 and a[2] == 7 and a[3] == nil) -pcall(select, 10000) -pcall(select, -10000) - - --- bug in 5.2.2 - -function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, -p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, -p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, -p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, -p41, p42, p43, p44, p45, p46, p48, p49, p50, ...) - local a1,a2,a3,a4,a5,a6,a7 - local a8,a9,a10,a11,a12,a13,a14 -end - --- assertion fail here -f() - - -print('OK') - diff --git a/tofix/lua-5.3.4-tests/verybig.lua b/tofix/lua-5.3.4-tests/verybig.lua deleted file mode 100644 index 1cfd10b..0000000 --- a/tofix/lua-5.3.4-tests/verybig.lua +++ /dev/null @@ -1,152 +0,0 @@ --- $Id: verybig.lua,v 1.25 2016/11/07 13:11:28 roberto Exp $ --- See Copyright Notice in file all.lua - -print "testing RK" - --- testing opcodes with RK arguments larger than K limit -local function foo () - local dummy = { - -- fill first 256 entries in table of constants - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, - 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, - 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, - 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, - 97, 98, 99, 100, 101, 102, 103, 104, - 105, 106, 107, 108, 109, 110, 111, 112, - 113, 114, 115, 116, 117, 118, 119, 120, - 121, 122, 123, 124, 125, 126, 127, 128, - 129, 130, 131, 132, 133, 134, 135, 136, - 137, 138, 139, 140, 141, 142, 143, 144, - 145, 146, 147, 148, 149, 150, 151, 152, - 153, 154, 155, 156, 157, 158, 159, 160, - 161, 162, 163, 164, 165, 166, 167, 168, - 169, 170, 171, 172, 173, 174, 175, 176, - 177, 178, 179, 180, 181, 182, 183, 184, - 185, 186, 187, 188, 189, 190, 191, 192, - 193, 194, 195, 196, 197, 198, 199, 200, - 201, 202, 203, 204, 205, 206, 207, 208, - 209, 210, 211, 212, 213, 214, 215, 216, - 217, 218, 219, 220, 221, 222, 223, 224, - 225, 226, 227, 228, 229, 230, 231, 232, - 233, 234, 235, 236, 237, 238, 239, 240, - 241, 242, 243, 244, 245, 246, 247, 248, - 249, 250, 251, 252, 253, 254, 255, 256, - } - assert(24.5 + 0.6 == 25.1) - local t = {foo = function (self, x) return x + self.x end, x = 10} - t.t = t - assert(t:foo(1.5) == 11.5) - assert(t.t:foo(0.5) == 10.5) -- bug in 5.2 alpha - assert(24.3 == 24.3) - assert((function () return t.x end)() == 10) -end - - -foo() -foo = nil - -if _soft then return 10 end - -print "testing large programs (>64k)" - --- template to create a very big test file -prog = [[$ - -local a,b - -b = {$1$ - b30009 = 65534, - b30010 = 65535, - b30011 = 65536, - b30012 = 65537, - b30013 = 16777214, - b30014 = 16777215, - b30015 = 16777216, - b30016 = 16777217, - b30017 = 0x7fffff, - b30018 = -0x7fffff, - b30019 = 0x1ffffff, - b30020 = -0x1ffffd, - b30021 = -65534, - b30022 = -65535, - b30023 = -65536, - b30024 = -0xffffff, - b30025 = 15012.5, - $2$ -}; - -assert(b.a50008 == 25004 and b["a11"] == -5.5) -assert(b.a33007 == -16503.5 and b.a50009 == -25004.5) -assert(b["b"..30024] == -0xffffff) - -function b:xxx (a,b) return a+b end -assert(b:xxx(10, 12) == 22) -- pushself with non-constant index -b.xxx = nil - -s = 0; n=0 -for a,b in pairs(b) do s=s+b; n=n+1 end --- with 32-bit floats, exact value of 's' depends on summation order -assert(81800000.0 < s and s < 81860000 and n == 70001) - -a = nil; b = nil -print'+' - -function f(x) b=x end - -a = f{$3$} or 10 - -assert(a==10) -assert(b[1] == "a10" and b[2] == 5 and b[#b-1] == "a50009") - - -function xxxx (x) return b[x] end - -assert(xxxx(3) == "a11") - -a = nil; b=nil -xxxx = nil - -return 10 - -]] - --- functions to fill in the $n$ - -local function sig (x) - return (x % 2 == 0) and '' or '-' -end - -F = { -function () -- $1$ - for i=10,50009 do - io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') - end -end, - -function () -- $2$ - for i=30026,50009 do - io.write('b', i, ' = ', sig(i), 15013+((i-30026)/2), ',\n') - end -end, - -function () -- $3$ - for i=10,50009 do - io.write('"a', i, '", ', sig(i), 5+((i-10)/2), ',\n') - end -end, -} - -file = os.tmpname() -io.output(file) -for s in string.gmatch(prog, "$([^$]+)") do - local n = tonumber(s) - if not n then io.write(s) else F[n]() end -end -io.close() -result = dofile(file) -assert(os.remove(file)) -print'OK' -return result - From 9a4b261179f39a9c94d68f2aebfd97a4933ca01a Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Fri, 18 Apr 2025 12:38:54 +0100 Subject: [PATCH 08/10] add clang --- .helix/ignore | 9 +- 05/Makefile | 21 +- 05/tcc-final-old/.gitignore | 55 + 05/tcc-final-old/COPYING | 504 + 05/tcc-final-old/Changelog | 439 + 05/tcc-final-old/CodingStyle | 71 + 05/tcc-final-old/Makefile | 403 + 05/tcc-final-old/README | 95 + 05/tcc-final-old/RELICENSING | 60 + 05/tcc-final-old/TODO | 100 + 05/tcc-final-old/VERSION | 1 + 05/tcc-final-old/arm-asm.c | 94 + 05/tcc-final-old/arm-gen.c | 2151 +++++ 05/tcc-final-old/arm-link.c | 398 + 05/tcc-final-old/arm64-gen.c | 1837 ++++ 05/tcc-final-old/arm64-link.c | 256 + 05/tcc-final-old/c67-gen.c | 2540 +++++ 05/tcc-final-old/c67-link.c | 131 + 05/tcc-final-old/coff.h | 446 + 05/tcc-final-old/config.mak | 24 + 05/tcc-final-old/configure | 527 + 05/tcc-final-old/elf.h | 3237 +++++++ 05/tcc-final-old/examples/ex1.c | 8 + 05/tcc-final-old/examples/ex2.c | 98 + 05/tcc-final-old/examples/ex3.c | 23 + 05/tcc-final-old/examples/ex4.c | 26 + 05/tcc-final-old/examples/ex5.c | 8 + 05/tcc-final-old/i386-asm.c | 1723 ++++ 05/tcc-final-old/i386-asm.h | 480 + 05/tcc-final-old/i386-gen.c | 1164 +++ 05/tcc-final-old/i386-link.c | 247 + 05/tcc-final-old/i386-tok.h | 253 + 05/tcc-final-old/il-gen.c | 657 ++ 05/tcc-final-old/il-opcodes.h | 251 + 05/tcc-final-old/include/float.h | 57 + 05/tcc-final-old/include/stdarg.h | 79 + 05/tcc-final-old/include/stdbool.h | 11 + 05/tcc-final-old/include/stddef.h | 54 + 05/tcc-final-old/include/varargs.h | 12 + 05/tcc-final-old/lib/Makefile | 73 + 05/tcc-final-old/lib/alloca-arm.S | 17 + 05/tcc-final-old/lib/alloca86-bt.S | 47 + 05/tcc-final-old/lib/alloca86.S | 31 + 05/tcc-final-old/lib/alloca86_64-bt.S | 56 + 05/tcc-final-old/lib/alloca86_64.S | 34 + 05/tcc-final-old/lib/armeabi.c | 501 + 05/tcc-final-old/lib/armflush.c | 58 + 05/tcc-final-old/lib/bcheck.c | 979 ++ 05/tcc-final-old/lib/lib-arm64.c | 664 ++ 05/tcc-final-old/lib/libtcc1.c | 622 ++ 05/tcc-final-old/lib/va_list.c | 65 + 05/tcc-final-old/libtcc.c | 1981 ++++ 05/tcc-final-old/libtcc.h | 100 + 05/tcc-final-old/stab.def | 234 + 05/tcc-final-old/stab.h | 17 + 05/tcc-final-old/tcc-doc.texi | 1326 +++ 05/tcc-final-old/tcc.c | 371 + 05/tcc-final-old/tcc.h | 1660 ++++ 05/tcc-final-old/tccasm.c | 1277 +++ 05/tcc-final-old/tcccoff.c | 948 ++ 05/tcc-final-old/tccelf.c | 3058 ++++++ 05/tcc-final-old/tccgen.c | 7386 ++++++++++++++ 05/tcc-final-old/tcclib.h | 80 + 05/tcc-final-old/tccpe.c | 1996 ++++ 05/tcc-final-old/tccpp.c | 3903 ++++++++ 05/tcc-final-old/tccrun.c | 894 ++ 05/tcc-final-old/tcctok.h | 350 + 05/tcc-final-old/tcctools.c | 546 ++ 05/tcc-final-old/tests/42test.h | 13 + 05/tcc-final-old/tests/Makefile | 289 + 05/tcc-final-old/tests/abitest.c | 691 ++ 05/tcc-final-old/tests/asm-c-connect-1.c | 57 + 05/tcc-final-old/tests/asm-c-connect-2.c | 36 + 05/tcc-final-old/tests/asmtest.S | 978 ++ 05/tcc-final-old/tests/boundtest.c | 285 + 05/tcc-final-old/tests/gcctestsuite.sh | 33 + 05/tcc-final-old/tests/libtcc_test.c | 96 + 05/tcc-final-old/tests/pp/01.c | 6 + 05/tcc-final-old/tests/pp/01.expect | 1 + 05/tcc-final-old/tests/pp/02.c | 28 + 05/tcc-final-old/tests/pp/02.expect | 5 + 05/tcc-final-old/tests/pp/03.c | 15 + 05/tcc-final-old/tests/pp/03.expect | 5 + 05/tcc-final-old/tests/pp/04.c | 4 + 05/tcc-final-old/tests/pp/04.expect | 1 + 05/tcc-final-old/tests/pp/05.c | 7 + 05/tcc-final-old/tests/pp/05.expect | 3 + 05/tcc-final-old/tests/pp/06.c | 5 + 05/tcc-final-old/tests/pp/06.expect | 1 + 05/tcc-final-old/tests/pp/07.c | 4 + 05/tcc-final-old/tests/pp/07.expect | 2 + 05/tcc-final-old/tests/pp/08.c | 4 + 05/tcc-final-old/tests/pp/08.expect | 1 + 05/tcc-final-old/tests/pp/09.c | 4 + 05/tcc-final-old/tests/pp/09.expect | 1 + 05/tcc-final-old/tests/pp/10.c | 10 + 05/tcc-final-old/tests/pp/10.expect | 5 + 05/tcc-final-old/tests/pp/11.c | 31 + 05/tcc-final-old/tests/pp/11.expect | 15 + 05/tcc-final-old/tests/pp/12.S | 8 + 05/tcc-final-old/tests/pp/12.expect | 2 + 05/tcc-final-old/tests/pp/13.S | 6 + 05/tcc-final-old/tests/pp/13.expect | 2 + 05/tcc-final-old/tests/pp/14.c | 13 + 05/tcc-final-old/tests/pp/14.expect | 3 + 05/tcc-final-old/tests/pp/15.c | 18 + 05/tcc-final-old/tests/pp/15.expect | 5 + 05/tcc-final-old/tests/pp/16.c | 3 + 05/tcc-final-old/tests/pp/16.expect | 2 + 05/tcc-final-old/tests/pp/17.c | 14 + 05/tcc-final-old/tests/pp/17.expect | 6 + 05/tcc-final-old/tests/pp/18.c | 15 + 05/tcc-final-old/tests/pp/18.expect | 3 + 05/tcc-final-old/tests/pp/19.c | 101 + 05/tcc-final-old/tests/pp/19.expect | 14 + 05/tcc-final-old/tests/pp/20.c | 13 + 05/tcc-final-old/tests/pp/20.expect | 6 + 05/tcc-final-old/tests/pp/21.c | 36 + 05/tcc-final-old/tests/pp/21.expect | 8 + 05/tcc-final-old/tests/pp/Makefile | 49 + 05/tcc-final-old/tests/pp/pp-counter.c | 27 + 05/tcc-final-old/tests/pp/pp-counter.expect | 15 + 05/tcc-final-old/tests/tcctest.c | 3871 ++++++++ 05/tcc-final-old/tests/tcctest.h | 9 + 05/tcc-final-old/tests/testfp.c | 510 + 05/tcc-final-old/tests/tests2/00_assignment.c | 18 + .../tests/tests2/00_assignment.expect | 3 + 05/tcc-final-old/tests/tests2/01_comment.c | 14 + .../tests/tests2/01_comment.expect | 5 + 05/tcc-final-old/tests/tests2/02_printf.c | 18 + .../tests/tests2/02_printf.expect | 15 + 05/tcc-final-old/tests/tests2/03_struct.c | 31 + .../tests/tests2/03_struct.expect | 6 + 05/tcc-final-old/tests/tests2/04_for.c | 15 + 05/tcc-final-old/tests/tests2/04_for.expect | 10 + 05/tcc-final-old/tests/tests2/05_array.c | 21 + 05/tcc-final-old/tests/tests2/05_array.expect | 10 + 05/tcc-final-old/tests/tests2/06_case.c | 29 + 05/tcc-final-old/tests/tests2/06_case.expect | 8 + 05/tcc-final-old/tests/tests2/07_function.c | 30 + .../tests/tests2/07_function.expect | 4 + 05/tcc-final-old/tests/tests2/08_while.c | 24 + 05/tcc-final-old/tests/tests2/08_while.expect | 11 + 05/tcc-final-old/tests/tests2/09_do_while.c | 24 + .../tests/tests2/09_do_while.expect | 11 + 05/tcc-final-old/tests/tests2/10_pointer.c | 40 + .../tests/tests2/10_pointer.expect | 8 + 05/tcc-final-old/tests/tests2/11_precedence.c | 40 + .../tests/tests2/11_precedence.expect | 15 + 05/tcc-final-old/tests/tests2/12_hashdefine.c | 14 + .../tests/tests2/12_hashdefine.expect | 2 + .../tests/tests2/13_integer_literals.c | 20 + .../tests/tests2/13_integer_literals.expect | 5 + 05/tcc-final-old/tests/tests2/14_if.c | 21 + 05/tcc-final-old/tests/tests2/14_if.expect | 2 + 05/tcc-final-old/tests/tests2/15_recursion.c | 21 + .../tests/tests2/15_recursion.expect | 10 + 05/tcc-final-old/tests/tests2/16_nesting.c | 21 + .../tests/tests2/16_nesting.expect | 18 + 05/tcc-final-old/tests/tests2/17_enum.c | 72 + 05/tcc-final-old/tests/tests2/17_enum.expect | 4 + 05/tcc-final-old/tests/tests2/18_include.c | 12 + .../tests/tests2/18_include.expect | 3 + 05/tcc-final-old/tests/tests2/18_include.h | 1 + .../tests/tests2/19_pointer_arithmetic.c | 28 + .../tests/tests2/19_pointer_arithmetic.expect | 3 + .../tests/tests2/20_pointer_comparison.c | 24 + .../tests/tests2/20_pointer_comparison.expect | 6 + 05/tcc-final-old/tests/tests2/21_char_array.c | 33 + .../tests/tests2/21_char_array.expect | 7 + .../tests/tests2/22_floating_point.c | 50 + .../tests/tests2/22_floating_point.expect | 16 + .../tests/tests2/23_type_coercion.c | 54 + .../tests/tests2/23_type_coercion.expect | 12 + .../tests/tests2/24_math_library.c | 30 + .../tests/tests2/24_math_library.expect | 18 + 05/tcc-final-old/tests/tests2/25_quicksort.c | 83 + .../tests/tests2/25_quicksort.expect | 2 + .../tests/tests2/26_character_constants.c | 17 + .../tests2/26_character_constants.expect | 8 + 05/tcc-final-old/tests/tests2/27_sizeof.c | 18 + .../tests/tests2/27_sizeof.expect | 4 + 05/tcc-final-old/tests/tests2/28_strings.c | 45 + .../tests/tests2/28_strings.expect | 19 + .../tests/tests2/29_array_address.c | 13 + .../tests/tests2/29_array_address.expect | 1 + 05/tcc-final-old/tests/tests2/30_hanoi.c | 122 + 05/tcc-final-old/tests/tests2/30_hanoi.expect | 71 + 05/tcc-final-old/tests/tests2/31_args.c | 14 + 05/tcc-final-old/tests/tests2/31_args.expect | 6 + 05/tcc-final-old/tests/tests2/32_led.c | 266 + 05/tcc-final-old/tests/tests2/32_led.expect | 4 + 05/tcc-final-old/tests/tests2/33_ternary_op.c | 15 + .../tests/tests2/33_ternary_op.expect | 10 + .../tests/tests2/34_array_assignment.c | 23 + .../tests/tests2/34_array_assignment.expect | 2 + 05/tcc-final-old/tests/tests2/35_sizeof.c | 14 + .../tests/tests2/35_sizeof.expect | 2 + .../tests/tests2/36_array_initialisers.c | 21 + .../tests/tests2/36_array_initialisers.expect | 20 + 05/tcc-final-old/tests/tests2/37_sprintf.c | 17 + .../tests/tests2/37_sprintf.expect | 20 + .../tests/tests2/38_multiple_array_index.c | 32 + .../tests2/38_multiple_array_index.expect | 4 + 05/tcc-final-old/tests/tests2/39_typedef.c | 65 + .../tests/tests2/39_typedef.expect | 3 + 05/tcc-final-old/tests/tests2/40_stdio.c | 52 + 05/tcc-final-old/tests/tests2/40_stdio.expect | 27 + 05/tcc-final-old/tests/tests2/41_hashif.c | 85 + .../tests/tests2/41_hashif.expect | 6 + .../tests/tests2/42_function_pointer.c | 22 + .../tests/tests2/42_function_pointer.expect | 2 + 05/tcc-final-old/tests/tests2/43_void_param.c | 15 + .../tests/tests2/43_void_param.expect | 1 + .../tests/tests2/44_scoped_declarations.c | 17 + .../tests2/44_scoped_declarations.expect | 1 + 05/tcc-final-old/tests/tests2/45_empty_for.c | 18 + .../tests/tests2/45_empty_for.expect | 10 + 05/tcc-final-old/tests/tests2/46_grep.c | 568 ++ 05/tcc-final-old/tests/tests2/46_grep.expect | 3 + .../tests/tests2/47_switch_return.c | 24 + .../tests/tests2/47_switch_return.expect | 4 + .../tests/tests2/48_nested_break.c | 26 + .../tests/tests2/48_nested_break.expect | 1 + .../tests/tests2/49_bracket_evaluation.c | 23 + .../tests/tests2/49_bracket_evaluation.expect | 1 + .../tests/tests2/50_logical_second_arg.c | 29 + .../tests/tests2/50_logical_second_arg.expect | 20 + 05/tcc-final-old/tests/tests2/51_static.c | 30 + .../tests/tests2/51_static.expect | 8 + .../tests/tests2/52_unnamed_enum.c | 27 + .../tests/tests2/52_unnamed_enum.expect | 9 + 05/tcc-final-old/tests/tests2/54_goto.c | 56 + 05/tcc-final-old/tests/tests2/54_goto.expect | 8 + .../tests/tests2/55_lshift_type.c | 52 + .../tests/tests2/55_lshift_type.expect | 1 + .../tests/tests2/60_errors_and_warnings.c | 51 + .../tests2/60_errors_and_warnings.expect | 28 + .../tests/tests2/64_macro_nesting.c | 12 + .../tests/tests2/64_macro_nesting.expect | 1 + .../tests/tests2/67_macro_concat.c | 14 + .../tests/tests2/67_macro_concat.expect | 2 + .../tests/tests2/70_floating_point_literals.c | 77 + .../tests2/70_floating_point_literals.expect | 53 + .../tests/tests2/71_macro_empty_arg.c | 9 + .../tests/tests2/71_macro_empty_arg.expect | 1 + .../tests/tests2/72_long_long_constant.c | 19 + .../tests/tests2/72_long_long_constant.expect | 1 + 05/tcc-final-old/tests/tests2/73_arm64.c | 527 + 05/tcc-final-old/tests/tests2/73_arm64.expect | 174 + .../tests/tests2/75_array_in_struct_init.c | 33 + .../tests2/75_array_in_struct_init.expect | 72 + .../tests/tests2/76_dollars_in_identifiers.c | 41 + .../tests2/76_dollars_in_identifiers.expect | 14 + .../tests/tests2/77_push_pop_macro.c | 30 + .../tests/tests2/77_push_pop_macro.expect | 5 + 05/tcc-final-old/tests/tests2/78_vla_label.c | 45 + .../tests/tests2/78_vla_label.expect | 6 + .../tests/tests2/79_vla_continue.c | 116 + .../tests/tests2/79_vla_continue.expect | 5 + 05/tcc-final-old/tests/tests2/80_flexarray.c | 25 + .../tests/tests2/80_flexarray.expect | 0 05/tcc-final-old/tests/tests2/81_types.c | 43 + 05/tcc-final-old/tests/tests2/81_types.expect | 0 .../tests/tests2/82_attribs_position.c | 19 + .../tests/tests2/82_attribs_position.expect | 0 .../tests/tests2/83_utf8_in_identifiers.c | 9 + .../tests2/83_utf8_in_identifiers.expect | 2 + 05/tcc-final-old/tests/tests2/84_hex-float.c | 12 + .../tests/tests2/84_hex-float.expect | 1 + .../tests/tests2/85_asm-outside-function.c | 9 + .../tests2/85_asm-outside-function.expect | 1 + .../tests/tests2/86_memory-model.c | 38 + .../tests/tests2/86_memory-model.expect | 1 + 05/tcc-final-old/tests/tests2/87_dead_code.c | 122 + .../tests/tests2/87_dead_code.expect | 18 + 05/tcc-final-old/tests/tests2/88_codeopt.c | 68 + .../tests/tests2/88_codeopt.expect | 2 + .../tests/tests2/89_nocode_wanted.c | 112 + .../tests/tests2/89_nocode_wanted.expect | 14 + .../tests/tests2/90_struct-init.c | 282 + .../tests/tests2/90_struct-init.expect | 43 + .../tests/tests2/91_ptr_longlong_arith32.c | 15 + .../tests2/91_ptr_longlong_arith32.expect | 1 + .../tests/tests2/92_enum_bitfield.c | 57 + .../tests/tests2/92_enum_bitfield.expect | 0 .../tests/tests2/93_integer_promotion.c | 71 + .../tests/tests2/93_integer_promotion.expect | 46 + 05/tcc-final-old/tests/tests2/94_generic.c | 64 + .../tests/tests2/94_generic.expect | 13 + 05/tcc-final-old/tests/tests2/95_bitfields.c | 218 + .../tests/tests2/95_bitfields.expect | 149 + .../tests/tests2/95_bitfields_ms.c | 2 + .../tests/tests2/95_bitfields_ms.expect | 149 + .../tests/tests2/96_nodata_wanted.c | 84 + .../tests/tests2/96_nodata_wanted.expect | 23 + .../tests/tests2/97_utf8_string_literal.c | 12 + .../tests2/97_utf8_string_literal.expect | 1 + .../tests/tests2/98_al_ax_extend.c | 41 + .../tests/tests2/98_al_ax_extend.expect | 9 + 05/tcc-final-old/tests/tests2/99_fastcall.c | 276 + .../tests/tests2/99_fastcall.expect | 1 + 05/tcc-final-old/tests/tests2/LICENSE | 37 + 05/tcc-final-old/tests/tests2/Makefile | 112 + 05/tcc-final-old/tests/vla_test.c | 84 + 05/tcc-final-old/texi2pod.pl | 427 + 05/tcc-final-old/win32/build-tcc.bat | 189 + 05/tcc-final-old/win32/examples/dll.c | 13 + 05/tcc-final-old/win32/examples/fib.c | 24 + 05/tcc-final-old/win32/examples/hello_dll.c | 20 + 05/tcc-final-old/win32/examples/hello_win.c | 163 + 05/tcc-final-old/win32/include/_mingw.h | 170 + 05/tcc-final-old/win32/include/assert.h | 57 + 05/tcc-final-old/win32/include/conio.h | 409 + 05/tcc-final-old/win32/include/ctype.h | 281 + 05/tcc-final-old/win32/include/dir.h | 31 + 05/tcc-final-old/win32/include/direct.h | 68 + 05/tcc-final-old/win32/include/dirent.h | 135 + 05/tcc-final-old/win32/include/dos.h | 55 + 05/tcc-final-old/win32/include/errno.h | 75 + 05/tcc-final-old/win32/include/excpt.h | 123 + 05/tcc-final-old/win32/include/fcntl.h | 52 + 05/tcc-final-old/win32/include/fenv.h | 108 + 05/tcc-final-old/win32/include/inttypes.h | 297 + 05/tcc-final-old/win32/include/io.h | 418 + 05/tcc-final-old/win32/include/limits.h | 111 + 05/tcc-final-old/win32/include/locale.h | 91 + 05/tcc-final-old/win32/include/malloc.h | 181 + 05/tcc-final-old/win32/include/math.h | 737 ++ 05/tcc-final-old/win32/include/mem.h | 13 + 05/tcc-final-old/win32/include/memory.h | 40 + 05/tcc-final-old/win32/include/process.h | 176 + .../win32/include/sec_api/conio_s.h | 42 + .../win32/include/sec_api/crtdbg_s.h | 19 + 05/tcc-final-old/win32/include/sec_api/io_s.h | 33 + .../win32/include/sec_api/mbstring_s.h | 52 + .../win32/include/sec_api/search_s.h | 25 + .../win32/include/sec_api/stdio_s.h | 145 + .../win32/include/sec_api/stdlib_s.h | 67 + .../win32/include/sec_api/stralign_s.h | 30 + .../win32/include/sec_api/string_s.h | 41 + .../win32/include/sec_api/sys/timeb_s.h | 34 + .../win32/include/sec_api/tchar_s.h | 266 + .../win32/include/sec_api/time_s.h | 61 + .../win32/include/sec_api/wchar_s.h | 128 + 05/tcc-final-old/win32/include/setjmp.h | 160 + 05/tcc-final-old/win32/include/share.h | 28 + 05/tcc-final-old/win32/include/signal.h | 63 + 05/tcc-final-old/win32/include/stdint.h | 212 + 05/tcc-final-old/win32/include/stdio.h | 429 + 05/tcc-final-old/win32/include/stdlib.h | 580 ++ 05/tcc-final-old/win32/include/string.h | 164 + 05/tcc-final-old/win32/include/sys/fcntl.h | 13 + 05/tcc-final-old/win32/include/sys/file.h | 14 + 05/tcc-final-old/win32/include/sys/locking.h | 30 + 05/tcc-final-old/win32/include/sys/stat.h | 290 + 05/tcc-final-old/win32/include/sys/time.h | 69 + 05/tcc-final-old/win32/include/sys/timeb.h | 133 + 05/tcc-final-old/win32/include/sys/types.h | 118 + 05/tcc-final-old/win32/include/sys/unistd.h | 14 + 05/tcc-final-old/win32/include/sys/utime.h | 146 + 05/tcc-final-old/win32/include/tchar.h | 1102 +++ 05/tcc-final-old/win32/include/time.h | 287 + 05/tcc-final-old/win32/include/vadefs.h | 11 + 05/tcc-final-old/win32/include/values.h | 4 + 05/tcc-final-old/win32/include/wchar.h | 873 ++ 05/tcc-final-old/win32/include/wctype.h | 172 + .../win32/include/winapi/basetsd.h | 149 + .../win32/include/winapi/basetyps.h | 85 + .../win32/include/winapi/guiddef.h | 156 + .../win32/include/winapi/poppack.h | 8 + .../win32/include/winapi/pshpack1.h | 8 + .../win32/include/winapi/pshpack2.h | 8 + .../win32/include/winapi/pshpack4.h | 8 + .../win32/include/winapi/pshpack8.h | 8 + .../win32/include/winapi/winbase.h | 2951 ++++++ .../win32/include/winapi/wincon.h | 301 + .../win32/include/winapi/windef.h | 293 + .../win32/include/winapi/windows.h | 127 + .../win32/include/winapi/winerror.h | 3166 ++++++ .../win32/include/winapi/wingdi.h | 4080 ++++++++ 05/tcc-final-old/win32/include/winapi/winnt.h | 5835 ++++++++++++ .../win32/include/winapi/winreg.h | 272 + .../win32/include/winapi/winuser.h | 5651 +++++++++++ .../win32/include/winapi/winver.h | 160 + 05/tcc-final-old/win32/lib/chkstk.S | 191 + 05/tcc-final-old/win32/lib/crt1.c | 79 + 05/tcc-final-old/win32/lib/crt1w.c | 3 + 05/tcc-final-old/win32/lib/dllcrt1.c | 13 + 05/tcc-final-old/win32/lib/dllmain.c | 9 + 05/tcc-final-old/win32/lib/gdi32.def | 337 + 05/tcc-final-old/win32/lib/kernel32.def | 770 ++ 05/tcc-final-old/win32/lib/msvcrt.def | 1399 +++ 05/tcc-final-old/win32/lib/user32.def | 658 ++ 05/tcc-final-old/win32/lib/wincrt1.c | 75 + 05/tcc-final-old/win32/lib/wincrt1w.c | 3 + 05/tcc-final-old/win32/tcc-win32.txt | 168 + 05/tcc-final-old/x86_64-asm.h | 525 + 05/tcc-final-old/x86_64-gen.c | 2258 +++++ 05/tcc-final-old/x86_64-link.c | 298 + 05/tcc-final/.gitignore | 64 + 05/tcc-final/COPYING | 504 + 05/tcc-final/Changelog | 439 + 05/tcc-final/CodingStyle | 71 + 05/tcc-final/Makefile | 489 + 05/tcc-final/README | 96 + 05/tcc-final/RELICENSING | 63 + 05/tcc-final/TODO | 100 + 05/tcc-final/USES | 20 + 05/tcc-final/VERSION | 1 + 05/tcc-final/arm-asm.c | 3235 +++++++ 05/tcc-final/arm-gen.c | 2391 +++++ 05/tcc-final/arm-link.c | 444 + 05/tcc-final/arm-tok.h | 383 + 05/tcc-final/arm64-asm.c | 94 + 05/tcc-final/arm64-gen.c | 2141 +++++ 05/tcc-final/arm64-link.c | 322 + 05/tcc-final/c67-gen.c | 2543 +++++ 05/tcc-final/c67-link.c | 125 + 05/tcc-final/coff.h | 446 + 05/tcc-final/configure | 574 ++ 05/tcc-final/dwarf.h | 1046 ++ 05/tcc-final/elf.h | 3319 +++++++ 05/tcc-final/examples/ex1.c | 8 + 05/tcc-final/examples/ex2.c | 98 + 05/tcc-final/examples/ex3.c | 23 + 05/tcc-final/examples/ex4.c | 26 + 05/tcc-final/examples/ex5.c | 8 + 05/tcc-final/i386-asm.c | 1727 ++++ 05/tcc-final/i386-asm.h | 482 + 05/tcc-final/i386-gen.c | 1140 +++ 05/tcc-final/i386-link.c | 325 + 05/tcc-final/i386-tok.h | 329 + 05/tcc-final/il-gen.c | 657 ++ 05/tcc-final/il-opcodes.h | 251 + 05/tcc-final/include/float.h | 75 + 05/tcc-final/include/stdalign.h | 16 + 05/tcc-final/include/stdarg.h | 14 + 05/tcc-final/include/stdatomic.h | 137 + 05/tcc-final/include/stdbool.h | 11 + 05/tcc-final/include/stddef.h | 41 + 05/tcc-final/include/stdnoreturn.h | 7 + 05/tcc-final/include/tccdefs.h | 296 + 05/tcc-final/include/tgmath.h | 89 + 05/tcc-final/include/varargs.h | 12 + 05/tcc-final/lib/Makefile | 94 + 05/tcc-final/lib/alloca-bt.S | 96 + 05/tcc-final/lib/alloca.S | 85 + 05/tcc-final/lib/armeabi.c | 537 ++ 05/tcc-final/lib/armflush.c | 51 + 05/tcc-final/lib/bcheck.c | 2226 +++++ 05/tcc-final/lib/bt-dll.c | 74 + 05/tcc-final/lib/bt-exe.c | 73 + 05/tcc-final/lib/bt-log.c | 37 + 05/tcc-final/lib/dsohandle.c | 1 + 05/tcc-final/lib/fetch_and_add.S | 67 + 05/tcc-final/lib/lib-arm64.c | 677 ++ 05/tcc-final/lib/libtcc1.c | 641 ++ 05/tcc-final/lib/stdatomic.c | 124 + 05/tcc-final/lib/tcov.c | 428 + 05/tcc-final/lib/va_list.c | 67 + 05/tcc-final/libtcc.c | 2049 ++++ 05/tcc-final/libtcc.h | 111 + 05/tcc-final/riscv64-asm.c | 731 ++ 05/tcc-final/riscv64-gen.c | 1423 +++ 05/tcc-final/riscv64-link.c | 352 + 05/tcc-final/riscv64-tok.h | 193 + 05/tcc-final/stab.def | 234 + 05/tcc-final/stab.h | 17 + 05/tcc-final/tcc-doc.texi | 1396 +++ 05/tcc-final/tcc.c | 406 + 05/tcc-final/tcc.h | 1892 ++++ 05/tcc-final/tccasm.c | 1318 +++ 05/tcc-final/tcccoff.c | 948 ++ 05/tcc-final/tccdbg.c | 2119 +++++ 05/tcc-final/tccelf.c | 3836 ++++++++ 05/tcc-final/tccgen.c | 8467 +++++++++++++++++ 05/tcc-final/tcclib.h | 80 + 05/tcc-final/tccmacho.c | 1075 +++ 05/tcc-final/tccpe.c | 2060 ++++ 05/tcc-final/tccpp.c | 4101 ++++++++ 05/tcc-final/tccrun.c | 1427 +++ 05/tcc-final/tcctok.h | 408 + 05/tcc-final/tcctools.c | 568 ++ 05/tcc-final/tests/42test.h | 13 + 05/tcc-final/tests/Makefile | 336 + 05/tcc-final/tests/abitest.c | 691 ++ 05/tcc-final/tests/arm-asm-testsuite.sh | 243 + 05/tcc-final/tests/asm-c-connect-1.c | 69 + 05/tcc-final/tests/asm-c-connect-2.c | 48 + 05/tcc-final/tests/asmtest.S | 978 ++ 05/tcc-final/tests/boundtest.c | 309 + 05/tcc-final/tests/bug.c | 41 + 05/tcc-final/tests/gcctestsuite.sh | 48 + 05/tcc-final/tests/libtcc_test.c | 110 + 05/tcc-final/tests/libtcc_test_mt.c | 301 + 05/tcc-final/tests/pp/01.c | 6 + 05/tcc-final/tests/pp/01.expect | 1 + 05/tcc-final/tests/pp/02.c | 28 + 05/tcc-final/tests/pp/02.expect | 5 + 05/tcc-final/tests/pp/03.c | 15 + 05/tcc-final/tests/pp/03.expect | 5 + 05/tcc-final/tests/pp/04.c | 4 + 05/tcc-final/tests/pp/04.expect | 1 + 05/tcc-final/tests/pp/05.c | 7 + 05/tcc-final/tests/pp/05.expect | 3 + 05/tcc-final/tests/pp/06.c | 5 + 05/tcc-final/tests/pp/06.expect | 1 + 05/tcc-final/tests/pp/07.c | 4 + 05/tcc-final/tests/pp/07.expect | 2 + 05/tcc-final/tests/pp/08.c | 4 + 05/tcc-final/tests/pp/08.expect | 1 + 05/tcc-final/tests/pp/09.c | 4 + 05/tcc-final/tests/pp/09.expect | 1 + 05/tcc-final/tests/pp/10.c | 10 + 05/tcc-final/tests/pp/10.expect | 5 + 05/tcc-final/tests/pp/11.c | 31 + 05/tcc-final/tests/pp/11.expect | 15 + 05/tcc-final/tests/pp/12.S | 8 + 05/tcc-final/tests/pp/12.expect | 2 + 05/tcc-final/tests/pp/13.S | 6 + 05/tcc-final/tests/pp/13.expect | 2 + 05/tcc-final/tests/pp/14.c | 13 + 05/tcc-final/tests/pp/14.expect | 3 + 05/tcc-final/tests/pp/15.c | 18 + 05/tcc-final/tests/pp/15.expect | 5 + 05/tcc-final/tests/pp/16.c | 3 + 05/tcc-final/tests/pp/16.expect | 2 + 05/tcc-final/tests/pp/17.c | 14 + 05/tcc-final/tests/pp/17.expect | 6 + 05/tcc-final/tests/pp/18.c | 15 + 05/tcc-final/tests/pp/18.expect | 3 + 05/tcc-final/tests/pp/19.c | 101 + 05/tcc-final/tests/pp/19.expect | 14 + 05/tcc-final/tests/pp/20.c | 13 + 05/tcc-final/tests/pp/20.expect | 6 + 05/tcc-final/tests/pp/21.c | 36 + 05/tcc-final/tests/pp/21.expect | 8 + 05/tcc-final/tests/pp/Makefile | 51 + 05/tcc-final/tests/pp/pp-counter.c | 27 + 05/tcc-final/tests/pp/pp-counter.expect | 15 + 05/tcc-final/tests/tcctest.c | 4363 +++++++++ 05/tcc-final/tests/tcctest.h | 9 + 05/tcc-final/tests/testfp.c | 510 + 05/tcc-final/tests/tests2/00_assignment.c | 18 + .../tests/tests2/00_assignment.expect | 3 + 05/tcc-final/tests/tests2/01_comment.c | 14 + 05/tcc-final/tests/tests2/01_comment.expect | 5 + 05/tcc-final/tests/tests2/02_printf.c | 18 + 05/tcc-final/tests/tests2/02_printf.expect | 15 + 05/tcc-final/tests/tests2/03_struct.c | 38 + 05/tcc-final/tests/tests2/03_struct.expect | 8 + 05/tcc-final/tests/tests2/04_for.c | 15 + 05/tcc-final/tests/tests2/04_for.expect | 10 + 05/tcc-final/tests/tests2/05_array.c | 21 + 05/tcc-final/tests/tests2/05_array.expect | 10 + 05/tcc-final/tests/tests2/06_case.c | 29 + 05/tcc-final/tests/tests2/06_case.expect | 8 + 05/tcc-final/tests/tests2/07_function.c | 35 + 05/tcc-final/tests/tests2/07_function.expect | 4 + 05/tcc-final/tests/tests2/08_while.c | 24 + 05/tcc-final/tests/tests2/08_while.expect | 11 + 05/tcc-final/tests/tests2/09_do_while.c | 24 + 05/tcc-final/tests/tests2/09_do_while.expect | 11 + .../tests/tests2/100_c99array-decls.c | 34 + .../tests/tests2/100_c99array-decls.expect | 0 05/tcc-final/tests/tests2/101_cleanup.c | 227 + 05/tcc-final/tests/tests2/101_cleanup.expect | 59 + 05/tcc-final/tests/tests2/102_alignas.c | 29 + 05/tcc-final/tests/tests2/102_alignas.expect | 2 + .../tests/tests2/103_implicit_memmove.c | 20 + .../tests/tests2/103_implicit_memmove.expect | 0 05/tcc-final/tests/tests2/104+_inline.c | 54 + 05/tcc-final/tests/tests2/104_inline.c | 132 + 05/tcc-final/tests/tests2/104_inline.expect | 39 + 05/tcc-final/tests/tests2/105_local_extern.c | 12 + .../tests/tests2/105_local_extern.expect | 2 + 05/tcc-final/tests/tests2/106_versym.c | 18 + 05/tcc-final/tests/tests2/106_versym.expect | 1 + 05/tcc-final/tests/tests2/107_stack_safe.c | 13 + .../tests/tests2/107_stack_safe.expect | 1 + 05/tcc-final/tests/tests2/108_constructor.c | 20 + .../tests/tests2/108_constructor.expect | 3 + .../tests/tests2/109_float_struct_calling.c | 24 + .../tests2/109_float_struct_calling.expect | 1 + 05/tcc-final/tests/tests2/10_pointer.c | 40 + 05/tcc-final/tests/tests2/10_pointer.expect | 8 + 05/tcc-final/tests/tests2/110_average.c | 27 + 05/tcc-final/tests/tests2/110_average.expect | 1 + 05/tcc-final/tests/tests2/111_conversion.c | 22 + .../tests/tests2/111_conversion.expect | 1 + 05/tcc-final/tests/tests2/112_backtrace.c | 165 + .../tests/tests2/112_backtrace.expect | 142 + 05/tcc-final/tests/tests2/113_btdll.c | 43 + 05/tcc-final/tests/tests2/113_btdll.expect | 6 + 05/tcc-final/tests/tests2/114_bound_signal.c | 143 + .../tests/tests2/114_bound_signal.expect | 2 + 05/tcc-final/tests/tests2/115_bound_setjmp.c | 172 + .../tests/tests2/115_bound_setjmp.expect | 0 05/tcc-final/tests/tests2/116_bound_setjmp2.c | 84 + .../tests/tests2/116_bound_setjmp2.expect | 0 05/tcc-final/tests/tests2/117_builtins.c | 86 + 05/tcc-final/tests/tests2/117_builtins.expect | 4 + 05/tcc-final/tests/tests2/118_switch.c | 75 + 05/tcc-final/tests/tests2/118_switch.expect | 40 + 05/tcc-final/tests/tests2/119_random_stuff.c | 108 + .../tests/tests2/119_random_stuff.expect | 5 + 05/tcc-final/tests/tests2/11_precedence.c | 41 + .../tests/tests2/11_precedence.expect | 15 + 05/tcc-final/tests/tests2/120+_alias.c | 15 + 05/tcc-final/tests/tests2/120_alias.c | 29 + 05/tcc-final/tests/tests2/120_alias.expect | 5 + 05/tcc-final/tests/tests2/121_struct_return.c | 35 + .../tests/tests2/121_struct_return.expect | 1 + 05/tcc-final/tests/tests2/122_vla_reuse.c | 31 + .../tests/tests2/122_vla_reuse.expect | 1 + 05/tcc-final/tests/tests2/123_vla_bug.c | 40 + 05/tcc-final/tests/tests2/123_vla_bug.expect | 5 + .../tests/tests2/124_atomic_counter.c | 75 + .../tests/tests2/124_atomic_counter.expect | 2 + 05/tcc-final/tests/tests2/125_atomic_misc.c | 108 + .../tests/tests2/125_atomic_misc.expect | 33 + 05/tcc-final/tests/tests2/126_bound_global.c | 13 + .../tests/tests2/126_bound_global.expect | 2 + 05/tcc-final/tests/tests2/12_hashdefine.c | 14 + .../tests/tests2/12_hashdefine.expect | 2 + .../tests/tests2/13_integer_literals.c | 20 + .../tests/tests2/13_integer_literals.expect | 5 + 05/tcc-final/tests/tests2/14_if.c | 21 + 05/tcc-final/tests/tests2/14_if.expect | 2 + 05/tcc-final/tests/tests2/15_recursion.c | 21 + 05/tcc-final/tests/tests2/15_recursion.expect | 10 + 05/tcc-final/tests/tests2/16_nesting.c | 21 + 05/tcc-final/tests/tests2/16_nesting.expect | 18 + 05/tcc-final/tests/tests2/17_enum.c | 72 + 05/tcc-final/tests/tests2/17_enum.expect | 4 + 05/tcc-final/tests/tests2/18_include.c | 12 + 05/tcc-final/tests/tests2/18_include.expect | 3 + 05/tcc-final/tests/tests2/18_include.h | 1 + .../tests/tests2/19_pointer_arithmetic.c | 28 + .../tests/tests2/19_pointer_arithmetic.expect | 3 + .../tests/tests2/20_pointer_comparison.c | 24 + .../tests/tests2/20_pointer_comparison.expect | 6 + 05/tcc-final/tests/tests2/21_char_array.c | 33 + .../tests/tests2/21_char_array.expect | 7 + 05/tcc-final/tests/tests2/22_floating_point.c | 63 + .../tests/tests2/22_floating_point.expect | 16 + 05/tcc-final/tests/tests2/23_type_coercion.c | 54 + .../tests/tests2/23_type_coercion.expect | 12 + 05/tcc-final/tests/tests2/24_math_library.c | 30 + .../tests/tests2/24_math_library.expect | 18 + 05/tcc-final/tests/tests2/25_quicksort.c | 83 + 05/tcc-final/tests/tests2/25_quicksort.expect | 2 + .../tests/tests2/26_character_constants.c | 17 + .../tests2/26_character_constants.expect | 8 + 05/tcc-final/tests/tests2/27_sizeof.c | 18 + 05/tcc-final/tests/tests2/27_sizeof.expect | 4 + 05/tcc-final/tests/tests2/28_strings.c | 45 + 05/tcc-final/tests/tests2/28_strings.expect | 19 + 05/tcc-final/tests/tests2/29_array_address.c | 13 + .../tests/tests2/29_array_address.expect | 1 + 05/tcc-final/tests/tests2/30_hanoi.c | 122 + 05/tcc-final/tests/tests2/30_hanoi.expect | 71 + 05/tcc-final/tests/tests2/31_args.c | 14 + 05/tcc-final/tests/tests2/31_args.expect | 6 + 05/tcc-final/tests/tests2/32_led.c | 266 + 05/tcc-final/tests/tests2/32_led.expect | 4 + 05/tcc-final/tests/tests2/33_ternary_op.c | 80 + .../tests/tests2/33_ternary_op.expect | 12 + .../tests/tests2/34_array_assignment.c | 23 + .../tests/tests2/34_array_assignment.expect | 2 + 05/tcc-final/tests/tests2/35_sizeof.c | 14 + 05/tcc-final/tests/tests2/35_sizeof.expect | 2 + .../tests/tests2/36_array_initialisers.c | 25 + .../tests/tests2/36_array_initialisers.expect | 30 + 05/tcc-final/tests/tests2/37_sprintf.c | 17 + 05/tcc-final/tests/tests2/37_sprintf.expect | 20 + .../tests/tests2/38_multiple_array_index.c | 32 + .../tests2/38_multiple_array_index.expect | 4 + 05/tcc-final/tests/tests2/39_typedef.c | 65 + 05/tcc-final/tests/tests2/39_typedef.expect | 3 + 05/tcc-final/tests/tests2/40_stdio.c | 52 + 05/tcc-final/tests/tests2/40_stdio.expect | 27 + 05/tcc-final/tests/tests2/41_hashif.c | 85 + 05/tcc-final/tests/tests2/41_hashif.expect | 6 + .../tests/tests2/42_function_pointer.c | 22 + .../tests/tests2/42_function_pointer.expect | 2 + 05/tcc-final/tests/tests2/43_void_param.c | 15 + .../tests/tests2/43_void_param.expect | 1 + .../tests/tests2/44_scoped_declarations.c | 17 + .../tests2/44_scoped_declarations.expect | 1 + 05/tcc-final/tests/tests2/45_empty_for.c | 18 + 05/tcc-final/tests/tests2/45_empty_for.expect | 10 + 05/tcc-final/tests/tests2/46_grep.c | 570 ++ 05/tcc-final/tests/tests2/46_grep.expect | 3 + 05/tcc-final/tests/tests2/47_switch_return.c | 24 + .../tests/tests2/47_switch_return.expect | 4 + 05/tcc-final/tests/tests2/48_nested_break.c | 26 + .../tests/tests2/48_nested_break.expect | 1 + .../tests/tests2/49_bracket_evaluation.c | 23 + .../tests/tests2/49_bracket_evaluation.expect | 1 + .../tests/tests2/50_logical_second_arg.c | 29 + .../tests/tests2/50_logical_second_arg.expect | 20 + 05/tcc-final/tests/tests2/51_static.c | 30 + 05/tcc-final/tests/tests2/51_static.expect | 8 + 05/tcc-final/tests/tests2/52_unnamed_enum.c | 27 + .../tests/tests2/52_unnamed_enum.expect | 9 + 05/tcc-final/tests/tests2/54_goto.c | 56 + 05/tcc-final/tests/tests2/54_goto.expect | 8 + 05/tcc-final/tests/tests2/55_lshift_type.c | 52 + .../tests/tests2/55_lshift_type.expect | 1 + .../tests/tests2/60_errors_and_warnings.c | 451 + .../tests2/60_errors_and_warnings.expect | 221 + 05/tcc-final/tests/tests2/61_integers.c | 70 + 05/tcc-final/tests/tests2/61_integers.expect | 56 + 05/tcc-final/tests/tests2/64_macro_nesting.c | 12 + .../tests/tests2/64_macro_nesting.expect | 1 + 05/tcc-final/tests/tests2/67_macro_concat.c | 14 + .../tests/tests2/67_macro_concat.expect | 2 + .../tests/tests2/70_floating_point_literals.c | 77 + .../tests2/70_floating_point_literals.expect | 53 + .../tests/tests2/71_macro_empty_arg.c | 9 + .../tests/tests2/71_macro_empty_arg.expect | 1 + .../tests/tests2/72_long_long_constant.c | 19 + .../tests/tests2/72_long_long_constant.expect | 1 + 05/tcc-final/tests/tests2/73_arm64.c | 538 ++ 05/tcc-final/tests/tests2/73_arm64.expect | 174 + .../tests/tests2/75_array_in_struct_init.c | 33 + .../tests2/75_array_in_struct_init.expect | 72 + .../tests/tests2/76_dollars_in_identifiers.c | 41 + .../tests2/76_dollars_in_identifiers.expect | 14 + 05/tcc-final/tests/tests2/77_push_pop_macro.c | 30 + .../tests/tests2/77_push_pop_macro.expect | 5 + 05/tcc-final/tests/tests2/78_vla_label.c | 45 + 05/tcc-final/tests/tests2/78_vla_label.expect | 6 + 05/tcc-final/tests/tests2/79_vla_continue.c | 119 + .../tests/tests2/79_vla_continue.expect | 5 + 05/tcc-final/tests/tests2/80_flexarray.c | 26 + 05/tcc-final/tests/tests2/80_flexarray.expect | 0 05/tcc-final/tests/tests2/81_types.c | 55 + 05/tcc-final/tests/tests2/81_types.expect | 0 .../tests/tests2/82_attribs_position.c | 65 + .../tests/tests2/82_attribs_position.expect | 2 + .../tests/tests2/83_utf8_in_identifiers.c | 9 + .../tests2/83_utf8_in_identifiers.expect | 2 + 05/tcc-final/tests/tests2/84_hex-float.c | 12 + 05/tcc-final/tests/tests2/84_hex-float.expect | 1 + .../tests/tests2/85_asm-outside-function.c | 15 + .../tests2/85_asm-outside-function.expect | 1 + 05/tcc-final/tests/tests2/86_memory-model.c | 38 + .../tests/tests2/86_memory-model.expect | 1 + 05/tcc-final/tests/tests2/87_dead_code.c | 165 + 05/tcc-final/tests/tests2/87_dead_code.expect | 24 + 05/tcc-final/tests/tests2/88_codeopt.c | 69 + 05/tcc-final/tests/tests2/88_codeopt.expect | 2 + 05/tcc-final/tests/tests2/89_nocode_wanted.c | 112 + .../tests/tests2/89_nocode_wanted.expect | 14 + 05/tcc-final/tests/tests2/90_struct-init.c | 407 + .../tests/tests2/90_struct-init.expect | 58 + .../tests/tests2/91_ptr_longlong_arith32.c | 15 + .../tests2/91_ptr_longlong_arith32.expect | 1 + 05/tcc-final/tests/tests2/92_enum_bitfield.c | 58 + .../tests/tests2/92_enum_bitfield.expect | 0 .../tests/tests2/93_integer_promotion.c | 71 + .../tests/tests2/93_integer_promotion.expect | 46 + 05/tcc-final/tests/tests2/94_generic.c | 119 + 05/tcc-final/tests/tests2/94_generic.expect | 15 + 05/tcc-final/tests/tests2/95_bitfields.c | 241 + 05/tcc-final/tests/tests2/95_bitfields.expect | 173 + 05/tcc-final/tests/tests2/95_bitfields_ms.c | 2 + .../tests/tests2/95_bitfields_ms.expect | 173 + 05/tcc-final/tests/tests2/96_nodata_wanted.c | 102 + .../tests/tests2/96_nodata_wanted.expect | 26 + .../tests/tests2/97_utf8_string_literal.c | 20 + .../tests2/97_utf8_string_literal.expect | 1 + 05/tcc-final/tests/tests2/98_al_ax_extend.c | 41 + .../tests/tests2/98_al_ax_extend.expect | 9 + 05/tcc-final/tests/tests2/99_fastcall.c | 276 + 05/tcc-final/tests/tests2/99_fastcall.expect | 1 + 05/tcc-final/tests/tests2/LICENSE | 37 + 05/tcc-final/tests/tests2/Makefile | 183 + 05/tcc-final/tests/vla_test.c | 84 + 05/tcc-final/texi2pod.pl | 427 + 05/tcc-final/win32/build-tcc.bat | 196 + 05/tcc-final/win32/examples/dll.c | 13 + 05/tcc-final/win32/examples/fib.c | 24 + 05/tcc-final/win32/examples/hello_dll.c | 20 + 05/tcc-final/win32/examples/hello_win.c | 163 + 05/tcc-final/win32/include/_mingw.h | 164 + 05/tcc-final/win32/include/assert.h | 62 + 05/tcc-final/win32/include/conio.h | 409 + 05/tcc-final/win32/include/ctype.h | 281 + 05/tcc-final/win32/include/dir.h | 31 + 05/tcc-final/win32/include/direct.h | 68 + 05/tcc-final/win32/include/dirent.h | 135 + 05/tcc-final/win32/include/dos.h | 55 + 05/tcc-final/win32/include/errno.h | 75 + 05/tcc-final/win32/include/excpt.h | 123 + 05/tcc-final/win32/include/fcntl.h | 52 + 05/tcc-final/win32/include/fenv.h | 108 + 05/tcc-final/win32/include/inttypes.h | 297 + 05/tcc-final/win32/include/io.h | 418 + 05/tcc-final/win32/include/iso646.h | 36 + 05/tcc-final/win32/include/limits.h | 116 + 05/tcc-final/win32/include/locale.h | 91 + 05/tcc-final/win32/include/malloc.h | 181 + 05/tcc-final/win32/include/math.h | 497 + 05/tcc-final/win32/include/mem.h | 13 + 05/tcc-final/win32/include/memory.h | 40 + 05/tcc-final/win32/include/process.h | 176 + 05/tcc-final/win32/include/sec_api/conio_s.h | 42 + 05/tcc-final/win32/include/sec_api/crtdbg_s.h | 19 + 05/tcc-final/win32/include/sec_api/io_s.h | 33 + .../win32/include/sec_api/mbstring_s.h | 52 + 05/tcc-final/win32/include/sec_api/search_s.h | 25 + 05/tcc-final/win32/include/sec_api/stdio_s.h | 145 + 05/tcc-final/win32/include/sec_api/stdlib_s.h | 67 + .../win32/include/sec_api/stralign_s.h | 30 + 05/tcc-final/win32/include/sec_api/string_s.h | 41 + .../win32/include/sec_api/sys/timeb_s.h | 34 + 05/tcc-final/win32/include/sec_api/tchar_s.h | 266 + 05/tcc-final/win32/include/sec_api/time_s.h | 61 + 05/tcc-final/win32/include/sec_api/wchar_s.h | 128 + 05/tcc-final/win32/include/setjmp.h | 160 + 05/tcc-final/win32/include/share.h | 28 + 05/tcc-final/win32/include/signal.h | 63 + 05/tcc-final/win32/include/stdint.h | 212 + 05/tcc-final/win32/include/stdio.h | 429 + 05/tcc-final/win32/include/stdlib.h | 580 ++ 05/tcc-final/win32/include/string.h | 164 + 05/tcc-final/win32/include/sys/fcntl.h | 13 + 05/tcc-final/win32/include/sys/file.h | 14 + 05/tcc-final/win32/include/sys/locking.h | 30 + 05/tcc-final/win32/include/sys/stat.h | 290 + 05/tcc-final/win32/include/sys/time.h | 69 + 05/tcc-final/win32/include/sys/timeb.h | 133 + 05/tcc-final/win32/include/sys/types.h | 118 + 05/tcc-final/win32/include/sys/unistd.h | 14 + 05/tcc-final/win32/include/sys/utime.h | 146 + 05/tcc-final/win32/include/tchar.h | 1102 +++ 05/tcc-final/win32/include/time.h | 287 + 05/tcc-final/win32/include/uchar.h | 33 + 05/tcc-final/win32/include/vadefs.h | 11 + 05/tcc-final/win32/include/values.h | 4 + 05/tcc-final/win32/include/wchar.h | 873 ++ 05/tcc-final/win32/include/wctype.h | 172 + 05/tcc-final/win32/include/winapi/basetsd.h | 149 + 05/tcc-final/win32/include/winapi/basetyps.h | 85 + 05/tcc-final/win32/include/winapi/guiddef.h | 156 + 05/tcc-final/win32/include/winapi/poppack.h | 8 + 05/tcc-final/win32/include/winapi/pshpack1.h | 8 + 05/tcc-final/win32/include/winapi/pshpack2.h | 8 + 05/tcc-final/win32/include/winapi/pshpack4.h | 8 + 05/tcc-final/win32/include/winapi/pshpack8.h | 8 + 05/tcc-final/win32/include/winapi/qos.h | 72 + 05/tcc-final/win32/include/winapi/winbase.h | 2951 ++++++ 05/tcc-final/win32/include/winapi/wincon.h | 301 + 05/tcc-final/win32/include/winapi/windef.h | 293 + 05/tcc-final/win32/include/winapi/windows.h | 127 + 05/tcc-final/win32/include/winapi/winerror.h | 3166 ++++++ 05/tcc-final/win32/include/winapi/wingdi.h | 4080 ++++++++ 05/tcc-final/win32/include/winapi/winnls.h | 778 ++ 05/tcc-final/win32/include/winapi/winnt.h | 5835 ++++++++++++ 05/tcc-final/win32/include/winapi/winreg.h | 272 + 05/tcc-final/win32/include/winapi/winsock2.h | 1474 +++ 05/tcc-final/win32/include/winapi/winuser.h | 5651 +++++++++++ 05/tcc-final/win32/include/winapi/winver.h | 160 + 05/tcc-final/win32/include/winapi/ws2ipdef.h | 21 + 05/tcc-final/win32/include/winapi/ws2tcpip.h | 391 + 05/tcc-final/win32/lib/chkstk.S | 73 + 05/tcc-final/win32/lib/crt1.c | 95 + 05/tcc-final/win32/lib/crt1w.c | 3 + 05/tcc-final/win32/lib/crtinit.c | 26 + 05/tcc-final/win32/lib/dllcrt1.c | 18 + 05/tcc-final/win32/lib/dllmain.c | 9 + 05/tcc-final/win32/lib/gdi32.def | 337 + 05/tcc-final/win32/lib/kernel32.def | 773 ++ 05/tcc-final/win32/lib/msvcrt.def | 1320 +++ 05/tcc-final/win32/lib/user32.def | 658 ++ 05/tcc-final/win32/lib/wincrt1.c | 86 + 05/tcc-final/win32/lib/wincrt1w.c | 3 + 05/tcc-final/win32/lib/ws2_32.def | 198 + 05/tcc-final/win32/tcc-win32.txt | 168 + 05/tcc-final/x86_64-asm.h | 527 + 05/tcc-final/x86_64-gen.c | 2327 +++++ 05/tcc-final/x86_64-link.c | 399 + 05/test | Bin 125656 -> 115220 bytes 06 | 1 + Makefile | 5 +- scripts/run.sh | 3 +- 890 files changed, 229323 insertions(+), 20 deletions(-) create mode 100644 05/tcc-final-old/.gitignore create mode 100644 05/tcc-final-old/COPYING create mode 100644 05/tcc-final-old/Changelog create mode 100644 05/tcc-final-old/CodingStyle create mode 100644 05/tcc-final-old/Makefile create mode 100644 05/tcc-final-old/README create mode 100644 05/tcc-final-old/RELICENSING create mode 100644 05/tcc-final-old/TODO create mode 100644 05/tcc-final-old/VERSION create mode 100644 05/tcc-final-old/arm-asm.c create mode 100644 05/tcc-final-old/arm-gen.c create mode 100644 05/tcc-final-old/arm-link.c create mode 100644 05/tcc-final-old/arm64-gen.c create mode 100644 05/tcc-final-old/arm64-link.c create mode 100644 05/tcc-final-old/c67-gen.c create mode 100644 05/tcc-final-old/c67-link.c create mode 100644 05/tcc-final-old/coff.h create mode 100644 05/tcc-final-old/config.mak create mode 100755 05/tcc-final-old/configure create mode 100644 05/tcc-final-old/elf.h create mode 100755 05/tcc-final-old/examples/ex1.c create mode 100644 05/tcc-final-old/examples/ex2.c create mode 100644 05/tcc-final-old/examples/ex3.c create mode 100755 05/tcc-final-old/examples/ex4.c create mode 100644 05/tcc-final-old/examples/ex5.c create mode 100644 05/tcc-final-old/i386-asm.c create mode 100644 05/tcc-final-old/i386-asm.h create mode 100644 05/tcc-final-old/i386-gen.c create mode 100644 05/tcc-final-old/i386-link.c create mode 100644 05/tcc-final-old/i386-tok.h create mode 100644 05/tcc-final-old/il-gen.c create mode 100644 05/tcc-final-old/il-opcodes.h create mode 100644 05/tcc-final-old/include/float.h create mode 100644 05/tcc-final-old/include/stdarg.h create mode 100644 05/tcc-final-old/include/stdbool.h create mode 100644 05/tcc-final-old/include/stddef.h create mode 100644 05/tcc-final-old/include/varargs.h create mode 100644 05/tcc-final-old/lib/Makefile create mode 100644 05/tcc-final-old/lib/alloca-arm.S create mode 100644 05/tcc-final-old/lib/alloca86-bt.S create mode 100644 05/tcc-final-old/lib/alloca86.S create mode 100644 05/tcc-final-old/lib/alloca86_64-bt.S create mode 100644 05/tcc-final-old/lib/alloca86_64.S create mode 100644 05/tcc-final-old/lib/armeabi.c create mode 100644 05/tcc-final-old/lib/armflush.c create mode 100644 05/tcc-final-old/lib/bcheck.c create mode 100644 05/tcc-final-old/lib/lib-arm64.c create mode 100644 05/tcc-final-old/lib/libtcc1.c create mode 100644 05/tcc-final-old/lib/va_list.c create mode 100644 05/tcc-final-old/libtcc.c create mode 100644 05/tcc-final-old/libtcc.h create mode 100644 05/tcc-final-old/stab.def create mode 100644 05/tcc-final-old/stab.h create mode 100644 05/tcc-final-old/tcc-doc.texi create mode 100644 05/tcc-final-old/tcc.c create mode 100644 05/tcc-final-old/tcc.h create mode 100644 05/tcc-final-old/tccasm.c create mode 100644 05/tcc-final-old/tcccoff.c create mode 100644 05/tcc-final-old/tccelf.c create mode 100644 05/tcc-final-old/tccgen.c create mode 100644 05/tcc-final-old/tcclib.h create mode 100644 05/tcc-final-old/tccpe.c create mode 100644 05/tcc-final-old/tccpp.c create mode 100644 05/tcc-final-old/tccrun.c create mode 100644 05/tcc-final-old/tcctok.h create mode 100644 05/tcc-final-old/tcctools.c create mode 100644 05/tcc-final-old/tests/42test.h create mode 100644 05/tcc-final-old/tests/Makefile create mode 100644 05/tcc-final-old/tests/abitest.c create mode 100644 05/tcc-final-old/tests/asm-c-connect-1.c create mode 100644 05/tcc-final-old/tests/asm-c-connect-2.c create mode 100644 05/tcc-final-old/tests/asmtest.S create mode 100644 05/tcc-final-old/tests/boundtest.c create mode 100755 05/tcc-final-old/tests/gcctestsuite.sh create mode 100644 05/tcc-final-old/tests/libtcc_test.c create mode 100644 05/tcc-final-old/tests/pp/01.c create mode 100644 05/tcc-final-old/tests/pp/01.expect create mode 100644 05/tcc-final-old/tests/pp/02.c create mode 100644 05/tcc-final-old/tests/pp/02.expect create mode 100644 05/tcc-final-old/tests/pp/03.c create mode 100644 05/tcc-final-old/tests/pp/03.expect create mode 100644 05/tcc-final-old/tests/pp/04.c create mode 100644 05/tcc-final-old/tests/pp/04.expect create mode 100644 05/tcc-final-old/tests/pp/05.c create mode 100644 05/tcc-final-old/tests/pp/05.expect create mode 100644 05/tcc-final-old/tests/pp/06.c create mode 100644 05/tcc-final-old/tests/pp/06.expect create mode 100644 05/tcc-final-old/tests/pp/07.c create mode 100644 05/tcc-final-old/tests/pp/07.expect create mode 100644 05/tcc-final-old/tests/pp/08.c create mode 100644 05/tcc-final-old/tests/pp/08.expect create mode 100644 05/tcc-final-old/tests/pp/09.c create mode 100644 05/tcc-final-old/tests/pp/09.expect create mode 100644 05/tcc-final-old/tests/pp/10.c create mode 100644 05/tcc-final-old/tests/pp/10.expect create mode 100644 05/tcc-final-old/tests/pp/11.c create mode 100644 05/tcc-final-old/tests/pp/11.expect create mode 100644 05/tcc-final-old/tests/pp/12.S create mode 100644 05/tcc-final-old/tests/pp/12.expect create mode 100644 05/tcc-final-old/tests/pp/13.S create mode 100644 05/tcc-final-old/tests/pp/13.expect create mode 100644 05/tcc-final-old/tests/pp/14.c create mode 100644 05/tcc-final-old/tests/pp/14.expect create mode 100644 05/tcc-final-old/tests/pp/15.c create mode 100644 05/tcc-final-old/tests/pp/15.expect create mode 100644 05/tcc-final-old/tests/pp/16.c create mode 100644 05/tcc-final-old/tests/pp/16.expect create mode 100644 05/tcc-final-old/tests/pp/17.c create mode 100644 05/tcc-final-old/tests/pp/17.expect create mode 100644 05/tcc-final-old/tests/pp/18.c create mode 100644 05/tcc-final-old/tests/pp/18.expect create mode 100644 05/tcc-final-old/tests/pp/19.c create mode 100644 05/tcc-final-old/tests/pp/19.expect create mode 100644 05/tcc-final-old/tests/pp/20.c create mode 100644 05/tcc-final-old/tests/pp/20.expect create mode 100644 05/tcc-final-old/tests/pp/21.c create mode 100644 05/tcc-final-old/tests/pp/21.expect create mode 100644 05/tcc-final-old/tests/pp/Makefile create mode 100644 05/tcc-final-old/tests/pp/pp-counter.c create mode 100644 05/tcc-final-old/tests/pp/pp-counter.expect create mode 100644 05/tcc-final-old/tests/tcctest.c create mode 100644 05/tcc-final-old/tests/tcctest.h create mode 100644 05/tcc-final-old/tests/testfp.c create mode 100644 05/tcc-final-old/tests/tests2/00_assignment.c create mode 100644 05/tcc-final-old/tests/tests2/00_assignment.expect create mode 100644 05/tcc-final-old/tests/tests2/01_comment.c create mode 100644 05/tcc-final-old/tests/tests2/01_comment.expect create mode 100644 05/tcc-final-old/tests/tests2/02_printf.c create mode 100644 05/tcc-final-old/tests/tests2/02_printf.expect create mode 100644 05/tcc-final-old/tests/tests2/03_struct.c create mode 100644 05/tcc-final-old/tests/tests2/03_struct.expect create mode 100644 05/tcc-final-old/tests/tests2/04_for.c create mode 100644 05/tcc-final-old/tests/tests2/04_for.expect create mode 100644 05/tcc-final-old/tests/tests2/05_array.c create mode 100644 05/tcc-final-old/tests/tests2/05_array.expect create mode 100644 05/tcc-final-old/tests/tests2/06_case.c create mode 100644 05/tcc-final-old/tests/tests2/06_case.expect create mode 100644 05/tcc-final-old/tests/tests2/07_function.c create mode 100644 05/tcc-final-old/tests/tests2/07_function.expect create mode 100644 05/tcc-final-old/tests/tests2/08_while.c create mode 100644 05/tcc-final-old/tests/tests2/08_while.expect create mode 100644 05/tcc-final-old/tests/tests2/09_do_while.c create mode 100644 05/tcc-final-old/tests/tests2/09_do_while.expect create mode 100644 05/tcc-final-old/tests/tests2/10_pointer.c create mode 100644 05/tcc-final-old/tests/tests2/10_pointer.expect create mode 100644 05/tcc-final-old/tests/tests2/11_precedence.c create mode 100644 05/tcc-final-old/tests/tests2/11_precedence.expect create mode 100644 05/tcc-final-old/tests/tests2/12_hashdefine.c create mode 100644 05/tcc-final-old/tests/tests2/12_hashdefine.expect create mode 100644 05/tcc-final-old/tests/tests2/13_integer_literals.c create mode 100644 05/tcc-final-old/tests/tests2/13_integer_literals.expect create mode 100644 05/tcc-final-old/tests/tests2/14_if.c create mode 100644 05/tcc-final-old/tests/tests2/14_if.expect create mode 100644 05/tcc-final-old/tests/tests2/15_recursion.c create mode 100644 05/tcc-final-old/tests/tests2/15_recursion.expect create mode 100644 05/tcc-final-old/tests/tests2/16_nesting.c create mode 100644 05/tcc-final-old/tests/tests2/16_nesting.expect create mode 100644 05/tcc-final-old/tests/tests2/17_enum.c create mode 100644 05/tcc-final-old/tests/tests2/17_enum.expect create mode 100644 05/tcc-final-old/tests/tests2/18_include.c create mode 100644 05/tcc-final-old/tests/tests2/18_include.expect create mode 100644 05/tcc-final-old/tests/tests2/18_include.h create mode 100644 05/tcc-final-old/tests/tests2/19_pointer_arithmetic.c create mode 100644 05/tcc-final-old/tests/tests2/19_pointer_arithmetic.expect create mode 100644 05/tcc-final-old/tests/tests2/20_pointer_comparison.c create mode 100644 05/tcc-final-old/tests/tests2/20_pointer_comparison.expect create mode 100644 05/tcc-final-old/tests/tests2/21_char_array.c create mode 100644 05/tcc-final-old/tests/tests2/21_char_array.expect create mode 100644 05/tcc-final-old/tests/tests2/22_floating_point.c create mode 100644 05/tcc-final-old/tests/tests2/22_floating_point.expect create mode 100644 05/tcc-final-old/tests/tests2/23_type_coercion.c create mode 100644 05/tcc-final-old/tests/tests2/23_type_coercion.expect create mode 100644 05/tcc-final-old/tests/tests2/24_math_library.c create mode 100644 05/tcc-final-old/tests/tests2/24_math_library.expect create mode 100644 05/tcc-final-old/tests/tests2/25_quicksort.c create mode 100644 05/tcc-final-old/tests/tests2/25_quicksort.expect create mode 100644 05/tcc-final-old/tests/tests2/26_character_constants.c create mode 100644 05/tcc-final-old/tests/tests2/26_character_constants.expect create mode 100644 05/tcc-final-old/tests/tests2/27_sizeof.c create mode 100644 05/tcc-final-old/tests/tests2/27_sizeof.expect create mode 100644 05/tcc-final-old/tests/tests2/28_strings.c create mode 100644 05/tcc-final-old/tests/tests2/28_strings.expect create mode 100644 05/tcc-final-old/tests/tests2/29_array_address.c create mode 100644 05/tcc-final-old/tests/tests2/29_array_address.expect create mode 100644 05/tcc-final-old/tests/tests2/30_hanoi.c create mode 100644 05/tcc-final-old/tests/tests2/30_hanoi.expect create mode 100644 05/tcc-final-old/tests/tests2/31_args.c create mode 100644 05/tcc-final-old/tests/tests2/31_args.expect create mode 100644 05/tcc-final-old/tests/tests2/32_led.c create mode 100644 05/tcc-final-old/tests/tests2/32_led.expect create mode 100644 05/tcc-final-old/tests/tests2/33_ternary_op.c create mode 100644 05/tcc-final-old/tests/tests2/33_ternary_op.expect create mode 100644 05/tcc-final-old/tests/tests2/34_array_assignment.c create mode 100644 05/tcc-final-old/tests/tests2/34_array_assignment.expect create mode 100644 05/tcc-final-old/tests/tests2/35_sizeof.c create mode 100644 05/tcc-final-old/tests/tests2/35_sizeof.expect create mode 100644 05/tcc-final-old/tests/tests2/36_array_initialisers.c create mode 100644 05/tcc-final-old/tests/tests2/36_array_initialisers.expect create mode 100644 05/tcc-final-old/tests/tests2/37_sprintf.c create mode 100644 05/tcc-final-old/tests/tests2/37_sprintf.expect create mode 100644 05/tcc-final-old/tests/tests2/38_multiple_array_index.c create mode 100644 05/tcc-final-old/tests/tests2/38_multiple_array_index.expect create mode 100644 05/tcc-final-old/tests/tests2/39_typedef.c create mode 100644 05/tcc-final-old/tests/tests2/39_typedef.expect create mode 100644 05/tcc-final-old/tests/tests2/40_stdio.c create mode 100644 05/tcc-final-old/tests/tests2/40_stdio.expect create mode 100644 05/tcc-final-old/tests/tests2/41_hashif.c create mode 100644 05/tcc-final-old/tests/tests2/41_hashif.expect create mode 100644 05/tcc-final-old/tests/tests2/42_function_pointer.c create mode 100644 05/tcc-final-old/tests/tests2/42_function_pointer.expect create mode 100644 05/tcc-final-old/tests/tests2/43_void_param.c create mode 100644 05/tcc-final-old/tests/tests2/43_void_param.expect create mode 100644 05/tcc-final-old/tests/tests2/44_scoped_declarations.c create mode 100644 05/tcc-final-old/tests/tests2/44_scoped_declarations.expect create mode 100644 05/tcc-final-old/tests/tests2/45_empty_for.c create mode 100644 05/tcc-final-old/tests/tests2/45_empty_for.expect create mode 100644 05/tcc-final-old/tests/tests2/46_grep.c create mode 100644 05/tcc-final-old/tests/tests2/46_grep.expect create mode 100644 05/tcc-final-old/tests/tests2/47_switch_return.c create mode 100644 05/tcc-final-old/tests/tests2/47_switch_return.expect create mode 100644 05/tcc-final-old/tests/tests2/48_nested_break.c create mode 100644 05/tcc-final-old/tests/tests2/48_nested_break.expect create mode 100644 05/tcc-final-old/tests/tests2/49_bracket_evaluation.c create mode 100644 05/tcc-final-old/tests/tests2/49_bracket_evaluation.expect create mode 100644 05/tcc-final-old/tests/tests2/50_logical_second_arg.c create mode 100644 05/tcc-final-old/tests/tests2/50_logical_second_arg.expect create mode 100644 05/tcc-final-old/tests/tests2/51_static.c create mode 100644 05/tcc-final-old/tests/tests2/51_static.expect create mode 100644 05/tcc-final-old/tests/tests2/52_unnamed_enum.c create mode 100644 05/tcc-final-old/tests/tests2/52_unnamed_enum.expect create mode 100644 05/tcc-final-old/tests/tests2/54_goto.c create mode 100644 05/tcc-final-old/tests/tests2/54_goto.expect create mode 100644 05/tcc-final-old/tests/tests2/55_lshift_type.c create mode 100644 05/tcc-final-old/tests/tests2/55_lshift_type.expect create mode 100644 05/tcc-final-old/tests/tests2/60_errors_and_warnings.c create mode 100644 05/tcc-final-old/tests/tests2/60_errors_and_warnings.expect create mode 100644 05/tcc-final-old/tests/tests2/64_macro_nesting.c create mode 100644 05/tcc-final-old/tests/tests2/64_macro_nesting.expect create mode 100644 05/tcc-final-old/tests/tests2/67_macro_concat.c create mode 100644 05/tcc-final-old/tests/tests2/67_macro_concat.expect create mode 100644 05/tcc-final-old/tests/tests2/70_floating_point_literals.c create mode 100644 05/tcc-final-old/tests/tests2/70_floating_point_literals.expect create mode 100644 05/tcc-final-old/tests/tests2/71_macro_empty_arg.c create mode 100644 05/tcc-final-old/tests/tests2/71_macro_empty_arg.expect create mode 100644 05/tcc-final-old/tests/tests2/72_long_long_constant.c create mode 100644 05/tcc-final-old/tests/tests2/72_long_long_constant.expect create mode 100644 05/tcc-final-old/tests/tests2/73_arm64.c create mode 100644 05/tcc-final-old/tests/tests2/73_arm64.expect create mode 100644 05/tcc-final-old/tests/tests2/75_array_in_struct_init.c create mode 100644 05/tcc-final-old/tests/tests2/75_array_in_struct_init.expect create mode 100644 05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.c create mode 100644 05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.expect create mode 100644 05/tcc-final-old/tests/tests2/77_push_pop_macro.c create mode 100644 05/tcc-final-old/tests/tests2/77_push_pop_macro.expect create mode 100644 05/tcc-final-old/tests/tests2/78_vla_label.c create mode 100644 05/tcc-final-old/tests/tests2/78_vla_label.expect create mode 100644 05/tcc-final-old/tests/tests2/79_vla_continue.c create mode 100644 05/tcc-final-old/tests/tests2/79_vla_continue.expect create mode 100644 05/tcc-final-old/tests/tests2/80_flexarray.c create mode 100644 05/tcc-final-old/tests/tests2/80_flexarray.expect create mode 100644 05/tcc-final-old/tests/tests2/81_types.c create mode 100644 05/tcc-final-old/tests/tests2/81_types.expect create mode 100644 05/tcc-final-old/tests/tests2/82_attribs_position.c create mode 100644 05/tcc-final-old/tests/tests2/82_attribs_position.expect create mode 100644 05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.c create mode 100644 05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.expect create mode 100644 05/tcc-final-old/tests/tests2/84_hex-float.c create mode 100644 05/tcc-final-old/tests/tests2/84_hex-float.expect create mode 100644 05/tcc-final-old/tests/tests2/85_asm-outside-function.c create mode 100644 05/tcc-final-old/tests/tests2/85_asm-outside-function.expect create mode 100644 05/tcc-final-old/tests/tests2/86_memory-model.c create mode 100644 05/tcc-final-old/tests/tests2/86_memory-model.expect create mode 100644 05/tcc-final-old/tests/tests2/87_dead_code.c create mode 100644 05/tcc-final-old/tests/tests2/87_dead_code.expect create mode 100644 05/tcc-final-old/tests/tests2/88_codeopt.c create mode 100644 05/tcc-final-old/tests/tests2/88_codeopt.expect create mode 100644 05/tcc-final-old/tests/tests2/89_nocode_wanted.c create mode 100644 05/tcc-final-old/tests/tests2/89_nocode_wanted.expect create mode 100644 05/tcc-final-old/tests/tests2/90_struct-init.c create mode 100644 05/tcc-final-old/tests/tests2/90_struct-init.expect create mode 100644 05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.c create mode 100644 05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.expect create mode 100644 05/tcc-final-old/tests/tests2/92_enum_bitfield.c create mode 100644 05/tcc-final-old/tests/tests2/92_enum_bitfield.expect create mode 100644 05/tcc-final-old/tests/tests2/93_integer_promotion.c create mode 100644 05/tcc-final-old/tests/tests2/93_integer_promotion.expect create mode 100644 05/tcc-final-old/tests/tests2/94_generic.c create mode 100644 05/tcc-final-old/tests/tests2/94_generic.expect create mode 100644 05/tcc-final-old/tests/tests2/95_bitfields.c create mode 100644 05/tcc-final-old/tests/tests2/95_bitfields.expect create mode 100644 05/tcc-final-old/tests/tests2/95_bitfields_ms.c create mode 100644 05/tcc-final-old/tests/tests2/95_bitfields_ms.expect create mode 100644 05/tcc-final-old/tests/tests2/96_nodata_wanted.c create mode 100644 05/tcc-final-old/tests/tests2/96_nodata_wanted.expect create mode 100644 05/tcc-final-old/tests/tests2/97_utf8_string_literal.c create mode 100644 05/tcc-final-old/tests/tests2/97_utf8_string_literal.expect create mode 100644 05/tcc-final-old/tests/tests2/98_al_ax_extend.c create mode 100644 05/tcc-final-old/tests/tests2/98_al_ax_extend.expect create mode 100644 05/tcc-final-old/tests/tests2/99_fastcall.c create mode 100644 05/tcc-final-old/tests/tests2/99_fastcall.expect create mode 100644 05/tcc-final-old/tests/tests2/LICENSE create mode 100644 05/tcc-final-old/tests/tests2/Makefile create mode 100644 05/tcc-final-old/tests/vla_test.c create mode 100755 05/tcc-final-old/texi2pod.pl create mode 100755 05/tcc-final-old/win32/build-tcc.bat create mode 100644 05/tcc-final-old/win32/examples/dll.c create mode 100644 05/tcc-final-old/win32/examples/fib.c create mode 100644 05/tcc-final-old/win32/examples/hello_dll.c create mode 100644 05/tcc-final-old/win32/examples/hello_win.c create mode 100644 05/tcc-final-old/win32/include/_mingw.h create mode 100644 05/tcc-final-old/win32/include/assert.h create mode 100644 05/tcc-final-old/win32/include/conio.h create mode 100644 05/tcc-final-old/win32/include/ctype.h create mode 100644 05/tcc-final-old/win32/include/dir.h create mode 100644 05/tcc-final-old/win32/include/direct.h create mode 100644 05/tcc-final-old/win32/include/dirent.h create mode 100644 05/tcc-final-old/win32/include/dos.h create mode 100644 05/tcc-final-old/win32/include/errno.h create mode 100644 05/tcc-final-old/win32/include/excpt.h create mode 100644 05/tcc-final-old/win32/include/fcntl.h create mode 100644 05/tcc-final-old/win32/include/fenv.h create mode 100644 05/tcc-final-old/win32/include/inttypes.h create mode 100644 05/tcc-final-old/win32/include/io.h create mode 100644 05/tcc-final-old/win32/include/limits.h create mode 100644 05/tcc-final-old/win32/include/locale.h create mode 100644 05/tcc-final-old/win32/include/malloc.h create mode 100644 05/tcc-final-old/win32/include/math.h create mode 100644 05/tcc-final-old/win32/include/mem.h create mode 100644 05/tcc-final-old/win32/include/memory.h create mode 100644 05/tcc-final-old/win32/include/process.h create mode 100644 05/tcc-final-old/win32/include/sec_api/conio_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/crtdbg_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/io_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/mbstring_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/search_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/stdio_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/stdlib_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/stralign_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/string_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/sys/timeb_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/tchar_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/time_s.h create mode 100644 05/tcc-final-old/win32/include/sec_api/wchar_s.h create mode 100644 05/tcc-final-old/win32/include/setjmp.h create mode 100644 05/tcc-final-old/win32/include/share.h create mode 100644 05/tcc-final-old/win32/include/signal.h create mode 100644 05/tcc-final-old/win32/include/stdint.h create mode 100644 05/tcc-final-old/win32/include/stdio.h create mode 100644 05/tcc-final-old/win32/include/stdlib.h create mode 100644 05/tcc-final-old/win32/include/string.h create mode 100644 05/tcc-final-old/win32/include/sys/fcntl.h create mode 100644 05/tcc-final-old/win32/include/sys/file.h create mode 100644 05/tcc-final-old/win32/include/sys/locking.h create mode 100644 05/tcc-final-old/win32/include/sys/stat.h create mode 100644 05/tcc-final-old/win32/include/sys/time.h create mode 100644 05/tcc-final-old/win32/include/sys/timeb.h create mode 100644 05/tcc-final-old/win32/include/sys/types.h create mode 100644 05/tcc-final-old/win32/include/sys/unistd.h create mode 100644 05/tcc-final-old/win32/include/sys/utime.h create mode 100644 05/tcc-final-old/win32/include/tchar.h create mode 100644 05/tcc-final-old/win32/include/time.h create mode 100644 05/tcc-final-old/win32/include/vadefs.h create mode 100644 05/tcc-final-old/win32/include/values.h create mode 100644 05/tcc-final-old/win32/include/wchar.h create mode 100644 05/tcc-final-old/win32/include/wctype.h create mode 100644 05/tcc-final-old/win32/include/winapi/basetsd.h create mode 100644 05/tcc-final-old/win32/include/winapi/basetyps.h create mode 100644 05/tcc-final-old/win32/include/winapi/guiddef.h create mode 100644 05/tcc-final-old/win32/include/winapi/poppack.h create mode 100644 05/tcc-final-old/win32/include/winapi/pshpack1.h create mode 100644 05/tcc-final-old/win32/include/winapi/pshpack2.h create mode 100644 05/tcc-final-old/win32/include/winapi/pshpack4.h create mode 100644 05/tcc-final-old/win32/include/winapi/pshpack8.h create mode 100644 05/tcc-final-old/win32/include/winapi/winbase.h create mode 100644 05/tcc-final-old/win32/include/winapi/wincon.h create mode 100644 05/tcc-final-old/win32/include/winapi/windef.h create mode 100644 05/tcc-final-old/win32/include/winapi/windows.h create mode 100644 05/tcc-final-old/win32/include/winapi/winerror.h create mode 100644 05/tcc-final-old/win32/include/winapi/wingdi.h create mode 100644 05/tcc-final-old/win32/include/winapi/winnt.h create mode 100644 05/tcc-final-old/win32/include/winapi/winreg.h create mode 100644 05/tcc-final-old/win32/include/winapi/winuser.h create mode 100644 05/tcc-final-old/win32/include/winapi/winver.h create mode 100644 05/tcc-final-old/win32/lib/chkstk.S create mode 100644 05/tcc-final-old/win32/lib/crt1.c create mode 100644 05/tcc-final-old/win32/lib/crt1w.c create mode 100644 05/tcc-final-old/win32/lib/dllcrt1.c create mode 100644 05/tcc-final-old/win32/lib/dllmain.c create mode 100644 05/tcc-final-old/win32/lib/gdi32.def create mode 100644 05/tcc-final-old/win32/lib/kernel32.def create mode 100644 05/tcc-final-old/win32/lib/msvcrt.def create mode 100644 05/tcc-final-old/win32/lib/user32.def create mode 100644 05/tcc-final-old/win32/lib/wincrt1.c create mode 100644 05/tcc-final-old/win32/lib/wincrt1w.c create mode 100644 05/tcc-final-old/win32/tcc-win32.txt create mode 100644 05/tcc-final-old/x86_64-asm.h create mode 100644 05/tcc-final-old/x86_64-gen.c create mode 100644 05/tcc-final-old/x86_64-link.c create mode 100644 05/tcc-final/.gitignore create mode 100644 05/tcc-final/COPYING create mode 100644 05/tcc-final/Changelog create mode 100644 05/tcc-final/CodingStyle create mode 100644 05/tcc-final/Makefile create mode 100644 05/tcc-final/README create mode 100644 05/tcc-final/RELICENSING create mode 100644 05/tcc-final/TODO create mode 100644 05/tcc-final/USES create mode 100644 05/tcc-final/VERSION create mode 100644 05/tcc-final/arm-asm.c create mode 100644 05/tcc-final/arm-gen.c create mode 100644 05/tcc-final/arm-link.c create mode 100644 05/tcc-final/arm-tok.h create mode 100644 05/tcc-final/arm64-asm.c create mode 100644 05/tcc-final/arm64-gen.c create mode 100644 05/tcc-final/arm64-link.c create mode 100644 05/tcc-final/c67-gen.c create mode 100644 05/tcc-final/c67-link.c create mode 100644 05/tcc-final/coff.h create mode 100755 05/tcc-final/configure create mode 100644 05/tcc-final/dwarf.h create mode 100644 05/tcc-final/elf.h create mode 100755 05/tcc-final/examples/ex1.c create mode 100644 05/tcc-final/examples/ex2.c create mode 100644 05/tcc-final/examples/ex3.c create mode 100755 05/tcc-final/examples/ex4.c create mode 100644 05/tcc-final/examples/ex5.c create mode 100644 05/tcc-final/i386-asm.c create mode 100644 05/tcc-final/i386-asm.h create mode 100644 05/tcc-final/i386-gen.c create mode 100644 05/tcc-final/i386-link.c create mode 100644 05/tcc-final/i386-tok.h create mode 100644 05/tcc-final/il-gen.c create mode 100644 05/tcc-final/il-opcodes.h create mode 100644 05/tcc-final/include/float.h create mode 100644 05/tcc-final/include/stdalign.h create mode 100644 05/tcc-final/include/stdarg.h create mode 100644 05/tcc-final/include/stdatomic.h create mode 100644 05/tcc-final/include/stdbool.h create mode 100644 05/tcc-final/include/stddef.h create mode 100644 05/tcc-final/include/stdnoreturn.h create mode 100644 05/tcc-final/include/tccdefs.h create mode 100644 05/tcc-final/include/tgmath.h create mode 100644 05/tcc-final/include/varargs.h create mode 100644 05/tcc-final/lib/Makefile create mode 100644 05/tcc-final/lib/alloca-bt.S create mode 100644 05/tcc-final/lib/alloca.S create mode 100644 05/tcc-final/lib/armeabi.c create mode 100644 05/tcc-final/lib/armflush.c create mode 100644 05/tcc-final/lib/bcheck.c create mode 100644 05/tcc-final/lib/bt-dll.c create mode 100644 05/tcc-final/lib/bt-exe.c create mode 100644 05/tcc-final/lib/bt-log.c create mode 100644 05/tcc-final/lib/dsohandle.c create mode 100644 05/tcc-final/lib/fetch_and_add.S create mode 100644 05/tcc-final/lib/lib-arm64.c create mode 100644 05/tcc-final/lib/libtcc1.c create mode 100644 05/tcc-final/lib/stdatomic.c create mode 100644 05/tcc-final/lib/tcov.c create mode 100644 05/tcc-final/lib/va_list.c create mode 100644 05/tcc-final/libtcc.c create mode 100644 05/tcc-final/libtcc.h create mode 100644 05/tcc-final/riscv64-asm.c create mode 100644 05/tcc-final/riscv64-gen.c create mode 100644 05/tcc-final/riscv64-link.c create mode 100644 05/tcc-final/riscv64-tok.h create mode 100644 05/tcc-final/stab.def create mode 100644 05/tcc-final/stab.h create mode 100644 05/tcc-final/tcc-doc.texi create mode 100644 05/tcc-final/tcc.c create mode 100644 05/tcc-final/tcc.h create mode 100644 05/tcc-final/tccasm.c create mode 100644 05/tcc-final/tcccoff.c create mode 100644 05/tcc-final/tccdbg.c create mode 100644 05/tcc-final/tccelf.c create mode 100644 05/tcc-final/tccgen.c create mode 100644 05/tcc-final/tcclib.h create mode 100644 05/tcc-final/tccmacho.c create mode 100644 05/tcc-final/tccpe.c create mode 100644 05/tcc-final/tccpp.c create mode 100644 05/tcc-final/tccrun.c create mode 100644 05/tcc-final/tcctok.h create mode 100644 05/tcc-final/tcctools.c create mode 100644 05/tcc-final/tests/42test.h create mode 100644 05/tcc-final/tests/Makefile create mode 100644 05/tcc-final/tests/abitest.c create mode 100755 05/tcc-final/tests/arm-asm-testsuite.sh create mode 100644 05/tcc-final/tests/asm-c-connect-1.c create mode 100644 05/tcc-final/tests/asm-c-connect-2.c create mode 100644 05/tcc-final/tests/asmtest.S create mode 100644 05/tcc-final/tests/boundtest.c create mode 100644 05/tcc-final/tests/bug.c create mode 100644 05/tcc-final/tests/gcctestsuite.sh create mode 100644 05/tcc-final/tests/libtcc_test.c create mode 100644 05/tcc-final/tests/libtcc_test_mt.c create mode 100644 05/tcc-final/tests/pp/01.c create mode 100644 05/tcc-final/tests/pp/01.expect create mode 100644 05/tcc-final/tests/pp/02.c create mode 100644 05/tcc-final/tests/pp/02.expect create mode 100644 05/tcc-final/tests/pp/03.c create mode 100644 05/tcc-final/tests/pp/03.expect create mode 100644 05/tcc-final/tests/pp/04.c create mode 100644 05/tcc-final/tests/pp/04.expect create mode 100644 05/tcc-final/tests/pp/05.c create mode 100644 05/tcc-final/tests/pp/05.expect create mode 100644 05/tcc-final/tests/pp/06.c create mode 100644 05/tcc-final/tests/pp/06.expect create mode 100644 05/tcc-final/tests/pp/07.c create mode 100644 05/tcc-final/tests/pp/07.expect create mode 100644 05/tcc-final/tests/pp/08.c create mode 100644 05/tcc-final/tests/pp/08.expect create mode 100644 05/tcc-final/tests/pp/09.c create mode 100644 05/tcc-final/tests/pp/09.expect create mode 100644 05/tcc-final/tests/pp/10.c create mode 100644 05/tcc-final/tests/pp/10.expect create mode 100644 05/tcc-final/tests/pp/11.c create mode 100644 05/tcc-final/tests/pp/11.expect create mode 100644 05/tcc-final/tests/pp/12.S create mode 100644 05/tcc-final/tests/pp/12.expect create mode 100644 05/tcc-final/tests/pp/13.S create mode 100644 05/tcc-final/tests/pp/13.expect create mode 100644 05/tcc-final/tests/pp/14.c create mode 100644 05/tcc-final/tests/pp/14.expect create mode 100644 05/tcc-final/tests/pp/15.c create mode 100644 05/tcc-final/tests/pp/15.expect create mode 100644 05/tcc-final/tests/pp/16.c create mode 100644 05/tcc-final/tests/pp/16.expect create mode 100644 05/tcc-final/tests/pp/17.c create mode 100644 05/tcc-final/tests/pp/17.expect create mode 100644 05/tcc-final/tests/pp/18.c create mode 100644 05/tcc-final/tests/pp/18.expect create mode 100644 05/tcc-final/tests/pp/19.c create mode 100644 05/tcc-final/tests/pp/19.expect create mode 100644 05/tcc-final/tests/pp/20.c create mode 100644 05/tcc-final/tests/pp/20.expect create mode 100644 05/tcc-final/tests/pp/21.c create mode 100644 05/tcc-final/tests/pp/21.expect create mode 100644 05/tcc-final/tests/pp/Makefile create mode 100644 05/tcc-final/tests/pp/pp-counter.c create mode 100644 05/tcc-final/tests/pp/pp-counter.expect create mode 100644 05/tcc-final/tests/tcctest.c create mode 100644 05/tcc-final/tests/tcctest.h create mode 100644 05/tcc-final/tests/testfp.c create mode 100644 05/tcc-final/tests/tests2/00_assignment.c create mode 100644 05/tcc-final/tests/tests2/00_assignment.expect create mode 100644 05/tcc-final/tests/tests2/01_comment.c create mode 100644 05/tcc-final/tests/tests2/01_comment.expect create mode 100644 05/tcc-final/tests/tests2/02_printf.c create mode 100644 05/tcc-final/tests/tests2/02_printf.expect create mode 100644 05/tcc-final/tests/tests2/03_struct.c create mode 100644 05/tcc-final/tests/tests2/03_struct.expect create mode 100644 05/tcc-final/tests/tests2/04_for.c create mode 100644 05/tcc-final/tests/tests2/04_for.expect create mode 100644 05/tcc-final/tests/tests2/05_array.c create mode 100644 05/tcc-final/tests/tests2/05_array.expect create mode 100644 05/tcc-final/tests/tests2/06_case.c create mode 100644 05/tcc-final/tests/tests2/06_case.expect create mode 100644 05/tcc-final/tests/tests2/07_function.c create mode 100644 05/tcc-final/tests/tests2/07_function.expect create mode 100644 05/tcc-final/tests/tests2/08_while.c create mode 100644 05/tcc-final/tests/tests2/08_while.expect create mode 100644 05/tcc-final/tests/tests2/09_do_while.c create mode 100644 05/tcc-final/tests/tests2/09_do_while.expect create mode 100644 05/tcc-final/tests/tests2/100_c99array-decls.c create mode 100644 05/tcc-final/tests/tests2/100_c99array-decls.expect create mode 100644 05/tcc-final/tests/tests2/101_cleanup.c create mode 100644 05/tcc-final/tests/tests2/101_cleanup.expect create mode 100644 05/tcc-final/tests/tests2/102_alignas.c create mode 100644 05/tcc-final/tests/tests2/102_alignas.expect create mode 100644 05/tcc-final/tests/tests2/103_implicit_memmove.c create mode 100644 05/tcc-final/tests/tests2/103_implicit_memmove.expect create mode 100644 05/tcc-final/tests/tests2/104+_inline.c create mode 100644 05/tcc-final/tests/tests2/104_inline.c create mode 100644 05/tcc-final/tests/tests2/104_inline.expect create mode 100644 05/tcc-final/tests/tests2/105_local_extern.c create mode 100644 05/tcc-final/tests/tests2/105_local_extern.expect create mode 100644 05/tcc-final/tests/tests2/106_versym.c create mode 100644 05/tcc-final/tests/tests2/106_versym.expect create mode 100644 05/tcc-final/tests/tests2/107_stack_safe.c create mode 100644 05/tcc-final/tests/tests2/107_stack_safe.expect create mode 100644 05/tcc-final/tests/tests2/108_constructor.c create mode 100644 05/tcc-final/tests/tests2/108_constructor.expect create mode 100644 05/tcc-final/tests/tests2/109_float_struct_calling.c create mode 100644 05/tcc-final/tests/tests2/109_float_struct_calling.expect create mode 100644 05/tcc-final/tests/tests2/10_pointer.c create mode 100644 05/tcc-final/tests/tests2/10_pointer.expect create mode 100644 05/tcc-final/tests/tests2/110_average.c create mode 100644 05/tcc-final/tests/tests2/110_average.expect create mode 100644 05/tcc-final/tests/tests2/111_conversion.c create mode 100644 05/tcc-final/tests/tests2/111_conversion.expect create mode 100644 05/tcc-final/tests/tests2/112_backtrace.c create mode 100644 05/tcc-final/tests/tests2/112_backtrace.expect create mode 100644 05/tcc-final/tests/tests2/113_btdll.c create mode 100644 05/tcc-final/tests/tests2/113_btdll.expect create mode 100644 05/tcc-final/tests/tests2/114_bound_signal.c create mode 100644 05/tcc-final/tests/tests2/114_bound_signal.expect create mode 100644 05/tcc-final/tests/tests2/115_bound_setjmp.c create mode 100644 05/tcc-final/tests/tests2/115_bound_setjmp.expect create mode 100644 05/tcc-final/tests/tests2/116_bound_setjmp2.c create mode 100644 05/tcc-final/tests/tests2/116_bound_setjmp2.expect create mode 100644 05/tcc-final/tests/tests2/117_builtins.c create mode 100644 05/tcc-final/tests/tests2/117_builtins.expect create mode 100644 05/tcc-final/tests/tests2/118_switch.c create mode 100644 05/tcc-final/tests/tests2/118_switch.expect create mode 100644 05/tcc-final/tests/tests2/119_random_stuff.c create mode 100644 05/tcc-final/tests/tests2/119_random_stuff.expect create mode 100644 05/tcc-final/tests/tests2/11_precedence.c create mode 100644 05/tcc-final/tests/tests2/11_precedence.expect create mode 100644 05/tcc-final/tests/tests2/120+_alias.c create mode 100644 05/tcc-final/tests/tests2/120_alias.c create mode 100644 05/tcc-final/tests/tests2/120_alias.expect create mode 100644 05/tcc-final/tests/tests2/121_struct_return.c create mode 100644 05/tcc-final/tests/tests2/121_struct_return.expect create mode 100644 05/tcc-final/tests/tests2/122_vla_reuse.c create mode 100644 05/tcc-final/tests/tests2/122_vla_reuse.expect create mode 100644 05/tcc-final/tests/tests2/123_vla_bug.c create mode 100644 05/tcc-final/tests/tests2/123_vla_bug.expect create mode 100644 05/tcc-final/tests/tests2/124_atomic_counter.c create mode 100644 05/tcc-final/tests/tests2/124_atomic_counter.expect create mode 100644 05/tcc-final/tests/tests2/125_atomic_misc.c create mode 100644 05/tcc-final/tests/tests2/125_atomic_misc.expect create mode 100644 05/tcc-final/tests/tests2/126_bound_global.c create mode 100644 05/tcc-final/tests/tests2/126_bound_global.expect create mode 100644 05/tcc-final/tests/tests2/12_hashdefine.c create mode 100644 05/tcc-final/tests/tests2/12_hashdefine.expect create mode 100644 05/tcc-final/tests/tests2/13_integer_literals.c create mode 100644 05/tcc-final/tests/tests2/13_integer_literals.expect create mode 100644 05/tcc-final/tests/tests2/14_if.c create mode 100644 05/tcc-final/tests/tests2/14_if.expect create mode 100644 05/tcc-final/tests/tests2/15_recursion.c create mode 100644 05/tcc-final/tests/tests2/15_recursion.expect create mode 100644 05/tcc-final/tests/tests2/16_nesting.c create mode 100644 05/tcc-final/tests/tests2/16_nesting.expect create mode 100644 05/tcc-final/tests/tests2/17_enum.c create mode 100644 05/tcc-final/tests/tests2/17_enum.expect create mode 100644 05/tcc-final/tests/tests2/18_include.c create mode 100644 05/tcc-final/tests/tests2/18_include.expect create mode 100644 05/tcc-final/tests/tests2/18_include.h create mode 100644 05/tcc-final/tests/tests2/19_pointer_arithmetic.c create mode 100644 05/tcc-final/tests/tests2/19_pointer_arithmetic.expect create mode 100644 05/tcc-final/tests/tests2/20_pointer_comparison.c create mode 100644 05/tcc-final/tests/tests2/20_pointer_comparison.expect create mode 100644 05/tcc-final/tests/tests2/21_char_array.c create mode 100644 05/tcc-final/tests/tests2/21_char_array.expect create mode 100644 05/tcc-final/tests/tests2/22_floating_point.c create mode 100644 05/tcc-final/tests/tests2/22_floating_point.expect create mode 100644 05/tcc-final/tests/tests2/23_type_coercion.c create mode 100644 05/tcc-final/tests/tests2/23_type_coercion.expect create mode 100644 05/tcc-final/tests/tests2/24_math_library.c create mode 100644 05/tcc-final/tests/tests2/24_math_library.expect create mode 100644 05/tcc-final/tests/tests2/25_quicksort.c create mode 100644 05/tcc-final/tests/tests2/25_quicksort.expect create mode 100644 05/tcc-final/tests/tests2/26_character_constants.c create mode 100644 05/tcc-final/tests/tests2/26_character_constants.expect create mode 100644 05/tcc-final/tests/tests2/27_sizeof.c create mode 100644 05/tcc-final/tests/tests2/27_sizeof.expect create mode 100644 05/tcc-final/tests/tests2/28_strings.c create mode 100644 05/tcc-final/tests/tests2/28_strings.expect create mode 100644 05/tcc-final/tests/tests2/29_array_address.c create mode 100644 05/tcc-final/tests/tests2/29_array_address.expect create mode 100644 05/tcc-final/tests/tests2/30_hanoi.c create mode 100644 05/tcc-final/tests/tests2/30_hanoi.expect create mode 100644 05/tcc-final/tests/tests2/31_args.c create mode 100644 05/tcc-final/tests/tests2/31_args.expect create mode 100644 05/tcc-final/tests/tests2/32_led.c create mode 100644 05/tcc-final/tests/tests2/32_led.expect create mode 100644 05/tcc-final/tests/tests2/33_ternary_op.c create mode 100644 05/tcc-final/tests/tests2/33_ternary_op.expect create mode 100644 05/tcc-final/tests/tests2/34_array_assignment.c create mode 100644 05/tcc-final/tests/tests2/34_array_assignment.expect create mode 100644 05/tcc-final/tests/tests2/35_sizeof.c create mode 100644 05/tcc-final/tests/tests2/35_sizeof.expect create mode 100644 05/tcc-final/tests/tests2/36_array_initialisers.c create mode 100644 05/tcc-final/tests/tests2/36_array_initialisers.expect create mode 100644 05/tcc-final/tests/tests2/37_sprintf.c create mode 100644 05/tcc-final/tests/tests2/37_sprintf.expect create mode 100644 05/tcc-final/tests/tests2/38_multiple_array_index.c create mode 100644 05/tcc-final/tests/tests2/38_multiple_array_index.expect create mode 100644 05/tcc-final/tests/tests2/39_typedef.c create mode 100644 05/tcc-final/tests/tests2/39_typedef.expect create mode 100644 05/tcc-final/tests/tests2/40_stdio.c create mode 100644 05/tcc-final/tests/tests2/40_stdio.expect create mode 100644 05/tcc-final/tests/tests2/41_hashif.c create mode 100644 05/tcc-final/tests/tests2/41_hashif.expect create mode 100644 05/tcc-final/tests/tests2/42_function_pointer.c create mode 100644 05/tcc-final/tests/tests2/42_function_pointer.expect create mode 100644 05/tcc-final/tests/tests2/43_void_param.c create mode 100644 05/tcc-final/tests/tests2/43_void_param.expect create mode 100644 05/tcc-final/tests/tests2/44_scoped_declarations.c create mode 100644 05/tcc-final/tests/tests2/44_scoped_declarations.expect create mode 100644 05/tcc-final/tests/tests2/45_empty_for.c create mode 100644 05/tcc-final/tests/tests2/45_empty_for.expect create mode 100644 05/tcc-final/tests/tests2/46_grep.c create mode 100644 05/tcc-final/tests/tests2/46_grep.expect create mode 100644 05/tcc-final/tests/tests2/47_switch_return.c create mode 100644 05/tcc-final/tests/tests2/47_switch_return.expect create mode 100644 05/tcc-final/tests/tests2/48_nested_break.c create mode 100644 05/tcc-final/tests/tests2/48_nested_break.expect create mode 100644 05/tcc-final/tests/tests2/49_bracket_evaluation.c create mode 100644 05/tcc-final/tests/tests2/49_bracket_evaluation.expect create mode 100644 05/tcc-final/tests/tests2/50_logical_second_arg.c create mode 100644 05/tcc-final/tests/tests2/50_logical_second_arg.expect create mode 100644 05/tcc-final/tests/tests2/51_static.c create mode 100644 05/tcc-final/tests/tests2/51_static.expect create mode 100644 05/tcc-final/tests/tests2/52_unnamed_enum.c create mode 100644 05/tcc-final/tests/tests2/52_unnamed_enum.expect create mode 100644 05/tcc-final/tests/tests2/54_goto.c create mode 100644 05/tcc-final/tests/tests2/54_goto.expect create mode 100644 05/tcc-final/tests/tests2/55_lshift_type.c create mode 100644 05/tcc-final/tests/tests2/55_lshift_type.expect create mode 100644 05/tcc-final/tests/tests2/60_errors_and_warnings.c create mode 100644 05/tcc-final/tests/tests2/60_errors_and_warnings.expect create mode 100644 05/tcc-final/tests/tests2/61_integers.c create mode 100644 05/tcc-final/tests/tests2/61_integers.expect create mode 100644 05/tcc-final/tests/tests2/64_macro_nesting.c create mode 100644 05/tcc-final/tests/tests2/64_macro_nesting.expect create mode 100644 05/tcc-final/tests/tests2/67_macro_concat.c create mode 100644 05/tcc-final/tests/tests2/67_macro_concat.expect create mode 100644 05/tcc-final/tests/tests2/70_floating_point_literals.c create mode 100644 05/tcc-final/tests/tests2/70_floating_point_literals.expect create mode 100644 05/tcc-final/tests/tests2/71_macro_empty_arg.c create mode 100644 05/tcc-final/tests/tests2/71_macro_empty_arg.expect create mode 100644 05/tcc-final/tests/tests2/72_long_long_constant.c create mode 100644 05/tcc-final/tests/tests2/72_long_long_constant.expect create mode 100644 05/tcc-final/tests/tests2/73_arm64.c create mode 100644 05/tcc-final/tests/tests2/73_arm64.expect create mode 100644 05/tcc-final/tests/tests2/75_array_in_struct_init.c create mode 100644 05/tcc-final/tests/tests2/75_array_in_struct_init.expect create mode 100644 05/tcc-final/tests/tests2/76_dollars_in_identifiers.c create mode 100644 05/tcc-final/tests/tests2/76_dollars_in_identifiers.expect create mode 100644 05/tcc-final/tests/tests2/77_push_pop_macro.c create mode 100644 05/tcc-final/tests/tests2/77_push_pop_macro.expect create mode 100644 05/tcc-final/tests/tests2/78_vla_label.c create mode 100644 05/tcc-final/tests/tests2/78_vla_label.expect create mode 100644 05/tcc-final/tests/tests2/79_vla_continue.c create mode 100644 05/tcc-final/tests/tests2/79_vla_continue.expect create mode 100644 05/tcc-final/tests/tests2/80_flexarray.c create mode 100644 05/tcc-final/tests/tests2/80_flexarray.expect create mode 100644 05/tcc-final/tests/tests2/81_types.c create mode 100644 05/tcc-final/tests/tests2/81_types.expect create mode 100644 05/tcc-final/tests/tests2/82_attribs_position.c create mode 100644 05/tcc-final/tests/tests2/82_attribs_position.expect create mode 100644 05/tcc-final/tests/tests2/83_utf8_in_identifiers.c create mode 100644 05/tcc-final/tests/tests2/83_utf8_in_identifiers.expect create mode 100644 05/tcc-final/tests/tests2/84_hex-float.c create mode 100644 05/tcc-final/tests/tests2/84_hex-float.expect create mode 100644 05/tcc-final/tests/tests2/85_asm-outside-function.c create mode 100644 05/tcc-final/tests/tests2/85_asm-outside-function.expect create mode 100644 05/tcc-final/tests/tests2/86_memory-model.c create mode 100644 05/tcc-final/tests/tests2/86_memory-model.expect create mode 100644 05/tcc-final/tests/tests2/87_dead_code.c create mode 100644 05/tcc-final/tests/tests2/87_dead_code.expect create mode 100644 05/tcc-final/tests/tests2/88_codeopt.c create mode 100644 05/tcc-final/tests/tests2/88_codeopt.expect create mode 100644 05/tcc-final/tests/tests2/89_nocode_wanted.c create mode 100644 05/tcc-final/tests/tests2/89_nocode_wanted.expect create mode 100644 05/tcc-final/tests/tests2/90_struct-init.c create mode 100644 05/tcc-final/tests/tests2/90_struct-init.expect create mode 100644 05/tcc-final/tests/tests2/91_ptr_longlong_arith32.c create mode 100644 05/tcc-final/tests/tests2/91_ptr_longlong_arith32.expect create mode 100644 05/tcc-final/tests/tests2/92_enum_bitfield.c create mode 100644 05/tcc-final/tests/tests2/92_enum_bitfield.expect create mode 100644 05/tcc-final/tests/tests2/93_integer_promotion.c create mode 100644 05/tcc-final/tests/tests2/93_integer_promotion.expect create mode 100644 05/tcc-final/tests/tests2/94_generic.c create mode 100644 05/tcc-final/tests/tests2/94_generic.expect create mode 100644 05/tcc-final/tests/tests2/95_bitfields.c create mode 100644 05/tcc-final/tests/tests2/95_bitfields.expect create mode 100644 05/tcc-final/tests/tests2/95_bitfields_ms.c create mode 100644 05/tcc-final/tests/tests2/95_bitfields_ms.expect create mode 100644 05/tcc-final/tests/tests2/96_nodata_wanted.c create mode 100644 05/tcc-final/tests/tests2/96_nodata_wanted.expect create mode 100644 05/tcc-final/tests/tests2/97_utf8_string_literal.c create mode 100644 05/tcc-final/tests/tests2/97_utf8_string_literal.expect create mode 100644 05/tcc-final/tests/tests2/98_al_ax_extend.c create mode 100644 05/tcc-final/tests/tests2/98_al_ax_extend.expect create mode 100644 05/tcc-final/tests/tests2/99_fastcall.c create mode 100644 05/tcc-final/tests/tests2/99_fastcall.expect create mode 100644 05/tcc-final/tests/tests2/LICENSE create mode 100644 05/tcc-final/tests/tests2/Makefile create mode 100644 05/tcc-final/tests/vla_test.c create mode 100755 05/tcc-final/texi2pod.pl create mode 100644 05/tcc-final/win32/build-tcc.bat create mode 100644 05/tcc-final/win32/examples/dll.c create mode 100644 05/tcc-final/win32/examples/fib.c create mode 100644 05/tcc-final/win32/examples/hello_dll.c create mode 100644 05/tcc-final/win32/examples/hello_win.c create mode 100644 05/tcc-final/win32/include/_mingw.h create mode 100644 05/tcc-final/win32/include/assert.h create mode 100644 05/tcc-final/win32/include/conio.h create mode 100644 05/tcc-final/win32/include/ctype.h create mode 100644 05/tcc-final/win32/include/dir.h create mode 100644 05/tcc-final/win32/include/direct.h create mode 100644 05/tcc-final/win32/include/dirent.h create mode 100644 05/tcc-final/win32/include/dos.h create mode 100644 05/tcc-final/win32/include/errno.h create mode 100644 05/tcc-final/win32/include/excpt.h create mode 100644 05/tcc-final/win32/include/fcntl.h create mode 100644 05/tcc-final/win32/include/fenv.h create mode 100644 05/tcc-final/win32/include/inttypes.h create mode 100644 05/tcc-final/win32/include/io.h create mode 100644 05/tcc-final/win32/include/iso646.h create mode 100644 05/tcc-final/win32/include/limits.h create mode 100644 05/tcc-final/win32/include/locale.h create mode 100644 05/tcc-final/win32/include/malloc.h create mode 100644 05/tcc-final/win32/include/math.h create mode 100644 05/tcc-final/win32/include/mem.h create mode 100644 05/tcc-final/win32/include/memory.h create mode 100644 05/tcc-final/win32/include/process.h create mode 100644 05/tcc-final/win32/include/sec_api/conio_s.h create mode 100644 05/tcc-final/win32/include/sec_api/crtdbg_s.h create mode 100644 05/tcc-final/win32/include/sec_api/io_s.h create mode 100644 05/tcc-final/win32/include/sec_api/mbstring_s.h create mode 100644 05/tcc-final/win32/include/sec_api/search_s.h create mode 100644 05/tcc-final/win32/include/sec_api/stdio_s.h create mode 100644 05/tcc-final/win32/include/sec_api/stdlib_s.h create mode 100644 05/tcc-final/win32/include/sec_api/stralign_s.h create mode 100644 05/tcc-final/win32/include/sec_api/string_s.h create mode 100644 05/tcc-final/win32/include/sec_api/sys/timeb_s.h create mode 100644 05/tcc-final/win32/include/sec_api/tchar_s.h create mode 100644 05/tcc-final/win32/include/sec_api/time_s.h create mode 100644 05/tcc-final/win32/include/sec_api/wchar_s.h create mode 100644 05/tcc-final/win32/include/setjmp.h create mode 100644 05/tcc-final/win32/include/share.h create mode 100644 05/tcc-final/win32/include/signal.h create mode 100644 05/tcc-final/win32/include/stdint.h create mode 100644 05/tcc-final/win32/include/stdio.h create mode 100644 05/tcc-final/win32/include/stdlib.h create mode 100644 05/tcc-final/win32/include/string.h create mode 100644 05/tcc-final/win32/include/sys/fcntl.h create mode 100644 05/tcc-final/win32/include/sys/file.h create mode 100644 05/tcc-final/win32/include/sys/locking.h create mode 100644 05/tcc-final/win32/include/sys/stat.h create mode 100644 05/tcc-final/win32/include/sys/time.h create mode 100644 05/tcc-final/win32/include/sys/timeb.h create mode 100644 05/tcc-final/win32/include/sys/types.h create mode 100644 05/tcc-final/win32/include/sys/unistd.h create mode 100644 05/tcc-final/win32/include/sys/utime.h create mode 100644 05/tcc-final/win32/include/tchar.h create mode 100644 05/tcc-final/win32/include/time.h create mode 100755 05/tcc-final/win32/include/uchar.h create mode 100644 05/tcc-final/win32/include/vadefs.h create mode 100644 05/tcc-final/win32/include/values.h create mode 100644 05/tcc-final/win32/include/wchar.h create mode 100644 05/tcc-final/win32/include/wctype.h create mode 100644 05/tcc-final/win32/include/winapi/basetsd.h create mode 100644 05/tcc-final/win32/include/winapi/basetyps.h create mode 100644 05/tcc-final/win32/include/winapi/guiddef.h create mode 100644 05/tcc-final/win32/include/winapi/poppack.h create mode 100644 05/tcc-final/win32/include/winapi/pshpack1.h create mode 100644 05/tcc-final/win32/include/winapi/pshpack2.h create mode 100644 05/tcc-final/win32/include/winapi/pshpack4.h create mode 100644 05/tcc-final/win32/include/winapi/pshpack8.h create mode 100644 05/tcc-final/win32/include/winapi/qos.h create mode 100644 05/tcc-final/win32/include/winapi/winbase.h create mode 100644 05/tcc-final/win32/include/winapi/wincon.h create mode 100644 05/tcc-final/win32/include/winapi/windef.h create mode 100644 05/tcc-final/win32/include/winapi/windows.h create mode 100644 05/tcc-final/win32/include/winapi/winerror.h create mode 100644 05/tcc-final/win32/include/winapi/wingdi.h create mode 100644 05/tcc-final/win32/include/winapi/winnls.h create mode 100644 05/tcc-final/win32/include/winapi/winnt.h create mode 100644 05/tcc-final/win32/include/winapi/winreg.h create mode 100644 05/tcc-final/win32/include/winapi/winsock2.h create mode 100644 05/tcc-final/win32/include/winapi/winuser.h create mode 100644 05/tcc-final/win32/include/winapi/winver.h create mode 100644 05/tcc-final/win32/include/winapi/ws2ipdef.h create mode 100644 05/tcc-final/win32/include/winapi/ws2tcpip.h create mode 100644 05/tcc-final/win32/lib/chkstk.S create mode 100644 05/tcc-final/win32/lib/crt1.c create mode 100644 05/tcc-final/win32/lib/crt1w.c create mode 100644 05/tcc-final/win32/lib/crtinit.c create mode 100644 05/tcc-final/win32/lib/dllcrt1.c create mode 100644 05/tcc-final/win32/lib/dllmain.c create mode 100644 05/tcc-final/win32/lib/gdi32.def create mode 100644 05/tcc-final/win32/lib/kernel32.def create mode 100644 05/tcc-final/win32/lib/msvcrt.def create mode 100644 05/tcc-final/win32/lib/user32.def create mode 100644 05/tcc-final/win32/lib/wincrt1.c create mode 100644 05/tcc-final/win32/lib/wincrt1w.c create mode 100644 05/tcc-final/win32/lib/ws2_32.def create mode 100644 05/tcc-final/win32/tcc-win32.txt create mode 100644 05/tcc-final/x86_64-asm.h create mode 100644 05/tcc-final/x86_64-gen.c create mode 100644 05/tcc-final/x86_64-link.c create mode 160000 06 diff --git a/.helix/ignore b/.helix/ignore index 369e75e..ed43ad4 100644 --- a/.helix/ignore +++ b/.helix/ignore @@ -3,11 +3,8 @@ 02/* 03/* 04/* -05/* -tofix 04a -06/deps/* -06/zig -06/exercises -*.dot +# 05/musl* +05/tcc-0* +05/musl-0* diff --git a/05/Makefile b/05/Makefile index f64768d..0b31d5e 100644 --- a/05/Makefile +++ b/05/Makefile @@ -22,9 +22,9 @@ $(TCCDIR)/lib/libtcc1.a: $(TCC0) $(TCCDIR)/lib/*.[cS] $(TCC0) -c $(TCCDIR)/lib/libtcc1.c -o $(TCCDIR)/lib/libtcc1.o $(TCC0) -ar $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/lib/*.o musl: tcc-files - mkdir -p musl-boostrap/include - mkdir -p musl-boostrap/bin - mkdir -p musl-boostrap/lib + mkdir -p musl-bootstrap/include + mkdir -p musl-bootstrap/bin + mkdir -p musl-bootstrap/lib $(MAKE) -j8 -C musl-0.6.0 $(MAKE) -C musl-0.6.0 install tcc-files: $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/include/*.h @@ -32,16 +32,17 @@ tcc-files: $(TCCDIR)/lib/libtcc1.a $(TCCDIR)/include/*.h cp -r $(TCCDIR)/include/*.h $(TCCINST)/include/ cp -r $(TCCDIR)/lib/libtcc1.a $(TCCINST)/ $(TCC): $(TCC0) musl - cd $(TCCDIR) && ./tcc0 -nostdinc -nostdlib -B ../tcc-boostrap -I ../musl-bootstrap/include tcc.c ../musl-bootstrap/lib/*.[oa] -o tcc + cd $(TCCDIR) && ./tcc0 -nostdinc -nostdlib -B ../tcc-bootstrap -I ../musl-bootstrap/include tcc.c ../musl-bootstrap/lib/*.[oa] -o tcc tcc: $(TCC) musl-final: tcc - mkdir -p musl-boostrap-final/include - mkdir -p musl-boostrap-final/bin - mkdir -p musl-boostrap-final/lib - $(MAKE) -j8 -C musl-final - $(MAKE) -C musl-final install + mkdir -p musl-bootstrap-final/include + mkdir -p musl-bootstrap-final/bin + mkdir -p musl-bootstrap-final/lib + $(MAKE) -C musl-final +tcc-final: tcc musl-final + cd tcc-final && ../$(TCCDIR)/tcc -Wall -g -static -nostdinc -nostdlib -B ../tcc-bootstrap -I ../musl-final/include tcc.c ../musl-final/lib/*.[oa] -o tcc testapp: tcc musl-final - $(TCC) -Wall -g -static -nostdinc -nostdlib -I musl-final/include test.c musl-final/lib/*.[oa] -MD -o test + ./tcc-final/tcc -Wall -g -static -nostdinc -nostdlib -I ./tcc-final/include -I ./musl-final/include test.c ./musl-final/lib/*.[oa] -o test $(TCC)2: $(TCC)1 cd $(TCCDIR) && ./tcc1 tcc.c -o tcc2 diff --git a/05/tcc-final-old/.gitignore b/05/tcc-final-old/.gitignore new file mode 100644 index 0000000..024af76 --- /dev/null +++ b/05/tcc-final-old/.gitignore @@ -0,0 +1,55 @@ +*~ +\#* +.#* +*.o +*.a +*.exe +*.dll +*.obj +*.pdb +*.lib +*.exp +*.log +*.bz2 +*.zip +.gdb_history +a.out +tcc_g +tcc +*-tcc +libtcc*.def + +config.texi +conftest* +tags +TAGS +tcc.1 +tcc.pod +tcc-doc.html +tcc-doc.info + +win32/doc +win32/libtcc +win32/lib/32 +win32/lib/64 +win32/include/float.h +win32/include/stdarg.h +win32/include/stdbool.h +win32/include/stddef.h +win32/include/varargs.h +win32/include/tcclib.h + +tests/tcctest[1234] +tests/tcctest.gcc +tests/*.out* +tests/*.ref +tests/*.txt +tests/*.gcc +tests/*-cc* +tests/*-tcc* +tests/libtcc_test +tests/asm-c-connect +tests/asm-c-connect-sep +tests/vla_test +tests/hello +tests/tests2/fred.txt diff --git a/05/tcc-final-old/COPYING b/05/tcc-final-old/COPYING new file mode 100644 index 0000000..223ede7 --- /dev/null +++ b/05/tcc-final-old/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/05/tcc-final-old/Changelog b/05/tcc-final-old/Changelog new file mode 100644 index 0000000..17f5bde --- /dev/null +++ b/05/tcc-final-old/Changelog @@ -0,0 +1,439 @@ +Version 0.9.27: + +User interface: +- -x[c|a|n] filetype option (Sergey Korshunoff) +- -P[1], -dD, -dM preprocessor options (Sergey Korshunoff) +- -Wl,-(no-)whole-archive linker option (Reuben Thomas) +- -mms-bitfields option (David Mertens) +- -include option (Michael Matz) +- -mno-sse on x86-64 disables use of SSE instructions +- @listfile support (Vlad Vissoultchev) +- tcc -ar/-impdef - formerly tiny_xxx tools integrated (grischka) +- CPATH, C_INCLUDE_PATH and LIBRARY_PATH environment variables support + (Andrew Aladjev, Urs Janssen) + +Platforms: +- new AARCH64 (arm64) target (Edmund Grimley Evans) +- vastly improved support for ARM hard float calling convention + (Thomas Preud'homme, Daniel Glöckner) +- provide a runtime library for ARM (Thomas Preud'homme) +- many x86_64 ABI fixes incl. XMM register passing and tests (James Lyon) +- ABI tests with native compiler using libtcc (James Lyon) +- UNICODE startup code supports wmain and wWinMain (YX Hao) +- shared libraries for x86_64 (Michael Matz) +- Bootstrap native Windows 32/64 compiler using Cygwin+gcc (Christian Jullien) + +Features: +- VLA (variable length array) improved (James Lyon, Pip Cet) +- import functions by ordinal in .def files on windows (YX Hao) +- x86/x86_64 assembler much improved (Michael Matz) +- simple dead code suppression (Edmund Grimley Evans, Michael Matz, grischka) +- implement round/fmin/fmax etc. math on windows (Avi Halachmi) +- #pragma once support (Sergey Korshunoff, Vlad Vissoultchev, ...) +- switch/case code improved (Zdenek Pavlas) +- ~15% faster by TinyAlloc fast memory allocator (Vlad Vissoultchev) +- standard conforming (and GCC compatible) struct initialization + (Michael Matz) +- bit-field layout made compatible with GCC (Michael Matz) +- UTF8 in string literals supported (Zdenek Pavlas) +_ _Generic(...) supported (Matthias Gatto) + +Licensing: +- TinyCC partly relicensed to MIT license (See RELICENSING file). + +version 0.9.26: + +User interface: +- -MD/-MF (automatically generate dependencies for make) +- -pthread option (same as -D_REENTRANT -lpthread) (Henry Kroll III) +- -m32/-m64 to re-exec cross compiler (Henry Kroll III) +- -Wl, Mimic all GNU -option forms supported by ld (Kirill Smelkov) +- new LIBTCCAPI tcc_set_options() (grischka) + +Platforms: +- Many improvements for x86-64 target (Shinichiro Hamaji, Michael Matz, grischka) +- x86-64 assembler (Frederic Feret) +- Many improvements for ARM target (Daniel Glöckner, Thomas Preud'homme) +- Support WinCE PE ARM (Timo VJ Lahde) +- Support ARM hardfloat calling convention (Thomas Preud'homme) +- Support SELinux (Security-Enhanced Linux) (Henry Kroll III) +- Support Debian GNU/kFreeBSD kernels (Pierre Chifflier) +- Support GNU/Hurd kernels (Thomas Preud'homme) +- Support OSX (tcc -run only) (Milutin Jovanovic) +- Support multiarch configuration (Thomas Preud'homme) +- Support out-of-tree build (Akim Demaille) + +Features: +- C99 variable length arrays (Thomas Preud'homme & Joe Soroka) +- Asm labels for variables and functions (Thomas Preud'homme) +- STT_GNU_IFUNC (Indirect functions as externals) (Thomas Preud'homme) +- More tests (tests2) (Milutin Jovanovic) + +version 0.9.25: + +- first support for x86-64 target (Shinichiro Hamaji) +- support µClibc +- split tcc.c into tcc.h libtcc.c tccpp.c tccgen.c tcc.c +- improved preprocess output with linenumbers and spaces preserved +- tcc_relocate now copies code into user buffer +- fix bitfields with non-int types and in unions +- improve ARM cross-compiling (Daniel Glöckner) +- link stabstr sections from multiple objects +- better (still limited) support for multiple TCCStates + +version 0.9.24: + +- added verbosity levels -v, -vv, -vvv +- Accept standard input as an inputstream (Hanzac Chen) +- Support c89 compilers other than gcc (Hanzac Chen) +- -soname linker option (Marc Andre Tanner) +- Just warn about unknown directives, ignore quotes in #error/#warning +- Define __STDC_VERSION__=199901L (477) +- Switch to newer tccpe.c (includes support for resources) +- Handle backslashes within #include/#error/#warning +- Import changesets (part 4) 428,457,460,467: defines for openbsd etc. +- Use _WIN32 for a windows hosted tcc and define it for the PE target, + otherwise define __unix / __linux (Detlef Riekenberg) +- Import changesets (part 3) 409,410: ARM EABI by Daniel Glöckner +- Some in-between fixes: + TCC -E no longer hangs with macro calls involving newlines. + (next_nomacro1 now advances the read-pointer with TOK_LINEFEED) + Global cast (int g_i = 1LL;) no longer crashes tcc. + (nocode_wanted is initially 1, and only 0 for gen_function) + On win32 now tcc.exe finds 'include' & 'lib' even if itself is in 'bin'. + (new function w32_tcc_lib_path removes 'bin' if detected) + Added quick build batch file for mingw (win32/build-tcc.bat) + Last added case label optimization (455) produced wrong code. Reverted. + +- Import more changesets from Rob Landley's fork (part 2): + 487: Handle long long constants in gen_opic() (Rob Landley) + 484: Handle parentheses within __attribute__((...)) (Rob Landley) + 480: Remove a goto in decl_initializer_alloc (Rob Landley) + 475: Fix dereferences in inline assembly output (Joshua Phillips) + 474: Cast ptrs to ints of different sizes correctly (Joshua Phillips) + 473: Fix size of structs with empty array member (Joshua Phillips) + 470: No warning for && and || with mixed pointers/integers (Rob Landley) + 469: Fix symbol visibility problems in the linker (Vincent Pit) + 468: Allow && and || involving pointer arguments (Rob Landley) + 455: Optimize case labels with no code in between (Zdenek Pavlas) + 450: Implement alloca for x86 (grischka) + 415: Parse unicode escape sequences (Axel Liljencrantz) + 407: Add a simple va_copy() in stdarg.h (Hasso Tepper) + 400: Allow typedef names as symbols (Dave Dodge) + +- Import some changesets from Rob Landley's fork (part 1): + 462: Use LGPL with bcheck.c and il-gen.c + 458: Fix global compound literals (in unary: case '&':) (Andrew Johnson) + 456: Use return code from tcc_output_file in main() (Michael Somos) + 442: Fix indirections with function pointers (***fn)() (grischka) + 441: Fix LL left shift in libtcc1.c:__shldi3 (grischka) + 440: Pass structures and function ptrs through ?: (grischka) + 439: Keep rvalue in bit assignment (bit2 = bit1 = x) (grischka) + 438: Degrade nonportable pointer assignment to warning (grischka) + 437: Call 'saveregs()' before jumping with logical and/or/not (grischka) + 435: Put local static variables into global memory (grischka) + 432/434: Cast double and ptr to bool (grischka) + 420: Zero pad x87 tenbyte long doubles (Felix Nawothnig) + 417: Make 'sizeof' unsigned (Rob Landley) + 397: Fix save_reg for longlongs (Daniel Glöckner) + 396: Fix "invalid relocation entry" problem on ubuntu - (Bernhard Fischer) + +- ignore AS_NEEDED ld command +- mark executable sections as executable when running in memory +- added support for win32 wchar_t (Filip Navara) +- segment override prefix support (Filip Navara) +- normalized slashes in paths (Filip Navara) +- windows style fastcall (Filip Navara) +- support for empty input register section in asm (Filip Navara) +- anonymous union/struct support (Filip Navara) +- fixed parsing of function parameters +- workaround for function pointers in conditional expressions (Dave Dodge) +- initial '-E' option support to use the C preprocessor alone +- discard type qualifiers when comparing function parameters (Dave Dodge) +- Bug fix: A long long value used as a test expression ignores the + upper 32 bits at runtime (Dave Dodge) +- fixed multiple concatenation of PPNUM tokens (initial patch by Dave Dodge) +- fixed multiple typedef specifiers handling +- fixed sign extension in some type conversions (Dave Dodge) + +version 0.9.23: + +- initial PE executable format for windows version (grischka) +- '#pragma pack' support (grischka) +- '#include_next' support (Bernhard Fischer) +- ignore '-pipe' option +- added -f[no-]leading-underscore +- preprocessor function macro parsing fix (grischka) + +version 0.9.22: + +- simple memory optimisations: kernel compilation is 30% faster +- linker symbol definitions fixes +- gcc 3.4 fixes +- fixed value stack full error +- 'packed' attribute support for variables and structure fields +- ignore 'const' and 'volatile' in function prototypes +- allow '_Bool' in bit fields + +version 0.9.21: + +- ARM target support (Daniel Glöckner) +- added '-funsigned-char, '-fsigned-char' and + '-Wimplicit-function-declaration' +- fixed assignment of const struct in struct +- line comment fix (reported by Bertram Felgenhauer) +- initial TMS320C67xx target support (TK) +- win32 configure +- regparm() attribute +- many built-in assembler fixes +- added '.org', '.fill' and '.previous' assembler directives +- '-fno-common' option +- '-Ttext' linker option +- section alignment fixes +- bit fields fixes +- do not generate code for unused inline functions +- '-oformat' linker option. +- added 'binary' output format. + +version 0.9.20: + +- added '-w' option +- added '.gnu.linkonce' ELF sections support +- fixed libc linking when running in memory (avoid 'stat' function + errors). +- extended '-run' option to be able to give several arguments to a C + script. + +version 0.9.19: + +- "alacarte" linking (Dave Long) +- simpler function call +- more strict type checks +- added 'const' and 'volatile' support and associated warnings +- added -Werror, -Wunsupported, -Wwrite-strings, -Wall. +- added __builtin_types_compatible_p() and __builtin_constant_p() +- chars support in assembler (Dave Long) +- .string, .globl, .section, .text, .data and .bss asm directive + support (Dave Long) +- man page generated from tcc-doc.texi +- fixed macro argument substitution +- fixed zero argument macro parsing +- changed license to LGPL +- added -rdynamic option support + +version 0.9.18: + +- header fix (time.h) +- fixed inline asm without operand case +- fixed 'default:' or 'case x:' with '}' after (incorrect C construct accepted + by gcc) +- added 'A' inline asm constraint. + +version 0.9.17: + +- PLT generation fix +- tcc doc fixes (Peter Lund) +- struct parse fix (signaled by Pedro A. Aranda Gutierrez) +- better _Bool lvalue support (signaled by Alex Measday) +- function parameters must be converted to pointers (signaled by Neil Brown) +- sanitized string and character constant parsing +- fixed comment parse (signaled by Damian M Gryski) +- fixed macro function bug (signaled by Philippe Ribet) +- added configure (initial patch by Mitchell N Charity) +- added '-run' and '-v' options (initial patch by vlindos) +- added real date report in __DATE__ and __TIME__ macros + +version 0.9.16: + +- added assembler language support +- added GCC inline asm() support +- fixed multiple variable definitions : uninitialized variables are + created as COMMON symbols. +- optimized macro processing +- added GCC statement expressions support +- added GCC local labels support +- fixed array declaration in old style function parameters +- support casts in static structure initializations +- added various __xxx[__] keywords for GCC compatibility +- ignore __extension__ GCC in an expression or in a type (still not perfect) +- added '? :' GCC extension support + +version 0.9.15: + +- compilation fixes for glibc 2.2, gcc 2.95.3 and gcc 3.2. +- FreeBSD compile fixes. Makefile patches still missing (Carl Drougge). +- fixed file type guessing if '.' is in the path. +- fixed tcc_compile_string() +- add a dummy page in ELF files to fix RX/RW accesses (pageexec at + freemail dot hu). + +version 0.9.14: + +- added #warning. error message if invalid preprocessing directive. +- added CType structure to ease typing (faster parse). +- suppressed secondary hash tables (faster parse). +- rewrote parser by optimizing common cases (faster parse). +- fixed signed long long comparisons. +- fixed 'int a(), b();' declaration case. +- fixed structure init without '{}'. +- correct alignment support in structures. +- empty structures support. +- gcc testsuite now supported. +- output only warning if implicit integer/pointer conversions. +- added static bitfield init. + +version 0.9.13: + +- correct preprocessing token pasting (## operator) in all cases (added + preprocessing number token). +- fixed long long register spill. +- fixed signed long long '>>'. +- removed memory leaks. +- better error handling : processing can continue on link errors. A + custom callback can be added to display error messages. Most + errors do not call exit() now. +- ignore -O, -W, -m and -f options +- added old style function declarations +- added GCC __alignof__ support. +- added GCC typeof support. +- added GCC computed gotos support. +- added stack backtrace in runtime error message. Improved runtime + error position display. + +version 0.9.12: + +- more fixes for || and && handling. +- improved '? :' type handling. +- fixed bound checking generation with structures +- force '#endif' to be in same file as matching '#if' +- #include file optimization with '#ifndef #endif' construct detection +- macro handling optimization +- added tcc_relocate() and tcc_get_symbol() in libtcc. + +version 0.9.11: + +- stdarg.h fix for double type (thanks to Philippe Ribet). +- correct white space characters and added MSDOS newline support. +- fixed invalid implicit function call type declaration. +- special macros such as __LINE__ are defined if tested with defined(). +- fixed '!' operator with relocated address. +- added symbol + offset relocation (fixes some static variable initializers) +- '-l' option can be specified anywhere. '-c' option yields default + output name. added '-r' option for relocatable output. +- fixed '\nnn' octal parsing. +- fixed local extern variables declarations. + +version 0.9.10: + +- fixed lvalue type when saved in local stack. +- fixed '#include' syntax when using macros. +- fixed '#line' bug. +- removed size limit on strings. Unified string constants handling + with variable declarations. +- added correct support for '\xX' in wchar_t strings. +- added support for bound checking in generated executables +- fixed -I include order. +- fixed incorrect function displayed in runtime error. + +version 0.9.9: + +- fixed preprocessor expression parsing for #if/#elif. +- relocated debug info (.stab section). +- relocated bounds info (.bounds section). +- fixed cast to char of char constants ('\377' is -1 instead of 255) +- fixed implicit cast for unary plus. +- strings and '__func__' have now 'char[]' type instead of 'char *' + (fixes sizeof() return value). +- added __start_xxx and __stop_xxx symbols in linker. +- better DLL creation support (option -shared begins to work). +- ELF sections and hash tables are resized dynamically. +- executables and DLLs are stripped by default. + +version 0.9.8: + +- First version of full ELF linking support (generate objects, static + executable, dynamic executable, dynamic libraries). Dynamic library + support is not finished (need PIC support in compiler and some + patches in symbol exporting). +- First version of ELF loader for object (.o) and archive (.a) files. +- Support of simple GNU ld scripts (GROUP and FILE commands) +- Separated runtime library and bound check code from TCC (smaller + compiler core). +- fixed register reload in float compare. +- fixed implicit char/short to int casting. +- allow array type for address of ('&') operator. +- fixed unused || or && result. +- added GCC style variadic macro support. +- optimized bound checking code for array access. +- tcc includes are now in $(prefix)/lib/tcc/include. +- more command line options - more consistent handling of multiple + input files. +- added tcc man page (thanks to Cyril Bouthors). +- uClibc Makefile update +- converted documentation to texinfo format. +- added developper's guide in documentation. + +version 0.9.7: + +- added library API for easy dynamic compilation (see libtcc.h - first + draft). +- fixed long long register spill bug. +- fixed '? :' register spill bug. + +version 0.9.6: + +- added floating point constant propagation (fixes negative floating + point constants bug). + +version 0.9.5: + + - uClibc patches (submitted by Alfonso Martone). + - error reporting fix + - added CONFIG_TCC_BCHECK to get smaller code if needed. + +version 0.9.4: + + - windows port (currently cannot use -g, -b and dll functions). + - faster and simpler I/O handling. + - '-D' option works in all cases. + - preprocessor fixes (#elif and empty macro args) + - floating point fixes + - first code for CIL generation (does not work yet) + +version 0.9.3: + + - better and smaller code generator. + - full ISOC99 64 bit 'long long' support. + - full 32 bit 'float', 64 bit 'double' and 96 bit 'long double' support. + - added '-U' option. + - added assembly sections support. + - even faster startup time by mmaping sections instead of mallocing them. + - added GNUC __attribute__ keyword support (currently supports + 'section' and 'aligned' attributes). + - added ELF file output (only usable for debugging now) + - added debug symbol generation (STAB format). + - added integrated runtime error analysis ('-g' option: print clear + run time error messages instead of "Segmentation fault"). + - added first version of tiny memory and bound checker ('-b' option). + +version 0.9.2: + + - even faster parsing. + - various syntax parsing fixes. + - fixed external relocation handling for variables or functions pointers. + - better function pointers type handling. + - can compile multiple files (-i option). + - ANSI C bit fields are supported. + - beginning of float/double/long double support. + - beginning of long long support. + +version 0.9.1: + + - full ISOC99 initializers handling. + - compound literals. + - structures handle in assignments and as function param or return value. + - wide chars and strings. + - macro bug fix + +version 0.9: + - initial version. diff --git a/05/tcc-final-old/CodingStyle b/05/tcc-final-old/CodingStyle new file mode 100644 index 0000000..93d1324 --- /dev/null +++ b/05/tcc-final-old/CodingStyle @@ -0,0 +1,71 @@ + +In general, use the same coding style as the surrounding code. + +However, do not make any unnecessary changes as that complicates +the VCS (git) history and makes it harder to merge patches. So +do not modify code just to make it conform to a coding style. + + Indentation + +Turn on a "fill tabs with spaces" option in your editor. + +Remove tabs and trailing spaces from any lines that are modified. + +Note that some files are indented with 2 spaces (when they +have large indentation) while most are indented with 4 spaces. + + Language + +TCC is mostly implemented in C90. Do not use any non-C90 features +that are not already in use. + +Non-C90 features currently in use, as revealed by +./configure --extra-cflags="-std=c90 -Wpedantic": + +- long long (including "LL" constants) +- inline +- very long string constants +- assignment between function pointer and 'void *' +- "//" comments +- empty macro arguments (DEF_ASMTEST in i386-tok.h) +- unnamed struct and union fields (in struct Sym), a C11 feature + + Testing + +A simple "make test" is sufficient for some simple changes. However, +before committing a change consider performing some of the following +additional tests: + +- Build and run "make test" on several architectures. + +- Build with ./configure --enable-cross. + +- If the generation of relocations has been changed, try compiling + with TCC and linking with GCC/Clang. If the linker has been + modified, try compiling with GCC/Clang and linking with TCC. + +- Test with ASan/UBSan to detect memory corruption and undefined behaviour: + +make clean +./configure +make +make test +cp libtcc.a libtcc.a.hide + +make clean +./configure --extra-cflags="-fsanitize=address,undefined -g" +make +cp libtcc.a.hide libtcc.a +make test + +- Test with Valgrind to detect some uses of uninitialised values: + +make clean +./configure +make +# On Intel, because Valgrind does floating-point arithmetic differently: +( cd tests && gcc -I.. tcctest.c && valgrind -q ./a.out > test.ref ) +make test TCC="valgrind -q --leak-check=full `pwd`/tcc -B`pwd` -I`pwd`" + + (Because of how VLAs are implemented, invalid reads are expected + with 79_vla_continue.) diff --git a/05/tcc-final-old/Makefile b/05/tcc-final-old/Makefile new file mode 100644 index 0000000..3ae466f --- /dev/null +++ b/05/tcc-final-old/Makefile @@ -0,0 +1,403 @@ +# -------------------------------------------------------------------------- +# +# Tiny C Compiler Makefile +# + +ifndef TOP + TOP = . + INCLUDED = no +endif + +include $(TOP)/config.mak + +ifeq (-$(CC)-$(GCC_MAJOR)-$(findstring $(GCC_MINOR),56789)-,-gcc-4--) + CFLAGS += -D_FORTIFY_SOURCE=0 +endif + +LIBTCC = libtcc.a +LIBTCC1 = libtcc1.a +LINK_LIBTCC = +LIBS = +CFLAGS += -I$(TOP) +CFLAGS += $(CPPFLAGS) +VPATH = $(TOPSRC) + +ifdef CONFIG_WIN32 + ifneq ($(CONFIG_static),yes) + LIBTCC = libtcc$(DLLSUF) + LIBTCCDEF = libtcc.def + endif + CFGWIN = -win + NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32) +else + LIBS=-lm + ifneq ($(CONFIG_ldl),no) + LIBS+=-ldl + endif + # make libtcc as static or dynamic library? + ifeq ($(CONFIG_static),no) + LIBTCC=libtcc$(DLLSUF) + export LD_LIBRARY_PATH := $(CURDIR)/$(TOP) + ifneq ($(CONFIG_rpath),no) + LINK_LIBTCC += -Wl,-rpath,"$(libdir)" + endif + endif + CFGWIN =-unx + NATIVE_TARGET = $(ARCH) + ifdef CONFIG_OSX + NATIVE_TARGET = $(ARCH)-osx + LDFLAGS += -flat_namespace -undefined warning + export MACOSX_DEPLOYMENT_TARGET := 10.2 + endif +endif + +# run local version of tcc with local libraries and includes +TCCFLAGS-unx = -B$(TOP) -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP) +TCCFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP) -L$(TOP) +TCCFLAGS = $(TCCFLAGS$(CFGWIN)) +TCC = $(TOP)/tcc$(EXESUF) $(TCCFLAGS) +ifdef CONFIG_OSX + TCCFLAGS += -D_ANSI_SOURCE +endif + +CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC -DTCC_PROFILE +LIBS_P = $(LIBS) +LDFLAGS_P = $(LDFLAGS) + +CONFIG_$(ARCH) = yes +NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386 +NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64 +NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE +NATIVE_DEFINES_$(CONFIG_OSX) += -DTCC_TARGET_MACHO +NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC +NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL +NATIVE_DEFINES_$(CONFIG_libgcc) += -DCONFIG_USE_LIBGCC +NATIVE_DEFINES_$(CONFIG_selinux) += -DHAVE_SELINUX +NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM +NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT +NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI +NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP +NATIVE_DEFINES_$(CONFIG_arm64) += -DTCC_TARGET_ARM64 +NATIVE_DEFINES += $(NATIVE_DEFINES_yes) + +ifeq ($(INCLUDED),no) +# -------------------------------------------------------------------------- +# running top Makefile + +PROGS = tcc$(EXESUF) +TCCLIBS = $(LIBTCC1) $(LIBTCC) $(LIBTCCDEF) +TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info + +all: $(PROGS) $(TCCLIBS) $(TCCDOCS) + +# cross compiler targets to build +TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67 +# TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi + +# cross libtcc1.a targets to build +LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince + +PROGS_CROSS = $(foreach X,$(TCC_X),$X-tcc$(EXESUF)) +LIBTCC1_CROSS = $(foreach X,$(LIBTCC1_X),$X-libtcc1.a) + +# build cross compilers & libs +cross: $(LIBTCC1_CROSS) $(PROGS_CROSS) + +# build specific cross compiler & lib +cross-%: %-tcc$(EXESUF) %-libtcc1.a ; + +install: ; @$(MAKE) --no-print-directory install$(CFGWIN) +install-strip: ; @$(MAKE) --no-print-directory install$(CFGWIN) CONFIG_strip=yes +uninstall: ; @$(MAKE) --no-print-directory uninstall$(CFGWIN) + +ifdef CONFIG_cross +all : cross +endif + +# -------------------------------------------- + +T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown) +X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-) + +DEF-i386 = -DTCC_TARGET_I386 +DEF-x86_64 = -DTCC_TARGET_X86_64 +DEF-i386-win32 = -DTCC_TARGET_PE -DTCC_TARGET_I386 +DEF-x86_64-win32= -DTCC_TARGET_PE -DTCC_TARGET_X86_64 +DEF-x86_64-osx = -DTCC_TARGET_MACHO -DTCC_TARGET_X86_64 +DEF-arm-wince = -DTCC_TARGET_PE -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP -DTCC_ARM_HARDFLOAT +DEF-arm64 = -DTCC_TARGET_ARM64 +DEF-c67 = -DTCC_TARGET_C67 -w # disable warnigs +DEF-arm-fpa = -DTCC_TARGET_ARM +DEF-arm-fpa-ld = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12 +DEF-arm-vfp = -DTCC_TARGET_ARM -DTCC_ARM_VFP +DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI +DEF-arm-eabihf = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT +DEF-arm = $(DEF-arm-eabihf) +DEF-$(NATIVE_TARGET) = $(NATIVE_DEFINES) + +DEFINES += $(DEF-$T) $(DEF-all) +DEFINES += $(if $(ROOT-$T),-DCONFIG_SYSROOT="\"$(ROOT-$T)\"") +DEFINES += $(if $(CRT-$T),-DCONFIG_TCC_CRTPREFIX="\"$(CRT-$T)\"") +DEFINES += $(if $(LIB-$T),-DCONFIG_TCC_LIBPATHS="\"$(LIB-$T)\"") +DEFINES += $(if $(INC-$T),-DCONFIG_TCC_SYSINCLUDEPATHS="\"$(INC-$T)\"") +DEFINES += $(DEF-$(or $(findstring win,$T),unx)) + +ifneq ($(X),) +ifeq ($(CONFIG_WIN32),yes) +DEF-win += -DTCC_LIBTCC1="\"$(X)libtcc1.a\"" +DEF-unx += -DTCC_LIBTCC1="\"lib/$(X)libtcc1.a\"" +else +DEF-all += -DTCC_LIBTCC1="\"$(X)libtcc1.a\"" +DEF-win += -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" +endif +endif + +# include custom configuration (see make help) +-include config-extra.mak + +CORE_FILES = tcc.c tcctools.c libtcc.c tccpp.c tccgen.c tccelf.c tccasm.c tccrun.c +CORE_FILES += tcc.h config.h libtcc.h tcctok.h +i386_FILES = $(CORE_FILES) i386-gen.c i386-link.c i386-asm.c i386-asm.h i386-tok.h +i386-win32_FILES = $(i386_FILES) tccpe.c +x86_64_FILES = $(CORE_FILES) x86_64-gen.c x86_64-link.c i386-asm.c x86_64-asm.h +x86_64-win32_FILES = $(x86_64_FILES) tccpe.c +x86_64-osx_FILES = $(x86_64_FILES) +arm_FILES = $(CORE_FILES) arm-gen.c arm-link.c arm-asm.c +arm-wince_FILES = $(arm_FILES) tccpe.c +arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c +c67_FILES = $(CORE_FILES) c67-gen.c c67-link.c tcccoff.c + +# libtcc sources +LIBTCC_SRC = $(filter-out tcc.c tcctools.c,$(filter %.c,$($T_FILES))) + +ifeq ($(ONE_SOURCE),yes) +LIBTCC_OBJ = $(X)libtcc.o +LIBTCC_INC = $($T_FILES) +TCC_FILES = $(X)tcc.o +tcc.o : DEFINES += -DONE_SOURCE=0 +else +LIBTCC_OBJ = $(patsubst %.c,$(X)%.o,$(LIBTCC_SRC)) +LIBTCC_INC = $(filter %.h %-gen.c %-link.c,$($T_FILES)) +TCC_FILES = $(X)tcc.o $(LIBTCC_OBJ) +$(TCC_FILES) : DEFINES += -DONE_SOURCE=0 +endif + +# target specific object rule +$(X)%.o : %.c $(LIBTCC_INC) + $(CC) -o $@ -c $< $(DEFINES) $(CFLAGS) + +# additional dependencies +$(X)tcc.o : tcctools.c + +# Host Tiny C Compiler +tcc$(EXESUF): tcc.o $(LIBTCC) + $(CC) -o $@ $^ $(LIBS) $(LDFLAGS) $(LINK_LIBTCC) + +# Cross Tiny C Compilers +%-tcc$(EXESUF): FORCE + @$(MAKE) --no-print-directory $@ CROSS_TARGET=$* ONE_SOURCE=$(or $(ONE_SOURCE),yes) + +$(CROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES) + $(CC) -o $@ $^ $(LIBS) $(LDFLAGS) + +# profiling version +tcc_p$(EXESUF): $($T_FILES) + $(CC) -o $@ $< $(DEFINES) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P) + +# static libtcc library +libtcc.a: $(LIBTCC_OBJ) + $(AR) rcs $@ $^ + +# dynamic libtcc library +libtcc.so: $(LIBTCC_OBJ) + $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDFLAGS) + +libtcc.so: CFLAGS+=-fPIC +libtcc.so: LDFLAGS+=-fPIC + +# windows dynamic libtcc library +libtcc.dll : $(LIBTCC_OBJ) + $(CC) -shared -o $@ $^ $(LDFLAGS) +libtcc.dll : DEFINES += -DLIBTCC_AS_DLL + +# import file for windows libtcc.dll +libtcc.def : libtcc.dll tcc$(EXESUF) + $(XTCC) -impdef $< -o $@ +XTCC ?= ./tcc$(EXESUF) + +# TinyCC runtime libraries +libtcc1.a : tcc$(EXESUF) FORCE + @$(MAKE) -C lib DEFINES='$(DEF-$T)' + +# Cross libtcc1.a +%-libtcc1.a : %-tcc$(EXESUF) FORCE + @$(MAKE) -C lib DEFINES='$(DEF-$*)' CROSS_TARGET=$* + +.PRECIOUS: %-libtcc1.a +FORCE: + +# -------------------------------------------------------------------------- +# documentation and man page +tcc-doc.html: tcc-doc.texi + makeinfo --no-split --html --number-sections -o $@ $< || true + +tcc.1: tcc-doc.texi + $(TOPSRC)/texi2pod.pl $< tcc.pod \ + && pod2man --section=1 --center="Tiny C Compiler" --release="$(VERSION)" tcc.pod >tmp.1 \ + && mv tmp.1 $@ || rm -f tmp.1 + +tcc-doc.info: tcc-doc.texi + makeinfo $< || true + +# -------------------------------------------------------------------------- +# install + +INSTALL = install -m644 +INSTALLBIN = install -m755 $(STRIP_$(CONFIG_strip)) +STRIP_yes = -s + +LIBTCC1_W = $(filter %-win32-libtcc1.a %-wince-libtcc1.a,$(LIBTCC1_CROSS)) +LIBTCC1_U = $(filter-out $(LIBTCC1_W),$(LIBTCC1_CROSS)) +IB = $(if $1,mkdir -p $2 && $(INSTALLBIN) $1 $2) +IBw = $(call IB,$(wildcard $1),$2) +IF = $(if $1,mkdir -p $2 && $(INSTALL) $1 $2) +IFw = $(call IF,$(wildcard $1),$2) +IR = mkdir -p $2 && cp -r $1/. $2 + +# install progs & libs +install-unx: + $(call IBw,$(PROGS) $(PROGS_CROSS),"$(bindir)") + $(call IFw,$(LIBTCC1) $(LIBTCC1_U),"$(tccdir)") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include") + $(call $(if $(findstring .so,$(LIBTCC)),IBw,IFw),$(LIBTCC),"$(libdir)") + $(call IF,$(TOPSRC)/libtcc.h,"$(includedir)") + $(call IFw,tcc.1,"$(mandir)/man1") + $(call IFw,tcc-doc.info,"$(infodir)") + $(call IFw,tcc-doc.html,"$(docdir)") +ifneq "$(wildcard $(LIBTCC1_W))" "" + $(call IFw,$(TOPSRC)/win32/lib/*.def $(LIBTCC1_W),"$(tccdir)/win32/lib") + $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/win32/include") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/win32/include") +endif + +# uninstall +uninstall-unx: + @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS),"$(bindir)/$P") + @rm -fv "$(libdir)/libtcc.a" "$(libdir)/libtcc.so" "$(includedir)/libtcc.h" + @rm -fv "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info" + @rm -fv "$(docdir)/tcc-doc.html" + rm -r "$(tccdir)" + +# install progs & libs on windows +install-win: + $(call IBw,$(PROGS) $(PROGS_CROSS) $(subst libtcc.a,,$(LIBTCC)),"$(bindir)") + $(call IF,$(TOPSRC)/win32/lib/*.def,"$(tccdir)/lib") + $(call IFw,libtcc1.a $(LIBTCC1_W),"$(tccdir)/lib") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include") + $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/include") + $(call IR,$(TOPSRC)/win32/examples,"$(tccdir)/examples") + $(call IF,$(TOPSRC)/tests/libtcc_test.c,"$(tccdir)/examples") + $(call IFw,$(TOPSRC)/libtcc.h $(subst .dll,.def,$(LIBTCC)),"$(libdir)") + $(call IFw,$(TOPSRC)/win32/tcc-win32.txt tcc-doc.html,"$(docdir)") +ifneq "$(wildcard $(LIBTCC1_U))" "" + $(call IFw,$(LIBTCC1_U),"$(tccdir)/lib") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/lib/include") +endif + +# the msys-git shell works to configure && make except it does not have install +ifeq "$(and $(CONFIG_WIN32),$(shell which install >/dev/null 2>&1 || echo no))" "no" +install-win : INSTALL = cp +install-win : INSTALLBIN = cp +endif + +# uninstall on windows +uninstall-win: + @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS) libtcc.dll,"$(bindir)/$P") + @rm -fv $(foreach F,tcc-doc.html tcc-win32.txt,"$(docdir)/$F") + @rm -fv $(foreach F,libtcc.h libtcc.def libtcc.a,"$(libdir)/$F") + rm -r "$(tccdir)" + +# -------------------------------------------------------------------------- +# other stuff + +TAGFILES = *.[ch] include/*.h lib/*.[chS] +tags : ; ctags $(TAGFILES) +# cannot have both tags and TAGS on windows +ETAGS : ; etags $(TAGFILES) + +# create release tarball from *current* git branch (including tcc-doc.html +# and converting two files to CRLF) +TCC-VERSION = tcc-$(VERSION) +tar: tcc-doc.html + mkdir $(TCC-VERSION) + ( cd $(TCC-VERSION) && git --git-dir ../.git checkout -f ) + cp tcc-doc.html $(TCC-VERSION) + for f in tcc-win32.txt build-tcc.bat ; do \ + cat win32/$$f | sed 's,\(.*\),\1\r,g' > $(TCC-VERSION)/win32/$$f ; \ + done + tar cjf $(TCC-VERSION).tar.bz2 $(TCC-VERSION) + rm -rf $(TCC-VERSION) + git reset + +config.mak: + $(if $(wildcard $@),,@echo "Please run ./configure." && exit 1) + +# run all tests +test: + $(MAKE) -C tests +# run test(s) from tests2 subdir (see make help) +tests2.%: + $(MAKE) -C tests/tests2 $@ + +clean: + rm -f tcc$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF) tcc.pod + rm -f *~ *.o *.a *.so* *.out *.log lib*.def *.exe *.dll a.out tags TAGS + @$(MAKE) -C lib $@ + @$(MAKE) -C tests $@ + +distclean: clean + rm -f config.h config.mak config.texi tcc.1 tcc-doc.info tcc-doc.html + +.PHONY: all clean test tar tags ETAGS distclean install uninstall FORCE + +help: + @echo "make" + @echo " build native compiler (from separate objects)" + @echo "" + @echo "make cross" + @echo " build cross compilers (from one source)" + @echo "" + @echo "make ONE_SOURCE=yes / no" + @echo " force building from one source / separate objects" + @echo "" + @echo "make cross-TARGET" + @echo " build one specific cross compiler for 'TARGET', as in" + @echo " $(TCC_X)" + @echo "" + @echo "Custom configuration:" + @echo " The makefile includes a file 'config-extra.mak' if it is present." + @echo " This file may contain some custom configuration. For example:" + @echo "" + @echo " NATIVE_DEFINES += -D..." + @echo "" + @echo " Or for example to configure the search paths for a cross-compiler" + @echo " that expects the linux files in /i386-linux:" + @echo "" + @echo " ROOT-i386 = {B}/i386-linux" + @echo " CRT-i386 = {B}/i386-linux/usr/lib" + @echo " LIB-i386 = {B}/i386-linux/lib:{B}/i386-linux/usr/lib" + @echo " INC-i386 = {B}/lib/include:{B}/i386-linux/usr/include" + @echo " DEF-i386 += -D__linux__" + @echo "" + @echo "make test" + @echo " run all tests" + @echo "" + @echo "make tests2.all / make tests2.37 / make tests2.37+" + @echo " run all/single test(s) from tests2, optionally update .expect" + @echo "" + @echo "Other supported make targets:" + @echo " install install-strip tags ETAGS tar clean distclean help" + @echo "" + +# -------------------------------------------------------------------------- +endif # ($(INCLUDED),no) diff --git a/05/tcc-final-old/README b/05/tcc-final-old/README new file mode 100644 index 0000000..3a3f90b --- /dev/null +++ b/05/tcc-final-old/README @@ -0,0 +1,95 @@ +Tiny C Compiler - C Scripting Everywhere - The Smallest ANSI C compiler +----------------------------------------------------------------------- + +Features: +-------- + +- SMALL! You can compile and execute C code everywhere, for example on + rescue disks. + +- FAST! tcc generates optimized x86 code. No byte code + overhead. Compile, assemble and link about 7 times faster than 'gcc + -O0'. + +- UNLIMITED! Any C dynamic library can be used directly. TCC is + heading torward full ISOC99 compliance. TCC can of course compile + itself. + +- SAFE! tcc includes an optional memory and bound checker. Bound + checked code can be mixed freely with standard code. + +- Compile and execute C source directly. No linking or assembly + necessary. Full C preprocessor included. + +- C script supported : just add '#!/usr/local/bin/tcc -run' at the first + line of your C source, and execute it directly from the command + line. + +Documentation: +------------- + +1) Installation on a i386/x86_64/arm Linux/OSX/FreeBSD host + + ./configure + make + make test + make install + + Notes: For OSX and FreeBSD, gmake should be used instead of make. + For Windows read tcc-win32.txt. + +makeinfo must be installed to compile the doc. By default, tcc is +installed in /usr/local/bin. ./configure --help shows configuration +options. + + +2) Introduction + +We assume here that you know ANSI C. Look at the example ex1.c to know +what the programs look like. + +The include file can be used if you want a small basic libc +include support (especially useful for floppy disks). Of course, you +can also use standard headers, although they are slower to compile. + +You can begin your C script with '#!/usr/local/bin/tcc -run' on the first +line and set its execute bits (chmod a+x your_script). Then, you can +launch the C code as a shell or perl script :-) The command line +arguments are put in 'argc' and 'argv' of the main functions, as in +ANSI C. + +3) Examples + +ex1.c: simplest example (hello world). Can also be launched directly +as a script: './ex1.c'. + +ex2.c: more complicated example: find a number with the four +operations given a list of numbers (benchmark). + +ex3.c: compute fibonacci numbers (benchmark). + +ex4.c: more complicated: X11 program. Very complicated test in fact +because standard headers are being used ! As for ex1.c, can also be launched +directly as a script: './ex4.c'. + +ex5.c: 'hello world' with standard glibc headers. + +tcc.c: TCC can of course compile itself. Used to check the code +generator. + +tcctest.c: auto test for TCC which tests many subtle possible bugs. Used +when doing 'make test'. + +4) Full Documentation + +Please read tcc-doc.html to have all the features of TCC. + +Additional information is available for the Windows port in tcc-win32.txt. + +License: +------- + +TCC is distributed under the GNU Lesser General Public License (see +COPYING file). + +Fabrice Bellard. diff --git a/05/tcc-final-old/RELICENSING b/05/tcc-final-old/RELICENSING new file mode 100644 index 0000000..20841a7 --- /dev/null +++ b/05/tcc-final-old/RELICENSING @@ -0,0 +1,60 @@ + + Relicensing TinyCC + ------------------ + + The authors listed below hereby confirm their agreement to relicense TinyCC + including their past contributions under the following terms: + + + * 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. + + + Author (name) I agree (YES/NO) Files/Features (optional) + ------------------------------------------------------------------------------ + Adam Sampson YES makefiles + Daniel Glöckner NO arm-gen.c + Daniel Glöckner YES not arm-gen.c + Edmund Grimley Evans YES arm64 + Fabrice Bellard YES original author + Frédéric Féret YES x86 64/16 bit asm + grischka YES tccpe.c + Henry Kroll YES + Joe Soroka YES + Kirill Smelkov YES + mingodad YES + Pip Cet YES + Shinichiro Hamaji YES x86_64-gen.c + Vincent Lefèvre YES + Thomas Preud'homme YES arm-gen.c + Timo VJ Lähde (Timppa) ? tiny_libmaker.c + TK ? tcccoff.c c67-gen.c + Urs Janssen YES + waddlesplash YES + Christian Jullien YES Windows Cygwin build and tests + + + ------------------------------------------------------------------------------ + + Please add yourself to the list above (rsp. replace the question mark) + and (after fetching the latest version) commit to the "mob" branch with + commit message: + + Relicensing TinyCC + + Thanks. diff --git a/05/tcc-final-old/TODO b/05/tcc-final-old/TODO new file mode 100644 index 0000000..d810088 --- /dev/null +++ b/05/tcc-final-old/TODO @@ -0,0 +1,100 @@ +TODO list: + +Bugs: + +- i386 fastcall is mostly wrong +- FPU st(0) is left unclean (kwisatz haderach). Incompatible with + optimized gcc/msc code +- see transparent union pb in /urs/include/sys/socket.h +- precise behaviour of typeof with arrays ? (__put_user macro) + but should suffice for most cases) +- handle '? x, y : z' in unsized variable initialization (',' is + considered incorrectly as separator in preparser) +- transform functions to function pointers in function parameters + (net/ipv4/ip_output.c) +- fix function pointer type display +- check section alignment in C +- fix invalid cast in comparison 'if (v == (int8_t)v)' +- finish varargs.h support (gcc 3.2 testsuite issue) +- fix static functions declared inside block +- fix multiple unions init +- make libtcc fully reentrant (except for the compilation stage itself). +- struct/union/enum definitions in nested scopes (see also Debian bug #770657) +- __STDC_IEC_559__: float f(void) { static float x = 0.0 / 0.0; return x; } +- memory may be leaked after errors (longjmp). + +Portability: + +- it is assumed that int is 32-bit and sizeof(int) == 4 +- int is used when host or target size_t would make more sense +- TCC handles target floating-point (fp) values using the host's fp + arithmetic, which is simple and fast but may lead to exceptions + and inaccuracy and wrong representations when cross-compiling + +Linking: + +- static linking (-static) does not work + +Bound checking: + +- fix bound exit on RedHat 7.3 +- setjmp is not supported properly in bound checking. +- fix bound check code with '&' on local variables (currently done + only for local arrays). +- bound checking and float/long long/struct copy code. bound + checking and symbol + offset optimization + +Missing features: + +- disable-asm and disable-bcheck options +- __builtin_expect() +- atexit (Nigel Horne) +- C99: add complex types (gcc 3.2 testsuite issue) +- postfix compound literals (see 20010124-1.c) +- interactive mode / integrated debugger + +Optimizations: + +- suppress specific anonymous symbol handling +- more parse optimizations (=even faster compilation) +- memory alloc optimizations (=even faster compilation) +- optimize VT_LOCAL + const +- better local variables handling (needed for other targets) + +Not critical: + +- C99: fix multiple compound literals inits in blocks (ISOC99 + normative example - only relevant when using gotos! -> must add + boolean variable to tell if compound literal was already + initialized). +- add PowerPC generator and improve codegen for RISC (need + to suppress VT_LOCAL and use a base register instead). +- fix preprocessor symbol redefinition +- add portable byte code generator and interpreter for other + unsupported architectures. +- C++: variable declaration in for, minimal 'class' support. +- win32: __intxx. use resolve for bchecked malloc et al. + check exception code (exception filter func). +- handle void (__attribute__() *ptr)() +- VLAs are implemented in a way that is not compatible with signals: + http://lists.gnu.org/archive/html/tinycc-devel/2015-11/msg00018.html + +Fixed (probably): + +- bug with defines: + #define spin_lock(lock) do { } while (0) + #define wq_spin_lock spin_lock + #define TEST() wq_spin_lock(a) +- typedefs can be structure fields +- see bugfixes.diff + improvement.diff from Daniel Glockner +- long long constant evaluation +- add alloca() +- gcc '-E' option. +- #include_next support for /usr/include/limits ? +- function pointers/lvalues in ? : (linux kernel net/core/dev.c) +- win32: add __stdcall, check GetModuleHandle for dlls. +- macro substitution with nested definitions (ShangHongzhang) +- with "-run" and libtcc, a PLT is now built. +- '-E' option was improved +- packed attribute is now supported +- ARM and ARM64 code generators have been added. diff --git a/05/tcc-final-old/VERSION b/05/tcc-final-old/VERSION new file mode 100644 index 0000000..9a54223 --- /dev/null +++ b/05/tcc-final-old/VERSION @@ -0,0 +1 @@ +0.9.27 diff --git a/05/tcc-final-old/arm-asm.c b/05/tcc-final-old/arm-asm.c new file mode 100644 index 0000000..3b5ae66 --- /dev/null +++ b/05/tcc-final-old/arm-asm.c @@ -0,0 +1,94 @@ +/*************************************************************/ +/* + * ARM dummy assembler for TCC + * + */ + +#ifdef TARGET_DEFS_ONLY + +#define CONFIG_TCC_ASM +#define NB_ASM_REGS 16 + +ST_FUNC void g(int c); +ST_FUNC void gen_le16(int c); +ST_FUNC void gen_le32(int c); + +/*************************************************************/ +#else +/*************************************************************/ + +#include "tcc.h" + +static void asm_error(void) +{ + tcc_error("ARM asm not implemented."); +} + +/* XXX: make it faster ? */ +ST_FUNC void g(int c) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 1; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; + ind = ind1; +} + +ST_FUNC void gen_le16 (int i) +{ + g(i); + g(i>>8); +} + +ST_FUNC void gen_le32 (int i) +{ + gen_le16(i); + gen_le16(i>>16); +} + +ST_FUNC void gen_expr32(ExprValue *pe) +{ + gen_le32(pe->v); +} + +ST_FUNC void asm_opcode(TCCState *s1, int opcode) +{ + asm_error(); +} + +ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier) +{ + asm_error(); +} + +/* generate prolog and epilog code for asm statement */ +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg) +{ +} + +ST_FUNC void asm_compute_constraints(ASMOperand *operands, + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg) +{ +} + +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str) +{ + asm_error(); +} + +ST_FUNC int asm_parse_regvar (int t) +{ + asm_error(); + return -1; +} + +/*************************************************************/ +#endif /* ndef TARGET_DEFS_ONLY */ diff --git a/05/tcc-final-old/arm-gen.c b/05/tcc-final-old/arm-gen.c new file mode 100644 index 0000000..f535a09 --- /dev/null +++ b/05/tcc-final-old/arm-gen.c @@ -0,0 +1,2151 @@ +/* + * ARMv4 code generator for TCC + * + * Copyright (c) 2003 Daniel Glöckner + * Copyright (c) 2012 Thomas Preud'homme + * + * Based on i386-gen.c by Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +#if defined(TCC_ARM_EABI) && !defined(TCC_ARM_VFP) +#error "Currently TinyCC only supports float computation with VFP instructions" +#endif + +/* number of available registers */ +#ifdef TCC_ARM_VFP +#define NB_REGS 13 +#else +#define NB_REGS 9 +#endif + +#ifndef TCC_CPU_VERSION +# define TCC_CPU_VERSION 5 +#endif + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_R0 0x0004 +#define RC_R1 0x0008 +#define RC_R2 0x0010 +#define RC_R3 0x0020 +#define RC_R12 0x0040 +#define RC_F0 0x0080 +#define RC_F1 0x0100 +#define RC_F2 0x0200 +#define RC_F3 0x0400 +#ifdef TCC_ARM_VFP +#define RC_F4 0x0800 +#define RC_F5 0x1000 +#define RC_F6 0x2000 +#define RC_F7 0x4000 +#endif +#define RC_IRET RC_R0 /* function return: integer register */ +#define RC_LRET RC_R1 /* function return: second integer register */ +#define RC_FRET RC_F0 /* function return: float register */ + +/* pretty names for the registers */ +enum { + TREG_R0 = 0, + TREG_R1, + TREG_R2, + TREG_R3, + TREG_R12, + TREG_F0, + TREG_F1, + TREG_F2, + TREG_F3, +#ifdef TCC_ARM_VFP + TREG_F4, + TREG_F5, + TREG_F6, + TREG_F7, +#endif + TREG_SP = 13, + TREG_LR, +}; + +#ifdef TCC_ARM_VFP +#define T2CPR(t) (((t) & VT_BTYPE) != VT_FLOAT ? 0x100 : 0) +#endif + +/* return registers for function */ +#define REG_IRET TREG_R0 /* single word int return register */ +#define REG_LRET TREG_R1 /* second word return register (for long long) */ +#define REG_FRET TREG_F0 /* float return register */ + +#ifdef TCC_ARM_EABI +#define TOK___divdi3 TOK___aeabi_ldivmod +#define TOK___moddi3 TOK___aeabi_ldivmod +#define TOK___udivdi3 TOK___aeabi_uldivmod +#define TOK___umoddi3 TOK___aeabi_uldivmod +#endif + +/* defined if function parameters must be evaluated in reverse order */ +#define INVERT_FUNC_PARAMS + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#ifdef TCC_ARM_VFP +#define LDOUBLE_SIZE 8 +#endif + +#ifndef LDOUBLE_SIZE +#define LDOUBLE_SIZE 8 +#endif + +#ifdef TCC_ARM_EABI +#define LDOUBLE_ALIGN 8 +#else +#define LDOUBLE_ALIGN 4 +#endif + +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 8 + +#define CHAR_IS_UNSIGNED + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#include "tcc.h" + +enum float_abi float_abi; + +ST_DATA const int reg_classes[NB_REGS] = { + /* r0 */ RC_INT | RC_R0, + /* r1 */ RC_INT | RC_R1, + /* r2 */ RC_INT | RC_R2, + /* r3 */ RC_INT | RC_R3, + /* r12 */ RC_INT | RC_R12, + /* f0 */ RC_FLOAT | RC_F0, + /* f1 */ RC_FLOAT | RC_F1, + /* f2 */ RC_FLOAT | RC_F2, + /* f3 */ RC_FLOAT | RC_F3, +#ifdef TCC_ARM_VFP + /* d4/s8 */ RC_FLOAT | RC_F4, +/* d5/s10 */ RC_FLOAT | RC_F5, +/* d6/s12 */ RC_FLOAT | RC_F6, +/* d7/s14 */ RC_FLOAT | RC_F7, +#endif +}; + +static int func_sub_sp_offset, last_itod_magic; +static int leaffunc; + +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) +static CType float_type, double_type, func_float_type, func_double_type; +ST_FUNC void arm_init(struct TCCState *s) +{ + float_type.t = VT_FLOAT; + double_type.t = VT_DOUBLE; + func_float_type.t = VT_FUNC; + func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD); + func_double_type.t = VT_FUNC; + func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD); + + float_abi = s->float_abi; +#ifndef TCC_ARM_HARDFLOAT + tcc_warning("soft float ABI currently not supported: default to softfp"); +#endif +} +#else +#define func_float_type func_old_type +#define func_double_type func_old_type +#define func_ldouble_type func_old_type +ST_FUNC void arm_init(struct TCCState *s) +{ +#if 0 +#if !defined (TCC_ARM_VFP) + tcc_warning("Support for FPA is deprecated and will be removed in next" + " release"); +#endif +#if !defined (TCC_ARM_EABI) + tcc_warning("Support for OABI is deprecated and will be removed in next" + " release"); +#endif +#endif +} +#endif + +static int two2mask(int a,int b) { + return (reg_classes[a]|reg_classes[b])&~(RC_INT|RC_FLOAT); +} + +static int regmask(int r) { + return reg_classes[r]&~(RC_INT|RC_FLOAT); +} + +/******************************************************/ + +#if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP) +const char *default_elfinterp(struct TCCState *s) +{ + if (s->float_abi == ARM_HARD_FLOAT) + return "/lib/ld-linux-armhf.so.3"; + else + return "/lib/ld-linux.so.3"; +} +#endif + +void o(uint32_t i) +{ + /* this is a good place to start adding big-endian support*/ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 4; + if (!cur_text_section) + tcc_error("compiler error! This happens f.ex. if the compiler\n" + "can't evaluate constant expressions outside of a function."); + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind++] = i&255; + i>>=8; + cur_text_section->data[ind++] = i&255; + i>>=8; + cur_text_section->data[ind++] = i&255; + i>>=8; + cur_text_section->data[ind++] = i; +} + +static uint32_t stuff_const(uint32_t op, uint32_t c) +{ + int try_neg=0; + uint32_t nc = 0, negop = 0; + + switch(op&0x1F00000) + { + case 0x800000: //add + case 0x400000: //sub + try_neg=1; + negop=op^0xC00000; + nc=-c; + break; + case 0x1A00000: //mov + case 0x1E00000: //mvn + try_neg=1; + negop=op^0x400000; + nc=~c; + break; + case 0x200000: //xor + if(c==~0) + return (op&0xF010F000)|((op>>16)&0xF)|0x1E00000; + break; + case 0x0: //and + if(c==~0) + return (op&0xF010F000)|((op>>16)&0xF)|0x1A00000; + case 0x1C00000: //bic + try_neg=1; + negop=op^0x1C00000; + nc=~c; + break; + case 0x1800000: //orr + if(c==~0) + return (op&0xFFF0FFFF)|0x1E00000; + break; + } + do { + uint32_t m; + int i; + if(c<256) /* catch undefined <<32 */ + return op|c; + for(i=2;i<32;i+=2) { + m=(0xff>>i)|(0xff<<(32-i)); + if(!(c&~m)) + return op|(i<<7)|(c<>(32-i)); + } + op=negop; + c=nc; + } while(try_neg--); + return 0; +} + + +//only add,sub +void stuff_const_harder(uint32_t op, uint32_t v) { + uint32_t x; + x=stuff_const(op,v); + if(x) + o(x); + else { + uint32_t a[16], nv, no, o2, n2; + int i,j,k; + a[0]=0xff; + o2=(op&0xfff0ffff)|((op&0xf000)<<4);; + for(i=1;i<16;i++) + a[i]=(a[i-1]>>2)|(a[i-1]<<30); + for(i=0;i<12;i++) + for(j=i<4?i+12:15;j>=i+4;j--) + if((v&(a[i]|a[j]))==v) { + o(stuff_const(op,v&a[i])); + o(stuff_const(o2,v&a[j])); + return; + } + no=op^0xC00000; + n2=o2^0xC00000; + nv=-v; + for(i=0;i<12;i++) + for(j=i<4?i+12:15;j>=i+4;j--) + if((nv&(a[i]|a[j]))==nv) { + o(stuff_const(no,nv&a[i])); + o(stuff_const(n2,nv&a[j])); + return; + } + for(i=0;i<8;i++) + for(j=i+4;j<12;j++) + for(k=i<4?i+12:15;k>=j+4;k--) + if((v&(a[i]|a[j]|a[k]))==v) { + o(stuff_const(op,v&a[i])); + o(stuff_const(o2,v&a[j])); + o(stuff_const(o2,v&a[k])); + return; + } + no=op^0xC00000; + nv=-v; + for(i=0;i<8;i++) + for(j=i+4;j<12;j++) + for(k=i<4?i+12:15;k>=j+4;k--) + if((nv&(a[i]|a[j]|a[k]))==nv) { + o(stuff_const(no,nv&a[i])); + o(stuff_const(n2,nv&a[j])); + o(stuff_const(n2,nv&a[k])); + return; + } + o(stuff_const(op,v&a[0])); + o(stuff_const(o2,v&a[4])); + o(stuff_const(o2,v&a[8])); + o(stuff_const(o2,v&a[12])); + } +} + +uint32_t encbranch(int pos, int addr, int fail) +{ + addr-=pos+8; + addr/=4; + if(addr>=0x1000000 || addr<-0x1000000) { + if(fail) + tcc_error("FIXME: function bigger than 32MB"); + return 0; + } + return 0x0A000000|(addr&0xffffff); +} + +int decbranch(int pos) +{ + int x; + x=*(uint32_t *)(cur_text_section->data + pos); + x&=0x00ffffff; + if(x&0x800000) + x-=0x1000000; + return x*4+pos+8; +} + +/* output a symbol and patch all calls to it */ +void gsym_addr(int t, int a) +{ + uint32_t *x; + int lt; + while(t) { + x=(uint32_t *)(cur_text_section->data + t); + t=decbranch(lt=t); + if(a==lt+4) + *x=0xE1A00000; // nop + else { + *x &= 0xff000000; + *x |= encbranch(lt,a,1); + } + } +} + +void gsym(int t) +{ + gsym_addr(t, ind); +} + +#ifdef TCC_ARM_VFP +static uint32_t vfpr(int r) +{ + if(rTREG_F7) + tcc_error("compiler error! register %i is no vfp register",r); + return r - TREG_F0; +} +#else +static uint32_t fpr(int r) +{ + if(rTREG_F3) + tcc_error("compiler error! register %i is no fpa register",r); + return r - TREG_F0; +} +#endif + +static uint32_t intr(int r) +{ + if(r == TREG_R12) + return 12; + if(r >= TREG_R0 && r <= TREG_R3) + return r - TREG_R0; + if (r >= TREG_SP && r <= TREG_LR) + return r + (13 - TREG_SP); + tcc_error("compiler error! register %i is no int register",r); +} + +static void calcaddr(uint32_t *base, int *off, int *sgn, int maxoff, unsigned shift) +{ + if(*off>maxoff || *off&((1<r; + ft = sv->type.t; + fc = sv->c.i; + + if(fc>=0) + sign=0; + else { + sign=1; + fc=-fc; + } + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + uint32_t base = 0xB; // fp + if(v == VT_LLOCAL) { + v1.type.t = VT_PTR; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = sv->c.i; + load(TREG_LR, &v1); + base = 14; /* lr */ + fc=sign=0; + v=VT_LOCAL; + } else if(v == VT_CONST) { + v1.type.t = VT_PTR; + v1.r = fr&~VT_LVAL; + v1.c.i = sv->c.i; + v1.sym=sv->sym; + load(TREG_LR, &v1); + base = 14; /* lr */ + fc=sign=0; + v=VT_LOCAL; + } else if(v < VT_CONST) { + base=intr(v); + fc=sign=0; + v=VT_LOCAL; + } + if(v == VT_LOCAL) { + if(is_float(ft)) { + calcaddr(&base,&fc,&sign,1020,2); +#ifdef TCC_ARM_VFP + op=0xED100A00; /* flds */ + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x100; /* flds -> fldd */ + o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16)); +#else + op=0xED100100; + if(!sign) + op|=0x800000; +#if LDOUBLE_SIZE == 8 + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x8000; +#else + if ((ft & VT_BTYPE) == VT_DOUBLE) + op|=0x8000; + else if ((ft & VT_BTYPE) == VT_LDOUBLE) + op|=0x400000; +#endif + o(op|(fpr(r)<<12)|(fc>>2)|(base<<16)); +#endif + } else if((ft & (VT_BTYPE|VT_UNSIGNED)) == VT_BYTE + || (ft & VT_BTYPE) == VT_SHORT) { + calcaddr(&base,&fc,&sign,255,0); + op=0xE1500090; + if ((ft & VT_BTYPE) == VT_SHORT) + op|=0x20; + if ((ft & VT_UNSIGNED) == 0) + op|=0x40; + if(!sign) + op|=0x800000; + o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf)); + } else { + calcaddr(&base,&fc,&sign,4095,0); + op=0xE5100000; + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) == VT_BYTE || (ft & VT_BTYPE) == VT_BOOL) + op|=0x400000; + o(op|(intr(r)<<12)|fc|(base<<16)); + } + return; + } + } else { + if (v == VT_CONST) { + op=stuff_const(0xE3A00000|(intr(r)<<12),sv->c.i); + if (fr & VT_SYM || !op) { + o(0xE59F0000|(intr(r)<<12)); + o(0xEA000000); + if(fr & VT_SYM) + greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32); + o(sv->c.i); + } else + o(op); + return; + } else if (v == VT_LOCAL) { + op=stuff_const(0xE28B0000|(intr(r)<<12),sv->c.i); + if (fr & VT_SYM || !op) { + o(0xE59F0000|(intr(r)<<12)); + o(0xEA000000); + if(fr & VT_SYM) // needed ? + greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32); + o(sv->c.i); + o(0xE08B0000|(intr(r)<<12)|intr(r)); + } else + o(op); + return; + } else if(v == VT_CMP) { + o(mapcc(sv->c.i)|0x3A00001|(intr(r)<<12)); + o(mapcc(negcc(sv->c.i))|0x3A00000|(intr(r)<<12)); + return; + } else if (v == VT_JMP || v == VT_JMPI) { + int t; + t = v & 1; + o(0xE3A00000|(intr(r)<<12)|t); + o(0xEA000000); + gsym(sv->c.i); + o(0xE3A00000|(intr(r)<<12)|(t^1)); + return; + } else if (v < VT_CONST) { + if(is_float(ft)) +#ifdef TCC_ARM_VFP + o(0xEEB00A40|(vfpr(r)<<12)|vfpr(v)|T2CPR(ft)); /* fcpyX */ +#else + o(0xEE008180|(fpr(r)<<12)|fpr(v)); +#endif + else + o(0xE1A00000|(intr(r)<<12)|intr(v)); + return; + } + } + tcc_error("load unimplemented!"); +} + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue *sv) +{ + SValue v1; + int v, ft, fc, fr, sign; + uint32_t op; + + fr = sv->r; + ft = sv->type.t; + fc = sv->c.i; + + if(fc>=0) + sign=0; + else { + sign=1; + fc=-fc; + } + + v = fr & VT_VALMASK; + if (fr & VT_LVAL || fr == VT_LOCAL) { + uint32_t base = 0xb; /* fp */ + if(v < VT_CONST) { + base=intr(v); + v=VT_LOCAL; + fc=sign=0; + } else if(v == VT_CONST) { + v1.type.t = ft; + v1.r = fr&~VT_LVAL; + v1.c.i = sv->c.i; + v1.sym=sv->sym; + load(TREG_LR, &v1); + base = 14; /* lr */ + fc=sign=0; + v=VT_LOCAL; + } + if(v == VT_LOCAL) { + if(is_float(ft)) { + calcaddr(&base,&fc,&sign,1020,2); +#ifdef TCC_ARM_VFP + op=0xED000A00; /* fsts */ + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x100; /* fsts -> fstd */ + o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16)); +#else + op=0xED000100; + if(!sign) + op|=0x800000; +#if LDOUBLE_SIZE == 8 + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x8000; +#else + if ((ft & VT_BTYPE) == VT_DOUBLE) + op|=0x8000; + if ((ft & VT_BTYPE) == VT_LDOUBLE) + op|=0x400000; +#endif + o(op|(fpr(r)<<12)|(fc>>2)|(base<<16)); +#endif + return; + } else if((ft & VT_BTYPE) == VT_SHORT) { + calcaddr(&base,&fc,&sign,255,0); + op=0xE14000B0; + if(!sign) + op|=0x800000; + o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf)); + } else { + calcaddr(&base,&fc,&sign,4095,0); + op=0xE5000000; + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) == VT_BYTE || (ft & VT_BTYPE) == VT_BOOL) + op|=0x400000; + o(op|(intr(r)<<12)|fc|(base<<16)); + } + return; + } + } + tcc_error("store unimplemented"); +} + +static void gadd_sp(int val) +{ + stuff_const_harder(0xE28DD000,val); +} + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + uint32_t x; + /* constant case */ + x=encbranch(ind,ind+vtop->c.i,0); + if(x) { + if (vtop->r & VT_SYM) { + /* relocation case */ + greloc(cur_text_section, vtop->sym, ind, R_ARM_PC24); + } else + put_elf_reloc(symtab_section, cur_text_section, ind, R_ARM_PC24, 0); + o(x|(is_jmp?0xE0000000:0xE1000000)); + } else { + if(!is_jmp) + o(0xE28FE004); // add lr,pc,#4 + o(0xE51FF004); // ldr pc,[pc,#-4] + if (vtop->r & VT_SYM) + greloc(cur_text_section, vtop->sym, ind, R_ARM_ABS32); + o(vtop->c.i); + } + } else { + /* otherwise, indirect call */ + r = gv(RC_INT); + if(!is_jmp) + o(0xE1A0E00F); // mov lr,pc + o(0xE1A0F000|intr(r)); // mov pc,r + } +} + +static int unalias_ldbl(int btype) +{ +#if LDOUBLE_SIZE == 8 + if (btype == VT_LDOUBLE) + btype = VT_DOUBLE; +#endif + return btype; +} + +/* Return whether a structure is an homogeneous float aggregate or not. + The answer is true if all the elements of the structure are of the same + primitive float type and there is less than 4 elements. + + type: the type corresponding to the structure to be tested */ +static int is_hgen_float_aggr(CType *type) +{ + if ((type->t & VT_BTYPE) == VT_STRUCT) { + struct Sym *ref; + int btype, nb_fields = 0; + + ref = type->ref->next; + btype = unalias_ldbl(ref->type.t & VT_BTYPE); + if (btype == VT_FLOAT || btype == VT_DOUBLE) { + for(; ref && btype == unalias_ldbl(ref->type.t & VT_BTYPE); ref = ref->next, nb_fields++); + return !ref && nb_fields <= 4; + } + } + return 0; +} + +struct avail_regs { + signed char avail[3]; /* 3 holes max with only float and double alignments */ + int first_hole; /* first available hole */ + int last_hole; /* last available hole (none if equal to first_hole) */ + int first_free_reg; /* next free register in the sequence, hole excluded */ +}; + +#define AVAIL_REGS_INITIALIZER (struct avail_regs) { { 0, 0, 0}, 0, 0, 0 } + +/* Find suitable registers for a VFP Co-Processor Register Candidate (VFP CPRC + param) according to the rules described in the procedure call standard for + the ARM architecture (AAPCS). If found, the registers are assigned to this + VFP CPRC parameter. Registers are allocated in sequence unless a hole exists + and the parameter is a single float. + + avregs: opaque structure to keep track of available VFP co-processor regs + align: alignment constraints for the param, as returned by type_size() + size: size of the parameter, as returned by type_size() */ +int assign_vfpreg(struct avail_regs *avregs, int align, int size) +{ + int first_reg = 0; + + if (avregs->first_free_reg == -1) + return -1; + if (align >> 3) { /* double alignment */ + first_reg = avregs->first_free_reg; + /* alignment constraint not respected so use next reg and record hole */ + if (first_reg & 1) + avregs->avail[avregs->last_hole++] = first_reg++; + } else { /* no special alignment (float or array of float) */ + /* if single float and a hole is available, assign the param to it */ + if (size == 4 && avregs->first_hole != avregs->last_hole) + return avregs->avail[avregs->first_hole++]; + else + first_reg = avregs->first_free_reg; + } + if (first_reg + size / 4 <= 16) { + avregs->first_free_reg = first_reg + size / 4; + return first_reg; + } + avregs->first_free_reg = -1; + return -1; +} + +/* Returns whether all params need to be passed in core registers or not. + This is the case for function part of the runtime ABI. */ +int floats_in_core_regs(SValue *sval) +{ + if (!sval->sym) + return 0; + + switch (sval->sym->v) { + case TOK___floatundisf: + case TOK___floatundidf: + case TOK___fixunssfdi: + case TOK___fixunsdfdi: +#ifndef TCC_ARM_VFP + case TOK___fixunsxfdi: +#endif + case TOK___floatdisf: + case TOK___floatdidf: + case TOK___fixsfdi: + case TOK___fixdfdi: + return 1; + + default: + return 0; + } +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) { +#ifdef TCC_ARM_EABI + int size, align; + size = type_size(vt, &align); + if (float_abi == ARM_HARD_FLOAT && !variadic && + (is_float(vt->t) || is_hgen_float_aggr(vt))) { + *ret_align = 8; + *regsize = 8; + ret->ref = NULL; + ret->t = VT_DOUBLE; + return (size + 7) >> 3; + } else if (size <= 4) { + *ret_align = 4; + *regsize = 4; + ret->ref = NULL; + ret->t = VT_INT; + return 1; + } else + return 0; +#else + return 0; +#endif +} + +/* Parameters are classified according to how they are copied to their final + destination for the function call. Because the copying is performed class + after class according to the order in the union below, it is important that + some constraints about the order of the members of this union are respected: + - CORE_STRUCT_CLASS must come after STACK_CLASS; + - CORE_CLASS must come after STACK_CLASS, CORE_STRUCT_CLASS and + VFP_STRUCT_CLASS; + - VFP_STRUCT_CLASS must come after VFP_CLASS. + See the comment for the main loop in copy_params() for the reason. */ +enum reg_class { + STACK_CLASS = 0, + CORE_STRUCT_CLASS, + VFP_CLASS, + VFP_STRUCT_CLASS, + CORE_CLASS, + NB_CLASSES +}; + +struct param_plan { + int start; /* first reg or addr used depending on the class */ + int end; /* last reg used or next free addr depending on the class */ + SValue *sval; /* pointer to SValue on the value stack */ + struct param_plan *prev; /* previous element in this class */ +}; + +struct plan { + struct param_plan *pplans; /* array of all the param plans */ + struct param_plan *clsplans[NB_CLASSES]; /* per class lists of param plans */ +}; + +#define add_param_plan(plan,pplan,class) \ + do { \ + pplan.prev = plan->clsplans[class]; \ + plan->pplans[plan ## _nb] = pplan; \ + plan->clsplans[class] = &plan->pplans[plan ## _nb++]; \ + } while(0) + +/* Assign parameters to registers and stack with alignment according to the + rules in the procedure call standard for the ARM architecture (AAPCS). + The overall assignment is recorded in an array of per parameter structures + called parameter plans. The parameter plans are also further organized in a + number of linked lists, one per class of parameter (see the comment for the + definition of union reg_class). + + nb_args: number of parameters of the function for which a call is generated + float_abi: float ABI in use for this function call + plan: the structure where the overall assignment is recorded + todo: a bitmap that record which core registers hold a parameter + + Returns the amount of stack space needed for parameter passing + + Note: this function allocated an array in plan->pplans with tcc_malloc. It + is the responsibility of the caller to free this array once used (ie not + before copy_params). */ +static int assign_regs(int nb_args, int float_abi, struct plan *plan, int *todo) +{ + int i, size, align; + int ncrn /* next core register number */, nsaa /* next stacked argument address*/; + int plan_nb = 0; + struct param_plan pplan; + struct avail_regs avregs = AVAIL_REGS_INITIALIZER; + + ncrn = nsaa = 0; + *todo = 0; + plan->pplans = tcc_malloc(nb_args * sizeof(*plan->pplans)); + memset(plan->clsplans, 0, sizeof(plan->clsplans)); + for(i = nb_args; i-- ;) { + int j, start_vfpreg = 0; + CType type = vtop[-i].type; + type.t &= ~VT_ARRAY; + size = type_size(&type, &align); + size = (size + 3) & ~3; + align = (align + 3) & ~3; + switch(vtop[-i].type.t & VT_BTYPE) { + case VT_STRUCT: + case VT_FLOAT: + case VT_DOUBLE: + case VT_LDOUBLE: + if (float_abi == ARM_HARD_FLOAT) { + int is_hfa = 0; /* Homogeneous float aggregate */ + + if (is_float(vtop[-i].type.t) + || (is_hfa = is_hgen_float_aggr(&vtop[-i].type))) { + int end_vfpreg; + + start_vfpreg = assign_vfpreg(&avregs, align, size); + end_vfpreg = start_vfpreg + ((size - 1) >> 2); + if (start_vfpreg >= 0) { + pplan = (struct param_plan) {start_vfpreg, end_vfpreg, &vtop[-i]}; + if (is_hfa) + add_param_plan(plan, pplan, VFP_STRUCT_CLASS); + else + add_param_plan(plan, pplan, VFP_CLASS); + continue; + } else + break; + } + } + ncrn = (ncrn + (align-1)/4) & ~((align/4) - 1); + if (ncrn + size/4 <= 4 || (ncrn < 4 && start_vfpreg != -1)) { + /* The parameter is allocated both in core register and on stack. As + * such, it can be of either class: it would either be the last of + * CORE_STRUCT_CLASS or the first of STACK_CLASS. */ + for (j = ncrn; j < 4 && j < ncrn + size / 4; j++) + *todo|=(1< 4) + nsaa = (ncrn - 4) * 4; + } else { + ncrn = 4; + break; + } + continue; + default: + if (ncrn < 4) { + int is_long = (vtop[-i].type.t & VT_BTYPE) == VT_LLONG; + + if (is_long) { + ncrn = (ncrn + 1) & -2; + if (ncrn == 4) + break; + } + pplan = (struct param_plan) {ncrn, ncrn, &vtop[-i]}; + ncrn++; + if (is_long) + pplan.end = ncrn++; + add_param_plan(plan, pplan, CORE_CLASS); + continue; + } + } + nsaa = (nsaa + (align - 1)) & ~(align - 1); + pplan = (struct param_plan) {nsaa, nsaa + size, &vtop[-i]}; + add_param_plan(plan, pplan, STACK_CLASS); + nsaa += size; /* size already rounded up before */ + } + return nsaa; +} + +#undef add_param_plan + +/* Copy parameters to their final destination (core reg, VFP reg or stack) for + function call. + + nb_args: number of parameters the function take + plan: the overall assignment plan for parameters + todo: a bitmap indicating what core reg will hold a parameter + + Returns the number of SValue added by this function on the value stack */ +static int copy_params(int nb_args, struct plan *plan, int todo) +{ + int size, align, r, i, nb_extra_sval = 0; + struct param_plan *pplan; + int pass = 0; + + /* Several constraints require parameters to be copied in a specific order: + - structures are copied to the stack before being loaded in a reg; + - floats loaded to an odd numbered VFP reg are first copied to the + preceding even numbered VFP reg and then moved to the next VFP reg. + + It is thus important that: + - structures assigned to core regs must be copied after parameters + assigned to the stack but before structures assigned to VFP regs because + a structure can lie partly in core registers and partly on the stack; + - parameters assigned to the stack and all structures be copied before + parameters assigned to a core reg since copying a parameter to the stack + require using a core reg; + - parameters assigned to VFP regs be copied before structures assigned to + VFP regs as the copy might use an even numbered VFP reg that already + holds part of a structure. */ +again: + for(i = 0; i < NB_CLASSES; i++) { + for(pplan = plan->clsplans[i]; pplan; pplan = pplan->prev) { + + if (pass + && (i != CORE_CLASS || pplan->sval->r < VT_CONST)) + continue; + + vpushv(pplan->sval); + pplan->sval->r = pplan->sval->r2 = VT_CONST; /* disable entry */ + switch(i) { + case STACK_CLASS: + case CORE_STRUCT_CLASS: + case VFP_STRUCT_CLASS: + if ((pplan->sval->type.t & VT_BTYPE) == VT_STRUCT) { + int padding = 0; + size = type_size(&pplan->sval->type, &align); + /* align to stack align size */ + size = (size + 3) & ~3; + if (i == STACK_CLASS && pplan->prev) + padding = pplan->start - pplan->prev->end; + size += padding; /* Add padding if any */ + /* allocate the necessary size on stack */ + gadd_sp(-size); + /* generate structure store */ + r = get_reg(RC_INT); + o(0xE28D0000|(intr(r)<<12)|padding); /* add r, sp, padding */ + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); /* memcpy to current sp + potential padding */ + + /* Homogeneous float aggregate are loaded to VFP registers + immediately since there is no way of loading data in multiple + non consecutive VFP registers as what is done for other + structures (see the use of todo). */ + if (i == VFP_STRUCT_CLASS) { + int first = pplan->start, nb = pplan->end - first + 1; + /* vpop.32 {pplan->start, ..., pplan->end} */ + o(0xECBD0A00|(first&1)<<22|(first>>1)<<12|nb); + /* No need to write the register used to a SValue since VFP regs + cannot be used for gcall_or_jmp */ + } + } else { + if (is_float(pplan->sval->type.t)) { +#ifdef TCC_ARM_VFP + r = vfpr(gv(RC_FLOAT)) << 12; + if ((pplan->sval->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else { + size = 8; + r |= 0x101; /* vpush.32 -> vpush.64 */ + } + o(0xED2D0A01 + r); /* vpush */ +#else + r = fpr(gv(RC_FLOAT)) << 12; + if ((pplan->sval->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else if ((pplan->sval->type.t & VT_BTYPE) == VT_DOUBLE) + size = 8; + else + size = LDOUBLE_SIZE; + + if (size == 12) + r |= 0x400000; + else if(size == 8) + r|=0x8000; + + o(0xED2D0100|r|(size>>2)); /* some kind of vpush for FPA */ +#endif + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + size=4; + if ((pplan->sval->type.t & VT_BTYPE) == VT_LLONG) { + lexpand_nr(); + size = 8; + r = gv(RC_INT); + o(0xE52D0004|(intr(r)<<12)); /* push r */ + vtop--; + } + r = gv(RC_INT); + o(0xE52D0004|(intr(r)<<12)); /* push r */ + } + if (i == STACK_CLASS && pplan->prev) + gadd_sp(pplan->prev->end - pplan->start); /* Add padding if any */ + } + break; + + case VFP_CLASS: + gv(regmask(TREG_F0 + (pplan->start >> 1))); + if (pplan->start & 1) { /* Must be in upper part of double register */ + o(0xEEF00A40|((pplan->start>>1)<<12)|(pplan->start>>1)); /* vmov.f32 s(n+1), sn */ + vtop->r = VT_CONST; /* avoid being saved on stack by gv for next float */ + } + break; + + case CORE_CLASS: + if ((pplan->sval->type.t & VT_BTYPE) == VT_LLONG) { + lexpand_nr(); + gv(regmask(pplan->end)); + pplan->sval->r2 = vtop->r; + vtop--; + } + gv(regmask(pplan->start)); + /* Mark register as used so that gcall_or_jmp use another one + (regs >=4 are free as never used to pass parameters) */ + pplan->sval->r = vtop->r; + break; + } + vtop--; + } + } + + /* second pass to restore registers that were saved on stack by accident. + Maybe redundant after the "lvalue_save" patch in tccgen.c:gv() */ + if (++pass < 2) + goto again; + + /* Manually free remaining registers since next parameters are loaded + * manually, without the help of gv(int). */ + save_regs(nb_args); + + if(todo) { + o(0xE8BD0000|todo); /* pop {todo} */ + for(pplan = plan->clsplans[CORE_STRUCT_CLASS]; pplan; pplan = pplan->prev) { + int r; + pplan->sval->r = pplan->start; + /* An SValue can only pin 2 registers at best (r and r2) but a structure + can occupy more than 2 registers. Thus, we need to push on the value + stack some fake parameter to have on SValue for each registers used + by a structure (r2 is not used). */ + for (r = pplan->start + 1; r <= pplan->end; r++) { + if (todo & (1 << r)) { + nb_extra_sval++; + vpushi(0); + vtop->r = r; + } + } + } + } + return nb_extra_sval; +} + +/* Generate function call. The function address is pushed first, then + all the parameters in call order. This functions pops all the + parameters and the function address. */ +void gfunc_call(int nb_args) +{ + int r, args_size; + int def_float_abi = float_abi; + int todo; + struct plan plan; + +#ifdef TCC_ARM_EABI + int variadic; + + if (float_abi == ARM_HARD_FLOAT) { + variadic = (vtop[-nb_args].type.ref->f.func_type == FUNC_ELLIPSIS); + if (variadic || floats_in_core_regs(&vtop[-nb_args])) + float_abi = ARM_SOFTFP_FLOAT; + } +#endif + /* cannot let cpu flags if other instruction are generated. Also avoid leaving + VT_JMP anywhere except on the top of the stack because it would complicate + the code generator. */ + r = vtop->r & VT_VALMASK; + if (r == VT_CMP || (r & ~1) == VT_JMP) + gv(RC_INT); + + args_size = assign_regs(nb_args, float_abi, &plan, &todo); + +#ifdef TCC_ARM_EABI + if (args_size & 7) { /* Stack must be 8 byte aligned at fct call for EABI */ + args_size = (args_size + 7) & ~7; + o(0xE24DD004); /* sub sp, sp, #4 */ + } +#endif + + nb_args += copy_params(nb_args, &plan, todo); + tcc_free(plan.pplans); + + /* Move fct SValue on top as required by gcall_or_jmp */ + vrotb(nb_args + 1); + gcall_or_jmp(0); + if (args_size) + gadd_sp(args_size); /* pop all parameters passed on the stack */ +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) + if(float_abi == ARM_SOFTFP_FLOAT && is_float(vtop->type.ref->type.t)) { + if((vtop->type.ref->type.t & VT_BTYPE) == VT_FLOAT) { + o(0xEE000A10); /*vmov s0, r0 */ + } else { + o(0xEE000B10); /* vmov.32 d0[0], r0 */ + o(0xEE201B10); /* vmov.32 d0[1], r1 */ + } + } +#endif + vtop -= nb_args + 1; /* Pop all params and fct address from value stack */ + leaffunc = 0; /* we are calling a function, so we aren't in a leaf function */ + float_abi = def_float_abi; +} + +/* generate function prolog of type 't' */ +void gfunc_prolog(CType *func_type) +{ + Sym *sym,*sym2; + int n, nf, size, align, rs, struct_ret = 0; + int addr, pn, sn; /* pn=core, sn=stack */ + CType ret_type; + +#ifdef TCC_ARM_EABI + struct avail_regs avregs = AVAIL_REGS_INITIALIZER; +#endif + + sym = func_type->ref; + func_vt = sym->type; + func_var = (func_type->ref->f.func_type == FUNC_ELLIPSIS); + + n = nf = 0; + if ((func_vt.t & VT_BTYPE) == VT_STRUCT && + !gfunc_sret(&func_vt, func_var, &ret_type, &align, &rs)) + { + n++; + struct_ret = 1; + func_vc = 12; /* Offset from fp of the place to store the result */ + } + for(sym2 = sym->next; sym2 && (n < 4 || nf < 16); sym2 = sym2->next) { + size = type_size(&sym2->type, &align); +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT && !func_var && + (is_float(sym2->type.t) || is_hgen_float_aggr(&sym2->type))) { + int tmpnf = assign_vfpreg(&avregs, align, size); + tmpnf += (size + 3) / 4; + nf = (tmpnf > nf) ? tmpnf : nf; + } else +#endif + if (n < 4) + n += (size + 3) / 4; + } + o(0xE1A0C00D); /* mov ip,sp */ + if (func_var) + n=4; + if (n) { + if(n>4) + n=4; +#ifdef TCC_ARM_EABI + n=(n+1)&-2; +#endif + o(0xE92D0000|((1<16) + nf=16; + nf=(nf+1)&-2; /* nf => HARDFLOAT => EABI */ + o(0xED2D0A00|nf); /* save s0-s15 on stack if needed */ + } + o(0xE92D5800); /* save fp, ip, lr */ + o(0xE1A0B00D); /* mov fp, sp */ + func_sub_sp_offset = ind; + o(0xE1A00000); /* nop, leave space for stack adjustment in epilog */ + +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT) { + func_vc += nf * 4; + avregs = AVAIL_REGS_INITIALIZER; + } +#endif + pn = struct_ret, sn = 0; + while ((sym = sym->next)) { + CType *type; + type = &sym->type; + size = type_size(type, &align); + size = (size + 3) >> 2; + align = (align + 3) & ~3; +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT && !func_var && (is_float(sym->type.t) + || is_hgen_float_aggr(&sym->type))) { + int fpn = assign_vfpreg(&avregs, align, size << 2); + if (fpn >= 0) + addr = fpn * 4; + else + goto from_stack; + } else +#endif + if (pn < 4) { +#ifdef TCC_ARM_EABI + pn = (pn + (align-1)/4) & -(align/4); +#endif + addr = (nf + pn) * 4; + pn += size; + if (!sn && pn > 4) + sn = (pn - 4); + } else { +#ifdef TCC_ARM_EABI +from_stack: + sn = (sn + (align-1)/4) & -(align/4); +#endif + addr = (n + nf + sn) * 4; + sn += size; + } + sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), + addr + 12); + } + last_itod_magic=0; + leaffunc = 1; + loc = 0; +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + uint32_t x; + int diff; + /* Copy float return value to core register if base standard is used and + float computation is made with VFP */ +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) + if ((float_abi == ARM_SOFTFP_FLOAT || func_var) && is_float(func_vt.t)) { + if((func_vt.t & VT_BTYPE) == VT_FLOAT) + o(0xEE100A10); /* fmrs r0, s0 */ + else { + o(0xEE100B10); /* fmrdl r0, d0 */ + o(0xEE301B10); /* fmrdh r1, d0 */ + } + } +#endif + o(0xE89BA800); /* restore fp, sp, pc */ + diff = (-loc + 3) & -4; +#ifdef TCC_ARM_EABI + if(!leaffunc) + diff = ((diff + 11) & -8) - 4; +#endif + if(diff > 0) { + x=stuff_const(0xE24BD000, diff); /* sub sp,fp,# */ + if(x) + *(uint32_t *)(cur_text_section->data + func_sub_sp_offset) = x; + else { + int addr; + addr=ind; + o(0xE59FC004); /* ldr ip,[pc+4] */ + o(0xE04BD00C); /* sub sp,fp,ip */ + o(0xE1A0F00E); /* mov pc,lr */ + o(diff); + *(uint32_t *)(cur_text_section->data + func_sub_sp_offset) = 0xE1000000|encbranch(func_sub_sp_offset,addr,1); + } + } +} + +/* generate a jump to a label */ +int gjmp(int t) +{ + int r; + if (nocode_wanted) + return t; + r=ind; + o(0xE0000000|encbranch(r,t,1)); + return r; +} + +/* generate a jump to a fixed address */ +void gjmp_addr(int a) +{ + gjmp(a); +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +int gtst(int inv, int t) +{ + int v, r; + uint32_t op; + + v = vtop->r & VT_VALMASK; + r=ind; + + if (nocode_wanted) { + ; + } else if (v == VT_CMP) { + op=mapcc(inv?negcc(vtop->c.i):vtop->c.i); + op|=encbranch(r,t,1); + o(op); + t=r; + } else if (v == VT_JMP || v == VT_JMPI) { + if ((v & 1) == inv) { + if(!vtop->c.i) + vtop->c.i=t; + else { + uint32_t *x; + int p,lp; + if(t) { + p = vtop->c.i; + do { + p = decbranch(lp=p); + } while(p); + x = (uint32_t *)(cur_text_section->data + lp); + *x &= 0xff000000; + *x |= encbranch(lp,t,1); + } + t = vtop->c.i; + } + } else { + t = gjmp(t); + gsym(vtop->c.i); + } + } + vtop--; + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + int c, func = 0; + uint32_t opc = 0, r, fr; + unsigned short retreg = REG_IRET; + + c=0; + switch(op) { + case '+': + opc = 0x8; + c=1; + break; + case TOK_ADDC1: /* add with carry generation */ + opc = 0x9; + c=1; + break; + case '-': + opc = 0x4; + c=1; + break; + case TOK_SUBC1: /* sub with carry generation */ + opc = 0x5; + c=1; + break; + case TOK_ADDC2: /* add with carry use */ + opc = 0xA; + c=1; + break; + case TOK_SUBC2: /* sub with carry use */ + opc = 0xC; + c=1; + break; + case '&': + opc = 0x0; + c=1; + break; + case '^': + opc = 0x2; + c=1; + break; + case '|': + opc = 0x18; + c=1; + break; + case '*': + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + o(0xE0000090|(intr(r)<<16)|(intr(r)<<8)|intr(fr)); + return; + case TOK_SHL: + opc = 0; + c=2; + break; + case TOK_SHR: + opc = 1; + c=2; + break; + case TOK_SAR: + opc = 2; + c=2; + break; + case '/': + case TOK_PDIV: + func=TOK___divsi3; + c=3; + break; + case TOK_UDIV: + func=TOK___udivsi3; + c=3; + break; + case '%': +#ifdef TCC_ARM_EABI + func=TOK___aeabi_idivmod; + retreg=REG_LRET; +#else + func=TOK___modsi3; +#endif + c=3; + break; + case TOK_UMOD: +#ifdef TCC_ARM_EABI + func=TOK___aeabi_uidivmod; + retreg=REG_LRET; +#else + func=TOK___umodsi3; +#endif + c=3; + break; + case TOK_UMULL: + gv2(RC_INT, RC_INT); + r=intr(vtop[-1].r2=get_reg(RC_INT)); + c=vtop[-1].r; + vtop[-1].r=get_reg_ex(RC_INT,regmask(c)); + vtop--; + o(0xE0800090|(r<<16)|(intr(vtop->r)<<12)|(intr(c)<<8)|intr(vtop[1].r)); + return; + default: + opc = 0x15; + c=1; + break; + } + switch(c) { + case 1: + if((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + if(opc == 4 || opc == 5 || opc == 0xc) { + vswap(); + opc|=2; // sub -> rsb + } + } + if ((vtop->r & VT_VALMASK) == VT_CMP || + (vtop->r & (VT_VALMASK & ~1)) == VT_JMP) + gv(RC_INT); + vswap(); + c=intr(gv(RC_INT)); + vswap(); + opc=0xE0000000|(opc<<20)|(c<<16); + if((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + uint32_t x; + x=stuff_const(opc|0x2000000,vtop->c.i); + if(x) { + r=intr(vtop[-1].r=get_reg_ex(RC_INT,regmask(vtop[-1].r))); + o(x|(r<<12)); + goto done; + } + } + fr=intr(gv(RC_INT)); + r=intr(vtop[-1].r=get_reg_ex(RC_INT,two2mask(vtop->r,vtop[-1].r))); + o(opc|(r<<12)|fr); +done: + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) { + vtop->r = VT_CMP; + vtop->c.i = op; + } + break; + case 2: + opc=0xE1A00000|(opc<<5); + if ((vtop->r & VT_VALMASK) == VT_CMP || + (vtop->r & (VT_VALMASK & ~1)) == VT_JMP) + gv(RC_INT); + vswap(); + r=intr(gv(RC_INT)); + vswap(); + opc|=r; + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + fr=intr(vtop[-1].r=get_reg_ex(RC_INT,regmask(vtop[-1].r))); + c = vtop->c.i & 0x1f; + o(opc|(c<<7)|(fr<<12)); + } else { + fr=intr(gv(RC_INT)); + c=intr(vtop[-1].r=get_reg_ex(RC_INT,two2mask(vtop->r,vtop[-1].r))); + o(opc|(c<<12)|(fr<<8)|0x10); + } + vtop--; + break; + case 3: + vpush_global_sym(&func_old_type, func); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = retreg; + break; + default: + tcc_error("gen_opi %i unimplemented!",op); + } +} + +#ifdef TCC_ARM_VFP +static int is_zero(int i) +{ + if((vtop[i].r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + if (vtop[i].type.t == VT_FLOAT) + return (vtop[i].c.f == 0.f); + else if (vtop[i].type.t == VT_DOUBLE) + return (vtop[i].c.d == 0.0); + return (vtop[i].c.ld == 0.l); +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + * two operands are guaranteed to have the same floating point type */ +void gen_opf(int op) +{ + uint32_t x; + int fneg=0,r; + x=0xEE000A00|T2CPR(vtop->type.t); + switch(op) { + case '+': + if(is_zero(-1)) + vswap(); + if(is_zero(0)) { + vtop--; + return; + } + x|=0x300000; + break; + case '-': + x|=0x300040; + if(is_zero(0)) { + vtop--; + return; + } + if(is_zero(-1)) { + x|=0x810000; /* fsubX -> fnegX */ + vswap(); + vtop--; + fneg=1; + } + break; + case '*': + x|=0x200000; + break; + case '/': + x|=0x800000; + break; + default: + if(op < TOK_ULT || op > TOK_GT) { + tcc_error("unknown fp op %x!",op); + return; + } + if(is_zero(-1)) { + vswap(); + switch(op) { + case TOK_LT: op=TOK_GT; break; + case TOK_GE: op=TOK_ULE; break; + case TOK_LE: op=TOK_GE; break; + case TOK_GT: op=TOK_ULT; break; + } + } + x|=0xB40040; /* fcmpX */ + if(op!=TOK_EQ && op!=TOK_NE) + x|=0x80; /* fcmpX -> fcmpeX */ + if(is_zero(0)) { + vtop--; + o(x|0x10000|(vfpr(gv(RC_FLOAT))<<12)); /* fcmp(e)X -> fcmp(e)zX */ + } else { + x|=vfpr(gv(RC_FLOAT)); + vswap(); + o(x|(vfpr(gv(RC_FLOAT))<<12)); + vtop--; + } + o(0xEEF1FA10); /* fmstat */ + + switch(op) { + case TOK_LE: op=TOK_ULE; break; + case TOK_LT: op=TOK_ULT; break; + case TOK_UGE: op=TOK_GE; break; + case TOK_UGT: op=TOK_GT; break; + } + + vtop->r = VT_CMP; + vtop->c.i = op; + return; + } + r=gv(RC_FLOAT); + x|=vfpr(r); + r=regmask(r); + if(!fneg) { + int r2; + vswap(); + r2=gv(RC_FLOAT); + x|=vfpr(r2)<<16; + r|=regmask(r2); + } + vtop->r=get_reg_ex(RC_FLOAT,r); + if(!fneg) + vtop--; + o(x|(vfpr(vtop->r)<<12)); +} + +#else +static uint32_t is_fconst() +{ + long double f; + uint32_t r; + if((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + if (vtop->type.t == VT_FLOAT) + f = vtop->c.f; + else if (vtop->type.t == VT_DOUBLE) + f = vtop->c.d; + else + f = vtop->c.ld; + if(!ieee_finite(f)) + return 0; + r=0x8; + if(f<0.0) { + r=0x18; + f=-f; + } + if(f==0.0) + return r; + if(f==1.0) + return r|1; + if(f==2.0) + return r|2; + if(f==3.0) + return r|3; + if(f==4.0) + return r|4; + if(f==5.0) + return r|5; + if(f==0.5) + return r|6; + if(f==10.0) + return r|7; + return 0; +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +void gen_opf(int op) +{ + uint32_t x, r, r2, c1, c2; + //fputs("gen_opf\n",stderr); + vswap(); + c1 = is_fconst(); + vswap(); + c2 = is_fconst(); + x=0xEE000100; +#if LDOUBLE_SIZE == 8 + if ((vtop->type.t & VT_BTYPE) != VT_FLOAT) + x|=0x80; +#else + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) + x|=0x80; + else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) + x|=0x80000; +#endif + switch(op) + { + case '+': + if(!c2) { + vswap(); + c2=c1; + } + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + if(c2) { + if(c2>0xf) + x|=0x200000; // suf + r2=c2&0xf; + } else { + r2=fpr(gv(RC_FLOAT)); + } + break; + case '-': + if(c2) { + if(c2<=0xf) + x|=0x200000; // suf + r2=c2&0xf; + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } else if(c1 && c1<=0xf) { + x|=0x300000; // rsf + r2=c1; + r=fpr(gv(RC_FLOAT)); + vswap(); + } else { + x|=0x200000; // suf + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + r2=fpr(gv(RC_FLOAT)); + } + break; + case '*': + if(!c2 || c2>0xf) { + vswap(); + c2=c1; + } + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + if(c2 && c2<=0xf) + r2=c2; + else + r2=fpr(gv(RC_FLOAT)); + x|=0x100000; // muf + break; + case '/': + if(c2 && c2<=0xf) { + x|=0x400000; // dvf + r2=c2; + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } else if(c1 && c1<=0xf) { + x|=0x500000; // rdf + r2=c1; + r=fpr(gv(RC_FLOAT)); + vswap(); + } else { + x|=0x400000; // dvf + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + r2=fpr(gv(RC_FLOAT)); + } + break; + default: + if(op >= TOK_ULT && op <= TOK_GT) { + x|=0xd0f110; // cmfe +/* bug (intention?) in Linux FPU emulator + doesn't set carry if equal */ + switch(op) { + case TOK_ULT: + case TOK_UGE: + case TOK_ULE: + case TOK_UGT: + tcc_error("unsigned comparison on floats?"); + break; + case TOK_LT: + op=TOK_Nset; + break; + case TOK_LE: + op=TOK_ULE; /* correct in unordered case only if AC bit in FPSR set */ + break; + case TOK_EQ: + case TOK_NE: + x&=~0x400000; // cmfe -> cmf + break; + } + if(c1 && !c2) { + c2=c1; + vswap(); + switch(op) { + case TOK_Nset: + op=TOK_GT; + break; + case TOK_GE: + op=TOK_ULE; + break; + case TOK_ULE: + op=TOK_GE; + break; + case TOK_GT: + op=TOK_Nset; + break; + } + } + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + if(c2) { + if(c2>0xf) + x|=0x200000; + r2=c2&0xf; + } else { + r2=fpr(gv(RC_FLOAT)); + } + vtop[-1].r = VT_CMP; + vtop[-1].c.i = op; + } else { + tcc_error("unknown fp op %x!",op); + return; + } + } + if(vtop[-1].r == VT_CMP) + c1=15; + else { + c1=vtop->r; + if(r2&0x8) + c1=vtop[-1].r; + vtop[-1].r=get_reg_ex(RC_FLOAT,two2mask(vtop[-1].r,c1)); + c1=fpr(vtop[-1].r); + } + vtop--; + o(x|(r<<16)|(c1<<12)|r2); +} +#endif + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +ST_FUNC void gen_cvt_itof1(int t) +{ + uint32_t r, r2; + int bt; + bt=vtop->type.t & VT_BTYPE; + if(bt == VT_INT || bt == VT_SHORT || bt == VT_BYTE) { +#ifndef TCC_ARM_VFP + uint32_t dsize = 0; +#endif + r=intr(gv(RC_INT)); +#ifdef TCC_ARM_VFP + r2=vfpr(vtop->r=get_reg(RC_FLOAT)); + o(0xEE000A10|(r<<12)|(r2<<16)); /* fmsr */ + r2|=r2<<12; + if(!(vtop->type.t & VT_UNSIGNED)) + r2|=0x80; /* fuitoX -> fsituX */ + o(0xEEB80A40|r2|T2CPR(t)); /* fYitoX*/ +#else + r2=fpr(vtop->r=get_reg(RC_FLOAT)); + if((t & VT_BTYPE) != VT_FLOAT) + dsize=0x80; /* flts -> fltd */ + o(0xEE000110|dsize|(r2<<16)|(r<<12)); /* flts */ + if((vtop->type.t & (VT_UNSIGNED|VT_BTYPE)) == (VT_UNSIGNED|VT_INT)) { + uint32_t off = 0; + o(0xE3500000|(r<<12)); /* cmp */ + r=fpr(get_reg(RC_FLOAT)); + if(last_itod_magic) { + off=ind+8-last_itod_magic; + off/=4; + if(off>255) + off=0; + } + o(0xBD1F0100|(r<<12)|off); /* ldflts */ + if(!off) { + o(0xEA000000); /* b */ + last_itod_magic=ind; + o(0x4F800000); /* 4294967296.0f */ + } + o(0xBE000100|dsize|(r2<<16)|(r2<<12)|r); /* adflt */ + } +#endif + return; + } else if(bt == VT_LLONG) { + int func; + CType *func_type = 0; + if((t & VT_BTYPE) == VT_FLOAT) { + func_type = &func_float_type; + if(vtop->type.t & VT_UNSIGNED) + func=TOK___floatundisf; + else + func=TOK___floatdisf; +#if LDOUBLE_SIZE != 8 + } else if((t & VT_BTYPE) == VT_LDOUBLE) { + func_type = &func_ldouble_type; + if(vtop->type.t & VT_UNSIGNED) + func=TOK___floatundixf; + else + func=TOK___floatdixf; + } else if((t & VT_BTYPE) == VT_DOUBLE) { +#else + } else if((t & VT_BTYPE) == VT_DOUBLE || (t & VT_BTYPE) == VT_LDOUBLE) { +#endif + func_type = &func_double_type; + if(vtop->type.t & VT_UNSIGNED) + func=TOK___floatundidf; + else + func=TOK___floatdidf; + } + if(func_type) { + vpush_global_sym(func_type, func); + vswap(); + gfunc_call(1); + vpushi(0); + vtop->r=TREG_F0; + return; + } + } + tcc_error("unimplemented gen_cvt_itof %x!",vtop->type.t); +} + +/* convert fp to int 't' type */ +void gen_cvt_ftoi(int t) +{ + uint32_t r, r2; + int u, func = 0; + u=t&VT_UNSIGNED; + t&=VT_BTYPE; + r2=vtop->type.t & VT_BTYPE; + if(t==VT_INT) { +#ifdef TCC_ARM_VFP + r=vfpr(gv(RC_FLOAT)); + u=u?0:0x10000; + o(0xEEBC0AC0|(r<<12)|r|T2CPR(r2)|u); /* ftoXizY */ + r2=intr(vtop->r=get_reg(RC_INT)); + o(0xEE100A10|(r<<16)|(r2<<12)); + return; +#else + if(u) { + if(r2 == VT_FLOAT) + func=TOK___fixunssfsi; +#if LDOUBLE_SIZE != 8 + else if(r2 == VT_LDOUBLE) + func=TOK___fixunsxfsi; + else if(r2 == VT_DOUBLE) +#else + else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE) +#endif + func=TOK___fixunsdfsi; + } else { + r=fpr(gv(RC_FLOAT)); + r2=intr(vtop->r=get_reg(RC_INT)); + o(0xEE100170|(r2<<12)|r); + return; + } +#endif + } else if(t == VT_LLONG) { // unsigned handled in gen_cvt_ftoi1 + if(r2 == VT_FLOAT) + func=TOK___fixsfdi; +#if LDOUBLE_SIZE != 8 + else if(r2 == VT_LDOUBLE) + func=TOK___fixxfdi; + else if(r2 == VT_DOUBLE) +#else + else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE) +#endif + func=TOK___fixdfdi; + } + if(func) { + vpush_global_sym(&func_old_type, func); + vswap(); + gfunc_call(1); + vpushi(0); + if(t == VT_LLONG) + vtop->r2 = REG_LRET; + vtop->r = REG_IRET; + return; + } + tcc_error("unimplemented gen_cvt_ftoi!"); +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ +#ifdef TCC_ARM_VFP + if(((vtop->type.t & VT_BTYPE) == VT_FLOAT) != ((t & VT_BTYPE) == VT_FLOAT)) { + uint32_t r = vfpr(gv(RC_FLOAT)); + o(0xEEB70AC0|(r<<12)|r|T2CPR(vtop->type.t)); + } +#else + /* all we have to do on i386 and FPA ARM is to put the float in a register */ + gv(RC_FLOAT); +#endif +} + +/* computed goto support */ +void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* Save the stack pointer onto the stack and return the location of its address */ +ST_FUNC void gen_vla_sp_save(int addr) { + SValue v; + v.type.t = VT_PTR; + v.r = VT_LOCAL | VT_LVAL; + v.c.i = addr; + store(TREG_SP, &v); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + SValue v; + v.type.t = VT_PTR; + v.r = VT_LOCAL | VT_LVAL; + v.c.i = addr; + load(TREG_SP, &v); +} + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { + int r = intr(gv(RC_INT)); + o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */ +#ifdef TCC_ARM_EABI + if (align < 8) + align = 8; +#else + if (align < 4) + align = 4; +#endif + if (align & (align - 1)) + tcc_error("alignment is not a power of 2: %i", align); + o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */ + vpop(); +} + +/* end of ARM code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final-old/arm-link.c b/05/tcc-final-old/arm-link.c new file mode 100644 index 0000000..92a24eb --- /dev/null +++ b/05/tcc-final-old/arm-link.c @@ -0,0 +1,398 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_ARM + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_ARM_ABS32 +#define R_DATA_PTR R_ARM_ABS32 +#define R_JMP_SLOT R_ARM_JUMP_SLOT +#define R_GLOB_DAT R_ARM_GLOB_DAT +#define R_COPY R_ARM_COPY +#define R_RELATIVE R_ARM_RELATIVE + +#define R_NUM R_ARM_NUM + +#define ELF_START_ADDR 0x00008000 +#define ELF_PAGE_SIZE 0x1000 + +#define PCRELATIVE_DLLPLT 1 +#define RELOCATE_DLLPLT 0 + +enum float_abi { + ARM_SOFTFP_FLOAT, + ARM_HARD_FLOAT, +}; + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_ARM_MOVT_ABS: + case R_ARM_MOVW_ABS_NC: + case R_ARM_THM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + case R_ARM_ABS32: + case R_ARM_REL32: + case R_ARM_GOTPC: + case R_ARM_GOTOFF: + case R_ARM_GOT32: + case R_ARM_COPY: + case R_ARM_GLOB_DAT: + case R_ARM_NONE: + return 0; + + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_PLT32: + case R_ARM_THM_PC22: + case R_ARM_THM_JUMP24: + case R_ARM_PREL31: + case R_ARM_V4BX: + case R_ARM_JUMP_SLOT: + return 1; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_ARM_NONE: + case R_ARM_COPY: + case R_ARM_GLOB_DAT: + case R_ARM_JUMP_SLOT: + return NO_GOTPLT_ENTRY; + + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_PLT32: + case R_ARM_THM_PC22: + case R_ARM_THM_JUMP24: + case R_ARM_MOVT_ABS: + case R_ARM_MOVW_ABS_NC: + case R_ARM_THM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + case R_ARM_PREL31: + case R_ARM_ABS32: + case R_ARM_REL32: + case R_ARM_V4BX: + return AUTO_GOTPLT_ENTRY; + + case R_ARM_GOTPC: + case R_ARM_GOTOFF: + return BUILD_GOT_ONLY; + + case R_ARM_GOT32: + return ALWAYS_GOTPLT_ENTRY; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + unsigned plt_offset; + + /* when building a DLL, GOT entry accesses must be done relative to + start of GOT (see x86_64 example above) */ + if (s1->output_type == TCC_OUTPUT_DLL) + tcc_error("DLLs unimplemented!"); + + /* empty PLT: create PLT0 entry that push address of call site and + jump to ld.so resolution routine (GOT + 8) */ + if (plt->data_offset == 0) { + p = section_ptr_add(plt, 20); + write32le(p, 0xe52de004); /* push {lr} */ + write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */ + write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */ + write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */ + /* p+16 is set in relocate_plt */ + } + plt_offset = plt->data_offset; + + if (attr->plt_thumb_stub) { + p = section_ptr_add(plt, 4); + write32le(p, 0x4778); /* bx pc */ + write32le(p+2, 0x46c0); /* nop */ + } + p = section_ptr_add(plt, 16); + /* Jump to GOT entry where ld.so initially put address of PLT0 */ + write32le(p, 0xe59fc004); /* ldr ip, [pc, #4] */ + write32le(p+4, 0xe08fc00c); /* add ip, pc, ip */ + write32le(p+8, 0xe59cf000); /* ldr pc, [ip] */ + /* p + 12 contains offset to GOT entry once patched by relocate_plt */ + write32le(p+12, got_offset); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + int x = s1->got->sh_addr - s1->plt->sh_addr - 12; + write32le(s1->plt->data + 16, x - 16); + p += 20; + while (p < p_end) { + if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */ + p += 4; + add32le(p + 12, x + s1->plt->data - p); + p += 16; + } + } +} + +void relocate_init(Section *sr) {} + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + ElfW(Sym) *sym; + int sym_index; + + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + + switch(type) { + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_PLT32: + { + int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko; + x = (*(int *) ptr) & 0xffffff; +#ifdef DEBUG_RELOC + printf ("reloc %d: x=0x%x val=0x%x ", type, x, val); +#endif + (*(int *)ptr) &= 0xff000000; + if (x & 0x800000) + x -= 0x1000000; + x <<= 2; + blx_avail = (TCC_CPU_VERSION >= 5); + is_thumb = val & 1; + is_bl = (*(unsigned *) ptr) >> 24 == 0xeb; + is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl)); + x += val - addr; +#ifdef DEBUG_RELOC + printf (" newx=0x%x name=%s\n", x, + (char *) symtab_section->link->data + sym->st_name); +#endif + h = x & 2; + th_ko = (x & 3) && (!blx_avail || !is_call); + if (th_ko || x >= 0x2000000 || x < -0x2000000) + tcc_error("can't relocate value at %x,%d",addr, type); + x >>= 2; + x &= 0xffffff; + /* Only reached if blx is avail and it is a call */ + if (is_thumb) { + x |= h << 24; + (*(int *)ptr) = 0xfa << 24; /* bl -> blx */ + } + (*(int *) ptr) |= x; + } + return; + /* Since these relocations only concern Thumb-2 and blx instruction was + introduced before Thumb-2, we can assume blx is available and not + guard its use */ + case R_ARM_THM_PC22: + case R_ARM_THM_JUMP24: + { + int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11; + int to_thumb, is_call, to_plt, blx_bit = 1 << 12; + Section *plt; + + /* weak reference */ + if (sym->st_shndx == SHN_UNDEF && + ELFW(ST_BIND)(sym->st_info) == STB_WEAK) + return; + + /* Get initial offset */ + hi = (*(uint16_t *)ptr); + lo = (*(uint16_t *)(ptr+2)); + s = (hi >> 10) & 1; + j1 = (lo >> 13) & 1; + j2 = (lo >> 11) & 1; + i1 = (j1 ^ s) ^ 1; + i2 = (j2 ^ s) ^ 1; + imm10 = hi & 0x3ff; + imm11 = lo & 0x7ff; + x = (s << 24) | (i1 << 23) | (i2 << 22) | + (imm10 << 12) | (imm11 << 1); + if (x & 0x01000000) + x -= 0x02000000; + + /* Relocation infos */ + to_thumb = val & 1; + plt = s1->plt; + to_plt = (val >= plt->sh_addr) && + (val < plt->sh_addr + plt->data_offset); + is_call = (type == R_ARM_THM_PC22); + + if (!to_thumb && !to_plt && !is_call) { + int index; + uint8_t *p; + char *name, buf[1024]; + Section *text_section; + + name = (char *) symtab_section->link->data + sym->st_name; + text_section = s1->sections[sym->st_shndx]; + /* Modify reloc to target a thumb stub to switch to ARM */ + snprintf(buf, sizeof(buf), "%s_from_thumb", name); + index = put_elf_sym(symtab_section, + text_section->data_offset + 1, + sym->st_size, sym->st_info, 0, + sym->st_shndx, buf); + to_thumb = 1; + val = text_section->data_offset + 1; + rel->r_info = ELFW(R_INFO)(index, type); + /* Create a thumb stub function to switch to ARM mode */ + put_elf_reloc(symtab_section, text_section, + text_section->data_offset + 4, R_ARM_JUMP24, + sym_index); + p = section_ptr_add(text_section, 8); + write32le(p, 0x4778); /* bx pc */ + write32le(p+2, 0x46c0); /* nop */ + write32le(p+4, 0xeafffffe); /* b $sym */ + } + + /* Compute final offset */ + x += val - addr; + if (!to_thumb && is_call) { + blx_bit = 0; /* bl -> blx */ + x = (x + 3) & -4; /* Compute offset from aligned PC */ + } + + /* Check that relocation is possible + * offset must not be out of range + * if target is to be entered in arm mode: + - bit 1 must not set + - instruction must be a call (bl) or a jump to PLT */ + if (!to_thumb || x >= 0x1000000 || x < -0x1000000) + if (to_thumb || (val & 2) || (!is_call && !to_plt)) + tcc_error("can't relocate value at %x,%d",addr, type); + + /* Compute and store final offset */ + s = (x >> 24) & 1; + i1 = (x >> 23) & 1; + i2 = (x >> 22) & 1; + j1 = s ^ (i1 ^ 1); + j2 = s ^ (i2 ^ 1); + imm10 = (x >> 12) & 0x3ff; + imm11 = (x >> 1) & 0x7ff; + (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) | + (s << 10) | imm10); + (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) | + (j1 << 13) | blx_bit | (j2 << 11) | + imm11); + } + return; + case R_ARM_MOVT_ABS: + case R_ARM_MOVW_ABS_NC: + { + int x, imm4, imm12; + if (type == R_ARM_MOVT_ABS) + val >>= 16; + imm12 = val & 0xfff; + imm4 = (val >> 12) & 0xf; + x = (imm4 << 16) | imm12; + if (type == R_ARM_THM_MOVT_ABS) + *(int *)ptr |= x; + else + *(int *)ptr += x; + } + return; + case R_ARM_THM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + { + int x, i, imm4, imm3, imm8; + if (type == R_ARM_THM_MOVT_ABS) + val >>= 16; + imm8 = val & 0xff; + imm3 = (val >> 8) & 0x7; + i = (val >> 11) & 1; + imm4 = (val >> 12) & 0xf; + x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4; + if (type == R_ARM_THM_MOVT_ABS) + *(int *)ptr |= x; + else + *(int *)ptr += x; + } + return; + case R_ARM_PREL31: + { + int x; + x = (*(int *)ptr) & 0x7fffffff; + (*(int *)ptr) &= 0x80000000; + x = (x * 2) / 2; + x += val - addr; + if((x^(x>>1))&0x40000000) + tcc_error("can't relocate value at %x,%d",addr, type); + (*(int *)ptr) |= x & 0x7fffffff; + } + case R_ARM_ABS32: + *(int *)ptr += val; + return; + case R_ARM_REL32: + *(int *)ptr += val - addr; + return; + case R_ARM_GOTPC: + *(int *)ptr += s1->got->sh_addr - addr; + return; + case R_ARM_GOTOFF: + *(int *)ptr += val - s1->got->sh_addr; + return; + case R_ARM_GOT32: + /* we load the got offset */ + *(int *)ptr += s1->sym_attrs[sym_index].got_offset; + return; + case R_ARM_COPY: + return; + case R_ARM_V4BX: + /* trade Thumb support for ARMv4 support */ + if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10) + *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */ + return; + case R_ARM_GLOB_DAT: + case R_ARM_JUMP_SLOT: + *(addr_t *)ptr = val; + return; + case R_ARM_NONE: + /* Nothing to do. Normally used to indicate a dependency + on a certain symbol (like for exception handling under EABI). */ + return; + case R_ARM_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + return; + default: + fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n", + type, (unsigned)addr, ptr, (unsigned)val); + return; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final-old/arm64-gen.c b/05/tcc-final-old/arm64-gen.c new file mode 100644 index 0000000..86b3af7 --- /dev/null +++ b/05/tcc-final-old/arm64-gen.c @@ -0,0 +1,1837 @@ +/* + * A64 code generator for TCC + * + * Copyright (c) 2014-2015 Edmund Grimley Evans + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. This file is offered as-is, + * without any warranty. + */ + +#ifdef TARGET_DEFS_ONLY + +// Number of registers available to allocator: +#define NB_REGS 28 // x0-x18, x30, v0-v7 + +#define TREG_R(x) (x) // x = 0..18 +#define TREG_R30 19 +#define TREG_F(x) (x + 20) // x = 0..7 + +// Register classes sorted from more general to more precise: +#define RC_INT (1 << 0) +#define RC_FLOAT (1 << 1) +#define RC_R(x) (1 << (2 + (x))) // x = 0..18 +#define RC_R30 (1 << 21) +#define RC_F(x) (1 << (22 + (x))) // x = 0..7 + +#define RC_IRET (RC_R(0)) // int return register class +#define RC_FRET (RC_F(0)) // float return register class + +#define REG_IRET (TREG_R(0)) // int return register number +#define REG_FRET (TREG_F(0)) // float return register number + +#define PTR_SIZE 8 + +#define LDOUBLE_SIZE 16 +#define LDOUBLE_ALIGN 16 + +#define MAX_ALIGN 16 + +#define CHAR_IS_UNSIGNED + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#include "tcc.h" +#include + +ST_DATA const int reg_classes[NB_REGS] = { + RC_INT | RC_R(0), + RC_INT | RC_R(1), + RC_INT | RC_R(2), + RC_INT | RC_R(3), + RC_INT | RC_R(4), + RC_INT | RC_R(5), + RC_INT | RC_R(6), + RC_INT | RC_R(7), + RC_INT | RC_R(8), + RC_INT | RC_R(9), + RC_INT | RC_R(10), + RC_INT | RC_R(11), + RC_INT | RC_R(12), + RC_INT | RC_R(13), + RC_INT | RC_R(14), + RC_INT | RC_R(15), + RC_INT | RC_R(16), + RC_INT | RC_R(17), + RC_INT | RC_R(18), + RC_R30, // not in RC_INT as we make special use of x30 + RC_FLOAT | RC_F(0), + RC_FLOAT | RC_F(1), + RC_FLOAT | RC_F(2), + RC_FLOAT | RC_F(3), + RC_FLOAT | RC_F(4), + RC_FLOAT | RC_F(5), + RC_FLOAT | RC_F(6), + RC_FLOAT | RC_F(7) +}; + +#define IS_FREG(x) ((x) >= TREG_F(0)) + +static uint32_t intr(int r) +{ + assert(TREG_R(0) <= r && r <= TREG_R30); + return r < TREG_R30 ? r : 30; +} + +static uint32_t fltr(int r) +{ + assert(TREG_F(0) <= r && r <= TREG_F(7)); + return r - TREG_F(0); +} + +// Add an instruction to text section: +ST_FUNC void o(unsigned int c) +{ + int ind1 = ind + 4; + if (nocode_wanted) + return; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + write32le(cur_text_section->data + ind, c); + ind = ind1; +} + +static int arm64_encode_bimm64(uint64_t x) +{ + int neg = x & 1; + int rep, pos, len; + + if (neg) + x = ~x; + if (!x) + return -1; + + if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1))) + rep = 2, x &= ((uint64_t)1 << 2) - 1; + else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1))) + rep = 4, x &= ((uint64_t)1 << 4) - 1; + else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1))) + rep = 8, x &= ((uint64_t)1 << 8) - 1; + else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1))) + rep = 16, x &= ((uint64_t)1 << 16) - 1; + else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1))) + rep = 32, x &= ((uint64_t)1 << 32) - 1; + else + rep = 64; + + pos = 0; + if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32; + if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16; + if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8; + if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4; + if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2; + if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1; + + len = 0; + if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32; + if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16; + if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8; + if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4; + if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2; + if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1; + + if (x) + return -1; + if (neg) { + pos = (pos + len) & (rep - 1); + len = rep - len; + } + return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) | + ((rep - pos) & (rep - 1)) << 6 | (len - 1)); +} + +static uint32_t arm64_movi(int r, uint64_t x) +{ + uint64_t m = 0xffff; + int e; + if (!(x & ~m)) + return 0x52800000 | r | x << 5; // movz w(r),#(x) + if (!(x & ~(m << 16))) + return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16 + if (!(x & ~(m << 32))) + return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32 + if (!(x & ~(m << 48))) + return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48 + if ((x & ~m) == m << 16) + return (0x12800000 | r | + (~x << 5 & 0x1fffe0)); // movn w(r),#(~x) + if ((x & ~(m << 16)) == m) + return (0x12a00000 | r | + (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16 + if (!~(x | m)) + return (0x92800000 | r | + (~x << 5 & 0x1fffe0)); // movn x(r),#(~x) + if (!~(x | m << 16)) + return (0x92a00000 | r | + (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16 + if (!~(x | m << 32)) + return (0x92c00000 | r | + (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32 + if (!~(x | m << 48)) + return (0x92e00000 | r | + (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32 + if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0) + return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x) + if ((e = arm64_encode_bimm64(x)) >= 0) + return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x) + return 0; +} + +static void arm64_movimm(int r, uint64_t x) +{ + uint32_t i; + if ((i = arm64_movi(r, x))) + o(i); // a single MOV + else { + // MOVZ/MOVN and 1-3 MOVKs + int z = 0, m = 0; + uint32_t mov1 = 0xd2800000; // movz + uint64_t x1 = x; + for (i = 0; i < 64; i += 16) { + z += !(x >> i & 0xffff); + m += !(~x >> i & 0xffff); + } + if (m > z) { + x1 = ~x; + mov1 = 0x92800000; // movn + } + for (i = 0; i < 64; i += 16) + if (x1 >> i & 0xffff) { + o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17); + // movz/movn x(r),#(*),lsl #(i) + break; + } + for (i += 16; i < 64; i += 16) + if (x1 >> i & 0xffff) + o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17); + // movk x(r),#(*),lsl #(i) + } +} + +// Patch all branches in list pointed to by t to branch to a: +ST_FUNC void gsym_addr(int t_, int a_) +{ + uint32_t t = t_; + uint32_t a = a_; + while (t) { + unsigned char *ptr = cur_text_section->data + t; + uint32_t next = read32le(ptr); + if (a - t + 0x8000000 >= 0x10000000) + tcc_error("branch out of range"); + write32le(ptr, (a - t == 4 ? 0xd503201f : // nop + 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b + t = next; + } +} + +// Patch all branches in list pointed to by t to branch to current location: +ST_FUNC void gsym(int t) +{ + gsym_addr(t, ind); +} + +static int arm64_type_size(int t) +{ + switch (t & VT_BTYPE) { + case VT_INT: return 2; + case VT_BYTE: return 0; + case VT_SHORT: return 1; + case VT_PTR: return 3; + case VT_FUNC: return 3; + case VT_FLOAT: return 2; + case VT_DOUBLE: return 3; + case VT_LDOUBLE: return 4; + case VT_BOOL: return 0; + case VT_LLONG: return 3; + } + assert(0); + return 0; +} + +static void arm64_spoff(int reg, uint64_t off) +{ + uint32_t sub = off >> 63; + if (sub) + off = -off; + if (off < 4096) + o(0x910003e0 | sub << 30 | reg | off << 10); + // (add|sub) x(reg),sp,#(off) + else { + arm64_movimm(30, off); // use x30 for offset + o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30 + } +} + +static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (sz >= 2) + sg = 0; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x39400000 | dst | bas << 5 | off << (10 - sz) | + (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)] + else if (off < 256 || -off <= 256) + o(0x38400000 | dst | bas << 5 | (off & 511) << 12 | + (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | + (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30] + } +} + +static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x3d400000 | dst | bas << 5 | off << (10 - sz) | + (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)] + else if (off < 256 || -off <= 256) + o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 | + (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 | + sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30] + } +} + +static void arm64_ldrs(int reg_, int size) +{ + uint32_t reg = reg_; + // Use x30 for intermediate value in some cases. + switch (size) { + default: assert(0); break; + case 1: + arm64_ldrx(0, 0, reg, reg, 0); + break; + case 2: + arm64_ldrx(0, 1, reg, reg, 0); + break; + case 3: + arm64_ldrx(0, 1, 30, reg, 0); + arm64_ldrx(0, 0, reg, reg, 2); + o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16 + break; + case 4: + arm64_ldrx(0, 2, reg, reg, 0); + break; + case 5: + arm64_ldrx(0, 2, 30, reg, 0); + arm64_ldrx(0, 0, reg, reg, 4); + o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32 + break; + case 6: + arm64_ldrx(0, 2, 30, reg, 0); + arm64_ldrx(0, 1, reg, reg, 4); + o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32 + break; + case 7: + arm64_ldrx(0, 2, 30, reg, 0); + arm64_ldrx(0, 2, reg, reg, 3); + o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8 + o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32 + break; + case 8: + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 9: + arm64_ldrx(0, 0, reg + 1, reg, 8); + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 10: + arm64_ldrx(0, 1, reg + 1, reg, 8); + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 11: + arm64_ldrx(0, 2, reg + 1, reg, 7); + o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 12: + arm64_ldrx(0, 2, reg + 1, reg, 8); + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 13: + arm64_ldrx(0, 3, reg + 1, reg, 5); + o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 14: + arm64_ldrx(0, 3, reg + 1, reg, 6); + o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 15: + arm64_ldrx(0, 3, reg + 1, reg, 7); + o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 16: + o(0xa9400000 | reg | (reg+1) << 10 | reg << 5); + // ldp x(reg),x(reg+1),[x(reg)] + break; + } +} + +static void arm64_strx(int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30); + // str(*) x(dst),[x(bas],#(off)] + else if (off < 256 || -off <= 256) + o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30); + // stur(*) x(dst),[x(bas],#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30); + // str(*) x(dst),[x(bas),x30] + } +} + +static void arm64_strv(int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x3d000000 | dst | bas << 5 | off << (10 - sz) | + (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)] + else if (off < 256 || -off <= 256) + o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 | + (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 | + sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30] + } +} + +static void arm64_sym(int r, Sym *sym, unsigned long addend) +{ + // Currently TCC's linker does not generate COPY relocations for + // STT_OBJECTs when tcc is invoked with "-run". This typically + // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when + // a program refers to stdin. A workaround is to avoid that + // relocation and use only relocations with unlimited range. + int avoid_adrp = 1; + + if (avoid_adrp || sym->a.weak) { + // (GCC uses a R_AARCH64_ABS64 in this case.) + greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend); + o(0xd2800000 | r); // mov x(rt),#0,lsl #0 + greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend); + o(0xf2a00000 | r); // movk x(rt),#0,lsl #16 + greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend); + o(0xf2c00000 | r); // movk x(rt),#0,lsl #32 + greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend); + o(0xf2e00000 | r); // movk x(rt),#0,lsl #48 + } + else { + greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend); + o(0x90000000 | r); + greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend); + o(0x91000000 | r | r << 5); + } +} + +ST_FUNC void load(int r, SValue *sv) +{ + int svtt = sv->type.t; + int svr = sv->r & ~VT_LVAL_TYPE; + int svrv = svr & VT_VALMASK; + uint64_t svcul = (uint32_t)sv->c.i; + svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul; + + if (svr == (VT_LOCAL | VT_LVAL)) { + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), 29, svcul); + return; + } + + if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) { + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), intr(svrv), 0); + return; + } + + if (svr == (VT_CONST | VT_LVAL | VT_SYM)) { + arm64_sym(30, sv->sym, svcul); // use x30 for address + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), 30, 0); + return; + } + + if (svr == (VT_CONST | VT_SYM)) { + arm64_sym(intr(r), sv->sym, svcul); + return; + } + + if (svr == VT_CONST) { + if ((svtt & VT_BTYPE) != VT_VOID) + arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ? + sv->c.i : (uint32_t)svcul); + return; + } + + if (svr < VT_CONST) { + if (IS_FREG(r) && IS_FREG(svr)) + if (svtt == VT_LDOUBLE) + o(0x4ea01c00 | fltr(r) | fltr(svr) << 5); + // mov v(r).16b,v(svr).16b + else + o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr) + else if (!IS_FREG(r) && !IS_FREG(svr)) + o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr) + else + assert(0); + return; + } + + if (svr == VT_LOCAL) { + if (-svcul < 0x1000) + o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#... + else { + arm64_movimm(30, -svcul); // use x30 for offset + o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30 + } + return; + } + + if (svr == VT_JMP || svr == VT_JMPI) { + int t = (svr == VT_JMPI); + arm64_movimm(intr(r), t); + o(0x14000002); // b .+8 + gsym(svcul); + arm64_movimm(intr(r), t ^ 1); + return; + } + + if (svr == (VT_LLOCAL | VT_LVAL)) { + arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), 30, 0); + return; + } + + printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul); + assert(0); +} + +ST_FUNC void store(int r, SValue *sv) +{ + int svtt = sv->type.t; + int svr = sv->r & ~VT_LVAL_TYPE; + int svrv = svr & VT_VALMASK; + uint64_t svcul = (uint32_t)sv->c.i; + svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul; + + if (svr == (VT_LOCAL | VT_LVAL)) { + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul); + else + arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul); + return; + } + + if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) { + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0); + else + arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0); + return; + } + + if (svr == (VT_CONST | VT_LVAL | VT_SYM)) { + arm64_sym(30, sv->sym, svcul); // use x30 for address + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0); + else + arm64_strx(arm64_type_size(svtt), intr(r), 30, 0); + return; + } + + printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul); + assert(0); +} + +static void arm64_gen_bl_or_b(int b) +{ + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + assert(!b && (vtop->r & VT_SYM)); + greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0); + o(0x94000000); // bl . + } + else + o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr +} + +static int arm64_hfa_aux(CType *type, int *fsize, int num) +{ + if (is_float(type->t)) { + int a, n = type_size(type, &a); + if (num >= 4 || (*fsize && *fsize != n)) + return -1; + *fsize = n; + return num + 1; + } + else if ((type->t & VT_BTYPE) == VT_STRUCT) { + int is_struct = 0; // rather than union + Sym *field; + for (field = type->ref->next; field; field = field->next) + if (field->c) { + is_struct = 1; + break; + } + if (is_struct) { + int num0 = num; + for (field = type->ref->next; field; field = field->next) { + if (field->c != (num - num0) * *fsize) + return -1; + num = arm64_hfa_aux(&field->type, fsize, num); + if (num == -1) + return -1; + } + if (type->ref->c != (num - num0) * *fsize) + return -1; + return num; + } + else { // union + int num0 = num; + for (field = type->ref->next; field; field = field->next) { + int num1 = arm64_hfa_aux(&field->type, fsize, num0); + if (num1 == -1) + return -1; + num = num1 < num ? num : num1; + } + if (type->ref->c != (num - num0) * *fsize) + return -1; + return num; + } + } + else if (type->t & VT_ARRAY) { + int num1; + if (!type->ref->c) + return num; + num1 = arm64_hfa_aux(&type->ref->type, fsize, num); + if (num1 == -1 || (num1 != num && type->ref->c > 4)) + return -1; + num1 = num + type->ref->c * (num1 - num); + if (num1 > 4) + return -1; + return num1; + } + return -1; +} + +static int arm64_hfa(CType *type, int *fsize) +{ + if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) { + int sz = 0; + int n = arm64_hfa_aux(type, &sz, 0); + if (0 < n && n <= 4) { + if (fsize) + *fsize = sz; + return n; + } + } + return 0; +} + +static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a) +{ + int nx = 0; // next integer register + int nv = 0; // next vector register + unsigned long ns = 32; // next stack offset + int i; + + for (i = 0; i < n; i++) { + int hfa = arm64_hfa(type[i], 0); + int size, align; + + if ((type[i]->t & VT_ARRAY) || + (type[i]->t & VT_BTYPE) == VT_FUNC) + size = align = 8; + else + size = type_size(type[i], &align); + + if (hfa) + // B.2 + ; + else if (size > 16) { + // B.3: replace with pointer + if (nx < 8) + a[i] = nx++ << 1 | 1; + else { + ns = (ns + 7) & ~7; + a[i] = ns | 1; + ns += 8; + } + continue; + } + else if ((type[i]->t & VT_BTYPE) == VT_STRUCT) + // B.4 + size = (size + 7) & ~7; + + // C.1 + if (is_float(type[i]->t) && nv < 8) { + a[i] = 16 + (nv++ << 1); + continue; + } + + // C.2 + if (hfa && nv + hfa <= 8) { + a[i] = 16 + (nv << 1); + nv += hfa; + continue; + } + + // C.3 + if (hfa) { + nv = 8; + size = (size + 7) & ~7; + } + + // C.4 + if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) { + ns = (ns + 7) & ~7; + ns = (ns + align - 1) & -align; + } + + // C.5 + if ((type[i]->t & VT_BTYPE) == VT_FLOAT) + size = 8; + + // C.6 + if (hfa || is_float(type[i]->t)) { + a[i] = ns; + ns += size; + continue; + } + + // C.7 + if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) { + a[i] = nx++ << 1; + continue; + } + + // C.8 + if (align == 16) + nx = (nx + 1) & ~1; + + // C.9 + if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) { + a[i] = nx << 1; + nx += 2; + continue; + } + + // C.10 + if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) { + a[i] = nx << 1; + nx += (size + 7) >> 3; + continue; + } + + // C.11 + nx = 8; + + // C.12 + ns = (ns + 7) & ~7; + ns = (ns + align - 1) & -align; + + // C.13 + if ((type[i]->t & VT_BTYPE) == VT_STRUCT) { + a[i] = ns; + ns += size; + continue; + } + + // C.14 + if (size < 8) + size = 8; + + // C.15 + a[i] = ns; + ns += size; + } + + return ns - 32; +} + +static unsigned long arm64_pcs(int n, CType **type, unsigned long *a) +{ + unsigned long stack; + + // Return type: + if ((type[0]->t & VT_BTYPE) == VT_VOID) + a[0] = -1; + else { + arm64_pcs_aux(1, type, a); + assert(a[0] == 0 || a[0] == 1 || a[0] == 16); + } + + // Argument types: + stack = arm64_pcs_aux(n, type + 1, a + 1); + + if (0) { + int i; + for (i = 0; i <= n; i++) { + if (!i) + printf("arm64_pcs return: "); + else + printf("arm64_pcs arg %d: ", i); + if (a[i] == (unsigned long)-1) + printf("void\n"); + else if (a[i] == 1 && !i) + printf("X8 pointer\n"); + else if (a[i] < 16) + printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : ""); + else if (a[i] < 32) + printf("V%lu\n", a[i] / 2 - 8); + else + printf("stack %lu%s\n", + (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : ""); + } + } + + return stack; +} + +ST_FUNC void gfunc_call(int nb_args) +{ + CType *return_type; + CType **t; + unsigned long *a, *a1; + unsigned long stack; + int i; + + return_type = &vtop[-nb_args].type.ref->type; + if ((return_type->t & VT_BTYPE) == VT_STRUCT) + --nb_args; + + t = tcc_malloc((nb_args + 1) * sizeof(*t)); + a = tcc_malloc((nb_args + 1) * sizeof(*a)); + a1 = tcc_malloc((nb_args + 1) * sizeof(*a1)); + + t[0] = return_type; + for (i = 0; i < nb_args; i++) + t[nb_args - i] = &vtop[-i].type; + + stack = arm64_pcs(nb_args, t, a); + + // Allocate space for structs replaced by pointer: + for (i = nb_args; i; i--) + if (a[i] & 1) { + SValue *arg = &vtop[i - nb_args]; + int align, size = type_size(&arg->type, &align); + assert((arg->type.t & VT_BTYPE) == VT_STRUCT); + stack = (stack + align - 1) & -align; + a1[i] = stack; + stack += size; + } + + stack = (stack + 15) >> 4 << 4; + + assert(stack < 0x1000); + if (stack) + o(0xd10003ff | stack << 10); // sub sp,sp,#(n) + + // First pass: set all values on stack + for (i = nb_args; i; i--) { + vpushv(vtop - nb_args + i); + + if (a[i] & 1) { + // struct replaced by pointer + int r = get_reg(RC_INT); + arm64_spoff(intr(r), a1[i]); + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + if (a[i] >= 32) { + // pointer on stack + r = get_reg(RC_INT); + arm64_spoff(intr(r), a1[i]); + arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1); + } + } + else if (a[i] >= 32) { + // value on stack + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + int r = get_reg(RC_INT); + arm64_spoff(intr(r), a[i] - 32); + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + } + else if (is_float(vtop->type.t)) { + gv(RC_FLOAT); + arm64_strv(arm64_type_size(vtop[0].type.t), + fltr(vtop[0].r), 31, a[i] - 32); + } + else { + gv(RC_INT); + arm64_strx(arm64_type_size(vtop[0].type.t), + intr(vtop[0].r), 31, a[i] - 32); + } + } + + --vtop; + } + + // Second pass: assign values to registers + for (i = nb_args; i; i--, vtop--) { + if (a[i] < 16 && !(a[i] & 1)) { + // value in general-purpose registers + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + int align, size = type_size(&vtop->type, &align); + vtop->type.t = VT_PTR; + gaddrof(); + gv(RC_R(a[i] / 2)); + arm64_ldrs(a[i] / 2, size); + } + else + gv(RC_R(a[i] / 2)); + } + else if (a[i] < 16) + // struct replaced by pointer in register + arm64_spoff(a[i] / 2, a1[i]); + else if (a[i] < 32) { + // value in floating-point registers + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz); + vtop->type.t = VT_PTR; + gaddrof(); + gv(RC_R30); + for (j = 0; j < n; j++) + o(0x3d4003c0 | + (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 | + (a[i] / 2 - 8 + j) | + j << 10); // ldr ([sdq])(*),[x30,#(j * sz)] + } + else + gv(RC_F(a[i] / 2 - 8)); + } + } + + if ((return_type->t & VT_BTYPE) == VT_STRUCT) { + if (a[0] == 1) { + // indirect return: set x8 and discard the stack value + gv(RC_R(8)); + --vtop; + } + else + // return in registers: keep the address for after the call + vswap(); + } + + save_regs(0); + arm64_gen_bl_or_b(0); + --vtop; + if (stack) + o(0x910003ff | stack << 10); // add sp,sp,#(n) + + { + int rt = return_type->t; + int bt = rt & VT_BTYPE; + if (bt == VT_BYTE || bt == VT_SHORT) + // Promote small integers: + o(0x13001c00 | (bt == VT_SHORT) << 13 | + (uint32_t)!!(rt & VT_UNSIGNED) << 30); // [su]xt[bh] w0,w0 + else if (bt == VT_STRUCT && !(a[0] & 1)) { + // A struct was returned in registers, so write it out: + gv(RC_R(8)); + --vtop; + if (a[0] == 0) { + int align, size = type_size(return_type, &align); + assert(size <= 16); + if (size > 8) + o(0xa9000500); // stp x0,x1,[x8] + else if (size) + arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0); + + } + else if (a[0] == 16) { + uint32_t j, sz, n = arm64_hfa(return_type, &sz); + for (j = 0; j < n; j++) + o(0x3d000100 | + (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 | + (a[i] / 2 - 8 + j) | + j << 10); // str ([sdq])(*),[x8,#(j * sz)] + } + } + } + + tcc_free(a1); + tcc_free(a); + tcc_free(t); +} + +static unsigned long arm64_func_va_list_stack; +static int arm64_func_va_list_gr_offs; +static int arm64_func_va_list_vr_offs; +static int arm64_func_sub_sp_offset; + +ST_FUNC void gfunc_prolog(CType *func_type) +{ + int n = 0; + int i = 0; + Sym *sym; + CType **t; + unsigned long *a; + + // Why doesn't the caller (gen_function) set func_vt? + func_vt = func_type->ref->type; + func_vc = 144; // offset of where x8 is stored + + for (sym = func_type->ref; sym; sym = sym->next) + ++n; + t = tcc_malloc(n * sizeof(*t)); + a = tcc_malloc(n * sizeof(*a)); + + for (sym = func_type->ref; sym; sym = sym->next) + t[i++] = &sym->type; + + arm64_func_va_list_stack = arm64_pcs(n - 1, t, a); + + o(0xa9b27bfd); // stp x29,x30,[sp,#-224]! + o(0xad0087e0); // stp q0,q1,[sp,#16] + o(0xad018fe2); // stp q2,q3,[sp,#48] + o(0xad0297e4); // stp q4,q5,[sp,#80] + o(0xad039fe6); // stp q6,q7,[sp,#112] + o(0xa90923e8); // stp x8,x8,[sp,#144] + o(0xa90a07e0); // stp x0,x1,[sp,#160] + o(0xa90b0fe2); // stp x2,x3,[sp,#176] + o(0xa90c17e4); // stp x4,x5,[sp,#192] + o(0xa90d1fe6); // stp x6,x7,[sp,#208] + + arm64_func_va_list_gr_offs = -64; + arm64_func_va_list_vr_offs = -128; + + for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) { + int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 : + a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 : + 224 + ((a[i] - 32) >> 1 << 1)); + sym_push(sym->v & ~SYM_FIELD, &sym->type, + (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t), + off); + + if (a[i] < 16) { + int align, size = type_size(&sym->type, &align); + arm64_func_va_list_gr_offs = (a[i] / 2 - 7 + + (!(a[i] & 1) && size > 8)) * 8; + } + else if (a[i] < 32) { + uint32_t hfa = arm64_hfa(&sym->type, 0); + arm64_func_va_list_vr_offs = (a[i] / 2 - 16 + + (hfa ? hfa : 1)) * 16; + } + + // HFAs of float and double need to be written differently: + if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) { + uint32_t j, sz, k = arm64_hfa(&sym->type, &sz); + if (sz < 16) + for (j = 0; j < k; j++) { + o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 | + ((a[i] - 16) / 2 + j) | (off / sz + j) << 10); + // str ([sdq])(*),[sp,#(j * sz)] + } + } + } + + tcc_free(a); + tcc_free(t); + + o(0x910003fd); // mov x29,sp + arm64_func_sub_sp_offset = ind; + // In gfunc_epilog these will be replaced with code to decrement SP: + o(0xd503201f); // nop + o(0xd503201f); // nop + loc = 0; +} + +ST_FUNC void gen_va_start(void) +{ + int r; + --vtop; // we don't need the "arg" + gaddrof(); + r = intr(gv(RC_INT)); + + if (arm64_func_va_list_stack) { + //xx could use add (immediate) here + arm64_movimm(30, arm64_func_va_list_stack + 224); + o(0x8b1e03be); // add x30,x29,x30 + } + else + o(0x910383be); // add x30,x29,#224 + o(0xf900001e | r << 5); // str x30,[x(r)] + + if (arm64_func_va_list_gr_offs) { + if (arm64_func_va_list_stack) + o(0x910383be); // add x30,x29,#224 + o(0xf900041e | r << 5); // str x30,[x(r),#8] + } + + if (arm64_func_va_list_vr_offs) { + o(0x910243be); // add x30,x29,#144 + o(0xf900081e | r << 5); // str x30,[x(r),#16] + } + + arm64_movimm(30, arm64_func_va_list_gr_offs); + o(0xb900181e | r << 5); // str w30,[x(r),#24] + + arm64_movimm(30, arm64_func_va_list_vr_offs); + o(0xb9001c1e | r << 5); // str w30,[x(r),#28] + + --vtop; +} + +ST_FUNC void gen_va_arg(CType *t) +{ + int align, size = type_size(t, &align); + int fsize, hfa = arm64_hfa(t, &fsize); + uint32_t r0, r1; + + if (is_float(t->t)) { + hfa = 1; + fsize = size; + } + + gaddrof(); + r0 = intr(gv(RC_INT)); + r1 = get_reg(RC_INT); + vtop[0].r = r1 | lvalue_type(t->t); + r1 = intr(r1); + + if (!hfa) { + uint32_t n = size > 16 ? 8 : (size + 7) & -8; + o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs + if (align == 16) { + assert(0); // this path untested but needed for __uint128_t + o(0x11003fde); // add w30,w30,#15 + o(0x121c6fde); // and w30,w30,#-16 + } + o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n) + o(0x540000ad); // b.le .+20 + o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack + o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n) + o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack + o(0x14000004); // b .+16 + o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs + o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top + o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw + if (size > 16) + o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)] + } + else { + uint32_t rsz = hfa << 4; + uint32_t ssz = (size + 7) & -(uint32_t)8; + uint32_t b1, b2; + o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs + o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz) + b1 = ind; o(0x5400000d); // b.le lab1 + o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack + if (fsize == 16) { + o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15 + o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16 + } + o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz) + o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack + b2 = ind; o(0x14000000); // b lab2 + // lab1: + write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3); + o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs + o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top + if (hfa == 1 || fsize == 16) + o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw + else { + // We need to change the layout of this HFA. + // Get some space on the stack using global variable "loc": + loc = (loc - size) & -(uint32_t)align; + o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw + arm64_movimm(r1, loc); + o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1) + o(0x4c402bdc | (uint32_t)fsize << 7 | + (uint32_t)(hfa == 2) << 15 | + (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30] + o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 | + (uint32_t)(hfa != 2) << 13 | + (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)] + } + // lab2: + write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2); + } +} + +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, + int *align, int *regsize) +{ + return 0; +} + +ST_FUNC void gfunc_return(CType *func_type) +{ + CType *t = func_type; + unsigned long a; + + arm64_pcs(0, &t, &a); + switch (a) { + case -1: + break; + case 0: + if ((func_type->t & VT_BTYPE) == VT_STRUCT) { + int align, size = type_size(func_type, &align); + gaddrof(); + gv(RC_R(0)); + arm64_ldrs(0, size); + } + else + gv(RC_IRET); + break; + case 1: { + CType type = *func_type; + mk_pointer(&type); + vset(&type, VT_LOCAL | VT_LVAL, func_vc); + indir(); + vswap(); + vstore(); + break; + } + case 16: + if ((func_type->t & VT_BTYPE) == VT_STRUCT) { + uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz); + gaddrof(); + gv(RC_R(0)); + for (j = 0; j < n; j++) + o(0x3d400000 | + (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 | + j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)] + } + else + gv(RC_FRET); + break; + default: + assert(0); + } + vtop--; +} + +ST_FUNC void gfunc_epilog(void) +{ + if (loc) { + // Insert instructions to subtract size of stack frame from SP. + unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset; + uint64_t diff = (-loc + 15) & ~15; + if (!(diff >> 24)) { + if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff) + write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10); + if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12 + write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10); + } + else { + // In this case we may subtract more than necessary, + // but always less than 17/16 of what we were aiming for. + int i = 0; + int j = 0; + while (diff >> 20) { + diff = (diff + 0xffff) >> 16; + ++i; + } + while (diff >> 16) { + diff = (diff + 1) >> 1; + ++j; + } + write32le(ptr, 0xd2800010 | diff << 5 | i << 21); + // mov x16,#(diff),lsl #(16 * i) + write32le(ptr + 4, 0xcb3063ff | j << 10); + // sub sp,sp,x16,lsl #(j) + } + } + o(0x910003bf); // mov sp,x29 + o(0xa8ce7bfd); // ldp x29,x30,[sp],#224 + + o(0xd65f03c0); // ret +} + +// Generate forward branch to label: +ST_FUNC int gjmp(int t) +{ + int r = ind; + if (nocode_wanted) + return t; + o(t); + return r; +} + +// Generate branch to known address: +ST_FUNC void gjmp_addr(int a) +{ + assert(a - ind + 0x8000000 < 0x10000000); + o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff)); +} + +ST_FUNC int gtst(int inv, int t) +{ + int bt = vtop->type.t & VT_BTYPE; + if (bt == VT_LDOUBLE) { + uint32_t a, b, f = fltr(gv(RC_FLOAT)); + a = get_reg(RC_INT); + vpushi(0); + vtop[0].r = a; + b = get_reg(RC_INT); + a = intr(a); + b = intr(b); + o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0] + o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1] + o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1 + o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8 + --vtop; + } + else if (bt == VT_FLOAT || bt == VT_DOUBLE) { + uint32_t a = fltr(gv(RC_FLOAT)); + o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp + o(0x54000040 | !!inv); // b.eq/b.ne .+8 + } + else { + uint32_t ll = (bt == VT_PTR || bt == VT_LLONG); + uint32_t a = intr(gv(RC_INT)); + o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8 + } + --vtop; + return gjmp(t); +} + +static int arm64_iconst(uint64_t *val, SValue *sv) +{ + if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + if (val) { + int t = sv->type.t; + int bt = t & VT_BTYPE; + *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i : + (uint32_t)sv->c.i | + (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000))); + } + return 1; +} + +static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val, + uint32_t x, uint32_t a) +{ + if (op == '-' && !rev) { + val = -val; + op = '+'; + } + val = l ? val : (uint32_t)val; + + switch (op) { + + case '+': { + uint32_t s = l ? val >> 63 : val >> 31; + val = s ? -val : val; + val = l ? val : (uint32_t)val; + if (!(val & ~(uint64_t)0xfff)) + o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10); + else if (!(val & ~(uint64_t)0xfff000)) + o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10); + else { + arm64_movimm(30, val); // use x30 + o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5); + } + return 1; + } + + case '-': + if (!val) + o(0x4b0003e0 | l << 31 | x | a << 16); // neg + else if (val == (l ? (uint64_t)-1 : (uint32_t)-1)) + o(0x2a2003e0 | l << 31 | x | a << 16); // mvn + else { + arm64_movimm(30, val); // use x30 + o(0x4b0003c0 | l << 31 | x | a << 16); // sub + } + return 1; + + case '^': + if (val == -1 || (val == 0xffffffff && !l)) { + o(0x2a2003e0 | l << 31 | x | a << 16); // mvn + return 1; + } + // fall through + case '&': + case '|': { + int e = arm64_encode_bimm64(l ? val : val | val << 32); + if (e < 0) + return 0; + o((op == '&' ? 0x12000000 : + op == '|' ? 0x32000000 : 0x52000000) | + l << 31 | x | a << 5 | (uint32_t)e << 10); + return 1; + } + + case TOK_SAR: + case TOK_SHL: + case TOK_SHR: { + uint32_t n = 32 << l; + val = val & (n - 1); + if (rev) + return 0; + if (!val) + assert(0); + else if (op == TOK_SHL) + o(0x53000000 | l << 31 | l << 22 | x | a << 5 | + (n - val) << 16 | (n - 1 - val) << 10); // lsl + else + o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 | + x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr + return 1; + } + + } + return 0; +} + +static void arm64_gen_opil(int op, uint32_t l) +{ + uint32_t x, a, b; + + // Special treatment for operations with a constant operand: + { + uint64_t val; + int rev = 1; + + if (arm64_iconst(0, &vtop[0])) { + vswap(); + rev = 0; + } + if (arm64_iconst(&val, &vtop[-1])) { + gv(RC_INT); + a = intr(vtop[0].r); + --vtop; + x = get_reg(RC_INT); + ++vtop; + if (arm64_gen_opic(op, l, rev, val, intr(x), a)) { + vtop[0].r = x; + vswap(); + --vtop; + return; + } + } + if (!rev) + vswap(); + } + + gv2(RC_INT, RC_INT); + assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST); + a = intr(vtop[-1].r); + b = intr(vtop[0].r); + vtop -= 2; + x = get_reg(RC_INT); + ++vtop; + vtop[0].r = x; + x = intr(x); + + switch (op) { + case '%': + // Use x30 for quotient: + o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv + o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 | + b << 16 | a << 10); // msub + break; + case '&': + o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and + break; + case '*': + o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul + break; + case '+': + o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add + break; + case '-': + o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub + break; + case '/': + o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv + break; + case '^': + o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor + break; + case '|': + o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr + break; + case TOK_EQ: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f17e0 | x); // cset wA,eq + break; + case TOK_GE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fb7e0 | x); // cset wA,ge + break; + case TOK_GT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fd7e0 | x); // cset wA,gt + break; + case TOK_LE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fc7e0 | x); // cset wA,le + break; + case TOK_LT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fa7e0 | x); // cset wA,lt + break; + case TOK_NE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f07e0 | x); // cset wA,ne + break; + case TOK_SAR: + o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr + break; + case TOK_SHL: + o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl + break; + case TOK_SHR: + o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr + break; + case TOK_UDIV: + case TOK_PDIV: + o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv + break; + case TOK_UGE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f37e0 | x); // cset wA,cs + break; + case TOK_UGT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f97e0 | x); // cset wA,hi + break; + case TOK_ULT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f27e0 | x); // cset wA,cc + break; + case TOK_ULE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f87e0 | x); // cset wA,ls + break; + case TOK_UMOD: + // Use x30 for quotient: + o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv + o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 | + b << 16 | a << 10); // msub + break; + default: + assert(0); + } +} + +ST_FUNC void gen_opi(int op) +{ + arm64_gen_opil(op, 0); +} + +ST_FUNC void gen_opl(int op) +{ + arm64_gen_opil(op, 1); +} + +ST_FUNC void gen_opf(int op) +{ + uint32_t x, a, b, dbl; + + if (vtop[0].type.t == VT_LDOUBLE) { + CType type = vtop[0].type; + int func = 0; + int cond = -1; + switch (op) { + case '*': func = TOK___multf3; break; + case '+': func = TOK___addtf3; break; + case '-': func = TOK___subtf3; break; + case '/': func = TOK___divtf3; break; + case TOK_EQ: func = TOK___eqtf2; cond = 1; break; + case TOK_NE: func = TOK___netf2; cond = 0; break; + case TOK_LT: func = TOK___lttf2; cond = 10; break; + case TOK_GE: func = TOK___getf2; cond = 11; break; + case TOK_LE: func = TOK___letf2; cond = 12; break; + case TOK_GT: func = TOK___gttf2; cond = 13; break; + default: assert(0); break; + } + vpush_global_sym(&func_old_type, func); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = cond < 0 ? REG_FRET : REG_IRET; + if (cond < 0) + vtop->type = type; + else { + o(0x7100001f); // cmp w0,#0 + o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond) + } + return; + } + + dbl = vtop[0].type.t != VT_FLOAT; + gv2(RC_FLOAT, RC_FLOAT); + assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST); + a = fltr(vtop[-1].r); + b = fltr(vtop[0].r); + vtop -= 2; + switch (op) { + case TOK_EQ: case TOK_NE: + case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT: + x = get_reg(RC_INT); + ++vtop; + vtop[0].r = x; + x = intr(x); + break; + default: + x = get_reg(RC_FLOAT); + ++vtop; + vtop[0].r = x; + x = fltr(x); + break; + } + + switch (op) { + case '*': + o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul + break; + case '+': + o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd + break; + case '-': + o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub + break; + case '/': + o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv + break; + case TOK_EQ: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f17e0 | x); // cset w(x),eq + break; + case TOK_GE: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9fb7e0 | x); // cset w(x),ge + break; + case TOK_GT: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9fd7e0 | x); // cset w(x),gt + break; + case TOK_LE: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f87e0 | x); // cset w(x),ls + break; + case TOK_LT: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f57e0 | x); // cset w(x),mi + break; + case TOK_NE: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f07e0 | x); // cset w(x),ne + break; + default: + assert(0); + } +} + +// Generate sign extension from 32 to 64 bits: +ST_FUNC void gen_cvt_sxtw(void) +{ + uint32_t r = intr(gv(RC_INT)); + o(0x93407c00 | r | r << 5); // sxtw x(r),w(r) +} + +ST_FUNC void gen_cvt_itof(int t) +{ + if (t == VT_LDOUBLE) { + int f = vtop->type.t; + int func = (f & VT_BTYPE) == VT_LLONG ? + (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) : + (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf); + vpush_global_sym(&func_old_type, func); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->type.t = t; + vtop->r = REG_FRET; + return; + } + else { + int d, n = intr(gv(RC_INT)); + int s = !(vtop->type.t & VT_UNSIGNED); + uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG); + --vtop; + d = get_reg(RC_FLOAT); + ++vtop; + vtop[0].r = d; + o(0x1e220000 | (uint32_t)!s << 16 | + (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) | + l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n) + } +} + +ST_FUNC void gen_cvt_ftoi(int t) +{ + if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + int func = (t & VT_BTYPE) == VT_LLONG ? + (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) : + (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi); + vpush_global_sym(&func_old_type, func); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->type.t = t; + vtop->r = REG_IRET; + return; + } + else { + int d, n = fltr(gv(RC_FLOAT)); + uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT); + --vtop; + d = get_reg(RC_INT); + ++vtop; + vtop[0].r = d; + o(0x1e380000 | + (uint32_t)!!(t & VT_UNSIGNED) << 16 | + (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) | + l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n) + } +} + +ST_FUNC void gen_cvt_ftof(int t) +{ + int f = vtop[0].type.t; + assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE); + assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE); + if (t == f) + return; + + if (t == VT_LDOUBLE || f == VT_LDOUBLE) { + int func = (t == VT_LDOUBLE) ? + (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) : + (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2); + vpush_global_sym(&func_old_type, func); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->type.t = t; + vtop->r = REG_FRET; + } + else { + int x, a; + gv(RC_FLOAT); + assert(vtop[0].r < VT_CONST); + a = fltr(vtop[0].r); + --vtop; + x = get_reg(RC_FLOAT); + ++vtop; + vtop[0].r = x; + x = fltr(x); + + if (f == VT_FLOAT) + o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a) + else + o(0x1e624000 | x | a << 5); // fcvt s(x),d(a) + } +} + +ST_FUNC void ggoto(void) +{ + arm64_gen_bl_or_b(1); + --vtop; +} + +ST_FUNC void gen_clear_cache(void) +{ + uint32_t beg, end, dsz, isz, p, lab1, b1; + gv2(RC_INT, RC_INT); + vpushi(0); + vtop->r = get_reg(RC_INT); + vpushi(0); + vtop->r = get_reg(RC_INT); + vpushi(0); + vtop->r = get_reg(RC_INT); + beg = intr(vtop[-4].r); // x0 + end = intr(vtop[-3].r); // x1 + dsz = intr(vtop[-2].r); // x2 + isz = intr(vtop[-1].r); // x3 + p = intr(vtop[0].r); // x4 + vtop -= 5; + + o(0xd53b0020 | isz); // mrs x(isz),ctr_el0 + o(0x52800080 | p); // mov w(p),#4 + o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4 + o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz) + o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15 + o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz) + o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1 + o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p) + b1 = ind; o(0x14000000); // b + lab1 = ind; + o(0xd50b7b20 | p); // dc cvau,x(p) + o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz) + write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2); + o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end) + o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1 + o(0xd5033b9f); // dsb ish + o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1 + o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p) + b1 = ind; o(0x14000000); // b + lab1 = ind; + o(0xd50b7520 | p); // ic ivau,x(p) + o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz) + write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2); + o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end) + o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1 + o(0xd5033b9f); // dsb ish + o(0xd5033fdf); // isb +} + +ST_FUNC void gen_vla_sp_save(int addr) { + uint32_t r = intr(get_reg(RC_INT)); + o(0x910003e0 | r); // mov x(r),sp + arm64_strx(3, r, 29, addr); +} + +ST_FUNC void gen_vla_sp_restore(int addr) { + // Use x30 because this function can be called when there + // is a live return value in x0 but there is nothing on + // the value stack to prevent get_reg from returning x0. + uint32_t r = 30; + arm64_ldrx(0, 3, r, 29, addr); + o(0x9100001f | r << 5); // mov sp,x(r) +} + +ST_FUNC void gen_vla_alloc(CType *type, int align) { + uint32_t r = intr(gv(RC_INT)); + o(0x91003c00 | r | r << 5); // add x(r),x(r),#15 + o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15 + o(0xcb2063ff | r << 16); // sub sp,sp,x(r) + vpop(); +} + +/* end of A64 code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final-old/arm64-link.c b/05/tcc-final-old/arm64-link.c new file mode 100644 index 0000000..59322c5 --- /dev/null +++ b/05/tcc-final-old/arm64-link.c @@ -0,0 +1,256 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_AARCH64 + +#define R_DATA_32 R_AARCH64_ABS32 +#define R_DATA_PTR R_AARCH64_ABS64 +#define R_JMP_SLOT R_AARCH64_JUMP_SLOT +#define R_GLOB_DAT R_AARCH64_GLOB_DAT +#define R_COPY R_AARCH64_COPY +#define R_RELATIVE R_AARCH64_RELATIVE + +#define R_NUM R_AARCH64_NUM + +#define ELF_START_ADDR 0x00400000 +#define ELF_PAGE_SIZE 0x1000 + +#define PCRELATIVE_DLLPLT 1 +#define RELOCATE_DLLPLT 1 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_AARCH64_ABS32: + case R_AARCH64_ABS64: + case R_AARCH64_PREL32: + case R_AARCH64_MOVW_UABS_G0_NC: + case R_AARCH64_MOVW_UABS_G1_NC: + case R_AARCH64_MOVW_UABS_G2_NC: + case R_AARCH64_MOVW_UABS_G3: + case R_AARCH64_ADR_PREL_PG_HI21: + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_ADR_GOT_PAGE: + case R_AARCH64_LD64_GOT_LO12_NC: + case R_AARCH64_GLOB_DAT: + case R_AARCH64_COPY: + return 0; + + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: + case R_AARCH64_JUMP_SLOT: + return 1; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_AARCH64_PREL32: + case R_AARCH64_MOVW_UABS_G0_NC: + case R_AARCH64_MOVW_UABS_G1_NC: + case R_AARCH64_MOVW_UABS_G2_NC: + case R_AARCH64_MOVW_UABS_G3: + case R_AARCH64_ADR_PREL_PG_HI21: + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_GLOB_DAT: + case R_AARCH64_JUMP_SLOT: + case R_AARCH64_COPY: + return NO_GOTPLT_ENTRY; + + case R_AARCH64_ABS32: + case R_AARCH64_ABS64: + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: + return AUTO_GOTPLT_ENTRY; + + case R_AARCH64_ADR_GOT_PAGE: + case R_AARCH64_LD64_GOT_LO12_NC: + return ALWAYS_GOTPLT_ENTRY; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + unsigned plt_offset; + + if (s1->output_type == TCC_OUTPUT_DLL) + tcc_error("DLLs unimplemented!"); + + if (plt->data_offset == 0) { + section_ptr_add(plt, 32); + } + plt_offset = plt->data_offset; + + p = section_ptr_add(plt, 16); + write32le(p, got_offset); + write32le(p + 4, (uint64_t) got_offset >> 32); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + uint64_t plt = s1->plt->sh_addr; + uint64_t got = s1->got->sh_addr; + uint64_t off = (got >> 12) - (plt >> 12); + if ((off + ((uint32_t)1 << 20)) >> 21) + tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", off, got, plt); + write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]! + write32le(p + 4, (0x90000010 | // adrp x16,... + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...] + (got & 0xff8) << 7)); + write32le(p + 12, (0x91000210 | // add x16,x16,#... + (got & 0xfff) << 10)); + write32le(p + 16, 0xd61f0220); // br x17 + write32le(p + 20, 0xd503201f); // nop + write32le(p + 24, 0xd503201f); // nop + write32le(p + 28, 0xd503201f); // nop + p += 32; + while (p < p_end) { + uint64_t pc = plt + (p - s1->plt->data); + uint64_t addr = got + read64le(p); + uint64_t off = (addr >> 12) - (pc >> 12); + if ((off + ((uint32_t)1 << 20)) >> 21) + tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", off, addr, pc); + write32le(p, (0x90000010 | // adrp x16,... + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...] + (addr & 0xff8) << 7)); + write32le(p + 8, (0x91000210 | // add x16,x16,#... + (addr & 0xfff) << 10)); + write32le(p + 12, 0xd61f0220); // br x17 + p += 16; + } + } +} + +void relocate_init(Section *sr) {} + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + int sym_index = ELFW(R_SYM)(rel->r_info); +#ifdef DEBUG_RELOC + ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; +#endif + + switch(type) { + case R_AARCH64_ABS64: + write64le(ptr, val); + return; + case R_AARCH64_ABS32: + write32le(ptr, val); + return; + case R_AARCH64_PREL32: + write32le(ptr, val - addr); + return; + case R_AARCH64_MOVW_UABS_G0_NC: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val & 0xffff) << 5)); + return; + case R_AARCH64_MOVW_UABS_G1_NC: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val >> 16 & 0xffff) << 5)); + return; + case R_AARCH64_MOVW_UABS_G2_NC: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val >> 32 & 0xffff) << 5)); + return; + case R_AARCH64_MOVW_UABS_G3: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val >> 48 & 0xffff) << 5)); + return; + case R_AARCH64_ADR_PREL_PG_HI21: { + uint64_t off = (val >> 12) - (addr >> 12); + if ((off + ((uint64_t)1 << 20)) >> 21) + tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed"); + write32le(ptr, ((read32le(ptr) & 0x9f00001f) | + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + return; + } + case R_AARCH64_ADD_ABS_LO12_NC: + write32le(ptr, ((read32le(ptr) & 0xffc003ff) | + (val & 0xfff) << 10)); + return; + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: +#ifdef DEBUG_RELOC + printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val, + (char *) symtab_section->link->data + sym->st_name); +#endif + if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc) + tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed" + " (val=%lx, addr=%lx)", val, addr); + write32le(ptr, (0x14000000 | + (uint32_t)(type == R_AARCH64_CALL26) << 31 | + ((val - addr) >> 2 & 0x3ffffff))); + return; + case R_AARCH64_ADR_GOT_PAGE: { + uint64_t off = + (((s1->got->sh_addr + + s1->sym_attrs[sym_index].got_offset) >> 12) - (addr >> 12)); + if ((off + ((uint64_t)1 << 20)) >> 21) + tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed"); + write32le(ptr, ((read32le(ptr) & 0x9f00001f) | + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + return; + } + case R_AARCH64_LD64_GOT_LO12_NC: + write32le(ptr, + ((read32le(ptr) & 0xfff803ff) | + ((s1->got->sh_addr + + s1->sym_attrs[sym_index].got_offset) & 0xff8) << 7)); + return; + case R_AARCH64_COPY: + return; + case R_AARCH64_GLOB_DAT: + case R_AARCH64_JUMP_SLOT: + /* They don't need addend */ +#ifdef DEBUG_RELOC + printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, + val - rel->r_addend, + (char *) symtab_section->link->data + sym->st_name); +#endif + write64le(ptr, val - rel->r_addend); + return; + case R_AARCH64_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + return; + default: + fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n", + type, (unsigned)addr, ptr, (unsigned)val); + return; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final-old/c67-gen.c b/05/tcc-final-old/c67-gen.c new file mode 100644 index 0000000..bcb4b0e --- /dev/null +++ b/05/tcc-final-old/c67-gen.c @@ -0,0 +1,2540 @@ +/* + * TMS320C67xx code generator for TCC + * + * Copyright (c) 2001, 2002 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +/* #define ASSEMBLY_LISTING_C67 */ + +/* number of available registers */ +#define NB_REGS 24 + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_EAX 0x0004 +#define RC_ST0 0x0008 +#define RC_ECX 0x0010 +#define RC_EDX 0x0020 +#define RC_INT_BSIDE 0x00000040 /* generic integer register on b side */ +#define RC_C67_A4 0x00000100 +#define RC_C67_A5 0x00000200 +#define RC_C67_B4 0x00000400 +#define RC_C67_B5 0x00000800 +#define RC_C67_A6 0x00001000 +#define RC_C67_A7 0x00002000 +#define RC_C67_B6 0x00004000 +#define RC_C67_B7 0x00008000 +#define RC_C67_A8 0x00010000 +#define RC_C67_A9 0x00020000 +#define RC_C67_B8 0x00040000 +#define RC_C67_B9 0x00080000 +#define RC_C67_A10 0x00100000 +#define RC_C67_A11 0x00200000 +#define RC_C67_B10 0x00400000 +#define RC_C67_B11 0x00800000 +#define RC_C67_A12 0x01000000 +#define RC_C67_A13 0x02000000 +#define RC_C67_B12 0x04000000 +#define RC_C67_B13 0x08000000 +#define RC_IRET RC_C67_A4 /* function return: integer register */ +#define RC_LRET RC_C67_A5 /* function return: second integer register */ +#define RC_FRET RC_C67_A4 /* function return: float register */ + +/* pretty names for the registers */ +enum { + TREG_EAX = 0, // really A2 + TREG_ECX, // really A3 + TREG_EDX, // really B0 + TREG_ST0, // really B1 + TREG_C67_A4, + TREG_C67_A5, + TREG_C67_B4, + TREG_C67_B5, + TREG_C67_A6, + TREG_C67_A7, + TREG_C67_B6, + TREG_C67_B7, + TREG_C67_A8, + TREG_C67_A9, + TREG_C67_B8, + TREG_C67_B9, + TREG_C67_A10, + TREG_C67_A11, + TREG_C67_B10, + TREG_C67_B11, + TREG_C67_A12, + TREG_C67_A13, + TREG_C67_B12, + TREG_C67_B13, +}; + +/* return registers for function */ +#define REG_IRET TREG_C67_A4 /* single word int return register */ +#define REG_LRET TREG_C67_A5 /* second word return register (for long long) */ +#define REG_FRET TREG_C67_A4 /* float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +/* #define INVERT_FUNC_PARAMS */ + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 12 +#define LDOUBLE_ALIGN 4 +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 8 + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#include "tcc.h" + +ST_DATA const int reg_classes[NB_REGS] = { + /* eax */ RC_INT | RC_FLOAT | RC_EAX, + // only allow even regs for floats (allow for doubles) + /* ecx */ RC_INT | RC_ECX, + /* edx */ RC_INT | RC_INT_BSIDE | RC_FLOAT | RC_EDX, + // only allow even regs for floats (allow for doubles) + /* st0 */ RC_INT | RC_INT_BSIDE | RC_ST0, + /* A4 */ RC_C67_A4, + /* A5 */ RC_C67_A5, + /* B4 */ RC_C67_B4, + /* B5 */ RC_C67_B5, + /* A6 */ RC_C67_A6, + /* A7 */ RC_C67_A7, + /* B6 */ RC_C67_B6, + /* B7 */ RC_C67_B7, + /* A8 */ RC_C67_A8, + /* A9 */ RC_C67_A9, + /* B8 */ RC_C67_B8, + /* B9 */ RC_C67_B9, + /* A10 */ RC_C67_A10, + /* A11 */ RC_C67_A11, + /* B10 */ RC_C67_B10, + /* B11 */ RC_C67_B11, + /* A12 */ RC_C67_A10, + /* A13 */ RC_C67_A11, + /* B12 */ RC_C67_B10, + /* B13 */ RC_C67_B11 +}; + +// although tcc thinks it is passing parameters on the stack, +// the C67 really passes up to the first 10 params in special +// regs or regs pairs (for 64 bit params). So keep track of +// the stack offsets so we can translate to the appropriate +// reg (pair) + +#define NoCallArgsPassedOnStack 10 +int NoOfCurFuncArgs; +int TranslateStackToReg[NoCallArgsPassedOnStack]; +int ParamLocOnStack[NoCallArgsPassedOnStack]; +int TotalBytesPushedOnStack; + +#ifndef FALSE +# define FALSE 0 +# define TRUE 1 +#endif + +#undef BOOL +#define BOOL int + +#define ALWAYS_ASSERT(x) \ +do {\ + if (!(x))\ + tcc_error("internal compiler error file at %s:%d", __FILE__, __LINE__);\ +} while (0) + +/******************************************************/ +static unsigned long func_sub_sp_offset; +static int func_ret_sub; + +static BOOL C67_invert_test; +static int C67_compare_reg; + +#ifdef ASSEMBLY_LISTING_C67 +FILE *f = NULL; +#endif + +void C67_g(int c) +{ + int ind1; + if (nocode_wanted) + return; +#ifdef ASSEMBLY_LISTING_C67 + fprintf(f, " %08X", c); +#endif + ind1 = ind + 4; + if (ind1 > (int) cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c & 0xff; + cur_text_section->data[ind + 1] = (c >> 8) & 0xff; + cur_text_section->data[ind + 2] = (c >> 16) & 0xff; + cur_text_section->data[ind + 3] = (c >> 24) & 0xff; + ind = ind1; +} + + +/* output a symbol and patch all calls to it */ +void gsym_addr(int t, int a) +{ + int n, *ptr; + while (t) { + ptr = (int *) (cur_text_section->data + t); + { + Sym *sym; + + // extract 32 bit address from MVKH/MVKL + n = ((*ptr >> 7) & 0xffff); + n |= ((*(ptr + 1) >> 7) & 0xffff) << 16; + + // define a label that will be relocated + + sym = get_sym_ref(&char_pointer_type, cur_text_section, a, 0); + greloc(cur_text_section, sym, t, R_C60LO16); + greloc(cur_text_section, sym, t + 4, R_C60HI16); + + // clear out where the pointer was + + *ptr &= ~(0xffff << 7); + *(ptr + 1) &= ~(0xffff << 7); + } + t = n; + } +} + +void gsym(int t) +{ + gsym_addr(t, ind); +} + +// these are regs that tcc doesn't really know about, +// but assign them unique values so the mapping routines +// can distinguish them + +#define C67_A0 105 +#define C67_SP 106 +#define C67_B3 107 +#define C67_FP 108 +#define C67_B2 109 +#define C67_CREG_ZERO -1 /* Special code for no condition reg test */ + + +int ConvertRegToRegClass(int r) +{ + // only works for A4-B13 + + return RC_C67_A4 << (r - TREG_C67_A4); +} + + +// map TCC reg to C67 reg number + +int C67_map_regn(int r) +{ + if (r == 0) // normal tcc regs + return 0x2; // A2 + else if (r == 1) // normal tcc regs + return 3; // A3 + else if (r == 2) // normal tcc regs + return 0; // B0 + else if (r == 3) // normal tcc regs + return 1; // B1 + else if (r >= TREG_C67_A4 && r <= TREG_C67_B13) // these form a pattern of alt pairs + return (((r & 0xfffffffc) >> 1) | (r & 1)) + 2; + else if (r == C67_A0) + return 0; // set to A0 (offset reg) + else if (r == C67_B2) + return 2; // set to B2 (offset reg) + else if (r == C67_B3) + return 3; // set to B3 (return address reg) + else if (r == C67_SP) + return 15; // set to SP (B15) (offset reg) + else if (r == C67_FP) + return 15; // set to FP (A15) (offset reg) + else if (r == C67_CREG_ZERO) + return 0; // Special code for no condition reg test + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + +// mapping from tcc reg number to +// C67 register to condition code field +// +// valid condition code regs are: +// +// tcc reg 2 ->B0 -> 1 +// tcc reg 3 ->B1 -> 2 +// tcc reg 0 -> A2 -> 5 +// tcc reg 1 -> A3 -> X +// tcc reg B2 -> 3 + +int C67_map_regc(int r) +{ + if (r == 0) // normal tcc regs + return 0x5; + else if (r == 2) // normal tcc regs + return 0x1; + else if (r == 3) // normal tcc regs + return 0x2; + else if (r == C67_B2) // normal tcc regs + return 0x3; + else if (r == C67_CREG_ZERO) + return 0; // Special code for no condition reg test + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + + +// map TCC reg to C67 reg side A or B + +int C67_map_regs(int r) +{ + if (r == 0) // normal tcc regs + return 0x0; + else if (r == 1) // normal tcc regs + return 0x0; + else if (r == 2) // normal tcc regs + return 0x1; + else if (r == 3) // normal tcc regs + return 0x1; + else if (r >= TREG_C67_A4 && r <= TREG_C67_B13) // these form a pattern of alt pairs + return (r & 2) >> 1; + else if (r == C67_A0) + return 0; // set to A side + else if (r == C67_B2) + return 1; // set to B side + else if (r == C67_B3) + return 1; // set to B side + else if (r == C67_SP) + return 0x1; // set to SP (B15) B side + else if (r == C67_FP) + return 0x0; // set to FP (A15) A side + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + +int C67_map_S12(char *s) +{ + if (strstr(s, ".S1") != NULL) + return 0; + else if (strcmp(s, ".S2")) + return 1; + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + +int C67_map_D12(char *s) +{ + if (strstr(s, ".D1") != NULL) + return 0; + else if (strcmp(s, ".D2")) + return 1; + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + + + +void C67_asm(char *s, int a, int b, int c) +{ + BOOL xpath; + +#ifdef ASSEMBLY_LISTING_C67 + if (!f) { + f = fopen("TCC67_out.txt", "wt"); + } + fprintf(f, "%04X ", ind); +#endif + + if (strstr(s, "MVKL") == s) { + C67_g((C67_map_regn(b) << 23) | + ((a & 0xffff) << 7) | (0x0a << 2) | (C67_map_regs(b) << 1)); + } else if (strstr(s, "MVKH") == s) { + C67_g((C67_map_regn(b) << 23) | + (((a >> 16) & 0xffff) << 7) | + (0x1a << 2) | (C67_map_regs(b) << 1)); + } else if (strstr(s, "STW.D SP POST DEC") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //SP B15 + (2 << 13) | //ucst5 (must keep 8 byte boundary !!) + (0xa << 9) | //mode a = post dec ucst + (0 << 8) | //r (LDDW bit 0) + (1 << 7) | //y D1/D2 use B side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STB.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STH.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STB.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STH.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STW.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STW.D *") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STH.D *") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STB.D *") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STW.D +*") == s) { + ALWAYS_ASSERT(c < 32); + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (c << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D SP PRE INC") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg B15 + (2 << 13) | //ucst5 (must keep 8 byte boundary) + (9 << 9) | //mode 9 = pre inc ucst5 + (0 << 8) | //r (LDDW bit 0) + (1 << 7) | //y D1/D2 B side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDDW.D SP PRE INC") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg B15 + (1 << 13) | //ucst5 (must keep 8 byte boundary) + (9 << 9) | //mode 9 = pre inc ucst5 + (1 << 8) | //r (LDDW bit 1) + (1 << 7) | //y D1/D2 B side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDDW.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (1 << 8) | //r (LDDW bit 1) + (0 << 7) | //y D1/D2 A side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDH.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (4 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDB.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (2 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDHU.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (0 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDBU.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (1 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDDW.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (1 << 8) | //r (LDDW bit 1) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDH.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (4 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDB.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (2 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDHU.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (0 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDBU.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (1 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D +*") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (1 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "CMPLTSP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x3a << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGTSP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x39 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPEQSP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x38 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } + + else if (strstr(s, "CMPLTDP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x2a << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGTDP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x29 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPEQDP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x28 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPLT") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x57 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGT") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x47 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPEQ") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x53 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPLTU") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x5f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGTU") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x4f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "B DISP") == s) { + C67_g((0 << 29) | //creg + (0 << 28) | //z + (a << 7) | //cnst + (0x4 << 2) | //opcode fixed + (0 << 1) | //S0/S1 + (0 << 0)); //parallel + } else if (strstr(s, "B.") == s) { + xpath = C67_map_regs(c) ^ 1; + + C67_g((C67_map_regc(b) << 29) | //creg + (a << 28) | //inv + (0 << 23) | //dst + (C67_map_regn(c) << 18) | //src2 + (0 << 13) | // + (xpath << 12) | //x cross path if !B side + (0xd << 6) | //opcode + (0x8 << 2) | //opcode fixed + (1 << 1) | //must be S2 + (0 << 0)); //parallel + } else if (strstr(s, "MV.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 (cst5) + (xpath << 12) | //x cross path if opposite sides + (0x2 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SPTRUNC.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0xb << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "DPTRUNC.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x1 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTSP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x4a << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTSPU.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x49 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x39 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTDPU.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x3b << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SPDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x2 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "DPSP.L") == s) { + ALWAYS_ASSERT(C67_map_regs(b) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason + (0 << 13) | //src1 NA + (0 << 12) | //x cross path if opposite sides + (0x9 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "ADD.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x3 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SUB.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x7 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "OR.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x7f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "AND.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x7b << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "XOR.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x6f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "ADDSP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x10 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "ADDDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x18 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SUBSP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x11 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SUBDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x19 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "MPYSP.M") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x1c << 7) | //opcode + (0x0 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "MPYDP.M") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x0e << 7) | //opcode + (0x0 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "MPYI.M") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 (cst5) + (xpath << 12) | //x cross path if opposite sides + (0x4 << 7) | //opcode + (0x0 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SHR.S") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x37 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SHRU.S") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x27 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SHL.S") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x33 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "||ADDK") == s) { + xpath = 0; // no xpath required just use the side of the src/dst + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(b) << 23) | //dst + (a << 07) | //scst16 + (0x14 << 2) | //opcode fixed + (C67_map_regs(b) << 1) | //side of dst + (1 << 0)); //parallel + } else if (strstr(s, "ADDK") == s) { + xpath = 0; // no xpath required just use the side of the src/dst + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(b) << 23) | //dst + (a << 07) | //scst16 + (0x14 << 2) | //opcode fixed + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "NOP") == s) { + C67_g(((a - 1) << 13) | //no of cycles + (0 << 0)); //parallel + } else + ALWAYS_ASSERT(FALSE); + +#ifdef ASSEMBLY_LISTING_C67 + fprintf(f, " %s %d %d %d\n", s, a, b, c); +#endif + +} + +//r=reg to load, fr=from reg, symbol for relocation, constant + +void C67_MVKL(int r, int fc) +{ + C67_asm("MVKL.", fc, r, 0); +} + +void C67_MVKH(int r, int fc) +{ + C67_asm("MVKH.", fc, r, 0); +} + +void C67_STB_SP_A0(int r) +{ + C67_asm("STB.D *+SP[A0]", r, 0, 0); // STB r,*+SP[A0] +} + +void C67_STH_SP_A0(int r) +{ + C67_asm("STH.D *+SP[A0]", r, 0, 0); // STH r,*+SP[A0] +} + +void C67_STW_SP_A0(int r) +{ + C67_asm("STW.D *+SP[A0]", r, 0, 0); // STW r,*+SP[A0] +} + +void C67_STB_PTR(int r, int r2) +{ + C67_asm("STB.D *", r, r2, 0); // STB r, *r2 +} + +void C67_STH_PTR(int r, int r2) +{ + C67_asm("STH.D *", r, r2, 0); // STH r, *r2 +} + +void C67_STW_PTR(int r, int r2) +{ + C67_asm("STW.D *", r, r2, 0); // STW r, *r2 +} + +void C67_STW_PTR_PRE_INC(int r, int r2, int n) +{ + C67_asm("STW.D +*", r, r2, n); // STW r, *+r2 +} + +void C67_PUSH(int r) +{ + C67_asm("STW.D SP POST DEC", r, 0, 0); // STW r,*SP-- +} + +void C67_LDW_SP_A0(int r) +{ + C67_asm("LDW.D *+SP[A0]", r, 0, 0); // LDW *+SP[A0],r +} + +void C67_LDDW_SP_A0(int r) +{ + C67_asm("LDDW.D *+SP[A0]", r, 0, 0); // LDDW *+SP[A0],r +} + +void C67_LDH_SP_A0(int r) +{ + C67_asm("LDH.D *+SP[A0]", r, 0, 0); // LDH *+SP[A0],r +} + +void C67_LDB_SP_A0(int r) +{ + C67_asm("LDB.D *+SP[A0]", r, 0, 0); // LDB *+SP[A0],r +} + +void C67_LDHU_SP_A0(int r) +{ + C67_asm("LDHU.D *+SP[A0]", r, 0, 0); // LDHU *+SP[A0],r +} + +void C67_LDBU_SP_A0(int r) +{ + C67_asm("LDBU.D *+SP[A0]", r, 0, 0); // LDBU *+SP[A0],r +} + +void C67_LDW_PTR(int r, int r2) +{ + C67_asm("LDW.D *", r, r2, 0); // LDW *r,r2 +} + +void C67_LDDW_PTR(int r, int r2) +{ + C67_asm("LDDW.D *", r, r2, 0); // LDDW *r,r2 +} + +void C67_LDH_PTR(int r, int r2) +{ + C67_asm("LDH.D *", r, r2, 0); // LDH *r,r2 +} + +void C67_LDB_PTR(int r, int r2) +{ + C67_asm("LDB.D *", r, r2, 0); // LDB *r,r2 +} + +void C67_LDHU_PTR(int r, int r2) +{ + C67_asm("LDHU.D *", r, r2, 0); // LDHU *r,r2 +} + +void C67_LDBU_PTR(int r, int r2) +{ + C67_asm("LDBU.D *", r, r2, 0); // LDBU *r,r2 +} + +void C67_LDW_PTR_PRE_INC(int r, int r2) +{ + C67_asm("LDW.D +*", r, r2, 0); // LDW *+r,r2 +} + +void C67_POP(int r) +{ + C67_asm("LDW.D SP PRE INC", r, 0, 0); // LDW *++SP,r +} + +void C67_POP_DW(int r) +{ + C67_asm("LDDW.D SP PRE INC", r, 0, 0); // LDDW *++SP,r +} + +void C67_CMPLT(int s1, int s2, int dst) +{ + C67_asm("CMPLT.L1", s1, s2, dst); +} + +void C67_CMPGT(int s1, int s2, int dst) +{ + C67_asm("CMPGT.L1", s1, s2, dst); +} + +void C67_CMPEQ(int s1, int s2, int dst) +{ + C67_asm("CMPEQ.L1", s1, s2, dst); +} + +void C67_CMPLTU(int s1, int s2, int dst) +{ + C67_asm("CMPLTU.L1", s1, s2, dst); +} + +void C67_CMPGTU(int s1, int s2, int dst) +{ + C67_asm("CMPGTU.L1", s1, s2, dst); +} + + +void C67_CMPLTSP(int s1, int s2, int dst) +{ + C67_asm("CMPLTSP.S1", s1, s2, dst); +} + +void C67_CMPGTSP(int s1, int s2, int dst) +{ + C67_asm("CMPGTSP.S1", s1, s2, dst); +} + +void C67_CMPEQSP(int s1, int s2, int dst) +{ + C67_asm("CMPEQSP.S1", s1, s2, dst); +} + +void C67_CMPLTDP(int s1, int s2, int dst) +{ + C67_asm("CMPLTDP.S1", s1, s2, dst); +} + +void C67_CMPGTDP(int s1, int s2, int dst) +{ + C67_asm("CMPGTDP.S1", s1, s2, dst); +} + +void C67_CMPEQDP(int s1, int s2, int dst) +{ + C67_asm("CMPEQDP.S1", s1, s2, dst); +} + + +void C67_IREG_B_REG(int inv, int r1, int r2) // [!R] B r2 +{ + C67_asm("B.S2", inv, r1, r2); +} + + +// call with how many 32 bit words to skip +// (0 would branch to the branch instruction) + +void C67_B_DISP(int disp) // B +2 Branch with constant displacement +{ + // Branch point is relative to the 8 word fetch packet + // + // we will assume the text section always starts on an 8 word (32 byte boundary) + // + // so add in how many words into the fetch packet the branch is + + + C67_asm("B DISP", disp + ((ind & 31) >> 2), 0, 0); +} + +void C67_NOP(int n) +{ + C67_asm("NOP", n, 0, 0); +} + +void C67_ADDK(int n, int r) +{ + ALWAYS_ASSERT(abs(n) < 32767); + + C67_asm("ADDK", n, r, 0); +} + +void C67_ADDK_PARALLEL(int n, int r) +{ + ALWAYS_ASSERT(abs(n) < 32767); + + C67_asm("||ADDK", n, r, 0); +} + +void C67_Adjust_ADDK(int *inst, int n) +{ + ALWAYS_ASSERT(abs(n) < 32767); + + *inst = (*inst & (~(0xffff << 7))) | ((n & 0xffff) << 7); +} + +void C67_MV(int r, int v) +{ + C67_asm("MV.L", 0, r, v); +} + + +void C67_DPTRUNC(int r, int v) +{ + C67_asm("DPTRUNC.L", 0, r, v); +} + +void C67_SPTRUNC(int r, int v) +{ + C67_asm("SPTRUNC.L", 0, r, v); +} + +void C67_INTSP(int r, int v) +{ + C67_asm("INTSP.L", 0, r, v); +} + +void C67_INTDP(int r, int v) +{ + C67_asm("INTDP.L", 0, r, v); +} + +void C67_INTSPU(int r, int v) +{ + C67_asm("INTSPU.L", 0, r, v); +} + +void C67_INTDPU(int r, int v) +{ + C67_asm("INTDPU.L", 0, r, v); +} + +void C67_SPDP(int r, int v) +{ + C67_asm("SPDP.L", 0, r, v); +} + +void C67_DPSP(int r, int v) // note regs must be on the same side +{ + C67_asm("DPSP.L", 0, r, v); +} + +void C67_ADD(int r, int v) +{ + C67_asm("ADD.L", v, r, v); +} + +void C67_SUB(int r, int v) +{ + C67_asm("SUB.L", v, r, v); +} + +void C67_AND(int r, int v) +{ + C67_asm("AND.L", v, r, v); +} + +void C67_OR(int r, int v) +{ + C67_asm("OR.L", v, r, v); +} + +void C67_XOR(int r, int v) +{ + C67_asm("XOR.L", v, r, v); +} + +void C67_ADDSP(int r, int v) +{ + C67_asm("ADDSP.L", v, r, v); +} + +void C67_SUBSP(int r, int v) +{ + C67_asm("SUBSP.L", v, r, v); +} + +void C67_MPYSP(int r, int v) +{ + C67_asm("MPYSP.M", v, r, v); +} + +void C67_ADDDP(int r, int v) +{ + C67_asm("ADDDP.L", v, r, v); +} + +void C67_SUBDP(int r, int v) +{ + C67_asm("SUBDP.L", v, r, v); +} + +void C67_MPYDP(int r, int v) +{ + C67_asm("MPYDP.M", v, r, v); +} + +void C67_MPYI(int r, int v) +{ + C67_asm("MPYI.M", v, r, v); +} + +void C67_SHL(int r, int v) +{ + C67_asm("SHL.S", r, v, v); +} + +void C67_SHRU(int r, int v) +{ + C67_asm("SHRU.S", r, v, v); +} + +void C67_SHR(int r, int v) +{ + C67_asm("SHR.S", r, v, v); +} + + + +/* load 'r' from value 'sv' */ +void load(int r, SValue * sv) +{ + int v, t, ft, fc, fr, size = 0, element; + BOOL Unsigned = FALSE; + SValue v1; + + fr = sv->r; + ft = sv->type.t; + fc = sv->c.i; + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + if (v == VT_LLOCAL) { + v1.type.t = VT_INT; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + load(r, &v1); + fr = r; + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + tcc_error("long double not supported"); + } else if ((ft & VT_TYPE) == VT_BYTE) { + size = 1; + } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) { + size = 1; + Unsigned = TRUE; + } else if ((ft & VT_TYPE) == VT_SHORT) { + size = 2; + } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) { + size = 2; + Unsigned = TRUE; + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + size = 8; + } else { + size = 4; + } + + // check if fc is a positive reference on the stack, + // if it is tcc is referencing what it thinks is a parameter + // on the stack, so check if it is really in a register. + + + if (v == VT_LOCAL && fc > 0) { + int stack_pos = 8; + + for (t = 0; t < NoCallArgsPassedOnStack; t++) { + if (fc == stack_pos) + break; + + stack_pos += TranslateStackToReg[t]; + } + + // param has been pushed on stack, get it like a local var + + fc = ParamLocOnStack[t] - 8; + } + + if ((fr & VT_VALMASK) < VT_CONST) // check for pure indirect + { + if (size == 1) { + if (Unsigned) + C67_LDBU_PTR(v, r); // LDBU *v,r + else + C67_LDB_PTR(v, r); // LDB *v,r + } else if (size == 2) { + if (Unsigned) + C67_LDHU_PTR(v, r); // LDHU *v,r + else + C67_LDH_PTR(v, r); // LDH *v,r + } else if (size == 4) { + C67_LDW_PTR(v, r); // LDW *v,r + } else if (size == 8) { + C67_LDDW_PTR(v, r); // LDDW *v,r + } + + C67_NOP(4); // NOP 4 + return; + } else if (fr & VT_SYM) { + greloc(cur_text_section, sv->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sv->sym, ind + 4, R_C60HI16); + + + C67_MVKL(C67_A0, fc); //r=reg to load, constant + C67_MVKH(C67_A0, fc); //r=reg to load, constant + + + if (size == 1) { + if (Unsigned) + C67_LDBU_PTR(C67_A0, r); // LDBU *A0,r + else + C67_LDB_PTR(C67_A0, r); // LDB *A0,r + } else if (size == 2) { + if (Unsigned) + C67_LDHU_PTR(C67_A0, r); // LDHU *A0,r + else + C67_LDH_PTR(C67_A0, r); // LDH *A0,r + } else if (size == 4) { + C67_LDW_PTR(C67_A0, r); // LDW *A0,r + } else if (size == 8) { + C67_LDDW_PTR(C67_A0, r); // LDDW *A0,r + } + + C67_NOP(4); // NOP 4 + return; + } else { + element = size; + + // divide offset in bytes to create element index + C67_MVKL(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + C67_MVKH(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + + if (size == 1) { + if (Unsigned) + C67_LDBU_SP_A0(r); // LDBU r, SP[A0] + else + C67_LDB_SP_A0(r); // LDB r, SP[A0] + } else if (size == 2) { + if (Unsigned) + C67_LDHU_SP_A0(r); // LDHU r, SP[A0] + else + C67_LDH_SP_A0(r); // LDH r, SP[A0] + } else if (size == 4) { + C67_LDW_SP_A0(r); // LDW r, SP[A0] + } else if (size == 8) { + C67_LDDW_SP_A0(r); // LDDW r, SP[A0] + } + + + C67_NOP(4); // NOP 4 + return; + } + } else { + if (v == VT_CONST) { + if (fr & VT_SYM) { + greloc(cur_text_section, sv->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sv->sym, ind + 4, R_C60HI16); + } + C67_MVKL(r, fc); //r=reg to load, constant + C67_MVKH(r, fc); //r=reg to load, constant + } else if (v == VT_LOCAL) { + C67_MVKL(r, fc + 8); //r=reg to load, constant C67 stack points to next free + C67_MVKH(r, fc + 8); //r=reg to load, constant + C67_ADD(C67_FP, r); // MV v,r v -> r + } else if (v == VT_CMP) { + C67_MV(C67_compare_reg, r); // MV v,r v -> r + } else if (v == VT_JMP || v == VT_JMPI) { + t = v & 1; + C67_B_DISP(4); // Branch with constant displacement, skip over this branch, load, nop, load + C67_MVKL(r, t); // r=reg to load, 0 or 1 (do this while branching) + C67_NOP(4); // NOP 4 + gsym(fc); // modifies other branches to branch here + C67_MVKL(r, t ^ 1); // r=reg to load, 0 or 1 + } else if (v != r) { + C67_MV(v, r); // MV v,r v -> r + + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_MV(v + 1, r + 1); // MV v,r v -> r + } + } +} + + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue * v) +{ + int fr, bt, ft, fc, size, t, element; + + ft = v->type.t; + fc = v->c.i; + fr = v->r & VT_VALMASK; + bt = ft & VT_BTYPE; + /* XXX: incorrect if float reg to reg */ + + if (bt == VT_LDOUBLE) { + tcc_error("long double not supported"); + } else { + if (bt == VT_SHORT) + size = 2; + else if (bt == VT_BYTE) + size = 1; + else if (bt == VT_DOUBLE) + size = 8; + else + size = 4; + + if ((v->r & VT_VALMASK) == VT_CONST) { + /* constant memory reference */ + + if (v->r & VT_SYM) { + greloc(cur_text_section, v->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, v->sym, ind + 4, R_C60HI16); + } + C67_MVKL(C67_A0, fc); //r=reg to load, constant + C67_MVKH(C67_A0, fc); //r=reg to load, constant + + if (size == 1) + C67_STB_PTR(r, C67_A0); // STB r, *A0 + else if (size == 2) + C67_STH_PTR(r, C67_A0); // STH r, *A0 + else if (size == 4 || size == 8) + C67_STW_PTR(r, C67_A0); // STW r, *A0 + + if (size == 8) + C67_STW_PTR_PRE_INC(r + 1, C67_A0, 1); // STW r, *+A0[1] + } else if ((v->r & VT_VALMASK) == VT_LOCAL) { + // check case of storing to passed argument that + // tcc thinks is on the stack but for C67 is + // passed as a reg. However it may have been + // saved to the stack, if that reg was required + // for a call to a child function + + if (fc > 0) // argument ?? + { + // walk through sizes and figure which param + + int stack_pos = 8; + + for (t = 0; t < NoCallArgsPassedOnStack; t++) { + if (fc == stack_pos) + break; + + stack_pos += TranslateStackToReg[t]; + } + + // param has been pushed on stack, get it like a local var + fc = ParamLocOnStack[t] - 8; + } + + if (size == 8) + element = 4; + else + element = size; + + // divide offset in bytes to create word index + C67_MVKL(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + C67_MVKH(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + + + + if (size == 1) + C67_STB_SP_A0(r); // STB r, SP[A0] + else if (size == 2) + C67_STH_SP_A0(r); // STH r, SP[A0] + else if (size == 4 || size == 8) + C67_STW_SP_A0(r); // STW r, SP[A0] + + if (size == 8) { + C67_ADDK(1, C67_A0); // ADDK 1,A0 + C67_STW_SP_A0(r + 1); // STW r, SP[A0] + } + } else { + if (size == 1) + C67_STB_PTR(r, fr); // STB r, *fr + else if (size == 2) + C67_STH_PTR(r, fr); // STH r, *fr + else if (size == 4 || size == 8) + C67_STW_PTR(r, fr); // STW r, *fr + + if (size == 8) { + C67_STW_PTR_PRE_INC(r + 1, fr, 1); // STW r, *+fr[1] + } + } + } +} + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + Sym *sym; + + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + /* constant case */ + if (vtop->r & VT_SYM) { + /* relocation case */ + + // get add into A0, then start the jump B3 + + greloc(cur_text_section, vtop->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, vtop->sym, ind + 4, R_C60HI16); + + C67_MVKL(C67_A0, 0); //r=reg to load, constant + C67_MVKH(C67_A0, 0); //r=reg to load, constant + C67_IREG_B_REG(0, C67_CREG_ZERO, C67_A0); // B.S2x A0 + + if (is_jmp) { + C67_NOP(5); // simple jump, just put NOP + } else { + // Call, must load return address into B3 during delay slots + + sym = get_sym_ref(&char_pointer_type, cur_text_section, ind + 12, 0); // symbol for return address + greloc(cur_text_section, sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sym, ind + 4, R_C60HI16); + C67_MVKL(C67_B3, 0); //r=reg to load, constant + C67_MVKH(C67_B3, 0); //r=reg to load, constant + C67_NOP(3); // put remaining NOPs + } + } else { + /* put an empty PC32 relocation */ + ALWAYS_ASSERT(FALSE); + } + } else { + /* otherwise, indirect call */ + r = gv(RC_INT); + C67_IREG_B_REG(0, C67_CREG_ZERO, r); // B.S2x r + + if (is_jmp) { + C67_NOP(5); // simple jump, just put NOP + } else { + // Call, must load return address into B3 during delay slots + + sym = get_sym_ref(&char_pointer_type, cur_text_section, ind + 12, 0); // symbol for return address + greloc(cur_text_section, sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sym, ind + 4, R_C60HI16); + C67_MVKL(C67_B3, 0); //r=reg to load, constant + C67_MVKH(C67_B3, 0); //r=reg to load, constant + C67_NOP(3); // put remaining NOPs + } + } +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) { + *ret_align = 1; // Never have to re-align return values for x86-64 + return 0; +} + +/* generate function call with address in (vtop->t, vtop->c) and free function + context. Stack entry is popped */ +void gfunc_call(int nb_args) +{ + int i, r, size = 0; + int args_sizes[NoCallArgsPassedOnStack]; + + if (nb_args > NoCallArgsPassedOnStack) { + tcc_error("more than 10 function params not currently supported"); + // handle more than 10, put some on the stack + } + + for (i = 0; i < nb_args; i++) { + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + ALWAYS_ASSERT(FALSE); + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + + + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + tcc_error("long long not supported"); + } else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + tcc_error("long double not supported"); + } else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) { + size = 8; + } else { + size = 4; + } + + // put the parameter into the corresponding reg (pair) + + r = gv(RC_C67_A4 << (2 * i)); + + // must put on stack because with 1 pass compiler , no way to tell + // if an up coming nested call might overwrite these regs + + C67_PUSH(r); + + if (size == 8) { + C67_STW_PTR_PRE_INC(r + 1, C67_SP, 3); // STW r, *+SP[3] (go back and put the other) + } + args_sizes[i] = size; + } + vtop--; + } + // POP all the params on the stack into registers for the + // immediate call (in reverse order) + + for (i = nb_args - 1; i >= 0; i--) { + + if (args_sizes[i] == 8) + C67_POP_DW(TREG_C67_A4 + i * 2); + else + C67_POP(TREG_C67_A4 + i * 2); + } + gcall_or_jmp(0); + vtop--; +} + + +// to be compatible with Code Composer for the C67 +// the first 10 parameters must be passed in registers +// (pairs for 64 bits) starting wit; A4:A5, then B4:B5 and +// ending with B12:B13. +// +// When a call is made, if the caller has its parameters +// in regs A4-B13 these must be saved before/as the call +// parameters are loaded and restored upon return (or if/when needed). + +/* generate function prolog of type 't' */ +void gfunc_prolog(CType * func_type) +{ + int addr, align, size, func_call, i; + Sym *sym; + CType *type; + + sym = func_type->ref; + func_call = sym->f.func_call; + addr = 8; + /* if the function returns a structure, then add an + implicit pointer parameter */ + func_vt = sym->type; + func_var = (sym->f.func_type == FUNC_ELLIPSIS); + if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { + func_vc = addr; + addr += 4; + } + + NoOfCurFuncArgs = 0; + + /* define parameters */ + while ((sym = sym->next) != NULL) { + type = &sym->type; + sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), addr); + size = type_size(type, &align); + size = (size + 3) & ~3; + + // keep track of size of arguments so + // we can translate where tcc thinks they + // are on the stack into the appropriate reg + + TranslateStackToReg[NoOfCurFuncArgs] = size; + NoOfCurFuncArgs++; + +#ifdef FUNC_STRUCT_PARAM_AS_PTR + /* structs are passed as pointer */ + if ((type->t & VT_BTYPE) == VT_STRUCT) { + size = 4; + } +#endif + addr += size; + } + func_ret_sub = 0; + /* pascal type call ? */ + if (func_call == FUNC_STDCALL) + func_ret_sub = addr - 8; + + C67_MV(C67_FP, C67_A0); // move FP -> A0 + C67_MV(C67_SP, C67_FP); // move SP -> FP + + // place all the args passed in regs onto the stack + + loc = 0; + for (i = 0; i < NoOfCurFuncArgs; i++) { + + ParamLocOnStack[i] = loc; // remember where the param is + loc += -8; + + C67_PUSH(TREG_C67_A4 + i * 2); + + if (TranslateStackToReg[i] == 8) { + C67_STW_PTR_PRE_INC(TREG_C67_A4 + i * 2 + 1, C67_SP, 3); // STW r, *+SP[1] (go back and put the other) + } + } + + TotalBytesPushedOnStack = -loc; + + func_sub_sp_offset = ind; // remember where we put the stack instruction + C67_ADDK(0, C67_SP); // ADDK.L2 loc,SP (just put zero temporarily) + + C67_PUSH(C67_A0); + C67_PUSH(C67_B3); +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + { + int local = (-loc + 7) & -8; // stack must stay aligned to 8 bytes for LDDW instr + C67_POP(C67_B3); + C67_NOP(4); // NOP wait for load + C67_IREG_B_REG(0, C67_CREG_ZERO, C67_B3); // B.S2 B3 + C67_POP(C67_FP); + C67_ADDK(local, C67_SP); // ADDK.L2 loc,SP + C67_Adjust_ADDK((int *) (cur_text_section->data + + func_sub_sp_offset), + -local + TotalBytesPushedOnStack); + C67_NOP(3); // NOP + } +} + +/* generate a jump to a label */ +int gjmp(int t) +{ + int ind1 = ind; + if (nocode_wanted) + return t; + + C67_MVKL(C67_A0, t); //r=reg to load, constant + C67_MVKH(C67_A0, t); //r=reg to load, constant + C67_IREG_B_REG(0, C67_CREG_ZERO, C67_A0); // [!R] B.S2x A0 + C67_NOP(5); + return ind1; +} + +/* generate a jump to a fixed address */ +void gjmp_addr(int a) +{ + Sym *sym; + // I guess this routine is used for relative short + // local jumps, for now just handle it as the general + // case + + // define a label that will be relocated + + sym = get_sym_ref(&char_pointer_type, cur_text_section, a, 0); + greloc(cur_text_section, sym, ind, R_C60LO16); + greloc(cur_text_section, sym, ind + 4, R_C60HI16); + + gjmp(0); // place a zero there later the symbol will be added to it +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +int gtst(int inv, int t) +{ + int ind1, n; + int v, *p; + + v = vtop->r & VT_VALMASK; + if (nocode_wanted) { + ; + } else if (v == VT_CMP) { + /* fast case : can jump directly since flags are set */ + // C67 uses B2 sort of as flags register + ind1 = ind; + C67_MVKL(C67_A0, t); //r=reg to load, constant + C67_MVKH(C67_A0, t); //r=reg to load, constant + + if (C67_compare_reg != TREG_EAX && // check if not already in a conditional test reg + C67_compare_reg != TREG_EDX && + C67_compare_reg != TREG_ST0 && C67_compare_reg != C67_B2) { + C67_MV(C67_compare_reg, C67_B2); + C67_compare_reg = C67_B2; + } + + C67_IREG_B_REG(C67_invert_test ^ inv, C67_compare_reg, C67_A0); // [!R] B.S2x A0 + C67_NOP(5); + t = ind1; //return where we need to patch + + } else if (v == VT_JMP || v == VT_JMPI) { + /* && or || optimization */ + if ((v & 1) == inv) { + /* insert vtop->c jump list in t */ + + // I guess the idea is to traverse to the + // null at the end of the list and store t + // there + + n = vtop->c.i; + while (n != 0) { + p = (int *) (cur_text_section->data + n); + + // extract 32 bit address from MVKH/MVKL + n = ((*p >> 7) & 0xffff); + n |= ((*(p + 1) >> 7) & 0xffff) << 16; + } + *p |= (t & 0xffff) << 7; + *(p + 1) |= ((t >> 16) & 0xffff) << 7; + t = vtop->c.i; + + } else { + t = gjmp(t); + gsym(vtop->c.i); + } + } + vtop--; + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + int r, fr, opc, t; + + switch (op) { + case '+': + case TOK_ADDC1: /* add with carry generation */ + opc = 0; + gen_op8: + + +// C67 can't do const compares, must load into a reg +// so just go to gv2 directly - tktk + + + + if (op >= TOK_ULT && op <= TOK_GT) + gv2(RC_INT_BSIDE, RC_INT); // make sure r (src1) is on the B Side of CPU + else + gv2(RC_INT, RC_INT); + + r = vtop[-1].r; + fr = vtop[0].r; + + C67_compare_reg = C67_B2; + + + if (op == TOK_LT) { + C67_CMPLT(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_GE) { + C67_CMPLT(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_GT) { + C67_CMPGT(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_LE) { + C67_CMPGT(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_EQ) { + C67_CMPEQ(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_NE) { + C67_CMPEQ(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_ULT) { + C67_CMPLTU(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_UGE) { + C67_CMPLTU(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_UGT) { + C67_CMPGTU(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_ULE) { + C67_CMPGTU(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == '+') + C67_ADD(fr, r); // ADD r,fr,r + else if (op == '-') + C67_SUB(fr, r); // SUB r,fr,r + else if (op == '&') + C67_AND(fr, r); // AND r,fr,r + else if (op == '|') + C67_OR(fr, r); // OR r,fr,r + else if (op == '^') + C67_XOR(fr, r); // XOR r,fr,r + else + ALWAYS_ASSERT(FALSE); + + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) { + vtop->r = VT_CMP; + vtop->c.i = op; + } + break; + case '-': + case TOK_SUBC1: /* sub with carry generation */ + opc = 5; + goto gen_op8; + case TOK_ADDC2: /* add with carry use */ + opc = 2; + goto gen_op8; + case TOK_SUBC2: /* sub with carry use */ + opc = 3; + goto gen_op8; + case '&': + opc = 4; + goto gen_op8; + case '^': + opc = 6; + goto gen_op8; + case '|': + opc = 1; + goto gen_op8; + case '*': + case TOK_UMULL: + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_MPYI(fr, r); // 32 bit multiply fr,r,fr + C67_NOP(8); // NOP 8 for worst case + break; + case TOK_SHL: + gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_SHL(fr, r); // arithmetic/logical shift + break; + + case TOK_SHR: + gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_SHRU(fr, r); // logical shift + break; + + case TOK_SAR: + gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_SHR(fr, r); // arithmetic shift + break; + + case '/': + t = TOK__divi; + call_func: + vswap(); + /* call generic idiv function */ + vpush_global_sym(&func_old_type, t); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = REG_IRET; + vtop->r2 = VT_CONST; + break; + case TOK_UDIV: + case TOK_PDIV: + t = TOK__divu; + goto call_func; + case '%': + t = TOK__remi; + goto call_func; + case TOK_UMOD: + t = TOK__remu; + goto call_func; + + default: + opc = 7; + goto gen_op8; + } +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +/* XXX: need to use ST1 too */ +void gen_opf(int op) +{ + int ft, fc, fr, r; + + if (op >= TOK_ULT && op <= TOK_GT) + gv2(RC_EDX, RC_EAX); // make sure src2 is on b side + else + gv2(RC_FLOAT, RC_FLOAT); // make sure src2 is on b side + + ft = vtop->type.t; + fc = vtop->c.i; + r = vtop->r; + fr = vtop[-1].r; + + + if ((ft & VT_BTYPE) == VT_LDOUBLE) + tcc_error("long doubles not supported"); + + if (op >= TOK_ULT && op <= TOK_GT) { + + r = vtop[-1].r; + fr = vtop[0].r; + + C67_compare_reg = C67_B2; + + if (op == TOK_LT) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPLTDP(r, fr, C67_B2); + else + C67_CMPLTSP(r, fr, C67_B2); + + C67_invert_test = FALSE; + } else if (op == TOK_GE) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPLTDP(r, fr, C67_B2); + else + C67_CMPLTSP(r, fr, C67_B2); + + C67_invert_test = TRUE; + } else if (op == TOK_GT) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPGTDP(r, fr, C67_B2); + else + C67_CMPGTSP(r, fr, C67_B2); + + C67_invert_test = FALSE; + } else if (op == TOK_LE) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPGTDP(r, fr, C67_B2); + else + C67_CMPGTSP(r, fr, C67_B2); + + C67_invert_test = TRUE; + } else if (op == TOK_EQ) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPEQDP(r, fr, C67_B2); + else + C67_CMPEQSP(r, fr, C67_B2); + + C67_invert_test = FALSE; + } else if (op == TOK_NE) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPEQDP(r, fr, C67_B2); + else + C67_CMPEQSP(r, fr, C67_B2); + + C67_invert_test = TRUE; + } else { + ALWAYS_ASSERT(FALSE); + } + vtop->r = VT_CMP; // tell TCC that result is in "flags" actually B2 + } else { + if (op == '+') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + C67_ADDDP(r, fr); // ADD fr,r,fr + C67_NOP(6); + } else { + C67_ADDSP(r, fr); // ADD fr,r,fr + C67_NOP(3); + } + vtop--; + } else if (op == '-') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + C67_SUBDP(r, fr); // SUB fr,r,fr + C67_NOP(6); + } else { + C67_SUBSP(r, fr); // SUB fr,r,fr + C67_NOP(3); + } + vtop--; + } else if (op == '*') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + C67_MPYDP(r, fr); // MPY fr,r,fr + C67_NOP(9); + } else { + C67_MPYSP(r, fr); // MPY fr,r,fr + C67_NOP(3); + } + vtop--; + } else if (op == '/') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + // must call intrinsic DP floating point divide + vswap(); + /* call generic idiv function */ + vpush_global_sym(&func_old_type, TOK__divd); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = REG_FRET; + vtop->r2 = REG_LRET; + + } else { + // must call intrinsic SP floating point divide + vswap(); + /* call generic idiv function */ + vpush_global_sym(&func_old_type, TOK__divf); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = REG_FRET; + vtop->r2 = VT_CONST; + } + } else + ALWAYS_ASSERT(FALSE); + + + } +} + + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +void gen_cvt_itof(int t) +{ + int r; + + gv(RC_INT); + r = vtop->r; + + if ((t & VT_BTYPE) == VT_DOUBLE) { + if (t & VT_UNSIGNED) + C67_INTDPU(r, r); + else + C67_INTDP(r, r); + + C67_NOP(4); + vtop->type.t = VT_DOUBLE; + } else { + if (t & VT_UNSIGNED) + C67_INTSPU(r, r); + else + C67_INTSP(r, r); + C67_NOP(3); + vtop->type.t = VT_FLOAT; + } + +} + +/* convert fp to int 't' type */ +/* XXX: handle long long case */ +void gen_cvt_ftoi(int t) +{ + int r; + + gv(RC_FLOAT); + r = vtop->r; + + if (t != VT_INT) + tcc_error("long long not supported"); + else { + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) { + C67_DPTRUNC(r, r); + C67_NOP(3); + } else { + C67_SPTRUNC(r, r); + C67_NOP(3); + } + + vtop->type.t = VT_INT; + + } +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ + int r, r2; + + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE && + (t & VT_BTYPE) == VT_FLOAT) { + // convert double to float + + gv(RC_FLOAT); // get it in a register pair + + r = vtop->r; + + C67_DPSP(r, r); // convert it to SP same register + C67_NOP(3); + + vtop->type.t = VT_FLOAT; + vtop->r2 = VT_CONST; // set this as unused + } else if ((vtop->type.t & VT_BTYPE) == VT_FLOAT && + (t & VT_BTYPE) == VT_DOUBLE) { + // convert float to double + + gv(RC_FLOAT); // get it in a register + + r = vtop->r; + + if (r == TREG_EAX) { // make sure the paired reg is avail + r2 = get_reg(RC_ECX); + } else if (r == TREG_EDX) { + r2 = get_reg(RC_ST0); + } else { + ALWAYS_ASSERT(FALSE); + r2 = 0; /* avoid warning */ + } + + C67_SPDP(r, r); // convert it to DP same register + C67_NOP(1); + + vtop->type.t = VT_DOUBLE; + vtop->r2 = r2; // set this as unused + } else { + ALWAYS_ASSERT(FALSE); + } +} + +/* computed goto support */ +void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* Save the stack pointer onto the stack and return the location of its address */ +ST_FUNC void gen_vla_sp_save(int addr) { + tcc_error("variable length arrays unsupported for this target"); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + tcc_error("variable length arrays unsupported for this target"); +} + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { + tcc_error("variable length arrays unsupported for this target"); +} + +/* end of C67 code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final-old/c67-link.c b/05/tcc-final-old/c67-link.c new file mode 100644 index 0000000..de72e44 --- /dev/null +++ b/05/tcc-final-old/c67-link.c @@ -0,0 +1,131 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_C60 + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_C60_32 +#define R_DATA_PTR R_C60_32 +#define R_JMP_SLOT R_C60_JMP_SLOT +#define R_GLOB_DAT R_C60_GLOB_DAT +#define R_COPY R_C60_COPY +#define R_RELATIVE R_C60_RELATIVE + +#define R_NUM R_C60_NUM + +#define ELF_START_ADDR 0x00000400 +#define ELF_PAGE_SIZE 0x1000 + +#define PCRELATIVE_DLLPLT 0 +#define RELOCATE_DLLPLT 0 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_C60_32: + case R_C60LO16: + case R_C60HI16: + case R_C60_GOT32: + case R_C60_GOTOFF: + case R_C60_GOTPC: + case R_C60_COPY: + return 0; + + case R_C60_PLT32: + return 1; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_C60_32: + case R_C60LO16: + case R_C60HI16: + case R_C60_COPY: + return NO_GOTPLT_ENTRY; + + case R_C60_GOTOFF: + case R_C60_GOTPC: + return BUILD_GOT_ONLY; + + case R_C60_PLT32: + case R_C60_GOT32: + return ALWAYS_GOTPLT_ENTRY; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + tcc_error("C67 got not implemented"); + return 0; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + /* XXX: TODO */ + while (p < p_end) { + /* XXX: TODO */ + } + } +} + +void relocate_init(Section *sr) {} + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + switch(type) { + case R_C60_32: + *(int *)ptr += val; + break; + case R_C60LO16: + { + uint32_t orig; + + /* put the low 16 bits of the absolute address add to what is + already there */ + orig = ((*(int *)(ptr )) >> 7) & 0xffff; + orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16; + + /* patch both at once - assumes always in pairs Low - High */ + *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | + (((val+orig) & 0xffff) << 7); + *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | + ((((val+orig)>>16) & 0xffff) << 7); + } + break; + case R_C60HI16: + break; + default: + fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n", + type, (unsigned) addr, ptr, (unsigned) val); + break; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final-old/coff.h b/05/tcc-final-old/coff.h new file mode 100644 index 0000000..e8e6185 --- /dev/null +++ b/05/tcc-final-old/coff.h @@ -0,0 +1,446 @@ +/**************************************************************************/ +/* COFF.H */ +/* COFF data structures and related definitions used by the linker */ +/**************************************************************************/ + +/*------------------------------------------------------------------------*/ +/* COFF FILE HEADER */ +/*------------------------------------------------------------------------*/ +struct filehdr { + unsigned short f_magic; /* magic number */ + unsigned short f_nscns; /* number of sections */ + long f_timdat; /* time & date stamp */ + long f_symptr; /* file pointer to symtab */ + long f_nsyms; /* number of symtab entries */ + unsigned short f_opthdr; /* sizeof(optional hdr) */ + unsigned short f_flags; /* flags */ + unsigned short f_TargetID; /* for C6x = 0x0099 */ + }; + +/*------------------------------------------------------------------------*/ +/* File header flags */ +/*------------------------------------------------------------------------*/ +#define F_RELFLG 0x01 /* relocation info stripped from file */ +#define F_EXEC 0x02 /* file is executable (no unresolved refs) */ +#define F_LNNO 0x04 /* line numbers stripped from file */ +#define F_LSYMS 0x08 /* local symbols stripped from file */ +#define F_GSP10 0x10 /* 34010 version */ +#define F_GSP20 0x20 /* 34020 version */ +#define F_SWABD 0x40 /* bytes swabbed (in names) */ +#define F_AR16WR 0x80 /* byte ordering of an AR16WR (PDP-11) */ +#define F_LITTLE 0x100 /* byte ordering of an AR32WR (vax) */ +#define F_BIG 0x200 /* byte ordering of an AR32W (3B, maxi) */ +#define F_PATCH 0x400 /* contains "patch" list in optional header */ +#define F_NODF 0x400 + +#define F_VERSION (F_GSP10 | F_GSP20) +#define F_BYTE_ORDER (F_LITTLE | F_BIG) +#define FILHDR struct filehdr + +/* #define FILHSZ sizeof(FILHDR) */ +#define FILHSZ 22 /* above rounds to align on 4 bytes which causes problems */ + +#define COFF_C67_MAGIC 0x00c2 + +/*------------------------------------------------------------------------*/ +/* Macros to recognize magic numbers */ +/*------------------------------------------------------------------------*/ +#define ISMAGIC(x) (((unsigned short)(x))==(unsigned short)magic) +#define ISARCHIVE(x) ((((unsigned short)(x))==(unsigned short)ARTYPE)) +#define BADMAGIC(x) (((unsigned short)(x) & 0x8080) && !ISMAGIC(x)) + + +/*------------------------------------------------------------------------*/ +/* OPTIONAL FILE HEADER */ +/*------------------------------------------------------------------------*/ +typedef struct aouthdr { + short magic; /* see magic.h */ + short vstamp; /* version stamp */ + long tsize; /* text size in bytes, padded to FW bdry*/ + long dsize; /* initialized data " " */ + long bsize; /* uninitialized data " " */ + long entrypt; /* entry pt. */ + long text_start; /* base of text used for this file */ + long data_start; /* base of data used for this file */ +} AOUTHDR; + +#define AOUTSZ sizeof(AOUTHDR) + +/*----------------------------------------------------------------------*/ +/* When a UNIX aout header is to be built in the optional header, */ +/* the following magic numbers can appear in that header: */ +/* */ +/* AOUT1MAGIC : default : readonly sharable text segment */ +/* AOUT2MAGIC: : writable text segment */ +/* PAGEMAGIC : : configured for paging */ +/*----------------------------------------------------------------------*/ +#define AOUT1MAGIC 0410 +#define AOUT2MAGIC 0407 +#define PAGEMAGIC 0413 + + +/*------------------------------------------------------------------------*/ +/* COMMON ARCHIVE FILE STRUCTURES */ +/* */ +/* ARCHIVE File Organization: */ +/* _______________________________________________ */ +/* |__________ARCHIVE_MAGIC_STRING_______________| */ +/* |__________ARCHIVE_FILE_MEMBER_1______________| */ +/* | | */ +/* | Archive File Header "ar_hdr" | */ +/* |.............................................| */ +/* | Member Contents | */ +/* | 1. External symbol directory | */ +/* | 2. Text file | */ +/* |_____________________________________________| */ +/* |________ARCHIVE_FILE_MEMBER_2________________| */ +/* | "ar_hdr" | */ +/* |.............................................| */ +/* | Member Contents (.o or text file) | */ +/* |_____________________________________________| */ +/* | . . . | */ +/* | . . . | */ +/* | . . . | */ +/* |_____________________________________________| */ +/* |________ARCHIVE_FILE_MEMBER_n________________| */ +/* | "ar_hdr" | */ +/* |.............................................| */ +/* | Member Contents | */ +/* |_____________________________________________| */ +/* */ +/*------------------------------------------------------------------------*/ + +#define COFF_ARMAG "!\n" +#define SARMAG 8 +#define ARFMAG "`\n" + +struct ar_hdr /* archive file member header - printable ascii */ +{ + char ar_name[16]; /* file member name - `/' terminated */ + char ar_date[12]; /* file member date - decimal */ + char ar_uid[6]; /* file member user id - decimal */ + char ar_gid[6]; /* file member group id - decimal */ + char ar_mode[8]; /* file member mode - octal */ + char ar_size[10]; /* file member size - decimal */ + char ar_fmag[2]; /* ARFMAG - string to end header */ +}; + + +/*------------------------------------------------------------------------*/ +/* SECTION HEADER */ +/*------------------------------------------------------------------------*/ +struct scnhdr { + char s_name[8]; /* section name */ + long s_paddr; /* physical address */ + long s_vaddr; /* virtual address */ + long s_size; /* section size */ + long s_scnptr; /* file ptr to raw data for section */ + long s_relptr; /* file ptr to relocation */ + long s_lnnoptr; /* file ptr to line numbers */ + unsigned int s_nreloc; /* number of relocation entries */ + unsigned int s_nlnno; /* number of line number entries */ + unsigned int s_flags; /* flags */ + unsigned short s_reserved; /* reserved byte */ + unsigned short s_page; /* memory page id */ + }; + +#define SCNHDR struct scnhdr +#define SCNHSZ sizeof(SCNHDR) + +/*------------------------------------------------------------------------*/ +/* Define constants for names of "special" sections */ +/*------------------------------------------------------------------------*/ +/* #define _TEXT ".text" */ +#define _DATA ".data" +#define _BSS ".bss" +#define _CINIT ".cinit" +#define _TV ".tv" + +/*------------------------------------------------------------------------*/ +/* The low 4 bits of s_flags is used as a section "type" */ +/*------------------------------------------------------------------------*/ +#define STYP_REG 0x00 /* "regular" : allocated, relocated, loaded */ +#define STYP_DSECT 0x01 /* "dummy" : not allocated, relocated, not loaded */ +#define STYP_NOLOAD 0x02 /* "noload" : allocated, relocated, not loaded */ +#define STYP_GROUP 0x04 /* "grouped" : formed of input sections */ +#define STYP_PAD 0x08 /* "padding" : not allocated, not relocated, loaded */ +#define STYP_COPY 0x10 /* "copy" : used for C init tables - + not allocated, relocated, + loaded; reloc & lineno + entries processed normally */ +#define STYP_TEXT 0x20 /* section contains text only */ +#define STYP_DATA 0x40 /* section contains data only */ +#define STYP_BSS 0x80 /* section contains bss only */ + +#define STYP_ALIGN 0x100 /* align flag passed by old version assemblers */ +#define ALIGN_MASK 0x0F00 /* part of s_flags that is used for align vals */ +#define ALIGNSIZE(x) (1 << ((x & ALIGN_MASK) >> 8)) + + +/*------------------------------------------------------------------------*/ +/* RELOCATION ENTRIES */ +/*------------------------------------------------------------------------*/ +struct reloc +{ + long r_vaddr; /* (virtual) address of reference */ + short r_symndx; /* index into symbol table */ + unsigned short r_disp; /* additional bits for address calculation */ + unsigned short r_type; /* relocation type */ +}; + +#define RELOC struct reloc +#define RELSZ 10 /* sizeof(RELOC) */ + +/*--------------------------------------------------------------------------*/ +/* define all relocation types */ +/*--------------------------------------------------------------------------*/ + +#define R_ABS 0 /* absolute address - no relocation */ +#define R_DIR16 01 /* UNUSED */ +#define R_REL16 02 /* UNUSED */ +#define R_DIR24 04 /* UNUSED */ +#define R_REL24 05 /* 24 bits, direct */ +#define R_DIR32 06 /* UNUSED */ +#define R_RELBYTE 017 /* 8 bits, direct */ +#define R_RELWORD 020 /* 16 bits, direct */ +#define R_RELLONG 021 /* 32 bits, direct */ +#define R_PCRBYTE 022 /* 8 bits, PC-relative */ +#define R_PCRWORD 023 /* 16 bits, PC-relative */ +#define R_PCRLONG 024 /* 32 bits, PC-relative */ +#define R_OCRLONG 030 /* GSP: 32 bits, one's complement direct */ +#define R_GSPPCR16 031 /* GSP: 16 bits, PC relative (in words) */ +#define R_GSPOPR32 032 /* GSP: 32 bits, direct big-endian */ +#define R_PARTLS16 040 /* Brahma: 16 bit offset of 24 bit address*/ +#define R_PARTMS8 041 /* Brahma: 8 bit page of 24 bit address */ +#define R_PARTLS7 050 /* DSP: 7 bit offset of 16 bit address */ +#define R_PARTMS9 051 /* DSP: 9 bit page of 16 bit address */ +#define R_REL13 052 /* DSP: 13 bits, direct */ + + +/*------------------------------------------------------------------------*/ +/* LINE NUMBER ENTRIES */ +/*------------------------------------------------------------------------*/ +struct lineno +{ + union + { + long l_symndx ; /* sym. table index of function name + iff l_lnno == 0 */ + long l_paddr ; /* (physical) address of line number */ + } l_addr ; + unsigned short l_lnno ; /* line number */ +}; + +#define LINENO struct lineno +#define LINESZ 6 /* sizeof(LINENO) */ + + +/*------------------------------------------------------------------------*/ +/* STORAGE CLASSES */ +/*------------------------------------------------------------------------*/ +#define C_EFCN -1 /* physical end of function */ +#define C_NULL 0 +#define C_AUTO 1 /* automatic variable */ +#define C_EXT 2 /* external symbol */ +#define C_STAT 3 /* static */ +#define C_REG 4 /* register variable */ +#define C_EXTDEF 5 /* external definition */ +#define C_LABEL 6 /* label */ +#define C_ULABEL 7 /* undefined label */ +#define C_MOS 8 /* member of structure */ +#define C_ARG 9 /* function argument */ +#define C_STRTAG 10 /* structure tag */ +#define C_MOU 11 /* member of union */ +#define C_UNTAG 12 /* union tag */ +#define C_TPDEF 13 /* type definition */ +#define C_USTATIC 14 /* undefined static */ +#define C_ENTAG 15 /* enumeration tag */ +#define C_MOE 16 /* member of enumeration */ +#define C_REGPARM 17 /* register parameter */ +#define C_FIELD 18 /* bit field */ + +#define C_BLOCK 100 /* ".bb" or ".eb" */ +#define C_FCN 101 /* ".bf" or ".ef" */ +#define C_EOS 102 /* end of structure */ +#define C_FILE 103 /* file name */ +#define C_LINE 104 /* dummy sclass for line number entry */ +#define C_ALIAS 105 /* duplicate tag */ +#define C_HIDDEN 106 /* special storage class for external */ + /* symbols in dmert public libraries */ + +/*------------------------------------------------------------------------*/ +/* SYMBOL TABLE ENTRIES */ +/*------------------------------------------------------------------------*/ + +#define SYMNMLEN 8 /* Number of characters in a symbol name */ +#define FILNMLEN 14 /* Number of characters in a file name */ +#define DIMNUM 4 /* Number of array dimensions in auxiliary entry */ + + +struct syment +{ + union + { + char _n_name[SYMNMLEN]; /* old COFF version */ + struct + { + long _n_zeroes; /* new == 0 */ + long _n_offset; /* offset into string table */ + } _n_n; + char *_n_nptr[2]; /* allows for overlaying */ + } _n; + long n_value; /* value of symbol */ + short n_scnum; /* section number */ + unsigned short n_type; /* type and derived type */ + char n_sclass; /* storage class */ + char n_numaux; /* number of aux. entries */ +}; + +#define n_name _n._n_name +#define n_nptr _n._n_nptr[1] +#define n_zeroes _n._n_n._n_zeroes +#define n_offset _n._n_n._n_offset + +/*------------------------------------------------------------------------*/ +/* Relocatable symbols have a section number of the */ +/* section in which they are defined. Otherwise, section */ +/* numbers have the following meanings: */ +/*------------------------------------------------------------------------*/ +#define N_UNDEF 0 /* undefined symbol */ +#define N_ABS -1 /* value of symbol is absolute */ +#define N_DEBUG -2 /* special debugging symbol */ +#define N_TV (unsigned short)-3 /* needs transfer vector (preload) */ +#define P_TV (unsigned short)-4 /* needs transfer vector (postload) */ + + +/*------------------------------------------------------------------------*/ +/* The fundamental type of a symbol packed into the low */ +/* 4 bits of the word. */ +/*------------------------------------------------------------------------*/ +#define _EF ".ef" + +#define T_NULL 0 /* no type info */ +#define T_ARG 1 /* function argument (only used by compiler) */ +#define T_CHAR 2 /* character */ +#define T_SHORT 3 /* short integer */ +#define T_INT 4 /* integer */ +#define T_LONG 5 /* long integer */ +#define T_FLOAT 6 /* floating point */ +#define T_DOUBLE 7 /* double word */ +#define T_STRUCT 8 /* structure */ +#define T_UNION 9 /* union */ +#define T_ENUM 10 /* enumeration */ +#define T_MOE 11 /* member of enumeration */ +#define T_UCHAR 12 /* unsigned character */ +#define T_USHORT 13 /* unsigned short */ +#define T_UINT 14 /* unsigned integer */ +#define T_ULONG 15 /* unsigned long */ + +/*------------------------------------------------------------------------*/ +/* derived types are: */ +/*------------------------------------------------------------------------*/ +#define DT_NON 0 /* no derived type */ +#define DT_PTR 1 /* pointer */ +#define DT_FCN 2 /* function */ +#define DT_ARY 3 /* array */ + +#define MKTYPE(basic, d1,d2,d3,d4,d5,d6) \ + ((basic) | ((d1) << 4) | ((d2) << 6) | ((d3) << 8) |\ + ((d4) << 10) | ((d5) << 12) | ((d6) << 14)) + +/*------------------------------------------------------------------------*/ +/* type packing constants and macros */ +/*------------------------------------------------------------------------*/ +#define N_BTMASK_COFF 017 +#define N_TMASK_COFF 060 +#define N_TMASK1_COFF 0300 +#define N_TMASK2_COFF 0360 +#define N_BTSHFT_COFF 4 +#define N_TSHIFT_COFF 2 + +#define BTYPE_COFF(x) ((x) & N_BTMASK_COFF) +#define ISINT(x) (((x) >= T_CHAR && (x) <= T_LONG) || \ + ((x) >= T_UCHAR && (x) <= T_ULONG) || (x) == T_ENUM) +#define ISFLT_COFF(x) ((x) == T_DOUBLE || (x) == T_FLOAT) +#define ISPTR_COFF(x) (((x) & N_TMASK_COFF) == (DT_PTR << N_BTSHFT_COFF)) +#define ISFCN_COFF(x) (((x) & N_TMASK_COFF) == (DT_FCN << N_BTSHFT_COFF)) +#define ISARY_COFF(x) (((x) & N_TMASK_COFF) == (DT_ARY << N_BTSHFT_COFF)) +#define ISTAG_COFF(x) ((x)==C_STRTAG || (x)==C_UNTAG || (x)==C_ENTAG) + +#define INCREF_COFF(x) ((((x)&~N_BTMASK_COFF)<>N_TSHIFT_COFF)&~N_BTMASK_COFF)|((x)&N_BTMASK_COFF)) + + +/*------------------------------------------------------------------------*/ +/* AUXILIARY SYMBOL ENTRY */ +/*------------------------------------------------------------------------*/ +union auxent +{ + struct + { + long x_tagndx; /* str, un, or enum tag indx */ + union + { + struct + { + unsigned short x_lnno; /* declaration line number */ + unsigned short x_size; /* str, union, array size */ + } x_lnsz; + long x_fsize; /* size of function */ + } x_misc; + union + { + struct /* if ISFCN, tag, or .bb */ + { + long x_lnnoptr; /* ptr to fcn line # */ + long x_endndx; /* entry ndx past block end */ + } x_fcn; + struct /* if ISARY, up to 4 dimen. */ + { + unsigned short x_dimen[DIMNUM]; + } x_ary; + } x_fcnary; + unsigned short x_regcount; /* number of registers used by func */ + } x_sym; + struct + { + char x_fname[FILNMLEN]; + } x_file; + struct + { + long x_scnlen; /* section length */ + unsigned short x_nreloc; /* number of relocation entries */ + unsigned short x_nlinno; /* number of line numbers */ + } x_scn; +}; + +#define SYMENT struct syment +#define SYMESZ 18 /* sizeof(SYMENT) */ + +#define AUXENT union auxent +#define AUXESZ 18 /* sizeof(AUXENT) */ + +/*------------------------------------------------------------------------*/ +/* NAMES OF "SPECIAL" SYMBOLS */ +/*------------------------------------------------------------------------*/ +#define _STEXT ".text" +#define _ETEXT "etext" +#define _SDATA ".data" +#define _EDATA "edata" +#define _SBSS ".bss" +#define _END "end" +#define _CINITPTR "cinit" + +/*--------------------------------------------------------------------------*/ +/* ENTRY POINT SYMBOLS */ +/*--------------------------------------------------------------------------*/ +#define _START "_start" +#define _MAIN "_main" + /* _CSTART "_c_int00" (defined in params.h) */ + + +#define _TVORIG "_tvorig" +#define _TORIGIN "_torigin" +#define _DORIGIN "_dorigin" + +#define _SORIGIN "_sorigin" diff --git a/05/tcc-final-old/config.mak b/05/tcc-final-old/config.mak new file mode 100644 index 0000000..8d2d4fc --- /dev/null +++ b/05/tcc-final-old/config.mak @@ -0,0 +1,24 @@ +# Automatically generated by configure - do not modify +prefix=/usr/local +bindir=$(DESTDIR)/usr/local/bin +tccdir=$(DESTDIR)/usr/local/lib/tcc +libdir=$(DESTDIR)/usr/local/lib +includedir=$(DESTDIR)/usr/local/include +mandir=$(DESTDIR)/usr/local/share/man +infodir=$(DESTDIR)/usr/local/share/info +docdir=$(DESTDIR)/usr/local/share/doc +CC=gcc +GCC_MAJOR=11 +GCC_MINOR=4 +AR=ar +STRIP=strip -s -R .comment -R .note +CFLAGS=-Wall -g -O2 -Wdeclaration-after-statement -fno-strict-aliasing -Wno-pointer-sign -Wno-sign-compare -Wno-unused-result +LDFLAGS= +LIBSUF=.a +EXESUF= +DLLSUF=.so +NATIVE_DEFINES+=-DCONFIG_TRIPLET="\"x86_64-linux-gnu\"" +ARCH=x86_64 +TARGETOS=Linux +VERSION = 0.9.27 +TOPSRC=$(TOP) diff --git a/05/tcc-final-old/configure b/05/tcc-final-old/configure new file mode 100755 index 0000000..1ee3acb --- /dev/null +++ b/05/tcc-final-old/configure @@ -0,0 +1,527 @@ +#!/bin/sh +# +# tcc configure script (c) 2003 Fabrice Bellard + +# set temporary file name +# if test ! -z "$TMPDIR" ; then +# TMPDIR1="${TMPDIR}" +# elif test ! -z "$TEMPDIR" ; then +# TMPDIR1="${TEMPDIR}" +# else +# TMPDIR1="/tmp" +# fi +# +# bashism: TMPN="${TMPDIR1}/tcc-conf-${RANDOM}-$$-${RANDOM}.c" + +TMPN="./conftest-$$" +TMPH=$TMPN.h + +# default parameters +prefix="" +execprefix="" +bindir="" +libdir="" +tccdir="" +includedir="" +mandir="" +infodir="" +sysroot="" +cross_prefix="" +cc="gcc" +ar="ar" +strip="strip" +bigendian="no" +mingw32="no" +LIBSUF=".a" +EXESUF="" +DLLSUF=".so" +tcc_sysincludepaths="" +tcc_libpaths="" +tcc_crtprefix="" +tcc_elfinterp="" +triplet= +tcc_lddir= +confvars= +suggest="yes" +cpu= +cpuver= +gcc_major=0 +gcc_minor=0 + +# OS specific +targetos=`uname` +case $targetos in + Darwin) + confvars="$confvars OSX" + DLLSUF=".dylib" + ;; + MINGW*|MSYS*|CYGWIN*) + mingw32=yes + ;; + DragonFly|OpenBSD|FreeBSD|NetBSD) + confvars="$confvars ldl=no" + ;; + *) + ;; +esac + +# find source path +source_path=${0%configure} +source_path=${source_path%/} +source_path_used="yes" +if test -z "$source_path" -o "$source_path" = "." ; then + source_path=`pwd` + source_path_used="no" +fi + +for opt do + eval opt=\"$opt\" + case "$opt" in + --prefix=*) prefix=`echo $opt | cut -d '=' -f 2` + ;; + --exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2` + ;; + --tccdir=*) tccdir=`echo $opt | cut -d '=' -f 2` + ;; + --bindir=*) bindir=`echo $opt | cut -d '=' -f 2` + ;; + --libdir=*) libdir=`echo $opt | cut -d '=' -f 2` + ;; + --includedir=*) includedir=`echo $opt | cut -d '=' -f 2` + ;; + --sharedir=*) sharedir=`echo $opt | cut -d '=' -f 2` + ;; + --mandir=*) mandir=`echo $opt | cut -d '=' -f 2` + ;; + --infodir=*) infodir=`echo $opt | cut -d '=' -f 2` + ;; + --docdir=*) docdir=`echo $opt | cut -d '=' -f 2` + ;; + --sysroot=*) sysroot=`echo $opt | cut -d '=' -f 2` + ;; + --source-path=*) source_path=`echo $opt | cut -d '=' -f 2` + ;; + --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2` + ;; + --cc=*) cc=`echo $opt | cut -d '=' -f 2` + ;; + --ar=*) ar=`echo $opt | cut -d '=' -f 2` + ;; + --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}" + ;; + --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}" + ;; + --extra-libs=*) extralibs="${opt#--extra-libs=}" + ;; + --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2` + ;; + --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2` + ;; + --crtprefix=*) tcc_crtprefix=`echo $opt | cut -d '=' -f 2` + ;; + --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2` + ;; + --triplet=*) triplet=`echo $opt | cut -d '=' -f 2` + ;; + --cpu=*) cpu=`echo $opt | cut -d '=' -f 2` + ;; + --enable-cross) confvars="$confvars cross" + ;; + --disable-static) confvars="$confvars static=no" + ;; + --enable-static) confvars="$confvars static" + ;; + --disable-rpath) confvars="$confvars rpath=no" + ;; + --strip-binaries) confvars="$confvars strip" + ;; + --with-libgcc) confvars="$confvars libgcc" + ;; + --with-selinux) confvars="$confvars selinux" + ;; + --config-mingw32*) mingw32=$(echo "$opt=yes" | cut -d '=' -f 2) + ;; + --config-*) confvars="$confvars ${opt#--config-}"; suggest="no" + ;; + --help|-h) show_help="yes" + ;; + *) echo "configure: WARNING: unrecognized option $opt" + ;; + esac +done + +if test -z "$cpu" ; then + if test -n "$ARCH" ; then + cpu="$ARCH" + else + cpu=`uname -m` + fi +fi + +case "$cpu" in + x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386) + cpu="i386" + ;; + x86_64|amd64|x86-64) + cpu="x86_64" + ;; + arm*) + case "$cpu" in + arm|armv4l) + cpuver=4 + ;; + armv5tel|armv5tejl) + cpuver=5 + ;; + armv6j|armv6l) + cpuver=6 + ;; + armv7a|armv7l) + cpuver=7 + ;; + esac + cpu="arm" + ;; + aarch64) + cpu="aarch64" + ;; + alpha) + cpu="alpha" + ;; + "Power Macintosh"|ppc|ppc64) + cpu="ppc" + ;; + mips) + cpu="mips" + ;; + s390) + cpu="s390" + ;; + *) + echo "Unsupported CPU" + exit 1 + ;; +esac + +# Checking for CFLAGS +if test -z "$CFLAGS"; then + CFLAGS="-Wall -g -O2" +fi + +if test "$mingw32" = "yes" ; then + if test "$source_path_used" = "no"; then + source_path="." + fi + if test "$cc" = gcc; then + test -z "$LDFLAGS" && LDFLAGS="-static" + fi + test -z "$prefix" && prefix="C:/Program Files/tcc" + test -z "$tccdir" && tccdir="${prefix}" + test -z "$bindir" && bindir="${tccdir}" + test -z "$docdir" && docdir="${tccdir}/doc" + test -z "$libdir" && libdir="${tccdir}/libtcc" + confvars="$confvars WIN32" + LIBSUF=".lib" + EXESUF=".exe" + DLLSUF=".dll" +else + if test -z "$prefix" ; then + prefix="/usr/local" + fi + if test -z "$sharedir" ; then + sharedir="${prefix}/share" + fi + if test x"$execprefix" = x""; then + execprefix="${prefix}" + fi + if test x"$libdir" = x""; then + libdir="${execprefix}/lib" + fi + if test x"$bindir" = x""; then + bindir="${execprefix}/bin" + fi + if test x"$docdir" = x""; then + docdir="${sharedir}/doc" + fi + if test x"$mandir" = x""; then + mandir="${sharedir}/man" + fi + if test x"$infodir" = x""; then + infodir="${sharedir}/info" + fi + if test x"$tccdir" = x""; then + tccdir="${libdir}/tcc" + fi + if test x"$includedir" = x""; then + includedir="${prefix}/include" + fi +fi # mingw32 + +if test x"$show_help" = "xyes" ; then +cat << EOF +Usage: configure [options] +Options: [defaults in brackets after descriptions] + +Standard options: + --help print this message + --prefix=PREFIX install in PREFIX [$prefix] + --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX + [same as prefix] + --bindir=DIR user executables in DIR [EPREFIX/bin] + --libdir=DIR object code libraries in DIR [EPREFIX/lib] + --tccdir=DIR installation directory [EPREFIX/lib/tcc] + --includedir=DIR C header files in DIR [PREFIX/include] + --sharedir=DIR documentation root DIR [PREFIX/share] + --docdir=DIR documentation in DIR [SHAREDIR/doc/tcc] + --mandir=DIR man documentation in DIR [SHAREDIR/man] + --infodir=DIR info documentation in DIR [SHAREDIR/info] + +Advanced options (experts only): + --source-path=PATH path of source code [$source_path] + --cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix] + --sysroot=PREFIX prepend PREFIX to library/include paths [] + --cc=CC use C compiler CC [$cc] + --ar=AR create archives using AR [$ar] + --extra-cflags= specify compiler flags [$CFLAGS] + --extra-ldflags= specify linker options [] + --cpu=CPU CPU [$cpu] + --strip-binaries strip symbol tables from resulting binaries + --disable-static make libtcc.so instead of libtcc.a + --enable-static make libtcc.a instead of libtcc.dll (win32) + --disable-rpath disable use of -rpath with the above + --with-libgcc use libgcc_s.so.1 instead of libtcc1.a + --enable-cross build cross compilers + --with-selinux use mmap for executable memory (with tcc -run) + --sysincludepaths=... specify system include paths, colon separated + --libpaths=... specify system library paths, colon separated + --crtprefix=... specify locations of crt?.o, colon separated + --elfinterp=... specify elf interpreter + --triplet=... specify system library/include directory triplet + --config-uClibc,-musl,-mingw32... enable system specific configurations +EOF +#echo "NOTE: The object files are build at the place where configure is launched" +exit 1 +fi + +cc="${cross_prefix}${cc}" +ar="${cross_prefix}${ar}" +strip="${cross_prefix}${strip}" + +if test -z "$cross_prefix" ; then + CONFTEST=./conftest$EXESUF + if ! $cc -o $CONFTEST $source_path/conftest.c 2>/dev/null ; then + echo "configure: error: '$cc' failed to compile conftest.c." + else + gcc_major="$($CONFTEST version)" + gcc_minor="$($CONFTEST minor)" + fi + bigendian="$($CONFTEST bigendian)" + if test "$mingw32" = "no" ; then + + if test -z "$triplet"; then + tt="$($CONFTEST triplet)" + if test -n "$tt" -a -f "/usr/lib/$tt/crti.o" ; then + triplet="$tt" + fi + fi + + if test -z "$triplet"; then + if test $cpu = "x86_64" -o $cpu = "aarch64" ; then + if test -f "/usr/lib64/crti.o" ; then + tcc_lddir="lib64" + fi + fi + fi + + if test "$cpu" = "arm" ; then + if test "${triplet%eabihf}" != "$triplet" ; then + confvars="$confvars arm_eabihf" + elif test "${triplet%eabi}" != "$triplet" ; then + confvars="$confvars arm_eabi" + fi + if grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then + confvars="$confvars arm_vfp" + fi + fi + + if test "$suggest" = "yes"; then + if test -f "/lib/ld-uClibc.so.0" ; then + echo "Perhaps you want ./configure --config-uClibc" + fi + if test -f "/lib/ld-musl-$cpu.so.1"; then + echo "Perhaps you want ./configure --config-musl" + fi + fi + fi +else + # if cross compiling, cannot launch a program, so make a static guess + case $cpu in + ppc|mips|s390) bigendian=yes;; + esac +fi + +if test "$bigendian" = "yes" ; then + confvars="$confvars BIGENDIAN" +fi + +# a final configuration tuning +if ! echo "$cc" | grep -q "tcc"; then + OPT1="-Wdeclaration-after-statement -fno-strict-aliasing" + # we want -Wno- but gcc does not always reject unknown -Wno- options + OPT2="-Wpointer-sign -Wsign-compare -Wunused-result" + if echo "$cc" | grep -q "clang"; then + OPT1="$OPT1 -fheinous-gnu-extensions" + OPT2="$OPT2 -Wstring-plus-int" + fi + $cc $OPT1 $OPT2 -o a.out -c -xc - < /dev/null > cc_msg.txt 2>&1 + for o in $OPT1; do # enable these options + if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS $o"; fi + done + for o in $OPT2; do # disable these options + if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS -Wno-${o#-W*}"; fi + done + # cat cc_msg.txt + # echo $CFLAGS + rm -f cc_msg.txt a.out +fi + +fcho() { if test -n "$2"; then echo "$1$2"; fi } + +fcho "Binary directory " "$bindir" +fcho "TinyCC directory " "$tccdir" +fcho "Library directory " "$libdir" +fcho "Include directory " "$includedir" +fcho "Manual directory " "$mandir" +fcho "Info directory " "$infodir" +fcho "Doc directory " "$docdir" +fcho "Target root prefix " "$sysroot" +echo "Source path $source_path" +echo "C compiler $cc ($gcc_major.$gcc_minor)" +echo "Target OS $targetos" +echo "CPU $cpu" +fcho "Triplet " "$triplet" +fcho "Config " "${confvars# }" +echo "Creating config.mak and config.h" + +cat >config.mak <> $TMPH + echo "# define $1 \"$2\"" >> $TMPH + echo "#endif" >> $TMPH + fi +} + +print_mak() { + if test -n "$2"; then + echo "NATIVE_DEFINES+=-D$1=\"\\\"$2\\\"\"" >> config.mak + fi +} + +print_mak_int() { + if test -n "$2"; then + echo "NATIVE_DEFINES+=-D$1=$2" >> config.mak + fi +} + +echo "/* Automatically generated by configure - do not modify */" > $TMPH + +print_inc CONFIG_SYSROOT "$sysroot" +print_inc CONFIG_TCCDIR "$tccdir" +print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths" +print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths" +print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix" +print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp" +print_mak CONFIG_LDDIR "$tcc_lddir" +print_mak CONFIG_TRIPLET "$triplet" +print_mak_int TCC_CPU_VERSION "$cpuver" + +if test "$cpu" = "aarch64" ; then + echo "ARCH=arm64" >> config.mak +else + echo "ARCH=$cpu" >> config.mak +fi +echo "TARGETOS=$targetos" >> config.mak + +for v in $confvars ; do + if test "${v%=*}" = "$v"; then + echo "CONFIG_$v=yes" >> config.mak + else + echo "CONFIG_$v" >> config.mak + fi +done + +version=`head $source_path/VERSION` +echo "VERSION = $version" >> config.mak +echo "#define TCC_VERSION \"$version\"" >> $TMPH +echo "@set VERSION $version" > config.texi + +if test "$source_path_used" = "yes" ; then + case $source_path in + /*) echo "TOPSRC=$source_path";; + *) echo "TOPSRC=\$(TOP)/$source_path";; + esac >>config.mak +else + echo 'TOPSRC=$(TOP)' >>config.mak +fi + +diff $TMPH config.h >/dev/null 2>&1 +if test $? -ne 0 ; then + mv -f $TMPH config.h +else + echo "config.h is unchanged" +fi + +rm -f $TMPN* $CONFTEST + +# --------------------------------------------------------------------------- +# build tree in object directory if source path is different from current one + +fn_makelink() +{ + tgt=$1/$2 + case $2 in + */*) dn=${2%/*} + test -d $dn || mkdir -p $dn + case $1 in + /*) ;; + *) while test $dn ; do + tgt=../$tgt; dn=${dn#${dn%%/*}}; dn=${dn#/} + done + ;; + esac + ;; + esac + + ln -sfn $tgt $2 || ( echo "ln failed. Using cp instead."; cp -f $1/$2 $2 ) +} + +if test "$source_path_used" = "yes" ; then + FILES="Makefile lib/Makefile tests/Makefile tests/tests2/Makefile tests/pp/Makefile" + for f in $FILES ; do + fn_makelink $source_path $f + done +fi + +# --------------------------------------------------------------------------- diff --git a/05/tcc-final-old/elf.h b/05/tcc-final-old/elf.h new file mode 100644 index 0000000..9fed6eb --- /dev/null +++ b/05/tcc-final-old/elf.h @@ -0,0 +1,3237 @@ +/* This file defines standard ELF types, structures, and macros. + Copyright (C) 1995-2012 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _ELF_H +#define _ELF_H 1 + +#ifndef _WIN32 +#include +#else +#ifndef __int8_t_defined +#define __int8_t_defined +typedef signed char int8_t; +typedef short int int16_t; +typedef int int32_t; +typedef long long int int64_t; +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long int uint64_t; +#endif +#endif + +/* Standard ELF types. */ + +/* Type for a 16-bit quantity. */ +typedef uint16_t Elf32_Half; +typedef uint16_t Elf64_Half; + +/* Types for signed and unsigned 32-bit quantities. */ +typedef uint32_t Elf32_Word; +typedef int32_t Elf32_Sword; +typedef uint32_t Elf64_Word; +typedef int32_t Elf64_Sword; + +/* Types for signed and unsigned 64-bit quantities. */ +typedef uint64_t Elf32_Xword; +typedef int64_t Elf32_Sxword; +typedef uint64_t Elf64_Xword; +typedef int64_t Elf64_Sxword; + +/* Type of addresses. */ +typedef uint32_t Elf32_Addr; +typedef uint64_t Elf64_Addr; + +/* Type of file offsets. */ +typedef uint32_t Elf32_Off; +typedef uint64_t Elf64_Off; + +/* Type for section indices, which are 16-bit quantities. */ +typedef uint16_t Elf32_Section; +typedef uint16_t Elf64_Section; + +/* Type for version symbol information. */ +typedef Elf32_Half Elf32_Versym; +typedef Elf64_Half Elf64_Versym; + + +/* The ELF file header. This appears at the start of every ELF file. */ + +#define EI_NIDENT (16) + +typedef struct +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf32_Half e_type; /* Object file type */ + Elf32_Half e_machine; /* Architecture */ + Elf32_Word e_version; /* Object file version */ + Elf32_Addr e_entry; /* Entry point virtual address */ + Elf32_Off e_phoff; /* Program header table file offset */ + Elf32_Off e_shoff; /* Section header table file offset */ + Elf32_Word e_flags; /* Processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size in bytes */ + Elf32_Half e_phentsize; /* Program header table entry size */ + Elf32_Half e_phnum; /* Program header table entry count */ + Elf32_Half e_shentsize; /* Section header table entry size */ + Elf32_Half e_shnum; /* Section header table entry count */ + Elf32_Half e_shstrndx; /* Section header string table index */ +} Elf32_Ehdr; + +typedef struct +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ +} Elf64_Ehdr; + +/* Fields in the e_ident array. The EI_* macros are indices into the + array. The macros under each EI_* macro are the values the byte + may have. */ + +#define EI_MAG0 0 /* File identification byte 0 index */ +#define ELFMAG0 0x7f /* Magic number byte 0 */ + +#define EI_MAG1 1 /* File identification byte 1 index */ +#define ELFMAG1 'E' /* Magic number byte 1 */ + +#define EI_MAG2 2 /* File identification byte 2 index */ +#define ELFMAG2 'L' /* Magic number byte 2 */ + +#define EI_MAG3 3 /* File identification byte 3 index */ +#define ELFMAG3 'F' /* Magic number byte 3 */ + +/* Conglomeration of the identification bytes, for easy testing as a word. */ +#define ELFMAG "\177ELF" +#define SELFMAG 4 + +#define EI_CLASS 4 /* File class byte index */ +#define ELFCLASSNONE 0 /* Invalid class */ +#define ELFCLASS32 1 /* 32-bit objects */ +#define ELFCLASS64 2 /* 64-bit objects */ +#define ELFCLASSNUM 3 + +#define EI_DATA 5 /* Data encoding byte index */ +#define ELFDATANONE 0 /* Invalid data encoding */ +#define ELFDATA2LSB 1 /* 2's complement, little endian */ +#define ELFDATA2MSB 2 /* 2's complement, big endian */ +#define ELFDATANUM 3 + +#define EI_VERSION 6 /* File version byte index */ + /* Value must be EV_CURRENT */ + +#define EI_OSABI 7 /* OS ABI identification */ +#define ELFOSABI_NONE 0 /* UNIX System V ABI */ +#define ELFOSABI_SYSV 0 /* Alias. */ +#define ELFOSABI_HPUX 1 /* HP-UX */ +#define ELFOSABI_NETBSD 2 /* NetBSD. */ +#define ELFOSABI_GNU 3 /* Object uses GNU ELF extensions. */ +#define ELFOSABI_LINUX ELFOSABI_GNU /* Compatibility alias. */ +#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ +#define ELFOSABI_AIX 7 /* IBM AIX. */ +#define ELFOSABI_IRIX 8 /* SGI Irix. */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD. */ +#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto. */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD. */ +#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */ +#define ELFOSABI_ARM 97 /* ARM */ +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ + +#define EI_ABIVERSION 8 /* ABI version */ + +#define EI_PAD 9 /* Byte index of padding bytes */ + +/* Legal values for e_type (object file type). */ + +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* Relocatable file */ +#define ET_EXEC 2 /* Executable file */ +#define ET_DYN 3 /* Shared object file */ +#define ET_CORE 4 /* Core file */ +#define ET_NUM 5 /* Number of defined types */ +#define ET_LOOS 0xfe00 /* OS-specific range start */ +#define ET_HIOS 0xfeff /* OS-specific range end */ +#define ET_LOPROC 0xff00 /* Processor-specific range start */ +#define ET_HIPROC 0xffff /* Processor-specific range end */ + +/* Legal values for e_machine (architecture). */ + +#define EM_NONE 0 /* No machine */ +#define EM_M32 1 /* AT&T WE 32100 */ +#define EM_SPARC 2 /* SUN SPARC */ +#define EM_386 3 /* Intel 80386 */ +#define EM_68K 4 /* Motorola m68k family */ +#define EM_88K 5 /* Motorola m88k family */ +#define EM_860 7 /* Intel 80860 */ +#define EM_MIPS 8 /* MIPS R3000 big-endian */ +#define EM_S370 9 /* IBM System/370 */ +#define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ + +#define EM_PARISC 15 /* HPPA */ +#define EM_VPP500 17 /* Fujitsu VPP500 */ +#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ +#define EM_960 19 /* Intel 80960 */ +#define EM_PPC 20 /* PowerPC */ +#define EM_PPC64 21 /* PowerPC 64-bit */ +#define EM_S390 22 /* IBM S390 */ + +#define EM_V800 36 /* NEC V800 series */ +#define EM_FR20 37 /* Fujitsu FR20 */ +#define EM_RH32 38 /* TRW RH-32 */ +#define EM_RCE 39 /* Motorola RCE */ +#define EM_ARM 40 /* ARM */ +#define EM_FAKE_ALPHA 41 /* Digital Alpha */ +#define EM_SH 42 /* Hitachi SH */ +#define EM_SPARCV9 43 /* SPARC v9 64-bit */ +#define EM_TRICORE 44 /* Siemens Tricore */ +#define EM_ARC 45 /* Argonaut RISC Core */ +#define EM_H8_300 46 /* Hitachi H8/300 */ +#define EM_H8_300H 47 /* Hitachi H8/300H */ +#define EM_H8S 48 /* Hitachi H8S */ +#define EM_H8_500 49 /* Hitachi H8/500 */ +#define EM_IA_64 50 /* Intel Merced */ +#define EM_MIPS_X 51 /* Stanford MIPS-X */ +#define EM_COLDFIRE 52 /* Motorola Coldfire */ +#define EM_68HC12 53 /* Motorola M68HC12 */ +#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/ +#define EM_PCP 55 /* Siemens PCP */ +#define EM_NCPU 56 /* Sony nCPU embedded RISC */ +#define EM_NDR1 57 /* Denso NDR1 microprocessor */ +#define EM_STARCORE 58 /* Motorola Start*Core processor */ +#define EM_ME16 59 /* Toyota ME16 processor */ +#define EM_ST100 60 /* STMicroelectronic ST100 processor */ +#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/ +#define EM_X86_64 62 /* AMD x86-64 architecture */ +#define EM_PDSP 63 /* Sony DSP Processor */ + +#define EM_FX66 66 /* Siemens FX66 microcontroller */ +#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ +#define EM_ST7 68 /* STMicroelectronics ST7 8 bit mc */ +#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ +#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ +#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ +#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ +#define EM_SVX 73 /* Silicon Graphics SVx */ +#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ +#define EM_VAX 75 /* Digital VAX */ +#define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ +#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */ +#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ +#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ +#define EM_MMIX 80 /* Donald Knuth's educational 64-bit processor */ +#define EM_HUANY 81 /* Harvard University machine-independent object files */ +#define EM_PRISM 82 /* SiTera Prism */ +#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ +#define EM_FR30 84 /* Fujitsu FR30 */ +#define EM_D10V 85 /* Mitsubishi D10V */ +#define EM_D30V 86 /* Mitsubishi D30V */ +#define EM_V850 87 /* NEC v850 */ +#define EM_M32R 88 /* Mitsubishi M32R */ +#define EM_MN10300 89 /* Matsushita MN10300 */ +#define EM_MN10200 90 /* Matsushita MN10200 */ +#define EM_PJ 91 /* picoJava */ +#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ +#define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */ +#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ +#define EM_AARCH64 183 /* ARM AARCH64 */ +#define EM_TILEPRO 188 /* Tilera TILEPro */ +#define EM_TILEGX 191 /* Tilera TILE-Gx */ +#define EM_NUM 192 + +/* If it is necessary to assign new unofficial EM_* values, please + pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the + chances of collision with official or non-GNU unofficial values. */ + +#define EM_ALPHA 0x9026 +#define EM_C60 0x9c60 + +/* Legal values for e_version (version). */ + +#define EV_NONE 0 /* Invalid ELF version */ +#define EV_CURRENT 1 /* Current version */ +#define EV_NUM 2 + +/* Section header. */ + +typedef struct +{ + Elf32_Word sh_name; /* Section name (string tbl index) */ + Elf32_Word sh_type; /* Section type */ + Elf32_Word sh_flags; /* Section flags */ + Elf32_Addr sh_addr; /* Section virtual addr at execution */ + Elf32_Off sh_offset; /* Section file offset */ + Elf32_Word sh_size; /* Section size in bytes */ + Elf32_Word sh_link; /* Link to another section */ + Elf32_Word sh_info; /* Additional section information */ + Elf32_Word sh_addralign; /* Section alignment */ + Elf32_Word sh_entsize; /* Entry size if section holds table */ +} Elf32_Shdr; + +typedef struct +{ + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ +} Elf64_Shdr; + +/* Special section indices. */ + +#define SHN_UNDEF 0 /* Undefined section */ +#define SHN_LORESERVE 0xff00 /* Start of reserved indices */ +#define SHN_LOPROC 0xff00 /* Start of processor-specific */ +#define SHN_BEFORE 0xff00 /* Order section before all others + (Solaris). */ +#define SHN_AFTER 0xff01 /* Order section after all others + (Solaris). */ +#define SHN_HIPROC 0xff1f /* End of processor-specific */ +#define SHN_LOOS 0xff20 /* Start of OS-specific */ +#define SHN_HIOS 0xff3f /* End of OS-specific */ +#define SHN_ABS 0xfff1 /* Associated symbol is absolute */ +#define SHN_COMMON 0xfff2 /* Associated symbol is common */ +#define SHN_XINDEX 0xffff /* Index is in extra table. */ +#define SHN_HIRESERVE 0xffff /* End of reserved indices */ + +/* Legal values for sh_type (section type). */ + +#define SHT_NULL 0 /* Section header table entry unused */ +#define SHT_PROGBITS 1 /* Program data */ +#define SHT_SYMTAB 2 /* Symbol table */ +#define SHT_STRTAB 3 /* String table */ +#define SHT_RELA 4 /* Relocation entries with addends */ +#define SHT_HASH 5 /* Symbol hash table */ +#define SHT_DYNAMIC 6 /* Dynamic linking information */ +#define SHT_NOTE 7 /* Notes */ +#define SHT_NOBITS 8 /* Program space with no data (bss) */ +#define SHT_REL 9 /* Relocation entries, no addends */ +#define SHT_SHLIB 10 /* Reserved */ +#define SHT_DYNSYM 11 /* Dynamic linker symbol table */ +#define SHT_INIT_ARRAY 14 /* Array of constructors */ +#define SHT_FINI_ARRAY 15 /* Array of destructors */ +#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ +#define SHT_GROUP 17 /* Section group */ +#define SHT_SYMTAB_SHNDX 18 /* Extended section indices */ +#define SHT_NUM 19 /* Number of defined types. */ +#define SHT_LOOS 0x60000000 /* Start OS-specific. */ +#define SHT_GNU_ATTRIBUTES 0x6ffffff5 /* Object attributes. */ +#define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table. */ +#define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */ +#define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */ +#define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */ +#define SHT_SUNW_move 0x6ffffffa +#define SHT_SUNW_COMDAT 0x6ffffffb +#define SHT_SUNW_syminfo 0x6ffffffc +#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */ +#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */ +#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */ +#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */ +#define SHT_HIOS 0x6fffffff /* End OS-specific type */ +#define SHT_LOPROC 0x70000000 /* Start of processor-specific */ +#define SHT_HIPROC 0x7fffffff /* End of processor-specific */ +#define SHT_LOUSER 0x80000000 /* Start of application-specific */ +#define SHT_HIUSER 0x8fffffff /* End of application-specific */ + +/* Legal values for sh_flags (section flags). */ + +#define SHF_WRITE (1 << 0) /* Writable */ +#define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ +#define SHF_EXECINSTR (1 << 2) /* Executable */ +#define SHF_MERGE (1 << 4) /* Might be merged */ +#define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */ +#define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */ +#define SHF_LINK_ORDER (1 << 7) /* Preserve order after combining */ +#define SHF_OS_NONCONFORMING (1 << 8) /* Non-standard OS specific handling + required */ +#define SHF_GROUP (1 << 9) /* Section is member of a group. */ +#define SHF_TLS (1 << 10) /* Section hold thread-local data. */ +#define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */ +#define SHF_MASKOS 0x0ff00000 /* OS-specific. */ +#define SHF_MASKPROC 0xf0000000 /* Processor-specific */ +#define SHF_ORDERED (1 << 30) /* Special ordering requirement + (Solaris). */ +#define SHF_EXCLUDE (1 << 31) /* Section is excluded unless + referenced or allocated (Solaris).*/ + +/* Section group handling. */ +#define GRP_COMDAT 0x1 /* Mark group as COMDAT. */ + +/* Symbol table entry. */ + +typedef struct +{ + Elf32_Word st_name; /* Symbol name (string tbl index) */ + Elf32_Addr st_value; /* Symbol value */ + Elf32_Word st_size; /* Symbol size */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf32_Section st_shndx; /* Section index */ +} Elf32_Sym; + +typedef struct +{ + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ +} Elf64_Sym; + +/* The syminfo section if available contains additional information about + every dynamic symbol. */ + +typedef struct +{ + Elf32_Half si_boundto; /* Direct bindings, symbol bound to */ + Elf32_Half si_flags; /* Per symbol flags */ +} Elf32_Syminfo; + +typedef struct +{ + Elf64_Half si_boundto; /* Direct bindings, symbol bound to */ + Elf64_Half si_flags; /* Per symbol flags */ +} Elf64_Syminfo; + +/* Possible values for si_boundto. */ +#define SYMINFO_BT_SELF 0xffff /* Symbol bound to self */ +#define SYMINFO_BT_PARENT 0xfffe /* Symbol bound to parent */ +#define SYMINFO_BT_LOWRESERVE 0xff00 /* Beginning of reserved entries */ + +/* Possible bitmasks for si_flags. */ +#define SYMINFO_FLG_DIRECT 0x0001 /* Direct bound symbol */ +#define SYMINFO_FLG_PASSTHRU 0x0002 /* Pass-thru symbol for translator */ +#define SYMINFO_FLG_COPY 0x0004 /* Symbol is a copy-reloc */ +#define SYMINFO_FLG_LAZYLOAD 0x0008 /* Symbol bound to object to be lazy + loaded */ +/* Syminfo version values. */ +#define SYMINFO_NONE 0 +#define SYMINFO_CURRENT 1 +#define SYMINFO_NUM 2 + + +/* How to extract and insert information held in the st_info field. */ + +#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) +#define ELF32_ST_TYPE(val) ((val) & 0xf) +#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) + +/* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field. */ +#define ELF64_ST_BIND(val) ELF32_ST_BIND (val) +#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) +#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) + +/* Legal values for ST_BIND subfield of st_info (symbol binding). */ + +#define STB_LOCAL 0 /* Local symbol */ +#define STB_GLOBAL 1 /* Global symbol */ +#define STB_WEAK 2 /* Weak symbol */ +#define STB_NUM 3 /* Number of defined types. */ +#define STB_LOOS 10 /* Start of OS-specific */ +#define STB_GNU_UNIQUE 10 /* Unique symbol. */ +#define STB_HIOS 12 /* End of OS-specific */ +#define STB_LOPROC 13 /* Start of processor-specific */ +#define STB_HIPROC 15 /* End of processor-specific */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_NOTYPE 0 /* Symbol type is unspecified */ +#define STT_OBJECT 1 /* Symbol is a data object */ +#define STT_FUNC 2 /* Symbol is a code object */ +#define STT_SECTION 3 /* Symbol associated with a section */ +#define STT_FILE 4 /* Symbol's name is file name */ +#define STT_COMMON 5 /* Symbol is a common data object */ +#define STT_TLS 6 /* Symbol is thread-local data object*/ +#define STT_NUM 7 /* Number of defined types. */ +#define STT_LOOS 10 /* Start of OS-specific */ +#define STT_GNU_IFUNC 10 /* Symbol is indirect code object */ +#define STT_HIOS 12 /* End of OS-specific */ +#define STT_LOPROC 13 /* Start of processor-specific */ +#define STT_HIPROC 15 /* End of processor-specific */ + + +/* Symbol table indices are found in the hash buckets and chain table + of a symbol hash table section. This special index value indicates + the end of a chain, meaning no further symbols are found in that bucket. */ + +#define STN_UNDEF 0 /* End of a chain. */ + + +/* How to extract and insert information held in the st_other field. */ + +#define ELF32_ST_VISIBILITY(o) ((o) & 0x03) + +/* For ELF64 the definitions are the same. */ +#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) + +/* Symbol visibility specification encoded in the st_other field. */ +#define STV_DEFAULT 0 /* Default symbol visibility rules */ +#define STV_INTERNAL 1 /* Processor specific hidden class */ +#define STV_HIDDEN 2 /* Sym unavailable in other modules */ +#define STV_PROTECTED 3 /* Not preemptible, not exported */ + + +/* Relocation table entry without addend (in section of type SHT_REL). */ + +typedef struct +{ + Elf32_Addr r_offset; /* Address */ + Elf32_Word r_info; /* Relocation type and symbol index */ +} Elf32_Rel; + +/* I have seen two different definitions of the Elf64_Rel and + Elf64_Rela structures, so we'll leave them out until Novell (or + whoever) gets their act together. */ +/* The following, at least, is used on Sparc v9, MIPS, and Alpha. */ + +typedef struct +{ + Elf64_Addr r_offset; /* Address */ + Elf64_Xword r_info; /* Relocation type and symbol index */ +} Elf64_Rel; + +/* Relocation table entry with addend (in section of type SHT_RELA). */ + +typedef struct +{ + Elf32_Addr r_offset; /* Address */ + Elf32_Word r_info; /* Relocation type and symbol index */ + Elf32_Sword r_addend; /* Addend */ +} Elf32_Rela; + +typedef struct +{ + Elf64_Addr r_offset; /* Address */ + Elf64_Xword r_info; /* Relocation type and symbol index */ + Elf64_Sxword r_addend; /* Addend */ +} Elf64_Rela; + +/* How to extract and insert information held in the r_info field. */ + +#define ELF32_R_SYM(val) ((val) >> 8) +#define ELF32_R_TYPE(val) ((val) & 0xff) +#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) + +#define ELF64_R_SYM(i) ((i) >> 32) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) +#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type)) + +/* Program segment header. */ + +typedef struct +{ + Elf32_Word p_type; /* Segment type */ + Elf32_Off p_offset; /* Segment file offset */ + Elf32_Addr p_vaddr; /* Segment virtual address */ + Elf32_Addr p_paddr; /* Segment physical address */ + Elf32_Word p_filesz; /* Segment size in file */ + Elf32_Word p_memsz; /* Segment size in memory */ + Elf32_Word p_flags; /* Segment flags */ + Elf32_Word p_align; /* Segment alignment */ +} Elf32_Phdr; + +typedef struct +{ + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ +} Elf64_Phdr; + +/* Special value for e_phnum. This indicates that the real number of + program headers is too large to fit into e_phnum. Instead the real + value is in the field sh_info of section 0. */ + +#define PN_XNUM 0xffff + +/* Legal values for p_type (segment type). */ + +#define PT_NULL 0 /* Program header table entry unused */ +#define PT_LOAD 1 /* Loadable program segment */ +#define PT_DYNAMIC 2 /* Dynamic linking information */ +#define PT_INTERP 3 /* Program interpreter */ +#define PT_NOTE 4 /* Auxiliary information */ +#define PT_SHLIB 5 /* Reserved */ +#define PT_PHDR 6 /* Entry for header table itself */ +#define PT_TLS 7 /* Thread-local storage segment */ +#define PT_NUM 8 /* Number of defined types */ +#define PT_LOOS 0x60000000 /* Start of OS-specific */ +#define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */ +#define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */ +#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */ +#define PT_LOSUNW 0x6ffffffa +#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */ +#define PT_SUNWSTACK 0x6ffffffb /* Stack segment */ +#define PT_HISUNW 0x6fffffff +#define PT_HIOS 0x6fffffff /* End of OS-specific */ +#define PT_LOPROC 0x70000000 /* Start of processor-specific */ +#define PT_HIPROC 0x7fffffff /* End of processor-specific */ + +/* Legal values for p_flags (segment flags). */ + +#define PF_X (1 << 0) /* Segment is executable */ +#define PF_W (1 << 1) /* Segment is writable */ +#define PF_R (1 << 2) /* Segment is readable */ +#define PF_MASKOS 0x0ff00000 /* OS-specific */ +#define PF_MASKPROC 0xf0000000 /* Processor-specific */ + +/* Legal values for note segment descriptor types for core files. */ + +#define NT_PRSTATUS 1 /* Contains copy of prstatus struct */ +#define NT_FPREGSET 2 /* Contains copy of fpregset struct */ +#define NT_PRPSINFO 3 /* Contains copy of prpsinfo struct */ +#define NT_PRXREG 4 /* Contains copy of prxregset struct */ +#define NT_TASKSTRUCT 4 /* Contains copy of task structure */ +#define NT_PLATFORM 5 /* String from sysinfo(SI_PLATFORM) */ +#define NT_AUXV 6 /* Contains copy of auxv array */ +#define NT_GWINDOWS 7 /* Contains copy of gwindows struct */ +#define NT_ASRS 8 /* Contains copy of asrset struct */ +#define NT_PSTATUS 10 /* Contains copy of pstatus struct */ +#define NT_PSINFO 13 /* Contains copy of psinfo struct */ +#define NT_PRCRED 14 /* Contains copy of prcred struct */ +#define NT_UTSNAME 15 /* Contains copy of utsname struct */ +#define NT_LWPSTATUS 16 /* Contains copy of lwpstatus struct */ +#define NT_LWPSINFO 17 /* Contains copy of lwpinfo struct */ +#define NT_PRFPXREG 20 /* Contains copy of fprxregset struct */ +#define NT_PRXFPREG 0x46e62b7f /* Contains copy of user_fxsr_struct */ +#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ +#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ +#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ +#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ +#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ +#define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ +#define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ +#define NT_S390_TIMER 0x301 /* s390 timer register */ +#define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ +#define NT_S390_TODPREG 0x303 /* s390 TOD programmable register */ +#define NT_S390_CTRS 0x304 /* s390 control registers */ +#define NT_S390_PREFIX 0x305 /* s390 prefix register */ +#define NT_S390_LAST_BREAK 0x306 /* s390 breaking event address */ +#define NT_S390_SYSTEM_CALL 0x307 /* s390 system call restart data */ +#define NT_ARM_VFP 0x400 /* ARM VFP/NEON registers */ +#define NT_ARM_TLS 0x401 /* ARM TLS register */ +#define NT_ARM_HW_BREAK 0x402 /* ARM hardware breakpoint registers */ +#define NT_ARM_HW_WATCH 0x403 /* ARM hardware watchpoint registers */ + +/* Legal values for the note segment descriptor types for object files. */ + +#define NT_VERSION 1 /* Contains a version string. */ + + +/* Dynamic section entry. */ + +typedef struct +{ + Elf32_Sword d_tag; /* Dynamic entry type */ + union + { + Elf32_Word d_val; /* Integer value */ + Elf32_Addr d_ptr; /* Address value */ + } d_un; +} Elf32_Dyn; + +typedef struct +{ + Elf64_Sxword d_tag; /* Dynamic entry type */ + union + { + Elf64_Xword d_val; /* Integer value */ + Elf64_Addr d_ptr; /* Address value */ + } d_un; +} Elf64_Dyn; + +/* Legal values for d_tag (dynamic entry type). */ + +#define DT_NULL 0 /* Marks end of dynamic section */ +#define DT_NEEDED 1 /* Name of needed library */ +#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */ +#define DT_PLTGOT 3 /* Processor defined value */ +#define DT_HASH 4 /* Address of symbol hash table */ +#define DT_STRTAB 5 /* Address of string table */ +#define DT_SYMTAB 6 /* Address of symbol table */ +#define DT_RELA 7 /* Address of Rela relocs */ +#define DT_RELASZ 8 /* Total size of Rela relocs */ +#define DT_RELAENT 9 /* Size of one Rela reloc */ +#define DT_STRSZ 10 /* Size of string table */ +#define DT_SYMENT 11 /* Size of one symbol table entry */ +#define DT_INIT 12 /* Address of init function */ +#define DT_FINI 13 /* Address of termination function */ +#define DT_SONAME 14 /* Name of shared object */ +#define DT_RPATH 15 /* Library search path (deprecated) */ +#define DT_SYMBOLIC 16 /* Start symbol search here */ +#define DT_REL 17 /* Address of Rel relocs */ +#define DT_RELSZ 18 /* Total size of Rel relocs */ +#define DT_RELENT 19 /* Size of one Rel reloc */ +#define DT_PLTREL 20 /* Type of reloc in PLT */ +#define DT_DEBUG 21 /* For debugging; unspecified */ +#define DT_TEXTREL 22 /* Reloc might modify .text */ +#define DT_JMPREL 23 /* Address of PLT relocs */ +#define DT_BIND_NOW 24 /* Process relocations of object */ +#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ +#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ +#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ +#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ +#define DT_RUNPATH 29 /* Library search path */ +#define DT_FLAGS 30 /* Flags for the object being loaded */ +#define DT_ENCODING 32 /* Start of encoded range */ +#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/ +#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */ +#define DT_NUM 34 /* Number used */ +#define DT_LOOS 0x6000000d /* Start of OS-specific */ +#define DT_HIOS 0x6ffff000 /* End of OS-specific */ +#define DT_LOPROC 0x70000000 /* Start of processor-specific */ +#define DT_HIPROC 0x7fffffff /* End of processor-specific */ +#define DT_PROCNUM DT_MIPS_NUM /* Most used by any processor */ + +/* DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the + Dyn.d_un.d_val field of the Elf*_Dyn structure. This follows Sun's + approach. */ +#define DT_VALRNGLO 0x6ffffd00 +#define DT_GNU_PRELINKED 0x6ffffdf5 /* Prelinking timestamp */ +#define DT_GNU_CONFLICTSZ 0x6ffffdf6 /* Size of conflict section */ +#define DT_GNU_LIBLISTSZ 0x6ffffdf7 /* Size of library list */ +#define DT_CHECKSUM 0x6ffffdf8 +#define DT_PLTPADSZ 0x6ffffdf9 +#define DT_MOVEENT 0x6ffffdfa +#define DT_MOVESZ 0x6ffffdfb +#define DT_FEATURE_1 0x6ffffdfc /* Feature selection (DTF_*). */ +#define DT_POSFLAG_1 0x6ffffdfd /* Flags for DT_* entries, effecting + the following DT_* entry. */ +#define DT_SYMINSZ 0x6ffffdfe /* Size of syminfo table (in bytes) */ +#define DT_SYMINENT 0x6ffffdff /* Entry size of syminfo */ +#define DT_VALRNGHI 0x6ffffdff +#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag)) /* Reverse order! */ +#define DT_VALNUM 12 + +/* DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the + Dyn.d_un.d_ptr field of the Elf*_Dyn structure. + + If any adjustment is made to the ELF object after it has been + built these entries will need to be adjusted. */ +#define DT_ADDRRNGLO 0x6ffffe00 +#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */ +#define DT_TLSDESC_PLT 0x6ffffef6 +#define DT_TLSDESC_GOT 0x6ffffef7 +#define DT_GNU_CONFLICT 0x6ffffef8 /* Start of conflict section */ +#define DT_GNU_LIBLIST 0x6ffffef9 /* Library list */ +#define DT_CONFIG 0x6ffffefa /* Configuration information. */ +#define DT_DEPAUDIT 0x6ffffefb /* Dependency auditing. */ +#define DT_AUDIT 0x6ffffefc /* Object auditing. */ +#define DT_PLTPAD 0x6ffffefd /* PLT padding. */ +#define DT_MOVETAB 0x6ffffefe /* Move table. */ +#define DT_SYMINFO 0x6ffffeff /* Syminfo table. */ +#define DT_ADDRRNGHI 0x6ffffeff +#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag)) /* Reverse order! */ +#define DT_ADDRNUM 11 + +/* The versioning entry types. The next are defined as part of the + GNU extension. */ +#define DT_VERSYM 0x6ffffff0 + +#define DT_RELACOUNT 0x6ffffff9 +#define DT_RELCOUNT 0x6ffffffa + +/* These were chosen by Sun. */ +#define DT_FLAGS_1 0x6ffffffb /* State flags, see DF_1_* below. */ +#define DT_VERDEF 0x6ffffffc /* Address of version definition + table */ +#define DT_VERDEFNUM 0x6ffffffd /* Number of version definitions */ +#define DT_VERNEED 0x6ffffffe /* Address of table with needed + versions */ +#define DT_VERNEEDNUM 0x6fffffff /* Number of needed versions */ +#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */ +#define DT_VERSIONTAGNUM 16 + +/* Sun added these machine-independent extensions in the "processor-specific" + range. Be compatible. */ +#define DT_AUXILIARY 0x7ffffffd /* Shared object to load before self */ +#define DT_FILTER 0x7fffffff /* Shared object to get values from */ +#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1) +#define DT_EXTRANUM 3 + +/* Values of `d_un.d_val' in the DT_FLAGS entry. */ +#define DF_ORIGIN 0x00000001 /* Object may use DF_ORIGIN */ +#define DF_SYMBOLIC 0x00000002 /* Symbol resolutions starts here */ +#define DF_TEXTREL 0x00000004 /* Object contains text relocations */ +#define DF_BIND_NOW 0x00000008 /* No lazy binding for this object */ +#define DF_STATIC_TLS 0x00000010 /* Module uses the static TLS model */ + +/* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1 + entry in the dynamic section. */ +#define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */ +#define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */ +#define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */ +#define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object.*/ +#define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime.*/ +#define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object*/ +#define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */ +#define DF_1_ORIGIN 0x00000080 /* $ORIGIN must be handled. */ +#define DF_1_DIRECT 0x00000100 /* Direct binding enabled. */ +#define DF_1_TRANS 0x00000200 +#define DF_1_INTERPOSE 0x00000400 /* Object is used to interpose. */ +#define DF_1_NODEFLIB 0x00000800 /* Ignore default lib search path. */ +#define DF_1_NODUMP 0x00001000 /* Object can't be dldump'ed. */ +#define DF_1_CONFALT 0x00002000 /* Configuration alternative created.*/ +#define DF_1_ENDFILTEE 0x00004000 /* Filtee terminates filters search. */ +#define DF_1_DISPRELDNE 0x00008000 /* Disp reloc applied at build time. */ +#define DF_1_DISPRELPND 0x00010000 /* Disp reloc applied at run-time. */ +#define DF_1_NODIRECT 0x00020000 /* Object has no-direct binding. */ +#define DF_1_IGNMULDEF 0x00040000 +#define DF_1_NOKSYMS 0x00080000 +#define DF_1_NOHDR 0x00100000 +#define DF_1_EDITED 0x00200000 /* Object is modified after built. */ +#define DF_1_NORELOC 0x00400000 +#define DF_1_SYMINTPOSE 0x00800000 /* Object has individual interposers. */ +#define DF_1_GLOBAUDIT 0x01000000 /* Global auditing required. */ +#define DF_1_SINGLETON 0x02000000 /* Singleton symbols are used. */ + +/* Flags for the feature selection in DT_FEATURE_1. */ +#define DTF_1_PARINIT 0x00000001 +#define DTF_1_CONFEXP 0x00000002 + +/* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */ +#define DF_P1_LAZYLOAD 0x00000001 /* Lazyload following object. */ +#define DF_P1_GROUPPERM 0x00000002 /* Symbols from next object are not + generally available. */ + +/* Version definition sections. */ + +typedef struct +{ + Elf32_Half vd_version; /* Version revision */ + Elf32_Half vd_flags; /* Version information */ + Elf32_Half vd_ndx; /* Version Index */ + Elf32_Half vd_cnt; /* Number of associated aux entries */ + Elf32_Word vd_hash; /* Version name hash value */ + Elf32_Word vd_aux; /* Offset in bytes to verdaux array */ + Elf32_Word vd_next; /* Offset in bytes to next verdef + entry */ +} Elf32_Verdef; + +typedef struct +{ + Elf64_Half vd_version; /* Version revision */ + Elf64_Half vd_flags; /* Version information */ + Elf64_Half vd_ndx; /* Version Index */ + Elf64_Half vd_cnt; /* Number of associated aux entries */ + Elf64_Word vd_hash; /* Version name hash value */ + Elf64_Word vd_aux; /* Offset in bytes to verdaux array */ + Elf64_Word vd_next; /* Offset in bytes to next verdef + entry */ +} Elf64_Verdef; + + +/* Legal values for vd_version (version revision). */ +#define VER_DEF_NONE 0 /* No version */ +#define VER_DEF_CURRENT 1 /* Current version */ +#define VER_DEF_NUM 2 /* Given version number */ + +/* Legal values for vd_flags (version information flags). */ +#define VER_FLG_BASE 0x1 /* Version definition of file itself */ +#define VER_FLG_WEAK 0x2 /* Weak version identifier */ + +/* Versym symbol index values. */ +#define VER_NDX_LOCAL 0 /* Symbol is local. */ +#define VER_NDX_GLOBAL 1 /* Symbol is global. */ +#define VER_NDX_LORESERVE 0xff00 /* Beginning of reserved entries. */ +#define VER_NDX_ELIMINATE 0xff01 /* Symbol is to be eliminated. */ + +/* Auxiliary version information. */ + +typedef struct +{ + Elf32_Word vda_name; /* Version or dependency names */ + Elf32_Word vda_next; /* Offset in bytes to next verdaux + entry */ +} Elf32_Verdaux; + +typedef struct +{ + Elf64_Word vda_name; /* Version or dependency names */ + Elf64_Word vda_next; /* Offset in bytes to next verdaux + entry */ +} Elf64_Verdaux; + + +/* Version dependency section. */ + +typedef struct +{ + Elf32_Half vn_version; /* Version of structure */ + Elf32_Half vn_cnt; /* Number of associated aux entries */ + Elf32_Word vn_file; /* Offset of filename for this + dependency */ + Elf32_Word vn_aux; /* Offset in bytes to vernaux array */ + Elf32_Word vn_next; /* Offset in bytes to next verneed + entry */ +} Elf32_Verneed; + +typedef struct +{ + Elf64_Half vn_version; /* Version of structure */ + Elf64_Half vn_cnt; /* Number of associated aux entries */ + Elf64_Word vn_file; /* Offset of filename for this + dependency */ + Elf64_Word vn_aux; /* Offset in bytes to vernaux array */ + Elf64_Word vn_next; /* Offset in bytes to next verneed + entry */ +} Elf64_Verneed; + + +/* Legal values for vn_version (version revision). */ +#define VER_NEED_NONE 0 /* No version */ +#define VER_NEED_CURRENT 1 /* Current version */ +#define VER_NEED_NUM 2 /* Given version number */ + +/* Auxiliary needed version information. */ + +typedef struct +{ + Elf32_Word vna_hash; /* Hash value of dependency name */ + Elf32_Half vna_flags; /* Dependency specific information */ + Elf32_Half vna_other; /* Unused */ + Elf32_Word vna_name; /* Dependency name string offset */ + Elf32_Word vna_next; /* Offset in bytes to next vernaux + entry */ +} Elf32_Vernaux; + +typedef struct +{ + Elf64_Word vna_hash; /* Hash value of dependency name */ + Elf64_Half vna_flags; /* Dependency specific information */ + Elf64_Half vna_other; /* Unused */ + Elf64_Word vna_name; /* Dependency name string offset */ + Elf64_Word vna_next; /* Offset in bytes to next vernaux + entry */ +} Elf64_Vernaux; + + +/* Legal values for vna_flags. */ +#define VER_FLG_WEAK 0x2 /* Weak version identifier */ + + +/* Auxiliary vector. */ + +/* This vector is normally only used by the program interpreter. The + usual definition in an ABI supplement uses the name auxv_t. The + vector is not usually defined in a standard file, but it + can't hurt. We rename it to avoid conflicts. The sizes of these + types are an arrangement between the exec server and the program + interpreter, so we don't fully specify them here. */ + +typedef struct +{ + uint32_t a_type; /* Entry type */ + union + { + uint32_t a_val; /* Integer value */ + /* We use to have pointer elements added here. We cannot do that, + though, since it does not work when using 32-bit definitions + on 64-bit platforms and vice versa. */ + } a_un; +} Elf32_auxv_t; + +typedef struct +{ + uint64_t a_type; /* Entry type */ + union + { + uint64_t a_val; /* Integer value */ + /* We use to have pointer elements added here. We cannot do that, + though, since it does not work when using 32-bit definitions + on 64-bit platforms and vice versa. */ + } a_un; +} Elf64_auxv_t; + +/* Legal values for a_type (entry type). */ + +#define AT_NULL 0 /* End of vector */ +#define AT_IGNORE 1 /* Entry should be ignored */ +#define AT_EXECFD 2 /* File descriptor of program */ +#define AT_PHDR 3 /* Program headers for program */ +#define AT_PHENT 4 /* Size of program header entry */ +#define AT_PHNUM 5 /* Number of program headers */ +#define AT_PAGESZ 6 /* System page size */ +#define AT_BASE 7 /* Base address of interpreter */ +#define AT_FLAGS 8 /* Flags */ +#define AT_ENTRY 9 /* Entry point of program */ +#define AT_NOTELF 10 /* Program is not ELF */ +#define AT_UID 11 /* Real uid */ +#define AT_EUID 12 /* Effective uid */ +#define AT_GID 13 /* Real gid */ +#define AT_EGID 14 /* Effective gid */ +#define AT_CLKTCK 17 /* Frequency of times() */ + +/* Some more special a_type values describing the hardware. */ +#define AT_PLATFORM 15 /* String identifying platform. */ +#define AT_HWCAP 16 /* Machine dependent hints about + processor capabilities. */ + +/* This entry gives some information about the FPU initialization + performed by the kernel. */ +#define AT_FPUCW 18 /* Used FPU control word. */ + +/* Cache block sizes. */ +#define AT_DCACHEBSIZE 19 /* Data cache block size. */ +#define AT_ICACHEBSIZE 20 /* Instruction cache block size. */ +#define AT_UCACHEBSIZE 21 /* Unified cache block size. */ + +/* A special ignored value for PPC, used by the kernel to control the + interpretation of the AUXV. Must be > 16. */ +#define AT_IGNOREPPC 22 /* Entry should be ignored. */ + +#define AT_SECURE 23 /* Boolean, was exec setuid-like? */ + +#define AT_BASE_PLATFORM 24 /* String identifying real platforms.*/ + +#define AT_RANDOM 25 /* Address of 16 random bytes. */ + +#define AT_EXECFN 31 /* Filename of executable. */ + +/* Pointer to the global system page used for system calls and other + nice things. */ +#define AT_SYSINFO 32 +#define AT_SYSINFO_EHDR 33 + +/* Shapes of the caches. Bits 0-3 contains associativity; bits 4-7 contains + log2 of line size; mask those to get cache size. */ +#define AT_L1I_CACHESHAPE 34 +#define AT_L1D_CACHESHAPE 35 +#define AT_L2_CACHESHAPE 36 +#define AT_L3_CACHESHAPE 37 + +/* Note section contents. Each entry in the note section begins with + a header of a fixed form. */ + +typedef struct +{ + Elf32_Word n_namesz; /* Length of the note's name. */ + Elf32_Word n_descsz; /* Length of the note's descriptor. */ + Elf32_Word n_type; /* Type of the note. */ +} Elf32_Nhdr; + +typedef struct +{ + Elf64_Word n_namesz; /* Length of the note's name. */ + Elf64_Word n_descsz; /* Length of the note's descriptor. */ + Elf64_Word n_type; /* Type of the note. */ +} Elf64_Nhdr; + +/* Known names of notes. */ + +/* Solaris entries in the note section have this name. */ +#define ELF_NOTE_SOLARIS "SUNW Solaris" + +/* Note entries for GNU systems have this name. */ +#define ELF_NOTE_GNU "GNU" + + +/* Defined types of notes for Solaris. */ + +/* Value of descriptor (one word) is desired pagesize for the binary. */ +#define ELF_NOTE_PAGESIZE_HINT 1 + + +/* Defined note types for GNU systems. */ + +/* ABI information. The descriptor consists of words: + word 0: OS descriptor + word 1: major version of the ABI + word 2: minor version of the ABI + word 3: subminor version of the ABI +*/ +#define NT_GNU_ABI_TAG 1 +#define ELF_NOTE_ABI NT_GNU_ABI_TAG /* Old name. */ + +/* Known OSes. These values can appear in word 0 of an + NT_GNU_ABI_TAG note section entry. */ +#define ELF_NOTE_OS_LINUX 0 +#define ELF_NOTE_OS_GNU 1 +#define ELF_NOTE_OS_SOLARIS2 2 +#define ELF_NOTE_OS_FREEBSD 3 + +/* Synthetic hwcap information. The descriptor begins with two words: + word 0: number of entries + word 1: bitmask of enabled entries + Then follow variable-length entries, one byte followed by a + '\0'-terminated hwcap name string. The byte gives the bit + number to test if enabled, (1U << bit) & bitmask. */ +#define NT_GNU_HWCAP 2 + +/* Build ID bits as generated by ld --build-id. + The descriptor consists of any nonzero number of bytes. */ +#define NT_GNU_BUILD_ID 3 + +/* Version note generated by GNU gold containing a version string. */ +#define NT_GNU_GOLD_VERSION 4 + + +/* Move records. */ +typedef struct +{ + Elf32_Xword m_value; /* Symbol value. */ + Elf32_Word m_info; /* Size and index. */ + Elf32_Word m_poffset; /* Symbol offset. */ + Elf32_Half m_repeat; /* Repeat count. */ + Elf32_Half m_stride; /* Stride info. */ +} Elf32_Move; + +typedef struct +{ + Elf64_Xword m_value; /* Symbol value. */ + Elf64_Xword m_info; /* Size and index. */ + Elf64_Xword m_poffset; /* Symbol offset. */ + Elf64_Half m_repeat; /* Repeat count. */ + Elf64_Half m_stride; /* Stride info. */ +} Elf64_Move; + +/* Macro to construct move records. */ +#define ELF32_M_SYM(info) ((info) >> 8) +#define ELF32_M_SIZE(info) ((unsigned char) (info)) +#define ELF32_M_INFO(sym, size) (((sym) << 8) + (unsigned char) (size)) + +#define ELF64_M_SYM(info) ELF32_M_SYM (info) +#define ELF64_M_SIZE(info) ELF32_M_SIZE (info) +#define ELF64_M_INFO(sym, size) ELF32_M_INFO (sym, size) + + +/* Motorola 68k specific definitions. */ + +/* Values for Elf32_Ehdr.e_flags. */ +#define EF_CPU32 0x00810000 + +/* m68k relocs. */ + +#define R_68K_NONE 0 /* No reloc */ +#define R_68K_32 1 /* Direct 32 bit */ +#define R_68K_16 2 /* Direct 16 bit */ +#define R_68K_8 3 /* Direct 8 bit */ +#define R_68K_PC32 4 /* PC relative 32 bit */ +#define R_68K_PC16 5 /* PC relative 16 bit */ +#define R_68K_PC8 6 /* PC relative 8 bit */ +#define R_68K_GOT32 7 /* 32 bit PC relative GOT entry */ +#define R_68K_GOT16 8 /* 16 bit PC relative GOT entry */ +#define R_68K_GOT8 9 /* 8 bit PC relative GOT entry */ +#define R_68K_GOT32O 10 /* 32 bit GOT offset */ +#define R_68K_GOT16O 11 /* 16 bit GOT offset */ +#define R_68K_GOT8O 12 /* 8 bit GOT offset */ +#define R_68K_PLT32 13 /* 32 bit PC relative PLT address */ +#define R_68K_PLT16 14 /* 16 bit PC relative PLT address */ +#define R_68K_PLT8 15 /* 8 bit PC relative PLT address */ +#define R_68K_PLT32O 16 /* 32 bit PLT offset */ +#define R_68K_PLT16O 17 /* 16 bit PLT offset */ +#define R_68K_PLT8O 18 /* 8 bit PLT offset */ +#define R_68K_COPY 19 /* Copy symbol at runtime */ +#define R_68K_GLOB_DAT 20 /* Create GOT entry */ +#define R_68K_JMP_SLOT 21 /* Create PLT entry */ +#define R_68K_RELATIVE 22 /* Adjust by program base */ +#define R_68K_TLS_GD32 25 /* 32 bit GOT offset for GD */ +#define R_68K_TLS_GD16 26 /* 16 bit GOT offset for GD */ +#define R_68K_TLS_GD8 27 /* 8 bit GOT offset for GD */ +#define R_68K_TLS_LDM32 28 /* 32 bit GOT offset for LDM */ +#define R_68K_TLS_LDM16 29 /* 16 bit GOT offset for LDM */ +#define R_68K_TLS_LDM8 30 /* 8 bit GOT offset for LDM */ +#define R_68K_TLS_LDO32 31 /* 32 bit module-relative offset */ +#define R_68K_TLS_LDO16 32 /* 16 bit module-relative offset */ +#define R_68K_TLS_LDO8 33 /* 8 bit module-relative offset */ +#define R_68K_TLS_IE32 34 /* 32 bit GOT offset for IE */ +#define R_68K_TLS_IE16 35 /* 16 bit GOT offset for IE */ +#define R_68K_TLS_IE8 36 /* 8 bit GOT offset for IE */ +#define R_68K_TLS_LE32 37 /* 32 bit offset relative to + static TLS block */ +#define R_68K_TLS_LE16 38 /* 16 bit offset relative to + static TLS block */ +#define R_68K_TLS_LE8 39 /* 8 bit offset relative to + static TLS block */ +#define R_68K_TLS_DTPMOD32 40 /* 32 bit module number */ +#define R_68K_TLS_DTPREL32 41 /* 32 bit module-relative offset */ +#define R_68K_TLS_TPREL32 42 /* 32 bit TP-relative offset */ +/* Keep this the last entry. */ +#define R_68K_NUM 43 + +/* Intel 80386 specific definitions. */ + +/* i386 relocs. */ + +#define R_386_NONE 0 /* No reloc */ +#define R_386_32 1 /* Direct 32 bit */ +#define R_386_PC32 2 /* PC relative 32 bit */ +#define R_386_GOT32 3 /* 32 bit GOT entry */ +#define R_386_PLT32 4 /* 32 bit PLT address */ +#define R_386_COPY 5 /* Copy symbol at runtime */ +#define R_386_GLOB_DAT 6 /* Create GOT entry */ +#define R_386_JMP_SLOT 7 /* Create PLT entry */ +#define R_386_RELATIVE 8 /* Adjust by program base */ +#define R_386_GOTOFF 9 /* 32 bit offset to GOT */ +#define R_386_GOTPC 10 /* 32 bit PC relative offset to GOT */ +#define R_386_32PLT 11 +#define R_386_TLS_TPOFF 14 /* Offset in static TLS block */ +#define R_386_TLS_IE 15 /* Address of GOT entry for static TLS + block offset */ +#define R_386_TLS_GOTIE 16 /* GOT entry for static TLS block + offset */ +#define R_386_TLS_LE 17 /* Offset relative to static TLS + block */ +#define R_386_TLS_GD 18 /* Direct 32 bit for GNU version of + general dynamic thread local data */ +#define R_386_TLS_LDM 19 /* Direct 32 bit for GNU version of + local dynamic thread local data + in LE code */ +#define R_386_16 20 +#define R_386_PC16 21 +#define R_386_8 22 +#define R_386_PC8 23 +#define R_386_TLS_GD_32 24 /* Direct 32 bit for general dynamic + thread local data */ +#define R_386_TLS_GD_PUSH 25 /* Tag for pushl in GD TLS code */ +#define R_386_TLS_GD_CALL 26 /* Relocation for call to + __tls_get_addr() */ +#define R_386_TLS_GD_POP 27 /* Tag for popl in GD TLS code */ +#define R_386_TLS_LDM_32 28 /* Direct 32 bit for local dynamic + thread local data in LE code */ +#define R_386_TLS_LDM_PUSH 29 /* Tag for pushl in LDM TLS code */ +#define R_386_TLS_LDM_CALL 30 /* Relocation for call to + __tls_get_addr() in LDM code */ +#define R_386_TLS_LDM_POP 31 /* Tag for popl in LDM TLS code */ +#define R_386_TLS_LDO_32 32 /* Offset relative to TLS block */ +#define R_386_TLS_IE_32 33 /* GOT entry for negated static TLS + block offset */ +#define R_386_TLS_LE_32 34 /* Negated offset relative to static + TLS block */ +#define R_386_TLS_DTPMOD32 35 /* ID of module containing symbol */ +#define R_386_TLS_DTPOFF32 36 /* Offset in TLS block */ +#define R_386_TLS_TPOFF32 37 /* Negated offset in static TLS block */ +/* 38? */ +#define R_386_TLS_GOTDESC 39 /* GOT offset for TLS descriptor. */ +#define R_386_TLS_DESC_CALL 40 /* Marker of call through TLS + descriptor for + relaxation. */ +#define R_386_TLS_DESC 41 /* TLS descriptor containing + pointer to code and to + argument, returning the TLS + offset for the symbol. */ +#define R_386_IRELATIVE 42 /* Adjust indirectly by program base */ +#define R_386_GOT32X 43 /* 32 bit GOT entry, relaxable */ +/* Keep this the last entry. */ +#define R_386_NUM 44 + +/* SUN SPARC specific definitions. */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_SPARC_REGISTER 13 /* Global register reserved to app. */ + +/* Values for Elf64_Ehdr.e_flags. */ + +#define EF_SPARCV9_MM 3 +#define EF_SPARCV9_TSO 0 +#define EF_SPARCV9_PSO 1 +#define EF_SPARCV9_RMO 2 +#define EF_SPARC_LEDATA 0x800000 /* little endian data */ +#define EF_SPARC_EXT_MASK 0xFFFF00 +#define EF_SPARC_32PLUS 0x000100 /* generic V8+ features */ +#define EF_SPARC_SUN_US1 0x000200 /* Sun UltraSPARC1 extensions */ +#define EF_SPARC_HAL_R1 0x000400 /* HAL R1 extensions */ +#define EF_SPARC_SUN_US3 0x000800 /* Sun UltraSPARCIII extensions */ + +/* SPARC relocs. */ + +#define R_SPARC_NONE 0 /* No reloc */ +#define R_SPARC_8 1 /* Direct 8 bit */ +#define R_SPARC_16 2 /* Direct 16 bit */ +#define R_SPARC_32 3 /* Direct 32 bit */ +#define R_SPARC_DISP8 4 /* PC relative 8 bit */ +#define R_SPARC_DISP16 5 /* PC relative 16 bit */ +#define R_SPARC_DISP32 6 /* PC relative 32 bit */ +#define R_SPARC_WDISP30 7 /* PC relative 30 bit shifted */ +#define R_SPARC_WDISP22 8 /* PC relative 22 bit shifted */ +#define R_SPARC_HI22 9 /* High 22 bit */ +#define R_SPARC_22 10 /* Direct 22 bit */ +#define R_SPARC_13 11 /* Direct 13 bit */ +#define R_SPARC_LO10 12 /* Truncated 10 bit */ +#define R_SPARC_GOT10 13 /* Truncated 10 bit GOT entry */ +#define R_SPARC_GOT13 14 /* 13 bit GOT entry */ +#define R_SPARC_GOT22 15 /* 22 bit GOT entry shifted */ +#define R_SPARC_PC10 16 /* PC relative 10 bit truncated */ +#define R_SPARC_PC22 17 /* PC relative 22 bit shifted */ +#define R_SPARC_WPLT30 18 /* 30 bit PC relative PLT address */ +#define R_SPARC_COPY 19 /* Copy symbol at runtime */ +#define R_SPARC_GLOB_DAT 20 /* Create GOT entry */ +#define R_SPARC_JMP_SLOT 21 /* Create PLT entry */ +#define R_SPARC_RELATIVE 22 /* Adjust by program base */ +#define R_SPARC_UA32 23 /* Direct 32 bit unaligned */ + +/* Additional Sparc64 relocs. */ + +#define R_SPARC_PLT32 24 /* Direct 32 bit ref to PLT entry */ +#define R_SPARC_HIPLT22 25 /* High 22 bit PLT entry */ +#define R_SPARC_LOPLT10 26 /* Truncated 10 bit PLT entry */ +#define R_SPARC_PCPLT32 27 /* PC rel 32 bit ref to PLT entry */ +#define R_SPARC_PCPLT22 28 /* PC rel high 22 bit PLT entry */ +#define R_SPARC_PCPLT10 29 /* PC rel trunc 10 bit PLT entry */ +#define R_SPARC_10 30 /* Direct 10 bit */ +#define R_SPARC_11 31 /* Direct 11 bit */ +#define R_SPARC_64 32 /* Direct 64 bit */ +#define R_SPARC_OLO10 33 /* 10bit with secondary 13bit addend */ +#define R_SPARC_HH22 34 /* Top 22 bits of direct 64 bit */ +#define R_SPARC_HM10 35 /* High middle 10 bits of ... */ +#define R_SPARC_LM22 36 /* Low middle 22 bits of ... */ +#define R_SPARC_PC_HH22 37 /* Top 22 bits of pc rel 64 bit */ +#define R_SPARC_PC_HM10 38 /* High middle 10 bit of ... */ +#define R_SPARC_PC_LM22 39 /* Low middle 22 bits of ... */ +#define R_SPARC_WDISP16 40 /* PC relative 16 bit shifted */ +#define R_SPARC_WDISP19 41 /* PC relative 19 bit shifted */ +#define R_SPARC_GLOB_JMP 42 /* was part of v9 ABI but was removed */ +#define R_SPARC_7 43 /* Direct 7 bit */ +#define R_SPARC_5 44 /* Direct 5 bit */ +#define R_SPARC_6 45 /* Direct 6 bit */ +#define R_SPARC_DISP64 46 /* PC relative 64 bit */ +#define R_SPARC_PLT64 47 /* Direct 64 bit ref to PLT entry */ +#define R_SPARC_HIX22 48 /* High 22 bit complemented */ +#define R_SPARC_LOX10 49 /* Truncated 11 bit complemented */ +#define R_SPARC_H44 50 /* Direct high 12 of 44 bit */ +#define R_SPARC_M44 51 /* Direct mid 22 of 44 bit */ +#define R_SPARC_L44 52 /* Direct low 10 of 44 bit */ +#define R_SPARC_REGISTER 53 /* Global register usage */ +#define R_SPARC_UA64 54 /* Direct 64 bit unaligned */ +#define R_SPARC_UA16 55 /* Direct 16 bit unaligned */ +#define R_SPARC_TLS_GD_HI22 56 +#define R_SPARC_TLS_GD_LO10 57 +#define R_SPARC_TLS_GD_ADD 58 +#define R_SPARC_TLS_GD_CALL 59 +#define R_SPARC_TLS_LDM_HI22 60 +#define R_SPARC_TLS_LDM_LO10 61 +#define R_SPARC_TLS_LDM_ADD 62 +#define R_SPARC_TLS_LDM_CALL 63 +#define R_SPARC_TLS_LDO_HIX22 64 +#define R_SPARC_TLS_LDO_LOX10 65 +#define R_SPARC_TLS_LDO_ADD 66 +#define R_SPARC_TLS_IE_HI22 67 +#define R_SPARC_TLS_IE_LO10 68 +#define R_SPARC_TLS_IE_LD 69 +#define R_SPARC_TLS_IE_LDX 70 +#define R_SPARC_TLS_IE_ADD 71 +#define R_SPARC_TLS_LE_HIX22 72 +#define R_SPARC_TLS_LE_LOX10 73 +#define R_SPARC_TLS_DTPMOD32 74 +#define R_SPARC_TLS_DTPMOD64 75 +#define R_SPARC_TLS_DTPOFF32 76 +#define R_SPARC_TLS_DTPOFF64 77 +#define R_SPARC_TLS_TPOFF32 78 +#define R_SPARC_TLS_TPOFF64 79 +#define R_SPARC_GOTDATA_HIX22 80 +#define R_SPARC_GOTDATA_LOX10 81 +#define R_SPARC_GOTDATA_OP_HIX22 82 +#define R_SPARC_GOTDATA_OP_LOX10 83 +#define R_SPARC_GOTDATA_OP 84 +#define R_SPARC_H34 85 +#define R_SPARC_SIZE32 86 +#define R_SPARC_SIZE64 87 +#define R_SPARC_WDISP10 88 +#define R_SPARC_JMP_IREL 248 +#define R_SPARC_IRELATIVE 249 +#define R_SPARC_GNU_VTINHERIT 250 +#define R_SPARC_GNU_VTENTRY 251 +#define R_SPARC_REV32 252 +/* Keep this the last entry. */ +#define R_SPARC_NUM 253 + +/* For Sparc64, legal values for d_tag of Elf64_Dyn. */ + +#define DT_SPARC_REGISTER 0x70000001 +#define DT_SPARC_NUM 2 + +/* MIPS R3000 specific definitions. */ + +/* Legal values for e_flags field of Elf32_Ehdr. */ + +#define EF_MIPS_NOREORDER 1 /* A .noreorder directive was used */ +#define EF_MIPS_PIC 2 /* Contains PIC code */ +#define EF_MIPS_CPIC 4 /* Uses PIC calling sequence */ +#define EF_MIPS_XGOT 8 +#define EF_MIPS_64BIT_WHIRL 16 +#define EF_MIPS_ABI2 32 +#define EF_MIPS_ABI_ON32 64 +#define EF_MIPS_ARCH 0xf0000000 /* MIPS architecture level */ + +/* Legal values for MIPS architecture level. */ + +#define EF_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ +#define EF_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ +#define EF_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ +#define EF_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ +#define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ +#define EF_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */ +#define EF_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */ + +/* The following are non-official names and should not be used. */ + +#define E_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ +#define E_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ +#define E_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ +#define E_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ +#define E_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ +#define E_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */ +#define E_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */ + +/* Special section indices. */ + +#define SHN_MIPS_ACOMMON 0xff00 /* Allocated common symbols */ +#define SHN_MIPS_TEXT 0xff01 /* Allocated test symbols. */ +#define SHN_MIPS_DATA 0xff02 /* Allocated data symbols. */ +#define SHN_MIPS_SCOMMON 0xff03 /* Small common symbols */ +#define SHN_MIPS_SUNDEFINED 0xff04 /* Small undefined symbols */ + +/* Legal values for sh_type field of Elf32_Shdr. */ + +#define SHT_MIPS_LIBLIST 0x70000000 /* Shared objects used in link */ +#define SHT_MIPS_MSYM 0x70000001 +#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicting symbols */ +#define SHT_MIPS_GPTAB 0x70000003 /* Global data area sizes */ +#define SHT_MIPS_UCODE 0x70000004 /* Reserved for SGI/MIPS compilers */ +#define SHT_MIPS_DEBUG 0x70000005 /* MIPS ECOFF debugging information*/ +#define SHT_MIPS_REGINFO 0x70000006 /* Register usage information */ +#define SHT_MIPS_PACKAGE 0x70000007 +#define SHT_MIPS_PACKSYM 0x70000008 +#define SHT_MIPS_RELD 0x70000009 +#define SHT_MIPS_IFACE 0x7000000b +#define SHT_MIPS_CONTENT 0x7000000c +#define SHT_MIPS_OPTIONS 0x7000000d /* Miscellaneous options. */ +#define SHT_MIPS_SHDR 0x70000010 +#define SHT_MIPS_FDESC 0x70000011 +#define SHT_MIPS_EXTSYM 0x70000012 +#define SHT_MIPS_DENSE 0x70000013 +#define SHT_MIPS_PDESC 0x70000014 +#define SHT_MIPS_LOCSYM 0x70000015 +#define SHT_MIPS_AUXSYM 0x70000016 +#define SHT_MIPS_OPTSYM 0x70000017 +#define SHT_MIPS_LOCSTR 0x70000018 +#define SHT_MIPS_LINE 0x70000019 +#define SHT_MIPS_RFDESC 0x7000001a +#define SHT_MIPS_DELTASYM 0x7000001b +#define SHT_MIPS_DELTAINST 0x7000001c +#define SHT_MIPS_DELTACLASS 0x7000001d +#define SHT_MIPS_DWARF 0x7000001e /* DWARF debugging information. */ +#define SHT_MIPS_DELTADECL 0x7000001f +#define SHT_MIPS_SYMBOL_LIB 0x70000020 +#define SHT_MIPS_EVENTS 0x70000021 /* Event section. */ +#define SHT_MIPS_TRANSLATE 0x70000022 +#define SHT_MIPS_PIXIE 0x70000023 +#define SHT_MIPS_XLATE 0x70000024 +#define SHT_MIPS_XLATE_DEBUG 0x70000025 +#define SHT_MIPS_WHIRL 0x70000026 +#define SHT_MIPS_EH_REGION 0x70000027 +#define SHT_MIPS_XLATE_OLD 0x70000028 +#define SHT_MIPS_PDR_EXCEPTION 0x70000029 + +/* Legal values for sh_flags field of Elf32_Shdr. */ + +#define SHF_MIPS_GPREL 0x10000000 /* Must be part of global data area */ +#define SHF_MIPS_MERGE 0x20000000 +#define SHF_MIPS_ADDR 0x40000000 +#define SHF_MIPS_STRINGS 0x80000000 +#define SHF_MIPS_NOSTRIP 0x08000000 +#define SHF_MIPS_LOCAL 0x04000000 +#define SHF_MIPS_NAMES 0x02000000 +#define SHF_MIPS_NODUPE 0x01000000 + + +/* Symbol tables. */ + +/* MIPS specific values for `st_other'. */ +#define STO_MIPS_DEFAULT 0x0 +#define STO_MIPS_INTERNAL 0x1 +#define STO_MIPS_HIDDEN 0x2 +#define STO_MIPS_PROTECTED 0x3 +#define STO_MIPS_PLT 0x8 +#define STO_MIPS_SC_ALIGN_UNUSED 0xff + +/* MIPS specific values for `st_info'. */ +#define STB_MIPS_SPLIT_COMMON 13 + +/* Entries found in sections of type SHT_MIPS_GPTAB. */ + +typedef union +{ + struct + { + Elf32_Word gt_current_g_value; /* -G value used for compilation */ + Elf32_Word gt_unused; /* Not used */ + } gt_header; /* First entry in section */ + struct + { + Elf32_Word gt_g_value; /* If this value were used for -G */ + Elf32_Word gt_bytes; /* This many bytes would be used */ + } gt_entry; /* Subsequent entries in section */ +} Elf32_gptab; + +/* Entry found in sections of type SHT_MIPS_REGINFO. */ + +typedef struct +{ + Elf32_Word ri_gprmask; /* General registers used */ + Elf32_Word ri_cprmask[4]; /* Coprocessor registers used */ + Elf32_Sword ri_gp_value; /* $gp register value */ +} Elf32_RegInfo; + +/* Entries found in sections of type SHT_MIPS_OPTIONS. */ + +typedef struct +{ + unsigned char kind; /* Determines interpretation of the + variable part of descriptor. */ + unsigned char size; /* Size of descriptor, including header. */ + Elf32_Section section; /* Section header index of section affected, + 0 for global options. */ + Elf32_Word info; /* Kind-specific information. */ +} Elf_Options; + +/* Values for `kind' field in Elf_Options. */ + +#define ODK_NULL 0 /* Undefined. */ +#define ODK_REGINFO 1 /* Register usage information. */ +#define ODK_EXCEPTIONS 2 /* Exception processing options. */ +#define ODK_PAD 3 /* Section padding options. */ +#define ODK_HWPATCH 4 /* Hardware workarounds performed */ +#define ODK_FILL 5 /* record the fill value used by the linker. */ +#define ODK_TAGS 6 /* reserve space for desktop tools to write. */ +#define ODK_HWAND 7 /* HW workarounds. 'AND' bits when merging. */ +#define ODK_HWOR 8 /* HW workarounds. 'OR' bits when merging. */ + +/* Values for `info' in Elf_Options for ODK_EXCEPTIONS entries. */ + +#define OEX_FPU_MIN 0x1f /* FPE's which MUST be enabled. */ +#define OEX_FPU_MAX 0x1f00 /* FPE's which MAY be enabled. */ +#define OEX_PAGE0 0x10000 /* page zero must be mapped. */ +#define OEX_SMM 0x20000 /* Force sequential memory mode? */ +#define OEX_FPDBUG 0x40000 /* Force floating point debug mode? */ +#define OEX_PRECISEFP OEX_FPDBUG +#define OEX_DISMISS 0x80000 /* Dismiss invalid address faults? */ + +#define OEX_FPU_INVAL 0x10 +#define OEX_FPU_DIV0 0x08 +#define OEX_FPU_OFLO 0x04 +#define OEX_FPU_UFLO 0x02 +#define OEX_FPU_INEX 0x01 + +/* Masks for `info' in Elf_Options for an ODK_HWPATCH entry. */ + +#define OHW_R4KEOP 0x1 /* R4000 end-of-page patch. */ +#define OHW_R8KPFETCH 0x2 /* may need R8000 prefetch patch. */ +#define OHW_R5KEOP 0x4 /* R5000 end-of-page patch. */ +#define OHW_R5KCVTL 0x8 /* R5000 cvt.[ds].l bug. clean=1. */ + +#define OPAD_PREFIX 0x1 +#define OPAD_POSTFIX 0x2 +#define OPAD_SYMBOL 0x4 + +/* Entry found in `.options' section. */ + +typedef struct +{ + Elf32_Word hwp_flags1; /* Extra flags. */ + Elf32_Word hwp_flags2; /* Extra flags. */ +} Elf_Options_Hw; + +/* Masks for `info' in ElfOptions for ODK_HWAND and ODK_HWOR entries. */ + +#define OHWA0_R4KEOP_CHECKED 0x00000001 +#define OHWA1_R4KEOP_CLEAN 0x00000002 + +/* MIPS relocs. */ + +#define R_MIPS_NONE 0 /* No reloc */ +#define R_MIPS_16 1 /* Direct 16 bit */ +#define R_MIPS_32 2 /* Direct 32 bit */ +#define R_MIPS_REL32 3 /* PC relative 32 bit */ +#define R_MIPS_26 4 /* Direct 26 bit shifted */ +#define R_MIPS_HI16 5 /* High 16 bit */ +#define R_MIPS_LO16 6 /* Low 16 bit */ +#define R_MIPS_GPREL16 7 /* GP relative 16 bit */ +#define R_MIPS_LITERAL 8 /* 16 bit literal entry */ +#define R_MIPS_GOT16 9 /* 16 bit GOT entry */ +#define R_MIPS_PC16 10 /* PC relative 16 bit */ +#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */ +#define R_MIPS_GPREL32 12 /* GP relative 32 bit */ + +#define R_MIPS_SHIFT5 16 +#define R_MIPS_SHIFT6 17 +#define R_MIPS_64 18 +#define R_MIPS_GOT_DISP 19 +#define R_MIPS_GOT_PAGE 20 +#define R_MIPS_GOT_OFST 21 +#define R_MIPS_GOT_HI16 22 +#define R_MIPS_GOT_LO16 23 +#define R_MIPS_SUB 24 +#define R_MIPS_INSERT_A 25 +#define R_MIPS_INSERT_B 26 +#define R_MIPS_DELETE 27 +#define R_MIPS_HIGHER 28 +#define R_MIPS_HIGHEST 29 +#define R_MIPS_CALL_HI16 30 +#define R_MIPS_CALL_LO16 31 +#define R_MIPS_SCN_DISP 32 +#define R_MIPS_REL16 33 +#define R_MIPS_ADD_IMMEDIATE 34 +#define R_MIPS_PJUMP 35 +#define R_MIPS_RELGOT 36 +#define R_MIPS_JALR 37 +#define R_MIPS_TLS_DTPMOD32 38 /* Module number 32 bit */ +#define R_MIPS_TLS_DTPREL32 39 /* Module-relative offset 32 bit */ +#define R_MIPS_TLS_DTPMOD64 40 /* Module number 64 bit */ +#define R_MIPS_TLS_DTPREL64 41 /* Module-relative offset 64 bit */ +#define R_MIPS_TLS_GD 42 /* 16 bit GOT offset for GD */ +#define R_MIPS_TLS_LDM 43 /* 16 bit GOT offset for LDM */ +#define R_MIPS_TLS_DTPREL_HI16 44 /* Module-relative offset, high 16 bits */ +#define R_MIPS_TLS_DTPREL_LO16 45 /* Module-relative offset, low 16 bits */ +#define R_MIPS_TLS_GOTTPREL 46 /* 16 bit GOT offset for IE */ +#define R_MIPS_TLS_TPREL32 47 /* TP-relative offset, 32 bit */ +#define R_MIPS_TLS_TPREL64 48 /* TP-relative offset, 64 bit */ +#define R_MIPS_TLS_TPREL_HI16 49 /* TP-relative offset, high 16 bits */ +#define R_MIPS_TLS_TPREL_LO16 50 /* TP-relative offset, low 16 bits */ +#define R_MIPS_GLOB_DAT 51 +#define R_MIPS_COPY 126 +#define R_MIPS_JUMP_SLOT 127 +/* Keep this the last entry. */ +#define R_MIPS_NUM 128 + +/* Legal values for p_type field of Elf32_Phdr. */ + +#define PT_MIPS_REGINFO 0x70000000 /* Register usage information */ +#define PT_MIPS_RTPROC 0x70000001 /* Runtime procedure table. */ +#define PT_MIPS_OPTIONS 0x70000002 + +/* Special program header types. */ + +#define PF_MIPS_LOCAL 0x10000000 + +/* Legal values for d_tag field of Elf32_Dyn. */ + +#define DT_MIPS_RLD_VERSION 0x70000001 /* Runtime linker interface version */ +#define DT_MIPS_TIME_STAMP 0x70000002 /* Timestamp */ +#define DT_MIPS_ICHECKSUM 0x70000003 /* Checksum */ +#define DT_MIPS_IVERSION 0x70000004 /* Version string (string tbl index) */ +#define DT_MIPS_FLAGS 0x70000005 /* Flags */ +#define DT_MIPS_BASE_ADDRESS 0x70000006 /* Base address */ +#define DT_MIPS_MSYM 0x70000007 +#define DT_MIPS_CONFLICT 0x70000008 /* Address of CONFLICT section */ +#define DT_MIPS_LIBLIST 0x70000009 /* Address of LIBLIST section */ +#define DT_MIPS_LOCAL_GOTNO 0x7000000a /* Number of local GOT entries */ +#define DT_MIPS_CONFLICTNO 0x7000000b /* Number of CONFLICT entries */ +#define DT_MIPS_LIBLISTNO 0x70000010 /* Number of LIBLIST entries */ +#define DT_MIPS_SYMTABNO 0x70000011 /* Number of DYNSYM entries */ +#define DT_MIPS_UNREFEXTNO 0x70000012 /* First external DYNSYM */ +#define DT_MIPS_GOTSYM 0x70000013 /* First GOT entry in DYNSYM */ +#define DT_MIPS_HIPAGENO 0x70000014 /* Number of GOT page table entries */ +#define DT_MIPS_RLD_MAP 0x70000016 /* Address of run time loader map. */ +#define DT_MIPS_DELTA_CLASS 0x70000017 /* Delta C++ class definition. */ +#define DT_MIPS_DELTA_CLASS_NO 0x70000018 /* Number of entries in + DT_MIPS_DELTA_CLASS. */ +#define DT_MIPS_DELTA_INSTANCE 0x70000019 /* Delta C++ class instances. */ +#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a /* Number of entries in + DT_MIPS_DELTA_INSTANCE. */ +#define DT_MIPS_DELTA_RELOC 0x7000001b /* Delta relocations. */ +#define DT_MIPS_DELTA_RELOC_NO 0x7000001c /* Number of entries in + DT_MIPS_DELTA_RELOC. */ +#define DT_MIPS_DELTA_SYM 0x7000001d /* Delta symbols that Delta + relocations refer to. */ +#define DT_MIPS_DELTA_SYM_NO 0x7000001e /* Number of entries in + DT_MIPS_DELTA_SYM. */ +#define DT_MIPS_DELTA_CLASSSYM 0x70000020 /* Delta symbols that hold the + class declaration. */ +#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 /* Number of entries in + DT_MIPS_DELTA_CLASSSYM. */ +#define DT_MIPS_CXX_FLAGS 0x70000022 /* Flags indicating for C++ flavor. */ +#define DT_MIPS_PIXIE_INIT 0x70000023 +#define DT_MIPS_SYMBOL_LIB 0x70000024 +#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025 +#define DT_MIPS_LOCAL_GOTIDX 0x70000026 +#define DT_MIPS_HIDDEN_GOTIDX 0x70000027 +#define DT_MIPS_PROTECTED_GOTIDX 0x70000028 +#define DT_MIPS_OPTIONS 0x70000029 /* Address of .options. */ +#define DT_MIPS_INTERFACE 0x7000002a /* Address of .interface. */ +#define DT_MIPS_DYNSTR_ALIGN 0x7000002b +#define DT_MIPS_INTERFACE_SIZE 0x7000002c /* Size of the .interface section. */ +#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d /* Address of rld_text_rsolve + function stored in GOT. */ +#define DT_MIPS_PERF_SUFFIX 0x7000002e /* Default suffix of dso to be added + by rld on dlopen() calls. */ +#define DT_MIPS_COMPACT_SIZE 0x7000002f /* (O32)Size of compact rel section. */ +#define DT_MIPS_GP_VALUE 0x70000030 /* GP value for aux GOTs. */ +#define DT_MIPS_AUX_DYNAMIC 0x70000031 /* Address of aux .dynamic. */ +/* The address of .got.plt in an executable using the new non-PIC ABI. */ +#define DT_MIPS_PLTGOT 0x70000032 +/* The base of the PLT in an executable using the new non-PIC ABI if that + PLT is writable. For a non-writable PLT, this is omitted or has a zero + value. */ +#define DT_MIPS_RWPLT 0x70000034 +#define DT_MIPS_NUM 0x35 + +/* Legal values for DT_MIPS_FLAGS Elf32_Dyn entry. */ + +#define RHF_NONE 0 /* No flags */ +#define RHF_QUICKSTART (1 << 0) /* Use quickstart */ +#define RHF_NOTPOT (1 << 1) /* Hash size not power of 2 */ +#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) /* Ignore LD_LIBRARY_PATH */ +#define RHF_NO_MOVE (1 << 3) +#define RHF_SGI_ONLY (1 << 4) +#define RHF_GUARANTEE_INIT (1 << 5) +#define RHF_DELTA_C_PLUS_PLUS (1 << 6) +#define RHF_GUARANTEE_START_INIT (1 << 7) +#define RHF_PIXIE (1 << 8) +#define RHF_DEFAULT_DELAY_LOAD (1 << 9) +#define RHF_REQUICKSTART (1 << 10) +#define RHF_REQUICKSTARTED (1 << 11) +#define RHF_CORD (1 << 12) +#define RHF_NO_UNRES_UNDEF (1 << 13) +#define RHF_RLD_ORDER_SAFE (1 << 14) + +/* Entries found in sections of type SHT_MIPS_LIBLIST. */ + +typedef struct +{ + Elf32_Word l_name; /* Name (string table index) */ + Elf32_Word l_time_stamp; /* Timestamp */ + Elf32_Word l_checksum; /* Checksum */ + Elf32_Word l_version; /* Interface version */ + Elf32_Word l_flags; /* Flags */ +} Elf32_Lib; + +typedef struct +{ + Elf64_Word l_name; /* Name (string table index) */ + Elf64_Word l_time_stamp; /* Timestamp */ + Elf64_Word l_checksum; /* Checksum */ + Elf64_Word l_version; /* Interface version */ + Elf64_Word l_flags; /* Flags */ +} Elf64_Lib; + + +/* Legal values for l_flags. */ + +#define LL_NONE 0 +#define LL_EXACT_MATCH (1 << 0) /* Require exact match */ +#define LL_IGNORE_INT_VER (1 << 1) /* Ignore interface version */ +#define LL_REQUIRE_MINOR (1 << 2) +#define LL_EXPORTS (1 << 3) +#define LL_DELAY_LOAD (1 << 4) +#define LL_DELTA (1 << 5) + +/* Entries found in sections of type SHT_MIPS_CONFLICT. */ + +typedef Elf32_Addr Elf32_Conflict; + + +/* HPPA specific definitions. */ + +/* Legal values for e_flags field of Elf32_Ehdr. */ + +#define EF_PARISC_TRAPNIL 0x00010000 /* Trap nil pointer dereference. */ +#define EF_PARISC_EXT 0x00020000 /* Program uses arch. extensions. */ +#define EF_PARISC_LSB 0x00040000 /* Program expects little endian. */ +#define EF_PARISC_WIDE 0x00080000 /* Program expects wide mode. */ +#define EF_PARISC_NO_KABP 0x00100000 /* No kernel assisted branch + prediction. */ +#define EF_PARISC_LAZYSWAP 0x00400000 /* Allow lazy swapping. */ +#define EF_PARISC_ARCH 0x0000ffff /* Architecture version. */ + +/* Defined values for `e_flags & EF_PARISC_ARCH' are: */ + +#define EFA_PARISC_1_0 0x020b /* PA-RISC 1.0 big-endian. */ +#define EFA_PARISC_1_1 0x0210 /* PA-RISC 1.1 big-endian. */ +#define EFA_PARISC_2_0 0x0214 /* PA-RISC 2.0 big-endian. */ + +/* Additional section indices. */ + +#define SHN_PARISC_ANSI_COMMON 0xff00 /* Section for tentatively declared + symbols in ANSI C. */ +#define SHN_PARISC_HUGE_COMMON 0xff01 /* Common blocks in huge model. */ + +/* Legal values for sh_type field of Elf32_Shdr. */ + +#define SHT_PARISC_EXT 0x70000000 /* Contains product specific ext. */ +#define SHT_PARISC_UNWIND 0x70000001 /* Unwind information. */ +#define SHT_PARISC_DOC 0x70000002 /* Debug info for optimized code. */ + +/* Legal values for sh_flags field of Elf32_Shdr. */ + +#define SHF_PARISC_SHORT 0x20000000 /* Section with short addressing. */ +#define SHF_PARISC_HUGE 0x40000000 /* Section far from gp. */ +#define SHF_PARISC_SBP 0x80000000 /* Static branch prediction code. */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_PARISC_MILLICODE 13 /* Millicode function entry point. */ + +#define STT_HP_OPAQUE (STT_LOOS + 0x1) +#define STT_HP_STUB (STT_LOOS + 0x2) + +/* HPPA relocs. */ + +#define R_PARISC_NONE 0 /* No reloc. */ +#define R_PARISC_DIR32 1 /* Direct 32-bit reference. */ +#define R_PARISC_DIR21L 2 /* Left 21 bits of eff. address. */ +#define R_PARISC_DIR17R 3 /* Right 17 bits of eff. address. */ +#define R_PARISC_DIR17F 4 /* 17 bits of eff. address. */ +#define R_PARISC_DIR14R 6 /* Right 14 bits of eff. address. */ +#define R_PARISC_PCREL32 9 /* 32-bit rel. address. */ +#define R_PARISC_PCREL21L 10 /* Left 21 bits of rel. address. */ +#define R_PARISC_PCREL17R 11 /* Right 17 bits of rel. address. */ +#define R_PARISC_PCREL17F 12 /* 17 bits of rel. address. */ +#define R_PARISC_PCREL14R 14 /* Right 14 bits of rel. address. */ +#define R_PARISC_DPREL21L 18 /* Left 21 bits of rel. address. */ +#define R_PARISC_DPREL14R 22 /* Right 14 bits of rel. address. */ +#define R_PARISC_GPREL21L 26 /* GP-relative, left 21 bits. */ +#define R_PARISC_GPREL14R 30 /* GP-relative, right 14 bits. */ +#define R_PARISC_LTOFF21L 34 /* LT-relative, left 21 bits. */ +#define R_PARISC_LTOFF14R 38 /* LT-relative, right 14 bits. */ +#define R_PARISC_SECREL32 41 /* 32 bits section rel. address. */ +#define R_PARISC_SEGBASE 48 /* No relocation, set segment base. */ +#define R_PARISC_SEGREL32 49 /* 32 bits segment rel. address. */ +#define R_PARISC_PLTOFF21L 50 /* PLT rel. address, left 21 bits. */ +#define R_PARISC_PLTOFF14R 54 /* PLT rel. address, right 14 bits. */ +#define R_PARISC_LTOFF_FPTR32 57 /* 32 bits LT-rel. function pointer. */ +#define R_PARISC_LTOFF_FPTR21L 58 /* LT-rel. fct ptr, left 21 bits. */ +#define R_PARISC_LTOFF_FPTR14R 62 /* LT-rel. fct ptr, right 14 bits. */ +#define R_PARISC_FPTR64 64 /* 64 bits function address. */ +#define R_PARISC_PLABEL32 65 /* 32 bits function address. */ +#define R_PARISC_PLABEL21L 66 /* Left 21 bits of fdesc address. */ +#define R_PARISC_PLABEL14R 70 /* Right 14 bits of fdesc address. */ +#define R_PARISC_PCREL64 72 /* 64 bits PC-rel. address. */ +#define R_PARISC_PCREL22F 74 /* 22 bits PC-rel. address. */ +#define R_PARISC_PCREL14WR 75 /* PC-rel. address, right 14 bits. */ +#define R_PARISC_PCREL14DR 76 /* PC rel. address, right 14 bits. */ +#define R_PARISC_PCREL16F 77 /* 16 bits PC-rel. address. */ +#define R_PARISC_PCREL16WF 78 /* 16 bits PC-rel. address. */ +#define R_PARISC_PCREL16DF 79 /* 16 bits PC-rel. address. */ +#define R_PARISC_DIR64 80 /* 64 bits of eff. address. */ +#define R_PARISC_DIR14WR 83 /* 14 bits of eff. address. */ +#define R_PARISC_DIR14DR 84 /* 14 bits of eff. address. */ +#define R_PARISC_DIR16F 85 /* 16 bits of eff. address. */ +#define R_PARISC_DIR16WF 86 /* 16 bits of eff. address. */ +#define R_PARISC_DIR16DF 87 /* 16 bits of eff. address. */ +#define R_PARISC_GPREL64 88 /* 64 bits of GP-rel. address. */ +#define R_PARISC_GPREL14WR 91 /* GP-rel. address, right 14 bits. */ +#define R_PARISC_GPREL14DR 92 /* GP-rel. address, right 14 bits. */ +#define R_PARISC_GPREL16F 93 /* 16 bits GP-rel. address. */ +#define R_PARISC_GPREL16WF 94 /* 16 bits GP-rel. address. */ +#define R_PARISC_GPREL16DF 95 /* 16 bits GP-rel. address. */ +#define R_PARISC_LTOFF64 96 /* 64 bits LT-rel. address. */ +#define R_PARISC_LTOFF14WR 99 /* LT-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF14DR 100 /* LT-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF16F 101 /* 16 bits LT-rel. address. */ +#define R_PARISC_LTOFF16WF 102 /* 16 bits LT-rel. address. */ +#define R_PARISC_LTOFF16DF 103 /* 16 bits LT-rel. address. */ +#define R_PARISC_SECREL64 104 /* 64 bits section rel. address. */ +#define R_PARISC_SEGREL64 112 /* 64 bits segment rel. address. */ +#define R_PARISC_PLTOFF14WR 115 /* PLT-rel. address, right 14 bits. */ +#define R_PARISC_PLTOFF14DR 116 /* PLT-rel. address, right 14 bits. */ +#define R_PARISC_PLTOFF16F 117 /* 16 bits LT-rel. address. */ +#define R_PARISC_PLTOFF16WF 118 /* 16 bits PLT-rel. address. */ +#define R_PARISC_PLTOFF16DF 119 /* 16 bits PLT-rel. address. */ +#define R_PARISC_LTOFF_FPTR64 120 /* 64 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR14WR 123 /* LT-rel. fct. ptr., right 14 bits. */ +#define R_PARISC_LTOFF_FPTR14DR 124 /* LT-rel. fct. ptr., right 14 bits. */ +#define R_PARISC_LTOFF_FPTR16F 125 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR16WF 126 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR16DF 127 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LORESERVE 128 +#define R_PARISC_COPY 128 /* Copy relocation. */ +#define R_PARISC_IPLT 129 /* Dynamic reloc, imported PLT */ +#define R_PARISC_EPLT 130 /* Dynamic reloc, exported PLT */ +#define R_PARISC_TPREL32 153 /* 32 bits TP-rel. address. */ +#define R_PARISC_TPREL21L 154 /* TP-rel. address, left 21 bits. */ +#define R_PARISC_TPREL14R 158 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF_TP21L 162 /* LT-TP-rel. address, left 21 bits. */ +#define R_PARISC_LTOFF_TP14R 166 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP14F 167 /* 14 bits LT-TP-rel. address. */ +#define R_PARISC_TPREL64 216 /* 64 bits TP-rel. address. */ +#define R_PARISC_TPREL14WR 219 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_TPREL14DR 220 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_TPREL16F 221 /* 16 bits TP-rel. address. */ +#define R_PARISC_TPREL16WF 222 /* 16 bits TP-rel. address. */ +#define R_PARISC_TPREL16DF 223 /* 16 bits TP-rel. address. */ +#define R_PARISC_LTOFF_TP64 224 /* 64 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP14WR 227 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP14DR 228 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP16F 229 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP16WF 230 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP16DF 231 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_GNU_VTENTRY 232 +#define R_PARISC_GNU_VTINHERIT 233 +#define R_PARISC_TLS_GD21L 234 /* GD 21-bit left. */ +#define R_PARISC_TLS_GD14R 235 /* GD 14-bit right. */ +#define R_PARISC_TLS_GDCALL 236 /* GD call to __t_g_a. */ +#define R_PARISC_TLS_LDM21L 237 /* LD module 21-bit left. */ +#define R_PARISC_TLS_LDM14R 238 /* LD module 14-bit right. */ +#define R_PARISC_TLS_LDMCALL 239 /* LD module call to __t_g_a. */ +#define R_PARISC_TLS_LDO21L 240 /* LD offset 21-bit left. */ +#define R_PARISC_TLS_LDO14R 241 /* LD offset 14-bit right. */ +#define R_PARISC_TLS_DTPMOD32 242 /* DTP module 32-bit. */ +#define R_PARISC_TLS_DTPMOD64 243 /* DTP module 64-bit. */ +#define R_PARISC_TLS_DTPOFF32 244 /* DTP offset 32-bit. */ +#define R_PARISC_TLS_DTPOFF64 245 /* DTP offset 32-bit. */ +#define R_PARISC_TLS_LE21L R_PARISC_TPREL21L +#define R_PARISC_TLS_LE14R R_PARISC_TPREL14R +#define R_PARISC_TLS_IE21L R_PARISC_LTOFF_TP21L +#define R_PARISC_TLS_IE14R R_PARISC_LTOFF_TP14R +#define R_PARISC_TLS_TPREL32 R_PARISC_TPREL32 +#define R_PARISC_TLS_TPREL64 R_PARISC_TPREL64 +#define R_PARISC_HIRESERVE 255 + +/* Legal values for p_type field of Elf32_Phdr/Elf64_Phdr. */ + +#define PT_HP_TLS (PT_LOOS + 0x0) +#define PT_HP_CORE_NONE (PT_LOOS + 0x1) +#define PT_HP_CORE_VERSION (PT_LOOS + 0x2) +#define PT_HP_CORE_KERNEL (PT_LOOS + 0x3) +#define PT_HP_CORE_COMM (PT_LOOS + 0x4) +#define PT_HP_CORE_PROC (PT_LOOS + 0x5) +#define PT_HP_CORE_LOADABLE (PT_LOOS + 0x6) +#define PT_HP_CORE_STACK (PT_LOOS + 0x7) +#define PT_HP_CORE_SHM (PT_LOOS + 0x8) +#define PT_HP_CORE_MMF (PT_LOOS + 0x9) +#define PT_HP_PARALLEL (PT_LOOS + 0x10) +#define PT_HP_FASTBIND (PT_LOOS + 0x11) +#define PT_HP_OPT_ANNOT (PT_LOOS + 0x12) +#define PT_HP_HSL_ANNOT (PT_LOOS + 0x13) +#define PT_HP_STACK (PT_LOOS + 0x14) + +#define PT_PARISC_ARCHEXT 0x70000000 +#define PT_PARISC_UNWIND 0x70000001 + +/* Legal values for p_flags field of Elf32_Phdr/Elf64_Phdr. */ + +#define PF_PARISC_SBP 0x08000000 + +#define PF_HP_PAGE_SIZE 0x00100000 +#define PF_HP_FAR_SHARED 0x00200000 +#define PF_HP_NEAR_SHARED 0x00400000 +#define PF_HP_CODE 0x01000000 +#define PF_HP_MODIFY 0x02000000 +#define PF_HP_LAZYSWAP 0x04000000 +#define PF_HP_SBP 0x08000000 + + +/* Alpha specific definitions. */ + +/* Legal values for e_flags field of Elf64_Ehdr. */ + +#define EF_ALPHA_32BIT 1 /* All addresses must be < 2GB. */ +#define EF_ALPHA_CANRELAX 2 /* Relocations for relaxing exist. */ + +/* Legal values for sh_type field of Elf64_Shdr. */ + +/* These two are primarily concerned with ECOFF debugging info. */ +#define SHT_ALPHA_DEBUG 0x70000001 +#define SHT_ALPHA_REGINFO 0x70000002 + +/* Legal values for sh_flags field of Elf64_Shdr. */ + +#define SHF_ALPHA_GPREL 0x10000000 + +/* Legal values for st_other field of Elf64_Sym. */ +#define STO_ALPHA_NOPV 0x80 /* No PV required. */ +#define STO_ALPHA_STD_GPLOAD 0x88 /* PV only used for initial ldgp. */ + +/* Alpha relocs. */ + +#define R_ALPHA_NONE 0 /* No reloc */ +#define R_ALPHA_REFLONG 1 /* Direct 32 bit */ +#define R_ALPHA_REFQUAD 2 /* Direct 64 bit */ +#define R_ALPHA_GPREL32 3 /* GP relative 32 bit */ +#define R_ALPHA_LITERAL 4 /* GP relative 16 bit w/optimization */ +#define R_ALPHA_LITUSE 5 /* Optimization hint for LITERAL */ +#define R_ALPHA_GPDISP 6 /* Add displacement to GP */ +#define R_ALPHA_BRADDR 7 /* PC+4 relative 23 bit shifted */ +#define R_ALPHA_HINT 8 /* PC+4 relative 16 bit shifted */ +#define R_ALPHA_SREL16 9 /* PC relative 16 bit */ +#define R_ALPHA_SREL32 10 /* PC relative 32 bit */ +#define R_ALPHA_SREL64 11 /* PC relative 64 bit */ +#define R_ALPHA_GPRELHIGH 17 /* GP relative 32 bit, high 16 bits */ +#define R_ALPHA_GPRELLOW 18 /* GP relative 32 bit, low 16 bits */ +#define R_ALPHA_GPREL16 19 /* GP relative 16 bit */ +#define R_ALPHA_COPY 24 /* Copy symbol at runtime */ +#define R_ALPHA_GLOB_DAT 25 /* Create GOT entry */ +#define R_ALPHA_JMP_SLOT 26 /* Create PLT entry */ +#define R_ALPHA_RELATIVE 27 /* Adjust by program base */ +#define R_ALPHA_TLS_GD_HI 28 +#define R_ALPHA_TLSGD 29 +#define R_ALPHA_TLS_LDM 30 +#define R_ALPHA_DTPMOD64 31 +#define R_ALPHA_GOTDTPREL 32 +#define R_ALPHA_DTPREL64 33 +#define R_ALPHA_DTPRELHI 34 +#define R_ALPHA_DTPRELLO 35 +#define R_ALPHA_DTPREL16 36 +#define R_ALPHA_GOTTPREL 37 +#define R_ALPHA_TPREL64 38 +#define R_ALPHA_TPRELHI 39 +#define R_ALPHA_TPRELLO 40 +#define R_ALPHA_TPREL16 41 +/* Keep this the last entry. */ +#define R_ALPHA_NUM 46 + +/* Magic values of the LITUSE relocation addend. */ +#define LITUSE_ALPHA_ADDR 0 +#define LITUSE_ALPHA_BASE 1 +#define LITUSE_ALPHA_BYTOFF 2 +#define LITUSE_ALPHA_JSR 3 +#define LITUSE_ALPHA_TLS_GD 4 +#define LITUSE_ALPHA_TLS_LDM 5 + +/* Legal values for d_tag of Elf64_Dyn. */ +#define DT_ALPHA_PLTRO (DT_LOPROC + 0) +#define DT_ALPHA_NUM 1 + +/* PowerPC specific declarations */ + +/* Values for Elf32/64_Ehdr.e_flags. */ +#define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */ + +/* Cygnus local bits below */ +#define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/ +#define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib + flag */ + +/* PowerPC relocations defined by the ABIs */ +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 /* 32bit absolute address */ +#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */ +#define R_PPC_ADDR16 3 /* 16bit absolute address */ +#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */ +#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */ +#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */ +#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */ +#define R_PPC_ADDR14_BRTAKEN 8 +#define R_PPC_ADDR14_BRNTAKEN 9 +#define R_PPC_REL24 10 /* PC relative 26 bit */ +#define R_PPC_REL14 11 /* PC relative 16 bit */ +#define R_PPC_REL14_BRTAKEN 12 +#define R_PPC_REL14_BRNTAKEN 13 +#define R_PPC_GOT16 14 +#define R_PPC_GOT16_LO 15 +#define R_PPC_GOT16_HI 16 +#define R_PPC_GOT16_HA 17 +#define R_PPC_PLTREL24 18 +#define R_PPC_COPY 19 +#define R_PPC_GLOB_DAT 20 +#define R_PPC_JMP_SLOT 21 +#define R_PPC_RELATIVE 22 +#define R_PPC_LOCAL24PC 23 +#define R_PPC_UADDR32 24 +#define R_PPC_UADDR16 25 +#define R_PPC_REL32 26 +#define R_PPC_PLT32 27 +#define R_PPC_PLTREL32 28 +#define R_PPC_PLT16_LO 29 +#define R_PPC_PLT16_HI 30 +#define R_PPC_PLT16_HA 31 +#define R_PPC_SDAREL16 32 +#define R_PPC_SECTOFF 33 +#define R_PPC_SECTOFF_LO 34 +#define R_PPC_SECTOFF_HI 35 +#define R_PPC_SECTOFF_HA 36 + +/* PowerPC relocations defined for the TLS access ABI. */ +#define R_PPC_TLS 67 /* none (sym+add)@tls */ +#define R_PPC_DTPMOD32 68 /* word32 (sym+add)@dtpmod */ +#define R_PPC_TPREL16 69 /* half16* (sym+add)@tprel */ +#define R_PPC_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */ +#define R_PPC_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */ +#define R_PPC_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */ +#define R_PPC_TPREL32 73 /* word32 (sym+add)@tprel */ +#define R_PPC_DTPREL16 74 /* half16* (sym+add)@dtprel */ +#define R_PPC_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */ +#define R_PPC_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */ +#define R_PPC_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */ +#define R_PPC_DTPREL32 78 /* word32 (sym+add)@dtprel */ +#define R_PPC_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */ +#define R_PPC_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */ +#define R_PPC_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */ +#define R_PPC_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */ +#define R_PPC_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */ +#define R_PPC_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */ +#define R_PPC_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */ +#define R_PPC_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */ +#define R_PPC_GOT_TPREL16 87 /* half16* (sym+add)@got@tprel */ +#define R_PPC_GOT_TPREL16_LO 88 /* half16 (sym+add)@got@tprel@l */ +#define R_PPC_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */ +#define R_PPC_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */ +#define R_PPC_GOT_DTPREL16 91 /* half16* (sym+add)@got@dtprel */ +#define R_PPC_GOT_DTPREL16_LO 92 /* half16* (sym+add)@got@dtprel@l */ +#define R_PPC_GOT_DTPREL16_HI 93 /* half16* (sym+add)@got@dtprel@h */ +#define R_PPC_GOT_DTPREL16_HA 94 /* half16* (sym+add)@got@dtprel@ha */ + +/* The remaining relocs are from the Embedded ELF ABI, and are not + in the SVR4 ELF ABI. */ +#define R_PPC_EMB_NADDR32 101 +#define R_PPC_EMB_NADDR16 102 +#define R_PPC_EMB_NADDR16_LO 103 +#define R_PPC_EMB_NADDR16_HI 104 +#define R_PPC_EMB_NADDR16_HA 105 +#define R_PPC_EMB_SDAI16 106 +#define R_PPC_EMB_SDA2I16 107 +#define R_PPC_EMB_SDA2REL 108 +#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */ +#define R_PPC_EMB_MRKREF 110 +#define R_PPC_EMB_RELSEC16 111 +#define R_PPC_EMB_RELST_LO 112 +#define R_PPC_EMB_RELST_HI 113 +#define R_PPC_EMB_RELST_HA 114 +#define R_PPC_EMB_BIT_FLD 115 +#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */ + +/* Diab tool relocations. */ +#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */ +#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */ +#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */ +#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */ +#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */ +#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */ + +/* GNU extension to support local ifunc. */ +#define R_PPC_IRELATIVE 248 + +/* GNU relocs used in PIC code sequences. */ +#define R_PPC_REL16 249 /* half16 (sym+add-.) */ +#define R_PPC_REL16_LO 250 /* half16 (sym+add-.)@l */ +#define R_PPC_REL16_HI 251 /* half16 (sym+add-.)@h */ +#define R_PPC_REL16_HA 252 /* half16 (sym+add-.)@ha */ + +/* This is a phony reloc to handle any old fashioned TOC16 references + that may still be in object files. */ +#define R_PPC_TOC16 255 + +/* PowerPC specific values for the Dyn d_tag field. */ +#define DT_PPC_GOT (DT_LOPROC + 0) +#define DT_PPC_NUM 1 + +/* PowerPC64 relocations defined by the ABIs */ +#define R_PPC64_NONE R_PPC_NONE +#define R_PPC64_ADDR32 R_PPC_ADDR32 /* 32bit absolute address */ +#define R_PPC64_ADDR24 R_PPC_ADDR24 /* 26bit address, word aligned */ +#define R_PPC64_ADDR16 R_PPC_ADDR16 /* 16bit absolute address */ +#define R_PPC64_ADDR16_LO R_PPC_ADDR16_LO /* lower 16bits of address */ +#define R_PPC64_ADDR16_HI R_PPC_ADDR16_HI /* high 16bits of address. */ +#define R_PPC64_ADDR16_HA R_PPC_ADDR16_HA /* adjusted high 16bits. */ +#define R_PPC64_ADDR14 R_PPC_ADDR14 /* 16bit address, word aligned */ +#define R_PPC64_ADDR14_BRTAKEN R_PPC_ADDR14_BRTAKEN +#define R_PPC64_ADDR14_BRNTAKEN R_PPC_ADDR14_BRNTAKEN +#define R_PPC64_REL24 R_PPC_REL24 /* PC-rel. 26 bit, word aligned */ +#define R_PPC64_REL14 R_PPC_REL14 /* PC relative 16 bit */ +#define R_PPC64_REL14_BRTAKEN R_PPC_REL14_BRTAKEN +#define R_PPC64_REL14_BRNTAKEN R_PPC_REL14_BRNTAKEN +#define R_PPC64_GOT16 R_PPC_GOT16 +#define R_PPC64_GOT16_LO R_PPC_GOT16_LO +#define R_PPC64_GOT16_HI R_PPC_GOT16_HI +#define R_PPC64_GOT16_HA R_PPC_GOT16_HA + +#define R_PPC64_COPY R_PPC_COPY +#define R_PPC64_GLOB_DAT R_PPC_GLOB_DAT +#define R_PPC64_JMP_SLOT R_PPC_JMP_SLOT +#define R_PPC64_RELATIVE R_PPC_RELATIVE + +#define R_PPC64_UADDR32 R_PPC_UADDR32 +#define R_PPC64_UADDR16 R_PPC_UADDR16 +#define R_PPC64_REL32 R_PPC_REL32 +#define R_PPC64_PLT32 R_PPC_PLT32 +#define R_PPC64_PLTREL32 R_PPC_PLTREL32 +#define R_PPC64_PLT16_LO R_PPC_PLT16_LO +#define R_PPC64_PLT16_HI R_PPC_PLT16_HI +#define R_PPC64_PLT16_HA R_PPC_PLT16_HA + +#define R_PPC64_SECTOFF R_PPC_SECTOFF +#define R_PPC64_SECTOFF_LO R_PPC_SECTOFF_LO +#define R_PPC64_SECTOFF_HI R_PPC_SECTOFF_HI +#define R_PPC64_SECTOFF_HA R_PPC_SECTOFF_HA +#define R_PPC64_ADDR30 37 /* word30 (S + A - P) >> 2 */ +#define R_PPC64_ADDR64 38 /* doubleword64 S + A */ +#define R_PPC64_ADDR16_HIGHER 39 /* half16 #higher(S + A) */ +#define R_PPC64_ADDR16_HIGHERA 40 /* half16 #highera(S + A) */ +#define R_PPC64_ADDR16_HIGHEST 41 /* half16 #highest(S + A) */ +#define R_PPC64_ADDR16_HIGHESTA 42 /* half16 #highesta(S + A) */ +#define R_PPC64_UADDR64 43 /* doubleword64 S + A */ +#define R_PPC64_REL64 44 /* doubleword64 S + A - P */ +#define R_PPC64_PLT64 45 /* doubleword64 L + A */ +#define R_PPC64_PLTREL64 46 /* doubleword64 L + A - P */ +#define R_PPC64_TOC16 47 /* half16* S + A - .TOC */ +#define R_PPC64_TOC16_LO 48 /* half16 #lo(S + A - .TOC.) */ +#define R_PPC64_TOC16_HI 49 /* half16 #hi(S + A - .TOC.) */ +#define R_PPC64_TOC16_HA 50 /* half16 #ha(S + A - .TOC.) */ +#define R_PPC64_TOC 51 /* doubleword64 .TOC */ +#define R_PPC64_PLTGOT16 52 /* half16* M + A */ +#define R_PPC64_PLTGOT16_LO 53 /* half16 #lo(M + A) */ +#define R_PPC64_PLTGOT16_HI 54 /* half16 #hi(M + A) */ +#define R_PPC64_PLTGOT16_HA 55 /* half16 #ha(M + A) */ + +#define R_PPC64_ADDR16_DS 56 /* half16ds* (S + A) >> 2 */ +#define R_PPC64_ADDR16_LO_DS 57 /* half16ds #lo(S + A) >> 2 */ +#define R_PPC64_GOT16_DS 58 /* half16ds* (G + A) >> 2 */ +#define R_PPC64_GOT16_LO_DS 59 /* half16ds #lo(G + A) >> 2 */ +#define R_PPC64_PLT16_LO_DS 60 /* half16ds #lo(L + A) >> 2 */ +#define R_PPC64_SECTOFF_DS 61 /* half16ds* (R + A) >> 2 */ +#define R_PPC64_SECTOFF_LO_DS 62 /* half16ds #lo(R + A) >> 2 */ +#define R_PPC64_TOC16_DS 63 /* half16ds* (S + A - .TOC.) >> 2 */ +#define R_PPC64_TOC16_LO_DS 64 /* half16ds #lo(S + A - .TOC.) >> 2 */ +#define R_PPC64_PLTGOT16_DS 65 /* half16ds* (M + A) >> 2 */ +#define R_PPC64_PLTGOT16_LO_DS 66 /* half16ds #lo(M + A) >> 2 */ + +/* PowerPC64 relocations defined for the TLS access ABI. */ +#define R_PPC64_TLS 67 /* none (sym+add)@tls */ +#define R_PPC64_DTPMOD64 68 /* doubleword64 (sym+add)@dtpmod */ +#define R_PPC64_TPREL16 69 /* half16* (sym+add)@tprel */ +#define R_PPC64_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */ +#define R_PPC64_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */ +#define R_PPC64_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */ +#define R_PPC64_TPREL64 73 /* doubleword64 (sym+add)@tprel */ +#define R_PPC64_DTPREL16 74 /* half16* (sym+add)@dtprel */ +#define R_PPC64_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */ +#define R_PPC64_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */ +#define R_PPC64_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */ +#define R_PPC64_DTPREL64 78 /* doubleword64 (sym+add)@dtprel */ +#define R_PPC64_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */ +#define R_PPC64_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */ +#define R_PPC64_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */ +#define R_PPC64_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */ +#define R_PPC64_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */ +#define R_PPC64_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */ +#define R_PPC64_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */ +#define R_PPC64_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */ +#define R_PPC64_GOT_TPREL16_DS 87 /* half16ds* (sym+add)@got@tprel */ +#define R_PPC64_GOT_TPREL16_LO_DS 88 /* half16ds (sym+add)@got@tprel@l */ +#define R_PPC64_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */ +#define R_PPC64_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */ +#define R_PPC64_GOT_DTPREL16_DS 91 /* half16ds* (sym+add)@got@dtprel */ +#define R_PPC64_GOT_DTPREL16_LO_DS 92 /* half16ds (sym+add)@got@dtprel@l */ +#define R_PPC64_GOT_DTPREL16_HI 93 /* half16 (sym+add)@got@dtprel@h */ +#define R_PPC64_GOT_DTPREL16_HA 94 /* half16 (sym+add)@got@dtprel@ha */ +#define R_PPC64_TPREL16_DS 95 /* half16ds* (sym+add)@tprel */ +#define R_PPC64_TPREL16_LO_DS 96 /* half16ds (sym+add)@tprel@l */ +#define R_PPC64_TPREL16_HIGHER 97 /* half16 (sym+add)@tprel@higher */ +#define R_PPC64_TPREL16_HIGHERA 98 /* half16 (sym+add)@tprel@highera */ +#define R_PPC64_TPREL16_HIGHEST 99 /* half16 (sym+add)@tprel@highest */ +#define R_PPC64_TPREL16_HIGHESTA 100 /* half16 (sym+add)@tprel@highesta */ +#define R_PPC64_DTPREL16_DS 101 /* half16ds* (sym+add)@dtprel */ +#define R_PPC64_DTPREL16_LO_DS 102 /* half16ds (sym+add)@dtprel@l */ +#define R_PPC64_DTPREL16_HIGHER 103 /* half16 (sym+add)@dtprel@higher */ +#define R_PPC64_DTPREL16_HIGHERA 104 /* half16 (sym+add)@dtprel@highera */ +#define R_PPC64_DTPREL16_HIGHEST 105 /* half16 (sym+add)@dtprel@highest */ +#define R_PPC64_DTPREL16_HIGHESTA 106 /* half16 (sym+add)@dtprel@highesta */ + +/* GNU extension to support local ifunc. */ +#define R_PPC64_JMP_IREL 247 +#define R_PPC64_IRELATIVE 248 +#define R_PPC64_REL16 249 /* half16 (sym+add-.) */ +#define R_PPC64_REL16_LO 250 /* half16 (sym+add-.)@l */ +#define R_PPC64_REL16_HI 251 /* half16 (sym+add-.)@h */ +#define R_PPC64_REL16_HA 252 /* half16 (sym+add-.)@ha */ + +/* PowerPC64 specific values for the Dyn d_tag field. */ +#define DT_PPC64_GLINK (DT_LOPROC + 0) +#define DT_PPC64_OPD (DT_LOPROC + 1) +#define DT_PPC64_OPDSZ (DT_LOPROC + 2) +#define DT_PPC64_NUM 3 + + +/* ARM specific declarations */ + +/* Processor specific flags for the ELF header e_flags field. */ +#define EF_ARM_RELEXEC 0x01 +#define EF_ARM_HASENTRY 0x02 +#define EF_ARM_INTERWORK 0x04 +#define EF_ARM_APCS_26 0x08 +#define EF_ARM_APCS_FLOAT 0x10 +#define EF_ARM_PIC 0x20 +#define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */ +#define EF_ARM_NEW_ABI 0x80 +#define EF_ARM_OLD_ABI 0x100 +#define EF_ARM_SOFT_FLOAT 0x200 +#define EF_ARM_VFP_FLOAT 0x400 +#define EF_ARM_MAVERICK_FLOAT 0x800 + +#define EF_ARM_ABI_FLOAT_SOFT 0x200 /* NB conflicts with EF_ARM_SOFT_FLOAT */ +#define EF_ARM_ABI_FLOAT_HARD 0x400 /* NB conflicts with EF_ARM_VFP_FLOAT */ + + +/* Other constants defined in the ARM ELF spec. version B-01. */ +/* NB. These conflict with values defined above. */ +#define EF_ARM_SYMSARESORTED 0x04 +#define EF_ARM_DYNSYMSUSESEGIDX 0x08 +#define EF_ARM_MAPSYMSFIRST 0x10 +#define EF_ARM_EABIMASK 0XFF000000 + +/* Constants defined in AAELF. */ +#define EF_ARM_BE8 0x00800000 +#define EF_ARM_LE8 0x00400000 + +#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK) +#define EF_ARM_EABI_UNKNOWN 0x00000000 +#define EF_ARM_EABI_VER1 0x01000000 +#define EF_ARM_EABI_VER2 0x02000000 +#define EF_ARM_EABI_VER3 0x03000000 +#define EF_ARM_EABI_VER4 0x04000000 +#define EF_ARM_EABI_VER5 0x05000000 + +/* Additional symbol types for Thumb. */ +#define STT_ARM_TFUNC STT_LOPROC /* A Thumb function. */ +#define STT_ARM_16BIT STT_HIPROC /* A Thumb label. */ + +/* ARM-specific values for sh_flags */ +#define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */ +#define SHF_ARM_COMDEF 0x80000000 /* Section may be multiply defined + in the input to a link step. */ + +/* ARM-specific program header flags */ +#define PF_ARM_SB 0x10000000 /* Segment contains the location + addressed by the static base. */ +#define PF_ARM_PI 0x20000000 /* Position-independent segment. */ +#define PF_ARM_ABS 0x40000000 /* Absolute segment. */ + +/* Processor specific values for the Phdr p_type field. */ +#define PT_ARM_EXIDX (PT_LOPROC + 1) /* ARM unwind segment. */ + +/* Processor specific values for the Shdr sh_type field. */ +#define SHT_ARM_EXIDX (SHT_LOPROC + 1) /* ARM unwind section. */ +#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) /* Preemption details. */ +#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) /* ARM attributes section. */ + + +/* AArch64 relocs. */ + +#define R_AARCH64_NONE 0 /* No relocation. */ +#define R_AARCH64_ABS64 257 /* Direct 64 bit. */ +#define R_AARCH64_ABS32 258 /* Direct 32 bit. */ +#define R_AARCH64_ABS16 259 /* Direct 16-bit. */ +#define R_AARCH64_PREL64 260 /* PC-relative 64-bit. */ +#define R_AARCH64_PREL32 261 /* PC-relative 32-bit. */ +#define R_AARCH64_PREL16 262 /* PC-relative 16-bit. */ +#define R_AARCH64_MOVW_UABS_G0 263 /* Dir. MOVZ imm. from bits 15:0. */ +#define R_AARCH64_MOVW_UABS_G0_NC 264 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_UABS_G1 265 /* Dir. MOVZ imm. from bits 31:16. */ +#define R_AARCH64_MOVW_UABS_G1_NC 266 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_UABS_G2 267 /* Dir. MOVZ imm. from bits 47:32. */ +#define R_AARCH64_MOVW_UABS_G2_NC 268 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_UABS_G3 269 /* Dir. MOV{K,Z} imm. from 63:48. */ +#define R_AARCH64_MOVW_SABS_G0 270 /* Dir. MOV{N,Z} imm. from 15:0. */ +#define R_AARCH64_MOVW_SABS_G1 271 /* Dir. MOV{N,Z} imm. from 31:16. */ +#define R_AARCH64_MOVW_SABS_G2 272 /* Dir. MOV{N,Z} imm. from 47:32. */ +#define R_AARCH64_LD_PREL_LO19 273 /* PC-rel. LD imm. from bits 20:2. */ +#define R_AARCH64_ADR_PREL_LO21 274 /* PC-rel. ADR imm. from bits 20:0. */ +#define R_AARCH64_ADR_PREL_PG_HI21 275 /* Page-rel. ADRP imm. from 32:12. */ +#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 /* Likewise; no overflow check. */ +#define R_AARCH64_ADD_ABS_LO12_NC 277 /* Dir. ADD imm. from bits 11:0. */ +#define R_AARCH64_LDST8_ABS_LO12_NC 278 /* Likewise for LD/ST; no check. */ +#define R_AARCH64_TSTBR14 279 /* PC-rel. TBZ/TBNZ imm. from 15:2. */ +#define R_AARCH64_CONDBR19 280 /* PC-rel. cond. br. imm. from 20:2. */ +#define R_AARCH64_JUMP26 282 /* PC-rel. B imm. from bits 27:2. */ +#define R_AARCH64_CALL26 283 /* Likewise for CALL. */ +#define R_AARCH64_LDST16_ABS_LO12_NC 284 /* Dir. ADD imm. from bits 11:1. */ +#define R_AARCH64_LDST32_ABS_LO12_NC 285 /* Likewise for bits 11:2. */ +#define R_AARCH64_LDST64_ABS_LO12_NC 286 /* Likewise for bits 11:3. */ +#define R_AARCH64_MOVW_PREL_G0 287 /* PC-rel. MOV{N,Z} imm. from 15:0. */ +#define R_AARCH64_MOVW_PREL_G0_NC 288 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_PREL_G1 289 /* PC-rel. MOV{N,Z} imm. from 31:16. */ +#define R_AARCH64_MOVW_PREL_G1_NC 290 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_PREL_G2 291 /* PC-rel. MOV{N,Z} imm. from 47:32. */ +#define R_AARCH64_MOVW_PREL_G2_NC 292 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_PREL_G3 293 /* PC-rel. MOV{N,Z} imm. from 63:48. */ +#define R_AARCH64_LDST128_ABS_LO12_NC 299 /* Dir. ADD imm. from bits 11:4. */ +#define R_AARCH64_MOVW_GOTOFF_G0 300 /* GOT-rel. off. MOV{N,Z} imm. 15:0. */ +#define R_AARCH64_MOVW_GOTOFF_G0_NC 301 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_GOTOFF_G1 302 /* GOT-rel. o. MOV{N,Z} imm. 31:16. */ +#define R_AARCH64_MOVW_GOTOFF_G1_NC 303 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_GOTOFF_G2 304 /* GOT-rel. o. MOV{N,Z} imm. 47:32. */ +#define R_AARCH64_MOVW_GOTOFF_G2_NC 305 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_GOTOFF_G3 306 /* GOT-rel. o. MOV{N,Z} imm. 63:48. */ +#define R_AARCH64_GOTREL64 307 /* GOT-relative 64-bit. */ +#define R_AARCH64_GOTREL32 308 /* GOT-relative 32-bit. */ +#define R_AARCH64_GOT_LD_PREL19 309 /* PC-rel. GOT off. load imm. 20:2. */ +#define R_AARCH64_LD64_GOTOFF_LO15 310 /* GOT-rel. off. LD/ST imm. 14:3. */ +#define R_AARCH64_ADR_GOT_PAGE 311 /* P-page-rel. GOT off. ADRP 32:12. */ +#define R_AARCH64_LD64_GOT_LO12_NC 312 /* Dir. GOT off. LD/ST imm. 11:3. */ +#define R_AARCH64_LD64_GOTPAGE_LO15 313 /* GOT-page-rel. GOT off. LD/ST 14:3 */ +#define R_AARCH64_TLSGD_ADR_PREL21 512 /* PC-relative ADR imm. 20:0. */ +#define R_AARCH64_TLSGD_ADR_PAGE21 513 /* page-rel. ADRP imm. 32:12. */ +#define R_AARCH64_TLSGD_ADD_LO12_NC 514 /* direct ADD imm. from 11:0. */ +#define R_AARCH64_TLSGD_MOVW_G1 515 /* GOT-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSGD_MOVW_G0_NC 516 /* GOT-rel. MOVK imm. 15:0. */ +#define R_AARCH64_TLSLD_ADR_PREL21 517 /* Like 512; local dynamic model. */ +#define R_AARCH64_TLSLD_ADR_PAGE21 518 /* Like 513; local dynamic model. */ +#define R_AARCH64_TLSLD_ADD_LO12_NC 519 /* Like 514; local dynamic model. */ +#define R_AARCH64_TLSLD_MOVW_G1 520 /* Like 515; local dynamic model. */ +#define R_AARCH64_TLSLD_MOVW_G0_NC 521 /* Like 516; local dynamic model. */ +#define R_AARCH64_TLSLD_LD_PREL19 522 /* TLS PC-rel. load imm. 20:2. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G2 523 /* TLS DTP-rel. MOV{N,Z} 47:32. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G1 524 /* TLS DTP-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC 525 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G0 526 /* TLS DTP-rel. MOV{N,Z} 15:0. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC 527 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLD_ADD_DTPREL_HI12 528 /* DTP-rel. ADD imm. from 23:12. */ +#define R_AARCH64_TLSLD_ADD_DTPREL_LO12 529 /* DTP-rel. ADD imm. from 11:0. */ +#define R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC 530 /* Likewise; no ovfl. check. */ +#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12 531 /* DTP-rel. LD/ST imm. 11:0. */ +#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC 532 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12 533 /* DTP-rel. LD/ST imm. 11:1. */ +#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC 534 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12 535 /* DTP-rel. LD/ST imm. 11:2. */ +#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC 536 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12 537 /* DTP-rel. LD/ST imm. 11:3. */ +#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC 538 /* Likewise; no check. */ +#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G1 539 /* GOT-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC 540 /* GOT-rel. MOVK 15:0. */ +#define R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 541 /* Page-rel. ADRP 32:12. */ +#define R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC 542 /* Direct LD off. 11:3. */ +#define R_AARCH64_TLSIE_LD_GOTTPREL_PREL19 543 /* PC-rel. load imm. 20:2. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G2 544 /* TLS TP-rel. MOV{N,Z} 47:32. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G1 545 /* TLS TP-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G1_NC 546 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G0 547 /* TLS TP-rel. MOV{N,Z} 15:0. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G0_NC 548 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLE_ADD_TPREL_HI12 549 /* TP-rel. ADD imm. 23:12. */ +#define R_AARCH64_TLSLE_ADD_TPREL_LO12 550 /* TP-rel. ADD imm. 11:0. */ +#define R_AARCH64_TLSLE_ADD_TPREL_LO12_NC 551 /* Likewise; no ovfl. check. */ +#define R_AARCH64_TLSLE_LDST8_TPREL_LO12 552 /* TP-rel. LD/ST off. 11:0. */ +#define R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC 553 /* Likewise; no ovfl. check. */ +#define R_AARCH64_TLSLE_LDST16_TPREL_LO12 554 /* TP-rel. LD/ST off. 11:1. */ +#define R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC 555 /* Likewise; no check. */ +#define R_AARCH64_TLSLE_LDST32_TPREL_LO12 556 /* TP-rel. LD/ST off. 11:2. */ +#define R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC 557 /* Likewise; no check. */ +#define R_AARCH64_TLSLE_LDST64_TPREL_LO12 558 /* TP-rel. LD/ST off. 11:3. */ +#define R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC 559 /* Likewise; no check. */ +#define R_AARCH64_TLSDESC_LD_PREL19 560 /* PC-rel. load immediate 20:2. */ +#define R_AARCH64_TLSDESC_ADR_PREL21 561 /* PC-rel. ADR immediate 20:0. */ +#define R_AARCH64_TLSDESC_ADR_PAGE21 562 /* Page-rel. ADRP imm. 32:12. */ +#define R_AARCH64_TLSDESC_LD64_LO12 563 /* Direct LD off. from 11:3. */ +#define R_AARCH64_TLSDESC_ADD_LO12 564 /* Direct ADD imm. from 11:0. */ +#define R_AARCH64_TLSDESC_OFF_G1 565 /* GOT-rel. MOV{N,Z} imm. 31:16. */ +#define R_AARCH64_TLSDESC_OFF_G0_NC 566 /* GOT-rel. MOVK imm. 15:0; no ck. */ +#define R_AARCH64_TLSDESC_LDR 567 /* Relax LDR. */ +#define R_AARCH64_TLSDESC_ADD 568 /* Relax ADD. */ +#define R_AARCH64_TLSDESC_CALL 569 /* Relax BLR. */ +#define R_AARCH64_TLSLE_LDST128_TPREL_LO12 570 /* TP-rel. LD/ST off. 11:4. */ +#define R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC 571 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST128_DTPREL_LO12 572 /* DTP-rel. LD/ST imm. 11:4. */ +#define R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC 573 /* Likewise; no check. */ +#define R_AARCH64_COPY 1024 /* Copy symbol at runtime. */ +#define R_AARCH64_GLOB_DAT 1025 /* Create GOT entry. */ +#define R_AARCH64_JUMP_SLOT 1026 /* Create PLT entry. */ +#define R_AARCH64_RELATIVE 1027 /* Adjust by program base. */ +#define R_AARCH64_TLS_DTPMOD64 1028 /* Module number, 64 bit. */ +#define R_AARCH64_TLS_DTPREL64 1029 /* Module-relative offset, 64 bit. */ +#define R_AARCH64_TLS_TPREL64 1030 /* TP-relative offset, 64 bit. */ +#define R_AARCH64_TLSDESC 1031 /* TLS Descriptor. */ +#define R_AARCH64_IRELATIVE 1032 /* STT_GNU_IFUNC relocation. */ +/* Keep this the last entry. */ +#define R_AARCH64_NUM 1033 + +/* ARM relocs. */ + +#define R_ARM_NONE 0 /* No reloc */ +#define R_ARM_PC24 1 /* PC relative 26 bit branch */ +#define R_ARM_ABS32 2 /* Direct 32 bit */ +#define R_ARM_REL32 3 /* PC relative 32 bit */ +#define R_ARM_PC13 4 +#define R_ARM_ABS16 5 /* Direct 16 bit */ +#define R_ARM_ABS12 6 /* Direct 12 bit */ +#define R_ARM_THM_ABS5 7 +#define R_ARM_ABS8 8 /* Direct 8 bit */ +#define R_ARM_SBREL32 9 +#define R_ARM_THM_PC22 10 +#define R_ARM_THM_PC8 11 +#define R_ARM_AMP_VCALL9 12 +#define R_ARM_SWI24 13 /* Obsolete static relocation. */ +#define R_ARM_TLS_DESC 13 /* Dynamic relocation. */ +#define R_ARM_THM_SWI8 14 +#define R_ARM_XPC25 15 +#define R_ARM_THM_XPC22 16 +#define R_ARM_TLS_DTPMOD32 17 /* ID of module containing symbol */ +#define R_ARM_TLS_DTPOFF32 18 /* Offset in TLS block */ +#define R_ARM_TLS_TPOFF32 19 /* Offset in static TLS block */ +#define R_ARM_COPY 20 /* Copy symbol at runtime */ +#define R_ARM_GLOB_DAT 21 /* Create GOT entry */ +#define R_ARM_JUMP_SLOT 22 /* Create PLT entry */ +#define R_ARM_RELATIVE 23 /* Adjust by program base */ +#define R_ARM_GOTOFF 24 /* 32 bit offset to GOT */ +#define R_ARM_GOTPC 25 /* 32 bit PC relative offset to GOT */ +#define R_ARM_GOT32 26 /* 32 bit GOT entry */ +#define R_ARM_PLT32 27 /* 32 bit PLT address */ +#define R_ARM_CALL 28 +#define R_ARM_JUMP24 29 +#define R_ARM_THM_JUMP24 30 +#define R_ARM_ALU_PCREL_7_0 32 +#define R_ARM_ALU_PCREL_15_8 33 +#define R_ARM_ALU_PCREL_23_15 34 +#define R_ARM_LDR_SBREL_11_0 35 +#define R_ARM_ALU_SBREL_19_12 36 +#define R_ARM_ALU_SBREL_27_20 37 +#define R_ARM_V4BX 40 +#define R_ARM_PREL31 42 +#define R_ARM_MOVW_ABS_NC 43 +#define R_ARM_MOVT_ABS 44 +#define R_ARM_THM_MOVW_ABS_NC 47 +#define R_ARM_THM_MOVT_ABS 48 +#define R_ARM_TLS_GOTDESC 90 +#define R_ARM_TLS_CALL 91 +#define R_ARM_TLS_DESCSEQ 92 +#define R_ARM_THM_TLS_CALL 93 +#define R_ARM_GNU_VTENTRY 100 +#define R_ARM_GNU_VTINHERIT 101 +#define R_ARM_THM_PC11 102 /* thumb unconditional branch */ +#define R_ARM_THM_PC9 103 /* thumb conditional branch */ +#define R_ARM_TLS_GD32 104 /* PC-rel 32 bit for global dynamic + thread local data */ +#define R_ARM_TLS_LDM32 105 /* PC-rel 32 bit for local dynamic + thread local data */ +#define R_ARM_TLS_LDO32 106 /* 32 bit offset relative to TLS + block */ +#define R_ARM_TLS_IE32 107 /* PC-rel 32 bit for GOT entry of + static TLS block offset */ +#define R_ARM_TLS_LE32 108 /* 32 bit offset relative to static + TLS block */ +#define R_ARM_THM_TLS_DESCSEQ 129 +#define R_ARM_IRELATIVE 160 +#define R_ARM_RXPC25 249 +#define R_ARM_RSBREL32 250 +#define R_ARM_THM_RPC22 251 +#define R_ARM_RREL32 252 +#define R_ARM_RABS22 253 +#define R_ARM_RPC24 254 +#define R_ARM_RBASE 255 +/* Keep this the last entry. */ +#define R_ARM_NUM 256 + +/* TMS320C67xx specific declarations */ + +/* XXX: no ELF standard yet*/ + +/* TMS320C67xx relocs. */ +#define R_C60_32 1 +#define R_C60_GOT32 3 /* 32 bit GOT entry */ +#define R_C60_PLT32 4 /* 32 bit PLT address */ +#define R_C60_COPY 5 /* Copy symbol at runtime */ +#define R_C60_GLOB_DAT 6 /* Create GOT entry */ +#define R_C60_JMP_SLOT 7 /* Create PLT entry */ +#define R_C60_RELATIVE 8 /* Adjust by program base */ +#define R_C60_GOTOFF 9 /* 32 bit offset to GOT */ +#define R_C60_GOTPC 10 /* 32 bit PC relative offset to GOT */ + +#define R_C60LO16 0x54 /* low 16 bit MVKL embedded */ +#define R_C60HI16 0x55 /* high 16 bit MVKH embedded */ +/* Keep this the last entry. */ +#define R_C60_NUM 0x56 + +/* IA-64 specific declarations. */ + +/* Processor specific flags for the Ehdr e_flags field. */ +#define EF_IA_64_MASKOS 0x0000000f /* os-specific flags */ +#define EF_IA_64_ABI64 0x00000010 /* 64-bit ABI */ +#define EF_IA_64_ARCH 0xff000000 /* arch. version mask */ + +/* Processor specific values for the Phdr p_type field. */ +#define PT_IA_64_ARCHEXT (PT_LOPROC + 0) /* arch extension bits */ +#define PT_IA_64_UNWIND (PT_LOPROC + 1) /* ia64 unwind bits */ +#define PT_IA_64_HP_OPT_ANOT (PT_LOOS + 0x12) +#define PT_IA_64_HP_HSL_ANOT (PT_LOOS + 0x13) +#define PT_IA_64_HP_STACK (PT_LOOS + 0x14) + +/* Processor specific flags for the Phdr p_flags field. */ +#define PF_IA_64_NORECOV 0x80000000 /* spec insns w/o recovery */ + +/* Processor specific values for the Shdr sh_type field. */ +#define SHT_IA_64_EXT (SHT_LOPROC + 0) /* extension bits */ +#define SHT_IA_64_UNWIND (SHT_LOPROC + 1) /* unwind bits */ + +/* Processor specific flags for the Shdr sh_flags field. */ +#define SHF_IA_64_SHORT 0x10000000 /* section near gp */ +#define SHF_IA_64_NORECOV 0x20000000 /* spec insns w/o recovery */ + +/* Processor specific values for the Dyn d_tag field. */ +#define DT_IA_64_PLT_RESERVE (DT_LOPROC + 0) +#define DT_IA_64_NUM 1 + +/* IA-64 relocations. */ +#define R_IA64_NONE 0x00 /* none */ +#define R_IA64_IMM14 0x21 /* symbol + addend, add imm14 */ +#define R_IA64_IMM22 0x22 /* symbol + addend, add imm22 */ +#define R_IA64_IMM64 0x23 /* symbol + addend, mov imm64 */ +#define R_IA64_DIR32MSB 0x24 /* symbol + addend, data4 MSB */ +#define R_IA64_DIR32LSB 0x25 /* symbol + addend, data4 LSB */ +#define R_IA64_DIR64MSB 0x26 /* symbol + addend, data8 MSB */ +#define R_IA64_DIR64LSB 0x27 /* symbol + addend, data8 LSB */ +#define R_IA64_GPREL22 0x2a /* @gprel(sym + add), add imm22 */ +#define R_IA64_GPREL64I 0x2b /* @gprel(sym + add), mov imm64 */ +#define R_IA64_GPREL32MSB 0x2c /* @gprel(sym + add), data4 MSB */ +#define R_IA64_GPREL32LSB 0x2d /* @gprel(sym + add), data4 LSB */ +#define R_IA64_GPREL64MSB 0x2e /* @gprel(sym + add), data8 MSB */ +#define R_IA64_GPREL64LSB 0x2f /* @gprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF22 0x32 /* @ltoff(sym + add), add imm22 */ +#define R_IA64_LTOFF64I 0x33 /* @ltoff(sym + add), mov imm64 */ +#define R_IA64_PLTOFF22 0x3a /* @pltoff(sym + add), add imm22 */ +#define R_IA64_PLTOFF64I 0x3b /* @pltoff(sym + add), mov imm64 */ +#define R_IA64_PLTOFF64MSB 0x3e /* @pltoff(sym + add), data8 MSB */ +#define R_IA64_PLTOFF64LSB 0x3f /* @pltoff(sym + add), data8 LSB */ +#define R_IA64_FPTR64I 0x43 /* @fptr(sym + add), mov imm64 */ +#define R_IA64_FPTR32MSB 0x44 /* @fptr(sym + add), data4 MSB */ +#define R_IA64_FPTR32LSB 0x45 /* @fptr(sym + add), data4 LSB */ +#define R_IA64_FPTR64MSB 0x46 /* @fptr(sym + add), data8 MSB */ +#define R_IA64_FPTR64LSB 0x47 /* @fptr(sym + add), data8 LSB */ +#define R_IA64_PCREL60B 0x48 /* @pcrel(sym + add), brl */ +#define R_IA64_PCREL21B 0x49 /* @pcrel(sym + add), ptb, call */ +#define R_IA64_PCREL21M 0x4a /* @pcrel(sym + add), chk.s */ +#define R_IA64_PCREL21F 0x4b /* @pcrel(sym + add), fchkf */ +#define R_IA64_PCREL32MSB 0x4c /* @pcrel(sym + add), data4 MSB */ +#define R_IA64_PCREL32LSB 0x4d /* @pcrel(sym + add), data4 LSB */ +#define R_IA64_PCREL64MSB 0x4e /* @pcrel(sym + add), data8 MSB */ +#define R_IA64_PCREL64LSB 0x4f /* @pcrel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_FPTR22 0x52 /* @ltoff(@fptr(s+a)), imm22 */ +#define R_IA64_LTOFF_FPTR64I 0x53 /* @ltoff(@fptr(s+a)), imm64 */ +#define R_IA64_LTOFF_FPTR32MSB 0x54 /* @ltoff(@fptr(s+a)), data4 MSB */ +#define R_IA64_LTOFF_FPTR32LSB 0x55 /* @ltoff(@fptr(s+a)), data4 LSB */ +#define R_IA64_LTOFF_FPTR64MSB 0x56 /* @ltoff(@fptr(s+a)), data8 MSB */ +#define R_IA64_LTOFF_FPTR64LSB 0x57 /* @ltoff(@fptr(s+a)), data8 LSB */ +#define R_IA64_SEGREL32MSB 0x5c /* @segrel(sym + add), data4 MSB */ +#define R_IA64_SEGREL32LSB 0x5d /* @segrel(sym + add), data4 LSB */ +#define R_IA64_SEGREL64MSB 0x5e /* @segrel(sym + add), data8 MSB */ +#define R_IA64_SEGREL64LSB 0x5f /* @segrel(sym + add), data8 LSB */ +#define R_IA64_SECREL32MSB 0x64 /* @secrel(sym + add), data4 MSB */ +#define R_IA64_SECREL32LSB 0x65 /* @secrel(sym + add), data4 LSB */ +#define R_IA64_SECREL64MSB 0x66 /* @secrel(sym + add), data8 MSB */ +#define R_IA64_SECREL64LSB 0x67 /* @secrel(sym + add), data8 LSB */ +#define R_IA64_REL32MSB 0x6c /* data 4 + REL */ +#define R_IA64_REL32LSB 0x6d /* data 4 + REL */ +#define R_IA64_REL64MSB 0x6e /* data 8 + REL */ +#define R_IA64_REL64LSB 0x6f /* data 8 + REL */ +#define R_IA64_LTV32MSB 0x74 /* symbol + addend, data4 MSB */ +#define R_IA64_LTV32LSB 0x75 /* symbol + addend, data4 LSB */ +#define R_IA64_LTV64MSB 0x76 /* symbol + addend, data8 MSB */ +#define R_IA64_LTV64LSB 0x77 /* symbol + addend, data8 LSB */ +#define R_IA64_PCREL21BI 0x79 /* @pcrel(sym + add), 21bit inst */ +#define R_IA64_PCREL22 0x7a /* @pcrel(sym + add), 22bit inst */ +#define R_IA64_PCREL64I 0x7b /* @pcrel(sym + add), 64bit inst */ +#define R_IA64_IPLTMSB 0x80 /* dynamic reloc, imported PLT, MSB */ +#define R_IA64_IPLTLSB 0x81 /* dynamic reloc, imported PLT, LSB */ +#define R_IA64_COPY 0x84 /* copy relocation */ +#define R_IA64_SUB 0x85 /* Addend and symbol difference */ +#define R_IA64_LTOFF22X 0x86 /* LTOFF22, relaxable. */ +#define R_IA64_LDXMOV 0x87 /* Use of LTOFF22X. */ +#define R_IA64_TPREL14 0x91 /* @tprel(sym + add), imm14 */ +#define R_IA64_TPREL22 0x92 /* @tprel(sym + add), imm22 */ +#define R_IA64_TPREL64I 0x93 /* @tprel(sym + add), imm64 */ +#define R_IA64_TPREL64MSB 0x96 /* @tprel(sym + add), data8 MSB */ +#define R_IA64_TPREL64LSB 0x97 /* @tprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_TPREL22 0x9a /* @ltoff(@tprel(s+a)), imm2 */ +#define R_IA64_DTPMOD64MSB 0xa6 /* @dtpmod(sym + add), data8 MSB */ +#define R_IA64_DTPMOD64LSB 0xa7 /* @dtpmod(sym + add), data8 LSB */ +#define R_IA64_LTOFF_DTPMOD22 0xaa /* @ltoff(@dtpmod(sym + add)), imm22 */ +#define R_IA64_DTPREL14 0xb1 /* @dtprel(sym + add), imm14 */ +#define R_IA64_DTPREL22 0xb2 /* @dtprel(sym + add), imm22 */ +#define R_IA64_DTPREL64I 0xb3 /* @dtprel(sym + add), imm64 */ +#define R_IA64_DTPREL32MSB 0xb4 /* @dtprel(sym + add), data4 MSB */ +#define R_IA64_DTPREL32LSB 0xb5 /* @dtprel(sym + add), data4 LSB */ +#define R_IA64_DTPREL64MSB 0xb6 /* @dtprel(sym + add), data8 MSB */ +#define R_IA64_DTPREL64LSB 0xb7 /* @dtprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_DTPREL22 0xba /* @ltoff(@dtprel(s+a)), imm22 */ + +/* SH specific declarations */ + +/* Processor specific flags for the ELF header e_flags field. */ +#define EF_SH_MACH_MASK 0x1f +#define EF_SH_UNKNOWN 0x0 +#define EF_SH1 0x1 +#define EF_SH2 0x2 +#define EF_SH3 0x3 +#define EF_SH_DSP 0x4 +#define EF_SH3_DSP 0x5 +#define EF_SH4AL_DSP 0x6 +#define EF_SH3E 0x8 +#define EF_SH4 0x9 +#define EF_SH2E 0xb +#define EF_SH4A 0xc +#define EF_SH2A 0xd +#define EF_SH4_NOFPU 0x10 +#define EF_SH4A_NOFPU 0x11 +#define EF_SH4_NOMMU_NOFPU 0x12 +#define EF_SH2A_NOFPU 0x13 +#define EF_SH3_NOMMU 0x14 +#define EF_SH2A_SH4_NOFPU 0x15 +#define EF_SH2A_SH3_NOFPU 0x16 +#define EF_SH2A_SH4 0x17 +#define EF_SH2A_SH3E 0x18 + +/* SH relocs. */ +#define R_SH_NONE 0 +#define R_SH_DIR32 1 +#define R_SH_REL32 2 +#define R_SH_DIR8WPN 3 +#define R_SH_IND12W 4 +#define R_SH_DIR8WPL 5 +#define R_SH_DIR8WPZ 6 +#define R_SH_DIR8BP 7 +#define R_SH_DIR8W 8 +#define R_SH_DIR8L 9 +#define R_SH_SWITCH16 25 +#define R_SH_SWITCH32 26 +#define R_SH_USES 27 +#define R_SH_COUNT 28 +#define R_SH_ALIGN 29 +#define R_SH_CODE 30 +#define R_SH_DATA 31 +#define R_SH_LABEL 32 +#define R_SH_SWITCH8 33 +#define R_SH_GNU_VTINHERIT 34 +#define R_SH_GNU_VTENTRY 35 +#define R_SH_TLS_GD_32 144 +#define R_SH_TLS_LD_32 145 +#define R_SH_TLS_LDO_32 146 +#define R_SH_TLS_IE_32 147 +#define R_SH_TLS_LE_32 148 +#define R_SH_TLS_DTPMOD32 149 +#define R_SH_TLS_DTPOFF32 150 +#define R_SH_TLS_TPOFF32 151 +#define R_SH_GOT32 160 +#define R_SH_PLT32 161 +#define R_SH_COPY 162 +#define R_SH_GLOB_DAT 163 +#define R_SH_JMP_SLOT 164 +#define R_SH_RELATIVE 165 +#define R_SH_GOTOFF 166 +#define R_SH_GOTPC 167 +/* Keep this the last entry. */ +#define R_SH_NUM 256 + +/* S/390 specific definitions. */ + +/* Valid values for the e_flags field. */ + +#define EF_S390_HIGH_GPRS 0x00000001 /* High GPRs kernel facility needed. */ + +/* Additional s390 relocs */ + +#define R_390_NONE 0 /* No reloc. */ +#define R_390_8 1 /* Direct 8 bit. */ +#define R_390_12 2 /* Direct 12 bit. */ +#define R_390_16 3 /* Direct 16 bit. */ +#define R_390_32 4 /* Direct 32 bit. */ +#define R_390_PC32 5 /* PC relative 32 bit. */ +#define R_390_GOT12 6 /* 12 bit GOT offset. */ +#define R_390_GOT32 7 /* 32 bit GOT offset. */ +#define R_390_PLT32 8 /* 32 bit PC relative PLT address. */ +#define R_390_COPY 9 /* Copy symbol at runtime. */ +#define R_390_GLOB_DAT 10 /* Create GOT entry. */ +#define R_390_JMP_SLOT 11 /* Create PLT entry. */ +#define R_390_RELATIVE 12 /* Adjust by program base. */ +#define R_390_GOTOFF32 13 /* 32 bit offset to GOT. */ +#define R_390_GOTPC 14 /* 32 bit PC relative offset to GOT. */ +#define R_390_GOT16 15 /* 16 bit GOT offset. */ +#define R_390_PC16 16 /* PC relative 16 bit. */ +#define R_390_PC16DBL 17 /* PC relative 16 bit shifted by 1. */ +#define R_390_PLT16DBL 18 /* 16 bit PC rel. PLT shifted by 1. */ +#define R_390_PC32DBL 19 /* PC relative 32 bit shifted by 1. */ +#define R_390_PLT32DBL 20 /* 32 bit PC rel. PLT shifted by 1. */ +#define R_390_GOTPCDBL 21 /* 32 bit PC rel. GOT shifted by 1. */ +#define R_390_64 22 /* Direct 64 bit. */ +#define R_390_PC64 23 /* PC relative 64 bit. */ +#define R_390_GOT64 24 /* 64 bit GOT offset. */ +#define R_390_PLT64 25 /* 64 bit PC relative PLT address. */ +#define R_390_GOTENT 26 /* 32 bit PC rel. to GOT entry >> 1. */ +#define R_390_GOTOFF16 27 /* 16 bit offset to GOT. */ +#define R_390_GOTOFF64 28 /* 64 bit offset to GOT. */ +#define R_390_GOTPLT12 29 /* 12 bit offset to jump slot. */ +#define R_390_GOTPLT16 30 /* 16 bit offset to jump slot. */ +#define R_390_GOTPLT32 31 /* 32 bit offset to jump slot. */ +#define R_390_GOTPLT64 32 /* 64 bit offset to jump slot. */ +#define R_390_GOTPLTENT 33 /* 32 bit rel. offset to jump slot. */ +#define R_390_PLTOFF16 34 /* 16 bit offset from GOT to PLT. */ +#define R_390_PLTOFF32 35 /* 32 bit offset from GOT to PLT. */ +#define R_390_PLTOFF64 36 /* 16 bit offset from GOT to PLT. */ +#define R_390_TLS_LOAD 37 /* Tag for load insn in TLS code. */ +#define R_390_TLS_GDCALL 38 /* Tag for function call in general + dynamic TLS code. */ +#define R_390_TLS_LDCALL 39 /* Tag for function call in local + dynamic TLS code. */ +#define R_390_TLS_GD32 40 /* Direct 32 bit for general dynamic + thread local data. */ +#define R_390_TLS_GD64 41 /* Direct 64 bit for general dynamic + thread local data. */ +#define R_390_TLS_GOTIE12 42 /* 12 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_GOTIE32 43 /* 32 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_GOTIE64 44 /* 64 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_LDM32 45 /* Direct 32 bit for local dynamic + thread local data in LE code. */ +#define R_390_TLS_LDM64 46 /* Direct 64 bit for local dynamic + thread local data in LE code. */ +#define R_390_TLS_IE32 47 /* 32 bit address of GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_IE64 48 /* 64 bit address of GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_IEENT 49 /* 32 bit rel. offset to GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_LE32 50 /* 32 bit negated offset relative to + static TLS block. */ +#define R_390_TLS_LE64 51 /* 64 bit negated offset relative to + static TLS block. */ +#define R_390_TLS_LDO32 52 /* 32 bit offset relative to TLS + block. */ +#define R_390_TLS_LDO64 53 /* 64 bit offset relative to TLS + block. */ +#define R_390_TLS_DTPMOD 54 /* ID of module containing symbol. */ +#define R_390_TLS_DTPOFF 55 /* Offset in TLS block. */ +#define R_390_TLS_TPOFF 56 /* Negated offset in static TLS + block. */ +#define R_390_20 57 /* Direct 20 bit. */ +#define R_390_GOT20 58 /* 20 bit GOT offset. */ +#define R_390_GOTPLT20 59 /* 20 bit offset to jump slot. */ +#define R_390_TLS_GOTIE20 60 /* 20 bit GOT offset for static TLS + block offset. */ +#define R_390_IRELATIVE 61 /* STT_GNU_IFUNC relocation. */ +/* Keep this the last entry. */ +#define R_390_NUM 62 + + +/* CRIS relocations. */ +#define R_CRIS_NONE 0 +#define R_CRIS_8 1 +#define R_CRIS_16 2 +#define R_CRIS_32 3 +#define R_CRIS_8_PCREL 4 +#define R_CRIS_16_PCREL 5 +#define R_CRIS_32_PCREL 6 +#define R_CRIS_GNU_VTINHERIT 7 +#define R_CRIS_GNU_VTENTRY 8 +#define R_CRIS_COPY 9 +#define R_CRIS_GLOB_DAT 10 +#define R_CRIS_JUMP_SLOT 11 +#define R_CRIS_RELATIVE 12 +#define R_CRIS_16_GOT 13 +#define R_CRIS_32_GOT 14 +#define R_CRIS_16_GOTPLT 15 +#define R_CRIS_32_GOTPLT 16 +#define R_CRIS_32_GOTREL 17 +#define R_CRIS_32_PLT_GOTREL 18 +#define R_CRIS_32_PLT_PCREL 19 + +#define R_CRIS_NUM 20 + + +/* AMD x86-64 relocations. */ +#define R_X86_64_NONE 0 /* No reloc */ +#define R_X86_64_64 1 /* Direct 64 bit */ +#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ +#define R_X86_64_GOT32 3 /* 32 bit GOT entry */ +#define R_X86_64_PLT32 4 /* 32 bit PLT address */ +#define R_X86_64_COPY 5 /* Copy symbol at runtime */ +#define R_X86_64_GLOB_DAT 6 /* Create GOT entry */ +#define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */ +#define R_X86_64_RELATIVE 8 /* Adjust by program base */ +#define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative + offset to GOT */ +#define R_X86_64_32 10 /* Direct 32 bit zero extended */ +#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ +#define R_X86_64_16 12 /* Direct 16 bit zero extended */ +#define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */ +#define R_X86_64_8 14 /* Direct 8 bit sign extended */ +#define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */ +#define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */ +#define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */ +#define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */ +#define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset + to two GOT entries for GD symbol */ +#define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset + to two GOT entries for LD symbol */ +#define R_X86_64_DTPOFF32 21 /* Offset in TLS block */ +#define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset + to GOT entry for IE symbol */ +#define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */ +#define R_X86_64_PC64 24 /* PC relative 64 bit */ +#define R_X86_64_GOTOFF64 25 /* 64 bit offset to GOT */ +#define R_X86_64_GOTPC32 26 /* 32 bit signed pc relative + offset to GOT */ +#define R_X86_64_GOT64 27 /* 64-bit GOT entry offset */ +#define R_X86_64_GOTPCREL64 28 /* 64-bit PC relative offset + to GOT entry */ +#define R_X86_64_GOTPC64 29 /* 64-bit PC relative offset to GOT */ +#define R_X86_64_GOTPLT64 30 /* like GOT64, says PLT entry needed */ +#define R_X86_64_PLTOFF64 31 /* 64-bit GOT relative offset + to PLT entry */ +#define R_X86_64_SIZE32 32 /* Size of symbol plus 32-bit addend */ +#define R_X86_64_SIZE64 33 /* Size of symbol plus 64-bit addend */ +#define R_X86_64_GOTPC32_TLSDESC 34 /* GOT offset for TLS descriptor. */ +#define R_X86_64_TLSDESC_CALL 35 /* Marker for call through TLS + descriptor. */ +#define R_X86_64_TLSDESC 36 /* TLS descriptor. */ +#define R_X86_64_IRELATIVE 37 /* Adjust indirectly by program base */ +#define R_X86_64_RELATIVE64 38 /* 64-bit adjust by program base */ +#define R_X86_64_GOTPCRELX 41 /* like GOTPCREL, but optionally with + linker optimizations */ +#define R_X86_64_REX_GOTPCRELX 42 /* like GOTPCRELX, but a REX prefix + is present */ + +#define R_X86_64_NUM 43 + + +/* AM33 relocations. */ +#define R_MN10300_NONE 0 /* No reloc. */ +#define R_MN10300_32 1 /* Direct 32 bit. */ +#define R_MN10300_16 2 /* Direct 16 bit. */ +#define R_MN10300_8 3 /* Direct 8 bit. */ +#define R_MN10300_PCREL32 4 /* PC-relative 32-bit. */ +#define R_MN10300_PCREL16 5 /* PC-relative 16-bit signed. */ +#define R_MN10300_PCREL8 6 /* PC-relative 8-bit signed. */ +#define R_MN10300_GNU_VTINHERIT 7 /* Ancient C++ vtable garbage... */ +#define R_MN10300_GNU_VTENTRY 8 /* ... collection annotation. */ +#define R_MN10300_24 9 /* Direct 24 bit. */ +#define R_MN10300_GOTPC32 10 /* 32-bit PCrel offset to GOT. */ +#define R_MN10300_GOTPC16 11 /* 16-bit PCrel offset to GOT. */ +#define R_MN10300_GOTOFF32 12 /* 32-bit offset from GOT. */ +#define R_MN10300_GOTOFF24 13 /* 24-bit offset from GOT. */ +#define R_MN10300_GOTOFF16 14 /* 16-bit offset from GOT. */ +#define R_MN10300_PLT32 15 /* 32-bit PCrel to PLT entry. */ +#define R_MN10300_PLT16 16 /* 16-bit PCrel to PLT entry. */ +#define R_MN10300_GOT32 17 /* 32-bit offset to GOT entry. */ +#define R_MN10300_GOT24 18 /* 24-bit offset to GOT entry. */ +#define R_MN10300_GOT16 19 /* 16-bit offset to GOT entry. */ +#define R_MN10300_COPY 20 /* Copy symbol at runtime. */ +#define R_MN10300_GLOB_DAT 21 /* Create GOT entry. */ +#define R_MN10300_JMP_SLOT 22 /* Create PLT entry. */ +#define R_MN10300_RELATIVE 23 /* Adjust by program base. */ +#define R_MN10300_TLS_GD 24 /* 32-bit offset for global dynamic. */ +#define R_MN10300_TLS_LD 25 /* 32-bit offset for local dynamic. */ +#define R_MN10300_TLS_LDO 26 /* Module-relative offset. */ +#define R_MN10300_TLS_GOTIE 27 /* GOT offset for static TLS block + offset. */ +#define R_MN10300_TLS_IE 28 /* GOT address for static TLS block + offset. */ +#define R_MN10300_TLS_LE 29 /* Offset relative to static TLS + block. */ +#define R_MN10300_TLS_DTPMOD 30 /* ID of module containing symbol. */ +#define R_MN10300_TLS_DTPOFF 31 /* Offset in module TLS block. */ +#define R_MN10300_TLS_TPOFF 32 /* Offset in static TLS block. */ +#define R_MN10300_SYM_DIFF 33 /* Adjustment for next reloc as needed + by linker relaxation. */ +#define R_MN10300_ALIGN 34 /* Alignment requirement for linker + relaxation. */ +#define R_MN10300_NUM 35 + + +/* M32R relocs. */ +#define R_M32R_NONE 0 /* No reloc. */ +#define R_M32R_16 1 /* Direct 16 bit. */ +#define R_M32R_32 2 /* Direct 32 bit. */ +#define R_M32R_24 3 /* Direct 24 bit. */ +#define R_M32R_10_PCREL 4 /* PC relative 10 bit shifted. */ +#define R_M32R_18_PCREL 5 /* PC relative 18 bit shifted. */ +#define R_M32R_26_PCREL 6 /* PC relative 26 bit shifted. */ +#define R_M32R_HI16_ULO 7 /* High 16 bit with unsigned low. */ +#define R_M32R_HI16_SLO 8 /* High 16 bit with signed low. */ +#define R_M32R_LO16 9 /* Low 16 bit. */ +#define R_M32R_SDA16 10 /* 16 bit offset in SDA. */ +#define R_M32R_GNU_VTINHERIT 11 +#define R_M32R_GNU_VTENTRY 12 +/* M32R relocs use SHT_RELA. */ +#define R_M32R_16_RELA 33 /* Direct 16 bit. */ +#define R_M32R_32_RELA 34 /* Direct 32 bit. */ +#define R_M32R_24_RELA 35 /* Direct 24 bit. */ +#define R_M32R_10_PCREL_RELA 36 /* PC relative 10 bit shifted. */ +#define R_M32R_18_PCREL_RELA 37 /* PC relative 18 bit shifted. */ +#define R_M32R_26_PCREL_RELA 38 /* PC relative 26 bit shifted. */ +#define R_M32R_HI16_ULO_RELA 39 /* High 16 bit with unsigned low */ +#define R_M32R_HI16_SLO_RELA 40 /* High 16 bit with signed low */ +#define R_M32R_LO16_RELA 41 /* Low 16 bit */ +#define R_M32R_SDA16_RELA 42 /* 16 bit offset in SDA */ +#define R_M32R_RELA_GNU_VTINHERIT 43 +#define R_M32R_RELA_GNU_VTENTRY 44 +#define R_M32R_REL32 45 /* PC relative 32 bit. */ + +#define R_M32R_GOT24 48 /* 24 bit GOT entry */ +#define R_M32R_26_PLTREL 49 /* 26 bit PC relative to PLT shifted */ +#define R_M32R_COPY 50 /* Copy symbol at runtime */ +#define R_M32R_GLOB_DAT 51 /* Create GOT entry */ +#define R_M32R_JMP_SLOT 52 /* Create PLT entry */ +#define R_M32R_RELATIVE 53 /* Adjust by program base */ +#define R_M32R_GOTOFF 54 /* 24 bit offset to GOT */ +#define R_M32R_GOTPC24 55 /* 24 bit PC relative offset to GOT */ +#define R_M32R_GOT16_HI_ULO 56 /* High 16 bit GOT entry with unsigned + low */ +#define R_M32R_GOT16_HI_SLO 57 /* High 16 bit GOT entry with signed + low */ +#define R_M32R_GOT16_LO 58 /* Low 16 bit GOT entry */ +#define R_M32R_GOTPC_HI_ULO 59 /* High 16 bit PC relative offset to + GOT with unsigned low */ +#define R_M32R_GOTPC_HI_SLO 60 /* High 16 bit PC relative offset to + GOT with signed low */ +#define R_M32R_GOTPC_LO 61 /* Low 16 bit PC relative offset to + GOT */ +#define R_M32R_GOTOFF_HI_ULO 62 /* High 16 bit offset to GOT + with unsigned low */ +#define R_M32R_GOTOFF_HI_SLO 63 /* High 16 bit offset to GOT + with signed low */ +#define R_M32R_GOTOFF_LO 64 /* Low 16 bit offset to GOT */ +#define R_M32R_NUM 256 /* Keep this the last entry. */ + + +/* TILEPro relocations. */ +#define R_TILEPRO_NONE 0 /* No reloc */ +#define R_TILEPRO_32 1 /* Direct 32 bit */ +#define R_TILEPRO_16 2 /* Direct 16 bit */ +#define R_TILEPRO_8 3 /* Direct 8 bit */ +#define R_TILEPRO_32_PCREL 4 /* PC relative 32 bit */ +#define R_TILEPRO_16_PCREL 5 /* PC relative 16 bit */ +#define R_TILEPRO_8_PCREL 6 /* PC relative 8 bit */ +#define R_TILEPRO_LO16 7 /* Low 16 bit */ +#define R_TILEPRO_HI16 8 /* High 16 bit */ +#define R_TILEPRO_HA16 9 /* High 16 bit, adjusted */ +#define R_TILEPRO_COPY 10 /* Copy relocation */ +#define R_TILEPRO_GLOB_DAT 11 /* Create GOT entry */ +#define R_TILEPRO_JMP_SLOT 12 /* Create PLT entry */ +#define R_TILEPRO_RELATIVE 13 /* Adjust by program base */ +#define R_TILEPRO_BROFF_X1 14 /* X1 pipe branch offset */ +#define R_TILEPRO_JOFFLONG_X1 15 /* X1 pipe jump offset */ +#define R_TILEPRO_JOFFLONG_X1_PLT 16 /* X1 pipe jump offset to PLT */ +#define R_TILEPRO_IMM8_X0 17 /* X0 pipe 8-bit */ +#define R_TILEPRO_IMM8_Y0 18 /* Y0 pipe 8-bit */ +#define R_TILEPRO_IMM8_X1 19 /* X1 pipe 8-bit */ +#define R_TILEPRO_IMM8_Y1 20 /* Y1 pipe 8-bit */ +#define R_TILEPRO_MT_IMM15_X1 21 /* X1 pipe mtspr */ +#define R_TILEPRO_MF_IMM15_X1 22 /* X1 pipe mfspr */ +#define R_TILEPRO_IMM16_X0 23 /* X0 pipe 16-bit */ +#define R_TILEPRO_IMM16_X1 24 /* X1 pipe 16-bit */ +#define R_TILEPRO_IMM16_X0_LO 25 /* X0 pipe low 16-bit */ +#define R_TILEPRO_IMM16_X1_LO 26 /* X1 pipe low 16-bit */ +#define R_TILEPRO_IMM16_X0_HI 27 /* X0 pipe high 16-bit */ +#define R_TILEPRO_IMM16_X1_HI 28 /* X1 pipe high 16-bit */ +#define R_TILEPRO_IMM16_X0_HA 29 /* X0 pipe high 16-bit, adjusted */ +#define R_TILEPRO_IMM16_X1_HA 30 /* X1 pipe high 16-bit, adjusted */ +#define R_TILEPRO_IMM16_X0_PCREL 31 /* X0 pipe PC relative 16 bit */ +#define R_TILEPRO_IMM16_X1_PCREL 32 /* X1 pipe PC relative 16 bit */ +#define R_TILEPRO_IMM16_X0_LO_PCREL 33 /* X0 pipe PC relative low 16 bit */ +#define R_TILEPRO_IMM16_X1_LO_PCREL 34 /* X1 pipe PC relative low 16 bit */ +#define R_TILEPRO_IMM16_X0_HI_PCREL 35 /* X0 pipe PC relative high 16 bit */ +#define R_TILEPRO_IMM16_X1_HI_PCREL 36 /* X1 pipe PC relative high 16 bit */ +#define R_TILEPRO_IMM16_X0_HA_PCREL 37 /* X0 pipe PC relative ha() 16 bit */ +#define R_TILEPRO_IMM16_X1_HA_PCREL 38 /* X1 pipe PC relative ha() 16 bit */ +#define R_TILEPRO_IMM16_X0_GOT 39 /* X0 pipe 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT 40 /* X1 pipe 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_LO 41 /* X0 pipe low 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_LO 42 /* X1 pipe low 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_HI 43 /* X0 pipe high 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_HI 44 /* X1 pipe high 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_HA 45 /* X0 pipe ha() 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_HA 46 /* X1 pipe ha() 16-bit GOT offset */ +#define R_TILEPRO_MMSTART_X0 47 /* X0 pipe mm "start" */ +#define R_TILEPRO_MMEND_X0 48 /* X0 pipe mm "end" */ +#define R_TILEPRO_MMSTART_X1 49 /* X1 pipe mm "start" */ +#define R_TILEPRO_MMEND_X1 50 /* X1 pipe mm "end" */ +#define R_TILEPRO_SHAMT_X0 51 /* X0 pipe shift amount */ +#define R_TILEPRO_SHAMT_X1 52 /* X1 pipe shift amount */ +#define R_TILEPRO_SHAMT_Y0 53 /* Y0 pipe shift amount */ +#define R_TILEPRO_SHAMT_Y1 54 /* Y1 pipe shift amount */ +#define R_TILEPRO_DEST_IMM8_X1 55 /* X1 pipe destination 8-bit */ +/* Relocs 56-59 are currently not defined. */ +#define R_TILEPRO_TLS_GD_CALL 60 /* "jal" for TLS GD */ +#define R_TILEPRO_IMM8_X0_TLS_GD_ADD 61 /* X0 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_X1_TLS_GD_ADD 62 /* X1 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_Y0_TLS_GD_ADD 63 /* Y0 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_Y1_TLS_GD_ADD 64 /* Y1 pipe "addi" for TLS GD */ +#define R_TILEPRO_TLS_IE_LOAD 65 /* "lw_tls" for TLS IE */ +#define R_TILEPRO_IMM16_X0_TLS_GD 66 /* X0 pipe 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD 67 /* X1 pipe 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_LO 68 /* X0 pipe low 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_LO 69 /* X1 pipe low 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_HI 70 /* X0 pipe high 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_HI 71 /* X1 pipe high 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_HA 72 /* X0 pipe ha() 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_HA 73 /* X1 pipe ha() 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE 74 /* X0 pipe 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE 75 /* X1 pipe 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_LO 76 /* X0 pipe low 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_LO 77 /* X1 pipe low 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_HI 78 /* X0 pipe high 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_HI 79 /* X1 pipe high 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_HA 80 /* X0 pipe ha() 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_HA 81 /* X1 pipe ha() 16-bit TLS IE offset */ +#define R_TILEPRO_TLS_DTPMOD32 82 /* ID of module containing symbol */ +#define R_TILEPRO_TLS_DTPOFF32 83 /* Offset in TLS block */ +#define R_TILEPRO_TLS_TPOFF32 84 /* Offset in static TLS block */ +#define R_TILEPRO_IMM16_X0_TLS_LE 85 /* X0 pipe 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE 86 /* X1 pipe 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_LO 87 /* X0 pipe low 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_LO 88 /* X1 pipe low 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_HI 89 /* X0 pipe high 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_HI 90 /* X1 pipe high 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_HA 91 /* X0 pipe ha() 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_HA 92 /* X1 pipe ha() 16-bit TLS LE offset */ + +#define R_TILEPRO_GNU_VTINHERIT 128 /* GNU C++ vtable hierarchy */ +#define R_TILEPRO_GNU_VTENTRY 129 /* GNU C++ vtable member usage */ + +#define R_TILEPRO_NUM 130 + + +/* TILE-Gx relocations. */ +#define R_TILEGX_NONE 0 /* No reloc */ +#define R_TILEGX_64 1 /* Direct 64 bit */ +#define R_TILEGX_32 2 /* Direct 32 bit */ +#define R_TILEGX_16 3 /* Direct 16 bit */ +#define R_TILEGX_8 4 /* Direct 8 bit */ +#define R_TILEGX_64_PCREL 5 /* PC relative 64 bit */ +#define R_TILEGX_32_PCREL 6 /* PC relative 32 bit */ +#define R_TILEGX_16_PCREL 7 /* PC relative 16 bit */ +#define R_TILEGX_8_PCREL 8 /* PC relative 8 bit */ +#define R_TILEGX_HW0 9 /* hword 0 16-bit */ +#define R_TILEGX_HW1 10 /* hword 1 16-bit */ +#define R_TILEGX_HW2 11 /* hword 2 16-bit */ +#define R_TILEGX_HW3 12 /* hword 3 16-bit */ +#define R_TILEGX_HW0_LAST 13 /* last hword 0 16-bit */ +#define R_TILEGX_HW1_LAST 14 /* last hword 1 16-bit */ +#define R_TILEGX_HW2_LAST 15 /* last hword 2 16-bit */ +#define R_TILEGX_COPY 16 /* Copy relocation */ +#define R_TILEGX_GLOB_DAT 17 /* Create GOT entry */ +#define R_TILEGX_JMP_SLOT 18 /* Create PLT entry */ +#define R_TILEGX_RELATIVE 19 /* Adjust by program base */ +#define R_TILEGX_BROFF_X1 20 /* X1 pipe branch offset */ +#define R_TILEGX_JUMPOFF_X1 21 /* X1 pipe jump offset */ +#define R_TILEGX_JUMPOFF_X1_PLT 22 /* X1 pipe jump offset to PLT */ +#define R_TILEGX_IMM8_X0 23 /* X0 pipe 8-bit */ +#define R_TILEGX_IMM8_Y0 24 /* Y0 pipe 8-bit */ +#define R_TILEGX_IMM8_X1 25 /* X1 pipe 8-bit */ +#define R_TILEGX_IMM8_Y1 26 /* Y1 pipe 8-bit */ +#define R_TILEGX_DEST_IMM8_X1 27 /* X1 pipe destination 8-bit */ +#define R_TILEGX_MT_IMM14_X1 28 /* X1 pipe mtspr */ +#define R_TILEGX_MF_IMM14_X1 29 /* X1 pipe mfspr */ +#define R_TILEGX_MMSTART_X0 30 /* X0 pipe mm "start" */ +#define R_TILEGX_MMEND_X0 31 /* X0 pipe mm "end" */ +#define R_TILEGX_SHAMT_X0 32 /* X0 pipe shift amount */ +#define R_TILEGX_SHAMT_X1 33 /* X1 pipe shift amount */ +#define R_TILEGX_SHAMT_Y0 34 /* Y0 pipe shift amount */ +#define R_TILEGX_SHAMT_Y1 35 /* Y1 pipe shift amount */ +#define R_TILEGX_IMM16_X0_HW0 36 /* X0 pipe hword 0 */ +#define R_TILEGX_IMM16_X1_HW0 37 /* X1 pipe hword 0 */ +#define R_TILEGX_IMM16_X0_HW1 38 /* X0 pipe hword 1 */ +#define R_TILEGX_IMM16_X1_HW1 39 /* X1 pipe hword 1 */ +#define R_TILEGX_IMM16_X0_HW2 40 /* X0 pipe hword 2 */ +#define R_TILEGX_IMM16_X1_HW2 41 /* X1 pipe hword 2 */ +#define R_TILEGX_IMM16_X0_HW3 42 /* X0 pipe hword 3 */ +#define R_TILEGX_IMM16_X1_HW3 43 /* X1 pipe hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_LAST 44 /* X0 pipe last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST 45 /* X1 pipe last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST 46 /* X0 pipe last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST 47 /* X1 pipe last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST 48 /* X0 pipe last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST 49 /* X1 pipe last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_PCREL 50 /* X0 pipe PC relative hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_PCREL 51 /* X1 pipe PC relative hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_PCREL 52 /* X0 pipe PC relative hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_PCREL 53 /* X1 pipe PC relative hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_PCREL 54 /* X0 pipe PC relative hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_PCREL 55 /* X1 pipe PC relative hword 2 */ +#define R_TILEGX_IMM16_X0_HW3_PCREL 56 /* X0 pipe PC relative hword 3 */ +#define R_TILEGX_IMM16_X1_HW3_PCREL 57 /* X1 pipe PC relative hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_PCREL 58 /* X0 pipe PC-rel last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST_PCREL 59 /* X1 pipe PC-rel last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST_PCREL 60 /* X0 pipe PC-rel last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST_PCREL 61 /* X1 pipe PC-rel last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST_PCREL 62 /* X0 pipe PC-rel last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST_PCREL 63 /* X1 pipe PC-rel last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_GOT 64 /* X0 pipe hword 0 GOT offset */ +#define R_TILEGX_IMM16_X1_HW0_GOT 65 /* X1 pipe hword 0 GOT offset */ +#define R_TILEGX_IMM16_X0_HW0_PLT_PCREL 66 /* X0 pipe PC-rel PLT hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_PLT_PCREL 67 /* X1 pipe PC-rel PLT hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_PLT_PCREL 68 /* X0 pipe PC-rel PLT hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_PLT_PCREL 69 /* X1 pipe PC-rel PLT hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_PLT_PCREL 70 /* X0 pipe PC-rel PLT hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_PLT_PCREL 71 /* X1 pipe PC-rel PLT hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_GOT 72 /* X0 pipe last hword 0 GOT offset */ +#define R_TILEGX_IMM16_X1_HW0_LAST_GOT 73 /* X1 pipe last hword 0 GOT offset */ +#define R_TILEGX_IMM16_X0_HW1_LAST_GOT 74 /* X0 pipe last hword 1 GOT offset */ +#define R_TILEGX_IMM16_X1_HW1_LAST_GOT 75 /* X1 pipe last hword 1 GOT offset */ +#define R_TILEGX_IMM16_X0_HW3_PLT_PCREL 76 /* X0 pipe PC-rel PLT hword 3 */ +#define R_TILEGX_IMM16_X1_HW3_PLT_PCREL 77 /* X1 pipe PC-rel PLT hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_TLS_GD 78 /* X0 pipe hword 0 TLS GD offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_GD 79 /* X1 pipe hword 0 TLS GD offset */ +#define R_TILEGX_IMM16_X0_HW0_TLS_LE 80 /* X0 pipe hword 0 TLS LE offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_LE 81 /* X1 pipe hword 0 TLS LE offset */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE 82 /* X0 pipe last hword 0 LE off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE 83 /* X1 pipe last hword 0 LE off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE 84 /* X0 pipe last hword 1 LE off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE 85 /* X1 pipe last hword 1 LE off */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD 86 /* X0 pipe last hword 0 GD off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD 87 /* X1 pipe last hword 0 GD off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD 88 /* X0 pipe last hword 1 GD off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD 89 /* X1 pipe last hword 1 GD off */ +/* Relocs 90-91 are currently not defined. */ +#define R_TILEGX_IMM16_X0_HW0_TLS_IE 92 /* X0 pipe hword 0 TLS IE offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_IE 93 /* X1 pipe hword 0 TLS IE offset */ +#define R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL 94 /* X0 pipe PC-rel PLT last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL 95 /* X1 pipe PC-rel PLT last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL 96 /* X0 pipe PC-rel PLT last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL 97 /* X1 pipe PC-rel PLT last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL 98 /* X0 pipe PC-rel PLT last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL 99 /* X1 pipe PC-rel PLT last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE 100 /* X0 pipe last hword 0 IE off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE 101 /* X1 pipe last hword 0 IE off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE 102 /* X0 pipe last hword 1 IE off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE 103 /* X1 pipe last hword 1 IE off */ +/* Relocs 104-105 are currently not defined. */ +#define R_TILEGX_TLS_DTPMOD64 106 /* 64-bit ID of symbol's module */ +#define R_TILEGX_TLS_DTPOFF64 107 /* 64-bit offset in TLS block */ +#define R_TILEGX_TLS_TPOFF64 108 /* 64-bit offset in static TLS block */ +#define R_TILEGX_TLS_DTPMOD32 109 /* 32-bit ID of symbol's module */ +#define R_TILEGX_TLS_DTPOFF32 110 /* 32-bit offset in TLS block */ +#define R_TILEGX_TLS_TPOFF32 111 /* 32-bit offset in static TLS block */ +#define R_TILEGX_TLS_GD_CALL 112 /* "jal" for TLS GD */ +#define R_TILEGX_IMM8_X0_TLS_GD_ADD 113 /* X0 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_X1_TLS_GD_ADD 114 /* X1 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_Y0_TLS_GD_ADD 115 /* Y0 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_Y1_TLS_GD_ADD 116 /* Y1 pipe "addi" for TLS GD */ +#define R_TILEGX_TLS_IE_LOAD 117 /* "ld_tls" for TLS IE */ +#define R_TILEGX_IMM8_X0_TLS_ADD 118 /* X0 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_X1_TLS_ADD 119 /* X1 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_Y0_TLS_ADD 120 /* Y0 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_Y1_TLS_ADD 121 /* Y1 pipe "addi" for TLS GD/IE */ + +#define R_TILEGX_GNU_VTINHERIT 128 /* GNU C++ vtable hierarchy */ +#define R_TILEGX_GNU_VTENTRY 129 /* GNU C++ vtable member usage */ + +#define R_TILEGX_NUM 130 + + +#endif /* elf.h */ diff --git a/05/tcc-final-old/examples/ex1.c b/05/tcc-final-old/examples/ex1.c new file mode 100755 index 0000000..3d2a3e1 --- /dev/null +++ b/05/tcc-final-old/examples/ex1.c @@ -0,0 +1,8 @@ +#!/usr/local/bin/tcc -run +#include + +int main() +{ + printf("Hello World\n"); + return 0; +} diff --git a/05/tcc-final-old/examples/ex2.c b/05/tcc-final-old/examples/ex2.c new file mode 100644 index 0000000..d415e39 --- /dev/null +++ b/05/tcc-final-old/examples/ex2.c @@ -0,0 +1,98 @@ +#include +#include + +#define N 20 + +int nb_num; +int tab[N]; +int stack_ptr; +int stack_op[N]; +int stack_res[60]; +int result; + +int find(int n, int i1, int a, int b, int op) +{ + int i, j; + int c; + + if (stack_ptr >= 0) { + stack_res[3*stack_ptr] = a; + stack_op[stack_ptr] = op; + stack_res[3*stack_ptr+1] = b; + stack_res[3*stack_ptr+2] = n; + if (n == result) + return 1; + tab[i1] = n; + } + + for(i=0;i + +int fib(n) +{ + if (n <= 2) + return 1; + else + return fib(n-1) + fib(n-2); +} + +int main(int argc, char **argv) +{ + int n; + if (argc < 2) { + printf("usage: fib n\n" + "Compute nth Fibonacci number\n"); + return 1; + } + + n = atoi(argv[1]); + printf("fib(%d) = %d\n", n, fib(n, 2)); + return 0; +} diff --git a/05/tcc-final-old/examples/ex4.c b/05/tcc-final-old/examples/ex4.c new file mode 100755 index 0000000..f92c0da --- /dev/null +++ b/05/tcc-final-old/examples/ex4.c @@ -0,0 +1,26 @@ +#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11 +#include +#include +#include + +/* Yes, TCC can use X11 too ! */ + +int main(int argc, char **argv) +{ + Display *display; + Screen *screen; + + display = XOpenDisplay(""); + if (!display) { + fprintf(stderr, "Could not open X11 display\n"); + exit(1); + } + printf("X11 display opened.\n"); + screen = XScreenOfDisplay(display, 0); + printf("width = %d\nheight = %d\ndepth = %d\n", + screen->width, + screen->height, + screen->root_depth); + XCloseDisplay(display); + return 0; +} diff --git a/05/tcc-final-old/examples/ex5.c b/05/tcc-final-old/examples/ex5.c new file mode 100644 index 0000000..156425e --- /dev/null +++ b/05/tcc-final-old/examples/ex5.c @@ -0,0 +1,8 @@ +#include +#include + +int main() +{ + printf("Hello World\n"); + return 0; +} diff --git a/05/tcc-final-old/i386-asm.c b/05/tcc-final-old/i386-asm.c new file mode 100644 index 0000000..55c95af --- /dev/null +++ b/05/tcc-final-old/i386-asm.c @@ -0,0 +1,1723 @@ +/* + * i386 specific functions for TCC assembler + * + * Copyright (c) 2001, 2002 Fabrice Bellard + * Copyright (c) 2009 Frédéric Feret (x86_64 support) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +#define MAX_OPERANDS 3 + +#define TOK_ASM_first TOK_ASM_clc +#define TOK_ASM_last TOK_ASM_emms +#define TOK_ASM_alllast TOK_ASM_subps + +#define OPC_B 0x01 /* only used with OPC_WL */ +#define OPC_WL 0x02 /* accepts w, l or no suffix */ +#define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */ +#define OPC_REG 0x04 /* register is added to opcode */ +#define OPC_MODRM 0x08 /* modrm encoding */ + +#define OPCT_MASK 0x70 +#define OPC_FWAIT 0x10 /* add fwait opcode */ +#define OPC_SHIFT 0x20 /* shift opcodes */ +#define OPC_ARITH 0x30 /* arithmetic opcodes */ +#define OPC_FARITH 0x40 /* FPU arithmetic opcodes */ +#define OPC_TEST 0x50 /* test opcodes */ +#define OPCT_IS(v,i) (((v) & OPCT_MASK) == (i)) + +#define OPC_0F 0x100 /* Is secondary map (0x0f prefix) */ +#define OPC_48 0x200 /* Always has REX prefix */ +#ifdef TCC_TARGET_X86_64 +# define OPC_WLQ 0x1000 /* accepts w, l, q or no suffix */ +# define OPC_BWLQ (OPC_B | OPC_WLQ) /* accepts b, w, l, q or no suffix */ +# define OPC_WLX OPC_WLQ +# define OPC_BWLX OPC_BWLQ +#else +# define OPC_WLX OPC_WL +# define OPC_BWLX OPC_BWL +#endif + +#define OPC_GROUP_SHIFT 13 + +/* in order to compress the operand type, we use specific operands and + we or only with EA */ +enum { + OPT_REG8=0, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_REG16, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_REG32, /* warning: value is hardcoded from TOK_ASM_xxx */ +#ifdef TCC_TARGET_X86_64 + OPT_REG64, /* warning: value is hardcoded from TOK_ASM_xxx */ +#endif + OPT_MMX, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_SSE, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_CR, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_TR, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_DB, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_SEG, + OPT_ST, +#ifdef TCC_TARGET_X86_64 + OPT_REG8_LOW, /* %spl,%bpl,%sil,%dil, encoded like ah,ch,dh,bh, but + with REX prefix, not used in insn templates */ +#endif + OPT_IM8, + OPT_IM8S, + OPT_IM16, + OPT_IM32, +#ifdef TCC_TARGET_X86_64 + OPT_IM64, +#endif + OPT_EAX, /* %al, %ax, %eax or %rax register */ + OPT_ST0, /* %st(0) register */ + OPT_CL, /* %cl register */ + OPT_DX, /* %dx register */ + OPT_ADDR, /* OP_EA with only offset */ + OPT_INDIR, /* *(expr) */ + /* composite types */ + OPT_COMPOSITE_FIRST, + OPT_IM, /* IM8 | IM16 | IM32 */ + OPT_REG, /* REG8 | REG16 | REG32 | REG64 */ + OPT_REGW, /* REG16 | REG32 | REG64 */ + OPT_IMW, /* IM16 | IM32 */ + OPT_MMXSSE, /* MMX | SSE */ + OPT_DISP, /* Like OPT_ADDR, but emitted as displacement (for jumps) */ + OPT_DISP8, /* Like OPT_ADDR, but only 8bit (short jumps) */ + /* can be ored with any OPT_xxx */ + OPT_EA = 0x80 +}; + +#define OP_REG8 (1 << OPT_REG8) +#define OP_REG16 (1 << OPT_REG16) +#define OP_REG32 (1 << OPT_REG32) +#define OP_MMX (1 << OPT_MMX) +#define OP_SSE (1 << OPT_SSE) +#define OP_CR (1 << OPT_CR) +#define OP_TR (1 << OPT_TR) +#define OP_DB (1 << OPT_DB) +#define OP_SEG (1 << OPT_SEG) +#define OP_ST (1 << OPT_ST) +#define OP_IM8 (1 << OPT_IM8) +#define OP_IM8S (1 << OPT_IM8S) +#define OP_IM16 (1 << OPT_IM16) +#define OP_IM32 (1 << OPT_IM32) +#define OP_EAX (1 << OPT_EAX) +#define OP_ST0 (1 << OPT_ST0) +#define OP_CL (1 << OPT_CL) +#define OP_DX (1 << OPT_DX) +#define OP_ADDR (1 << OPT_ADDR) +#define OP_INDIR (1 << OPT_INDIR) +#ifdef TCC_TARGET_X86_64 +# define OP_REG64 (1 << OPT_REG64) +# define OP_REG8_LOW (1 << OPT_REG8_LOW) +# define OP_IM64 (1 << OPT_IM64) +# define OP_EA32 (OP_EA << 1) +#else +# define OP_REG64 0 +# define OP_REG8_LOW 0 +# define OP_IM64 0 +# define OP_EA32 0 +#endif + +#define OP_EA 0x40000000 +#define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64) + +#ifdef TCC_TARGET_X86_64 +# define TREG_XAX TREG_RAX +# define TREG_XCX TREG_RCX +# define TREG_XDX TREG_RDX +#else +# define TREG_XAX TREG_EAX +# define TREG_XCX TREG_ECX +# define TREG_XDX TREG_EDX +#endif + +typedef struct ASMInstr { + uint16_t sym; + uint16_t opcode; + uint16_t instr_type; + uint8_t nb_ops; + uint8_t op_type[MAX_OPERANDS]; /* see OP_xxx */ +} ASMInstr; + +typedef struct Operand { + uint32_t type; + int8_t reg; /* register, -1 if none */ + int8_t reg2; /* second register, -1 if none */ + uint8_t shift; + ExprValue e; +} Operand; + +static const uint8_t reg_to_size[9] = { +/* + [OP_REG8] = 0, + [OP_REG16] = 1, + [OP_REG32] = 2, +#ifdef TCC_TARGET_X86_64 + [OP_REG64] = 3, +#endif +*/ + 0, 0, 1, 0, 2, 0, 0, 0, 3 +}; + +#define NB_TEST_OPCODES 30 + +static const uint8_t test_bits[NB_TEST_OPCODES] = { + 0x00, /* o */ + 0x01, /* no */ + 0x02, /* b */ + 0x02, /* c */ + 0x02, /* nae */ + 0x03, /* nb */ + 0x03, /* nc */ + 0x03, /* ae */ + 0x04, /* e */ + 0x04, /* z */ + 0x05, /* ne */ + 0x05, /* nz */ + 0x06, /* be */ + 0x06, /* na */ + 0x07, /* nbe */ + 0x07, /* a */ + 0x08, /* s */ + 0x09, /* ns */ + 0x0a, /* p */ + 0x0a, /* pe */ + 0x0b, /* np */ + 0x0b, /* po */ + 0x0c, /* l */ + 0x0c, /* nge */ + 0x0d, /* nl */ + 0x0d, /* ge */ + 0x0e, /* le */ + 0x0e, /* ng */ + 0x0f, /* nle */ + 0x0f, /* g */ +}; + +static const uint8_t segment_prefixes[] = { + 0x26, /* es */ + 0x2e, /* cs */ + 0x36, /* ss */ + 0x3e, /* ds */ + 0x64, /* fs */ + 0x65 /* gs */ +}; + +static const ASMInstr asm_instrs[] = { +#define ALT(x) x +/* This removes a 0x0f in the second byte */ +#define O(o) ((uint64_t) ((((o) & 0xff00) == 0x0f00) ? ((((o) >> 8) & ~0xff) | ((o) & 0xff)) : (o))) +/* This constructs instr_type from opcode, type and group. */ +#define T(o,i,g) ((i) | ((g) << OPC_GROUP_SHIFT) | ((((o) & 0xff00) == 0x0f00) ? OPC_0F : 0)) +#define DEF_ASM_OP0(name, opcode) +#define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 0, { 0 } }, +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 1, { op0 }}, +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 2, { op0, op1 }}, +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 3, { op0, op1, op2 }}, +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif + /* last operation */ + { 0, }, +}; + +static const uint16_t op0_codes[] = { +#define ALT(x) +#define DEF_ASM_OP0(x, opcode) opcode, +#define DEF_ASM_OP0L(name, opcode, group, instr_type) +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif +}; + +static inline int get_reg_shift(TCCState *s1) +{ + int shift, v; + v = asm_int_expr(s1); + switch(v) { + case 1: + shift = 0; + break; + case 2: + shift = 1; + break; + case 4: + shift = 2; + break; + case 8: + shift = 3; + break; + default: + expect("1, 2, 4 or 8 constant"); + shift = 0; + break; + } + return shift; +} + +#ifdef TCC_TARGET_X86_64 +static int asm_parse_numeric_reg(int t, unsigned int *type) +{ + int reg = -1; + if (t >= TOK_IDENT && t < tok_ident) { + const char *s = table_ident[t - TOK_IDENT]->str; + char c; + *type = OP_REG64; + if (*s == 'c') { + s++; + *type = OP_CR; + } + if (*s++ != 'r') + return -1; + /* Don't allow leading '0'. */ + if ((c = *s++) >= '1' && c <= '9') + reg = c - '0'; + else + return -1; + if ((c = *s) >= '0' && c <= '5') + s++, reg = reg * 10 + c - '0'; + if (reg > 15) + return -1; + if ((c = *s) == 0) + ; + else if (*type != OP_REG64) + return -1; + else if (c == 'b' && !s[1]) + *type = OP_REG8; + else if (c == 'w' && !s[1]) + *type = OP_REG16; + else if (c == 'd' && !s[1]) + *type = OP_REG32; + else + return -1; + } + return reg; +} +#endif + +static int asm_parse_reg(unsigned int *type) +{ + int reg = 0; + *type = 0; + if (tok != '%') + goto error_32; + next(); + if (tok >= TOK_ASM_eax && tok <= TOK_ASM_edi) { + reg = tok - TOK_ASM_eax; + *type = OP_REG32; +#ifdef TCC_TARGET_X86_64 + } else if (tok >= TOK_ASM_rax && tok <= TOK_ASM_rdi) { + reg = tok - TOK_ASM_rax; + *type = OP_REG64; + } else if (tok == TOK_ASM_rip) { + reg = -2; /* Probably should use different escape code. */ + *type = OP_REG64; + } else if ((reg = asm_parse_numeric_reg(tok, type)) >= 0 + && (*type == OP_REG32 || *type == OP_REG64)) { + ; +#endif + } else { + error_32: + expect("register"); + } + next(); + return reg; +} + +static void parse_operand(TCCState *s1, Operand *op) +{ + ExprValue e; + int reg, indir; + const char *p; + + indir = 0; + if (tok == '*') { + next(); + indir = OP_INDIR; + } + + if (tok == '%') { + next(); + if (tok >= TOK_ASM_al && tok <= TOK_ASM_db7) { + reg = tok - TOK_ASM_al; + op->type = 1 << (reg >> 3); /* WARNING: do not change constant order */ + op->reg = reg & 7; + if ((op->type & OP_REG) && op->reg == TREG_XAX) + op->type |= OP_EAX; + else if (op->type == OP_REG8 && op->reg == TREG_XCX) + op->type |= OP_CL; + else if (op->type == OP_REG16 && op->reg == TREG_XDX) + op->type |= OP_DX; + } else if (tok >= TOK_ASM_dr0 && tok <= TOK_ASM_dr7) { + op->type = OP_DB; + op->reg = tok - TOK_ASM_dr0; + } else if (tok >= TOK_ASM_es && tok <= TOK_ASM_gs) { + op->type = OP_SEG; + op->reg = tok - TOK_ASM_es; + } else if (tok == TOK_ASM_st) { + op->type = OP_ST; + op->reg = 0; + next(); + if (tok == '(') { + next(); + if (tok != TOK_PPNUM) + goto reg_error; + p = tokc.str.data; + reg = p[0] - '0'; + if ((unsigned)reg >= 8 || p[1] != '\0') + goto reg_error; + op->reg = reg; + next(); + skip(')'); + } + if (op->reg == 0) + op->type |= OP_ST0; + goto no_skip; +#ifdef TCC_TARGET_X86_64 + } else if (tok >= TOK_ASM_spl && tok <= TOK_ASM_dil) { + op->type = OP_REG8 | OP_REG8_LOW; + op->reg = 4 + tok - TOK_ASM_spl; + } else if ((op->reg = asm_parse_numeric_reg(tok, &op->type)) >= 0) { + ; +#endif + } else { + reg_error: + tcc_error("unknown register %%%s", get_tok_str(tok, &tokc)); + } + next(); + no_skip: ; + } else if (tok == '$') { + /* constant value */ + next(); + asm_expr(s1, &e); + op->type = OP_IM32; + op->e = e; + if (!op->e.sym) { + if (op->e.v == (uint8_t)op->e.v) + op->type |= OP_IM8; + if (op->e.v == (int8_t)op->e.v) + op->type |= OP_IM8S; + if (op->e.v == (uint16_t)op->e.v) + op->type |= OP_IM16; +#ifdef TCC_TARGET_X86_64 + if (op->e.v != (int32_t)op->e.v && op->e.v != (uint32_t)op->e.v) + op->type = OP_IM64; +#endif + } + } else { + /* address(reg,reg2,shift) with all variants */ + op->type = OP_EA; + op->reg = -1; + op->reg2 = -1; + op->shift = 0; + if (tok != '(') { + asm_expr(s1, &e); + op->e = e; + } else { + next(); + if (tok == '%') { + unget_tok('('); + op->e.v = 0; + op->e.sym = NULL; + } else { + /* bracketed offset expression */ + asm_expr(s1, &e); + if (tok != ')') + expect(")"); + next(); + op->e.v = e.v; + op->e.sym = e.sym; + } + op->e.pcrel = 0; + } + if (tok == '(') { + unsigned int type = 0; + next(); + if (tok != ',') { + op->reg = asm_parse_reg(&type); + } + if (tok == ',') { + next(); + if (tok != ',') { + op->reg2 = asm_parse_reg(&type); + } + if (tok == ',') { + next(); + op->shift = get_reg_shift(s1); + } + } + if (type & OP_REG32) + op->type |= OP_EA32; + skip(')'); + } + if (op->reg == -1 && op->reg2 == -1) + op->type |= OP_ADDR; + } + op->type |= indir; +} + +/* XXX: unify with C code output ? */ +ST_FUNC void gen_expr32(ExprValue *pe) +{ + if (pe->pcrel) + /* If PC-relative, always set VT_SYM, even without symbol, + so as to force a relocation to be emitted. */ + gen_addrpc32(VT_SYM, pe->sym, pe->v); + else + gen_addr32(pe->sym ? VT_SYM : 0, pe->sym, pe->v); +} + +#ifdef TCC_TARGET_X86_64 +ST_FUNC void gen_expr64(ExprValue *pe) +{ + gen_addr64(pe->sym ? VT_SYM : 0, pe->sym, pe->v); +} +#endif + +/* XXX: unify with C code output ? */ +static void gen_disp32(ExprValue *pe) +{ + Sym *sym = pe->sym; + ElfSym *esym = elfsym(sym); + if (esym && esym->st_shndx == cur_text_section->sh_num) { + /* same section: we can output an absolute value. Note + that the TCC compiler behaves differently here because + it always outputs a relocation to ease (future) code + elimination in the linker */ + gen_le32(pe->v + esym->st_value - ind - 4); + } else { + if (sym && sym->type.t == VT_VOID) { + sym->type.t = VT_FUNC; + sym->type.ref = NULL; + } + gen_addrpc32(VT_SYM, sym, pe->v); + } +} + +/* generate the modrm operand */ +static inline int asm_modrm(int reg, Operand *op) +{ + int mod, reg1, reg2, sib_reg1; + + if (op->type & (OP_REG | OP_MMX | OP_SSE)) { + g(0xc0 + (reg << 3) + op->reg); + } else if (op->reg == -1 && op->reg2 == -1) { + /* displacement only */ +#ifdef TCC_TARGET_X86_64 + g(0x04 + (reg << 3)); + g(0x25); +#else + g(0x05 + (reg << 3)); +#endif + gen_expr32(&op->e); +#ifdef TCC_TARGET_X86_64 + } else if (op->reg == -2) { + ExprValue *pe = &op->e; + g(0x05 + (reg << 3)); + gen_addrpc32(pe->sym ? VT_SYM : 0, pe->sym, pe->v); + return ind; +#endif + } else { + sib_reg1 = op->reg; + /* fist compute displacement encoding */ + if (sib_reg1 == -1) { + sib_reg1 = 5; + mod = 0x00; + } else if (op->e.v == 0 && !op->e.sym && op->reg != 5) { + mod = 0x00; + } else if (op->e.v == (int8_t)op->e.v && !op->e.sym) { + mod = 0x40; + } else { + mod = 0x80; + } + /* compute if sib byte needed */ + reg1 = op->reg; + if (op->reg2 != -1) + reg1 = 4; + g(mod + (reg << 3) + reg1); + if (reg1 == 4) { + /* add sib byte */ + reg2 = op->reg2; + if (reg2 == -1) + reg2 = 4; /* indicate no index */ + g((op->shift << 6) + (reg2 << 3) + sib_reg1); + } + /* add offset */ + if (mod == 0x40) { + g(op->e.v); + } else if (mod == 0x80 || op->reg == -1) { + gen_expr32(&op->e); + } + } + return 0; +} + +#ifdef TCC_TARGET_X86_64 +#define REX_W 0x48 +#define REX_R 0x44 +#define REX_X 0x42 +#define REX_B 0x41 + +static void asm_rex(int width64, Operand *ops, int nb_ops, int *op_type, + int regi, int rmi) +{ + unsigned char rex = width64 ? 0x48 : 0; + int saw_high_8bit = 0; + int i; + if (rmi == -1) { + /* No mod/rm byte, but we might have a register op nevertheless + (we will add it to the opcode later). */ + for(i = 0; i < nb_ops; i++) { + if (op_type[i] & (OP_REG | OP_ST)) { + if (ops[i].reg >= 8) { + rex |= REX_B; + ops[i].reg -= 8; + } else if (ops[i].type & OP_REG8_LOW) + rex |= 0x40; + else if (ops[i].type & OP_REG8 && ops[i].reg >= 4) + /* An 8 bit reg >= 4 without REG8 is ah/ch/dh/bh */ + saw_high_8bit = ops[i].reg; + break; + } + } + } else { + if (regi != -1) { + if (ops[regi].reg >= 8) { + rex |= REX_R; + ops[regi].reg -= 8; + } else if (ops[regi].type & OP_REG8_LOW) + rex |= 0x40; + else if (ops[regi].type & OP_REG8 && ops[regi].reg >= 4) + /* An 8 bit reg >= 4 without REG8 is ah/ch/dh/bh */ + saw_high_8bit = ops[regi].reg; + } + if (ops[rmi].type & (OP_REG | OP_MMX | OP_SSE | OP_CR | OP_EA)) { + if (ops[rmi].reg >= 8) { + rex |= REX_B; + ops[rmi].reg -= 8; + } else if (ops[rmi].type & OP_REG8_LOW) + rex |= 0x40; + else if (ops[rmi].type & OP_REG8 && ops[rmi].reg >= 4) + /* An 8 bit reg >= 4 without REG8 is ah/ch/dh/bh */ + saw_high_8bit = ops[rmi].reg; + } + if (ops[rmi].type & OP_EA && ops[rmi].reg2 >= 8) { + rex |= REX_X; + ops[rmi].reg2 -= 8; + } + } + if (rex) { + if (saw_high_8bit) + tcc_error("can't encode register %%%ch when REX prefix is required", + "acdb"[saw_high_8bit-4]); + g(rex); + } +} +#endif + +static void maybe_print_stats (void) +{ + static int already = 1; + if (!already) + /* print stats about opcodes */ + { + const struct ASMInstr *pa; + int freq[4]; + int op_vals[500]; + int nb_op_vals, i, j; + + already = 1; + nb_op_vals = 0; + memset(freq, 0, sizeof(freq)); + for(pa = asm_instrs; pa->sym != 0; pa++) { + freq[pa->nb_ops]++; + //for(i=0;inb_ops;i++) { + for(j=0;jop_type[i] == op_vals[j]) + if (pa->instr_type == op_vals[j]) + goto found; + } + //op_vals[nb_op_vals++] = pa->op_type[i]; + op_vals[nb_op_vals++] = pa->instr_type; + found: ; + //} + } + for(i=0;i= TOK_ASM_wait && opcode <= TOK_ASM_repnz) + unget_tok(';'); + + /* get operands */ + pop = ops; + nb_ops = 0; + seg_prefix = 0; + alltypes = 0; + for(;;) { + if (tok == ';' || tok == TOK_LINEFEED) + break; + if (nb_ops >= MAX_OPERANDS) { + tcc_error("incorrect number of operands"); + } + parse_operand(s1, pop); + if (tok == ':') { + if (pop->type != OP_SEG || seg_prefix) + tcc_error("incorrect prefix"); + seg_prefix = segment_prefixes[pop->reg]; + next(); + parse_operand(s1, pop); + if (!(pop->type & OP_EA)) { + tcc_error("segment prefix must be followed by memory reference"); + } + } + pop++; + nb_ops++; + if (tok != ',') + break; + next(); + } + + s = 0; /* avoid warning */ + +again: + /* optimize matching by using a lookup table (no hashing is needed + !) */ + for(pa = asm_instrs; pa->sym != 0; pa++) { + int it = pa->instr_type & OPCT_MASK; + s = 0; + if (it == OPC_FARITH) { + v = opcode - pa->sym; + if (!((unsigned)v < 8 * 6 && (v % 6) == 0)) + continue; + } else if (it == OPC_ARITH) { + if (!(opcode >= pa->sym && opcode < pa->sym + 8*NBWLX)) + continue; + s = (opcode - pa->sym) % NBWLX; + if ((pa->instr_type & OPC_BWLX) == OPC_WLX) + { + /* We need to reject the xxxb opcodes that we accepted above. + Note that pa->sym for WLX opcodes is the 'w' token, + to get the 'b' token subtract one. */ + if (((opcode - pa->sym + 1) % NBWLX) == 0) + continue; + s++; + } + } else if (it == OPC_SHIFT) { + if (!(opcode >= pa->sym && opcode < pa->sym + 7*NBWLX)) + continue; + s = (opcode - pa->sym) % NBWLX; + } else if (it == OPC_TEST) { + if (!(opcode >= pa->sym && opcode < pa->sym + NB_TEST_OPCODES)) + continue; + /* cmovxx is a test opcode but accepts multiple sizes. + The suffixes aren't encoded in the table, instead we + simply force size autodetection always and deal with suffixed + variants below when we don't find e.g. "cmovzl". */ + if (pa->instr_type & OPC_WLX) + s = NBWLX - 1; + } else if (pa->instr_type & OPC_B) { +#ifdef TCC_TARGET_X86_64 + /* Some instructions don't have the full size but only + bwl form. insb e.g. */ + if ((pa->instr_type & OPC_WLQ) != OPC_WLQ + && !(opcode >= pa->sym && opcode < pa->sym + NBWLX-1)) + continue; +#endif + if (!(opcode >= pa->sym && opcode < pa->sym + NBWLX)) + continue; + s = opcode - pa->sym; + } else if (pa->instr_type & OPC_WLX) { + if (!(opcode >= pa->sym && opcode < pa->sym + NBWLX-1)) + continue; + s = opcode - pa->sym + 1; + } else { + if (pa->sym != opcode) + continue; + } + if (pa->nb_ops != nb_ops) + continue; +#ifdef TCC_TARGET_X86_64 + /* Special case for moves. Selecting the IM64->REG64 form + should only be done if we really have an >32bit imm64, and that + is hardcoded. Ignore it here. */ + if (pa->opcode == 0xb0 && ops[0].type != OP_IM64 + && (ops[1].type & OP_REG) == OP_REG64 + && !(pa->instr_type & OPC_0F)) + continue; +#endif + /* now decode and check each operand */ + alltypes = 0; + for(i = 0; i < nb_ops; i++) { + int op1, op2; + op1 = pa->op_type[i]; + op2 = op1 & 0x1f; + switch(op2) { + case OPT_IM: + v = OP_IM8 | OP_IM16 | OP_IM32; + break; + case OPT_REG: + v = OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64; + break; + case OPT_REGW: + v = OP_REG16 | OP_REG32 | OP_REG64; + break; + case OPT_IMW: + v = OP_IM16 | OP_IM32; + break; + case OPT_MMXSSE: + v = OP_MMX | OP_SSE; + break; + case OPT_DISP: + case OPT_DISP8: + v = OP_ADDR; + break; + default: + v = 1 << op2; + break; + } + if (op1 & OPT_EA) + v |= OP_EA; + op_type[i] = v; + if ((ops[i].type & v) == 0) + goto next; + alltypes |= ops[i].type; + } + /* all is matching ! */ + break; + next: ; + } + if (pa->sym == 0) { + if (opcode >= TOK_ASM_first && opcode <= TOK_ASM_last) { + int b; + b = op0_codes[opcode - TOK_ASM_first]; + if (b & 0xff00) + g(b >> 8); + g(b); + return; + } else if (opcode <= TOK_ASM_alllast) { + tcc_error("bad operand with opcode '%s'", + get_tok_str(opcode, NULL)); + } else { + /* Special case for cmovcc, we accept size suffixes but ignore + them, but we don't want them to blow up our tables. */ + TokenSym *ts = table_ident[opcode - TOK_IDENT]; + if (ts->len >= 6 + && strchr("wlq", ts->str[ts->len-1]) + && !memcmp(ts->str, "cmov", 4)) { + opcode = tok_alloc(ts->str, ts->len-1)->tok; + goto again; + } + tcc_error("unknown opcode '%s'", ts->str); + } + } + /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */ + autosize = NBWLX-1; +#ifdef TCC_TARGET_X86_64 + /* XXX the autosize should rather be zero, to not have to adjust this + all the time. */ + if ((pa->instr_type & OPC_BWLQ) == OPC_B) + autosize = NBWLX-2; +#endif + if (s == autosize) { + /* Check for register operands providing hints about the size. + Start from the end, i.e. destination operands. This matters + only for opcodes accepting different sized registers, lar and lsl + are such opcodes. */ + for(i = nb_ops - 1; s == autosize && i >= 0; i--) { + if ((ops[i].type & OP_REG) && !(op_type[i] & (OP_CL | OP_DX))) + s = reg_to_size[ops[i].type & OP_REG]; + } + if (s == autosize) { + if ((opcode == TOK_ASM_push || opcode == TOK_ASM_pop) && + (ops[0].type & (OP_SEG | OP_IM8S | OP_IM32))) + s = 2; + else if ((opcode == TOK_ASM_push || opcode == TOK_ASM_pop) && + (ops[0].type & OP_EA)) + s = NBWLX - 2; + else + tcc_error("cannot infer opcode suffix"); + } + } + +#ifdef TCC_TARGET_X86_64 + /* Generate addr32 prefix if needed */ + for(i = 0; i < nb_ops; i++) { + if (ops[i].type & OP_EA32) { + g(0x67); + break; + } + } +#endif + /* generate data16 prefix if needed */ + p66 = 0; + if (s == 1) + p66 = 1; + else { + /* accepting mmx+sse in all operands --> needs 0x66 to + switch to sse mode. Accepting only sse in an operand --> is + already SSE insn and needs 0x66/f2/f3 handling. */ + for (i = 0; i < nb_ops; i++) + if ((op_type[i] & (OP_MMX | OP_SSE)) == (OP_MMX | OP_SSE) + && ops[i].type & OP_SSE) + p66 = 1; + } + if (p66) + g(0x66); +#ifdef TCC_TARGET_X86_64 + rex64 = 0; + if (pa->instr_type & OPC_48) + rex64 = 1; + else if (s == 3 || (alltypes & OP_REG64)) { + /* generate REX prefix */ + int default64 = 0; + for(i = 0; i < nb_ops; i++) { + if (op_type[i] == OP_REG64 && pa->opcode != 0xb8) { + /* If only 64bit regs are accepted in one operand + this is a default64 instruction without need for + REX prefixes, except for movabs(0xb8). */ + default64 = 1; + break; + } + } + /* XXX find better encoding for the default64 instructions. */ + if (((opcode != TOK_ASM_push && opcode != TOK_ASM_pop + && opcode != TOK_ASM_pushw && opcode != TOK_ASM_pushl + && opcode != TOK_ASM_pushq && opcode != TOK_ASM_popw + && opcode != TOK_ASM_popl && opcode != TOK_ASM_popq + && opcode != TOK_ASM_call && opcode != TOK_ASM_jmp)) + && !default64) + rex64 = 1; + } +#endif + + /* now generates the operation */ + if (OPCT_IS(pa->instr_type, OPC_FWAIT)) + g(0x9b); + if (seg_prefix) + g(seg_prefix); + + v = pa->opcode; + if (pa->instr_type & OPC_0F) + v = ((v & ~0xff) << 8) | 0x0f00 | (v & 0xff); + if ((v == 0x69 || v == 0x6b) && nb_ops == 2) { + /* kludge for imul $im, %reg */ + nb_ops = 3; + ops[2] = ops[1]; + op_type[2] = op_type[1]; + } else if (v == 0xcd && ops[0].e.v == 3 && !ops[0].e.sym) { + v--; /* int $3 case */ + nb_ops = 0; + } else if ((v == 0x06 || v == 0x07)) { + if (ops[0].reg >= 4) { + /* push/pop %fs or %gs */ + v = 0x0fa0 + (v - 0x06) + ((ops[0].reg - 4) << 3); + } else { + v += ops[0].reg << 3; + } + nb_ops = 0; + } else if (v <= 0x05) { + /* arith case */ + v += ((opcode - TOK_ASM_addb) / NBWLX) << 3; + } else if ((pa->instr_type & (OPCT_MASK | OPC_MODRM)) == OPC_FARITH) { + /* fpu arith case */ + v += ((opcode - pa->sym) / 6) << 3; + } + + /* search which operand will be used for modrm */ + modrm_index = -1; + modreg_index = -1; + if (pa->instr_type & OPC_MODRM) { + if (!nb_ops) { + /* A modrm opcode without operands is a special case (e.g. mfence). + It has a group and acts as if there's an register operand 0 + (ax). */ + i = 0; + ops[i].type = OP_REG; + ops[i].reg = 0; + goto modrm_found; + } + /* first look for an ea operand */ + for(i = 0;i < nb_ops; i++) { + if (op_type[i] & OP_EA) + goto modrm_found; + } + /* then if not found, a register or indirection (shift instructions) */ + for(i = 0;i < nb_ops; i++) { + if (op_type[i] & (OP_REG | OP_MMX | OP_SSE | OP_INDIR)) + goto modrm_found; + } +#ifdef ASM_DEBUG + tcc_error("bad op table"); +#endif + modrm_found: + modrm_index = i; + /* if a register is used in another operand then it is + used instead of group */ + for(i = 0;i < nb_ops; i++) { + int t = op_type[i]; + if (i != modrm_index && + (t & (OP_REG | OP_MMX | OP_SSE | OP_CR | OP_TR | OP_DB | OP_SEG))) { + modreg_index = i; + break; + } + } + } +#ifdef TCC_TARGET_X86_64 + asm_rex (rex64, ops, nb_ops, op_type, modreg_index, modrm_index); +#endif + + if (pa->instr_type & OPC_REG) { + /* mov $im, %reg case */ + if (v == 0xb0 && s >= 1) + v += 7; + for(i = 0; i < nb_ops; i++) { + if (op_type[i] & (OP_REG | OP_ST)) { + v += ops[i].reg; + break; + } + } + } + if (pa->instr_type & OPC_B) + v += s >= 1; + if (nb_ops == 1 && pa->op_type[0] == OPT_DISP8) { + ElfSym *esym; + int jmp_disp; + + /* see if we can really generate the jump with a byte offset */ + esym = elfsym(ops[0].e.sym); + if (!esym || esym->st_shndx != cur_text_section->sh_num) + goto no_short_jump; + jmp_disp = ops[0].e.v + esym->st_value - ind - 2 - (v >= 0xff); + if (jmp_disp == (int8_t)jmp_disp) { + /* OK to generate jump */ + ops[0].e.sym = 0; + ops[0].e.v = jmp_disp; + op_type[0] = OP_IM8S; + } else { + no_short_jump: + /* long jump will be allowed. need to modify the + opcode slightly */ + if (v == 0xeb) /* jmp */ + v = 0xe9; + else if (v == 0x70) /* jcc */ + v += 0x0f10; + else + tcc_error("invalid displacement"); + } + } + if (OPCT_IS(pa->instr_type, OPC_TEST)) + v += test_bits[opcode - pa->sym]; + op1 = v >> 16; + if (op1) + g(op1); + op1 = (v >> 8) & 0xff; + if (op1) + g(op1); + g(v); + + if (OPCT_IS(pa->instr_type, OPC_SHIFT)) { + reg = (opcode - pa->sym) / NBWLX; + if (reg == 6) + reg = 7; + } else if (OPCT_IS(pa->instr_type, OPC_ARITH)) { + reg = (opcode - pa->sym) / NBWLX; + } else if (OPCT_IS(pa->instr_type, OPC_FARITH)) { + reg = (opcode - pa->sym) / 6; + } else { + reg = (pa->instr_type >> OPC_GROUP_SHIFT) & 7; + } + + pc = 0; + if (pa->instr_type & OPC_MODRM) { + /* if a register is used in another operand then it is + used instead of group */ + if (modreg_index >= 0) + reg = ops[modreg_index].reg; + pc = asm_modrm(reg, &ops[modrm_index]); + } + + /* emit constants */ +#ifndef TCC_TARGET_X86_64 + if (!(pa->instr_type & OPC_0F) + && (pa->opcode == 0x9a || pa->opcode == 0xea)) { + /* ljmp or lcall kludge */ + gen_expr32(&ops[1].e); + if (ops[0].e.sym) + tcc_error("cannot relocate"); + gen_le16(ops[0].e.v); + return; + } +#endif + for(i = 0;i < nb_ops; i++) { + v = op_type[i]; + if (v & (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64 | OP_IM8S | OP_ADDR)) { + /* if multiple sizes are given it means we must look + at the op size */ + if ((v | OP_IM8 | OP_IM64) == (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64)) { + if (s == 0) + v = OP_IM8; + else if (s == 1) + v = OP_IM16; + else if (s == 2 || (v & OP_IM64) == 0) + v = OP_IM32; + else + v = OP_IM64; + } + + if ((v & (OP_IM8 | OP_IM8S | OP_IM16)) && ops[i].e.sym) + tcc_error("cannot relocate"); + + if (v & (OP_IM8 | OP_IM8S)) { + g(ops[i].e.v); + } else if (v & OP_IM16) { + gen_le16(ops[i].e.v); +#ifdef TCC_TARGET_X86_64 + } else if (v & OP_IM64) { + gen_expr64(&ops[i].e); +#endif + } else if (pa->op_type[i] == OPT_DISP || pa->op_type[i] == OPT_DISP8) { + gen_disp32(&ops[i].e); + } else { + gen_expr32(&ops[i].e); + } + } + } + + /* after immediate operands, adjust pc-relative address */ + if (pc) + add32le(cur_text_section->data + pc - 4, pc - ind); +} + +/* return the constraint priority (we allocate first the lowest + numbered constraints) */ +static inline int constraint_priority(const char *str) +{ + int priority, c, pr; + + /* we take the lowest priority */ + priority = 0; + for(;;) { + c = *str; + if (c == '\0') + break; + str++; + switch(c) { + case 'A': + pr = 0; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'S': + case 'D': + pr = 1; + break; + case 'q': + pr = 2; + break; + case 'r': + case 'R': + case 'p': + pr = 3; + break; + case 'N': + case 'M': + case 'I': + case 'e': + case 'i': + case 'm': + case 'g': + pr = 4; + break; + default: + tcc_error("unknown constraint '%c'", c); + pr = 0; + } + if (pr > priority) + priority = pr; + } + return priority; +} + +static const char *skip_constraint_modifiers(const char *p) +{ + while (*p == '=' || *p == '&' || *p == '+' || *p == '%') + p++; + return p; +} + +/* If T (a token) is of the form "%reg" returns the register + number and type, otherwise return -1. */ +ST_FUNC int asm_parse_regvar (int t) +{ + const char *s; + Operand op; + if (t < TOK_IDENT) + return -1; + s = table_ident[t - TOK_IDENT]->str; + if (s[0] != '%') + return -1; + t = tok_alloc(s+1, strlen(s)-1)->tok; + unget_tok(t); + unget_tok('%'); + parse_operand(tcc_state, &op); + /* Accept only integer regs for now. */ + if (op.type & OP_REG) + return op.reg; + else + return -1; +} + +#define REG_OUT_MASK 0x01 +#define REG_IN_MASK 0x02 + +#define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask) + +ST_FUNC void asm_compute_constraints(ASMOperand *operands, + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg) +{ + ASMOperand *op; + int sorted_op[MAX_ASM_OPERANDS]; + int i, j, k, p1, p2, tmp, reg, c, reg_mask; + const char *str; + uint8_t regs_allocated[NB_ASM_REGS]; + + /* init fields */ + for(i=0;iinput_index = -1; + op->ref_index = -1; + op->reg = -1; + op->is_memory = 0; + op->is_rw = 0; + } + /* compute constraint priority and evaluate references to output + constraints if input constraints */ + for(i=0;iconstraint; + str = skip_constraint_modifiers(str); + if (isnum(*str) || *str == '[') { + /* this is a reference to another constraint */ + k = find_constraint(operands, nb_operands, str, NULL); + if ((unsigned)k >= i || i < nb_outputs) + tcc_error("invalid reference in constraint %d ('%s')", + i, str); + op->ref_index = k; + if (operands[k].input_index >= 0) + tcc_error("cannot reference twice the same operand"); + operands[k].input_index = i; + op->priority = 5; + } else if ((op->vt->r & VT_VALMASK) == VT_LOCAL + && op->vt->sym + && (reg = op->vt->sym->r & VT_VALMASK) < VT_CONST) { + op->priority = 1; + op->reg = reg; + } else { + op->priority = constraint_priority(str); + } + } + + /* sort operands according to their priority */ + for(i=0;iconstraint; + /* no need to allocate references */ + if (op->ref_index >= 0) + continue; + /* select if register is used for output, input or both */ + if (op->input_index >= 0) { + reg_mask = REG_IN_MASK | REG_OUT_MASK; + } else if (j < nb_outputs) { + reg_mask = REG_OUT_MASK; + } else { + reg_mask = REG_IN_MASK; + } + if (op->reg >= 0) { + if (is_reg_allocated(op->reg)) + tcc_error("asm regvar requests register that's taken already"); + reg = op->reg; + goto reg_found; + } + try_next: + c = *str++; + switch(c) { + case '=': + goto try_next; + case '+': + op->is_rw = 1; + /* FALL THRU */ + case '&': + if (j >= nb_outputs) + tcc_error("'%c' modifier can only be applied to outputs", c); + reg_mask = REG_IN_MASK | REG_OUT_MASK; + goto try_next; + case 'A': + /* allocate both eax and edx */ + if (is_reg_allocated(TREG_XAX) || + is_reg_allocated(TREG_XDX)) + goto try_next; + op->is_llong = 1; + op->reg = TREG_XAX; + regs_allocated[TREG_XAX] |= reg_mask; + regs_allocated[TREG_XDX] |= reg_mask; + break; + case 'a': + reg = TREG_XAX; + goto alloc_reg; + case 'b': + reg = 3; + goto alloc_reg; + case 'c': + reg = TREG_XCX; + goto alloc_reg; + case 'd': + reg = TREG_XDX; + goto alloc_reg; + case 'S': + reg = 6; + goto alloc_reg; + case 'D': + reg = 7; + alloc_reg: + if (is_reg_allocated(reg)) + goto try_next; + goto reg_found; + case 'q': + /* eax, ebx, ecx or edx */ + for(reg = 0; reg < 4; reg++) { + if (!is_reg_allocated(reg)) + goto reg_found; + } + goto try_next; + case 'r': + case 'R': + case 'p': /* A general address, for x86(64) any register is acceptable*/ + /* any general register */ + for(reg = 0; reg < 8; reg++) { + if (!is_reg_allocated(reg)) + goto reg_found; + } + goto try_next; + reg_found: + /* now we can reload in the register */ + op->is_llong = 0; + op->reg = reg; + regs_allocated[reg] |= reg_mask; + break; + case 'e': + case 'i': + if (!((op->vt->r & (VT_VALMASK | VT_LVAL)) == VT_CONST)) + goto try_next; + break; + case 'I': + case 'N': + case 'M': + if (!((op->vt->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)) + goto try_next; + break; + case 'm': + case 'g': + /* nothing special to do because the operand is already in + memory, except if the pointer itself is stored in a + memory variable (VT_LLOCAL case) */ + /* XXX: fix constant case */ + /* if it is a reference to a memory zone, it must lie + in a register, so we reserve the register in the + input registers and a load will be generated + later */ + if (j < nb_outputs || c == 'm') { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) { + /* any general register */ + for(reg = 0; reg < 8; reg++) { + if (!(regs_allocated[reg] & REG_IN_MASK)) + goto reg_found1; + } + goto try_next; + reg_found1: + /* now we can reload in the register */ + regs_allocated[reg] |= REG_IN_MASK; + op->reg = reg; + op->is_memory = 1; + } + } + break; + default: + tcc_error("asm constraint %d ('%s') could not be satisfied", + j, op->constraint); + break; + } + /* if a reference is present for that operand, we assign it too */ + if (op->input_index >= 0) { + operands[op->input_index].reg = op->reg; + operands[op->input_index].is_llong = op->is_llong; + } + } + + /* compute out_reg. It is used to store outputs registers to memory + locations references by pointers (VT_LLOCAL case) */ + *pout_reg = -1; + for(i=0;ireg >= 0 && + (op->vt->r & VT_VALMASK) == VT_LLOCAL && + !op->is_memory) { + for(reg = 0; reg < 8; reg++) { + if (!(regs_allocated[reg] & REG_OUT_MASK)) + goto reg_found2; + } + tcc_error("could not find free output register for reloading"); + reg_found2: + *pout_reg = reg; + break; + } + } + + /* print sorted constraints */ +#ifdef ASM_DEBUG + for(i=0;iid ? get_tok_str(op->id, NULL) : "", + op->constraint, + op->vt->r, + op->reg); + } + if (*pout_reg >= 0) + printf("out_reg=%d\n", *pout_reg); +#endif +} + +ST_FUNC void subst_asm_operand(CString *add_str, + SValue *sv, int modifier) +{ + int r, reg, size, val; + char buf[64]; + + r = sv->r; + if ((r & VT_VALMASK) == VT_CONST) { + if (!(r & VT_LVAL) && modifier != 'c' && modifier != 'n' && + modifier != 'P') + cstr_ccat(add_str, '$'); + if (r & VT_SYM) { + const char *name = get_tok_str(sv->sym->v, NULL); + if (sv->sym->v >= SYM_FIRST_ANOM) { + /* In case of anonymous symbols ("L.42", used + for static data labels) we can't find them + in the C symbol table when later looking up + this name. So enter them now into the asm label + list when we still know the symbol. */ + get_asm_sym(tok_alloc(name, strlen(name))->tok, sv->sym); + } + cstr_cat(add_str, name, -1); + if ((uint32_t)sv->c.i == 0) + goto no_offset; + cstr_ccat(add_str, '+'); + } + val = sv->c.i; + if (modifier == 'n') + val = -val; + snprintf(buf, sizeof(buf), "%d", (int)sv->c.i); + cstr_cat(add_str, buf, -1); + no_offset:; +#ifdef TCC_TARGET_X86_64 + if (r & VT_LVAL) + cstr_cat(add_str, "(%rip)", -1); +#endif + } else if ((r & VT_VALMASK) == VT_LOCAL) { +#ifdef TCC_TARGET_X86_64 + snprintf(buf, sizeof(buf), "%d(%%rbp)", (int)sv->c.i); +#else + snprintf(buf, sizeof(buf), "%d(%%ebp)", (int)sv->c.i); +#endif + cstr_cat(add_str, buf, -1); + } else if (r & VT_LVAL) { + reg = r & VT_VALMASK; + if (reg >= VT_CONST) + tcc_error("internal compiler error"); + snprintf(buf, sizeof(buf), "(%%%s)", +#ifdef TCC_TARGET_X86_64 + get_tok_str(TOK_ASM_rax + reg, NULL) +#else + get_tok_str(TOK_ASM_eax + reg, NULL) +#endif + ); + cstr_cat(add_str, buf, -1); + } else { + /* register case */ + reg = r & VT_VALMASK; + if (reg >= VT_CONST) + tcc_error("internal compiler error"); + + /* choose register operand size */ + if ((sv->type.t & VT_BTYPE) == VT_BYTE || + (sv->type.t & VT_BTYPE) == VT_BOOL) + size = 1; + else if ((sv->type.t & VT_BTYPE) == VT_SHORT) + size = 2; +#ifdef TCC_TARGET_X86_64 + else if ((sv->type.t & VT_BTYPE) == VT_LLONG || + (sv->type.t & VT_BTYPE) == VT_PTR) + size = 8; +#endif + else + size = 4; + if (size == 1 && reg >= 4) + size = 4; + + if (modifier == 'b') { + if (reg >= 4) + tcc_error("cannot use byte register"); + size = 1; + } else if (modifier == 'h') { + if (reg >= 4) + tcc_error("cannot use byte register"); + size = -1; + } else if (modifier == 'w') { + size = 2; + } else if (modifier == 'k') { + size = 4; +#ifdef TCC_TARGET_X86_64 + } else if (modifier == 'q') { + size = 8; +#endif + } + + switch(size) { + case -1: + reg = TOK_ASM_ah + reg; + break; + case 1: + reg = TOK_ASM_al + reg; + break; + case 2: + reg = TOK_ASM_ax + reg; + break; + default: + reg = TOK_ASM_eax + reg; + break; +#ifdef TCC_TARGET_X86_64 + case 8: + reg = TOK_ASM_rax + reg; + break; +#endif + } + snprintf(buf, sizeof(buf), "%%%s", get_tok_str(reg, NULL)); + cstr_cat(add_str, buf, -1); + } +} + +/* generate prolog and epilog code for asm statement */ +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg) +{ + uint8_t regs_allocated[NB_ASM_REGS]; + ASMOperand *op; + int i, reg; + + /* Strictly speaking %Xbp and %Xsp should be included in the + call-preserved registers, but currently it doesn't matter. */ +#ifdef TCC_TARGET_X86_64 +#ifdef TCC_TARGET_PE + static uint8_t reg_saved[] = { 3, 6, 7, 12, 13, 14, 15 }; +#else + static uint8_t reg_saved[] = { 3, 12, 13, 14, 15 }; +#endif +#else + static uint8_t reg_saved[] = { 3, 6, 7 }; +#endif + + /* mark all used registers */ + memcpy(regs_allocated, clobber_regs, sizeof(regs_allocated)); + for(i = 0; i < nb_operands;i++) { + op = &operands[i]; + if (op->reg >= 0) + regs_allocated[op->reg] = 1; + } + if (!is_output) { + /* generate reg save code */ + for(i = 0; i < sizeof(reg_saved)/sizeof(reg_saved[0]); i++) { + reg = reg_saved[i]; + if (regs_allocated[reg]) { + if (reg >= 8) + g(0x41), reg-=8; + g(0x50 + reg); + } + } + + /* generate load code */ + for(i = 0; i < nb_operands; i++) { + op = &operands[i]; + if (op->reg >= 0) { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && + op->is_memory) { + /* memory reference case (for both input and + output cases) */ + SValue sv; + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; + sv.type.t = VT_PTR; + load(op->reg, &sv); + } else if (i >= nb_outputs || op->is_rw) { + /* load value in register */ + load(op->reg, op->vt); + if (op->is_llong) { + SValue sv; + sv = *op->vt; + sv.c.i += 4; + load(TREG_XDX, &sv); + } + } + } + } + } else { + /* generate save code */ + for(i = 0 ; i < nb_outputs; i++) { + op = &operands[i]; + if (op->reg >= 0) { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) { + if (!op->is_memory) { + SValue sv; + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL; + sv.type.t = VT_PTR; + load(out_reg, &sv); + + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | out_reg; + store(op->reg, &sv); + } + } else { + store(op->reg, op->vt); + if (op->is_llong) { + SValue sv; + sv = *op->vt; + sv.c.i += 4; + store(TREG_XDX, &sv); + } + } + } + } + /* generate reg restore code */ + for(i = sizeof(reg_saved)/sizeof(reg_saved[0]) - 1; i >= 0; i--) { + reg = reg_saved[i]; + if (regs_allocated[reg]) { + if (reg >= 8) + g(0x41), reg-=8; + g(0x58 + reg); + } + } + } +} + +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str) +{ + int reg; + TokenSym *ts; +#ifdef TCC_TARGET_X86_64 + unsigned int type; +#endif + + if (!strcmp(str, "memory") || + !strcmp(str, "cc") || + !strcmp(str, "flags")) + return; + ts = tok_alloc(str, strlen(str)); + reg = ts->tok; + if (reg >= TOK_ASM_eax && reg <= TOK_ASM_edi) { + reg -= TOK_ASM_eax; + } else if (reg >= TOK_ASM_ax && reg <= TOK_ASM_di) { + reg -= TOK_ASM_ax; +#ifdef TCC_TARGET_X86_64 + } else if (reg >= TOK_ASM_rax && reg <= TOK_ASM_rdi) { + reg -= TOK_ASM_rax; + } else if ((reg = asm_parse_numeric_reg(reg, &type)) >= 0) { + ; +#endif + } else { + tcc_error("invalid clobber register '%s'", str); + } + clobber_regs[reg] = 1; +} diff --git a/05/tcc-final-old/i386-asm.h b/05/tcc-final-old/i386-asm.h new file mode 100644 index 0000000..65d5179 --- /dev/null +++ b/05/tcc-final-old/i386-asm.h @@ -0,0 +1,480 @@ + DEF_ASM_OP0(clc, 0xf8) /* must be first OP0 */ + DEF_ASM_OP0(cld, 0xfc) + DEF_ASM_OP0(cli, 0xfa) + DEF_ASM_OP0(clts, 0x0f06) + DEF_ASM_OP0(cmc, 0xf5) + DEF_ASM_OP0(lahf, 0x9f) + DEF_ASM_OP0(sahf, 0x9e) + DEF_ASM_OP0(pusha, 0x60) + DEF_ASM_OP0(popa, 0x61) + DEF_ASM_OP0(pushfl, 0x9c) + DEF_ASM_OP0(popfl, 0x9d) + DEF_ASM_OP0(pushf, 0x9c) + DEF_ASM_OP0(popf, 0x9d) + DEF_ASM_OP0(stc, 0xf9) + DEF_ASM_OP0(std, 0xfd) + DEF_ASM_OP0(sti, 0xfb) + DEF_ASM_OP0(aaa, 0x37) + DEF_ASM_OP0(aas, 0x3f) + DEF_ASM_OP0(daa, 0x27) + DEF_ASM_OP0(das, 0x2f) + DEF_ASM_OP0(aad, 0xd50a) + DEF_ASM_OP0(aam, 0xd40a) + DEF_ASM_OP0(cbw, 0x6698) + DEF_ASM_OP0(cwd, 0x6699) + DEF_ASM_OP0(cwde, 0x98) + DEF_ASM_OP0(cdq, 0x99) + DEF_ASM_OP0(cbtw, 0x6698) + DEF_ASM_OP0(cwtl, 0x98) + DEF_ASM_OP0(cwtd, 0x6699) + DEF_ASM_OP0(cltd, 0x99) + DEF_ASM_OP0(int3, 0xcc) + DEF_ASM_OP0(into, 0xce) + DEF_ASM_OP0(iret, 0xcf) + DEF_ASM_OP0(rsm, 0x0faa) + DEF_ASM_OP0(hlt, 0xf4) + DEF_ASM_OP0(nop, 0x90) + DEF_ASM_OP0(pause, 0xf390) + DEF_ASM_OP0(xlat, 0xd7) + + /* strings */ +ALT(DEF_ASM_OP0L(cmpsb, 0xa6, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(scmpb, 0xa6, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(insb, 0x6c, 0, OPC_BWL)) +ALT(DEF_ASM_OP0L(outsb, 0x6e, 0, OPC_BWL)) + +ALT(DEF_ASM_OP0L(lodsb, 0xac, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(slodb, 0xac, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(movsb, 0xa4, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(smovb, 0xa4, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(scasb, 0xae, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(sscab, 0xae, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(stosb, 0xaa, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(sstob, 0xaa, 0, OPC_BWLX)) + + /* bits */ + +ALT(DEF_ASM_OP2(bsfw, 0x0fbc, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(bsrw, 0x0fbd, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) + +ALT(DEF_ASM_OP2(btw, 0x0fa3, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btw, 0x0fba, 4, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btsw, 0x0fab, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btsw, 0x0fba, 5, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btrw, 0x0fb3, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btrw, 0x0fba, 6, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btcw, 0x0fbb, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btcw, 0x0fba, 7, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + + /* prefixes */ + DEF_ASM_OP0(wait, 0x9b) + DEF_ASM_OP0(fwait, 0x9b) + DEF_ASM_OP0(aword, 0x67) + DEF_ASM_OP0(addr16, 0x67) + ALT(DEF_ASM_OP0(word, 0x66)) + DEF_ASM_OP0(data16, 0x66) + DEF_ASM_OP0(lock, 0xf0) + DEF_ASM_OP0(rep, 0xf3) + DEF_ASM_OP0(repe, 0xf3) + DEF_ASM_OP0(repz, 0xf3) + DEF_ASM_OP0(repne, 0xf2) + DEF_ASM_OP0(repnz, 0xf2) + + DEF_ASM_OP0(invd, 0x0f08) + DEF_ASM_OP0(wbinvd, 0x0f09) + DEF_ASM_OP0(cpuid, 0x0fa2) + DEF_ASM_OP0(wrmsr, 0x0f30) + DEF_ASM_OP0(rdtsc, 0x0f31) + DEF_ASM_OP0(rdmsr, 0x0f32) + DEF_ASM_OP0(rdpmc, 0x0f33) + DEF_ASM_OP0(ud2, 0x0f0b) + + /* NOTE: we took the same order as gas opcode definition order */ +ALT(DEF_ASM_OP2(movb, 0xa0, 0, OPC_BWLX, OPT_ADDR, OPT_EAX)) +ALT(DEF_ASM_OP2(movb, 0xa2, 0, OPC_BWLX, OPT_EAX, OPT_ADDR)) +ALT(DEF_ASM_OP2(movb, 0x88, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0x8a, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0xb0, 0, OPC_REG | OPC_BWLX, OPT_IM, OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0xc6, 0, OPC_MODRM | OPC_BWLX, OPT_IM, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP2(movw, 0x8c, 0, OPC_MODRM | OPC_WLX, OPT_SEG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(movw, 0x8e, 0, OPC_MODRM | OPC_WLX, OPT_EA | OPT_REG, OPT_SEG)) + +ALT(DEF_ASM_OP2(movw, 0x0f20, 0, OPC_MODRM | OPC_WLX, OPT_CR, OPT_REG32)) +ALT(DEF_ASM_OP2(movw, 0x0f21, 0, OPC_MODRM | OPC_WLX, OPT_DB, OPT_REG32)) +ALT(DEF_ASM_OP2(movw, 0x0f24, 0, OPC_MODRM | OPC_WLX, OPT_TR, OPT_REG32)) +ALT(DEF_ASM_OP2(movw, 0x0f22, 0, OPC_MODRM | OPC_WLX, OPT_REG32, OPT_CR)) +ALT(DEF_ASM_OP2(movw, 0x0f23, 0, OPC_MODRM | OPC_WLX, OPT_REG32, OPT_DB)) +ALT(DEF_ASM_OP2(movw, 0x0f26, 0, OPC_MODRM | OPC_WLX, OPT_REG32, OPT_TR)) + +ALT(DEF_ASM_OP2(movsbl, 0x0fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movsbw, 0x660fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG16)) +ALT(DEF_ASM_OP2(movswl, 0x0fbf, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movzbw, 0x0fb6, 0, OPC_MODRM | OPC_WLX, OPT_REG8 | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(movzwl, 0x0fb7, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32)) + +ALT(DEF_ASM_OP1(pushw, 0x50, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(pushw, 0xff, 6, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP1(pushw, 0x6a, 0, OPC_WLX, OPT_IM8S)) +ALT(DEF_ASM_OP1(pushw, 0x68, 0, OPC_WLX, OPT_IM32)) +ALT(DEF_ASM_OP1(pushw, 0x06, 0, OPC_WLX, OPT_SEG)) + +ALT(DEF_ASM_OP1(popw, 0x58, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(popw, 0x8f, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP1(popw, 0x07, 0, OPC_WLX, OPT_SEG)) + +ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WLX, OPT_REGW, OPT_EAX)) +ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WLX, OPT_EAX, OPT_REGW)) +ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) + +ALT(DEF_ASM_OP2(inb, 0xe4, 0, OPC_BWL, OPT_IM8, OPT_EAX)) +ALT(DEF_ASM_OP1(inb, 0xe4, 0, OPC_BWL, OPT_IM8)) +ALT(DEF_ASM_OP2(inb, 0xec, 0, OPC_BWL, OPT_DX, OPT_EAX)) +ALT(DEF_ASM_OP1(inb, 0xec, 0, OPC_BWL, OPT_DX)) + +ALT(DEF_ASM_OP2(outb, 0xe6, 0, OPC_BWL, OPT_EAX, OPT_IM8)) +ALT(DEF_ASM_OP1(outb, 0xe6, 0, OPC_BWL, OPT_IM8)) +ALT(DEF_ASM_OP2(outb, 0xee, 0, OPC_BWL, OPT_EAX, OPT_DX)) +ALT(DEF_ASM_OP1(outb, 0xee, 0, OPC_BWL, OPT_DX)) + +ALT(DEF_ASM_OP2(leaw, 0x8d, 0, OPC_MODRM | OPC_WLX, OPT_EA, OPT_REG)) + +ALT(DEF_ASM_OP2(les, 0xc4, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lds, 0xc5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lss, 0x0fb2, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lfs, 0x0fb4, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lgs, 0x0fb5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) + + /* arith */ +ALT(DEF_ASM_OP2(addb, 0x00, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) /* XXX: use D bit ? */ +ALT(DEF_ASM_OP2(addb, 0x02, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(addb, 0x04, 0, OPC_ARITH | OPC_BWLX, OPT_IM, OPT_EAX)) +ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(addb, 0x80, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(testb, 0xa8, 0, OPC_BWLX, OPT_IM, OPT_EAX)) +ALT(DEF_ASM_OP2(testb, 0xf6, 0, OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP1(incw, 0x40, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(incb, 0xfe, 0, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(decw, 0x48, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(decb, 0xfe, 1, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP1(notb, 0xf6, 2, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(negb, 0xf6, 3, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP1(mulb, 0xf6, 4, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(imulb, 0xf6, 5, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP2(imulw, 0x0faf, 0, OPC_MODRM | OPC_WLX, OPT_REG | OPT_EA, OPT_REG)) +ALT(DEF_ASM_OP3(imulw, 0x6b, 0, OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(imulw, 0x6b, 0, OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_REGW)) +ALT(DEF_ASM_OP3(imulw, 0x69, 0, OPC_MODRM | OPC_WLX, OPT_IMW, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(imulw, 0x69, 0, OPC_MODRM | OPC_WLX, OPT_IMW, OPT_REGW)) + +ALT(DEF_ASM_OP1(divb, 0xf6, 6, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP2(divb, 0xf6, 6, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA, OPT_EAX)) +ALT(DEF_ASM_OP1(idivb, 0xf6, 7, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP2(idivb, 0xf6, 7, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA, OPT_EAX)) + + /* shifts */ +ALT(DEF_ASM_OP2(rolb, 0xc0, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_IM8, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(rolb, 0xd2, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_CL, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP1(rolb, 0xd0, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP3(shldw, 0x0fa4, 0, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WLX, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shrdw, 0x0fac, 0, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WLX, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_EA | OPT_REGW)) + +ALT(DEF_ASM_OP1(call, 0xff, 2, OPC_MODRM, OPT_INDIR)) +ALT(DEF_ASM_OP1(call, 0xe8, 0, 0, OPT_DISP)) +ALT(DEF_ASM_OP1(jmp, 0xff, 4, OPC_MODRM, OPT_INDIR)) +ALT(DEF_ASM_OP1(jmp, 0xeb, 0, 0, OPT_DISP8)) + +ALT(DEF_ASM_OP2(lcall, 0x9a, 0, 0, OPT_IM16, OPT_IM32)) +ALT(DEF_ASM_OP1(lcall, 0xff, 3, OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP2(ljmp, 0xea, 0, 0, OPT_IM16, OPT_IM32)) +ALT(DEF_ASM_OP1(ljmp, 0xff, 5, OPC_MODRM, OPT_EA)) + +ALT(DEF_ASM_OP1(int, 0xcd, 0, 0, OPT_IM8)) +ALT(DEF_ASM_OP1(seto, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA)) +ALT(DEF_ASM_OP1(setob, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA)) + DEF_ASM_OP2(enter, 0xc8, 0, 0, OPT_IM16, OPT_IM8) + DEF_ASM_OP0(leave, 0xc9) + DEF_ASM_OP0(ret, 0xc3) + DEF_ASM_OP0(retl,0xc3) +ALT(DEF_ASM_OP1(retl,0xc2, 0, 0, OPT_IM16)) +ALT(DEF_ASM_OP1(ret, 0xc2, 0, 0, OPT_IM16)) + DEF_ASM_OP0(lret, 0xcb) +ALT(DEF_ASM_OP1(lret, 0xca, 0, 0, OPT_IM16)) + +ALT(DEF_ASM_OP1(jo, 0x70, 0, OPC_TEST, OPT_DISP8)) + DEF_ASM_OP1(loopne, 0xe0, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loopnz, 0xe0, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loope, 0xe1, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loopz, 0xe1, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loop, 0xe2, 0, 0, OPT_DISP8) + DEF_ASM_OP1(jecxz, 0xe3, 0, 0, OPT_DISP8) + + /* float */ + /* specific fcomp handling */ +ALT(DEF_ASM_OP0L(fcomp, 0xd8d9, 0, 0)) + +ALT(DEF_ASM_OP1(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST)) +ALT(DEF_ASM_OP2(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0)) +ALT(DEF_ASM_OP2(fadd, 0xdcc0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP2(fmul, 0xdcc8, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP0L(fadd, 0xdec1, 0, OPC_FARITH)) +ALT(DEF_ASM_OP1(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST)) +ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0)) +ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP0L(faddp, 0xdec1, 0, OPC_FARITH)) +ALT(DEF_ASM_OP1(fadds, 0xd8, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(fiaddl, 0xda, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(faddl, 0xdc, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(fiadds, 0xde, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) + + DEF_ASM_OP0(fucompp, 0xdae9) + DEF_ASM_OP0(ftst, 0xd9e4) + DEF_ASM_OP0(fxam, 0xd9e5) + DEF_ASM_OP0(fld1, 0xd9e8) + DEF_ASM_OP0(fldl2t, 0xd9e9) + DEF_ASM_OP0(fldl2e, 0xd9ea) + DEF_ASM_OP0(fldpi, 0xd9eb) + DEF_ASM_OP0(fldlg2, 0xd9ec) + DEF_ASM_OP0(fldln2, 0xd9ed) + DEF_ASM_OP0(fldz, 0xd9ee) + + DEF_ASM_OP0(f2xm1, 0xd9f0) + DEF_ASM_OP0(fyl2x, 0xd9f1) + DEF_ASM_OP0(fptan, 0xd9f2) + DEF_ASM_OP0(fpatan, 0xd9f3) + DEF_ASM_OP0(fxtract, 0xd9f4) + DEF_ASM_OP0(fprem1, 0xd9f5) + DEF_ASM_OP0(fdecstp, 0xd9f6) + DEF_ASM_OP0(fincstp, 0xd9f7) + DEF_ASM_OP0(fprem, 0xd9f8) + DEF_ASM_OP0(fyl2xp1, 0xd9f9) + DEF_ASM_OP0(fsqrt, 0xd9fa) + DEF_ASM_OP0(fsincos, 0xd9fb) + DEF_ASM_OP0(frndint, 0xd9fc) + DEF_ASM_OP0(fscale, 0xd9fd) + DEF_ASM_OP0(fsin, 0xd9fe) + DEF_ASM_OP0(fcos, 0xd9ff) + DEF_ASM_OP0(fchs, 0xd9e0) + DEF_ASM_OP0(fabs, 0xd9e1) + DEF_ASM_OP0(fninit, 0xdbe3) + DEF_ASM_OP0(fnclex, 0xdbe2) + DEF_ASM_OP0(fnop, 0xd9d0) + + /* fp load */ + DEF_ASM_OP1(fld, 0xd9c0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fldl, 0xd9c0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(flds, 0xd9, 0, OPC_MODRM, OPT_EA) +ALT(DEF_ASM_OP1(fldl, 0xdd, 0, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(fildl, 0xdb, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fildq, 0xdf, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fildll, 0xdf, 5, OPC_MODRM,OPT_EA) + DEF_ASM_OP1(fldt, 0xdb, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fbld, 0xdf, 4, OPC_MODRM, OPT_EA) + + /* fp store */ + DEF_ASM_OP1(fst, 0xddd0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fstl, 0xddd0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fsts, 0xd9, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fstps, 0xd9, 3, OPC_MODRM, OPT_EA) +ALT(DEF_ASM_OP1(fstl, 0xdd, 2, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(fstpl, 0xdd, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fist, 0xdf, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistp, 0xdf, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistl, 0xdb, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistpl, 0xdb, 3, OPC_MODRM, OPT_EA) + + DEF_ASM_OP1(fstp, 0xddd8, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fistpq, 0xdf, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistpll, 0xdf, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fstpt, 0xdb, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fbstp, 0xdf, 6, OPC_MODRM, OPT_EA) + + /* exchange */ + DEF_ASM_OP0(fxch, 0xd9c9) +ALT(DEF_ASM_OP1(fxch, 0xd9c8, 0, OPC_REG, OPT_ST)) + + /* misc FPU */ + DEF_ASM_OP1(fucom, 0xdde0, 0, OPC_REG, OPT_ST ) + DEF_ASM_OP1(fucomp, 0xdde8, 0, OPC_REG, OPT_ST ) + + DEF_ASM_OP0L(finit, 0xdbe3, 0, OPC_FWAIT) + DEF_ASM_OP1(fldcw, 0xd9, 5, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fnstcw, 0xd9, 7, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fstcw, 0xd9, 7, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP0(fnstsw, 0xdfe0) +ALT(DEF_ASM_OP1(fnstsw, 0xdfe0, 0, 0, OPT_EAX )) +ALT(DEF_ASM_OP1(fnstsw, 0xdd, 7, OPC_MODRM, OPT_EA )) + DEF_ASM_OP1(fstsw, 0xdfe0, 0, OPC_FWAIT, OPT_EAX ) +ALT(DEF_ASM_OP0L(fstsw, 0xdfe0, 0, OPC_FWAIT)) +ALT(DEF_ASM_OP1(fstsw, 0xdd, 7, OPC_MODRM | OPC_FWAIT, OPT_EA )) + DEF_ASM_OP0L(fclex, 0xdbe2, 0, OPC_FWAIT) + DEF_ASM_OP1(fnstenv, 0xd9, 6, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fstenv, 0xd9, 6, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP1(fldenv, 0xd9, 4, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fnsave, 0xdd, 6, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fsave, 0xdd, 6, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP1(frstor, 0xdd, 4, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(ffree, 0xddc0, 4, OPC_REG, OPT_ST ) + DEF_ASM_OP1(ffreep, 0xdfc0, 4, OPC_REG, OPT_ST ) + DEF_ASM_OP1(fxsave, 0x0fae, 0, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fxrstor, 0x0fae, 1, OPC_MODRM, OPT_EA ) + + /* segments */ + DEF_ASM_OP2(arpl, 0x63, 0, OPC_MODRM, OPT_REG16, OPT_REG16 | OPT_EA) +ALT(DEF_ASM_OP2(larw, 0x0f02, 0, OPC_MODRM | OPC_WLX, OPT_REG | OPT_EA, OPT_REG)) + DEF_ASM_OP1(lgdt, 0x0f01, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lidt, 0x0f01, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lldt, 0x0f00, 2, OPC_MODRM, OPT_EA | OPT_REG) + DEF_ASM_OP1(lmsw, 0x0f01, 6, OPC_MODRM, OPT_EA | OPT_REG) +ALT(DEF_ASM_OP2(lslw, 0x0f03, 0, OPC_MODRM | OPC_WLX, OPT_EA | OPT_REG, OPT_REG)) + DEF_ASM_OP1(ltr, 0x0f00, 3, OPC_MODRM, OPT_EA | OPT_REG) + DEF_ASM_OP1(sgdt, 0x0f01, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sidt, 0x0f01, 1, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sldt, 0x0f00, 0, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(smsw, 0x0f01, 4, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(str, 0x0f00, 1, OPC_MODRM, OPT_REG16| OPT_EA) + DEF_ASM_OP1(verr, 0x0f00, 4, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(verw, 0x0f00, 5, OPC_MODRM, OPT_REG | OPT_EA) + + /* 486 */ + DEF_ASM_OP1(bswap, 0x0fc8, 0, OPC_REG, OPT_REG32 ) +ALT(DEF_ASM_OP2(xaddb, 0x0fc0, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_REG | OPT_EA )) +ALT(DEF_ASM_OP2(cmpxchgb, 0x0fb0, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_REG | OPT_EA )) + DEF_ASM_OP1(invlpg, 0x0f01, 7, OPC_MODRM, OPT_EA ) + + DEF_ASM_OP2(boundl, 0x62, 0, OPC_MODRM, OPT_REG32, OPT_EA) + DEF_ASM_OP2(boundw, 0x6662, 0, OPC_MODRM, OPT_REG16, OPT_EA) + + /* pentium */ + DEF_ASM_OP1(cmpxchg8b, 0x0fc7, 1, OPC_MODRM, OPT_EA ) + + /* pentium pro */ +ALT(DEF_ASM_OP2(cmovo, 0x0f40, 0, OPC_MODRM | OPC_TEST | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) + DEF_ASM_OP2(fcmovb, 0xdac0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmove, 0xdac8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovbe, 0xdad0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovu, 0xdad8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnb, 0xdbc0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovne, 0xdbc8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnbe, 0xdbd0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnu, 0xdbd8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + + DEF_ASM_OP2(fucomi, 0xdbe8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcomi, 0xdbf0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fucomip, 0xdfe8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcomip, 0xdff0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + + /* mmx */ + DEF_ASM_OP0(emms, 0x0f77) /* must be last OP0 */ + DEF_ASM_OP2(movd, 0x0f6e, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_MMXSSE ) + DEF_ASM_OP2(movq, 0x0f6f, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) +ALT(DEF_ASM_OP2(movd, 0x0f7e, 0, OPC_MODRM, OPT_MMXSSE, OPT_EA | OPT_REG32 )) +ALT(DEF_ASM_OP2(movq, 0x0f7f, 0, OPC_MODRM, OPT_MMX, OPT_EA | OPT_MMX )) +ALT(DEF_ASM_OP2(movq, 0x660fd6, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_SSE )) +ALT(DEF_ASM_OP2(movq, 0xf30f7e, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE )) + + DEF_ASM_OP2(packssdw, 0x0f6b, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(packsswb, 0x0f63, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(packuswb, 0x0f67, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddb, 0x0ffc, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddw, 0x0ffd, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddd, 0x0ffe, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddsb, 0x0fec, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddsw, 0x0fed, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddusb, 0x0fdc, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddusw, 0x0fdd, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pand, 0x0fdb, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pandn, 0x0fdf, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqb, 0x0f74, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqw, 0x0f75, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqd, 0x0f76, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtb, 0x0f64, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtw, 0x0f65, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtd, 0x0f66, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmaddwd, 0x0ff5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmulhw, 0x0fe5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmullw, 0x0fd5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(por, 0x0feb, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psllw, 0x0ff1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psllw, 0x0f71, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(pslld, 0x0ff2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(pslld, 0x0f72, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psllq, 0x0ff3, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psllq, 0x0f73, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psraw, 0x0fe1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psraw, 0x0f71, 4, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrad, 0x0fe2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrad, 0x0f72, 4, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrlw, 0x0fd1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrlw, 0x0f71, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrld, 0x0fd2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrld, 0x0f72, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrlq, 0x0fd3, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psubb, 0x0ff8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubw, 0x0ff9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubd, 0x0ffa, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubsb, 0x0fe8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubsw, 0x0fe9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubusb, 0x0fd8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubusw, 0x0fd9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhbw, 0x0f68, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhwd, 0x0f69, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhdq, 0x0f6a, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpcklbw, 0x0f60, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpcklwd, 0x0f61, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckldq, 0x0f62, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + + /* sse */ + DEF_ASM_OP2(movups, 0x0f10, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movups, 0x0f11, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(movaps, 0x0f28, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movaps, 0x0f29, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(movhps, 0x0f16, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movhps, 0x0f17, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(addps, 0x0f58, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(cvtpi2ps, 0x0f2a, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_SSE ) + DEF_ASM_OP2(cvtps2pi, 0x0f2d, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_MMX ) + DEF_ASM_OP2(cvttps2pi, 0x0f2c, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_MMX ) + DEF_ASM_OP2(divps, 0x0f5e, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(maxps, 0x0f5f, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(minps, 0x0f5d, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(mulps, 0x0f59, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pavgb, 0x0fe0, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pavgw, 0x0fe3, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pmaxsw, 0x0fee, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmaxub, 0x0fde, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pminsw, 0x0fea, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pminub, 0x0fda, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(rcpss, 0x0f53, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(rsqrtps, 0x0f52, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(sqrtps, 0x0f51, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(subps, 0x0f5c, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + +#undef ALT +#undef DEF_ASM_OP0 +#undef DEF_ASM_OP0L +#undef DEF_ASM_OP1 +#undef DEF_ASM_OP2 +#undef DEF_ASM_OP3 diff --git a/05/tcc-final-old/i386-gen.c b/05/tcc-final-old/i386-gen.c new file mode 100644 index 0000000..b9c3599 --- /dev/null +++ b/05/tcc-final-old/i386-gen.c @@ -0,0 +1,1164 @@ +/* + * X86 code generator for TCC + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +/* number of available registers */ +#define NB_REGS 5 +#define NB_ASM_REGS 8 +#define CONFIG_TCC_ASM + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_EAX 0x0004 +#define RC_ST0 0x0008 +#define RC_ECX 0x0010 +#define RC_EDX 0x0020 +#define RC_EBX 0x0040 + +#define RC_IRET RC_EAX /* function return: integer register */ +#define RC_LRET RC_EDX /* function return: second integer register */ +#define RC_FRET RC_ST0 /* function return: float register */ + +/* pretty names for the registers */ +enum { + TREG_EAX = 0, + TREG_ECX, + TREG_EDX, + TREG_EBX, + TREG_ST0, + TREG_ESP = 4 +}; + +/* return registers for function */ +#define REG_IRET TREG_EAX /* single word int return register */ +#define REG_LRET TREG_EDX /* second word return register (for long long) */ +#define REG_FRET TREG_ST0 /* float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +#define INVERT_FUNC_PARAMS + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 12 +#define LDOUBLE_ALIGN 4 +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 8 + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#include "tcc.h" + +/* define to 1/0 to [not] have EBX as 4th register */ +#define USE_EBX 0 + +ST_DATA const int reg_classes[NB_REGS] = { + /* eax */ RC_INT | RC_EAX, + /* ecx */ RC_INT | RC_ECX, + /* edx */ RC_INT | RC_EDX, + /* ebx */ (RC_INT | RC_EBX) * USE_EBX, + /* st0 */ RC_FLOAT | RC_ST0, +}; + +static unsigned long func_sub_sp_offset; +static int func_ret_sub; +#ifdef CONFIG_TCC_BCHECK +static addr_t func_bound_offset; +static unsigned long func_bound_ind; +#endif + +/* XXX: make it faster ? */ +ST_FUNC void g(int c) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 1; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; + ind = ind1; +} + +ST_FUNC void o(unsigned int c) +{ + while (c) { + g(c); + c = c >> 8; + } +} + +ST_FUNC void gen_le16(int v) +{ + g(v); + g(v >> 8); +} + +ST_FUNC void gen_le32(int c) +{ + g(c); + g(c >> 8); + g(c >> 16); + g(c >> 24); +} + +/* output a symbol and patch all calls to it */ +ST_FUNC void gsym_addr(int t, int a) +{ + while (t) { + unsigned char *ptr = cur_text_section->data + t; + uint32_t n = read32le(ptr); /* next value */ + write32le(ptr, a - t - 4); + t = n; + } +} + +ST_FUNC void gsym(int t) +{ + gsym_addr(t, ind); +} + +/* instruction + 4 bytes data. Return the address of the data */ +static int oad(int c, int s) +{ + int t; + if (nocode_wanted) + return s; + o(c); + t = ind; + gen_le32(s); + return t; +} + +/* generate jmp to a label */ +#define gjmp2(instr,lbl) oad(instr,lbl) + +/* output constant with relocation if 'r & VT_SYM' is true */ +ST_FUNC void gen_addr32(int r, Sym *sym, int c) +{ + if (r & VT_SYM) + greloc(cur_text_section, sym, ind, R_386_32); + gen_le32(c); +} + +ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) +{ + if (r & VT_SYM) + greloc(cur_text_section, sym, ind, R_386_PC32); + gen_le32(c - 4); +} + +/* generate a modrm reference. 'op_reg' contains the additional 3 + opcode bits */ +static void gen_modrm(int op_reg, int r, Sym *sym, int c) +{ + op_reg = op_reg << 3; + if ((r & VT_VALMASK) == VT_CONST) { + /* constant memory reference */ + o(0x05 | op_reg); + gen_addr32(r, sym, c); + } else if ((r & VT_VALMASK) == VT_LOCAL) { + /* currently, we use only ebp as base */ + if (c == (char)c) { + /* short reference */ + o(0x45 | op_reg); + g(c); + } else { + oad(0x85 | op_reg, c); + } + } else { + g(0x00 | op_reg | (r & VT_VALMASK)); + } +} + +/* load 'r' from value 'sv' */ +ST_FUNC void load(int r, SValue *sv) +{ + int v, t, ft, fc, fr; + SValue v1; + +#ifdef TCC_TARGET_PE + SValue v2; + sv = pe_getimport(sv, &v2); +#endif + + fr = sv->r; + ft = sv->type.t & ~VT_DEFSIGN; + fc = sv->c.i; + + ft &= ~(VT_VOLATILE | VT_CONSTANT); + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + if (v == VT_LLOCAL) { + v1.type.t = VT_INT; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + fr = r; + if (!(reg_classes[fr] & RC_INT)) + fr = get_reg(RC_INT); + load(fr, &v1); + } + if ((ft & VT_BTYPE) == VT_FLOAT) { + o(0xd9); /* flds */ + r = 0; + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + o(0xdd); /* fldl */ + r = 0; + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + o(0xdb); /* fldt */ + r = 5; + } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) { + o(0xbe0f); /* movsbl */ + } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) { + o(0xb60f); /* movzbl */ + } else if ((ft & VT_TYPE) == VT_SHORT) { + o(0xbf0f); /* movswl */ + } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) { + o(0xb70f); /* movzwl */ + } else { + o(0x8b); /* movl */ + } + gen_modrm(r, fr, sv->sym, fc); + } else { + if (v == VT_CONST) { + o(0xb8 + r); /* mov $xx, r */ + gen_addr32(fr, sv->sym, fc); + } else if (v == VT_LOCAL) { + if (fc) { + o(0x8d); /* lea xxx(%ebp), r */ + gen_modrm(r, VT_LOCAL, sv->sym, fc); + } else { + o(0x89); + o(0xe8 + r); /* mov %ebp, r */ + } + } else if (v == VT_CMP) { + oad(0xb8 + r, 0); /* mov $0, r */ + o(0x0f); /* setxx %br */ + o(fc); + o(0xc0 + r); + } else if (v == VT_JMP || v == VT_JMPI) { + t = v & 1; + oad(0xb8 + r, t); /* mov $1, r */ + o(0x05eb); /* jmp after */ + gsym(fc); + oad(0xb8 + r, t ^ 1); /* mov $0, r */ + } else if (v != r) { + o(0x89); + o(0xc0 + r + v * 8); /* mov v, r */ + } + } +} + +/* store register 'r' in lvalue 'v' */ +ST_FUNC void store(int r, SValue *v) +{ + int fr, bt, ft, fc; + +#ifdef TCC_TARGET_PE + SValue v2; + v = pe_getimport(v, &v2); +#endif + + ft = v->type.t; + fc = v->c.i; + fr = v->r & VT_VALMASK; + ft &= ~(VT_VOLATILE | VT_CONSTANT); + bt = ft & VT_BTYPE; + /* XXX: incorrect if float reg to reg */ + if (bt == VT_FLOAT) { + o(0xd9); /* fsts */ + r = 2; + } else if (bt == VT_DOUBLE) { + o(0xdd); /* fstpl */ + r = 2; + } else if (bt == VT_LDOUBLE) { + o(0xc0d9); /* fld %st(0) */ + o(0xdb); /* fstpt */ + r = 7; + } else { + if (bt == VT_SHORT) + o(0x66); + if (bt == VT_BYTE || bt == VT_BOOL) + o(0x88); + else + o(0x89); + } + if (fr == VT_CONST || + fr == VT_LOCAL || + (v->r & VT_LVAL)) { + gen_modrm(r, v->r, v->sym, fc); + } else if (fr != r) { + o(0xc0 + fr + r * 8); /* mov r, fr */ + } +} + +static void gadd_sp(int val) +{ + if (val == (char)val) { + o(0xc483); + g(val); + } else { + oad(0xc481, val); /* add $xxx, %esp */ + } +} + +#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE +static void gen_static_call(int v) +{ + Sym *sym; + + sym = external_global_sym(v, &func_old_type, 0); + oad(0xe8, -4); + greloc(cur_text_section, sym, ind-4, R_386_PC32); +} +#endif + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) { + /* constant and relocation case */ + greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32); + oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */ + } else { + /* otherwise, indirect call */ + r = gv(RC_INT); + o(0xff); /* call/jmp *r */ + o(0xd0 + r + (is_jmp << 4)); + } + if (!is_jmp) { + int rt; + /* extend the return value to the whole register if necessary + visual studio and gcc do not always set the whole eax register + when assigning the return value of a function */ + rt = vtop->type.ref->type.t; + switch (rt & VT_BTYPE) { + case VT_BYTE: + if (rt & VT_UNSIGNED) { + o(0xc0b60f); /* movzx %al, %eax */ + } + else { + o(0xc0be0f); /* movsx %al, %eax */ + } + break; + case VT_SHORT: + if (rt & VT_UNSIGNED) { + o(0xc0b70f); /* movzx %ax, %eax */ + } + else { + o(0xc0bf0f); /* movsx %ax, %eax */ + } + break; + default: + break; + } + } +} + +static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX }; +static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) +{ +#ifdef TCC_TARGET_PE + int size, align; + *ret_align = 1; // Never have to re-align return values for x86 + *regsize = 4; + size = type_size(vt, &align); + if (size > 8 || (size & (size - 1))) + return 0; + if (size == 8) + ret->t = VT_LLONG; + else if (size == 4) + ret->t = VT_INT; + else if (size == 2) + ret->t = VT_SHORT; + else + ret->t = VT_BYTE; + ret->ref = NULL; + return 1; +#else + *ret_align = 1; // Never have to re-align return values for x86 + return 0; +#endif +} + +/* Generate function call. The function address is pushed first, then + all the parameters in call order. This functions pops all the + parameters and the function address. */ +ST_FUNC void gfunc_call(int nb_args) +{ + int size, align, r, args_size, i, func_call; + Sym *func_sym; + + args_size = 0; + for(i = 0;i < nb_args; i++) { + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + size = type_size(&vtop->type, &align); + /* align to stack align size */ + size = (size + 3) & ~3; + /* allocate the necessary size on stack */ + oad(0xec81, size); /* sub $xxx, %esp */ + /* generate structure store */ + r = get_reg(RC_INT); + o(0x89); /* mov %esp, r */ + o(0xe0 + r); + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + args_size += size; + } else if (is_float(vtop->type.t)) { + gv(RC_FLOAT); /* only one float register */ + if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) + size = 8; + else + size = 12; + oad(0xec81, size); /* sub $xxx, %esp */ + if (size == 12) + o(0x7cdb); + else + o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */ + g(0x24); + g(0x00); + args_size += size; + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + r = gv(RC_INT); + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + size = 8; + o(0x50 + vtop->r2); /* push r */ + } else { + size = 4; + } + o(0x50 + r); /* push r */ + args_size += size; + } + vtop--; + } + save_regs(0); /* save used temporary registers */ + func_sym = vtop->type.ref; + func_call = func_sym->f.func_call; + /* fast call case */ + if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) || + func_call == FUNC_FASTCALLW) { + int fastcall_nb_regs; + uint8_t *fastcall_regs_ptr; + if (func_call == FUNC_FASTCALLW) { + fastcall_regs_ptr = fastcallw_regs; + fastcall_nb_regs = 2; + } else { + fastcall_regs_ptr = fastcall_regs; + fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1; + } + for(i = 0;i < fastcall_nb_regs; i++) { + if (args_size <= 0) + break; + o(0x58 + fastcall_regs_ptr[i]); /* pop r */ + /* XXX: incorrect for struct/floats */ + args_size -= 4; + } + } +#ifndef TCC_TARGET_PE + else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT) + args_size -= 4; +#endif + gcall_or_jmp(0); + + if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW) + gadd_sp(args_size); + vtop--; +} + +#ifdef TCC_TARGET_PE +#define FUNC_PROLOG_SIZE (10 + USE_EBX) +#else +#define FUNC_PROLOG_SIZE (9 + USE_EBX) +#endif + +/* generate function prolog of type 't' */ +ST_FUNC void gfunc_prolog(CType *func_type) +{ + int addr, align, size, func_call, fastcall_nb_regs; + int param_index, param_addr; + uint8_t *fastcall_regs_ptr; + Sym *sym; + CType *type; + + sym = func_type->ref; + func_call = sym->f.func_call; + addr = 8; + loc = 0; + func_vc = 0; + + if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) { + fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1; + fastcall_regs_ptr = fastcall_regs; + } else if (func_call == FUNC_FASTCALLW) { + fastcall_nb_regs = 2; + fastcall_regs_ptr = fastcallw_regs; + } else { + fastcall_nb_regs = 0; + fastcall_regs_ptr = NULL; + } + param_index = 0; + + ind += FUNC_PROLOG_SIZE; + func_sub_sp_offset = ind; + /* if the function returns a structure, then add an + implicit pointer parameter */ + func_vt = sym->type; + func_var = (sym->f.func_type == FUNC_ELLIPSIS); +#ifdef TCC_TARGET_PE + size = type_size(&func_vt,&align); + if (((func_vt.t & VT_BTYPE) == VT_STRUCT) + && (size > 8 || (size & (size - 1)))) { +#else + if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { +#endif + /* XXX: fastcall case ? */ + func_vc = addr; + addr += 4; + param_index++; + } + /* define parameters */ + while ((sym = sym->next) != NULL) { + type = &sym->type; + size = type_size(type, &align); + size = (size + 3) & ~3; +#ifdef FUNC_STRUCT_PARAM_AS_PTR + /* structs are passed as pointer */ + if ((type->t & VT_BTYPE) == VT_STRUCT) { + size = 4; + } +#endif + if (param_index < fastcall_nb_regs) { + /* save FASTCALL register */ + loc -= 4; + o(0x89); /* movl */ + gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc); + param_addr = loc; + } else { + param_addr = addr; + addr += size; + } + sym_push(sym->v & ~SYM_FIELD, type, + VT_LOCAL | lvalue_type(type->t), param_addr); + param_index++; + } + func_ret_sub = 0; + /* pascal type call or fastcall ? */ + if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW) + func_ret_sub = addr - 8; +#ifndef TCC_TARGET_PE + else if (func_vc) + func_ret_sub = 4; +#endif + +#ifdef CONFIG_TCC_BCHECK + /* leave some room for bound checking code */ + if (tcc_state->do_bounds_check) { + func_bound_offset = lbounds_section->data_offset; + func_bound_ind = ind; + oad(0xb8, 0); /* lbound section pointer */ + oad(0xb8, 0); /* call to function */ + } +#endif +} + +/* generate function epilog */ +ST_FUNC void gfunc_epilog(void) +{ + addr_t v, saved_ind; + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check + && func_bound_offset != lbounds_section->data_offset) { + addr_t saved_ind; + addr_t *bounds_ptr; + Sym *sym_data; + + /* add end of table info */ + bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t)); + *bounds_ptr = 0; + + /* generate bound local allocation */ + saved_ind = ind; + ind = func_bound_ind; + sym_data = get_sym_ref(&char_pointer_type, lbounds_section, + func_bound_offset, lbounds_section->data_offset); + greloc(cur_text_section, sym_data, + ind + 1, R_386_32); + oad(0xb8, 0); /* mov %eax, xxx */ + gen_static_call(TOK___bound_local_new); + ind = saved_ind; + + /* generate bound check local freeing */ + o(0x5250); /* save returned value, if any */ + greloc(cur_text_section, sym_data, ind + 1, R_386_32); + oad(0xb8, 0); /* mov %eax, xxx */ + gen_static_call(TOK___bound_local_delete); + o(0x585a); /* restore returned value, if any */ + } +#endif + + /* align local size to word & save local variables */ + v = (-loc + 3) & -4; + +#if USE_EBX + o(0x8b); + gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4)); +#endif + + o(0xc9); /* leave */ + if (func_ret_sub == 0) { + o(0xc3); /* ret */ + } else { + o(0xc2); /* ret n */ + g(func_ret_sub); + g(func_ret_sub >> 8); + } + saved_ind = ind; + ind = func_sub_sp_offset - FUNC_PROLOG_SIZE; +#ifdef TCC_TARGET_PE + if (v >= 4096) { + oad(0xb8, v); /* mov stacksize, %eax */ + gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */ + } else +#endif + { + o(0xe58955); /* push %ebp, mov %esp, %ebp */ + o(0xec81); /* sub esp, stacksize */ + gen_le32(v); +#ifdef TCC_TARGET_PE + o(0x90); /* adjust to FUNC_PROLOG_SIZE */ +#endif + } + o(0x53 * USE_EBX); /* push ebx */ + ind = saved_ind; +} + +/* generate a jump to a label */ +ST_FUNC int gjmp(int t) +{ + return gjmp2(0xe9, t); +} + +/* generate a jump to a fixed address */ +ST_FUNC void gjmp_addr(int a) +{ + int r; + r = a - ind - 2; + if (r == (char)r) { + g(0xeb); + g(r); + } else { + oad(0xe9, a - ind - 5); + } +} + +ST_FUNC void gtst_addr(int inv, int a) +{ + int v = vtop->r & VT_VALMASK; + if (v == VT_CMP) { + inv ^= (vtop--)->c.i; + a -= ind + 2; + if (a == (char)a) { + g(inv - 32); + g(a); + } else { + g(0x0f); + oad(inv - 16, a - 4); + } + } else if ((v & ~1) == VT_JMP) { + if ((v & 1) != inv) { + gjmp_addr(a); + gsym(vtop->c.i); + } else { + gsym(vtop->c.i); + o(0x05eb); + gjmp_addr(a); + } + vtop--; + } +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +ST_FUNC int gtst(int inv, int t) +{ + int v = vtop->r & VT_VALMASK; + if (nocode_wanted) { + ; + } else if (v == VT_CMP) { + /* fast case : can jump directly since flags are set */ + g(0x0f); + t = gjmp2((vtop->c.i - 16) ^ inv, t); + } else if (v == VT_JMP || v == VT_JMPI) { + /* && or || optimization */ + if ((v & 1) == inv) { + /* insert vtop->c jump list in t */ + uint32_t n1, n = vtop->c.i; + if (n) { + while ((n1 = read32le(cur_text_section->data + n))) + n = n1; + write32le(cur_text_section->data + n, t); + t = vtop->c.i; + } + } else { + t = gjmp(t); + gsym(vtop->c.i); + } + } + vtop--; + return t; +} + +/* generate an integer binary operation */ +ST_FUNC void gen_opi(int op) +{ + int r, fr, opc, c; + + switch(op) { + case '+': + case TOK_ADDC1: /* add with carry generation */ + opc = 0; + gen_op8: + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + /* constant case */ + vswap(); + r = gv(RC_INT); + vswap(); + c = vtop->c.i; + if (c == (char)c) { + /* generate inc and dec for smaller code */ + if (c==1 && opc==0 && op != TOK_ADDC1) { + o (0x40 | r); // inc + } else if (c==1 && opc==5 && op != TOK_SUBC1) { + o (0x48 | r); // dec + } else { + o(0x83); + o(0xc0 | (opc << 3) | r); + g(c); + } + } else { + o(0x81); + oad(0xc0 | (opc << 3) | r, c); + } + } else { + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + o((opc << 3) | 0x01); + o(0xc0 + r + fr * 8); + } + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) { + vtop->r = VT_CMP; + vtop->c.i = op; + } + break; + case '-': + case TOK_SUBC1: /* sub with carry generation */ + opc = 5; + goto gen_op8; + case TOK_ADDC2: /* add with carry use */ + opc = 2; + goto gen_op8; + case TOK_SUBC2: /* sub with carry use */ + opc = 3; + goto gen_op8; + case '&': + opc = 4; + goto gen_op8; + case '^': + opc = 6; + goto gen_op8; + case '|': + opc = 1; + goto gen_op8; + case '*': + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + o(0xaf0f); /* imul fr, r */ + o(0xc0 + fr + r * 8); + break; + case TOK_SHL: + opc = 4; + goto gen_shift; + case TOK_SHR: + opc = 5; + goto gen_shift; + case TOK_SAR: + opc = 7; + gen_shift: + opc = 0xc0 | (opc << 3); + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + /* constant case */ + vswap(); + r = gv(RC_INT); + vswap(); + c = vtop->c.i & 0x1f; + o(0xc1); /* shl/shr/sar $xxx, r */ + o(opc | r); + g(c); + } else { + /* we generate the shift in ecx */ + gv2(RC_INT, RC_ECX); + r = vtop[-1].r; + o(0xd3); /* shl/shr/sar %cl, r */ + o(opc | r); + } + vtop--; + break; + case '/': + case TOK_UDIV: + case TOK_PDIV: + case '%': + case TOK_UMOD: + case TOK_UMULL: + /* first operand must be in eax */ + /* XXX: need better constraint for second operand */ + gv2(RC_EAX, RC_ECX); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + save_reg(TREG_EDX); + /* save EAX too if used otherwise */ + save_reg_upstack(TREG_EAX, 1); + if (op == TOK_UMULL) { + o(0xf7); /* mul fr */ + o(0xe0 + fr); + vtop->r2 = TREG_EDX; + r = TREG_EAX; + } else { + if (op == TOK_UDIV || op == TOK_UMOD) { + o(0xf7d231); /* xor %edx, %edx, div fr, %eax */ + o(0xf0 + fr); + } else { + o(0xf799); /* cltd, idiv fr, %eax */ + o(0xf8 + fr); + } + if (op == '%' || op == TOK_UMOD) + r = TREG_EDX; + else + r = TREG_EAX; + } + vtop->r = r; + break; + default: + opc = 7; + goto gen_op8; + } +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +/* XXX: need to use ST1 too */ +ST_FUNC void gen_opf(int op) +{ + int a, ft, fc, swapped, r; + + /* convert constants to memory references */ + if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + vswap(); + gv(RC_FLOAT); + vswap(); + } + if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) + gv(RC_FLOAT); + + /* must put at least one value in the floating point register */ + if ((vtop[-1].r & VT_LVAL) && + (vtop[0].r & VT_LVAL)) { + vswap(); + gv(RC_FLOAT); + vswap(); + } + swapped = 0; + /* swap the stack if needed so that t1 is the register and t2 is + the memory reference */ + if (vtop[-1].r & VT_LVAL) { + vswap(); + swapped = 1; + } + if (op >= TOK_ULT && op <= TOK_GT) { + /* load on stack second operand */ + load(TREG_ST0, vtop); + save_reg(TREG_EAX); /* eax is used by FP comparison code */ + if (op == TOK_GE || op == TOK_GT) + swapped = !swapped; + else if (op == TOK_EQ || op == TOK_NE) + swapped = 0; + if (swapped) + o(0xc9d9); /* fxch %st(1) */ + if (op == TOK_EQ || op == TOK_NE) + o(0xe9da); /* fucompp */ + else + o(0xd9de); /* fcompp */ + o(0xe0df); /* fnstsw %ax */ + if (op == TOK_EQ) { + o(0x45e480); /* and $0x45, %ah */ + o(0x40fC80); /* cmp $0x40, %ah */ + } else if (op == TOK_NE) { + o(0x45e480); /* and $0x45, %ah */ + o(0x40f480); /* xor $0x40, %ah */ + op = TOK_NE; + } else if (op == TOK_GE || op == TOK_LE) { + o(0x05c4f6); /* test $0x05, %ah */ + op = TOK_EQ; + } else { + o(0x45c4f6); /* test $0x45, %ah */ + op = TOK_EQ; + } + vtop--; + vtop->r = VT_CMP; + vtop->c.i = op; + } else { + /* no memory reference possible for long double operations */ + if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + load(TREG_ST0, vtop); + swapped = !swapped; + } + + switch(op) { + default: + case '+': + a = 0; + break; + case '-': + a = 4; + if (swapped) + a++; + break; + case '*': + a = 1; + break; + case '/': + a = 6; + if (swapped) + a++; + break; + } + ft = vtop->type.t; + fc = vtop->c.i; + if ((ft & VT_BTYPE) == VT_LDOUBLE) { + o(0xde); /* fxxxp %st, %st(1) */ + o(0xc1 + (a << 3)); + } else { + /* if saved lvalue, then we must reload it */ + r = vtop->r; + if ((r & VT_VALMASK) == VT_LLOCAL) { + SValue v1; + r = get_reg(RC_INT); + v1.type.t = VT_INT; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + load(r, &v1); + fc = 0; + } + + if ((ft & VT_BTYPE) == VT_DOUBLE) + o(0xdc); + else + o(0xd8); + gen_modrm(a, r, vtop->sym, fc); + } + vtop--; + } +} + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +ST_FUNC void gen_cvt_itof(int t) +{ + save_reg(TREG_ST0); + gv(RC_INT); + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + /* signed long long to float/double/long double (unsigned case + is handled generically) */ + o(0x50 + vtop->r2); /* push r2 */ + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x242cdf); /* fildll (%esp) */ + o(0x08c483); /* add $8, %esp */ + } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == + (VT_INT | VT_UNSIGNED)) { + /* unsigned int to float/double/long double */ + o(0x6a); /* push $0 */ + g(0x00); + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x242cdf); /* fildll (%esp) */ + o(0x08c483); /* add $8, %esp */ + } else { + /* int to float/double/long double */ + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x2404db); /* fildl (%esp) */ + o(0x04c483); /* add $4, %esp */ + } + vtop->r = TREG_ST0; +} + +/* convert fp to int 't' type */ +ST_FUNC void gen_cvt_ftoi(int t) +{ + int bt = vtop->type.t & VT_BTYPE; + if (bt == VT_FLOAT) + vpush_global_sym(&func_old_type, TOK___fixsfdi); + else if (bt == VT_LDOUBLE) + vpush_global_sym(&func_old_type, TOK___fixxfdi); + else + vpush_global_sym(&func_old_type, TOK___fixdfdi); + vswap(); + gfunc_call(1); + vpushi(0); + vtop->r = REG_IRET; + vtop->r2 = REG_LRET; +} + +/* convert from one floating point type to another */ +ST_FUNC void gen_cvt_ftof(int t) +{ + /* all we have to do on i386 is to put the float in a register */ + gv(RC_FLOAT); +} + +/* computed goto support */ +ST_FUNC void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* bound check support functions */ +#ifdef CONFIG_TCC_BCHECK + +/* generate a bounded pointer addition */ +ST_FUNC void gen_bounded_ptr_add(void) +{ + /* prepare fast i386 function call (args in eax and edx) */ + gv2(RC_EAX, RC_EDX); + /* save all temporary registers */ + vtop -= 2; + save_regs(0); + /* do a fast function call */ + gen_static_call(TOK___bound_ptr_add); + /* returned pointer is in eax */ + vtop++; + vtop->r = TREG_EAX | VT_BOUNDED; + /* address of bounding function call point */ + vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel)); +} + +/* patch pointer addition in vtop so that pointer dereferencing is + also tested */ +ST_FUNC void gen_bounded_ptr_deref(void) +{ + addr_t func; + int size, align; + Elf32_Rel *rel; + Sym *sym; + + size = 0; + /* XXX: put that code in generic part of tcc */ + if (!is_float(vtop->type.t)) { + if (vtop->r & VT_LVAL_BYTE) + size = 1; + else if (vtop->r & VT_LVAL_SHORT) + size = 2; + } + if (!size) + size = type_size(&vtop->type, &align); + switch(size) { + case 1: func = TOK___bound_ptr_indir1; break; + case 2: func = TOK___bound_ptr_indir2; break; + case 4: func = TOK___bound_ptr_indir4; break; + case 8: func = TOK___bound_ptr_indir8; break; + case 12: func = TOK___bound_ptr_indir12; break; + case 16: func = TOK___bound_ptr_indir16; break; + default: + tcc_error("unhandled size when dereferencing bounded pointer"); + func = 0; + break; + } + + /* patch relocation */ + /* XXX: find a better solution ? */ + rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i); + sym = external_global_sym(func, &func_old_type, 0); + if (!sym->c) + put_extern_sym(sym, NULL, 0, 0); + rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info)); +} +#endif + +/* Save the stack pointer onto the stack */ +ST_FUNC void gen_vla_sp_save(int addr) { + /* mov %esp,addr(%ebp)*/ + o(0x89); + gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + o(0x8b); + gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr); +} + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { +#ifdef TCC_TARGET_PE + /* alloca does more than just adjust %rsp on Windows */ + vpush_global_sym(&func_old_type, TOK_alloca); + vswap(); /* Move alloca ref past allocation size */ + gfunc_call(1); +#else + int r; + r = gv(RC_INT); /* allocation size */ + /* sub r,%rsp */ + o(0x2b); + o(0xe0 | r); + /* We align to 16 bytes rather than align */ + /* and ~15, %esp */ + o(0xf0e483); + vpop(); +#endif +} + +/* end of X86 code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final-old/i386-link.c b/05/tcc-final-old/i386-link.c new file mode 100644 index 0000000..aea3c21 --- /dev/null +++ b/05/tcc-final-old/i386-link.c @@ -0,0 +1,247 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_386 + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_386_32 +#define R_DATA_PTR R_386_32 +#define R_JMP_SLOT R_386_JMP_SLOT +#define R_GLOB_DAT R_386_GLOB_DAT +#define R_COPY R_386_COPY +#define R_RELATIVE R_386_RELATIVE + +#define R_NUM R_386_NUM + +#define ELF_START_ADDR 0x08048000 +#define ELF_PAGE_SIZE 0x1000 + +#define PCRELATIVE_DLLPLT 0 +#define RELOCATE_DLLPLT 0 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_386_RELATIVE: + case R_386_16: + case R_386_32: + case R_386_GOTPC: + case R_386_GOTOFF: + case R_386_GOT32: + case R_386_GOT32X: + case R_386_GLOB_DAT: + case R_386_COPY: + return 0; + + case R_386_PC16: + case R_386_PC32: + case R_386_PLT32: + case R_386_JMP_SLOT: + return 1; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_386_RELATIVE: + case R_386_16: + case R_386_GLOB_DAT: + case R_386_JMP_SLOT: + case R_386_COPY: + return NO_GOTPLT_ENTRY; + + case R_386_32: + /* This relocations shouldn't normally need GOT or PLT + slots if it weren't for simplicity in the code generator. + See our caller for comments. */ + return AUTO_GOTPLT_ENTRY; + + case R_386_PC16: + case R_386_PC32: + return AUTO_GOTPLT_ENTRY; + + case R_386_GOTPC: + case R_386_GOTOFF: + return BUILD_GOT_ONLY; + + case R_386_GOT32: + case R_386_GOT32X: + case R_386_PLT32: + return ALWAYS_GOTPLT_ENTRY; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + int modrm; + unsigned plt_offset, relofs; + + /* on i386 if we build a DLL, we add a %ebx offset */ + if (s1->output_type == TCC_OUTPUT_DLL) + modrm = 0xa3; + else + modrm = 0x25; + + /* empty PLT: create PLT0 entry that pushes the library identifier + (GOT + PTR_SIZE) and jumps to ld.so resolution routine + (GOT + 2 * PTR_SIZE) */ + if (plt->data_offset == 0) { + p = section_ptr_add(plt, 16); + p[0] = 0xff; /* pushl got + PTR_SIZE */ + p[1] = modrm + 0x10; + write32le(p + 2, PTR_SIZE); + p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */ + p[7] = modrm; + write32le(p + 8, PTR_SIZE * 2); + } + plt_offset = plt->data_offset; + + /* The PLT slot refers to the relocation entry it needs via offset. + The reloc entry is created below, so its offset is the current + data_offset */ + relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0; + + /* Jump to GOT entry where ld.so initially put the address of ip + 4 */ + p = section_ptr_add(plt, 16); + p[0] = 0xff; /* jmp *(got + x) */ + p[1] = modrm; + write32le(p + 2, got_offset); + p[6] = 0x68; /* push $xxx */ + write32le(p + 7, relofs); + p[11] = 0xe9; /* jmp plt_start */ + write32le(p + 12, -(plt->data_offset)); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + add32le(p + 2, s1->got->sh_addr); + add32le(p + 8, s1->got->sh_addr); + p += 16; + while (p < p_end) { + add32le(p + 2, s1->got->sh_addr); + p += 16; + } + } +} + +static ElfW_Rel *qrel; /* ptr to next reloc entry reused */ + +void relocate_init(Section *sr) +{ + qrel = (ElfW_Rel *) sr->data; +} + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + int sym_index, esym_index; + + sym_index = ELFW(R_SYM)(rel->r_info); + + switch (type) { + case R_386_32: + if (s1->output_type == TCC_OUTPUT_DLL) { + esym_index = s1->sym_attrs[sym_index].dyn_index; + qrel->r_offset = rel->r_offset; + if (esym_index) { + qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32); + qrel++; + return; + } else { + qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE); + qrel++; + } + } + add32le(ptr, val); + return; + case R_386_PC32: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* DLL relocation */ + esym_index = s1->sym_attrs[sym_index].dyn_index; + if (esym_index) { + qrel->r_offset = rel->r_offset; + qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32); + qrel++; + return; + } + } + add32le(ptr, val - addr); + return; + case R_386_PLT32: + add32le(ptr, val - addr); + return; + case R_386_GLOB_DAT: + case R_386_JMP_SLOT: + write32le(ptr, val); + return; + case R_386_GOTPC: + add32le(ptr, s1->got->sh_addr - addr); + return; + case R_386_GOTOFF: + add32le(ptr, val - s1->got->sh_addr); + return; + case R_386_GOT32: + case R_386_GOT32X: + /* we load the got offset */ + add32le(ptr, s1->sym_attrs[sym_index].got_offset); + return; + case R_386_16: + if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) { + output_file: + tcc_error("can only produce 16-bit binary files"); + } + write16le(ptr, read16le(ptr) + val); + return; + case R_386_PC16: + if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) + goto output_file; + write16le(ptr, read16le(ptr) + val - addr); + return; + case R_386_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + return; + case R_386_COPY: + /* This relocation must copy initialized data from the library + to the program .bss segment. Currently made like for ARM + (to remove noise of default case). Is this true? + */ + return; + default: + fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n", + type, (unsigned)addr, ptr, (unsigned)val); + return; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final-old/i386-tok.h b/05/tcc-final-old/i386-tok.h new file mode 100644 index 0000000..8c25af0 --- /dev/null +++ b/05/tcc-final-old/i386-tok.h @@ -0,0 +1,253 @@ +/* ------------------------------------------------------------------ */ +/* WARNING: relative order of tokens is important. */ + +/* register */ + DEF_ASM(al) + DEF_ASM(cl) + DEF_ASM(dl) + DEF_ASM(bl) + DEF_ASM(ah) + DEF_ASM(ch) + DEF_ASM(dh) + DEF_ASM(bh) + DEF_ASM(ax) + DEF_ASM(cx) + DEF_ASM(dx) + DEF_ASM(bx) + DEF_ASM(sp) + DEF_ASM(bp) + DEF_ASM(si) + DEF_ASM(di) + DEF_ASM(eax) + DEF_ASM(ecx) + DEF_ASM(edx) + DEF_ASM(ebx) + DEF_ASM(esp) + DEF_ASM(ebp) + DEF_ASM(esi) + DEF_ASM(edi) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(rax) + DEF_ASM(rcx) + DEF_ASM(rdx) + DEF_ASM(rbx) + DEF_ASM(rsp) + DEF_ASM(rbp) + DEF_ASM(rsi) + DEF_ASM(rdi) +#endif + DEF_ASM(mm0) + DEF_ASM(mm1) + DEF_ASM(mm2) + DEF_ASM(mm3) + DEF_ASM(mm4) + DEF_ASM(mm5) + DEF_ASM(mm6) + DEF_ASM(mm7) + DEF_ASM(xmm0) + DEF_ASM(xmm1) + DEF_ASM(xmm2) + DEF_ASM(xmm3) + DEF_ASM(xmm4) + DEF_ASM(xmm5) + DEF_ASM(xmm6) + DEF_ASM(xmm7) + DEF_ASM(cr0) + DEF_ASM(cr1) + DEF_ASM(cr2) + DEF_ASM(cr3) + DEF_ASM(cr4) + DEF_ASM(cr5) + DEF_ASM(cr6) + DEF_ASM(cr7) + DEF_ASM(tr0) + DEF_ASM(tr1) + DEF_ASM(tr2) + DEF_ASM(tr3) + DEF_ASM(tr4) + DEF_ASM(tr5) + DEF_ASM(tr6) + DEF_ASM(tr7) + DEF_ASM(db0) + DEF_ASM(db1) + DEF_ASM(db2) + DEF_ASM(db3) + DEF_ASM(db4) + DEF_ASM(db5) + DEF_ASM(db6) + DEF_ASM(db7) + DEF_ASM(dr0) + DEF_ASM(dr1) + DEF_ASM(dr2) + DEF_ASM(dr3) + DEF_ASM(dr4) + DEF_ASM(dr5) + DEF_ASM(dr6) + DEF_ASM(dr7) + DEF_ASM(es) + DEF_ASM(cs) + DEF_ASM(ss) + DEF_ASM(ds) + DEF_ASM(fs) + DEF_ASM(gs) + DEF_ASM(st) + DEF_ASM(rip) + +#ifdef TCC_TARGET_X86_64 + /* The four low parts of sp/bp/si/di that exist only on + x86-64 (encoding aliased to ah,ch,dh,dh when not using REX). */ + DEF_ASM(spl) + DEF_ASM(bpl) + DEF_ASM(sil) + DEF_ASM(dil) +#endif + /* generic two operands */ + DEF_BWLX(mov) + + DEF_BWLX(add) + DEF_BWLX(or) + DEF_BWLX(adc) + DEF_BWLX(sbb) + DEF_BWLX(and) + DEF_BWLX(sub) + DEF_BWLX(xor) + DEF_BWLX(cmp) + + /* unary ops */ + DEF_BWLX(inc) + DEF_BWLX(dec) + DEF_BWLX(not) + DEF_BWLX(neg) + DEF_BWLX(mul) + DEF_BWLX(imul) + DEF_BWLX(div) + DEF_BWLX(idiv) + + DEF_BWLX(xchg) + DEF_BWLX(test) + + /* shifts */ + DEF_BWLX(rol) + DEF_BWLX(ror) + DEF_BWLX(rcl) + DEF_BWLX(rcr) + DEF_BWLX(shl) + DEF_BWLX(shr) + DEF_BWLX(sar) + + DEF_WLX(shld) + DEF_WLX(shrd) + + DEF_ASM(pushw) + DEF_ASM(pushl) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(pushq) +#endif + DEF_ASM(push) + + DEF_ASM(popw) + DEF_ASM(popl) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(popq) +#endif + DEF_ASM(pop) + + DEF_BWL(in) + DEF_BWL(out) + + DEF_WLX(movzb) + DEF_ASM(movzwl) + DEF_ASM(movsbw) + DEF_ASM(movsbl) + DEF_ASM(movswl) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(movsbq) + DEF_ASM(movswq) + DEF_ASM(movzwq) + DEF_ASM(movslq) +#endif + + DEF_WLX(lea) + + DEF_ASM(les) + DEF_ASM(lds) + DEF_ASM(lss) + DEF_ASM(lfs) + DEF_ASM(lgs) + + DEF_ASM(call) + DEF_ASM(jmp) + DEF_ASM(lcall) + DEF_ASM(ljmp) + + DEF_ASMTEST(j,) + + DEF_ASMTEST(set,) + DEF_ASMTEST(set,b) + DEF_ASMTEST(cmov,) + + DEF_WLX(bsf) + DEF_WLX(bsr) + DEF_WLX(bt) + DEF_WLX(bts) + DEF_WLX(btr) + DEF_WLX(btc) + + DEF_WLX(lar) + DEF_WLX(lsl) + + /* generic FP ops */ + DEF_FP(add) + DEF_FP(mul) + + DEF_ASM(fcom) + DEF_ASM(fcom_1) /* non existent op, just to have a regular table */ + DEF_FP1(com) + + DEF_FP(comp) + DEF_FP(sub) + DEF_FP(subr) + DEF_FP(div) + DEF_FP(divr) + + DEF_BWLX(xadd) + DEF_BWLX(cmpxchg) + + /* string ops */ + DEF_BWLX(cmps) + DEF_BWLX(scmp) + DEF_BWL(ins) + DEF_BWL(outs) + DEF_BWLX(lods) + DEF_BWLX(slod) + DEF_BWLX(movs) + DEF_BWLX(smov) + DEF_BWLX(scas) + DEF_BWLX(ssca) + DEF_BWLX(stos) + DEF_BWLX(ssto) + + /* generic asm ops */ +#define ALT(x) +#define DEF_ASM_OP0(name, opcode) DEF_ASM(name) +#define DEF_ASM_OP0L(name, opcode, group, instr_type) +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif + +#define ALT(x) +#define DEF_ASM_OP0(name, opcode) +#define DEF_ASM_OP0L(name, opcode, group, instr_type) DEF_ASM(name) +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) DEF_ASM(name) +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) DEF_ASM(name) +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) DEF_ASM(name) +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif diff --git a/05/tcc-final-old/il-gen.c b/05/tcc-final-old/il-gen.c new file mode 100644 index 0000000..bb670cc --- /dev/null +++ b/05/tcc-final-old/il-gen.c @@ -0,0 +1,657 @@ +/* + * CIL code generator for TCC + * + * Copyright (c) 2002 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#error this code has bit-rotted since 2003 + +/* number of available registers */ +#define NB_REGS 3 + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_ST 0x0001 /* any stack entry */ +#define RC_ST0 0x0002 /* top of stack */ +#define RC_ST1 0x0004 /* top - 1 */ + +#define RC_INT RC_ST +#define RC_FLOAT RC_ST +#define RC_IRET RC_ST0 /* function return: integer register */ +#define RC_LRET RC_ST0 /* function return: second integer register */ +#define RC_FRET RC_ST0 /* function return: float register */ + +/* pretty names for the registers */ +enum { + REG_ST0 = 0, + REG_ST1, + REG_ST2, +}; + +const int reg_classes[NB_REGS] = { + /* ST0 */ RC_ST | RC_ST0, + /* ST1 */ RC_ST | RC_ST1, + /* ST2 */ RC_ST, +}; + +/* return registers for function */ +#define REG_IRET REG_ST0 /* single word int return register */ +#define REG_LRET REG_ST0 /* second word return register (for long long) */ +#define REG_FRET REG_ST0 /* float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +/* #define INVERT_FUNC_PARAMS */ + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 8 +#define LDOUBLE_ALIGN 8 + +/* function call context */ +typedef struct GFuncContext { + int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */ +} GFuncContext; + +/******************************************************/ +/* opcode definitions */ + +#define IL_OP_PREFIX 0xFE + +enum ILOPCodes { +#define OP(name, str, n) IL_OP_ ## name = n, +#include "il-opcodes.h" +#undef OP +}; + +char *il_opcodes_str[] = { +#define OP(name, str, n) [n] = str, +#include "il-opcodes.h" +#undef OP +}; + +/******************************************************/ + +/* arguments variable numbers start from there */ +#define ARG_BASE 0x70000000 + +static FILE *il_outfile; + +static void out_byte(int c) +{ + *(char *)ind++ = c; +} + +static void out_le32(int c) +{ + out_byte(c); + out_byte(c >> 8); + out_byte(c >> 16); + out_byte(c >> 24); +} + +static void init_outfile(void) +{ + if (!il_outfile) { + il_outfile = stdout; + fprintf(il_outfile, + ".assembly extern mscorlib\n" + "{\n" + ".ver 1:0:2411:0\n" + "}\n\n"); + } +} + +static void out_op1(int op) +{ + if (op & 0x100) + out_byte(IL_OP_PREFIX); + out_byte(op & 0xff); +} + +/* output an opcode with prefix */ +static void out_op(int op) +{ + out_op1(op); + fprintf(il_outfile, " %s\n", il_opcodes_str[op]); +} + +static void out_opb(int op, int c) +{ + out_op1(op); + out_byte(c); + fprintf(il_outfile, " %s %d\n", il_opcodes_str[op], c); +} + +static void out_opi(int op, int c) +{ + out_op1(op); + out_le32(c); + fprintf(il_outfile, " %s 0x%x\n", il_opcodes_str[op], c); +} + +/* XXX: not complete */ +static void il_type_to_str(char *buf, int buf_size, + int t, const char *varstr) +{ + int bt; + Sym *s, *sa; + char buf1[256]; + const char *tstr; + + t = t & VT_TYPE; + bt = t & VT_BTYPE; + buf[0] = '\0'; + if (t & VT_UNSIGNED) + pstrcat(buf, buf_size, "unsigned "); + switch(bt) { + case VT_VOID: + tstr = "void"; + goto add_tstr; + case VT_BOOL: + tstr = "bool"; + goto add_tstr; + case VT_BYTE: + tstr = "int8"; + goto add_tstr; + case VT_SHORT: + tstr = "int16"; + goto add_tstr; + case VT_ENUM: + case VT_INT: + case VT_LONG: + tstr = "int32"; + goto add_tstr; + case VT_LLONG: + tstr = "int64"; + goto add_tstr; + case VT_FLOAT: + tstr = "float32"; + goto add_tstr; + case VT_DOUBLE: + case VT_LDOUBLE: + tstr = "float64"; + add_tstr: + pstrcat(buf, buf_size, tstr); + break; + case VT_STRUCT: + tcc_error("structures not handled yet"); + break; + case VT_FUNC: + s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); + il_type_to_str(buf, buf_size, s->t, varstr); + pstrcat(buf, buf_size, "("); + sa = s->next; + while (sa != NULL) { + il_type_to_str(buf1, sizeof(buf1), sa->t, NULL); + pstrcat(buf, buf_size, buf1); + sa = sa->next; + if (sa) + pstrcat(buf, buf_size, ", "); + } + pstrcat(buf, buf_size, ")"); + goto no_var; + case VT_PTR: + s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); + pstrcpy(buf1, sizeof(buf1), "*"); + if (varstr) + pstrcat(buf1, sizeof(buf1), varstr); + il_type_to_str(buf, buf_size, s->t, buf1); + goto no_var; + } + if (varstr) { + pstrcat(buf, buf_size, " "); + pstrcat(buf, buf_size, varstr); + } + no_var: ; +} + + +/* patch relocation entry with value 'val' */ +void greloc_patch1(Reloc *p, int val) +{ +} + +/* output a symbol and patch all calls to it */ +void gsym_addr(t, a) +{ +} + +/* output jump and return symbol */ +static int out_opj(int op, int c) +{ + out_op1(op); + out_le32(0); + if (c == 0) { + c = ind - (int)cur_text_section->data; + } + fprintf(il_outfile, " %s L%d\n", il_opcodes_str[op], c); + return c; +} + +void gsym(int t) +{ + fprintf(il_outfile, "L%d:\n", t); +} + +/* load 'r' from value 'sv' */ +void load(int r, SValue *sv) +{ + int v, fc, ft; + + v = sv->r & VT_VALMASK; + fc = sv->c.i; + ft = sv->t; + + if (sv->r & VT_LVAL) { + if (v == VT_LOCAL) { + if (fc >= ARG_BASE) { + fc -= ARG_BASE; + if (fc >= 0 && fc <= 4) { + out_op(IL_OP_LDARG_0 + fc); + } else if (fc <= 0xff) { + out_opb(IL_OP_LDARG_S, fc); + } else { + out_opi(IL_OP_LDARG, fc); + } + } else { + if (fc >= 0 && fc <= 4) { + out_op(IL_OP_LDLOC_0 + fc); + } else if (fc <= 0xff) { + out_opb(IL_OP_LDLOC_S, fc); + } else { + out_opi(IL_OP_LDLOC, fc); + } + } + } else if (v == VT_CONST) { + /* XXX: handle globals */ + out_opi(IL_OP_LDSFLD, 0); + } else { + if ((ft & VT_BTYPE) == VT_FLOAT) { + out_op(IL_OP_LDIND_R4); + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + out_op(IL_OP_LDIND_R8); + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + out_op(IL_OP_LDIND_R8); + } else if ((ft & VT_TYPE) == VT_BYTE) + out_op(IL_OP_LDIND_I1); + else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) + out_op(IL_OP_LDIND_U1); + else if ((ft & VT_TYPE) == VT_SHORT) + out_op(IL_OP_LDIND_I2); + else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) + out_op(IL_OP_LDIND_U2); + else + out_op(IL_OP_LDIND_I4); + } + } else { + if (v == VT_CONST) { + /* XXX: handle globals */ + if (fc >= -1 && fc <= 8) { + out_op(IL_OP_LDC_I4_M1 + fc + 1); + } else { + out_opi(IL_OP_LDC_I4, fc); + } + } else if (v == VT_LOCAL) { + if (fc >= ARG_BASE) { + fc -= ARG_BASE; + if (fc <= 0xff) { + out_opb(IL_OP_LDARGA_S, fc); + } else { + out_opi(IL_OP_LDARGA, fc); + } + } else { + if (fc <= 0xff) { + out_opb(IL_OP_LDLOCA_S, fc); + } else { + out_opi(IL_OP_LDLOCA, fc); + } + } + } else { + /* XXX: do it */ + } + } +} + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue *sv) +{ + int v, fc, ft; + + v = sv->r & VT_VALMASK; + fc = sv->c.i; + ft = sv->t; + if (v == VT_LOCAL) { + if (fc >= ARG_BASE) { + fc -= ARG_BASE; + /* XXX: check IL arg store semantics */ + if (fc <= 0xff) { + out_opb(IL_OP_STARG_S, fc); + } else { + out_opi(IL_OP_STARG, fc); + } + } else { + if (fc >= 0 && fc <= 4) { + out_op(IL_OP_STLOC_0 + fc); + } else if (fc <= 0xff) { + out_opb(IL_OP_STLOC_S, fc); + } else { + out_opi(IL_OP_STLOC, fc); + } + } + } else if (v == VT_CONST) { + /* XXX: handle globals */ + out_opi(IL_OP_STSFLD, 0); + } else { + if ((ft & VT_BTYPE) == VT_FLOAT) + out_op(IL_OP_STIND_R4); + else if ((ft & VT_BTYPE) == VT_DOUBLE) + out_op(IL_OP_STIND_R8); + else if ((ft & VT_BTYPE) == VT_LDOUBLE) + out_op(IL_OP_STIND_R8); + else if ((ft & VT_BTYPE) == VT_BYTE) + out_op(IL_OP_STIND_I1); + else if ((ft & VT_BTYPE) == VT_SHORT) + out_op(IL_OP_STIND_I2); + else + out_op(IL_OP_STIND_I4); + } +} + +/* start function call and return function call context */ +void gfunc_start(GFuncContext *c, int func_call) +{ + c->func_call = func_call; +} + +/* push function parameter which is in (vtop->t, vtop->c). Stack entry + is then popped. */ +void gfunc_param(GFuncContext *c) +{ + if ((vtop->t & VT_BTYPE) == VT_STRUCT) { + tcc_error("structures passed as value not handled yet"); + } else { + /* simply push on stack */ + gv(RC_ST0); + } + vtop--; +} + +/* generate function call with address in (vtop->t, vtop->c) and free function + context. Stack entry is popped */ +void gfunc_call(GFuncContext *c) +{ + char buf[1024]; + + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + /* XXX: more info needed from tcc */ + il_type_to_str(buf, sizeof(buf), vtop->t, "xxx"); + fprintf(il_outfile, " call %s\n", buf); + } else { + /* indirect call */ + gv(RC_INT); + il_type_to_str(buf, sizeof(buf), vtop->t, NULL); + fprintf(il_outfile, " calli %s\n", buf); + } + vtop--; +} + +/* generate function prolog of type 't' */ +void gfunc_prolog(int t) +{ + int addr, u, func_call; + Sym *sym; + char buf[1024]; + + init_outfile(); + + /* XXX: pass function name to gfunc_prolog */ + il_type_to_str(buf, sizeof(buf), t, funcname); + fprintf(il_outfile, ".method static %s il managed\n", buf); + fprintf(il_outfile, "{\n"); + /* XXX: cannot do better now */ + fprintf(il_outfile, " .maxstack %d\n", NB_REGS); + fprintf(il_outfile, " .locals (int32, int32, int32, int32, int32, int32, int32, int32)\n"); + + if (!strcmp(funcname, "main")) + fprintf(il_outfile, " .entrypoint\n"); + + sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT); + func_call = sym->r; + + addr = ARG_BASE; + /* if the function returns a structure, then add an + implicit pointer parameter */ + func_vt = sym->t; + func_var = (sym->c == FUNC_ELLIPSIS); + if ((func_vt & VT_BTYPE) == VT_STRUCT) { + func_vc = addr; + addr++; + } + /* define parameters */ + while ((sym = sym->next) != NULL) { + u = sym->t; + sym_push(sym->v & ~SYM_FIELD, u, + VT_LOCAL | lvalue_type(sym->type.t), addr); + addr++; + } +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + out_op(IL_OP_RET); + fprintf(il_outfile, "}\n\n"); +} + +/* generate a jump to a label */ +int gjmp(int t) +{ + return out_opj(IL_OP_BR, t); +} + +/* generate a jump to a fixed address */ +void gjmp_addr(int a) +{ + /* XXX: handle syms */ + out_opi(IL_OP_BR, a); +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +int gtst(int inv, int t) +{ + int v, *p, c; + + v = vtop->r & VT_VALMASK; + if (v == VT_CMP) { + c = vtop->c.i ^ inv; + switch(c) { + case TOK_EQ: + c = IL_OP_BEQ; + break; + case TOK_NE: + c = IL_OP_BNE_UN; + break; + case TOK_LT: + c = IL_OP_BLT; + break; + case TOK_LE: + c = IL_OP_BLE; + break; + case TOK_GT: + c = IL_OP_BGT; + break; + case TOK_GE: + c = IL_OP_BGE; + break; + case TOK_ULT: + c = IL_OP_BLT_UN; + break; + case TOK_ULE: + c = IL_OP_BLE_UN; + break; + case TOK_UGT: + c = IL_OP_BGT_UN; + break; + case TOK_UGE: + c = IL_OP_BGE_UN; + break; + } + t = out_opj(c, t); + } else if (v == VT_JMP || v == VT_JMPI) { + /* && or || optimization */ + if ((v & 1) == inv) { + /* insert vtop->c jump list in t */ + p = &vtop->c.i; + while (*p != 0) + p = (int *)*p; + *p = t; + t = vtop->c.i; + } else { + t = gjmp(t); + gsym(vtop->c.i); + } + } + vtop--; + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + gv2(RC_ST1, RC_ST0); + switch(op) { + case '+': + out_op(IL_OP_ADD); + goto std_op; + case '-': + out_op(IL_OP_SUB); + goto std_op; + case '&': + out_op(IL_OP_AND); + goto std_op; + case '^': + out_op(IL_OP_XOR); + goto std_op; + case '|': + out_op(IL_OP_OR); + goto std_op; + case '*': + out_op(IL_OP_MUL); + goto std_op; + case TOK_SHL: + out_op(IL_OP_SHL); + goto std_op; + case TOK_SHR: + out_op(IL_OP_SHR_UN); + goto std_op; + case TOK_SAR: + out_op(IL_OP_SHR); + goto std_op; + case '/': + case TOK_PDIV: + out_op(IL_OP_DIV); + goto std_op; + case TOK_UDIV: + out_op(IL_OP_DIV_UN); + goto std_op; + case '%': + out_op(IL_OP_REM); + goto std_op; + case TOK_UMOD: + out_op(IL_OP_REM_UN); + std_op: + vtop--; + vtop[0].r = REG_ST0; + break; + case TOK_EQ: + case TOK_NE: + case TOK_LT: + case TOK_LE: + case TOK_GT: + case TOK_GE: + case TOK_ULT: + case TOK_ULE: + case TOK_UGT: + case TOK_UGE: + vtop--; + vtop[0].r = VT_CMP; + vtop[0].c.i = op; + break; + } +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +void gen_opf(int op) +{ + /* same as integer */ + gen_opi(op); +} + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +void gen_cvt_itof(int t) +{ + gv(RC_ST0); + if (t == VT_FLOAT) + out_op(IL_OP_CONV_R4); + else + out_op(IL_OP_CONV_R8); +} + +/* convert fp to int 't' type */ +/* XXX: handle long long case */ +void gen_cvt_ftoi(int t) +{ + gv(RC_ST0); + switch(t) { + case VT_INT | VT_UNSIGNED: + out_op(IL_OP_CONV_U4); + break; + case VT_LLONG: + out_op(IL_OP_CONV_I8); + break; + case VT_LLONG | VT_UNSIGNED: + out_op(IL_OP_CONV_U8); + break; + default: + out_op(IL_OP_CONV_I4); + break; + } +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ + gv(RC_ST0); + if (t == VT_FLOAT) { + out_op(IL_OP_CONV_R4); + } else { + out_op(IL_OP_CONV_R8); + } +} + +/* end of CIL code generator */ +/*************************************************************/ + diff --git a/05/tcc-final-old/il-opcodes.h b/05/tcc-final-old/il-opcodes.h new file mode 100644 index 0000000..d53ffb2 --- /dev/null +++ b/05/tcc-final-old/il-opcodes.h @@ -0,0 +1,251 @@ +/* + * CIL opcode definition + * + * Copyright (c) 2002 Fabrice Bellard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +OP(NOP, "nop", 0x00) +OP(BREAK, "break", 0x01) +OP(LDARG_0, "ldarg.0", 0x02) +OP(LDARG_1, "ldarg.1", 0x03) +OP(LDARG_2, "ldarg.2", 0x04) +OP(LDARG_3, "ldarg.3", 0x05) +OP(LDLOC_0, "ldloc.0", 0x06) +OP(LDLOC_1, "ldloc.1", 0x07) +OP(LDLOC_2, "ldloc.2", 0x08) +OP(LDLOC_3, "ldloc.3", 0x09) +OP(STLOC_0, "stloc.0", 0x0a) +OP(STLOC_1, "stloc.1", 0x0b) +OP(STLOC_2, "stloc.2", 0x0c) +OP(STLOC_3, "stloc.3", 0x0d) +OP(LDARG_S, "ldarg.s", 0x0e) +OP(LDARGA_S, "ldarga.s", 0x0f) +OP(STARG_S, "starg.s", 0x10) +OP(LDLOC_S, "ldloc.s", 0x11) +OP(LDLOCA_S, "ldloca.s", 0x12) +OP(STLOC_S, "stloc.s", 0x13) +OP(LDNULL, "ldnull", 0x14) +OP(LDC_I4_M1, "ldc.i4.m1", 0x15) +OP(LDC_I4_0, "ldc.i4.0", 0x16) +OP(LDC_I4_1, "ldc.i4.1", 0x17) +OP(LDC_I4_2, "ldc.i4.2", 0x18) +OP(LDC_I4_3, "ldc.i4.3", 0x19) +OP(LDC_I4_4, "ldc.i4.4", 0x1a) +OP(LDC_I4_5, "ldc.i4.5", 0x1b) +OP(LDC_I4_6, "ldc.i4.6", 0x1c) +OP(LDC_I4_7, "ldc.i4.7", 0x1d) +OP(LDC_I4_8, "ldc.i4.8", 0x1e) +OP(LDC_I4_S, "ldc.i4.s", 0x1f) +OP(LDC_I4, "ldc.i4", 0x20) +OP(LDC_I8, "ldc.i8", 0x21) +OP(LDC_R4, "ldc.r4", 0x22) +OP(LDC_R8, "ldc.r8", 0x23) +OP(LDPTR, "ldptr", 0x24) +OP(DUP, "dup", 0x25) +OP(POP, "pop", 0x26) +OP(JMP, "jmp", 0x27) +OP(CALL, "call", 0x28) +OP(CALLI, "calli", 0x29) +OP(RET, "ret", 0x2a) +OP(BR_S, "br.s", 0x2b) +OP(BRFALSE_S, "brfalse.s", 0x2c) +OP(BRTRUE_S, "brtrue.s", 0x2d) +OP(BEQ_S, "beq.s", 0x2e) +OP(BGE_S, "bge.s", 0x2f) +OP(BGT_S, "bgt.s", 0x30) +OP(BLE_S, "ble.s", 0x31) +OP(BLT_S, "blt.s", 0x32) +OP(BNE_UN_S, "bne.un.s", 0x33) +OP(BGE_UN_S, "bge.un.s", 0x34) +OP(BGT_UN_S, "bgt.un.s", 0x35) +OP(BLE_UN_S, "ble.un.s", 0x36) +OP(BLT_UN_S, "blt.un.s", 0x37) +OP(BR, "br", 0x38) +OP(BRFALSE, "brfalse", 0x39) +OP(BRTRUE, "brtrue", 0x3a) +OP(BEQ, "beq", 0x3b) +OP(BGE, "bge", 0x3c) +OP(BGT, "bgt", 0x3d) +OP(BLE, "ble", 0x3e) +OP(BLT, "blt", 0x3f) +OP(BNE_UN, "bne.un", 0x40) +OP(BGE_UN, "bge.un", 0x41) +OP(BGT_UN, "bgt.un", 0x42) +OP(BLE_UN, "ble.un", 0x43) +OP(BLT_UN, "blt.un", 0x44) +OP(SWITCH, "switch", 0x45) +OP(LDIND_I1, "ldind.i1", 0x46) +OP(LDIND_U1, "ldind.u1", 0x47) +OP(LDIND_I2, "ldind.i2", 0x48) +OP(LDIND_U2, "ldind.u2", 0x49) +OP(LDIND_I4, "ldind.i4", 0x4a) +OP(LDIND_U4, "ldind.u4", 0x4b) +OP(LDIND_I8, "ldind.i8", 0x4c) +OP(LDIND_I, "ldind.i", 0x4d) +OP(LDIND_R4, "ldind.r4", 0x4e) +OP(LDIND_R8, "ldind.r8", 0x4f) +OP(LDIND_REF, "ldind.ref", 0x50) +OP(STIND_REF, "stind.ref", 0x51) +OP(STIND_I1, "stind.i1", 0x52) +OP(STIND_I2, "stind.i2", 0x53) +OP(STIND_I4, "stind.i4", 0x54) +OP(STIND_I8, "stind.i8", 0x55) +OP(STIND_R4, "stind.r4", 0x56) +OP(STIND_R8, "stind.r8", 0x57) +OP(ADD, "add", 0x58) +OP(SUB, "sub", 0x59) +OP(MUL, "mul", 0x5a) +OP(DIV, "div", 0x5b) +OP(DIV_UN, "div.un", 0x5c) +OP(REM, "rem", 0x5d) +OP(REM_UN, "rem.un", 0x5e) +OP(AND, "and", 0x5f) +OP(OR, "or", 0x60) +OP(XOR, "xor", 0x61) +OP(SHL, "shl", 0x62) +OP(SHR, "shr", 0x63) +OP(SHR_UN, "shr.un", 0x64) +OP(NEG, "neg", 0x65) +OP(NOT, "not", 0x66) +OP(CONV_I1, "conv.i1", 0x67) +OP(CONV_I2, "conv.i2", 0x68) +OP(CONV_I4, "conv.i4", 0x69) +OP(CONV_I8, "conv.i8", 0x6a) +OP(CONV_R4, "conv.r4", 0x6b) +OP(CONV_R8, "conv.r8", 0x6c) +OP(CONV_U4, "conv.u4", 0x6d) +OP(CONV_U8, "conv.u8", 0x6e) +OP(CALLVIRT, "callvirt", 0x6f) +OP(CPOBJ, "cpobj", 0x70) +OP(LDOBJ, "ldobj", 0x71) +OP(LDSTR, "ldstr", 0x72) +OP(NEWOBJ, "newobj", 0x73) +OP(CASTCLASS, "castclass", 0x74) +OP(ISINST, "isinst", 0x75) +OP(CONV_R_UN, "conv.r.un", 0x76) +OP(ANN_DATA_S, "ann.data.s", 0x77) +OP(UNBOX, "unbox", 0x79) +OP(THROW, "throw", 0x7a) +OP(LDFLD, "ldfld", 0x7b) +OP(LDFLDA, "ldflda", 0x7c) +OP(STFLD, "stfld", 0x7d) +OP(LDSFLD, "ldsfld", 0x7e) +OP(LDSFLDA, "ldsflda", 0x7f) +OP(STSFLD, "stsfld", 0x80) +OP(STOBJ, "stobj", 0x81) +OP(CONV_OVF_I1_UN, "conv.ovf.i1.un", 0x82) +OP(CONV_OVF_I2_UN, "conv.ovf.i2.un", 0x83) +OP(CONV_OVF_I4_UN, "conv.ovf.i4.un", 0x84) +OP(CONV_OVF_I8_UN, "conv.ovf.i8.un", 0x85) +OP(CONV_OVF_U1_UN, "conv.ovf.u1.un", 0x86) +OP(CONV_OVF_U2_UN, "conv.ovf.u2.un", 0x87) +OP(CONV_OVF_U4_UN, "conv.ovf.u4.un", 0x88) +OP(CONV_OVF_U8_UN, "conv.ovf.u8.un", 0x89) +OP(CONV_OVF_I_UN, "conv.ovf.i.un", 0x8a) +OP(CONV_OVF_U_UN, "conv.ovf.u.un", 0x8b) +OP(BOX, "box", 0x8c) +OP(NEWARR, "newarr", 0x8d) +OP(LDLEN, "ldlen", 0x8e) +OP(LDELEMA, "ldelema", 0x8f) +OP(LDELEM_I1, "ldelem.i1", 0x90) +OP(LDELEM_U1, "ldelem.u1", 0x91) +OP(LDELEM_I2, "ldelem.i2", 0x92) +OP(LDELEM_U2, "ldelem.u2", 0x93) +OP(LDELEM_I4, "ldelem.i4", 0x94) +OP(LDELEM_U4, "ldelem.u4", 0x95) +OP(LDELEM_I8, "ldelem.i8", 0x96) +OP(LDELEM_I, "ldelem.i", 0x97) +OP(LDELEM_R4, "ldelem.r4", 0x98) +OP(LDELEM_R8, "ldelem.r8", 0x99) +OP(LDELEM_REF, "ldelem.ref", 0x9a) +OP(STELEM_I, "stelem.i", 0x9b) +OP(STELEM_I1, "stelem.i1", 0x9c) +OP(STELEM_I2, "stelem.i2", 0x9d) +OP(STELEM_I4, "stelem.i4", 0x9e) +OP(STELEM_I8, "stelem.i8", 0x9f) +OP(STELEM_R4, "stelem.r4", 0xa0) +OP(STELEM_R8, "stelem.r8", 0xa1) +OP(STELEM_REF, "stelem.ref", 0xa2) +OP(CONV_OVF_I1, "conv.ovf.i1", 0xb3) +OP(CONV_OVF_U1, "conv.ovf.u1", 0xb4) +OP(CONV_OVF_I2, "conv.ovf.i2", 0xb5) +OP(CONV_OVF_U2, "conv.ovf.u2", 0xb6) +OP(CONV_OVF_I4, "conv.ovf.i4", 0xb7) +OP(CONV_OVF_U4, "conv.ovf.u4", 0xb8) +OP(CONV_OVF_I8, "conv.ovf.i8", 0xb9) +OP(CONV_OVF_U8, "conv.ovf.u8", 0xba) +OP(REFANYVAL, "refanyval", 0xc2) +OP(CKFINITE, "ckfinite", 0xc3) +OP(MKREFANY, "mkrefany", 0xc6) +OP(ANN_CALL, "ann.call", 0xc7) +OP(ANN_CATCH, "ann.catch", 0xc8) +OP(ANN_DEAD, "ann.dead", 0xc9) +OP(ANN_HOISTED, "ann.hoisted", 0xca) +OP(ANN_HOISTED_CALL, "ann.hoisted.call", 0xcb) +OP(ANN_LAB, "ann.lab", 0xcc) +OP(ANN_DEF, "ann.def", 0xcd) +OP(ANN_REF_S, "ann.ref.s", 0xce) +OP(ANN_PHI, "ann.phi", 0xcf) +OP(LDTOKEN, "ldtoken", 0xd0) +OP(CONV_U2, "conv.u2", 0xd1) +OP(CONV_U1, "conv.u1", 0xd2) +OP(CONV_I, "conv.i", 0xd3) +OP(CONV_OVF_I, "conv.ovf.i", 0xd4) +OP(CONV_OVF_U, "conv.ovf.u", 0xd5) +OP(ADD_OVF, "add.ovf", 0xd6) +OP(ADD_OVF_UN, "add.ovf.un", 0xd7) +OP(MUL_OVF, "mul.ovf", 0xd8) +OP(MUL_OVF_UN, "mul.ovf.un", 0xd9) +OP(SUB_OVF, "sub.ovf", 0xda) +OP(SUB_OVF_UN, "sub.ovf.un", 0xdb) +OP(ENDFINALLY, "endfinally", 0xdc) +OP(LEAVE, "leave", 0xdd) +OP(LEAVE_S, "leave.s", 0xde) +OP(STIND_I, "stind.i", 0xdf) +OP(CONV_U, "conv.u", 0xe0) + +/* prefix instructions. we use an opcode >= 256 to ease coding */ + +OP(ARGLIST, "arglist", 0x100) +OP(CEQ, "ceq", 0x101) +OP(CGT, "cgt", 0x102) +OP(CGT_UN, "cgt.un", 0x103) +OP(CLT, "clt", 0x104) +OP(CLT_UN, "clt.un", 0x105) +OP(LDFTN, "ldftn", 0x106) +OP(LDVIRTFTN, "ldvirtftn", 0x107) +OP(JMPI, "jmpi", 0x108) +OP(LDARG, "ldarg", 0x109) +OP(LDARGA, "ldarga", 0x10a) +OP(STARG, "starg", 0x10b) +OP(LDLOC, "ldloc", 0x10c) +OP(LDLOCA, "ldloca", 0x10d) +OP(STLOC, "stloc", 0x10e) +OP(LOCALLOC, "localloc", 0x10f) +OP(ENDFILTER, "endfilter", 0x111) +OP(UNALIGNED, "unaligned", 0x112) +OP(VOLATILE, "volatile", 0x113) +OP(TAIL, "tail", 0x114) +OP(INITOBJ, "initobj", 0x115) +OP(ANN_LIVE, "ann.live", 0x116) +OP(CPBLK, "cpblk", 0x117) +OP(INITBLK, "initblk", 0x118) +OP(ANN_REF, "ann.ref", 0x119) +OP(RETHROW, "rethrow", 0x11a) +OP(SIZEOF, "sizeof", 0x11c) +OP(REFANYTYPE, "refanytype", 0x11d) +OP(ANN_DATA, "ann.data", 0x122) +OP(ANN_ARG, "ann.arg", 0x123) diff --git a/05/tcc-final-old/include/float.h b/05/tcc-final-old/include/float.h new file mode 100644 index 0000000..f16f1f0 --- /dev/null +++ b/05/tcc-final-old/include/float.h @@ -0,0 +1,57 @@ +#ifndef _FLOAT_H_ +#define _FLOAT_H_ + +#define FLT_RADIX 2 + +/* IEEE float */ +#define FLT_MANT_DIG 24 +#define FLT_DIG 6 +#define FLT_ROUNDS 1 +#define FLT_EPSILON 1.19209290e-07F +#define FLT_MIN_EXP (-125) +#define FLT_MIN 1.17549435e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_EXP 128 +#define FLT_MAX 3.40282347e+38F +#define FLT_MAX_10_EXP 38 + +/* IEEE double */ +#define DBL_MANT_DIG 53 +#define DBL_DIG 15 +#define DBL_EPSILON 2.2204460492503131e-16 +#define DBL_MIN_EXP (-1021) +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_EXP 1024 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_MAX_10_EXP 308 + +/* horrible intel long double */ +#if defined __i386__ || defined __x86_64__ + +#define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define LDBL_EPSILON 1.08420217248550443401e-19L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176502e+4932L +#define LDBL_MAX_10_EXP 4932 + +#else + +/* same as IEEE double */ +#define LDBL_MANT_DIG 53 +#define LDBL_DIG 15 +#define LDBL_EPSILON 2.2204460492503131e-16 +#define LDBL_MIN_EXP (-1021) +#define LDBL_MIN 2.2250738585072014e-308 +#define LDBL_MIN_10_EXP (-307) +#define LDBL_MAX_EXP 1024 +#define LDBL_MAX 1.7976931348623157e+308 +#define LDBL_MAX_10_EXP 308 + +#endif + +#endif /* _FLOAT_H_ */ diff --git a/05/tcc-final-old/include/stdarg.h b/05/tcc-final-old/include/stdarg.h new file mode 100644 index 0000000..10ce733 --- /dev/null +++ b/05/tcc-final-old/include/stdarg.h @@ -0,0 +1,79 @@ +#ifndef _STDARG_H +#define _STDARG_H + +#ifdef __x86_64__ +#ifndef _WIN64 + +//This should be in sync with the declaration on our lib/libtcc1.c +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; + +typedef __va_list_struct va_list[1]; + +void __va_start(__va_list_struct *ap, void *fp); +void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align); + +#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0)) +#define va_arg(ap, type) \ + (*(type *)(__va_arg(ap, __builtin_va_arg_types(type), sizeof(type), __alignof__(type)))) +#define va_copy(dest, src) (*(dest) = *(src)) +#define va_end(ap) + +/* avoid conflicting definition for va_list on Macs. */ +#define _VA_LIST_T + +#else /* _WIN64 */ +typedef char *va_list; +#define va_start(ap,last) __builtin_va_start(ap,last) +#define va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \ + ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8)) +#define va_copy(dest, src) ((dest) = (src)) +#define va_end(ap) +#endif + +#elif __arm__ +typedef char *va_list; +#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) +#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) + +#elif defined(__aarch64__) +typedef struct { + void *__stack; + void *__gr_top; + void *__vr_top; + int __gr_offs; + int __vr_offs; +} va_list; +#define va_start(ap, last) __va_start(ap, last) +#define va_arg(ap, type) __va_arg(ap, type) +#define va_end(ap) +#define va_copy(dest, src) ((dest) = (src)) + +#else /* __i386__ */ +typedef char *va_list; +/* only correct for i386 */ +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) +#endif + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED + +#endif /* _STDARG_H */ diff --git a/05/tcc-final-old/include/stdbool.h b/05/tcc-final-old/include/stdbool.h new file mode 100644 index 0000000..d2ee446 --- /dev/null +++ b/05/tcc-final-old/include/stdbool.h @@ -0,0 +1,11 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +/* ISOC99 boolean */ + +#define bool _Bool +#define true 1 +#define false 0 +#define __bool_true_false_are_defined 1 + +#endif /* _STDBOOL_H */ diff --git a/05/tcc-final-old/include/stddef.h b/05/tcc-final-old/include/stddef.h new file mode 100644 index 0000000..694d503 --- /dev/null +++ b/05/tcc-final-old/include/stddef.h @@ -0,0 +1,54 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +typedef __SIZE_TYPE__ size_t; +typedef __PTRDIFF_TYPE__ ssize_t; +typedef __WCHAR_TYPE__ wchar_t; +typedef __PTRDIFF_TYPE__ ptrdiff_t; +typedef __PTRDIFF_TYPE__ intptr_t; +typedef __SIZE_TYPE__ uintptr_t; + +#ifndef __int8_t_defined +#define __int8_t_defined +typedef signed char int8_t; +typedef signed short int int16_t; +typedef signed int int32_t; +#ifdef __LP64__ +typedef signed long int int64_t; +#else +typedef signed long long int int64_t; +#endif +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +#ifdef __LP64__ +typedef unsigned long int uint64_t; +#else +typedef unsigned long long int uint64_t; +#endif +#endif + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#define offsetof(type, field) ((size_t)&((type *)0)->field) + +void *alloca(size_t size); + +#endif + +/* Older glibc require a wint_t from (when requested + by __need_wint_t, as otherwise stddef.h isn't allowed to + define this type). Note that this must be outside the normal + _STDDEF_H guard, so that it works even when we've included the file + already (without requiring wint_t). Some other libs define _WINT_T + if they've already provided that type, so we can use that as guard. + TCC defines __WINT_TYPE__ for us. */ +#if defined (__need_wint_t) +#ifndef _WINT_T +#define _WINT_T +typedef __WINT_TYPE__ wint_t; +#endif +#undef __need_wint_t +#endif diff --git a/05/tcc-final-old/include/varargs.h b/05/tcc-final-old/include/varargs.h new file mode 100644 index 0000000..d614366 --- /dev/null +++ b/05/tcc-final-old/include/varargs.h @@ -0,0 +1,12 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _VARARGS_H +#define _VARARGS_H + +#error "TinyCC no longer implements ." +#error "Revise your code to use ." + +#endif diff --git a/05/tcc-final-old/lib/Makefile b/05/tcc-final-old/lib/Makefile new file mode 100644 index 0000000..0c1ec54 --- /dev/null +++ b/05/tcc-final-old/lib/Makefile @@ -0,0 +1,73 @@ +# +# Tiny C Compiler Makefile for libtcc1.a +# + +TOP = .. +include $(TOP)/Makefile +VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib +T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown) +X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-) +BIN = $(TOP)/$(X)libtcc1.a + +XTCC ?= $(TOP)/$(X)tcc$(EXESUF) +XCC = $(XTCC) +XAR = $(XTCC) -ar +XFLAGS-unx = -B$(TOPSRC) +XFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include +XFLAGS = $(XFLAGS$(XCFG)) +XCFG = $(or $(findstring -win,$T),-unx) + +# in order to use gcc, tyoe: make -libtcc1-usegcc=yes +arm-libtcc1-usegcc ?= no + +ifeq "$($(T)-libtcc1-usegcc)" "yes" + XCC = $(CC) + XAR = $(AR) + XFLAGS = $(CFLAGS) -fPIC +endif + +# only for native compiler +$(X)BCHECK_O = bcheck.o + +ifeq ($(CONFIG_musl)$(CONFIG_uClibc),yes) + BCHECK_O = +endif + +ifdef CONFIG_OSX + XFLAGS += -D_ANSI_SOURCE +endif + +I386_O = libtcc1.o alloca86.o alloca86-bt.o +X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o +ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o +ARM64_O = lib-arm64.o +WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o + +OBJ-i386 = $(I386_O) $(BCHECK_O) +OBJ-x86_64 = $(X86_64_O) va_list.o $(BCHECK_O) +OBJ-x86_64-osx = $(X86_64_O) va_list.o +OBJ-i386-win32 = $(I386_O) chkstk.o bcheck.o $(WIN_O) +OBJ-x86_64-win32 = $(X86_64_O) chkstk.o bcheck.o $(WIN_O) +OBJ-arm64 = $(ARM64_O) +OBJ-arm = $(ARM_O) +OBJ-arm-fpa = $(ARM_O) +OBJ-arm-fpa-ld = $(ARM_O) +OBJ-arm-vfp = $(ARM_O) +OBJ-arm-eabi = $(ARM_O) +OBJ-arm-eabihf = $(ARM_O) +OBJ-arm-wince = $(ARM_O) $(WIN_O) + +$(BIN) : $(patsubst %.o,$(X)%.o,$(OBJ-$T)) + $(XAR) rcs $@ $^ + +$(X)%.o : %.c + $(XCC) -c $< -o $@ $(XFLAGS) + +$(X)%.o : %.S + $(XCC) -c $< -o $@ $(XFLAGS) + +$(X)crt1w.o : crt1.c +$(X)wincrt1w.o : wincrt1.c + +clean : + rm -f *.a *.o $(BIN) diff --git a/05/tcc-final-old/lib/alloca-arm.S b/05/tcc-final-old/lib/alloca-arm.S new file mode 100644 index 0000000..68556e3 --- /dev/null +++ b/05/tcc-final-old/lib/alloca-arm.S @@ -0,0 +1,17 @@ + .text + .align 2 + .global alloca + .type alloca, %function +alloca: +#ifdef __TINYC__ + .int 0xe060d00d + .int 0xe3cdd007 + .int 0xe1a0000d + .int 0xe1a0f00e +#else + rsb sp, r0, sp + bic sp, sp, #7 + mov r0, sp + mov pc, lr +#endif + .size alloca, .-alloca diff --git a/05/tcc-final-old/lib/alloca86-bt.S b/05/tcc-final-old/lib/alloca86-bt.S new file mode 100644 index 0000000..4f95cf1 --- /dev/null +++ b/05/tcc-final-old/lib/alloca86-bt.S @@ -0,0 +1,47 @@ +/* ---------------------------------------------- */ +/* alloca86-bt.S */ + +.globl __bound_alloca + +__bound_alloca: + pop %edx + pop %eax + mov %eax, %ecx + add $3,%eax + and $-4,%eax + jz p6 + +#ifdef _WIN32 +p4: + cmp $4096,%eax + jbe p5 + test %eax,-4096(%esp) + sub $4096,%esp + sub $4096,%eax + jmp p4 + +p5: +#endif + + sub %eax,%esp + mov %esp,%eax + + push %edx + push %eax + push %ecx + push %eax + call __bound_new_region + add $8, %esp + pop %eax + pop %edx + +p6: + push %edx + push %edx + ret + +/* mark stack as nonexecutable */ +#if defined __ELF__ && defined __linux__ + .section .note.GNU-stack,"",@progbits +#endif +/* ---------------------------------------------- */ diff --git a/05/tcc-final-old/lib/alloca86.S b/05/tcc-final-old/lib/alloca86.S new file mode 100644 index 0000000..bb7a2c2 --- /dev/null +++ b/05/tcc-final-old/lib/alloca86.S @@ -0,0 +1,31 @@ +/* ---------------------------------------------- */ +/* alloca86.S */ + +.globl alloca + +alloca: + pop %edx + pop %eax + add $3,%eax + and $-4,%eax + jz p3 + +#ifdef _WIN32 +p1: + cmp $4096,%eax + jbe p2 + test %eax,-4096(%esp) + sub $4096,%esp + sub $4096,%eax + jmp p1 +p2: +#endif + + sub %eax,%esp + mov %esp,%eax +p3: + push %edx + push %edx + ret + +/* ---------------------------------------------- */ diff --git a/05/tcc-final-old/lib/alloca86_64-bt.S b/05/tcc-final-old/lib/alloca86_64-bt.S new file mode 100644 index 0000000..4cbad90 --- /dev/null +++ b/05/tcc-final-old/lib/alloca86_64-bt.S @@ -0,0 +1,56 @@ +/* ---------------------------------------------- */ +/* alloca86_64.S */ + +.globl __bound_alloca +__bound_alloca: + +#ifdef _WIN32 + # bound checking is not implemented + pop %rdx + mov %rcx,%rax + add $15,%rax + and $-16,%rax + jz p3 + +p1: + cmp $4096,%rax + jbe p2 + test %rax,-4096(%rsp) + sub $4096,%rsp + sub $4096,%rax + jmp p1 +p2: + + sub %rax,%rsp + mov %rsp,%rax + add $32,%rax + +p3: + push %rdx + ret +#else + pop %rdx + mov %rdi,%rax + mov %rax,%rsi # size, a second parm to the __bound_new_region + + add $15,%rax + and $-16,%rax + jz p3 + + + sub %rax,%rsp + mov %rsp,%rdi # pointer, a first parm to the __bound_new_region + mov %rsp,%rax + + push %rdx + push %rax + call __bound_new_region + pop %rax + pop %rdx + +p3: + push %rdx + ret +#endif + +/* ---------------------------------------------- */ diff --git a/05/tcc-final-old/lib/alloca86_64.S b/05/tcc-final-old/lib/alloca86_64.S new file mode 100644 index 0000000..ae3c97d --- /dev/null +++ b/05/tcc-final-old/lib/alloca86_64.S @@ -0,0 +1,34 @@ +/* ---------------------------------------------- */ +/* alloca86_64.S */ + +.globl alloca + +alloca: + pop %rdx +#ifdef _WIN32 + mov %rcx,%rax +#else + mov %rdi,%rax +#endif + add $15,%rax + and $-16,%rax + jz p3 + +#ifdef _WIN32 +p1: + cmp $4096,%rax + jbe p2 + test %rax,-4096(%rsp) + sub $4096,%rsp + sub $4096,%rax + jmp p1 +p2: +#endif + + sub %rax,%rsp + mov %rsp,%rax +p3: + push %rdx + ret + +/* ---------------------------------------------- */ diff --git a/05/tcc-final-old/lib/armeabi.c b/05/tcc-final-old/lib/armeabi.c new file mode 100644 index 0000000..a59640d --- /dev/null +++ b/05/tcc-final-old/lib/armeabi.c @@ -0,0 +1,501 @@ +/* TCC ARM runtime EABI + Copyright (C) 2013 Thomas Preud'homme + +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 __TINYC__ +#define INT_MIN (-2147483647 - 1) +#define INT_MAX 2147483647 +#define UINT_MAX 0xffffffff +#define LONG_MIN (-2147483647L - 1) +#define LONG_MAX 2147483647L +#define ULONG_MAX 0xffffffffUL +#define LLONG_MAX 9223372036854775807LL +#define LLONG_MIN (-9223372036854775807LL - 1) +#define ULLONG_MAX 0xffffffffffffffffULL +#else +#include +#endif + +/* We rely on the little endianness and EABI calling convention for this to + work */ + +typedef struct double_unsigned_struct { + unsigned low; + unsigned high; +} double_unsigned_struct; + +typedef struct unsigned_int_struct { + unsigned low; + int high; +} unsigned_int_struct; + +#define REGS_RETURN(name, type) \ + void name ## _return(type ret) {} + + +/* Float helper functions */ + +#define FLOAT_EXP_BITS 8 +#define FLOAT_FRAC_BITS 23 + +#define DOUBLE_EXP_BITS 11 +#define DOUBLE_FRAC_BITS 52 + +#define ONE_EXP(type) ((1 << (type ## _EXP_BITS - 1)) - 1) + +REGS_RETURN(unsigned_int_struct, unsigned_int_struct) +REGS_RETURN(double_unsigned_struct, double_unsigned_struct) + +/* float -> integer: (sign) 1.fraction x 2^(exponent - exp_for_one) */ + + +/* float to [unsigned] long long conversion */ +#define DEFINE__AEABI_F2XLZ(name, with_sign) \ +void __aeabi_ ## name(unsigned val) \ +{ \ + int exp, high_shift, sign; \ + double_unsigned_struct ret; \ + \ + /* compute sign */ \ + sign = val >> 31; \ + \ + /* compute real exponent */ \ + exp = val >> FLOAT_FRAC_BITS; \ + exp &= (1 << FLOAT_EXP_BITS) - 1; \ + exp -= ONE_EXP(FLOAT); \ + \ + /* undefined behavior if truncated value cannot be represented */ \ + if (with_sign) { \ + if (exp > 62) /* |val| too big, double cannot represent LLONG_MAX */ \ + return; \ + } else { \ + if ((sign && exp >= 0) || exp > 63) /* if val < 0 || val too big */ \ + return; \ + } \ + \ + val &= (1 << FLOAT_FRAC_BITS) - 1; \ + if (exp >= 32) { \ + ret.high = 1 << (exp - 32); \ + if (exp - 32 >= FLOAT_FRAC_BITS) { \ + ret.high |= val << (exp - 32 - FLOAT_FRAC_BITS); \ + ret.low = 0; \ + } else { \ + high_shift = FLOAT_FRAC_BITS - (exp - 32); \ + ret.high |= val >> high_shift; \ + ret.low = val << (32 - high_shift); \ + } \ + } else { \ + ret.high = 0; \ + ret.low = 1 << exp; \ + if (exp > FLOAT_FRAC_BITS) \ + ret.low |= val << (exp - FLOAT_FRAC_BITS); \ + else \ + ret.low |= val >> (FLOAT_FRAC_BITS - exp); \ + } \ + \ + /* encode negative integer using 2's complement */ \ + if (with_sign && sign) { \ + ret.low = ~ret.low; \ + ret.high = ~ret.high; \ + if (ret.low == UINT_MAX) { \ + ret.low = 0; \ + ret.high++; \ + } else \ + ret.low++; \ + } \ + \ + double_unsigned_struct_return(ret); \ +} + +/* float to unsigned long long conversion */ +DEFINE__AEABI_F2XLZ(f2ulz, 0) + +/* float to long long conversion */ +DEFINE__AEABI_F2XLZ(f2lz, 1) + +/* double to [unsigned] long long conversion */ +#define DEFINE__AEABI_D2XLZ(name, with_sign) \ +void __aeabi_ ## name(double_unsigned_struct val) \ +{ \ + int exp, high_shift, sign; \ + double_unsigned_struct ret; \ + \ + /* compute sign */ \ + sign = val.high >> 31; \ + \ + /* compute real exponent */ \ + exp = (val.high >> (DOUBLE_FRAC_BITS - 32)); \ + exp &= (1 << DOUBLE_EXP_BITS) - 1; \ + exp -= ONE_EXP(DOUBLE); \ + \ + /* undefined behavior if truncated value cannot be represented */ \ + if (with_sign) { \ + if (exp > 62) /* |val| too big, double cannot represent LLONG_MAX */ \ + return; \ + } else { \ + if ((sign && exp >= 0) || exp > 63) /* if val < 0 || val too big */ \ + return; \ + } \ + \ + val.high &= (1 << (DOUBLE_FRAC_BITS - 32)) - 1; \ + if (exp >= 32) { \ + ret.high = 1 << (exp - 32); \ + if (exp >= DOUBLE_FRAC_BITS) { \ + high_shift = exp - DOUBLE_FRAC_BITS; \ + ret.high |= val.high << high_shift; \ + ret.high |= val.low >> (32 - high_shift); \ + ret.low = val.low << high_shift; \ + } else { \ + high_shift = DOUBLE_FRAC_BITS - exp; \ + ret.high |= val.high >> high_shift; \ + ret.low = val.high << (32 - high_shift); \ + ret.low |= val.low >> high_shift; \ + } \ + } else { \ + ret.high = 0; \ + ret.low = 1 << exp; \ + if (exp > DOUBLE_FRAC_BITS - 32) { \ + high_shift = exp - DOUBLE_FRAC_BITS - 32; \ + ret.low |= val.high << high_shift; \ + ret.low |= val.low >> (32 - high_shift); \ + } else \ + ret.low |= val.high >> (DOUBLE_FRAC_BITS - 32 - exp); \ + } \ + \ + /* encode negative integer using 2's complement */ \ + if (with_sign && sign) { \ + ret.low = ~ret.low; \ + ret.high = ~ret.high; \ + if (ret.low == UINT_MAX) { \ + ret.low = 0; \ + ret.high++; \ + } else \ + ret.low++; \ + } \ + \ + double_unsigned_struct_return(ret); \ +} + +/* double to unsigned long long conversion */ +DEFINE__AEABI_D2XLZ(d2ulz, 0) + +/* double to long long conversion */ +DEFINE__AEABI_D2XLZ(d2lz, 1) + +/* long long to float conversion */ +#define DEFINE__AEABI_XL2F(name, with_sign) \ +unsigned __aeabi_ ## name(unsigned long long v) \ +{ \ + int s /* shift */, flb /* first lost bit */, sign = 0; \ + unsigned p = 0 /* power */, ret; \ + double_unsigned_struct val; \ + \ + /* fraction in negative float is encoded in 1's complement */ \ + if (with_sign && (v & (1ULL << 63))) { \ + sign = 1; \ + v = ~v + 1; \ + } \ + val.low = v; \ + val.high = v >> 32; \ + /* fill fraction bits */ \ + for (s = 31, p = 1 << 31; p && !(val.high & p); s--, p >>= 1); \ + if (p) { \ + ret = val.high & (p - 1); \ + if (s < FLOAT_FRAC_BITS) { \ + ret <<= FLOAT_FRAC_BITS - s; \ + ret |= val.low >> (32 - (FLOAT_FRAC_BITS - s)); \ + flb = (val.low >> (32 - (FLOAT_FRAC_BITS - s - 1))) & 1; \ + } else { \ + flb = (ret >> (s - FLOAT_FRAC_BITS - 1)) & 1; \ + ret >>= s - FLOAT_FRAC_BITS; \ + } \ + s += 32; \ + } else { \ + for (s = 31, p = 1 << 31; p && !(val.low & p); s--, p >>= 1); \ + if (p) { \ + ret = val.low & (p - 1); \ + if (s <= FLOAT_FRAC_BITS) { \ + ret <<= FLOAT_FRAC_BITS - s; \ + flb = 0; \ + } else { \ + flb = (ret >> (s - FLOAT_FRAC_BITS - 1)) & 1; \ + ret >>= s - FLOAT_FRAC_BITS; \ + } \ + } else \ + return 0; \ + } \ + if (flb) \ + ret++; \ + \ + /* fill exponent bits */ \ + ret |= (s + ONE_EXP(FLOAT)) << FLOAT_FRAC_BITS; \ + \ + /* fill sign bit */ \ + ret |= sign << 31; \ + \ + return ret; \ +} + +/* unsigned long long to float conversion */ +DEFINE__AEABI_XL2F(ul2f, 0) + +/* long long to float conversion */ +DEFINE__AEABI_XL2F(l2f, 1) + +/* long long to double conversion */ +#define __AEABI_XL2D(name, with_sign) \ +void __aeabi_ ## name(unsigned long long v) \ +{ \ + int s /* shift */, high_shift, sign = 0; \ + unsigned tmp, p = 0; \ + double_unsigned_struct val, ret; \ + \ + /* fraction in negative float is encoded in 1's complement */ \ + if (with_sign && (v & (1ULL << 63))) { \ + sign = 1; \ + v = ~v + 1; \ + } \ + val.low = v; \ + val.high = v >> 32; \ + \ + /* fill fraction bits */ \ + for (s = 31, p = 1 << 31; p && !(val.high & p); s--, p >>= 1); \ + if (p) { \ + tmp = val.high & (p - 1); \ + if (s < DOUBLE_FRAC_BITS - 32) { \ + high_shift = DOUBLE_FRAC_BITS - 32 - s; \ + ret.high = tmp << high_shift; \ + ret.high |= val.low >> (32 - high_shift); \ + ret.low = val.low << high_shift; \ + } else { \ + high_shift = s - (DOUBLE_FRAC_BITS - 32); \ + ret.high = tmp >> high_shift; \ + ret.low = tmp << (32 - high_shift); \ + ret.low |= val.low >> high_shift; \ + if ((val.low >> (high_shift - 1)) & 1) { \ + if (ret.low == UINT_MAX) { \ + ret.high++; \ + ret.low = 0; \ + } else \ + ret.low++; \ + } \ + } \ + s += 32; \ + } else { \ + for (s = 31, p = 1 << 31; p && !(val.low & p); s--, p >>= 1); \ + if (p) { \ + tmp = val.low & (p - 1); \ + if (s <= DOUBLE_FRAC_BITS - 32) { \ + high_shift = DOUBLE_FRAC_BITS - 32 - s; \ + ret.high = tmp << high_shift; \ + ret.low = 0; \ + } else { \ + high_shift = s - (DOUBLE_FRAC_BITS - 32); \ + ret.high = tmp >> high_shift; \ + ret.low = tmp << (32 - high_shift); \ + } \ + } else { \ + ret.high = ret.low = 0; \ + double_unsigned_struct_return(ret); \ + } \ + } \ + \ + /* fill exponent bits */ \ + ret.high |= (s + ONE_EXP(DOUBLE)) << (DOUBLE_FRAC_BITS - 32); \ + \ + /* fill sign bit */ \ + ret.high |= sign << 31; \ + \ + double_unsigned_struct_return(ret); \ +} + +/* unsigned long long to double conversion */ +__AEABI_XL2D(ul2d, 0) + +/* long long to double conversion */ +__AEABI_XL2D(l2d, 1) + + +/* Long long helper functions */ + +/* TODO: add error in case of den == 0 (see §4.3.1 and §4.3.2) */ + +#define define_aeabi_xdivmod_signed_type(basetype, type) \ +typedef struct type { \ + basetype quot; \ + unsigned basetype rem; \ +} type + +#define define_aeabi_xdivmod_unsigned_type(basetype, type) \ +typedef struct type { \ + basetype quot; \ + basetype rem; \ +} type + +#define AEABI_UXDIVMOD(name,type, rettype, typemacro) \ +static inline rettype aeabi_ ## name (type num, type den) \ +{ \ + rettype ret; \ + type quot = 0; \ + \ + /* Increase quotient while it is less than numerator */ \ + while (num >= den) { \ + type q = 1; \ + \ + /* Find closest power of two */ \ + while ((q << 1) * den <= num && q * den <= typemacro ## _MAX / 2) \ + q <<= 1; \ + \ + /* Compute difference between current quotient and numerator */ \ + num -= q * den; \ + quot += q; \ + } \ + ret.quot = quot; \ + ret.rem = num; \ + return ret; \ +} + +#define __AEABI_XDIVMOD(name, type, uiname, rettype, urettype, typemacro) \ +void __aeabi_ ## name(type numerator, type denominator) \ +{ \ + unsigned type num, den; \ + urettype uxdiv_ret; \ + rettype ret; \ + \ + if (numerator >= 0) \ + num = numerator; \ + else \ + num = 0 - numerator; \ + if (denominator >= 0) \ + den = denominator; \ + else \ + den = 0 - denominator; \ + uxdiv_ret = aeabi_ ## uiname(num, den); \ + /* signs differ */ \ + if ((numerator & typemacro ## _MIN) != (denominator & typemacro ## _MIN)) \ + ret.quot = 0 - uxdiv_ret.quot; \ + else \ + ret.quot = uxdiv_ret.quot; \ + if (numerator < 0) \ + ret.rem = 0 - uxdiv_ret.rem; \ + else \ + ret.rem = uxdiv_ret.rem; \ + \ + rettype ## _return(ret); \ +} + +define_aeabi_xdivmod_signed_type(long long, lldiv_t); +define_aeabi_xdivmod_unsigned_type(unsigned long long, ulldiv_t); +define_aeabi_xdivmod_signed_type(int, idiv_t); +define_aeabi_xdivmod_unsigned_type(unsigned, uidiv_t); + +REGS_RETURN(lldiv_t, lldiv_t) +REGS_RETURN(ulldiv_t, ulldiv_t) +REGS_RETURN(idiv_t, idiv_t) +REGS_RETURN(uidiv_t, uidiv_t) + +AEABI_UXDIVMOD(uldivmod, unsigned long long, ulldiv_t, ULLONG) + +__AEABI_XDIVMOD(ldivmod, long long, uldivmod, lldiv_t, ulldiv_t, LLONG) + +void __aeabi_uldivmod(unsigned long long num, unsigned long long den) +{ + ulldiv_t_return(aeabi_uldivmod(num, den)); +} + +void __aeabi_llsl(double_unsigned_struct val, int shift) +{ + double_unsigned_struct ret; + + if (shift >= 32) { + val.high = val.low; + val.low = 0; + shift -= 32; + } + if (shift > 0) { + ret.low = val.low << shift; + ret.high = (val.high << shift) | (val.low >> (32 - shift)); + double_unsigned_struct_return(ret); + return; + } + double_unsigned_struct_return(val); +} + +#define aeabi_lsr(val, shift, fill, type) \ + type ## _struct ret; \ + \ + if (shift >= 32) { \ + val.low = val.high; \ + val.high = fill; \ + shift -= 32; \ + } \ + if (shift > 0) { \ + ret.high = val.high >> shift; \ + ret.low = (val.high << (32 - shift)) | (val.low >> shift); \ + type ## _struct_return(ret); \ + return; \ + } \ + type ## _struct_return(val); + +void __aeabi_llsr(double_unsigned_struct val, int shift) +{ + aeabi_lsr(val, shift, 0, double_unsigned); +} + +void __aeabi_lasr(unsigned_int_struct val, int shift) +{ + aeabi_lsr(val, shift, val.high >> 31, unsigned_int); +} + + +/* Integer division functions */ + +AEABI_UXDIVMOD(uidivmod, unsigned, uidiv_t, UINT) + +int __aeabi_idiv(int numerator, int denominator) +{ + unsigned num, den; + uidiv_t ret; + + if (numerator >= 0) + num = numerator; + else + num = 0 - numerator; + if (denominator >= 0) + den = denominator; + else + den = 0 - denominator; + ret = aeabi_uidivmod(num, den); + if ((numerator & INT_MIN) != (denominator & INT_MIN)) /* signs differ */ + ret.quot *= -1; + return ret.quot; +} + +unsigned __aeabi_uidiv(unsigned num, unsigned den) +{ + return aeabi_uidivmod(num, den).quot; +} + +__AEABI_XDIVMOD(idivmod, int, uidivmod, idiv_t, uidiv_t, INT) + +void __aeabi_uidivmod(unsigned num, unsigned den) +{ + uidiv_t_return(aeabi_uidivmod(num, den)); +} diff --git a/05/tcc-final-old/lib/armflush.c b/05/tcc-final-old/lib/armflush.c new file mode 100644 index 0000000..eae3260 --- /dev/null +++ b/05/tcc-final-old/lib/armflush.c @@ -0,0 +1,58 @@ +/* armflush.c - flush the instruction cache + + __clear_cache is used in tccrun.c, It is a built-in + intrinsic with gcc. However tcc in order to compile + itself needs this function */ + +#ifdef __TINYC__ + +/* syscall wrapper */ +unsigned syscall(unsigned syscall_nr, ...); + +/* arm-tcc supports only fake asm currently */ +__asm__( + ".global syscall\n" + "syscall:\n" + ".int 0xe92d4080\n" // push {r7, lr} + ".int 0xe1a07000\n" // mov r7, r0 + ".int 0xe1a00001\n" // mov r0, r1 + ".int 0xe1a01002\n" // mov r1, r2 + ".int 0xe1a02003\n" // mov r2, r3 + ".int 0xef000000\n" // svc 0x00000000 + ".int 0xe8bd8080\n" // pop {r7, pc} + ); + +/* from unistd.h: */ +#if defined(__thumb__) || defined(__ARM_EABI__) +# define __NR_SYSCALL_BASE 0x0 +#else +# define __NR_SYSCALL_BASE 0x900000 +#endif +#define __ARM_NR_BASE (__NR_SYSCALL_BASE+0x0f0000) +#define __ARM_NR_cacheflush (__ARM_NR_BASE+2) + +#else + +#define _GNU_SOURCE +#include +#include +#include + +#endif + +/* Flushing for tccrun */ +void __clear_cache(void *beginning, void *end) +{ +/* __ARM_NR_cacheflush is kernel private and should not be used in user space. + * However, there is no ARM asm parser in tcc so we use it for now */ +#if 1 + syscall(__ARM_NR_cacheflush, beginning, end, 0); +#else + __asm__ ("push {r7}\n\t" + "mov r7, #0xf0002\n\t" + "mov r2, #0\n\t" + "swi 0\n\t" + "pop {r7}\n\t" + "ret"); +#endif +} diff --git a/05/tcc-final-old/lib/bcheck.c b/05/tcc-final-old/lib/bcheck.c new file mode 100644 index 0000000..90f0ad2 --- /dev/null +++ b/05/tcc-final-old/lib/bcheck.c @@ -0,0 +1,979 @@ +/* + * Tiny C Memory and bounds checker + * + * Copyright (c) 2002 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include +#include +#include + +#if !defined(__FreeBSD__) \ + && !defined(__FreeBSD_kernel__) \ + && !defined(__DragonFly__) \ + && !defined(__OpenBSD__) \ + && !defined(__NetBSD__) +#include +#endif + +#if !defined(_WIN32) +#include +#endif + +/* #define BOUND_DEBUG */ + +#ifdef BOUND_DEBUG + #define dprintf(a...) fprintf(a) +#else + #define dprintf(a...) +#endif + +/* define so that bound array is static (faster, but use memory if + bound checking not used) */ +/* #define BOUND_STATIC */ + +/* use malloc hooks. Currently the code cannot be reliable if no hooks */ +#define CONFIG_TCC_MALLOC_HOOKS +#define HAVE_MEMALIGN + +#if defined(__FreeBSD__) \ + || defined(__FreeBSD_kernel__) \ + || defined(__DragonFly__) \ + || defined(__OpenBSD__) \ + || defined(__NetBSD__) \ + || defined(__dietlibc__) \ + || defined(_WIN32) +//#warning Bound checking does not support malloc (etc.) in this environment. +#undef CONFIG_TCC_MALLOC_HOOKS +#undef HAVE_MEMALIGN +#endif + +#define BOUND_T1_BITS 13 +#define BOUND_T2_BITS 11 +#define BOUND_T3_BITS (sizeof(size_t)*8 - BOUND_T1_BITS - BOUND_T2_BITS) +#define BOUND_E_BITS (sizeof(size_t)) + +#define BOUND_T1_SIZE ((size_t)1 << BOUND_T1_BITS) +#define BOUND_T2_SIZE ((size_t)1 << BOUND_T2_BITS) +#define BOUND_T3_SIZE ((size_t)1 << BOUND_T3_BITS) + +#define BOUND_T23_BITS (BOUND_T2_BITS + BOUND_T3_BITS) +#define BOUND_T23_SIZE ((size_t)1 << BOUND_T23_BITS) + + +/* this pointer is generated when bound check is incorrect */ +#define INVALID_POINTER ((void *)(-2)) +/* size of an empty region */ +#define EMPTY_SIZE ((size_t)(-1)) +/* size of an invalid region */ +#define INVALID_SIZE 0 + +typedef struct BoundEntry { + size_t start; + size_t size; + struct BoundEntry *next; + size_t is_invalid; /* true if pointers outside region are invalid */ +} BoundEntry; + +/* external interface */ +void __bound_init(void); +void __bound_new_region(void *p, size_t size); +int __bound_delete_region(void *p); + +#ifdef __attribute__ + /* an __attribute__ macro is defined in the system headers */ + #undef __attribute__ +#endif +#define FASTCALL __attribute__((regparm(3))) + +void *__bound_malloc(size_t size, const void *caller); +void *__bound_memalign(size_t size, size_t align, const void *caller); +void __bound_free(void *ptr, const void *caller); +void *__bound_realloc(void *ptr, size_t size, const void *caller); +static void *libc_malloc(size_t size); +static void libc_free(void *ptr); +static void install_malloc_hooks(void); +static void restore_malloc_hooks(void); + +#ifdef CONFIG_TCC_MALLOC_HOOKS +static void *saved_malloc_hook; +static void *saved_free_hook; +static void *saved_realloc_hook; +static void *saved_memalign_hook; +#endif + +/* TCC definitions */ +extern char __bounds_start; /* start of static bounds table */ +/* error message, just for TCC */ +const char *__bound_error_msg; + +/* runtime error output */ +extern void rt_error(size_t pc, const char *fmt, ...); + +#ifdef BOUND_STATIC +static BoundEntry *__bound_t1[BOUND_T1_SIZE]; /* page table */ +#else +static BoundEntry **__bound_t1; /* page table */ +#endif +static BoundEntry *__bound_empty_t2; /* empty page, for unused pages */ +static BoundEntry *__bound_invalid_t2; /* invalid page, for invalid pointers */ + +static BoundEntry *__bound_find_region(BoundEntry *e1, void *p) +{ + size_t addr, tmp; + BoundEntry *e; + + e = e1; + while (e != NULL) { + addr = (size_t)p; + addr -= e->start; + if (addr <= e->size) { + /* put region at the head */ + tmp = e1->start; + e1->start = e->start; + e->start = tmp; + tmp = e1->size; + e1->size = e->size; + e->size = tmp; + return e1; + } + e = e->next; + } + /* no entry found: return empty entry or invalid entry */ + if (e1->is_invalid) + return __bound_invalid_t2; + else + return __bound_empty_t2; +} + +/* print a bound error message */ +static void bound_error(const char *fmt, ...) +{ + __bound_error_msg = fmt; + fprintf(stderr,"%s %s: %s\n", __FILE__, __FUNCTION__, fmt); + *(void **)0 = 0; /* force a runtime error */ +} + +static void bound_alloc_error(void) +{ + bound_error("not enough memory for bound checking code"); +} + +/* return '(p + offset)' for pointer arithmetic (a pointer can reach + the end of a region in this case */ +void * FASTCALL __bound_ptr_add(void *p, size_t offset) +{ + size_t addr = (size_t)p; + BoundEntry *e; + + dprintf(stderr, "%s %s: %p %x\n", + __FILE__, __FUNCTION__, p, (unsigned)offset); + + __bound_init(); + + e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; + e = (BoundEntry *)((char *)e + + ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); + addr -= e->start; + if (addr > e->size) { + e = __bound_find_region(e, p); + addr = (size_t)p - e->start; + } + addr += offset; + if (addr >= e->size) { + fprintf(stderr,"%s %s: %p is outside of the region\n", + __FILE__, __FUNCTION__, p + offset); + return INVALID_POINTER; /* return an invalid pointer */ + } + return p + offset; +} + +/* return '(p + offset)' for pointer indirection (the resulting must + be strictly inside the region */ +#define BOUND_PTR_INDIR(dsize) \ +void * FASTCALL __bound_ptr_indir ## dsize (void *p, size_t offset) \ +{ \ + size_t addr = (size_t)p; \ + BoundEntry *e; \ + \ + dprintf(stderr, "%s %s: %p %x start\n", \ + __FILE__, __FUNCTION__, p, (unsigned)offset); \ + \ + __bound_init(); \ + e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; \ + e = (BoundEntry *)((char *)e + \ + ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & \ + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); \ + addr -= e->start; \ + if (addr > e->size) { \ + e = __bound_find_region(e, p); \ + addr = (size_t)p - e->start; \ + } \ + addr += offset + dsize; \ + if (addr > e->size) { \ + fprintf(stderr,"%s %s: %p is outside of the region\n", \ + __FILE__, __FUNCTION__, p + offset); \ + return INVALID_POINTER; /* return an invalid pointer */ \ + } \ + dprintf(stderr, "%s %s: return p+offset = %p\n", \ + __FILE__, __FUNCTION__, p + offset); \ + return p + offset; \ +} + +BOUND_PTR_INDIR(1) +BOUND_PTR_INDIR(2) +BOUND_PTR_INDIR(4) +BOUND_PTR_INDIR(8) +BOUND_PTR_INDIR(12) +BOUND_PTR_INDIR(16) + +#if defined(__GNUC__) && (__GNUC__ >= 6) +/* + * At least gcc 6.2 complains when __builtin_frame_address is used with + * nonzero argument. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wframe-address" +#endif + +/* return the frame pointer of the caller */ +#define GET_CALLER_FP(fp)\ +{\ + fp = (size_t)__builtin_frame_address(1);\ +} + +/* called when entering a function to add all the local regions */ +void FASTCALL __bound_local_new(void *p1) +{ + size_t addr, size, fp, *p = p1; + + dprintf(stderr, "%s, %s start p1=%p\n", __FILE__, __FUNCTION__, p); + GET_CALLER_FP(fp); + for(;;) { + addr = p[0]; + if (addr == 0) + break; + addr += fp; + size = p[1]; + p += 2; + __bound_new_region((void *)addr, size); + } + dprintf(stderr, "%s, %s end\n", __FILE__, __FUNCTION__); +} + +/* called when leaving a function to delete all the local regions */ +void FASTCALL __bound_local_delete(void *p1) +{ + size_t addr, fp, *p = p1; + GET_CALLER_FP(fp); + for(;;) { + addr = p[0]; + if (addr == 0) + break; + addr += fp; + p += 2; + __bound_delete_region((void *)addr); + } +} + +#if defined(__GNUC__) && (__GNUC__ >= 6) +#pragma GCC diagnostic pop +#endif + +static BoundEntry *__bound_new_page(void) +{ + BoundEntry *page; + size_t i; + + page = libc_malloc(sizeof(BoundEntry) * BOUND_T2_SIZE); + if (!page) + bound_alloc_error(); + for(i=0;i> BOUND_T3_BITS; + if (end != 0) + t2_end = end >> BOUND_T3_BITS; + else + t2_end = 1 << (BOUND_T1_BITS + BOUND_T2_BITS); + +#if 0 + dprintf(stderr, "mark_invalid: start = %x %x\n", t2_start, t2_end); +#endif + + /* first we handle full pages */ + t1_start = (t2_start + BOUND_T2_SIZE - 1) >> BOUND_T2_BITS; + t1_end = t2_end >> BOUND_T2_BITS; + + i = t2_start & (BOUND_T2_SIZE - 1); + j = t2_end & (BOUND_T2_SIZE - 1); + + if (t1_start == t1_end) { + page = get_page(t2_start >> BOUND_T2_BITS); + for(; i < j; i++) { + page[i].size = INVALID_SIZE; + page[i].is_invalid = 1; + } + } else { + if (i > 0) { + page = get_page(t2_start >> BOUND_T2_BITS); + for(; i < BOUND_T2_SIZE; i++) { + page[i].size = INVALID_SIZE; + page[i].is_invalid = 1; + } + } + for(i = t1_start; i < t1_end; i++) { + __bound_t1[i] = __bound_invalid_t2; + } + if (j != 0) { + page = get_page(t1_end); + for(i = 0; i < j; i++) { + page[i].size = INVALID_SIZE; + page[i].is_invalid = 1; + } + } + } +} + +void __bound_init(void) +{ + size_t i; + BoundEntry *page; + size_t start, size; + size_t *p; + + static int inited; + if (inited) + return; + + inited = 1; + + dprintf(stderr, "%s, %s() start\n", __FILE__, __FUNCTION__); + + /* save malloc hooks and install bound check hooks */ + install_malloc_hooks(); + +#ifndef BOUND_STATIC + __bound_t1 = libc_malloc(BOUND_T1_SIZE * sizeof(BoundEntry *)); + if (!__bound_t1) + bound_alloc_error(); +#endif + __bound_empty_t2 = __bound_new_page(); + for(i=0;i= v3.3, the alternative is to read + * start_brk from /proc/self/stat + */ + start = (size_t)sbrk(0); + size = 128 * 0x100000; + mark_invalid(start, size); +#endif + + /* add all static bound check values */ + p = (size_t *)&__bounds_start; + while (p[0] != 0) { + __bound_new_region((void *)p[0], p[1]); + p += 2; + } + + dprintf(stderr, "%s, %s() end\n\n", __FILE__, __FUNCTION__); +} + +void __bound_main_arg(void **p) +{ + void *start = p; + while (*p++); + + dprintf(stderr, "%s, %s calling __bound_new_region(%p %x)\n", + __FILE__, __FUNCTION__, start, (unsigned)((void *)p - start)); + + __bound_new_region(start, (void *) p - start); +} + +void __bound_exit(void) +{ + dprintf(stderr, "%s, %s()\n", __FILE__, __FUNCTION__); + restore_malloc_hooks(); +} + +static inline void add_region(BoundEntry *e, + size_t start, size_t size) +{ + BoundEntry *e1; + if (e->start == 0) { + /* no region : add it */ + e->start = start; + e->size = size; + } else { + /* already regions in the list: add it at the head */ + e1 = bound_new_entry(); + e1->start = e->start; + e1->size = e->size; + e1->next = e->next; + e->start = start; + e->size = size; + e->next = e1; + } +} + +/* create a new region. It should not already exist in the region list */ +void __bound_new_region(void *p, size_t size) +{ + size_t start, end; + BoundEntry *page, *e, *e2; + size_t t1_start, t1_end, i, t2_start, t2_end; + + dprintf(stderr, "%s, %s(%p, %x) start\n", + __FILE__, __FUNCTION__, p, (unsigned)size); + + __bound_init(); + + start = (size_t)p; + end = start + size; + t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS); + t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS); + + /* start */ + page = get_page(t1_start); + t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) & + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS); + t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) & + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS); + + + e = (BoundEntry *)((char *)page + t2_start); + add_region(e, start, size); + + if (t1_end == t1_start) { + /* same ending page */ + e2 = (BoundEntry *)((char *)page + t2_end); + if (e2 > e) { + e++; + for(;estart = start; + e->size = size; + } + add_region(e, start, size); + } + } else { + /* mark until end of page */ + e2 = page + BOUND_T2_SIZE; + e++; + for(;estart = start; + e->size = size; + } + /* mark intermediate pages, if any */ + for(i=t1_start+1;istart = start; + e->size = size; + } + } + /* last page */ + page = get_page(t1_end); + e2 = (BoundEntry *)((char *)page + t2_end); + for(e=page;estart = start; + e->size = size; + } + add_region(e, start, size); + } + + dprintf(stderr, "%s, %s end\n", __FILE__, __FUNCTION__); +} + +/* delete a region */ +static inline void delete_region(BoundEntry *e, void *p, size_t empty_size) +{ + size_t addr; + BoundEntry *e1; + + addr = (size_t)p; + addr -= e->start; + if (addr <= e->size) { + /* region found is first one */ + e1 = e->next; + if (e1 == NULL) { + /* no more region: mark it empty */ + e->start = 0; + e->size = empty_size; + } else { + /* copy next region in head */ + e->start = e1->start; + e->size = e1->size; + e->next = e1->next; + bound_free_entry(e1); + } + } else { + /* find the matching region */ + for(;;) { + e1 = e; + e = e->next; + /* region not found: do nothing */ + if (e == NULL) + break; + addr = (size_t)p - e->start; + if (addr <= e->size) { + /* found: remove entry */ + e1->next = e->next; + bound_free_entry(e); + break; + } + } + } +} + +/* WARNING: 'p' must be the starting point of the region. */ +/* return non zero if error */ +int __bound_delete_region(void *p) +{ + size_t start, end, addr, size, empty_size; + BoundEntry *page, *e, *e2; + size_t t1_start, t1_end, t2_start, t2_end, i; + + dprintf(stderr, "%s %s() start\n", __FILE__, __FUNCTION__); + + __bound_init(); + + start = (size_t)p; + t1_start = start >> (BOUND_T2_BITS + BOUND_T3_BITS); + t2_start = (start >> (BOUND_T3_BITS - BOUND_E_BITS)) & + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS); + + /* find region size */ + page = __bound_t1[t1_start]; + e = (BoundEntry *)((char *)page + t2_start); + addr = start - e->start; + if (addr > e->size) + e = __bound_find_region(e, p); + /* test if invalid region */ + if (e->size == EMPTY_SIZE || (size_t)p != e->start) + return -1; + /* compute the size we put in invalid regions */ + if (e->is_invalid) + empty_size = INVALID_SIZE; + else + empty_size = EMPTY_SIZE; + size = e->size; + end = start + size; + + /* now we can free each entry */ + t1_end = end >> (BOUND_T2_BITS + BOUND_T3_BITS); + t2_end = (end >> (BOUND_T3_BITS - BOUND_E_BITS)) & + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS); + + delete_region(e, p, empty_size); + if (t1_end == t1_start) { + /* same ending page */ + e2 = (BoundEntry *)((char *)page + t2_end); + if (e2 > e) { + e++; + for(;estart = 0; + e->size = empty_size; + } + delete_region(e, p, empty_size); + } + } else { + /* mark until end of page */ + e2 = page + BOUND_T2_SIZE; + e++; + for(;estart = 0; + e->size = empty_size; + } + /* mark intermediate pages, if any */ + /* XXX: should free them */ + for(i=t1_start+1;istart = 0; + e->size = empty_size; + } + } + /* last page */ + page = get_page(t1_end); + e2 = (BoundEntry *)((char *)page + t2_end); + for(e=page;estart = 0; + e->size = empty_size; + } + delete_region(e, p, empty_size); + } + + dprintf(stderr, "%s %s() end\n", __FILE__, __FUNCTION__); + + return 0; +} + +/* return the size of the region starting at p, or EMPTY_SIZE if non + existent region. */ +static size_t get_region_size(void *p) +{ + size_t addr = (size_t)p; + BoundEntry *e; + + e = __bound_t1[addr >> (BOUND_T2_BITS + BOUND_T3_BITS)]; + e = (BoundEntry *)((char *)e + + ((addr >> (BOUND_T3_BITS - BOUND_E_BITS)) & + ((BOUND_T2_SIZE - 1) << BOUND_E_BITS))); + addr -= e->start; + if (addr > e->size) + e = __bound_find_region(e, p); + if (e->start != (size_t)p) + return EMPTY_SIZE; + return e->size; +} + +/* patched memory functions */ + +/* force compiler to perform stores coded up to this point */ +#define barrier() __asm__ __volatile__ ("": : : "memory") + +static void install_malloc_hooks(void) +{ +#ifdef CONFIG_TCC_MALLOC_HOOKS + saved_malloc_hook = __malloc_hook; + saved_free_hook = __free_hook; + saved_realloc_hook = __realloc_hook; + saved_memalign_hook = __memalign_hook; + __malloc_hook = __bound_malloc; + __free_hook = __bound_free; + __realloc_hook = __bound_realloc; + __memalign_hook = __bound_memalign; + + barrier(); +#endif +} + +static void restore_malloc_hooks(void) +{ +#ifdef CONFIG_TCC_MALLOC_HOOKS + __malloc_hook = saved_malloc_hook; + __free_hook = saved_free_hook; + __realloc_hook = saved_realloc_hook; + __memalign_hook = saved_memalign_hook; + + barrier(); +#endif +} + +static void *libc_malloc(size_t size) +{ + void *ptr; + restore_malloc_hooks(); + ptr = malloc(size); + install_malloc_hooks(); + return ptr; +} + +static void libc_free(void *ptr) +{ + restore_malloc_hooks(); + free(ptr); + install_malloc_hooks(); +} + +/* XXX: we should use a malloc which ensure that it is unlikely that + two malloc'ed data have the same address if 'free' are made in + between. */ +void *__bound_malloc(size_t size, const void *caller) +{ + void *ptr; + + /* we allocate one more byte to ensure the regions will be + separated by at least one byte. With the glibc malloc, it may + be in fact not necessary */ + ptr = libc_malloc(size + 1); + + if (!ptr) + return NULL; + + dprintf(stderr, "%s, %s calling __bound_new_region(%p, %x)\n", + __FILE__, __FUNCTION__, ptr, (unsigned)size); + + __bound_new_region(ptr, size); + return ptr; +} + +void *__bound_memalign(size_t size, size_t align, const void *caller) +{ + void *ptr; + + restore_malloc_hooks(); + +#ifndef HAVE_MEMALIGN + if (align > 4) { + /* XXX: handle it ? */ + ptr = NULL; + } else { + /* we suppose that malloc aligns to at least four bytes */ + ptr = malloc(size + 1); + } +#else + /* we allocate one more byte to ensure the regions will be + separated by at least one byte. With the glibc malloc, it may + be in fact not necessary */ + ptr = memalign(size + 1, align); +#endif + + install_malloc_hooks(); + + if (!ptr) + return NULL; + + dprintf(stderr, "%s, %s calling __bound_new_region(%p, %x)\n", + __FILE__, __FUNCTION__, ptr, (unsigned)size); + + __bound_new_region(ptr, size); + return ptr; +} + +void __bound_free(void *ptr, const void *caller) +{ + if (ptr == NULL) + return; + if (__bound_delete_region(ptr) != 0) + bound_error("freeing invalid region"); + + libc_free(ptr); +} + +void *__bound_realloc(void *ptr, size_t size, const void *caller) +{ + void *ptr1; + size_t old_size; + + if (size == 0) { + __bound_free(ptr, caller); + return NULL; + } else { + ptr1 = __bound_malloc(size, caller); + if (ptr == NULL || ptr1 == NULL) + return ptr1; + old_size = get_region_size(ptr); + if (old_size == EMPTY_SIZE) + bound_error("realloc'ing invalid pointer"); + memcpy(ptr1, ptr, old_size); + __bound_free(ptr, caller); + return ptr1; + } +} + +#ifndef CONFIG_TCC_MALLOC_HOOKS +void *__bound_calloc(size_t nmemb, size_t size) +{ + void *ptr; + size = size * nmemb; + ptr = __bound_malloc(size, NULL); + if (!ptr) + return NULL; + memset(ptr, 0, size); + return ptr; +} +#endif + +#if 0 +static void bound_dump(void) +{ + BoundEntry *page, *e; + size_t i, j; + + fprintf(stderr, "region dump:\n"); + for(i=0;isize != EMPTY_SIZE && e->start != 0) { + fprintf(stderr, "%08x:", + (i << (BOUND_T2_BITS + BOUND_T3_BITS)) + + (j << BOUND_T3_BITS)); + do { + fprintf(stderr, " %08lx:%08lx", e->start, e->start + e->size); + e = e->next; + } while (e != NULL); + fprintf(stderr, "\n"); + } + } + } +} +#endif + +/* some useful checked functions */ + +/* check that (p ... p + size - 1) lies inside 'p' region, if any */ +static void __bound_check(const void *p, size_t size) +{ + if (size == 0) + return; + p = __bound_ptr_add((void *)p, size - 1); + if (p == INVALID_POINTER) + bound_error("invalid pointer"); +} + +void *__bound_memcpy(void *dst, const void *src, size_t size) +{ + void* p; + + dprintf(stderr, "%s %s: start, dst=%p src=%p size=%x\n", + __FILE__, __FUNCTION__, dst, src, (unsigned)size); + + __bound_check(dst, size); + __bound_check(src, size); + /* check also region overlap */ + if (src >= dst && src < dst + size) + bound_error("overlapping regions in memcpy()"); + + p = memcpy(dst, src, size); + + dprintf(stderr, "%s %s: end, p=%p\n", __FILE__, __FUNCTION__, p); + return p; +} + +void *__bound_memmove(void *dst, const void *src, size_t size) +{ + __bound_check(dst, size); + __bound_check(src, size); + return memmove(dst, src, size); +} + +void *__bound_memset(void *dst, int c, size_t size) +{ + __bound_check(dst, size); + return memset(dst, c, size); +} + +/* XXX: could be optimized */ +int __bound_strlen(const char *s) +{ + const char *p; + size_t len; + + len = 0; + for(;;) { + p = __bound_ptr_indir1((char *)s, len); + if (p == INVALID_POINTER) + bound_error("bad pointer in strlen()"); + if (*p == '\0') + break; + len++; + } + return len; +} + +char *__bound_strcpy(char *dst, const char *src) +{ + size_t len; + void *p; + + dprintf(stderr, "%s %s: strcpy start, dst=%p src=%p\n", + __FILE__, __FUNCTION__, dst, src); + len = __bound_strlen(src); + p = __bound_memcpy(dst, src, len + 1); + dprintf(stderr, "%s %s: strcpy end, p = %p\n", + __FILE__, __FUNCTION__, p); + return p; +} diff --git a/05/tcc-final-old/lib/lib-arm64.c b/05/tcc-final-old/lib/lib-arm64.c new file mode 100644 index 0000000..b8fd9e8 --- /dev/null +++ b/05/tcc-final-old/lib/lib-arm64.c @@ -0,0 +1,664 @@ +/* + * TCC runtime library for arm64. + * + * Copyright (c) 2015 Edmund Grimley Evans + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. This file is offered as-is, + * without any warranty. + */ + +#ifdef __TINYC__ +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned uint32_t; +typedef long long int64_t; +typedef unsigned long long uint64_t; +void *memcpy(void*,void*,__SIZE_TYPE__); +#else +#include +#include +#endif + +void __clear_cache(void *beg, void *end) +{ + __arm64_clear_cache(beg, end); +} + +typedef struct { + uint64_t x0, x1; +} u128_t; + +static long double f3_zero(int sgn) +{ + long double f; + u128_t x = { 0, (uint64_t)sgn << 63 }; + memcpy(&f, &x, 16); + return f; +} + +static long double f3_infinity(int sgn) +{ + long double f; + u128_t x = { 0, (uint64_t)sgn << 63 | 0x7fff000000000000 }; + memcpy(&f, &x, 16); + return f; +} + +static long double f3_NaN(void) +{ + long double f; +#if 0 + // ARM's default NaN usually has just the top fraction bit set: + u128_t x = { 0, 0x7fff800000000000 }; +#else + // GCC's library sets all fraction bits: + u128_t x = { -1, 0x7fffffffffffffff }; +#endif + memcpy(&f, &x, 16); + return f; +} + +static int fp3_convert_NaN(long double *f, int sgn, u128_t mnt) +{ + u128_t x = { mnt.x0, + mnt.x1 | 0x7fff800000000000 | (uint64_t)sgn << 63 }; + memcpy(f, &x, 16); + return 1; +} + +static int fp3_detect_NaNs(long double *f, + int a_sgn, int a_exp, u128_t a, + int b_sgn, int b_exp, u128_t b) +{ + // Detect signalling NaNs: + if (a_exp == 32767 && (a.x0 | a.x1 << 16) && !(a.x1 >> 47 & 1)) + return fp3_convert_NaN(f, a_sgn, a); + if (b_exp == 32767 && (b.x0 | b.x1 << 16) && !(b.x1 >> 47 & 1)) + return fp3_convert_NaN(f, b_sgn, b); + + // Detect quiet NaNs: + if (a_exp == 32767 && (a.x0 | a.x1 << 16)) + return fp3_convert_NaN(f, a_sgn, a); + if (b_exp == 32767 && (b.x0 | b.x1 << 16)) + return fp3_convert_NaN(f, b_sgn, b); + + return 0; +} + +static void f3_unpack(int *sgn, int32_t *exp, u128_t *mnt, long double f) +{ + u128_t x; + memcpy(&x, &f, 16); + *sgn = x.x1 >> 63; + *exp = x.x1 >> 48 & 32767; + x.x1 = x.x1 << 16 >> 16; + if (*exp) + x.x1 |= (uint64_t)1 << 48; + else + *exp = 1; + *mnt = x; +} + +static u128_t f3_normalise(int32_t *exp, u128_t mnt) +{ + int sh; + if (!(mnt.x0 | mnt.x1)) + return mnt; + if (!mnt.x1) { + mnt.x1 = mnt.x0; + mnt.x0 = 0; + *exp -= 64; + } + for (sh = 32; sh; sh >>= 1) { + if (!(mnt.x1 >> (64 - sh))) { + mnt.x1 = mnt.x1 << sh | mnt.x0 >> (64 - sh); + mnt.x0 = mnt.x0 << sh; + *exp -= sh; + } + } + return mnt; +} + +static u128_t f3_sticky_shift(int32_t sh, u128_t x) +{ + if (sh >= 128) { + x.x0 = !!(x.x0 | x.x1); + x.x1 = 0; + return x; + } + if (sh >= 64) { + x.x0 = x.x1 | !!x.x0; + x.x1 = 0; + sh -= 64; + } + if (sh > 0) { + x.x0 = x.x0 >> sh | x.x1 << (64 - sh) | !!(x.x0 << (64 - sh)); + x.x1 = x.x1 >> sh; + } + return x; +} + +static long double f3_round(int sgn, int32_t exp, u128_t x) +{ + long double f; + int error; + + if (exp > 0) { + x = f3_sticky_shift(13, x); + } + else { + x = f3_sticky_shift(14 - exp, x); + exp = 0; + } + + error = x.x0 & 3; + x.x0 = x.x0 >> 2 | x.x1 << 62; + x.x1 = x.x1 >> 2; + + if (error == 3 || ((error == 2) & (x.x0 & 1))) { + if (!++x.x0) { + ++x.x1; + if (x.x1 == (uint64_t)1 << 48) + exp = 1; + else if (x.x1 == (uint64_t)1 << 49) { + ++exp; + x.x0 = x.x0 >> 1 | x.x1 << 63; + x.x1 = x.x1 >> 1; + } + } + } + + if (exp >= 32767) + return f3_infinity(sgn); + + x.x1 = x.x1 << 16 >> 16 | (uint64_t)exp << 48 | (uint64_t)sgn << 63; + memcpy(&f, &x, 16); + return f; +} + +static long double f3_add(long double fa, long double fb, int neg) +{ + u128_t a, b, x; + int32_t a_exp, b_exp, x_exp; + int a_sgn, b_sgn, x_sgn; + long double fx; + + f3_unpack(&a_sgn, &a_exp, &a, fa); + f3_unpack(&b_sgn, &b_exp, &b, fb); + + if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b)) + return fx; + + b_sgn ^= neg; + + // Handle infinities and zeroes: + if (a_exp == 32767 && b_exp == 32767 && a_sgn != b_sgn) + return f3_NaN(); + if (a_exp == 32767) + return f3_infinity(a_sgn); + if (b_exp == 32767) + return f3_infinity(b_sgn); + if (!(a.x0 | a.x1 | b.x0 | b.x1)) + return f3_zero(a_sgn & b_sgn); + + a.x1 = a.x1 << 3 | a.x0 >> 61; + a.x0 = a.x0 << 3; + b.x1 = b.x1 << 3 | b.x0 >> 61; + b.x0 = b.x0 << 3; + + if (a_exp <= b_exp) { + a = f3_sticky_shift(b_exp - a_exp, a); + a_exp = b_exp; + } + else { + b = f3_sticky_shift(a_exp - b_exp, b); + b_exp = a_exp; + } + + x_sgn = a_sgn; + x_exp = a_exp; + if (a_sgn == b_sgn) { + x.x0 = a.x0 + b.x0; + x.x1 = a.x1 + b.x1 + (x.x0 < a.x0); + } + else { + x.x0 = a.x0 - b.x0; + x.x1 = a.x1 - b.x1 - (x.x0 > a.x0); + if (x.x1 >> 63) { + x_sgn ^= 1; + x.x0 = -x.x0; + x.x1 = -x.x1 - !!x.x0; + } + } + + if (!(x.x0 | x.x1)) + return f3_zero(0); + + x = f3_normalise(&x_exp, x); + + return f3_round(x_sgn, x_exp + 12, x); +} + +long double __addtf3(long double a, long double b) +{ + return f3_add(a, b, 0); +} + +long double __subtf3(long double a, long double b) +{ + return f3_add(a, b, 1); +} + +long double __multf3(long double fa, long double fb) +{ + u128_t a, b, x; + int32_t a_exp, b_exp, x_exp; + int a_sgn, b_sgn, x_sgn; + long double fx; + + f3_unpack(&a_sgn, &a_exp, &a, fa); + f3_unpack(&b_sgn, &b_exp, &b, fb); + + if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b)) + return fx; + + // Handle infinities and zeroes: + if ((a_exp == 32767 && !(b.x0 | b.x1)) || + (b_exp == 32767 && !(a.x0 | a.x1))) + return f3_NaN(); + if (a_exp == 32767 || b_exp == 32767) + return f3_infinity(a_sgn ^ b_sgn); + if (!(a.x0 | a.x1) || !(b.x0 | b.x1)) + return f3_zero(a_sgn ^ b_sgn); + + a = f3_normalise(&a_exp, a); + b = f3_normalise(&b_exp, b); + + x_sgn = a_sgn ^ b_sgn; + x_exp = a_exp + b_exp - 16352; + + { + // Convert to base (1 << 30), discarding bottom 6 bits, which are zero, + // so there are (32, 30, 30, 30) bits in (a3, a2, a1, a0): + uint64_t a0 = a.x0 << 28 >> 34; + uint64_t b0 = b.x0 << 28 >> 34; + uint64_t a1 = a.x0 >> 36 | a.x1 << 62 >> 34; + uint64_t b1 = b.x0 >> 36 | b.x1 << 62 >> 34; + uint64_t a2 = a.x1 << 32 >> 34; + uint64_t b2 = b.x1 << 32 >> 34; + uint64_t a3 = a.x1 >> 32; + uint64_t b3 = b.x1 >> 32; + // Use 16 small multiplications and additions that do not overflow: + uint64_t x0 = a0 * b0; + uint64_t x1 = (x0 >> 30) + a0 * b1 + a1 * b0; + uint64_t x2 = (x1 >> 30) + a0 * b2 + a1 * b1 + a2 * b0; + uint64_t x3 = (x2 >> 30) + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; + uint64_t x4 = (x3 >> 30) + a1 * b3 + a2 * b2 + a3 * b1; + uint64_t x5 = (x4 >> 30) + a2 * b3 + a3 * b2; + uint64_t x6 = (x5 >> 30) + a3 * b3; + // We now have (64, 30, 30, ...) bits in (x6, x5, x4, ...). + // Take the top 128 bits, setting bottom bit if any lower bits were set: + uint64_t y0 = (x5 << 34 | x4 << 34 >> 30 | x3 << 34 >> 60 | + !!(x3 << 38 | (x2 | x1 | x0) << 34)); + uint64_t y1 = x6; + // Top bit may be zero. Renormalise: + if (!(y1 >> 63)) { + y1 = y1 << 1 | y0 >> 63; + y0 = y0 << 1; + --x_exp; + } + x.x0 = y0; + x.x1 = y1; + } + + return f3_round(x_sgn, x_exp, x); +} + +long double __divtf3(long double fa, long double fb) +{ + u128_t a, b, x; + int32_t a_exp, b_exp, x_exp; + int a_sgn, b_sgn, x_sgn, i; + long double fx; + + f3_unpack(&a_sgn, &a_exp, &a, fa); + f3_unpack(&b_sgn, &b_exp, &b, fb); + + if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b)) + return fx; + + // Handle infinities and zeroes: + if ((a_exp == 32767 && b_exp == 32767) || + (!(a.x0 | a.x1) && !(b.x0 | b.x1))) + return f3_NaN(); + if (a_exp == 32767 || !(b.x0 | b.x1)) + return f3_infinity(a_sgn ^ b_sgn); + if (!(a.x0 | a.x1) || b_exp == 32767) + return f3_zero(a_sgn ^ b_sgn); + + a = f3_normalise(&a_exp, a); + b = f3_normalise(&b_exp, b); + + x_sgn = a_sgn ^ b_sgn; + x_exp = a_exp - b_exp + 16395; + + a.x0 = a.x0 >> 1 | a.x1 << 63; + a.x1 = a.x1 >> 1; + b.x0 = b.x0 >> 1 | b.x1 << 63; + b.x1 = b.x1 >> 1; + x.x0 = 0; + x.x1 = 0; + for (i = 0; i < 116; i++) { + x.x1 = x.x1 << 1 | x.x0 >> 63; + x.x0 = x.x0 << 1; + if (a.x1 > b.x1 || (a.x1 == b.x1 && a.x0 >= b.x0)) { + a.x1 = a.x1 - b.x1 - (a.x0 < b.x0); + a.x0 = a.x0 - b.x0; + x.x0 |= 1; + } + a.x1 = a.x1 << 1 | a.x0 >> 63; + a.x0 = a.x0 << 1; + } + x.x0 |= !!(a.x0 | a.x1); + + x = f3_normalise(&x_exp, x); + + return f3_round(x_sgn, x_exp, x); +} + +long double __extendsftf2(float f) +{ + long double fx; + u128_t x; + uint32_t a; + uint64_t aa; + memcpy(&a, &f, 4); + aa = a; + x.x0 = 0; + if (!(a << 1)) + x.x1 = aa << 32; + else if (a << 1 >> 24 == 255) + x.x1 = (0x7fff000000000000 | aa >> 31 << 63 | aa << 41 >> 16 | + (uint64_t)!!(a << 9) << 47); + else + x.x1 = (aa >> 31 << 63 | ((aa >> 23 & 255) + 16256) << 48 | + aa << 41 >> 16); + memcpy(&fx, &x, 16); + return fx; +} + +long double __extenddftf2(double f) +{ + long double fx; + u128_t x; + uint64_t a; + memcpy(&a, &f, 8); + x.x0 = a << 60; + if (!(a << 1)) + x.x1 = a; + else if (a << 1 >> 53 == 2047) + x.x1 = (0x7fff000000000000 | a >> 63 << 63 | a << 12 >> 16 | + (uint64_t)!!(a << 12) << 47); + else + x.x1 = a >> 63 << 63 | ((a >> 52 & 2047) + 15360) << 48 | a << 12 >> 16; + memcpy(&fx, &x, 16); + return fx; +} + +float __trunctfsf2(long double f) +{ + u128_t mnt; + int32_t exp; + int sgn; + uint32_t x; + float fx; + + f3_unpack(&sgn, &exp, &mnt, f); + + if (exp == 32767 && (mnt.x0 | mnt.x1 << 16)) + x = 0x7fc00000 | (uint32_t)sgn << 31 | (mnt.x1 >> 25 & 0x007fffff); + else if (exp > 16510) + x = 0x7f800000 | (uint32_t)sgn << 31; + else if (exp < 16233) + x = (uint32_t)sgn << 31; + else { + exp -= 16257; + x = mnt.x1 >> 23 | !!(mnt.x0 | mnt.x1 << 41); + if (exp < 0) { + x = x >> -exp | !!(x << (32 + exp)); + exp = 0; + } + if ((x & 3) == 3 || (x & 7) == 6) + x += 4; + x = ((x >> 2) + (exp << 23)) | (uint32_t)sgn << 31; + } + memcpy(&fx, &x, 4); + return fx; +} + +double __trunctfdf2(long double f) +{ + u128_t mnt; + int32_t exp; + int sgn; + uint64_t x; + double fx; + + f3_unpack(&sgn, &exp, &mnt, f); + + if (exp == 32767 && (mnt.x0 | mnt.x1 << 16)) + x = (0x7ff8000000000000 | (uint64_t)sgn << 63 | + mnt.x1 << 16 >> 12 | mnt.x0 >> 60); + else if (exp > 17406) + x = 0x7ff0000000000000 | (uint64_t)sgn << 63; + else if (exp < 15308) + x = (uint64_t)sgn << 63; + else { + exp -= 15361; + x = mnt.x1 << 6 | mnt.x0 >> 58 | !!(mnt.x0 << 6); + if (exp < 0) { + x = x >> -exp | !!(x << (64 + exp)); + exp = 0; + } + if ((x & 3) == 3 || (x & 7) == 6) + x += 4; + x = ((x >> 2) + ((uint64_t)exp << 52)) | (uint64_t)sgn << 63; + } + memcpy(&fx, &x, 8); + return fx; +} + +int32_t __fixtfsi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + int32_t x; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_exp < 16369) + return 0; + if (a_exp > 16413) + return a_sgn ? -0x80000000 : 0x7fffffff; + x = a.x1 >> (16431 - a_exp); + return a_sgn ? -x : x; +} + +int64_t __fixtfdi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + int64_t x; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_exp < 16383) + return 0; + if (a_exp > 16445) + return a_sgn ? -0x8000000000000000 : 0x7fffffffffffffff; + x = (a.x1 << 15 | a.x0 >> 49) >> (16446 - a_exp); + return a_sgn ? -x : x; +} + +uint32_t __fixunstfsi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_sgn || a_exp < 16369) + return 0; + if (a_exp > 16414) + return -1; + return a.x1 >> (16431 - a_exp); +} + +uint64_t __fixunstfdi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_sgn || a_exp < 16383) + return 0; + if (a_exp > 16446) + return -1; + return (a.x1 << 15 | a.x0 >> 49) >> (16446 - a_exp); +} + +long double __floatsitf(int32_t a) +{ + int sgn = 0; + int exp = 16414; + uint32_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + if (a < 0) { + sgn = 1; + mnt = -mnt; + } + for (i = 16; i; i >>= 1) + if (!(mnt >> (32 - i))) { + mnt <<= i; + exp -= i; + } + x.x1 = ((uint64_t)sgn << 63 | (uint64_t)exp << 48 | + (uint64_t)(mnt << 1) << 16); + } + memcpy(&f, &x, 16); + return f; +} + +long double __floatditf(int64_t a) +{ + int sgn = 0; + int exp = 16446; + uint64_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + if (a < 0) { + sgn = 1; + mnt = -mnt; + } + for (i = 32; i; i >>= 1) + if (!(mnt >> (64 - i))) { + mnt <<= i; + exp -= i; + } + x.x0 = mnt << 49; + x.x1 = (uint64_t)sgn << 63 | (uint64_t)exp << 48 | mnt << 1 >> 16; + } + memcpy(&f, &x, 16); + return f; +} + +long double __floatunsitf(uint32_t a) +{ + int exp = 16414; + uint32_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + for (i = 16; i; i >>= 1) + if (!(mnt >> (32 - i))) { + mnt <<= i; + exp -= i; + } + x.x1 = (uint64_t)exp << 48 | (uint64_t)(mnt << 1) << 16; + } + memcpy(&f, &x, 16); + return f; +} + +long double __floatunditf(uint64_t a) +{ + int exp = 16446; + uint64_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + for (i = 32; i; i >>= 1) + if (!(mnt >> (64 - i))) { + mnt <<= i; + exp -= i; + } + x.x0 = mnt << 49; + x.x1 = (uint64_t)exp << 48 | mnt << 1 >> 16; + } + memcpy(&f, &x, 16); + return f; +} + +static int f3_cmp(long double fa, long double fb) +{ + u128_t a, b; + memcpy(&a, &fa, 16); + memcpy(&b, &fb, 16); + return (!(a.x0 | a.x1 << 1 | b.x0 | b.x1 << 1) ? 0 : + ((a.x1 << 1 >> 49 == 0x7fff && (a.x0 | a.x1 << 16)) || + (b.x1 << 1 >> 49 == 0x7fff && (b.x0 | b.x1 << 16))) ? 2 : + a.x1 >> 63 != b.x1 >> 63 ? (int)(b.x1 >> 63) - (int)(a.x1 >> 63) : + a.x1 < b.x1 ? (int)(a.x1 >> 63 << 1) - 1 : + a.x1 > b.x1 ? 1 - (int)(a.x1 >> 63 << 1) : + a.x0 < b.x0 ? (int)(a.x1 >> 63 << 1) - 1 : + b.x0 < a.x0 ? 1 - (int)(a.x1 >> 63 << 1) : 0); +} + +int __eqtf2(long double a, long double b) +{ + return !!f3_cmp(a, b); +} + +int __netf2(long double a, long double b) +{ + return !!f3_cmp(a, b); +} + +int __lttf2(long double a, long double b) +{ + return f3_cmp(a, b); +} + +int __letf2(long double a, long double b) +{ + return f3_cmp(a, b); +} + +int __gttf2(long double a, long double b) +{ + return -f3_cmp(b, a); +} + +int __getf2(long double a, long double b) +{ + return -f3_cmp(b, a); +} diff --git a/05/tcc-final-old/lib/libtcc1.c b/05/tcc-final-old/lib/libtcc1.c new file mode 100644 index 0000000..0e46618 --- /dev/null +++ b/05/tcc-final-old/lib/libtcc1.c @@ -0,0 +1,622 @@ +/* TCC runtime library. + Parts of this code are (c) 2002 Fabrice Bellard + + Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc. + +This file is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +In addition to the permissions in the GNU General Public License, the +Free Software Foundation gives you unlimited permission to link the +compiled version of this file into combinations with other programs, +and to distribute those combinations without any restriction coming +from the use of this file. (The General Public License restrictions +do apply in other respects; for example, they cover modification of +the file, and distribution when not linked into a combine +executable.) + +This file is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING. If not, write to +the Free Software Foundation, 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +*/ + +#define W_TYPE_SIZE 32 +#define BITS_PER_UNIT 8 + +typedef int Wtype; +typedef unsigned int UWtype; +typedef unsigned int USItype; +typedef long long DWtype; +typedef unsigned long long UDWtype; + +struct DWstruct { + Wtype low, high; +}; + +typedef union +{ + struct DWstruct s; + DWtype ll; +} DWunion; + +typedef long double XFtype; +#define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT) +#define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE) + +/* the following deal with IEEE single-precision numbers */ +#define EXCESS 126 +#define SIGNBIT 0x80000000 +#define HIDDEN (1 << 23) +#define SIGN(fp) ((fp) & SIGNBIT) +#define EXP(fp) (((fp) >> 23) & 0xFF) +#define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN) +#define PACK(s,e,m) ((s) | ((e) << 23) | (m)) + +/* the following deal with IEEE double-precision numbers */ +#define EXCESSD 1022 +#define HIDDEND (1 << 20) +#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF) +#define SIGND(fp) ((fp.l.upper) & SIGNBIT) +#define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \ + (fp.l.lower >> 22)) +#define HIDDEND_LL ((long long)1 << 52) +#define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL) +#define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m)) + +/* the following deal with x86 long double-precision numbers */ +#define EXCESSLD 16382 +#define EXPLD(fp) (fp.l.upper & 0x7fff) +#define SIGNLD(fp) ((fp.l.upper) & 0x8000) + +/* only for x86 */ +union ldouble_long { + long double ld; + struct { + unsigned long long lower; + unsigned short upper; + } l; +}; + +union double_long { + double d; +#if 1 + struct { + unsigned int lower; + int upper; + } l; +#else + struct { + int upper; + unsigned int lower; + } l; +#endif + long long ll; +}; + +union float_long { + float f; + unsigned int l; +}; + +/* XXX: we don't support several builtin supports for now */ +#if !defined __x86_64__ && !defined __arm__ + +/* XXX: use gcc/tcc intrinsic ? */ +#if defined __i386__ +#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ + __asm__ ("subl %5,%1\n\tsbbl %3,%0" \ + : "=r" ((USItype) (sh)), \ + "=&r" ((USItype) (sl)) \ + : "0" ((USItype) (ah)), \ + "g" ((USItype) (bh)), \ + "1" ((USItype) (al)), \ + "g" ((USItype) (bl))) +#define umul_ppmm(w1, w0, u, v) \ + __asm__ ("mull %3" \ + : "=a" ((USItype) (w0)), \ + "=d" ((USItype) (w1)) \ + : "%0" ((USItype) (u)), \ + "rm" ((USItype) (v))) +#define udiv_qrnnd(q, r, n1, n0, dv) \ + __asm__ ("divl %4" \ + : "=a" ((USItype) (q)), \ + "=d" ((USItype) (r)) \ + : "0" ((USItype) (n0)), \ + "1" ((USItype) (n1)), \ + "rm" ((USItype) (dv))) +#define count_leading_zeros(count, x) \ + do { \ + USItype __cbtmp; \ + __asm__ ("bsrl %1,%0" \ + : "=r" (__cbtmp) : "rm" ((USItype) (x))); \ + (count) = __cbtmp ^ 31; \ + } while (0) +#else +#error unsupported CPU type +#endif + +/* most of this code is taken from libgcc2.c from gcc */ + +static UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp) +{ + DWunion ww; + DWunion nn, dd; + DWunion rr; + UWtype d0, d1, n0, n1, n2; + UWtype q0, q1; + UWtype b, bm; + + nn.ll = n; + dd.ll = d; + + d0 = dd.s.low; + d1 = dd.s.high; + n0 = nn.s.low; + n1 = nn.s.high; + +#if !defined(UDIV_NEEDS_NORMALIZATION) + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + udiv_qrnnd (q1, n1, 0, n1, d0); + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0. */ + } + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = 0; + *rp = rr.ll; + } + } + +#else /* UDIV_NEEDS_NORMALIZATION */ + + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + count_leading_zeros (bm, d0); + + if (bm != 0) + { + /* Normalize, i.e. make the most significant bit of the + denominator set. */ + + d0 = d0 << bm; + n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm)); + n0 = n0 << bm; + } + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0 >> bm. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + count_leading_zeros (bm, d0); + + if (bm == 0) + { + /* From (n1 >= d0) /\ (the most significant bit of d0 is set), + conclude (the most significant bit of n1 is set) /\ (the + leading quotient digit q1 = 1). + + This special case is necessary, not an optimization. + (Shifts counts of W_TYPE_SIZE are undefined.) */ + + n1 -= d0; + q1 = 1; + } + else + { + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q1, n1, n2, n1, d0); + } + + /* n1 != d0... */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0 >> bm. */ + } + + if (rp != 0) + { + rr.s.low = n0 >> bm; + rr.s.high = 0; + *rp = rr.ll; + } + } +#endif /* UDIV_NEEDS_NORMALIZATION */ + + else + { + if (d1 > n1) + { + /* 00 = nn / DD */ + + q0 = 0; + q1 = 0; + + /* Remainder in n1n0. */ + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + /* 0q = NN / dd */ + + count_leading_zeros (bm, d1); + if (bm == 0) + { + /* From (n1 >= d1) /\ (the most significant bit of d1 is set), + conclude (the most significant bit of n1 is set) /\ (the + quotient digit q0 = 0 or 1). + + This special case is necessary, not an optimization. */ + + /* The condition on the next line takes advantage of that + n1 >= d1 (true due to program flow). */ + if (n1 > d1 || n0 >= d0) + { + q0 = 1; + sub_ddmmss (n1, n0, n1, n0, d1, d0); + } + else + q0 = 0; + + q1 = 0; + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + UWtype m1, m0; + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d1 = (d1 << bm) | (d0 >> b); + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q0, n1, n2, n1, d1); + umul_ppmm (m1, m0, q0, d0); + + if (m1 > n1 || (m1 == n1 && m0 > n0)) + { + q0--; + sub_ddmmss (m1, m0, m1, m0, d1, d0); + } + + q1 = 0; + + /* Remainder in (n1n0 - m1m0) >> bm. */ + if (rp != 0) + { + sub_ddmmss (n1, n0, n1, n0, m1, m0); + rr.s.low = (n1 << b) | (n0 >> bm); + rr.s.high = n1 >> bm; + *rp = rr.ll; + } + } + } + } + + ww.s.low = q0; + ww.s.high = q1; + return ww.ll; +} + +#define __negdi2(a) (-(a)) + +long long __divdi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) { + c = ~c; + vv.ll = __negdi2 (vv.ll); + } + w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0); + if (c) + w = __negdi2 (w); + return w; +} + +long long __moddi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) + vv.ll = __negdi2 (vv.ll); + + __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) &w); + if (c) + w = __negdi2 (w); + return w; +} + +unsigned long long __udivdi3(unsigned long long u, unsigned long long v) +{ + return __udivmoddi4 (u, v, (UDWtype *) 0); +} + +unsigned long long __umoddi3(unsigned long long u, unsigned long long v) +{ + UDWtype w; + + __udivmoddi4 (u, v, &w); + return w; +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashrdi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = u.s.high >> (b - 32); + u.s.high = u.s.high >> 31; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +unsigned long long __lshrdi3(unsigned long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = (unsigned)u.s.high >> (b - 32); + u.s.high = 0; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = (unsigned)u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashldi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.high = (unsigned)u.s.low << (b - 32); + u.s.low = 0; + } else if (b != 0) { + u.s.high = ((unsigned)u.s.high << b) | ((unsigned)u.s.low >> (32 - b)); + u.s.low = (unsigned)u.s.low << b; + } + return u.ll; +#else + return a << b; +#endif +} + +#endif /* !__x86_64__ */ + +/* XXX: fix tcc's code generator to do this instead */ +float __floatundisf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (float)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (float)r; + } +} + +double __floatundidf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (double)r; + } +} + +long double __floatundixf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (long double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (long double)r; + } +} + +unsigned long long __fixunssfdi (float a1) +{ + register union float_long fl1; + register int exp; + register unsigned long l; + + fl1.f = a1; + + if (fl1.l == 0) + return (0); + + exp = EXP (fl1.l) - EXCESS - 24; + + l = MANT(fl1.l); + if (exp >= 41) + return (unsigned long long)-1; + else if (exp >= 0) + return (unsigned long long)l << exp; + else if (exp >= -23) + return l >> -exp; + else + return 0; +} + +long long __fixsfdi (float a1) +{ + long long ret; int s; + ret = __fixunssfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +unsigned long long __fixunsdfdi (double a1) +{ + register union double_long dl1; + register int exp; + register unsigned long long l; + + dl1.d = a1; + + if (dl1.ll == 0) + return (0); + + exp = EXPD (dl1) - EXCESSD - 53; + + l = MANTD_LL(dl1); + + if (exp >= 12) + return (unsigned long long)-1; + else if (exp >= 0) + return l << exp; + else if (exp >= -52) + return l >> -exp; + else + return 0; +} + +long long __fixdfdi (double a1) +{ + long long ret; int s; + ret = __fixunsdfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +#ifndef __arm__ +unsigned long long __fixunsxfdi (long double a1) +{ + register union ldouble_long dl1; + register int exp; + register unsigned long long l; + + dl1.ld = a1; + + if (dl1.l.lower == 0 && dl1.l.upper == 0) + return (0); + + exp = EXPLD (dl1) - EXCESSLD - 64; + + l = dl1.l.lower; + + if (exp > 0) + return (unsigned long long)-1; + else if (exp >= -63) + return l >> -exp; + else + return 0; +} + +long long __fixxfdi (long double a1) +{ + long long ret; int s; + ret = __fixunsxfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} +#endif /* !ARM */ diff --git a/05/tcc-final-old/lib/va_list.c b/05/tcc-final-old/lib/va_list.c new file mode 100644 index 0000000..8749f46 --- /dev/null +++ b/05/tcc-final-old/lib/va_list.c @@ -0,0 +1,65 @@ +/* va_list.c - tinycc support for va_list on X86_64 */ + +#if defined __x86_64__ + +/* Avoid include files, they may not be available when cross compiling */ +extern void *memset(void *s, int c, __SIZE_TYPE__ n); +extern void abort(void); + +/* This should be in sync with our include/stdarg.h */ +enum __va_arg_type { + __va_gen_reg, __va_float_reg, __va_stack +}; + +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; + +void __va_start(__va_list_struct *ap, void *fp) +{ + memset(ap, 0, sizeof(__va_list_struct)); + *ap = *(__va_list_struct *)((char *)fp - 16); + ap->overflow_arg_area = (char *)fp + ap->overflow_offset; + ap->reg_save_area = (char *)fp - 176 - 16; +} + +void *__va_arg(__va_list_struct *ap, + enum __va_arg_type arg_type, + int size, int align) +{ + size = (size + 7) & ~7; + align = (align + 7) & ~7; + switch (arg_type) { + case __va_gen_reg: + if (ap->gp_offset + size <= 48) { + ap->gp_offset += size; + return ap->reg_save_area + ap->gp_offset - size; + } + goto use_overflow_area; + + case __va_float_reg: + if (ap->fp_offset < 128 + 48) { + ap->fp_offset += 16; + return ap->reg_save_area + ap->fp_offset - 16; + } + size = 8; + goto use_overflow_area; + + case __va_stack: + use_overflow_area: + ap->overflow_arg_area += size; + ap->overflow_arg_area = (char*)((long long)(ap->overflow_arg_area + align - 1) & -align); + return ap->overflow_arg_area - size; + + default: /* should never happen */ + abort(); + } +} +#endif diff --git a/05/tcc-final-old/libtcc.c b/05/tcc-final-old/libtcc.c new file mode 100644 index 0000000..1e9dd97 --- /dev/null +++ b/05/tcc-final-old/libtcc.c @@ -0,0 +1,1981 @@ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +/********************************************************/ +/* global variables */ + +/* use GNU C extensions */ +ST_DATA int gnu_ext = 1; + +/* use TinyCC extensions */ +ST_DATA int tcc_ext = 1; + +/* XXX: get rid of this ASAP */ +ST_DATA struct TCCState *tcc_state; + +static int nb_states; + +/********************************************************/ + +#if ONE_SOURCE +#include "tccpp.c" +#include "tccgen.c" +#include "tccelf.c" +#include "tccrun.c" +#ifdef TCC_TARGET_I386 +#include "i386-gen.c" +#include "i386-link.c" +#include "i386-asm.c" +#endif +#ifdef TCC_TARGET_ARM +#include "arm-gen.c" +#include "arm-link.c" +#include "arm-asm.c" +#endif +#ifdef TCC_TARGET_ARM64 +#include "arm64-gen.c" +#include "arm64-link.c" +#endif +#ifdef TCC_TARGET_C67 +#include "c67-gen.c" +#include "c67-link.c" +#include "tcccoff.c" +#endif +#ifdef TCC_TARGET_X86_64 +#include "x86_64-gen.c" +#include "x86_64-link.c" +#include "i386-asm.c" +#endif +#ifdef CONFIG_TCC_ASM +#include "tccasm.c" +#endif +#ifdef TCC_TARGET_PE +#include "tccpe.c" +#endif +#endif /* ONE_SOURCE */ + +/********************************************************/ +#ifndef CONFIG_TCC_ASM +ST_FUNC void asm_instr(void) +{ + tcc_error("inline asm() not supported"); +} +ST_FUNC void asm_global_instr(void) +{ + tcc_error("inline asm() not supported"); +} +#endif + +/********************************************************/ +#ifdef _WIN32 +ST_FUNC char *normalize_slashes(char *path) +{ + char *p; + for (p = path; *p; ++p) + if (*p == '\\') + *p = '/'; + return path; +} + +static HMODULE tcc_module; + +/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */ +static void tcc_set_lib_path_w32(TCCState *s) +{ + char path[1024], *p; + GetModuleFileNameA(tcc_module, path, sizeof path); + p = tcc_basename(normalize_slashes(strlwr(path))); + if (p > path) + --p; + *p = 0; + tcc_set_lib_path(s, path); +} + +#ifdef TCC_TARGET_PE +static void tcc_add_systemdir(TCCState *s) +{ + char buf[1000]; + GetSystemDirectory(buf, sizeof buf); + tcc_add_library_path(s, normalize_slashes(buf)); +} +#endif + +#ifdef LIBTCC_AS_DLL +BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) +{ + if (DLL_PROCESS_ATTACH == dwReason) + tcc_module = hDll; + return TRUE; +} +#endif +#endif + +/********************************************************/ +/* copy a string and truncate it. */ +ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s) +{ + char *q, *q_end; + int c; + + if (buf_size > 0) { + q = buf; + q_end = buf + buf_size - 1; + while (q < q_end) { + c = *s++; + if (c == '\0') + break; + *q++ = c; + } + *q = '\0'; + } + return buf; +} + +/* strcat and truncate. */ +ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s) +{ + int len; + len = strlen(buf); + if (len < buf_size) + pstrcpy(buf + len, buf_size - len, s); + return buf; +} + +ST_FUNC char *pstrncpy(char *out, const char *in, size_t num) +{ + memcpy(out, in, num); + out[num] = '\0'; + return out; +} + +/* extract the basename of a file */ +PUB_FUNC char *tcc_basename(const char *name) +{ + char *p = strchr(name, 0); + while (p > name && !IS_DIRSEP(p[-1])) + --p; + return p; +} + +/* extract extension part of a file + * + * (if no extension, return pointer to end-of-string) + */ +PUB_FUNC char *tcc_fileextension (const char *name) +{ + char *b = tcc_basename(name); + char *e = strrchr(b, '.'); + return e ? e : strchr(b, 0); +} + +/********************************************************/ +/* memory management */ + +#undef free +#undef malloc +#undef realloc + +#ifndef MEM_DEBUG + +PUB_FUNC void tcc_free(void *ptr) +{ + free(ptr); +} + +PUB_FUNC void *tcc_malloc(unsigned long size) +{ + void *ptr; + ptr = malloc(size); + if (!ptr && size) + tcc_error("memory full (malloc)"); + return ptr; +} + +PUB_FUNC void *tcc_mallocz(unsigned long size) +{ + void *ptr; + ptr = tcc_malloc(size); + memset(ptr, 0, size); + return ptr; +} + +PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size) +{ + void *ptr1; + ptr1 = realloc(ptr, size); + if (!ptr1 && size) + tcc_error("memory full (realloc)"); + return ptr1; +} + +PUB_FUNC char *tcc_strdup(const char *str) +{ + char *ptr; + ptr = tcc_malloc(strlen(str) + 1); + strcpy(ptr, str); + return ptr; +} + +PUB_FUNC void tcc_memcheck(void) +{ +} + +#else + +#define MEM_DEBUG_MAGIC1 0xFEEDDEB1 +#define MEM_DEBUG_MAGIC2 0xFEEDDEB2 +#define MEM_DEBUG_MAGIC3 0xFEEDDEB3 +#define MEM_DEBUG_FILE_LEN 40 +#define MEM_DEBUG_CHECK3(header) \ + ((mem_debug_header_t*)((char*)header + header->size))->magic3 +#define MEM_USER_PTR(header) \ + ((char *)header + offsetof(mem_debug_header_t, magic3)) +#define MEM_HEADER_PTR(ptr) \ + (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3)) + +struct mem_debug_header { + unsigned magic1; + unsigned size; + struct mem_debug_header *prev; + struct mem_debug_header *next; + int line_num; + char file_name[MEM_DEBUG_FILE_LEN + 1]; + unsigned magic2; + ALIGNED(16) unsigned magic3; +}; + +typedef struct mem_debug_header mem_debug_header_t; + +static mem_debug_header_t *mem_debug_chain; +static unsigned mem_cur_size; +static unsigned mem_max_size; + +static mem_debug_header_t *malloc_check(void *ptr, const char *msg) +{ + mem_debug_header_t * header = MEM_HEADER_PTR(ptr); + if (header->magic1 != MEM_DEBUG_MAGIC1 || + header->magic2 != MEM_DEBUG_MAGIC2 || + MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 || + header->size == (unsigned)-1) { + fprintf(stderr, "%s check failed\n", msg); + if (header->magic1 == MEM_DEBUG_MAGIC1) + fprintf(stderr, "%s:%u: block allocated here.\n", + header->file_name, header->line_num); + exit(1); + } + return header; +} + +PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line) +{ + int ofs; + mem_debug_header_t *header; + + header = malloc(sizeof(mem_debug_header_t) + size); + if (!header) + tcc_error("memory full (malloc)"); + + header->magic1 = MEM_DEBUG_MAGIC1; + header->magic2 = MEM_DEBUG_MAGIC2; + header->size = size; + MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3; + header->line_num = line; + ofs = strlen(file) - MEM_DEBUG_FILE_LEN; + strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN); + header->file_name[MEM_DEBUG_FILE_LEN] = 0; + + header->next = mem_debug_chain; + header->prev = NULL; + if (header->next) + header->next->prev = header; + mem_debug_chain = header; + + mem_cur_size += size; + if (mem_cur_size > mem_max_size) + mem_max_size = mem_cur_size; + + return MEM_USER_PTR(header); +} + +PUB_FUNC void tcc_free_debug(void *ptr) +{ + mem_debug_header_t *header; + if (!ptr) + return; + header = malloc_check(ptr, "tcc_free"); + mem_cur_size -= header->size; + header->size = (unsigned)-1; + if (header->next) + header->next->prev = header->prev; + if (header->prev) + header->prev->next = header->next; + if (header == mem_debug_chain) + mem_debug_chain = header->next; + free(header); +} + +PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line) +{ + void *ptr; + ptr = tcc_malloc_debug(size,file,line); + memset(ptr, 0, size); + return ptr; +} + +PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line) +{ + mem_debug_header_t *header; + int mem_debug_chain_update = 0; + if (!ptr) + return tcc_malloc_debug(size, file, line); + header = malloc_check(ptr, "tcc_realloc"); + mem_cur_size -= header->size; + mem_debug_chain_update = (header == mem_debug_chain); + header = realloc(header, sizeof(mem_debug_header_t) + size); + if (!header) + tcc_error("memory full (realloc)"); + header->size = size; + MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3; + if (header->next) + header->next->prev = header; + if (header->prev) + header->prev->next = header; + if (mem_debug_chain_update) + mem_debug_chain = header; + mem_cur_size += size; + if (mem_cur_size > mem_max_size) + mem_max_size = mem_cur_size; + return MEM_USER_PTR(header); +} + +PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line) +{ + char *ptr; + ptr = tcc_malloc_debug(strlen(str) + 1, file, line); + strcpy(ptr, str); + return ptr; +} + +PUB_FUNC void tcc_memcheck(void) +{ + if (mem_cur_size) { + mem_debug_header_t *header = mem_debug_chain; + fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n", + mem_cur_size, mem_max_size); + while (header) { + fprintf(stderr, "%s:%u: error: %u bytes leaked\n", + header->file_name, header->line_num, header->size); + header = header->next; + } +#if MEM_DEBUG-0 == 2 + exit(2); +#endif + } +} +#endif /* MEM_DEBUG */ + +#define free(p) use_tcc_free(p) +#define malloc(s) use_tcc_malloc(s) +#define realloc(p, s) use_tcc_realloc(p, s) + +/********************************************************/ +/* dynarrays */ + +ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data) +{ + int nb, nb_alloc; + void **pp; + + nb = *nb_ptr; + pp = *(void ***)ptab; + /* every power of two we double array size */ + if ((nb & (nb - 1)) == 0) { + if (!nb) + nb_alloc = 1; + else + nb_alloc = nb * 2; + pp = tcc_realloc(pp, nb_alloc * sizeof(void *)); + *(void***)ptab = pp; + } + pp[nb++] = data; + *nb_ptr = nb; +} + +ST_FUNC void dynarray_reset(void *pp, int *n) +{ + void **p; + for (p = *(void***)pp; *n; ++p, --*n) + if (*p) + tcc_free(*p); + tcc_free(*(void**)pp); + *(void**)pp = NULL; +} + +static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in) +{ + const char *p; + do { + int c; + CString str; + + cstr_new(&str); + for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) { + if (c == '{' && p[1] && p[2] == '}') { + c = p[1], p += 2; + if (c == 'B') + cstr_cat(&str, s->tcc_lib_path, -1); + } else { + cstr_ccat(&str, c); + } + } + if (str.size) { + cstr_ccat(&str, '\0'); + dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data)); + } + cstr_free(&str); + in = p+1; + } while (*p); +} + +/********************************************************/ + +static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap) +{ + int len; + len = strlen(buf); + vsnprintf(buf + len, buf_size - len, fmt, ap); +} + +static void strcat_printf(char *buf, int buf_size, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + strcat_vprintf(buf, buf_size, fmt, ap); + va_end(ap); +} + +static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap) +{ + char buf[2048]; + BufferedFile **pf, *f; + + buf[0] = '\0'; + /* use upper file if inline ":asm:" or token ":paste:" */ + for (f = file; f && f->filename[0] == ':'; f = f->prev) + ; + if (f) { + for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++) + strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n", + (*pf)->filename, (*pf)->line_num); + if (s1->error_set_jmp_enabled) { + strcat_printf(buf, sizeof(buf), "%s:%d: ", + f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL)); + } else { + strcat_printf(buf, sizeof(buf), "%s: ", + f->filename); + } + } else { + strcat_printf(buf, sizeof(buf), "tcc: "); + } + if (is_warning) + strcat_printf(buf, sizeof(buf), "warning: "); + else + strcat_printf(buf, sizeof(buf), "error: "); + strcat_vprintf(buf, sizeof(buf), fmt, ap); + + if (!s1->error_func) { + /* default case: stderr */ + if (s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout) + /* print a newline during tcc -E */ + printf("\n"), fflush(stdout); + fflush(stdout); /* flush -v output */ + fprintf(stderr, "%s\n", buf); + fflush(stderr); /* print error/warning now (win32) */ + } else { + s1->error_func(s1->error_opaque, buf); + } + if (!is_warning || s1->warn_error) + s1->nb_errors++; +} + +LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, + void (*error_func)(void *opaque, const char *msg)) +{ + s->error_opaque = error_opaque; + s->error_func = error_func; +} + +/* error without aborting current compilation */ +PUB_FUNC void tcc_error_noabort(const char *fmt, ...) +{ + TCCState *s1 = tcc_state; + va_list ap; + + va_start(ap, fmt); + error1(s1, 0, fmt, ap); + va_end(ap); +} + +PUB_FUNC void tcc_error(const char *fmt, ...) +{ + TCCState *s1 = tcc_state; + va_list ap; + + va_start(ap, fmt); + error1(s1, 0, fmt, ap); + va_end(ap); + /* better than nothing: in some cases, we accept to handle errors */ + if (s1->error_set_jmp_enabled) { + longjmp(s1->error_jmp_buf, 1); + } else { + /* XXX: eliminate this someday */ + exit(1); + } +} + +PUB_FUNC void tcc_warning(const char *fmt, ...) +{ + TCCState *s1 = tcc_state; + va_list ap; + + if (s1->warn_none) + return; + + va_start(ap, fmt); + error1(s1, 1, fmt, ap); + va_end(ap); +} + +/********************************************************/ +/* I/O layer */ + +ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen) +{ + BufferedFile *bf; + int buflen = initlen ? initlen : IO_BUF_SIZE; + + bf = tcc_mallocz(sizeof(BufferedFile) + buflen); + bf->buf_ptr = bf->buffer; + bf->buf_end = bf->buffer + initlen; + bf->buf_end[0] = CH_EOB; /* put eob symbol */ + pstrcpy(bf->filename, sizeof(bf->filename), filename); + bf->true_filename = bf->filename; + bf->line_num = 1; + bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; + bf->fd = -1; + bf->prev = file; + file = bf; + tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; +} + +ST_FUNC void tcc_close(void) +{ + BufferedFile *bf = file; + if (bf->fd > 0) { + close(bf->fd); + total_lines += bf->line_num; + } + if (bf->true_filename != bf->filename) + tcc_free(bf->true_filename); + file = bf->prev; + tcc_free(bf); +} + +ST_FUNC int tcc_open(TCCState *s1, const char *filename) +{ + int fd; + if (strcmp(filename, "-") == 0) + fd = 0, filename = ""; + else + fd = open(filename, O_RDONLY | O_BINARY); + if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3) + printf("%s %*s%s\n", fd < 0 ? "nf":"->", + (int)(s1->include_stack_ptr - s1->include_stack), "", filename); + if (fd < 0) + return -1; + tcc_open_bf(s1, filename, 0); +#ifdef _WIN32 + normalize_slashes(file->filename); +#endif + file->fd = fd; + return fd; +} + +/* compile the file opened in 'file'. Return non zero if errors. */ +static int tcc_compile(TCCState *s1) +{ + Sym *define_start; + int filetype, is_asm; + + define_start = define_stack; + filetype = s1->filetype; + is_asm = filetype == AFF_TYPE_ASM || filetype == AFF_TYPE_ASMPP; + tccelf_begin_file(s1); + + if (setjmp(s1->error_jmp_buf) == 0) { + s1->nb_errors = 0; + s1->error_set_jmp_enabled = 1; + + preprocess_start(s1, is_asm); + if (s1->output_type == TCC_OUTPUT_PREPROCESS) { + tcc_preprocess(s1); + } else if (is_asm) { +#ifdef CONFIG_TCC_ASM + tcc_assemble(s1, filetype == AFF_TYPE_ASMPP); +#else + tcc_error_noabort("asm not supported"); +#endif + } else { + tccgen_compile(s1); + } + } + s1->error_set_jmp_enabled = 0; + + preprocess_end(s1); + free_inline_functions(s1); + /* reset define stack, but keep -D and built-ins */ + free_defines(define_start); + sym_pop(&global_stack, NULL, 0); + sym_pop(&local_stack, NULL, 0); + tccelf_end_file(s1); + return s1->nb_errors != 0 ? -1 : 0; +} + +LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str) +{ + int len, ret; + + len = strlen(str); + tcc_open_bf(s, "", len); + memcpy(file->buffer, str, len); + ret = tcc_compile(s); + tcc_close(); + return ret; +} + +/* define a preprocessor symbol. A value can also be provided with the '=' operator */ +LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) +{ + int len1, len2; + /* default value */ + if (!value) + value = "1"; + len1 = strlen(sym); + len2 = strlen(value); + + /* init file structure */ + tcc_open_bf(s1, "", len1 + len2 + 1); + memcpy(file->buffer, sym, len1); + file->buffer[len1] = ' '; + memcpy(file->buffer + len1 + 1, value, len2); + + /* parse with define parser */ + next_nomacro(); + parse_define(); + tcc_close(); +} + +/* undefine a preprocessor symbol */ +LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym) +{ + TokenSym *ts; + Sym *s; + ts = tok_alloc(sym, strlen(sym)); + s = define_find(ts->tok); + /* undefine symbol by putting an invalid name */ + if (s) + define_undef(s); +} + +/* cleanup all static data used during compilation */ +static void tcc_cleanup(void) +{ + if (NULL == tcc_state) + return; + while (file) + tcc_close(); + tccpp_delete(tcc_state); + tcc_state = NULL; + /* free sym_pools */ + dynarray_reset(&sym_pools, &nb_sym_pools); + /* reset symbol stack */ + sym_free_first = NULL; +} + +LIBTCCAPI TCCState *tcc_new(void) +{ + TCCState *s; + + tcc_cleanup(); + + s = tcc_mallocz(sizeof(TCCState)); + if (!s) + return NULL; + tcc_state = s; + ++nb_states; + + s->alacarte_link = 1; + s->nocommon = 1; + s->warn_implicit_function_declaration = 1; + s->ms_extensions = 1; + +#ifdef CHAR_IS_UNSIGNED + s->char_is_unsigned = 1; +#endif +#ifdef TCC_TARGET_I386 + s->seg_size = 32; +#endif + /* enable this if you want symbols with leading underscore on windows: */ +#if 0 /* def TCC_TARGET_PE */ + s->leading_underscore = 1; +#endif +#ifdef _WIN32 + tcc_set_lib_path_w32(s); +#else + tcc_set_lib_path(s, CONFIG_TCCDIR); +#endif + tccelf_new(s); + tccpp_new(s); + + /* we add dummy defines for some special macros to speed up tests + and to have working defined() */ + define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL); + define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL); + define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL); + define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL); + define_push(TOK___COUNTER__, MACRO_OBJ, NULL, NULL); + { + /* define __TINYC__ 92X */ + char buffer[32]; int a,b,c; + sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c); + sprintf(buffer, "%d", a*10000 + b*100 + c); + tcc_define_symbol(s, "__TINYC__", buffer); + } + + /* standard defines */ + tcc_define_symbol(s, "__STDC__", NULL); + tcc_define_symbol(s, "__STDC_VERSION__", "199901L"); + tcc_define_symbol(s, "__STDC_HOSTED__", NULL); + + /* target defines */ +#if defined(TCC_TARGET_I386) + tcc_define_symbol(s, "__i386__", NULL); + tcc_define_symbol(s, "__i386", NULL); + tcc_define_symbol(s, "i386", NULL); +#elif defined(TCC_TARGET_X86_64) + tcc_define_symbol(s, "__x86_64__", NULL); +#elif defined(TCC_TARGET_ARM) + tcc_define_symbol(s, "__ARM_ARCH_4__", NULL); + tcc_define_symbol(s, "__arm_elf__", NULL); + tcc_define_symbol(s, "__arm_elf", NULL); + tcc_define_symbol(s, "arm_elf", NULL); + tcc_define_symbol(s, "__arm__", NULL); + tcc_define_symbol(s, "__arm", NULL); + tcc_define_symbol(s, "arm", NULL); + tcc_define_symbol(s, "__APCS_32__", NULL); + tcc_define_symbol(s, "__ARMEL__", NULL); +#if defined(TCC_ARM_EABI) + tcc_define_symbol(s, "__ARM_EABI__", NULL); +#endif +#if defined(TCC_ARM_HARDFLOAT) + s->float_abi = ARM_HARD_FLOAT; + tcc_define_symbol(s, "__ARM_PCS_VFP", NULL); +#else + s->float_abi = ARM_SOFTFP_FLOAT; +#endif +#elif defined(TCC_TARGET_ARM64) + tcc_define_symbol(s, "__aarch64__", NULL); +#elif defined TCC_TARGET_C67 + tcc_define_symbol(s, "__C67__", NULL); +#endif + +#ifdef TCC_TARGET_PE + tcc_define_symbol(s, "_WIN32", NULL); +# ifdef TCC_TARGET_X86_64 + tcc_define_symbol(s, "_WIN64", NULL); +# endif +#else + tcc_define_symbol(s, "__unix__", NULL); + tcc_define_symbol(s, "__unix", NULL); + tcc_define_symbol(s, "unix", NULL); +# if defined(__linux__) + tcc_define_symbol(s, "__linux__", NULL); + tcc_define_symbol(s, "__linux", NULL); +# endif +# if defined(__FreeBSD__) + tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__"); + /* No 'Thread Storage Local' on FreeBSD with tcc */ + tcc_define_symbol(s, "__NO_TLS", NULL); +# endif +# if defined(__FreeBSD_kernel__) + tcc_define_symbol(s, "__FreeBSD_kernel__", NULL); +# endif +# if defined(__NetBSD__) + tcc_define_symbol(s, "__NetBSD__", "__NetBSD__"); +# endif +# if defined(__OpenBSD__) + tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__"); +# endif +#endif + + /* TinyCC & gcc defines */ +#if PTR_SIZE == 4 + /* 32bit systems. */ + tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int"); + tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int"); + tcc_define_symbol(s, "__ILP32__", NULL); +#elif LONG_SIZE == 4 + /* 64bit Windows. */ + tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long"); + tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long"); + tcc_define_symbol(s, "__LLP64__", NULL); +#else + /* Other 64bit systems. */ + tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long"); + tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long"); + tcc_define_symbol(s, "__LP64__", NULL); +#endif + +#ifdef TCC_TARGET_PE + tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short"); + tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short"); +#else + tcc_define_symbol(s, "__WCHAR_TYPE__", "int"); + /* wint_t is unsigned int by default, but (signed) int on BSDs + and unsigned short on windows. Other OSes might have still + other conventions, sigh. */ +# if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) + tcc_define_symbol(s, "__WINT_TYPE__", "int"); +# ifdef __FreeBSD__ + /* define __GNUC__ to have some useful stuff from sys/cdefs.h + that are unconditionally used in FreeBSDs other system headers :/ */ + tcc_define_symbol(s, "__GNUC__", "2"); + tcc_define_symbol(s, "__GNUC_MINOR__", "7"); + tcc_define_symbol(s, "__builtin_alloca", "alloca"); +# endif +# else + tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int"); + /* glibc defines */ + tcc_define_symbol(s, "__REDIRECT(name, proto, alias)", + "name proto __asm__ (#alias)"); + tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", + "name proto __asm__ (#alias) __THROW"); +# endif +# if defined(TCC_MUSL) + tcc_define_symbol(s, "__DEFINED_va_list", ""); + tcc_define_symbol(s, "__DEFINED___isoc_va_list", ""); + tcc_define_symbol(s, "__isoc_va_list", "void *"); +# endif /* TCC_MUSL */ + /* Some GCC builtins that are simple to express as macros. */ + tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x"); +#endif /* ndef TCC_TARGET_PE */ + return s; +} + +LIBTCCAPI void tcc_delete(TCCState *s1) +{ + tcc_cleanup(); + + /* free sections */ + tccelf_delete(s1); + + /* free library paths */ + dynarray_reset(&s1->library_paths, &s1->nb_library_paths); + dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths); + + /* free include paths */ + dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes); + dynarray_reset(&s1->include_paths, &s1->nb_include_paths); + dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths); + dynarray_reset(&s1->cmd_include_files, &s1->nb_cmd_include_files); + + tcc_free(s1->tcc_lib_path); + tcc_free(s1->soname); + tcc_free(s1->rpath); + tcc_free(s1->init_symbol); + tcc_free(s1->fini_symbol); + tcc_free(s1->outfile); + tcc_free(s1->deps_outfile); + dynarray_reset(&s1->files, &s1->nb_files); + dynarray_reset(&s1->target_deps, &s1->nb_target_deps); + dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs); + dynarray_reset(&s1->argv, &s1->argc); + +#ifdef TCC_IS_NATIVE + /* free runtime memory */ + tcc_run_free(s1); +#endif + + tcc_free(s1); + if (0 == --nb_states) + tcc_memcheck(); +} + +LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) +{ + s->output_type = output_type; + + /* always elf for objects */ + if (output_type == TCC_OUTPUT_OBJ) + s->output_format = TCC_OUTPUT_FORMAT_ELF; + + if (s->char_is_unsigned) + tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL); + + if (!s->nostdinc) { + /* default include paths */ + /* -isystem paths have already been handled */ + tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS); + } + +#ifdef CONFIG_TCC_BCHECK + if (s->do_bounds_check) { + /* if bound checking, then add corresponding sections */ + tccelf_bounds_new(s); + /* define symbol */ + tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL); + } +#endif + if (s->do_debug) { + /* add debug sections */ + tccelf_stab_new(s); + } + + tcc_add_library_path(s, CONFIG_TCC_LIBPATHS); + +#ifdef TCC_TARGET_PE +# ifdef _WIN32 + if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ) + tcc_add_systemdir(s); +# endif +#else + /* paths for crt objects */ + tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX); + /* add libc crt1/crti objects */ + if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) && + !s->nostdlib) { + if (output_type != TCC_OUTPUT_DLL) + tcc_add_crt(s, "crt1.o"); + tcc_add_crt(s, "crti.o"); + } +#endif + return 0; +} + +LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname) +{ + tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname); + return 0; +} + +LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname) +{ + tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname); + return 0; +} + +ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) +{ + int ret; + + /* open the file */ + ret = tcc_open(s1, filename); + if (ret < 0) { + if (flags & AFF_PRINT_ERROR) + tcc_error_noabort("file '%s' not found", filename); + return ret; + } + + /* update target deps */ + dynarray_add(&s1->target_deps, &s1->nb_target_deps, + tcc_strdup(filename)); + + if (flags & AFF_TYPE_BIN) { + ElfW(Ehdr) ehdr; + int fd, obj_type; + + fd = file->fd; + obj_type = tcc_object_type(fd, &ehdr); + lseek(fd, 0, SEEK_SET); + +#ifdef TCC_TARGET_MACHO + if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib")) + obj_type = AFF_BINTYPE_DYN; +#endif + + switch (obj_type) { + case AFF_BINTYPE_REL: + ret = tcc_load_object_file(s1, fd, 0); + break; +#ifndef TCC_TARGET_PE + case AFF_BINTYPE_DYN: + if (s1->output_type == TCC_OUTPUT_MEMORY) { + ret = 0; +#ifdef TCC_IS_NATIVE + if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) + ret = -1; +#endif + } else { + ret = tcc_load_dll(s1, fd, filename, + (flags & AFF_REFERENCED_DLL) != 0); + } + break; +#endif + case AFF_BINTYPE_AR: + ret = tcc_load_archive(s1, fd); + break; +#ifdef TCC_TARGET_COFF + case AFF_BINTYPE_C67: + ret = tcc_load_coff(s1, fd); + break; +#endif + default: +#ifdef TCC_TARGET_PE + ret = pe_load_file(s1, filename, fd); +#else + /* as GNU ld, consider it is an ld script if not recognized */ + ret = tcc_load_ldscript(s1); +#endif + if (ret < 0) + tcc_error_noabort("unrecognized file type"); + break; + } + } else { + ret = tcc_compile(s1); + } + tcc_close(); + return ret; +} + +LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename) +{ + int filetype = s->filetype; + int flags = AFF_PRINT_ERROR; + if (filetype == 0) { + /* use a file extension to detect a filetype */ + const char *ext = tcc_fileextension(filename); + if (ext[0]) { + ext++; + if (!strcmp(ext, "S")) + filetype = AFF_TYPE_ASMPP; + else if (!strcmp(ext, "s")) + filetype = AFF_TYPE_ASM; + else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i")) + filetype = AFF_TYPE_C; + else + flags |= AFF_TYPE_BIN; + } else { + filetype = AFF_TYPE_C; + } + s->filetype = filetype; + } + return tcc_add_file_internal(s, filename, flags); +} + +LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname) +{ + tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname); + return 0; +} + +static int tcc_add_library_internal(TCCState *s, const char *fmt, + const char *filename, int flags, char **paths, int nb_paths) +{ + char buf[1024]; + int i; + + for(i = 0; i < nb_paths; i++) { + snprintf(buf, sizeof(buf), fmt, paths[i], filename); + if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0) + return 0; + } + return -1; +} + +/* find and load a dll. Return non zero if not found */ +/* XXX: add '-rpath' option support ? */ +ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags) +{ + return tcc_add_library_internal(s, "%s/%s", filename, flags, + s->library_paths, s->nb_library_paths); +} + +ST_FUNC int tcc_add_crt(TCCState *s, const char *filename) +{ + if (-1 == tcc_add_library_internal(s, "%s/%s", + filename, 0, s->crt_paths, s->nb_crt_paths)) + tcc_error_noabort("file '%s' not found", filename); + return 0; +} + +/* the library name is the same as the argument of the '-l' option */ +LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) +{ +#if defined TCC_TARGET_PE + const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL }; + const char **pp = s->static_link ? libs + 4 : libs; +#elif defined TCC_TARGET_MACHO + const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL }; + const char **pp = s->static_link ? libs + 1 : libs; +#else + const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL }; + const char **pp = s->static_link ? libs + 1 : libs; +#endif + while (*pp) { + if (0 == tcc_add_library_internal(s, *pp, + libraryname, 0, s->library_paths, s->nb_library_paths)) + return 0; + ++pp; + } + return -1; +} + +PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname) +{ + int ret = tcc_add_library(s, libname); + if (ret < 0) + tcc_error_noabort("library '%s' not found", libname); + return ret; +} + +/* handle #pragma comment(lib,) */ +ST_FUNC void tcc_add_pragma_libs(TCCState *s1) +{ + int i; + for (i = 0; i < s1->nb_pragma_libs; i++) + tcc_add_library_err(s1, s1->pragma_libs[i]); +} + +LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val) +{ +#ifdef TCC_TARGET_PE + /* On x86_64 'val' might not be reachable with a 32bit offset. + So it is handled here as if it were in a DLL. */ + pe_putimport(s, 0, name, (uintptr_t)val); +#else + set_elf_sym(symtab_section, (uintptr_t)val, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + SHN_ABS, name); +#endif + return 0; +} + +LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path) +{ + tcc_free(s->tcc_lib_path); + s->tcc_lib_path = tcc_strdup(path); +} + +#define WD_ALL 0x0001 /* warning is activated when using -Wall */ +#define FD_INVERT 0x0002 /* invert value before storing */ + +typedef struct FlagDef { + uint16_t offset; + uint16_t flags; + const char *name; +} FlagDef; + +static int no_flag(const char **pp) +{ + const char *p = *pp; + if (*p != 'n' || *++p != 'o' || *++p != '-') + return 0; + *pp = p + 1; + return 1; +} + +ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name) +{ + int value, ret; + const FlagDef *p; + const char *r; + + value = 1; + r = name; + if (no_flag(&r)) + value = 0; + + for (ret = -1, p = flags; p->name; ++p) { + if (ret) { + if (strcmp(r, p->name)) + continue; + } else { + if (0 == (p->flags & WD_ALL)) + continue; + } + if (p->offset) { + *(int*)((char *)s + p->offset) = + p->flags & FD_INVERT ? !value : value; + if (ret) + return 0; + } else { + ret = 0; + } + } + return ret; +} + +static int strstart(const char *val, const char **str) +{ + const char *p, *q; + p = *str; + q = val; + while (*q) { + if (*p != *q) + return 0; + p++; + q++; + } + *str = p; + return 1; +} + +/* Like strstart, but automatically takes into account that ld options can + * + * - start with double or single dash (e.g. '--soname' or '-soname') + * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so' + * or '-Wl,-soname=x.so') + * + * you provide `val` always in 'option[=]' form (no leading -) + */ +static int link_option(const char *str, const char *val, const char **ptr) +{ + const char *p, *q; + int ret; + + /* there should be 1 or 2 dashes */ + if (*str++ != '-') + return 0; + if (*str == '-') + str++; + + /* then str & val should match (potentially up to '=') */ + p = str; + q = val; + + ret = 1; + if (q[0] == '?') { + ++q; + if (no_flag(&p)) + ret = -1; + } + + while (*q != '\0' && *q != '=') { + if (*p != *q) + return 0; + p++; + q++; + } + + /* '=' near eos means ',' or '=' is ok */ + if (*q == '=') { + if (*p == 0) + *ptr = p; + if (*p != ',' && *p != '=') + return 0; + p++; + } else if (*p) { + return 0; + } + *ptr = p; + return ret; +} + +static const char *skip_linker_arg(const char **str) +{ + const char *s1 = *str; + const char *s2 = strchr(s1, ','); + *str = s2 ? s2++ : (s2 = s1 + strlen(s1)); + return s2; +} + +static void copy_linker_arg(char **pp, const char *s, int sep) +{ + const char *q = s; + char *p = *pp; + int l = 0; + if (p && sep) + p[l = strlen(p)] = sep, ++l; + skip_linker_arg(&q); + pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s); +} + +/* set linker options */ +static int tcc_set_linker(TCCState *s, const char *option) +{ + while (*option) { + + const char *p = NULL; + char *end = NULL; + int ignoring = 0; + int ret; + + if (link_option(option, "Bsymbolic", &p)) { + s->symbolic = 1; + } else if (link_option(option, "nostdlib", &p)) { + s->nostdlib = 1; + } else if (link_option(option, "fini=", &p)) { + copy_linker_arg(&s->fini_symbol, p, 0); + ignoring = 1; + } else if (link_option(option, "image-base=", &p) + || link_option(option, "Ttext=", &p)) { + s->text_addr = strtoull(p, &end, 16); + s->has_text_addr = 1; + } else if (link_option(option, "init=", &p)) { + copy_linker_arg(&s->init_symbol, p, 0); + ignoring = 1; + } else if (link_option(option, "oformat=", &p)) { +#if defined(TCC_TARGET_PE) + if (strstart("pe-", &p)) { +#elif PTR_SIZE == 8 + if (strstart("elf64-", &p)) { +#else + if (strstart("elf32-", &p)) { +#endif + s->output_format = TCC_OUTPUT_FORMAT_ELF; + } else if (!strcmp(p, "binary")) { + s->output_format = TCC_OUTPUT_FORMAT_BINARY; +#ifdef TCC_TARGET_COFF + } else if (!strcmp(p, "coff")) { + s->output_format = TCC_OUTPUT_FORMAT_COFF; +#endif + } else + goto err; + + } else if (link_option(option, "as-needed", &p)) { + ignoring = 1; + } else if (link_option(option, "O", &p)) { + ignoring = 1; + } else if (link_option(option, "export-all-symbols", &p)) { + s->rdynamic = 1; + } else if (link_option(option, "rpath=", &p)) { + copy_linker_arg(&s->rpath, p, ':'); + } else if (link_option(option, "enable-new-dtags", &p)) { + s->enable_new_dtags = 1; + } else if (link_option(option, "section-alignment=", &p)) { + s->section_align = strtoul(p, &end, 16); + } else if (link_option(option, "soname=", &p)) { + copy_linker_arg(&s->soname, p, 0); +#ifdef TCC_TARGET_PE + } else if (link_option(option, "large-address-aware", &p)) { + s->pe_characteristics |= 0x20; + } else if (link_option(option, "file-alignment=", &p)) { + s->pe_file_align = strtoul(p, &end, 16); + } else if (link_option(option, "stack=", &p)) { + s->pe_stack_size = strtoul(p, &end, 10); + } else if (link_option(option, "subsystem=", &p)) { +#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) + if (!strcmp(p, "native")) { + s->pe_subsystem = 1; + } else if (!strcmp(p, "console")) { + s->pe_subsystem = 3; + } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) { + s->pe_subsystem = 2; + } else if (!strcmp(p, "posix")) { + s->pe_subsystem = 7; + } else if (!strcmp(p, "efiapp")) { + s->pe_subsystem = 10; + } else if (!strcmp(p, "efiboot")) { + s->pe_subsystem = 11; + } else if (!strcmp(p, "efiruntime")) { + s->pe_subsystem = 12; + } else if (!strcmp(p, "efirom")) { + s->pe_subsystem = 13; +#elif defined(TCC_TARGET_ARM) + if (!strcmp(p, "wince")) { + s->pe_subsystem = 9; +#endif + } else + goto err; +#endif + } else if (ret = link_option(option, "?whole-archive", &p), ret) { + s->alacarte_link = ret < 0; + } else if (p) { + return 0; + } else { + err: + tcc_error("unsupported linker option '%s'", option); + } + + if (ignoring && s->warn_unsupported) + tcc_warning("unsupported linker option '%s'", option); + + option = skip_linker_arg(&p); + } + return 1; +} + +typedef struct TCCOption { + const char *name; + uint16_t index; + uint16_t flags; +} TCCOption; + +enum { + TCC_OPTION_HELP, + TCC_OPTION_HELP2, + TCC_OPTION_v, + TCC_OPTION_I, + TCC_OPTION_D, + TCC_OPTION_U, + TCC_OPTION_P, + TCC_OPTION_L, + TCC_OPTION_B, + TCC_OPTION_l, + TCC_OPTION_bench, + TCC_OPTION_bt, + TCC_OPTION_b, + TCC_OPTION_g, + TCC_OPTION_c, + TCC_OPTION_dumpversion, + TCC_OPTION_d, + TCC_OPTION_static, + TCC_OPTION_std, + TCC_OPTION_shared, + TCC_OPTION_soname, + TCC_OPTION_o, + TCC_OPTION_r, + TCC_OPTION_s, + TCC_OPTION_traditional, + TCC_OPTION_Wl, + TCC_OPTION_Wp, + TCC_OPTION_W, + TCC_OPTION_O, + TCC_OPTION_mfloat_abi, + TCC_OPTION_m, + TCC_OPTION_f, + TCC_OPTION_isystem, + TCC_OPTION_iwithprefix, + TCC_OPTION_include, + TCC_OPTION_nostdinc, + TCC_OPTION_nostdlib, + TCC_OPTION_print_search_dirs, + TCC_OPTION_rdynamic, + TCC_OPTION_param, + TCC_OPTION_pedantic, + TCC_OPTION_pthread, + TCC_OPTION_run, + TCC_OPTION_w, + TCC_OPTION_pipe, + TCC_OPTION_E, + TCC_OPTION_MD, + TCC_OPTION_MF, + TCC_OPTION_x, + TCC_OPTION_ar, + TCC_OPTION_impdef +}; + +#define TCC_OPTION_HAS_ARG 0x0001 +#define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */ + +static const TCCOption tcc_options[] = { + { "h", TCC_OPTION_HELP, 0 }, + { "-help", TCC_OPTION_HELP, 0 }, + { "?", TCC_OPTION_HELP, 0 }, + { "hh", TCC_OPTION_HELP2, 0 }, + { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG }, + { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG }, + { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG }, + { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG }, + { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG }, + { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "bench", TCC_OPTION_bench, 0 }, +#ifdef CONFIG_TCC_BACKTRACE + { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG }, +#endif +#ifdef CONFIG_TCC_BCHECK + { "b", TCC_OPTION_b, 0 }, +#endif + { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "c", TCC_OPTION_c, 0 }, + { "dumpversion", TCC_OPTION_dumpversion, 0}, + { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "static", TCC_OPTION_static, 0 }, + { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "shared", TCC_OPTION_shared, 0 }, + { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG }, + { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG }, + { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG }, + { "pedantic", TCC_OPTION_pedantic, 0}, + { "pthread", TCC_OPTION_pthread, 0}, + { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "rdynamic", TCC_OPTION_rdynamic, 0 }, + { "r", TCC_OPTION_r, 0 }, + { "s", TCC_OPTION_s, 0 }, + { "traditional", TCC_OPTION_traditional, 0 }, + { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, +#ifdef TCC_TARGET_ARM + { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG }, +#endif + { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG }, + { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG }, + { "nostdinc", TCC_OPTION_nostdinc, 0 }, + { "nostdlib", TCC_OPTION_nostdlib, 0 }, + { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 }, + { "w", TCC_OPTION_w, 0 }, + { "pipe", TCC_OPTION_pipe, 0}, + { "E", TCC_OPTION_E, 0}, + { "MD", TCC_OPTION_MD, 0}, + { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG }, + { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG }, + { "ar", TCC_OPTION_ar, 0}, +#ifdef TCC_TARGET_PE + { "impdef", TCC_OPTION_impdef, 0}, +#endif + { NULL, 0, 0 }, +}; + +static const FlagDef options_W[] = { + { 0, 0, "all" }, + { offsetof(TCCState, warn_unsupported), 0, "unsupported" }, + { offsetof(TCCState, warn_write_strings), 0, "write-strings" }, + { offsetof(TCCState, warn_error), 0, "error" }, + { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" }, + { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL, + "implicit-function-declaration" }, + { 0, 0, NULL } +}; + +static const FlagDef options_f[] = { + { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" }, + { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" }, + { offsetof(TCCState, nocommon), FD_INVERT, "common" }, + { offsetof(TCCState, leading_underscore), 0, "leading-underscore" }, + { offsetof(TCCState, ms_extensions), 0, "ms-extensions" }, + { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" }, + { 0, 0, NULL } +}; + +static const FlagDef options_m[] = { + { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" }, +#ifdef TCC_TARGET_X86_64 + { offsetof(TCCState, nosse), FD_INVERT, "sse" }, +#endif + { 0, 0, NULL } +}; + +static void parse_option_D(TCCState *s1, const char *optarg) +{ + char *sym = tcc_strdup(optarg); + char *value = strchr(sym, '='); + if (value) + *value++ = '\0'; + tcc_define_symbol(s1, sym, value); + tcc_free(sym); +} + +static void args_parser_add_file(TCCState *s, const char* filename, int filetype) +{ + struct filespec *f = tcc_malloc(sizeof *f + strlen(filename)); + f->type = filetype; + f->alacarte = s->alacarte_link; + strcpy(f->name, filename); + dynarray_add(&s->files, &s->nb_files, f); +} + +static int args_parser_make_argv(const char *r, int *argc, char ***argv) +{ + int ret = 0, q, c; + CString str; + for(;;) { + while (c = (unsigned char)*r, c && c <= ' ') + ++r; + if (c == 0) + break; + q = 0; + cstr_new(&str); + while (c = (unsigned char)*r, c) { + ++r; + if (c == '\\' && (*r == '"' || *r == '\\')) { + c = *r++; + } else if (c == '"') { + q = !q; + continue; + } else if (q == 0 && c <= ' ') { + break; + } + cstr_ccat(&str, c); + } + cstr_ccat(&str, 0); + //printf("<%s>\n", str.data), fflush(stdout); + dynarray_add(argv, argc, tcc_strdup(str.data)); + cstr_free(&str); + ++ret; + } + return ret; +} + +/* read list file */ +static void args_parser_listfile(TCCState *s, + const char *filename, int optind, int *pargc, char ***pargv) +{ + int fd, i; + size_t len; + char *p; + int argc = 0; + char **argv = NULL; + + fd = open(filename, O_RDONLY | O_BINARY); + if (fd < 0) + tcc_error("listfile '%s' not found", filename); + + len = lseek(fd, 0, SEEK_END); + p = tcc_malloc(len + 1), p[len] = 0; + lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd); + + for (i = 0; i < *pargc; ++i) + if (i == optind) + args_parser_make_argv(p, &argc, &argv); + else + dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i])); + + tcc_free(p); + dynarray_reset(&s->argv, &s->argc); + *pargc = s->argc = argc, *pargv = s->argv = argv; +} + +PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind) +{ + const TCCOption *popt; + const char *optarg, *r; + const char *run = NULL; + int last_o = -1; + int x; + CString linker_arg; /* collect -Wl options */ + int tool = 0, arg_start = 0, noaction = optind; + char **argv = *pargv; + int argc = *pargc; + + cstr_new(&linker_arg); + + while (optind < argc) { + r = argv[optind]; + if (r[0] == '@' && r[1] != '\0') { + args_parser_listfile(s, r + 1, optind, &argc, &argv); + continue; + } + optind++; + if (tool) { + if (r[0] == '-' && r[1] == 'v' && r[2] == 0) + ++s->verbose; + continue; + } +reparse: + if (r[0] != '-' || r[1] == '\0') { + if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */ + args_parser_add_file(s, r, s->filetype); + if (run) { + tcc_set_options(s, run); + arg_start = optind - 1; + break; + } + continue; + } + + /* find option in table */ + for(popt = tcc_options; ; ++popt) { + const char *p1 = popt->name; + const char *r1 = r + 1; + if (p1 == NULL) + tcc_error("invalid option -- '%s'", r); + if (!strstart(p1, &r1)) + continue; + optarg = r1; + if (popt->flags & TCC_OPTION_HAS_ARG) { + if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) { + if (optind >= argc) + arg_err: + tcc_error("argument to '%s' is missing", r); + optarg = argv[optind++]; + } + } else if (*r1 != '\0') + continue; + break; + } + + switch(popt->index) { + case TCC_OPTION_HELP: + return OPT_HELP; + case TCC_OPTION_HELP2: + return OPT_HELP2; + case TCC_OPTION_I: + tcc_add_include_path(s, optarg); + break; + case TCC_OPTION_D: + parse_option_D(s, optarg); + break; + case TCC_OPTION_U: + tcc_undefine_symbol(s, optarg); + break; + case TCC_OPTION_L: + tcc_add_library_path(s, optarg); + break; + case TCC_OPTION_B: + /* set tcc utilities path (mainly for tcc development) */ + tcc_set_lib_path(s, optarg); + break; + case TCC_OPTION_l: + args_parser_add_file(s, optarg, AFF_TYPE_LIB); + s->nb_libraries++; + break; + case TCC_OPTION_pthread: + parse_option_D(s, "_REENTRANT"); + s->option_pthread = 1; + break; + case TCC_OPTION_bench: + s->do_bench = 1; + break; +#ifdef CONFIG_TCC_BACKTRACE + case TCC_OPTION_bt: + tcc_set_num_callers(atoi(optarg)); + break; +#endif +#ifdef CONFIG_TCC_BCHECK + case TCC_OPTION_b: + s->do_bounds_check = 1; + s->do_debug = 1; + break; +#endif + case TCC_OPTION_g: + s->do_debug = 1; + break; + case TCC_OPTION_c: + x = TCC_OUTPUT_OBJ; + set_output_type: + if (s->output_type) + tcc_warning("-%s: overriding compiler action already specified", popt->name); + s->output_type = x; + break; + case TCC_OPTION_d: + if (*optarg == 'D') + s->dflag = 3; + else if (*optarg == 'M') + s->dflag = 7; + else if (*optarg == 't') + s->dflag = 16; + else if (isnum(*optarg)) + g_debug = atoi(optarg); + else + goto unsupported_option; + break; + case TCC_OPTION_static: + s->static_link = 1; + break; + case TCC_OPTION_std: + /* silently ignore, a current purpose: + allow to use a tcc as a reference compiler for "make test" */ + break; + case TCC_OPTION_shared: + x = TCC_OUTPUT_DLL; + goto set_output_type; + case TCC_OPTION_soname: + s->soname = tcc_strdup(optarg); + break; + case TCC_OPTION_o: + if (s->outfile) { + tcc_warning("multiple -o option"); + tcc_free(s->outfile); + } + s->outfile = tcc_strdup(optarg); + break; + case TCC_OPTION_r: + /* generate a .o merging several output files */ + s->option_r = 1; + x = TCC_OUTPUT_OBJ; + goto set_output_type; + case TCC_OPTION_isystem: + tcc_add_sysinclude_path(s, optarg); + break; + case TCC_OPTION_include: + dynarray_add(&s->cmd_include_files, + &s->nb_cmd_include_files, tcc_strdup(optarg)); + break; + case TCC_OPTION_nostdinc: + s->nostdinc = 1; + break; + case TCC_OPTION_nostdlib: + s->nostdlib = 1; + break; + case TCC_OPTION_run: +#ifndef TCC_IS_NATIVE + tcc_error("-run is not available in a cross compiler"); +#endif + run = optarg; + x = TCC_OUTPUT_MEMORY; + goto set_output_type; + case TCC_OPTION_v: + do ++s->verbose; while (*optarg++ == 'v'); + ++noaction; + break; + case TCC_OPTION_f: + if (set_flag(s, options_f, optarg) < 0) + goto unsupported_option; + break; +#ifdef TCC_TARGET_ARM + case TCC_OPTION_mfloat_abi: + /* tcc doesn't support soft float yet */ + if (!strcmp(optarg, "softfp")) { + s->float_abi = ARM_SOFTFP_FLOAT; + tcc_undefine_symbol(s, "__ARM_PCS_VFP"); + } else if (!strcmp(optarg, "hard")) + s->float_abi = ARM_HARD_FLOAT; + else + tcc_error("unsupported float abi '%s'", optarg); + break; +#endif + case TCC_OPTION_m: + if (set_flag(s, options_m, optarg) < 0) { + if (x = atoi(optarg), x != 32 && x != 64) + goto unsupported_option; + if (PTR_SIZE != x/8) + return x; + ++noaction; + } + break; + case TCC_OPTION_W: + if (set_flag(s, options_W, optarg) < 0) + goto unsupported_option; + break; + case TCC_OPTION_w: + s->warn_none = 1; + break; + case TCC_OPTION_rdynamic: + s->rdynamic = 1; + break; + case TCC_OPTION_Wl: + if (linker_arg.size) + --linker_arg.size, cstr_ccat(&linker_arg, ','); + cstr_cat(&linker_arg, optarg, 0); + if (tcc_set_linker(s, linker_arg.data)) + cstr_free(&linker_arg); + break; + case TCC_OPTION_Wp: + r = optarg; + goto reparse; + case TCC_OPTION_E: + x = TCC_OUTPUT_PREPROCESS; + goto set_output_type; + case TCC_OPTION_P: + s->Pflag = atoi(optarg) + 1; + break; + case TCC_OPTION_MD: + s->gen_deps = 1; + break; + case TCC_OPTION_MF: + s->deps_outfile = tcc_strdup(optarg); + break; + case TCC_OPTION_dumpversion: + printf ("%s\n", TCC_VERSION); + exit(0); + break; + case TCC_OPTION_x: + if (*optarg == 'c') + s->filetype = AFF_TYPE_C; + else if (*optarg == 'a') + s->filetype = AFF_TYPE_ASMPP; + else if (*optarg == 'n') + s->filetype = AFF_TYPE_NONE; + else + tcc_warning("unsupported language '%s'", optarg); + break; + case TCC_OPTION_O: + last_o = atoi(optarg); + break; + case TCC_OPTION_print_search_dirs: + x = OPT_PRINT_DIRS; + goto extra_action; + case TCC_OPTION_impdef: + x = OPT_IMPDEF; + goto extra_action; + case TCC_OPTION_ar: + x = OPT_AR; + extra_action: + arg_start = optind - 1; + if (arg_start != noaction) + tcc_error("cannot parse %s here", r); + tool = x; + break; + case TCC_OPTION_traditional: + case TCC_OPTION_pedantic: + case TCC_OPTION_pipe: + case TCC_OPTION_s: + /* ignored */ + break; + default: +unsupported_option: + if (s->warn_unsupported) + tcc_warning("unsupported option '%s'", r); + break; + } + } + if (last_o > 0) + tcc_define_symbol(s, "__OPTIMIZE__", NULL); + if (linker_arg.size) { + r = linker_arg.data; + goto arg_err; + } + *pargc = argc - arg_start; + *pargv = argv + arg_start; + if (tool) + return tool; + if (optind != noaction) + return 0; + if (s->verbose == 2) + return OPT_PRINT_DIRS; + if (s->verbose) + return OPT_V; + return OPT_HELP; +} + +LIBTCCAPI void tcc_set_options(TCCState *s, const char *r) +{ + char **argv = NULL; + int argc = 0; + args_parser_make_argv(r, &argc, &argv); + tcc_parse_args(s, &argc, &argv, 0); + dynarray_reset(&argv, &argc); +} + +PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time) +{ + if (total_time < 1) + total_time = 1; + if (total_bytes < 1) + total_bytes = 1; + fprintf(stderr, "* %d idents, %d lines, %d bytes\n" + "* %0.3f s, %u lines/s, %0.1f MB/s\n", + tok_ident - TOK_IDENT, total_lines, total_bytes, + (double)total_time/1000, + (unsigned)total_lines*1000/total_time, + (double)total_bytes/1000/total_time); +#ifdef MEM_DEBUG + fprintf(stderr, "* %d bytes memory used\n", mem_max_size); +#endif +} diff --git a/05/tcc-final-old/libtcc.h b/05/tcc-final-old/libtcc.h new file mode 100644 index 0000000..a1b31e3 --- /dev/null +++ b/05/tcc-final-old/libtcc.h @@ -0,0 +1,100 @@ +#ifndef LIBTCC_H +#define LIBTCC_H + +#ifndef LIBTCCAPI +# define LIBTCCAPI +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +struct TCCState; + +typedef struct TCCState TCCState; + +/* create a new TCC compilation context */ +LIBTCCAPI TCCState *tcc_new(void); + +/* free a TCC compilation context */ +LIBTCCAPI void tcc_delete(TCCState *s); + +/* set CONFIG_TCCDIR at runtime */ +LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path); + +/* set error/warning display callback */ +LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, + void (*error_func)(void *opaque, const char *msg)); + +/* set options as from command line (multiple supported) */ +LIBTCCAPI void tcc_set_options(TCCState *s, const char *str); + +/*****************************/ +/* preprocessor */ + +/* add include path */ +LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname); + +/* add in system include path */ +LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname); + +/* define preprocessor symbol 'sym'. Can put optional value */ +LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value); + +/* undefine preprocess symbol 'sym' */ +LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); + +/*****************************/ +/* compiling */ + +/* add a file (C file, dll, object, library, ld script). Return -1 if error. */ +LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename); + +/* compile a string containing a C source. Return -1 if error. */ +LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf); + +/*****************************/ +/* linking commands */ + +/* set output type. MUST BE CALLED before any compilation */ +LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type); +#define TCC_OUTPUT_MEMORY 1 /* output will be run in memory (default) */ +#define TCC_OUTPUT_EXE 2 /* executable file */ +#define TCC_OUTPUT_DLL 3 /* dynamic library */ +#define TCC_OUTPUT_OBJ 4 /* object file */ +#define TCC_OUTPUT_PREPROCESS 5 /* only preprocess (used internally) */ + +/* equivalent to -Lpath option */ +LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname); + +/* the library name is the same as the argument of the '-l' option */ +LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname); + +/* add a symbol to the compiled program */ +LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val); + +/* output an executable, library or object file. DO NOT call + tcc_relocate() before. */ +LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename); + +/* link and run main() function and return its value. DO NOT call + tcc_relocate() before. */ +LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv); + +/* do all relocations (needed before using tcc_get_symbol()) */ +LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr); +/* possible values for 'ptr': + - TCC_RELOCATE_AUTO : Allocate and manage memory internally + - NULL : return required memory size for the step below + - memory address : copy code to memory passed by the caller + returns -1 if error. */ +#define TCC_RELOCATE_AUTO (void*)1 + +/* return symbol value or NULL if not found */ +LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/tcc-final-old/stab.def b/05/tcc-final-old/stab.def new file mode 100644 index 0000000..48ea231 --- /dev/null +++ b/05/tcc-final-old/stab.def @@ -0,0 +1,234 @@ +/* Table of DBX symbol codes for the GNU system. + Copyright (C) 1988, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* This contains contribution from Cygnus Support. */ + +/* Global variable. Only the name is significant. + To find the address, look in the corresponding external symbol. */ +__define_stab (N_GSYM, 0x20, "GSYM") + +/* Function name for BSD Fortran. Only the name is significant. + To find the address, look in the corresponding external symbol. */ +__define_stab (N_FNAME, 0x22, "FNAME") + +/* Function name or text-segment variable for C. Value is its address. + Desc is supposedly starting line number, but GCC doesn't set it + and DBX seems not to miss it. */ +__define_stab (N_FUN, 0x24, "FUN") + +/* Data-segment variable with internal linkage. Value is its address. + "Static Sym". */ +__define_stab (N_STSYM, 0x26, "STSYM") + +/* BSS-segment variable with internal linkage. Value is its address. */ +__define_stab (N_LCSYM, 0x28, "LCSYM") + +/* Name of main routine. Only the name is significant. + This is not used in C. */ +__define_stab (N_MAIN, 0x2a, "MAIN") + +/* Global symbol in Pascal. + Supposedly the value is its line number; I'm skeptical. */ +__define_stab (N_PC, 0x30, "PC") + +/* Number of symbols: 0, files,,funcs,lines according to Ultrix V4.0. */ +__define_stab (N_NSYMS, 0x32, "NSYMS") + +/* "No DST map for sym: name, ,0,type,ignored" according to Ultrix V4.0. */ +__define_stab (N_NOMAP, 0x34, "NOMAP") + +/* New stab from Solaris. I don't know what it means, but it + don't seem to contain useful information. */ +__define_stab (N_OBJ, 0x38, "OBJ") + +/* New stab from Solaris. I don't know what it means, but it + don't seem to contain useful information. Possibly related to the + optimization flags used in this module. */ +__define_stab (N_OPT, 0x3c, "OPT") + +/* Register variable. Value is number of register. */ +__define_stab (N_RSYM, 0x40, "RSYM") + +/* Modula-2 compilation unit. Can someone say what info it contains? */ +__define_stab (N_M2C, 0x42, "M2C") + +/* Line number in text segment. Desc is the line number; + value is corresponding address. */ +__define_stab (N_SLINE, 0x44, "SLINE") + +/* Similar, for data segment. */ +__define_stab (N_DSLINE, 0x46, "DSLINE") + +/* Similar, for bss segment. */ +__define_stab (N_BSLINE, 0x48, "BSLINE") + +/* Sun's source-code browser stabs. ?? Don't know what the fields are. + Supposedly the field is "path to associated .cb file". THIS VALUE + OVERLAPS WITH N_BSLINE! */ +__define_stab (N_BROWS, 0x48, "BROWS") + +/* GNU Modula-2 definition module dependency. Value is the modification time + of the definition file. Other is non-zero if it is imported with the + GNU M2 keyword %INITIALIZE. Perhaps N_M2C can be used if there + are enough empty fields? */ +__define_stab(N_DEFD, 0x4a, "DEFD") + +/* THE FOLLOWING TWO STAB VALUES CONFLICT. Happily, one is for Modula-2 + and one is for C++. Still,... */ +/* GNU C++ exception variable. Name is variable name. */ +__define_stab (N_EHDECL, 0x50, "EHDECL") +/* Modula2 info "for imc": name,,0,0,0 according to Ultrix V4.0. */ +__define_stab (N_MOD2, 0x50, "MOD2") + +/* GNU C++ `catch' clause. Value is its address. Desc is nonzero if + this entry is immediately followed by a CAUGHT stab saying what exception + was caught. Multiple CAUGHT stabs means that multiple exceptions + can be caught here. If Desc is 0, it means all exceptions are caught + here. */ +__define_stab (N_CATCH, 0x54, "CATCH") + +/* Structure or union element. Value is offset in the structure. */ +__define_stab (N_SSYM, 0x60, "SSYM") + +/* Name of main source file. + Value is starting text address of the compilation. */ +__define_stab (N_SO, 0x64, "SO") + +/* Automatic variable in the stack. Value is offset from frame pointer. + Also used for type descriptions. */ +__define_stab (N_LSYM, 0x80, "LSYM") + +/* Beginning of an include file. Only Sun uses this. + In an object file, only the name is significant. + The Sun linker puts data into some of the other fields. */ +__define_stab (N_BINCL, 0x82, "BINCL") + +/* Name of sub-source file (#include file). + Value is starting text address of the compilation. */ +__define_stab (N_SOL, 0x84, "SOL") + +/* Parameter variable. Value is offset from argument pointer. + (On most machines the argument pointer is the same as the frame pointer. */ +__define_stab (N_PSYM, 0xa0, "PSYM") + +/* End of an include file. No name. + This and N_BINCL act as brackets around the file's output. + In an object file, there is no significant data in this entry. + The Sun linker puts data into some of the fields. */ +__define_stab (N_EINCL, 0xa2, "EINCL") + +/* Alternate entry point. Value is its address. */ +__define_stab (N_ENTRY, 0xa4, "ENTRY") + +/* Beginning of lexical block. + The desc is the nesting level in lexical blocks. + The value is the address of the start of the text for the block. + The variables declared inside the block *precede* the N_LBRAC symbol. */ +__define_stab (N_LBRAC, 0xc0, "LBRAC") + +/* Place holder for deleted include file. Replaces a N_BINCL and everything + up to the corresponding N_EINCL. The Sun linker generates these when + it finds multiple identical copies of the symbols from an include file. + This appears only in output from the Sun linker. */ +__define_stab (N_EXCL, 0xc2, "EXCL") + +/* Modula-2 scope information. Can someone say what info it contains? */ +__define_stab (N_SCOPE, 0xc4, "SCOPE") + +/* End of a lexical block. Desc matches the N_LBRAC's desc. + The value is the address of the end of the text for the block. */ +__define_stab (N_RBRAC, 0xe0, "RBRAC") + +/* Begin named common block. Only the name is significant. */ +__define_stab (N_BCOMM, 0xe2, "BCOMM") + +/* End named common block. Only the name is significant + (and it should match the N_BCOMM). */ +__define_stab (N_ECOMM, 0xe4, "ECOMM") + +/* End common (local name): value is address. + I'm not sure how this is used. */ +__define_stab (N_ECOML, 0xe8, "ECOML") + +/* These STAB's are used on Gould systems for Non-Base register symbols + or something like that. FIXME. I have assigned the values at random + since I don't have a Gould here. Fixups from Gould folk welcome... */ +__define_stab (N_NBTEXT, 0xF0, "NBTEXT") +__define_stab (N_NBDATA, 0xF2, "NBDATA") +__define_stab (N_NBBSS, 0xF4, "NBBSS") +__define_stab (N_NBSTS, 0xF6, "NBSTS") +__define_stab (N_NBLCS, 0xF8, "NBLCS") + +/* Second symbol entry containing a length-value for the preceding entry. + The value is the length. */ +__define_stab (N_LENG, 0xfe, "LENG") + +/* The above information, in matrix format. + + STAB MATRIX + _________________________________________________ + | 00 - 1F are not dbx stab symbols | + | In most cases, the low bit is the EXTernal bit| + + | 00 UNDEF | 02 ABS | 04 TEXT | 06 DATA | + | 01 |EXT | 03 |EXT | 05 |EXT | 07 |EXT | + + | 08 BSS | 0A INDR | 0C FN_SEQ | 0E | + | 09 |EXT | 0B | 0D | 0F | + + | 10 | 12 COMM | 14 SETA | 16 SETT | + | 11 | 13 | 15 | 17 | + + | 18 SETD | 1A SETB | 1C SETV | 1E WARNING| + | 19 | 1B | 1D | 1F FN | + + |_______________________________________________| + | Debug entries with bit 01 set are unused. | + | 20 GSYM | 22 FNAME | 24 FUN | 26 STSYM | + | 28 LCSYM | 2A MAIN | 2C | 2E | + | 30 PC | 32 NSYMS | 34 NOMAP | 36 | + | 38 OBJ | 3A | 3C OPT | 3E | + | 40 RSYM | 42 M2C | 44 SLINE | 46 DSLINE | + | 48 BSLINE*| 4A DEFD | 4C | 4E | + | 50 EHDECL*| 52 | 54 CATCH | 56 | + | 58 | 5A | 5C | 5E | + | 60 SSYM | 62 | 64 SO | 66 | + | 68 | 6A | 6C | 6E | + | 70 | 72 | 74 | 76 | + | 78 | 7A | 7C | 7E | + | 80 LSYM | 82 BINCL | 84 SOL | 86 | + | 88 | 8A | 8C | 8E | + | 90 | 92 | 94 | 96 | + | 98 | 9A | 9C | 9E | + | A0 PSYM | A2 EINCL | A4 ENTRY | A6 | + | A8 | AA | AC | AE | + | B0 | B2 | B4 | B6 | + | B8 | BA | BC | BE | + | C0 LBRAC | C2 EXCL | C4 SCOPE | C6 | + | C8 | CA | CC | CE | + | D0 | D2 | D4 | D6 | + | D8 | DA | DC | DE | + | E0 RBRAC | E2 BCOMM | E4 ECOMM | E6 | + | E8 ECOML | EA | EC | EE | + | F0 | F2 | F4 | F6 | + | F8 | FA | FC | FE LENG | + +-----------------------------------------------+ + * 50 EHDECL is also MOD2. + * 48 BSLINE is also BROWS. + */ diff --git a/05/tcc-final-old/stab.h b/05/tcc-final-old/stab.h new file mode 100644 index 0000000..80bd594 --- /dev/null +++ b/05/tcc-final-old/stab.h @@ -0,0 +1,17 @@ +#ifndef __GNU_STAB__ + +/* Indicate the GNU stab.h is in use. */ + +#define __GNU_STAB__ + +#define __define_stab(NAME, CODE, STRING) NAME=CODE, + +enum __stab_debug_code +{ +#include "stab.def" +LAST_UNUSED_STAB_CODE +}; + +#undef __define_stab + +#endif /* __GNU_STAB_ */ diff --git a/05/tcc-final-old/tcc-doc.texi b/05/tcc-final-old/tcc-doc.texi new file mode 100644 index 0000000..5e718a2 --- /dev/null +++ b/05/tcc-final-old/tcc-doc.texi @@ -0,0 +1,1326 @@ +\input texinfo @c -*- texinfo -*- +@c %**start of header +@setfilename tcc-doc.info +@settitle Tiny C Compiler Reference Documentation +@dircategory Software development +@direntry +* TCC: (tcc-doc). The Tiny C Compiler. +@end direntry +@c %**end of header + +@include config.texi + +@iftex +@titlepage +@afourpaper +@sp 7 +@center @titlefont{Tiny C Compiler Reference Documentation} +@sp 3 +@end titlepage +@headings double +@end iftex + +@contents + +@node Top, Introduction, (dir), (dir) +@top Tiny C Compiler Reference Documentation + +This manual documents version @value{VERSION} of the Tiny C Compiler. + +@menu +* Introduction:: Introduction to tcc. +* Invoke:: Invocation of tcc (command line, options). +* Clang:: ANSI C and extensions. +* asm:: Assembler syntax. +* linker:: Output file generation and supported targets. +* Bounds:: Automatic bounds-checking of C code. +* Libtcc:: The libtcc library. +* devel:: Guide for Developers. +@end menu + + +@node Introduction +@chapter Introduction + +TinyCC (aka TCC) is a small but hyper fast C compiler. Unlike other C +compilers, it is meant to be self-relying: you do not need an +external assembler or linker because TCC does that for you. + +TCC compiles so @emph{fast} that even for big projects @code{Makefile}s may +not be necessary. + +TCC not only supports ANSI C, but also most of the new ISO C99 +standard and many GNUC extensions including inline assembly. + +TCC can also be used to make @emph{C scripts}, i.e. pieces of C source +that you run as a Perl or Python script. Compilation is so fast that +your script will be as fast as if it was an executable. + +TCC can also automatically generate memory and bound checks +(@pxref{Bounds}) while allowing all C pointers operations. TCC can do +these checks even if non patched libraries are used. + +With @code{libtcc}, you can use TCC as a backend for dynamic code +generation (@pxref{Libtcc}). + +TCC mainly supports the i386 target on Linux and Windows. There are alpha +ports for the ARM (@code{arm-tcc}) and the TMS320C67xx targets +(@code{c67-tcc}). More information about the ARM port is available at +@url{http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html}. + +For usage on Windows, see also @url{tcc-win32.txt}. + +@node Invoke +@chapter Command line invocation + +@section Quick start + +@example +@c man begin SYNOPSIS +usage: tcc [options] [@var{infile1} @var{infile2}@dots{}] [@option{-run} @var{infile} @var{args}@dots{}] +@c man end +@end example + +@noindent +@c man begin DESCRIPTION +TCC options are a very much like gcc options. The main difference is that TCC +can also execute directly the resulting program and give it runtime +arguments. + +Here are some examples to understand the logic: + +@table @code +@item @samp{tcc -run a.c} +Compile @file{a.c} and execute it directly + +@item @samp{tcc -run a.c arg1} +Compile a.c and execute it directly. arg1 is given as first argument to +the @code{main()} of a.c. + +@item @samp{tcc a.c -run b.c arg1} +Compile @file{a.c} and @file{b.c}, link them together and execute them. arg1 is given +as first argument to the @code{main()} of the resulting program. +@ignore +Because multiple C files are specified, @option{--} are necessary to clearly +separate the program arguments from the TCC options. +@end ignore + +@item @samp{tcc -o myprog a.c b.c} +Compile @file{a.c} and @file{b.c}, link them and generate the executable @file{myprog}. + +@item @samp{tcc -o myprog a.o b.o} +link @file{a.o} and @file{b.o} together and generate the executable @file{myprog}. + +@item @samp{tcc -c a.c} +Compile @file{a.c} and generate object file @file{a.o}. + +@item @samp{tcc -c asmfile.S} +Preprocess with C preprocess and assemble @file{asmfile.S} and generate +object file @file{asmfile.o}. + +@item @samp{tcc -c asmfile.s} +Assemble (but not preprocess) @file{asmfile.s} and generate object file +@file{asmfile.o}. + +@item @samp{tcc -r -o ab.o a.c b.c} +Compile @file{a.c} and @file{b.c}, link them together and generate the object file @file{ab.o}. + +@end table + +Scripting: + +TCC can be invoked from @emph{scripts}, just as shell scripts. You just +need to add @code{#!/usr/local/bin/tcc -run} at the start of your C source: + +@example +#!/usr/local/bin/tcc -run +#include + +int main() +@{ + printf("Hello World\n"); + return 0; +@} +@end example + +TCC can read C source code from @emph{standard input} when @option{-} is used in +place of @option{infile}. Example: + +@example +echo 'main()@{puts("hello");@}' | tcc -run - +@end example +@c man end + +@section Option summary + +General Options: + +@c man begin OPTIONS +@table @option +@item -c +Generate an object file. + +@item -o outfile +Put object file, executable, or dll into output file @file{outfile}. + +@item -run source [args...] +Compile file @var{source} and run it with the command line arguments +@var{args}. In order to be able to give more than one argument to a +script, several TCC options can be given @emph{after} the +@option{-run} option, separated by spaces: +@example +tcc "-run -L/usr/X11R6/lib -lX11" ex4.c +@end example +In a script, it gives the following header: +@example +#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11 +@end example + +@item -v +Display TCC version. + +@item -vv +Show included files. As sole argument, print search dirs. -vvv shows tries too. + +@item -bench +Display compilation statistics. + +@end table + +Preprocessor options: + +@table @option +@item -Idir +Specify an additional include path. Include paths are searched in the +order they are specified. + +System include paths are always searched after. The default system +include paths are: @file{/usr/local/include}, @file{/usr/include} +and @file{PREFIX/lib/tcc/include}. (@file{PREFIX} is usually +@file{/usr} or @file{/usr/local}). + +@item -Dsym[=val] +Define preprocessor symbol @samp{sym} to +val. If val is not present, its value is @samp{1}. Function-like macros can +also be defined: @option{-DF(a)=a+1} + +@item -Usym +Undefine preprocessor symbol @samp{sym}. + +@item -E +Preprocess only, to stdout or file (with -o). + +@end table + +Compilation flags: + +Note: each of the following options has a negative form beginning with +@option{-fno-}. + +@table @option +@item -funsigned-char +Let the @code{char} type be unsigned. + +@item -fsigned-char +Let the @code{char} type be signed. + +@item -fno-common +Do not generate common symbols for uninitialized data. + +@item -fleading-underscore +Add a leading underscore at the beginning of each C symbol. + +@item -fms-extensions +Allow a MS C compiler extensions to the language. Currently this +assumes a nested named structure declaration without an identifier +behaves like an unnamed one. + +@item -fdollars-in-identifiers +Allow dollar signs in identifiers + +@end table + +Warning options: + +@table @option +@item -w +Disable all warnings. + +@end table + +Note: each of the following warning options has a negative form beginning with +@option{-Wno-}. + +@table @option +@item -Wimplicit-function-declaration +Warn about implicit function declaration. + +@item -Wunsupported +Warn about unsupported GCC features that are ignored by TCC. + +@item -Wwrite-strings +Make string constants be of type @code{const char *} instead of @code{char +*}. + +@item -Werror +Abort compilation if warnings are issued. + +@item -Wall +Activate all warnings, except @option{-Werror}, @option{-Wunusupported} and +@option{-Wwrite-strings}. + +@end table + +Linker options: + +@table @option +@item -Ldir +Specify an additional static library path for the @option{-l} option. The +default library paths are @file{/usr/local/lib}, @file{/usr/lib} and @file{/lib}. + +@item -lxxx +Link your program with dynamic library libxxx.so or static library +libxxx.a. The library is searched in the paths specified by the +@option{-L} option and @env{LIBRARY_PATH} variable. + +@item -Bdir +Set the path where the tcc internal libraries (and include files) can be +found (default is @file{PREFIX/lib/tcc}). + +@item -shared +Generate a shared library instead of an executable. + +@item -soname name +set name for shared library to be used at runtime + +@item -static +Generate a statically linked executable (default is a shared linked +executable). + +@item -rdynamic +Export global symbols to the dynamic linker. It is useful when a library +opened with @code{dlopen()} needs to access executable symbols. + +@item -r +Generate an object file combining all input files. + +@item -Wl,-rpath=path +Put custom search path for dynamic libraries into executable. + +@item -Wl,--enable-new-dtags +When putting a custom search path for dynamic libraries into the executable, +create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH. + +@item -Wl,--oformat=fmt +Use @var{fmt} as output format. The supported output formats are: +@table @code +@item elf32-i386 +ELF output format (default) +@item binary +Binary image (only for executable output) +@item coff +COFF output format (only for executable output for TMS320C67xx target) +@end table + +@item -Wl,-subsystem=console/gui/wince/... +Set type for PE (Windows) executables. + +@item -Wl,-[Ttext=# | section-alignment=# | file-alignment=# | image-base=# | stack=#] +Modify executable layout. + +@item -Wl,-Bsymbolic +Set DT_SYMBOLIC tag. + +@item -Wl,-(no-)whole-archive +Turn on/off linking of all objects in archives. + +@end table + +Debugger options: + +@table @option +@item -g +Generate run time debug information so that you get clear run time +error messages: @code{ test.c:68: in function 'test5()': dereferencing +invalid pointer} instead of the laconic @code{Segmentation +fault}. + +@item -b +Generate additional support code to check +memory allocations and array/pointer bounds. @option{-g} is implied. Note +that the generated code is slower and bigger in this case. + +Note: @option{-b} is only available on i386 when using libtcc for the moment. + +@item -bt N +Display N callers in stack traces. This is useful with @option{-g} or +@option{-b}. + +@end table + +Misc options: + +@table @option +@item -MD +Generate makefile fragment with dependencies. + +@item -MF depfile +Use @file{depfile} as output for -MD. + +@item -print-search-dirs +Print the configured installation directory and a list of library +and include directories tcc will search. + +@item -dumpversion +Print version. + +@end table + +Target specific options: + +@table @option +@item -mms-bitfields +Use an algorithm for bitfield alignment consistent with MSVC. Default is +gcc's algorithm. + +@item -mfloat-abi (ARM only) +Select the float ABI. Possible values: @code{softfp} and @code{hard} + +@item -mno-sse +Do not use sse registers on x86_64 + +@item -m32, -m64 +Pass command line to the i386/x86_64 cross compiler. + +@end table + +Note: GCC options @option{-Ox}, @option{-fx} and @option{-mx} are +ignored. +@c man end + +@c man begin ENVIRONMENT +Environment variables that affect how tcc operates. + +@table @option + +@item CPATH +@item C_INCLUDE_PATH +A colon-separated list of directories searched for include files, +directories given with @option{-I} are searched first. + +@item LIBRARY_PATH +A colon-separated list of directories searched for libraries for the +@option{-l} option, directories given with @option{-L} are searched first. + +@end table + +@c man end + +@ignore + +@setfilename tcc +@settitle Tiny C Compiler + +@c man begin SEEALSO +cpp(1), +gcc(1) +@c man end + +@c man begin AUTHOR +Fabrice Bellard +@c man end + +@end ignore + +@node Clang +@chapter C language support + +@section ANSI C + +TCC implements all the ANSI C standard, including structure bit fields +and floating point numbers (@code{long double}, @code{double}, and +@code{float} fully supported). + +@section ISOC99 extensions + +TCC implements many features of the new C standard: ISO C99. Currently +missing items are: complex and imaginary numbers. + +Currently implemented ISOC99 features: + +@itemize + +@item variable length arrays. + +@item 64 bit @code{long long} types are fully supported. + +@item The boolean type @code{_Bool} is supported. + +@item @code{__func__} is a string variable containing the current +function name. + +@item Variadic macros: @code{__VA_ARGS__} can be used for + function-like macros: +@example + #define dprintf(level, __VA_ARGS__) printf(__VA_ARGS__) +@end example + +@noindent +@code{dprintf} can then be used with a variable number of parameters. + +@item Declarations can appear anywhere in a block (as in C++). + +@item Array and struct/union elements can be initialized in any order by + using designators: +@example + struct @{ int x, y; @} st[10] = @{ [0].x = 1, [0].y = 2 @}; + + int tab[10] = @{ 1, 2, [5] = 5, [9] = 9@}; +@end example + +@item Compound initializers are supported: +@example + int *p = (int [])@{ 1, 2, 3 @}; +@end example +to initialize a pointer pointing to an initialized array. The same +works for structures and strings. + +@item Hexadecimal floating point constants are supported: +@example + double d = 0x1234p10; +@end example + +@noindent +is the same as writing +@example + double d = 4771840.0; +@end example + +@item @code{inline} keyword is ignored. + +@item @code{restrict} keyword is ignored. +@end itemize + +@section GNU C extensions + +TCC implements some GNU C extensions: + +@itemize + +@item array designators can be used without '=': +@example + int a[10] = @{ [0] 1, [5] 2, 3, 4 @}; +@end example + +@item Structure field designators can be a label: +@example + struct @{ int x, y; @} st = @{ x: 1, y: 1@}; +@end example +instead of +@example + struct @{ int x, y; @} st = @{ .x = 1, .y = 1@}; +@end example + +@item @code{\e} is ASCII character 27. + +@item case ranges : ranges can be used in @code{case}s: +@example + switch(a) @{ + case 1 @dots{} 9: + printf("range 1 to 9\n"); + break; + default: + printf("unexpected\n"); + break; + @} +@end example + +@cindex aligned attribute +@cindex packed attribute +@cindex section attribute +@cindex unused attribute +@cindex cdecl attribute +@cindex stdcall attribute +@cindex regparm attribute +@cindex dllexport attribute + +@item The keyword @code{__attribute__} is handled to specify variable or +function attributes. The following attributes are supported: + @itemize + + @item @code{aligned(n)}: align a variable or a structure field to n bytes +(must be a power of two). + + @item @code{packed}: force alignment of a variable or a structure field to + 1. + + @item @code{section(name)}: generate function or data in assembly section +name (name is a string containing the section name) instead of the default +section. + + @item @code{unused}: specify that the variable or the function is unused. + + @item @code{cdecl}: use standard C calling convention (default). + + @item @code{stdcall}: use Pascal-like calling convention. + + @item @code{regparm(n)}: use fast i386 calling convention. @var{n} must be +between 1 and 3. The first @var{n} function parameters are respectively put in +registers @code{%eax}, @code{%edx} and @code{%ecx}. + + @item @code{dllexport}: export function from dll/executable (win32 only) + + @end itemize + +Here are some examples: +@example + int a __attribute__ ((aligned(8), section(".mysection"))); +@end example + +@noindent +align variable @code{a} to 8 bytes and put it in section @code{.mysection}. + +@example + int my_add(int a, int b) __attribute__ ((section(".mycodesection"))) + @{ + return a + b; + @} +@end example + +@noindent +generate function @code{my_add} in section @code{.mycodesection}. + +@item GNU style variadic macros: +@example + #define dprintf(fmt, args@dots{}) printf(fmt, ## args) + + dprintf("no arg\n"); + dprintf("one arg %d\n", 1); +@end example + +@item @code{__FUNCTION__} is interpreted as C99 @code{__func__} +(so it has not exactly the same semantics as string literal GNUC +where it is a string literal). + +@item The @code{__alignof__} keyword can be used as @code{sizeof} +to get the alignment of a type or an expression. + +@item The @code{typeof(x)} returns the type of @code{x}. +@code{x} is an expression or a type. + +@item Computed gotos: @code{&&label} returns a pointer of type +@code{void *} on the goto label @code{label}. @code{goto *expr} can be +used to jump on the pointer resulting from @code{expr}. + +@item Inline assembly with asm instruction: +@cindex inline assembly +@cindex assembly, inline +@cindex __asm__ +@example +static inline void * my_memcpy(void * to, const void * from, size_t n) +@{ +int d0, d1, d2; +__asm__ __volatile__( + "rep ; movsl\n\t" + "testb $2,%b4\n\t" + "je 1f\n\t" + "movsw\n" + "1:\ttestb $1,%b4\n\t" + "je 2f\n\t" + "movsb\n" + "2:" + : "=&c" (d0), "=&D" (d1), "=&S" (d2) + :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from) + : "memory"); +return (to); +@} +@end example + +@noindent +@cindex gas +TCC includes its own x86 inline assembler with a @code{gas}-like (GNU +assembler) syntax. No intermediate files are generated. GCC 3.x named +operands are supported. + +@item @code{__builtin_types_compatible_p()} and @code{__builtin_constant_p()} +are supported. + +@item @code{#pragma pack} is supported for win32 compatibility. + +@end itemize + +@section TinyCC extensions + +@itemize + +@item @code{__TINYC__} is a predefined macro to indicate that you use TCC. + +@item @code{#!} at the start of a line is ignored to allow scripting. + +@item Binary digits can be entered (@code{0b101} instead of +@code{5}). + +@item @code{__BOUNDS_CHECKING_ON} is defined if bound checking is activated. + +@end itemize + +@node asm +@chapter TinyCC Assembler + +Since version 0.9.16, TinyCC integrates its own assembler. TinyCC +assembler supports a gas-like syntax (GNU assembler). You can +deactivate assembler support if you want a smaller TinyCC executable +(the C compiler does not rely on the assembler). + +TinyCC Assembler is used to handle files with @file{.S} (C +preprocessed assembler) and @file{.s} extensions. It is also used to +handle the GNU inline assembler with the @code{asm} keyword. + +@section Syntax + +TinyCC Assembler supports most of the gas syntax. The tokens are the +same as C. + +@itemize + +@item C and C++ comments are supported. + +@item Identifiers are the same as C, so you cannot use '.' or '$'. + +@item Only 32 bit integer numbers are supported. + +@end itemize + +@section Expressions + +@itemize + +@item Integers in decimal, octal and hexa are supported. + +@item Unary operators: +, -, ~. + +@item Binary operators in decreasing priority order: + +@enumerate +@item *, /, % +@item &, |, ^ +@item +, - +@end enumerate + +@item A value is either an absolute number or a label plus an offset. +All operators accept absolute values except '+' and '-'. '+' or '-' can be +used to add an offset to a label. '-' supports two labels only if they +are the same or if they are both defined and in the same section. + +@end itemize + +@section Labels + +@itemize + +@item All labels are considered as local, except undefined ones. + +@item Numeric labels can be used as local @code{gas}-like labels. +They can be defined several times in the same source. Use 'b' +(backward) or 'f' (forward) as suffix to reference them: + +@example + 1: + jmp 1b /* jump to '1' label before */ + jmp 1f /* jump to '1' label after */ + 1: +@end example + +@end itemize + +@section Directives +@cindex assembler directives +@cindex directives, assembler +@cindex align directive +@cindex skip directive +@cindex space directive +@cindex byte directive +@cindex word directive +@cindex short directive +@cindex int directive +@cindex long directive +@cindex quad directive +@cindex globl directive +@cindex global directive +@cindex section directive +@cindex text directive +@cindex data directive +@cindex bss directive +@cindex fill directive +@cindex org directive +@cindex previous directive +@cindex string directive +@cindex asciz directive +@cindex ascii directive + +All directives are preceded by a '.'. The following directives are +supported: + +@itemize +@item .align n[,value] +@item .skip n[,value] +@item .space n[,value] +@item .byte value1[,...] +@item .word value1[,...] +@item .short value1[,...] +@item .int value1[,...] +@item .long value1[,...] +@item .quad immediate_value1[,...] +@item .globl symbol +@item .global symbol +@item .section section +@item .text +@item .data +@item .bss +@item .fill repeat[,size[,value]] +@item .org n +@item .previous +@item .string string[,...] +@item .asciz string[,...] +@item .ascii string[,...] +@end itemize + +@section X86 Assembler +@cindex assembler + +All X86 opcodes are supported. Only ATT syntax is supported (source +then destination operand order). If no size suffix is given, TinyCC +tries to guess it from the operand sizes. + +Currently, MMX opcodes are supported but not SSE ones. + +@node linker +@chapter TinyCC Linker +@cindex linker + +@section ELF file generation +@cindex ELF + +TCC can directly output relocatable ELF files (object files), +executable ELF files and dynamic ELF libraries without relying on an +external linker. + +Dynamic ELF libraries can be output but the C compiler does not generate +position independent code (PIC). It means that the dynamic library +code generated by TCC cannot be factorized among processes yet. + +TCC linker eliminates unreferenced object code in libraries. A single pass is +done on the object and library list, so the order in which object files and +libraries are specified is important (same constraint as GNU ld). No grouping +options (@option{--start-group} and @option{--end-group}) are supported. + +@section ELF file loader + +TCC can load ELF object files, archives (.a files) and dynamic +libraries (.so). + +@section PE-i386 file generation +@cindex PE-i386 + +TCC for Windows supports the native Win32 executable file format (PE-i386). It +generates EXE files (console and gui) and DLL files. + +For usage on Windows, see also tcc-win32.txt. + +@section GNU Linker Scripts +@cindex scripts, linker +@cindex linker scripts +@cindex GROUP, linker command +@cindex FILE, linker command +@cindex OUTPUT_FORMAT, linker command +@cindex TARGET, linker command + +Because on many Linux systems some dynamic libraries (such as +@file{/usr/lib/libc.so}) are in fact GNU ld link scripts (horrible!), +the TCC linker also supports a subset of GNU ld scripts. + +The @code{GROUP} and @code{FILE} commands are supported. @code{OUTPUT_FORMAT} +and @code{TARGET} are ignored. + +Example from @file{/usr/lib/libc.so}: +@example +/* GNU ld script + Use the shared library, but some functions are only in + the static library, so try that secondarily. */ +GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a ) +@end example + +@node Bounds +@chapter TinyCC Memory and Bound checks +@cindex bound checks +@cindex memory checks + +This feature is activated with the @option{-b} (@pxref{Invoke}). + +Note that pointer size is @emph{unchanged} and that code generated +with bound checks is @emph{fully compatible} with unchecked +code. When a pointer comes from unchecked code, it is assumed to be +valid. Even very obscure C code with casts should work correctly. + +For more information about the ideas behind this method, see +@url{http://www.doc.ic.ac.uk/~phjk/BoundsChecking.html}. + +Here are some examples of caught errors: + +@table @asis + +@item Invalid range with standard string function: +@example +@{ + char tab[10]; + memset(tab, 0, 11); +@} +@end example + +@item Out of bounds-error in global or local arrays: +@example +@{ + int tab[10]; + for(i=0;i<11;i++) @{ + sum += tab[i]; + @} +@} +@end example + +@item Out of bounds-error in malloc'ed data: +@example +@{ + int *tab; + tab = malloc(20 * sizeof(int)); + for(i=0;i<21;i++) @{ + sum += tab4[i]; + @} + free(tab); +@} +@end example + +@item Access of freed memory: +@example +@{ + int *tab; + tab = malloc(20 * sizeof(int)); + free(tab); + for(i=0;i<20;i++) @{ + sum += tab4[i]; + @} +@} +@end example + +@item Double free: +@example +@{ + int *tab; + tab = malloc(20 * sizeof(int)); + free(tab); + free(tab); +@} +@end example + +@end table + +@node Libtcc +@chapter The @code{libtcc} library + +The @code{libtcc} library enables you to use TCC as a backend for +dynamic code generation. + +Read the @file{libtcc.h} to have an overview of the API. Read +@file{libtcc_test.c} to have a very simple example. + +The idea consists in giving a C string containing the program you want +to compile directly to @code{libtcc}. Then you can access to any global +symbol (function or variable) defined. + +@node devel +@chapter Developer's guide + +This chapter gives some hints to understand how TCC works. You can skip +it if you do not intend to modify the TCC code. + +@section File reading + +The @code{BufferedFile} structure contains the context needed to read a +file, including the current line number. @code{tcc_open()} opens a new +file and @code{tcc_close()} closes it. @code{inp()} returns the next +character. + +@section Lexer + +@code{next()} reads the next token in the current +file. @code{next_nomacro()} reads the next token without macro +expansion. + +@code{tok} contains the current token (see @code{TOK_xxx}) +constants. Identifiers and keywords are also keywords. @code{tokc} +contains additional infos about the token (for example a constant value +if number or string token). + +@section Parser + +The parser is hardcoded (yacc is not necessary). It does only one pass, +except: + +@itemize + +@item For initialized arrays with unknown size, a first pass +is done to count the number of elements. + +@item For architectures where arguments are evaluated in +reverse order, a first pass is done to reverse the argument order. + +@end itemize + +@section Types + +The types are stored in a single 'int' variable. It was chosen in the +first stages of development when tcc was much simpler. Now, it may not +be the best solution. + +@example +#define VT_INT 0 /* integer type */ +#define VT_BYTE 1 /* signed byte type */ +#define VT_SHORT 2 /* short type */ +#define VT_VOID 3 /* void type */ +#define VT_PTR 4 /* pointer */ +#define VT_ENUM 5 /* enum definition */ +#define VT_FUNC 6 /* function type */ +#define VT_STRUCT 7 /* struct/union definition */ +#define VT_FLOAT 8 /* IEEE float */ +#define VT_DOUBLE 9 /* IEEE double */ +#define VT_LDOUBLE 10 /* IEEE long double */ +#define VT_BOOL 11 /* ISOC99 boolean type */ +#define VT_LLONG 12 /* 64 bit integer */ +#define VT_LONG 13 /* long integer (NEVER USED as type, only + during parsing) */ +#define VT_BTYPE 0x000f /* mask for basic type */ +#define VT_UNSIGNED 0x0010 /* unsigned type */ +#define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */ +#define VT_VLA 0x20000 /* VLA type (also has VT_PTR and VT_ARRAY) */ +#define VT_BITFIELD 0x0040 /* bitfield modifier */ +#define VT_CONSTANT 0x0800 /* const modifier */ +#define VT_VOLATILE 0x1000 /* volatile modifier */ +#define VT_DEFSIGN 0x2000 /* signed type */ + +#define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */ +@end example + +When a reference to another type is needed (for pointers, functions and +structures), the @code{32 - VT_STRUCT_SHIFT} high order bits are used to +store an identifier reference. + +The @code{VT_UNSIGNED} flag can be set for chars, shorts, ints and long +longs. + +Arrays are considered as pointers @code{VT_PTR} with the flag +@code{VT_ARRAY} set. Variable length arrays are considered as special +arrays and have flag @code{VT_VLA} set instead of @code{VT_ARRAY}. + +The @code{VT_BITFIELD} flag can be set for chars, shorts, ints and long +longs. If it is set, then the bitfield position is stored from bits +VT_STRUCT_SHIFT to VT_STRUCT_SHIFT + 5 and the bit field size is stored +from bits VT_STRUCT_SHIFT + 6 to VT_STRUCT_SHIFT + 11. + +@code{VT_LONG} is never used except during parsing. + +During parsing, the storage of an object is also stored in the type +integer: + +@example +#define VT_EXTERN 0x00000080 /* extern definition */ +#define VT_STATIC 0x00000100 /* static variable */ +#define VT_TYPEDEF 0x00000200 /* typedef definition */ +#define VT_INLINE 0x00000400 /* inline definition */ +#define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */ +#define VT_EXPORT 0x00008000 /* win32: data exported from dll */ +#define VT_WEAK 0x00010000 /* win32: data exported from dll */ +@end example + +@section Symbols + +All symbols are stored in hashed symbol stacks. Each symbol stack +contains @code{Sym} structures. + +@code{Sym.v} contains the symbol name (remember +an identifier is also a token, so a string is never necessary to store +it). @code{Sym.t} gives the type of the symbol. @code{Sym.r} is usually +the register in which the corresponding variable is stored. @code{Sym.c} is +usually a constant associated to the symbol like its address for normal +symbols, and the number of entries for symbols representing arrays. +Variable length array types use @code{Sym.c} as a location on the stack +which holds the runtime sizeof for the type. + +Four main symbol stacks are defined: + +@table @code + +@item define_stack +for the macros (@code{#define}s). + +@item global_stack +for the global variables, functions and types. + +@item local_stack +for the local variables, functions and types. + +@item global_label_stack +for the local labels (for @code{goto}). + +@item label_stack +for GCC block local labels (see the @code{__label__} keyword). + +@end table + +@code{sym_push()} is used to add a new symbol in the local symbol +stack. If no local symbol stack is active, it is added in the global +symbol stack. + +@code{sym_pop(st,b)} pops symbols from the symbol stack @var{st} until +the symbol @var{b} is on the top of stack. If @var{b} is NULL, the stack +is emptied. + +@code{sym_find(v)} return the symbol associated to the identifier +@var{v}. The local stack is searched first from top to bottom, then the +global stack. + +@section Sections + +The generated code and data are written in sections. The structure +@code{Section} contains all the necessary information for a given +section. @code{new_section()} creates a new section. ELF file semantics +is assumed for each section. + +The following sections are predefined: + +@table @code + +@item text_section +is the section containing the generated code. @var{ind} contains the +current position in the code section. + +@item data_section +contains initialized data + +@item bss_section +contains uninitialized data + +@item bounds_section +@itemx lbounds_section +are used when bound checking is activated + +@item stab_section +@itemx stabstr_section +are used when debugging is active to store debug information + +@item symtab_section +@itemx strtab_section +contain the exported symbols (currently only used for debugging). + +@end table + +@section Code generation +@cindex code generation + +@subsection Introduction + +The TCC code generator directly generates linked binary code in one +pass. It is rather unusual these days (see gcc for example which +generates text assembly), but it can be very fast and surprisingly +little complicated. + +The TCC code generator is register based. Optimization is only done at +the expression level. No intermediate representation of expression is +kept except the current values stored in the @emph{value stack}. + +On x86, three temporary registers are used. When more registers are +needed, one register is spilled into a new temporary variable on the stack. + +@subsection The value stack +@cindex value stack, introduction + +When an expression is parsed, its value is pushed on the value stack +(@var{vstack}). The top of the value stack is @var{vtop}. Each value +stack entry is the structure @code{SValue}. + +@code{SValue.t} is the type. @code{SValue.r} indicates how the value is +currently stored in the generated code. It is usually a CPU register +index (@code{REG_xxx} constants), but additional values and flags are +defined: + +@example +#define VT_CONST 0x00f0 +#define VT_LLOCAL 0x00f1 +#define VT_LOCAL 0x00f2 +#define VT_CMP 0x00f3 +#define VT_JMP 0x00f4 +#define VT_JMPI 0x00f5 +#define VT_LVAL 0x0100 +#define VT_SYM 0x0200 +#define VT_MUSTCAST 0x0400 +#define VT_MUSTBOUND 0x0800 +#define VT_BOUNDED 0x8000 +#define VT_LVAL_BYTE 0x1000 +#define VT_LVAL_SHORT 0x2000 +#define VT_LVAL_UNSIGNED 0x4000 +#define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED) +@end example + +@table @code + +@item VT_CONST +indicates that the value is a constant. It is stored in the union +@code{SValue.c}, depending on its type. + +@item VT_LOCAL +indicates a local variable pointer at offset @code{SValue.c.i} in the +stack. + +@item VT_CMP +indicates that the value is actually stored in the CPU flags (i.e. the +value is the consequence of a test). The value is either 0 or 1. The +actual CPU flags used is indicated in @code{SValue.c.i}. + +If any code is generated which destroys the CPU flags, this value MUST be +put in a normal register. + +@item VT_JMP +@itemx VT_JMPI +indicates that the value is the consequence of a conditional jump. For VT_JMP, +it is 1 if the jump is taken, 0 otherwise. For VT_JMPI it is inverted. + +These values are used to compile the @code{||} and @code{&&} logical +operators. + +If any code is generated, this value MUST be put in a normal +register. Otherwise, the generated code won't be executed if the jump is +taken. + +@item VT_LVAL +is a flag indicating that the value is actually an lvalue (left value of +an assignment). It means that the value stored is actually a pointer to +the wanted value. + +Understanding the use @code{VT_LVAL} is very important if you want to +understand how TCC works. + +@item VT_LVAL_BYTE +@itemx VT_LVAL_SHORT +@itemx VT_LVAL_UNSIGNED +if the lvalue has an integer type, then these flags give its real +type. The type alone is not enough in case of cast optimisations. + +@item VT_LLOCAL +is a saved lvalue on the stack. @code{VT_LVAL} must also be set with +@code{VT_LLOCAL}. @code{VT_LLOCAL} can arise when a @code{VT_LVAL} in +a register has to be saved to the stack, or it can come from an +architecture-specific calling convention. + +@item VT_MUSTCAST +indicates that a cast to the value type must be performed if the value +is used (lazy casting). + +@item VT_SYM +indicates that the symbol @code{SValue.sym} must be added to the constant. + +@item VT_MUSTBOUND +@itemx VT_BOUNDED +are only used for optional bound checking. + +@end table + +@subsection Manipulating the value stack +@cindex value stack + +@code{vsetc()} and @code{vset()} pushes a new value on the value +stack. If the previous @var{vtop} was stored in a very unsafe place(for +example in the CPU flags), then some code is generated to put the +previous @var{vtop} in a safe storage. + +@code{vpop()} pops @var{vtop}. In some cases, it also generates cleanup +code (for example if stacked floating point registers are used as on +x86). + +The @code{gv(rc)} function generates code to evaluate @var{vtop} (the +top value of the stack) into registers. @var{rc} selects in which +register class the value should be put. @code{gv()} is the @emph{most +important function} of the code generator. + +@code{gv2()} is the same as @code{gv()} but for the top two stack +entries. + +@subsection CPU dependent code generation +@cindex CPU dependent +See the @file{i386-gen.c} file to have an example. + +@table @code + +@item load() +must generate the code needed to load a stack value into a register. + +@item store() +must generate the code needed to store a register into a stack value +lvalue. + +@item gfunc_start() +@itemx gfunc_param() +@itemx gfunc_call() +should generate a function call + +@item gfunc_prolog() +@itemx gfunc_epilog() +should generate a function prolog/epilog. + +@item gen_opi(op) +must generate the binary integer operation @var{op} on the two top +entries of the stack which are guaranteed to contain integer types. + +The result value should be put on the stack. + +@item gen_opf(op) +same as @code{gen_opi()} for floating point operations. The two top +entries of the stack are guaranteed to contain floating point values of +same types. + +@item gen_cvt_itof() +integer to floating point conversion. + +@item gen_cvt_ftoi() +floating point to integer conversion. + +@item gen_cvt_ftof() +floating point to floating point of different size conversion. + +@item gen_bounded_ptr_add() +@item gen_bounded_ptr_deref() +are only used for bounds checking. + +@end table + +@section Optimizations done +@cindex optimizations +@cindex constant propagation +@cindex strength reduction +@cindex comparison operators +@cindex caching processor flags +@cindex flags, caching +@cindex jump optimization +Constant propagation is done for all operations. Multiplications and +divisions are optimized to shifts when appropriate. Comparison +operators are optimized by maintaining a special cache for the +processor flags. &&, || and ! are optimized by maintaining a special +'jump target' value. No other jump optimization is currently performed +because it would require to store the code in a more abstract fashion. + +@unnumbered Concept Index +@printindex cp + +@bye + +@c Local variables: +@c fill-column: 78 +@c texinfo-column-for-description: 32 +@c End: diff --git a/05/tcc-final-old/tcc.c b/05/tcc-final-old/tcc.c new file mode 100644 index 0000000..740aa49 --- /dev/null +++ b/05/tcc-final-old/tcc.c @@ -0,0 +1,371 @@ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" +#if ONE_SOURCE +# include "libtcc.c" +#endif +#include "tcctools.c" + +static const char help[] = + "Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n" + "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n" + " tcc [options...] -run infile [arguments...]\n" + "General options:\n" + " -c compile only - generate an object file\n" + " -o outfile set output filename\n" + " -run run compiled source\n" + " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n" + " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n" + " -w disable all warnings\n" + " -v -vv show version, show search paths or loaded files\n" + " -h -hh show this, show more help\n" + " -bench show compilation statistics\n" + " - use stdin pipe as infile\n" + " @listfile read arguments from listfile\n" + "Preprocessor options:\n" + " -Idir add include path 'dir'\n" + " -Dsym[=val] define 'sym' with value 'val'\n" + " -Usym undefine 'sym'\n" + " -E preprocess only\n" + "Linker options:\n" + " -Ldir add library path 'dir'\n" + " -llib link with dynamic or static library 'lib'\n" + " -r generate (relocatable) object file\n" + " -shared generate a shared library/dll\n" + " -rdynamic export all global symbols to dynamic linker\n" + " -soname set name for shared library to be used at runtime\n" + " -Wl,-opt[=val] set linker option (see tcc -hh)\n" + "Debugger options:\n" + " -g generate runtime debug info\n" +#ifdef CONFIG_TCC_BCHECK + " -b compile with built-in memory and bounds checker (implies -g)\n" +#endif +#ifdef CONFIG_TCC_BACKTRACE + " -bt N show N callers in stack traces\n" +#endif + "Misc. options:\n" + " -x[c|a|n] specify type of the next infile\n" + " -nostdinc do not use standard system include paths\n" + " -nostdlib do not link with standard crt and libraries\n" + " -Bdir set tcc's private include/library dir\n" + " -MD generate dependency file for make\n" + " -MF file specify dependency file name\n" + " -m32/64 defer to i386/x86_64 cross compiler\n" + "Tools:\n" + " create library : tcc -ar [rcsv] lib.a files\n" +#ifdef TCC_TARGET_PE + " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n" +#endif + ; + +static const char help2[] = + "Tiny C Compiler "TCC_VERSION" - More Options\n" + "Special options:\n" + " -P -P1 with -E: no/alternative #line output\n" + " -dD -dM with -E: output #define directives\n" + " -pthread same as -D_REENTRANT and -lpthread\n" + " -On same as -D__OPTIMIZE__ for n > 0\n" + " -Wp,-opt same as -opt\n" + " -include file include 'file' above each input file\n" + " -isystem dir add 'dir' to system include path\n" + " -static link to static libraries (not recommended)\n" + " -dumpversion print version\n" + " -print-search-dirs print search paths\n" + " -dt with -run/-E: auto-define 'test_...' macros\n" + "Ignored options:\n" + " --param -pedantic -pipe -s -std -traditional\n" + "-W... warnings:\n" + " all turn on some (*) warnings\n" + " error stop after first warning\n" + " unsupported warn about ignored options, pragmas, etc.\n" + " write-strings strings are const\n" + " implicit-function-declaration warn for missing prototype (*)\n" + "-f[no-]... flags:\n" + " unsigned-char default char is unsigned\n" + " signed-char default char is signed\n" + " common use common section instead of bss\n" + " leading-underscore decorate extern symbols\n" + " ms-extensions allow anonymous struct in struct\n" + " dollars-in-identifiers allow '$' in C symbols\n" + "-m... target specific options:\n" + " ms-bitfields use MSVC bitfield layout\n" +#ifdef TCC_TARGET_ARM + " float-abi hard/softfp on arm\n" +#endif +#ifdef TCC_TARGET_X86_64 + " no-sse disable floats on x86_64\n" +#endif + "-Wl,... linker options:\n" + " -nostdlib do not link with standard crt/libs\n" + " -[no-]whole-archive load lib(s) fully/only as needed\n" + " -export-all-symbols same as -rdynamic\n" + " -image-base= -Ttext= set base address of executable\n" + " -section-alignment= set section alignment in executable\n" +#ifdef TCC_TARGET_PE + " -file-alignment= set PE file alignment\n" + " -stack= set PE stack reserve\n" + " -large-address-aware set related PE option\n" + " -subsystem=[console/windows] set PE subsystem\n" + " -oformat=[pe-* binary] set executable output format\n" + "Predefined macros:\n" + " tcc -E -dM - < nul\n" +#else + " -rpath= set dynamic library search path\n" + " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n" + " -soname= set DT_SONAME elf tag\n" + " -Bsymbolic set DT_SYMBOLIC elf tag\n" + " -oformat=[elf32/64-* binary] set executable output format\n" + " -init= -fini= -as-needed -O (ignored)\n" + "Predefined macros:\n" + " tcc -E -dM - < /dev/null\n" +#endif + "See also the manual for more details.\n" + ; + +static const char version[] = + "tcc version "TCC_VERSION" (" +#ifdef TCC_TARGET_I386 + "i386" +#elif defined TCC_TARGET_X86_64 + "x86_64" +#elif defined TCC_TARGET_C67 + "C67" +#elif defined TCC_TARGET_ARM + "ARM" +#elif defined TCC_TARGET_ARM64 + "AArch64" +#endif +#ifdef TCC_ARM_HARDFLOAT + " Hard Float" +#endif +#ifdef TCC_TARGET_PE + " Windows" +#elif defined(TCC_TARGET_MACHO) + " Darwin" +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + " FreeBSD" +#else + " Linux" +#endif + ")\n" + ; + +static void print_dirs(const char *msg, char **paths, int nb_paths) +{ + int i; + printf("%s:\n%s", msg, nb_paths ? "" : " -\n"); + for(i = 0; i < nb_paths; i++) + printf(" %s\n", paths[i]); +} + +static void print_search_dirs(TCCState *s) +{ + printf("install: %s\n", s->tcc_lib_path); + /* print_dirs("programs", NULL, 0); */ + print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths); + print_dirs("libraries", s->library_paths, s->nb_library_paths); + printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path); +#ifndef TCC_TARGET_PE + print_dirs("crt", s->crt_paths, s->nb_crt_paths); + printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s)); +#endif +} + +static void set_environment(TCCState *s) +{ + char * path; + + path = getenv("C_INCLUDE_PATH"); + if(path != NULL) { + tcc_add_sysinclude_path(s, path); + } + path = getenv("CPATH"); + if(path != NULL) { + tcc_add_include_path(s, path); + } + path = getenv("LIBRARY_PATH"); + if(path != NULL) { + tcc_add_library_path(s, path); + } +} + +static char *default_outputfile(TCCState *s, const char *first_file) +{ + char buf[1024]; + char *ext; + const char *name = "a"; + + if (first_file && strcmp(first_file, "-")) + name = tcc_basename(first_file); + snprintf(buf, sizeof(buf), "%s", name); + ext = tcc_fileextension(buf); +#ifdef TCC_TARGET_PE + if (s->output_type == TCC_OUTPUT_DLL) + strcpy(ext, ".dll"); + else + if (s->output_type == TCC_OUTPUT_EXE) + strcpy(ext, ".exe"); + else +#endif + if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext) + strcpy(ext, ".o"); + else + strcpy(buf, "a.out"); + return tcc_strdup(buf); +} + +static unsigned getclock_ms(void) +{ +#ifdef _WIN32 + return GetTickCount(); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec*1000 + (tv.tv_usec+500)/1000; +#endif +} + +int main(int argc0, char **argv0) +{ + TCCState *s; + int ret, opt, n = 0, t = 0; + unsigned start_time = 0; + const char *first_file; + int argc; char **argv; + FILE *ppfp = stdout; + +redo: + argc = argc0, argv = argv0; + s = tcc_new(); + opt = tcc_parse_args(s, &argc, &argv, 1); + + if ((n | t) == 0) { + if (opt == OPT_HELP) + return printf(help), 0; + if (opt == OPT_HELP2) + return printf(help2), 0; + if (opt == OPT_M32 || opt == OPT_M64) + tcc_tool_cross(s, argv, opt); /* never returns */ + if (s->verbose) + printf(version); + if (opt == OPT_AR) + return tcc_tool_ar(s, argc, argv); +#ifdef TCC_TARGET_PE + if (opt == OPT_IMPDEF) + return tcc_tool_impdef(s, argc, argv); +#endif + if (opt == OPT_V) + return 0; + if (opt == OPT_PRINT_DIRS) { + /* initialize search dirs */ + set_environment(s); + tcc_set_output_type(s, TCC_OUTPUT_MEMORY); + print_search_dirs(s); + return 0; + } + + n = s->nb_files; + if (n == 0) + tcc_error("no input files\n"); + + if (s->output_type == TCC_OUTPUT_PREPROCESS) { + if (s->outfile) { + ppfp = fopen(s->outfile, "w"); + if (!ppfp) + tcc_error("could not write '%s'", s->outfile); + } + } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) { + if (s->nb_libraries) + tcc_error("cannot specify libraries with -c"); + if (n > 1 && s->outfile) + tcc_error("cannot specify output file with -c many files"); + } else { + if (s->option_pthread) + tcc_set_options(s, "-lpthread"); + } + + if (s->do_bench) + start_time = getclock_ms(); + } + + set_environment(s); + if (s->output_type == 0) + s->output_type = TCC_OUTPUT_EXE; + tcc_set_output_type(s, s->output_type); + s->ppfp = ppfp; + + if ((s->output_type == TCC_OUTPUT_MEMORY + || s->output_type == TCC_OUTPUT_PREPROCESS) && (s->dflag & 16)) + s->dflag |= t ? 32 : 0, s->run_test = ++t, n = s->nb_files; + + /* compile or add each files or library */ + for (first_file = NULL, ret = 0;;) { + struct filespec *f = s->files[s->nb_files - n]; + s->filetype = f->type; + s->alacarte_link = f->alacarte; + if (f->type == AFF_TYPE_LIB) { + if (tcc_add_library_err(s, f->name) < 0) + ret = 1; + } else { + if (1 == s->verbose) + printf("-> %s\n", f->name); + if (!first_file) + first_file = f->name; + if (tcc_add_file(s, f->name) < 0) + ret = 1; + } + s->filetype = 0; + s->alacarte_link = 1; + if (--n == 0 || ret + || (s->output_type == TCC_OUTPUT_OBJ && !s->option_r)) + break; + } + + if (s->run_test) { + t = 0; + } else if (s->output_type == TCC_OUTPUT_PREPROCESS) { + ; + } else if (0 == ret) { + if (s->output_type == TCC_OUTPUT_MEMORY) { +#ifdef TCC_IS_NATIVE + ret = tcc_run(s, argc, argv); +#endif + } else { + if (!s->outfile) + s->outfile = default_outputfile(s, first_file); + if (tcc_output_file(s, s->outfile)) + ret = 1; + else if (s->gen_deps) + gen_makedeps(s, s->outfile, s->deps_outfile); + } + } + + if (s->do_bench && (n | t | ret) == 0) + tcc_print_stats(s, getclock_ms() - start_time); + tcc_delete(s); + if (ret == 0 && n) + goto redo; /* compile more files with -c */ + if (t) + goto redo; /* run more tests with -dt -run */ + if (ppfp && ppfp != stdout) + fclose(ppfp); + return ret; +} diff --git a/05/tcc-final-old/tcc.h b/05/tcc-final-old/tcc.h new file mode 100644 index 0000000..cd67973 --- /dev/null +++ b/05/tcc-final-old/tcc.h @@ -0,0 +1,1660 @@ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _TCC_H +#define _TCC_H + +#define _GNU_SOURCE +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef _WIN32 +# include +# include +# ifndef CONFIG_TCC_STATIC +# include +# endif +/* XXX: need to define this to use them in non ISOC99 context */ +extern float strtof (const char *__nptr, char **__endptr); +extern long double strtold (const char *__nptr, char **__endptr); +#endif + +#ifdef _WIN32 +# include +# include /* open, close etc. */ +# include /* getcwd */ +# ifdef __GNUC__ +# include +# endif +# define inline __inline +# define snprintf _snprintf +# define vsnprintf _vsnprintf +# ifndef __GNUC__ +# define strtold (long double)strtod +# define strtof (float)strtod +# define strtoll _strtoi64 +# define strtoull _strtoui64 +# endif +# ifdef LIBTCC_AS_DLL +# define LIBTCCAPI __declspec(dllexport) +# define PUB_FUNC LIBTCCAPI +# endif +# define inp next_inp /* inp is an intrinsic on msvc/mingw */ +# ifdef _MSC_VER +# pragma warning (disable : 4244) // conversion from 'uint64_t' to 'int', possible loss of data +# pragma warning (disable : 4267) // conversion from 'size_t' to 'int', possible loss of data +# pragma warning (disable : 4996) // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name +# pragma warning (disable : 4018) // signed/unsigned mismatch +# pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned +# define ssize_t intptr_t +# endif +# undef CONFIG_TCC_STATIC +#endif + +#ifndef O_BINARY +# define O_BINARY 0 +#endif + +#ifndef offsetof +#define offsetof(type, field) ((size_t) &((type *)0)->field) +#endif + +#ifndef countof +#define countof(tab) (sizeof(tab) / sizeof((tab)[0])) +#endif + +#ifdef _MSC_VER +# define NORETURN __declspec(noreturn) +# define ALIGNED(x) __declspec(align(x)) +#else +# define NORETURN __attribute__((noreturn)) +# define ALIGNED(x) __attribute__((aligned(x))) +#endif + +#ifdef _WIN32 +# define IS_DIRSEP(c) (c == '/' || c == '\\') +# define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2]))) +# define PATHCMP stricmp +# define PATHSEP ";" +#else +# define IS_DIRSEP(c) (c == '/') +# define IS_ABSPATH(p) IS_DIRSEP(p[0]) +# define PATHCMP strcmp +# define PATHSEP ":" +#endif + +/* -------------------------------------------- */ + +/* parser debug */ +/* #define PARSE_DEBUG */ +/* preprocessor debug */ +/* #define PP_DEBUG */ +/* include file debug */ +/* #define INC_DEBUG */ +/* memory leak debug */ +/* #define MEM_DEBUG */ +/* assembler debug */ +/* #define ASM_DEBUG */ + +/* target selection */ +/* #define TCC_TARGET_I386 *//* i386 code generator */ +/* #define TCC_TARGET_X86_64 *//* x86-64 code generator */ +/* #define TCC_TARGET_ARM *//* ARMv4 code generator */ +/* #define TCC_TARGET_ARM64 *//* ARMv8 code generator */ +/* #define TCC_TARGET_C67 *//* TMS320C67xx code generator */ + +/* default target is I386 */ +#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \ + !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \ + !defined(TCC_TARGET_X86_64) +# if defined __x86_64__ || defined _AMD64_ +# define TCC_TARGET_X86_64 +# elif defined __arm__ +# define TCC_TARGET_ARM +# define TCC_ARM_EABI +# define TCC_ARM_HARDFLOAT +# elif defined __aarch64__ +# define TCC_TARGET_ARM64 +# else +# define TCC_TARGET_I386 +# endif +# ifdef _WIN32 +# define TCC_TARGET_PE 1 +# endif +#endif + +/* only native compiler supports -run */ +#if defined _WIN32 == defined TCC_TARGET_PE +# if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386 +# define TCC_IS_NATIVE +# elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64 +# define TCC_IS_NATIVE +# elif defined __arm__ && defined TCC_TARGET_ARM +# define TCC_IS_NATIVE +# elif defined __aarch64__ && defined TCC_TARGET_ARM64 +# define TCC_IS_NATIVE +# endif +#endif + +#if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT +# define CONFIG_TCC_BACKTRACE +# if (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) \ + && !defined TCC_UCLIBC && !defined TCC_MUSL +# define CONFIG_TCC_BCHECK /* enable bound checking code */ +# endif +#endif + +/* ------------ path configuration ------------ */ + +#ifndef CONFIG_SYSROOT +# define CONFIG_SYSROOT "" +#endif +#ifndef CONFIG_TCCDIR +# define CONFIG_TCCDIR "/usr/local/lib/tcc" +#endif +#ifndef CONFIG_LDDIR +# define CONFIG_LDDIR "lib" +#endif +#ifdef CONFIG_TRIPLET +# define USE_TRIPLET(s) s "/" CONFIG_TRIPLET +# define ALSO_TRIPLET(s) USE_TRIPLET(s) ":" s +#else +# define USE_TRIPLET(s) s +# define ALSO_TRIPLET(s) s +#endif + +/* path to find crt1.o, crti.o and crtn.o */ +#ifndef CONFIG_TCC_CRTPREFIX +# define CONFIG_TCC_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) +#endif + +/* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */ + +/* system include paths */ +#ifndef CONFIG_TCC_SYSINCLUDEPATHS +# ifdef TCC_TARGET_PE +# define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include"PATHSEP"{B}/include/winapi" +# else +# define CONFIG_TCC_SYSINCLUDEPATHS \ + "{B}/include" \ + ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/include") \ + ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/include") +# endif +#endif + +/* library search paths */ +#ifndef CONFIG_TCC_LIBPATHS +# ifdef TCC_TARGET_PE +# define CONFIG_TCC_LIBPATHS "{B}/lib" +# else +# define CONFIG_TCC_LIBPATHS \ + ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \ + ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \ + ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR) +# endif +#endif + +/* name of ELF interpreter */ +#ifndef CONFIG_TCC_ELFINTERP +# if defined __FreeBSD__ +# define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1" +# elif defined __FreeBSD_kernel__ +# if defined(TCC_TARGET_X86_64) +# define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib/ld.so.1" +# endif +# elif defined __DragonFly__ +# define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2" +# elif defined __NetBSD__ +# define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so" +# elif defined __GNU__ +# define CONFIG_TCC_ELFINTERP "/lib/ld.so" +# elif defined(TCC_TARGET_PE) +# define CONFIG_TCC_ELFINTERP "-" +# elif defined(TCC_UCLIBC) +# define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */ +# elif defined TCC_TARGET_ARM64 +# if defined(TCC_MUSL) +# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-aarch64.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1" +# endif +# elif defined(TCC_TARGET_X86_64) +# if defined(TCC_MUSL) +# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-x86_64.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2" +# endif +# elif !defined(TCC_ARM_EABI) +# if defined(TCC_MUSL) +# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-arm.so.1" +# else +# define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2" +# endif +# endif +#endif + +/* var elf_interp dans *-gen.c */ +#ifdef CONFIG_TCC_ELFINTERP +# define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP +#else +# define DEFAULT_ELFINTERP(s) default_elfinterp(s) +#endif + +/* (target specific) libtcc1.a */ +#ifndef TCC_LIBTCC1 +# define TCC_LIBTCC1 "libtcc1.a" +#endif + +/* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */ +#if defined CONFIG_USE_LIBGCC && !defined TCC_LIBGCC +#define TCC_LIBGCC USE_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1" +#endif + +/* -------------------------------------------- */ + +#include "libtcc.h" +#include "elf.h" +#include "stab.h" + +/* -------------------------------------------- */ + +#ifndef PUB_FUNC /* functions used by tcc.c but not in libtcc.h */ +# define PUB_FUNC +#endif + +#ifndef ONE_SOURCE +# define ONE_SOURCE 1 +#endif + +#if ONE_SOURCE +#define ST_INLN static inline +#define ST_FUNC static +#define ST_DATA static +#else +#define ST_INLN +#define ST_FUNC +#define ST_DATA extern +#endif + +#ifdef TCC_PROFILE /* profile all functions */ +# define static +#endif + +/* -------------------------------------------- */ +/* include the target specific definitions */ + +#define TARGET_DEFS_ONLY +#ifdef TCC_TARGET_I386 +# include "i386-gen.c" +# include "i386-link.c" +#endif +#ifdef TCC_TARGET_X86_64 +# include "x86_64-gen.c" +# include "x86_64-link.c" +#endif +#ifdef TCC_TARGET_ARM +# include "arm-gen.c" +# include "arm-link.c" +# include "arm-asm.c" +#endif +#ifdef TCC_TARGET_ARM64 +# include "arm64-gen.c" +# include "arm64-link.c" +#endif +#ifdef TCC_TARGET_C67 +# define TCC_TARGET_COFF +# include "coff.h" +# include "c67-gen.c" +# include "c67-link.c" +#endif +#undef TARGET_DEFS_ONLY + +/* -------------------------------------------- */ + +#if PTR_SIZE == 8 +# define ELFCLASSW ELFCLASS64 +# define ElfW(type) Elf##64##_##type +# define ELFW(type) ELF##64##_##type +# define ElfW_Rel ElfW(Rela) +# define SHT_RELX SHT_RELA +# define REL_SECTION_FMT ".rela%s" +#else +# define ELFCLASSW ELFCLASS32 +# define ElfW(type) Elf##32##_##type +# define ELFW(type) ELF##32##_##type +# define ElfW_Rel ElfW(Rel) +# define SHT_RELX SHT_REL +# define REL_SECTION_FMT ".rel%s" +#endif +/* target address type */ +#define addr_t ElfW(Addr) +#define ElfSym ElfW(Sym) + +#if PTR_SIZE == 8 && !defined TCC_TARGET_PE +# define LONG_SIZE 8 +#else +# define LONG_SIZE 4 +#endif + +/* -------------------------------------------- */ + +#define INCLUDE_STACK_SIZE 32 +#define IFDEF_STACK_SIZE 64 +#define VSTACK_SIZE 256 +#define STRING_MAX_SIZE 1024 +#define TOKSTR_MAX_SIZE 256 +#define PACK_STACK_SIZE 8 + +#define TOK_HASH_SIZE 16384 /* must be a power of two */ +#define TOK_ALLOC_INCR 512 /* must be a power of two */ +#define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */ + +/* token symbol management */ +typedef struct TokenSym { + struct TokenSym *hash_next; + struct Sym *sym_define; /* direct pointer to define */ + struct Sym *sym_label; /* direct pointer to label */ + struct Sym *sym_struct; /* direct pointer to structure */ + struct Sym *sym_identifier; /* direct pointer to identifier */ + int tok; /* token number */ + int len; + char str[1]; +} TokenSym; + +#ifdef TCC_TARGET_PE +typedef unsigned short nwchar_t; +#else +typedef int nwchar_t; +#endif + +typedef struct CString { + int size; /* size in bytes */ + void *data; /* either 'char *' or 'nwchar_t *' */ + int size_allocated; +} CString; + +/* type definition */ +typedef struct CType { + int t; + struct Sym *ref; +} CType; + +/* constant value */ +typedef union CValue { + long double ld; + double d; + float f; + uint64_t i; + struct { + int size; + const void *data; + } str; + int tab[LDOUBLE_SIZE/4]; +} CValue; + +/* value on stack */ +typedef struct SValue { + CType type; /* type */ + unsigned short r; /* register + flags */ + unsigned short r2; /* second register, used for 'long long' + type. If not used, set to VT_CONST */ + CValue c; /* constant, if VT_CONST */ + struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST), or if + result of unary() for an identifier. */ +} SValue; + +/* symbol attributes */ +struct SymAttr { + unsigned short + aligned : 5, /* alignment as log2+1 (0 == unspecified) */ + packed : 1, + weak : 1, + visibility : 2, + dllexport : 1, + dllimport : 1, + unused : 5; +}; + +/* function attributes or temporary attributes for parsing */ +struct FuncAttr { + unsigned + func_call : 3, /* calling convention (0..5), see below */ + func_type : 2, /* FUNC_OLD/NEW/ELLIPSIS */ + func_args : 8; /* PE __stdcall args */ +}; + +/* GNUC attribute definition */ +typedef struct AttributeDef { + struct SymAttr a; + struct FuncAttr f; + struct Section *section; + int alias_target; /* token */ + int asm_label; /* associated asm label */ + char attr_mode; /* __attribute__((__mode__(...))) */ +} AttributeDef; + +/* symbol management */ +typedef struct Sym { + int v; /* symbol token */ + unsigned short r; /* associated register or VT_CONST/VT_LOCAL and LVAL type */ + struct SymAttr a; /* symbol attributes */ + union { + struct { + int c; /* associated number or Elf symbol index */ + union { + int sym_scope; /* scope level for locals */ + int jnext; /* next jump label */ + struct FuncAttr f; /* function attributes */ + int auxtype; /* bitfield access type */ + }; + }; + long long enum_val; /* enum constant if IS_ENUM_VAL */ + int *d; /* define token stream */ + }; + CType type; /* associated type */ + union { + struct Sym *next; /* next related symbol (for fields and anoms) */ + int asm_label; /* associated asm label */ + }; + struct Sym *prev; /* prev symbol in stack */ + struct Sym *prev_tok; /* previous symbol for this token */ +} Sym; + +/* section definition */ +typedef struct Section { + unsigned long data_offset; /* current data offset */ + unsigned char *data; /* section data */ + unsigned long data_allocated; /* used for realloc() handling */ + int sh_name; /* elf section name (only used during output) */ + int sh_num; /* elf section number */ + int sh_type; /* elf section type */ + int sh_flags; /* elf section flags */ + int sh_info; /* elf section info */ + int sh_addralign; /* elf section alignment */ + int sh_entsize; /* elf entry size */ + unsigned long sh_size; /* section size (only used during output) */ + addr_t sh_addr; /* address at which the section is relocated */ + unsigned long sh_offset; /* file offset */ + int nb_hashed_syms; /* used to resize the hash table */ + struct Section *link; /* link to another section */ + struct Section *reloc; /* corresponding section for relocation, if any */ + struct Section *hash; /* hash table for symbols */ + struct Section *prev; /* previous section on section stack */ + char name[1]; /* section name */ +} Section; + +typedef struct DLLReference { + int level; + void *handle; + char name[1]; +} DLLReference; + +/* -------------------------------------------------- */ + +#define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ +#define SYM_FIELD 0x20000000 /* struct/union field symbol space */ +#define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */ + +/* stored in 'Sym->f.func_type' field */ +#define FUNC_NEW 1 /* ansi function prototype */ +#define FUNC_OLD 2 /* old function prototype */ +#define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */ + +/* stored in 'Sym->f.func_call' field */ +#define FUNC_CDECL 0 /* standard c call */ +#define FUNC_STDCALL 1 /* pascal c call */ +#define FUNC_FASTCALL1 2 /* first param in %eax */ +#define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */ +#define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */ +#define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */ + +/* field 'Sym.t' for macros */ +#define MACRO_OBJ 0 /* object like macro */ +#define MACRO_FUNC 1 /* function like macro */ + +/* field 'Sym.r' for C labels */ +#define LABEL_DEFINED 0 /* label is defined */ +#define LABEL_FORWARD 1 /* label is forward defined */ +#define LABEL_DECLARED 2 /* label is declared but never used */ + +/* type_decl() types */ +#define TYPE_ABSTRACT 1 /* type without variable */ +#define TYPE_DIRECT 2 /* type with variable */ + +#define IO_BUF_SIZE 8192 + +typedef struct BufferedFile { + uint8_t *buf_ptr; + uint8_t *buf_end; + int fd; + struct BufferedFile *prev; + int line_num; /* current line number - here to simplify code */ + int line_ref; /* tcc -E: last printed line */ + int ifndef_macro; /* #ifndef macro / #endif search */ + int ifndef_macro_saved; /* saved ifndef_macro */ + int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */ + int include_next_index; /* next search path */ + char filename[1024]; /* filename */ + char *true_filename; /* filename not modified by # line directive */ + unsigned char unget[4]; + unsigned char buffer[1]; /* extra size for CH_EOB char */ +} BufferedFile; + +#define CH_EOB '\\' /* end of buffer or '\0' char in file */ +#define CH_EOF (-1) /* end of file */ + +/* used to record tokens */ +typedef struct TokenString { + int *str; + int len; + int lastlen; + int allocated_len; + int last_line_num; + int save_line_num; + /* used to chain token-strings with begin/end_macro() */ + struct TokenString *prev; + const int *prev_ptr; + char alloc; +} TokenString; + +/* inline functions */ +typedef struct InlineFunc { + TokenString *func_str; + Sym *sym; + char filename[1]; +} InlineFunc; + +/* include file cache, used to find files faster and also to eliminate + inclusion if the include file is protected by #ifndef ... #endif */ +typedef struct CachedInclude { + int ifndef_macro; + int once; + int hash_next; /* -1 if none */ + char filename[1]; /* path specified in #include */ +} CachedInclude; + +#define CACHED_INCLUDES_HASH_SIZE 32 + +#ifdef CONFIG_TCC_ASM +typedef struct ExprValue { + uint64_t v; + Sym *sym; + int pcrel; +} ExprValue; + +#define MAX_ASM_OPERANDS 30 +typedef struct ASMOperand { + int id; /* GCC 3 optional identifier (0 if number only supported */ + char *constraint; + char asm_str[16]; /* computed asm string for operand */ + SValue *vt; /* C value of the expression */ + int ref_index; /* if >= 0, gives reference to a output constraint */ + int input_index; /* if >= 0, gives reference to an input constraint */ + int priority; /* priority, used to assign registers */ + int reg; /* if >= 0, register number used for this operand */ + int is_llong; /* true if double register value */ + int is_memory; /* true if memory operand */ + int is_rw; /* for '+' modifier */ +} ASMOperand; +#endif + +/* extra symbol attributes (not in symbol table) */ +struct sym_attr { + unsigned got_offset; + unsigned plt_offset; + int plt_sym; + int dyn_index; +#ifdef TCC_TARGET_ARM + unsigned char plt_thumb_stub:1; +#endif +}; + +struct TCCState { + + int verbose; /* if true, display some information during compilation */ + int nostdinc; /* if true, no standard headers are added */ + int nostdlib; /* if true, no standard libraries are added */ + int nocommon; /* if true, do not use common symbols for .bss data */ + int static_link; /* if true, static linking is performed */ + int rdynamic; /* if true, all symbols are exported */ + int symbolic; /* if true, resolve symbols in the current module first */ + int alacarte_link; /* if true, only link in referenced objects from archive */ + + char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */ + char *soname; /* as specified on the command line (-soname) */ + char *rpath; /* as specified on the command line (-Wl,-rpath=) */ + int enable_new_dtags; /* ditto, (-Wl,--enable-new-dtags) */ + + /* output type, see TCC_OUTPUT_XXX */ + int output_type; + /* output format, see TCC_OUTPUT_FORMAT_xxx */ + int output_format; + + /* C language options */ + int char_is_unsigned; + int leading_underscore; + int ms_extensions; /* allow nested named struct w/o identifier behave like unnamed */ + int dollars_in_identifiers; /* allows '$' char in identifiers */ + int ms_bitfields; /* if true, emulate MS algorithm for aligning bitfields */ + + /* warning switches */ + int warn_write_strings; + int warn_unsupported; + int warn_error; + int warn_none; + int warn_implicit_function_declaration; + int warn_gcc_compat; + + /* compile with debug symbol (and use them if error during execution) */ + int do_debug; +#ifdef CONFIG_TCC_BCHECK + /* compile with built-in memory and bounds checker */ + int do_bounds_check; +#endif +#ifdef TCC_TARGET_ARM + enum float_abi float_abi; /* float ABI of the generated code*/ +#endif + int run_test; /* nth test to run with -dt -run */ + + addr_t text_addr; /* address of text section */ + int has_text_addr; + + unsigned section_align; /* section alignment */ + + char *init_symbol; /* symbols to call at load-time (not used currently) */ + char *fini_symbol; /* symbols to call at unload-time (not used currently) */ + +#ifdef TCC_TARGET_I386 + int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */ +#endif +#ifdef TCC_TARGET_X86_64 + int nosse; /* For -mno-sse support. */ +#endif + + /* array of all loaded dlls (including those referenced by loaded dlls) */ + DLLReference **loaded_dlls; + int nb_loaded_dlls; + + /* include paths */ + char **include_paths; + int nb_include_paths; + + char **sysinclude_paths; + int nb_sysinclude_paths; + + /* library paths */ + char **library_paths; + int nb_library_paths; + + /* crt?.o object path */ + char **crt_paths; + int nb_crt_paths; + + /* -include files */ + char **cmd_include_files; + int nb_cmd_include_files; + + /* error handling */ + void *error_opaque; + void (*error_func)(void *opaque, const char *msg); + int error_set_jmp_enabled; + jmp_buf error_jmp_buf; + int nb_errors; + + /* output file for preprocessing (-E) */ + FILE *ppfp; + enum { + LINE_MACRO_OUTPUT_FORMAT_GCC, + LINE_MACRO_OUTPUT_FORMAT_NONE, + LINE_MACRO_OUTPUT_FORMAT_STD, + LINE_MACRO_OUTPUT_FORMAT_P10 = 11 + } Pflag; /* -P switch */ + char dflag; /* -dX value */ + + /* for -MD/-MF: collected dependencies for this compilation */ + char **target_deps; + int nb_target_deps; + + /* compilation */ + BufferedFile *include_stack[INCLUDE_STACK_SIZE]; + BufferedFile **include_stack_ptr; + + int ifdef_stack[IFDEF_STACK_SIZE]; + int *ifdef_stack_ptr; + + /* included files enclosed with #ifndef MACRO */ + int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE]; + CachedInclude **cached_includes; + int nb_cached_includes; + + /* #pragma pack stack */ + int pack_stack[PACK_STACK_SIZE]; + int *pack_stack_ptr; + char **pragma_libs; + int nb_pragma_libs; + + /* inline functions are stored as token lists and compiled last + only if referenced */ + struct InlineFunc **inline_fns; + int nb_inline_fns; + + /* sections */ + Section **sections; + int nb_sections; /* number of sections, including first dummy section */ + + Section **priv_sections; + int nb_priv_sections; /* number of private sections */ + + /* got & plt handling */ + Section *got; + Section *plt; + + /* temporary dynamic symbol sections (for dll loading) */ + Section *dynsymtab_section; + /* exported dynamic symbol section */ + Section *dynsym; + /* copy of the global symtab_section variable */ + Section *symtab; + /* extra attributes (eg. GOT/PLT value) for symtab symbols */ + struct sym_attr *sym_attrs; + int nb_sym_attrs; + +#ifdef TCC_TARGET_PE + /* PE info */ + int pe_subsystem; + unsigned pe_characteristics; + unsigned pe_file_align; + unsigned pe_stack_size; + addr_t pe_imagebase; +# ifdef TCC_TARGET_X86_64 + Section *uw_pdata; + int uw_sym; + unsigned uw_offs; +# endif +#endif + +#ifdef TCC_IS_NATIVE + const char *runtime_main; + void **runtime_mem; + int nb_runtime_mem; +#endif + + /* used by main and tcc_parse_args only */ + struct filespec **files; /* files seen on command line */ + int nb_files; /* number thereof */ + int nb_libraries; /* number of libs thereof */ + int filetype; + char *outfile; /* output filename */ + int option_r; /* option -r */ + int do_bench; /* option -bench */ + int gen_deps; /* option -MD */ + char *deps_outfile; /* option -MF */ + int option_pthread; /* -pthread option */ + int argc; + char **argv; +}; + +struct filespec { + char type; + char alacarte; + char name[1]; +}; + +/* The current value can be: */ +#define VT_VALMASK 0x003f /* mask for value location, register or: */ +#define VT_CONST 0x0030 /* constant in vc (must be first non register value) */ +#define VT_LLOCAL 0x0031 /* lvalue, offset on stack */ +#define VT_LOCAL 0x0032 /* offset on stack */ +#define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */ +#define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */ +#define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */ +#define VT_LVAL 0x0100 /* var is an lvalue */ +#define VT_SYM 0x0200 /* a symbol value is added */ +#define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for + char/short stored in integer registers) */ +#define VT_MUSTBOUND 0x0800 /* bound checking must be done before + dereferencing value */ +#define VT_BOUNDED 0x8000 /* value is bounded. The address of the + bounding function call point is in vc */ +#define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */ +#define VT_LVAL_SHORT 0x2000 /* lvalue is a short */ +#define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */ +#define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED) + +/* types */ +#define VT_BTYPE 0x000f /* mask for basic type */ +#define VT_VOID 0 /* void type */ +#define VT_BYTE 1 /* signed byte type */ +#define VT_SHORT 2 /* short type */ +#define VT_INT 3 /* integer type */ +#define VT_LLONG 4 /* 64 bit integer */ +#define VT_PTR 5 /* pointer */ +#define VT_FUNC 6 /* function type */ +#define VT_STRUCT 7 /* struct/union definition */ +#define VT_FLOAT 8 /* IEEE float */ +#define VT_DOUBLE 9 /* IEEE double */ +#define VT_LDOUBLE 10 /* IEEE long double */ +#define VT_BOOL 11 /* ISOC99 boolean type */ +#define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */ +#define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */ + +#define VT_UNSIGNED 0x0010 /* unsigned type */ +#define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */ +#define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */ +#define VT_BITFIELD 0x0080 /* bitfield modifier */ +#define VT_CONSTANT 0x0100 /* const modifier */ +#define VT_VOLATILE 0x0200 /* volatile modifier */ +#define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */ +#define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */ + +/* storage */ +#define VT_EXTERN 0x00001000 /* extern definition */ +#define VT_STATIC 0x00002000 /* static variable */ +#define VT_TYPEDEF 0x00004000 /* typedef definition */ +#define VT_INLINE 0x00008000 /* inline definition */ +/* currently unused: 0x000[1248]0000 */ + +#define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */ +#define VT_STRUCT_MASK (((1 << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD) +#define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f) +#define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f) + +#define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT) +#define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */ +#define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */ + +#define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM) +#define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL) +#define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION) + +/* type mask (except storage) */ +#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE) +#define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK)) + +/* symbol was created by tccasm.c first */ +#define VT_ASM (VT_VOID | VT_UNSIGNED) +#define IS_ASM_SYM(sym) (((sym)->type.t & (VT_BTYPE | VT_ASM)) == VT_ASM) + +/* token values */ + +/* warning: the following compare tokens depend on i386 asm code */ +#define TOK_ULT 0x92 +#define TOK_UGE 0x93 +#define TOK_EQ 0x94 +#define TOK_NE 0x95 +#define TOK_ULE 0x96 +#define TOK_UGT 0x97 +#define TOK_Nset 0x98 +#define TOK_Nclear 0x99 +#define TOK_LT 0x9c +#define TOK_GE 0x9d +#define TOK_LE 0x9e +#define TOK_GT 0x9f + +#define TOK_LAND 0xa0 +#define TOK_LOR 0xa1 +#define TOK_DEC 0xa2 +#define TOK_MID 0xa3 /* inc/dec, to void constant */ +#define TOK_INC 0xa4 +#define TOK_UDIV 0xb0 /* unsigned division */ +#define TOK_UMOD 0xb1 /* unsigned modulo */ +#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */ + +/* tokens that carry values (in additional token string space / tokc) --> */ +#define TOK_CCHAR 0xb3 /* char constant in tokc */ +#define TOK_LCHAR 0xb4 +#define TOK_CINT 0xb5 /* number in tokc */ +#define TOK_CUINT 0xb6 /* unsigned int constant */ +#define TOK_CLLONG 0xb7 /* long long constant */ +#define TOK_CULLONG 0xb8 /* unsigned long long constant */ +#define TOK_STR 0xb9 /* pointer to string in tokc */ +#define TOK_LSTR 0xba +#define TOK_CFLOAT 0xbb /* float constant */ +#define TOK_CDOUBLE 0xbc /* double constant */ +#define TOK_CLDOUBLE 0xbd /* long double constant */ +#define TOK_PPNUM 0xbe /* preprocessor number */ +#define TOK_PPSTR 0xbf /* preprocessor string */ +#define TOK_LINENUM 0xc0 /* line number info */ +#define TOK_TWODOTS 0xa8 /* C++ token ? */ +/* <-- */ + +#define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */ +#define TOK_ADDC1 0xc3 /* add with carry generation */ +#define TOK_ADDC2 0xc4 /* add with carry use */ +#define TOK_SUBC1 0xc5 /* add with carry generation */ +#define TOK_SUBC2 0xc6 /* add with carry use */ +#define TOK_ARROW 0xc7 +#define TOK_DOTS 0xc8 /* three dots */ +#define TOK_SHR 0xc9 /* unsigned shift right */ +#define TOK_TWOSHARPS 0xca /* ## preprocessing token */ +#define TOK_PLCHLDR 0xcb /* placeholder token as defined in C99 */ +#define TOK_NOSUBST 0xcc /* means following token has already been pp'd */ +#define TOK_PPJOIN 0xcd /* A '##' in the right position to mean pasting */ +#define TOK_CLONG 0xce /* long constant */ +#define TOK_CULONG 0xcf /* unsigned long constant */ + +#define TOK_SHL 0x01 /* shift left */ +#define TOK_SAR 0x02 /* signed shift right */ + +/* assignment operators : normal operator or 0x80 */ +#define TOK_A_MOD 0xa5 +#define TOK_A_AND 0xa6 +#define TOK_A_MUL 0xaa +#define TOK_A_ADD 0xab +#define TOK_A_SUB 0xad +#define TOK_A_DIV 0xaf +#define TOK_A_XOR 0xde +#define TOK_A_OR 0xfc +#define TOK_A_SHL 0x81 +#define TOK_A_SAR 0x82 + +#define TOK_EOF (-1) /* end of file */ +#define TOK_LINEFEED 10 /* line feed */ + +/* all identifiers and strings have token above that */ +#define TOK_IDENT 256 + +#define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x) +#define TOK_ASM_int TOK_INT +#define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x) +#define TOK_ASMDIR_FIRST TOK_ASMDIR_byte +#define TOK_ASMDIR_LAST TOK_ASMDIR_section + +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 +/* only used for i386 asm opcodes definitions */ +#define DEF_BWL(x) \ + DEF(TOK_ASM_ ## x ## b, #x "b") \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x, #x) +#define DEF_WL(x) \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x, #x) +#ifdef TCC_TARGET_X86_64 +# define DEF_BWLQ(x) \ + DEF(TOK_ASM_ ## x ## b, #x "b") \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x ## q, #x "q") \ + DEF(TOK_ASM_ ## x, #x) +# define DEF_WLQ(x) \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x ## q, #x "q") \ + DEF(TOK_ASM_ ## x, #x) +# define DEF_BWLX DEF_BWLQ +# define DEF_WLX DEF_WLQ +/* number of sizes + 1 */ +# define NBWLX 5 +#else +# define DEF_BWLX DEF_BWL +# define DEF_WLX DEF_WL +/* number of sizes + 1 */ +# define NBWLX 4 +#endif + +#define DEF_FP1(x) \ + DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \ + DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \ + DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \ + DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s") + +#define DEF_FP(x) \ + DEF(TOK_ASM_ ## f ## x, "f" #x ) \ + DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \ + DEF_FP1(x) + +#define DEF_ASMTEST(x,suffix) \ + DEF_ASM(x ## o ## suffix) \ + DEF_ASM(x ## no ## suffix) \ + DEF_ASM(x ## b ## suffix) \ + DEF_ASM(x ## c ## suffix) \ + DEF_ASM(x ## nae ## suffix) \ + DEF_ASM(x ## nb ## suffix) \ + DEF_ASM(x ## nc ## suffix) \ + DEF_ASM(x ## ae ## suffix) \ + DEF_ASM(x ## e ## suffix) \ + DEF_ASM(x ## z ## suffix) \ + DEF_ASM(x ## ne ## suffix) \ + DEF_ASM(x ## nz ## suffix) \ + DEF_ASM(x ## be ## suffix) \ + DEF_ASM(x ## na ## suffix) \ + DEF_ASM(x ## nbe ## suffix) \ + DEF_ASM(x ## a ## suffix) \ + DEF_ASM(x ## s ## suffix) \ + DEF_ASM(x ## ns ## suffix) \ + DEF_ASM(x ## p ## suffix) \ + DEF_ASM(x ## pe ## suffix) \ + DEF_ASM(x ## np ## suffix) \ + DEF_ASM(x ## po ## suffix) \ + DEF_ASM(x ## l ## suffix) \ + DEF_ASM(x ## nge ## suffix) \ + DEF_ASM(x ## nl ## suffix) \ + DEF_ASM(x ## ge ## suffix) \ + DEF_ASM(x ## le ## suffix) \ + DEF_ASM(x ## ng ## suffix) \ + DEF_ASM(x ## nle ## suffix) \ + DEF_ASM(x ## g ## suffix) + +#endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */ + +enum tcc_token { + TOK_LAST = TOK_IDENT - 1 +#define DEF(id, str) ,id +#include "tcctok.h" +#undef DEF +}; + +/* keywords: tok >= TOK_IDENT && tok < TOK_UIDENT */ +#define TOK_UIDENT TOK_DEFINE + +/* ------------ libtcc.c ------------ */ + +/* use GNU C extensions */ +ST_DATA int gnu_ext; +/* use Tiny C extensions */ +ST_DATA int tcc_ext; +/* XXX: get rid of this ASAP */ +ST_DATA struct TCCState *tcc_state; + +/* public functions currently used by the tcc main function */ +ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s); +ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s); +ST_FUNC char *pstrncpy(char *out, const char *in, size_t num); +PUB_FUNC char *tcc_basename(const char *name); +PUB_FUNC char *tcc_fileextension (const char *name); + +#ifndef MEM_DEBUG +PUB_FUNC void tcc_free(void *ptr); +PUB_FUNC void *tcc_malloc(unsigned long size); +PUB_FUNC void *tcc_mallocz(unsigned long size); +PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size); +PUB_FUNC char *tcc_strdup(const char *str); +#else +#define tcc_free(ptr) tcc_free_debug(ptr) +#define tcc_malloc(size) tcc_malloc_debug(size, __FILE__, __LINE__) +#define tcc_mallocz(size) tcc_mallocz_debug(size, __FILE__, __LINE__) +#define tcc_realloc(ptr,size) tcc_realloc_debug(ptr, size, __FILE__, __LINE__) +#define tcc_strdup(str) tcc_strdup_debug(str, __FILE__, __LINE__) +PUB_FUNC void tcc_free_debug(void *ptr); +PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line); +PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line); +PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line); +PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line); +#endif + +#define free(p) use_tcc_free(p) +#define malloc(s) use_tcc_malloc(s) +#define realloc(p, s) use_tcc_realloc(p, s) +#undef strdup +#define strdup(s) use_tcc_strdup(s) +PUB_FUNC void tcc_memcheck(void); +PUB_FUNC void tcc_error_noabort(const char *fmt, ...); +PUB_FUNC NORETURN void tcc_error(const char *fmt, ...); +PUB_FUNC void tcc_warning(const char *fmt, ...); + +/* other utilities */ +ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data); +ST_FUNC void dynarray_reset(void *pp, int *n); +ST_INLN void cstr_ccat(CString *cstr, int ch); +ST_FUNC void cstr_cat(CString *cstr, const char *str, int len); +ST_FUNC void cstr_wccat(CString *cstr, int ch); +ST_FUNC void cstr_new(CString *cstr); +ST_FUNC void cstr_free(CString *cstr); +ST_FUNC void cstr_reset(CString *cstr); + +ST_INLN void sym_free(Sym *sym); +ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c); +ST_FUNC Sym *sym_find2(Sym *s, int v); +ST_FUNC Sym *sym_push(int v, CType *type, int r, int c); +ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep); +ST_INLN Sym *struct_find(int v); +ST_INLN Sym *sym_find(int v); +ST_FUNC Sym *global_identifier_push(int v, int t, int c); + +ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen); +ST_FUNC int tcc_open(TCCState *s1, const char *filename); +ST_FUNC void tcc_close(void); + +ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags); +/* flags: */ +#define AFF_PRINT_ERROR 0x10 /* print error if file not found */ +#define AFF_REFERENCED_DLL 0x20 /* load a referenced dll from another dll */ +#define AFF_TYPE_BIN 0x40 /* file to add is binary */ +/* s->filetype: */ +#define AFF_TYPE_NONE 0 +#define AFF_TYPE_C 1 +#define AFF_TYPE_ASM 2 +#define AFF_TYPE_ASMPP 3 +#define AFF_TYPE_LIB 4 +/* values from tcc_object_type(...) */ +#define AFF_BINTYPE_REL 1 +#define AFF_BINTYPE_DYN 2 +#define AFF_BINTYPE_AR 3 +#define AFF_BINTYPE_C67 4 + + +ST_FUNC int tcc_add_crt(TCCState *s, const char *filename); +ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags); +ST_FUNC void tcc_add_pragma_libs(TCCState *s1); +PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f); +PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time); +PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind); +#ifdef _WIN32 +ST_FUNC char *normalize_slashes(char *path); +#endif + +/* tcc_parse_args return codes: */ +#define OPT_HELP 1 +#define OPT_HELP2 2 +#define OPT_V 3 +#define OPT_PRINT_DIRS 4 +#define OPT_AR 5 +#define OPT_IMPDEF 6 +#define OPT_M32 32 +#define OPT_M64 64 + +/* ------------ tccpp.c ------------ */ + +ST_DATA struct BufferedFile *file; +ST_DATA int ch, tok; +ST_DATA CValue tokc; +ST_DATA const int *macro_ptr; +ST_DATA int parse_flags; +ST_DATA int tok_flags; +ST_DATA CString tokcstr; /* current parsed string, if any */ + +/* display benchmark infos */ +ST_DATA int total_lines; +ST_DATA int total_bytes; +ST_DATA int tok_ident; +ST_DATA TokenSym **table_ident; + +#define TOK_FLAG_BOL 0x0001 /* beginning of line before */ +#define TOK_FLAG_BOF 0x0002 /* beginning of file before */ +#define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */ +#define TOK_FLAG_EOF 0x0008 /* end of file */ + +#define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */ +#define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */ +#define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a + token. line feed is also + returned at eof */ +#define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */ +#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */ +#define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */ +#define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */ + +/* isidnum_table flags: */ +#define IS_SPC 1 +#define IS_ID 2 +#define IS_NUM 4 + +ST_FUNC TokenSym *tok_alloc(const char *str, int len); +ST_FUNC const char *get_tok_str(int v, CValue *cv); +ST_FUNC void begin_macro(TokenString *str, int alloc); +ST_FUNC void end_macro(void); +ST_FUNC int set_idnum(int c, int val); +ST_INLN void tok_str_new(TokenString *s); +ST_FUNC TokenString *tok_str_alloc(void); +ST_FUNC void tok_str_free(TokenString *s); +ST_FUNC void tok_str_free_str(int *str); +ST_FUNC void tok_str_add(TokenString *s, int t); +ST_FUNC void tok_str_add_tok(TokenString *s); +ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg); +ST_FUNC void define_undef(Sym *s); +ST_INLN Sym *define_find(int v); +ST_FUNC void free_defines(Sym *b); +ST_FUNC Sym *label_find(int v); +ST_FUNC Sym *label_push(Sym **ptop, int v, int flags); +ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep); +ST_FUNC void parse_define(void); +ST_FUNC void preprocess(int is_bof); +ST_FUNC void next_nomacro(void); +ST_FUNC void next(void); +ST_INLN void unget_tok(int last_tok); +ST_FUNC void preprocess_start(TCCState *s1, int is_asm); +ST_FUNC void preprocess_end(TCCState *s1); +ST_FUNC void tccpp_new(TCCState *s); +ST_FUNC void tccpp_delete(TCCState *s); +ST_FUNC int tcc_preprocess(TCCState *s1); +ST_FUNC void skip(int c); +ST_FUNC NORETURN void expect(const char *msg); + +/* space excluding newline */ +static inline int is_space(int ch) { + return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r'; +} +static inline int isid(int c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; +} +static inline int isnum(int c) { + return c >= '0' && c <= '9'; +} +static inline int isoct(int c) { + return c >= '0' && c <= '7'; +} +static inline int toup(int c) { + return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; +} + +/* ------------ tccgen.c ------------ */ + +#define SYM_POOL_NB (8192 / sizeof(Sym)) +ST_DATA Sym *sym_free_first; +ST_DATA void **sym_pools; +ST_DATA int nb_sym_pools; + +ST_DATA Sym *global_stack; +ST_DATA Sym *local_stack; +ST_DATA Sym *local_label_stack; +ST_DATA Sym *global_label_stack; +ST_DATA Sym *define_stack; +ST_DATA CType char_pointer_type, func_old_type, int_type, size_type; +ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop, *pvtop; +#define vstack (__vstack + 1) +ST_DATA int rsym, anon_sym, ind, loc; + +ST_DATA int const_wanted; /* true if constant wanted */ +ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ +ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */ +ST_DATA CType func_vt; /* current function return type (used by return instruction) */ +ST_DATA int func_var; /* true if current function is variadic */ +ST_DATA int func_vc; +ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ +ST_DATA const char *funcname; +ST_DATA int g_debug; + +ST_FUNC void tcc_debug_start(TCCState *s1); +ST_FUNC void tcc_debug_end(TCCState *s1); +ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym); +ST_FUNC void tcc_debug_funcend(TCCState *s1, int size); +ST_FUNC void tcc_debug_line(TCCState *s1); + +ST_FUNC int tccgen_compile(TCCState *s1); +ST_FUNC void free_inline_functions(TCCState *s); +ST_FUNC void check_vstack(void); + +ST_INLN int is_float(int t); +ST_FUNC int ieee_finite(double d); +ST_FUNC void test_lvalue(void); +ST_FUNC void vpushi(int v); +ST_FUNC ElfSym *elfsym(Sym *); +ST_FUNC void update_storage(Sym *sym); +ST_FUNC Sym *external_global_sym(int v, CType *type, int r); +ST_FUNC void vset(CType *type, int r, int v); +ST_FUNC void vswap(void); +ST_FUNC void vpush_global_sym(CType *type, int v); +ST_FUNC void vrote(SValue *e, int n); +ST_FUNC void vrott(int n); +ST_FUNC void vrotb(int n); +#ifdef TCC_TARGET_ARM +ST_FUNC int get_reg_ex(int rc, int rc2); +ST_FUNC void lexpand_nr(void); +#endif +ST_FUNC void vpushv(SValue *v); +ST_FUNC void save_reg(int r); +ST_FUNC void save_reg_upstack(int r, int n); +ST_FUNC int get_reg(int rc); +ST_FUNC void save_regs(int n); +ST_FUNC void gaddrof(void); +ST_FUNC int gv(int rc); +ST_FUNC void gv2(int rc1, int rc2); +ST_FUNC void vpop(void); +ST_FUNC void gen_op(int op); +ST_FUNC int type_size(CType *type, int *a); +ST_FUNC void mk_pointer(CType *type); +ST_FUNC void vstore(void); +ST_FUNC void inc(int post, int c); +ST_FUNC void parse_mult_str (CString *astr, const char *msg); +ST_FUNC void parse_asm_str(CString *astr); +ST_FUNC int lvalue_type(int t); +ST_FUNC void indir(void); +ST_FUNC void unary(void); +ST_FUNC void expr_prod(void); +ST_FUNC void expr_sum(void); +ST_FUNC void gexpr(void); +ST_FUNC int expr_const(void); +#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67 +ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size); +#endif +#if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE +ST_FUNC int classify_x86_64_va_arg(CType *ty); +#endif + +/* ------------ tccelf.c ------------ */ + +#define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */ +#define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */ +#define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */ + +#define ARMAG "!\012" /* For COFF and a.out archives */ + +typedef struct { + unsigned int n_strx; /* index into string table of name */ + unsigned char n_type; /* type of symbol */ + unsigned char n_other; /* misc info (usually empty) */ + unsigned short n_desc; /* description field */ + unsigned int n_value; /* value of symbol */ +} Stab_Sym; + +ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */ +ST_DATA Section *common_section; +ST_DATA Section *cur_text_section; /* current section where function code is generated */ +#ifdef CONFIG_TCC_ASM +ST_DATA Section *last_text_section; /* to handle .previous asm directive */ +#endif +#ifdef CONFIG_TCC_BCHECK +/* bound check related sections */ +ST_DATA Section *bounds_section; /* contains global data bound description */ +ST_DATA Section *lbounds_section; /* contains local data bound description */ +ST_FUNC void tccelf_bounds_new(TCCState *s); +#endif +/* symbol sections */ +ST_DATA Section *symtab_section; +/* debug sections */ +ST_DATA Section *stab_section, *stabstr_section; + +ST_FUNC void tccelf_new(TCCState *s); +ST_FUNC void tccelf_delete(TCCState *s); +ST_FUNC void tccelf_stab_new(TCCState *s); +ST_FUNC void tccelf_begin_file(TCCState *s1); +ST_FUNC void tccelf_end_file(TCCState *s1); + +ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags); +ST_FUNC void section_realloc(Section *sec, unsigned long new_size); +ST_FUNC size_t section_add(Section *sec, addr_t size, int align); +ST_FUNC void *section_ptr_add(Section *sec, addr_t size); +ST_FUNC void section_reserve(Section *sec, unsigned long size); +ST_FUNC Section *find_section(TCCState *s1, const char *name); +ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, const char *strtab_name, const char *hash_name, int hash_sh_flags); + +ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, addr_t value, unsigned long size, int can_add_underscore); +ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size); +#if PTR_SIZE == 4 +ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type); +#endif +ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_t addend); + +ST_FUNC int put_elf_str(Section *s, const char *sym); +ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name); +ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name); +ST_FUNC int find_elf_sym(Section *s, const char *name); +ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol); +ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend); + +ST_FUNC void put_stabs(const char *str, int type, int other, int desc, unsigned long value); +ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index); +ST_FUNC void put_stabn(int type, int other, int desc, int value); +ST_FUNC void put_stabd(int type, int other, int desc); + +ST_FUNC void resolve_common_syms(TCCState *s1); +ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve); +ST_FUNC void relocate_section(TCCState *s1, Section *s); + +ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h); +ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset); +ST_FUNC int tcc_load_archive(TCCState *s1, int fd); +ST_FUNC void tcc_add_bcheck(TCCState *s1); +ST_FUNC void tcc_add_runtime(TCCState *s1); + +ST_FUNC void build_got_entries(TCCState *s1); +ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc); +ST_FUNC void squeeze_multi_relocs(Section *sec, size_t oldrelocoffset); + +ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err); +#if defined TCC_IS_NATIVE || defined TCC_TARGET_PE +ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); +#endif + +#ifndef TCC_TARGET_PE +ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level); +ST_FUNC int tcc_load_ldscript(TCCState *s1); +ST_FUNC uint8_t *parse_comment(uint8_t *p); +ST_FUNC void minp(void); +ST_INLN void inp(void); +ST_FUNC int handle_eob(void); +#endif + +/* ------------ xxx-link.c ------------ */ + +/* Whether to generate a GOT/PLT entry and when. NO_GOTPLT_ENTRY is first so + that unknown relocation don't create a GOT or PLT entry */ +enum gotplt_entry { + NO_GOTPLT_ENTRY, /* never generate (eg. GLOB_DAT & JMP_SLOT relocs) */ + BUILD_GOT_ONLY, /* only build GOT (eg. TPOFF relocs) */ + AUTO_GOTPLT_ENTRY, /* generate if sym is UNDEF */ + ALWAYS_GOTPLT_ENTRY /* always generate (eg. PLTOFF relocs) */ +}; + +ST_FUNC int code_reloc (int reloc_type); +ST_FUNC int gotplt_entry_type (int reloc_type); +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr); +ST_FUNC void relocate_init(Section *sr); +ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val); +ST_FUNC void relocate_plt(TCCState *s1); + +/* ------------ xxx-gen.c ------------ */ + +ST_DATA const int reg_classes[NB_REGS]; + +ST_FUNC void gsym_addr(int t, int a); +ST_FUNC void gsym(int t); +ST_FUNC void load(int r, SValue *sv); +ST_FUNC void store(int r, SValue *v); +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align, int *regsize); +ST_FUNC void gfunc_call(int nb_args); +ST_FUNC void gfunc_prolog(CType *func_type); +ST_FUNC void gfunc_epilog(void); +ST_FUNC int gjmp(int t); +ST_FUNC void gjmp_addr(int a); +ST_FUNC int gtst(int inv, int t); +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 +ST_FUNC void gtst_addr(int inv, int a); +#else +#define gtst_addr(inv, a) gsym_addr(gtst(inv, 0), a) +#endif +ST_FUNC void gen_opi(int op); +ST_FUNC void gen_opf(int op); +ST_FUNC void gen_cvt_ftoi(int t); +ST_FUNC void gen_cvt_ftof(int t); +ST_FUNC void ggoto(void); +#ifndef TCC_TARGET_C67 +ST_FUNC void o(unsigned int c); +#endif +#ifndef TCC_TARGET_ARM +ST_FUNC void gen_cvt_itof(int t); +#endif +ST_FUNC void gen_vla_sp_save(int addr); +ST_FUNC void gen_vla_sp_restore(int addr); +ST_FUNC void gen_vla_alloc(CType *type, int align); + +static inline uint16_t read16le(unsigned char *p) { + return p[0] | (uint16_t)p[1] << 8; +} +static inline void write16le(unsigned char *p, uint16_t x) { + p[0] = x & 255; p[1] = x >> 8 & 255; +} +static inline uint32_t read32le(unsigned char *p) { + return read16le(p) | (uint32_t)read16le(p + 2) << 16; +} +static inline void write32le(unsigned char *p, uint32_t x) { + write16le(p, x); write16le(p + 2, x >> 16); +} +static inline void add32le(unsigned char *p, int32_t x) { + write32le(p, read32le(p) + x); +} +static inline uint64_t read64le(unsigned char *p) { + return read32le(p) | (uint64_t)read32le(p + 4) << 32; +} +static inline void write64le(unsigned char *p, uint64_t x) { + write32le(p, x); write32le(p + 4, x >> 32); +} +static inline void add64le(unsigned char *p, int64_t x) { + write64le(p, read64le(p) + x); +} + +/* ------------ i386-gen.c ------------ */ +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 +ST_FUNC void g(int c); +ST_FUNC void gen_le16(int c); +ST_FUNC void gen_le32(int c); +ST_FUNC void gen_addr32(int r, Sym *sym, int c); +ST_FUNC void gen_addrpc32(int r, Sym *sym, int c); +#endif + +#ifdef CONFIG_TCC_BCHECK +ST_FUNC void gen_bounded_ptr_add(void); +ST_FUNC void gen_bounded_ptr_deref(void); +#endif + +/* ------------ x86_64-gen.c ------------ */ +#ifdef TCC_TARGET_X86_64 +ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c); +ST_FUNC void gen_opl(int op); +#ifdef TCC_TARGET_PE +ST_FUNC void gen_vla_result(int addr); +#endif +#endif + +/* ------------ arm-gen.c ------------ */ +#ifdef TCC_TARGET_ARM +#if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP) +PUB_FUNC const char *default_elfinterp(struct TCCState *s); +#endif +ST_FUNC void arm_init(struct TCCState *s); +ST_FUNC void gen_cvt_itof1(int t); +#endif + +/* ------------ arm64-gen.c ------------ */ +#ifdef TCC_TARGET_ARM64 +ST_FUNC void gen_cvt_sxtw(void); +ST_FUNC void gen_opl(int op); +ST_FUNC void gfunc_return(CType *func_type); +ST_FUNC void gen_va_start(void); +ST_FUNC void gen_va_arg(CType *t); +ST_FUNC void gen_clear_cache(void); +#endif + +/* ------------ c67-gen.c ------------ */ +#ifdef TCC_TARGET_C67 +#endif + +/* ------------ tcccoff.c ------------ */ + +#ifdef TCC_TARGET_COFF +ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f); +ST_FUNC int tcc_load_coff(TCCState * s1, int fd); +#endif + +/* ------------ tccasm.c ------------ */ +ST_FUNC void asm_instr(void); +ST_FUNC void asm_global_instr(void); +#ifdef CONFIG_TCC_ASM +ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp); +ST_FUNC Sym* get_asm_sym(int name, Sym *csym); +ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe); +ST_FUNC int asm_int_expr(TCCState *s1); +ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess); +/* ------------ i386-asm.c ------------ */ +ST_FUNC void gen_expr32(ExprValue *pe); +#ifdef TCC_TARGET_X86_64 +ST_FUNC void gen_expr64(ExprValue *pe); +#endif +ST_FUNC void asm_opcode(TCCState *s1, int opcode); +ST_FUNC int asm_parse_regvar(int t); +ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg); +ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier); +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg); +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str); +#endif + +/* ------------ tccpe.c -------------- */ +#ifdef TCC_TARGET_PE +ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd); +ST_FUNC int pe_output_file(TCCState * s1, const char *filename); +ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value); +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 +ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2); +#endif +#ifdef TCC_TARGET_X86_64 +ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack); +#endif +PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp); +/* symbol properties stored in Elf32_Sym->st_other */ +# define ST_PE_EXPORT 0x10 +# define ST_PE_IMPORT 0x20 +# define ST_PE_STDCALL 0x40 +#endif +#define ST_ASM_SET 0x04 + +/* ------------ tccrun.c ----------------- */ +#ifdef TCC_IS_NATIVE +#ifdef CONFIG_TCC_STATIC +#define RTLD_LAZY 0x001 +#define RTLD_NOW 0x002 +#define RTLD_GLOBAL 0x100 +#define RTLD_DEFAULT NULL +/* dummy function for profiling */ +ST_FUNC void *dlopen(const char *filename, int flag); +ST_FUNC void dlclose(void *p); +ST_FUNC const char *dlerror(void); +ST_FUNC void *dlsym(void *handle, const char *symbol); +#endif +#ifdef CONFIG_TCC_BACKTRACE +ST_DATA int rt_num_callers; +ST_DATA const char **rt_bound_error_msg; +ST_DATA void *rt_prog_main; +ST_FUNC void tcc_set_num_callers(int n); +#endif +ST_FUNC void tcc_run_free(TCCState *s1); +#endif + +/* ------------ tcctools.c ----------------- */ +#if 0 /* included in tcc.c */ +ST_FUNC int tcc_tool_ar(TCCState *s, int argc, char **argv); +#ifdef TCC_TARGET_PE +ST_FUNC int tcc_tool_impdef(TCCState *s, int argc, char **argv); +#endif +ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option); +ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename); +#endif + +/********************************************************/ +#undef ST_DATA +#if ONE_SOURCE +#define ST_DATA static +#else +#define ST_DATA +#endif +/********************************************************/ +#endif /* _TCC_H */ diff --git a/05/tcc-final-old/tccasm.c b/05/tcc-final-old/tccasm.c new file mode 100644 index 0000000..c035c8b --- /dev/null +++ b/05/tcc-final-old/tccasm.c @@ -0,0 +1,1277 @@ +/* + * GAS like assembler for TCC + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" +#ifdef CONFIG_TCC_ASM + +ST_FUNC int asm_get_local_label_name(TCCState *s1, unsigned int n) +{ + char buf[64]; + TokenSym *ts; + + snprintf(buf, sizeof(buf), "L..%u", n); + ts = tok_alloc(buf, strlen(buf)); + return ts->tok; +} + +static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global); +static Sym* asm_new_label(TCCState *s1, int label, int is_local); +static Sym* asm_new_label1(TCCState *s1, int label, int is_local, int sh_num, int value); + +static Sym *asm_label_find(int v) +{ + Sym *sym = sym_find(v); + while (sym && sym->sym_scope) + sym = sym->prev_tok; + return sym; +} + +static Sym *asm_label_push(int v) +{ + /* We always add VT_EXTERN, for sym definition that's tentative + (for .set, removed for real defs), for mere references it's correct + as is. */ + Sym *sym = global_identifier_push(v, VT_ASM | VT_EXTERN | VT_STATIC, 0); + sym->r = VT_CONST | VT_SYM; + return sym; +} + +/* Return a symbol we can use inside the assembler, having name NAME. + Symbols from asm and C source share a namespace. If we generate + an asm symbol it's also a (file-global) C symbol, but it's + either not accessible by name (like "L.123"), or its type information + is such that it's not usable without a proper C declaration. + + Sometimes we need symbols accessible by name from asm, which + are anonymous in C, in this case CSYM can be used to transfer + all information from that symbol to the (possibly newly created) + asm symbol. */ +ST_FUNC Sym* get_asm_sym(int name, Sym *csym) +{ + Sym *sym = asm_label_find(name); + if (!sym) { + sym = asm_label_push(name); + if (csym) + sym->c = csym->c; + } + return sym; +} + +static Sym* asm_section_sym(TCCState *s1, Section *sec) +{ + char buf[100]; + int label = tok_alloc(buf, + snprintf(buf, sizeof buf, "L.%s", sec->name) + )->tok; + Sym *sym = asm_label_find(label); + return sym ? sym : asm_new_label1(s1, label, 1, sec->sh_num, 0); +} + +/* We do not use the C expression parser to handle symbols. Maybe the + C expression parser could be tweaked to do so. */ + +static void asm_expr_unary(TCCState *s1, ExprValue *pe) +{ + Sym *sym; + int op, label; + uint64_t n; + const char *p; + + switch(tok) { + case TOK_PPNUM: + p = tokc.str.data; + n = strtoull(p, (char **)&p, 0); + if (*p == 'b' || *p == 'f') { + /* backward or forward label */ + label = asm_get_local_label_name(s1, n); + sym = asm_label_find(label); + if (*p == 'b') { + /* backward : find the last corresponding defined label */ + if (sym && (!sym->c || elfsym(sym)->st_shndx == SHN_UNDEF)) + sym = sym->prev_tok; + if (!sym) + tcc_error("local label '%d' not found backward", n); + } else { + /* forward */ + if (!sym || (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)) { + /* if the last label is defined, then define a new one */ + sym = asm_label_push(label); + } + } + pe->v = 0; + pe->sym = sym; + pe->pcrel = 0; + } else if (*p == '\0') { + pe->v = n; + pe->sym = NULL; + pe->pcrel = 0; + } else { + tcc_error("invalid number syntax"); + } + next(); + break; + case '+': + next(); + asm_expr_unary(s1, pe); + break; + case '-': + case '~': + op = tok; + next(); + asm_expr_unary(s1, pe); + if (pe->sym) + tcc_error("invalid operation with label"); + if (op == '-') + pe->v = -pe->v; + else + pe->v = ~pe->v; + break; + case TOK_CCHAR: + case TOK_LCHAR: + pe->v = tokc.i; + pe->sym = NULL; + pe->pcrel = 0; + next(); + break; + case '(': + next(); + asm_expr(s1, pe); + skip(')'); + break; + case '.': + pe->v = ind; + pe->sym = asm_section_sym(s1, cur_text_section); + pe->pcrel = 0; + next(); + break; + default: + if (tok >= TOK_IDENT) { + ElfSym *esym; + /* label case : if the label was not found, add one */ + sym = get_asm_sym(tok, NULL); + esym = elfsym(sym); + if (esym && esym->st_shndx == SHN_ABS) { + /* if absolute symbol, no need to put a symbol value */ + pe->v = esym->st_value; + pe->sym = NULL; + pe->pcrel = 0; + } else { + pe->v = 0; + pe->sym = sym; + pe->pcrel = 0; + } + next(); + } else { + tcc_error("bad expression syntax [%s]", get_tok_str(tok, &tokc)); + } + break; + } +} + +static void asm_expr_prod(TCCState *s1, ExprValue *pe) +{ + int op; + ExprValue e2; + + asm_expr_unary(s1, pe); + for(;;) { + op = tok; + if (op != '*' && op != '/' && op != '%' && + op != TOK_SHL && op != TOK_SAR) + break; + next(); + asm_expr_unary(s1, &e2); + if (pe->sym || e2.sym) + tcc_error("invalid operation with label"); + switch(op) { + case '*': + pe->v *= e2.v; + break; + case '/': + if (e2.v == 0) { + div_error: + tcc_error("division by zero"); + } + pe->v /= e2.v; + break; + case '%': + if (e2.v == 0) + goto div_error; + pe->v %= e2.v; + break; + case TOK_SHL: + pe->v <<= e2.v; + break; + default: + case TOK_SAR: + pe->v >>= e2.v; + break; + } + } +} + +static void asm_expr_logic(TCCState *s1, ExprValue *pe) +{ + int op; + ExprValue e2; + + asm_expr_prod(s1, pe); + for(;;) { + op = tok; + if (op != '&' && op != '|' && op != '^') + break; + next(); + asm_expr_prod(s1, &e2); + if (pe->sym || e2.sym) + tcc_error("invalid operation with label"); + switch(op) { + case '&': + pe->v &= e2.v; + break; + case '|': + pe->v |= e2.v; + break; + default: + case '^': + pe->v ^= e2.v; + break; + } + } +} + +static inline void asm_expr_sum(TCCState *s1, ExprValue *pe) +{ + int op; + ExprValue e2; + + asm_expr_logic(s1, pe); + for(;;) { + op = tok; + if (op != '+' && op != '-') + break; + next(); + asm_expr_logic(s1, &e2); + if (op == '+') { + if (pe->sym != NULL && e2.sym != NULL) + goto cannot_relocate; + pe->v += e2.v; + if (pe->sym == NULL && e2.sym != NULL) + pe->sym = e2.sym; + } else { + pe->v -= e2.v; + /* NOTE: we are less powerful than gas in that case + because we store only one symbol in the expression */ + if (!e2.sym) { + /* OK */ + } else if (pe->sym == e2.sym) { + /* OK */ + pe->sym = NULL; /* same symbols can be subtracted to NULL */ + } else { + ElfSym *esym1, *esym2; + esym1 = elfsym(pe->sym); + esym2 = elfsym(e2.sym); + if (esym1 && esym1->st_shndx == esym2->st_shndx + && esym1->st_shndx != SHN_UNDEF) { + /* we also accept defined symbols in the same section */ + pe->v += esym1->st_value - esym2->st_value; + pe->sym = NULL; + } else if (esym2->st_shndx == cur_text_section->sh_num) { + /* When subtracting a defined symbol in current section + this actually makes the value PC-relative. */ + pe->v -= esym2->st_value - ind - 4; + pe->pcrel = 1; + e2.sym = NULL; + } else { +cannot_relocate: + tcc_error("invalid operation with label"); + } + } + } + } +} + +static inline void asm_expr_cmp(TCCState *s1, ExprValue *pe) +{ + int op; + ExprValue e2; + + asm_expr_sum(s1, pe); + for(;;) { + op = tok; + if (op != TOK_EQ && op != TOK_NE + && (op > TOK_GT || op < TOK_ULE)) + break; + next(); + asm_expr_sum(s1, &e2); + if (pe->sym || e2.sym) + tcc_error("invalid operation with label"); + switch(op) { + case TOK_EQ: + pe->v = pe->v == e2.v; + break; + case TOK_NE: + pe->v = pe->v != e2.v; + break; + case TOK_LT: + pe->v = (int64_t)pe->v < (int64_t)e2.v; + break; + case TOK_GE: + pe->v = (int64_t)pe->v >= (int64_t)e2.v; + break; + case TOK_LE: + pe->v = (int64_t)pe->v <= (int64_t)e2.v; + break; + case TOK_GT: + pe->v = (int64_t)pe->v > (int64_t)e2.v; + break; + default: + break; + } + /* GAS compare results are -1/0 not 1/0. */ + pe->v = -(int64_t)pe->v; + } +} + +ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe) +{ + asm_expr_cmp(s1, pe); +} + +ST_FUNC int asm_int_expr(TCCState *s1) +{ + ExprValue e; + asm_expr(s1, &e); + if (e.sym) + expect("constant"); + return e.v; +} + +static Sym* asm_new_label1(TCCState *s1, int label, int is_local, + int sh_num, int value) +{ + Sym *sym; + ElfSym *esym; + + sym = asm_label_find(label); + if (sym) { + esym = elfsym(sym); + /* A VT_EXTERN symbol, even if it has a section is considered + overridable. This is how we "define" .set targets. Real + definitions won't have VT_EXTERN set. */ + if (esym && esym->st_shndx != SHN_UNDEF) { + /* the label is already defined */ + if (IS_ASM_SYM(sym) + && (is_local == 1 || (sym->type.t & VT_EXTERN))) + goto new_label; + if (!(sym->type.t & VT_EXTERN)) + tcc_error("assembler label '%s' already defined", + get_tok_str(label, NULL)); + } + } else { + new_label: + sym = asm_label_push(label); + } + if (!sym->c) + put_extern_sym2(sym, SHN_UNDEF, 0, 0, 0); + esym = elfsym(sym); + esym->st_shndx = sh_num; + esym->st_value = value; + if (is_local != 2) + sym->type.t &= ~VT_EXTERN; + return sym; +} + +static Sym* asm_new_label(TCCState *s1, int label, int is_local) +{ + return asm_new_label1(s1, label, is_local, cur_text_section->sh_num, ind); +} + +/* Set the value of LABEL to that of some expression (possibly + involving other symbols). LABEL can be overwritten later still. */ +static Sym* set_symbol(TCCState *s1, int label) +{ + long n; + ExprValue e; + Sym *sym; + ElfSym *esym; + next(); + asm_expr(s1, &e); + n = e.v; + esym = elfsym(e.sym); + if (esym) + n += esym->st_value; + sym = asm_new_label1(s1, label, 2, esym ? esym->st_shndx : SHN_ABS, n); + elfsym(sym)->st_other |= ST_ASM_SET; + return sym; +} + +static void use_section1(TCCState *s1, Section *sec) +{ + cur_text_section->data_offset = ind; + cur_text_section = sec; + ind = cur_text_section->data_offset; +} + +static void use_section(TCCState *s1, const char *name) +{ + Section *sec; + sec = find_section(s1, name); + use_section1(s1, sec); +} + +static void push_section(TCCState *s1, const char *name) +{ + Section *sec = find_section(s1, name); + sec->prev = cur_text_section; + use_section1(s1, sec); +} + +static void pop_section(TCCState *s1) +{ + Section *prev = cur_text_section->prev; + if (!prev) + tcc_error(".popsection without .pushsection"); + cur_text_section->prev = NULL; + use_section1(s1, prev); +} + +static void asm_parse_directive(TCCState *s1, int global) +{ + int n, offset, v, size, tok1; + Section *sec; + uint8_t *ptr; + + /* assembler directive */ + sec = cur_text_section; + switch(tok) { + case TOK_ASMDIR_align: + case TOK_ASMDIR_balign: + case TOK_ASMDIR_p2align: + case TOK_ASMDIR_skip: + case TOK_ASMDIR_space: + tok1 = tok; + next(); + n = asm_int_expr(s1); + if (tok1 == TOK_ASMDIR_p2align) + { + if (n < 0 || n > 30) + tcc_error("invalid p2align, must be between 0 and 30"); + n = 1 << n; + tok1 = TOK_ASMDIR_align; + } + if (tok1 == TOK_ASMDIR_align || tok1 == TOK_ASMDIR_balign) { + if (n < 0 || (n & (n-1)) != 0) + tcc_error("alignment must be a positive power of two"); + offset = (ind + n - 1) & -n; + size = offset - ind; + /* the section must have a compatible alignment */ + if (sec->sh_addralign < n) + sec->sh_addralign = n; + } else { + if (n < 0) + n = 0; + size = n; + } + v = 0; + if (tok == ',') { + next(); + v = asm_int_expr(s1); + } + zero_pad: + if (sec->sh_type != SHT_NOBITS) { + sec->data_offset = ind; + ptr = section_ptr_add(sec, size); + memset(ptr, v, size); + } + ind += size; + break; + case TOK_ASMDIR_quad: +#ifdef TCC_TARGET_X86_64 + size = 8; + goto asm_data; +#else + next(); + for(;;) { + uint64_t vl; + const char *p; + + p = tokc.str.data; + if (tok != TOK_PPNUM) { + error_constant: + tcc_error("64 bit constant"); + } + vl = strtoll(p, (char **)&p, 0); + if (*p != '\0') + goto error_constant; + next(); + if (sec->sh_type != SHT_NOBITS) { + /* XXX: endianness */ + gen_le32(vl); + gen_le32(vl >> 32); + } else { + ind += 8; + } + if (tok != ',') + break; + next(); + } + break; +#endif + case TOK_ASMDIR_byte: + size = 1; + goto asm_data; + case TOK_ASMDIR_word: + case TOK_ASMDIR_short: + size = 2; + goto asm_data; + case TOK_ASMDIR_long: + case TOK_ASMDIR_int: + size = 4; + asm_data: + next(); + for(;;) { + ExprValue e; + asm_expr(s1, &e); + if (sec->sh_type != SHT_NOBITS) { + if (size == 4) { + gen_expr32(&e); +#ifdef TCC_TARGET_X86_64 + } else if (size == 8) { + gen_expr64(&e); +#endif + } else { + if (e.sym) + expect("constant"); + if (size == 1) + g(e.v); + else + gen_le16(e.v); + } + } else { + ind += size; + } + if (tok != ',') + break; + next(); + } + break; + case TOK_ASMDIR_fill: + { + int repeat, size, val, i, j; + uint8_t repeat_buf[8]; + next(); + repeat = asm_int_expr(s1); + if (repeat < 0) { + tcc_error("repeat < 0; .fill ignored"); + break; + } + size = 1; + val = 0; + if (tok == ',') { + next(); + size = asm_int_expr(s1); + if (size < 0) { + tcc_error("size < 0; .fill ignored"); + break; + } + if (size > 8) + size = 8; + if (tok == ',') { + next(); + val = asm_int_expr(s1); + } + } + /* XXX: endianness */ + repeat_buf[0] = val; + repeat_buf[1] = val >> 8; + repeat_buf[2] = val >> 16; + repeat_buf[3] = val >> 24; + repeat_buf[4] = 0; + repeat_buf[5] = 0; + repeat_buf[6] = 0; + repeat_buf[7] = 0; + for(i = 0; i < repeat; i++) { + for(j = 0; j < size; j++) { + g(repeat_buf[j]); + } + } + } + break; + case TOK_ASMDIR_rept: + { + int repeat; + TokenString *init_str; + next(); + repeat = asm_int_expr(s1); + init_str = tok_str_alloc(); + while (next(), tok != TOK_ASMDIR_endr) { + if (tok == CH_EOF) + tcc_error("we at end of file, .endr not found"); + tok_str_add_tok(init_str); + } + tok_str_add(init_str, -1); + tok_str_add(init_str, 0); + begin_macro(init_str, 1); + while (repeat-- > 0) { + tcc_assemble_internal(s1, (parse_flags & PARSE_FLAG_PREPROCESS), + global); + macro_ptr = init_str->str; + } + end_macro(); + next(); + break; + } + case TOK_ASMDIR_org: + { + unsigned long n; + ExprValue e; + ElfSym *esym; + next(); + asm_expr(s1, &e); + n = e.v; + esym = elfsym(e.sym); + if (esym) { + if (esym->st_shndx != cur_text_section->sh_num) + expect("constant or same-section symbol"); + n += esym->st_value; + } + if (n < ind) + tcc_error("attempt to .org backwards"); + v = 0; + size = n - ind; + goto zero_pad; + } + break; + case TOK_ASMDIR_set: + next(); + tok1 = tok; + next(); + /* Also accept '.set stuff', but don't do anything with this. + It's used in GAS to set various features like '.set mips16'. */ + if (tok == ',') + set_symbol(s1, tok1); + break; + case TOK_ASMDIR_globl: + case TOK_ASMDIR_global: + case TOK_ASMDIR_weak: + case TOK_ASMDIR_hidden: + tok1 = tok; + do { + Sym *sym; + next(); + sym = get_asm_sym(tok, NULL); + if (tok1 != TOK_ASMDIR_hidden) + sym->type.t &= ~VT_STATIC; + if (tok1 == TOK_ASMDIR_weak) + sym->a.weak = 1; + else if (tok1 == TOK_ASMDIR_hidden) + sym->a.visibility = STV_HIDDEN; + update_storage(sym); + next(); + } while (tok == ','); + break; + case TOK_ASMDIR_string: + case TOK_ASMDIR_ascii: + case TOK_ASMDIR_asciz: + { + const uint8_t *p; + int i, size, t; + + t = tok; + next(); + for(;;) { + if (tok != TOK_STR) + expect("string constant"); + p = tokc.str.data; + size = tokc.str.size; + if (t == TOK_ASMDIR_ascii && size > 0) + size--; + for(i = 0; i < size; i++) + g(p[i]); + next(); + if (tok == ',') { + next(); + } else if (tok != TOK_STR) { + break; + } + } + } + break; + case TOK_ASMDIR_text: + case TOK_ASMDIR_data: + case TOK_ASMDIR_bss: + { + char sname[64]; + tok1 = tok; + n = 0; + next(); + if (tok != ';' && tok != TOK_LINEFEED) { + n = asm_int_expr(s1); + next(); + } + if (n) + sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n); + else + sprintf(sname, "%s", get_tok_str(tok1, NULL)); + use_section(s1, sname); + } + break; + case TOK_ASMDIR_file: + { + char filename[512]; + + filename[0] = '\0'; + next(); + + if (tok == TOK_STR) + pstrcat(filename, sizeof(filename), tokc.str.data); + else + pstrcat(filename, sizeof(filename), get_tok_str(tok, NULL)); + + if (s1->warn_unsupported) + tcc_warning("ignoring .file %s", filename); + + next(); + } + break; + case TOK_ASMDIR_ident: + { + char ident[256]; + + ident[0] = '\0'; + next(); + + if (tok == TOK_STR) + pstrcat(ident, sizeof(ident), tokc.str.data); + else + pstrcat(ident, sizeof(ident), get_tok_str(tok, NULL)); + + if (s1->warn_unsupported) + tcc_warning("ignoring .ident %s", ident); + + next(); + } + break; + case TOK_ASMDIR_size: + { + Sym *sym; + + next(); + sym = asm_label_find(tok); + if (!sym) { + tcc_error("label not found: %s", get_tok_str(tok, NULL)); + } + + /* XXX .size name,label2-label1 */ + if (s1->warn_unsupported) + tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL)); + + next(); + skip(','); + while (tok != TOK_LINEFEED && tok != ';' && tok != CH_EOF) { + next(); + } + } + break; + case TOK_ASMDIR_type: + { + Sym *sym; + const char *newtype; + + next(); + sym = get_asm_sym(tok, NULL); + next(); + skip(','); + if (tok == TOK_STR) { + newtype = tokc.str.data; + } else { + if (tok == '@' || tok == '%') + next(); + newtype = get_tok_str(tok, NULL); + } + + if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) { + sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC; + } + else if (s1->warn_unsupported) + tcc_warning("change type of '%s' from 0x%x to '%s' ignored", + get_tok_str(sym->v, NULL), sym->type.t, newtype); + + next(); + } + break; + case TOK_ASMDIR_pushsection: + case TOK_ASMDIR_section: + { + char sname[256]; + int old_nb_section = s1->nb_sections; + + tok1 = tok; + /* XXX: support more options */ + next(); + sname[0] = '\0'; + while (tok != ';' && tok != TOK_LINEFEED && tok != ',') { + if (tok == TOK_STR) + pstrcat(sname, sizeof(sname), tokc.str.data); + else + pstrcat(sname, sizeof(sname), get_tok_str(tok, NULL)); + next(); + } + if (tok == ',') { + /* skip section options */ + next(); + if (tok != TOK_STR) + expect("string constant"); + next(); + if (tok == ',') { + next(); + if (tok == '@' || tok == '%') + next(); + next(); + } + } + last_text_section = cur_text_section; + if (tok1 == TOK_ASMDIR_section) + use_section(s1, sname); + else + push_section(s1, sname); + /* If we just allocated a new section reset its alignment to + 1. new_section normally acts for GCC compatibility and + sets alignment to PTR_SIZE. The assembler behaves different. */ + if (old_nb_section != s1->nb_sections) + cur_text_section->sh_addralign = 1; + } + break; + case TOK_ASMDIR_previous: + { + Section *sec; + next(); + if (!last_text_section) + tcc_error("no previous section referenced"); + sec = cur_text_section; + use_section1(s1, last_text_section); + last_text_section = sec; + } + break; + case TOK_ASMDIR_popsection: + next(); + pop_section(s1); + break; +#ifdef TCC_TARGET_I386 + case TOK_ASMDIR_code16: + { + next(); + s1->seg_size = 16; + } + break; + case TOK_ASMDIR_code32: + { + next(); + s1->seg_size = 32; + } + break; +#endif +#ifdef TCC_TARGET_X86_64 + /* added for compatibility with GAS */ + case TOK_ASMDIR_code64: + next(); + break; +#endif + default: + tcc_error("unknown assembler directive '.%s'", get_tok_str(tok, NULL)); + break; + } +} + + +/* assemble a file */ +static int tcc_assemble_internal(TCCState *s1, int do_preprocess, int global) +{ + int opcode; + int saved_parse_flags = parse_flags; + + parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR; + if (do_preprocess) + parse_flags |= PARSE_FLAG_PREPROCESS; + for(;;) { + next(); + if (tok == TOK_EOF) + break; + /* generate line number info */ + if (global && s1->do_debug) + tcc_debug_line(s1); + parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */ + redo: + if (tok == '#') { + /* horrible gas comment */ + while (tok != TOK_LINEFEED) + next(); + } else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) { + asm_parse_directive(s1, global); + } else if (tok == TOK_PPNUM) { + const char *p; + int n; + p = tokc.str.data; + n = strtoul(p, (char **)&p, 10); + if (*p != '\0') + expect("':'"); + /* new local label */ + asm_new_label(s1, asm_get_local_label_name(s1, n), 1); + next(); + skip(':'); + goto redo; + } else if (tok >= TOK_IDENT) { + /* instruction or label */ + opcode = tok; + next(); + if (tok == ':') { + /* new label */ + asm_new_label(s1, opcode, 0); + next(); + goto redo; + } else if (tok == '=') { + set_symbol(s1, opcode); + goto redo; + } else { + asm_opcode(s1, opcode); + } + } + /* end of line */ + if (tok != ';' && tok != TOK_LINEFEED) + expect("end of line"); + parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */ + } + + parse_flags = saved_parse_flags; + return 0; +} + +/* Assemble the current file */ +ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess) +{ + int ret; + tcc_debug_start(s1); + /* default section is text */ + cur_text_section = text_section; + ind = cur_text_section->data_offset; + nocode_wanted = 0; + ret = tcc_assemble_internal(s1, do_preprocess, 1); + cur_text_section->data_offset = ind; + tcc_debug_end(s1); + return ret; +} + +/********************************************************************/ +/* GCC inline asm support */ + +/* assemble the string 'str' in the current C compilation unit without + C preprocessing. NOTE: str is modified by modifying the '\0' at the + end */ +static void tcc_assemble_inline(TCCState *s1, char *str, int len, int global) +{ + const int *saved_macro_ptr = macro_ptr; + int dotid = set_idnum('.', IS_ID); + + tcc_open_bf(s1, ":asm:", len); + memcpy(file->buffer, str, len); + macro_ptr = NULL; + tcc_assemble_internal(s1, 0, global); + tcc_close(); + + set_idnum('.', dotid); + macro_ptr = saved_macro_ptr; +} + +/* find a constraint by its number or id (gcc 3 extended + syntax). return -1 if not found. Return in *pp in char after the + constraint */ +ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, + const char *name, const char **pp) +{ + int index; + TokenSym *ts; + const char *p; + + if (isnum(*name)) { + index = 0; + while (isnum(*name)) { + index = (index * 10) + (*name) - '0'; + name++; + } + if ((unsigned)index >= nb_operands) + index = -1; + } else if (*name == '[') { + name++; + p = strchr(name, ']'); + if (p) { + ts = tok_alloc(name, p - name); + for(index = 0; index < nb_operands; index++) { + if (operands[index].id == ts->tok) + goto found; + } + index = -1; + found: + name = p + 1; + } else { + index = -1; + } + } else { + index = -1; + } + if (pp) + *pp = name; + return index; +} + +static void subst_asm_operands(ASMOperand *operands, int nb_operands, + CString *out_str, CString *in_str) +{ + int c, index, modifier; + const char *str; + ASMOperand *op; + SValue sv; + + cstr_new(out_str); + str = in_str->data; + for(;;) { + c = *str++; + if (c == '%') { + if (*str == '%') { + str++; + goto add_char; + } + modifier = 0; + if (*str == 'c' || *str == 'n' || + *str == 'b' || *str == 'w' || *str == 'h' || *str == 'k' || + *str == 'q' || + /* P in GCC would add "@PLT" to symbol refs in PIC mode, + and make literal operands not be decorated with '$'. */ + *str == 'P') + modifier = *str++; + index = find_constraint(operands, nb_operands, str, &str); + if (index < 0) + tcc_error("invalid operand reference after %%"); + op = &operands[index]; + sv = *op->vt; + if (op->reg >= 0) { + sv.r = op->reg; + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && op->is_memory) + sv.r |= VT_LVAL; + } + subst_asm_operand(out_str, &sv, modifier); + } else { + add_char: + cstr_ccat(out_str, c); + if (c == '\0') + break; + } + } +} + + +static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr, + int is_output) +{ + ASMOperand *op; + int nb_operands; + + if (tok != ':') { + nb_operands = *nb_operands_ptr; + for(;;) { + CString astr; + if (nb_operands >= MAX_ASM_OPERANDS) + tcc_error("too many asm operands"); + op = &operands[nb_operands++]; + op->id = 0; + if (tok == '[') { + next(); + if (tok < TOK_IDENT) + expect("identifier"); + op->id = tok; + next(); + skip(']'); + } + parse_mult_str(&astr, "string constant"); + op->constraint = tcc_malloc(astr.size); + strcpy(op->constraint, astr.data); + cstr_free(&astr); + skip('('); + gexpr(); + if (is_output) { + if (!(vtop->type.t & VT_ARRAY)) + test_lvalue(); + } else { + /* we want to avoid LLOCAL case, except when the 'm' + constraint is used. Note that it may come from + register storage, so we need to convert (reg) + case */ + if ((vtop->r & VT_LVAL) && + ((vtop->r & VT_VALMASK) == VT_LLOCAL || + (vtop->r & VT_VALMASK) < VT_CONST) && + !strchr(op->constraint, 'm')) { + gv(RC_INT); + } + } + op->vt = vtop; + skip(')'); + if (tok == ',') { + next(); + } else { + break; + } + } + *nb_operands_ptr = nb_operands; + } +} + +/* parse the GCC asm() instruction */ +ST_FUNC void asm_instr(void) +{ + CString astr, astr1; + ASMOperand operands[MAX_ASM_OPERANDS]; + int nb_outputs, nb_operands, i, must_subst, out_reg; + uint8_t clobber_regs[NB_ASM_REGS]; + + next(); + /* since we always generate the asm() instruction, we can ignore + volatile */ + if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) { + next(); + } + parse_asm_str(&astr); + nb_operands = 0; + nb_outputs = 0; + must_subst = 0; + memset(clobber_regs, 0, sizeof(clobber_regs)); + if (tok == ':') { + next(); + must_subst = 1; + /* output args */ + parse_asm_operands(operands, &nb_operands, 1); + nb_outputs = nb_operands; + if (tok == ':') { + next(); + if (tok != ')') { + /* input args */ + parse_asm_operands(operands, &nb_operands, 0); + if (tok == ':') { + /* clobber list */ + /* XXX: handle registers */ + next(); + for(;;) { + if (tok != TOK_STR) + expect("string constant"); + asm_clobber(clobber_regs, tokc.str.data); + next(); + if (tok == ',') { + next(); + } else { + break; + } + } + } + } + } + } + skip(')'); + /* NOTE: we do not eat the ';' so that we can restore the current + token after the assembler parsing */ + if (tok != ';') + expect("';'"); + + /* save all values in the memory */ + save_regs(0); + + /* compute constraints */ + asm_compute_constraints(operands, nb_operands, nb_outputs, + clobber_regs, &out_reg); + + /* substitute the operands in the asm string. No substitution is + done if no operands (GCC behaviour) */ +#ifdef ASM_DEBUG + printf("asm: \"%s\"\n", (char *)astr.data); +#endif + if (must_subst) { + subst_asm_operands(operands, nb_operands, &astr1, &astr); + cstr_free(&astr); + } else { + astr1 = astr; + } +#ifdef ASM_DEBUG + printf("subst_asm: \"%s\"\n", (char *)astr1.data); +#endif + + /* generate loads */ + asm_gen_code(operands, nb_operands, nb_outputs, 0, + clobber_regs, out_reg); + + /* assemble the string with tcc internal assembler */ + tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1, 0); + + /* restore the current C token */ + next(); + + /* store the output values if needed */ + asm_gen_code(operands, nb_operands, nb_outputs, 1, + clobber_regs, out_reg); + + /* free everything */ + for(i=0;iconstraint); + vpop(); + } + cstr_free(&astr1); +} + +ST_FUNC void asm_global_instr(void) +{ + CString astr; + int saved_nocode_wanted = nocode_wanted; + + /* Global asm blocks are always emitted. */ + nocode_wanted = 0; + next(); + parse_asm_str(&astr); + skip(')'); + /* NOTE: we do not eat the ';' so that we can restore the current + token after the assembler parsing */ + if (tok != ';') + expect("';'"); + +#ifdef ASM_DEBUG + printf("asm_global: \"%s\"\n", (char *)astr.data); +#endif + cur_text_section = text_section; + ind = cur_text_section->data_offset; + + /* assemble the string with tcc internal assembler */ + tcc_assemble_inline(tcc_state, astr.data, astr.size - 1, 1); + + cur_text_section->data_offset = ind; + + /* restore the current C token */ + next(); + + cstr_free(&astr); + nocode_wanted = saved_nocode_wanted; +} +#endif /* CONFIG_TCC_ASM */ diff --git a/05/tcc-final-old/tcccoff.c b/05/tcc-final-old/tcccoff.c new file mode 100644 index 0000000..1421ca2 --- /dev/null +++ b/05/tcc-final-old/tcccoff.c @@ -0,0 +1,948 @@ +/* + * COFF file handling for TCC + * + * Copyright (c) 2003, 2004 TK + * Copyright (c) 2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +#define MAXNSCNS 255 /* MAXIMUM NUMBER OF SECTIONS */ +#define MAX_STR_TABLE 1000000 +AOUTHDR o_filehdr; /* OPTIONAL (A.OUT) FILE HEADER */ + +SCNHDR section_header[MAXNSCNS]; + +#define MAX_FUNCS 1000 +#define MAX_FUNC_NAME_LENGTH 128 + +int nFuncs; +char Func[MAX_FUNCS][MAX_FUNC_NAME_LENGTH]; +char AssociatedFile[MAX_FUNCS][MAX_FUNC_NAME_LENGTH]; +int LineNoFilePtr[MAX_FUNCS]; +int EndAddress[MAX_FUNCS]; +int LastLineNo[MAX_FUNCS]; +int FuncEntries[MAX_FUNCS]; + +int OutputTheSection(Section * sect); +short int GetCoffFlags(const char *s); +void SortSymbolTable(void); +Section *FindSection(TCCState * s1, const char *sname); + +int C67_main_entry_point; + +int FindCoffSymbolIndex(const char *func_name); +int nb_syms; + +typedef struct { + long tag; + long size; + long fileptr; + long nextsym; + short int dummy; +} AUXFUNC; + +typedef struct { + long regmask; + unsigned short lineno; + unsigned short nentries; + int localframe; + int nextentry; + short int dummy; +} AUXBF; + +typedef struct { + long dummy; + unsigned short lineno; + unsigned short dummy1; + int dummy2; + int dummy3; + unsigned short dummy4; +} AUXEF; + +ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f) +{ + Section *tcc_sect; + SCNHDR *coff_sec; + int file_pointer; + char *Coff_str_table, *pCoff_str_table; + int CoffTextSectionNo, coff_nb_syms; + FILHDR file_hdr; /* FILE HEADER STRUCTURE */ + Section *stext, *sdata, *sbss; + int i, NSectionsToOutput = 0; + + Coff_str_table = pCoff_str_table = NULL; + + stext = FindSection(s1, ".text"); + sdata = FindSection(s1, ".data"); + sbss = FindSection(s1, ".bss"); + + nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym); + coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1"); + + file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */ + file_hdr.f_timdat = 0; /* time & date stamp */ + file_hdr.f_opthdr = sizeof(AOUTHDR); /* sizeof(optional hdr) */ + file_hdr.f_flags = 0x1143; /* flags (copied from what code composer does) */ + file_hdr.f_TargetID = 0x99; /* for C6x = 0x0099 */ + + o_filehdr.magic = 0x0108; /* see magic.h */ + o_filehdr.vstamp = 0x0190; /* version stamp */ + o_filehdr.tsize = stext->data_offset; /* text size in bytes, padded to FW bdry */ + o_filehdr.dsize = sdata->data_offset; /* initialized data " " */ + o_filehdr.bsize = sbss->data_offset; /* uninitialized data " " */ + o_filehdr.entrypt = C67_main_entry_point; /* entry pt. */ + o_filehdr.text_start = stext->sh_addr; /* base of text used for this file */ + o_filehdr.data_start = sdata->sh_addr; /* base of data used for this file */ + + + // create all the section headers + + file_pointer = FILHSZ + sizeof(AOUTHDR); + + CoffTextSectionNo = -1; + + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (OutputTheSection(tcc_sect)) { + NSectionsToOutput++; + + if (CoffTextSectionNo == -1 && tcc_sect == stext) + CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is + + strcpy(coff_sec->s_name, tcc_sect->name); /* section name */ + + coff_sec->s_paddr = tcc_sect->sh_addr; /* physical address */ + coff_sec->s_vaddr = tcc_sect->sh_addr; /* virtual address */ + coff_sec->s_size = tcc_sect->data_offset; /* section size */ + coff_sec->s_scnptr = 0; /* file ptr to raw data for section */ + coff_sec->s_relptr = 0; /* file ptr to relocation */ + coff_sec->s_lnnoptr = 0; /* file ptr to line numbers */ + coff_sec->s_nreloc = 0; /* number of relocation entries */ + coff_sec->s_flags = GetCoffFlags(coff_sec->s_name); /* flags */ + coff_sec->s_reserved = 0; /* reserved byte */ + coff_sec->s_page = 0; /* memory page id */ + + file_pointer += sizeof(SCNHDR); + } + } + + file_hdr.f_nscns = NSectionsToOutput; /* number of sections */ + + // now loop through and determine file pointer locations + // for the raw data + + + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (OutputTheSection(tcc_sect)) { + // put raw data + coff_sec->s_scnptr = file_pointer; /* file ptr to raw data for section */ + file_pointer += coff_sec->s_size; + } + } + + // now loop through and determine file pointer locations + // for the relocation data + + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (OutputTheSection(tcc_sect)) { + // put relocations data + if (coff_sec->s_nreloc > 0) { + coff_sec->s_relptr = file_pointer; /* file ptr to relocation */ + file_pointer += coff_sec->s_nreloc * sizeof(struct reloc); + } + } + } + + // now loop through and determine file pointer locations + // for the line number data + + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + coff_sec->s_nlnno = 0; + coff_sec->s_lnnoptr = 0; + + if (s1->do_debug && tcc_sect == stext) { + // count how many line nos data + + // also find association between source file name and function + // so we can sort the symbol table + + + Stab_Sym *sym, *sym_end; + char func_name[MAX_FUNC_NAME_LENGTH], + last_func_name[MAX_FUNC_NAME_LENGTH]; + unsigned long func_addr, last_pc, pc; + const char *incl_files[INCLUDE_STACK_SIZE]; + int incl_index, len, last_line_num; + const char *str, *p; + + coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */ + + + func_name[0] = '\0'; + func_addr = 0; + incl_index = 0; + last_func_name[0] = '\0'; + last_pc = 0xffffffff; + last_line_num = 1; + sym = (Stab_Sym *) stab_section->data + 1; + sym_end = + (Stab_Sym *) (stab_section->data + + stab_section->data_offset); + + nFuncs = 0; + while (sym < sym_end) { + switch (sym->n_type) { + /* function start or end */ + case N_FUN: + if (sym->n_strx == 0) { + // end of function + + coff_sec->s_nlnno++; + file_pointer += LINESZ; + + pc = sym->n_value + func_addr; + func_name[0] = '\0'; + func_addr = 0; + EndAddress[nFuncs] = pc; + FuncEntries[nFuncs] = + (file_pointer - + LineNoFilePtr[nFuncs]) / LINESZ - 1; + LastLineNo[nFuncs++] = last_line_num + 1; + } else { + // beginning of function + + LineNoFilePtr[nFuncs] = file_pointer; + coff_sec->s_nlnno++; + file_pointer += LINESZ; + + str = + (const char *) stabstr_section->data + + sym->n_strx; + + p = strchr(str, ':'); + if (!p) { + pstrcpy(func_name, sizeof(func_name), str); + pstrcpy(Func[nFuncs], sizeof(func_name), str); + } else { + len = p - str; + if (len > sizeof(func_name) - 1) + len = sizeof(func_name) - 1; + memcpy(func_name, str, len); + memcpy(Func[nFuncs], str, len); + func_name[len] = '\0'; + } + + // save the file that it came in so we can sort later + pstrcpy(AssociatedFile[nFuncs], sizeof(func_name), + incl_files[incl_index - 1]); + + func_addr = sym->n_value; + } + break; + + /* line number info */ + case N_SLINE: + pc = sym->n_value + func_addr; + + last_pc = pc; + last_line_num = sym->n_desc; + + /* XXX: slow! */ + strcpy(last_func_name, func_name); + + coff_sec->s_nlnno++; + file_pointer += LINESZ; + break; + /* include files */ + case N_BINCL: + str = + (const char *) stabstr_section->data + sym->n_strx; + add_incl: + if (incl_index < INCLUDE_STACK_SIZE) { + incl_files[incl_index++] = str; + } + break; + case N_EINCL: + if (incl_index > 1) + incl_index--; + break; + case N_SO: + if (sym->n_strx == 0) { + incl_index = 0; /* end of translation unit */ + } else { + str = + (const char *) stabstr_section->data + + sym->n_strx; + /* do not add path */ + len = strlen(str); + if (len > 0 && str[len - 1] != '/') + goto add_incl; + } + break; + } + sym++; + } + } + + } + + file_hdr.f_symptr = file_pointer; /* file pointer to symtab */ + + if (s1->do_debug) + file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */ + else + file_hdr.f_nsyms = 0; + + file_pointer += file_hdr.f_nsyms * SYMNMLEN; + + // OK now we are all set to write the file + + + fwrite(&file_hdr, FILHSZ, 1, f); + fwrite(&o_filehdr, sizeof(o_filehdr), 1, f); + + // write section headers + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (OutputTheSection(tcc_sect)) { + fwrite(coff_sec, sizeof(SCNHDR), 1, f); + } + } + + // write raw data + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (OutputTheSection(tcc_sect)) { + fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f); + } + } + + // write relocation data + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (OutputTheSection(tcc_sect)) { + // put relocations data + if (coff_sec->s_nreloc > 0) { + fwrite(tcc_sect->reloc, + coff_sec->s_nreloc * sizeof(struct reloc), 1, f); + } + } + } + + + // group the symbols in order of filename, func1, func2, etc + // finally global symbols + + if (s1->do_debug) + SortSymbolTable(); + + // write line no data + + for (i = 1; i < s1->nb_sections; i++) { + coff_sec = §ion_header[i]; + tcc_sect = s1->sections[i]; + + if (s1->do_debug && tcc_sect == stext) { + // count how many line nos data + + + Stab_Sym *sym, *sym_end; + char func_name[128], last_func_name[128]; + unsigned long func_addr, last_pc, pc; + const char *incl_files[INCLUDE_STACK_SIZE]; + int incl_index, len, last_line_num; + const char *str, *p; + + LINENO CoffLineNo; + + func_name[0] = '\0'; + func_addr = 0; + incl_index = 0; + last_func_name[0] = '\0'; + last_pc = 0; + last_line_num = 1; + sym = (Stab_Sym *) stab_section->data + 1; + sym_end = + (Stab_Sym *) (stab_section->data + + stab_section->data_offset); + + while (sym < sym_end) { + switch (sym->n_type) { + /* function start or end */ + case N_FUN: + if (sym->n_strx == 0) { + // end of function + + CoffLineNo.l_addr.l_paddr = last_pc; + CoffLineNo.l_lnno = last_line_num + 1; + fwrite(&CoffLineNo, 6, 1, f); + + pc = sym->n_value + func_addr; + func_name[0] = '\0'; + func_addr = 0; + } else { + // beginning of function + + str = + (const char *) stabstr_section->data + + sym->n_strx; + + + p = strchr(str, ':'); + if (!p) { + pstrcpy(func_name, sizeof(func_name), str); + } else { + len = p - str; + if (len > sizeof(func_name) - 1) + len = sizeof(func_name) - 1; + memcpy(func_name, str, len); + func_name[len] = '\0'; + } + func_addr = sym->n_value; + last_pc = func_addr; + last_line_num = -1; + + // output a function begin + + CoffLineNo.l_addr.l_symndx = + FindCoffSymbolIndex(func_name); + CoffLineNo.l_lnno = 0; + + fwrite(&CoffLineNo, 6, 1, f); + } + break; + + /* line number info */ + case N_SLINE: + pc = sym->n_value + func_addr; + + + /* XXX: slow! */ + strcpy(last_func_name, func_name); + + // output a line reference + + CoffLineNo.l_addr.l_paddr = last_pc; + + if (last_line_num == -1) { + CoffLineNo.l_lnno = sym->n_desc; + } else { + CoffLineNo.l_lnno = last_line_num + 1; + } + + fwrite(&CoffLineNo, 6, 1, f); + + last_pc = pc; + last_line_num = sym->n_desc; + + break; + + /* include files */ + case N_BINCL: + str = + (const char *) stabstr_section->data + sym->n_strx; + add_incl2: + if (incl_index < INCLUDE_STACK_SIZE) { + incl_files[incl_index++] = str; + } + break; + case N_EINCL: + if (incl_index > 1) + incl_index--; + break; + case N_SO: + if (sym->n_strx == 0) { + incl_index = 0; /* end of translation unit */ + } else { + str = + (const char *) stabstr_section->data + + sym->n_strx; + /* do not add path */ + len = strlen(str); + if (len > 0 && str[len - 1] != '/') + goto add_incl2; + } + break; + } + sym++; + } + } + } + + // write symbol table + if (s1->do_debug) { + int k; + struct syment csym; + AUXFUNC auxfunc; + AUXBF auxbf; + AUXEF auxef; + int i; + Elf32_Sym *p; + const char *name; + int nstr; + int n = 0; + + Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE); + pCoff_str_table = Coff_str_table; + nstr = 0; + + p = (Elf32_Sym *) symtab_section->data; + + + for (i = 0; i < nb_syms; i++) { + + name = symtab_section->link->data + p->st_name; + + for (k = 0; k < 8; k++) + csym._n._n_name[k] = 0; + + if (strlen(name) <= 8) { + strcpy(csym._n._n_name, name); + } else { + if (pCoff_str_table - Coff_str_table + strlen(name) > + MAX_STR_TABLE - 1) + tcc_error("String table too large"); + + csym._n._n_n._n_zeroes = 0; + csym._n._n_n._n_offset = + pCoff_str_table - Coff_str_table + 4; + + strcpy(pCoff_str_table, name); + pCoff_str_table += strlen(name) + 1; // skip over null + nstr++; + } + + if (p->st_info == 4) { + // put a filename symbol + csym.n_value = 33; // ????? + csym.n_scnum = N_DEBUG; + csym.n_type = 0; + csym.n_sclass = C_FILE; + csym.n_numaux = 0; + fwrite(&csym, 18, 1, f); + n++; + + } else if (p->st_info == 0x12) { + // find the function data + + for (k = 0; k < nFuncs; k++) { + if (strcmp(name, Func[k]) == 0) + break; + } + + if (k >= nFuncs) { + tcc_error("debug info can't find function: %s", name); + } + // put a Function Name + + csym.n_value = p->st_value; // physical address + csym.n_scnum = CoffTextSectionNo; + csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0); + csym.n_sclass = C_EXT; + csym.n_numaux = 1; + fwrite(&csym, 18, 1, f); + + // now put aux info + + auxfunc.tag = 0; + auxfunc.size = EndAddress[k] - p->st_value; + auxfunc.fileptr = LineNoFilePtr[k]; + auxfunc.nextsym = n + 6; // tktk + auxfunc.dummy = 0; + fwrite(&auxfunc, 18, 1, f); + + // put a .bf + + strcpy(csym._n._n_name, ".bf"); + csym.n_value = p->st_value; // physical address + csym.n_scnum = CoffTextSectionNo; + csym.n_type = 0; + csym.n_sclass = C_FCN; + csym.n_numaux = 1; + fwrite(&csym, 18, 1, f); + + // now put aux info + + auxbf.regmask = 0; + auxbf.lineno = 0; + auxbf.nentries = FuncEntries[k]; + auxbf.localframe = 0; + auxbf.nextentry = n + 6; + auxbf.dummy = 0; + fwrite(&auxbf, 18, 1, f); + + // put a .ef + + strcpy(csym._n._n_name, ".ef"); + csym.n_value = EndAddress[k]; // physical address + csym.n_scnum = CoffTextSectionNo; + csym.n_type = 0; + csym.n_sclass = C_FCN; + csym.n_numaux = 1; + fwrite(&csym, 18, 1, f); + + // now put aux info + + auxef.dummy = 0; + auxef.lineno = LastLineNo[k]; + auxef.dummy1 = 0; + auxef.dummy2 = 0; + auxef.dummy3 = 0; + auxef.dummy4 = 0; + fwrite(&auxef, 18, 1, f); + + n += 6; + + } else { + // try an put some type info + + if ((p->st_other & VT_BTYPE) == VT_DOUBLE) { + csym.n_type = T_DOUBLE; // int + csym.n_sclass = C_EXT; + } else if ((p->st_other & VT_BTYPE) == VT_FLOAT) { + csym.n_type = T_FLOAT; + csym.n_sclass = C_EXT; + } else if ((p->st_other & VT_BTYPE) == VT_INT) { + csym.n_type = T_INT; // int + csym.n_sclass = C_EXT; + } else if ((p->st_other & VT_BTYPE) == VT_SHORT) { + csym.n_type = T_SHORT; + csym.n_sclass = C_EXT; + } else if ((p->st_other & VT_BTYPE) == VT_BYTE) { + csym.n_type = T_CHAR; + csym.n_sclass = C_EXT; + } else { + csym.n_type = T_INT; // just mark as a label + csym.n_sclass = C_LABEL; + } + + + csym.n_value = p->st_value; + csym.n_scnum = 2; + csym.n_numaux = 1; + fwrite(&csym, 18, 1, f); + + auxfunc.tag = 0; + auxfunc.size = 0x20; + auxfunc.fileptr = 0; + auxfunc.nextsym = 0; + auxfunc.dummy = 0; + fwrite(&auxfunc, 18, 1, f); + n++; + n++; + + } + + p++; + } + } + + if (s1->do_debug) { + // write string table + + // first write the size + i = pCoff_str_table - Coff_str_table; + fwrite(&i, 4, 1, f); + + // then write the strings + fwrite(Coff_str_table, i, 1, f); + + tcc_free(Coff_str_table); + } + + return 0; +} + + + +// group the symbols in order of filename, func1, func2, etc +// finally global symbols + +void SortSymbolTable(void) +{ + int i, j, k, n = 0; + Elf32_Sym *p, *p2, *NewTable; + char *name, *name2; + + NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym)); + + p = (Elf32_Sym *) symtab_section->data; + + + // find a file symbol, copy it over + // then scan the whole symbol list and copy any function + // symbols that match the file association + + for (i = 0; i < nb_syms; i++) { + if (p->st_info == 4) { + name = (char *) symtab_section->link->data + p->st_name; + + // this is a file symbol, copy it over + + NewTable[n++] = *p; + + p2 = (Elf32_Sym *) symtab_section->data; + + for (j = 0; j < nb_syms; j++) { + if (p2->st_info == 0x12) { + // this is a func symbol + + name2 = + (char *) symtab_section->link->data + p2->st_name; + + // find the function data index + + for (k = 0; k < nFuncs; k++) { + if (strcmp(name2, Func[k]) == 0) + break; + } + + if (k >= nFuncs) { + tcc_error("debug (sort) info can't find function: %s", name2); + } + + if (strcmp(AssociatedFile[k], name) == 0) { + // yes they match copy it over + + NewTable[n++] = *p2; + } + } + p2++; + } + } + p++; + } + + // now all the filename and func symbols should have been copied over + // copy all the rest over (all except file and funcs) + + p = (Elf32_Sym *) symtab_section->data; + for (i = 0; i < nb_syms; i++) { + if (p->st_info != 4 && p->st_info != 0x12) { + NewTable[n++] = *p; + } + p++; + } + + if (n != nb_syms) + tcc_error("Internal Compiler error, debug info"); + + // copy it all back + + p = (Elf32_Sym *) symtab_section->data; + for (i = 0; i < nb_syms; i++) { + *p++ = NewTable[i]; + } + + tcc_free(NewTable); +} + + +int FindCoffSymbolIndex(const char *func_name) +{ + int i, n = 0; + Elf32_Sym *p; + char *name; + + p = (Elf32_Sym *) symtab_section->data; + + for (i = 0; i < nb_syms; i++) { + + name = (char *) symtab_section->link->data + p->st_name; + + if (p->st_info == 4) { + // put a filename symbol + n++; + } else if (p->st_info == 0x12) { + + if (strcmp(func_name, name) == 0) + return n; + + n += 6; + + // put a Function Name + + // now put aux info + + // put a .bf + + // now put aux info + + // put a .ef + + // now put aux info + + } else { + n += 2; + } + + p++; + } + + return n; // total number of symbols +} + +int OutputTheSection(Section * sect) +{ + const char *s = sect->name; + + if (!strcmp(s, ".text")) + return 1; + else if (!strcmp(s, ".data")) + return 1; + else + return 0; +} + +short int GetCoffFlags(const char *s) +{ + if (!strcmp(s, ".text")) + return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400; + else if (!strcmp(s, ".data")) + return STYP_DATA; + else if (!strcmp(s, ".bss")) + return STYP_BSS; + else if (!strcmp(s, ".stack")) + return STYP_BSS | STYP_ALIGN | 0x200; + else if (!strcmp(s, ".cinit")) + return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200; + else + return 0; +} + +Section *FindSection(TCCState * s1, const char *sname) +{ + Section *s; + int i; + + for (i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + + if (!strcmp(sname, s->name)) + return s; + } + + tcc_error("could not find section %s", sname); + return 0; +} + +ST_FUNC int tcc_load_coff(TCCState * s1, int fd) +{ +// tktk TokenSym *ts; + + FILE *f; + unsigned int str_size; + char *Coff_str_table, *name; + int i, k; + struct syment csym; + char name2[9]; + FILHDR file_hdr; /* FILE HEADER STRUCTURE */ + + f = fdopen(fd, "rb"); + if (!f) { + tcc_error("Unable to open .out file for input"); + } + + if (fread(&file_hdr, FILHSZ, 1, f) != 1) + tcc_error("error reading .out file for input"); + + if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1) + tcc_error("error reading .out file for input"); + + // first read the string table + + if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET)) + tcc_error("error reading .out file for input"); + + if (fread(&str_size, sizeof(int), 1, f) != 1) + tcc_error("error reading .out file for input"); + + + Coff_str_table = (char *) tcc_malloc(str_size); + + if (fread(Coff_str_table, str_size - 4, 1, f) != 1) + tcc_error("error reading .out file for input"); + + // read/process all the symbols + + // seek back to symbols + + if (fseek(f, file_hdr.f_symptr, SEEK_SET)) + tcc_error("error reading .out file for input"); + + for (i = 0; i < file_hdr.f_nsyms; i++) { + if (fread(&csym, SYMESZ, 1, f) != 1) + tcc_error("error reading .out file for input"); + + if (csym._n._n_n._n_zeroes == 0) { + name = Coff_str_table + csym._n._n_n._n_offset - 4; + } else { + name = csym._n._n_name; + + if (name[7] != 0) { + for (k = 0; k < 8; k++) + name2[k] = name[k]; + + name2[8] = 0; + + name = name2; + } + } +// if (strcmp("_DAC_Buffer",name)==0) // tktk +// name[0]=0; + + if (((csym.n_type & 0x30) == 0x20 && csym.n_sclass == 0x2) || ((csym.n_type & 0x30) == 0x30 && csym.n_sclass == 0x2) || (csym.n_type == 0x4 && csym.n_sclass == 0x2) || (csym.n_type == 0x8 && csym.n_sclass == 0x2) || // structures + (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure + (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles + (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats + { + // strip off any leading underscore (except for other main routine) + + if (name[0] == '_' && strcmp(name, "_main") != 0) + name++; + + tcc_add_symbol(s1, name, (void*)(uintptr_t)csym.n_value); + } + // skip any aux records + + if (csym.n_numaux == 1) { + if (fread(&csym, SYMESZ, 1, f) != 1) + tcc_error("error reading .out file for input"); + i++; + } + } + + return 0; +} diff --git a/05/tcc-final-old/tccelf.c b/05/tcc-final-old/tccelf.c new file mode 100644 index 0000000..70d47e1 --- /dev/null +++ b/05/tcc-final-old/tccelf.c @@ -0,0 +1,3058 @@ +/* + * ELF file handling for TCC + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +/* Define this to get some debug output during relocation processing. */ +#undef DEBUG_RELOC + +/********************************************************/ +/* global variables */ + +ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */ +ST_DATA Section *common_section; +ST_DATA Section *cur_text_section; /* current section where function code is generated */ +#ifdef CONFIG_TCC_ASM +ST_DATA Section *last_text_section; /* to handle .previous asm directive */ +#endif +#ifdef CONFIG_TCC_BCHECK +/* bound check related sections */ +ST_DATA Section *bounds_section; /* contains global data bound description */ +ST_DATA Section *lbounds_section; /* contains local data bound description */ +#endif +/* symbol sections */ +ST_DATA Section *symtab_section; +/* debug sections */ +ST_DATA Section *stab_section, *stabstr_section; + +/* XXX: avoid static variable */ +static int new_undef_sym = 0; /* Is there a new undefined sym since last new_undef_sym() */ + +/* special flag to indicate that the section should not be linked to the other ones */ +#define SHF_PRIVATE 0x80000000 +/* section is dynsymtab_section */ +#define SHF_DYNSYM 0x40000000 + +/* ------------------------------------------------------------------------- */ + +ST_FUNC void tccelf_new(TCCState *s) +{ + /* no section zero */ + dynarray_add(&s->sections, &s->nb_sections, NULL); + + /* create standard sections */ + text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR); + data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); + bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); + common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE); + common_section->sh_num = SHN_COMMON; + + /* symbols are always generated for linking stage */ + symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0, + ".strtab", + ".hashtab", SHF_PRIVATE); + s->symtab = symtab_section; + + /* private symbol table for dynamic symbols */ + s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE|SHF_DYNSYM, + ".dynstrtab", + ".dynhashtab", SHF_PRIVATE); + get_sym_attr(s, 0, 1); +} + +#ifdef CONFIG_TCC_BCHECK +ST_FUNC void tccelf_bounds_new(TCCState *s) +{ + /* create bounds sections */ + bounds_section = new_section(s, ".bounds", + SHT_PROGBITS, SHF_ALLOC); + lbounds_section = new_section(s, ".lbounds", + SHT_PROGBITS, SHF_ALLOC); +} +#endif + +ST_FUNC void tccelf_stab_new(TCCState *s) +{ + stab_section = new_section(s, ".stab", SHT_PROGBITS, 0); + stab_section->sh_entsize = sizeof(Stab_Sym); + stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0); + put_elf_str(stabstr_section, ""); + stab_section->link = stabstr_section; + /* put first entry */ + put_stabs("", 0, 0, 0, 0); +} + +static void free_section(Section *s) +{ + tcc_free(s->data); +} + +ST_FUNC void tccelf_delete(TCCState *s1) +{ + int i; + + /* free all sections */ + for(i = 1; i < s1->nb_sections; i++) + free_section(s1->sections[i]); + dynarray_reset(&s1->sections, &s1->nb_sections); + + for(i = 0; i < s1->nb_priv_sections; i++) + free_section(s1->priv_sections[i]); + dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections); + + /* free any loaded DLLs */ +#ifdef TCC_IS_NATIVE + for ( i = 0; i < s1->nb_loaded_dlls; i++) { + DLLReference *ref = s1->loaded_dlls[i]; + if ( ref->handle ) +# ifdef _WIN32 + FreeLibrary((HMODULE)ref->handle); +# else + dlclose(ref->handle); +# endif + } +#endif + /* free loaded dlls array */ + dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls); + tcc_free(s1->sym_attrs); + + symtab_section = NULL; /* for tccrun.c:rt_printline() */ +} + +/* save section data state */ +ST_FUNC void tccelf_begin_file(TCCState *s1) +{ + Section *s; int i; + for (i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + s->sh_offset = s->data_offset; + } + /* disable symbol hashing during compilation */ + s = s1->symtab, s->reloc = s->hash, s->hash = NULL; +#if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE + s1->uw_sym = 0; +#endif +} + +/* At the end of compilation, convert any UNDEF syms to global, and merge + with previously existing symbols */ +ST_FUNC void tccelf_end_file(TCCState *s1) +{ + Section *s = s1->symtab; + int first_sym, nb_syms, *tr, i; + + first_sym = s->sh_offset / sizeof (ElfSym); + nb_syms = s->data_offset / sizeof (ElfSym) - first_sym; + s->data_offset = s->sh_offset; + s->link->data_offset = s->link->sh_offset; + s->hash = s->reloc, s->reloc = NULL; + tr = tcc_mallocz(nb_syms * sizeof *tr); + + for (i = 0; i < nb_syms; ++i) { + ElfSym *sym = (ElfSym*)s->data + first_sym + i; + if (sym->st_shndx == SHN_UNDEF + && ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) + sym->st_info = ELFW(ST_INFO)(STB_GLOBAL, ELFW(ST_TYPE)(sym->st_info)); + tr[i] = set_elf_sym(s, sym->st_value, sym->st_size, sym->st_info, + sym->st_other, sym->st_shndx, s->link->data + sym->st_name); + } + /* now update relocations */ + for (i = 1; i < s1->nb_sections; i++) { + Section *sr = s1->sections[i]; + if (sr->sh_type == SHT_RELX && sr->link == s) { + ElfW_Rel *rel = (ElfW_Rel*)(sr->data + sr->sh_offset); + ElfW_Rel *rel_end = (ElfW_Rel*)(sr->data + sr->data_offset); + for (; rel < rel_end; ++rel) { + int n = ELFW(R_SYM)(rel->r_info) - first_sym; + //if (n < 0) tcc_error("internal: invalid symbol index in relocation"); + rel->r_info = ELFW(R_INFO)(tr[n], ELFW(R_TYPE)(rel->r_info)); + } + } + } + tcc_free(tr); +} + +ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags) +{ + Section *sec; + + sec = tcc_mallocz(sizeof(Section) + strlen(name)); + strcpy(sec->name, name); + sec->sh_type = sh_type; + sec->sh_flags = sh_flags; + switch(sh_type) { + case SHT_HASH: + case SHT_REL: + case SHT_RELA: + case SHT_DYNSYM: + case SHT_SYMTAB: + case SHT_DYNAMIC: + sec->sh_addralign = 4; + break; + case SHT_STRTAB: + sec->sh_addralign = 1; + break; + default: + sec->sh_addralign = PTR_SIZE; /* gcc/pcc default alignment */ + break; + } + + if (sh_flags & SHF_PRIVATE) { + dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec); + } else { + sec->sh_num = s1->nb_sections; + dynarray_add(&s1->sections, &s1->nb_sections, sec); + } + + return sec; +} + +ST_FUNC Section *new_symtab(TCCState *s1, + const char *symtab_name, int sh_type, int sh_flags, + const char *strtab_name, + const char *hash_name, int hash_sh_flags) +{ + Section *symtab, *strtab, *hash; + int *ptr, nb_buckets; + + symtab = new_section(s1, symtab_name, sh_type, sh_flags); + symtab->sh_entsize = sizeof(ElfW(Sym)); + strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags); + put_elf_str(strtab, ""); + symtab->link = strtab; + put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL); + + nb_buckets = 1; + + hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags); + hash->sh_entsize = sizeof(int); + symtab->hash = hash; + hash->link = symtab; + + ptr = section_ptr_add(hash, (2 + nb_buckets + 1) * sizeof(int)); + ptr[0] = nb_buckets; + ptr[1] = 1; + memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int)); + return symtab; +} + +/* realloc section and set its content to zero */ +ST_FUNC void section_realloc(Section *sec, unsigned long new_size) +{ + unsigned long size; + unsigned char *data; + + size = sec->data_allocated; + if (size == 0) + size = 1; + while (size < new_size) + size = size * 2; + data = tcc_realloc(sec->data, size); + memset(data + sec->data_allocated, 0, size - sec->data_allocated); + sec->data = data; + sec->data_allocated = size; +} + +/* reserve at least 'size' bytes aligned per 'align' in section + 'sec' from current offset, and return the aligned offset */ +ST_FUNC size_t section_add(Section *sec, addr_t size, int align) +{ + size_t offset, offset1; + + offset = (sec->data_offset + align - 1) & -align; + offset1 = offset + size; + if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated) + section_realloc(sec, offset1); + sec->data_offset = offset1; + if (align > sec->sh_addralign) + sec->sh_addralign = align; + return offset; +} + +/* reserve at least 'size' bytes in section 'sec' from + sec->data_offset. */ +ST_FUNC void *section_ptr_add(Section *sec, addr_t size) +{ + size_t offset = section_add(sec, size, 1); + return sec->data + offset; +} + +/* reserve at least 'size' bytes from section start */ +ST_FUNC void section_reserve(Section *sec, unsigned long size) +{ + if (size > sec->data_allocated) + section_realloc(sec, size); + if (size > sec->data_offset) + sec->data_offset = size; +} + +/* return a reference to a section, and create it if it does not + exists */ +ST_FUNC Section *find_section(TCCState *s1, const char *name) +{ + Section *sec; + int i; + for(i = 1; i < s1->nb_sections; i++) { + sec = s1->sections[i]; + if (!strcmp(name, sec->name)) + return sec; + } + /* sections are created as PROGBITS */ + return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC); +} + +/* ------------------------------------------------------------------------- */ + +ST_FUNC int put_elf_str(Section *s, const char *sym) +{ + int offset, len; + char *ptr; + + len = strlen(sym) + 1; + offset = s->data_offset; + ptr = section_ptr_add(s, len); + memmove(ptr, sym, len); + return offset; +} + +/* elf symbol hashing function */ +static unsigned long elf_hash(const unsigned char *name) +{ + unsigned long h = 0, g; + + while (*name) { + h = (h << 4) + *name++; + g = h & 0xf0000000; + if (g) + h ^= g >> 24; + h &= ~g; + } + return h; +} + +/* rebuild hash table of section s */ +/* NOTE: we do factorize the hash table code to go faster */ +static void rebuild_hash(Section *s, unsigned int nb_buckets) +{ + ElfW(Sym) *sym; + int *ptr, *hash, nb_syms, sym_index, h; + unsigned char *strtab; + + strtab = s->link->data; + nb_syms = s->data_offset / sizeof(ElfW(Sym)); + + if (!nb_buckets) + nb_buckets = ((int*)s->hash->data)[0]; + + s->hash->data_offset = 0; + ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int)); + ptr[0] = nb_buckets; + ptr[1] = nb_syms; + ptr += 2; + hash = ptr; + memset(hash, 0, (nb_buckets + 1) * sizeof(int)); + ptr += nb_buckets + 1; + + sym = (ElfW(Sym) *)s->data + 1; + for(sym_index = 1; sym_index < nb_syms; sym_index++) { + if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + h = elf_hash(strtab + sym->st_name) % nb_buckets; + *ptr = hash[h]; + hash[h] = sym_index; + } else { + *ptr = 0; + } + ptr++; + sym++; + } +} + +/* return the symbol number */ +ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, + int info, int other, int shndx, const char *name) +{ + int name_offset, sym_index; + int nbuckets, h; + ElfW(Sym) *sym; + Section *hs; + + sym = section_ptr_add(s, sizeof(ElfW(Sym))); + if (name && name[0]) + name_offset = put_elf_str(s->link, name); + else + name_offset = 0; + /* XXX: endianness */ + sym->st_name = name_offset; + sym->st_value = value; + sym->st_size = size; + sym->st_info = info; + sym->st_other = other; + sym->st_shndx = shndx; + sym_index = sym - (ElfW(Sym) *)s->data; + hs = s->hash; + if (hs) { + int *ptr, *base; + ptr = section_ptr_add(hs, sizeof(int)); + base = (int *)hs->data; + /* only add global or weak symbols. */ + if (ELFW(ST_BIND)(info) != STB_LOCAL) { + /* add another hashing entry */ + nbuckets = base[0]; + h = elf_hash((unsigned char *)s->link->data + name_offset) % nbuckets; + *ptr = base[2 + h]; + base[2 + h] = sym_index; + base[1]++; + /* we resize the hash table */ + hs->nb_hashed_syms++; + if (hs->nb_hashed_syms > 2 * nbuckets) { + rebuild_hash(s, 2 * nbuckets); + } + } else { + *ptr = 0; + base[1]++; + } + } + return sym_index; +} + +ST_FUNC int find_elf_sym(Section *s, const char *name) +{ + ElfW(Sym) *sym; + Section *hs; + int nbuckets, sym_index, h; + const char *name1; + + hs = s->hash; + if (!hs) + return 0; + nbuckets = ((int *)hs->data)[0]; + h = elf_hash((unsigned char *) name) % nbuckets; + sym_index = ((int *)hs->data)[2 + h]; + while (sym_index != 0) { + sym = &((ElfW(Sym) *)s->data)[sym_index]; + name1 = (char *) s->link->data + sym->st_name; + if (!strcmp(name, name1)) + return sym_index; + sym_index = ((int *)hs->data)[2 + nbuckets + sym_index]; + } + return 0; +} + +/* return elf symbol value, signal error if 'err' is nonzero */ +ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err) +{ + int sym_index; + ElfW(Sym) *sym; + + sym_index = find_elf_sym(s->symtab, name); + sym = &((ElfW(Sym) *)s->symtab->data)[sym_index]; + if (!sym_index || sym->st_shndx == SHN_UNDEF) { + if (err) + tcc_error("%s not defined", name); + return 0; + } + return sym->st_value; +} + +/* return elf symbol value */ +LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name) +{ + return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0); +} + +#if defined TCC_IS_NATIVE || defined TCC_TARGET_PE +/* return elf symbol value or error */ +ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name) +{ + return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1); +} +#endif + +/* add an elf symbol : check if it is already defined and patch + it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */ +ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, + int info, int other, int shndx, const char *name) +{ + ElfW(Sym) *esym; + int sym_bind, sym_index, sym_type, esym_bind; + unsigned char sym_vis, esym_vis, new_vis; + + sym_bind = ELFW(ST_BIND)(info); + sym_type = ELFW(ST_TYPE)(info); + sym_vis = ELFW(ST_VISIBILITY)(other); + + if (sym_bind != STB_LOCAL) { + /* we search global or weak symbols */ + sym_index = find_elf_sym(s, name); + if (!sym_index) + goto do_def; + esym = &((ElfW(Sym) *)s->data)[sym_index]; + if (esym->st_value == value && esym->st_size == size && esym->st_info == info + && esym->st_other == other && esym->st_shndx == shndx) + return sym_index; + if (esym->st_shndx != SHN_UNDEF) { + esym_bind = ELFW(ST_BIND)(esym->st_info); + /* propagate the most constraining visibility */ + /* STV_DEFAULT(0)st_other); + if (esym_vis == STV_DEFAULT) { + new_vis = sym_vis; + } else if (sym_vis == STV_DEFAULT) { + new_vis = esym_vis; + } else { + new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis; + } + esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) + | new_vis; + other = esym->st_other; /* in case we have to patch esym */ + if (shndx == SHN_UNDEF) { + /* ignore adding of undefined symbol if the + corresponding symbol is already defined */ + } else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) { + /* global overrides weak, so patch */ + goto do_patch; + } else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) { + /* weak is ignored if already global */ + } else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) { + /* keep first-found weak definition, ignore subsequents */ + } else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) { + /* ignore hidden symbols after */ + } else if ((esym->st_shndx == SHN_COMMON + || esym->st_shndx == bss_section->sh_num) + && (shndx < SHN_LORESERVE + && shndx != bss_section->sh_num)) { + /* data symbol gets precedence over common/bss */ + goto do_patch; + } else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) { + /* data symbol keeps precedence over common/bss */ + } else if (s->sh_flags & SHF_DYNSYM) { + /* we accept that two DLL define the same symbol */ + } else if (esym->st_other & ST_ASM_SET) { + /* If the existing symbol came from an asm .set + we can override. */ + goto do_patch; + } else { +#if 0 + printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n", + sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis); +#endif + tcc_error_noabort("'%s' defined twice", name); + } + } else { + do_patch: + esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type); + esym->st_shndx = shndx; + new_undef_sym = 1; + esym->st_value = value; + esym->st_size = size; + esym->st_other = other; + } + } else { + do_def: + sym_index = put_elf_sym(s, value, size, + ELFW(ST_INFO)(sym_bind, sym_type), other, + shndx, name); + } + return sym_index; +} + +/* put relocation */ +ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, + int type, int symbol, addr_t addend) +{ + char buf[256]; + Section *sr; + ElfW_Rel *rel; + + sr = s->reloc; + if (!sr) { + /* if no relocation section, create it */ + snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name); + /* if the symtab is allocated, then we consider the relocation + are also */ + sr = new_section(tcc_state, buf, SHT_RELX, symtab->sh_flags); + sr->sh_entsize = sizeof(ElfW_Rel); + sr->link = symtab; + sr->sh_info = s->sh_num; + s->reloc = sr; + } + rel = section_ptr_add(sr, sizeof(ElfW_Rel)); + rel->r_offset = offset; + rel->r_info = ELFW(R_INFO)(symbol, type); +#if SHT_RELX == SHT_RELA + rel->r_addend = addend; +#else + if (addend) + tcc_error("non-zero addend on REL architecture"); +#endif +} + +ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, + int type, int symbol) +{ + put_elf_reloca(symtab, s, offset, type, symbol, 0); +} + +/* Remove relocations for section S->reloc starting at oldrelocoffset + that are to the same place, retaining the last of them. As side effect + the relocations are sorted. Possibly reduces the number of relocs. */ +ST_FUNC void squeeze_multi_relocs(Section *s, size_t oldrelocoffset) +{ + Section *sr = s->reloc; + ElfW_Rel *r, *dest; + ssize_t a; + ElfW(Addr) addr; + + if (oldrelocoffset + sizeof(*r) >= sr->data_offset) + return; + /* The relocs we're dealing with are the result of initializer parsing. + So they will be mostly in order and there aren't many of them. + Secondly we need a stable sort (which qsort isn't). We use + a simple insertion sort. */ + for (a = oldrelocoffset + sizeof(*r); a < sr->data_offset; a += sizeof(*r)) { + ssize_t i = a - sizeof(*r); + addr = ((ElfW_Rel*)(sr->data + a))->r_offset; + for (; i >= (ssize_t)oldrelocoffset && + ((ElfW_Rel*)(sr->data + i))->r_offset > addr; i -= sizeof(*r)) { + ElfW_Rel tmp = *(ElfW_Rel*)(sr->data + a); + *(ElfW_Rel*)(sr->data + a) = *(ElfW_Rel*)(sr->data + i); + *(ElfW_Rel*)(sr->data + i) = tmp; + } + } + + r = (ElfW_Rel*)(sr->data + oldrelocoffset); + dest = r; + for (; r < (ElfW_Rel*)(sr->data + sr->data_offset); r++) { + if (dest->r_offset != r->r_offset) + dest++; + *dest = *r; + } + sr->data_offset = (unsigned char*)dest - sr->data + sizeof(*r); +} + +/* put stab debug information */ + +ST_FUNC void put_stabs(const char *str, int type, int other, int desc, + unsigned long value) +{ + Stab_Sym *sym; + + sym = section_ptr_add(stab_section, sizeof(Stab_Sym)); + if (str) { + sym->n_strx = put_elf_str(stabstr_section, str); + } else { + sym->n_strx = 0; + } + sym->n_type = type; + sym->n_other = other; + sym->n_desc = desc; + sym->n_value = value; +} + +ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, + unsigned long value, Section *sec, int sym_index) +{ + put_stabs(str, type, other, desc, value); + put_elf_reloc(symtab_section, stab_section, + stab_section->data_offset - sizeof(unsigned int), + R_DATA_32, sym_index); +} + +ST_FUNC void put_stabn(int type, int other, int desc, int value) +{ + put_stabs(NULL, type, other, desc, value); +} + +ST_FUNC void put_stabd(int type, int other, int desc) +{ + put_stabs(NULL, type, other, desc, 0); +} + +ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc) +{ + int n; + struct sym_attr *tab; + + if (index >= s1->nb_sym_attrs) { + if (!alloc) + return s1->sym_attrs; + /* find immediately bigger power of 2 and reallocate array */ + n = 1; + while (index >= n) + n *= 2; + tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs)); + s1->sym_attrs = tab; + memset(s1->sym_attrs + s1->nb_sym_attrs, 0, + (n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs)); + s1->nb_sym_attrs = n; + } + return &s1->sym_attrs[index]; +} + +/* Browse each elem of type in section starting at elem + using variable */ +#define for_each_elem(sec, startoff, elem, type) \ + for (elem = (type *) sec->data + startoff; \ + elem < (type *) (sec->data + sec->data_offset); elem++) + +/* In an ELF file symbol table, the local symbols must appear below + the global and weak ones. Since TCC cannot sort it while generating + the code, we must do it after. All the relocation tables are also + modified to take into account the symbol table sorting */ +static void sort_syms(TCCState *s1, Section *s) +{ + int *old_to_new_syms; + ElfW(Sym) *new_syms; + int nb_syms, i; + ElfW(Sym) *p, *q; + ElfW_Rel *rel; + Section *sr; + int type, sym_index; + + nb_syms = s->data_offset / sizeof(ElfW(Sym)); + new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym))); + old_to_new_syms = tcc_malloc(nb_syms * sizeof(int)); + + /* first pass for local symbols */ + p = (ElfW(Sym) *)s->data; + q = new_syms; + for(i = 0; i < nb_syms; i++) { + if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) { + old_to_new_syms[i] = q - new_syms; + *q++ = *p; + } + p++; + } + /* save the number of local symbols in section header */ + if( s->sh_size ) /* this 'if' makes IDA happy */ + s->sh_info = q - new_syms; + + /* then second pass for non local symbols */ + p = (ElfW(Sym) *)s->data; + for(i = 0; i < nb_syms; i++) { + if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) { + old_to_new_syms[i] = q - new_syms; + *q++ = *p; + } + p++; + } + + /* we copy the new symbols to the old */ + memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym))); + tcc_free(new_syms); + + /* now we modify all the relocations */ + for(i = 1; i < s1->nb_sections; i++) { + sr = s1->sections[i]; + if (sr->sh_type == SHT_RELX && sr->link == s) { + for_each_elem(sr, 0, rel, ElfW_Rel) { + sym_index = ELFW(R_SYM)(rel->r_info); + type = ELFW(R_TYPE)(rel->r_info); + sym_index = old_to_new_syms[sym_index]; + rel->r_info = ELFW(R_INFO)(sym_index, type); + } + } + } + + tcc_free(old_to_new_syms); +} + +/* relocate symbol table, resolve undefined symbols if do_resolve is + true and output error if undefined symbol. */ +ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) +{ + ElfW(Sym) *sym; + int sym_bind, sh_num; + const char *name; + + for_each_elem(symtab, 1, sym, ElfW(Sym)) { + sh_num = sym->st_shndx; + if (sh_num == SHN_UNDEF) { + name = (char *) s1->symtab->link->data + sym->st_name; + /* Use ld.so to resolve symbol for us (for tcc -run) */ + if (do_resolve) { +#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE + void *addr = dlsym(RTLD_DEFAULT, name); + if (addr) { + sym->st_value = (addr_t) addr; +#ifdef DEBUG_RELOC + printf ("relocate_sym: %s -> 0x%lx\n", name, sym->st_value); +#endif + goto found; + } +#endif + /* if dynamic symbol exist, it will be used in relocate_section */ + } else if (s1->dynsym && find_elf_sym(s1->dynsym, name)) + goto found; + /* XXX: _fp_hw seems to be part of the ABI, so we ignore + it */ + if (!strcmp(name, "_fp_hw")) + goto found; + /* only weak symbols are accepted to be undefined. Their + value is zero */ + sym_bind = ELFW(ST_BIND)(sym->st_info); + if (sym_bind == STB_WEAK) + sym->st_value = 0; + else + tcc_error_noabort("undefined symbol '%s'", name); + } else if (sh_num < SHN_LORESERVE) { + /* add section base */ + sym->st_value += s1->sections[sym->st_shndx]->sh_addr; + } + found: ; + } +} + +/* relocate a given section (CPU dependent) by applying the relocations + in the associated relocation section */ +ST_FUNC void relocate_section(TCCState *s1, Section *s) +{ + Section *sr = s->reloc; + ElfW_Rel *rel; + ElfW(Sym) *sym; + int type, sym_index; + unsigned char *ptr; + addr_t tgt, addr; + + relocate_init(sr); + + for_each_elem(sr, 0, rel, ElfW_Rel) { + ptr = s->data + rel->r_offset; + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + type = ELFW(R_TYPE)(rel->r_info); + tgt = sym->st_value; +#if SHT_RELX == SHT_RELA + tgt += rel->r_addend; +#endif + addr = s->sh_addr + rel->r_offset; + relocate(s1, rel, type, ptr, addr, tgt); + } + /* if the relocation is allocated, we change its symbol table */ + if (sr->sh_flags & SHF_ALLOC) + sr->link = s1->dynsym; +} + +/* relocate relocation table in 'sr' */ +static void relocate_rel(TCCState *s1, Section *sr) +{ + Section *s; + ElfW_Rel *rel; + + s = s1->sections[sr->sh_info]; + for_each_elem(sr, 0, rel, ElfW_Rel) + rel->r_offset += s->sh_addr; +} + +/* count the number of dynamic relocations so that we can reserve + their space */ +static int prepare_dynamic_rel(TCCState *s1, Section *sr) +{ + ElfW_Rel *rel; + int sym_index, type, count; + + count = 0; + for_each_elem(sr, 0, rel, ElfW_Rel) { + sym_index = ELFW(R_SYM)(rel->r_info); + type = ELFW(R_TYPE)(rel->r_info); + switch(type) { +#if defined(TCC_TARGET_I386) + case R_386_32: + if (!get_sym_attr(s1, sym_index, 0)->dyn_index + && ((ElfW(Sym)*)symtab_section->data + sym_index)->st_shndx == SHN_UNDEF) { + /* don't fixup unresolved (weak) symbols */ + rel->r_info = ELFW(R_INFO)(sym_index, R_386_RELATIVE); + break; + } +#elif defined(TCC_TARGET_X86_64) + case R_X86_64_32: + case R_X86_64_32S: + case R_X86_64_64: +#endif + count++; + break; +#if defined(TCC_TARGET_I386) + case R_386_PC32: +#elif defined(TCC_TARGET_X86_64) + case R_X86_64_PC32: +#endif + if (get_sym_attr(s1, sym_index, 0)->dyn_index) + count++; + break; + default: + break; + } + } + if (count) { + /* allocate the section */ + sr->sh_flags |= SHF_ALLOC; + sr->sh_size = count * sizeof(ElfW_Rel); + } + return count; +} + +static void build_got(TCCState *s1) +{ + /* if no got, then create it */ + s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); + s1->got->sh_entsize = 4; + set_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), + 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_"); + /* keep space for _DYNAMIC pointer and two dummy got entries */ + section_ptr_add(s1->got, 3 * PTR_SIZE); +} + +/* Create a GOT and (for function call) a PLT entry corresponding to a symbol + in s1->symtab. When creating the dynamic symbol table entry for the GOT + relocation, use 'size' and 'info' for the corresponding symbol metadata. + Returns the offset of the GOT or (if any) PLT entry. */ +static struct sym_attr * put_got_entry(TCCState *s1, int dyn_reloc_type, + unsigned long size, + int info, int sym_index) +{ + int need_plt_entry; + const char *name; + ElfW(Sym) *sym; + struct sym_attr *attr; + unsigned got_offset; + char plt_name[100]; + int len; + + need_plt_entry = (dyn_reloc_type == R_JMP_SLOT); + attr = get_sym_attr(s1, sym_index, 1); + + /* In case a function is both called and its address taken 2 GOT entries + are created, one for taking the address (GOT) and the other for the PLT + entry (PLTGOT). */ + if (need_plt_entry ? attr->plt_offset : attr->got_offset) + return attr; + + /* create the GOT entry */ + got_offset = s1->got->data_offset; + section_ptr_add(s1->got, PTR_SIZE); + + /* Create the GOT relocation that will insert the address of the object or + function of interest in the GOT entry. This is a static relocation for + memory output (dlsym will give us the address of symbols) and dynamic + relocation otherwise (executable and DLLs). The relocation should be + done lazily for GOT entry with *_JUMP_SLOT relocation type (the one + associated to a PLT entry) but is currently done at load time for an + unknown reason. */ + + sym = &((ElfW(Sym) *) symtab_section->data)[sym_index]; + name = (char *) symtab_section->link->data + sym->st_name; + + if (s1->dynsym) { + if (ELFW(ST_BIND)(sym->st_info) == STB_LOCAL) { + /* Hack alarm. We don't want to emit dynamic symbols + and symbol based relocs for STB_LOCAL symbols, but rather + want to resolve them directly. At this point the symbol + values aren't final yet, so we must defer this. We will later + have to create a RELATIVE reloc anyway, so we misuse the + relocation slot to smuggle the symbol reference until + fill_local_got_entries. Not that the sym_index is + relative to symtab_section, not s1->dynsym! Nevertheless + we use s1->dyn_sym so that if this is the first call + that got->reloc is correctly created. Also note that + RELATIVE relocs are not normally created for the .got, + so the types serves as a marker for later (and is retained + also for the final output, which is okay because then the + got is just normal data). */ + put_elf_reloc(s1->dynsym, s1->got, got_offset, R_RELATIVE, + sym_index); + } else { + if (0 == attr->dyn_index) + attr->dyn_index = set_elf_sym(s1->dynsym, sym->st_value, size, + info, 0, sym->st_shndx, name); + put_elf_reloc(s1->dynsym, s1->got, got_offset, dyn_reloc_type, + attr->dyn_index); + } + } else { + put_elf_reloc(symtab_section, s1->got, got_offset, dyn_reloc_type, + sym_index); + } + + if (need_plt_entry) { + if (!s1->plt) { + s1->plt = new_section(s1, ".plt", SHT_PROGBITS, + SHF_ALLOC | SHF_EXECINSTR); + s1->plt->sh_entsize = 4; + } + + attr->plt_offset = create_plt_entry(s1, got_offset, attr); + + /* create a symbol 'sym@plt' for the PLT jump vector */ + len = strlen(name); + if (len > sizeof plt_name - 5) + len = sizeof plt_name - 5; + memcpy(plt_name, name, len); + strcpy(plt_name + len, "@plt"); + attr->plt_sym = put_elf_sym(s1->symtab, attr->plt_offset, sym->st_size, + ELFW(ST_INFO)(STB_GLOBAL, STT_FUNC), 0, s1->plt->sh_num, plt_name); + + } else { + attr->got_offset = got_offset; + } + + return attr; +} + +/* build GOT and PLT entries */ +ST_FUNC void build_got_entries(TCCState *s1) +{ + Section *s; + ElfW_Rel *rel; + ElfW(Sym) *sym; + int i, type, gotplt_entry, reloc_type, sym_index; + struct sym_attr *attr; + + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (s->sh_type != SHT_RELX) + continue; + /* no need to handle got relocations */ + if (s->link != symtab_section) + continue; + for_each_elem(s, 0, rel, ElfW_Rel) { + type = ELFW(R_TYPE)(rel->r_info); + gotplt_entry = gotplt_entry_type(type); + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + + if (gotplt_entry == NO_GOTPLT_ENTRY) { + continue; + } + + /* Automatically create PLT/GOT [entry] if it is an undefined + reference (resolved at runtime), or the symbol is absolute, + probably created by tcc_add_symbol, and thus on 64-bit + targets might be too far from application code. */ + if (gotplt_entry == AUTO_GOTPLT_ENTRY) { + if (sym->st_shndx == SHN_UNDEF) { + ElfW(Sym) *esym; + int dynindex; + if (s1->output_type == TCC_OUTPUT_DLL && ! PCRELATIVE_DLLPLT) + continue; + /* Relocations for UNDEF symbols would normally need + to be transferred into the executable or shared object. + If that were done AUTO_GOTPLT_ENTRY wouldn't exist. + But TCC doesn't do that (at least for exes), so we + need to resolve all such relocs locally. And that + means PLT slots for functions in DLLs and COPY relocs for + data symbols. COPY relocs were generated in + bind_exe_dynsyms (and the symbol adjusted to be defined), + and for functions we were generated a dynamic symbol + of function type. */ + if (s1->dynsym) { + /* dynsym isn't set for -run :-/ */ + dynindex = get_sym_attr(s1, sym_index, 0)->dyn_index; + esym = (ElfW(Sym) *)s1->dynsym->data + dynindex; + if (dynindex + && (ELFW(ST_TYPE)(esym->st_info) == STT_FUNC + || (ELFW(ST_TYPE)(esym->st_info) == STT_NOTYPE + && ELFW(ST_TYPE)(sym->st_info) == STT_FUNC))) + goto jmp_slot; + } + } else if (!(sym->st_shndx == SHN_ABS +#ifndef TCC_TARGET_ARM + && PTR_SIZE == 8 +#endif + )) + continue; + } + +#ifdef TCC_TARGET_X86_64 + if ((type == R_X86_64_PLT32 || type == R_X86_64_PC32) && + (ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT || + ELFW(ST_BIND)(sym->st_info) == STB_LOCAL)) { + rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32); + continue; + } +#endif + if (code_reloc(type)) { + jmp_slot: + reloc_type = R_JMP_SLOT; + } else + reloc_type = R_GLOB_DAT; + + if (!s1->got) + build_got(s1); + + if (gotplt_entry == BUILD_GOT_ONLY) + continue; + + attr = put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, + sym_index); + + if (reloc_type == R_JMP_SLOT) + rel->r_info = ELFW(R_INFO)(attr->plt_sym, type); + } + } +} + +/* put dynamic tag */ +static void put_dt(Section *dynamic, int dt, addr_t val) +{ + ElfW(Dyn) *dyn; + dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn))); + dyn->d_tag = dt; + dyn->d_un.d_val = val; +} + +#ifndef TCC_TARGET_PE +static void add_init_array_defines(TCCState *s1, const char *section_name) +{ + Section *s; + long end_offset; + char sym_start[1024]; + char sym_end[1024]; + + snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1); + snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1); + + s = find_section(s1, section_name); + if (!s) { + end_offset = 0; + s = data_section; + } else { + end_offset = s->data_offset; + } + + set_elf_sym(symtab_section, + 0, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + s->sh_num, sym_start); + set_elf_sym(symtab_section, + end_offset, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + s->sh_num, sym_end); +} +#endif + +static int tcc_add_support(TCCState *s1, const char *filename) +{ + char buf[1024]; + snprintf(buf, sizeof(buf), "%s/%s", s1->tcc_lib_path, filename); + return tcc_add_file(s1, buf); +} + +ST_FUNC void tcc_add_bcheck(TCCState *s1) +{ +#ifdef CONFIG_TCC_BCHECK + addr_t *ptr; + int sym_index; + + if (0 == s1->do_bounds_check) + return; + /* XXX: add an object file to do that */ + ptr = section_ptr_add(bounds_section, sizeof(*ptr)); + *ptr = 0; + set_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + bounds_section->sh_num, "__bounds_start"); + /* pull bcheck.o from libtcc1.a */ + sym_index = set_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + SHN_UNDEF, "__bound_init"); + if (s1->output_type != TCC_OUTPUT_MEMORY) { + /* add 'call __bound_init()' in .init section */ + Section *init_section = find_section(s1, ".init"); + unsigned char *pinit = section_ptr_add(init_section, 5); + pinit[0] = 0xe8; + write32le(pinit + 1, -4); + put_elf_reloc(symtab_section, init_section, + init_section->data_offset - 4, R_386_PC32, sym_index); + /* R_386_PC32 = R_X86_64_PC32 = 2 */ + } +#endif +} + +/* add tcc runtime libraries */ +ST_FUNC void tcc_add_runtime(TCCState *s1) +{ + tcc_add_bcheck(s1); + tcc_add_pragma_libs(s1); + /* add libc */ + if (!s1->nostdlib) { + tcc_add_library_err(s1, "c"); +#ifdef TCC_LIBGCC + if (!s1->static_link) { + if (TCC_LIBGCC[0] == '/') + tcc_add_file(s1, TCC_LIBGCC); + else + tcc_add_dll(s1, TCC_LIBGCC, 0); + } +#endif + tcc_add_support(s1, TCC_LIBTCC1); + /* add crt end if not memory output */ + if (s1->output_type != TCC_OUTPUT_MEMORY) + tcc_add_crt(s1, "crtn.o"); + } +} + +/* add various standard linker symbols (must be done after the + sections are filled (for example after allocating common + symbols)) */ +static void tcc_add_linker_symbols(TCCState *s1) +{ + char buf[1024]; + int i; + Section *s; + + set_elf_sym(symtab_section, + text_section->data_offset, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + text_section->sh_num, "_etext"); + set_elf_sym(symtab_section, + data_section->data_offset, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + data_section->sh_num, "_edata"); + set_elf_sym(symtab_section, + bss_section->data_offset, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + bss_section->sh_num, "_end"); +#ifndef TCC_TARGET_PE + /* horrible new standard ldscript defines */ + add_init_array_defines(s1, ".preinit_array"); + add_init_array_defines(s1, ".init_array"); + add_init_array_defines(s1, ".fini_array"); +#endif + + /* add start and stop symbols for sections whose name can be + expressed in C */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (s->sh_type == SHT_PROGBITS && + (s->sh_flags & SHF_ALLOC)) { + const char *p; + int ch; + + /* check if section name can be expressed in C */ + p = s->name; + for(;;) { + ch = *p; + if (!ch) + break; + if (!isid(ch) && !isnum(ch)) + goto next_sec; + p++; + } + snprintf(buf, sizeof(buf), "__start_%s", s->name); + set_elf_sym(symtab_section, + 0, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + s->sh_num, buf); + snprintf(buf, sizeof(buf), "__stop_%s", s->name); + set_elf_sym(symtab_section, + s->data_offset, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + s->sh_num, buf); + } + next_sec: ; + } +} + +ST_FUNC void resolve_common_syms(TCCState *s1) +{ + ElfW(Sym) *sym; + + /* Allocate common symbols in BSS. */ + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { + if (sym->st_shndx == SHN_COMMON) { + /* symbol alignment is in st_value for SHN_COMMONs */ + sym->st_value = section_add(bss_section, sym->st_size, + sym->st_value); + sym->st_shndx = bss_section->sh_num; + } + } + + /* Now assign linker provided symbols their value. */ + tcc_add_linker_symbols(s1); +} + +static void tcc_output_binary(TCCState *s1, FILE *f, + const int *sec_order) +{ + Section *s; + int i, offset, size; + + offset = 0; + for(i=1;inb_sections;i++) { + s = s1->sections[sec_order[i]]; + if (s->sh_type != SHT_NOBITS && + (s->sh_flags & SHF_ALLOC)) { + while (offset < s->sh_offset) { + fputc(0, f); + offset++; + } + size = s->sh_size; + fwrite(s->data, 1, size, f); + offset += size; + } + } +} + +ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel) +{ + int sym_index = ELFW(R_SYM) (rel->r_info); + ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index]; + struct sym_attr *attr = get_sym_attr(s1, sym_index, 0); + unsigned offset = attr->got_offset; + + if (0 == offset) + return; + section_reserve(s1->got, offset + PTR_SIZE); +#ifdef TCC_TARGET_X86_64 + write64le(s1->got->data + offset, sym->st_value); +#else + write32le(s1->got->data + offset, sym->st_value); +#endif +} + +/* Perform relocation to GOT or PLT entries */ +ST_FUNC void fill_got(TCCState *s1) +{ + Section *s; + ElfW_Rel *rel; + int i; + + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (s->sh_type != SHT_RELX) + continue; + /* no need to handle got relocations */ + if (s->link != symtab_section) + continue; + for_each_elem(s, 0, rel, ElfW_Rel) { + switch (ELFW(R_TYPE) (rel->r_info)) { + case R_X86_64_GOT32: + case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + case R_X86_64_REX_GOTPCRELX: + case R_X86_64_PLT32: + fill_got_entry(s1, rel); + break; + } + } + } +} + +/* See put_got_entry for a description. This is the second stage + where GOT references to local defined symbols are rewritten. */ +static void fill_local_got_entries(TCCState *s1) +{ + ElfW_Rel *rel; + for_each_elem(s1->got->reloc, 0, rel, ElfW_Rel) { + if (ELFW(R_TYPE)(rel->r_info) == R_RELATIVE) { + int sym_index = ELFW(R_SYM) (rel->r_info); + ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index]; + struct sym_attr *attr = get_sym_attr(s1, sym_index, 0); + unsigned offset = attr->got_offset; + if (offset != rel->r_offset - s1->got->sh_addr) + tcc_error_noabort("huh"); + rel->r_info = ELFW(R_INFO)(0, R_RELATIVE); +#if SHT_RELX == SHT_RELA + rel->r_addend = sym->st_value; +#else + /* All our REL architectures also happen to be 32bit LE. */ + write32le(s1->got->data + offset, sym->st_value); +#endif + } + } +} + +/* Bind symbols of executable: resolve undefined symbols from exported symbols + in shared libraries and export non local defined symbols to shared libraries + if -rdynamic switch was given on command line */ +static void bind_exe_dynsyms(TCCState *s1) +{ + const char *name; + int sym_index, index; + ElfW(Sym) *sym, *esym; + int type; + + /* Resolve undefined symbols from dynamic symbols. When there is a match: + - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT + - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */ + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { + if (sym->st_shndx == SHN_UNDEF) { + name = (char *) symtab_section->link->data + sym->st_name; + sym_index = find_elf_sym(s1->dynsymtab_section, name); + if (sym_index) { + esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index]; + type = ELFW(ST_TYPE)(esym->st_info); + if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) { + /* Indirect functions shall have STT_FUNC type in executable + * dynsym section. Indeed, a dlsym call following a lazy + * resolution would pick the symbol value from the + * executable dynsym entry which would contain the address + * of the function wanted by the caller of dlsym instead of + * the address of the function that would return that + * address */ + int dynindex + = put_elf_sym(s1->dynsym, 0, esym->st_size, + ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), 0, 0, + name); + int index = sym - (ElfW(Sym) *) symtab_section->data; + get_sym_attr(s1, index, 1)->dyn_index = dynindex; + } else if (type == STT_OBJECT) { + unsigned long offset; + ElfW(Sym) *dynsym; + offset = bss_section->data_offset; + /* XXX: which alignment ? */ + offset = (offset + 16 - 1) & -16; + set_elf_sym (s1->symtab, offset, esym->st_size, + esym->st_info, 0, bss_section->sh_num, name); + index = put_elf_sym(s1->dynsym, offset, esym->st_size, + esym->st_info, 0, bss_section->sh_num, + name); + + /* Ensure R_COPY works for weak symbol aliases */ + if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) { + for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) { + if ((dynsym->st_value == esym->st_value) + && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) { + char *dynname = (char *) s1->dynsymtab_section->link->data + + dynsym->st_name; + put_elf_sym(s1->dynsym, offset, dynsym->st_size, + dynsym->st_info, 0, + bss_section->sh_num, dynname); + break; + } + } + } + + put_elf_reloc(s1->dynsym, bss_section, + offset, R_COPY, index); + offset += esym->st_size; + bss_section->data_offset = offset; + } + } else { + /* STB_WEAK undefined symbols are accepted */ + /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */ + if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK || + !strcmp(name, "_fp_hw")) { + } else { + tcc_error_noabort("undefined symbol '%s'", name); + } + } + } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + /* if -rdynamic option, then export all non local symbols */ + name = (char *) symtab_section->link->data + sym->st_name; + set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info, + 0, sym->st_shndx, name); + } + } +} + +/* Bind symbols of libraries: export all non local symbols of executable that + are referenced by shared libraries. The reason is that the dynamic loader + search symbol first in executable and then in libraries. Therefore a + reference to a symbol already defined by a library can still be resolved by + a symbol in the executable. */ +static void bind_libs_dynsyms(TCCState *s1) +{ + const char *name; + int sym_index; + ElfW(Sym) *sym, *esym; + + for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) { + name = (char *) s1->dynsymtab_section->link->data + esym->st_name; + sym_index = find_elf_sym(symtab_section, name); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + if (sym_index && sym->st_shndx != SHN_UNDEF + && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + set_elf_sym(s1->dynsym, sym->st_value, sym->st_size, + sym->st_info, 0, sym->st_shndx, name); + } else if (esym->st_shndx == SHN_UNDEF) { + /* weak symbols can stay undefined */ + if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK) + tcc_warning("undefined dynamic symbol '%s'", name); + } + } +} + +/* Export all non local symbols. This is used by shared libraries so that the + non local symbols they define can resolve a reference in another shared + library or in the executable. Correspondingly, it allows undefined local + symbols to be resolved by other shared libraries or by the executable. */ +static void export_global_syms(TCCState *s1) +{ + int dynindex, index; + const char *name; + ElfW(Sym) *sym; + + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { + if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + name = (char *) symtab_section->link->data + sym->st_name; + dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, + sym->st_info, 0, sym->st_shndx, name); + index = sym - (ElfW(Sym) *) symtab_section->data; + get_sym_attr(s1, index, 1)->dyn_index = dynindex; + } + } +} + +/* Allocate strings for section names and decide if an unallocated section + should be output. + NOTE: the strsec section comes last, so its size is also correct ! */ +static int alloc_sec_names(TCCState *s1, int file_type, Section *strsec) +{ + int i; + Section *s; + int textrel = 0; + + /* Allocate strings for section names */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + /* when generating a DLL, we include relocations but we may + patch them */ + if (file_type == TCC_OUTPUT_DLL && + s->sh_type == SHT_RELX && + !(s->sh_flags & SHF_ALLOC) && + (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) && + prepare_dynamic_rel(s1, s)) { + if (s1->sections[s->sh_info]->sh_flags & SHF_EXECINSTR) + textrel = 1; + } else if (s1->do_debug || + file_type == TCC_OUTPUT_OBJ || + (s->sh_flags & SHF_ALLOC) || + i == (s1->nb_sections - 1)) { + /* we output all sections if debug or object file */ + s->sh_size = s->data_offset; + } + if (s->sh_size || (s->sh_flags & SHF_ALLOC)) + s->sh_name = put_elf_str(strsec, s->name); + } + strsec->sh_size = strsec->data_offset; + return textrel; +} + +/* Info to be copied in dynamic section */ +struct dyn_inf { + Section *dynamic; + Section *dynstr; + unsigned long data_offset; + addr_t rel_addr; + addr_t rel_size; +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + addr_t bss_addr; + addr_t bss_size; +#endif +}; + +/* Assign sections to segments and decide how are sections laid out when loaded + in memory. This function also fills corresponding program headers. */ +static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum, + Section *interp, Section* strsec, + struct dyn_inf *dyninf, int *sec_order) +{ + int i, j, k, file_type, sh_order_index, file_offset; + unsigned long s_align; + long long tmp; + addr_t addr; + ElfW(Phdr) *ph; + Section *s; + + file_type = s1->output_type; + sh_order_index = 1; + file_offset = 0; + if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) + file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)); + s_align = ELF_PAGE_SIZE; + if (s1->section_align) + s_align = s1->section_align; + + if (phnum > 0) { + if (s1->has_text_addr) { + int a_offset, p_offset; + addr = s1->text_addr; + /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset % + ELF_PAGE_SIZE */ + a_offset = (int) (addr & (s_align - 1)); + p_offset = file_offset & (s_align - 1); + if (a_offset < p_offset) + a_offset += s_align; + file_offset += (a_offset - p_offset); + } else { + if (file_type == TCC_OUTPUT_DLL) + addr = 0; + else + addr = ELF_START_ADDR; + /* compute address after headers */ + addr += (file_offset & (s_align - 1)); + } + + ph = &phdr[0]; + /* Leave one program headers for the program interpreter and one for + the program header table itself if needed. These are done later as + they require section layout to be done first. */ + if (interp) + ph += 2; + + /* dynamic relocation table information, for .dynamic section */ + dyninf->rel_addr = dyninf->rel_size = 0; +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + dyninf->bss_addr = dyninf->bss_size = 0; +#endif + + for(j = 0; j < 2; j++) { + ph->p_type = PT_LOAD; + if (j == 0) + ph->p_flags = PF_R | PF_X; + else + ph->p_flags = PF_R | PF_W; + ph->p_align = s_align; + + /* Decide the layout of sections loaded in memory. This must + be done before program headers are filled since they contain + info about the layout. We do the following ordering: interp, + symbol tables, relocations, progbits, nobits */ + /* XXX: do faster and simpler sorting */ + for(k = 0; k < 5; k++) { + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + /* compute if section should be included */ + if (j == 0) { + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + SHF_ALLOC) + continue; + } else { + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + (SHF_ALLOC | SHF_WRITE)) + continue; + } + if (s == interp) { + if (k != 0) + continue; + } else if (s->sh_type == SHT_DYNSYM || + s->sh_type == SHT_STRTAB || + s->sh_type == SHT_HASH) { + if (k != 1) + continue; + } else if (s->sh_type == SHT_RELX) { + if (k != 2) + continue; + } else if (s->sh_type == SHT_NOBITS) { + if (k != 4) + continue; + } else { + if (k != 3) + continue; + } + sec_order[sh_order_index++] = i; + + /* section matches: we align it and add its size */ + tmp = addr; + addr = (addr + s->sh_addralign - 1) & + ~(s->sh_addralign - 1); + file_offset += (int) ( addr - tmp ); + s->sh_offset = file_offset; + s->sh_addr = addr; + + /* update program header infos */ + if (ph->p_offset == 0) { + ph->p_offset = file_offset; + ph->p_vaddr = addr; + ph->p_paddr = ph->p_vaddr; + } + /* update dynamic relocation infos */ + if (s->sh_type == SHT_RELX) { +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + if (!strcmp(strsec->data + s->sh_name, ".rel.got")) { + dyninf->rel_addr = addr; + dyninf->rel_size += s->sh_size; /* XXX only first rel. */ + } + if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) { + dyninf->bss_addr = addr; + dyninf->bss_size = s->sh_size; /* XXX only first rel. */ + } +#else + if (dyninf->rel_size == 0) + dyninf->rel_addr = addr; + dyninf->rel_size += s->sh_size; +#endif + } + addr += s->sh_size; + if (s->sh_type != SHT_NOBITS) + file_offset += s->sh_size; + } + } + if (j == 0) { + /* Make the first PT_LOAD segment include the program + headers itself (and the ELF header as well), it'll + come out with same memory use but will make various + tools like binutils strip work better. */ + ph->p_offset &= ~(ph->p_align - 1); + ph->p_vaddr &= ~(ph->p_align - 1); + ph->p_paddr &= ~(ph->p_align - 1); + } + ph->p_filesz = file_offset - ph->p_offset; + ph->p_memsz = addr - ph->p_vaddr; + ph++; + if (j == 0) { + if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) { + /* if in the middle of a page, we duplicate the page in + memory so that one copy is RX and the other is RW */ + if ((addr & (s_align - 1)) != 0) + addr += s_align; + } else { + addr = (addr + s_align - 1) & ~(s_align - 1); + file_offset = (file_offset + s_align - 1) & ~(s_align - 1); + } + } + } + } + + /* all other sections come after */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (phnum > 0 && (s->sh_flags & SHF_ALLOC)) + continue; + sec_order[sh_order_index++] = i; + + file_offset = (file_offset + s->sh_addralign - 1) & + ~(s->sh_addralign - 1); + s->sh_offset = file_offset; + if (s->sh_type != SHT_NOBITS) + file_offset += s->sh_size; + } + + return file_offset; +} + +static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, + Section *dynamic) +{ + ElfW(Phdr) *ph; + + /* if interpreter, then add corresponding program header */ + if (interp) { + ph = &phdr[0]; + + ph->p_type = PT_PHDR; + ph->p_offset = sizeof(ElfW(Ehdr)); + ph->p_filesz = ph->p_memsz = phnum * sizeof(ElfW(Phdr)); + ph->p_vaddr = interp->sh_addr - ph->p_filesz; + ph->p_paddr = ph->p_vaddr; + ph->p_flags = PF_R | PF_X; + ph->p_align = 4; /* interp->sh_addralign; */ + ph++; + + ph->p_type = PT_INTERP; + ph->p_offset = interp->sh_offset; + ph->p_vaddr = interp->sh_addr; + ph->p_paddr = ph->p_vaddr; + ph->p_filesz = interp->sh_size; + ph->p_memsz = interp->sh_size; + ph->p_flags = PF_R; + ph->p_align = interp->sh_addralign; + } + + /* if dynamic section, then add corresponding program header */ + if (dynamic) { + ph = &phdr[phnum - 1]; + + ph->p_type = PT_DYNAMIC; + ph->p_offset = dynamic->sh_offset; + ph->p_vaddr = dynamic->sh_addr; + ph->p_paddr = ph->p_vaddr; + ph->p_filesz = dynamic->sh_size; + ph->p_memsz = dynamic->sh_size; + ph->p_flags = PF_R | PF_W; + ph->p_align = dynamic->sh_addralign; + } +} + +/* Fill the dynamic section with tags describing the address and size of + sections */ +static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf) +{ + Section *dynamic = dyninf->dynamic; + + /* put dynamic section entries */ + put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr); + put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr); + put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr); + put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset); + put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym))); +#if PTR_SIZE == 8 + put_dt(dynamic, DT_RELA, dyninf->rel_addr); + put_dt(dynamic, DT_RELASZ, dyninf->rel_size); + put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel)); +#else +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr); + put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size); + put_dt(dynamic, DT_JMPREL, dyninf->rel_addr); + put_dt(dynamic, DT_PLTREL, DT_REL); + put_dt(dynamic, DT_REL, dyninf->bss_addr); + put_dt(dynamic, DT_RELSZ, dyninf->bss_size); +#else + put_dt(dynamic, DT_REL, dyninf->rel_addr); + put_dt(dynamic, DT_RELSZ, dyninf->rel_size); + put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel)); +#endif +#endif + if (s1->do_debug) + put_dt(dynamic, DT_DEBUG, 0); + put_dt(dynamic, DT_NULL, 0); +} + +/* Relocate remaining sections and symbols (that is those not related to + dynamic linking) */ +static int final_sections_reloc(TCCState *s1) +{ + int i; + Section *s; + + relocate_syms(s1, s1->symtab, 0); + + if (s1->nb_errors != 0) + return -1; + + /* relocate sections */ + /* XXX: ignore sections with allocated relocations ? */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; +#if defined(TCC_TARGET_I386) || defined(TCC_MUSL) + if (s->reloc && s != s1->got && (s->sh_flags & SHF_ALLOC)) //gr + /* On X86 gdb 7.3 works in any case but gdb 6.6 will crash if SHF_ALLOC + checking is removed */ +#else + if (s->reloc && s != s1->got) + /* On X86_64 gdb 7.3 will crash if SHF_ALLOC checking is present */ +#endif + relocate_section(s1, s); + } + + /* relocate relocation entries if the relocation tables are + allocated in the executable */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if ((s->sh_flags & SHF_ALLOC) && + s->sh_type == SHT_RELX) { + relocate_rel(s1, s); + } + } + return 0; +} + +/* Create an ELF file on disk. + This function handle ELF specific layout requirements */ +static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr, + int file_offset, int *sec_order) +{ + int i, shnum, offset, size, file_type; + Section *s; + ElfW(Ehdr) ehdr; + ElfW(Shdr) shdr, *sh; + + file_type = s1->output_type; + shnum = s1->nb_sections; + + memset(&ehdr, 0, sizeof(ehdr)); + + if (phnum > 0) { + ehdr.e_phentsize = sizeof(ElfW(Phdr)); + ehdr.e_phnum = phnum; + ehdr.e_phoff = sizeof(ElfW(Ehdr)); + } + + /* align to 4 */ + file_offset = (file_offset + 3) & -4; + + /* fill header */ + ehdr.e_ident[0] = ELFMAG0; + ehdr.e_ident[1] = ELFMAG1; + ehdr.e_ident[2] = ELFMAG2; + ehdr.e_ident[3] = ELFMAG3; + ehdr.e_ident[4] = ELFCLASSW; + ehdr.e_ident[5] = ELFDATA2LSB; + ehdr.e_ident[6] = EV_CURRENT; +#if !defined(TCC_TARGET_PE) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__)) + /* FIXME: should set only for freebsd _target_, but we exclude only PE target */ + ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; +#endif +#ifdef TCC_TARGET_ARM +#ifdef TCC_ARM_EABI + ehdr.e_ident[EI_OSABI] = 0; + ehdr.e_flags = EF_ARM_EABI_VER4; + if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL) + ehdr.e_flags |= EF_ARM_HASENTRY; + if (s1->float_abi == ARM_HARD_FLOAT) + ehdr.e_flags |= EF_ARM_VFP_FLOAT; + else + ehdr.e_flags |= EF_ARM_SOFT_FLOAT; +#else + ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM; +#endif +#endif + switch(file_type) { + default: + case TCC_OUTPUT_EXE: + ehdr.e_type = ET_EXEC; + ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1); + break; + case TCC_OUTPUT_DLL: + ehdr.e_type = ET_DYN; + ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */ + break; + case TCC_OUTPUT_OBJ: + ehdr.e_type = ET_REL; + break; + } + ehdr.e_machine = EM_TCC_TARGET; + ehdr.e_version = EV_CURRENT; + ehdr.e_shoff = file_offset; + ehdr.e_ehsize = sizeof(ElfW(Ehdr)); + ehdr.e_shentsize = sizeof(ElfW(Shdr)); + ehdr.e_shnum = shnum; + ehdr.e_shstrndx = shnum - 1; + + fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f); + fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f); + offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)); + + sort_syms(s1, symtab_section); + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[sec_order[i]]; + if (s->sh_type != SHT_NOBITS) { + while (offset < s->sh_offset) { + fputc(0, f); + offset++; + } + size = s->sh_size; + if (size) + fwrite(s->data, 1, size, f); + offset += size; + } + } + + /* output section headers */ + while (offset < ehdr.e_shoff) { + fputc(0, f); + offset++; + } + + for(i = 0; i < s1->nb_sections; i++) { + sh = &shdr; + memset(sh, 0, sizeof(ElfW(Shdr))); + s = s1->sections[i]; + if (s) { + sh->sh_name = s->sh_name; + sh->sh_type = s->sh_type; + sh->sh_flags = s->sh_flags; + sh->sh_entsize = s->sh_entsize; + sh->sh_info = s->sh_info; + if (s->link) + sh->sh_link = s->link->sh_num; + sh->sh_addralign = s->sh_addralign; + sh->sh_addr = s->sh_addr; + sh->sh_offset = s->sh_offset; + sh->sh_size = s->sh_size; + } + fwrite(sh, 1, sizeof(ElfW(Shdr)), f); + } +} + +/* Write an elf, coff or "binary" file */ +static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum, + ElfW(Phdr) *phdr, int file_offset, int *sec_order) +{ + int fd, mode, file_type; + FILE *f; + + file_type = s1->output_type; + if (file_type == TCC_OUTPUT_OBJ) + mode = 0666; + else + mode = 0777; + unlink(filename); + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); + if (fd < 0) { + tcc_error_noabort("could not write '%s'", filename); + return -1; + } + f = fdopen(fd, "wb"); + if (s1->verbose) + printf("<- %s\n", filename); + +#ifdef TCC_TARGET_COFF + if (s1->output_format == TCC_OUTPUT_FORMAT_COFF) + tcc_output_coff(s1, f); + else +#endif + if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) + tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order); + else + tcc_output_binary(s1, f, sec_order); + fclose(f); + + return 0; +} + +/* Sort section headers by assigned sh_addr, remove sections + that we aren't going to output. */ +static void tidy_section_headers(TCCState *s1, int *sec_order) +{ + int i, nnew, l, *backmap; + Section **snew, *s; + ElfW(Sym) *sym; + + snew = tcc_malloc(s1->nb_sections * sizeof(snew[0])); + backmap = tcc_malloc(s1->nb_sections * sizeof(backmap[0])); + for (i = 0, nnew = 0, l = s1->nb_sections; i < s1->nb_sections; i++) { + s = s1->sections[sec_order[i]]; + if (!i || s->sh_name) { + backmap[sec_order[i]] = nnew; + snew[nnew] = s; + ++nnew; + } else { + backmap[sec_order[i]] = 0; + snew[--l] = s; + } + } + for (i = 0; i < nnew; i++) { + s = snew[i]; + if (s) { + s->sh_num = i; + if (s->sh_type == SHT_RELX) + s->sh_info = backmap[s->sh_info]; + } + } + + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) + if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) + sym->st_shndx = backmap[sym->st_shndx]; + if( !s1->static_link ) { + for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) + if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) + sym->st_shndx = backmap[sym->st_shndx]; + } + for (i = 0; i < s1->nb_sections; i++) + sec_order[i] = i; + tcc_free(s1->sections); + s1->sections = snew; + s1->nb_sections = nnew; + tcc_free(backmap); +} + +/* Output an elf, coff or binary file */ +/* XXX: suppress unneeded sections */ +static int elf_output_file(TCCState *s1, const char *filename) +{ + int i, ret, phnum, shnum, file_type, file_offset, *sec_order; + struct dyn_inf dyninf = {0}; + ElfW(Phdr) *phdr; + ElfW(Sym) *sym; + Section *strsec, *interp, *dynamic, *dynstr; + int textrel; + + file_type = s1->output_type; + s1->nb_errors = 0; + ret = -1; + phdr = NULL; + sec_order = NULL; + interp = dynamic = dynstr = NULL; /* avoid warning */ + textrel = 0; + + if (file_type != TCC_OUTPUT_OBJ) { + /* if linking, also link in runtime libraries (libc, libgcc, etc.) */ + tcc_add_runtime(s1); + resolve_common_syms(s1); + + if (!s1->static_link) { + if (file_type == TCC_OUTPUT_EXE) { + char *ptr; + /* allow override the dynamic loader */ + const char *elfint = getenv("LD_SO"); + if (elfint == NULL) + elfint = DEFAULT_ELFINTERP(s1); + /* add interpreter section only if executable */ + interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC); + interp->sh_addralign = 1; + ptr = section_ptr_add(interp, 1 + strlen(elfint)); + strcpy(ptr, elfint); + } + + /* add dynamic symbol table */ + s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC, + ".dynstr", + ".hash", SHF_ALLOC); + dynstr = s1->dynsym->link; + + /* add dynamic section */ + dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC, + SHF_ALLOC | SHF_WRITE); + dynamic->link = dynstr; + dynamic->sh_entsize = sizeof(ElfW(Dyn)); + + build_got(s1); + + if (file_type == TCC_OUTPUT_EXE) { + bind_exe_dynsyms(s1); + if (s1->nb_errors) + goto the_end; + bind_libs_dynsyms(s1); + } else { + /* shared library case: simply export all global symbols */ + export_global_syms(s1); + } + } + build_got_entries(s1); + } + + /* we add a section for symbols */ + strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0); + put_elf_str(strsec, ""); + + /* Allocate strings for section names */ + textrel = alloc_sec_names(s1, file_type, strsec); + + if (dynamic) { + /* add a list of needed dlls */ + for(i = 0; i < s1->nb_loaded_dlls; i++) { + DLLReference *dllref = s1->loaded_dlls[i]; + if (dllref->level == 0) + put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name)); + } + + if (s1->rpath) + put_dt(dynamic, s1->enable_new_dtags ? DT_RUNPATH : DT_RPATH, + put_elf_str(dynstr, s1->rpath)); + + if (file_type == TCC_OUTPUT_DLL) { + if (s1->soname) + put_dt(dynamic, DT_SONAME, put_elf_str(dynstr, s1->soname)); + /* XXX: currently, since we do not handle PIC code, we + must relocate the readonly segments */ + if (textrel) + put_dt(dynamic, DT_TEXTREL, 0); + } + + if (s1->symbolic) + put_dt(dynamic, DT_SYMBOLIC, 0); + + dyninf.dynamic = dynamic; + dyninf.dynstr = dynstr; + /* remember offset and reserve space for 2nd call below */ + dyninf.data_offset = dynamic->data_offset; + fill_dynamic(s1, &dyninf); + dynamic->sh_size = dynamic->data_offset; + dynstr->sh_size = dynstr->data_offset; + } + + /* compute number of program headers */ + if (file_type == TCC_OUTPUT_OBJ) + phnum = 0; + else if (file_type == TCC_OUTPUT_DLL) + phnum = 3; + else if (s1->static_link) + phnum = 2; + else + phnum = 5; + + /* allocate program segment headers */ + phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr))); + + /* compute number of sections */ + shnum = s1->nb_sections; + + /* this array is used to reorder sections in the output file */ + sec_order = tcc_malloc(sizeof(int) * shnum); + sec_order[0] = 0; + + /* compute section to program header mapping */ + file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf, + sec_order); + + /* Fill remaining program header and finalize relocation related to dynamic + linking. */ + if (file_type != TCC_OUTPUT_OBJ) { + fill_unloadable_phdr(phdr, phnum, interp, dynamic); + if (dynamic) { + dynamic->data_offset = dyninf.data_offset; + fill_dynamic(s1, &dyninf); + + /* put in GOT the dynamic section address and relocate PLT */ + write32le(s1->got->data, dynamic->sh_addr); + if (file_type == TCC_OUTPUT_EXE + || (RELOCATE_DLLPLT && file_type == TCC_OUTPUT_DLL)) + relocate_plt(s1); + + /* relocate symbols in .dynsym now that final addresses are known */ + for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) { + if (sym->st_shndx != SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) { + /* do symbol relocation */ + sym->st_value += s1->sections[sym->st_shndx]->sh_addr; + } + } + } + + /* if building executable or DLL, then relocate each section + except the GOT which is already relocated */ + ret = final_sections_reloc(s1); + if (ret) + goto the_end; + tidy_section_headers(s1, sec_order); + + /* Perform relocation to GOT or PLT entries */ + if (file_type == TCC_OUTPUT_EXE && s1->static_link) + fill_got(s1); + else if (s1->got) + fill_local_got_entries(s1); + } + + /* Create the ELF file with name 'filename' */ + ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order); + s1->nb_sections = shnum; + the_end: + tcc_free(sec_order); + tcc_free(phdr); + return ret; +} + +LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename) +{ + int ret; +#ifdef TCC_TARGET_PE + if (s->output_type != TCC_OUTPUT_OBJ) { + ret = pe_output_file(s, filename); + } else +#endif + ret = elf_output_file(s, filename); + return ret; +} + +static void *load_data(int fd, unsigned long file_offset, unsigned long size) +{ + void *data; + + data = tcc_malloc(size); + lseek(fd, file_offset, SEEK_SET); + read(fd, data, size); + return data; +} + +typedef struct SectionMergeInfo { + Section *s; /* corresponding existing section */ + unsigned long offset; /* offset of the new section in the existing section */ + uint8_t new_section; /* true if section 's' was added */ + uint8_t link_once; /* true if link once section */ +} SectionMergeInfo; + +ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h) +{ + int size = read(fd, h, sizeof *h); + if (size == sizeof *h && 0 == memcmp(h, ELFMAG, 4)) { + if (h->e_type == ET_REL) + return AFF_BINTYPE_REL; + if (h->e_type == ET_DYN) + return AFF_BINTYPE_DYN; + } else if (size >= 8) { + if (0 == memcmp(h, ARMAG, 8)) + return AFF_BINTYPE_AR; +#ifdef TCC_TARGET_COFF + if (((struct filehdr*)h)->f_magic == COFF_C67_MAGIC) + return AFF_BINTYPE_C67; +#endif + } + return 0; +} + +/* load an object file and merge it with current files */ +/* XXX: handle correctly stab (debug) info */ +ST_FUNC int tcc_load_object_file(TCCState *s1, + int fd, unsigned long file_offset) +{ + ElfW(Ehdr) ehdr; + ElfW(Shdr) *shdr, *sh; + int size, i, j, offset, offseti, nb_syms, sym_index, ret, seencompressed; + unsigned char *strsec, *strtab; + int *old_to_new_syms; + char *sh_name, *name; + SectionMergeInfo *sm_table, *sm; + ElfW(Sym) *sym, *symtab; + ElfW_Rel *rel; + Section *s; + + int stab_index; + int stabstr_index; + + stab_index = stabstr_index = 0; + + lseek(fd, file_offset, SEEK_SET); + if (tcc_object_type(fd, &ehdr) != AFF_BINTYPE_REL) + goto fail1; + /* test CPU specific stuff */ + if (ehdr.e_ident[5] != ELFDATA2LSB || + ehdr.e_machine != EM_TCC_TARGET) { + fail1: + tcc_error_noabort("invalid object file"); + return -1; + } + /* read sections */ + shdr = load_data(fd, file_offset + ehdr.e_shoff, + sizeof(ElfW(Shdr)) * ehdr.e_shnum); + sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum); + + /* load section names */ + sh = &shdr[ehdr.e_shstrndx]; + strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); + + /* load symtab and strtab */ + old_to_new_syms = NULL; + symtab = NULL; + strtab = NULL; + nb_syms = 0; + seencompressed = 0; + for(i = 1; i < ehdr.e_shnum; i++) { + sh = &shdr[i]; + if (sh->sh_type == SHT_SYMTAB) { + if (symtab) { + tcc_error_noabort("object must contain only one symtab"); + fail: + ret = -1; + goto the_end; + } + nb_syms = sh->sh_size / sizeof(ElfW(Sym)); + symtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); + sm_table[i].s = symtab_section; + + /* now load strtab */ + sh = &shdr[sh->sh_link]; + strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); + } + if (sh->sh_flags & SHF_COMPRESSED) + seencompressed = 1; + } + + /* now examine each section and try to merge its content with the + ones in memory */ + for(i = 1; i < ehdr.e_shnum; i++) { + /* no need to examine section name strtab */ + if (i == ehdr.e_shstrndx) + continue; + sh = &shdr[i]; + sh_name = (char *) strsec + sh->sh_name; + /* ignore sections types we do not handle */ + if (sh->sh_type != SHT_PROGBITS && + sh->sh_type != SHT_RELX && +#ifdef TCC_ARM_EABI + sh->sh_type != SHT_ARM_EXIDX && +#endif + sh->sh_type != SHT_NOBITS && + sh->sh_type != SHT_PREINIT_ARRAY && + sh->sh_type != SHT_INIT_ARRAY && + sh->sh_type != SHT_FINI_ARRAY && + strcmp(sh_name, ".stabstr") + ) + continue; + if (seencompressed + && (!strncmp(sh_name, ".debug_", sizeof(".debug_")-1) + || (sh->sh_type == SHT_RELX + && !strncmp((char*)strsec + shdr[sh->sh_info].sh_name, + ".debug_", sizeof(".debug_")-1)))) + continue; + if (sh->sh_addralign < 1) + sh->sh_addralign = 1; + /* find corresponding section, if any */ + for(j = 1; j < s1->nb_sections;j++) { + s = s1->sections[j]; + if (!strcmp(s->name, sh_name)) { + if (!strncmp(sh_name, ".gnu.linkonce", + sizeof(".gnu.linkonce") - 1)) { + /* if a 'linkonce' section is already present, we + do not add it again. It is a little tricky as + symbols can still be defined in + it. */ + sm_table[i].link_once = 1; + goto next; + } else { + goto found; + } + } + } + /* not found: create new section */ + s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP); + /* take as much info as possible from the section. sh_link and + sh_info will be updated later */ + s->sh_addralign = sh->sh_addralign; + s->sh_entsize = sh->sh_entsize; + sm_table[i].new_section = 1; + found: + if (sh->sh_type != s->sh_type) { + tcc_error_noabort("invalid section type"); + goto fail; + } + + /* align start of section */ + offset = s->data_offset; + + if (0 == strcmp(sh_name, ".stab")) { + stab_index = i; + goto no_align; + } + if (0 == strcmp(sh_name, ".stabstr")) { + stabstr_index = i; + goto no_align; + } + + size = sh->sh_addralign - 1; + offset = (offset + size) & ~size; + if (sh->sh_addralign > s->sh_addralign) + s->sh_addralign = sh->sh_addralign; + s->data_offset = offset; + no_align: + sm_table[i].offset = offset; + sm_table[i].s = s; + /* concatenate sections */ + size = sh->sh_size; + if (sh->sh_type != SHT_NOBITS) { + unsigned char *ptr; + lseek(fd, file_offset + sh->sh_offset, SEEK_SET); + ptr = section_ptr_add(s, size); + read(fd, ptr, size); + } else { + s->data_offset += size; + } + next: ; + } + + /* gr relocate stab strings */ + if (stab_index && stabstr_index) { + Stab_Sym *a, *b; + unsigned o; + s = sm_table[stab_index].s; + a = (Stab_Sym *)(s->data + sm_table[stab_index].offset); + b = (Stab_Sym *)(s->data + s->data_offset); + o = sm_table[stabstr_index].offset; + while (a < b) + a->n_strx += o, a++; + } + + /* second short pass to update sh_link and sh_info fields of new + sections */ + for(i = 1; i < ehdr.e_shnum; i++) { + s = sm_table[i].s; + if (!s || !sm_table[i].new_section) + continue; + sh = &shdr[i]; + if (sh->sh_link > 0) + s->link = sm_table[sh->sh_link].s; + if (sh->sh_type == SHT_RELX) { + s->sh_info = sm_table[sh->sh_info].s->sh_num; + /* update backward link */ + s1->sections[s->sh_info]->reloc = s; + } + } + sm = sm_table; + + /* resolve symbols */ + old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int)); + + sym = symtab + 1; + for(i = 1; i < nb_syms; i++, sym++) { + if (sym->st_shndx != SHN_UNDEF && + sym->st_shndx < SHN_LORESERVE) { + sm = &sm_table[sym->st_shndx]; + if (sm->link_once) { + /* if a symbol is in a link once section, we use the + already defined symbol. It is very important to get + correct relocations */ + if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + name = (char *) strtab + sym->st_name; + sym_index = find_elf_sym(symtab_section, name); + if (sym_index) + old_to_new_syms[i] = sym_index; + } + continue; + } + /* if no corresponding section added, no need to add symbol */ + if (!sm->s) + continue; + /* convert section number */ + sym->st_shndx = sm->s->sh_num; + /* offset value */ + sym->st_value += sm->offset; + } + /* add symbol */ + name = (char *) strtab + sym->st_name; + sym_index = set_elf_sym(symtab_section, sym->st_value, sym->st_size, + sym->st_info, sym->st_other, + sym->st_shndx, name); + old_to_new_syms[i] = sym_index; + } + + /* third pass to patch relocation entries */ + for(i = 1; i < ehdr.e_shnum; i++) { + s = sm_table[i].s; + if (!s) + continue; + sh = &shdr[i]; + offset = sm_table[i].offset; + switch(s->sh_type) { + case SHT_RELX: + /* take relocation offset information */ + offseti = sm_table[sh->sh_info].offset; + for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) { + int type; + unsigned sym_index; + /* convert symbol index */ + type = ELFW(R_TYPE)(rel->r_info); + sym_index = ELFW(R_SYM)(rel->r_info); + /* NOTE: only one symtab assumed */ + if (sym_index >= nb_syms) + goto invalid_reloc; + sym_index = old_to_new_syms[sym_index]; + /* ignore link_once in rel section. */ + if (!sym_index && !sm->link_once +#ifdef TCC_TARGET_ARM + && type != R_ARM_V4BX +#endif + ) { + invalid_reloc: + tcc_error_noabort("Invalid relocation entry [%2d] '%s' @ %.8x", + i, strsec + sh->sh_name, rel->r_offset); + goto fail; + } + rel->r_info = ELFW(R_INFO)(sym_index, type); + /* offset the relocation offset */ + rel->r_offset += offseti; +#ifdef TCC_TARGET_ARM + /* Jumps and branches from a Thumb code to a PLT entry need + special handling since PLT entries are ARM code. + Unconditional bl instructions referencing PLT entries are + handled by converting these instructions into blx + instructions. Other case of instructions referencing a PLT + entry require to add a Thumb stub before the PLT entry to + switch to ARM mode. We set bit plt_thumb_stub of the + attribute of a symbol to indicate such a case. */ + if (type == R_ARM_THM_JUMP24) + get_sym_attr(s1, sym_index, 1)->plt_thumb_stub = 1; +#endif + } + break; + default: + break; + } + } + + ret = 0; + the_end: + tcc_free(symtab); + tcc_free(strtab); + tcc_free(old_to_new_syms); + tcc_free(sm_table); + tcc_free(strsec); + tcc_free(shdr); + return ret; +} + +typedef struct ArchiveHeader { + char ar_name[16]; /* name of this member */ + char ar_date[12]; /* file mtime */ + char ar_uid[6]; /* owner uid; printed as decimal */ + char ar_gid[6]; /* owner gid; printed as decimal */ + char ar_mode[8]; /* file mode, printed as octal */ + char ar_size[10]; /* file size, printed as decimal */ + char ar_fmag[2]; /* should contain ARFMAG */ +} ArchiveHeader; + +static int get_be32(const uint8_t *b) +{ + return b[3] | (b[2] << 8) | (b[1] << 16) | (b[0] << 24); +} + +static long get_be64(const uint8_t *b) +{ + long long ret = get_be32(b); + ret = (ret << 32) | (unsigned)get_be32(b+4); + return (long)ret; +} + +/* load only the objects which resolve undefined symbols */ +static int tcc_load_alacarte(TCCState *s1, int fd, int size, int entrysize) +{ + long i, bound, nsyms, sym_index, off, ret; + uint8_t *data; + const char *ar_names, *p; + const uint8_t *ar_index; + ElfW(Sym) *sym; + + data = tcc_malloc(size); + if (read(fd, data, size) != size) + goto fail; + nsyms = entrysize == 4 ? get_be32(data) : get_be64(data); + ar_index = data + entrysize; + ar_names = (char *) ar_index + nsyms * entrysize; + + do { + bound = 0; + for(p = ar_names, i = 0; i < nsyms; i++, p += strlen(p)+1) { + sym_index = find_elf_sym(symtab_section, p); + if(sym_index) { + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + if(sym->st_shndx == SHN_UNDEF) { + off = (entrysize == 4 + ? get_be32(ar_index + i * 4) + : get_be64(ar_index + i * 8)) + + sizeof(ArchiveHeader); + ++bound; + if(tcc_load_object_file(s1, fd, off) < 0) { + fail: + ret = -1; + goto the_end; + } + } + } + } + } while(bound); + ret = 0; + the_end: + tcc_free(data); + return ret; +} + +/* load a '.a' file */ +ST_FUNC int tcc_load_archive(TCCState *s1, int fd) +{ + ArchiveHeader hdr; + char ar_size[11]; + char ar_name[17]; + char magic[8]; + int size, len, i; + unsigned long file_offset; + + /* skip magic which was already checked */ + read(fd, magic, sizeof(magic)); + + for(;;) { + len = read(fd, &hdr, sizeof(hdr)); + if (len == 0) + break; + if (len != sizeof(hdr)) { + tcc_error_noabort("invalid archive"); + return -1; + } + memcpy(ar_size, hdr.ar_size, sizeof(hdr.ar_size)); + ar_size[sizeof(hdr.ar_size)] = '\0'; + size = strtol(ar_size, NULL, 0); + memcpy(ar_name, hdr.ar_name, sizeof(hdr.ar_name)); + for(i = sizeof(hdr.ar_name) - 1; i >= 0; i--) { + if (ar_name[i] != ' ') + break; + } + ar_name[i + 1] = '\0'; + file_offset = lseek(fd, 0, SEEK_CUR); + /* align to even */ + size = (size + 1) & ~1; + if (!strcmp(ar_name, "/")) { + /* coff symbol table : we handle it */ + if(s1->alacarte_link) + return tcc_load_alacarte(s1, fd, size, 4); + } else if (!strcmp(ar_name, "/SYM64/")) { + if(s1->alacarte_link) + return tcc_load_alacarte(s1, fd, size, 8); + } else { + ElfW(Ehdr) ehdr; + if (tcc_object_type(fd, &ehdr) == AFF_BINTYPE_REL) { + if (tcc_load_object_file(s1, fd, file_offset) < 0) + return -1; + } + } + lseek(fd, file_offset + size, SEEK_SET); + } + return 0; +} + +#ifndef TCC_TARGET_PE +/* load a DLL and all referenced DLLs. 'level = 0' means that the DLL + is referenced by the user (so it should be added as DT_NEEDED in + the generated ELF file) */ +ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) +{ + ElfW(Ehdr) ehdr; + ElfW(Shdr) *shdr, *sh, *sh1; + int i, j, nb_syms, nb_dts, sym_bind, ret; + ElfW(Sym) *sym, *dynsym; + ElfW(Dyn) *dt, *dynamic; + unsigned char *dynstr; + const char *name, *soname; + DLLReference *dllref; + + read(fd, &ehdr, sizeof(ehdr)); + + /* test CPU specific stuff */ + if (ehdr.e_ident[5] != ELFDATA2LSB || + ehdr.e_machine != EM_TCC_TARGET) { + tcc_error_noabort("bad architecture"); + return -1; + } + + /* read sections */ + shdr = load_data(fd, ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum); + + /* load dynamic section and dynamic symbols */ + nb_syms = 0; + nb_dts = 0; + dynamic = NULL; + dynsym = NULL; /* avoid warning */ + dynstr = NULL; /* avoid warning */ + for(i = 0, sh = shdr; i < ehdr.e_shnum; i++, sh++) { + switch(sh->sh_type) { + case SHT_DYNAMIC: + nb_dts = sh->sh_size / sizeof(ElfW(Dyn)); + dynamic = load_data(fd, sh->sh_offset, sh->sh_size); + break; + case SHT_DYNSYM: + nb_syms = sh->sh_size / sizeof(ElfW(Sym)); + dynsym = load_data(fd, sh->sh_offset, sh->sh_size); + sh1 = &shdr[sh->sh_link]; + dynstr = load_data(fd, sh1->sh_offset, sh1->sh_size); + break; + default: + break; + } + } + + /* compute the real library name */ + soname = tcc_basename(filename); + + for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) { + if (dt->d_tag == DT_SONAME) { + soname = (char *) dynstr + dt->d_un.d_val; + } + } + + /* if the dll is already loaded, do not load it */ + for(i = 0; i < s1->nb_loaded_dlls; i++) { + dllref = s1->loaded_dlls[i]; + if (!strcmp(soname, dllref->name)) { + /* but update level if needed */ + if (level < dllref->level) + dllref->level = level; + ret = 0; + goto the_end; + } + } + + /* add the dll and its level */ + dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname)); + dllref->level = level; + strcpy(dllref->name, soname); + dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref); + + /* add dynamic symbols in dynsym_section */ + for(i = 1, sym = dynsym + 1; i < nb_syms; i++, sym++) { + sym_bind = ELFW(ST_BIND)(sym->st_info); + if (sym_bind == STB_LOCAL) + continue; + name = (char *) dynstr + sym->st_name; + set_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size, + sym->st_info, sym->st_other, sym->st_shndx, name); + } + + /* load all referenced DLLs */ + for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) { + switch(dt->d_tag) { + case DT_NEEDED: + name = (char *) dynstr + dt->d_un.d_val; + for(j = 0; j < s1->nb_loaded_dlls; j++) { + dllref = s1->loaded_dlls[j]; + if (!strcmp(name, dllref->name)) + goto already_loaded; + } + if (tcc_add_dll(s1, name, AFF_REFERENCED_DLL) < 0) { + tcc_error_noabort("referenced dll '%s' not found", name); + ret = -1; + goto the_end; + } + already_loaded: + break; + } + } + ret = 0; + the_end: + tcc_free(dynstr); + tcc_free(dynsym); + tcc_free(dynamic); + tcc_free(shdr); + return ret; +} + +#define LD_TOK_NAME 256 +#define LD_TOK_EOF (-1) + +/* return next ld script token */ +static int ld_next(TCCState *s1, char *name, int name_size) +{ + int c; + char *q; + + redo: + switch(ch) { + case ' ': + case '\t': + case '\f': + case '\v': + case '\r': + case '\n': + inp(); + goto redo; + case '/': + minp(); + if (ch == '*') { + file->buf_ptr = parse_comment(file->buf_ptr); + ch = file->buf_ptr[0]; + goto redo; + } else { + q = name; + *q++ = '/'; + goto parse_name; + } + break; + case '\\': + ch = handle_eob(); + if (ch != '\\') + goto redo; + /* fall through */ + /* case 'a' ... 'z': */ + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + /* case 'A' ... 'z': */ + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + case '_': + case '.': + case '$': + case '~': + q = name; + parse_name: + for(;;) { + if (!((ch >= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') || + (ch >= '0' && ch <= '9') || + strchr("/.-_+=$:\\,~", ch))) + break; + if ((q - name) < name_size - 1) { + *q++ = ch; + } + minp(); + } + *q = '\0'; + c = LD_TOK_NAME; + break; + case CH_EOF: + c = LD_TOK_EOF; + break; + default: + c = ch; + inp(); + break; + } + return c; +} + +static int ld_add_file(TCCState *s1, const char filename[]) +{ + if (filename[0] == '/') { + if (CONFIG_SYSROOT[0] == '\0' + && tcc_add_file_internal(s1, filename, AFF_TYPE_BIN) == 0) + return 0; + filename = tcc_basename(filename); + } + return tcc_add_dll(s1, filename, 0); +} + +static inline int new_undef_syms(void) +{ + int ret = 0; + ret = new_undef_sym; + new_undef_sym = 0; + return ret; +} + +static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed) +{ + char filename[1024], libname[1024]; + int t, group, nblibs = 0, ret = 0; + char **libs = NULL; + + group = !strcmp(cmd, "GROUP"); + if (!as_needed) + new_undef_syms(); + t = ld_next(s1, filename, sizeof(filename)); + if (t != '(') + expect("("); + t = ld_next(s1, filename, sizeof(filename)); + for(;;) { + libname[0] = '\0'; + if (t == LD_TOK_EOF) { + tcc_error_noabort("unexpected end of file"); + ret = -1; + goto lib_parse_error; + } else if (t == ')') { + break; + } else if (t == '-') { + t = ld_next(s1, filename, sizeof(filename)); + if ((t != LD_TOK_NAME) || (filename[0] != 'l')) { + tcc_error_noabort("library name expected"); + ret = -1; + goto lib_parse_error; + } + pstrcpy(libname, sizeof libname, &filename[1]); + if (s1->static_link) { + snprintf(filename, sizeof filename, "lib%s.a", libname); + } else { + snprintf(filename, sizeof filename, "lib%s.so", libname); + } + } else if (t != LD_TOK_NAME) { + tcc_error_noabort("filename expected"); + ret = -1; + goto lib_parse_error; + } + if (!strcmp(filename, "AS_NEEDED")) { + ret = ld_add_file_list(s1, cmd, 1); + if (ret) + goto lib_parse_error; + } else { + /* TODO: Implement AS_NEEDED support. Ignore it for now */ + if (!as_needed) { + ret = ld_add_file(s1, filename); + if (ret) + goto lib_parse_error; + if (group) { + /* Add the filename *and* the libname to avoid future conversions */ + dynarray_add(&libs, &nblibs, tcc_strdup(filename)); + if (libname[0] != '\0') + dynarray_add(&libs, &nblibs, tcc_strdup(libname)); + } + } + } + t = ld_next(s1, filename, sizeof(filename)); + if (t == ',') { + t = ld_next(s1, filename, sizeof(filename)); + } + } + if (group && !as_needed) { + while (new_undef_syms()) { + int i; + + for (i = 0; i < nblibs; i ++) + ld_add_file(s1, libs[i]); + } + } +lib_parse_error: + dynarray_reset(&libs, &nblibs); + return ret; +} + +/* interpret a subset of GNU ldscripts to handle the dummy libc.so + files */ +ST_FUNC int tcc_load_ldscript(TCCState *s1) +{ + char cmd[64]; + char filename[1024]; + int t, ret; + + ch = handle_eob(); + for(;;) { + t = ld_next(s1, cmd, sizeof(cmd)); + if (t == LD_TOK_EOF) + return 0; + else if (t != LD_TOK_NAME) + return -1; + if (!strcmp(cmd, "INPUT") || + !strcmp(cmd, "GROUP")) { + ret = ld_add_file_list(s1, cmd, 0); + if (ret) + return ret; + } else if (!strcmp(cmd, "OUTPUT_FORMAT") || + !strcmp(cmd, "TARGET")) { + /* ignore some commands */ + t = ld_next(s1, cmd, sizeof(cmd)); + if (t != '(') + expect("("); + for(;;) { + t = ld_next(s1, filename, sizeof(filename)); + if (t == LD_TOK_EOF) { + tcc_error_noabort("unexpected end of file"); + return -1; + } else if (t == ')') { + break; + } + } + } else { + return -1; + } + } + return 0; +} +#endif /* !TCC_TARGET_PE */ diff --git a/05/tcc-final-old/tccgen.c b/05/tcc-final-old/tccgen.c new file mode 100644 index 0000000..e0b744e --- /dev/null +++ b/05/tcc-final-old/tccgen.c @@ -0,0 +1,7386 @@ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +/********************************************************/ +/* global variables */ + +/* loc : local variable index + ind : output code index + rsym: return symbol + anon_sym: anonymous symbol index +*/ +ST_DATA int rsym, anon_sym, ind, loc; + +ST_DATA Sym *sym_free_first; +ST_DATA void **sym_pools; +ST_DATA int nb_sym_pools; + +ST_DATA Sym *global_stack; +ST_DATA Sym *local_stack; +ST_DATA Sym *define_stack; +ST_DATA Sym *global_label_stack; +ST_DATA Sym *local_label_stack; +static int local_scope; +static int in_sizeof; +static int section_sym; + +ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */ +ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */ +ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */ + +ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop, *pvtop; + +ST_DATA int const_wanted; /* true if constant wanted */ +ST_DATA int nocode_wanted; /* no code generation wanted */ +#define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */ +#define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */ +ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */ +ST_DATA CType func_vt; /* current function return type (used by return instruction) */ +ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */ +ST_DATA int func_vc; +ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ +ST_DATA const char *funcname; +ST_DATA int g_debug; + +ST_DATA CType char_pointer_type, func_old_type, int_type, size_type, ptrdiff_type; + +ST_DATA struct switch_t { + struct case_t { + int64_t v1, v2; + int sym; + } **p; int n; /* list of case ranges */ + int def_sym; /* default symbol */ +} *cur_switch; /* current switch */ + +/* ------------------------------------------------------------------------- */ + +static void gen_cast(CType *type); +static void gen_cast_s(int t); +static inline CType *pointed_type(CType *type); +static int is_compatible_types(CType *type1, CType *type2); +static int parse_btype(CType *type, AttributeDef *ad); +static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td); +static void parse_expr_type(CType *type); +static void init_putv(CType *type, Section *sec, unsigned long c); +static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only); +static void block(int *bsym, int *csym, int is_expr); +static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope); +static void decl(int l); +static int decl0(int l, int is_for_loop_init, Sym *); +static void expr_eq(void); +static void vla_runtime_type_size(CType *type, int *a); +static void vla_sp_restore(void); +static void vla_sp_restore_root(void); +static int is_compatible_unqualified_types(CType *type1, CType *type2); +static inline int64_t expr_const64(void); +static void vpush64(int ty, unsigned long long v); +static void vpush(CType *type); +static int gvtst(int inv, int t); +static void gen_inline_functions(TCCState *s); +static void skip_or_save_block(TokenString **str); +static void gv_dup(void); + +ST_INLN int is_float(int t) +{ + int bt; + bt = t & VT_BTYPE; + return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT; +} + +/* we use our own 'finite' function to avoid potential problems with + non standard math libs */ +/* XXX: endianness dependent */ +ST_FUNC int ieee_finite(double d) +{ + int p[4]; + memcpy(p, &d, sizeof(double)); + return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31; +} + +/* compiling intel long double natively */ +#if (defined __i386__ || defined __x86_64__) \ + && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) +# define TCC_IS_NATIVE_387 +#endif + +ST_FUNC void test_lvalue(void) +{ + if (!(vtop->r & VT_LVAL)) + expect("lvalue"); +} + +ST_FUNC void check_vstack(void) +{ + if (pvtop != vtop) + tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop); +} + +/* ------------------------------------------------------------------------- */ +/* vstack debugging aid */ + +#if 0 +void pv (const char *lbl, int a, int b) +{ + int i; + for (i = a; i < a + b; ++i) { + SValue *p = &vtop[-i]; + printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n", + lbl, i, p->type.t, p->r, p->r2, (int)p->c.i); + } +} +#endif + +/* ------------------------------------------------------------------------- */ +/* start of translation unit info */ +ST_FUNC void tcc_debug_start(TCCState *s1) +{ + if (s1->do_debug) { + char buf[512]; + + /* file info: full path + filename */ + section_sym = put_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0, + text_section->sh_num, NULL); + getcwd(buf, sizeof(buf)); +#ifdef _WIN32 + normalize_slashes(buf); +#endif + pstrcat(buf, sizeof(buf), "/"); + put_stabs_r(buf, N_SO, 0, 0, + text_section->data_offset, text_section, section_sym); + put_stabs_r(file->filename, N_SO, 0, 0, + text_section->data_offset, text_section, section_sym); + last_ind = 0; + last_line_num = 0; + } + + /* an elf symbol of type STT_FILE must be put so that STB_LOCAL + symbols can be safely used */ + put_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0, + SHN_ABS, file->filename); +} + +/* put end of translation unit info */ +ST_FUNC void tcc_debug_end(TCCState *s1) +{ + if (!s1->do_debug) + return; + put_stabs_r(NULL, N_SO, 0, 0, + text_section->data_offset, text_section, section_sym); + +} + +/* generate line number info */ +ST_FUNC void tcc_debug_line(TCCState *s1) +{ + if (!s1->do_debug) + return; + if ((last_line_num != file->line_num || last_ind != ind)) { + put_stabn(N_SLINE, 0, file->line_num, ind - func_ind); + last_ind = ind; + last_line_num = file->line_num; + } +} + +/* put function symbol */ +ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym) +{ + char buf[512]; + + if (!s1->do_debug) + return; + + /* stabs info */ + /* XXX: we put here a dummy type */ + snprintf(buf, sizeof(buf), "%s:%c1", + funcname, sym->type.t & VT_STATIC ? 'f' : 'F'); + put_stabs_r(buf, N_FUN, 0, file->line_num, 0, + cur_text_section, sym->c); + /* //gr gdb wants a line at the function */ + put_stabn(N_SLINE, 0, file->line_num, 0); + + last_ind = 0; + last_line_num = 0; +} + +/* put function size */ +ST_FUNC void tcc_debug_funcend(TCCState *s1, int size) +{ + if (!s1->do_debug) + return; + put_stabn(N_FUN, 0, 0, size); +} + +/* ------------------------------------------------------------------------- */ +ST_FUNC int tccgen_compile(TCCState *s1) +{ + cur_text_section = NULL; + funcname = ""; + anon_sym = SYM_FIRST_ANOM; + section_sym = 0; + const_wanted = 0; + nocode_wanted = 0x80000000; + + /* define some often used types */ + int_type.t = VT_INT; + char_pointer_type.t = VT_BYTE; + mk_pointer(&char_pointer_type); +#if PTR_SIZE == 4 + size_type.t = VT_INT | VT_UNSIGNED; + ptrdiff_type.t = VT_INT; +#elif LONG_SIZE == 4 + size_type.t = VT_LLONG | VT_UNSIGNED; + ptrdiff_type.t = VT_LLONG; +#else + size_type.t = VT_LONG | VT_LLONG | VT_UNSIGNED; + ptrdiff_type.t = VT_LONG | VT_LLONG; +#endif + func_old_type.t = VT_FUNC; + func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0); + func_old_type.ref->f.func_call = FUNC_CDECL; + func_old_type.ref->f.func_type = FUNC_OLD; + + tcc_debug_start(s1); + +#ifdef TCC_TARGET_ARM + arm_init(s1); +#endif + +#ifdef INC_DEBUG + printf("%s: **** new file\n", file->filename); +#endif + + parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR; + next(); + decl(VT_CONST); + gen_inline_functions(s1); + check_vstack(); + /* end of translation unit info */ + tcc_debug_end(s1); + return 0; +} + +/* ------------------------------------------------------------------------- */ +ST_FUNC ElfSym *elfsym(Sym *s) +{ + if (!s || !s->c) + return NULL; + return &((ElfSym *)symtab_section->data)[s->c]; +} + +/* apply storage attributes to Elf symbol */ +ST_FUNC void update_storage(Sym *sym) +{ + ElfSym *esym; + int sym_bind, old_sym_bind; + + esym = elfsym(sym); + if (!esym) + return; + + if (sym->a.visibility) + esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) + | sym->a.visibility; + + if (sym->type.t & VT_STATIC) + sym_bind = STB_LOCAL; + else if (sym->a.weak) + sym_bind = STB_WEAK; + else + sym_bind = STB_GLOBAL; + old_sym_bind = ELFW(ST_BIND)(esym->st_info); + if (sym_bind != old_sym_bind) { + esym->st_info = ELFW(ST_INFO)(sym_bind, ELFW(ST_TYPE)(esym->st_info)); + } + +#ifdef TCC_TARGET_PE + if (sym->a.dllimport) + esym->st_other |= ST_PE_IMPORT; + if (sym->a.dllexport) + esym->st_other |= ST_PE_EXPORT; +#endif + +#if 0 + printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n", + get_tok_str(sym->v, NULL), + sym_bind == STB_WEAK ? 'w' : sym_bind == STB_LOCAL ? 'l' : 'g', + sym->a.visibility, + sym->a.dllexport, + sym->a.dllimport + ); +#endif +} + +/* ------------------------------------------------------------------------- */ +/* update sym->c so that it points to an external symbol in section + 'section' with value 'value' */ + +ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, + addr_t value, unsigned long size, + int can_add_underscore) +{ + int sym_type, sym_bind, info, other, t; + ElfSym *esym; + const char *name; + char buf1[256]; +#ifdef CONFIG_TCC_BCHECK + char buf[32]; +#endif + + if (!sym->c) { + name = get_tok_str(sym->v, NULL); +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) { + /* XXX: avoid doing that for statics ? */ + /* if bound checking is activated, we change some function + names by adding the "__bound" prefix */ + switch(sym->v) { +#ifdef TCC_TARGET_PE + /* XXX: we rely only on malloc hooks */ + case TOK_malloc: + case TOK_free: + case TOK_realloc: + case TOK_memalign: + case TOK_calloc: +#endif + case TOK_memcpy: + case TOK_memmove: + case TOK_memset: + case TOK_strlen: + case TOK_strcpy: + case TOK_alloca: + strcpy(buf, "__bound_"); + strcat(buf, name); + name = buf; + break; + } + } +#endif + t = sym->type.t; + if ((t & VT_BTYPE) == VT_FUNC) { + sym_type = STT_FUNC; + } else if ((t & VT_BTYPE) == VT_VOID) { + sym_type = STT_NOTYPE; + } else { + sym_type = STT_OBJECT; + } + if (t & VT_STATIC) + sym_bind = STB_LOCAL; + else + sym_bind = STB_GLOBAL; + other = 0; +#ifdef TCC_TARGET_PE + if (sym_type == STT_FUNC && sym->type.ref) { + Sym *ref = sym->type.ref; + if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) { + sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE); + name = buf1; + other |= ST_PE_STDCALL; + can_add_underscore = 0; + } + } +#endif + if (tcc_state->leading_underscore && can_add_underscore) { + buf1[0] = '_'; + pstrcpy(buf1 + 1, sizeof(buf1) - 1, name); + name = buf1; + } + if (sym->asm_label) + name = get_tok_str(sym->asm_label, NULL); + info = ELFW(ST_INFO)(sym_bind, sym_type); + sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name); + } else { + esym = elfsym(sym); + esym->st_value = value; + esym->st_size = size; + esym->st_shndx = sh_num; + } + update_storage(sym); +} + +ST_FUNC void put_extern_sym(Sym *sym, Section *section, + addr_t value, unsigned long size) +{ + int sh_num = section ? section->sh_num : SHN_UNDEF; + put_extern_sym2(sym, sh_num, value, size, 1); +} + +/* add a new relocation entry to symbol 'sym' in section 's' */ +ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, + addr_t addend) +{ + int c = 0; + + if (nocode_wanted && s == cur_text_section) + return; + + if (sym) { + if (0 == sym->c) + put_extern_sym(sym, NULL, 0, 0); + c = sym->c; + } + + /* now we can add ELF relocation info */ + put_elf_reloca(symtab_section, s, offset, type, c, addend); +} + +#if PTR_SIZE == 4 +ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type) +{ + greloca(s, sym, offset, type, 0); +} +#endif + +/* ------------------------------------------------------------------------- */ +/* symbol allocator */ +static Sym *__sym_malloc(void) +{ + Sym *sym_pool, *sym, *last_sym; + int i; + + sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym)); + dynarray_add(&sym_pools, &nb_sym_pools, sym_pool); + + last_sym = sym_free_first; + sym = sym_pool; + for(i = 0; i < SYM_POOL_NB; i++) { + sym->next = last_sym; + last_sym = sym; + sym++; + } + sym_free_first = last_sym; + return last_sym; +} + +static inline Sym *sym_malloc(void) +{ + Sym *sym; +#ifndef SYM_DEBUG + sym = sym_free_first; + if (!sym) + sym = __sym_malloc(); + sym_free_first = sym->next; + return sym; +#else + sym = tcc_malloc(sizeof(Sym)); + return sym; +#endif +} + +ST_INLN void sym_free(Sym *sym) +{ +#ifndef SYM_DEBUG + sym->next = sym_free_first; + sym_free_first = sym; +#else + tcc_free(sym); +#endif +} + +/* push, without hashing */ +ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c) +{ + Sym *s; + + s = sym_malloc(); + memset(s, 0, sizeof *s); + s->v = v; + s->type.t = t; + s->c = c; + /* add in stack */ + s->prev = *ps; + *ps = s; + return s; +} + +/* find a symbol and return its associated structure. 's' is the top + of the symbol stack */ +ST_FUNC Sym *sym_find2(Sym *s, int v) +{ + while (s) { + if (s->v == v) + return s; + else if (s->v == -1) + return NULL; + s = s->prev; + } + return NULL; +} + +/* structure lookup */ +ST_INLN Sym *struct_find(int v) +{ + v -= TOK_IDENT; + if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT)) + return NULL; + return table_ident[v]->sym_struct; +} + +/* find an identifier */ +ST_INLN Sym *sym_find(int v) +{ + v -= TOK_IDENT; + if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT)) + return NULL; + return table_ident[v]->sym_identifier; +} + +/* push a given symbol on the symbol stack */ +ST_FUNC Sym *sym_push(int v, CType *type, int r, int c) +{ + Sym *s, **ps; + TokenSym *ts; + + if (local_stack) + ps = &local_stack; + else + ps = &global_stack; + s = sym_push2(ps, v, type->t, c); + s->type.ref = type->ref; + s->r = r; + /* don't record fields or anonymous symbols */ + /* XXX: simplify */ + if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) { + /* record symbol in token array */ + ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT]; + if (v & SYM_STRUCT) + ps = &ts->sym_struct; + else + ps = &ts->sym_identifier; + s->prev_tok = *ps; + *ps = s; + s->sym_scope = local_scope; + if (s->prev_tok && s->prev_tok->sym_scope == s->sym_scope) + tcc_error("redeclaration of '%s'", + get_tok_str(v & ~SYM_STRUCT, NULL)); + } + return s; +} + +/* push a global identifier */ +ST_FUNC Sym *global_identifier_push(int v, int t, int c) +{ + Sym *s, **ps; + s = sym_push2(&global_stack, v, t, c); + /* don't record anonymous symbol */ + if (v < SYM_FIRST_ANOM) { + ps = &table_ident[v - TOK_IDENT]->sym_identifier; + /* modify the top most local identifier, so that + sym_identifier will point to 's' when popped */ + while (*ps != NULL && (*ps)->sym_scope) + ps = &(*ps)->prev_tok; + s->prev_tok = *ps; + *ps = s; + } + return s; +} + +/* pop symbols until top reaches 'b'. If KEEP is non-zero don't really + pop them yet from the list, but do remove them from the token array. */ +ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep) +{ + Sym *s, *ss, **ps; + TokenSym *ts; + int v; + + s = *ptop; + while(s != b) { + ss = s->prev; + v = s->v; + /* remove symbol in token array */ + /* XXX: simplify */ + if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) { + ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT]; + if (v & SYM_STRUCT) + ps = &ts->sym_struct; + else + ps = &ts->sym_identifier; + *ps = s->prev_tok; + } + if (!keep) + sym_free(s); + s = ss; + } + if (!keep) + *ptop = b; +} + +/* ------------------------------------------------------------------------- */ + +static void vsetc(CType *type, int r, CValue *vc) +{ + int v; + + if (vtop >= vstack + (VSTACK_SIZE - 1)) + tcc_error("memory full (vstack)"); + /* cannot let cpu flags if other instruction are generated. Also + avoid leaving VT_JMP anywhere except on the top of the stack + because it would complicate the code generator. + + Don't do this when nocode_wanted. vtop might come from + !nocode_wanted regions (see 88_codeopt.c) and transforming + it to a register without actually generating code is wrong + as their value might still be used for real. All values + we push under nocode_wanted will eventually be popped + again, so that the VT_CMP/VT_JMP value will be in vtop + when code is unsuppressed again. + + Same logic below in vswap(); */ + if (vtop >= vstack && !nocode_wanted) { + v = vtop->r & VT_VALMASK; + if (v == VT_CMP || (v & ~1) == VT_JMP) + gv(RC_INT); + } + + vtop++; + vtop->type = *type; + vtop->r = r; + vtop->r2 = VT_CONST; + vtop->c = *vc; + vtop->sym = NULL; +} + +ST_FUNC void vswap(void) +{ + SValue tmp; + /* cannot vswap cpu flags. See comment at vsetc() above */ + if (vtop >= vstack && !nocode_wanted) { + int v = vtop->r & VT_VALMASK; + if (v == VT_CMP || (v & ~1) == VT_JMP) + gv(RC_INT); + } + tmp = vtop[0]; + vtop[0] = vtop[-1]; + vtop[-1] = tmp; +} + +/* pop stack value */ +ST_FUNC void vpop(void) +{ + int v; + v = vtop->r & VT_VALMASK; +#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) + /* for x86, we need to pop the FP stack */ + if (v == TREG_ST0) { + o(0xd8dd); /* fstp %st(0) */ + } else +#endif + if (v == VT_JMP || v == VT_JMPI) { + /* need to put correct jump if && or || without test */ + gsym(vtop->c.i); + } + vtop--; +} + +/* push constant of type "type" with useless value */ +ST_FUNC void vpush(CType *type) +{ + vset(type, VT_CONST, 0); +} + +/* push integer constant */ +ST_FUNC void vpushi(int v) +{ + CValue cval; + cval.i = v; + vsetc(&int_type, VT_CONST, &cval); +} + +/* push a pointer sized constant */ +static void vpushs(addr_t v) +{ + CValue cval; + cval.i = v; + vsetc(&size_type, VT_CONST, &cval); +} + +/* push arbitrary 64bit constant */ +ST_FUNC void vpush64(int ty, unsigned long long v) +{ + CValue cval; + CType ctype; + ctype.t = ty; + ctype.ref = NULL; + cval.i = v; + vsetc(&ctype, VT_CONST, &cval); +} + +/* push long long constant */ +static inline void vpushll(long long v) +{ + vpush64(VT_LLONG, v); +} + +ST_FUNC void vset(CType *type, int r, int v) +{ + CValue cval; + + cval.i = v; + vsetc(type, r, &cval); +} + +static void vseti(int r, int v) +{ + CType type; + type.t = VT_INT; + type.ref = NULL; + vset(&type, r, v); +} + +ST_FUNC void vpushv(SValue *v) +{ + if (vtop >= vstack + (VSTACK_SIZE - 1)) + tcc_error("memory full (vstack)"); + vtop++; + *vtop = *v; +} + +static void vdup(void) +{ + vpushv(vtop); +} + +/* rotate n first stack elements to the bottom + I1 ... In -> I2 ... In I1 [top is right] +*/ +ST_FUNC void vrotb(int n) +{ + int i; + SValue tmp; + + tmp = vtop[-n + 1]; + for(i=-n+1;i!=0;i++) + vtop[i] = vtop[i+1]; + vtop[0] = tmp; +} + +/* rotate the n elements before entry e towards the top + I1 ... In ... -> In I1 ... I(n-1) ... [top is right] + */ +ST_FUNC void vrote(SValue *e, int n) +{ + int i; + SValue tmp; + + tmp = *e; + for(i = 0;i < n - 1; i++) + e[-i] = e[-i - 1]; + e[-n + 1] = tmp; +} + +/* rotate n first stack elements to the top + I1 ... In -> In I1 ... I(n-1) [top is right] + */ +ST_FUNC void vrott(int n) +{ + vrote(vtop, n); +} + +/* push a symbol value of TYPE */ +static inline void vpushsym(CType *type, Sym *sym) +{ + CValue cval; + cval.i = 0; + vsetc(type, VT_CONST | VT_SYM, &cval); + vtop->sym = sym; +} + +/* Return a static symbol pointing to a section */ +ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size) +{ + int v; + Sym *sym; + + v = anon_sym++; + sym = global_identifier_push(v, type->t | VT_STATIC, 0); + sym->type.ref = type->ref; + sym->r = VT_CONST | VT_SYM; + put_extern_sym(sym, sec, offset, size); + return sym; +} + +/* push a reference to a section offset by adding a dummy symbol */ +static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size) +{ + vpushsym(type, get_sym_ref(type, sec, offset, size)); +} + +/* define a new external reference to a symbol 'v' of type 'u' */ +ST_FUNC Sym *external_global_sym(int v, CType *type, int r) +{ + Sym *s; + + s = sym_find(v); + if (!s) { + /* push forward reference */ + s = global_identifier_push(v, type->t | VT_EXTERN, 0); + s->type.ref = type->ref; + s->r = r | VT_CONST | VT_SYM; + } else if (IS_ASM_SYM(s)) { + s->type.t = type->t | (s->type.t & VT_EXTERN); + s->type.ref = type->ref; + update_storage(s); + } + return s; +} + +/* Merge some type attributes. */ +static void patch_type(Sym *sym, CType *type) +{ + if (!(type->t & VT_EXTERN)) { + if (!(sym->type.t & VT_EXTERN)) + tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL)); + sym->type.t &= ~VT_EXTERN; + } + + if (IS_ASM_SYM(sym)) { + /* stay static if both are static */ + sym->type.t = type->t & (sym->type.t | ~VT_STATIC); + sym->type.ref = type->ref; + } + + if (!is_compatible_types(&sym->type, type)) { + tcc_error("incompatible types for redefinition of '%s'", + get_tok_str(sym->v, NULL)); + + } else if ((sym->type.t & VT_BTYPE) == VT_FUNC) { + int static_proto = sym->type.t & VT_STATIC; + /* warn if static follows non-static function declaration */ + if ((type->t & VT_STATIC) && !static_proto && !(type->t & VT_INLINE)) + tcc_warning("static storage ignored for redefinition of '%s'", + get_tok_str(sym->v, NULL)); + + if (0 == (type->t & VT_EXTERN)) { + /* put complete type, use static from prototype */ + sym->type.t = (type->t & ~VT_STATIC) | static_proto; + if (type->t & VT_INLINE) + sym->type.t = type->t; + sym->type.ref = type->ref; + } + + } else { + if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) { + /* set array size if it was omitted in extern declaration */ + if (sym->type.ref->c < 0) + sym->type.ref->c = type->ref->c; + else if (sym->type.ref->c != type->ref->c) + tcc_error("conflicting type for '%s'", get_tok_str(sym->v, NULL)); + } + if ((type->t ^ sym->type.t) & VT_STATIC) + tcc_warning("storage mismatch for redefinition of '%s'", + get_tok_str(sym->v, NULL)); + } +} + + +/* Merge some storage attributes. */ +static void patch_storage(Sym *sym, AttributeDef *ad, CType *type) +{ + if (type) + patch_type(sym, type); + +#ifdef TCC_TARGET_PE + if (sym->a.dllimport != ad->a.dllimport) + tcc_error("incompatible dll linkage for redefinition of '%s'", + get_tok_str(sym->v, NULL)); + sym->a.dllexport |= ad->a.dllexport; +#endif + sym->a.weak |= ad->a.weak; + if (ad->a.visibility) { + int vis = sym->a.visibility; + int vis2 = ad->a.visibility; + if (vis == STV_DEFAULT) + vis = vis2; + else if (vis2 != STV_DEFAULT) + vis = (vis < vis2) ? vis : vis2; + sym->a.visibility = vis; + } + if (ad->a.aligned) + sym->a.aligned = ad->a.aligned; + if (ad->asm_label) + sym->asm_label = ad->asm_label; + update_storage(sym); +} + +/* define a new external reference to a symbol 'v' */ +static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad) +{ + Sym *s; + s = sym_find(v); + if (!s) { + /* push forward reference */ + s = sym_push(v, type, r | VT_CONST | VT_SYM, 0); + s->type.t |= VT_EXTERN; + s->a = ad->a; + s->sym_scope = 0; + } else { + if (s->type.ref == func_old_type.ref) { + s->type.ref = type->ref; + s->r = r | VT_CONST | VT_SYM; + s->type.t |= VT_EXTERN; + } + patch_storage(s, ad, type); + } + return s; +} + +/* push a reference to global symbol v */ +ST_FUNC void vpush_global_sym(CType *type, int v) +{ + vpushsym(type, external_global_sym(v, type, 0)); +} + +/* save registers up to (vtop - n) stack entry */ +ST_FUNC void save_regs(int n) +{ + SValue *p, *p1; + for(p = vstack, p1 = vtop - n; p <= p1; p++) + save_reg(p->r); +} + +/* save r to the memory stack, and mark it as being free */ +ST_FUNC void save_reg(int r) +{ + save_reg_upstack(r, 0); +} + +/* save r to the memory stack, and mark it as being free, + if seen up to (vtop - n) stack entry */ +ST_FUNC void save_reg_upstack(int r, int n) +{ + int l, saved, size, align; + SValue *p, *p1, sv; + CType *type; + + if ((r &= VT_VALMASK) >= VT_CONST) + return; + if (nocode_wanted) + return; + + /* modify all stack values */ + saved = 0; + l = 0; + for(p = vstack, p1 = vtop - n; p <= p1; p++) { + if ((p->r & VT_VALMASK) == r || + ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) { + /* must save value on stack if not already done */ + if (!saved) { + /* NOTE: must reload 'r' because r might be equal to r2 */ + r = p->r & VT_VALMASK; + /* store register in the stack */ + type = &p->type; + if ((p->r & VT_LVAL) || + (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG)) +#if PTR_SIZE == 8 + type = &char_pointer_type; +#else + type = &int_type; +#endif + size = type_size(type, &align); + loc = (loc - size) & -align; + sv.type.t = type->t; + sv.r = VT_LOCAL | VT_LVAL; + sv.c.i = loc; + store(r, &sv); +#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) + /* x86 specific: need to pop fp register ST0 if saved */ + if (r == TREG_ST0) { + o(0xd8dd); /* fstp %st(0) */ + } +#endif +#if PTR_SIZE == 4 + /* special long long case */ + if ((type->t & VT_BTYPE) == VT_LLONG) { + sv.c.i += 4; + store(p->r2, &sv); + } +#endif + l = loc; + saved = 1; + } + /* mark that stack entry as being saved on the stack */ + if (p->r & VT_LVAL) { + /* also clear the bounded flag because the + relocation address of the function was stored in + p->c.i */ + p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL; + } else { + p->r = lvalue_type(p->type.t) | VT_LOCAL; + } + p->r2 = VT_CONST; + p->c.i = l; + } + } +} + +#ifdef TCC_TARGET_ARM +/* find a register of class 'rc2' with at most one reference on stack. + * If none, call get_reg(rc) */ +ST_FUNC int get_reg_ex(int rc, int rc2) +{ + int r; + SValue *p; + + for(r=0;rr & VT_VALMASK) == r || + (p->r2 & VT_VALMASK) == r) + n++; + } + if (n <= 1) + return r; + } + } + return get_reg(rc); +} +#endif + +/* find a free register of class 'rc'. If none, save one register */ +ST_FUNC int get_reg(int rc) +{ + int r; + SValue *p; + + /* find a free register */ + for(r=0;rr & VT_VALMASK) == r || + (p->r2 & VT_VALMASK) == r) + goto notfound; + } + return r; + } + notfound: ; + } + + /* no register left : free the first one on the stack (VERY + IMPORTANT to start from the bottom to ensure that we don't + spill registers used in gen_opi()) */ + for(p=vstack;p<=vtop;p++) { + /* look at second register (if long long) */ + r = p->r2 & VT_VALMASK; + if (r < VT_CONST && (reg_classes[r] & rc)) + goto save_found; + r = p->r & VT_VALMASK; + if (r < VT_CONST && (reg_classes[r] & rc)) { + save_found: + save_reg(r); + return r; + } + } + /* Should never comes here */ + return -1; +} + +/* move register 's' (of type 't') to 'r', and flush previous value of r to memory + if needed */ +static void move_reg(int r, int s, int t) +{ + SValue sv; + + if (r != s) { + save_reg(r); + sv.type.t = t; + sv.type.ref = NULL; + sv.r = s; + sv.c.i = 0; + load(r, &sv); + } +} + +/* get address of vtop (vtop MUST BE an lvalue) */ +ST_FUNC void gaddrof(void) +{ + vtop->r &= ~VT_LVAL; + /* tricky: if saved lvalue, then we can go back to lvalue */ + if ((vtop->r & VT_VALMASK) == VT_LLOCAL) + vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL; + + +} + +#ifdef CONFIG_TCC_BCHECK +/* generate lvalue bound code */ +static void gbound(void) +{ + int lval_type; + CType type1; + + vtop->r &= ~VT_MUSTBOUND; + /* if lvalue, then use checking code before dereferencing */ + if (vtop->r & VT_LVAL) { + /* if not VT_BOUNDED value, then make one */ + if (!(vtop->r & VT_BOUNDED)) { + lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL); + /* must save type because we must set it to int to get pointer */ + type1 = vtop->type; + vtop->type.t = VT_PTR; + gaddrof(); + vpushi(0); + gen_bounded_ptr_add(); + vtop->r |= lval_type; + vtop->type = type1; + } + /* then check for dereferencing */ + gen_bounded_ptr_deref(); + } +} +#endif + +static void incr_bf_adr(int o) +{ + vtop->type = char_pointer_type; + gaddrof(); + vpushi(o); + gen_op('+'); + vtop->type.t = (vtop->type.t & ~(VT_BTYPE|VT_DEFSIGN)) + | (VT_BYTE|VT_UNSIGNED); + vtop->r = (vtop->r & ~VT_LVAL_TYPE) + | (VT_LVAL_BYTE|VT_LVAL_UNSIGNED|VT_LVAL); +} + +/* single-byte load mode for packed or otherwise unaligned bitfields */ +static void load_packed_bf(CType *type, int bit_pos, int bit_size) +{ + int n, o, bits; + save_reg_upstack(vtop->r, 1); + vpush64(type->t & VT_BTYPE, 0); // B X + bits = 0, o = bit_pos >> 3, bit_pos &= 7; + do { + vswap(); // X B + incr_bf_adr(o); + vdup(); // X B B + n = 8 - bit_pos; + if (n > bit_size) + n = bit_size; + if (bit_pos) + vpushi(bit_pos), gen_op(TOK_SHR), bit_pos = 0; // X B Y + if (n < 8) + vpushi((1 << n) - 1), gen_op('&'); + gen_cast(type); + if (bits) + vpushi(bits), gen_op(TOK_SHL); + vrotb(3); // B Y X + gen_op('|'); // B X + bits += n, bit_size -= n, o = 1; + } while (bit_size); + vswap(), vpop(); + if (!(type->t & VT_UNSIGNED)) { + n = ((type->t & VT_BTYPE) == VT_LLONG ? 64 : 32) - bits; + vpushi(n), gen_op(TOK_SHL); + vpushi(n), gen_op(TOK_SAR); + } +} + +/* single-byte store mode for packed or otherwise unaligned bitfields */ +static void store_packed_bf(int bit_pos, int bit_size) +{ + int bits, n, o, m, c; + + c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + vswap(); // X B + save_reg_upstack(vtop->r, 1); + bits = 0, o = bit_pos >> 3, bit_pos &= 7; + do { + incr_bf_adr(o); // X B + vswap(); //B X + c ? vdup() : gv_dup(); // B V X + vrott(3); // X B V + if (bits) + vpushi(bits), gen_op(TOK_SHR); + if (bit_pos) + vpushi(bit_pos), gen_op(TOK_SHL); + n = 8 - bit_pos; + if (n > bit_size) + n = bit_size; + if (n < 8) { + m = ((1 << n) - 1) << bit_pos; + vpushi(m), gen_op('&'); // X B V1 + vpushv(vtop-1); // X B V1 B + vpushi(m & 0x80 ? ~m & 0x7f : ~m); + gen_op('&'); // X B V1 B1 + gen_op('|'); // X B V2 + } + vdup(), vtop[-1] = vtop[-2]; // X B B V2 + vstore(), vpop(); // X B + bits += n, bit_size -= n, bit_pos = 0, o = 1; + } while (bit_size); + vpop(), vpop(); +} + +static int adjust_bf(SValue *sv, int bit_pos, int bit_size) +{ + int t; + if (0 == sv->type.ref) + return 0; + t = sv->type.ref->auxtype; + if (t != -1 && t != VT_STRUCT) { + sv->type.t = (sv->type.t & ~VT_BTYPE) | t; + sv->r = (sv->r & ~VT_LVAL_TYPE) | lvalue_type(sv->type.t); + } + return t; +} + +/* store vtop a register belonging to class 'rc'. lvalues are + converted to values. Cannot be used if cannot be converted to + register value (such as structures). */ +ST_FUNC int gv(int rc) +{ + int r, bit_pos, bit_size, size, align, rc2; + + /* NOTE: get_reg can modify vstack[] */ + if (vtop->type.t & VT_BITFIELD) { + CType type; + + bit_pos = BIT_POS(vtop->type.t); + bit_size = BIT_SIZE(vtop->type.t); + /* remove bit field info to avoid loops */ + vtop->type.t &= ~VT_STRUCT_MASK; + + type.ref = NULL; + type.t = vtop->type.t & VT_UNSIGNED; + if ((vtop->type.t & VT_BTYPE) == VT_BOOL) + type.t |= VT_UNSIGNED; + + r = adjust_bf(vtop, bit_pos, bit_size); + + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) + type.t |= VT_LLONG; + else + type.t |= VT_INT; + + if (r == VT_STRUCT) { + load_packed_bf(&type, bit_pos, bit_size); + } else { + int bits = (type.t & VT_BTYPE) == VT_LLONG ? 64 : 32; + /* cast to int to propagate signedness in following ops */ + gen_cast(&type); + /* generate shifts */ + vpushi(bits - (bit_pos + bit_size)); + gen_op(TOK_SHL); + vpushi(bits - bit_size); + /* NOTE: transformed to SHR if unsigned */ + gen_op(TOK_SAR); + } + r = gv(rc); + } else { + if (is_float(vtop->type.t) && + (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + unsigned long offset; + /* CPUs usually cannot use float constants, so we store them + generically in data segment */ + size = type_size(&vtop->type, &align); + if (NODATA_WANTED) + size = 0, align = 1; + offset = section_add(data_section, size, align); + vpush_ref(&vtop->type, data_section, offset, size); + vswap(); + init_putv(&vtop->type, data_section, offset); + vtop->r |= VT_LVAL; + } +#ifdef CONFIG_TCC_BCHECK + if (vtop->r & VT_MUSTBOUND) + gbound(); +#endif + + r = vtop->r & VT_VALMASK; + rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT; +#ifndef TCC_TARGET_ARM64 + if (rc == RC_IRET) + rc2 = RC_LRET; +#ifdef TCC_TARGET_X86_64 + else if (rc == RC_FRET) + rc2 = RC_QRET; +#endif +#endif + /* need to reload if: + - constant + - lvalue (need to dereference pointer) + - already a register, but not in the right class */ + if (r >= VT_CONST + || (vtop->r & VT_LVAL) + || !(reg_classes[r] & rc) +#if PTR_SIZE == 8 + || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2)) + || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2)) +#else + || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2)) +#endif + ) + { + r = get_reg(rc); +#if PTR_SIZE == 8 + if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) { + int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE; +#else + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + int addr_type = VT_INT, load_size = 4, load_type = VT_INT; + unsigned long long ll; +#endif + int r2, original_type; + original_type = vtop->type.t; + /* two register type load : expand to two words + temporarily */ +#if PTR_SIZE == 4 + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + /* load constant */ + ll = vtop->c.i; + vtop->c.i = ll; /* first word */ + load(r, vtop); + vtop->r = r; /* save register value */ + vpushi(ll >> 32); /* second word */ + } else +#endif + if (vtop->r & VT_LVAL) { + /* We do not want to modifier the long long + pointer here, so the safest (and less + efficient) is to save all the other registers + in the stack. XXX: totally inefficient. */ + #if 0 + save_regs(1); + #else + /* lvalue_save: save only if used further down the stack */ + save_reg_upstack(vtop->r, 1); + #endif + /* load from memory */ + vtop->type.t = load_type; + load(r, vtop); + vdup(); + vtop[-1].r = r; /* save register value */ + /* increment pointer to get second word */ + vtop->type.t = addr_type; + gaddrof(); + vpushi(load_size); + gen_op('+'); + vtop->r |= VT_LVAL; + vtop->type.t = load_type; + } else { + /* move registers */ + load(r, vtop); + vdup(); + vtop[-1].r = r; /* save register value */ + vtop->r = vtop[-1].r2; + } + /* Allocate second register. Here we rely on the fact that + get_reg() tries first to free r2 of an SValue. */ + r2 = get_reg(rc2); + load(r2, vtop); + vpop(); + /* write second register */ + vtop->r2 = r2; + vtop->type.t = original_type; + } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) { + int t1, t; + /* lvalue of scalar type : need to use lvalue type + because of possible cast */ + t = vtop->type.t; + t1 = t; + /* compute memory access type */ + if (vtop->r & VT_LVAL_BYTE) + t = VT_BYTE; + else if (vtop->r & VT_LVAL_SHORT) + t = VT_SHORT; + if (vtop->r & VT_LVAL_UNSIGNED) + t |= VT_UNSIGNED; + vtop->type.t = t; + load(r, vtop); + /* restore wanted type */ + vtop->type.t = t1; + } else { + /* one register type load */ + load(r, vtop); + } + } + vtop->r = r; +#ifdef TCC_TARGET_C67 + /* uses register pairs for doubles */ + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) + vtop->r2 = r+1; +#endif + } + return r; +} + +/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */ +ST_FUNC void gv2(int rc1, int rc2) +{ + int v; + + /* generate more generic register first. But VT_JMP or VT_CMP + values must be generated first in all cases to avoid possible + reload errors */ + v = vtop[0].r & VT_VALMASK; + if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) { + vswap(); + gv(rc1); + vswap(); + gv(rc2); + /* test if reload is needed for first register */ + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + gv(rc1); + vswap(); + } + } else { + gv(rc2); + vswap(); + gv(rc1); + vswap(); + /* test if reload is needed for first register */ + if ((vtop[0].r & VT_VALMASK) >= VT_CONST) { + gv(rc2); + } + } +} + +#ifndef TCC_TARGET_ARM64 +/* wrapper around RC_FRET to return a register by type */ +static int rc_fret(int t) +{ +#ifdef TCC_TARGET_X86_64 + if (t == VT_LDOUBLE) { + return RC_ST0; + } +#endif + return RC_FRET; +} +#endif + +/* wrapper around REG_FRET to return a register by type */ +static int reg_fret(int t) +{ +#ifdef TCC_TARGET_X86_64 + if (t == VT_LDOUBLE) { + return TREG_ST0; + } +#endif + return REG_FRET; +} + +#if PTR_SIZE == 4 +/* expand 64bit on stack in two ints */ +static void lexpand(void) +{ + int u, v; + u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED); + v = vtop->r & (VT_VALMASK | VT_LVAL); + if (v == VT_CONST) { + vdup(); + vtop[0].c.i >>= 32; + } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) { + vdup(); + vtop[0].c.i += 4; + } else { + gv(RC_INT); + vdup(); + vtop[0].r = vtop[-1].r2; + vtop[0].r2 = vtop[-1].r2 = VT_CONST; + } + vtop[0].type.t = vtop[-1].type.t = VT_INT | u; +} +#endif + +#ifdef TCC_TARGET_ARM +/* expand long long on stack */ +ST_FUNC void lexpand_nr(void) +{ + int u,v; + + u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED); + vdup(); + vtop->r2 = VT_CONST; + vtop->type.t = VT_INT | u; + v=vtop[-1].r & (VT_VALMASK | VT_LVAL); + if (v == VT_CONST) { + vtop[-1].c.i = vtop->c.i; + vtop->c.i = vtop->c.i >> 32; + vtop->r = VT_CONST; + } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) { + vtop->c.i += 4; + vtop->r = vtop[-1].r; + } else if (v > VT_CONST) { + vtop--; + lexpand(); + } else + vtop->r = vtop[-1].r2; + vtop[-1].r2 = VT_CONST; + vtop[-1].type.t = VT_INT | u; +} +#endif + +#if PTR_SIZE == 4 +/* build a long long from two ints */ +static void lbuild(int t) +{ + gv2(RC_INT, RC_INT); + vtop[-1].r2 = vtop[0].r; + vtop[-1].type.t = t; + vpop(); +} +#endif + +/* convert stack entry to register and duplicate its value in another + register */ +static void gv_dup(void) +{ + int rc, t, r, r1; + SValue sv; + + t = vtop->type.t; +#if PTR_SIZE == 4 + if ((t & VT_BTYPE) == VT_LLONG) { + if (t & VT_BITFIELD) { + gv(RC_INT); + t = vtop->type.t; + } + lexpand(); + gv_dup(); + vswap(); + vrotb(3); + gv_dup(); + vrotb(4); + /* stack: H L L1 H1 */ + lbuild(t); + vrotb(3); + vrotb(3); + vswap(); + lbuild(t); + vswap(); + } else +#endif + { + /* duplicate value */ + rc = RC_INT; + sv.type.t = VT_INT; + if (is_float(t)) { + rc = RC_FLOAT; +#ifdef TCC_TARGET_X86_64 + if ((t & VT_BTYPE) == VT_LDOUBLE) { + rc = RC_ST0; + } +#endif + sv.type.t = t; + } + r = gv(rc); + r1 = get_reg(rc); + sv.r = r; + sv.c.i = 0; + load(r1, &sv); /* move r to r1 */ + vdup(); + /* duplicates value */ + if (r != r1) + vtop->r = r1; + } +} + +/* Generate value test + * + * Generate a test for any value (jump, comparison and integers) */ +ST_FUNC int gvtst(int inv, int t) +{ + int v = vtop->r & VT_VALMASK; + if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) { + vpushi(0); + gen_op(TOK_NE); + } + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + /* constant jmp optimization */ + if ((vtop->c.i != 0) != inv) + t = gjmp(t); + vtop--; + return t; + } + return gtst(inv, t); +} + +#if PTR_SIZE == 4 +/* generate CPU independent (unsigned) long long operations */ +static void gen_opl(int op) +{ + int t, a, b, op1, c, i; + int func; + unsigned short reg_iret = REG_IRET; + unsigned short reg_lret = REG_LRET; + SValue tmp; + + switch(op) { + case '/': + case TOK_PDIV: + func = TOK___divdi3; + goto gen_func; + case TOK_UDIV: + func = TOK___udivdi3; + goto gen_func; + case '%': + func = TOK___moddi3; + goto gen_mod_func; + case TOK_UMOD: + func = TOK___umoddi3; + gen_mod_func: +#ifdef TCC_ARM_EABI + reg_iret = TREG_R2; + reg_lret = TREG_R3; +#endif + gen_func: + /* call generic long long function */ + vpush_global_sym(&func_old_type, func); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = reg_iret; + vtop->r2 = reg_lret; + break; + case '^': + case '&': + case '|': + case '*': + case '+': + case '-': + //pv("gen_opl A",0,2); + t = vtop->type.t; + vswap(); + lexpand(); + vrotb(3); + lexpand(); + /* stack: L1 H1 L2 H2 */ + tmp = vtop[0]; + vtop[0] = vtop[-3]; + vtop[-3] = tmp; + tmp = vtop[-2]; + vtop[-2] = vtop[-3]; + vtop[-3] = tmp; + vswap(); + /* stack: H1 H2 L1 L2 */ + //pv("gen_opl B",0,4); + if (op == '*') { + vpushv(vtop - 1); + vpushv(vtop - 1); + gen_op(TOK_UMULL); + lexpand(); + /* stack: H1 H2 L1 L2 ML MH */ + for(i=0;i<4;i++) + vrotb(6); + /* stack: ML MH H1 H2 L1 L2 */ + tmp = vtop[0]; + vtop[0] = vtop[-2]; + vtop[-2] = tmp; + /* stack: ML MH H1 L2 H2 L1 */ + gen_op('*'); + vrotb(3); + vrotb(3); + gen_op('*'); + /* stack: ML MH M1 M2 */ + gen_op('+'); + gen_op('+'); + } else if (op == '+' || op == '-') { + /* XXX: add non carry method too (for MIPS or alpha) */ + if (op == '+') + op1 = TOK_ADDC1; + else + op1 = TOK_SUBC1; + gen_op(op1); + /* stack: H1 H2 (L1 op L2) */ + vrotb(3); + vrotb(3); + gen_op(op1 + 1); /* TOK_xxxC2 */ + } else { + gen_op(op); + /* stack: H1 H2 (L1 op L2) */ + vrotb(3); + vrotb(3); + /* stack: (L1 op L2) H1 H2 */ + gen_op(op); + /* stack: (L1 op L2) (H1 op H2) */ + } + /* stack: L H */ + lbuild(t); + break; + case TOK_SAR: + case TOK_SHR: + case TOK_SHL: + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + t = vtop[-1].type.t; + vswap(); + lexpand(); + vrotb(3); + /* stack: L H shift */ + c = (int)vtop->c.i; + /* constant: simpler */ + /* NOTE: all comments are for SHL. the other cases are + done by swapping words */ + vpop(); + if (op != TOK_SHL) + vswap(); + if (c >= 32) { + /* stack: L H */ + vpop(); + if (c > 32) { + vpushi(c - 32); + gen_op(op); + } + if (op != TOK_SAR) { + vpushi(0); + } else { + gv_dup(); + vpushi(31); + gen_op(TOK_SAR); + } + vswap(); + } else { + vswap(); + gv_dup(); + /* stack: H L L */ + vpushi(c); + gen_op(op); + vswap(); + vpushi(32 - c); + if (op == TOK_SHL) + gen_op(TOK_SHR); + else + gen_op(TOK_SHL); + vrotb(3); + /* stack: L L H */ + vpushi(c); + if (op == TOK_SHL) + gen_op(TOK_SHL); + else + gen_op(TOK_SHR); + gen_op('|'); + } + if (op != TOK_SHL) + vswap(); + lbuild(t); + } else { + /* XXX: should provide a faster fallback on x86 ? */ + switch(op) { + case TOK_SAR: + func = TOK___ashrdi3; + goto gen_func; + case TOK_SHR: + func = TOK___lshrdi3; + goto gen_func; + case TOK_SHL: + func = TOK___ashldi3; + goto gen_func; + } + } + break; + default: + /* compare operations */ + t = vtop->type.t; + vswap(); + lexpand(); + vrotb(3); + lexpand(); + /* stack: L1 H1 L2 H2 */ + tmp = vtop[-1]; + vtop[-1] = vtop[-2]; + vtop[-2] = tmp; + /* stack: L1 L2 H1 H2 */ + /* compare high */ + op1 = op; + /* when values are equal, we need to compare low words. since + the jump is inverted, we invert the test too. */ + if (op1 == TOK_LT) + op1 = TOK_LE; + else if (op1 == TOK_GT) + op1 = TOK_GE; + else if (op1 == TOK_ULT) + op1 = TOK_ULE; + else if (op1 == TOK_UGT) + op1 = TOK_UGE; + a = 0; + b = 0; + gen_op(op1); + if (op == TOK_NE) { + b = gvtst(0, 0); + } else { + a = gvtst(1, 0); + if (op != TOK_EQ) { + /* generate non equal test */ + vpushi(TOK_NE); + vtop->r = VT_CMP; + b = gvtst(0, 0); + } + } + /* compare low. Always unsigned */ + op1 = op; + if (op1 == TOK_LT) + op1 = TOK_ULT; + else if (op1 == TOK_LE) + op1 = TOK_ULE; + else if (op1 == TOK_GT) + op1 = TOK_UGT; + else if (op1 == TOK_GE) + op1 = TOK_UGE; + gen_op(op1); + a = gvtst(1, a); + gsym(b); + vseti(VT_JMPI, a); + break; + } +} +#endif + +static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b) +{ + uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b); + return (a ^ b) >> 63 ? -x : x; +} + +static int gen_opic_lt(uint64_t a, uint64_t b) +{ + return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63); +} + +/* handle integer constant optimizations and various machine + independent opt */ +static void gen_opic(int op) +{ + SValue *v1 = vtop - 1; + SValue *v2 = vtop; + int t1 = v1->type.t & VT_BTYPE; + int t2 = v2->type.t & VT_BTYPE; + int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + uint64_t l1 = c1 ? v1->c.i : 0; + uint64_t l2 = c2 ? v2->c.i : 0; + int shm = (t1 == VT_LLONG) ? 63 : 31; + + if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR)) + l1 = ((uint32_t)l1 | + (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000))); + if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR)) + l2 = ((uint32_t)l2 | + (v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000))); + + if (c1 && c2) { + switch(op) { + case '+': l1 += l2; break; + case '-': l1 -= l2; break; + case '&': l1 &= l2; break; + case '^': l1 ^= l2; break; + case '|': l1 |= l2; break; + case '*': l1 *= l2; break; + + case TOK_PDIV: + case '/': + case '%': + case TOK_UDIV: + case TOK_UMOD: + /* if division by zero, generate explicit division */ + if (l2 == 0) { + if (const_wanted) + tcc_error("division by zero in constant"); + goto general_case; + } + switch(op) { + default: l1 = gen_opic_sdiv(l1, l2); break; + case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break; + case TOK_UDIV: l1 = l1 / l2; break; + case TOK_UMOD: l1 = l1 % l2; break; + } + break; + case TOK_SHL: l1 <<= (l2 & shm); break; + case TOK_SHR: l1 >>= (l2 & shm); break; + case TOK_SAR: + l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm); + break; + /* tests */ + case TOK_ULT: l1 = l1 < l2; break; + case TOK_UGE: l1 = l1 >= l2; break; + case TOK_EQ: l1 = l1 == l2; break; + case TOK_NE: l1 = l1 != l2; break; + case TOK_ULE: l1 = l1 <= l2; break; + case TOK_UGT: l1 = l1 > l2; break; + case TOK_LT: l1 = gen_opic_lt(l1, l2); break; + case TOK_GE: l1 = !gen_opic_lt(l1, l2); break; + case TOK_LE: l1 = !gen_opic_lt(l2, l1); break; + case TOK_GT: l1 = gen_opic_lt(l2, l1); break; + /* logical */ + case TOK_LAND: l1 = l1 && l2; break; + case TOK_LOR: l1 = l1 || l2; break; + default: + goto general_case; + } + if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR)) + l1 = ((uint32_t)l1 | + (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000))); + v1->c.i = l1; + vtop--; + } else { + /* if commutative ops, put c2 as constant */ + if (c1 && (op == '+' || op == '&' || op == '^' || + op == '|' || op == '*')) { + vswap(); + c2 = c1; //c = c1, c1 = c2, c2 = c; + l2 = l1; //l = l1, l1 = l2, l2 = l; + } + if (!const_wanted && + c1 && ((l1 == 0 && + (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) || + (l1 == -1 && op == TOK_SAR))) { + /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */ + vtop--; + } else if (!const_wanted && + c2 && ((l2 == 0 && (op == '&' || op == '*')) || + (op == '|' && + (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))) || + (l2 == 1 && (op == '%' || op == TOK_UMOD)))) { + /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */ + if (l2 == 1) + vtop->c.i = 0; + vswap(); + vtop--; + } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV || + op == TOK_PDIV) && + l2 == 1) || + ((op == '+' || op == '-' || op == '|' || op == '^' || + op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) && + l2 == 0) || + (op == '&' && + (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))))) { + /* filter out NOP operations like x*1, x-0, x&-1... */ + vtop--; + } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) { + /* try to use shifts instead of muls or divs */ + if (l2 > 0 && (l2 & (l2 - 1)) == 0) { + int n = -1; + while (l2) { + l2 >>= 1; + n++; + } + vtop->c.i = n; + if (op == '*') + op = TOK_SHL; + else if (op == TOK_PDIV) + op = TOK_SAR; + else + op = TOK_SHR; + } + goto general_case; + } else if (c2 && (op == '+' || op == '-') && + (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM)) + || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) { + /* symbol + constant case */ + if (op == '-') + l2 = -l2; + l2 += vtop[-1].c.i; + /* The backends can't always deal with addends to symbols + larger than +-1<<31. Don't construct such. */ + if ((int)l2 != l2) + goto general_case; + vtop--; + vtop->c.i = l2; + } else { + general_case: + /* call low level op generator */ + if (t1 == VT_LLONG || t2 == VT_LLONG || + (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR))) + gen_opl(op); + else + gen_opi(op); + } + } +} + +/* generate a floating point operation with constant propagation */ +static void gen_opif(int op) +{ + int c1, c2; + SValue *v1, *v2; +#if defined _MSC_VER && defined _AMD64_ + /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */ + volatile +#endif + long double f1, f2; + + v1 = vtop - 1; + v2 = vtop; + /* currently, we cannot do computations with forward symbols */ + c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + if (c1 && c2) { + if (v1->type.t == VT_FLOAT) { + f1 = v1->c.f; + f2 = v2->c.f; + } else if (v1->type.t == VT_DOUBLE) { + f1 = v1->c.d; + f2 = v2->c.d; + } else { + f1 = v1->c.ld; + f2 = v2->c.ld; + } + + /* NOTE: we only do constant propagation if finite number (not + NaN or infinity) (ANSI spec) */ + if (!ieee_finite(f1) || !ieee_finite(f2)) + goto general_case; + + switch(op) { + case '+': f1 += f2; break; + case '-': f1 -= f2; break; + case '*': f1 *= f2; break; + case '/': + if (f2 == 0.0) { + if (const_wanted) + tcc_error("division by zero in constant"); + goto general_case; + } + f1 /= f2; + break; + /* XXX: also handles tests ? */ + default: + goto general_case; + } + /* XXX: overflow test ? */ + if (v1->type.t == VT_FLOAT) { + v1->c.f = f1; + } else if (v1->type.t == VT_DOUBLE) { + v1->c.d = f1; + } else { + v1->c.ld = f1; + } + vtop--; + } else { + general_case: + gen_opf(op); + } +} + +static int pointed_size(CType *type) +{ + int align; + return type_size(pointed_type(type), &align); +} + +static void vla_runtime_pointed_size(CType *type) +{ + int align; + vla_runtime_type_size(pointed_type(type), &align); +} + +static inline int is_null_pointer(SValue *p) +{ + if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) || + ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) || + ((p->type.t & VT_BTYPE) == VT_PTR && + (PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0)); +} + +static inline int is_integer_btype(int bt) +{ + return (bt == VT_BYTE || bt == VT_SHORT || + bt == VT_INT || bt == VT_LLONG); +} + +/* check types for comparison or subtraction of pointers */ +static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op) +{ + CType *type1, *type2, tmp_type1, tmp_type2; + int bt1, bt2; + + /* null pointers are accepted for all comparisons as gcc */ + if (is_null_pointer(p1) || is_null_pointer(p2)) + return; + type1 = &p1->type; + type2 = &p2->type; + bt1 = type1->t & VT_BTYPE; + bt2 = type2->t & VT_BTYPE; + /* accept comparison between pointer and integer with a warning */ + if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') { + if (op != TOK_LOR && op != TOK_LAND ) + tcc_warning("comparison between pointer and integer"); + return; + } + + /* both must be pointers or implicit function pointers */ + if (bt1 == VT_PTR) { + type1 = pointed_type(type1); + } else if (bt1 != VT_FUNC) + goto invalid_operands; + + if (bt2 == VT_PTR) { + type2 = pointed_type(type2); + } else if (bt2 != VT_FUNC) { + invalid_operands: + tcc_error("invalid operands to binary %s", get_tok_str(op, NULL)); + } + if ((type1->t & VT_BTYPE) == VT_VOID || + (type2->t & VT_BTYPE) == VT_VOID) + return; + tmp_type1 = *type1; + tmp_type2 = *type2; + tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + if (!is_compatible_types(&tmp_type1, &tmp_type2)) { + /* gcc-like error if '-' is used */ + if (op == '-') + goto invalid_operands; + else + tcc_warning("comparison of distinct pointer types lacks a cast"); + } +} + +/* generic gen_op: handles types problems */ +ST_FUNC void gen_op(int op) +{ + int u, t1, t2, bt1, bt2, t; + CType type1; + +redo: + t1 = vtop[-1].type.t; + t2 = vtop[0].type.t; + bt1 = t1 & VT_BTYPE; + bt2 = t2 & VT_BTYPE; + + if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) { + tcc_error("operation on a struct"); + } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) { + if (bt2 == VT_FUNC) { + mk_pointer(&vtop->type); + gaddrof(); + } + if (bt1 == VT_FUNC) { + vswap(); + mk_pointer(&vtop->type); + gaddrof(); + vswap(); + } + goto redo; + } else if (bt1 == VT_PTR || bt2 == VT_PTR) { + /* at least one operand is a pointer */ + /* relational op: must be both pointers */ + if (op >= TOK_ULT && op <= TOK_LOR) { + check_comparison_pointer_types(vtop - 1, vtop, op); + /* pointers are handled are unsigned */ +#if PTR_SIZE == 8 + t = VT_LLONG | VT_UNSIGNED; +#else + t = VT_INT | VT_UNSIGNED; +#endif + goto std_op; + } + /* if both pointers, then it must be the '-' op */ + if (bt1 == VT_PTR && bt2 == VT_PTR) { + if (op != '-') + tcc_error("cannot use pointers here"); + check_comparison_pointer_types(vtop - 1, vtop, op); + /* XXX: check that types are compatible */ + if (vtop[-1].type.t & VT_VLA) { + vla_runtime_pointed_size(&vtop[-1].type); + } else { + vpushi(pointed_size(&vtop[-1].type)); + } + vrott(3); + gen_opic(op); + vtop->type.t = ptrdiff_type.t; + vswap(); + gen_op(TOK_PDIV); + } else { + /* exactly one pointer : must be '+' or '-'. */ + if (op != '-' && op != '+') + tcc_error("cannot use pointers here"); + /* Put pointer as first operand */ + if (bt2 == VT_PTR) { + vswap(); + t = t1, t1 = t2, t2 = t; + } +#if PTR_SIZE == 4 + if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG) + /* XXX: truncate here because gen_opl can't handle ptr + long long */ + gen_cast_s(VT_INT); +#endif + type1 = vtop[-1].type; + type1.t &= ~VT_ARRAY; + if (vtop[-1].type.t & VT_VLA) + vla_runtime_pointed_size(&vtop[-1].type); + else { + u = pointed_size(&vtop[-1].type); + if (u < 0) + tcc_error("unknown array element size"); +#if PTR_SIZE == 8 + vpushll(u); +#else + /* XXX: cast to int ? (long long case) */ + vpushi(u); +#endif + } + gen_op('*'); +#if 0 +/* #ifdef CONFIG_TCC_BCHECK + The main reason to removing this code: + #include + int main () + { + int v[10]; + int i = 10; + int j = 9; + fprintf(stderr, "v+i-j = %p\n", v+i-j); + fprintf(stderr, "v+(i-j) = %p\n", v+(i-j)); + } + When this code is on. then the output looks like + v+i-j = 0xfffffffe + v+(i-j) = 0xbff84000 + */ + /* if evaluating constant expression, no code should be + generated, so no bound check */ + if (tcc_state->do_bounds_check && !const_wanted) { + /* if bounded pointers, we generate a special code to + test bounds */ + if (op == '-') { + vpushi(0); + vswap(); + gen_op('-'); + } + gen_bounded_ptr_add(); + } else +#endif + { + gen_opic(op); + } + /* put again type if gen_opic() swaped operands */ + vtop->type = type1; + } + } else if (is_float(bt1) || is_float(bt2)) { + /* compute bigger type and do implicit casts */ + if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) { + t = VT_LDOUBLE; + } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) { + t = VT_DOUBLE; + } else { + t = VT_FLOAT; + } + /* floats can only be used for a few operations */ + if (op != '+' && op != '-' && op != '*' && op != '/' && + (op < TOK_ULT || op > TOK_GT)) + tcc_error("invalid operands for binary operation"); + goto std_op; + } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) { + t = bt1 == VT_LLONG ? VT_LLONG : VT_INT; + if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED)) + t |= VT_UNSIGNED; + t |= (VT_LONG & t1); + goto std_op; + } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) { + /* cast to biggest op */ + t = VT_LLONG | VT_LONG; + if (bt1 == VT_LLONG) + t &= t1; + if (bt2 == VT_LLONG) + t &= t2; + /* convert to unsigned if it does not fit in a long long */ + if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) || + (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED)) + t |= VT_UNSIGNED; + goto std_op; + } else { + /* integer operations */ + t = VT_INT | (VT_LONG & (t1 | t2)); + /* convert to unsigned if it does not fit in an integer */ + if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) || + (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED)) + t |= VT_UNSIGNED; + std_op: + /* XXX: currently, some unsigned operations are explicit, so + we modify them here */ + if (t & VT_UNSIGNED) { + if (op == TOK_SAR) + op = TOK_SHR; + else if (op == '/') + op = TOK_UDIV; + else if (op == '%') + op = TOK_UMOD; + else if (op == TOK_LT) + op = TOK_ULT; + else if (op == TOK_GT) + op = TOK_UGT; + else if (op == TOK_LE) + op = TOK_ULE; + else if (op == TOK_GE) + op = TOK_UGE; + } + vswap(); + type1.t = t; + type1.ref = NULL; + gen_cast(&type1); + vswap(); + /* special case for shifts and long long: we keep the shift as + an integer */ + if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) + type1.t = VT_INT; + gen_cast(&type1); + if (is_float(t)) + gen_opif(op); + else + gen_opic(op); + if (op >= TOK_ULT && op <= TOK_GT) { + /* relational op: the result is an int */ + vtop->type.t = VT_INT; + } else { + vtop->type.t = t; + } + } + // Make sure that we have converted to an rvalue: + if (vtop->r & VT_LVAL) + gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT); +} + +#ifndef TCC_TARGET_ARM +/* generic itof for unsigned long long case */ +static void gen_cvt_itof1(int t) +{ +#ifdef TCC_TARGET_ARM64 + gen_cvt_itof(t); +#else + if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == + (VT_LLONG | VT_UNSIGNED)) { + + if (t == VT_FLOAT) + vpush_global_sym(&func_old_type, TOK___floatundisf); +#if LDOUBLE_SIZE != 8 + else if (t == VT_LDOUBLE) + vpush_global_sym(&func_old_type, TOK___floatundixf); +#endif + else + vpush_global_sym(&func_old_type, TOK___floatundidf); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->r = reg_fret(t); + } else { + gen_cvt_itof(t); + } +#endif +} +#endif + +/* generic ftoi for unsigned long long case */ +static void gen_cvt_ftoi1(int t) +{ +#ifdef TCC_TARGET_ARM64 + gen_cvt_ftoi(t); +#else + int st; + + if (t == (VT_LLONG | VT_UNSIGNED)) { + /* not handled natively */ + st = vtop->type.t & VT_BTYPE; + if (st == VT_FLOAT) + vpush_global_sym(&func_old_type, TOK___fixunssfdi); +#if LDOUBLE_SIZE != 8 + else if (st == VT_LDOUBLE) + vpush_global_sym(&func_old_type, TOK___fixunsxfdi); +#endif + else + vpush_global_sym(&func_old_type, TOK___fixunsdfdi); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->r = REG_IRET; + vtop->r2 = REG_LRET; + } else { + gen_cvt_ftoi(t); + } +#endif +} + +/* force char or short cast */ +static void force_charshort_cast(int t) +{ + int bits, dbt; + + /* cannot cast static initializers */ + if (STATIC_DATA_WANTED) + return; + + dbt = t & VT_BTYPE; + /* XXX: add optimization if lvalue : just change type and offset */ + if (dbt == VT_BYTE) + bits = 8; + else + bits = 16; + if (t & VT_UNSIGNED) { + vpushi((1 << bits) - 1); + gen_op('&'); + } else { + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) + bits = 64 - bits; + else + bits = 32 - bits; + vpushi(bits); + gen_op(TOK_SHL); + /* result must be signed or the SAR is converted to an SHL + This was not the case when "t" was a signed short + and the last value on the stack was an unsigned int */ + vtop->type.t &= ~VT_UNSIGNED; + vpushi(bits); + gen_op(TOK_SAR); + } +} + +/* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */ +static void gen_cast_s(int t) +{ + CType type; + type.t = t; + type.ref = NULL; + gen_cast(&type); +} + +static void gen_cast(CType *type) +{ + int sbt, dbt, sf, df, c, p; + + /* special delayed cast for char/short */ + /* XXX: in some cases (multiple cascaded casts), it may still + be incorrect */ + if (vtop->r & VT_MUSTCAST) { + vtop->r &= ~VT_MUSTCAST; + force_charshort_cast(vtop->type.t); + } + + /* bitfields first get cast to ints */ + if (vtop->type.t & VT_BITFIELD) { + gv(RC_INT); + } + + dbt = type->t & (VT_BTYPE | VT_UNSIGNED); + sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED); + + if (sbt != dbt) { + sf = is_float(sbt); + df = is_float(dbt); + c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM); +#if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387 + c &= dbt != VT_LDOUBLE; +#endif + if (c) { + /* constant case: we can do it now */ + /* XXX: in ISOC, cannot do it if error in convert */ + if (sbt == VT_FLOAT) + vtop->c.ld = vtop->c.f; + else if (sbt == VT_DOUBLE) + vtop->c.ld = vtop->c.d; + + if (df) { + if ((sbt & VT_BTYPE) == VT_LLONG) { + if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 63)) + vtop->c.ld = vtop->c.i; + else + vtop->c.ld = -(long double)-vtop->c.i; + } else if(!sf) { + if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 31)) + vtop->c.ld = (uint32_t)vtop->c.i; + else + vtop->c.ld = -(long double)-(uint32_t)vtop->c.i; + } + + if (dbt == VT_FLOAT) + vtop->c.f = (float)vtop->c.ld; + else if (dbt == VT_DOUBLE) + vtop->c.d = (double)vtop->c.ld; + } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) { + vtop->c.i = vtop->c.ld; + } else if (sf && dbt == VT_BOOL) { + vtop->c.i = (vtop->c.ld != 0); + } else { + if(sf) + vtop->c.i = vtop->c.ld; + else if (sbt == (VT_LLONG|VT_UNSIGNED)) + ; + else if (sbt & VT_UNSIGNED) + vtop->c.i = (uint32_t)vtop->c.i; +#if PTR_SIZE == 8 + else if (sbt == VT_PTR) + ; +#endif + else if (sbt != VT_LLONG) + vtop->c.i = ((uint32_t)vtop->c.i | + -(vtop->c.i & 0x80000000)); + + if (dbt == (VT_LLONG|VT_UNSIGNED)) + ; + else if (dbt == VT_BOOL) + vtop->c.i = (vtop->c.i != 0); +#if PTR_SIZE == 8 + else if (dbt == VT_PTR) + ; +#endif + else if (dbt != VT_LLONG) { + uint32_t m = ((dbt & VT_BTYPE) == VT_BYTE ? 0xff : + (dbt & VT_BTYPE) == VT_SHORT ? 0xffff : + 0xffffffff); + vtop->c.i &= m; + if (!(dbt & VT_UNSIGNED)) + vtop->c.i |= -(vtop->c.i & ((m >> 1) + 1)); + } + } + } else if (p && dbt == VT_BOOL) { + vtop->r = VT_CONST; + vtop->c.i = 1; + } else { + /* non constant case: generate code */ + if (sf && df) { + /* convert from fp to fp */ + gen_cvt_ftof(dbt); + } else if (df) { + /* convert int to fp */ + gen_cvt_itof1(dbt); + } else if (sf) { + /* convert fp to int */ + if (dbt == VT_BOOL) { + vpushi(0); + gen_op(TOK_NE); + } else { + /* we handle char/short/etc... with generic code */ + if (dbt != (VT_INT | VT_UNSIGNED) && + dbt != (VT_LLONG | VT_UNSIGNED) && + dbt != VT_LLONG) + dbt = VT_INT; + gen_cvt_ftoi1(dbt); + if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) { + /* additional cast for char/short... */ + vtop->type.t = dbt; + gen_cast(type); + } + } +#if PTR_SIZE == 4 + } else if ((dbt & VT_BTYPE) == VT_LLONG) { + if ((sbt & VT_BTYPE) != VT_LLONG) { + /* scalar to long long */ + /* machine independent conversion */ + gv(RC_INT); + /* generate high word */ + if (sbt == (VT_INT | VT_UNSIGNED)) { + vpushi(0); + gv(RC_INT); + } else { + if (sbt == VT_PTR) { + /* cast from pointer to int before we apply + shift operation, which pointers don't support*/ + gen_cast_s(VT_INT); + } + gv_dup(); + vpushi(31); + gen_op(TOK_SAR); + } + /* patch second register */ + vtop[-1].r2 = vtop->r; + vpop(); + } +#else + } else if ((dbt & VT_BTYPE) == VT_LLONG || + (dbt & VT_BTYPE) == VT_PTR || + (dbt & VT_BTYPE) == VT_FUNC) { + if ((sbt & VT_BTYPE) != VT_LLONG && + (sbt & VT_BTYPE) != VT_PTR && + (sbt & VT_BTYPE) != VT_FUNC) { + /* need to convert from 32bit to 64bit */ + gv(RC_INT); + if (sbt != (VT_INT | VT_UNSIGNED)) { +#if defined(TCC_TARGET_ARM64) + gen_cvt_sxtw(); +#elif defined(TCC_TARGET_X86_64) + int r = gv(RC_INT); + /* x86_64 specific: movslq */ + o(0x6348); + o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r)); +#else +#error +#endif + } + } +#endif + } else if (dbt == VT_BOOL) { + /* scalar to bool */ + vpushi(0); + gen_op(TOK_NE); + } else if ((dbt & VT_BTYPE) == VT_BYTE || + (dbt & VT_BTYPE) == VT_SHORT) { + if (sbt == VT_PTR) { + vtop->type.t = VT_INT; + tcc_warning("nonportable conversion from pointer to char/short"); + } + force_charshort_cast(dbt); +#if PTR_SIZE == 4 + } else if ((dbt & VT_BTYPE) == VT_INT) { + /* scalar to int */ + if ((sbt & VT_BTYPE) == VT_LLONG) { + /* from long long: just take low order word */ + lexpand(); + vpop(); + } + /* if lvalue and single word type, nothing to do because + the lvalue already contains the real type size (see + VT_LVAL_xxx constants) */ +#endif + } + } + } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) { + /* if we are casting between pointer types, + we must update the VT_LVAL_xxx size */ + vtop->r = (vtop->r & ~VT_LVAL_TYPE) + | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE); + } + vtop->type = *type; +} + +/* return type size as known at compile time. Put alignment at 'a' */ +ST_FUNC int type_size(CType *type, int *a) +{ + Sym *s; + int bt; + + bt = type->t & VT_BTYPE; + if (bt == VT_STRUCT) { + /* struct/union */ + s = type->ref; + *a = s->r; + return s->c; + } else if (bt == VT_PTR) { + if (type->t & VT_ARRAY) { + int ts; + + s = type->ref; + ts = type_size(&s->type, a); + + if (ts < 0 && s->c < 0) + ts = -ts; + + return ts * s->c; + } else { + *a = PTR_SIZE; + return PTR_SIZE; + } + } else if (IS_ENUM(type->t) && type->ref->c == -1) { + return -1; /* incomplete enum */ + } else if (bt == VT_LDOUBLE) { + *a = LDOUBLE_ALIGN; + return LDOUBLE_SIZE; + } else if (bt == VT_DOUBLE || bt == VT_LLONG) { +#ifdef TCC_TARGET_I386 +#ifdef TCC_TARGET_PE + *a = 8; +#else + *a = 4; +#endif +#elif defined(TCC_TARGET_ARM) +#ifdef TCC_ARM_EABI + *a = 8; +#else + *a = 4; +#endif +#else + *a = 8; +#endif + return 8; + } else if (bt == VT_INT || bt == VT_FLOAT) { + *a = 4; + return 4; + } else if (bt == VT_SHORT) { + *a = 2; + return 2; + } else if (bt == VT_QLONG || bt == VT_QFLOAT) { + *a = 8; + return 16; + } else { + /* char, void, function, _Bool */ + *a = 1; + return 1; + } +} + +/* push type size as known at runtime time on top of value stack. Put + alignment at 'a' */ +ST_FUNC void vla_runtime_type_size(CType *type, int *a) +{ + if (type->t & VT_VLA) { + type_size(&type->ref->type, a); + vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c); + } else { + vpushi(type_size(type, a)); + } +} + +static void vla_sp_restore(void) { + if (vlas_in_scope) { + gen_vla_sp_restore(vla_sp_loc); + } +} + +static void vla_sp_restore_root(void) { + if (vlas_in_scope) { + gen_vla_sp_restore(vla_sp_root_loc); + } +} + +/* return the pointed type of t */ +static inline CType *pointed_type(CType *type) +{ + return &type->ref->type; +} + +/* modify type so that its it is a pointer to type. */ +ST_FUNC void mk_pointer(CType *type) +{ + Sym *s; + s = sym_push(SYM_FIELD, type, 0, -1); + type->t = VT_PTR | (type->t & VT_STORAGE); + type->ref = s; +} + +/* compare function types. OLD functions match any new functions */ +static int is_compatible_func(CType *type1, CType *type2) +{ + Sym *s1, *s2; + + s1 = type1->ref; + s2 = type2->ref; + if (!is_compatible_types(&s1->type, &s2->type)) + return 0; + /* check func_call */ + if (s1->f.func_call != s2->f.func_call) + return 0; + /* XXX: not complete */ + if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD) + return 1; + if (s1->f.func_type != s2->f.func_type) + return 0; + while (s1 != NULL) { + if (s2 == NULL) + return 0; + if (!is_compatible_unqualified_types(&s1->type, &s2->type)) + return 0; + s1 = s1->next; + s2 = s2->next; + } + if (s2) + return 0; + return 1; +} + +/* return true if type1 and type2 are the same. If unqualified is + true, qualifiers on the types are ignored. + + - enums are not checked as gcc __builtin_types_compatible_p () + */ +static int compare_types(CType *type1, CType *type2, int unqualified) +{ + int bt1, t1, t2; + + t1 = type1->t & VT_TYPE; + t2 = type2->t & VT_TYPE; + if (unqualified) { + /* strip qualifiers before comparing */ + t1 &= ~(VT_CONSTANT | VT_VOLATILE); + t2 &= ~(VT_CONSTANT | VT_VOLATILE); + } + + /* Default Vs explicit signedness only matters for char */ + if ((t1 & VT_BTYPE) != VT_BYTE) { + t1 &= ~VT_DEFSIGN; + t2 &= ~VT_DEFSIGN; + } + /* XXX: bitfields ? */ + if (t1 != t2) + return 0; + /* test more complicated cases */ + bt1 = t1 & VT_BTYPE; + if (bt1 == VT_PTR) { + type1 = pointed_type(type1); + type2 = pointed_type(type2); + return is_compatible_types(type1, type2); + } else if (bt1 == VT_STRUCT) { + return (type1->ref == type2->ref); + } else if (bt1 == VT_FUNC) { + return is_compatible_func(type1, type2); + } else { + return 1; + } +} + +/* return true if type1 and type2 are exactly the same (including + qualifiers). +*/ +static int is_compatible_types(CType *type1, CType *type2) +{ + return compare_types(type1,type2,0); +} + +/* return true if type1 and type2 are the same (ignoring qualifiers). +*/ +static int is_compatible_unqualified_types(CType *type1, CType *type2) +{ + return compare_types(type1,type2,1); +} + +/* print a type. If 'varstr' is not NULL, then the variable is also + printed in the type */ +/* XXX: union */ +/* XXX: add array and function pointers */ +static void type_to_str(char *buf, int buf_size, + CType *type, const char *varstr) +{ + int bt, v, t; + Sym *s, *sa; + char buf1[256]; + const char *tstr; + + t = type->t; + bt = t & VT_BTYPE; + buf[0] = '\0'; + + if (t & VT_EXTERN) + pstrcat(buf, buf_size, "extern "); + if (t & VT_STATIC) + pstrcat(buf, buf_size, "static "); + if (t & VT_TYPEDEF) + pstrcat(buf, buf_size, "typedef "); + if (t & VT_INLINE) + pstrcat(buf, buf_size, "inline "); + if (t & VT_VOLATILE) + pstrcat(buf, buf_size, "volatile "); + if (t & VT_CONSTANT) + pstrcat(buf, buf_size, "const "); + + if (((t & VT_DEFSIGN) && bt == VT_BYTE) + || ((t & VT_UNSIGNED) + && (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG) + && !IS_ENUM(t) + )) + pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed "); + + buf_size -= strlen(buf); + buf += strlen(buf); + + switch(bt) { + case VT_VOID: + tstr = "void"; + goto add_tstr; + case VT_BOOL: + tstr = "_Bool"; + goto add_tstr; + case VT_BYTE: + tstr = "char"; + goto add_tstr; + case VT_SHORT: + tstr = "short"; + goto add_tstr; + case VT_INT: + tstr = "int"; + goto maybe_long; + case VT_LLONG: + tstr = "long long"; + maybe_long: + if (t & VT_LONG) + tstr = "long"; + if (!IS_ENUM(t)) + goto add_tstr; + tstr = "enum "; + goto tstruct; + case VT_FLOAT: + tstr = "float"; + goto add_tstr; + case VT_DOUBLE: + tstr = "double"; + goto add_tstr; + case VT_LDOUBLE: + tstr = "long double"; + add_tstr: + pstrcat(buf, buf_size, tstr); + break; + case VT_STRUCT: + tstr = "struct "; + if (IS_UNION(t)) + tstr = "union "; + tstruct: + pstrcat(buf, buf_size, tstr); + v = type->ref->v & ~SYM_STRUCT; + if (v >= SYM_FIRST_ANOM) + pstrcat(buf, buf_size, ""); + else + pstrcat(buf, buf_size, get_tok_str(v, NULL)); + break; + case VT_FUNC: + s = type->ref; + type_to_str(buf, buf_size, &s->type, varstr); + pstrcat(buf, buf_size, "("); + sa = s->next; + while (sa != NULL) { + type_to_str(buf1, sizeof(buf1), &sa->type, NULL); + pstrcat(buf, buf_size, buf1); + sa = sa->next; + if (sa) + pstrcat(buf, buf_size, ", "); + } + pstrcat(buf, buf_size, ")"); + goto no_var; + case VT_PTR: + s = type->ref; + if (t & VT_ARRAY) { + snprintf(buf1, sizeof(buf1), "%s[%d]", varstr ? varstr : "", s->c); + type_to_str(buf, buf_size, &s->type, buf1); + goto no_var; + } + pstrcpy(buf1, sizeof(buf1), "*"); + if (t & VT_CONSTANT) + pstrcat(buf1, buf_size, "const "); + if (t & VT_VOLATILE) + pstrcat(buf1, buf_size, "volatile "); + if (varstr) + pstrcat(buf1, sizeof(buf1), varstr); + type_to_str(buf, buf_size, &s->type, buf1); + goto no_var; + } + if (varstr) { + pstrcat(buf, buf_size, " "); + pstrcat(buf, buf_size, varstr); + } + no_var: ; +} + +/* verify type compatibility to store vtop in 'dt' type, and generate + casts if needed. */ +static void gen_assign_cast(CType *dt) +{ + CType *st, *type1, *type2; + char buf1[256], buf2[256]; + int dbt, sbt; + + st = &vtop->type; /* source type */ + dbt = dt->t & VT_BTYPE; + sbt = st->t & VT_BTYPE; + if (sbt == VT_VOID || dbt == VT_VOID) { + if (sbt == VT_VOID && dbt == VT_VOID) + ; /* + It is Ok if both are void + A test program: + void func1() {} + void func2() { + return func1(); + } + gcc accepts this program + */ + else + tcc_error("cannot cast from/to void"); + } + if (dt->t & VT_CONSTANT) + tcc_warning("assignment of read-only location"); + switch(dbt) { + case VT_PTR: + /* special cases for pointers */ + /* '0' can also be a pointer */ + if (is_null_pointer(vtop)) + goto type_ok; + /* accept implicit pointer to integer cast with warning */ + if (is_integer_btype(sbt)) { + tcc_warning("assignment makes pointer from integer without a cast"); + goto type_ok; + } + type1 = pointed_type(dt); + /* a function is implicitly a function pointer */ + if (sbt == VT_FUNC) { + if ((type1->t & VT_BTYPE) != VT_VOID && + !is_compatible_types(pointed_type(dt), st)) + tcc_warning("assignment from incompatible pointer type"); + goto type_ok; + } + if (sbt != VT_PTR) + goto error; + type2 = pointed_type(st); + if ((type1->t & VT_BTYPE) == VT_VOID || + (type2->t & VT_BTYPE) == VT_VOID) { + /* void * can match anything */ + } else { + //printf("types %08x %08x\n", type1->t, type2->t); + /* exact type match, except for qualifiers */ + if (!is_compatible_unqualified_types(type1, type2)) { + /* Like GCC don't warn by default for merely changes + in pointer target signedness. Do warn for different + base types, though, in particular for unsigned enums + and signed int targets. */ + if ((type1->t & (VT_BTYPE|VT_LONG)) != (type2->t & (VT_BTYPE|VT_LONG)) + || IS_ENUM(type1->t) || IS_ENUM(type2->t) + ) + tcc_warning("assignment from incompatible pointer type"); + } + } + /* check const and volatile */ + if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) || + (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE))) + tcc_warning("assignment discards qualifiers from pointer target type"); + break; + case VT_BYTE: + case VT_SHORT: + case VT_INT: + case VT_LLONG: + if (sbt == VT_PTR || sbt == VT_FUNC) { + tcc_warning("assignment makes integer from pointer without a cast"); + } else if (sbt == VT_STRUCT) { + goto case_VT_STRUCT; + } + /* XXX: more tests */ + break; + case VT_STRUCT: + case_VT_STRUCT: + if (!is_compatible_unqualified_types(dt, st)) { + error: + type_to_str(buf1, sizeof(buf1), st, NULL); + type_to_str(buf2, sizeof(buf2), dt, NULL); + tcc_error("cannot cast '%s' to '%s'", buf1, buf2); + } + break; + } + type_ok: + gen_cast(dt); +} + +/* store vtop in lvalue pushed on stack */ +ST_FUNC void vstore(void) +{ + int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast; + + ft = vtop[-1].type.t; + sbt = vtop->type.t & VT_BTYPE; + dbt = ft & VT_BTYPE; + if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) || + (sbt == VT_INT && dbt == VT_SHORT)) + && !(vtop->type.t & VT_BITFIELD)) { + /* optimize char/short casts */ + delayed_cast = VT_MUSTCAST; + vtop->type.t = ft & VT_TYPE; + /* XXX: factorize */ + if (ft & VT_CONSTANT) + tcc_warning("assignment of read-only location"); + } else { + delayed_cast = 0; + if (!(ft & VT_BITFIELD)) + gen_assign_cast(&vtop[-1].type); + } + + if (sbt == VT_STRUCT) { + /* if structure, only generate pointer */ + /* structure assignment : generate memcpy */ + /* XXX: optimize if small size */ + size = type_size(&vtop->type, &align); + + /* destination */ + vswap(); + vtop->type.t = VT_PTR; + gaddrof(); + + /* address of memcpy() */ +#ifdef TCC_ARM_EABI + if(!(align & 7)) + vpush_global_sym(&func_old_type, TOK_memcpy8); + else if(!(align & 3)) + vpush_global_sym(&func_old_type, TOK_memcpy4); + else +#endif + /* Use memmove, rather than memcpy, as dest and src may be same: */ + vpush_global_sym(&func_old_type, TOK_memmove); + + vswap(); + /* source */ + vpushv(vtop - 2); + vtop->type.t = VT_PTR; + gaddrof(); + /* type size */ + vpushi(size); + gfunc_call(3); + + /* leave source on stack */ + } else if (ft & VT_BITFIELD) { + /* bitfield store handling */ + + /* save lvalue as expression result (example: s.b = s.a = n;) */ + vdup(), vtop[-1] = vtop[-2]; + + bit_pos = BIT_POS(ft); + bit_size = BIT_SIZE(ft); + /* remove bit field info to avoid loops */ + vtop[-1].type.t = ft & ~VT_STRUCT_MASK; + + if ((ft & VT_BTYPE) == VT_BOOL) { + gen_cast(&vtop[-1].type); + vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED); + } + + r = adjust_bf(vtop - 1, bit_pos, bit_size); + if (r == VT_STRUCT) { + gen_cast_s((ft & VT_BTYPE) == VT_LLONG ? VT_LLONG : VT_INT); + store_packed_bf(bit_pos, bit_size); + } else { + unsigned long long mask = (1ULL << bit_size) - 1; + if ((ft & VT_BTYPE) != VT_BOOL) { + /* mask source */ + if ((vtop[-1].type.t & VT_BTYPE) == VT_LLONG) + vpushll(mask); + else + vpushi((unsigned)mask); + gen_op('&'); + } + /* shift source */ + vpushi(bit_pos); + gen_op(TOK_SHL); + vswap(); + /* duplicate destination */ + vdup(); + vrott(3); + /* load destination, mask and or with source */ + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) + vpushll(~(mask << bit_pos)); + else + vpushi(~((unsigned)mask << bit_pos)); + gen_op('&'); + gen_op('|'); + /* store result */ + vstore(); + /* ... and discard */ + vpop(); + } + } else if (dbt == VT_VOID) { + --vtop; + } else { +#ifdef CONFIG_TCC_BCHECK + /* bound check case */ + if (vtop[-1].r & VT_MUSTBOUND) { + vswap(); + gbound(); + vswap(); + } +#endif + rc = RC_INT; + if (is_float(ft)) { + rc = RC_FLOAT; +#ifdef TCC_TARGET_X86_64 + if ((ft & VT_BTYPE) == VT_LDOUBLE) { + rc = RC_ST0; + } else if ((ft & VT_BTYPE) == VT_QFLOAT) { + rc = RC_FRET; + } +#endif + } + r = gv(rc); /* generate value */ + /* if lvalue was saved on stack, must read it */ + if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) { + SValue sv; + t = get_reg(RC_INT); +#if PTR_SIZE == 8 + sv.type.t = VT_PTR; +#else + sv.type.t = VT_INT; +#endif + sv.r = VT_LOCAL | VT_LVAL; + sv.c.i = vtop[-1].c.i; + load(t, &sv); + vtop[-1].r = t | VT_LVAL; + } + /* two word case handling : store second register at word + 4 (or +8 for x86-64) */ +#if PTR_SIZE == 8 + if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) { + int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE; +#else + if ((ft & VT_BTYPE) == VT_LLONG) { + int addr_type = VT_INT, load_size = 4, load_type = VT_INT; +#endif + vtop[-1].type.t = load_type; + store(r, vtop - 1); + vswap(); + /* convert to int to increment easily */ + vtop->type.t = addr_type; + gaddrof(); + vpushi(load_size); + gen_op('+'); + vtop->r |= VT_LVAL; + vswap(); + vtop[-1].type.t = load_type; + /* XXX: it works because r2 is spilled last ! */ + store(vtop->r2, vtop - 1); + } else { + store(r, vtop - 1); + } + + vswap(); + vtop--; /* NOT vpop() because on x86 it would flush the fp stack */ + vtop->r |= delayed_cast; + } +} + +/* post defines POST/PRE add. c is the token ++ or -- */ +ST_FUNC void inc(int post, int c) +{ + test_lvalue(); + vdup(); /* save lvalue */ + if (post) { + gv_dup(); /* duplicate value */ + vrotb(3); + vrotb(3); + } + /* add constant */ + vpushi(c - TOK_MID); + gen_op('+'); + vstore(); /* store value */ + if (post) + vpop(); /* if post op, return saved value */ +} + +ST_FUNC void parse_mult_str (CString *astr, const char *msg) +{ + /* read the string */ + if (tok != TOK_STR) + expect(msg); + cstr_new(astr); + while (tok == TOK_STR) { + /* XXX: add \0 handling too ? */ + cstr_cat(astr, tokc.str.data, -1); + next(); + } + cstr_ccat(astr, '\0'); +} + +/* If I is >= 1 and a power of two, returns log2(i)+1. + If I is 0 returns 0. */ +static int exact_log2p1(int i) +{ + int ret; + if (!i) + return 0; + for (ret = 1; i >= 1 << 8; ret += 8) + i >>= 8; + if (i >= 1 << 4) + ret += 4, i >>= 4; + if (i >= 1 << 2) + ret += 2, i >>= 2; + if (i >= 1 << 1) + ret++; + return ret; +} + +/* Parse __attribute__((...)) GNUC extension. */ +static void parse_attribute(AttributeDef *ad) +{ + int t, n; + CString astr; + +redo: + if (tok != TOK_ATTRIBUTE1 && tok != TOK_ATTRIBUTE2) + return; + next(); + skip('('); + skip('('); + while (tok != ')') { + if (tok < TOK_IDENT) + expect("attribute name"); + t = tok; + next(); + switch(t) { + case TOK_SECTION1: + case TOK_SECTION2: + skip('('); + parse_mult_str(&astr, "section name"); + ad->section = find_section(tcc_state, (char *)astr.data); + skip(')'); + cstr_free(&astr); + break; + case TOK_ALIAS1: + case TOK_ALIAS2: + skip('('); + parse_mult_str(&astr, "alias(\"target\")"); + ad->alias_target = /* save string as token, for later */ + tok_alloc((char*)astr.data, astr.size-1)->tok; + skip(')'); + cstr_free(&astr); + break; + case TOK_VISIBILITY1: + case TOK_VISIBILITY2: + skip('('); + parse_mult_str(&astr, + "visibility(\"default|hidden|internal|protected\")"); + if (!strcmp (astr.data, "default")) + ad->a.visibility = STV_DEFAULT; + else if (!strcmp (astr.data, "hidden")) + ad->a.visibility = STV_HIDDEN; + else if (!strcmp (astr.data, "internal")) + ad->a.visibility = STV_INTERNAL; + else if (!strcmp (astr.data, "protected")) + ad->a.visibility = STV_PROTECTED; + else + expect("visibility(\"default|hidden|internal|protected\")"); + skip(')'); + cstr_free(&astr); + break; + case TOK_ALIGNED1: + case TOK_ALIGNED2: + if (tok == '(') { + next(); + n = expr_const(); + if (n <= 0 || (n & (n - 1)) != 0) + tcc_error("alignment must be a positive power of two"); + skip(')'); + } else { + n = MAX_ALIGN; + } + ad->a.aligned = exact_log2p1(n); + if (n != 1 << (ad->a.aligned - 1)) + tcc_error("alignment of %d is larger than implemented", n); + break; + case TOK_PACKED1: + case TOK_PACKED2: + ad->a.packed = 1; + break; + case TOK_WEAK1: + case TOK_WEAK2: + ad->a.weak = 1; + break; + case TOK_UNUSED1: + case TOK_UNUSED2: + /* currently, no need to handle it because tcc does not + track unused objects */ + break; + case TOK_NORETURN1: + case TOK_NORETURN2: + /* currently, no need to handle it because tcc does not + track unused objects */ + break; + case TOK_CDECL1: + case TOK_CDECL2: + case TOK_CDECL3: + ad->f.func_call = FUNC_CDECL; + break; + case TOK_STDCALL1: + case TOK_STDCALL2: + case TOK_STDCALL3: + ad->f.func_call = FUNC_STDCALL; + break; +#ifdef TCC_TARGET_I386 + case TOK_REGPARM1: + case TOK_REGPARM2: + skip('('); + n = expr_const(); + if (n > 3) + n = 3; + else if (n < 0) + n = 0; + if (n > 0) + ad->f.func_call = FUNC_FASTCALL1 + n - 1; + skip(')'); + break; + case TOK_FASTCALL1: + case TOK_FASTCALL2: + case TOK_FASTCALL3: + ad->f.func_call = FUNC_FASTCALLW; + break; +#endif + case TOK_MODE: + skip('('); + switch(tok) { + case TOK_MODE_DI: + ad->attr_mode = VT_LLONG + 1; + break; + case TOK_MODE_QI: + ad->attr_mode = VT_BYTE + 1; + break; + case TOK_MODE_HI: + ad->attr_mode = VT_SHORT + 1; + break; + case TOK_MODE_SI: + case TOK_MODE_word: + ad->attr_mode = VT_INT + 1; + break; + default: + tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL)); + break; + } + next(); + skip(')'); + break; + case TOK_DLLEXPORT: + ad->a.dllexport = 1; + break; + case TOK_DLLIMPORT: + ad->a.dllimport = 1; + break; + default: + if (tcc_state->warn_unsupported) + tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL)); + /* skip parameters */ + if (tok == '(') { + int parenthesis = 0; + do { + if (tok == '(') + parenthesis++; + else if (tok == ')') + parenthesis--; + next(); + } while (parenthesis && tok != -1); + } + break; + } + if (tok != ',') + break; + next(); + } + skip(')'); + skip(')'); + goto redo; +} + +static Sym * find_field (CType *type, int v) +{ + Sym *s = type->ref; + v |= SYM_FIELD; + while ((s = s->next) != NULL) { + if ((s->v & SYM_FIELD) && + (s->type.t & VT_BTYPE) == VT_STRUCT && + (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) { + Sym *ret = find_field (&s->type, v); + if (ret) + return ret; + } + if (s->v == v) + break; + } + return s; +} + +static void struct_add_offset (Sym *s, int offset) +{ + while ((s = s->next) != NULL) { + if ((s->v & SYM_FIELD) && + (s->type.t & VT_BTYPE) == VT_STRUCT && + (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) { + struct_add_offset(s->type.ref, offset); + } else + s->c += offset; + } +} + +static void struct_layout(CType *type, AttributeDef *ad) +{ + int size, align, maxalign, offset, c, bit_pos, bit_size; + int packed, a, bt, prevbt, prev_bit_size; + int pcc = !tcc_state->ms_bitfields; + int pragma_pack = *tcc_state->pack_stack_ptr; + Sym *f; + + maxalign = 1; + offset = 0; + c = 0; + bit_pos = 0; + prevbt = VT_STRUCT; /* make it never match */ + prev_bit_size = 0; + +//#define BF_DEBUG + + for (f = type->ref->next; f; f = f->next) { + if (f->type.t & VT_BITFIELD) + bit_size = BIT_SIZE(f->type.t); + else + bit_size = -1; + size = type_size(&f->type, &align); + a = f->a.aligned ? 1 << (f->a.aligned - 1) : 0; + packed = 0; + + if (pcc && bit_size == 0) { + /* in pcc mode, packing does not affect zero-width bitfields */ + + } else { + /* in pcc mode, attribute packed overrides if set. */ + if (pcc && (f->a.packed || ad->a.packed)) + align = packed = 1; + + /* pragma pack overrides align if lesser and packs bitfields always */ + if (pragma_pack) { + packed = 1; + if (pragma_pack < align) + align = pragma_pack; + /* in pcc mode pragma pack also overrides individual align */ + if (pcc && pragma_pack < a) + a = 0; + } + } + /* some individual align was specified */ + if (a) + align = a; + + if (type->ref->type.t == VT_UNION) { + if (pcc && bit_size >= 0) + size = (bit_size + 7) >> 3; + offset = 0; + if (size > c) + c = size; + + } else if (bit_size < 0) { + if (pcc) + c += (bit_pos + 7) >> 3; + c = (c + align - 1) & -align; + offset = c; + if (size > 0) + c += size; + bit_pos = 0; + prevbt = VT_STRUCT; + prev_bit_size = 0; + + } else { + /* A bit-field. Layout is more complicated. There are two + options: PCC (GCC) compatible and MS compatible */ + if (pcc) { + /* In PCC layout a bit-field is placed adjacent to the + preceding bit-fields, except if: + - it has zero-width + - an individual alignment was given + - it would overflow its base type container and + there is no packing */ + if (bit_size == 0) { + new_field: + c = (c + ((bit_pos + 7) >> 3) + align - 1) & -align; + bit_pos = 0; + } else if (f->a.aligned) { + goto new_field; + } else if (!packed) { + int a8 = align * 8; + int ofs = ((c * 8 + bit_pos) % a8 + bit_size + a8 - 1) / a8; + if (ofs > size / align) + goto new_field; + } + + /* in pcc mode, long long bitfields have type int if they fit */ + if (size == 8 && bit_size <= 32) + f->type.t = (f->type.t & ~VT_BTYPE) | VT_INT, size = 4; + + while (bit_pos >= align * 8) + c += align, bit_pos -= align * 8; + offset = c; + + /* In PCC layout named bit-fields influence the alignment + of the containing struct using the base types alignment, + except for packed fields (which here have correct align). */ + if (f->v & SYM_FIRST_ANOM + // && bit_size // ??? gcc on ARM/rpi does that + ) + align = 1; + + } else { + bt = f->type.t & VT_BTYPE; + if ((bit_pos + bit_size > size * 8) + || (bit_size > 0) == (bt != prevbt) + ) { + c = (c + align - 1) & -align; + offset = c; + bit_pos = 0; + /* In MS bitfield mode a bit-field run always uses + at least as many bits as the underlying type. + To start a new run it's also required that this + or the last bit-field had non-zero width. */ + if (bit_size || prev_bit_size) + c += size; + } + /* In MS layout the records alignment is normally + influenced by the field, except for a zero-width + field at the start of a run (but by further zero-width + fields it is again). */ + if (bit_size == 0 && prevbt != bt) + align = 1; + prevbt = bt; + prev_bit_size = bit_size; + } + + f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT)) + | (bit_pos << VT_STRUCT_SHIFT); + bit_pos += bit_size; + } + if (align > maxalign) + maxalign = align; + +#ifdef BF_DEBUG + printf("set field %s offset %-2d size %-2d align %-2d", + get_tok_str(f->v & ~SYM_FIELD, NULL), offset, size, align); + if (f->type.t & VT_BITFIELD) { + printf(" pos %-2d bits %-2d", + BIT_POS(f->type.t), + BIT_SIZE(f->type.t) + ); + } + printf("\n"); +#endif + + if (f->v & SYM_FIRST_ANOM && (f->type.t & VT_BTYPE) == VT_STRUCT) { + Sym *ass; + /* An anonymous struct/union. Adjust member offsets + to reflect the real offset of our containing struct. + Also set the offset of this anon member inside + the outer struct to be zero. Via this it + works when accessing the field offset directly + (from base object), as well as when recursing + members in initializer handling. */ + int v2 = f->type.ref->v; + if (!(v2 & SYM_FIELD) && + (v2 & ~SYM_STRUCT) < SYM_FIRST_ANOM) { + Sym **pps; + /* This happens only with MS extensions. The + anon member has a named struct type, so it + potentially is shared with other references. + We need to unshare members so we can modify + them. */ + ass = f->type.ref; + f->type.ref = sym_push(anon_sym++ | SYM_FIELD, + &f->type.ref->type, 0, + f->type.ref->c); + pps = &f->type.ref->next; + while ((ass = ass->next) != NULL) { + *pps = sym_push(ass->v, &ass->type, 0, ass->c); + pps = &((*pps)->next); + } + *pps = NULL; + } + struct_add_offset(f->type.ref, offset); + f->c = 0; + } else { + f->c = offset; + } + + f->r = 0; + } + + if (pcc) + c += (bit_pos + 7) >> 3; + + /* store size and alignment */ + a = bt = ad->a.aligned ? 1 << (ad->a.aligned - 1) : 1; + if (a < maxalign) + a = maxalign; + type->ref->r = a; + if (pragma_pack && pragma_pack < maxalign && 0 == pcc) { + /* can happen if individual align for some member was given. In + this case MSVC ignores maxalign when aligning the size */ + a = pragma_pack; + if (a < bt) + a = bt; + } + c = (c + a - 1) & -a; + type->ref->c = c; + +#ifdef BF_DEBUG + printf("struct size %-2d align %-2d\n\n", c, a), fflush(stdout); +#endif + + /* check whether we can access bitfields by their type */ + for (f = type->ref->next; f; f = f->next) { + int s, px, cx, c0; + CType t; + + if (0 == (f->type.t & VT_BITFIELD)) + continue; + f->type.ref = f; + f->auxtype = -1; + bit_size = BIT_SIZE(f->type.t); + if (bit_size == 0) + continue; + bit_pos = BIT_POS(f->type.t); + size = type_size(&f->type, &align); + if (bit_pos + bit_size <= size * 8 && f->c + size <= c) + continue; + + /* try to access the field using a different type */ + c0 = -1, s = align = 1; + for (;;) { + px = f->c * 8 + bit_pos; + cx = (px >> 3) & -align; + px = px - (cx << 3); + if (c0 == cx) + break; + s = (px + bit_size + 7) >> 3; + if (s > 4) { + t.t = VT_LLONG; + } else if (s > 2) { + t.t = VT_INT; + } else if (s > 1) { + t.t = VT_SHORT; + } else { + t.t = VT_BYTE; + } + s = type_size(&t, &align); + c0 = cx; + } + + if (px + bit_size <= s * 8 && cx + s <= c) { + /* update offset and bit position */ + f->c = cx; + bit_pos = px; + f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT)) + | (bit_pos << VT_STRUCT_SHIFT); + if (s != size) + f->auxtype = t.t; +#ifdef BF_DEBUG + printf("FIX field %s offset %-2d size %-2d align %-2d " + "pos %-2d bits %-2d\n", + get_tok_str(f->v & ~SYM_FIELD, NULL), + cx, s, align, px, bit_size); +#endif + } else { + /* fall back to load/store single-byte wise */ + f->auxtype = VT_STRUCT; +#ifdef BF_DEBUG + printf("FIX field %s : load byte-wise\n", + get_tok_str(f->v & ~SYM_FIELD, NULL)); +#endif + } + } +} + +/* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */ +static void struct_decl(CType *type, int u) +{ + int v, c, size, align, flexible; + int bit_size, bsize, bt; + Sym *s, *ss, **ps; + AttributeDef ad, ad1; + CType type1, btype; + + memset(&ad, 0, sizeof ad); + next(); + parse_attribute(&ad); + if (tok != '{') { + v = tok; + next(); + /* struct already defined ? return it */ + if (v < TOK_IDENT) + expect("struct/union/enum name"); + s = struct_find(v); + if (s && (s->sym_scope == local_scope || tok != '{')) { + if (u == s->type.t) + goto do_decl; + if (u == VT_ENUM && IS_ENUM(s->type.t)) + goto do_decl; + tcc_error("redefinition of '%s'", get_tok_str(v, NULL)); + } + } else { + v = anon_sym++; + } + /* Record the original enum/struct/union token. */ + type1.t = u == VT_ENUM ? u | VT_INT | VT_UNSIGNED : u; + type1.ref = NULL; + /* we put an undefined size for struct/union */ + s = sym_push(v | SYM_STRUCT, &type1, 0, -1); + s->r = 0; /* default alignment is zero as gcc */ +do_decl: + type->t = s->type.t; + type->ref = s; + + if (tok == '{') { + next(); + if (s->c != -1) + tcc_error("struct/union/enum already defined"); + /* cannot be empty */ + /* non empty enums are not allowed */ + ps = &s->next; + if (u == VT_ENUM) { + long long ll = 0, pl = 0, nl = 0; + CType t; + t.ref = s; + /* enum symbols have static storage */ + t.t = VT_INT|VT_STATIC|VT_ENUM_VAL; + for(;;) { + v = tok; + if (v < TOK_UIDENT) + expect("identifier"); + ss = sym_find(v); + if (ss && !local_stack) + tcc_error("redefinition of enumerator '%s'", + get_tok_str(v, NULL)); + next(); + if (tok == '=') { + next(); + ll = expr_const64(); + } + ss = sym_push(v, &t, VT_CONST, 0); + ss->enum_val = ll; + *ps = ss, ps = &ss->next; + if (ll < nl) + nl = ll; + if (ll > pl) + pl = ll; + if (tok != ',') + break; + next(); + ll++; + /* NOTE: we accept a trailing comma */ + if (tok == '}') + break; + } + skip('}'); + /* set integral type of the enum */ + t.t = VT_INT; + if (nl >= 0) { + if (pl != (unsigned)pl) + t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG); + t.t |= VT_UNSIGNED; + } else if (pl != (int)pl || nl != (int)nl) + t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG); + s->type.t = type->t = t.t | VT_ENUM; + s->c = 0; + /* set type for enum members */ + for (ss = s->next; ss; ss = ss->next) { + ll = ss->enum_val; + if (ll == (int)ll) /* default is int if it fits */ + continue; + if (t.t & VT_UNSIGNED) { + ss->type.t |= VT_UNSIGNED; + if (ll == (unsigned)ll) + continue; + } + ss->type.t = (ss->type.t & ~VT_BTYPE) + | (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG); + } + } else { + c = 0; + flexible = 0; + while (tok != '}') { + if (!parse_btype(&btype, &ad1)) { + skip(';'); + continue; + } + while (1) { + if (flexible) + tcc_error("flexible array member '%s' not at the end of struct", + get_tok_str(v, NULL)); + bit_size = -1; + v = 0; + type1 = btype; + if (tok != ':') { + if (tok != ';') + type_decl(&type1, &ad1, &v, TYPE_DIRECT); + if (v == 0) { + if ((type1.t & VT_BTYPE) != VT_STRUCT) + expect("identifier"); + else { + int v = btype.ref->v; + if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) { + if (tcc_state->ms_extensions == 0) + expect("identifier"); + } + } + } + if (type_size(&type1, &align) < 0) { + if ((u == VT_STRUCT) && (type1.t & VT_ARRAY) && c) + flexible = 1; + else + tcc_error("field '%s' has incomplete type", + get_tok_str(v, NULL)); + } + if ((type1.t & VT_BTYPE) == VT_FUNC || + (type1.t & VT_STORAGE)) + tcc_error("invalid type for '%s'", + get_tok_str(v, NULL)); + } + if (tok == ':') { + next(); + bit_size = expr_const(); + /* XXX: handle v = 0 case for messages */ + if (bit_size < 0) + tcc_error("negative width in bit-field '%s'", + get_tok_str(v, NULL)); + if (v && bit_size == 0) + tcc_error("zero width for bit-field '%s'", + get_tok_str(v, NULL)); + parse_attribute(&ad1); + } + size = type_size(&type1, &align); + if (bit_size >= 0) { + bt = type1.t & VT_BTYPE; + if (bt != VT_INT && + bt != VT_BYTE && + bt != VT_SHORT && + bt != VT_BOOL && + bt != VT_LLONG) + tcc_error("bitfields must have scalar type"); + bsize = size * 8; + if (bit_size > bsize) { + tcc_error("width of '%s' exceeds its type", + get_tok_str(v, NULL)); + } else if (bit_size == bsize + && !ad.a.packed && !ad1.a.packed) { + /* no need for bit fields */ + ; + } else if (bit_size == 64) { + tcc_error("field width 64 not implemented"); + } else { + type1.t = (type1.t & ~VT_STRUCT_MASK) + | VT_BITFIELD + | (bit_size << (VT_STRUCT_SHIFT + 6)); + } + } + if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) { + /* Remember we've seen a real field to check + for placement of flexible array member. */ + c = 1; + } + /* If member is a struct or bit-field, enforce + placing into the struct (as anonymous). */ + if (v == 0 && + ((type1.t & VT_BTYPE) == VT_STRUCT || + bit_size >= 0)) { + v = anon_sym++; + } + if (v) { + ss = sym_push(v | SYM_FIELD, &type1, 0, 0); + ss->a = ad1.a; + *ps = ss; + ps = &ss->next; + } + if (tok == ';' || tok == TOK_EOF) + break; + skip(','); + } + skip(';'); + } + skip('}'); + parse_attribute(&ad); + struct_layout(type, &ad); + } + } +} + +static void sym_to_attr(AttributeDef *ad, Sym *s) +{ + if (s->a.aligned && 0 == ad->a.aligned) + ad->a.aligned = s->a.aligned; + if (s->f.func_call && 0 == ad->f.func_call) + ad->f.func_call = s->f.func_call; + if (s->f.func_type && 0 == ad->f.func_type) + ad->f.func_type = s->f.func_type; + if (s->a.packed) + ad->a.packed = 1; +} + +/* Add type qualifiers to a type. If the type is an array then the qualifiers + are added to the element type, copied because it could be a typedef. */ +static void parse_btype_qualify(CType *type, int qualifiers) +{ + while (type->t & VT_ARRAY) { + type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c); + type = &type->ref->type; + } + type->t |= qualifiers; +} + +/* return 0 if no type declaration. otherwise, return the basic type + and skip it. + */ +static int parse_btype(CType *type, AttributeDef *ad) +{ + int t, u, bt, st, type_found, typespec_found, g; + Sym *s; + CType type1; + + memset(ad, 0, sizeof(AttributeDef)); + type_found = 0; + typespec_found = 0; + t = VT_INT; + bt = st = -1; + type->ref = NULL; + + while(1) { + switch(tok) { + case TOK_EXTENSION: + /* currently, we really ignore extension */ + next(); + continue; + + /* basic types */ + case TOK_CHAR: + u = VT_BYTE; + basic_type: + next(); + basic_type1: + if (u == VT_SHORT || u == VT_LONG) { + if (st != -1 || (bt != -1 && bt != VT_INT)) + tmbt: tcc_error("too many basic types"); + st = u; + } else { + if (bt != -1 || (st != -1 && u != VT_INT)) + goto tmbt; + bt = u; + } + if (u != VT_INT) + t = (t & ~(VT_BTYPE|VT_LONG)) | u; + typespec_found = 1; + break; + case TOK_VOID: + u = VT_VOID; + goto basic_type; + case TOK_SHORT: + u = VT_SHORT; + goto basic_type; + case TOK_INT: + u = VT_INT; + goto basic_type; + case TOK_LONG: + if ((t & VT_BTYPE) == VT_DOUBLE) { + t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE; + } else if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) { + t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LLONG; + } else { + u = VT_LONG; + goto basic_type; + } + next(); + break; +#ifdef TCC_TARGET_ARM64 + case TOK_UINT128: + /* GCC's __uint128_t appears in some Linux header files. Make it a + synonym for long double to get the size and alignment right. */ + u = VT_LDOUBLE; + goto basic_type; +#endif + case TOK_BOOL: + u = VT_BOOL; + goto basic_type; + case TOK_FLOAT: + u = VT_FLOAT; + goto basic_type; + case TOK_DOUBLE: + if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) { + t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE; + } else { + u = VT_DOUBLE; + goto basic_type; + } + next(); + break; + case TOK_ENUM: + struct_decl(&type1, VT_ENUM); + basic_type2: + u = type1.t; + type->ref = type1.ref; + goto basic_type1; + case TOK_STRUCT: + struct_decl(&type1, VT_STRUCT); + goto basic_type2; + case TOK_UNION: + struct_decl(&type1, VT_UNION); + goto basic_type2; + + /* type modifiers */ + case TOK_CONST1: + case TOK_CONST2: + case TOK_CONST3: + type->t = t; + parse_btype_qualify(type, VT_CONSTANT); + t = type->t; + next(); + break; + case TOK_VOLATILE1: + case TOK_VOLATILE2: + case TOK_VOLATILE3: + type->t = t; + parse_btype_qualify(type, VT_VOLATILE); + t = type->t; + next(); + break; + case TOK_SIGNED1: + case TOK_SIGNED2: + case TOK_SIGNED3: + if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED)) + tcc_error("signed and unsigned modifier"); + t |= VT_DEFSIGN; + next(); + typespec_found = 1; + break; + case TOK_REGISTER: + case TOK_AUTO: + case TOK_RESTRICT1: + case TOK_RESTRICT2: + case TOK_RESTRICT3: + next(); + break; + case TOK_UNSIGNED: + if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN) + tcc_error("signed and unsigned modifier"); + t |= VT_DEFSIGN | VT_UNSIGNED; + next(); + typespec_found = 1; + break; + + /* storage */ + case TOK_EXTERN: + g = VT_EXTERN; + goto storage; + case TOK_STATIC: + g = VT_STATIC; + goto storage; + case TOK_TYPEDEF: + g = VT_TYPEDEF; + goto storage; + storage: + if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g) + tcc_error("multiple storage classes"); + t |= g; + next(); + break; + case TOK_INLINE1: + case TOK_INLINE2: + case TOK_INLINE3: + t |= VT_INLINE; + next(); + break; + + /* GNUC attribute */ + case TOK_ATTRIBUTE1: + case TOK_ATTRIBUTE2: + parse_attribute(ad); + if (ad->attr_mode) { + u = ad->attr_mode -1; + t = (t & ~(VT_BTYPE|VT_LONG)) | u; + } + break; + /* GNUC typeof */ + case TOK_TYPEOF1: + case TOK_TYPEOF2: + case TOK_TYPEOF3: + next(); + parse_expr_type(&type1); + /* remove all storage modifiers except typedef */ + type1.t &= ~(VT_STORAGE&~VT_TYPEDEF); + if (type1.ref) + sym_to_attr(ad, type1.ref); + goto basic_type2; + default: + if (typespec_found) + goto the_end; + s = sym_find(tok); + if (!s || !(s->type.t & VT_TYPEDEF)) + goto the_end; + t &= ~(VT_BTYPE|VT_LONG); + u = t & ~(VT_CONSTANT | VT_VOLATILE), t ^= u; + type->t = (s->type.t & ~VT_TYPEDEF) | u; + type->ref = s->type.ref; + if (t) + parse_btype_qualify(type, t); + t = type->t; + /* get attributes from typedef */ + sym_to_attr(ad, s); + next(); + typespec_found = 1; + st = bt = -2; + break; + } + type_found = 1; + } +the_end: + if (tcc_state->char_is_unsigned) { + if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE) + t |= VT_UNSIGNED; + } + /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */ + bt = t & (VT_BTYPE|VT_LONG); + if (bt == VT_LONG) + t |= LONG_SIZE == 8 ? VT_LLONG : VT_INT; +#ifdef TCC_TARGET_PE + if (bt == VT_LDOUBLE) + t = (t & ~(VT_BTYPE|VT_LONG)) | VT_DOUBLE; +#endif + type->t = t; + return type_found; +} + +/* convert a function parameter type (array to pointer and function to + function pointer) */ +static inline void convert_parameter_type(CType *pt) +{ + /* remove const and volatile qualifiers (XXX: const could be used + to indicate a const function parameter */ + pt->t &= ~(VT_CONSTANT | VT_VOLATILE); + /* array must be transformed to pointer according to ANSI C */ + pt->t &= ~VT_ARRAY; + if ((pt->t & VT_BTYPE) == VT_FUNC) { + mk_pointer(pt); + } +} + +ST_FUNC void parse_asm_str(CString *astr) +{ + skip('('); + parse_mult_str(astr, "string constant"); +} + +/* Parse an asm label and return the token */ +static int asm_label_instr(void) +{ + int v; + CString astr; + + next(); + parse_asm_str(&astr); + skip(')'); +#ifdef ASM_DEBUG + printf("asm_alias: \"%s\"\n", (char *)astr.data); +#endif + v = tok_alloc(astr.data, astr.size - 1)->tok; + cstr_free(&astr); + return v; +} + +static int post_type(CType *type, AttributeDef *ad, int storage, int td) +{ + int n, l, t1, arg_size, align; + Sym **plast, *s, *first; + AttributeDef ad1; + CType pt; + + if (tok == '(') { + /* function type, or recursive declarator (return if so) */ + next(); + if (td && !(td & TYPE_ABSTRACT)) + return 0; + if (tok == ')') + l = 0; + else if (parse_btype(&pt, &ad1)) + l = FUNC_NEW; + else if (td) + return 0; + else + l = FUNC_OLD; + first = NULL; + plast = &first; + arg_size = 0; + if (l) { + for(;;) { + /* read param name and compute offset */ + if (l != FUNC_OLD) { + if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')') + break; + type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT); + if ((pt.t & VT_BTYPE) == VT_VOID) + tcc_error("parameter declared as void"); + arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE; + } else { + n = tok; + if (n < TOK_UIDENT) + expect("identifier"); + pt.t = VT_VOID; /* invalid type */ + next(); + } + convert_parameter_type(&pt); + s = sym_push(n | SYM_FIELD, &pt, 0, 0); + *plast = s; + plast = &s->next; + if (tok == ')') + break; + skip(','); + if (l == FUNC_NEW && tok == TOK_DOTS) { + l = FUNC_ELLIPSIS; + next(); + break; + } + if (l == FUNC_NEW && !parse_btype(&pt, &ad1)) + tcc_error("invalid type"); + } + } else + /* if no parameters, then old type prototype */ + l = FUNC_OLD; + skip(')'); + /* NOTE: const is ignored in returned type as it has a special + meaning in gcc / C++ */ + type->t &= ~VT_CONSTANT; + /* some ancient pre-K&R C allows a function to return an array + and the array brackets to be put after the arguments, such + that "int c()[]" means something like "int[] c()" */ + if (tok == '[') { + next(); + skip(']'); /* only handle simple "[]" */ + mk_pointer(type); + } + /* we push a anonymous symbol which will contain the function prototype */ + ad->f.func_args = arg_size; + ad->f.func_type = l; + s = sym_push(SYM_FIELD, type, 0, 0); + s->a = ad->a; + s->f = ad->f; + s->next = first; + type->t = VT_FUNC; + type->ref = s; + } else if (tok == '[') { + int saved_nocode_wanted = nocode_wanted; + /* array definition */ + next(); + if (tok == TOK_RESTRICT1) + next(); + n = -1; + t1 = 0; + if (tok != ']') { + if (!local_stack || (storage & VT_STATIC)) + vpushi(expr_const()); + else { + /* VLAs (which can only happen with local_stack && !VT_STATIC) + length must always be evaluated, even under nocode_wanted, + so that its size slot is initialized (e.g. under sizeof + or typeof). */ + nocode_wanted = 0; + gexpr(); + } + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + n = vtop->c.i; + if (n < 0) + tcc_error("invalid array size"); + } else { + if (!is_integer_btype(vtop->type.t & VT_BTYPE)) + tcc_error("size of variable length array should be an integer"); + t1 = VT_VLA; + } + } + skip(']'); + /* parse next post type */ + post_type(type, ad, storage, 0); + if (type->t == VT_FUNC) + tcc_error("declaration of an array of functions"); + t1 |= type->t & VT_VLA; + + if (t1 & VT_VLA) { + loc -= type_size(&int_type, &align); + loc &= -align; + n = loc; + + vla_runtime_type_size(type, &align); + gen_op('*'); + vset(&int_type, VT_LOCAL|VT_LVAL, n); + vswap(); + vstore(); + } + if (n != -1) + vpop(); + nocode_wanted = saved_nocode_wanted; + + /* we push an anonymous symbol which will contain the array + element type */ + s = sym_push(SYM_FIELD, type, 0, n); + type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR; + type->ref = s; + } + return 1; +} + +/* Parse a type declarator (except basic type), and return the type + in 'type'. 'td' is a bitmask indicating which kind of type decl is + expected. 'type' should contain the basic type. 'ad' is the + attribute definition of the basic type. It can be modified by + type_decl(). If this (possibly abstract) declarator is a pointer chain + it returns the innermost pointed to type (equals *type, but is a different + pointer), otherwise returns type itself, that's used for recursive calls. */ +static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td) +{ + CType *post, *ret; + int qualifiers, storage; + + /* recursive type, remove storage bits first, apply them later again */ + storage = type->t & VT_STORAGE; + type->t &= ~VT_STORAGE; + post = ret = type; + + while (tok == '*') { + qualifiers = 0; + redo: + next(); + switch(tok) { + case TOK_CONST1: + case TOK_CONST2: + case TOK_CONST3: + qualifiers |= VT_CONSTANT; + goto redo; + case TOK_VOLATILE1: + case TOK_VOLATILE2: + case TOK_VOLATILE3: + qualifiers |= VT_VOLATILE; + goto redo; + case TOK_RESTRICT1: + case TOK_RESTRICT2: + case TOK_RESTRICT3: + goto redo; + /* XXX: clarify attribute handling */ + case TOK_ATTRIBUTE1: + case TOK_ATTRIBUTE2: + parse_attribute(ad); + break; + } + mk_pointer(type); + type->t |= qualifiers; + if (ret == type) + /* innermost pointed to type is the one for the first derivation */ + ret = pointed_type(type); + } + + if (tok == '(') { + /* This is possibly a parameter type list for abstract declarators + ('int ()'), use post_type for testing this. */ + if (!post_type(type, ad, 0, td)) { + /* It's not, so it's a nested declarator, and the post operations + apply to the innermost pointed to type (if any). */ + /* XXX: this is not correct to modify 'ad' at this point, but + the syntax is not clear */ + parse_attribute(ad); + post = type_decl(type, ad, v, td); + skip(')'); + } + } else if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) { + /* type identifier */ + *v = tok; + next(); + } else { + if (!(td & TYPE_ABSTRACT)) + expect("identifier"); + *v = 0; + } + post_type(post, ad, storage, 0); + parse_attribute(ad); + type->t |= storage; + return ret; +} + +/* compute the lvalue VT_LVAL_xxx needed to match type t. */ +ST_FUNC int lvalue_type(int t) +{ + int bt, r; + r = VT_LVAL; + bt = t & VT_BTYPE; + if (bt == VT_BYTE || bt == VT_BOOL) + r |= VT_LVAL_BYTE; + else if (bt == VT_SHORT) + r |= VT_LVAL_SHORT; + else + return r; + if (t & VT_UNSIGNED) + r |= VT_LVAL_UNSIGNED; + return r; +} + +/* indirection with full error checking and bound check */ +ST_FUNC void indir(void) +{ + if ((vtop->type.t & VT_BTYPE) != VT_PTR) { + if ((vtop->type.t & VT_BTYPE) == VT_FUNC) + return; + expect("pointer"); + } + if (vtop->r & VT_LVAL) + gv(RC_INT); + vtop->type = *pointed_type(&vtop->type); + /* Arrays and functions are never lvalues */ + if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA) + && (vtop->type.t & VT_BTYPE) != VT_FUNC) { + vtop->r |= lvalue_type(vtop->type.t); + /* if bound checking, the referenced pointer must be checked */ +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + vtop->r |= VT_MUSTBOUND; +#endif + } +} + +/* pass a parameter to a function and do type checking and casting */ +static void gfunc_param_typed(Sym *func, Sym *arg) +{ + int func_type; + CType type; + + func_type = func->f.func_type; + if (func_type == FUNC_OLD || + (func_type == FUNC_ELLIPSIS && arg == NULL)) { + /* default casting : only need to convert float to double */ + if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) { + gen_cast_s(VT_DOUBLE); + } else if (vtop->type.t & VT_BITFIELD) { + type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED); + type.ref = vtop->type.ref; + gen_cast(&type); + } + } else if (arg == NULL) { + tcc_error("too many arguments to function"); + } else { + type = arg->type; + type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */ + gen_assign_cast(&type); + } +} + +/* parse an expression and return its type without any side effect. */ +static void expr_type(CType *type, void (*expr_fn)(void)) +{ + nocode_wanted++; + expr_fn(); + *type = vtop->type; + vpop(); + nocode_wanted--; +} + +/* parse an expression of the form '(type)' or '(expr)' and return its + type */ +static void parse_expr_type(CType *type) +{ + int n; + AttributeDef ad; + + skip('('); + if (parse_btype(type, &ad)) { + type_decl(type, &ad, &n, TYPE_ABSTRACT); + } else { + expr_type(type, gexpr); + } + skip(')'); +} + +static void parse_type(CType *type) +{ + AttributeDef ad; + int n; + + if (!parse_btype(type, &ad)) { + expect("type"); + } + type_decl(type, &ad, &n, TYPE_ABSTRACT); +} + +static void parse_builtin_params(int nc, const char *args) +{ + char c, sep = '('; + CType t; + if (nc) + nocode_wanted++; + next(); + while ((c = *args++)) { + skip(sep); + sep = ','; + switch (c) { + case 'e': expr_eq(); continue; + case 't': parse_type(&t); vpush(&t); continue; + default: tcc_error("internal error"); break; + } + } + skip(')'); + if (nc) + nocode_wanted--; +} + +ST_FUNC void unary(void) +{ + int n, t, align, size, r, sizeof_caller; + CType type; + Sym *s; + AttributeDef ad; + + sizeof_caller = in_sizeof; + in_sizeof = 0; + type.ref = NULL; + /* XXX: GCC 2.95.3 does not generate a table although it should be + better here */ + tok_next: + switch(tok) { + case TOK_EXTENSION: + next(); + goto tok_next; + case TOK_LCHAR: +#ifdef TCC_TARGET_PE + t = VT_SHORT|VT_UNSIGNED; + goto push_tokc; +#endif + case TOK_CINT: + case TOK_CCHAR: + t = VT_INT; + push_tokc: + type.t = t; + vsetc(&type, VT_CONST, &tokc); + next(); + break; + case TOK_CUINT: + t = VT_INT | VT_UNSIGNED; + goto push_tokc; + case TOK_CLLONG: + t = VT_LLONG; + goto push_tokc; + case TOK_CULLONG: + t = VT_LLONG | VT_UNSIGNED; + goto push_tokc; + case TOK_CFLOAT: + t = VT_FLOAT; + goto push_tokc; + case TOK_CDOUBLE: + t = VT_DOUBLE; + goto push_tokc; + case TOK_CLDOUBLE: + t = VT_LDOUBLE; + goto push_tokc; + case TOK_CLONG: + t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG; + goto push_tokc; + case TOK_CULONG: + t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG | VT_UNSIGNED; + goto push_tokc; + case TOK___FUNCTION__: + if (!gnu_ext) + goto tok_identifier; + /* fall thru */ + case TOK___FUNC__: + { + void *ptr; + int len; + /* special function name identifier */ + len = strlen(funcname) + 1; + /* generate char[len] type */ + type.t = VT_BYTE; + mk_pointer(&type); + type.t |= VT_ARRAY; + type.ref->c = len; + vpush_ref(&type, data_section, data_section->data_offset, len); + if (!NODATA_WANTED) { + ptr = section_ptr_add(data_section, len); + memcpy(ptr, funcname, len); + } + next(); + } + break; + case TOK_LSTR: +#ifdef TCC_TARGET_PE + t = VT_SHORT | VT_UNSIGNED; +#else + t = VT_INT; +#endif + goto str_init; + case TOK_STR: + /* string parsing */ + t = VT_BYTE; + if (tcc_state->char_is_unsigned) + t = VT_BYTE | VT_UNSIGNED; + str_init: + if (tcc_state->warn_write_strings) + t |= VT_CONSTANT; + type.t = t; + mk_pointer(&type); + type.t |= VT_ARRAY; + memset(&ad, 0, sizeof(AttributeDef)); + decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0); + break; + case '(': + next(); + /* cast ? */ + if (parse_btype(&type, &ad)) { + type_decl(&type, &ad, &n, TYPE_ABSTRACT); + skip(')'); + /* check ISOC99 compound literal */ + if (tok == '{') { + /* data is allocated locally by default */ + if (global_expr) + r = VT_CONST; + else + r = VT_LOCAL; + /* all except arrays are lvalues */ + if (!(type.t & VT_ARRAY)) + r |= lvalue_type(type.t); + memset(&ad, 0, sizeof(AttributeDef)); + decl_initializer_alloc(&type, &ad, r, 1, 0, 0); + } else { + if (sizeof_caller) { + vpush(&type); + return; + } + unary(); + gen_cast(&type); + } + } else if (tok == '{') { + int saved_nocode_wanted = nocode_wanted; + if (const_wanted) + tcc_error("expected constant"); + /* save all registers */ + save_regs(0); + /* statement expression : we do not accept break/continue + inside as GCC does. We do retain the nocode_wanted state, + as statement expressions can't ever be entered from the + outside, so any reactivation of code emission (from labels + or loop heads) can be disabled again after the end of it. */ + block(NULL, NULL, 1); + nocode_wanted = saved_nocode_wanted; + skip(')'); + } else { + gexpr(); + skip(')'); + } + break; + case '*': + next(); + unary(); + indir(); + break; + case '&': + next(); + unary(); + /* functions names must be treated as function pointers, + except for unary '&' and sizeof. Since we consider that + functions are not lvalues, we only have to handle it + there and in function calls. */ + /* arrays can also be used although they are not lvalues */ + if ((vtop->type.t & VT_BTYPE) != VT_FUNC && + !(vtop->type.t & VT_ARRAY)) + test_lvalue(); + mk_pointer(&vtop->type); + gaddrof(); + break; + case '!': + next(); + unary(); + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + gen_cast_s(VT_BOOL); + vtop->c.i = !vtop->c.i; + } else if ((vtop->r & VT_VALMASK) == VT_CMP) + vtop->c.i ^= 1; + else { + save_regs(1); + vseti(VT_JMP, gvtst(1, 0)); + } + break; + case '~': + next(); + unary(); + vpushi(-1); + gen_op('^'); + break; + case '+': + next(); + unary(); + if ((vtop->type.t & VT_BTYPE) == VT_PTR) + tcc_error("pointer not accepted for unary plus"); + /* In order to force cast, we add zero, except for floating point + where we really need an noop (otherwise -0.0 will be transformed + into +0.0). */ + if (!is_float(vtop->type.t)) { + vpushi(0); + gen_op('+'); + } + break; + case TOK_SIZEOF: + case TOK_ALIGNOF1: + case TOK_ALIGNOF2: + t = tok; + next(); + in_sizeof++; + expr_type(&type, unary); /* Perform a in_sizeof = 0; */ + s = vtop[1].sym; /* hack: accessing previous vtop */ + size = type_size(&type, &align); + if (s && s->a.aligned) + align = 1 << (s->a.aligned - 1); + if (t == TOK_SIZEOF) { + if (!(type.t & VT_VLA)) { + if (size < 0) + tcc_error("sizeof applied to an incomplete type"); + vpushs(size); + } else { + vla_runtime_type_size(&type, &align); + } + } else { + vpushs(align); + } + vtop->type.t |= VT_UNSIGNED; + break; + + case TOK_builtin_expect: + /* __builtin_expect is a no-op for now */ + parse_builtin_params(0, "ee"); + vpop(); + break; + case TOK_builtin_types_compatible_p: + parse_builtin_params(0, "tt"); + vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE); + vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE); + n = is_compatible_types(&vtop[-1].type, &vtop[0].type); + vtop -= 2; + vpushi(n); + break; + case TOK_builtin_choose_expr: + { + int64_t c; + next(); + skip('('); + c = expr_const64(); + skip(','); + if (!c) { + nocode_wanted++; + } + expr_eq(); + if (!c) { + vpop(); + nocode_wanted--; + } + skip(','); + if (c) { + nocode_wanted++; + } + expr_eq(); + if (c) { + vpop(); + nocode_wanted--; + } + skip(')'); + } + break; + case TOK_builtin_constant_p: + parse_builtin_params(1, "e"); + n = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + vtop--; + vpushi(n); + break; + case TOK_builtin_frame_address: + case TOK_builtin_return_address: + { + int tok1 = tok; + int level; + next(); + skip('('); + if (tok != TOK_CINT) { + tcc_error("%s only takes positive integers", + tok1 == TOK_builtin_return_address ? + "__builtin_return_address" : + "__builtin_frame_address"); + } + level = (uint32_t)tokc.i; + next(); + skip(')'); + type.t = VT_VOID; + mk_pointer(&type); + vset(&type, VT_LOCAL, 0); /* local frame */ + while (level--) { + mk_pointer(&vtop->type); + indir(); /* -> parent frame */ + } + if (tok1 == TOK_builtin_return_address) { + // assume return address is just above frame pointer on stack + vpushi(PTR_SIZE); + gen_op('+'); + mk_pointer(&vtop->type); + indir(); + } + } + break; +#ifdef TCC_TARGET_X86_64 +#ifdef TCC_TARGET_PE + case TOK_builtin_va_start: + parse_builtin_params(0, "ee"); + r = vtop->r & VT_VALMASK; + if (r == VT_LLOCAL) + r = VT_LOCAL; + if (r != VT_LOCAL) + tcc_error("__builtin_va_start expects a local variable"); + vtop->r = r; + vtop->type = char_pointer_type; + vtop->c.i += 8; + vstore(); + break; +#else + case TOK_builtin_va_arg_types: + parse_builtin_params(0, "t"); + vpushi(classify_x86_64_va_arg(&vtop->type)); + vswap(); + vpop(); + break; +#endif +#endif + +#ifdef TCC_TARGET_ARM64 + case TOK___va_start: { + parse_builtin_params(0, "ee"); + //xx check types + gen_va_start(); + vpushi(0); + vtop->type.t = VT_VOID; + break; + } + case TOK___va_arg: { + parse_builtin_params(0, "et"); + type = vtop->type; + vpop(); + //xx check types + gen_va_arg(&type); + vtop->type = type; + break; + } + case TOK___arm64_clear_cache: { + parse_builtin_params(0, "ee"); + gen_clear_cache(); + vpushi(0); + vtop->type.t = VT_VOID; + break; + } +#endif + /* pre operations */ + case TOK_INC: + case TOK_DEC: + t = tok; + next(); + unary(); + inc(0, t); + break; + case '-': + next(); + unary(); + t = vtop->type.t & VT_BTYPE; + if (is_float(t)) { + /* In IEEE negate(x) isn't subtract(0,x), but rather + subtract(-0, x). */ + vpush(&vtop->type); + if (t == VT_FLOAT) + vtop->c.f = -1.0 * 0.0; + else if (t == VT_DOUBLE) + vtop->c.d = -1.0 * 0.0; + else + vtop->c.ld = -1.0 * 0.0; + } else + vpushi(0); + vswap(); + gen_op('-'); + break; + case TOK_LAND: + if (!gnu_ext) + goto tok_identifier; + next(); + /* allow to take the address of a label */ + if (tok < TOK_UIDENT) + expect("label identifier"); + s = label_find(tok); + if (!s) { + s = label_push(&global_label_stack, tok, LABEL_FORWARD); + } else { + if (s->r == LABEL_DECLARED) + s->r = LABEL_FORWARD; + } + if (!s->type.t) { + s->type.t = VT_VOID; + mk_pointer(&s->type); + s->type.t |= VT_STATIC; + } + vpushsym(&s->type, s); + next(); + break; + + case TOK_GENERIC: + { + CType controlling_type; + int has_default = 0; + int has_match = 0; + int learn = 0; + TokenString *str = NULL; + + next(); + skip('('); + expr_type(&controlling_type, expr_eq); + controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY); + for (;;) { + learn = 0; + skip(','); + if (tok == TOK_DEFAULT) { + if (has_default) + tcc_error("too many 'default'"); + has_default = 1; + if (!has_match) + learn = 1; + next(); + } else { + AttributeDef ad_tmp; + int itmp; + CType cur_type; + parse_btype(&cur_type, &ad_tmp); + type_decl(&cur_type, &ad_tmp, &itmp, TYPE_ABSTRACT); + if (compare_types(&controlling_type, &cur_type, 0)) { + if (has_match) { + tcc_error("type match twice"); + } + has_match = 1; + learn = 1; + } + } + skip(':'); + if (learn) { + if (str) + tok_str_free(str); + skip_or_save_block(&str); + } else { + skip_or_save_block(NULL); + } + if (tok == ')') + break; + } + if (!str) { + char buf[60]; + type_to_str(buf, sizeof buf, &controlling_type, NULL); + tcc_error("type '%s' does not match any association", buf); + } + begin_macro(str, 1); + next(); + expr_eq(); + if (tok != TOK_EOF) + expect(","); + end_macro(); + next(); + break; + } + // special qnan , snan and infinity values + case TOK___NAN__: + vpush64(VT_DOUBLE, 0x7ff8000000000000ULL); + next(); + break; + case TOK___SNAN__: + vpush64(VT_DOUBLE, 0x7ff0000000000001ULL); + next(); + break; + case TOK___INF__: + vpush64(VT_DOUBLE, 0x7ff0000000000000ULL); + next(); + break; + + default: + tok_identifier: + t = tok; + next(); + if (t < TOK_UIDENT) + expect("identifier"); + s = sym_find(t); + if (!s || IS_ASM_SYM(s)) { + const char *name = get_tok_str(t, NULL); + if (tok != '(') + tcc_error("'%s' undeclared", name); + /* for simple function calls, we tolerate undeclared + external reference to int() function */ + if (tcc_state->warn_implicit_function_declaration +#ifdef TCC_TARGET_PE + /* people must be warned about using undeclared WINAPI functions + (which usually start with uppercase letter) */ + || (name[0] >= 'A' && name[0] <= 'Z') +#endif + ) + tcc_warning("implicit declaration of function '%s'", name); + s = external_global_sym(t, &func_old_type, 0); + } + + r = s->r; + /* A symbol that has a register is a local register variable, + which starts out as VT_LOCAL value. */ + if ((r & VT_VALMASK) < VT_CONST) + r = (r & ~VT_VALMASK) | VT_LOCAL; + + vset(&s->type, r, s->c); + /* Point to s as backpointer (even without r&VT_SYM). + Will be used by at least the x86 inline asm parser for + regvars. */ + vtop->sym = s; + + if (r & VT_SYM) { + vtop->c.i = 0; + } else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) { + vtop->c.i = s->enum_val; + } + break; + } + + /* post operations */ + while (1) { + if (tok == TOK_INC || tok == TOK_DEC) { + inc(1, tok); + next(); + } else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) { + int qualifiers; + /* field */ + if (tok == TOK_ARROW) + indir(); + qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE); + test_lvalue(); + gaddrof(); + /* expect pointer on structure */ + if ((vtop->type.t & VT_BTYPE) != VT_STRUCT) + expect("struct or union"); + if (tok == TOK_CDOUBLE) + expect("field name"); + next(); + if (tok == TOK_CINT || tok == TOK_CUINT) + expect("field name"); + s = find_field(&vtop->type, tok); + if (!s) + tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc)); + /* add field offset to pointer */ + vtop->type = char_pointer_type; /* change type to 'char *' */ + vpushi(s->c); + gen_op('+'); + /* change type to field type, and set to lvalue */ + vtop->type = s->type; + vtop->type.t |= qualifiers; + /* an array is never an lvalue */ + if (!(vtop->type.t & VT_ARRAY)) { + vtop->r |= lvalue_type(vtop->type.t); +#ifdef CONFIG_TCC_BCHECK + /* if bound checking, the referenced pointer must be checked */ + if (tcc_state->do_bounds_check && (vtop->r & VT_VALMASK) != VT_LOCAL) + vtop->r |= VT_MUSTBOUND; +#endif + } + next(); + } else if (tok == '[') { + next(); + gexpr(); + gen_op('+'); + indir(); + skip(']'); + } else if (tok == '(') { + SValue ret; + Sym *sa; + int nb_args, ret_nregs, ret_align, regsize, variadic; + + /* function call */ + if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { + /* pointer test (no array accepted) */ + if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) { + vtop->type = *pointed_type(&vtop->type); + if ((vtop->type.t & VT_BTYPE) != VT_FUNC) + goto error_func; + } else { + error_func: + expect("function pointer"); + } + } else { + vtop->r &= ~VT_LVAL; /* no lvalue */ + } + /* get return type */ + s = vtop->type.ref; + next(); + sa = s->next; /* first parameter */ + nb_args = regsize = 0; + ret.r2 = VT_CONST; + /* compute first implicit argument if a structure is returned */ + if ((s->type.t & VT_BTYPE) == VT_STRUCT) { + variadic = (s->f.func_type == FUNC_ELLIPSIS); + ret_nregs = gfunc_sret(&s->type, variadic, &ret.type, + &ret_align, ®size); + if (!ret_nregs) { + /* get some space for the returned structure */ + size = type_size(&s->type, &align); +#ifdef TCC_TARGET_ARM64 + /* On arm64, a small struct is return in registers. + It is much easier to write it to memory if we know + that we are allowed to write some extra bytes, so + round the allocated space up to a power of 2: */ + if (size < 16) + while (size & (size - 1)) + size = (size | (size - 1)) + 1; +#endif + loc = (loc - size) & -align; + ret.type = s->type; + ret.r = VT_LOCAL | VT_LVAL; + /* pass it as 'int' to avoid structure arg passing + problems */ + vseti(VT_LOCAL, loc); + ret.c = vtop->c; + nb_args++; + } + } else { + ret_nregs = 1; + ret.type = s->type; + } + + if (ret_nregs) { + /* return in register */ + if (is_float(ret.type.t)) { + ret.r = reg_fret(ret.type.t); +#ifdef TCC_TARGET_X86_64 + if ((ret.type.t & VT_BTYPE) == VT_QFLOAT) + ret.r2 = REG_QRET; +#endif + } else { +#ifndef TCC_TARGET_ARM64 +#ifdef TCC_TARGET_X86_64 + if ((ret.type.t & VT_BTYPE) == VT_QLONG) +#else + if ((ret.type.t & VT_BTYPE) == VT_LLONG) +#endif + ret.r2 = REG_LRET; +#endif + ret.r = REG_IRET; + } + ret.c.i = 0; + } + if (tok != ')') { + for(;;) { + expr_eq(); + gfunc_param_typed(s, sa); + nb_args++; + if (sa) + sa = sa->next; + if (tok == ')') + break; + skip(','); + } + } + if (sa) + tcc_error("too few arguments to function"); + skip(')'); + gfunc_call(nb_args); + + /* return value */ + for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) { + vsetc(&ret.type, r, &ret.c); + vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */ + } + + /* handle packed struct return */ + if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) { + int addr, offset; + + size = type_size(&s->type, &align); + /* We're writing whole regs often, make sure there's enough + space. Assume register size is power of 2. */ + if (regsize > align) + align = regsize; + loc = (loc - size) & -align; + addr = loc; + offset = 0; + for (;;) { + vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset); + vswap(); + vstore(); + vtop--; + if (--ret_nregs == 0) + break; + offset += regsize; + } + vset(&s->type, VT_LOCAL | VT_LVAL, addr); + } + } else { + break; + } + } +} + +ST_FUNC void expr_prod(void) +{ + int t; + + unary(); + while (tok == '*' || tok == '/' || tok == '%') { + t = tok; + next(); + unary(); + gen_op(t); + } +} + +ST_FUNC void expr_sum(void) +{ + int t; + + expr_prod(); + while (tok == '+' || tok == '-') { + t = tok; + next(); + expr_prod(); + gen_op(t); + } +} + +static void expr_shift(void) +{ + int t; + + expr_sum(); + while (tok == TOK_SHL || tok == TOK_SAR) { + t = tok; + next(); + expr_sum(); + gen_op(t); + } +} + +static void expr_cmp(void) +{ + int t; + + expr_shift(); + while ((tok >= TOK_ULE && tok <= TOK_GT) || + tok == TOK_ULT || tok == TOK_UGE) { + t = tok; + next(); + expr_shift(); + gen_op(t); + } +} + +static void expr_cmpeq(void) +{ + int t; + + expr_cmp(); + while (tok == TOK_EQ || tok == TOK_NE) { + t = tok; + next(); + expr_cmp(); + gen_op(t); + } +} + +static void expr_and(void) +{ + expr_cmpeq(); + while (tok == '&') { + next(); + expr_cmpeq(); + gen_op('&'); + } +} + +static void expr_xor(void) +{ + expr_and(); + while (tok == '^') { + next(); + expr_and(); + gen_op('^'); + } +} + +static void expr_or(void) +{ + expr_xor(); + while (tok == '|') { + next(); + expr_xor(); + gen_op('|'); + } +} + +static void expr_land(void) +{ + expr_or(); + if (tok == TOK_LAND) { + int t = 0; + for(;;) { + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + gen_cast_s(VT_BOOL); + if (vtop->c.i) { + vpop(); + } else { + nocode_wanted++; + while (tok == TOK_LAND) { + next(); + expr_or(); + vpop(); + } + nocode_wanted--; + if (t) + gsym(t); + gen_cast_s(VT_INT); + break; + } + } else { + if (!t) + save_regs(1); + t = gvtst(1, t); + } + if (tok != TOK_LAND) { + if (t) + vseti(VT_JMPI, t); + else + vpushi(1); + break; + } + next(); + expr_or(); + } + } +} + +static void expr_lor(void) +{ + expr_land(); + if (tok == TOK_LOR) { + int t = 0; + for(;;) { + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + gen_cast_s(VT_BOOL); + if (!vtop->c.i) { + vpop(); + } else { + nocode_wanted++; + while (tok == TOK_LOR) { + next(); + expr_land(); + vpop(); + } + nocode_wanted--; + if (t) + gsym(t); + gen_cast_s(VT_INT); + break; + } + } else { + if (!t) + save_regs(1); + t = gvtst(0, t); + } + if (tok != TOK_LOR) { + if (t) + vseti(VT_JMP, t); + else + vpushi(0); + break; + } + next(); + expr_land(); + } + } +} + +/* Assuming vtop is a value used in a conditional context + (i.e. compared with zero) return 0 if it's false, 1 if + true and -1 if it can't be statically determined. */ +static int condition_3way(void) +{ + int c = -1; + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && + (!(vtop->r & VT_SYM) || !vtop->sym->a.weak)) { + vdup(); + gen_cast_s(VT_BOOL); + c = vtop->c.i; + vpop(); + } + return c; +} + +static void expr_cond(void) +{ + int tt, u, r1, r2, rc, t1, t2, bt1, bt2, islv, c, g; + SValue sv; + CType type, type1, type2; + + expr_lor(); + if (tok == '?') { + next(); + c = condition_3way(); + g = (tok == ':' && gnu_ext); + if (c < 0) { + /* needed to avoid having different registers saved in + each branch */ + if (is_float(vtop->type.t)) { + rc = RC_FLOAT; +#ifdef TCC_TARGET_X86_64 + if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + rc = RC_ST0; + } +#endif + } else + rc = RC_INT; + gv(rc); + save_regs(1); + if (g) + gv_dup(); + tt = gvtst(1, 0); + + } else { + if (!g) + vpop(); + tt = 0; + } + + if (1) { + if (c == 0) + nocode_wanted++; + if (!g) + gexpr(); + + type1 = vtop->type; + sv = *vtop; /* save value to handle it later */ + vtop--; /* no vpop so that FP stack is not flushed */ + skip(':'); + + u = 0; + if (c < 0) + u = gjmp(0); + gsym(tt); + + if (c == 0) + nocode_wanted--; + if (c == 1) + nocode_wanted++; + expr_cond(); + if (c == 1) + nocode_wanted--; + + type2 = vtop->type; + t1 = type1.t; + bt1 = t1 & VT_BTYPE; + t2 = type2.t; + bt2 = t2 & VT_BTYPE; + type.ref = NULL; + + /* cast operands to correct type according to ISOC rules */ + if (is_float(bt1) || is_float(bt2)) { + if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) { + type.t = VT_LDOUBLE; + + } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) { + type.t = VT_DOUBLE; + } else { + type.t = VT_FLOAT; + } + } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) { + /* cast to biggest op */ + type.t = VT_LLONG | VT_LONG; + if (bt1 == VT_LLONG) + type.t &= t1; + if (bt2 == VT_LLONG) + type.t &= t2; + /* convert to unsigned if it does not fit in a long long */ + if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) || + (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED)) + type.t |= VT_UNSIGNED; + } else if (bt1 == VT_PTR || bt2 == VT_PTR) { + /* If one is a null ptr constant the result type + is the other. */ + if (is_null_pointer (vtop)) + type = type1; + else if (is_null_pointer (&sv)) + type = type2; + /* XXX: test pointer compatibility, C99 has more elaborate + rules here. */ + else + type = type1; + } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) { + /* XXX: test function pointer compatibility */ + type = bt1 == VT_FUNC ? type1 : type2; + } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) { + /* XXX: test structure compatibility */ + type = bt1 == VT_STRUCT ? type1 : type2; + } else if (bt1 == VT_VOID || bt2 == VT_VOID) { + /* NOTE: as an extension, we accept void on only one side */ + type.t = VT_VOID; + } else { + /* integer operations */ + type.t = VT_INT | (VT_LONG & (t1 | t2)); + /* convert to unsigned if it does not fit in an integer */ + if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) || + (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED)) + type.t |= VT_UNSIGNED; + } + /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so + that `(expr ? a : b).mem` does not error with "lvalue expected" */ + islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE); + islv &= c < 0; + + /* now we convert second operand */ + if (c != 1) { + gen_cast(&type); + if (islv) { + mk_pointer(&vtop->type); + gaddrof(); + } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE)) + gaddrof(); + } + + rc = RC_INT; + if (is_float(type.t)) { + rc = RC_FLOAT; +#ifdef TCC_TARGET_X86_64 + if ((type.t & VT_BTYPE) == VT_LDOUBLE) { + rc = RC_ST0; + } +#endif + } else if ((type.t & VT_BTYPE) == VT_LLONG) { + /* for long longs, we use fixed registers to avoid having + to handle a complicated move */ + rc = RC_IRET; + } + + tt = r2 = 0; + if (c < 0) { + r2 = gv(rc); + tt = gjmp(0); + } + gsym(u); + + /* this is horrible, but we must also convert first + operand */ + if (c != 0) { + *vtop = sv; + gen_cast(&type); + if (islv) { + mk_pointer(&vtop->type); + gaddrof(); + } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE)) + gaddrof(); + } + + if (c < 0) { + r1 = gv(rc); + move_reg(r2, r1, type.t); + vtop->r = r2; + gsym(tt); + if (islv) + indir(); + } + } + } +} + +static void expr_eq(void) +{ + int t; + + expr_cond(); + if (tok == '=' || + (tok >= TOK_A_MOD && tok <= TOK_A_DIV) || + tok == TOK_A_XOR || tok == TOK_A_OR || + tok == TOK_A_SHL || tok == TOK_A_SAR) { + test_lvalue(); + t = tok; + next(); + if (t == '=') { + expr_eq(); + } else { + vdup(); + expr_eq(); + gen_op(t & 0x7f); + } + vstore(); + } +} + +ST_FUNC void gexpr(void) +{ + while (1) { + expr_eq(); + if (tok != ',') + break; + vpop(); + next(); + } +} + +/* parse a constant expression and return value in vtop. */ +static void expr_const1(void) +{ + const_wanted++; + nocode_wanted++; + expr_cond(); + nocode_wanted--; + const_wanted--; +} + +/* parse an integer constant and return its value. */ +static inline int64_t expr_const64(void) +{ + int64_t c; + expr_const1(); + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + expect("constant expression"); + c = vtop->c.i; + vpop(); + return c; +} + +/* parse an integer constant and return its value. + Complain if it doesn't fit 32bit (signed or unsigned). */ +ST_FUNC int expr_const(void) +{ + int c; + int64_t wc = expr_const64(); + c = wc; + if (c != wc && (unsigned)c != wc) + tcc_error("constant exceeds 32 bit"); + return c; +} + +/* return the label token if current token is a label, otherwise + return zero */ +static int is_label(void) +{ + int last_tok; + + /* fast test first */ + if (tok < TOK_UIDENT) + return 0; + /* no need to save tokc because tok is an identifier */ + last_tok = tok; + next(); + if (tok == ':') { + return last_tok; + } else { + unget_tok(last_tok); + return 0; + } +} + +#ifndef TCC_TARGET_ARM64 +static void gfunc_return(CType *func_type) +{ + if ((func_type->t & VT_BTYPE) == VT_STRUCT) { + CType type, ret_type; + int ret_align, ret_nregs, regsize; + ret_nregs = gfunc_sret(func_type, func_var, &ret_type, + &ret_align, ®size); + if (0 == ret_nregs) { + /* if returning structure, must copy it to implicit + first pointer arg location */ + type = *func_type; + mk_pointer(&type); + vset(&type, VT_LOCAL | VT_LVAL, func_vc); + indir(); + vswap(); + /* copy structure value to pointer */ + vstore(); + } else { + /* returning structure packed into registers */ + int r, size, addr, align; + size = type_size(func_type,&align); + if ((vtop->r != (VT_LOCAL | VT_LVAL) || + (vtop->c.i & (ret_align-1))) + && (align & (ret_align-1))) { + loc = (loc - size) & -ret_align; + addr = loc; + type = *func_type; + vset(&type, VT_LOCAL | VT_LVAL, addr); + vswap(); + vstore(); + vpop(); + vset(&ret_type, VT_LOCAL | VT_LVAL, addr); + } + vtop->type = ret_type; + if (is_float(ret_type.t)) + r = rc_fret(ret_type.t); + else + r = RC_IRET; + + if (ret_nregs == 1) + gv(r); + else { + for (;;) { + vdup(); + gv(r); + vpop(); + if (--ret_nregs == 0) + break; + /* We assume that when a structure is returned in multiple + registers, their classes are consecutive values of the + suite s(n) = 2^n */ + r <<= 1; + vtop->c.i += regsize; + } + } + } + } else if (is_float(func_type->t)) { + gv(rc_fret(func_type->t)); + } else { + gv(RC_IRET); + } + vtop--; /* NOT vpop() because on x86 it would flush the fp stack */ +} +#endif + +static int case_cmp(const void *pa, const void *pb) +{ + int64_t a = (*(struct case_t**) pa)->v1; + int64_t b = (*(struct case_t**) pb)->v1; + return a < b ? -1 : a > b; +} + +static void gcase(struct case_t **base, int len, int *bsym) +{ + struct case_t *p; + int e; + int ll = (vtop->type.t & VT_BTYPE) == VT_LLONG; + gv(RC_INT); + while (len > 4) { + /* binary search */ + p = base[len/2]; + vdup(); + if (ll) + vpushll(p->v2); + else + vpushi(p->v2); + gen_op(TOK_LE); + e = gtst(1, 0); + vdup(); + if (ll) + vpushll(p->v1); + else + vpushi(p->v1); + gen_op(TOK_GE); + gtst_addr(0, p->sym); /* v1 <= x <= v2 */ + /* x < v1 */ + gcase(base, len/2, bsym); + if (cur_switch->def_sym) + gjmp_addr(cur_switch->def_sym); + else + *bsym = gjmp(*bsym); + /* x > v2 */ + gsym(e); + e = len/2 + 1; + base += e; len -= e; + } + /* linear scan */ + while (len--) { + p = *base++; + vdup(); + if (ll) + vpushll(p->v2); + else + vpushi(p->v2); + if (p->v1 == p->v2) { + gen_op(TOK_EQ); + gtst_addr(0, p->sym); + } else { + gen_op(TOK_LE); + e = gtst(1, 0); + vdup(); + if (ll) + vpushll(p->v1); + else + vpushi(p->v1); + gen_op(TOK_GE); + gtst_addr(0, p->sym); + gsym(e); + } + } +} + +static void block(int *bsym, int *csym, int is_expr) +{ + int a, b, c, d, cond; + Sym *s; + + /* generate line number info */ + if (tcc_state->do_debug) + tcc_debug_line(tcc_state); + + if (is_expr) { + /* default return value is (void) */ + vpushi(0); + vtop->type.t = VT_VOID; + } + + if (tok == TOK_IF) { + /* if test */ + int saved_nocode_wanted = nocode_wanted; + next(); + skip('('); + gexpr(); + skip(')'); + cond = condition_3way(); + if (cond == 1) + a = 0, vpop(); + else + a = gvtst(1, 0); + if (cond == 0) + nocode_wanted |= 0x20000000; + block(bsym, csym, 0); + if (cond != 1) + nocode_wanted = saved_nocode_wanted; + c = tok; + if (c == TOK_ELSE) { + next(); + d = gjmp(0); + gsym(a); + if (cond == 1) + nocode_wanted |= 0x20000000; + block(bsym, csym, 0); + gsym(d); /* patch else jmp */ + if (cond != 0) + nocode_wanted = saved_nocode_wanted; + } else + gsym(a); + } else if (tok == TOK_WHILE) { + int saved_nocode_wanted; + nocode_wanted &= ~0x20000000; + next(); + d = ind; + vla_sp_restore(); + skip('('); + gexpr(); + skip(')'); + a = gvtst(1, 0); + b = 0; + ++local_scope; + saved_nocode_wanted = nocode_wanted; + block(&a, &b, 0); + nocode_wanted = saved_nocode_wanted; + --local_scope; + gjmp_addr(d); + gsym(a); + gsym_addr(b, d); + } else if (tok == '{') { + Sym *llabel; + int block_vla_sp_loc = vla_sp_loc, saved_vlas_in_scope = vlas_in_scope; + + next(); + /* record local declaration stack position */ + s = local_stack; + llabel = local_label_stack; + ++local_scope; + + /* handle local labels declarations */ + if (tok == TOK_LABEL) { + next(); + for(;;) { + if (tok < TOK_UIDENT) + expect("label identifier"); + label_push(&local_label_stack, tok, LABEL_DECLARED); + next(); + if (tok == ',') { + next(); + } else { + skip(';'); + break; + } + } + } + while (tok != '}') { + if ((a = is_label())) + unget_tok(a); + else + decl(VT_LOCAL); + if (tok != '}') { + if (is_expr) + vpop(); + block(bsym, csym, is_expr); + } + } + /* pop locally defined labels */ + label_pop(&local_label_stack, llabel, is_expr); + /* pop locally defined symbols */ + --local_scope; + /* In the is_expr case (a statement expression is finished here), + vtop might refer to symbols on the local_stack. Either via the + type or via vtop->sym. We can't pop those nor any that in turn + might be referred to. To make it easier we don't roll back + any symbols in that case; some upper level call to block() will + do that. We do have to remove such symbols from the lookup + tables, though. sym_pop will do that. */ + sym_pop(&local_stack, s, is_expr); + + /* Pop VLA frames and restore stack pointer if required */ + if (vlas_in_scope > saved_vlas_in_scope) { + vla_sp_loc = saved_vlas_in_scope ? block_vla_sp_loc : vla_sp_root_loc; + vla_sp_restore(); + } + vlas_in_scope = saved_vlas_in_scope; + + next(); + } else if (tok == TOK_RETURN) { + next(); + if (tok != ';') { + gexpr(); + gen_assign_cast(&func_vt); + if ((func_vt.t & VT_BTYPE) == VT_VOID) + vtop--; + else + gfunc_return(&func_vt); + } + skip(';'); + /* jump unless last stmt in top-level block */ + if (tok != '}' || local_scope != 1) + rsym = gjmp(rsym); + nocode_wanted |= 0x20000000; + } else if (tok == TOK_BREAK) { + /* compute jump */ + if (!bsym) + tcc_error("cannot break"); + *bsym = gjmp(*bsym); + next(); + skip(';'); + nocode_wanted |= 0x20000000; + } else if (tok == TOK_CONTINUE) { + /* compute jump */ + if (!csym) + tcc_error("cannot continue"); + vla_sp_restore_root(); + *csym = gjmp(*csym); + next(); + skip(';'); + } else if (tok == TOK_FOR) { + int e; + int saved_nocode_wanted; + nocode_wanted &= ~0x20000000; + next(); + skip('('); + s = local_stack; + ++local_scope; + if (tok != ';') { + /* c99 for-loop init decl? */ + if (!decl0(VT_LOCAL, 1, NULL)) { + /* no, regular for-loop init expr */ + gexpr(); + vpop(); + } + } + skip(';'); + d = ind; + c = ind; + vla_sp_restore(); + a = 0; + b = 0; + if (tok != ';') { + gexpr(); + a = gvtst(1, 0); + } + skip(';'); + if (tok != ')') { + e = gjmp(0); + c = ind; + vla_sp_restore(); + gexpr(); + vpop(); + gjmp_addr(d); + gsym(e); + } + skip(')'); + saved_nocode_wanted = nocode_wanted; + block(&a, &b, 0); + nocode_wanted = saved_nocode_wanted; + gjmp_addr(c); + gsym(a); + gsym_addr(b, c); + --local_scope; + sym_pop(&local_stack, s, 0); + + } else + if (tok == TOK_DO) { + int saved_nocode_wanted; + nocode_wanted &= ~0x20000000; + next(); + a = 0; + b = 0; + d = ind; + vla_sp_restore(); + saved_nocode_wanted = nocode_wanted; + block(&a, &b, 0); + skip(TOK_WHILE); + skip('('); + gsym(b); + gexpr(); + c = gvtst(0, 0); + gsym_addr(c, d); + nocode_wanted = saved_nocode_wanted; + skip(')'); + gsym(a); + skip(';'); + } else + if (tok == TOK_SWITCH) { + struct switch_t *saved, sw; + int saved_nocode_wanted = nocode_wanted; + SValue switchval; + next(); + skip('('); + gexpr(); + skip(')'); + switchval = *vtop--; + a = 0; + b = gjmp(0); /* jump to first case */ + sw.p = NULL; sw.n = 0; sw.def_sym = 0; + saved = cur_switch; + cur_switch = &sw; + block(&a, csym, 0); + nocode_wanted = saved_nocode_wanted; + a = gjmp(a); /* add implicit break */ + /* case lookup */ + gsym(b); + qsort(sw.p, sw.n, sizeof(void*), case_cmp); + for (b = 1; b < sw.n; b++) + if (sw.p[b - 1]->v2 >= sw.p[b]->v1) + tcc_error("duplicate case value"); + /* Our switch table sorting is signed, so the compared + value needs to be as well when it's 64bit. */ + if ((switchval.type.t & VT_BTYPE) == VT_LLONG) + switchval.type.t &= ~VT_UNSIGNED; + vpushv(&switchval); + gcase(sw.p, sw.n, &a); + vpop(); + if (sw.def_sym) + gjmp_addr(sw.def_sym); + dynarray_reset(&sw.p, &sw.n); + cur_switch = saved; + /* break label */ + gsym(a); + } else + if (tok == TOK_CASE) { + struct case_t *cr = tcc_malloc(sizeof(struct case_t)); + if (!cur_switch) + expect("switch"); + nocode_wanted &= ~0x20000000; + next(); + cr->v1 = cr->v2 = expr_const64(); + if (gnu_ext && tok == TOK_DOTS) { + next(); + cr->v2 = expr_const64(); + if (cr->v2 < cr->v1) + tcc_warning("empty case range"); + } + cr->sym = ind; + dynarray_add(&cur_switch->p, &cur_switch->n, cr); + skip(':'); + is_expr = 0; + goto block_after_label; + } else + if (tok == TOK_DEFAULT) { + next(); + skip(':'); + if (!cur_switch) + expect("switch"); + if (cur_switch->def_sym) + tcc_error("too many 'default'"); + cur_switch->def_sym = ind; + is_expr = 0; + goto block_after_label; + } else + if (tok == TOK_GOTO) { + next(); + if (tok == '*' && gnu_ext) { + /* computed goto */ + next(); + gexpr(); + if ((vtop->type.t & VT_BTYPE) != VT_PTR) + expect("pointer"); + ggoto(); + } else if (tok >= TOK_UIDENT) { + s = label_find(tok); + /* put forward definition if needed */ + if (!s) { + s = label_push(&global_label_stack, tok, LABEL_FORWARD); + } else { + if (s->r == LABEL_DECLARED) + s->r = LABEL_FORWARD; + } + vla_sp_restore_root(); + if (s->r & LABEL_FORWARD) + s->jnext = gjmp(s->jnext); + else + gjmp_addr(s->jnext); + next(); + } else { + expect("label identifier"); + } + skip(';'); + } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) { + asm_instr(); + } else { + b = is_label(); + if (b) { + /* label case */ + next(); + s = label_find(b); + if (s) { + if (s->r == LABEL_DEFINED) + tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL)); + gsym(s->jnext); + s->r = LABEL_DEFINED; + } else { + s = label_push(&global_label_stack, b, LABEL_DEFINED); + } + s->jnext = ind; + vla_sp_restore(); + /* we accept this, but it is a mistake */ + block_after_label: + nocode_wanted &= ~0x20000000; + if (tok == '}') { + tcc_warning("deprecated use of label at end of compound statement"); + } else { + if (is_expr) + vpop(); + block(bsym, csym, is_expr); + } + } else { + /* expression case */ + if (tok != ';') { + if (is_expr) { + vpop(); + gexpr(); + } else { + gexpr(); + vpop(); + } + } + skip(';'); + } + } +} + +/* This skips over a stream of tokens containing balanced {} and () + pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started + with a '{'). If STR then allocates and stores the skipped tokens + in *STR. This doesn't check if () and {} are nested correctly, + i.e. "({)}" is accepted. */ +static void skip_or_save_block(TokenString **str) +{ + int braces = tok == '{'; + int level = 0; + if (str) + *str = tok_str_alloc(); + + while ((level > 0 || (tok != '}' && tok != ',' && tok != ';' && tok != ')'))) { + int t; + if (tok == TOK_EOF) { + if (str || level > 0) + tcc_error("unexpected end of file"); + else + break; + } + if (str) + tok_str_add_tok(*str); + t = tok; + next(); + if (t == '{' || t == '(') { + level++; + } else if (t == '}' || t == ')') { + level--; + if (level == 0 && braces && t == '}') + break; + } + } + if (str) { + tok_str_add(*str, -1); + tok_str_add(*str, 0); + } +} + +#define EXPR_CONST 1 +#define EXPR_ANY 2 + +static void parse_init_elem(int expr_type) +{ + int saved_global_expr; + switch(expr_type) { + case EXPR_CONST: + /* compound literals must be allocated globally in this case */ + saved_global_expr = global_expr; + global_expr = 1; + expr_const1(); + global_expr = saved_global_expr; + /* NOTE: symbols are accepted, as well as lvalue for anon symbols + (compound literals). */ + if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST + && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL) + || vtop->sym->v < SYM_FIRST_ANOM)) +#ifdef TCC_TARGET_PE + || ((vtop->r & VT_SYM) && vtop->sym->a.dllimport) +#endif + ) + tcc_error("initializer element is not constant"); + break; + case EXPR_ANY: + expr_eq(); + break; + } +} + +/* put zeros for variable based init */ +static void init_putz(Section *sec, unsigned long c, int size) +{ + if (sec) { + /* nothing to do because globals are already set to zero */ + } else { + vpush_global_sym(&func_old_type, TOK_memset); + vseti(VT_LOCAL, c); +#ifdef TCC_TARGET_ARM + vpushs(size); + vpushi(0); +#else + vpushi(0); + vpushs(size); +#endif + gfunc_call(3); + } +} + +/* t is the array or struct type. c is the array or struct + address. cur_field is the pointer to the current + field, for arrays the 'c' member contains the current start + index. 'size_only' is true if only size info is needed (only used + in arrays). al contains the already initialized length of the + current container (starting at c). This returns the new length of that. */ +static int decl_designator(CType *type, Section *sec, unsigned long c, + Sym **cur_field, int size_only, int al) +{ + Sym *s, *f; + int index, index_last, align, l, nb_elems, elem_size; + unsigned long corig = c; + + elem_size = 0; + nb_elems = 1; + if (gnu_ext && (l = is_label()) != 0) + goto struct_field; + /* NOTE: we only support ranges for last designator */ + while (nb_elems == 1 && (tok == '[' || tok == '.')) { + if (tok == '[') { + if (!(type->t & VT_ARRAY)) + expect("array type"); + next(); + index = index_last = expr_const(); + if (tok == TOK_DOTS && gnu_ext) { + next(); + index_last = expr_const(); + } + skip(']'); + s = type->ref; + if (index < 0 || (s->c >= 0 && index_last >= s->c) || + index_last < index) + tcc_error("invalid index"); + if (cur_field) + (*cur_field)->c = index_last; + type = pointed_type(type); + elem_size = type_size(type, &align); + c += index * elem_size; + nb_elems = index_last - index + 1; + } else { + next(); + l = tok; + struct_field: + next(); + if ((type->t & VT_BTYPE) != VT_STRUCT) + expect("struct/union type"); + f = find_field(type, l); + if (!f) + expect("field"); + if (cur_field) + *cur_field = f; + type = &f->type; + c += f->c; + } + cur_field = NULL; + } + if (!cur_field) { + if (tok == '=') { + next(); + } else if (!gnu_ext) { + expect("="); + } + } else { + if (type->t & VT_ARRAY) { + index = (*cur_field)->c; + if (type->ref->c >= 0 && index >= type->ref->c) + tcc_error("index too large"); + type = pointed_type(type); + c += index * type_size(type, &align); + } else { + f = *cur_field; + while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD)) + *cur_field = f = f->next; + if (!f) + tcc_error("too many field init"); + type = &f->type; + c += f->c; + } + } + /* must put zero in holes (note that doing it that way + ensures that it even works with designators) */ + if (!size_only && c - corig > al) + init_putz(sec, corig + al, c - corig - al); + decl_initializer(type, sec, c, 0, size_only); + + /* XXX: make it more general */ + if (!size_only && nb_elems > 1) { + unsigned long c_end; + uint8_t *src, *dst; + int i; + + if (!sec) { + vset(type, VT_LOCAL|VT_LVAL, c); + for (i = 1; i < nb_elems; i++) { + vset(type, VT_LOCAL|VT_LVAL, c + elem_size * i); + vswap(); + vstore(); + } + vpop(); + } else if (!NODATA_WANTED) { + c_end = c + nb_elems * elem_size; + if (c_end > sec->data_allocated) + section_realloc(sec, c_end); + src = sec->data + c; + dst = src; + for(i = 1; i < nb_elems; i++) { + dst += elem_size; + memcpy(dst, src, elem_size); + } + } + } + c += nb_elems * type_size(type, &align); + if (c - corig > al) + al = c - corig; + return al; +} + +/* store a value or an expression directly in global data or in local array */ +static void init_putv(CType *type, Section *sec, unsigned long c) +{ + int bt; + void *ptr; + CType dtype; + + dtype = *type; + dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */ + + if (sec) { + int size, align; + /* XXX: not portable */ + /* XXX: generate error if incorrect relocation */ + gen_assign_cast(&dtype); + bt = type->t & VT_BTYPE; + + if ((vtop->r & VT_SYM) + && bt != VT_PTR + && bt != VT_FUNC + && (bt != (PTR_SIZE == 8 ? VT_LLONG : VT_INT) + || (type->t & VT_BITFIELD)) + && !((vtop->r & VT_CONST) && vtop->sym->v >= SYM_FIRST_ANOM) + ) + tcc_error("initializer element is not computable at load time"); + + if (NODATA_WANTED) { + vtop--; + return; + } + + size = type_size(type, &align); + section_reserve(sec, c + size); + ptr = sec->data + c; + + /* XXX: make code faster ? */ + if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) && + vtop->sym->v >= SYM_FIRST_ANOM && + /* XXX This rejects compound literals like + '(void *){ptr}'. The problem is that '&sym' is + represented the same way, which would be ruled out + by the SYM_FIRST_ANOM check above, but also '"string"' + in 'char *p = "string"' is represented the same + with the type being VT_PTR and the symbol being an + anonymous one. That is, there's no difference in vtop + between '(void *){x}' and '&(void *){x}'. Ignore + pointer typed entities here. Hopefully no real code + will every use compound literals with scalar type. */ + (vtop->type.t & VT_BTYPE) != VT_PTR) { + /* These come from compound literals, memcpy stuff over. */ + Section *ssec; + ElfSym *esym; + ElfW_Rel *rel; + esym = elfsym(vtop->sym); + ssec = tcc_state->sections[esym->st_shndx]; + memmove (ptr, ssec->data + esym->st_value, size); + if (ssec->reloc) { + /* We need to copy over all memory contents, and that + includes relocations. Use the fact that relocs are + created it order, so look from the end of relocs + until we hit one before the copied region. */ + int num_relocs = ssec->reloc->data_offset / sizeof(*rel); + rel = (ElfW_Rel*)(ssec->reloc->data + ssec->reloc->data_offset); + while (num_relocs--) { + rel--; + if (rel->r_offset >= esym->st_value + size) + continue; + if (rel->r_offset < esym->st_value) + break; + /* Note: if the same fields are initialized multiple + times (possible with designators) then we possibly + add multiple relocations for the same offset here. + That would lead to wrong code, the last reloc needs + to win. We clean this up later after the whole + initializer is parsed. */ + put_elf_reloca(symtab_section, sec, + c + rel->r_offset - esym->st_value, + ELFW(R_TYPE)(rel->r_info), + ELFW(R_SYM)(rel->r_info), +#if PTR_SIZE == 8 + rel->r_addend +#else + 0 +#endif + ); + } + } + } else { + if (type->t & VT_BITFIELD) { + int bit_pos, bit_size, bits, n; + unsigned char *p, v, m; + bit_pos = BIT_POS(vtop->type.t); + bit_size = BIT_SIZE(vtop->type.t); + p = (unsigned char*)ptr + (bit_pos >> 3); + bit_pos &= 7, bits = 0; + while (bit_size) { + n = 8 - bit_pos; + if (n > bit_size) + n = bit_size; + v = vtop->c.i >> bits << bit_pos; + m = ((1 << n) - 1) << bit_pos; + *p = (*p & ~m) | (v & m); + bits += n, bit_size -= n, bit_pos = 0, ++p; + } + } else + switch(bt) { + /* XXX: when cross-compiling we assume that each type has the + same representation on host and target, which is likely to + be wrong in the case of long double */ + case VT_BOOL: + vtop->c.i = vtop->c.i != 0; + case VT_BYTE: + *(char *)ptr |= vtop->c.i; + break; + case VT_SHORT: + *(short *)ptr |= vtop->c.i; + break; + case VT_FLOAT: + *(float*)ptr = vtop->c.f; + break; + case VT_DOUBLE: + *(double *)ptr = vtop->c.d; + break; + case VT_LDOUBLE: +#if defined TCC_IS_NATIVE_387 + if (sizeof (long double) >= 10) /* zero pad ten-byte LD */ + memcpy(ptr, &vtop->c.ld, 10); +#ifdef __TINYC__ + else if (sizeof (long double) == sizeof (double)) + __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr) : "m" (vtop->c.ld)); +#endif + else if (vtop->c.ld == 0.0) + ; + else +#endif + if (sizeof(long double) == LDOUBLE_SIZE) + *(long double*)ptr = vtop->c.ld; + else if (sizeof(double) == LDOUBLE_SIZE) + *(double *)ptr = (double)vtop->c.ld; + else + tcc_error("can't cross compile long double constants"); + break; +#if PTR_SIZE != 8 + case VT_LLONG: + *(long long *)ptr |= vtop->c.i; + break; +#else + case VT_LLONG: +#endif + case VT_PTR: + { + addr_t val = vtop->c.i; +#if PTR_SIZE == 8 + if (vtop->r & VT_SYM) + greloca(sec, vtop->sym, c, R_DATA_PTR, val); + else + *(addr_t *)ptr |= val; +#else + if (vtop->r & VT_SYM) + greloc(sec, vtop->sym, c, R_DATA_PTR); + *(addr_t *)ptr |= val; +#endif + break; + } + default: + { + int val = vtop->c.i; +#if PTR_SIZE == 8 + if (vtop->r & VT_SYM) + greloca(sec, vtop->sym, c, R_DATA_PTR, val); + else + *(int *)ptr |= val; +#else + if (vtop->r & VT_SYM) + greloc(sec, vtop->sym, c, R_DATA_PTR); + *(int *)ptr |= val; +#endif + break; + } + } + } + vtop--; + } else { + vset(&dtype, VT_LOCAL|VT_LVAL, c); + vswap(); + vstore(); + vpop(); + } +} + +/* 't' contains the type and storage info. 'c' is the offset of the + object in section 'sec'. If 'sec' is NULL, it means stack based + allocation. 'first' is true if array '{' must be read (multi + dimension implicit array init handling). 'size_only' is true if + size only evaluation is wanted (only for arrays). */ +static void decl_initializer(CType *type, Section *sec, unsigned long c, + int first, int size_only) +{ + int len, n, no_oblock, nb, i; + int size1, align1; + int have_elem; + Sym *s, *f; + Sym indexsym; + CType *t1; + + /* If we currently are at an '}' or ',' we have read an initializer + element in one of our callers, and not yet consumed it. */ + have_elem = tok == '}' || tok == ','; + if (!have_elem && tok != '{' && + /* In case of strings we have special handling for arrays, so + don't consume them as initializer value (which would commit them + to some anonymous symbol). */ + tok != TOK_LSTR && tok != TOK_STR && + !size_only) { + parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST); + have_elem = 1; + } + + if (have_elem && + !(type->t & VT_ARRAY) && + /* Use i_c_parameter_t, to strip toplevel qualifiers. + The source type might have VT_CONSTANT set, which is + of course assignable to non-const elements. */ + is_compatible_unqualified_types(type, &vtop->type)) { + init_putv(type, sec, c); + } else if (type->t & VT_ARRAY) { + s = type->ref; + n = s->c; + t1 = pointed_type(type); + size1 = type_size(t1, &align1); + + no_oblock = 1; + if ((first && tok != TOK_LSTR && tok != TOK_STR) || + tok == '{') { + if (tok != '{') + tcc_error("character array initializer must be a literal," + " optionally enclosed in braces"); + skip('{'); + no_oblock = 0; + } + + /* only parse strings here if correct type (otherwise: handle + them as ((w)char *) expressions */ + if ((tok == TOK_LSTR && +#ifdef TCC_TARGET_PE + (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED) +#else + (t1->t & VT_BTYPE) == VT_INT +#endif + ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) { + len = 0; + while (tok == TOK_STR || tok == TOK_LSTR) { + int cstr_len, ch; + + /* compute maximum number of chars wanted */ + if (tok == TOK_STR) + cstr_len = tokc.str.size; + else + cstr_len = tokc.str.size / sizeof(nwchar_t); + cstr_len--; + nb = cstr_len; + if (n >= 0 && nb > (n - len)) + nb = n - len; + if (!size_only) { + if (cstr_len > nb) + tcc_warning("initializer-string for array is too long"); + /* in order to go faster for common case (char + string in global variable, we handle it + specifically */ + if (sec && tok == TOK_STR && size1 == 1) { + if (!NODATA_WANTED) + memcpy(sec->data + c + len, tokc.str.data, nb); + } else { + for(i=0;it & VT_ARRAY) { + ++indexsym.c; + /* special test for multi dimensional arrays (may not + be strictly correct if designators are used at the + same time) */ + if (no_oblock && len >= n*size1) + break; + } else { + if (s->type.t == VT_UNION) + f = NULL; + else + f = f->next; + if (no_oblock && f == NULL) + break; + } + + if (tok == '}') + break; + skip(','); + } + } + /* put zeros at the end */ + if (!size_only && len < n*size1) + init_putz(sec, c + len, n*size1 - len); + if (!no_oblock) + skip('}'); + /* patch type size if needed, which happens only for array types */ + if (n < 0) + s->c = size1 == 1 ? len : ((len + size1 - 1)/size1); + } else if ((type->t & VT_BTYPE) == VT_STRUCT) { + size1 = 1; + no_oblock = 1; + if (first || tok == '{') { + skip('{'); + no_oblock = 0; + } + s = type->ref; + f = s->next; + n = s->c; + goto do_init_list; + } else if (tok == '{') { + next(); + decl_initializer(type, sec, c, first, size_only); + skip('}'); + } else if (size_only) { + /* If we supported only ISO C we wouldn't have to accept calling + this on anything than an array size_only==1 (and even then + only on the outermost level, so no recursion would be needed), + because initializing a flex array member isn't supported. + But GNU C supports it, so we need to recurse even into + subfields of structs and arrays when size_only is set. */ + /* just skip expression */ + skip_or_save_block(NULL); + } else { + if (!have_elem) { + /* This should happen only when we haven't parsed + the init element above for fear of committing a + string constant to memory too early. */ + if (tok != TOK_STR && tok != TOK_LSTR) + expect("string constant"); + parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST); + } + init_putv(type, sec, c); + } +} + +/* parse an initializer for type 't' if 'has_init' is non zero, and + allocate space in local or global data space ('r' is either + VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated + variable 'v' of scope 'scope' is declared before initializers + are parsed. If 'v' is zero, then a reference to the new object + is put in the value stack. If 'has_init' is 2, a special parsing + is done to handle string constants. */ +static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, + int has_init, int v, int scope) +{ + int size, align, addr; + TokenString *init_str = NULL; + + Section *sec; + Sym *flexible_array; + Sym *sym = NULL; + int saved_nocode_wanted = nocode_wanted; +#ifdef CONFIG_TCC_BCHECK + int bcheck = tcc_state->do_bounds_check && !NODATA_WANTED; +#endif + + if (type->t & VT_STATIC) + nocode_wanted |= NODATA_WANTED ? 0x40000000 : 0x80000000; + + flexible_array = NULL; + if ((type->t & VT_BTYPE) == VT_STRUCT) { + Sym *field = type->ref->next; + if (field) { + while (field->next) + field = field->next; + if (field->type.t & VT_ARRAY && field->type.ref->c < 0) + flexible_array = field; + } + } + + size = type_size(type, &align); + /* If unknown size, we must evaluate it before + evaluating initializers because + initializers can generate global data too + (e.g. string pointers or ISOC99 compound + literals). It also simplifies local + initializers handling */ + if (size < 0 || (flexible_array && has_init)) { + if (!has_init) + tcc_error("unknown type size"); + /* get all init string */ + if (has_init == 2) { + init_str = tok_str_alloc(); + /* only get strings */ + while (tok == TOK_STR || tok == TOK_LSTR) { + tok_str_add_tok(init_str); + next(); + } + tok_str_add(init_str, -1); + tok_str_add(init_str, 0); + } else { + skip_or_save_block(&init_str); + } + unget_tok(0); + + /* compute size */ + begin_macro(init_str, 1); + next(); + decl_initializer(type, NULL, 0, 1, 1); + /* prepare second initializer parsing */ + macro_ptr = init_str->str; + next(); + + /* if still unknown size, error */ + size = type_size(type, &align); + if (size < 0) + tcc_error("unknown type size"); + } + /* If there's a flex member and it was used in the initializer + adjust size. */ + if (flexible_array && + flexible_array->type.ref->c > 0) + size += flexible_array->type.ref->c + * pointed_size(&flexible_array->type); + /* take into account specified alignment if bigger */ + if (ad->a.aligned) { + int speca = 1 << (ad->a.aligned - 1); + if (speca > align) + align = speca; + } else if (ad->a.packed) { + align = 1; + } + + if (NODATA_WANTED) + size = 0, align = 1; + + if ((r & VT_VALMASK) == VT_LOCAL) { + sec = NULL; +#ifdef CONFIG_TCC_BCHECK + if (bcheck && (type->t & VT_ARRAY)) { + loc--; + } +#endif + loc = (loc - size) & -align; + addr = loc; +#ifdef CONFIG_TCC_BCHECK + /* handles bounds */ + /* XXX: currently, since we do only one pass, we cannot track + '&' operators, so we add only arrays */ + if (bcheck && (type->t & VT_ARRAY)) { + addr_t *bounds_ptr; + /* add padding between regions */ + loc--; + /* then add local bound info */ + bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t)); + bounds_ptr[0] = addr; + bounds_ptr[1] = size; + } +#endif + if (v) { + /* local variable */ +#ifdef CONFIG_TCC_ASM + if (ad->asm_label) { + int reg = asm_parse_regvar(ad->asm_label); + if (reg >= 0) + r = (r & ~VT_VALMASK) | reg; + } +#endif + sym = sym_push(v, type, r, addr); + sym->a = ad->a; + } else { + /* push local reference */ + vset(type, r, addr); + } + } else { + if (v && scope == VT_CONST) { + /* see if the symbol was already defined */ + sym = sym_find(v); + if (sym) { + patch_storage(sym, ad, type); + /* we accept several definitions of the same global variable. */ + if (!has_init && sym->c && elfsym(sym)->st_shndx != SHN_UNDEF) + goto no_alloc; + } + } + + /* allocate symbol in corresponding section */ + sec = ad->section; + if (!sec) { + if (has_init) + sec = data_section; + else if (tcc_state->nocommon) + sec = bss_section; + } + + if (sec) { + addr = section_add(sec, size, align); +#ifdef CONFIG_TCC_BCHECK + /* add padding if bound check */ + if (bcheck) + section_add(sec, 1, 1); +#endif + } else { + addr = align; /* SHN_COMMON is special, symbol value is align */ + sec = common_section; + } + + if (v) { + if (!sym) { + sym = sym_push(v, type, r | VT_SYM, 0); + patch_storage(sym, ad, NULL); + } + /* Local statics have a scope until now (for + warnings), remove it here. */ + sym->sym_scope = 0; + /* update symbol definition */ + put_extern_sym(sym, sec, addr, size); + } else { + /* push global reference */ + sym = get_sym_ref(type, sec, addr, size); + vpushsym(type, sym); + vtop->r |= r; + } + +#ifdef CONFIG_TCC_BCHECK + /* handles bounds now because the symbol must be defined + before for the relocation */ + if (bcheck) { + addr_t *bounds_ptr; + + greloca(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR, 0); + /* then add global bound info */ + bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t)); + bounds_ptr[0] = 0; /* relocated */ + bounds_ptr[1] = size; + } +#endif + } + + if (type->t & VT_VLA) { + int a; + + if (NODATA_WANTED) + goto no_alloc; + + /* save current stack pointer */ + if (vlas_in_scope == 0) { + if (vla_sp_root_loc == -1) + vla_sp_root_loc = (loc -= PTR_SIZE); + gen_vla_sp_save(vla_sp_root_loc); + } + + vla_runtime_type_size(type, &a); + gen_vla_alloc(type, a); +#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64 + /* on _WIN64, because of the function args scratch area, the + result of alloca differs from RSP and is returned in RAX. */ + gen_vla_result(addr), addr = (loc -= PTR_SIZE); +#endif + gen_vla_sp_save(addr); + vla_sp_loc = addr; + vlas_in_scope++; + + } else if (has_init) { + size_t oldreloc_offset = 0; + if (sec && sec->reloc) + oldreloc_offset = sec->reloc->data_offset; + decl_initializer(type, sec, addr, 1, 0); + if (sec && sec->reloc) + squeeze_multi_relocs(sec, oldreloc_offset); + /* patch flexible array member size back to -1, */ + /* for possible subsequent similar declarations */ + if (flexible_array) + flexible_array->type.ref->c = -1; + } + + no_alloc: + /* restore parse state if needed */ + if (init_str) { + end_macro(); + next(); + } + + nocode_wanted = saved_nocode_wanted; +} + +/* parse a function defined by symbol 'sym' and generate its code in + 'cur_text_section' */ +static void gen_function(Sym *sym) +{ + nocode_wanted = 0; + ind = cur_text_section->data_offset; + /* NOTE: we patch the symbol size later */ + put_extern_sym(sym, cur_text_section, ind, 0); + funcname = get_tok_str(sym->v, NULL); + func_ind = ind; + /* Initialize VLA state */ + vla_sp_loc = -1; + vla_sp_root_loc = -1; + /* put debug symbol */ + tcc_debug_funcstart(tcc_state, sym); + /* push a dummy symbol to enable local sym storage */ + sym_push2(&local_stack, SYM_FIELD, 0, 0); + local_scope = 1; /* for function parameters */ + gfunc_prolog(&sym->type); + local_scope = 0; + rsym = 0; + block(NULL, NULL, 0); + nocode_wanted = 0; + gsym(rsym); + gfunc_epilog(); + cur_text_section->data_offset = ind; + label_pop(&global_label_stack, NULL, 0); + /* reset local stack */ + local_scope = 0; + sym_pop(&local_stack, NULL, 0); + /* end of function */ + /* patch symbol size */ + elfsym(sym)->st_size = ind - func_ind; + tcc_debug_funcend(tcc_state, ind - func_ind); + /* It's better to crash than to generate wrong code */ + cur_text_section = NULL; + funcname = ""; /* for safety */ + func_vt.t = VT_VOID; /* for safety */ + func_var = 0; /* for safety */ + ind = 0; /* for safety */ + nocode_wanted = 0x80000000; + check_vstack(); +} + +static void gen_inline_functions(TCCState *s) +{ + Sym *sym; + int inline_generated, i, ln; + struct InlineFunc *fn; + + ln = file->line_num; + /* iterate while inline function are referenced */ + do { + inline_generated = 0; + for (i = 0; i < s->nb_inline_fns; ++i) { + fn = s->inline_fns[i]; + sym = fn->sym; + if (sym && sym->c) { + /* the function was used: generate its code and + convert it to a normal function */ + fn->sym = NULL; + if (file) + pstrcpy(file->filename, sizeof file->filename, fn->filename); + sym->type.t &= ~VT_INLINE; + + begin_macro(fn->func_str, 1); + next(); + cur_text_section = text_section; + gen_function(sym); + end_macro(); + + inline_generated = 1; + } + } + } while (inline_generated); + file->line_num = ln; +} + +ST_FUNC void free_inline_functions(TCCState *s) +{ + int i; + /* free tokens of unused inline functions */ + for (i = 0; i < s->nb_inline_fns; ++i) { + struct InlineFunc *fn = s->inline_fns[i]; + if (fn->sym) + tok_str_free(fn->func_str); + } + dynarray_reset(&s->inline_fns, &s->nb_inline_fns); +} + +/* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP + if parsing old style parameter decl list (and FUNC_SYM is set then) */ +static int decl0(int l, int is_for_loop_init, Sym *func_sym) +{ + int v, has_init, r; + CType type, btype; + Sym *sym; + AttributeDef ad; + + while (1) { + if (!parse_btype(&btype, &ad)) { + if (is_for_loop_init) + return 0; + /* skip redundant ';' if not in old parameter decl scope */ + if (tok == ';' && l != VT_CMP) { + next(); + continue; + } + if (l != VT_CONST) + break; + if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) { + /* global asm block */ + asm_global_instr(); + continue; + } + if (tok >= TOK_UIDENT) { + /* special test for old K&R protos without explicit int + type. Only accepted when defining global data */ + btype.t = VT_INT; + } else { + if (tok != TOK_EOF) + expect("declaration"); + break; + } + } + if (tok == ';') { + if ((btype.t & VT_BTYPE) == VT_STRUCT) { + int v = btype.ref->v; + if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM) + tcc_warning("unnamed struct/union that defines no instances"); + next(); + continue; + } + if (IS_ENUM(btype.t)) { + next(); + continue; + } + } + while (1) { /* iterate thru each declaration */ + type = btype; + /* If the base type itself was an array type of unspecified + size (like in 'typedef int arr[]; arr x = {1};') then + we will overwrite the unknown size by the real one for + this decl. We need to unshare the ref symbol holding + that size. */ + if ((type.t & VT_ARRAY) && type.ref->c < 0) { + type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c); + } + type_decl(&type, &ad, &v, TYPE_DIRECT); +#if 0 + { + char buf[500]; + type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL)); + printf("type = '%s'\n", buf); + } +#endif + if ((type.t & VT_BTYPE) == VT_FUNC) { + if ((type.t & VT_STATIC) && (l == VT_LOCAL)) { + tcc_error("function without file scope cannot be static"); + } + /* if old style function prototype, we accept a + declaration list */ + sym = type.ref; + if (sym->f.func_type == FUNC_OLD && l == VT_CONST) + decl0(VT_CMP, 0, sym); + } + + if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) { + ad.asm_label = asm_label_instr(); + /* parse one last attribute list, after asm label */ + parse_attribute(&ad); + if (tok == '{') + expect(";"); + } + +#ifdef TCC_TARGET_PE + if (ad.a.dllimport || ad.a.dllexport) { + if (type.t & (VT_STATIC|VT_TYPEDEF)) + tcc_error("cannot have dll linkage with static or typedef"); + if (ad.a.dllimport) { + if ((type.t & VT_BTYPE) == VT_FUNC) + ad.a.dllimport = 0; + else + type.t |= VT_EXTERN; + } + } +#endif + if (tok == '{') { + if (l != VT_CONST) + tcc_error("cannot use local functions"); + if ((type.t & VT_BTYPE) != VT_FUNC) + expect("function definition"); + + /* reject abstract declarators in function definition + make old style params without decl have int type */ + sym = type.ref; + while ((sym = sym->next) != NULL) { + if (!(sym->v & ~SYM_FIELD)) + expect("identifier"); + if (sym->type.t == VT_VOID) + sym->type = int_type; + } + + /* XXX: cannot do better now: convert extern line to static inline */ + if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE)) + type.t = (type.t & ~VT_EXTERN) | VT_STATIC; + + /* put function symbol */ + sym = external_global_sym(v, &type, 0); + type.t &= ~VT_EXTERN; + patch_storage(sym, &ad, &type); + + /* static inline functions are just recorded as a kind + of macro. Their code will be emitted at the end of + the compilation unit only if they are used */ + if ((type.t & (VT_INLINE | VT_STATIC)) == + (VT_INLINE | VT_STATIC)) { + struct InlineFunc *fn; + const char *filename; + + filename = file ? file->filename : ""; + fn = tcc_malloc(sizeof *fn + strlen(filename)); + strcpy(fn->filename, filename); + fn->sym = sym; + skip_or_save_block(&fn->func_str); + dynarray_add(&tcc_state->inline_fns, + &tcc_state->nb_inline_fns, fn); + } else { + /* compute text section */ + cur_text_section = ad.section; + if (!cur_text_section) + cur_text_section = text_section; + gen_function(sym); + } + break; + } else { + if (l == VT_CMP) { + /* find parameter in function parameter list */ + for (sym = func_sym->next; sym; sym = sym->next) + if ((sym->v & ~SYM_FIELD) == v) + goto found; + tcc_error("declaration for parameter '%s' but no such parameter", + get_tok_str(v, NULL)); +found: + if (type.t & VT_STORAGE) /* 'register' is okay */ + tcc_error("storage class specified for '%s'", + get_tok_str(v, NULL)); + if (sym->type.t != VT_VOID) + tcc_error("redefinition of parameter '%s'", + get_tok_str(v, NULL)); + convert_parameter_type(&type); + sym->type = type; + } else if (type.t & VT_TYPEDEF) { + /* save typedefed type */ + /* XXX: test storage specifiers ? */ + sym = sym_find(v); + if (sym && sym->sym_scope == local_scope) { + if (!is_compatible_types(&sym->type, &type) + || !(sym->type.t & VT_TYPEDEF)) + tcc_error("incompatible redefinition of '%s'", + get_tok_str(v, NULL)); + sym->type = type; + } else { + sym = sym_push(v, &type, 0, 0); + } + sym->a = ad.a; + sym->f = ad.f; + } else { + r = 0; + if ((type.t & VT_BTYPE) == VT_FUNC) { + /* external function definition */ + /* specific case for func_call attribute */ + type.ref->f = ad.f; + } else if (!(type.t & VT_ARRAY)) { + /* not lvalue if array */ + r |= lvalue_type(type.t); + } + has_init = (tok == '='); + if (has_init && (type.t & VT_VLA)) + tcc_error("variable length array cannot be initialized"); + if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) || + ((type.t & VT_BTYPE) == VT_FUNC) || + ((type.t & VT_ARRAY) && (type.t & VT_STATIC) && + !has_init && l == VT_CONST && type.ref->c < 0)) { + /* external variable or function */ + /* NOTE: as GCC, uninitialized global static + arrays of null size are considered as + extern */ + type.t |= VT_EXTERN; + sym = external_sym(v, &type, r, &ad); + if (ad.alias_target) { + ElfSym *esym; + Sym *alias_target; + alias_target = sym_find(ad.alias_target); + esym = elfsym(alias_target); + if (!esym) + tcc_error("unsupported forward __alias__ attribute"); + /* Local statics have a scope until now (for + warnings), remove it here. */ + sym->sym_scope = 0; + put_extern_sym2(sym, esym->st_shndx, esym->st_value, esym->st_size, 0); + } + } else { + if (type.t & VT_STATIC) + r |= VT_CONST; + else + r |= l; + if (has_init) + next(); + else if (l == VT_CONST) + /* uninitialized global variables may be overridden */ + type.t |= VT_EXTERN; + decl_initializer_alloc(&type, &ad, r, has_init, v, l); + } + } + if (tok != ',') { + if (is_for_loop_init) + return 1; + skip(';'); + break; + } + next(); + } + ad.a.aligned = 0; + } + } + return 0; +} + +static void decl(int l) +{ + decl0(l, 0, NULL); +} + +/* ------------------------------------------------------------------------- */ diff --git a/05/tcc-final-old/tcclib.h b/05/tcc-final-old/tcclib.h new file mode 100644 index 0000000..8d59e4c --- /dev/null +++ b/05/tcc-final-old/tcclib.h @@ -0,0 +1,80 @@ +/* Simple libc header for TCC + * + * Add any function you want from the libc there. This file is here + * only for your convenience so that you do not need to put the whole + * glibc include files on your floppy disk + */ +#ifndef _TCCLIB_H +#define _TCCLIB_H + +#include +#include + +/* stdlib.h */ +void *calloc(size_t nmemb, size_t size); +void *malloc(size_t size); +void free(void *ptr); +void *realloc(void *ptr, size_t size); +int atoi(const char *nptr); +long int strtol(const char *nptr, char **endptr, int base); +unsigned long int strtoul(const char *nptr, char **endptr, int base); +void exit(int); + +/* stdio.h */ +typedef struct __FILE FILE; +#define EOF (-1) +extern FILE *stdin; +extern FILE *stdout; +extern FILE *stderr; +FILE *fopen(const char *path, const char *mode); +FILE *fdopen(int fildes, const char *mode); +FILE *freopen(const char *path, const char *mode, FILE *stream); +int fclose(FILE *stream); +size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); +size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream); +int fgetc(FILE *stream); +char *fgets(char *s, int size, FILE *stream); +int getc(FILE *stream); +int getchar(void); +char *gets(char *s); +int ungetc(int c, FILE *stream); +int fflush(FILE *stream); +int putchar (int c); + +int printf(const char *format, ...); +int fprintf(FILE *stream, const char *format, ...); +int sprintf(char *str, const char *format, ...); +int snprintf(char *str, size_t size, const char *format, ...); +int asprintf(char **strp, const char *format, ...); +int dprintf(int fd, const char *format, ...); +int vprintf(const char *format, va_list ap); +int vfprintf(FILE *stream, const char *format, va_list ap); +int vsprintf(char *str, const char *format, va_list ap); +int vsnprintf(char *str, size_t size, const char *format, va_list ap); +int vasprintf(char **strp, const char *format, va_list ap); +int vdprintf(int fd, const char *format, va_list ap); + +void perror(const char *s); + +/* string.h */ +char *strcat(char *dest, const char *src); +char *strchr(const char *s, int c); +char *strrchr(const char *s, int c); +char *strcpy(char *dest, const char *src); +void *memcpy(void *dest, const void *src, size_t n); +void *memmove(void *dest, const void *src, size_t n); +void *memset(void *s, int c, size_t n); +char *strdup(const char *s); +size_t strlen(const char *s); + +/* dlfcn.h */ +#define RTLD_LAZY 0x001 +#define RTLD_NOW 0x002 +#define RTLD_GLOBAL 0x100 + +void *dlopen(const char *filename, int flag); +const char *dlerror(void); +void *dlsym(void *handle, char *symbol); +int dlclose(void *handle); + +#endif /* _TCCLIB_H */ diff --git a/05/tcc-final-old/tccpe.c b/05/tcc-final-old/tccpe.c new file mode 100644 index 0000000..a7266c9 --- /dev/null +++ b/05/tcc-final-old/tccpe.c @@ -0,0 +1,1996 @@ +/* + * TCCPE.C - PE file output for the Tiny C Compiler + * + * Copyright (c) 2005-2007 grischka + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +#define PE_MERGE_DATA +/* #define PE_PRINT_SECTIONS */ + +#ifndef _WIN32 +#define stricmp strcasecmp +#define strnicmp strncasecmp +#include /* chmod() */ +#endif + +#ifdef TCC_TARGET_X86_64 +# define ADDR3264 ULONGLONG +# define PE_IMAGE_REL IMAGE_REL_BASED_DIR64 +# define REL_TYPE_DIRECT R_X86_64_64 +# define R_XXX_THUNKFIX R_X86_64_PC32 +# define R_XXX_RELATIVE R_X86_64_RELATIVE +# define IMAGE_FILE_MACHINE 0x8664 +# define RSRC_RELTYPE 3 + +#elif defined TCC_TARGET_ARM +# define ADDR3264 DWORD +# define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW +# define REL_TYPE_DIRECT R_ARM_ABS32 +# define R_XXX_THUNKFIX R_ARM_ABS32 +# define R_XXX_RELATIVE R_ARM_RELATIVE +# define IMAGE_FILE_MACHINE 0x01C0 +# define RSRC_RELTYPE 7 /* ??? (not tested) */ + +#elif defined TCC_TARGET_I386 +# define ADDR3264 DWORD +# define PE_IMAGE_REL IMAGE_REL_BASED_HIGHLOW +# define REL_TYPE_DIRECT R_386_32 +# define R_XXX_THUNKFIX R_386_32 +# define R_XXX_RELATIVE R_386_RELATIVE +# define IMAGE_FILE_MACHINE 0x014C +# define RSRC_RELTYPE 7 /* DIR32NB */ + +#endif + +#ifndef IMAGE_NT_SIGNATURE +/* ----------------------------------------------------------- */ +/* definitions below are from winnt.h */ + +typedef unsigned char BYTE; +typedef unsigned short WORD; +typedef unsigned int DWORD; +typedef unsigned long long ULONGLONG; +#pragma pack(push, 1) + +typedef struct _IMAGE_DOS_HEADER { /* DOS .EXE header */ + WORD e_magic; /* Magic number */ + WORD e_cblp; /* Bytes on last page of file */ + WORD e_cp; /* Pages in file */ + WORD e_crlc; /* Relocations */ + WORD e_cparhdr; /* Size of header in paragraphs */ + WORD e_minalloc; /* Minimum extra paragraphs needed */ + WORD e_maxalloc; /* Maximum extra paragraphs needed */ + WORD e_ss; /* Initial (relative) SS value */ + WORD e_sp; /* Initial SP value */ + WORD e_csum; /* Checksum */ + WORD e_ip; /* Initial IP value */ + WORD e_cs; /* Initial (relative) CS value */ + WORD e_lfarlc; /* File address of relocation table */ + WORD e_ovno; /* Overlay number */ + WORD e_res[4]; /* Reserved words */ + WORD e_oemid; /* OEM identifier (for e_oeminfo) */ + WORD e_oeminfo; /* OEM information; e_oemid specific */ + WORD e_res2[10]; /* Reserved words */ + DWORD e_lfanew; /* File address of new exe header */ +} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER; + +#define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */ +#define SIZE_OF_NT_SIGNATURE 4 + +typedef struct _IMAGE_FILE_HEADER { + WORD Machine; + WORD NumberOfSections; + DWORD TimeDateStamp; + DWORD PointerToSymbolTable; + DWORD NumberOfSymbols; + WORD SizeOfOptionalHeader; + WORD Characteristics; +} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER; + + +#define IMAGE_SIZEOF_FILE_HEADER 20 + +typedef struct _IMAGE_DATA_DIRECTORY { + DWORD VirtualAddress; + DWORD Size; +} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY; + + +typedef struct _IMAGE_OPTIONAL_HEADER { + /* Standard fields. */ + WORD Magic; + BYTE MajorLinkerVersion; + BYTE MinorLinkerVersion; + DWORD SizeOfCode; + DWORD SizeOfInitializedData; + DWORD SizeOfUninitializedData; + DWORD AddressOfEntryPoint; + DWORD BaseOfCode; +#ifndef TCC_TARGET_X86_64 + DWORD BaseOfData; +#endif + /* NT additional fields. */ + ADDR3264 ImageBase; + DWORD SectionAlignment; + DWORD FileAlignment; + WORD MajorOperatingSystemVersion; + WORD MinorOperatingSystemVersion; + WORD MajorImageVersion; + WORD MinorImageVersion; + WORD MajorSubsystemVersion; + WORD MinorSubsystemVersion; + DWORD Win32VersionValue; + DWORD SizeOfImage; + DWORD SizeOfHeaders; + DWORD CheckSum; + WORD Subsystem; + WORD DllCharacteristics; + ADDR3264 SizeOfStackReserve; + ADDR3264 SizeOfStackCommit; + ADDR3264 SizeOfHeapReserve; + ADDR3264 SizeOfHeapCommit; + DWORD LoaderFlags; + DWORD NumberOfRvaAndSizes; + IMAGE_DATA_DIRECTORY DataDirectory[16]; +} IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, IMAGE_OPTIONAL_HEADER; + +#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */ +#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */ +#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */ +#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */ +#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */ +#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */ +#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */ +/* IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 (X86 usage) */ +#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 /* Architecture Specific Data */ +#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* RVA of GP */ +#define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */ +#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */ +#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */ +#define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */ +#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 /* Delay Load Import Descriptors */ +#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 /* COM Runtime descriptor */ + +/* Section header format. */ +#define IMAGE_SIZEOF_SHORT_NAME 8 + +typedef struct _IMAGE_SECTION_HEADER { + BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; + union { + DWORD PhysicalAddress; + DWORD VirtualSize; + } Misc; + DWORD VirtualAddress; + DWORD SizeOfRawData; + DWORD PointerToRawData; + DWORD PointerToRelocations; + DWORD PointerToLinenumbers; + WORD NumberOfRelocations; + WORD NumberOfLinenumbers; + DWORD Characteristics; +} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER; + +#define IMAGE_SIZEOF_SECTION_HEADER 40 + +typedef struct _IMAGE_EXPORT_DIRECTORY { + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD Name; + DWORD Base; + DWORD NumberOfFunctions; + DWORD NumberOfNames; + DWORD AddressOfFunctions; + DWORD AddressOfNames; + DWORD AddressOfNameOrdinals; +} IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY; + +typedef struct _IMAGE_IMPORT_DESCRIPTOR { + union { + DWORD Characteristics; + DWORD OriginalFirstThunk; + }; + DWORD TimeDateStamp; + DWORD ForwarderChain; + DWORD Name; + DWORD FirstThunk; +} IMAGE_IMPORT_DESCRIPTOR; + +typedef struct _IMAGE_BASE_RELOCATION { + DWORD VirtualAddress; + DWORD SizeOfBlock; +// WORD TypeOffset[1]; +} IMAGE_BASE_RELOCATION; + +#define IMAGE_SIZEOF_BASE_RELOCATION 8 + +#define IMAGE_REL_BASED_ABSOLUTE 0 +#define IMAGE_REL_BASED_HIGH 1 +#define IMAGE_REL_BASED_LOW 2 +#define IMAGE_REL_BASED_HIGHLOW 3 +#define IMAGE_REL_BASED_HIGHADJ 4 +#define IMAGE_REL_BASED_MIPS_JMPADDR 5 +#define IMAGE_REL_BASED_SECTION 6 +#define IMAGE_REL_BASED_REL32 7 +#define IMAGE_REL_BASED_DIR64 10 + +#pragma pack(pop) + +/* ----------------------------------------------------------- */ +#endif /* ndef IMAGE_NT_SIGNATURE */ +/* ----------------------------------------------------------- */ + +#ifndef IMAGE_REL_BASED_DIR64 +# define IMAGE_REL_BASED_DIR64 10 +#endif + +#pragma pack(push, 1) +struct pe_header +{ + IMAGE_DOS_HEADER doshdr; + BYTE dosstub[0x40]; + DWORD nt_sig; + IMAGE_FILE_HEADER filehdr; +#ifdef TCC_TARGET_X86_64 + IMAGE_OPTIONAL_HEADER64 opthdr; +#else +#ifdef _WIN64 + IMAGE_OPTIONAL_HEADER32 opthdr; +#else + IMAGE_OPTIONAL_HEADER opthdr; +#endif +#endif +}; + +struct pe_reloc_header { + DWORD offset; + DWORD size; +}; + +struct pe_rsrc_header { + struct _IMAGE_FILE_HEADER filehdr; + struct _IMAGE_SECTION_HEADER sectionhdr; +}; + +struct pe_rsrc_reloc { + DWORD offset; + DWORD size; + WORD type; +}; +#pragma pack(pop) + +/* ------------------------------------------------------------- */ +/* internal temporary structures */ + +/* +#define IMAGE_SCN_CNT_CODE 0x00000020 +#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 +#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 +#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 +#define IMAGE_SCN_MEM_SHARED 0x10000000 +#define IMAGE_SCN_MEM_EXECUTE 0x20000000 +#define IMAGE_SCN_MEM_READ 0x40000000 +#define IMAGE_SCN_MEM_WRITE 0x80000000 +*/ + +enum { + sec_text = 0, + sec_data , + sec_bss , + sec_idata , + sec_pdata , + sec_other , + sec_rsrc , + sec_stab , + sec_reloc , + sec_last +}; + +static const DWORD pe_sec_flags[] = { + 0x60000020, /* ".text" , */ + 0xC0000040, /* ".data" , */ + 0xC0000080, /* ".bss" , */ + 0x40000040, /* ".idata" , */ + 0x40000040, /* ".pdata" , */ + 0xE0000060, /* < other > , */ + 0x40000040, /* ".rsrc" , */ + 0x42000802, /* ".stab" , */ + 0x42000040, /* ".reloc" , */ +}; + +struct section_info { + int cls, ord; + char name[32]; + DWORD sh_addr; + DWORD sh_size; + DWORD sh_flags; + unsigned char *data; + DWORD data_size; + IMAGE_SECTION_HEADER ish; +}; + +struct import_symbol { + int sym_index; + int iat_index; + int thk_offset; +}; + +struct pe_import_info { + int dll_index; + int sym_count; + struct import_symbol **symbols; +}; + +struct pe_info { + TCCState *s1; + Section *reloc; + Section *thunk; + const char *filename; + int type; + DWORD sizeofheaders; + ADDR3264 imagebase; + const char *start_symbol; + DWORD start_addr; + DWORD imp_offs; + DWORD imp_size; + DWORD iat_offs; + DWORD iat_size; + DWORD exp_offs; + DWORD exp_size; + int subsystem; + DWORD section_align; + DWORD file_align; + struct section_info *sec_info; + int sec_count; + struct pe_import_info **imp_info; + int imp_count; +}; + +#define PE_NUL 0 +#define PE_DLL 1 +#define PE_GUI 2 +#define PE_EXE 3 +#define PE_RUN 4 + +/* --------------------------------------------*/ + +static const char *pe_export_name(TCCState *s1, ElfW(Sym) *sym) +{ + const char *name = (char*)symtab_section->link->data + sym->st_name; + if (s1->leading_underscore && name[0] == '_' && !(sym->st_other & ST_PE_STDCALL)) + return name + 1; + return name; +} + +static int pe_find_import(TCCState * s1, ElfW(Sym) *sym) +{ + char buffer[200]; + const char *s, *p; + int sym_index = 0, n = 0; + int a, err = 0; + + do { + s = pe_export_name(s1, sym); + a = 0; + if (n) { + /* second try: */ + if (sym->st_other & ST_PE_STDCALL) { + /* try w/0 stdcall deco (windows API convention) */ + p = strrchr(s, '@'); + if (!p || s[0] != '_') + break; + strcpy(buffer, s+1)[p-s-1] = 0; + } else if (s[0] != '_') { /* try non-ansi function */ + buffer[0] = '_', strcpy(buffer + 1, s); + } else if (0 == memcmp(s, "__imp_", 6)) { /* mingw 2.0 */ + strcpy(buffer, s + 6), a = 1; + } else if (0 == memcmp(s, "_imp__", 6)) { /* mingw 3.7 */ + strcpy(buffer, s + 6), a = 1; + } else { + continue; + } + s = buffer; + } + sym_index = find_elf_sym(s1->dynsymtab_section, s); + // printf("find (%d) %d %s\n", n, sym_index, s); + if (sym_index + && ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT + && 0 == (sym->st_other & ST_PE_IMPORT) + && 0 == a + ) err = -1, sym_index = 0; + } while (0 == sym_index && ++n < 2); + return n == 2 ? err : sym_index; +} + +/*----------------------------------------------------------------------------*/ + +static int dynarray_assoc(void **pp, int n, int key) +{ + int i; + for (i = 0; i < n; ++i, ++pp) + if (key == **(int **) pp) + return i; + return -1; +} + +#if 0 +ST_FN DWORD umin(DWORD a, DWORD b) +{ + return a < b ? a : b; +} +#endif + +static DWORD umax(DWORD a, DWORD b) +{ + return a < b ? b : a; +} + +static DWORD pe_file_align(struct pe_info *pe, DWORD n) +{ + return (n + (pe->file_align - 1)) & ~(pe->file_align - 1); +} + +static DWORD pe_virtual_align(struct pe_info *pe, DWORD n) +{ + return (n + (pe->section_align - 1)) & ~(pe->section_align - 1); +} + +static void pe_align_section(Section *s, int a) +{ + int i = s->data_offset & (a-1); + if (i) + section_ptr_add(s, a - i); +} + +static void pe_set_datadir(struct pe_header *hdr, int dir, DWORD addr, DWORD size) +{ + hdr->opthdr.DataDirectory[dir].VirtualAddress = addr; + hdr->opthdr.DataDirectory[dir].Size = size; +} + +static int pe_fwrite(void *data, unsigned len, FILE *fp, DWORD *psum) +{ + if (psum) { + DWORD sum = *psum; + WORD *p = data; + int i; + for (i = len; i > 0; i -= 2) { + sum += (i >= 2) ? *p++ : *(BYTE*)p; + sum = (sum + (sum >> 16)) & 0xFFFF; + } + *psum = sum; + } + return len == fwrite(data, 1, len, fp) ? 0 : -1; +} + +static void pe_fpad(FILE *fp, DWORD new_pos) +{ + DWORD pos = ftell(fp); + while (++pos <= new_pos) + fputc(0, fp); +} + +/*----------------------------------------------------------------------------*/ +static int pe_write(struct pe_info *pe) +{ + static const struct pe_header pe_template = { + { + /* IMAGE_DOS_HEADER doshdr */ + 0x5A4D, /*WORD e_magic; Magic number */ + 0x0090, /*WORD e_cblp; Bytes on last page of file */ + 0x0003, /*WORD e_cp; Pages in file */ + 0x0000, /*WORD e_crlc; Relocations */ + + 0x0004, /*WORD e_cparhdr; Size of header in paragraphs */ + 0x0000, /*WORD e_minalloc; Minimum extra paragraphs needed */ + 0xFFFF, /*WORD e_maxalloc; Maximum extra paragraphs needed */ + 0x0000, /*WORD e_ss; Initial (relative) SS value */ + + 0x00B8, /*WORD e_sp; Initial SP value */ + 0x0000, /*WORD e_csum; Checksum */ + 0x0000, /*WORD e_ip; Initial IP value */ + 0x0000, /*WORD e_cs; Initial (relative) CS value */ + 0x0040, /*WORD e_lfarlc; File address of relocation table */ + 0x0000, /*WORD e_ovno; Overlay number */ + {0,0,0,0}, /*WORD e_res[4]; Reserved words */ + 0x0000, /*WORD e_oemid; OEM identifier (for e_oeminfo) */ + 0x0000, /*WORD e_oeminfo; OEM information; e_oemid specific */ + {0,0,0,0,0,0,0,0,0,0}, /*WORD e_res2[10]; Reserved words */ + 0x00000080 /*DWORD e_lfanew; File address of new exe header */ + },{ + /* BYTE dosstub[0x40] */ + /* 14 code bytes + "This program cannot be run in DOS mode.\r\r\n$" + 6 * 0x00 */ + 0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,0x4c,0xcd,0x21,0x54,0x68, + 0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x63,0x61,0x6e,0x6e,0x6f, + 0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,0x69,0x6e,0x20,0x44,0x4f,0x53,0x20, + 0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + }, + 0x00004550, /* DWORD nt_sig = IMAGE_NT_SIGNATURE */ + { + /* IMAGE_FILE_HEADER filehdr */ + IMAGE_FILE_MACHINE, /*WORD Machine; */ + 0x0003, /*WORD NumberOfSections; */ + 0x00000000, /*DWORD TimeDateStamp; */ + 0x00000000, /*DWORD PointerToSymbolTable; */ + 0x00000000, /*DWORD NumberOfSymbols; */ +#if defined(TCC_TARGET_X86_64) + 0x00F0, /*WORD SizeOfOptionalHeader; */ + 0x022F /*WORD Characteristics; */ +#define CHARACTERISTICS_DLL 0x222E +#elif defined(TCC_TARGET_I386) + 0x00E0, /*WORD SizeOfOptionalHeader; */ + 0x030F /*WORD Characteristics; */ +#define CHARACTERISTICS_DLL 0x230E +#elif defined(TCC_TARGET_ARM) + 0x00E0, /*WORD SizeOfOptionalHeader; */ + 0x010F, /*WORD Characteristics; */ +#define CHARACTERISTICS_DLL 0x230F +#endif +},{ + /* IMAGE_OPTIONAL_HEADER opthdr */ + /* Standard fields. */ +#ifdef TCC_TARGET_X86_64 + 0x020B, /*WORD Magic; */ +#else + 0x010B, /*WORD Magic; */ +#endif + 0x06, /*BYTE MajorLinkerVersion; */ + 0x00, /*BYTE MinorLinkerVersion; */ + 0x00000000, /*DWORD SizeOfCode; */ + 0x00000000, /*DWORD SizeOfInitializedData; */ + 0x00000000, /*DWORD SizeOfUninitializedData; */ + 0x00000000, /*DWORD AddressOfEntryPoint; */ + 0x00000000, /*DWORD BaseOfCode; */ +#ifndef TCC_TARGET_X86_64 + 0x00000000, /*DWORD BaseOfData; */ +#endif + /* NT additional fields. */ +#if defined(TCC_TARGET_ARM) + 0x00100000, /*DWORD ImageBase; */ +#else + 0x00400000, /*DWORD ImageBase; */ +#endif + 0x00001000, /*DWORD SectionAlignment; */ + 0x00000200, /*DWORD FileAlignment; */ + 0x0004, /*WORD MajorOperatingSystemVersion; */ + 0x0000, /*WORD MinorOperatingSystemVersion; */ + 0x0000, /*WORD MajorImageVersion; */ + 0x0000, /*WORD MinorImageVersion; */ + 0x0004, /*WORD MajorSubsystemVersion; */ + 0x0000, /*WORD MinorSubsystemVersion; */ + 0x00000000, /*DWORD Win32VersionValue; */ + 0x00000000, /*DWORD SizeOfImage; */ + 0x00000200, /*DWORD SizeOfHeaders; */ + 0x00000000, /*DWORD CheckSum; */ + 0x0002, /*WORD Subsystem; */ + 0x0000, /*WORD DllCharacteristics; */ + 0x00100000, /*DWORD SizeOfStackReserve; */ + 0x00001000, /*DWORD SizeOfStackCommit; */ + 0x00100000, /*DWORD SizeOfHeapReserve; */ + 0x00001000, /*DWORD SizeOfHeapCommit; */ + 0x00000000, /*DWORD LoaderFlags; */ + 0x00000010, /*DWORD NumberOfRvaAndSizes; */ + + /* IMAGE_DATA_DIRECTORY DataDirectory[16]; */ + {{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, + {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}} + }}; + + struct pe_header pe_header = pe_template; + + int i; + FILE *op; + DWORD file_offset, sum; + struct section_info *si; + IMAGE_SECTION_HEADER *psh; + + op = fopen(pe->filename, "wb"); + if (NULL == op) { + tcc_error_noabort("could not write '%s': %s", pe->filename, strerror(errno)); + return -1; + } + + pe->sizeofheaders = pe_file_align(pe, + sizeof (struct pe_header) + + pe->sec_count * sizeof (IMAGE_SECTION_HEADER) + ); + + file_offset = pe->sizeofheaders; + + if (2 == pe->s1->verbose) + printf("-------------------------------" + "\n virt file size section" "\n"); + for (i = 0; i < pe->sec_count; ++i) { + DWORD addr, size; + const char *sh_name; + + si = pe->sec_info + i; + sh_name = si->name; + addr = si->sh_addr - pe->imagebase; + size = si->sh_size; + psh = &si->ish; + + if (2 == pe->s1->verbose) + printf("%6x %6x %6x %s\n", + (unsigned)addr, (unsigned)file_offset, (unsigned)size, sh_name); + + switch (si->cls) { + case sec_text: + pe_header.opthdr.BaseOfCode = addr; + break; + + case sec_data: +#ifndef TCC_TARGET_X86_64 + pe_header.opthdr.BaseOfData = addr; +#endif + break; + + case sec_bss: + break; + + case sec_reloc: + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_BASERELOC, addr, size); + break; + + case sec_rsrc: + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_RESOURCE, addr, size); + break; + + case sec_pdata: + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXCEPTION, addr, size); + break; + + case sec_stab: + break; + } + + if (pe->thunk == pe->s1->sections[si->ord]) { + if (pe->imp_size) { + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IMPORT, + pe->imp_offs + addr, pe->imp_size); + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_IAT, + pe->iat_offs + addr, pe->iat_size); + } + if (pe->exp_size) { + pe_set_datadir(&pe_header, IMAGE_DIRECTORY_ENTRY_EXPORT, + pe->exp_offs + addr, pe->exp_size); + } + } + + strncpy((char*)psh->Name, sh_name, sizeof psh->Name); + + psh->Characteristics = pe_sec_flags[si->cls]; + psh->VirtualAddress = addr; + psh->Misc.VirtualSize = size; + pe_header.opthdr.SizeOfImage = + umax(pe_virtual_align(pe, size + addr), pe_header.opthdr.SizeOfImage); + + if (si->data_size) { + psh->PointerToRawData = file_offset; + file_offset = pe_file_align(pe, file_offset + si->data_size); + psh->SizeOfRawData = file_offset - psh->PointerToRawData; + if (si->cls == sec_text) + pe_header.opthdr.SizeOfCode += psh->SizeOfRawData; + else + pe_header.opthdr.SizeOfInitializedData += psh->SizeOfRawData; + } + } + + //pe_header.filehdr.TimeDateStamp = time(NULL); + pe_header.filehdr.NumberOfSections = pe->sec_count; + pe_header.opthdr.AddressOfEntryPoint = pe->start_addr; + pe_header.opthdr.SizeOfHeaders = pe->sizeofheaders; + pe_header.opthdr.ImageBase = pe->imagebase; + pe_header.opthdr.Subsystem = pe->subsystem; + if (pe->s1->pe_stack_size) + pe_header.opthdr.SizeOfStackReserve = pe->s1->pe_stack_size; + if (PE_DLL == pe->type) + pe_header.filehdr.Characteristics = CHARACTERISTICS_DLL; + pe_header.filehdr.Characteristics |= pe->s1->pe_characteristics; + + sum = 0; + pe_fwrite(&pe_header, sizeof pe_header, op, &sum); + for (i = 0; i < pe->sec_count; ++i) + pe_fwrite(&pe->sec_info[i].ish, sizeof(IMAGE_SECTION_HEADER), op, &sum); + pe_fpad(op, pe->sizeofheaders); + for (i = 0; i < pe->sec_count; ++i) { + si = pe->sec_info + i; + psh = &si->ish; + if (si->data_size) { + pe_fwrite(si->data, si->data_size, op, &sum); + file_offset = psh->PointerToRawData + psh->SizeOfRawData; + pe_fpad(op, file_offset); + } + } + + pe_header.opthdr.CheckSum = sum + file_offset; + fseek(op, offsetof(struct pe_header, opthdr.CheckSum), SEEK_SET); + pe_fwrite(&pe_header.opthdr.CheckSum, sizeof pe_header.opthdr.CheckSum, op, NULL); + fclose (op); +#ifndef _WIN32 + chmod(pe->filename, 0777); +#endif + + if (2 == pe->s1->verbose) + printf("-------------------------------\n"); + if (pe->s1->verbose) + printf("<- %s (%u bytes)\n", pe->filename, (unsigned)file_offset); + + return 0; +} + +/*----------------------------------------------------------------------------*/ + +static struct import_symbol *pe_add_import(struct pe_info *pe, int sym_index) +{ + int i; + int dll_index; + struct pe_import_info *p; + struct import_symbol *s; + ElfW(Sym) *isym; + + isym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index; + dll_index = isym->st_size; + + i = dynarray_assoc ((void**)pe->imp_info, pe->imp_count, dll_index); + if (-1 != i) { + p = pe->imp_info[i]; + goto found_dll; + } + p = tcc_mallocz(sizeof *p); + p->dll_index = dll_index; + dynarray_add(&pe->imp_info, &pe->imp_count, p); + +found_dll: + i = dynarray_assoc ((void**)p->symbols, p->sym_count, sym_index); + if (-1 != i) + return p->symbols[i]; + + s = tcc_mallocz(sizeof *s); + dynarray_add(&p->symbols, &p->sym_count, s); + s->sym_index = sym_index; + return s; +} + +void pe_free_imports(struct pe_info *pe) +{ + int i; + for (i = 0; i < pe->imp_count; ++i) { + struct pe_import_info *p = pe->imp_info[i]; + dynarray_reset(&p->symbols, &p->sym_count); + } + dynarray_reset(&pe->imp_info, &pe->imp_count); +} + +/*----------------------------------------------------------------------------*/ +static void pe_build_imports(struct pe_info *pe) +{ + int thk_ptr, ent_ptr, dll_ptr, sym_cnt, i; + DWORD rva_base = pe->thunk->sh_addr - pe->imagebase; + int ndlls = pe->imp_count; + + for (sym_cnt = i = 0; i < ndlls; ++i) + sym_cnt += pe->imp_info[i]->sym_count; + + if (0 == sym_cnt) + return; + + pe_align_section(pe->thunk, 16); + + pe->imp_offs = dll_ptr = pe->thunk->data_offset; + pe->imp_size = (ndlls + 1) * sizeof(IMAGE_IMPORT_DESCRIPTOR); + pe->iat_offs = dll_ptr + pe->imp_size; + pe->iat_size = (sym_cnt + ndlls) * sizeof(ADDR3264); + section_ptr_add(pe->thunk, pe->imp_size + 2*pe->iat_size); + + thk_ptr = pe->iat_offs; + ent_ptr = pe->iat_offs + pe->iat_size; + + for (i = 0; i < pe->imp_count; ++i) { + IMAGE_IMPORT_DESCRIPTOR *hdr; + int k, n, dllindex; + ADDR3264 v; + struct pe_import_info *p = pe->imp_info[i]; + const char *name; + DLLReference *dllref; + + dllindex = p->dll_index; + if (dllindex) + name = (dllref = pe->s1->loaded_dlls[dllindex-1])->name; + else + name = "", dllref = NULL; + + /* put the dll name into the import header */ + v = put_elf_str(pe->thunk, name); + hdr = (IMAGE_IMPORT_DESCRIPTOR*)(pe->thunk->data + dll_ptr); + hdr->FirstThunk = thk_ptr + rva_base; + hdr->OriginalFirstThunk = ent_ptr + rva_base; + hdr->Name = v + rva_base; + + for (k = 0, n = p->sym_count; k <= n; ++k) { + if (k < n) { + int iat_index = p->symbols[k]->iat_index; + int sym_index = p->symbols[k]->sym_index; + ElfW(Sym) *imp_sym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index; + ElfW(Sym) *org_sym = (ElfW(Sym) *)symtab_section->data + iat_index; + const char *name = (char*)pe->s1->dynsymtab_section->link->data + imp_sym->st_name; + int ordinal; + + org_sym->st_value = thk_ptr; + org_sym->st_shndx = pe->thunk->sh_num; + + if (dllref) + v = 0, ordinal = imp_sym->st_value; /* ordinal from pe_load_def */ + else + ordinal = 0, v = imp_sym->st_value; /* address from tcc_add_symbol() */ + +#ifdef TCC_IS_NATIVE + if (pe->type == PE_RUN) { + if (dllref) { + if ( !dllref->handle ) + dllref->handle = LoadLibrary(dllref->name); + v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name); + } + if (!v) + tcc_error_noabort("can't build symbol '%s'", name); + } else +#endif + if (ordinal) { + v = ordinal | (ADDR3264)1 << (sizeof(ADDR3264)*8 - 1); + } else { + v = pe->thunk->data_offset + rva_base; + section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */ + put_elf_str(pe->thunk, name); + } + + } else { + v = 0; /* last entry is zero */ + } + + *(ADDR3264*)(pe->thunk->data+thk_ptr) = + *(ADDR3264*)(pe->thunk->data+ent_ptr) = v; + thk_ptr += sizeof (ADDR3264); + ent_ptr += sizeof (ADDR3264); + } + dll_ptr += sizeof(IMAGE_IMPORT_DESCRIPTOR); + } +} + +/* ------------------------------------------------------------- */ + +struct pe_sort_sym +{ + int index; + const char *name; +}; + +static int sym_cmp(const void *va, const void *vb) +{ + const char *ca = (*(struct pe_sort_sym**)va)->name; + const char *cb = (*(struct pe_sort_sym**)vb)->name; + return strcmp(ca, cb); +} + +static void pe_build_exports(struct pe_info *pe) +{ + ElfW(Sym) *sym; + int sym_index, sym_end; + DWORD rva_base, func_o, name_o, ord_o, str_o; + IMAGE_EXPORT_DIRECTORY *hdr; + int sym_count, ord; + struct pe_sort_sym **sorted, *p; + + FILE *op; + char buf[260]; + const char *dllname; + const char *name; + + rva_base = pe->thunk->sh_addr - pe->imagebase; + sym_count = 0, sorted = NULL, op = NULL; + + sym_end = symtab_section->data_offset / sizeof(ElfW(Sym)); + for (sym_index = 1; sym_index < sym_end; ++sym_index) { + sym = (ElfW(Sym)*)symtab_section->data + sym_index; + name = pe_export_name(pe->s1, sym); + if ((sym->st_other & ST_PE_EXPORT) + /* export only symbols from actually written sections */ + && pe->s1->sections[sym->st_shndx]->sh_addr) { + p = tcc_malloc(sizeof *p); + p->index = sym_index; + p->name = name; + dynarray_add(&sorted, &sym_count, p); + } +#if 0 + if (sym->st_other & ST_PE_EXPORT) + printf("export: %s\n", name); + if (sym->st_other & ST_PE_STDCALL) + printf("stdcall: %s\n", name); +#endif + } + + if (0 == sym_count) + return; + + qsort (sorted, sym_count, sizeof *sorted, sym_cmp); + + pe_align_section(pe->thunk, 16); + dllname = tcc_basename(pe->filename); + + pe->exp_offs = pe->thunk->data_offset; + func_o = pe->exp_offs + sizeof(IMAGE_EXPORT_DIRECTORY); + name_o = func_o + sym_count * sizeof (DWORD); + ord_o = name_o + sym_count * sizeof (DWORD); + str_o = ord_o + sym_count * sizeof(WORD); + + hdr = section_ptr_add(pe->thunk, str_o - pe->exp_offs); + hdr->Characteristics = 0; + hdr->Base = 1; + hdr->NumberOfFunctions = sym_count; + hdr->NumberOfNames = sym_count; + hdr->AddressOfFunctions = func_o + rva_base; + hdr->AddressOfNames = name_o + rva_base; + hdr->AddressOfNameOrdinals = ord_o + rva_base; + hdr->Name = str_o + rva_base; + put_elf_str(pe->thunk, dllname); + +#if 1 + /* automatically write exports to .def */ + pstrcpy(buf, sizeof buf, pe->filename); + strcpy(tcc_fileextension(buf), ".def"); + op = fopen(buf, "wb"); + if (NULL == op) { + tcc_error_noabort("could not create '%s': %s", buf, strerror(errno)); + } else { + fprintf(op, "LIBRARY %s\n\nEXPORTS\n", dllname); + if (pe->s1->verbose) + printf("<- %s (%d symbol%s)\n", buf, sym_count, &"s"[sym_count < 2]); + } +#endif + + for (ord = 0; ord < sym_count; ++ord) + { + p = sorted[ord], sym_index = p->index, name = p->name; + /* insert actual address later in relocate_section() */ + put_elf_reloc(symtab_section, pe->thunk, + func_o, R_XXX_RELATIVE, sym_index); + *(DWORD*)(pe->thunk->data + name_o) + = pe->thunk->data_offset + rva_base; + *(WORD*)(pe->thunk->data + ord_o) + = ord; + put_elf_str(pe->thunk, name); + func_o += sizeof (DWORD); + name_o += sizeof (DWORD); + ord_o += sizeof (WORD); + if (op) + fprintf(op, "%s\n", name); + } + pe->exp_size = pe->thunk->data_offset - pe->exp_offs; + dynarray_reset(&sorted, &sym_count); + if (op) + fclose(op); +} + +/* ------------------------------------------------------------- */ +static void pe_build_reloc (struct pe_info *pe) +{ + DWORD offset, block_ptr, addr; + int count, i; + ElfW_Rel *rel, *rel_end; + Section *s = NULL, *sr; + + offset = addr = block_ptr = count = i = 0; + rel = rel_end = NULL; + + for(;;) { + if (rel < rel_end) { + int type = ELFW(R_TYPE)(rel->r_info); + addr = rel->r_offset + s->sh_addr; + ++ rel; + if (type != REL_TYPE_DIRECT) + continue; + if (count == 0) { /* new block */ + block_ptr = pe->reloc->data_offset; + section_ptr_add(pe->reloc, sizeof(struct pe_reloc_header)); + offset = addr & 0xFFFFFFFF<<12; + } + if ((addr -= offset) < (1<<12)) { /* one block spans 4k addresses */ + WORD *wp = section_ptr_add(pe->reloc, sizeof (WORD)); + *wp = addr | PE_IMAGE_REL<<12; + ++count; + continue; + } + -- rel; + + } else if (i < pe->sec_count) { + sr = (s = pe->s1->sections[pe->sec_info[i++].ord])->reloc; + if (sr) { + rel = (ElfW_Rel *)sr->data; + rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); + } + continue; + } + + if (count) { + /* store the last block and ready for a new one */ + struct pe_reloc_header *hdr; + if (count & 1) /* align for DWORDS */ + section_ptr_add(pe->reloc, sizeof(WORD)), ++count; + hdr = (struct pe_reloc_header *)(pe->reloc->data + block_ptr); + hdr -> offset = offset - pe->imagebase; + hdr -> size = count * sizeof(WORD) + sizeof(struct pe_reloc_header); + count = 0; + } + + if (rel >= rel_end) + break; + } +} + +/* ------------------------------------------------------------- */ +static int pe_section_class(Section *s) +{ + int type, flags; + const char *name; + + type = s->sh_type; + flags = s->sh_flags; + name = s->name; + if (flags & SHF_ALLOC) { + if (type == SHT_PROGBITS) { + if (flags & SHF_EXECINSTR) + return sec_text; + if (flags & SHF_WRITE) + return sec_data; + if (0 == strcmp(name, ".rsrc")) + return sec_rsrc; + if (0 == strcmp(name, ".iedat")) + return sec_idata; + if (0 == strcmp(name, ".pdata")) + return sec_pdata; + return sec_other; + } else if (type == SHT_NOBITS) { + if (flags & SHF_WRITE) + return sec_bss; + } + } else { + if (0 == strcmp(name, ".reloc")) + return sec_reloc; + if (0 == strncmp(name, ".stab", 5)) /* .stab and .stabstr */ + return sec_stab; + } + return -1; +} + +static int pe_assign_addresses (struct pe_info *pe) +{ + int i, k, o, c; + DWORD addr; + int *section_order; + struct section_info *si; + Section *s; + + if (PE_DLL == pe->type) + pe->reloc = new_section(pe->s1, ".reloc", SHT_PROGBITS, 0); + + // pe->thunk = new_section(pe->s1, ".iedat", SHT_PROGBITS, SHF_ALLOC); + + section_order = tcc_malloc(pe->s1->nb_sections * sizeof (int)); + for (o = k = 0 ; k < sec_last; ++k) { + for (i = 1; i < pe->s1->nb_sections; ++i) { + s = pe->s1->sections[i]; + if (k == pe_section_class(s)) { + // printf("%s %d\n", s->name, k); + s->sh_addr = pe->imagebase; + section_order[o++] = i; + } + } + } + + pe->sec_info = tcc_mallocz(o * sizeof (struct section_info)); + addr = pe->imagebase + 1; + + for (i = 0; i < o; ++i) + { + k = section_order[i]; + s = pe->s1->sections[k]; + c = pe_section_class(s); + si = &pe->sec_info[pe->sec_count]; + +#ifdef PE_MERGE_DATA + if (c == sec_bss && pe->sec_count && si[-1].cls == sec_data) { + /* append .bss to .data */ + s->sh_addr = addr = ((addr-1) | (s->sh_addralign-1)) + 1; + addr += s->data_offset; + si[-1].sh_size = addr - si[-1].sh_addr; + continue; + } +#endif + if (c == sec_stab && 0 == pe->s1->do_debug) + continue; + + strcpy(si->name, s->name); + si->cls = c; + si->ord = k; + si->sh_addr = s->sh_addr = addr = pe_virtual_align(pe, addr); + si->sh_flags = s->sh_flags; + + if (c == sec_data && NULL == pe->thunk) + pe->thunk = s; + + if (s == pe->thunk) { + pe_build_imports(pe); + pe_build_exports(pe); + } + + if (c == sec_reloc) + pe_build_reloc (pe); + + if (s->data_offset) + { + if (s->sh_type != SHT_NOBITS) { + si->data = s->data; + si->data_size = s->data_offset; + } + + addr += s->data_offset; + si->sh_size = s->data_offset; + ++pe->sec_count; + } + // printf("%08x %05x %s\n", si->sh_addr, si->sh_size, si->name); + } + +#if 0 + for (i = 1; i < pe->s1->nb_sections; ++i) { + Section *s = pe->s1->sections[i]; + int type = s->sh_type; + int flags = s->sh_flags; + printf("section %-16s %-10s %5x %s,%s,%s\n", + s->name, + type == SHT_PROGBITS ? "progbits" : + type == SHT_NOBITS ? "nobits" : + type == SHT_SYMTAB ? "symtab" : + type == SHT_STRTAB ? "strtab" : + type == SHT_RELX ? "rel" : "???", + s->data_offset, + flags & SHF_ALLOC ? "alloc" : "", + flags & SHF_WRITE ? "write" : "", + flags & SHF_EXECINSTR ? "exec" : "" + ); + } + pe->s1->verbose = 2; +#endif + + tcc_free(section_order); + return 0; +} + +/*----------------------------------------------------------------------------*/ + +static int pe_isafunc(int sym_index) +{ + Section *sr = text_section->reloc; + ElfW_Rel *rel, *rel_end; + Elf32_Word info = ELF32_R_INFO(sym_index, R_386_PC32); + if (!sr) + return 0; + rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); + for (rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) + if (rel->r_info == info) + return 1; + return 0; +} + +/*----------------------------------------------------------------------------*/ +static int pe_check_symbols(struct pe_info *pe) +{ + ElfW(Sym) *sym; + int sym_index, sym_end; + int ret = 0; + + pe_align_section(text_section, 8); + + sym_end = symtab_section->data_offset / sizeof(ElfW(Sym)); + for (sym_index = 1; sym_index < sym_end; ++sym_index) { + + sym = (ElfW(Sym) *)symtab_section->data + sym_index; + if (sym->st_shndx == SHN_UNDEF) { + + const char *name = (char*)symtab_section->link->data + sym->st_name; + unsigned type = ELFW(ST_TYPE)(sym->st_info); + int imp_sym = pe_find_import(pe->s1, sym); + struct import_symbol *is; + + if (imp_sym <= 0) + goto not_found; + + if (type == STT_NOTYPE) { + /* symbols from assembler have no type, find out which */ + if (pe_isafunc(sym_index)) + type = STT_FUNC; + else + type = STT_OBJECT; + } + + is = pe_add_import(pe, imp_sym); + + if (type == STT_FUNC) { + unsigned long offset = is->thk_offset; + if (offset) { + /* got aliased symbol, like stricmp and _stricmp */ + + } else { + char buffer[100]; + WORD *p; + + offset = text_section->data_offset; + /* add the 'jmp IAT[x]' instruction */ +#ifdef TCC_TARGET_ARM + p = section_ptr_add(text_section, 8+4); // room for code and address + (*(DWORD*)(p)) = 0xE59FC000; // arm code ldr ip, [pc] ; PC+8+0 = 0001xxxx + (*(DWORD*)(p+2)) = 0xE59CF000; // arm code ldr pc, [ip] +#else + p = section_ptr_add(text_section, 8); + *p = 0x25FF; +#ifdef TCC_TARGET_X86_64 + *(DWORD*)(p+1) = (DWORD)-4; +#endif +#endif + /* add a helper symbol, will be patched later in + pe_build_imports */ + sprintf(buffer, "IAT.%s", name); + is->iat_index = put_elf_sym( + symtab_section, 0, sizeof(DWORD), + ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), + 0, SHN_UNDEF, buffer); +#ifdef TCC_TARGET_ARM + put_elf_reloc(symtab_section, text_section, + offset + 8, R_XXX_THUNKFIX, is->iat_index); // offset to IAT position +#else + put_elf_reloc(symtab_section, text_section, + offset + 2, R_XXX_THUNKFIX, is->iat_index); +#endif + is->thk_offset = offset; + } + + /* tcc_realloc might have altered sym's address */ + sym = (ElfW(Sym) *)symtab_section->data + sym_index; + + /* patch the original symbol */ + sym->st_value = offset; + sym->st_shndx = text_section->sh_num; + sym->st_other &= ~ST_PE_EXPORT; /* do not export */ + continue; + } + + if (type == STT_OBJECT) { /* data, ptr to that should be */ + if (0 == is->iat_index) { + /* original symbol will be patched later in pe_build_imports */ + is->iat_index = sym_index; + continue; + } + } + + not_found: + if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK) + /* STB_WEAK undefined symbols are accepted */ + continue; + tcc_error_noabort("undefined symbol '%s'%s", name, + imp_sym < 0 ? ", missing __declspec(dllimport)?":""); + ret = -1; + + } else if (pe->s1->rdynamic + && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + /* if -rdynamic option, then export all non local symbols */ + sym->st_other |= ST_PE_EXPORT; + } + } + return ret; +} + +/*----------------------------------------------------------------------------*/ +#ifdef PE_PRINT_SECTIONS +static void pe_print_section(FILE * f, Section * s) +{ + /* just if you're curious */ + BYTE *p, *e, b; + int i, n, l, m; + p = s->data; + e = s->data + s->data_offset; + l = e - p; + + fprintf(f, "section \"%s\"", s->name); + if (s->link) + fprintf(f, "\nlink \"%s\"", s->link->name); + if (s->reloc) + fprintf(f, "\nreloc \"%s\"", s->reloc->name); + fprintf(f, "\nv_addr %08X", (unsigned)s->sh_addr); + fprintf(f, "\ncontents %08X", (unsigned)l); + fprintf(f, "\n\n"); + + if (s->sh_type == SHT_NOBITS) + return; + + if (0 == l) + return; + + if (s->sh_type == SHT_SYMTAB) + m = sizeof(ElfW(Sym)); + else if (s->sh_type == SHT_RELX) + m = sizeof(ElfW_Rel); + else + m = 16; + + fprintf(f, "%-8s", "offset"); + for (i = 0; i < m; ++i) + fprintf(f, " %02x", i); + n = 56; + + if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_RELX) { + const char *fields1[] = { + "name", + "value", + "size", + "bind", + "type", + "other", + "shndx", + NULL + }; + + const char *fields2[] = { + "offs", + "type", + "symb", + NULL + }; + + const char **p; + + if (s->sh_type == SHT_SYMTAB) + p = fields1, n = 106; + else + p = fields2, n = 58; + + for (i = 0; p[i]; ++i) + fprintf(f, "%6s", p[i]); + fprintf(f, " symbol"); + } + + fprintf(f, "\n"); + for (i = 0; i < n; ++i) + fprintf(f, "-"); + fprintf(f, "\n"); + + for (i = 0; i < l;) + { + fprintf(f, "%08X", i); + for (n = 0; n < m; ++n) { + if (n + i < l) + fprintf(f, " %02X", p[i + n]); + else + fprintf(f, " "); + } + + if (s->sh_type == SHT_SYMTAB) { + ElfW(Sym) *sym = (ElfW(Sym) *) (p + i); + const char *name = s->link->data + sym->st_name; + fprintf(f, " %04X %04X %04X %02X %02X %02X %04X \"%s\"", + (unsigned)sym->st_name, + (unsigned)sym->st_value, + (unsigned)sym->st_size, + (unsigned)ELFW(ST_BIND)(sym->st_info), + (unsigned)ELFW(ST_TYPE)(sym->st_info), + (unsigned)sym->st_other, + (unsigned)sym->st_shndx, + name); + + } else if (s->sh_type == SHT_RELX) { + ElfW_Rel *rel = (ElfW_Rel *) (p + i); + ElfW(Sym) *sym = + (ElfW(Sym) *) s->link->data + ELFW(R_SYM)(rel->r_info); + const char *name = s->link->link->data + sym->st_name; + fprintf(f, " %04X %02X %04X \"%s\"", + (unsigned)rel->r_offset, + (unsigned)ELFW(R_TYPE)(rel->r_info), + (unsigned)ELFW(R_SYM)(rel->r_info), + name); + } else { + fprintf(f, " "); + for (n = 0; n < m; ++n) { + if (n + i < l) { + b = p[i + n]; + if (b < 32 || b >= 127) + b = '.'; + fprintf(f, "%c", b); + } + } + } + i += m; + fprintf(f, "\n"); + } + fprintf(f, "\n\n"); +} + +static void pe_print_sections(TCCState *s1, const char *fname) +{ + Section *s; + FILE *f; + int i; + f = fopen(fname, "w"); + for (i = 1; i < s1->nb_sections; ++i) { + s = s1->sections[i]; + pe_print_section(f, s); + } + pe_print_section(f, s1->dynsymtab_section); + fclose(f); +} +#endif + +/* ------------------------------------------------------------- */ +/* helper function for load/store to insert one more indirection */ + +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 +ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2) +{ + int r2; + if ((sv->r & (VT_VALMASK|VT_SYM)) != (VT_CONST|VT_SYM) || (sv->r2 != VT_CONST)) + return sv; + if (!sv->sym->a.dllimport) + return sv; + // printf("import %04x %04x %04x %s\n", sv->type.t, sv->sym->type.t, sv->r, get_tok_str(sv->sym->v, NULL)); + memset(v2, 0, sizeof *v2); + v2->type.t = VT_PTR; + v2->r = VT_CONST | VT_SYM | VT_LVAL; + v2->sym = sv->sym; + + r2 = get_reg(RC_INT); + load(r2, v2); + v2->r = r2; + if ((uint32_t)sv->c.i) { + vpushv(v2); + vpushi(sv->c.i); + gen_opi('+'); + *v2 = *vtop--; + } + v2->type.t = sv->type.t; + v2->r |= sv->r & VT_LVAL; + return v2; +} +#endif + +ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value) +{ + return set_elf_sym( + s1->dynsymtab_section, + value, + dllindex, /* st_size */ + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), + 0, + value ? SHN_ABS : SHN_UNDEF, + name + ); +} + +static int add_dllref(TCCState *s1, const char *dllname) +{ + DLLReference *dllref; + int i; + for (i = 0; i < s1->nb_loaded_dlls; ++i) + if (0 == strcmp(s1->loaded_dlls[i]->name, dllname)) + return i + 1; + dllref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname)); + strcpy(dllref->name, dllname); + dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref); + return s1->nb_loaded_dlls; +} + +/* ------------------------------------------------------------- */ + +static int read_mem(int fd, unsigned offset, void *buffer, unsigned len) +{ + lseek(fd, offset, SEEK_SET); + return len == read(fd, buffer, len); +} + +/* ------------------------------------------------------------- */ + +PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp) +{ + int l, i, n, n0, ret; + char *p; + int fd; + + IMAGE_SECTION_HEADER ish; + IMAGE_EXPORT_DIRECTORY ied; + IMAGE_DOS_HEADER dh; + IMAGE_FILE_HEADER ih; + DWORD sig, ref, addr, ptr, namep; + + int pef_hdroffset, opt_hdroffset, sec_hdroffset; + + n = n0 = 0; + p = NULL; + ret = -1; + + fd = open(filename, O_RDONLY | O_BINARY); + if (fd < 0) + goto the_end_1; + ret = 1; + if (!read_mem(fd, 0, &dh, sizeof dh)) + goto the_end; + if (!read_mem(fd, dh.e_lfanew, &sig, sizeof sig)) + goto the_end; + if (sig != 0x00004550) + goto the_end; + pef_hdroffset = dh.e_lfanew + sizeof sig; + if (!read_mem(fd, pef_hdroffset, &ih, sizeof ih)) + goto the_end; + opt_hdroffset = pef_hdroffset + sizeof ih; + if (ih.Machine == 0x014C) { + IMAGE_OPTIONAL_HEADER32 oh; + sec_hdroffset = opt_hdroffset + sizeof oh; + if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh)) + goto the_end; + if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes) + goto the_end_0; + addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; + } else if (ih.Machine == 0x8664) { + IMAGE_OPTIONAL_HEADER64 oh; + sec_hdroffset = opt_hdroffset + sizeof oh; + if (!read_mem(fd, opt_hdroffset, &oh, sizeof oh)) + goto the_end; + if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes) + goto the_end_0; + addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; + } else + goto the_end; + + //printf("addr: %08x\n", addr); + for (i = 0; i < ih.NumberOfSections; ++i) { + if (!read_mem(fd, sec_hdroffset + i * sizeof ish, &ish, sizeof ish)) + goto the_end; + //printf("vaddr: %08x\n", ish.VirtualAddress); + if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData) + goto found; + } + goto the_end_0; + +found: + ref = ish.VirtualAddress - ish.PointerToRawData; + if (!read_mem(fd, addr - ref, &ied, sizeof ied)) + goto the_end; + + namep = ied.AddressOfNames - ref; + for (i = 0; i < ied.NumberOfNames; ++i) { + if (!read_mem(fd, namep, &ptr, sizeof ptr)) + goto the_end; + namep += sizeof ptr; + for (l = 0;;) { + if (n+1 >= n0) + p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256); + if (!read_mem(fd, ptr - ref + l++, p + n, 1)) { + tcc_free(p), p = NULL; + goto the_end; + } + if (p[n++] == 0) + break; + } + } + if (p) + p[n] = 0; +the_end_0: + ret = 0; +the_end: + close(fd); +the_end_1: + *pp = p; + return ret; +} + +/* ------------------------------------------------------------- + * This is for compiled windows resources in 'coff' format + * as generated by 'windres.exe -O coff ...'. + */ + +static int pe_load_res(TCCState *s1, int fd) +{ + struct pe_rsrc_header hdr; + Section *rsrc_section; + int i, ret = -1, sym_index; + BYTE *ptr; + unsigned offs; + + if (!read_mem(fd, 0, &hdr, sizeof hdr)) + goto quit; + + if (hdr.filehdr.Machine != IMAGE_FILE_MACHINE + || hdr.filehdr.NumberOfSections != 1 + || strcmp((char*)hdr.sectionhdr.Name, ".rsrc") != 0) + goto quit; + + rsrc_section = new_section(s1, ".rsrc", SHT_PROGBITS, SHF_ALLOC); + ptr = section_ptr_add(rsrc_section, hdr.sectionhdr.SizeOfRawData); + offs = hdr.sectionhdr.PointerToRawData; + if (!read_mem(fd, offs, ptr, hdr.sectionhdr.SizeOfRawData)) + goto quit; + offs = hdr.sectionhdr.PointerToRelocations; + sym_index = put_elf_sym(symtab_section, 0, 0, 0, 0, rsrc_section->sh_num, ".rsrc"); + for (i = 0; i < hdr.sectionhdr.NumberOfRelocations; ++i) { + struct pe_rsrc_reloc rel; + if (!read_mem(fd, offs, &rel, sizeof rel)) + goto quit; + // printf("rsrc_reloc: %x %x %x\n", rel.offset, rel.size, rel.type); + if (rel.type != RSRC_RELTYPE) + goto quit; + put_elf_reloc(symtab_section, rsrc_section, + rel.offset, R_XXX_RELATIVE, sym_index); + offs += sizeof rel; + } + ret = 0; +quit: + return ret; +} + +/* ------------------------------------------------------------- */ + +static char *trimfront(char *p) +{ + while (*p && (unsigned char)*p <= ' ') + ++p; + return p; +} + +static char *trimback(char *a, char *e) +{ + while (e > a && (unsigned char)e[-1] <= ' ') + --e; + *e = 0;; + return a; +} + +/* ------------------------------------------------------------- */ +static int pe_load_def(TCCState *s1, int fd) +{ + int state = 0, ret = -1, dllindex = 0, ord; + char line[400], dllname[80], *p, *x; + FILE *fp; + + fp = fdopen(dup(fd), "rb"); + while (fgets(line, sizeof line, fp)) + { + p = trimfront(trimback(line, strchr(line, 0))); + if (0 == *p || ';' == *p) + continue; + + switch (state) { + case 0: + if (0 != strnicmp(p, "LIBRARY", 7)) + goto quit; + pstrcpy(dllname, sizeof dllname, trimfront(p+7)); + ++state; + continue; + + case 1: + if (0 != stricmp(p, "EXPORTS")) + goto quit; + ++state; + continue; + + case 2: + dllindex = add_dllref(s1, dllname); + ++state; + /* fall through */ + default: + /* get ordinal and will store in sym->st_value */ + ord = 0; + x = strchr(p, ' '); + if (x) { + *x = 0, x = strrchr(x + 1, '@'); + if (x) { + char *d; + ord = (int)strtol(x + 1, &d, 10); + if (*d) + ord = 0; + } + } + pe_putimport(s1, dllindex, p, ord); + continue; + } + } + ret = 0; +quit: + fclose(fp); + return ret; +} + +/* ------------------------------------------------------------- */ +static int pe_load_dll(TCCState *s1, const char *filename) +{ + char *p, *q; + int index, ret; + + ret = tcc_get_dllexports(filename, &p); + if (ret) { + return -1; + } else if (p) { + index = add_dllref(s1, tcc_basename(filename)); + for (q = p; *q; q += 1 + strlen(q)) + pe_putimport(s1, index, q, 0); + tcc_free(p); + } + return 0; +} + +/* ------------------------------------------------------------- */ +ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd) +{ + int ret = -1; + char buf[10]; + if (0 == strcmp(tcc_fileextension(filename), ".def")) + ret = pe_load_def(s1, fd); + else if (pe_load_res(s1, fd) == 0) + ret = 0; + else if (read_mem(fd, 0, buf, 4) && 0 == memcmp(buf, "MZ\220", 4)) + ret = pe_load_dll(s1, filename); + return ret; +} + +/* ------------------------------------------------------------- */ +#ifdef TCC_TARGET_X86_64 +static unsigned pe_add_uwwind_info(TCCState *s1) +{ + if (NULL == s1->uw_pdata) { + s1->uw_pdata = find_section(tcc_state, ".pdata"); + s1->uw_pdata->sh_addralign = 4; + } + if (0 == s1->uw_sym) + s1->uw_sym = put_elf_sym(symtab_section, 0, 0, 0, 0, text_section->sh_num, ".uw_base"); + if (0 == s1->uw_offs) { + /* As our functions all have the same stackframe, we use one entry for all */ + static const unsigned char uw_info[] = { + 0x01, // UBYTE: 3 Version , UBYTE: 5 Flags + 0x04, // UBYTE Size of prolog + 0x02, // UBYTE Count of unwind codes + 0x05, // UBYTE: 4 Frame Register (rbp), UBYTE: 4 Frame Register offset (scaled) + // USHORT * n Unwind codes array + // 0x0b, 0x01, 0xff, 0xff, // stack size + 0x04, 0x03, // set frame ptr (mov rsp -> rbp) + 0x01, 0x50 // push reg (rbp) + }; + + Section *s = text_section; + unsigned char *p; + + section_ptr_add(s, -s->data_offset & 3); /* align */ + s1->uw_offs = s->data_offset; + p = section_ptr_add(s, sizeof uw_info); + memcpy(p, uw_info, sizeof uw_info); + } + + return s1->uw_offs; +} + +ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack) +{ + TCCState *s1 = tcc_state; + Section *pd; + unsigned o, n, d; + struct /* _RUNTIME_FUNCTION */ { + DWORD BeginAddress; + DWORD EndAddress; + DWORD UnwindData; + } *p; + + d = pe_add_uwwind_info(s1); + pd = s1->uw_pdata; + o = pd->data_offset; + p = section_ptr_add(pd, sizeof *p); + + /* record this function */ + p->BeginAddress = start; + p->EndAddress = end; + p->UnwindData = d; + + /* put relocations on it */ + for (n = o + sizeof *p; o < n; o += sizeof p->BeginAddress) + put_elf_reloc(symtab_section, pd, o, R_XXX_RELATIVE, s1->uw_sym); +} +#endif +/* ------------------------------------------------------------- */ +#ifdef TCC_TARGET_X86_64 +#define PE_STDSYM(n,s) n +#else +#define PE_STDSYM(n,s) "_" n s +#endif + +static void pe_add_runtime(TCCState *s1, struct pe_info *pe) +{ + const char *start_symbol; + int pe_type = 0; + int unicode_entry = 0; + + if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16"))) + pe_type = PE_GUI; + else + if (find_elf_sym(symtab_section, PE_STDSYM("wWinMain","@16"))) { + pe_type = PE_GUI; + unicode_entry = PE_GUI; + } + else + if (TCC_OUTPUT_DLL == s1->output_type) { + pe_type = PE_DLL; + /* need this for 'tccelf.c:relocate_section()' */ + s1->output_type = TCC_OUTPUT_EXE; + } + else { + pe_type = PE_EXE; + if (find_elf_sym(symtab_section, "wmain")) + unicode_entry = PE_EXE; + } + + start_symbol = + TCC_OUTPUT_MEMORY == s1->output_type + ? PE_GUI == pe_type ? (unicode_entry ? "__runwwinmain" : "__runwinmain") + : (unicode_entry ? "__runwmain" : "__runmain") + : PE_DLL == pe_type ? PE_STDSYM("__dllstart","@12") + : PE_GUI == pe_type ? (unicode_entry ? "__wwinstart": "__winstart") + : (unicode_entry ? "__wstart" : "__start") + ; + + if (!s1->leading_underscore || strchr(start_symbol, '@')) + ++start_symbol; + + /* grab the startup code from libtcc1 */ +#ifdef TCC_IS_NATIVE + if (TCC_OUTPUT_MEMORY != s1->output_type || s1->runtime_main) +#endif + set_elf_sym(symtab_section, + 0, 0, + ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, + SHN_UNDEF, start_symbol); + + tcc_add_pragma_libs(s1); + + if (0 == s1->nostdlib) { + static const char *libs[] = { + TCC_LIBTCC1, "msvcrt", "kernel32", "", "user32", "gdi32", NULL + }; + const char **pp, *p; + for (pp = libs; 0 != (p = *pp); ++pp) { + if (0 == *p) { + if (PE_DLL != pe_type && PE_GUI != pe_type) + break; + } else if (pp == libs && tcc_add_dll(s1, p, 0) >= 0) { + continue; + } else { + tcc_add_library_err(s1, p); + } + } + } + + if (TCC_OUTPUT_MEMORY == s1->output_type) + pe_type = PE_RUN; + pe->type = pe_type; + pe->start_symbol = start_symbol; +} + +static void pe_set_options(TCCState * s1, struct pe_info *pe) +{ + if (PE_DLL == pe->type) { + /* XXX: check if is correct for arm-pe target */ + pe->imagebase = 0x10000000; + } else { +#if defined(TCC_TARGET_ARM) + pe->imagebase = 0x00010000; +#else + pe->imagebase = 0x00400000; +#endif + } + +#if defined(TCC_TARGET_ARM) + /* we use "console" subsystem by default */ + pe->subsystem = 9; +#else + if (PE_DLL == pe->type || PE_GUI == pe->type) + pe->subsystem = 2; + else + pe->subsystem = 3; +#endif + /* Allow override via -Wl,-subsystem=... option */ + if (s1->pe_subsystem != 0) + pe->subsystem = s1->pe_subsystem; + + /* set default file/section alignment */ + if (pe->subsystem == 1) { + pe->section_align = 0x20; + pe->file_align = 0x20; + } else { + pe->section_align = 0x1000; + pe->file_align = 0x200; + } + + if (s1->section_align != 0) + pe->section_align = s1->section_align; + if (s1->pe_file_align != 0) + pe->file_align = s1->pe_file_align; + + if ((pe->subsystem >= 10) && (pe->subsystem <= 12)) + pe->imagebase = 0; + + if (s1->has_text_addr) + pe->imagebase = s1->text_addr; +} + +ST_FUNC int pe_output_file(TCCState *s1, const char *filename) +{ + int ret; + struct pe_info pe; + int i; + + memset(&pe, 0, sizeof pe); + pe.filename = filename; + pe.s1 = s1; + + tcc_add_bcheck(s1); + pe_add_runtime(s1, &pe); + resolve_common_syms(s1); + pe_set_options(s1, &pe); + + ret = pe_check_symbols(&pe); + if (ret) + ; + else if (filename) { + pe_assign_addresses(&pe); + relocate_syms(s1, s1->symtab, 0); + s1->pe_imagebase = pe.imagebase; + for (i = 1; i < s1->nb_sections; ++i) { + Section *s = s1->sections[i]; + if (s->reloc) { + relocate_section(s1, s); + } + } + pe.start_addr = (DWORD) + ((uintptr_t)tcc_get_symbol_err(s1, pe.start_symbol) + - pe.imagebase); + if (s1->nb_errors) + ret = -1; + else + ret = pe_write(&pe); + tcc_free(pe.sec_info); + } else { +#ifdef TCC_IS_NATIVE + pe.thunk = data_section; + pe_build_imports(&pe); + s1->runtime_main = pe.start_symbol; +#ifdef TCC_TARGET_X86_64 + s1->uw_pdata = find_section(s1, ".pdata"); +#endif +#endif + } + + pe_free_imports(&pe); + +#ifdef PE_PRINT_SECTIONS + pe_print_sections(s1, "tcc.log"); +#endif + return ret; +} + +/* ------------------------------------------------------------- */ diff --git a/05/tcc-final-old/tccpp.c b/05/tcc-final-old/tccpp.c new file mode 100644 index 0000000..76f9e42 --- /dev/null +++ b/05/tcc-final-old/tccpp.c @@ -0,0 +1,3903 @@ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "tcc.h" + +/********************************************************/ +/* global variables */ + +ST_DATA int tok_flags; +ST_DATA int parse_flags; + +ST_DATA struct BufferedFile *file; +ST_DATA int ch, tok; +ST_DATA CValue tokc; +ST_DATA const int *macro_ptr; +ST_DATA CString tokcstr; /* current parsed string, if any */ + +/* display benchmark infos */ +ST_DATA int total_lines; +ST_DATA int total_bytes; +ST_DATA int tok_ident; +ST_DATA TokenSym **table_ident; + +/* ------------------------------------------------------------------------- */ + +static TokenSym *hash_ident[TOK_HASH_SIZE]; +static char token_buf[STRING_MAX_SIZE + 1]; +static CString cstr_buf; +static CString macro_equal_buf; +static TokenString tokstr_buf; +static unsigned char isidnum_table[256 - CH_EOF]; +static int pp_debug_tok, pp_debug_symv; +static int pp_once; +static int pp_expr; +static int pp_counter; +static void tok_print(const char *msg, const int *str); + +static struct TinyAlloc *toksym_alloc; +static struct TinyAlloc *tokstr_alloc; +static struct TinyAlloc *cstr_alloc; + +static TokenString *macro_stack; + +static const char tcc_keywords[] = +#define DEF(id, str) str "\0" +#include "tcctok.h" +#undef DEF +; + +/* WARNING: the content of this string encodes token numbers */ +static const unsigned char tok_two_chars[] = +/* outdated -- gr + "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253" + "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266"; +*/{ + '<','=', TOK_LE, + '>','=', TOK_GE, + '!','=', TOK_NE, + '&','&', TOK_LAND, + '|','|', TOK_LOR, + '+','+', TOK_INC, + '-','-', TOK_DEC, + '=','=', TOK_EQ, + '<','<', TOK_SHL, + '>','>', TOK_SAR, + '+','=', TOK_A_ADD, + '-','=', TOK_A_SUB, + '*','=', TOK_A_MUL, + '/','=', TOK_A_DIV, + '%','=', TOK_A_MOD, + '&','=', TOK_A_AND, + '^','=', TOK_A_XOR, + '|','=', TOK_A_OR, + '-','>', TOK_ARROW, + '.','.', TOK_TWODOTS, + '#','#', TOK_TWOSHARPS, + 0 +}; + +static void next_nomacro_spc(void); + +ST_FUNC void skip(int c) +{ + if (tok != c) + tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc)); + next(); +} + +ST_FUNC void expect(const char *msg) +{ + tcc_error("%s expected", msg); +} + +/* ------------------------------------------------------------------------- */ +/* Custom allocator for tiny objects */ + +#define USE_TAL + +#ifndef USE_TAL +#define tal_free(al, p) tcc_free(p) +#define tal_realloc(al, p, size) tcc_realloc(p, size) +#define tal_new(a,b,c) +#define tal_delete(a) +#else +#if !defined(MEM_DEBUG) +#define tal_free(al, p) tal_free_impl(al, p) +#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size) +#define TAL_DEBUG_PARAMS +#else +#define TAL_DEBUG 1 +//#define TAL_INFO 1 /* collect and dump allocators stats */ +#define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__) +#define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__) +#define TAL_DEBUG_PARAMS , const char *file, int line +#define TAL_DEBUG_FILE_LEN 40 +#endif + +#define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */ +#define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */ +#define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */ +#define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */ +#define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */ +#define CSTR_TAL_LIMIT 1024 + +typedef struct TinyAlloc { + unsigned limit; + unsigned size; + uint8_t *buffer; + uint8_t *p; + unsigned nb_allocs; + struct TinyAlloc *next, *top; +#ifdef TAL_INFO + unsigned nb_peak; + unsigned nb_total; + unsigned nb_missed; + uint8_t *peak_p; +#endif +} TinyAlloc; + +typedef struct tal_header_t { + unsigned size; +#ifdef TAL_DEBUG + int line_num; /* negative line_num used for double free check */ + char file_name[TAL_DEBUG_FILE_LEN + 1]; +#endif +} tal_header_t; + +/* ------------------------------------------------------------------------- */ + +static TinyAlloc *tal_new(TinyAlloc **pal, unsigned limit, unsigned size) +{ + TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc)); + al->p = al->buffer = tcc_malloc(size); + al->limit = limit; + al->size = size; + if (pal) *pal = al; + return al; +} + +static void tal_delete(TinyAlloc *al) +{ + TinyAlloc *next; + +tail_call: + if (!al) + return; +#ifdef TAL_INFO + fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n", + al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed, + (al->peak_p - al->buffer) * 100.0 / al->size); +#endif +#ifdef TAL_DEBUG + if (al->nb_allocs > 0) { + uint8_t *p; + fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n", + al->nb_allocs, al->limit); + p = al->buffer; + while (p < al->p) { + tal_header_t *header = (tal_header_t *)p; + if (header->line_num > 0) { + fprintf(stderr, "%s:%d: chunk of %d bytes leaked\n", + header->file_name, header->line_num, header->size); + } + p += header->size + sizeof(tal_header_t); + } +#if MEM_DEBUG-0 == 2 + exit(2); +#endif + } +#endif + next = al->next; + tcc_free(al->buffer); + tcc_free(al); + al = next; + goto tail_call; +} + +static void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS) +{ + if (!p) + return; +tail_call: + if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) { +#ifdef TAL_DEBUG + tal_header_t *header = (((tal_header_t *)p) - 1); + if (header->line_num < 0) { + fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n", + file, line); + fprintf(stderr, "%s:%d: %d bytes\n", + header->file_name, (int)-header->line_num, (int)header->size); + } else + header->line_num = -header->line_num; +#endif + al->nb_allocs--; + if (!al->nb_allocs) + al->p = al->buffer; + } else if (al->next) { + al = al->next; + goto tail_call; + } + else + tcc_free(p); +} + +static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS) +{ + tal_header_t *header; + void *ret; + int is_own; + unsigned adj_size = (size + 3) & -4; + TinyAlloc *al = *pal; + +tail_call: + is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size); + if ((!p || is_own) && size <= al->limit) { + if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) { + header = (tal_header_t *)al->p; + header->size = adj_size; +#ifdef TAL_DEBUG + { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN; + strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN); + header->file_name[TAL_DEBUG_FILE_LEN] = 0; + header->line_num = line; } +#endif + ret = al->p + sizeof(tal_header_t); + al->p += adj_size + sizeof(tal_header_t); + if (is_own) { + header = (((tal_header_t *)p) - 1); + memcpy(ret, p, header->size); +#ifdef TAL_DEBUG + header->line_num = -header->line_num; +#endif + } else { + al->nb_allocs++; + } +#ifdef TAL_INFO + if (al->nb_peak < al->nb_allocs) + al->nb_peak = al->nb_allocs; + if (al->peak_p < al->p) + al->peak_p = al->p; + al->nb_total++; +#endif + return ret; + } else if (is_own) { + al->nb_allocs--; + ret = tal_realloc(*pal, 0, size); + header = (((tal_header_t *)p) - 1); + memcpy(ret, p, header->size); +#ifdef TAL_DEBUG + header->line_num = -header->line_num; +#endif + return ret; + } + if (al->next) { + al = al->next; + } else { + TinyAlloc *bottom = al, *next = al->top ? al->top : al; + + al = tal_new(pal, next->limit, next->size * 2); + al->next = next; + bottom->top = al; + } + goto tail_call; + } + if (is_own) { + al->nb_allocs--; + ret = tcc_malloc(size); + header = (((tal_header_t *)p) - 1); + memcpy(ret, p, header->size); +#ifdef TAL_DEBUG + header->line_num = -header->line_num; +#endif + } else if (al->next) { + al = al->next; + goto tail_call; + } else + ret = tcc_realloc(p, size); +#ifdef TAL_INFO + al->nb_missed++; +#endif + return ret; +} + +#endif /* USE_TAL */ + +/* ------------------------------------------------------------------------- */ +/* CString handling */ +static void cstr_realloc(CString *cstr, int new_size) +{ + int size; + + size = cstr->size_allocated; + if (size < 8) + size = 8; /* no need to allocate a too small first string */ + while (size < new_size) + size = size * 2; + cstr->data = tal_realloc(cstr_alloc, cstr->data, size); + cstr->size_allocated = size; +} + +/* add a byte */ +ST_INLN void cstr_ccat(CString *cstr, int ch) +{ + int size; + size = cstr->size + 1; + if (size > cstr->size_allocated) + cstr_realloc(cstr, size); + ((unsigned char *)cstr->data)[size - 1] = ch; + cstr->size = size; +} + +ST_FUNC void cstr_cat(CString *cstr, const char *str, int len) +{ + int size; + if (len <= 0) + len = strlen(str) + 1 + len; + size = cstr->size + len; + if (size > cstr->size_allocated) + cstr_realloc(cstr, size); + memmove(((unsigned char *)cstr->data) + cstr->size, str, len); + cstr->size = size; +} + +/* add a wide char */ +ST_FUNC void cstr_wccat(CString *cstr, int ch) +{ + int size; + size = cstr->size + sizeof(nwchar_t); + if (size > cstr->size_allocated) + cstr_realloc(cstr, size); + *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch; + cstr->size = size; +} + +ST_FUNC void cstr_new(CString *cstr) +{ + memset(cstr, 0, sizeof(CString)); +} + +/* free string and reset it to NULL */ +ST_FUNC void cstr_free(CString *cstr) +{ + tal_free(cstr_alloc, cstr->data); + cstr_new(cstr); +} + +/* reset string to empty */ +ST_FUNC void cstr_reset(CString *cstr) +{ + cstr->size = 0; +} + +/* XXX: unicode ? */ +static void add_char(CString *cstr, int c) +{ + if (c == '\'' || c == '\"' || c == '\\') { + /* XXX: could be more precise if char or string */ + cstr_ccat(cstr, '\\'); + } + if (c >= 32 && c <= 126) { + cstr_ccat(cstr, c); + } else { + cstr_ccat(cstr, '\\'); + if (c == '\n') { + cstr_ccat(cstr, 'n'); + } else { + cstr_ccat(cstr, '0' + ((c >> 6) & 7)); + cstr_ccat(cstr, '0' + ((c >> 3) & 7)); + cstr_ccat(cstr, '0' + (c & 7)); + } + } +} + +/* ------------------------------------------------------------------------- */ +/* allocate a new token */ +static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len) +{ + TokenSym *ts, **ptable; + int i; + + if (tok_ident >= SYM_FIRST_ANOM) + tcc_error("memory full (symbols)"); + + /* expand token table if needed */ + i = tok_ident - TOK_IDENT; + if ((i % TOK_ALLOC_INCR) == 0) { + ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *)); + table_ident = ptable; + } + + ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len); + table_ident[i] = ts; + ts->tok = tok_ident++; + ts->sym_define = NULL; + ts->sym_label = NULL; + ts->sym_struct = NULL; + ts->sym_identifier = NULL; + ts->len = len; + ts->hash_next = NULL; + memcpy(ts->str, str, len); + ts->str[len] = '\0'; + *pts = ts; + return ts; +} + +#define TOK_HASH_INIT 1 +#define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c)) + + +/* find a token and add it if not found */ +ST_FUNC TokenSym *tok_alloc(const char *str, int len) +{ + TokenSym *ts, **pts; + int i; + unsigned int h; + + h = TOK_HASH_INIT; + for(i=0;ilen == len && !memcmp(ts->str, str, len)) + return ts; + pts = &(ts->hash_next); + } + return tok_alloc_new(pts, str, len); +} + +/* XXX: buffer overflow */ +/* XXX: float tokens */ +ST_FUNC const char *get_tok_str(int v, CValue *cv) +{ + char *p; + int i, len; + + cstr_reset(&cstr_buf); + p = cstr_buf.data; + + switch(v) { + case TOK_CINT: + case TOK_CUINT: + case TOK_CLONG: + case TOK_CULONG: + case TOK_CLLONG: + case TOK_CULLONG: + /* XXX: not quite exact, but only useful for testing */ +#ifdef _WIN32 + sprintf(p, "%u", (unsigned)cv->i); +#else + sprintf(p, "%llu", (unsigned long long)cv->i); +#endif + break; + case TOK_LCHAR: + cstr_ccat(&cstr_buf, 'L'); + case TOK_CCHAR: + cstr_ccat(&cstr_buf, '\''); + add_char(&cstr_buf, cv->i); + cstr_ccat(&cstr_buf, '\''); + cstr_ccat(&cstr_buf, '\0'); + break; + case TOK_PPNUM: + case TOK_PPSTR: + return (char*)cv->str.data; + case TOK_LSTR: + cstr_ccat(&cstr_buf, 'L'); + case TOK_STR: + cstr_ccat(&cstr_buf, '\"'); + if (v == TOK_STR) { + len = cv->str.size - 1; + for(i=0;istr.data)[i]); + } else { + len = (cv->str.size / sizeof(nwchar_t)) - 1; + for(i=0;istr.data)[i]); + } + cstr_ccat(&cstr_buf, '\"'); + cstr_ccat(&cstr_buf, '\0'); + break; + + case TOK_CFLOAT: + cstr_cat(&cstr_buf, "", 0); + break; + case TOK_CDOUBLE: + cstr_cat(&cstr_buf, "", 0); + break; + case TOK_CLDOUBLE: + cstr_cat(&cstr_buf, "", 0); + break; + case TOK_LINENUM: + cstr_cat(&cstr_buf, "", 0); + break; + + /* above tokens have value, the ones below don't */ + case TOK_LT: + v = '<'; + goto addv; + case TOK_GT: + v = '>'; + goto addv; + case TOK_DOTS: + return strcpy(p, "..."); + case TOK_A_SHL: + return strcpy(p, "<<="); + case TOK_A_SAR: + return strcpy(p, ">>="); + case TOK_EOF: + return strcpy(p, ""); + default: + if (v < TOK_IDENT) { + /* search in two bytes table */ + const unsigned char *q = tok_two_chars; + while (*q) { + if (q[2] == v) { + *p++ = q[0]; + *p++ = q[1]; + *p = '\0'; + return cstr_buf.data; + } + q += 3; + } + if (v >= 127) { + sprintf(cstr_buf.data, "<%02x>", v); + return cstr_buf.data; + } + addv: + *p++ = v; + *p = '\0'; + } else if (v < tok_ident) { + return table_ident[v - TOK_IDENT]->str; + } else if (v >= SYM_FIRST_ANOM) { + /* special name for anonymous symbol */ + sprintf(p, "L.%u", v - SYM_FIRST_ANOM); + } else { + /* should never happen */ + return NULL; + } + break; + } + return cstr_buf.data; +} + +/* return the current character, handling end of block if necessary + (but not stray) */ +ST_FUNC int handle_eob(void) +{ + BufferedFile *bf = file; + int len; + + /* only tries to read if really end of buffer */ + if (bf->buf_ptr >= bf->buf_end) { + if (bf->fd >= 0) { +#if defined(PARSE_DEBUG) + len = 1; +#else + len = IO_BUF_SIZE; +#endif + len = read(bf->fd, bf->buffer, len); + if (len < 0) + len = 0; + } else { + len = 0; + } + total_bytes += len; + bf->buf_ptr = bf->buffer; + bf->buf_end = bf->buffer + len; + *bf->buf_end = CH_EOB; + } + if (bf->buf_ptr < bf->buf_end) { + return bf->buf_ptr[0]; + } else { + bf->buf_ptr = bf->buf_end; + return CH_EOF; + } +} + +/* read next char from current input file and handle end of input buffer */ +ST_INLN void inp(void) +{ + ch = *(++(file->buf_ptr)); + /* end of buffer/file handling */ + if (ch == CH_EOB) + ch = handle_eob(); +} + +/* handle '\[\r]\n' */ +static int handle_stray_noerror(void) +{ + while (ch == '\\') { + inp(); + if (ch == '\n') { + file->line_num++; + inp(); + } else if (ch == '\r') { + inp(); + if (ch != '\n') + goto fail; + file->line_num++; + inp(); + } else { + fail: + return 1; + } + } + return 0; +} + +static void handle_stray(void) +{ + if (handle_stray_noerror()) + tcc_error("stray '\\' in program"); +} + +/* skip the stray and handle the \\n case. Output an error if + incorrect char after the stray */ +static int handle_stray1(uint8_t *p) +{ + int c; + + file->buf_ptr = p; + if (p >= file->buf_end) { + c = handle_eob(); + if (c != '\\') + return c; + p = file->buf_ptr; + } + ch = *p; + if (handle_stray_noerror()) { + if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) + tcc_error("stray '\\' in program"); + *--file->buf_ptr = '\\'; + } + p = file->buf_ptr; + c = *p; + return c; +} + +/* handle just the EOB case, but not stray */ +#define PEEKC_EOB(c, p)\ +{\ + p++;\ + c = *p;\ + if (c == '\\') {\ + file->buf_ptr = p;\ + c = handle_eob();\ + p = file->buf_ptr;\ + }\ +} + +/* handle the complicated stray case */ +#define PEEKC(c, p)\ +{\ + p++;\ + c = *p;\ + if (c == '\\') {\ + c = handle_stray1(p);\ + p = file->buf_ptr;\ + }\ +} + +/* input with '\[\r]\n' handling. Note that this function cannot + handle other characters after '\', so you cannot call it inside + strings or comments */ +ST_FUNC void minp(void) +{ + inp(); + if (ch == '\\') + handle_stray(); +} + +/* single line C++ comments */ +static uint8_t *parse_line_comment(uint8_t *p) +{ + int c; + + p++; + for(;;) { + c = *p; + redo: + if (c == '\n' || c == CH_EOF) { + break; + } else if (c == '\\') { + file->buf_ptr = p; + c = handle_eob(); + p = file->buf_ptr; + if (c == '\\') { + PEEKC_EOB(c, p); + if (c == '\n') { + file->line_num++; + PEEKC_EOB(c, p); + } else if (c == '\r') { + PEEKC_EOB(c, p); + if (c == '\n') { + file->line_num++; + PEEKC_EOB(c, p); + } + } + } else { + goto redo; + } + } else { + p++; + } + } + return p; +} + +/* C comments */ +ST_FUNC uint8_t *parse_comment(uint8_t *p) +{ + int c; + + p++; + for(;;) { + /* fast skip loop */ + for(;;) { + c = *p; + if (c == '\n' || c == '*' || c == '\\') + break; + p++; + c = *p; + if (c == '\n' || c == '*' || c == '\\') + break; + p++; + } + /* now we can handle all the cases */ + if (c == '\n') { + file->line_num++; + p++; + } else if (c == '*') { + p++; + for(;;) { + c = *p; + if (c == '*') { + p++; + } else if (c == '/') { + goto end_of_comment; + } else if (c == '\\') { + file->buf_ptr = p; + c = handle_eob(); + p = file->buf_ptr; + if (c == CH_EOF) + tcc_error("unexpected end of file in comment"); + if (c == '\\') { + /* skip '\[\r]\n', otherwise just skip the stray */ + while (c == '\\') { + PEEKC_EOB(c, p); + if (c == '\n') { + file->line_num++; + PEEKC_EOB(c, p); + } else if (c == '\r') { + PEEKC_EOB(c, p); + if (c == '\n') { + file->line_num++; + PEEKC_EOB(c, p); + } + } else { + goto after_star; + } + } + } + } else { + break; + } + } + after_star: ; + } else { + /* stray, eob or eof */ + file->buf_ptr = p; + c = handle_eob(); + p = file->buf_ptr; + if (c == CH_EOF) { + tcc_error("unexpected end of file in comment"); + } else if (c == '\\') { + p++; + } + } + } + end_of_comment: + p++; + return p; +} + +ST_FUNC int set_idnum(int c, int val) +{ + int prev = isidnum_table[c - CH_EOF]; + isidnum_table[c - CH_EOF] = val; + return prev; +} + +#define cinp minp + +static inline void skip_spaces(void) +{ + while (isidnum_table[ch - CH_EOF] & IS_SPC) + cinp(); +} + +static inline int check_space(int t, int *spc) +{ + if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) { + if (*spc) + return 1; + *spc = 1; + } else + *spc = 0; + return 0; +} + +/* parse a string without interpreting escapes */ +static uint8_t *parse_pp_string(uint8_t *p, + int sep, CString *str) +{ + int c; + p++; + for(;;) { + c = *p; + if (c == sep) { + break; + } else if (c == '\\') { + file->buf_ptr = p; + c = handle_eob(); + p = file->buf_ptr; + if (c == CH_EOF) { + unterminated_string: + /* XXX: indicate line number of start of string */ + tcc_error("missing terminating %c character", sep); + } else if (c == '\\') { + /* escape : just skip \[\r]\n */ + PEEKC_EOB(c, p); + if (c == '\n') { + file->line_num++; + p++; + } else if (c == '\r') { + PEEKC_EOB(c, p); + if (c != '\n') + expect("'\n' after '\r'"); + file->line_num++; + p++; + } else if (c == CH_EOF) { + goto unterminated_string; + } else { + if (str) { + cstr_ccat(str, '\\'); + cstr_ccat(str, c); + } + p++; + } + } + } else if (c == '\n') { + file->line_num++; + goto add_char; + } else if (c == '\r') { + PEEKC_EOB(c, p); + if (c != '\n') { + if (str) + cstr_ccat(str, '\r'); + } else { + file->line_num++; + goto add_char; + } + } else { + add_char: + if (str) + cstr_ccat(str, c); + p++; + } + } + p++; + return p; +} + +/* skip block of text until #else, #elif or #endif. skip also pairs of + #if/#endif */ +static void preprocess_skip(void) +{ + int a, start_of_line, c, in_warn_or_error; + uint8_t *p; + + p = file->buf_ptr; + a = 0; +redo_start: + start_of_line = 1; + in_warn_or_error = 0; + for(;;) { + redo_no_start: + c = *p; + switch(c) { + case ' ': + case '\t': + case '\f': + case '\v': + case '\r': + p++; + goto redo_no_start; + case '\n': + file->line_num++; + p++; + goto redo_start; + case '\\': + file->buf_ptr = p; + c = handle_eob(); + if (c == CH_EOF) { + expect("#endif"); + } else if (c == '\\') { + ch = file->buf_ptr[0]; + handle_stray_noerror(); + } + p = file->buf_ptr; + goto redo_no_start; + /* skip strings */ + case '\"': + case '\'': + if (in_warn_or_error) + goto _default; + p = parse_pp_string(p, c, NULL); + break; + /* skip comments */ + case '/': + if (in_warn_or_error) + goto _default; + file->buf_ptr = p; + ch = *p; + minp(); + p = file->buf_ptr; + if (ch == '*') { + p = parse_comment(p); + } else if (ch == '/') { + p = parse_line_comment(p); + } + break; + case '#': + p++; + if (start_of_line) { + file->buf_ptr = p; + next_nomacro(); + p = file->buf_ptr; + if (a == 0 && + (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF)) + goto the_end; + if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF) + a++; + else if (tok == TOK_ENDIF) + a--; + else if( tok == TOK_ERROR || tok == TOK_WARNING) + in_warn_or_error = 1; + else if (tok == TOK_LINEFEED) + goto redo_start; + else if (parse_flags & PARSE_FLAG_ASM_FILE) + p = parse_line_comment(p - 1); + } else if (parse_flags & PARSE_FLAG_ASM_FILE) + p = parse_line_comment(p - 1); + break; +_default: + default: + p++; + break; + } + start_of_line = 0; + } + the_end: ; + file->buf_ptr = p; +} + +#if 0 +/* return the number of additional 'ints' necessary to store the + token */ +static inline int tok_size(const int *p) +{ + switch(*p) { + /* 4 bytes */ + case TOK_CINT: + case TOK_CUINT: + case TOK_CCHAR: + case TOK_LCHAR: + case TOK_CFLOAT: + case TOK_LINENUM: + return 1 + 1; + case TOK_STR: + case TOK_LSTR: + case TOK_PPNUM: + case TOK_PPSTR: + return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2); + case TOK_CLONG: + case TOK_CULONG: + return 1 + LONG_SIZE / 4; + case TOK_CDOUBLE: + case TOK_CLLONG: + case TOK_CULLONG: + return 1 + 2; + case TOK_CLDOUBLE: + return 1 + LDOUBLE_SIZE / 4; + default: + return 1 + 0; + } +} +#endif + +/* token string handling */ +ST_INLN void tok_str_new(TokenString *s) +{ + s->str = NULL; + s->len = s->lastlen = 0; + s->allocated_len = 0; + s->last_line_num = -1; +} + +ST_FUNC TokenString *tok_str_alloc(void) +{ + TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str); + tok_str_new(str); + return str; +} + +ST_FUNC int *tok_str_dup(TokenString *s) +{ + int *str; + + str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int)); + memcpy(str, s->str, s->len * sizeof(int)); + return str; +} + +ST_FUNC void tok_str_free_str(int *str) +{ + tal_free(tokstr_alloc, str); +} + +ST_FUNC void tok_str_free(TokenString *str) +{ + tok_str_free_str(str->str); + tal_free(tokstr_alloc, str); +} + +ST_FUNC int *tok_str_realloc(TokenString *s, int new_size) +{ + int *str, size; + + size = s->allocated_len; + if (size < 16) + size = 16; + while (size < new_size) + size = size * 2; + if (size > s->allocated_len) { + str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int)); + s->allocated_len = size; + s->str = str; + } + return s->str; +} + +ST_FUNC void tok_str_add(TokenString *s, int t) +{ + int len, *str; + + len = s->len; + str = s->str; + if (len >= s->allocated_len) + str = tok_str_realloc(s, len + 1); + str[len++] = t; + s->len = len; +} + +ST_FUNC void begin_macro(TokenString *str, int alloc) +{ + str->alloc = alloc; + str->prev = macro_stack; + str->prev_ptr = macro_ptr; + str->save_line_num = file->line_num; + macro_ptr = str->str; + macro_stack = str; +} + +ST_FUNC void end_macro(void) +{ + TokenString *str = macro_stack; + macro_stack = str->prev; + macro_ptr = str->prev_ptr; + file->line_num = str->save_line_num; + if (str->alloc == 2) { + str->alloc = 3; /* just mark as finished */ + } else { + tok_str_free(str); + } +} + +static void tok_str_add2(TokenString *s, int t, CValue *cv) +{ + int len, *str; + + len = s->lastlen = s->len; + str = s->str; + + /* allocate space for worst case */ + if (len + TOK_MAX_SIZE >= s->allocated_len) + str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1); + str[len++] = t; + switch(t) { + case TOK_CINT: + case TOK_CUINT: + case TOK_CCHAR: + case TOK_LCHAR: + case TOK_CFLOAT: + case TOK_LINENUM: +#if LONG_SIZE == 4 + case TOK_CLONG: + case TOK_CULONG: +#endif + str[len++] = cv->tab[0]; + break; + case TOK_PPNUM: + case TOK_PPSTR: + case TOK_STR: + case TOK_LSTR: + { + /* Insert the string into the int array. */ + size_t nb_words = + 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int); + if (len + nb_words >= s->allocated_len) + str = tok_str_realloc(s, len + nb_words + 1); + str[len] = cv->str.size; + memcpy(&str[len + 1], cv->str.data, cv->str.size); + len += nb_words; + } + break; + case TOK_CDOUBLE: + case TOK_CLLONG: + case TOK_CULLONG: +#if LONG_SIZE == 8 + case TOK_CLONG: + case TOK_CULONG: +#endif +#if LDOUBLE_SIZE == 8 + case TOK_CLDOUBLE: +#endif + str[len++] = cv->tab[0]; + str[len++] = cv->tab[1]; + break; +#if LDOUBLE_SIZE == 12 + case TOK_CLDOUBLE: + str[len++] = cv->tab[0]; + str[len++] = cv->tab[1]; + str[len++] = cv->tab[2]; +#elif LDOUBLE_SIZE == 16 + case TOK_CLDOUBLE: + str[len++] = cv->tab[0]; + str[len++] = cv->tab[1]; + str[len++] = cv->tab[2]; + str[len++] = cv->tab[3]; +#elif LDOUBLE_SIZE != 8 +#error add long double size support +#endif + break; + default: + break; + } + s->len = len; +} + +/* add the current parse token in token string 's' */ +ST_FUNC void tok_str_add_tok(TokenString *s) +{ + CValue cval; + + /* save line number info */ + if (file->line_num != s->last_line_num) { + s->last_line_num = file->line_num; + cval.i = s->last_line_num; + tok_str_add2(s, TOK_LINENUM, &cval); + } + tok_str_add2(s, tok, &tokc); +} + +/* get a token from an integer array and increment pointer + accordingly. we code it as a macro to avoid pointer aliasing. */ +static inline void TOK_GET(int *t, const int **pp, CValue *cv) +{ + const int *p = *pp; + int n, *tab; + + tab = cv->tab; + switch(*t = *p++) { +#if LONG_SIZE == 4 + case TOK_CLONG: +#endif + case TOK_CINT: + case TOK_CCHAR: + case TOK_LCHAR: + case TOK_LINENUM: + cv->i = *p++; + break; +#if LONG_SIZE == 4 + case TOK_CULONG: +#endif + case TOK_CUINT: + cv->i = (unsigned)*p++; + break; + case TOK_CFLOAT: + tab[0] = *p++; + break; + case TOK_STR: + case TOK_LSTR: + case TOK_PPNUM: + case TOK_PPSTR: + cv->str.size = *p++; + cv->str.data = p; + p += (cv->str.size + sizeof(int) - 1) / sizeof(int); + break; + case TOK_CDOUBLE: + case TOK_CLLONG: + case TOK_CULLONG: +#if LONG_SIZE == 8 + case TOK_CLONG: + case TOK_CULONG: +#endif + n = 2; + goto copy; + case TOK_CLDOUBLE: +#if LDOUBLE_SIZE == 16 + n = 4; +#elif LDOUBLE_SIZE == 12 + n = 3; +#elif LDOUBLE_SIZE == 8 + n = 2; +#else +# error add long double size support +#endif + copy: + do + *tab++ = *p++; + while (--n); + break; + default: + break; + } + *pp = p; +} + +static int macro_is_equal(const int *a, const int *b) +{ + CValue cv; + int t; + + if (!a || !b) + return 1; + + while (*a && *b) { + /* first time preallocate macro_equal_buf, next time only reset position to start */ + cstr_reset(¯o_equal_buf); + TOK_GET(&t, &a, &cv); + cstr_cat(¯o_equal_buf, get_tok_str(t, &cv), 0); + TOK_GET(&t, &b, &cv); + if (strcmp(macro_equal_buf.data, get_tok_str(t, &cv))) + return 0; + } + return !(*a || *b); +} + +/* defines handling */ +ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg) +{ + Sym *s, *o; + + o = define_find(v); + s = sym_push2(&define_stack, v, macro_type, 0); + s->d = str; + s->next = first_arg; + table_ident[v - TOK_IDENT]->sym_define = s; + + if (o && !macro_is_equal(o->d, s->d)) + tcc_warning("%s redefined", get_tok_str(v, NULL)); +} + +/* undefined a define symbol. Its name is just set to zero */ +ST_FUNC void define_undef(Sym *s) +{ + int v = s->v; + if (v >= TOK_IDENT && v < tok_ident) + table_ident[v - TOK_IDENT]->sym_define = NULL; +} + +ST_INLN Sym *define_find(int v) +{ + v -= TOK_IDENT; + if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT)) + return NULL; + return table_ident[v]->sym_define; +} + +/* free define stack until top reaches 'b' */ +ST_FUNC void free_defines(Sym *b) +{ + while (define_stack != b) { + Sym *top = define_stack; + define_stack = top->prev; + tok_str_free_str(top->d); + define_undef(top); + sym_free(top); + } + + /* restore remaining (-D or predefined) symbols if they were + #undef'd in the file */ + while (b) { + int v = b->v; + if (v >= TOK_IDENT && v < tok_ident) { + Sym **d = &table_ident[v - TOK_IDENT]->sym_define; + if (!*d) + *d = b; + } + b = b->prev; + } +} + +/* label lookup */ +ST_FUNC Sym *label_find(int v) +{ + v -= TOK_IDENT; + if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT)) + return NULL; + return table_ident[v]->sym_label; +} + +ST_FUNC Sym *label_push(Sym **ptop, int v, int flags) +{ + Sym *s, **ps; + s = sym_push2(ptop, v, 0, 0); + s->r = flags; + ps = &table_ident[v - TOK_IDENT]->sym_label; + if (ptop == &global_label_stack) { + /* modify the top most local identifier, so that + sym_identifier will point to 's' when popped */ + while (*ps != NULL) + ps = &(*ps)->prev_tok; + } + s->prev_tok = *ps; + *ps = s; + return s; +} + +/* pop labels until element last is reached. Look if any labels are + undefined. Define symbols if '&&label' was used. */ +ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep) +{ + Sym *s, *s1; + for(s = *ptop; s != slast; s = s1) { + s1 = s->prev; + if (s->r == LABEL_DECLARED) { + tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL)); + } else if (s->r == LABEL_FORWARD) { + tcc_error("label '%s' used but not defined", + get_tok_str(s->v, NULL)); + } else { + if (s->c) { + /* define corresponding symbol. A size of + 1 is put. */ + put_extern_sym(s, cur_text_section, s->jnext, 1); + } + } + /* remove label */ + table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok; + if (!keep) + sym_free(s); + } + if (!keep) + *ptop = slast; +} + +/* fake the nth "#if defined test_..." for tcc -dt -run */ +static void maybe_run_test(TCCState *s) +{ + const char *p; + if (s->include_stack_ptr != s->include_stack) + return; + p = get_tok_str(tok, NULL); + if (0 != memcmp(p, "test_", 5)) + return; + if (0 != --s->run_test) + return; + fprintf(s->ppfp, "\n[%s]\n" + !(s->dflag & 32), p), fflush(s->ppfp); + define_push(tok, MACRO_OBJ, NULL, NULL); +} + +/* eval an expression for #if/#elif */ +static int expr_preprocess(void) +{ + int c, t; + TokenString *str; + + str = tok_str_alloc(); + pp_expr = 1; + while (tok != TOK_LINEFEED && tok != TOK_EOF) { + next(); /* do macro subst */ + if (tok == TOK_DEFINED) { + next_nomacro(); + t = tok; + if (t == '(') + next_nomacro(); + if (tok < TOK_IDENT) + expect("identifier"); + if (tcc_state->run_test) + maybe_run_test(tcc_state); + c = define_find(tok) != 0; + if (t == '(') { + next_nomacro(); + if (tok != ')') + expect("')'"); + } + tok = TOK_CINT; + tokc.i = c; + } else if (tok >= TOK_IDENT) { + /* if undefined macro */ + tok = TOK_CINT; + tokc.i = 0; + } + tok_str_add_tok(str); + } + pp_expr = 0; + tok_str_add(str, -1); /* simulate end of file */ + tok_str_add(str, 0); + /* now evaluate C constant expression */ + begin_macro(str, 1); + next(); + c = expr_const(); + end_macro(); + return c != 0; +} + + +/* parse after #define */ +ST_FUNC void parse_define(void) +{ + Sym *s, *first, **ps; + int v, t, varg, is_vaargs, spc; + int saved_parse_flags = parse_flags; + + v = tok; + if (v < TOK_IDENT || v == TOK_DEFINED) + tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc)); + /* XXX: should check if same macro (ANSI) */ + first = NULL; + t = MACRO_OBJ; + /* We have to parse the whole define as if not in asm mode, in particular + no line comment with '#' must be ignored. Also for function + macros the argument list must be parsed without '.' being an ID + character. */ + parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES); + /* '(' must be just after macro definition for MACRO_FUNC */ + next_nomacro_spc(); + if (tok == '(') { + int dotid = set_idnum('.', 0); + next_nomacro(); + ps = &first; + if (tok != ')') for (;;) { + varg = tok; + next_nomacro(); + is_vaargs = 0; + if (varg == TOK_DOTS) { + varg = TOK___VA_ARGS__; + is_vaargs = 1; + } else if (tok == TOK_DOTS && gnu_ext) { + is_vaargs = 1; + next_nomacro(); + } + if (varg < TOK_IDENT) + bad_list: + tcc_error("bad macro parameter list"); + s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0); + *ps = s; + ps = &s->next; + if (tok == ')') + break; + if (tok != ',' || is_vaargs) + goto bad_list; + next_nomacro(); + } + next_nomacro_spc(); + t = MACRO_FUNC; + set_idnum('.', dotid); + } + + tokstr_buf.len = 0; + spc = 2; + parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED; + /* The body of a macro definition should be parsed such that identifiers + are parsed like the file mode determines (i.e. with '.' being an + ID character in asm mode). But '#' should be retained instead of + regarded as line comment leader, so still don't set ASM_FILE + in parse_flags. */ + while (tok != TOK_LINEFEED && tok != TOK_EOF) { + /* remove spaces around ## and after '#' */ + if (TOK_TWOSHARPS == tok) { + if (2 == spc) + goto bad_twosharp; + if (1 == spc) + --tokstr_buf.len; + spc = 3; + tok = TOK_PPJOIN; + } else if ('#' == tok) { + spc = 4; + } else if (check_space(tok, &spc)) { + goto skip; + } + tok_str_add2(&tokstr_buf, tok, &tokc); + skip: + next_nomacro_spc(); + } + + parse_flags = saved_parse_flags; + if (spc == 1) + --tokstr_buf.len; /* remove trailing space */ + tok_str_add(&tokstr_buf, 0); + if (3 == spc) +bad_twosharp: + tcc_error("'##' cannot appear at either end of macro"); + define_push(v, t, tok_str_dup(&tokstr_buf), first); +} + +static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add) +{ + const unsigned char *s; + unsigned int h; + CachedInclude *e; + int i; + + h = TOK_HASH_INIT; + s = (unsigned char *) filename; + while (*s) { +#ifdef _WIN32 + h = TOK_HASH_FUNC(h, toup(*s)); +#else + h = TOK_HASH_FUNC(h, *s); +#endif + s++; + } + h &= (CACHED_INCLUDES_HASH_SIZE - 1); + + i = s1->cached_includes_hash[h]; + for(;;) { + if (i == 0) + break; + e = s1->cached_includes[i - 1]; + if (0 == PATHCMP(e->filename, filename)) + return e; + i = e->hash_next; + } + if (!add) + return NULL; + + e = tcc_malloc(sizeof(CachedInclude) + strlen(filename)); + strcpy(e->filename, filename); + e->ifndef_macro = e->once = 0; + dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e); + /* add in hash table */ + e->hash_next = s1->cached_includes_hash[h]; + s1->cached_includes_hash[h] = s1->nb_cached_includes; +#ifdef INC_DEBUG + printf("adding cached '%s'\n", filename); +#endif + return e; +} + +static void pragma_parse(TCCState *s1) +{ + next_nomacro(); + if (tok == TOK_push_macro || tok == TOK_pop_macro) { + int t = tok, v; + Sym *s; + + if (next(), tok != '(') + goto pragma_err; + if (next(), tok != TOK_STR) + goto pragma_err; + v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok; + if (next(), tok != ')') + goto pragma_err; + if (t == TOK_push_macro) { + while (NULL == (s = define_find(v))) + define_push(v, 0, NULL, NULL); + s->type.ref = s; /* set push boundary */ + } else { + for (s = define_stack; s; s = s->prev) + if (s->v == v && s->type.ref == s) { + s->type.ref = NULL; + break; + } + } + if (s) + table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL; + else + tcc_warning("unbalanced #pragma pop_macro"); + pp_debug_tok = t, pp_debug_symv = v; + + } else if (tok == TOK_once) { + search_cached_include(s1, file->filename, 1)->once = pp_once; + + } else if (s1->output_type == TCC_OUTPUT_PREPROCESS) { + /* tcc -E: keep pragmas below unchanged */ + unget_tok(' '); + unget_tok(TOK_PRAGMA); + unget_tok('#'); + unget_tok(TOK_LINEFEED); + + } else if (tok == TOK_pack) { + /* This may be: + #pragma pack(1) // set + #pragma pack() // reset to default + #pragma pack(push,1) // push & set + #pragma pack(pop) // restore previous */ + next(); + skip('('); + if (tok == TOK_ASM_pop) { + next(); + if (s1->pack_stack_ptr <= s1->pack_stack) { + stk_error: + tcc_error("out of pack stack"); + } + s1->pack_stack_ptr--; + } else { + int val = 0; + if (tok != ')') { + if (tok == TOK_ASM_push) { + next(); + if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1) + goto stk_error; + s1->pack_stack_ptr++; + skip(','); + } + if (tok != TOK_CINT) + goto pragma_err; + val = tokc.i; + if (val < 1 || val > 16 || (val & (val - 1)) != 0) + goto pragma_err; + next(); + } + *s1->pack_stack_ptr = val; + } + if (tok != ')') + goto pragma_err; + + } else if (tok == TOK_comment) { + char *p; int t; + next(); + skip('('); + t = tok; + next(); + skip(','); + if (tok != TOK_STR) + goto pragma_err; + p = tcc_strdup((char *)tokc.str.data); + next(); + if (tok != ')') + goto pragma_err; + if (t == TOK_lib) { + dynarray_add(&s1->pragma_libs, &s1->nb_pragma_libs, p); + } else { + if (t == TOK_option) + tcc_set_options(s1, p); + tcc_free(p); + } + + } else if (s1->warn_unsupported) { + tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc)); + } + return; + +pragma_err: + tcc_error("malformed #pragma directive"); + return; +} + +/* is_bof is true if first non space token at beginning of file */ +ST_FUNC void preprocess(int is_bof) +{ + TCCState *s1 = tcc_state; + int i, c, n, saved_parse_flags; + char buf[1024], *q; + Sym *s; + + saved_parse_flags = parse_flags; + parse_flags = PARSE_FLAG_PREPROCESS + | PARSE_FLAG_TOK_NUM + | PARSE_FLAG_TOK_STR + | PARSE_FLAG_LINEFEED + | (parse_flags & PARSE_FLAG_ASM_FILE) + ; + + next_nomacro(); + redo: + switch(tok) { + case TOK_DEFINE: + pp_debug_tok = tok; + next_nomacro(); + pp_debug_symv = tok; + parse_define(); + break; + case TOK_UNDEF: + pp_debug_tok = tok; + next_nomacro(); + pp_debug_symv = tok; + s = define_find(tok); + /* undefine symbol by putting an invalid name */ + if (s) + define_undef(s); + break; + case TOK_INCLUDE: + case TOK_INCLUDE_NEXT: + ch = file->buf_ptr[0]; + /* XXX: incorrect if comments : use next_nomacro with a special mode */ + skip_spaces(); + if (ch == '<') { + c = '>'; + goto read_name; + } else if (ch == '\"') { + c = ch; + read_name: + inp(); + q = buf; + while (ch != c && ch != '\n' && ch != CH_EOF) { + if ((q - buf) < sizeof(buf) - 1) + *q++ = ch; + if (ch == '\\') { + if (handle_stray_noerror() == 0) + --q; + } else + inp(); + } + *q = '\0'; + minp(); +#if 0 + /* eat all spaces and comments after include */ + /* XXX: slightly incorrect */ + while (ch1 != '\n' && ch1 != CH_EOF) + inp(); +#endif + } else { + int len; + /* computed #include : concatenate everything up to linefeed, + the result must be one of the two accepted forms. + Don't convert pp-tokens to tokens here. */ + parse_flags = (PARSE_FLAG_PREPROCESS + | PARSE_FLAG_LINEFEED + | (parse_flags & PARSE_FLAG_ASM_FILE)); + next(); + buf[0] = '\0'; + while (tok != TOK_LINEFEED) { + pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc)); + next(); + } + len = strlen(buf); + /* check syntax and remove '<>|""' */ + if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') && + (buf[0] != '<' || buf[len-1] != '>')))) + tcc_error("'#include' expects \"FILENAME\" or "); + c = buf[len-1]; + memmove(buf, buf + 1, len - 2); + buf[len - 2] = '\0'; + } + + if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE) + tcc_error("#include recursion too deep"); + /* store current file in stack, but increment stack later below */ + *s1->include_stack_ptr = file; + i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0; + n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths; + for (; i < n; ++i) { + char buf1[sizeof file->filename]; + CachedInclude *e; + const char *path; + + if (i == 0) { + /* check absolute include path */ + if (!IS_ABSPATH(buf)) + continue; + buf1[0] = 0; + + } else if (i == 1) { + /* search in file's dir if "header.h" */ + if (c != '\"') + continue; + /* https://savannah.nongnu.org/bugs/index.php?50847 */ + path = file->true_filename; + pstrncpy(buf1, path, tcc_basename(path) - path); + + } else { + /* search in all the include paths */ + int j = i - 2, k = j - s1->nb_include_paths; + path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k]; + pstrcpy(buf1, sizeof(buf1), path); + pstrcat(buf1, sizeof(buf1), "/"); + } + + pstrcat(buf1, sizeof(buf1), buf); + e = search_cached_include(s1, buf1, 0); + if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) { + /* no need to parse the include because the 'ifndef macro' + is defined (or had #pragma once) */ +#ifdef INC_DEBUG + printf("%s: skipping cached %s\n", file->filename, buf1); +#endif + goto include_done; + } + + if (tcc_open(s1, buf1) < 0) + continue; + + file->include_next_index = i + 1; +#ifdef INC_DEBUG + printf("%s: including %s\n", file->prev->filename, file->filename); +#endif + /* update target deps */ + dynarray_add(&s1->target_deps, &s1->nb_target_deps, + tcc_strdup(buf1)); + /* push current file in stack */ + ++s1->include_stack_ptr; + /* add include file debug info */ + if (s1->do_debug) + put_stabs(file->filename, N_BINCL, 0, 0, 0); + tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL; + ch = file->buf_ptr[0]; + goto the_end; + } + tcc_error("include file '%s' not found", buf); +include_done: + break; + case TOK_IFNDEF: + c = 1; + goto do_ifdef; + case TOK_IF: + c = expr_preprocess(); + goto do_if; + case TOK_IFDEF: + c = 0; + do_ifdef: + next_nomacro(); + if (tok < TOK_IDENT) + tcc_error("invalid argument for '#if%sdef'", c ? "n" : ""); + if (is_bof) { + if (c) { +#ifdef INC_DEBUG + printf("#ifndef %s\n", get_tok_str(tok, NULL)); +#endif + file->ifndef_macro = tok; + } + } + c = (define_find(tok) != 0) ^ c; + do_if: + if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE) + tcc_error("memory full (ifdef)"); + *s1->ifdef_stack_ptr++ = c; + goto test_skip; + case TOK_ELSE: + if (s1->ifdef_stack_ptr == s1->ifdef_stack) + tcc_error("#else without matching #if"); + if (s1->ifdef_stack_ptr[-1] & 2) + tcc_error("#else after #else"); + c = (s1->ifdef_stack_ptr[-1] ^= 3); + goto test_else; + case TOK_ELIF: + if (s1->ifdef_stack_ptr == s1->ifdef_stack) + tcc_error("#elif without matching #if"); + c = s1->ifdef_stack_ptr[-1]; + if (c > 1) + tcc_error("#elif after #else"); + /* last #if/#elif expression was true: we skip */ + if (c == 1) { + c = 0; + } else { + c = expr_preprocess(); + s1->ifdef_stack_ptr[-1] = c; + } + test_else: + if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1) + file->ifndef_macro = 0; + test_skip: + if (!(c & 1)) { + preprocess_skip(); + is_bof = 0; + goto redo; + } + break; + case TOK_ENDIF: + if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr) + tcc_error("#endif without matching #if"); + s1->ifdef_stack_ptr--; + /* '#ifndef macro' was at the start of file. Now we check if + an '#endif' is exactly at the end of file */ + if (file->ifndef_macro && + s1->ifdef_stack_ptr == file->ifdef_stack_ptr) { + file->ifndef_macro_saved = file->ifndef_macro; + /* need to set to zero to avoid false matches if another + #ifndef at middle of file */ + file->ifndef_macro = 0; + while (tok != TOK_LINEFEED) + next_nomacro(); + tok_flags |= TOK_FLAG_ENDIF; + goto the_end; + } + break; + case TOK_PPNUM: + n = strtoul((char*)tokc.str.data, &q, 10); + goto _line_num; + case TOK_LINE: + next(); + if (tok != TOK_CINT) + _line_err: + tcc_error("wrong #line format"); + n = tokc.i; + _line_num: + next(); + if (tok != TOK_LINEFEED) { + if (tok == TOK_STR) { + if (file->true_filename == file->filename) + file->true_filename = tcc_strdup(file->filename); + pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data); + } else if (parse_flags & PARSE_FLAG_ASM_FILE) + break; + else + goto _line_err; + --n; + } + if (file->fd > 0) + total_lines += file->line_num - n; + file->line_num = n; + if (s1->do_debug) + put_stabs(file->filename, N_BINCL, 0, 0, 0); + break; + case TOK_ERROR: + case TOK_WARNING: + c = tok; + ch = file->buf_ptr[0]; + skip_spaces(); + q = buf; + while (ch != '\n' && ch != CH_EOF) { + if ((q - buf) < sizeof(buf) - 1) + *q++ = ch; + if (ch == '\\') { + if (handle_stray_noerror() == 0) + --q; + } else + inp(); + } + *q = '\0'; + if (c == TOK_ERROR) + tcc_error("#error %s", buf); + else + tcc_warning("#warning %s", buf); + break; + case TOK_PRAGMA: + pragma_parse(s1); + break; + case TOK_LINEFEED: + goto the_end; + default: + /* ignore gas line comment in an 'S' file. */ + if (saved_parse_flags & PARSE_FLAG_ASM_FILE) + goto ignore; + if (tok == '!' && is_bof) + /* '!' is ignored at beginning to allow C scripts. */ + goto ignore; + tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc)); + ignore: + file->buf_ptr = parse_line_comment(file->buf_ptr - 1); + goto the_end; + } + /* ignore other preprocess commands or #! for C scripts */ + while (tok != TOK_LINEFEED) + next_nomacro(); + the_end: + parse_flags = saved_parse_flags; +} + +/* evaluate escape codes in a string. */ +static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long) +{ + int c, n; + const uint8_t *p; + + p = buf; + for(;;) { + c = *p; + if (c == '\0') + break; + if (c == '\\') { + p++; + /* escape */ + c = *p; + switch(c) { + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + /* at most three octal digits */ + n = c - '0'; + p++; + c = *p; + if (isoct(c)) { + n = n * 8 + c - '0'; + p++; + c = *p; + if (isoct(c)) { + n = n * 8 + c - '0'; + p++; + } + } + c = n; + goto add_char_nonext; + case 'x': + case 'u': + case 'U': + p++; + n = 0; + for(;;) { + c = *p; + if (c >= 'a' && c <= 'f') + c = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + c = c - 'A' + 10; + else if (isnum(c)) + c = c - '0'; + else + break; + n = n * 16 + c; + p++; + } + c = n; + goto add_char_nonext; + 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': + if (!gnu_ext) + goto invalid_escape; + c = 27; + break; + case '\'': + case '\"': + case '\\': + case '?': + break; + default: + invalid_escape: + if (c >= '!' && c <= '~') + tcc_warning("unknown escape sequence: \'\\%c\'", c); + else + tcc_warning("unknown escape sequence: \'\\x%x\'", c); + break; + } + } else if (is_long && c >= 0x80) { + /* assume we are processing UTF-8 sequence */ + /* reference: The Unicode Standard, Version 10.0, ch3.9 */ + + int cont; /* count of continuation bytes */ + int skip; /* how many bytes should skip when error occurred */ + int i; + + /* decode leading byte */ + if (c < 0xC2) { + skip = 1; goto invalid_utf8_sequence; + } else if (c <= 0xDF) { + cont = 1; n = c & 0x1f; + } else if (c <= 0xEF) { + cont = 2; n = c & 0xf; + } else if (c <= 0xF4) { + cont = 3; n = c & 0x7; + } else { + skip = 1; goto invalid_utf8_sequence; + } + + /* decode continuation bytes */ + for (i = 1; i <= cont; i++) { + int l = 0x80, h = 0xBF; + + /* adjust limit for second byte */ + if (i == 1) { + switch (c) { + case 0xE0: l = 0xA0; break; + case 0xED: h = 0x9F; break; + case 0xF0: l = 0x90; break; + case 0xF4: h = 0x8F; break; + } + } + + if (p[i] < l || p[i] > h) { + skip = i; goto invalid_utf8_sequence; + } + + n = (n << 6) | (p[i] & 0x3f); + } + + /* advance pointer */ + p += 1 + cont; + c = n; + goto add_char_nonext; + + /* error handling */ + invalid_utf8_sequence: + tcc_warning("ill-formed UTF-8 subsequence starting with: \'\\x%x\'", c); + c = 0xFFFD; + p += skip; + goto add_char_nonext; + + } + p++; + add_char_nonext: + if (!is_long) + cstr_ccat(outstr, c); + else { +#ifdef TCC_TARGET_PE + /* store as UTF-16 */ + if (c < 0x10000) { + cstr_wccat(outstr, c); + } else { + c -= 0x10000; + cstr_wccat(outstr, (c >> 10) + 0xD800); + cstr_wccat(outstr, (c & 0x3FF) + 0xDC00); + } +#else + cstr_wccat(outstr, c); +#endif + } + } + /* add a trailing '\0' */ + if (!is_long) + cstr_ccat(outstr, '\0'); + else + cstr_wccat(outstr, '\0'); +} + +static void parse_string(const char *s, int len) +{ + uint8_t buf[1000], *p = buf; + int is_long, sep; + + if ((is_long = *s == 'L')) + ++s, --len; + sep = *s++; + len -= 2; + if (len >= sizeof buf) + p = tcc_malloc(len + 1); + memcpy(p, s, len); + p[len] = 0; + + cstr_reset(&tokcstr); + parse_escape_string(&tokcstr, p, is_long); + if (p != buf) + tcc_free(p); + + if (sep == '\'') { + int char_size, i, n, c; + /* XXX: make it portable */ + if (!is_long) + tok = TOK_CCHAR, char_size = 1; + else + tok = TOK_LCHAR, char_size = sizeof(nwchar_t); + n = tokcstr.size / char_size - 1; + if (n < 1) + tcc_error("empty character constant"); + if (n > 1) + tcc_warning("multi-character character constant"); + for (c = i = 0; i < n; ++i) { + if (is_long) + c = ((nwchar_t *)tokcstr.data)[i]; + else + c = (c << 8) | ((char *)tokcstr.data)[i]; + } + tokc.i = c; + } else { + tokc.str.size = tokcstr.size; + tokc.str.data = tokcstr.data; + if (!is_long) + tok = TOK_STR; + else + tok = TOK_LSTR; + } +} + +/* we use 64 bit numbers */ +#define BN_SIZE 2 + +/* bn = (bn << shift) | or_val */ +static void bn_lshift(unsigned int *bn, int shift, int or_val) +{ + int i; + unsigned int v; + for(i=0;i> (32 - shift); + } +} + +static void bn_zero(unsigned int *bn) +{ + int i; + for(i=0;i= 'a' && ch <= 'f') + t = ch - 'a' + 10; + else if (ch >= 'A' && ch <= 'F') + t = ch - 'A' + 10; + else if (isnum(ch)) + t = ch - '0'; + else + break; + if (t >= b) + break; + if (q >= token_buf + STRING_MAX_SIZE) { + num_too_long: + tcc_error("number too long"); + } + *q++ = ch; + ch = *p++; + } + if (ch == '.' || + ((ch == 'e' || ch == 'E') && b == 10) || + ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) { + if (b != 10) { + /* NOTE: strtox should support that for hexa numbers, but + non ISOC99 libcs do not support it, so we prefer to do + it by hand */ + /* hexadecimal or binary floats */ + /* XXX: handle overflows */ + *q = '\0'; + if (b == 16) + shift = 4; + else + shift = 1; + bn_zero(bn); + q = token_buf; + while (1) { + t = *q++; + if (t == '\0') { + break; + } else if (t >= 'a') { + t = t - 'a' + 10; + } else if (t >= 'A') { + t = t - 'A' + 10; + } else { + t = t - '0'; + } + bn_lshift(bn, shift, t); + } + frac_bits = 0; + if (ch == '.') { + ch = *p++; + while (1) { + t = ch; + if (t >= 'a' && t <= 'f') { + t = t - 'a' + 10; + } else if (t >= 'A' && t <= 'F') { + t = t - 'A' + 10; + } else if (t >= '0' && t <= '9') { + t = t - '0'; + } else { + break; + } + if (t >= b) + tcc_error("invalid digit"); + bn_lshift(bn, shift, t); + frac_bits += shift; + ch = *p++; + } + } + if (ch != 'p' && ch != 'P') + expect("exponent"); + ch = *p++; + s = 1; + exp_val = 0; + if (ch == '+') { + ch = *p++; + } else if (ch == '-') { + s = -1; + ch = *p++; + } + if (ch < '0' || ch > '9') + expect("exponent digits"); + while (ch >= '0' && ch <= '9') { + exp_val = exp_val * 10 + ch - '0'; + ch = *p++; + } + exp_val = exp_val * s; + + /* now we can generate the number */ + /* XXX: should patch directly float number */ + d = (double)bn[1] * 4294967296.0 + (double)bn[0]; + d = ldexp(d, exp_val - frac_bits); + t = toup(ch); + if (t == 'F') { + ch = *p++; + tok = TOK_CFLOAT; + /* float : should handle overflow */ + tokc.f = (float)d; + } else if (t == 'L') { + ch = *p++; +#ifdef TCC_TARGET_PE + tok = TOK_CDOUBLE; + tokc.d = d; +#else + tok = TOK_CLDOUBLE; + /* XXX: not large enough */ + tokc.ld = (long double)d; +#endif + } else { + tok = TOK_CDOUBLE; + tokc.d = d; + } + } else { + /* decimal floats */ + if (ch == '.') { + if (q >= token_buf + STRING_MAX_SIZE) + goto num_too_long; + *q++ = ch; + ch = *p++; + float_frac_parse: + while (ch >= '0' && ch <= '9') { + if (q >= token_buf + STRING_MAX_SIZE) + goto num_too_long; + *q++ = ch; + ch = *p++; + } + } + if (ch == 'e' || ch == 'E') { + if (q >= token_buf + STRING_MAX_SIZE) + goto num_too_long; + *q++ = ch; + ch = *p++; + if (ch == '-' || ch == '+') { + if (q >= token_buf + STRING_MAX_SIZE) + goto num_too_long; + *q++ = ch; + ch = *p++; + } + if (ch < '0' || ch > '9') + expect("exponent digits"); + while (ch >= '0' && ch <= '9') { + if (q >= token_buf + STRING_MAX_SIZE) + goto num_too_long; + *q++ = ch; + ch = *p++; + } + } + *q = '\0'; + t = toup(ch); + errno = 0; + if (t == 'F') { + ch = *p++; + tok = TOK_CFLOAT; + tokc.f = strtof(token_buf, NULL); + } else if (t == 'L') { + ch = *p++; +#ifdef TCC_TARGET_PE + tok = TOK_CDOUBLE; + tokc.d = strtod(token_buf, NULL); +#else + tok = TOK_CLDOUBLE; + tokc.ld = strtold(token_buf, NULL); +#endif + } else { + tok = TOK_CDOUBLE; + tokc.d = strtod(token_buf, NULL); + } + } + } else { + unsigned long long n, n1; + int lcount, ucount, ov = 0; + const char *p1; + + /* integer number */ + *q = '\0'; + q = token_buf; + if (b == 10 && *q == '0') { + b = 8; + q++; + } + n = 0; + while(1) { + t = *q++; + /* no need for checks except for base 10 / 8 errors */ + if (t == '\0') + break; + else if (t >= 'a') + t = t - 'a' + 10; + else if (t >= 'A') + t = t - 'A' + 10; + else + t = t - '0'; + if (t >= b) + tcc_error("invalid digit"); + n1 = n; + n = n * b + t; + /* detect overflow */ + if (n1 >= 0x1000000000000000ULL && n / b != n1) + ov = 1; + } + + /* Determine the characteristics (unsigned and/or 64bit) the type of + the constant must have according to the constant suffix(es) */ + lcount = ucount = 0; + p1 = p; + for(;;) { + t = toup(ch); + if (t == 'L') { + if (lcount >= 2) + tcc_error("three 'l's in integer constant"); + if (lcount && *(p - 1) != ch) + tcc_error("incorrect integer suffix: %s", p1); + lcount++; + ch = *p++; + } else if (t == 'U') { + if (ucount >= 1) + tcc_error("two 'u's in integer constant"); + ucount++; + ch = *p++; + } else { + break; + } + } + + /* Determine if it needs 64 bits and/or unsigned in order to fit */ + if (ucount == 0 && b == 10) { + if (lcount <= (LONG_SIZE == 4)) { + if (n >= 0x80000000U) + lcount = (LONG_SIZE == 4) + 1; + } + if (n >= 0x8000000000000000ULL) + ov = 1, ucount = 1; + } else { + if (lcount <= (LONG_SIZE == 4)) { + if (n >= 0x100000000ULL) + lcount = (LONG_SIZE == 4) + 1; + else if (n >= 0x80000000U) + ucount = 1; + } + if (n >= 0x8000000000000000ULL) + ucount = 1; + } + + if (ov) + tcc_warning("integer constant overflow"); + + tok = TOK_CINT; + if (lcount) { + tok = TOK_CLONG; + if (lcount == 2) + tok = TOK_CLLONG; + } + if (ucount) + ++tok; /* TOK_CU... */ + tokc.i = n; + } + if (ch) + tcc_error("invalid number\n"); +} + + +#define PARSE2(c1, tok1, c2, tok2) \ + case c1: \ + PEEKC(c, p); \ + if (c == c2) { \ + p++; \ + tok = tok2; \ + } else { \ + tok = tok1; \ + } \ + break; + +/* return next token without macro substitution */ +static inline void next_nomacro1(void) +{ + int t, c, is_long, len; + TokenSym *ts; + uint8_t *p, *p1; + unsigned int h; + + p = file->buf_ptr; + redo_no_start: + c = *p; + switch(c) { + case ' ': + case '\t': + tok = c; + p++; + if (parse_flags & PARSE_FLAG_SPACES) + goto keep_tok_flags; + while (isidnum_table[*p - CH_EOF] & IS_SPC) + ++p; + goto redo_no_start; + case '\f': + case '\v': + case '\r': + p++; + goto redo_no_start; + case '\\': + /* first look if it is in fact an end of buffer */ + c = handle_stray1(p); + p = file->buf_ptr; + if (c == '\\') + goto parse_simple; + if (c != CH_EOF) + goto redo_no_start; + { + TCCState *s1 = tcc_state; + if ((parse_flags & PARSE_FLAG_LINEFEED) + && !(tok_flags & TOK_FLAG_EOF)) { + tok_flags |= TOK_FLAG_EOF; + tok = TOK_LINEFEED; + goto keep_tok_flags; + } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) { + tok = TOK_EOF; + } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) { + tcc_error("missing #endif"); + } else if (s1->include_stack_ptr == s1->include_stack) { + /* no include left : end of file. */ + tok = TOK_EOF; + } else { + tok_flags &= ~TOK_FLAG_EOF; + /* pop include file */ + + /* test if previous '#endif' was after a #ifdef at + start of file */ + if (tok_flags & TOK_FLAG_ENDIF) { +#ifdef INC_DEBUG + printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL)); +#endif + search_cached_include(s1, file->filename, 1) + ->ifndef_macro = file->ifndef_macro_saved; + tok_flags &= ~TOK_FLAG_ENDIF; + } + + /* add end of include file debug info */ + if (tcc_state->do_debug) { + put_stabd(N_EINCL, 0, 0); + } + /* pop include stack */ + tcc_close(); + s1->include_stack_ptr--; + p = file->buf_ptr; + if (p == file->buffer) + tok_flags = TOK_FLAG_BOF|TOK_FLAG_BOL; + goto redo_no_start; + } + } + break; + + case '\n': + file->line_num++; + tok_flags |= TOK_FLAG_BOL; + p++; +maybe_newline: + if (0 == (parse_flags & PARSE_FLAG_LINEFEED)) + goto redo_no_start; + tok = TOK_LINEFEED; + goto keep_tok_flags; + + case '#': + /* XXX: simplify */ + PEEKC(c, p); + if ((tok_flags & TOK_FLAG_BOL) && + (parse_flags & PARSE_FLAG_PREPROCESS)) { + file->buf_ptr = p; + preprocess(tok_flags & TOK_FLAG_BOF); + p = file->buf_ptr; + goto maybe_newline; + } else { + if (c == '#') { + p++; + tok = TOK_TWOSHARPS; + } else { + if (parse_flags & PARSE_FLAG_ASM_FILE) { + p = parse_line_comment(p - 1); + goto redo_no_start; + } else { + tok = '#'; + } + } + } + break; + + /* dollar is allowed to start identifiers when not parsing asm */ + case '$': + if (!(isidnum_table[c - CH_EOF] & IS_ID) + || (parse_flags & PARSE_FLAG_ASM_FILE)) + goto parse_simple; + + case 'a': case 'b': case 'c': case 'd': + case 'e': case 'f': case 'g': case 'h': + case 'i': case 'j': case 'k': case 'l': + case 'm': case 'n': case 'o': case 'p': + case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': + case 'y': case 'z': + case 'A': case 'B': case 'C': case 'D': + case 'E': case 'F': case 'G': case 'H': + case 'I': case 'J': case 'K': + case 'M': case 'N': case 'O': case 'P': + case 'Q': case 'R': case 'S': case 'T': + case 'U': case 'V': case 'W': case 'X': + case 'Y': case 'Z': + case '_': + parse_ident_fast: + p1 = p; + h = TOK_HASH_INIT; + h = TOK_HASH_FUNC(h, c); + while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) + h = TOK_HASH_FUNC(h, c); + len = p - p1; + if (c != '\\') { + TokenSym **pts; + + /* fast case : no stray found, so we have the full token + and we have already hashed it */ + h &= (TOK_HASH_SIZE - 1); + pts = &hash_ident[h]; + for(;;) { + ts = *pts; + if (!ts) + break; + if (ts->len == len && !memcmp(ts->str, p1, len)) + goto token_found; + pts = &(ts->hash_next); + } + ts = tok_alloc_new(pts, (char *) p1, len); + token_found: ; + } else { + /* slower case */ + cstr_reset(&tokcstr); + cstr_cat(&tokcstr, (char *) p1, len); + p--; + PEEKC(c, p); + parse_ident_slow: + while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) + { + cstr_ccat(&tokcstr, c); + PEEKC(c, p); + } + ts = tok_alloc(tokcstr.data, tokcstr.size); + } + tok = ts->tok; + break; + case 'L': + t = p[1]; + if (t != '\\' && t != '\'' && t != '\"') { + /* fast case */ + goto parse_ident_fast; + } else { + PEEKC(c, p); + if (c == '\'' || c == '\"') { + is_long = 1; + goto str_const; + } else { + cstr_reset(&tokcstr); + cstr_ccat(&tokcstr, 'L'); + goto parse_ident_slow; + } + } + break; + + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + case '8': case '9': + t = c; + PEEKC(c, p); + /* after the first digit, accept digits, alpha, '.' or sign if + prefixed by 'eEpP' */ + parse_num: + cstr_reset(&tokcstr); + for(;;) { + cstr_ccat(&tokcstr, t); + if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) + || c == '.' + || ((c == '+' || c == '-') + && (((t == 'e' || t == 'E') + && !(parse_flags & PARSE_FLAG_ASM_FILE + /* 0xe+1 is 3 tokens in asm */ + && ((char*)tokcstr.data)[0] == '0' + && toup(((char*)tokcstr.data)[1]) == 'X')) + || t == 'p' || t == 'P')))) + break; + t = c; + PEEKC(c, p); + } + /* We add a trailing '\0' to ease parsing */ + cstr_ccat(&tokcstr, '\0'); + tokc.str.size = tokcstr.size; + tokc.str.data = tokcstr.data; + tok = TOK_PPNUM; + break; + + case '.': + /* special dot handling because it can also start a number */ + PEEKC(c, p); + if (isnum(c)) { + t = '.'; + goto parse_num; + } else if ((isidnum_table['.' - CH_EOF] & IS_ID) + && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) { + *--p = c = '.'; + goto parse_ident_fast; + } else if (c == '.') { + PEEKC(c, p); + if (c == '.') { + p++; + tok = TOK_DOTS; + } else { + *--p = '.'; /* may underflow into file->unget[] */ + tok = '.'; + } + } else { + tok = '.'; + } + break; + case '\'': + case '\"': + is_long = 0; + str_const: + cstr_reset(&tokcstr); + if (is_long) + cstr_ccat(&tokcstr, 'L'); + cstr_ccat(&tokcstr, c); + p = parse_pp_string(p, c, &tokcstr); + cstr_ccat(&tokcstr, c); + cstr_ccat(&tokcstr, '\0'); + tokc.str.size = tokcstr.size; + tokc.str.data = tokcstr.data; + tok = TOK_PPSTR; + break; + + case '<': + PEEKC(c, p); + if (c == '=') { + p++; + tok = TOK_LE; + } else if (c == '<') { + PEEKC(c, p); + if (c == '=') { + p++; + tok = TOK_A_SHL; + } else { + tok = TOK_SHL; + } + } else { + tok = TOK_LT; + } + break; + case '>': + PEEKC(c, p); + if (c == '=') { + p++; + tok = TOK_GE; + } else if (c == '>') { + PEEKC(c, p); + if (c == '=') { + p++; + tok = TOK_A_SAR; + } else { + tok = TOK_SAR; + } + } else { + tok = TOK_GT; + } + break; + + case '&': + PEEKC(c, p); + if (c == '&') { + p++; + tok = TOK_LAND; + } else if (c == '=') { + p++; + tok = TOK_A_AND; + } else { + tok = '&'; + } + break; + + case '|': + PEEKC(c, p); + if (c == '|') { + p++; + tok = TOK_LOR; + } else if (c == '=') { + p++; + tok = TOK_A_OR; + } else { + tok = '|'; + } + break; + + case '+': + PEEKC(c, p); + if (c == '+') { + p++; + tok = TOK_INC; + } else if (c == '=') { + p++; + tok = TOK_A_ADD; + } else { + tok = '+'; + } + break; + + case '-': + PEEKC(c, p); + if (c == '-') { + p++; + tok = TOK_DEC; + } else if (c == '=') { + p++; + tok = TOK_A_SUB; + } else if (c == '>') { + p++; + tok = TOK_ARROW; + } else { + tok = '-'; + } + break; + + PARSE2('!', '!', '=', TOK_NE) + PARSE2('=', '=', '=', TOK_EQ) + PARSE2('*', '*', '=', TOK_A_MUL) + PARSE2('%', '%', '=', TOK_A_MOD) + PARSE2('^', '^', '=', TOK_A_XOR) + + /* comments or operator */ + case '/': + PEEKC(c, p); + if (c == '*') { + p = parse_comment(p); + /* comments replaced by a blank */ + tok = ' '; + goto keep_tok_flags; + } else if (c == '/') { + p = parse_line_comment(p); + tok = ' '; + goto keep_tok_flags; + } else if (c == '=') { + p++; + tok = TOK_A_DIV; + } else { + tok = '/'; + } + break; + + /* simple tokens */ + case '(': + case ')': + case '[': + case ']': + case '{': + case '}': + case ',': + case ';': + case ':': + case '?': + case '~': + case '@': /* only used in assembler */ + parse_simple: + tok = c; + p++; + break; + default: + if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */ + goto parse_ident_fast; + if (parse_flags & PARSE_FLAG_ASM_FILE) + goto parse_simple; + tcc_error("unrecognized character \\x%02x", c); + break; + } + tok_flags = 0; +keep_tok_flags: + file->buf_ptr = p; +#if defined(PARSE_DEBUG) + printf("token = %d %s\n", tok, get_tok_str(tok, &tokc)); +#endif +} + +/* return next token without macro substitution. Can read input from + macro_ptr buffer */ +static void next_nomacro_spc(void) +{ + if (macro_ptr) { + redo: + tok = *macro_ptr; + if (tok) { + TOK_GET(&tok, ¯o_ptr, &tokc); + if (tok == TOK_LINENUM) { + file->line_num = tokc.i; + goto redo; + } + } + } else { + next_nomacro1(); + } + //printf("token = %s\n", get_tok_str(tok, &tokc)); +} + +ST_FUNC void next_nomacro(void) +{ + do { + next_nomacro_spc(); + } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC)); +} + + +static void macro_subst( + TokenString *tok_str, + Sym **nested_list, + const int *macro_str + ); + +/* substitute arguments in replacement lists in macro_str by the values in + args (field d) and return allocated string */ +static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) +{ + int t, t0, t1, spc; + const int *st; + Sym *s; + CValue cval; + TokenString str; + CString cstr; + + tok_str_new(&str); + t0 = t1 = 0; + while(1) { + TOK_GET(&t, ¯o_str, &cval); + if (!t) + break; + if (t == '#') { + /* stringize */ + TOK_GET(&t, ¯o_str, &cval); + if (!t) + goto bad_stringy; + s = sym_find2(args, t); + if (s) { + cstr_new(&cstr); + cstr_ccat(&cstr, '\"'); + st = s->d; + spc = 0; + while (*st >= 0) { + TOK_GET(&t, &st, &cval); + if (t != TOK_PLCHLDR + && t != TOK_NOSUBST + && 0 == check_space(t, &spc)) { + const char *s = get_tok_str(t, &cval); + while (*s) { + if (t == TOK_PPSTR && *s != '\'') + add_char(&cstr, *s); + else + cstr_ccat(&cstr, *s); + ++s; + } + } + } + cstr.size -= spc; + cstr_ccat(&cstr, '\"'); + cstr_ccat(&cstr, '\0'); +#ifdef PP_DEBUG + printf("\nstringize: <%s>\n", (char *)cstr.data); +#endif + /* add string */ + cval.str.size = cstr.size; + cval.str.data = cstr.data; + tok_str_add2(&str, TOK_PPSTR, &cval); + cstr_free(&cstr); + } else { + bad_stringy: + expect("macro parameter after '#'"); + } + } else if (t >= TOK_IDENT) { + s = sym_find2(args, t); + if (s) { + int l0 = str.len; + st = s->d; + /* if '##' is present before or after, no arg substitution */ + if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) { + /* special case for var arg macros : ## eats the ',' + if empty VA_ARGS variable. */ + if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) { + if (*st <= 0) { + /* suppress ',' '##' */ + str.len -= 2; + } else { + /* suppress '##' and add variable */ + str.len--; + goto add_var; + } + } + } else { + add_var: + if (!s->next) { + /* Expand arguments tokens and store them. In most + cases we could also re-expand each argument if + used multiple times, but not if the argument + contains the __COUNTER__ macro. */ + TokenString str2; + sym_push2(&s->next, s->v, s->type.t, 0); + tok_str_new(&str2); + macro_subst(&str2, nested_list, st); + tok_str_add(&str2, 0); + s->next->d = str2.str; + } + st = s->next->d; + } + for(;;) { + int t2; + TOK_GET(&t2, &st, &cval); + if (t2 <= 0) + break; + tok_str_add2(&str, t2, &cval); + } + if (str.len == l0) /* expanded to empty string */ + tok_str_add(&str, TOK_PLCHLDR); + } else { + tok_str_add(&str, t); + } + } else { + tok_str_add2(&str, t, &cval); + } + t0 = t1, t1 = t; + } + tok_str_add(&str, 0); + return str.str; +} + +static char const ab_month_name[12][4] = +{ + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +}; + +static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2) +{ + CString cstr; + int n, ret = 1; + + cstr_new(&cstr); + if (t1 != TOK_PLCHLDR) + cstr_cat(&cstr, get_tok_str(t1, v1), -1); + n = cstr.size; + if (t2 != TOK_PLCHLDR) + cstr_cat(&cstr, get_tok_str(t2, v2), -1); + cstr_ccat(&cstr, '\0'); + + tcc_open_bf(tcc_state, ":paste:", cstr.size); + memcpy(file->buffer, cstr.data, cstr.size); + tok_flags = 0; + for (;;) { + next_nomacro1(); + if (0 == *file->buf_ptr) + break; + if (is_space(tok)) + continue; + tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid" + " preprocessing token", n, cstr.data, (char*)cstr.data + n); + ret = 0; + break; + } + tcc_close(); + //printf("paste <%s>\n", (char*)cstr.data); + cstr_free(&cstr); + return ret; +} + +/* handle the '##' operator. Return NULL if no '##' seen. Otherwise + return the resulting string (which must be freed). */ +static inline int *macro_twosharps(const int *ptr0) +{ + int t; + CValue cval; + TokenString macro_str1; + int start_of_nosubsts = -1; + const int *ptr; + + /* we search the first '##' */ + for (ptr = ptr0;;) { + TOK_GET(&t, &ptr, &cval); + if (t == TOK_PPJOIN) + break; + if (t == 0) + return NULL; + } + + tok_str_new(¯o_str1); + + //tok_print(" $$$", ptr0); + for (ptr = ptr0;;) { + TOK_GET(&t, &ptr, &cval); + if (t == 0) + break; + if (t == TOK_PPJOIN) + continue; + while (*ptr == TOK_PPJOIN) { + int t1; CValue cv1; + /* given 'a##b', remove nosubsts preceding 'a' */ + if (start_of_nosubsts >= 0) + macro_str1.len = start_of_nosubsts; + /* given 'a##b', remove nosubsts preceding 'b' */ + while ((t1 = *++ptr) == TOK_NOSUBST) + ; + if (t1 && t1 != TOK_PPJOIN) { + TOK_GET(&t1, &ptr, &cv1); + if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) { + if (paste_tokens(t, &cval, t1, &cv1)) { + t = tok, cval = tokc; + } else { + tok_str_add2(¯o_str1, t, &cval); + t = t1, cval = cv1; + } + } + } + } + if (t == TOK_NOSUBST) { + if (start_of_nosubsts < 0) + start_of_nosubsts = macro_str1.len; + } else { + start_of_nosubsts = -1; + } + tok_str_add2(¯o_str1, t, &cval); + } + tok_str_add(¯o_str1, 0); + //tok_print(" ###", macro_str1.str); + return macro_str1.str; +} + +/* peek or read [ws_str == NULL] next token from function macro call, + walking up macro levels up to the file if necessary */ +static int next_argstream(Sym **nested_list, TokenString *ws_str) +{ + int t; + const int *p; + Sym *sa; + + for (;;) { + if (macro_ptr) { + p = macro_ptr, t = *p; + if (ws_str) { + while (is_space(t) || TOK_LINEFEED == t || TOK_PLCHLDR == t) + tok_str_add(ws_str, t), t = *++p; + } + if (t == 0) { + end_macro(); + /* also, end of scope for nested defined symbol */ + sa = *nested_list; + while (sa && sa->v == 0) + sa = sa->prev; + if (sa) + sa->v = 0; + continue; + } + } else { + ch = handle_eob(); + if (ws_str) { + while (is_space(ch) || ch == '\n' || ch == '/') { + if (ch == '/') { + int c; + uint8_t *p = file->buf_ptr; + PEEKC(c, p); + if (c == '*') { + p = parse_comment(p); + file->buf_ptr = p - 1; + } else if (c == '/') { + p = parse_line_comment(p); + file->buf_ptr = p - 1; + } else + break; + ch = ' '; + } + if (ch == '\n') + file->line_num++; + if (!(ch == '\f' || ch == '\v' || ch == '\r')) + tok_str_add(ws_str, ch); + cinp(); + } + } + t = ch; + } + + if (ws_str) + return t; + next_nomacro_spc(); + return tok; + } +} + +/* do macro substitution of current token with macro 's' and add + result to (tok_str,tok_len). 'nested_list' is the list of all + macros we got inside to avoid recursing. Return non zero if no + substitution needs to be done */ +static int macro_subst_tok( + TokenString *tok_str, + Sym **nested_list, + Sym *s) +{ + Sym *args, *sa, *sa1; + int parlevel, t, t1, spc; + TokenString str; + char *cstrval; + CValue cval; + CString cstr; + char buf[32]; + + /* if symbol is a macro, prepare substitution */ + /* special macros */ + if (tok == TOK___LINE__ || tok == TOK___COUNTER__) { + t = tok == TOK___LINE__ ? file->line_num : pp_counter++; + snprintf(buf, sizeof(buf), "%d", t); + cstrval = buf; + t1 = TOK_PPNUM; + goto add_cstr1; + } else if (tok == TOK___FILE__) { + cstrval = file->filename; + goto add_cstr; + } else if (tok == TOK___DATE__ || tok == TOK___TIME__) { + time_t ti; + struct tm *tm; + + time(&ti); + tm = localtime(&ti); + if (tok == TOK___DATE__) { + snprintf(buf, sizeof(buf), "%s %2d %d", + ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900); + } else { + snprintf(buf, sizeof(buf), "%02d:%02d:%02d", + tm->tm_hour, tm->tm_min, tm->tm_sec); + } + cstrval = buf; + add_cstr: + t1 = TOK_STR; + add_cstr1: + cstr_new(&cstr); + cstr_cat(&cstr, cstrval, 0); + cval.str.size = cstr.size; + cval.str.data = cstr.data; + tok_str_add2(tok_str, t1, &cval); + cstr_free(&cstr); + } else if (s->d) { + int saved_parse_flags = parse_flags; + int *joined_str = NULL; + int *mstr = s->d; + + if (s->type.t == MACRO_FUNC) { + /* whitespace between macro name and argument list */ + TokenString ws_str; + tok_str_new(&ws_str); + + spc = 0; + parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED + | PARSE_FLAG_ACCEPT_STRAYS; + + /* get next token from argument stream */ + t = next_argstream(nested_list, &ws_str); + if (t != '(') { + /* not a macro substitution after all, restore the + * macro token plus all whitespace we've read. + * whitespace is intentionally not merged to preserve + * newlines. */ + parse_flags = saved_parse_flags; + tok_str_add(tok_str, tok); + if (parse_flags & PARSE_FLAG_SPACES) { + int i; + for (i = 0; i < ws_str.len; i++) + tok_str_add(tok_str, ws_str.str[i]); + } + tok_str_free_str(ws_str.str); + return 0; + } else { + tok_str_free_str(ws_str.str); + } + do { + next_nomacro(); /* eat '(' */ + } while (tok == TOK_PLCHLDR); + + /* argument macro */ + args = NULL; + sa = s->next; + /* NOTE: empty args are allowed, except if no args */ + for(;;) { + do { + next_argstream(nested_list, NULL); + } while (is_space(tok) || TOK_LINEFEED == tok); + empty_arg: + /* handle '()' case */ + if (!args && !sa && tok == ')') + break; + if (!sa) + tcc_error("macro '%s' used with too many args", + get_tok_str(s->v, 0)); + tok_str_new(&str); + parlevel = spc = 0; + /* NOTE: non zero sa->t indicates VA_ARGS */ + while ((parlevel > 0 || + (tok != ')' && + (tok != ',' || sa->type.t)))) { + if (tok == TOK_EOF || tok == 0) + break; + if (tok == '(') + parlevel++; + else if (tok == ')') + parlevel--; + if (tok == TOK_LINEFEED) + tok = ' '; + if (!check_space(tok, &spc)) + tok_str_add2(&str, tok, &tokc); + next_argstream(nested_list, NULL); + } + if (parlevel) + expect(")"); + str.len -= spc; + tok_str_add(&str, -1); + tok_str_add(&str, 0); + sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0); + sa1->d = str.str; + sa = sa->next; + if (tok == ')') { + /* special case for gcc var args: add an empty + var arg argument if it is omitted */ + if (sa && sa->type.t && gnu_ext) + goto empty_arg; + break; + } + if (tok != ',') + expect(","); + } + if (sa) { + tcc_error("macro '%s' used with too few args", + get_tok_str(s->v, 0)); + } + + parse_flags = saved_parse_flags; + + /* now subst each arg */ + mstr = macro_arg_subst(nested_list, mstr, args); + /* free memory */ + sa = args; + while (sa) { + sa1 = sa->prev; + tok_str_free_str(sa->d); + if (sa->next) { + tok_str_free_str(sa->next->d); + sym_free(sa->next); + } + sym_free(sa); + sa = sa1; + } + } + + sym_push2(nested_list, s->v, 0, 0); + parse_flags = saved_parse_flags; + joined_str = macro_twosharps(mstr); + macro_subst(tok_str, nested_list, joined_str ? joined_str : mstr); + + /* pop nested defined symbol */ + sa1 = *nested_list; + *nested_list = sa1->prev; + sym_free(sa1); + if (joined_str) + tok_str_free_str(joined_str); + if (mstr != s->d) + tok_str_free_str(mstr); + } + return 0; +} + +/* do macro substitution of macro_str and add result to + (tok_str,tok_len). 'nested_list' is the list of all macros we got + inside to avoid recursing. */ +static void macro_subst( + TokenString *tok_str, + Sym **nested_list, + const int *macro_str + ) +{ + Sym *s; + int t, spc, nosubst; + CValue cval; + + spc = nosubst = 0; + + while (1) { + TOK_GET(&t, ¯o_str, &cval); + if (t <= 0) + break; + + if (t >= TOK_IDENT && 0 == nosubst) { + s = define_find(t); + if (s == NULL) + goto no_subst; + + /* if nested substitution, do nothing */ + if (sym_find2(*nested_list, t)) { + /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */ + tok_str_add2(tok_str, TOK_NOSUBST, NULL); + goto no_subst; + } + + { + TokenString str; + str.str = (int*)macro_str; + begin_macro(&str, 2); + + tok = t; + macro_subst_tok(tok_str, nested_list, s); + + if (str.alloc == 3) { + /* already finished by reading function macro arguments */ + break; + } + + macro_str = macro_ptr; + end_macro (); + } + if (tok_str->len) + spc = is_space(t = tok_str->str[tok_str->lastlen]); + } else { + if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) + tcc_error("stray '\\' in program"); +no_subst: + if (!check_space(t, &spc)) + tok_str_add2(tok_str, t, &cval); + + if (nosubst) { + if (nosubst > 1 && (spc || (++nosubst == 3 && t == '('))) + continue; + nosubst = 0; + } + if (t == TOK_NOSUBST) + nosubst = 1; + } + /* GCC supports 'defined' as result of a macro substitution */ + if (t == TOK_DEFINED && pp_expr) + nosubst = 2; + } +} + +/* return next token with macro substitution */ +ST_FUNC void next(void) +{ + redo: + if (parse_flags & PARSE_FLAG_SPACES) + next_nomacro_spc(); + else + next_nomacro(); + + if (macro_ptr) { + if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) { + /* discard preprocessor markers */ + goto redo; + } else if (tok == 0) { + /* end of macro or unget token string */ + end_macro(); + goto redo; + } + } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) { + Sym *s; + /* if reading from file, try to substitute macros */ + s = define_find(tok); + if (s) { + Sym *nested_list = NULL; + tokstr_buf.len = 0; + macro_subst_tok(&tokstr_buf, &nested_list, s); + tok_str_add(&tokstr_buf, 0); + begin_macro(&tokstr_buf, 2); + goto redo; + } + } + /* convert preprocessor tokens into C tokens */ + if (tok == TOK_PPNUM) { + if (parse_flags & PARSE_FLAG_TOK_NUM) + parse_number((char *)tokc.str.data); + } else if (tok == TOK_PPSTR) { + if (parse_flags & PARSE_FLAG_TOK_STR) + parse_string((char *)tokc.str.data, tokc.str.size - 1); + } +} + +/* push back current token and set current token to 'last_tok'. Only + identifier case handled for labels. */ +ST_INLN void unget_tok(int last_tok) +{ + + TokenString *str = tok_str_alloc(); + tok_str_add2(str, tok, &tokc); + tok_str_add(str, 0); + begin_macro(str, 1); + tok = last_tok; +} + +ST_FUNC void preprocess_start(TCCState *s1, int is_asm) +{ + CString cstr; + int i; + + s1->include_stack_ptr = s1->include_stack; + s1->ifdef_stack_ptr = s1->ifdef_stack; + file->ifdef_stack_ptr = s1->ifdef_stack_ptr; + pp_expr = 0; + pp_counter = 0; + pp_debug_tok = pp_debug_symv = 0; + pp_once++; + pvtop = vtop = vstack - 1; + s1->pack_stack[0] = 0; + s1->pack_stack_ptr = s1->pack_stack; + + set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0); + set_idnum('.', is_asm ? IS_ID : 0); + + cstr_new(&cstr); + cstr_cat(&cstr, "\"", -1); + cstr_cat(&cstr, file->filename, -1); + cstr_cat(&cstr, "\"", 0); + tcc_define_symbol(s1, "__BASE_FILE__", cstr.data); + + cstr_reset(&cstr); + for (i = 0; i < s1->nb_cmd_include_files; i++) { + cstr_cat(&cstr, "#include \"", -1); + cstr_cat(&cstr, s1->cmd_include_files[i], -1); + cstr_cat(&cstr, "\"\n", -1); + } + if (cstr.size) { + *s1->include_stack_ptr++ = file; + tcc_open_bf(s1, "", cstr.size); + memcpy(file->buffer, cstr.data, cstr.size); + } + cstr_free(&cstr); + + if (is_asm) + tcc_define_symbol(s1, "__ASSEMBLER__", NULL); + + parse_flags = is_asm ? PARSE_FLAG_ASM_FILE : 0; + tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; +} + +/* cleanup from error/setjmp */ +ST_FUNC void preprocess_end(TCCState *s1) +{ + while (macro_stack) + end_macro(); + macro_ptr = NULL; +} + +ST_FUNC void tccpp_new(TCCState *s) +{ + int i, c; + const char *p, *r; + + /* might be used in error() before preprocess_start() */ + s->include_stack_ptr = s->include_stack; + s->ppfp = stdout; + + /* init isid table */ + for(i = CH_EOF; i<128; i++) + set_idnum(i, + is_space(i) ? IS_SPC + : isid(i) ? IS_ID + : isnum(i) ? IS_NUM + : 0); + + for(i = 128; i<256; i++) + set_idnum(i, IS_ID); + + /* init allocators */ + tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE); + tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE); + tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE); + + memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *)); + cstr_new(&cstr_buf); + cstr_realloc(&cstr_buf, STRING_MAX_SIZE); + tok_str_new(&tokstr_buf); + tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE); + + tok_ident = TOK_IDENT; + p = tcc_keywords; + while (*p) { + r = p; + for(;;) { + c = *r++; + if (c == '\0') + break; + } + tok_alloc(p, r - p - 1); + p = r; + } +} + +ST_FUNC void tccpp_delete(TCCState *s) +{ + int i, n; + + /* free -D and compiler defines */ + free_defines(NULL); + + /* free tokens */ + n = tok_ident - TOK_IDENT; + for(i = 0; i < n; i++) + tal_free(toksym_alloc, table_ident[i]); + tcc_free(table_ident); + table_ident = NULL; + + /* free static buffers */ + cstr_free(&tokcstr); + cstr_free(&cstr_buf); + cstr_free(¯o_equal_buf); + tok_str_free_str(tokstr_buf.str); + + /* free allocators */ + tal_delete(toksym_alloc); + toksym_alloc = NULL; + tal_delete(tokstr_alloc); + tokstr_alloc = NULL; + tal_delete(cstr_alloc); + cstr_alloc = NULL; +} + +/* ------------------------------------------------------------------------- */ +/* tcc -E [-P[1]] [-dD} support */ + +static void tok_print(const char *msg, const int *str) +{ + FILE *fp; + int t, s = 0; + CValue cval; + + fp = tcc_state->ppfp; + fprintf(fp, "%s", msg); + while (str) { + TOK_GET(&t, &str, &cval); + if (!t) + break; + fprintf(fp, " %s" + s, get_tok_str(t, &cval)), s = 1; + } + fprintf(fp, "\n"); +} + +static void pp_line(TCCState *s1, BufferedFile *f, int level) +{ + int d = f->line_num - f->line_ref; + + if (s1->dflag & 4) + return; + + if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) { + ; + } else if (level == 0 && f->line_ref && d < 8) { + while (d > 0) + fputs("\n", s1->ppfp), --d; + } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) { + fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename); + } else { + fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, + level > 0 ? " 1" : level < 0 ? " 2" : ""); + } + f->line_ref = f->line_num; +} + +static void define_print(TCCState *s1, int v) +{ + FILE *fp; + Sym *s; + + s = define_find(v); + if (NULL == s || NULL == s->d) + return; + + fp = s1->ppfp; + fprintf(fp, "#define %s", get_tok_str(v, NULL)); + if (s->type.t == MACRO_FUNC) { + Sym *a = s->next; + fprintf(fp,"("); + if (a) + for (;;) { + fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL)); + if (!(a = a->next)) + break; + fprintf(fp,","); + } + fprintf(fp,")"); + } + tok_print("", s->d); +} + +static void pp_debug_defines(TCCState *s1) +{ + int v, t; + const char *vs; + FILE *fp; + + t = pp_debug_tok; + if (t == 0) + return; + + file->line_num--; + pp_line(s1, file, 0); + file->line_ref = ++file->line_num; + + fp = s1->ppfp; + v = pp_debug_symv; + vs = get_tok_str(v, NULL); + if (t == TOK_DEFINE) { + define_print(s1, v); + } else if (t == TOK_UNDEF) { + fprintf(fp, "#undef %s\n", vs); + } else if (t == TOK_push_macro) { + fprintf(fp, "#pragma push_macro(\"%s\")\n", vs); + } else if (t == TOK_pop_macro) { + fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs); + } + pp_debug_tok = 0; +} + +static void pp_debug_builtins(TCCState *s1) +{ + int v; + for (v = TOK_IDENT; v < tok_ident; ++v) + define_print(s1, v); +} + +/* Add a space between tokens a and b to avoid unwanted textual pasting */ +static int pp_need_space(int a, int b) +{ + return 'E' == a ? '+' == b || '-' == b + : '+' == a ? TOK_INC == b || '+' == b + : '-' == a ? TOK_DEC == b || '-' == b + : a >= TOK_IDENT ? b >= TOK_IDENT + : a == TOK_PPNUM ? b >= TOK_IDENT + : 0; +} + +/* maybe hex like 0x1e */ +static int pp_check_he0xE(int t, const char *p) +{ + if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E') + return 'E'; + return t; +} + +/* Preprocess the current file */ +ST_FUNC int tcc_preprocess(TCCState *s1) +{ + BufferedFile **iptr; + int token_seen, spcs, level; + const char *p; + char white[400]; + + parse_flags = PARSE_FLAG_PREPROCESS + | (parse_flags & PARSE_FLAG_ASM_FILE) + | PARSE_FLAG_LINEFEED + | PARSE_FLAG_SPACES + | PARSE_FLAG_ACCEPT_STRAYS + ; + /* Credits to Fabrice Bellard's initial revision to demonstrate its + capability to compile and run itself, provided all numbers are + given as decimals. tcc -E -P10 will do. */ + if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_P10) + parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1; + +#ifdef PP_BENCH + /* for PP benchmarks */ + do next(); while (tok != TOK_EOF); + return 0; +#endif + + if (s1->dflag & 1) { + pp_debug_builtins(s1); + s1->dflag &= ~1; + } + + token_seen = TOK_LINEFEED, spcs = 0; + pp_line(s1, file, 0); + for (;;) { + iptr = s1->include_stack_ptr; + next(); + if (tok == TOK_EOF) + break; + + level = s1->include_stack_ptr - iptr; + if (level) { + if (level > 0) + pp_line(s1, *iptr, 0); + pp_line(s1, file, level); + } + if (s1->dflag & 7) { + pp_debug_defines(s1); + if (s1->dflag & 4) + continue; + } + + if (is_space(tok)) { + if (spcs < sizeof white - 1) + white[spcs++] = tok; + continue; + } else if (tok == TOK_LINEFEED) { + spcs = 0; + if (token_seen == TOK_LINEFEED) + continue; + ++file->line_ref; + } else if (token_seen == TOK_LINEFEED) { + pp_line(s1, file, 0); + } else if (spcs == 0 && pp_need_space(token_seen, tok)) { + white[spcs++] = ' '; + } + + white[spcs] = 0, fputs(white, s1->ppfp), spcs = 0; + fputs(p = get_tok_str(tok, &tokc), s1->ppfp); + token_seen = pp_check_he0xE(tok, p); + } + return 0; +} + +/* ------------------------------------------------------------------------- */ diff --git a/05/tcc-final-old/tccrun.c b/05/tcc-final-old/tccrun.c new file mode 100644 index 0000000..2247499 --- /dev/null +++ b/05/tcc-final-old/tccrun.c @@ -0,0 +1,894 @@ +/* + * TCC - Tiny C Compiler - Support for -run switch + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +enum +{ + REG_R8 = 0, +# define REG_R8 REG_R8 + REG_R9, +# define REG_R9 REG_R9 + REG_R10, +# define REG_R10 REG_R10 + REG_R11, +# define REG_R11 REG_R11 + REG_R12, +# define REG_R12 REG_R12 + REG_R13, +# define REG_R13 REG_R13 + REG_R14, +# define REG_R14 REG_R14 + REG_R15, +# define REG_R15 REG_R15 + REG_RDI, +# define REG_RDI REG_RDI + REG_RSI, +# define REG_RSI REG_RSI + REG_RBP, +# define REG_RBP REG_RBP + REG_RBX, +# define REG_RBX REG_RBX + REG_RDX, +# define REG_RDX REG_RDX + REG_RAX, +# define REG_RAX REG_RAX + REG_RCX, +# define REG_RCX REG_RCX + REG_RSP, +# define REG_RSP REG_RSP + REG_RIP, +# define REG_RIP REG_RIP + REG_EFL, +# define REG_EFL REG_EFL + REG_CSGSFS, /* Actually short cs, gs, fs, __pad0. */ +# define REG_CSGSFS REG_CSGSFS + REG_ERR, +# define REG_ERR REG_ERR + REG_TRAPNO, +# define REG_TRAPNO REG_TRAPNO + REG_OLDMASK, +# define REG_OLDMASK REG_OLDMASK + REG_CR2 +# define REG_CR2 REG_CR2 +}; + +#include "tcc.h" + +/* only native compiler supports -run */ +#ifdef TCC_IS_NATIVE + +#ifndef _WIN32 +# include +#endif + +#ifdef CONFIG_TCC_BACKTRACE +# ifndef _WIN32 +# include +# ifndef __OpenBSD__ +# include +# endif +# else +# define ucontext_t CONTEXT +# endif +ST_DATA int rt_num_callers = 6; +ST_DATA const char **rt_bound_error_msg; +ST_DATA void *rt_prog_main; +static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level); +static void rt_error(ucontext_t *uc, const char *fmt, ...); +static void set_exception_handler(void); +#endif + +static void set_pages_executable(void *ptr, unsigned long length); +static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff); + +#ifdef _WIN64 +static void *win64_add_function_table(TCCState *s1); +static void win64_del_function_table(void *); +#endif + +/* ------------------------------------------------------------- */ +/* Do all relocations (needed before using tcc_get_symbol()) + Returns -1 on error. */ + +LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr) +{ + int size; + addr_t ptr_diff = 0; + + if (TCC_RELOCATE_AUTO != ptr) + return tcc_relocate_ex(s1, ptr, 0); + + size = tcc_relocate_ex(s1, NULL, 0); + if (size < 0) + return -1; + +#ifdef HAVE_SELINUX +{ + /* Using mmap instead of malloc */ + void *prx; + char tmpfname[] = "/tmp/.tccrunXXXXXX"; + int fd = mkstemp(tmpfname); + unlink(tmpfname); + ftruncate(fd, size); + + ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + prx = mmap (NULL, size, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); + if (ptr == MAP_FAILED || prx == MAP_FAILED) + tcc_error("tccrun: could not map memory"); + dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size); + dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, prx); + ptr_diff = (char*)prx - (char*)ptr; +} +#else + ptr = tcc_malloc(size); +#endif + tcc_relocate_ex(s1, ptr, ptr_diff); /* no more errors expected */ + dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr); + return 0; +} + +ST_FUNC void tcc_run_free(TCCState *s1) +{ + int i; + + for (i = 0; i < s1->nb_runtime_mem; ++i) { +#ifdef HAVE_SELINUX + unsigned size = (unsigned)(addr_t)s1->runtime_mem[i++]; + munmap(s1->runtime_mem[i++], size); + munmap(s1->runtime_mem[i], size); +#else +#ifdef _WIN64 + win64_del_function_table(*(void**)s1->runtime_mem[i]); +#endif + tcc_free(s1->runtime_mem[i]); +#endif + } + tcc_free(s1->runtime_mem); +} + +/* launch the compiled program with the given arguments */ +LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) +{ + int (*prog_main)(int, char **); + + s1->runtime_main = "main"; + if ((s1->dflag & 16) && !find_elf_sym(s1->symtab, s1->runtime_main)) + return 0; + if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0) + return -1; + prog_main = tcc_get_symbol_err(s1, s1->runtime_main); + +#ifdef CONFIG_TCC_BACKTRACE + if (s1->do_debug) { + set_exception_handler(); + rt_prog_main = prog_main; + } +#endif + + errno = 0; /* clean errno value */ + +#ifdef CONFIG_TCC_BCHECK + if (s1->do_bounds_check) { + void (*bound_init)(void); + void (*bound_exit)(void); + void (*bound_new_region)(void *p, addr_t size); + int (*bound_delete_region)(void *p); + int i, ret; + + /* set error function */ + rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); + /* XXX: use .init section so that it also work in binary ? */ + bound_init = tcc_get_symbol_err(s1, "__bound_init"); + bound_exit = tcc_get_symbol_err(s1, "__bound_exit"); + bound_new_region = tcc_get_symbol_err(s1, "__bound_new_region"); + bound_delete_region = tcc_get_symbol_err(s1, "__bound_delete_region"); + + bound_init(); + /* mark argv area as valid */ + bound_new_region(argv, argc*sizeof(argv[0])); + for (i=0; inb_errors = 0; +#ifdef TCC_TARGET_PE + pe_output_file(s1, NULL); +#else + tcc_add_runtime(s1); + resolve_common_syms(s1); + build_got_entries(s1); +#endif + if (s1->nb_errors) + return -1; + } + + offset = 0, mem = (addr_t)ptr; + fill = -mem & RUN_SECTION_ALIGNMENT; +#ifdef _WIN64 + offset += sizeof (void*); +#endif + for (k = 0; k < 2; ++k) { + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (0 == (s->sh_flags & SHF_ALLOC)) + continue; + if (k != !(s->sh_flags & SHF_EXECINSTR)) + continue; + offset += fill; + if (!mem) + s->sh_addr = 0; + else if (s->sh_flags & SHF_EXECINSTR) + s->sh_addr = mem + offset + ptr_diff; + else + s->sh_addr = mem + offset; +#if 0 + if (mem) + printf("%-16s +%02lx %p %04x\n", + s->name, fill, (void*)s->sh_addr, (unsigned)s->data_offset); +#endif + offset += s->data_offset; + fill = -(mem + offset) & 15; + } +#if RUN_SECTION_ALIGNMENT > 15 + /* To avoid that x86 processors would reload cached instructions each time + when data is written in the near, we need to make sure that code and data + do not share the same 64 byte unit */ + fill = -(mem + offset) & RUN_SECTION_ALIGNMENT; +#endif + } + + /* relocate symbols */ + relocate_syms(s1, s1->symtab, 1); + if (s1->nb_errors) + return -1; + + if (0 == mem) + return offset + RUN_SECTION_ALIGNMENT; + +#ifdef TCC_TARGET_PE + s1->pe_imagebase = mem; +#endif + + /* relocate each section */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (s->reloc) + relocate_section(s1, s); + } + relocate_plt(s1); + + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (0 == (s->sh_flags & SHF_ALLOC)) + continue; + length = s->data_offset; + ptr = (void*)s->sh_addr; + if (s->sh_flags & SHF_EXECINSTR) + ptr = (char*)ptr - ptr_diff; + if (NULL == s->data || s->sh_type == SHT_NOBITS) + memset(ptr, 0, length); + else + memcpy(ptr, s->data, length); + /* mark executable sections as executable in memory */ + if (s->sh_flags & SHF_EXECINSTR) + set_pages_executable((char*)ptr + ptr_diff, length); + } + +#ifdef _WIN64 + *(void**)mem = win64_add_function_table(s1); +#endif + + return 0; +} + +/* ------------------------------------------------------------- */ +/* allow to run code in memory */ + +static void set_pages_executable(void *ptr, unsigned long length) +{ +#ifdef _WIN32 + unsigned long old_protect; + VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect); +#else + void __clear_cache(void *beginning, void *end); +# ifndef HAVE_SELINUX + addr_t start, end; +# ifndef PAGESIZE +# define PAGESIZE 4096 +# endif + start = (addr_t)ptr & ~(PAGESIZE - 1); + end = (addr_t)ptr + length; + end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1); + if (mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC)) + tcc_error("mprotect failed: did you mean to configure --with-selinux?"); +# endif +# if defined TCC_TARGET_ARM || defined TCC_TARGET_ARM64 + __clear_cache(ptr, (char *)ptr + length); +# endif +#endif +} + +#ifdef _WIN64 +static void *win64_add_function_table(TCCState *s1) +{ + void *p = NULL; + if (s1->uw_pdata) { + p = (void*)s1->uw_pdata->sh_addr; + RtlAddFunctionTable( + (RUNTIME_FUNCTION*)p, + s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION), + s1->pe_imagebase + ); + s1->uw_pdata = NULL; + } + return p; +} + +static void win64_del_function_table(void *p) +{ + if (p) { + RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p); + } +} +#endif + +/* ------------------------------------------------------------- */ +#ifdef CONFIG_TCC_BACKTRACE + +ST_FUNC void tcc_set_num_callers(int n) +{ + rt_num_callers = n; +} + +/* print the position in the source file of PC value 'pc' by reading + the stabs debug information */ +static addr_t rt_printline(addr_t wanted_pc, const char *msg) +{ + char func_name[128], last_func_name[128]; + addr_t func_addr, last_pc, pc; + const char *incl_files[INCLUDE_STACK_SIZE]; + int incl_index, len, last_line_num, i; + const char *str, *p; + + Stab_Sym *stab_sym = NULL, *stab_sym_end, *sym; + int stab_len = 0; + char *stab_str = NULL; + + if (stab_section) { + stab_len = stab_section->data_offset; + stab_sym = (Stab_Sym *)stab_section->data; + stab_str = (char *) stabstr_section->data; + } + + func_name[0] = '\0'; + func_addr = 0; + incl_index = 0; + last_func_name[0] = '\0'; + last_pc = (addr_t)-1; + last_line_num = 1; + + if (!stab_sym) + goto no_stabs; + + stab_sym_end = (Stab_Sym*)((char*)stab_sym + stab_len); + for (sym = stab_sym + 1; sym < stab_sym_end; ++sym) { + switch(sym->n_type) { + /* function start or end */ + case N_FUN: + if (sym->n_strx == 0) { + /* we test if between last line and end of function */ + pc = sym->n_value + func_addr; + if (wanted_pc >= last_pc && wanted_pc < pc) + goto found; + func_name[0] = '\0'; + func_addr = 0; + } else { + str = stab_str + sym->n_strx; + p = strchr(str, ':'); + if (!p) { + pstrcpy(func_name, sizeof(func_name), str); + } else { + len = p - str; + if (len > sizeof(func_name) - 1) + len = sizeof(func_name) - 1; + memcpy(func_name, str, len); + func_name[len] = '\0'; + } + func_addr = sym->n_value; + } + break; + /* line number info */ + case N_SLINE: + pc = sym->n_value + func_addr; + if (wanted_pc >= last_pc && wanted_pc < pc) + goto found; + last_pc = pc; + last_line_num = sym->n_desc; + /* XXX: slow! */ + strcpy(last_func_name, func_name); + break; + /* include files */ + case N_BINCL: + str = stab_str + sym->n_strx; + add_incl: + if (incl_index < INCLUDE_STACK_SIZE) { + incl_files[incl_index++] = str; + } + break; + case N_EINCL: + if (incl_index > 1) + incl_index--; + break; + case N_SO: + if (sym->n_strx == 0) { + incl_index = 0; /* end of translation unit */ + } else { + str = stab_str + sym->n_strx; + /* do not add path */ + len = strlen(str); + if (len > 0 && str[len - 1] != '/') + goto add_incl; + } + break; + } + } + +no_stabs: + /* second pass: we try symtab symbols (no line number info) */ + incl_index = 0; + if (symtab_section) + { + ElfW(Sym) *sym, *sym_end; + int type; + + sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); + for(sym = (ElfW(Sym) *)symtab_section->data + 1; + sym < sym_end; + sym++) { + type = ELFW(ST_TYPE)(sym->st_info); + if (type == STT_FUNC || type == STT_GNU_IFUNC) { + if (wanted_pc >= sym->st_value && + wanted_pc < sym->st_value + sym->st_size) { + pstrcpy(last_func_name, sizeof(last_func_name), + (char *) symtab_section->link->data + sym->st_name); + func_addr = sym->st_value; + goto found; + } + } + } + } + /* did not find any info: */ + fprintf(stderr, "%s %p ???\n", msg, (void*)wanted_pc); + fflush(stderr); + return 0; + found: + i = incl_index; + if (i > 0) + fprintf(stderr, "%s:%d: ", incl_files[--i], last_line_num); + fprintf(stderr, "%s %p", msg, (void*)wanted_pc); + if (last_func_name[0] != '\0') + fprintf(stderr, " %s()", last_func_name); + if (--i >= 0) { + fprintf(stderr, " (included from "); + for (;;) { + fprintf(stderr, "%s", incl_files[i]); + if (--i < 0) + break; + fprintf(stderr, ", "); + } + fprintf(stderr, ")"); + } + fprintf(stderr, "\n"); + fflush(stderr); + return func_addr; +} + +/* emit a run time error at position 'pc' */ +static void rt_error(ucontext_t *uc, const char *fmt, ...) +{ + va_list ap; + addr_t pc; + int i; + + fprintf(stderr, "Runtime error: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); + + for(i=0;isi_code) { +// case FPE_INTDIV: +// case FPE_FLTDIV: +// rt_error(uc, "division by zero"); +// break; +// default: +// rt_error(uc, "floating point exception"); +// break; +// } +// break; +// case SIGBUS: +// case SIGSEGV: +// if (rt_bound_error_msg && *rt_bound_error_msg) +// rt_error(uc, *rt_bound_error_msg); +// else +// rt_error(uc, "dereferencing invalid pointer"); +// break; +// case SIGILL: +// rt_error(uc, "illegal instruction"); +// break; +// case SIGABRT: +// rt_error(uc, "abort() called"); +// break; +// default: +// rt_error(uc, "caught signal %d", signum); +// break; +// } +// exit(255); +// } + +#ifndef SA_SIGINFO +# define SA_SIGINFO 0x00000004u +#endif + +/* Generate a stack backtrace when a CPU exception occurs. */ +static void set_exception_handler(void) +{ + struct sigaction sigact; + /* install TCC signal handlers to print debug info on fatal + runtime errors */ + sigact.sa_flags = SA_SIGINFO | SA_RESETHAND; + // sigact.sa_sigaction = sig_error; + sigemptyset(&sigact.sa_mask); + sigaction(SIGFPE, &sigact, NULL); + sigaction(SIGILL, &sigact, NULL); + sigaction(SIGSEGV, &sigact, NULL); + sigaction(SIGBUS, &sigact, NULL); + sigaction(SIGABRT, &sigact, NULL); +} + +/* ------------------------------------------------------------- */ +#ifdef __i386__ + +/* fix for glibc 2.1 */ +#ifndef REG_EIP +#define REG_EIP EIP +#define REG_EBP EBP +#endif + +/* return the PC at frame level 'level'. Return negative if not found */ +static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) +{ + addr_t fp; + int i; + + if (level == 0) { +#if defined(__APPLE__) + *paddr = uc->uc_mcontext->__ss.__eip; +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + *paddr = uc->uc_mcontext.mc_eip; +#elif defined(__dietlibc__) + *paddr = uc->uc_mcontext.eip; +#elif defined(__NetBSD__) + *paddr = uc->uc_mcontext.__gregs[_REG_EIP]; +#elif defined(__OpenBSD__) + *paddr = uc->sc_eip; +#else + *paddr = uc->uc_mcontext.__gregs[REG_EIP]; +#endif + return 0; + } else { +#if defined(__APPLE__) + fp = uc->uc_mcontext->__ss.__ebp; +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + fp = uc->uc_mcontext.mc_ebp; +#elif defined(__dietlibc__) + fp = uc->uc_mcontext.ebp; +#elif defined(__NetBSD__) + fp = uc->uc_mcontext.__gregs[_REG_EBP]; +#elif defined(__OpenBSD__) + *paddr = uc->sc_ebp; +#else + fp = uc->uc_mcontext.__gregs[REG_EBP]; +#endif + for(i=1;i= 0xc0000000) + return -1; + fp = ((addr_t *)fp)[0]; + } + *paddr = ((addr_t *)fp)[1]; + return 0; + } +} + +/* ------------------------------------------------------------- */ +#elif defined(__x86_64__) + +/* return the PC at frame level 'level'. Return negative if not found */ +static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) +{ + addr_t fp; + int i; + + if (level == 0) { + /* XXX: only support linux */ +#if defined(__APPLE__) + *paddr = uc->uc_mcontext->__ss.__rip; +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + *paddr = uc->uc_mcontext.mc_rip; +#elif defined(__NetBSD__) + *paddr = uc->uc_mcontext.__gregs[_REG_RIP]; +#else + *paddr = uc->uc_mcontext.__gregs[REG_RIP]; +#endif + return 0; + } else { +#if defined(__APPLE__) + fp = uc->uc_mcontext->__ss.__rbp; +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + fp = uc->uc_mcontext.mc_rbp; +#elif defined(__NetBSD__) + fp = uc->uc_mcontext.__gregs[_REG_RBP]; +#else + fp = uc->uc_mcontext.__gregs[REG_RBP]; +#endif + for(i=1;iuc_mcontext.arm_pc; +#else + return -1; +#endif + return 0; + } else { +#if defined(__linux__) + fp = uc->uc_mcontext.arm_fp; + sp = uc->uc_mcontext.arm_sp; + if (sp < 0x1000) + sp = 0x1000; +#else + return -1; +#endif + /* XXX: specific to tinycc stack frames */ + if (fp < sp + 12 || fp & 3) + return -1; + for(i = 1; i < level; i++) { + sp = ((addr_t *)fp)[-2]; + if (sp < fp || sp - fp > 16 || sp & 3) + return -1; + fp = ((addr_t *)fp)[-3]; + if (fp <= sp || fp - sp < 12 || fp & 3) + return -1; + } + /* XXX: check address validity with program info */ + *paddr = ((addr_t *)fp)[-1]; + return 0; + } +} + +/* ------------------------------------------------------------- */ +#elif defined(__aarch64__) + +static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) +{ + if (level < 0) + return -1; + else if (level == 0) { + *paddr = uc->uc_mcontext.pc; + return 0; + } + else { + addr_t *fp = (addr_t *)uc->uc_mcontext.regs[29]; + int i; + for (i = 1; i < level; i++) + fp = (addr_t *)fp[0]; + *paddr = fp[1]; + return 0; + } +} + +/* ------------------------------------------------------------- */ +#else + +#warning add arch specific rt_get_caller_pc() +static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) +{ + return -1; +} + +#endif /* !__i386__ */ + +/* ------------------------------------------------------------- */ +#else /* WIN32 */ + +static long __stdcall cpu_exception_handler(EXCEPTION_POINTERS *ex_info) +{ + EXCEPTION_RECORD *er = ex_info->ExceptionRecord; + CONTEXT *uc = ex_info->ContextRecord; + switch (er->ExceptionCode) { + case EXCEPTION_ACCESS_VIOLATION: + if (rt_bound_error_msg && *rt_bound_error_msg) + rt_error(uc, *rt_bound_error_msg); + else + rt_error(uc, "access violation"); + break; + case EXCEPTION_STACK_OVERFLOW: + rt_error(uc, "stack overflow"); + break; + case EXCEPTION_INT_DIVIDE_BY_ZERO: + rt_error(uc, "division by zero"); + break; + default: + rt_error(uc, "exception caught"); + break; + } + return EXCEPTION_EXECUTE_HANDLER; +} + +/* Generate a stack backtrace when a CPU exception occurs. */ +static void set_exception_handler(void) +{ + SetUnhandledExceptionFilter(cpu_exception_handler); +} + +/* return the PC at frame level 'level'. Return non zero if not found */ +static int rt_get_caller_pc(addr_t *paddr, CONTEXT *uc, int level) +{ + addr_t fp, pc; + int i; +#ifdef _WIN64 + pc = uc->Rip; + fp = uc->Rbp; +#else + pc = uc->Eip; + fp = uc->Ebp; +#endif + if (level > 0) { + for(i=1;i= 0xc0000000) + return -1; + fp = ((addr_t*)fp)[0]; + } + pc = ((addr_t*)fp)[1]; + } + *paddr = pc; + return 0; +} + +#endif /* _WIN32 */ +#endif /* CONFIG_TCC_BACKTRACE */ +/* ------------------------------------------------------------- */ +#ifdef CONFIG_TCC_STATIC + +/* dummy function for profiling */ +ST_FUNC void *dlopen(const char *filename, int flag) +{ + return NULL; +} + +ST_FUNC void dlclose(void *p) +{ +} + +ST_FUNC const char *dlerror(void) +{ + return "error"; +} + +typedef struct TCCSyms { + char *str; + void *ptr; +} TCCSyms; + + +/* add the symbol you want here if no dynamic linking is done */ +static TCCSyms tcc_syms[] = { +#if !defined(CONFIG_TCCBOOT) +#define TCCSYM(a) { #a, &a, }, + TCCSYM(printf) + TCCSYM(fprintf) + TCCSYM(fopen) + TCCSYM(fclose) +#undef TCCSYM +#endif + { NULL, NULL }, +}; + +ST_FUNC void *dlsym(void *handle, const char *symbol) +{ + TCCSyms *p; + p = tcc_syms; + while (p->str != NULL) { + if (!strcmp(p->str, symbol)) + return p->ptr; + p++; + } + return NULL; +} + +#endif /* CONFIG_TCC_STATIC */ +#endif /* TCC_IS_NATIVE */ +/* ------------------------------------------------------------- */ diff --git a/05/tcc-final-old/tcctok.h b/05/tcc-final-old/tcctok.h new file mode 100644 index 0000000..317f64c --- /dev/null +++ b/05/tcc-final-old/tcctok.h @@ -0,0 +1,350 @@ +/* keywords */ + DEF(TOK_INT, "int") + DEF(TOK_VOID, "void") + DEF(TOK_CHAR, "char") + DEF(TOK_IF, "if") + DEF(TOK_ELSE, "else") + DEF(TOK_WHILE, "while") + DEF(TOK_BREAK, "break") + DEF(TOK_RETURN, "return") + DEF(TOK_FOR, "for") + DEF(TOK_EXTERN, "extern") + DEF(TOK_STATIC, "static") + DEF(TOK_UNSIGNED, "unsigned") + DEF(TOK_GOTO, "goto") + DEF(TOK_DO, "do") + DEF(TOK_CONTINUE, "continue") + DEF(TOK_SWITCH, "switch") + DEF(TOK_CASE, "case") + + DEF(TOK_CONST1, "const") + DEF(TOK_CONST2, "__const") /* gcc keyword */ + DEF(TOK_CONST3, "__const__") /* gcc keyword */ + DEF(TOK_VOLATILE1, "volatile") + DEF(TOK_VOLATILE2, "__volatile") /* gcc keyword */ + DEF(TOK_VOLATILE3, "__volatile__") /* gcc keyword */ + DEF(TOK_LONG, "long") + DEF(TOK_REGISTER, "register") + DEF(TOK_SIGNED1, "signed") + DEF(TOK_SIGNED2, "__signed") /* gcc keyword */ + DEF(TOK_SIGNED3, "__signed__") /* gcc keyword */ + DEF(TOK_AUTO, "auto") + DEF(TOK_INLINE1, "inline") + DEF(TOK_INLINE2, "__inline") /* gcc keyword */ + DEF(TOK_INLINE3, "__inline__") /* gcc keyword */ + DEF(TOK_RESTRICT1, "restrict") + DEF(TOK_RESTRICT2, "__restrict") + DEF(TOK_RESTRICT3, "__restrict__") + DEF(TOK_EXTENSION, "__extension__") /* gcc keyword */ + + DEF(TOK_GENERIC, "_Generic") + + DEF(TOK_FLOAT, "float") + DEF(TOK_DOUBLE, "double") + DEF(TOK_BOOL, "_Bool") + DEF(TOK_SHORT, "short") + DEF(TOK_STRUCT, "struct") + DEF(TOK_UNION, "union") + DEF(TOK_TYPEDEF, "typedef") + DEF(TOK_DEFAULT, "default") + DEF(TOK_ENUM, "enum") + DEF(TOK_SIZEOF, "sizeof") + DEF(TOK_ATTRIBUTE1, "__attribute") + DEF(TOK_ATTRIBUTE2, "__attribute__") + DEF(TOK_ALIGNOF1, "__alignof") + DEF(TOK_ALIGNOF2, "__alignof__") + DEF(TOK_TYPEOF1, "typeof") + DEF(TOK_TYPEOF2, "__typeof") + DEF(TOK_TYPEOF3, "__typeof__") + DEF(TOK_LABEL, "__label__") + DEF(TOK_ASM1, "asm") + DEF(TOK_ASM2, "__asm") + DEF(TOK_ASM3, "__asm__") + +#ifdef TCC_TARGET_ARM64 + DEF(TOK_UINT128, "__uint128_t") +#endif + +/*********************************************************************/ +/* the following are not keywords. They are included to ease parsing */ +/* preprocessor only */ + DEF(TOK_DEFINE, "define") + DEF(TOK_INCLUDE, "include") + DEF(TOK_INCLUDE_NEXT, "include_next") + DEF(TOK_IFDEF, "ifdef") + DEF(TOK_IFNDEF, "ifndef") + DEF(TOK_ELIF, "elif") + DEF(TOK_ENDIF, "endif") + DEF(TOK_DEFINED, "defined") + DEF(TOK_UNDEF, "undef") + DEF(TOK_ERROR, "error") + DEF(TOK_WARNING, "warning") + DEF(TOK_LINE, "line") + DEF(TOK_PRAGMA, "pragma") + DEF(TOK___LINE__, "__LINE__") + DEF(TOK___FILE__, "__FILE__") + DEF(TOK___DATE__, "__DATE__") + DEF(TOK___TIME__, "__TIME__") + DEF(TOK___FUNCTION__, "__FUNCTION__") + DEF(TOK___VA_ARGS__, "__VA_ARGS__") + DEF(TOK___COUNTER__, "__COUNTER__") + +/* special identifiers */ + DEF(TOK___FUNC__, "__func__") + +/* special floating point values */ + DEF(TOK___NAN__, "__nan__") + DEF(TOK___SNAN__, "__snan__") + DEF(TOK___INF__, "__inf__") + +/* attribute identifiers */ +/* XXX: handle all tokens generically since speed is not critical */ + DEF(TOK_SECTION1, "section") + DEF(TOK_SECTION2, "__section__") + DEF(TOK_ALIGNED1, "aligned") + DEF(TOK_ALIGNED2, "__aligned__") + DEF(TOK_PACKED1, "packed") + DEF(TOK_PACKED2, "__packed__") + DEF(TOK_WEAK1, "weak") + DEF(TOK_WEAK2, "__weak__") + DEF(TOK_ALIAS1, "alias") + DEF(TOK_ALIAS2, "__alias__") + DEF(TOK_UNUSED1, "unused") + DEF(TOK_UNUSED2, "__unused__") + DEF(TOK_CDECL1, "cdecl") + DEF(TOK_CDECL2, "__cdecl") + DEF(TOK_CDECL3, "__cdecl__") + DEF(TOK_STDCALL1, "stdcall") + DEF(TOK_STDCALL2, "__stdcall") + DEF(TOK_STDCALL3, "__stdcall__") + DEF(TOK_FASTCALL1, "fastcall") + DEF(TOK_FASTCALL2, "__fastcall") + DEF(TOK_FASTCALL3, "__fastcall__") + DEF(TOK_REGPARM1, "regparm") + DEF(TOK_REGPARM2, "__regparm__") + + DEF(TOK_MODE, "__mode__") + DEF(TOK_MODE_QI, "__QI__") + DEF(TOK_MODE_DI, "__DI__") + DEF(TOK_MODE_HI, "__HI__") + DEF(TOK_MODE_SI, "__SI__") + DEF(TOK_MODE_word, "__word__") + + DEF(TOK_DLLEXPORT, "dllexport") + DEF(TOK_DLLIMPORT, "dllimport") + DEF(TOK_NORETURN1, "noreturn") + DEF(TOK_NORETURN2, "__noreturn__") + DEF(TOK_VISIBILITY1, "visibility") + DEF(TOK_VISIBILITY2, "__visibility__") + + DEF(TOK_builtin_types_compatible_p, "__builtin_types_compatible_p") + DEF(TOK_builtin_choose_expr, "__builtin_choose_expr") + DEF(TOK_builtin_constant_p, "__builtin_constant_p") + DEF(TOK_builtin_frame_address, "__builtin_frame_address") + DEF(TOK_builtin_return_address, "__builtin_return_address") + DEF(TOK_builtin_expect, "__builtin_expect") + /*DEF(TOK_builtin_va_list, "__builtin_va_list")*/ +#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64 + DEF(TOK_builtin_va_start, "__builtin_va_start") +#elif defined TCC_TARGET_X86_64 + DEF(TOK_builtin_va_arg_types, "__builtin_va_arg_types") +#elif defined TCC_TARGET_ARM64 + DEF(TOK___va_start, "__va_start") + DEF(TOK___va_arg, "__va_arg") +#endif + +/* pragma */ + DEF(TOK_pack, "pack") +#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_X86_64) + /* already defined for assembler */ + DEF(TOK_ASM_push, "push") + DEF(TOK_ASM_pop, "pop") +#endif + DEF(TOK_comment, "comment") + DEF(TOK_lib, "lib") + DEF(TOK_push_macro, "push_macro") + DEF(TOK_pop_macro, "pop_macro") + DEF(TOK_once, "once") + DEF(TOK_option, "option") + +/* builtin functions or variables */ +#ifndef TCC_ARM_EABI + DEF(TOK_memcpy, "memcpy") + DEF(TOK_memmove, "memmove") + DEF(TOK_memset, "memset") + DEF(TOK___divdi3, "__divdi3") + DEF(TOK___moddi3, "__moddi3") + DEF(TOK___udivdi3, "__udivdi3") + DEF(TOK___umoddi3, "__umoddi3") + DEF(TOK___ashrdi3, "__ashrdi3") + DEF(TOK___lshrdi3, "__lshrdi3") + DEF(TOK___ashldi3, "__ashldi3") + DEF(TOK___floatundisf, "__floatundisf") + DEF(TOK___floatundidf, "__floatundidf") +# ifndef TCC_ARM_VFP + DEF(TOK___floatundixf, "__floatundixf") + DEF(TOK___fixunsxfdi, "__fixunsxfdi") +# endif + DEF(TOK___fixunssfdi, "__fixunssfdi") + DEF(TOK___fixunsdfdi, "__fixunsdfdi") +#endif + +#if defined TCC_TARGET_ARM +# ifdef TCC_ARM_EABI + DEF(TOK_memcpy, "__aeabi_memcpy") + DEF(TOK_memcpy4, "__aeabi_memcpy4") + DEF(TOK_memcpy8, "__aeabi_memcpy8") + DEF(TOK_memmove, "__aeabi_memmove") + DEF(TOK_memset, "__aeabi_memset") + DEF(TOK___aeabi_ldivmod, "__aeabi_ldivmod") + DEF(TOK___aeabi_uldivmod, "__aeabi_uldivmod") + DEF(TOK___aeabi_idivmod, "__aeabi_idivmod") + DEF(TOK___aeabi_uidivmod, "__aeabi_uidivmod") + DEF(TOK___divsi3, "__aeabi_idiv") + DEF(TOK___udivsi3, "__aeabi_uidiv") + DEF(TOK___floatdisf, "__aeabi_l2f") + DEF(TOK___floatdidf, "__aeabi_l2d") + DEF(TOK___fixsfdi, "__aeabi_f2lz") + DEF(TOK___fixdfdi, "__aeabi_d2lz") + DEF(TOK___ashrdi3, "__aeabi_lasr") + DEF(TOK___lshrdi3, "__aeabi_llsr") + DEF(TOK___ashldi3, "__aeabi_llsl") + DEF(TOK___floatundisf, "__aeabi_ul2f") + DEF(TOK___floatundidf, "__aeabi_ul2d") + DEF(TOK___fixunssfdi, "__aeabi_f2ulz") + DEF(TOK___fixunsdfdi, "__aeabi_d2ulz") +# else + DEF(TOK___modsi3, "__modsi3") + DEF(TOK___umodsi3, "__umodsi3") + DEF(TOK___divsi3, "__divsi3") + DEF(TOK___udivsi3, "__udivsi3") + DEF(TOK___floatdisf, "__floatdisf") + DEF(TOK___floatdidf, "__floatdidf") +# ifndef TCC_ARM_VFP + DEF(TOK___floatdixf, "__floatdixf") + DEF(TOK___fixunssfsi, "__fixunssfsi") + DEF(TOK___fixunsdfsi, "__fixunsdfsi") + DEF(TOK___fixunsxfsi, "__fixunsxfsi") + DEF(TOK___fixxfdi, "__fixxfdi") +# endif + DEF(TOK___fixsfdi, "__fixsfdi") + DEF(TOK___fixdfdi, "__fixdfdi") +# endif +#endif + +#if defined TCC_TARGET_C67 + DEF(TOK__divi, "_divi") + DEF(TOK__divu, "_divu") + DEF(TOK__divf, "_divf") + DEF(TOK__divd, "_divd") + DEF(TOK__remi, "_remi") + DEF(TOK__remu, "_remu") +#endif + +#if defined TCC_TARGET_I386 + DEF(TOK___fixsfdi, "__fixsfdi") + DEF(TOK___fixdfdi, "__fixdfdi") + DEF(TOK___fixxfdi, "__fixxfdi") +#endif + +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 + DEF(TOK_alloca, "alloca") +#endif + +#if defined TCC_TARGET_PE + DEF(TOK___chkstk, "__chkstk") +#endif +#ifdef TCC_TARGET_ARM64 + DEF(TOK___arm64_clear_cache, "__arm64_clear_cache") + DEF(TOK___addtf3, "__addtf3") + DEF(TOK___subtf3, "__subtf3") + DEF(TOK___multf3, "__multf3") + DEF(TOK___divtf3, "__divtf3") + DEF(TOK___extendsftf2, "__extendsftf2") + DEF(TOK___extenddftf2, "__extenddftf2") + DEF(TOK___trunctfsf2, "__trunctfsf2") + DEF(TOK___trunctfdf2, "__trunctfdf2") + DEF(TOK___fixtfsi, "__fixtfsi") + DEF(TOK___fixtfdi, "__fixtfdi") + DEF(TOK___fixunstfsi, "__fixunstfsi") + DEF(TOK___fixunstfdi, "__fixunstfdi") + DEF(TOK___floatsitf, "__floatsitf") + DEF(TOK___floatditf, "__floatditf") + DEF(TOK___floatunsitf, "__floatunsitf") + DEF(TOK___floatunditf, "__floatunditf") + DEF(TOK___eqtf2, "__eqtf2") + DEF(TOK___netf2, "__netf2") + DEF(TOK___lttf2, "__lttf2") + DEF(TOK___letf2, "__letf2") + DEF(TOK___gttf2, "__gttf2") + DEF(TOK___getf2, "__getf2") +#endif + +/* bound checking symbols */ +#ifdef CONFIG_TCC_BCHECK + DEF(TOK___bound_ptr_add, "__bound_ptr_add") + DEF(TOK___bound_ptr_indir1, "__bound_ptr_indir1") + DEF(TOK___bound_ptr_indir2, "__bound_ptr_indir2") + DEF(TOK___bound_ptr_indir4, "__bound_ptr_indir4") + DEF(TOK___bound_ptr_indir8, "__bound_ptr_indir8") + DEF(TOK___bound_ptr_indir12, "__bound_ptr_indir12") + DEF(TOK___bound_ptr_indir16, "__bound_ptr_indir16") + DEF(TOK___bound_main_arg, "__bound_main_arg") + DEF(TOK___bound_local_new, "__bound_local_new") + DEF(TOK___bound_local_delete, "__bound_local_delete") +# ifdef TCC_TARGET_PE + DEF(TOK_malloc, "malloc") + DEF(TOK_free, "free") + DEF(TOK_realloc, "realloc") + DEF(TOK_memalign, "memalign") + DEF(TOK_calloc, "calloc") +# endif + DEF(TOK_strlen, "strlen") + DEF(TOK_strcpy, "strcpy") +#endif + +/* Tiny Assembler */ + DEF_ASMDIR(byte) /* must be first directive */ + DEF_ASMDIR(word) + DEF_ASMDIR(align) + DEF_ASMDIR(balign) + DEF_ASMDIR(p2align) + DEF_ASMDIR(set) + DEF_ASMDIR(skip) + DEF_ASMDIR(space) + DEF_ASMDIR(string) + DEF_ASMDIR(asciz) + DEF_ASMDIR(ascii) + DEF_ASMDIR(file) + DEF_ASMDIR(globl) + DEF_ASMDIR(global) + DEF_ASMDIR(weak) + DEF_ASMDIR(hidden) + DEF_ASMDIR(ident) + DEF_ASMDIR(size) + DEF_ASMDIR(type) + DEF_ASMDIR(text) + DEF_ASMDIR(data) + DEF_ASMDIR(bss) + DEF_ASMDIR(previous) + DEF_ASMDIR(pushsection) + DEF_ASMDIR(popsection) + DEF_ASMDIR(fill) + DEF_ASMDIR(rept) + DEF_ASMDIR(endr) + DEF_ASMDIR(org) + DEF_ASMDIR(quad) +#if defined(TCC_TARGET_I386) + DEF_ASMDIR(code16) + DEF_ASMDIR(code32) +#elif defined(TCC_TARGET_X86_64) + DEF_ASMDIR(code64) +#endif + DEF_ASMDIR(short) + DEF_ASMDIR(long) + DEF_ASMDIR(int) + DEF_ASMDIR(section) /* must be last directive */ + +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 +#include "i386-tok.h" +#endif diff --git a/05/tcc-final-old/tcctools.c b/05/tcc-final-old/tcctools.c new file mode 100644 index 0000000..1d4424e --- /dev/null +++ b/05/tcc-final-old/tcctools.c @@ -0,0 +1,546 @@ +/* -------------------------------------------------------------- */ +/* + * TCC - Tiny C Compiler + * + * tcctools.c - extra tools and and -m32/64 support + * + */ + +/* -------------------------------------------------------------- */ +/* + * This program is for making libtcc1.a without ar + * tiny_libmaker - tiny elf lib maker + * usage: tiny_libmaker [lib] files... + * Copyright (c) 2007 Timppa + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "tcc.h" + +//#define ARMAG "!\n" +#define ARFMAG "`\n" + +typedef struct { + char ar_name[16]; + char ar_date[12]; + char ar_uid[6]; + char ar_gid[6]; + char ar_mode[8]; + char ar_size[10]; + char ar_fmag[2]; +} ArHdr; + +static unsigned long le2belong(unsigned long ul) { + return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) + + ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8); +} + +/* Returns 1 if s contains any of the chars of list, else 0 */ +static int contains_any(const char *s, const char *list) { + const char *l; + for (; *s; s++) { + for (l = list; *l; l++) { + if (*s == *l) + return 1; + } + } + return 0; +} + +static int ar_usage(int ret) { + fprintf(stderr, "usage: tcc -ar [rcsv] lib file...\n"); + fprintf(stderr, "create library ([abdioptxN] not supported).\n"); + return ret; +} + +ST_FUNC int tcc_tool_ar(TCCState *s1, int argc, char **argv) +{ + static ArHdr arhdr = { + "/ ", + " ", + "0 ", + "0 ", + "0 ", + " ", + ARFMAG + }; + + static ArHdr arhdro = { + " ", + " ", + "0 ", + "0 ", + "0 ", + " ", + ARFMAG + }; + + FILE *fi, *fh = NULL, *fo = NULL; + ElfW(Ehdr) *ehdr; + ElfW(Shdr) *shdr; + ElfW(Sym) *sym; + int i, fsize, i_lib, i_obj; + char *buf, *shstr, *symtab = NULL, *strtab = NULL; + int symtabsize = 0;//, strtabsize = 0; + char *anames = NULL; + int *afpos = NULL; + int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs; + char tfile[260], stmp[20]; + char *file, *name; + int ret = 2; + const char *ops_conflict = "habdioptxN"; // unsupported but destructive if ignored. + int verbose = 0; + + i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj + for (i = 1; i < argc; i++) { + const char *a = argv[i]; + if (*a == '-' && strstr(a, ".")) + ret = 1; // -x.y is always invalid (same as gnu ar) + if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument + if (contains_any(a, ops_conflict)) + ret = 1; + if (strstr(a, "v")) + verbose = 1; + } else { // lib or obj files: don't abort - keep validating all args. + if (!i_lib) // first file is the lib + i_lib = i; + else if (!i_obj) // second file is the first obj + i_obj = i; + } + } + + if (!i_obj) // i_obj implies also i_lib. we require both. + ret = 1; + + if (ret == 1) + return ar_usage(ret); + + if ((fh = fopen(argv[i_lib], "wb")) == NULL) + { + fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_lib]); + goto the_end; + } + + sprintf(tfile, "%s.tmp", argv[i_lib]); + if ((fo = fopen(tfile, "wb+")) == NULL) + { + fprintf(stderr, "tcc: ar: can't create temporary file %s\n", tfile); + goto the_end; + } + + funcmax = 250; + afpos = tcc_realloc(NULL, funcmax * sizeof *afpos); // 250 func + memcpy(&arhdro.ar_mode, "100666", 6); + + // i_obj = first input object file + while (i_obj < argc) + { + if (*argv[i_obj] == '-') { // by now, all options start with '-' + i_obj++; + continue; + } + if ((fi = fopen(argv[i_obj], "rb")) == NULL) { + fprintf(stderr, "tcc: ar: can't open file %s \n", argv[i_obj]); + goto the_end; + } + if (verbose) + printf("a - %s\n", argv[i_obj]); + + fseek(fi, 0, SEEK_END); + fsize = ftell(fi); + fseek(fi, 0, SEEK_SET); + buf = tcc_malloc(fsize + 1); + fread(buf, fsize, 1, fi); + fclose(fi); + + // elf header + ehdr = (ElfW(Ehdr) *)buf; + if (ehdr->e_ident[4] != ELFCLASSW) + { + fprintf(stderr, "tcc: ar: Unsupported Elf Class: %s\n", argv[i_obj]); + goto the_end; + } + + shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize); + shstr = (char *)(buf + shdr->sh_offset); + for (i = 0; i < ehdr->e_shnum; i++) + { + shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize); + if (!shdr->sh_offset) + continue; + if (shdr->sh_type == SHT_SYMTAB) + { + symtab = (char *)(buf + shdr->sh_offset); + symtabsize = shdr->sh_size; + } + if (shdr->sh_type == SHT_STRTAB) + { + if (!strcmp(shstr + shdr->sh_name, ".strtab")) + { + strtab = (char *)(buf + shdr->sh_offset); + //strtabsize = shdr->sh_size; + } + } + } + + if (symtab && symtabsize) + { + int nsym = symtabsize / sizeof(ElfW(Sym)); + //printf("symtab: info size shndx name\n"); + for (i = 1; i < nsym; i++) + { + sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym))); + if (sym->st_shndx && + (sym->st_info == 0x10 + || sym->st_info == 0x11 + || sym->st_info == 0x12 + )) { + //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name); + istrlen = strlen(strtab + sym->st_name)+1; + anames = tcc_realloc(anames, strpos+istrlen); + strcpy(anames + strpos, strtab + sym->st_name); + strpos += istrlen; + if (++funccnt >= funcmax) { + funcmax += 250; + afpos = tcc_realloc(afpos, funcmax * sizeof *afpos); // 250 func more + } + afpos[funccnt] = fpos; + } + } + } + + file = argv[i_obj]; + for (name = strchr(file, 0); + name > file && name[-1] != '/' && name[-1] != '\\'; + --name); + istrlen = strlen(name); + if (istrlen >= sizeof(arhdro.ar_name)) + istrlen = sizeof(arhdro.ar_name) - 1; + memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name)); + memcpy(arhdro.ar_name, name, istrlen); + arhdro.ar_name[istrlen] = '/'; + sprintf(stmp, "%-10d", fsize); + memcpy(&arhdro.ar_size, stmp, 10); + fwrite(&arhdro, sizeof(arhdro), 1, fo); + fwrite(buf, fsize, 1, fo); + tcc_free(buf); + i_obj++; + fpos += (fsize + sizeof(arhdro)); + } + hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int); + fpos = 0; + if ((hofs & 1)) // align + hofs++, fpos = 1; + // write header + fwrite("!\n", 8, 1, fh); + sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int))); + memcpy(&arhdr.ar_size, stmp, 10); + fwrite(&arhdr, sizeof(arhdr), 1, fh); + afpos[0] = le2belong(funccnt); + for (i=1; i<=funccnt; i++) + afpos[i] = le2belong(afpos[i] + hofs); + fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh); + fwrite(anames, strpos, 1, fh); + if (fpos) + fwrite("", 1, 1, fh); + // write objects + fseek(fo, 0, SEEK_END); + fsize = ftell(fo); + fseek(fo, 0, SEEK_SET); + buf = tcc_malloc(fsize + 1); + fread(buf, fsize, 1, fo); + fwrite(buf, fsize, 1, fh); + tcc_free(buf); + ret = 0; +the_end: + if (anames) + tcc_free(anames); + if (afpos) + tcc_free(afpos); + if (fh) + fclose(fh); + if (fo) + fclose(fo), remove(tfile); + return ret; +} + +/* -------------------------------------------------------------- */ +/* + * tiny_impdef creates an export definition file (.def) from a dll + * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]" + * + * Copyright (c) 2005,2007 grischka + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifdef TCC_TARGET_PE + +ST_FUNC int tcc_tool_impdef(TCCState *s1, int argc, char **argv) +{ + int ret, v, i; + char infile[260]; + char outfile[260]; + + const char *file; + char *p, *q; + FILE *fp, *op; + +#ifdef _WIN32 + char path[260]; +#endif + + infile[0] = outfile[0] = 0; + fp = op = NULL; + ret = 1; + p = NULL; + v = 0; + + for (i = 1; i < argc; ++i) { + const char *a = argv[i]; + if ('-' == a[0]) { + if (0 == strcmp(a, "-v")) { + v = 1; + } else if (0 == strcmp(a, "-o")) { + if (++i == argc) + goto usage; + strcpy(outfile, argv[i]); + } else + goto usage; + } else if (0 == infile[0]) + strcpy(infile, a); + else + goto usage; + } + + if (0 == infile[0]) { +usage: + fprintf(stderr, + "usage: tcc -impdef library.dll [-v] [-o outputfile]\n" + "create export definition file (.def) from dll\n" + ); + goto the_end; + } + + if (0 == outfile[0]) { + strcpy(outfile, tcc_basename(infile)); + q = strrchr(outfile, '.'); + if (NULL == q) + q = strchr(outfile, 0); + strcpy(q, ".def"); + } + + file = infile; +#ifdef _WIN32 + if (SearchPath(NULL, file, ".dll", sizeof path, path, NULL)) + file = path; +#endif + ret = tcc_get_dllexports(file, &p); + if (ret || !p) { + fprintf(stderr, "tcc: impdef: %s '%s'\n", + ret == -1 ? "can't find file" : + ret == 1 ? "can't read symbols" : + ret == 0 ? "no symbols found in" : + "unknown file type", file); + ret = 1; + goto the_end; + } + + if (v) + printf("-> %s\n", file); + + op = fopen(outfile, "wb"); + if (NULL == op) { + fprintf(stderr, "tcc: impdef: could not create output file: %s\n", outfile); + goto the_end; + } + + fprintf(op, "LIBRARY %s\n\nEXPORTS\n", tcc_basename(file)); + for (q = p, i = 0; *q; ++i) { + fprintf(op, "%s\n", q); + q += strlen(q) + 1; + } + + if (v) + printf("<- %s (%d symbol%s)\n", outfile, i, &"s"[i<2]); + + ret = 0; + +the_end: + /* cannot free memory received from tcc_get_dllexports + if it came from a dll */ + /* if (p) + tcc_free(p); */ + if (fp) + fclose(fp); + if (op) + fclose(op); + return ret; +} + +#endif /* TCC_TARGET_PE */ + +/* -------------------------------------------------------------- */ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */ + +#if !defined TCC_TARGET_I386 && !defined TCC_TARGET_X86_64 + +ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option) +{ + tcc_error("-m%d not implemented.", option); +} + +#else +#ifdef _WIN32 +#include + +static char *str_replace(const char *str, const char *p, const char *r) +{ + const char *s, *s0; + char *d, *d0; + int sl, pl, rl; + + sl = strlen(str); + pl = strlen(p); + rl = strlen(r); + for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) { + for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) { + if (d) { + memcpy(d, s0, sl = s - s0), d += sl; + memcpy(d, r, rl), d += rl; + } else + sl += rl - pl; + } + if (d) { + strcpy(d, s0); + return d0; + } + } +} + +static int execvp_win32(const char *prog, char **argv) +{ + int ret; char **p; + /* replace all " by \" */ + for (p = argv; *p; ++p) + if (strchr(*p, '"')) + *p = str_replace(*p, "\"", "\\\""); + ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv); + if (-1 == ret) + return ret; + _cwait(&ret, ret, WAIT_CHILD); + exit(ret); +} +#define execvp execvp_win32 +#endif /* _WIN32 */ + +ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int target) +{ + char program[4096]; + char *a0 = argv[0]; + int prefix = tcc_basename(a0) - a0; + + snprintf(program, sizeof program, + "%.*s%s" +#ifdef TCC_TARGET_PE + "-win32" +#endif + "-tcc" +#ifdef _WIN32 + ".exe" +#endif + , prefix, a0, target == 64 ? "x86_64" : "i386"); + + if (strcmp(a0, program)) + execvp(argv[0] = program, argv); + tcc_error("could not run '%s'", program); +} + +#endif /* TCC_TARGET_I386 && TCC_TARGET_X86_64 */ +/* -------------------------------------------------------------- */ +/* enable commandline wildcard expansion (tcc -o x.exe *.c) */ + +#ifdef _WIN32 +int _CRT_glob = 1; +#ifndef _CRT_glob +int _dowildcard = 1; +#endif +#endif + +/* -------------------------------------------------------------- */ +/* generate xxx.d file */ + +ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename) +{ + FILE *depout; + char buf[1024]; + int i; + + if (!filename) { + /* compute filename automatically: dir/file.o -> dir/file.d */ + snprintf(buf, sizeof buf, "%.*s.d", + (int)(tcc_fileextension(target) - target), target); + filename = buf; + } + + if (s->verbose) + printf("<- %s\n", filename); + + /* XXX return err codes instead of error() ? */ + depout = fopen(filename, "w"); + if (!depout) + tcc_error("could not open '%s'", filename); + + fprintf(depout, "%s: \\\n", target); + for (i=0; inb_target_deps; ++i) + fprintf(depout, " %s \\\n", s->target_deps[i]); + fprintf(depout, "\n"); + fclose(depout); +} + +/* -------------------------------------------------------------- */ diff --git a/05/tcc-final-old/tests/42test.h b/05/tcc-final-old/tests/42test.h new file mode 100644 index 0000000..5db7d1c --- /dev/null +++ b/05/tcc-final-old/tests/42test.h @@ -0,0 +1,13 @@ +/* This file is to test compute #include directives. It's named so + that it starts with a pre-processing number which isn't a valid + number (42test.h). Including this must work. */ +#ifndef INC42_FIRST +int have_included_42test_h; +#define INC42_FIRST +#elif !defined INC42_SECOND +#define INC42_SECOND +int have_included_42test_h_second; +#else +#define INC42_THIRD +int have_included_42test_h_third; +#endif diff --git a/05/tcc-final-old/tests/Makefile b/05/tcc-final-old/tests/Makefile new file mode 100644 index 0000000..5f6777d --- /dev/null +++ b/05/tcc-final-old/tests/Makefile @@ -0,0 +1,289 @@ +# +# Tiny C Compiler Makefile - tests +# + +TOP = .. +include $(TOP)/Makefile +VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP) +CFLAGS := $(filter-out -W% -g% -O%,$(CFLAGS)) -I$(TOPSRC) $(LDFLAGS) + +# what tests to run +TESTS = \ + hello-exe \ + hello-run \ + libtest \ + test3 \ + memtest \ + dlltest \ + abitest \ + asm-c-connect-test \ + vla_test-run \ + cross-test \ + tests2-dir \ + pp-dir + +BTESTS = test1b test3b btest + +# test4 -- problem with -static +# asmtest / asmtest2 -- minor differences with gcc +# btest -- works on i386 (including win32) + +# bounds-checking is supported only on i386 +ifneq ($(ARCH),i386) + TESTS := $(filter-out $(BTESTS),$(TESTS)) +endif +ifdef CONFIG_WIN32 + TESTS := $(filter-out $(BTESTS),$(TESTS)) +endif +ifdef CONFIG_OSX # -run only + TESTS := hello-run libtest tests2-dir pp-dir +endif +ifeq (,$(filter arm64 i386 x86_64,$(ARCH))) + TESTS := $(filter-out vla_test-run,$(TESTS)) +endif +ifeq ($(CONFIG_arm_eabi),yes) + TESTS := $(filter-out test3,$(TESTS)) +endif +ifeq (,$(filter i386 x86_64,$(ARCH))) + TESTS := $(filter-out dlltest asm-c-connect-test,$(TESTS)) +endif +ifndef CONFIG_cross + TESTS := $(filter-out cross-%,$(TESTS)) +endif + +ifeq ($(OS),Windows_NT) # for libtcc_test to find libtcc.dll + PATH := $(CURDIR)/$(TOP)$(if $(findstring :\,$(PATH)),;,:)$(PATH) +endif + +RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) +DISAS = objdump -d +DUMPTCC = (set -x; $(TOP)/tcc -vv; ldd $(TOP)/tcc; exit 1) + +all test : clean-s $(TESTS) + +hello-exe: ../examples/ex1.c + @echo ------------ $@ ------------ + $(TCC) $< -o hello$(EXESUF) && ./hello$(EXESUF) || $(DUMPTCC) + +hello-run: ../examples/ex1.c + @echo ------------ $@ ------------ + $(TCC) -run $< || $(DUMPTCC) + +libtest: libtcc_test$(EXESUF) + @echo ------------ $@ ------------ + ./libtcc_test$(EXESUF) $(TCCFLAGS) + +libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC) + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) + +%-dir: + @echo ------------ $@ ------------ + $(MAKE) -k -C $* + +# test.ref - generate using cc +test.ref: tcctest.c + $(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer + ./tcctest.gcc > $@ + +# auto test +test1 test1b: tcctest.c test.ref + @echo ------------ $@ ------------ + $(TCC) -run $< > test.out1 + @diff -u test.ref test.out1 && echo "Auto Test OK" + +# iterated test2 (compile tcc then compile tcctest.c !) +test2 test2b: tcctest.c test.ref + @echo ------------ $@ ------------ + $(TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out2 + @diff -u test.ref test.out2 && echo "Auto Test2 OK" + +# iterated test3 (compile tcc then compile tcc then compile tcctest.c !) +test3 test3b: tcctest.c test.ref + @echo ------------ $@ ------------ + $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out3 + @diff -u test.ref test.out3 && echo "Auto Test3 OK" + +test%b : TCCFLAGS += -b + +# binary output test +test4: tcctest.c test.ref + @echo ------------ $@ ------------ +# object + link output + $(TCC) -c -o tcctest3.o $< + $(TCC) -o tcctest3 tcctest3.o + ./tcctest3 > test3.out + @if diff -u test.ref test3.out ; then echo "Object Auto Test OK"; fi +# dynamic output + $(TCC) -o tcctest1 $< + ./tcctest1 > test1.out + @if diff -u test.ref test1.out ; then echo "Dynamic Auto Test OK"; fi +# dynamic output + bound check + $(TCC) -b -o tcctest4 $< + ./tcctest4 > test4.out + @if diff -u test.ref test4.out ; then echo "BCheck Auto Test OK"; fi +# static output + $(TCC) -static -o tcctest2 $< + ./tcctest2 > test2.out + @if diff -u test.ref test2.out ; then echo "Static Auto Test OK"; fi + +# use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them +dlltest: + @echo ------------ $@ ------------ + $(TCC) $(NATIVE_DEFINES) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF) + $(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF) + ./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c +ifndef CONFIG_WIN32 + @echo ------------ $@ with PIC ------------ + $(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c + $(TCC) libtcc.o $(LIBS) -shared -o libtcc2$(DLLSUF) + $(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF) + ./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c +endif + @rm tcc2$(EXESUF) libtcc2$(DLLSUF) + +memtest: + @echo ------------ $@ ------------ + $(CC) $(CFLAGS) $(NATIVE_DEFINES) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF) + ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) $(TOPSRC)/tcc.c $(LIBS) + ./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) $(TOPSRC)/tests/tcctest.c + + +# memory and bound check auto test +BOUNDS_OK = 1 4 8 10 14 +BOUNDS_FAIL= 2 5 7 9 11 12 13 15 + +btest: boundtest.c + @echo ------------ $@ ------------ + @for i in $(BOUNDS_OK); do \ + echo ; echo --- boundtest $$i ---; \ + if $(TCC) -b -run $< $$i ; then \ + echo succeeded as expected; \ + else\ + echo Failed positive test $$i ; exit 1 ; \ + fi ;\ + done ;\ + for i in $(BOUNDS_FAIL); do \ + echo ; echo --- boundtest $$i ---; \ + if $(TCC) -b -run $< $$i ; then \ + echo Failed negative test $$i ; exit 1 ;\ + else\ + echo failed as expected; \ + fi ;\ + done ;\ + echo; echo Bound test OK + +# speed test +speedtest: ex2 ex3 + @echo ------------ $@ ------------ + time ./ex2 1238 2 3 4 10 13 4 + time $(TCC) -run $(TOPSRC)/examples/ex2.c 1238 2 3 4 10 13 4 + time ./ex3 35 + time $(TCC) -run $(TOPSRC)/examples/ex3.c 35 + +weaktest: tcctest.c test.ref + $(TCC) -c $< -o weaktest.tcc.o + $(CC) -c $< -o weaktest.gcc.o $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer + objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt + objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt + diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK" + +ex%: $(TOPSRC)/examples/ex%.c + $(CC) -o $@ $< $(CFLAGS) + +# tiny assembler testing +asmtest.ref: asmtest.S + $(CC) -Wa,-W -o asmtest.ref.o -c asmtest.S + objdump -D asmtest.ref.o > asmtest.ref + +asmtest asmtest2: asmtest.ref + @echo ------------ $@ ------------ + $(TCC) $(MAYBE_RUN_TCC) -c asmtest.S + objdump -D asmtest.o > asmtest.out + @if diff -u --ignore-matching-lines="file format" asmtest.ref asmtest.out ; then echo "ASM Auto Test OK"; fi + +# test assembler with tcc compiled by itself +asmtest2: MAYBE_RUN_TCC = $(RUN_TCC) + +# Check that code generated by libtcc is binary compatible with +# that generated by CC +abitest-cc$(EXESUF): abitest.c $(LIBTCC) + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w + +abitest-tcc$(EXESUF): abitest.c libtcc.c + $(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS) + +ABITESTS := abitest-cc$(EXESUF) +ifneq ($(CONFIG_arm_eabi),yes) # not ARM soft-float + ABITESTS += abitest-tcc$(EXESUF) +endif + +abitest: $(ABITESTS) + @echo ------------ $@ ------------ + ./abitest-cc$(EXESUF) $(TCCFLAGS) +ifneq ($(CONFIG_arm_eabi),yes) # not ARM soft-float + ./abitest-tcc$(EXESUF) $(TCCFLAGS) +endif + +vla_test$(EXESUF): vla_test.c + $(TCC) -o $@ $^ + +vla_test-run: vla_test$(EXESUF) + @echo ------------ $@ ------------ + ./vla_test$(EXESUF) + +asm-c-connect$(EXESUF): asm-c-connect-1.c asm-c-connect-2.c + $(TCC) -o $@ $^ + +asm-c-connect-%.o: asm-c-connect-%.c + $(TCC) -c -o $@ $< + +asm-c-connect-sep$(EXESUF): asm-c-connect-1.o asm-c-connect-2.o + $(TCC) -o $@ $^ + +asm-c-connect-test: asm-c-connect$(EXESUF) asm-c-connect-sep$(EXESUF) + @echo ------------ $@ ------------ + ./asm-c-connect$(EXESUF) > asm-c-connect.out1 && cat asm-c-connect.out1 + ./asm-c-connect-sep$(EXESUF) > asm-c-connect.out2 && cat asm-c-connect.out2 + @diff -u asm-c-connect.out1 asm-c-connect.out2 && echo "ok" + +cross-test : + @echo ------------ $@ ------------ + $(TOP)/i386-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/i386-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/x86_64-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/x86_64-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/arm-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/arm-wince-tcc$(EXESUF) $(TCCFLAGS-win) -c $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/arm64-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/c67-tcc$(EXESUF) $(TCCFLAGS-unx) -c $(TOPSRC)/examples/ex3.c && echo "ok" + $(TOP)/i386-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/win32/examples/hello_win.c && echo "ok" + $(TOP)/x86_64-win32-tcc$(EXESUF) $(TCCFLAGS-win) $(TOPSRC)/win32/examples/hello_win.c && echo "ok" + $(TOP)/arm-wince-tcc$(EXESUF) $(TCCFLAGS-win) -c $(TOPSRC)/win32/examples/hello_win.c && echo "ok" + +# targets for development +%.bin: %.c tcc + $(TCC) -g -o $@ $< + $(DISAS) $@ + +instr: instr.o + objdump -d instr.o + +instr.o: instr.S + $(CC) -o $@ -c $< -O2 -Wall -g + +cache: tcc_g + cachegrind ./tcc_g -o /tmp/linpack -lm bench/linpack.c + vg_annotate tcc.c > /tmp/linpack.cache.log + +# clean +clean: + rm -f *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc *.gcc + rm -f *-cc *-gcc *-tcc *.exe hello libtcc_test vla_test tcctest[1234] + rm -f asm-c-connect$(EXESUF) + rm -f ex? tcc_g weaktest.*.txt *.def + @$(MAKE) -C tests2 $@ + @$(MAKE) -C pp $@ + +# silent clean, used before running tests +clean-s: + @$(MAKE) -s --no-print-directory clean diff --git a/05/tcc-final-old/tests/abitest.c b/05/tcc-final-old/tests/abitest.c new file mode 100644 index 0000000..4a192bd --- /dev/null +++ b/05/tcc-final-old/tests/abitest.c @@ -0,0 +1,691 @@ +#include +#include +#include +#include +#include + +// MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC +#if defined(_WIN32) && defined(__GNUC__) +#define LONG_DOUBLE double +#define LONG_DOUBLE_LITERAL(x) x +#else +#define LONG_DOUBLE long double +#define LONG_DOUBLE_LITERAL(x) x ## L +#endif + +static int g_argc; +static char **g_argv; + +static void set_options(TCCState *s, int argc, char **argv) +{ + int i; + for (i = 1; i < argc; ++i) { + char *a = argv[i]; + if (a[0] == '-') { + if (a[1] == 'B') + tcc_set_lib_path(s, a+2); + else if (a[1] == 'I') + tcc_add_include_path(s, a+2); + else if (a[1] == 'L') + tcc_add_library_path(s, a+2); + } + } +} + +typedef int (*callback_type) (void*); + +/* + * Compile source code and call a callback with a pointer to the symbol "f". + */ +static int run_callback(const char *src, callback_type callback) { + TCCState *s; + int result; + void *ptr; + + s = tcc_new(); + if (!s) + return -1; + + set_options(s, g_argc, g_argv); + + if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1) + return -1; + if (tcc_compile_string(s, src) == -1) + return -1; + if (tcc_relocate(s, TCC_RELOCATE_AUTO) == -1) + return -1; + + ptr = tcc_get_symbol(s, "f"); + if (!ptr) + return -1; + result = callback(ptr); + + tcc_delete(s); + + return result; +} + +#define STR2(x) #x +#define STR(x) STR2(x) + +#define RET_PRIMITIVE_TEST(name, type, val) \ + static int ret_ ## name ## _test_callback(void *ptr) { \ + type (*callback) (type) = (type(*)(type))ptr; \ + type x = val; \ + type y = callback(x); \ + return (y == x+x) ? 0 : -1; \ + } \ + \ + static int ret_ ## name ## _test(void) { \ + const char *src = STR(type) " f(" STR(type) " x) {return x+x;}"; \ + return run_callback(src, ret_ ## name ## _test_callback); \ + } + +RET_PRIMITIVE_TEST(int, int, 70000) +RET_PRIMITIVE_TEST(longlong, long long, 4333369356528LL) +RET_PRIMITIVE_TEST(float, float, 63.0) +RET_PRIMITIVE_TEST(double, double, 14789798.0) +RET_PRIMITIVE_TEST(longdouble, LONG_DOUBLE, LONG_DOUBLE_LITERAL(378943892.0)) + +/* + * ret_2float_test: + * + * On x86-64, a struct with 2 floats should be packed into a single + * SSE register (VT_DOUBLE is used for this purpose). + */ +typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type; +typedef ret_2float_test_type (*ret_2float_test_function_type) (ret_2float_test_type); + +static int ret_2float_test_callback(void *ptr) { + ret_2float_test_function_type f = (ret_2float_test_function_type)ptr; + ret_2float_test_type a = {10, 35}; + ret_2float_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; +} + +static int ret_2float_test(void) { + const char *src = + "typedef struct ret_2float_test_type_s {float x, y;} ret_2float_test_type;" + "ret_2float_test_type f(ret_2float_test_type a) {\n" + " ret_2float_test_type r = {a.x*5, a.y*3};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_2float_test_callback); +} + +/* + * ret_2double_test: + * + * On x86-64, a struct with 2 doubles should be passed in two SSE + * registers. + */ +typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type; +typedef ret_2double_test_type (*ret_2double_test_function_type) (ret_2double_test_type); + +static int ret_2double_test_callback(void *ptr) { + ret_2double_test_function_type f = (ret_2double_test_function_type)ptr; + ret_2double_test_type a = {10, 35}; + ret_2double_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; +} + +static int ret_2double_test(void) { + const char *src = + "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;" + "ret_2double_test_type f(ret_2double_test_type a) {\n" + " ret_2double_test_type r = {a.x*5, a.y*3};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_2double_test_callback); +} + +/* + * ret_8plus2double_test: + * + * This catches a corner case in the x86_64 ABI code: the first 7 + * arguments fit into registers, the 8th doesn't, but the 9th argument + * fits into the 8th XMM register. + * + * Note that the purpose of the 10th argument is to avoid a situation + * in which gcc would accidentally put the double at the right + * address, thus causing a success message even though TCC actually + * generated incorrect code. + */ +typedef ret_2double_test_type (*ret_8plus2double_test_function_type) (double, double, double, double, double, double, double, ret_2double_test_type, double, double); + +static int ret_8plus2double_test_callback(void *ptr) { + ret_8plus2double_test_function_type f = (ret_8plus2double_test_function_type)ptr; + ret_2double_test_type a = {10, 35}; + ret_2double_test_type r; + r = f(0, 0, 0, 0, 0, 0, 0, a, 37, 38); + return ((r.x == 37) && (r.y == 37)) ? 0 : -1; +} + +static int ret_8plus2double_test(void) { + const char *src = + "typedef struct ret_2double_test_type_s {double x, y;} ret_2double_test_type;" + "ret_2double_test_type f(double x1, double x2, double x3, double x4, double x5, double x6, double x7, ret_2double_test_type a, double x8, double x9) {\n" + " ret_2double_test_type r = { x8, x8 };\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_8plus2double_test_callback); +} + +/* + * ret_mixed_test: + * + * On x86-64, a struct with a double and a 64-bit integer should be + * passed in one SSE register and one integer register. + */ +typedef struct ret_mixed_test_type_s {double x; long long y;} ret_mixed_test_type; +typedef ret_mixed_test_type (*ret_mixed_test_function_type) (ret_mixed_test_type); + +static int ret_mixed_test_callback(void *ptr) { + ret_mixed_test_function_type f = (ret_mixed_test_function_type)ptr; + ret_mixed_test_type a = {10, 35}; + ret_mixed_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; +} + +static int ret_mixed_test(void) { + const char *src = + "typedef struct ret_mixed_test_type_s {double x; long long y;} ret_mixed_test_type;" + "ret_mixed_test_type f(ret_mixed_test_type a) {\n" + " ret_mixed_test_type r = {a.x*5, a.y*3};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_mixed_test_callback); +} + +/* + * ret_mixed2_test: + * + * On x86-64, a struct with two floats and two 32-bit integers should + * be passed in one SSE register and one integer register. + */ +typedef struct ret_mixed2_test_type_s {float x,x2; int y,y2;} ret_mixed2_test_type; +typedef ret_mixed2_test_type (*ret_mixed2_test_function_type) (ret_mixed2_test_type); + +static int ret_mixed2_test_callback(void *ptr) { + ret_mixed2_test_function_type f = (ret_mixed2_test_function_type)ptr; + ret_mixed2_test_type a = {10, 5, 35, 7 }; + ret_mixed2_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; +} + +static int ret_mixed2_test(void) { + const char *src = + "typedef struct ret_mixed2_test_type_s {float x, x2; int y,y2;} ret_mixed2_test_type;" + "ret_mixed2_test_type f(ret_mixed2_test_type a) {\n" + " ret_mixed2_test_type r = {a.x*5, 0, a.y*3, 0};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_mixed2_test_callback); +} + +/* + * ret_mixed3_test: + * + * On x86-64, this struct should be passed in two integer registers. + */ +typedef struct ret_mixed3_test_type_s {float x; int y; float x2; int y2;} ret_mixed3_test_type; +typedef ret_mixed3_test_type (*ret_mixed3_test_function_type) (ret_mixed3_test_type); + +static int ret_mixed3_test_callback(void *ptr) { + ret_mixed3_test_function_type f = (ret_mixed3_test_function_type)ptr; + ret_mixed3_test_type a = {10, 5, 35, 7 }; + ret_mixed3_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y2 == a.y*3)) ? 0 : -1; +} + +static int ret_mixed3_test(void) { + const char *src = + "typedef struct ret_mixed3_test_type_s {float x; int y; float x2; int y2;} ret_mixed3_test_type;" + "ret_mixed3_test_type f(ret_mixed3_test_type a) {\n" + " ret_mixed3_test_type r = {a.x*5, 0, 0, a.y*3};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_mixed3_test_callback); +} + +/* + * reg_pack_test: return a small struct which should be packed into + * registers (Win32) during return. + */ +typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type; +typedef reg_pack_test_type (*reg_pack_test_function_type) (reg_pack_test_type); + +static int reg_pack_test_callback(void *ptr) { + reg_pack_test_function_type f = (reg_pack_test_function_type)ptr; + reg_pack_test_type a = {10, 35}; + reg_pack_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; +} + +static int reg_pack_test(void) { + const char *src = + "typedef struct reg_pack_test_type_s {int x, y;} reg_pack_test_type;" + "reg_pack_test_type f(reg_pack_test_type a) {\n" + " reg_pack_test_type r = {a.x*5, a.y*3};\n" + " return r;\n" + "}\n"; + + return run_callback(src, reg_pack_test_callback); +} + +/* + * reg_pack_longlong_test: return a small struct which should be packed into + * registers (x86-64) during return. + */ +typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type; +typedef reg_pack_longlong_test_type (*reg_pack_longlong_test_function_type) (reg_pack_longlong_test_type); + +static int reg_pack_longlong_test_callback(void *ptr) { + reg_pack_longlong_test_function_type f = (reg_pack_longlong_test_function_type)ptr; + reg_pack_longlong_test_type a = {10, 35}; + reg_pack_longlong_test_type r; + r = f(a); + return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; +} + +static int reg_pack_longlong_test(void) { + const char *src = + "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;" + "reg_pack_longlong_test_type f(reg_pack_longlong_test_type a) {\n" + " reg_pack_longlong_test_type r = {a.x*5, a.y*3};\n" + " return r;\n" + "}\n"; + + return run_callback(src, reg_pack_longlong_test_callback); +} + +/* + * ret_6plus2longlong_test: + * + * This catches a corner case in the x86_64 ABI code: the first 5 + * arguments fit into registers, the 6th doesn't, but the 7th argument + * fits into the 6th argument integer register, %r9. + * + * Note that the purpose of the 10th argument is to avoid a situation + * in which gcc would accidentally put the longlong at the right + * address, thus causing a success message even though TCC actually + * generated incorrect code. + */ +typedef reg_pack_longlong_test_type (*ret_6plus2longlong_test_function_type) (long long, long long, long long, long long, long long, reg_pack_longlong_test_type, long long, long long); + +static int ret_6plus2longlong_test_callback(void *ptr) { + ret_6plus2longlong_test_function_type f = (ret_6plus2longlong_test_function_type)ptr; + reg_pack_longlong_test_type a = {10, 35}; + reg_pack_longlong_test_type r; + r = f(0, 0, 0, 0, 0, a, 37, 38); + return ((r.x == 37) && (r.y == 37)) ? 0 : -1; +} + +static int ret_6plus2longlong_test(void) { + const char *src = + "typedef struct reg_pack_longlong_test_type_s {long long x, y;} reg_pack_longlong_test_type;" + "reg_pack_longlong_test_type f(long long x1, long long x2, long long x3, long long x4, long long x5, reg_pack_longlong_test_type a, long long x8, long long x9) {\n" + " reg_pack_longlong_test_type r = { x8, x8 };\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_6plus2longlong_test_callback); +} + +/* + * sret_test: Create a struct large enough to be returned via sret + * (hidden pointer as first function argument) + */ +typedef struct sret_test_type_s {long long a, b, c;} sret_test_type; +typedef sret_test_type (*sret_test_function_type) (sret_test_type); + +static int sret_test_callback(void *ptr) { + sret_test_function_type f = (sret_test_function_type)(ptr); + sret_test_type x = {5436LL, 658277698LL, 43878957LL}; + sret_test_type r = f(x); + return ((r.a==x.a*35)&&(r.b==x.b*19)&&(r.c==x.c*21)) ? 0 : -1; +} + +static int sret_test(void) { + const char *src = + "typedef struct sret_test_type_s {long long a, b, c;} sret_test_type;\n" + "sret_test_type f(sret_test_type x) {\n" + " sret_test_type r = {x.a*35, x.b*19, x.c*21};\n" + " return r;\n" + "}\n"; + + return run_callback(src, sret_test_callback); +} + +/* + * one_member_union_test: + * + * In the x86-64 ABI a union should always be passed on the stack. However + * it appears that a single member union is treated by GCC as its member. + */ +typedef union one_member_union_test_type_u {int x;} one_member_union_test_type; +typedef one_member_union_test_type (*one_member_union_test_function_type) (one_member_union_test_type); + +static int one_member_union_test_callback(void *ptr) { + one_member_union_test_function_type f = (one_member_union_test_function_type)ptr; + one_member_union_test_type a, b; + a.x = 34; + b = f(a); + return (b.x == a.x*2) ? 0 : -1; +} + +static int one_member_union_test(void) { + const char *src = + "typedef union one_member_union_test_type_u {int x;} one_member_union_test_type;\n" + "one_member_union_test_type f(one_member_union_test_type a) {\n" + " one_member_union_test_type b;\n" + " b.x = a.x * 2;\n" + " return b;\n" + "}\n"; + return run_callback(src, one_member_union_test_callback); +} + +/* + * two_member_union_test: + * + * In the x86-64 ABI a union should always be passed on the stack. + */ +typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type; +typedef two_member_union_test_type (*two_member_union_test_function_type) (two_member_union_test_type); + +static int two_member_union_test_callback(void *ptr) { + two_member_union_test_function_type f = (two_member_union_test_function_type)ptr; + two_member_union_test_type a, b; + a.x = 34; + b = f(a); + return (b.x == a.x*2) ? 0 : -1; +} + +static int two_member_union_test(void) { + const char *src = + "typedef union two_member_union_test_type_u {int x; long y;} two_member_union_test_type;\n" + "two_member_union_test_type f(two_member_union_test_type a) {\n" + " two_member_union_test_type b;\n" + " b.x = a.x * 2;\n" + " return b;\n" + "}\n"; + return run_callback(src, two_member_union_test_callback); +} + +/* + * Win64 calling convention test. + */ + +typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type; +typedef many_struct_test_type (*many_struct_test_function_type) (many_struct_test_type,many_struct_test_type,many_struct_test_type,many_struct_test_type,many_struct_test_type,many_struct_test_type); + +static int many_struct_test_callback(void *ptr) { + many_struct_test_function_type f = (many_struct_test_function_type)ptr; + many_struct_test_type v = {1, 2, 3}; + many_struct_test_type r = f(v,v,v,v,v,v); + return ((r.a == 6) && (r.b == 12) && (r.c == 18))?0:-1; +} + +static int many_struct_test(void) { + const char *src = + "typedef struct many_struct_test_type_s {long long a, b, c;} many_struct_test_type;\n" + "many_struct_test_type f(many_struct_test_type x1, many_struct_test_type x2, many_struct_test_type x3, many_struct_test_type x4, many_struct_test_type x5, many_struct_test_type x6) {\n" + " many_struct_test_type y;\n" + " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n" + " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n" + " y.c = x1.c + x2.c + x3.c + x4.c + x5.c + x6.c;\n" + " return y;\n" + "}\n"; + return run_callback(src, many_struct_test_callback); +} + +/* + * Win64 calling convention test. + */ + +typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type; +typedef many_struct_test_2_type (*many_struct_test_2_function_type) (many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type,many_struct_test_2_type); + +static int many_struct_test_2_callback(void *ptr) { + many_struct_test_2_function_type f = (many_struct_test_2_function_type)ptr; + many_struct_test_2_type v = {1,2}; + many_struct_test_2_type r = f(v,v,v,v,v,v); + return ((r.a == 6) && (r.b == 12))?0:-1; +} + +static int many_struct_test_2(void) { + const char *src = + "typedef struct many_struct_test_2_type_s {int a, b;} many_struct_test_2_type;\n" + "many_struct_test_2_type f(many_struct_test_2_type x1, many_struct_test_2_type x2, many_struct_test_2_type x3, many_struct_test_2_type x4, many_struct_test_2_type x5, many_struct_test_2_type x6) {\n" + " many_struct_test_2_type y;\n" + " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n" + " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n" + " return y;\n" + "}\n"; + return run_callback(src, many_struct_test_2_callback); +} + +/* + * Win64 calling convention test. + */ + +typedef struct many_struct_test_3_type_s {int a, b;} many_struct_test_3_type; +typedef many_struct_test_3_type (*many_struct_test_3_function_type) (many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type,many_struct_test_3_type, ...); +typedef struct many_struct_test_3_struct_type { many_struct_test_3_function_type f; many_struct_test_3_function_type *f2; } many_struct_test_3_struct_type; + +static void many_struct_test_3_dummy(double d, ...) +{ + volatile double x = d; +} + +static int many_struct_test_3_callback(void *ptr) { + many_struct_test_3_struct_type s = { ptr, }; + many_struct_test_3_struct_type *s2 = &s; + s2->f2 = &s2->f; + many_struct_test_3_dummy(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, &s2); + many_struct_test_3_function_type f = *(s2->f2); + many_struct_test_3_type v = {1,2}; + many_struct_test_3_type r = (*((s2->f2=&f)+0))(v,v,v,v,v,v,1.0); + return ((r.a == 6) && (r.b == 12))?0:-1; +} + +static int many_struct_test_3(void) { + const char *src = + "typedef struct many_struct_test_3_type_s {int a, b;} many_struct_test_3_type;\n" + "many_struct_test_3_type f(many_struct_test_3_type x1, many_struct_test_3_type x2, many_struct_test_3_type x3, many_struct_test_3_type x4, many_struct_test_3_type x5, many_struct_test_3_type x6, ...) {\n" + " many_struct_test_3_type y;\n" + " y.a = x1.a + x2.a + x3.a + x4.a + x5.a + x6.a;\n" + " y.b = x1.b + x2.b + x3.b + x4.b + x5.b + x6.b;\n" + " return y;\n" + "}\n"; + return run_callback(src, many_struct_test_3_callback); +} + +/* + * stdarg_test: Test variable argument list ABI + */ + +typedef struct {long long a, b, c;} stdarg_test_struct_type; +typedef void (*stdarg_test_function_type) (int,int,int,...); + +static int stdarg_test_callback(void *ptr) { + stdarg_test_function_type f = (stdarg_test_function_type)ptr; + int x; + double y; + stdarg_test_struct_type z = {1, 2, 3}, w; + f(10, 10, 5, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, &x, + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, &y, + z, z, z, z, z, &w); + return ((x == 55) && (y == 55) && (w.a == 5) && (w.b == 10) && (w.c == 15)) ? 0 : -1; +} + +static int stdarg_test(void) { + const char *src = + "#include \n" + "typedef struct {long long a, b, c;} stdarg_test_struct_type;\n" + "void f(int n_int, int n_float, int n_struct, ...) {\n" + " int i, ti = 0;\n" + " double td = 0.0;\n" + " stdarg_test_struct_type ts = {0,0,0}, tmp;\n" + " va_list ap;\n" + " va_start(ap, n_struct);\n" + " for (i = 0, ti = 0; i < n_int; ++i)\n" + " ti += va_arg(ap, int);\n" + " *va_arg(ap, int*) = ti;\n" + " for (i = 0, td = 0; i < n_float; ++i)\n" + " td += va_arg(ap, double);\n" + " *va_arg(ap, double*) = td;\n" + " for (i = 0; i < n_struct; ++i) {\n" + " tmp = va_arg(ap, stdarg_test_struct_type);\n" + " ts.a += tmp.a; ts.b += tmp.b; ts.c += tmp.c;" + " }\n" + " *va_arg(ap, stdarg_test_struct_type*) = ts;\n" + " va_end(ap);" + "}\n"; + return run_callback(src, stdarg_test_callback); +} + +typedef struct {long long a, b;} stdarg_many_test_struct_type; +typedef void (*stdarg_many_test_function_type) (int, int, int, int, int, + stdarg_many_test_struct_type, + int, int, ...); + +static int stdarg_many_test_callback(void *ptr) +{ + stdarg_many_test_function_type f = (stdarg_many_test_function_type)ptr; + int x; + stdarg_many_test_struct_type l = {10, 11}; + f(1, 2, 3, 4, 5, l, 6, 7, &x, 44); + return x == 44 ? 0 : -1; +} + +static int stdarg_many_test(void) +{ + const char *src = + "#include \n" + "typedef struct {long long a, b;} stdarg_many_test_struct_type;\n" + "void f (int a, int b, int c, int d, int e, stdarg_many_test_struct_type l, int f, int g, ...){\n" + " va_list ap;\n" + " int *p;\n" + " va_start (ap, g);\n" + " p = va_arg(ap, int*);\n" + " *p = va_arg(ap, int);\n" + " va_end (ap);\n" + "}\n"; + return run_callback(src, stdarg_many_test_callback); +} + +/* + * Test Win32 stdarg handling, since the calling convention will pass a pointer + * to the struct and the stdarg pointer must point to that pointer initially. + */ + +typedef struct {long long a, b, c;} stdarg_struct_test_struct_type; +typedef int (*stdarg_struct_test_function_type) (stdarg_struct_test_struct_type a, ...); + +static int stdarg_struct_test_callback(void *ptr) { + stdarg_struct_test_function_type f = (stdarg_struct_test_function_type)ptr; + stdarg_struct_test_struct_type v = {10, 35, 99}; + int x = f(v, 234); + return (x == 378) ? 0 : -1; +} + +static int stdarg_struct_test(void) { + const char *src = + "#include \n" + "typedef struct {long long a, b, c;} stdarg_struct_test_struct_type;\n" + "int f(stdarg_struct_test_struct_type a, ...) {\n" + " va_list ap;\n" + " va_start(ap, a);\n" + " int z = va_arg(ap, int);\n" + " va_end(ap);\n" + " return z + a.a + a.b + a.c;\n" + "}\n"; + return run_callback(src, stdarg_struct_test_callback); +} + +/* Test that x86-64 arranges the stack correctly for arguments with alignment >8 bytes */ + +typedef LONG_DOUBLE (*arg_align_test_callback_type) (LONG_DOUBLE,int,LONG_DOUBLE,int,LONG_DOUBLE); + +static int arg_align_test_callback(void *ptr) { + arg_align_test_callback_type f = (arg_align_test_callback_type)ptr; + long double x = f(12, 0, 25, 0, 37); + return (x == 74) ? 0 : -1; +} + +static int arg_align_test(void) { + const char *src = + "long double f(long double a, int b, long double c, int d, long double e) {\n" + " return a + c + e;\n" + "}\n"; + return run_callback(src, arg_align_test_callback); +} + +#define RUN_TEST(t) \ + if (!testname || (strcmp(#t, testname) == 0)) { \ + fputs(#t "... ", stdout); \ + fflush(stdout); \ + if (t() == 0) { \ + fputs("success\n", stdout); \ + } else { \ + fputs("failure\n", stdout); \ + retval = EXIT_FAILURE; \ + } \ + } + +int main(int argc, char **argv) { + int i; + const char *testname = NULL; + int retval = EXIT_SUCCESS; + + /* if tcclib.h and libtcc1.a are not installed, where can we find them */ + for (i = 1; i < argc; ++i) { + if (!memcmp(argv[i], "run_test=", 9)) + testname = argv[i] + 9; + } + + g_argv = argv, g_argc = argc; + + RUN_TEST(ret_int_test); + RUN_TEST(ret_longlong_test); + RUN_TEST(ret_float_test); + RUN_TEST(ret_double_test); + RUN_TEST(ret_longdouble_test); + RUN_TEST(ret_2float_test); + RUN_TEST(ret_2double_test); + RUN_TEST(ret_8plus2double_test); + RUN_TEST(ret_6plus2longlong_test); +#if !defined __x86_64__ || defined _WIN32 + /* currently broken on x86_64 linux */ + RUN_TEST(ret_mixed_test); + RUN_TEST(ret_mixed2_test); +#endif + RUN_TEST(ret_mixed3_test); + RUN_TEST(reg_pack_test); + RUN_TEST(reg_pack_longlong_test); + RUN_TEST(sret_test); + RUN_TEST(one_member_union_test); + RUN_TEST(two_member_union_test); + RUN_TEST(many_struct_test); + RUN_TEST(many_struct_test_2); + RUN_TEST(many_struct_test_3); + RUN_TEST(stdarg_test); + RUN_TEST(stdarg_many_test); + RUN_TEST(stdarg_struct_test); + RUN_TEST(arg_align_test); + return retval; +} diff --git a/05/tcc-final-old/tests/asm-c-connect-1.c b/05/tcc-final-old/tests/asm-c-connect-1.c new file mode 100644 index 0000000..8a28d78 --- /dev/null +++ b/05/tcc-final-old/tests/asm-c-connect-1.c @@ -0,0 +1,57 @@ +#include + +#if defined _WIN32 && !defined __TINYC__ +# define _ "_" +#else +# define _ +#endif + +static int x1_c(void) +{ + printf(" x1"); + return 1; +} + +asm(".text;"_"x1: call "_"x1_c; ret"); + +void callx4(void); +void callx5_again(void); + +void x6() +{ + printf(" x6-1"); +} + +int main(int argc, char *argv[]) +{ + printf("*"); + asm("call "_"x1"); + asm("call "_"x2"); + asm("call "_"x3"); + callx4(); + asm("call "_"x5"); + callx5_again(); + x6(); + printf(" *\n"); + return 0; +} + +static +int x2(void) +{ + printf(" x2"); + return 2; +} + +extern int x3(void); + +void x4(void) +{ + printf(" x4"); +} + +void x5(void); +void x5(void) +{ + printf(" x5"); +} diff --git a/05/tcc-final-old/tests/asm-c-connect-2.c b/05/tcc-final-old/tests/asm-c-connect-2.c new file mode 100644 index 0000000..3440b40 --- /dev/null +++ b/05/tcc-final-old/tests/asm-c-connect-2.c @@ -0,0 +1,36 @@ +#include + +#if defined _WIN32 && !defined __TINYC__ +# define _ "_" +#else +# define _ +#endif + +int x3(void) +{ + printf(" x3"); + return 3; +} + +/* That callx4 is defined globally (as if ".globl callx4") + is a TCC extension. GCC doesn't behave like this. */ +void callx4(void); +__asm__(_"callx4: call "_"x4; ret;" +#ifndef __TINYC__ + " .global "_"callx4" +#endif +); + +extern void x5(void); + +void callx5_again(void); +void callx5_again(void) +{ + x5(); + asm("call "_"x6"); +} + +static void x6() +{ + printf(" x6-2"); +} diff --git a/05/tcc-final-old/tests/asmtest.S b/05/tcc-final-old/tests/asmtest.S new file mode 100644 index 0000000..e9c0e32 --- /dev/null +++ b/05/tcc-final-old/tests/asmtest.S @@ -0,0 +1,978 @@ +# gas comment with ``gnu'' style quotes + +/* some directive tests */ + + .byte 0xff + .byte 1, 2, 3 + .short 1, 2, 3 + .word 1, 2, 3 + .long 1, 2, 3 + .int 1, 2, 3 + .align 8 + .byte 1 +/* .align 16, 0x90 gas is too clever for us with 0x90 fill */ + .balign 4, 0x92 + .align 16, 0x91 /* 0x91 tests the non-clever behaviour */ + .skip 3 + .skip 15, 0x90 + .string "hello\0world" +/* Macro expansion should work like with C, the #n shouldn't be parsed + as asm line comment */ +#define __stringify(n) #n +#define stringify(n) __stringify(n) + .skip 8,0x90 + .asciz stringify(BLA) + .skip 8,0x90 + +# 28 "asmtest.S" # a line directive (and a line comment) + movl %eax, %ebx # some more asm comment +/* some label tests */ +L1: + movl %eax, %ebx + mov 0x10000, %eax +L2: + movl $L2 - L1, %ecx +var1: + nop ; nop ; nop ; nop + + mov var1, %eax + +/* instruction tests */ +movl %eax, %ebx +mov 0x10000, %eax +mov 0x10000, %ax +mov 0x10000, %al +mov %al, 0x10000 + +mov $1, %edx +mov $1, %dx +mov $1, %cl +movb $2, 0x100(%ebx,%edx,2) +movw $2, 0x100(%ebx,%edx,2) +movl $2, 0x100(%ebx,%edx,2) +movl %eax, 0x100(%ebx,%edx,2) +movl 0x100(%ebx,%edx,2), %edx +movw %ax, 0x100(%ebx,%edx,2) + +movw $0x1122,%si +movl $0x112233,%edx +movl $0x80000000, %esi +movl $-0x7fffffff, %edi +#ifdef __x86_64__ +mov $0x11223344,%rbx +movq $0x11223344,%rbx +mov $0x1122334455,%rbx +movq $0x1122334455,%rbx +movl $0x11334455,(%rbx) +#endif + +mov %eax, 0x12(,%edx,2) + +#ifdef __i386__ +mov %cr3, %edx +mov %ecx, %cr3 +movl %cr3, %eax +movl %tr3, %eax +movl %db3, %ebx +movl %dr6, %eax +#else +mov %cr3, %rdx +mov %rcx, %cr3 +movq %cr3, %rax +movq %db3, %rbx +movq %dr6, %rax +mov %cr8, %rsi +mov %rdi, %cr8 +#endif +movl %fs, %ecx +movl %ebx, %fs + +#ifdef __x86_64__ +movq %r8, %r9 +movq %r10, %r11 +movq %r12, %r13 +movq %r14, %r15 +movq %rax, %r9 +movq %r15, %rsi +inc %r9b +dec %r10w +not %r11d +negq %r12 +decb %r13b +incw %r14w +notl %r15d +#endif + + movsbl 0x1000, %eax + movsbw 0x1000, %ax + movswl 0x1000, %eax + + movzbl 0x1000, %eax + movzbw 0x1000, %ax + movzwl 0x1000, %eax + + movzb 0x1000, %eax + movzb 0x1000, %ax + + mov $0x12345678,%eax + +#ifdef __x86_64__ + movzb 0x1000, %rax + movzbq 0x1000, %rbx + movsbq 0x1000, %rdx + movzwq 0x1000, %rdi + movswq 0x1000, %rdx + movslq %eax, %rcx + mov $0x12345678,%rax + mov $0x12345678,%rdx + mov $0x12345678,%r10 + mov $0x123456789abcdef0,%rax + mov $0x123456789abcdef0,%rcx + mov $0x123456789abcdef0,%r11 +#endif + +#ifdef __i386__ + pushl %eax + push %eax + push %cs +#else + pushq %rax + push %rax +#endif + pushw %ax + push %gs + push $1 + push $100 + push 0x42(%eax) + pop 0x43(%esi) + +#ifdef __i386__ + popl %eax + pop %eax + pop %ds +#else + popq %rax + pop %rax +#endif + popw %ax + pop %fs + + xchg %eax, %ecx + xchg %edx, %eax + xchg %bx, 0x10000 + xchg 0x10000, %ebx + xchg 0x10000, %dl + + in $100, %al + in $100, %ax + in $100, %eax + in %dx, %al + in %dx, %ax + in %dx, %eax + inb %dx + inw %dx + inl %dx + + out %al, $100 + out %ax, $100 + out %eax, $100 + + /* NOTE: gas is bugged here, so size must be added */ + outb %al, %dx + outw %ax, %dx + outl %eax, %dx + + leal 0x1000(%ebx), %ecx + lea 0x1000(%ebx), %ecx + +#ifdef __i386__ + les 0x2000, %eax + lds 0x2000, %ebx + lss 0x2000, %edx +#endif + lfs 0x2000, %ecx + lgs 0x2000, %edx + +addl $0x123, %eax +add $0x123, %ebx +add $-16, %ecx +add $-0x123, %esi +add $1, %bx +add $1, %ebx +add $-1, %bx +add $-1, %ebx +add $127, %bx +addl $127, %ebx +addl $-128, %ebx +addl $-128, %ebx +addl $-129, %ebx +addl $128, %ebx +addl $255, %ebx +addl $256, %ebx +andb $0xf, %ah +andb $-15, %cl +xorb $127, %dh +cmpb $42, (%eax) +addl $0x123, 0x100 +addl $0x123, 0x100(%ebx) +addl $0x123, 0x100(%ebx,%edx,2) +addl $0x123, 0x100(%esp) +addl $0x123, (3*8)(%esp) +addl $0x123, (%ebp) +addl $0x123, (%esp) +cmpl $0x123, (%esp) + +#ifdef __x86_64__ +xor %bl,%ah +xor %bl,%r8b +xor %r9b,%bl +xor %sil,%cl +add %eax,(%r8d) +add %ebx,(%r9) +add %edx,(%r10d,%r11d) +add %ecx,(%r12,%r13) +add %esi,(%r14,%r15,4) +add %edi,0x1000(%rbx,%r12,8) +add %r11,0x1000(%ebp,%r9d,8) +movb $12, %ah +movb $13, %bpl +movb $14, %dil +movb $15, %r12b +#endif + +add %eax, (%ebx) +add (%ebx), %eax + +or %dx, (%ebx) +or (%ebx), %si + +add %cl, (%ebx) +add (%ebx), %dl + + inc %edx + incl 0x10000 + incb 0x10000 + dec %dx + + test $1, %al + test $1, %cl + + testl $1, 0x1000 + testb $1, 0x1000 + testw $1, 0x1000 + test %eax, %ebx + test %eax, 0x1000 + test 0x1000, %edx + + not %edx + notw 0x10000 + notl 0x10000 + notb 0x10000 + + neg %edx + negw 0x10000 + negl 0x10000 + negb 0x10000 + + imul %ecx + mul %edx + mulb %cl + + imul %eax, %ecx + imul 0x1000, %cx + imul $10, %eax, %ecx + imul $10, %ax, %cx + imul $10, %eax + imul $0x1100000, %eax + imul $1, %eax + + idivw 0x1000 + div %ecx + div %bl + div %ecx, %eax + +and $15,%bx +and $-20,%edx + +shl %edx +shl $10, %edx +shl %cl, %edx + +shld $1, %eax, %edx +shld %cl, %eax, %edx +shld %eax, %edx + +shrd $1, %eax, %edx +shrd %cl, %eax, %edx +shrd %eax, %edx + +L4: +call 0x1000 +call L4 +#ifdef __i386__ +call *%eax +#else +call *%rax +#endif +call *0x1000 +call func1 + +.global L5,L6 + +L5: +L6: + +#ifdef __i386__ +lcall $0x100, $0x1000 +#else +lcall *0x100 +lcall *(%rax) +#endif + +jmp 0x1000 +jmp *(%edi) +#ifdef __i386__ +jmp *%eax +#else +jmp *%rax +#endif +jmp *0x1000 + +#ifdef __i386__ +ljmp $0x100, $0x1000 +#else +ljmp *0x100 +ljmp *(%rdi) +ljmpl *(%esi) +ljmpw *(%esi) +#endif + +ret +ret $10 +#ifdef __i386__ +retl +retl $10 +#else +retq +retq $10 +#endif + +lret + +lret $10 + +enter $1234, $10 + +L3: + jo 0x1000 + jnp 0x1001 + jne 0x1002 + jg 0x1003 + + jo L3 + jnp L3 + jne L3 + jg L3 + + loopne L3 + loopnz L3 + loope L3 + loopz L3 + loop L3 + jecxz L3 + + + seto %al + setc %al + setcb %al + setnp 0x1000 + setl 0xaaaa + setg %dl + + fadd + fadd %st(1), %st + fadd %st(0), %st(1) + fadd %st(3) + + fmul %st(0),%st(0) + fmul %st(0),%st(1) + + faddp %st(5) + faddp + faddp %st(1), %st + + fadds 0x1000 + fiadds 0x1002 + faddl 0x1004 + fiaddl 0x1006 + + fmul + fmul %st(1), %st + fmul %st(3) + + fmulp %st(5) + fmulp + fmulp %st(1), %st + + fmuls 0x1000 + fimuls 0x1002 + fmull 0x1004 + fimull 0x1006 + + fsub + fsub %st(1), %st + fsub %st(3) + + fsubp %st(5) + fsubp + fsubp %st(1), %st + + fsubs 0x1000 + fisubs 0x1002 + fsubl 0x1004 + fisubl 0x1006 + + fsubr + fsubr %st(1), %st + fsubr %st(3) + + fsubrp %st(5) + fsubrp + fsubrp %st(1), %st + + fsubrs 0x1000 + fisubrs 0x1002 + fsubrl 0x1004 + fisubrl 0x1006 + + fdiv + fdiv %st(1), %st + fdiv %st(3) + + fdivp %st(5) + fdivp + fdivp %st(1), %st + + fdivs 0x1000 + fidivs 0x1002 + fdivl 0x1004 + fidivl 0x1006 + + fcom %st(3) + + fcoms 0x1000 + ficoms 0x1002 + fcoml 0x1004 + ficoml 0x1006 + + fcomp %st(5) + fcomp + fcompp + + fcomps 0x1000 + ficomps 0x1002 + fcompl 0x1004 + ficompl 0x1006 + + fld %st(5) + fldl 0x1000 + flds 0x1002 + fildl 0x1004 + fst %st(4) + fstp %st(6) + fstpt 0x1006 + fbstp 0x1008 + + fxch + fxch %st(4) + + fucom %st(6) + fucomp %st(3) + fucompp + + finit + fninit + fldcw 0x1000 + fnstcw 0x1002 + fstcw 0x1002 + fnstsw 0x1004 + fnstsw (%eax) + fstsw 0x1004 + fstsw (%eax) + fnclex + fclex + fnstenv 0x1000 + fstenv 0x1000 + fldenv 0x1000 + fnsave 0x1002 + fsave 0x1000 + frstor 0x1000 + ffree %st(7) + ffreep %st(6) + + 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 + fchs + fabs + fnop + fwait + +bswap %edx +bswapl %ecx +xadd %ecx, %edx +xaddb %dl, 0x1000 +xaddw %ax, 0x1000 +xaddl %eax, 0x1000 +cmpxchg %ecx, %edx +cmpxchgb %dl, 0x1000 +cmpxchgw %ax, 0x1000 +cmpxchgl %eax, 0x1000 +invlpg 0x1000 +cmpxchg8b 0x1002 +#ifdef __x86_64__ +cmpxchg16b (%rax) +cmpxchg16b (%r10,%r11) +#endif + +fcmovb %st(5), %st +fcmove %st(5), %st +fcmovbe %st(5), %st +fcmovu %st(5), %st +fcmovnb %st(5), %st +fcmovne %st(5), %st +fcmovnbe %st(5), %st +fcmovnu %st(5), %st +fcomi %st(5), %st +fucomi %st(5), %st +fcomip %st(5), %st +fucomip %st(5), %st + + + + cmovo 0x1000, %eax + cmovs 0x1000, %eax + cmovns %edx, %edi + cmovne %ax, %si + cmovbw %ax, %di + cmovnbel %edx, %ecx +#ifdef __x86_64__ + bswapq %rsi + bswapq %r10 + cmovz %rdi,%rbx + cmovpeq %rsi, %rdx +#endif + +int $3 +int $0x10 + +#ifdef __i386__ + pusha + popa +#endif + clc # another comment + cld # a comment with embedded ' tick + cli + clts + cmc + lahf + sahf +#ifdef __i386__ + pushfl + popfl +#else + pushfq + popfq +#endif + pushf + popf + stc + std + sti +#ifdef __i386__ + aaa + aas + daa + das + aad + aam + into +#endif + cbw + cwd + cwde + cdq + cbtw + cwtd + cwtl + cltd + leave + int3 + iret + rsm + hlt + wait + nop + + /* XXX: handle prefixes */ +#if 0 + aword + addr16 +#endif + lock + rep + repe + repz + repne + repnz + nop + + lock ;negl (%eax) + wait ;pushf + rep ;stosb + repe ;lodsb + repz ;cmpsb + repne;movsb + repnz;outsb + + /* handle one-line prefix + ops */ + lock negl (%eax) + wait pushf + rep stosb + repe lodsb + repz cmpsb + repne movsb + repnz outsb + + invd + wbinvd + cpuid + wrmsr + rdtsc + rdmsr + rdpmc + ud2 +#ifdef __x86_64__ + syscall + sysret + sysretq + lfence + mfence + sfence + prefetchnta 0x18(%rdx) + prefetcht0 (%rcx) + prefetcht1 (%rsi) + prefetcht2 (%rdi) + prefetchw (%rdi) + clflush 0x1000(%rax,%rcx) + fxsaveq (%rdx) + fxsaveq (%r11) + fxrstorq (%rcx) + fxrstorq (%r10) + +#endif + + lar %ax,%dx + lar %eax,%dx + lar %ax,%edx + lar %eax,%edx +#ifdef __x86_64__ + lar %ax,%rdx + lar %eax,%rdx +#endif + emms + movd %edx, %mm3 + movd 0x1000, %mm2 + movd %mm4, %ecx + movd %mm5, 0x1000 + + movq 0x1000, %mm2 + movq %mm4, 0x1000 + + pand 0x1000, %mm3 + pand %mm4, %mm5 + + psllw $1, %mm6 + psllw 0x1000, %mm7 + psllw %mm2, %mm7 + + xlat + cmpsb + scmpw + insl + outsw + lodsb + slodl + movsb + movsl + smovb + scasb + sscaw + stosw + sstol + + bsf 0x1000, %ebx + bsr 0x1000, %ebx + bt %edx, 0x1000 + btl $2, 0x1000 + btc %edx, 0x1000 + btcl $2, 0x1000 + btr %edx, 0x1000 + btrl $2, 0x1000 + bts %edx, 0x1000 + btsl $2, 0x1000 + + + +#ifdef __i386__ + boundl %edx, 0x10000 + boundw %bx, 0x1000 + + arpl %bx, 0x1000 +#endif + lar 0x1000, %eax + lgdt 0x1000 + lidt 0x1000 + lldt 0x1000 + sgdt 0x1000 + sidt 0x1000 + sldt 0x1000 +#ifdef __x86_64__ + lgdtq 0x1000 + lidtq 0x1000 + sgdtq 0x1000 + sidtq 0x1000 + + swapgs + + str %rdx + str %r9 +#endif + + lmsw 0x1000 + lsl 0x1000, %ecx + ltr 0x1000 + ltr %si + smsw 0x1000 + str 0x1000 + str %ecx + str %dx + + verr 0x1000 + verw 0x1000 + +#ifdef __i386__ + push %ds + pushw %ds + pushl %ds + pop %ds + popw %ds + popl %ds +#endif + fxsave 1(%ebx) + fxrstor 1(%ecx) +#ifdef __i386__ + pushl $1 +#else + pushq $1 +#endif + pushw $1 + push $1 + +#ifdef __ASSEMBLER__ // should be defined, for S files + inc %eax +#endif + +#ifndef _WIN32 +ft1: ft2: ft3: ft4: ft5: ft6: ft7: ft8: ft9: + xor %eax, %eax + ret + +.type ft1,STT_FUNC +.type ft2,@STT_FUNC +.type ft3,%STT_FUNC +.type ft4,"STT_FUNC" +.type ft5,function +.type ft6,@function +.type ft7,%function +.type ft8,"function" +#endif + + pause +.rept 6 + nop +.endr +.fill 4,1,0x90 + +.section .text.one,"ax" +nop +.previous +.pushsection .text.one,"ax" +nop +.pushsection .text.two,"ax" +nop +.popsection +.popsection + +1: ud2 +.pushsection __bug_table,"a" +.align 8 +2: .long 1b - 2b + .long 0x600000 - 2b + .long 1b + 42 + .long 43 + 1b + .long 2b + 144 + .long 145 + 2b + .word 164, 0 + .org 2b+32 +#ifdef __x86_64__ + .quad 1b +#else + .long 1b +#endif +.popsection +3: mov %eax,%ecx +4: +.pushsection .text.three, "ax" +nop +.skip (-((4b-3b) > 0) * 2) , 0x90 +.popsection + +.globl overrideme +.weak overrideme + nop +.globl notimplemented +notimplemented: + ret +.set overrideme, notimplemented +overrideme = notimplemented +overrideme: + ret + + movd %esi, %mm1 + movd %edi, %xmm2 + movd (%ebx), %mm3 + movd (%ebx), %xmm3 + movd %mm1, %esi + movd %xmm2, %edi + movd %mm3, (%edx) + movd %xmm3, (%edx) +#ifdef __x86_64__ + movd %rsi, %mm1 + movd %rdi, %xmm2 + movd (%rbx), %mm3 + movd (%rbx), %xmm3 + movd %mm1, %r12 + movd %xmm2, %rdi + movd %mm3, (%r8) + movd %xmm3, (%r13) +#endif + + movq (%ebp), %mm1 + movq %mm2, (%edi) + movq (%edi), %xmm3 + movq %mm4, %mm5 +#ifdef __x86_64__ + movq %rcx, %mm1 + movq %rdx, %xmm2 + movq %r13, %xmm3 + /* movq mem64->xmm is encoded as f30f7e by GAS, but as + 660f6e by tcc (which really is a movd and would need + a REX.W prefix to be movq). */ + movq (%rsi), %xmm3 + movq %mm1, %rdx + movq %xmm3, %rcx + movq %xmm4, (%rsi) +#endif + +#define TEST_MMX_SSE(insn) \ + insn %mm1, %mm2; \ + insn %xmm2, %xmm3; \ + insn (%ebx), %xmm3; +#define TEST_MMX_SSE_I8(insn) \ + TEST_MMX_SSE(insn) \ + insn $0x42, %mm4; \ + insn $0x42, %xmm4; + + TEST_MMX_SSE(packssdw) + TEST_MMX_SSE(packsswb) + TEST_MMX_SSE(packuswb) + TEST_MMX_SSE(paddb) + TEST_MMX_SSE(paddw) + TEST_MMX_SSE(paddd) + TEST_MMX_SSE(paddsb) + TEST_MMX_SSE(paddsw) + TEST_MMX_SSE(paddusb) + TEST_MMX_SSE(paddusw) + TEST_MMX_SSE(pand) + TEST_MMX_SSE(pandn) + TEST_MMX_SSE(pcmpeqb) + TEST_MMX_SSE(pcmpeqw) + TEST_MMX_SSE(pcmpeqd) + TEST_MMX_SSE(pcmpgtb) + TEST_MMX_SSE(pcmpgtw) + TEST_MMX_SSE(pcmpgtd) + TEST_MMX_SSE(pmaddwd) + TEST_MMX_SSE(pmulhw) + TEST_MMX_SSE(pmullw) + TEST_MMX_SSE(por) + TEST_MMX_SSE(psllw) +TEST_MMX_SSE_I8(psllw) + TEST_MMX_SSE(pslld) +TEST_MMX_SSE_I8(pslld) + TEST_MMX_SSE(psllq) +TEST_MMX_SSE_I8(psllq) + TEST_MMX_SSE(psraw) +TEST_MMX_SSE_I8(psraw) + TEST_MMX_SSE(psrad) +TEST_MMX_SSE_I8(psrad) + TEST_MMX_SSE(psrlw) +TEST_MMX_SSE_I8(psrlw) + TEST_MMX_SSE(psrld) +TEST_MMX_SSE_I8(psrld) + TEST_MMX_SSE(psrlq) +TEST_MMX_SSE_I8(psrlq) + TEST_MMX_SSE(psubb) + TEST_MMX_SSE(psubw) + TEST_MMX_SSE(psubd) + TEST_MMX_SSE(psubsb) + TEST_MMX_SSE(psubsw) + TEST_MMX_SSE(psubusb) + TEST_MMX_SSE(psubusw) + TEST_MMX_SSE(punpckhbw) + TEST_MMX_SSE(punpckhwd) + TEST_MMX_SSE(punpckhdq) + TEST_MMX_SSE(punpcklbw) + TEST_MMX_SSE(punpcklwd) + TEST_MMX_SSE(punpckldq) + TEST_MMX_SSE(pxor) + + cvtpi2ps %mm1, %xmm2 + cvtpi2ps (%ebx), %xmm2 + TEST_MMX_SSE(pmaxsw) + TEST_MMX_SSE(pmaxub) + TEST_MMX_SSE(pminsw) + TEST_MMX_SSE(pminub) diff --git a/05/tcc-final-old/tests/boundtest.c b/05/tcc-final-old/tests/boundtest.c new file mode 100644 index 0000000..15bffb4 --- /dev/null +++ b/05/tcc-final-old/tests/boundtest.c @@ -0,0 +1,285 @@ +#include +#include +#include + +#define NB_ITS 1000000 +//#define NB_ITS 1 +#define TAB_SIZE 100 + +int tab[TAB_SIZE]; +int ret_sum; +char tab3[256]; + +int test1(void) +{ + int i, sum = 0; + for(i=0;i= 2) + index = atoi(argv[1]) - 1; + + if ((index < 0) || (index >= index_max)) { + printf("N is outside of the valid range (%d)\n", index); + exit(2); + } + + /* well, we also use bounds on this ! */ + ftest = table_test[index]; + ftest(); + + return 0; +} + +/* + * without bound 0.77 s + * with bounds 4.73 + */ diff --git a/05/tcc-final-old/tests/gcctestsuite.sh b/05/tcc-final-old/tests/gcctestsuite.sh new file mode 100755 index 0000000..f3cc538 --- /dev/null +++ b/05/tcc-final-old/tests/gcctestsuite.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +TESTSUITE_PATH=$HOME/gcc/gcc-3.2/gcc/testsuite/gcc.c-torture +TCC="./tcc -B. -I. -DNO_TRAMPOLINES" +rm -f tcc.sum tcc.log +nb_failed="0" + +for src in $TESTSUITE_PATH/compile/*.c ; do + echo $TCC -o /tmp/test.o -c $src + $TCC -o /tmp/test.o -c $src >> tcc.log 2>&1 + if [ "$?" = "0" ] ; then + result="PASS" + else + result="FAIL" + nb_failed=$(( $nb_failed + 1 )) + fi + echo "$result: $src" >> tcc.sum +done + +for src in $TESTSUITE_PATH/execute/*.c ; do + echo $TCC $src + $TCC $src >> tcc.log 2>&1 + if [ "$?" = "0" ] ; then + result="PASS" + else + result="FAIL" + nb_failed=$(( $nb_failed + 1 )) + fi + echo "$result: $src" >> tcc.sum +done + +echo "$nb_failed test(s) failed." >> tcc.sum +echo "$nb_failed test(s) failed." diff --git a/05/tcc-final-old/tests/libtcc_test.c b/05/tcc-final-old/tests/libtcc_test.c new file mode 100644 index 0000000..480d314 --- /dev/null +++ b/05/tcc-final-old/tests/libtcc_test.c @@ -0,0 +1,96 @@ +/* + * Simple Test program for libtcc + * + * libtcc can be useful to use tcc as a "backend" for a code generator. + */ +#include +#include +#include + +#include "libtcc.h" + +/* this function is called by the generated code */ +int add(int a, int b) +{ + return a + b; +} + +/* this strinc is referenced by the generated code */ +const char hello[] = "Hello World!"; + +char my_program[] = +"#include \n" /* include the "Simple libc header for TCC" */ +"extern int add(int a, int b);\n" +"#ifdef _WIN32\n" /* dynamically linked data needs 'dllimport' */ +" __attribute__((dllimport))\n" +"#endif\n" +"extern const char hello[];\n" +"int fib(int n)\n" +"{\n" +" if (n <= 2)\n" +" return 1;\n" +" else\n" +" return fib(n-1) + fib(n-2);\n" +"}\n" +"\n" +"int foo(int n)\n" +"{\n" +" printf(\"%s\\n\", hello);\n" +" printf(\"fib(%d) = %d\\n\", n, fib(n));\n" +" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n" +" return 0;\n" +"}\n"; + +int main(int argc, char **argv) +{ + TCCState *s; + int i; + int (*func)(int); + + s = tcc_new(); + if (!s) { + fprintf(stderr, "Could not create tcc state\n"); + exit(1); + } + + /* if tcclib.h and libtcc1.a are not installed, where can we find them */ + for (i = 1; i < argc; ++i) { + char *a = argv[i]; + if (a[0] == '-') { + if (a[1] == 'B') + tcc_set_lib_path(s, a+2); + else if (a[1] == 'I') + tcc_add_include_path(s, a+2); + else if (a[1] == 'L') + tcc_add_library_path(s, a+2); + } + } + + /* MUST BE CALLED before any compilation */ + tcc_set_output_type(s, TCC_OUTPUT_MEMORY); + + if (tcc_compile_string(s, my_program) == -1) + return 1; + + /* as a test, we add symbols that the compiled program can use. + You may also open a dll with tcc_add_dll() and use symbols from that */ + tcc_add_symbol(s, "add", add); + tcc_add_symbol(s, "hello", hello); + + /* relocate the code */ + if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0) + return 1; + + /* get entry symbol */ + func = tcc_get_symbol(s, "foo"); + if (!func) + return 1; + + /* run the code */ + func(32); + + /* delete the state */ + tcc_delete(s); + + return 0; +} diff --git a/05/tcc-final-old/tests/pp/01.c b/05/tcc-final-old/tests/pp/01.c new file mode 100644 index 0000000..2fc3d79 --- /dev/null +++ b/05/tcc-final-old/tests/pp/01.c @@ -0,0 +1,6 @@ +#define hash_hash # ## # +#define mkstr(a) # a +#define in_between(a) mkstr(a) +#define join(c, d) in_between(c hash_hash d) +char p[] = join(x, y); +// char p[] = "x ## y"; diff --git a/05/tcc-final-old/tests/pp/01.expect b/05/tcc-final-old/tests/pp/01.expect new file mode 100644 index 0000000..cf5b153 --- /dev/null +++ b/05/tcc-final-old/tests/pp/01.expect @@ -0,0 +1 @@ +char p[] = "x ## y"; diff --git a/05/tcc-final-old/tests/pp/02.c b/05/tcc-final-old/tests/pp/02.c new file mode 100644 index 0000000..feb1254 --- /dev/null +++ b/05/tcc-final-old/tests/pp/02.c @@ -0,0 +1,28 @@ +#define x 3 +#define f(a) f(x * (a)) +#undef x +#define x 2 +#define g f +#define z z[0] +#define h g(~ +#define m(a) a(w) +#define w 0,1 +#define t(a) a +#define p() int +#define q(x) x +#define r(x,y) x ## y +#define str(x) # x +f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); +g(x+(3,4)-w) | h 5) & m +(f)^m(m); +char c[2][6] = { str(hello), str() }; +/* + * f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); + * f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); + * char c[2][6] = { "hello", "" }; + */ +#define L21 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); +#define L22 g(x+(3,4)-w) | h 5) & m\ +(f)^m(m); +L21 +L22 diff --git a/05/tcc-final-old/tests/pp/02.expect b/05/tcc-final-old/tests/pp/02.expect new file mode 100644 index 0000000..8ae2eb9 --- /dev/null +++ b/05/tcc-final-old/tests/pp/02.expect @@ -0,0 +1,5 @@ +f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); +f(2 * (2 +(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); +char c[2][6] = { "hello", "" }; +f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); +f(2 * (2 +(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); diff --git a/05/tcc-final-old/tests/pp/03.c b/05/tcc-final-old/tests/pp/03.c new file mode 100644 index 0000000..a659245 --- /dev/null +++ b/05/tcc-final-old/tests/pp/03.c @@ -0,0 +1,15 @@ +#define str(s) # s +#define xstr(s) str(s) +#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \ + x ## s, x ## t) +#define INCFILE(n) vers ## n +#define glue(a, b) a ## b +#define xglue(a, b) glue(a, b) +#define HIGHLOW "hello" +#define LOW LOW ", world" +debug(1, 2); +fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away + == 0) str(: @\n), s); +\#include xstr(INCFILE(2).h) +glue(HIGH, LOW); +xglue(HIGH, LOW) diff --git a/05/tcc-final-old/tests/pp/03.expect b/05/tcc-final-old/tests/pp/03.expect new file mode 100644 index 0000000..44aad0a --- /dev/null +++ b/05/tcc-final-old/tests/pp/03.expect @@ -0,0 +1,5 @@ +printf("x" "1" "= %d, x" "2" "= %s", x1, x2); +fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s); +\#include "vers2.h" +"hello"; +"hello" ", world" diff --git a/05/tcc-final-old/tests/pp/04.c b/05/tcc-final-old/tests/pp/04.c new file mode 100644 index 0000000..0068f37 --- /dev/null +++ b/05/tcc-final-old/tests/pp/04.c @@ -0,0 +1,4 @@ +#define foobar 1 +#define C(x,y) x##y +#define D(x) (C(x,bar)) +D(foo) diff --git a/05/tcc-final-old/tests/pp/04.expect b/05/tcc-final-old/tests/pp/04.expect new file mode 100644 index 0000000..7c67b01 --- /dev/null +++ b/05/tcc-final-old/tests/pp/04.expect @@ -0,0 +1 @@ +(1) diff --git a/05/tcc-final-old/tests/pp/05.c b/05/tcc-final-old/tests/pp/05.c new file mode 100644 index 0000000..7274941 --- /dev/null +++ b/05/tcc-final-old/tests/pp/05.c @@ -0,0 +1,7 @@ +#define t(x,y,z) x ## y ## z +#define xxx(s) int s[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), \ + t(10,,), t(,11,), t(,,12), t(,,) }; + +int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), + t(10,,), t(,11,), t(,,12), t(,,) }; +xxx(j) diff --git a/05/tcc-final-old/tests/pp/05.expect b/05/tcc-final-old/tests/pp/05.expect new file mode 100644 index 0000000..9f9be37 --- /dev/null +++ b/05/tcc-final-old/tests/pp/05.expect @@ -0,0 +1,3 @@ +int j[] = { 123, 45, 67, 89, + 10, 11, 12, }; +int j[] = { 123, 45, 67, 89, 10, 11, 12, }; diff --git a/05/tcc-final-old/tests/pp/06.c b/05/tcc-final-old/tests/pp/06.c new file mode 100644 index 0000000..28cfdde --- /dev/null +++ b/05/tcc-final-old/tests/pp/06.c @@ -0,0 +1,5 @@ +#define X(a,b, \ + c,d) \ + foo + +X(1,2,3,4) diff --git a/05/tcc-final-old/tests/pp/06.expect b/05/tcc-final-old/tests/pp/06.expect new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/05/tcc-final-old/tests/pp/06.expect @@ -0,0 +1 @@ +foo diff --git a/05/tcc-final-old/tests/pp/07.c b/05/tcc-final-old/tests/pp/07.c new file mode 100644 index 0000000..b22b22b --- /dev/null +++ b/05/tcc-final-old/tests/pp/07.c @@ -0,0 +1,4 @@ +#define a() YES +#define b() a +b() +b()() diff --git a/05/tcc-final-old/tests/pp/07.expect b/05/tcc-final-old/tests/pp/07.expect new file mode 100644 index 0000000..ad0e20a --- /dev/null +++ b/05/tcc-final-old/tests/pp/07.expect @@ -0,0 +1,2 @@ +a +YES diff --git a/05/tcc-final-old/tests/pp/08.c b/05/tcc-final-old/tests/pp/08.c new file mode 100644 index 0000000..93018e1 --- /dev/null +++ b/05/tcc-final-old/tests/pp/08.c @@ -0,0 +1,4 @@ +// test macro expansion in arguments +#define s_pos s_s.s_pos +#define foo(x) (x) +foo(hej.s_pos) diff --git a/05/tcc-final-old/tests/pp/08.expect b/05/tcc-final-old/tests/pp/08.expect new file mode 100644 index 0000000..2b2e3ee --- /dev/null +++ b/05/tcc-final-old/tests/pp/08.expect @@ -0,0 +1 @@ +(hej.s_s.s_pos) diff --git a/05/tcc-final-old/tests/pp/09.c b/05/tcc-final-old/tests/pp/09.c new file mode 100644 index 0000000..315b297 --- /dev/null +++ b/05/tcc-final-old/tests/pp/09.c @@ -0,0 +1,4 @@ +#define C(a,b,c) a##b##c +#define N(x,y) C(x,_,y) +#define A_O aaaaoooo +N(A,O) diff --git a/05/tcc-final-old/tests/pp/09.expect b/05/tcc-final-old/tests/pp/09.expect new file mode 100644 index 0000000..adce0f9 --- /dev/null +++ b/05/tcc-final-old/tests/pp/09.expect @@ -0,0 +1 @@ +aaaaoooo diff --git a/05/tcc-final-old/tests/pp/10.c b/05/tcc-final-old/tests/pp/10.c new file mode 100644 index 0000000..f180eff --- /dev/null +++ b/05/tcc-final-old/tests/pp/10.c @@ -0,0 +1,10 @@ +#define f(x) x +#define g(x) f(x) f(x +#define i(x) g(x)) g(x +#define h(x) i(x))) i(x +#define k(x) i(x))) i(x)))) +f(x) +g(x)) +i(x))) +h(x)))) +k(x)))) diff --git a/05/tcc-final-old/tests/pp/10.expect b/05/tcc-final-old/tests/pp/10.expect new file mode 100644 index 0000000..bd18e18 --- /dev/null +++ b/05/tcc-final-old/tests/pp/10.expect @@ -0,0 +1,5 @@ +x +x x +x x x x +x x x x x x x x +x x x x x x x x)))) diff --git a/05/tcc-final-old/tests/pp/11.c b/05/tcc-final-old/tests/pp/11.c new file mode 100644 index 0000000..c0edf62 --- /dev/null +++ b/05/tcc-final-old/tests/pp/11.c @@ -0,0 +1,31 @@ +#define D1(s, ...) s +#define D2(s, ...) s D1(__VA_ARGS__) +#define D3(s, ...) s D2(__VA_ARGS__) +#define D4(s, ...) s D3(__VA_ARGS__) + +D1(a) +D2(a, b) +D3(a, b, c) +D4(a, b, c, d) + +x D4(a, b, c, d) y +x D4(a, b, c) y +x D4(a, b) y +x D4(a) y +x D4() y + +#define GNU_COMMA(X,Y...) X,## Y + +x GNU_COMMA(A,B,C) y +x GNU_COMMA(A,B) y +x GNU_COMMA(A) y +x GNU_COMMA() y + +#define __sun_attr___noreturn__ __attribute__((__noreturn__)) +#define ___sun_attr_inner(__a) __sun_attr_##__a +#define __sun_attr__(__a) ___sun_attr_inner __a +#define __NORETURN __sun_attr__((__noreturn__)) +__NORETURN +#define X(...) +#define Y(...) 1 __VA_ARGS__ 2 +Y(X X() ()) diff --git a/05/tcc-final-old/tests/pp/11.expect b/05/tcc-final-old/tests/pp/11.expect new file mode 100644 index 0000000..6b9806c --- /dev/null +++ b/05/tcc-final-old/tests/pp/11.expect @@ -0,0 +1,15 @@ +a +a b +a b c +a b c d +x a b c d y +x a b c y +x a b y +x a y +x y +x A,B,C y +x A,B y +x A y +x y +__attribute__((__noreturn__)) +1 2 diff --git a/05/tcc-final-old/tests/pp/12.S b/05/tcc-final-old/tests/pp/12.S new file mode 100644 index 0000000..597ccb4 --- /dev/null +++ b/05/tcc-final-old/tests/pp/12.S @@ -0,0 +1,8 @@ +#define SRC(y...) \ + 9999: y; \ + .section __ex_table, "a"; \ + .long 9999b, 6001f ; \ + // .previous + + SRC(1: movw (%esi), %bx) +6001: diff --git a/05/tcc-final-old/tests/pp/12.expect b/05/tcc-final-old/tests/pp/12.expect new file mode 100644 index 0000000..17a861c --- /dev/null +++ b/05/tcc-final-old/tests/pp/12.expect @@ -0,0 +1,2 @@ + 9999: 1: movw (%esi), %bx; .section __ex_table, "a"; .long 9999b, 6001f ; +6001: diff --git a/05/tcc-final-old/tests/pp/13.S b/05/tcc-final-old/tests/pp/13.S new file mode 100644 index 0000000..bf0b525 --- /dev/null +++ b/05/tcc-final-old/tests/pp/13.S @@ -0,0 +1,6 @@ +# `modelist' label. Each video mode record looks like: +#ifdef AAA +# modelist' label. Each video mode record looks like: +#endif +.text +endtext: diff --git a/05/tcc-final-old/tests/pp/13.expect b/05/tcc-final-old/tests/pp/13.expect new file mode 100644 index 0000000..c7a3230 --- /dev/null +++ b/05/tcc-final-old/tests/pp/13.expect @@ -0,0 +1,2 @@ +.text +endtext: diff --git a/05/tcc-final-old/tests/pp/14.c b/05/tcc-final-old/tests/pp/14.c new file mode 100644 index 0000000..e15468c --- /dev/null +++ b/05/tcc-final-old/tests/pp/14.c @@ -0,0 +1,13 @@ +#define W Z +#define Z(X) W(X,2) +#define Y(X) Z(X) +#define X Y +return X(X(1)); + +#define P Q +#define Q(n) P(n,2) +return P(1); + +#define A (B * B) +#define B (A + A) +return A + B; diff --git a/05/tcc-final-old/tests/pp/14.expect b/05/tcc-final-old/tests/pp/14.expect new file mode 100644 index 0000000..3786044 --- /dev/null +++ b/05/tcc-final-old/tests/pp/14.expect @@ -0,0 +1,3 @@ +return Z(Z(1,2),2); +return Q(1,2); +return ((A + A) * (A + A)) + ((B * B) + (B * B)); diff --git a/05/tcc-final-old/tests/pp/15.c b/05/tcc-final-old/tests/pp/15.c new file mode 100644 index 0000000..cf13f9a --- /dev/null +++ b/05/tcc-final-old/tests/pp/15.c @@ -0,0 +1,18 @@ +// insert a space between two tokens if otherwise they +// would form a single token when read back + +#define n(x) x + +return (n(long)n(double))d; +return n(A)n(++)n(+)n(B); +return n(A)n(+)n(++)n(B); +return n(A)n(++)n(+)n(+)n(B); + +// not a hex float +return n(0x1E)n(-1); + +// unlike gcc but correct +// XXX: return n(x)+n(x)-n(1)+n(1)-2; + +// unlike gcc, but cannot appear in valid C +// XXX: return n(x)n(x)n(1)n(2)n(x); diff --git a/05/tcc-final-old/tests/pp/15.expect b/05/tcc-final-old/tests/pp/15.expect new file mode 100644 index 0000000..b4f885e --- /dev/null +++ b/05/tcc-final-old/tests/pp/15.expect @@ -0,0 +1,5 @@ +return (long double)d; +return A+++B; +return A+ ++B; +return A+++ +B; +return 0x1E -1; diff --git a/05/tcc-final-old/tests/pp/16.c b/05/tcc-final-old/tests/pp/16.c new file mode 100644 index 0000000..8b5b642 --- /dev/null +++ b/05/tcc-final-old/tests/pp/16.c @@ -0,0 +1,3 @@ +/* The following should warn */ +#define A ... +#define A <<= diff --git a/05/tcc-final-old/tests/pp/16.expect b/05/tcc-final-old/tests/pp/16.expect new file mode 100644 index 0000000..695d6d4 --- /dev/null +++ b/05/tcc-final-old/tests/pp/16.expect @@ -0,0 +1,2 @@ + +16.c:3: warning: A redefined diff --git a/05/tcc-final-old/tests/pp/17.c b/05/tcc-final-old/tests/pp/17.c new file mode 100644 index 0000000..f12196f --- /dev/null +++ b/05/tcc-final-old/tests/pp/17.c @@ -0,0 +1,14 @@ +#define STR1(u) # u +#define pass(a) a +#define __ASM_REG(reg) STR1(one##reg) +#define _ASM_DX __ASM_REG(tok) +X162 pass(__ASM_REG(tok)) +X161 pass(_ASM_DX) +X163 pass(STR1(one##tok)) + +X170 pass(x ## y) +X171 pass(x pass(##) y) + +#define Y(x) Z(x) +#define X Y +X180 return X(X(1)); diff --git a/05/tcc-final-old/tests/pp/17.expect b/05/tcc-final-old/tests/pp/17.expect new file mode 100644 index 0000000..e95aafe --- /dev/null +++ b/05/tcc-final-old/tests/pp/17.expect @@ -0,0 +1,6 @@ +X162 "onetok" +X161 "onetok" +X163 "one##tok" +X170 x ## y +X171 x ## y +X180 return Z(Z(1)); diff --git a/05/tcc-final-old/tests/pp/18.c b/05/tcc-final-old/tests/pp/18.c new file mode 100644 index 0000000..0961426 --- /dev/null +++ b/05/tcc-final-old/tests/pp/18.c @@ -0,0 +1,15 @@ +#define M_RETI_ARG27(x,y,z,aa, ...) aa +#define M_RET_ARG27(...) M_RETI_ARG27(__VA_ARGS__) +#define M_COMMA_P(...) M_RET_ARG27(__VA_ARGS__, 1, 1, 0, useless) +#define M_EMPTYI_DETECT(...) 0, 1, +#define M_EMPTYI_P_C1(...) M_COMMA_P(M_EMPTYI_DETECT __VA_ARGS__ () ) +#define EX +#define empty(x) +#define fnlike(x) yeah x +/* If the following macro is called with empty arg (X183), the use + of 'x' between fnlike and '(' doesn't hinder the recognition of this + being a further fnlike macro invocation. */ +#define usefnlike(x) fnlike x (x) +X181 M_EMPTYI_P_C1() +X182 M_EMPTYI_P_C1(x) +X183 usefnlike() diff --git a/05/tcc-final-old/tests/pp/18.expect b/05/tcc-final-old/tests/pp/18.expect new file mode 100644 index 0000000..447a9b2 --- /dev/null +++ b/05/tcc-final-old/tests/pp/18.expect @@ -0,0 +1,3 @@ +X181 1 +X182 0 +X183 yeah diff --git a/05/tcc-final-old/tests/pp/19.c b/05/tcc-final-old/tests/pp/19.c new file mode 100644 index 0000000..aa91abe --- /dev/null +++ b/05/tcc-final-old/tests/pp/19.c @@ -0,0 +1,101 @@ +#define M_C2I(a, ...) a ## __VA_ARGS__ +#define M_C(a, ...) M_C2I(a, __VA_ARGS__) +#define M_C3I(a, b, ...) a ## b ## __VA_ARGS__ +#define M_C3(a, b, ...) M_C3I(a ,b, __VA_ARGS__) + +#define M_RETI_ARG2(a, b, ...) b +#define M_RET_ARG2(...) M_RETI_ARG2(__VA_ARGS__) +#define M_RETI_ARG27(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa, ...) aa +#define M_RET_ARG27(...) M_RETI_ARG27(__VA_ARGS__) + +#define M_TOBOOLI_0 1, 0, +#define M_BOOL(x) M_RET_ARG2(M_C(M_TOBOOLI_, x), 1, useless) + +#define M_IFI_0(true_macro, ...) __VA_ARGS__ +#define M_IFI_1(true_macro, ...) true_macro +#define M_IF(c) M_C(M_IFI_, M_BOOL(c)) + +#define M_FLAT(...) __VA_ARGS__ +#define M_INVI_0 1 +#define M_INVI_1 0 +#define M_INV(x) M_C(M_INVI_, x) + +#define M_ANDI_00 0 +#define M_ANDI_01 0 +#define M_ANDI_10 0 +#define M_ANDI_11 1 +#define M_AND(x,y) M_C3(M_ANDI_, x, y) + +#define M_ORI_00 0 +#define M_ORI_01 1 +#define M_ORI_10 1 +#define M_ORI_11 1 +#define M_OR(x,y) M_C3(M_ORI_, x, y) + +#define M_COMMA_P(...) M_RET_ARG27(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, useless) + +#define M_EMPTYI_DETECT(...) 0, 1, +#define M_EMPTYI_P_C1(...) M_COMMA_P(M_EMPTYI_DETECT __VA_ARGS__ ()) +#define M_EMPTYI_P_C2(...) M_COMMA_P(M_EMPTYI_DETECT __VA_ARGS__) +#define M_EMPTYI_P_C3(...) M_COMMA_P(__VA_ARGS__ () ) +#define M_EMPTY_P(...) M_AND(M_EMPTYI_P_C1(__VA_ARGS__), M_INV(M_OR(M_OR(M_EMPTYI_P_C2(__VA_ARGS__), M_COMMA_P(__VA_ARGS__)),M_EMPTYI_P_C3(__VA_ARGS__)))) +#define M_APPLY_FUNC2B(func, arg1, arg2) \ + M_IF(M_EMPTY_P(arg2))(,func(arg1, arg2)) +#define M_MAP2B_0(func, data, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,...) \ + M_APPLY_FUNC2B(func, data, a) M_APPLY_FUNC2B(func, data, b) M_APPLY_FUNC2B(func, data, c) \ + M_APPLY_FUNC2B(func, data, d) M_APPLY_FUNC2B(func, data, e) M_APPLY_FUNC2B(func, data, f) \ + M_APPLY_FUNC2B(func, data, g) M_APPLY_FUNC2B(func, data, h) M_APPLY_FUNC2B(func, data, i) \ + M_APPLY_FUNC2B(func, data, j) M_APPLY_FUNC2B(func, data, k) M_APPLY_FUNC2B(func, data, l) \ + M_APPLY_FUNC2B(func, data, m) M_APPLY_FUNC2B(func, data, n) M_APPLY_FUNC2B(func, data, o) \ + M_APPLY_FUNC2B(func, data, p) M_APPLY_FUNC2B(func, data, q) M_APPLY_FUNC2B(func, data, r) \ + M_APPLY_FUNC2B(func, data, s) M_APPLY_FUNC2B(func, data, t) M_APPLY_FUNC2B(func, data, u) \ + M_APPLY_FUNC2B(func, data, v) M_APPLY_FUNC2B(func, data, w) M_APPLY_FUNC2B(func, data, x) \ + M_APPLY_FUNC2B(func, data, y) M_APPLY_FUNC2B(func, data, z) +#define M_MAP2B(f, ...) M_MAP2B_0(f, __VA_ARGS__, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ) +#define M_INIT_INIT(a) ,a, + +#define M_GET_METHOD(method, method_default, ...) \ + M_RET_ARG2 (M_MAP2B(M_C, M_C3(M_, method, _), __VA_ARGS__), method_default,) + +#define M_TEST_METHOD_P(method, oplist) \ + M_BOOL(M_GET_METHOD (method, 0, M_FLAT oplist)) + +#define TRUE 1 +#define TEST1(n) \ + M_IF(n)(ok,nok) +#define TEST2(op) \ + M_TEST_METHOD_P(INIT, op) +#define TEST3(op) \ + M_IF(M_TEST_METHOD_P(INIT, op))(ok, nok) +#define TEST4(op) \ + TEST1(TEST2(op)) +#define KO(a) ((void)1) + +/* This checks that the various expansions that ultimately lead to + something like 'KO(arg,arg)', where 'KO' comes from a macro + expansion reducing from a large macro chain do not are regarded + as funclike macro invocation of KO. E.g. X93 and X94 expand to 'KO', + but X95 must not consume the (a,b) arguments outside the M_IF() + invocation to reduce the 'KO' macro to an invocation. Instead + X95 should reduce via M_IF(KO)(a,b) to 'a'. + + The other lines here are variations on this scheme, with X1 to + X6 coming from the bug report at + http://lists.nongnu.org/archive/html/tinycc-devel/2017-07/msg00017.html */ +X92 M_IF(KO) +X93 M_GET_METHOD(INIT, 0, INIT(KO)) +X94 M_GET_METHOD(INIT, 0, M_FLAT (INIT(KO))) +X95 M_IF(M_GET_METHOD(INIT, 0, INIT(KO)))(a,b) +X96 M_IF(M_GET_METHOD(INIT, 0, M_FLAT (INIT(KO)))) +X97 M_IF(M_GET_METHOD(INIT, 0, M_FLAT (INIT(KO))))(ok,nok) +X98 (M_TEST_METHOD_P(INIT, (INIT(KO))))(ok, nok) +X99 M_IF(M_TEST_METHOD_P(INIT, (INIT(KO))))(ok, nok) +// test begins +X1 TEST1(TRUE) // ==> expect ok, get ok +// First test with a token which is not a macro +X2 TEST2((INIT(ok))) // ==> expect 1, get 1 +X3 TEST3((INIT(ok))) // ==> expect ok, get ok +// Then test with a token which is a macro, but should not be expanded. +X4 TEST2((INIT(KO))) // ==> expect 1, get 1 +X5 TEST4(INIT(KO)) +X6 TEST3((INIT(KO))) // ==> expect ok, get "error: macro 'KO' used with too many args" diff --git a/05/tcc-final-old/tests/pp/19.expect b/05/tcc-final-old/tests/pp/19.expect new file mode 100644 index 0000000..08c0858 --- /dev/null +++ b/05/tcc-final-old/tests/pp/19.expect @@ -0,0 +1,14 @@ +X92 M_IFI_1 +X93 KO +X94 KO +X95 a +X96 M_IFI_1 +X97 ok +X98 (1)(ok, nok) +X99 ok +X1 ok +X2 1 +X3 ok +X4 1 +X5 nok +X6 ok diff --git a/05/tcc-final-old/tests/pp/20.c b/05/tcc-final-old/tests/pp/20.c new file mode 100644 index 0000000..7944d62 --- /dev/null +++ b/05/tcc-final-old/tests/pp/20.c @@ -0,0 +1,13 @@ +/* Various things I encountered while hacking the pre processor */ +#define wrap(x) x +#define pr_warning(fmt, ...) printk(KERN_WARNING fmt, ##__VA_ARGS__) +#define pr_warn(x,y) pr_warning(x,y) +#define net_ratelimited_function(function, ...) function(__VA_ARGS__) +X1 net_ratelimited_function(pr_warn, "pipapo", bla); +X2 net_ratelimited_function(wrap(pr_warn), "bla", foo); +#define two m n +#define chain4(a,b,c,d) a ## b ## c ## d +X2 chain4(two,o,p,q) +X3 chain4(o,two,p,q) +X4 chain4(o,p,two,q) +X5 chain4(o,p,q,two) diff --git a/05/tcc-final-old/tests/pp/20.expect b/05/tcc-final-old/tests/pp/20.expect new file mode 100644 index 0000000..d19405d --- /dev/null +++ b/05/tcc-final-old/tests/pp/20.expect @@ -0,0 +1,6 @@ +X1 printk(KERN_WARNING "pipapo",bla); +X2 printk(KERN_WARNING "bla",foo); +X2 twoopq +X3 otwopq +X4 optwoq +X5 opqtwo diff --git a/05/tcc-final-old/tests/pp/21.c b/05/tcc-final-old/tests/pp/21.c new file mode 100644 index 0000000..1316226 --- /dev/null +++ b/05/tcc-final-old/tests/pp/21.c @@ -0,0 +1,36 @@ +/* accept 'defined' as result of substitution */ + +----- 1 ------ +#define AAA 2 +#define BBB +#define CCC (defined ( AAA ) && AAA > 1 && !defined BBB) +#if !CCC +OK +#else +NOT OK +#endif + +----- 2 ------ +#undef BBB +#if CCC +OK +#else +NOT OK +#endif + +----- 3 ------ +#define DEFINED defined +#define DDD (DEFINED ( AAA ) && AAA > 1 && !DEFINED BBB) +#if (DDD) +OK +#else +NOT OK +#endif + +----- 4 ------ +#undef AAA +#if !(DDD) +OK +#else +NOT OK +#endif diff --git a/05/tcc-final-old/tests/pp/21.expect b/05/tcc-final-old/tests/pp/21.expect new file mode 100644 index 0000000..5a1376b --- /dev/null +++ b/05/tcc-final-old/tests/pp/21.expect @@ -0,0 +1,8 @@ +----- 1 ------ +OK +----- 2 ------ +OK +----- 3 ------ +OK +----- 4 ------ +OK diff --git a/05/tcc-final-old/tests/pp/Makefile b/05/tcc-final-old/tests/pp/Makefile new file mode 100644 index 0000000..687aa52 --- /dev/null +++ b/05/tcc-final-old/tests/pp/Makefile @@ -0,0 +1,49 @@ +# +# credits: 01..13.c from the pcc cpp-tests suite +# + +TOP = ../.. +include $(TOP)/Makefile +SRC = $(TOPSRC)/tests/pp +VPATH = $(SRC) + +files = $(patsubst %.$1,%.test,$(notdir $(wildcard $(SRC)/*.$1))) +TESTS = $(call files,c) $(call files,S) + +all test : $(sort $(TESTS)) + +DIFF_OPTS = -Nu -b -B + +# Filter source directory in warnings/errors (out-of-tree builds) +FILTER = 2>&1 | sed 's,$(SRC)/,,g' + +%.test: %.c %.expect + @echo PPTest $* ... + -@$(TCC) -E -P $< $(FILTER) >$*.output 2>&1 ; \ + diff $(DIFF_OPTS) $(SRC)/$*.expect $*.output \ + && rm -f $*.output + +%.test: %.S %.expect + @echo PPTest $* ... + -@$(TCC) -E -P $< $(FILTER) >$*.output 2>&1 ; \ + diff $(DIFF_OPTS) $(SRC)/$*.expect $*.output \ + && rm -f $*.output + +# automatically generate .expect files with gcc: +%.expect: # %.c + gcc -E -P $*.[cS] >$*.expect 2>&1 + +# tell make not to delete +.PRECIOUS: %.expect + +clean: + rm -f *.output + +02.test : DIFF_OPTS += -w +# 15.test : DIFF_OPTS += -I"^XXX:" + +# diff options: +# -b ighore space changes +# -w ighore all whitespace +# -B ignore blank lines +# -I ignore lines matching RE diff --git a/05/tcc-final-old/tests/pp/pp-counter.c b/05/tcc-final-old/tests/pp/pp-counter.c new file mode 100644 index 0000000..3978e1a --- /dev/null +++ b/05/tcc-final-old/tests/pp/pp-counter.c @@ -0,0 +1,27 @@ +X1 __COUNTER__ +X2 __COUNTER__ +#if __COUNTER__ +X3 __COUNTER__ +#endif +#define pass(x) x +#define a x __COUNTER__ y +#define a2 pass(__COUNTER__) +#define f(c) c __COUNTER__ +#define apply(d) d d __COUNTER__ x2 f(d) y2 __COUNTER__ +#define _paste(a,b) a ## b +#define paste(a,b) _paste(a,b) +#define _paste3(a,b,c) a ## b ## c +#define doublepaste(a,b) _paste3(a,b,b) +#define str(x) #x +X4 a +X5 f(a) +X6 f(b) +X7 f(__COUNTER__) +X8 apply(a) +X9 apply(f(a)) +X10 apply(__COUNTER__) +X11 apply(a2) +X12 str(__COUNTER__) +X13 paste(x,__COUNTER__) +X14 _paste(x,__COUNTER__) +X15 doublepaste(x,__COUNTER__) diff --git a/05/tcc-final-old/tests/pp/pp-counter.expect b/05/tcc-final-old/tests/pp/pp-counter.expect new file mode 100644 index 0000000..02fc535 --- /dev/null +++ b/05/tcc-final-old/tests/pp/pp-counter.expect @@ -0,0 +1,15 @@ +X1 0 +X2 1 +X3 3 +X4 x 4 y +X5 x 5 y 6 +X6 b 7 +X7 8 9 +X8 x 10 y x 10 y 11 x2 x 10 y 12 y2 13 +X9 x 14 y 15 x 14 y 15 16 x2 x 14 y 15 17 y2 18 +X10 19 19 20 x2 19 21 y2 22 +X11 23 23 24 x2 23 25 y2 26 +X12 "__COUNTER__" +X13 x27 +X14 x__COUNTER__ +X15 x2828 diff --git a/05/tcc-final-old/tests/tcctest.c b/05/tcc-final-old/tests/tcctest.c new file mode 100644 index 0000000..57670be --- /dev/null +++ b/05/tcc-final-old/tests/tcctest.c @@ -0,0 +1,3871 @@ +/* + * TCC auto test program + */ +#include "config.h" + +#if GCC_MAJOR >= 3 + +/* Unfortunately, gcc version < 3 does not handle that! */ +#define ALL_ISOC99 + +/* only gcc 3 handles _Bool correctly */ +#define BOOL_ISOC99 + +/* gcc 2.95.3 does not handle correctly CR in strings or after strays */ +#define CORRECT_CR_HANDLING + +#endif + +#if defined(_WIN32) +#define LONG_LONG_FORMAT "%lld" +#define ULONG_LONG_FORMAT "%llu" +#else +#define LONG_LONG_FORMAT "%Ld" +#define ULONG_LONG_FORMAT "%Lu" +#endif + +// MinGW has 80-bit rather than 64-bit long double which isn't compatible with TCC or MSVC +#if defined(_WIN32) && defined(__GNUC__) +#define LONG_DOUBLE double +#define LONG_DOUBLE_LITERAL(x) x +#else +#define LONG_DOUBLE long double +#define LONG_DOUBLE_LITERAL(x) x ## L +#endif + +/* deprecated and no longer supported in gcc 3.3 */ +//#define ACCEPT_CR_IN_STRINGS + +/* __VA_ARGS__ and __func__ support */ +#define C99_MACROS + +/* test various include syntaxes */ + +#define TCCLIB_INC +#define TCCLIB_INC1 +#define TCCLIB_INC3 "tcclib.h" + +#include TCCLIB_INC + +#include TCCLIB_INC1.TCCLIB_INC2 + +#include TCCLIB_INC1.h> + +#include TCCLIB_INC3 + +#include + +#include "tcclib.h" + +#include "tcctest.h" + +/* Test two more ways to include a file named like a pp-number */ +#define INC(name) +#define funnyname 42test.h +#define incdir tests/ +#define incname < incdir funnyname > +#define __stringify(x) #x +#define stringify(x) __stringify(x) +#include INC(42test) +#include incname +#include stringify(funnyname) + +void intdiv_test(); +void string_test(); +void expr_test(); +void macro_test(); +void recursive_macro_test(); +void scope_test(); +void forward_test(); +void funcptr_test(); +void loop_test(); +void switch_test(); +void goto_test(); +void enum_test(); +void typedef_test(); +void struct_test(); +void array_test(); +void expr_ptr_test(); +void bool_test(); +void optimize_out(); +void expr2_test(); +void constant_expr_test(); +void expr_cmp_test(); +void char_short_test(); +void init_test(void); +void compound_literal_test(void); +int kr_test(); +void struct_assign_test(void); +void cast_test(void); +void bitfield_test(void); +void c99_bool_test(void); +void float_test(void); +void longlong_test(void); +void manyarg_test(void); +void stdarg_test(void); +void whitespace_test(void); +void relocation_test(void); +void old_style_function(void); +void alloca_test(void); +void c99_vla_test(int size1, int size2); +void sizeof_test(void); +void typeof_test(void); +void local_label_test(void); +void statement_expr_test(void); +void asm_test(void); +void builtin_test(void); +void weak_test(void); +void global_data_test(void); +void cmp_comparison_test(void); +void math_cmp_test(void); +void callsave_test(void); +void builtin_frame_address_test(void); +void attrib_test(void); + +int fib(int n); +void num(int n); +void forward_ref(void); +int isid(int c); + +/* Line joining happens before tokenization, so the following + must be parsed as ellipsis. */ +void funny_line_continuation (int, ..\ +. ); + +char via_volatile (char); + +#define A 2 +#define N 1234 + A +#define pf printf +#define M1(a, b) (a) + (b) + +#define str\ +(s) # s +#define glue(a, b) a ## b +#define xglue(a, b) glue(a, b) +#define HIGHLOW "hello" +#define LOW LOW ", world" + +static int onetwothree = 123; +#define onetwothree4 onetwothree +#define onetwothree xglue(onetwothree,4) + +#define min(a, b) ((a) < (b) ? (a) : (b)) + +#ifdef C99_MACROS +#define dprintf(level,...) printf(__VA_ARGS__) +#endif + +/* gcc vararg macros */ +#define dprintf1(level, fmt, args...) printf(fmt, ## args) + +#define MACRO_NOARGS() + +#define AAA 3 +#undef AAA +#define AAA 4 + +#if 1 +#define B3 1 +#elif 1 +#define B3 2 +#elif 0 +#define B3 3 +#else +#define B3 4 +#endif + +#ifdef __TINYC__ +/* We try to handle this syntax. Make at least sure it doesn't segfault. */ +char invalid_function_def()[] {} +#endif + +#define __INT64_C(c) c ## LL +#define INT64_MIN (-__INT64_C(9223372036854775807)-1) + +int qq(int x) +{ + return x + 40; +} +#define qq(x) x + +#define spin_lock(lock) do { } while (0) +#define wq_spin_lock spin_lock +#define TEST2() wq_spin_lock(a) + +#define UINT_MAX ((unsigned) -1) + +void intdiv_test(void) +{ + printf("18/21=%u\n", 18/21); + printf("18%%21=%u\n", 18%21); + printf("41/21=%u\n", 41/21); + printf("41%%21=%u\n", 41%21); + printf("42/21=%u\n", 42/21); + printf("42%%21=%u\n", 42%21); + printf("43/21=%u\n", 43/21); + printf("43%%21=%u\n", 43%21); + printf("126/21=%u\n", 126/21); + printf("126%%21=%u\n", 126%21); + printf("131/21=%u\n", 131/21); + printf("131%%21=%u\n", 131%21); + printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2); + printf("(UINT_MAX/2+3)%%2=%u\n", (UINT_MAX/2+3)%2); + + printf("18/-21=%u\n", 18/-21); + printf("18%%-21=%u\n", 18%-21); + printf("41/-21=%u\n", 41/-21); + printf("41%%-21=%u\n", 41%-21); + printf("42/-21=%u\n", 42/-21); + printf("42%%-21=%u\n", 42%-21); + printf("43/-21=%u\n", 43/-21); + printf("43%%-21=%u\n", 43%-21); + printf("126/-21=%u\n", 126/-21); + printf("126%%-21=%u\n", 126%-21); + printf("131/-21=%u\n", 131/-21); + printf("131%%-21=%u\n", 131%-21); + printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2); + printf("(UINT_MAX/2+3)%%-2=%u\n", (UINT_MAX/2+3)%-2); + + printf("-18/21=%u\n", -18/21); + printf("-18%%21=%u\n", -18%21); + printf("-41/21=%u\n", -41/21); + printf("-41%%21=%u\n", -41%21); + printf("-42/21=%u\n", -42/21); + printf("-42%%21=%u\n", -42%21); + printf("-43/21=%u\n", -43/21); + printf("-43%%21=%u\n", -43%21); + printf("-126/21=%u\n", -126/21); + printf("-126%%21=%u\n", -126%21); + printf("-131/21=%u\n", -131/21); + printf("-131%%21=%u\n", -131%21); + printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2); + printf("-(UINT_MAX/2+3)%%2=%u\n", (0-(UINT_MAX/2+3))%2); + + printf("-18/-21=%u\n", -18/-21); + printf("-18%%-21=%u\n", -18%-21); + printf("-41/-21=%u\n", -41/-21); + printf("-41%%-21=%u\n", -41%-21); + printf("-42/-21=%u\n", -42/-21); + printf("-42%%-21=%u\n", -42%-21); + printf("-43/-21=%u\n", -43/-21); + printf("-43%%-21=%u\n", -43%-21); + printf("-126/-21=%u\n", -126/-21); + printf("-126%%-21=%u\n", -126%-21); + printf("-131/-21=%u\n", -131/-21); + printf("-131%%-21=%u\n", -131%-21); + printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2); + printf("-(UINT_MAX/2+3)%%-2=%u\n", (0-(UINT_MAX/2+3))%-2); +} + +void macro_test(void) +{ + printf("macro:\n"); + pf("N=%d\n", N); + printf("aaa=%d\n", AAA); + + printf("min=%d\n", min(1, min(2, -1))); + + printf("s1=%s\n", glue(HIGH, LOW)); + printf("s2=%s\n", xglue(HIGH, LOW)); + printf("s3=%s\n", str("c")); + printf("s4=%s\n", str(a1)); + printf("B3=%d\n", B3); + + printf("onetwothree=%d\n", onetwothree); + +#ifdef A + printf("A defined\n"); +#endif +#ifdef B + printf("B defined\n"); +#endif +#ifdef A + printf("A defined\n"); +#else + printf("A not defined\n"); +#endif +#ifdef B + printf("B defined\n"); +#else + printf("B not defined\n"); +#endif + +#ifdef A + printf("A defined\n"); +#ifdef B + printf("B1 defined\n"); +#else + printf("B1 not defined\n"); +#endif +#else + printf("A not defined\n"); +#ifdef B + printf("B2 defined\n"); +#else + printf("B2 not defined\n"); +#endif +#endif + +#if 1+1 + printf("test true1\n"); +#endif +#if 0 + printf("test true2\n"); +#endif +#if 1-1 + printf("test true3\n"); +#endif +#if defined(A) + printf("test trueA\n"); +#endif +#if defined(B) + printf("test trueB\n"); +#endif + +#if 0 + printf("test 0\n"); +#elif 0 + printf("test 1\n"); +#elif 2 + printf("test 2\n"); +#else + printf("test 3\n"); +#endif + + MACRO_NOARGS(); + +#ifdef __LINE__ + printf("__LINE__ defined\n"); +#endif + + printf("__LINE__=%d __FILE__=%s\n", + __LINE__, __FILE__); +#if 0 +#line 200 + printf("__LINE__=%d __FILE__=%s\n", + __LINE__, __FILE__); +#line 203 "test" + printf("__LINE__=%d __FILE__=%s\n", + __LINE__, __FILE__); +#line 227 "tcctest.c" +#endif + + /* not strictly preprocessor, but we test it there */ +#ifdef C99_MACROS + printf("__func__ = %s\n", __func__); + dprintf(1, "vaarg=%d\n", 1); +#endif + dprintf1(1, "vaarg1\n"); + dprintf1(1, "vaarg1=%d\n", 2); + dprintf1(1, "vaarg1=%d %d\n", 1, 2); + + /* gcc extension */ + printf("func='%s'\n", __FUNCTION__); + + /* complicated macros in glibc */ + printf("INT64_MIN=" LONG_LONG_FORMAT "\n", INT64_MIN); + { + int a; + a = 1; + glue(a+, +); + printf("a=%d\n", a); + glue(a <, <= 2); + printf("a=%d\n", a); + } + + /* macro function with argument outside the macro string */ +#define MF_s MF_hello +#define MF_hello(msg) printf("%s\n",msg) + +#define MF_t printf("tralala\n"); MF_hello + + MF_s("hi"); + MF_t("hi"); + + /* test macro substitution inside args (should not eat stream) */ + printf("qq=%d\n", qq(qq)(2)); + + /* test zero argument case. NOTE: gcc 2.95.x does not accept a + null argument without a space. gcc 3.2 fixes that. */ + +#define qq1(x) 1 + printf("qq1=%d\n", qq1( )); + + /* comment with stray handling *\ +/ + /* this is a valid *\/ comment */ + /* this is a valid comment *\*/ + // this is a valid\ +comment + + /* test function macro substitution when the function name is + substituted */ + TEST2(); + + /* And again when the name and parentheses are separated by a + comment. */ + TEST2 /* the comment */ (); + + printf("%s\n", get_basefile_from_header()); + printf("%s\n", __BASE_FILE__); + printf("%s\n", get_file_from_header()); + printf("%s\n", __FILE__); + + /* Check that funnily named include was in fact included */ + have_included_42test_h = 1; + have_included_42test_h_second = 1; + have_included_42test_h_third = 1; +} + + +static void print_num(char *fn, int line, int num) { + printf("fn %s, line %d, num %d\n", fn, line, num); +} + +void recursive_macro_test(void) +{ + +#define ELF32_ST_TYPE(val) ((val) & 0xf) +#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) +#define STB_WEAK 2 /* Weak symbol */ +#define ELFW(type) ELF##32##_##type + printf("%d\n", ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(123))); + +#define WRAP(x) x + +#define print_num(x) print_num(__FILE__,__LINE__,x) + print_num(123); + WRAP(print_num(123)); + WRAP(WRAP(print_num(123))); + +static struct recursive_macro { int rm_field; } G; +#define rm_field (G.rm_field) + printf("rm_field = %d\n", rm_field); + printf("rm_field = %d\n", WRAP(rm_field)); + WRAP((printf("rm_field = %d %d\n", rm_field, WRAP(rm_field)))); +} + +int op(a,b) +{ + return a / b; +} + +int ret(a) +{ + if (a == 2) + return 1; + if (a == 3) + return 2; + return 0; +} + +void ps(const char *s) +{ + int c; + while (1) { + c = *s; + if (c == 0) + break; + printf("%c", c); + s++; + } +} + +const char foo1_string[] = "\ +bar\n\ +test\14\ +1"; + +void string_test() +{ + unsigned int b; + printf("string:\n"); + printf("\141\1423\143\n");/* dezdez test */ + printf("\x41\x42\x43\x3a\n"); + printf("c=%c\n", 'r'); + printf("wc=%C 0x%lx %C\n", L'a', L'\x1234', L'c'); + printf("foo1_string='%s'\n", foo1_string); +#if 0 + printf("wstring=%S\n", L"abc"); + printf("wstring=%S\n", L"abc" L"def" "ghi"); + printf("'\\377'=%d '\\xff'=%d\n", '\377', '\xff'); + printf("L'\\377'=%d L'\\xff'=%d\n", L'\377', L'\xff'); +#endif + ps("test\n"); + b = 32; + while ((b = b + 1) < 96) { + printf("%c", b); + } + printf("\n"); + printf("fib=%d\n", fib(33)); + b = 262144; + while (b != 0x80000000) { + num(b); + b = b * 2; + } +} + +void loop_test() +{ + int i; + i = 0; + while (i < 10) + printf("%d", i++); + printf("\n"); + for(i = 0; i < 10;i++) + printf("%d", i); + printf("\n"); + i = 0; + do { + printf("%d", i++); + } while (i < 10); + printf("\n"); + + char count = 123; + /* c99 for loop init test */ + for (size_t count = 1; count < 3; count++) + printf("count=%d\n", count); + printf("count = %d\n", count); + + /* break/continue tests */ + i = 0; + while (1) { + if (i == 6) + break; + i++; + if (i == 3) + continue; + printf("%d", i); + } + printf("\n"); + + /* break/continue tests */ + i = 0; + do { + if (i == 6) + break; + i++; + if (i == 3) + continue; + printf("%d", i); + } while(1); + printf("\n"); + + for(i = 0;i < 10;i++) { + if (i == 3) + continue; + printf("%d", i); + } + printf("\n"); +} + +typedef int typedef_and_label; + +void goto_test() +{ + int i; + static void *label_table[3] = { &&label1, &&label2, &&label3 }; + + printf("goto:\n"); + i = 0; + /* This needs to parse as label, not as start of decl. */ + typedef_and_label: + s_loop: + if (i >= 10) + goto s_end; + printf("%d", i); + i++; + goto s_loop; + s_end: + printf("\n"); + + /* we also test computed gotos (GCC extension) */ + for(i=0;i<3;i++) { + goto *label_table[i]; + label1: + printf("label1\n"); + goto next; + label2: + printf("label2\n"); + goto next; + label3: + printf("label3\n"); + next: ; + } +} + +enum { + E0, + E1 = 2, + E2 = 4, + E3, + E4, +}; + +enum test { + E5 = 1000, +}; + +struct S_enum { + enum {E6 = 42, E7, E8} e:8; +}; + +enum ELong { + /* This is either 0 on L32 machines, or a large number + on L64 machines. We should be able to store this. */ + EL_large = ((unsigned long)0xf000 << 31) << 1, +}; + +enum { BIASU = -1U<<31 }; +enum { BIASS = -1 << 31 }; + +static int getint(int i) +{ + if (i) + return 0; + else + return (int)(-1U << 31); +} + +void enum_test() +{ + enum test b1; + /* The following should give no warning */ + unsigned *p = &b1; + struct S_enum s = {E7}; + printf("enum: %d\n", s.e); + printf("enum:\n%d %d %d %d %d %d\n", + E0, E1, E2, E3, E4, E5); + b1 = 1; + printf("b1=%d\n", b1); + printf("enum large: %ld\n", EL_large); + + if (getint(0) == BIASU) + printf("enum unsigned: ok\n"); + else + printf("enum unsigned: wrong\n"); + if (getint(0) == BIASS) + printf("enum unsigned: ok\n"); + else + printf("enum unsigned: wrong\n"); +} + +typedef int *my_ptr; + +typedef int mytype1; +typedef int mytype2; + +void typedef_test() +{ + my_ptr a; + mytype1 mytype2; + int b; + + a = &b; + *a = 1234; + printf("typedef:\n"); + printf("a=%d\n", *a); + mytype2 = 2; + printf("mytype2=%d\n", mytype2); +} + +void forward_test() +{ + printf("forward:\n"); + forward_ref(); + forward_ref(); +} + + +void forward_ref(void) +{ + printf("forward ok\n"); +} + +typedef struct struct1 { + int f1; + int f2, f3; + union union1 { + int v1; + int v2; + } u; + char str[3]; +} struct1; + +struct struct2 { + int a; + char b; +}; + +union union2 { + int w1; + int w2; +}; + +struct struct1 st1, st2; + +struct empty_mem { + /* nothing */ ; + int x; +}; + +int main(int argc, char **argv) +{ + string_test(); + expr_test(); + macro_test(); + recursive_macro_test(); + scope_test(); + forward_test(); + funcptr_test(); + loop_test(); + switch_test(); + goto_test(); + enum_test(); + typedef_test(); + struct_test(); + array_test(); + expr_ptr_test(); + bool_test(); + optimize_out(); + expr2_test(); + constant_expr_test(); + expr_cmp_test(); + char_short_test(); + init_test(); + compound_literal_test(); + kr_test(); + struct_assign_test(); + cast_test(); + bitfield_test(); + c99_bool_test(); + float_test(); + longlong_test(); + manyarg_test(); + stdarg_test(); + whitespace_test(); + relocation_test(); + old_style_function(); + alloca_test(); + c99_vla_test(5, 2); + sizeof_test(); + typeof_test(); + statement_expr_test(); + local_label_test(); + asm_test(); + builtin_test(); +#ifndef _WIN32 + weak_test(); +#endif + global_data_test(); + cmp_comparison_test(); + math_cmp_test(); + callsave_test(); + builtin_frame_address_test(); + intdiv_test(); + if (via_volatile (42) != 42) + printf ("via_volatile broken\n"); + attrib_test(); + return 0; +} + +int tab[3]; +int tab2[3][2]; + +int g; + +void f1(g) +{ + printf("g1=%d\n", g); +} + +void scope_test() +{ + printf("scope:\n"); + g = 2; + f1(1); + printf("g2=%d\n", g); + { + int g; + g = 3; + printf("g3=%d\n", g); + { + int g; + g = 4; + printf("g4=%d\n", g); + } + } + printf("g5=%d\n", g); +} + +void array_test() +{ + int i, j, a[4]; + + printf("array:\n"); + printf("sizeof(a) = %d\n", sizeof(a)); + printf("sizeof(\"a\") = %d\n", sizeof("a")); +#ifdef C99_MACROS + printf("sizeof(__func__) = %d\n", sizeof(__func__)); +#endif + printf("sizeof tab %d\n", sizeof(tab)); + printf("sizeof tab2 %d\n", sizeof tab2); + tab[0] = 1; + tab[1] = 2; + tab[2] = 3; + printf("%d %d %d\n", tab[0], tab[1], tab[2]); + for(i=0;i<3;i++) + for(j=0;j<2;j++) + tab2[i][j] = 10 * i + j; + for(i=0;i<3*2;i++) { + printf(" %3d", ((int *)tab2)[i]); + } + printf("\n"); + printf("sizeof(size_t)=%d\n", sizeof(size_t)); + printf("sizeof(ptrdiff_t)=%d\n", sizeof(ptrdiff_t)); +} + +void expr_test() +{ + int a, b; + a = 0; + printf("%d\n", a += 1); + printf("%d\n", a -= 2); + printf("%d\n", a *= 31232132); + printf("%d\n", a /= 4); + printf("%d\n", a %= 20); + printf("%d\n", a &= 6); + printf("%d\n", a ^= 7); + printf("%d\n", a |= 8); + printf("%d\n", a >>= 3); + printf("%d\n", a <<= 4); + + a = 22321; + b = -22321; + printf("%d\n", a + 1); + printf("%d\n", a - 2); + printf("%d\n", a * 312); + printf("%d\n", a / 4); + printf("%d\n", b / 4); + printf("%d\n", (unsigned)b / 4); + printf("%d\n", a % 20); + printf("%d\n", b % 20); + printf("%d\n", (unsigned)b % 20); + printf("%d\n", a & 6); + printf("%d\n", a ^ 7); + printf("%d\n", a | 8); + printf("%d\n", a >> 3); + printf("%d\n", b >> 3); + printf("%d\n", (unsigned)b >> 3); + printf("%d\n", a << 4); + printf("%d\n", ~a); + printf("%d\n", -a); + printf("%d\n", +a); + + printf("%d\n", 12 + 1); + printf("%d\n", 12 - 2); + printf("%d\n", 12 * 312); + printf("%d\n", 12 / 4); + printf("%d\n", 12 % 20); + printf("%d\n", 12 & 6); + printf("%d\n", 12 ^ 7); + printf("%d\n", 12 | 8); + printf("%d\n", 12 >> 2); + printf("%d\n", 12 << 4); + printf("%d\n", ~12); + printf("%d\n", -12); + printf("%d\n", +12); + printf("%d %d %d %d\n", + isid('a'), + isid('g'), + isid('T'), + isid('(')); +} + +int isid(int c) +{ + return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z') | c == '_'; +} + +/**********************/ + +int vstack[10], *vstack_ptr; + +void vpush(int vt, int vc) +{ + *vstack_ptr++ = vt; + *vstack_ptr++ = vc; +} + +void vpop(int *ft, int *fc) +{ + *fc = *--vstack_ptr; + *ft = *--vstack_ptr; +} + +void expr2_test() +{ + int a, b; + + printf("expr2:\n"); + vstack_ptr = vstack; + vpush(1432432, 2); + vstack_ptr[-2] &= ~0xffffff80; + vpop(&a, &b); + printf("res= %d %d\n", a, b); +} + +void constant_expr_test() +{ + int a; + printf("constant_expr:\n"); + a = 3; + printf("%d\n", a * 16); + printf("%d\n", a * 1); + printf("%d\n", a + 0); +} + +int tab4[10]; + +void expr_ptr_test() +{ + int *p, *q; + int i = -1; + + printf("expr_ptr:\n"); + p = tab4; + q = tab4 + 10; + printf("diff=%d\n", q - p); + p++; + printf("inc=%d\n", p - tab4); + p--; + printf("dec=%d\n", p - tab4); + ++p; + printf("inc=%d\n", p - tab4); + --p; + printf("dec=%d\n", p - tab4); + printf("add=%d\n", p + 3 - tab4); + printf("add=%d\n", 3 + p - tab4); + + /* check if 64bit support is ok */ + q = p = 0; + q += i; + printf("%p %p %ld\n", q, p, p-q); + printf("%d %d %d %d %d %d\n", + p == q, p != q, p < q, p <= q, p >= q, p > q); + i = 0xf0000000; + p += i; + printf("%p %p %ld\n", q, p, p-q); + printf("%d %d %d %d %d %d\n", + p == q, p != q, p < q, p <= q, p >= q, p > q); + p = (int *)((char *)p + 0xf0000000); + printf("%p %p %ld\n", q, p, p-q); + printf("%d %d %d %d %d %d\n", + p == q, p != q, p < q, p <= q, p >= q, p > q); + p += 0xf0000000; + printf("%p %p %ld\n", q, p, p-q); + printf("%d %d %d %d %d %d\n", + p == q, p != q, p < q, p <= q, p >= q, p > q); + { + struct size12 { + int i, j, k; + }; + struct size12 s[2], *sp = s; + int i, j; + sp->i = 42; + sp++; + j = -1; + printf("%d\n", sp[j].i); + } +#ifdef __LP64__ + i = 1; + p = (int*)0x100000000UL + i; + i = ((long)p) >> 32; + printf("largeptr: %p %d\n", p, i); +#endif +} + +void expr_cmp_test() +{ + int a, b; + printf("constant_expr:\n"); + a = -1; + b = 1; + printf("%d\n", a == a); + printf("%d\n", a != a); + + printf("%d\n", a < b); + printf("%d\n", a <= b); + printf("%d\n", a <= a); + printf("%d\n", b >= a); + printf("%d\n", a >= a); + printf("%d\n", b > a); + + printf("%d\n", (unsigned)a < b); + printf("%d\n", (unsigned)a <= b); + printf("%d\n", (unsigned)a <= a); + printf("%d\n", (unsigned)b >= a); + printf("%d\n", (unsigned)a >= a); + printf("%d\n", (unsigned)b > a); +} + +struct empty { +}; + +struct aligntest1 { + char a[10]; +}; + +struct aligntest2 { + int a; + char b[10]; +}; + +struct aligntest3 { + double a, b; +}; + +struct aligntest4 { + double a[0]; +}; + +struct __attribute__((aligned(16))) aligntest5 +{ + int i; +}; +struct aligntest6 +{ + int i; +} __attribute__((aligned(16))); +struct aligntest7 +{ + int i; +}; +struct aligntest5 altest5[2]; +struct aligntest6 altest6[2]; +int pad1; +/* altest7 is correctly aligned to 16 bytes also with TCC, + but __alignof__ returns the wrong result (4) because we + can't store the alignment yet when specified on symbols + directly (it's stored in the type so we'd need to make + a copy of it). -- FIXED */ +struct aligntest7 altest7[2] __attribute__((aligned(16))); + +struct aligntest8 +{ + int i; +} __attribute__((aligned(4096))); + +struct Large { + unsigned long flags; + union { + void *u1; + int *u2; + }; + + struct { + union { + unsigned long index; + void *freelist; + }; + union { + unsigned long counters; + struct { + int bla; + }; + }; + }; + + union { + struct { + long u3; + long u4; + }; + void *u5; + struct { + unsigned long compound_head; + unsigned int compound_dtor; + unsigned int compound_order; + }; + }; +} __attribute__((aligned(2 * sizeof(long)))); + +typedef unsigned long long __attribute__((aligned(4))) unaligned_u64; + +struct aligntest9 { + unsigned int buf_nr; + unaligned_u64 start_lba; +}; + +struct aligntest10 { + unsigned int buf_nr; + unsigned long long start_lba; +}; + +void struct_test() +{ + struct1 *s; + union union2 u; + struct Large ls; + + printf("struct:\n"); + printf("sizes: %d %d %d %d\n", + sizeof(struct struct1), + sizeof(struct struct2), + sizeof(union union1), + sizeof(union union2)); + printf("offsets: %d\n", (int)((char*)&st1.u.v1 - (char*)&st1)); + st1.f1 = 1; + st1.f2 = 2; + st1.f3 = 3; + printf("st1: %d %d %d\n", + st1.f1, st1.f2, st1.f3); + st1.u.v1 = 1; + st1.u.v2 = 2; + printf("union1: %d\n", st1.u.v1); + u.w1 = 1; + u.w2 = 2; + printf("union2: %d\n", u.w1); + s = &st2; + s->f1 = 3; + s->f2 = 2; + s->f3 = 1; + printf("st2: %d %d %d\n", + s->f1, s->f2, s->f3); + printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1); + + /* align / size tests */ + printf("aligntest1 sizeof=%d alignof=%d\n", + sizeof(struct aligntest1), __alignof__(struct aligntest1)); + printf("aligntest2 sizeof=%d alignof=%d\n", + sizeof(struct aligntest2), __alignof__(struct aligntest2)); + printf("aligntest3 sizeof=%d alignof=%d\n", + sizeof(struct aligntest3), __alignof__(struct aligntest3)); + printf("aligntest4 sizeof=%d alignof=%d\n", + sizeof(struct aligntest4), __alignof__(struct aligntest4)); + printf("aligntest5 sizeof=%d alignof=%d\n", + sizeof(struct aligntest5), __alignof__(struct aligntest5)); + printf("aligntest6 sizeof=%d alignof=%d\n", + sizeof(struct aligntest6), __alignof__(struct aligntest6)); + printf("aligntest7 sizeof=%d alignof=%d\n", + sizeof(struct aligntest7), __alignof__(struct aligntest7)); + printf("aligntest8 sizeof=%d alignof=%d\n", + sizeof(struct aligntest8), __alignof__(struct aligntest8)); + printf("aligntest9 sizeof=%d alignof=%d\n", + sizeof(struct aligntest9), __alignof__(struct aligntest9)); + printf("aligntest10 sizeof=%d alignof=%d\n", + sizeof(struct aligntest10), __alignof__(struct aligntest10)); + printf("altest5 sizeof=%d alignof=%d\n", + sizeof(altest5), __alignof__(altest5)); + printf("altest6 sizeof=%d alignof=%d\n", + sizeof(altest6), __alignof__(altest6)); + printf("altest7 sizeof=%d alignof=%d\n", + sizeof(altest7), __alignof__(altest7)); + + /* empty structures (GCC extension) */ + printf("sizeof(struct empty) = %d\n", sizeof(struct empty)); + printf("alignof(struct empty) = %d\n", __alignof__(struct empty)); + + printf("Large: sizeof=%d\n", sizeof(ls)); + memset(&ls, 0, sizeof(ls)); + ls.compound_head = 42; + printf("Large: offsetof(compound_head)=%d\n", (int)((char*)&ls.compound_head - (char*)&ls)); +} + +/* XXX: depend on endianness */ +void char_short_test() +{ + int var1, var2; + + printf("char_short:\n"); + + var1 = 0x01020304; + var2 = 0xfffefdfc; + printf("s8=%d %d\n", + *(char *)&var1, *(char *)&var2); + printf("u8=%d %d\n", + *(unsigned char *)&var1, *(unsigned char *)&var2); + printf("s16=%d %d\n", + *(short *)&var1, *(short *)&var2); + printf("u16=%d %d\n", + *(unsigned short *)&var1, *(unsigned short *)&var2); + printf("s32=%d %d\n", + *(int *)&var1, *(int *)&var2); + printf("u32=%d %d\n", + *(unsigned int *)&var1, *(unsigned int *)&var2); + *(char *)&var1 = 0x08; + printf("var1=%x\n", var1); + *(short *)&var1 = 0x0809; + printf("var1=%x\n", var1); + *(int *)&var1 = 0x08090a0b; + printf("var1=%x\n", var1); +} + +/******************/ + +typedef struct Sym { + int v; + int t; + int c; + struct Sym *next; + struct Sym *prev; +} Sym; + +#define ISLOWER(c) ('a' <= (c) && (c) <= 'z') +#define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c)) + +static int toupper1(int a) +{ + return TOUPPER(a); +} + +static unsigned int calc_vm_flags(unsigned int prot) +{ + unsigned int prot_bits; + /* This used to segfault in some revisions: */ + prot_bits = ((0x1==0x00000001)?(prot&0x1):(prot&0x1)?0x00000001:0); + return prot_bits; +} + +void bool_test() +{ + int *s, a, b, t, f, i; + + a = 0; + s = (void*)0; + printf("!s=%d\n", !s); + + if (!s || !s[0]) + a = 1; + printf("a=%d\n", a); + + printf("a=%d %d %d\n", 0 || 0, 0 || 1, 1 || 1); + printf("a=%d %d %d\n", 0 && 0, 0 && 1, 1 && 1); + printf("a=%d %d\n", 1 ? 1 : 0, 0 ? 1 : 0); +#if 1 && 1 + printf("a1\n"); +#endif +#if 1 || 0 + printf("a2\n"); +#endif +#if 1 ? 0 : 1 + printf("a3\n"); +#endif +#if 0 ? 0 : 1 + printf("a4\n"); +#endif + + a = 4; + printf("b=%d\n", a + (0 ? 1 : a / 2)); + + /* test register spilling */ + a = 10; + b = 10; + a = (a + b) * ((a < b) ? + ((b - a) * (a - b)): a + b); + printf("a=%d\n", a); + + /* test complex || or && expressions */ + t = 1; + f = 0; + a = 32; + printf("exp=%d\n", f == (32 <= a && a <= 3)); + printf("r=%d\n", (t || f) + (t && f)); + + /* test ? : cast */ + { + int aspect_on; + int aspect_native = 65536; + double bfu_aspect = 1.0; + int aspect; + for(aspect_on = 0; aspect_on < 2; aspect_on++) { + aspect=aspect_on?(aspect_native*bfu_aspect+0.5):65535UL; + printf("aspect=%d\n", aspect); + } + } + + /* test ? : GCC extension */ + { + static int v1 = 34 ? : -1; /* constant case */ + static int v2 = 0 ? : -1; /* constant case */ + int a = 30; + + printf("%d %d\n", v1, v2); + printf("%d %d\n", a - 30 ? : a * 2, a + 1 ? : a * 2); + } + + /* again complex expression */ + for(i=0;i<256;i++) { + if (toupper1 (i) != TOUPPER (i)) + printf("error %d\n", i); + } + printf ("bits = 0x%x\n", calc_vm_flags (0x1)); +} + +extern int undefined_function(void); +extern int defined_function(void); + +static inline void refer_to_undefined(void) +{ + undefined_function(); +} + +void optimize_out(void) +{ + int i = 0 ? undefined_function() : defined_function(); + printf ("oo:%d\n", i); + int j = 1 ? defined_function() : undefined_function(); + printf ("oo:%d\n", j); + if (0) + printf("oo:%d\n", undefined_function()); + else + printf("oo:%d\n", defined_function()); + if (1) + printf("oo:%d\n", defined_function()); + else + printf("oo:%d\n", undefined_function()); + while (1) { + printf("oow:%d\n", defined_function()); + break; + printf("oow:%d\n", undefined_function()); + } + j = 1; + /* Following is a switch without {} block intentionally. */ + switch (j) + case 1: break; + printf ("oos:%d\n", defined_function()); + /* The following break shouldn't lead to disabled code after + the while. */ + while (1) + break; + printf ("ool1:%d\n", defined_function()); + /* Same for the other types of loops. */ + do + break; + while (1); + printf ("ool2:%d\n", defined_function()); + for (;;) + break; + printf ("ool3:%d\n", defined_function()); + /* Normal {} blocks without controlling statements + shouldn't reactivate code emission */ + while (1) { + { + break; + } + printf ("ool4:%d\n", undefined_function()); + } + j = 1; + while (j) { + if (j == 0) + break; /* this break shouldn't disable code outside the if. */ + printf("ool5:%d\n", defined_function()); + j--; + } + + j = 1; + while (j) { + if (1) + j--; + else + breakhere: break; + printf("ool6:%d\n", defined_function()); + goto breakhere; + } + + /* Test that constants in logical && are optimized: */ + i = 0 && undefined_function(); + i = defined_function() && 0 && undefined_function(); + if (0 && undefined_function()) + undefined_function(); + if (defined_function() && 0) + undefined_function(); + if (0 && 0) + undefined_function(); + if (defined_function() && 0 && undefined_function()) + undefined_function(); + /* The same for || : */ + i = 1 || undefined_function(); + i = defined_function() || 1 || undefined_function(); + if (1 || undefined_function()) + ; + else + undefined_function(); + if (defined_function() || 1) + ; + else + undefined_function(); + if (1 || 1) + ; + else + undefined_function(); + if (defined_function() || 1 || undefined_function()) + ; + else + undefined_function(); + + if (defined_function() && 0) + refer_to_undefined(); + + if (0) { + (void)sizeof( ({ + do { } while (0); + 0; + }) ); + undefined_function(); + } + + /* Leave the "if(1)return; printf()" in this order and last in the function */ + if (1) + return; + printf ("oor:%d\n", undefined_function()); +} + +int defined_function(void) +{ + static int i = 40; + return i++; +} + +/* GCC accepts that */ +static int tab_reinit[]; +static int tab_reinit[10]; + +//int cinit1; /* a global variable can be defined several times without error ! */ +int cinit1; +int cinit1; +int cinit1 = 0; +int *cinit2 = (int []){3, 2, 1}; + +void compound_literal_test(void) +{ + int *p, i; + char *q, *q3; + + printf("compound_test:\n"); + + p = (int []){1, 2, 3}; + for(i=0;i<3;i++) + printf(" %d", p[i]); + printf("\n"); + + for(i=0;i<3;i++) + printf("%d", cinit2[i]); + printf("\n"); + + q = "tralala1"; + printf("q1=%s\n", q); + + q = (char *){ "tralala2" }; + printf("q2=%s\n", q); + + q3 = (char *){ q }; + printf("q3=%s\n", q3); + + q = (char []){ "tralala3" }; + printf("q4=%s\n", q); + +#ifdef ALL_ISOC99 + p = (int []){1, 2, cinit1 + 3}; + for(i=0;i<3;i++) + printf(" %d", p[i]); + printf("\n"); + + for(i=0;i<3;i++) { + p = (int []){1, 2, 4 + i}; + printf("%d %d %d\n", + p[0], + p[1], + p[2]); + } +#endif +} + +/* K & R protos */ + +kr_func1(a, b) +{ + return a + b; +} + +int kr_func2(a, b) +{ + return a + b; +} + +kr_test() +{ + printf("kr_test:\n"); + printf("func1=%d\n", kr_func1(3, 4)); + printf("func2=%d\n", kr_func2(3, 4)); + return 0; +} + +void num(int n) +{ + char *tab, *p; + tab = (char*)malloc(20); + p = tab; + while (1) { + *p = 48 + (n % 10); + p++; + n = n / 10; + if (n == 0) + break; + } + while (p != tab) { + p--; + printf("%c", *p); + } + printf("\n"); + free(tab); +} + +/* structure assignment tests */ +struct structa1 { + int f1; + char f2; +}; + +struct structa1 ssta1; + +void struct_assign_test1(struct structa1 s1, int t, float f) +{ + printf("%d %d %d %f\n", s1.f1, s1.f2, t, f); +} + +struct structa1 struct_assign_test2(struct structa1 s1, int t) +{ + s1.f1 += t; + s1.f2 -= t; + return s1; +} + +void struct_assign_test(void) +{ + struct S { + struct structa1 lsta1, lsta2; + int i; + } s, *ps; + + ps = &s; + ps->i = 4; +#if 0 + printf("struct_assign_test:\n"); + + s.lsta1.f1 = 1; + s.lsta1.f2 = 2; + printf("%d %d\n", s.lsta1.f1, s.lsta1.f2); + s.lsta2 = s.lsta1; + printf("%d %d\n", s.lsta2.f1, s.lsta2.f2); +#else + s.lsta2.f1 = 1; + s.lsta2.f2 = 2; +#endif + struct_assign_test1(ps->lsta2, 3, 4.5); + + printf("before call: %d %d\n", s.lsta2.f1, s.lsta2.f2); + ps->lsta2 = struct_assign_test2(ps->lsta2, ps->i); + printf("after call: %d %d\n", ps->lsta2.f1, ps->lsta2.f2); + + static struct { + void (*elem)(); + } t[] = { + /* XXX: we should allow this even without braces */ + { struct_assign_test } + }; + printf("%d\n", struct_assign_test == t[0].elem); +} + +/* casts to short/char */ + +void cast1(char a, short b, unsigned char c, unsigned short d) +{ + printf("%d %d %d %d\n", a, b, c, d); +} + +char bcast; +short scast; + +void cast_test() +{ + int a; + char c; + char tab[10]; + unsigned b,d; + short s; + char *p = NULL; + p -= 0x700000000042; + + printf("cast_test:\n"); + a = 0xfffff; + cast1(a, a, a, a); + a = 0xffffe; + printf("%d %d %d %d\n", + (char)(a + 1), + (short)(a + 1), + (unsigned char)(a + 1), + (unsigned short)(a + 1)); + printf("%d %d %d %d\n", + (char)0xfffff, + (short)0xfffff, + (unsigned char)0xfffff, + (unsigned short)0xfffff); + + a = (bcast = 128) + 1; + printf("%d\n", a); + a = (scast = 65536) + 1; + printf("%d\n", a); + + printf("sizeof(c) = %d, sizeof((int)c) = %d\n", sizeof(c), sizeof((int)c)); + + /* test cast from unsigned to signed short to int */ + b = 0xf000; + d = (short)b; + printf("((unsigned)(short)0x%08x) = 0x%08x\n", b, d); + b = 0xf0f0; + d = (char)b; + printf("((unsigned)(char)0x%08x) = 0x%08x\n", b, d); + + /* test implicit int casting for array accesses */ + c = 0; + tab[1] = 2; + tab[c] = 1; + printf("%d %d\n", tab[0], tab[1]); + + /* test implicit casting on some operators */ + printf("sizeof(+(char)'a') = %d\n", sizeof(+(char)'a')); + printf("sizeof(-(char)'a') = %d\n", sizeof(-(char)'a')); + printf("sizeof(~(char)'a') = %d\n", sizeof(-(char)'a')); + + /* from pointer to integer types */ + printf("%d %d %ld %ld %lld %lld\n", + (int)p, (unsigned int)p, + (long)p, (unsigned long)p, + (long long)p, (unsigned long long)p); + + /* from integers to pointers */ + printf("%p %p %p %p\n", + (void *)a, (void *)b, (void *)c, (void *)d); +} + +/* initializers tests */ +struct structinit1 { + int f1; + char f2; + short f3; + int farray[3]; +}; + +int sinit1 = 2; +int sinit2 = { 3 }; +int sinit3[3] = { 1, 2, {{3}}, }; +int sinit4[3][2] = { {1, 2}, {3, 4}, {5, 6} }; +int sinit5[3][2] = { 1, 2, 3, 4, 5, 6 }; +int sinit6[] = { 1, 2, 3 }; +int sinit7[] = { [2] = 3, [0] = 1, 2 }; +char sinit8[] = "hello" "trala"; + +struct structinit1 sinit9 = { 1, 2, 3 }; +struct structinit1 sinit10 = { .f2 = 2, 3, .f1 = 1 }; +struct structinit1 sinit11 = { .f2 = 2, 3, .f1 = 1, +#ifdef ALL_ISOC99 + .farray[0] = 10, + .farray[1] = 11, + .farray[2] = 12, +#endif +}; + +char *sinit12 = "hello world"; +char *sinit13[] = { + "test1", + "test2", + "test3", +}; +char sinit14[10] = { "abc" }; +int sinit15[3] = { sizeof(sinit15), 1, 2 }; + +struct { int a[3], b; } sinit16[] = { { 1 }, 2 }; + +struct bar { + char *s; + int len; +} sinit17[] = { + "a1", 4, + "a2", 1 +}; + +int sinit18[10] = { + [2 ... 5] = 20, + 2, + [8] = 10, +}; + +struct complexinit0 { + int a; + int b; +}; + +struct complexinit { + int a; + const struct complexinit0 *b; +}; + +const static struct complexinit cix[] = { + [0] = { + .a = 2000, + .b = (const struct complexinit0[]) { + { 2001, 2002 }, + { 2003, 2003 }, + {} + } + } +}; + +struct complexinit2 { + int a; + int b[]; +}; + +struct complexinit2 cix20; + +struct complexinit2 cix21 = { + .a = 3000, + .b = { 3001, 3002, 3003 } +}; + +struct complexinit2 cix22 = { + .a = 4000, + .b = { 4001, 4002, 4003, 4004, 4005, 4006 } +}; + +typedef int arrtype1[]; +arrtype1 sinit19 = {1}; +arrtype1 sinit20 = {2,3}; +typedef int arrtype2[3]; +arrtype2 sinit21 = {4}; +arrtype2 sinit22 = {5,6,7}; + +/* Address comparisons of non-weak symbols with zero can be const-folded */ +int sinit23[2] = { "astring" ? sizeof("astring") : -1, + &sinit23 ? 42 : -1 }; + +extern int external_inited = 42; + +void init_test(void) +{ + int linit1 = 2; + int linit2 = { 3 }; + int linit4[3][2] = { {1, 2}, {3, 4}, {5, 6} }; + int linit6[] = { 1, 2, 3 }; + int i, j; + char linit8[] = "hello" "trala"; + int linit12[10] = { 1, 2 }; + int linit13[10] = { 1, 2, [7] = 3, [3] = 4, }; + char linit14[10] = "abc"; + int linit15[10] = { linit1, linit1 + 1, [6] = linit1 + 2, }; + struct linit16 { int a1, a2, a3, a4; } linit16 = { 1, .a3 = 2 }; + int linit17 = sizeof(linit17); + int zero = 0; + /* Addresses on non-weak symbols are non-zero, but not the access itself */ + int linit18[2] = {&zero ? 1 : -1, zero ? -1 : 1 }; + + printf("init_test:\n"); + + printf("sinit1=%d\n", sinit1); + printf("sinit2=%d\n", sinit2); + printf("sinit3=%d %d %d %d\n", + sizeof(sinit3), + sinit3[0], + sinit3[1], + sinit3[2] + ); + printf("sinit6=%d\n", sizeof(sinit6)); + printf("sinit7=%d %d %d %d\n", + sizeof(sinit7), + sinit7[0], + sinit7[1], + sinit7[2] + ); + printf("sinit8=%s\n", sinit8); + printf("sinit9=%d %d %d\n", + sinit9.f1, + sinit9.f2, + sinit9.f3 + ); + printf("sinit10=%d %d %d\n", + sinit10.f1, + sinit10.f2, + sinit10.f3 + ); + printf("sinit11=%d %d %d %d %d %d\n", + sinit11.f1, + sinit11.f2, + sinit11.f3, + sinit11.farray[0], + sinit11.farray[1], + sinit11.farray[2] + ); + + for(i=0;i<3;i++) + for(j=0;j<2;j++) + printf("[%d][%d] = %d %d %d\n", + i, j, sinit4[i][j], sinit5[i][j], linit4[i][j]); + printf("linit1=%d\n", linit1); + printf("linit2=%d\n", linit2); + printf("linit6=%d\n", sizeof(linit6)); + printf("linit8=%d %s\n", sizeof(linit8), linit8); + + printf("sinit12=%s\n", sinit12); + printf("sinit13=%d %s %s %s\n", + sizeof(sinit13), + sinit13[0], + sinit13[1], + sinit13[2]); + printf("sinit14=%s\n", sinit14); + + for(i=0;i<10;i++) printf(" %d", linit12[i]); + printf("\n"); + for(i=0;i<10;i++) printf(" %d", linit13[i]); + printf("\n"); + for(i=0;i<10;i++) printf(" %d", linit14[i]); + printf("\n"); + for(i=0;i<10;i++) printf(" %d", linit15[i]); + printf("\n"); + printf("%d %d %d %d\n", + linit16.a1, + linit16.a2, + linit16.a3, + linit16.a4); + /* test that initialisation is done after variable declare */ + printf("linit17=%d\n", linit17); + printf("sinit15=%d\n", sinit15[0]); + printf("sinit16=%d %d\n", sinit16[0].a[0], sinit16[1].a[0]); + printf("sinit17=%s %d %s %d\n", + sinit17[0].s, sinit17[0].len, + sinit17[1].s, sinit17[1].len); + for(i=0;i<10;i++) + printf("%x ", sinit18[i]); + printf("\n"); + /* complex init check */ + printf("cix: %d %d %d %d %d %d %d\n", + cix[0].a, + cix[0].b[0].a, cix[0].b[0].b, + cix[0].b[1].a, cix[0].b[1].b, + cix[0].b[2].a, cix[0].b[2].b); + printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]); + printf("sizeof cix20 %d, cix21 %d, sizeof cix22 %d\n", sizeof cix20, sizeof cix21, sizeof cix22); + + printf("arrtype1: %d %d %d\n", sinit19[0], sinit20[0], sinit20[1]); + printf("arrtype2: %d %d\n", sizeof(sinit19), sizeof(sinit20)); + printf("arrtype3: %d %d %d\n", sinit21[0], sinit21[1], sinit21[2]); + printf("arrtype4: %d %d %d\n", sinit22[0], sinit22[1], sinit22[2]); + printf("arrtype5: %d %d\n", sizeof(sinit21), sizeof(sinit22)); + printf("arrtype6: %d\n", sizeof(arrtype2)); + + printf("sinit23= %d %d\n", sinit23[0], sinit23[1]); + printf("linit18= %d %d\n", linit18[0], linit18[1]); +} + +void switch_uc(unsigned char uc) +{ + switch (uc) { + case 0xfb ... 0xfe: + printf("ucsw:1\n"); + break; + case 0xff: + printf("ucsw:2\n"); + break; + case 0 ... 5: + printf("ucsw:3\n"); + break; + default: + printf("ucsw: broken!\n"); + } +} + +void switch_sc(signed char sc) +{ + switch (sc) { + case -5 ... -2: + printf("scsw:1\n"); + break; + case -1: + printf("scsw:2\n"); + break; + case 0 ... 5: + printf("scsw:3\n"); + break; + default: + printf("scsw: broken!\n"); + } +} + +void switch_test() +{ + int i; + unsigned long long ull; + long long ll; + + for(i=0;i<15;i++) { + switch(i) { + case 0: + case 1: + printf("a"); + break; + default: + printf("%d", i); + break; + case 8 ... 12: + printf("c"); + break; + case 3: + printf("b"); + break; + case 0xc33c6b9fU: + case 0x7c9eeeb9U: + break; + } + } + printf("\n"); + + for (i = 1; i <= 5; i++) { + ull = (unsigned long long)i << 61; + switch (ull) { + case 1ULL << 61: + printf("ullsw:1\n"); + break; + case 2ULL << 61: + printf("ullsw:2\n"); + break; + case 3ULL << 61: + printf("ullsw:3\n"); + break; + case 4ULL << 61: + printf("ullsw:4\n"); + break; + case 5ULL << 61: + printf("ullsw:5\n"); + break; + default: + printf("ullsw: broken!\n"); + } + } + + for (i = 1; i <= 5; i++) { + ll = (long long)i << 61; + switch (ll) { + case 1LL << 61: + printf("llsw:1\n"); + break; + case 2LL << 61: + printf("llsw:2\n"); + break; + case 3LL << 61: + printf("llsw:3\n"); + break; + case 4LL << 61: + printf("llsw:4\n"); + break; + case 5LL << 61: + printf("llsw:5\n"); + break; + default: + printf("llsw: broken!\n"); + } + } + + for (i = -5; i <= 5; i++) { + switch_uc((unsigned char)i); + } + + for (i = -5; i <= 5; i++) { + switch_sc ((signed char)i); + } +} + +/* ISOC99 _Bool type */ +void c99_bool_test(void) +{ +#ifdef BOOL_ISOC99 + int a; + _Bool b; + + printf("bool_test:\n"); + printf("sizeof(_Bool) = %d\n", sizeof(_Bool)); + a = 3; + printf("cast: %d %d %d\n", (_Bool)10, (_Bool)0, (_Bool)a); + b = 3; + printf("b = %d\n", b); + b++; + printf("b = %d\n", b); +#endif +} + +void bitfield_test(void) +{ + int a; + short sa; + unsigned char ca; + struct sbf1 { + int f1 : 3; + int : 2; + int f2 : 1; + int : 0; + int f3 : 5; + int f4 : 7; + unsigned int f5 : 7; + } st1; + printf("bitfield_test:"); + printf("sizeof(st1) = %d\n", sizeof(st1)); + + st1.f1 = 3; + st1.f2 = 1; + st1.f3 = 15; + a = 120; + st1.f4 = a; + st1.f5 = a; + st1.f5++; + printf("%d %d %d %d %d\n", + st1.f1, st1.f2, st1.f3, st1.f4, st1.f5); + sa = st1.f5; + ca = st1.f5; + printf("%d %d\n", sa, ca); + + st1.f1 = 7; + if (st1.f1 == -1) + printf("st1.f1 == -1\n"); + else + printf("st1.f1 != -1\n"); + if (st1.f2 == -1) + printf("st1.f2 == -1\n"); + else + printf("st1.f2 != -1\n"); + + struct sbf2 { + long long f1 : 45; + long long : 2; + long long f2 : 35; + unsigned long long f3 : 38; + } st2; + st2.f1 = 0x123456789ULL; + a = 120; + st2.f2 = (long long)a << 25; + st2.f3 = a; + st2.f2++; + printf("%lld %lld %lld\n", st2.f1, st2.f2, st2.f3); + +#if 0 + Disabled for now until further clarification re GCC compatibility + struct sbf3 { + int f1 : 7; + int f2 : 1; + char f3; + int f4 : 8; + int f5 : 1; + int f6 : 16; + } st3; + printf("sizeof(st3) = %d\n", sizeof(st3)); +#endif + + struct sbf4 { + int x : 31; + char y : 2; + } st4; + st4.y = 1; + printf("st4.y == %d\n", st4.y); + struct sbf5 { + int a; + char b; + int x : 12, y : 4, : 0, : 4, z : 3; + char c; + } st5 = { 1, 2, 3, 4, -3, 6 }; + printf("st5 = %d %d %d %d %d %d\n", st5.a, st5.b, st5.x, st5.y, st5.z, st5.c); + struct sbf6 { + short x : 12; + unsigned char y : 2; + } st6; + st6.y = 1; + printf("st6.y == %d\n", st6.y); +} + +#ifdef __x86_64__ +#define FLOAT_FMT "%f\n" +#else +/* x86's float isn't compatible with GCC */ +#define FLOAT_FMT "%.5f\n" +#endif + +/* declare strto* functions as they are C99 */ +double strtod(const char *nptr, char **endptr); + +#if defined(_WIN32) +float strtof(const char *nptr, char **endptr) {return (float)strtod(nptr, endptr);} +LONG_DOUBLE strtold(const char *nptr, char **endptr) {return (LONG_DOUBLE)strtod(nptr, endptr);} +#else +float strtof(const char *nptr, char **endptr); +LONG_DOUBLE strtold(const char *nptr, char **endptr); +#endif + +#define FTEST(prefix, typename, type, fmt)\ +void prefix ## cmp(type a, type b)\ +{\ + printf("%d %d %d %d %d %d\n",\ + a == b,\ + a != b,\ + a < b,\ + a > b,\ + a >= b,\ + a <= b);\ + printf(fmt " " fmt " " fmt " " fmt " " fmt " " fmt " " fmt "\n",\ + a,\ + b,\ + a + b,\ + a - b,\ + a * b,\ + a / b,\ + -a);\ + printf(fmt "\n", ++a);\ + printf(fmt "\n", a++);\ + printf(fmt "\n", a);\ + b = 0;\ + printf("%d %d\n", !a, !b);\ +}\ +void prefix ## fcast(type a)\ +{\ + float fa;\ + double da;\ + LONG_DOUBLE la;\ + int ia;\ + long long llia;\ + unsigned int ua;\ + unsigned long long llua;\ + type b;\ + fa = a;\ + da = a;\ + la = a;\ + printf("ftof: %f %f %Lf\n", fa, da, la);\ + ia = (int)a;\ + llia = (long long)a;\ + a = (a >= 0) ? a : -a;\ + ua = (unsigned int)a;\ + llua = (unsigned long long)a;\ + printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\ + ia = -1234;\ + ua = 0x81234500;\ + llia = -0x123456789012345LL;\ + llua = 0xf123456789012345LLU;\ + b = ia;\ + printf("itof: " fmt "\n", b);\ + b = ua;\ + printf("utof: " fmt "\n", b);\ + b = llia;\ + printf("lltof: " fmt "\n", b);\ + b = llua;\ + printf("ulltof: " fmt "\n", b);\ +}\ +\ +float prefix ## retf(type a) { return a; }\ +double prefix ## retd(type a) { return a; }\ +LONG_DOUBLE prefix ## retld(type a) { return a; }\ +\ +void prefix ## call(void)\ +{\ + printf("float: " FLOAT_FMT, prefix ## retf(42.123456789));\ + printf("double: %f\n", prefix ## retd(42.123456789));\ + printf("long double: %Lf\n", prefix ## retld(42.123456789));\ + printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\ +}\ +\ +void prefix ## signed_zeros(void) \ +{\ + type x = 0.0, y = -0.0, n, p;\ + if (x == y)\ + printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / y);\ + else\ + printf ("x != y; this is wrong!\n");\ +\ + n = -x;\ + if (x == n)\ + printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / n);\ + else\ + printf ("x != -x; this is wrong!\n");\ +\ + p = +y;\ + if (x == p)\ + printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / p);\ + else\ + printf ("x != +y; this is wrong!\n");\ + p = -y;\ + if (x == p)\ + printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\ + 1.0 / x != 1.0 / p);\ + else\ + printf ("x != -y; this is wrong!\n");\ +}\ +void prefix ## test(void)\ +{\ + printf("testing '%s'\n", #typename);\ + prefix ## cmp(1, 2.5);\ + prefix ## cmp(2, 1.5);\ + prefix ## cmp(1, 1);\ + prefix ## fcast(234.6);\ + prefix ## fcast(-2334.6);\ + prefix ## call();\ + prefix ## signed_zeros();\ +} + +FTEST(f, float, float, "%f") +FTEST(d, double, double, "%f") +FTEST(ld, long double, LONG_DOUBLE, "%Lf") + +double ftab1[3] = { 1.2, 3.4, -5.6 }; + + +void float_test(void) +{ +#if !defined(__arm__) || defined(__ARM_PCS_VFP) + float fa, fb; + double da, db; + int a; + unsigned int b; + + printf("float_test:\n"); + printf("sizeof(float) = %d\n", sizeof(float)); + printf("sizeof(double) = %d\n", sizeof(double)); + printf("sizeof(long double) = %d\n", sizeof(LONG_DOUBLE)); + ftest(); + dtest(); + ldtest(); + printf("%f %f %f\n", ftab1[0], ftab1[1], ftab1[2]); + printf("%f %f %f\n", 2.12, .5, 2.3e10); + // printf("%f %f %f\n", 0x1234p12, 0x1e23.23p10, 0x12dp-10); + da = 123; + printf("da=%f\n", da); + fa = 123; + printf("fa=%f\n", fa); + a = 4000000000; + da = a; + printf("da = %f\n", da); + b = 4000000000; + db = b; + printf("db = %f\n", db); +#endif +} + +int fib(int n) +{ + if (n <= 2) + return 1; + else + return fib(n-1) + fib(n-2); +} + +void funcptr_test() +{ + void (*func)(int); + int a; + struct { + int dummy; + void (*func)(int); + } st1; + long diff; + + printf("funcptr:\n"); + func = # + (*func)(12345); + func = num; + a = 1; + a = 1; + func(12345); + /* more complicated pointer computation */ + st1.func = num; + st1.func(12346); + printf("sizeof1 = %d\n", sizeof(funcptr_test)); + printf("sizeof2 = %d\n", sizeof funcptr_test); + printf("sizeof3 = %d\n", sizeof(&funcptr_test)); + printf("sizeof4 = %d\n", sizeof &funcptr_test); + a = 0; + func = num + a; + diff = func - num; + func(42); + (func + diff)(42); + (num + a)(43); +} + +void lloptest(long long a, long long b) +{ + unsigned long long ua, ub; + + ua = a; + ub = b; + /* arith */ + printf("arith: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", + a + b, + a - b, + a * b); + + if (b != 0) { + printf("arith1: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", + a / b, + a % b); + } + + /* binary */ + printf("bin: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", + a & b, + a | b, + a ^ b); + + /* tests */ + printf("test: %d %d %d %d %d %d\n", + a == b, + a != b, + a < b, + a > b, + a >= b, + a <= b); + + printf("utest: %d %d %d %d %d %d\n", + ua == ub, + ua != ub, + ua < ub, + ua > ub, + ua >= ub, + ua <= ub); + + /* arith2 */ + a++; + b++; + printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b); + printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a++, b++); + printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", --a, --b); + printf("arith2: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b); + b = ub = 0; + printf("not: %d %d %d %d\n", !a, !ua, !b, !ub); +} + +void llshift(long long a, int b) +{ + printf("shift: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", + (unsigned long long)a >> b, + a >> b, + a << b); + printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", + (unsigned long long)a >> 3, + a >> 3, + a << 3); + printf("shiftc: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", + (unsigned long long)a >> 35, + a >> 35, + a << 35); +} + +void llfloat(void) +{ + float fa; + double da; + LONG_DOUBLE lda; + long long la, lb, lc; + unsigned long long ula, ulb, ulc; + la = 0x12345678; + ula = 0x72345678; + la = (la << 20) | 0x12345; + ula = ula << 33; + printf("la=" LONG_LONG_FORMAT " ula=" ULONG_LONG_FORMAT "\n", la, ula); + + fa = la; + da = la; + lda = la; + printf("lltof: %f %f %Lf\n", fa, da, lda); + + la = fa; + lb = da; + lc = lda; + printf("ftoll: " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", la, lb, lc); + + fa = ula; + da = ula; + lda = ula; + printf("ulltof: %f %f %Lf\n", fa, da, lda); + + ula = fa; + ulb = da; + ulc = lda; + printf("ftoull: " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT " " ULONG_LONG_FORMAT "\n", ula, ulb, ulc); +} + +long long llfunc1(int a) +{ + return a * 2; +} + +struct S { + int id; + char item; +}; + +long long int value(struct S *v) +{ + return ((long long int)v->item); +} + +long long llfunc2(long long x, long long y, int z) +{ + return x * y * z; +} + +void longlong_test(void) +{ + long long a, b, c; + int ia; + unsigned int ua; + printf("longlong_test:\n"); + printf("sizeof(long long) = %d\n", sizeof(long long)); + ia = -1; + ua = -2; + a = ia; + b = ua; + printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT "\n", a, b); + printf(LONG_LONG_FORMAT " " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %Lx\n", + (long long)1, + (long long)-2, + 1LL, + 0x1234567812345679); + a = llfunc1(-3); + printf(LONG_LONG_FORMAT "\n", a); + + lloptest(1000, 23); + lloptest(0xff, 0x1234); + b = 0x72345678 << 10; + lloptest(-3, b); + llshift(0x123, 5); + llshift(-23, 5); + b = 0x72345678LL << 10; + llshift(b, 47); + + llfloat(); +#if 1 + b = 0x12345678; + a = -1; + c = a + b; + printf("%Lx\n", c); +#endif + + /* long long reg spill test */ + { + struct S a; + + a.item = 3; + printf("%lld\n", value(&a)); + } + lloptest(0x80000000, 0); + + { + long long *p, v, **pp; + v = 1; + p = &v; + p[0]++; + printf("another long long spill test : %lld\n", *p); + pp = &p; + + v = llfunc2(**pp, **pp, ia); + printf("a long long function (arm-)reg-args test : %lld\n", v); + } + a = 68719476720LL; + b = 4294967295LL; + printf("%d %d %d %d\n", a > b, a < b, a >= b, a <= b); + + printf(LONG_LONG_FORMAT "\n", 0x123456789LLU); + + /* long long pointer deref in argument passing test */ + a = 0x123; + long long *p = &a; + llshift(*p, 5); +} + +void manyarg_test(void) +{ + LONG_DOUBLE ld = 1234567891234LL; + printf("manyarg_test:\n"); + printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0); + printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + 1234567891234LL, 987654321986LL, + 42.0, 43.0); + printf("%Lf %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f\n", + ld, 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + 1234567891234LL, 987654321986LL, + 42.0, 43.0); + printf("%d %d %d %d %d %d %d %d %Lf\n", + 1, 2, 3, 4, 5, 6, 7, 8, ld); + printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + LONG_LONG_FORMAT " " LONG_LONG_FORMAT "%f %f %Lf\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + 1234567891234LL, 987654321986LL, + 42.0, 43.0, ld); + printf("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + "%Lf " LONG_LONG_FORMAT " " LONG_LONG_FORMAT " %f %f %Lf\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + ld, 1234567891234LL, 987654321986LL, + 42.0, 43.0, ld); +} + +void vprintf1(const char *fmt, ...) +{ + va_list ap, aq; + const char *p; + int c, i; + double d; + long long ll; + LONG_DOUBLE ld; + + va_start(aq, fmt); + va_copy(ap, aq); + + p = fmt; + for(;;) { + c = *p; + if (c == '\0') + break; + p++; + if (c == '%') { + c = *p; + switch(c) { + case '\0': + goto the_end; + case 'd': + i = va_arg(ap, int); + printf("%d", i); + break; + case 'f': + d = va_arg(ap, double); + printf("%f", d); + break; + case 'l': + ll = va_arg(ap, long long); + printf(LONG_LONG_FORMAT, ll); + break; + case 'F': + ld = va_arg(ap, LONG_DOUBLE); + printf("%Lf", ld); + break; + } + p++; + } else { + putchar(c); + } + } + the_end: + va_end(aq); + va_end(ap); +} + +struct myspace { + short int profile; +}; + +void stdarg_for_struct(struct myspace bob, ...) +{ + struct myspace george, bill; + va_list ap; + short int validate; + + va_start(ap, bob); + bill = va_arg(ap, struct myspace); + george = va_arg(ap, struct myspace); + validate = va_arg(ap, int); + printf("stdarg_for_struct: %d %d %d %d\n", + bob.profile, bill.profile, george.profile, validate); + va_end(ap); +} + +void stdarg_for_libc(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); +} + +void stdarg_test(void) +{ + LONG_DOUBLE ld = 1234567891234LL; + struct myspace bob; + + vprintf1("%d %d %d\n", 1, 2, 3); + vprintf1("%f %d %f\n", 1.0, 2, 3.0); + vprintf1("%l %l %d %f\n", 1234567891234LL, 987654321986LL, 3, 1234.0); + vprintf1("%F %F %F\n", LONG_DOUBLE_LITERAL(1.2), LONG_DOUBLE_LITERAL(2.3), LONG_DOUBLE_LITERAL(3.4)); + vprintf1("%d %f %l %F %d %f %l %F\n", + 1, 1.2, 3LL, LONG_DOUBLE_LITERAL(4.5), 6, 7.8, 9LL, LONG_DOUBLE_LITERAL(0.1)); + vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8); + vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0); + vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + "%l %l %f %f\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + 1234567891234LL, 987654321986LL, + 42.0, 43.0); + vprintf1("%F %d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + "%l %l %f %f\n", + ld, 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + 1234567891234LL, 987654321986LL, + 42.0, 43.0); + vprintf1("%d %d %d %d %d %d %d %d %F\n", + 1, 2, 3, 4, 5, 6, 7, 8, ld); + vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + "%l %l %f %f %F\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + 1234567891234LL, 987654321986LL, + 42.0, 43.0, ld); + vprintf1("%d %d %d %d %d %d %d %d %f %f %f %f %f %f %f %f %f %f " + "%F %l %l %f %f %F\n", + 1, 2, 3, 4, 5, 6, 7, 8, + 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, + ld, 1234567891234LL, 987654321986LL, + 42.0, 43.0, ld); + + bob.profile = 42; + stdarg_for_struct(bob, bob, bob, bob.profile); + stdarg_for_libc("stdarg_for_libc: %s %.2f %d\n", "string", 1.23, 456); +} + +void whitespace_test(void) +{ + char *str; + + #if 1 + pri\ +ntf("whitspace:\n"); +#endif + pf("N=%d\n", 2); + +#ifdef CORRECT_CR_HANDLING + pri\ +ntf("aaa=%d\n", 3); +#endif + + pri\ +\ +ntf("min=%d\n", 4); + +#ifdef ACCEPT_CR_IN_STRINGS + printf("len1=%d\n", strlen(" +")); +#ifdef CORRECT_CR_HANDLING + str = " +"; + printf("len1=%d str[0]=%d\n", strlen(str), str[0]); +#endif + printf("len1=%d\n", strlen(" a +")); +#endif /* ACCEPT_CR_IN_STRINGS */ +} + +int reltab[3] = { 1, 2, 3 }; + +int *rel1 = &reltab[1]; +int *rel2 = &reltab[2]; + +#ifdef _WIN64 +void relocation_test(void) {} +#else +void getmyaddress(void) +{ + printf("in getmyaddress\n"); +} + +#ifdef __LP64__ +long __pa_symbol(void) +{ + /* This 64bit constant was handled incorrectly, it was used as addend + (which can hold 64bit just fine) in connection with a symbol, + and TCC generates wrong code for that (displacements are 32bit only). + This effectively is "+ 0x80000000", and if addresses of globals + are below 2GB the result should be a number without high 32 bits set. */ + return ((long)(((unsigned long)(&rel1))) - (0xffffffff80000000UL)); +} +#endif + +unsigned long theaddress = (unsigned long)getmyaddress; +void relocation_test(void) +{ + void (*fptr)(void) = (void (*)(void))theaddress; + printf("*rel1=%d\n", *rel1); + printf("*rel2=%d\n", *rel2); + fptr(); +#ifdef __LP64__ + printf("pa_symbol=0x%lx\n", __pa_symbol() >> 63); +#endif +} +#endif + +void old_style_f(a,b,c) + int a, b; + double c; +{ + printf("a=%d b=%d b=%f\n", a, b, c); +} + +void decl_func1(int cmpfn()) +{ + printf("cmpfn=%lx\n", (long)cmpfn); +} + +void decl_func2(cmpfn) +int cmpfn(); +{ + printf("cmpfn=%lx\n", (long)cmpfn); +} + +void old_style_function(void) +{ + old_style_f((void *)1, 2, 3.0); + decl_func1(NULL); + decl_func2(NULL); +} + +void alloca_test() +{ +#if defined __i386__ || defined __x86_64__ || defined __arm__ + char *p = alloca(16); + strcpy(p,"123456789012345"); + printf("alloca: p is %s\n", p); + char *demo = "This is only a test.\n"; + /* Test alloca embedded in a larger expression */ + printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) ); +#endif +} + +void *bounds_checking_is_enabled() +{ + char ca[10], *cp = ca-1; + return (ca != cp + 1) ? cp : NULL; +} + +typedef int constant_negative_array_size_as_compile_time_assertion_idiom[(1 ? 2 : 0) - 1]; + +void c99_vla_test(int size1, int size2) +{ +#if defined __i386__ || defined __x86_64__ + int size = size1 * size2; + int tab1[size][2], tab2[10][2]; + void *tab1_ptr, *tab2_ptr, *bad_ptr; + + /* "size" should have been 'captured' at tab1 declaration, + so modifying it should have no effect on VLA behaviour. */ + size = size-1; + + printf("Test C99 VLA 1 (sizeof): "); + printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED"); + tab1_ptr = tab1; + tab2_ptr = tab2; + printf("Test C99 VLA 2 (ptrs subtract): "); + printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED"); + printf("Test C99 VLA 3 (ptr add): "); + printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED"); + printf("Test C99 VLA 4 (ptr access): "); + tab1[size1][1] = 42; + printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED"); + + printf("Test C99 VLA 5 (bounds checking (might be disabled)): "); + if (bad_ptr = bounds_checking_is_enabled()) { + int *t1 = &tab1[size1 * size2 - 1][3]; + int *t2 = &tab2[9][3]; + printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED"); + printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED"); + + char*c1 = 1 + sizeof(tab1) + (char*)tab1; + char*c2 = 1 + sizeof(tab2) + (char*)tab2; + printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED"); + printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED"); + + int *i1 = tab1[-1]; + int *i2 = tab2[-1]; + printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED"); + printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED"); + + int *x1 = tab1[size1 * size2 + 1]; + int *x2 = tab2[10 + 1]; + printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED"); + printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED"); + } else { + printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED "); + } + printf("\n"); +#endif +} + +#ifndef __TINYC__ +typedef __SIZE_TYPE__ uintptr_t; +#endif + +void sizeof_test(void) +{ + int a; + int **ptr; + + printf("sizeof(int) = %d\n", sizeof(int)); + printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int)); + printf("sizeof(long) = %d\n", sizeof(long)); + printf("sizeof(unsigned long) = %d\n", sizeof(unsigned long)); + printf("sizeof(short) = %d\n", sizeof(short)); + printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short)); + printf("sizeof(char) = %d\n", sizeof(char)); + printf("sizeof(unsigned char) = %d\n", sizeof(unsigned char)); + printf("sizeof(func) = %d\n", sizeof sizeof_test()); + a = 1; + printf("sizeof(a++) = %d\n", sizeof a++); + printf("a=%d\n", a); + ptr = NULL; + printf("sizeof(**ptr) = %d\n", sizeof (**ptr)); + + /* The type of sizeof should be as large as a pointer, actually + it should be size_t. */ + printf("sizeof(sizeof(int) = %d\n", sizeof(sizeof(int))); + uintptr_t t = 1; + uintptr_t t2; + /* Effectively <<32, but defined also on 32bit machines. */ + t <<= 16; + t <<= 16; + t++; + /* This checks that sizeof really can be used to manipulate + uintptr_t objects, without truncation. */ + t2 = t & -sizeof(uintptr_t); + printf ("%lu %lu\n", t, t2); + + /* some alignof tests */ + printf("__alignof__(int) = %d\n", __alignof__(int)); + printf("__alignof__(unsigned int) = %d\n", __alignof__(unsigned int)); + printf("__alignof__(short) = %d\n", __alignof__(short)); + printf("__alignof__(unsigned short) = %d\n", __alignof__(unsigned short)); + printf("__alignof__(char) = %d\n", __alignof__(char)); + printf("__alignof__(unsigned char) = %d\n", __alignof__(unsigned char)); + printf("__alignof__(func) = %d\n", __alignof__ sizeof_test()); + + /* sizes of VLAs need to be evaluated even inside sizeof: */ + a = 2; + printf("sizeof(char[1+2*a]) = %d\n", sizeof(char[1+2*a])); + /* And checking if sizeof compound literal works. Parenthesized: */ + printf("sizeof( (struct {int i; int j;}){4,5} ) = %d\n", + sizeof( (struct {int i; int j;}){4,5} )); + /* And as direct sizeof argument (as unary expression): */ + printf("sizeof (struct {short i; short j;}){4,5} = %d\n", + sizeof (struct {short i; short j;}){4,5} ); + + /* sizeof(x && y) should be sizeof(int), even if constant + evaluating is possible. */ + printf("sizeof(t && 0) = %d\n", sizeof(t && 0)); + printf("sizeof(1 && 1) = %d\n", sizeof(1 && 1)); + printf("sizeof(t || 1) = %d\n", sizeof(t || 1)); + printf("sizeof(0 || 0) = %d\n", sizeof(0 || 0)); +} + +void typeof_test(void) +{ + double a; + typeof(a) b; + typeof(float) c; + + a = 1.5; + b = 2.5; + c = 3.5; + printf("a=%f b=%f c=%f\n", a, b, c); +} + + +struct hlist_node; +struct hlist_head { + struct hlist_node *first, *last; +}; + +void consume_ulong (unsigned long i) +{ + i = 0; +} + +void statement_expr_test(void) +{ + int a, i; + + /* Basic stmt expr test */ + a = 0; + for(i=0;i<10;i++) { + a += 1 + + ( { int b, j; + b = 0; + for(j=0;j<5;j++) + b += j; b; + } ); + } + printf("a=%d\n", a); + + /* Test that symbols aren't freed prematurely. + With SYM_DEBUG valgrind will show a read from a freed + symbol, and tcc will show an (invalid) warning on the initialization + of 'ptr' below, if symbols are popped after the stmt expr. */ + void *v = (void*)39; + typeof(({ + (struct hlist_node *)v; + })) x; + typeof (x) + ptr = (struct hlist_node *)v; + + /* This part used to segfault when symbols were popped prematurely. + The symbols for the static local would be overwritten with + helper symbols from the pre-processor expansions in between. */ +#define some_attr __attribute__((aligned(1))) +#define tps(str) ({ \ + static const char *t some_attr = str; \ + t; \ + }) + printf ("stmtexpr: %s %s\n", + tps("somerandomlongstring"), + tps("anotherlongstring")); + + /* Test that the three decls of 't' don't interact. */ + int t = 40; + int b = ({ int t = 41; t; }); + int c = ({ int t = 42; t; }); + + /* Test that aggregate return values work. */ + struct hlist_head h + = ({ + typedef struct hlist_head T; + long pre = 48; + T t = { (void*)43, (void*)44 }; + long post = 49; + t; + }); + printf ("stmtexpr: %d %d %d\n", t, b, c); + printf ("stmtexpr: %ld %ld\n", (long)h.first, (long)h.last); + + /* Test that we can give out addresses of local labels. */ + consume_ulong(({ __label__ __here; __here: (unsigned long)&&__here; })); +} + +void local_label_test(void) +{ + int a; + goto l1; + l2: + a = 1 + ({ + __label__ l1, l2, l3, l4; + goto l1; + l4: + printf("aa1\n"); + goto l3; + l2: + printf("aa3\n"); + goto l4; + l1: + printf("aa2\n"); + goto l2; + l3:; + 1; + }); + printf("a=%d\n", a); + return; + l4: + printf("bb1\n"); + goto l2; + l1: + printf("bb2\n"); + goto l4; +} + +/* inline assembler test */ +#if defined(__i386__) || defined(__x86_64__) + +/* from linux kernel */ +static char * strncat1(char * dest,const char * src,size_t count) +{ +long d0, d1, d2, d3; +__asm__ __volatile__( + "repne\n\t" + "scasb\n\t" + "dec %1\n\t" + "mov %8,%3\n" + "1:\tdec %3\n\t" + "js 2f\n\t" + "lodsb\n\t" + "stosb\n\t" + "testb %%al,%%al\n\t" + "jne 1b\n" + "2:\txor %2,%2\n\t" + "stosb" + : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3) + : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count) + : "memory"); +return dest; +} + +static char * strncat2(char * dest,const char * src,size_t count) +{ +long d0, d1, d2, d3; +__asm__ __volatile__( + "repne scasb\n\t" /* one-line repne prefix + string op */ + "dec %1\n\t" + "mov %8,%3\n" + "1:\tdec %3\n\t" + "js 2f\n\t" + "lodsb\n\t" + "stosb\n\t" + "testb %%al,%%al\n\t" + "jne 1b\n" + "2:\txor %2,%2\n\t" + "stosb" + : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3) + : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count) + : "memory"); +return dest; +} + +static inline void * memcpy1(void * to, const void * from, size_t n) +{ +long d0, d1, d2; +__asm__ __volatile__( + "rep ; movsl\n\t" + "testb $2,%b4\n\t" + "je 1f\n\t" + "movsw\n" + "1:\ttestb $1,%b4\n\t" + "je 2f\n\t" + "movsb\n" + "2:" + : "=&c" (d0), "=&D" (d1), "=&S" (d2) + :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from) + : "memory"); +return (to); +} + +static inline void * memcpy2(void * to, const void * from, size_t n) +{ +long d0, d1, d2; +__asm__ __volatile__( + "rep movsl\n\t" /* one-line rep prefix + string op */ + "testb $2,%b4\n\t" + "je 1f\n\t" + "movsw\n" + "1:\ttestb $1,%b4\n\t" + "je 2f\n\t" + "movsb\n" + "2:" + : "=&c" (d0), "=&D" (d1), "=&S" (d2) + :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from) + : "memory"); +return (to); +} + +static __inline__ void sigaddset1(unsigned int *set, int _sig) +{ + __asm__("btsl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc"); +} + +static __inline__ void sigdelset1(unsigned int *set, int _sig) +{ + asm("btrl %1,%0" : "=m"(*set) : "Ir"(_sig - 1) : "cc", "flags"); +} + +static __inline__ __const__ unsigned int swab32(unsigned int x) +{ + __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */ + "rorl $16,%0\n\t" /* swap words */ + "xchgb %b0,%h0" /* swap higher bytes */ + :"=" "q" (x) + : "0" (x)); + return x; +} + +static __inline__ unsigned long long mul64(unsigned int a, unsigned int b) +{ + unsigned long long res; +#ifdef __x86_64__ + /* Using the A constraint is wrong (it means rdx:rax, which is too large) + but still test the 32bit->64bit mull. */ + unsigned int resh, resl; + __asm__("mull %2" : "=a" (resl), "=d" (resh) : "a" (a), "r" (b)); + res = ((unsigned long long)resh << 32) | resl; +#else + __asm__("mull %2" : "=A" (res) : "a" (a), "r" (b)); +#endif + return res; +} + +static __inline__ unsigned long long inc64(unsigned long long a) +{ + unsigned long long res; +#ifdef __x86_64__ + /* Using the A constraint is wrong, and increments are tested + elsewhere. */ + res = a + 1; +#else + __asm__("addl $1, %%eax ; adcl $0, %%edx" : "=A" (res) : "A" (a)); +#endif + return res; +} + +struct struct123 { + int a; + int b; +}; +struct struct1231 { + unsigned long addr; +}; + +unsigned long mconstraint_test(struct struct1231 *r) +{ + unsigned long ret; + unsigned int a[2]; + a[0] = 0; + __asm__ volatile ("lea %2,%0; movl 4(%0),%k0; addl %2,%k0; movl $51,%2; movl $52,4%2; movl $63,%1" + : "=&r" (ret), "=m" (a) + : "m" (*(struct struct123 *)r->addr)); + return ret + a[0]; +} + +#ifdef __x86_64__ +int fls64(unsigned long long x) +{ + int bitpos = -1; + asm("bsrq %1,%q0" + : "+r" (bitpos) + : "rm" (x)); + return bitpos + 1; +} +#endif + +void other_constraints_test(void) +{ + unsigned long ret; + int var; +#ifndef _WIN64 + __asm__ volatile ("mov %P1,%0" : "=r" (ret) : "p" (&var)); + printf ("oc1: %d\n", ret == (unsigned long)&var); +#endif +} + +#ifndef _WIN32 +/* Test global asm blocks playing with aliases. */ +void base_func(void) +{ + printf ("asmc: base\n"); +} + +extern void override_func1 (void); +extern void override_func2 (void); + +asm(".weak override_func1\n.set override_func1, base_func"); +asm(".set override_func1, base_func"); +asm(".set override_func2, base_func"); + +void override_func2 (void) +{ + printf ("asmc: override2\n"); +} + +/* This checks a construct used by the linux kernel to encode + references to strings by PC relative references. */ +extern int bug_table[] __attribute__((section("__bug_table"))); +char * get_asm_string (void) +{ + extern int some_symbol; + asm volatile (".globl some_symbol\n" + "jmp .+6\n" + "1:\n" + "some_symbol: .long 0\n" + ".pushsection __bug_table, \"a\"\n" + ".globl bug_table\n" + "bug_table:\n" + /* The first entry (1b-2b) is unused in this test, + but we include it to check if cross-section + PC-relative references work. */ + "2:\t.long 1b - 2b, %c0 - 2b\n" + ".popsection\n" : : "i" ("A string")); + char * str = ((char*)bug_table) + bug_table[1]; + return str; +} + +/* This checks another constructs with local labels. */ +extern unsigned char alld_stuff[]; +asm(".data\n" + ".byte 41\n" + "alld_stuff:\n" + "661:\n" + ".byte 42\n" + "662:\n" + ".pushsection .data.ignore\n" + ".long 661b - .\n" /* This reference to 661 generates an external sym + which shouldn't somehow overwrite the offset that's + already determined for it. */ + ".popsection\n" + ".byte 662b - 661b\n" /* So that this value is undeniably 1. */); + +void asm_local_label_diff (void) +{ + printf ("asm_local_label_diff: %d %d\n", alld_stuff[0], alld_stuff[1]); +} + +/* This checks that static local variables are available from assembler. */ +void asm_local_statics (void) +{ + static int localint = 41; + asm("incl %0" : "+m" (localint)); + printf ("asm_local_statics: %d\n", localint); +} +#endif + +static +unsigned int set; + +void fancy_copy (unsigned *in, unsigned *out) +{ + asm volatile ("" : "=r" (*out) : "0" (*in)); +} + +void fancy_copy2 (unsigned *in, unsigned *out) +{ + asm volatile ("mov %0,(%1)" : : "r" (*in), "r" (out) : "memory"); +} + +#if defined __x86_64__ && !defined _WIN64 +void clobber_r12(void) +{ + asm volatile("mov $1, %%r12" ::: "r12"); +} +#endif + +void test_high_clobbers(void) +{ +#if defined __x86_64__ && !defined _WIN64 + register long val asm("r12"); + long val2; + /* This tests if asm clobbers correctly save/restore callee saved + registers if they are clobbered and if it's the high 8 x86-64 + registers. This is fragile for GCC as the constraints do not + correctly capture the data flow, but good enough for us. */ + asm volatile("mov $0x4542, %%r12" : "=r" (val):: "memory"); + clobber_r12(); + asm volatile("mov %%r12, %0" : "=r" (val2) : "r" (val): "memory"); + printf("asmhc: 0x%x\n", val2); +#endif +} + +static long cpu_number; +void trace_console(long len, long len2) +{ +#ifdef __x86_64__ + /* This generated invalid code when the emission of the switch + table isn't disabled. The asms are necessary to show the bug, + normal statements don't work (they need to generate some code + even under nocode_wanted, which normal statements don't do, + but asms do). Also at least these number of cases is necessary + to generate enough "random" bytes. They ultimately are enough + to create invalid instruction patterns to which the first + skip-to-decision-table jump jumps. If decision table emission + is disabled all of this is no problem. + + It also is necessary that the switches are in a statement expression + (which has the property of not being enterable from outside. no + matter what). */ + if (0 + && + ({ + long pscr_ret__; + switch(len) { + case 4: + { + long pfo_ret__; + switch (len2) { + case 8: printf("bla"); pfo_ret__ = 42; break; + } + pscr_ret__ = pfo_ret__; + } + break; + case 8: + { + long pfo_ret__; + switch (len2) { + case 1:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break; + case 2:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break; + case 4:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break; + case 8:asm("movq %1,%0": "=r" (pfo_ret__) : "m" (cpu_number)); break; + default: printf("impossible\n"); + } + pscr_ret__ = pfo_ret__; + }; + break; + } + pscr_ret__; + })) + { + printf("huh?\n"); + } +#endif +} + +void test_asm_dead_code(void) +{ + long rdi; + /* Try to make sure that xdi contains a zero, and hence will + lead to a segfault if the next asm is evaluated without + arguments being set up. */ + asm volatile ("" : "=D" (rdi) : "0" (0)); + (void)sizeof (({ + int var; + /* This shouldn't trigger a segfault, either the argument + registers need to be set up and the asm emitted despite + this being in an unevaluated context, or both the argument + setup _and_ the asm emission need to be suppressed. The latter + is better. Disabling asm code gen when suppression is on + also fixes the above trace_console bug, but that came earlier + than asm suppression. */ + asm volatile ("movl $0,(%0)" : : "D" (&var) : "memory"); + var; + })); +} + +void test_asm_call(void) +{ +#if defined __x86_64__ && !defined _WIN64 + static char str[] = "PATH"; + char *s; + /* This tests if a reference to an undefined symbol from an asm + block, which isn't otherwise referenced in this file, is correctly + regarded as global symbol, so that it's resolved by other object files + or libraries. We chose getenv here, which isn't used anywhere else + in this file. (If we used e.g. printf, which is used we already + would have a global symbol entry, not triggering the bug which is + tested here). */ + /* two pushes so stack remains aligned */ + asm volatile ("push %%rdi; push %%rdi; mov %0, %%rdi;" +#if 1 && !defined(__TINYC__) && (defined(__PIC__) || defined(__PIE__)) + "call getenv@plt;" +#else + "call getenv;" +#endif + "pop %%rdi; pop %%rdi" + : "=a" (s) : "r" (str)); + printf("asmd: %s\n", s); +#endif +} + +#if defined __x86_64__ +# define RX "(%rip)" +#else +# define RX +#endif + +void asm_dot_test(void) +{ + int x; + for (x = 1;; ++x) { + int r = x; + switch (x) { + case 1: + asm(".text; lea S"RX",%eax; lea ."RX",%ecx; sub %ecx,%eax; S=.; jmp p0"); + case 2: + asm(".text; jmp .+6; .int 123; mov .-4"RX",%eax; jmp p0"); + case 3: + asm(".data; Y=.; .int 999; X=Y; .int 456; X=.-4"); + asm(".text; mov X"RX",%eax; jmp p0"); + case 4: + asm(".data; X=.; .int 789; Y=.; .int 999"); + asm(".text; mov X"RX",%eax; X=Y; jmp p0"); + case 0: + asm(".text; p0=.; mov %%eax,%0;" : "=m"(r)); break; + } + if (r == x) + break; + printf("asm_dot_test %d: %d\n", x, r); + } +} + +void asm_test(void) +{ + char buf[128]; + unsigned int val, val2; + struct struct123 s1; + struct struct1231 s2 = { (unsigned long)&s1 }; + /* Hide the outer base_func, but check later that the inline + asm block gets the outer one. */ + int base_func = 42; + void override_func3 (void); + unsigned long asmret; +#ifdef BOOL_ISOC99 + _Bool somebool; +#endif + register int regvar asm("%esi"); + + printf("inline asm:\n"); + + // parse 0x1E-1 as 3 tokens in asm mode + asm volatile ("mov $0x1E-1,%eax"); + + /* test the no operand case */ + asm volatile ("xorl %eax, %eax"); + + memcpy1(buf, "hello", 6); + strncat1(buf, " worldXXXXX", 3); + printf("%s\n", buf); + + memcpy2(buf, "hello", 6); + strncat2(buf, " worldXXXXX", 3); + printf("%s\n", buf); + + /* 'A' constraint test */ + printf("mul64=0x%Lx\n", mul64(0x12345678, 0xabcd1234)); + printf("inc64=0x%Lx\n", inc64(0x12345678ffffffff)); + + s1.a = 42; + s1.b = 43; + printf("mconstraint: %d", mconstraint_test(&s2)); + printf(" %d %d\n", s1.a, s1.b); + other_constraints_test(); + set = 0xff; + sigdelset1(&set, 2); + sigaddset1(&set, 16); + /* NOTE: we test here if C labels are correctly restored after the + asm statement */ + goto label1; + label2: + __asm__("btsl %1,%0" : "=m"(set) : "Ir"(20) : "cc"); + printf("set=0x%x\n", set); + val = 0x01020304; + printf("swab32(0x%08x) = 0x%0x\n", val, swab32(val)); +#ifndef _WIN32 + override_func1(); + override_func2(); + /* The base_func ref from the following inline asm should find + the global one, not the local decl from this function. */ + asm volatile(".weak override_func3\n.set override_func3, base_func"); + override_func3(); + printf("asmstr: %s\n", get_asm_string()); + asm_local_label_diff(); + asm_local_statics(); +#endif + /* Check that we can also load structs of appropriate layout + into registers. */ + asm volatile("" : "=r" (asmret) : "0"(s2)); + if (asmret != s2.addr) + printf("asmstr: failed\n"); +#ifdef BOOL_ISOC99 + /* Check that the typesize correctly sets the register size to + 8 bit. */ + asm volatile("cmp %1,%2; sete %0" : "=a"(somebool) : "r"(1), "r"(2)); + if (!somebool) + printf("asmbool: failed\n"); +#endif + val = 43; + fancy_copy (&val, &val2); + printf ("fancycpy(%d)=%d\n", val, val2); + val = 44; + fancy_copy2 (&val, &val2); + printf ("fancycpy2(%d)=%d\n", val, val2); + asm volatile ("mov $0x4243, %%esi" : "=r" (regvar)); + printf ("regvar=%x\n", regvar); + test_high_clobbers(); + trace_console(8, 8); + test_asm_dead_code(); + test_asm_call(); + asm_dot_test(); + return; + label1: + goto label2; +} + +#else + +void asm_test(void) +{ +} + +#endif + +#define COMPAT_TYPE(type1, type2) \ +{\ + printf("__builtin_types_compatible_p(%s, %s) = %d\n", #type1, #type2, \ + __builtin_types_compatible_p (type1, type2));\ +} + +int constant_p_var; + +void builtin_test(void) +{ + short s; + int i; + long long ll; +#if GCC_MAJOR >= 3 + COMPAT_TYPE(int, int); + COMPAT_TYPE(int, unsigned int); + COMPAT_TYPE(int, char); + COMPAT_TYPE(int, const int); + COMPAT_TYPE(int, volatile int); + COMPAT_TYPE(int *, int *); + COMPAT_TYPE(int *, void *); + COMPAT_TYPE(int *, const int *); + COMPAT_TYPE(char *, unsigned char *); + COMPAT_TYPE(char *, signed char *); + COMPAT_TYPE(char *, char *); +/* space is needed because tcc preprocessor introduces a space between each token */ + COMPAT_TYPE(char * *, void *); +#endif + printf("res = %d\n", __builtin_constant_p(1)); + printf("res = %d\n", __builtin_constant_p(1 + 2)); + printf("res = %d\n", __builtin_constant_p(&constant_p_var)); + printf("res = %d\n", __builtin_constant_p(constant_p_var)); + printf("res = %d\n", __builtin_constant_p(100000 / constant_p_var)); + s = 1; + ll = 2; + i = __builtin_choose_expr (1 != 0, ll, s); + printf("bce: %d\n", i); + i = __builtin_choose_expr (1 != 1, ll, s); + printf("bce: %d\n", i); + i = sizeof (__builtin_choose_expr (1, ll, s)); + printf("bce: %d\n", i); + i = sizeof (__builtin_choose_expr (0, ll, s)); + printf("bce: %d\n", i); + + //printf("bera: %p\n", __builtin_extract_return_addr((void*)43)); +} + +#ifndef _WIN32 +extern int __attribute__((weak)) weak_f1(void); +extern int __attribute__((weak)) weak_f2(void); +extern int weak_f3(void); +extern int __attribute__((weak)) weak_v1; +extern int __attribute__((weak)) weak_v2; +extern int weak_v3; + +extern int (*weak_fpa)() __attribute__((weak)); +extern int __attribute__((weak)) (*weak_fpb)(); +extern __attribute__((weak)) int (*weak_fpc)(); + +extern int weak_asm_f1(void) asm("weak_asm_f1x") __attribute((weak)); +extern int __attribute((weak)) weak_asm_f2(void) asm("weak_asm_f2x") ; +extern int __attribute((weak)) weak_asm_f3(void) asm("weak_asm_f3x") __attribute((weak)); +extern int weak_asm_v1 asm("weak_asm_v1x") __attribute((weak)); +extern int __attribute((weak)) weak_asm_v2 asm("weak_asm_v2x") ; +extern int __attribute((weak)) weak_asm_v3(void) asm("weak_asm_v3x") __attribute((weak)); + +static const size_t dummy = 0; +extern __typeof(dummy) weak_dummy1 __attribute__((weak, alias("dummy"))); +extern __typeof(dummy) __attribute__((weak, alias("dummy"))) weak_dummy2; +extern __attribute__((weak, alias("dummy"))) __typeof(dummy) weak_dummy3; + +int some_lib_func(void); +int dummy_impl_of_slf(void) { return 444; } +int some_lib_func(void) __attribute__((weak, alias("dummy_impl_of_slf"))); + +int weak_toolate() __attribute__((weak)); +int weak_toolate() { return 0; } + +void __attribute__((weak)) weak_test(void) +{ + printf("weak_f1=%d\n", weak_f1 ? weak_f1() : 123); + printf("weak_f2=%d\n", weak_f2 ? weak_f2() : 123); + printf("weak_f3=%d\n", weak_f3 ? weak_f3() : 123); + printf("weak_v1=%d\n",&weak_v1 ? weak_v1 : 123); + printf("weak_v2=%d\n",&weak_v2 ? weak_v2 : 123); + printf("weak_v3=%d\n",&weak_v3 ? weak_v3 : 123); + + printf("weak_fpa=%d\n",&weak_fpa ? weak_fpa() : 123); + printf("weak_fpb=%d\n",&weak_fpb ? weak_fpb() : 123); + printf("weak_fpc=%d\n",&weak_fpc ? weak_fpc() : 123); + + printf("weak_asm_f1=%d\n", weak_asm_f1 != NULL); + printf("weak_asm_f2=%d\n", weak_asm_f2 != NULL); + printf("weak_asm_f3=%d\n", weak_asm_f3 != NULL); + printf("weak_asm_v1=%d\n",&weak_asm_v1 != NULL); + printf("weak_asm_v2=%d\n",&weak_asm_v2 != NULL); + printf("weak_asm_v3=%d\n",&weak_asm_v3 != NULL); + printf("some_lib_func=%d\n", &some_lib_func ? some_lib_func() : 0); +} + +int __attribute__((weak)) weak_f2() { return 222; } +int __attribute__((weak)) weak_f3() { return 333; } +int __attribute__((weak)) weak_v2 = 222; +int __attribute__((weak)) weak_v3 = 333; +#endif + +void const_func(const int a) +{ +} + +void const_warn_test(void) +{ + const_func(1); +} + +struct condstruct { + int i; +}; + +int getme (struct condstruct *s, int i) +{ + int i1 = (i == 0 ? 0 : s)->i; + int i2 = (i == 0 ? s : 0)->i; + int i3 = (i == 0 ? (void*)0 : s)->i; + int i4 = (i == 0 ? s : (void*)0)->i; + return i1 + i2 + i3 + i4; +} + +struct global_data +{ + int a[40]; + int *b[40]; +}; + +struct global_data global_data; + +int global_data_getstuff (int *, int); + +void global_data_callit (int i) +{ + *global_data.b[i] = global_data_getstuff (global_data.b[i], 1); +} + +int global_data_getstuff (int *p, int i) +{ + return *p + i; +} + +void global_data_test (void) +{ + global_data.a[0] = 42; + global_data.b[0] = &global_data.a[0]; + global_data_callit (0); + printf ("%d\n", global_data.a[0]); +} + +struct cmpcmpS +{ + unsigned char fill : 3; + unsigned char b1 : 1; + unsigned char b2 : 1; + unsigned char fill2 : 3; +}; + +int glob1, glob2, glob3; + +void compare_comparisons (struct cmpcmpS *s) +{ + if (s->b1 != (glob1 == glob2) + || (s->b2 != (glob1 == glob3))) + printf ("comparing comparisons broken\n"); +} + +void cmp_comparison_test(void) +{ + struct cmpcmpS s; + s.b1 = 1; + glob1 = 42; glob2 = 42; + s.b2 = 0; + glob3 = 43; + compare_comparisons (&s); +} + +int fcompare (double a, double b, int code) +{ + switch (code) { + case 0: return a == b; + case 1: return a != b; + case 2: return a < b; + case 3: return a >= b; + case 4: return a > b; + case 5: return a <= b; + } +} + +void math_cmp_test(void) +{ + double nan = 0.0/0.0; + double one = 1.0; + double two = 2.0; + int comp = 0; +#define bug(a,b,op,iop,part) printf("Test broken: %s %s %s %s %d\n", #a, #b, #op, #iop, part) + + /* This asserts that "a op b" is _not_ true, but "a iop b" is true. + And it does this in various ways so that all code generation paths + are checked (generating inverted tests, or non-inverted tests, or + producing a 0/1 value without jumps (that's done in the fcompare + function). */ +#define FCMP(a,b,op,iop,code) \ + if (fcompare (a,b,code)) \ + bug (a,b,op,iop,1); \ + if (a op b) \ + bug (a,b,op,iop,2); \ + if (a iop b) \ + ; \ + else \ + bug (a,b,op,iop,3); \ + if ((a op b) || comp) \ + bug (a,b,op,iop,4); \ + if ((a iop b) || comp) \ + ; \ + else \ + bug (a,b,op,iop,5); + + /* Equality tests. */ + FCMP(nan, nan, ==, !=, 0); + FCMP(one, two, ==, !=, 0); + FCMP(one, one, !=, ==, 1); + /* Non-equality is a bit special. */ + if (!fcompare (nan, nan, 1)) + bug (nan, nan, !=, ==, 6); + + /* Relational tests on numbers. */ + FCMP(two, one, <, >=, 2); + FCMP(one, two, >=, <, 3); + FCMP(one, two, >, <=, 4); + FCMP(two, one, <=, >, 5); + + /* Relational tests on NaNs. Note that the inverse op here is + always !=, there's no operator in C that is equivalent to !(a < b), + when NaNs are involved, same for the other relational ops. */ + FCMP(nan, nan, <, !=, 2); + FCMP(nan, nan, >=, !=, 3); + FCMP(nan, nan, >, !=, 4); + FCMP(nan, nan, <=, !=, 5); +} + +double get100 () { return 100.0; } + +void callsave_test(void) +{ +#if defined __i386__ || defined __x86_64__ || defined __arm__ + int i, s; double *d; double t; + s = sizeof (double); + printf ("callsavetest: %d\n", s); + d = alloca (sizeof(double)); + d[0] = 10.0; + /* x86-64 had a bug were the next call to get100 would evict + the lvalue &d[0] as VT_LLOCAL, and the reload would be done + in int type, not pointer type. When alloca returns a pointer + with the high 32 bit set (which is likely on x86-64) the access + generates a segfault. */ + i = d[0] > get100 (); + printf ("%d\n", i); +#endif +} + + +void bfa3(ptrdiff_t str_offset) +{ + printf("bfa3: %s\n", (char *)__builtin_frame_address(3) + str_offset); +} +void bfa2(ptrdiff_t str_offset) +{ + printf("bfa2: %s\n", (char *)__builtin_frame_address(2) + str_offset); + bfa3(str_offset); +} +void bfa1(ptrdiff_t str_offset) +{ + printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset); + bfa2(str_offset); +} + +void builtin_frame_address_test(void) +{ +/* builtin_frame_address fails on ARM with gcc which make test3 fail */ +#ifndef __arm__ + char str[] = "__builtin_frame_address"; + char *fp0 = __builtin_frame_address(0); + + printf("str: %s\n", str); + bfa1(str-fp0); +#endif +} + +char via_volatile (char i) +{ + char volatile vi; + vi = i; + return vi; +} + +struct __attribute__((__packed__)) Spacked { + char a; + short b; + int c; +}; +struct Spacked spacked; +typedef struct __attribute__((__packed__)) { + char a; + short b; + int c; +} Spacked2; +Spacked2 spacked2; +typedef struct Spacked3_s { + char a; + short b; + int c; +} __attribute__((__packed__)) Spacked3; +Spacked3 spacked3; +struct gate_struct64 { + unsigned short offset_low; + unsigned short segment; + unsigned ist : 3, zero0 : 5, type : 5, dpl : 2, p : 1; + unsigned short offset_middle; + unsigned offset_high; + unsigned zero1; +} __attribute__((packed)); +typedef struct gate_struct64 gate_desc; +gate_desc a_gate_desc; +void attrib_test(void) +{ +#ifndef _WIN32 + printf("attr: %d %d %d %d\n", sizeof(struct Spacked), + sizeof(spacked), sizeof(Spacked2), sizeof(spacked2)); + printf("attr: %d %d\n", sizeof(Spacked3), sizeof(spacked3)); + printf("attr: %d %d\n", sizeof(gate_desc), sizeof(a_gate_desc)); +#endif +} +extern __attribute__((__unused__)) char * __attribute__((__unused__)) * +strange_attrib_placement (void); + +void * __attribute__((__unused__)) get_void_ptr (void *a) +{ + return a; +} + +/* This part checks for a bug in TOK_GET (used for inline expansion), + where the large long long constant left the the high bits set for + the integer constant token. */ +static inline +int __get_order(unsigned long long size) +{ + int order; + size -= 0xffff880000000000ULL; // this const left high bits set in the token + { + struct S { int i : 1; } s; // constructed for this '1' + } + order = size; + return order; +} + +/* This just forces the above inline function to be actually emitted. */ +int force_get_order(unsigned long s) +{ + return __get_order(s); +} diff --git a/05/tcc-final-old/tests/tcctest.h b/05/tcc-final-old/tests/tcctest.h new file mode 100644 index 0000000..b301c7c --- /dev/null +++ b/05/tcc-final-old/tests/tcctest.h @@ -0,0 +1,9 @@ +static inline const char *get_basefile_from_header(void) +{ + return __BASE_FILE__; +} + +static inline const char *get_file_from_header(void) +{ + return __FILE__; +} diff --git a/05/tcc-final-old/tests/testfp.c b/05/tcc-final-old/tests/testfp.c new file mode 100644 index 0000000..63342b4 --- /dev/null +++ b/05/tcc-final-old/tests/testfp.c @@ -0,0 +1,510 @@ +/* + * Test 128-bit floating-point arithmetic on arm64: + * build with two different compilers and compare the output. + * + * Copyright (c) 2015 Edmund Grimley Evans + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. This file is offered as-is, + * without any warranty. + */ + +#include +#include +#include +#include + +#define check(x) ((x) ? (void)0 : check_fail(#x, __FILE__, __LINE__)) + +void check_fail(const char *assertion, const char *file, unsigned int line) +{ + printf("%s:%d: Check (%s) failed.", file, line, assertion); + exit(1); +} + +typedef struct { + unsigned long long x0, x1; +} u128_t; + +float copy_fi(uint32_t x) +{ + float f; + memcpy(&f, &x, 4); + return f; +} + +double copy_di(uint64_t x) +{ + double f; + memcpy(&f, &x, 8); + return f; +} + +long double copy_ldi(u128_t x) +{ + long double f; + memcpy(&f, &x, 16); + return f; +} + +uint32_t copy_if(float f) +{ + uint32_t x; + memcpy(&x, &f, 4); + return x; +} + +uint64_t copy_id(double f) +{ + uint64_t x; + memcpy(&x, &f, 8); + return x; +} + +u128_t copy_ild(long double f) +{ + u128_t x; + memcpy(&x, &f, 16); + return x; +} + +long double make(int sgn, int exp, uint64_t high, uint64_t low) +{ + u128_t x = { low, + (0x0000ffffffffffff & high) | + (0x7fff000000000000 & (uint64_t)exp << 48) | + (0x8000000000000000 & (uint64_t)sgn << 63) }; + return copy_ldi(x); +} + +void cmp(long double a, long double b) +{ + u128_t ax = copy_ild(a); + u128_t bx = copy_ild(b); + int eq = (a == b); + int ne = (a != b); + int lt = (a < b); + int le = (a <= b); + int gt = (a > b); + int ge = (a >= b); + + check(eq == 0 || eq == 1); + check(lt == 0 || lt == 1); + check(gt == 0 || gt == 1); + check(ne == !eq && le == (lt | eq) && ge == (gt | eq)); + check(eq + lt + gt < 2); + + printf("cmp %016llx%016llx %016llx%016llx %d %d %d\n", + ax.x1, ax.x0, bx.x1, bx.x0, lt, eq, gt); +} + +void cmps(void) +{ + int i, j; + + for (i = 0; i < 2; i++) + for (j = 0; j < 2; j++) + cmp(make(i, 0, 0, 0), make(j, 0, 0, 0)); + + for (i = 0; i < 2; i++) { + for (j = 0; j < 64; j++) { + long double f1 = make(i, 32767, (uint64_t)1 << j, 0); + long double f2 = make(i, 32767, 0, (uint64_t)1 << j); + cmp(f1, 0); + cmp(f2, 0); + cmp(0, f1); + cmp(0, f2); + } + } + + for (i = 0; i < 6; i++) + for (j = 0; j < 6; j++) + cmp(make(i & 1, i >> 1, 0, 0), + make(j & 1, j >> 1, 0, 0)); + + for (i = 0; i < 2; i++) { + for (j = 0; j < 2; j++) { + int a, b; + for (a = 0; a < 2; a++) { + for (b = 0; b < 2; b++) { + cmp(make(i, j, a, b), make(i, j, 0, 0)); + cmp(make(i, j, 0, 0), make(i, j, a, b)); + } + } + } + } +} + +void xop(const char *name, long double a, long double b, long double c) +{ + u128_t ax = copy_ild(a); + u128_t bx = copy_ild(b); + u128_t cx = copy_ild(c); + printf("%s %016llx%016llx %016llx%016llx %016llx%016llx\n", + name, ax.x1, ax.x0, bx.x1, bx.x0, cx.x1, cx.x0); +} + +void fadd(long double a, long double b) +{ + xop("add", a, b, a + b); +} + +void fsub(long double a, long double b) +{ + xop("sub", a, b, a - b); +} + +void fmul(long double a, long double b) +{ + xop("mul", a, b, a * b); +} + +void fdiv(long double a, long double b) +{ + xop("div", a, b, a / b); +} + +void nanz(void) +{ + // Check NaNs: + { + long double x[7]; + int i, j, n = 0; + x[n++] = make(0, 32000, 0x95132b76effc, 0xd79035214b4f8d53); + x[n++] = make(1, 32001, 0xbe71d7a51587, 0x30601c6815d6c3ac); + x[n++] = make(0, 32767, 0, 1); + x[n++] = make(0, 32767, (uint64_t)1 << 46, 0); + x[n++] = make(1, 32767, (uint64_t)1 << 47, 0); + x[n++] = make(1, 32767, 0x7596c7099ad5, 0xe25fed2c58f73fc9); + x[n++] = make(0, 32767, 0x835d143360f9, 0x5e315efb35630666); + check(n == sizeof(x) / sizeof(*x)); + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + fadd(x[i], x[j]); + fsub(x[i], x[j]); + fmul(x[i], x[j]); + fdiv(x[i], x[j]); + } + } + } + + // Check infinities and zeroes: + { + long double x[6]; + int i, j, n = 0; + x[n++] = make(1, 32000, 0x62acda85f700, 0x47b6c9f35edc4044); + x[n++] = make(0, 32001, 0x94b7abf55af7, 0x9f425fe354428e19); + x[n++] = make(0, 32767, 0, 0); + x[n++] = make(1, 32767, 0, 0); + x[n++] = make(0, 0, 0, 0); + x[n++] = make(1, 0, 0, 0); + check(n == sizeof(x) / sizeof(*x)); + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + fadd(x[i], x[j]); + fsub(x[i], x[j]); + fmul(x[i], x[j]); + fdiv(x[i], x[j]); + } + } + } +} + +void adds(void) +{ + // Check shifting and add/sub: + { + int i; + for (i = -130; i <= 130; i++) { + int s1 = (uint32_t)i % 3 < 1; + int s2 = (uint32_t)i % 5 < 2; + fadd(make(s1, 16384 , 0x502c065e4f71a65d, 0xd2f9bdb031f4f031), + make(s2, 16384 + i, 0xae267395a9bc1033, 0xb56b5800da1ba448)); + } + } + + // Check normalisation: + { + uint64_t a0 = 0xc6bab0a6afbef5ed; + uint64_t a1 = 0x4f84136c4a2e9b52; + int ee[] = { 0, 1, 10000 }; + int e, i; + for (e = 0; e < sizeof(ee) / sizeof(*ee); e++) { + int exp = ee[e]; + fsub(make(0, exp, a1, a0), make(0, 0, 0, 0)); + for (i = 63; i >= 0; i--) + fsub(make(0, exp, a1 | (uint64_t)1 << i >> 1, a0), + make(0, exp, a1 >> i << i, 0)); + for (i = 63; i >=0; i--) + fsub(make(0, exp, a1, a0 | (uint64_t)1 << i >> 1), + make(0, exp, a1, a0 >> i << i)); + } + } + + // Carry/overflow from rounding: + { + fadd(make(0, 114, -1, -1), make(0, 1, 0, 0)); + fadd(make(0, 32766, -1, -1), make(0, 32653, 0, 0)); + fsub(make(1, 32766, -1, -1), make(0, 32653, 0, 0)); + } +} + +void muls(void) +{ + int i, j; + + { + long double max = make(0, 32766, -1, -1); + long double min = make(0, 0, 0, 1); + fmul(max, max); + fmul(max, min); + fmul(min, min); + } + + for (i = 117; i > 0; i--) + fmul(make(0, 16268, 0x643dcea76edc, 0xe0877a598403627a), + make(i & 1, i, 0, 0)); + + fmul(make(0, 16383, -1, -3), make(0, 16383, 0, 1)); + // Round to next exponent: + fmul(make(0, 16383, -1, -2), make(0, 16383, 0, 1)); + // Round from subnormal to normal: + fmul(make(0, 1, -1, -1), make(0, 16382, 0, 0)); + + for (i = 0; i < 2; i++) + for (j = 0; j < 112; j++) + fmul(make(0, 16383, (uint64_t)1 << i, 0), + make(0, 16383, + j < 64 ? 0 : (uint64_t)1 << (j - 64), + j < 64 ? (uint64_t)1 << j : 0)); +} + +void divs(void) +{ + int i; + + { + long double max = make(0, 32766, -1, -1); + long double min = make(0, 0, 0, 1); + fdiv(max, max); + fdiv(max, min); + fdiv(min, max); + fdiv(min, min); + } + + for (i = 0; i < 64; i++) + fdiv(make(0, 16383, -1, -1), make(0, 16383, -1, -(uint64_t)1 << i)); + for (i = 0; i < 48; i++) + fdiv(make(0, 16383, -1, -1), make(0, 16383, -(uint64_t)1 << i, 0)); +} + +void cvtlsw(int32_t a) +{ + long double f = a; + u128_t x = copy_ild(f); + printf("cvtlsw %08lx %016llx%016llx\n", (long)(uint32_t)a, x.x1, x.x0); +} + +void cvtlsx(int64_t a) +{ + long double f = a; + u128_t x = copy_ild(f); + printf("cvtlsx %016llx %016llx%016llx\n", + (long long)(uint64_t)a, x.x1, x.x0); +} + +void cvtluw(uint32_t a) +{ + long double f = a; + u128_t x = copy_ild(f); + printf("cvtluw %08lx %016llx%016llx\n", (long)a, x.x1, x.x0); +} + +void cvtlux(uint64_t a) +{ + long double f = a; + u128_t x = copy_ild(f); + printf("cvtlux %016llx %016llx%016llx\n", (long long)a, x.x1, x.x0); +} + +void cvtil(long double a) +{ + u128_t x = copy_ild(a); + int32_t b1 = a; + int64_t b2 = a; + uint32_t b3 = a; + uint64_t b4 = a; + printf("cvtswl %016llx%016llx %08lx\n", + x.x1, x.x0, (long)(uint32_t)b1); + printf("cvtsxl %016llx%016llx %016llx\n", + x.x1, x.x0, (long long)(uint64_t)b2); + printf("cvtuwl %016llx%016llx %08lx\n", + x.x1, x.x0, (long)b3); + printf("cvtuxl %016llx%016llx %016llx\n", + x.x1, x.x0, (long long)b4); +} + +void cvtlf(float a) +{ + uint32_t ax = copy_if(a); + long double b = a; + u128_t bx = copy_ild(b); + printf("cvtlf %08lx %016llx%016llx\n", (long)ax, bx.x1, bx.x0); +} + +void cvtld(double a) +{ + uint64_t ax = copy_id(a); + long double b = a; + u128_t bx = copy_ild(b); + printf("cvtld %016llx %016llx%016llx\n", (long long)ax, bx.x1, bx.x0); +} + +void cvtfl(long double a) +{ + u128_t ax = copy_ild(a); + float b = a; + uint32_t bx = copy_if(b); + printf("cvtfl %016llx%016llx %08lx\n", ax.x1, ax.x0, (long)bx); +} + +void cvtdl(long double a) +{ + u128_t ax = copy_ild(a); + double b = a; + uint64_t bx = copy_id(b); + printf("cvtdl %016llx%016llx %016llx\n", ax.x1, ax.x0, (long long)bx); +} + +void cvts(void) +{ + int i, j; + + { + uint32_t x = 0xad040c5b; + cvtlsw(0); + for (i = 0; i < 31; i++) + cvtlsw(x >> (31 - i)); + for (i = 0; i < 31; i++) + cvtlsw(-(x >> (31 - i))); + cvtlsw(0x80000000); + } + { + uint64_t x = 0xb630a248cad9afd2; + cvtlsx(0); + for (i = 0; i < 63; i++) + cvtlsx(x >> (63 - i)); + for (i = 0; i < 63; i++) + cvtlsx(-(x >> (63 - i))); + cvtlsx(0x8000000000000000); + } + { + uint32_t x = 0xad040c5b; + cvtluw(0); + for (i = 0; i < 32; i++) + cvtluw(x >> (31 - i)); + } + { + uint64_t x = 0xb630a248cad9afd2; + cvtlux(0); + for (i = 0; i < 64; i++) + cvtlux(x >> (63 - i)); + } + + for (i = 0; i < 2; i++) { + cvtil(make(i, 32767, 0, 1)); + cvtil(make(i, 32767, (uint64_t)1 << 47, 0)); + cvtil(make(i, 32767, 123, 456)); + cvtil(make(i, 32767, 0, 0)); + cvtil(make(i, 16382, -1, -1)); + cvtil(make(i, 16383, -1, -1)); + cvtil(make(i, 16384, 0x7fffffffffff, -1)); + cvtil(make(i, 16384, 0x800000000000, 0)); + for (j = 0; j < 68; j++) + cvtil(make(i, 16381 + j, 0xd4822c0a10ec, 0x1fe2f8b2669f5c9d)); + } + + cvtlf(copy_fi(0x00000000)); + cvtlf(copy_fi(0x456789ab)); + cvtlf(copy_fi(0x7f800000)); + cvtlf(copy_fi(0x7f923456)); + cvtlf(copy_fi(0x7fdbcdef)); + cvtlf(copy_fi(0x80000000)); + cvtlf(copy_fi(0xabcdef12)); + cvtlf(copy_fi(0xff800000)); + cvtlf(copy_fi(0xff923456)); + cvtlf(copy_fi(0xffdbcdef)); + + cvtld(copy_di(0x0000000000000000)); + cvtld(copy_di(0x456789abcdef0123)); + cvtld(copy_di(0x7ff0000000000000)); + cvtld(copy_di(0x7ff123456789abcd)); + cvtld(copy_di(0x7ffabcdef1234567)); + cvtld(copy_di(0x8000000000000000)); + cvtld(copy_di(0xcdef123456789abc)); + cvtld(copy_di(0xfff0000000000000)); + cvtld(copy_di(0xfff123456789abcd)); + cvtld(copy_di(0xfffabcdef1234567)); + + for (i = 0; i < 2; i++) { \ + cvtfl(make(i, 0, 0, 0)); + cvtfl(make(i, 16232, -1, -1)); + cvtfl(make(i, 16233, 0, 0)); + cvtfl(make(i, 16233, 0, 1)); + cvtfl(make(i, 16383, 0xab0ffd000000, 0)); + cvtfl(make(i, 16383, 0xab0ffd000001, 0)); + cvtfl(make(i, 16383, 0xab0ffeffffff, 0)); + cvtfl(make(i, 16383, 0xab0fff000000, 0)); + cvtfl(make(i, 16383, 0xab0fff000001, 0)); + cvtfl(make(i, 16510, 0xfffffeffffff, -1)); + cvtfl(make(i, 16510, 0xffffff000000, 0)); + cvtfl(make(i, 16511, 0, 0)); + cvtfl(make(i, 32767, 0, 0)); + cvtfl(make(i, 32767, 0, 1)); + cvtfl(make(i, 32767, 0x4cbe01ac5f40, 0x75cee3c6afbb00b5)); + cvtfl(make(i, 32767, 0x800000000000, 1)); + cvtfl(make(i, 32767, 0xa11caaaf6a52, 0x696033e871eab099)); + } + + for (i = 0; i < 2; i++) { + cvtdl(make(i, 0, 0, 0)); + cvtdl(make(i, 15307, -1, -1)); + cvtdl(make(i, 15308, 0, 0)); + cvtdl(make(i, 15308, 0, 1)); + cvtdl(make(i, 16383, 0xabc123abc0ff, 0xe800000000000000)); + cvtdl(make(i, 16383, 0xabc123abc0ff, 0xe800000000000001)); + cvtdl(make(i, 16383, 0xabc123abc0ff, 0xf7ffffffffffffff)); + cvtdl(make(i, 16383, 0xabc123abc0ff, 0xf800000000000000)); + cvtdl(make(i, 16383, 0xabc123abc0ff, 0xf800000000000001)); + cvtdl(make(i, 17406, 0xffffffffffff, 0xf7ffffffffffffff)); + cvtdl(make(i, 17406, 0xffffffffffff, 0xf800000000000000)); + cvtdl(make(i, 17407, 0, 0)); + cvtdl(make(i, 32767, 0, 0)); + cvtdl(make(i, 32767, 0, 1)); + cvtdl(make(i, 32767, 0x4cbe01ac5f40, 0x75cee3c6afbb00b5)); + cvtdl(make(i, 32767, 0x800000000000, 1)); + cvtdl(make(i, 32767, 0xa11caaaf6a52, 0x696033e871eab099)); + } +} + +void tests(void) +{ + cmps(); + nanz(); + adds(); + muls(); + divs(); + cvts(); +} + +int main() +{ +#ifdef __aarch64__ + tests(); +#else + printf("This test program is intended for a little-endian architecture\n" + "with an IEEE-standard 128-bit long double.\n"); +#endif + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/00_assignment.c b/05/tcc-final-old/tests/tests2/00_assignment.c new file mode 100644 index 0000000..c96109f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/00_assignment.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + int a; + a = 42; + printf("%d\n", a); + + int b = 64; + printf("%d\n", b); + + int c = 12, d = 34; + printf("%d, %d\n", c, d); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/00_assignment.expect b/05/tcc-final-old/tests/tests2/00_assignment.expect new file mode 100644 index 0000000..d4407f3 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/00_assignment.expect @@ -0,0 +1,3 @@ +42 +64 +12, 34 diff --git a/05/tcc-final-old/tests/tests2/01_comment.c b/05/tcc-final-old/tests/tests2/01_comment.c new file mode 100644 index 0000000..a2e6bc6 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/01_comment.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + printf("Hello\n"); + printf("Hello\n"); /* this is a comment */ printf("Hello\n"); + printf("Hello\n"); + // this is also a comment sayhello(); + printf("Hello\n"); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/01_comment.expect b/05/tcc-final-old/tests/tests2/01_comment.expect new file mode 100644 index 0000000..b1387ad --- /dev/null +++ b/05/tcc-final-old/tests/tests2/01_comment.expect @@ -0,0 +1,5 @@ +Hello +Hello +Hello +Hello +Hello diff --git a/05/tcc-final-old/tests/tests2/02_printf.c b/05/tcc-final-old/tests/tests2/02_printf.c new file mode 100644 index 0000000..4c34dd8 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/02_printf.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + printf("Hello world\n"); + + int Count; + for (Count = -5; Count <= 5; Count++) + printf("Count = %d\n", Count); + + printf("String 'hello', 'there' is '%s', '%s'\n", "hello", "there"); + printf("Character 'A' is '%c'\n", 65); + printf("Character 'a' is '%c'\n", 'a'); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/02_printf.expect b/05/tcc-final-old/tests/tests2/02_printf.expect new file mode 100644 index 0000000..f67a0f6 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/02_printf.expect @@ -0,0 +1,15 @@ +Hello world +Count = -5 +Count = -4 +Count = -3 +Count = -2 +Count = -1 +Count = 0 +Count = 1 +Count = 2 +Count = 3 +Count = 4 +Count = 5 +String 'hello', 'there' is 'hello', 'there' +Character 'A' is 'A' +Character 'a' is 'a' diff --git a/05/tcc-final-old/tests/tests2/03_struct.c b/05/tcc-final-old/tests/tests2/03_struct.c new file mode 100644 index 0000000..c5d48c5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/03_struct.c @@ -0,0 +1,31 @@ +#include + +struct fred +{ + int boris; + int natasha; +}; + +int main() +{ + struct fred bloggs; + + bloggs.boris = 12; + bloggs.natasha = 34; + + printf("%d\n", bloggs.boris); + printf("%d\n", bloggs.natasha); + + struct fred jones[2]; + jones[0].boris = 12; + jones[0].natasha = 34; + jones[1].boris = 56; + jones[1].natasha = 78; + + printf("%d\n", jones[0].boris); + printf("%d\n", jones[0].natasha); + printf("%d\n", jones[1].boris); + printf("%d\n", jones[1].natasha); + + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/03_struct.expect b/05/tcc-final-old/tests/tests2/03_struct.expect new file mode 100644 index 0000000..ecbf589 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/03_struct.expect @@ -0,0 +1,6 @@ +12 +34 +12 +34 +56 +78 diff --git a/05/tcc-final-old/tests/tests2/04_for.c b/05/tcc-final-old/tests/tests2/04_for.c new file mode 100644 index 0000000..312fed8 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/04_for.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int Count; + + for (Count = 1; Count <= 10; Count++) + { + printf("%d\n", Count); + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/04_for.expect b/05/tcc-final-old/tests/tests2/04_for.expect new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/04_for.expect @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/05/tcc-final-old/tests/tests2/05_array.c b/05/tcc-final-old/tests/tests2/05_array.c new file mode 100644 index 0000000..c218f31 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/05_array.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int Count; + int Array[10]; + + for (Count = 1; Count <= 10; Count++) + { + Array[Count-1] = Count * Count; + } + + for (Count = 0; Count < 10; Count++) + { + printf("%d\n", Array[Count]); + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/05_array.expect b/05/tcc-final-old/tests/tests2/05_array.expect new file mode 100644 index 0000000..bc7257c --- /dev/null +++ b/05/tcc-final-old/tests/tests2/05_array.expect @@ -0,0 +1,10 @@ +1 +4 +9 +16 +25 +36 +49 +64 +81 +100 diff --git a/05/tcc-final-old/tests/tests2/06_case.c b/05/tcc-final-old/tests/tests2/06_case.c new file mode 100644 index 0000000..c0191e2 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/06_case.c @@ -0,0 +1,29 @@ +#include + +int main() +{ + int Count; + + for (Count = 0; Count < 4; Count++) + { + printf("%d\n", Count); + switch (Count) + { + case 1: + printf("%d\n", 1); + break; + + case 2: + printf("%d\n", 2); + break; + + default: + printf("%d\n", 0); + break; + } + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/06_case.expect b/05/tcc-final-old/tests/tests2/06_case.expect new file mode 100644 index 0000000..fab2c20 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/06_case.expect @@ -0,0 +1,8 @@ +0 +0 +1 +1 +2 +2 +3 +0 diff --git a/05/tcc-final-old/tests/tests2/07_function.c b/05/tcc-final-old/tests/tests2/07_function.c new file mode 100644 index 0000000..0477ce1 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/07_function.c @@ -0,0 +1,30 @@ +#include + +int myfunc(int x) +{ + return x * x; +} + +void vfunc(int a) +{ + printf("a=%d\n", a); +} + +void qfunc() +{ + printf("qfunc()\n"); +} + +int main() +{ + printf("%d\n", myfunc(3)); + printf("%d\n", myfunc(4)); + + vfunc(1234); + + qfunc(); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/07_function.expect b/05/tcc-final-old/tests/tests2/07_function.expect new file mode 100644 index 0000000..8ffb0a7 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/07_function.expect @@ -0,0 +1,4 @@ +9 +16 +a=1234 +qfunc() diff --git a/05/tcc-final-old/tests/tests2/08_while.c b/05/tcc-final-old/tests/tests2/08_while.c new file mode 100644 index 0000000..602ffc7 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/08_while.c @@ -0,0 +1,24 @@ +#include + +int main() +{ + int a; + int p; + int t; + + a = 1; + p = 0; + t = 0; + + while (a < 100) + { + printf("%d\n", a); + t = a; + a = t + p; + p = t; + } + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/08_while.expect b/05/tcc-final-old/tests/tests2/08_while.expect new file mode 100644 index 0000000..702d4c0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/08_while.expect @@ -0,0 +1,11 @@ +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 diff --git a/05/tcc-final-old/tests/tests2/09_do_while.c b/05/tcc-final-old/tests/tests2/09_do_while.c new file mode 100644 index 0000000..1d3315d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/09_do_while.c @@ -0,0 +1,24 @@ +#include + +int main() +{ + int a; + int p; + int t; + + a = 1; + p = 0; + t = 0; + + do + { + printf("%d\n", a); + t = a; + a = t + p; + p = t; + } while (a < 100); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/09_do_while.expect b/05/tcc-final-old/tests/tests2/09_do_while.expect new file mode 100644 index 0000000..702d4c0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/09_do_while.expect @@ -0,0 +1,11 @@ +1 +1 +2 +3 +5 +8 +13 +21 +34 +55 +89 diff --git a/05/tcc-final-old/tests/tests2/10_pointer.c b/05/tcc-final-old/tests/tests2/10_pointer.c new file mode 100644 index 0000000..0177f4d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/10_pointer.c @@ -0,0 +1,40 @@ +#include + +struct ziggy +{ + int a; + int b; + int c; +} bolshevic; + +int main() +{ + int a; + int *b; + int c; + + a = 42; + b = &a; + printf("a = %d\n", *b); + + bolshevic.a = 12; + bolshevic.b = 34; + bolshevic.c = 56; + + printf("bolshevic.a = %d\n", bolshevic.a); + printf("bolshevic.b = %d\n", bolshevic.b); + printf("bolshevic.c = %d\n", bolshevic.c); + + struct ziggy *tsar = &bolshevic; + + printf("tsar->a = %d\n", tsar->a); + printf("tsar->b = %d\n", tsar->b); + printf("tsar->c = %d\n", tsar->c); + + b = &(bolshevic.b); + printf("bolshevic.b = %d\n", *b); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/10_pointer.expect b/05/tcc-final-old/tests/tests2/10_pointer.expect new file mode 100644 index 0000000..1e3c473 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/10_pointer.expect @@ -0,0 +1,8 @@ +a = 42 +bolshevic.a = 12 +bolshevic.b = 34 +bolshevic.c = 56 +tsar->a = 12 +tsar->b = 34 +tsar->c = 56 +bolshevic.b = 34 diff --git a/05/tcc-final-old/tests/tests2/11_precedence.c b/05/tcc-final-old/tests/tests2/11_precedence.c new file mode 100644 index 0000000..db2049d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/11_precedence.c @@ -0,0 +1,40 @@ +#include + +int main() +{ + int a; + int b; + int c; + int d; + int e; + int f; + int x; + int y; + + a = 12; + b = 34; + c = 56; + d = 78; + e = 0; + f = 1; + + printf("%d\n", c + d); + printf("%d\n", (y = c + d)); + printf("%d\n", e || e && f); + printf("%d\n", e || f && f); + printf("%d\n", e && e || f); + printf("%d\n", e && f || f); + printf("%d\n", a && f | f); + printf("%d\n", a | b ^ c & d); + printf("%d, %d\n", a == a, a == b); + printf("%d, %d\n", a != a, a != b); + printf("%d\n", a != b && c != d); + printf("%d\n", a + b * c / f); + printf("%d\n", a + b * c / f); + printf("%d\n", (4 << 4)); + printf("%d\n", (64 >> 4)); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/11_precedence.expect b/05/tcc-final-old/tests/tests2/11_precedence.expect new file mode 100644 index 0000000..b692396 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/11_precedence.expect @@ -0,0 +1,15 @@ +134 +134 +0 +1 +1 +1 +1 +46 +1, 0 +0, 1 +1 +1916 +1916 +64 +4 diff --git a/05/tcc-final-old/tests/tests2/12_hashdefine.c b/05/tcc-final-old/tests/tests2/12_hashdefine.c new file mode 100644 index 0000000..5c521e0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/12_hashdefine.c @@ -0,0 +1,14 @@ +#include + +#define FRED 12 +#define BLOGGS(x) (12*(x)) + +int main() +{ + printf("%d\n", FRED); + printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3)); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/12_hashdefine.expect b/05/tcc-final-old/tests/tests2/12_hashdefine.expect new file mode 100644 index 0000000..99f2ed5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/12_hashdefine.expect @@ -0,0 +1,2 @@ +12 +12, 24, 36 diff --git a/05/tcc-final-old/tests/tests2/13_integer_literals.c b/05/tcc-final-old/tests/tests2/13_integer_literals.c new file mode 100644 index 0000000..7cee98b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/13_integer_literals.c @@ -0,0 +1,20 @@ +#include + +int main() +{ + int a = 24680; + int b = 01234567; + int c = 0x2468ac; + int d = 0x2468AC; + int e = 0b010101010101; + + printf("%d\n", a); + printf("%d\n", b); + printf("%d\n", c); + printf("%d\n", d); + printf("%d\n", e); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/13_integer_literals.expect b/05/tcc-final-old/tests/tests2/13_integer_literals.expect new file mode 100644 index 0000000..f5aca06 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/13_integer_literals.expect @@ -0,0 +1,5 @@ +24680 +342391 +2386092 +2386092 +1365 diff --git a/05/tcc-final-old/tests/tests2/14_if.c b/05/tcc-final-old/tests/tests2/14_if.c new file mode 100644 index 0000000..2bd2550 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/14_if.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int a = 1; + + if (a) + printf("a is true\n"); + else + printf("a is false\n"); + + int b = 0; + if (b) + printf("b is true\n"); + else + printf("b is false\n"); + + return 0; +} + +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : diff --git a/05/tcc-final-old/tests/tests2/14_if.expect b/05/tcc-final-old/tests/tests2/14_if.expect new file mode 100644 index 0000000..c32c415 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/14_if.expect @@ -0,0 +1,2 @@ +a is true +b is false diff --git a/05/tcc-final-old/tests/tests2/15_recursion.c b/05/tcc-final-old/tests/tests2/15_recursion.c new file mode 100644 index 0000000..f79a00d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/15_recursion.c @@ -0,0 +1,21 @@ +#include + +int factorial(int i) +{ + if (i < 2) + return i; + else + return i * factorial(i - 1); +} + +int main() +{ + int Count; + + for (Count = 1; Count <= 10; Count++) + printf("%d\n", factorial(Count)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/15_recursion.expect b/05/tcc-final-old/tests/tests2/15_recursion.expect new file mode 100644 index 0000000..db47b28 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/15_recursion.expect @@ -0,0 +1,10 @@ +1 +2 +6 +24 +120 +720 +5040 +40320 +362880 +3628800 diff --git a/05/tcc-final-old/tests/tests2/16_nesting.c b/05/tcc-final-old/tests/tests2/16_nesting.c new file mode 100644 index 0000000..2b72cc0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/16_nesting.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int x, y, z; + + for (x = 0; x < 2; x++) + { + for (y = 0; y < 3; y++) + { + for (z = 0; z < 3; z++) + { + printf("%d %d %d\n", x, y, z); + } + } + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/16_nesting.expect b/05/tcc-final-old/tests/tests2/16_nesting.expect new file mode 100644 index 0000000..5a3431e --- /dev/null +++ b/05/tcc-final-old/tests/tests2/16_nesting.expect @@ -0,0 +1,18 @@ +0 0 0 +0 0 1 +0 0 2 +0 1 0 +0 1 1 +0 1 2 +0 2 0 +0 2 1 +0 2 2 +1 0 0 +1 0 1 +1 0 2 +1 1 0 +1 1 1 +1 1 2 +1 2 0 +1 2 1 +1 2 2 diff --git a/05/tcc-final-old/tests/tests2/17_enum.c b/05/tcc-final-old/tests/tests2/17_enum.c new file mode 100644 index 0000000..e2bc736 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/17_enum.c @@ -0,0 +1,72 @@ +#include + +enum fred +{ + a, + b, + c, + d, + e = 54, + f = 73, + g, + h +}; + +/* All following uses of enum efoo should compile + without warning. While forward enums aren't ISO C, + it's accepted by GCC also in strict mode, and only warned + about with -pedantic. This happens in the real world. */ +/* Strict ISO C doesn't allow this kind of forward declaration of + enums, but GCC accepts it (and gives only pedantic warning), and + it occurs in the wild. */ +enum efoo; +struct Sforward_use { + int (*fmember) (enum efoo x); +}; + +extern enum efoo it_real_fn(void); +enum efoo { + ONE, + TWO, +}; +struct S2 { + enum efoo (*f2) (void); +}; +void should_compile(struct S2 *s) +{ + s->f2 = it_real_fn; +} + +enum efoo it_real_fn(void) +{ + return TWO; +} + +static unsigned int deref_uintptr(unsigned int *p) +{ + return *p; +} + +enum Epositive { + epos_one, epos_two +}; + +int main() +{ + enum fred frod; + enum Epositive epos = epos_two; + + printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h); + /* printf("%d\n", frod); */ + frod = 12; + printf("%d\n", frod); + frod = e; + printf("%d\n", frod); + + /* Following should compile without warning. */ + printf ("enum to int: %u\n", deref_uintptr(&epos)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/17_enum.expect b/05/tcc-final-old/tests/tests2/17_enum.expect new file mode 100644 index 0000000..d453a61 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/17_enum.expect @@ -0,0 +1,4 @@ +0 1 2 3 54 73 74 75 +12 +54 +enum to int: 1 diff --git a/05/tcc-final-old/tests/tests2/18_include.c b/05/tcc-final-old/tests/tests2/18_include.c new file mode 100644 index 0000000..dbae3aa --- /dev/null +++ b/05/tcc-final-old/tests/tests2/18_include.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + printf("including\n"); +#include "18_include.h" + printf("done\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/18_include.expect b/05/tcc-final-old/tests/tests2/18_include.expect new file mode 100644 index 0000000..58c6d29 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/18_include.expect @@ -0,0 +1,3 @@ +including +included +done diff --git a/05/tcc-final-old/tests/tests2/18_include.h b/05/tcc-final-old/tests/tests2/18_include.h new file mode 100644 index 0000000..dc86080 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/18_include.h @@ -0,0 +1 @@ +printf("included\n"); diff --git a/05/tcc-final-old/tests/tests2/19_pointer_arithmetic.c b/05/tcc-final-old/tests/tests2/19_pointer_arithmetic.c new file mode 100644 index 0000000..aff65e5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/19_pointer_arithmetic.c @@ -0,0 +1,28 @@ +#include + +int main() +{ + int a; + int *b; + int *c; + + a = 42; + b = &a; + c = NULL; + + printf("%d\n", *b); + + if (b == NULL) + printf("b is NULL\n"); + else + printf("b is not NULL\n"); + + if (c == NULL) + printf("c is NULL\n"); + else + printf("c is not NULL\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/19_pointer_arithmetic.expect b/05/tcc-final-old/tests/tests2/19_pointer_arithmetic.expect new file mode 100644 index 0000000..0cf781b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/19_pointer_arithmetic.expect @@ -0,0 +1,3 @@ +42 +b is not NULL +c is NULL diff --git a/05/tcc-final-old/tests/tests2/20_pointer_comparison.c b/05/tcc-final-old/tests/tests2/20_pointer_comparison.c new file mode 100644 index 0000000..825f778 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/20_pointer_comparison.c @@ -0,0 +1,24 @@ +#include + +int main() +{ + int a; + int b; + int *d; + int *e; + d = &a; + e = &b; + a = 12; + b = 34; + printf("%d\n", *d); + printf("%d\n", *e); + printf("%d\n", d == e); + printf("%d\n", d != e); + d = e; + printf("%d\n", d == e); + printf("%d\n", d != e); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/20_pointer_comparison.expect b/05/tcc-final-old/tests/tests2/20_pointer_comparison.expect new file mode 100644 index 0000000..5d1e5f5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/20_pointer_comparison.expect @@ -0,0 +1,6 @@ +12 +34 +0 +1 +1 +0 diff --git a/05/tcc-final-old/tests/tests2/21_char_array.c b/05/tcc-final-old/tests/tests2/21_char_array.c new file mode 100644 index 0000000..f22f527 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/21_char_array.c @@ -0,0 +1,33 @@ +#include + +int main() +{ + int x = 'a'; + char y = x; + + char *a = "hello"; + + printf("%s\n", a); + + int c; + c = *a; + + char *b; + for (b = a; *b != 0; b++) + printf("%c: %d\n", *b, *b); + + char destarray[10]; + char *dest = &destarray[0]; + char *src = a; + + while (*src != 0) + *dest++ = *src++; + + *dest = 0; + + printf("copied string is %s\n", destarray); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/21_char_array.expect b/05/tcc-final-old/tests/tests2/21_char_array.expect new file mode 100644 index 0000000..dbc6068 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/21_char_array.expect @@ -0,0 +1,7 @@ +hello +h: 104 +e: 101 +l: 108 +l: 108 +o: 111 +copied string is hello diff --git a/05/tcc-final-old/tests/tests2/22_floating_point.c b/05/tcc-final-old/tests/tests2/22_floating_point.c new file mode 100644 index 0000000..e3491f5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/22_floating_point.c @@ -0,0 +1,50 @@ +#include +#include + +int main() +{ + // variables + float a = 12.34 + 56.78; + printf("%f\n", a); + + // infix operators + printf("%f\n", 12.34 + 56.78); + printf("%f\n", 12.34 - 56.78); + printf("%f\n", 12.34 * 56.78); + printf("%f\n", 12.34 / 56.78); + + // comparison operators + printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78); + printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34); + printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34); + + // assignment operators + a = 12.34; + a += 56.78; + printf("%f\n", a); + + a = 12.34; + a -= 56.78; + printf("%f\n", a); + + a = 12.34; + a *= 56.78; + printf("%f\n", a); + + a = 12.34; + a /= 56.78; + printf("%f\n", a); + + // prefix operators + printf("%f\n", +12.34); + printf("%f\n", -12.34); + + // type coercion + a = 2; + printf("%f\n", a); + printf("%f\n", sin(2)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/22_floating_point.expect b/05/tcc-final-old/tests/tests2/22_floating_point.expect new file mode 100644 index 0000000..75ea3a7 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/22_floating_point.expect @@ -0,0 +1,16 @@ +69.120003 +69.120000 +-44.440000 +700.665200 +0.217330 +1 1 0 0 0 1 +0 1 1 1 0 0 +0 0 0 1 1 1 +69.120003 +-44.439999 +700.665222 +0.217330 +12.340000 +-12.340000 +2.000000 +0.909297 diff --git a/05/tcc-final-old/tests/tests2/23_type_coercion.c b/05/tcc-final-old/tests/tests2/23_type_coercion.c new file mode 100644 index 0000000..1fcc335 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/23_type_coercion.c @@ -0,0 +1,54 @@ +#include + +void charfunc(char a) +{ + printf("char: %c\n", a); +} + +void intfunc(int a) +{ + printf("int: %d\n", a); +} + +void floatfunc(float a) +{ + printf("float: %f\n", a); +} + +int main() +{ + charfunc('a'); + charfunc(98); + charfunc(99.0); + + intfunc('a'); + intfunc(98); + intfunc(99.0); + + floatfunc('a'); + floatfunc(98); + floatfunc(99.0); + + /* printf("%c %d %f\n", 'a', 'b', 'c'); */ + /* printf("%c %d %f\n", 97, 98, 99); */ + /* printf("%c %d %f\n", 97.0, 98.0, 99.0); */ + + char b = 97; + char c = 97.0; + + printf("%d %d\n", b, c); + + int d = 'a'; + int e = 97.0; + + printf("%d %d\n", d, e); + + float f = 'a'; + float g = 97; + + printf("%f %f\n", f, g); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/23_type_coercion.expect b/05/tcc-final-old/tests/tests2/23_type_coercion.expect new file mode 100644 index 0000000..d9076f0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/23_type_coercion.expect @@ -0,0 +1,12 @@ +char: a +char: b +char: c +int: 97 +int: 98 +int: 99 +float: 97.000000 +float: 98.000000 +float: 99.000000 +97 97 +97 97 +97.000000 97.000000 diff --git a/05/tcc-final-old/tests/tests2/24_math_library.c b/05/tcc-final-old/tests/tests2/24_math_library.c new file mode 100644 index 0000000..514a25f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/24_math_library.c @@ -0,0 +1,30 @@ +#define _ISOC99_SOURCE 1 + +#include +#include + +int main() +{ + printf("%f\n", sin(0.12)); + printf("%f\n", cos(0.12)); + printf("%f\n", tan(0.12)); + printf("%f\n", asin(0.12)); + printf("%f\n", acos(0.12)); + printf("%f\n", atan(0.12)); + printf("%f\n", sinh(0.12)); + printf("%f\n", cosh(0.12)); + printf("%f\n", tanh(0.12)); + printf("%f\n", exp(0.12)); + printf("%f\n", fabs(-0.12)); + printf("%f\n", log(0.12)); + printf("%f\n", log10(0.12)); + printf("%f\n", pow(0.12, 0.12)); + printf("%f\n", sqrt(0.12)); + printf("%f\n", round(12.34)); + printf("%f\n", ceil(12.34)); + printf("%f\n", floor(12.34)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/24_math_library.expect b/05/tcc-final-old/tests/tests2/24_math_library.expect new file mode 100644 index 0000000..99f7299 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/24_math_library.expect @@ -0,0 +1,18 @@ +0.119712 +0.992809 +0.120579 +0.120290 +1.450506 +0.119429 +0.120288 +1.007209 +0.119427 +1.127497 +0.120000 +-2.120264 +-0.920819 +0.775357 +0.346410 +12.000000 +13.000000 +12.000000 diff --git a/05/tcc-final-old/tests/tests2/25_quicksort.c b/05/tcc-final-old/tests/tests2/25_quicksort.c new file mode 100644 index 0000000..5cc08bd --- /dev/null +++ b/05/tcc-final-old/tests/tests2/25_quicksort.c @@ -0,0 +1,83 @@ +#include + +int array[16]; + +//Swap integer values by array indexes +void swap(int a, int b) +{ + int tmp = array[a]; + array[a] = array[b]; + array[b] = tmp; +} + +//Partition the array into two halves and return the +//index about which the array is partitioned +int partition(int left, int right) +{ + int pivotIndex = left; + int pivotValue = array[pivotIndex]; + int index = left; + int i; + + swap(pivotIndex, right); + for(i = left; i < right; i++) + { + if(array[i] < pivotValue) + { + swap(i, index); + index += 1; + } + } + swap(right, index); + + return index; +} + +//Quicksort the array +void quicksort(int left, int right) +{ + if(left >= right) + return; + + int index = partition(left, right); + quicksort(left, index - 1); + quicksort(index + 1, right); +} + +int main() +{ + int i; + + array[0] = 62; + array[1] = 83; + array[2] = 4; + array[3] = 89; + array[4] = 36; + array[5] = 21; + array[6] = 74; + array[7] = 37; + array[8] = 65; + array[9] = 33; + array[10] = 96; + array[11] = 38; + array[12] = 53; + array[13] = 16; + array[14] = 74; + array[15] = 55; + + for (i = 0; i < 16; i++) + printf("%d ", array[i]); + + printf("\n"); + + quicksort(0, 15); + + for (i = 0; i < 16; i++) + printf("%d ", array[i]); + + printf("\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/25_quicksort.expect b/05/tcc-final-old/tests/tests2/25_quicksort.expect new file mode 100644 index 0000000..2d39cd3 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/25_quicksort.expect @@ -0,0 +1,2 @@ +62 83 4 89 36 21 74 37 65 33 96 38 53 16 74 55 +4 16 21 33 36 37 38 53 55 62 65 74 74 83 89 96 diff --git a/05/tcc-final-old/tests/tests2/26_character_constants.c b/05/tcc-final-old/tests/tests2/26_character_constants.c new file mode 100644 index 0000000..95c4423 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/26_character_constants.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + printf("%d\n", '\1'); + printf("%d\n", '\10'); + printf("%d\n", '\100'); + printf("%d\n", '\x01'); + printf("%d\n", '\x0e'); + printf("%d\n", '\x10'); + printf("%d\n", '\x40'); + printf("test \x40\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/26_character_constants.expect b/05/tcc-final-old/tests/tests2/26_character_constants.expect new file mode 100644 index 0000000..8f8bfa4 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/26_character_constants.expect @@ -0,0 +1,8 @@ +1 +8 +64 +1 +14 +16 +64 +test @ diff --git a/05/tcc-final-old/tests/tests2/27_sizeof.c b/05/tcc-final-old/tests/tests2/27_sizeof.c new file mode 100644 index 0000000..5ae0ede --- /dev/null +++ b/05/tcc-final-old/tests/tests2/27_sizeof.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + char a; + int b; + double c; + + printf("%d\n", sizeof(a)); + printf("%d\n", sizeof(b)); + printf("%d\n", sizeof(c)); + + printf("%d\n", sizeof(!a)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/27_sizeof.expect b/05/tcc-final-old/tests/tests2/27_sizeof.expect new file mode 100644 index 0000000..a47ea3a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/27_sizeof.expect @@ -0,0 +1,4 @@ +1 +4 +8 +4 diff --git a/05/tcc-final-old/tests/tests2/28_strings.c b/05/tcc-final-old/tests/tests2/28_strings.c new file mode 100644 index 0000000..2db2298 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/28_strings.c @@ -0,0 +1,45 @@ +#include +#include + +int main() +{ + char a[10]; + + strcpy(a, "hello"); + printf("%s\n", a); + + strncpy(a, "gosh", 2); + printf("%s\n", a); + + printf("%d\n", strcmp(a, "apple") > 0); + printf("%d\n", strcmp(a, "goere") > 0); + printf("%d\n", strcmp(a, "zebra") < 0); + + printf("%d\n", strlen(a)); + + strcat(a, "!"); + printf("%s\n", a); + + printf("%d\n", strncmp(a, "apple", 2) > 0); + printf("%d\n", strncmp(a, "goere", 2) == 0); + printf("%d\n", strncmp(a, "goerg", 2) == 0); + printf("%d\n", strncmp(a, "zebra", 2) < 0); + + printf("%s\n", strchr(a, 'o')); + printf("%s\n", strrchr(a, 'l')); + printf("%d\n", strrchr(a, 'x') == NULL); + + memset(&a[1], 'r', 4); + printf("%s\n", a); + + memcpy(&a[2], a, 2); + printf("%s\n", a); + + printf("%d\n", memcmp(a, "apple", 4) > 0); + printf("%d\n", memcmp(a, "grgr", 4) == 0); + printf("%d\n", memcmp(a, "zebra", 4) < 0); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/28_strings.expect b/05/tcc-final-old/tests/tests2/28_strings.expect new file mode 100644 index 0000000..fd9217a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/28_strings.expect @@ -0,0 +1,19 @@ +hello +gollo +1 +1 +1 +5 +gollo! +1 +1 +1 +1 +ollo! +lo! +1 +grrrr! +grgrr! +1 +1 +1 diff --git a/05/tcc-final-old/tests/tests2/29_array_address.c b/05/tcc-final-old/tests/tests2/29_array_address.c new file mode 100644 index 0000000..bda5ddd --- /dev/null +++ b/05/tcc-final-old/tests/tests2/29_array_address.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + char a[10]; + strcpy(a, "abcdef"); + printf("%s\n", &a[1]); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/29_array_address.expect b/05/tcc-final-old/tests/tests2/29_array_address.expect new file mode 100644 index 0000000..9bc8683 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/29_array_address.expect @@ -0,0 +1 @@ +bcdef diff --git a/05/tcc-final-old/tests/tests2/30_hanoi.c b/05/tcc-final-old/tests/tests2/30_hanoi.c new file mode 100644 index 0000000..7c0893b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/30_hanoi.c @@ -0,0 +1,122 @@ +/* example from http://barnyard.syr.edu/quickies/hanoi.c */ + +/* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */ +/* By Terry R. McConnell (12/2/97) */ +/* Compile: cc -o hanoi hanoi.c */ + +/* This program does no error checking. But then, if it's right, + it's right ... right ? */ + + +/* The original towers of hanoi problem seems to have been originally posed + by one M. Claus in 1883. There is a popular legend that goes along with + it that has been often repeated and paraphrased. It goes something like this: + In the great temple at Benares there are 3 golden spikes. On one of them, + God placed 64 disks increasing in size from bottom to top, at the beginning + of time. Since then, and to this day, the priest on duty constantly transfers + disks, one at a time, in such a way that no larger disk is ever put on top + of a smaller one. When the disks have been transferred entirely to another + spike the Universe will come to an end in a large thunderclap. + + This paraphrases the original legend due to DeParville, La Nature, Paris 1884, + Part I, 285-286. For this and further information see: Mathematical + Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967, + 303-305. + * + * + */ + +#include +#include + +#define TRUE 1 +#define FALSE 0 + +/* This is the number of "disks" on tower A initially. Taken to be 64 in the + * legend. The number of moves required, in general, is 2^N - 1. For N = 64, + * this is 18,446,744,073,709,551,615 */ +#define N 4 + +/* These are the three towers. For example if the state of A is 0,1,3,4, that + * means that there are three discs on A of sizes 1, 3, and 4. (Think of right + * as being the "down" direction.) */ +int A[N], B[N], C[N]; + +void Hanoi(int,int*,int*,int*); + +/* Print the current configuration of A, B, and C to the screen */ +void PrintAll() +{ + int i; + + printf("A: "); + for(i=0;i + +int main(int argc, char **argv) +{ + int Count; + + printf("hello world %d\n", argc); + for (Count = 1; Count < argc; Count++) + printf("arg %d: %s\n", Count, argv[Count]); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/31_args.expect b/05/tcc-final-old/tests/tests2/31_args.expect new file mode 100644 index 0000000..8c60bfc --- /dev/null +++ b/05/tcc-final-old/tests/tests2/31_args.expect @@ -0,0 +1,6 @@ +hello world 6 +arg 1: arg1 +arg 2: arg2 +arg 3: arg3 +arg 4: arg4 +arg 5: arg5 diff --git a/05/tcc-final-old/tests/tests2/32_led.c b/05/tcc-final-old/tests/tests2/32_led.c new file mode 100644 index 0000000..5596cbf --- /dev/null +++ b/05/tcc-final-old/tests/tests2/32_led.c @@ -0,0 +1,266 @@ +/* example from http://barnyard.syr.edu/quickies/led.c */ + +/* led.c: print out number as if on 7 line led display. I.e., write integer + given on command line like this: + _ _ _ + | _| _| |_| |_ + | |_ _| | _| etc. + + We assume the terminal behaves like a classical teletype. So the top + lines of all digits have to be printed first, then the middle lines of + all digits, etc. + + By Terry R. McConnell + +compile: cc -o led led.c + +If you just want to link in the subroutine print_led that does all the +work, compile with -DNO_MAIN, and declare the following in any source file +that uses the call: + +extern void print_led(unsigned long x, char *buf); + +Bug: you cannot call repeatedly to print more than one number to a line. +That would require curses or some other terminal API that allows moving the +cursor to a previous line. + +*/ + + + +#include +#include + +#define MAX_DIGITS 32 +#define NO_MAIN + + +/* Print the top line of the digit d into buffer. + Does not null terminate buffer. */ + +void topline(int d, char *p){ + + *p++ = ' '; + switch(d){ + + /* all these have _ on top line */ + + case 0: + case 2: + case 3: + case 5: + case 7: + case 8: + case 9: + *p++ = '_'; + break; + default: + *p++=' '; + + } + *p++=' '; +} + +/* Print the middle line of the digit d into the buffer. + Does not null terminate. */ + +void midline(int d, char *p){ + + switch(d){ + + /* those that have leading | on middle line */ + + case 0: + case 4: + case 5: + case 6: + case 8: + case 9: + *p++='|'; + break; + default: + *p++=' '; + } + switch(d){ + + /* those that have _ on middle line */ + + case 2: + case 3: + case 4: + case 5: + case 6: + case 8: + case 9: + *p++='_'; + break; + default: + *p++=' '; + + } + switch(d){ + + /* those that have closing | on middle line */ + + case 0: + case 1: + case 2: + case 3: + case 4: + case 7: + case 8: + case 9: + *p++='|'; + break; + default: + *p++=' '; + + } +} + +/* Print the bottom line of the digit d. Does not null terminate. */ + +void botline(int d, char *p){ + + + switch(d){ + + /* those that have leading | on bottom line */ + + case 0: + case 2: + case 6: + case 8: + *p++='|'; + break; + default: + *p++=' '; + } + switch(d){ + + /* those that have _ on bottom line */ + + case 0: + case 2: + case 3: + case 5: + case 6: + case 8: + *p++='_'; + break; + default: + *p++=' '; + + } + switch(d){ + + /* those that have closing | on bottom line */ + + case 0: + case 1: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + *p++='|'; + break; + default: + *p++=' '; + + } +} + +/* Write the led representation of integer to string buffer. */ + +void print_led(unsigned long x, char *buf) +{ + + int i=0,n; + static int d[MAX_DIGITS]; + + + /* extract digits from x */ + + n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */ + + while(x){ + d[n++] = (int)(x%10L); + if(n >= MAX_DIGITS)break; + x = x/10L; + } + + /* print top lines of all digits */ + + for(i=n-1;i>=0;i--){ + topline(d[i],buf); + buf += 3; + *buf++=' '; + } + *buf++='\n'; /* move teletype to next line */ + + /* print middle lines of all digits */ + + for(i=n-1;i>=0;i--){ + midline(d[i],buf); + buf += 3; + *buf++=' '; + } + *buf++='\n'; + + /* print bottom lines of all digits */ + + for(i=n-1;i>=0;i--){ + botline(d[i],buf); + buf += 3; + *buf++=' '; + } + *buf++='\n'; + *buf='\0'; +} + +int main() +{ + char buf[5*MAX_DIGITS]; + print_led(1234567, buf); + printf("%s\n",buf); + + return 0; +} + +#ifndef NO_MAIN +int main(int argc, char **argv) +{ + + int i=0,n; + long x; + static int d[MAX_DIGITS]; + char buf[5*MAX_DIGITS]; + + if(argc != 2){ + fprintf(stderr,"led: usage: led integer\n"); + return 1; + } + + /* fetch argument from command line */ + + x = atol(argv[1]); + + /* sanity check */ + + if(x<0){ + fprintf(stderr,"led: %d must be non-negative\n",x); + return 1; + } + + print_led(x,buf); + printf("%s\n",buf); + + return 0; + +} +#endif + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/32_led.expect b/05/tcc-final-old/tests/tests2/32_led.expect new file mode 100644 index 0000000..c53b58a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/32_led.expect @@ -0,0 +1,4 @@ + _ _ _ _ + | _| _| |_| |_ |_ | + | |_ _| | _| |_| | + diff --git a/05/tcc-final-old/tests/tests2/33_ternary_op.c b/05/tcc-final-old/tests/tests2/33_ternary_op.c new file mode 100644 index 0000000..8579b50 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/33_ternary_op.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int Count; + + for (Count = 0; Count < 10; Count++) + { + printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3)); + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/33_ternary_op.expect b/05/tcc-final-old/tests/tests2/33_ternary_op.expect new file mode 100644 index 0000000..45ea507 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/33_ternary_op.expect @@ -0,0 +1,10 @@ +0 +1 +4 +9 +16 +15 +18 +21 +24 +27 diff --git a/05/tcc-final-old/tests/tests2/34_array_assignment.c b/05/tcc-final-old/tests/tests2/34_array_assignment.c new file mode 100644 index 0000000..5885c97 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/34_array_assignment.c @@ -0,0 +1,23 @@ +#include + +int main() +{ + int a[4]; + + a[0] = 12; + a[1] = 23; + a[2] = 34; + a[3] = 45; + + printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]); + + int b[4]; + + b = a; + + printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/34_array_assignment.expect b/05/tcc-final-old/tests/tests2/34_array_assignment.expect new file mode 100644 index 0000000..9736bf5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/34_array_assignment.expect @@ -0,0 +1,2 @@ +12 23 34 45 +12 23 34 45 diff --git a/05/tcc-final-old/tests/tests2/35_sizeof.c b/05/tcc-final-old/tests/tests2/35_sizeof.c new file mode 100644 index 0000000..672e87e --- /dev/null +++ b/05/tcc-final-old/tests/tests2/35_sizeof.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + char a; + short b; + + printf("%d %d\n", sizeof(char), sizeof(a)); + printf("%d %d\n", sizeof(short), sizeof(b)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/35_sizeof.expect b/05/tcc-final-old/tests/tests2/35_sizeof.expect new file mode 100644 index 0000000..534fb83 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/35_sizeof.expect @@ -0,0 +1,2 @@ +1 1 +2 2 diff --git a/05/tcc-final-old/tests/tests2/36_array_initialisers.c b/05/tcc-final-old/tests/tests2/36_array_initialisers.c new file mode 100644 index 0000000..1bc8ee0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/36_array_initialisers.c @@ -0,0 +1,21 @@ +#include + +int main() +{ + int Count; + + int Array[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753 }; + + for (Count = 0; Count < 10; Count++) + printf("%d: %d\n", Count, Array[Count]); + + int Array2[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753, }; + + for (Count = 0; Count < 10; Count++) + printf("%d: %d\n", Count, Array2[Count]); + + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/36_array_initialisers.expect b/05/tcc-final-old/tests/tests2/36_array_initialisers.expect new file mode 100644 index 0000000..3ac6c77 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/36_array_initialisers.expect @@ -0,0 +1,20 @@ +0: 12 +1: 34 +2: 56 +3: 78 +4: 90 +5: 123 +6: 456 +7: 789 +8: 8642 +9: 9753 +0: 12 +1: 34 +2: 56 +3: 78 +4: 90 +5: 123 +6: 456 +7: 789 +8: 8642 +9: 9753 diff --git a/05/tcc-final-old/tests/tests2/37_sprintf.c b/05/tcc-final-old/tests/tests2/37_sprintf.c new file mode 100644 index 0000000..1dd1dce --- /dev/null +++ b/05/tcc-final-old/tests/tests2/37_sprintf.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + char Buf[100]; + int Count; + + for (Count = 1; Count <= 20; Count++) + { + sprintf(Buf, "->%02d<-\n", Count); + printf("%s", Buf); + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/37_sprintf.expect b/05/tcc-final-old/tests/tests2/37_sprintf.expect new file mode 100644 index 0000000..a643da8 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/37_sprintf.expect @@ -0,0 +1,20 @@ +->01<- +->02<- +->03<- +->04<- +->05<- +->06<- +->07<- +->08<- +->09<- +->10<- +->11<- +->12<- +->13<- +->14<- +->15<- +->16<- +->17<- +->18<- +->19<- +->20<- diff --git a/05/tcc-final-old/tests/tests2/38_multiple_array_index.c b/05/tcc-final-old/tests/tests2/38_multiple_array_index.c new file mode 100644 index 0000000..4e1868e --- /dev/null +++ b/05/tcc-final-old/tests/tests2/38_multiple_array_index.c @@ -0,0 +1,32 @@ +#include + +int main() +{ + int a[4][4]; + int b = 0; + int x; + int y; + + for (x = 0; x < 4; x++) + { + for (y = 0; y < 4; y++) + { + b++; + a[x][y] = b; + } + } + + for (x = 0; x < 4; x++) + { + printf("x=%d: ", x); + for (y = 0; y < 4; y++) + { + printf("%d ", a[x][y]); + } + printf("\n"); + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/38_multiple_array_index.expect b/05/tcc-final-old/tests/tests2/38_multiple_array_index.expect new file mode 100644 index 0000000..747ad75 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/38_multiple_array_index.expect @@ -0,0 +1,4 @@ +x=0: 1 2 3 4 +x=1: 5 6 7 8 +x=2: 9 10 11 12 +x=3: 13 14 15 16 diff --git a/05/tcc-final-old/tests/tests2/39_typedef.c b/05/tcc-final-old/tests/tests2/39_typedef.c new file mode 100644 index 0000000..da73f71 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/39_typedef.c @@ -0,0 +1,65 @@ +#include + +typedef int MyInt; + +struct FunStruct +{ + int i; + int j; +}; + +typedef struct FunStruct MyFunStruct; + +typedef MyFunStruct *MoreFunThanEver; + +int main() +{ + MyInt a = 1; + printf("%d\n", a); + + MyFunStruct b; + b.i = 12; + b.j = 34; + printf("%d,%d\n", b.i, b.j); + + MoreFunThanEver c = &b; + printf("%d,%d\n", c->i, c->j); + + return 0; +} + +/* "If the specification of an array type includes any type qualifiers, + the element type is so-qualified, not the array type." */ + +typedef int A[3]; +extern A const ca; +extern const A ca; +extern const int ca[3]; + +typedef A B[1][2]; +extern B const cb; +extern const B cb; +extern const int cb[1][2][3]; + +extern B b; +extern int b[1][2][3]; + +/* Funny but valid function declaration. */ +typedef int functype (int); +extern functype func; +int func(int i) +{ + return i + 1; +} + +/* Even funnier function decl and definition using typeof. */ +int set_anon_super(void); +int set_anon_super(void) +{ + return 42; +} +typedef int sas_type (void); +extern typeof(set_anon_super) set_anon_super; +extern sas_type set_anon_super; + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/39_typedef.expect b/05/tcc-final-old/tests/tests2/39_typedef.expect new file mode 100644 index 0000000..b9050a9 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/39_typedef.expect @@ -0,0 +1,3 @@ +1 +12,34 +12,34 diff --git a/05/tcc-final-old/tests/tests2/40_stdio.c b/05/tcc-final-old/tests/tests2/40_stdio.c new file mode 100644 index 0000000..b986093 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/40_stdio.c @@ -0,0 +1,52 @@ +#include + +int main() +{ + FILE *f = fopen("fred.txt", "w"); + fwrite("hello\nhello\n", 1, 12, f); + fclose(f); + + char freddy[7]; + f = fopen("fred.txt", "r"); + if (fread(freddy, 1, 6, f) != 6) + printf("couldn't read fred.txt\n"); + + freddy[6] = '\0'; + fclose(f); + + printf("%s", freddy); + + int InChar; + char ShowChar; + f = fopen("fred.txt", "r"); + while ( (InChar = fgetc(f)) != EOF) + { + ShowChar = InChar; + if (ShowChar < ' ') + ShowChar = '.'; + + printf("ch: %d '%c'\n", InChar, ShowChar); + } + fclose(f); + + f = fopen("fred.txt", "r"); + while ( (InChar = getc(f)) != EOF) + { + ShowChar = InChar; + if (ShowChar < ' ') + ShowChar = '.'; + + printf("ch: %d '%c'\n", InChar, ShowChar); + } + fclose(f); + + f = fopen("fred.txt", "r"); + while (fgets(freddy, sizeof(freddy), f) != NULL) + printf("x: %s", freddy); + + fclose(f); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/40_stdio.expect b/05/tcc-final-old/tests/tests2/40_stdio.expect new file mode 100644 index 0000000..e08167a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/40_stdio.expect @@ -0,0 +1,27 @@ +hello +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +ch: 104 'h' +ch: 101 'e' +ch: 108 'l' +ch: 108 'l' +ch: 111 'o' +ch: 10 '.' +x: hello +x: hello diff --git a/05/tcc-final-old/tests/tests2/41_hashif.c b/05/tcc-final-old/tests/tests2/41_hashif.c new file mode 100644 index 0000000..cb37b9e --- /dev/null +++ b/05/tcc-final-old/tests/tests2/41_hashif.c @@ -0,0 +1,85 @@ +#include + +int main() +{ + printf("#include test\n"); + +#if 1 +#if 0 + printf("a\n"); +#else + printf("b\n"); +#endif +#else +#if 0 + printf("c\n"); +#else + printf("d\n"); +#endif +#endif + +#if 0 +#if 1 + printf("e\n"); +#else + printf("f\n"); +#endif +#else +#if 1 + printf("g\n"); +#else + printf("h\n"); +#endif +#endif + +#define DEF + +#ifdef DEF +#ifdef DEF + printf("i\n"); +#else + printf("j\n"); +#endif +#else +#ifdef DEF + printf("k\n"); +#else + printf("l\n"); +#endif +#endif + +#ifndef DEF +#ifndef DEF + printf("m\n"); +#else + printf("n\n"); +#endif +#else +#ifndef DEF + printf("o\n"); +#else + printf("p\n"); +#endif +#endif + +#define ONE 1 +#define ZERO 0 + +#if ONE +#if ZERO + printf("q\n"); +#else + printf("r\n"); +#endif +#else +#if ZERO + printf("s\n"); +#else + printf("t\n"); +#endif +#endif + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/41_hashif.expect b/05/tcc-final-old/tests/tests2/41_hashif.expect new file mode 100644 index 0000000..5fd414b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/41_hashif.expect @@ -0,0 +1,6 @@ +#include test +b +g +i +p +r diff --git a/05/tcc-final-old/tests/tests2/42_function_pointer.c b/05/tcc-final-old/tests/tests2/42_function_pointer.c new file mode 100644 index 0000000..697bd79 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/42_function_pointer.c @@ -0,0 +1,22 @@ +#include + +int fred(int p) +{ + printf("yo %d\n", p); + return 42; +} + +int (*f)(int) = &fred; + +/* To test what this is supposed to test the destination function + (fprint here) must not be called directly anywhere in the test. */ +int (*fprintfptr)(FILE *, const char *, ...) = &fprintf; + +int main() +{ + fprintfptr(stdout, "%d\n", (*f)(24)); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/42_function_pointer.expect b/05/tcc-final-old/tests/tests2/42_function_pointer.expect new file mode 100644 index 0000000..6c8b6ce --- /dev/null +++ b/05/tcc-final-old/tests/tests2/42_function_pointer.expect @@ -0,0 +1,2 @@ +yo 24 +42 diff --git a/05/tcc-final-old/tests/tests2/43_void_param.c b/05/tcc-final-old/tests/tests2/43_void_param.c new file mode 100644 index 0000000..de17098 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/43_void_param.c @@ -0,0 +1,15 @@ +#include + +void fred(void) +{ + printf("yo\n"); +} + +int main() +{ + fred(); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/43_void_param.expect b/05/tcc-final-old/tests/tests2/43_void_param.expect new file mode 100644 index 0000000..092bfb9 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/43_void_param.expect @@ -0,0 +1 @@ +yo diff --git a/05/tcc-final-old/tests/tests2/44_scoped_declarations.c b/05/tcc-final-old/tests/tests2/44_scoped_declarations.c new file mode 100644 index 0000000..f38664f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/44_scoped_declarations.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + int a; + + for (a = 0; a < 2; a++) + { + int b = a; + } + + printf("it's all good\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/44_scoped_declarations.expect b/05/tcc-final-old/tests/tests2/44_scoped_declarations.expect new file mode 100644 index 0000000..231ccc0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/44_scoped_declarations.expect @@ -0,0 +1 @@ +it's all good diff --git a/05/tcc-final-old/tests/tests2/45_empty_for.c b/05/tcc-final-old/tests/tests2/45_empty_for.c new file mode 100644 index 0000000..7cef513 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/45_empty_for.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + int Count = 0; + + for (;;) + { + Count++; + printf("%d\n", Count); + if (Count >= 10) + break; + } + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/45_empty_for.expect b/05/tcc-final-old/tests/tests2/45_empty_for.expect new file mode 100644 index 0000000..f00c965 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/45_empty_for.expect @@ -0,0 +1,10 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/05/tcc-final-old/tests/tests2/46_grep.c b/05/tcc-final-old/tests/tests2/46_grep.c new file mode 100644 index 0000000..049dfb1 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/46_grep.c @@ -0,0 +1,568 @@ +/* + * The information in this document is subject to change + * without notice and should not be construed as a commitment + * by Digital Equipment Corporation or by DECUS. + * + * Neither Digital Equipment Corporation, DECUS, nor the authors + * assume any responsibility for the use or reliability of this + * document or the described software. + * + * Copyright (C) 1980, DECUS + * + * General permission to copy or modify, but not for profit, is + * hereby granted, provided that the above copyright notice is + * included and reference made to the fact that reproduction + * privileges were granted by DECUS. + */ +#include +#include +#include // tolower() + +/* + * grep + * + * Runs on the Decus compiler or on vms, On vms, define as: + * grep :== "$disk:[account]grep" (native) + * grep :== "$disk:[account]grep grep" (Decus) + * See below for more information. + */ + +char *documentation[] = { + "grep searches a file for a given pattern. Execute by", + " grep [flags] regular_expression file_list\n", + "Flags are single characters preceded by '-':", + " -c Only a count of matching lines is printed", + " -f Print file name for matching lines switch, see below", + " -n Each line is preceded by its line number", + " -v Only print non-matching lines\n", + "The file_list is a list of files (wildcards are acceptable on RSX modes).", + "\nThe file name is normally printed if there is a file given.", + "The -f flag reverses this action (print name no file, not if more).\n", + 0 }; + +char *patdoc[] = { + "The regular_expression defines the pattern to search for. Upper- and", + "lower-case are always ignored. Blank lines never match. The expression", + "should be quoted to prevent file-name translation.", + "x An ordinary character (not mentioned below) matches that character.", + "'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.", + "'^' A circumflex at the beginning of an expression matches the", + " beginning of a line.", + "'$' A dollar-sign at the end of an expression matches the end of a line.", + "'.' A period matches any character except \"new-line\".", + "':a' A colon matches a class of characters described by the following", + "':d' character. \":a\" matches any alphabetic, \":d\" matches digits,", + "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and", + "': ' other control characters, such as new-line.", + "'*' An expression followed by an asterisk matches zero or more", + " occurrences of that expression: \"fo*\" matches \"f\", \"fo\"", + " \"foo\", etc.", + "'+' An expression followed by a plus sign matches one or more", + " occurrences of that expression: \"fo+\" matches \"fo\", etc.", + "'-' An expression followed by a minus sign optionally matches", + " the expression.", + "'[]' A string enclosed in square brackets matches any character in", + " that string, but no others. If the first character in the", + " string is a circumflex, the expression matches any character", + " except \"new-line\" and the characters in the string. For", + " example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"", + " matches \"abc\" but not \"axb\". A range of characters may be", + " specified by two characters separated by \"-\". Note that,", + " [a-z] matches alphabetics, while [z-a] never matches.", + "The concatenation of regular expressions is a regular expression.", + 0}; + +#define LMAX 512 +#define PMAX 256 + +#define CHAR 1 +#define BOL 2 +#define EOL 3 +#define ANY 4 +#define CLASS 5 +#define NCLASS 6 +#define STAR 7 +#define PLUS 8 +#define MINUS 9 +#define ALPHA 10 +#define DIGIT 11 +#define NALPHA 12 +#define PUNCT 13 +#define RANGE 14 +#define ENDPAT 15 + +int cflag=0, fflag=0, nflag=0, vflag=0, nfile=0, debug=0; + +char *pp, lbuf[LMAX], pbuf[PMAX]; + +char *cclass(); +char *pmatch(); +void store(int); +void error(char *); +void badpat(char *, char *, char *); +int match(void); + + +/*** Display a file name *******************************/ +void file(char *s) +{ + printf("File %s:\n", s); +} + +/*** Report unopenable file ****************************/ +void cant(char *s) +{ + fprintf(stderr, "%s: cannot open\n", s); +} + +/*** Give good help ************************************/ +void help(char **hp) +{ + char **dp; + + for (dp = hp; *dp; ++dp) + printf("%s\n", *dp); +} + +/*** Display usage summary *****************************/ +void usage(char *s) +{ + fprintf(stderr, "?GREP-E-%s\n", s); + fprintf(stderr, + "Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n"); + exit(1); +} + +/*** Compile the pattern into global pbuf[] ************/ +void compile(char *source) +{ + char *s; /* Source string pointer */ + char *lp; /* Last pattern pointer */ + int c; /* Current character */ + int o; /* Temp */ + char *spp; /* Save beginning of pattern */ + + s = source; + if (debug) + printf("Pattern = \"%s\"\n", s); + pp = pbuf; + while (c = *s++) { + /* + * STAR, PLUS and MINUS are special. + */ + if (c == '*' || c == '+' || c == '-') { + if (pp == pbuf || + (o=pp[-1]) == BOL || + o == EOL || + o == STAR || + o == PLUS || + o == MINUS) + badpat("Illegal occurrence op.", source, s); + store(ENDPAT); + store(ENDPAT); + spp = pp; /* Save pattern end */ + while (--pp > lp) /* Move pattern down */ + *pp = pp[-1]; /* one byte */ + *pp = (c == '*') ? STAR : + (c == '-') ? MINUS : PLUS; + pp = spp; /* Restore pattern end */ + continue; + } + /* + * All the rest. + */ + lp = pp; /* Remember start */ + switch(c) { + + case '^': + store(BOL); + break; + + case '$': + store(EOL); + break; + + case '.': + store(ANY); + break; + + case '[': + s = cclass(source, s); + break; + + case ':': + if (*s) { + switch(tolower(c = *s++)) { + + case 'a': + case 'A': + store(ALPHA); + break; + + case 'd': + case 'D': + store(DIGIT); + break; + + case 'n': + case 'N': + store(NALPHA); + break; + + case ' ': + store(PUNCT); + break; + + default: + badpat("Unknown : type", source, s); + + } + break; + } + else badpat("No : type", source, s); + + case '\\': + if (*s) + c = *s++; + + default: + store(CHAR); + store(tolower(c)); + } + } + store(ENDPAT); + store(0); /* Terminate string */ + if (debug) { + for (lp = pbuf; lp < pp;) { + if ((c = (*lp++ & 0377)) < ' ') + printf("\\%o ", c); + else printf("%c ", c); + } + printf("\n"); + } +} + +/*** Compile a class (within []) ***********************/ +char *cclass(char *source, char *src) + /* char *source; // Pattern start -- for error msg. */ + /* char *src; // Class start */ +{ + char *s; /* Source pointer */ + char *cp; /* Pattern start */ + int c; /* Current character */ + int o; /* Temp */ + + s = src; + o = CLASS; + if (*s == '^') { + ++s; + o = NCLASS; + } + store(o); + cp = pp; + store(0); /* Byte count */ + while ((c = *s++) && c!=']') { + if (c == '\\') { /* Store quoted char */ + if ((c = *s++) == '\0') /* Gotta get something */ + badpat("Class terminates badly", source, s); + else store(tolower(c)); + } + else if (c == '-' && + (pp - cp) > 1 && *s != ']' && *s != '\0') { + c = pp[-1]; /* Range start */ + pp[-1] = RANGE; /* Range signal */ + store(c); /* Re-store start */ + c = *s++; /* Get end char and*/ + store(tolower(c)); /* Store it */ + } + else { + store(tolower(c)); /* Store normal char */ + } + } + if (c != ']') + badpat("Unterminated class", source, s); + if ((c = (pp - cp)) >= 256) + badpat("Class too large", source, s); + if (c == 0) + badpat("Empty class", source, s); + *cp = c; + return(s); +} + +/*** Store an entry in the pattern buffer **************/ +void store(int op) +{ + if (pp >= &pbuf[PMAX]) + error("Pattern too complex\n"); + *pp++ = op; +} + +/*** Report a bad pattern specification ****************/ +void badpat(char *message, char *source, char *stop) + /* char *message; // Error message */ + /* char *source; // Pattern start */ + /* char *stop; // Pattern end */ +{ + fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source); + fprintf(stderr, "-GREP-E-Stopped at byte %ld, '%c'\n", + stop-source, stop[-1]); + error("?GREP-E-Bad pattern\n"); +} + +/*** Scan the file for the pattern in pbuf[] ***********/ +void grep(FILE *fp, char *fn) + /* FILE *fp; // File to process */ + /* char *fn; // File name (for -f option) */ +{ + int lno, count, m; + + lno = 0; + count = 0; + while (fgets(lbuf, LMAX, fp)) { + ++lno; + m = match(); + if ((m && !vflag) || (!m && vflag)) { + ++count; + if (!cflag) { + if (fflag && fn) { + file(fn); + fn = 0; + } + if (nflag) + printf("%d\t", lno); + printf("%s\n", lbuf); + } + } + } + if (cflag) { + if (fflag && fn) + file(fn); + printf("%d\n", count); + } +} + +/*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/ +int match() +{ + char *l; /* Line pointer */ + + for (l = lbuf; *l; ++l) { + if (pmatch(l, pbuf)) + return(1); + } + return(0); +} + +/*** Match partial line with pattern *******************/ +char *pmatch(char *line, char *pattern) + /* char *line; // (partial) line to match */ + /* char *pattern; // (partial) pattern to match */ +{ + char *l; /* Current line pointer */ + char *p; /* Current pattern pointer */ + char c; /* Current character */ + char *e; /* End for STAR and PLUS match */ + int op; /* Pattern operation */ + int n; /* Class counter */ + char *are; /* Start of STAR match */ + + l = line; + if (debug > 1) + printf("pmatch(\"%s\")\n", line); + p = pattern; + while ((op = *p++) != ENDPAT) { + if (debug > 1) + printf("byte[%ld] = 0%o, '%c', op = 0%o\n", + l-line, *l, *l, op); + switch(op) { + + case CHAR: + if (tolower(*l++) != *p++) + return(0); + break; + + case BOL: + if (l != lbuf) + return(0); + break; + + case EOL: + if (*l != '\0') + return(0); + break; + + case ANY: + if (*l++ == '\0') + return(0); + break; + + case DIGIT: + if ((c = *l++) < '0' || (c > '9')) + return(0); + break; + + case ALPHA: + c = tolower(*l++); + if (c < 'a' || c > 'z') + return(0); + break; + + case NALPHA: + c = tolower(*l++); + if (c >= 'a' && c <= 'z') + break; + else if (c < '0' || c > '9') + return(0); + break; + + case PUNCT: + c = *l++; + if (c == 0 || c > ' ') + return(0); + break; + + case CLASS: + case NCLASS: + c = tolower(*l++); + n = *p++ & 0377; + do { + if (*p == RANGE) { + p += 3; + n -= 2; + if (c >= p[-2] && c <= p[-1]) + break; + } + else if (c == *p++) + break; + } while (--n > 1); + if ((op == CLASS) == (n <= 1)) + return(0); + if (op == CLASS) + p += n - 2; + break; + + case MINUS: + e = pmatch(l, p); /* Look for a match */ + while (*p++ != ENDPAT); /* Skip over pattern */ + if (e) /* Got a match? */ + l = e; /* Yes, update string */ + break; /* Always succeeds */ + + case PLUS: /* One or more ... */ + if ((l = pmatch(l, p)) == 0) + return(0); /* Gotta have a match */ + case STAR: /* Zero or more ... */ + are = l; /* Remember line start */ + while (*l && (e = pmatch(l, p))) + l = e; /* Get longest match */ + while (*p++ != ENDPAT); /* Skip over pattern */ + while (l >= are) { /* Try to match rest */ + if (e = pmatch(l, p)) + return(e); + --l; /* Nope, try earlier */ + } + return(0); /* Nothing else worked */ + + default: + printf("Bad op code %d\n", op); + error("Cannot happen -- match\n"); + } + } + return(l); +} + +/*** Report an error ***********************************/ +void error(char *s) +{ + fprintf(stderr, "%s", s); + exit(1); +} + +/*** Main program - parse arguments & grep *************/ +int main(int argc, char **argv) +{ + char *p; + int c, i; + int gotpattern; + + FILE *f; + + if (argc <= 1) + usage("No arguments"); + if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) { + help(documentation); + help(patdoc); + return 0; + } + nfile = argc-1; + gotpattern = 0; + for (i=1; i < argc; ++i) { + p = argv[i]; + if (*p == '-') { + ++p; + while (c = *p++) { + switch(tolower(c)) { + + case '?': + help(documentation); + break; + + case 'C': + case 'c': + ++cflag; + break; + + case 'D': + case 'd': + ++debug; + break; + + case 'F': + case 'f': + ++fflag; + break; + + case 'n': + case 'N': + ++nflag; + break; + + case 'v': + case 'V': + ++vflag; + break; + + default: + usage("Unknown flag"); + } + } + argv[i] = 0; + --nfile; + } else if (!gotpattern) { + compile(p); + argv[i] = 0; + ++gotpattern; + --nfile; + } + } + if (!gotpattern) + usage("No pattern"); + if (nfile == 0) + grep(stdin, 0); + else { + fflag = fflag ^ (nfile > 0); + for (i=1; i < argc; ++i) { + if (p = argv[i]) { + if ((f=fopen(p, "r")) == NULL) + cant(p); + else { + grep(f, p); + fclose(f); + } + } + } + } + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/46_grep.expect b/05/tcc-final-old/tests/tests2/46_grep.expect new file mode 100644 index 0000000..e8a6791 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/46_grep.expect @@ -0,0 +1,3 @@ +File 46_grep.c: +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ + diff --git a/05/tcc-final-old/tests/tests2/47_switch_return.c b/05/tcc-final-old/tests/tests2/47_switch_return.c new file mode 100644 index 0000000..1ec7924 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/47_switch_return.c @@ -0,0 +1,24 @@ +#include + +void fred(int x) +{ + switch (x) + { + case 1: printf("1\n"); return; + case 2: printf("2\n"); break; + case 3: printf("3\n"); return; + } + + printf("out\n"); +} + +int main() +{ + fred(1); + fred(2); + fred(3); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/47_switch_return.expect b/05/tcc-final-old/tests/tests2/47_switch_return.expect new file mode 100644 index 0000000..b6deb7e --- /dev/null +++ b/05/tcc-final-old/tests/tests2/47_switch_return.expect @@ -0,0 +1,4 @@ +1 +2 +out +3 diff --git a/05/tcc-final-old/tests/tests2/48_nested_break.c b/05/tcc-final-old/tests/tests2/48_nested_break.c new file mode 100644 index 0000000..5bc5ba4 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/48_nested_break.c @@ -0,0 +1,26 @@ +#include + +int main() +{ + int a; + char b; + + a = 0; + while (a < 2) + { + printf("%d", a++); + break; + + b = 'A'; + while (b < 'C') + { + printf("%c", b++); + } + printf("e"); + } + printf("\n"); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/48_nested_break.expect b/05/tcc-final-old/tests/tests2/48_nested_break.expect new file mode 100644 index 0000000..573541a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/48_nested_break.expect @@ -0,0 +1 @@ +0 diff --git a/05/tcc-final-old/tests/tests2/49_bracket_evaluation.c b/05/tcc-final-old/tests/tests2/49_bracket_evaluation.c new file mode 100644 index 0000000..0cbe57d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/49_bracket_evaluation.c @@ -0,0 +1,23 @@ +#include + +struct point +{ + double x; + double y; +}; + +struct point point_array[100]; + +int main() +{ + int my_point = 10; + + point_array[my_point].x = 12.34; + point_array[my_point].y = 56.78; + + printf("%f, %f\n", point_array[my_point].x, point_array[my_point].y); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/49_bracket_evaluation.expect b/05/tcc-final-old/tests/tests2/49_bracket_evaluation.expect new file mode 100644 index 0000000..1da66db --- /dev/null +++ b/05/tcc-final-old/tests/tests2/49_bracket_evaluation.expect @@ -0,0 +1 @@ +12.340000, 56.780000 diff --git a/05/tcc-final-old/tests/tests2/50_logical_second_arg.c b/05/tcc-final-old/tests/tests2/50_logical_second_arg.c new file mode 100644 index 0000000..ddec08c --- /dev/null +++ b/05/tcc-final-old/tests/tests2/50_logical_second_arg.c @@ -0,0 +1,29 @@ +#include + +int fred() +{ + printf("fred\n"); + return 0; +} + +int joe() +{ + printf("joe\n"); + return 1; +} + +int main() +{ + printf("%d\n", fred() && joe()); + printf("%d\n", fred() || joe()); + printf("%d\n", joe() && fred()); + printf("%d\n", joe() || fred()); + printf("%d\n", fred() && (1 + joe())); + printf("%d\n", fred() || (0 + joe())); + printf("%d\n", joe() && (0 + fred())); + printf("%d\n", joe() || (1 + fred())); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/50_logical_second_arg.expect b/05/tcc-final-old/tests/tests2/50_logical_second_arg.expect new file mode 100644 index 0000000..d6174ae --- /dev/null +++ b/05/tcc-final-old/tests/tests2/50_logical_second_arg.expect @@ -0,0 +1,20 @@ +fred +0 +fred +joe +1 +joe +fred +0 +joe +1 +fred +0 +fred +joe +1 +joe +fred +0 +joe +1 diff --git a/05/tcc-final-old/tests/tests2/51_static.c b/05/tcc-final-old/tests/tests2/51_static.c new file mode 100644 index 0000000..d6c0917 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/51_static.c @@ -0,0 +1,30 @@ +#include + +static int fred = 1234; +static int joe; + +void henry() +{ + static int fred = 4567; + + printf("%d\n", fred); + fred++; +} + +int main() +{ + printf("%d\n", fred); + henry(); + henry(); + henry(); + henry(); + printf("%d\n", fred); + fred = 8901; + joe = 2345; + printf("%d\n", fred); + printf("%d\n", joe); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/51_static.expect b/05/tcc-final-old/tests/tests2/51_static.expect new file mode 100644 index 0000000..18224fa --- /dev/null +++ b/05/tcc-final-old/tests/tests2/51_static.expect @@ -0,0 +1,8 @@ +1234 +4567 +4568 +4569 +4570 +1234 +8901 +2345 diff --git a/05/tcc-final-old/tests/tests2/52_unnamed_enum.c b/05/tcc-final-old/tests/tests2/52_unnamed_enum.c new file mode 100644 index 0000000..d0395b2 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/52_unnamed_enum.c @@ -0,0 +1,27 @@ +#include + +enum fred { a, b, c }; + +int main() +{ + printf("a=%d\n", a); + printf("b=%d\n", b); + printf("c=%d\n", c); + + enum fred d; + + typedef enum { e, f, g } h; + typedef enum { i, j, k } m; + + printf("e=%d\n", e); + printf("f=%d\n", f); + printf("g=%d\n", g); + + printf("i=%d\n", i); + printf("j=%d\n", j); + printf("k=%d\n", k); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/52_unnamed_enum.expect b/05/tcc-final-old/tests/tests2/52_unnamed_enum.expect new file mode 100644 index 0000000..84f2ac8 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/52_unnamed_enum.expect @@ -0,0 +1,9 @@ +a=0 +b=1 +c=2 +e=0 +f=1 +g=2 +i=0 +j=1 +k=2 diff --git a/05/tcc-final-old/tests/tests2/54_goto.c b/05/tcc-final-old/tests/tests2/54_goto.c new file mode 100644 index 0000000..2e151bb --- /dev/null +++ b/05/tcc-final-old/tests/tests2/54_goto.c @@ -0,0 +1,56 @@ +#include + +void fred() +{ + printf("In fred()\n"); + goto done; + printf("In middle\n"); +done: + printf("At end\n"); +} + +void joe() +{ + int b = 5678; + + printf("In joe()\n"); + + { + int c = 1234; + printf("c = %d\n", c); + goto outer; + printf("uh-oh\n"); + } + +outer: + + printf("done\n"); +} + +void henry() +{ + int a; + + printf("In henry()\n"); + goto inner; + + { + int b; +inner: + b = 1234; + printf("b = %d\n", b); + } + + printf("done\n"); +} + +int main() +{ + fred(); + joe(); + henry(); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/54_goto.expect b/05/tcc-final-old/tests/tests2/54_goto.expect new file mode 100644 index 0000000..8e553fa --- /dev/null +++ b/05/tcc-final-old/tests/tests2/54_goto.expect @@ -0,0 +1,8 @@ +In fred() +At end +In joe() +c = 1234 +done +In henry() +b = 1234 +done diff --git a/05/tcc-final-old/tests/tests2/55_lshift_type.c b/05/tcc-final-old/tests/tests2/55_lshift_type.c new file mode 100644 index 0000000..aa3e51a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/55_lshift_type.c @@ -0,0 +1,52 @@ +/* $Id: lshift-type.c 53089 2012-07-06 11:18:26Z vinc17/ypig $ + +Tests on left-shift type, written by Vincent Lefevre . + +ISO C99 TC3 says: [6.5.7#3] "The integer promotions are performed on +each of the operands. The type of the result is that of the promoted +left operand." +*/ + +#include + +#define PTYPE(M) ((M) < 0 || -(M) < 0 ? -1 : 1) * (int) sizeof((M)+0) +#define CHECK(X,T) check(#X, PTYPE(X), PTYPE((X) << (T) 1)) +#define TEST1(X,T) do { CHECK(X,T); CHECK(X,unsigned T); } while (0) +#define TEST2(X) \ + do \ + { \ + TEST1((X),short); \ + TEST1((X),int); \ + TEST1((X),long); \ + TEST1((X),long long); \ + } \ + while (0) +#define TEST3(X,T) do { TEST2((T)(X)); TEST2((unsigned T)(X)); } while (0) +#define TEST4(X) \ + do \ + { \ + TEST3((X),short); \ + TEST3((X),int); \ + TEST3((X),long); \ + TEST3((X),long long); \ + } \ + while (0) + +static int debug, nfailed = 0; + +static void check (const char *s, int arg1, int shift) +{ + int failed = arg1 != shift; + if (debug || failed) + printf ("%s %d %d\n", s, arg1, shift); + nfailed += failed; +} + +int main (int argc, char **argv) +{ + debug = argc > 1; + TEST4(1); + TEST4(-1); + printf ("%d test(s) failed\n", nfailed); + return nfailed != 0; +} diff --git a/05/tcc-final-old/tests/tests2/55_lshift_type.expect b/05/tcc-final-old/tests/tests2/55_lshift_type.expect new file mode 100644 index 0000000..8523767 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/55_lshift_type.expect @@ -0,0 +1 @@ +0 test(s) failed diff --git a/05/tcc-final-old/tests/tests2/60_errors_and_warnings.c b/05/tcc-final-old/tests/tests2/60_errors_and_warnings.c new file mode 100644 index 0000000..0028caf --- /dev/null +++ b/05/tcc-final-old/tests/tests2/60_errors_and_warnings.c @@ -0,0 +1,51 @@ +#if defined test_56_btype_excess_1 +struct A {} int i; + +#elif defined test_57_btype_excess_2 +char int i; + +#elif defined test_58_function_redefinition +int f(void) { return 0; } +int f(void) { return 1; } + +#elif defined test_global_redefinition +int xxx = 1; +int xxx; +int xxx = 2; + +#elif defined test_59_function_array +int (*fct)[42](int x); + +#elif defined test_60_enum_redefinition +enum color { RED, GREEN, BLUE }; +enum color { R, G, B }; +enum color c; + +#elif defined test_62_enumerator_redefinition +enum color { RED, GREEN, BLUE }; +enum rgb { RED, G, B}; +enum color c = RED; + +#elif defined test_63_local_enumerator_redefinition +enum { + FOO, + BAR +}; + +int main(void) +{ + enum { + FOO = 2, + BAR + }; + + return BAR - FOO; +} + +#elif defined test_61_undefined_enum +enum rgb3 c = 42; + +#elif defined test_74_non_const_init +int i = i++; + +#endif diff --git a/05/tcc-final-old/tests/tests2/60_errors_and_warnings.expect b/05/tcc-final-old/tests/tests2/60_errors_and_warnings.expect new file mode 100644 index 0000000..ed6a690 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/60_errors_and_warnings.expect @@ -0,0 +1,28 @@ +[test_56_btype_excess_1] +60_errors_and_warnings.c:2: error: too many basic types + +[test_57_btype_excess_2] +60_errors_and_warnings.c:5: error: too many basic types + +[test_58_function_redefinition] +60_errors_and_warnings.c:9: error: redefinition of 'f' + +[test_global_redefinition] +60_errors_and_warnings.c:14: error: redefinition of 'xxx' + +[test_59_function_array] +60_errors_and_warnings.c:17: error: declaration of an array of functions + +[test_60_enum_redefinition] +60_errors_and_warnings.c:21: error: struct/union/enum already defined + +[test_62_enumerator_redefinition] +60_errors_and_warnings.c:26: error: redefinition of enumerator 'RED' + +[test_63_local_enumerator_redefinition] + +[test_61_undefined_enum] +60_errors_and_warnings.c:46: error: unknown type size + +[test_74_non_const_init] +60_errors_and_warnings.c:49: error: initializer element is not constant diff --git a/05/tcc-final-old/tests/tests2/64_macro_nesting.c b/05/tcc-final-old/tests/tests2/64_macro_nesting.c new file mode 100644 index 0000000..676e5d3 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/64_macro_nesting.c @@ -0,0 +1,12 @@ +#include // printf() + +#define CAT2(a,b) a##b +#define CAT(a,b) CAT2(a,b) +#define AB(x) CAT(x,y) + +int main(void) +{ + int xy = 42; + printf("%d\n", CAT(A,B)(x)); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/64_macro_nesting.expect b/05/tcc-final-old/tests/tests2/64_macro_nesting.expect new file mode 100644 index 0000000..d81cc07 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/64_macro_nesting.expect @@ -0,0 +1 @@ +42 diff --git a/05/tcc-final-old/tests/tests2/67_macro_concat.c b/05/tcc-final-old/tests/tests2/67_macro_concat.c new file mode 100644 index 0000000..c580d3a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/67_macro_concat.c @@ -0,0 +1,14 @@ +#include + +#define P(A,B) A ## B ; bob +#define Q(A,B) A ## B+ + +int main(void) +{ + int bob, jim = 21; + bob = P(jim,) *= 2; + printf("jim: %d, bob: %d\n", jim, bob); + jim = 60 Q(+,)3; + printf("jim: %d\n", jim); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/67_macro_concat.expect b/05/tcc-final-old/tests/tests2/67_macro_concat.expect new file mode 100644 index 0000000..8386c2d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/67_macro_concat.expect @@ -0,0 +1,2 @@ +jim: 21, bob: 42 +jim: 63 diff --git a/05/tcc-final-old/tests/tests2/70_floating_point_literals.c b/05/tcc-final-old/tests/tests2/70_floating_point_literals.c new file mode 100644 index 0000000..012fb4f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/70_floating_point_literals.c @@ -0,0 +1,77 @@ +#include + +int main() +{ + /* decimal floating constant */ + float fa0 = .123f; + float fa1 = .123E12F; + float fa2 = .123e-12f; + float fa3 = .123e+12f; + printf("%f\n%f\n%f\n%f\n\n", fa0, fa1, fa2, fa3); + + float fb0 = 123.123f; + float fb1 = 123.123E12F; + float fb2 = 123.123e-12f; + float fb3 = 123.123e+12f; + printf("%f\n%f\n%f\n%f\n\n", fb0, fb1, fb2, fb3); + + float fc0 = 123.f; + float fc1 = 123.E12F; + float fc2 = 123.e-12f; + float fc3 = 123.e+12f; + printf("%f\n%f\n%f\n%f\n\n", fc0, fc1, fc2, fc3); + + float fd0 = 123E12F; + float fd1 = 123e-12f; + float fd2 = 123e+12f; + printf("%f\n%f\n%f\n\n", fd0, fd1, fd2); + printf("\n"); + + /* hexadecimal floating constant */ + double da0 = 0X.1ACP12; + double da1 = 0x.1acp-12; + double da2 = 0x.1acp+12; + printf("%f\n%f\n%f\n\n", da0, da1, da2); + + double db0 = 0X1AC.BDP12; + double db1 = 0x1ac.bdp-12; + double db2 = 0x1ac.dbp+12; + printf("%f\n%f\n%f\n\n", db0, db1, db2); + + double dc0 = 0X1AC.P12; + double dc1 = 0x1ac.p-12; + double dc2 = 0x1ac.p+12; + printf("%f\n%f\n%f\n\n", dc0, dc1, dc2); + + double dd0 = 0X1ACP12; + double dd1 = 0x1acp-12; + double dd2 = 0x1acp+12; + printf("%f\n%f\n%f\n\n", dd0, dd1, dd2); + printf("\n"); + +#ifdef __TINYC__ + /* TCC extension + binary floating constant */ + long double la0 = 0B.110101100P12L; + long double la1 = 0b.110101100p-12l; + long double la2 = 0b.110101100p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", la0, la1, la2); + + long double lb0 = 0B110101100.10111101P12L; + long double lb1 = 0b110101100.10111101p-12l; + long double lb2 = 0b110101100.10111101p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", lb0, lb1, lb2); + + long double lc0 = 0B110101100.P12L; + long double lc1 = 0b110101100.p-12l; + long double lc2 = 0b110101100.p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", lc0, lc1, lc2); + + long double ld0 = 0B110101100P12L; + long double ld1 = 0b110101100p-12l; + long double ld2 = 0b110101100p+12l; + printf("%Lf\n%Lf\n%Lf\n\n", ld0, ld1, ld2); +#endif + + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/70_floating_point_literals.expect b/05/tcc-final-old/tests/tests2/70_floating_point_literals.expect new file mode 100644 index 0000000..7eb1efb --- /dev/null +++ b/05/tcc-final-old/tests/tests2/70_floating_point_literals.expect @@ -0,0 +1,53 @@ +0.123000 +122999996416.000000 +0.000000 +122999996416.000000 + +123.123001 +123122997002240.000000 +0.000000 +123122997002240.000000 + +123.000000 +123000003231744.000000 +0.000000 +123000003231744.000000 + +123000003231744.000000 +0.000000 +123000003231744.000000 + + +428.000000 +0.000026 +428.000000 + +1756112.000000 +0.104672 +1756592.000000 + +1753088.000000 +0.104492 +1753088.000000 + +1753088.000000 +0.104492 +1753088.000000 + + +3424.000000 +0.000204 +3424.000000 + +1756112.000000 +0.104672 +1756112.000000 + +1753088.000000 +0.104492 +1753088.000000 + +1753088.000000 +0.104492 +1753088.000000 + diff --git a/05/tcc-final-old/tests/tests2/71_macro_empty_arg.c b/05/tcc-final-old/tests/tests2/71_macro_empty_arg.c new file mode 100644 index 0000000..f0d3511 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/71_macro_empty_arg.c @@ -0,0 +1,9 @@ +#include + +#define T(a,b,c) a b c + +int main(void) +{ + printf("%d", T(1,+,2) T(+,,) T(,2,*) T(,7,) T(,,)); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/71_macro_empty_arg.expect b/05/tcc-final-old/tests/tests2/71_macro_empty_arg.expect new file mode 100644 index 0000000..98d9bcb --- /dev/null +++ b/05/tcc-final-old/tests/tests2/71_macro_empty_arg.expect @@ -0,0 +1 @@ +17 diff --git a/05/tcc-final-old/tests/tests2/72_long_long_constant.c b/05/tcc-final-old/tests/tests2/72_long_long_constant.c new file mode 100644 index 0000000..6608213 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/72_long_long_constant.c @@ -0,0 +1,19 @@ +#include + +int main() +{ + long long int res = 0; + + if (res < -2147483648LL) { + printf("Error: 0 < -2147483648\n"); + return 1; + } + else + if (2147483647LL < res) { + printf("Error: 2147483647 < 0\n"); + return 2; + } + else + printf("long long constant test ok.\n"); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/72_long_long_constant.expect b/05/tcc-final-old/tests/tests2/72_long_long_constant.expect new file mode 100644 index 0000000..dda9e66 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/72_long_long_constant.expect @@ -0,0 +1 @@ +long long constant test ok. diff --git a/05/tcc-final-old/tests/tests2/73_arm64.c b/05/tcc-final-old/tests/tests2/73_arm64.c new file mode 100644 index 0000000..8de61b3 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/73_arm64.c @@ -0,0 +1,527 @@ +// This program is designed to test some arm64-specific things, such as the +// calling convention, but should give the same results on any architecture. + +#include +#include +#include + +struct s1 { char x[1]; } s1 = { "0" }; +struct s2 { char x[2]; } s2 = { "12" }; +struct s3 { char x[3]; } s3 = { "345" }; +struct s4 { char x[4]; } s4 = { "6789" }; +struct s5 { char x[5]; } s5 = { "abcde" }; +struct s6 { char x[6]; } s6 = { "fghijk" }; +struct s7 { char x[7]; } s7 = { "lmnopqr" }; +struct s8 { char x[8]; } s8 = { "stuvwxyz" }; +struct s9 { char x[9]; } s9 = { "ABCDEFGHI" }; +struct s10 { char x[10]; } s10 = { "JKLMNOPQRS" }; +struct s11 { char x[11]; } s11 = { "TUVWXYZ0123" }; +struct s12 { char x[12]; } s12 = { "456789abcdef" }; +struct s13 { char x[13]; } s13 = { "ghijklmnopqrs" }; +struct s14 { char x[14]; } s14 = { "tuvwxyzABCDEFG" }; +struct s15 { char x[15]; } s15 = { "HIJKLMNOPQRSTUV" }; +struct s16 { char x[16]; } s16 = { "WXYZ0123456789ab" }; +struct s17 { char x[17]; } s17 = { "cdefghijklmnopqrs" }; + +struct hfa11 { float a; } hfa11 = { 11.1 }; +struct hfa12 { float a, b; } hfa12 = { 12.1, 12.2 }; +struct hfa13 { float a, b, c; } hfa13 = { 13.1, 13.2, 13.3 }; +struct hfa14 { float a, b, c, d; } hfa14 = { 14.1, 14.2, 14.3, 14.4 }; + +struct hfa21 { double a; } hfa21 = { 21.1 }; +struct hfa22 { double a, b; } hfa22 = { 22.1, 22.2 }; +struct hfa23 { double a, b, c; } hfa23 = { 23.1, 23.2, 23.3 }; +struct hfa24 { double a, b, c, d; } hfa24 = { 24.1, 24.2, 24.3, 24.4 }; + +struct hfa31 { long double a; } hfa31 = { 31.1 }; +struct hfa32 { long double a, b; } hfa32 = { 32.1, 32.2 }; +struct hfa33 { long double a, b, c; } hfa33 = { 33.1, 33.2, 33.3 }; +struct hfa34 { long double a, b, c, d; } hfa34 = { 34.1, 34.2, 34.3, 34.4 }; + +void fa_s1(struct s1 a) { printf("%.1s\n", a.x); } +void fa_s2(struct s2 a) { printf("%.2s\n", a.x); } +void fa_s3(struct s3 a) { printf("%.3s\n", a.x); } +void fa_s4(struct s4 a) { printf("%.4s\n", a.x); } +void fa_s5(struct s5 a) { printf("%.5s\n", a.x); } +void fa_s6(struct s6 a) { printf("%.6s\n", a.x); } +void fa_s7(struct s7 a) { printf("%.7s\n", a.x); } +void fa_s8(struct s8 a) { printf("%.8s\n", a.x); } +void fa_s9(struct s9 a) { printf("%.9s\n", a.x); } +void fa_s10(struct s10 a) { printf("%.10s\n", a.x); } +void fa_s11(struct s11 a) { printf("%.11s\n", a.x); } +void fa_s12(struct s12 a) { printf("%.12s\n", a.x); } +void fa_s13(struct s13 a) { printf("%.13s\n", a.x); } +void fa_s14(struct s14 a) { printf("%.14s\n", a.x); } +void fa_s15(struct s15 a) { printf("%.15s\n", a.x); } +void fa_s16(struct s16 a) { printf("%.16s\n", a.x); } +void fa_s17(struct s17 a) { printf("%.17s\n", a.x); } + +void fa_hfa11(struct hfa11 a) +{ printf("%.1f\n", a.a); } +void fa_hfa12(struct hfa12 a) +{ printf("%.1f %.1f\n", a.a, a.a); } +void fa_hfa13(struct hfa13 a) +{ printf("%.1f %.1f %.1f\n", a.a, a.b, a.c); } +void fa_hfa14(struct hfa14 a) +{ printf("%.1f %.1f %.1f %.1f\n", a.a, a.b, a.c, a.d); } + +void fa_hfa21(struct hfa21 a) +{ printf("%.1f\n", a.a); } +void fa_hfa22(struct hfa22 a) +{ printf("%.1f %.1f\n", a.a, a.a); } +void fa_hfa23(struct hfa23 a) +{ printf("%.1f %.1f %.1f\n", a.a, a.b, a.c); } +void fa_hfa24(struct hfa24 a) +{ printf("%.1f %.1f %.1f %.1f\n", a.a, a.b, a.c, a.d); } + +void fa_hfa31(struct hfa31 a) +{ printf("%.1Lf\n", a.a); } +void fa_hfa32(struct hfa32 a) +{ printf("%.1Lf %.1Lf\n", a.a, a.a); } +void fa_hfa33(struct hfa33 a) +{ printf("%.1Lf %.1Lf %.1Lf\n", a.a, a.b, a.c); } +void fa_hfa34(struct hfa34 a) +{ printf("%.1Lf %.1Lf %.1Lf %.1Lf\n", a.a, a.b, a.c, a.d); } + +void fa1(struct s8 a, struct s9 b, struct s10 c, struct s11 d, + struct s12 e, struct s13 f) +{ + printf("%.3s %.3s %.3s %.3s %.3s %.3s\n", a.x, b.x, c.x, d.x, e.x, f.x); +} + +void fa2(struct s9 a, struct s10 b, struct s11 c, struct s12 d, + struct s13 e, struct s14 f) +{ + printf("%.3s %.3s %.3s %.3s %.3s %.3s\n", a.x, b.x, c.x, d.x, e.x, f.x); +} + +void fa3(struct hfa14 a, struct hfa23 b, struct hfa32 c) +{ + printf("%.1f %.1f %.1f %.1f %.1Lf %.1Lf\n", + a.a, a.d, b.a, b.c, c.a, c.b); +} + +void fa4(struct s1 a, struct hfa14 b, struct s2 c, struct hfa24 d, + struct s3 e, struct hfa34 f) +{ + printf("%.1s %.1f %.1f %.2s %.1f %.1f %.3s %.1Lf %.1Lf\n", + a.x, b.a, b.d, c.x, d.a, d.d, e.x, f.a, f.d); +} + +void arg(void) +{ + printf("Arguments:\n"); + fa_s1(s1); + fa_s2(s2); + fa_s3(s3); + fa_s4(s4); + fa_s5(s5); + fa_s6(s6); + fa_s7(s7); + fa_s8(s8); + fa_s9(s9); + fa_s10(s10); + fa_s11(s11); + fa_s12(s12); + fa_s13(s13); + fa_s14(s14); + fa_s15(s15); + fa_s16(s16); + fa_s17(s17); + fa_hfa11(hfa11); + fa_hfa12(hfa12); + fa_hfa13(hfa13); + fa_hfa14(hfa14); + fa_hfa21(hfa21); + fa_hfa22(hfa22); + fa_hfa23(hfa23); + fa_hfa24(hfa24); + fa_hfa31(hfa31); + fa_hfa32(hfa32); + fa_hfa33(hfa33); + fa_hfa34(hfa34); + fa1(s8, s9, s10, s11, s12, s13); + fa2(s9, s10, s11, s12, s13, s14); + fa3(hfa14, hfa23, hfa32); + fa4(s1, hfa14, s2, hfa24, s3, hfa34); +} + +struct s1 fr_s1(void) { return s1; } +struct s2 fr_s2(void) { return s2; } +struct s3 fr_s3(void) { return s3; } +struct s4 fr_s4(void) { return s4; } +struct s5 fr_s5(void) { return s5; } +struct s6 fr_s6(void) { return s6; } +struct s7 fr_s7(void) { return s7; } +struct s8 fr_s8(void) { return s8; } +struct s9 fr_s9(void) { return s9; } +struct s10 fr_s10(void) { return s10; } +struct s11 fr_s11(void) { return s11; } +struct s12 fr_s12(void) { return s12; } +struct s13 fr_s13(void) { return s13; } +struct s14 fr_s14(void) { return s14; } +struct s15 fr_s15(void) { return s15; } +struct s16 fr_s16(void) { return s16; } +struct s17 fr_s17(void) { return s17; } + +struct hfa11 fr_hfa11(void) { return hfa11; } +struct hfa12 fr_hfa12(void) { return hfa12; } +struct hfa13 fr_hfa13(void) { return hfa13; } +struct hfa14 fr_hfa14(void) { return hfa14; } + +struct hfa21 fr_hfa21(void) { return hfa21; } +struct hfa22 fr_hfa22(void) { return hfa22; } +struct hfa23 fr_hfa23(void) { return hfa23; } +struct hfa24 fr_hfa24(void) { return hfa24; } + +struct hfa31 fr_hfa31(void) { return hfa31; } +struct hfa32 fr_hfa32(void) { return hfa32; } +struct hfa33 fr_hfa33(void) { return hfa33; } +struct hfa34 fr_hfa34(void) { return hfa34; } + +void ret(void) +{ + struct s1 t1 = fr_s1(); + struct s2 t2 = fr_s2(); + struct s3 t3 = fr_s3(); + struct s4 t4 = fr_s4(); + struct s5 t5 = fr_s5(); + struct s6 t6 = fr_s6(); + struct s7 t7 = fr_s7(); + struct s8 t8 = fr_s8(); + struct s9 t9 = fr_s9(); + struct s10 t10 = fr_s10(); + struct s11 t11 = fr_s11(); + struct s12 t12 = fr_s12(); + struct s13 t13 = fr_s13(); + struct s14 t14 = fr_s14(); + struct s15 t15 = fr_s15(); + struct s16 t16 = fr_s16(); + struct s17 t17 = fr_s17(); + printf("Return values:\n"); + printf("%.1s\n", t1.x); + printf("%.2s\n", t2.x); + printf("%.3s\n", t3.x); + printf("%.4s\n", t4.x); + printf("%.5s\n", t5.x); + printf("%.6s\n", t6.x); + printf("%.7s\n", t7.x); + printf("%.8s\n", t8.x); + printf("%.9s\n", t9.x); + printf("%.10s\n", t10.x); + printf("%.11s\n", t11.x); + printf("%.12s\n", t12.x); + printf("%.13s\n", t13.x); + printf("%.14s\n", t14.x); + printf("%.15s\n", t15.x); + printf("%.16s\n", t16.x); + printf("%.17s\n", t17.x); + printf("%.1f\n", fr_hfa11().a); + printf("%.1f %.1f\n", fr_hfa12().a, fr_hfa12().b); + printf("%.1f %.1f\n", fr_hfa13().a, fr_hfa13().c); + printf("%.1f %.1f\n", fr_hfa14().a, fr_hfa14().d); + printf("%.1f\n", fr_hfa21().a); + printf("%.1f %.1f\n", fr_hfa22().a, fr_hfa22().b); + printf("%.1f %.1f\n", fr_hfa23().a, fr_hfa23().c); + printf("%.1f %.1f\n", fr_hfa24().a, fr_hfa24().d); + printf("%.1Lf\n", fr_hfa31().a); + printf("%.1Lf %.1Lf\n", fr_hfa32().a, fr_hfa32().b); + printf("%.1Lf %.1Lf\n", fr_hfa33().a, fr_hfa33().c); + printf("%.1Lf %.1Lf\n", fr_hfa34().a, fr_hfa34().d); +} + +int match(const char **s, const char *f) +{ + const char *p = *s; + for (p = *s; *f && *f == *p; f++, p++) + ; + if (!*f) { + *s = p - 1; + return 1; + } + return 0; +} + +void myprintf(const char *format, ...) +{ + const char *s; + va_list ap; + va_start(ap, format); + for (s = format; *s; s++) { + if (match(&s, "%7s")) { + struct s7 t7 = va_arg(ap, struct s7); + printf("%.7s", t7.x); + } + else if (match(&s, "%9s")) { + struct s9 t9 = va_arg(ap, struct s9); + printf("%.9s", t9.x); + } + else if (match(&s, "%hfa11")) { + struct hfa11 x = va_arg(ap, struct hfa11); + printf("%.1f,%.1f", x.a, x.a); + } + else if (match(&s, "%hfa12")) { + struct hfa12 x = va_arg(ap, struct hfa12); + printf("%.1f,%.1f", x.a, x.b); + } + else if (match(&s, "%hfa13")) { + struct hfa13 x = va_arg(ap, struct hfa13); + printf("%.1f,%.1f", x.a, x.c); + } + else if (match(&s, "%hfa14")) { + struct hfa14 x = va_arg(ap, struct hfa14); + printf("%.1f,%.1f", x.a, x.d); + } + else if (match(&s, "%hfa21")) { + struct hfa21 x = va_arg(ap, struct hfa21); + printf("%.1f,%.1f", x.a, x.a); + } + else if (match(&s, "%hfa22")) { + struct hfa22 x = va_arg(ap, struct hfa22); + printf("%.1f,%.1f", x.a, x.b); + } + else if (match(&s, "%hfa23")) { + struct hfa23 x = va_arg(ap, struct hfa23); + printf("%.1f,%.1f", x.a, x.c); + } + else if (match(&s, "%hfa24")) { + struct hfa24 x = va_arg(ap, struct hfa24); + printf("%.1f,%.1f", x.a, x.d); + } + else if (match(&s, "%hfa31")) { + struct hfa31 x = va_arg(ap, struct hfa31); + printf("%.1Lf,%.1Lf", x.a, x.a); + } + else if (match(&s, "%hfa32")) { + struct hfa32 x = va_arg(ap, struct hfa32); + printf("%.1Lf,%.1Lf", x.a, x.b); + } + else if (match(&s, "%hfa33")) { + struct hfa33 x = va_arg(ap, struct hfa33); + printf("%.1Lf,%.1Lf", x.a, x.c); + } + else if (match(&s, "%hfa34")) { + struct hfa34 x = va_arg(ap, struct hfa34); + printf("%.1Lf,%.1Lf", x.a, x.d); + } + else + putchar(*s); + } + putchar('\n'); +} + +void stdarg(void) +{ + printf("stdarg:\n"); + myprintf("%9s %9s %9s %9s %9s %9s", s9, s9, s9, s9, s9, s9); + myprintf("%7s %9s %9s %9s %9s %9s", s7, s9, s9, s9, s9, s9); + + myprintf("HFA long double:"); + myprintf("%hfa34 %hfa34 %hfa34 %hfa34", hfa34, hfa34, hfa34, hfa34); + myprintf("%hfa33 %hfa34 %hfa34 %hfa34", hfa33, hfa34, hfa34, hfa34); + myprintf("%hfa32 %hfa34 %hfa34 %hfa34", hfa32, hfa34, hfa34, hfa34); + myprintf("%hfa31 %hfa34 %hfa34 %hfa34", hfa31, hfa34, hfa34, hfa34); + + myprintf("%hfa32 %hfa33 %hfa33 %hfa33 %hfa33", + hfa32, hfa33, hfa33, hfa33, hfa33); + myprintf("%hfa31 %hfa33 %hfa33 %hfa33 %hfa33", + hfa31, hfa33, hfa33, hfa33, hfa33); + myprintf("%hfa33 %hfa33 %hfa33 %hfa33", + hfa33, hfa33, hfa33, hfa33); + + myprintf("%hfa34 %hfa32 %hfa32 %hfa32 %hfa32", + hfa34, hfa32, hfa32, hfa32, hfa32); + myprintf("%hfa33 %hfa32 %hfa32 %hfa32 %hfa32", + hfa33, hfa32, hfa32, hfa32, hfa32); + + myprintf("%hfa34 %hfa32 %hfa31 %hfa31 %hfa31 %hfa31", + hfa34, hfa32, hfa31, hfa31, hfa31, hfa31); + + myprintf("HFA double:"); + myprintf("%hfa24 %hfa24 %hfa24 %hfa24", hfa24, hfa24, hfa24, hfa24); + myprintf("%hfa23 %hfa24 %hfa24 %hfa24", hfa23, hfa24, hfa24, hfa24); + myprintf("%hfa22 %hfa24 %hfa24 %hfa24", hfa22, hfa24, hfa24, hfa24); + myprintf("%hfa21 %hfa24 %hfa24 %hfa24", hfa21, hfa24, hfa24, hfa24); + + myprintf("%hfa22 %hfa23 %hfa23 %hfa23 %hfa23", + hfa22, hfa23, hfa23, hfa23, hfa23); + myprintf("%hfa21 %hfa23 %hfa23 %hfa23 %hfa23", + hfa21, hfa23, hfa23, hfa23, hfa23); + myprintf("%hfa23 %hfa23 %hfa23 %hfa23", + hfa23, hfa23, hfa23, hfa23); + + myprintf("%hfa24 %hfa22 %hfa22 %hfa22 %hfa22", + hfa24, hfa22, hfa22, hfa22, hfa22); + myprintf("%hfa23 %hfa22 %hfa22 %hfa22 %hfa22", + hfa23, hfa22, hfa22, hfa22, hfa22); + + myprintf("%hfa24 %hfa22 %hfa21 %hfa21 %hfa21 %hfa21", + hfa24, hfa22, hfa21, hfa21, hfa21, hfa21); + + myprintf("HFA float:"); + myprintf("%hfa14 %hfa14 %hfa14 %hfa14", hfa14, hfa14, hfa14, hfa14); + myprintf("%hfa13 %hfa14 %hfa14 %hfa14", hfa13, hfa14, hfa14, hfa14); + myprintf("%hfa12 %hfa14 %hfa14 %hfa14", hfa12, hfa14, hfa14, hfa14); + myprintf("%hfa11 %hfa14 %hfa14 %hfa14", hfa11, hfa14, hfa14, hfa14); + + myprintf("%hfa12 %hfa13 %hfa13 %hfa13 %hfa13", + hfa12, hfa13, hfa13, hfa13, hfa13); + myprintf("%hfa11 %hfa13 %hfa13 %hfa13 %hfa13", + hfa11, hfa13, hfa13, hfa13, hfa13); + myprintf("%hfa13 %hfa13 %hfa13 %hfa13", + hfa13, hfa13, hfa13, hfa13); + + myprintf("%hfa14 %hfa12 %hfa12 %hfa12 %hfa12", + hfa14, hfa12, hfa12, hfa12, hfa12); + myprintf("%hfa13 %hfa12 %hfa12 %hfa12 %hfa12", + hfa13, hfa12, hfa12, hfa12, hfa12); + + myprintf("%hfa14 %hfa12 %hfa11 %hfa11 %hfa11 %hfa11", + hfa14, hfa12, hfa11, hfa11, hfa11, hfa11); +} + +void pll(unsigned long long x) +{ + printf("%llx\n", x); +} + +void movi(void) +{ + printf("MOVI:\n"); + pll(0); + pll(0xabcd); + pll(0xabcd0000); + pll(0xabcd00000000); + pll(0xabcd000000000000); + pll(0xffffabcd); + pll(0xabcdffff); + pll(0xffffffffffffabcd); + pll(0xffffffffabcdffff); + pll(0xffffabcdffffffff); + pll(0xabcdffffffffffff); + pll(0xaaaaaaaa); + pll(0x5555555555555555); + pll(0x77777777); + pll(0x3333333333333333); + pll(0xf8f8f8f8); + pll(0x1e1e1e1e1e1e1e1e); + pll(0x3f803f80); + pll(0x01ff01ff01ff01ff); + pll(0x007fffc0); + pll(0x03fff80003fff800); + pll(0x0007fffffffffe00); + + pll(0xabcd1234); + pll(0xabcd00001234); + pll(0xabcd000000001234); + pll(0xabcd12340000); + pll(0xabcd000012340000); + pll(0xabcd123400000000); + pll(0xffffffffabcd1234); + pll(0xffffabcdffff1234); + pll(0xabcdffffffff1234); + pll(0xffffabcd1234ffff); + pll(0xabcdffff1234ffff); + pll(0xabcd1234ffffffff); + + pll(0xffffef0123456789); + pll(0xabcdef012345ffff); + + pll(0xabcdef0123456789); +} + +static uint32_t addip0(uint32_t x) { return x + 0; } +static uint64_t sublp0(uint64_t x) { return x - 0; } +static uint32_t addip123(uint32_t x) { return x + 123; } +static uint64_t addlm123(uint64_t x) { return x + -123; } +static uint64_t sublp4095(uint64_t x) { return x - 4095; } +static uint32_t subim503808(uint32_t x) { return x - -503808; } +static uint64_t addp12345(uint64_t x) { return x + 12345; } +static uint32_t subp12345(uint32_t x) { return x - 12345; } + +static uint32_t mvni(uint32_t x) { return 0xffffffff - x; } +static uint64_t negl(uint64_t x) { return 0 - x; } +static uint32_t rsbi123(uint32_t x) { return 123 - x; } +static uint64_t rsbl123(uint64_t x) { return 123 - x; } + +static uint32_t andi0(uint32_t x) { return x & 0; } +static uint64_t andlm1(uint64_t x) { return x & -1; } +static uint64_t orrl0(uint64_t x) { return x | 0; } +static uint32_t orrim1(uint32_t x) { return x | -1; } +static uint32_t eori0(uint32_t x) { return x ^ 0; } +static uint64_t eorlm1(uint64_t x) { return x ^ -1; } +static uint32_t and0xf0(uint32_t x) { return x & 0xf0; } +static uint64_t orr0xf0(uint64_t x) { return x | 0xf0; } +static uint64_t eor0xf0(uint64_t x) { return x ^ 0xf0; } + +static uint32_t lsli0(uint32_t x) { return x << 0; } +static uint32_t lsri0(uint32_t x) { return x >> 0; } +static int64_t asrl0(int64_t x) { return x >> 0; } +static uint32_t lsli1(uint32_t x) { return x << 1; } +static uint32_t lsli31(uint32_t x) { return x << 31; } +static uint64_t lsll1(uint64_t x) { return x << 1; } +static uint64_t lsll63(uint64_t x) { return x << 63; } +static uint32_t lsri1(uint32_t x) { return x >> 1; } +static uint32_t lsri31(uint32_t x) { return x >> 31; } +static uint64_t lsrl1(uint64_t x) { return x >> 1; } +static uint64_t lsrl63(uint64_t x) { return x >> 63; } +static int32_t asri1(int32_t x) { return x >> 1; } +static int32_t asri31(int32_t x) { return x >> 31; } +static int64_t asrl1(int64_t x) { return x >> 1; } +static int64_t asrl63(int64_t x) { return x >> 63; } + +void opi(void) +{ + int x = 1000; + pll(addip0(x)); + pll(sublp0(x)); + pll(addip123(x)); + pll(addlm123(x)); + pll(sublp4095(x)); + pll(subim503808(x)); + pll(addp12345(x)); + pll(subp12345(x)); + pll(mvni(x)); + pll(negl(x)); + pll(rsbi123(x)); + pll(rsbl123(x)); + pll(andi0(x)); + pll(andlm1(x)); + pll(orrl0(x)); + pll(orrim1(x)); + pll(eori0(x)); + pll(eorlm1(x)); + pll(and0xf0(x)); + pll(orr0xf0(x)); + pll(eor0xf0(x)); + pll(lsli0(x)); + pll(lsri0(x)); + pll(asrl0(x)); + pll(lsli1(x)); + pll(lsli31(x)); + pll(lsll1(x)); + pll(lsll63(x)); + pll(lsri1(x)); + pll(lsri31(x)); + pll(lsrl1(x)); + pll(lsrl63(x)); + pll(asri1(x)); + pll(asri31(x)); + pll(asrl1(x)); + pll(asrl63(x)); +} + +void pcs(void) +{ + arg(); + ret(); + stdarg(); + movi(); + opi(); +} + +int main() +{ + pcs(); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/73_arm64.expect b/05/tcc-final-old/tests/tests2/73_arm64.expect new file mode 100644 index 0000000..7bdebd3 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/73_arm64.expect @@ -0,0 +1,174 @@ +Arguments: +0 +12 +345 +6789 +abcde +fghijk +lmnopqr +stuvwxyz +ABCDEFGHI +JKLMNOPQRS +TUVWXYZ0123 +456789abcdef +ghijklmnopqrs +tuvwxyzABCDEFG +HIJKLMNOPQRSTUV +WXYZ0123456789ab +cdefghijklmnopqrs +11.1 +12.1 12.1 +13.1 13.2 13.3 +14.1 14.2 14.3 14.4 +21.1 +22.1 22.1 +23.1 23.2 23.3 +24.1 24.2 24.3 24.4 +31.1 +32.1 32.1 +33.1 33.2 33.3 +34.1 34.2 34.3 34.4 +stu ABC JKL TUV 456 ghi +ABC JKL TUV 456 ghi tuv +14.1 14.4 23.1 23.3 32.1 32.2 +0 14.1 14.4 12 24.1 24.4 345 34.1 34.4 +Return values: +0 +12 +345 +6789 +abcde +fghijk +lmnopqr +stuvwxyz +ABCDEFGHI +JKLMNOPQRS +TUVWXYZ0123 +456789abcdef +ghijklmnopqrs +tuvwxyzABCDEFG +HIJKLMNOPQRSTUV +WXYZ0123456789ab +cdefghijklmnopqrs +11.1 +12.1 12.2 +13.1 13.3 +14.1 14.4 +21.1 +22.1 22.2 +23.1 23.3 +24.1 24.4 +31.1 +32.1 32.2 +33.1 33.3 +34.1 34.4 +stdarg: +ABCDEFGHI ABCDEFGHI ABCDEFGHI ABCDEFGHI ABCDEFGHI ABCDEFGHI +lmnopqr ABCDEFGHI ABCDEFGHI ABCDEFGHI ABCDEFGHI ABCDEFGHI +HFA long double: +34.1,34.4 34.1,34.4 34.1,34.4 34.1,34.4 +33.1,33.3 34.1,34.4 34.1,34.4 34.1,34.4 +32.1,32.2 34.1,34.4 34.1,34.4 34.1,34.4 +31.1,31.1 34.1,34.4 34.1,34.4 34.1,34.4 +32.1,32.2 33.1,33.3 33.1,33.3 33.1,33.3 33.1,33.3 +31.1,31.1 33.1,33.3 33.1,33.3 33.1,33.3 33.1,33.3 +33.1,33.3 33.1,33.3 33.1,33.3 33.1,33.3 +34.1,34.4 32.1,32.2 32.1,32.2 32.1,32.2 32.1,32.2 +33.1,33.3 32.1,32.2 32.1,32.2 32.1,32.2 32.1,32.2 +34.1,34.4 32.1,32.2 31.1,31.1 31.1,31.1 31.1,31.1 31.1,31.1 +HFA double: +24.1,24.4 24.1,24.4 24.1,24.4 24.1,24.4 +23.1,23.3 24.1,24.4 24.1,24.4 24.1,24.4 +22.1,22.2 24.1,24.4 24.1,24.4 24.1,24.4 +21.1,21.1 24.1,24.4 24.1,24.4 24.1,24.4 +22.1,22.2 23.1,23.3 23.1,23.3 23.1,23.3 23.1,23.3 +21.1,21.1 23.1,23.3 23.1,23.3 23.1,23.3 23.1,23.3 +23.1,23.3 23.1,23.3 23.1,23.3 23.1,23.3 +24.1,24.4 22.1,22.2 22.1,22.2 22.1,22.2 22.1,22.2 +23.1,23.3 22.1,22.2 22.1,22.2 22.1,22.2 22.1,22.2 +24.1,24.4 22.1,22.2 21.1,21.1 21.1,21.1 21.1,21.1 21.1,21.1 +HFA float: +14.1,14.4 14.1,14.4 14.1,14.4 14.1,14.4 +13.1,13.3 14.1,14.4 14.1,14.4 14.1,14.4 +12.1,12.2 14.1,14.4 14.1,14.4 14.1,14.4 +11.1,11.1 14.1,14.4 14.1,14.4 14.1,14.4 +12.1,12.2 13.1,13.3 13.1,13.3 13.1,13.3 13.1,13.3 +11.1,11.1 13.1,13.3 13.1,13.3 13.1,13.3 13.1,13.3 +13.1,13.3 13.1,13.3 13.1,13.3 13.1,13.3 +14.1,14.4 12.1,12.2 12.1,12.2 12.1,12.2 12.1,12.2 +13.1,13.3 12.1,12.2 12.1,12.2 12.1,12.2 12.1,12.2 +14.1,14.4 12.1,12.2 11.1,11.1 11.1,11.1 11.1,11.1 11.1,11.1 +MOVI: +0 +abcd +abcd0000 +abcd00000000 +abcd000000000000 +ffffabcd +abcdffff +ffffffffffffabcd +ffffffffabcdffff +ffffabcdffffffff +abcdffffffffffff +aaaaaaaa +5555555555555555 +77777777 +3333333333333333 +f8f8f8f8 +1e1e1e1e1e1e1e1e +3f803f80 +1ff01ff01ff01ff +7fffc0 +3fff80003fff800 +7fffffffffe00 +abcd1234 +abcd00001234 +abcd000000001234 +abcd12340000 +abcd000012340000 +abcd123400000000 +ffffffffabcd1234 +ffffabcdffff1234 +abcdffffffff1234 +ffffabcd1234ffff +abcdffff1234ffff +abcd1234ffffffff +ffffef0123456789 +abcdef012345ffff +abcdef0123456789 +3e8 +3e8 +463 +36d +fffffffffffff3e9 +7b3e8 +3421 +ffffd3af +fffffc17 +fffffffffffffc18 +fffffc93 +fffffffffffffc93 +0 +3e8 +3e8 +ffffffff +3e8 +fffffffffffffc17 +e0 +3f8 +318 +3e8 +3e8 +3e8 +7d0 +0 +7d0 +0 +1f4 +0 +1f4 +0 +1f4 +0 +1f4 +0 diff --git a/05/tcc-final-old/tests/tests2/75_array_in_struct_init.c b/05/tcc-final-old/tests/tests2/75_array_in_struct_init.c new file mode 100644 index 0000000..234e3c4 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/75_array_in_struct_init.c @@ -0,0 +1,33 @@ +#include + +/* This test is a snippet from the J interpreter */ + +typedef long I; +typedef struct{I c[4];I b,e,k;} PT; + +PT cases[] = { + ((I)4194304L +(I)2097152L +(I)67108864L), (I)262144L, (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), -1L, 1,2,1, + ((I)+4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L, (I)262144L, (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), 2,3,2, + ((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)262144L, (((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), 1,3,2, + ((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)524288L, -1L, 1,2,1, + ((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)1048576L, (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), 1,3,1, + ((I)4194304L +(I)2097152L +(I)67108864L)+( (I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)262144L, (I)262144L, 1,3,1, + ((I)4194304L +(I)2097152L +(I)67108864L), ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), -1L, 1,2,1, + (I)33554432L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L), (I)2097152L, ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), -1L, 0,2,1, + (I)67108864L, ((I)1048576L +(I)524288L +(I)262144L +(((I)1L +(I)256L +(I)4L +(I)8L +(I)16L +(I)64L +(I)128L +(I)268435456L +(I)536870912L +(I)1024L +(I)4096L +(I)8192L +(I)16384L)+((I)2L +(I)131072L +(I)2048L)+(I)32L +(I)32768L +(I)65536L)), (I)134217728L, -1L, 0,2,0, +}; + +int main() { + int i, j; + + for(j=0; j < sizeof(cases)/sizeof(cases[0]); j++) { + for(i=0; i < sizeof(cases->c)/sizeof(cases->c[0]); i++) + printf("cases[%d].c[%d]=%ld\n", j, i, cases[j].c[i]); + + printf("cases[%d].b=%ld\n", j, cases[j].b); + printf("cases[%d].e=%ld\n", j, cases[j].e); + printf("cases[%d].k=%ld\n", j, cases[j].k); + printf("\n"); + } + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/75_array_in_struct_init.expect b/05/tcc-final-old/tests/tests2/75_array_in_struct_init.expect new file mode 100644 index 0000000..2b75aa5 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/75_array_in_struct_init.expect @@ -0,0 +1,72 @@ +cases[0].c[0]=73400320 +cases[0].c[1]=262144 +cases[0].c[2]=805567999 +cases[0].c[3]=-1 +cases[0].b=1 +cases[0].e=2 +cases[0].k=1 + +cases[1].c[0]=879754751 +cases[1].c[1]=262144 +cases[1].c[2]=262144 +cases[1].c[3]=805567999 +cases[1].b=2 +cases[1].e=3 +cases[1].k=2 + +cases[2].c[0]=879754751 +cases[2].c[1]=805567999 +cases[2].c[2]=262144 +cases[2].c[3]=805567999 +cases[2].b=1 +cases[2].e=3 +cases[2].k=2 + +cases[3].c[0]=879754751 +cases[3].c[1]=805830143 +cases[3].c[2]=524288 +cases[3].c[3]=-1 +cases[3].b=1 +cases[3].e=2 +cases[3].k=1 + +cases[4].c[0]=879754751 +cases[4].c[1]=805830143 +cases[4].c[2]=1048576 +cases[4].c[3]=805830143 +cases[4].b=1 +cases[4].e=3 +cases[4].k=1 + +cases[5].c[0]=879754751 +cases[5].c[1]=805830143 +cases[5].c[2]=262144 +cases[5].c[3]=262144 +cases[5].b=1 +cases[5].e=3 +cases[5].k=1 + +cases[6].c[0]=73400320 +cases[6].c[1]=807403007 +cases[6].c[2]=807403007 +cases[6].c[3]=-1 +cases[6].b=1 +cases[6].e=2 +cases[6].k=1 + +cases[7].c[0]=839122431 +cases[7].c[1]=2097152 +cases[7].c[2]=807403007 +cases[7].c[3]=-1 +cases[7].b=0 +cases[7].e=2 +cases[7].k=1 + +cases[8].c[0]=67108864 +cases[8].c[1]=807403007 +cases[8].c[2]=134217728 +cases[8].c[3]=-1 +cases[8].b=0 +cases[8].e=2 +cases[8].k=0 + diff --git a/05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.c b/05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.c new file mode 100644 index 0000000..c5fcf99 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.c @@ -0,0 +1,41 @@ +#include + +#define $(x) x +#define $fred 10 +#define joe$ 20 +#define hen$y 30 + +#define $10(x) x*10 +#define _$10(x) x/10 + +int main() +{ + printf("fred=%d\n", $fred); + printf("joe=%d\n", joe$); + printf("henry=%d\n", hen$y); + + printf("fred2=%d\n", $($fred)); + printf("joe2=%d\n", $(joe$)); + printf("henry2=%d\n", $(hen$y)); + + printf("fred10=%d\n", $10($fred)); + printf("joe_10=%d\n", _$10(joe$)); + + int $ = 10; + int a100$ = 100; + int a$$ = 1000; + int a$c$b = 2121; + int $100 = 10000; + const char *$$$ = "money"; + + printf("local=%d\n", $); + printf("a100$=%d\n", a100$); + printf("a$$=%d\n", a$$); + printf("a$c$b=%d\n", a$c$b); + printf("$100=%d\n", $100); + printf("$$$=%s", $$$); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.expect b/05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.expect new file mode 100644 index 0000000..4a20a52 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/76_dollars_in_identifiers.expect @@ -0,0 +1,14 @@ +fred=10 +joe=20 +henry=30 +fred2=10 +joe2=20 +henry2=30 +fred10=100 +joe_10=2 +local=10 +a100$=100 +a$$=1000 +a$c$b=2121 +$100=10000 +$$$=money diff --git a/05/tcc-final-old/tests/tests2/77_push_pop_macro.c b/05/tcc-final-old/tests/tests2/77_push_pop_macro.c new file mode 100644 index 0000000..d38e0bf --- /dev/null +++ b/05/tcc-final-old/tests/tests2/77_push_pop_macro.c @@ -0,0 +1,30 @@ +#include + +int main() +{ + /* must not affect how #pragma ppop_macro works */ + #define pop_macro foobar1 + + /* must not affect how #pragma push_macro works */ + #define push_macro foobar2 + + #undef abort + #define abort "111" + printf("abort = %s\n", abort); + + #pragma push_macro("abort") + #undef abort + #define abort "222" + printf("abort = %s\n", abort); + + #pragma push_macro("abort") + #undef abort + #define abort "333" + printf("abort = %s\n", abort); + + #pragma pop_macro("abort") + printf("abort = %s\n", abort); + + #pragma pop_macro("abort") + printf("abort = %s\n", abort); +} diff --git a/05/tcc-final-old/tests/tests2/77_push_pop_macro.expect b/05/tcc-final-old/tests/tests2/77_push_pop_macro.expect new file mode 100644 index 0000000..d8a5530 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/77_push_pop_macro.expect @@ -0,0 +1,5 @@ +abort = 111 +abort = 222 +abort = 333 +abort = 222 +abort = 111 diff --git a/05/tcc-final-old/tests/tests2/78_vla_label.c b/05/tcc-final-old/tests/tests2/78_vla_label.c new file mode 100644 index 0000000..4096495 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/78_vla_label.c @@ -0,0 +1,45 @@ +#include + +/* This test segfaults as of April 27, 2015. */ +void f1(int argc) +{ + char test[argc]; + if(0) + label: + printf("boom!\n"); + if(argc-- == 0) + return; + goto label; +} + +/* This segfaulted on 2015-11-19. */ +void f2(void) +{ + goto start; + { + int a[1 && 1]; /* not a variable-length array */ + int b[1 || 1]; /* not a variable-length array */ + int c[1 ? 1 : 1]; /* not a variable-length array */ + start: + a[0] = 0; + b[0] = 0; + c[0] = 0; + } +} + +void f3(void) +{ + printf("%d\n", 0 ? printf("x1\n") : 11); + printf("%d\n", 1 ? 12 : printf("x2\n")); + printf("%d\n", 0 && printf("x3\n")); + printf("%d\n", 1 || printf("x4\n")); +} + +int main() +{ + f1(2); + f2(); + f3(); + + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/78_vla_label.expect b/05/tcc-final-old/tests/tests2/78_vla_label.expect new file mode 100644 index 0000000..3f4063b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/78_vla_label.expect @@ -0,0 +1,6 @@ +boom! +boom! +11 +12 +0 +1 diff --git a/05/tcc-final-old/tests/tests2/79_vla_continue.c b/05/tcc-final-old/tests/tests2/79_vla_continue.c new file mode 100644 index 0000000..91215c9 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/79_vla_continue.c @@ -0,0 +1,116 @@ +#include + +int f(void) +{ + return 5; +} + +void test1() +{ + int count = 10; + void *addr[10]; + for(;count--;) { + int a[f()]; + + addr[count] = a; + + continue; + } + + if(addr[9] == addr[0]) { + printf("OK\n"); + } else { + printf("NOT OK\n"); + } +} + +void test2() +{ + int count = 10; + void *addr[count]; + for(;count--;) { + int a[f()]; + + addr[count] = a; + + continue; + } + + if(addr[9] == addr[0]) { + printf("OK\n"); + } else { + printf("NOT OK\n"); + } +} + +void test3() +{ + int count = 10; + void *addr[count]; + while(count--) { + int a[f()]; + + addr[count] = a; + + continue; + } + + if(addr[9] == addr[0]) { + printf("OK\n"); + } else { + printf("NOT OK\n"); + } +} + +void test4() +{ + int count = 10; + void *addr[count]; + do { + int a[f()]; + + addr[--count] = a; + + continue; + } while (count); + + if(addr[9] == addr[0]) { + printf("OK\n"); + } else { + printf("NOT OK\n"); + } +} + +void test5() +{ + int count = 10; + int a[f()]; + int c[f()]; + + c[0] = 42; + + for(;count--;) { + int b[f()]; + int i; + for (i=0; i +struct wchar { + char *data; char mem[]; +}; +struct wint { + char *data; int mem[]; +}; +int f1char (void) { + char s[9]="nonono"; + struct wchar q = {"bugs"}; + return !s[0]; +} +int f1int (void) { + char s[9]="nonono"; + struct wint q = {"bugs"}; + return !s[0]; +} +int main (void) { + char s[9]="nonono"; + static struct wchar q = {"bugs", {'c'}}; + //printf ("tcc has %s %s\n", s, q.data); + if (f1char() || f1int()) + printf ("bla\n"); + return !s[0]; +} diff --git a/05/tcc-final-old/tests/tests2/80_flexarray.expect b/05/tcc-final-old/tests/tests2/80_flexarray.expect new file mode 100644 index 0000000..e69de29 diff --git a/05/tcc-final-old/tests/tests2/81_types.c b/05/tcc-final-old/tests/tests2/81_types.c new file mode 100644 index 0000000..fd6d71b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/81_types.c @@ -0,0 +1,43 @@ +/* The following are all valid decls, even though some subtypes + are incomplete. */ +enum E *e; +const enum E *e1; +enum E const *e2; +struct S *s; +const struct S *s1; +struct S const *s2; + +/* Various strangely looking declarators, which are all valid + and have to map to the same numbered typedefs. */ +typedef int (*fptr1)(); +int f1 (int (), int); +typedef int (*fptr2)(int x); +int f2 (int (int x), int); +typedef int (*fptr3)(int); +int f3 (int (int), int); +typedef int (*fptr4[4])(int); +int f4 (int (*[4])(int), int); +typedef int (*fptr5)(fptr1); +int f5 (int (int()), fptr1); +int f1 (fptr1 fp, int i) +{ + return (*fp)(i); +} +int f2 (fptr2 fp, int i) +{ + return (*fp)(i); +} +int f3 (fptr3 fp, int i) +{ + return (*fp)(i); +} +int f4 (fptr4 fp, int i) +{ + return (*fp[i])(i); +} +int f5 (fptr5 fp, fptr1 i) +{ + return fp(i); +} +int f8 (int ([4]), int); +int main () { return 0; } diff --git a/05/tcc-final-old/tests/tests2/81_types.expect b/05/tcc-final-old/tests/tests2/81_types.expect new file mode 100644 index 0000000..e69de29 diff --git a/05/tcc-final-old/tests/tests2/82_attribs_position.c b/05/tcc-final-old/tests/tests2/82_attribs_position.c new file mode 100644 index 0000000..7c9f987 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/82_attribs_position.c @@ -0,0 +1,19 @@ +typedef unsigned short uint16_t; +typedef unsigned char uint8_t; + +typedef union Unaligned16a { + uint16_t u; + uint8_t b[2]; +} __attribute__((packed)) Unaligned16a; + +typedef union __attribute__((packed)) Unaligned16b { + uint16_t u; + uint8_t b[2]; +} Unaligned16b; + +extern void foo (void) __attribute__((stdcall)); +void __attribute__((stdcall)) foo (void) +{ +} + +int main () { return 0; } diff --git a/05/tcc-final-old/tests/tests2/82_attribs_position.expect b/05/tcc-final-old/tests/tests2/82_attribs_position.expect new file mode 100644 index 0000000..e69de29 diff --git a/05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.c b/05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.c new file mode 100644 index 0000000..1f86095 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.c @@ -0,0 +1,9 @@ +#include +double привет=0.1; +int Lefèvre=2; +int main(){ + printf("привет=%g\n",привет); + printf("Lefèvre=%d\n",Lefèvre); + return 0; +} +// pcc & tcc only diff --git a/05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.expect b/05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.expect new file mode 100644 index 0000000..1553f5f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/83_utf8_in_identifiers.expect @@ -0,0 +1,2 @@ +привет=0.1 +Lefèvre=2 diff --git a/05/tcc-final-old/tests/tests2/84_hex-float.c b/05/tcc-final-old/tests/tests2/84_hex-float.c new file mode 100644 index 0000000..0ef09bf --- /dev/null +++ b/05/tcc-final-old/tests/tests2/84_hex-float.c @@ -0,0 +1,12 @@ +extern int printf(const char *format, ...); + +#define ACPI_TYPE_INVALID 0x1E +#define NUM_NS_TYPES ACPI_TYPE_INVALID+1 +int array[NUM_NS_TYPES]; + +#define n 0xe +int main() +{ + printf("n+1 = %d\n", n+1); +// printf("n+1 = %d\n", 0xe+1); +} diff --git a/05/tcc-final-old/tests/tests2/84_hex-float.expect b/05/tcc-final-old/tests/tests2/84_hex-float.expect new file mode 100644 index 0000000..2175385 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/84_hex-float.expect @@ -0,0 +1 @@ +n+1 = 15 diff --git a/05/tcc-final-old/tests/tests2/85_asm-outside-function.c b/05/tcc-final-old/tests/tests2/85_asm-outside-function.c new file mode 100644 index 0000000..dc5639a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/85_asm-outside-function.c @@ -0,0 +1,9 @@ +extern int printf (const char *, ...); +extern void vide(void); +__asm__("vide: ret"); + +int main() { + vide(); + printf ("okay\n"); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/85_asm-outside-function.expect b/05/tcc-final-old/tests/tests2/85_asm-outside-function.expect new file mode 100644 index 0000000..dcf02b2 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/85_asm-outside-function.expect @@ -0,0 +1 @@ +okay diff --git a/05/tcc-final-old/tests/tests2/86_memory-model.c b/05/tcc-final-old/tests/tests2/86_memory-model.c new file mode 100644 index 0000000..744c3e2 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/86_memory-model.c @@ -0,0 +1,38 @@ +#include + +int +main() +{ +#if defined(__LLP64__) + if (sizeof(short) == 2 + && sizeof(int) == 4 + && sizeof(long int) == 4 + && sizeof(long long int) == 8 + && sizeof(void*) == 8) { + (void)printf("Ok\n"); + } else { + (void)printf("KO __LLP64__\n"); + } +#elif defined(__LP64__) + if (sizeof(short) == 2 + && sizeof(int) == 4 + && sizeof(long int) == 8 + && sizeof(long long int) == 8 + && sizeof(void*) == 8) { + (void)printf("Ok\n"); + } else { + (void)printf("KO __LP64__\n"); + } +#elif defined(__ILP32__) + if (sizeof(short) == 2 + && sizeof(int) == 4 + && sizeof(long int) == 4 + && sizeof(void*) == 4) { + (void)printf("Ok\n"); + } else { + (void)printf("KO __ILP32__\n"); + } +#else + (void)printf("KO no __*LP*__ defined.\n"); +#endif +} diff --git a/05/tcc-final-old/tests/tests2/86_memory-model.expect b/05/tcc-final-old/tests/tests2/86_memory-model.expect new file mode 100644 index 0000000..7326d96 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/86_memory-model.expect @@ -0,0 +1 @@ +Ok diff --git a/05/tcc-final-old/tests/tests2/87_dead_code.c b/05/tcc-final-old/tests/tests2/87_dead_code.c new file mode 100644 index 0000000..98d4566 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/87_dead_code.c @@ -0,0 +1,122 @@ +/* This checks various ways of dead code inside if statements + where there are non-obvious ways of how the code is actually + not dead due to reachable by labels. */ +extern int printf (const char *, ...); +static void kb_wait_1(void) +{ + unsigned long timeout = 2; + do { + /* Here the else arm is a statement expression that's supposed + to be suppressed. The label inside the while would unsuppress + code generation again if not handled correctly. And that + would wreak havoc to the cond-expression because there's no + jump-around emitted, the whole statement expression really + needs to not generate code (perhaps except useless forward jumps). */ + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + int i = 1; + while (1) + while (i--) + some_label: + printf("error\n"); + goto some_label; + }) + ); + timeout--; + } while (timeout); +} +int main (void) +{ + int i = 1; + kb_wait_1(); + + /* Simple test of dead code at first sight which isn't actually dead. */ + if (0) { +yeah: + printf ("yeah\n"); + } else { + printf ("boo\n"); + } + if (i--) + goto yeah; + + /* Some more non-obvious uses where the problems are loops, so that even + the first loop statements aren't actually dead. */ + i = 1; + if (0) { + while (i--) { + printf ("once\n"); +enterloop: + printf ("twice\n"); + } + } + if (i >= 0) + goto enterloop; + + /* The same with statement expressions. One might be tempted to + handle them specially by counting if inside statement exprs and + not unsuppressing code at loops at all then. + See kb_wait_1 for the other side of the medal where that wouldn't work. */ + i = ({ + int j = 1; + if (0) { + while (j--) { + printf ("SEonce\n"); + enterexprloop: + printf ("SEtwice\n"); + } + } + if (j >= 0) + goto enterexprloop; + j; }); + + /* The other two loop forms: */ + i = 1; + if (0) { + for (i = 1; i--;) { + printf ("once2\n"); +enterloop2: + printf ("twice2\n"); + } + } + if (i > 0) + goto enterloop2; + + i = 1; + if (0) { + do { + printf ("once3\n"); +enterloop3: + printf ("twice3\n"); + } while (i--); + } + if (i > 0) + goto enterloop3; + + /* And check that case and default labels have the same effect + of disabling code suppression. */ + i = 41; + switch (i) { + if (0) { + printf ("error\n"); + case 42: + printf ("error2\n"); + case 41: + printf ("caseok\n"); + } + } + + i = 41; + switch (i) { + if (0) { + printf ("error3\n"); + default: + printf ("caseok2\n"); + break; + case 42: + printf ("error4\n"); + } + } + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/87_dead_code.expect b/05/tcc-final-old/tests/tests2/87_dead_code.expect new file mode 100644 index 0000000..0b3ec1d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/87_dead_code.expect @@ -0,0 +1,18 @@ +timeout=2 +timeout=1 +boo +yeah +twice +once +twice +SEtwice +SEonce +SEtwice +twice2 +once2 +twice2 +twice3 +once3 +twice3 +caseok +caseok2 diff --git a/05/tcc-final-old/tests/tests2/88_codeopt.c b/05/tcc-final-old/tests/tests2/88_codeopt.c new file mode 100644 index 0000000..647626f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/88_codeopt.c @@ -0,0 +1,68 @@ +/* Check some way in where code suppression caused various + miscompilations. */ +extern int printf (const char *, ...); +typedef unsigned long size_t; + +size_t _brk_start, _brk_end; +void * extend_brk(size_t size, size_t align) +{ + size_t mask = align - 1; + void *ret = 0; + + do { + if (__builtin_expect(!!(_brk_start == 0), 0)) + do { + printf("wrong1\n"); + } while (0); + } while (0); + _brk_end = (_brk_end + mask) & ~mask; + ret = (void *)_brk_end; + _brk_end += size; + + return ret; +} + +static void get_args (int a, int b) +{ + if (a != 1) + printf("wrong2\n"); + else + printf("okay\n"); +} + +void bla(void) +{ + int __ret = 42; + ({ + if (__builtin_expect(!!(0), 0)) { + if (__builtin_expect(!!__ret, 0)) + printf("wrong3\n"); + int x = !!(__ret); + } + __ret; + }); + get_args(!!__ret, sizeof(__ret)); +} + +_Bool chk(unsigned long addr, unsigned long limit, unsigned long size) +{ + _Bool ret; + /* This just needs to compile, no runtime test. (And it doesn't compile + only with certain internal checking added that's not committed). */ + if (0) + ret = 0 != (!!(addr > limit - size)); +} + +int main() +{ + void *r; + _brk_start = 1024; + _brk_end = 1024; + r = extend_brk (4096, 16); + if (!r) + printf("wrong4\n"); + else + printf("okay\n"); + bla(); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/88_codeopt.expect b/05/tcc-final-old/tests/tests2/88_codeopt.expect new file mode 100644 index 0000000..439edfd --- /dev/null +++ b/05/tcc-final-old/tests/tests2/88_codeopt.expect @@ -0,0 +1,2 @@ +okay +okay diff --git a/05/tcc-final-old/tests/tests2/89_nocode_wanted.c b/05/tcc-final-old/tests/tests2/89_nocode_wanted.c new file mode 100644 index 0000000..73e0a4b --- /dev/null +++ b/05/tcc-final-old/tests/tests2/89_nocode_wanted.c @@ -0,0 +1,112 @@ +extern int printf(const char *format, ...); +static void kb_wait_1(void) +{ + unsigned long timeout = 2; + do { + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + while (1) + printf("error\n"); + }) + ); + timeout--; + } while (timeout); +} +static void kb_wait_2(void) +{ + unsigned long timeout = 2; + do { + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + for (;;) + printf("error\n"); + }) + ); + timeout--; + } while (timeout); +} +static void kb_wait_2_1(void) +{ + unsigned long timeout = 2; + do { + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + do { + printf("error\n"); + } while (1); + }) + ); + timeout--; + } while (timeout); +} +static void kb_wait_2_2(void) +{ + unsigned long timeout = 2; + do { + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + label: + printf("error\n"); + goto label; + }) + ); + timeout--; + } while (timeout); +} +static void kb_wait_3(void) +{ + unsigned long timeout = 2; + do { + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + int i = 1; + goto label; + i = i + 2; + label: + i = i + 3; + }) + ); + timeout--; + } while (timeout); +} +static void kb_wait_4(void) +{ + unsigned long timeout = 2; + do { + (1 ? + printf("timeout=%ld\n", timeout) : + ({ + switch(timeout) { + case 2: + printf("timeout is 2"); + break; + case 1: + printf("timeout is 1"); + break; + default: + printf("timeout is 0?"); + break; + }; + // return; + }) + ); + timeout--; + } while (timeout); +} +int main() +{ + printf("begin\n"); + kb_wait_1(); + kb_wait_2(); + kb_wait_2_1(); + kb_wait_2_2(); + kb_wait_3(); + kb_wait_4(); + printf("end\n"); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/89_nocode_wanted.expect b/05/tcc-final-old/tests/tests2/89_nocode_wanted.expect new file mode 100644 index 0000000..c44d4ea --- /dev/null +++ b/05/tcc-final-old/tests/tests2/89_nocode_wanted.expect @@ -0,0 +1,14 @@ +begin +timeout=2 +timeout=1 +timeout=2 +timeout=1 +timeout=2 +timeout=1 +timeout=2 +timeout=1 +timeout=2 +timeout=1 +timeout=2 +timeout=1 +end diff --git a/05/tcc-final-old/tests/tests2/90_struct-init.c b/05/tcc-final-old/tests/tests2/90_struct-init.c new file mode 100644 index 0000000..d931e23 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/90_struct-init.c @@ -0,0 +1,282 @@ +typedef unsigned char u8; +typedef struct {} empty_s; +struct contains_empty { + u8 a; + empty_s empty; + u8 b; +}; +struct contains_empty ce = { { (1) }, (empty_s){}, 022, }; +/* The following decl of 'q' would demonstrate the TCC bug in init_putv when + handling copying compound literals. (Compound literals + aren't acceptable constant initializers in isoc99, but + we accept them like gcc, except for this case) +//char *q = (char *){ "trara" }; */ +struct SS {u8 a[3], b; }; +struct SS sinit16[] = { { 1 }, 2 }; +struct S +{ + u8 a,b; + u8 c[2]; +}; + +struct T +{ + u8 s[16]; + u8 a; +}; + +struct U +{ + u8 a; + struct S s; + u8 b; + struct T t; +}; + +struct V +{ + struct S s; + struct T t; + u8 a; +}; + +struct W +{ + struct V t; + struct S s[]; +}; + +struct S gs = ((struct S){1, 2, 3, 4}); +struct S gs2 = {1, 2, {3, 4}}; +struct T gt = {"hello", 42}; +struct U gu = {3, 5,6,7,8, 4, "huhu", 43}; +struct U gu2 = {3, {5,6,7,8}, 4, {"huhu", 43}}; +/* Optional braces around scalar initializers. Accepted, but with + a warning. */ +struct U gu3 = { {3}, {5,6,7,8,}, 4, {"huhu", 43}}; +/* Many superfluous braces and leaving out one initializer for U.s.c[1] */ +struct U gu4 = { 3, {5,6,7,}, 5, { "bla", {44}} }; +/* Superfluous braces and useless parens around values */ +struct S gs3 = { (1), {(2)}, {(((3))), {4}}}; +/* Superfluous braces, and leaving out braces for V.t, plus cast */ +struct V gv = {{{3},4,{5,6}}, "haha", (u8)45, 46}; +/* Compound literal */ +struct V gv2 = {(struct S){7,8,{9,10}}, {"hihi", 47}, 48}; +/* Parens around compound literal */ +struct V gv3 = {((struct S){7,8,{9,10}}), {"hoho", 49}, 50}; +/* Initialization of a flex array member (warns in GCC) */ +struct W gw = {{1,2,3,4}, {1,2,3,4,5}}; + +union UU { + u8 a; + u8 b; +}; +struct SU { + union UU u; + u8 c; +}; +struct SU gsu = {5,6}; + +/* Unnamed struct/union members aren't ISO C, but it's a widely accepted + extension. See below for further extensions to that under -fms-extension.*/ +union UV { + struct {u8 a,b;}; + struct S s; +}; +union UV guv = {{6,5}}; +union UV guv2 = {{.b = 7, .a = 8}}; +union UV guv3 = {.b = 8, .a = 7}; + +/* Under -fms-extensions also the following is valid: +union UV2 { + struct Anon {u8 a,b;}; // unnamed member, but tagged struct, ... + struct S s; +}; +struct Anon gan = { 10, 11 }; // ... which makes it available here. +union UV2 guv4 = {{4,3}}; // and the other inits from above as well +*/ + +struct in6_addr { + union { + u8 u6_addr8[16]; + unsigned short u6_addr16[8]; + } u; +}; +struct flowi6 { + struct in6_addr saddr, daddr; +}; +struct pkthdr { + struct in6_addr daddr, saddr; +}; +struct pkthdr phdr = { { { 6,5,4,3 } }, { { 9,8,7,6 } } }; + +struct Wrap { + void *func; +}; +int global; +void inc_global (void) +{ + global++; +} + +struct Wrap global_wrap[] = { + ((struct Wrap) {inc_global}), + inc_global, +}; + +#include +void print_ (const char *name, const u8 *p, long size) +{ + printf ("%s:", name); + while (size--) { + printf (" %x", *p++); + } + printf ("\n"); +} +#define print(x) print_(#x, (u8*)&x, sizeof (x)) +#if 1 +void foo (struct W *w, struct pkthdr *phdr_) +{ + struct S ls = {1, 2, 3, 4}; + struct S ls2 = {1, 2, {3, 4}}; + struct T lt = {"hello", 42}; + struct U lu = {3, 5,6,7,8, 4, "huhu", 43}; + struct U lu1 = {3, ls, 4, {"huhu", 43}}; + struct U lu2 = {3, (ls), 4, {"huhu", 43}}; + const struct S *pls = &ls; + struct S ls21 = *pls; + struct U lu22 = {3, *pls, 4, {"huhu", 43}}; + /* Incomplete bracing. */ + struct U lu21 = {3, ls, 4, "huhu", 43}; + /* Optional braces around scalar initializers. Accepted, but with + a warning. */ + struct U lu3 = { 3, {5,6,7,8,}, 4, {"huhu", 43}}; + /* Many superfluous braces and leaving out one initializer for U.s.c[1] */ + struct U lu4 = { 3, {5,6,7,}, 5, { "bla", 44} }; + /* Superfluous braces and useless parens around values */ + struct S ls3 = { (1), (2), {(((3))), 4}}; + /* Superfluous braces, and leaving out braces for V.t, plus cast */ + struct V lv = {{3,4,{5,6}}, "haha", (u8)45, 46}; + /* Compound literal */ + struct V lv2 = {(struct S)w->t.s, {"hihi", 47}, 48}; + /* Parens around compound literal */ + struct V lv3 = {((struct S){7,8,{9,10}}), ((const struct W *)w)->t.t, 50}; + const struct pkthdr *phdr = phdr_; + struct flowi6 flow = { .daddr = phdr->daddr, .saddr = phdr->saddr }; + int elt = 0x42; + /* Range init, overlapping */ + struct T lt2 = { { [1 ... 5] = 9, [6 ... 10] = elt, [4 ... 7] = elt+1 }, 1 }; + print(ls); + print(ls2); + print(lt); + print(lu); + print(lu1); + print(lu2); + print(ls21); + print(lu21); + print(lu22); + print(lu3); + print(lu4); + print(ls3); + print(lv); + print(lv2); + print(lv3); + print(lt2); + print(flow); +} +#endif + +void test_compound_with_relocs (void) +{ + struct Wrap local_wrap[] = { + ((struct Wrap) {inc_global}), + inc_global, + }; + void (*p)(void); + p = global_wrap[0].func; p(); + p = global_wrap[1].func; p(); + p = local_wrap[0].func; p(); + p = local_wrap[1].func; p(); +} + +void sys_ni(void) { printf("ni\n"); } +void sys_one(void) { printf("one\n"); } +void sys_two(void) { printf("two\n"); } +void sys_three(void) { printf("three\n"); } +typedef void (*fptr)(void); +const fptr table[3] = { + [0 ... 2] = &sys_ni, + [0] = sys_one, + [1] = sys_two, + [2] = sys_three, +}; + +void test_multi_relocs(void) +{ + int i; + for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) + table[i](); +} + +/* Following is from GCC gcc.c-torture/execute/20050613-1.c. */ + +struct SEA { int i; int j; int k; int l; }; +struct SEB { struct SEA a; int r[1]; }; +struct SEC { struct SEA a; int r[0]; }; +struct SED { struct SEA a; int r[]; }; + +static void +test_correct_filling (struct SEA *x) +{ + static int i; + if (x->i != 0 || x->j != 5 || x->k != 0 || x->l != 0) + printf("sea_fill%d: wrong\n", i); + else + printf("sea_fill%d: okay\n", i); + i++; +} + +int +test_zero_init (void) +{ + /* The peculiarity here is that only a.j is initialized. That + means that all other members must be zero initialized. TCC + once didn't do that for sub-level designators. */ + struct SEB b = { .a.j = 5 }; + struct SEC c = { .a.j = 5 }; + struct SED d = { .a.j = 5 }; + test_correct_filling (&b.a); + test_correct_filling (&c.a); + test_correct_filling (&d.a); + return 0; +} + +int main() +{ + print(ce); + print(gs); + print(gs2); + print(gt); + print(gu); + print(gu2); + print(gu3); + print(gu4); + print(gs3); + print(gv); + print(gv2); + print(gv3); + print(sinit16); + print(gw); + print(gsu); + print(guv); + print(guv.b); + print(guv2); + print(guv3); + print(phdr); + foo(&gw, &phdr); + //printf("q: %s\n", q); + test_compound_with_relocs(); + test_multi_relocs(); + test_zero_init(); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/90_struct-init.expect b/05/tcc-final-old/tests/tests2/90_struct-init.expect new file mode 100644 index 0000000..e366121 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/90_struct-init.expect @@ -0,0 +1,43 @@ +ce: 1 12 +gs: 1 2 3 4 +gs2: 1 2 3 4 +gt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a +gu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +gu2: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +gu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +gu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c +gs3: 1 2 3 4 +gv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e +gv2: 7 8 9 a 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30 +gv3: 7 8 9 a 68 6f 68 6f 0 0 0 0 0 0 0 0 0 0 0 0 31 32 +sinit16: 1 0 0 0 2 0 0 0 +gw: 1 2 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +gsu: 5 6 +guv: 6 5 0 0 +guv.b: 5 +guv2: 8 7 0 0 +guv3: 7 8 0 0 +phdr: 6 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0 9 8 7 6 0 0 0 0 0 0 0 0 0 0 0 0 +ls: 1 2 3 4 +ls2: 1 2 3 4 +lt: 68 65 6c 6c 6f 0 0 0 0 0 0 0 0 0 0 0 2a +lu: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +lu1: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +lu2: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +ls21: 1 2 3 4 +lu21: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +lu22: 3 1 2 3 4 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +lu3: 3 5 6 7 8 4 68 75 68 75 0 0 0 0 0 0 0 0 0 0 0 0 2b +lu4: 3 5 6 7 0 5 62 6c 61 0 0 0 0 0 0 0 0 0 0 0 0 0 2c +ls3: 1 2 3 4 +lv: 3 4 5 6 68 61 68 61 0 0 0 0 0 0 0 0 0 0 0 0 2d 2e +lv2: 1 2 3 4 68 69 68 69 0 0 0 0 0 0 0 0 0 0 0 0 2f 30 +lv3: 7 8 9 a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 +lt2: 0 9 9 9 43 43 43 43 42 42 42 0 0 0 0 0 1 +flow: 9 8 7 6 0 0 0 0 0 0 0 0 0 0 0 0 6 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0 +one +two +three +sea_fill0: okay +sea_fill1: okay +sea_fill2: okay diff --git a/05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.c b/05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.c new file mode 100644 index 0000000..bf07915 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.c @@ -0,0 +1,15 @@ +int printf(const char *, ...); +char t[] = "012345678"; + +int main(void) +{ + char *data = t; + unsigned long long r = 4; + unsigned a = 5; + unsigned long long b = 12; + + *(unsigned*)(data + r) += a - b; + + printf("data = \"%s\"\n", data); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.expect b/05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.expect new file mode 100644 index 0000000..f91e4b4 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/91_ptr_longlong_arith32.expect @@ -0,0 +1 @@ +data = "0123-5678" diff --git a/05/tcc-final-old/tests/tests2/92_enum_bitfield.c b/05/tcc-final-old/tests/tests2/92_enum_bitfield.c new file mode 100644 index 0000000..bb6dc35 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/92_enum_bitfield.c @@ -0,0 +1,57 @@ +/* This checks if enums needing 8 bit but only having positive + values are correctly zero extended (instead of sign extended) + when stored into/loaded from a 8 bit bit-field of enum type (which + itself is implementation defined, so isn't necessarily supported by all + other compilers). */ +enum tree_code { + SOME_CODE = 148, /* has bit 7 set, and hence all further enum values as well */ + LAST_AND_UNUSED_TREE_CODE +}; +typedef union tree_node *tree; +struct tree_common +{ + union tree_node *chain; + union tree_node *type; + enum tree_code code : 8; + unsigned side_effects_flag : 1; +}; +union tree_node +{ + struct tree_common common; + }; +enum c_tree_code { + C_DUMMY_TREE_CODE = LAST_AND_UNUSED_TREE_CODE, + STMT_EXPR, + LAST_C_TREE_CODE +}; +enum cplus_tree_code { + CP_DUMMY_TREE_CODE = LAST_C_TREE_CODE, + AMBIG_CONV, + LAST_CPLUS_TREE_CODE +}; + +extern int printf(const char *, ...); +int blah(){return 0;} + +int convert_like_real (tree convs) +{ + switch (((enum tree_code) (convs)->common.code)) + { + case AMBIG_CONV: /* This has bit 7 set, which must not be the sign + bit in tree_common.code, i.e. the bitfield must + be somehow marked unsigned. */ + return blah(); + default: + break; + }; + printf("unsigned enum bit-fields broken\n"); +} + +int main() +{ + union tree_node convs; + + convs.common.code = AMBIG_CONV; + convert_like_real (&convs); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/92_enum_bitfield.expect b/05/tcc-final-old/tests/tests2/92_enum_bitfield.expect new file mode 100644 index 0000000..e69de29 diff --git a/05/tcc-final-old/tests/tests2/93_integer_promotion.c b/05/tcc-final-old/tests/tests2/93_integer_promotion.c new file mode 100644 index 0000000..a1176fc --- /dev/null +++ b/05/tcc-final-old/tests/tests2/93_integer_promotion.c @@ -0,0 +1,71 @@ +/* integer promotion */ + +int printf(const char*, ...); +#define promote(s) printf(" %ssigned : %s\n", (s) - 100 < 0 ? " " : "un", #s); + +int main (void) +{ + struct { + unsigned ub:3; + unsigned u:32; + unsigned long long ullb:35; + unsigned long long ull:64; + unsigned char c; + } s = { 1, 1, 1 }; + + promote(s.ub); + promote(s.u); + promote(s.ullb); + promote(s.ull); + promote(s.c); + printf("\n"); + + promote((1 ? s.ub : 1)); + promote((1 ? s.u : 1)); + promote((1 ? s.ullb : 1)); + promote((1 ? s.ull : 1)); + promote((1 ? s.c : 1)); + printf("\n"); + + promote(s.ub << 1); + promote(s.u << 1); + promote(s.ullb << 1); + promote(s.ull << 1); + promote(s.c << 1); + printf("\n"); + + promote(+s.ub); + promote(+s.u); + promote(+s.ullb); + promote(+s.ull); + promote(+s.c); + printf("\n"); + + promote(-s.ub); + promote(-s.u); + promote(-s.ullb); + promote(-s.ull); + promote(-s.c); + printf("\n"); + + promote(~s.ub); + promote(~s.u); + promote(~s.ullb); + promote(~s.ull); + promote(~s.c); + printf("\n"); + + promote(!s.ub); + promote(!s.u); + promote(!s.ullb); + promote(!s.ull); + promote(!s.c); + printf("\n"); + + promote(+(unsigned)s.ub); + promote(-(unsigned)s.ub); + promote(~(unsigned)s.ub); + promote(!(unsigned)s.ub); + + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/93_integer_promotion.expect b/05/tcc-final-old/tests/tests2/93_integer_promotion.expect new file mode 100644 index 0000000..34b9c14 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/93_integer_promotion.expect @@ -0,0 +1,46 @@ + signed : s.ub + unsigned : s.u + signed : s.ullb + unsigned : s.ull + signed : s.c + + signed : (1 ? s.ub : 1) + unsigned : (1 ? s.u : 1) + signed : (1 ? s.ullb : 1) + unsigned : (1 ? s.ull : 1) + signed : (1 ? s.c : 1) + + signed : s.ub << 1 + unsigned : s.u << 1 + signed : s.ullb << 1 + unsigned : s.ull << 1 + signed : s.c << 1 + + signed : +s.ub + unsigned : +s.u + signed : +s.ullb + unsigned : +s.ull + signed : +s.c + + signed : -s.ub + unsigned : -s.u + signed : -s.ullb + unsigned : -s.ull + signed : -s.c + + signed : ~s.ub + unsigned : ~s.u + signed : ~s.ullb + unsigned : ~s.ull + signed : ~s.c + + signed : !s.ub + signed : !s.u + signed : !s.ullb + signed : !s.ull + signed : !s.c + + unsigned : +(unsigned)s.ub + unsigned : -(unsigned)s.ub + unsigned : ~(unsigned)s.ub + signed : !(unsigned)s.ub diff --git a/05/tcc-final-old/tests/tests2/94_generic.c b/05/tcc-final-old/tests/tests2/94_generic.c new file mode 100644 index 0000000..d7fb5fc --- /dev/null +++ b/05/tcc-final-old/tests/tests2/94_generic.c @@ -0,0 +1,64 @@ +#include + +const int a = 0; + +struct a { + int a; +}; + +struct b { + int a; +}; + +int a_f() +{ + return 20; +} + +int b_f() +{ + return 10; +} + +typedef int int_type1; + +#define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123); + +int main() +{ + int i = 0; + signed long int l = 2; + struct b titi; + const int * const ptr; + const char *ti; + int_type1 i2; + + i = _Generic(a, int: a_f, const int: b_f)(); + printf("%d\n", i); + i = _Generic(a, int: a_f() / 2, const int: b_f() / 2); + printf("%d\n", i); + i = _Generic(ptr, int *:1, int * const:2, default:20); + printf("%d\n", i); + i = gen_sw(a); + printf("%d\n", i); + i = _Generic(titi, struct a:1, struct b:2, default:20); + printf("%d\n", i); + i = _Generic(i2, char: 1, int : 0); + printf("%d\n", i); + i = _Generic(a, char:1, int[4]:2, default:5); + printf("%d\n", i); + i = _Generic(17, int :1, int **:2); + printf("%d\n", i); + i = _Generic(17L, int :1, long :2, long long : 3); + printf("%d\n", i); + i = _Generic("17, io", char *: 3, const char *: 1); + printf("%d\n", i); + i = _Generic(ti, const unsigned char *:1, const char *:4, char *:3, + const signed char *:2); + printf("%d\n", i); + printf("%s\n", _Generic(i + 2L, long: "long", int: "int", + long long: "long long")); + i = _Generic(l, long: 1, int: 2); + printf("%d\n", i); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/94_generic.expect b/05/tcc-final-old/tests/tests2/94_generic.expect new file mode 100644 index 0000000..9aa9275 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/94_generic.expect @@ -0,0 +1,13 @@ +20 +10 +20 +123 +2 +0 +5 +1 +2 +3 +4 +long +1 \ No newline at end of file diff --git a/05/tcc-final-old/tests/tests2/95_bitfields.c b/05/tcc-final-old/tests/tests2/95_bitfields.c new file mode 100644 index 0000000..f025c57 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/95_bitfields.c @@ -0,0 +1,218 @@ +/* ----------------------------------------------------------------------- */ +#if TEST == 1 +{ + struct M P A __s + { + unsigned x : 12; + unsigned char y : 7; + unsigned z : 28; + unsigned a: 4; + unsigned b: 5; + }; + TEST_STRUCT(0x333,0x44,0x555555,6,7); +} + +/* ----------------------------------------------------------------------- */ +#elif TEST == 2 +{ + struct M P __s + { + int x: 12; + char y: 6; + long long z:63; + A char a:4; + long long b:2; + + }; + TEST_STRUCT(3,30,0x123456789abcdef0LL,5,2); +} + +/* ----------------------------------------------------------------------- */ +#elif TEST == 3 +{ + struct M P __s + { + unsigned x:5, y:5, :0, z:5; char a:5; A short b:5; + }; + TEST_STRUCT(21,23,25,6,14); +} + +/* ----------------------------------------------------------------------- */ +#elif TEST == 4 +{ + struct M P __s { + int x : 3; + int : 2; + int y : 1; + int : 0; + int z : 5; + int a : 7; + unsigned int b : 7; + }; + TEST_STRUCT(3,1,15,120,120); +} + +/* ----------------------------------------------------------------------- */ +#elif TEST == 5 +{ + struct M P __s { + long long x : 45; + long long : 2; + long long y : 30; + unsigned long long z : 38; + char a; short b; + }; + TEST_STRUCT(0x123456789ULL, 120<<25, 120, 0x44, 0x77); +} + +/* ----------------------------------------------------------------------- */ +#elif TEST == 6 +{ + struct M P __s { + int a; + signed char b; + int x : 12, y : 4, : 0, : 4, z : 3; + char d; + }; + TEST_STRUCT(1,2,3,4,-3); +} + +/* ----------------------------------------------------------------------- */ +#elif defined PACK + +#if PACK +# pragma pack(push,1) +# define P //_P +#else +# define P +#endif + +printf("\n\n" + 2*top); +#define TEST 1 +#include SELF +top = 0; +#define TEST 2 +#include SELF +#define TEST 3 +#include SELF +#define TEST 4 +#include SELF +#define TEST 5 +#include SELF +#define TEST 6 +#include SELF + +#if PACK +# pragma pack(pop) +#endif + +#undef P +#undef PACK + +/* ----------------------------------------------------------------------- */ +#elif defined ALIGN + +#if ALIGN +# define A _A(16) +#else +# define A +#endif + +#define PACK 0 +#include SELF +#define PACK 1 +#include SELF + +#undef A +#undef ALIGN + +/* ----------------------------------------------------------------------- */ +#elif defined MS_BF + +#if MS_BF +# ifdef __TINYC__ +# pragma comment(option, "-mms-bitfields") +# elif defined __GNUC__ +# define M __attribute__((ms_struct)) +# endif +#else +# ifdef __TINYC__ +# pragma comment(option, "-mno-ms-bitfields") +# elif defined __GNUC__ +# define M __attribute__((gcc_struct)) +# endif +#endif +#ifndef M +# define M +#endif + +#define ALIGN 0 +#include SELF +#define ALIGN 1 +#include SELF + +#undef M +#undef MS_BF + +/* ----------------------------------------------------------------------- */ +#else + +#include +#include +/* some gcc headers #define __attribute__ to empty if it's not gcc */ +#undef __attribute__ + +void dump(void *p, int s) +{ + int i; + for (i = s; --i >= 0;) + printf("%02X", ((unsigned char*)p)[i]); + printf("\n"); +} + +#define pv(m) \ + printf(sizeof (s->m + 0) == 8 ? " %016llx" : " %02x", s->m) + +#define TEST_STRUCT(v1,v2,v3,v4,v5) { \ + struct __s _s, *s = & _s; \ + printf("\n---- TEST %d%s%s%s ----\n" + top, \ + TEST, MS_BF?" - MS-BITFIELDS":"", \ + PACK?" - PACKED":"", \ + ALIGN?" - WITH ALIGN":""); \ + memset(s, 0, sizeof *s); \ + s->x = -1, s->y = -1, s->z = -1, s->a = -1, s->b = -1; \ + printf("bits in use : "), dump(s, sizeof *s); \ + s->x = v1, s->y = v2, s->z = v3, s->a += v4, ++s->a, s->b = v5; \ + printf("bits as set : "), dump(s, sizeof *s); \ + printf("values :"), pv(x), pv(y), pv(z), pv(a), pv(b), printf("\n"); \ + printf("align/size : %d %d\n", alignof(struct __s),sizeof(struct __s)); \ + } + +#ifdef _MSC_VER +# define _A(n) __declspec(align(n)) +# define _P +# define alignof(x) __alignof(x) +#else +# define _A(n) __attribute__((aligned(n))) +# define _P __attribute__((packed)) +# define alignof(x) __alignof__(x) +#endif + +#ifndef MS_BITFIELDS +# define MS_BITFIELDS 0 +#endif + +#define SELF "95_bitfields.c" + +int top = 1; + +int main() +{ +#define MS_BF MS_BITFIELDS +#include SELF + return 0; +} + +/* ----------------------------------------------------------------------- */ +#endif +#undef TEST diff --git a/05/tcc-final-old/tests/tests2/95_bitfields.expect b/05/tcc-final-old/tests/tests2/95_bitfields.expect new file mode 100644 index 0000000..6a8fd9a --- /dev/null +++ b/05/tcc-final-old/tests/tests2/95_bitfields.expect @@ -0,0 +1,149 @@ +---- TEST 1 ---- +bits in use : 0000001FFFFFFFFF007F0FFF +bits as set : 000000076055555500440333 +values : 333 44 555555 06 07 +align/size : 4 12 + +---- TEST 2 ---- +bits in use : 000000000000003F7FFFFFFFFFFFFFFF00000000003F0FFF +bits as set : 0000000000000025123456789ABCDEF000000000001E0003 +values : 03 1e 123456789abcdef0 05 fffffffe +align/size : 8 24 + +---- TEST 3 ---- +bits in use : 001F1F1F000003FF +bits as set : 000E0619000002F5 +values : 15 17 19 06 0e +align/size : 4 8 + +---- TEST 4 ---- +bits in use : 0007FFFF00000027 +bits as set : 00078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 4 8 + +---- TEST 5 ---- +bits in use : FFFFFF3FFFFFFFFF000000003FFFFFFF00001FFFFFFFFFFF +bits as set : 007744000000007800000000300000000000000123456789 +values : 0000000123456789 f0000000 0000000000000078 44 77 +align/size : 8 24 + +---- TEST 6 ---- +bits in use : 0000007000FFFFFFFFFFFFFF +bits as set : 00000030002001FD00000004 +values : 01 02 03 04 fffffffd +align/size : 4 12 + + + +---- TEST 1 - PACKED ---- +bits in use : FFFFFFFFFFFFFF +bits as set : 3B02AAAAAC4333 +values : 333 44 555555 06 07 +align/size : 1 7 + +---- TEST 2 - PACKED ---- +bits in use : 7FFFFFFFFFFFFFFFFFFFFF +bits as set : 4A48D159E26AF37BC1E003 +values : 03 1e 123456789abcdef0 05 fffffffe +align/size : 1 11 + +---- TEST 3 - PACKED ---- +bits in use : 7FFF000003FF +bits as set : 38D9000002F5 +values : 15 17 19 06 0e +align/size : 1 6 + +---- TEST 4 - PACKED ---- +bits in use : 07FFFF00000027 +bits as set : 078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 1 7 + +---- TEST 5 - PACKED ---- +bits in use : FFFFFF07FFFFFFFFFFFFFFFF9FFFFFFFFFFF +bits as set : 007744000000000F18000000000123456789 +values : 0000000123456789 f0000000 0000000000000078 44 77 +align/size : 1 18 + +---- TEST 6 - PACKED ---- +bits in use : 007000FFFFFFFFFFFFFF +bits as set : 0030002001FD00000004 +values : 01 02 03 04 fffffffd +align/size : 1 10 + + + +---- TEST 1 - WITH ALIGN ---- +bits in use : 000000000000001FFFFFFFFF007F0FFF +bits as set : 00000000000000076055555500440333 +values : 333 44 555555 06 07 +align/size : 16 16 + +---- TEST 2 - WITH ALIGN ---- +bits in use : 0000000000000000000000000000003F7FFFFFFFFFFFFFFF00000000003F0FFF +bits as set : 00000000000000000000000000000025123456789ABCDEF000000000001E0003 +values : 03 1e 123456789abcdef0 05 fffffffe +align/size : 16 32 + +---- TEST 3 - WITH ALIGN ---- +bits in use : 0000000000000000000000000000001F000000000000000000001F1F000003FF +bits as set : 0000000000000000000000000000000E000000000000000000000619000002F5 +values : 15 17 19 06 0e +align/size : 16 32 + +---- TEST 4 - WITH ALIGN ---- +bits in use : 0007FFFF00000027 +bits as set : 00078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 4 8 + +---- TEST 5 - WITH ALIGN ---- +bits in use : FFFFFF3FFFFFFFFF000000003FFFFFFF00001FFFFFFFFFFF +bits as set : 007744000000007800000000300000000000000123456789 +values : 0000000123456789 f0000000 0000000000000078 44 77 +align/size : 8 24 + +---- TEST 6 - WITH ALIGN ---- +bits in use : 0000007000FFFFFFFFFFFFFF +bits as set : 00000030002001FD00000004 +values : 01 02 03 04 fffffffd +align/size : 4 12 + + + +---- TEST 1 - PACKED - WITH ALIGN ---- +bits in use : 000000000000000000FFFFFFFFFFFFFF +bits as set : 0000000000000000003B02AAAAAC4333 +values : 333 44 555555 06 07 +align/size : 16 16 + +---- TEST 2 - PACKED - WITH ALIGN ---- +bits in use : 3F01FFFFFFFFFFFFFFFFFFFF +bits as set : 250048D159E26AF37BC1E003 +values : 03 1e 123456789abcdef0 05 fffffffe +align/size : 1 12 + +---- TEST 3 - PACKED - WITH ALIGN ---- +bits in use : 1F03FF000003FF +bits as set : 0E00D9000002F5 +values : 15 17 19 06 0e +align/size : 1 7 + +---- TEST 4 - PACKED - WITH ALIGN ---- +bits in use : 07FFFF00000027 +bits as set : 078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 1 7 + +---- TEST 5 - PACKED - WITH ALIGN ---- +bits in use : FFFFFF07FFFFFFFFFFFFFFFF9FFFFFFFFFFF +bits as set : 007744000000000F18000000000123456789 +values : 0000000123456789 f0000000 0000000000000078 44 77 +align/size : 1 18 + +---- TEST 6 - PACKED - WITH ALIGN ---- +bits in use : 007000FFFFFFFFFFFFFF +bits as set : 0030002001FD00000004 +values : 01 02 03 04 fffffffd +align/size : 1 10 diff --git a/05/tcc-final-old/tests/tests2/95_bitfields_ms.c b/05/tcc-final-old/tests/tests2/95_bitfields_ms.c new file mode 100644 index 0000000..b196fbd --- /dev/null +++ b/05/tcc-final-old/tests/tests2/95_bitfields_ms.c @@ -0,0 +1,2 @@ +#define MS_BITFIELDS 1 +#include "95_bitfields.c" diff --git a/05/tcc-final-old/tests/tests2/95_bitfields_ms.expect b/05/tcc-final-old/tests/tests2/95_bitfields_ms.expect new file mode 100644 index 0000000..8ccafb7 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/95_bitfields_ms.expect @@ -0,0 +1,149 @@ +---- TEST 1 - MS-BITFIELDS ---- +bits in use : 0000001FFFFFFFFF0000007F00000FFF +bits as set : 00000007605555550000004400000333 +values : 333 44 555555 06 07 +align/size : 4 16 + +---- TEST 2 - MS-BITFIELDS ---- +bits in use : 0000000000000003000000000000000F7FFFFFFFFFFFFFFF0000003F00000FFF +bits as set : 00000000000000020000000000000005123456789ABCDEF00000001E00000003 +values : 03 1e 123456789abcdef0 05 fffffffffffffffe +align/size : 8 32 + +---- TEST 3 - MS-BITFIELDS ---- +bits in use : 001F001F0000001F000003FF +bits as set : 000E000600000019000002F5 +values : 15 17 19 06 0e +align/size : 4 12 + +---- TEST 4 - MS-BITFIELDS ---- +bits in use : 0007FFFF00000027 +bits as set : 00078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 4 8 + +---- TEST 5 - MS-BITFIELDS ---- +bits in use : 00000000FFFF00FF0000003FFFFFFFFF000000003FFFFFFF00001FFFFFFFFFFF +bits as set : 0000000000770044000000000000007800000000300000000000000123456789 +values : 0000000123456789 fffffffff0000000 0000000000000078 44 77 +align/size : 8 32 + +---- TEST 6 - MS-BITFIELDS ---- +bits in use : 00000000000000700000FFFF000000FFFFFFFFFF +bits as set : 000000000000003000002001000000FD00000004 +values : 01 02 03 04 fffffffd +align/size : 4 20 + + + +---- TEST 1 - MS-BITFIELDS - PACKED ---- +bits in use : 0000001FFFFFFFFF7F00000FFF +bits as set : 00000007605555554400000333 +values : 333 44 555555 06 07 +align/size : 1 13 + +---- TEST 2 - MS-BITFIELDS - PACKED ---- +bits in use : 00000000000000030F7FFFFFFFFFFFFFFF3F00000FFF +bits as set : 000000000000000205123456789ABCDEF01E00000003 +values : 03 1e 123456789abcdef0 05 fffffffffffffffe +align/size : 1 22 + +---- TEST 3 - MS-BITFIELDS - PACKED ---- +bits in use : 001F1F0000001F000003FF +bits as set : 000E0600000019000002F5 +values : 15 17 19 06 0e +align/size : 1 11 + +---- TEST 4 - MS-BITFIELDS - PACKED ---- +bits in use : 0007FFFF00000027 +bits as set : 00078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 1 8 + +---- TEST 5 - MS-BITFIELDS - PACKED ---- +bits in use : FFFFFF0000003FFFFFFFFF000000003FFFFFFF00001FFFFFFFFFFF +bits as set : 007744000000000000007800000000300000000000000123456789 +values : 0000000123456789 fffffffff0000000 0000000000000078 44 77 +align/size : 1 27 + +---- TEST 6 - MS-BITFIELDS - PACKED ---- +bits in use : 00000000700000FFFFFFFFFFFFFF +bits as set : 000000003000002001FD00000004 +values : 01 02 03 04 fffffffd +align/size : 1 14 + + + +---- TEST 1 - MS-BITFIELDS - WITH ALIGN ---- +bits in use : 0000001FFFFFFFFF0000007F00000FFF +bits as set : 00000007605555550000004400000333 +values : 333 44 555555 06 07 +align/size : 16 16 + +---- TEST 2 - MS-BITFIELDS - WITH ALIGN ---- +bits in use : 0000000000000003000000000000000F7FFFFFFFFFFFFFFF0000003F00000FFF +bits as set : 00000000000000020000000000000005123456789ABCDEF00000001E00000003 +values : 03 1e 123456789abcdef0 05 fffffffffffffffe +align/size : 16 32 + +---- TEST 3 - MS-BITFIELDS - WITH ALIGN ---- +bits in use : 0000000000000000000000000000001F000000000000001F0000001F000003FF +bits as set : 0000000000000000000000000000000E000000000000000600000019000002F5 +values : 15 17 19 06 0e +align/size : 16 32 + +---- TEST 4 - MS-BITFIELDS - WITH ALIGN ---- +bits in use : 0007FFFF00000027 +bits as set : 00078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 4 8 + +---- TEST 5 - MS-BITFIELDS - WITH ALIGN ---- +bits in use : 00000000FFFF00FF0000003FFFFFFFFF000000003FFFFFFF00001FFFFFFFFFFF +bits as set : 0000000000770044000000000000007800000000300000000000000123456789 +values : 0000000123456789 fffffffff0000000 0000000000000078 44 77 +align/size : 8 32 + +---- TEST 6 - MS-BITFIELDS - WITH ALIGN ---- +bits in use : 00000000000000700000FFFF000000FFFFFFFFFF +bits as set : 000000000000003000002001000000FD00000004 +values : 01 02 03 04 fffffffd +align/size : 4 20 + + + +---- TEST 1 - MS-BITFIELDS - PACKED - WITH ALIGN ---- +bits in use : 0000000000001FFFFFFFFF7F00000FFF +bits as set : 00000000000007605555554400000333 +values : 333 44 555555 06 07 +align/size : 16 16 + +---- TEST 2 - MS-BITFIELDS - PACKED - WITH ALIGN ---- +bits in use : 00000000000000030F0000007FFFFFFFFFFFFFFF3F00000FFF +bits as set : 000000000000000205000000123456789ABCDEF01E00000003 +values : 03 1e 123456789abcdef0 05 fffffffffffffffe +align/size : 16 25 + +---- TEST 3 - MS-BITFIELDS - PACKED - WITH ALIGN ---- +bits in use : 001F000000000000001F0000001F000003FF +bits as set : 000E000000000000000600000019000002F5 +values : 15 17 19 06 0e +align/size : 16 18 + +---- TEST 4 - MS-BITFIELDS - PACKED - WITH ALIGN ---- +bits in use : 0007FFFF00000027 +bits as set : 00078F0F00000023 +values : 03 ffffffff 0f fffffff8 78 +align/size : 1 8 + +---- TEST 5 - MS-BITFIELDS - PACKED - WITH ALIGN ---- +bits in use : FFFFFF0000003FFFFFFFFF000000003FFFFFFF00001FFFFFFFFFFF +bits as set : 007744000000000000007800000000300000000000000123456789 +values : 0000000123456789 fffffffff0000000 0000000000000078 44 77 +align/size : 1 27 + +---- TEST 6 - MS-BITFIELDS - PACKED - WITH ALIGN ---- +bits in use : 00000000700000FFFFFFFFFFFFFF +bits as set : 000000003000002001FD00000004 +values : 01 02 03 04 fffffffd +align/size : 1 14 diff --git a/05/tcc-final-old/tests/tests2/96_nodata_wanted.c b/05/tcc-final-old/tests/tests2/96_nodata_wanted.c new file mode 100644 index 0000000..cc211d3 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/96_nodata_wanted.c @@ -0,0 +1,84 @@ +/*****************************************************************************/ +/* test 'nodata_wanted' data output suppression */ + +#if defined test_static_data_error +void foo() { + if (1) { + static short w = (int)&foo; /* initializer not computable */ + } +} + +#elif defined test_static_nodata_error +void foo() { + if (0) { + static short w = (int)&foo; /* initializer not computable */ + } +} + +#elif defined test_global_data_error +void foo(); +static short w = (int)&foo; /* initializer not computable */ + + +#elif defined test_local_data_noerror +void foo() { + short w = &foo; /* 2 cast warnings */ +} + +#elif defined test_data_suppression_off || defined test_data_suppression_on + +#if defined test_data_suppression_on +# define SKIP 1 +#else +# define SKIP 0 +#endif + +#include +/* some gcc headers #define __attribute__ to empty if it's not gcc */ +#undef __attribute__ + +int main() +{ + __label__ ts0, te0, ts1, te1; + int tl, dl; + + static char ds0 = 0; + static char de0 = 0; + /* get reference size of empty jmp */ +ts0:; + if (!SKIP) {} +te0:; + dl = -(&de0 - &ds0); + tl = -(&&te0 - &&ts0); + + /* test data and code suppression */ + static char ds1 = 0; +ts1:; + if (!SKIP) { + static void *p = (void*)&main; + static char cc[] = "static string"; + static double d = 8.0; + + static struct __attribute__((packed)) { + unsigned x : 12; + unsigned char y : 7; + unsigned z : 28, a: 4, b: 5; + } s = { 0x333,0x44,0x555555,6,7 }; + + printf("data:\n"); + printf(" %d - %.1f - %.1f - %s - %s\n", + sizeof 8.0, 8.0, d, __FUNCTION__, cc); + printf(" %x %x %x %x %x\n", + s.x, s.y, s.z, s.a, s.b); + } +te1:; + static char de1 = 0; + + dl += &de1 - &ds1; + tl += &&te1 - &&ts1; + printf("size of data/text:\n %s/%s\n", + dl ? "non-zero":"zero", tl ? "non-zero":"zero"); + /*printf("# %d/%d\n", dl, tl);*/ +} + +#endif diff --git a/05/tcc-final-old/tests/tests2/96_nodata_wanted.expect b/05/tcc-final-old/tests/tests2/96_nodata_wanted.expect new file mode 100644 index 0000000..2749109 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/96_nodata_wanted.expect @@ -0,0 +1,23 @@ +[test_static_data_error] +96_nodata_wanted.c:7: error: initializer element is not computable at load time + +[test_static_nodata_error] +96_nodata_wanted.c:14: error: initializer element is not computable at load time + +[test_global_data_error] +96_nodata_wanted.c:20: error: initializer element is not computable at load time + +[test_local_data_noerror] +96_nodata_wanted.c:25: warning: assignment makes integer from pointer without a cast +96_nodata_wanted.c:25: warning: nonportable conversion from pointer to char/short + +[test_data_suppression_off] +data: + 8 - 8.0 - 8.0 - main - static string + 333 44 555555 6 7 +size of data/text: + non-zero/non-zero + +[test_data_suppression_on] +size of data/text: + zero/zero diff --git a/05/tcc-final-old/tests/tests2/97_utf8_string_literal.c b/05/tcc-final-old/tests/tests2/97_utf8_string_literal.c new file mode 100644 index 0000000..96fbab0 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/97_utf8_string_literal.c @@ -0,0 +1,12 @@ +// this file contains BMP chars encoded in UTF-8 +#include +#include + +int main() +{ + wchar_t s[] = L"hello$$你好¢¢世界€€world"; + wchar_t *p; + for (p = s; *p; p++) printf("%04X ", (unsigned) *p); + printf("\n"); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/97_utf8_string_literal.expect b/05/tcc-final-old/tests/tests2/97_utf8_string_literal.expect new file mode 100644 index 0000000..9a1593c --- /dev/null +++ b/05/tcc-final-old/tests/tests2/97_utf8_string_literal.expect @@ -0,0 +1 @@ +0068 0065 006C 006C 006F 0024 0024 4F60 597D 00A2 00A2 4E16 754C 20AC 20AC 0077 006F 0072 006C 0064 diff --git a/05/tcc-final-old/tests/tests2/98_al_ax_extend.c b/05/tcc-final-old/tests/tests2/98_al_ax_extend.c new file mode 100644 index 0000000..9b4e02f --- /dev/null +++ b/05/tcc-final-old/tests/tests2/98_al_ax_extend.c @@ -0,0 +1,41 @@ +#include +#include +asm ( + ".text;" + ".globl _us;.globl _ss;.globl _uc;.globl _sc;" + "_us:;_ss:;_uc:;_sc:;" + "movl $0x1234ABCD, %eax;" + "ret;" +); + +#if 1 +#define us _us +#define ss _ss +#define uc _uc +#define sc _sc +#endif + +int main() +{ + unsigned short us(void); + short ss(void); + unsigned char uc(void); + signed char sc(void); + + unsigned short (*fpus)(void) = us; + short (*fpss)(void) = ss; + unsigned char (*fpuc)(void) = uc; + signed char (*fpsc)(void) = sc; + + printf("%08X %08X\n", us() + 1, fpus() + 1); + printf("%08X %08X\n", ss() + 1, fpss() + 1); + printf("%08X %08X\n", uc() + 1, fpuc() + 1); + printf("%08X %08X\n", sc() + 1, fpsc() + 1); + printf("\n"); + printf("%08X %08X\n", fpus() + 1, us() + 1); + printf("%08X %08X\n", fpss() + 1, ss() + 1); + printf("%08X %08X\n", fpuc() + 1, uc() + 1); + printf("%08X %08X\n", fpsc() + 1, sc() + 1); + + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/98_al_ax_extend.expect b/05/tcc-final-old/tests/tests2/98_al_ax_extend.expect new file mode 100644 index 0000000..c5752e8 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/98_al_ax_extend.expect @@ -0,0 +1,9 @@ +0000ABCE 0000ABCE +FFFFABCE FFFFABCE +000000CE 000000CE +FFFFFFCE FFFFFFCE + +0000ABCE 0000ABCE +FFFFABCE FFFFABCE +000000CE 000000CE +FFFFFFCE FFFFFFCE diff --git a/05/tcc-final-old/tests/tests2/99_fastcall.c b/05/tcc-final-old/tests/tests2/99_fastcall.c new file mode 100644 index 0000000..ee4b67d --- /dev/null +++ b/05/tcc-final-old/tests/tests2/99_fastcall.c @@ -0,0 +1,276 @@ +#include +#include + +#ifndef _WIN32 +#define __fastcall __attribute((fastcall)) +#endif + +#if 1 +#define SYMBOL(x) _##x +#else +#define SYMBOL(x) x +#endif + +///////////////////////////////////////////////////////////////////////// +////////// TRAP FRAMEWORK +///////////////////////////////////////////////////////////////////////// +// if you cast 'TRAP' to a function pointer and call it, +// it will save all 8 registers, +// and jump into C-code (previously set using 'SET_TRAP_HANDLER(x)'), +// in C-code you can pop DWORDs from stack and modify registers +// + +void *SYMBOL(trap_handler); + +extern unsigned char SYMBOL(trap)[]; +asm ( + ".text;" + "_trap:;" + "pushl %esp;" + "pusha;" + "addl $0x4, 0xc(%esp);" + "pushl %esp;" + "call *_trap_handler;" + "addl $0x4, %esp;" + "movl 0xc(%esp), %eax;" + "movl %eax, 0x20(%esp);" + "popa;" + "popl %esp;" + "ret;" +); + +struct trapframe { + unsigned edi, esi, ebp, esp, ebx, edx, ecx, eax; +}; + + +#define M_FLOAT(addr) (*(float *)(addr)) +#define M_DWORD(addr) (*(unsigned *)(addr)) +#define M_WORD(addr) (*(unsigned short *)(addr)) +#define M_BYTE(addr) (*(unsigned char *)(addr)) +#define R_EAX ((tf)->eax) +#define R_ECX ((tf)->ecx) +#define R_EDX ((tf)->edx) +#define R_EBX ((tf)->ebx) +#define R_ESP ((tf)->esp) +#define R_EBP ((tf)->ebp) +#define R_ESI ((tf)->esi) +#define R_EDI ((tf)->edi) + +#define ARG(x) (M_DWORD(R_ESP + (x) * 4)) + +#define RETN(x) do { \ + M_DWORD(R_ESP + (x)) = M_DWORD(R_ESP); \ + R_ESP += (x); \ +} while (0) + +#define DUMP() do { \ + unsigned i; \ + printf("EAX: %08X\n", R_EAX); \ + printf("ECX: %08X\n", R_ECX); \ + printf("EDX: %08X\n", R_EDX); \ + printf("EBX: %08X\n", R_EBX); \ + printf("ESP: %08X\n", R_ESP); \ + printf("EBP: %08X\n", R_EBP); \ + printf("ESI: %08X\n", R_ESI); \ + printf("EDI: %08X\n", R_EDI); \ + printf("\n"); \ + printf("[RETADDR]: %08X\n", M_DWORD(R_ESP)); \ + for (i = 1; i <= 8; i++) { \ + printf("[ARG%4d]: %08X\n", i, ARG(i)); \ + } \ +} while (0) + +#define SET_TRAP_HANDLER(x) ((SYMBOL(trap_handler)) = (x)) +#define TRAP ((void *) &SYMBOL(trap)) + + + +///////////////////////////////////////////////////////////////////////// +////////// SAFECALL FRAMEWORK +///////////////////////////////////////////////////////////////////////// +// this framework will convert any calling convention to cdecl +// usage: first set call target with 'SET_SAFECALL_TARGET(x)' +// then cast 'SAFECALL' to target function pointer type and invoke it +// after calling, 'ESPDIFF' is the difference of old and new esp + +void *SYMBOL(sc_call_target); +unsigned SYMBOL(sc_retn_addr); +unsigned SYMBOL(sc_old_esp); +unsigned SYMBOL(sc_new_esp); + +extern unsigned char SYMBOL(safecall)[]; +asm ( + ".text;" + "_safecall:;" + "popl _sc_retn_addr;" + "movl %esp, _sc_old_esp;" + "call *_sc_call_target;" + "movl %esp, _sc_new_esp;" + "movl _sc_old_esp, %esp;" + "jmp *_sc_retn_addr;" +); + +#define SET_SAFECALL_TARGET(x) ((SYMBOL(sc_call_target)) = (x)) +#define SAFECALL ((void *) &SYMBOL(safecall)) +#define ESPDIFF (SYMBOL(sc_new_esp) - SYMBOL(sc_old_esp)) + + +///////////////////////////////////////////////////////////////////////// +////////// TEST FASTCALL INVOKE +///////////////////////////////////////////////////////////////////////// + +void check_fastcall_invoke_0(struct trapframe *tf) +{ + //DUMP(); + RETN(0); +} + +void check_fastcall_invoke_1(struct trapframe *tf) +{ + //DUMP(); + assert(R_ECX == 0x11111111); + RETN(0); +} +void check_fastcall_invoke_2(struct trapframe *tf) +{ + //DUMP(); + assert(R_ECX == 0x11111111); + assert(R_EDX == 0x22222222); + RETN(0); +} +void check_fastcall_invoke_3(struct trapframe *tf) +{ + //DUMP(); + assert(R_ECX == 0x11111111); + assert(R_EDX == 0x22222222); + assert(ARG(1) == 0x33333333); + RETN(1*4); +} +void check_fastcall_invoke_4(struct trapframe *tf) +{ + //DUMP(); + assert(R_ECX == 0x11111111); + assert(R_EDX == 0x22222222); + assert(ARG(1) == 0x33333333); + assert(ARG(2) == 0x44444444); + RETN(2*4); +} + +void check_fastcall_invoke_5(struct trapframe *tf) +{ + //DUMP(); + assert(R_ECX == 0x11111111); + assert(R_EDX == 0x22222222); + assert(ARG(1) == 0x33333333); + assert(ARG(2) == 0x44444444); + assert(ARG(3) == 0x55555555); + RETN(3*4); +} + +void test_fastcall_invoke() +{ + SET_TRAP_HANDLER(check_fastcall_invoke_0); + ((void __fastcall (*)(void)) TRAP)(); + + SET_TRAP_HANDLER(check_fastcall_invoke_1); + ((void __fastcall (*)(unsigned)) TRAP)(0x11111111); + + SET_TRAP_HANDLER(check_fastcall_invoke_2); + ((void __fastcall (*)(unsigned, unsigned)) TRAP)(0x11111111, 0x22222222); + + SET_TRAP_HANDLER(check_fastcall_invoke_3); + ((void __fastcall (*)(unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333); + + SET_TRAP_HANDLER(check_fastcall_invoke_4); + ((void __fastcall (*)(unsigned, unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333, 0x44444444); + + SET_TRAP_HANDLER(check_fastcall_invoke_5); + ((void __fastcall (*)(unsigned, unsigned, unsigned, unsigned, unsigned)) TRAP)(0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555); +} + + +///////////////////////////////////////////////////////////////////////// +////////// TEST FUNCTION CODE GENERATION +///////////////////////////////////////////////////////////////////////// + +int __fastcall check_fastcall_espdiff_0(void) +{ + return 0; +} + +int __fastcall check_fastcall_espdiff_1(int a) +{ + return a; +} + +int __fastcall check_fastcall_espdiff_2(int a, int b) +{ + return a + b; +} + +int __fastcall check_fastcall_espdiff_3(int a, int b, int c) +{ + return a + b + c; +} + +int __fastcall check_fastcall_espdiff_4(int a, int b, int c, int d) +{ + return a + b + c + d; +} + +int __fastcall check_fastcall_espdiff_5(int a, int b, int c, int d, int e) +{ + return a + b + c + d + e; +} + +void test_fastcall_espdiff() +{ + int x; + SET_SAFECALL_TARGET(check_fastcall_espdiff_0); + x = ((typeof(&check_fastcall_espdiff_0))SAFECALL)(); + assert(x == 0); + assert(ESPDIFF == 0); + + SET_SAFECALL_TARGET(check_fastcall_espdiff_1); + x = ((typeof(&check_fastcall_espdiff_1))SAFECALL)(1); + assert(x == 1); + assert(ESPDIFF == 0); + + SET_SAFECALL_TARGET(check_fastcall_espdiff_2); + x = ((typeof(&check_fastcall_espdiff_2))SAFECALL)(1, 2); + assert(x == 1 + 2); + assert(ESPDIFF == 0); + + SET_SAFECALL_TARGET(check_fastcall_espdiff_3); + x = ((typeof(&check_fastcall_espdiff_3))SAFECALL)(1, 2, 3); + assert(x == 1 + 2 + 3); + assert(ESPDIFF == 1*4); + + SET_SAFECALL_TARGET(check_fastcall_espdiff_4); + x = ((typeof(&check_fastcall_espdiff_4))SAFECALL)(1, 2, 3, 4); + assert(x == 1 + 2 + 3 + 4); + assert(ESPDIFF == 2*4); + + SET_SAFECALL_TARGET(check_fastcall_espdiff_5); + x = ((typeof(&check_fastcall_espdiff_5))SAFECALL)(1, 2, 3, 4, 5); + assert(x == 1 + 2 + 3 + 4 + 5); + assert(ESPDIFF == 3*4); +} + +int main() +{ +#define N 10000 + int i; + + for (i = 1; i <= N; i++) { + test_fastcall_espdiff(); + } + + for (i = 1; i <= N; i++) { + test_fastcall_invoke(); + } + + puts("TEST OK"); + return 0; +} diff --git a/05/tcc-final-old/tests/tests2/99_fastcall.expect b/05/tcc-final-old/tests/tests2/99_fastcall.expect new file mode 100644 index 0000000..3835d63 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/99_fastcall.expect @@ -0,0 +1 @@ +TEST OK diff --git a/05/tcc-final-old/tests/tests2/LICENSE b/05/tcc-final-old/tests/tests2/LICENSE new file mode 100644 index 0000000..b08a652 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/LICENSE @@ -0,0 +1,37 @@ +The tests in this directory are either directly copied from the picoc project or +are subsequently modified and added to for the purpose of TinyCC project. All +these modifications are licensed under the same terms as TinyCC as specified in +the file COPYING. + +=== picoc license === + +Copyright (c) 2009-2011, Zik Saleeba +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + * Neither the name of the Zik Saleeba nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/05/tcc-final-old/tests/tests2/Makefile b/05/tcc-final-old/tests/tests2/Makefile new file mode 100644 index 0000000..190b2d9 --- /dev/null +++ b/05/tcc-final-old/tests/tests2/Makefile @@ -0,0 +1,112 @@ +TOP = ../.. +include $(TOP)/Makefile +SRC = $(TOPSRC)/tests/tests2 +VPATH = $(SRC) + +TESTS = $(patsubst %.c,%.test,$(sort $(notdir $(wildcard $(SRC)/*.c)))) + +# some tests do not pass on all platforms, remove them for now +SKIP = 34_array_assignment.test # array assignment is not in C standard +ifeq ($(CONFIG_arm_eabi),yes) # not ARM soft-float + SKIP += 22_floating_point.test +endif +ifdef CONFIG_OSX + SKIP += 40_stdio.test 42_function_pointer.test + FLAGS += -w +endif +ifeq ($(ARCH),x86_64) + SKIP += 73_arm64.test +endif +ifeq (,$(filter i386,$(ARCH))) + SKIP += 98_al_ax_extend.test 99_fastcall.test +endif +ifeq (,$(filter i386 x86_64,$(ARCH))) + SKIP += 85_asm-outside-function.test +endif +ifeq (-$(findstring gcc,$(CC))-,--) + SKIP += $(patsubst %.expect,%.test,$(GEN-ALWAYS)) +endif +ifeq (-$(CONFIG_WIN32)-$(CONFIG_i386)$(CONFIG_arm)-,--yes-) + SKIP += 95_bitfields%.test # type_align is different on 32bit-non-windows +endif + +# Some tests might need arguments +ARGS = +31_args.test : ARGS = arg1 arg2 arg3 arg4 arg5 +46_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' $(SRC)/46_grep.c + +# And some tests don't test the right thing with -run +NORUN = +42_function_pointer.test : NORUN = true + +# Some tests might need different flags +FLAGS = +76_dollars_in_identifiers.test : FLAGS += -fdollars-in-identifiers + +# These tests run several snippets from the same file one by one +60_errors_and_warnings.test : FLAGS += -dt +96_nodata_wanted.test : FLAGS += -dt + +# Always generate certain .expects (don't put these in the GIT), +GEN-ALWAYS = +# GEN-ALWAYS += 95_bitfields.expect # does not work + +# using the ms compiler for the really ms-compatible bitfields +95_bitfields_ms.test : GEN = $(GEN-MSC) + +# Filter source directory in warnings/errors (out-of-tree builds) +FILTER = 2>&1 | sed 's,$(SRC)/,,g' +# Filter some always-warning +ifeq (-$(findstring arm,$(ARCH))-,-arm-) +FILTER += 2>&1 | grep -v 'warning: soft float ABI currently not supported' +endif + +all test tests2.all: $(filter-out $(SKIP),$(TESTS)) ; + +%.test: %.c %.expect + @echo Test: $*... + @$(if $(NORUN),$(T1),$(T2)) $(if $(NODIFF),,$(T3)) + +T1 = $(TCC) $(FLAGS) $< -o a.exe && ./a.exe $(ARGS) +T2 = $(TCC) $(FLAGS) -run $< $(ARGS) +T3 = $(FILTER) >$*.output 2>&1 || true \ + && diff -Nbu $(filter %.expect,$^) $*.output \ + && rm -f $*.output $(filter $*.expect,$(GEN-ALWAYS)) + +# run single test and update .expect file, e.g. "make tests2.37+" +tests2.%+: + @$(MAKE) $(call F2,$(call F1,$*)) --no-print-directory + +# just run tcc to see the output, e.g. "make tests2.37-" +tests2.%-: + @$(MAKE) $(call F1,$*) NODIFF=true --no-print-directory + +# run single test, e.g. "make tests2.37" +tests2.%: + @$(MAKE) $(call F1,$*) --no-print-directory + +F1 = $(or $(filter $1_%,$(TESTS)),$1_???.test) +F2 = $1 UPDATE="$(patsubst %.test,%.expect,$1)" + +# automatically generate .expect files with gcc: +%.expect : + @echo Generating: $@ + @$(call GEN,$(SRC)/$*.c) $(FILTER) >$@ 2>&1 + @rm -f *.exe *.obj *.pdb + +# using TCC for .expect if -dt in FLAGS +GEN = $(if $(filter -dt,$(FLAGS)),$(GEN-TCC),$(GEN-CC)) +GEN-CC = $(CC) -w -std=gnu99 $(FLAGS) $1 -o a.exe && ./a.exe $(ARGS) +GEN-TCC = $(TCC) $(FLAGS) -run $1 $(ARGS) +GEN-MSC = $(MS-CC) $1 && ./$(basename $@).exe +MS-CC = cl + +# tell make not to delete +.PRECIOUS: %.expect + +# force .expect generation for these files +$(sort $(GEN-ALWAYS) $(UPDATE)) : force +force: + +clean: + rm -f fred.txt *.output a.exe $(GEN-ALWAYS) diff --git a/05/tcc-final-old/tests/vla_test.c b/05/tcc-final-old/tests/vla_test.c new file mode 100644 index 0000000..3616c46 --- /dev/null +++ b/05/tcc-final-old/tests/vla_test.c @@ -0,0 +1,84 @@ +/* + * Test that allocating a variable length array in a loop + * does not use up a linear amount of memory + */ + +#include +#include +#include + +#define LOOP_COUNT 1000 +#define ARRAY_SIZE 100 + +/* Overwrite a VLA. This will overwrite the return address if SP is incorrect */ +void smash(char *p, int n) { + memset(p, 0, n); +} + +int test1(int n) { + int i; + char *array_ptrs[LOOP_COUNT]; + + for (i = 0; i < LOOP_COUNT; ++i) { + char test[n]; + smash(test, n); + array_ptrs[i] = test; + } + + return (array_ptrs[0]-array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1; +} + +/* ensure goto does not circumvent array free */ +int test2(int n) { + char *array_ptrs[LOOP_COUNT]; + + int i = 0; +loop:; + char test[n]; + smash(test, n); + if (i >= LOOP_COUNT) + goto end; + array_ptrs[i] = test; + ++i; + goto loop; + +end: + smash(test, n); + char test2[n]; + smash(test2, n); + return (array_ptrs[0] - array_ptrs[LOOP_COUNT-1] < n) ? 0 : 1; +} + +int test3(int n) { + char test[n]; + smash(test, n); + goto label; +label: + smash(test, n); + char test2[n]; + smash(test2, n); + return (test-test2 >= n) ? 0 : 1; +} + +#define RUN_TEST(t) \ + if (!testname || (strcmp(#t, testname) == 0)) { \ + fputs(#t "... ", stdout); \ + fflush(stdout); \ + if (t(ARRAY_SIZE) == 0) { \ + fputs("success\n", stdout); \ + } else { \ + fputs("failure\n", stdout); \ + retval = EXIT_FAILURE; \ + } \ + } + +int main(int argc, char **argv) { + const char *testname = NULL; + int retval = EXIT_SUCCESS; + if (argc > 1) + testname = argv[1]; + RUN_TEST(test1) + RUN_TEST(test2) + RUN_TEST(test3) + return retval; +} diff --git a/05/tcc-final-old/texi2pod.pl b/05/tcc-final-old/texi2pod.pl new file mode 100755 index 0000000..d86e176 --- /dev/null +++ b/05/tcc-final-old/texi2pod.pl @@ -0,0 +1,427 @@ +#! /usr/bin/perl -w + +# Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. + +# This file is part of GNU CC. + +# GNU CC is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. + +# GNU CC is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with GNU CC; see the file COPYING. If not, write to +# the Free Software Foundation, 59 Temple Place - Suite 330, +# Boston MA 02111-1307, USA. + +# This does trivial (and I mean _trivial_) conversion of Texinfo +# markup to Perl POD format. It's intended to be used to extract +# something suitable for a manpage from a Texinfo document. + +$output = 0; +$skipping = 0; +%sects = (); +$section = ""; +@icstack = (); +@endwstack = (); +@skstack = (); +@instack = (); +$shift = ""; +%defs = (); +$fnno = 1; +$inf = ""; +$ibase = ""; + +while ($_ = shift) { + if (/^-D(.*)$/) { + if ($1 ne "") { + $flag = $1; + } else { + $flag = shift; + } + $value = ""; + ($flag, $value) = ($flag =~ /^([^=]+)(?:=(.+))?/); + die "no flag specified for -D\n" + unless $flag ne ""; + die "flags may only contain letters, digits, hyphens, dashes and underscores\n" + unless $flag =~ /^[a-zA-Z0-9_-]+$/; + $defs{$flag} = $value; + } elsif (/^-/) { + usage(); + } else { + $in = $_, next unless defined $in; + $out = $_, next unless defined $out; + usage(); + } +} + +if (defined $in) { + $inf = gensym(); + open($inf, "<$in") or die "opening \"$in\": $!\n"; + $ibase = $1 if $in =~ m|^(.+)/[^/]+$|; +} else { + $inf = \*STDIN; +} + +if (defined $out) { + open(STDOUT, ">$out") or die "opening \"$out\": $!\n"; +} + +while(defined $inf) { +while(<$inf>) { + # Certain commands are discarded without further processing. + /^\@(?: + [a-z]+index # @*index: useful only in complete manual + |need # @need: useful only in printed manual + |(?:end\s+)?group # @group .. @end group: ditto + |page # @page: ditto + |node # @node: useful only in .info file + |(?:end\s+)?ifnottex # @ifnottex .. @end ifnottex: use contents + )\b/x and next; + + chomp; + + # Look for filename and title markers. + /^\@setfilename\s+([^.]+)/ and $fn = $1, next; + /^\@settitle\s+([^.]+)/ and $tl = postprocess($1), next; + + # Identify a man title but keep only the one we are interested in. + /^\@c\s+man\s+title\s+([A-Za-z0-9-]+)\s+(.+)/ and do { + if (exists $defs{$1}) { + $fn = $1; + $tl = postprocess($2); + } + next; + }; + + # Look for blocks surrounded by @c man begin SECTION ... @c man end. + # This really oughta be @ifman ... @end ifman and the like, but such + # would require rev'ing all other Texinfo translators. + /^\@c\s+man\s+begin\s+([A-Z]+)\s+([A-Za-z0-9-]+)/ and do { + $output = 1 if exists $defs{$2}; + $sect = $1; + next; + }; + /^\@c\s+man\s+begin\s+([A-Z]+)/ and $sect = $1, $output = 1, next; + /^\@c\s+man\s+end/ and do { + $sects{$sect} = "" unless exists $sects{$sect}; + $sects{$sect} .= postprocess($section); + $section = ""; + $output = 0; + next; + }; + + # handle variables + /^\@set\s+([a-zA-Z0-9_-]+)\s*(.*)$/ and do { + $defs{$1} = $2; + next; + }; + /^\@clear\s+([a-zA-Z0-9_-]+)/ and do { + delete $defs{$1}; + next; + }; + + next unless $output; + + # Discard comments. (Can't do it above, because then we'd never see + # @c man lines.) + /^\@c\b/ and next; + + # End-block handler goes up here because it needs to operate even + # if we are skipping. + /^\@end\s+([a-z]+)/ and do { + # Ignore @end foo, where foo is not an operation which may + # cause us to skip, if we are presently skipping. + my $ended = $1; + next if $skipping && $ended !~ /^(?:ifset|ifclear|ignore|menu|iftex)$/; + + die "\@end $ended without \@$ended at line $.\n" unless defined $endw; + die "\@$endw ended by \@end $ended at line $.\n" unless $ended eq $endw; + + $endw = pop @endwstack; + + if ($ended =~ /^(?:ifset|ifclear|ignore|menu|iftex)$/) { + $skipping = pop @skstack; + next; + } elsif ($ended =~ /^(?:example|smallexample|display)$/) { + $shift = ""; + $_ = ""; # need a paragraph break + } elsif ($ended =~ /^(?:itemize|enumerate|[fv]?table)$/) { + $_ = "\n=back\n"; + $ic = pop @icstack; + } else { + die "unknown command \@end $ended at line $.\n"; + } + }; + + # We must handle commands which can cause skipping even while we + # are skipping, otherwise we will not process nested conditionals + # correctly. + /^\@ifset\s+([a-zA-Z0-9_-]+)/ and do { + push @endwstack, $endw; + push @skstack, $skipping; + $endw = "ifset"; + $skipping = 1 unless exists $defs{$1}; + next; + }; + + /^\@ifclear\s+([a-zA-Z0-9_-]+)/ and do { + push @endwstack, $endw; + push @skstack, $skipping; + $endw = "ifclear"; + $skipping = 1 if exists $defs{$1}; + next; + }; + + /^\@(ignore|menu|iftex)\b/ and do { + push @endwstack, $endw; + push @skstack, $skipping; + $endw = $1; + $skipping = 1; + next; + }; + + next if $skipping; + + # Character entities. First the ones that can be replaced by raw text + # or discarded outright: + s/\@copyright\{\}/(c)/g; + s/\@dots\{\}/.../g; + s/\@enddots\{\}/..../g; + s/\@([.!? ])/$1/g; + s/\@[:-]//g; + s/\@bullet(?:\{\})?/*/g; + s/\@TeX\{\}/TeX/g; + s/\@pounds\{\}/\#/g; + s/\@minus(?:\{\})?/-/g; + s/\\,/,/g; + + # Now the ones that have to be replaced by special escapes + # (which will be turned back into text by unmunge()) + s/&/&/g; + s/\@\{/{/g; + s/\@\}/}/g; + s/\@\@/&at;/g; + + # Inside a verbatim block, handle @var specially. + if ($shift ne "") { + s/\@var\{([^\}]*)\}/<$1>/g; + } + + # POD doesn't interpret E<> inside a verbatim block. + if ($shift eq "") { + s//>/g; + } else { + s//>/g; + } + + # Single line command handlers. + + /^\@include\s+(.+)$/ and do { + push @instack, $inf; + $inf = gensym(); + + # Try cwd and $ibase. + open($inf, "<" . $1) + or open($inf, "<" . $ibase . "/" . $1) + or die "cannot open $1 or $ibase/$1: $!\n"; + next; + }; + + /^\@(?:section|unnumbered|unnumberedsec|center)\s+(.+)$/ + and $_ = "\n=head2 $1\n"; + /^\@subsection\s+(.+)$/ + and $_ = "\n=head3 $1\n"; + + # Block command handlers: + /^\@itemize\s+(\@[a-z]+|\*|-)/ and do { + push @endwstack, $endw; + push @icstack, $ic; + $ic = $1; + $_ = "\n=over 4\n"; + $endw = "itemize"; + }; + + /^\@enumerate(?:\s+([a-zA-Z0-9]+))?/ and do { + push @endwstack, $endw; + push @icstack, $ic; + if (defined $1) { + $ic = $1 . "."; + } else { + $ic = "1."; + } + $_ = "\n=over 4\n"; + $endw = "enumerate"; + }; + + /^\@([fv]?table)\s+(\@[a-z]+)/ and do { + push @endwstack, $endw; + push @icstack, $ic; + $endw = $1; + $ic = $2; + $ic =~ s/\@(?:samp|strong|key|gcctabopt|option|env)/B/; + $ic =~ s/\@(?:code|kbd)/C/; + $ic =~ s/\@(?:dfn|var|emph|cite|i)/I/; + $ic =~ s/\@(?:file)/F/; + $_ = "\n=over 4\n"; + }; + + /^\@((?:small)?example|display)/ and do { + push @endwstack, $endw; + $endw = $1; + $shift = "\t"; + $_ = ""; # need a paragraph break + }; + + /^\@itemx?\s*(.+)?$/ and do { + if (defined $1) { + # Entity escapes prevent munging by the <> processing below. + $_ = "\n=item $ic\<$1\>\n"; + } else { + $_ = "\n=item $ic\n"; + $ic =~ y/A-Ya-y/B-Zb-z/; + $ic =~ s/(\d+)/$1 + 1/eg; + } + }; + + $section .= $shift.$_."\n"; +} +# End of current file. +close($inf); +$inf = pop @instack; +} + +die "No filename or title\n" unless defined $fn && defined $tl; + +$sects{NAME} = "$fn \- $tl\n"; +$sects{FOOTNOTES} .= "=back\n" if exists $sects{FOOTNOTES}; + +for $sect (qw(NAME SYNOPSIS DESCRIPTION OPTIONS ENVIRONMENT FILES + BUGS NOTES FOOTNOTES SEEALSO AUTHOR COPYRIGHT)) { + if(exists $sects{$sect}) { + $head = $sect; + $head =~ s/SEEALSO/SEE ALSO/; + print "=head1 $head\n\n"; + print scalar unmunge ($sects{$sect}); + print "\n"; + } +} + +sub usage +{ + die "usage: $0 [-D toggle...] [infile [outfile]]\n"; +} + +sub postprocess +{ + local $_ = $_[0]; + + # @value{foo} is replaced by whatever 'foo' is defined as. + while (m/(\@value\{([a-zA-Z0-9_-]+)\})/g) { + if (! exists $defs{$2}) { + print STDERR "Option $2 not defined\n"; + s/\Q$1\E//; + } else { + $value = $defs{$2}; + s/\Q$1\E/$value/; + } + } + + # Formatting commands. + # Temporary escape for @r. + s/\@r\{([^\}]*)\}/R<$1>/g; + s/\@(?:dfn|var|emph|cite|i)\{([^\}]*)\}/I<$1>/g; + s/\@(?:code|kbd)\{([^\}]*)\}/C<$1>/g; + s/\@(?:gccoptlist|samp|strong|key|option|env|command|b)\{([^\}]*)\}/B<$1>/g; + s/\@sc\{([^\}]*)\}/\U$1/g; + s/\@file\{([^\}]*)\}/F<$1>/g; + s/\@w\{([^\}]*)\}/S<$1>/g; + s/\@(?:dmn|math)\{([^\}]*)\}/$1/g; + + # Cross references are thrown away, as are @noindent and @refill. + # (@noindent is impossible in .pod, and @refill is unnecessary.) + # @* is also impossible in .pod; we discard it and any newline that + # follows it. Similarly, our macro @gol must be discarded. + + s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g; + s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g; + s/;\s+\@pxref\{(?:[^\}]*)\}//g; + s/\@noindent\s*//g; + s/\@refill//g; + s/\@gol//g; + s/\@\*\s*\n?//g; + + # @uref can take one, two, or three arguments, with different + # semantics each time. @url and @email are just like @uref with + # one argument, for our purposes. + s/\@(?:uref|url|email)\{([^\},]*)\}/<B<$1>>/g; + s/\@uref\{([^\},]*),([^\},]*)\}/$2 (C<$1>)/g; + s/\@uref\{([^\},]*),([^\},]*),([^\},]*)\}/$3/g; + + # Turn B blah> into B I B to + # match Texinfo semantics of @emph inside @samp. Also handle @r + # inside bold. + s/<//g; + 1 while s/B<((?:[^<>]|I<[^<>]*>)*)R<([^>]*)>/B<$1>${2}B]*)I<([^>]+)>/B<$1>I<$2>B]*)B<([^>]+)>/I<$1>B<$2>I//g; + s/([BI])<(\s+)([^>]+)>/$2$1<$3>/g; + s/([BI])<([^>]+?)(\s+)>/$1<$2>$3/g; + + # Extract footnotes. This has to be done after all other + # processing because otherwise the regexp will choke on formatting + # inside @footnote. + while (/\@footnote/g) { + s/\@footnote\{([^\}]+)\}/[$fnno]/; + add_footnote($1, $fnno); + $fnno++; + } + + return $_; +} + +sub unmunge +{ + # Replace escaped symbols with their equivalents. + local $_ = $_[0]; + + s/</E/g; + s/>/E/g; + s/{/\{/g; + s/}/\}/g; + s/&at;/\@/g; + s/&/&/g; + return $_; +} + +sub add_footnote +{ + unless (exists $sects{FOOTNOTES}) { + $sects{FOOTNOTES} = "\n=over 4\n\n"; + } + + $sects{FOOTNOTES} .= "=item $fnno.\n\n"; $fnno++; + $sects{FOOTNOTES} .= $_[0]; + $sects{FOOTNOTES} .= "\n\n"; +} + +# stolen from Symbol.pm +{ + my $genseq = 0; + sub gensym + { + my $name = "GEN" . $genseq++; + my $ref = \*{$name}; + delete $::{$name}; + return $ref; + } +} diff --git a/05/tcc-final-old/win32/build-tcc.bat b/05/tcc-final-old/win32/build-tcc.bat new file mode 100755 index 0000000..913b068 --- /dev/null +++ b/05/tcc-final-old/win32/build-tcc.bat @@ -0,0 +1,189 @@ +@rem ------------------------------------------------------ +@rem batch file to build tcc using mingw, msvc or tcc itself +@rem ------------------------------------------------------ + +@echo off +setlocal +if (%1)==(-clean) goto :cleanup +set CC=gcc +set /p VERSION= < ..\VERSION +set INST= +set BIN= +set DOC=no +set EXES_ONLY=no +goto :a0 +:a2 +shift +:a3 +shift +:a0 +if not (%1)==(-c) goto :a1 +set CC=%~2 +if (%2)==(cl) set CC=@call :cl +goto :a2 +:a1 +if (%1)==(-t) set T=%2&& goto :a2 +if (%1)==(-v) set VERSION=%~2&& goto :a2 +if (%1)==(-i) set INST=%2&& goto :a2 +if (%1)==(-b) set BIN=%2&& goto :a2 +if (%1)==(-d) set DOC=yes&& goto :a3 +if (%1)==(-x) set EXES_ONLY=yes&& goto :a3 +if (%1)==() goto :p1 +:usage +echo usage: build-tcc.bat [ options ... ] +echo options: +echo -c prog use prog (gcc/tcc/cl) to compile tcc +echo -c "prog options" use prog with options to compile tcc +echo -t 32/64 force 32/64 bit default target +echo -v "version" set tcc version +echo -i tccdir install tcc into tccdir +echo -b bindir optionally install binaries into bindir elsewhere +echo -d create tcc-doc.html too (needs makeinfo) +echo -x just create the executables +echo -clean delete all previously produced files and directories +exit /B 1 + +@rem ------------------------------------------------------ +@rem sub-routines + +:cleanup +set LOG=echo +%LOG% removing files: +for %%f in (*tcc.exe libtcc.dll lib\*.a) do call :del_file %%f +for %%f in (..\config.h ..\config.texi) do call :del_file %%f +for %%f in (include\*.h) do @if exist ..\%%f call :del_file %%f +for %%f in (include\tcclib.h examples\libtcc_test.c) do call :del_file %%f +for %%f in (*.o *.obj *.def *.pdb *.lib *.exp *.ilk) do call :del_file %%f +%LOG% removing directories: +for %%f in (doc libtcc) do call :del_dir %%f +%LOG% done. +exit /B 0 +:del_file +if exist %1 del %1 && %LOG% %1 +exit /B 0 +:del_dir +if exist %1 rmdir /Q/S %1 && %LOG% %1 +exit /B 0 + +:cl +@echo off +set CMD=cl +:c0 +set ARG=%1 +set ARG=%ARG:.dll=.lib% +if (%1)==(-shared) set ARG=-LD +if (%1)==(-o) shift && set ARG=-Fe%2 +set CMD=%CMD% %ARG% +shift +if not (%1)==() goto :c0 +echo on +%CMD% -O1 -W2 -Zi -MT -GS- -nologo -link -opt:ref,icf +@exit /B %ERRORLEVEL% + +@rem ------------------------------------------------------ +@rem main program + +:p1 +if not %T%_==_ goto :p2 +set T=32 +if %PROCESSOR_ARCHITECTURE%_==AMD64_ set T=64 +if %PROCESSOR_ARCHITEW6432%_==AMD64_ set T=64 +:p2 +if "%CC:~-3%"=="gcc" set CC=%CC% -Os -s -static +set D32=-DTCC_TARGET_PE -DTCC_TARGET_I386 +set D64=-DTCC_TARGET_PE -DTCC_TARGET_X86_64 +set P32=i386-win32 +set P64=x86_64-win32 +if %T%==64 goto :t64 +set D=%D32% +set DX=%D64% +set PX=%P64% +goto :p3 +:t64 +set D=%D64% +set DX=%D32% +set PX=%P32% +goto :p3 + +:p3 +@echo on + +:config.h +echo>..\config.h #define TCC_VERSION "%VERSION%" +echo>> ..\config.h #ifdef TCC_TARGET_X86_64 +echo>> ..\config.h #define TCC_LIBTCC1 "libtcc1-64.a" +echo>> ..\config.h #else +echo>> ..\config.h #define TCC_LIBTCC1 "libtcc1-32.a" +echo>> ..\config.h #endif + +for %%f in (*tcc.exe *tcc.dll) do @del %%f + +:compiler +%CC% -o libtcc.dll -shared ..\libtcc.c %D% -DLIBTCC_AS_DLL +@if errorlevel 1 goto :the_end +%CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0" +%CC% -o %PX%-tcc.exe ..\tcc.c %DX% + +@if (%EXES_ONLY%)==(yes) goto :files-done + +if not exist libtcc mkdir libtcc +if not exist doc mkdir doc +copy>nul ..\include\*.h include +copy>nul ..\tcclib.h include +copy>nul ..\libtcc.h libtcc +copy>nul ..\tests\libtcc_test.c examples +copy>nul tcc-win32.txt doc + +.\tcc -impdef libtcc.dll -o libtcc\libtcc.def +@if errorlevel 1 goto :the_end + +:libtcc1.a +@set O1=libtcc1.o crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o chkstk.o bcheck.o +.\tcc -m32 -c ../lib/libtcc1.c +.\tcc -m32 -c lib/crt1.c +.\tcc -m32 -c lib/crt1w.c +.\tcc -m32 -c lib/wincrt1.c +.\tcc -m32 -c lib/wincrt1w.c +.\tcc -m32 -c lib/dllcrt1.c +.\tcc -m32 -c lib/dllmain.c +.\tcc -m32 -c lib/chkstk.S +.\tcc -m32 -w -c ../lib/bcheck.c +.\tcc -m32 -c ../lib/alloca86.S +.\tcc -m32 -c ../lib/alloca86-bt.S +.\tcc -m32 -ar lib/libtcc1-32.a %O1% alloca86.o alloca86-bt.o +@if errorlevel 1 goto :the_end +.\tcc -m64 -c ../lib/libtcc1.c +.\tcc -m64 -c lib/crt1.c +.\tcc -m64 -c lib/crt1w.c +.\tcc -m64 -c lib/wincrt1.c +.\tcc -m64 -c lib/wincrt1w.c +.\tcc -m64 -c lib/dllcrt1.c +.\tcc -m64 -c lib/dllmain.c +.\tcc -m64 -c lib/chkstk.S +.\tcc -m64 -w -c ../lib/bcheck.c +.\tcc -m64 -c ../lib/alloca86_64.S +.\tcc -m64 -c ../lib/alloca86_64-bt.S +.\tcc -m64 -ar lib/libtcc1-64.a %O1% alloca86_64.o alloca86_64-bt.o +@if errorlevel 1 goto :the_end + +:tcc-doc.html +@if not (%DOC%)==(yes) goto :doc-done +echo>..\config.texi @set VERSION %VERSION% +cmd /c makeinfo --html --no-split ../tcc-doc.texi -o doc/tcc-doc.html +:doc-done + +:files-done +for %%f in (*.o *.def) do @del %%f + +:copy-install +@if (%INST%)==() goto :the_end +if not exist %INST% mkdir %INST% +@if (%BIN%)==() set BIN=%INST% +if not exist %BIN% mkdir %BIN% +for %%f in (*tcc.exe *tcc.dll) do @copy>nul %%f %BIN%\%%f +@if not exist %INST%\lib mkdir %INST%\lib +for %%f in (lib\*.a lib\*.def) do @copy>nul %%f %INST%\%%f +for %%f in (include examples libtcc doc) do @xcopy>nul /s/i/q/y %%f %INST%\%%f + +:the_end +exit /B %ERRORLEVEL% diff --git a/05/tcc-final-old/win32/examples/dll.c b/05/tcc-final-old/win32/examples/dll.c new file mode 100644 index 0000000..052a056 --- /dev/null +++ b/05/tcc-final-old/win32/examples/dll.c @@ -0,0 +1,13 @@ +//+--------------------------------------------------------------------------- +// +// dll.c - Windows DLL example - dynamically linked part +// + +#include + +__declspec(dllexport) const char *hello_data = "(not set)"; + +__declspec(dllexport) void hello_func (void) +{ + MessageBox (0, hello_data, "From DLL", MB_ICONINFORMATION); +} diff --git a/05/tcc-final-old/win32/examples/fib.c b/05/tcc-final-old/win32/examples/fib.c new file mode 100644 index 0000000..8da26bc --- /dev/null +++ b/05/tcc-final-old/win32/examples/fib.c @@ -0,0 +1,24 @@ +#include +#include // atoi() + +int fib(n) +{ + if (n <= 2) + return 1; + else + return fib(n-1) + fib(n-2); +} + +int main(int argc, char **argv) +{ + int n; + if (argc < 2) { + printf("usage: fib n\n" + "Compute nth Fibonacci number\n"); + return 1; + } + + n = atoi(argv[1]); + printf("fib(%d) = %d\n", n, fib(n)); + return 0; +} diff --git a/05/tcc-final-old/win32/examples/hello_dll.c b/05/tcc-final-old/win32/examples/hello_dll.c new file mode 100644 index 0000000..4813c5b --- /dev/null +++ b/05/tcc-final-old/win32/examples/hello_dll.c @@ -0,0 +1,20 @@ +//+--------------------------------------------------------------------------- +// +// HELLO_DLL.C - Windows DLL example - main application part +// + +#include + +void hello_func (void); +__declspec(dllimport) extern const char *hello_data; + +int WINAPI WinMain( + HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow) +{ + hello_data = "Hello World!"; + hello_func(); + return 0; +} diff --git a/05/tcc-final-old/win32/examples/hello_win.c b/05/tcc-final-old/win32/examples/hello_win.c new file mode 100644 index 0000000..96546e4 --- /dev/null +++ b/05/tcc-final-old/win32/examples/hello_win.c @@ -0,0 +1,163 @@ +//+--------------------------------------------------------------------------- +// +// HELLO_WIN.C - Windows GUI 'Hello World!' Example +// +//+--------------------------------------------------------------------------- + +#include + +#define APPNAME "HELLO_WIN" + +char szAppName[] = APPNAME; // The name of this application +char szTitle[] = APPNAME; // The title bar text +const char *pWindowText; + +void CenterWindow(HWND hWnd); + +//+--------------------------------------------------------------------------- +// +// Function: WndProc +// +// Synopsis: very unusual type of function - gets called by system to +// process windows messages. +// +// Arguments: same as always. +//---------------------------------------------------------------------------- + +LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch (message) { + + // ----------------------- first and last + case WM_CREATE: + CenterWindow(hwnd); + break; + + case WM_DESTROY: + PostQuitMessage(0); + break; + + // ----------------------- get out of it... + case WM_RBUTTONUP: + DestroyWindow(hwnd); + break; + + case WM_KEYDOWN: + if (VK_ESCAPE == wParam) + DestroyWindow(hwnd); + break; + + // ----------------------- display our minimal info + case WM_PAINT: + { + PAINTSTRUCT ps; + HDC hdc; + RECT rc; + hdc = BeginPaint(hwnd, &ps); + + GetClientRect(hwnd, &rc); + SetTextColor(hdc, RGB(240,240,96)); + SetBkMode(hdc, TRANSPARENT); + DrawText(hdc, pWindowText, -1, &rc, DT_CENTER|DT_SINGLELINE|DT_VCENTER); + + EndPaint(hwnd, &ps); + break; + } + + // ----------------------- let windows do all other stuff + default: + return DefWindowProc(hwnd, message, wParam, lParam); + } + return 0; +} + +//+--------------------------------------------------------------------------- +// +// Function: WinMain +// +// Synopsis: standard entrypoint for GUI Win32 apps +// +//---------------------------------------------------------------------------- +int APIENTRY WinMain( + HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow + ) +{ + MSG msg; + WNDCLASS wc; + HWND hwnd; + + pWindowText = lpCmdLine[0] ? lpCmdLine : "Hello Windows!"; + + // Fill in window class structure with parameters that describe + // the main window. + + ZeroMemory(&wc, sizeof wc); + wc.hInstance = hInstance; + wc.lpszClassName = szAppName; + wc.lpfnWndProc = (WNDPROC)WndProc; + wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW; + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + + if (FALSE == RegisterClass(&wc)) + return 0; + + // create the browser + hwnd = CreateWindow( + szAppName, + szTitle, + WS_OVERLAPPEDWINDOW|WS_VISIBLE, + CW_USEDEFAULT, + CW_USEDEFAULT, + 360,//CW_USEDEFAULT, + 240,//CW_USEDEFAULT, + 0, + 0, + hInstance, + 0); + + if (NULL == hwnd) + return 0; + + // Main message loop: + while (GetMessage(&msg, NULL, 0, 0) > 0) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + return msg.wParam; +} + +//+--------------------------------------------------------------------------- + +//+--------------------------------------------------------------------------- + +void CenterWindow(HWND hwnd_self) +{ + HWND hwnd_parent; + RECT rw_self, rc_parent, rw_parent; + int xpos, ypos; + + hwnd_parent = GetParent(hwnd_self); + if (NULL == hwnd_parent) + hwnd_parent = GetDesktopWindow(); + + GetWindowRect(hwnd_parent, &rw_parent); + GetClientRect(hwnd_parent, &rc_parent); + GetWindowRect(hwnd_self, &rw_self); + + xpos = rw_parent.left + (rc_parent.right + rw_self.left - rw_self.right) / 2; + ypos = rw_parent.top + (rc_parent.bottom + rw_self.top - rw_self.bottom) / 2; + + SetWindowPos( + hwnd_self, NULL, + xpos, ypos, 0, 0, + SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE + ); +} + +//+--------------------------------------------------------------------------- diff --git a/05/tcc-final-old/win32/include/_mingw.h b/05/tcc-final-old/win32/include/_mingw.h new file mode 100644 index 0000000..2fc9798 --- /dev/null +++ b/05/tcc-final-old/win32/include/_mingw.h @@ -0,0 +1,170 @@ +/* + * _mingw.h + * + * This file is for TinyCC and not part of the Mingw32 package. + * + * THIS SOFTWARE IS NOT COPYRIGHTED + * + * This source code is offered for use in the public domain. You may + * use, modify or distribute it freely. + * + * This code is distributed in the hope that it will be useful but + * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY + * DISCLAIMED. This includes but is not limited to warranties of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + */ + +#ifndef __MINGW_H +#define __MINGW_H + +/* some winapi files define these before including _mingw.h --> */ +#undef __cdecl +#undef _X86_ +#undef WIN32 +/* <-- */ + +#include +#include + +#define __int8 char +#define __int16 short +#define __int32 int +#define __int64 long long +#define _HAVE_INT64 + +#define __cdecl +#define __declspec(x) __attribute__((x)) +#define __unaligned __attribute__((packed)) +#define __fastcall __attribute__((fastcall)) + +#define __MSVCRT__ 1 +#undef _MSVCRT_ +#define __MINGW_IMPORT extern __declspec(dllimport) +#define __MINGW_ATTRIB_NORETURN +#define __MINGW_ATTRIB_CONST +#define __MINGW_ATTRIB_DEPRECATED +#define __MINGW_ATTRIB_MALLOC +#define __MINGW_ATTRIB_PURE +#define __MINGW_ATTRIB_NONNULL(arg) +#define __MINGW_NOTHROW +#define __GNUC_VA_LIST + +#define _CRTIMP extern +#define __CRT_INLINE extern __inline__ + +#define _CRT_ALIGN(x) __attribute__((aligned(x))) +#define DECLSPEC_ALIGN(x) __attribute__((aligned(x))) +#define _CRT_PACKING 8 +#define __CRT_UNALIGNED +#define _CONST_RETURN + +#ifndef _TRUNCATE +#define _TRUNCATE ((size_t)-1) +#endif + +#define __CRT_STRINGIZE(_Value) #_Value +#define _CRT_STRINGIZE(_Value) __CRT_STRINGIZE(_Value) +#define __CRT_WIDE(_String) L ## _String +#define _CRT_WIDE(_String) __CRT_WIDE(_String) + +#ifdef _WIN64 +#define __stdcall +#define _AMD64_ 1 +#define __x86_64 1 +#define _M_X64 100 /* Visual Studio */ +#define _M_AMD64 100 /* Visual Studio */ +#define USE_MINGW_SETJMP_TWO_ARGS +#define mingw_getsp tinyc_getbp +#define __TRY__ +#else +#define __stdcall __attribute__((__stdcall__)) +#define _X86_ 1 +#define _M_IX86 300 /* Visual Studio */ +#define WIN32 1 +#define _USE_32BIT_TIME_T +#ifdef __arm__ +#define __TRY__ +#else +#define __TRY__ void __try__(void**), *_sehrec[6]; __try__(_sehrec); +#endif +#endif + +/* in stddef.h */ +#define _SIZE_T_DEFINED +#define _SSIZE_T_DEFINED +#define _PTRDIFF_T_DEFINED +#define _WCHAR_T_DEFINED +#define _UINTPTR_T_DEFINED +#define _INTPTR_T_DEFINED +#define _INTEGRAL_MAX_BITS 64 + +#ifndef _TIME32_T_DEFINED +#define _TIME32_T_DEFINED +typedef long __time32_t; +#endif + +#ifndef _TIME64_T_DEFINED +#define _TIME64_T_DEFINED +typedef long long __time64_t; +#endif + +#ifndef _TIME_T_DEFINED +#define _TIME_T_DEFINED +#ifdef _USE_32BIT_TIME_T +typedef __time32_t time_t; +#else +typedef __time64_t time_t; +#endif +#endif + +#ifndef _WCTYPE_T_DEFINED +#define _WCTYPE_T_DEFINED +typedef wchar_t wctype_t; +#endif + +#ifndef _WINT_T +#define _WINT_T +typedef __WINT_TYPE__ wint_t; +#endif + +typedef int errno_t; +#define _ERRCODE_DEFINED + +typedef struct threadlocaleinfostruct *pthreadlocinfo; +typedef struct threadmbcinfostruct *pthreadmbcinfo; +typedef struct localeinfo_struct _locale_tstruct,*_locale_t; + +/* for winapi */ +#define _ANONYMOUS_UNION +#define _ANONYMOUS_STRUCT +#define DECLSPEC_NORETURN +#define DECLARE_STDCALL_P(type) __stdcall type +#define NOSERVICE 1 +#define NOMCX 1 +#define NOIME 1 +#define __INTRIN_H_ +#ifndef DUMMYUNIONNAME +# define DUMMYUNIONNAME +# define DUMMYUNIONNAME1 +# define DUMMYUNIONNAME2 +# define DUMMYUNIONNAME3 +# define DUMMYUNIONNAME4 +# define DUMMYUNIONNAME5 +#endif +#ifndef DUMMYSTRUCTNAME +# define DUMMYSTRUCTNAME +#endif +#ifndef WINVER +# define WINVER 0x0502 +#endif +#ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x502 +#endif + +#define __C89_NAMELESS +#define __MINGW_EXTENSION +#define WINAPI_FAMILY_PARTITION(X) 1 +#define MINGW_HAS_SECURE_API + +#endif /* __MINGW_H */ diff --git a/05/tcc-final-old/win32/include/assert.h b/05/tcc-final-old/win32/include/assert.h new file mode 100644 index 0000000..466d457 --- /dev/null +++ b/05/tcc-final-old/win32/include/assert.h @@ -0,0 +1,57 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef __ASSERT_H_ +#define __ASSERT_H_ + +#include <_mingw.h> +#ifdef __cplusplus +#include +#endif + +#ifdef NDEBUG +#ifndef assert +#define assert(_Expression) ((void)0) +#endif +#else + +#ifndef _CRT_TERMINATE_DEFINED +#define _CRT_TERMINATE_DEFINED + void __cdecl __MINGW_NOTHROW exit(int _Code) __MINGW_ATTRIB_NORETURN; + _CRTIMP void __cdecl __MINGW_NOTHROW _exit(int _Code) __MINGW_ATTRIB_NORETURN; +#if !defined __NO_ISOCEXT /* extern stub in static libmingwex.a */ +/* C99 function name */ +void __cdecl _Exit(int) __MINGW_ATTRIB_NORETURN; +__CRT_INLINE __MINGW_ATTRIB_NORETURN void __cdecl _Exit(int status) +{ _exit(status); } +#endif + +#pragma push_macro("abort") +#undef abort + void __cdecl __declspec(noreturn) abort(void); +#pragma pop_macro("abort") + +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + +extern void __cdecl _wassert(const wchar_t *_Message,const wchar_t *_File,unsigned _Line); +extern void __cdecl _assert(const char *, const char *, unsigned); + +#ifdef __cplusplus +} +#endif + +#ifndef assert +//#define assert(_Expression) (void)((!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression),_CRT_WIDE(__FILE__),__LINE__),0)) +#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__)) +#endif + +#endif + +#endif diff --git a/05/tcc-final-old/win32/include/conio.h b/05/tcc-final-old/win32/include/conio.h new file mode 100644 index 0000000..39f779e --- /dev/null +++ b/05/tcc-final-old/win32/include/conio.h @@ -0,0 +1,409 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_CONIO +#define _INC_CONIO + +#include <_mingw.h> + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP char *_cgets(char *_Buffer); + _CRTIMP int __cdecl _cprintf(const char *_Format,...); + _CRTIMP int __cdecl _cputs(const char *_Str); + _CRTIMP int __cdecl _cscanf(const char *_Format,...); + _CRTIMP int __cdecl _cscanf_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _getch(void); + _CRTIMP int __cdecl _getche(void); + _CRTIMP int __cdecl _vcprintf(const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cprintf_p(const char *_Format,...); + _CRTIMP int __cdecl _vcprintf_p(const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cprintf_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcprintf_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _cprintf_p_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcprintf_p_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _kbhit(void); + +#if defined(_X86_) && !defined(__x86_64) + int __cdecl _inp(unsigned short); + unsigned short __cdecl _inpw(unsigned short); + unsigned long __cdecl _inpd(unsigned short); + int __cdecl _outp(unsigned short,int); + unsigned short __cdecl _outpw(unsigned short,unsigned short); + unsigned long __cdecl _outpd(unsigned short,unsigned long); +#endif + + _CRTIMP int __cdecl _putch(int _Ch); + _CRTIMP int __cdecl _ungetch(int _Ch); + _CRTIMP int __cdecl _getch_nolock(void); + _CRTIMP int __cdecl _getche_nolock(void); + _CRTIMP int __cdecl _putch_nolock(int _Ch); + _CRTIMP int __cdecl _ungetch_nolock(int _Ch); + +#ifndef _WCONIO_DEFINED +#define _WCONIO_DEFINED + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + + _CRTIMP wchar_t *_cgetws(wchar_t *_Buffer); + _CRTIMP wint_t __cdecl _getwch(void); + _CRTIMP wint_t __cdecl _getwche(void); + _CRTIMP wint_t __cdecl _putwch(wchar_t _WCh); + _CRTIMP wint_t __cdecl _ungetwch(wint_t _WCh); + _CRTIMP int __cdecl _cputws(const wchar_t *_String); + _CRTIMP int __cdecl _cwprintf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_p(const wchar_t *_Format,...); + _CRTIMP int __cdecl _vcwprintf_p(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP wint_t __cdecl _putwch_nolock(wchar_t _WCh); + _CRTIMP wint_t __cdecl _getwch_nolock(void); + _CRTIMP wint_t __cdecl _getwche_nolock(void); + _CRTIMP wint_t __cdecl _ungetwch_nolock(wint_t _WCh); +#endif + +#ifndef NO_OLDNAMES + char *__cdecl cgets(char *_Buffer); + int __cdecl cprintf(const char *_Format,...); + int __cdecl cputs(const char *_Str); + int __cdecl cscanf(const char *_Format,...); + int __cdecl getch(void); + int __cdecl getche(void); + int __cdecl kbhit(void); + int __cdecl putch(int _Ch); + int __cdecl ungetch(int _Ch); + +#if (defined(_X86_) && !defined(__x86_64)) + int __cdecl inp(unsigned short); + unsigned short __cdecl inpw(unsigned short); + int __cdecl outp(unsigned short,int); + unsigned short __cdecl outpw(unsigned short,unsigned short); +#endif + + /* I/O intrin functions. */ + __CRT_INLINE unsigned char __inbyte(unsigned short Port) + { + unsigned char value; + __asm__ __volatile__ ("inb %w1,%b0" + : "=a" (value) + : "Nd" (Port)); + return value; + } + __CRT_INLINE unsigned short __inword(unsigned short Port) + { + unsigned short value; + __asm__ __volatile__ ("inw %w1,%w0" + : "=a" (value) + : "Nd" (Port)); + return value; + } + __CRT_INLINE unsigned long __indword(unsigned short Port) + { + unsigned long value; + __asm__ __volatile__ ("inl %w1,%0" + : "=a" (value) + : "Nd" (Port)); + return value; + } + __CRT_INLINE void __outbyte(unsigned short Port,unsigned char Data) + { + __asm__ __volatile__ ("outb %b0,%w1" + : + : "a" (Data), "Nd" (Port)); + } + __CRT_INLINE void __outword(unsigned short Port,unsigned short Data) + { + __asm__ __volatile__ ("outw %w0,%w1" + : + : "a" (Data), "Nd" (Port)); + } + __CRT_INLINE void __outdword(unsigned short Port,unsigned long Data) + { + __asm__ __volatile__ ("outl %0,%w1" + : + : "a" (Data), "Nd" (Port)); + } + __CRT_INLINE void __inbytestring(unsigned short Port,unsigned char *Buffer,unsigned long Count) + { + __asm__ __volatile__ ( + "cld ; rep ; insb " + : "=D" (Buffer), "=c" (Count) + : "d"(Port), "0"(Buffer), "1" (Count) + ); + } + __CRT_INLINE void __inwordstring(unsigned short Port,unsigned short *Buffer,unsigned long Count) + { + __asm__ __volatile__ ( + "cld ; rep ; insw " + : "=D" (Buffer), "=c" (Count) + : "d"(Port), "0"(Buffer), "1" (Count) + ); + } + __CRT_INLINE void __indwordstring(unsigned short Port,unsigned long *Buffer,unsigned long Count) + { + __asm__ __volatile__ ( + "cld ; rep ; insl " + : "=D" (Buffer), "=c" (Count) + : "d"(Port), "0"(Buffer), "1" (Count) + ); + } + + __CRT_INLINE void __outbytestring(unsigned short Port,unsigned char *Buffer,unsigned long Count) + { + __asm__ __volatile__ ( + "cld ; rep ; outsb " + : "=S" (Buffer), "=c" (Count) + : "d"(Port), "0"(Buffer), "1" (Count) + ); + } + __CRT_INLINE void __outwordstring(unsigned short Port,unsigned short *Buffer,unsigned long Count) + { + __asm__ __volatile__ ( + "cld ; rep ; outsw " + : "=S" (Buffer), "=c" (Count) + : "d"(Port), "0"(Buffer), "1" (Count) + ); + } + __CRT_INLINE void __outdwordstring(unsigned short Port,unsigned long *Buffer,unsigned long Count) + { + __asm__ __volatile__ ( + "cld ; rep ; outsl " + : "=S" (Buffer), "=c" (Count) + : "d"(Port), "0"(Buffer), "1" (Count) + ); + } + + __CRT_INLINE unsigned __int64 __readcr0(void) + { + unsigned __int64 value; + __asm__ __volatile__ ( + "mov %%cr0, %[value]" + : [value] "=q" (value)); + return value; + } + + /* Register sizes are different between 32/64 bit mode. So we have to do this for _WIN64 and _WIN32 + separately. */ + +#ifdef _WIN64 + __CRT_INLINE void __writecr0(unsigned __int64 Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr0" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned __int64 __readcr2(void) + { + unsigned __int64 value; + __asm__ __volatile__ ( + "mov %%cr2, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr2(unsigned __int64 Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr2" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned __int64 __readcr3(void) + { + unsigned __int64 value; + __asm__ __volatile__ ( + "mov %%cr3, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr3(unsigned __int64 Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr3" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned __int64 __readcr4(void) + { + unsigned __int64 value; + __asm__ __volatile__ ( + "mov %%cr4, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr4(unsigned __int64 Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr4" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned __int64 __readcr8(void) + { + unsigned __int64 value; + __asm__ __volatile__ ( + "mov %%cr8, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr8(unsigned __int64 Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr8" + : + : [Data] "q" (Data) + : "memory"); + } + +#elif defined(_WIN32) + + __CRT_INLINE void __writecr0(unsigned Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr0" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned long __readcr2(void) + { + unsigned long value; + __asm__ __volatile__ ( + "mov %%cr2, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr2(unsigned Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr2" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned long __readcr3(void) + { + unsigned long value; + __asm__ __volatile__ ( + "mov %%cr3, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr3(unsigned Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr3" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned long __readcr4(void) + { + unsigned long value; + __asm__ __volatile__ ( + "mov %%cr4, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr4(unsigned Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr4" + : + : [Data] "q" (Data) + : "memory"); + } + + __CRT_INLINE unsigned long __readcr8(void) + { + unsigned long value; __asm__ __volatile__ ( + "mov %%cr8, %[value]" + : [value] "=q" (value)); + return value; + } + + __CRT_INLINE void __writecr8(unsigned Data) + { + __asm__ __volatile__ ( + "mov %[Data], %%cr8" + : + : [Data] "q" (Data) + : "memory"); + } + +#endif + + __CRT_INLINE unsigned __int64 __readmsr(unsigned long msr) + { + unsigned __int64 val1, val2; + __asm__ __volatile__( + "rdmsr" + : "=a" (val1), "=d" (val2) + : "c" (msr)); + return val1 | (val2 << 32); + } + + __CRT_INLINE void __writemsr (unsigned long msr, unsigned __int64 Value) + { + unsigned long val1 = Value, val2 = Value >> 32; + __asm__ __volatile__ ( + "wrmsr" + : + : "c" (msr), "a" (val1), "d" (val2)); + } + + __CRT_INLINE unsigned __int64 __rdtsc(void) + { + unsigned __int64 val1, val2; + __asm__ __volatile__ ( + "rdtsc" + : "=a" (val1), "=d" (val2)); + return val1 | (val2 << 32); + } + + __CRT_INLINE void __cpuid(int CPUInfo[4], int InfoType) + { + __asm__ __volatile__ ( + "cpuid" + : "=a" (CPUInfo [0]), "=b" (CPUInfo [1]), "=c" (CPUInfo [2]), "=d" (CPUInfo [3]) + : "a" (InfoType)); + } + +#endif + +#ifdef __cplusplus +} +#endif + +#include + +#endif diff --git a/05/tcc-final-old/win32/include/ctype.h b/05/tcc-final-old/win32/include/ctype.h new file mode 100644 index 0000000..7e90100 --- /dev/null +++ b/05/tcc-final-old/win32/include/ctype.h @@ -0,0 +1,281 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_CTYPE +#define _INC_CTYPE + +#include <_mingw.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + +#ifndef _CRT_CTYPEDATA_DEFINED +#define _CRT_CTYPEDATA_DEFINED +#ifndef _CTYPE_DISABLE_MACROS + +#ifndef __PCTYPE_FUNC +#define __PCTYPE_FUNC __pctype_func() +#ifdef _MSVCRT_ +#define __pctype_func() (_pctype) +#else +#define __pctype_func() (*_imp___pctype) +#endif +#endif + +#ifndef _pctype +#ifdef _MSVCRT_ + extern unsigned short *_pctype; +#else + extern unsigned short **_imp___pctype; +#define _pctype (*_imp___pctype) +#endif +#endif + +#endif +#endif + +#ifndef _CRT_WCTYPEDATA_DEFINED +#define _CRT_WCTYPEDATA_DEFINED +#ifndef _CTYPE_DISABLE_MACROS +#ifndef _wctype +#ifdef _MSVCRT_ + extern unsigned short *_wctype; +#else + extern unsigned short **_imp___wctype; +#define _wctype (*_imp___wctype) +#endif +#endif +#ifdef _MSVCRT_ +#define __pwctype_func() (_pwctype) +#ifndef _pwctype + extern unsigned short *_pwctype; +#endif +#else +#define __pwctype_func() (*_imp___pwctype) +#ifndef _pwctype + extern unsigned short **_imp___pwctype; +#define _pwctype (*_imp___pwctype) +#endif +#endif +#endif +#endif + + /* CRT stuff */ +#if 1 + extern const unsigned char __newclmap[]; + extern const unsigned char __newcumap[]; + extern pthreadlocinfo __ptlocinfo; + extern pthreadmbcinfo __ptmbcinfo; + extern int __globallocalestatus; + extern int __locale_changed; + extern struct threadlocaleinfostruct __initiallocinfo; + extern _locale_tstruct __initiallocalestructinfo; + pthreadlocinfo __cdecl __updatetlocinfo(void); + pthreadmbcinfo __cdecl __updatetmbcinfo(void); +#endif + +#define _UPPER 0x1 +#define _LOWER 0x2 +#define _DIGIT 0x4 +#define _SPACE 0x8 + +#define _PUNCT 0x10 +#define _CONTROL 0x20 +#define _BLANK 0x40 +#define _HEX 0x80 + +#define _LEADBYTE 0x8000 +#define _ALPHA (0x0100|_UPPER|_LOWER) + +#ifndef _CTYPE_DEFINED +#define _CTYPE_DEFINED + + _CRTIMP int __cdecl _isctype(int _C,int _Type); + _CRTIMP int __cdecl _isctype_l(int _C,int _Type,_locale_t _Locale); + _CRTIMP int __cdecl isalpha(int _C); + _CRTIMP int __cdecl _isalpha_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isupper(int _C); + _CRTIMP int __cdecl _isupper_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl islower(int _C); + _CRTIMP int __cdecl _islower_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isdigit(int _C); + _CRTIMP int __cdecl _isdigit_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isxdigit(int _C); + _CRTIMP int __cdecl _isxdigit_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isspace(int _C); + _CRTIMP int __cdecl _isspace_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl ispunct(int _C); + _CRTIMP int __cdecl _ispunct_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isalnum(int _C); + _CRTIMP int __cdecl _isalnum_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isprint(int _C); + _CRTIMP int __cdecl _isprint_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl isgraph(int _C); + _CRTIMP int __cdecl _isgraph_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl iscntrl(int _C); + _CRTIMP int __cdecl _iscntrl_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl toupper(int _C); + _CRTIMP int __cdecl tolower(int _C); + _CRTIMP int __cdecl _tolower(int _C); + _CRTIMP int __cdecl _tolower_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl _toupper(int _C); + _CRTIMP int __cdecl _toupper_l(int _C,_locale_t _Locale); + _CRTIMP int __cdecl __isascii(int _C); + _CRTIMP int __cdecl __toascii(int _C); + _CRTIMP int __cdecl __iscsymf(int _C); + _CRTIMP int __cdecl __iscsym(int _C); + +#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || !defined (NO_OLDNAMES) +int __cdecl isblank(int _C); +#endif +#endif + +#ifndef _WCTYPE_DEFINED +#define _WCTYPE_DEFINED + + int __cdecl iswalpha(wint_t _C); + _CRTIMP int __cdecl _iswalpha_l(wint_t _C,_locale_t _Locale); + int __cdecl iswupper(wint_t _C); + _CRTIMP int __cdecl _iswupper_l(wint_t _C,_locale_t _Locale); + int __cdecl iswlower(wint_t _C); + _CRTIMP int __cdecl _iswlower_l(wint_t _C,_locale_t _Locale); + int __cdecl iswdigit(wint_t _C); + _CRTIMP int __cdecl _iswdigit_l(wint_t _C,_locale_t _Locale); + int __cdecl iswxdigit(wint_t _C); + _CRTIMP int __cdecl _iswxdigit_l(wint_t _C,_locale_t _Locale); + int __cdecl iswspace(wint_t _C); + _CRTIMP int __cdecl _iswspace_l(wint_t _C,_locale_t _Locale); + int __cdecl iswpunct(wint_t _C); + _CRTIMP int __cdecl _iswpunct_l(wint_t _C,_locale_t _Locale); + int __cdecl iswalnum(wint_t _C); + _CRTIMP int __cdecl _iswalnum_l(wint_t _C,_locale_t _Locale); + int __cdecl iswprint(wint_t _C); + _CRTIMP int __cdecl _iswprint_l(wint_t _C,_locale_t _Locale); + int __cdecl iswgraph(wint_t _C); + _CRTIMP int __cdecl _iswgraph_l(wint_t _C,_locale_t _Locale); + int __cdecl iswcntrl(wint_t _C); + _CRTIMP int __cdecl _iswcntrl_l(wint_t _C,_locale_t _Locale); + int __cdecl iswascii(wint_t _C); + int __cdecl isleadbyte(int _C); + _CRTIMP int __cdecl _isleadbyte_l(int _C,_locale_t _Locale); + wint_t __cdecl towupper(wint_t _C); + _CRTIMP wint_t __cdecl _towupper_l(wint_t _C,_locale_t _Locale); + wint_t __cdecl towlower(wint_t _C); + _CRTIMP wint_t __cdecl _towlower_l(wint_t _C,_locale_t _Locale); + int __cdecl iswctype(wint_t _C,wctype_t _Type); + _CRTIMP int __cdecl _iswctype_l(wint_t _C,wctype_t _Type,_locale_t _Locale); + _CRTIMP int __cdecl __iswcsymf(wint_t _C); + _CRTIMP int __cdecl _iswcsymf_l(wint_t _C,_locale_t _Locale); + _CRTIMP int __cdecl __iswcsym(wint_t _C); + _CRTIMP int __cdecl _iswcsym_l(wint_t _C,_locale_t _Locale); + int __cdecl is_wctype(wint_t _C,wctype_t _Type); + +#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || !defined (NO_OLDNAMES) +int __cdecl iswblank(wint_t _C); +#endif +#endif + +#ifndef _CTYPE_DISABLE_MACROS + +#ifndef MB_CUR_MAX +#define MB_CUR_MAX ___mb_cur_max_func() +#ifndef __mb_cur_max +#ifdef _MSVCRT_ + extern int __mb_cur_max; +#else +#define __mb_cur_max (*_imp____mb_cur_max) + extern int *_imp____mb_cur_max; +#endif +#endif +#ifdef _MSVCRT_ +#define ___mb_cur_max_func() (__mb_cur_max) +#else +#define ___mb_cur_max_func() (*_imp____mb_cur_max) +#endif +#endif + +#define __chvalidchk(a,b) (__PCTYPE_FUNC[(a)] & (b)) +#define _chvalidchk_l(_Char,_Flag,_Locale) (!_Locale ? __chvalidchk(_Char,_Flag) : ((_locale_t)_Locale)->locinfo->pctype[_Char] & (_Flag)) +#define _ischartype_l(_Char,_Flag,_Locale) (((_Locale)!=NULL && (((_locale_t)(_Locale))->locinfo->mb_cur_max) > 1) ? _isctype_l(_Char,(_Flag),_Locale) : _chvalidchk_l(_Char,_Flag,_Locale)) +#define _isalpha_l(_Char,_Locale) _ischartype_l(_Char,_ALPHA,_Locale) +#define _isupper_l(_Char,_Locale) _ischartype_l(_Char,_UPPER,_Locale) +#define _islower_l(_Char,_Locale) _ischartype_l(_Char,_LOWER,_Locale) +#define _isdigit_l(_Char,_Locale) _ischartype_l(_Char,_DIGIT,_Locale) +#define _isxdigit_l(_Char,_Locale) _ischartype_l(_Char,_HEX,_Locale) +#define _isspace_l(_Char,_Locale) _ischartype_l(_Char,_SPACE,_Locale) +#define _ispunct_l(_Char,_Locale) _ischartype_l(_Char,_PUNCT,_Locale) +#define _isalnum_l(_Char,_Locale) _ischartype_l(_Char,_ALPHA|_DIGIT,_Locale) +#define _isprint_l(_Char,_Locale) _ischartype_l(_Char,_BLANK|_PUNCT|_ALPHA|_DIGIT,_Locale) +#define _isgraph_l(_Char,_Locale) _ischartype_l(_Char,_PUNCT|_ALPHA|_DIGIT,_Locale) +#define _iscntrl_l(_Char,_Locale) _ischartype_l(_Char,_CONTROL,_Locale) +#define _tolower(_Char) ((_Char)-'A'+'a') +#define _toupper(_Char) ((_Char)-'a'+'A') +#define __isascii(_Char) ((unsigned)(_Char) < 0x80) +#define __toascii(_Char) ((_Char) & 0x7f) + +#ifndef _WCTYPE_INLINE_DEFINED +#define _WCTYPE_INLINE_DEFINED + +#undef _CRT_WCTYPE_NOINLINE +#ifndef __cplusplus +#define iswalpha(_c) (iswctype(_c,_ALPHA)) +#define iswupper(_c) (iswctype(_c,_UPPER)) +#define iswlower(_c) (iswctype(_c,_LOWER)) +#define iswdigit(_c) (iswctype(_c,_DIGIT)) +#define iswxdigit(_c) (iswctype(_c,_HEX)) +#define iswspace(_c) (iswctype(_c,_SPACE)) +#define iswpunct(_c) (iswctype(_c,_PUNCT)) +#define iswalnum(_c) (iswctype(_c,_ALPHA|_DIGIT)) +#define iswprint(_c) (iswctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT)) +#define iswgraph(_c) (iswctype(_c,_PUNCT|_ALPHA|_DIGIT)) +#define iswcntrl(_c) (iswctype(_c,_CONTROL)) +#define iswascii(_c) ((unsigned)(_c) < 0x80) +#define _iswalpha_l(_c,_p) (_iswctype_l(_c,_ALPHA,_p)) +#define _iswupper_l(_c,_p) (_iswctype_l(_c,_UPPER,_p)) +#define _iswlower_l(_c,_p) (_iswctype_l(_c,_LOWER,_p)) +#define _iswdigit_l(_c,_p) (_iswctype_l(_c,_DIGIT,_p)) +#define _iswxdigit_l(_c,_p) (_iswctype_l(_c,_HEX,_p)) +#define _iswspace_l(_c,_p) (_iswctype_l(_c,_SPACE,_p)) +#define _iswpunct_l(_c,_p) (_iswctype_l(_c,_PUNCT,_p)) +#define _iswalnum_l(_c,_p) (_iswctype_l(_c,_ALPHA|_DIGIT,_p)) +#define _iswprint_l(_c,_p) (_iswctype_l(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT,_p)) +#define _iswgraph_l(_c,_p) (_iswctype_l(_c,_PUNCT|_ALPHA|_DIGIT,_p)) +#define _iswcntrl_l(_c,_p) (_iswctype_l(_c,_CONTROL,_p)) +#endif +#endif + +#define __iscsymf(_c) (isalpha(_c) || ((_c)=='_')) +#define __iscsym(_c) (isalnum(_c) || ((_c)=='_')) +#define __iswcsymf(_c) (iswalpha(_c) || ((_c)=='_')) +#define __iswcsym(_c) (iswalnum(_c) || ((_c)=='_')) +#define _iscsymf_l(_c,_p) (_isalpha_l(_c,_p) || ((_c)=='_')) +#define _iscsym_l(_c,_p) (_isalnum_l(_c,_p) || ((_c)=='_')) +#define _iswcsymf_l(_c,_p) (_iswalpha_l(_c,_p) || ((_c)=='_')) +#define _iswcsym_l(_c,_p) (_iswalnum_l(_c,_p) || ((_c)=='_')) +#endif + +#ifndef NO_OLDNAMES +#ifndef _CTYPE_DEFINED + int __cdecl isascii(int _C); + int __cdecl toascii(int _C); + int __cdecl iscsymf(int _C); + int __cdecl iscsym(int _C); +#else +#define isascii __isascii +#define toascii __toascii +#define iscsymf __iscsymf +#define iscsym __iscsym +#endif +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/dir.h b/05/tcc-final-old/win32/include/dir.h new file mode 100644 index 0000000..f38f750 --- /dev/null +++ b/05/tcc-final-old/win32/include/dir.h @@ -0,0 +1,31 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* + * dir.h + * + * This file OBSOLESCENT and only provided for backward compatibility. + * Please use io.h instead. + * + * This file is part of the Mingw32 package. + * + * Contributors: + * Created by Colin Peters + * Mumit Khan + * + * THIS SOFTWARE IS NOT COPYRIGHTED + * + * This source code is offered for use in the public domain. You may + * use, modify or distribute it freely. + * + * This code is distributed in the hope that it will be useful but + * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY + * DISCLAIMED. This includes but is not limited to warranties of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + */ + +#include + diff --git a/05/tcc-final-old/win32/include/direct.h b/05/tcc-final-old/win32/include/direct.h new file mode 100644 index 0000000..99ce69d --- /dev/null +++ b/05/tcc-final-old/win32/include/direct.h @@ -0,0 +1,68 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_DIRECT +#define _INC_DIRECT + +#include <_mingw.h> +#include + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _DISKFREE_T_DEFINED +#define _DISKFREE_T_DEFINED + struct _diskfree_t { + unsigned total_clusters; + unsigned avail_clusters; + unsigned sectors_per_cluster; + unsigned bytes_per_sector; + }; +#endif + + _CRTIMP char *__cdecl _getcwd(char *_DstBuf,int _SizeInBytes); + _CRTIMP char *__cdecl _getdcwd(int _Drive,char *_DstBuf,int _SizeInBytes); + char *__cdecl _getdcwd_nolock(int _Drive,char *_DstBuf,int _SizeInBytes); + _CRTIMP int __cdecl _chdir(const char *_Path); + _CRTIMP int __cdecl _mkdir(const char *_Path); + _CRTIMP int __cdecl _rmdir(const char *_Path); + _CRTIMP int __cdecl _chdrive(int _Drive); + _CRTIMP int __cdecl _getdrive(void); + _CRTIMP unsigned long __cdecl _getdrives(void); + +#ifndef _GETDISKFREE_DEFINED +#define _GETDISKFREE_DEFINED + _CRTIMP unsigned __cdecl _getdiskfree(unsigned _Drive,struct _diskfree_t *_DiskFree); +#endif + +#ifndef _WDIRECT_DEFINED +#define _WDIRECT_DEFINED + _CRTIMP wchar_t *__cdecl _wgetcwd(wchar_t *_DstBuf,int _SizeInWords); + _CRTIMP wchar_t *__cdecl _wgetdcwd(int _Drive,wchar_t *_DstBuf,int _SizeInWords); + wchar_t *__cdecl _wgetdcwd_nolock(int _Drive,wchar_t *_DstBuf,int _SizeInWords); + _CRTIMP int __cdecl _wchdir(const wchar_t *_Path); + _CRTIMP int __cdecl _wmkdir(const wchar_t *_Path); + _CRTIMP int __cdecl _wrmdir(const wchar_t *_Path); +#endif + +#ifndef NO_OLDNAMES + +#define diskfree_t _diskfree_t + + char *__cdecl getcwd(char *_DstBuf,int _SizeInBytes); + int __cdecl chdir(const char *_Path); + int __cdecl mkdir(const char *_Path); + int __cdecl rmdir(const char *_Path); +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/dirent.h b/05/tcc-final-old/win32/include/dirent.h new file mode 100644 index 0000000..cd31f59 --- /dev/null +++ b/05/tcc-final-old/win32/include/dirent.h @@ -0,0 +1,135 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* All the headers include this file. */ +#include <_mingw.h> + +#ifndef __STRICT_ANSI__ + +#ifndef _DIRENT_H_ +#define _DIRENT_H_ + + +#pragma pack(push,_CRT_PACKING) + +#include + +#ifndef RC_INVOKED + +#ifdef __cplusplus +extern "C" { +#endif + + struct dirent + { + long d_ino; /* Always zero. */ + unsigned short d_reclen; /* Always zero. */ + unsigned short d_namlen; /* Length of name in d_name. */ + char* d_name; /* File name. */ + /* NOTE: The name in the dirent structure points to the name in the + * finddata_t structure in the DIR. */ + }; + + /* + * This is an internal data structure. Good programmers will not use it + * except as an argument to one of the functions below. + * dd_stat field is now int (was short in older versions). + */ + typedef struct + { + /* disk transfer area for this dir */ + struct _finddata_t dd_dta; + + /* dirent struct to return from dir (NOTE: this makes this thread + * safe as long as only one thread uses a particular DIR struct at + * a time) */ + struct dirent dd_dir; + + /* _findnext handle */ + long dd_handle; + + /* + * Status of search: + * 0 = not started yet (next entry to read is first entry) + * -1 = off the end + * positive = 0 based index of next entry + */ + int dd_stat; + + /* given path for dir with search pattern (struct is extended) */ + char dd_name[1]; + } DIR; + + DIR* __cdecl opendir (const char*); + struct dirent* __cdecl readdir (DIR*); + int __cdecl closedir (DIR*); + void __cdecl rewinddir (DIR*); + long __cdecl telldir (DIR*); + void __cdecl seekdir (DIR*, long); + + + /* wide char versions */ + + struct _wdirent + { + long d_ino; /* Always zero. */ + unsigned short d_reclen; /* Always zero. */ + unsigned short d_namlen; /* Length of name in d_name. */ + wchar_t* d_name; /* File name. */ + /* NOTE: The name in the dirent structure points to the name in the * wfinddata_t structure in the _WDIR. */ + }; + + /* + * This is an internal data structure. Good programmers will not use it + * except as an argument to one of the functions below. + */ + typedef struct + { + /* disk transfer area for this dir */ + struct _wfinddata_t dd_dta; + + /* dirent struct to return from dir (NOTE: this makes this thread + * safe as long as only one thread uses a particular DIR struct at + * a time) */ + struct _wdirent dd_dir; + + /* _findnext handle */ + long dd_handle; + + /* + * Status of search: + * 0 = not started yet (next entry to read is first entry) + * -1 = off the end + * positive = 0 based index of next entry + */ + int dd_stat; + + /* given path for dir with search pattern (struct is extended) */ + wchar_t dd_name[1]; + } _WDIR; + + + + _WDIR* __cdecl _wopendir (const wchar_t*); + struct _wdirent* __cdecl _wreaddir (_WDIR*); + int __cdecl _wclosedir (_WDIR*); + void __cdecl _wrewinddir (_WDIR*); + long __cdecl _wtelldir (_WDIR*); + void __cdecl _wseekdir (_WDIR*, long); + + +#ifdef __cplusplus +} +#endif + +#endif /* Not RC_INVOKED */ + +#pragma pack(pop) + +#endif /* Not _DIRENT_H_ */ + + +#endif /* Not __STRICT_ANSI__ */ + diff --git a/05/tcc-final-old/win32/include/dos.h b/05/tcc-final-old/win32/include/dos.h new file mode 100644 index 0000000..294e8fe --- /dev/null +++ b/05/tcc-final-old/win32/include/dos.h @@ -0,0 +1,55 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_DOS +#define _INC_DOS + +#include <_mingw.h> +#include + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _DISKFREE_T_DEFINED +#define _DISKFREE_T_DEFINED + + struct _diskfree_t { + unsigned total_clusters; + unsigned avail_clusters; + unsigned sectors_per_cluster; + unsigned bytes_per_sector; + }; +#endif + +#define _A_NORMAL 0x00 +#define _A_RDONLY 0x01 +#define _A_HIDDEN 0x02 +#define _A_SYSTEM 0x04 +#define _A_SUBDIR 0x10 +#define _A_ARCH 0x20 + +#ifndef _GETDISKFREE_DEFINED +#define _GETDISKFREE_DEFINED + _CRTIMP unsigned __cdecl _getdiskfree(unsigned _Drive,struct _diskfree_t *_DiskFree); +#endif + +#if (defined(_X86_) && !defined(__x86_64)) + void __cdecl _disable(void); + void __cdecl _enable(void); +#endif + +#ifndef NO_OLDNAMES +#define diskfree_t _diskfree_t +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/errno.h b/05/tcc-final-old/win32/include/errno.h new file mode 100644 index 0000000..c2df015 --- /dev/null +++ b/05/tcc-final-old/win32/include/errno.h @@ -0,0 +1,75 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_ERRNO +#define _INC_ERRNO + +#include <_mingw.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CRT_ERRNO_DEFINED +#define _CRT_ERRNO_DEFINED + _CRTIMP extern int *__cdecl _errno(void); +#define errno (*_errno()) + + errno_t __cdecl _set_errno(int _Value); + errno_t __cdecl _get_errno(int *_Value); +#endif + +#define EPERM 1 +#define ENOENT 2 +#define ESRCH 3 +#define EINTR 4 +#define EIO 5 +#define ENXIO 6 +#define E2BIG 7 +#define ENOEXEC 8 +#define EBADF 9 +#define ECHILD 10 +#define EAGAIN 11 +#define ENOMEM 12 +#define EACCES 13 +#define EFAULT 14 +#define EBUSY 16 +#define EEXIST 17 +#define EXDEV 18 +#define ENODEV 19 +#define ENOTDIR 20 +#define EISDIR 21 +#define ENFILE 23 +#define EMFILE 24 +#define ENOTTY 25 +#define EFBIG 27 +#define ENOSPC 28 +#define ESPIPE 29 +#define EROFS 30 +#define EMLINK 31 +#define EPIPE 32 +#define EDOM 33 +#define EDEADLK 36 +#define ENAMETOOLONG 38 +#define ENOLCK 39 +#define ENOSYS 40 +#define ENOTEMPTY 41 + +#ifndef RC_INVOKED +#if !defined(_SECURECRT_ERRCODE_VALUES_DEFINED) +#define _SECURECRT_ERRCODE_VALUES_DEFINED +#define EINVAL 22 +#define ERANGE 34 +#define EILSEQ 42 +#define STRUNCATE 80 +#endif +#endif + +#define EDEADLOCK EDEADLK + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/excpt.h b/05/tcc-final-old/win32/include/excpt.h new file mode 100644 index 0000000..26cc943 --- /dev/null +++ b/05/tcc-final-old/win32/include/excpt.h @@ -0,0 +1,123 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_EXCPT +#define _INC_EXCPT + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + + struct _EXCEPTION_POINTERS; + +#ifndef EXCEPTION_DISPOSITION +#define EXCEPTION_DISPOSITION int +#endif +#define ExceptionContinueExecution 0 +#define ExceptionContinueSearch 1 +#define ExceptionNestedException 2 +#define ExceptionCollidedUnwind 3 + +#if (defined(_X86_) && !defined(__x86_64)) + struct _EXCEPTION_RECORD; + struct _CONTEXT; + + EXCEPTION_DISPOSITION __cdecl _except_handler(struct _EXCEPTION_RECORD *_ExceptionRecord,void *_EstablisherFrame,struct _CONTEXT *_ContextRecord,void *_DispatcherContext); +#elif defined(__ia64__) + + typedef struct _EXCEPTION_POINTERS *Exception_info_ptr; + struct _EXCEPTION_RECORD; + struct _CONTEXT; + struct _DISPATCHER_CONTEXT; + + _CRTIMP EXCEPTION_DISPOSITION __cdecl __C_specific_handler (struct _EXCEPTION_RECORD *_ExceptionRecord,unsigned __int64 _MemoryStackFp,unsigned __int64 _BackingStoreFp,struct _CONTEXT *_ContextRecord,struct _DISPATCHER_CONTEXT *_DispatcherContext,unsigned __int64 _GlobalPointer); +#elif defined(__x86_64) + + struct _EXCEPTION_RECORD; + struct _CONTEXT; +#endif + +#define GetExceptionCode _exception_code +#define exception_code _exception_code +#define GetExceptionInformation (struct _EXCEPTION_POINTERS *)_exception_info +#define exception_info (struct _EXCEPTION_POINTERS *)_exception_info +#define AbnormalTermination _abnormal_termination +#define abnormal_termination _abnormal_termination + + unsigned long __cdecl _exception_code(void); + void *__cdecl _exception_info(void); + int __cdecl _abnormal_termination(void); + +#define EXCEPTION_EXECUTE_HANDLER 1 +#define EXCEPTION_CONTINUE_SEARCH 0 +#define EXCEPTION_CONTINUE_EXECUTION -1 + + /* CRT stuff */ + typedef void (__cdecl * _PHNDLR)(int); + + struct _XCPT_ACTION { + unsigned long XcptNum; + int SigNum; + _PHNDLR XcptAction; + }; + + extern struct _XCPT_ACTION _XcptActTab[]; + extern int _XcptActTabCount; + extern int _XcptActTabSize; + extern int _First_FPE_Indx; + extern int _Num_FPE; + + int __cdecl __CppXcptFilter(unsigned long _ExceptionNum,struct _EXCEPTION_POINTERS * _ExceptionPtr); + int __cdecl _XcptFilter(unsigned long _ExceptionNum,struct _EXCEPTION_POINTERS * _ExceptionPtr); + + /* + * The type of function that is expected as an exception handler to be + * installed with _try1. + */ + typedef EXCEPTION_DISPOSITION (*PEXCEPTION_HANDLER)(struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*); + +#ifndef HAVE_NO_SEH + /* + * This is not entirely necessary, but it is the structure installed by + * the _try1 primitive below. + */ + typedef struct _EXCEPTION_REGISTRATION { + struct _EXCEPTION_REGISTRATION *prev; + EXCEPTION_DISPOSITION (*handler)(struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*); + } EXCEPTION_REGISTRATION, *PEXCEPTION_REGISTRATION; + + typedef EXCEPTION_REGISTRATION EXCEPTION_REGISTRATION_RECORD; + typedef PEXCEPTION_REGISTRATION PEXCEPTION_REGISTRATION_RECORD; +#endif + +#if (defined(_X86_) && !defined(__x86_64)) +#define __try1(pHandler) \ + __asm__ ("pushl %0;pushl %%fs:0;movl %%esp,%%fs:0;" : : "g" (pHandler)); + +#define __except1 \ + __asm__ ("movl (%%esp),%%eax;movl %%eax,%%fs:0;addl $8,%%esp;" \ + : : : "%eax"); +#elif defined(__x86_64) +#define __try1(pHandler) \ + __asm__ ("pushq %0;pushq %%gs:0;movq %%rsp,%%gs:0;" : : "g" (pHandler)); + +#define __except1 \ + __asm__ ("movq (%%rsp),%%rax;movq %%rax,%%gs:0;addq $16,%%rsp;" \ + : : : "%rax"); +#else +#define __try1(pHandler) +#define __except1 +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/fcntl.h b/05/tcc-final-old/win32/include/fcntl.h new file mode 100644 index 0000000..9202b08 --- /dev/null +++ b/05/tcc-final-old/win32/include/fcntl.h @@ -0,0 +1,52 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#include <_mingw.h> + +#include + +#ifndef _INC_FCNTL +#define _INC_FCNTL + +#define _O_RDONLY 0x0000 +#define _O_WRONLY 0x0001 +#define _O_RDWR 0x0002 +#define _O_APPEND 0x0008 +#define _O_CREAT 0x0100 +#define _O_TRUNC 0x0200 +#define _O_EXCL 0x0400 +#define _O_TEXT 0x4000 +#define _O_BINARY 0x8000 +#define _O_WTEXT 0x10000 +#define _O_U16TEXT 0x20000 +#define _O_U8TEXT 0x40000 +#define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR) + +#define _O_RAW _O_BINARY +#define _O_NOINHERIT 0x0080 +#define _O_TEMPORARY 0x0040 +#define _O_SHORT_LIVED 0x1000 + +#define _O_SEQUENTIAL 0x0020 +#define _O_RANDOM 0x0010 + +#if !defined(NO_OLDNAMES) || defined(_POSIX) +#define O_RDONLY _O_RDONLY +#define O_WRONLY _O_WRONLY +#define O_RDWR _O_RDWR +#define O_APPEND _O_APPEND +#define O_CREAT _O_CREAT +#define O_TRUNC _O_TRUNC +#define O_EXCL _O_EXCL +#define O_TEXT _O_TEXT +#define O_BINARY _O_BINARY +#define O_RAW _O_BINARY +#define O_TEMPORARY _O_TEMPORARY +#define O_NOINHERIT _O_NOINHERIT +#define O_SEQUENTIAL _O_SEQUENTIAL +#define O_RANDOM _O_RANDOM +#define O_ACCMODE _O_ACCMODE +#endif +#endif diff --git a/05/tcc-final-old/win32/include/fenv.h b/05/tcc-final-old/win32/include/fenv.h new file mode 100644 index 0000000..258f3a5 --- /dev/null +++ b/05/tcc-final-old/win32/include/fenv.h @@ -0,0 +1,108 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _FENV_H_ +#define _FENV_H_ + +#include <_mingw.h> + +/* FPU status word exception flags */ +#define FE_INVALID 0x01 +#define FE_DENORMAL 0x02 +#define FE_DIVBYZERO 0x04 +#define FE_OVERFLOW 0x08 +#define FE_UNDERFLOW 0x10 +#define FE_INEXACT 0x20 +#define FE_ALL_EXCEPT (FE_INVALID | FE_DENORMAL | FE_DIVBYZERO \ + | FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT) + +/* FPU control word rounding flags */ +#define FE_TONEAREST 0x0000 +#define FE_DOWNWARD 0x0400 +#define FE_UPWARD 0x0800 +#define FE_TOWARDZERO 0x0c00 + +/* The MXCSR exception flags are the same as the + FE flags. */ +#define __MXCSR_EXCEPT_FLAG_SHIFT 0 + +/* How much to shift FE status word exception flags + to get MXCSR rounding flags, */ +#define __MXCSR_ROUND_FLAG_SHIFT 3 + +#ifndef RC_INVOKED +/* + For now, support only for the basic abstraction of flags that are + either set or clear. fexcept_t could be structure that holds more + info about the fp environment. +*/ +typedef unsigned short fexcept_t; + +/* This 32-byte struct represents the entire floating point + environment as stored by fnstenv or fstenv, augmented by + the contents of the MXCSR register, as stored by stmxcsr + (if CPU supports it). */ +typedef struct +{ + unsigned short __control_word; + unsigned short __unused0; + unsigned short __status_word; + unsigned short __unused1; + unsigned short __tag_word; + unsigned short __unused2; + unsigned int __ip_offset; /* instruction pointer offset */ + unsigned short __ip_selector; + unsigned short __opcode; + unsigned int __data_offset; + unsigned short __data_selector; + unsigned short __unused3; + unsigned int __mxcsr; /* contents of the MXCSR register */ +} fenv_t; + + +/*The C99 standard (7.6.9) allows us to define implementation-specific macros for + different fp environments */ + +/* The default Intel x87 floating point environment (64-bit mantissa) */ +#define FE_PC64_ENV ((const fenv_t *)-1) + +/* The floating point environment set by MSVCRT _fpreset (53-bit mantissa) */ +#define FE_PC53_ENV ((const fenv_t *)-2) + +/* The FE_DFL_ENV macro is required by standard. + fesetenv will use the environment set at app startup.*/ +#define FE_DFL_ENV ((const fenv_t *) 0) + +#ifdef __cplusplus +extern "C" { +#endif + +/*TODO: Some of these could be inlined */ +/* 7.6.2 Exception */ + +extern int __cdecl feclearexcept (int); +extern int __cdecl fegetexceptflag (fexcept_t * flagp, int excepts); +extern int __cdecl feraiseexcept (int excepts ); +extern int __cdecl fesetexceptflag (const fexcept_t *, int); +extern int __cdecl fetestexcept (int excepts); + +/* 7.6.3 Rounding */ + +extern int __cdecl fegetround (void); +extern int __cdecl fesetround (int mode); + +/* 7.6.4 Environment */ + +extern int __cdecl fegetenv(fenv_t * envp); +extern int __cdecl fesetenv(const fenv_t * ); +extern int __cdecl feupdateenv(const fenv_t *); +extern int __cdecl feholdexcept(fenv_t *); + +#ifdef __cplusplus +} +#endif +#endif /* Not RC_INVOKED */ + +#endif /* ndef _FENV_H */ diff --git a/05/tcc-final-old/win32/include/inttypes.h b/05/tcc-final-old/win32/include/inttypes.h new file mode 100644 index 0000000..7360091 --- /dev/null +++ b/05/tcc-final-old/win32/include/inttypes.h @@ -0,0 +1,297 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* 7.8 Format conversion of integer types */ + +#ifndef _INTTYPES_H_ +#define _INTTYPES_H_ + +#include <_mingw.h> +#include +#define __need_wchar_t +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + intmax_t quot; + intmax_t rem; + } imaxdiv_t; + +#if !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) + +/* 7.8.1 Macros for format specifiers + * + * MS runtime does not yet understand C9x standard "ll" + * length specifier. It appears to treat "ll" as "l". + * The non-standard I64 length specifier causes warning in GCC, + * but understood by MS runtime functions. + */ + +/* fprintf macros for signed types */ +#define PRId8 "d" +#define PRId16 "d" +#define PRId32 "d" +#define PRId64 "I64d" + +#define PRIdLEAST8 "d" +#define PRIdLEAST16 "d" +#define PRIdLEAST32 "d" +#define PRIdLEAST64 "I64d" + +#define PRIdFAST8 "d" +#define PRIdFAST16 "d" +#define PRIdFAST32 "d" +#define PRIdFAST64 "I64d" + +#define PRIdMAX "I64d" + +#define PRIi8 "i" +#define PRIi16 "i" +#define PRIi32 "i" +#define PRIi64 "I64i" + +#define PRIiLEAST8 "i" +#define PRIiLEAST16 "i" +#define PRIiLEAST32 "i" +#define PRIiLEAST64 "I64i" + +#define PRIiFAST8 "i" +#define PRIiFAST16 "i" +#define PRIiFAST32 "i" +#define PRIiFAST64 "I64i" + +#define PRIiMAX "I64i" + +#define PRIo8 "o" +#define PRIo16 "o" +#define PRIo32 "o" +#define PRIo64 "I64o" + +#define PRIoLEAST8 "o" +#define PRIoLEAST16 "o" +#define PRIoLEAST32 "o" +#define PRIoLEAST64 "I64o" + +#define PRIoFAST8 "o" +#define PRIoFAST16 "o" +#define PRIoFAST32 "o" +#define PRIoFAST64 "I64o" + +#define PRIoMAX "I64o" + +/* fprintf macros for unsigned types */ +#define PRIu8 "u" +#define PRIu16 "u" +#define PRIu32 "u" +#define PRIu64 "I64u" + + +#define PRIuLEAST8 "u" +#define PRIuLEAST16 "u" +#define PRIuLEAST32 "u" +#define PRIuLEAST64 "I64u" + +#define PRIuFAST8 "u" +#define PRIuFAST16 "u" +#define PRIuFAST32 "u" +#define PRIuFAST64 "I64u" + +#define PRIuMAX "I64u" + +#define PRIx8 "x" +#define PRIx16 "x" +#define PRIx32 "x" +#define PRIx64 "I64x" + +#define PRIxLEAST8 "x" +#define PRIxLEAST16 "x" +#define PRIxLEAST32 "x" +#define PRIxLEAST64 "I64x" + +#define PRIxFAST8 "x" +#define PRIxFAST16 "x" +#define PRIxFAST32 "x" +#define PRIxFAST64 "I64x" + +#define PRIxMAX "I64x" + +#define PRIX8 "X" +#define PRIX16 "X" +#define PRIX32 "X" +#define PRIX64 "I64X" + +#define PRIXLEAST8 "X" +#define PRIXLEAST16 "X" +#define PRIXLEAST32 "X" +#define PRIXLEAST64 "I64X" + +#define PRIXFAST8 "X" +#define PRIXFAST16 "X" +#define PRIXFAST32 "X" +#define PRIXFAST64 "I64X" + +#define PRIXMAX "I64X" + +/* + * fscanf macros for signed int types + * NOTE: if 32-bit int is used for int_fast8_t and int_fast16_t + * (see stdint.h, 7.18.1.3), FAST8 and FAST16 should have + * no length identifiers + */ + +#define SCNd16 "hd" +#define SCNd32 "d" +#define SCNd64 "I64d" + +#define SCNdLEAST16 "hd" +#define SCNdLEAST32 "d" +#define SCNdLEAST64 "I64d" + +#define SCNdFAST16 "hd" +#define SCNdFAST32 "d" +#define SCNdFAST64 "I64d" + +#define SCNdMAX "I64d" + +#define SCNi16 "hi" +#define SCNi32 "i" +#define SCNi64 "I64i" + +#define SCNiLEAST16 "hi" +#define SCNiLEAST32 "i" +#define SCNiLEAST64 "I64i" + +#define SCNiFAST16 "hi" +#define SCNiFAST32 "i" +#define SCNiFAST64 "I64i" + +#define SCNiMAX "I64i" + +#define SCNo16 "ho" +#define SCNo32 "o" +#define SCNo64 "I64o" + +#define SCNoLEAST16 "ho" +#define SCNoLEAST32 "o" +#define SCNoLEAST64 "I64o" + +#define SCNoFAST16 "ho" +#define SCNoFAST32 "o" +#define SCNoFAST64 "I64o" + +#define SCNoMAX "I64o" + +#define SCNx16 "hx" +#define SCNx32 "x" +#define SCNx64 "I64x" + +#define SCNxLEAST16 "hx" +#define SCNxLEAST32 "x" +#define SCNxLEAST64 "I64x" + +#define SCNxFAST16 "hx" +#define SCNxFAST32 "x" +#define SCNxFAST64 "I64x" + +#define SCNxMAX "I64x" + +/* fscanf macros for unsigned int types */ + +#define SCNu16 "hu" +#define SCNu32 "u" +#define SCNu64 "I64u" + +#define SCNuLEAST16 "hu" +#define SCNuLEAST32 "u" +#define SCNuLEAST64 "I64u" + +#define SCNuFAST16 "hu" +#define SCNuFAST32 "u" +#define SCNuFAST64 "I64u" + +#define SCNuMAX "I64u" + +#ifdef _WIN64 +#define PRIdPTR "I64d" +#define PRIiPTR "I64i" +#define PRIoPTR "I64o" +#define PRIuPTR "I64u" +#define PRIxPTR "I64x" +#define PRIXPTR "I64X" +#define SCNdPTR "I64d" +#define SCNiPTR "I64i" +#define SCNoPTR "I64o" +#define SCNxPTR "I64x" +#define SCNuPTR "I64u" +#else +#define PRIdPTR "d" +#define PRIiPTR "i" +#define PRIoPTR "o" +#define PRIuPTR "u" +#define PRIxPTR "x" +#define PRIXPTR "X" +#define SCNdPTR "d" +#define SCNiPTR "i" +#define SCNoPTR "o" +#define SCNxPTR "x" +#define SCNuPTR "u" +#endif + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +/* + * no length modifier for char types prior to C9x + * MS runtime scanf appears to treat "hh" as "h" + */ + +/* signed char */ +#define SCNd8 "hhd" +#define SCNdLEAST8 "hhd" +#define SCNdFAST8 "hhd" + +#define SCNi8 "hhi" +#define SCNiLEAST8 "hhi" +#define SCNiFAST8 "hhi" + +#define SCNo8 "hho" +#define SCNoLEAST8 "hho" +#define SCNoFAST8 "hho" + +#define SCNx8 "hhx" +#define SCNxLEAST8 "hhx" +#define SCNxFAST8 "hhx" + +/* unsigned char */ +#define SCNu8 "hhu" +#define SCNuLEAST8 "hhu" +#define SCNuFAST8 "hhu" +#endif /* __STDC_VERSION__ >= 199901 */ + +#endif /* !defined(__cplusplus) || defined(__STDC_FORMAT_MACROS) */ + +intmax_t __cdecl imaxabs (intmax_t j); +__CRT_INLINE intmax_t __cdecl imaxabs (intmax_t j) + {return (j >= 0 ? j : -j);} +imaxdiv_t __cdecl imaxdiv (intmax_t numer, intmax_t denom); + +/* 7.8.2 Conversion functions for greatest-width integer types */ + +intmax_t __cdecl strtoimax (const char* __restrict__ nptr, + char** __restrict__ endptr, int base); +uintmax_t __cdecl strtoumax (const char* __restrict__ nptr, + char** __restrict__ endptr, int base); + +intmax_t __cdecl wcstoimax (const wchar_t* __restrict__ nptr, + wchar_t** __restrict__ endptr, int base); +uintmax_t __cdecl wcstoumax (const wchar_t* __restrict__ nptr, + wchar_t** __restrict__ endptr, int base); + +#ifdef __cplusplus +} +#endif + +#endif /* ndef _INTTYPES_H */ diff --git a/05/tcc-final-old/win32/include/io.h b/05/tcc-final-old/win32/include/io.h new file mode 100644 index 0000000..e2aeec3 --- /dev/null +++ b/05/tcc-final-old/win32/include/io.h @@ -0,0 +1,418 @@ + +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _IO_H_ +#define _IO_H_ + +#include <_mingw.h> +#include + +#pragma pack(push,_CRT_PACKING) + +#ifndef _POSIX_ + +#ifdef __cplusplus +extern "C" { +#endif + +_CRTIMP char* __cdecl _getcwd (char*, int); +#ifndef _FSIZE_T_DEFINED + typedef unsigned long _fsize_t; +#define _FSIZE_T_DEFINED +#endif + +#ifndef _FINDDATA_T_DEFINED + + struct _finddata32_t { + unsigned attrib; + __time32_t time_create; + __time32_t time_access; + __time32_t time_write; + _fsize_t size; + char name[260]; + }; + +/*#if _INTEGRAL_MAX_BITS >= 64*/ + + struct _finddata32i64_t { + unsigned attrib; + __time32_t time_create; + __time32_t time_access; + __time32_t time_write; + __int64 size; + char name[260]; + }; + + struct _finddata64i32_t { + unsigned attrib; + __time64_t time_create; + __time64_t time_access; + __time64_t time_write; + _fsize_t size; + char name[260]; + }; + + struct __finddata64_t { + unsigned attrib; + __time64_t time_create; + __time64_t time_access; + __time64_t time_write; + __int64 size; + char name[260]; + }; +/* #endif */ + +#ifdef _USE_32BIT_TIME_T +#define _finddata_t _finddata32_t +#define _finddatai64_t _finddata32i64_t + +#ifdef _WIN64 +#define _findfirst _findfirst32 +#define _findnext _findnext32 +#else +#define _findfirst32 _findfirst +#define _findnext32 _findnext +#endif +#define _findfirsti64 _findfirst32i64 +#define _findnexti64 _findnext32i64 +#else +#define _finddata_t _finddata64i32_t +#define _finddatai64_t __finddata64_t + +#define _findfirst _findfirst64i32 +#define _findnext _findnext64i32 +#define _findfirsti64 _findfirst64 +#define _findnexti64 _findnext64 +#endif + +#define _FINDDATA_T_DEFINED +#endif + +#ifndef _WFINDDATA_T_DEFINED + + struct _wfinddata32_t { + unsigned attrib; + __time32_t time_create; + __time32_t time_access; + __time32_t time_write; + _fsize_t size; + wchar_t name[260]; + }; + +/* #if _INTEGRAL_MAX_BITS >= 64 */ + + struct _wfinddata32i64_t { + unsigned attrib; + __time32_t time_create; + __time32_t time_access; + __time32_t time_write; + __int64 size; + wchar_t name[260]; + }; + + struct _wfinddata64i32_t { + unsigned attrib; + __time64_t time_create; + __time64_t time_access; + __time64_t time_write; + _fsize_t size; + wchar_t name[260]; + }; + + struct _wfinddata64_t { + unsigned attrib; + __time64_t time_create; + __time64_t time_access; + __time64_t time_write; + __int64 size; + wchar_t name[260]; + }; +/* #endif */ + +#ifdef _USE_32BIT_TIME_T +#define _wfinddata_t _wfinddata32_t +#define _wfinddatai64_t _wfinddata32i64_t + +#define _wfindfirst _wfindfirst32 +#define _wfindnext _wfindnext32 +#define _wfindfirsti64 _wfindfirst32i64 +#define _wfindnexti64 _wfindnext32i64 +#else +#define _wfinddata_t _wfinddata64i32_t +#define _wfinddatai64_t _wfinddata64_t + +#define _wfindfirst _wfindfirst64i32 +#define _wfindnext _wfindnext64i32 +#define _wfindfirsti64 _wfindfirst64 +#define _wfindnexti64 _wfindnext64 +#endif + +#define _WFINDDATA_T_DEFINED +#endif + +#define _A_NORMAL 0x00 +#define _A_RDONLY 0x01 +#define _A_HIDDEN 0x02 +#define _A_SYSTEM 0x04 +#define _A_SUBDIR 0x10 +#define _A_ARCH 0x20 + +#ifndef _SIZE_T_DEFINED +#define _SIZE_T_DEFINED +#undef size_t +#ifdef _WIN64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef unsigned int size_t __attribute__ ((mode (DI))); +#else + typedef unsigned __int64 size_t; +#endif +#else + typedef unsigned int size_t; +#endif +#endif + +#ifndef _SSIZE_T_DEFINED +#define _SSIZE_T_DEFINED +#undef ssize_t +#ifdef _WIN64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef int ssize_t __attribute__ ((mode (DI))); +#else + typedef __int64 ssize_t; +#endif +#else + typedef int ssize_t; +#endif +#endif + +#ifndef _OFF_T_DEFINED +#define _OFF_T_DEFINED +#ifndef _OFF_T_ +#define _OFF_T_ + typedef long _off_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long off_t; +#endif +#endif +#endif + +#ifndef _OFF64_T_DEFINED +#define _OFF64_T_DEFINED +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef int _off64_t __attribute__ ((mode (DI))); +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef int off64_t __attribute__ ((mode (DI))); +#endif +#else + typedef long long _off64_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long long off64_t; +#endif +#endif +#endif + + /* Some defines for _access nAccessMode (MS doesn't define them, but + * it doesn't seem to hurt to add them). */ +#define F_OK 0 /* Check for file existence */ +#define X_OK 1 /* Check for execute permission. */ +#define W_OK 2 /* Check for write permission */ +#define R_OK 4 /* Check for read permission */ + + _CRTIMP int __cdecl _access(const char *_Filename,int _AccessMode); + _CRTIMP int __cdecl _chmod(const char *_Filename,int _Mode); + _CRTIMP int __cdecl _chsize(int _FileHandle,long _Size); + _CRTIMP int __cdecl _close(int _FileHandle); + _CRTIMP int __cdecl _commit(int _FileHandle); + _CRTIMP int __cdecl _creat(const char *_Filename,int _PermissionMode); + _CRTIMP int __cdecl _dup(int _FileHandle); + _CRTIMP int __cdecl _dup2(int _FileHandleSrc,int _FileHandleDst); + _CRTIMP int __cdecl _eof(int _FileHandle); + _CRTIMP long __cdecl _filelength(int _FileHandle); + _CRTIMP intptr_t __cdecl _findfirst32(const char *_Filename,struct _finddata32_t *_FindData); + _CRTIMP int __cdecl _findnext32(intptr_t _FindHandle,struct _finddata32_t *_FindData); + _CRTIMP int __cdecl _findclose(intptr_t _FindHandle); + _CRTIMP int __cdecl _isatty(int _FileHandle); + _CRTIMP int __cdecl _locking(int _FileHandle,int _LockMode,long _NumOfBytes); + _CRTIMP long __cdecl _lseek(int _FileHandle,long _Offset,int _Origin); + _off64_t lseek64(int fd,_off64_t offset, int whence); + _CRTIMP char *__cdecl _mktemp(char *_TemplateName); + _CRTIMP int __cdecl _pipe(int *_PtHandles,unsigned int _PipeSize,int _TextMode); + _CRTIMP int __cdecl _read(int _FileHandle,void *_DstBuf,unsigned int _MaxCharCount); + +#ifndef _CRT_DIRECTORY_DEFINED +#define _CRT_DIRECTORY_DEFINED + int __cdecl remove(const char *_Filename); + int __cdecl rename(const char *_OldFilename,const char *_NewFilename); + _CRTIMP int __cdecl _unlink(const char *_Filename); +#ifndef NO_OLDNAMES + int __cdecl unlink(const char *_Filename); +#endif +#endif + + _CRTIMP int __cdecl _setmode(int _FileHandle,int _Mode); + _CRTIMP long __cdecl _tell(int _FileHandle); + _CRTIMP int __cdecl _umask(int _Mode); + _CRTIMP int __cdecl _write(int _FileHandle,const void *_Buf,unsigned int _MaxCharCount); + +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP __int64 __cdecl _filelengthi64(int _FileHandle); + _CRTIMP intptr_t __cdecl _findfirst32i64(const char *_Filename,struct _finddata32i64_t *_FindData); + _CRTIMP intptr_t __cdecl _findfirst64(const char *_Filename,struct __finddata64_t *_FindData); +#ifdef __cplusplus +#include +#endif + intptr_t __cdecl _findfirst64i32(const char *_Filename,struct _finddata64i32_t *_FindData); + __CRT_INLINE intptr_t __cdecl _findfirst64i32(const char *_Filename,struct _finddata64i32_t *_FindData) + { + struct __finddata64_t fd; + intptr_t ret = _findfirst64(_Filename,&fd); + _FindData->attrib=fd.attrib; + _FindData->time_create=fd.time_create; + _FindData->time_access=fd.time_access; + _FindData->time_write=fd.time_write; + _FindData->size=(_fsize_t) fd.size; + strncpy(_FindData->name,fd.name,260); + return ret; + } + _CRTIMP int __cdecl _findnext32i64(intptr_t _FindHandle,struct _finddata32i64_t *_FindData); + _CRTIMP int __cdecl _findnext64(intptr_t _FindHandle,struct __finddata64_t *_FindData); + int __cdecl _findnext64i32(intptr_t _FindHandle,struct _finddata64i32_t *_FindData); + __CRT_INLINE int __cdecl _findnext64i32(intptr_t _FindHandle,struct _finddata64i32_t *_FindData) + { + struct __finddata64_t fd; + int ret = _findnext64(_FindHandle,&fd); + _FindData->attrib=fd.attrib; + _FindData->time_create=fd.time_create; + _FindData->time_access=fd.time_access; + _FindData->time_write=fd.time_write; + _FindData->size=(_fsize_t) fd.size; + strncpy(_FindData->name,fd.name,260); + return ret; + } + __int64 __cdecl _lseeki64(int _FileHandle,__int64 _Offset,int _Origin); + __int64 __cdecl _telli64(int _FileHandle); +#endif +#ifndef NO_OLDNAMES + +#ifndef _UWIN + int __cdecl chdir (const char *); + char *__cdecl getcwd (char *, int); + int __cdecl mkdir (const char *); + char *__cdecl mktemp(char *); + int __cdecl rmdir (const char*); + int __cdecl chmod (const char *, int); +#endif /* _UWIN */ + +#endif /* Not NO_OLDNAMES */ + + _CRTIMP errno_t __cdecl _sopen_s(int *_FileHandle,const char *_Filename,int _OpenFlag,int _ShareFlag,int _PermissionMode); + +#ifndef __cplusplus + _CRTIMP int __cdecl _open(const char *_Filename,int _OpenFlag,...); + _CRTIMP int __cdecl _sopen(const char *_Filename,int _OpenFlag,int _ShareFlag,...); +#else + extern "C++" _CRTIMP int __cdecl _open(const char *_Filename,int _Openflag,int _PermissionMode = 0); + extern "C++" _CRTIMP int __cdecl _sopen(const char *_Filename,int _Openflag,int _ShareFlag,int _PermissionMode = 0); +#endif + +#ifndef _WIO_DEFINED +#define _WIO_DEFINED + _CRTIMP int __cdecl _waccess(const wchar_t *_Filename,int _AccessMode); + _CRTIMP int __cdecl _wchmod(const wchar_t *_Filename,int _Mode); + _CRTIMP int __cdecl _wcreat(const wchar_t *_Filename,int _PermissionMode); + _CRTIMP intptr_t __cdecl _wfindfirst32(const wchar_t *_Filename,struct _wfinddata32_t *_FindData); + _CRTIMP int __cdecl _wfindnext32(intptr_t _FindHandle,struct _wfinddata32_t *_FindData); + _CRTIMP int __cdecl _wunlink(const wchar_t *_Filename); + _CRTIMP int __cdecl _wrename(const wchar_t *_NewFilename,const wchar_t *_OldFilename); + _CRTIMP wchar_t *__cdecl _wmktemp(wchar_t *_TemplateName); + +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP intptr_t __cdecl _wfindfirst32i64(const wchar_t *_Filename,struct _wfinddata32i64_t *_FindData); + intptr_t __cdecl _wfindfirst64i32(const wchar_t *_Filename,struct _wfinddata64i32_t *_FindData); + _CRTIMP intptr_t __cdecl _wfindfirst64(const wchar_t *_Filename,struct _wfinddata64_t *_FindData); + _CRTIMP int __cdecl _wfindnext32i64(intptr_t _FindHandle,struct _wfinddata32i64_t *_FindData); + int __cdecl _wfindnext64i32(intptr_t _FindHandle,struct _wfinddata64i32_t *_FindData); + _CRTIMP int __cdecl _wfindnext64(intptr_t _FindHandle,struct _wfinddata64_t *_FindData); +#endif + + _CRTIMP errno_t __cdecl _wsopen_s(int *_FileHandle,const wchar_t *_Filename,int _OpenFlag,int _ShareFlag,int _PermissionFlag); + +#if !defined(__cplusplus) || !(defined(_X86_) && !defined(__x86_64)) + _CRTIMP int __cdecl _wopen(const wchar_t *_Filename,int _OpenFlag,...); + _CRTIMP int __cdecl _wsopen(const wchar_t *_Filename,int _OpenFlag,int _ShareFlag,...); +#else + extern "C++" _CRTIMP int __cdecl _wopen(const wchar_t *_Filename,int _OpenFlag,int _PermissionMode = 0); + extern "C++" _CRTIMP int __cdecl _wsopen(const wchar_t *_Filename,int _OpenFlag,int _ShareFlag,int _PermissionMode = 0); +#endif + +#endif + + int __cdecl __lock_fhandle(int _Filehandle); + void __cdecl _unlock_fhandle(int _Filehandle); + _CRTIMP intptr_t __cdecl _get_osfhandle(int _FileHandle); + _CRTIMP int __cdecl _open_osfhandle(intptr_t _OSFileHandle,int _Flags); + +#ifndef NO_OLDNAMES + int __cdecl access(const char *_Filename,int _AccessMode); + int __cdecl chmod(const char *_Filename,int _AccessMode); + int __cdecl chsize(int _FileHandle,long _Size); + int __cdecl close(int _FileHandle); + int __cdecl creat(const char *_Filename,int _PermissionMode); + int __cdecl dup(int _FileHandle); + int __cdecl dup2(int _FileHandleSrc,int _FileHandleDst); + int __cdecl eof(int _FileHandle); + long __cdecl filelength(int _FileHandle); + int __cdecl isatty(int _FileHandle); + int __cdecl locking(int _FileHandle,int _LockMode,long _NumOfBytes); + long __cdecl lseek(int _FileHandle,long _Offset,int _Origin); + char *__cdecl mktemp(char *_TemplateName); + int __cdecl open(const char *_Filename,int _OpenFlag,...); + int __cdecl read(int _FileHandle,void *_DstBuf,unsigned int _MaxCharCount); + int __cdecl setmode(int _FileHandle,int _Mode); + int __cdecl sopen(const char *_Filename,int _OpenFlag,int _ShareFlag,...); + long __cdecl tell(int _FileHandle); + int __cdecl umask(int _Mode); + int __cdecl write(int _Filehandle,const void *_Buf,unsigned int _MaxCharCount); +#endif + +#ifdef __cplusplus +} +#endif +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* Misc stuff */ +char *getlogin(void); +#ifdef __USE_MINGW_ALARM +unsigned int alarm(unsigned int seconds); +#endif + +#ifdef __USE_MINGW_ACCESS +/* Old versions of MSVCRT access() just ignored X_OK, while the version + shipped with Vista, returns an error code. This will restore the + old behaviour */ +static inline int __mingw_access (const char *__fname, int __mode) { + return _access (__fname, __mode & ~X_OK); +} + +#define access(__f,__m) __mingw_access (__f, __m) +#endif + + +#ifdef __cplusplus +} +#endif + + +#pragma pack(pop) + +#include + +#endif /* End _IO_H_ */ + diff --git a/05/tcc-final-old/win32/include/limits.h b/05/tcc-final-old/win32/include/limits.h new file mode 100644 index 0000000..fafb04a --- /dev/null +++ b/05/tcc-final-old/win32/include/limits.h @@ -0,0 +1,111 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#include <_mingw.h> + +#ifndef _INC_LIMITS +#define _INC_LIMITS + +/* +* File system limits +* +* TODO: NAME_MAX and OPEN_MAX are file system limits or not? Are they the +* same as FILENAME_MAX and FOPEN_MAX from stdio.h? +* NOTE: Apparently the actual size of PATH_MAX is 260, but a space is +* required for the NUL. TODO: Test? +*/ +#define PATH_MAX (259) + +#define CHAR_BIT 8 +#define SCHAR_MIN (-128) +#define SCHAR_MAX 127 +#define UCHAR_MAX 0xff + +#define CHAR_MIN SCHAR_MIN +#define CHAR_MAX SCHAR_MAX + +#define MB_LEN_MAX 5 +#define SHRT_MIN (-32768) +#define SHRT_MAX 32767 +#define USHRT_MAX 0xffff +#define INT_MIN (-2147483647 - 1) +#define INT_MAX 2147483647 +#define UINT_MAX 0xffffffff +#define LONG_MIN (-2147483647L - 1) +#define LONG_MAX 2147483647L +#define ULONG_MAX 0xffffffffUL +#define LLONG_MAX 9223372036854775807ll +#define LLONG_MIN (-9223372036854775807ll - 1) +#define ULLONG_MAX 0xffffffffffffffffull + +#if _INTEGRAL_MAX_BITS >= 8 +#define _I8_MIN (-127 - 1) +#define _I8_MAX 127i8 +#define _UI8_MAX 0xffu +#endif + +#if _INTEGRAL_MAX_BITS >= 16 +#define _I16_MIN (-32767 - 1) +#define _I16_MAX 32767i16 +#define _UI16_MAX 0xffffu +#endif + +#if _INTEGRAL_MAX_BITS >= 32 +#define _I32_MIN (-2147483647 - 1) +#define _I32_MAX 2147483647 +#define _UI32_MAX 0xffffffffu +#endif + +#if defined(__GNUC__) +#undef LONG_LONG_MAX +#define LONG_LONG_MAX 9223372036854775807ll +#undef LONG_LONG_MIN +#define LONG_LONG_MIN (-LONG_LONG_MAX-1) +#undef ULONG_LONG_MAX +#define ULONG_LONG_MAX (2ull * LONG_LONG_MAX + 1ull) +#endif + +#if _INTEGRAL_MAX_BITS >= 64 +#define _I64_MIN (-9223372036854775807ll - 1) +#define _I64_MAX 9223372036854775807ll +#define _UI64_MAX 0xffffffffffffffffull +#endif + +#ifndef SIZE_MAX +#ifdef _WIN64 +#define SIZE_MAX _UI64_MAX +#else +#define SIZE_MAX UINT_MAX +#endif +#endif + +#ifdef _POSIX_ +#define _POSIX_ARG_MAX 4096 +#define _POSIX_CHILD_MAX 6 +#define _POSIX_LINK_MAX 8 +#define _POSIX_MAX_CANON 255 +#define _POSIX_MAX_INPUT 255 +#define _POSIX_NAME_MAX 14 +#define _POSIX_NGROUPS_MAX 0 +#define _POSIX_OPEN_MAX 16 +#define _POSIX_PATH_MAX 255 +#define _POSIX_PIPE_BUF 512 +#define _POSIX_SSIZE_MAX 32767 +#define _POSIX_STREAM_MAX 8 +#define _POSIX_TZNAME_MAX 3 +#define ARG_MAX 14500 +#define LINK_MAX 1024 +#define MAX_CANON _POSIX_MAX_CANON +#define MAX_INPUT _POSIX_MAX_INPUT +#define NAME_MAX 255 +#define NGROUPS_MAX 16 +#define OPEN_MAX 32 +#define PATH_MAX 512 +#define PIPE_BUF _POSIX_PIPE_BUF +#define SSIZE_MAX _POSIX_SSIZE_MAX +#define STREAM_MAX 20 +#define TZNAME_MAX 10 +#endif +#endif diff --git a/05/tcc-final-old/win32/include/locale.h b/05/tcc-final-old/win32/include/locale.h new file mode 100644 index 0000000..686aa9b --- /dev/null +++ b/05/tcc-final-old/win32/include/locale.h @@ -0,0 +1,91 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_LOCALE +#define _INC_LOCALE + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#define LC_ALL 0 +#define LC_COLLATE 1 +#define LC_CTYPE 2 +#define LC_MONETARY 3 +#define LC_NUMERIC 4 +#define LC_TIME 5 + +#define LC_MIN LC_ALL +#define LC_MAX LC_TIME + +#ifndef _LCONV_DEFINED +#define _LCONV_DEFINED + struct lconv { + char *decimal_point; + char *thousands_sep; + char *grouping; + char *int_curr_symbol; + char *currency_symbol; + char *mon_decimal_point; + char *mon_thousands_sep; + char *mon_grouping; + char *positive_sign; + char *negative_sign; + char int_frac_digits; + char frac_digits; + char p_cs_precedes; + char p_sep_by_space; + char n_cs_precedes; + char n_sep_by_space; + char p_sign_posn; + char n_sign_posn; + }; +#endif + +#ifndef _CONFIG_LOCALE_SWT +#define _CONFIG_LOCALE_SWT + +#define _ENABLE_PER_THREAD_LOCALE 0x1 +#define _DISABLE_PER_THREAD_LOCALE 0x2 +#define _ENABLE_PER_THREAD_LOCALE_GLOBAL 0x10 +#define _DISABLE_PER_THREAD_LOCALE_GLOBAL 0x20 +#define _ENABLE_PER_THREAD_LOCALE_NEW 0x100 +#define _DISABLE_PER_THREAD_LOCALE_NEW 0x200 + +#endif + + int __cdecl _configthreadlocale(int _Flag); + char *__cdecl setlocale(int _Category,const char *_Locale); + _CRTIMP struct lconv *__cdecl localeconv(void); + _locale_t __cdecl _get_current_locale(void); + _locale_t __cdecl _create_locale(int _Category,const char *_Locale); + void __cdecl _free_locale(_locale_t _Locale); + _locale_t __cdecl __get_current_locale(void); + _locale_t __cdecl __create_locale(int _Category,const char *_Locale); + void __cdecl __free_locale(_locale_t _Locale); + +#ifndef _WLOCALE_DEFINED +#define _WLOCALE_DEFINED + _CRTIMP wchar_t *__cdecl _wsetlocale(int _Category,const wchar_t *_Locale); +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/malloc.h b/05/tcc-final-old/win32/include/malloc.h new file mode 100644 index 0000000..fc783a8 --- /dev/null +++ b/05/tcc-final-old/win32/include/malloc.h @@ -0,0 +1,181 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _MALLOC_H_ +#define _MALLOC_H_ + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifndef _MM_MALLOC_H_INCLUDED +#define _MM_MALLOC_H_INCLUDED +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef _WIN64 +#define _HEAP_MAXREQ 0xFFFFFFFFFFFFFFE0 +#else +#define _HEAP_MAXREQ 0xFFFFFFE0 +#endif + +#ifndef _STATIC_ASSERT +#define _STATIC_ASSERT(expr) extern void __static_assert_t(int [(expr)?1:-1]) +#endif + +/* Return codes for _heapwalk() */ +#define _HEAPEMPTY (-1) +#define _HEAPOK (-2) +#define _HEAPBADBEGIN (-3) +#define _HEAPBADNODE (-4) +#define _HEAPEND (-5) +#define _HEAPBADPTR (-6) + +/* Values for _heapinfo.useflag */ +#define _FREEENTRY 0 +#define _USEDENTRY 1 + +#ifndef _HEAPINFO_DEFINED +#define _HEAPINFO_DEFINED + /* The structure used to walk through the heap with _heapwalk. */ + typedef struct _heapinfo { + int *_pentry; + size_t _size; + int _useflag; + } _HEAPINFO; +#endif + + extern unsigned int _amblksiz; + +#define _mm_free(a) _aligned_free(a) +#define _mm_malloc(a,b) _aligned_malloc(a,b) + +#ifndef _CRT_ALLOCATION_DEFINED +#define _CRT_ALLOCATION_DEFINED + void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements); + void __cdecl free(void *_Memory); + void *__cdecl malloc(size_t _Size); + void *__cdecl realloc(void *_Memory,size_t _NewSize); + _CRTIMP void *__cdecl _recalloc(void *_Memory,size_t _Count,size_t _Size); + /* _CRTIMP void __cdecl _aligned_free(void *_Memory); + _CRTIMP void *__cdecl _aligned_malloc(size_t _Size,size_t _Alignment); */ + _CRTIMP void *__cdecl _aligned_offset_malloc(size_t _Size,size_t _Alignment,size_t _Offset); + _CRTIMP void *__cdecl _aligned_realloc(void *_Memory,size_t _Size,size_t _Alignment); + _CRTIMP void *__cdecl _aligned_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment); + _CRTIMP void *__cdecl _aligned_offset_realloc(void *_Memory,size_t _Size,size_t _Alignment,size_t _Offset); + _CRTIMP void *__cdecl _aligned_offset_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment,size_t _Offset); +#endif + +#define _MAX_WAIT_MALLOC_CRT 60000 + + _CRTIMP int __cdecl _resetstkoflw (void); + _CRTIMP unsigned long __cdecl _set_malloc_crt_max_wait(unsigned long _NewValue); + + _CRTIMP void *__cdecl _expand(void *_Memory,size_t _NewSize); + _CRTIMP size_t __cdecl _msize(void *_Memory); +#ifdef __GNUC__ +#undef _alloca +#define _alloca(x) __builtin_alloca((x)) +#else + /* tcc implements alloca internally and exposes it (since commit d778bde7). + /* alloca is declared at include/stddef.h (which is distributed with tcc). + */ +#ifdef _alloca +#undef _alloca +#endif +#define _alloca(x) alloca((x)) +#endif + _CRTIMP size_t __cdecl _get_sbh_threshold(void); + _CRTIMP int __cdecl _set_sbh_threshold(size_t _NewValue); + _CRTIMP errno_t __cdecl _set_amblksiz(size_t _Value); + _CRTIMP errno_t __cdecl _get_amblksiz(size_t *_Value); + _CRTIMP int __cdecl _heapadd(void *_Memory,size_t _Size); + _CRTIMP int __cdecl _heapchk(void); + _CRTIMP int __cdecl _heapmin(void); + _CRTIMP int __cdecl _heapset(unsigned int _Fill); + _CRTIMP int __cdecl _heapwalk(_HEAPINFO *_EntryInfo); + _CRTIMP size_t __cdecl _heapused(size_t *_Used,size_t *_Commit); + _CRTIMP intptr_t __cdecl _get_heap_handle(void); + +#define _ALLOCA_S_THRESHOLD 1024 +#define _ALLOCA_S_STACK_MARKER 0xCCCC +#define _ALLOCA_S_HEAP_MARKER 0xDDDD + +#if(defined(_X86_) && !defined(__x86_64)) +#define _ALLOCA_S_MARKER_SIZE 8 +#elif defined(__ia64__) || defined(__x86_64) +#define _ALLOCA_S_MARKER_SIZE 16 +#endif + +#if !defined(RC_INVOKED) + static __inline void *_MarkAllocaS(void *_Ptr,unsigned int _Marker) { + if(_Ptr) { + *((unsigned int*)_Ptr) = _Marker; + _Ptr = (char*)_Ptr + _ALLOCA_S_MARKER_SIZE; + } + return _Ptr; + } +#endif + +#undef _malloca +#define _malloca(size) \ + ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \ + _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_STACK_MARKER) : \ + _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE),_ALLOCA_S_HEAP_MARKER)) +#undef _FREEA_INLINE +#define _FREEA_INLINE + +#ifndef RC_INVOKED +#undef _freea + static __inline void __cdecl _freea(void *_Memory) { + unsigned int _Marker; + if(_Memory) { + _Memory = (char*)_Memory - _ALLOCA_S_MARKER_SIZE; + _Marker = *(unsigned int *)_Memory; + if(_Marker==_ALLOCA_S_HEAP_MARKER) { + free(_Memory); + } +#ifdef _ASSERTE + else if(_Marker!=_ALLOCA_S_STACK_MARKER) { + _ASSERTE(("Corrupted pointer passed to _freea",0)); + } +#endif + } + } +#endif /* RC_INVOKED */ + +#ifndef NO_OLDNAMES +#ifdef __GNUC__ +#undef alloca +#define alloca(x) __builtin_alloca((x)) +#endif +#endif + +#ifdef HEAPHOOK +#ifndef _HEAPHOOK_DEFINED +#define _HEAPHOOK_DEFINED + typedef int (__cdecl *_HEAPHOOK)(int,size_t,void *,void **); +#endif + + _CRTIMP _HEAPHOOK __cdecl _setheaphook(_HEAPHOOK _NewHook); + +#define _HEAP_MALLOC 1 +#define _HEAP_CALLOC 2 +#define _HEAP_FREE 3 +#define _HEAP_REALLOC 4 +#define _HEAP_MSIZE 5 +#define _HEAP_EXPAND 6 +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) + +#endif /* _MALLOC_H_ */ diff --git a/05/tcc-final-old/win32/include/math.h b/05/tcc-final-old/win32/include/math.h new file mode 100644 index 0000000..74add20 --- /dev/null +++ b/05/tcc-final-old/win32/include/math.h @@ -0,0 +1,737 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _MATH_H_ +#define _MATH_H_ + +#if __GNUC__ >= 3 +#pragma GCC system_header +#endif + +#include <_mingw.h> + +struct exception; + +#pragma pack(push,_CRT_PACKING) + +#define _DOMAIN 1 +#define _SING 2 +#define _OVERFLOW 3 +#define _UNDERFLOW 4 +#define _TLOSS 5 +#define _PLOSS 6 + +#ifndef __STRICT_ANSI__ +#ifndef NO_OLDNAMES +#define DOMAIN _DOMAIN +#define SING _SING +#define OVERFLOW _OVERFLOW +#define UNDERFLOW _UNDERFLOW +#define TLOSS _TLOSS +#define PLOSS _PLOSS +#endif +#endif + +#ifndef __STRICT_ANSI__ +#define M_E 2.71828182845904523536 +#define M_LOG2E 1.44269504088896340736 +#define M_LOG10E 0.434294481903251827651 +#define M_LN2 0.693147180559945309417 +#define M_LN10 2.30258509299404568402 +#define M_PI 3.14159265358979323846 +#define M_PI_2 1.57079632679489661923 +#define M_PI_4 0.785398163397448309616 +#define M_1_PI 0.318309886183790671538 +#define M_2_PI 0.636619772367581343076 +#define M_2_SQRTPI 1.12837916709551257390 +#define M_SQRT2 1.41421356237309504880 +#define M_SQRT1_2 0.707106781186547524401 +#endif + +#ifndef __STRICT_ANSI__ +/* See also float.h */ +#ifndef __MINGW_FPCLASS_DEFINED +#define __MINGW_FPCLASS_DEFINED 1 +#define _FPCLASS_SNAN 0x0001 /* Signaling "Not a Number" */ +#define _FPCLASS_QNAN 0x0002 /* Quiet "Not a Number" */ +#define _FPCLASS_NINF 0x0004 /* Negative Infinity */ +#define _FPCLASS_NN 0x0008 /* Negative Normal */ +#define _FPCLASS_ND 0x0010 /* Negative Denormal */ +#define _FPCLASS_NZ 0x0020 /* Negative Zero */ +#define _FPCLASS_PZ 0x0040 /* Positive Zero */ +#define _FPCLASS_PD 0x0080 /* Positive Denormal */ +#define _FPCLASS_PN 0x0100 /* Positive Normal */ +#define _FPCLASS_PINF 0x0200 /* Positive Infinity */ +#endif +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _EXCEPTION_DEFINED +#define _EXCEPTION_DEFINED + struct _exception { + int type; + char *name; + double arg1; + double arg2; + double retval; + }; +#endif + +#ifndef _COMPLEX_DEFINED +#define _COMPLEX_DEFINED + struct _complex { + double x,y; + }; +#endif + +#define EDOM 33 +#define ERANGE 34 + +#ifndef _HUGE +#ifdef _MSVCRT_ + extern double *_HUGE; +#else + extern double *_imp___HUGE; +#define _HUGE (*_imp___HUGE) +#endif +#endif + +#define HUGE_VAL _HUGE + +#ifndef _CRT_ABS_DEFINED +#define _CRT_ABS_DEFINED + int __cdecl abs(int _X); + long __cdecl labs(long _X); +#endif + double __cdecl acos(double _X); + double __cdecl asin(double _X); + double __cdecl atan(double _X); + double __cdecl atan2(double _Y,double _X); +#ifndef _SIGN_DEFINED +#define _SIGN_DEFINED + _CRTIMP double __cdecl _copysign (double _Number,double _Sign); + _CRTIMP double __cdecl _chgsign (double _X); +#endif + double __cdecl cos(double _X); + double __cdecl cosh(double _X); + double __cdecl exp(double _X); + double __cdecl expm1(double _X); + double __cdecl fabs(double _X); + double __cdecl fmod(double _X,double _Y); + double __cdecl log(double _X); + double __cdecl log10(double _X); + double __cdecl pow(double _X,double _Y); + double __cdecl sin(double _X); + double __cdecl sinh(double _X); + double __cdecl tan(double _X); + double __cdecl tanh(double _X); + double __cdecl sqrt(double _X); +#ifndef _CRT_ATOF_DEFINED +#define _CRT_ATOF_DEFINED + double __cdecl atof(const char *_String); + double __cdecl _atof_l(const char *_String,_locale_t _Locale); +#endif + + _CRTIMP double __cdecl _cabs(struct _complex _ComplexA); + double __cdecl ceil(double _X); + double __cdecl floor(double _X); + double __cdecl frexp(double _X,int *_Y); + double __cdecl _hypot(double _X,double _Y); + _CRTIMP double __cdecl _j0(double _X); + _CRTIMP double __cdecl _j1(double _X); + _CRTIMP double __cdecl _jn(int _X,double _Y); + double __cdecl ldexp(double _X,int _Y); +#ifndef _CRT_MATHERR_DEFINED +#define _CRT_MATHERR_DEFINED + int __cdecl _matherr(struct _exception *_Except); +#endif + double __cdecl modf(double _X,double *_Y); + _CRTIMP double __cdecl _y0(double _X); + _CRTIMP double __cdecl _y1(double _X); + _CRTIMP double __cdecl _yn(int _X,double _Y); + +#if(defined(_X86_) && !defined(__x86_64)) + _CRTIMP int __cdecl _set_SSE2_enable(int _Flag); + /* from libmingwex */ + float __cdecl _hypotf(float _X,float _Y); +#endif + + float frexpf(float _X,int *_Y); + float __cdecl ldexpf(float _X,int _Y); + long double __cdecl ldexpl(long double _X,int _Y); + float __cdecl acosf(float _X); + float __cdecl asinf(float _X); + float __cdecl atanf(float _X); + float __cdecl atan2f(float _X,float _Y); + float __cdecl cosf(float _X); + float __cdecl sinf(float _X); + float __cdecl tanf(float _X); + float __cdecl coshf(float _X); + float __cdecl sinhf(float _X); + float __cdecl tanhf(float _X); + float __cdecl expf(float _X); + float __cdecl expm1f(float _X); + float __cdecl logf(float _X); + float __cdecl log10f(float _X); + float __cdecl modff(float _X,float *_Y); + float __cdecl powf(float _X,float _Y); + float __cdecl sqrtf(float _X); + float __cdecl ceilf(float _X); + float __cdecl floorf(float _X); + float __cdecl fmodf(float _X,float _Y); + float __cdecl _hypotf(float _X,float _Y); + float __cdecl fabsf(float _X); +#if !defined(__ia64__) + /* from libmingwex */ + float __cdecl _copysignf (float _Number,float _Sign); + float __cdecl _chgsignf (float _X); + float __cdecl _logbf(float _X); + float __cdecl _nextafterf(float _X,float _Y); + int __cdecl _finitef(float _X); + int __cdecl _isnanf(float _X); + int __cdecl _fpclassf(float _X); +#endif + +#ifndef __cplusplus + __CRT_INLINE long double __cdecl fabsl (long double x) + { + long double res; + __asm__ ("fabs;" : "=t" (res) : "0" (x)); + return res; + } +#define _hypotl(x,y) ((long double)_hypot((double)(x),(double)(y))) +#define _matherrl _matherr + __CRT_INLINE long double _chgsignl(long double _Number) { return _chgsign((double)(_Number)); } + __CRT_INLINE long double _copysignl(long double _Number,long double _Sign) { return _copysign((double)(_Number),(double)(_Sign)); } + __CRT_INLINE float frexpf(float _X,int *_Y) { return ((float)frexp((double)_X,_Y)); } + +#if !defined (__ia64__) + __CRT_INLINE float __cdecl fabsf (float x) + { + float res; + __asm__ ("fabs;" : "=t" (res) : "0" (x)); + return res; + } + + __CRT_INLINE float __cdecl ldexpf (float x, int expn) { return (float) ldexp (x, expn); } +#endif +#else + // cplusplus + __CRT_INLINE long double __cdecl fabsl (long double x) + { + long double res; + __asm__ ("fabs;" : "=t" (res) : "0" (x)); + return res; + } + __CRT_INLINE long double modfl(long double _X,long double *_Y) { + double _Di,_Df = modf((double)_X,&_Di); + *_Y = (long double)_Di; + return (_Df); + } + __CRT_INLINE long double _chgsignl(long double _Number) { return _chgsign(static_cast(_Number)); } + __CRT_INLINE long double _copysignl(long double _Number,long double _Sign) { return _copysign(static_cast(_Number),static_cast(_Sign)); } + __CRT_INLINE float frexpf(float _X,int *_Y) { return ((float)frexp((double)_X,_Y)); } +#ifndef __ia64__ + __CRT_INLINE float __cdecl fabsf (float x) + { + float res; + __asm__ ("fabs;" : "=t" (res) : "0" (x)); + return res; + } + __CRT_INLINE float __cdecl ldexpf (float x, int expn) { return (float) ldexp (x, expn); } +#ifndef __x86_64 + __CRT_INLINE float acosf(float _X) { return ((float)acos((double)_X)); } + __CRT_INLINE float asinf(float _X) { return ((float)asin((double)_X)); } + __CRT_INLINE float atanf(float _X) { return ((float)atan((double)_X)); } + __CRT_INLINE float atan2f(float _X,float _Y) { return ((float)atan2((double)_X,(double)_Y)); } + __CRT_INLINE float ceilf(float _X) { return ((float)ceil((double)_X)); } + __CRT_INLINE float cosf(float _X) { return ((float)cos((double)_X)); } + __CRT_INLINE float coshf(float _X) { return ((float)cosh((double)_X)); } + __CRT_INLINE float expf(float _X) { return ((float)exp((double)_X)); } + __CRT_INLINE float floorf(float _X) { return ((float)floor((double)_X)); } + __CRT_INLINE float fmodf(float _X,float _Y) { return ((float)fmod((double)_X,(double)_Y)); } + __CRT_INLINE float logf(float _X) { return ((float)log((double)_X)); } + __CRT_INLINE float log10f(float _X) { return ((float)log10((double)_X)); } + __CRT_INLINE float modff(float _X,float *_Y) { + double _Di,_Df = modf((double)_X,&_Di); + *_Y = (float)_Di; + return ((float)_Df); + } + __CRT_INLINE float powf(float _X,float _Y) { return ((float)pow((double)_X,(double)_Y)); } + __CRT_INLINE float sinf(float _X) { return ((float)sin((double)_X)); } + __CRT_INLINE float sinhf(float _X) { return ((float)sinh((double)_X)); } + __CRT_INLINE float sqrtf(float _X) { return ((float)sqrt((double)_X)); } + __CRT_INLINE float tanf(float _X) { return ((float)tan((double)_X)); } + __CRT_INLINE float tanhf(float _X) { return ((float)tanh((double)_X)); } +#endif +#endif +#endif + +#ifndef NO_OLDNAMES +#define matherr _matherr + +#define HUGE _HUGE + /* double __cdecl cabs(struct _complex _X); */ + double __cdecl hypot(double _X,double _Y); + _CRTIMP double __cdecl j0(double _X); + _CRTIMP double __cdecl j1(double _X); + _CRTIMP double __cdecl jn(int _X,double _Y); + _CRTIMP double __cdecl y0(double _X); + _CRTIMP double __cdecl y1(double _X); + _CRTIMP double __cdecl yn(int _X,double _Y); +#endif + +#ifndef __NO_ISOCEXT +#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \ + || !defined __STRICT_ANSI__ || defined __GLIBCPP__ + +#define NAN (0.0F/0.0F) +#define HUGE_VALF (1.0F/0.0F) +#define HUGE_VALL (1.0L/0.0L) +#define INFINITY (1.0F/0.0F) + + +#define FP_NAN 0x0100 +#define FP_NORMAL 0x0400 +#define FP_INFINITE (FP_NAN | FP_NORMAL) +#define FP_ZERO 0x4000 +#define FP_SUBNORMAL (FP_NORMAL | FP_ZERO) + /* 0x0200 is signbit mask */ + + + /* + We can't __CRT_INLINE float or double, because we want to ensure truncation + to semantic type before classification. + (A normal long double value might become subnormal when + converted to double, and zero when converted to float.) + */ + + extern int __cdecl __fpclassifyf (float); + extern int __cdecl __fpclassify (double); + extern int __cdecl __fpclassifyl (long double); + +/* Implemented at tcc/tcc_libm.h */ +#define fpclassify(x) (sizeof (x) == sizeof (float) ? __fpclassifyf (x) \ + : sizeof (x) == sizeof (double) ? __fpclassify (x) \ + : __fpclassifyl (x)) + + /* 7.12.3.2 */ +#define isfinite(x) ((fpclassify(x) & FP_NAN) == 0) + + /* 7.12.3.3 */ +#define isinf(x) (fpclassify(x) == FP_INFINITE) + + /* 7.12.3.4 */ + /* We don't need to worry about truncation here: + A NaN stays a NaN. */ +#define isnan(x) (fpclassify(x) == FP_NAN) + + /* 7.12.3.5 */ +#define isnormal(x) (fpclassify(x) == FP_NORMAL) + + /* 7.12.3.6 The signbit macro */ + + extern int __cdecl __signbitf (float); + extern int __cdecl __signbit (double); + extern int __cdecl __signbitl (long double); + +/* Implemented at tcc/tcc_libm.h */ +#define signbit(x) (sizeof (x) == sizeof (float) ? __signbitf (x) \ + : sizeof (x) == sizeof (double) ? __signbit (x) \ + : __signbitl (x)) + + extern double __cdecl exp2(double); + extern float __cdecl exp2f(float); + extern long double __cdecl exp2l(long double); + +#define FP_ILOGB0 ((int)0x80000000) +#define FP_ILOGBNAN ((int)0x80000000) + extern int __cdecl ilogb (double); + extern int __cdecl ilogbf (float); + extern int __cdecl ilogbl (long double); + + extern double __cdecl log1p(double); + extern float __cdecl log1pf(float); + extern long double __cdecl log1pl(long double); + + extern double __cdecl log2 (double); + extern float __cdecl log2f (float); + extern long double __cdecl log2l (long double); + + extern double __cdecl logb (double); + extern float __cdecl logbf (float); + extern long double __cdecl logbl (long double); + + __CRT_INLINE double __cdecl logb (double x) + { + double res; + __asm__ ("fxtract\n\t" + "fstp %%st" : "=t" (res) : "0" (x)); + return res; + } + + __CRT_INLINE float __cdecl logbf (float x) + { + float res; + __asm__ ("fxtract\n\t" + "fstp %%st" : "=t" (res) : "0" (x)); + return res; + } + + __CRT_INLINE long double __cdecl logbl (long double x) + { + long double res; + __asm__ ("fxtract\n\t" + "fstp %%st" : "=t" (res) : "0" (x)); + return res; + } + + extern long double __cdecl modfl (long double, long double*); + + /* 7.12.6.13 */ + extern double __cdecl scalbn (double, int); + extern float __cdecl scalbnf (float, int); + extern long double __cdecl scalbnl (long double, int); + + extern double __cdecl scalbln (double, long); + extern float __cdecl scalblnf (float, long); + extern long double __cdecl scalblnl (long double, long); + + /* 7.12.7.1 */ + /* Implementations adapted from Cephes versions */ + extern double __cdecl cbrt (double); + extern float __cdecl cbrtf (float); + extern long double __cdecl cbrtl (long double); + + __CRT_INLINE float __cdecl hypotf (float x, float y) + { return (float) hypot (x, y);} + extern long double __cdecl hypotl (long double, long double); + + extern long double __cdecl powl (long double, long double); + extern long double __cdecl expl(long double); + extern long double __cdecl expm1l(long double); + extern long double __cdecl coshl(long double); + extern long double __cdecl fabsl (long double); + extern long double __cdecl acosl(long double); + extern long double __cdecl asinl(long double); + extern long double __cdecl atanl(long double); + extern long double __cdecl atan2l(long double,long double); + extern long double __cdecl sinhl(long double); + extern long double __cdecl tanhl(long double); + + /* 7.12.8.1 The erf functions */ + extern double __cdecl erf (double); + extern float __cdecl erff (float); + /* TODO + extern long double __cdecl erfl (long double); + */ + + /* 7.12.8.2 The erfc functions */ + extern double __cdecl erfc (double); + extern float __cdecl erfcf (float); + /* TODO + extern long double __cdecl erfcl (long double); + */ + + /* 7.12.8.3 The lgamma functions */ + extern double __cdecl lgamma (double); + extern float __cdecl lgammaf (float); + extern long double __cdecl lgammal (long double); + + /* 7.12.8.4 The tgamma functions */ + extern double __cdecl tgamma (double); + extern float __cdecl tgammaf (float); + extern long double __cdecl tgammal (long double); + + extern long double __cdecl ceill (long double); + extern long double __cdecl floorl (long double); + extern long double __cdecl frexpl(long double,int *); + extern long double __cdecl log10l(long double); + extern long double __cdecl logl(long double); + extern long double __cdecl cosl(long double); + extern long double __cdecl sinl(long double); + extern long double __cdecl tanl(long double); + extern long double sqrtl(long double); + + /* 7.12.9.3 */ + extern double __cdecl nearbyint ( double); + extern float __cdecl nearbyintf (float); + extern long double __cdecl nearbyintl (long double); + + /* 7.12.9.4 */ + /* round, using fpu control word settings */ + __CRT_INLINE double __cdecl rint (double x) + { + double retval; + __asm__ ( + "fldl %1\n" + "frndint \n" + "fstl %0\n" : "=m" (retval) : "m" (x)); + return retval; + } + + __CRT_INLINE float __cdecl rintf (float x) + { + float retval; + __asm__ ( + "flds %1\n" + "frndint \n" + "fsts %0\n" : "=m" (retval) : "m" (x)); + return retval; + } + + __CRT_INLINE long double __cdecl rintl (long double x) + { + long double retval; + __asm__ ( + "fldt %1\n" + "frndint \n" + "fstt %0\n" : "=m" (retval) : "m" (x)); + return retval; + } + + /* 7.12.9.5 */ + __CRT_INLINE long __cdecl lrint (double x) + { + long retval; + __asm__ __volatile__ \ + ("fldl %1\n" \ + "fistpl %0" : "=m" (retval) : "m" (x)); \ + return retval; + } + + __CRT_INLINE long __cdecl lrintf (float x) + { + long retval; + __asm__ __volatile__ \ + ("flds %1\n" \ + "fistpl %0" : "=m" (retval) : "m" (x)); \ + return retval; + } + + __CRT_INLINE long __cdecl lrintl (long double x) + { + long retval; + __asm__ __volatile__ \ + ("fldt %1\n" \ + "fistpl %0" : "=m" (retval) : "m" (x)); \ + return retval; + } + + __CRT_INLINE long long __cdecl llrint (double x) + { + long long retval; + __asm__ __volatile__ \ + ("fldl %1\n" \ + "fistpll %0" : "=m" (retval) : "m" (x)); \ + return retval; + } + + __CRT_INLINE long long __cdecl llrintf (float x) + { + long long retval; + __asm__ __volatile__ \ + ("flds %1\n" \ + "fistpll %0" : "=m" (retval) : "m" (x)); \ + return retval; + } + + __CRT_INLINE long long __cdecl llrintl (long double x) + { + long long retval; + __asm__ __volatile__ \ + ("fldt %1\n" \ + "fistpll %0" : "=m" (retval) : "m" (x)); \ + return retval; + } + + #define FE_TONEAREST 0x0000 + #define FE_DOWNWARD 0x0400 + #define FE_UPWARD 0x0800 + #define FE_TOWARDZERO 0x0c00 + + __CRT_INLINE double trunc (double _x) + { + double retval; + unsigned short saved_cw; + unsigned short tmp_cw; + __asm__ ("fnstcw %0;" : "=m" (saved_cw)); /* save FPU control word */ + tmp_cw = (saved_cw & ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)) + | FE_TOWARDZERO; + __asm__ ("fldcw %0;" : : "m" (tmp_cw)); + __asm__ ("fldl %1;" + "frndint;" + "fstl %0;" : "=m" (retval) : "m" (_x)); /* round towards zero */ + __asm__ ("fldcw %0;" : : "m" (saved_cw) ); /* restore saved control word */ + return retval; + } + + /* 7.12.9.6 */ + /* round away from zero, regardless of fpu control word settings */ + extern double __cdecl round (double); + extern float __cdecl roundf (float); + extern long double __cdecl roundl (long double); + + /* 7.12.9.7 */ + extern long __cdecl lround (double); + extern long __cdecl lroundf (float); + extern long __cdecl lroundl (long double); + + extern long long __cdecl llround (double); + extern long long __cdecl llroundf (float); + extern long long __cdecl llroundl (long double); + + /* 7.12.9.8 */ + /* round towards zero, regardless of fpu control word settings */ + extern double __cdecl trunc (double); + extern float __cdecl truncf (float); + extern long double __cdecl truncl (long double); + + extern long double __cdecl fmodl (long double, long double); + + /* 7.12.10.2 */ + extern double __cdecl remainder (double, double); + extern float __cdecl remainderf (float, float); + extern long double __cdecl remainderl (long double, long double); + + /* 7.12.10.3 */ + extern double __cdecl remquo(double, double, int *); + extern float __cdecl remquof(float, float, int *); + extern long double __cdecl remquol(long double, long double, int *); + + /* 7.12.11.1 */ + extern double __cdecl copysign (double, double); /* in libmoldname.a */ + extern float __cdecl copysignf (float, float); + extern long double __cdecl copysignl (long double, long double); + + /* 7.12.11.2 Return a NaN */ + extern double __cdecl nan(const char *tagp); + extern float __cdecl nanf(const char *tagp); + extern long double __cdecl nanl(const char *tagp); + +#ifndef __STRICT_ANSI__ +#define _nan() nan("") +#define _nanf() nanf("") +#define _nanl() nanl("") +#endif + + /* 7.12.11.3 */ + extern double __cdecl nextafter (double, double); /* in libmoldname.a */ + extern float __cdecl nextafterf (float, float); + extern long double __cdecl nextafterl (long double, long double); + + /* 7.12.11.4 The nexttoward functions: TODO */ + + /* 7.12.12.1 */ + /* x > y ? (x - y) : 0.0 */ + extern double __cdecl fdim (double x, double y); + extern float __cdecl fdimf (float x, float y); + extern long double __cdecl fdiml (long double x, long double y); + + /* fmax and fmin. + NaN arguments are treated as missing data: if one argument is a NaN + and the other numeric, then these functions choose the numeric + value. */ + + /* 7.12.12.2 */ + extern double __cdecl fmax (double, double); + extern float __cdecl fmaxf (float, float); + extern long double __cdecl fmaxl (long double, long double); + + /* 7.12.12.3 */ + extern double __cdecl fmin (double, double); + extern float __cdecl fminf (float, float); + extern long double __cdecl fminl (long double, long double); + + /* 7.12.13.1 */ + /* return x * y + z as a ternary op */ + extern double __cdecl fma (double, double, double); + extern float __cdecl fmaf (float, float, float); + extern long double __cdecl fmal (long double, long double, long double); + + +#if 0 // gr: duplicate, see below + /* 7.12.14 */ + /* + * With these functions, comparisons involving quiet NaNs set the FP + * condition code to "unordered". The IEEE floating-point spec + * dictates that the result of floating-point comparisons should be + * false whenever a NaN is involved, with the exception of the != op, + * which always returns true: yes, (NaN != NaN) is true). + */ + +#if __GNUC__ >= 3 + +#define isgreater(x, y) __builtin_isgreater(x, y) +#define isgreaterequal(x, y) __builtin_isgreaterequal(x, y) +#define isless(x, y) __builtin_isless(x, y) +#define islessequal(x, y) __builtin_islessequal(x, y) +#define islessgreater(x, y) __builtin_islessgreater(x, y) +#define isunordered(x, y) __builtin_isunordered(x, y) + +#else + /* helper */ + __CRT_INLINE int __cdecl + __fp_unordered_compare (long double x, long double y){ + unsigned short retval; + __asm__ ("fucom %%st(1);" + "fnstsw;": "=a" (retval) : "t" (x), "u" (y)); + return retval; + } + +#define isgreater(x, y) ((__fp_unordered_compare(x, y) \ + & 0x4500) == 0) +#define isless(x, y) ((__fp_unordered_compare (y, x) \ + & 0x4500) == 0) +#define isgreaterequal(x, y) ((__fp_unordered_compare (x, y) \ + & FP_INFINITE) == 0) +#define islessequal(x, y) ((__fp_unordered_compare(y, x) \ + & FP_INFINITE) == 0) +#define islessgreater(x, y) ((__fp_unordered_compare(x, y) \ + & FP_SUBNORMAL) == 0) +#define isunordered(x, y) ((__fp_unordered_compare(x, y) \ + & 0x4500) == 0x4500) + +#endif +#endif //0 + + +#endif /* __STDC_VERSION__ >= 199901L */ +#endif /* __NO_ISOCEXT */ + +#ifdef __cplusplus +} +extern "C++" { + template inline _Ty _Pow_int(_Ty _X,int _Y) { + unsigned int _N; + if(_Y >= 0) _N = (unsigned int)_Y; + else _N = (unsigned int)(-_Y); + for(_Ty _Z = _Ty(1);;_X *= _X) { + if((_N & 1)!=0) _Z *= _X; + if((_N >>= 1)==0) return (_Y < 0 ? _Ty(1) / _Z : _Z); + } + } +} +#endif + +#pragma pack(pop) + +/* 7.12.14 */ +/* + * With these functions, comparisons involving quiet NaNs set the FP + * condition code to "unordered". The IEEE floating-point spec + * dictates that the result of floating-point comparisons should be + * false whenever a NaN is involved, with the exception of the != op, + * which always returns true: yes, (NaN != NaN) is true). + */ + +/* Mini libm (inline __fpclassify*, __signbit* and variants) */ +#include "tcc/tcc_libm.h" + +#endif /* End _MATH_H_ */ + diff --git a/05/tcc-final-old/win32/include/mem.h b/05/tcc-final-old/win32/include/mem.h new file mode 100644 index 0000000..2552023 --- /dev/null +++ b/05/tcc-final-old/win32/include/mem.h @@ -0,0 +1,13 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* + * This file is part of the Mingw32 package. + * + * mem.h maps to string.h + */ +#ifndef __STRICT_ANSI__ +#include +#endif diff --git a/05/tcc-final-old/win32/include/memory.h b/05/tcc-final-old/win32/include/memory.h new file mode 100644 index 0000000..90d88ae --- /dev/null +++ b/05/tcc-final-old/win32/include/memory.h @@ -0,0 +1,40 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_MEMORY +#define _INC_MEMORY + +#include <_mingw.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CONST_RETURN +#define _CONST_RETURN +#endif + +#define _WConst_return _CONST_RETURN + +#ifndef _CRT_MEMORY_DEFINED +#define _CRT_MEMORY_DEFINED + _CRTIMP void *__cdecl _memccpy(void *_Dst,const void *_Src,int _Val,size_t _MaxCount); + _CONST_RETURN void *__cdecl memchr(const void *_Buf ,int _Val,size_t _MaxCount); + _CRTIMP int __cdecl _memicmp(const void *_Buf1,const void *_Buf2,size_t _Size); + _CRTIMP int __cdecl _memicmp_l(const void *_Buf1,const void *_Buf2,size_t _Size,_locale_t _Locale); + int __cdecl memcmp(const void *_Buf1,const void *_Buf2,size_t _Size); + void *__cdecl memcpy(void *_Dst,const void *_Src,size_t _Size); + void *__cdecl memset(void *_Dst,int _Val,size_t _Size); + +#ifndef NO_OLDNAMES + void *__cdecl memccpy(void *_Dst,const void *_Src,int _Val,size_t _Size); + int __cdecl memicmp(const void *_Buf1,const void *_Buf2,size_t _Size); +#endif +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/process.h b/05/tcc-final-old/win32/include/process.h new file mode 100644 index 0000000..dadaf2b --- /dev/null +++ b/05/tcc-final-old/win32/include/process.h @@ -0,0 +1,176 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_PROCESS +#define _INC_PROCESS + +#include <_mingw.h> + +/* Includes a definition of _pid_t and pid_t */ +#include + +#ifndef _POSIX_ +#ifdef __cplusplus +extern "C" { +#endif + +#define _P_WAIT 0 +#define _P_NOWAIT 1 +#define _OLD_P_OVERLAY 2 +#define _P_NOWAITO 3 +#define _P_DETACH 4 +#define _P_OVERLAY 2 + +#define _WAIT_CHILD 0 +#define _WAIT_GRANDCHILD 1 + + _CRTIMP uintptr_t __cdecl _beginthread(void (__cdecl *_StartAddress) (void *),unsigned _StackSize,void *_ArgList); + _CRTIMP void __cdecl _endthread(void); + _CRTIMP uintptr_t __cdecl _beginthreadex(void *_Security,unsigned _StackSize,unsigned (__stdcall *_StartAddress) (void *),void *_ArgList,unsigned _InitFlag,unsigned *_ThrdAddr); + _CRTIMP void __cdecl _endthreadex(unsigned _Retval); + +#ifndef _CRT_TERMINATE_DEFINED +#define _CRT_TERMINATE_DEFINED + void __cdecl __MINGW_NOTHROW exit(int _Code) __MINGW_ATTRIB_NORETURN; + _CRTIMP void __cdecl __MINGW_NOTHROW _exit(int _Code) __MINGW_ATTRIB_NORETURN; + +#pragma push_macro("abort") +#undef abort + void __cdecl __declspec(noreturn) abort(void); +#pragma pop_macro("abort") + +#endif + + _CRTIMP void __cdecl __MINGW_NOTHROW _cexit(void); + _CRTIMP void __cdecl __MINGW_NOTHROW _c_exit(void); + _CRTIMP int __cdecl _getpid(void); + _CRTIMP intptr_t __cdecl _cwait(int *_TermStat,intptr_t _ProcHandle,int _Action); + _CRTIMP intptr_t __cdecl _execl(const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _execle(const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _execlp(const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _execlpe(const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _execv(const char *_Filename,const char *const *_ArgList); + _CRTIMP intptr_t __cdecl _execve(const char *_Filename,const char *const *_ArgList,const char *const *_Env); + _CRTIMP intptr_t __cdecl _execvp(const char *_Filename,const char *const *_ArgList); + _CRTIMP intptr_t __cdecl _execvpe(const char *_Filename,const char *const *_ArgList,const char *const *_Env); + _CRTIMP intptr_t __cdecl _spawnl(int _Mode,const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _spawnle(int _Mode,const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _spawnlp(int _Mode,const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _spawnlpe(int _Mode,const char *_Filename,const char *_ArgList,...); + _CRTIMP intptr_t __cdecl _spawnv(int _Mode,const char *_Filename,const char *const *_ArgList); + _CRTIMP intptr_t __cdecl _spawnve(int _Mode,const char *_Filename,const char *const *_ArgList,const char *const *_Env); + _CRTIMP intptr_t __cdecl _spawnvp(int _Mode,const char *_Filename,const char *const *_ArgList); + _CRTIMP intptr_t __cdecl _spawnvpe(int _Mode,const char *_Filename,const char *const *_ArgList,const char *const *_Env); + +#ifndef _CRT_SYSTEM_DEFINED +#define _CRT_SYSTEM_DEFINED + int __cdecl system(const char *_Command); +#endif + +#ifndef _WPROCESS_DEFINED +#define _WPROCESS_DEFINED + _CRTIMP intptr_t __cdecl _wexecl(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexecle(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexeclp(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexeclpe(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexecv(const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wexecve(const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); + _CRTIMP intptr_t __cdecl _wexecvp(const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wexecvpe(const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); + _CRTIMP intptr_t __cdecl _wspawnl(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnle(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnlp(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnlpe(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnv(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wspawnve(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); + _CRTIMP intptr_t __cdecl _wspawnvp(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wspawnvpe(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); +#ifndef _CRT_WSYSTEM_DEFINED +#define _CRT_WSYSTEM_DEFINED + _CRTIMP int __cdecl _wsystem(const wchar_t *_Command); +#endif +#endif + + void __cdecl __security_init_cookie(void); +#if (defined(_X86_) && !defined(__x86_64)) + void __fastcall __security_check_cookie(uintptr_t _StackCookie); + __declspec(noreturn) void __cdecl __report_gsfailure(void); +#else + void __cdecl __security_check_cookie(uintptr_t _StackCookie); + __declspec(noreturn) void __cdecl __report_gsfailure(uintptr_t _StackCookie); +#endif + extern uintptr_t __security_cookie; + + intptr_t __cdecl _loaddll(char *_Filename); + int __cdecl _unloaddll(intptr_t _Handle); + int (__cdecl *__cdecl _getdllprocaddr(intptr_t _Handle,char *_ProcedureName,intptr_t _Ordinal))(void); + +#ifdef _DECL_DLLMAIN +#ifdef _WINDOWS_ + WINBOOL WINAPI DllMain(HANDLE _HDllHandle,DWORD _Reason,LPVOID _Reserved); + WINBOOL WINAPI _CRT_INIT(HANDLE _HDllHandle,DWORD _Reason,LPVOID _Reserved); + WINBOOL WINAPI _wCRT_INIT(HANDLE _HDllHandle,DWORD _Reason,LPVOID _Reserved); + extern WINBOOL (WINAPI *const _pRawDllMain)(HANDLE,DWORD,LPVOID); +#else + int __stdcall DllMain(void *_HDllHandle,unsigned _Reason,void *_Reserved); + int __stdcall _CRT_INIT(void *_HDllHandle,unsigned _Reason,void *_Reserved); + int __stdcall _wCRT_INIT(void *_HDllHandle,unsigned _Reason,void *_Reserved); + extern int (__stdcall *const _pRawDllMain)(void *,unsigned,void *); +#endif +#endif + +#ifndef NO_OLDNAMES +#define P_WAIT _P_WAIT +#define P_NOWAIT _P_NOWAIT +#define P_OVERLAY _P_OVERLAY +#define OLD_P_OVERLAY _OLD_P_OVERLAY +#define P_NOWAITO _P_NOWAITO +#define P_DETACH _P_DETACH +#define WAIT_CHILD _WAIT_CHILD +#define WAIT_GRANDCHILD _WAIT_GRANDCHILD + + intptr_t __cdecl cwait(int *_TermStat,intptr_t _ProcHandle,int _Action); +#ifdef __GNUC__ + int __cdecl execl(const char *_Filename,const char *_ArgList,...); + int __cdecl execle(const char *_Filename,const char *_ArgList,...); + int __cdecl execlp(const char *_Filename,const char *_ArgList,...); + int __cdecl execlpe(const char *_Filename,const char *_ArgList,...); +#else + intptr_t __cdecl execl(const char *_Filename,const char *_ArgList,...); + intptr_t __cdecl execle(const char *_Filename,const char *_ArgList,...); + intptr_t __cdecl execlp(const char *_Filename,const char *_ArgList,...); + intptr_t __cdecl execlpe(const char *_Filename,const char *_ArgList,...); +#endif + intptr_t __cdecl spawnl(int,const char *_Filename,const char *_ArgList,...); + intptr_t __cdecl spawnle(int,const char *_Filename,const char *_ArgList,...); + intptr_t __cdecl spawnlp(int,const char *_Filename,const char *_ArgList,...); + intptr_t __cdecl spawnlpe(int,const char *_Filename,const char *_ArgList,...); + int __cdecl getpid(void); +#ifdef __GNUC__ + /* Those methods are predefined by gcc builtins to return int. So to prevent + stupid warnings, define them in POSIX way. This is save, because those + methods do not return in success case, so that the return value is not + really dependent to its scalar width. */ + int __cdecl execv(const char *_Filename,const char *const _ArgList[]); + int __cdecl execve(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); + int __cdecl execvp(const char *_Filename,const char *const _ArgList[]); + int __cdecl execvpe(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); +#else + intptr_t __cdecl execv(const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl execve(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); + intptr_t __cdecl execvp(const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl execvpe(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); +#endif + intptr_t __cdecl spawnv(int,const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl spawnve(int,const char *_Filename,const char *const _ArgList[],const char *const _Env[]); + intptr_t __cdecl spawnvp(int,const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl spawnvpe(int,const char *_Filename,const char *const _ArgList[],char *const _Env[]); +#endif + +#ifdef __cplusplus +} +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/conio_s.h b/05/tcc-final-old/win32/include/sec_api/conio_s.h new file mode 100644 index 0000000..98d97ba --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/conio_s.h @@ -0,0 +1,42 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ + +#ifndef _INC_CONIO_S +#define _INC_CONIO_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP errno_t __cdecl _cgets_s(char *_Buffer,size_t _Size,size_t *_SizeRead); + _CRTIMP int __cdecl _cprintf_s(const char *_Format,...); + _CRTIMP int __cdecl _cscanf_s(const char *_Format,...); + _CRTIMP int __cdecl _cscanf_s_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcprintf_s(const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cprintf_s_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcprintf_s_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + +#ifndef _WCONIO_DEFINED_S +#define _WCONIO_DEFINED_S + _CRTIMP errno_t __cdecl _cgetws_s(wchar_t *_Buffer,size_t _SizeInWords,size_t *_SizeRead); + _CRTIMP int __cdecl _cwprintf_s(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf_s(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_s(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_s_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); +#endif + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/crtdbg_s.h b/05/tcc-final-old/win32/include/sec_api/crtdbg_s.h new file mode 100644 index 0000000..4598b4f --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/crtdbg_s.h @@ -0,0 +1,19 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ + +#ifndef _INC_CRTDBG_S +#define _INC_CRTDBG_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#define _dupenv_s_dbg(ps1,size,s2,t,f,l) _dupenv_s(ps1,size,s2) +#define _wdupenv_s_dbg(ps1,size,s2,t,f,l) _wdupenv_s(ps1,size,s2) + +#endif + +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/io_s.h b/05/tcc-final-old/win32/include/sec_api/io_s.h new file mode 100644 index 0000000..ec565a6 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/io_s.h @@ -0,0 +1,33 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_IO_S +#define _INC_IO_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP errno_t __cdecl _access_s(const char *_Filename,int _AccessMode); + _CRTIMP errno_t __cdecl _chsize_s(int _FileHandle,__int64 _Size); + _CRTIMP errno_t __cdecl _mktemp_s(char *_TemplateName,size_t _Size); + _CRTIMP errno_t __cdecl _umask_s(int _NewMode,int *_OldMode); + +#ifndef _WIO_S_DEFINED +#define _WIO_S_DEFINED + _CRTIMP errno_t __cdecl _waccess_s(const wchar_t *_Filename,int _AccessMode); + _CRTIMP errno_t __cdecl _wmktemp_s(wchar_t *_TemplateName,size_t _SizeInWords); +#endif + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/mbstring_s.h b/05/tcc-final-old/win32/include/sec_api/mbstring_s.h new file mode 100644 index 0000000..6b2b188 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/mbstring_s.h @@ -0,0 +1,52 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_MBSTRING_S +#define _INC_MBSTRING_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _MBSTRING_S_DEFINED +#define _MBSTRING_S_DEFINED + _CRTIMP errno_t __cdecl _mbscat_s(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src); + _CRTIMP errno_t __cdecl _mbscat_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbscpy_s(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src); + _CRTIMP errno_t __cdecl _mbscpy_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbslwr_s(unsigned char *_Str,size_t _SizeInBytes); + _CRTIMP errno_t __cdecl _mbslwr_s_l(unsigned char *_Str,size_t _SizeInBytes,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsnbcat_s(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbsnbcat_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsnbcpy_s(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbsnbcpy_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsnbset_s(unsigned char *_Dst,size_t _DstSizeInBytes,unsigned int _Ch,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbsnbset_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,unsigned int _Ch,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsncat_s(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbsncat_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsncpy_s(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbsncpy_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,const unsigned char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsnset_s(unsigned char *_Dst,size_t _DstSizeInBytes,unsigned int _Val,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbsnset_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,unsigned int _Val,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsset_s(unsigned char *_Dst,size_t _DstSizeInBytes,unsigned int _Val); + _CRTIMP errno_t __cdecl _mbsset_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,unsigned int _Val,_locale_t _Locale); + _CRTIMP unsigned char *__cdecl _mbstok_s(unsigned char *_Str,const unsigned char *_Delim,unsigned char **_Context); + _CRTIMP unsigned char *__cdecl _mbstok_s_l(unsigned char *_Str,const unsigned char *_Delim,unsigned char **_Context,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbsupr_s(unsigned char *_Str,size_t _SizeInBytes); + _CRTIMP errno_t __cdecl _mbsupr_s_l(unsigned char *_Str,size_t _SizeInBytes,_locale_t _Locale); + _CRTIMP errno_t __cdecl _mbccpy_s(unsigned char *_Dst,size_t _DstSizeInBytes,int *_PCopied,const unsigned char *_Src); + _CRTIMP errno_t __cdecl _mbccpy_s_l(unsigned char *_Dst,size_t _DstSizeInBytes,int *_PCopied,const unsigned char *_Src,_locale_t _Locale); +#endif + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/search_s.h b/05/tcc-final-old/win32/include/sec_api/search_s.h new file mode 100644 index 0000000..cae8998 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/search_s.h @@ -0,0 +1,25 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_SEARCH_S +#define _INC_SEARCH_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP void *__cdecl _lfind_s(const void *_Key,const void *_Base,unsigned int *_NumOfElements,size_t _SizeOfElements,int (__cdecl *_PtFuncCompare)(void *,const void *,const void *),void *_Context); + _CRTIMP void *__cdecl _lsearch_s(const void *_Key,void *_Base,unsigned int *_NumOfElements,size_t _SizeOfElements,int (__cdecl *_PtFuncCompare)(void *,const void *,const void *),void *_Context); + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/stdio_s.h b/05/tcc-final-old/win32/include/sec_api/stdio_s.h new file mode 100644 index 0000000..c9b803b --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/stdio_s.h @@ -0,0 +1,145 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STDIO_S +#define _INC_STDIO_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _STDIO_S_DEFINED +#define _STDIO_S_DEFINED + _CRTIMP errno_t __cdecl clearerr_s(FILE *_File); + int __cdecl fprintf_s(FILE *_File,const char *_Format,...); + size_t __cdecl fread_s(void *_DstBuf,size_t _DstSize,size_t _ElementSize,size_t _Count,FILE *_File); + _CRTIMP int __cdecl _fscanf_s_l(FILE *_File,const char *_Format,_locale_t _Locale,...); + int __cdecl printf_s(const char *_Format,...); + _CRTIMP int __cdecl _scanf_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _scanf_s_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snprintf_s(char *_DstBuf,size_t _DstSize,size_t _MaxCount,const char *_Format,...); + _CRTIMP int __cdecl _snprintf_c(char *_DstBuf,size_t _MaxCount,const char *_Format,...); + _CRTIMP int __cdecl _vsnprintf_c(char *_DstBuf,size_t _MaxCount,const char *_Format,va_list _ArgList); + int __cdecl sprintf_s(char *_DstBuf,size_t _DstSize,const char *_Format,...); + _CRTIMP int __cdecl _fscanf_l(FILE *_File,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _sscanf_l(const char *_Src,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _sscanf_s_l(const char *_Src,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snscanf_s(const char *_Src,size_t _MaxCount,const char *_Format,...); + _CRTIMP int __cdecl _snscanf_l(const char *_Src,size_t _MaxCount,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snscanf_s_l(const char *_Src,size_t _MaxCount,const char *_Format,_locale_t _Locale,...); + int __cdecl vfprintf_s(FILE *_File,const char *_Format,va_list _ArgList); + int __cdecl vprintf_s(const char *_Format,va_list _ArgList); + int __cdecl vsnprintf_s(char *_DstBuf,size_t _DstSize,size_t _MaxCount,const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _vsnprintf_s(char *_DstBuf,size_t _DstSize,size_t _MaxCount,const char *_Format,va_list _ArgList); + int __cdecl vsprintf_s(char *_DstBuf,size_t _Size,const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _fprintf_p(FILE *_File,const char *_Format,...); + _CRTIMP int __cdecl _printf_p(const char *_Format,...); + _CRTIMP int __cdecl _sprintf_p(char *_Dst,size_t _MaxCount,const char *_Format,...); + _CRTIMP int __cdecl _vfprintf_p(FILE *_File,const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _vprintf_p(const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _vsprintf_p(char *_Dst,size_t _MaxCount,const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _scprintf_p(const char *_Format,...); + _CRTIMP int __cdecl _vscprintf_p(const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _printf_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _printf_p_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vprintf_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vprintf_p_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fprintf_l(FILE *_File,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _fprintf_p_l(FILE *_File,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vfprintf_l(FILE *_File,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vfprintf_p_l(FILE *_File,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _sprintf_l(char *_DstBuf,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _sprintf_p_l(char *_DstBuf,size_t _MaxCount,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsprintf_l(char *_DstBuf,const char *_Format,_locale_t,va_list _ArgList); + _CRTIMP int __cdecl _vsprintf_p_l(char *_DstBuf,size_t _MaxCount,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _scprintf_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _scprintf_p_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vscprintf_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vscprintf_p_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _printf_s_l(const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vprintf_s_l(const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fprintf_s_l(FILE *_File,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vfprintf_s_l(FILE *_File,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _sprintf_s_l(char *_DstBuf,size_t _DstSize,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsprintf_s_l(char *_DstBuf,size_t _DstSize,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _snprintf_s_l(char *_DstBuf,size_t _DstSize,size_t _MaxCount,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsnprintf_s_l(char *_DstBuf,size_t _DstSize,size_t _MaxCount,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _snprintf_l(char *_DstBuf,size_t _MaxCount,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snprintf_c_l(char *_DstBuf,size_t _MaxCount,const char *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsnprintf_l(char *_DstBuf,size_t _MaxCount,const char *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vsnprintf_c_l(char *_DstBuf,size_t _MaxCount,const char *,_locale_t _Locale,va_list _ArgList); + +#ifndef _WSTDIO_S_DEFINED +#define _WSTDIO_S_DEFINED + _CRTIMP wchar_t *__cdecl _getws_s(wchar_t *_Str,size_t _SizeInWords); + int __cdecl fwprintf_s(FILE *_File,const wchar_t *_Format,...); + int __cdecl wprintf_s(const wchar_t *_Format,...); + int __cdecl vwprintf_s(const wchar_t *_Format,va_list _ArgList); + int __cdecl swprintf_s(wchar_t *_Dst,size_t _SizeInWords,const wchar_t *_Format,...); + int __cdecl vswprintf_s(wchar_t *_Dst,size_t _SizeInWords,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf_s(wchar_t *_DstBuf,size_t _DstSizeInWords,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vsnwprintf_s(wchar_t *_DstBuf,size_t _DstSizeInWords,size_t _MaxCount,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _wprintf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vwprintf_s_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwprintf_s_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vfwprintf_s_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _swprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vswprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsnwprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwscanf_s_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _swscanf_s_l(const wchar_t *_Src,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snwscanf_s(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _snwscanf_s_l(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _wscanf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP errno_t __cdecl _wfopen_s(FILE **_File,const wchar_t *_Filename,const wchar_t *_Mode); + _CRTIMP errno_t __cdecl _wfreopen_s(FILE **_File,const wchar_t *_Filename,const wchar_t *_Mode,FILE *_OldFile); + _CRTIMP errno_t __cdecl _wtmpnam_s(wchar_t *_DstBuf,size_t _SizeInWords); + _CRTIMP int __cdecl _fwprintf_p(FILE *_File,const wchar_t *_Format,...); + _CRTIMP int __cdecl _wprintf_p(const wchar_t *_Format,...); + _CRTIMP int __cdecl _vfwprintf_p(FILE *_File,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _vwprintf_p(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _swprintf_p(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vswprintf_p(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _scwprintf_p(const wchar_t *_Format,...); + _CRTIMP int __cdecl _vscwprintf_p(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _wprintf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _wprintf_p_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vwprintf_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwprintf_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _fwprintf_p_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vfwprintf_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vfwprintf_p_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _swprintf_c_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _swprintf_p_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vswprintf_c_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vswprintf_p_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _scwprintf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _scwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vscwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsnwprintf_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl __swprintf_l(wchar_t *_Dest,const wchar_t *_Format,_locale_t _Plocinfo,...); + _CRTIMP int __cdecl __vswprintf_l(wchar_t *_Dest,const wchar_t *_Format,_locale_t _Plocinfo,va_list _Args); + _CRTIMP int __cdecl _vscwprintf_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwscanf_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _swscanf_l(const wchar_t *_Src,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snwscanf_l(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _wscanf_l(const wchar_t *_Format,_locale_t _Locale,...); +#endif +#endif + + _CRTIMP size_t __cdecl _fread_nolock_s(void *_DstBuf,size_t _DstSize,size_t _ElementSize,size_t _Count,FILE *_File); + +#ifdef __cplusplus +} +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/stdlib_s.h b/05/tcc-final-old/win32/include/sec_api/stdlib_s.h new file mode 100644 index 0000000..f98262c --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/stdlib_s.h @@ -0,0 +1,67 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STDLIB_S +#define _INC_STDLIB_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP errno_t __cdecl _dupenv_s(char **_PBuffer,size_t *_PBufferSizeInBytes,const char *_VarName); + _CRTIMP errno_t __cdecl _itoa_s(int _Value,char *_DstBuf,size_t _Size,int _Radix); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _i64toa_s(__int64 _Val,char *_DstBuf,size_t _Size,int _Radix); + _CRTIMP errno_t __cdecl _ui64toa_s(unsigned __int64 _Val,char *_DstBuf,size_t _Size,int _Radix); +#endif + _CRTIMP errno_t __cdecl _ltoa_s(long _Val,char *_DstBuf,size_t _Size,int _Radix); + _CRTIMP errno_t __cdecl mbstowcs_s(size_t *_PtNumOfCharConverted,wchar_t *_DstBuf,size_t _SizeInWords,const char *_SrcBuf,size_t _MaxCount); + _CRTIMP errno_t __cdecl _mbstowcs_s_l(size_t *_PtNumOfCharConverted,wchar_t *_DstBuf,size_t _SizeInWords,const char *_SrcBuf,size_t _MaxCount,_locale_t _Locale); + _CRTIMP errno_t __cdecl _ultoa_s(unsigned long _Val,char *_DstBuf,size_t _Size,int _Radix); + _CRTIMP errno_t __cdecl _wctomb_s_l(int *_SizeConverted,char *_MbCh,size_t _SizeInBytes,wchar_t _WCh,_locale_t _Locale); + _CRTIMP errno_t __cdecl wcstombs_s(size_t *_PtNumOfCharConverted,char *_Dst,size_t _DstSizeInBytes,const wchar_t *_Src,size_t _MaxCountInBytes); + _CRTIMP errno_t __cdecl _wcstombs_s_l(size_t *_PtNumOfCharConverted,char *_Dst,size_t _DstSizeInBytes,const wchar_t *_Src,size_t _MaxCountInBytes,_locale_t _Locale); + +#ifndef _WSTDLIB_S_DEFINED +#define _WSTDLIB_S_DEFINED + _CRTIMP errno_t __cdecl _itow_s (int _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _ltow_s (long _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _ultow_s (unsigned long _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _wgetenv_s(size_t *_ReturnSize,wchar_t *_DstBuf,size_t _DstSizeInWords,const wchar_t *_VarName); + _CRTIMP errno_t __cdecl _wdupenv_s(wchar_t **_Buffer,size_t *_BufferSizeInWords,const wchar_t *_VarName); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _i64tow_s(__int64 _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _ui64tow_s(unsigned __int64 _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); +#endif +#endif + +#ifndef _POSIX_ + _CRTIMP errno_t __cdecl _ecvt_s(char *_DstBuf,size_t _Size,double _Val,int _NumOfDights,int *_PtDec,int *_PtSign); + _CRTIMP errno_t __cdecl _fcvt_s(char *_DstBuf,size_t _Size,double _Val,int _NumOfDec,int *_PtDec,int *_PtSign); + _CRTIMP errno_t __cdecl _gcvt_s(char *_DstBuf,size_t _Size,double _Val,int _NumOfDigits); + _CRTIMP errno_t __cdecl _makepath_s(char *_PathResult,size_t _Size,const char *_Drive,const char *_Dir,const char *_Filename,const char *_Ext); + _CRTIMP errno_t __cdecl _putenv_s(const char *_Name,const char *_Value); + _CRTIMP errno_t __cdecl _searchenv_s(const char *_Filename,const char *_EnvVar,char *_ResultPath,size_t _SizeInBytes); + _CRTIMP errno_t __cdecl _splitpath_s(const char *_FullPath,char *_Drive,size_t _DriveSize,char *_Dir,size_t _DirSize,char *_Filename,size_t _FilenameSize,char *_Ext,size_t _ExtSize); + +#ifndef _WSTDLIBP_S_DEFINED +#define _WSTDLIBP_S_DEFINED + _CRTIMP errno_t __cdecl _wmakepath_s(wchar_t *_PathResult,size_t _SizeInWords,const wchar_t *_Drive,const wchar_t *_Dir,const wchar_t *_Filename,const wchar_t *_Ext); + _CRTIMP errno_t __cdecl _wputenv_s(const wchar_t *_Name,const wchar_t *_Value); + _CRTIMP errno_t __cdecl _wsearchenv_s(const wchar_t *_Filename,const wchar_t *_EnvVar,wchar_t *_ResultPath,size_t _SizeInWords); + _CRTIMP errno_t __cdecl _wsplitpath_s(const wchar_t *_FullPath,wchar_t *_Drive,size_t _DriveSizeInWords,wchar_t *_Dir,size_t _DirSizeInWords,wchar_t *_Filename,size_t _FilenameSizeInWords,wchar_t *_Ext,size_t _ExtSizeInWords); +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/stralign_s.h b/05/tcc-final-old/win32/include/sec_api/stralign_s.h new file mode 100644 index 0000000..5b78f58 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/stralign_s.h @@ -0,0 +1,30 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef __STRALIGN_H_S_ +#define __STRALIGN_H_S_ + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(I_X86_) && defined(_WSTRING_S_DEFINED) +#if defined(__cplusplus) && defined(_WConst_Return) + static __inline PUWSTR ua_wcscpy_s(PUWSTR Destination,size_t DestinationSize,PCUWSTR Source) { + if(WSTR_ALIGNED(Source) && WSTR_ALIGNED(Destination)) return (wcscpy_s((PWSTR)Destination,DestinationSize,(PCWSTR)Source)==0 ? Destination : NULL); + return uaw_wcscpy((PCUWSTR)String,Character); + } +#endif +#endif + +#ifdef __cplusplus +} +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/string_s.h b/05/tcc-final-old/win32/include/sec_api/string_s.h new file mode 100644 index 0000000..9db70e7 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/string_s.h @@ -0,0 +1,41 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STRING_S +#define _INC_STRING_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP errno_t __cdecl _strset_s(char *_Dst,size_t _DstSize,int _Value); + _CRTIMP errno_t __cdecl _strerror_s(char *_Buf,size_t _SizeInBytes,const char *_ErrMsg); + _CRTIMP errno_t __cdecl _strlwr_s(char *_Str,size_t _Size); + _CRTIMP errno_t __cdecl _strlwr_s_l(char *_Str,size_t _Size,_locale_t _Locale); + _CRTIMP errno_t __cdecl _strnset_s(char *_Str,size_t _Size,int _Val,size_t _MaxCount); + _CRTIMP errno_t __cdecl _strupr_s(char *_Str,size_t _Size); + _CRTIMP errno_t __cdecl _strupr_s_l(char *_Str,size_t _Size,_locale_t _Locale); +#ifndef _WSTRING_S_DEFINED +#define _WSTRING_S_DEFINED + _CRTIMP wchar_t *__cdecl wcstok_s(wchar_t *_Str,const wchar_t *_Delim,wchar_t **_Context); + _CRTIMP errno_t __cdecl _wcserror_s(wchar_t *_Buf,size_t _SizeInWords,int _ErrNum); + _CRTIMP errno_t __cdecl __wcserror_s(wchar_t *_Buffer,size_t _SizeInWords,const wchar_t *_ErrMsg); + _CRTIMP errno_t __cdecl _wcsnset_s(wchar_t *_Dst,size_t _DstSizeInWords,wchar_t _Val,size_t _MaxCount); + _CRTIMP errno_t __cdecl _wcsset_s(wchar_t *_Str,size_t _SizeInWords,wchar_t _Val); + _CRTIMP errno_t __cdecl _wcslwr_s(wchar_t *_Str,size_t _SizeInWords); + _CRTIMP errno_t __cdecl _wcslwr_s_l(wchar_t *_Str,size_t _SizeInWords,_locale_t _Locale); + _CRTIMP errno_t __cdecl _wcsupr_s(wchar_t *_Str,size_t _Size); + _CRTIMP errno_t __cdecl _wcsupr_s_l(wchar_t *_Str,size_t _Size,_locale_t _Locale); +#endif + +#ifdef __cplusplus +} +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/sys/timeb_s.h b/05/tcc-final-old/win32/include/sec_api/sys/timeb_s.h new file mode 100644 index 0000000..af5ef09 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/sys/timeb_s.h @@ -0,0 +1,34 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ + +#ifndef _TIMEB_H_S +#define _TIMEB_H_S + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef _USE_32BIT_TIME_T +#define _ftime_s _ftime32_s +#else +#define _ftime_s _ftime64_s +#endif + + _CRTIMP errno_t __cdecl _ftime32_s(struct __timeb32 *_Time); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _ftime64_s(struct __timeb64 *_Time); +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/tchar_s.h b/05/tcc-final-old/win32/include/sec_api/tchar_s.h new file mode 100644 index 0000000..343d348 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/tchar_s.h @@ -0,0 +1,266 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_TCHAR_S +#define _INC_TCHAR_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef _UNICODE + +#define _tprintf_s wprintf_s +#define _tprintf_s_l _wprintf_s_l +#define _tcprintf_s _cwprintf_s +#define _tcprintf_s_l _cwprintf_s_l +#define _vtcprintf_s _vcwprintf_s +#define _vtcprintf_s_l _vcwprintf_s_l +#define _ftprintf_s fwprintf_s +#define _ftprintf_s_l _fwprintf_s_l +#define _stprintf_s swprintf_s +#define _stprintf_s_l _swprintf_s_l +#define _sntprintf_s _snwprintf_s +#define _sntprintf_s_l _snwprintf_s_l +#define _vtprintf_s vwprintf_s +#define _vtprintf_s_l _vwprintf_s_l +#define _vftprintf_s vfwprintf_s +#define _vftprintf_s_l _vfwprintf_s_l +#define _vstprintf_s vswprintf_s +#define _vstprintf_s_l _vswprintf_s_l +#define _vsntprintf_s _vsnwprintf_s +#define _vsntprintf_s_l _vsnwprintf_s_l + +#define _tscanf_s wscanf_s +#define _tscanf_s_l _wscanf_s_l +#define _tcscanf_s _cwscanf_s +#define _tcscanf_s_l _cwscanf_s_l +#define _ftscanf_s fwscanf_s +#define _ftscanf_s_l _fwscanf_s_l +#define _stscanf_s swscanf_s +#define _stscanf_s_l _swscanf_s_l +#define _sntscanf_s _snwscanf_s +#define _sntscanf_s_l _snwscanf_s_l + +#define _cgetts_s _cgetws_s +#define _getts_s _getws_s + +#define _itot_s _itow_s +#define _ltot_s _ltow_s +#define _ultot_s _ultow_s +#define _i64tot_s _i64tow_s +#define _ui64tot_s _ui64tow_s + +#define _tcscat_s wcscat_s +#define _tcscpy_s wcscpy_s +#define _tcsncat_s wcsncat_s +#define _tcsncat_s_l _wcsncat_s_l +#define _tcsncpy_s wcsncpy_s +#define _tcsncpy_s_l _wcsncpy_s_l +#define _tcstok_s wcstok_s +#define _tcstok_s_l _wcstok_s_l +#define _tcserror_s _wcserror_s +#define __tcserror_s __wcserror_s + +#define _tcsnset_s _wcsnset_s +#define _tcsnset_s_l _wcsnset_s_l +#define _tcsset_s _wcsset_s +#define _tcsset_s_l _wcsset_s_l + +#define _tasctime_s _wasctime_s +#define _tctime_s _wctime_s +#define _tctime32_s _wctime32_s +#define _tctime64_s _wctime64_s +#define _tstrdate_s _wstrdate_s +#define _tstrtime_s _wstrtime_s + +#define _tgetenv_s _wgetenv_s +#define _tdupenv_s _wdupenv_s +#define _tmakepath_s _wmakepath_s +#define _tputenv_s _wputenv_s +#define _tsearchenv_s _wsearchenv_s +#define _tsplitpath_s _wsplitpath_s + +#define _tfopen_s _wfopen_s +#define _tfreopen_s _wfreopen_s +#define _ttmpnam_s _wtmpnam_s +#define _taccess_s _waccess_s +#define _tmktemp_s _wmktemp_s + +#define _tcsnccat_s wcsncat_s +#define _tcsnccat_s_l _wcsncat_s_l +#define _tcsnccpy_s wcsncpy_s +#define _tcsnccpy_s_l _wcsncpy_s_l + +#define _tcslwr_s _wcslwr_s +#define _tcslwr_s_l _wcslwr_s_l +#define _tcsupr_s _wcsupr_s +#define _tcsupr_s_l _wcsupr_s_l + +#define _wcstok_s_l(_String,_Delimiters,_Current_position,_Locale) (wcstok_s(_String,_Delimiters,_Current_position)) +#define _wcsnset_s_l(_Destination,_Destination_size_chars,_Value,_Count,_Locale) (_wcsnset_s(_Destination,_Destination_size_chars,_Value,_Count)) +#define _wcsset_s_l(_Destination,_Destination_size_chars,_Value,_Locale) (_wcsset_s(_Destination,_Destination_size_chars,_Value)) + +#else + +#define _tprintf_s printf_s +#define _tprintf_s_l _printf_s_l +#define _tcprintf_s _cprintf_s +#define _tcprintf_s_l _cprintf_s_l +#define _vtcprintf_s _vcprintf_s +#define _vtcprintf_s_l _vcprintf_s_l +#define _ftprintf_s fprintf_s +#define _ftprintf_s_l _fprintf_s_l +#define _stprintf_s sprintf_s +#define _stprintf_s_l _sprintf_s_l +#define _sntprintf_s _snprintf_s +#define _sntprintf_s_l _snprintf_s_l +#define _vtprintf_s vprintf_s +#define _vtprintf_s_l _vprintf_s_l +#define _vftprintf_s vfprintf_s +#define _vftprintf_s_l _vfprintf_s_l +#define _vstprintf_s vsprintf_s +#define _vstprintf_s_l _vsprintf_s_l +#define _vsntprintf_s _vsnprintf_s +#define _vsntprintf_s_l _vsnprintf_s_l +#define _tscanf_s scanf_s +#define _tscanf_s_l _scanf_s_l +#define _tcscanf_s _cscanf_s +#define _tcscanf_s_l _cscanf_s_l +#define _ftscanf_s fscanf_s +#define _ftscanf_s_l _fscanf_s_l +#define _stscanf_s sscanf_s +#define _stscanf_s_l _sscanf_s_l +#define _sntscanf_s _snscanf_s +#define _sntscanf_s_l _snscanf_s_l + +#define _getts_s gets_s +#define _cgetts_s _cgets_s +#define _itot_s _itoa_s +#define _ltot_s _ltoa_s +#define _ultot_s _ultoa_s +#define _i64tot_s _i64toa_s +#define _ui64tot_s _ui64toa_s + +#define _tcscat_s strcat_s +#define _tcscpy_s strcpy_s +#define _tcserror_s strerror_s +#define __tcserror_s _strerror_s + +#define _tasctime_s asctime_s +#define _tctime_s ctime_s +#define _tctime32_s _ctime32_s +#define _tctime64_s _ctime64_s +#define _tstrdate_s _strdate_s +#define _tstrtime_s _strtime_s + +#define _tgetenv_s getenv_s +#define _tdupenv_s _dupenv_s +#define _tmakepath_s _makepath_s +#define _tputenv_s _putenv_s +#define _tsearchenv_s _searchenv_s +#define _tsplitpath_s _splitpath_s + +#define _tfopen_s fopen_s +#define _tfreopen_s freopen_s +#define _ttmpnam_s tmpnam_s +#define _tmktemp_s _mktemp_s + +#ifndef _POSIX_ +#define _taccess_s _access_s +#endif + +#define _tsopen_s _sopen_s + +#ifdef _MBCS + +#ifdef _MB_MAP_DIRECT + +#define _tcsncat_s _mbsnbcat_s +#define _tcsncat_s_l _mbsnbcat_s_l +#define _tcsncpy_s _mbsnbcpy_s +#define _tcsncpy_s_l _mbsnbcpy_s_l +#define _tcstok_s _mbstok_s +#define _tcstok_s_l _mbstok_s_l + +#define _tcsnset_s _mbsnbset_s +#define _tcsnset_s_l _mbsnbset_s_l +#define _tcsset_s _mbsset_s +#define _tcsset_s_l _mbsset_s_l + +#define _tcsnccat_s _mbsncat_s +#define _tcsnccat_s_l _mbsncat_s_l +#define _tcsnccpy_s _mbsncpy_s +#define _tcsnccpy_s_l _mbsncpy_s_l +#define _tcsncset_s _mbsnset_s +#define _tcsncset_s_l _mbsnset_s_l + +#define _tcslwr_s _mbslwr_s +#define _tcslwr_s_l _mbslwr_s_l +#define _tcsupr_s _mbsupr_s +#define _tcsupr_s_l _mbsupr_s_l + +#define _tccpy_s _mbccpy_s +#define _tccpy_s_l _mbccpy_s_l +#else + + _CRTIMP char *__cdecl _tcsncat_s(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsncat_s_l(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsncpy_s(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsncpy_s_l(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcstok_s(char *_Str,const char *_Delim,char **_Context); + _CRTIMP char *__cdecl _tcstok_s_l(char *_Str,const char *_Delim,char **_Context,_locale_t _Locale); + _CRTIMP errno_t __cdecl _tcsset_s(char *_Str,size_t _SizeInChars,unsigned int _Val); + _CRTIMP errno_t __cdecl _tcsset_s_l(char *_Str,size_t _SizeInChars,unsigned int,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsnccat_s(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsnccat_s_l(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsnccpy_s(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsnccpy_s_l(char *_Dst,size_t _DstSizeInChars,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcslwr_s(char *_Str,size_t _SizeInChars); + _CRTIMP char *__cdecl _tcslwr_s_l(char *_Str,size_t _SizeInChars,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsupr_s(char *_Str,size_t _SizeInChars); + _CRTIMP char *__cdecl _tcsupr_s_l(char *_Str,size_t _SizeInChars,_locale_t _Locale); + +#endif + +#else + +#define _tcsncat_s strncat_s +#define _tcsncat_s_l _strncat_s_l +#define _tcsncpy_s strncpy_s +#define _tcsncpy_s_l _strncpy_s_l +#define _tcstok_s strtok_s +#define _tcstok_s_l _strtok_s_l + +#define _tcsnset_s _strnset_s +#define _tcsnset_s_l _strnset_s_l +#define _tcsset_s _strset_s +#define _tcsset_s _strset_s +#define _tcsset_s_l _strset_s_l + +#define _tcsnccat_s strncat_s +#define _tcsnccat_s_l _strncat_s_l +#define _tcsnccpy_s strncpy_s +#define _tcsnccpy_s_l _strncpy_s_l + +#define _tcslwr_s _strlwr_s +#define _tcslwr_s_l _strlwr_s_l +#define _tcsupr_s _strupr_s +#define _tcsupr_s_l _strupr_s_l + +#define _strnset_s_l(_Destination,_Destination_size_chars,_Value,_Count,_Locale) (_strnset_s(_Destination,_Destination_size_chars,_Value,_Count)) +#define _strset_s_l(_Destination,_Destination_size_chars,_Value,_Locale) (_strset_s(_Destination,_Destination_size_chars,_Value)) +#endif +#endif + +#ifdef __cplusplus +} +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/time_s.h b/05/tcc-final-old/win32/include/sec_api/time_s.h new file mode 100644 index 0000000..9603b94 --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/time_s.h @@ -0,0 +1,61 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _TIME_H__S +#define _TIME_H__S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + + _CRTIMP errno_t __cdecl _ctime32_s(char *_Buf,size_t _SizeInBytes,const __time32_t *_Time); + _CRTIMP errno_t __cdecl _gmtime32_s(struct tm *_Tm,const __time32_t *_Time); + _CRTIMP errno_t __cdecl _localtime32_s(struct tm *_Tm,const __time32_t *_Time); + _CRTIMP errno_t __cdecl _strdate_s(char *_Buf,size_t _SizeInBytes); + _CRTIMP errno_t __cdecl _strtime_s(char *_Buf ,size_t _SizeInBytes); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _ctime64_s(char *_Buf,size_t _SizeInBytes,const __time64_t *_Time); + _CRTIMP errno_t __cdecl _gmtime64_s(struct tm *_Tm,const __time64_t *_Time); + _CRTIMP errno_t __cdecl _localtime64_s(struct tm *_Tm,const __time64_t *_Time); +#endif + +#ifndef _WTIME_S_DEFINED +#define _WTIME_S_DEFINED + _CRTIMP errno_t __cdecl _wasctime_s(wchar_t *_Buf,size_t _SizeInWords,const struct tm *_Tm); + _CRTIMP errno_t __cdecl _wctime32_s(wchar_t *_Buf,size_t _SizeInWords,const __time32_t *_Time); + _CRTIMP errno_t __cdecl _wstrdate_s(wchar_t *_Buf,size_t _SizeInWords); + _CRTIMP errno_t __cdecl _wstrtime_s(wchar_t *_Buf,size_t _SizeInWords); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _wctime64_s(wchar_t *_Buf,size_t _SizeInWords,const __time64_t *_Time); +#endif + +#if !defined (RC_INVOKED) && !defined (_INC_WTIME_S_INL) +#define _INC_WTIME_S_INL +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE errno_t __cdecl _wctime_s(wchar_t *_Buffer,size_t _SizeInWords,const time_t *_Time) { return _wctime32_s(_Buffer,_SizeInWords,_Time); } +#else +__CRT_INLINE errno_t __cdecl _wctime_s(wchar_t *_Buffer,size_t _SizeInWords,const time_t *_Time) { return _wctime64_s(_Buffer,_SizeInWords,_Time); } +#endif +#endif +#endif + +#ifndef RC_INVOKED +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE errno_t __cdecl localtime_s(struct tm *_Tm,const time_t *_Time) { return _localtime32_s(_Tm,_Time); } +#else +__CRT_INLINE errno_t __cdecl localtime_s(struct tm *_Tm,const time_t *_Time) { return _localtime64_s(_Tm,_Time); } +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/sec_api/wchar_s.h b/05/tcc-final-old/win32/include/sec_api/wchar_s.h new file mode 100644 index 0000000..94251aa --- /dev/null +++ b/05/tcc-final-old/win32/include/sec_api/wchar_s.h @@ -0,0 +1,128 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_WCHAR_S +#define _INC_WCHAR_S + +#include + +#if defined(MINGW_HAS_SECURE_API) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _WIO_S_DEFINED +#define _WIO_S_DEFINED + _CRTIMP errno_t __cdecl _waccess_s(const wchar_t *_Filename,int _AccessMode); + _CRTIMP errno_t __cdecl _wmktemp_s(wchar_t *_TemplateName,size_t _SizeInWords); +#endif + +#ifndef _WCONIO_S_DEFINED +#define _WCONIO_S_DEFINED + _CRTIMP errno_t __cdecl _cgetws_s(wchar_t *_Buffer,size_t _SizeInWords,size_t *_SizeRead); + _CRTIMP int __cdecl _cwprintf_s(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf_s(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_s(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_s_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); +#endif + +#ifndef _WSTDIO_S_DEFINED +#define _WSTDIO_S_DEFINED + _CRTIMP wchar_t *__cdecl _getws_s(wchar_t *_Str,size_t _SizeInWords); + int __cdecl fwprintf_s(FILE *_File,const wchar_t *_Format,...); + int __cdecl wprintf_s(const wchar_t *_Format,...); + int __cdecl vfwprintf_s(FILE *_File,const wchar_t *_Format,va_list _ArgList); + int __cdecl vwprintf_s(const wchar_t *_Format,va_list _ArgList); + int __cdecl swprintf_s(wchar_t *_Dst,size_t _SizeInWords,const wchar_t *_Format,...); + int __cdecl vswprintf_s(wchar_t *_Dst,size_t _SizeInWords,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf_s(wchar_t *_DstBuf,size_t _DstSizeInWords,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vsnwprintf_s(wchar_t *_DstBuf,size_t _DstSizeInWords,size_t _MaxCount,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _wprintf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vwprintf_s_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwprintf_s_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vfwprintf_s_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _swprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vswprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsnwprintf_s_l(wchar_t *_DstBuf,size_t _DstSize,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwscanf_s_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _swscanf_s_l(const wchar_t *_Src,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snwscanf_s(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _snwscanf_s_l(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _wscanf_s_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP errno_t __cdecl _wfopen_s(FILE **_File,const wchar_t *_Filename,const wchar_t *_Mode); + _CRTIMP errno_t __cdecl _wfreopen_s(FILE **_File,const wchar_t *_Filename,const wchar_t *_Mode,FILE *_OldFile); + _CRTIMP errno_t __cdecl _wtmpnam_s(wchar_t *_DstBuf,size_t _SizeInWords); +#endif + +#ifndef _WSTDLIB_S_DEFINED +#define _WSTDLIB_S_DEFINED + _CRTIMP errno_t __cdecl _itow_s (int _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _ltow_s (long _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _ultow_s (unsigned long _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _wgetenv_s(size_t *_ReturnSize,wchar_t *_DstBuf,size_t _DstSizeInWords,const wchar_t *_VarName); + _CRTIMP errno_t __cdecl _wdupenv_s(wchar_t **_Buffer,size_t *_BufferSizeInWords,const wchar_t *_VarName); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _i64tow_s(__int64 _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); + _CRTIMP errno_t __cdecl _ui64tow_s(unsigned __int64 _Val,wchar_t *_DstBuf,size_t _SizeInWords,int _Radix); +#endif +#endif + +#ifndef _POSIX_ +#ifndef _WSTDLIBP_S_DEFINED +#define _WSTDLIBP_S_DEFINED + _CRTIMP errno_t __cdecl _wmakepath_s(wchar_t *_PathResult,size_t _SizeInWords,const wchar_t *_Drive,const wchar_t *_Dir,const wchar_t *_Filename,const wchar_t *_Ext); + _CRTIMP errno_t __cdecl _wputenv_s(const wchar_t *_Name,const wchar_t *_Value); + _CRTIMP errno_t __cdecl _wsearchenv_s(const wchar_t *_Filename,const wchar_t *_EnvVar,wchar_t *_ResultPath,size_t _SizeInWords); + _CRTIMP errno_t __cdecl _wsplitpath_s(const wchar_t *_FullPath,wchar_t *_Drive,size_t _DriveSizeInWords,wchar_t *_Dir,size_t _DirSizeInWords,wchar_t *_Filename,size_t _FilenameSizeInWords,wchar_t *_Ext,size_t _ExtSizeInWords); +#endif +#endif + +#ifndef _WSTRING_S_DEFINED +#define _WSTRING_S_DEFINED + _CRTIMP wchar_t *__cdecl wcstok_s(wchar_t *_Str,const wchar_t *_Delim,wchar_t **_Context); + _CRTIMP errno_t __cdecl _wcserror_s(wchar_t *_Buf,size_t _SizeInWords,int _ErrNum); + _CRTIMP errno_t __cdecl __wcserror_s(wchar_t *_Buffer,size_t _SizeInWords,const wchar_t *_ErrMsg); + _CRTIMP errno_t __cdecl _wcsnset_s(wchar_t *_Dst,size_t _DstSizeInWords,wchar_t _Val,size_t _MaxCount); + _CRTIMP errno_t __cdecl _wcsset_s(wchar_t *_Str,size_t _SizeInWords,wchar_t _Val); + _CRTIMP errno_t __cdecl _wcslwr_s(wchar_t *_Str,size_t _SizeInWords); + _CRTIMP errno_t __cdecl _wcslwr_s_l(wchar_t *_Str,size_t _SizeInWords,_locale_t _Locale); + _CRTIMP errno_t __cdecl _wcsupr_s(wchar_t *_Str,size_t _Size); + _CRTIMP errno_t __cdecl _wcsupr_s_l(wchar_t *_Str,size_t _Size,_locale_t _Locale); +#endif + +#ifndef _WTIME_S_DEFINED +#define _WTIME_S_DEFINED + _CRTIMP errno_t __cdecl _wasctime_s(wchar_t *_Buf,size_t _SizeInWords,const struct tm *_Tm); + _CRTIMP errno_t __cdecl _wctime32_s(wchar_t *_Buf,size_t _SizeInWords,const __time32_t *_Time); + _CRTIMP errno_t __cdecl _wstrdate_s(wchar_t *_Buf,size_t _SizeInWords); + _CRTIMP errno_t __cdecl _wstrtime_s(wchar_t *_Buf,size_t _SizeInWords); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP errno_t __cdecl _wctime64_s(wchar_t *_Buf,size_t _SizeInWords,const __time64_t *_Time); +#endif + +#if !defined (RC_INVOKED) && !defined (_INC_WTIME_S_INL) +#define _INC_WTIME_S_INL +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE errno_t __cdecl _wctime_s(wchar_t *_Buffer,size_t _SizeInWords,const time_t *_Time) { return _wctime32_s(_Buffer,_SizeInWords,_Time); } +#else +__CRT_INLINE errno_t __cdecl _wctime_s(wchar_t *_Buffer,size_t _SizeInWords,const time_t *_Time) { return _wctime64_s(_Buffer,_SizeInWords,_Time); } +#endif +#endif +#endif + + _CRTIMP errno_t __cdecl mbsrtowcs_s(size_t *_Retval,wchar_t *_Dst,size_t _SizeInWords,const char **_PSrc,size_t _N,mbstate_t *_State); + _CRTIMP errno_t __cdecl wcrtomb_s(size_t *_Retval,char *_Dst,size_t _SizeInBytes,wchar_t _Ch,mbstate_t *_State); + _CRTIMP errno_t __cdecl wcsrtombs_s(size_t *_Retval,char *_Dst,size_t _SizeInBytes,const wchar_t **_Src,size_t _Size,mbstate_t *_State); + +#ifdef __cplusplus +} +#endif + +#endif +#endif diff --git a/05/tcc-final-old/win32/include/setjmp.h b/05/tcc-final-old/win32/include/setjmp.h new file mode 100644 index 0000000..e4f142a --- /dev/null +++ b/05/tcc-final-old/win32/include/setjmp.h @@ -0,0 +1,160 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_SETJMP +#define _INC_SETJMP + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#if (defined(_X86_) && !defined(__x86_64)) + +#define _JBLEN 16 +#define _JBTYPE int + + typedef struct __JUMP_BUFFER { + unsigned long Ebp; + unsigned long Ebx; + unsigned long Edi; + unsigned long Esi; + unsigned long Esp; + unsigned long Eip; + unsigned long Registration; + unsigned long TryLevel; + unsigned long Cookie; + unsigned long UnwindFunc; + unsigned long UnwindData[6]; + } _JUMP_BUFFER; +#elif defined(__ia64__) + typedef _CRT_ALIGN(16) struct _SETJMP_FLOAT128 { + __int64 LowPart; + __int64 HighPart; + } SETJMP_FLOAT128; + +#define _JBLEN 33 + typedef SETJMP_FLOAT128 _JBTYPE; + + typedef struct __JUMP_BUFFER { + + unsigned long iAReserved[6]; + + unsigned long Registration; + unsigned long TryLevel; + unsigned long Cookie; + unsigned long UnwindFunc; + + unsigned long UnwindData[6]; + + SETJMP_FLOAT128 FltS0; + SETJMP_FLOAT128 FltS1; + SETJMP_FLOAT128 FltS2; + SETJMP_FLOAT128 FltS3; + SETJMP_FLOAT128 FltS4; + SETJMP_FLOAT128 FltS5; + SETJMP_FLOAT128 FltS6; + SETJMP_FLOAT128 FltS7; + SETJMP_FLOAT128 FltS8; + SETJMP_FLOAT128 FltS9; + SETJMP_FLOAT128 FltS10; + SETJMP_FLOAT128 FltS11; + SETJMP_FLOAT128 FltS12; + SETJMP_FLOAT128 FltS13; + SETJMP_FLOAT128 FltS14; + SETJMP_FLOAT128 FltS15; + SETJMP_FLOAT128 FltS16; + SETJMP_FLOAT128 FltS17; + SETJMP_FLOAT128 FltS18; + SETJMP_FLOAT128 FltS19; + __int64 FPSR; + __int64 StIIP; + __int64 BrS0; + __int64 BrS1; + __int64 BrS2; + __int64 BrS3; + __int64 BrS4; + __int64 IntS0; + __int64 IntS1; + __int64 IntS2; + __int64 IntS3; + __int64 RsBSP; + __int64 RsPFS; + __int64 ApUNAT; + __int64 ApLC; + __int64 IntSp; + __int64 IntNats; + __int64 Preds; + + } _JUMP_BUFFER; +#elif defined(__x86_64) + typedef _CRT_ALIGN(16) struct _SETJMP_FLOAT128 { + unsigned __int64 Part[2]; + } SETJMP_FLOAT128; + +#define _JBLEN 16 + typedef SETJMP_FLOAT128 _JBTYPE; + + typedef struct _JUMP_BUFFER { + unsigned __int64 Frame; + unsigned __int64 Rbx; + unsigned __int64 Rsp; + unsigned __int64 Rbp; + unsigned __int64 Rsi; + unsigned __int64 Rdi; + unsigned __int64 R12; + unsigned __int64 R13; + unsigned __int64 R14; + unsigned __int64 R15; + unsigned __int64 Rip; + unsigned __int64 Spare; + SETJMP_FLOAT128 Xmm6; + SETJMP_FLOAT128 Xmm7; + SETJMP_FLOAT128 Xmm8; + SETJMP_FLOAT128 Xmm9; + SETJMP_FLOAT128 Xmm10; + SETJMP_FLOAT128 Xmm11; + SETJMP_FLOAT128 Xmm12; + SETJMP_FLOAT128 Xmm13; + SETJMP_FLOAT128 Xmm14; + SETJMP_FLOAT128 Xmm15; + } _JUMP_BUFFER; +#endif +#ifndef _JMP_BUF_DEFINED + typedef _JBTYPE jmp_buf[_JBLEN]; +#define _JMP_BUF_DEFINED +#endif + + void * __cdecl __attribute__ ((__nothrow__)) mingw_getsp(void); + +#ifdef USE_MINGW_SETJMP_TWO_ARGS +#ifndef _INC_SETJMPEX +#define setjmp(BUF) _setjmp((BUF),mingw_getsp()) + int __cdecl __attribute__ ((__nothrow__)) _setjmp(jmp_buf _Buf,void *_Ctx); +#else +#undef setjmp +#define setjmp(BUF) _setjmpex((BUF),mingw_getsp()) +#define setjmpex(BUF) _setjmpex((BUF),mingw_getsp()) + int __cdecl __attribute__ ((__nothrow__)) _setjmpex(jmp_buf _Buf,void *_Ctx); +#endif +#else +#ifndef _INC_SETJMPEX +#define setjmp _setjmp +#endif + int __cdecl __attribute__ ((__nothrow__)) setjmp(jmp_buf _Buf); +#endif + + __declspec(noreturn) __attribute__ ((__nothrow__)) void __cdecl ms_longjmp(jmp_buf _Buf,int _Value)/* throw(...)*/; + __declspec(noreturn) __attribute__ ((__nothrow__)) void __cdecl longjmp(jmp_buf _Buf,int _Value); + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/share.h b/05/tcc-final-old/win32/include/share.h new file mode 100644 index 0000000..358855f --- /dev/null +++ b/05/tcc-final-old/win32/include/share.h @@ -0,0 +1,28 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_SHARE +#define _INC_SHARE + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#define _SH_COMPAT 0x00 +#define _SH_DENYRW 0x10 +#define _SH_DENYWR 0x20 +#define _SH_DENYRD 0x30 +#define _SH_DENYNO 0x40 +#define _SH_SECURE 0x80 + +#ifndef NO_OLDNAMES +#define SH_COMPAT _SH_COMPAT +#define SH_DENYRW _SH_DENYRW +#define SH_DENYWR _SH_DENYWR +#define SH_DENYRD _SH_DENYRD +#define SH_DENYNO _SH_DENYNO +#endif + +#endif diff --git a/05/tcc-final-old/win32/include/signal.h b/05/tcc-final-old/win32/include/signal.h new file mode 100644 index 0000000..a518f6b --- /dev/null +++ b/05/tcc-final-old/win32/include/signal.h @@ -0,0 +1,63 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_SIGNAL +#define _INC_SIGNAL + +#include <_mingw.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _SIG_ATOMIC_T_DEFINED +#define _SIG_ATOMIC_T_DEFINED + typedef int sig_atomic_t; +#endif + +#define NSIG 23 + +#define SIGHUP 1 /* hangup */ +#define SIGINT 2 +#define SIGQUIT 3 /* quit */ +#define SIGILL 4 +#define SIGTRAP 5 /* trace trap (not reset when caught) */ +#define SIGIOT 6 /* IOT instruction */ +#define SIGABRT 6 /* used by abort, replace SIGIOT in the future */ +#define SIGEMT 7 /* EMT instruction */ +#define SIGFPE 8 +#define SIGKILL 9 /* kill (cannot be caught or ignored) */ +#define SIGBUS 10 /* bus error */ +#define SIGSEGV 11 +#define SIGSYS 12 /* bad argument to system call */ +#define SIGPIPE 13 /* write on a pipe with no one to read it */ +#ifdef __USE_MINGW_ALARM +#define SIGALRM 14 /* alarm clock */ +#endif +#define SIGTERM 15 +#define SIGBREAK 21 +#define SIGABRT2 22 + +#define SIGABRT_COMPAT 6 + + typedef void (*__p_sig_fn_t)(int); + +#define SIG_DFL (__p_sig_fn_t)0 +#define SIG_IGN (__p_sig_fn_t)1 +#define SIG_GET (__p_sig_fn_t)2 +#define SIG_SGE (__p_sig_fn_t)3 +#define SIG_ACK (__p_sig_fn_t)4 +#define SIG_ERR (__p_sig_fn_t)-1 + + extern void **__cdecl __pxcptinfoptrs(void); +#define _pxcptinfoptrs (*__pxcptinfoptrs()) + + __p_sig_fn_t __cdecl signal(int _SigNum,__p_sig_fn_t _Func); + int __cdecl raise(int _SigNum); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/stdint.h b/05/tcc-final-old/win32/include/stdint.h new file mode 100644 index 0000000..cde32b6 --- /dev/null +++ b/05/tcc-final-old/win32/include/stdint.h @@ -0,0 +1,212 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* ISO C9x 7.18 Integer types + * Based on ISO/IEC SC22/WG14 9899 Committee draft (SC22 N2794) + * + * THIS SOFTWARE IS NOT COPYRIGHTED + * + * Contributor: Danny Smith + * + * This source code is offered for use in the public domain. You may + * use, modify or distribute it freely. + * + * This code is distributed in the hope that it will be useful but + * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY + * DISCLAIMED. This includes but is not limited to warranties of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * Date: 2000-12-02 + */ + + +#ifndef _STDINT_H +#define _STDINT_H + +#include <_mingw.h> + +#define __need_wint_t +#define __need_wchar_t +#include "stddef.h" + +#ifndef __int8_t_defined +#define __int8_t_defined +/* 7.18.1.1 Exact-width integer types */ +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned uint32_t; +typedef long long int64_t; +typedef unsigned long long uint64_t; +#endif + +/* 7.18.1.2 Minimum-width integer types */ +typedef signed char int_least8_t; +typedef unsigned char uint_least8_t; +typedef short int_least16_t; +typedef unsigned short uint_least16_t; +typedef int int_least32_t; +typedef unsigned uint_least32_t; +typedef long long int_least64_t; +typedef unsigned long long uint_least64_t; + +/* 7.18.1.3 Fastest minimum-width integer types + * Not actually guaranteed to be fastest for all purposes + * Here we use the exact-width types for 8 and 16-bit ints. + */ +typedef char int_fast8_t; +typedef unsigned char uint_fast8_t; +typedef short int_fast16_t; +typedef unsigned short uint_fast16_t; +typedef int int_fast32_t; +typedef unsigned int uint_fast32_t; +typedef long long int_fast64_t; +typedef unsigned long long uint_fast64_t; + +/* 7.18.1.5 Greatest-width integer types */ +typedef long long intmax_t; +typedef unsigned long long uintmax_t; + +/* 7.18.2 Limits of specified-width integer types */ +#if !defined ( __cplusplus) || defined (__STDC_LIMIT_MACROS) + +/* 7.18.2.1 Limits of exact-width integer types */ +#define INT8_MIN (-128) +#define INT16_MIN (-32768) +#define INT32_MIN (-2147483647 - 1) +#define INT64_MIN (-9223372036854775807LL - 1) + +#define INT8_MAX 127 +#define INT16_MAX 32767 +#define INT32_MAX 2147483647 +#define INT64_MAX 9223372036854775807LL + +#define UINT8_MAX 0xff /* 255U */ +#define UINT16_MAX 0xffff /* 65535U */ +#define UINT32_MAX 0xffffffff /* 4294967295U */ +#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */ + +/* 7.18.2.2 Limits of minimum-width integer types */ +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST64_MIN INT64_MIN + +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MAX INT64_MAX + +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +/* 7.18.2.3 Limits of fastest minimum-width integer types */ +#define INT_FAST8_MIN INT8_MIN +#define INT_FAST16_MIN INT16_MIN +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST64_MIN INT64_MIN + +#define INT_FAST8_MAX INT8_MAX +#define INT_FAST16_MAX INT16_MAX +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MAX INT64_MAX + +#define UINT_FAST8_MAX UINT8_MAX +#define UINT_FAST16_MAX UINT16_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +/* 7.18.2.4 Limits of integer types capable of holding + object pointers */ +#ifdef _WIN64 +#define INTPTR_MIN INT64_MIN +#define INTPTR_MAX INT64_MAX +#define UINTPTR_MAX UINT64_MAX +#else +#define INTPTR_MIN INT32_MIN +#define INTPTR_MAX INT32_MAX +#define UINTPTR_MAX UINT32_MAX +#endif + +/* 7.18.2.5 Limits of greatest-width integer types */ +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +/* 7.18.3 Limits of other integer types */ +#ifdef _WIN64 +#define PTRDIFF_MIN INT64_MIN +#define PTRDIFF_MAX INT64_MAX +#else +#define PTRDIFF_MIN INT32_MIN +#define PTRDIFF_MAX INT32_MAX +#endif + +#define SIG_ATOMIC_MIN INT32_MIN +#define SIG_ATOMIC_MAX INT32_MAX + +#ifndef SIZE_MAX +#ifdef _WIN64 +#define SIZE_MAX UINT64_MAX +#else +#define SIZE_MAX UINT32_MAX +#endif +#endif + +#ifndef WCHAR_MIN /* also in wchar.h */ +#define WCHAR_MIN 0 +#define WCHAR_MAX ((wchar_t)-1) /* UINT16_MAX */ +#endif + +/* + * wint_t is unsigned short for compatibility with MS runtime + */ +#define WINT_MIN 0 +#define WINT_MAX ((wint_t)-1) /* UINT16_MAX */ + +#endif /* !defined ( __cplusplus) || defined __STDC_LIMIT_MACROS */ + + +/* 7.18.4 Macros for integer constants */ +#if !defined ( __cplusplus) || defined (__STDC_CONSTANT_MACROS) + +/* 7.18.4.1 Macros for minimum-width integer constants + + According to Douglas Gwyn : + "This spec was changed in ISO/IEC 9899:1999 TC1; in ISO/IEC + 9899:1999 as initially published, the expansion was required + to be an integer constant of precisely matching type, which + is impossible to accomplish for the shorter types on most + platforms, because C99 provides no standard way to designate + an integer constant with width less than that of type int. + TC1 changed this to require just an integer constant + *expression* with *promoted* type." + + The trick used here is from Clive D W Feather. +*/ + +#define INT8_C(val) (INT_LEAST8_MAX-INT_LEAST8_MAX+(val)) +#define INT16_C(val) (INT_LEAST16_MAX-INT_LEAST16_MAX+(val)) +#define INT32_C(val) (INT_LEAST32_MAX-INT_LEAST32_MAX+(val)) +/* The 'trick' doesn't work in C89 for long long because, without + suffix, (val) will be evaluated as int, not intmax_t */ +#define INT64_C(val) val##LL + +#define UINT8_C(val) (UINT_LEAST8_MAX-UINT_LEAST8_MAX+(val)) +#define UINT16_C(val) (UINT_LEAST16_MAX-UINT_LEAST16_MAX+(val)) +#define UINT32_C(val) (UINT_LEAST32_MAX-UINT_LEAST32_MAX+(val)) +#define UINT64_C(val) val##ULL + +/* 7.18.4.2 Macros for greatest-width integer constants */ +#define INTMAX_C(val) val##LL +#define UINTMAX_C(val) val##ULL + +#endif /* !defined ( __cplusplus) || defined __STDC_CONSTANT_MACROS */ + +#endif diff --git a/05/tcc-final-old/win32/include/stdio.h b/05/tcc-final-old/win32/include/stdio.h new file mode 100644 index 0000000..da88793 --- /dev/null +++ b/05/tcc-final-old/win32/include/stdio.h @@ -0,0 +1,429 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STDIO +#define _INC_STDIO + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#define BUFSIZ 512 +#define _NFILE _NSTREAM_ +#define _NSTREAM_ 512 +#define _IOB_ENTRIES 20 +#define EOF (-1) + +#ifndef _FILE_DEFINED + struct _iobuf { + char *_ptr; + int _cnt; + char *_base; + int _flag; + int _file; + int _charbuf; + int _bufsiz; + char *_tmpfname; + }; + typedef struct _iobuf FILE; +#define _FILE_DEFINED +#endif + +#ifdef _POSIX_ +#define _P_tmpdir "/" +#define _wP_tmpdir L"/" +#else +#define _P_tmpdir "\\" +#define _wP_tmpdir L"\\" +#endif + +#define L_tmpnam (sizeof(_P_tmpdir) + 12) + +#ifdef _POSIX_ +#define L_ctermid 9 +#define L_cuserid 32 +#endif + +#define SEEK_CUR 1 +#define SEEK_END 2 +#define SEEK_SET 0 + +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +#define FILENAME_MAX 260 +#define FOPEN_MAX 20 +#define _SYS_OPEN 20 +#define TMP_MAX 32767 + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#ifndef _OFF_T_DEFINED +#define _OFF_T_DEFINED +#ifndef _OFF_T_ +#define _OFF_T_ + typedef long _off_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long off_t; +#endif +#endif +#endif + +#ifndef _OFF64_T_DEFINED +#define _OFF64_T_DEFINED + typedef long long _off64_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long long off64_t; +#endif +#endif + +#ifndef _STDIO_DEFINED +#ifdef _WIN64 + _CRTIMP FILE *__cdecl __iob_func(void); +#else +#ifdef _MSVCRT_ +extern FILE _iob[]; /* A pointer to an array of FILE */ +#define __iob_func() (_iob) +#else +extern FILE (*_imp___iob)[]; /* A pointer to an array of FILE */ +#define __iob_func() (*_imp___iob) +#define _iob __iob_func() +#endif +#endif +#endif + +#ifndef _FPOS_T_DEFINED +#define _FPOS_T_DEFINED +#undef _FPOSOFF + +#if (!defined(NO_OLDNAMES) || defined(__GNUC__)) && _INTEGRAL_MAX_BITS >= 64 + typedef __int64 fpos_t; +#define _FPOSOFF(fp) ((long)(fp)) +#else + typedef long long fpos_t; +#define _FPOSOFF(fp) ((long)(fp)) +#endif + +#endif + +#ifndef _STDSTREAM_DEFINED +#define _STDSTREAM_DEFINED + +#define stdin (&__iob_func()[0]) +#define stdout (&__iob_func()[1]) +#define stderr (&__iob_func()[2]) +#endif + +#define _IOREAD 0x0001 +#define _IOWRT 0x0002 + +#define _IOFBF 0x0000 +#define _IOLBF 0x0040 +#define _IONBF 0x0004 + +#define _IOMYBUF 0x0008 +#define _IOEOF 0x0010 +#define _IOERR 0x0020 +#define _IOSTRG 0x0040 +#define _IORW 0x0080 +#ifdef _POSIX_ +#define _IOAPPEND 0x0200 +#endif + +#define _TWO_DIGIT_EXPONENT 0x1 + +#ifndef _STDIO_DEFINED + + _CRTIMP int __cdecl _filbuf(FILE *_File); + _CRTIMP int __cdecl _flsbuf(int _Ch,FILE *_File); +#ifdef _POSIX_ + _CRTIMP FILE *__cdecl _fsopen(const char *_Filename,const char *_Mode); +#else + _CRTIMP FILE *__cdecl _fsopen(const char *_Filename,const char *_Mode,int _ShFlag); +#endif + void __cdecl clearerr(FILE *_File); + int __cdecl fclose(FILE *_File); + _CRTIMP int __cdecl _fcloseall(void); +#ifdef _POSIX_ + FILE *__cdecl fdopen(int _FileHandle,const char *_Mode); +#else + _CRTIMP FILE *__cdecl _fdopen(int _FileHandle,const char *_Mode); +#endif + int __cdecl feof(FILE *_File); + int __cdecl ferror(FILE *_File); + int __cdecl fflush(FILE *_File); + int __cdecl fgetc(FILE *_File); + _CRTIMP int __cdecl _fgetchar(void); + int __cdecl fgetpos(FILE *_File ,fpos_t *_Pos); + char *__cdecl fgets(char *_Buf,int _MaxCount,FILE *_File); +#ifdef _POSIX_ + int __cdecl fileno(FILE *_File); +#else + _CRTIMP int __cdecl _fileno(FILE *_File); +#endif + _CRTIMP char *__cdecl _tempnam(const char *_DirName,const char *_FilePrefix); + _CRTIMP int __cdecl _flushall(void); + FILE *__cdecl fopen(const char *_Filename,const char *_Mode); + FILE *fopen64(const char *filename,const char *mode); + int __cdecl fprintf(FILE *_File,const char *_Format,...); + int __cdecl fputc(int _Ch,FILE *_File); + _CRTIMP int __cdecl _fputchar(int _Ch); + int __cdecl fputs(const char *_Str,FILE *_File); + size_t __cdecl fread(void *_DstBuf,size_t _ElementSize,size_t _Count,FILE *_File); + FILE *__cdecl freopen(const char *_Filename,const char *_Mode,FILE *_File); + int __cdecl fscanf(FILE *_File,const char *_Format,...); + int __cdecl fsetpos(FILE *_File,const fpos_t *_Pos); + int __cdecl fseek(FILE *_File,long _Offset,int _Origin); + int fseeko64(FILE* stream, _off64_t offset, int whence); + long __cdecl ftell(FILE *_File); + _off64_t ftello64(FILE * stream); + int __cdecl _fseeki64(FILE *_File,__int64 _Offset,int _Origin); + __int64 __cdecl _ftelli64(FILE *_File); + size_t __cdecl fwrite(const void *_Str,size_t _Size,size_t _Count,FILE *_File); + int __cdecl getc(FILE *_File); + int __cdecl getchar(void); + _CRTIMP int __cdecl _getmaxstdio(void); + char *__cdecl gets(char *_Buffer); + int __cdecl _getw(FILE *_File); +#ifndef _CRT_PERROR_DEFINED +#define _CRT_PERROR_DEFINED + void __cdecl perror(const char *_ErrMsg); +#endif + _CRTIMP int __cdecl _pclose(FILE *_File); + _CRTIMP FILE *__cdecl _popen(const char *_Command,const char *_Mode); +#if !defined(NO_OLDNAMES) && !defined(popen) +#define popen _popen +#define pclose _pclose +#endif + int __cdecl printf(const char *_Format,...); + int __cdecl putc(int _Ch,FILE *_File); + int __cdecl putchar(int _Ch); + int __cdecl puts(const char *_Str); + _CRTIMP int __cdecl _putw(int _Word,FILE *_File); +#ifndef _CRT_DIRECTORY_DEFINED +#define _CRT_DIRECTORY_DEFINED + int __cdecl remove(const char *_Filename); + int __cdecl rename(const char *_OldFilename,const char *_NewFilename); + _CRTIMP int __cdecl _unlink(const char *_Filename); +#ifndef NO_OLDNAMES + int __cdecl unlink(const char *_Filename); +#endif +#endif + void __cdecl rewind(FILE *_File); + _CRTIMP int __cdecl _rmtmp(void); + int __cdecl scanf(const char *_Format,...); + void __cdecl setbuf(FILE *_File,char *_Buffer); + _CRTIMP int __cdecl _setmaxstdio(int _Max); + _CRTIMP unsigned int __cdecl _set_output_format(unsigned int _Format); + _CRTIMP unsigned int __cdecl _get_output_format(void); + int __cdecl setvbuf(FILE *_File,char *_Buf,int _Mode,size_t _Size); + _CRTIMP int __cdecl _scprintf(const char *_Format,...); + int __cdecl sscanf(const char *_Src,const char *_Format,...); + _CRTIMP int __cdecl _snscanf(const char *_Src,size_t _MaxCount,const char *_Format,...); + FILE *__cdecl tmpfile(void); + char *__cdecl tmpnam(char *_Buffer); + int __cdecl ungetc(int _Ch,FILE *_File); + int __cdecl vfprintf(FILE *_File,const char *_Format,va_list _ArgList); + int __cdecl vprintf(const char *_Format,va_list _ArgList); + /* Make sure macros are not defined. */ +#pragma push_macro("vsnprintf") +#pragma push_macro("snprintf") +# undef vsnprintf +# undef snprintf + extern + __attribute__((format(gnu_printf, 3, 0))) __attribute__((nonnull (3))) + int __mingw_vsnprintf(char *_DstBuf,size_t _MaxCount,const char *_Format,va_list _ArgList); + extern + __attribute__((format(gnu_printf, 3, 4))) __attribute__((nonnull (3))) + int __mingw_snprintf(char* s, size_t n, const char* format, ...); + int __cdecl vsnprintf(char *_DstBuf,size_t _MaxCount,const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _snprintf(char *_Dest,size_t _Count,const char *_Format,...); + _CRTIMP int __cdecl _vsnprintf(char *_Dest,size_t _Count,const char *_Format,va_list _Args); + int __cdecl sprintf(char *_Dest,const char *_Format,...); + int __cdecl vsprintf(char *_Dest,const char *_Format,va_list _Args); +#ifndef __NO_ISOCEXT /* externs in libmingwex.a */ + int __cdecl snprintf(char* s, size_t n, const char* format, ...); + __CRT_INLINE int __cdecl vsnprintf (char* s, size_t n, const char* format,va_list arg) { + return _vsnprintf ( s, n, format, arg); + } + int __cdecl vscanf(const char * Format, va_list argp); + int __cdecl vfscanf (FILE * fp, const char * Format,va_list argp); + int __cdecl vsscanf (const char * _Str,const char * Format,va_list argp); +#endif +/* Restore may prior defined macros snprintf/vsnprintf. */ +#pragma pop_macro("snprintf") +#pragma pop_macro("vsnprintf") +/* Check if vsnprintf and snprintf are defaulting to gnu-style. */ +# if defined(USE_MINGW_GNU_SNPRINTF) && USE_MINGW_GNU_SNPRINTF +# ifndef vsnprint +# define vsnprintf __mingw_vsnprintf +# endif +# ifndef snprintf +# define snprintf __mingw_snprintf +# endif +# endif + _CRTIMP int __cdecl _vscprintf(const char *_Format,va_list _ArgList); + _CRTIMP int __cdecl _set_printf_count_output(int _Value); + _CRTIMP int __cdecl _get_printf_count_output(void); + +#ifndef _WSTDIO_DEFINED + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + +#ifdef _POSIX_ + _CRTIMP FILE *__cdecl _wfsopen(const wchar_t *_Filename,const wchar_t *_Mode); +#else + _CRTIMP FILE *__cdecl _wfsopen(const wchar_t *_Filename,const wchar_t *_Mode,int _ShFlag); +#endif + wint_t __cdecl fgetwc(FILE *_File); + _CRTIMP wint_t __cdecl _fgetwchar(void); + wint_t __cdecl fputwc(wchar_t _Ch,FILE *_File); + _CRTIMP wint_t __cdecl _fputwchar(wchar_t _Ch); + wint_t __cdecl getwc(FILE *_File); + wint_t __cdecl getwchar(void); + wint_t __cdecl putwc(wchar_t _Ch,FILE *_File); + wint_t __cdecl putwchar(wchar_t _Ch); + wint_t __cdecl ungetwc(wint_t _Ch,FILE *_File); + wchar_t *__cdecl fgetws(wchar_t *_Dst,int _SizeInWords,FILE *_File); + int __cdecl fputws(const wchar_t *_Str,FILE *_File); + _CRTIMP wchar_t *__cdecl _getws(wchar_t *_String); + _CRTIMP int __cdecl _putws(const wchar_t *_Str); + int __cdecl fwprintf(FILE *_File,const wchar_t *_Format,...); + int __cdecl wprintf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _scwprintf(const wchar_t *_Format,...); + int __cdecl vfwprintf(FILE *_File,const wchar_t *_Format,va_list _ArgList); + int __cdecl vwprintf(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl swprintf(wchar_t*, const wchar_t*, ...); + _CRTIMP int __cdecl vswprintf(wchar_t*, const wchar_t*,va_list); + _CRTIMP int __cdecl _swprintf_c(wchar_t *_DstBuf,size_t _SizeInWords,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vswprintf_c(wchar_t *_DstBuf,size_t _SizeInWords,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf(wchar_t *_Dest,size_t _Count,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vsnwprintf(wchar_t *_Dest,size_t _Count,const wchar_t *_Format,va_list _Args); +#ifndef __NO_ISOCEXT /* externs in libmingwex.a */ + int __cdecl snwprintf (wchar_t* s, size_t n, const wchar_t* format, ...); + __CRT_INLINE int __cdecl vsnwprintf (wchar_t* s, size_t n, const wchar_t* format, va_list arg) { return _vsnwprintf(s,n,format,arg); } + int __cdecl vwscanf (const wchar_t *, va_list); + int __cdecl vfwscanf (FILE *,const wchar_t *,va_list); + int __cdecl vswscanf (const wchar_t *,const wchar_t *,va_list); +#endif + _CRTIMP int __cdecl _swprintf(wchar_t *_Dest,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vswprintf(wchar_t *_Dest,const wchar_t *_Format,va_list _Args); + +#ifndef RC_INVOKED +#include +#endif + +#ifdef _CRT_NON_CONFORMING_SWPRINTFS +#ifndef __cplusplus +#define swprintf _swprintf +#define vswprintf _vswprintf +#define _swprintf_l __swprintf_l +#define _vswprintf_l __vswprintf_l +#endif +#endif + + _CRTIMP wchar_t *__cdecl _wtempnam(const wchar_t *_Directory,const wchar_t *_FilePrefix); + _CRTIMP int __cdecl _vscwprintf(const wchar_t *_Format,va_list _ArgList); + int __cdecl fwscanf(FILE *_File,const wchar_t *_Format,...); + int __cdecl swscanf(const wchar_t *_Src,const wchar_t *_Format,...); + _CRTIMP int __cdecl _snwscanf(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,...); + int __cdecl wscanf(const wchar_t *_Format,...); + _CRTIMP FILE *__cdecl _wfdopen(int _FileHandle ,const wchar_t *_Mode); + _CRTIMP FILE *__cdecl _wfopen(const wchar_t *_Filename,const wchar_t *_Mode); + _CRTIMP FILE *__cdecl _wfreopen(const wchar_t *_Filename,const wchar_t *_Mode,FILE *_OldFile); +#ifndef _CRT_WPERROR_DEFINED +#define _CRT_WPERROR_DEFINED + _CRTIMP void __cdecl _wperror(const wchar_t *_ErrMsg); +#endif + _CRTIMP FILE *__cdecl _wpopen(const wchar_t *_Command,const wchar_t *_Mode); +#if !defined(NO_OLDNAMES) && !defined(wpopen) +#define wpopen _wpopen +#endif + _CRTIMP int __cdecl _wremove(const wchar_t *_Filename); + _CRTIMP wchar_t *__cdecl _wtmpnam(wchar_t *_Buffer); + _CRTIMP wint_t __cdecl _fgetwc_nolock(FILE *_File); + _CRTIMP wint_t __cdecl _fputwc_nolock(wchar_t _Ch,FILE *_File); + _CRTIMP wint_t __cdecl _ungetwc_nolock(wint_t _Ch,FILE *_File); + +#undef _CRT_GETPUTWCHAR_NOINLINE + +#if !defined(__cplusplus) || defined(_CRT_GETPUTWCHAR_NOINLINE) +#define getwchar() fgetwc(stdin) +#define putwchar(_c) fputwc((_c),stdout) +#else + __CRT_INLINE wint_t __cdecl getwchar() { return (fgetwc(stdin)); } + __CRT_INLINE wint_t __cdecl putwchar(wchar_t _C) { return (fputwc(_C,stdout)); } +#endif + +#define getwc(_stm) fgetwc(_stm) +#define putwc(_c,_stm) fputwc(_c,_stm) +#define _putwc_nolock(_c,_stm) _fputwc_nolock(_c,_stm) +#define _getwc_nolock(_stm) _fgetwc_nolock(_stm) + +#define _WSTDIO_DEFINED +#endif + +#define _STDIO_DEFINED +#endif + +#define _fgetc_nolock(_stream) (--(_stream)->_cnt >= 0 ? 0xff & *(_stream)->_ptr++ : _filbuf(_stream)) +#define _fputc_nolock(_c,_stream) (--(_stream)->_cnt >= 0 ? 0xff & (*(_stream)->_ptr++ = (char)(_c)) : _flsbuf((_c),(_stream))) +#define _getc_nolock(_stream) _fgetc_nolock(_stream) +#define _putc_nolock(_c,_stream) _fputc_nolock(_c,_stream) +#define _getchar_nolock() _getc_nolock(stdin) +#define _putchar_nolock(_c) _putc_nolock((_c),stdout) +#define _getwchar_nolock() _getwc_nolock(stdin) +#define _putwchar_nolock(_c) _putwc_nolock((_c),stdout) + + _CRTIMP void __cdecl _lock_file(FILE *_File); + _CRTIMP void __cdecl _unlock_file(FILE *_File); + _CRTIMP int __cdecl _fclose_nolock(FILE *_File); + _CRTIMP int __cdecl _fflush_nolock(FILE *_File); + _CRTIMP size_t __cdecl _fread_nolock(void *_DstBuf,size_t _ElementSize,size_t _Count,FILE *_File); + _CRTIMP int __cdecl _fseek_nolock(FILE *_File,long _Offset,int _Origin); + _CRTIMP long __cdecl _ftell_nolock(FILE *_File); + _CRTIMP int __cdecl _fseeki64_nolock(FILE *_File,__int64 _Offset,int _Origin); + _CRTIMP __int64 __cdecl _ftelli64_nolock(FILE *_File); + _CRTIMP size_t __cdecl _fwrite_nolock(const void *_DstBuf,size_t _Size,size_t _Count,FILE *_File); + _CRTIMP int __cdecl _ungetc_nolock(int _Ch,FILE *_File); + +#if !defined(NO_OLDNAMES) || !defined(_POSIX) +#define P_tmpdir _P_tmpdir +#define SYS_OPEN _SYS_OPEN + + char *__cdecl tempnam(const char *_Directory,const char *_FilePrefix); + int __cdecl fcloseall(void); + FILE *__cdecl fdopen(int _FileHandle,const char *_Format); + int __cdecl fgetchar(void); + int __cdecl fileno(FILE *_File); + int __cdecl flushall(void); + int __cdecl fputchar(int _Ch); + int __cdecl getw(FILE *_File); + int __cdecl putw(int _Ch,FILE *_File); + int __cdecl rmtmp(void); +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) + +#include + +#endif diff --git a/05/tcc-final-old/win32/include/stdlib.h b/05/tcc-final-old/win32/include/stdlib.h new file mode 100644 index 0000000..96765b2 --- /dev/null +++ b/05/tcc-final-old/win32/include/stdlib.h @@ -0,0 +1,580 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STDLIB +#define _INC_STDLIB + +#include <_mingw.h> +#include + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 + +#ifndef _ONEXIT_T_DEFINED +#define _ONEXIT_T_DEFINED + + typedef int (__cdecl *_onexit_t)(void); + +#ifndef NO_OLDNAMES +#define onexit_t _onexit_t +#endif +#endif + +#ifndef _DIV_T_DEFINED +#define _DIV_T_DEFINED + + typedef struct _div_t { + int quot; + int rem; + } div_t; + + typedef struct _ldiv_t { + long quot; + long rem; + } ldiv_t; +#endif + +#ifndef _CRT_DOUBLE_DEC +#define _CRT_DOUBLE_DEC + +#pragma pack(4) + typedef struct { + unsigned char ld[10]; + } _LDOUBLE; +#pragma pack() + +#define _PTR_LD(x) ((unsigned char *)(&(x)->ld)) + + typedef struct { + double x; + } _CRT_DOUBLE; + + typedef struct { + float f; + } _CRT_FLOAT; + +#pragma push_macro("long") +#undef long + + typedef struct { + long double x; + } _LONGDOUBLE; + +#pragma pop_macro("long") + +#pragma pack(4) + typedef struct { + unsigned char ld12[12]; + } _LDBL12; +#pragma pack() +#endif + +#define RAND_MAX 0x7fff + +#ifndef MB_CUR_MAX +#define MB_CUR_MAX ___mb_cur_max_func() +#ifndef __mb_cur_max +#ifdef _MSVCRT_ + extern int __mb_cur_max; +#else +#define __mb_cur_max (*_imp____mb_cur_max) + extern int *_imp____mb_cur_max; +#endif +#endif +#ifdef _MSVCRT_ + extern int __mbcur_max; +#define ___mb_cur_max_func() (__mb_cur_max) +#else + extern int* _imp____mbcur_max; +#define ___mb_cur_max_func() (*_imp____mb_cur_max) +#endif +#endif + +#define __max(a,b) (((a) > (b)) ? (a) : (b)) +#define __min(a,b) (((a) < (b)) ? (a) : (b)) + +#define _MAX_PATH 260 +#define _MAX_DRIVE 3 +#define _MAX_DIR 256 +#define _MAX_FNAME 256 +#define _MAX_EXT 256 + +#define _OUT_TO_DEFAULT 0 +#define _OUT_TO_STDERR 1 +#define _OUT_TO_MSGBOX 2 +#define _REPORT_ERRMODE 3 + +#define _WRITE_ABORT_MSG 0x1 +#define _CALL_REPORTFAULT 0x2 + +#define _MAX_ENV 32767 + + typedef void (__cdecl *_purecall_handler)(void); + + _CRTIMP _purecall_handler __cdecl _set_purecall_handler(_purecall_handler _Handler); + _CRTIMP _purecall_handler __cdecl _get_purecall_handler(void); + + typedef void (__cdecl *_invalid_parameter_handler)(const wchar_t *,const wchar_t *,const wchar_t *,unsigned int,uintptr_t); + _invalid_parameter_handler __cdecl _set_invalid_parameter_handler(_invalid_parameter_handler _Handler); + _invalid_parameter_handler __cdecl _get_invalid_parameter_handler(void); + +#ifndef _CRT_ERRNO_DEFINED +#define _CRT_ERRNO_DEFINED + _CRTIMP extern int *__cdecl _errno(void); +#define errno (*_errno()) + errno_t __cdecl _set_errno(int _Value); + errno_t __cdecl _get_errno(int *_Value); +#endif + _CRTIMP unsigned long *__cdecl __doserrno(void); +#define _doserrno (*__doserrno()) + errno_t __cdecl _set_doserrno(unsigned long _Value); + errno_t __cdecl _get_doserrno(unsigned long *_Value); +#ifdef _MSVCRT_ + extern char *_sys_errlist[]; + extern int _sys_nerr; +#else + _CRTIMP char *_sys_errlist[1]; + _CRTIMP int _sys_nerr; +#endif +#if (defined(_X86_) && !defined(__x86_64)) + _CRTIMP int *__cdecl __p___argc(void); + _CRTIMP char ***__cdecl __p___argv(void); + _CRTIMP wchar_t ***__cdecl __p___wargv(void); + _CRTIMP char ***__cdecl __p__environ(void); + _CRTIMP wchar_t ***__cdecl __p__wenviron(void); + _CRTIMP char **__cdecl __p__pgmptr(void); + _CRTIMP wchar_t **__cdecl __p__wpgmptr(void); +#endif +#ifndef __argc +#ifdef _MSVCRT_ + extern int __argc; +#else +#define __argc (*_imp____argc) + extern int *_imp____argc; +#endif +#endif +#ifndef __argv +#ifdef _MSVCRT_ + extern char **__argv; +#else +#define __argv (*_imp____argv) + extern char ***_imp____argv; +#endif +#endif +#ifndef __wargv +#ifdef _MSVCRT_ + extern wchar_t **__wargv; +#else +#define __wargv (*_imp____wargv) + extern wchar_t ***_imp____wargv; +#endif +#endif + +#ifdef _POSIX_ + extern char **environ; +#else +#ifndef _environ +#ifdef _MSVCRT_ + extern char **_environ; +#else +#define _environ (*_imp___environ) + extern char ***_imp___environ; +#endif +#endif + +#ifndef _wenviron +#ifdef _MSVCRT_ + extern wchar_t **_wenviron; +#else +#define _wenviron (*_imp___wenviron) + extern wchar_t ***_imp___wenviron; +#endif +#endif +#endif +#ifndef _pgmptr +#ifdef _MSVCRT_ + extern char *_pgmptr; +#else +#define _pgmptr (*_imp___pgmptr) + extern char **_imp___pgmptr; +#endif +#endif + +#ifndef _wpgmptr +#ifdef _MSVCRT_ + extern wchar_t *_wpgmptr; +#else +#define _wpgmptr (*_imp___wpgmptr) + extern wchar_t **_imp___wpgmptr; +#endif +#endif + errno_t __cdecl _get_pgmptr(char **_Value); + errno_t __cdecl _get_wpgmptr(wchar_t **_Value); +#ifndef _fmode +#ifdef _MSVCRT_ + extern int _fmode; +#else +#define _fmode (*_imp___fmode) + extern int *_imp___fmode; +#endif +#endif + _CRTIMP errno_t __cdecl _set_fmode(int _Mode); + _CRTIMP errno_t __cdecl _get_fmode(int *_PMode); + +#ifndef _osplatform +#ifdef _MSVCRT_ + extern unsigned int _osplatform; +#else +#define _osplatform (*_imp___osplatform) + extern unsigned int *_imp___osplatform; +#endif +#endif + +#ifndef _osver +#ifdef _MSVCRT_ + extern unsigned int _osver; +#else +#define _osver (*_imp___osver) + extern unsigned int *_imp___osver; +#endif +#endif + +#ifndef _winver +#ifdef _MSVCRT_ + extern unsigned int _winver; +#else +#define _winver (*_imp___winver) + extern unsigned int *_imp___winver; +#endif +#endif + +#ifndef _winmajor +#ifdef _MSVCRT_ + extern unsigned int _winmajor; +#else +#define _winmajor (*_imp___winmajor) + extern unsigned int *_imp___winmajor; +#endif +#endif + +#ifndef _winminor +#ifdef _MSVCRT_ + extern unsigned int _winminor; +#else +#define _winminor (*_imp___winminor) + extern unsigned int *_imp___winminor; +#endif +#endif + + errno_t __cdecl _get_osplatform(unsigned int *_Value); + errno_t __cdecl _get_osver(unsigned int *_Value); + errno_t __cdecl _get_winver(unsigned int *_Value); + errno_t __cdecl _get_winmajor(unsigned int *_Value); + errno_t __cdecl _get_winminor(unsigned int *_Value); +#ifndef _countof +#ifndef __cplusplus +#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) +#else + extern "C++" { + template char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; +#define _countof(_Array) sizeof(*__countof_helper(_Array)) + } +#endif +#endif + +#ifndef _CRT_TERMINATE_DEFINED +#define _CRT_TERMINATE_DEFINED + void __cdecl __MINGW_NOTHROW exit(int _Code) __MINGW_ATTRIB_NORETURN; + _CRTIMP void __cdecl __MINGW_NOTHROW _exit(int _Code) __MINGW_ATTRIB_NORETURN; +#if !defined __NO_ISOCEXT /* extern stub in static libmingwex.a */ + /* C99 function name */ + void __cdecl _Exit(int) __MINGW_ATTRIB_NORETURN; + __CRT_INLINE __MINGW_ATTRIB_NORETURN void __cdecl _Exit(int status) + { _exit(status); } +#endif + +#pragma push_macro("abort") +#undef abort + void __cdecl __declspec(noreturn) abort(void); +#pragma pop_macro("abort") + +#endif + + _CRTIMP unsigned int __cdecl _set_abort_behavior(unsigned int _Flags,unsigned int _Mask); + +#ifndef _CRT_ABS_DEFINED +#define _CRT_ABS_DEFINED + int __cdecl abs(int _X); + long __cdecl labs(long _X); +#endif + +#if _INTEGRAL_MAX_BITS >= 64 + __int64 __cdecl _abs64(__int64); +#endif + int __cdecl atexit(void (__cdecl *)(void)); +#ifndef _CRT_ATOF_DEFINED +#define _CRT_ATOF_DEFINED + double __cdecl atof(const char *_String); + double __cdecl _atof_l(const char *_String,_locale_t _Locale); +#endif + int __cdecl atoi(const char *_Str); + _CRTIMP int __cdecl _atoi_l(const char *_Str,_locale_t _Locale); + long __cdecl atol(const char *_Str); + _CRTIMP long __cdecl _atol_l(const char *_Str,_locale_t _Locale); +#ifndef _CRT_ALGO_DEFINED +#define _CRT_ALGO_DEFINED + void *__cdecl bsearch(const void *_Key,const void *_Base,size_t _NumOfElements,size_t _SizeOfElements,int (__cdecl *_PtFuncCompare)(const void *,const void *)); + void __cdecl qsort(void *_Base,size_t _NumOfElements,size_t _SizeOfElements,int (__cdecl *_PtFuncCompare)(const void *,const void *)); +#endif + unsigned short __cdecl _byteswap_ushort(unsigned short _Short); + /*unsigned long __cdecl _byteswap_ulong (unsigned long _Long); */ +#if _INTEGRAL_MAX_BITS >= 64 + unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64 _Int64); +#endif + div_t __cdecl div(int _Numerator,int _Denominator); + char *__cdecl getenv(const char *_VarName); + _CRTIMP char *__cdecl _itoa(int _Value,char *_Dest,int _Radix); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP char *__cdecl _i64toa(__int64 _Val,char *_DstBuf,int _Radix); + _CRTIMP char *__cdecl _ui64toa(unsigned __int64 _Val,char *_DstBuf,int _Radix); + _CRTIMP __int64 __cdecl _atoi64(const char *_String); + _CRTIMP __int64 __cdecl _atoi64_l(const char *_String,_locale_t _Locale); + _CRTIMP __int64 __cdecl _strtoi64(const char *_String,char **_EndPtr,int _Radix); + _CRTIMP __int64 __cdecl _strtoi64_l(const char *_String,char **_EndPtr,int _Radix,_locale_t _Locale); + _CRTIMP unsigned __int64 __cdecl _strtoui64(const char *_String,char **_EndPtr,int _Radix); + _CRTIMP unsigned __int64 __cdecl _strtoui64_l(const char *_String,char **_EndPtr,int _Radix,_locale_t _Locale); +#endif + ldiv_t __cdecl ldiv(long _Numerator,long _Denominator); + _CRTIMP char *__cdecl _ltoa(long _Value,char *_Dest,int _Radix); + int __cdecl mblen(const char *_Ch,size_t _MaxCount); + _CRTIMP int __cdecl _mblen_l(const char *_Ch,size_t _MaxCount,_locale_t _Locale); + _CRTIMP size_t __cdecl _mbstrlen(const char *_Str); + _CRTIMP size_t __cdecl _mbstrlen_l(const char *_Str,_locale_t _Locale); + _CRTIMP size_t __cdecl _mbstrnlen(const char *_Str,size_t _MaxCount); + _CRTIMP size_t __cdecl _mbstrnlen_l(const char *_Str,size_t _MaxCount,_locale_t _Locale); + int __cdecl mbtowc(wchar_t *_DstCh,const char *_SrcCh,size_t _SrcSizeInBytes); + _CRTIMP int __cdecl _mbtowc_l(wchar_t *_DstCh,const char *_SrcCh,size_t _SrcSizeInBytes,_locale_t _Locale); + size_t __cdecl mbstowcs(wchar_t *_Dest,const char *_Source,size_t _MaxCount); + _CRTIMP size_t __cdecl _mbstowcs_l(wchar_t *_Dest,const char *_Source,size_t _MaxCount,_locale_t _Locale); + int __cdecl rand(void); + _CRTIMP int __cdecl _set_error_mode(int _Mode); + void __cdecl srand(unsigned int _Seed); + double __cdecl strtod(const char *_Str,char **_EndPtr); + float __cdecl strtof(const char *nptr, char **endptr); +#if !defined __NO_ISOCEXT /* in libmingwex.a */ + float __cdecl strtof (const char * __restrict__, char ** __restrict__); + long double __cdecl strtold(const char * __restrict__, char ** __restrict__); +#endif /* __NO_ISOCEXT */ + _CRTIMP double __cdecl _strtod_l(const char *_Str,char **_EndPtr,_locale_t _Locale); + long __cdecl strtol(const char *_Str,char **_EndPtr,int _Radix); + _CRTIMP long __cdecl _strtol_l(const char *_Str,char **_EndPtr,int _Radix,_locale_t _Locale); + unsigned long __cdecl strtoul(const char *_Str,char **_EndPtr,int _Radix); + _CRTIMP unsigned long __cdecl _strtoul_l(const char *_Str,char **_EndPtr,int _Radix,_locale_t _Locale); +#ifndef _CRT_SYSTEM_DEFINED +#define _CRT_SYSTEM_DEFINED + int __cdecl system(const char *_Command); +#endif + _CRTIMP char *__cdecl _ultoa(unsigned long _Value,char *_Dest,int _Radix); + int __cdecl wctomb(char *_MbCh,wchar_t _WCh); + _CRTIMP int __cdecl _wctomb_l(char *_MbCh,wchar_t _WCh,_locale_t _Locale); + size_t __cdecl wcstombs(char *_Dest,const wchar_t *_Source,size_t _MaxCount); + _CRTIMP size_t __cdecl _wcstombs_l(char *_Dest,const wchar_t *_Source,size_t _MaxCount,_locale_t _Locale); + +#ifndef _CRT_ALLOCATION_DEFINED +#define _CRT_ALLOCATION_DEFINED + void *__cdecl calloc(size_t _NumOfElements,size_t _SizeOfElements); + void __cdecl free(void *_Memory); + void *__cdecl malloc(size_t _Size); + void *__cdecl realloc(void *_Memory,size_t _NewSize); + _CRTIMP void *__cdecl _recalloc(void *_Memory,size_t _Count,size_t _Size); + //_CRTIMP void __cdecl _aligned_free(void *_Memory); + //_CRTIMP void *__cdecl _aligned_malloc(size_t _Size,size_t _Alignment); + _CRTIMP void *__cdecl _aligned_offset_malloc(size_t _Size,size_t _Alignment,size_t _Offset); + _CRTIMP void *__cdecl _aligned_realloc(void *_Memory,size_t _Size,size_t _Alignment); + _CRTIMP void *__cdecl _aligned_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment); + _CRTIMP void *__cdecl _aligned_offset_realloc(void *_Memory,size_t _Size,size_t _Alignment,size_t _Offset); + _CRTIMP void *__cdecl _aligned_offset_recalloc(void *_Memory,size_t _Count,size_t _Size,size_t _Alignment,size_t _Offset); +#endif + +#ifndef _WSTDLIB_DEFINED +#define _WSTDLIB_DEFINED + + _CRTIMP wchar_t *__cdecl _itow(int _Value,wchar_t *_Dest,int _Radix); + _CRTIMP wchar_t *__cdecl _ltow(long _Value,wchar_t *_Dest,int _Radix); + _CRTIMP wchar_t *__cdecl _ultow(unsigned long _Value,wchar_t *_Dest,int _Radix); + double __cdecl wcstod(const wchar_t *_Str,wchar_t **_EndPtr); + float __cdecl wcstof(const wchar_t *nptr, wchar_t **endptr); +#if !defined __NO_ISOCEXT /* in libmingwex.a */ + float __cdecl wcstof( const wchar_t * __restrict__, wchar_t ** __restrict__); + long double __cdecl wcstold(const wchar_t * __restrict__, wchar_t ** __restrict__); +#endif /* __NO_ISOCEXT */ + _CRTIMP double __cdecl _wcstod_l(const wchar_t *_Str,wchar_t **_EndPtr,_locale_t _Locale); + long __cdecl wcstol(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP long __cdecl _wcstol_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); + unsigned long __cdecl wcstoul(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP unsigned long __cdecl _wcstoul_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wgetenv(const wchar_t *_VarName); +#ifndef _CRT_WSYSTEM_DEFINED +#define _CRT_WSYSTEM_DEFINED + _CRTIMP int __cdecl _wsystem(const wchar_t *_Command); +#endif + _CRTIMP double __cdecl _wtof(const wchar_t *_Str); + _CRTIMP double __cdecl _wtof_l(const wchar_t *_Str,_locale_t _Locale); + _CRTIMP int __cdecl _wtoi(const wchar_t *_Str); + _CRTIMP int __cdecl _wtoi_l(const wchar_t *_Str,_locale_t _Locale); + _CRTIMP long __cdecl _wtol(const wchar_t *_Str); + _CRTIMP long __cdecl _wtol_l(const wchar_t *_Str,_locale_t _Locale); + +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP wchar_t *__cdecl _i64tow(__int64 _Val,wchar_t *_DstBuf,int _Radix); + _CRTIMP wchar_t *__cdecl _ui64tow(unsigned __int64 _Val,wchar_t *_DstBuf,int _Radix); + _CRTIMP __int64 __cdecl _wtoi64(const wchar_t *_Str); + _CRTIMP __int64 __cdecl _wtoi64_l(const wchar_t *_Str,_locale_t _Locale); + _CRTIMP __int64 __cdecl _wcstoi64(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP __int64 __cdecl _wcstoi64_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); + _CRTIMP unsigned __int64 __cdecl _wcstoui64(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP unsigned __int64 __cdecl _wcstoui64_l(const wchar_t *_Str ,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); +#endif +#endif + +#ifndef _POSIX_ +#define _CVTBUFSIZE (309+40) + _CRTIMP char *__cdecl _fullpath(char *_FullPath,const char *_Path,size_t _SizeInBytes); + _CRTIMP char *__cdecl _ecvt(double _Val,int _NumOfDigits,int *_PtDec,int *_PtSign); + _CRTIMP char *__cdecl _fcvt(double _Val,int _NumOfDec,int *_PtDec,int *_PtSign); + _CRTIMP char *__cdecl _gcvt(double _Val,int _NumOfDigits,char *_DstBuf); + _CRTIMP int __cdecl _atodbl(_CRT_DOUBLE *_Result,char *_Str); + _CRTIMP int __cdecl _atoldbl(_LDOUBLE *_Result,char *_Str); + _CRTIMP int __cdecl _atoflt(_CRT_FLOAT *_Result,char *_Str); + _CRTIMP int __cdecl _atodbl_l(_CRT_DOUBLE *_Result,char *_Str,_locale_t _Locale); + _CRTIMP int __cdecl _atoldbl_l(_LDOUBLE *_Result,char *_Str,_locale_t _Locale); + _CRTIMP int __cdecl _atoflt_l(_CRT_FLOAT *_Result,char *_Str,_locale_t _Locale); + unsigned long __cdecl _lrotl(unsigned long _Val,int _Shift); + unsigned long __cdecl _lrotr(unsigned long _Val,int _Shift); + _CRTIMP void __cdecl _makepath(char *_Path,const char *_Drive,const char *_Dir,const char *_Filename,const char *_Ext); + _onexit_t __cdecl _onexit(_onexit_t _Func); + +#ifndef _CRT_PERROR_DEFINED +#define _CRT_PERROR_DEFINED + void __cdecl perror(const char *_ErrMsg); +#endif + _CRTIMP int __cdecl _putenv(const char *_EnvString); + unsigned int __cdecl _rotl(unsigned int _Val,int _Shift); +#if _INTEGRAL_MAX_BITS >= 64 + unsigned __int64 __cdecl _rotl64(unsigned __int64 _Val,int _Shift); +#endif + unsigned int __cdecl _rotr(unsigned int _Val,int _Shift); +#if _INTEGRAL_MAX_BITS >= 64 + unsigned __int64 __cdecl _rotr64(unsigned __int64 _Val,int _Shift); +#endif + _CRTIMP void __cdecl _searchenv(const char *_Filename,const char *_EnvVar,char *_ResultPath); + _CRTIMP void __cdecl _splitpath(const char *_FullPath,char *_Drive,char *_Dir,char *_Filename,char *_Ext); + _CRTIMP void __cdecl _swab(char *_Buf1,char *_Buf2,int _SizeInBytes); + +#ifndef _WSTDLIBP_DEFINED +#define _WSTDLIBP_DEFINED + _CRTIMP wchar_t *__cdecl _wfullpath(wchar_t *_FullPath,const wchar_t *_Path,size_t _SizeInWords); + _CRTIMP void __cdecl _wmakepath(wchar_t *_ResultPath,const wchar_t *_Drive,const wchar_t *_Dir,const wchar_t *_Filename,const wchar_t *_Ext); +#ifndef _CRT_WPERROR_DEFINED +#define _CRT_WPERROR_DEFINED + _CRTIMP void __cdecl _wperror(const wchar_t *_ErrMsg); +#endif + _CRTIMP int __cdecl _wputenv(const wchar_t *_EnvString); + _CRTIMP void __cdecl _wsearchenv(const wchar_t *_Filename,const wchar_t *_EnvVar,wchar_t *_ResultPath); + _CRTIMP void __cdecl _wsplitpath(const wchar_t *_FullPath,wchar_t *_Drive,wchar_t *_Dir,wchar_t *_Filename,wchar_t *_Ext); +#endif + + _CRTIMP void __cdecl _beep(unsigned _Frequency,unsigned _Duration) __MINGW_ATTRIB_DEPRECATED; + /* Not to be confused with _set_error_mode (int). */ + _CRTIMP void __cdecl _seterrormode(int _Mode) __MINGW_ATTRIB_DEPRECATED; + _CRTIMP void __cdecl _sleep(unsigned long _Duration) __MINGW_ATTRIB_DEPRECATED; +#endif + +#ifndef NO_OLDNAMES +#ifndef _POSIX_ +#if 0 +#ifndef __cplusplus +#ifndef NOMINMAX +#ifndef max +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#endif +#ifndef min +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif +#endif +#endif +#endif + +#define sys_errlist _sys_errlist +#define sys_nerr _sys_nerr +#define environ _environ + char *__cdecl ecvt(double _Val,int _NumOfDigits,int *_PtDec,int *_PtSign); + char *__cdecl fcvt(double _Val,int _NumOfDec,int *_PtDec,int *_PtSign); + char *__cdecl gcvt(double _Val,int _NumOfDigits,char *_DstBuf); + char *__cdecl itoa(int _Val,char *_DstBuf,int _Radix); + char *__cdecl ltoa(long _Val,char *_DstBuf,int _Radix); + int __cdecl putenv(const char *_EnvString); + void __cdecl swab(char *_Buf1,char *_Buf2,int _SizeInBytes); + char *__cdecl ultoa(unsigned long _Val,char *_Dstbuf,int _Radix); + onexit_t __cdecl onexit(onexit_t _Func); +#endif +#endif + +#if !defined __NO_ISOCEXT /* externs in static libmingwex.a */ + + typedef struct { long long quot, rem; } lldiv_t; + + lldiv_t __cdecl lldiv(long long, long long); + + __CRT_INLINE long long __cdecl llabs(long long _j) { return (_j >= 0 ? _j : -_j); } + + long long __cdecl strtoll(const char* __restrict__, char** __restrict, int); + unsigned long long __cdecl strtoull(const char* __restrict__, char** __restrict__, int); + + /* these are stubs for MS _i64 versions */ + long long __cdecl atoll (const char *); + +#ifndef __STRICT_ANSI__ + long long __cdecl wtoll (const wchar_t *); + char *__cdecl lltoa (long long, char *, int); + char *__cdecl ulltoa (unsigned long long , char *, int); + wchar_t *__cdecl lltow (long long, wchar_t *, int); + wchar_t *__cdecl ulltow (unsigned long long, wchar_t *, int); + + /* __CRT_INLINE using non-ansi functions */ + __CRT_INLINE long long __cdecl atoll (const char * _c) { return _atoi64 (_c); } + __CRT_INLINE char *__cdecl lltoa (long long _n, char * _c, int _i) { return _i64toa (_n, _c, _i); } + __CRT_INLINE char *__cdecl ulltoa (unsigned long long _n, char * _c, int _i) { return _ui64toa (_n, _c, _i); } + __CRT_INLINE long long __cdecl wtoll (const wchar_t * _w) { return _wtoi64 (_w); } + __CRT_INLINE wchar_t *__cdecl lltow (long long _n, wchar_t * _w, int _i) { return _i64tow (_n, _w, _i); } + __CRT_INLINE wchar_t *__cdecl ulltow (unsigned long long _n, wchar_t * _w, int _i) { return _ui64tow (_n, _w, _i); } +#endif /* (__STRICT_ANSI__) */ + +#endif /* !__NO_ISOCEXT */ + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) + +#include +#include + +#endif diff --git a/05/tcc-final-old/win32/include/string.h b/05/tcc-final-old/win32/include/string.h new file mode 100644 index 0000000..3249dc3 --- /dev/null +++ b/05/tcc-final-old/win32/include/string.h @@ -0,0 +1,164 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STRING +#define _INC_STRING + +#include <_mingw.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _NLSCMP_DEFINED +#define _NLSCMP_DEFINED +#define _NLSCMPERROR 2147483647 +#endif + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#define _WConst_return _CONST_RETURN + +#ifndef _CRT_MEMORY_DEFINED +#define _CRT_MEMORY_DEFINED + _CRTIMP void *__cdecl _memccpy(void *_Dst,const void *_Src,int _Val,size_t _MaxCount); + _CONST_RETURN void *__cdecl memchr(const void *_Buf ,int _Val,size_t _MaxCount); + _CRTIMP int __cdecl _memicmp(const void *_Buf1,const void *_Buf2,size_t _Size); + _CRTIMP int __cdecl _memicmp_l(const void *_Buf1,const void *_Buf2,size_t _Size,_locale_t _Locale); + int __cdecl memcmp(const void *_Buf1,const void *_Buf2,size_t _Size); + void *__cdecl memcpy(void *_Dst,const void *_Src,size_t _Size); + void *__cdecl memset(void *_Dst,int _Val,size_t _Size); +#ifndef NO_OLDNAMES + void *__cdecl memccpy(void *_Dst,const void *_Src,int _Val,size_t _Size); + int __cdecl memicmp(const void *_Buf1,const void *_Buf2,size_t _Size); +#endif +#endif + char *__cdecl _strset(char *_Str,int _Val); + char *__cdecl strcpy(char *_Dest,const char *_Source); + char *__cdecl strcat(char *_Dest,const char *_Source); + int __cdecl strcmp(const char *_Str1,const char *_Str2); + size_t __cdecl strlen(const char *_Str); +#if 0 + size_t __cdecl strnlen(const char *_Str,size_t _MaxCount); +#endif + void *__cdecl memmove(void *_Dst,const void *_Src,size_t _Size); + _CRTIMP char *__cdecl _strdup(const char *_Src); + _CONST_RETURN char *__cdecl strchr(const char *_Str,int _Val); + _CRTIMP int __cdecl _stricmp(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _strcmpi(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _stricmp_l(const char *_Str1,const char *_Str2,_locale_t _Locale); + int __cdecl strcoll(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _strcoll_l(const char *_Str1,const char *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _stricoll(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _stricoll_l(const char *_Str1,const char *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _strncoll (const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _strncoll_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _strnicoll (const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _strnicoll_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + size_t __cdecl strcspn(const char *_Str,const char *_Control); + _CRTIMP char *__cdecl _strerror(const char *_ErrMsg); + char *__cdecl strerror(int); + _CRTIMP char *__cdecl _strlwr(char *_String); + char *strlwr_l(char *_String,_locale_t _Locale); + char *__cdecl strncat(char *_Dest,const char *_Source,size_t _Count); + int __cdecl strncmp(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _strnicmp(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _strnicmp_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + char *strncpy(char *_Dest,const char *_Source,size_t _Count); + _CRTIMP char *__cdecl _strnset(char *_Str,int _Val,size_t _MaxCount); + _CONST_RETURN char *__cdecl strpbrk(const char *_Str,const char *_Control); + _CONST_RETURN char *__cdecl strrchr(const char *_Str,int _Ch); + _CRTIMP char *__cdecl _strrev(char *_Str); + size_t __cdecl strspn(const char *_Str,const char *_Control); + _CONST_RETURN char *__cdecl strstr(const char *_Str,const char *_SubStr); + char *__cdecl strtok(char *_Str,const char *_Delim); + _CRTIMP char *__cdecl _strupr(char *_String); + _CRTIMP char *_strupr_l(char *_String,_locale_t _Locale); + size_t __cdecl strxfrm(char *_Dst,const char *_Src,size_t _MaxCount); + _CRTIMP size_t __cdecl _strxfrm_l(char *_Dst,const char *_Src,size_t _MaxCount,_locale_t _Locale); + +#ifndef NO_OLDNAMES + char *__cdecl strdup(const char *_Src); + int __cdecl strcmpi(const char *_Str1,const char *_Str2); + int __cdecl stricmp(const char *_Str1,const char *_Str2); + char *__cdecl strlwr(char *_Str); + int __cdecl strnicmp(const char *_Str1,const char *_Str,size_t _MaxCount); + __CRT_INLINE int __cdecl strncasecmp (const char *__sz1, const char *__sz2, size_t __sizeMaxCompare) { return _strnicmp (__sz1, __sz2, __sizeMaxCompare); } + __CRT_INLINE int __cdecl strcasecmp (const char *__sz1, const char *__sz2) { return _stricmp (__sz1, __sz2); } + char *__cdecl strnset(char *_Str,int _Val,size_t _MaxCount); + char *__cdecl strrev(char *_Str); + char *__cdecl strset(char *_Str,int _Val); + char *__cdecl strupr(char *_Str); +#endif + +#ifndef _WSTRING_DEFINED +#define _WSTRING_DEFINED + + _CRTIMP wchar_t *__cdecl _wcsdup(const wchar_t *_Str); + wchar_t *__cdecl wcscat(wchar_t *_Dest,const wchar_t *_Source); + _CONST_RETURN wchar_t *__cdecl wcschr(const wchar_t *_Str,wchar_t _Ch); + int __cdecl wcscmp(const wchar_t *_Str1,const wchar_t *_Str2); + wchar_t *__cdecl wcscpy(wchar_t *_Dest,const wchar_t *_Source); + size_t __cdecl wcscspn(const wchar_t *_Str,const wchar_t *_Control); + size_t __cdecl wcslen(const wchar_t *_Str); + size_t __cdecl wcsnlen(const wchar_t *_Src,size_t _MaxCount); + wchar_t *wcsncat(wchar_t *_Dest,const wchar_t *_Source,size_t _Count); + int __cdecl wcsncmp(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + wchar_t *wcsncpy(wchar_t *_Dest,const wchar_t *_Source,size_t _Count); + _CONST_RETURN wchar_t *__cdecl wcspbrk(const wchar_t *_Str,const wchar_t *_Control); + _CONST_RETURN wchar_t *__cdecl wcsrchr(const wchar_t *_Str,wchar_t _Ch); + size_t __cdecl wcsspn(const wchar_t *_Str,const wchar_t *_Control); + _CONST_RETURN wchar_t *__cdecl wcsstr(const wchar_t *_Str,const wchar_t *_SubStr); + wchar_t *__cdecl wcstok(wchar_t *_Str,const wchar_t *_Delim); + _CRTIMP wchar_t *__cdecl _wcserror(int _ErrNum); + _CRTIMP wchar_t *__cdecl __wcserror(const wchar_t *_Str); + _CRTIMP int __cdecl _wcsicmp(const wchar_t *_Str1,const wchar_t *_Str2); + _CRTIMP int __cdecl _wcsicmp_l(const wchar_t *_Str1,const wchar_t *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _wcsnicmp(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _wcsnicmp_l(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wcsnset(wchar_t *_Str,wchar_t _Val,size_t _MaxCount); + _CRTIMP wchar_t *__cdecl _wcsrev(wchar_t *_Str); + _CRTIMP wchar_t *__cdecl _wcsset(wchar_t *_Str,wchar_t _Val); + _CRTIMP wchar_t *__cdecl _wcslwr(wchar_t *_String); + _CRTIMP wchar_t *_wcslwr_l(wchar_t *_String,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wcsupr(wchar_t *_String); + _CRTIMP wchar_t *_wcsupr_l(wchar_t *_String,_locale_t _Locale); + size_t __cdecl wcsxfrm(wchar_t *_Dst,const wchar_t *_Src,size_t _MaxCount); + _CRTIMP size_t __cdecl _wcsxfrm_l(wchar_t *_Dst,const wchar_t *_Src,size_t _MaxCount,_locale_t _Locale); + int __cdecl wcscoll(const wchar_t *_Str1,const wchar_t *_Str2); + _CRTIMP int __cdecl _wcscoll_l(const wchar_t *_Str1,const wchar_t *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _wcsicoll(const wchar_t *_Str1,const wchar_t *_Str2); + _CRTIMP int __cdecl _wcsicoll_l(const wchar_t *_Str1,const wchar_t *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _wcsncoll(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _wcsncoll_l(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _wcsnicoll(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _wcsnicoll_l(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount,_locale_t _Locale); + +#ifndef NO_OLDNAMES + wchar_t *__cdecl wcsdup(const wchar_t *_Str); +#define wcswcs wcsstr + int __cdecl wcsicmp(const wchar_t *_Str1,const wchar_t *_Str2); + int __cdecl wcsnicmp(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + wchar_t *__cdecl wcsnset(wchar_t *_Str,wchar_t _Val,size_t _MaxCount); + wchar_t *__cdecl wcsrev(wchar_t *_Str); + wchar_t *__cdecl wcsset(wchar_t *_Str,wchar_t _Val); + wchar_t *__cdecl wcslwr(wchar_t *_Str); + wchar_t *__cdecl wcsupr(wchar_t *_Str); + int __cdecl wcsicoll(const wchar_t *_Str1,const wchar_t *_Str2); +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#include +#endif diff --git a/05/tcc-final-old/win32/include/sys/fcntl.h b/05/tcc-final-old/win32/include/sys/fcntl.h new file mode 100644 index 0000000..29fd55a --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/fcntl.h @@ -0,0 +1,13 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* + * This file is part of the Mingw32 package. + * + * This fcntl.h maps to the root fcntl.h + */ +#ifndef __STRICT_ANSI__ +#include +#endif diff --git a/05/tcc-final-old/win32/include/sys/file.h b/05/tcc-final-old/win32/include/sys/file.h new file mode 100644 index 0000000..370f352 --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/file.h @@ -0,0 +1,14 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* + * This file is part of the Mingw32 package. + * + * This file.h maps to the root fcntl.h + * TODO? + */ +#ifndef __STRICT_ANSI__ +#include +#endif diff --git a/05/tcc-final-old/win32/include/sys/locking.h b/05/tcc-final-old/win32/include/sys/locking.h new file mode 100644 index 0000000..e3fc85b --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/locking.h @@ -0,0 +1,30 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_LOCKING +#define _INC_LOCKING + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +/* All the headers include this file. */ +#include <_mingw.h> + +#define _LK_UNLCK 0 +#define _LK_LOCK 1 +#define _LK_NBLCK 2 +#define _LK_RLCK 3 +#define _LK_NBRLCK 4 + +#ifndef NO_OLDNAMES +#define LK_UNLCK _LK_UNLCK +#define LK_LOCK _LK_LOCK +#define LK_NBLCK _LK_NBLCK +#define LK_RLCK _LK_RLCK +#define LK_NBRLCK _LK_NBRLCK +#endif + +#endif diff --git a/05/tcc-final-old/win32/include/sys/stat.h b/05/tcc-final-old/win32/include/sys/stat.h new file mode 100644 index 0000000..344d4a2 --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/stat.h @@ -0,0 +1,290 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_STAT +#define _INC_STAT + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#include <_mingw.h> +#include + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CRTIMP +#define _CRTIMP __declspec(dllimport) +#endif + +#include + +#ifndef __TINYC__ /* gr */ +#ifdef _USE_32BIT_TIME_T +#ifdef _WIN64 +#undef _USE_32BIT_TIME_T +#endif +#else +#if _INTEGRAL_MAX_BITS < 64 +#define _USE_32BIT_TIME_T +#endif +#endif +#endif + +#ifndef _TIME32_T_DEFINED + typedef long __time32_t; +#define _TIME32_T_DEFINED +#endif + +#ifndef _TIME64_T_DEFINED +#if _INTEGRAL_MAX_BITS >= 64 + typedef __int64 __time64_t; +#endif +#define _TIME64_T_DEFINED +#endif + +#ifndef _TIME_T_DEFINED +#ifdef _USE_32BIT_TIME_T + typedef __time32_t time_t; +#else + typedef __time64_t time_t; +#endif +#define _TIME_T_DEFINED +#endif + +#ifndef _WCHAR_T_DEFINED + typedef unsigned short wchar_t; +#define _WCHAR_T_DEFINED +#endif + +#ifndef _STAT_DEFINED + +#ifdef _USE_32BIT_TIME_T +#ifndef _WIN64 +#define _fstat32 _fstat +#define _stat32 _stat +#define _wstat32 _wstat +#else +#define _fstat _fstat32 +#define _stat _stat32 +#define _wstat _wstat32 +#endif +#define _fstati64 _fstat32i64 +#define _stati64 _stat32i64 +#define _wstati64 _wstat32i64 +#else +#define _fstat _fstat64i32 +#define _fstati64 _fstat64 +#define _stat _stat64i32 +#define _stati64 _stat64 +#define _wstat _wstat64i32 +#define _wstati64 _wstat64 +#endif + + struct _stat32 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + _off_t st_size; + __time32_t st_atime; + __time32_t st_mtime; + __time32_t st_ctime; + }; + +#ifndef NO_OLDNAMES + struct stat { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + _off_t st_size; + time_t st_atime; + time_t st_mtime; + time_t st_ctime; + }; +#endif + +#if _INTEGRAL_MAX_BITS >= 64 + struct _stat32i64 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + __int64 st_size; + __time32_t st_atime; + __time32_t st_mtime; + __time32_t st_ctime; + }; + + struct _stat64i32 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + _off_t st_size; + __time64_t st_atime; + __time64_t st_mtime; + __time64_t st_ctime; + }; + + struct _stat64 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + __int64 st_size; + __time64_t st_atime; + __time64_t st_mtime; + __time64_t st_ctime; + }; +#endif + +#define __stat64 _stat64 + +#define _STAT_DEFINED +#endif + +#define _S_IFMT 0xF000 +#define _S_IFDIR 0x4000 +#define _S_IFCHR 0x2000 +#define _S_IFIFO 0x1000 +#define _S_IFREG 0x8000 +#define _S_IREAD 0x0100 +#define _S_IWRITE 0x0080 +#define _S_IEXEC 0x0040 + + _CRTIMP int __cdecl _fstat32(int _FileDes,struct _stat32 *_Stat); + _CRTIMP int __cdecl _stat32(const char *_Name,struct _stat32 *_Stat); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP int __cdecl _fstat64(int _FileDes,struct _stat64 *_Stat); + _CRTIMP int __cdecl _fstat32i64(int _FileDes,struct _stat32i64 *_Stat); + int __cdecl _fstat64i32(int _FileDes,struct _stat64i32 *_Stat); + __CRT_INLINE int __cdecl _fstat64i32(int _FileDes,struct _stat64i32 *_Stat) + { + struct _stat64 st; + int ret=_fstat64(_FileDes,&st); + _Stat->st_dev=st.st_dev; + _Stat->st_ino=st.st_ino; + _Stat->st_mode=st.st_mode; + _Stat->st_nlink=st.st_nlink; + _Stat->st_uid=st.st_uid; + _Stat->st_gid=st.st_gid; + _Stat->st_rdev=st.st_rdev; + _Stat->st_size=(_off_t) st.st_size; + _Stat->st_atime=st.st_atime; + _Stat->st_mtime=st.st_mtime; + _Stat->st_ctime=st.st_ctime; + return ret; + } + _CRTIMP int __cdecl _stat64(const char *_Name,struct _stat64 *_Stat); + _CRTIMP int __cdecl _stat32i64(const char *_Name,struct _stat32i64 *_Stat); + int __cdecl _stat64i32(const char *_Name,struct _stat64i32 *_Stat); + __CRT_INLINE int __cdecl _stat64i32(const char *_Name,struct _stat64i32 *_Stat) + { + struct _stat64 st; + int ret=_stat64(_Name,&st); + _Stat->st_dev=st.st_dev; + _Stat->st_ino=st.st_ino; + _Stat->st_mode=st.st_mode; + _Stat->st_nlink=st.st_nlink; + _Stat->st_uid=st.st_uid; + _Stat->st_gid=st.st_gid; + _Stat->st_rdev=st.st_rdev; + _Stat->st_size=(_off_t) st.st_size; + _Stat->st_atime=st.st_atime; + _Stat->st_mtime=st.st_mtime; + _Stat->st_ctime=st.st_ctime; + return ret; + } +#endif + +#ifndef _WSTAT_DEFINED +#define _WSTAT_DEFINED + _CRTIMP int __cdecl _wstat32(const wchar_t *_Name,struct _stat32 *_Stat); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP int __cdecl _wstat32i64(const wchar_t *_Name,struct _stat32i64 *_Stat); + int __cdecl _wstat64i32(const wchar_t *_Name,struct _stat64i32 *_Stat); + _CRTIMP int __cdecl _wstat64(const wchar_t *_Name,struct _stat64 *_Stat); +#endif +#endif + +#ifndef NO_OLDNAMES +#define _S_IFBLK 0x3000 /* Block: Is this ever set under w32? */ + +#define S_IFMT _S_IFMT +#define S_IFDIR _S_IFDIR +#define S_IFCHR _S_IFCHR +#define S_IFREG _S_IFREG +#define S_IREAD _S_IREAD +#define S_IWRITE _S_IWRITE +#define S_IEXEC _S_IEXEC +#define S_IFIFO _S_IFIFO +#define S_IFBLK _S_IFBLK + +#define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC) +#define _S_IXUSR _S_IEXEC +#define _S_IWUSR _S_IWRITE + +#define S_IRWXU _S_IRWXU +#define S_IXUSR _S_IXUSR +#define S_IWUSR _S_IWUSR +#define S_IRUSR _S_IRUSR +#define _S_IRUSR _S_IREAD + +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) + +#endif + +#if !defined (RC_INVOKED) && !defined (NO_OLDNAMES) +int __cdecl stat(const char *_Filename,struct stat *_Stat); +int __cdecl fstat(int _Desc,struct stat *_Stat); +int __cdecl wstat(const wchar_t *_Filename,struct stat *_Stat); +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE int __cdecl fstat(int _Desc,struct stat *_Stat) { + return _fstat32(_Desc,(struct _stat32 *)_Stat); +} +__CRT_INLINE int __cdecl stat(const char *_Filename,struct stat *_Stat) { + return _stat32(_Filename,(struct _stat32 *)_Stat); +} +#else +__CRT_INLINE int __cdecl fstat(int _Desc,struct stat *_Stat) { + return _fstat64i32(_Desc,(struct _stat64i32 *)_Stat); +} +__CRT_INLINE int __cdecl stat(const char *_Filename,struct stat *_Stat) { + return _stat64i32(_Filename,(struct _stat64i32 *)_Stat); +} +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/sys/time.h b/05/tcc-final-old/win32/include/sys/time.h new file mode 100644 index 0000000..8ccab83 --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/time.h @@ -0,0 +1,69 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ + +#ifndef _SYS_TIME_H_ +#define _SYS_TIME_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __STRICT_ANSI__ +#ifndef _TIMEVAL_DEFINED /* also in winsock[2].h */ +#define _TIMEVAL_DEFINED +struct timeval { + long tv_sec; + long tv_usec; +}; +#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) +#define timercmp(tvp, uvp, cmp) \ + (((tvp)->tv_sec != (uvp)->tv_sec) ? \ + ((tvp)->tv_sec cmp (uvp)->tv_sec) : \ + ((tvp)->tv_usec cmp (uvp)->tv_usec)) +#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 +#endif /* _TIMEVAL_DEFINED */ + +#ifndef _TIMEZONE_DEFINED /* also in sys/time.h */ +#define _TIMEZONE_DEFINED +/* Provided for compatibility with code that assumes that + the presence of gettimeofday function implies a definition + of struct timezone. */ +struct timezone +{ + int tz_minuteswest; /* of Greenwich */ + int tz_dsttime; /* type of dst correction to apply */ +}; + + extern int __cdecl mingw_gettimeofday (struct timeval *p, struct timezone *z); + +#endif + +/* + Implementation as per: + The Open Group Base Specifications, Issue 6 + IEEE Std 1003.1, 2004 Edition + + The timezone pointer arg is ignored. Errors are ignored. +*/ +#ifndef _GETTIMEOFDAY_DEFINED +#define _GETTIMEOFDAY_DEFINED +int __cdecl gettimeofday(struct timeval *__restrict__, + void *__restrict__ /* tzp (unused) */); +#endif + +#endif /* __STRICT_ANSI__ */ + +#ifdef __cplusplus +} +#endif + +/* Adding timespec definition. */ +#include + + +#endif /* _SYS_TIME_H_ */ diff --git a/05/tcc-final-old/win32/include/sys/timeb.h b/05/tcc-final-old/win32/include/sys/timeb.h new file mode 100644 index 0000000..3483773 --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/timeb.h @@ -0,0 +1,133 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _TIMEB_H_ +#define _TIMEB_H_ + +#include <_mingw.h> + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CRTIMP +#define _CRTIMP __declspec(dllimport) +#endif + +#ifndef __TINYC__ /* gr */ +#ifdef _USE_32BIT_TIME_T +#ifdef _WIN64 +#undef _USE_32BIT_TIME_T +#endif +#else +#if _INTEGRAL_MAX_BITS < 64 +#define _USE_32BIT_TIME_T +#endif +#endif +#endif + +#ifndef _TIME32_T_DEFINED + typedef long __time32_t; +#define _TIME32_T_DEFINED +#endif + +#ifndef _TIME64_T_DEFINED +#if _INTEGRAL_MAX_BITS >= 64 + typedef __int64 __time64_t; +#endif +#define _TIME64_T_DEFINED +#endif + +#ifndef _TIME_T_DEFINED +#ifdef _USE_32BIT_TIME_T + typedef __time32_t time_t; +#else + typedef __time64_t time_t; +#endif +#define _TIME_T_DEFINED +#endif + +#ifndef _TIMEB_DEFINED +#define _TIMEB_DEFINED + + struct __timeb32 { + __time32_t time; + unsigned short millitm; + short timezone; + short dstflag; + }; + +#ifndef NO_OLDNAMES + struct timeb { + time_t time; + unsigned short millitm; + short timezone; + short dstflag; + }; +#endif + +#if _INTEGRAL_MAX_BITS >= 64 + struct __timeb64 { + __time64_t time; + unsigned short millitm; + short timezone; + short dstflag; + }; +#endif + +#ifdef _USE_32BIT_TIME_T +#define _timeb __timeb32 +//gr #define _ftime _ftime32 +#define _ftime32 _ftime +#else +#define _timeb __timeb64 +#define _ftime _ftime64 +#endif +#endif + + _CRTIMP void __cdecl _ftime32(struct __timeb32 *_Time); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP void __cdecl _ftime64(struct __timeb64 *_Time); +#endif + +#ifndef _TIMESPEC_DEFINED +#define _TIMESPEC_DEFINED +struct timespec { + time_t tv_sec; /* Seconds */ + long tv_nsec; /* Nanoseconds */ +}; + +struct itimerspec { + struct timespec it_interval; /* Timer period */ + struct timespec it_value; /* Timer expiration */ +}; +#endif + +#if !defined (RC_INVOKED) && !defined (NO_OLDNAMES) +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE void __cdecl ftime(struct timeb *_Tmb) { + _ftime32((struct __timeb32 *)_Tmb); +} +#else +__CRT_INLINE void __cdecl ftime(struct timeb *_Tmb) { + _ftime64((struct __timeb64 *)_Tmb); +} +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) + +#include +#endif diff --git a/05/tcc-final-old/win32/include/sys/types.h b/05/tcc-final-old/win32/include/sys/types.h new file mode 100644 index 0000000..7379b0f --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/types.h @@ -0,0 +1,118 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_TYPES +#define _INC_TYPES + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#include <_mingw.h> + +#ifndef __TINYC__ /* gr */ +#ifdef _USE_32BIT_TIME_T +#ifdef _WIN64 +#undef _USE_32BIT_TIME_T +#endif +#else +#if _INTEGRAL_MAX_BITS < 64 +#define _USE_32BIT_TIME_T +#endif +#endif +#endif + +#ifndef _TIME32_T_DEFINED +#define _TIME32_T_DEFINED +typedef long __time32_t; +#endif + +#ifndef _TIME64_T_DEFINED +#define _TIME64_T_DEFINED +#if _INTEGRAL_MAX_BITS >= 64 +typedef __int64 __time64_t; +#endif +#endif + +#ifndef _TIME_T_DEFINED +#define _TIME_T_DEFINED +#ifdef _USE_32BIT_TIME_T +typedef __time32_t time_t; +#else +typedef __time64_t time_t; +#endif +#endif + +#ifndef _INO_T_DEFINED +#define _INO_T_DEFINED +typedef unsigned short _ino_t; +#ifndef NO_OLDNAMES +typedef unsigned short ino_t; +#endif +#endif + +#ifndef _DEV_T_DEFINED +#define _DEV_T_DEFINED +typedef unsigned int _dev_t; +#ifndef NO_OLDNAMES +typedef unsigned int dev_t; +#endif +#endif + +#ifndef _PID_T_ +#define _PID_T_ +#ifndef _WIN64 +typedef int _pid_t; +#else +typedef __int64 _pid_t; +#endif + +#ifndef NO_OLDNAMES +typedef _pid_t pid_t; +#endif +#endif /* Not _PID_T_ */ + +#ifndef _MODE_T_ +#define _MODE_T_ +typedef unsigned short _mode_t; + +#ifndef NO_OLDNAMES +typedef _mode_t mode_t; +#endif +#endif /* Not _MODE_T_ */ + +#ifndef _OFF_T_DEFINED +#define _OFF_T_DEFINED +#ifndef _OFF_T_ +#define _OFF_T_ + typedef long _off_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long off_t; +#endif +#endif +#endif + +#ifndef _OFF64_T_DEFINED +#define _OFF64_T_DEFINED + typedef long long _off64_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long long off64_t; +#endif +#endif + +#ifndef _TIMESPEC_DEFINED +#define _TIMESPEC_DEFINED +struct timespec { + time_t tv_sec; /* Seconds */ + long tv_nsec; /* Nanoseconds */ +}; + +struct itimerspec { + struct timespec it_interval; /* Timer period */ + struct timespec it_value; /* Timer expiration */ +}; +#endif + +#endif diff --git a/05/tcc-final-old/win32/include/sys/unistd.h b/05/tcc-final-old/win32/include/sys/unistd.h new file mode 100644 index 0000000..31006d3 --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/unistd.h @@ -0,0 +1,14 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +/* + * This file is part of the Mingw32 package. + * + * unistd.h maps (roughly) to io.h + */ +#ifndef __STRICT_ANSI__ +#include +#endif + diff --git a/05/tcc-final-old/win32/include/sys/utime.h b/05/tcc-final-old/win32/include/sys/utime.h new file mode 100644 index 0000000..fec8304 --- /dev/null +++ b/05/tcc-final-old/win32/include/sys/utime.h @@ -0,0 +1,146 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_UTIME +#define _INC_UTIME + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CRTIMP +#define _CRTIMP __declspec(dllimport) +#endif + +#ifndef _WCHAR_T_DEFINED + typedef unsigned short wchar_t; +#define _WCHAR_T_DEFINED +#endif + +#ifndef __TINYC__ /* gr */ +#ifdef _USE_32BIT_TIME_T +#ifdef _WIN64 +#undef _USE_32BIT_TIME_T +#endif +#else +#if _INTEGRAL_MAX_BITS < 64 +#define _USE_32BIT_TIME_T +#endif +#endif +#endif + +#ifndef _TIME32_T_DEFINED +#define _TIME32_T_DEFINED + typedef long __time32_t; +#endif + +#ifndef _TIME64_T_DEFINED +#define _TIME64_T_DEFINED +#if _INTEGRAL_MAX_BITS >= 64 + typedef __int64 __time64_t; +#endif +#endif + +#ifndef _TIME_T_DEFINED +#define _TIME_T_DEFINED +#ifdef _USE_32BIT_TIME_T + typedef __time32_t time_t; +#else + typedef __time64_t time_t; +#endif +#endif + +#ifndef _UTIMBUF_DEFINED +#define _UTIMBUF_DEFINED + + struct _utimbuf { + time_t actime; + time_t modtime; + }; + + struct __utimbuf32 { + __time32_t actime; + __time32_t modtime; + }; + +#if _INTEGRAL_MAX_BITS >= 64 + struct __utimbuf64 { + __time64_t actime; + __time64_t modtime; + }; +#endif + +#ifndef NO_OLDNAMES + struct utimbuf { + time_t actime; + time_t modtime; + }; + + struct utimbuf32 { + __time32_t actime; + __time32_t modtime; + }; +#endif +#endif + + _CRTIMP int __cdecl _utime32(const char *_Filename,struct __utimbuf32 *_Time); + _CRTIMP int __cdecl _futime32(int _FileDes,struct __utimbuf32 *_Time); + _CRTIMP int __cdecl _wutime32(const wchar_t *_Filename,struct __utimbuf32 *_Time); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP int __cdecl _utime64(const char *_Filename,struct __utimbuf64 *_Time); + _CRTIMP int __cdecl _futime64(int _FileDes,struct __utimbuf64 *_Time); + _CRTIMP int __cdecl _wutime64(const wchar_t *_Filename,struct __utimbuf64 *_Time); +#endif + +#ifndef RC_INVOKED +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE int __cdecl _utime(const char *_Filename,struct _utimbuf *_Utimbuf) { + return _utime32(_Filename,(struct __utimbuf32 *)_Utimbuf); +} +__CRT_INLINE int __cdecl _futime(int _Desc,struct _utimbuf *_Utimbuf) { + return _futime32(_Desc,(struct __utimbuf32 *)_Utimbuf); +} +__CRT_INLINE int __cdecl _wutime(const wchar_t *_Filename,struct _utimbuf *_Utimbuf) { + return _wutime32(_Filename,(struct __utimbuf32 *)_Utimbuf); +} +#else +__CRT_INLINE int __cdecl _utime(const char *_Filename,struct _utimbuf *_Utimbuf) { + return _utime64(_Filename,(struct __utimbuf64 *)_Utimbuf); +} +__CRT_INLINE int __cdecl _futime(int _Desc,struct _utimbuf *_Utimbuf) { + return _futime64(_Desc,(struct __utimbuf64 *)_Utimbuf); +} +__CRT_INLINE int __cdecl _wutime(const wchar_t *_Filename,struct _utimbuf *_Utimbuf) { + return _wutime64(_Filename,(struct __utimbuf64 *)_Utimbuf); +} +#endif + +#ifndef NO_OLDNAMES +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE int __cdecl utime(const char *_Filename,struct utimbuf *_Utimbuf) { + return _utime32(_Filename,(struct __utimbuf32 *)_Utimbuf); +} +#else +__CRT_INLINE int __cdecl utime(const char *_Filename,struct utimbuf *_Utimbuf) { + return _utime64(_Filename,(struct __utimbuf64 *)_Utimbuf); +} +#endif +#endif +#endif + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/tchar.h b/05/tcc-final-old/win32/include/tchar.h new file mode 100644 index 0000000..cd44bec --- /dev/null +++ b/05/tcc-final-old/win32/include/tchar.h @@ -0,0 +1,1102 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#include <_mingw.h> + +#ifndef _INC_TCHAR +#define _INC_TCHAR + +#ifdef _STRSAFE_H_INCLUDED_ +#error Need to include strsafe.h after tchar.h +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define _ftcscat _tcscat +#define _ftcschr _tcschr +#define _ftcscpy _tcscpy +#define _ftcscspn _tcscspn +#define _ftcslen _tcslen +#define _ftcsncat _tcsncat +#define _ftcsncpy _tcsncpy +#define _ftcspbrk _tcspbrk +#define _ftcsrchr _tcsrchr +#define _ftcsspn _tcsspn +#define _ftcsstr _tcsstr +#define _ftcstok _tcstok + +#define _ftcsdup _tcsdup +#define _ftcsnset _tcsnset +#define _ftcsrev _tcsrev +#define _ftcsset _tcsset + +#define _ftcscmp _tcscmp +#define _ftcsicmp _tcsicmp +#define _ftcsnccmp _tcsnccmp +#define _ftcsncmp _tcsncmp +#define _ftcsncicmp _tcsncicmp +#define _ftcsnicmp _tcsnicmp + +#define _ftcscoll _tcscoll +#define _ftcsicoll _tcsicoll +#define _ftcsnccoll _tcsnccoll +#define _ftcsncoll _tcsncoll +#define _ftcsncicoll _tcsncicoll +#define _ftcsnicoll _tcsnicoll + +#define _ftcsclen _tcsclen +#define _ftcsnccat _tcsnccat +#define _ftcsnccpy _tcsnccpy +#define _ftcsncset _tcsncset + +#define _ftcsdec _tcsdec +#define _ftcsinc _tcsinc +#define _ftcsnbcnt _tcsnbcnt +#define _ftcsnccnt _tcsnccnt +#define _ftcsnextc _tcsnextc +#define _ftcsninc _tcsninc +#define _ftcsspnp _tcsspnp + +#define _ftcslwr _tcslwr +#define _ftcsupr _tcsupr + +#define _ftclen _tclen +#define _ftccpy _tccpy +#define _ftccmp _tccmp + +#ifndef _CONST_RETURN +#ifdef __cplusplus +#define _CONST_RETURN const +#define _CRT_CONST_CORRECT_OVERLOADS +#else +#define _CONST_RETURN +#endif +#endif + +#define _WConst_return _CONST_RETURN + +#ifdef _UNICODE + +#ifdef __cplusplus +} +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _WCTYPE_T_DEFINED +#define _WCTYPE_T_DEFINED + typedef unsigned short wint_t; + typedef unsigned short wctype_t; +#endif + +#ifndef __TCHAR_DEFINED +#define __TCHAR_DEFINED + typedef wchar_t _TCHAR; + typedef wchar_t _TSCHAR; + typedef wchar_t _TUCHAR; + typedef wchar_t _TXCHAR; + typedef wint_t _TINT; +#endif + +#ifndef _TCHAR_DEFINED +#define _TCHAR_DEFINED +#ifndef NO_OLDNAMES + typedef wchar_t TCHAR; +#endif +#endif + +#define _TEOF WEOF + +#define __T(x) L##x + +#define _tmain wmain +#define _tWinMain wWinMain +#define _tenviron _wenviron +#define __targv __wargv + +#define _tprintf wprintf +#define _tprintf_l _wprintf_l +#define _tprintf_p _wprintf_p +#define _tprintf_p_l _wprintf_p_l +#define _tcprintf _cwprintf +#define _tcprintf_l _cwprintf_l +#define _tcprintf_p _cwprintf_p +#define _tcprintf_p_l _cwprintf_p_l +#define _vtcprintf _vcwprintf +#define _vtcprintf_l _vcwprintf_l +#define _vtcprintf_p _vcwprintf_p +#define _vtcprintf_p_l _vcwprintf_p_l +#define _ftprintf fwprintf +#define _ftprintf_l _fwprintf_l +#define _ftprintf_p _fwprintf_p +#define _ftprintf_p_l _fwprintf_p_l +#define _stprintf swprintf +#define _stprintf_l __swprintf_l +#define _stprintf_p _swprintf_p +#define _stprintf_p_l _swprintf_p_l +#define _sctprintf _scwprintf +#define _sctprintf_l _scwprintf_l +#define _sctprintf_p _scwprintf_p +#define _sctprintf_p_l _scwprintf_p_l +#define _sntprintf _snwprintf +#define _sntprintf_l _snwprintf_l +#define _vtprintf vwprintf +#define _vtprintf_l _vwprintf_l +#define _vtprintf_p _vwprintf_p +#define _vtprintf_p_l _vwprintf_p_l +#define _vftprintf vfwprintf +#define _vftprintf_l _vfwprintf_l +#define _vftprintf_p _vfwprintf_p +#define _vftprintf_p_l _vfwprintf_p_l +#define _vstprintf vswprintf +#define _vstprintf_l _vswprintf_l +#define _vstprintf_p _vswprintf_p +#define _vstprintf_p_l _vswprintf_p_l +#define _vsctprintf _vscwprintf +#define _vsctprintf_l _vscwprintf_l +#define _vsctprintf_p _vscwprintf_p +#define _vsctprintf_p_l _vscwprintf_p_l +#define _vsntprintf _vsnwprintf +#define _vsntprintf_l _vsnwprintf_l + +#define _tscanf wscanf +#define _tscanf_l _wscanf_l +#define _tcscanf _cwscanf +#define _tcscanf_l _cwscanf_l +#define _ftscanf fwscanf +#define _ftscanf_l _fwscanf_l +#define _stscanf swscanf +#define _stscanf_l _swscanf_l +#define _sntscanf _snwscanf +#define _sntscanf_l _snwscanf_l + +#define _fgettc fgetwc +#define _fgettc_nolock _fgetwc_nolock +#define _fgettchar _fgetwchar +#define _fgetts fgetws +#define _fputtc fputwc +#define _fputtc_nolock _fputwc_nolock +#define _fputtchar _fputwchar +#define _fputts fputws +#define _cputts _cputws +#define _cgetts _cgetws +#define _gettc getwc +#define _gettc_nolock _getwc_nolock +#define _gettch _getwch +#define _gettch_nolock _getwch_nolock +#define _gettche _getwche +#define _gettche_nolock _getwche_nolock +#define _gettchar getwchar +#define _gettchar_nolock _getwchar_nolock +#define _getts _getws +#define _puttc putwc +#define _puttc_nolock _putwc_nolock +#define _puttchar putwchar +#define _puttchar_nolock _putwchar_nolock +#define _puttch _putwch +#define _puttch_nolock _putwch_nolock +#define _putts _putws +#define _ungettc ungetwc +#define _ungettc_nolock _ungetwc_nolock +#define _ungettch _ungetwch +#define _ungettch_nolock _ungetwch_nolock + +#define _tcstod wcstod +#define _tcstol wcstol +#define _tcstoul wcstoul +#define _tcstoi64 _wcstoi64 +#define _tcstoui64 _wcstoui64 +#define _tstof _wtof +#define _tstol _wtol +#define _tstoi _wtoi +#define _tstoi64 _wtoi64 +#define _tcstod_l _wcstod_l +#define _tcstol_l _wcstol_l +#define _tcstoul_l _wcstoul_l +#define _tcstoi64_l _wcstoi64_l +#define _tcstoui64_l _wcstoui64_l +#define _tstof_l _wtof_l +#define _tstol_l _wtol_l +#define _tstoi_l _wtoi_l +#define _tstoi64_l _wtoi64_l + +#define _itot _itow +#define _ltot _ltow +#define _ultot _ultow +#define _ttoi _wtoi +#define _ttol _wtol + +#define _ttoi64 _wtoi64 +#define _i64tot _i64tow +#define _ui64tot _ui64tow + +#define _tcscat wcscat +#define _tcschr wcschr +#define _tcscpy wcscpy +#define _tcscspn wcscspn +#define _tcslen wcslen +#define _tcsnlen wcsnlen +#define _tcsncat wcsncat +#define _tcsncat_l _wcsncat_l +#define _tcsncpy wcsncpy +#define _tcsncpy_l _wcsncpy_l +#define _tcspbrk wcspbrk +#define _tcsrchr wcsrchr +#define _tcsspn wcsspn +#define _tcsstr wcsstr +#define _tcstok wcstok +#define _tcstok_l _wcstok_l +#define _tcserror _wcserror +#define __tcserror __wcserror + +#define _tcsdup _wcsdup +#define _tcsnset _wcsnset +#define _tcsnset_l _wcsnset_l +#define _tcsrev _wcsrev +#define _tcsset _wcsset +#define _tcsset_l _wcsset_l + +#define _tcscmp wcscmp +#define _tcsicmp _wcsicmp +#define _tcsicmp_l _wcsicmp_l +#define _tcsnccmp wcsncmp +#define _tcsncmp wcsncmp +#define _tcsncicmp _wcsnicmp +#define _tcsncicmp_l _wcsnicmp_l +#define _tcsnicmp _wcsnicmp +#define _tcsnicmp_l _wcsnicmp_l + +#define _tcscoll wcscoll +#define _tcscoll_l _wcscoll_l +#define _tcsicoll _wcsicoll +#define _tcsicoll_l _wcsicoll_l +#define _tcsnccoll _wcsncoll +#define _tcsnccoll_l _wcsncoll_l +#define _tcsncoll _wcsncoll +#define _tcsncoll_l _wcsncoll_l +#define _tcsncicoll _wcsnicoll +#define _tcsncicoll_l _wcsnicoll_l +#define _tcsnicoll _wcsnicoll +#define _tcsnicoll_l _wcsnicoll_l + +#define _texecl _wexecl +#define _texecle _wexecle +#define _texeclp _wexeclp +#define _texeclpe _wexeclpe +#define _texecv _wexecv +#define _texecve _wexecve +#define _texecvp _wexecvp +#define _texecvpe _wexecvpe + +#define _tspawnl _wspawnl +#define _tspawnle _wspawnle +#define _tspawnlp _wspawnlp +#define _tspawnlpe _wspawnlpe +#define _tspawnv _wspawnv +#define _tspawnve _wspawnve +#define _tspawnvp _wspawnvp +#define _tspawnvp _wspawnvp +#define _tspawnvpe _wspawnvpe + +#define _tsystem _wsystem + +#define _tasctime _wasctime +#define _tctime _wctime +#define _tctime32 _wctime32 +#define _tctime64 _wctime64 +#define _tstrdate _wstrdate +#define _tstrtime _wstrtime +#define _tutime _wutime +#define _tutime32 _wutime32 +#define _tutime64 _wutime64 +#define _tcsftime wcsftime +#define _tcsftime_l _wcsftime_l + +#define _tchdir _wchdir +#define _tgetcwd _wgetcwd +#define _tgetdcwd _wgetdcwd +#define _tgetdcwd_nolock _wgetdcwd_nolock +#define _tmkdir _wmkdir +#define _trmdir _wrmdir + +#define _tfullpath _wfullpath +#define _tgetenv _wgetenv +#define _tmakepath _wmakepath +#define _tpgmptr _wpgmptr +#define _get_tpgmptr _get_wpgmptr +#define _tputenv _wputenv +#define _tsearchenv _wsearchenv +#define _tsplitpath _wsplitpath + +#define _tfdopen _wfdopen +#define _tfsopen _wfsopen +#define _tfopen _wfopen +#define _tfreopen _wfreopen +#define _tperror _wperror +#define _tpopen _wpopen +#define _ttempnam _wtempnam +#define _ttmpnam _wtmpnam + +#define _taccess _waccess +#define _tchmod _wchmod +#define _tcreat _wcreat +#define _tfindfirst _wfindfirst +#define _tfindfirst32 _wfindfirst32 +#define _tfindfirst64 _wfindfirst64 +#define _tfindfirsti64 _wfindfirsti64 +#define _tfindfirst32i64 _wfindfirst32i64 +#define _tfindfirst64i32 _wfindfirst64i32 +#define _tfindnext _wfindnext +#define _tfindnext32 _wfindnext32 +#define _tfindnext64 _wfindnext64 +#define _tfindnexti64 _wfindnexti64 +#define _tfindnext32i64 _wfindnext32i64 +#define _tfindnext64i32 _wfindnext64i32 +#define _tmktemp _wmktemp +#define _topen _wopen +#define _tremove _wremove +#define _trename _wrename +#define _tsopen _wsopen +#define _tunlink _wunlink + +#define _tfinddata_t _wfinddata_t +#define _tfinddata32_t _wfinddata32_t +#define _tfinddata64_t _wfinddata64_t +#define _tfinddatai64_t _wfinddatai64_t +#define _tfinddata32i64_t _wfinddata32i64_t +#define _tfinddata64i32_t _wfinddata64i32_t + +#define _tstat _wstat +#define _tstat32 _wstat32 +#define _tstat32i64 _wstat32i64 +#define _tstat64 _wstat64 +#define _tstat64i32 _wstat64i32 +#define _tstati64 _wstati64 + +#define _tsetlocale _wsetlocale + +#define _tcsclen wcslen +#define _tcscnlen wcsnlen +#define _tcsclen_l(_String,_Locale) wcslen(_String) +#define _tcscnlen_l(_String,_Max_count,_Locale) wcsnlen_l((_String),(_Max_count)) +#define _tcsnccat wcsncat +#define _tcsnccat_l _wcsncat_l +#define _tcsnccpy wcsncpy +#define _tcsnccpy_l _wcsncpy_l +#define _tcsncset _wcsnset + +#define _tcsdec _wcsdec +#define _tcsinc _wcsinc +#define _tcsnbcnt _wcsncnt +#define _tcsnccnt _wcsncnt +#define _tcsnextc _wcsnextc +#define _tcsninc _wcsninc +#define _tcsspnp _wcsspnp + +#define _tcslwr _wcslwr +#define _tcslwr_l _wcslwr_l +#define _tcsupr _wcsupr +#define _tcsupr_l _wcsupr_l +#define _tcsxfrm wcsxfrm +#define _tcsxfrm_l _wcsxfrm_l + +#define _tclen(_pc) (1) +#define _tccpy(_pc1,_cpc2) ((*(_pc1) = *(_cpc2))) +#define _tccmp(_cpc1,_cpc2) ((*(_cpc1))-(*(_cpc2))) + +#define _istalnum iswalnum +#define _istalnum_l _iswalnum_l +#define _istalpha iswalpha +#define _istalpha_l _iswalpha_l +#define _istascii iswascii +#define _istcntrl iswcntrl +#define _istcntrl_l _iswcntrl_l +#define _istdigit iswdigit +#define _istdigit_l _iswdigit_l +#define _istgraph iswgraph +#define _istgraph_l _iswgraph_l +#define _istlower iswlower +#define _istlower_l _iswlower_l +#define _istprint iswprint +#define _istprint_l _iswprint_l +#define _istpunct iswpunct +#define _istpunct_l _iswpunct_l +#define _istspace iswspace +#define _istspace_l _iswspace_l +#define _istupper iswupper +#define _istupper_l _iswupper_l +#define _istxdigit iswxdigit +#define _istxdigit_l _iswxdigit_l + +#define _totupper towupper +#define _totupper_l _towupper_l +#define _totlower towlower +#define _totlower_l _towlower_l + +#define _istlegal(_Char) (1) +#define _istlead(_Char) (0) +#define _istleadbyte(_Char) (0) +#define _istleadbyte_l(_Char,_Locale) (0) + +#define _wcsdec(_cpc1,_cpc2) ((_cpc1)>=(_cpc2) ? NULL : (_cpc2)-1) +#define _wcsinc(_pc) ((_pc)+1) +#define _wcsnextc(_cpc) ((unsigned int) *(_cpc)) +#define _wcsninc(_pc,_sz) (((_pc)+(_sz))) + _CRTIMP size_t __cdecl __wcsncnt(const wchar_t *_Str,size_t _MaxCount); +#define _wcsncnt(_cpc,_sz) (__wcsncnt(_cpc,_sz)) +#define _wcsspnp(_cpc1,_cpc2) (!_cpc1 ? NULL : ((*((_cpc1)+wcsspn(_cpc1,_cpc2))) ? ((_cpc1)+wcsspn(_cpc1,_cpc2)) : NULL)) +#define _wcsncpy_l(_Destination,_Source,_Count,_Locale) (wcsncpy(_Destination,_Source,_Count)) +#define _wcsncat_l(_Destination,_Source,_Count,_Locale) (wcsncat(_Destination,_Source,_Count)) +#define _wcstok_l(_String,_Delimiters,_Locale) (wcstok(_String,_Delimiters)) +#define _wcsnset_l(_Destination,_Value,_Count,_Locale) (_wcsnset(_Destination,_Value,_Count)) +#define _wcsset_l(_Destination,_Value,_Locale) (_wcsset(_Destination,_Value)) + + /* dirent structures and functions */ +#define _tdirent _wdirent +#define _TDIR _WDIR +#define _topendir _wopendir +#define _tclosedir _wclosedir +#define _treaddir _wreaddir +#define _trewinddir _wrewinddir +#define _ttelldir _wtelldir +#define _tseekdir _wseekdir + +#else + +#ifdef __cplusplus +} +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define _TEOF EOF + +#define __T(x) x + +#define _tmain main +#define _tWinMain WinMain +#ifdef _POSIX_ +#define _tenviron environ +#else +#define _tenviron _environ +#endif +#define __targv __argv + +#define _tprintf printf +#define _tprintf_l _printf_l +#define _tprintf_p _printf_p +#define _tprintf_p_l _printf_p_l +#define _tcprintf _cprintf +#define _tcprintf_l _cprintf_l +#define _tcprintf_p _cprintf_p +#define _tcprintf_p_l _cprintf_p_l +#define _vtcprintf _vcprintf +#define _vtcprintf_l _vcprintf_l +#define _vtcprintf_p _vcprintf_p +#define _vtcprintf_p_l _vcprintf_p_l +#define _ftprintf fprintf +#define _ftprintf_l _fprintf_l +#define _ftprintf_p _fprintf_p +#define _ftprintf_p_l _fprintf_p_l +#define _stprintf sprintf +#define _stprintf_l _sprintf_l +#define _stprintf_p _sprintf_p +#define _stprintf_p_l _sprintf_p_l +#define _sctprintf _scprintf +#define _sctprintf_l _scprintf_l +#define _sctprintf_p _scprintf_p +#define _sctprintf_p_l _scprintf_p_l +#define _sntprintf _snprintf +#define _sntprintf_l _snprintf_l +#define _vtprintf vprintf +#define _vtprintf_l _vprintf_l +#define _vtprintf_p _vprintf_p +#define _vtprintf_p_l _vprintf_p_l +#define _vftprintf vfprintf +#define _vftprintf_l _vfprintf_l +#define _vftprintf_p _vfprintf_p +#define _vftprintf_p_l _vfprintf_p_l +#define _vstprintf vsprintf +#define _vstprintf_l _vsprintf_l +#define _vstprintf_p _vsprintf_p +#define _vstprintf_p_l _vsprintf_p_l +#define _vsctprintf _vscprintf +#define _vsctprintf_l _vscprintf_l +#define _vsctprintf_p _vscprintf_p +#define _vsctprintf_p_l _vscprintf_p_l +#define _vsntprintf _vsnprintf +#define _vsntprintf_l _vsnprintf_l + +#define _tscanf scanf +#define _tscanf_l _scanf_l +#define _tcscanf _cscanf +#define _tcscanf_l _cscanf_l +#define _ftscanf fscanf +#define _ftscanf_l _fscanf_l +#define _stscanf sscanf +#define _stscanf_l _sscanf_l +#define _sntscanf _snscanf +#define _sntscanf_l _snscanf_l + +#define _fgettc fgetc +#define _fgettc_nolock _fgetc_nolock +#define _fgettchar _fgetchar +#define _fgetts fgets +#define _fputtc fputc +#define _fputtc_nolock _fputc_nolock +#define _fputtchar _fputchar +#define _fputts fputs +#define _cputts _cputs +#define _gettc getc +#define _gettc_nolock _getc_nolock +#define _gettch _getch +#define _gettch_nolock _getch_nolock +#define _gettche _getche +#define _gettche_nolock _getche_nolock +#define _gettchar getchar +#define _gettchar_nolock _getchar_nolock +#define _getts gets +#define _cgetts _cgets +#define _puttc putc +#define _puttc_nolock _putc_nolock +#define _puttchar putchar +#define _puttchar_nolock _putchar_nolock +#define _puttch _putch +#define _puttch_nolock _putch_nolock +#define _putts puts +#define _ungettc ungetc +#define _ungettc_nolock _ungetc_nolock +#define _ungettch _ungetch +#define _ungettch_nolock _ungetch_nolock + +#define _tcstod strtod +#define _tcstol strtol +#define _tcstoul strtoul +#define _tstof atof +#define _tstol atol +#define _tstoi atoi +#define _tstoi64 _atoi64 +#define _tcstod_l _strtod_l +#define _tcstol_l _strtol_l +#define _tcstoul_l _strtoul_l +#define _tstof_l _atof_l +#define _tstol_l _atol_l +#define _tstoi_l _atoi_l +#define _tstoi64_l _atoi64_l + +#define _itot _itoa +#define _ltot _ltoa +#define _ultot _ultoa +#define _ttoi atoi +#define _ttol atol + +#define _ttoi64 _atoi64 +#define _tcstoi64 _strtoi64 +#define _tcstoi64_l _strtoi64_l +#define _tcstoui64 _strtoui64 +#define _tcstoui64_l _strtoui64_l +#define _i64tot _i64toa +#define _ui64tot _ui64toa + +#define _tcscat strcat +#define _tcscpy strcpy +#define _tcsdup _strdup +#define _tcslen strlen +#if 0 +#define _tcsnlen strnlen +#endif +#define _tcsxfrm strxfrm +#define _tcsxfrm_l _strxfrm_l +#define _tcserror strerror +#define __tcserror _strerror + +#define _texecl _execl +#define _texecle _execle +#define _texeclp _execlp +#define _texeclpe _execlpe +#define _texecv _execv +#define _texecve _execve +#define _texecvp _execvp +#define _texecvpe _execvpe + +#define _tspawnl _spawnl +#define _tspawnle _spawnle +#define _tspawnlp _spawnlp +#define _tspawnlpe _spawnlpe +#define _tspawnv _spawnv +#define _tspawnve _spawnve +#define _tspawnvp _spawnvp +#define _tspawnvpe _spawnvpe + +#define _tsystem system + +#define _tasctime asctime +#define _tctime ctime +#define _tctime32 _ctime32 +#define _tctime64 _ctime64 +#define _tstrdate _strdate +#define _tstrtime _strtime +#define _tutime _utime +#define _tutime32 _utime32 +#define _tutime64 _utime64 +#define _tcsftime strftime +#define _tcsftime_l _strftime_l + +#define _tchdir _chdir +#define _tgetcwd _getcwd +#define _tgetdcwd _getdcwd +#define _tgetdcwd_nolock _getdcwd_nolock +#define _tmkdir _mkdir +#define _trmdir _rmdir + +#define _tfullpath _fullpath +#define _tgetenv getenv +#define _tmakepath _makepath +#define _tpgmptr _pgmptr +#define _get_tpgmptr _get_pgmptr +#define _tputenv _putenv +#define _tsearchenv _searchenv +#define _tsplitpath _splitpath + +#ifdef _POSIX_ +#define _tfdopen fdopen +#else +#define _tfdopen _fdopen +#endif +#define _tfsopen _fsopen +#define _tfopen fopen +#define _tfreopen freopen +#define _tperror perror +#define _tpopen _popen +#define _ttempnam _tempnam +#define _ttmpnam tmpnam + +#define _tchmod _chmod +#define _tcreat _creat +#define _tfindfirst _findfirst +#define _tfindfirst32 _findfirst32 +#define _tfindfirst64 _findfirst64 +#define _tfindfirsti64 _findfirsti64 +#define _tfindfirst32i64 _findfirst32i64 +#define _tfindfirst64i32 _findfirst64i32 +#define _tfindnext _findnext +#define _tfindnext32 _findnext32 +#define _tfindnext64 _findnext64 +#define _tfindnexti64 _findnexti64 +#define _tfindnext32i64 _findnext32i64 +#define _tfindnext64i32 _findnext64i32 +#define _tmktemp _mktemp + +#ifdef _POSIX_ +#define _topen open +#define _taccess access +#else +#define _topen _open +#define _taccess _access +#endif + +#define _tremove remove +#define _trename rename +#define _tsopen _sopen +#define _tunlink _unlink + +#define _tfinddata_t _finddata_t +#define _tfinddata32_t _finddata32_t +#define _tfinddata64_t __finddata64_t +#define _tfinddatai64_t _finddatai64_t +#define _tfinddata32i64_t _finddata32i64_t +#define _tfinddata64i32_t _finddata64i32_t + +#define _istascii __isascii +#define _istcntrl iscntrl +#define _istcntrl_l _iscntrl_l +#define _istxdigit isxdigit +#define _istxdigit_l _isxdigit_l + +#define _tstat _stat +#define _tstat32 _stat32 +#define _tstat32i64 _stat32i64 +#define _tstat64 _stat64 +#define _tstat64i32 _stat64i32 +#define _tstati64 _stati64 + +#define _tsetlocale setlocale + +#ifdef _MBCS + +#ifdef __cplusplus +} +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __TCHAR_DEFINED + typedef char _TCHAR; + typedef signed char _TSCHAR; + typedef unsigned char _TUCHAR; + typedef unsigned char _TXCHAR; + typedef unsigned int _TINT; +#define __TCHAR_DEFINED +#endif + +#ifndef _TCHAR_DEFINED +#ifndef NO_OLDNAMES + typedef char TCHAR; +#endif +#define _TCHAR_DEFINED +#endif + +#ifdef _MB_MAP_DIRECT + +#define _tcschr _mbschr +#define _tcscspn _mbscspn +#define _tcsncat _mbsnbcat +#define _tcsncat_l _mbsnbcat_l +#define _tcsncpy _mbsnbcpy +#define _tcsncpy_l _mbsnbcpy_l +#define _tcspbrk _mbspbrk +#define _tcsrchr _mbsrchr +#define _tcsspn _mbsspn +#define _tcsstr _mbsstr +#define _tcstok _mbstok +#define _tcstok_l _mbstok_l + +#define _tcsnset _mbsnbset +#define _tcsnset_l _mbsnbset_l +#define _tcsrev _mbsrev +#define _tcsset _mbsset +#define _tcsset_l _mbsset_l + +#define _tcscmp _mbscmp +#define _tcsicmp _mbsicmp +#define _tcsicmp_l _mbsicmp_l +#define _tcsnccmp _mbsncmp +#define _tcsncmp _mbsnbcmp +#define _tcsncicmp _mbsnicmp +#define _tcsncicmp_l _mbsnicmp_l +#define _tcsnicmp _mbsnbicmp +#define _tcsnicmp_l _mbsnbicmp_l + +#define _tcscoll _mbscoll +#define _tcscoll_l _mbscoll_l +#define _tcsicoll _mbsicoll +#define _tcsicoll_l _mbsicoll_l +#define _tcsnccoll _mbsncoll +#define _tcsnccoll_l _mbsncoll_l +#define _tcsncoll _mbsnbcoll +#define _tcsncoll_l _mbsnbcoll_l +#define _tcsncicoll _mbsnicoll +#define _tcsncicoll_l _mbsnicoll_l +#define _tcsnicoll _mbsnbicoll +#define _tcsnicoll_l _mbsnbicoll_l + +#define _tcsclen _mbslen +#define _tcscnlen _mbsnlen +#define _tcsclen_l _mbslen_l +#define _tcscnlen_l _mbsnlen_l +#define _tcsnccat _mbsncat +#define _tcsnccat_l _mbsncat_l +#define _tcsnccpy _mbsncpy +#define _tcsnccpy_l _mbsncpy_l +#define _tcsncset _mbsnset +#define _tcsncset_l _mbsnset_l + +#define _tcsdec _mbsdec +#define _tcsinc _mbsinc +#define _tcsnbcnt _mbsnbcnt +#define _tcsnccnt _mbsnccnt +#define _tcsnextc _mbsnextc +#define _tcsninc _mbsninc +#define _tcsspnp _mbsspnp + +#define _tcslwr _mbslwr +#define _tcslwr_l _mbslwr_l +#define _tcsupr _mbsupr +#define _tcsupr_l _mbsupr_l + +#define _tclen _mbclen +#define _tccpy _mbccpy +#define _tccpy_l _mbccpy_l +#else + + _CRTIMP _CONST_RETURN char *__cdecl _tcschr(const char *_Str,unsigned int _Val); + _CRTIMP size_t __cdecl _tcscspn(const char *_Str,const char *_Control); + _CRTIMP char *__cdecl _tcsncat(char *_Dst,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsncat_l(char *_Dst,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsncpy(char *_Dst,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsncpy_l(char *_Dst,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP _CONST_RETURN char *__cdecl _tcspbrk(const char *_Str,const char *_Control); + _CRTIMP _CONST_RETURN char *__cdecl _tcsrchr(const char *_Str,unsigned int _Ch); + _CRTIMP size_t __cdecl _tcsspn(const char *_Str,const char *_Control); + _CRTIMP _CONST_RETURN char *__cdecl _tcsstr(const char *_Str,const char *_Substr); + _CRTIMP char *__cdecl _tcstok(char *_Str,const char *_Delim); + _CRTIMP char *__cdecl _tcstok_l(char *_Str,const char *_Delim,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsnset(char *_Str,unsigned int _Val,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsrev(char *_Str); + _CRTIMP char *__cdecl _tcsset(char *_Str,unsigned int _Val); + _CRTIMP char *__cdecl _tcsset_l(char *_Str,unsigned int _Val,_locale_t _Locale); + _CRTIMP int __cdecl _tcscmp(const char *_Str1,const char *_Str); + _CRTIMP int __cdecl _tcsicmp(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _tcsicmp_l(const char *_Str1,const char *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _tcsnccmp(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsncmp(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsncicmp(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsncicmp_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _tcsnicmp(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsnicmp_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _tcscoll(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _tcscoll_l(const char *_Str1,const char *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _tcsicoll(const char *_Str1,const char *_Str2); + _CRTIMP int __cdecl _tcsicoll_l(const char *_Str1,const char *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _tcsnccoll(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsnccoll_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _tcsncoll(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsncoll_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _tcsncicoll(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsncicoll_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _tcsnicoll(const char *_Str1,const char *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _tcsnicoll_l(const char *_Str1,const char *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP size_t __cdecl _tcsclen(const char *_Str); + _CRTIMP size_t __cdecl _tcscnlen(const char *_Str,size_t _MaxCount); + _CRTIMP size_t __cdecl _tcsclen_l(const char *_Str,_locale_t _Locale); + _CRTIMP size_t __cdecl _tcscnlen_l(const char *_Str,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsnccat(char *_Dst,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsnccat_l(char *_Dst,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsnccpy(char *_Dst,const char *_Src,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsnccpy_l(char *_Dst,const char *_Src,size_t _MaxCount,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsncset(char *_Str,unsigned int _Val,size_t _MaxCount); + _CRTIMP char *__cdecl _tcsdec(const char *_Start,const char *_Pos); + _CRTIMP char *__cdecl _tcsinc(const char *_Ptr); + _CRTIMP size_t __cdecl _tcsnbcnt(const char *_Str,size_t _MaxCount); + _CRTIMP size_t __cdecl _tcsnccnt(const char *_Str,size_t _MaxCount); + _CRTIMP unsigned int __cdecl _tcsnextc (const char *_Str); + _CRTIMP char *__cdecl _tcsninc(const char *_Ptr,size_t _Count); + _CRTIMP char *__cdecl _tcsspnp(const char *_Str1,const char *_Str2); + _CRTIMP char *__cdecl _tcslwr(char *_Str); + _CRTIMP char *__cdecl _tcslwr_l(char *_Str,_locale_t _Locale); + _CRTIMP char *__cdecl _tcsupr(char *_Str); + _CRTIMP char *__cdecl _tcsupr_l(char *_Str,_locale_t _Locale); + _CRTIMP size_t __cdecl _tclen(const char *_Str); + _CRTIMP void __cdecl _tccpy(char *_DstCh,const char *_SrcCh); + +#ifdef __cplusplus +#ifndef _CPP_TCHAR_INLINES_DEFINED +#define _CPP_TCHAR_INLINES_DEFINED + extern "C++" { + extern inline char *__cdecl _tcschr(char *_S,unsigned int _C) { return ((char *)_tcschr((const char *)_S,_C)); } + extern inline char *__cdecl _tcspbrk(char *_S,const char *_P) { return ((char *)_tcspbrk((const char *)_S,_P)); } + extern inline char *__cdecl _tcsrchr(char *_S,unsigned int _C) { return ((char *)_tcsrchr((const char *)_S,_C)); } + extern inline char *__cdecl _tcsstr(char *_S,const char *_P) { return ((char *)_tcsstr((const char *)_S,_P)); } + } +#endif +#endif +#endif + +#define _tccmp(_cp1,_cp2) _tcsnccmp(_cp1,_cp2,1) + +#define _istalnum _ismbcalnum +#define _istalnum_l _ismbcalnum_l +#define _istalpha _ismbcalpha +#define _istalpha_l _ismbcalpha_l +#define _istdigit _ismbcdigit +#define _istdigit_l _ismbcdigit_l +#define _istgraph _ismbcgraph +#define _istgraph_l _ismbcgraph_l +#define _istlegal _ismbclegal +#define _istlegal_l _ismbclegal_l +#define _istlower _ismbclower +#define _istlower_l _ismbclower_l +#define _istprint _ismbcprint +#define _istprint_l _ismbcprint_l +#define _istpunct _ismbcpunct +#define _istpunct_l _ismbcpunct_l +#define _istspace _ismbcspace +#define _istspace_l _ismbcspace_l +#define _istupper _ismbcupper +#define _istupper_l _ismbcupper_l + +#define _totupper _mbctoupper +#define _totupper_l _mbctoupper_l +#define _totlower _mbctolower +#define _totlower_l _mbctolower_l + +#define _istlead _ismbblead +#define _istleadbyte isleadbyte +#define _istleadbyte_l _isleadbyte_l +#else + +#ifndef __TCHAR_DEFINED +#define __TCHAR_DEFINED + typedef char _TCHAR; + typedef signed char _TSCHAR; + typedef unsigned char _TUCHAR; + typedef char _TXCHAR; + typedef int _TINT; +#endif + +#ifndef _TCHAR_DEFINED +#define _TCHAR_DEFINED +#ifndef NO_OLDNAMES + typedef char TCHAR; +#endif +#endif + +#define _tcschr strchr +#define _tcscspn strcspn +#define _tcsncat strncat +#define _tcsncat_l _strncat_l +#define _tcsncpy strncpy +#define _tcsncpy_l _strncpy_l +#define _tcspbrk strpbrk +#define _tcsrchr strrchr +#define _tcsspn strspn +#define _tcsstr strstr +#define _tcstok strtok +#define _tcstok_l _strtok_l + +#define _tcsnset _strnset +#define _tcsnset_l _strnset_l +#define _tcsrev _strrev +#define _tcsset _strset + +#define _tcscmp strcmp +#define _tcsicmp _stricmp +#define _tcsicmp_l _stricmp_l +#define _tcsnccmp strncmp +#define _tcsncmp strncmp +#define _tcsncicmp _strnicmp +#define _tcsncicmp_l _strnicmp_l +#define _tcsnicmp _strnicmp +#define _tcsnicmp_l _strnicmp_l + +#define _tcscoll strcoll +#define _tcscoll_l _strcoll_l +#define _tcsicoll _stricoll +#define _tcsicoll_l _stricoll_l +#define _tcsnccoll _strncoll +#define _tcsnccoll_l _strncoll_l +#define _tcsncoll _strncoll +#define _tcsncoll_l _strncoll_l +#define _tcsncicoll _strnicoll +#define _tcsncicoll_l _strnicoll_l +#define _tcsnicoll _strnicoll +#define _tcsnicoll_l _strnicoll_l + +#define _tcsclen strlen +#define _tcscnlen strnlen +#define _tcsclen_l(_String,_Locale) strlen(_String) +#define _tcscnlen_l(_String,_Max_count,_Locale) strnlen_l((_String),(_Max_count)) +#define _tcsnccat strncat +#define _tcsnccat_l _strncat_l +#define _tcsnccpy strncpy +#define _tcsnccpy_l _strncpy_l +#define _tcsncset _strnset + +#define _tcsdec _strdec +#define _tcsinc _strinc +#define _tcsnbcnt _strncnt +#define _tcsnccnt _strncnt +#define _tcsnextc _strnextc +#define _tcsninc _strninc +#define _tcsspnp _strspnp + +#define _tcslwr _strlwr +#define _tcslwr_l _strlwr_l +#define _tcsupr _strupr +#define _tcsupr_l _strupr_l +#define _tcsxfrm strxfrm +#define _tcsxfrm_l _strxfrm_l + +#define _istlead(_Char) (0) +#define _istleadbyte(_Char) (0) +#define _istleadbyte_l(_Char,_Locale) (0) + +#define _tclen(_pc) (1) +#define _tccpy(_pc1,_cpc2) (*(_pc1) = *(_cpc2)) +#define _tccmp(_cpc1,_cpc2) (((unsigned char)*(_cpc1))-((unsigned char)*(_cpc2))) + + /* dirent structures and functions */ +#define _tdirent dirent +#define _TDIR DIR +#define _topendir opendir +#define _tclosedir closedir +#define _treaddir readdir +#define _trewinddir rewinddir +#define _ttelldir telldir +#define _tseekdir seekdir + +#define _istalnum isalnum +#define _istalnum_l _isalnum_l +#define _istalpha isalpha +#define _istalpha_l _isalpha_l +#define _istdigit isdigit +#define _istdigit_l _isdigit_l +#define _istgraph isgraph +#define _istgraph_l _isgraph_l +#define _istlower islower +#define _istlower_l _islower_l +#define _istprint isprint +#define _istprint_l _isprint_l +#define _istpunct ispunct +#define _istpunct_l _ispunct_l +#define _istspace isspace +#define _istspace_l _isspace_l +#define _istupper isupper +#define _istupper_l _isupper_l + +#define _totupper toupper +#define _totupper_l _toupper_l +#define _totlower tolower +#define _totlower_l _tolower_l + +#define _istlegal(_c) (1) + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#define _strdec(_cpc1,_cpc2) ((_cpc1)>=(_cpc2) ? NULL : (_cpc2)-1) +#define _strinc(_pc) ((_pc)+1) +#define _strnextc(_cpc) ((unsigned int) *(const unsigned char *)(_cpc)) +#define _strninc(_pc,_sz) (((_pc)+(_sz))) + _CRTIMP size_t __cdecl __strncnt(const char *_Str,size_t _Cnt); +#define _strncnt(_cpc,_sz) (__strncnt(_cpc,_sz)) +#define _strspnp(_cpc1,_cpc2) (!_cpc1 ? NULL : ((*((_cpc1)+strspn(_cpc1,_cpc2))) ? ((_cpc1)+strspn(_cpc1,_cpc2)) : NULL)) + +#define _strncpy_l(_Destination,_Source,_Count,_Locale) (strncpy(_Destination,_Source,_Count)) +#define _strncat_l(_Destination,_Source,_Count,_Locale) (strncat(_Destination,_Source,_Count)) +#define _strtok_l(_String,_Delimiters,_Locale) (strtok(_String,_Delimiters)) +#define _strnset_l(_Destination,_Value,_Count,_Locale) (_strnset(_Destination,_Value,_Count)) +#define _strset_l(_Destination,_Value,_Locale) (_strset(_Destination,_Value)) +#endif +#endif + +#define _T(x) __T(x) +#define _TEXT(x) __T(x) + +#ifdef __cplusplus +} +#endif + +#include +#endif diff --git a/05/tcc-final-old/win32/include/time.h b/05/tcc-final-old/win32/include/time.h new file mode 100644 index 0000000..6c72e26 --- /dev/null +++ b/05/tcc-final-old/win32/include/time.h @@ -0,0 +1,287 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _TIME_H_ +#define _TIME_H_ + +#include <_mingw.h> + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CRTIMP +#define _CRTIMP __declspec(dllimport) +#endif + +#ifndef _WCHAR_T_DEFINED +#define _WCHAR_T_DEFINED + typedef unsigned short wchar_t; +#endif + +#ifndef _TIME32_T_DEFINED +#define _TIME32_T_DEFINED + typedef long __time32_t; +#endif + +#ifndef _TIME64_T_DEFINED +#define _TIME64_T_DEFINED +#if _INTEGRAL_MAX_BITS >= 64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef int _time64_t __attribute__ ((mode (DI))); +#else + typedef __int64 __time64_t; +#endif +#endif +#endif + +#ifndef _TIME_T_DEFINED +#define _TIME_T_DEFINED +#ifdef _USE_32BIT_TIME_T + typedef __time32_t time_t; +#else + typedef __time64_t time_t; +#endif +#endif + +#ifndef _CLOCK_T_DEFINED +#define _CLOCK_T_DEFINED + typedef long clock_t; +#endif + +#ifndef _SIZE_T_DEFINED +#define _SIZE_T_DEFINED +#undef size_t +#ifdef _WIN64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef unsigned int size_t __attribute__ ((mode (DI))); +#else + typedef unsigned __int64 size_t; +#endif +#else + typedef unsigned int size_t; +#endif +#endif + +#ifndef _SSIZE_T_DEFINED +#define _SSIZE_T_DEFINED +#undef ssize_t +#ifdef _WIN64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef int ssize_t __attribute__ ((mode (DI))); +#else + typedef __int64 ssize_t; +#endif +#else + typedef int ssize_t; +#endif +#endif + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#ifdef _USE_32BIT_TIME_T +#define _localtime32 localtime +#define _difftime32 difftime +#define _ctime32 ctime +#define _gmtime32 gmtime +#define _mktime32 mktime +#define _time32 time +#endif + +#ifndef _TM_DEFINED +#define _TM_DEFINED + struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mday; + int tm_mon; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; + }; +#endif + +#define CLOCKS_PER_SEC 1000 + + __MINGW_IMPORT int _daylight; + __MINGW_IMPORT long _dstbias; + __MINGW_IMPORT long _timezone; + __MINGW_IMPORT char * _tzname[2]; + _CRTIMP errno_t __cdecl _get_daylight(int *_Daylight); + _CRTIMP errno_t __cdecl _get_dstbias(long *_Daylight_savings_bias); + _CRTIMP errno_t __cdecl _get_timezone(long *_Timezone); + _CRTIMP errno_t __cdecl _get_tzname(size_t *_ReturnValue,char *_Buffer,size_t _SizeInBytes,int _Index); + char *__cdecl asctime(const struct tm *_Tm); + _CRTIMP char *__cdecl _ctime32(const __time32_t *_Time); + clock_t __cdecl clock(void); + _CRTIMP double __cdecl _difftime32(__time32_t _Time1,__time32_t _Time2); + _CRTIMP struct tm *__cdecl _gmtime32(const __time32_t *_Time); + _CRTIMP struct tm *__cdecl _localtime32(const __time32_t *_Time); + size_t __cdecl strftime(char *_Buf,size_t _SizeInBytes,const char *_Format,const struct tm *_Tm); + _CRTIMP size_t __cdecl _strftime_l(char *_Buf,size_t _Max_size,const char *_Format,const struct tm *_Tm,_locale_t _Locale); + _CRTIMP char *__cdecl _strdate(char *_Buffer); + _CRTIMP char *__cdecl _strtime(char *_Buffer); + _CRTIMP __time32_t __cdecl _time32(__time32_t *_Time); + _CRTIMP __time32_t __cdecl _mktime32(struct tm *_Tm); + _CRTIMP __time32_t __cdecl _mkgmtime32(struct tm *_Tm); +#if defined (_POSIX_) || defined(__GNUC__) + void __cdecl tzset(void); +#else + _CRTIMP void __cdecl _tzset(void); +#endif + +#if _INTEGRAL_MAX_BITS >= 64 + double __cdecl _difftime64(__time64_t _Time1,__time64_t _Time2); + _CRTIMP char *__cdecl _ctime64(const __time64_t *_Time); + _CRTIMP struct tm *__cdecl _gmtime64(const __time64_t *_Time); + _CRTIMP struct tm *__cdecl _localtime64(const __time64_t *_Time); + _CRTIMP __time64_t __cdecl _mktime64(struct tm *_Tm); + _CRTIMP __time64_t __cdecl _mkgmtime64(struct tm *_Tm); + _CRTIMP __time64_t __cdecl _time64(__time64_t *_Time); +#endif + unsigned __cdecl _getsystime(struct tm *_Tm); + unsigned __cdecl _setsystime(struct tm *_Tm,unsigned _MilliSec); + +#ifndef _SIZE_T_DEFINED +#define _SIZE_T_DEFINED +#ifdef _WIN64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef unsigned int size_t __attribute__ ((mode (DI))); +#else + typedef unsigned __int64 size_t; +#endif +#else + typedef unsigned long size_t; +#endif +#endif + +#ifndef _SSIZE_T_DEFINED +#define _SSIZE_T_DEFINED +#ifdef _WIN64 +#if defined(__GNUC__) && defined(__STRICT_ANSI__) + typedef int ssize_t __attribute__ ((mode (DI))); +#else + typedef __int64 ssize_t; +#endif +#else + typedef long ssize_t; +#endif +#endif + +#ifndef _WTIME_DEFINED + _CRTIMP wchar_t *__cdecl _wasctime(const struct tm *_Tm); + _CRTIMP wchar_t *__cdecl _wctime32(const __time32_t *_Time); + size_t __cdecl wcsftime(wchar_t *_Buf,size_t _SizeInWords,const wchar_t *_Format,const struct tm *_Tm); + _CRTIMP size_t __cdecl _wcsftime_l(wchar_t *_Buf,size_t _SizeInWords,const wchar_t *_Format,const struct tm *_Tm,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wstrdate(wchar_t *_Buffer); + _CRTIMP wchar_t *__cdecl _wstrtime(wchar_t *_Buffer); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP wchar_t *__cdecl _wctime64(const __time64_t *_Time); +#endif + +#if !defined (RC_INVOKED) && !defined (_INC_WTIME_INL) +#define _INC_WTIME_INL +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE wchar_t *__cdecl _wctime(const time_t *_Time) { return _wctime32(_Time); } +#else +__CRT_INLINE wchar_t *__cdecl _wctime(const time_t *_Time) { return _wctime64(_Time); } +#endif +#endif + +#define _WTIME_DEFINED +#endif + +#ifndef RC_INVOKED +double __cdecl difftime(time_t _Time1,time_t _Time2); +char *__cdecl ctime(const time_t *_Time); +struct tm *__cdecl gmtime(const time_t *_Time); +struct tm *__cdecl localtime(const time_t *_Time); +struct tm *__cdecl localtime_r(const time_t *_Time,struct tm *); + +time_t __cdecl mktime(struct tm *_Tm); +time_t __cdecl _mkgmtime(struct tm *_Tm); +time_t __cdecl time(time_t *_Time); + +#ifdef _USE_32BIT_TIME_T +#if 0 +__CRT_INLINE double __cdecl difftime(time_t _Time1,time_t _Time2) { return _difftime32(_Time1,_Time2); } +__CRT_INLINE char *__cdecl ctime(const time_t *_Time) { return _ctime32(_Time); } +__CRT_INLINE struct tm *__cdecl gmtime(const time_t *_Time) { return _gmtime32(_Time); } +__CRT_INLINE struct tm *__cdecl localtime(const time_t *_Time) { return _localtime32(_Time); } +__CRT_INLINE time_t __cdecl mktime(struct tm *_Tm) { return _mktime32(_Tm); } +__CRT_INLINE time_t __cdecl _mkgmtime(struct tm *_Tm) { return _mkgmtime32(_Tm); } +__CRT_INLINE time_t __cdecl time(time_t *_Time) { return _time32(_Time); } +#endif +#else +__CRT_INLINE double __cdecl difftime(time_t _Time1,time_t _Time2) { return _difftime64(_Time1,_Time2); } +__CRT_INLINE char *__cdecl ctime(const time_t *_Time) { return _ctime64(_Time); } +__CRT_INLINE struct tm *__cdecl gmtime(const time_t *_Time) { return _gmtime64(_Time); } +__CRT_INLINE struct tm *__cdecl localtime(const time_t *_Time) { return _localtime64(_Time); } +__CRT_INLINE time_t __cdecl mktime(struct tm *_Tm) { return _mktime64(_Tm); } +__CRT_INLINE time_t __cdecl _mkgmtime(struct tm *_Tm) { return _mkgmtime64(_Tm); } +__CRT_INLINE time_t __cdecl time(time_t *_Time) { return _time64(_Time); } +#endif +#endif + +#if !defined(NO_OLDNAMES) || defined(_POSIX) +#define CLK_TCK CLOCKS_PER_SEC + + __MINGW_IMPORT int daylight; + __MINGW_IMPORT long dstbias; + __MINGW_IMPORT long timezone; + __MINGW_IMPORT char *tzname[2]; + void __cdecl tzset(void); +#endif + +#ifndef _TIMEVAL_DEFINED /* also in winsock[2].h */ +#define _TIMEVAL_DEFINED +struct timeval { + long tv_sec; + long tv_usec; +}; +#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) +#define timercmp(tvp,uvp,cmp) ((tvp)->tv_sec cmp (uvp)->tv_sec || (tvp)->tv_sec==(uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec) +#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 +#endif /* _TIMEVAL_DEFINED */ + +#ifndef __STRICT_ANSI__ +#ifndef _TIMEZONE_DEFINED /* also in sys/time.h */ +#define _TIMEZONE_DEFINED +struct timezone { + int tz_minuteswest; + int tz_dsttime; +}; + + extern int __cdecl mingw_gettimeofday (struct timeval *p, struct timezone *z); +#endif +#endif /* __STRICT_ANSI__ */ + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) + +#include + +/* Adding timespec definition. */ +#include + +#endif /* End _TIME_H_ */ + diff --git a/05/tcc-final-old/win32/include/vadefs.h b/05/tcc-final-old/win32/include/vadefs.h new file mode 100644 index 0000000..749b0bd --- /dev/null +++ b/05/tcc-final-old/win32/include/vadefs.h @@ -0,0 +1,11 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_VADEFS +#define _INC_VADEFS + +//!__TINYC__: GNUC specific stuff removed + +#endif diff --git a/05/tcc-final-old/win32/include/values.h b/05/tcc-final-old/win32/include/values.h new file mode 100644 index 0000000..1cd643c --- /dev/null +++ b/05/tcc-final-old/win32/include/values.h @@ -0,0 +1,4 @@ +/* + * TODO: Nothing here yet. Should provide UNIX compatibility constants + * comparable to those in limits.h and float.h. + */ diff --git a/05/tcc-final-old/win32/include/wchar.h b/05/tcc-final-old/win32/include/wchar.h new file mode 100644 index 0000000..389196f --- /dev/null +++ b/05/tcc-final-old/win32/include/wchar.h @@ -0,0 +1,873 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_WCHAR +#define _INC_WCHAR + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WCHAR_MIN /* also at stdint.h */ +#define WCHAR_MIN 0 +#define WCHAR_MAX ((wchar_t) -1) /* UINT16_MAX */ +#endif + +#ifndef __GNUC_VA_LIST +#define __GNUC_VA_LIST + typedef __builtin_va_list __gnuc_va_list; +#endif + +#ifndef _VA_LIST_DEFINED +#define _VA_LIST_DEFINED + typedef __gnuc_va_list va_list; +#endif + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + +#ifndef _FILE_DEFINED + struct _iobuf { + char *_ptr; + int _cnt; + char *_base; + int _flag; + int _file; + int _charbuf; + int _bufsiz; + char *_tmpfname; + }; + typedef struct _iobuf FILE; +#define _FILE_DEFINED +#endif + +#ifndef _STDIO_DEFINED +#ifdef _WIN64 + _CRTIMP FILE *__cdecl __iob_func(void); +#else +#ifdef _MSVCRT_ +extern FILE _iob[]; /* A pointer to an array of FILE */ +#define __iob_func() (_iob) +#else +extern FILE (*_imp___iob)[]; /* A pointer to an array of FILE */ +#define __iob_func() (*_imp___iob) +#define _iob __iob_func() +#endif +#endif + +#define _iob __iob_func() +#endif + +#ifndef _STDSTREAM_DEFINED +#define stdin (&__iob_func()[0]) +#define stdout (&__iob_func()[1]) +#define stderr (&__iob_func()[2]) +#define _STDSTREAM_DEFINED +#endif + +#ifndef _FSIZE_T_DEFINED + typedef unsigned long _fsize_t; +#define _FSIZE_T_DEFINED +#endif + +#ifndef _WFINDDATA_T_DEFINED + struct _wfinddata32_t { + unsigned attrib; + __time32_t time_create; + __time32_t time_access; + __time32_t time_write; + _fsize_t size; + wchar_t name[260]; + }; + +/* #if _INTEGRAL_MAX_BITS >= 64 */ + + struct _wfinddata32i64_t { + unsigned attrib; + __time32_t time_create; + __time32_t time_access; + __time32_t time_write; + __int64 size; + wchar_t name[260]; + }; + + struct _wfinddata64i32_t { + unsigned attrib; + __time64_t time_create; + __time64_t time_access; + __time64_t time_write; + _fsize_t size; + wchar_t name[260]; + }; + + struct _wfinddata64_t { + unsigned attrib; + __time64_t time_create; + __time64_t time_access; + __time64_t time_write; + __int64 size; + wchar_t name[260]; + }; +/* #endif */ + +#ifdef _USE_32BIT_TIME_T +#define _wfinddata_t _wfinddata32_t +#define _wfinddatai64_t _wfinddata32i64_t + +#define _wfindfirst _wfindfirst32 +#define _wfindnext _wfindnext32 +#define _wfindfirsti64 _wfindfirst32i64 +#define _wfindnexti64 _wfindnext32i64 +#else +#define _wfinddata_t _wfinddata64i32_t +#define _wfinddatai64_t _wfinddata64_t + +#define _wfindfirst _wfindfirst64i32 +#define _wfindnext _wfindnext64i32 +#define _wfindfirsti64 _wfindfirst64 +#define _wfindnexti64 _wfindnext64 +#endif + +#define _WFINDDATA_T_DEFINED +#endif + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#ifndef _CONST_RETURN +#define _CONST_RETURN +#endif + +#define _WConst_return _CONST_RETURN + +#ifndef _CRT_CTYPEDATA_DEFINED +#define _CRT_CTYPEDATA_DEFINED +#ifndef _CTYPE_DISABLE_MACROS + +#ifndef __PCTYPE_FUNC +#define __PCTYPE_FUNC __pctype_func() +#ifdef _MSVCRT_ +#define __pctype_func() (_pctype) +#else +#define __pctype_func() (*_imp___pctype) +#endif +#endif + +#ifndef _pctype +#ifdef _MSVCRT_ + extern unsigned short *_pctype; +#else + extern unsigned short **_imp___pctype; +#define _pctype (*_imp___pctype) +#endif +#endif +#endif +#endif + +#ifndef _CRT_WCTYPEDATA_DEFINED +#define _CRT_WCTYPEDATA_DEFINED +#ifndef _CTYPE_DISABLE_MACROS +#ifndef _wctype +#ifdef _MSVCRT_ + extern unsigned short *_wctype; +#else + extern unsigned short **_imp___wctype; +#define _wctype (*_imp___wctype) +#endif +#endif + +#ifdef _MSVCRT_ +#define __pwctype_func() (_pwctype) +#else +#define __pwctype_func() (*_imp___pwctype) +#endif + +#ifndef _pwctype +#ifdef _MSVCRT_ + extern unsigned short *_pwctype; +#else + extern unsigned short **_imp___pwctype; +#define _pwctype (*_imp___pwctype) +#endif +#endif + +#endif +#endif + +#define _UPPER 0x1 +#define _LOWER 0x2 +#define _DIGIT 0x4 +#define _SPACE 0x8 + +#define _PUNCT 0x10 +#define _CONTROL 0x20 +#define _BLANK 0x40 +#define _HEX 0x80 + +#define _LEADBYTE 0x8000 +#define _ALPHA (0x0100|_UPPER|_LOWER) + +#ifndef _WCTYPE_DEFINED +#define _WCTYPE_DEFINED + + int __cdecl iswalpha(wint_t _C); + _CRTIMP int __cdecl _iswalpha_l(wint_t _C,_locale_t _Locale); + int __cdecl iswupper(wint_t _C); + _CRTIMP int __cdecl _iswupper_l(wint_t _C,_locale_t _Locale); + int __cdecl iswlower(wint_t _C); + _CRTIMP int __cdecl _iswlower_l(wint_t _C,_locale_t _Locale); + int __cdecl iswdigit(wint_t _C); + _CRTIMP int __cdecl _iswdigit_l(wint_t _C,_locale_t _Locale); + int __cdecl iswxdigit(wint_t _C); + _CRTIMP int __cdecl _iswxdigit_l(wint_t _C,_locale_t _Locale); + int __cdecl iswspace(wint_t _C); + _CRTIMP int __cdecl _iswspace_l(wint_t _C,_locale_t _Locale); + int __cdecl iswpunct(wint_t _C); + _CRTIMP int __cdecl _iswpunct_l(wint_t _C,_locale_t _Locale); + int __cdecl iswalnum(wint_t _C); + _CRTIMP int __cdecl _iswalnum_l(wint_t _C,_locale_t _Locale); + int __cdecl iswprint(wint_t _C); + _CRTIMP int __cdecl _iswprint_l(wint_t _C,_locale_t _Locale); + int __cdecl iswgraph(wint_t _C); + _CRTIMP int __cdecl _iswgraph_l(wint_t _C,_locale_t _Locale); + int __cdecl iswcntrl(wint_t _C); + _CRTIMP int __cdecl _iswcntrl_l(wint_t _C,_locale_t _Locale); + int __cdecl iswascii(wint_t _C); + int __cdecl isleadbyte(int _C); + _CRTIMP int __cdecl _isleadbyte_l(int _C,_locale_t _Locale); + wint_t __cdecl towupper(wint_t _C); + _CRTIMP wint_t __cdecl _towupper_l(wint_t _C,_locale_t _Locale); + wint_t __cdecl towlower(wint_t _C); + _CRTIMP wint_t __cdecl _towlower_l(wint_t _C,_locale_t _Locale); + int __cdecl iswctype(wint_t _C,wctype_t _Type); + _CRTIMP int __cdecl _iswctype_l(wint_t _C,wctype_t _Type,_locale_t _Locale); + _CRTIMP int __cdecl __iswcsymf(wint_t _C); + _CRTIMP int __cdecl _iswcsymf_l(wint_t _C,_locale_t _Locale); + _CRTIMP int __cdecl __iswcsym(wint_t _C); + _CRTIMP int __cdecl _iswcsym_l(wint_t _C,_locale_t _Locale); + int __cdecl is_wctype(wint_t _C,wctype_t _Type); +#endif + +#ifndef _WDIRECT_DEFINED +#define _WDIRECT_DEFINED + + _CRTIMP wchar_t *__cdecl _wgetcwd(wchar_t *_DstBuf,int _SizeInWords); + _CRTIMP wchar_t *__cdecl _wgetdcwd(int _Drive,wchar_t *_DstBuf,int _SizeInWords); + wchar_t *__cdecl _wgetdcwd_nolock(int _Drive,wchar_t *_DstBuf,int _SizeInWords); + _CRTIMP int __cdecl _wchdir(const wchar_t *_Path); + _CRTIMP int __cdecl _wmkdir(const wchar_t *_Path); + _CRTIMP int __cdecl _wrmdir(const wchar_t *_Path); +#endif + +#ifndef _WIO_DEFINED +#define _WIO_DEFINED + + _CRTIMP int __cdecl _waccess(const wchar_t *_Filename,int _AccessMode); + _CRTIMP int __cdecl _wchmod(const wchar_t *_Filename,int _Mode); + _CRTIMP int __cdecl _wcreat(const wchar_t *_Filename,int _PermissionMode); + _CRTIMP intptr_t __cdecl _wfindfirst32(const wchar_t *_Filename,struct _wfinddata32_t *_FindData); + _CRTIMP int __cdecl _wfindnext32(intptr_t _FindHandle,struct _wfinddata32_t *_FindData); + _CRTIMP int __cdecl _wunlink(const wchar_t *_Filename); + _CRTIMP int __cdecl _wrename(const wchar_t *_NewFilename,const wchar_t *_OldFilename); + _CRTIMP wchar_t *__cdecl _wmktemp(wchar_t *_TemplateName); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP intptr_t __cdecl _wfindfirst32i64(const wchar_t *_Filename,struct _wfinddata32i64_t *_FindData); + intptr_t __cdecl _wfindfirst64i32(const wchar_t *_Filename,struct _wfinddata64i32_t *_FindData); + _CRTIMP intptr_t __cdecl _wfindfirst64(const wchar_t *_Filename,struct _wfinddata64_t *_FindData); + _CRTIMP int __cdecl _wfindnext32i64(intptr_t _FindHandle,struct _wfinddata32i64_t *_FindData); + int __cdecl _wfindnext64i32(intptr_t _FindHandle,struct _wfinddata64i32_t *_FindData); + _CRTIMP int __cdecl _wfindnext64(intptr_t _FindHandle,struct _wfinddata64_t *_FindData); +#endif + _CRTIMP errno_t __cdecl _wsopen_s(int *_FileHandle,const wchar_t *_Filename,int _OpenFlag,int _ShareFlag,int _PermissionFlag); +#if !defined(__cplusplus) || !(defined(_X86_) && !defined(__x86_64)) + _CRTIMP int __cdecl _wopen(const wchar_t *_Filename,int _OpenFlag,...); + _CRTIMP int __cdecl _wsopen(const wchar_t *_Filename,int _OpenFlag,int _ShareFlag,...); +#else + extern "C++" _CRTIMP int __cdecl _wopen(const wchar_t *_Filename,int _OpenFlag,int _PermissionMode = 0); + extern "C++" _CRTIMP int __cdecl _wsopen(const wchar_t *_Filename,int _OpenFlag,int _ShareFlag,int _PermissionMode = 0); +#endif +#endif + +#ifndef _WLOCALE_DEFINED +#define _WLOCALE_DEFINED + _CRTIMP wchar_t *__cdecl _wsetlocale(int _Category,const wchar_t *_Locale); +#endif + +#ifndef _WPROCESS_DEFINED +#define _WPROCESS_DEFINED + + _CRTIMP intptr_t __cdecl _wexecl(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexecle(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexeclp(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexeclpe(const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wexecv(const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wexecve(const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); + _CRTIMP intptr_t __cdecl _wexecvp(const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wexecvpe(const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); + _CRTIMP intptr_t __cdecl _wspawnl(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnle(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnlp(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnlpe(int _Mode,const wchar_t *_Filename,const wchar_t *_ArgList,...); + _CRTIMP intptr_t __cdecl _wspawnv(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wspawnve(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); + _CRTIMP intptr_t __cdecl _wspawnvp(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList); + _CRTIMP intptr_t __cdecl _wspawnvpe(int _Mode,const wchar_t *_Filename,const wchar_t *const *_ArgList,const wchar_t *const *_Env); +#ifndef _CRT_WSYSTEM_DEFINED +#define _CRT_WSYSTEM_DEFINED + _CRTIMP int __cdecl _wsystem(const wchar_t *_Command); +#endif +#endif + +#ifndef _WCTYPE_INLINE_DEFINED +#undef _CRT_WCTYPE_NOINLINE +#if !defined(__cplusplus) || defined(_CRT_WCTYPE_NOINLINE) +#define iswalpha(_c) (iswctype(_c,_ALPHA)) +#define iswupper(_c) (iswctype(_c,_UPPER)) +#define iswlower(_c) (iswctype(_c,_LOWER)) +#define iswdigit(_c) (iswctype(_c,_DIGIT)) +#define iswxdigit(_c) (iswctype(_c,_HEX)) +#define iswspace(_c) (iswctype(_c,_SPACE)) +#define iswpunct(_c) (iswctype(_c,_PUNCT)) +#define iswalnum(_c) (iswctype(_c,_ALPHA|_DIGIT)) +#define iswprint(_c) (iswctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT)) +#define iswgraph(_c) (iswctype(_c,_PUNCT|_ALPHA|_DIGIT)) +#define iswcntrl(_c) (iswctype(_c,_CONTROL)) +#define iswascii(_c) ((unsigned)(_c) < 0x80) + +#define _iswalpha_l(_c,_p) (_iswctype_l(_c,_ALPHA,_p)) +#define _iswupper_l(_c,_p) (_iswctype_l(_c,_UPPER,_p)) +#define _iswlower_l(_c,_p) (_iswctype_l(_c,_LOWER,_p)) +#define _iswdigit_l(_c,_p) (_iswctype_l(_c,_DIGIT,_p)) +#define _iswxdigit_l(_c,_p) (_iswctype_l(_c,_HEX,_p)) +#define _iswspace_l(_c,_p) (_iswctype_l(_c,_SPACE,_p)) +#define _iswpunct_l(_c,_p) (_iswctype_l(_c,_PUNCT,_p)) +#define _iswalnum_l(_c,_p) (_iswctype_l(_c,_ALPHA|_DIGIT,_p)) +#define _iswprint_l(_c,_p) (_iswctype_l(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT,_p)) +#define _iswgraph_l(_c,_p) (_iswctype_l(_c,_PUNCT|_ALPHA|_DIGIT,_p)) +#define _iswcntrl_l(_c,_p) (_iswctype_l(_c,_CONTROL,_p)) +#ifndef _CTYPE_DISABLE_MACROS +#define isleadbyte(_c) (__PCTYPE_FUNC[(unsigned char)(_c)] & _LEADBYTE) +#endif +#endif +#define _WCTYPE_INLINE_DEFINED +#endif + +#if !defined(_POSIX_) || defined(__GNUC__) +#ifndef _INO_T_DEFINED +#define _INO_T_DEFINED + typedef unsigned short _ino_t; +#ifndef NO_OLDNAMES + typedef unsigned short ino_t; +#endif +#endif + +#ifndef _DEV_T_DEFINED +#define _DEV_T_DEFINED + typedef unsigned int _dev_t; +#ifndef NO_OLDNAMES + typedef unsigned int dev_t; +#endif +#endif + +#ifndef _OFF_T_DEFINED +#define _OFF_T_DEFINED +#ifndef _OFF_T_ +#define _OFF_T_ + typedef long _off_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long off_t; +#endif +#endif +#endif + +#ifndef _OFF64_T_DEFINED +#define _OFF64_T_DEFINED + typedef long long _off64_t; +#if !defined(NO_OLDNAMES) || defined(_POSIX) + typedef long long off64_t; +#endif +#endif + +#ifndef _STAT_DEFINED +#define _STAT_DEFINED + +#ifdef _USE_32BIT_TIME_T +#ifdef WIN64 +#define _fstat _fstat32 +#define _stat _stat32 +#define _wstat _wstat32 +#else +#define _fstat32 _fstat +#define _stat32 _stat +#define _wstat32 _wstat +#endif +#define _fstati64 _fstat32i64 +#define _stati64 _stat32i64 +#define _wstati64 _wstat32i64 +#else +#define _fstat _fstat64i32 +#define _fstati64 _fstat64 +#define _stat _stat64i32 +#define _stati64 _stat64 +#define _wstat _wstat64i32 +#define _wstati64 _wstat64 +#endif + + struct _stat32 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + _off_t st_size; + __time32_t st_atime; + __time32_t st_mtime; + __time32_t st_ctime; + }; + +#ifndef NO_OLDNAMES + struct stat { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + _off_t st_size; + time_t st_atime; + time_t st_mtime; + time_t st_ctime; + }; +#endif + +#if _INTEGRAL_MAX_BITS >= 64 + + struct _stat32i64 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + __int64 st_size; + __time32_t st_atime; + __time32_t st_mtime; + __time32_t st_ctime; + }; + + struct _stat64i32 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + _off_t st_size; + __time64_t st_atime; + __time64_t st_mtime; + __time64_t st_ctime; + }; + + struct _stat64 { + _dev_t st_dev; + _ino_t st_ino; + unsigned short st_mode; + short st_nlink; + short st_uid; + short st_gid; + _dev_t st_rdev; + __int64 st_size; + __time64_t st_atime; + __time64_t st_mtime; + __time64_t st_ctime; + }; +#endif + +#define __stat64 _stat64 + +#endif + +#ifndef _WSTAT_DEFINED +#define _WSTAT_DEFINED + + _CRTIMP int __cdecl _wstat32(const wchar_t *_Name,struct _stat32 *_Stat); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP int __cdecl _wstat32i64(const wchar_t *_Name,struct _stat32i64 *_Stat); + int __cdecl _wstat64i32(const wchar_t *_Name,struct _stat64i32 *_Stat); + _CRTIMP int __cdecl _wstat64(const wchar_t *_Name,struct _stat64 *_Stat); +#endif +#endif +#endif + +#ifndef _WCONIO_DEFINED +#define _WCONIO_DEFINED + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + + _CRTIMP wchar_t *_cgetws(wchar_t *_Buffer); + _CRTIMP wint_t __cdecl _getwch(void); + _CRTIMP wint_t __cdecl _getwche(void); + _CRTIMP wint_t __cdecl _putwch(wchar_t _WCh); + _CRTIMP wint_t __cdecl _ungetwch(wint_t _WCh); + _CRTIMP int __cdecl _cputws(const wchar_t *_String); + _CRTIMP int __cdecl _cwprintf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _cwscanf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_p(const wchar_t *_Format,...); + _CRTIMP int __cdecl _vcwprintf_p(const wchar_t *_Format,va_list _ArgList); + + _CRTIMP int __cdecl _cwprintf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _cwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vcwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + wint_t __cdecl _putwch_nolock(wchar_t _WCh); + wint_t __cdecl _getwch_nolock(void); + wint_t __cdecl _getwche_nolock(void); + wint_t __cdecl _ungetwch_nolock(wint_t _WCh); +#endif + +#ifndef _WSTDIO_DEFINED +#define _WSTDIO_DEFINED + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + +#ifdef _POSIX_ + _CRTIMP FILE *__cdecl _wfsopen(const wchar_t *_Filename,const wchar_t *_Mode); +#else + _CRTIMP FILE *__cdecl _wfsopen(const wchar_t *_Filename,const wchar_t *_Mode,int _ShFlag); +#endif + + wint_t __cdecl fgetwc(FILE *_File); + _CRTIMP wint_t __cdecl _fgetwchar(void); + wint_t __cdecl fputwc(wchar_t _Ch,FILE *_File); + _CRTIMP wint_t __cdecl _fputwchar(wchar_t _Ch); + wint_t __cdecl getwc(FILE *_File); + wint_t __cdecl getwchar(void); + wint_t __cdecl putwc(wchar_t _Ch,FILE *_File); + wint_t __cdecl putwchar(wchar_t _Ch); + wint_t __cdecl ungetwc(wint_t _Ch,FILE *_File); + wchar_t *__cdecl fgetws(wchar_t *_Dst,int _SizeInWords,FILE *_File); + int __cdecl fputws(const wchar_t *_Str,FILE *_File); + _CRTIMP wchar_t *__cdecl _getws(wchar_t *_String); + _CRTIMP int __cdecl _putws(const wchar_t *_Str); + int __cdecl fwprintf(FILE *_File,const wchar_t *_Format,...); + int __cdecl wprintf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _scwprintf(const wchar_t *_Format,...); + int __cdecl vfwprintf(FILE *_File,const wchar_t *_Format,va_list _ArgList); + int __cdecl vwprintf(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl swprintf(wchar_t*, const wchar_t*, ...); + _CRTIMP int __cdecl vswprintf(wchar_t*, const wchar_t*,va_list); + _CRTIMP int __cdecl _swprintf_c(wchar_t *_DstBuf,size_t _SizeInWords,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vswprintf_c(wchar_t *_DstBuf,size_t _SizeInWords,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf(wchar_t *_Dest,size_t _Count,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vsnwprintf(wchar_t *_Dest,size_t _Count,const wchar_t *_Format,va_list _Args); +#ifndef __NO_ISOCEXT /* externs in libmingwex.a */ + int __cdecl snwprintf (wchar_t *s, size_t n, const wchar_t * format, ...); + __CRT_INLINE int __cdecl vsnwprintf (wchar_t *s, size_t n, const wchar_t *format, va_list arg) { return _vsnwprintf(s,n,format,arg); } + int __cdecl vwscanf (const wchar_t *, va_list); + int __cdecl vfwscanf (FILE *,const wchar_t *,va_list); + int __cdecl vswscanf (const wchar_t *,const wchar_t *,va_list); +#endif + _CRTIMP int __cdecl _fwprintf_p(FILE *_File,const wchar_t *_Format,...); + _CRTIMP int __cdecl _wprintf_p(const wchar_t *_Format,...); + _CRTIMP int __cdecl _vfwprintf_p(FILE *_File,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _vwprintf_p(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _swprintf_p(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vswprintf_p(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _scwprintf_p(const wchar_t *_Format,...); + _CRTIMP int __cdecl _vscwprintf_p(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _wprintf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _wprintf_p_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vwprintf_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _fwprintf_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _fwprintf_p_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vfwprintf_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vfwprintf_p_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _swprintf_c_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _swprintf_p_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vswprintf_c_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _vswprintf_p_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _scwprintf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _scwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vscwprintf_p_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _snwprintf_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _vsnwprintf_l(wchar_t *_DstBuf,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + _CRTIMP int __cdecl _swprintf(wchar_t *_Dest,const wchar_t *_Format,...); + _CRTIMP int __cdecl _vswprintf(wchar_t *_Dest,const wchar_t *_Format,va_list _Args); + _CRTIMP int __cdecl __swprintf_l(wchar_t *_Dest,const wchar_t *_Format,_locale_t _Plocinfo,...); + _CRTIMP int __cdecl __vswprintf_l(wchar_t *_Dest,const wchar_t *_Format,_locale_t _Plocinfo,va_list _Args); +#ifndef RC_INVOKED +#include +#endif + +#ifdef _CRT_NON_CONFORMING_SWPRINTFS +#ifndef __cplusplus +#define swprintf _swprintf +#define vswprintf _vswprintf +#define _swprintf_l __swprintf_l +#define _vswprintf_l __vswprintf_l +#endif +#endif + + _CRTIMP wchar_t *__cdecl _wtempnam(const wchar_t *_Directory,const wchar_t *_FilePrefix); + _CRTIMP int __cdecl _vscwprintf(const wchar_t *_Format,va_list _ArgList); + _CRTIMP int __cdecl _vscwprintf_l(const wchar_t *_Format,_locale_t _Locale,va_list _ArgList); + int __cdecl fwscanf(FILE *_File,const wchar_t *_Format,...); + _CRTIMP int __cdecl _fwscanf_l(FILE *_File,const wchar_t *_Format,_locale_t _Locale,...); + int __cdecl swscanf(const wchar_t *_Src,const wchar_t *_Format,...); + _CRTIMP int __cdecl _swscanf_l(const wchar_t *_Src,const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP int __cdecl _snwscanf(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,...); + _CRTIMP int __cdecl _snwscanf_l(const wchar_t *_Src,size_t _MaxCount,const wchar_t *_Format,_locale_t _Locale,...); + int __cdecl wscanf(const wchar_t *_Format,...); + _CRTIMP int __cdecl _wscanf_l(const wchar_t *_Format,_locale_t _Locale,...); + _CRTIMP FILE *__cdecl _wfdopen(int _FileHandle ,const wchar_t *_Mode); + _CRTIMP FILE *__cdecl _wfopen(const wchar_t *_Filename,const wchar_t *_Mode); + _CRTIMP FILE *__cdecl _wfreopen(const wchar_t *_Filename,const wchar_t *_Mode,FILE *_OldFile); + +#ifndef _CRT_WPERROR_DEFINED +#define _CRT_WPERROR_DEFINED + _CRTIMP void __cdecl _wperror(const wchar_t *_ErrMsg); +#endif + _CRTIMP FILE *__cdecl _wpopen(const wchar_t *_Command,const wchar_t *_Mode); +#if !defined(NO_OLDNAMES) && !defined(wpopen) +#define wpopen _wpopen +#endif + _CRTIMP int __cdecl _wremove(const wchar_t *_Filename); + _CRTIMP wchar_t *__cdecl _wtmpnam(wchar_t *_Buffer); + _CRTIMP wint_t __cdecl _fgetwc_nolock(FILE *_File); + _CRTIMP wint_t __cdecl _fputwc_nolock(wchar_t _Ch,FILE *_File); + _CRTIMP wint_t __cdecl _ungetwc_nolock(wint_t _Ch,FILE *_File); + +#undef _CRT_GETPUTWCHAR_NOINLINE + +#if !defined(__cplusplus) || defined(_CRT_GETPUTWCHAR_NOINLINE) +#define getwchar() fgetwc(stdin) +#define putwchar(_c) fputwc((_c),stdout) +#else + __CRT_INLINE wint_t __cdecl getwchar() {return (fgetwc(stdin)); } + __CRT_INLINE wint_t __cdecl putwchar(wchar_t _C) {return (fputwc(_C,stdout)); } +#endif + +#define getwc(_stm) fgetwc(_stm) +#define putwc(_c,_stm) fputwc(_c,_stm) +#define _putwc_nolock(_c,_stm) _fputwc_nolock(_c,_stm) +#define _getwc_nolock(_c) _fgetwc_nolock(_c) +#endif + +#ifndef _WSTDLIB_DEFINED +#define _WSTDLIB_DEFINED + + _CRTIMP wchar_t *__cdecl _itow(int _Value,wchar_t *_Dest,int _Radix); + _CRTIMP wchar_t *__cdecl _ltow(long _Value,wchar_t *_Dest,int _Radix); + _CRTIMP wchar_t *__cdecl _ultow(unsigned long _Value,wchar_t *_Dest,int _Radix); + double __cdecl wcstod(const wchar_t *_Str,wchar_t **_EndPtr); + _CRTIMP double __cdecl _wcstod_l(const wchar_t *_Str,wchar_t **_EndPtr,_locale_t _Locale); + float __cdecl wcstof( const wchar_t *nptr, wchar_t **endptr); +#if !defined __NO_ISOCEXT /* in libmingwex.a */ + float __cdecl wcstof (const wchar_t * __restrict__, wchar_t ** __restrict__); + long double __cdecl wcstold (const wchar_t * __restrict__, wchar_t ** __restrict__); +#endif /* __NO_ISOCEXT */ + long __cdecl wcstol(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP long __cdecl _wcstol_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); + unsigned long __cdecl wcstoul(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP unsigned long __cdecl _wcstoul_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wgetenv(const wchar_t *_VarName); +#ifndef _CRT_WSYSTEM_DEFINED +#define _CRT_WSYSTEM_DEFINED + _CRTIMP int __cdecl _wsystem(const wchar_t *_Command); +#endif + _CRTIMP double __cdecl _wtof(const wchar_t *_Str); + _CRTIMP double __cdecl _wtof_l(const wchar_t *_Str,_locale_t _Locale); + _CRTIMP int __cdecl _wtoi(const wchar_t *_Str); + _CRTIMP int __cdecl _wtoi_l(const wchar_t *_Str,_locale_t _Locale); + _CRTIMP long __cdecl _wtol(const wchar_t *_Str); + _CRTIMP long __cdecl _wtol_l(const wchar_t *_Str,_locale_t _Locale); + +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP wchar_t *__cdecl _i64tow(__int64 _Val,wchar_t *_DstBuf,int _Radix); + _CRTIMP wchar_t *__cdecl _ui64tow(unsigned __int64 _Val,wchar_t *_DstBuf,int _Radix); + _CRTIMP __int64 __cdecl _wtoi64(const wchar_t *_Str); + _CRTIMP __int64 __cdecl _wtoi64_l(const wchar_t *_Str,_locale_t _Locale); + _CRTIMP __int64 __cdecl _wcstoi64(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP __int64 __cdecl _wcstoi64_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); + _CRTIMP unsigned __int64 __cdecl _wcstoui64(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix); + _CRTIMP unsigned __int64 __cdecl _wcstoui64_l(const wchar_t *_Str,wchar_t **_EndPtr,int _Radix,_locale_t _Locale); +#endif +#endif + +#ifndef _POSIX_ +#ifndef _WSTDLIBP_DEFINED +#define _WSTDLIBP_DEFINED + _CRTIMP wchar_t *__cdecl _wfullpath(wchar_t *_FullPath,const wchar_t *_Path,size_t _SizeInWords); + _CRTIMP void __cdecl _wmakepath(wchar_t *_ResultPath,const wchar_t *_Drive,const wchar_t *_Dir,const wchar_t *_Filename,const wchar_t *_Ext); +#ifndef _CRT_WPERROR_DEFINED +#define _CRT_WPERROR_DEFINED + _CRTIMP void __cdecl _wperror(const wchar_t *_ErrMsg); +#endif + _CRTIMP int __cdecl _wputenv(const wchar_t *_EnvString); + _CRTIMP void __cdecl _wsearchenv(const wchar_t *_Filename,const wchar_t *_EnvVar,wchar_t *_ResultPath); + _CRTIMP void __cdecl _wsplitpath(const wchar_t *_FullPath,wchar_t *_Drive,wchar_t *_Dir,wchar_t *_Filename,wchar_t *_Ext); +#endif +#endif + +#ifndef _WSTRING_DEFINED +#define _WSTRING_DEFINED + _CRTIMP wchar_t *__cdecl _wcsdup(const wchar_t *_Str); + wchar_t *__cdecl wcscat(wchar_t *_Dest,const wchar_t *_Source); + _CONST_RETURN wchar_t *__cdecl wcschr(const wchar_t *_Str,wchar_t _Ch); + int __cdecl wcscmp(const wchar_t *_Str1,const wchar_t *_Str2); + wchar_t *__cdecl wcscpy(wchar_t *_Dest,const wchar_t *_Source); + size_t __cdecl wcscspn(const wchar_t *_Str,const wchar_t *_Control); + size_t __cdecl wcslen(const wchar_t *_Str); + size_t __cdecl wcsnlen(const wchar_t *_Src,size_t _MaxCount); + wchar_t *__cdecl wcsncat(wchar_t *_Dest,const wchar_t *_Source,size_t _Count); + int __cdecl wcsncmp(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + wchar_t *__cdecl wcsncpy(wchar_t *_Dest,const wchar_t *_Source,size_t _Count); + _CONST_RETURN wchar_t *__cdecl wcspbrk(const wchar_t *_Str,const wchar_t *_Control); + _CONST_RETURN wchar_t *__cdecl wcsrchr(const wchar_t *_Str,wchar_t _Ch); + size_t __cdecl wcsspn(const wchar_t *_Str,const wchar_t *_Control); + _CONST_RETURN wchar_t *__cdecl wcsstr(const wchar_t *_Str,const wchar_t *_SubStr); + wchar_t *__cdecl wcstok(wchar_t *_Str,const wchar_t *_Delim); + _CRTIMP wchar_t *__cdecl _wcserror(int _ErrNum); + _CRTIMP wchar_t *__cdecl __wcserror(const wchar_t *_Str); + _CRTIMP int __cdecl _wcsicmp(const wchar_t *_Str1,const wchar_t *_Str2); + _CRTIMP int __cdecl _wcsicmp_l(const wchar_t *_Str1,const wchar_t *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _wcsnicmp(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _wcsnicmp_l(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wcsnset(wchar_t *_Str,wchar_t _Val,size_t _MaxCount); + _CRTIMP wchar_t *__cdecl _wcsrev(wchar_t *_Str); + _CRTIMP wchar_t *__cdecl _wcsset(wchar_t *_Str,wchar_t _Val); + _CRTIMP wchar_t *__cdecl _wcslwr(wchar_t *_String); + _CRTIMP wchar_t *_wcslwr_l(wchar_t *_String,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wcsupr(wchar_t *_String); + _CRTIMP wchar_t *_wcsupr_l(wchar_t *_String,_locale_t _Locale); + size_t __cdecl wcsxfrm(wchar_t *_Dst,const wchar_t *_Src,size_t _MaxCount); + _CRTIMP size_t __cdecl _wcsxfrm_l(wchar_t *_Dst,const wchar_t *_Src,size_t _MaxCount,_locale_t _Locale); + int __cdecl wcscoll(const wchar_t *_Str1,const wchar_t *_Str2); + _CRTIMP int __cdecl _wcscoll_l(const wchar_t *_Str1,const wchar_t *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _wcsicoll(const wchar_t *_Str1,const wchar_t *_Str2); + _CRTIMP int __cdecl _wcsicoll_l(const wchar_t *_Str1,const wchar_t *_Str2,_locale_t _Locale); + _CRTIMP int __cdecl _wcsncoll(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _wcsncoll_l(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount,_locale_t _Locale); + _CRTIMP int __cdecl _wcsnicoll(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + _CRTIMP int __cdecl _wcsnicoll_l(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount,_locale_t _Locale); + +#ifndef NO_OLDNAMES + wchar_t *__cdecl wcsdup(const wchar_t *_Str); +#define wcswcs wcsstr + int __cdecl wcsicmp(const wchar_t *_Str1,const wchar_t *_Str2); + int __cdecl wcsnicmp(const wchar_t *_Str1,const wchar_t *_Str2,size_t _MaxCount); + wchar_t *__cdecl wcsnset(wchar_t *_Str,wchar_t _Val,size_t _MaxCount); + wchar_t *__cdecl wcsrev(wchar_t *_Str); + wchar_t *__cdecl wcsset(wchar_t *_Str,wchar_t _Val); + wchar_t *__cdecl wcslwr(wchar_t *_Str); + wchar_t *__cdecl wcsupr(wchar_t *_Str); + int __cdecl wcsicoll(const wchar_t *_Str1,const wchar_t *_Str2); +#endif +#endif + +#ifndef _TM_DEFINED +#define _TM_DEFINED + struct tm { + int tm_sec; + int tm_min; + int tm_hour; + int tm_mday; + int tm_mon; + int tm_year; + int tm_wday; + int tm_yday; + int tm_isdst; + }; +#endif + +#ifndef _WTIME_DEFINED +#define _WTIME_DEFINED + + _CRTIMP wchar_t *__cdecl _wasctime(const struct tm *_Tm); + _CRTIMP wchar_t *__cdecl _wctime32(const __time32_t *_Time); + size_t __cdecl wcsftime(wchar_t *_Buf,size_t _SizeInWords,const wchar_t *_Format,const struct tm *_Tm); + _CRTIMP size_t __cdecl _wcsftime_l(wchar_t *_Buf,size_t _SizeInWords,const wchar_t *_Format,const struct tm *_Tm,_locale_t _Locale); + _CRTIMP wchar_t *__cdecl _wstrdate(wchar_t *_Buffer); + _CRTIMP wchar_t *__cdecl _wstrtime(wchar_t *_Buffer); +#if _INTEGRAL_MAX_BITS >= 64 + _CRTIMP wchar_t *__cdecl _wctime64(const __time64_t *_Time); +#endif + +#if !defined (RC_INVOKED) && !defined (_INC_WTIME_INL) +#define _INC_WTIME_INL +#ifdef _USE_32BIT_TIME_T +__CRT_INLINE wchar_t *__cdecl _wctime(const time_t *_Time) { return _wctime32(_Time); } +#else +__CRT_INLINE wchar_t *__cdecl _wctime(const time_t *_Time) { return _wctime64(_Time); } +#endif +#endif +#endif + + typedef int mbstate_t; + typedef wchar_t _Wint_t; + + wint_t __cdecl btowc(int); + size_t __cdecl mbrlen(const char *_Ch,size_t _SizeInBytes,mbstate_t *_State); + size_t __cdecl mbrtowc(wchar_t *_DstCh,const char *_SrcCh,size_t _SizeInBytes,mbstate_t *_State); + size_t __cdecl mbsrtowcs(wchar_t *_Dest,const char **_PSrc,size_t _Count,mbstate_t *_State); + size_t __cdecl wcrtomb(char *_Dest,wchar_t _Source,mbstate_t *_State); + size_t __cdecl wcsrtombs(char *_Dest,const wchar_t **_PSource,size_t _Count,mbstate_t *_State); + int __cdecl wctob(wint_t _WCh); + +#ifndef __NO_ISOCEXT /* these need static lib libmingwex.a */ + wchar_t *__cdecl wmemset(wchar_t *s, wchar_t c, size_t n); + _CONST_RETURN wchar_t *__cdecl wmemchr(const wchar_t *s, wchar_t c, size_t n); + int wmemcmp(const wchar_t *s1, const wchar_t *s2,size_t n); + wchar_t *__cdecl wmemcpy(wchar_t *s1,const wchar_t *s2,size_t n); + wchar_t *__cdecl wmemmove(wchar_t *s1, const wchar_t *s2, size_t n); + long long __cdecl wcstoll(const wchar_t *nptr,wchar_t **endptr, int base); + unsigned long long __cdecl wcstoull(const wchar_t *nptr,wchar_t **endptr, int base); +#endif /* __NO_ISOCEXT */ + + void *__cdecl memmove(void *_Dst,const void *_Src,size_t _MaxCount); + void *__cdecl memcpy(void *_Dst,const void *_Src,size_t _MaxCount); + __CRT_INLINE int __cdecl fwide(FILE *_F,int _M) { (void)_F; return (_M); } + __CRT_INLINE int __cdecl mbsinit(const mbstate_t *_P) { return (!_P || *_P==0); } + __CRT_INLINE _CONST_RETURN wchar_t *__cdecl wmemchr(const wchar_t *_S,wchar_t _C,size_t _N) { for (;0<_N;++_S,--_N) if (*_S==_C) return (_CONST_RETURN wchar_t *)(_S); return (0); } + __CRT_INLINE int __cdecl wmemcmp(const wchar_t *_S1,const wchar_t *_S2,size_t _N) { for (; 0 < _N; ++_S1,++_S2,--_N) if (*_S1!=*_S2) return (*_S1 < *_S2 ? -1 : +1); return (0); } + __CRT_INLINE wchar_t *__cdecl wmemcpy(wchar_t *_S1,const wchar_t *_S2,size_t _N) { return (wchar_t *)memcpy(_S1,_S2,_N*sizeof(wchar_t)); } + __CRT_INLINE wchar_t *__cdecl wmemmove(wchar_t *_S1,const wchar_t *_S2,size_t _N) { return (wchar_t *)memmove(_S1,_S2,_N*sizeof(wchar_t)); } + __CRT_INLINE wchar_t *__cdecl wmemset(wchar_t *_S,wchar_t _C,size_t _N) { + wchar_t *_Su = _S; + for (;0<_N;++_Su,--_N) { + *_Su = _C; + } + return (_S); + } +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) + +#include +#endif diff --git a/05/tcc-final-old/win32/include/wctype.h b/05/tcc-final-old/win32/include/wctype.h new file mode 100644 index 0000000..a44cb38 --- /dev/null +++ b/05/tcc-final-old/win32/include/wctype.h @@ -0,0 +1,172 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _INC_WCTYPE +#define _INC_WCTYPE + +#ifndef _WIN32 +#error Only Win32 target is supported! +#endif + +#include <_mingw.h> + +#pragma pack(push,_CRT_PACKING) + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _CRTIMP +#define _CRTIMP __declspec(dllimport) +#endif + +#ifndef _WCHAR_T_DEFINED + typedef unsigned short wchar_t; +#define _WCHAR_T_DEFINED +#endif + +#ifndef _WCTYPE_T_DEFINED + typedef unsigned short wint_t; + typedef unsigned short wctype_t; +#define _WCTYPE_T_DEFINED +#endif + +#ifndef WEOF +#define WEOF (wint_t)(0xFFFF) +#endif + +#ifndef _CRT_CTYPEDATA_DEFINED +#define _CRT_CTYPEDATA_DEFINED +#ifndef _CTYPE_DISABLE_MACROS + +#ifndef __PCTYPE_FUNC +#define __PCTYPE_FUNC __pctype_func() +#ifdef _MSVCRT_ +#define __pctype_func() (_pctype) +#else +#define __pctype_func() (*_imp___pctype) +#endif +#endif + +#ifndef _pctype +#ifdef _MSVCRT_ + extern unsigned short *_pctype; +#else + extern unsigned short **_imp___pctype; +#define _pctype (*_imp___pctype) +#endif +#endif + +#endif +#endif + +#ifndef _CRT_WCTYPEDATA_DEFINED +#define _CRT_WCTYPEDATA_DEFINED +#ifndef _CTYPE_DISABLE_MACROS +#ifndef _wctype +#ifdef _MSVCRT_ + extern unsigned short *_wctype; +#else + extern unsigned short **_imp___wctype; +#define _wctype (*_imp___wctype) +#endif +#endif + +#ifndef _pwctype +#ifdef _MSVCRT_ + extern unsigned short *_pwctype; +#else + extern unsigned short **_imp___pwctype; +#define _pwctype (*_imp___pwctype) +#define __pwctype_func() (*_imp___pwctype) +#endif +#endif +#endif +#endif + +#define _UPPER 0x1 +#define _LOWER 0x2 +#define _DIGIT 0x4 +#define _SPACE 0x8 + +#define _PUNCT 0x10 +#define _CONTROL 0x20 +#define _BLANK 0x40 +#define _HEX 0x80 + +#define _LEADBYTE 0x8000 +#define _ALPHA (0x0100|_UPPER|_LOWER) + +#ifndef _WCTYPE_DEFINED +#define _WCTYPE_DEFINED + + int __cdecl iswalpha(wint_t); + int __cdecl iswupper(wint_t); + int __cdecl iswlower(wint_t); + int __cdecl iswdigit(wint_t); + int __cdecl iswxdigit(wint_t); + int __cdecl iswspace(wint_t); + int __cdecl iswpunct(wint_t); + int __cdecl iswalnum(wint_t); + int __cdecl iswprint(wint_t); + int __cdecl iswgraph(wint_t); + int __cdecl iswcntrl(wint_t); + int __cdecl iswascii(wint_t); + int __cdecl isleadbyte(int); + wint_t __cdecl towupper(wint_t); + wint_t __cdecl towlower(wint_t); + int __cdecl iswctype(wint_t,wctype_t); + _CRTIMP int __cdecl __iswcsymf(wint_t); + _CRTIMP int __cdecl __iswcsym(wint_t); + int __cdecl is_wctype(wint_t,wctype_t); +#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || !defined (NO_OLDNAMES) +int __cdecl isblank(int _C); +#endif +#endif + +#ifndef _WCTYPE_INLINE_DEFINED +#define _WCTYPE_INLINE_DEFINED +#ifndef __cplusplus +#define iswalpha(_c) (iswctype(_c,_ALPHA)) +#define iswupper(_c) (iswctype(_c,_UPPER)) +#define iswlower(_c) (iswctype(_c,_LOWER)) +#define iswdigit(_c) (iswctype(_c,_DIGIT)) +#define iswxdigit(_c) (iswctype(_c,_HEX)) +#define iswspace(_c) (iswctype(_c,_SPACE)) +#define iswpunct(_c) (iswctype(_c,_PUNCT)) +#define iswalnum(_c) (iswctype(_c,_ALPHA|_DIGIT)) +#define iswprint(_c) (iswctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT)) +#define iswgraph(_c) (iswctype(_c,_PUNCT|_ALPHA|_DIGIT)) +#define iswcntrl(_c) (iswctype(_c,_CONTROL)) +#define iswascii(_c) ((unsigned)(_c) < 0x80) +#define isleadbyte(c) (__pctype_func()[(unsigned char)(c)] & _LEADBYTE) +#else + __CRT_INLINE int __cdecl iswalpha(wint_t _C) {return (iswctype(_C,_ALPHA)); } + __CRT_INLINE int __cdecl iswupper(wint_t _C) {return (iswctype(_C,_UPPER)); } + __CRT_INLINE int __cdecl iswlower(wint_t _C) {return (iswctype(_C,_LOWER)); } + __CRT_INLINE int __cdecl iswdigit(wint_t _C) {return (iswctype(_C,_DIGIT)); } + __CRT_INLINE int __cdecl iswxdigit(wint_t _C) {return (iswctype(_C,_HEX)); } + __CRT_INLINE int __cdecl iswspace(wint_t _C) {return (iswctype(_C,_SPACE)); } + __CRT_INLINE int __cdecl iswpunct(wint_t _C) {return (iswctype(_C,_PUNCT)); } + __CRT_INLINE int __cdecl iswalnum(wint_t _C) {return (iswctype(_C,_ALPHA|_DIGIT)); } + __CRT_INLINE int __cdecl iswprint(wint_t _C) {return (iswctype(_C,_BLANK|_PUNCT|_ALPHA|_DIGIT)); } + __CRT_INLINE int __cdecl iswgraph(wint_t _C) {return (iswctype(_C,_PUNCT|_ALPHA|_DIGIT)); } + __CRT_INLINE int __cdecl iswcntrl(wint_t _C) {return (iswctype(_C,_CONTROL)); } + __CRT_INLINE int __cdecl iswascii(wint_t _C) {return ((unsigned)(_C) < 0x80); } + __CRT_INLINE int __cdecl isleadbyte(int _C) {return (__pctype_func()[(unsigned char)(_C)] & _LEADBYTE); } +#endif +#endif + + typedef wchar_t wctrans_t; + wint_t __cdecl towctrans(wint_t,wctrans_t); + wctrans_t __cdecl wctrans(const char *); + wctype_t __cdecl wctype(const char *); + +#ifdef __cplusplus +} +#endif + +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/winapi/basetsd.h b/05/tcc-final-old/win32/include/winapi/basetsd.h new file mode 100644 index 0000000..47d78c4 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/basetsd.h @@ -0,0 +1,149 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _BASETSD_H_ +#define _BASETSD_H_ + +#if (defined(__x86_64) || defined(__ia64__)) && !defined(RC_INVOKED) +typedef unsigned __int64 POINTER_64_INT; +#else +typedef unsigned long POINTER_64_INT; +#endif + +#define POINTER_32 +#define POINTER_64 +#define FIRMWARE_PTR + +#ifdef __cplusplus +extern "C" { +#endif + + typedef signed char INT8,*PINT8; + typedef signed short INT16,*PINT16; + typedef signed int INT32,*PINT32; + typedef signed __int64 INT64,*PINT64; + typedef unsigned char UINT8,*PUINT8; + typedef unsigned short UINT16,*PUINT16; + typedef unsigned int UINT32,*PUINT32; + typedef unsigned __int64 UINT64,*PUINT64; + typedef signed int LONG32,*PLONG32; + typedef unsigned int ULONG32,*PULONG32; + typedef unsigned int DWORD32,*PDWORD32; + +#ifndef _W64 +#define _W64 +#endif + +#ifdef _WIN64 + typedef __int64 INT_PTR,*PINT_PTR; + typedef unsigned __int64 UINT_PTR,*PUINT_PTR; + typedef __int64 LONG_PTR,*PLONG_PTR; + typedef unsigned __int64 ULONG_PTR,*PULONG_PTR; +#define __int3264 __int64 +#else + typedef int INT_PTR,*PINT_PTR; + typedef unsigned int UINT_PTR,*PUINT_PTR; + typedef long LONG_PTR,*PLONG_PTR; + typedef unsigned long ULONG_PTR,*PULONG_PTR; +#define __int3264 __int32 +#endif + +#ifdef _WIN64 +#define ADDRESS_TAG_BIT 0x40000000000ULL + typedef __int64 SHANDLE_PTR; + typedef unsigned __int64 HANDLE_PTR; + typedef unsigned int UHALF_PTR,*PUHALF_PTR; + typedef int HALF_PTR,*PHALF_PTR; + + static __inline unsigned long HandleToULong(const void *h) { return((unsigned long) (ULONG_PTR) h); } + static __inline long HandleToLong(const void *h) { return((long) (LONG_PTR) h); } + static __inline void *ULongToHandle(const unsigned long h) { return((void *) (UINT_PTR) h); } + static __inline void *LongToHandle(const long h) { return((void *) (INT_PTR) h); } + static __inline unsigned long PtrToUlong(const void *p) { return((unsigned long) (ULONG_PTR) p); } + static __inline unsigned int PtrToUint(const void *p) { return((unsigned int) (UINT_PTR) p); } + static __inline unsigned short PtrToUshort(const void *p) { return((unsigned short) (unsigned long) (ULONG_PTR) p); } + static __inline long PtrToLong(const void *p) { return((long) (LONG_PTR) p); } + static __inline int PtrToInt(const void *p) { return((int) (INT_PTR) p); } + static __inline short PtrToShort(const void *p) { return((short) (long) (LONG_PTR) p); } + static __inline void *IntToPtr(const int i) { return((void *)(INT_PTR)i); } + static __inline void *UIntToPtr(const unsigned int ui) { return((void *)(UINT_PTR)ui); } + static __inline void *LongToPtr(const long l) { return((void *)(LONG_PTR)l); } + static __inline void *ULongToPtr(const unsigned long ul) { return((void *)(ULONG_PTR)ul); } + +#define PtrToPtr64(p) ((void *) p) +#define Ptr64ToPtr(p) ((void *) p) +#define HandleToHandle64(h) (PtrToPtr64(h)) +#define Handle64ToHandle(h) (Ptr64ToPtr(h)) + + static __inline void *Ptr32ToPtr(const void *p) { return (void *)p; } + static __inline void *Handle32ToHandle(const void *h) { return((void *) h); } + static __inline void *PtrToPtr32(const void *p) { return((void *) (ULONG_PTR) p); } + +#define HandleToHandle32(h) (PtrToPtr32(h)) +#else + +#define ADDRESS_TAG_BIT 0x80000000UL + + typedef unsigned short UHALF_PTR,*PUHALF_PTR; + typedef short HALF_PTR,*PHALF_PTR; + typedef long SHANDLE_PTR; + typedef unsigned long HANDLE_PTR; + +#define HandleToULong(h) ((ULONG)(ULONG_PTR)(h)) +#define HandleToLong(h) ((LONG)(LONG_PTR) (h)) +#define ULongToHandle(ul) ((HANDLE)(ULONG_PTR) (ul)) +#define LongToHandle(h) ((HANDLE)(LONG_PTR) (h)) +#define PtrToUlong(p) ((ULONG)(ULONG_PTR) (p)) +#define PtrToLong(p) ((LONG)(LONG_PTR) (p)) +#define PtrToUint(p) ((UINT)(UINT_PTR) (p)) +#define PtrToInt(p) ((INT)(INT_PTR) (p)) +#define PtrToUshort(p) ((unsigned short)(ULONG_PTR)(p)) +#define PtrToShort(p) ((short)(LONG_PTR)(p)) +#define IntToPtr(i) ((VOID *)(INT_PTR)((int)i)) +#define UIntToPtr(ui) ((VOID *)(UINT_PTR)((unsigned int)ui)) +#define LongToPtr(l) ((VOID *)(LONG_PTR)((long)l)) +#define ULongToPtr(ul) ((VOID *)(ULONG_PTR)((unsigned long)ul)) + + static __inline void *PtrToPtr64(const void *p) { return((void *) (ULONG_PTR)p); } + static __inline void *Ptr64ToPtr(const void *p) { return((void *) (ULONG_PTR) p); } + static __inline void *HandleToHandle64(const void *h) { return((void *) h); } + static __inline void *Handle64ToHandle(const void *h) { return((void *) (ULONG_PTR) h); } + +#define Ptr32ToPtr(p) ((void *) p) +#define Handle32ToHandle(h) (Ptr32ToPtr(h)) +#define PtrToPtr32(p) ((void *) p) +#define HandleToHandle32(h) (PtrToPtr32(h)) +#endif + +#define HandleToUlong(h) HandleToULong(h) +#define UlongToHandle(ul) ULongToHandle(ul) +#define UlongToPtr(ul) ULongToPtr(ul) +#define UintToPtr(ui) UIntToPtr(ui) + +#define MAXUINT_PTR (~((UINT_PTR)0)) +#define MAXINT_PTR ((INT_PTR)(MAXUINT_PTR >> 1)) +#define MININT_PTR (~MAXINT_PTR) + +#define MAXULONG_PTR (~((ULONG_PTR)0)) +#define MAXLONG_PTR ((LONG_PTR)(MAXULONG_PTR >> 1)) +#define MINLONG_PTR (~MAXLONG_PTR) + +#define MAXUHALF_PTR ((UHALF_PTR)~0) +#define MAXHALF_PTR ((HALF_PTR)(MAXUHALF_PTR >> 1)) +#define MINHALF_PTR (~MAXHALF_PTR) + + typedef ULONG_PTR SIZE_T,*PSIZE_T; + typedef LONG_PTR SSIZE_T,*PSSIZE_T; + typedef ULONG_PTR DWORD_PTR,*PDWORD_PTR; + typedef __int64 LONG64,*PLONG64; + typedef unsigned __int64 ULONG64,*PULONG64; + typedef unsigned __int64 DWORD64,*PDWORD64; + typedef ULONG_PTR KAFFINITY; + typedef KAFFINITY *PKAFFINITY; + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/basetyps.h b/05/tcc-final-old/win32/include/winapi/basetyps.h new file mode 100644 index 0000000..376665e --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/basetyps.h @@ -0,0 +1,85 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#if !defined(_BASETYPS_H_) +#define _BASETYPS_H_ + +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C extern +#endif + +#define STDMETHODCALLTYPE WINAPI +#define STDMETHODVCALLTYPE __cdecl + +#define STDAPICALLTYPE WINAPI +#define STDAPIVCALLTYPE __cdecl + +#define STDAPI EXTERN_C HRESULT WINAPI +#define STDAPI_(type) EXTERN_C type WINAPI + +#define STDMETHODIMP HRESULT WINAPI +#define STDMETHODIMP_(type) type WINAPI + +#define STDAPIV EXTERN_C HRESULT STDAPIVCALLTYPE +#define STDAPIV_(type) EXTERN_C type STDAPIVCALLTYPE + +#define STDMETHODIMPV HRESULT STDMETHODVCALLTYPE +#define STDMETHODIMPV_(type) type STDMETHODVCALLTYPE + +#if defined(__cplusplus) && !defined(CINTERFACE) + +#define __STRUCT__ struct +#define STDMETHOD(method) virtual HRESULT WINAPI method +#define STDMETHOD_(type,method) virtual type WINAPI method +#define STDMETHODV(method) virtual HRESULT STDMETHODVCALLTYPE method +#define STDMETHODV_(type,method) virtual type STDMETHODVCALLTYPE method +#define PURE = 0 +#define THIS_ +#define THIS void +#define DECLARE_INTERFACE(iface) __STRUCT__ iface +#define DECLARE_INTERFACE_(iface,baseiface) __STRUCT__ iface : public baseiface +#else + +#ifndef __OBJC__ +#define interface struct +#endif + +#define STDMETHOD(method) HRESULT (WINAPI *method) +#define STDMETHOD_(type,method) type (WINAPI *method) +#define STDMETHODV(method) HRESULT (STDMETHODVCALLTYPE *method) +#define STDMETHODV_(type,method) type (STDMETHODVCALLTYPE *method) + +#define PURE +#define THIS_ INTERFACE *This, +#define THIS INTERFACE *This +#ifdef CONST_VTABLE +#define DECLARE_INTERFACE(iface) typedef struct iface { \ + const struct iface##Vtbl *lpVtbl; } iface; \ + typedef const struct iface##Vtbl iface##Vtbl; \ + const struct iface##Vtbl +#else +#define DECLARE_INTERFACE(iface) typedef struct iface { \ + struct iface##Vtbl *lpVtbl; \ + } iface; \ + typedef struct iface##Vtbl iface##Vtbl; \ + struct iface##Vtbl +#endif +#define DECLARE_INTERFACE_(iface,baseiface) DECLARE_INTERFACE(iface) +#endif + +#include + +#ifndef _ERROR_STATUS_T_DEFINED +#define _ERROR_STATUS_T_DEFINED +typedef unsigned long error_status_t; +#endif + +#ifndef _WCHAR_T_DEFINED +typedef unsigned short wchar_t; +#define _WCHAR_T_DEFINED +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/guiddef.h b/05/tcc-final-old/win32/include/winapi/guiddef.h new file mode 100644 index 0000000..4e7909a --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/guiddef.h @@ -0,0 +1,156 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef GUID_DEFINED +#define GUID_DEFINED +typedef struct _GUID { + unsigned long Data1; + unsigned short Data2; + unsigned short Data3; + unsigned char Data4[8 ]; +} GUID; +#endif + +#ifndef UUID_DEFINED +#define UUID_DEFINED +typedef GUID UUID; +#endif + +#ifndef FAR +#define FAR +#endif + +#ifndef DECLSPEC_SELECTANY +#define DECLSPEC_SELECTANY __declspec(selectany) +#endif + +#ifndef EXTERN_C +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C extern +#endif +#endif + +#ifdef DEFINE_GUID +#undef DEFINE_GUID +#endif + +#ifdef INITGUID +#ifdef __cplusplus +#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) EXTERN_C const GUID DECLSPEC_SELECTANY name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } } +#else +#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) const GUID DECLSPEC_SELECTANY name = { l,w1,w2,{ b1,b2,b3,b4,b5,b6,b7,b8 } } +#endif +#else +#define DEFINE_GUID(name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) EXTERN_C const GUID name +#endif + +#define DEFINE_OLEGUID(name,l,w1,w2) DEFINE_GUID(name,l,w1,w2,0xC0,0,0,0,0,0,0,0x46) + +#ifndef _GUIDDEF_H_ +#define _GUIDDEF_H_ + +#ifndef __LPGUID_DEFINED__ +#define __LPGUID_DEFINED__ +typedef GUID *LPGUID; +#endif + +#ifndef __LPCGUID_DEFINED__ +#define __LPCGUID_DEFINED__ +typedef const GUID *LPCGUID; +#endif + +#ifndef __IID_DEFINED__ +#define __IID_DEFINED__ + +typedef GUID IID; +typedef IID *LPIID; +#define IID_NULL GUID_NULL +#define IsEqualIID(riid1,riid2) IsEqualGUID(riid1,riid2) +typedef GUID CLSID; +typedef CLSID *LPCLSID; +#define CLSID_NULL GUID_NULL +#define IsEqualCLSID(rclsid1,rclsid2) IsEqualGUID(rclsid1,rclsid2) +typedef GUID FMTID; +typedef FMTID *LPFMTID; +#define FMTID_NULL GUID_NULL +#define IsEqualFMTID(rfmtid1,rfmtid2) IsEqualGUID(rfmtid1,rfmtid2) + +#ifdef __midl_proxy +#define __MIDL_CONST +#else +#define __MIDL_CONST const +#endif + +#ifndef _REFGUID_DEFINED +#define _REFGUID_DEFINED +#ifdef __cplusplus +#define REFGUID const GUID & +#else +#define REFGUID const GUID *__MIDL_CONST +#endif +#endif + +#ifndef _REFIID_DEFINED +#define _REFIID_DEFINED +#ifdef __cplusplus +#define REFIID const IID & +#else +#define REFIID const IID *__MIDL_CONST +#endif +#endif + +#ifndef _REFCLSID_DEFINED +#define _REFCLSID_DEFINED +#ifdef __cplusplus +#define REFCLSID const IID & +#else +#define REFCLSID const IID *__MIDL_CONST +#endif +#endif + +#ifndef _REFFMTID_DEFINED +#define _REFFMTID_DEFINED +#ifdef __cplusplus +#define REFFMTID const IID & +#else +#define REFFMTID const IID *__MIDL_CONST +#endif +#endif +#endif + +#ifndef _SYS_GUID_OPERATORS_ +#define _SYS_GUID_OPERATORS_ +#include + +#ifdef __cplusplus +__inline int InlineIsEqualGUID(REFGUID rguid1,REFGUID rguid2) { + return (((unsigned long *) &rguid1)[0]==((unsigned long *) &rguid2)[0] && ((unsigned long *) &rguid1)[1]==((unsigned long *) &rguid2)[1] && + ((unsigned long *) &rguid1)[2]==((unsigned long *) &rguid2)[2] && ((unsigned long *) &rguid1)[3]==((unsigned long *) &rguid2)[3]); +} +__inline int IsEqualGUID(REFGUID rguid1,REFGUID rguid2) { return !memcmp(&rguid1,&rguid2,sizeof(GUID)); } +#else +#define InlineIsEqualGUID(rguid1,rguid2) (((unsigned long *) rguid1)[0]==((unsigned long *) rguid2)[0] && ((unsigned long *) rguid1)[1]==((unsigned long *) rguid2)[1] && ((unsigned long *) rguid1)[2]==((unsigned long *) rguid2)[2] && ((unsigned long *) rguid1)[3]==((unsigned long *) rguid2)[3]) +#define IsEqualGUID(rguid1,rguid2) (!memcmp(rguid1,rguid2,sizeof(GUID))) +#endif + +#ifdef __INLINE_ISEQUAL_GUID +#undef IsEqualGUID +#define IsEqualGUID(rguid1,rguid2) InlineIsEqualGUID(rguid1,rguid2) +#endif + +#define IsEqualIID(riid1,riid2) IsEqualGUID(riid1,riid2) +#define IsEqualCLSID(rclsid1,rclsid2) IsEqualGUID(rclsid1,rclsid2) + +#if !defined _SYS_GUID_OPERATOR_EQ_ && !defined _NO_SYS_GUID_OPERATOR_EQ_ +#define _SYS_GUID_OPERATOR_EQ_ +#ifdef __cplusplus +__inline int operator==(REFGUID guidOne,REFGUID guidOther) { return IsEqualGUID(guidOne,guidOther); } +__inline int operator!=(REFGUID guidOne,REFGUID guidOther) { return !(guidOne==guidOther); } +#endif +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/poppack.h b/05/tcc-final-old/win32/include/winapi/poppack.h new file mode 100644 index 0000000..b08cba2 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/poppack.h @@ -0,0 +1,8 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#if !(defined(lint) || defined(RC_INVOKED)) +#pragma pack(pop) +#endif diff --git a/05/tcc-final-old/win32/include/winapi/pshpack1.h b/05/tcc-final-old/win32/include/winapi/pshpack1.h new file mode 100644 index 0000000..d18d9e8 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/pshpack1.h @@ -0,0 +1,8 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#if !(defined(lint) || defined(RC_INVOKED)) +#pragma pack(push,1) +#endif diff --git a/05/tcc-final-old/win32/include/winapi/pshpack2.h b/05/tcc-final-old/win32/include/winapi/pshpack2.h new file mode 100644 index 0000000..7de16fd --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/pshpack2.h @@ -0,0 +1,8 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#if !(defined(lint) || defined(RC_INVOKED)) +#pragma pack(push,2) +#endif diff --git a/05/tcc-final-old/win32/include/winapi/pshpack4.h b/05/tcc-final-old/win32/include/winapi/pshpack4.h new file mode 100644 index 0000000..1c8e61d --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/pshpack4.h @@ -0,0 +1,8 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#if !(defined(lint) || defined(RC_INVOKED)) +#pragma pack(push,4) +#endif diff --git a/05/tcc-final-old/win32/include/winapi/pshpack8.h b/05/tcc-final-old/win32/include/winapi/pshpack8.h new file mode 100644 index 0000000..70a3c7f --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/pshpack8.h @@ -0,0 +1,8 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#if !(defined(lint) || defined(RC_INVOKED)) +#pragma pack(push,8) +#endif diff --git a/05/tcc-final-old/win32/include/winapi/winbase.h b/05/tcc-final-old/win32/include/winapi/winbase.h new file mode 100644 index 0000000..4a38006 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/winbase.h @@ -0,0 +1,2951 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINBASE_ +#define _WINBASE_ + +#define WINADVAPI DECLSPEC_IMPORT +#define WINBASEAPI DECLSPEC_IMPORT +#define ZAWPROXYAPI DECLSPEC_IMPORT + +#ifdef __cplusplus +extern "C" { +#endif + +#define DefineHandleTable(w) ((w),TRUE) +#define LimitEmsPages(dw) +#define SetSwapAreaSize(w) (w) +#define LockSegment(w) GlobalFix((HANDLE)(w)) +#define UnlockSegment(w) GlobalUnfix((HANDLE)(w)) +#define GetCurrentTime() GetTickCount() + +#define Yield() + +#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1) +#define INVALID_FILE_SIZE ((DWORD)0xffffffff) +#define INVALID_SET_FILE_POINTER ((DWORD)-1) +#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) + +#define FILE_BEGIN 0 +#define FILE_CURRENT 1 +#define FILE_END 2 + +#define TIME_ZONE_ID_INVALID ((DWORD)0xffffffff) + +#define WAIT_FAILED ((DWORD)0xffffffff) +#define WAIT_OBJECT_0 ((STATUS_WAIT_0) + 0) +#define WAIT_ABANDONED ((STATUS_ABANDONED_WAIT_0) + 0) +#define WAIT_ABANDONED_0 ((STATUS_ABANDONED_WAIT_0) + 0) +#define WAIT_IO_COMPLETION STATUS_USER_APC +#define STILL_ACTIVE STATUS_PENDING +#define EXCEPTION_ACCESS_VIOLATION STATUS_ACCESS_VIOLATION +#define EXCEPTION_DATATYPE_MISALIGNMENT STATUS_DATATYPE_MISALIGNMENT +#define EXCEPTION_BREAKPOINT STATUS_BREAKPOINT +#define EXCEPTION_SINGLE_STEP STATUS_SINGLE_STEP +#define EXCEPTION_ARRAY_BOUNDS_EXCEEDED STATUS_ARRAY_BOUNDS_EXCEEDED +#define EXCEPTION_FLT_DENORMAL_OPERAND STATUS_FLOAT_DENORMAL_OPERAND +#define EXCEPTION_FLT_DIVIDE_BY_ZERO STATUS_FLOAT_DIVIDE_BY_ZERO +#define EXCEPTION_FLT_INEXACT_RESULT STATUS_FLOAT_INEXACT_RESULT +#define EXCEPTION_FLT_INVALID_OPERATION STATUS_FLOAT_INVALID_OPERATION +#define EXCEPTION_FLT_OVERFLOW STATUS_FLOAT_OVERFLOW +#define EXCEPTION_FLT_STACK_CHECK STATUS_FLOAT_STACK_CHECK +#define EXCEPTION_FLT_UNDERFLOW STATUS_FLOAT_UNDERFLOW +#define EXCEPTION_INT_DIVIDE_BY_ZERO STATUS_INTEGER_DIVIDE_BY_ZERO +#define EXCEPTION_INT_OVERFLOW STATUS_INTEGER_OVERFLOW +#define EXCEPTION_PRIV_INSTRUCTION STATUS_PRIVILEGED_INSTRUCTION +#define EXCEPTION_IN_PAGE_ERROR STATUS_IN_PAGE_ERROR +#define EXCEPTION_ILLEGAL_INSTRUCTION STATUS_ILLEGAL_INSTRUCTION +#define EXCEPTION_NONCONTINUABLE_EXCEPTION STATUS_NONCONTINUABLE_EXCEPTION +#define EXCEPTION_STACK_OVERFLOW STATUS_STACK_OVERFLOW +#define EXCEPTION_INVALID_DISPOSITION STATUS_INVALID_DISPOSITION +#define EXCEPTION_GUARD_PAGE STATUS_GUARD_PAGE_VIOLATION +#define EXCEPTION_INVALID_HANDLE STATUS_INVALID_HANDLE +#define EXCEPTION_POSSIBLE_DEADLOCK STATUS_POSSIBLE_DEADLOCK +#define CONTROL_C_EXIT STATUS_CONTROL_C_EXIT +#define MoveMemory RtlMoveMemory +#define CopyMemory RtlCopyMemory +#define FillMemory RtlFillMemory +#define ZeroMemory RtlZeroMemory +#define SecureZeroMemory RtlSecureZeroMemory + +#define FILE_FLAG_WRITE_THROUGH 0x80000000 +#define FILE_FLAG_OVERLAPPED 0x40000000 +#define FILE_FLAG_NO_BUFFERING 0x20000000 +#define FILE_FLAG_RANDOM_ACCESS 0x10000000 +#define FILE_FLAG_SEQUENTIAL_SCAN 0x8000000 +#define FILE_FLAG_DELETE_ON_CLOSE 0x4000000 +#define FILE_FLAG_BACKUP_SEMANTICS 0x2000000 +#define FILE_FLAG_POSIX_SEMANTICS 0x1000000 +#define FILE_FLAG_OPEN_REPARSE_POINT 0x200000 +#define FILE_FLAG_OPEN_NO_RECALL 0x100000 +#define FILE_FLAG_FIRST_PIPE_INSTANCE 0x80000 + +#define CREATE_NEW 1 +#define CREATE_ALWAYS 2 +#define OPEN_EXISTING 3 +#define OPEN_ALWAYS 4 +#define TRUNCATE_EXISTING 5 + +#define PROGRESS_CONTINUE 0 +#define PROGRESS_CANCEL 1 +#define PROGRESS_STOP 2 +#define PROGRESS_QUIET 3 + +#define CALLBACK_CHUNK_FINISHED 0x0 +#define CALLBACK_STREAM_SWITCH 0x1 + +#define COPY_FILE_FAIL_IF_EXISTS 0x1 +#define COPY_FILE_RESTARTABLE 0x2 +#define COPY_FILE_OPEN_SOURCE_FOR_WRITE 0x4 +#define COPY_FILE_ALLOW_DECRYPTED_DESTINATION 0x8 + +#define REPLACEFILE_WRITE_THROUGH 0x1 +#define REPLACEFILE_IGNORE_MERGE_ERRORS 0x2 + +#define PIPE_ACCESS_INBOUND 0x1 +#define PIPE_ACCESS_OUTBOUND 0x2 +#define PIPE_ACCESS_DUPLEX 0x3 + +#define PIPE_CLIENT_END 0x0 +#define PIPE_SERVER_END 0x1 + +#define PIPE_WAIT 0x0 +#define PIPE_NOWAIT 0x1 +#define PIPE_READMODE_BYTE 0x0 +#define PIPE_READMODE_MESSAGE 0x2 +#define PIPE_TYPE_BYTE 0x0 +#define PIPE_TYPE_MESSAGE 0x4 + +#define PIPE_UNLIMITED_INSTANCES 255 + +#define SECURITY_ANONYMOUS (SecurityAnonymous << 16) +#define SECURITY_IDENTIFICATION (SecurityIdentification << 16) +#define SECURITY_IMPERSONATION (SecurityImpersonation << 16) +#define SECURITY_DELEGATION (SecurityDelegation << 16) + +#define SECURITY_CONTEXT_TRACKING 0x40000 +#define SECURITY_EFFECTIVE_ONLY 0x80000 + +#define SECURITY_SQOS_PRESENT 0x100000 +#define SECURITY_VALID_SQOS_FLAGS 0x1f0000 + + typedef struct _OVERLAPPED { + ULONG_PTR Internal; + ULONG_PTR InternalHigh; + union { + struct { + DWORD Offset; + DWORD OffsetHigh; + }; + PVOID Pointer; + }; + HANDLE hEvent; + } OVERLAPPED,*LPOVERLAPPED; + + typedef struct _SECURITY_ATTRIBUTES { + DWORD nLength; + LPVOID lpSecurityDescriptor; + WINBOOL bInheritHandle; + } SECURITY_ATTRIBUTES,*PSECURITY_ATTRIBUTES,*LPSECURITY_ATTRIBUTES; + + typedef struct _PROCESS_INFORMATION { + HANDLE hProcess; + HANDLE hThread; + DWORD dwProcessId; + DWORD dwThreadId; + } PROCESS_INFORMATION,*PPROCESS_INFORMATION,*LPPROCESS_INFORMATION; + +#ifndef _FILETIME_ +#define _FILETIME_ + typedef struct _FILETIME { + DWORD dwLowDateTime; + DWORD dwHighDateTime; + } FILETIME,*PFILETIME,*LPFILETIME; +#endif + + typedef struct _SYSTEMTIME { + WORD wYear; + WORD wMonth; + WORD wDayOfWeek; + WORD wDay; + WORD wHour; + WORD wMinute; + WORD wSecond; + WORD wMilliseconds; + } SYSTEMTIME,*PSYSTEMTIME,*LPSYSTEMTIME; + + typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter); + typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE; + typedef VOID (WINAPI *PFIBER_START_ROUTINE)(LPVOID lpFiberParameter); + typedef PFIBER_START_ROUTINE LPFIBER_START_ROUTINE; + + typedef RTL_CRITICAL_SECTION CRITICAL_SECTION; + typedef PRTL_CRITICAL_SECTION PCRITICAL_SECTION; + typedef PRTL_CRITICAL_SECTION LPCRITICAL_SECTION; + typedef RTL_CRITICAL_SECTION_DEBUG CRITICAL_SECTION_DEBUG; + typedef PRTL_CRITICAL_SECTION_DEBUG PCRITICAL_SECTION_DEBUG; + typedef PRTL_CRITICAL_SECTION_DEBUG LPCRITICAL_SECTION_DEBUG; + + WINBASEAPI PVOID WINAPI EncodePointer(PVOID Ptr); + WINBASEAPI PVOID WINAPI DecodePointer(PVOID Ptr); + WINBASEAPI PVOID WINAPI EncodeSystemPointer(PVOID Ptr); + WINBASEAPI PVOID WINAPI DecodeSystemPointer(PVOID Ptr); + +#ifdef I_X86_ + typedef PLDT_ENTRY LPLDT_ENTRY; +#else + typedef LPVOID LPLDT_ENTRY; +#endif + +#define MUTEX_MODIFY_STATE MUTANT_QUERY_STATE +#define MUTEX_ALL_ACCESS MUTANT_ALL_ACCESS + +#define SP_SERIALCOMM ((DWORD)0x1) + +#define PST_UNSPECIFIED ((DWORD)0x0) +#define PST_RS232 ((DWORD)0x1) +#define PST_PARALLELPORT ((DWORD)0x2) +#define PST_RS422 ((DWORD)0x3) +#define PST_RS423 ((DWORD)0x4) +#define PST_RS449 ((DWORD)0x5) +#define PST_MODEM ((DWORD)0x6) +#define PST_FAX ((DWORD)0x21) +#define PST_SCANNER ((DWORD)0x22) +#define PST_NETWORK_BRIDGE ((DWORD)0x100) +#define PST_LAT ((DWORD)0x101) +#define PST_TCPIP_TELNET ((DWORD)0x102) +#define PST_X25 ((DWORD)0x103) + +#define PCF_DTRDSR ((DWORD)0x1) +#define PCF_RTSCTS ((DWORD)0x2) +#define PCF_RLSD ((DWORD)0x4) +#define PCF_PARITY_CHECK ((DWORD)0x8) +#define PCF_XONXOFF ((DWORD)0x10) +#define PCF_SETXCHAR ((DWORD)0x20) +#define PCF_TOTALTIMEOUTS ((DWORD)0x40) +#define PCF_INTTIMEOUTS ((DWORD)0x80) +#define PCF_SPECIALCHARS ((DWORD)0x100) +#define PCF_16BITMODE ((DWORD)0x200) + +#define SP_PARITY ((DWORD)0x1) +#define SP_BAUD ((DWORD)0x2) +#define SP_DATABITS ((DWORD)0x4) +#define SP_STOPBITS ((DWORD)0x8) +#define SP_HANDSHAKING ((DWORD)0x10) +#define SP_PARITY_CHECK ((DWORD)0x20) +#define SP_RLSD ((DWORD)0x40) + +#define BAUD_075 ((DWORD)0x1) +#define BAUD_110 ((DWORD)0x2) +#define BAUD_134_5 ((DWORD)0x4) +#define BAUD_150 ((DWORD)0x8) +#define BAUD_300 ((DWORD)0x10) +#define BAUD_600 ((DWORD)0x20) +#define BAUD_1200 ((DWORD)0x40) +#define BAUD_1800 ((DWORD)0x80) +#define BAUD_2400 ((DWORD)0x100) +#define BAUD_4800 ((DWORD)0x200) +#define BAUD_7200 ((DWORD)0x400) +#define BAUD_9600 ((DWORD)0x800) +#define BAUD_14400 ((DWORD)0x1000) +#define BAUD_19200 ((DWORD)0x2000) +#define BAUD_38400 ((DWORD)0x4000) +#define BAUD_56K ((DWORD)0x8000) +#define BAUD_128K ((DWORD)0x10000) +#define BAUD_115200 ((DWORD)0x20000) +#define BAUD_57600 ((DWORD)0x40000) +#define BAUD_USER ((DWORD)0x10000000) + +#define DATABITS_5 ((WORD)0x1) +#define DATABITS_6 ((WORD)0x2) +#define DATABITS_7 ((WORD)0x4) +#define DATABITS_8 ((WORD)0x8) +#define DATABITS_16 ((WORD)0x10) +#define DATABITS_16X ((WORD)0x20) + +#define STOPBITS_10 ((WORD)0x1) +#define STOPBITS_15 ((WORD)0x2) +#define STOPBITS_20 ((WORD)0x4) +#define PARITY_NONE ((WORD)0x100) +#define PARITY_ODD ((WORD)0x200) +#define PARITY_EVEN ((WORD)0x400) +#define PARITY_MARK ((WORD)0x800) +#define PARITY_SPACE ((WORD)0x1000) + + typedef struct _COMMPROP { + WORD wPacketLength; + WORD wPacketVersion; + DWORD dwServiceMask; + DWORD dwReserved1; + DWORD dwMaxTxQueue; + DWORD dwMaxRxQueue; + DWORD dwMaxBaud; + DWORD dwProvSubType; + DWORD dwProvCapabilities; + DWORD dwSettableParams; + DWORD dwSettableBaud; + WORD wSettableData; + WORD wSettableStopParity; + DWORD dwCurrentTxQueue; + DWORD dwCurrentRxQueue; + DWORD dwProvSpec1; + DWORD dwProvSpec2; + WCHAR wcProvChar[1]; + } COMMPROP,*LPCOMMPROP; + +#define COMMPROP_INITIALIZED ((DWORD)0xE73CF52E) + + typedef struct _COMSTAT { + DWORD fCtsHold : 1; + DWORD fDsrHold : 1; + DWORD fRlsdHold : 1; + DWORD fXoffHold : 1; + DWORD fXoffSent : 1; + DWORD fEof : 1; + DWORD fTxim : 1; + DWORD fReserved : 25; + DWORD cbInQue; + DWORD cbOutQue; + } COMSTAT,*LPCOMSTAT; + +#define DTR_CONTROL_DISABLE 0x0 +#define DTR_CONTROL_ENABLE 0x1 +#define DTR_CONTROL_HANDSHAKE 0x2 + +#define RTS_CONTROL_DISABLE 0x0 +#define RTS_CONTROL_ENABLE 0x1 +#define RTS_CONTROL_HANDSHAKE 0x2 +#define RTS_CONTROL_TOGGLE 0x3 + + typedef struct _DCB { + DWORD DCBlength; + DWORD BaudRate; + DWORD fBinary: 1; + DWORD fParity: 1; + DWORD fOutxCtsFlow:1; + DWORD fOutxDsrFlow:1; + DWORD fDtrControl:2; + DWORD fDsrSensitivity:1; + DWORD fTXContinueOnXoff: 1; + DWORD fOutX: 1; + DWORD fInX: 1; + DWORD fErrorChar: 1; + DWORD fNull: 1; + DWORD fRtsControl:2; + DWORD fAbortOnError:1; + DWORD fDummy2:17; + WORD wReserved; + WORD XonLim; + WORD XoffLim; + BYTE ByteSize; + BYTE Parity; + BYTE StopBits; + char XonChar; + char XoffChar; + char ErrorChar; + char EofChar; + char EvtChar; + WORD wReserved1; + } DCB,*LPDCB; + + typedef struct _COMMTIMEOUTS { + DWORD ReadIntervalTimeout; + DWORD ReadTotalTimeoutMultiplier; + DWORD ReadTotalTimeoutConstant; + DWORD WriteTotalTimeoutMultiplier; + DWORD WriteTotalTimeoutConstant; + } COMMTIMEOUTS,*LPCOMMTIMEOUTS; + + typedef struct _COMMCONFIG { + DWORD dwSize; + WORD wVersion; + WORD wReserved; + DCB dcb; + DWORD dwProviderSubType; + DWORD dwProviderOffset; + DWORD dwProviderSize; + WCHAR wcProviderData[1]; + } COMMCONFIG,*LPCOMMCONFIG; + + typedef struct _SYSTEM_INFO { + union { + DWORD dwOemId; + struct { + WORD wProcessorArchitecture; + WORD wReserved; + }; + }; + DWORD dwPageSize; + LPVOID lpMinimumApplicationAddress; + LPVOID lpMaximumApplicationAddress; + DWORD_PTR dwActiveProcessorMask; + DWORD dwNumberOfProcessors; + DWORD dwProcessorType; + DWORD dwAllocationGranularity; + WORD wProcessorLevel; + WORD wProcessorRevision; + } SYSTEM_INFO,*LPSYSTEM_INFO; + +#define FreeModule(hLibModule) FreeLibrary((hLibModule)) +#define MakeProcInstance(lpProc,hInstance) (lpProc) +#define FreeProcInstance(lpProc) (lpProc) + +#define GMEM_FIXED 0x0 +#define GMEM_MOVEABLE 0x2 +#define GMEM_NOCOMPACT 0x10 +#define GMEM_NODISCARD 0x20 +#define GMEM_ZEROINIT 0x40 +#define GMEM_MODIFY 0x80 +#define GMEM_DISCARDABLE 0x100 +#define GMEM_NOT_BANKED 0x1000 +#define GMEM_SHARE 0x2000 +#define GMEM_DDESHARE 0x2000 +#define GMEM_NOTIFY 0x4000 +#define GMEM_LOWER GMEM_NOT_BANKED +#define GMEM_VALID_FLAGS 0x7F72 +#define GMEM_INVALID_HANDLE 0x8000 + +#define GHND (GMEM_MOVEABLE | GMEM_ZEROINIT) +#define GPTR (GMEM_FIXED | GMEM_ZEROINIT) + +#define GlobalLRUNewest(h) ((HANDLE)(h)) +#define GlobalLRUOldest(h) ((HANDLE)(h)) +#define GlobalDiscard(h) GlobalReAlloc((h),0,GMEM_MOVEABLE) + +#define GMEM_DISCARDED 0x4000 +#define GMEM_LOCKCOUNT 0xff + + typedef struct _MEMORYSTATUS { + DWORD dwLength; + DWORD dwMemoryLoad; + SIZE_T dwTotalPhys; + SIZE_T dwAvailPhys; + SIZE_T dwTotalPageFile; + SIZE_T dwAvailPageFile; + SIZE_T dwTotalVirtual; + SIZE_T dwAvailVirtual; + } MEMORYSTATUS,*LPMEMORYSTATUS; + +#define LMEM_FIXED 0x0 +#define LMEM_MOVEABLE 0x2 +#define LMEM_NOCOMPACT 0x10 +#define LMEM_NODISCARD 0x20 +#define LMEM_ZEROINIT 0x40 +#define LMEM_MODIFY 0x80 +#define LMEM_DISCARDABLE 0xf00 +#define LMEM_VALID_FLAGS 0xf72 +#define LMEM_INVALID_HANDLE 0x8000 + +#define LHND (LMEM_MOVEABLE | LMEM_ZEROINIT) +#define LPTR (LMEM_FIXED | LMEM_ZEROINIT) + +#define NONZEROLHND (LMEM_MOVEABLE) +#define NONZEROLPTR (LMEM_FIXED) + +#define LocalDiscard(h) LocalReAlloc((h),0,LMEM_MOVEABLE) + +#define LMEM_DISCARDED 0x4000 +#define LMEM_LOCKCOUNT 0xff + +#define DEBUG_PROCESS 0x1 +#define DEBUG_ONLY_THIS_PROCESS 0x2 +#define CREATE_SUSPENDED 0x4 +#define DETACHED_PROCESS 0x8 +#define CREATE_NEW_CONSOLE 0x10 +#define NORMAL_PRIORITY_CLASS 0x20 +#define IDLE_PRIORITY_CLASS 0x40 +#define HIGH_PRIORITY_CLASS 0x80 +#define REALTIME_PRIORITY_CLASS 0x100 +#define CREATE_NEW_PROCESS_GROUP 0x200 +#define CREATE_UNICODE_ENVIRONMENT 0x400 +#define CREATE_SEPARATE_WOW_VDM 0x800 +#define CREATE_SHARED_WOW_VDM 0x1000 +#define CREATE_FORCEDOS 0x2000 +#define BELOW_NORMAL_PRIORITY_CLASS 0x4000 +#define ABOVE_NORMAL_PRIORITY_CLASS 0x8000 +#define STACK_SIZE_PARAM_IS_A_RESERVATION 0x10000 + +#define CREATE_BREAKAWAY_FROM_JOB 0x1000000 +#define CREATE_PRESERVE_CODE_AUTHZ_LEVEL 0x2000000 + +#define CREATE_DEFAULT_ERROR_MODE 0x4000000 +#define CREATE_NO_WINDOW 0x8000000 + +#define PROFILE_USER 0x10000000 +#define PROFILE_KERNEL 0x20000000 +#define PROFILE_SERVER 0x40000000 + +#define CREATE_IGNORE_SYSTEM_DEFAULT 0x80000000 + +#define THREAD_PRIORITY_LOWEST THREAD_BASE_PRIORITY_MIN +#define THREAD_PRIORITY_BELOW_NORMAL (THREAD_PRIORITY_LOWEST+1) +#define THREAD_PRIORITY_NORMAL 0 +#define THREAD_PRIORITY_HIGHEST THREAD_BASE_PRIORITY_MAX +#define THREAD_PRIORITY_ABOVE_NORMAL (THREAD_PRIORITY_HIGHEST-1) +#define THREAD_PRIORITY_ERROR_RETURN (MAXLONG) + +#define THREAD_PRIORITY_TIME_CRITICAL THREAD_BASE_PRIORITY_LOWRT +#define THREAD_PRIORITY_IDLE THREAD_BASE_PRIORITY_IDLE + +#define EXCEPTION_DEBUG_EVENT 1 +#define CREATE_THREAD_DEBUG_EVENT 2 +#define CREATE_PROCESS_DEBUG_EVENT 3 +#define EXIT_THREAD_DEBUG_EVENT 4 +#define EXIT_PROCESS_DEBUG_EVENT 5 +#define LOAD_DLL_DEBUG_EVENT 6 +#define UNLOAD_DLL_DEBUG_EVENT 7 +#define OUTPUT_DEBUG_STRING_EVENT 8 +#define RIP_EVENT 9 + + typedef struct _EXCEPTION_DEBUG_INFO { + EXCEPTION_RECORD ExceptionRecord; + DWORD dwFirstChance; + } EXCEPTION_DEBUG_INFO,*LPEXCEPTION_DEBUG_INFO; + + typedef struct _CREATE_THREAD_DEBUG_INFO { + HANDLE hThread; + LPVOID lpThreadLocalBase; + LPTHREAD_START_ROUTINE lpStartAddress; + } CREATE_THREAD_DEBUG_INFO,*LPCREATE_THREAD_DEBUG_INFO; + + typedef struct _CREATE_PROCESS_DEBUG_INFO { + HANDLE hFile; + HANDLE hProcess; + HANDLE hThread; + LPVOID lpBaseOfImage; + DWORD dwDebugInfoFileOffset; + DWORD nDebugInfoSize; + LPVOID lpThreadLocalBase; + LPTHREAD_START_ROUTINE lpStartAddress; + LPVOID lpImageName; + WORD fUnicode; + } CREATE_PROCESS_DEBUG_INFO,*LPCREATE_PROCESS_DEBUG_INFO; + + typedef struct _EXIT_THREAD_DEBUG_INFO { + DWORD dwExitCode; + } EXIT_THREAD_DEBUG_INFO,*LPEXIT_THREAD_DEBUG_INFO; + + typedef struct _EXIT_PROCESS_DEBUG_INFO { + DWORD dwExitCode; + } EXIT_PROCESS_DEBUG_INFO,*LPEXIT_PROCESS_DEBUG_INFO; + + typedef struct _LOAD_DLL_DEBUG_INFO { + HANDLE hFile; + LPVOID lpBaseOfDll; + DWORD dwDebugInfoFileOffset; + DWORD nDebugInfoSize; + LPVOID lpImageName; + WORD fUnicode; + } LOAD_DLL_DEBUG_INFO,*LPLOAD_DLL_DEBUG_INFO; + + typedef struct _UNLOAD_DLL_DEBUG_INFO { + LPVOID lpBaseOfDll; + } UNLOAD_DLL_DEBUG_INFO,*LPUNLOAD_DLL_DEBUG_INFO; + + typedef struct _OUTPUT_DEBUG_STRING_INFO { + LPSTR lpDebugStringData; + WORD fUnicode; + WORD nDebugStringLength; + } OUTPUT_DEBUG_STRING_INFO,*LPOUTPUT_DEBUG_STRING_INFO; + + typedef struct _RIP_INFO { + DWORD dwError; + DWORD dwType; + } RIP_INFO,*LPRIP_INFO; + + typedef struct _DEBUG_EVENT { + DWORD dwDebugEventCode; + DWORD dwProcessId; + DWORD dwThreadId; + union { + EXCEPTION_DEBUG_INFO Exception; + CREATE_THREAD_DEBUG_INFO CreateThread; + CREATE_PROCESS_DEBUG_INFO CreateProcessInfo; + EXIT_THREAD_DEBUG_INFO ExitThread; + EXIT_PROCESS_DEBUG_INFO ExitProcess; + LOAD_DLL_DEBUG_INFO LoadDll; + UNLOAD_DLL_DEBUG_INFO UnloadDll; + OUTPUT_DEBUG_STRING_INFO DebugString; + RIP_INFO RipInfo; + } u; + } DEBUG_EVENT,*LPDEBUG_EVENT; + + typedef PCONTEXT LPCONTEXT; + typedef PEXCEPTION_RECORD LPEXCEPTION_RECORD; + typedef PEXCEPTION_POINTERS LPEXCEPTION_POINTERS; + +#define DRIVE_UNKNOWN 0 +#define DRIVE_NO_ROOT_DIR 1 +#define DRIVE_REMOVABLE 2 +#define DRIVE_FIXED 3 +#define DRIVE_REMOTE 4 +#define DRIVE_CDROM 5 +#define DRIVE_RAMDISK 6 + +#define GetFreeSpace(w) (0x100000L) +#define FILE_TYPE_UNKNOWN 0x0 +#define FILE_TYPE_DISK 0x1 +#define FILE_TYPE_CHAR 0x2 +#define FILE_TYPE_PIPE 0x3 +#define FILE_TYPE_REMOTE 0x8000 + +#define STD_INPUT_HANDLE ((DWORD)-10) +#define STD_OUTPUT_HANDLE ((DWORD)-11) +#define STD_ERROR_HANDLE ((DWORD)-12) + +#define NOPARITY 0 +#define ODDPARITY 1 +#define EVENPARITY 2 +#define MARKPARITY 3 +#define SPACEPARITY 4 + +#define ONESTOPBIT 0 +#define ONE5STOPBITS 1 +#define TWOSTOPBITS 2 + +#define IGNORE 0 +#define INFINITE 0xffffffff + +#define CBR_110 110 +#define CBR_300 300 +#define CBR_600 600 +#define CBR_1200 1200 +#define CBR_2400 2400 +#define CBR_4800 4800 +#define CBR_9600 9600 +#define CBR_14400 14400 +#define CBR_19200 19200 +#define CBR_38400 38400 +#define CBR_56000 56000 +#define CBR_57600 57600 +#define CBR_115200 115200 +#define CBR_128000 128000 +#define CBR_256000 256000 + +#define CE_RXOVER 0x1 +#define CE_OVERRUN 0x2 +#define CE_RXPARITY 0x4 +#define CE_FRAME 0x8 +#define CE_BREAK 0x10 +#define CE_TXFULL 0x100 +#define CE_PTO 0x200 +#define CE_IOE 0x400 +#define CE_DNS 0x800 +#define CE_OOP 0x1000 +#define CE_MODE 0x8000 + +#define IE_BADID (-1) +#define IE_OPEN (-2) +#define IE_NOPEN (-3) +#define IE_MEMORY (-4) +#define IE_DEFAULT (-5) +#define IE_HARDWARE (-10) +#define IE_BYTESIZE (-11) +#define IE_BAUDRATE (-12) + +#define EV_RXCHAR 0x1 +#define EV_RXFLAG 0x2 +#define EV_TXEMPTY 0x4 +#define EV_CTS 0x8 +#define EV_DSR 0x10 +#define EV_RLSD 0x20 +#define EV_BREAK 0x40 +#define EV_ERR 0x80 +#define EV_RING 0x100 +#define EV_PERR 0x200 +#define EV_RX80FULL 0x400 +#define EV_EVENT1 0x800 +#define EV_EVENT2 0x1000 + +#define SETXOFF 1 +#define SETXON 2 +#define SETRTS 3 +#define CLRRTS 4 +#define SETDTR 5 +#define CLRDTR 6 +#define RESETDEV 7 +#define SETBREAK 8 +#define CLRBREAK 9 + +#define PURGE_TXABORT 0x1 +#define PURGE_RXABORT 0x2 +#define PURGE_TXCLEAR 0x4 +#define PURGE_RXCLEAR 0x8 + +#define LPTx 0x80 + +#define MS_CTS_ON ((DWORD)0x10) +#define MS_DSR_ON ((DWORD)0x20) +#define MS_RING_ON ((DWORD)0x40) +#define MS_RLSD_ON ((DWORD)0x80) + +#define S_QUEUEEMPTY 0 +#define S_THRESHOLD 1 +#define S_ALLTHRESHOLD 2 + +#define S_NORMAL 0 +#define S_LEGATO 1 +#define S_STACCATO 2 + +#define S_PERIOD512 0 +#define S_PERIOD1024 1 +#define S_PERIOD2048 2 +#define S_PERIODVOICE 3 +#define S_WHITE512 4 +#define S_WHITE1024 5 +#define S_WHITE2048 6 +#define S_WHITEVOICE 7 + +#define S_SERDVNA (-1) +#define S_SEROFM (-2) +#define S_SERMACT (-3) +#define S_SERQFUL (-4) +#define S_SERBDNT (-5) +#define S_SERDLN (-6) +#define S_SERDCC (-7) +#define S_SERDTP (-8) +#define S_SERDVL (-9) +#define S_SERDMD (-10) +#define S_SERDSH (-11) +#define S_SERDPT (-12) +#define S_SERDFQ (-13) +#define S_SERDDR (-14) +#define S_SERDSR (-15) +#define S_SERDST (-16) + +#define NMPWAIT_WAIT_FOREVER 0xffffffff +#define NMPWAIT_NOWAIT 0x1 +#define NMPWAIT_USE_DEFAULT_WAIT 0x0 + +#define FS_CASE_IS_PRESERVED FILE_CASE_PRESERVED_NAMES +#define FS_CASE_SENSITIVE FILE_CASE_SENSITIVE_SEARCH +#define FS_UNICODE_STORED_ON_DISK FILE_UNICODE_ON_DISK +#define FS_PERSISTENT_ACLS FILE_PERSISTENT_ACLS +#define FS_VOL_IS_COMPRESSED FILE_VOLUME_IS_COMPRESSED +#define FS_FILE_COMPRESSION FILE_FILE_COMPRESSION +#define FS_FILE_ENCRYPTION FILE_SUPPORTS_ENCRYPTION + +#define FILE_MAP_COPY SECTION_QUERY +#define FILE_MAP_WRITE SECTION_MAP_WRITE +#define FILE_MAP_READ SECTION_MAP_READ +#define FILE_MAP_ALL_ACCESS SECTION_ALL_ACCESS +#define FILE_MAP_EXECUTE SECTION_MAP_EXECUTE_EXPLICIT + +#define OF_READ 0x0 +#define OF_WRITE 0x1 +#define OF_READWRITE 0x2 +#define OF_SHARE_COMPAT 0x0 +#define OF_SHARE_EXCLUSIVE 0x10 +#define OF_SHARE_DENY_WRITE 0x20 +#define OF_SHARE_DENY_READ 0x30 +#define OF_SHARE_DENY_NONE 0x40 +#define OF_PARSE 0x100 +#define OF_DELETE 0x200 +#define OF_VERIFY 0x400 +#define OF_CANCEL 0x800 +#define OF_CREATE 0x1000 +#define OF_PROMPT 0x2000 +#define OF_EXIST 0x4000 +#define OF_REOPEN 0x8000 + +#define OFS_MAXPATHNAME 128 + typedef struct _OFSTRUCT { + BYTE cBytes; + BYTE fFixedDisk; + WORD nErrCode; + WORD Reserved1; + WORD Reserved2; + CHAR szPathName[OFS_MAXPATHNAME]; + } OFSTRUCT,*LPOFSTRUCT,*POFSTRUCT; + +#ifndef NOWINBASEINTERLOCK + +#ifndef _NTOS_ + +#if defined(__ia64__) && !defined(RC_INVOKED) + +#define InterlockedIncrement _InterlockedIncrement +#define InterlockedIncrementAcquire _InterlockedIncrement_acq +#define InterlockedIncrementRelease _InterlockedIncrement_rel +#define InterlockedDecrement _InterlockedDecrement +#define InterlockedDecrementAcquire _InterlockedDecrement_acq +#define InterlockedDecrementRelease _InterlockedDecrement_rel +#define InterlockedExchange _InterlockedExchange +#define InterlockedExchangeAdd _InterlockedExchangeAdd +#define InterlockedCompareExchange _InterlockedCompareExchange +#define InterlockedCompareExchangeAcquire _InterlockedCompareExchange_acq +#define InterlockedCompareExchangeRelease _InterlockedCompareExchange_rel +#define InterlockedExchangePointer _InterlockedExchangePointer +#define InterlockedCompareExchangePointer _InterlockedCompareExchangePointer +#define InterlockedCompareExchangePointerRelease _InterlockedCompareExchangePointer_rel +#define InterlockedCompareExchangePointerAcquire _InterlockedCompareExchangePointer_acq + +#define InterlockedIncrement64 _InterlockedIncrement64 +#define InterlockedDecrement64 _InterlockedDecrement64 +#define InterlockedExchange64 _InterlockedExchange64 +#define InterlockedExchangeAcquire64 _InterlockedExchange64_acq +#define InterlockedExchangeAdd64 _InterlockedExchangeAdd64 +#define InterlockedCompareExchange64 _InterlockedCompareExchange64 +#define InterlockedCompareExchangeAcquire64 _InterlockedCompareExchange64_acq +#define InterlockedCompareExchangeRelease64 _InterlockedCompareExchange64_rel + + LONGLONG __cdecl InterlockedIncrement64(LONGLONG volatile *Addend); + LONGLONG __cdecl InterlockedDecrement64(LONGLONG volatile *Addend); + LONG __cdecl InterlockedIncrementAcquire(LONG volatile *Addend); + LONG __cdecl InterlockedDecrementAcquire(LONG volatile *Addend); + LONG __cdecl InterlockedIncrementRelease(LONG volatile *Addend); + LONG __cdecl InterlockedDecrementRelease(LONG volatile *Addend); + LONGLONG __cdecl InterlockedExchange64 (LONGLONG volatile *Target,LONGLONG Value); + LONGLONG __cdecl InterlockedExchangeAcquire64 (LONGLONG volatile *Target,LONGLONG Value); + LONGLONG __cdecl InterlockedExchangeAdd64 (LONGLONG volatile *Addend,LONGLONG Value); + LONGLONG __cdecl InterlockedCompareExchange64 (LONGLONG volatile *Destination,LONGLONG ExChange,LONGLONG Comperand); + LONGLONG __cdecl InterlockedCompareExchangeAcquire64 (LONGLONG volatile *Destination,LONGLONG ExChange,LONGLONG Comperand); + LONGLONG __cdecl InterlockedCompareExchangeRelease64 (LONGLONG volatile *Destination,LONGLONG ExChange,LONGLONG Comperand); + LONG __cdecl InterlockedIncrement(LONG volatile *lpAddend); + LONG __cdecl InterlockedDecrement(LONG volatile *lpAddend); + LONG __cdecl InterlockedExchange(LONG volatile *Target,LONG Value); + LONG __cdecl InterlockedExchangeAdd(LONG volatile *Addend,LONG Value); + LONG __cdecl InterlockedCompareExchange(LONG volatile *Destination,LONG ExChange,LONG Comperand); + LONG __cdecl InterlockedCompareExchangeRelease(LONG volatile *Destination,LONG ExChange,LONG Comperand); + LONG __cdecl InterlockedCompareExchangeAcquire(LONG volatile *Destination,LONG ExChange,LONG Comperand); + PVOID __cdecl InterlockedExchangePointer(PVOID volatile *Target,PVOID Value); + PVOID __cdecl InterlockedCompareExchangePointer(PVOID volatile *Destination,PVOID ExChange,PVOID Comperand); + PVOID __cdecl InterlockedCompareExchangePointerAcquire(PVOID volatile *Destination,PVOID Exchange,PVOID Comperand); + PVOID __cdecl InterlockedCompareExchangePointerRelease(PVOID volatile *Destination,PVOID Exchange,PVOID Comperand); + +#ifndef InterlockedAnd +#define InterlockedAnd InterlockedAnd_Inline + __CRT_INLINE LONG InterlockedAnd_Inline(LONG volatile *Target,LONG Set) { + LONG i; + LONG j; + j = *Target; + do { + i = j; + j = InterlockedCompareExchange(Target,i & Set,i); + } while(i!=j); + return j; + } +#endif + +#ifndef InterlockedOr +#define InterlockedOr InterlockedOr_Inline + + __CRT_INLINE LONG InterlockedOr_Inline(LONG volatile *Target,LONG Set) { + LONG i; + LONG j; + j = *Target; + do { + i = j; + j = InterlockedCompareExchange(Target,i | Set,i); + } while(i!=j); + return j; + } +#endif + +#ifndef InterlockedXor +#define InterlockedXor InterlockedXor_Inline + + __CRT_INLINE LONG InterlockedXor_Inline(LONG volatile *Target,LONG Set) { + LONG i; + LONG j; + j = *Target; + do { + i = j; + j = InterlockedCompareExchange(Target,i ^ Set,i); + } while(i!=j); + return j; + } +#endif + +#ifndef !defined (InterlockedAnd64) +#define InterlockedAnd64 InterlockedAnd64_Inline + + __CRT_INLINE LONGLONG InterlockedAnd64_Inline (LONGLONG volatile *Destination,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Destination; + } while(InterlockedCompareExchange64(Destination,Old & Value,Old)!=Old); + return Old; + } +#endif + +#ifndef InterlockedOr64 +#define InterlockedOr64 InterlockedOr64_Inline + + __CRT_INLINE LONGLONG InterlockedOr64_Inline (LONGLONG volatile *Destination,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Destination; + } while(InterlockedCompareExchange64(Destination,Old | Value,Old)!=Old); + return Old; + } +#endif + +#ifndef InterlockedXor64 +#define InterlockedXor64 InterlockedXor64_Inline + + __CRT_INLINE LONGLONG InterlockedXor64_Inline (LONGLONG volatile *Destination,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Destination; + } while(InterlockedCompareExchange64(Destination,Old ^ Value,Old)!=Old); + return Old; + } +#endif + +#ifndef InterlockedBitTestAndSet +#define InterlockedBitTestAndSet InterlockedBitTestAndSet_Inline + + __CRT_INLINE BOOLEAN InterlockedBitTestAndSet_Inline(LONG *Base,LONG Bit) { + LONG tBit; + tBit = 1<<(Bit & (sizeof (*Base)*8-1)); + return (BOOLEAN)((InterlockedOr(&Base[Bit/(sizeof(*Base)*8)],tBit)&tBit)!=0); + } +#endif + +#ifndef InterlockedBitTestAndReset +#define InterlockedBitTestAndReset InterlockedBitTestAndReset_Inline + + __CRT_INLINE BOOLEAN InterlockedBitTestAndReset_Inline(LONG *Base,LONG Bit) { + LONG tBit; + tBit = 1<<(Bit & (sizeof (*Base)*8-1)); + return (BOOLEAN)((InterlockedAnd(&Base[Bit/(sizeof(*Base)*8)],~tBit)&tBit)!=0); + } +#endif + +#ifndef InterlockedBitTestAndComplement +#define InterlockedBitTestAndComplement InterlockedBitTestAndComplement_Inline + + __CRT_INLINE BOOLEAN InterlockedBitTestAndComplement_Inline(LONG *Base,LONG Bit) { + LONG tBit; + tBit = 1<<(Bit & (sizeof (*Base)*8-1)); + return (BOOLEAN)((InterlockedXor(&Base[Bit/(sizeof(*Base)*8)],tBit)&tBit)!=0); + } +#endif +#elif defined(__x86_64) && !defined(RC_INVOKED) + +#define InterlockedIncrement _InterlockedIncrement +#define InterlockedIncrementAcquire InterlockedIncrement +#define InterlockedIncrementRelease InterlockedIncrement +#define InterlockedDecrement _InterlockedDecrement +#define InterlockedDecrementAcquire InterlockedDecrement +#define InterlockedDecrementRelease InterlockedDecrement +#define InterlockedExchange _InterlockedExchange +#define InterlockedExchangeAdd _InterlockedExchangeAdd +#define InterlockedCompareExchange _InterlockedCompareExchange +#define InterlockedCompareExchangeAcquire InterlockedCompareExchange +#define InterlockedCompareExchangeRelease InterlockedCompareExchange +#define InterlockedExchangePointer _InterlockedExchangePointer +#define InterlockedCompareExchangePointer _InterlockedCompareExchangePointer +#define InterlockedCompareExchangePointerAcquire _InterlockedCompareExchangePointer +#define InterlockedCompareExchangePointerRelease _InterlockedCompareExchangePointer +#define InterlockedAnd64 _InterlockedAnd64 +#define InterlockedOr64 _InterlockedOr64 +#define InterlockedXor64 _InterlockedXor64 +#define InterlockedIncrement64 _InterlockedIncrement64 +#define InterlockedDecrement64 _InterlockedDecrement64 +#define InterlockedExchange64 _InterlockedExchange64 +#define InterlockedExchangeAdd64 _InterlockedExchangeAdd64 +#define InterlockedCompareExchange64 _InterlockedCompareExchange64 +#define InterlockedCompareExchangeAcquire64 InterlockedCompareExchange64 +#define InterlockedCompareExchangeRelease64 InterlockedCompareExchange64 + + LONG InterlockedIncrement(LONG volatile *Addend); + LONG InterlockedDecrement(LONG volatile *Addend); + LONG InterlockedExchange(LONG volatile *Target,LONG Value); + LONG InterlockedExchangeAdd(LONG volatile *Addend,LONG Value); + LONG InterlockedCompareExchange(LONG volatile *Destination,LONG ExChange,LONG Comperand); + PVOID InterlockedCompareExchangePointer(PVOID volatile *Destination,PVOID Exchange,PVOID Comperand); + PVOID InterlockedExchangePointer(PVOID volatile *Target,PVOID Value); + LONG64 InterlockedAnd64(LONG64 volatile *Destination,LONG64 Value); + LONG64 InterlockedOr64(LONG64 volatile *Destination,LONG64 Value); + LONG64 InterlockedXor64(LONG64 volatile *Destination,LONG64 Value); + LONG64 InterlockedIncrement64(LONG64 volatile *Addend); + LONG64 InterlockedDecrement64(LONG64 volatile *Addend); + LONG64 InterlockedExchange64(LONG64 volatile *Target,LONG64 Value); + LONG64 InterlockedExchangeAdd64(LONG64 volatile *Addend,LONG64 Value); + LONG64 InterlockedCompareExchange64(LONG64 volatile *Destination,LONG64 ExChange,LONG64 Comperand); +#else + LONG WINAPI InterlockedIncrement(LONG volatile *lpAddend); + LONG WINAPI InterlockedDecrement(LONG volatile *lpAddend); + LONG WINAPI InterlockedExchange(LONG volatile *Target,LONG Value); + +#define InterlockedExchangePointer(Target,Value) (PVOID)InterlockedExchange((PLONG)(Target),(LONG)(Value)) + + LONG WINAPI InterlockedExchangeAdd(LONG volatile *Addend,LONG Value); + LONG WINAPI InterlockedCompareExchange(LONG volatile *Destination,LONG Exchange,LONG Comperand); + LONGLONG WINAPI InterlockedCompareExchange64(LONGLONG volatile *Destination,LONGLONG Exchange,LONGLONG Comperand); + + __CRT_INLINE LONGLONG InterlockedAnd64 (LONGLONG volatile *Destination,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Destination; + } while(InterlockedCompareExchange64(Destination,Old & Value,Old)!=Old); + return Old; + } + + __CRT_INLINE LONGLONG InterlockedOr64 (LONGLONG volatile *Destination,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Destination; + } while(InterlockedCompareExchange64(Destination,Old | Value,Old)!=Old); + return Old; + } + + __CRT_INLINE LONGLONG InterlockedXor64 (LONGLONG volatile *Destination,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Destination; + } while(InterlockedCompareExchange64(Destination,Old ^ Value,Old)!=Old); + + return Old; + } + + __CRT_INLINE LONGLONG InterlockedIncrement64(LONGLONG volatile *Addend) { + LONGLONG Old; + do { + Old = *Addend; + } while(InterlockedCompareExchange64(Addend,Old + 1,Old)!=Old); + return Old + 1; + } + + __CRT_INLINE LONGLONG InterlockedDecrement64(LONGLONG volatile *Addend) { + LONGLONG Old; + do { + Old = *Addend; + } while(InterlockedCompareExchange64(Addend,Old - 1,Old)!=Old); + return Old - 1; + } + + __CRT_INLINE LONGLONG InterlockedExchange64(LONGLONG volatile *Target,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Target; + } while(InterlockedCompareExchange64(Target,Value,Old)!=Old); + return Old; + } + + __CRT_INLINE LONGLONG InterlockedExchangeAdd64(LONGLONG volatile *Addend,LONGLONG Value) { + LONGLONG Old; + do { + Old = *Addend; + } while(InterlockedCompareExchange64(Addend,Old + Value,Old)!=Old); + return Old; + } + +#ifdef __cplusplus + __CRT_INLINE PVOID __cdecl __InlineInterlockedCompareExchangePointer(PVOID volatile *Destination,PVOID ExChange,PVOID Comperand) { + return((PVOID)(LONG_PTR)InterlockedCompareExchange((LONG volatile *)Destination,(LONG)(LONG_PTR)ExChange,(LONG)(LONG_PTR)Comperand)); + } +#define InterlockedCompareExchangePointer __InlineInterlockedCompareExchangePointer +#else +#define InterlockedCompareExchangePointer(Destination,ExChange,Comperand)(PVOID)(LONG_PTR)InterlockedCompareExchange((LONG volatile *)(Destination),(LONG)(LONG_PTR)(ExChange),(LONG)(LONG_PTR)(Comperand)) +#endif + +#define InterlockedIncrementAcquire InterlockedIncrement +#define InterlockedIncrementRelease InterlockedIncrement +#define InterlockedDecrementAcquire InterlockedDecrement +#define InterlockedDecrementRelease InterlockedDecrement +#define InterlockedIncrementAcquire InterlockedIncrement +#define InterlockedIncrementRelease InterlockedIncrement +#define InterlockedCompareExchangeAcquire InterlockedCompareExchange +#define InterlockedCompareExchangeRelease InterlockedCompareExchange +#define InterlockedCompareExchangeAcquire64 InterlockedCompareExchange64 +#define InterlockedCompareExchangeRelease64 InterlockedCompareExchange64 +#define InterlockedCompareExchangePointerAcquire InterlockedCompareExchangePointer +#define InterlockedCompareExchangePointerRelease InterlockedCompareExchangePointer +#endif + +#if defined(_SLIST_HEADER_) && !defined(_NTOSP_) + WINBASEAPI VOID WINAPI InitializeSListHead(PSLIST_HEADER ListHead); + WINBASEAPI PSLIST_ENTRY WINAPI InterlockedPopEntrySList(PSLIST_HEADER ListHead); + WINBASEAPI PSLIST_ENTRY WINAPI InterlockedPushEntrySList(PSLIST_HEADER ListHead,PSLIST_ENTRY ListEntry); + WINBASEAPI PSLIST_ENTRY WINAPI InterlockedFlushSList(PSLIST_HEADER ListHead); + WINBASEAPI USHORT WINAPI QueryDepthSList(PSLIST_HEADER ListHead); +#endif +#endif +#endif + + WINBASEAPI WINBOOL WINAPI FreeResource(HGLOBAL hResData); + WINBASEAPI LPVOID WINAPI LockResource(HGLOBAL hResData); + +#define UnlockResource(hResData) ((hResData),0) +#define MAXINTATOM 0xC000 +#define MAKEINTATOM(i) (LPTSTR)((ULONG_PTR)((WORD)(i))) +#define INVALID_ATOM ((ATOM)0) + + int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd); + WINBASEAPI WINBOOL WINAPI FreeLibrary(HMODULE hLibModule); + WINBASEAPI DECLSPEC_NORETURN VOID WINAPI FreeLibraryAndExitThread(HMODULE hLibModule,DWORD dwExitCode); + WINBASEAPI WINBOOL WINAPI DisableThreadLibraryCalls(HMODULE hLibModule); + WINBASEAPI FARPROC WINAPI GetProcAddress(HMODULE hModule,LPCSTR lpProcName); + WINBASEAPI DWORD WINAPI GetVersion(VOID); + WINBASEAPI HGLOBAL WINAPI GlobalAlloc(UINT uFlags,SIZE_T dwBytes); + WINBASEAPI HGLOBAL WINAPI GlobalReAlloc(HGLOBAL hMem,SIZE_T dwBytes,UINT uFlags); + WINBASEAPI SIZE_T WINAPI GlobalSize(HGLOBAL hMem); + WINBASEAPI UINT WINAPI GlobalFlags(HGLOBAL hMem); + WINBASEAPI LPVOID WINAPI GlobalLock(HGLOBAL hMem); + WINBASEAPI HGLOBAL WINAPI GlobalHandle(LPCVOID pMem); + WINBASEAPI WINBOOL WINAPI GlobalUnlock(HGLOBAL hMem); + WINBASEAPI HGLOBAL WINAPI GlobalFree(HGLOBAL hMem); + WINBASEAPI SIZE_T WINAPI GlobalCompact(DWORD dwMinFree); + WINBASEAPI VOID WINAPI GlobalFix(HGLOBAL hMem); + WINBASEAPI VOID WINAPI GlobalUnfix(HGLOBAL hMem); + WINBASEAPI LPVOID WINAPI GlobalWire(HGLOBAL hMem); + WINBASEAPI WINBOOL WINAPI GlobalUnWire(HGLOBAL hMem); + WINBASEAPI VOID WINAPI GlobalMemoryStatus(LPMEMORYSTATUS lpBuffer); + + typedef struct _MEMORYSTATUSEX { + DWORD dwLength; + DWORD dwMemoryLoad; + DWORDLONG ullTotalPhys; + DWORDLONG ullAvailPhys; + DWORDLONG ullTotalPageFile; + DWORDLONG ullAvailPageFile; + DWORDLONG ullTotalVirtual; + DWORDLONG ullAvailVirtual; + DWORDLONG ullAvailExtendedVirtual; + } MEMORYSTATUSEX,*LPMEMORYSTATUSEX; + + WINBASEAPI WINBOOL WINAPI GlobalMemoryStatusEx(LPMEMORYSTATUSEX lpBuffer); + WINBASEAPI HLOCAL WINAPI LocalAlloc(UINT uFlags,SIZE_T uBytes); + WINBASEAPI HLOCAL WINAPI LocalReAlloc(HLOCAL hMem,SIZE_T uBytes,UINT uFlags); + WINBASEAPI LPVOID WINAPI LocalLock(HLOCAL hMem); + WINBASEAPI HLOCAL WINAPI LocalHandle(LPCVOID pMem); + WINBASEAPI WINBOOL WINAPI LocalUnlock(HLOCAL hMem); + WINBASEAPI SIZE_T WINAPI LocalSize(HLOCAL hMem); + WINBASEAPI UINT WINAPI LocalFlags(HLOCAL hMem); + WINBASEAPI HLOCAL WINAPI LocalFree(HLOCAL hMem); + WINBASEAPI SIZE_T WINAPI LocalShrink(HLOCAL hMem,UINT cbNewSize); + WINBASEAPI SIZE_T WINAPI LocalCompact(UINT uMinFree); + WINBASEAPI WINBOOL WINAPI FlushInstructionCache(HANDLE hProcess,LPCVOID lpBaseAddress,SIZE_T dwSize); + WINBASEAPI LPVOID WINAPI VirtualAlloc(LPVOID lpAddress,SIZE_T dwSize,DWORD flAllocationType,DWORD flProtect); + WINBASEAPI WINBOOL WINAPI VirtualFree(LPVOID lpAddress,SIZE_T dwSize,DWORD dwFreeType); + WINBASEAPI WINBOOL WINAPI VirtualProtect(LPVOID lpAddress,SIZE_T dwSize,DWORD flNewProtect,PDWORD lpflOldProtect); + WINBASEAPI SIZE_T WINAPI VirtualQuery(LPCVOID lpAddress,PMEMORY_BASIC_INFORMATION lpBuffer,SIZE_T dwLength); + WINBASEAPI LPVOID WINAPI VirtualAllocEx(HANDLE hProcess,LPVOID lpAddress,SIZE_T dwSize,DWORD flAllocationType,DWORD flProtect); + WINBASEAPI UINT WINAPI GetWriteWatch(DWORD dwFlags,PVOID lpBaseAddress,SIZE_T dwRegionSize,PVOID *lpAddresses,ULONG_PTR *lpdwCount,PULONG lpdwGranularity); + WINBASEAPI UINT WINAPI ResetWriteWatch(LPVOID lpBaseAddress,SIZE_T dwRegionSize); + WINBASEAPI SIZE_T WINAPI GetLargePageMinimum(VOID); + WINBASEAPI UINT WINAPI EnumSystemFirmwareTables(DWORD FirmwareTableProviderSignature,PVOID pFirmwareTableEnumBuffer,DWORD BufferSize); + WINBASEAPI UINT WINAPI GetSystemFirmwareTable(DWORD FirmwareTableProviderSignature,DWORD FirmwareTableID,PVOID pFirmwareTableBuffer,DWORD BufferSize); + WINBASEAPI WINBOOL WINAPI VirtualFreeEx(HANDLE hProcess,LPVOID lpAddress,SIZE_T dwSize,DWORD dwFreeType); + WINBASEAPI WINBOOL WINAPI VirtualProtectEx(HANDLE hProcess,LPVOID lpAddress,SIZE_T dwSize,DWORD flNewProtect,PDWORD lpflOldProtect); + WINBASEAPI SIZE_T WINAPI VirtualQueryEx(HANDLE hProcess,LPCVOID lpAddress,PMEMORY_BASIC_INFORMATION lpBuffer,SIZE_T dwLength); + WINBASEAPI HANDLE WINAPI HeapCreate(DWORD flOptions,SIZE_T dwInitialSize,SIZE_T dwMaximumSize); + WINBASEAPI WINBOOL WINAPI HeapDestroy(HANDLE hHeap); + WINBASEAPI LPVOID WINAPI HeapAlloc(HANDLE hHeap,DWORD dwFlags,SIZE_T dwBytes); + WINBASEAPI LPVOID WINAPI HeapReAlloc(HANDLE hHeap,DWORD dwFlags,LPVOID lpMem,SIZE_T dwBytes); + WINBASEAPI WINBOOL WINAPI HeapFree(HANDLE hHeap,DWORD dwFlags,LPVOID lpMem); + WINBASEAPI SIZE_T WINAPI HeapSize(HANDLE hHeap,DWORD dwFlags,LPCVOID lpMem); + WINBASEAPI WINBOOL WINAPI HeapValidate(HANDLE hHeap,DWORD dwFlags,LPCVOID lpMem); + WINBASEAPI SIZE_T WINAPI HeapCompact(HANDLE hHeap,DWORD dwFlags); + WINBASEAPI HANDLE WINAPI GetProcessHeap(VOID); + WINBASEAPI DWORD WINAPI GetProcessHeaps(DWORD NumberOfHeaps,PHANDLE ProcessHeaps); + + typedef struct _PROCESS_HEAP_ENTRY { + PVOID lpData; + DWORD cbData; + BYTE cbOverhead; + BYTE iRegionIndex; + WORD wFlags; + union { + struct { + HANDLE hMem; + DWORD dwReserved[3]; + } Block; + struct { + DWORD dwCommittedSize; + DWORD dwUnCommittedSize; + LPVOID lpFirstBlock; + LPVOID lpLastBlock; + } Region; + }; + } PROCESS_HEAP_ENTRY,*LPPROCESS_HEAP_ENTRY,*PPROCESS_HEAP_ENTRY; + +#define PROCESS_HEAP_REGION 0x1 +#define PROCESS_HEAP_UNCOMMITTED_RANGE 0x2 +#define PROCESS_HEAP_ENTRY_BUSY 0x4 +#define PROCESS_HEAP_ENTRY_MOVEABLE 0x10 +#define PROCESS_HEAP_ENTRY_DDESHARE 0x20 + + WINBASEAPI WINBOOL WINAPI HeapLock(HANDLE hHeap); + WINBASEAPI WINBOOL WINAPI HeapUnlock(HANDLE hHeap); + WINBASEAPI WINBOOL WINAPI HeapWalk(HANDLE hHeap,LPPROCESS_HEAP_ENTRY lpEntry); + WINBASEAPI WINBOOL WINAPI HeapSetInformation(HANDLE HeapHandle,HEAP_INFORMATION_CLASS HeapInformationClass,PVOID HeapInformation,SIZE_T HeapInformationLength); + WINBASEAPI WINBOOL WINAPI HeapQueryInformation(HANDLE HeapHandle,HEAP_INFORMATION_CLASS HeapInformationClass,PVOID HeapInformation,SIZE_T HeapInformationLength,PSIZE_T ReturnLength); + +#define SCS_32BIT_BINARY 0 +#define SCS_DOS_BINARY 1 +#define SCS_WOW_BINARY 2 +#define SCS_PIF_BINARY 3 +#define SCS_POSIX_BINARY 4 +#define SCS_OS216_BINARY 5 +#define SCS_64BIT_BINARY 6 + +#ifdef UNICODE +#define GetBinaryType GetBinaryTypeW +#define GetShortPathName GetShortPathNameW +#define GetLongPathName GetLongPathNameW +#define GetEnvironmentStrings GetEnvironmentStringsW +#define SetEnvironmentStrings SetEnvironmentStringsW +#define FreeEnvironmentStrings FreeEnvironmentStringsW +#else +#define GetBinaryType GetBinaryTypeA +#define GetShortPathName GetShortPathNameA +#define GetLongPathName GetLongPathNameA +#define GetEnvironmentStringsA GetEnvironmentStrings +#define SetEnvironmentStrings SetEnvironmentStringsA +#define FreeEnvironmentStrings FreeEnvironmentStringsA +#endif + +#ifdef _WIN64 +#define SCS_THIS_PLATFORM_BINARY SCS_64BIT_BINARY +#else +#define SCS_THIS_PLATFORM_BINARY SCS_32BIT_BINARY +#endif + + WINBASEAPI WINBOOL WINAPI GetBinaryTypeA(LPCSTR lpApplicationName,LPDWORD lpBinaryType); + WINBASEAPI WINBOOL WINAPI GetBinaryTypeW(LPCWSTR lpApplicationName,LPDWORD lpBinaryType); + WINBASEAPI DWORD WINAPI GetShortPathNameA(LPCSTR lpszLongPath,LPSTR lpszShortPath,DWORD cchBuffer); + WINBASEAPI DWORD WINAPI GetShortPathNameW(LPCWSTR lpszLongPath,LPWSTR lpszShortPath,DWORD cchBuffer); + WINBASEAPI DWORD WINAPI GetLongPathNameA(LPCSTR lpszShortPath,LPSTR lpszLongPath,DWORD cchBuffer); + WINBASEAPI DWORD WINAPI GetLongPathNameW(LPCWSTR lpszShortPath,LPWSTR lpszLongPath,DWORD cchBuffer); + WINBASEAPI WINBOOL WINAPI GetProcessAffinityMask(HANDLE hProcess,PDWORD_PTR lpProcessAffinityMask,PDWORD_PTR lpSystemAffinityMask); + WINBASEAPI WINBOOL WINAPI SetProcessAffinityMask(HANDLE hProcess,DWORD_PTR dwProcessAffinityMask); + WINBASEAPI WINBOOL WINAPI GetProcessHandleCount(HANDLE hProcess,PDWORD pdwHandleCount); + WINBASEAPI WINBOOL WINAPI GetProcessTimes(HANDLE hProcess,LPFILETIME lpCreationTime,LPFILETIME lpExitTime,LPFILETIME lpKernelTime,LPFILETIME lpUserTime); + WINBASEAPI WINBOOL WINAPI GetProcessIoCounters(HANDLE hProcess,PIO_COUNTERS lpIoCounters); + WINBASEAPI WINBOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,PSIZE_T lpMinimumWorkingSetSize,PSIZE_T lpMaximumWorkingSetSize); + WINBASEAPI WINBOOL WINAPI GetProcessWorkingSetSizeEx(HANDLE hProcess,PSIZE_T lpMinimumWorkingSetSize,PSIZE_T lpMaximumWorkingSetSize,PDWORD Flags); + WINBASEAPI WINBOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,SIZE_T dwMinimumWorkingSetSize,SIZE_T dwMaximumWorkingSetSize); + WINBASEAPI WINBOOL WINAPI SetProcessWorkingSetSizeEx(HANDLE hProcess,SIZE_T dwMinimumWorkingSetSize,SIZE_T dwMaximumWorkingSetSize,DWORD Flags); + WINBASEAPI HANDLE WINAPI OpenProcess(DWORD dwDesiredAccess,WINBOOL bInheritHandle,DWORD dwProcessId); + WINBASEAPI HANDLE WINAPI GetCurrentProcess(VOID); + WINBASEAPI DWORD WINAPI GetCurrentProcessId(VOID); + WINBASEAPI DECLSPEC_NORETURN VOID WINAPI ExitProcess(UINT uExitCode); + WINBASEAPI WINBOOL WINAPI TerminateProcess(HANDLE hProcess,UINT uExitCode); + WINBASEAPI WINBOOL WINAPI GetExitCodeProcess(HANDLE hProcess,LPDWORD lpExitCode); + WINBASEAPI VOID WINAPI FatalExit(int ExitCode); + /* WINBASEAPI LPCH WINAPI GetEnvironmentStrings(VOID); */ + WINBASEAPI LPWCH WINAPI GetEnvironmentStringsW(VOID); + WINBASEAPI WINBOOL WINAPI SetEnvironmentStringsA(LPCH NewEnvironment); + WINBASEAPI WINBOOL WINAPI SetEnvironmentStringsW(LPWCH NewEnvironment); + WINBASEAPI WINBOOL WINAPI FreeEnvironmentStringsA(LPCH); + WINBASEAPI WINBOOL WINAPI FreeEnvironmentStringsW(LPWCH); + WINBASEAPI VOID WINAPI RaiseException(DWORD dwExceptionCode,DWORD dwExceptionFlags,DWORD nNumberOfArguments,CONST ULONG_PTR *lpArguments); + WINBASEAPI LONG WINAPI UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo); + + typedef LONG (WINAPI *PTOP_LEVEL_EXCEPTION_FILTER)(struct _EXCEPTION_POINTERS *ExceptionInfo); + typedef PTOP_LEVEL_EXCEPTION_FILTER LPTOP_LEVEL_EXCEPTION_FILTER; + + WINBASEAPI LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter); + +#define FIBER_FLAG_FLOAT_SWITCH 0x1 + + WINBASEAPI LPVOID WINAPI CreateFiber(SIZE_T dwStackSize,LPFIBER_START_ROUTINE lpStartAddress,LPVOID lpParameter); + WINBASEAPI LPVOID WINAPI CreateFiberEx(SIZE_T dwStackCommitSize,SIZE_T dwStackReserveSize,DWORD dwFlags,LPFIBER_START_ROUTINE lpStartAddress,LPVOID lpParameter); + WINBASEAPI VOID WINAPI DeleteFiber(LPVOID lpFiber); + WINBASEAPI LPVOID WINAPI ConvertThreadToFiber(LPVOID lpParameter); + WINBASEAPI LPVOID WINAPI ConvertThreadToFiberEx(LPVOID lpParameter,DWORD dwFlags); + WINBASEAPI WINBOOL WINAPI ConvertFiberToThread(VOID); + WINBASEAPI VOID WINAPI SwitchToFiber(LPVOID lpFiber); + WINBASEAPI WINBOOL WINAPI SwitchToThread(VOID); + WINBASEAPI HANDLE WINAPI CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes,SIZE_T dwStackSize,LPTHREAD_START_ROUTINE lpStartAddress,LPVOID lpParameter,DWORD dwCreationFlags,LPDWORD lpThreadId); + WINBASEAPI HANDLE WINAPI CreateRemoteThread(HANDLE hProcess,LPSECURITY_ATTRIBUTES lpThreadAttributes,SIZE_T dwStackSize,LPTHREAD_START_ROUTINE lpStartAddress,LPVOID lpParameter,DWORD dwCreationFlags,LPDWORD lpThreadId); + WINBASEAPI HANDLE WINAPI GetCurrentThread(VOID); + WINBASEAPI DWORD WINAPI GetCurrentThreadId(VOID); + WINBASEAPI WINBOOL WINAPI SetThreadStackGuarantee (PULONG StackSizeInBytes); + WINBASEAPI DWORD WINAPI GetProcessIdOfThread(HANDLE Thread); + WINBASEAPI DWORD WINAPI GetThreadId(HANDLE Thread); + WINBASEAPI DWORD WINAPI GetProcessId(HANDLE Process); + WINBASEAPI DWORD WINAPI GetCurrentProcessorNumber(VOID); + WINBASEAPI DWORD_PTR WINAPI SetThreadAffinityMask(HANDLE hThread,DWORD_PTR dwThreadAffinityMask); + WINBASEAPI DWORD WINAPI SetThreadIdealProcessor(HANDLE hThread,DWORD dwIdealProcessor); + WINBASEAPI WINBOOL WINAPI SetProcessPriorityBoost(HANDLE hProcess,WINBOOL bDisablePriorityBoost); + WINBASEAPI WINBOOL WINAPI GetProcessPriorityBoost(HANDLE hProcess,PBOOL pDisablePriorityBoost); + WINBASEAPI WINBOOL WINAPI RequestWakeupLatency(LATENCY_TIME latency); + WINBASEAPI WINBOOL WINAPI IsSystemResumeAutomatic(VOID); + WINBASEAPI HANDLE WINAPI OpenThread(DWORD dwDesiredAccess,WINBOOL bInheritHandle,DWORD dwThreadId); + WINBASEAPI WINBOOL WINAPI SetThreadPriority(HANDLE hThread,int nPriority); + WINBASEAPI WINBOOL WINAPI SetThreadPriorityBoost(HANDLE hThread,WINBOOL bDisablePriorityBoost); + WINBASEAPI WINBOOL WINAPI GetThreadPriorityBoost(HANDLE hThread,PBOOL pDisablePriorityBoost); + WINBASEAPI int WINAPI GetThreadPriority(HANDLE hThread); + WINBASEAPI WINBOOL WINAPI GetThreadTimes(HANDLE hThread,LPFILETIME lpCreationTime,LPFILETIME lpExitTime,LPFILETIME lpKernelTime,LPFILETIME lpUserTime); + WINBASEAPI WINBOOL WINAPI GetThreadIOPendingFlag(HANDLE hThread,PBOOL lpIOIsPending); + WINBASEAPI DECLSPEC_NORETURN VOID WINAPI ExitThread(DWORD dwExitCode); + WINBASEAPI WINBOOL WINAPI TerminateThread(HANDLE hThread,DWORD dwExitCode); + WINBASEAPI WINBOOL WINAPI GetExitCodeThread(HANDLE hThread,LPDWORD lpExitCode); + WINBASEAPI WINBOOL WINAPI GetThreadSelectorEntry(HANDLE hThread,DWORD dwSelector,LPLDT_ENTRY lpSelectorEntry); + WINBASEAPI EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE esFlags); + WINBASEAPI DWORD WINAPI GetLastError(VOID); + WINBASEAPI VOID WINAPI SetLastError(DWORD dwErrCode); + +#ifndef RC_INVOKED +#ifdef WINBASE_DECLARE_RESTORE_LAST_ERROR + WINBASEAPI VOID WINAPI RestoreLastError(DWORD dwErrCode); + + typedef VOID (WINAPI *PRESTORE_LAST_ERROR)(DWORD); + +#define RESTORE_LAST_ERROR_NAME_A "RestoreLastError" +#define RESTORE_LAST_ERROR_NAME_W L"RestoreLastError" +#define RESTORE_LAST_ERROR_NAME TEXT("RestoreLastError") +#endif +#endif + +#define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal)!=STATUS_PENDING) + + WINBASEAPI WINBOOL WINAPI GetOverlappedResult(HANDLE hFile,LPOVERLAPPED lpOverlapped,LPDWORD lpNumberOfBytesTransferred,WINBOOL bWait); + WINBASEAPI HANDLE WINAPI CreateIoCompletionPort(HANDLE FileHandle,HANDLE ExistingCompletionPort,ULONG_PTR CompletionKey,DWORD NumberOfConcurrentThreads); + WINBASEAPI WINBOOL WINAPI GetQueuedCompletionStatus(HANDLE CompletionPort,LPDWORD lpNumberOfBytesTransferred,PULONG_PTR lpCompletionKey,LPOVERLAPPED *lpOverlapped,DWORD dwMilliseconds); + WINBASEAPI WINBOOL WINAPI PostQueuedCompletionStatus(HANDLE CompletionPort,DWORD dwNumberOfBytesTransferred,ULONG_PTR dwCompletionKey,LPOVERLAPPED lpOverlapped); + +#define SEM_FAILCRITICALERRORS 0x1 +#define SEM_NOGPFAULTERRORBOX 0x2 +#define SEM_NOALIGNMENTFAULTEXCEPT 0x4 +#define SEM_NOOPENFILEERRORBOX 0x8000 + + WINBASEAPI UINT WINAPI SetErrorMode(UINT uMode); + WINBASEAPI WINBOOL WINAPI ReadProcessMemory(HANDLE hProcess,LPCVOID lpBaseAddress,LPVOID lpBuffer,SIZE_T nSize,SIZE_T *lpNumberOfBytesRead); + WINBASEAPI WINBOOL WINAPI WriteProcessMemory(HANDLE hProcess,LPVOID lpBaseAddress,LPCVOID lpBuffer,SIZE_T nSize,SIZE_T *lpNumberOfBytesWritten); + WINBASEAPI WINBOOL WINAPI GetThreadContext(HANDLE hThread,LPCONTEXT lpContext); + WINBASEAPI WINBOOL WINAPI SetThreadContext(HANDLE hThread,CONST CONTEXT *lpContext); + WINBASEAPI DWORD WINAPI SuspendThread(HANDLE hThread); + WINBASEAPI DWORD WINAPI ResumeThread(HANDLE hThread); + + typedef VOID (WINAPI *PAPCFUNC)(ULONG_PTR dwParam); + + WINBASEAPI DWORD WINAPI QueueUserAPC(PAPCFUNC pfnAPC,HANDLE hThread,ULONG_PTR dwData); + WINBASEAPI WINBOOL WINAPI IsDebuggerPresent(VOID); + WINBASEAPI WINBOOL WINAPI CheckRemoteDebuggerPresent(HANDLE hProcess,PBOOL pbDebuggerPresent); + WINBASEAPI VOID WINAPI DebugBreak(VOID); + WINBASEAPI WINBOOL WINAPI WaitForDebugEvent(LPDEBUG_EVENT lpDebugEvent,DWORD dwMilliseconds); + WINBASEAPI WINBOOL WINAPI ContinueDebugEvent(DWORD dwProcessId,DWORD dwThreadId,DWORD dwContinueStatus); + WINBASEAPI WINBOOL WINAPI DebugActiveProcess(DWORD dwProcessId); + WINBASEAPI WINBOOL WINAPI DebugActiveProcessStop(DWORD dwProcessId); + WINBASEAPI WINBOOL WINAPI DebugSetProcessKillOnExit(WINBOOL KillOnExit); + WINBASEAPI WINBOOL WINAPI DebugBreakProcess(HANDLE Process); + WINBASEAPI VOID WINAPI InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection); + WINBASEAPI VOID WINAPI EnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection); + WINBASEAPI VOID WINAPI LeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection); + WINBASEAPI WINBOOL WINAPI InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpCriticalSection,DWORD dwSpinCount); + WINBASEAPI DWORD WINAPI SetCriticalSectionSpinCount(LPCRITICAL_SECTION lpCriticalSection,DWORD dwSpinCount); + WINBASEAPI WINBOOL WINAPI TryEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection); + WINBASEAPI VOID WINAPI DeleteCriticalSection(LPCRITICAL_SECTION lpCriticalSection); + WINBASEAPI WINBOOL WINAPI SetEvent(HANDLE hEvent); + WINBASEAPI WINBOOL WINAPI ResetEvent(HANDLE hEvent); + WINBASEAPI WINBOOL WINAPI PulseEvent(HANDLE hEvent); + WINBASEAPI WINBOOL WINAPI ReleaseSemaphore(HANDLE hSemaphore,LONG lReleaseCount,LPLONG lpPreviousCount); + WINBASEAPI WINBOOL WINAPI ReleaseMutex(HANDLE hMutex); + WINBASEAPI DWORD WINAPI WaitForSingleObject(HANDLE hHandle,DWORD dwMilliseconds); + WINBASEAPI DWORD WINAPI WaitForMultipleObjects(DWORD nCount,CONST HANDLE *lpHandles,WINBOOL bWaitAll,DWORD dwMilliseconds); + WINBASEAPI VOID WINAPI Sleep(DWORD dwMilliseconds); + WINBASEAPI HGLOBAL WINAPI LoadResource(HMODULE hModule,HRSRC hResInfo); + WINBASEAPI DWORD WINAPI SizeofResource(HMODULE hModule,HRSRC hResInfo); + WINBASEAPI ATOM WINAPI GlobalDeleteAtom(ATOM nAtom); + WINBASEAPI WINBOOL WINAPI InitAtomTable(DWORD nSize); + WINBASEAPI ATOM WINAPI DeleteAtom(ATOM nAtom); + WINBASEAPI UINT WINAPI SetHandleCount(UINT uNumber); + WINBASEAPI DWORD WINAPI GetLogicalDrives(VOID); + WINBASEAPI WINBOOL WINAPI LockFile(HANDLE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh); + WINBASEAPI WINBOOL WINAPI UnlockFile(HANDLE hFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh); + WINBASEAPI WINBOOL WINAPI LockFileEx(HANDLE hFile,DWORD dwFlags,DWORD dwReserved,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh,LPOVERLAPPED lpOverlapped); + +#define LOCKFILE_FAIL_IMMEDIATELY 0x1 +#define LOCKFILE_EXCLUSIVE_LOCK 0x2 + + WINBASEAPI WINBOOL WINAPI UnlockFileEx(HANDLE hFile,DWORD dwReserved,DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh,LPOVERLAPPED lpOverlapped); + + typedef struct _BY_HANDLE_FILE_INFORMATION { + DWORD dwFileAttributes; + FILETIME ftCreationTime; + FILETIME ftLastAccessTime; + FILETIME ftLastWriteTime; + DWORD dwVolumeSerialNumber; + DWORD nFileSizeHigh; + DWORD nFileSizeLow; + DWORD nNumberOfLinks; + DWORD nFileIndexHigh; + DWORD nFileIndexLow; + } BY_HANDLE_FILE_INFORMATION,*PBY_HANDLE_FILE_INFORMATION,*LPBY_HANDLE_FILE_INFORMATION; + +#ifdef UNICODE +#define SetFileShortName SetFileShortNameW +#else +#define SetFileShortName SetFileShortNameA +#endif + + WINBASEAPI WINBOOL WINAPI GetFileInformationByHandle(HANDLE hFile,LPBY_HANDLE_FILE_INFORMATION lpFileInformation); + WINBASEAPI DWORD WINAPI GetFileType(HANDLE hFile); + WINBASEAPI DWORD WINAPI GetFileSize(HANDLE hFile,LPDWORD lpFileSizeHigh); + WINBASEAPI WINBOOL WINAPI GetFileSizeEx(HANDLE hFile,PLARGE_INTEGER lpFileSize); + WINBASEAPI HANDLE WINAPI GetStdHandle(DWORD nStdHandle); + WINBASEAPI WINBOOL WINAPI SetStdHandle(DWORD nStdHandle,HANDLE hHandle); + WINBASEAPI WINBOOL WINAPI WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped); + WINBASEAPI WINBOOL WINAPI ReadFile(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped); + WINBASEAPI WINBOOL WINAPI FlushFileBuffers(HANDLE hFile); + WINBASEAPI WINBOOL WINAPI DeviceIoControl(HANDLE hDevice,DWORD dwIoControlCode,LPVOID lpInBuffer,DWORD nInBufferSize,LPVOID lpOutBuffer,DWORD nOutBufferSize,LPDWORD lpBytesReturned,LPOVERLAPPED lpOverlapped); + WINBASEAPI WINBOOL WINAPI RequestDeviceWakeup(HANDLE hDevice); + WINBASEAPI WINBOOL WINAPI CancelDeviceWakeupRequest(HANDLE hDevice); + WINBASEAPI WINBOOL WINAPI GetDevicePowerState(HANDLE hDevice,WINBOOL *pfOn); + WINBASEAPI WINBOOL WINAPI SetMessageWaitingIndicator(HANDLE hMsgIndicator,ULONG ulMsgCount); + WINBASEAPI WINBOOL WINAPI SetEndOfFile(HANDLE hFile); + WINBASEAPI DWORD WINAPI SetFilePointer(HANDLE hFile,LONG lDistanceToMove,PLONG lpDistanceToMoveHigh,DWORD dwMoveMethod); + WINBASEAPI WINBOOL WINAPI SetFilePointerEx(HANDLE hFile,LARGE_INTEGER liDistanceToMove,PLARGE_INTEGER lpNewFilePointer,DWORD dwMoveMethod); + WINBASEAPI WINBOOL WINAPI FindClose(HANDLE hFindFile); + WINBASEAPI WINBOOL WINAPI GetFileTime(HANDLE hFile,LPFILETIME lpCreationTime,LPFILETIME lpLastAccessTime,LPFILETIME lpLastWriteTime); + WINBASEAPI WINBOOL WINAPI SetFileTime(HANDLE hFile,CONST FILETIME *lpCreationTime,CONST FILETIME *lpLastAccessTime,CONST FILETIME *lpLastWriteTime); + WINBASEAPI WINBOOL WINAPI SetFileValidData(HANDLE hFile,LONGLONG ValidDataLength); + WINBASEAPI WINBOOL WINAPI SetFileShortNameA(HANDLE hFile,LPCSTR lpShortName); + WINBASEAPI WINBOOL WINAPI SetFileShortNameW(HANDLE hFile,LPCWSTR lpShortName); + WINBASEAPI WINBOOL WINAPI CloseHandle(HANDLE hObject); + WINBASEAPI WINBOOL WINAPI DuplicateHandle(HANDLE hSourceProcessHandle,HANDLE hSourceHandle,HANDLE hTargetProcessHandle,LPHANDLE lpTargetHandle,DWORD dwDesiredAccess,WINBOOL bInheritHandle,DWORD dwOptions); + WINBASEAPI WINBOOL WINAPI GetHandleInformation(HANDLE hObject,LPDWORD lpdwFlags); + WINBASEAPI WINBOOL WINAPI SetHandleInformation(HANDLE hObject,DWORD dwMask,DWORD dwFlags); + +#define HANDLE_FLAG_INHERIT 0x1 +#define HANDLE_FLAG_PROTECT_FROM_CLOSE 0x2 + +#define HINSTANCE_ERROR 32 + + WINBASEAPI DWORD WINAPI LoadModule(LPCSTR lpModuleName,LPVOID lpParameterBlock); + WINBASEAPI UINT WINAPI WinExec(LPCSTR lpCmdLine,UINT uCmdShow); + WINBASEAPI WINBOOL WINAPI ClearCommBreak(HANDLE hFile); + WINBASEAPI WINBOOL WINAPI ClearCommError(HANDLE hFile,LPDWORD lpErrors,LPCOMSTAT lpStat); + WINBASEAPI WINBOOL WINAPI SetupComm(HANDLE hFile,DWORD dwInQueue,DWORD dwOutQueue); + WINBASEAPI WINBOOL WINAPI EscapeCommFunction(HANDLE hFile,DWORD dwFunc); + WINBASEAPI WINBOOL WINAPI GetCommConfig(HANDLE hCommDev,LPCOMMCONFIG lpCC,LPDWORD lpdwSize); + WINBASEAPI WINBOOL WINAPI GetCommMask(HANDLE hFile,LPDWORD lpEvtMask); + WINBASEAPI WINBOOL WINAPI GetCommProperties(HANDLE hFile,LPCOMMPROP lpCommProp); + WINBASEAPI WINBOOL WINAPI GetCommModemStatus(HANDLE hFile,LPDWORD lpModemStat); + WINBASEAPI WINBOOL WINAPI GetCommState(HANDLE hFile,LPDCB lpDCB); + WINBASEAPI WINBOOL WINAPI GetCommTimeouts(HANDLE hFile,LPCOMMTIMEOUTS lpCommTimeouts); + WINBASEAPI WINBOOL WINAPI PurgeComm(HANDLE hFile,DWORD dwFlags); + WINBASEAPI WINBOOL WINAPI SetCommBreak(HANDLE hFile); + WINBASEAPI WINBOOL WINAPI SetCommConfig(HANDLE hCommDev,LPCOMMCONFIG lpCC,DWORD dwSize); + WINBASEAPI WINBOOL WINAPI SetCommMask(HANDLE hFile,DWORD dwEvtMask); + WINBASEAPI WINBOOL WINAPI SetCommState(HANDLE hFile,LPDCB lpDCB); + WINBASEAPI WINBOOL WINAPI SetCommTimeouts(HANDLE hFile,LPCOMMTIMEOUTS lpCommTimeouts); + WINBASEAPI WINBOOL WINAPI TransmitCommChar(HANDLE hFile,char cChar); + WINBASEAPI WINBOOL WINAPI WaitCommEvent(HANDLE hFile,LPDWORD lpEvtMask,LPOVERLAPPED lpOverlapped); + WINBASEAPI DWORD WINAPI SetTapePosition(HANDLE hDevice,DWORD dwPositionMethod,DWORD dwPartition,DWORD dwOffsetLow,DWORD dwOffsetHigh,WINBOOL bImmediate); + WINBASEAPI DWORD WINAPI GetTapePosition(HANDLE hDevice,DWORD dwPositionType,LPDWORD lpdwPartition,LPDWORD lpdwOffsetLow,LPDWORD lpdwOffsetHigh); + WINBASEAPI DWORD WINAPI PrepareTape(HANDLE hDevice,DWORD dwOperation,WINBOOL bImmediate); + WINBASEAPI DWORD WINAPI EraseTape(HANDLE hDevice,DWORD dwEraseType,WINBOOL bImmediate); + WINBASEAPI DWORD WINAPI CreateTapePartition(HANDLE hDevice,DWORD dwPartitionMethod,DWORD dwCount,DWORD dwSize); + WINBASEAPI DWORD WINAPI WriteTapemark(HANDLE hDevice,DWORD dwTapemarkType,DWORD dwTapemarkCount,WINBOOL bImmediate); + WINBASEAPI DWORD WINAPI GetTapeStatus(HANDLE hDevice); + WINBASEAPI DWORD WINAPI GetTapeParameters(HANDLE hDevice,DWORD dwOperation,LPDWORD lpdwSize,LPVOID lpTapeInformation); + +#define GET_TAPE_MEDIA_INFORMATION 0 +#define GET_TAPE_DRIVE_INFORMATION 1 + + WINBASEAPI DWORD WINAPI SetTapeParameters(HANDLE hDevice,DWORD dwOperation,LPVOID lpTapeInformation); + +#define SET_TAPE_MEDIA_INFORMATION 0 +#define SET_TAPE_DRIVE_INFORMATION 1 + + WINBASEAPI WINBOOL WINAPI Beep(DWORD dwFreq,DWORD dwDuration); + WINBASEAPI int WINAPI MulDiv(int nNumber,int nNumerator,int nDenominator); + WINBASEAPI VOID WINAPI GetSystemTime(LPSYSTEMTIME lpSystemTime); + WINBASEAPI VOID WINAPI GetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime); + WINBASEAPI WINBOOL WINAPI SetSystemTime(CONST SYSTEMTIME *lpSystemTime); + WINBASEAPI VOID WINAPI GetLocalTime(LPSYSTEMTIME lpSystemTime); + WINBASEAPI WINBOOL WINAPI SetLocalTime(CONST SYSTEMTIME *lpSystemTime); + WINBASEAPI VOID WINAPI GetSystemInfo(LPSYSTEM_INFO lpSystemInfo); + WINBASEAPI WINBOOL WINAPI SetSystemFileCacheSize(SIZE_T MinimumFileCacheSize,SIZE_T MaximumFileCacheSize,DWORD Flags); + WINBASEAPI WINBOOL WINAPI GetSystemFileCacheSize(PSIZE_T lpMinimumFileCacheSize,PSIZE_T lpMaximumFileCacheSize,PDWORD lpFlags); + WINBASEAPI WINBOOL WINAPI GetSystemRegistryQuota(PDWORD pdwQuotaAllowed,PDWORD pdwQuotaUsed); + WINBOOL WINAPI GetSystemTimes(LPFILETIME lpIdleTime,LPFILETIME lpKernelTime,LPFILETIME lpUserTime); + WINBASEAPI VOID WINAPI GetNativeSystemInfo(LPSYSTEM_INFO lpSystemInfo); + WINBASEAPI WINBOOL WINAPI IsProcessorFeaturePresent(DWORD ProcessorFeature); + + typedef struct _TIME_ZONE_INFORMATION { + LONG Bias; + WCHAR StandardName[32]; + SYSTEMTIME StandardDate; + LONG StandardBias; + WCHAR DaylightName[32]; + SYSTEMTIME DaylightDate; + LONG DaylightBias; + } TIME_ZONE_INFORMATION,*PTIME_ZONE_INFORMATION,*LPTIME_ZONE_INFORMATION; + +#ifdef UNICODE +#define FormatMessage FormatMessageW +#else +#define FormatMessage FormatMessageA +#endif + + WINBASEAPI WINBOOL WINAPI SystemTimeToTzSpecificLocalTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation,LPSYSTEMTIME lpUniversalTime,LPSYSTEMTIME lpLocalTime); + WINBASEAPI WINBOOL WINAPI TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation,LPSYSTEMTIME lpLocalTime,LPSYSTEMTIME lpUniversalTime); + WINBASEAPI DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation); + WINBASEAPI WINBOOL WINAPI SetTimeZoneInformation(CONST TIME_ZONE_INFORMATION *lpTimeZoneInformation); + WINBASEAPI WINBOOL WINAPI SystemTimeToFileTime(CONST SYSTEMTIME *lpSystemTime,LPFILETIME lpFileTime); + WINBASEAPI WINBOOL WINAPI FileTimeToLocalFileTime(CONST FILETIME *lpFileTime,LPFILETIME lpLocalFileTime); + WINBASEAPI WINBOOL WINAPI LocalFileTimeToFileTime(CONST FILETIME *lpLocalFileTime,LPFILETIME lpFileTime); + WINBASEAPI WINBOOL WINAPI FileTimeToSystemTime(CONST FILETIME *lpFileTime,LPSYSTEMTIME lpSystemTime); + WINBASEAPI LONG WINAPI CompareFileTime(CONST FILETIME *lpFileTime1,CONST FILETIME *lpFileTime2); + WINBASEAPI WINBOOL WINAPI FileTimeToDosDateTime(CONST FILETIME *lpFileTime,LPWORD lpFatDate,LPWORD lpFatTime); + WINBASEAPI WINBOOL WINAPI DosDateTimeToFileTime(WORD wFatDate,WORD wFatTime,LPFILETIME lpFileTime); + WINBASEAPI DWORD WINAPI GetTickCount(VOID); + WINBASEAPI WINBOOL WINAPI SetSystemTimeAdjustment(DWORD dwTimeAdjustment,WINBOOL bTimeAdjustmentDisabled); + WINBASEAPI WINBOOL WINAPI GetSystemTimeAdjustment(PDWORD lpTimeAdjustment,PDWORD lpTimeIncrement,PBOOL lpTimeAdjustmentDisabled); + WINBASEAPI DWORD WINAPI FormatMessageA(DWORD dwFlags,LPCVOID lpSource,DWORD dwMessageId,DWORD dwLanguageId,LPSTR lpBuffer,DWORD nSize,va_list *Arguments); + WINBASEAPI DWORD WINAPI FormatMessageW(DWORD dwFlags,LPCVOID lpSource,DWORD dwMessageId,DWORD dwLanguageId,LPWSTR lpBuffer,DWORD nSize,va_list *Arguments); + +#define FORMAT_MESSAGE_ALLOCATE_BUFFER 0x100 +#define FORMAT_MESSAGE_IGNORE_INSERTS 0x200 +#define FORMAT_MESSAGE_FROM_STRING 0x400 +#define FORMAT_MESSAGE_FROM_HMODULE 0x800 +#define FORMAT_MESSAGE_FROM_SYSTEM 0x1000 +#define FORMAT_MESSAGE_ARGUMENT_ARRAY 0x2000 +#define FORMAT_MESSAGE_MAX_WIDTH_MASK 0xff + +#ifdef UNICODE +#define CreateMailslot CreateMailslotW +#define EncryptFile EncryptFileW +#define DecryptFile DecryptFileW +#define FileEncryptionStatus FileEncryptionStatusW +#else +#define CreateMailslot CreateMailslotA +#define EncryptFile EncryptFileA +#define DecryptFile DecryptFileA +#define FileEncryptionStatus FileEncryptionStatusA +#endif + + WINBASEAPI WINBOOL WINAPI CreatePipe(PHANDLE hReadPipe,PHANDLE hWritePipe,LPSECURITY_ATTRIBUTES lpPipeAttributes,DWORD nSize); + WINBASEAPI WINBOOL WINAPI ConnectNamedPipe(HANDLE hNamedPipe,LPOVERLAPPED lpOverlapped); + WINBASEAPI WINBOOL WINAPI DisconnectNamedPipe(HANDLE hNamedPipe); + WINBASEAPI WINBOOL WINAPI SetNamedPipeHandleState(HANDLE hNamedPipe,LPDWORD lpMode,LPDWORD lpMaxCollectionCount,LPDWORD lpCollectDataTimeout); + WINBASEAPI WINBOOL WINAPI GetNamedPipeInfo(HANDLE hNamedPipe,LPDWORD lpFlags,LPDWORD lpOutBufferSize,LPDWORD lpInBufferSize,LPDWORD lpMaxInstances); + WINBASEAPI WINBOOL WINAPI PeekNamedPipe(HANDLE hNamedPipe,LPVOID lpBuffer,DWORD nBufferSize,LPDWORD lpBytesRead,LPDWORD lpTotalBytesAvail,LPDWORD lpBytesLeftThisMessage); + WINBASEAPI WINBOOL WINAPI TransactNamedPipe(HANDLE hNamedPipe,LPVOID lpInBuffer,DWORD nInBufferSize,LPVOID lpOutBuffer,DWORD nOutBufferSize,LPDWORD lpBytesRead,LPOVERLAPPED lpOverlapped); + WINBASEAPI HANDLE WINAPI CreateMailslotA(LPCSTR lpName,DWORD nMaxMessageSize,DWORD lReadTimeout,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI HANDLE WINAPI CreateMailslotW(LPCWSTR lpName,DWORD nMaxMessageSize,DWORD lReadTimeout,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI GetMailslotInfo(HANDLE hMailslot,LPDWORD lpMaxMessageSize,LPDWORD lpNextSize,LPDWORD lpMessageCount,LPDWORD lpReadTimeout); + WINBASEAPI WINBOOL WINAPI SetMailslotInfo(HANDLE hMailslot,DWORD lReadTimeout); + WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE hFileMappingObject,DWORD dwDesiredAccess,DWORD dwFileOffsetHigh,DWORD dwFileOffsetLow,SIZE_T dwNumberOfBytesToMap); + WINBASEAPI WINBOOL WINAPI FlushViewOfFile(LPCVOID lpBaseAddress,SIZE_T dwNumberOfBytesToFlush); + WINBASEAPI WINBOOL WINAPI UnmapViewOfFile(LPCVOID lpBaseAddress); + WINADVAPI WINBOOL WINAPI EncryptFileA(LPCSTR lpFileName); + WINADVAPI WINBOOL WINAPI EncryptFileW(LPCWSTR lpFileName); + WINADVAPI WINBOOL WINAPI DecryptFileA(LPCSTR lpFileName,DWORD dwReserved); + WINADVAPI WINBOOL WINAPI DecryptFileW(LPCWSTR lpFileName,DWORD dwReserved); + +#define FILE_ENCRYPTABLE 0 +#define FILE_IS_ENCRYPTED 1 +#define FILE_SYSTEM_ATTR 2 +#define FILE_ROOT_DIR 3 +#define FILE_SYSTEM_DIR 4 +#define FILE_UNKNOWN 5 +#define FILE_SYSTEM_NOT_SUPPORT 6 +#define FILE_USER_DISALLOWED 7 +#define FILE_READ_ONLY 8 +#define FILE_DIR_DISALLOWED 9 + + WINADVAPI WINBOOL WINAPI FileEncryptionStatusA(LPCSTR lpFileName,LPDWORD lpStatus); + WINADVAPI WINBOOL WINAPI FileEncryptionStatusW(LPCWSTR lpFileName,LPDWORD lpStatus); + +#define EFS_USE_RECOVERY_KEYS (0x1) + + typedef DWORD (WINAPI *PFE_EXPORT_FUNC)(PBYTE pbData,PVOID pvCallbackContext,ULONG ulLength); + typedef DWORD (WINAPI *PFE_IMPORT_FUNC)(PBYTE pbData,PVOID pvCallbackContext,PULONG ulLength); + +#define CREATE_FOR_IMPORT (1) +#define CREATE_FOR_DIR (2) +#define OVERWRITE_HIDDEN (4) + +#ifdef UNICODE +#define OpenEncryptedFileRaw OpenEncryptedFileRawW +#define lstrcmp lstrcmpW +#define lstrcmpi lstrcmpiW +#define lstrcpyn lstrcpynW +#define lstrcpy lstrcpyW +#define lstrcat lstrcatW +#define lstrlen lstrlenW +#else +#define OpenEncryptedFileRaw OpenEncryptedFileRawA +#define lstrcmp lstrcmpA +#define lstrcmpi lstrcmpiA +#define lstrcpyn lstrcpynA +#define lstrcpy lstrcpyA +#define lstrcat lstrcatA +#define lstrlen lstrlenA +#endif + + WINADVAPI DWORD WINAPI OpenEncryptedFileRawA(LPCSTR lpFileName,ULONG ulFlags,PVOID *pvContext); + WINADVAPI DWORD WINAPI OpenEncryptedFileRawW(LPCWSTR lpFileName,ULONG ulFlags,PVOID *pvContext); + WINADVAPI DWORD WINAPI ReadEncryptedFileRaw(PFE_EXPORT_FUNC pfExportCallback,PVOID pvCallbackContext,PVOID pvContext); + WINADVAPI DWORD WINAPI WriteEncryptedFileRaw(PFE_IMPORT_FUNC pfImportCallback,PVOID pvCallbackContext,PVOID pvContext); + WINADVAPI VOID WINAPI CloseEncryptedFileRaw(PVOID pvContext); + WINBASEAPI int WINAPI lstrcmpA(LPCSTR lpString1,LPCSTR lpString2); + WINBASEAPI int WINAPI lstrcmpW(LPCWSTR lpString1,LPCWSTR lpString2); + WINBASEAPI int WINAPI lstrcmpiA(LPCSTR lpString1,LPCSTR lpString2); + WINBASEAPI int WINAPI lstrcmpiW(LPCWSTR lpString1,LPCWSTR lpString2); + WINBASEAPI LPSTR WINAPI lstrcpynA(LPSTR lpString1,LPCSTR lpString2,int iMaxLength); + WINBASEAPI LPWSTR WINAPI lstrcpynW(LPWSTR lpString1,LPCWSTR lpString2,int iMaxLength); + WINBASEAPI LPSTR WINAPI lstrcpyA(LPSTR lpString1,LPCSTR lpString2); + WINBASEAPI LPWSTR WINAPI lstrcpyW(LPWSTR lpString1,LPCWSTR lpString2); + WINBASEAPI LPSTR WINAPI lstrcatA(LPSTR lpString1,LPCSTR lpString2); + WINBASEAPI LPWSTR WINAPI lstrcatW(LPWSTR lpString1,LPCWSTR lpString2); + WINBASEAPI int WINAPI lstrlenA(LPCSTR lpString); + WINBASEAPI int WINAPI lstrlenW(LPCWSTR lpString); + WINBASEAPI HFILE WINAPI OpenFile(LPCSTR lpFileName,LPOFSTRUCT lpReOpenBuff,UINT uStyle); + WINBASEAPI HFILE WINAPI _lopen(LPCSTR lpPathName,int iReadWrite); + WINBASEAPI HFILE WINAPI _lcreat(LPCSTR lpPathName,int iAttribute); + WINBASEAPI UINT WINAPI _lread(HFILE hFile,LPVOID lpBuffer,UINT uBytes); + WINBASEAPI UINT WINAPI _lwrite(HFILE hFile,LPCCH lpBuffer,UINT uBytes); + WINBASEAPI long WINAPI _hread(HFILE hFile,LPVOID lpBuffer,long lBytes); + WINBASEAPI long WINAPI _hwrite(HFILE hFile,LPCCH lpBuffer,long lBytes); + WINBASEAPI HFILE WINAPI _lclose(HFILE hFile); + WINBASEAPI LONG WINAPI _llseek(HFILE hFile,LONG lOffset,int iOrigin); + WINADVAPI WINBOOL WINAPI IsTextUnicode(CONST VOID *lpv,int iSize,LPINT lpiResult); + +#define FLS_OUT_OF_INDEXES ((DWORD)0xffffffff) + + WINBASEAPI DWORD WINAPI FlsAlloc(PFLS_CALLBACK_FUNCTION lpCallback); + WINBASEAPI PVOID WINAPI FlsGetValue(DWORD dwFlsIndex); + WINBASEAPI WINBOOL WINAPI FlsSetValue(DWORD dwFlsIndex,PVOID lpFlsData); + WINBASEAPI WINBOOL WINAPI FlsFree(DWORD dwFlsIndex); + +#define TLS_OUT_OF_INDEXES ((DWORD)0xffffffff) + + WINBASEAPI DWORD WINAPI TlsAlloc(VOID); + WINBASEAPI LPVOID WINAPI TlsGetValue(DWORD dwTlsIndex); + WINBASEAPI WINBOOL WINAPI TlsSetValue(DWORD dwTlsIndex,LPVOID lpTlsValue); + WINBASEAPI WINBOOL WINAPI TlsFree(DWORD dwTlsIndex); + + typedef VOID (WINAPI *LPOVERLAPPED_COMPLETION_ROUTINE)(DWORD dwErrorCode,DWORD dwNumberOfBytesTransfered,LPOVERLAPPED lpOverlapped); + + WINBASEAPI DWORD WINAPI SleepEx(DWORD dwMilliseconds,WINBOOL bAlertable); + WINBASEAPI DWORD WINAPI WaitForSingleObjectEx(HANDLE hHandle,DWORD dwMilliseconds,WINBOOL bAlertable); + WINBASEAPI DWORD WINAPI WaitForMultipleObjectsEx(DWORD nCount,CONST HANDLE *lpHandles,WINBOOL bWaitAll,DWORD dwMilliseconds,WINBOOL bAlertable); + WINBASEAPI DWORD WINAPI SignalObjectAndWait(HANDLE hObjectToSignal,HANDLE hObjectToWaitOn,DWORD dwMilliseconds,WINBOOL bAlertable); + WINBASEAPI WINBOOL WINAPI ReadFileEx(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPOVERLAPPED lpOverlapped,LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + WINBASEAPI WINBOOL WINAPI WriteFileEx(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPOVERLAPPED lpOverlapped,LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + WINBASEAPI WINBOOL WINAPI BackupRead(HANDLE hFile,LPBYTE lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,WINBOOL bAbort,WINBOOL bProcessSecurity,LPVOID *lpContext); + WINBASEAPI WINBOOL WINAPI BackupSeek(HANDLE hFile,DWORD dwLowBytesToSeek,DWORD dwHighBytesToSeek,LPDWORD lpdwLowByteSeeked,LPDWORD lpdwHighByteSeeked,LPVOID *lpContext); + WINBASEAPI WINBOOL WINAPI BackupWrite(HANDLE hFile,LPBYTE lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,WINBOOL bAbort,WINBOOL bProcessSecurity,LPVOID *lpContext); + + typedef struct _WIN32_STREAM_ID { + DWORD dwStreamId; + DWORD dwStreamAttributes; + LARGE_INTEGER Size; + DWORD dwStreamNameSize; + WCHAR cStreamName[ANYSIZE_ARRAY]; + } WIN32_STREAM_ID,*LPWIN32_STREAM_ID; + +#define BACKUP_INVALID 0x0 +#define BACKUP_DATA 0x1 +#define BACKUP_EA_DATA 0x2 +#define BACKUP_SECURITY_DATA 0x3 +#define BACKUP_ALTERNATE_DATA 0x4 +#define BACKUP_LINK 0x5 +#define BACKUP_PROPERTY_DATA 0x6 +#define BACKUP_OBJECT_ID 0x7 +#define BACKUP_REPARSE_DATA 0x8 +#define BACKUP_SPARSE_BLOCK 0x9 + +#define STREAM_NORMAL_ATTRIBUTE 0x0 +#define STREAM_MODIFIED_WHEN_READ 0x1 +#define STREAM_CONTAINS_SECURITY 0x2 +#define STREAM_CONTAINS_PROPERTIES 0x4 +#define STREAM_SPARSE_ATTRIBUTE 0x8 + + WINBASEAPI WINBOOL WINAPI ReadFileScatter(HANDLE hFile,FILE_SEGMENT_ELEMENT aSegmentArray[],DWORD nNumberOfBytesToRead,LPDWORD lpReserved,LPOVERLAPPED lpOverlapped); + WINBASEAPI WINBOOL WINAPI WriteFileGather(HANDLE hFile,FILE_SEGMENT_ELEMENT aSegmentArray[],DWORD nNumberOfBytesToWrite,LPDWORD lpReserved,LPOVERLAPPED lpOverlapped); + +#define STARTF_USESHOWWINDOW 0x1 +#define STARTF_USESIZE 0x2 +#define STARTF_USEPOSITION 0x4 +#define STARTF_USECOUNTCHARS 0x8 +#define STARTF_USEFILLATTRIBUTE 0x10 +#define STARTF_RUNFULLSCREEN 0x20 +#define STARTF_FORCEONFEEDBACK 0x40 +#define STARTF_FORCEOFFFEEDBACK 0x80 +#define STARTF_USESTDHANDLES 0x100 + +#define STARTF_USEHOTKEY 0x200 + + typedef struct _STARTUPINFOA { + DWORD cb; + LPSTR lpReserved; + LPSTR lpDesktop; + LPSTR lpTitle; + DWORD dwX; + DWORD dwY; + DWORD dwXSize; + DWORD dwYSize; + DWORD dwXCountChars; + DWORD dwYCountChars; + DWORD dwFillAttribute; + DWORD dwFlags; + WORD wShowWindow; + WORD cbReserved2; + LPBYTE lpReserved2; + HANDLE hStdInput; + HANDLE hStdOutput; + HANDLE hStdError; + } STARTUPINFOA,*LPSTARTUPINFOA; + + typedef struct _STARTUPINFOW { + DWORD cb; + LPWSTR lpReserved; + LPWSTR lpDesktop; + LPWSTR lpTitle; + DWORD dwX; + DWORD dwY; + DWORD dwXSize; + DWORD dwYSize; + DWORD dwXCountChars; + DWORD dwYCountChars; + DWORD dwFillAttribute; + DWORD dwFlags; + WORD wShowWindow; + WORD cbReserved2; + LPBYTE lpReserved2; + HANDLE hStdInput; + HANDLE hStdOutput; + HANDLE hStdError; + } STARTUPINFOW,*LPSTARTUPINFOW; + +#ifdef UNICODE + typedef STARTUPINFOW STARTUPINFO; + typedef LPSTARTUPINFOW LPSTARTUPINFO; +#else + typedef STARTUPINFOA STARTUPINFO; + typedef LPSTARTUPINFOA LPSTARTUPINFO; +#endif + +#define SHUTDOWN_NORETRY 0x1 + + typedef struct _WIN32_FIND_DATAA { + DWORD dwFileAttributes; + FILETIME ftCreationTime; + FILETIME ftLastAccessTime; + FILETIME ftLastWriteTime; + DWORD nFileSizeHigh; + DWORD nFileSizeLow; + DWORD dwReserved0; + DWORD dwReserved1; + CHAR cFileName[MAX_PATH]; + CHAR cAlternateFileName[14]; + } WIN32_FIND_DATAA,*PWIN32_FIND_DATAA,*LPWIN32_FIND_DATAA; + + typedef struct _WIN32_FIND_DATAW { + DWORD dwFileAttributes; + FILETIME ftCreationTime; + FILETIME ftLastAccessTime; + FILETIME ftLastWriteTime; + DWORD nFileSizeHigh; + DWORD nFileSizeLow; + DWORD dwReserved0; + DWORD dwReserved1; + WCHAR cFileName[MAX_PATH]; + WCHAR cAlternateFileName[14]; + } WIN32_FIND_DATAW,*PWIN32_FIND_DATAW,*LPWIN32_FIND_DATAW; + +#ifdef UNICODE + typedef WIN32_FIND_DATAW WIN32_FIND_DATA; + typedef PWIN32_FIND_DATAW PWIN32_FIND_DATA; + typedef LPWIN32_FIND_DATAW LPWIN32_FIND_DATA; +#else + typedef WIN32_FIND_DATAA WIN32_FIND_DATA; + typedef PWIN32_FIND_DATAA PWIN32_FIND_DATA; + typedef LPWIN32_FIND_DATAA LPWIN32_FIND_DATA; +#endif + + typedef struct _WIN32_FILE_ATTRIBUTE_DATA { + DWORD dwFileAttributes; + FILETIME ftCreationTime; + FILETIME ftLastAccessTime; + FILETIME ftLastWriteTime; + DWORD nFileSizeHigh; + DWORD nFileSizeLow; + } WIN32_FILE_ATTRIBUTE_DATA,*LPWIN32_FILE_ATTRIBUTE_DATA; + +#ifdef UNICODE +#define CreateMutex CreateMutexW +#define OpenMutex OpenMutexW +#define CreateEvent CreateEventW +#define OpenEvent OpenEventW +#define CreateSemaphore CreateSemaphoreW +#define OpenSemaphore OpenSemaphoreW +#else +#define CreateMutex CreateMutexA +#define OpenMutex OpenMutexA +#define CreateEvent CreateEventA +#define OpenEvent OpenEventA +#define CreateSemaphore CreateSemaphoreA +#define OpenSemaphore OpenSemaphoreA +#endif + + WINBASEAPI HANDLE WINAPI CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes,WINBOOL bInitialOwner,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes,WINBOOL bInitialOwner,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI OpenMutexA(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI OpenMutexW(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes,WINBOOL bManualReset,WINBOOL bInitialState,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes,WINBOOL bManualReset,WINBOOL bInitialState,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI OpenEventA(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI OpenEventW(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI CreateSemaphoreA(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,LONG lInitialCount,LONG lMaximumCount,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI CreateSemaphoreW(LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,LONG lInitialCount,LONG lMaximumCount,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI OpenSemaphoreA(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI OpenSemaphoreW(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCWSTR lpName); + + typedef VOID (WINAPI *PTIMERAPCROUTINE)(LPVOID lpArgToCompletionRoutine,DWORD dwTimerLowValue,DWORD dwTimerHighValue); + +#ifdef UNICODE +#define CreateWaitableTimer CreateWaitableTimerW +#define OpenWaitableTimer OpenWaitableTimerW +#define CreateFileMapping CreateFileMappingW +#define OpenFileMapping OpenFileMappingW +#define GetLogicalDriveStrings GetLogicalDriveStringsW +#define LoadLibrary LoadLibraryW +#define LoadLibraryEx LoadLibraryExW +#define GetModuleFileName GetModuleFileNameW +#define GetModuleHandle GetModuleHandleW +#else +#define CreateWaitableTimer CreateWaitableTimerA +#define OpenWaitableTimer OpenWaitableTimerA +#define CreateFileMapping CreateFileMappingA +#define OpenFileMapping OpenFileMappingA +#define GetLogicalDriveStrings GetLogicalDriveStringsA +#define LoadLibrary LoadLibraryA +#define LoadLibraryEx LoadLibraryExA +#define GetModuleFileName GetModuleFileNameA +#define GetModuleHandle GetModuleHandleA +#endif + + WINBASEAPI HANDLE WINAPI CreateWaitableTimerA(LPSECURITY_ATTRIBUTES lpTimerAttributes,WINBOOL bManualReset,LPCSTR lpTimerName); + WINBASEAPI HANDLE WINAPI CreateWaitableTimerW(LPSECURITY_ATTRIBUTES lpTimerAttributes,WINBOOL bManualReset,LPCWSTR lpTimerName); + WINBASEAPI HANDLE WINAPI OpenWaitableTimerA(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCSTR lpTimerName); + WINBASEAPI HANDLE WINAPI OpenWaitableTimerW(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCWSTR lpTimerName); + WINBASEAPI WINBOOL WINAPI SetWaitableTimer(HANDLE hTimer,const LARGE_INTEGER *lpDueTime,LONG lPeriod,PTIMERAPCROUTINE pfnCompletionRoutine,LPVOID lpArgToCompletionRoutine,WINBOOL fResume); + WINBASEAPI WINBOOL WINAPI CancelWaitableTimer(HANDLE hTimer); + WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE hFile,LPSECURITY_ATTRIBUTES lpFileMappingAttributes,DWORD flProtect,DWORD dwMaximumSizeHigh,DWORD dwMaximumSizeLow,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE hFile,LPSECURITY_ATTRIBUTES lpFileMappingAttributes,DWORD flProtect,DWORD dwMaximumSizeHigh,DWORD dwMaximumSizeLow,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI OpenFileMappingA(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI OpenFileMappingW(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCWSTR lpName); + WINBASEAPI DWORD WINAPI GetLogicalDriveStringsA(DWORD nBufferLength,LPSTR lpBuffer); + WINBASEAPI DWORD WINAPI GetLogicalDriveStringsW(DWORD nBufferLength,LPWSTR lpBuffer); + + typedef enum _MEMORY_RESOURCE_NOTIFICATION_TYPE { + LowMemoryResourceNotification,HighMemoryResourceNotification + } MEMORY_RESOURCE_NOTIFICATION_TYPE; + + WINBASEAPI HANDLE WINAPI CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE NotificationType); + WINBASEAPI WINBOOL WINAPI QueryMemoryResourceNotification(HANDLE ResourceNotificationHandle,PBOOL ResourceState); + WINBASEAPI HMODULE WINAPI LoadLibraryA(LPCSTR lpLibFileName); + WINBASEAPI HMODULE WINAPI LoadLibraryW(LPCWSTR lpLibFileName); + WINBASEAPI HMODULE WINAPI LoadLibraryExA(LPCSTR lpLibFileName,HANDLE hFile,DWORD dwFlags); + WINBASEAPI HMODULE WINAPI LoadLibraryExW(LPCWSTR lpLibFileName,HANDLE hFile,DWORD dwFlags); + +#define DONT_RESOLVE_DLL_REFERENCES 0x1 +#define LOAD_LIBRARY_AS_DATAFILE 0x2 +#define LOAD_WITH_ALTERED_SEARCH_PATH 0x8 +#define LOAD_IGNORE_CODE_AUTHZ_LEVEL 0x10 +#define LOAD_LINRARY_AS_IMAGE_RESOURCE 0x20 +#define LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE 0x40 + + WINBASEAPI DWORD WINAPI GetModuleFileNameA(HMODULE hModule,LPCH lpFilename,DWORD nSize); + WINBASEAPI DWORD WINAPI GetModuleFileNameW(HMODULE hModule,LPWCH lpFilename,DWORD nSize); + WINBASEAPI HMODULE WINAPI GetModuleHandleA(LPCSTR lpModuleName); + WINBASEAPI HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName); + +#ifndef RC_INVOKED +#define GET_MODULE_HANDLE_EX_FLAG_PIN (0x1) +#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT (0x2) +#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS (0x4) + + typedef WINBOOL (WINAPI *PGET_MODULE_HANDLE_EXA)(DWORD dwFlags,LPCSTR lpModuleName,HMODULE *phModule); + typedef WINBOOL (WINAPI *PGET_MODULE_HANDLE_EXW)(DWORD dwFlags,LPCWSTR lpModuleName,HMODULE *phModule); + +#ifdef UNICODE +#define PGET_MODULE_HANDLE_EX PGET_MODULE_HANDLE_EXW +#define GetModuleHandleEx GetModuleHandleExW +#else +#define PGET_MODULE_HANDLE_EX PGET_MODULE_HANDLE_EXA +#define GetModuleHandleEx GetModuleHandleExA +#endif + + WINBASEAPI WINBOOL WINAPI GetModuleHandleExA(DWORD dwFlags,LPCSTR lpModuleName,HMODULE *phModule); + WINBASEAPI WINBOOL WINAPI GetModuleHandleExW(DWORD dwFlags,LPCWSTR lpModuleName,HMODULE *phModule); +#endif + +#ifdef UNICODE +#define NeedCurrentDirectoryForExePath NeedCurrentDirectoryForExePathW +#define CreateProcess CreateProcessW +#define FatalAppExit FatalAppExitW +#define GetStartupInfo GetStartupInfoW +#define GetCommandLine GetCommandLineW +#define GetEnvironmentVariable GetEnvironmentVariableW +#define SetEnvironmentVariable SetEnvironmentVariableW +#define ExpandEnvironmentStrings ExpandEnvironmentStringsW +#define GetFirmwareEnvironmentVariable GetFirmwareEnvironmentVariableW +#define SetFirmwareEnvironmentVariable SetFirmwareEnvironmentVariableW +#define OutputDebugString OutputDebugStringW +#define FindResource FindResourceW +#define FindResourceEx FindResourceExW +#else +#define NeedCurrentDirectoryForExePath NeedCurrentDirectoryForExePathA +#define CreateProcess CreateProcessA +#define FatalAppExit FatalAppExitA +#define GetStartupInfo GetStartupInfoA +#define GetCommandLine GetCommandLineA +#define GetEnvironmentVariable GetEnvironmentVariableA +#define SetEnvironmentVariable SetEnvironmentVariableA +#define ExpandEnvironmentStrings ExpandEnvironmentStringsA +#define GetFirmwareEnvironmentVariable GetFirmwareEnvironmentVariableA +#define SetFirmwareEnvironmentVariable SetFirmwareEnvironmentVariableA +#define OutputDebugString OutputDebugStringA +#define FindResource FindResourceA +#define FindResourceEx FindResourceExA +#endif + + WINBASEAPI WINBOOL WINAPI NeedCurrentDirectoryForExePathA(LPCSTR ExeName); + WINBASEAPI WINBOOL WINAPI NeedCurrentDirectoryForExePathW(LPCWSTR ExeName); + WINBASEAPI WINBOOL WINAPI CreateProcessA(LPCSTR lpApplicationName,LPSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,LPSECURITY_ATTRIBUTES lpThreadAttributes,WINBOOL bInheritHandles,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCSTR lpCurrentDirectory,LPSTARTUPINFOA lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation); + WINBASEAPI WINBOOL WINAPI CreateProcessW(LPCWSTR lpApplicationName,LPWSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,LPSECURITY_ATTRIBUTES lpThreadAttributes,WINBOOL bInheritHandles,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCWSTR lpCurrentDirectory,LPSTARTUPINFOW lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation); + WINBASEAPI DWORD WINAPI AddLocalAlternateComputerNameA(LPCSTR lpDnsFQHostname,ULONG ulFlags); + WINBASEAPI DWORD WINAPI AddLocalAlternateComputerNameW(LPCWSTR lpDnsFQHostname,ULONG ulFlags); + WINBASEAPI WINBOOL WINAPI SetProcessShutdownParameters(DWORD dwLevel,DWORD dwFlags); + WINBASEAPI WINBOOL WINAPI GetProcessShutdownParameters(LPDWORD lpdwLevel,LPDWORD lpdwFlags); + WINBASEAPI DWORD WINAPI GetProcessVersion(DWORD ProcessId); + WINBASEAPI VOID WINAPI FatalAppExitA(UINT uAction,LPCSTR lpMessageText); + WINBASEAPI VOID WINAPI FatalAppExitW(UINT uAction,LPCWSTR lpMessageText); + WINBASEAPI VOID WINAPI GetStartupInfoA(LPSTARTUPINFOA lpStartupInfo); + WINBASEAPI VOID WINAPI GetStartupInfoW(LPSTARTUPINFOW lpStartupInfo); + WINBASEAPI LPSTR WINAPI GetCommandLineA(VOID); + WINBASEAPI LPWSTR WINAPI GetCommandLineW(VOID); + WINBASEAPI DWORD WINAPI GetEnvironmentVariableA(LPCSTR lpName,LPSTR lpBuffer,DWORD nSize); + WINBASEAPI DWORD WINAPI GetEnvironmentVariableW(LPCWSTR lpName,LPWSTR lpBuffer,DWORD nSize); + WINBASEAPI WINBOOL WINAPI SetEnvironmentVariableA(LPCSTR lpName,LPCSTR lpValue); + WINBASEAPI WINBOOL WINAPI SetEnvironmentVariableW(LPCWSTR lpName,LPCWSTR lpValue); + WINBASEAPI DWORD WINAPI ExpandEnvironmentStringsA(LPCSTR lpSrc,LPSTR lpDst,DWORD nSize); + WINBASEAPI DWORD WINAPI ExpandEnvironmentStringsW(LPCWSTR lpSrc,LPWSTR lpDst,DWORD nSize); + WINBASEAPI DWORD WINAPI GetFirmwareEnvironmentVariableA(LPCSTR lpName,LPCSTR lpGuid,PVOID pBuffer,DWORD nSize); + WINBASEAPI DWORD WINAPI GetFirmwareEnvironmentVariableW(LPCWSTR lpName,LPCWSTR lpGuid,PVOID pBuffer,DWORD nSize); + WINBASEAPI WINBOOL WINAPI SetFirmwareEnvironmentVariableA(LPCSTR lpName,LPCSTR lpGuid,PVOID pValue,DWORD nSize); + WINBASEAPI WINBOOL WINAPI SetFirmwareEnvironmentVariableW(LPCWSTR lpName,LPCWSTR lpGuid,PVOID pValue,DWORD nSize); + WINBASEAPI VOID WINAPI OutputDebugStringA(LPCSTR lpOutputString); + WINBASEAPI VOID WINAPI OutputDebugStringW(LPCWSTR lpOutputString); + WINBASEAPI HRSRC WINAPI FindResourceA(HMODULE hModule,LPCSTR lpName,LPCSTR lpType); + WINBASEAPI HRSRC WINAPI FindResourceW(HMODULE hModule,LPCWSTR lpName,LPCWSTR lpType); + WINBASEAPI HRSRC WINAPI FindResourceExA(HMODULE hModule,LPCSTR lpType,LPCSTR lpName,WORD wLanguage); + WINBASEAPI HRSRC WINAPI FindResourceExW(HMODULE hModule,LPCWSTR lpType,LPCWSTR lpName,WORD wLanguage); + +#ifdef UNICODE +#define ENUMRESTYPEPROC ENUMRESTYPEPROCW +#define ENUMRESNAMEPROC ENUMRESNAMEPROCW +#define ENUMRESLANGPROC ENUMRESLANGPROCW +#define EnumResourceTypes EnumResourceTypesW +#define EnumResourceNames EnumResourceNamesW +#define EnumResourceLanguages EnumResourceLanguagesW +#define BeginUpdateResource BeginUpdateResourceW +#define UpdateResource UpdateResourceW +#define EndUpdateResource EndUpdateResourceW +#define GlobalAddAtom GlobalAddAtomW +#define GlobalFindAtom GlobalFindAtomW +#define GlobalGetAtomName GlobalGetAtomNameW +#define AddAtom AddAtomW +#define FindAtom FindAtomW +#define GetAtomName GetAtomNameW +#define GetProfileInt GetProfileIntW +#define GetProfileString GetProfileStringW +#define WriteProfileString WriteProfileStringW +#define GetProfileSection GetProfileSectionW +#define WriteProfileSection WriteProfileSectionW +#define GetPrivateProfileInt GetPrivateProfileIntW +#define GetPrivateProfileString GetPrivateProfileStringW +#define WritePrivateProfileString WritePrivateProfileStringW +#define GetPrivateProfileSection GetPrivateProfileSectionW +#define WritePrivateProfileSection WritePrivateProfileSectionW +#define GetPrivateProfileSectionNames GetPrivateProfileSectionNamesW +#define GetPrivateProfileStruct GetPrivateProfileStructW +#define WritePrivateProfileStruct WritePrivateProfileStructW +#define GetDriveType GetDriveTypeW +#define GetSystemDirectory GetSystemDirectoryW +#define GetTempPath GetTempPathW +#define GetTempFileName GetTempFileNameW +#define GetWindowsDirectory GetWindowsDirectoryW +#define GetSystemWindowsDirectory GetSystemWindowsDirectoryW +#define AddLocalAlternateComputerName AddLocalAlternateComputerNameW +#else +#define ENUMRESTYPEPROC ENUMRESTYPEPROCA +#define ENUMRESNAMEPROC ENUMRESNAMEPROCA +#define ENUMRESLANGPROC ENUMRESLANGPROCA +#define EnumResourceTypes EnumResourceTypesA +#define EnumResourceNames EnumResourceNamesA +#define EnumResourceLanguages EnumResourceLanguagesA +#define BeginUpdateResource BeginUpdateResourceA +#define UpdateResource UpdateResourceA +#define EndUpdateResource EndUpdateResourceA +#define GlobalAddAtom GlobalAddAtomA +#define GlobalFindAtom GlobalFindAtomA +#define GlobalGetAtomName GlobalGetAtomNameA +#define AddAtom AddAtomA +#define FindAtom FindAtomA +#define GetAtomName GetAtomNameA +#define GetProfileInt GetProfileIntA +#define GetProfileString GetProfileStringA +#define WriteProfileString WriteProfileStringA +#define GetProfileSection GetProfileSectionA +#define WriteProfileSection WriteProfileSectionA +#define GetPrivateProfileInt GetPrivateProfileIntA +#define GetPrivateProfileString GetPrivateProfileStringA +#define WritePrivateProfileString WritePrivateProfileStringA +#define GetPrivateProfileSection GetPrivateProfileSectionA +#define WritePrivateProfileSection WritePrivateProfileSectionA +#define GetPrivateProfileSectionNames GetPrivateProfileSectionNamesA +#define GetPrivateProfileStruct GetPrivateProfileStructA +#define WritePrivateProfileStruct WritePrivateProfileStructA +#define GetDriveType GetDriveTypeA +#define GetSystemDirectory GetSystemDirectoryA +#define GetTempPath GetTempPathA +#define GetTempFileName GetTempFileNameA +#define GetWindowsDirectory GetWindowsDirectoryA +#define GetSystemWindowsDirectory GetSystemWindowsDirectoryA +#define AddLocalAlternateComputerName AddLocalAlternateComputerNameA +#endif + + typedef WINBOOL (CALLBACK *ENUMRESTYPEPROCA)(HMODULE hModule,LPSTR lpType,LONG_PTR lParam); + typedef WINBOOL (CALLBACK *ENUMRESTYPEPROCW)(HMODULE hModule,LPWSTR lpType,LONG_PTR lParam); + typedef WINBOOL (CALLBACK *ENUMRESNAMEPROCA)(HMODULE hModule,LPCSTR lpType,LPSTR lpName,LONG_PTR lParam); + typedef WINBOOL (CALLBACK *ENUMRESNAMEPROCW)(HMODULE hModule,LPCWSTR lpType,LPWSTR lpName,LONG_PTR lParam); + typedef WINBOOL (CALLBACK *ENUMRESLANGPROCA)(HMODULE hModule,LPCSTR lpType,LPCSTR lpName,WORD wLanguage,LONG_PTR lParam); + typedef WINBOOL (CALLBACK *ENUMRESLANGPROCW)(HMODULE hModule,LPCWSTR lpType,LPCWSTR lpName,WORD wLanguage,LONG_PTR lParam); + + WINBASEAPI WINBOOL WINAPI EnumResourceTypesA(HMODULE hModule,ENUMRESTYPEPROCA lpEnumFunc,LONG_PTR lParam); + WINBASEAPI WINBOOL WINAPI EnumResourceTypesW(HMODULE hModule,ENUMRESTYPEPROCW lpEnumFunc,LONG_PTR lParam); + WINBASEAPI WINBOOL WINAPI EnumResourceNamesA(HMODULE hModule,LPCSTR lpType,ENUMRESNAMEPROCA lpEnumFunc,LONG_PTR lParam); + WINBASEAPI WINBOOL WINAPI EnumResourceNamesW(HMODULE hModule,LPCWSTR lpType,ENUMRESNAMEPROCW lpEnumFunc,LONG_PTR lParam); + WINBASEAPI WINBOOL WINAPI EnumResourceLanguagesA(HMODULE hModule,LPCSTR lpType,LPCSTR lpName,ENUMRESLANGPROCA lpEnumFunc,LONG_PTR lParam); + WINBASEAPI WINBOOL WINAPI EnumResourceLanguagesW(HMODULE hModule,LPCWSTR lpType,LPCWSTR lpName,ENUMRESLANGPROCW lpEnumFunc,LONG_PTR lParam); + WINBASEAPI HANDLE WINAPI BeginUpdateResourceA(LPCSTR pFileName,WINBOOL bDeleteExistingResources); + WINBASEAPI HANDLE WINAPI BeginUpdateResourceW(LPCWSTR pFileName,WINBOOL bDeleteExistingResources); + WINBASEAPI WINBOOL WINAPI UpdateResourceA(HANDLE hUpdate,LPCSTR lpType,LPCSTR lpName,WORD wLanguage,LPVOID lpData,DWORD cb); + WINBASEAPI WINBOOL WINAPI UpdateResourceW(HANDLE hUpdate,LPCWSTR lpType,LPCWSTR lpName,WORD wLanguage,LPVOID lpData,DWORD cb); + WINBASEAPI WINBOOL WINAPI EndUpdateResourceA(HANDLE hUpdate,WINBOOL fDiscard); + WINBASEAPI WINBOOL WINAPI EndUpdateResourceW(HANDLE hUpdate,WINBOOL fDiscard); + WINBASEAPI ATOM WINAPI GlobalAddAtomA(LPCSTR lpString); + WINBASEAPI ATOM WINAPI GlobalAddAtomW(LPCWSTR lpString); + WINBASEAPI ATOM WINAPI GlobalFindAtomA(LPCSTR lpString); + WINBASEAPI ATOM WINAPI GlobalFindAtomW(LPCWSTR lpString); + WINBASEAPI UINT WINAPI GlobalGetAtomNameA(ATOM nAtom,LPSTR lpBuffer,int nSize); + WINBASEAPI UINT WINAPI GlobalGetAtomNameW(ATOM nAtom,LPWSTR lpBuffer,int nSize); + WINBASEAPI ATOM WINAPI AddAtomA(LPCSTR lpString); + WINBASEAPI ATOM WINAPI AddAtomW(LPCWSTR lpString); + WINBASEAPI ATOM WINAPI FindAtomA(LPCSTR lpString); + WINBASEAPI ATOM WINAPI FindAtomW(LPCWSTR lpString); + WINBASEAPI UINT WINAPI GetAtomNameA(ATOM nAtom,LPSTR lpBuffer,int nSize); + WINBASEAPI UINT WINAPI GetAtomNameW(ATOM nAtom,LPWSTR lpBuffer,int nSize); + WINBASEAPI UINT WINAPI GetProfileIntA(LPCSTR lpAppName,LPCSTR lpKeyName,INT nDefault); + WINBASEAPI UINT WINAPI GetProfileIntW(LPCWSTR lpAppName,LPCWSTR lpKeyName,INT nDefault); + WINBASEAPI DWORD WINAPI GetProfileStringA(LPCSTR lpAppName,LPCSTR lpKeyName,LPCSTR lpDefault,LPSTR lpReturnedString,DWORD nSize); + WINBASEAPI DWORD WINAPI GetProfileStringW(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpDefault,LPWSTR lpReturnedString,DWORD nSize); + WINBASEAPI WINBOOL WINAPI WriteProfileStringA(LPCSTR lpAppName,LPCSTR lpKeyName,LPCSTR lpString); + WINBASEAPI WINBOOL WINAPI WriteProfileStringW(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpString); + WINBASEAPI DWORD WINAPI GetProfileSectionA(LPCSTR lpAppName,LPSTR lpReturnedString,DWORD nSize); + WINBASEAPI DWORD WINAPI GetProfileSectionW(LPCWSTR lpAppName,LPWSTR lpReturnedString,DWORD nSize); + WINBASEAPI WINBOOL WINAPI WriteProfileSectionA(LPCSTR lpAppName,LPCSTR lpString); + WINBASEAPI WINBOOL WINAPI WriteProfileSectionW(LPCWSTR lpAppName,LPCWSTR lpString); + WINBASEAPI UINT WINAPI GetPrivateProfileIntA(LPCSTR lpAppName,LPCSTR lpKeyName,INT nDefault,LPCSTR lpFileName); + WINBASEAPI UINT WINAPI GetPrivateProfileIntW(LPCWSTR lpAppName,LPCWSTR lpKeyName,INT nDefault,LPCWSTR lpFileName); + WINBASEAPI DWORD WINAPI GetPrivateProfileStringA(LPCSTR lpAppName,LPCSTR lpKeyName,LPCSTR lpDefault,LPSTR lpReturnedString,DWORD nSize,LPCSTR lpFileName); + WINBASEAPI DWORD WINAPI GetPrivateProfileStringW(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpDefault,LPWSTR lpReturnedString,DWORD nSize,LPCWSTR lpFileName); + WINBASEAPI WINBOOL WINAPI WritePrivateProfileStringA(LPCSTR lpAppName,LPCSTR lpKeyName,LPCSTR lpString,LPCSTR lpFileName); + WINBASEAPI WINBOOL WINAPI WritePrivateProfileStringW(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpString,LPCWSTR lpFileName); + WINBASEAPI DWORD WINAPI GetPrivateProfileSectionA(LPCSTR lpAppName,LPSTR lpReturnedString,DWORD nSize,LPCSTR lpFileName); + WINBASEAPI DWORD WINAPI GetPrivateProfileSectionW(LPCWSTR lpAppName,LPWSTR lpReturnedString,DWORD nSize,LPCWSTR lpFileName); + WINBASEAPI WINBOOL WINAPI WritePrivateProfileSectionA(LPCSTR lpAppName,LPCSTR lpString,LPCSTR lpFileName); + WINBASEAPI WINBOOL WINAPI WritePrivateProfileSectionW(LPCWSTR lpAppName,LPCWSTR lpString,LPCWSTR lpFileName); + WINBASEAPI DWORD WINAPI GetPrivateProfileSectionNamesA(LPSTR lpszReturnBuffer,DWORD nSize,LPCSTR lpFileName); + WINBASEAPI DWORD WINAPI GetPrivateProfileSectionNamesW(LPWSTR lpszReturnBuffer,DWORD nSize,LPCWSTR lpFileName); + WINBASEAPI WINBOOL WINAPI GetPrivateProfileStructA(LPCSTR lpszSection,LPCSTR lpszKey,LPVOID lpStruct,UINT uSizeStruct,LPCSTR szFile); + WINBASEAPI WINBOOL WINAPI GetPrivateProfileStructW(LPCWSTR lpszSection,LPCWSTR lpszKey,LPVOID lpStruct,UINT uSizeStruct,LPCWSTR szFile); + WINBASEAPI WINBOOL WINAPI WritePrivateProfileStructA(LPCSTR lpszSection,LPCSTR lpszKey,LPVOID lpStruct,UINT uSizeStruct,LPCSTR szFile); + WINBASEAPI WINBOOL WINAPI WritePrivateProfileStructW(LPCWSTR lpszSection,LPCWSTR lpszKey,LPVOID lpStruct,UINT uSizeStruct,LPCWSTR szFile); + WINBASEAPI UINT WINAPI GetDriveTypeA(LPCSTR lpRootPathName); + WINBASEAPI UINT WINAPI GetDriveTypeW(LPCWSTR lpRootPathName); + WINBASEAPI UINT WINAPI GetSystemDirectoryA(LPSTR lpBuffer,UINT uSize); + WINBASEAPI UINT WINAPI GetSystemDirectoryW(LPWSTR lpBuffer,UINT uSize); + WINBASEAPI DWORD WINAPI GetTempPathA(DWORD nBufferLength,LPSTR lpBuffer); + WINBASEAPI DWORD WINAPI GetTempPathW(DWORD nBufferLength,LPWSTR lpBuffer); + WINBASEAPI UINT WINAPI GetTempFileNameA(LPCSTR lpPathName,LPCSTR lpPrefixString,UINT uUnique,LPSTR lpTempFileName); + WINBASEAPI UINT WINAPI GetTempFileNameW(LPCWSTR lpPathName,LPCWSTR lpPrefixString,UINT uUnique,LPWSTR lpTempFileName); + WINBASEAPI UINT WINAPI GetWindowsDirectoryA(LPSTR lpBuffer,UINT uSize); + WINBASEAPI UINT WINAPI GetWindowsDirectoryW(LPWSTR lpBuffer,UINT uSize); + WINBASEAPI UINT WINAPI GetSystemWindowsDirectoryA(LPSTR lpBuffer,UINT uSize); + WINBASEAPI UINT WINAPI GetSystemWindowsDirectoryW(LPWSTR lpBuffer,UINT uSize); + +#ifndef RC_INVOKED +#ifdef UNICODE +#define GetSystemWow64Directory GetSystemWow64DirectoryW +#else +#define GetSystemWow64Directory GetSystemWow64DirectoryA +#endif + + WINBASEAPI UINT WINAPI GetSystemWow64DirectoryA(LPSTR lpBuffer,UINT uSize); + WINBASEAPI UINT WINAPI GetSystemWow64DirectoryW(LPWSTR lpBuffer,UINT uSize); + WINBASEAPI BOOLEAN WINAPI Wow64EnableWow64FsRedirection(BOOLEAN Wow64FsEnableRedirection); + WINBASEAPI WINBOOL WINAPI Wow64DisableWow64FsRedirection(PVOID *OldValue); + WINBASEAPI WINBOOL WINAPI Wow64RevertWow64FsRedirection(PVOID OlValue); + + typedef UINT (WINAPI *PGET_SYSTEM_WOW64_DIRECTORY_A)(LPSTR lpBuffer,UINT uSize); + typedef UINT (WINAPI *PGET_SYSTEM_WOW64_DIRECTORY_W)(LPWSTR lpBuffer,UINT uSize); + +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_A_A "GetSystemWow64DirectoryA" +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_A_W L"GetSystemWow64DirectoryA" +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_A_T TEXT("GetSystemWow64DirectoryA") +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_W_A "GetSystemWow64DirectoryW" +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_W_W L"GetSystemWow64DirectoryW" +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_W_T TEXT("GetSystemWow64DirectoryW") + +#ifdef UNICODE +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_T_A GET_SYSTEM_WOW64_DIRECTORY_NAME_W_A +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_T_W GET_SYSTEM_WOW64_DIRECTORY_NAME_W_W +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_T_T GET_SYSTEM_WOW64_DIRECTORY_NAME_W_T +#else +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_T_A GET_SYSTEM_WOW64_DIRECTORY_NAME_A_A +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_T_W GET_SYSTEM_WOW64_DIRECTORY_NAME_A_W +#define GET_SYSTEM_WOW64_DIRECTORY_NAME_T_T GET_SYSTEM_WOW64_DIRECTORY_NAME_A_T +#endif +#endif + +#ifdef UNICODE +#define SetCurrentDirectory SetCurrentDirectoryW +#define GetCurrentDirectory GetCurrentDirectoryW +#define SetDllDirectory SetDllDirectoryW +#define GetDllDirectory GetDllDirectoryW +#define GetDiskFreeSpace GetDiskFreeSpaceW +#define GetDiskFreeSpaceEx GetDiskFreeSpaceExW +#define CreateDirectory CreateDirectoryW +#define CreateDirectoryEx CreateDirectoryExW +#define RemoveDirectory RemoveDirectoryW +#define GetFullPathName GetFullPathNameW +#define DefineDosDevice DefineDosDeviceW +#define QueryDosDevice QueryDosDeviceW +#define CreateFile CreateFileW +#define SetFileAttributes SetFileAttributesW +#define GetFileAttributes GetFileAttributesW +#else +#define SetCurrentDirectory SetCurrentDirectoryA +#define GetCurrentDirectory GetCurrentDirectoryA +#define SetDllDirectory SetDllDirectoryA +#define GetDllDirectory GetDllDirectoryA +#define GetDiskFreeSpace GetDiskFreeSpaceA +#define GetDiskFreeSpaceEx GetDiskFreeSpaceExA +#define CreateDirectory CreateDirectoryA +#define CreateDirectoryEx CreateDirectoryExA +#define RemoveDirectory RemoveDirectoryA +#define GetFullPathName GetFullPathNameA +#define DefineDosDevice DefineDosDeviceA +#define QueryDosDevice QueryDosDeviceA +#define CreateFile CreateFileA +#define SetFileAttributes SetFileAttributesA +#define GetFileAttributes GetFileAttributesA +#endif + + WINBASEAPI WINBOOL WINAPI SetCurrentDirectoryA(LPCSTR lpPathName); + WINBASEAPI WINBOOL WINAPI SetCurrentDirectoryW(LPCWSTR lpPathName); + WINBASEAPI DWORD WINAPI GetCurrentDirectoryA(DWORD nBufferLength,LPSTR lpBuffer); + WINBASEAPI DWORD WINAPI GetCurrentDirectoryW(DWORD nBufferLength,LPWSTR lpBuffer); + WINBASEAPI WINBOOL WINAPI SetDllDirectoryA(LPCSTR lpPathName); + WINBASEAPI WINBOOL WINAPI SetDllDirectoryW(LPCWSTR lpPathName); + WINBASEAPI DWORD WINAPI GetDllDirectoryA(DWORD nBufferLength,LPSTR lpBuffer); + WINBASEAPI DWORD WINAPI GetDllDirectoryW(DWORD nBufferLength,LPWSTR lpBuffer); + WINBASEAPI WINBOOL WINAPI GetDiskFreeSpaceA(LPCSTR lpRootPathName,LPDWORD lpSectorsPerCluster,LPDWORD lpBytesPerSector,LPDWORD lpNumberOfFreeClusters,LPDWORD lpTotalNumberOfClusters); + WINBASEAPI WINBOOL WINAPI GetDiskFreeSpaceW(LPCWSTR lpRootPathName,LPDWORD lpSectorsPerCluster,LPDWORD lpBytesPerSector,LPDWORD lpNumberOfFreeClusters,LPDWORD lpTotalNumberOfClusters); + WINBASEAPI WINBOOL WINAPI GetDiskFreeSpaceExA(LPCSTR lpDirectoryName,PULARGE_INTEGER lpFreeBytesAvailableToCaller,PULARGE_INTEGER lpTotalNumberOfBytes,PULARGE_INTEGER lpTotalNumberOfFreeBytes); + WINBASEAPI WINBOOL WINAPI GetDiskFreeSpaceExW(LPCWSTR lpDirectoryName,PULARGE_INTEGER lpFreeBytesAvailableToCaller,PULARGE_INTEGER lpTotalNumberOfBytes,PULARGE_INTEGER lpTotalNumberOfFreeBytes); + WINBASEAPI WINBOOL WINAPI CreateDirectoryA(LPCSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI CreateDirectoryW(LPCWSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI CreateDirectoryExA(LPCSTR lpTemplateDirectory,LPCSTR lpNewDirectory,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI CreateDirectoryExW(LPCWSTR lpTemplateDirectory,LPCWSTR lpNewDirectory,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI RemoveDirectoryA(LPCSTR lpPathName); + WINBASEAPI WINBOOL WINAPI RemoveDirectoryW(LPCWSTR lpPathName); + WINBASEAPI DWORD WINAPI GetFullPathNameA(LPCSTR lpFileName,DWORD nBufferLength,LPSTR lpBuffer,LPSTR *lpFilePart); + WINBASEAPI DWORD WINAPI GetFullPathNameW(LPCWSTR lpFileName,DWORD nBufferLength,LPWSTR lpBuffer,LPWSTR *lpFilePart); + +#define DDD_RAW_TARGET_PATH 0x1 +#define DDD_REMOVE_DEFINITION 0x2 +#define DDD_EXACT_MATCH_ON_REMOVE 0x4 +#define DDD_NO_BROADCAST_SYSTEM 0x8 +#define DDD_LUID_BROADCAST_DRIVE 0x10 + + WINBASEAPI WINBOOL WINAPI DefineDosDeviceA(DWORD dwFlags,LPCSTR lpDeviceName,LPCSTR lpTargetPath); + WINBASEAPI WINBOOL WINAPI DefineDosDeviceW(DWORD dwFlags,LPCWSTR lpDeviceName,LPCWSTR lpTargetPath); + WINBASEAPI DWORD WINAPI QueryDosDeviceA(LPCSTR lpDeviceName,LPSTR lpTargetPath,DWORD ucchMax); + WINBASEAPI DWORD WINAPI QueryDosDeviceW(LPCWSTR lpDeviceName,LPWSTR lpTargetPath,DWORD ucchMax); + +#define EXPAND_LOCAL_DRIVES + + WINBASEAPI HANDLE WINAPI CreateFileA(LPCSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile); + WINBASEAPI HANDLE WINAPI CreateFileW(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile); + WINBASEAPI HANDLE WINAPI ReOpenFile(HANDLE hOriginalFile,DWORD dwDesiredAccess,DWORD dwShareMode,DWORD dwFlagsAndAttributes); + WINBASEAPI WINBOOL WINAPI SetFileAttributesA(LPCSTR lpFileName,DWORD dwFileAttributes); + WINBASEAPI WINBOOL WINAPI SetFileAttributesW(LPCWSTR lpFileName,DWORD dwFileAttributes); + WINBASEAPI DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName); + WINBASEAPI DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName); + + typedef enum _GET_FILEEX_INFO_LEVELS { + GetFileExInfoStandard,GetFileExMaxInfoLevel + } GET_FILEEX_INFO_LEVELS; + +#ifdef UNICODE +#define GetFileAttributesEx GetFileAttributesExW +#define GetCompressedFileSize GetCompressedFileSizeW +#define DeleteFile DeleteFileW +#define CheckNameLegalDOS8Dot3 CheckNameLegalDOS8Dot3W +#else +#define GetFileAttributesEx GetFileAttributesExA +#define GetCompressedFileSize GetCompressedFileSizeA +#define DeleteFile DeleteFileA +#define CheckNameLegalDOS8Dot3 CheckNameLegalDOS8Dot3A +#endif + + WINBASEAPI WINBOOL WINAPI GetFileAttributesExA(LPCSTR lpFileName,GET_FILEEX_INFO_LEVELS fInfoLevelId,LPVOID lpFileInformation); + WINBASEAPI WINBOOL WINAPI GetFileAttributesExW(LPCWSTR lpFileName,GET_FILEEX_INFO_LEVELS fInfoLevelId,LPVOID lpFileInformation); + WINBASEAPI DWORD WINAPI GetCompressedFileSizeA(LPCSTR lpFileName,LPDWORD lpFileSizeHigh); + WINBASEAPI DWORD WINAPI GetCompressedFileSizeW(LPCWSTR lpFileName,LPDWORD lpFileSizeHigh); + WINBASEAPI WINBOOL WINAPI DeleteFileA(LPCSTR lpFileName); + WINBASEAPI WINBOOL WINAPI DeleteFileW(LPCWSTR lpFileName); + WINBASEAPI WINBOOL WINAPI CheckNameLegalDOS8Dot3A(LPCSTR lpName,LPSTR lpOemName,DWORD OemNameSize,PBOOL pbNameContainsSpaces,PBOOL pbNameLegal); + WINBASEAPI WINBOOL WINAPI CheckNameLegalDOS8Dot3W(LPCWSTR lpName,LPSTR lpOemName,DWORD OemNameSize,PBOOL pbNameContainsSpaces,PBOOL pbNameLegal); + + typedef enum _FINDEX_INFO_LEVELS { + FindExInfoStandard,FindExInfoMaxInfoLevel + } FINDEX_INFO_LEVELS; + + typedef enum _FINDEX_SEARCH_OPS { + FindExSearchNameMatch,FindExSearchLimitToDirectories,FindExSearchLimitToDevices,FindExSearchMaxSearchOp + } FINDEX_SEARCH_OPS; + +#define FIND_FIRST_EX_CASE_SENSITIVE 0x1 + +#ifdef UNICODE +#define FindFirstFileEx FindFirstFileExW +#define FindFirstFile FindFirstFileW +#define FindNextFile FindNextFileW +#define SearchPath SearchPathW +#define CopyFile CopyFileW +#define CopyFileEx CopyFileExW +#define MoveFile MoveFileW +#define MoveFileEx MoveFileExW +#define MoveFileWithProgress MoveFileWithProgressW +#define ReplaceFile ReplaceFileW +#define CreateHardLink CreateHardLinkW +#define CreateNamedPipe CreateNamedPipeW +#define GetNamedPipeHandleState GetNamedPipeHandleStateW +#define CallNamedPipe CallNamedPipeW +#define WaitNamedPipe WaitNamedPipeW +#define SetVolumeLabel SetVolumeLabelW +#define GetVolumeInformation GetVolumeInformationW +#define ClearEventLog ClearEventLogW +#define BackupEventLog BackupEventLogW +#define OpenEventLog OpenEventLogW +#define RegisterEventSource RegisterEventSourceW +#define OpenBackupEventLog OpenBackupEventLogW +#define ReadEventLog ReadEventLogW +#define ReportEvent ReportEventW +#define AccessCheckAndAuditAlarm AccessCheckAndAuditAlarmW +#define AccessCheckByTypeAndAuditAlarm AccessCheckByTypeAndAuditAlarmW +#define AccessCheckByTypeResultListAndAuditAlarm AccessCheckByTypeResultListAndAuditAlarmW +#define AccessCheckByTypeResultListAndAuditAlarmByHandle AccessCheckByTypeResultListAndAuditAlarmByHandleW +#define ObjectOpenAuditAlarm ObjectOpenAuditAlarmW +#define ObjectPrivilegeAuditAlarm ObjectPrivilegeAuditAlarmW +#define ObjectCloseAuditAlarm ObjectCloseAuditAlarmW +#define ObjectDeleteAuditAlarm ObjectDeleteAuditAlarmW +#define PrivilegedServiceAuditAlarm PrivilegedServiceAuditAlarmW +#define SetFileSecurity SetFileSecurityW +#define GetFileSecurity GetFileSecurityW +#define FindFirstChangeNotification FindFirstChangeNotificationW +#define IsBadStringPtr IsBadStringPtrW +#define LookupAccountSid LookupAccountSidW +#define LookupAccountName LookupAccountNameW +#define LookupPrivilegeValue LookupPrivilegeValueW +#define LookupPrivilegeName LookupPrivilegeNameW +#define LookupPrivilegeDisplayName LookupPrivilegeDisplayNameW +#define BuildCommDCB BuildCommDCBW +#define BuildCommDCBAndTimeouts BuildCommDCBAndTimeoutsW +#define CommConfigDialog CommConfigDialogW +#define GetDefaultCommConfig GetDefaultCommConfigW +#define SetDefaultCommConfig SetDefaultCommConfigW +#define GetComputerName GetComputerNameW +#define SetComputerName SetComputerNameW +#define GetComputerNameEx GetComputerNameExW +#define SetComputerNameEx SetComputerNameExW +#define DnsHostnameToComputerName DnsHostnameToComputerNameW +#define GetUserName GetUserNameW +#else +#define FindFirstFileEx FindFirstFileExA +#define FindFirstFile FindFirstFileA +#define FindNextFile FindNextFileA +#define SearchPath SearchPathA +#define CopyFile CopyFileA +#define CopyFileEx CopyFileExA +#define MoveFile MoveFileA +#define MoveFileEx MoveFileExA +#define MoveFileWithProgress MoveFileWithProgressA +#define ReplaceFile ReplaceFileA +#define CreateHardLink CreateHardLinkA +#define CreateNamedPipe CreateNamedPipeA +#define GetNamedPipeHandleState GetNamedPipeHandleStateA +#define CallNamedPipe CallNamedPipeA +#define WaitNamedPipe WaitNamedPipeA +#define SetVolumeLabel SetVolumeLabelA +#define GetVolumeInformation GetVolumeInformationA +#define ClearEventLog ClearEventLogA +#define BackupEventLog BackupEventLogA +#define OpenEventLog OpenEventLogA +#define RegisterEventSource RegisterEventSourceA +#define OpenBackupEventLog OpenBackupEventLogA +#define ReadEventLog ReadEventLogA +#define ReportEvent ReportEventA +#define AccessCheckAndAuditAlarm AccessCheckAndAuditAlarmA +#define AccessCheckByTypeAndAuditAlarm AccessCheckByTypeAndAuditAlarmA +#define AccessCheckByTypeResultListAndAuditAlarm AccessCheckByTypeResultListAndAuditAlarmA +#define AccessCheckByTypeResultListAndAuditAlarmByHandle AccessCheckByTypeResultListAndAuditAlarmByHandleA +#define ObjectOpenAuditAlarm ObjectOpenAuditAlarmA +#define ObjectPrivilegeAuditAlarm ObjectPrivilegeAuditAlarmA +#define ObjectCloseAuditAlarm ObjectCloseAuditAlarmA +#define ObjectDeleteAuditAlarm ObjectDeleteAuditAlarmA +#define PrivilegedServiceAuditAlarm PrivilegedServiceAuditAlarmA +#define SetFileSecurity SetFileSecurityA +#define GetFileSecurity GetFileSecurityA +#define FindFirstChangeNotification FindFirstChangeNotificationA +#define IsBadStringPtr IsBadStringPtrA +#define LookupAccountSid LookupAccountSidA +#define LookupAccountName LookupAccountNameA +#define LookupPrivilegeValue LookupPrivilegeValueA +#define LookupPrivilegeName LookupPrivilegeNameA +#define LookupPrivilegeDisplayName LookupPrivilegeDisplayNameA +#define BuildCommDCB BuildCommDCBA +#define BuildCommDCBAndTimeouts BuildCommDCBAndTimeoutsA +#define CommConfigDialog CommConfigDialogA +#define GetDefaultCommConfig GetDefaultCommConfigA +#define SetDefaultCommConfig SetDefaultCommConfigA +#define GetComputerName GetComputerNameA +#define SetComputerName SetComputerNameA +#define GetComputerNameEx GetComputerNameExA +#define SetComputerNameEx SetComputerNameExA +#define DnsHostnameToComputerName DnsHostnameToComputerNameA +#define GetUserName GetUserNameA +#endif + + WINBASEAPI HANDLE WINAPI FindFirstFileExA(LPCSTR lpFileName,FINDEX_INFO_LEVELS fInfoLevelId,LPVOID lpFindFileData,FINDEX_SEARCH_OPS fSearchOp,LPVOID lpSearchFilter,DWORD dwAdditionalFlags); + WINBASEAPI HANDLE WINAPI FindFirstFileExW(LPCWSTR lpFileName,FINDEX_INFO_LEVELS fInfoLevelId,LPVOID lpFindFileData,FINDEX_SEARCH_OPS fSearchOp,LPVOID lpSearchFilter,DWORD dwAdditionalFlags); + WINBASEAPI HANDLE WINAPI FindFirstFileA(LPCSTR lpFileName,LPWIN32_FIND_DATAA lpFindFileData); + WINBASEAPI HANDLE WINAPI FindFirstFileW(LPCWSTR lpFileName,LPWIN32_FIND_DATAW lpFindFileData); + WINBASEAPI WINBOOL WINAPI FindNextFileA(HANDLE hFindFile,LPWIN32_FIND_DATAA lpFindFileData); + WINBASEAPI WINBOOL WINAPI FindNextFileW(HANDLE hFindFile,LPWIN32_FIND_DATAW lpFindFileData); + WINBASEAPI DWORD WINAPI SearchPathA(LPCSTR lpPath,LPCSTR lpFileName,LPCSTR lpExtension,DWORD nBufferLength,LPSTR lpBuffer,LPSTR *lpFilePart); + WINBASEAPI DWORD WINAPI SearchPathW(LPCWSTR lpPath,LPCWSTR lpFileName,LPCWSTR lpExtension,DWORD nBufferLength,LPWSTR lpBuffer,LPWSTR *lpFilePart); + WINBASEAPI WINBOOL WINAPI CopyFileA(LPCSTR lpExistingFileName,LPCSTR lpNewFileName,WINBOOL bFailIfExists); + WINBASEAPI WINBOOL WINAPI CopyFileW(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,WINBOOL bFailIfExists); + + typedef DWORD (WINAPI *LPPROGRESS_ROUTINE)(LARGE_INTEGER TotalFileSize,LARGE_INTEGER TotalBytesTransferred,LARGE_INTEGER StreamSize,LARGE_INTEGER StreamBytesTransferred,DWORD dwStreamNumber,DWORD dwCallbackReason,HANDLE hSourceFile,HANDLE hDestinationFile,LPVOID lpData); + + WINBASEAPI WINBOOL WINAPI CopyFileExA(LPCSTR lpExistingFileName,LPCSTR lpNewFileName,LPPROGRESS_ROUTINE lpProgressRoutine,LPVOID lpData,LPBOOL pbCancel,DWORD dwCopyFlags); + WINBASEAPI WINBOOL WINAPI CopyFileExW(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,LPPROGRESS_ROUTINE lpProgressRoutine,LPVOID lpData,LPBOOL pbCancel,DWORD dwCopyFlags); + WINBASEAPI WINBOOL WINAPI MoveFileA(LPCSTR lpExistingFileName,LPCSTR lpNewFileName); + WINBASEAPI WINBOOL WINAPI MoveFileW(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName); + WINBASEAPI WINBOOL WINAPI MoveFileExA(LPCSTR lpExistingFileName,LPCSTR lpNewFileName,DWORD dwFlags); + WINBASEAPI WINBOOL WINAPI MoveFileExW(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,DWORD dwFlags); + WINBASEAPI WINBOOL WINAPI MoveFileWithProgressA(LPCSTR lpExistingFileName,LPCSTR lpNewFileName,LPPROGRESS_ROUTINE lpProgressRoutine,LPVOID lpData,DWORD dwFlags); + WINBASEAPI WINBOOL WINAPI MoveFileWithProgressW(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,LPPROGRESS_ROUTINE lpProgressRoutine,LPVOID lpData,DWORD dwFlags); + +#define MOVEFILE_REPLACE_EXISTING 0x1 +#define MOVEFILE_COPY_ALLOWED 0x2 +#define MOVEFILE_DELAY_UNTIL_REBOOT 0x4 +#define MOVEFILE_WRITE_THROUGH 0x8 +#define MOVEFILE_CREATE_HARDLINK 0x10 +#define MOVEFILE_FAIL_IF_NOT_TRACKABLE 0x20 + + WINBASEAPI WINBOOL WINAPI ReplaceFileA(LPCSTR lpReplacedFileName,LPCSTR lpReplacementFileName,LPCSTR lpBackupFileName,DWORD dwReplaceFlags,LPVOID lpExclude,LPVOID lpReserved); + WINBASEAPI WINBOOL WINAPI ReplaceFileW(LPCWSTR lpReplacedFileName,LPCWSTR lpReplacementFileName,LPCWSTR lpBackupFileName,DWORD dwReplaceFlags,LPVOID lpExclude,LPVOID lpReserved); + WINBASEAPI WINBOOL WINAPI CreateHardLinkA(LPCSTR lpFileName,LPCSTR lpExistingFileName,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName,LPCWSTR lpExistingFileName,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + + typedef enum _STREAM_INFO_LEVELS { + FindStreamInfoStandard,FindStreamInfoMaxInfoLevel + } STREAM_INFO_LEVELS; + + typedef struct _WIN32_FIND_STREAM_DATA { + LARGE_INTEGER StreamSize; + WCHAR cStreamName[MAX_PATH + 36]; + } WIN32_FIND_STREAM_DATA,*PWIN32_FIND_STREAM_DATA; + + HANDLE WINAPI FindFirstStreamW(LPCWSTR lpFileName,STREAM_INFO_LEVELS InfoLevel,LPVOID lpFindStreamData,DWORD dwFlags); + WINBOOL WINAPI FindNextStreamW(HANDLE hFindStream,LPVOID lpFindStreamData); + WINBASEAPI HANDLE WINAPI CreateNamedPipeA(LPCSTR lpName,DWORD dwOpenMode,DWORD dwPipeMode,DWORD nMaxInstances,DWORD nOutBufferSize,DWORD nInBufferSize,DWORD nDefaultTimeOut,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI HANDLE WINAPI CreateNamedPipeW(LPCWSTR lpName,DWORD dwOpenMode,DWORD dwPipeMode,DWORD nMaxInstances,DWORD nOutBufferSize,DWORD nInBufferSize,DWORD nDefaultTimeOut,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINBASEAPI WINBOOL WINAPI GetNamedPipeHandleStateA(HANDLE hNamedPipe,LPDWORD lpState,LPDWORD lpCurInstances,LPDWORD lpMaxCollectionCount,LPDWORD lpCollectDataTimeout,LPSTR lpUserName,DWORD nMaxUserNameSize); + WINBASEAPI WINBOOL WINAPI GetNamedPipeHandleStateW(HANDLE hNamedPipe,LPDWORD lpState,LPDWORD lpCurInstances,LPDWORD lpMaxCollectionCount,LPDWORD lpCollectDataTimeout,LPWSTR lpUserName,DWORD nMaxUserNameSize); + WINBASEAPI WINBOOL WINAPI CallNamedPipeA(LPCSTR lpNamedPipeName,LPVOID lpInBuffer,DWORD nInBufferSize,LPVOID lpOutBuffer,DWORD nOutBufferSize,LPDWORD lpBytesRead,DWORD nTimeOut); + WINBASEAPI WINBOOL WINAPI CallNamedPipeW(LPCWSTR lpNamedPipeName,LPVOID lpInBuffer,DWORD nInBufferSize,LPVOID lpOutBuffer,DWORD nOutBufferSize,LPDWORD lpBytesRead,DWORD nTimeOut); + WINBASEAPI WINBOOL WINAPI WaitNamedPipeA(LPCSTR lpNamedPipeName,DWORD nTimeOut); + WINBASEAPI WINBOOL WINAPI WaitNamedPipeW(LPCWSTR lpNamedPipeName,DWORD nTimeOut); + WINBASEAPI WINBOOL WINAPI SetVolumeLabelA(LPCSTR lpRootPathName,LPCSTR lpVolumeName); + WINBASEAPI WINBOOL WINAPI SetVolumeLabelW(LPCWSTR lpRootPathName,LPCWSTR lpVolumeName); + WINBASEAPI VOID WINAPI SetFileApisToOEM(VOID); + WINBASEAPI VOID WINAPI SetFileApisToANSI(VOID); + WINBASEAPI WINBOOL WINAPI AreFileApisANSI(VOID); + WINBASEAPI WINBOOL WINAPI GetVolumeInformationA(LPCSTR lpRootPathName,LPSTR lpVolumeNameBuffer,DWORD nVolumeNameSize,LPDWORD lpVolumeSerialNumber,LPDWORD lpMaximumComponentLength,LPDWORD lpFileSystemFlags,LPSTR lpFileSystemNameBuffer,DWORD nFileSystemNameSize); + WINBASEAPI WINBOOL WINAPI GetVolumeInformationW(LPCWSTR lpRootPathName,LPWSTR lpVolumeNameBuffer,DWORD nVolumeNameSize,LPDWORD lpVolumeSerialNumber,LPDWORD lpMaximumComponentLength,LPDWORD lpFileSystemFlags,LPWSTR lpFileSystemNameBuffer,DWORD nFileSystemNameSize); + WINBASEAPI WINBOOL WINAPI CancelIo(HANDLE hFile); + WINADVAPI WINBOOL WINAPI ClearEventLogA(HANDLE hEventLog,LPCSTR lpBackupFileName); + WINADVAPI WINBOOL WINAPI ClearEventLogW(HANDLE hEventLog,LPCWSTR lpBackupFileName); + WINADVAPI WINBOOL WINAPI BackupEventLogA(HANDLE hEventLog,LPCSTR lpBackupFileName); + WINADVAPI WINBOOL WINAPI BackupEventLogW(HANDLE hEventLog,LPCWSTR lpBackupFileName); + WINADVAPI WINBOOL WINAPI CloseEventLog(HANDLE hEventLog); + WINADVAPI WINBOOL WINAPI DeregisterEventSource(HANDLE hEventLog); + WINADVAPI WINBOOL WINAPI NotifyChangeEventLog(HANDLE hEventLog,HANDLE hEvent); + WINADVAPI WINBOOL WINAPI GetNumberOfEventLogRecords(HANDLE hEventLog,PDWORD NumberOfRecords); + WINADVAPI WINBOOL WINAPI GetOldestEventLogRecord(HANDLE hEventLog,PDWORD OldestRecord); + WINADVAPI HANDLE WINAPI OpenEventLogA(LPCSTR lpUNCServerName,LPCSTR lpSourceName); + WINADVAPI HANDLE WINAPI OpenEventLogW(LPCWSTR lpUNCServerName,LPCWSTR lpSourceName); + WINADVAPI HANDLE WINAPI RegisterEventSourceA(LPCSTR lpUNCServerName,LPCSTR lpSourceName); + WINADVAPI HANDLE WINAPI RegisterEventSourceW(LPCWSTR lpUNCServerName,LPCWSTR lpSourceName); + WINADVAPI HANDLE WINAPI OpenBackupEventLogA(LPCSTR lpUNCServerName,LPCSTR lpFileName); + WINADVAPI HANDLE WINAPI OpenBackupEventLogW(LPCWSTR lpUNCServerName,LPCWSTR lpFileName); + WINADVAPI WINBOOL WINAPI ReadEventLogA(HANDLE hEventLog,DWORD dwReadFlags,DWORD dwRecordOffset,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,DWORD *pnBytesRead,DWORD *pnMinNumberOfBytesNeeded); + WINADVAPI WINBOOL WINAPI ReadEventLogW(HANDLE hEventLog,DWORD dwReadFlags,DWORD dwRecordOffset,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,DWORD *pnBytesRead,DWORD *pnMinNumberOfBytesNeeded); + WINADVAPI WINBOOL WINAPI ReportEventA(HANDLE hEventLog,WORD wType,WORD wCategory,DWORD dwEventID,PSID lpUserSid,WORD wNumStrings,DWORD dwDataSize,LPCSTR *lpStrings,LPVOID lpRawData); + WINADVAPI WINBOOL WINAPI ReportEventW(HANDLE hEventLog,WORD wType,WORD wCategory,DWORD dwEventID,PSID lpUserSid,WORD wNumStrings,DWORD dwDataSize,LPCWSTR *lpStrings,LPVOID lpRawData); + +#define EVENTLOG_FULL_INFO 0 + + typedef struct _EVENTLOG_FULL_INFORMATION { + DWORD dwFull; + } EVENTLOG_FULL_INFORMATION,*LPEVENTLOG_FULL_INFORMATION; + + WINADVAPI WINBOOL WINAPI GetEventLogInformation(HANDLE hEventLog,DWORD dwInfoLevel,LPVOID lpBuffer,DWORD cbBufSize,LPDWORD pcbBytesNeeded); + WINADVAPI WINBOOL WINAPI DuplicateToken(HANDLE ExistingTokenHandle,SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,PHANDLE DuplicateTokenHandle); + WINADVAPI WINBOOL WINAPI GetKernelObjectSecurity(HANDLE Handle,SECURITY_INFORMATION RequestedInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor,DWORD nLength,LPDWORD lpnLengthNeeded); + WINADVAPI WINBOOL WINAPI ImpersonateNamedPipeClient(HANDLE hNamedPipe); + WINADVAPI WINBOOL WINAPI ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel); + WINADVAPI WINBOOL WINAPI RevertToSelf(VOID); + WINADVAPI WINBOOL WINAPI SetThreadToken (PHANDLE Thread,HANDLE Token); + WINADVAPI WINBOOL WINAPI AccessCheck(PSECURITY_DESCRIPTOR pSecurityDescriptor,HANDLE ClientToken,DWORD DesiredAccess,PGENERIC_MAPPING GenericMapping,PPRIVILEGE_SET PrivilegeSet,LPDWORD PrivilegeSetLength,LPDWORD GrantedAccess,LPBOOL AccessStatus); + WINADVAPI WINBOOL WINAPI AccessCheckByType(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSID PrincipalSelfSid,HANDLE ClientToken,DWORD DesiredAccess,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,PPRIVILEGE_SET PrivilegeSet,LPDWORD PrivilegeSetLength,LPDWORD GrantedAccess,LPBOOL AccessStatus); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeResultList(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSID PrincipalSelfSid,HANDLE ClientToken,DWORD DesiredAccess,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,PPRIVILEGE_SET PrivilegeSet,LPDWORD PrivilegeSetLength,LPDWORD GrantedAccessList,LPDWORD AccessStatusList); + WINADVAPI WINBOOL WINAPI OpenProcessToken(HANDLE ProcessHandle,DWORD DesiredAccess,PHANDLE TokenHandle); + WINADVAPI WINBOOL WINAPI OpenThreadToken(HANDLE ThreadHandle,DWORD DesiredAccess,WINBOOL OpenAsSelf,PHANDLE TokenHandle); + WINADVAPI WINBOOL WINAPI GetTokenInformation(HANDLE TokenHandle,TOKEN_INFORMATION_CLASS TokenInformationClass,LPVOID TokenInformation,DWORD TokenInformationLength,PDWORD ReturnLength); + WINADVAPI WINBOOL WINAPI SetTokenInformation(HANDLE TokenHandle,TOKEN_INFORMATION_CLASS TokenInformationClass,LPVOID TokenInformation,DWORD TokenInformationLength); + WINADVAPI WINBOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle,WINBOOL DisableAllPrivileges,PTOKEN_PRIVILEGES NewState,DWORD BufferLength,PTOKEN_PRIVILEGES PreviousState,PDWORD ReturnLength); + WINADVAPI WINBOOL WINAPI AdjustTokenGroups(HANDLE TokenHandle,WINBOOL ResetToDefault,PTOKEN_GROUPS NewState,DWORD BufferLength,PTOKEN_GROUPS PreviousState,PDWORD ReturnLength); + WINADVAPI WINBOOL WINAPI PrivilegeCheck(HANDLE ClientToken,PPRIVILEGE_SET RequiredPrivileges,LPBOOL pfResult); + WINADVAPI WINBOOL WINAPI AccessCheckAndAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,LPSTR ObjectTypeName,LPSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,DWORD DesiredAccess,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPBOOL AccessStatus,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckAndAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,LPWSTR ObjectTypeName,LPWSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,DWORD DesiredAccess,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPBOOL AccessStatus,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeAndAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,LPCSTR ObjectTypeName,LPCSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,PSID PrincipalSelfSid,DWORD DesiredAccess,AUDIT_EVENT_TYPE AuditType,DWORD Flags,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPBOOL AccessStatus,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeAndAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,LPCWSTR ObjectTypeName,LPCWSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,PSID PrincipalSelfSid,DWORD DesiredAccess,AUDIT_EVENT_TYPE AuditType,DWORD Flags,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPBOOL AccessStatus,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeResultListAndAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,LPCSTR ObjectTypeName,LPCSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,PSID PrincipalSelfSid,DWORD DesiredAccess,AUDIT_EVENT_TYPE AuditType,DWORD Flags,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPDWORD AccessStatusList,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeResultListAndAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,LPCWSTR ObjectTypeName,LPCWSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,PSID PrincipalSelfSid,DWORD DesiredAccess,AUDIT_EVENT_TYPE AuditType,DWORD Flags,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPDWORD AccessStatusList,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeResultListAndAuditAlarmByHandleA(LPCSTR SubsystemName,LPVOID HandleId,HANDLE ClientToken,LPCSTR ObjectTypeName,LPCSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,PSID PrincipalSelfSid,DWORD DesiredAccess,AUDIT_EVENT_TYPE AuditType,DWORD Flags,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPDWORD AccessStatusList,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI AccessCheckByTypeResultListAndAuditAlarmByHandleW(LPCWSTR SubsystemName,LPVOID HandleId,HANDLE ClientToken,LPCWSTR ObjectTypeName,LPCWSTR ObjectName,PSECURITY_DESCRIPTOR SecurityDescriptor,PSID PrincipalSelfSid,DWORD DesiredAccess,AUDIT_EVENT_TYPE AuditType,DWORD Flags,POBJECT_TYPE_LIST ObjectTypeList,DWORD ObjectTypeListLength,PGENERIC_MAPPING GenericMapping,WINBOOL ObjectCreation,LPDWORD GrantedAccess,LPDWORD AccessStatusList,LPBOOL pfGenerateOnClose); + WINADVAPI WINBOOL WINAPI ObjectOpenAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,LPSTR ObjectTypeName,LPSTR ObjectName,PSECURITY_DESCRIPTOR pSecurityDescriptor,HANDLE ClientToken,DWORD DesiredAccess,DWORD GrantedAccess,PPRIVILEGE_SET Privileges,WINBOOL ObjectCreation,WINBOOL AccessGranted,LPBOOL GenerateOnClose); + WINADVAPI WINBOOL WINAPI ObjectOpenAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,LPWSTR ObjectTypeName,LPWSTR ObjectName,PSECURITY_DESCRIPTOR pSecurityDescriptor,HANDLE ClientToken,DWORD DesiredAccess,DWORD GrantedAccess,PPRIVILEGE_SET Privileges,WINBOOL ObjectCreation,WINBOOL AccessGranted,LPBOOL GenerateOnClose); + WINADVAPI WINBOOL WINAPI ObjectPrivilegeAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,HANDLE ClientToken,DWORD DesiredAccess,PPRIVILEGE_SET Privileges,WINBOOL AccessGranted); + WINADVAPI WINBOOL WINAPI ObjectPrivilegeAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,HANDLE ClientToken,DWORD DesiredAccess,PPRIVILEGE_SET Privileges,WINBOOL AccessGranted); + WINADVAPI WINBOOL WINAPI ObjectCloseAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,WINBOOL GenerateOnClose); + WINADVAPI WINBOOL WINAPI ObjectCloseAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,WINBOOL GenerateOnClose); + WINADVAPI WINBOOL WINAPI ObjectDeleteAuditAlarmA(LPCSTR SubsystemName,LPVOID HandleId,WINBOOL GenerateOnClose); + WINADVAPI WINBOOL WINAPI ObjectDeleteAuditAlarmW(LPCWSTR SubsystemName,LPVOID HandleId,WINBOOL GenerateOnClose); + WINADVAPI WINBOOL WINAPI PrivilegedServiceAuditAlarmA(LPCSTR SubsystemName,LPCSTR ServiceName,HANDLE ClientToken,PPRIVILEGE_SET Privileges,WINBOOL AccessGranted); + WINADVAPI WINBOOL WINAPI PrivilegedServiceAuditAlarmW(LPCWSTR SubsystemName,LPCWSTR ServiceName,HANDLE ClientToken,PPRIVILEGE_SET Privileges,WINBOOL AccessGranted); + WINADVAPI WINBOOL WINAPI IsWellKnownSid(PSID pSid,WELL_KNOWN_SID_TYPE WellKnownSidType); + WINADVAPI WINBOOL WINAPI CreateWellKnownSid(WELL_KNOWN_SID_TYPE WellKnownSidType,PSID DomainSid,PSID pSid,DWORD *cbSid); + WINADVAPI WINBOOL WINAPI EqualDomainSid(PSID pSid1,PSID pSid2,WINBOOL *pfEqual); + WINADVAPI WINBOOL WINAPI GetWindowsAccountDomainSid(PSID pSid,PSID pDomainSid,DWORD *cbDomainSid); + WINADVAPI WINBOOL WINAPI IsValidSid(PSID pSid); + WINADVAPI WINBOOL WINAPI EqualSid(PSID pSid1,PSID pSid2); + WINADVAPI WINBOOL WINAPI EqualPrefixSid(PSID pSid1,PSID pSid2); + WINADVAPI DWORD WINAPI GetSidLengthRequired (UCHAR nSubAuthorityCount); + WINADVAPI WINBOOL WINAPI AllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,BYTE nSubAuthorityCount,DWORD nSubAuthority0,DWORD nSubAuthority1,DWORD nSubAuthority2,DWORD nSubAuthority3,DWORD nSubAuthority4,DWORD nSubAuthority5,DWORD nSubAuthority6,DWORD nSubAuthority7,PSID *pSid); + WINADVAPI PVOID WINAPI FreeSid(PSID pSid); + WINADVAPI WINBOOL WINAPI InitializeSid(PSID Sid,PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,BYTE nSubAuthorityCount); + WINADVAPI PSID_IDENTIFIER_AUTHORITY WINAPI GetSidIdentifierAuthority(PSID pSid); + WINADVAPI PDWORD WINAPI GetSidSubAuthority(PSID pSid,DWORD nSubAuthority); + WINADVAPI PUCHAR WINAPI GetSidSubAuthorityCount(PSID pSid); + WINADVAPI DWORD WINAPI GetLengthSid(PSID pSid); + WINADVAPI WINBOOL WINAPI CopySid(DWORD nDestinationSidLength,PSID pDestinationSid,PSID pSourceSid); + WINADVAPI WINBOOL WINAPI AreAllAccessesGranted(DWORD GrantedAccess,DWORD DesiredAccess); + WINADVAPI WINBOOL WINAPI AreAnyAccessesGranted(DWORD GrantedAccess,DWORD DesiredAccess); + WINADVAPI VOID WINAPI MapGenericMask(PDWORD AccessMask,PGENERIC_MAPPING GenericMapping); + WINADVAPI WINBOOL WINAPI IsValidAcl(PACL pAcl); + WINADVAPI WINBOOL WINAPI InitializeAcl(PACL pAcl,DWORD nAclLength,DWORD dwAclRevision); + WINADVAPI WINBOOL WINAPI GetAclInformation(PACL pAcl,LPVOID pAclInformation,DWORD nAclInformationLength,ACL_INFORMATION_CLASS dwAclInformationClass); + WINADVAPI WINBOOL WINAPI SetAclInformation(PACL pAcl,LPVOID pAclInformation,DWORD nAclInformationLength,ACL_INFORMATION_CLASS dwAclInformationClass); + WINADVAPI WINBOOL WINAPI AddAce(PACL pAcl,DWORD dwAceRevision,DWORD dwStartingAceIndex,LPVOID pAceList,DWORD nAceListLength); + WINADVAPI WINBOOL WINAPI DeleteAce(PACL pAcl,DWORD dwAceIndex); + WINADVAPI WINBOOL WINAPI GetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce); + WINADVAPI WINBOOL WINAPI AddAccessAllowedAce(PACL pAcl,DWORD dwAceRevision,DWORD AccessMask,PSID pSid); + WINADVAPI WINBOOL WINAPI AddAccessAllowedAceEx(PACL pAcl,DWORD dwAceRevision,DWORD AceFlags,DWORD AccessMask,PSID pSid); + WINADVAPI WINBOOL WINAPI AddAccessDeniedAce(PACL pAcl,DWORD dwAceRevision,DWORD AccessMask,PSID pSid); + WINADVAPI WINBOOL WINAPI AddAccessDeniedAceEx(PACL pAcl,DWORD dwAceRevision,DWORD AceFlags,DWORD AccessMask,PSID pSid); + WINADVAPI WINBOOL WINAPI AddAuditAccessAce(PACL pAcl,DWORD dwAceRevision,DWORD dwAccessMask,PSID pSid,WINBOOL bAuditSuccess,WINBOOL bAuditFailure); + WINADVAPI WINBOOL WINAPI AddAuditAccessAceEx(PACL pAcl,DWORD dwAceRevision,DWORD AceFlags,DWORD dwAccessMask,PSID pSid,WINBOOL bAuditSuccess,WINBOOL bAuditFailure); + WINADVAPI WINBOOL WINAPI AddAccessAllowedObjectAce(PACL pAcl,DWORD dwAceRevision,DWORD AceFlags,DWORD AccessMask,GUID *ObjectTypeGuid,GUID *InheritedObjectTypeGuid,PSID pSid); + WINADVAPI WINBOOL WINAPI AddAccessDeniedObjectAce(PACL pAcl,DWORD dwAceRevision,DWORD AceFlags,DWORD AccessMask,GUID *ObjectTypeGuid,GUID *InheritedObjectTypeGuid,PSID pSid); + WINADVAPI WINBOOL WINAPI AddAuditAccessObjectAce(PACL pAcl,DWORD dwAceRevision,DWORD AceFlags,DWORD AccessMask,GUID *ObjectTypeGuid,GUID *InheritedObjectTypeGuid,PSID pSid,WINBOOL bAuditSuccess,WINBOOL bAuditFailure); + WINADVAPI WINBOOL WINAPI FindFirstFreeAce(PACL pAcl,LPVOID *pAce); + WINADVAPI WINBOOL WINAPI InitializeSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor,DWORD dwRevision); + WINADVAPI WINBOOL WINAPI IsValidSecurityDescriptor(PSECURITY_DESCRIPTOR pSecurityDescriptor); + WINADVAPI DWORD WINAPI GetSecurityDescriptorLength(PSECURITY_DESCRIPTOR pSecurityDescriptor); + WINADVAPI WINBOOL WINAPI GetSecurityDescriptorControl(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSECURITY_DESCRIPTOR_CONTROL pControl,LPDWORD lpdwRevision); + WINADVAPI WINBOOL WINAPI SetSecurityDescriptorControl(PSECURITY_DESCRIPTOR pSecurityDescriptor,SECURITY_DESCRIPTOR_CONTROL ControlBitsOfInterest,SECURITY_DESCRIPTOR_CONTROL ControlBitsToSet); + WINADVAPI WINBOOL WINAPI SetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor,WINBOOL bDaclPresent,PACL pDacl,WINBOOL bDaclDefaulted); + WINADVAPI WINBOOL WINAPI GetSecurityDescriptorDacl(PSECURITY_DESCRIPTOR pSecurityDescriptor,LPBOOL lpbDaclPresent,PACL *pDacl,LPBOOL lpbDaclDefaulted); + WINADVAPI WINBOOL WINAPI SetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR pSecurityDescriptor,WINBOOL bSaclPresent,PACL pSacl,WINBOOL bSaclDefaulted); + WINADVAPI WINBOOL WINAPI GetSecurityDescriptorSacl(PSECURITY_DESCRIPTOR pSecurityDescriptor,LPBOOL lpbSaclPresent,PACL *pSacl,LPBOOL lpbSaclDefaulted); + WINADVAPI WINBOOL WINAPI SetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSID pOwner,WINBOOL bOwnerDefaulted); + WINADVAPI WINBOOL WINAPI GetSecurityDescriptorOwner(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSID *pOwner,LPBOOL lpbOwnerDefaulted); + WINADVAPI WINBOOL WINAPI SetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSID pGroup,WINBOOL bGroupDefaulted); + WINADVAPI WINBOOL WINAPI GetSecurityDescriptorGroup(PSECURITY_DESCRIPTOR pSecurityDescriptor,PSID *pGroup,LPBOOL lpbGroupDefaulted); + WINADVAPI DWORD WINAPI SetSecurityDescriptorRMControl(PSECURITY_DESCRIPTOR SecurityDescriptor,PUCHAR RMControl); + WINADVAPI DWORD WINAPI GetSecurityDescriptorRMControl(PSECURITY_DESCRIPTOR SecurityDescriptor,PUCHAR RMControl); + WINADVAPI WINBOOL WINAPI CreatePrivateObjectSecurity(PSECURITY_DESCRIPTOR ParentDescriptor,PSECURITY_DESCRIPTOR CreatorDescriptor,PSECURITY_DESCRIPTOR *NewDescriptor,WINBOOL IsDirectoryObject,HANDLE Token,PGENERIC_MAPPING GenericMapping); + WINADVAPI WINBOOL WINAPI ConvertToAutoInheritPrivateObjectSecurity(PSECURITY_DESCRIPTOR ParentDescriptor,PSECURITY_DESCRIPTOR CurrentSecurityDescriptor,PSECURITY_DESCRIPTOR *NewSecurityDescriptor,GUID *ObjectType,BOOLEAN IsDirectoryObject,PGENERIC_MAPPING GenericMapping); + WINADVAPI WINBOOL WINAPI CreatePrivateObjectSecurityEx(PSECURITY_DESCRIPTOR ParentDescriptor,PSECURITY_DESCRIPTOR CreatorDescriptor,PSECURITY_DESCRIPTOR *NewDescriptor,GUID *ObjectType,WINBOOL IsContainerObject,ULONG AutoInheritFlags,HANDLE Token,PGENERIC_MAPPING GenericMapping); + WINADVAPI WINBOOL WINAPI CreatePrivateObjectSecurityWithMultipleInheritance(PSECURITY_DESCRIPTOR ParentDescriptor,PSECURITY_DESCRIPTOR CreatorDescriptor,PSECURITY_DESCRIPTOR *NewDescriptor,GUID **ObjectTypes,ULONG GuidCount,WINBOOL IsContainerObject,ULONG AutoInheritFlags,HANDLE Token,PGENERIC_MAPPING GenericMapping); + WINADVAPI WINBOOL WINAPI SetPrivateObjectSecurity (SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR ModificationDescriptor,PSECURITY_DESCRIPTOR *ObjectsSecurityDescriptor,PGENERIC_MAPPING GenericMapping,HANDLE Token); + WINADVAPI WINBOOL WINAPI SetPrivateObjectSecurityEx (SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR ModificationDescriptor,PSECURITY_DESCRIPTOR *ObjectsSecurityDescriptor,ULONG AutoInheritFlags,PGENERIC_MAPPING GenericMapping,HANDLE Token); + WINADVAPI WINBOOL WINAPI GetPrivateObjectSecurity(PSECURITY_DESCRIPTOR ObjectDescriptor,SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR ResultantDescriptor,DWORD DescriptorLength,PDWORD ReturnLength); + WINADVAPI WINBOOL WINAPI DestroyPrivateObjectSecurity(PSECURITY_DESCRIPTOR *ObjectDescriptor); + WINADVAPI WINBOOL WINAPI MakeSelfRelativeSD(PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,LPDWORD lpdwBufferLength); + WINADVAPI WINBOOL WINAPI MakeAbsoluteSD(PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor,LPDWORD lpdwAbsoluteSecurityDescriptorSize,PACL pDacl,LPDWORD lpdwDaclSize,PACL pSacl,LPDWORD lpdwSaclSize,PSID pOwner,LPDWORD lpdwOwnerSize,PSID pPrimaryGroup,LPDWORD lpdwPrimaryGroupSize); + WINADVAPI WINBOOL WINAPI MakeAbsoluteSD2(PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor,LPDWORD lpdwBufferSize); + WINADVAPI WINBOOL WINAPI SetFileSecurityA(LPCSTR lpFileName,SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor); + WINADVAPI WINBOOL WINAPI SetFileSecurityW(LPCWSTR lpFileName,SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor); + WINADVAPI WINBOOL WINAPI GetFileSecurityA(LPCSTR lpFileName,SECURITY_INFORMATION RequestedInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor,DWORD nLength,LPDWORD lpnLengthNeeded); + WINADVAPI WINBOOL WINAPI GetFileSecurityW(LPCWSTR lpFileName,SECURITY_INFORMATION RequestedInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor,DWORD nLength,LPDWORD lpnLengthNeeded); + WINADVAPI WINBOOL WINAPI SetKernelObjectSecurity(HANDLE Handle,SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR SecurityDescriptor); + WINBASEAPI HANDLE WINAPI FindFirstChangeNotificationA(LPCSTR lpPathName,WINBOOL bWatchSubtree,DWORD dwNotifyFilter); + WINBASEAPI HANDLE WINAPI FindFirstChangeNotificationW(LPCWSTR lpPathName,WINBOOL bWatchSubtree,DWORD dwNotifyFilter); + WINBASEAPI WINBOOL WINAPI FindNextChangeNotification(HANDLE hChangeHandle); + WINBASEAPI WINBOOL WINAPI FindCloseChangeNotification(HANDLE hChangeHandle); + WINBASEAPI WINBOOL WINAPI ReadDirectoryChangesW(HANDLE hDirectory,LPVOID lpBuffer,DWORD nBufferLength,WINBOOL bWatchSubtree,DWORD dwNotifyFilter,LPDWORD lpBytesReturned,LPOVERLAPPED lpOverlapped,LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine); + WINBASEAPI WINBOOL WINAPI VirtualLock(LPVOID lpAddress,SIZE_T dwSize); + WINBASEAPI WINBOOL WINAPI VirtualUnlock(LPVOID lpAddress,SIZE_T dwSize); + WINBASEAPI LPVOID WINAPI MapViewOfFileEx(HANDLE hFileMappingObject,DWORD dwDesiredAccess,DWORD dwFileOffsetHigh,DWORD dwFileOffsetLow,SIZE_T dwNumberOfBytesToMap,LPVOID lpBaseAddress); + WINBASEAPI WINBOOL WINAPI SetPriorityClass(HANDLE hProcess,DWORD dwPriorityClass); + WINBASEAPI DWORD WINAPI GetPriorityClass(HANDLE hProcess); + WINBASEAPI WINBOOL WINAPI IsBadReadPtr(CONST VOID *lp,UINT_PTR ucb); + WINBASEAPI WINBOOL WINAPI IsBadWritePtr(LPVOID lp,UINT_PTR ucb); + WINBASEAPI WINBOOL WINAPI IsBadHugeReadPtr(CONST VOID *lp,UINT_PTR ucb); + WINBASEAPI WINBOOL WINAPI IsBadHugeWritePtr(LPVOID lp,UINT_PTR ucb); + WINBASEAPI WINBOOL WINAPI IsBadCodePtr(FARPROC lpfn); + WINBASEAPI WINBOOL WINAPI IsBadStringPtrA(LPCSTR lpsz,UINT_PTR ucchMax); + WINBASEAPI WINBOOL WINAPI IsBadStringPtrW(LPCWSTR lpsz,UINT_PTR ucchMax); + WINADVAPI WINBOOL WINAPI LookupAccountSidA(LPCSTR lpSystemName,PSID Sid,LPSTR Name,LPDWORD cchName,LPSTR ReferencedDomainName,LPDWORD cchReferencedDomainName,PSID_NAME_USE peUse); + WINADVAPI WINBOOL WINAPI LookupAccountSidW(LPCWSTR lpSystemName,PSID Sid,LPWSTR Name,LPDWORD cchName,LPWSTR ReferencedDomainName,LPDWORD cchReferencedDomainName,PSID_NAME_USE peUse); + WINADVAPI WINBOOL WINAPI LookupAccountNameA(LPCSTR lpSystemName,LPCSTR lpAccountName,PSID Sid,LPDWORD cbSid,LPSTR ReferencedDomainName,LPDWORD cchReferencedDomainName,PSID_NAME_USE peUse); + WINADVAPI WINBOOL WINAPI LookupAccountNameW(LPCWSTR lpSystemName,LPCWSTR lpAccountName,PSID Sid,LPDWORD cbSid,LPWSTR ReferencedDomainName,LPDWORD cchReferencedDomainName,PSID_NAME_USE peUse); + WINADVAPI WINBOOL WINAPI LookupPrivilegeValueA(LPCSTR lpSystemName,LPCSTR lpName,PLUID lpLuid); + WINADVAPI WINBOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName,LPCWSTR lpName,PLUID lpLuid); + WINADVAPI WINBOOL WINAPI LookupPrivilegeNameA(LPCSTR lpSystemName,PLUID lpLuid,LPSTR lpName,LPDWORD cchName); + WINADVAPI WINBOOL WINAPI LookupPrivilegeNameW(LPCWSTR lpSystemName,PLUID lpLuid,LPWSTR lpName,LPDWORD cchName); + WINADVAPI WINBOOL WINAPI LookupPrivilegeDisplayNameA(LPCSTR lpSystemName,LPCSTR lpName,LPSTR lpDisplayName,LPDWORD cchDisplayName,LPDWORD lpLanguageId); + WINADVAPI WINBOOL WINAPI LookupPrivilegeDisplayNameW(LPCWSTR lpSystemName,LPCWSTR lpName,LPWSTR lpDisplayName,LPDWORD cchDisplayName,LPDWORD lpLanguageId); + WINADVAPI WINBOOL WINAPI AllocateLocallyUniqueId(PLUID Luid); + WINBASEAPI WINBOOL WINAPI BuildCommDCBA(LPCSTR lpDef,LPDCB lpDCB); + WINBASEAPI WINBOOL WINAPI BuildCommDCBW(LPCWSTR lpDef,LPDCB lpDCB); + WINBASEAPI WINBOOL WINAPI BuildCommDCBAndTimeoutsA(LPCSTR lpDef,LPDCB lpDCB,LPCOMMTIMEOUTS lpCommTimeouts); + WINBASEAPI WINBOOL WINAPI BuildCommDCBAndTimeoutsW(LPCWSTR lpDef,LPDCB lpDCB,LPCOMMTIMEOUTS lpCommTimeouts); + WINBASEAPI WINBOOL WINAPI CommConfigDialogA(LPCSTR lpszName,HWND hWnd,LPCOMMCONFIG lpCC); + WINBASEAPI WINBOOL WINAPI CommConfigDialogW(LPCWSTR lpszName,HWND hWnd,LPCOMMCONFIG lpCC); + WINBASEAPI WINBOOL WINAPI GetDefaultCommConfigA(LPCSTR lpszName,LPCOMMCONFIG lpCC,LPDWORD lpdwSize); + WINBASEAPI WINBOOL WINAPI GetDefaultCommConfigW(LPCWSTR lpszName,LPCOMMCONFIG lpCC,LPDWORD lpdwSize); + WINBASEAPI WINBOOL WINAPI SetDefaultCommConfigA(LPCSTR lpszName,LPCOMMCONFIG lpCC,DWORD dwSize); + WINBASEAPI WINBOOL WINAPI SetDefaultCommConfigW(LPCWSTR lpszName,LPCOMMCONFIG lpCC,DWORD dwSize); + +#define MAX_COMPUTERNAME_LENGTH 15 + + WINBASEAPI WINBOOL WINAPI GetComputerNameA(LPSTR lpBuffer,LPDWORD nSize); + WINBASEAPI WINBOOL WINAPI GetComputerNameW(LPWSTR lpBuffer,LPDWORD nSize); + WINBASEAPI WINBOOL WINAPI SetComputerNameA(LPCSTR lpComputerName); + WINBASEAPI WINBOOL WINAPI SetComputerNameW(LPCWSTR lpComputerName); + + typedef enum _COMPUTER_NAME_FORMAT { + ComputerNameNetBIOS,ComputerNameDnsHostname,ComputerNameDnsDomain,ComputerNameDnsFullyQualified,ComputerNamePhysicalNetBIOS,ComputerNamePhysicalDnsHostname,ComputerNamePhysicalDnsDomain,ComputerNamePhysicalDnsFullyQualified,ComputerNameMax + } COMPUTER_NAME_FORMAT; + + WINBASEAPI WINBOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT NameType,LPSTR lpBuffer,LPDWORD nSize); + WINBASEAPI WINBOOL WINAPI GetComputerNameExW(COMPUTER_NAME_FORMAT NameType,LPWSTR lpBuffer,LPDWORD nSize); + WINBASEAPI WINBOOL WINAPI SetComputerNameExA(COMPUTER_NAME_FORMAT NameType,LPCSTR lpBuffer); + WINBASEAPI WINBOOL WINAPI SetComputerNameExW(COMPUTER_NAME_FORMAT NameType,LPCWSTR lpBuffer); + WINBASEAPI WINBOOL WINAPI DnsHostnameToComputerNameA(LPCSTR Hostname,LPSTR ComputerName,LPDWORD nSize); + WINBASEAPI WINBOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR Hostname,LPWSTR ComputerName,LPDWORD nSize); + WINADVAPI WINBOOL WINAPI GetUserNameA(LPSTR lpBuffer,LPDWORD pcbBuffer); + WINADVAPI WINBOOL WINAPI GetUserNameW(LPWSTR lpBuffer,LPDWORD pcbBuffer); + +#define LOGON32_LOGON_INTERACTIVE 2 +#define LOGON32_LOGON_NETWORK 3 +#define LOGON32_LOGON_BATCH 4 +#define LOGON32_LOGON_SERVICE 5 +#define LOGON32_LOGON_UNLOCK 7 +#define LOGON32_LOGON_NETWORK_CLEARTEXT 8 +#define LOGON32_LOGON_NEW_CREDENTIALS 9 + +#define LOGON32_PROVIDER_DEFAULT 0 +#define LOGON32_PROVIDER_WINNT35 1 +#define LOGON32_PROVIDER_WINNT40 2 +#define LOGON32_PROVIDER_WINNT50 3 + +#ifdef UNICODE +#define LogonUser LogonUserW +#define LogonUserEx LogonUserExW +#define CreateProcessAsUser CreateProcessAsUserW +#else +#define LogonUser LogonUserA +#define LogonUserEx LogonUserExA +#define CreateProcessAsUser CreateProcessAsUserA +#endif + + WINADVAPI WINBOOL WINAPI LogonUserA(LPCSTR lpszUsername,LPCSTR lpszDomain,LPCSTR lpszPassword,DWORD dwLogonType,DWORD dwLogonProvider,PHANDLE phToken); + WINADVAPI WINBOOL WINAPI LogonUserW(LPCWSTR lpszUsername,LPCWSTR lpszDomain,LPCWSTR lpszPassword,DWORD dwLogonType,DWORD dwLogonProvider,PHANDLE phToken); + WINADVAPI WINBOOL WINAPI LogonUserExA(LPCSTR lpszUsername,LPCSTR lpszDomain,LPCSTR lpszPassword,DWORD dwLogonType,DWORD dwLogonProvider,PHANDLE phToken,PSID *ppLogonSid,PVOID *ppProfileBuffer,LPDWORD pdwProfileLength,PQUOTA_LIMITS pQuotaLimits); + WINADVAPI WINBOOL WINAPI LogonUserExW(LPCWSTR lpszUsername,LPCWSTR lpszDomain,LPCWSTR lpszPassword,DWORD dwLogonType,DWORD dwLogonProvider,PHANDLE phToken,PSID *ppLogonSid,PVOID *ppProfileBuffer,LPDWORD pdwProfileLength,PQUOTA_LIMITS pQuotaLimits); + WINADVAPI WINBOOL WINAPI ImpersonateLoggedOnUser(HANDLE hToken); + WINADVAPI WINBOOL WINAPI CreateProcessAsUserA(HANDLE hToken,LPCSTR lpApplicationName,LPSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,LPSECURITY_ATTRIBUTES lpThreadAttributes,WINBOOL bInheritHandles,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCSTR lpCurrentDirectory,LPSTARTUPINFOA lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation); + WINADVAPI WINBOOL WINAPI CreateProcessAsUserW(HANDLE hToken,LPCWSTR lpApplicationName,LPWSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,LPSECURITY_ATTRIBUTES lpThreadAttributes,WINBOOL bInheritHandles,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCWSTR lpCurrentDirectory,LPSTARTUPINFOW lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation); + +#define LOGON_WITH_PROFILE 0x1 +#define LOGON_NETCREDENTIALS_ONLY 0x2 +#define LOGON_ZERO_PASSWORD_BUFFER 0x80000000 + + WINADVAPI WINBOOL WINAPI CreateProcessWithLogonW(LPCWSTR lpUsername,LPCWSTR lpDomain,LPCWSTR lpPassword,DWORD dwLogonFlags,LPCWSTR lpApplicationName,LPWSTR lpCommandLine,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCWSTR lpCurrentDirectory,LPSTARTUPINFOW lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation); + WINADVAPI WINBOOL WINAPI CreateProcessWithTokenW(HANDLE hToken,DWORD dwLogonFlags,LPCWSTR lpApplicationName,LPWSTR lpCommandLine,DWORD dwCreationFlags,LPVOID lpEnvironment,LPCWSTR lpCurrentDirectory,LPSTARTUPINFOW lpStartupInfo,LPPROCESS_INFORMATION lpProcessInformation); + WINADVAPI WINBOOL WINAPI ImpersonateAnonymousToken(HANDLE ThreadHandle); + WINADVAPI WINBOOL WINAPI DuplicateTokenEx(HANDLE hExistingToken,DWORD dwDesiredAccess,LPSECURITY_ATTRIBUTES lpTokenAttributes,SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,TOKEN_TYPE TokenType,PHANDLE phNewToken); + WINADVAPI WINBOOL WINAPI CreateRestrictedToken(HANDLE ExistingTokenHandle,DWORD Flags,DWORD DisableSidCount,PSID_AND_ATTRIBUTES SidsToDisable,DWORD DeletePrivilegeCount,PLUID_AND_ATTRIBUTES PrivilegesToDelete,DWORD RestrictedSidCount,PSID_AND_ATTRIBUTES SidsToRestrict,PHANDLE NewTokenHandle); + WINADVAPI WINBOOL WINAPI IsTokenRestricted(HANDLE TokenHandle); + WINADVAPI WINBOOL WINAPI IsTokenUntrusted(HANDLE TokenHandle); + WINADVAPI WINBOOL WINAPI CheckTokenMembership(HANDLE TokenHandle,PSID SidToCheck,PBOOL IsMember); + + typedef WAITORTIMERCALLBACKFUNC WAITORTIMERCALLBACK; + + WINBASEAPI WINBOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject,HANDLE hObject,WAITORTIMERCALLBACK Callback,PVOID Context,ULONG dwMilliseconds,ULONG dwFlags); + WINBASEAPI HANDLE WINAPI RegisterWaitForSingleObjectEx(HANDLE hObject,WAITORTIMERCALLBACK Callback,PVOID Context,ULONG dwMilliseconds,ULONG dwFlags); + WINBASEAPI WINBOOL WINAPI UnregisterWait(HANDLE WaitHandle); + WINBASEAPI WINBOOL WINAPI UnregisterWaitEx(HANDLE WaitHandle,HANDLE CompletionEvent); + WINBASEAPI WINBOOL WINAPI QueueUserWorkItem(LPTHREAD_START_ROUTINE Function,PVOID Context,ULONG Flags); + WINBASEAPI WINBOOL WINAPI BindIoCompletionCallback(HANDLE FileHandle,LPOVERLAPPED_COMPLETION_ROUTINE Function,ULONG Flags); + WINBASEAPI HANDLE WINAPI CreateTimerQueue(VOID); + WINBASEAPI WINBOOL WINAPI CreateTimerQueueTimer(PHANDLE phNewTimer,HANDLE TimerQueue,WAITORTIMERCALLBACK Callback,PVOID Parameter,DWORD DueTime,DWORD Period,ULONG Flags); + WINBASEAPI WINBOOL WINAPI ChangeTimerQueueTimer(HANDLE TimerQueue,HANDLE Timer,ULONG DueTime,ULONG Period); + WINBASEAPI WINBOOL WINAPI DeleteTimerQueueTimer(HANDLE TimerQueue,HANDLE Timer,HANDLE CompletionEvent); + WINBASEAPI WINBOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue,HANDLE CompletionEvent); + WINBASEAPI HANDLE WINAPI SetTimerQueueTimer(HANDLE TimerQueue,WAITORTIMERCALLBACK Callback,PVOID Parameter,DWORD DueTime,DWORD Period,WINBOOL PreferIo); + WINBASEAPI WINBOOL WINAPI CancelTimerQueueTimer(HANDLE TimerQueue,HANDLE Timer); + WINBASEAPI WINBOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue); + +#define HW_PROFILE_GUIDLEN 39 +#define MAX_PROFILE_LEN 80 + +#define DOCKINFO_UNDOCKED (0x1) +#define DOCKINFO_DOCKED (0x2) +#define DOCKINFO_USER_SUPPLIED (0x4) +#define DOCKINFO_USER_UNDOCKED (DOCKINFO_USER_SUPPLIED | DOCKINFO_UNDOCKED) +#define DOCKINFO_USER_DOCKED (DOCKINFO_USER_SUPPLIED | DOCKINFO_DOCKED) + + typedef struct tagHW_PROFILE_INFOA { + DWORD dwDockInfo; + CHAR szHwProfileGuid[HW_PROFILE_GUIDLEN]; + CHAR szHwProfileName[MAX_PROFILE_LEN]; + } HW_PROFILE_INFOA,*LPHW_PROFILE_INFOA; + + typedef struct tagHW_PROFILE_INFOW { + DWORD dwDockInfo; + WCHAR szHwProfileGuid[HW_PROFILE_GUIDLEN]; + WCHAR szHwProfileName[MAX_PROFILE_LEN]; + } HW_PROFILE_INFOW,*LPHW_PROFILE_INFOW; + +#ifdef UNICODE + typedef HW_PROFILE_INFOW HW_PROFILE_INFO; + typedef LPHW_PROFILE_INFOW LPHW_PROFILE_INFO; +#else + typedef HW_PROFILE_INFOA HW_PROFILE_INFO; + typedef LPHW_PROFILE_INFOA LPHW_PROFILE_INFO; +#endif + +#ifdef UNICODE +#define GetCurrentHwProfile GetCurrentHwProfileW +#define GetVersionEx GetVersionExW +#define VerifyVersionInfo VerifyVersionInfoW +#else +#define GetCurrentHwProfile GetCurrentHwProfileA +#define GetVersionEx GetVersionExA +#define VerifyVersionInfo VerifyVersionInfoA +#endif + + WINADVAPI WINBOOL WINAPI GetCurrentHwProfileA (LPHW_PROFILE_INFOA lpHwProfileInfo); + WINADVAPI WINBOOL WINAPI GetCurrentHwProfileW (LPHW_PROFILE_INFOW lpHwProfileInfo); + WINBASEAPI WINBOOL WINAPI QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount); + WINBASEAPI WINBOOL WINAPI QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency); + WINBASEAPI WINBOOL WINAPI GetVersionExA(LPOSVERSIONINFOA lpVersionInformation); + WINBASEAPI WINBOOL WINAPI GetVersionExW(LPOSVERSIONINFOW lpVersionInformation); + WINBASEAPI WINBOOL WINAPI VerifyVersionInfoA(LPOSVERSIONINFOEXA lpVersionInformation,DWORD dwTypeMask,DWORDLONG dwlConditionMask); + WINBASEAPI WINBOOL WINAPI VerifyVersionInfoW(LPOSVERSIONINFOEXW lpVersionInformation,DWORD dwTypeMask,DWORDLONG dwlConditionMask); + +#include + +#define TC_NORMAL 0 +#define TC_HARDERR 1 +#define TC_GP_TRAP 2 +#define TC_SIGNAL 3 + +#define AC_LINE_OFFLINE 0x0 +#define AC_LINE_ONLINE 0x1 +#define AC_LINE_BACKUP_POWER 0x2 +#define AC_LINE_UNKNOWN 0xff + +#define BATTERY_FLAG_HIGH 0x1 +#define BATTERY_FLAG_LOW 0x2 +#define BATTERY_FLAG_CRITICAL 0x4 +#define BATTERY_FLAG_CHARGING 0x8 +#define BATTERY_FLAG_NO_BATTERY 0x80 +#define BATTERY_FLAG_UNKNOWN 0xff + +#define BATTERY_PERCENTAGE_UNKNOWN 0xff + +#define BATTERY_LIFE_UNKNOWN 0xffffffff + + typedef struct _SYSTEM_POWER_STATUS { + BYTE ACLineStatus; + BYTE BatteryFlag; + BYTE BatteryLifePercent; + BYTE Reserved1; + DWORD BatteryLifeTime; + DWORD BatteryFullLifeTime; + } SYSTEM_POWER_STATUS,*LPSYSTEM_POWER_STATUS; + +#ifdef UNICODE +#define CreateJobObject CreateJobObjectW +#define OpenJobObject OpenJobObjectW +#define FindFirstVolume FindFirstVolumeW +#define FindNextVolume FindNextVolumeW +#define FindFirstVolumeMountPoint FindFirstVolumeMountPointW +#define FindNextVolumeMountPoint FindNextVolumeMountPointW +#define SetVolumeMountPoint SetVolumeMountPointW +#define DeleteVolumeMountPoint DeleteVolumeMountPointW +#define GetVolumeNameForVolumeMountPoint GetVolumeNameForVolumeMountPointW +#define GetVolumePathName GetVolumePathNameW +#define GetVolumePathNamesForVolumeName GetVolumePathNamesForVolumeNameW +#else +#define CreateJobObject CreateJobObjectA +#define OpenJobObject OpenJobObjectA +#define FindFirstVolume FindFirstVolumeA +#define FindNextVolume FindNextVolumeA +#define FindFirstVolumeMountPoint FindFirstVolumeMountPointA +#define FindNextVolumeMountPoint FindNextVolumeMountPointA +#define SetVolumeMountPoint SetVolumeMountPointA +#define DeleteVolumeMountPoint DeleteVolumeMountPointA +#define GetVolumeNameForVolumeMountPoint GetVolumeNameForVolumeMountPointA +#define GetVolumePathName GetVolumePathNameA +#define GetVolumePathNamesForVolumeName GetVolumePathNamesForVolumeNameA +#endif + + WINBOOL WINAPI GetSystemPowerStatus(LPSYSTEM_POWER_STATUS lpSystemPowerStatus); + WINBOOL WINAPI SetSystemPowerState(WINBOOL fSuspend,WINBOOL fForce); + WINBASEAPI WINBOOL WINAPI AllocateUserPhysicalPages(HANDLE hProcess,PULONG_PTR NumberOfPages,PULONG_PTR PageArray); + WINBASEAPI WINBOOL WINAPI FreeUserPhysicalPages(HANDLE hProcess,PULONG_PTR NumberOfPages,PULONG_PTR PageArray); + WINBASEAPI WINBOOL WINAPI MapUserPhysicalPages(PVOID VirtualAddress,ULONG_PTR NumberOfPages,PULONG_PTR PageArray); + WINBASEAPI WINBOOL WINAPI MapUserPhysicalPagesScatter(PVOID *VirtualAddresses,ULONG_PTR NumberOfPages,PULONG_PTR PageArray); + WINBASEAPI HANDLE WINAPI CreateJobObjectA(LPSECURITY_ATTRIBUTES lpJobAttributes,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI CreateJobObjectW(LPSECURITY_ATTRIBUTES lpJobAttributes,LPCWSTR lpName); + WINBASEAPI HANDLE WINAPI OpenJobObjectA(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCSTR lpName); + WINBASEAPI HANDLE WINAPI OpenJobObjectW(DWORD dwDesiredAccess,WINBOOL bInheritHandle,LPCWSTR lpName); + WINBASEAPI WINBOOL WINAPI AssignProcessToJobObject(HANDLE hJob,HANDLE hProcess); + WINBASEAPI WINBOOL WINAPI TerminateJobObject(HANDLE hJob,UINT uExitCode); + WINBASEAPI WINBOOL WINAPI QueryInformationJobObject(HANDLE hJob,JOBOBJECTINFOCLASS JobObjectInformationClass,LPVOID lpJobObjectInformation,DWORD cbJobObjectInformationLength,LPDWORD lpReturnLength); + WINBASEAPI WINBOOL WINAPI SetInformationJobObject(HANDLE hJob,JOBOBJECTINFOCLASS JobObjectInformationClass,LPVOID lpJobObjectInformation,DWORD cbJobObjectInformationLength); + WINBASEAPI WINBOOL WINAPI IsProcessInJob(HANDLE ProcessHandle,HANDLE JobHandle,PBOOL Result); + WINBASEAPI WINBOOL WINAPI CreateJobSet(ULONG NumJob,PJOB_SET_ARRAY UserJobSet,ULONG Flags); + WINBASEAPI PVOID WINAPI AddVectoredExceptionHandler (ULONG First,PVECTORED_EXCEPTION_HANDLER Handler); + WINBASEAPI ULONG WINAPI RemoveVectoredExceptionHandler(PVOID Handle); + WINBASEAPI PVOID WINAPI AddVectoredContinueHandler (ULONG First,PVECTORED_EXCEPTION_HANDLER Handler); + WINBASEAPI ULONG WINAPI RemoveVectoredContinueHandler(PVOID Handle); + WINBASEAPI HANDLE WINAPI FindFirstVolumeA(LPSTR lpszVolumeName,DWORD cchBufferLength); + WINBASEAPI HANDLE WINAPI FindFirstVolumeW(LPWSTR lpszVolumeName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI FindNextVolumeA(HANDLE hFindVolume,LPSTR lpszVolumeName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI FindNextVolumeW(HANDLE hFindVolume,LPWSTR lpszVolumeName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI FindVolumeClose(HANDLE hFindVolume); + WINBASEAPI HANDLE WINAPI FindFirstVolumeMountPointA(LPCSTR lpszRootPathName,LPSTR lpszVolumeMountPoint,DWORD cchBufferLength); + WINBASEAPI HANDLE WINAPI FindFirstVolumeMountPointW(LPCWSTR lpszRootPathName,LPWSTR lpszVolumeMountPoint,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI FindNextVolumeMountPointA(HANDLE hFindVolumeMountPoint,LPSTR lpszVolumeMountPoint,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI FindNextVolumeMountPointW(HANDLE hFindVolumeMountPoint,LPWSTR lpszVolumeMountPoint,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI FindVolumeMountPointClose(HANDLE hFindVolumeMountPoint); + WINBASEAPI WINBOOL WINAPI SetVolumeMountPointA(LPCSTR lpszVolumeMountPoint,LPCSTR lpszVolumeName); + WINBASEAPI WINBOOL WINAPI SetVolumeMountPointW(LPCWSTR lpszVolumeMountPoint,LPCWSTR lpszVolumeName); + WINBASEAPI WINBOOL WINAPI DeleteVolumeMountPointA(LPCSTR lpszVolumeMountPoint); + WINBASEAPI WINBOOL WINAPI DeleteVolumeMountPointW(LPCWSTR lpszVolumeMountPoint); + WINBASEAPI WINBOOL WINAPI GetVolumeNameForVolumeMountPointA(LPCSTR lpszVolumeMountPoint,LPSTR lpszVolumeName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI GetVolumeNameForVolumeMountPointW(LPCWSTR lpszVolumeMountPoint,LPWSTR lpszVolumeName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI GetVolumePathNameA(LPCSTR lpszFileName,LPSTR lpszVolumePathName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI GetVolumePathNameW(LPCWSTR lpszFileName,LPWSTR lpszVolumePathName,DWORD cchBufferLength); + WINBASEAPI WINBOOL WINAPI GetVolumePathNamesForVolumeNameA(LPCSTR lpszVolumeName,LPCH lpszVolumePathNames,DWORD cchBufferLength,PDWORD lpcchReturnLength); + WINBASEAPI WINBOOL WINAPI GetVolumePathNamesForVolumeNameW(LPCWSTR lpszVolumeName,LPWCH lpszVolumePathNames,DWORD cchBufferLength,PDWORD lpcchReturnLength); + +#define ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID 0x1 +#define ACTCTX_FLAG_LANGID_VALID 0x2 +#define ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID 0x4 +#define ACTCTX_FLAG_RESOURCE_NAME_VALID 0x8 +#define ACTCTX_FLAG_SET_PROCESS_DEFAULT 0x10 +#define ACTCTX_FLAG_APPLICATION_NAME_VALID 0x20 +#define ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF 0x40 +#define ACTCTX_FLAG_HMODULE_VALID 0x80 + + typedef struct tagACTCTXA { + ULONG cbSize; + DWORD dwFlags; + LPCSTR lpSource; + USHORT wProcessorArchitecture; + LANGID wLangId; + LPCSTR lpAssemblyDirectory; + LPCSTR lpResourceName; + LPCSTR lpApplicationName; + HMODULE hModule; + } ACTCTXA,*PACTCTXA; + + typedef struct tagACTCTXW { + ULONG cbSize; + DWORD dwFlags; + LPCWSTR lpSource; + USHORT wProcessorArchitecture; + LANGID wLangId; + LPCWSTR lpAssemblyDirectory; + LPCWSTR lpResourceName; + LPCWSTR lpApplicationName; + HMODULE hModule; + } ACTCTXW,*PACTCTXW; + + typedef const ACTCTXA *PCACTCTXA; + typedef const ACTCTXW *PCACTCTXW; + +#ifdef UNICODE + typedef ACTCTXW ACTCTX; + typedef PACTCTXW PACTCTX; + typedef PCACTCTXW PCACTCTX; +#else + typedef ACTCTXA ACTCTX; + typedef PACTCTXA PACTCTX; + typedef PCACTCTXA PCACTCTX; +#endif + +#ifdef UNICODE +#define CreateActCtx CreateActCtxW +#else +#define CreateActCtx CreateActCtxA +#endif + + WINBASEAPI HANDLE WINAPI CreateActCtxA(PCACTCTXA pActCtx); + WINBASEAPI HANDLE WINAPI CreateActCtxW(PCACTCTXW pActCtx); + WINBASEAPI VOID WINAPI AddRefActCtx(HANDLE hActCtx); + WINBASEAPI VOID WINAPI ReleaseActCtx(HANDLE hActCtx); + WINBASEAPI WINBOOL WINAPI ZombifyActCtx(HANDLE hActCtx); + WINBASEAPI WINBOOL WINAPI ActivateActCtx(HANDLE hActCtx,ULONG_PTR *lpCookie); + +#define DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION (0x1) + + WINBASEAPI WINBOOL WINAPI DeactivateActCtx(DWORD dwFlags,ULONG_PTR ulCookie); + WINBASEAPI WINBOOL WINAPI GetCurrentActCtx(HANDLE *lphActCtx); + + typedef struct tagACTCTX_SECTION_KEYED_DATA_2600 { + ULONG cbSize; + ULONG ulDataFormatVersion; + PVOID lpData; + ULONG ulLength; + PVOID lpSectionGlobalData; + ULONG ulSectionGlobalDataLength; + PVOID lpSectionBase; + ULONG ulSectionTotalLength; + HANDLE hActCtx; + ULONG ulAssemblyRosterIndex; + } ACTCTX_SECTION_KEYED_DATA_2600,*PACTCTX_SECTION_KEYED_DATA_2600; + + typedef const ACTCTX_SECTION_KEYED_DATA_2600 *PCACTCTX_SECTION_KEYED_DATA_2600; + + typedef struct tagACTCTX_SECTION_KEYED_DATA_ASSEMBLY_METADATA { + PVOID lpInformation; + PVOID lpSectionBase; + ULONG ulSectionLength; + PVOID lpSectionGlobalDataBase; + ULONG ulSectionGlobalDataLength; + } ACTCTX_SECTION_KEYED_DATA_ASSEMBLY_METADATA,*PACTCTX_SECTION_KEYED_DATA_ASSEMBLY_METADATA; + + typedef const ACTCTX_SECTION_KEYED_DATA_ASSEMBLY_METADATA *PCACTCTX_SECTION_KEYED_DATA_ASSEMBLY_METADATA; + + typedef struct tagACTCTX_SECTION_KEYED_DATA { + ULONG cbSize; + ULONG ulDataFormatVersion; + PVOID lpData; + ULONG ulLength; + PVOID lpSectionGlobalData; + ULONG ulSectionGlobalDataLength; + PVOID lpSectionBase; + ULONG ulSectionTotalLength; + HANDLE hActCtx; + ULONG ulAssemblyRosterIndex; + + ULONG ulFlags; + ACTCTX_SECTION_KEYED_DATA_ASSEMBLY_METADATA AssemblyMetadata; + } ACTCTX_SECTION_KEYED_DATA,*PACTCTX_SECTION_KEYED_DATA; + + typedef const ACTCTX_SECTION_KEYED_DATA *PCACTCTX_SECTION_KEYED_DATA; + +#define FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX 0x1 +#define FIND_ACTCTX_SECTION_KEY_RETURN_FLAGS 0x2 +#define FIND_ACTCTX_SECTION_KEY_RETURN_ASSEMBLY_METADATA 0x4 + +#ifdef UNICODE +#define FindActCtxSectionString FindActCtxSectionStringW +#else +#define FindActCtxSectionString FindActCtxSectionStringA +#endif + + WINBASEAPI WINBOOL WINAPI FindActCtxSectionStringA(DWORD dwFlags,const GUID *lpExtensionGuid,ULONG ulSectionId,LPCSTR lpStringToFind,PACTCTX_SECTION_KEYED_DATA ReturnedData); + WINBASEAPI WINBOOL WINAPI FindActCtxSectionStringW(DWORD dwFlags,const GUID *lpExtensionGuid,ULONG ulSectionId,LPCWSTR lpStringToFind,PACTCTX_SECTION_KEYED_DATA ReturnedData); + WINBASEAPI WINBOOL WINAPI FindActCtxSectionGuid(DWORD dwFlags,const GUID *lpExtensionGuid,ULONG ulSectionId,const GUID *lpGuidToFind,PACTCTX_SECTION_KEYED_DATA ReturnedData); + +#ifndef RC_INVOKED +#ifndef ACTIVATION_CONTEXT_BASIC_INFORMATION_DEFINED + + typedef struct _ACTIVATION_CONTEXT_BASIC_INFORMATION { + HANDLE hActCtx; + DWORD dwFlags; + } ACTIVATION_CONTEXT_BASIC_INFORMATION,*PACTIVATION_CONTEXT_BASIC_INFORMATION; + + typedef const struct _ACTIVATION_CONTEXT_BASIC_INFORMATION *PCACTIVATION_CONTEXT_BASIC_INFORMATION; + +#define ACTIVATION_CONTEXT_BASIC_INFORMATION_DEFINED 1 +#endif +#endif + +#define QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX 0x4 +#define QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE 0x8 +#define QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS 0x10 +#define QUERY_ACTCTX_FLAG_NO_ADDREF 0x80000000 + + WINBASEAPI WINBOOL WINAPI QueryActCtxW(DWORD dwFlags,HANDLE hActCtx,PVOID pvSubInstance,ULONG ulInfoClass,PVOID pvBuffer,SIZE_T cbBuffer,SIZE_T *pcbWrittenOrRequired); + + typedef WINBOOL (WINAPI *PQUERYACTCTXW_FUNC)(DWORD dwFlags,HANDLE hActCtx,PVOID pvSubInstance,ULONG ulInfoClass,PVOID pvBuffer,SIZE_T cbBuffer,SIZE_T *pcbWrittenOrRequired); + + WINBASEAPI WINBOOL WINAPI ProcessIdToSessionId(DWORD dwProcessId,DWORD *pSessionId); + WINBASEAPI DWORD WINAPI WTSGetActiveConsoleSessionId(); + WINBASEAPI WINBOOL WINAPI IsWow64Process(HANDLE hProcess,PBOOL Wow64Process); + WINBASEAPI WINBOOL WINAPI GetLogicalProcessorInformation(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION Buffer,PDWORD ReturnedLength); + WINBASEAPI WINBOOL WINAPI GetNumaHighestNodeNumber(PULONG HighestNodeNumber); + WINBASEAPI WINBOOL WINAPI GetNumaProcessorNode(UCHAR Processor,PUCHAR NodeNumber); + WINBASEAPI WINBOOL WINAPI GetNumaNodeProcessorMask(UCHAR Node,PULONGLONG ProcessorMask); + WINBASEAPI WINBOOL WINAPI GetNumaAvailableMemoryNode(UCHAR Node,PULONGLONG AvailableBytes); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/wincon.h b/05/tcc-final-old/win32/include/winapi/wincon.h new file mode 100644 index 0000000..a3501ee --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/wincon.h @@ -0,0 +1,301 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINCON_ +#define _WINCON_ + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct _COORD { + SHORT X; + SHORT Y; + } COORD,*PCOORD; + + typedef struct _SMALL_RECT { + SHORT Left; + SHORT Top; + SHORT Right; + SHORT Bottom; + } SMALL_RECT,*PSMALL_RECT; + + typedef struct _KEY_EVENT_RECORD { + WINBOOL bKeyDown; + WORD wRepeatCount; + WORD wVirtualKeyCode; + WORD wVirtualScanCode; + union { + WCHAR UnicodeChar; + CHAR AsciiChar; + } uChar; + DWORD dwControlKeyState; + } KEY_EVENT_RECORD,*PKEY_EVENT_RECORD; + +#define RIGHT_ALT_PRESSED 0x1 +#define LEFT_ALT_PRESSED 0x2 +#define RIGHT_CTRL_PRESSED 0x4 +#define LEFT_CTRL_PRESSED 0x8 +#define SHIFT_PRESSED 0x10 +#define NUMLOCK_ON 0x20 +#define SCROLLLOCK_ON 0x40 +#define CAPSLOCK_ON 0x80 +#define ENHANCED_KEY 0x100 +#define NLS_DBCSCHAR 0x10000 +#define NLS_ALPHANUMERIC 0x0 +#define NLS_KATAKANA 0x20000 +#define NLS_HIRAGANA 0x40000 +#define NLS_ROMAN 0x400000 +#define NLS_IME_CONVERSION 0x800000 +#define NLS_IME_DISABLE 0x20000000 + + typedef struct _MOUSE_EVENT_RECORD { + COORD dwMousePosition; + DWORD dwButtonState; + DWORD dwControlKeyState; + DWORD dwEventFlags; + } MOUSE_EVENT_RECORD,*PMOUSE_EVENT_RECORD; + +#define FROM_LEFT_1ST_BUTTON_PRESSED 0x1 +#define RIGHTMOST_BUTTON_PRESSED 0x2 +#define FROM_LEFT_2ND_BUTTON_PRESSED 0x4 +#define FROM_LEFT_3RD_BUTTON_PRESSED 0x8 +#define FROM_LEFT_4TH_BUTTON_PRESSED 0x10 + +#define MOUSE_MOVED 0x1 +#define DOUBLE_CLICK 0x2 +#define MOUSE_WHEELED 0x4 + + typedef struct _WINDOW_BUFFER_SIZE_RECORD { + COORD dwSize; + } WINDOW_BUFFER_SIZE_RECORD,*PWINDOW_BUFFER_SIZE_RECORD; + + typedef struct _MENU_EVENT_RECORD { + UINT dwCommandId; + } MENU_EVENT_RECORD,*PMENU_EVENT_RECORD; + + typedef struct _FOCUS_EVENT_RECORD { + WINBOOL bSetFocus; + } FOCUS_EVENT_RECORD,*PFOCUS_EVENT_RECORD; + + typedef struct _INPUT_RECORD { + WORD EventType; + union { + KEY_EVENT_RECORD KeyEvent; + MOUSE_EVENT_RECORD MouseEvent; + WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent; + MENU_EVENT_RECORD MenuEvent; + FOCUS_EVENT_RECORD FocusEvent; + } Event; + } INPUT_RECORD,*PINPUT_RECORD; + +#define KEY_EVENT 0x1 +#define MOUSE_EVENT 0x2 +#define WINDOW_BUFFER_SIZE_EVENT 0x4 +#define MENU_EVENT 0x8 +#define FOCUS_EVENT 0x10 + + typedef struct _CHAR_INFO { + union { + WCHAR UnicodeChar; + CHAR AsciiChar; + } Char; + WORD Attributes; + } CHAR_INFO,*PCHAR_INFO; + +#define FOREGROUND_BLUE 0x1 +#define FOREGROUND_GREEN 0x2 +#define FOREGROUND_RED 0x4 +#define FOREGROUND_INTENSITY 0x8 +#define BACKGROUND_BLUE 0x10 +#define BACKGROUND_GREEN 0x20 +#define BACKGROUND_RED 0x40 +#define BACKGROUND_INTENSITY 0x80 +#define COMMON_LVB_LEADING_BYTE 0x100 +#define COMMON_LVB_TRAILING_BYTE 0x200 +#define COMMON_LVB_GRID_HORIZONTAL 0x400 +#define COMMON_LVB_GRID_LVERTICAL 0x800 +#define COMMON_LVB_GRID_RVERTICAL 0x1000 +#define COMMON_LVB_REVERSE_VIDEO 0x4000 +#define COMMON_LVB_UNDERSCORE 0x8000 + +#define COMMON_LVB_SBCSDBCS 0x300 + + typedef struct _CONSOLE_SCREEN_BUFFER_INFO { + COORD dwSize; + COORD dwCursorPosition; + WORD wAttributes; + SMALL_RECT srWindow; + COORD dwMaximumWindowSize; + } CONSOLE_SCREEN_BUFFER_INFO,*PCONSOLE_SCREEN_BUFFER_INFO; + + typedef struct _CONSOLE_CURSOR_INFO { + DWORD dwSize; + WINBOOL bVisible; + } CONSOLE_CURSOR_INFO,*PCONSOLE_CURSOR_INFO; + + typedef struct _CONSOLE_FONT_INFO { + DWORD nFont; + COORD dwFontSize; + } CONSOLE_FONT_INFO,*PCONSOLE_FONT_INFO; + + typedef struct _CONSOLE_SELECTION_INFO { + DWORD dwFlags; + COORD dwSelectionAnchor; + SMALL_RECT srSelection; + } CONSOLE_SELECTION_INFO,*PCONSOLE_SELECTION_INFO; + +#define CONSOLE_NO_SELECTION 0x0 +#define CONSOLE_SELECTION_IN_PROGRESS 0x1 +#define CONSOLE_SELECTION_NOT_EMPTY 0x2 +#define CONSOLE_MOUSE_SELECTION 0x4 +#define CONSOLE_MOUSE_DOWN 0x8 + + typedef WINBOOL (WINAPI *PHANDLER_ROUTINE)(DWORD CtrlType); + +#define CTRL_C_EVENT 0 +#define CTRL_BREAK_EVENT 1 +#define CTRL_CLOSE_EVENT 2 + +#define CTRL_LOGOFF_EVENT 5 +#define CTRL_SHUTDOWN_EVENT 6 + +#define ENABLE_PROCESSED_INPUT 0x1 +#define ENABLE_LINE_INPUT 0x2 +#define ENABLE_ECHO_INPUT 0x4 +#define ENABLE_WINDOW_INPUT 0x8 +#define ENABLE_MOUSE_INPUT 0x10 + +#define ENABLE_PROCESSED_OUTPUT 0x1 +#define ENABLE_WRAP_AT_EOL_OUTPUT 0x2 + +#ifdef UNICODE +#define PeekConsoleInput PeekConsoleInputW +#define ReadConsoleInput ReadConsoleInputW +#define WriteConsoleInput WriteConsoleInputW +#define ReadConsoleOutput ReadConsoleOutputW +#define WriteConsoleOutput WriteConsoleOutputW +#define ReadConsoleOutputCharacter ReadConsoleOutputCharacterW +#define WriteConsoleOutputCharacter WriteConsoleOutputCharacterW +#define FillConsoleOutputCharacter FillConsoleOutputCharacterW +#define ScrollConsoleScreenBuffer ScrollConsoleScreenBufferW +#define GetConsoleTitle GetConsoleTitleW +#define SetConsoleTitle SetConsoleTitleW +#define ReadConsole ReadConsoleW +#define WriteConsole WriteConsoleW +#define AddConsoleAlias AddConsoleAliasW +#define GetConsoleAlias GetConsoleAliasW +#define GetConsoleAliasesLength GetConsoleAliasesLengthW +#define GetConsoleAliasExesLength GetConsoleAliasExesLengthW +#define GetConsoleAliases GetConsoleAliasesW +#define GetConsoleAliasExes GetConsoleAliasExesW +#else +#define PeekConsoleInput PeekConsoleInputA +#define ReadConsoleInput ReadConsoleInputA +#define WriteConsoleInput WriteConsoleInputA +#define ReadConsoleOutput ReadConsoleOutputA +#define WriteConsoleOutput WriteConsoleOutputA +#define ReadConsoleOutputCharacter ReadConsoleOutputCharacterA +#define WriteConsoleOutputCharacter WriteConsoleOutputCharacterA +#define FillConsoleOutputCharacter FillConsoleOutputCharacterA +#define ScrollConsoleScreenBuffer ScrollConsoleScreenBufferA +#define GetConsoleTitle GetConsoleTitleA +#define SetConsoleTitle SetConsoleTitleA +#define ReadConsole ReadConsoleA +#define WriteConsole WriteConsoleA +#define AddConsoleAlias AddConsoleAliasA +#define GetConsoleAlias GetConsoleAliasA +#define GetConsoleAliasesLength GetConsoleAliasesLengthA +#define GetConsoleAliasExesLength GetConsoleAliasExesLengthA +#define GetConsoleAliases GetConsoleAliasesA +#define GetConsoleAliasExes GetConsoleAliasExesA +#endif + + WINBASEAPI WINBOOL WINAPI PeekConsoleInputA(HANDLE hConsoleInput,PINPUT_RECORD lpBuffer,DWORD nLength,LPDWORD lpNumberOfEventsRead); + WINBASEAPI WINBOOL WINAPI PeekConsoleInputW(HANDLE hConsoleInput,PINPUT_RECORD lpBuffer,DWORD nLength,LPDWORD lpNumberOfEventsRead); + WINBASEAPI WINBOOL WINAPI ReadConsoleInputA(HANDLE hConsoleInput,PINPUT_RECORD lpBuffer,DWORD nLength,LPDWORD lpNumberOfEventsRead); + WINBASEAPI WINBOOL WINAPI ReadConsoleInputW(HANDLE hConsoleInput,PINPUT_RECORD lpBuffer,DWORD nLength,LPDWORD lpNumberOfEventsRead); + WINBASEAPI WINBOOL WINAPI WriteConsoleInputA(HANDLE hConsoleInput,CONST INPUT_RECORD *lpBuffer,DWORD nLength,LPDWORD lpNumberOfEventsWritten); + WINBASEAPI WINBOOL WINAPI WriteConsoleInputW(HANDLE hConsoleInput,CONST INPUT_RECORD *lpBuffer,DWORD nLength,LPDWORD lpNumberOfEventsWritten); + WINBASEAPI WINBOOL WINAPI ReadConsoleOutputA(HANDLE hConsoleOutput,PCHAR_INFO lpBuffer,COORD dwBufferSize,COORD dwBufferCoord,PSMALL_RECT lpReadRegion); + WINBASEAPI WINBOOL WINAPI ReadConsoleOutputW(HANDLE hConsoleOutput,PCHAR_INFO lpBuffer,COORD dwBufferSize,COORD dwBufferCoord,PSMALL_RECT lpReadRegion); + WINBASEAPI WINBOOL WINAPI WriteConsoleOutputA(HANDLE hConsoleOutput,CONST CHAR_INFO *lpBuffer,COORD dwBufferSize,COORD dwBufferCoord,PSMALL_RECT lpWriteRegion); + WINBASEAPI WINBOOL WINAPI WriteConsoleOutputW(HANDLE hConsoleOutput,CONST CHAR_INFO *lpBuffer,COORD dwBufferSize,COORD dwBufferCoord,PSMALL_RECT lpWriteRegion); + WINBASEAPI WINBOOL WINAPI ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,LPSTR lpCharacter,DWORD nLength,COORD dwReadCoord,LPDWORD lpNumberOfCharsRead); + WINBASEAPI WINBOOL WINAPI ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,LPWSTR lpCharacter,DWORD nLength,COORD dwReadCoord,LPDWORD lpNumberOfCharsRead); + WINBASEAPI WINBOOL WINAPI ReadConsoleOutputAttribute(HANDLE hConsoleOutput,LPWORD lpAttribute,DWORD nLength,COORD dwReadCoord,LPDWORD lpNumberOfAttrsRead); + WINBASEAPI WINBOOL WINAPI WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,LPCSTR lpCharacter,DWORD nLength,COORD dwWriteCoord,LPDWORD lpNumberOfCharsWritten); + WINBASEAPI WINBOOL WINAPI WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,LPCWSTR lpCharacter,DWORD nLength,COORD dwWriteCoord,LPDWORD lpNumberOfCharsWritten); + WINBASEAPI WINBOOL WINAPI WriteConsoleOutputAttribute(HANDLE hConsoleOutput,CONST WORD *lpAttribute,DWORD nLength,COORD dwWriteCoord,LPDWORD lpNumberOfAttrsWritten); + WINBASEAPI WINBOOL WINAPI FillConsoleOutputCharacterA(HANDLE hConsoleOutput,CHAR cCharacter,DWORD nLength,COORD dwWriteCoord,LPDWORD lpNumberOfCharsWritten); + WINBASEAPI WINBOOL WINAPI FillConsoleOutputCharacterW(HANDLE hConsoleOutput,WCHAR cCharacter,DWORD nLength,COORD dwWriteCoord,LPDWORD lpNumberOfCharsWritten); + WINBASEAPI WINBOOL WINAPI FillConsoleOutputAttribute(HANDLE hConsoleOutput,WORD wAttribute,DWORD nLength,COORD dwWriteCoord,LPDWORD lpNumberOfAttrsWritten); + WINBASEAPI WINBOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle,LPDWORD lpMode); + WINBASEAPI WINBOOL WINAPI GetNumberOfConsoleInputEvents(HANDLE hConsoleInput,LPDWORD lpNumberOfEvents); + WINBASEAPI WINBOOL WINAPI GetConsoleScreenBufferInfo(HANDLE hConsoleOutput,PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo); + WINBASEAPI COORD WINAPI GetLargestConsoleWindowSize(HANDLE hConsoleOutput); + WINBASEAPI WINBOOL WINAPI GetConsoleCursorInfo(HANDLE hConsoleOutput,PCONSOLE_CURSOR_INFO lpConsoleCursorInfo); + WINBASEAPI WINBOOL WINAPI GetCurrentConsoleFont(HANDLE hConsoleOutput,WINBOOL bMaximumWindow,PCONSOLE_FONT_INFO lpConsoleCurrentFont); + WINBASEAPI COORD WINAPI GetConsoleFontSize(HANDLE hConsoleOutput,DWORD nFont); + WINBASEAPI WINBOOL WINAPI GetConsoleSelectionInfo(PCONSOLE_SELECTION_INFO lpConsoleSelectionInfo); + WINBASEAPI WINBOOL WINAPI GetNumberOfConsoleMouseButtons(LPDWORD lpNumberOfMouseButtons); + WINBASEAPI WINBOOL WINAPI SetConsoleMode(HANDLE hConsoleHandle,DWORD dwMode); + WINBASEAPI WINBOOL WINAPI SetConsoleActiveScreenBuffer(HANDLE hConsoleOutput); + WINBASEAPI WINBOOL WINAPI FlushConsoleInputBuffer(HANDLE hConsoleInput); + WINBASEAPI WINBOOL WINAPI SetConsoleScreenBufferSize(HANDLE hConsoleOutput,COORD dwSize); + WINBASEAPI WINBOOL WINAPI SetConsoleCursorPosition(HANDLE hConsoleOutput,COORD dwCursorPosition); + WINBASEAPI WINBOOL WINAPI SetConsoleCursorInfo(HANDLE hConsoleOutput,CONST CONSOLE_CURSOR_INFO *lpConsoleCursorInfo); + WINBASEAPI WINBOOL WINAPI ScrollConsoleScreenBufferA(HANDLE hConsoleOutput,CONST SMALL_RECT *lpScrollRectangle,CONST SMALL_RECT *lpClipRectangle,COORD dwDestinationOrigin,CONST CHAR_INFO *lpFill); + WINBASEAPI WINBOOL WINAPI ScrollConsoleScreenBufferW(HANDLE hConsoleOutput,CONST SMALL_RECT *lpScrollRectangle,CONST SMALL_RECT *lpClipRectangle,COORD dwDestinationOrigin,CONST CHAR_INFO *lpFill); + WINBASEAPI WINBOOL WINAPI SetConsoleWindowInfo(HANDLE hConsoleOutput,WINBOOL bAbsolute,CONST SMALL_RECT *lpConsoleWindow); + WINBASEAPI WINBOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput,WORD wAttributes); + WINBASEAPI WINBOOL WINAPI SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine,WINBOOL Add); + WINBASEAPI WINBOOL WINAPI GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,DWORD dwProcessGroupId); + WINBASEAPI WINBOOL WINAPI AllocConsole(VOID); + WINBASEAPI WINBOOL WINAPI FreeConsole(VOID); + WINBASEAPI WINBOOL WINAPI AttachConsole(DWORD dwProcessId); + +#define ATTACH_PARENT_PROCESS ((DWORD)-1) + + WINBASEAPI DWORD WINAPI GetConsoleTitleA(LPSTR lpConsoleTitle,DWORD nSize); + WINBASEAPI DWORD WINAPI GetConsoleTitleW(LPWSTR lpConsoleTitle,DWORD nSize); + WINBASEAPI WINBOOL WINAPI SetConsoleTitleA(LPCSTR lpConsoleTitle); + WINBASEAPI WINBOOL WINAPI SetConsoleTitleW(LPCWSTR lpConsoleTitle); + WINBASEAPI WINBOOL WINAPI ReadConsoleA(HANDLE hConsoleInput,LPVOID lpBuffer,DWORD nNumberOfCharsToRead,LPDWORD lpNumberOfCharsRead,LPVOID lpReserved); + WINBASEAPI WINBOOL WINAPI ReadConsoleW(HANDLE hConsoleInput,LPVOID lpBuffer,DWORD nNumberOfCharsToRead,LPDWORD lpNumberOfCharsRead,LPVOID lpReserved); + WINBASEAPI WINBOOL WINAPI WriteConsoleA(HANDLE hConsoleOutput,CONST VOID *lpBuffer,DWORD nNumberOfCharsToWrite,LPDWORD lpNumberOfCharsWritten,LPVOID lpReserved); + WINBASEAPI WINBOOL WINAPI WriteConsoleW(HANDLE hConsoleOutput,CONST VOID *lpBuffer,DWORD nNumberOfCharsToWrite,LPDWORD lpNumberOfCharsWritten,LPVOID lpReserved); + +#define CONSOLE_TEXTMODE_BUFFER 1 + + WINBASEAPI HANDLE WINAPI CreateConsoleScreenBuffer(DWORD dwDesiredAccess,DWORD dwShareMode,CONST SECURITY_ATTRIBUTES *lpSecurityAttributes,DWORD dwFlags,LPVOID lpScreenBufferData); + WINBASEAPI UINT WINAPI GetConsoleCP(VOID); + WINBASEAPI WINBOOL WINAPI SetConsoleCP(UINT wCodePageID); + WINBASEAPI UINT WINAPI GetConsoleOutputCP(VOID); + WINBASEAPI WINBOOL WINAPI SetConsoleOutputCP(UINT wCodePageID); + +#define CONSOLE_FULLSCREEN 1 +#define CONSOLE_FULLSCREEN_HARDWARE 2 + + WINBASEAPI WINBOOL WINAPI GetConsoleDisplayMode(LPDWORD lpModeFlags); + WINBASEAPI HWND WINAPI GetConsoleWindow(VOID); + WINBASEAPI DWORD WINAPI GetConsoleProcessList(LPDWORD lpdwProcessList,DWORD dwProcessCount); + WINBASEAPI WINBOOL WINAPI AddConsoleAliasA(LPSTR Source,LPSTR Target,LPSTR ExeName); + WINBASEAPI WINBOOL WINAPI AddConsoleAliasW(LPWSTR Source,LPWSTR Target,LPWSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasA(LPSTR Source,LPSTR TargetBuffer,DWORD TargetBufferLength,LPSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasW(LPWSTR Source,LPWSTR TargetBuffer,DWORD TargetBufferLength,LPWSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasesLengthA(LPSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasesLengthW(LPWSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasExesLengthA(VOID); + WINBASEAPI DWORD WINAPI GetConsoleAliasExesLengthW(VOID); + WINBASEAPI DWORD WINAPI GetConsoleAliasesA(LPSTR AliasBuffer,DWORD AliasBufferLength,LPSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasesW(LPWSTR AliasBuffer,DWORD AliasBufferLength,LPWSTR ExeName); + WINBASEAPI DWORD WINAPI GetConsoleAliasExesA(LPSTR ExeNameBuffer,DWORD ExeNameBufferLength); + WINBASEAPI DWORD WINAPI GetConsoleAliasExesW(LPWSTR ExeNameBuffer,DWORD ExeNameBufferLength); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/windef.h b/05/tcc-final-old/win32/include/winapi/windef.h new file mode 100644 index 0000000..d63bdef --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/windef.h @@ -0,0 +1,293 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINDEF_ +#define _WINDEF_ + +#ifndef STRICT +#define STRICT 1 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WINVER +#define WINVER 0x0502 +#endif + +#ifndef BASETYPES +#define BASETYPES + typedef unsigned long ULONG; + typedef ULONG *PULONG; + typedef unsigned short USHORT; + typedef USHORT *PUSHORT; + typedef unsigned char UCHAR; + typedef UCHAR *PUCHAR; + typedef char *PSZ; +#endif + +#define MAX_PATH 260 + +#ifndef NULL +#ifdef __cplusplus +#define NULL 0 +#else +#define NULL ((void *)0) +#endif +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef IN +#define IN +#endif + +#ifndef OUT +#define OUT +#endif + +#ifndef OPTIONAL +#define OPTIONAL +#endif + +#undef far +#undef near +#undef pascal + +#define far +#define near +#define pascal __stdcall + +#define cdecl +#ifndef CDECL +#define CDECL +#endif +#ifndef CALLBACK +#define CALLBACK __stdcall +#endif +#ifndef WINAPI +#define WINAPI __stdcall +#endif +#define WINAPIV __cdecl +#define APIENTRY WINAPI +#define APIPRIVATE WINAPI +#define PASCAL WINAPI +#define WINAPI_INLINE WINAPI + +#undef FAR +#undef NEAR +#define FAR +#define NEAR +#ifndef CONST +#define CONST const +#endif + + typedef unsigned long DWORD; + typedef int WINBOOL; +#define BOOL WINBOOL + typedef unsigned char BYTE; + typedef unsigned short WORD; + typedef float FLOAT; + typedef FLOAT *PFLOAT; + typedef WINBOOL *PBOOL; + typedef WINBOOL *LPBOOL; + typedef BYTE *PBYTE; + typedef BYTE *LPBYTE; + typedef int *PINT; + typedef int *LPINT; + typedef WORD *PWORD; + typedef WORD *LPWORD; + typedef long *LPLONG; + typedef DWORD *PDWORD; + typedef DWORD *LPDWORD; + typedef void *LPVOID; +# ifndef _LPCVOID_DEFINED +#define _LPCVOID_DEFINED +typedef CONST void *LPCVOID; +#endif + typedef int INT; + typedef unsigned int UINT; + typedef unsigned int *PUINT; + +#ifndef NT_INCLUDED +#include +#endif + +//gr #include + + typedef UINT_PTR WPARAM; + typedef LONG_PTR LPARAM; + typedef LONG_PTR LRESULT; + +#ifndef __cplusplus +#ifndef NOMINMAX +#ifndef max +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#endif + +#ifndef min +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif +#endif +#endif + +#define MAKEWORD(a,b) ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) | ((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8)) +#define MAKELONG(a,b) ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16)) +#define LOWORD(l) ((WORD)((DWORD_PTR)(l) & 0xffff)) +#define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16)) +#define LOBYTE(w) ((BYTE)((DWORD_PTR)(w) & 0xff)) +#define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8)) + +#ifndef WIN_INTERNAL + DECLARE_HANDLE (HWND); + DECLARE_HANDLE (HHOOK); +#ifdef WINABLE + DECLARE_HANDLE (HEVENT); +#endif +#endif + + typedef WORD ATOM; + + typedef HANDLE *SPHANDLE; + typedef HANDLE *LPHANDLE; + typedef HANDLE HGLOBAL; + typedef HANDLE HLOCAL; + typedef HANDLE GLOBALHANDLE; + typedef HANDLE LOCALHANDLE; +#ifdef _WIN64 + typedef INT_PTR (WINAPI *FARPROC)(); + typedef INT_PTR (WINAPI *NEARPROC)(); + typedef INT_PTR (WINAPI *PROC)(); +#else + typedef int (WINAPI *FARPROC)(); + typedef int (WINAPI *NEARPROC)(); + typedef int (WINAPI *PROC)(); +#endif + + typedef void *HGDIOBJ; + + DECLARE_HANDLE(HKEY); + typedef HKEY *PHKEY; + + DECLARE_HANDLE(HACCEL); + DECLARE_HANDLE(HBITMAP); + DECLARE_HANDLE(HBRUSH); + DECLARE_HANDLE(HCOLORSPACE); + DECLARE_HANDLE(HDC); + DECLARE_HANDLE(HGLRC); + DECLARE_HANDLE(HDESK); + DECLARE_HANDLE(HENHMETAFILE); + DECLARE_HANDLE(HFONT); + DECLARE_HANDLE(HICON); + DECLARE_HANDLE(HMENU); + DECLARE_HANDLE(HMETAFILE); + DECLARE_HANDLE(HINSTANCE); + typedef HINSTANCE HMODULE; + DECLARE_HANDLE(HPALETTE); + DECLARE_HANDLE(HPEN); + DECLARE_HANDLE(HRGN); + DECLARE_HANDLE(HRSRC); + DECLARE_HANDLE(HSTR); + DECLARE_HANDLE(HTASK); + DECLARE_HANDLE(HWINSTA); + DECLARE_HANDLE(HKL); + DECLARE_HANDLE(HMONITOR); + DECLARE_HANDLE(HWINEVENTHOOK); + DECLARE_HANDLE(HUMPD); + + typedef int HFILE; + typedef HICON HCURSOR; + typedef DWORD COLORREF; + typedef DWORD *LPCOLORREF; + +#define HFILE_ERROR ((HFILE)-1) + + typedef struct tagRECT { + LONG left; + LONG top; + LONG right; + LONG bottom; + } RECT,*PRECT,*NPRECT,*LPRECT; + + typedef const RECT *LPCRECT; + + typedef struct _RECTL { + LONG left; + LONG top; + LONG right; + LONG bottom; + } RECTL,*PRECTL,*LPRECTL; + + typedef const RECTL *LPCRECTL; + + typedef struct tagPOINT { + LONG x; + LONG y; + } POINT,*PPOINT,*NPPOINT,*LPPOINT; + + typedef struct _POINTL { + LONG x; + LONG y; + } POINTL,*PPOINTL; + + typedef struct tagSIZE { + LONG cx; + LONG cy; + } SIZE,*PSIZE,*LPSIZE; + + typedef SIZE SIZEL; + typedef SIZE *PSIZEL,*LPSIZEL; + + typedef struct tagPOINTS { + SHORT x; + SHORT y; + } POINTS,*PPOINTS,*LPPOINTS; + + typedef struct _FILETIME { + DWORD dwLowDateTime; + DWORD dwHighDateTime; + } FILETIME,*PFILETIME,*LPFILETIME; +#define _FILETIME_ + +#define DM_UPDATE 1 +#define DM_COPY 2 +#define DM_PROMPT 4 +#define DM_MODIFY 8 + +#define DM_IN_BUFFER DM_MODIFY +#define DM_IN_PROMPT DM_PROMPT +#define DM_OUT_BUFFER DM_COPY +#define DM_OUT_DEFAULT DM_UPDATE + +#define DC_FIELDS 1 +#define DC_PAPERS 2 +#define DC_PAPERSIZE 3 +#define DC_MINEXTENT 4 +#define DC_MAXEXTENT 5 +#define DC_BINS 6 +#define DC_DUPLEX 7 +#define DC_SIZE 8 +#define DC_EXTRA 9 +#define DC_VERSION 10 +#define DC_DRIVER 11 +#define DC_BINNAMES 12 +#define DC_ENUMRESOLUTIONS 13 +#define DC_FILEDEPENDENCIES 14 +#define DC_TRUETYPE 15 +#define DC_PAPERNAMES 16 +#define DC_ORIENTATION 17 +#define DC_COPIES 18 + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/windows.h b/05/tcc-final-old/win32/include/winapi/windows.h new file mode 100644 index 0000000..2660d7f --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/windows.h @@ -0,0 +1,127 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINDOWS_ +#define _WINDOWS_ + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif + +#ifndef WINVER +#define WINVER 0x0502 +#endif + +#include <_mingw.h> + +#ifndef _INC_WINDOWS +#define _INC_WINDOWS + +#if defined(RC_INVOKED) && !defined(NOWINRES) + +#include +#else + +#ifdef RC_INVOKED +#define NOATOM +#define NOGDI +#define NOGDICAPMASKS +#define NOMETAFILE +#define NOMINMAX +#define NOMSG +#define NOOPENFILE +#define NORASTEROPS +#define NOSCROLL +#define NOSOUND +#define NOSYSMETRICS +#define NOTEXTMETRIC +#define NOWH +#define NOCOMM +#define NOKANJI +#define NOCRYPT +#define NOMCX +#endif + +#if !defined(I_X86_) && !defined(_IA64_) && !defined(_AMD64_) && (defined(_X86_) && !defined(__x86_64)) +#define I_X86_ +#endif + +#if !defined(I_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(__x86_64) +#define _AMD64_ +#endif + +#if !defined(I_X86_) && !(defined(_X86_) && !defined(__x86_64)) && !defined(_AMD64_) && defined(__ia64__) +#if !defined(_IA64_) +#define _IA64_ +#endif +#endif + +#ifndef RC_INVOKED +#include +#include +#endif + +#include +#include +#include +#include +//gr #include +#include +#include +#include +//gr #include + +#ifndef WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef NOCRYPT +#include +#include +#include +#endif + +#ifndef NOUSER +#ifndef NOGDI +#include +#ifdef INC_OLE1 +#include +#else +#include +#endif +#include +#endif +#endif +#endif + +//gr #include + +#ifdef INC_OLE2 +#include +#endif + +#ifndef NOSERVICE +#include +#endif + +#ifndef NOMCX +#include +#endif + +#ifndef NOIME +#include +#endif + +#endif +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/winerror.h b/05/tcc-final-old/win32/include/winapi/winerror.h new file mode 100644 index 0000000..77d85ed --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/winerror.h @@ -0,0 +1,3166 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINERROR_ +#define _WINERROR_ + +#define FACILITY_WINDOWSUPDATE 36 +#define FACILITY_WINDOWS_CE 24 +#define FACILITY_WINDOWS 8 +#define FACILITY_URT 19 +#define FACILITY_UMI 22 +#define FACILITY_SXS 23 +#define FACILITY_STORAGE 3 +#define FACILITY_STATE_MANAGEMENT 34 +#define FACILITY_SSPI 9 +#define FACILITY_SCARD 16 +#define FACILITY_SETUPAPI 15 +#define FACILITY_SECURITY 9 +#define FACILITY_RPC 1 +#define FACILITY_WIN32 7 +#define FACILITY_CONTROL 10 +#define FACILITY_NULL 0 +#define FACILITY_METADIRECTORY 35 +#define FACILITY_MSMQ 14 +#define FACILITY_MEDIASERVER 13 +#define FACILITY_INTERNET 12 +#define FACILITY_ITF 4 +#define FACILITY_HTTP 25 +#define FACILITY_DPLAY 21 +#define FACILITY_DISPATCH 2 +#define FACILITY_DIRECTORYSERVICE 37 +#define FACILITY_CONFIGURATION 33 +#define FACILITY_COMPLUS 17 +#define FACILITY_CERT 11 +#define FACILITY_BACKGROUNDCOPY 32 +#define FACILITY_ACS 20 +#define FACILITY_AAF 18 +#define ERROR_SUCCESS 0L +#define NO_ERROR 0L +#define SEC_E_OK ((HRESULT)0x00000000L) +#define ERROR_INVALID_FUNCTION 1L +#define ERROR_FILE_NOT_FOUND 2L +#define ERROR_PATH_NOT_FOUND 3L +#define ERROR_TOO_MANY_OPEN_FILES 4L +#define ERROR_ACCESS_DENIED 5L +#define ERROR_INVALID_HANDLE 6L +#define ERROR_ARENA_TRASHED 7L +#define ERROR_NOT_ENOUGH_MEMORY 8L +#define ERROR_INVALID_BLOCK 9L +#define ERROR_BAD_ENVIRONMENT 10L +#define ERROR_BAD_FORMAT 11L +#define ERROR_INVALID_ACCESS 12L +#define ERROR_INVALID_DATA 13L +#define ERROR_OUTOFMEMORY 14L +#define ERROR_INVALID_DRIVE 15L +#define ERROR_CURRENT_DIRECTORY 16L +#define ERROR_NOT_SAME_DEVICE 17L +#define ERROR_NO_MORE_FILES 18L +#define ERROR_WRITE_PROTECT 19L +#define ERROR_BAD_UNIT 20L +#define ERROR_NOT_READY 21L +#define ERROR_BAD_COMMAND 22L +#define ERROR_CRC 23L +#define ERROR_BAD_LENGTH 24L +#define ERROR_SEEK 25L +#define ERROR_NOT_DOS_DISK 26L +#define ERROR_SECTOR_NOT_FOUND 27L +#define ERROR_OUT_OF_PAPER 28L +#define ERROR_WRITE_FAULT 29L +#define ERROR_READ_FAULT 30L +#define ERROR_GEN_FAILURE 31L +#define ERROR_SHARING_VIOLATION 32L +#define ERROR_LOCK_VIOLATION 33L +#define ERROR_WRONG_DISK 34L +#define ERROR_SHARING_BUFFER_EXCEEDED 36L +#define ERROR_HANDLE_EOF 38L +#define ERROR_HANDLE_DISK_FULL 39L +#define ERROR_NOT_SUPPORTED 50L +#define ERROR_REM_NOT_LIST 51L +#define ERROR_DUP_NAME 52L +#define ERROR_BAD_NETPATH 53L +#define ERROR_NETWORK_BUSY 54L +#define ERROR_DEV_NOT_EXIST 55L +#define ERROR_TOO_MANY_CMDS 56L +#define ERROR_ADAP_HDW_ERR 57L +#define ERROR_BAD_NET_RESP 58L +#define ERROR_UNEXP_NET_ERR 59L +#define ERROR_BAD_REM_ADAP 60L +#define ERROR_PRINTQ_FULL 61L +#define ERROR_NO_SPOOL_SPACE 62L +#define ERROR_PRINT_CANCELLED 63L +#define ERROR_NETNAME_DELETED 64L +#define ERROR_NETWORK_ACCESS_DENIED 65L +#define ERROR_BAD_DEV_TYPE 66L +#define ERROR_BAD_NET_NAME 67L +#define ERROR_TOO_MANY_NAMES 68L +#define ERROR_TOO_MANY_SESS 69L +#define ERROR_SHARING_PAUSED 70L +#define ERROR_REQ_NOT_ACCEP 71L +#define ERROR_REDIR_PAUSED 72L +#define ERROR_FILE_EXISTS 80L +#define ERROR_CANNOT_MAKE 82L +#define ERROR_FAIL_I24 83L +#define ERROR_OUT_OF_STRUCTURES 84L +#define ERROR_ALREADY_ASSIGNED 85L +#define ERROR_INVALID_PASSWORD 86L +#define ERROR_INVALID_PARAMETER 87L +#define ERROR_NET_WRITE_FAULT 88L +#define ERROR_NO_PROC_SLOTS 89L +#define ERROR_TOO_MANY_SEMAPHORES 100L +#define ERROR_EXCL_SEM_ALREADY_OWNED 101L +#define ERROR_SEM_IS_SET 102L +#define ERROR_TOO_MANY_SEM_REQUESTS 103L +#define ERROR_INVALID_AT_INTERRUPT_TIME 104L +#define ERROR_SEM_OWNER_DIED 105L +#define ERROR_SEM_USER_LIMIT 106L +#define ERROR_DISK_CHANGE 107L +#define ERROR_DRIVE_LOCKED 108L +#define ERROR_BROKEN_PIPE 109L +#define ERROR_OPEN_FAILED 110L +#define ERROR_BUFFER_OVERFLOW 111L +#define ERROR_DISK_FULL 112L +#define ERROR_NO_MORE_SEARCH_HANDLES 113L +#define ERROR_INVALID_TARGET_HANDLE 114L +#define ERROR_INVALID_CATEGORY 117L +#define ERROR_INVALID_VERIFY_SWITCH 118L +#define ERROR_BAD_DRIVER_LEVEL 119L +#define ERROR_CALL_NOT_IMPLEMENTED 120L +#define ERROR_SEM_TIMEOUT 121L +#define ERROR_INSUFFICIENT_BUFFER 122L +#define ERROR_INVALID_NAME 123L +#define ERROR_INVALID_LEVEL 124L +#define ERROR_NO_VOLUME_LABEL 125L +#define ERROR_MOD_NOT_FOUND 126L +#define ERROR_PROC_NOT_FOUND 127L +#define ERROR_WAIT_NO_CHILDREN 128L +#define ERROR_CHILD_NOT_COMPLETE 129L +#define ERROR_DIRECT_ACCESS_HANDLE 130L +#define ERROR_NEGATIVE_SEEK 131L +#define ERROR_SEEK_ON_DEVICE 132L +#define ERROR_IS_JOIN_TARGET 133L +#define ERROR_IS_JOINED 134L +#define ERROR_IS_SUBSTED 135L +#define ERROR_NOT_JOINED 136L +#define ERROR_NOT_SUBSTED 137L +#define ERROR_JOIN_TO_JOIN 138L +#define ERROR_SUBST_TO_SUBST 139L +#define ERROR_JOIN_TO_SUBST 140L +#define ERROR_SUBST_TO_JOIN 141L +#define ERROR_BUSY_DRIVE 142L +#define ERROR_SAME_DRIVE 143L +#define ERROR_DIR_NOT_ROOT 144L +#define ERROR_DIR_NOT_EMPTY 145L +#define ERROR_IS_SUBST_PATH 146L +#define ERROR_IS_JOIN_PATH 147L +#define ERROR_PATH_BUSY 148L +#define ERROR_IS_SUBST_TARGET 149L +#define ERROR_SYSTEM_TRACE 150L +#define ERROR_INVALID_EVENT_COUNT 151L +#define ERROR_TOO_MANY_MUXWAITERS 152L +#define ERROR_INVALID_LIST_FORMAT 153L +#define ERROR_LABEL_TOO_LONG 154L +#define ERROR_TOO_MANY_TCBS 155L +#define ERROR_SIGNAL_REFUSED 156L +#define ERROR_DISCARDED 157L +#define ERROR_NOT_LOCKED 158L +#define ERROR_BAD_THREADID_ADDR 159L +#define ERROR_BAD_ARGUMENTS 160L +#define ERROR_BAD_PATHNAME 161L +#define ERROR_SIGNAL_PENDING 162L +#define ERROR_MAX_THRDS_REACHED 164L +#define ERROR_LOCK_FAILED 167L +#define ERROR_BUSY 170L +#define ERROR_CANCEL_VIOLATION 173L +#define ERROR_ATOMIC_LOCKS_NOT_SUPPORTED 174L +#define ERROR_INVALID_SEGMENT_NUMBER 180L +#define ERROR_INVALID_ORDINAL 182L +#define ERROR_ALREADY_EXISTS 183L +#define ERROR_INVALID_FLAG_NUMBER 186L +#define ERROR_SEM_NOT_FOUND 187L +#define ERROR_INVALID_STARTING_CODESEG 188L +#define ERROR_INVALID_STACKSEG 189L +#define ERROR_INVALID_MODULETYPE 190L +#define ERROR_INVALID_EXE_SIGNATURE 191L +#define ERROR_EXE_MARKED_INVALID 192L +#define ERROR_BAD_EXE_FORMAT 193L +#define ERROR_ITERATED_DATA_EXCEEDS_64k 194L +#define ERROR_INVALID_MINALLOCSIZE 195L +#define ERROR_DYNLINK_FROM_INVALID_RING 196L +#define ERROR_IOPL_NOT_ENABLED 197L +#define ERROR_INVALID_SEGDPL 198L +#define ERROR_AUTODATASEG_EXCEEDS_64k 199L +#define ERROR_RING2SEG_MUST_BE_MOVABLE 200L +#define ERROR_RELOC_CHAIN_XEEDS_SEGLIM 201L +#define ERROR_INFLOOP_IN_RELOC_CHAIN 202L +#define ERROR_ENVVAR_NOT_FOUND 203L +#define ERROR_NO_SIGNAL_SENT 205L +#define ERROR_FILENAME_EXCED_RANGE 206L +#define ERROR_RING2_STACK_IN_USE 207L +#define ERROR_META_EXPANSION_TOO_LONG 208L +#define ERROR_INVALID_SIGNAL_NUMBER 209L +#define ERROR_THREAD_1_INACTIVE 210L +#define ERROR_LOCKED 212L +#define ERROR_TOO_MANY_MODULES 214L +#define ERROR_NESTING_NOT_ALLOWED 215L +#define ERROR_EXE_MACHINE_TYPE_MISMATCH 216L +#define ERROR_EXE_CANNOT_MODIFY_SIGNED_BINARY 217L +#define ERROR_EXE_CANNOT_MODIFY_STRONG_SIGNED_BINARY 218L +#define ERROR_BAD_PIPE 230L +#define ERROR_PIPE_BUSY 231L +#define ERROR_NO_DATA 232L +#define ERROR_PIPE_NOT_CONNECTED 233L +#define ERROR_MORE_DATA 234L +#define ERROR_VC_DISCONNECTED 240L +#define ERROR_INVALID_EA_NAME 254L +#define ERROR_EA_LIST_INCONSISTENT 255L +#define WAIT_TIMEOUT 258L +#define ERROR_NO_MORE_ITEMS 259L +#define ERROR_CANNOT_COPY 266L +#define ERROR_DIRECTORY 267L +#define ERROR_EAS_DIDNT_FIT 275L +#define ERROR_EA_FILE_CORRUPT 276L +#define ERROR_EA_TABLE_FULL 277L +#define ERROR_INVALID_EA_HANDLE 278L +#define ERROR_EAS_NOT_SUPPORTED 282L +#define ERROR_NOT_OWNER 288L +#define ERROR_TOO_MANY_POSTS 298L +#define ERROR_PARTIAL_COPY 299L +#define ERROR_OPLOCK_NOT_GRANTED 300L +#define ERROR_INVALID_OPLOCK_PROTOCOL 301L +#define ERROR_DISK_TOO_FRAGMENTED 302L +#define ERROR_DELETE_PENDING 303L +#define ERROR_MR_MID_NOT_FOUND 317L +#define ERROR_SCOPE_NOT_FOUND 318L +#define ERROR_INVALID_ADDRESS 487L +#define ERROR_ARITHMETIC_OVERFLOW 534L +#define ERROR_PIPE_CONNECTED 535L +#define ERROR_PIPE_LISTENING 536L +#define ERROR_EA_ACCESS_DENIED 994L +#define ERROR_OPERATION_ABORTED 995L +#define ERROR_IO_INCOMPLETE 996L +#define ERROR_IO_PENDING 997L +#define ERROR_NOACCESS 998L +#define ERROR_SWAPERROR 999L +#define ERROR_STACK_OVERFLOW 1001L +#define ERROR_INVALID_MESSAGE 1002L +#define ERROR_CAN_NOT_COMPLETE 1003L +#define ERROR_INVALID_FLAGS 1004L +#define ERROR_UNRECOGNIZED_VOLUME 1005L +#define ERROR_FILE_INVALID 1006L +#define ERROR_FULLSCREEN_MODE 1007L +#define ERROR_NO_TOKEN 1008L +#define ERROR_BADDB 1009L +#define ERROR_BADKEY 1010L +#define ERROR_CANTOPEN 1011L +#define ERROR_CANTREAD 1012L +#define ERROR_CANTWRITE 1013L +#define ERROR_REGISTRY_RECOVERED 1014L +#define ERROR_REGISTRY_CORRUPT 1015L +#define ERROR_REGISTRY_IO_FAILED 1016L +#define ERROR_NOT_REGISTRY_FILE 1017L +#define ERROR_KEY_DELETED 1018L +#define ERROR_NO_LOG_SPACE 1019L +#define ERROR_KEY_HAS_CHILDREN 1020L +#define ERROR_CHILD_MUST_BE_VOLATILE 1021L +#define ERROR_NOTIFY_ENUM_DIR 1022L +#define ERROR_DEPENDENT_SERVICES_RUNNING 1051L +#define ERROR_INVALID_SERVICE_CONTROL 1052L +#define ERROR_SERVICE_REQUEST_TIMEOUT 1053L +#define ERROR_SERVICE_NO_THREAD 1054L +#define ERROR_SERVICE_DATABASE_LOCKED 1055L +#define ERROR_SERVICE_ALREADY_RUNNING 1056L +#define ERROR_INVALID_SERVICE_ACCOUNT 1057L +#define ERROR_SERVICE_DISABLED 1058L +#define ERROR_CIRCULAR_DEPENDENCY 1059L +#define ERROR_SERVICE_DOES_NOT_EXIST 1060L +#define ERROR_SERVICE_CANNOT_ACCEPT_CTRL 1061L +#define ERROR_SERVICE_NOT_ACTIVE 1062L +#define ERROR_FAILED_SERVICE_CONTROLLER_CONNECT 1063L +#define ERROR_EXCEPTION_IN_SERVICE 1064L +#define ERROR_DATABASE_DOES_NOT_EXIST 1065L +#define ERROR_SERVICE_SPECIFIC_ERROR 1066L +#define ERROR_PROCESS_ABORTED 1067L +#define ERROR_SERVICE_DEPENDENCY_FAIL 1068L +#define ERROR_SERVICE_LOGON_FAILED 1069L +#define ERROR_SERVICE_START_HANG 1070L +#define ERROR_INVALID_SERVICE_LOCK 1071L +#define ERROR_SERVICE_MARKED_FOR_DELETE 1072L +#define ERROR_SERVICE_EXISTS 1073L +#define ERROR_ALREADY_RUNNING_LKG 1074L +#define ERROR_SERVICE_DEPENDENCY_DELETED 1075L +#define ERROR_BOOT_ALREADY_ACCEPTED 1076L +#define ERROR_SERVICE_NEVER_STARTED 1077L +#define ERROR_DUPLICATE_SERVICE_NAME 1078L +#define ERROR_DIFFERENT_SERVICE_ACCOUNT 1079L +#define ERROR_CANNOT_DETECT_DRIVER_FAILURE 1080L +#define ERROR_CANNOT_DETECT_PROCESS_ABORT 1081L +#define ERROR_NO_RECOVERY_PROGRAM 1082L +#define ERROR_SERVICE_NOT_IN_EXE 1083L +#define ERROR_NOT_SAFEBOOT_SERVICE 1084L +#define ERROR_END_OF_MEDIA 1100L +#define ERROR_FILEMARK_DETECTED 1101L +#define ERROR_BEGINNING_OF_MEDIA 1102L +#define ERROR_SETMARK_DETECTED 1103L +#define ERROR_NO_DATA_DETECTED 1104L +#define ERROR_PARTITION_FAILURE 1105L +#define ERROR_INVALID_BLOCK_LENGTH 1106L +#define ERROR_DEVICE_NOT_PARTITIONED 1107L +#define ERROR_UNABLE_TO_LOCK_MEDIA 1108L +#define ERROR_UNABLE_TO_UNLOAD_MEDIA 1109L +#define ERROR_MEDIA_CHANGED 1110L +#define ERROR_BUS_RESET 1111L +#define ERROR_NO_MEDIA_IN_DRIVE 1112L +#define ERROR_NO_UNICODE_TRANSLATION 1113L +#define ERROR_DLL_INIT_FAILED 1114L +#define ERROR_SHUTDOWN_IN_PROGRESS 1115L +#define ERROR_NO_SHUTDOWN_IN_PROGRESS 1116L +#define ERROR_IO_DEVICE 1117L +#define ERROR_SERIAL_NO_DEVICE 1118L +#define ERROR_IRQ_BUSY 1119L +#define ERROR_MORE_WRITES 1120L +#define ERROR_COUNTER_TIMEOUT 1121L +#define ERROR_FLOPPY_ID_MARK_NOT_FOUND 1122L +#define ERROR_FLOPPY_WRONG_CYLINDER 1123L +#define ERROR_FLOPPY_UNKNOWN_ERROR 1124L +#define ERROR_FLOPPY_BAD_REGISTERS 1125L +#define ERROR_DISK_RECALIBRATE_FAILED 1126L +#define ERROR_DISK_OPERATION_FAILED 1127L +#define ERROR_DISK_RESET_FAILED 1128L +#define ERROR_EOM_OVERFLOW 1129L +#define ERROR_NOT_ENOUGH_SERVER_MEMORY 1130L +#define ERROR_POSSIBLE_DEADLOCK 1131L +#define ERROR_MAPPED_ALIGNMENT 1132L +#define ERROR_SET_POWER_STATE_VETOED 1140L +#define ERROR_SET_POWER_STATE_FAILED 1141L +#define ERROR_TOO_MANY_LINKS 1142L +#define ERROR_OLD_WIN_VERSION 1150L +#define ERROR_APP_WRONG_OS 1151L +#define ERROR_SINGLE_INSTANCE_APP 1152L +#define ERROR_RMODE_APP 1153L +#define ERROR_INVALID_DLL 1154L +#define ERROR_NO_ASSOCIATION 1155L +#define ERROR_DDE_FAIL 1156L +#define ERROR_DLL_NOT_FOUND 1157L +#define ERROR_NO_MORE_USER_HANDLES 1158L +#define ERROR_MESSAGE_SYNC_ONLY 1159L +#define ERROR_SOURCE_ELEMENT_EMPTY 1160L +#define ERROR_DESTINATION_ELEMENT_FULL 1161L +#define ERROR_ILLEGAL_ELEMENT_ADDRESS 1162L +#define ERROR_MAGAZINE_NOT_PRESENT 1163L +#define ERROR_DEVICE_REINITIALIZATION_NEEDED 1164L +#define ERROR_DEVICE_REQUIRES_CLEANING 1165L +#define ERROR_DEVICE_DOOR_OPEN 1166L +#define ERROR_DEVICE_NOT_CONNECTED 1167L +#define ERROR_NOT_FOUND 1168L +#define ERROR_NO_MATCH 1169L +#define ERROR_SET_NOT_FOUND 1170L +#define ERROR_POINT_NOT_FOUND 1171L +#define ERROR_NO_TRACKING_SERVICE 1172L +#define ERROR_NO_VOLUME_ID 1173L +#define ERROR_UNABLE_TO_REMOVE_REPLACED 1175L +#define ERROR_UNABLE_TO_MOVE_REPLACEMENT 1176L +#define ERROR_UNABLE_TO_MOVE_REPLACEMENT_2 1177L +#define ERROR_JOURNAL_DELETE_IN_PROGRESS 1178L +#define ERROR_JOURNAL_NOT_ACTIVE 1179L +#define ERROR_POTENTIAL_FILE_FOUND 1180L +#define ERROR_JOURNAL_ENTRY_DELETED 1181L +#define ERROR_BAD_DEVICE 1200L +#define ERROR_CONNECTION_UNAVAIL 1201L +#define ERROR_DEVICE_ALREADY_REMEMBERED 1202L +#define ERROR_NO_NET_OR_BAD_PATH 1203L +#define ERROR_BAD_PROVIDER 1204L +#define ERROR_CANNOT_OPEN_PROFILE 1205L +#define ERROR_BAD_PROFILE 1206L +#define ERROR_NOT_CONTAINER 1207L +#define ERROR_EXTENDED_ERROR 1208L +#define ERROR_INVALID_GROUPNAME 1209L +#define ERROR_INVALID_COMPUTERNAME 1210L +#define ERROR_INVALID_EVENTNAME 1211L +#define ERROR_INVALID_DOMAINNAME 1212L +#define ERROR_INVALID_SERVICENAME 1213L +#define ERROR_INVALID_NETNAME 1214L +#define ERROR_INVALID_SHARENAME 1215L +#define ERROR_INVALID_PASSWORDNAME 1216L +#define ERROR_INVALID_MESSAGENAME 1217L +#define ERROR_INVALID_MESSAGEDEST 1218L +#define ERROR_SESSION_CREDENTIAL_CONFLICT 1219L +#define ERROR_REMOTE_SESSION_LIMIT_EXCEEDED 1220L +#define ERROR_DUP_DOMAINNAME 1221L +#define ERROR_NO_NETWORK 1222L +#define ERROR_CANCELLED 1223L +#define ERROR_USER_MAPPED_FILE 1224L +#define ERROR_CONNECTION_REFUSED 1225L +#define ERROR_GRACEFUL_DISCONNECT 1226L +#define ERROR_ADDRESS_ALREADY_ASSOCIATED 1227L +#define ERROR_ADDRESS_NOT_ASSOCIATED 1228L +#define ERROR_CONNECTION_INVALID 1229L +#define ERROR_CONNECTION_ACTIVE 1230L +#define ERROR_NETWORK_UNREACHABLE 1231L +#define ERROR_HOST_UNREACHABLE 1232L +#define ERROR_PROTOCOL_UNREACHABLE 1233L +#define ERROR_PORT_UNREACHABLE 1234L +#define ERROR_REQUEST_ABORTED 1235L +#define ERROR_CONNECTION_ABORTED 1236L +#define ERROR_RETRY 1237L +#define ERROR_CONNECTION_COUNT_LIMIT 1238L +#define ERROR_LOGIN_TIME_RESTRICTION 1239L +#define ERROR_LOGIN_WKSTA_RESTRICTION 1240L +#define ERROR_INCORRECT_ADDRESS 1241L +#define ERROR_ALREADY_REGISTERED 1242L +#define ERROR_SERVICE_NOT_FOUND 1243L +#define ERROR_NOT_AUTHENTICATED 1244L +#define ERROR_NOT_LOGGED_ON 1245L +#define ERROR_CONTINUE 1246L +#define ERROR_ALREADY_INITIALIZED 1247L +#define ERROR_NO_MORE_DEVICES 1248L +#define ERROR_NO_SUCH_SITE 1249L +#define ERROR_DOMAIN_CONTROLLER_EXISTS 1250L +#define ERROR_ONLY_IF_CONNECTED 1251L +#define ERROR_OVERRIDE_NOCHANGES 1252L +#define ERROR_BAD_USER_PROFILE 1253L +#define ERROR_NOT_SUPPORTED_ON_SBS 1254L +#define ERROR_SERVER_SHUTDOWN_IN_PROGRESS 1255L +#define ERROR_HOST_DOWN 1256L +#define ERROR_NON_ACCOUNT_SID 1257L +#define ERROR_NON_DOMAIN_SID 1258L +#define ERROR_APPHELP_BLOCK 1259L +#define ERROR_ACCESS_DISABLED_BY_POLICY 1260L +#define ERROR_REG_NAT_CONSUMPTION 1261L +#define ERROR_CSCSHARE_OFFLINE 1262L +#define ERROR_PKINIT_FAILURE 1263L +#define ERROR_SMARTCARD_SUBSYSTEM_FAILURE 1264L +#define ERROR_DOWNGRADE_DETECTED 1265L +#define ERROR_MACHINE_LOCKED 1271L +#define ERROR_CALLBACK_SUPPLIED_INVALID_DATA 1273L +#define ERROR_SYNC_FOREGROUND_REFRESH_REQUIRED 1274L +#define ERROR_DRIVER_BLOCKED 1275L +#define ERROR_INVALID_IMPORT_OF_NON_DLL 1276L +#define ERROR_ACCESS_DISABLED_WEBBLADE 1277L +#define ERROR_ACCESS_DISABLED_WEBBLADE_TAMPER 1278L +#define ERROR_RECOVERY_FAILURE 1279L +#define ERROR_ALREADY_FIBER 1280L +#define ERROR_ALREADY_THREAD 1281L +#define ERROR_STACK_BUFFER_OVERRUN 1282L +#define ERROR_PARAMETER_QUOTA_EXCEEDED 1283L +#define ERROR_DEBUGGER_INACTIVE 1284L +#define ERROR_DELAY_LOAD_FAILED 1285L +#define ERROR_VDM_DISALLOWED 1286L +#define ERROR_UNIDENTIFIED_ERROR 1287L +#define ERROR_NOT_ALL_ASSIGNED 1300L +#define ERROR_SOME_NOT_MAPPED 1301L +#define ERROR_NO_QUOTAS_FOR_ACCOUNT 1302L +#define ERROR_LOCAL_USER_SESSION_KEY 1303L +#define ERROR_NULL_LM_PASSWORD 1304L +#define ERROR_UNKNOWN_REVISION 1305L +#define ERROR_REVISION_MISMATCH 1306L +#define ERROR_INVALID_OWNER 1307L +#define ERROR_INVALID_PRIMARY_GROUP 1308L +#define ERROR_NO_IMPERSONATION_TOKEN 1309L +#define ERROR_CANT_DISABLE_MANDATORY 1310L +#define ERROR_NO_LOGON_SERVERS 1311L +#define ERROR_NO_SUCH_LOGON_SESSION 1312L +#define ERROR_NO_SUCH_PRIVILEGE 1313L +#define ERROR_PRIVILEGE_NOT_HELD 1314L +#define ERROR_INVALID_ACCOUNT_NAME 1315L +#define ERROR_USER_EXISTS 1316L +#define ERROR_NO_SUCH_USER 1317L +#define ERROR_GROUP_EXISTS 1318L +#define ERROR_NO_SUCH_GROUP 1319L +#define ERROR_MEMBER_IN_GROUP 1320L +#define ERROR_MEMBER_NOT_IN_GROUP 1321L +#define ERROR_LAST_ADMIN 1322L +#define ERROR_WRONG_PASSWORD 1323L +#define ERROR_ILL_FORMED_PASSWORD 1324L +#define ERROR_PASSWORD_RESTRICTION 1325L +#define ERROR_LOGON_FAILURE 1326L +#define ERROR_ACCOUNT_RESTRICTION 1327L +#define ERROR_INVALID_LOGON_HOURS 1328L +#define ERROR_INVALID_WORKSTATION 1329L +#define ERROR_PASSWORD_EXPIRED 1330L +#define ERROR_ACCOUNT_DISABLED 1331L +#define ERROR_NONE_MAPPED 1332L +#define ERROR_TOO_MANY_LUIDS_REQUESTED 1333L +#define ERROR_LUIDS_EXHAUSTED 1334L +#define ERROR_INVALID_SUB_AUTHORITY 1335L +#define ERROR_INVALID_ACL 1336L +#define ERROR_INVALID_SID 1337L +#define ERROR_INVALID_SECURITY_DESCR 1338L +#define ERROR_BAD_INHERITANCE_ACL 1340L +#define ERROR_SERVER_DISABLED 1341L +#define ERROR_SERVER_NOT_DISABLED 1342L +#define ERROR_INVALID_ID_AUTHORITY 1343L +#define ERROR_ALLOTTED_SPACE_EXCEEDED 1344L +#define ERROR_INVALID_GROUP_ATTRIBUTES 1345L +#define ERROR_BAD_IMPERSONATION_LEVEL 1346L +#define ERROR_CANT_OPEN_ANONYMOUS 1347L +#define ERROR_BAD_VALIDATION_CLASS 1348L +#define ERROR_BAD_TOKEN_TYPE 1349L +#define ERROR_NO_SECURITY_ON_OBJECT 1350L +#define ERROR_CANT_ACCESS_DOMAIN_INFO 1351L +#define ERROR_INVALID_SERVER_STATE 1352L +#define ERROR_INVALID_DOMAIN_STATE 1353L +#define ERROR_INVALID_DOMAIN_ROLE 1354L +#define ERROR_NO_SUCH_DOMAIN 1355L +#define ERROR_DOMAIN_EXISTS 1356L +#define ERROR_DOMAIN_LIMIT_EXCEEDED 1357L +#define ERROR_INTERNAL_DB_CORRUPTION 1358L +#define ERROR_INTERNAL_ERROR 1359L +#define ERROR_GENERIC_NOT_MAPPED 1360L +#define ERROR_BAD_DESCRIPTOR_FORMAT 1361L +#define ERROR_NOT_LOGON_PROCESS 1362L +#define ERROR_LOGON_SESSION_EXISTS 1363L +#define ERROR_NO_SUCH_PACKAGE 1364L +#define ERROR_BAD_LOGON_SESSION_STATE 1365L +#define ERROR_LOGON_SESSION_COLLISION 1366L +#define ERROR_INVALID_LOGON_TYPE 1367L +#define ERROR_CANNOT_IMPERSONATE 1368L +#define ERROR_RXACT_INVALID_STATE 1369L +#define ERROR_RXACT_COMMIT_FAILURE 1370L +#define ERROR_SPECIAL_ACCOUNT 1371L +#define ERROR_SPECIAL_GROUP 1372L +#define ERROR_SPECIAL_USER 1373L +#define ERROR_MEMBERS_PRIMARY_GROUP 1374L +#define ERROR_TOKEN_ALREADY_IN_USE 1375L +#define ERROR_NO_SUCH_ALIAS 1376L +#define ERROR_MEMBER_NOT_IN_ALIAS 1377L +#define ERROR_MEMBER_IN_ALIAS 1378L +#define ERROR_ALIAS_EXISTS 1379L +#define ERROR_LOGON_NOT_GRANTED 1380L +#define ERROR_TOO_MANY_SECRETS 1381L +#define ERROR_SECRET_TOO_LONG 1382L +#define ERROR_INTERNAL_DB_ERROR 1383L +#define ERROR_TOO_MANY_CONTEXT_IDS 1384L +#define ERROR_LOGON_TYPE_NOT_GRANTED 1385L +#define ERROR_NT_CROSS_ENCRYPTION_REQUIRED 1386L +#define ERROR_NO_SUCH_MEMBER 1387L +#define ERROR_INVALID_MEMBER 1388L +#define ERROR_TOO_MANY_SIDS 1389L +#define ERROR_LM_CROSS_ENCRYPTION_REQUIRED 1390L +#define ERROR_NO_INHERITANCE 1391L +#define ERROR_FILE_CORRUPT 1392L +#define ERROR_DISK_CORRUPT 1393L +#define ERROR_NO_USER_SESSION_KEY 1394L +#define ERROR_LICENSE_QUOTA_EXCEEDED 1395L +#define ERROR_WRONG_TARGET_NAME 1396L +#define ERROR_MUTUAL_AUTH_FAILED 1397L +#define ERROR_TIME_SKEW 1398L +#define ERROR_CURRENT_DOMAIN_NOT_ALLOWED 1399L +#define ERROR_INVALID_WINDOW_HANDLE 1400L +#define ERROR_INVALID_MENU_HANDLE 1401L +#define ERROR_INVALID_CURSOR_HANDLE 1402L +#define ERROR_INVALID_ACCEL_HANDLE 1403L +#define ERROR_INVALID_HOOK_HANDLE 1404L +#define ERROR_INVALID_DWP_HANDLE 1405L +#define ERROR_TLW_WITH_WSCHILD 1406L +#define ERROR_CANNOT_FIND_WND_CLASS 1407L +#define ERROR_WINDOW_OF_OTHER_THREAD 1408L +#define ERROR_HOTKEY_ALREADY_REGISTERED 1409L +#define ERROR_CLASS_ALREADY_EXISTS 1410L +#define ERROR_CLASS_DOES_NOT_EXIST 1411L +#define ERROR_CLASS_HAS_WINDOWS 1412L +#define ERROR_INVALID_INDEX 1413L +#define ERROR_INVALID_ICON_HANDLE 1414L +#define ERROR_PRIVATE_DIALOG_INDEX 1415L +#define ERROR_LISTBOX_ID_NOT_FOUND 1416L +#define ERROR_NO_WILDCARD_CHARACTERS 1417L +#define ERROR_CLIPBOARD_NOT_OPEN 1418L +#define ERROR_HOTKEY_NOT_REGISTERED 1419L +#define ERROR_WINDOW_NOT_DIALOG 1420L +#define ERROR_CONTROL_ID_NOT_FOUND 1421L +#define ERROR_INVALID_COMBOBOX_MESSAGE 1422L +#define ERROR_WINDOW_NOT_COMBOBOX 1423L +#define ERROR_INVALID_EDIT_HEIGHT 1424L +#define ERROR_DC_NOT_FOUND 1425L +#define ERROR_INVALID_HOOK_FILTER 1426L +#define ERROR_INVALID_FILTER_PROC 1427L +#define ERROR_HOOK_NEEDS_HMOD 1428L +#define ERROR_GLOBAL_ONLY_HOOK 1429L +#define ERROR_JOURNAL_HOOK_SET 1430L +#define ERROR_HOOK_NOT_INSTALLED 1431L +#define ERROR_INVALID_LB_MESSAGE 1432L +#define ERROR_SETCOUNT_ON_BAD_LB 1433L +#define ERROR_LB_WITHOUT_TABSTOPS 1434L +#define ERROR_DESTROY_OBJECT_OF_OTHER_THREAD 1435L +#define ERROR_CHILD_WINDOW_MENU 1436L +#define ERROR_NO_SYSTEM_MENU 1437L +#define ERROR_INVALID_MSGBOX_STYLE 1438L +#define ERROR_INVALID_SPI_VALUE 1439L +#define ERROR_SCREEN_ALREADY_LOCKED 1440L +#define ERROR_HWNDS_HAVE_DIFF_PARENT 1441L +#define ERROR_NOT_CHILD_WINDOW 1442L +#define ERROR_INVALID_GW_COMMAND 1443L +#define ERROR_INVALID_THREAD_ID 1444L +#define ERROR_NON_MDICHILD_WINDOW 1445L +#define ERROR_POPUP_ALREADY_ACTIVE 1446L +#define ERROR_NO_SCROLLBARS 1447L +#define ERROR_INVALID_SCROLLBAR_RANGE 1448L +#define ERROR_INVALID_SHOWWIN_COMMAND 1449L +#define ERROR_NO_SYSTEM_RESOURCES 1450L +#define ERROR_NONPAGED_SYSTEM_RESOURCES 1451L +#define ERROR_PAGED_SYSTEM_RESOURCES 1452L +#define ERROR_WORKING_SET_QUOTA 1453L +#define ERROR_PAGEFILE_QUOTA 1454L +#define ERROR_COMMITMENT_LIMIT 1455L +#define ERROR_MENU_ITEM_NOT_FOUND 1456L +#define ERROR_INVALID_KEYBOARD_HANDLE 1457L +#define ERROR_HOOK_TYPE_NOT_ALLOWED 1458L +#define ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION 1459L +#define ERROR_TIMEOUT 1460L +#define ERROR_INVALID_MONITOR_HANDLE 1461L +#define ERROR_INCORRECT_SIZE 1462L +#define ERROR_EVENTLOG_FILE_CORRUPT 1500L +#define ERROR_EVENTLOG_CANT_START 1501L +#define ERROR_LOG_FILE_FULL 1502L +#define ERROR_EVENTLOG_FILE_CHANGED 1503L +#define ERROR_INSTALL_SERVICE_FAILURE 1601L +#define ERROR_INSTALL_USEREXIT 1602L +#define ERROR_INSTALL_FAILURE 1603L +#define ERROR_INSTALL_SUSPEND 1604L +#define ERROR_UNKNOWN_PRODUCT 1605L +#define ERROR_UNKNOWN_FEATURE 1606L +#define ERROR_UNKNOWN_COMPONENT 1607L +#define ERROR_UNKNOWN_PROPERTY 1608L +#define ERROR_INVALID_HANDLE_STATE 1609L +#define ERROR_BAD_CONFIGURATION 1610L +#define ERROR_INDEX_ABSENT 1611L +#define ERROR_INSTALL_SOURCE_ABSENT 1612L +#define ERROR_INSTALL_PACKAGE_VERSION 1613L +#define ERROR_PRODUCT_UNINSTALLED 1614L +#define ERROR_BAD_QUERY_SYNTAX 1615L +#define ERROR_INVALID_FIELD 1616L +#define ERROR_DEVICE_REMOVED 1617L +#define ERROR_INSTALL_ALREADY_RUNNING 1618L +#define ERROR_INSTALL_PACKAGE_OPEN_FAILED 1619L +#define ERROR_INSTALL_PACKAGE_INVALID 1620L +#define ERROR_INSTALL_UI_FAILURE 1621L +#define ERROR_INSTALL_LOG_FAILURE 1622L +#define ERROR_INSTALL_LANGUAGE_UNSUPPORTED 1623L +#define ERROR_INSTALL_TRANSFORM_FAILURE 1624L +#define ERROR_INSTALL_PACKAGE_REJECTED 1625L +#define ERROR_FUNCTION_NOT_CALLED 1626L +#define ERROR_FUNCTION_FAILED 1627L +#define ERROR_INVALID_TABLE 1628L +#define ERROR_DATATYPE_MISMATCH 1629L +#define ERROR_UNSUPPORTED_TYPE 1630L +#define ERROR_CREATE_FAILED 1631L +#define ERROR_INSTALL_TEMP_UNWRITABLE 1632L +#define ERROR_INSTALL_PLATFORM_UNSUPPORTED 1633L +#define ERROR_INSTALL_NOTUSED 1634L +#define ERROR_PATCH_PACKAGE_OPEN_FAILED 1635L +#define ERROR_PATCH_PACKAGE_INVALID 1636L +#define ERROR_PATCH_PACKAGE_UNSUPPORTED 1637L +#define ERROR_PRODUCT_VERSION 1638L +#define ERROR_INVALID_COMMAND_LINE 1639L +#define ERROR_INSTALL_REMOTE_DISALLOWED 1640L +#define ERROR_SUCCESS_REBOOT_INITIATED 1641L +#define ERROR_PATCH_TARGET_NOT_FOUND 1642L +#define ERROR_PATCH_PACKAGE_REJECTED 1643L +#define ERROR_INSTALL_TRANSFORM_REJECTED 1644L +#define ERROR_INSTALL_REMOTE_PROHIBITED 1645L +#define RPC_S_INVALID_STRING_BINDING 1700L +#define RPC_S_WRONG_KIND_OF_BINDING 1701L +#define RPC_S_INVALID_BINDING 1702L +#define RPC_S_PROTSEQ_NOT_SUPPORTED 1703L +#define RPC_S_INVALID_RPC_PROTSEQ 1704L +#define RPC_S_INVALID_STRING_UUID 1705L +#define RPC_S_INVALID_ENDPOINT_FORMAT 1706L +#define RPC_S_INVALID_NET_ADDR 1707L +#define RPC_S_NO_ENDPOINT_FOUND 1708L +#define RPC_S_INVALID_TIMEOUT 1709L +#define RPC_S_OBJECT_NOT_FOUND 1710L +#define RPC_S_ALREADY_REGISTERED 1711L +#define RPC_S_TYPE_ALREADY_REGISTERED 1712L +#define RPC_S_ALREADY_LISTENING 1713L +#define RPC_S_NO_PROTSEQS_REGISTERED 1714L +#define RPC_S_NOT_LISTENING 1715L +#define RPC_S_UNKNOWN_MGR_TYPE 1716L +#define RPC_S_UNKNOWN_IF 1717L +#define RPC_S_NO_BINDINGS 1718L +#define RPC_S_NO_PROTSEQS 1719L +#define RPC_S_CANT_CREATE_ENDPOINT 1720L +#define RPC_S_OUT_OF_RESOURCES 1721L +#define RPC_S_SERVER_UNAVAILABLE 1722L +#define RPC_S_SERVER_TOO_BUSY 1723L +#define RPC_S_INVALID_NETWORK_OPTIONS 1724L +#define RPC_S_NO_CALL_ACTIVE 1725L +#define RPC_S_CALL_FAILED 1726L +#define RPC_S_CALL_FAILED_DNE 1727L +#define RPC_S_PROTOCOL_ERROR 1728L +#define RPC_S_UNSUPPORTED_TRANS_SYN 1730L +#define RPC_S_UNSUPPORTED_TYPE 1732L +#define RPC_S_INVALID_TAG 1733L +#define RPC_S_INVALID_BOUND 1734L +#define RPC_S_NO_ENTRY_NAME 1735L +#define RPC_S_INVALID_NAME_SYNTAX 1736L +#define RPC_S_UNSUPPORTED_NAME_SYNTAX 1737L +#define RPC_S_UUID_NO_ADDRESS 1739L +#define RPC_S_DUPLICATE_ENDPOINT 1740L +#define RPC_S_UNKNOWN_AUTHN_TYPE 1741L +#define RPC_S_MAX_CALLS_TOO_SMALL 1742L +#define RPC_S_STRING_TOO_LONG 1743L +#define RPC_S_PROTSEQ_NOT_FOUND 1744L +#define RPC_S_PROCNUM_OUT_OF_RANGE 1745L +#define RPC_S_BINDING_HAS_NO_AUTH 1746L +#define RPC_S_UNKNOWN_AUTHN_SERVICE 1747L +#define RPC_S_UNKNOWN_AUTHN_LEVEL 1748L +#define RPC_S_INVALID_AUTH_IDENTITY 1749L +#define RPC_S_UNKNOWN_AUTHZ_SERVICE 1750L +#define EPT_S_INVALID_ENTRY 1751L +#define EPT_S_CANT_PERFORM_OP 1752L +#define EPT_S_NOT_REGISTERED 1753L +#define RPC_S_NOTHING_TO_EXPORT 1754L +#define RPC_S_INCOMPLETE_NAME 1755L +#define RPC_S_INVALID_VERS_OPTION 1756L +#define RPC_S_NO_MORE_MEMBERS 1757L +#define RPC_S_NOT_ALL_OBJS_UNEXPORTED 1758L +#define RPC_S_INTERFACE_NOT_FOUND 1759L +#define RPC_S_ENTRY_ALREADY_EXISTS 1760L +#define RPC_S_ENTRY_NOT_FOUND 1761L +#define RPC_S_NAME_SERVICE_UNAVAILABLE 1762L +#define RPC_S_INVALID_NAF_ID 1763L +#define RPC_S_CANNOT_SUPPORT 1764L +#define RPC_S_NO_CONTEXT_AVAILABLE 1765L +#define RPC_S_INTERNAL_ERROR 1766L +#define RPC_S_ZERO_DIVIDE 1767L +#define RPC_S_ADDRESS_ERROR 1768L +#define RPC_S_FP_DIV_ZERO 1769L +#define RPC_S_FP_UNDERFLOW 1770L +#define RPC_S_FP_OVERFLOW 1771L +#define RPC_X_NO_MORE_ENTRIES 1772L +#define RPC_X_SS_CHAR_TRANS_OPEN_FAIL 1773L +#define RPC_X_SS_CHAR_TRANS_SHORT_FILE 1774L +#define RPC_X_SS_IN_NULL_CONTEXT 1775L +#define RPC_X_SS_CONTEXT_DAMAGED 1777L +#define RPC_X_SS_HANDLES_MISMATCH 1778L +#define RPC_X_SS_CANNOT_GET_CALL_HANDLE 1779L +#define RPC_X_NULL_REF_POINTER 1780L +#define RPC_X_ENUM_VALUE_OUT_OF_RANGE 1781L +#define RPC_X_BYTE_COUNT_TOO_SMALL 1782L +#define RPC_X_BAD_STUB_DATA 1783L +#define ERROR_INVALID_USER_BUFFER 1784L +#define ERROR_UNRECOGNIZED_MEDIA 1785L +#define ERROR_NO_TRUST_LSA_SECRET 1786L +#define ERROR_NO_TRUST_SAM_ACCOUNT 1787L +#define ERROR_TRUSTED_DOMAIN_FAILURE 1788L +#define ERROR_TRUSTED_RELATIONSHIP_FAILURE 1789L +#define ERROR_TRUST_FAILURE 1790L +#define RPC_S_CALL_IN_PROGRESS 1791L +#define ERROR_NETLOGON_NOT_STARTED 1792L +#define ERROR_ACCOUNT_EXPIRED 1793L +#define ERROR_REDIRECTOR_HAS_OPEN_HANDLES 1794L +#define ERROR_PRINTER_DRIVER_ALREADY_INSTALLED 1795L +#define ERROR_UNKNOWN_PORT 1796L +#define ERROR_UNKNOWN_PRINTER_DRIVER 1797L +#define ERROR_UNKNOWN_PRINTPROCESSOR 1798L +#define ERROR_INVALID_SEPARATOR_FILE 1799L +#define ERROR_INVALID_PRIORITY 1800L +#define ERROR_INVALID_PRINTER_NAME 1801L +#define ERROR_PRINTER_ALREADY_EXISTS 1802L +#define ERROR_INVALID_PRINTER_COMMAND 1803L +#define ERROR_INVALID_DATATYPE 1804L +#define ERROR_INVALID_ENVIRONMENT 1805L +#define RPC_S_NO_MORE_BINDINGS 1806L +#define ERROR_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT 1807L +#define ERROR_NOLOGON_WORKSTATION_TRUST_ACCOUNT 1808L +#define ERROR_NOLOGON_SERVER_TRUST_ACCOUNT 1809L +#define ERROR_DOMAIN_TRUST_INCONSISTENT 1810L +#define ERROR_SERVER_HAS_OPEN_HANDLES 1811L +#define ERROR_RESOURCE_DATA_NOT_FOUND 1812L +#define ERROR_RESOURCE_TYPE_NOT_FOUND 1813L +#define ERROR_RESOURCE_NAME_NOT_FOUND 1814L +#define ERROR_RESOURCE_LANG_NOT_FOUND 1815L +#define ERROR_NOT_ENOUGH_QUOTA 1816L +#define RPC_S_NO_INTERFACES 1817L +#define RPC_S_CALL_CANCELLED 1818L +#define RPC_S_BINDING_INCOMPLETE 1819L +#define RPC_S_COMM_FAILURE 1820L +#define RPC_S_UNSUPPORTED_AUTHN_LEVEL 1821L +#define RPC_S_NO_PRINC_NAME 1822L +#define RPC_S_NOT_RPC_ERROR 1823L +#define RPC_S_UUID_LOCAL_ONLY 1824L +#define RPC_S_SEC_PKG_ERROR 1825L +#define RPC_S_NOT_CANCELLED 1826L +#define RPC_X_INVALID_ES_ACTION 1827L +#define RPC_X_WRONG_ES_VERSION 1828L +#define RPC_X_WRONG_STUB_VERSION 1829L +#define RPC_X_INVALID_PIPE_OBJECT 1830L +#define RPC_X_WRONG_PIPE_ORDER 1831L +#define RPC_X_WRONG_PIPE_VERSION 1832L +#define RPC_S_GROUP_MEMBER_NOT_FOUND 1898L +#define EPT_S_CANT_CREATE 1899L +#define RPC_S_INVALID_OBJECT 1900L +#define ERROR_INVALID_TIME 1901L +#define ERROR_INVALID_FORM_NAME 1902L +#define ERROR_INVALID_FORM_SIZE 1903L +#define ERROR_ALREADY_WAITING 1904L +#define ERROR_PRINTER_DELETED 1905L +#define ERROR_INVALID_PRINTER_STATE 1906L +#define ERROR_PASSWORD_MUST_CHANGE 1907L +#define ERROR_DOMAIN_CONTROLLER_NOT_FOUND 1908L +#define ERROR_ACCOUNT_LOCKED_OUT 1909L +#define OR_INVALID_OXID 1910L +#define OR_INVALID_OID 1911L +#define OR_INVALID_SET 1912L +#define RPC_S_SEND_INCOMPLETE 1913L +#define RPC_S_INVALID_ASYNC_HANDLE 1914L +#define RPC_S_INVALID_ASYNC_CALL 1915L +#define RPC_X_PIPE_CLOSED 1916L +#define RPC_X_PIPE_DISCIPLINE_ERROR 1917L +#define RPC_X_PIPE_EMPTY 1918L +#define ERROR_NO_SITENAME 1919L +#define ERROR_CANT_ACCESS_FILE 1920L +#define ERROR_CANT_RESOLVE_FILENAME 1921L +#define RPC_S_ENTRY_TYPE_MISMATCH 1922L +#define RPC_S_NOT_ALL_OBJS_EXPORTED 1923L +#define RPC_S_INTERFACE_NOT_EXPORTED 1924L +#define RPC_S_PROFILE_NOT_ADDED 1925L +#define RPC_S_PRF_ELT_NOT_ADDED 1926L +#define RPC_S_PRF_ELT_NOT_REMOVED 1927L +#define RPC_S_GRP_ELT_NOT_ADDED 1928L +#define RPC_S_GRP_ELT_NOT_REMOVED 1929L +#define ERROR_KM_DRIVER_BLOCKED 1930L +#define ERROR_CONTEXT_EXPIRED 1931L +#define ERROR_PER_USER_TRUST_QUOTA_EXCEEDED 1932L +#define ERROR_ALL_USER_TRUST_QUOTA_EXCEEDED 1933L +#define ERROR_USER_DELETE_TRUST_QUOTA_EXCEEDED 1934L +#define ERROR_AUTHENTICATION_FIREWALL_FAILED 1935L +#define ERROR_REMOTE_PRINT_CONNECTIONS_BLOCKED 1936L +#define ERROR_INVALID_PIXEL_FORMAT 2000L +#define ERROR_BAD_DRIVER 2001L +#define ERROR_INVALID_WINDOW_STYLE 2002L +#define ERROR_METAFILE_NOT_SUPPORTED 2003L +#define ERROR_TRANSFORM_NOT_SUPPORTED 2004L +#define ERROR_CLIPPING_NOT_SUPPORTED 2005L +#define ERROR_INVALID_CMM 2010L +#define ERROR_INVALID_PROFILE 2011L +#define ERROR_TAG_NOT_FOUND 2012L +#define ERROR_TAG_NOT_PRESENT 2013L +#define ERROR_DUPLICATE_TAG 2014L +#define ERROR_PROFILE_NOT_ASSOCIATED_WITH_DEVICE 2015L +#define ERROR_PROFILE_NOT_FOUND 2016L +#define ERROR_INVALID_COLORSPACE 2017L +#define ERROR_ICM_NOT_ENABLED 2018L +#define ERROR_DELETING_ICM_XFORM 2019L +#define ERROR_INVALID_TRANSFORM 2020L +#define ERROR_COLORSPACE_MISMATCH 2021L +#define ERROR_INVALID_COLORINDEX 2022L +#define ERROR_CONNECTED_OTHER_PASSWORD 2108L +#define ERROR_CONNECTED_OTHER_PASSWORD_DEFAULT 2109L +#define ERROR_BAD_USERNAME 2202L +#define ERROR_NOT_CONNECTED 2250L +#define ERROR_OPEN_FILES 2401L +#define ERROR_ACTIVE_CONNECTIONS 2402L +#define ERROR_DEVICE_IN_USE 2404L +#define ERROR_UNKNOWN_PRINT_MONITOR 3000L +#define ERROR_PRINTER_DRIVER_IN_USE 3001L +#define ERROR_SPOOL_FILE_NOT_FOUND 3002L +#define ERROR_SPL_NO_STARTDOC 3003L +#define ERROR_SPL_NO_ADDJOB 3004L +#define ERROR_PRINT_PROCESSOR_ALREADY_INSTALLED 3005L +#define ERROR_PRINT_MONITOR_ALREADY_INSTALLED 3006L +#define ERROR_INVALID_PRINT_MONITOR 3007L +#define ERROR_PRINT_MONITOR_IN_USE 3008L +#define ERROR_PRINTER_HAS_JOBS_QUEUED 3009L +#define ERROR_SUCCESS_REBOOT_REQUIRED 3010L +#define ERROR_SUCCESS_RESTART_REQUIRED 3011L +#define ERROR_PRINTER_NOT_FOUND 3012L +#define ERROR_PRINTER_DRIVER_WARNED 3013L +#define ERROR_PRINTER_DRIVER_BLOCKED 3014L +#define ERROR_WINS_INTERNAL 4000L +#define ERROR_CAN_NOT_DEL_LOCAL_WINS 4001L +#define ERROR_STATIC_INIT 4002L +#define ERROR_INC_BACKUP 4003L +#define ERROR_FULL_BACKUP 4004L +#define ERROR_REC_NON_EXISTENT 4005L +#define ERROR_RPL_NOT_ALLOWED 4006L +#define ERROR_DHCP_ADDRESS_CONFLICT 4100L +#define ERROR_WMI_GUID_NOT_FOUND 4200L +#define ERROR_WMI_INSTANCE_NOT_FOUND 4201L +#define ERROR_WMI_ITEMID_NOT_FOUND 4202L +#define ERROR_WMI_TRY_AGAIN 4203L +#define ERROR_WMI_DP_NOT_FOUND 4204L +#define ERROR_WMI_UNRESOLVED_INSTANCE_REF 4205L +#define ERROR_WMI_ALREADY_ENABLED 4206L +#define ERROR_WMI_GUID_DISCONNECTED 4207L +#define ERROR_WMI_SERVER_UNAVAILABLE 4208L +#define ERROR_WMI_DP_FAILED 4209L +#define ERROR_WMI_INVALID_MOF 4210L +#define ERROR_WMI_INVALID_REGINFO 4211L +#define ERROR_WMI_ALREADY_DISABLED 4212L +#define ERROR_WMI_READ_ONLY 4213L +#define ERROR_WMI_SET_FAILURE 4214L +#define ERROR_INVALID_MEDIA 4300L +#define ERROR_INVALID_LIBRARY 4301L +#define ERROR_INVALID_MEDIA_POOL 4302L +#define ERROR_DRIVE_MEDIA_MISMATCH 4303L +#define ERROR_MEDIA_OFFLINE 4304L +#define ERROR_LIBRARY_OFFLINE 4305L +#define ERROR_EMPTY 4306L +#define ERROR_NOT_EMPTY 4307L +#define ERROR_MEDIA_UNAVAILABLE 4308L +#define ERROR_RESOURCE_DISABLED 4309L +#define ERROR_INVALID_CLEANER 4310L +#define ERROR_UNABLE_TO_CLEAN 4311L +#define ERROR_OBJECT_NOT_FOUND 4312L +#define ERROR_DATABASE_FAILURE 4313L +#define ERROR_DATABASE_FULL 4314L +#define ERROR_MEDIA_INCOMPATIBLE 4315L +#define ERROR_RESOURCE_NOT_PRESENT 4316L +#define ERROR_INVALID_OPERATION 4317L +#define ERROR_MEDIA_NOT_AVAILABLE 4318L +#define ERROR_DEVICE_NOT_AVAILABLE 4319L +#define ERROR_REQUEST_REFUSED 4320L +#define ERROR_INVALID_DRIVE_OBJECT 4321L +#define ERROR_LIBRARY_FULL 4322L +#define ERROR_MEDIUM_NOT_ACCESSIBLE 4323L +#define ERROR_UNABLE_TO_LOAD_MEDIUM 4324L +#define ERROR_UNABLE_TO_INVENTORY_DRIVE 4325L +#define ERROR_UNABLE_TO_INVENTORY_SLOT 4326L +#define ERROR_UNABLE_TO_INVENTORY_TRANSPORT 4327L +#define ERROR_TRANSPORT_FULL 4328L +#define ERROR_CONTROLLING_IEPORT 4329L +#define ERROR_UNABLE_TO_EJECT_MOUNTED_MEDIA 4330L +#define ERROR_CLEANER_SLOT_SET 4331L +#define ERROR_CLEANER_SLOT_NOT_SET 4332L +#define ERROR_CLEANER_CARTRIDGE_SPENT 4333L +#define ERROR_UNEXPECTED_OMID 4334L +#define ERROR_CANT_DELETE_LAST_ITEM 4335L +#define ERROR_MESSAGE_EXCEEDS_MAX_SIZE 4336L +#define ERROR_VOLUME_CONTAINS_SYS_FILES 4337L +#define ERROR_INDIGENOUS_TYPE 4338L +#define ERROR_NO_SUPPORTING_DRIVES 4339L +#define ERROR_CLEANER_CARTRIDGE_INSTALLED 4340L +#define ERROR_IEPORT_FULL 4341L +#define ERROR_FILE_OFFLINE 4350L +#define ERROR_REMOTE_STORAGE_NOT_ACTIVE 4351L +#define ERROR_REMOTE_STORAGE_MEDIA_ERROR 4352L +#define ERROR_NOT_A_REPARSE_POINT 4390L +#define ERROR_REPARSE_ATTRIBUTE_CONFLICT 4391L +#define ERROR_INVALID_REPARSE_DATA 4392L +#define ERROR_REPARSE_TAG_INVALID 4393L +#define ERROR_REPARSE_TAG_MISMATCH 4394L +#define ERROR_VOLUME_NOT_SIS_ENABLED 4500L +#define ERROR_DEPENDENT_RESOURCE_EXISTS 5001L +#define ERROR_DEPENDENCY_NOT_FOUND 5002L +#define ERROR_DEPENDENCY_ALREADY_EXISTS 5003L +#define ERROR_RESOURCE_NOT_ONLINE 5004L +#define ERROR_HOST_NODE_NOT_AVAILABLE 5005L +#define ERROR_RESOURCE_NOT_AVAILABLE 5006L +#define ERROR_RESOURCE_NOT_FOUND 5007L +#define ERROR_SHUTDOWN_CLUSTER 5008L +#define ERROR_CANT_EVICT_ACTIVE_NODE 5009L +#define ERROR_OBJECT_ALREADY_EXISTS 5010L +#define ERROR_OBJECT_IN_LIST 5011L +#define ERROR_GROUP_NOT_AVAILABLE 5012L +#define ERROR_GROUP_NOT_FOUND 5013L +#define ERROR_GROUP_NOT_ONLINE 5014L +#define ERROR_HOST_NODE_NOT_RESOURCE_OWNER 5015L +#define ERROR_HOST_NODE_NOT_GROUP_OWNER 5016L +#define ERROR_RESMON_CREATE_FAILED 5017L +#define ERROR_RESMON_ONLINE_FAILED 5018L +#define ERROR_RESOURCE_ONLINE 5019L +#define ERROR_QUORUM_RESOURCE 5020L +#define ERROR_NOT_QUORUM_CAPABLE 5021L +#define ERROR_CLUSTER_SHUTTING_DOWN 5022L +#define ERROR_INVALID_STATE 5023L +#define ERROR_RESOURCE_PROPERTIES_STORED 5024L +#define ERROR_NOT_QUORUM_CLASS 5025L +#define ERROR_CORE_RESOURCE 5026L +#define ERROR_QUORUM_RESOURCE_ONLINE_FAILED 5027L +#define ERROR_QUORUMLOG_OPEN_FAILED 5028L +#define ERROR_CLUSTERLOG_CORRUPT 5029L +#define ERROR_CLUSTERLOG_RECORD_EXCEEDS_MAXSIZE 5030L +#define ERROR_CLUSTERLOG_EXCEEDS_MAXSIZE 5031L +#define ERROR_CLUSTERLOG_CHKPOINT_NOT_FOUND 5032L +#define ERROR_CLUSTERLOG_NOT_ENOUGH_SPACE 5033L +#define ERROR_QUORUM_OWNER_ALIVE 5034L +#define ERROR_NETWORK_NOT_AVAILABLE 5035L +#define ERROR_NODE_NOT_AVAILABLE 5036L +#define ERROR_ALL_NODES_NOT_AVAILABLE 5037L +#define ERROR_RESOURCE_FAILED 5038L +#define ERROR_CLUSTER_INVALID_NODE 5039L +#define ERROR_CLUSTER_NODE_EXISTS 5040L +#define ERROR_CLUSTER_JOIN_IN_PROGRESS 5041L +#define ERROR_CLUSTER_NODE_NOT_FOUND 5042L +#define ERROR_CLUSTER_LOCAL_NODE_NOT_FOUND 5043L +#define ERROR_CLUSTER_NETWORK_EXISTS 5044L +#define ERROR_CLUSTER_NETWORK_NOT_FOUND 5045L +#define ERROR_CLUSTER_NETINTERFACE_EXISTS 5046L +#define ERROR_CLUSTER_NETINTERFACE_NOT_FOUND 5047L +#define ERROR_CLUSTER_INVALID_REQUEST 5048L +#define ERROR_CLUSTER_INVALID_NETWORK_PROVIDER 5049L +#define ERROR_CLUSTER_NODE_DOWN 5050L +#define ERROR_CLUSTER_NODE_UNREACHABLE 5051L +#define ERROR_CLUSTER_NODE_NOT_MEMBER 5052L +#define ERROR_CLUSTER_JOIN_NOT_IN_PROGRESS 5053L +#define ERROR_CLUSTER_INVALID_NETWORK 5054L +#define ERROR_CLUSTER_NODE_UP 5056L +#define ERROR_CLUSTER_IPADDR_IN_USE 5057L +#define ERROR_CLUSTER_NODE_NOT_PAUSED 5058L +#define ERROR_CLUSTER_NO_SECURITY_CONTEXT 5059L +#define ERROR_CLUSTER_NETWORK_NOT_INTERNAL 5060L +#define ERROR_CLUSTER_NODE_ALREADY_UP 5061L +#define ERROR_CLUSTER_NODE_ALREADY_DOWN 5062L +#define ERROR_CLUSTER_NETWORK_ALREADY_ONLINE 5063L +#define ERROR_CLUSTER_NETWORK_ALREADY_OFFLINE 5064L +#define ERROR_CLUSTER_NODE_ALREADY_MEMBER 5065L +#define ERROR_CLUSTER_LAST_INTERNAL_NETWORK 5066L +#define ERROR_CLUSTER_NETWORK_HAS_DEPENDENTS 5067L +#define ERROR_INVALID_OPERATION_ON_QUORUM 5068L +#define ERROR_DEPENDENCY_NOT_ALLOWED 5069L +#define ERROR_CLUSTER_NODE_PAUSED 5070L +#define ERROR_NODE_CANT_HOST_RESOURCE 5071L +#define ERROR_CLUSTER_NODE_NOT_READY 5072L +#define ERROR_CLUSTER_NODE_SHUTTING_DOWN 5073L +#define ERROR_CLUSTER_JOIN_ABORTED 5074L +#define ERROR_CLUSTER_INCOMPATIBLE_VERSIONS 5075L +#define ERROR_CLUSTER_MAXNUM_OF_RESOURCES_EXCEEDED 5076L +#define ERROR_CLUSTER_SYSTEM_CONFIG_CHANGED 5077L +#define ERROR_CLUSTER_RESOURCE_TYPE_NOT_FOUND 5078L +#define ERROR_CLUSTER_RESTYPE_NOT_SUPPORTED 5079L +#define ERROR_CLUSTER_RESNAME_NOT_FOUND 5080L +#define ERROR_CLUSTER_NO_RPC_PACKAGES_REGISTERED 5081L +#define ERROR_CLUSTER_OWNER_NOT_IN_PREFLIST 5082L +#define ERROR_CLUSTER_DATABASE_SEQMISMATCH 5083L +#define ERROR_RESMON_INVALID_STATE 5084L +#define ERROR_CLUSTER_GUM_NOT_LOCKER 5085L +#define ERROR_QUORUM_DISK_NOT_FOUND 5086L +#define ERROR_DATABASE_BACKUP_CORRUPT 5087L +#define ERROR_CLUSTER_NODE_ALREADY_HAS_DFS_ROOT 5088L +#define ERROR_RESOURCE_PROPERTY_UNCHANGEABLE 5089L +#define ERROR_CLUSTER_MEMBERSHIP_INVALID_STATE 5890L +#define ERROR_CLUSTER_QUORUMLOG_NOT_FOUND 5891L +#define ERROR_CLUSTER_MEMBERSHIP_HALT 5892L +#define ERROR_CLUSTER_INSTANCE_ID_MISMATCH 5893L +#define ERROR_CLUSTER_NETWORK_NOT_FOUND_FOR_IP 5894L +#define ERROR_CLUSTER_PROPERTY_DATA_TYPE_MISMATCH 5895L +#define ERROR_CLUSTER_EVICT_WITHOUT_CLEANUP 5896L +#define ERROR_CLUSTER_PARAMETER_MISMATCH 5897L +#define ERROR_NODE_CANNOT_BE_CLUSTERED 5898L +#define ERROR_CLUSTER_WRONG_OS_VERSION 5899L +#define ERROR_CLUSTER_CANT_CREATE_DUP_CLUSTER_NAME 5900L +#define ERROR_CLUSCFG_ALREADY_COMMITTED 5901L +#define ERROR_CLUSCFG_ROLLBACK_FAILED 5902L +#define ERROR_CLUSCFG_SYSTEM_DISK_DRIVE_LETTER_CONFLICT 5903L +#define ERROR_CLUSTER_OLD_VERSION 5904L +#define ERROR_CLUSTER_MISMATCHED_COMPUTER_ACCT_NAME 5905L +#define ERROR_ENCRYPTION_FAILED 6000L +#define ERROR_DECRYPTION_FAILED 6001L +#define ERROR_FILE_ENCRYPTED 6002L +#define ERROR_NO_RECOVERY_POLICY 6003L +#define ERROR_NO_EFS 6004L +#define ERROR_WRONG_EFS 6005L +#define ERROR_NO_USER_KEYS 6006L +#define ERROR_FILE_NOT_ENCRYPTED 6007L +#define ERROR_NOT_EXPORT_FORMAT 6008L +#define ERROR_FILE_READ_ONLY 6009L +#define ERROR_DIR_EFS_DISALLOWED 6010L +#define ERROR_EFS_SERVER_NOT_TRUSTED 6011L +#define ERROR_BAD_RECOVERY_POLICY 6012L +#define ERROR_EFS_ALG_BLOB_TOO_BIG 6013L +#define ERROR_VOLUME_NOT_SUPPORT_EFS 6014L +#define ERROR_EFS_DISABLED 6015L +#define ERROR_EFS_VERSION_NOT_SUPPORT 6016L +#define ERROR_NO_BROWSER_SERVERS_FOUND 6118L +#define SCHED_E_SERVICE_NOT_LOCALSYSTEM 6200L +#define ERROR_CTX_WINSTATION_NAME_INVALID 7001L +#define ERROR_CTX_INVALID_PD 7002L +#define ERROR_CTX_PD_NOT_FOUND 7003L +#define ERROR_CTX_WD_NOT_FOUND 7004L +#define ERROR_CTX_CANNOT_MAKE_EVENTLOG_ENTRY 7005L +#define ERROR_CTX_SERVICE_NAME_COLLISION 7006L +#define ERROR_CTX_CLOSE_PENDING 7007L +#define ERROR_CTX_NO_OUTBUF 7008L +#define ERROR_CTX_MODEM_INF_NOT_FOUND 7009L +#define ERROR_CTX_INVALID_MODEMNAME 7010L +#define ERROR_CTX_MODEM_RESPONSE_ERROR 7011L +#define ERROR_CTX_MODEM_RESPONSE_TIMEOUT 7012L +#define ERROR_CTX_MODEM_RESPONSE_NO_CARRIER 7013L +#define ERROR_CTX_MODEM_RESPONSE_NO_DIALTONE 7014L +#define ERROR_CTX_MODEM_RESPONSE_BUSY 7015L +#define ERROR_CTX_MODEM_RESPONSE_VOICE 7016L +#define ERROR_CTX_TD_ERROR 7017L +#define ERROR_CTX_WINSTATION_NOT_FOUND 7022L +#define ERROR_CTX_WINSTATION_ALREADY_EXISTS 7023L +#define ERROR_CTX_WINSTATION_BUSY 7024L +#define ERROR_CTX_BAD_VIDEO_MODE 7025L +#define ERROR_CTX_GRAPHICS_INVALID 7035L +#define ERROR_CTX_LOGON_DISABLED 7037L +#define ERROR_CTX_NOT_CONSOLE 7038L +#define ERROR_CTX_CLIENT_QUERY_TIMEOUT 7040L +#define ERROR_CTX_CONSOLE_DISCONNECT 7041L +#define ERROR_CTX_CONSOLE_CONNECT 7042L +#define ERROR_CTX_SHADOW_DENIED 7044L +#define ERROR_CTX_WINSTATION_ACCESS_DENIED 7045L +#define ERROR_CTX_INVALID_WD 7049L +#define ERROR_CTX_SHADOW_INVALID 7050L +#define ERROR_CTX_SHADOW_DISABLED 7051L +#define ERROR_CTX_CLIENT_LICENSE_IN_USE 7052L +#define ERROR_CTX_CLIENT_LICENSE_NOT_SET 7053L +#define ERROR_CTX_LICENSE_NOT_AVAILABLE 7054L +#define ERROR_CTX_LICENSE_CLIENT_INVALID 7055L +#define ERROR_CTX_LICENSE_EXPIRED 7056L +#define ERROR_CTX_SHADOW_NOT_RUNNING 7057L +#define ERROR_CTX_SHADOW_ENDED_BY_MODE_CHANGE 7058L +#define ERROR_ACTIVATION_COUNT_EXCEEDED 7059L +#define FRS_ERR_INVALID_API_SEQUENCE 8001L +#define FRS_ERR_STARTING_SERVICE 8002L +#define FRS_ERR_STOPPING_SERVICE 8003L +#define FRS_ERR_INTERNAL_API 8004L +#define FRS_ERR_INTERNAL 8005L +#define FRS_ERR_SERVICE_COMM 8006L +#define FRS_ERR_INSUFFICIENT_PRIV 8007L +#define FRS_ERR_AUTHENTICATION 8008L +#define FRS_ERR_PARENT_INSUFFICIENT_PRIV 8009L +#define FRS_ERR_PARENT_AUTHENTICATION 8010L +#define FRS_ERR_CHILD_TO_PARENT_COMM 8011L +#define FRS_ERR_PARENT_TO_CHILD_COMM 8012L +#define FRS_ERR_SYSVOL_POPULATE 8013L +#define FRS_ERR_SYSVOL_POPULATE_TIMEOUT 8014L +#define FRS_ERR_SYSVOL_IS_BUSY 8015L +#define FRS_ERR_SYSVOL_DEMOTE 8016L +#define FRS_ERR_INVALID_SERVICE_PARAMETER 8017L +#define DS_S_SUCCESS NO_ERROR +#define ERROR_DS_NOT_INSTALLED 8200L +#define ERROR_DS_MEMBERSHIP_EVALUATED_LOCALLY 8201L +#define ERROR_DS_NO_ATTRIBUTE_OR_VALUE 8202L +#define ERROR_DS_INVALID_ATTRIBUTE_SYNTAX 8203L +#define ERROR_DS_ATTRIBUTE_TYPE_UNDEFINED 8204L +#define ERROR_DS_ATTRIBUTE_OR_VALUE_EXISTS 8205L +#define ERROR_DS_BUSY 8206L +#define ERROR_DS_UNAVAILABLE 8207L +#define ERROR_DS_NO_RIDS_ALLOCATED 8208L +#define ERROR_DS_NO_MORE_RIDS 8209L +#define ERROR_DS_INCORRECT_ROLE_OWNER 8210L +#define ERROR_DS_RIDMGR_INIT_ERROR 8211L +#define ERROR_DS_OBJ_CLASS_VIOLATION 8212L +#define ERROR_DS_CANT_ON_NON_LEAF 8213L +#define ERROR_DS_CANT_ON_RDN 8214L +#define ERROR_DS_CANT_MOD_OBJ_CLASS 8215L +#define ERROR_DS_CROSS_DOM_MOVE_ERROR 8216L +#define ERROR_DS_GC_NOT_AVAILABLE 8217L +#define ERROR_SHARED_POLICY 8218L +#define ERROR_POLICY_OBJECT_NOT_FOUND 8219L +#define ERROR_POLICY_ONLY_IN_DS 8220L +#define ERROR_PROMOTION_ACTIVE 8221L +#define ERROR_NO_PROMOTION_ACTIVE 8222L +#define ERROR_DS_OPERATIONS_ERROR 8224L +#define ERROR_DS_PROTOCOL_ERROR 8225L +#define ERROR_DS_TIMELIMIT_EXCEEDED 8226L +#define ERROR_DS_SIZELIMIT_EXCEEDED 8227L +#define ERROR_DS_ADMIN_LIMIT_EXCEEDED 8228L +#define ERROR_DS_COMPARE_FALSE 8229L +#define ERROR_DS_COMPARE_TRUE 8230L +#define ERROR_DS_AUTH_METHOD_NOT_SUPPORTED 8231L +#define ERROR_DS_STRONG_AUTH_REQUIRED 8232L +#define ERROR_DS_INAPPROPRIATE_AUTH 8233L +#define ERROR_DS_AUTH_UNKNOWN 8234L +#define ERROR_DS_REFERRAL 8235L +#define ERROR_DS_UNAVAILABLE_CRIT_EXTENSION 8236L +#define ERROR_DS_CONFIDENTIALITY_REQUIRED 8237L +#define ERROR_DS_INAPPROPRIATE_MATCHING 8238L +#define ERROR_DS_CONSTRAINT_VIOLATION 8239L +#define ERROR_DS_NO_SUCH_OBJECT 8240L +#define ERROR_DS_ALIAS_PROBLEM 8241L +#define ERROR_DS_INVALID_DN_SYNTAX 8242L +#define ERROR_DS_IS_LEAF 8243L +#define ERROR_DS_ALIAS_DEREF_PROBLEM 8244L +#define ERROR_DS_UNWILLING_TO_PERFORM 8245L +#define ERROR_DS_LOOP_DETECT 8246L +#define ERROR_DS_NAMING_VIOLATION 8247L +#define ERROR_DS_OBJECT_RESULTS_TOO_LARGE 8248L +#define ERROR_DS_AFFECTS_MULTIPLE_DSAS 8249L +#define ERROR_DS_SERVER_DOWN 8250L +#define ERROR_DS_LOCAL_ERROR 8251L +#define ERROR_DS_ENCODING_ERROR 8252L +#define ERROR_DS_DECODING_ERROR 8253L +#define ERROR_DS_FILTER_UNKNOWN 8254L +#define ERROR_DS_PARAM_ERROR 8255L +#define ERROR_DS_NOT_SUPPORTED 8256L +#define ERROR_DS_NO_RESULTS_RETURNED 8257L +#define ERROR_DS_CONTROL_NOT_FOUND 8258L +#define ERROR_DS_CLIENT_LOOP 8259L +#define ERROR_DS_REFERRAL_LIMIT_EXCEEDED 8260L +#define ERROR_DS_SORT_CONTROL_MISSING 8261L +#define ERROR_DS_OFFSET_RANGE_ERROR 8262L +#define ERROR_DS_ROOT_MUST_BE_NC 8301L +#define ERROR_DS_ADD_REPLICA_INHIBITED 8302L +#define ERROR_DS_ATT_NOT_DEF_IN_SCHEMA 8303L +#define ERROR_DS_MAX_OBJ_SIZE_EXCEEDED 8304L +#define ERROR_DS_OBJ_STRING_NAME_EXISTS 8305L +#define ERROR_DS_NO_RDN_DEFINED_IN_SCHEMA 8306L +#define ERROR_DS_RDN_DOESNT_MATCH_SCHEMA 8307L +#define ERROR_DS_NO_REQUESTED_ATTS_FOUND 8308L +#define ERROR_DS_USER_BUFFER_TO_SMALL 8309L +#define ERROR_DS_ATT_IS_NOT_ON_OBJ 8310L +#define ERROR_DS_ILLEGAL_MOD_OPERATION 8311L +#define ERROR_DS_OBJ_TOO_LARGE 8312L +#define ERROR_DS_BAD_INSTANCE_TYPE 8313L +#define ERROR_DS_MASTERDSA_REQUIRED 8314L +#define ERROR_DS_OBJECT_CLASS_REQUIRED 8315L +#define ERROR_DS_MISSING_REQUIRED_ATT 8316L +#define ERROR_DS_ATT_NOT_DEF_FOR_CLASS 8317L +#define ERROR_DS_ATT_ALREADY_EXISTS 8318L +#define ERROR_DS_CANT_ADD_ATT_VALUES 8320L +#define ERROR_DS_SINGLE_VALUE_CONSTRAINT 8321L +#define ERROR_DS_RANGE_CONSTRAINT 8322L +#define ERROR_DS_ATT_VAL_ALREADY_EXISTS 8323L +#define ERROR_DS_CANT_REM_MISSING_ATT 8324L +#define ERROR_DS_CANT_REM_MISSING_ATT_VAL 8325L +#define ERROR_DS_ROOT_CANT_BE_SUBREF 8326L +#define ERROR_DS_NO_CHAINING 8327L +#define ERROR_DS_NO_CHAINED_EVAL 8328L +#define ERROR_DS_NO_PARENT_OBJECT 8329L +#define ERROR_DS_PARENT_IS_AN_ALIAS 8330L +#define ERROR_DS_CANT_MIX_MASTER_AND_REPS 8331L +#define ERROR_DS_CHILDREN_EXIST 8332L +#define ERROR_DS_OBJ_NOT_FOUND 8333L +#define ERROR_DS_ALIASED_OBJ_MISSING 8334L +#define ERROR_DS_BAD_NAME_SYNTAX 8335L +#define ERROR_DS_ALIAS_POINTS_TO_ALIAS 8336L +#define ERROR_DS_CANT_DEREF_ALIAS 8337L +#define ERROR_DS_OUT_OF_SCOPE 8338L +#define ERROR_DS_OBJECT_BEING_REMOVED 8339L +#define ERROR_DS_CANT_DELETE_DSA_OBJ 8340L +#define ERROR_DS_GENERIC_ERROR 8341L +#define ERROR_DS_DSA_MUST_BE_INT_MASTER 8342L +#define ERROR_DS_CLASS_NOT_DSA 8343L +#define ERROR_DS_INSUFF_ACCESS_RIGHTS 8344L +#define ERROR_DS_ILLEGAL_SUPERIOR 8345L +#define ERROR_DS_ATTRIBUTE_OWNED_BY_SAM 8346L +#define ERROR_DS_NAME_TOO_MANY_PARTS 8347L +#define ERROR_DS_NAME_TOO_LONG 8348L +#define ERROR_DS_NAME_VALUE_TOO_LONG 8349L +#define ERROR_DS_NAME_UNPARSEABLE 8350L +#define ERROR_DS_NAME_TYPE_UNKNOWN 8351L +#define ERROR_DS_NOT_AN_OBJECT 8352L +#define ERROR_DS_SEC_DESC_TOO_SHORT 8353L +#define ERROR_DS_SEC_DESC_INVALID 8354L +#define ERROR_DS_NO_DELETED_NAME 8355L +#define ERROR_DS_SUBREF_MUST_HAVE_PARENT 8356L +#define ERROR_DS_NCNAME_MUST_BE_NC 8357L +#define ERROR_DS_CANT_ADD_SYSTEM_ONLY 8358L +#define ERROR_DS_CLASS_MUST_BE_CONCRETE 8359L +#define ERROR_DS_INVALID_DMD 8360L +#define ERROR_DS_OBJ_GUID_EXISTS 8361L +#define ERROR_DS_NOT_ON_BACKLINK 8362L +#define ERROR_DS_NO_CROSSREF_FOR_NC 8363L +#define ERROR_DS_SHUTTING_DOWN 8364L +#define ERROR_DS_UNKNOWN_OPERATION 8365L +#define ERROR_DS_INVALID_ROLE_OWNER 8366L +#define ERROR_DS_COULDNT_CONTACT_FSMO 8367L +#define ERROR_DS_CROSS_NC_DN_RENAME 8368L +#define ERROR_DS_CANT_MOD_SYSTEM_ONLY 8369L +#define ERROR_DS_REPLICATOR_ONLY 8370L +#define ERROR_DS_OBJ_CLASS_NOT_DEFINED 8371L +#define ERROR_DS_OBJ_CLASS_NOT_SUBCLASS 8372L +#define ERROR_DS_NAME_REFERENCE_INVALID 8373L +#define ERROR_DS_CROSS_REF_EXISTS 8374L +#define ERROR_DS_CANT_DEL_MASTER_CROSSREF 8375L +#define ERROR_DS_SUBTREE_NOTIFY_NOT_NC_HEAD 8376L +#define ERROR_DS_NOTIFY_FILTER_TOO_COMPLEX 8377L +#define ERROR_DS_DUP_RDN 8378L +#define ERROR_DS_DUP_OID 8379L +#define ERROR_DS_DUP_MAPI_ID 8380L +#define ERROR_DS_DUP_SCHEMA_ID_GUID 8381L +#define ERROR_DS_DUP_LDAP_DISPLAY_NAME 8382L +#define ERROR_DS_SEMANTIC_ATT_TEST 8383L +#define ERROR_DS_SYNTAX_MISMATCH 8384L +#define ERROR_DS_EXISTS_IN_MUST_HAVE 8385L +#define ERROR_DS_EXISTS_IN_MAY_HAVE 8386L +#define ERROR_DS_NONEXISTENT_MAY_HAVE 8387L +#define ERROR_DS_NONEXISTENT_MUST_HAVE 8388L +#define ERROR_DS_AUX_CLS_TEST_FAIL 8389L +#define ERROR_DS_NONEXISTENT_POSS_SUP 8390L +#define ERROR_DS_SUB_CLS_TEST_FAIL 8391L +#define ERROR_DS_BAD_RDN_ATT_ID_SYNTAX 8392L +#define ERROR_DS_EXISTS_IN_AUX_CLS 8393L +#define ERROR_DS_EXISTS_IN_SUB_CLS 8394L +#define ERROR_DS_EXISTS_IN_POSS_SUP 8395L +#define ERROR_DS_RECALCSCHEMA_FAILED 8396L +#define ERROR_DS_TREE_DELETE_NOT_FINISHED 8397L +#define ERROR_DS_CANT_DELETE 8398L +#define ERROR_DS_ATT_SCHEMA_REQ_ID 8399L +#define ERROR_DS_BAD_ATT_SCHEMA_SYNTAX 8400L +#define ERROR_DS_CANT_CACHE_ATT 8401L +#define ERROR_DS_CANT_CACHE_CLASS 8402L +#define ERROR_DS_CANT_REMOVE_ATT_CACHE 8403L +#define ERROR_DS_CANT_REMOVE_CLASS_CACHE 8404L +#define ERROR_DS_CANT_RETRIEVE_DN 8405L +#define ERROR_DS_MISSING_SUPREF 8406L +#define ERROR_DS_CANT_RETRIEVE_INSTANCE 8407L +#define ERROR_DS_CODE_INCONSISTENCY 8408L +#define ERROR_DS_DATABASE_ERROR 8409L +#define ERROR_DS_GOVERNSID_MISSING 8410L +#define ERROR_DS_MISSING_EXPECTED_ATT 8411L +#define ERROR_DS_NCNAME_MISSING_CR_REF 8412L +#define ERROR_DS_SECURITY_CHECKING_ERROR 8413L +#define ERROR_DS_SCHEMA_NOT_LOADED 8414L +#define ERROR_DS_SCHEMA_ALLOC_FAILED 8415L +#define ERROR_DS_ATT_SCHEMA_REQ_SYNTAX 8416L +#define ERROR_DS_GCVERIFY_ERROR 8417L +#define ERROR_DS_DRA_SCHEMA_MISMATCH 8418L +#define ERROR_DS_CANT_FIND_DSA_OBJ 8419L +#define ERROR_DS_CANT_FIND_EXPECTED_NC 8420L +#define ERROR_DS_CANT_FIND_NC_IN_CACHE 8421L +#define ERROR_DS_CANT_RETRIEVE_CHILD 8422L +#define ERROR_DS_SECURITY_ILLEGAL_MODIFY 8423L +#define ERROR_DS_CANT_REPLACE_HIDDEN_REC 8424L +#define ERROR_DS_BAD_HIERARCHY_FILE 8425L +#define ERROR_DS_BUILD_HIERARCHY_TABLE_FAILED 8426L +#define ERROR_DS_CONFIG_PARAM_MISSING 8427L +#define ERROR_DS_COUNTING_AB_INDICES_FAILED 8428L +#define ERROR_DS_HIERARCHY_TABLE_MALLOC_FAILED 8429L +#define ERROR_DS_INTERNAL_FAILURE 8430L +#define ERROR_DS_UNKNOWN_ERROR 8431L +#define ERROR_DS_ROOT_REQUIRES_CLASS_TOP 8432L +#define ERROR_DS_REFUSING_FSMO_ROLES 8433L +#define ERROR_DS_MISSING_FSMO_SETTINGS 8434L +#define ERROR_DS_UNABLE_TO_SURRENDER_ROLES 8435L +#define ERROR_DS_DRA_GENERIC 8436L +#define ERROR_DS_DRA_INVALID_PARAMETER 8437L +#define ERROR_DS_DRA_BUSY 8438L +#define ERROR_DS_DRA_BAD_DN 8439L +#define ERROR_DS_DRA_BAD_NC 8440L +#define ERROR_DS_DRA_DN_EXISTS 8441L +#define ERROR_DS_DRA_INTERNAL_ERROR 8442L +#define ERROR_DS_DRA_INCONSISTENT_DIT 8443L +#define ERROR_DS_DRA_CONNECTION_FAILED 8444L +#define ERROR_DS_DRA_BAD_INSTANCE_TYPE 8445L +#define ERROR_DS_DRA_OUT_OF_MEM 8446L +#define ERROR_DS_DRA_MAIL_PROBLEM 8447L +#define ERROR_DS_DRA_REF_ALREADY_EXISTS 8448L +#define ERROR_DS_DRA_REF_NOT_FOUND 8449L +#define ERROR_DS_DRA_OBJ_IS_REP_SOURCE 8450L +#define ERROR_DS_DRA_DB_ERROR 8451L +#define ERROR_DS_DRA_NO_REPLICA 8452L +#define ERROR_DS_DRA_ACCESS_DENIED 8453L +#define ERROR_DS_DRA_NOT_SUPPORTED 8454L +#define ERROR_DS_DRA_RPC_CANCELLED 8455L +#define ERROR_DS_DRA_SOURCE_DISABLED 8456L +#define ERROR_DS_DRA_SINK_DISABLED 8457L +#define ERROR_DS_DRA_NAME_COLLISION 8458L +#define ERROR_DS_DRA_SOURCE_REINSTALLED 8459L +#define ERROR_DS_DRA_MISSING_PARENT 8460L +#define ERROR_DS_DRA_PREEMPTED 8461L +#define ERROR_DS_DRA_ABANDON_SYNC 8462L +#define ERROR_DS_DRA_SHUTDOWN 8463L +#define ERROR_DS_DRA_INCOMPATIBLE_PARTIAL_SET 8464L +#define ERROR_DS_DRA_SOURCE_IS_PARTIAL_REPLICA 8465L +#define ERROR_DS_DRA_EXTN_CONNECTION_FAILED 8466L +#define ERROR_DS_INSTALL_SCHEMA_MISMATCH 8467L +#define ERROR_DS_DUP_LINK_ID 8468L +#define ERROR_DS_NAME_ERROR_RESOLVING 8469L +#define ERROR_DS_NAME_ERROR_NOT_FOUND 8470L +#define ERROR_DS_NAME_ERROR_NOT_UNIQUE 8471L +#define ERROR_DS_NAME_ERROR_NO_MAPPING 8472L +#define ERROR_DS_NAME_ERROR_DOMAIN_ONLY 8473L +#define ERROR_DS_NAME_ERROR_NO_SYNTACTICAL_MAPPING 8474L +#define ERROR_DS_CONSTRUCTED_ATT_MOD 8475L +#define ERROR_DS_WRONG_OM_OBJ_CLASS 8476L +#define ERROR_DS_DRA_REPL_PENDING 8477L +#define ERROR_DS_DS_REQUIRED 8478L +#define ERROR_DS_INVALID_LDAP_DISPLAY_NAME 8479L +#define ERROR_DS_NON_BASE_SEARCH 8480L +#define ERROR_DS_CANT_RETRIEVE_ATTS 8481L +#define ERROR_DS_BACKLINK_WITHOUT_LINK 8482L +#define ERROR_DS_EPOCH_MISMATCH 8483L +#define ERROR_DS_SRC_NAME_MISMATCH 8484L +#define ERROR_DS_SRC_AND_DST_NC_IDENTICAL 8485L +#define ERROR_DS_DST_NC_MISMATCH 8486L +#define ERROR_DS_NOT_AUTHORITIVE_FOR_DST_NC 8487L +#define ERROR_DS_SRC_GUID_MISMATCH 8488L +#define ERROR_DS_CANT_MOVE_DELETED_OBJECT 8489L +#define ERROR_DS_PDC_OPERATION_IN_PROGRESS 8490L +#define ERROR_DS_CROSS_DOMAIN_CLEANUP_REQD 8491L +#define ERROR_DS_ILLEGAL_XDOM_MOVE_OPERATION 8492L +#define ERROR_DS_CANT_WITH_ACCT_GROUP_MEMBERSHPS 8493L +#define ERROR_DS_NC_MUST_HAVE_NC_PARENT 8494L +#define ERROR_DS_CR_IMPOSSIBLE_TO_VALIDATE 8495L +#define ERROR_DS_DST_DOMAIN_NOT_NATIVE 8496L +#define ERROR_DS_MISSING_INFRASTRUCTURE_CONTAINER 8497L +#define ERROR_DS_CANT_MOVE_ACCOUNT_GROUP 8498L +#define ERROR_DS_CANT_MOVE_RESOURCE_GROUP 8499L +#define ERROR_DS_INVALID_SEARCH_FLAG 8500L +#define ERROR_DS_NO_TREE_DELETE_ABOVE_NC 8501L +#define ERROR_DS_COULDNT_LOCK_TREE_FOR_DELETE 8502L +#define ERROR_DS_COULDNT_IDENTIFY_OBJECTS_FOR_TREE_DELETE 8503L +#define ERROR_DS_SAM_INIT_FAILURE 8504L +#define ERROR_DS_SENSITIVE_GROUP_VIOLATION 8505L +#define ERROR_DS_CANT_MOD_PRIMARYGROUPID 8506L +#define ERROR_DS_ILLEGAL_BASE_SCHEMA_MOD 8507L +#define ERROR_DS_NONSAFE_SCHEMA_CHANGE 8508L +#define ERROR_DS_SCHEMA_UPDATE_DISALLOWED 8509L +#define ERROR_DS_CANT_CREATE_UNDER_SCHEMA 8510L +#define ERROR_DS_INSTALL_NO_SRC_SCH_VERSION 8511L +#define ERROR_DS_INSTALL_NO_SCH_VERSION_IN_INIFILE 8512L +#define ERROR_DS_INVALID_GROUP_TYPE 8513L +#define ERROR_DS_NO_NEST_GLOBALGROUP_IN_MIXEDDOMAIN 8514L +#define ERROR_DS_NO_NEST_LOCALGROUP_IN_MIXEDDOMAIN 8515L +#define ERROR_DS_GLOBAL_CANT_HAVE_LOCAL_MEMBER 8516L +#define ERROR_DS_GLOBAL_CANT_HAVE_UNIVERSAL_MEMBER 8517L +#define ERROR_DS_UNIVERSAL_CANT_HAVE_LOCAL_MEMBER 8518L +#define ERROR_DS_GLOBAL_CANT_HAVE_CROSSDOMAIN_MEMBER 8519L +#define ERROR_DS_LOCAL_CANT_HAVE_CROSSDOMAIN_LOCAL_MEMBER 8520L +#define ERROR_DS_HAVE_PRIMARY_MEMBERS 8521L +#define ERROR_DS_STRING_SD_CONVERSION_FAILED 8522L +#define ERROR_DS_NAMING_MASTER_GC 8523L +#define ERROR_DS_DNS_LOOKUP_FAILURE 8524L +#define ERROR_DS_COULDNT_UPDATE_SPNS 8525L +#define ERROR_DS_CANT_RETRIEVE_SD 8526L +#define ERROR_DS_KEY_NOT_UNIQUE 8527L +#define ERROR_DS_WRONG_LINKED_ATT_SYNTAX 8528L +#define ERROR_DS_SAM_NEED_BOOTKEY_PASSWORD 8529L +#define ERROR_DS_SAM_NEED_BOOTKEY_FLOPPY 8530L +#define ERROR_DS_CANT_START 8531L +#define ERROR_DS_INIT_FAILURE 8532L +#define ERROR_DS_NO_PKT_PRIVACY_ON_CONNECTION 8533L +#define ERROR_DS_SOURCE_DOMAIN_IN_FOREST 8534L +#define ERROR_DS_DESTINATION_DOMAIN_NOT_IN_FOREST 8535L +#define ERROR_DS_DESTINATION_AUDITING_NOT_ENABLED 8536L +#define ERROR_DS_CANT_FIND_DC_FOR_SRC_DOMAIN 8537L +#define ERROR_DS_SRC_OBJ_NOT_GROUP_OR_USER 8538L +#define ERROR_DS_SRC_SID_EXISTS_IN_FOREST 8539L +#define ERROR_DS_SRC_AND_DST_OBJECT_CLASS_MISMATCH 8540L +#define ERROR_SAM_INIT_FAILURE 8541L +#define ERROR_DS_DRA_SCHEMA_INFO_SHIP 8542L +#define ERROR_DS_DRA_SCHEMA_CONFLICT 8543L +#define ERROR_DS_DRA_EARLIER_SCHEMA_CONFLICT 8544L +#define ERROR_DS_DRA_OBJ_NC_MISMATCH 8545L +#define ERROR_DS_NC_STILL_HAS_DSAS 8546L +#define ERROR_DS_GC_REQUIRED 8547L +#define ERROR_DS_LOCAL_MEMBER_OF_LOCAL_ONLY 8548L +#define ERROR_DS_NO_FPO_IN_UNIVERSAL_GROUPS 8549L +#define ERROR_DS_CANT_ADD_TO_GC 8550L +#define ERROR_DS_NO_CHECKPOINT_WITH_PDC 8551L +#define ERROR_DS_SOURCE_AUDITING_NOT_ENABLED 8552L +#define ERROR_DS_CANT_CREATE_IN_NONDOMAIN_NC 8553L +#define ERROR_DS_INVALID_NAME_FOR_SPN 8554L +#define ERROR_DS_FILTER_USES_CONTRUCTED_ATTRS 8555L +#define ERROR_DS_UNICODEPWD_NOT_IN_QUOTES 8556L +#define ERROR_DS_MACHINE_ACCOUNT_QUOTA_EXCEEDED 8557L +#define ERROR_DS_MUST_BE_RUN_ON_DST_DC 8558L +#define ERROR_DS_SRC_DC_MUST_BE_SP4_OR_GREATER 8559L +#define ERROR_DS_CANT_TREE_DELETE_CRITICAL_OBJ 8560L +#define ERROR_DS_INIT_FAILURE_CONSOLE 8561L +#define ERROR_DS_SAM_INIT_FAILURE_CONSOLE 8562L +#define ERROR_DS_FOREST_VERSION_TOO_HIGH 8563L +#define ERROR_DS_DOMAIN_VERSION_TOO_HIGH 8564L +#define ERROR_DS_FOREST_VERSION_TOO_LOW 8565L +#define ERROR_DS_DOMAIN_VERSION_TOO_LOW 8566L +#define ERROR_DS_INCOMPATIBLE_VERSION 8567L +#define ERROR_DS_LOW_DSA_VERSION 8568L +#define ERROR_DS_NO_BEHAVIOR_VERSION_IN_MIXEDDOMAIN 8569L +#define ERROR_DS_NOT_SUPPORTED_SORT_ORDER 8570L +#define ERROR_DS_NAME_NOT_UNIQUE 8571L +#define ERROR_DS_MACHINE_ACCOUNT_CREATED_PRENT4 8572L +#define ERROR_DS_OUT_OF_VERSION_STORE 8573L +#define ERROR_DS_INCOMPATIBLE_CONTROLS_USED 8574L +#define ERROR_DS_NO_REF_DOMAIN 8575L +#define ERROR_DS_RESERVED_LINK_ID 8576L +#define ERROR_DS_LINK_ID_NOT_AVAILABLE 8577L +#define ERROR_DS_AG_CANT_HAVE_UNIVERSAL_MEMBER 8578L +#define ERROR_DS_MODIFYDN_DISALLOWED_BY_INSTANCE_TYPE 8579L +#define ERROR_DS_NO_OBJECT_MOVE_IN_SCHEMA_NC 8580L +#define ERROR_DS_MODIFYDN_DISALLOWED_BY_FLAG 8581L +#define ERROR_DS_MODIFYDN_WRONG_GRANDPARENT 8582L +#define ERROR_DS_NAME_ERROR_TRUST_REFERRAL 8583L +#define ERROR_NOT_SUPPORTED_ON_STANDARD_SERVER 8584L +#define ERROR_DS_CANT_ACCESS_REMOTE_PART_OF_AD 8585L +#define ERROR_DS_CR_IMPOSSIBLE_TO_VALIDATE_V2 8586L +#define ERROR_DS_THREAD_LIMIT_EXCEEDED 8587L +#define ERROR_DS_NOT_CLOSEST 8588L +#define ERROR_DS_CANT_DERIVE_SPN_WITHOUT_SERVER_REF 8589L +#define ERROR_DS_SINGLE_USER_MODE_FAILED 8590L +#define ERROR_DS_NTDSCRIPT_SYNTAX_ERROR 8591L +#define ERROR_DS_NTDSCRIPT_PROCESS_ERROR 8592L +#define ERROR_DS_DIFFERENT_REPL_EPOCHS 8593L +#define ERROR_DS_DRS_EXTENSIONS_CHANGED 8594L +#define ERROR_DS_REPLICA_SET_CHANGE_NOT_ALLOWED_ON_DISABLED_CR 8595L +#define ERROR_DS_NO_MSDS_INTID 8596L +#define ERROR_DS_DUP_MSDS_INTID 8597L +#define ERROR_DS_EXISTS_IN_RDNATTID 8598L +#define ERROR_DS_AUTHORIZATION_FAILED 8599L +#define ERROR_DS_INVALID_SCRIPT 8600L +#define ERROR_DS_REMOTE_CROSSREF_OP_FAILED 8601L +#define ERROR_DS_CROSS_REF_BUSY 8602L +#define ERROR_DS_CANT_DERIVE_SPN_FOR_DELETED_DOMAIN 8603L +#define ERROR_DS_CANT_DEMOTE_WITH_WRITEABLE_NC 8604L +#define ERROR_DS_DUPLICATE_ID_FOUND 8605L +#define ERROR_DS_INSUFFICIENT_ATTR_TO_CREATE_OBJECT 8606L +#define ERROR_DS_GROUP_CONVERSION_ERROR 8607L +#define ERROR_DS_CANT_MOVE_APP_BASIC_GROUP 8608L +#define ERROR_DS_CANT_MOVE_APP_QUERY_GROUP 8609L +#define ERROR_DS_ROLE_NOT_VERIFIED 8610L +#define ERROR_DS_WKO_CONTAINER_CANNOT_BE_SPECIAL 8611L +#define ERROR_DS_DOMAIN_RENAME_IN_PROGRESS 8612L +#define ERROR_DS_EXISTING_AD_CHILD_NC 8613L +#define ERROR_DS_REPL_LIFETIME_EXCEEDED 8614L +#define ERROR_DS_DISALLOWED_IN_SYSTEM_CONTAINER 8615L +#define ERROR_DS_LDAP_SEND_QUEUE_FULL 8616L +#define ERROR_DS_DRA_OUT_SCHEDULE_WINDOW 8617L +#define DNS_ERROR_RESPONSE_CODES_BASE 9000 +#define DNS_ERROR_RCODE_NO_ERROR NO_ERROR +#define DNS_ERROR_MASK 0x00002328 +#define DNS_ERROR_RCODE_FORMAT_ERROR 9001L +#define DNS_ERROR_RCODE_SERVER_FAILURE 9002L +#define DNS_ERROR_RCODE_NAME_ERROR 9003L +#define DNS_ERROR_RCODE_NOT_IMPLEMENTED 9004L +#define DNS_ERROR_RCODE_REFUSED 9005L +#define DNS_ERROR_RCODE_YXDOMAIN 9006L +#define DNS_ERROR_RCODE_YXRRSET 9007L +#define DNS_ERROR_RCODE_NXRRSET 9008L +#define DNS_ERROR_RCODE_NOTAUTH 9009L +#define DNS_ERROR_RCODE_NOTZONE 9010L +#define DNS_ERROR_RCODE_BADSIG 9016L +#define DNS_ERROR_RCODE_BADKEY 9017L +#define DNS_ERROR_RCODE_BADTIME 9018L +#define DNS_ERROR_RCODE_LAST DNS_ERROR_RCODE_BADTIME +#define DNS_ERROR_PACKET_FMT_BASE 9500 +#define DNS_INFO_NO_RECORDS 9501L +#define DNS_ERROR_BAD_PACKET 9502L +#define DNS_ERROR_NO_PACKET 9503L +#define DNS_ERROR_RCODE 9504L +#define DNS_ERROR_UNSECURE_PACKET 9505L +#define DNS_STATUS_PACKET_UNSECURE DNS_ERROR_UNSECURE_PACKET +#define DNS_ERROR_NO_MEMORY ERROR_OUTOFMEMORY +#define DNS_ERROR_INVALID_NAME ERROR_INVALID_NAME +#define DNS_ERROR_INVALID_DATA ERROR_INVALID_DATA +#define DNS_ERROR_GENERAL_API_BASE 9550 +#define DNS_ERROR_INVALID_TYPE 9551L +#define DNS_ERROR_INVALID_IP_ADDRESS 9552L +#define DNS_ERROR_INVALID_PROPERTY 9553L +#define DNS_ERROR_TRY_AGAIN_LATER 9554L +#define DNS_ERROR_NOT_UNIQUE 9555L +#define DNS_ERROR_NON_RFC_NAME 9556L +#define DNS_STATUS_FQDN 9557L +#define DNS_STATUS_DOTTED_NAME 9558L +#define DNS_STATUS_SINGLE_PART_NAME 9559L +#define DNS_ERROR_INVALID_NAME_CHAR 9560L +#define DNS_ERROR_NUMERIC_NAME 9561L +#define DNS_ERROR_NOT_ALLOWED_ON_ROOT_SERVER 9562L +#define DNS_ERROR_NOT_ALLOWED_UNDER_DELEGATION 9563L +#define DNS_ERROR_CANNOT_FIND_ROOT_HINTS 9564L +#define DNS_ERROR_INCONSISTENT_ROOT_HINTS 9565L +#define DNS_ERROR_ZONE_BASE 9600 +#define DNS_ERROR_ZONE_DOES_NOT_EXIST 9601L +#define DNS_ERROR_NO_ZONE_INFO 9602L +#define DNS_ERROR_INVALID_ZONE_OPERATION 9603L +#define DNS_ERROR_ZONE_CONFIGURATION_ERROR 9604L +#define DNS_ERROR_ZONE_HAS_NO_SOA_RECORD 9605L +#define DNS_ERROR_ZONE_HAS_NO_NS_RECORDS 9606L +#define DNS_ERROR_ZONE_LOCKED 9607L +#define DNS_ERROR_ZONE_CREATION_FAILED 9608L +#define DNS_ERROR_ZONE_ALREADY_EXISTS 9609L +#define DNS_ERROR_AUTOZONE_ALREADY_EXISTS 9610L +#define DNS_ERROR_INVALID_ZONE_TYPE 9611L +#define DNS_ERROR_SECONDARY_REQUIRES_MASTER_IP 9612L +#define DNS_ERROR_ZONE_NOT_SECONDARY 9613L +#define DNS_ERROR_NEED_SECONDARY_ADDRESSES 9614L +#define DNS_ERROR_WINS_INIT_FAILED 9615L +#define DNS_ERROR_NEED_WINS_SERVERS 9616L +#define DNS_ERROR_NBSTAT_INIT_FAILED 9617L +#define DNS_ERROR_SOA_DELETE_INVALID 9618L +#define DNS_ERROR_FORWARDER_ALREADY_EXISTS 9619L +#define DNS_ERROR_ZONE_REQUIRES_MASTER_IP 9620L +#define DNS_ERROR_ZONE_IS_SHUTDOWN 9621L +#define DNS_ERROR_DATAFILE_BASE 9650 +#define DNS_ERROR_PRIMARY_REQUIRES_DATAFILE 9651L +#define DNS_ERROR_INVALID_DATAFILE_NAME 9652L +#define DNS_ERROR_DATAFILE_OPEN_FAILURE 9653L +#define DNS_ERROR_FILE_WRITEBACK_FAILED 9654L +#define DNS_ERROR_DATAFILE_PARSING 9655L +#define DNS_ERROR_DATABASE_BASE 9700 +#define DNS_ERROR_RECORD_DOES_NOT_EXIST 9701L +#define DNS_ERROR_RECORD_FORMAT 9702L +#define DNS_ERROR_NODE_CREATION_FAILED 9703L +#define DNS_ERROR_UNKNOWN_RECORD_TYPE 9704L +#define DNS_ERROR_RECORD_TIMED_OUT 9705L +#define DNS_ERROR_NAME_NOT_IN_ZONE 9706L +#define DNS_ERROR_CNAME_LOOP 9707L +#define DNS_ERROR_NODE_IS_CNAME 9708L +#define DNS_ERROR_CNAME_COLLISION 9709L +#define DNS_ERROR_RECORD_ONLY_AT_ZONE_ROOT 9710L +#define DNS_ERROR_RECORD_ALREADY_EXISTS 9711L +#define DNS_ERROR_SECONDARY_DATA 9712L +#define DNS_ERROR_NO_CREATE_CACHE_DATA 9713L +#define DNS_ERROR_NAME_DOES_NOT_EXIST 9714L +#define DNS_WARNING_PTR_CREATE_FAILED 9715L +#define DNS_WARNING_DOMAIN_UNDELETED 9716L +#define DNS_ERROR_DS_UNAVAILABLE 9717L +#define DNS_ERROR_DS_ZONE_ALREADY_EXISTS 9718L +#define DNS_ERROR_NO_BOOTFILE_IF_DS_ZONE 9719L +#define DNS_ERROR_OPERATION_BASE 9750 +#define DNS_INFO_AXFR_COMPLETE 9751L +#define DNS_ERROR_AXFR 9752L +#define DNS_INFO_ADDED_LOCAL_WINS 9753L +#define DNS_ERROR_SECURE_BASE 9800 +#define DNS_STATUS_CONTINUE_NEEDED 9801L +#define DNS_ERROR_SETUP_BASE 9850 +#define DNS_ERROR_NO_TCPIP 9851L +#define DNS_ERROR_NO_DNS_SERVERS 9852L +#define DNS_ERROR_DP_BASE 9900 +#define DNS_ERROR_DP_DOES_NOT_EXIST 9901L +#define DNS_ERROR_DP_ALREADY_EXISTS 9902L +#define DNS_ERROR_DP_NOT_ENLISTED 9903L +#define DNS_ERROR_DP_ALREADY_ENLISTED 9904L +#define DNS_ERROR_DP_NOT_AVAILABLE 9905L +#define DNS_ERROR_DP_FSMO_ERROR 9906L + +#ifndef WSABASEERR +#define WSABASEERR 10000 +#define WSAEINTR 10004L +#define WSAEBADF 10009L +#define WSAEACCES 10013L +#define WSAEFAULT 10014L +#define WSAEINVAL 10022L +#define WSAEMFILE 10024L +#define WSAEWOULDBLOCK 10035L +#define WSAEINPROGRESS 10036L +#define WSAEALREADY 10037L +#define WSAENOTSOCK 10038L +#define WSAEDESTADDRREQ 10039L +#define WSAEMSGSIZE 10040L +#define WSAEPROTOTYPE 10041L +#define WSAENOPROTOOPT 10042L +#define WSAEPROTONOSUPPORT 10043L +#define WSAESOCKTNOSUPPORT 10044L +#define WSAEOPNOTSUPP 10045L +#define WSAEPFNOSUPPORT 10046L +#define WSAEAFNOSUPPORT 10047L +#define WSAEADDRINUSE 10048L +#define WSAEADDRNOTAVAIL 10049L +#define WSAENETDOWN 10050L +#define WSAENETUNREACH 10051L +#define WSAENETRESET 10052L +#define WSAECONNABORTED 10053L +#define WSAECONNRESET 10054L +#define WSAENOBUFS 10055L +#define WSAEISCONN 10056L +#define WSAENOTCONN 10057L +#define WSAESHUTDOWN 10058L +#define WSAETOOMANYREFS 10059L +#define WSAETIMEDOUT 10060L +#define WSAECONNREFUSED 10061L +#define WSAELOOP 10062L +#define WSAENAMETOOLONG 10063L +#define WSAEHOSTDOWN 10064L +#define WSAEHOSTUNREACH 10065L +#define WSAENOTEMPTY 10066L +#define WSAEPROCLIM 10067L +#define WSAEUSERS 10068L +#define WSAEDQUOT 10069L +#define WSAESTALE 10070L +#define WSAEREMOTE 10071L +#define WSASYSNOTREADY 10091L +#define WSAVERNOTSUPPORTED 10092L +#define WSANOTINITIALISED 10093L +#define WSAEDISCON 10101L +#define WSAENOMORE 10102L +#define WSAECANCELLED 10103L +#define WSAEINVALIDPROCTABLE 10104L +#define WSAEINVALIDPROVIDER 10105L +#define WSAEPROVIDERFAILEDINIT 10106L +#define WSASYSCALLFAILURE 10107L +#define WSASERVICE_NOT_FOUND 10108L +#define WSATYPE_NOT_FOUND 10109L +#define WSA_E_NO_MORE 10110L +#define WSA_E_CANCELLED 10111L +#define WSAEREFUSED 10112L +#ifndef WSAHOST_NOT_FOUND +#define WSAHOST_NOT_FOUND 11001L +#endif +#ifndef WSATRY_AGAIN +#define WSATRY_AGAIN 11002L +#endif +#ifndef WSANO_RECOVERY +#define WSANO_RECOVERY 11003L +#endif +#ifndef WSANO_DATA +#define WSANO_DATA 11004L +#endif +#ifndef WSA_QOS_RECEIVERS +#define WSA_QOS_RECEIVERS 11005L +#endif +#ifndef WSA_QOS_SENDERS +#define WSA_QOS_SENDERS 11006L +#endif +#ifndef WSA_QOS_NO_SENDERS +#define WSA_QOS_NO_SENDERS 11007L +#endif +#ifndef WSA_QOS_NO_RECEIVERS +#define WSA_QOS_NO_RECEIVERS 11008L +#endif +#ifndef WSA_QOS_REQUEST_CONFIRMED +#define WSA_QOS_REQUEST_CONFIRMED 11009L +#endif +#ifndef WSA_QOS_ADMISSION_FAILURE +#define WSA_QOS_ADMISSION_FAILURE 11010L +#endif +#ifndef WSA_QOS_POLICY_FAILURE +#define WSA_QOS_POLICY_FAILURE 11011L +#endif +#ifndef WSA_QOS_BAD_STYLE +#define WSA_QOS_BAD_STYLE 11012L +#endif +#ifndef WSA_QOS_BAD_OBJECT +#define WSA_QOS_BAD_OBJECT 11013L +#endif +#ifndef WSA_QOS_TRAFFIC_CTRL_ERROR +#define WSA_QOS_TRAFFIC_CTRL_ERROR 11014L +#endif +#ifndef WSA_QOS_GENERIC_ERROR +#define WSA_QOS_GENERIC_ERROR 11015L +#endif +#ifndef WSA_QOS_ESERVICETYPE +#define WSA_QOS_ESERVICETYPE 11016L +#endif +#ifndef WSA_QOS_EFLOWSPEC +#define WSA_QOS_EFLOWSPEC 11017L +#endif +#ifndef WSA_QOS_EPROVSPECBUF +#define WSA_QOS_EPROVSPECBUF 11018L +#endif +#ifndef WSA_QOS_EFILTERSTYLE +#define WSA_QOS_EFILTERSTYLE 11019L +#endif +#ifndef WSA_QOS_EFILTERTYPE +#define WSA_QOS_EFILTERTYPE 11020L +#endif +#ifndef WSA_QOS_EFILTERCOUNT +#define WSA_QOS_EFILTERCOUNT 11021L +#endif +#ifndef WSA_QOS_EOBJLENGTH +#define WSA_QOS_EOBJLENGTH 11022L +#endif +#ifndef WSA_QOS_EFLOWCOUNT +#define WSA_QOS_EFLOWCOUNT 11023L +#endif +#ifndef WSA_QOS_EUNKNOWNPSOBJ +#define WSA_QOS_EUNKNOWNPSOBJ 11024L +#endif +#ifndef WSA_QOS_EPOLICYOBJ +#define WSA_QOS_EPOLICYOBJ 11025L +#endif +#ifndef WSA_QOS_EFLOWDESC +#define WSA_QOS_EFLOWDESC 11026L +#endif +#ifndef WSA_QOS_EPSFLOWSPEC +#define WSA_QOS_EPSFLOWSPEC 11027L +#endif +#ifndef WSA_QOS_EPSFILTERSPEC +#define WSA_QOS_EPSFILTERSPEC 11028L +#endif +#ifndef WSA_QOS_ESDMODEOBJ +#define WSA_QOS_ESDMODEOBJ 11029L +#endif +#ifndef WSA_QOS_ESHAPERATEOBJ +#define WSA_QOS_ESHAPERATEOBJ 11030L +#endif +#ifndef WSA_QOS_RESERVED_PETYPE +#define WSA_QOS_RESERVED_PETYPE 11031L +#endif +#endif /* WSABASEERR */ + +#define ERROR_SXS_SECTION_NOT_FOUND 14000L +#define ERROR_SXS_CANT_GEN_ACTCTX 14001L +#define ERROR_SXS_INVALID_ACTCTXDATA_FORMAT 14002L +#define ERROR_SXS_ASSEMBLY_NOT_FOUND 14003L +#define ERROR_SXS_MANIFEST_FORMAT_ERROR 14004L +#define ERROR_SXS_MANIFEST_PARSE_ERROR 14005L +#define ERROR_SXS_ACTIVATION_CONTEXT_DISABLED 14006L +#define ERROR_SXS_KEY_NOT_FOUND 14007L +#define ERROR_SXS_VERSION_CONFLICT 14008L +#define ERROR_SXS_WRONG_SECTION_TYPE 14009L +#define ERROR_SXS_THREAD_QUERIES_DISABLED 14010L +#define ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET 14011L +#define ERROR_SXS_UNKNOWN_ENCODING_GROUP 14012L +#define ERROR_SXS_UNKNOWN_ENCODING 14013L +#define ERROR_SXS_INVALID_XML_NAMESPACE_URI 14014L +#define ERROR_SXS_ROOT_MANIFEST_DEPENDENCY_NOT_INSTALLED 14015L +#define ERROR_SXS_LEAF_MANIFEST_DEPENDENCY_NOT_INSTALLED 14016L +#define ERROR_SXS_INVALID_ASSEMBLY_IDENTITY_ATTRIBUTE 14017L +#define ERROR_SXS_MANIFEST_MISSING_REQUIRED_DEFAULT_NAMESPACE 14018L +#define ERROR_SXS_MANIFEST_INVALID_REQUIRED_DEFAULT_NAMESPACE 14019L +#define ERROR_SXS_PRIVATE_MANIFEST_CROSS_PATH_WITH_REPARSE_POINT 14020L +#define ERROR_SXS_DUPLICATE_DLL_NAME 14021L +#define ERROR_SXS_DUPLICATE_WINDOWCLASS_NAME 14022L +#define ERROR_SXS_DUPLICATE_CLSID 14023L +#define ERROR_SXS_DUPLICATE_IID 14024L +#define ERROR_SXS_DUPLICATE_TLBID 14025L +#define ERROR_SXS_DUPLICATE_PROGID 14026L +#define ERROR_SXS_DUPLICATE_ASSEMBLY_NAME 14027L +#define ERROR_SXS_FILE_HASH_MISMATCH 14028L +#define ERROR_SXS_POLICY_PARSE_ERROR 14029L +#define ERROR_SXS_XML_E_MISSINGQUOTE 14030L +#define ERROR_SXS_XML_E_COMMENTSYNTAX 14031L +#define ERROR_SXS_XML_E_BADSTARTNAMECHAR 14032L +#define ERROR_SXS_XML_E_BADNAMECHAR 14033L +#define ERROR_SXS_XML_E_BADCHARINSTRING 14034L +#define ERROR_SXS_XML_E_XMLDECLSYNTAX 14035L +#define ERROR_SXS_XML_E_BADCHARDATA 14036L +#define ERROR_SXS_XML_E_MISSINGWHITESPACE 14037L +#define ERROR_SXS_XML_E_EXPECTINGTAGEND 14038L +#define ERROR_SXS_XML_E_MISSINGSEMICOLON 14039L +#define ERROR_SXS_XML_E_UNBALANCEDPAREN 14040L +#define ERROR_SXS_XML_E_INTERNALERROR 14041L +#define ERROR_SXS_XML_E_UNEXPECTED_WHITESPACE 14042L +#define ERROR_SXS_XML_E_INCOMPLETE_ENCODING 14043L +#define ERROR_SXS_XML_E_MISSING_PAREN 14044L +#define ERROR_SXS_XML_E_EXPECTINGCLOSEQUOTE 14045L +#define ERROR_SXS_XML_E_MULTIPLE_COLONS 14046L +#define ERROR_SXS_XML_E_INVALID_DECIMAL 14047L +#define ERROR_SXS_XML_E_INVALID_HEXIDECIMAL 14048L +#define ERROR_SXS_XML_E_INVALID_UNICODE 14049L +#define ERROR_SXS_XML_E_WHITESPACEORQUESTIONMARK 14050L +#define ERROR_SXS_XML_E_UNEXPECTEDENDTAG 14051L +#define ERROR_SXS_XML_E_UNCLOSEDTAG 14052L +#define ERROR_SXS_XML_E_DUPLICATEATTRIBUTE 14053L +#define ERROR_SXS_XML_E_MULTIPLEROOTS 14054L +#define ERROR_SXS_XML_E_INVALIDATROOTLEVEL 14055L +#define ERROR_SXS_XML_E_BADXMLDECL 14056L +#define ERROR_SXS_XML_E_MISSINGROOT 14057L +#define ERROR_SXS_XML_E_UNEXPECTEDEOF 14058L +#define ERROR_SXS_XML_E_BADPEREFINSUBSET 14059L +#define ERROR_SXS_XML_E_UNCLOSEDSTARTTAG 14060L +#define ERROR_SXS_XML_E_UNCLOSEDENDTAG 14061L +#define ERROR_SXS_XML_E_UNCLOSEDSTRING 14062L +#define ERROR_SXS_XML_E_UNCLOSEDCOMMENT 14063L +#define ERROR_SXS_XML_E_UNCLOSEDDECL 14064L +#define ERROR_SXS_XML_E_UNCLOSEDCDATA 14065L +#define ERROR_SXS_XML_E_RESERVEDNAMESPACE 14066L +#define ERROR_SXS_XML_E_INVALIDENCODING 14067L +#define ERROR_SXS_XML_E_INVALIDSWITCH 14068L +#define ERROR_SXS_XML_E_BADXMLCASE 14069L +#define ERROR_SXS_XML_E_INVALID_STANDALONE 14070L +#define ERROR_SXS_XML_E_UNEXPECTED_STANDALONE 14071L +#define ERROR_SXS_XML_E_INVALID_VERSION 14072L +#define ERROR_SXS_XML_E_MISSINGEQUALS 14073L +#define ERROR_SXS_PROTECTION_RECOVERY_FAILED 14074L +#define ERROR_SXS_PROTECTION_PUBLIC_KEY_TOO_SHORT 14075L +#define ERROR_SXS_PROTECTION_CATALOG_NOT_VALID 14076L +#define ERROR_SXS_UNTRANSLATABLE_HRESULT 14077L +#define ERROR_SXS_PROTECTION_CATALOG_FILE_MISSING 14078L +#define ERROR_SXS_MISSING_ASSEMBLY_IDENTITY_ATTRIBUTE 14079L +#define ERROR_SXS_INVALID_ASSEMBLY_IDENTITY_ATTRIBUTE_NAME 14080L +#define ERROR_IPSEC_QM_POLICY_EXISTS 13000L +#define ERROR_IPSEC_QM_POLICY_NOT_FOUND 13001L +#define ERROR_IPSEC_QM_POLICY_IN_USE 13002L +#define ERROR_IPSEC_MM_POLICY_EXISTS 13003L +#define ERROR_IPSEC_MM_POLICY_NOT_FOUND 13004L +#define ERROR_IPSEC_MM_POLICY_IN_USE 13005L +#define ERROR_IPSEC_MM_FILTER_EXISTS 13006L +#define ERROR_IPSEC_MM_FILTER_NOT_FOUND 13007L +#define ERROR_IPSEC_TRANSPORT_FILTER_EXISTS 13008L +#define ERROR_IPSEC_TRANSPORT_FILTER_NOT_FOUND 13009L +#define ERROR_IPSEC_MM_AUTH_EXISTS 13010L +#define ERROR_IPSEC_MM_AUTH_NOT_FOUND 13011L +#define ERROR_IPSEC_MM_AUTH_IN_USE 13012L +#define ERROR_IPSEC_DEFAULT_MM_POLICY_NOT_FOUND 13013L +#define ERROR_IPSEC_DEFAULT_MM_AUTH_NOT_FOUND 13014L +#define ERROR_IPSEC_DEFAULT_QM_POLICY_NOT_FOUND 13015L +#define ERROR_IPSEC_TUNNEL_FILTER_EXISTS 13016L +#define ERROR_IPSEC_TUNNEL_FILTER_NOT_FOUND 13017L +#define ERROR_IPSEC_MM_FILTER_PENDING_DELETION 13018L +#define ERROR_IPSEC_TRANSPORT_FILTER_PENDING_DELETION 13019L +#define ERROR_IPSEC_TUNNEL_FILTER_PENDING_DELETION 13020L +#define ERROR_IPSEC_MM_POLICY_PENDING_DELETION 13021L +#define ERROR_IPSEC_MM_AUTH_PENDING_DELETION 13022L +#define ERROR_IPSEC_QM_POLICY_PENDING_DELETION 13023L +#define WARNING_IPSEC_MM_POLICY_PRUNED 13024L +#define WARNING_IPSEC_QM_POLICY_PRUNED 13025L +#define ERROR_IPSEC_IKE_NEG_STATUS_BEGIN 13800L +#define ERROR_IPSEC_IKE_AUTH_FAIL 13801L +#define ERROR_IPSEC_IKE_ATTRIB_FAIL 13802L +#define ERROR_IPSEC_IKE_NEGOTIATION_PENDING 13803L +#define ERROR_IPSEC_IKE_GENERAL_PROCESSING_ERROR 13804L +#define ERROR_IPSEC_IKE_TIMED_OUT 13805L +#define ERROR_IPSEC_IKE_NO_CERT 13806L +#define ERROR_IPSEC_IKE_SA_DELETED 13807L +#define ERROR_IPSEC_IKE_SA_REAPED 13808L +#define ERROR_IPSEC_IKE_MM_ACQUIRE_DROP 13809L +#define ERROR_IPSEC_IKE_QM_ACQUIRE_DROP 13810L +#define ERROR_IPSEC_IKE_QUEUE_DROP_MM 13811L +#define ERROR_IPSEC_IKE_QUEUE_DROP_NO_MM 13812L +#define ERROR_IPSEC_IKE_DROP_NO_RESPONSE 13813L +#define ERROR_IPSEC_IKE_MM_DELAY_DROP 13814L +#define ERROR_IPSEC_IKE_QM_DELAY_DROP 13815L +#define ERROR_IPSEC_IKE_ERROR 13816L +#define ERROR_IPSEC_IKE_CRL_FAILED 13817L +#define ERROR_IPSEC_IKE_INVALID_KEY_USAGE 13818L +#define ERROR_IPSEC_IKE_INVALID_CERT_TYPE 13819L +#define ERROR_IPSEC_IKE_NO_PRIVATE_KEY 13820L +#define ERROR_IPSEC_IKE_DH_FAIL 13822L +#define ERROR_IPSEC_IKE_INVALID_HEADER 13824L +#define ERROR_IPSEC_IKE_NO_POLICY 13825L +#define ERROR_IPSEC_IKE_INVALID_SIGNATURE 13826L +#define ERROR_IPSEC_IKE_KERBEROS_ERROR 13827L +#define ERROR_IPSEC_IKE_NO_PUBLIC_KEY 13828L +#define ERROR_IPSEC_IKE_PROCESS_ERR 13829L +#define ERROR_IPSEC_IKE_PROCESS_ERR_SA 13830L +#define ERROR_IPSEC_IKE_PROCESS_ERR_PROP 13831L +#define ERROR_IPSEC_IKE_PROCESS_ERR_TRANS 13832L +#define ERROR_IPSEC_IKE_PROCESS_ERR_KE 13833L +#define ERROR_IPSEC_IKE_PROCESS_ERR_ID 13834L +#define ERROR_IPSEC_IKE_PROCESS_ERR_CERT 13835L +#define ERROR_IPSEC_IKE_PROCESS_ERR_CERT_REQ 13836L +#define ERROR_IPSEC_IKE_PROCESS_ERR_HASH 13837L +#define ERROR_IPSEC_IKE_PROCESS_ERR_SIG 13838L +#define ERROR_IPSEC_IKE_PROCESS_ERR_NONCE 13839L +#define ERROR_IPSEC_IKE_PROCESS_ERR_NOTIFY 13840L +#define ERROR_IPSEC_IKE_PROCESS_ERR_DELETE 13841L +#define ERROR_IPSEC_IKE_PROCESS_ERR_VENDOR 13842L +#define ERROR_IPSEC_IKE_INVALID_PAYLOAD 13843L +#define ERROR_IPSEC_IKE_LOAD_SOFT_SA 13844L +#define ERROR_IPSEC_IKE_SOFT_SA_TORN_DOWN 13845L +#define ERROR_IPSEC_IKE_INVALID_COOKIE 13846L +#define ERROR_IPSEC_IKE_NO_PEER_CERT 13847L +#define ERROR_IPSEC_IKE_PEER_CRL_FAILED 13848L +#define ERROR_IPSEC_IKE_POLICY_CHANGE 13849L +#define ERROR_IPSEC_IKE_NO_MM_POLICY 13850L +#define ERROR_IPSEC_IKE_NOTCBPRIV 13851L +#define ERROR_IPSEC_IKE_SECLOADFAIL 13852L +#define ERROR_IPSEC_IKE_FAILSSPINIT 13853L +#define ERROR_IPSEC_IKE_FAILQUERYSSP 13854L +#define ERROR_IPSEC_IKE_SRVACQFAIL 13855L +#define ERROR_IPSEC_IKE_SRVQUERYCRED 13856L +#define ERROR_IPSEC_IKE_GETSPIFAIL 13857L +#define ERROR_IPSEC_IKE_INVALID_FILTER 13858L +#define ERROR_IPSEC_IKE_OUT_OF_MEMORY 13859L +#define ERROR_IPSEC_IKE_ADD_UPDATE_KEY_FAILED 13860L +#define ERROR_IPSEC_IKE_INVALID_POLICY 13861L +#define ERROR_IPSEC_IKE_UNKNOWN_DOI 13862L +#define ERROR_IPSEC_IKE_INVALID_SITUATION 13863L +#define ERROR_IPSEC_IKE_DH_FAILURE 13864L +#define ERROR_IPSEC_IKE_INVALID_GROUP 13865L +#define ERROR_IPSEC_IKE_ENCRYPT 13866L +#define ERROR_IPSEC_IKE_DECRYPT 13867L +#define ERROR_IPSEC_IKE_POLICY_MATCH 13868L +#define ERROR_IPSEC_IKE_UNSUPPORTED_ID 13869L +#define ERROR_IPSEC_IKE_INVALID_HASH 13870L +#define ERROR_IPSEC_IKE_INVALID_HASH_ALG 13871L +#define ERROR_IPSEC_IKE_INVALID_HASH_SIZE 13872L +#define ERROR_IPSEC_IKE_INVALID_ENCRYPT_ALG 13873L +#define ERROR_IPSEC_IKE_INVALID_AUTH_ALG 13874L +#define ERROR_IPSEC_IKE_INVALID_SIG 13875L +#define ERROR_IPSEC_IKE_LOAD_FAILED 13876L +#define ERROR_IPSEC_IKE_RPC_DELETE 13877L +#define ERROR_IPSEC_IKE_BENIGN_REINIT 13878L +#define ERROR_IPSEC_IKE_INVALID_RESPONDER_LIFETIME_NOTIFY 13879L +#define ERROR_IPSEC_IKE_INVALID_CERT_KEYLEN 13881L +#define ERROR_IPSEC_IKE_MM_LIMIT 13882L +#define ERROR_IPSEC_IKE_NEGOTIATION_DISABLED 13883L +#define ERROR_IPSEC_IKE_NEG_STATUS_END 13884L +#define SEVERITY_SUCCESS 0 +#define SEVERITY_ERROR 1 +#define SUCCEEDED(hr) ((HRESULT)(hr) >= 0) +#define FAILED(hr) ((HRESULT)(hr) < 0) +#define IS_ERROR(Status) ((unsigned long)(Status) >> 31==SEVERITY_ERROR) +#define HRESULT_CODE(hr) ((hr) & 0xFFFF) +#define SCODE_CODE(sc) ((sc) & 0xFFFF) +#define HRESULT_FACILITY(hr) (((hr) >> 16) & 0x1fff) +#define SCODE_FACILITY(sc) (((sc) >> 16) & 0x1fff) +#define HRESULT_SEVERITY(hr) (((hr) >> 31) & 0x1) +#define SCODE_SEVERITY(sc) (((sc) >> 31) & 0x1) +#define MAKE_HRESULT(sev,fac,code) ((HRESULT) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) | ((unsigned long)(code)))) +#define MAKE_SCODE(sev,fac,code) ((SCODE) (((unsigned long)(sev)<<31) | ((unsigned long)(fac)<<16) | ((unsigned long)(code)))) +#define FACILITY_NT_BIT 0x10000000 +#define __HRESULT_FROM_WIN32(x) ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000))) +#ifdef INLINE_HRESULT_FROM_WIN32 +#ifndef _HRESULT_DEFINED +#define _HRESULT_DEFINED +typedef long HRESULT; +#endif +__CRT_INLINE HRESULT HRESULT_FROM_WIN32(long x) { return x <= 0 ? (HRESULT)x : (HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000);} +#else +#define HRESULT_FROM_WIN32(x) __HRESULT_FROM_WIN32(x) +#endif +#define HRESULT_FROM_NT(x) ((HRESULT) ((x) | FACILITY_NT_BIT)) +#define GetScode(hr) ((SCODE) (hr)) +#define ResultFromScode(sc) ((HRESULT) (sc)) +#define PropagateResult(hrPrevious,scBase) ((HRESULT) scBase) +#ifdef RC_INVOKED +#define _HRESULT_TYPEDEF_(_sc) _sc +#else +#define _HRESULT_TYPEDEF_(_sc) ((HRESULT)_sc) +#endif +#define NOERROR 0 +#define E_UNEXPECTED _HRESULT_TYPEDEF_(0x8000FFFFL) +#define E_NOTIMPL _HRESULT_TYPEDEF_(0x80004001L) +#define E_OUTOFMEMORY _HRESULT_TYPEDEF_(0x8007000EL) +#define E_INVALIDARG _HRESULT_TYPEDEF_(0x80070057L) +#define E_NOINTERFACE _HRESULT_TYPEDEF_(0x80004002L) +#define E_POINTER _HRESULT_TYPEDEF_(0x80004003L) +#define E_HANDLE _HRESULT_TYPEDEF_(0x80070006L) +#define E_ABORT _HRESULT_TYPEDEF_(0x80004004L) +#define E_FAIL _HRESULT_TYPEDEF_(0x80004005L) +#define E_ACCESSDENIED _HRESULT_TYPEDEF_(0x80070005L) +#define E_PENDING _HRESULT_TYPEDEF_(0x8000000AL) +#define CO_E_INIT_TLS _HRESULT_TYPEDEF_(0x80004006L) +#define CO_E_INIT_SHARED_ALLOCATOR _HRESULT_TYPEDEF_(0x80004007L) +#define CO_E_INIT_MEMORY_ALLOCATOR _HRESULT_TYPEDEF_(0x80004008L) +#define CO_E_INIT_CLASS_CACHE _HRESULT_TYPEDEF_(0x80004009L) +#define CO_E_INIT_RPC_CHANNEL _HRESULT_TYPEDEF_(0x8000400AL) +#define CO_E_INIT_TLS_SET_CHANNEL_CONTROL _HRESULT_TYPEDEF_(0x8000400BL) +#define CO_E_INIT_TLS_CHANNEL_CONTROL _HRESULT_TYPEDEF_(0x8000400CL) +#define CO_E_INIT_UNACCEPTED_USER_ALLOCATOR _HRESULT_TYPEDEF_(0x8000400DL) +#define CO_E_INIT_SCM_MUTEX_EXISTS _HRESULT_TYPEDEF_(0x8000400EL) +#define CO_E_INIT_SCM_FILE_MAPPING_EXISTS _HRESULT_TYPEDEF_(0x8000400FL) +#define CO_E_INIT_SCM_MAP_VIEW_OF_FILE _HRESULT_TYPEDEF_(0x80004010L) +#define CO_E_INIT_SCM_EXEC_FAILURE _HRESULT_TYPEDEF_(0x80004011L) +#define CO_E_INIT_ONLY_SINGLE_THREADED _HRESULT_TYPEDEF_(0x80004012L) +#define CO_E_CANT_REMOTE _HRESULT_TYPEDEF_(0x80004013L) +#define CO_E_BAD_SERVER_NAME _HRESULT_TYPEDEF_(0x80004014L) +#define CO_E_WRONG_SERVER_IDENTITY _HRESULT_TYPEDEF_(0x80004015L) +#define CO_E_OLE1DDE_DISABLED _HRESULT_TYPEDEF_(0x80004016L) +#define CO_E_RUNAS_SYNTAX _HRESULT_TYPEDEF_(0x80004017L) +#define CO_E_CREATEPROCESS_FAILURE _HRESULT_TYPEDEF_(0x80004018L) +#define CO_E_RUNAS_CREATEPROCESS_FAILURE _HRESULT_TYPEDEF_(0x80004019L) +#define CO_E_RUNAS_LOGON_FAILURE _HRESULT_TYPEDEF_(0x8000401AL) +#define CO_E_LAUNCH_PERMSSION_DENIED _HRESULT_TYPEDEF_(0x8000401BL) +#define CO_E_START_SERVICE_FAILURE _HRESULT_TYPEDEF_(0x8000401CL) +#define CO_E_REMOTE_COMMUNICATION_FAILURE _HRESULT_TYPEDEF_(0x8000401DL) +#define CO_E_SERVER_START_TIMEOUT _HRESULT_TYPEDEF_(0x8000401EL) +#define CO_E_CLSREG_INCONSISTENT _HRESULT_TYPEDEF_(0x8000401FL) +#define CO_E_IIDREG_INCONSISTENT _HRESULT_TYPEDEF_(0x80004020L) +#define CO_E_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x80004021L) +#define CO_E_RELOAD_DLL _HRESULT_TYPEDEF_(0x80004022L) +#define CO_E_MSI_ERROR _HRESULT_TYPEDEF_(0x80004023L) +#define CO_E_ATTEMPT_TO_CREATE_OUTSIDE_CLIENT_CONTEXT _HRESULT_TYPEDEF_(0x80004024L) +#define CO_E_SERVER_PAUSED _HRESULT_TYPEDEF_(0x80004025L) +#define CO_E_SERVER_NOT_PAUSED _HRESULT_TYPEDEF_(0x80004026L) +#define CO_E_CLASS_DISABLED _HRESULT_TYPEDEF_(0x80004027L) +#define CO_E_CLRNOTAVAILABLE _HRESULT_TYPEDEF_(0x80004028L) +#define CO_E_ASYNC_WORK_REJECTED _HRESULT_TYPEDEF_(0x80004029L) +#define CO_E_SERVER_INIT_TIMEOUT _HRESULT_TYPEDEF_(0x8000402AL) +#define CO_E_NO_SECCTX_IN_ACTIVATE _HRESULT_TYPEDEF_(0x8000402BL) +#define CO_E_TRACKER_CONFIG _HRESULT_TYPEDEF_(0x80004030L) +#define CO_E_THREADPOOL_CONFIG _HRESULT_TYPEDEF_(0x80004031L) +#define CO_E_SXS_CONFIG _HRESULT_TYPEDEF_(0x80004032L) +#define CO_E_MALFORMED_SPN _HRESULT_TYPEDEF_(0x80004033L) +#define S_OK ((HRESULT)0x00000000L) +#define S_FALSE ((HRESULT)0x00000001L) +#define OLE_E_FIRST ((HRESULT)0x80040000L) +#define OLE_E_LAST ((HRESULT)0x800400FFL) +#define OLE_S_FIRST ((HRESULT)0x00040000L) +#define OLE_S_LAST ((HRESULT)0x000400FFL) +#define OLE_E_OLEVERB _HRESULT_TYPEDEF_(0x80040000L) +#define OLE_E_ADVF _HRESULT_TYPEDEF_(0x80040001L) +#define OLE_E_ENUM_NOMORE _HRESULT_TYPEDEF_(0x80040002L) +#define OLE_E_ADVISENOTSUPPORTED _HRESULT_TYPEDEF_(0x80040003L) +#define OLE_E_NOCONNECTION _HRESULT_TYPEDEF_(0x80040004L) +#define OLE_E_NOTRUNNING _HRESULT_TYPEDEF_(0x80040005L) +#define OLE_E_NOCACHE _HRESULT_TYPEDEF_(0x80040006L) +#define OLE_E_BLANK _HRESULT_TYPEDEF_(0x80040007L) +#define OLE_E_CLASSDIFF _HRESULT_TYPEDEF_(0x80040008L) +#define OLE_E_CANT_GETMONIKER _HRESULT_TYPEDEF_(0x80040009L) +#define OLE_E_CANT_BINDTOSOURCE _HRESULT_TYPEDEF_(0x8004000AL) +#define OLE_E_STATIC _HRESULT_TYPEDEF_(0x8004000BL) +#define OLE_E_PROMPTSAVECANCELLED _HRESULT_TYPEDEF_(0x8004000CL) +#define OLE_E_INVALIDRECT _HRESULT_TYPEDEF_(0x8004000DL) +#define OLE_E_WRONGCOMPOBJ _HRESULT_TYPEDEF_(0x8004000EL) +#define OLE_E_INVALIDHWND _HRESULT_TYPEDEF_(0x8004000FL) +#define OLE_E_NOT_INPLACEACTIVE _HRESULT_TYPEDEF_(0x80040010L) +#define OLE_E_CANTCONVERT _HRESULT_TYPEDEF_(0x80040011L) +#define OLE_E_NOSTORAGE _HRESULT_TYPEDEF_(0x80040012L) +#define DV_E_FORMATETC _HRESULT_TYPEDEF_(0x80040064L) +#define DV_E_DVTARGETDEVICE _HRESULT_TYPEDEF_(0x80040065L) +#define DV_E_STGMEDIUM _HRESULT_TYPEDEF_(0x80040066L) +#define DV_E_STATDATA _HRESULT_TYPEDEF_(0x80040067L) +#define DV_E_LINDEX _HRESULT_TYPEDEF_(0x80040068L) +#define DV_E_TYMED _HRESULT_TYPEDEF_(0x80040069L) +#define DV_E_CLIPFORMAT _HRESULT_TYPEDEF_(0x8004006AL) +#define DV_E_DVASPECT _HRESULT_TYPEDEF_(0x8004006BL) +#define DV_E_DVTARGETDEVICE_SIZE _HRESULT_TYPEDEF_(0x8004006CL) +#define DV_E_NOIVIEWOBJECT _HRESULT_TYPEDEF_(0x8004006DL) +#define DRAGDROP_E_FIRST 0x80040100L +#define DRAGDROP_E_LAST 0x8004010FL +#define DRAGDROP_S_FIRST 0x00040100L +#define DRAGDROP_S_LAST 0x0004010FL +#define DRAGDROP_E_NOTREGISTERED _HRESULT_TYPEDEF_(0x80040100L) +#define DRAGDROP_E_ALREADYREGISTERED _HRESULT_TYPEDEF_(0x80040101L) +#define DRAGDROP_E_INVALIDHWND _HRESULT_TYPEDEF_(0x80040102L) +#define CLASSFACTORY_E_FIRST 0x80040110L +#define CLASSFACTORY_E_LAST 0x8004011FL +#define CLASSFACTORY_S_FIRST 0x00040110L +#define CLASSFACTORY_S_LAST 0x0004011FL +#define CLASS_E_NOAGGREGATION _HRESULT_TYPEDEF_(0x80040110L) +#define CLASS_E_CLASSNOTAVAILABLE _HRESULT_TYPEDEF_(0x80040111L) +#define CLASS_E_NOTLICENSED _HRESULT_TYPEDEF_(0x80040112L) +#define MARSHAL_E_FIRST 0x80040120L +#define MARSHAL_E_LAST 0x8004012FL +#define MARSHAL_S_FIRST 0x00040120L +#define MARSHAL_S_LAST 0x0004012FL +#define DATA_E_FIRST 0x80040130L +#define DATA_E_LAST 0x8004013FL +#define DATA_S_FIRST 0x00040130L +#define DATA_S_LAST 0x0004013FL +#define VIEW_E_FIRST 0x80040140L +#define VIEW_E_LAST 0x8004014FL +#define VIEW_S_FIRST 0x00040140L +#define VIEW_S_LAST 0x0004014FL +#define VIEW_E_DRAW _HRESULT_TYPEDEF_(0x80040140L) +#define REGDB_E_FIRST 0x80040150L +#define REGDB_E_LAST 0x8004015FL +#define REGDB_S_FIRST 0x00040150L +#define REGDB_S_LAST 0x0004015FL +#define REGDB_E_READREGDB _HRESULT_TYPEDEF_(0x80040150L) +#define REGDB_E_WRITEREGDB _HRESULT_TYPEDEF_(0x80040151L) +#define REGDB_E_KEYMISSING _HRESULT_TYPEDEF_(0x80040152L) +#define REGDB_E_INVALIDVALUE _HRESULT_TYPEDEF_(0x80040153L) +#define REGDB_E_CLASSNOTREG _HRESULT_TYPEDEF_(0x80040154L) +#define REGDB_E_IIDNOTREG _HRESULT_TYPEDEF_(0x80040155L) +#define REGDB_E_BADTHREADINGMODEL _HRESULT_TYPEDEF_(0x80040156L) +#define CAT_E_FIRST 0x80040160L +#define CAT_E_LAST 0x80040161L +#define CAT_E_CATIDNOEXIST _HRESULT_TYPEDEF_(0x80040160L) +#define CAT_E_NODESCRIPTION _HRESULT_TYPEDEF_(0x80040161L) +#define CS_E_FIRST 0x80040164L +#define CS_E_LAST 0x8004016FL +#define CS_E_PACKAGE_NOTFOUND _HRESULT_TYPEDEF_(0x80040164L) +#define CS_E_NOT_DELETABLE _HRESULT_TYPEDEF_(0x80040165L) +#define CS_E_CLASS_NOTFOUND _HRESULT_TYPEDEF_(0x80040166L) +#define CS_E_INVALID_VERSION _HRESULT_TYPEDEF_(0x80040167L) +#define CS_E_NO_CLASSSTORE _HRESULT_TYPEDEF_(0x80040168L) +#define CS_E_OBJECT_NOTFOUND _HRESULT_TYPEDEF_(0x80040169L) +#define CS_E_OBJECT_ALREADY_EXISTS _HRESULT_TYPEDEF_(0x8004016AL) +#define CS_E_INVALID_PATH _HRESULT_TYPEDEF_(0x8004016BL) +#define CS_E_NETWORK_ERROR _HRESULT_TYPEDEF_(0x8004016CL) +#define CS_E_ADMIN_LIMIT_EXCEEDED _HRESULT_TYPEDEF_(0x8004016DL) +#define CS_E_SCHEMA_MISMATCH _HRESULT_TYPEDEF_(0x8004016EL) +#define CS_E_INTERNAL_ERROR _HRESULT_TYPEDEF_(0x8004016FL) +#define CACHE_E_FIRST 0x80040170L +#define CACHE_E_LAST 0x8004017FL +#define CACHE_S_FIRST 0x00040170L +#define CACHE_S_LAST 0x0004017FL +#define CACHE_E_NOCACHE_UPDATED _HRESULT_TYPEDEF_(0x80040170L) +#define OLEOBJ_E_FIRST 0x80040180L +#define OLEOBJ_E_LAST 0x8004018FL +#define OLEOBJ_S_FIRST 0x00040180L +#define OLEOBJ_S_LAST 0x0004018FL +#define OLEOBJ_E_NOVERBS _HRESULT_TYPEDEF_(0x80040180L) +#define OLEOBJ_E_INVALIDVERB _HRESULT_TYPEDEF_(0x80040181L) +#define CLIENTSITE_E_FIRST 0x80040190L +#define CLIENTSITE_E_LAST 0x8004019FL +#define CLIENTSITE_S_FIRST 0x00040190L +#define CLIENTSITE_S_LAST 0x0004019FL +#define INPLACE_E_NOTUNDOABLE _HRESULT_TYPEDEF_(0x800401A0L) +#define INPLACE_E_NOTOOLSPACE _HRESULT_TYPEDEF_(0x800401A1L) +#define INPLACE_E_FIRST 0x800401A0L +#define INPLACE_E_LAST 0x800401AFL +#define INPLACE_S_FIRST 0x000401A0L +#define INPLACE_S_LAST 0x000401AFL +#define ENUM_E_FIRST 0x800401B0L +#define ENUM_E_LAST 0x800401BFL +#define ENUM_S_FIRST 0x000401B0L +#define ENUM_S_LAST 0x000401BFL +#define CONVERT10_E_FIRST 0x800401C0L +#define CONVERT10_E_LAST 0x800401CFL +#define CONVERT10_S_FIRST 0x000401C0L +#define CONVERT10_S_LAST 0x000401CFL +#define CONVERT10_E_OLESTREAM_GET _HRESULT_TYPEDEF_(0x800401C0L) +#define CONVERT10_E_OLESTREAM_PUT _HRESULT_TYPEDEF_(0x800401C1L) +#define CONVERT10_E_OLESTREAM_FMT _HRESULT_TYPEDEF_(0x800401C2L) +#define CONVERT10_E_OLESTREAM_BITMAP_TO_DIB _HRESULT_TYPEDEF_(0x800401C3L) +#define CONVERT10_E_STG_FMT _HRESULT_TYPEDEF_(0x800401C4L) +#define CONVERT10_E_STG_NO_STD_STREAM _HRESULT_TYPEDEF_(0x800401C5L) +#define CONVERT10_E_STG_DIB_TO_BITMAP _HRESULT_TYPEDEF_(0x800401C6L) +#define CLIPBRD_E_FIRST 0x800401D0L +#define CLIPBRD_E_LAST 0x800401DFL +#define CLIPBRD_S_FIRST 0x000401D0L +#define CLIPBRD_S_LAST 0x000401DFL +#define CLIPBRD_E_CANT_OPEN _HRESULT_TYPEDEF_(0x800401D0L) +#define CLIPBRD_E_CANT_EMPTY _HRESULT_TYPEDEF_(0x800401D1L) +#define CLIPBRD_E_CANT_SET _HRESULT_TYPEDEF_(0x800401D2L) +#define CLIPBRD_E_BAD_DATA _HRESULT_TYPEDEF_(0x800401D3L) +#define CLIPBRD_E_CANT_CLOSE _HRESULT_TYPEDEF_(0x800401D4L) +#define MK_E_FIRST 0x800401E0L +#define MK_E_LAST 0x800401EFL +#define MK_S_FIRST 0x000401E0L +#define MK_S_LAST 0x000401EFL +#define MK_E_CONNECTMANUALLY _HRESULT_TYPEDEF_(0x800401E0L) +#define MK_E_EXCEEDEDDEADLINE _HRESULT_TYPEDEF_(0x800401E1L) +#define MK_E_NEEDGENERIC _HRESULT_TYPEDEF_(0x800401E2L) +#define MK_E_UNAVAILABLE _HRESULT_TYPEDEF_(0x800401E3L) +#define MK_E_SYNTAX _HRESULT_TYPEDEF_(0x800401E4L) +#define MK_E_NOOBJECT _HRESULT_TYPEDEF_(0x800401E5L) +#define MK_E_INVALIDEXTENSION _HRESULT_TYPEDEF_(0x800401E6L) +#define MK_E_INTERMEDIATEINTERFACENOTSUPPORTED _HRESULT_TYPEDEF_(0x800401E7L) +#define MK_E_NOTBINDABLE _HRESULT_TYPEDEF_(0x800401E8L) +#define MK_E_NOTBOUND _HRESULT_TYPEDEF_(0x800401E9L) +#define MK_E_CANTOPENFILE _HRESULT_TYPEDEF_(0x800401EAL) +#define MK_E_MUSTBOTHERUSER _HRESULT_TYPEDEF_(0x800401EBL) +#define MK_E_NOINVERSE _HRESULT_TYPEDEF_(0x800401ECL) +#define MK_E_NOSTORAGE _HRESULT_TYPEDEF_(0x800401EDL) +#define MK_E_NOPREFIX _HRESULT_TYPEDEF_(0x800401EEL) +#define MK_E_ENUMERATION_FAILED _HRESULT_TYPEDEF_(0x800401EFL) +#define CO_E_FIRST 0x800401F0L +#define CO_E_LAST 0x800401FFL +#define CO_S_FIRST 0x000401F0L +#define CO_S_LAST 0x000401FFL +#define CO_E_NOTINITIALIZED _HRESULT_TYPEDEF_(0x800401F0L) +#define CO_E_ALREADYINITIALIZED _HRESULT_TYPEDEF_(0x800401F1L) +#define CO_E_CANTDETERMINECLASS _HRESULT_TYPEDEF_(0x800401F2L) +#define CO_E_CLASSSTRING _HRESULT_TYPEDEF_(0x800401F3L) +#define CO_E_IIDSTRING _HRESULT_TYPEDEF_(0x800401F4L) +#define CO_E_APPNOTFOUND _HRESULT_TYPEDEF_(0x800401F5L) +#define CO_E_APPSINGLEUSE _HRESULT_TYPEDEF_(0x800401F6L) +#define CO_E_ERRORINAPP _HRESULT_TYPEDEF_(0x800401F7L) +#define CO_E_DLLNOTFOUND _HRESULT_TYPEDEF_(0x800401F8L) +#define CO_E_ERRORINDLL _HRESULT_TYPEDEF_(0x800401F9L) +#define CO_E_WRONGOSFORAPP _HRESULT_TYPEDEF_(0x800401FAL) +#define CO_E_OBJNOTREG _HRESULT_TYPEDEF_(0x800401FBL) +#define CO_E_OBJISREG _HRESULT_TYPEDEF_(0x800401FCL) +#define CO_E_OBJNOTCONNECTED _HRESULT_TYPEDEF_(0x800401FDL) +#define CO_E_APPDIDNTREG _HRESULT_TYPEDEF_(0x800401FEL) +#define CO_E_RELEASED _HRESULT_TYPEDEF_(0x800401FFL) +#define EVENT_E_FIRST 0x80040200L +#define EVENT_E_LAST 0x8004021FL +#define EVENT_S_FIRST 0x00040200L +#define EVENT_S_LAST 0x0004021FL +#define EVENT_S_SOME_SUBSCRIBERS_FAILED _HRESULT_TYPEDEF_(0x00040200L) +#define EVENT_E_ALL_SUBSCRIBERS_FAILED _HRESULT_TYPEDEF_(0x80040201L) +#define EVENT_S_NOSUBSCRIBERS _HRESULT_TYPEDEF_(0x00040202L) +#define EVENT_E_QUERYSYNTAX _HRESULT_TYPEDEF_(0x80040203L) +#define EVENT_E_QUERYFIELD _HRESULT_TYPEDEF_(0x80040204L) +#define EVENT_E_INTERNALEXCEPTION _HRESULT_TYPEDEF_(0x80040205L) +#define EVENT_E_INTERNALERROR _HRESULT_TYPEDEF_(0x80040206L) +#define EVENT_E_INVALID_PER_USER_SID _HRESULT_TYPEDEF_(0x80040207L) +#define EVENT_E_USER_EXCEPTION _HRESULT_TYPEDEF_(0x80040208L) +#define EVENT_E_TOO_MANY_METHODS _HRESULT_TYPEDEF_(0x80040209L) +#define EVENT_E_MISSING_EVENTCLASS _HRESULT_TYPEDEF_(0x8004020AL) +#define EVENT_E_NOT_ALL_REMOVED _HRESULT_TYPEDEF_(0x8004020BL) +#define EVENT_E_COMPLUS_NOT_INSTALLED _HRESULT_TYPEDEF_(0x8004020CL) +#define EVENT_E_CANT_MODIFY_OR_DELETE_UNCONFIGURED_OBJECT _HRESULT_TYPEDEF_(0x8004020DL) +#define EVENT_E_CANT_MODIFY_OR_DELETE_CONFIGURED_OBJECT _HRESULT_TYPEDEF_(0x8004020EL) +#define EVENT_E_INVALID_EVENT_CLASS_PARTITION _HRESULT_TYPEDEF_(0x8004020FL) +#define EVENT_E_PER_USER_SID_NOT_LOGGED_ON _HRESULT_TYPEDEF_(0x80040210L) +#define XACT_E_FIRST 0x8004D000 +#define XACT_E_LAST 0x8004D029 +#define XACT_S_FIRST 0x0004D000 +#define XACT_S_LAST 0x0004D010 +#define XACT_E_ALREADYOTHERSINGLEPHASE _HRESULT_TYPEDEF_(0x8004D000L) +#define XACT_E_CANTRETAIN _HRESULT_TYPEDEF_(0x8004D001L) +#define XACT_E_COMMITFAILED _HRESULT_TYPEDEF_(0x8004D002L) +#define XACT_E_COMMITPREVENTED _HRESULT_TYPEDEF_(0x8004D003L) +#define XACT_E_HEURISTICABORT _HRESULT_TYPEDEF_(0x8004D004L) +#define XACT_E_HEURISTICCOMMIT _HRESULT_TYPEDEF_(0x8004D005L) +#define XACT_E_HEURISTICDAMAGE _HRESULT_TYPEDEF_(0x8004D006L) +#define XACT_E_HEURISTICDANGER _HRESULT_TYPEDEF_(0x8004D007L) +#define XACT_E_ISOLATIONLEVEL _HRESULT_TYPEDEF_(0x8004D008L) +#define XACT_E_NOASYNC _HRESULT_TYPEDEF_(0x8004D009L) +#define XACT_E_NOENLIST _HRESULT_TYPEDEF_(0x8004D00AL) +#define XACT_E_NOISORETAIN _HRESULT_TYPEDEF_(0x8004D00BL) +#define XACT_E_NORESOURCE _HRESULT_TYPEDEF_(0x8004D00CL) +#define XACT_E_NOTCURRENT _HRESULT_TYPEDEF_(0x8004D00DL) +#define XACT_E_NOTRANSACTION _HRESULT_TYPEDEF_(0x8004D00EL) +#define XACT_E_NOTSUPPORTED _HRESULT_TYPEDEF_(0x8004D00FL) +#define XACT_E_UNKNOWNRMGRID _HRESULT_TYPEDEF_(0x8004D010L) +#define XACT_E_WRONGSTATE _HRESULT_TYPEDEF_(0x8004D011L) +#define XACT_E_WRONGUOW _HRESULT_TYPEDEF_(0x8004D012L) +#define XACT_E_XTIONEXISTS _HRESULT_TYPEDEF_(0x8004D013L) +#define XACT_E_NOIMPORTOBJECT _HRESULT_TYPEDEF_(0x8004D014L) +#define XACT_E_INVALIDCOOKIE _HRESULT_TYPEDEF_(0x8004D015L) +#define XACT_E_INDOUBT _HRESULT_TYPEDEF_(0x8004D016L) +#define XACT_E_NOTIMEOUT _HRESULT_TYPEDEF_(0x8004D017L) +#define XACT_E_ALREADYINPROGRESS _HRESULT_TYPEDEF_(0x8004D018L) +#define XACT_E_ABORTED _HRESULT_TYPEDEF_(0x8004D019L) +#define XACT_E_LOGFULL _HRESULT_TYPEDEF_(0x8004D01AL) +#define XACT_E_TMNOTAVAILABLE _HRESULT_TYPEDEF_(0x8004D01BL) +#define XACT_E_CONNECTION_DOWN _HRESULT_TYPEDEF_(0x8004D01CL) +#define XACT_E_CONNECTION_DENIED _HRESULT_TYPEDEF_(0x8004D01DL) +#define XACT_E_REENLISTTIMEOUT _HRESULT_TYPEDEF_(0x8004D01EL) +#define XACT_E_TIP_CONNECT_FAILED _HRESULT_TYPEDEF_(0x8004D01FL) +#define XACT_E_TIP_PROTOCOL_ERROR _HRESULT_TYPEDEF_(0x8004D020L) +#define XACT_E_TIP_PULL_FAILED _HRESULT_TYPEDEF_(0x8004D021L) +#define XACT_E_DEST_TMNOTAVAILABLE _HRESULT_TYPEDEF_(0x8004D022L) +#define XACT_E_TIP_DISABLED _HRESULT_TYPEDEF_(0x8004D023L) +#define XACT_E_NETWORK_TX_DISABLED _HRESULT_TYPEDEF_(0x8004D024L) +#define XACT_E_PARTNER_NETWORK_TX_DISABLED _HRESULT_TYPEDEF_(0x8004D025L) +#define XACT_E_XA_TX_DISABLED _HRESULT_TYPEDEF_(0x8004D026L) +#define XACT_E_UNABLE_TO_READ_DTC_CONFIG _HRESULT_TYPEDEF_(0x8004D027L) +#define XACT_E_UNABLE_TO_LOAD_DTC_PROXY _HRESULT_TYPEDEF_(0x8004D028L) +#define XACT_E_ABORTING _HRESULT_TYPEDEF_(0x8004D029L) +#define XACT_E_CLERKNOTFOUND _HRESULT_TYPEDEF_(0x8004D080L) +#define XACT_E_CLERKEXISTS _HRESULT_TYPEDEF_(0x8004D081L) +#define XACT_E_RECOVERYINPROGRESS _HRESULT_TYPEDEF_(0x8004D082L) +#define XACT_E_TRANSACTIONCLOSED _HRESULT_TYPEDEF_(0x8004D083L) +#define XACT_E_INVALIDLSN _HRESULT_TYPEDEF_(0x8004D084L) +#define XACT_E_REPLAYREQUEST _HRESULT_TYPEDEF_(0x8004D085L) +#define XACT_S_ASYNC _HRESULT_TYPEDEF_(0x0004D000L) +#define XACT_S_DEFECT _HRESULT_TYPEDEF_(0x0004D001L) +#define XACT_S_READONLY _HRESULT_TYPEDEF_(0x0004D002L) +#define XACT_S_SOMENORETAIN _HRESULT_TYPEDEF_(0x0004D003L) +#define XACT_S_OKINFORM _HRESULT_TYPEDEF_(0x0004D004L) +#define XACT_S_MADECHANGESCONTENT _HRESULT_TYPEDEF_(0x0004D005L) +#define XACT_S_MADECHANGESINFORM _HRESULT_TYPEDEF_(0x0004D006L) +#define XACT_S_ALLNORETAIN _HRESULT_TYPEDEF_(0x0004D007L) +#define XACT_S_ABORTING _HRESULT_TYPEDEF_(0x0004D008L) +#define XACT_S_SINGLEPHASE _HRESULT_TYPEDEF_(0x0004D009L) +#define XACT_S_LOCALLY_OK _HRESULT_TYPEDEF_(0x0004D00AL) +#define XACT_S_LASTRESOURCEMANAGER _HRESULT_TYPEDEF_(0x0004D010L) +#define CONTEXT_E_FIRST 0x8004E000L +#define CONTEXT_E_LAST 0x8004E02FL +#define CONTEXT_S_FIRST 0x0004E000L +#define CONTEXT_S_LAST 0x0004E02FL +#define CONTEXT_E_ABORTED _HRESULT_TYPEDEF_(0x8004E002L) +#define CONTEXT_E_ABORTING _HRESULT_TYPEDEF_(0x8004E003L) +#define CONTEXT_E_NOCONTEXT _HRESULT_TYPEDEF_(0x8004E004L) +#define CONTEXT_E_WOULD_DEADLOCK _HRESULT_TYPEDEF_(0x8004E005L) +#define CONTEXT_E_SYNCH_TIMEOUT _HRESULT_TYPEDEF_(0x8004E006L) +#define CONTEXT_E_OLDREF _HRESULT_TYPEDEF_(0x8004E007L) +#define CONTEXT_E_ROLENOTFOUND _HRESULT_TYPEDEF_(0x8004E00CL) +#define CONTEXT_E_TMNOTAVAILABLE _HRESULT_TYPEDEF_(0x8004E00FL) +#define CO_E_ACTIVATIONFAILED _HRESULT_TYPEDEF_(0x8004E021L) +#define CO_E_ACTIVATIONFAILED_EVENTLOGGED _HRESULT_TYPEDEF_(0x8004E022L) +#define CO_E_ACTIVATIONFAILED_CATALOGERROR _HRESULT_TYPEDEF_(0x8004E023L) +#define CO_E_ACTIVATIONFAILED_TIMEOUT _HRESULT_TYPEDEF_(0x8004E024L) +#define CO_E_INITIALIZATIONFAILED _HRESULT_TYPEDEF_(0x8004E025L) +#define CONTEXT_E_NOJIT _HRESULT_TYPEDEF_(0x8004E026L) +#define CONTEXT_E_NOTRANSACTION _HRESULT_TYPEDEF_(0x8004E027L) +#define CO_E_THREADINGMODEL_CHANGED _HRESULT_TYPEDEF_(0x8004E028L) +#define CO_E_NOIISINTRINSICS _HRESULT_TYPEDEF_(0x8004E029L) +#define CO_E_NOCOOKIES _HRESULT_TYPEDEF_(0x8004E02AL) +#define CO_E_DBERROR _HRESULT_TYPEDEF_(0x8004E02BL) +#define CO_E_NOTPOOLED _HRESULT_TYPEDEF_(0x8004E02CL) +#define CO_E_NOTCONSTRUCTED _HRESULT_TYPEDEF_(0x8004E02DL) +#define CO_E_NOSYNCHRONIZATION _HRESULT_TYPEDEF_(0x8004E02EL) +#define CO_E_ISOLEVELMISMATCH _HRESULT_TYPEDEF_(0x8004E02FL) +#define OLE_S_USEREG _HRESULT_TYPEDEF_(0x00040000L) +#define OLE_S_STATIC _HRESULT_TYPEDEF_(0x00040001L) +#define OLE_S_MAC_CLIPFORMAT _HRESULT_TYPEDEF_(0x00040002L) +#define DRAGDROP_S_DROP _HRESULT_TYPEDEF_(0x00040100L) +#define DRAGDROP_S_CANCEL _HRESULT_TYPEDEF_(0x00040101L) +#define DRAGDROP_S_USEDEFAULTCURSORS _HRESULT_TYPEDEF_(0x00040102L) +#define DATA_S_SAMEFORMATETC _HRESULT_TYPEDEF_(0x00040130L) +#define VIEW_S_ALREADY_FROZEN _HRESULT_TYPEDEF_(0x00040140L) +#define CACHE_S_FORMATETC_NOTSUPPORTED _HRESULT_TYPEDEF_(0x00040170L) +#define CACHE_S_SAMECACHE _HRESULT_TYPEDEF_(0x00040171L) +#define CACHE_S_SOMECACHES_NOTUPDATED _HRESULT_TYPEDEF_(0x00040172L) +#define OLEOBJ_S_INVALIDVERB _HRESULT_TYPEDEF_(0x00040180L) +#define OLEOBJ_S_CANNOT_DOVERB_NOW _HRESULT_TYPEDEF_(0x00040181L) +#define OLEOBJ_S_INVALIDHWND _HRESULT_TYPEDEF_(0x00040182L) +#define INPLACE_S_TRUNCATED _HRESULT_TYPEDEF_(0x000401A0L) +#define CONVERT10_S_NO_PRESENTATION _HRESULT_TYPEDEF_(0x000401C0L) +#define MK_S_REDUCED_TO_SELF _HRESULT_TYPEDEF_(0x000401E2L) +#define MK_S_ME _HRESULT_TYPEDEF_(0x000401E4L) +#define MK_S_HIM _HRESULT_TYPEDEF_(0x000401E5L) +#define MK_S_US _HRESULT_TYPEDEF_(0x000401E6L) +#define MK_S_MONIKERALREADYREGISTERED _HRESULT_TYPEDEF_(0x000401E7L) +#define SCHED_S_TASK_READY _HRESULT_TYPEDEF_(0x00041300L) +#define SCHED_S_TASK_RUNNING _HRESULT_TYPEDEF_(0x00041301L) +#define SCHED_S_TASK_DISABLED _HRESULT_TYPEDEF_(0x00041302L) +#define SCHED_S_TASK_HAS_NOT_RUN _HRESULT_TYPEDEF_(0x00041303L) +#define SCHED_S_TASK_NO_MORE_RUNS _HRESULT_TYPEDEF_(0x00041304L) +#define SCHED_S_TASK_NOT_SCHEDULED _HRESULT_TYPEDEF_(0x00041305L) +#define SCHED_S_TASK_TERMINATED _HRESULT_TYPEDEF_(0x00041306L) +#define SCHED_S_TASK_NO_VALID_TRIGGERS _HRESULT_TYPEDEF_(0x00041307L) +#define SCHED_S_EVENT_TRIGGER _HRESULT_TYPEDEF_(0x00041308L) +#define SCHED_E_TRIGGER_NOT_FOUND _HRESULT_TYPEDEF_(0x80041309L) +#define SCHED_E_TASK_NOT_READY _HRESULT_TYPEDEF_(0x8004130AL) +#define SCHED_E_TASK_NOT_RUNNING _HRESULT_TYPEDEF_(0x8004130BL) +#define SCHED_E_SERVICE_NOT_INSTALLED _HRESULT_TYPEDEF_(0x8004130CL) +#define SCHED_E_CANNOT_OPEN_TASK _HRESULT_TYPEDEF_(0x8004130DL) +#define SCHED_E_INVALID_TASK _HRESULT_TYPEDEF_(0x8004130EL) +#define SCHED_E_ACCOUNT_INFORMATION_NOT_SET _HRESULT_TYPEDEF_(0x8004130FL) +#define SCHED_E_ACCOUNT_NAME_NOT_FOUND _HRESULT_TYPEDEF_(0x80041310L) +#define SCHED_E_ACCOUNT_DBASE_CORRUPT _HRESULT_TYPEDEF_(0x80041311L) +#define SCHED_E_NO_SECURITY_SERVICES _HRESULT_TYPEDEF_(0x80041312L) +#define SCHED_E_UNKNOWN_OBJECT_VERSION _HRESULT_TYPEDEF_(0x80041313L) +#define SCHED_E_UNSUPPORTED_ACCOUNT_OPTION _HRESULT_TYPEDEF_(0x80041314L) +#define SCHED_E_SERVICE_NOT_RUNNING _HRESULT_TYPEDEF_(0x80041315L) +#define CO_E_CLASS_CREATE_FAILED _HRESULT_TYPEDEF_(0x80080001L) +#define CO_E_SCM_ERROR _HRESULT_TYPEDEF_(0x80080002L) +#define CO_E_SCM_RPC_FAILURE _HRESULT_TYPEDEF_(0x80080003L) +#define CO_E_BAD_PATH _HRESULT_TYPEDEF_(0x80080004L) +#define CO_E_SERVER_EXEC_FAILURE _HRESULT_TYPEDEF_(0x80080005L) +#define CO_E_OBJSRV_RPC_FAILURE _HRESULT_TYPEDEF_(0x80080006L) +#define MK_E_NO_NORMALIZED _HRESULT_TYPEDEF_(0x80080007L) +#define CO_E_SERVER_STOPPING _HRESULT_TYPEDEF_(0x80080008L) +#define MEM_E_INVALID_ROOT _HRESULT_TYPEDEF_(0x80080009L) +#define MEM_E_INVALID_LINK _HRESULT_TYPEDEF_(0x80080010L) +#define MEM_E_INVALID_SIZE _HRESULT_TYPEDEF_(0x80080011L) +#define CO_S_NOTALLINTERFACES _HRESULT_TYPEDEF_(0x00080012L) +#define CO_S_MACHINENAMENOTFOUND _HRESULT_TYPEDEF_(0x00080013L) +#define DISP_E_UNKNOWNINTERFACE _HRESULT_TYPEDEF_(0x80020001L) +#define DISP_E_MEMBERNOTFOUND _HRESULT_TYPEDEF_(0x80020003L) +#define DISP_E_PARAMNOTFOUND _HRESULT_TYPEDEF_(0x80020004L) +#define DISP_E_TYPEMISMATCH _HRESULT_TYPEDEF_(0x80020005L) +#define DISP_E_UNKNOWNNAME _HRESULT_TYPEDEF_(0x80020006L) +#define DISP_E_NONAMEDARGS _HRESULT_TYPEDEF_(0x80020007L) +#define DISP_E_BADVARTYPE _HRESULT_TYPEDEF_(0x80020008L) +#define DISP_E_EXCEPTION _HRESULT_TYPEDEF_(0x80020009L) +#define DISP_E_OVERFLOW _HRESULT_TYPEDEF_(0x8002000AL) +#define DISP_E_BADINDEX _HRESULT_TYPEDEF_(0x8002000BL) +#define DISP_E_UNKNOWNLCID _HRESULT_TYPEDEF_(0x8002000CL) +#define DISP_E_ARRAYISLOCKED _HRESULT_TYPEDEF_(0x8002000DL) +#define DISP_E_BADPARAMCOUNT _HRESULT_TYPEDEF_(0x8002000EL) +#define DISP_E_PARAMNOTOPTIONAL _HRESULT_TYPEDEF_(0x8002000FL) +#define DISP_E_BADCALLEE _HRESULT_TYPEDEF_(0x80020010L) +#define DISP_E_NOTACOLLECTION _HRESULT_TYPEDEF_(0x80020011L) +#define DISP_E_DIVBYZERO _HRESULT_TYPEDEF_(0x80020012L) +#define DISP_E_BUFFERTOOSMALL _HRESULT_TYPEDEF_(0x80020013L) +#define TYPE_E_BUFFERTOOSMALL _HRESULT_TYPEDEF_(0x80028016L) +#define TYPE_E_FIELDNOTFOUND _HRESULT_TYPEDEF_(0x80028017L) +#define TYPE_E_INVDATAREAD _HRESULT_TYPEDEF_(0x80028018L) +#define TYPE_E_UNSUPFORMAT _HRESULT_TYPEDEF_(0x80028019L) +#define TYPE_E_REGISTRYACCESS _HRESULT_TYPEDEF_(0x8002801CL) +#define TYPE_E_LIBNOTREGISTERED _HRESULT_TYPEDEF_(0x8002801DL) +#define TYPE_E_UNDEFINEDTYPE _HRESULT_TYPEDEF_(0x80028027L) +#define TYPE_E_QUALIFIEDNAMEDISALLOWED _HRESULT_TYPEDEF_(0x80028028L) +#define TYPE_E_INVALIDSTATE _HRESULT_TYPEDEF_(0x80028029L) +#define TYPE_E_WRONGTYPEKIND _HRESULT_TYPEDEF_(0x8002802AL) +#define TYPE_E_ELEMENTNOTFOUND _HRESULT_TYPEDEF_(0x8002802BL) +#define TYPE_E_AMBIGUOUSNAME _HRESULT_TYPEDEF_(0x8002802CL) +#define TYPE_E_NAMECONFLICT _HRESULT_TYPEDEF_(0x8002802DL) +#define TYPE_E_UNKNOWNLCID _HRESULT_TYPEDEF_(0x8002802EL) +#define TYPE_E_DLLFUNCTIONNOTFOUND _HRESULT_TYPEDEF_(0x8002802FL) +#define TYPE_E_BADMODULEKIND _HRESULT_TYPEDEF_(0x800288BDL) +#define TYPE_E_SIZETOOBIG _HRESULT_TYPEDEF_(0x800288C5L) +#define TYPE_E_DUPLICATEID _HRESULT_TYPEDEF_(0x800288C6L) +#define TYPE_E_INVALIDID _HRESULT_TYPEDEF_(0x800288CFL) +#define TYPE_E_TYPEMISMATCH _HRESULT_TYPEDEF_(0x80028CA0L) +#define TYPE_E_OUTOFBOUNDS _HRESULT_TYPEDEF_(0x80028CA1L) +#define TYPE_E_IOERROR _HRESULT_TYPEDEF_(0x80028CA2L) +#define TYPE_E_CANTCREATETMPFILE _HRESULT_TYPEDEF_(0x80028CA3L) +#define TYPE_E_CANTLOADLIBRARY _HRESULT_TYPEDEF_(0x80029C4AL) +#define TYPE_E_INCONSISTENTPROPFUNCS _HRESULT_TYPEDEF_(0x80029C83L) +#define TYPE_E_CIRCULARTYPE _HRESULT_TYPEDEF_(0x80029C84L) +#define STG_E_INVALIDFUNCTION _HRESULT_TYPEDEF_(0x80030001L) +#define STG_E_FILENOTFOUND _HRESULT_TYPEDEF_(0x80030002L) +#define STG_E_PATHNOTFOUND _HRESULT_TYPEDEF_(0x80030003L) +#define STG_E_TOOMANYOPENFILES _HRESULT_TYPEDEF_(0x80030004L) +#define STG_E_ACCESSDENIED _HRESULT_TYPEDEF_(0x80030005L) +#define STG_E_INVALIDHANDLE _HRESULT_TYPEDEF_(0x80030006L) +#define STG_E_INSUFFICIENTMEMORY _HRESULT_TYPEDEF_(0x80030008L) +#define STG_E_INVALIDPOINTER _HRESULT_TYPEDEF_(0x80030009L) +#define STG_E_NOMOREFILES _HRESULT_TYPEDEF_(0x80030012L) +#define STG_E_DISKISWRITEPROTECTED _HRESULT_TYPEDEF_(0x80030013L) +#define STG_E_SEEKERROR _HRESULT_TYPEDEF_(0x80030019L) +#define STG_E_WRITEFAULT _HRESULT_TYPEDEF_(0x8003001DL) +#define STG_E_READFAULT _HRESULT_TYPEDEF_(0x8003001EL) +#define STG_E_SHAREVIOLATION _HRESULT_TYPEDEF_(0x80030020L) +#define STG_E_LOCKVIOLATION _HRESULT_TYPEDEF_(0x80030021L) +#define STG_E_FILEALREADYEXISTS _HRESULT_TYPEDEF_(0x80030050L) +#define STG_E_INVALIDPARAMETER _HRESULT_TYPEDEF_(0x80030057L) +#define STG_E_MEDIUMFULL _HRESULT_TYPEDEF_(0x80030070L) +#define STG_E_PROPSETMISMATCHED _HRESULT_TYPEDEF_(0x800300F0L) +#define STG_E_ABNORMALAPIEXIT _HRESULT_TYPEDEF_(0x800300FAL) +#define STG_E_INVALIDHEADER _HRESULT_TYPEDEF_(0x800300FBL) +#define STG_E_INVALIDNAME _HRESULT_TYPEDEF_(0x800300FCL) +#define STG_E_UNKNOWN _HRESULT_TYPEDEF_(0x800300FDL) +#define STG_E_UNIMPLEMENTEDFUNCTION _HRESULT_TYPEDEF_(0x800300FEL) +#define STG_E_INVALIDFLAG _HRESULT_TYPEDEF_(0x800300FFL) +#define STG_E_INUSE _HRESULT_TYPEDEF_(0x80030100L) +#define STG_E_NOTCURRENT _HRESULT_TYPEDEF_(0x80030101L) +#define STG_E_REVERTED _HRESULT_TYPEDEF_(0x80030102L) +#define STG_E_CANTSAVE _HRESULT_TYPEDEF_(0x80030103L) +#define STG_E_OLDFORMAT _HRESULT_TYPEDEF_(0x80030104L) +#define STG_E_OLDDLL _HRESULT_TYPEDEF_(0x80030105L) +#define STG_E_SHAREREQUIRED _HRESULT_TYPEDEF_(0x80030106L) +#define STG_E_NOTFILEBASEDSTORAGE _HRESULT_TYPEDEF_(0x80030107L) +#define STG_E_EXTANTMARSHALLINGS _HRESULT_TYPEDEF_(0x80030108L) +#define STG_E_DOCFILECORRUPT _HRESULT_TYPEDEF_(0x80030109L) +#define STG_E_BADBASEADDRESS _HRESULT_TYPEDEF_(0x80030110L) +#define STG_E_DOCFILETOOLARGE _HRESULT_TYPEDEF_(0x80030111L) +#define STG_E_NOTSIMPLEFORMAT _HRESULT_TYPEDEF_(0x80030112L) +#define STG_E_INCOMPLETE _HRESULT_TYPEDEF_(0x80030201L) +#define STG_E_TERMINATED _HRESULT_TYPEDEF_(0x80030202L) +#define STG_S_CONVERTED _HRESULT_TYPEDEF_(0x00030200L) +#define STG_S_BLOCK _HRESULT_TYPEDEF_(0x00030201L) +#define STG_S_RETRYNOW _HRESULT_TYPEDEF_(0x00030202L) +#define STG_S_MONITORING _HRESULT_TYPEDEF_(0x00030203L) +#define STG_S_MULTIPLEOPENS _HRESULT_TYPEDEF_(0x00030204L) +#define STG_S_CONSOLIDATIONFAILED _HRESULT_TYPEDEF_(0x00030205L) +#define STG_S_CANNOTCONSOLIDATE _HRESULT_TYPEDEF_(0x00030206L) +#define STG_E_STATUS_COPY_PROTECTION_FAILURE _HRESULT_TYPEDEF_(0x80030305L) +#define STG_E_CSS_AUTHENTICATION_FAILURE _HRESULT_TYPEDEF_(0x80030306L) +#define STG_E_CSS_KEY_NOT_PRESENT _HRESULT_TYPEDEF_(0x80030307L) +#define STG_E_CSS_KEY_NOT_ESTABLISHED _HRESULT_TYPEDEF_(0x80030308L) +#define STG_E_CSS_SCRAMBLED_SECTOR _HRESULT_TYPEDEF_(0x80030309L) +#define STG_E_CSS_REGION_MISMATCH _HRESULT_TYPEDEF_(0x8003030AL) +#define STG_E_RESETS_EXHAUSTED _HRESULT_TYPEDEF_(0x8003030BL) +#define RPC_E_CALL_REJECTED _HRESULT_TYPEDEF_(0x80010001L) +#define RPC_E_CALL_CANCELED _HRESULT_TYPEDEF_(0x80010002L) +#define RPC_E_CANTPOST_INSENDCALL _HRESULT_TYPEDEF_(0x80010003L) +#define RPC_E_CANTCALLOUT_INASYNCCALL _HRESULT_TYPEDEF_(0x80010004L) +#define RPC_E_CANTCALLOUT_INEXTERNALCALL _HRESULT_TYPEDEF_(0x80010005L) +#define RPC_E_CONNECTION_TERMINATED _HRESULT_TYPEDEF_(0x80010006L) +#define RPC_E_SERVER_DIED _HRESULT_TYPEDEF_(0x80010007L) +#define RPC_E_CLIENT_DIED _HRESULT_TYPEDEF_(0x80010008L) +#define RPC_E_INVALID_DATAPACKET _HRESULT_TYPEDEF_(0x80010009L) +#define RPC_E_CANTTRANSMIT_CALL _HRESULT_TYPEDEF_(0x8001000AL) +#define RPC_E_CLIENT_CANTMARSHAL_DATA _HRESULT_TYPEDEF_(0x8001000BL) +#define RPC_E_CLIENT_CANTUNMARSHAL_DATA _HRESULT_TYPEDEF_(0x8001000CL) +#define RPC_E_SERVER_CANTMARSHAL_DATA _HRESULT_TYPEDEF_(0x8001000DL) +#define RPC_E_SERVER_CANTUNMARSHAL_DATA _HRESULT_TYPEDEF_(0x8001000EL) +#define RPC_E_INVALID_DATA _HRESULT_TYPEDEF_(0x8001000FL) +#define RPC_E_INVALID_PARAMETER _HRESULT_TYPEDEF_(0x80010010L) +#define RPC_E_CANTCALLOUT_AGAIN _HRESULT_TYPEDEF_(0x80010011L) +#define RPC_E_SERVER_DIED_DNE _HRESULT_TYPEDEF_(0x80010012L) +#define RPC_E_SYS_CALL_FAILED _HRESULT_TYPEDEF_(0x80010100L) +#define RPC_E_OUT_OF_RESOURCES _HRESULT_TYPEDEF_(0x80010101L) +#define RPC_E_ATTEMPTED_MULTITHREAD _HRESULT_TYPEDEF_(0x80010102L) +#define RPC_E_NOT_REGISTERED _HRESULT_TYPEDEF_(0x80010103L) +#define RPC_E_FAULT _HRESULT_TYPEDEF_(0x80010104L) +#define RPC_E_SERVERFAULT _HRESULT_TYPEDEF_(0x80010105L) +#define RPC_E_CHANGED_MODE _HRESULT_TYPEDEF_(0x80010106L) +#define RPC_E_INVALIDMETHOD _HRESULT_TYPEDEF_(0x80010107L) +#define RPC_E_DISCONNECTED _HRESULT_TYPEDEF_(0x80010108L) +#define RPC_E_RETRY _HRESULT_TYPEDEF_(0x80010109L) +#define RPC_E_SERVERCALL_RETRYLATER _HRESULT_TYPEDEF_(0x8001010AL) +#define RPC_E_SERVERCALL_REJECTED _HRESULT_TYPEDEF_(0x8001010BL) +#define RPC_E_INVALID_CALLDATA _HRESULT_TYPEDEF_(0x8001010CL) +#define RPC_E_CANTCALLOUT_ININPUTSYNCCALL _HRESULT_TYPEDEF_(0x8001010DL) +#define RPC_E_WRONG_THREAD _HRESULT_TYPEDEF_(0x8001010EL) +#define RPC_E_THREAD_NOT_INIT _HRESULT_TYPEDEF_(0x8001010FL) +#define RPC_E_VERSION_MISMATCH _HRESULT_TYPEDEF_(0x80010110L) +#define RPC_E_INVALID_HEADER _HRESULT_TYPEDEF_(0x80010111L) +#define RPC_E_INVALID_EXTENSION _HRESULT_TYPEDEF_(0x80010112L) +#define RPC_E_INVALID_IPID _HRESULT_TYPEDEF_(0x80010113L) +#define RPC_E_INVALID_OBJECT _HRESULT_TYPEDEF_(0x80010114L) +#define RPC_S_CALLPENDING _HRESULT_TYPEDEF_(0x80010115L) +#define RPC_S_WAITONTIMER _HRESULT_TYPEDEF_(0x80010116L) +#define RPC_E_CALL_COMPLETE _HRESULT_TYPEDEF_(0x80010117L) +#define RPC_E_UNSECURE_CALL _HRESULT_TYPEDEF_(0x80010118L) +#define RPC_E_TOO_LATE _HRESULT_TYPEDEF_(0x80010119L) +#define RPC_E_NO_GOOD_SECURITY_PACKAGES _HRESULT_TYPEDEF_(0x8001011AL) +#define RPC_E_ACCESS_DENIED _HRESULT_TYPEDEF_(0x8001011BL) +#define RPC_E_REMOTE_DISABLED _HRESULT_TYPEDEF_(0x8001011CL) +#define RPC_E_INVALID_OBJREF _HRESULT_TYPEDEF_(0x8001011DL) +#define RPC_E_NO_CONTEXT _HRESULT_TYPEDEF_(0x8001011EL) +#define RPC_E_TIMEOUT _HRESULT_TYPEDEF_(0x8001011FL) +#define RPC_E_NO_SYNC _HRESULT_TYPEDEF_(0x80010120L) +#define RPC_E_FULLSIC_REQUIRED _HRESULT_TYPEDEF_(0x80010121L) +#define RPC_E_INVALID_STD_NAME _HRESULT_TYPEDEF_(0x80010122L) +#define CO_E_FAILEDTOIMPERSONATE _HRESULT_TYPEDEF_(0x80010123L) +#define CO_E_FAILEDTOGETSECCTX _HRESULT_TYPEDEF_(0x80010124L) +#define CO_E_FAILEDTOOPENTHREADTOKEN _HRESULT_TYPEDEF_(0x80010125L) +#define CO_E_FAILEDTOGETTOKENINFO _HRESULT_TYPEDEF_(0x80010126L) +#define CO_E_TRUSTEEDOESNTMATCHCLIENT _HRESULT_TYPEDEF_(0x80010127L) +#define CO_E_FAILEDTOQUERYCLIENTBLANKET _HRESULT_TYPEDEF_(0x80010128L) +#define CO_E_FAILEDTOSETDACL _HRESULT_TYPEDEF_(0x80010129L) +#define CO_E_ACCESSCHECKFAILED _HRESULT_TYPEDEF_(0x8001012AL) +#define CO_E_NETACCESSAPIFAILED _HRESULT_TYPEDEF_(0x8001012BL) +#define CO_E_WRONGTRUSTEENAMESYNTAX _HRESULT_TYPEDEF_(0x8001012CL) +#define CO_E_INVALIDSID _HRESULT_TYPEDEF_(0x8001012DL) +#define CO_E_CONVERSIONFAILED _HRESULT_TYPEDEF_(0x8001012EL) +#define CO_E_NOMATCHINGSIDFOUND _HRESULT_TYPEDEF_(0x8001012FL) +#define CO_E_LOOKUPACCSIDFAILED _HRESULT_TYPEDEF_(0x80010130L) +#define CO_E_NOMATCHINGNAMEFOUND _HRESULT_TYPEDEF_(0x80010131L) +#define CO_E_LOOKUPACCNAMEFAILED _HRESULT_TYPEDEF_(0x80010132L) +#define CO_E_SETSERLHNDLFAILED _HRESULT_TYPEDEF_(0x80010133L) +#define CO_E_FAILEDTOGETWINDIR _HRESULT_TYPEDEF_(0x80010134L) +#define CO_E_PATHTOOLONG _HRESULT_TYPEDEF_(0x80010135L) +#define CO_E_FAILEDTOGENUUID _HRESULT_TYPEDEF_(0x80010136L) +#define CO_E_FAILEDTOCREATEFILE _HRESULT_TYPEDEF_(0x80010137L) +#define CO_E_FAILEDTOCLOSEHANDLE _HRESULT_TYPEDEF_(0x80010138L) +#define CO_E_EXCEEDSYSACLLIMIT _HRESULT_TYPEDEF_(0x80010139L) +#define CO_E_ACESINWRONGORDER _HRESULT_TYPEDEF_(0x8001013AL) +#define CO_E_INCOMPATIBLESTREAMVERSION _HRESULT_TYPEDEF_(0x8001013BL) +#define CO_E_FAILEDTOOPENPROCESSTOKEN _HRESULT_TYPEDEF_(0x8001013CL) +#define CO_E_DECODEFAILED _HRESULT_TYPEDEF_(0x8001013DL) +#define CO_E_ACNOTINITIALIZED _HRESULT_TYPEDEF_(0x8001013FL) +#define CO_E_CANCEL_DISABLED _HRESULT_TYPEDEF_(0x80010140L) +#define RPC_E_UNEXPECTED _HRESULT_TYPEDEF_(0x8001FFFFL) +#define ERROR_AUDITING_DISABLED _HRESULT_TYPEDEF_(0xC0090001L) +#define ERROR_ALL_SIDS_FILTERED _HRESULT_TYPEDEF_(0xC0090002L) +#define NTE_BAD_UID _HRESULT_TYPEDEF_(0x80090001L) +#define NTE_BAD_HASH _HRESULT_TYPEDEF_(0x80090002L) +#define NTE_BAD_KEY _HRESULT_TYPEDEF_(0x80090003L) +#define NTE_BAD_LEN _HRESULT_TYPEDEF_(0x80090004L) +#define NTE_BAD_DATA _HRESULT_TYPEDEF_(0x80090005L) +#define NTE_BAD_SIGNATURE _HRESULT_TYPEDEF_(0x80090006L) +#define NTE_BAD_VER _HRESULT_TYPEDEF_(0x80090007L) +#define NTE_BAD_ALGID _HRESULT_TYPEDEF_(0x80090008L) +#define NTE_BAD_FLAGS _HRESULT_TYPEDEF_(0x80090009L) +#define NTE_BAD_TYPE _HRESULT_TYPEDEF_(0x8009000AL) +#define NTE_BAD_KEY_STATE _HRESULT_TYPEDEF_(0x8009000BL) +#define NTE_BAD_HASH_STATE _HRESULT_TYPEDEF_(0x8009000CL) +#define NTE_NO_KEY _HRESULT_TYPEDEF_(0x8009000DL) +#define NTE_NO_MEMORY _HRESULT_TYPEDEF_(0x8009000EL) +#define NTE_EXISTS _HRESULT_TYPEDEF_(0x8009000FL) +#define NTE_PERM _HRESULT_TYPEDEF_(0x80090010L) +#define NTE_NOT_FOUND _HRESULT_TYPEDEF_(0x80090011L) +#define NTE_DOUBLE_ENCRYPT _HRESULT_TYPEDEF_(0x80090012L) +#define NTE_BAD_PROVIDER _HRESULT_TYPEDEF_(0x80090013L) +#define NTE_BAD_PROV_TYPE _HRESULT_TYPEDEF_(0x80090014L) +#define NTE_BAD_PUBLIC_KEY _HRESULT_TYPEDEF_(0x80090015L) +#define NTE_BAD_KEYSET _HRESULT_TYPEDEF_(0x80090016L) +#define NTE_PROV_TYPE_NOT_DEF _HRESULT_TYPEDEF_(0x80090017L) +#define NTE_PROV_TYPE_ENTRY_BAD _HRESULT_TYPEDEF_(0x80090018L) +#define NTE_KEYSET_NOT_DEF _HRESULT_TYPEDEF_(0x80090019L) +#define NTE_KEYSET_ENTRY_BAD _HRESULT_TYPEDEF_(0x8009001AL) +#define NTE_PROV_TYPE_NO_MATCH _HRESULT_TYPEDEF_(0x8009001BL) +#define NTE_SIGNATURE_FILE_BAD _HRESULT_TYPEDEF_(0x8009001CL) +#define NTE_PROVIDER_DLL_FAIL _HRESULT_TYPEDEF_(0x8009001DL) +#define NTE_PROV_DLL_NOT_FOUND _HRESULT_TYPEDEF_(0x8009001EL) +#define NTE_BAD_KEYSET_PARAM _HRESULT_TYPEDEF_(0x8009001FL) +#define NTE_FAIL _HRESULT_TYPEDEF_(0x80090020L) +#define NTE_SYS_ERR _HRESULT_TYPEDEF_(0x80090021L) +#define NTE_SILENT_CONTEXT _HRESULT_TYPEDEF_(0x80090022L) +#define NTE_TOKEN_KEYSET_STORAGE_FULL _HRESULT_TYPEDEF_(0x80090023L) +#define NTE_TEMPORARY_PROFILE _HRESULT_TYPEDEF_(0x80090024L) +#define NTE_FIXEDPARAMETER _HRESULT_TYPEDEF_(0x80090025L) +#define SEC_E_INSUFFICIENT_MEMORY _HRESULT_TYPEDEF_(0x80090300L) +#define SEC_E_INVALID_HANDLE _HRESULT_TYPEDEF_(0x80090301L) +#define SEC_E_UNSUPPORTED_FUNCTION _HRESULT_TYPEDEF_(0x80090302L) +#define SEC_E_TARGET_UNKNOWN _HRESULT_TYPEDEF_(0x80090303L) +#define SEC_E_INTERNAL_ERROR _HRESULT_TYPEDEF_(0x80090304L) +#define SEC_E_SECPKG_NOT_FOUND _HRESULT_TYPEDEF_(0x80090305L) +#define SEC_E_NOT_OWNER _HRESULT_TYPEDEF_(0x80090306L) +#define SEC_E_CANNOT_INSTALL _HRESULT_TYPEDEF_(0x80090307L) +#define SEC_E_INVALID_TOKEN _HRESULT_TYPEDEF_(0x80090308L) +#define SEC_E_CANNOT_PACK _HRESULT_TYPEDEF_(0x80090309L) +#define SEC_E_QOP_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x8009030AL) +#define SEC_E_NO_IMPERSONATION _HRESULT_TYPEDEF_(0x8009030BL) +#define SEC_E_LOGON_DENIED _HRESULT_TYPEDEF_(0x8009030CL) +#define SEC_E_UNKNOWN_CREDENTIALS _HRESULT_TYPEDEF_(0x8009030DL) +#define SEC_E_NO_CREDENTIALS _HRESULT_TYPEDEF_(0x8009030EL) +#define SEC_E_MESSAGE_ALTERED _HRESULT_TYPEDEF_(0x8009030FL) +#define SEC_E_OUT_OF_SEQUENCE _HRESULT_TYPEDEF_(0x80090310L) +#define SEC_E_NO_AUTHENTICATING_AUTHORITY _HRESULT_TYPEDEF_(0x80090311L) +#define SEC_I_CONTINUE_NEEDED _HRESULT_TYPEDEF_(0x00090312L) +#define SEC_I_COMPLETE_NEEDED _HRESULT_TYPEDEF_(0x00090313L) +#define SEC_I_COMPLETE_AND_CONTINUE _HRESULT_TYPEDEF_(0x00090314L) +#define SEC_I_LOCAL_LOGON _HRESULT_TYPEDEF_(0x00090315L) +#define SEC_E_BAD_PKGID _HRESULT_TYPEDEF_(0x80090316L) +#define SEC_E_CONTEXT_EXPIRED _HRESULT_TYPEDEF_(0x80090317L) +#define SEC_I_CONTEXT_EXPIRED _HRESULT_TYPEDEF_(0x00090317L) +#define SEC_E_INCOMPLETE_MESSAGE _HRESULT_TYPEDEF_(0x80090318L) +#define SEC_E_INCOMPLETE_CREDENTIALS _HRESULT_TYPEDEF_(0x80090320L) +#define SEC_E_BUFFER_TOO_SMALL _HRESULT_TYPEDEF_(0x80090321L) +#define SEC_I_INCOMPLETE_CREDENTIALS _HRESULT_TYPEDEF_(0x00090320L) +#define SEC_I_RENEGOTIATE _HRESULT_TYPEDEF_(0x00090321L) +#define SEC_E_WRONG_PRINCIPAL _HRESULT_TYPEDEF_(0x80090322L) +#define SEC_I_NO_LSA_CONTEXT _HRESULT_TYPEDEF_(0x00090323L) +#define SEC_E_TIME_SKEW _HRESULT_TYPEDEF_(0x80090324L) +#define SEC_E_UNTRUSTED_ROOT _HRESULT_TYPEDEF_(0x80090325L) +#define SEC_E_ILLEGAL_MESSAGE _HRESULT_TYPEDEF_(0x80090326L) +#define SEC_E_CERT_UNKNOWN _HRESULT_TYPEDEF_(0x80090327L) +#define SEC_E_CERT_EXPIRED _HRESULT_TYPEDEF_(0x80090328L) +#define SEC_E_ENCRYPT_FAILURE _HRESULT_TYPEDEF_(0x80090329L) +#define SEC_E_DECRYPT_FAILURE _HRESULT_TYPEDEF_(0x80090330L) +#define SEC_E_ALGORITHM_MISMATCH _HRESULT_TYPEDEF_(0x80090331L) +#define SEC_E_SECURITY_QOS_FAILED _HRESULT_TYPEDEF_(0x80090332L) +#define SEC_E_UNFINISHED_CONTEXT_DELETED _HRESULT_TYPEDEF_(0x80090333L) +#define SEC_E_NO_TGT_REPLY _HRESULT_TYPEDEF_(0x80090334L) +#define SEC_E_NO_IP_ADDRESSES _HRESULT_TYPEDEF_(0x80090335L) +#define SEC_E_WRONG_CREDENTIAL_HANDLE _HRESULT_TYPEDEF_(0x80090336L) +#define SEC_E_CRYPTO_SYSTEM_INVALID _HRESULT_TYPEDEF_(0x80090337L) +#define SEC_E_MAX_REFERRALS_EXCEEDED _HRESULT_TYPEDEF_(0x80090338L) +#define SEC_E_MUST_BE_KDC _HRESULT_TYPEDEF_(0x80090339L) +#define SEC_E_STRONG_CRYPTO_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x8009033AL) +#define SEC_E_TOO_MANY_PRINCIPALS _HRESULT_TYPEDEF_(0x8009033BL) +#define SEC_E_NO_PA_DATA _HRESULT_TYPEDEF_(0x8009033CL) +#define SEC_E_PKINIT_NAME_MISMATCH _HRESULT_TYPEDEF_(0x8009033DL) +#define SEC_E_SMARTCARD_LOGON_REQUIRED _HRESULT_TYPEDEF_(0x8009033EL) +#define SEC_E_SHUTDOWN_IN_PROGRESS _HRESULT_TYPEDEF_(0x8009033FL) +#define SEC_E_KDC_INVALID_REQUEST _HRESULT_TYPEDEF_(0x80090340L) +#define SEC_E_KDC_UNABLE_TO_REFER _HRESULT_TYPEDEF_(0x80090341L) +#define SEC_E_KDC_UNKNOWN_ETYPE _HRESULT_TYPEDEF_(0x80090342L) +#define SEC_E_UNSUPPORTED_PREAUTH _HRESULT_TYPEDEF_(0x80090343L) +#define SEC_E_DELEGATION_REQUIRED _HRESULT_TYPEDEF_(0x80090345L) +#define SEC_E_BAD_BINDINGS _HRESULT_TYPEDEF_(0x80090346L) +#define SEC_E_MULTIPLE_ACCOUNTS _HRESULT_TYPEDEF_(0x80090347L) +#define SEC_E_NO_KERB_KEY _HRESULT_TYPEDEF_(0x80090348L) +#define SEC_E_CERT_WRONG_USAGE _HRESULT_TYPEDEF_(0x80090349L) +#define SEC_E_DOWNGRADE_DETECTED _HRESULT_TYPEDEF_(0x80090350L) +#define SEC_E_SMARTCARD_CERT_REVOKED _HRESULT_TYPEDEF_(0x80090351L) +#define SEC_E_ISSUING_CA_UNTRUSTED _HRESULT_TYPEDEF_(0x80090352L) +#define SEC_E_REVOCATION_OFFLINE_C _HRESULT_TYPEDEF_(0x80090353L) +#define SEC_E_PKINIT_CLIENT_FAILURE _HRESULT_TYPEDEF_(0x80090354L) +#define SEC_E_SMARTCARD_CERT_EXPIRED _HRESULT_TYPEDEF_(0x80090355L) +#define SEC_E_NO_S4U_PROT_SUPPORT _HRESULT_TYPEDEF_(0x80090356L) +#define SEC_E_CROSSREALM_DELEGATION_FAILURE _HRESULT_TYPEDEF_(0x80090357L) +#define SEC_E_REVOCATION_OFFLINE_KDC _HRESULT_TYPEDEF_(0x80090358L) +#define SEC_E_ISSUING_CA_UNTRUSTED_KDC _HRESULT_TYPEDEF_(0x80090359L) +#define SEC_E_KDC_CERT_EXPIRED _HRESULT_TYPEDEF_(0x8009035AL) +#define SEC_E_KDC_CERT_REVOKED _HRESULT_TYPEDEF_(0x8009035BL) +#define SEC_E_NO_SPM SEC_E_INTERNAL_ERROR +#define SEC_E_NOT_SUPPORTED SEC_E_UNSUPPORTED_FUNCTION +#define CRYPT_E_MSG_ERROR _HRESULT_TYPEDEF_(0x80091001L) +#define CRYPT_E_UNKNOWN_ALGO _HRESULT_TYPEDEF_(0x80091002L) +#define CRYPT_E_OID_FORMAT _HRESULT_TYPEDEF_(0x80091003L) +#define CRYPT_E_INVALID_MSG_TYPE _HRESULT_TYPEDEF_(0x80091004L) +#define CRYPT_E_UNEXPECTED_ENCODING _HRESULT_TYPEDEF_(0x80091005L) +#define CRYPT_E_AUTH_ATTR_MISSING _HRESULT_TYPEDEF_(0x80091006L) +#define CRYPT_E_HASH_VALUE _HRESULT_TYPEDEF_(0x80091007L) +#define CRYPT_E_INVALID_INDEX _HRESULT_TYPEDEF_(0x80091008L) +#define CRYPT_E_ALREADY_DECRYPTED _HRESULT_TYPEDEF_(0x80091009L) +#define CRYPT_E_NOT_DECRYPTED _HRESULT_TYPEDEF_(0x8009100AL) +#define CRYPT_E_RECIPIENT_NOT_FOUND _HRESULT_TYPEDEF_(0x8009100BL) +#define CRYPT_E_CONTROL_TYPE _HRESULT_TYPEDEF_(0x8009100CL) +#define CRYPT_E_ISSUER_SERIALNUMBER _HRESULT_TYPEDEF_(0x8009100DL) +#define CRYPT_E_SIGNER_NOT_FOUND _HRESULT_TYPEDEF_(0x8009100EL) +#define CRYPT_E_ATTRIBUTES_MISSING _HRESULT_TYPEDEF_(0x8009100FL) +#define CRYPT_E_STREAM_MSG_NOT_READY _HRESULT_TYPEDEF_(0x80091010L) +#define CRYPT_E_STREAM_INSUFFICIENT_DATA _HRESULT_TYPEDEF_(0x80091011L) +#define CRYPT_I_NEW_PROTECTION_REQUIRED _HRESULT_TYPEDEF_(0x00091012L) +#define CRYPT_E_BAD_LEN _HRESULT_TYPEDEF_(0x80092001L) +#define CRYPT_E_BAD_ENCODE _HRESULT_TYPEDEF_(0x80092002L) +#define CRYPT_E_FILE_ERROR _HRESULT_TYPEDEF_(0x80092003L) +#define CRYPT_E_NOT_FOUND _HRESULT_TYPEDEF_(0x80092004L) +#define CRYPT_E_EXISTS _HRESULT_TYPEDEF_(0x80092005L) +#define CRYPT_E_NO_PROVIDER _HRESULT_TYPEDEF_(0x80092006L) +#define CRYPT_E_SELF_SIGNED _HRESULT_TYPEDEF_(0x80092007L) +#define CRYPT_E_DELETED_PREV _HRESULT_TYPEDEF_(0x80092008L) +#define CRYPT_E_NO_MATCH _HRESULT_TYPEDEF_(0x80092009L) +#define CRYPT_E_UNEXPECTED_MSG_TYPE _HRESULT_TYPEDEF_(0x8009200AL) +#define CRYPT_E_NO_KEY_PROPERTY _HRESULT_TYPEDEF_(0x8009200BL) +#define CRYPT_E_NO_DECRYPT_CERT _HRESULT_TYPEDEF_(0x8009200CL) +#define CRYPT_E_BAD_MSG _HRESULT_TYPEDEF_(0x8009200DL) +#define CRYPT_E_NO_SIGNER _HRESULT_TYPEDEF_(0x8009200EL) +#define CRYPT_E_PENDING_CLOSE _HRESULT_TYPEDEF_(0x8009200FL) +#define CRYPT_E_REVOKED _HRESULT_TYPEDEF_(0x80092010L) +#define CRYPT_E_NO_REVOCATION_DLL _HRESULT_TYPEDEF_(0x80092011L) +#define CRYPT_E_NO_REVOCATION_CHECK _HRESULT_TYPEDEF_(0x80092012L) +#define CRYPT_E_REVOCATION_OFFLINE _HRESULT_TYPEDEF_(0x80092013L) +#define CRYPT_E_NOT_IN_REVOCATION_DATABASE _HRESULT_TYPEDEF_(0x80092014L) +#define CRYPT_E_INVALID_NUMERIC_STRING _HRESULT_TYPEDEF_(0x80092020L) +#define CRYPT_E_INVALID_PRINTABLE_STRING _HRESULT_TYPEDEF_(0x80092021L) +#define CRYPT_E_INVALID_IA5_STRING _HRESULT_TYPEDEF_(0x80092022L) +#define CRYPT_E_INVALID_X500_STRING _HRESULT_TYPEDEF_(0x80092023L) +#define CRYPT_E_NOT_CHAR_STRING _HRESULT_TYPEDEF_(0x80092024L) +#define CRYPT_E_FILERESIZED _HRESULT_TYPEDEF_(0x80092025L) +#define CRYPT_E_SECURITY_SETTINGS _HRESULT_TYPEDEF_(0x80092026L) +#define CRYPT_E_NO_VERIFY_USAGE_DLL _HRESULT_TYPEDEF_(0x80092027L) +#define CRYPT_E_NO_VERIFY_USAGE_CHECK _HRESULT_TYPEDEF_(0x80092028L) +#define CRYPT_E_VERIFY_USAGE_OFFLINE _HRESULT_TYPEDEF_(0x80092029L) +#define CRYPT_E_NOT_IN_CTL _HRESULT_TYPEDEF_(0x8009202AL) +#define CRYPT_E_NO_TRUSTED_SIGNER _HRESULT_TYPEDEF_(0x8009202BL) +#define CRYPT_E_MISSING_PUBKEY_PARA _HRESULT_TYPEDEF_(0x8009202CL) +#define CRYPT_E_OSS_ERROR _HRESULT_TYPEDEF_(0x80093000L) +#define OSS_MORE_BUF _HRESULT_TYPEDEF_(0x80093001L) +#define OSS_NEGATIVE_UINTEGER _HRESULT_TYPEDEF_(0x80093002L) +#define OSS_PDU_RANGE _HRESULT_TYPEDEF_(0x80093003L) +#define OSS_MORE_INPUT _HRESULT_TYPEDEF_(0x80093004L) +#define OSS_DATA_ERROR _HRESULT_TYPEDEF_(0x80093005L) +#define OSS_BAD_ARG _HRESULT_TYPEDEF_(0x80093006L) +#define OSS_BAD_VERSION _HRESULT_TYPEDEF_(0x80093007L) +#define OSS_OUT_MEMORY _HRESULT_TYPEDEF_(0x80093008L) +#define OSS_PDU_MISMATCH _HRESULT_TYPEDEF_(0x80093009L) +#define OSS_LIMITED _HRESULT_TYPEDEF_(0x8009300AL) +#define OSS_BAD_PTR _HRESULT_TYPEDEF_(0x8009300BL) +#define OSS_BAD_TIME _HRESULT_TYPEDEF_(0x8009300CL) +#define OSS_INDEFINITE_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x8009300DL) +#define OSS_MEM_ERROR _HRESULT_TYPEDEF_(0x8009300EL) +#define OSS_BAD_TABLE _HRESULT_TYPEDEF_(0x8009300FL) +#define OSS_TOO_LONG _HRESULT_TYPEDEF_(0x80093010L) +#define OSS_CONSTRAINT_VIOLATED _HRESULT_TYPEDEF_(0x80093011L) +#define OSS_FATAL_ERROR _HRESULT_TYPEDEF_(0x80093012L) +#define OSS_ACCESS_SERIALIZATION_ERROR _HRESULT_TYPEDEF_(0x80093013L) +#define OSS_NULL_TBL _HRESULT_TYPEDEF_(0x80093014L) +#define OSS_NULL_FCN _HRESULT_TYPEDEF_(0x80093015L) +#define OSS_BAD_ENCRULES _HRESULT_TYPEDEF_(0x80093016L) +#define OSS_UNAVAIL_ENCRULES _HRESULT_TYPEDEF_(0x80093017L) +#define OSS_CANT_OPEN_TRACE_WINDOW _HRESULT_TYPEDEF_(0x80093018L) +#define OSS_UNIMPLEMENTED _HRESULT_TYPEDEF_(0x80093019L) +#define OSS_OID_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x8009301AL) +#define OSS_CANT_OPEN_TRACE_FILE _HRESULT_TYPEDEF_(0x8009301BL) +#define OSS_TRACE_FILE_ALREADY_OPEN _HRESULT_TYPEDEF_(0x8009301CL) +#define OSS_TABLE_MISMATCH _HRESULT_TYPEDEF_(0x8009301DL) +#define OSS_TYPE_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x8009301EL) +#define OSS_REAL_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x8009301FL) +#define OSS_REAL_CODE_NOT_LINKED _HRESULT_TYPEDEF_(0x80093020L) +#define OSS_OUT_OF_RANGE _HRESULT_TYPEDEF_(0x80093021L) +#define OSS_COPIER_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x80093022L) +#define OSS_CONSTRAINT_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x80093023L) +#define OSS_COMPARATOR_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x80093024L) +#define OSS_COMPARATOR_CODE_NOT_LINKED _HRESULT_TYPEDEF_(0x80093025L) +#define OSS_MEM_MGR_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x80093026L) +#define OSS_PDV_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x80093027L) +#define OSS_PDV_CODE_NOT_LINKED _HRESULT_TYPEDEF_(0x80093028L) +#define OSS_API_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x80093029L) +#define OSS_BERDER_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x8009302AL) +#define OSS_PER_DLL_NOT_LINKED _HRESULT_TYPEDEF_(0x8009302BL) +#define OSS_OPEN_TYPE_ERROR _HRESULT_TYPEDEF_(0x8009302CL) +#define OSS_MUTEX_NOT_CREATED _HRESULT_TYPEDEF_(0x8009302DL) +#define OSS_CANT_CLOSE_TRACE_FILE _HRESULT_TYPEDEF_(0x8009302EL) +#define CRYPT_E_ASN1_ERROR _HRESULT_TYPEDEF_(0x80093100L) +#define CRYPT_E_ASN1_INTERNAL _HRESULT_TYPEDEF_(0x80093101L) +#define CRYPT_E_ASN1_EOD _HRESULT_TYPEDEF_(0x80093102L) +#define CRYPT_E_ASN1_CORRUPT _HRESULT_TYPEDEF_(0x80093103L) +#define CRYPT_E_ASN1_LARGE _HRESULT_TYPEDEF_(0x80093104L) +#define CRYPT_E_ASN1_CONSTRAINT _HRESULT_TYPEDEF_(0x80093105L) +#define CRYPT_E_ASN1_MEMORY _HRESULT_TYPEDEF_(0x80093106L) +#define CRYPT_E_ASN1_OVERFLOW _HRESULT_TYPEDEF_(0x80093107L) +#define CRYPT_E_ASN1_BADPDU _HRESULT_TYPEDEF_(0x80093108L) +#define CRYPT_E_ASN1_BADARGS _HRESULT_TYPEDEF_(0x80093109L) +#define CRYPT_E_ASN1_BADREAL _HRESULT_TYPEDEF_(0x8009310AL) +#define CRYPT_E_ASN1_BADTAG _HRESULT_TYPEDEF_(0x8009310BL) +#define CRYPT_E_ASN1_CHOICE _HRESULT_TYPEDEF_(0x8009310CL) +#define CRYPT_E_ASN1_RULE _HRESULT_TYPEDEF_(0x8009310DL) +#define CRYPT_E_ASN1_UTF8 _HRESULT_TYPEDEF_(0x8009310EL) +#define CRYPT_E_ASN1_PDU_TYPE _HRESULT_TYPEDEF_(0x80093133L) +#define CRYPT_E_ASN1_NYI _HRESULT_TYPEDEF_(0x80093134L) +#define CRYPT_E_ASN1_EXTENDED _HRESULT_TYPEDEF_(0x80093201L) +#define CRYPT_E_ASN1_NOEOD _HRESULT_TYPEDEF_(0x80093202L) +#define CERTSRV_E_BAD_REQUESTSUBJECT _HRESULT_TYPEDEF_(0x80094001L) +#define CERTSRV_E_NO_REQUEST _HRESULT_TYPEDEF_(0x80094002L) +#define CERTSRV_E_BAD_REQUESTSTATUS _HRESULT_TYPEDEF_(0x80094003L) +#define CERTSRV_E_PROPERTY_EMPTY _HRESULT_TYPEDEF_(0x80094004L) +#define CERTSRV_E_INVALID_CA_CERTIFICATE _HRESULT_TYPEDEF_(0x80094005L) +#define CERTSRV_E_SERVER_SUSPENDED _HRESULT_TYPEDEF_(0x80094006L) +#define CERTSRV_E_ENCODING_LENGTH _HRESULT_TYPEDEF_(0x80094007L) +#define CERTSRV_E_ROLECONFLICT _HRESULT_TYPEDEF_(0x80094008L) +#define CERTSRV_E_RESTRICTEDOFFICER _HRESULT_TYPEDEF_(0x80094009L) +#define CERTSRV_E_KEY_ARCHIVAL_NOT_CONFIGURED _HRESULT_TYPEDEF_(0x8009400AL) +#define CERTSRV_E_NO_VALID_KRA _HRESULT_TYPEDEF_(0x8009400BL) +#define CERTSRV_E_BAD_REQUEST_KEY_ARCHIVAL _HRESULT_TYPEDEF_(0x8009400CL) +#define CERTSRV_E_NO_CAADMIN_DEFINED _HRESULT_TYPEDEF_(0x8009400DL) +#define CERTSRV_E_BAD_RENEWAL_CERT_ATTRIBUTE _HRESULT_TYPEDEF_(0x8009400EL) +#define CERTSRV_E_NO_DB_SESSIONS _HRESULT_TYPEDEF_(0x8009400FL) +#define CERTSRV_E_ALIGNMENT_FAULT _HRESULT_TYPEDEF_(0x80094010L) +#define CERTSRV_E_ENROLL_DENIED _HRESULT_TYPEDEF_(0x80094011L) +#define CERTSRV_E_TEMPLATE_DENIED _HRESULT_TYPEDEF_(0x80094012L) +#define CERTSRV_E_DOWNLEVEL_DC_SSL_OR_UPGRADE _HRESULT_TYPEDEF_(0x80094013L) +#define CERTSRV_E_UNSUPPORTED_CERT_TYPE _HRESULT_TYPEDEF_(0x80094800L) +#define CERTSRV_E_NO_CERT_TYPE _HRESULT_TYPEDEF_(0x80094801L) +#define CERTSRV_E_TEMPLATE_CONFLICT _HRESULT_TYPEDEF_(0x80094802L) +#define CERTSRV_E_SUBJECT_ALT_NAME_REQUIRED _HRESULT_TYPEDEF_(0x80094803L) +#define CERTSRV_E_ARCHIVED_KEY_REQUIRED _HRESULT_TYPEDEF_(0x80094804L) +#define CERTSRV_E_SMIME_REQUIRED _HRESULT_TYPEDEF_(0x80094805L) +#define CERTSRV_E_BAD_RENEWAL_SUBJECT _HRESULT_TYPEDEF_(0x80094806L) +#define CERTSRV_E_BAD_TEMPLATE_VERSION _HRESULT_TYPEDEF_(0x80094807L) +#define CERTSRV_E_TEMPLATE_POLICY_REQUIRED _HRESULT_TYPEDEF_(0x80094808L) +#define CERTSRV_E_SIGNATURE_POLICY_REQUIRED _HRESULT_TYPEDEF_(0x80094809L) +#define CERTSRV_E_SIGNATURE_COUNT _HRESULT_TYPEDEF_(0x8009480AL) +#define CERTSRV_E_SIGNATURE_REJECTED _HRESULT_TYPEDEF_(0x8009480BL) +#define CERTSRV_E_ISSUANCE_POLICY_REQUIRED _HRESULT_TYPEDEF_(0x8009480CL) +#define CERTSRV_E_SUBJECT_UPN_REQUIRED _HRESULT_TYPEDEF_(0x8009480DL) +#define CERTSRV_E_SUBJECT_DIRECTORY_GUID_REQUIRED _HRESULT_TYPEDEF_(0x8009480EL) +#define CERTSRV_E_SUBJECT_DNS_REQUIRED _HRESULT_TYPEDEF_(0x8009480FL) +#define CERTSRV_E_ARCHIVED_KEY_UNEXPECTED _HRESULT_TYPEDEF_(0x80094810L) +#define CERTSRV_E_KEY_LENGTH _HRESULT_TYPEDEF_(0x80094811L) +#define CERTSRV_E_SUBJECT_EMAIL_REQUIRED _HRESULT_TYPEDEF_(0x80094812L) +#define CERTSRV_E_UNKNOWN_CERT_TYPE _HRESULT_TYPEDEF_(0x80094813L) +#define CERTSRV_E_CERT_TYPE_OVERLAP _HRESULT_TYPEDEF_(0x80094814L) +#define XENROLL_E_KEY_NOT_EXPORTABLE _HRESULT_TYPEDEF_(0x80095000L) +#define XENROLL_E_CANNOT_ADD_ROOT_CERT _HRESULT_TYPEDEF_(0x80095001L) +#define XENROLL_E_RESPONSE_KA_HASH_NOT_FOUND _HRESULT_TYPEDEF_(0x80095002L) +#define XENROLL_E_RESPONSE_UNEXPECTED_KA_HASH _HRESULT_TYPEDEF_(0x80095003L) +#define XENROLL_E_RESPONSE_KA_HASH_MISMATCH _HRESULT_TYPEDEF_(0x80095004L) +#define XENROLL_E_KEYSPEC_SMIME_MISMATCH _HRESULT_TYPEDEF_(0x80095005L) +#define TRUST_E_SYSTEM_ERROR _HRESULT_TYPEDEF_(0x80096001L) +#define TRUST_E_NO_SIGNER_CERT _HRESULT_TYPEDEF_(0x80096002L) +#define TRUST_E_COUNTER_SIGNER _HRESULT_TYPEDEF_(0x80096003L) +#define TRUST_E_CERT_SIGNATURE _HRESULT_TYPEDEF_(0x80096004L) +#define TRUST_E_TIME_STAMP _HRESULT_TYPEDEF_(0x80096005L) +#define TRUST_E_BAD_DIGEST _HRESULT_TYPEDEF_(0x80096010L) +#define TRUST_E_BASIC_CONSTRAINTS _HRESULT_TYPEDEF_(0x80096019L) +#define TRUST_E_FINANCIAL_CRITERIA _HRESULT_TYPEDEF_(0x8009601EL) +#define MSSIPOTF_E_OUTOFMEMRANGE _HRESULT_TYPEDEF_(0x80097001L) +#define MSSIPOTF_E_CANTGETOBJECT _HRESULT_TYPEDEF_(0x80097002L) +#define MSSIPOTF_E_NOHEADTABLE _HRESULT_TYPEDEF_(0x80097003L) +#define MSSIPOTF_E_BAD_MAGICNUMBER _HRESULT_TYPEDEF_(0x80097004L) +#define MSSIPOTF_E_BAD_OFFSET_TABLE _HRESULT_TYPEDEF_(0x80097005L) +#define MSSIPOTF_E_TABLE_TAGORDER _HRESULT_TYPEDEF_(0x80097006L) +#define MSSIPOTF_E_TABLE_LONGWORD _HRESULT_TYPEDEF_(0x80097007L) +#define MSSIPOTF_E_BAD_FIRST_TABLE_PLACEMENT _HRESULT_TYPEDEF_(0x80097008L) +#define MSSIPOTF_E_TABLES_OVERLAP _HRESULT_TYPEDEF_(0x80097009L) +#define MSSIPOTF_E_TABLE_PADBYTES _HRESULT_TYPEDEF_(0x8009700AL) +#define MSSIPOTF_E_FILETOOSMALL _HRESULT_TYPEDEF_(0x8009700BL) +#define MSSIPOTF_E_TABLE_CHECKSUM _HRESULT_TYPEDEF_(0x8009700CL) +#define MSSIPOTF_E_FILE_CHECKSUM _HRESULT_TYPEDEF_(0x8009700DL) +#define MSSIPOTF_E_FAILED_POLICY _HRESULT_TYPEDEF_(0x80097010L) +#define MSSIPOTF_E_FAILED_HINTS_CHECK _HRESULT_TYPEDEF_(0x80097011L) +#define MSSIPOTF_E_NOT_OPENTYPE _HRESULT_TYPEDEF_(0x80097012L) +#define MSSIPOTF_E_FILE _HRESULT_TYPEDEF_(0x80097013L) +#define MSSIPOTF_E_CRYPT _HRESULT_TYPEDEF_(0x80097014L) +#define MSSIPOTF_E_BADVERSION _HRESULT_TYPEDEF_(0x80097015L) +#define MSSIPOTF_E_DSIG_STRUCTURE _HRESULT_TYPEDEF_(0x80097016L) +#define MSSIPOTF_E_PCONST_CHECK _HRESULT_TYPEDEF_(0x80097017L) +#define MSSIPOTF_E_STRUCTURE _HRESULT_TYPEDEF_(0x80097018L) +#define NTE_OP_OK 0 +#define TRUST_E_PROVIDER_UNKNOWN _HRESULT_TYPEDEF_(0x800B0001L) +#define TRUST_E_ACTION_UNKNOWN _HRESULT_TYPEDEF_(0x800B0002L) +#define TRUST_E_SUBJECT_FORM_UNKNOWN _HRESULT_TYPEDEF_(0x800B0003L) +#define TRUST_E_SUBJECT_NOT_TRUSTED _HRESULT_TYPEDEF_(0x800B0004L) +#define DIGSIG_E_ENCODE _HRESULT_TYPEDEF_(0x800B0005L) +#define DIGSIG_E_DECODE _HRESULT_TYPEDEF_(0x800B0006L) +#define DIGSIG_E_EXTENSIBILITY _HRESULT_TYPEDEF_(0x800B0007L) +#define DIGSIG_E_CRYPTO _HRESULT_TYPEDEF_(0x800B0008L) +#define PERSIST_E_SIZEDEFINITE _HRESULT_TYPEDEF_(0x800B0009L) +#define PERSIST_E_SIZEINDEFINITE _HRESULT_TYPEDEF_(0x800B000AL) +#define PERSIST_E_NOTSELFSIZING _HRESULT_TYPEDEF_(0x800B000BL) +#define TRUST_E_NOSIGNATURE _HRESULT_TYPEDEF_(0x800B0100L) +#define CERT_E_EXPIRED _HRESULT_TYPEDEF_(0x800B0101L) +#define CERT_E_VALIDITYPERIODNESTING _HRESULT_TYPEDEF_(0x800B0102L) +#define CERT_E_ROLE _HRESULT_TYPEDEF_(0x800B0103L) +#define CERT_E_PATHLENCONST _HRESULT_TYPEDEF_(0x800B0104L) +#define CERT_E_CRITICAL _HRESULT_TYPEDEF_(0x800B0105L) +#define CERT_E_PURPOSE _HRESULT_TYPEDEF_(0x800B0106L) +#define CERT_E_ISSUERCHAINING _HRESULT_TYPEDEF_(0x800B0107L) +#define CERT_E_MALFORMED _HRESULT_TYPEDEF_(0x800B0108L) +#define CERT_E_UNTRUSTEDROOT _HRESULT_TYPEDEF_(0x800B0109L) +#define CERT_E_CHAINING _HRESULT_TYPEDEF_(0x800B010AL) +#define TRUST_E_FAIL _HRESULT_TYPEDEF_(0x800B010BL) +#define CERT_E_REVOKED _HRESULT_TYPEDEF_(0x800B010CL) +#define CERT_E_UNTRUSTEDTESTROOT _HRESULT_TYPEDEF_(0x800B010DL) +#define CERT_E_REVOCATION_FAILURE _HRESULT_TYPEDEF_(0x800B010EL) +#define CERT_E_CN_NO_MATCH _HRESULT_TYPEDEF_(0x800B010FL) +#define CERT_E_WRONG_USAGE _HRESULT_TYPEDEF_(0x800B0110L) +#define TRUST_E_EXPLICIT_DISTRUST _HRESULT_TYPEDEF_(0x800B0111L) +#define CERT_E_UNTRUSTEDCA _HRESULT_TYPEDEF_(0x800B0112L) +#define CERT_E_INVALID_POLICY _HRESULT_TYPEDEF_(0x800B0113L) +#define CERT_E_INVALID_NAME _HRESULT_TYPEDEF_(0x800B0114L) +#define HRESULT_FROM_SETUPAPI(x) ((((x) & (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR))==(APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR)) ? ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_SETUPAPI << 16) | 0x80000000)) : HRESULT_FROM_WIN32(x)) +#define SPAPI_E_EXPECTED_SECTION_NAME _HRESULT_TYPEDEF_(0x800F0000L) +#define SPAPI_E_BAD_SECTION_NAME_LINE _HRESULT_TYPEDEF_(0x800F0001L) +#define SPAPI_E_SECTION_NAME_TOO_LONG _HRESULT_TYPEDEF_(0x800F0002L) +#define SPAPI_E_GENERAL_SYNTAX _HRESULT_TYPEDEF_(0x800F0003L) +#define SPAPI_E_WRONG_INF_STYLE _HRESULT_TYPEDEF_(0x800F0100L) +#define SPAPI_E_SECTION_NOT_FOUND _HRESULT_TYPEDEF_(0x800F0101L) +#define SPAPI_E_LINE_NOT_FOUND _HRESULT_TYPEDEF_(0x800F0102L) +#define SPAPI_E_NO_BACKUP _HRESULT_TYPEDEF_(0x800F0103L) +#define SPAPI_E_NO_ASSOCIATED_CLASS _HRESULT_TYPEDEF_(0x800F0200L) +#define SPAPI_E_CLASS_MISMATCH _HRESULT_TYPEDEF_(0x800F0201L) +#define SPAPI_E_DUPLICATE_FOUND _HRESULT_TYPEDEF_(0x800F0202L) +#define SPAPI_E_NO_DRIVER_SELECTED _HRESULT_TYPEDEF_(0x800F0203L) +#define SPAPI_E_KEY_DOES_NOT_EXIST _HRESULT_TYPEDEF_(0x800F0204L) +#define SPAPI_E_INVALID_DEVINST_NAME _HRESULT_TYPEDEF_(0x800F0205L) +#define SPAPI_E_INVALID_CLASS _HRESULT_TYPEDEF_(0x800F0206L) +#define SPAPI_E_DEVINST_ALREADY_EXISTS _HRESULT_TYPEDEF_(0x800F0207L) +#define SPAPI_E_DEVINFO_NOT_REGISTERED _HRESULT_TYPEDEF_(0x800F0208L) +#define SPAPI_E_INVALID_REG_PROPERTY _HRESULT_TYPEDEF_(0x800F0209L) +#define SPAPI_E_NO_INF _HRESULT_TYPEDEF_(0x800F020AL) +#define SPAPI_E_NO_SUCH_DEVINST _HRESULT_TYPEDEF_(0x800F020BL) +#define SPAPI_E_CANT_LOAD_CLASS_ICON _HRESULT_TYPEDEF_(0x800F020CL) +#define SPAPI_E_INVALID_CLASS_INSTALLER _HRESULT_TYPEDEF_(0x800F020DL) +#define SPAPI_E_DI_DO_DEFAULT _HRESULT_TYPEDEF_(0x800F020EL) +#define SPAPI_E_DI_NOFILECOPY _HRESULT_TYPEDEF_(0x800F020FL) +#define SPAPI_E_INVALID_HWPROFILE _HRESULT_TYPEDEF_(0x800F0210L) +#define SPAPI_E_NO_DEVICE_SELECTED _HRESULT_TYPEDEF_(0x800F0211L) +#define SPAPI_E_DEVINFO_LIST_LOCKED _HRESULT_TYPEDEF_(0x800F0212L) +#define SPAPI_E_DEVINFO_DATA_LOCKED _HRESULT_TYPEDEF_(0x800F0213L) +#define SPAPI_E_DI_BAD_PATH _HRESULT_TYPEDEF_(0x800F0214L) +#define SPAPI_E_NO_CLASSINSTALL_PARAMS _HRESULT_TYPEDEF_(0x800F0215L) +#define SPAPI_E_FILEQUEUE_LOCKED _HRESULT_TYPEDEF_(0x800F0216L) +#define SPAPI_E_BAD_SERVICE_INSTALLSECT _HRESULT_TYPEDEF_(0x800F0217L) +#define SPAPI_E_NO_CLASS_DRIVER_LIST _HRESULT_TYPEDEF_(0x800F0218L) +#define SPAPI_E_NO_ASSOCIATED_SERVICE _HRESULT_TYPEDEF_(0x800F0219L) +#define SPAPI_E_NO_DEFAULT_DEVICE_INTERFACE _HRESULT_TYPEDEF_(0x800F021AL) +#define SPAPI_E_DEVICE_INTERFACE_ACTIVE _HRESULT_TYPEDEF_(0x800F021BL) +#define SPAPI_E_DEVICE_INTERFACE_REMOVED _HRESULT_TYPEDEF_(0x800F021CL) +#define SPAPI_E_BAD_INTERFACE_INSTALLSECT _HRESULT_TYPEDEF_(0x800F021DL) +#define SPAPI_E_NO_SUCH_INTERFACE_CLASS _HRESULT_TYPEDEF_(0x800F021EL) +#define SPAPI_E_INVALID_REFERENCE_STRING _HRESULT_TYPEDEF_(0x800F021FL) +#define SPAPI_E_INVALID_MACHINENAME _HRESULT_TYPEDEF_(0x800F0220L) +#define SPAPI_E_REMOTE_COMM_FAILURE _HRESULT_TYPEDEF_(0x800F0221L) +#define SPAPI_E_MACHINE_UNAVAILABLE _HRESULT_TYPEDEF_(0x800F0222L) +#define SPAPI_E_NO_CONFIGMGR_SERVICES _HRESULT_TYPEDEF_(0x800F0223L) +#define SPAPI_E_INVALID_PROPPAGE_PROVIDER _HRESULT_TYPEDEF_(0x800F0224L) +#define SPAPI_E_NO_SUCH_DEVICE_INTERFACE _HRESULT_TYPEDEF_(0x800F0225L) +#define SPAPI_E_DI_POSTPROCESSING_REQUIRED _HRESULT_TYPEDEF_(0x800F0226L) +#define SPAPI_E_INVALID_COINSTALLER _HRESULT_TYPEDEF_(0x800F0227L) +#define SPAPI_E_NO_COMPAT_DRIVERS _HRESULT_TYPEDEF_(0x800F0228L) +#define SPAPI_E_NO_DEVICE_ICON _HRESULT_TYPEDEF_(0x800F0229L) +#define SPAPI_E_INVALID_INF_LOGCONFIG _HRESULT_TYPEDEF_(0x800F022AL) +#define SPAPI_E_DI_DONT_INSTALL _HRESULT_TYPEDEF_(0x800F022BL) +#define SPAPI_E_INVALID_FILTER_DRIVER _HRESULT_TYPEDEF_(0x800F022CL) +#define SPAPI_E_NON_WINDOWS_NT_DRIVER _HRESULT_TYPEDEF_(0x800F022DL) +#define SPAPI_E_NON_WINDOWS_DRIVER _HRESULT_TYPEDEF_(0x800F022EL) +#define SPAPI_E_NO_CATALOG_FOR_OEM_INF _HRESULT_TYPEDEF_(0x800F022FL) +#define SPAPI_E_DEVINSTALL_QUEUE_NONNATIVE _HRESULT_TYPEDEF_(0x800F0230L) +#define SPAPI_E_NOT_DISABLEABLE _HRESULT_TYPEDEF_(0x800F0231L) +#define SPAPI_E_CANT_REMOVE_DEVINST _HRESULT_TYPEDEF_(0x800F0232L) +#define SPAPI_E_INVALID_TARGET _HRESULT_TYPEDEF_(0x800F0233L) +#define SPAPI_E_DRIVER_NONNATIVE _HRESULT_TYPEDEF_(0x800F0234L) +#define SPAPI_E_IN_WOW64 _HRESULT_TYPEDEF_(0x800F0235L) +#define SPAPI_E_SET_SYSTEM_RESTORE_POINT _HRESULT_TYPEDEF_(0x800F0236L) +#define SPAPI_E_INCORRECTLY_COPIED_INF _HRESULT_TYPEDEF_(0x800F0237L) +#define SPAPI_E_SCE_DISABLED _HRESULT_TYPEDEF_(0x800F0238L) +#define SPAPI_E_UNKNOWN_EXCEPTION _HRESULT_TYPEDEF_(0x800F0239L) +#define SPAPI_E_PNP_REGISTRY_ERROR _HRESULT_TYPEDEF_(0x800F023AL) +#define SPAPI_E_REMOTE_REQUEST_UNSUPPORTED _HRESULT_TYPEDEF_(0x800F023BL) +#define SPAPI_E_NOT_AN_INSTALLED_OEM_INF _HRESULT_TYPEDEF_(0x800F023CL) +#define SPAPI_E_INF_IN_USE_BY_DEVICES _HRESULT_TYPEDEF_(0x800F023DL) +#define SPAPI_E_DI_FUNCTION_OBSOLETE _HRESULT_TYPEDEF_(0x800F023EL) +#define SPAPI_E_NO_AUTHENTICODE_CATALOG _HRESULT_TYPEDEF_(0x800F023FL) +#define SPAPI_E_AUTHENTICODE_DISALLOWED _HRESULT_TYPEDEF_(0x800F0240L) +#define SPAPI_E_AUTHENTICODE_TRUSTED_PUBLISHER _HRESULT_TYPEDEF_(0x800F0241L) +#define SPAPI_E_AUTHENTICODE_TRUST_NOT_ESTABLISHED _HRESULT_TYPEDEF_(0x800F0242L) +#define SPAPI_E_AUTHENTICODE_PUBLISHER_NOT_TRUSTED _HRESULT_TYPEDEF_(0x800F0243L) +#define SPAPI_E_SIGNATURE_OSATTRIBUTE_MISMATCH _HRESULT_TYPEDEF_(0x800F0244L) +#define SPAPI_E_ONLY_VALIDATE_VIA_AUTHENTICODE _HRESULT_TYPEDEF_(0x800F0245L) +#define SPAPI_E_UNRECOVERABLE_STACK_OVERFLOW _HRESULT_TYPEDEF_(0x800F0300L) +#define SPAPI_E_ERROR_NOT_INSTALLED _HRESULT_TYPEDEF_(0x800F1000L) +#define SCARD_S_SUCCESS NO_ERROR +#define SCARD_F_INTERNAL_ERROR _HRESULT_TYPEDEF_(0x80100001L) +#define SCARD_E_CANCELLED _HRESULT_TYPEDEF_(0x80100002L) +#define SCARD_E_INVALID_HANDLE _HRESULT_TYPEDEF_(0x80100003L) +#define SCARD_E_INVALID_PARAMETER _HRESULT_TYPEDEF_(0x80100004L) +#define SCARD_E_INVALID_TARGET _HRESULT_TYPEDEF_(0x80100005L) +#define SCARD_E_NO_MEMORY _HRESULT_TYPEDEF_(0x80100006L) +#define SCARD_F_WAITED_TOO_LONG _HRESULT_TYPEDEF_(0x80100007L) +#define SCARD_E_INSUFFICIENT_BUFFER _HRESULT_TYPEDEF_(0x80100008L) +#define SCARD_E_UNKNOWN_READER _HRESULT_TYPEDEF_(0x80100009L) +#define SCARD_E_TIMEOUT _HRESULT_TYPEDEF_(0x8010000AL) +#define SCARD_E_SHARING_VIOLATION _HRESULT_TYPEDEF_(0x8010000BL) +#define SCARD_E_NO_SMARTCARD _HRESULT_TYPEDEF_(0x8010000CL) +#define SCARD_E_UNKNOWN_CARD _HRESULT_TYPEDEF_(0x8010000DL) +#define SCARD_E_CANT_DISPOSE _HRESULT_TYPEDEF_(0x8010000EL) +#define SCARD_E_PROTO_MISMATCH _HRESULT_TYPEDEF_(0x8010000FL) +#define SCARD_E_NOT_READY _HRESULT_TYPEDEF_(0x80100010L) +#define SCARD_E_INVALID_VALUE _HRESULT_TYPEDEF_(0x80100011L) +#define SCARD_E_SYSTEM_CANCELLED _HRESULT_TYPEDEF_(0x80100012L) +#define SCARD_F_COMM_ERROR _HRESULT_TYPEDEF_(0x80100013L) +#define SCARD_F_UNKNOWN_ERROR _HRESULT_TYPEDEF_(0x80100014L) +#define SCARD_E_INVALID_ATR _HRESULT_TYPEDEF_(0x80100015L) +#define SCARD_E_NOT_TRANSACTED _HRESULT_TYPEDEF_(0x80100016L) +#define SCARD_E_READER_UNAVAILABLE _HRESULT_TYPEDEF_(0x80100017L) +#define SCARD_P_SHUTDOWN _HRESULT_TYPEDEF_(0x80100018L) +#define SCARD_E_PCI_TOO_SMALL _HRESULT_TYPEDEF_(0x80100019L) +#define SCARD_E_READER_UNSUPPORTED _HRESULT_TYPEDEF_(0x8010001AL) +#define SCARD_E_DUPLICATE_READER _HRESULT_TYPEDEF_(0x8010001BL) +#define SCARD_E_CARD_UNSUPPORTED _HRESULT_TYPEDEF_(0x8010001CL) +#define SCARD_E_NO_SERVICE _HRESULT_TYPEDEF_(0x8010001DL) +#define SCARD_E_SERVICE_STOPPED _HRESULT_TYPEDEF_(0x8010001EL) +#define SCARD_E_UNEXPECTED _HRESULT_TYPEDEF_(0x8010001FL) +#define SCARD_E_ICC_INSTALLATION _HRESULT_TYPEDEF_(0x80100020L) +#define SCARD_E_ICC_CREATEORDER _HRESULT_TYPEDEF_(0x80100021L) +#define SCARD_E_UNSUPPORTED_FEATURE _HRESULT_TYPEDEF_(0x80100022L) +#define SCARD_E_DIR_NOT_FOUND _HRESULT_TYPEDEF_(0x80100023L) +#define SCARD_E_FILE_NOT_FOUND _HRESULT_TYPEDEF_(0x80100024L) +#define SCARD_E_NO_DIR _HRESULT_TYPEDEF_(0x80100025L) +#define SCARD_E_NO_FILE _HRESULT_TYPEDEF_(0x80100026L) +#define SCARD_E_NO_ACCESS _HRESULT_TYPEDEF_(0x80100027L) +#define SCARD_E_WRITE_TOO_MANY _HRESULT_TYPEDEF_(0x80100028L) +#define SCARD_E_BAD_SEEK _HRESULT_TYPEDEF_(0x80100029L) +#define SCARD_E_INVALID_CHV _HRESULT_TYPEDEF_(0x8010002AL) +#define SCARD_E_UNKNOWN_RES_MNG _HRESULT_TYPEDEF_(0x8010002BL) +#define SCARD_E_NO_SUCH_CERTIFICATE _HRESULT_TYPEDEF_(0x8010002CL) +#define SCARD_E_CERTIFICATE_UNAVAILABLE _HRESULT_TYPEDEF_(0x8010002DL) +#define SCARD_E_NO_READERS_AVAILABLE _HRESULT_TYPEDEF_(0x8010002EL) +#define SCARD_E_COMM_DATA_LOST _HRESULT_TYPEDEF_(0x8010002FL) +#define SCARD_E_NO_KEY_CONTAINER _HRESULT_TYPEDEF_(0x80100030L) +#define SCARD_E_SERVER_TOO_BUSY _HRESULT_TYPEDEF_(0x80100031L) +#define SCARD_W_UNSUPPORTED_CARD _HRESULT_TYPEDEF_(0x80100065L) +#define SCARD_W_UNRESPONSIVE_CARD _HRESULT_TYPEDEF_(0x80100066L) +#define SCARD_W_UNPOWERED_CARD _HRESULT_TYPEDEF_(0x80100067L) +#define SCARD_W_RESET_CARD _HRESULT_TYPEDEF_(0x80100068L) +#define SCARD_W_REMOVED_CARD _HRESULT_TYPEDEF_(0x80100069L) +#define SCARD_W_SECURITY_VIOLATION _HRESULT_TYPEDEF_(0x8010006AL) +#define SCARD_W_WRONG_CHV _HRESULT_TYPEDEF_(0x8010006BL) +#define SCARD_W_CHV_BLOCKED _HRESULT_TYPEDEF_(0x8010006CL) +#define SCARD_W_EOF _HRESULT_TYPEDEF_(0x8010006DL) +#define SCARD_W_CANCELLED_BY_USER _HRESULT_TYPEDEF_(0x8010006EL) +#define SCARD_W_CARD_NOT_AUTHENTICATED _HRESULT_TYPEDEF_(0x8010006FL) +#define COMADMIN_E_OBJECTERRORS _HRESULT_TYPEDEF_(0x80110401L) +#define COMADMIN_E_OBJECTINVALID _HRESULT_TYPEDEF_(0x80110402L) +#define COMADMIN_E_KEYMISSING _HRESULT_TYPEDEF_(0x80110403L) +#define COMADMIN_E_ALREADYINSTALLED _HRESULT_TYPEDEF_(0x80110404L) +#define COMADMIN_E_APP_FILE_WRITEFAIL _HRESULT_TYPEDEF_(0x80110407L) +#define COMADMIN_E_APP_FILE_READFAIL _HRESULT_TYPEDEF_(0x80110408L) +#define COMADMIN_E_APP_FILE_VERSION _HRESULT_TYPEDEF_(0x80110409L) +#define COMADMIN_E_BADPATH _HRESULT_TYPEDEF_(0x8011040AL) +#define COMADMIN_E_APPLICATIONEXISTS _HRESULT_TYPEDEF_(0x8011040BL) +#define COMADMIN_E_ROLEEXISTS _HRESULT_TYPEDEF_(0x8011040CL) +#define COMADMIN_E_CANTCOPYFILE _HRESULT_TYPEDEF_(0x8011040DL) +#define COMADMIN_E_NOUSER _HRESULT_TYPEDEF_(0x8011040FL) +#define COMADMIN_E_INVALIDUSERIDS _HRESULT_TYPEDEF_(0x80110410L) +#define COMADMIN_E_NOREGISTRYCLSID _HRESULT_TYPEDEF_(0x80110411L) +#define COMADMIN_E_BADREGISTRYPROGID _HRESULT_TYPEDEF_(0x80110412L) +#define COMADMIN_E_AUTHENTICATIONLEVEL _HRESULT_TYPEDEF_(0x80110413L) +#define COMADMIN_E_USERPASSWDNOTVALID _HRESULT_TYPEDEF_(0x80110414L) +#define COMADMIN_E_CLSIDORIIDMISMATCH _HRESULT_TYPEDEF_(0x80110418L) +#define COMADMIN_E_REMOTEINTERFACE _HRESULT_TYPEDEF_(0x80110419L) +#define COMADMIN_E_DLLREGISTERSERVER _HRESULT_TYPEDEF_(0x8011041AL) +#define COMADMIN_E_NOSERVERSHARE _HRESULT_TYPEDEF_(0x8011041BL) +#define COMADMIN_E_DLLLOADFAILED _HRESULT_TYPEDEF_(0x8011041DL) +#define COMADMIN_E_BADREGISTRYLIBID _HRESULT_TYPEDEF_(0x8011041EL) +#define COMADMIN_E_APPDIRNOTFOUND _HRESULT_TYPEDEF_(0x8011041FL) +#define COMADMIN_E_REGISTRARFAILED _HRESULT_TYPEDEF_(0x80110423L) +#define COMADMIN_E_COMPFILE_DOESNOTEXIST _HRESULT_TYPEDEF_(0x80110424L) +#define COMADMIN_E_COMPFILE_LOADDLLFAIL _HRESULT_TYPEDEF_(0x80110425L) +#define COMADMIN_E_COMPFILE_GETCLASSOBJ _HRESULT_TYPEDEF_(0x80110426L) +#define COMADMIN_E_COMPFILE_CLASSNOTAVAIL _HRESULT_TYPEDEF_(0x80110427L) +#define COMADMIN_E_COMPFILE_BADTLB _HRESULT_TYPEDEF_(0x80110428L) +#define COMADMIN_E_COMPFILE_NOTINSTALLABLE _HRESULT_TYPEDEF_(0x80110429L) +#define COMADMIN_E_NOTCHANGEABLE _HRESULT_TYPEDEF_(0x8011042AL) +#define COMADMIN_E_NOTDELETEABLE _HRESULT_TYPEDEF_(0x8011042BL) +#define COMADMIN_E_SESSION _HRESULT_TYPEDEF_(0x8011042CL) +#define COMADMIN_E_COMP_MOVE_LOCKED _HRESULT_TYPEDEF_(0x8011042DL) +#define COMADMIN_E_COMP_MOVE_BAD_DEST _HRESULT_TYPEDEF_(0x8011042EL) +#define COMADMIN_E_REGISTERTLB _HRESULT_TYPEDEF_(0x80110430L) +#define COMADMIN_E_SYSTEMAPP _HRESULT_TYPEDEF_(0x80110433L) +#define COMADMIN_E_COMPFILE_NOREGISTRAR _HRESULT_TYPEDEF_(0x80110434L) +#define COMADMIN_E_COREQCOMPINSTALLED _HRESULT_TYPEDEF_(0x80110435L) +#define COMADMIN_E_SERVICENOTINSTALLED _HRESULT_TYPEDEF_(0x80110436L) +#define COMADMIN_E_PROPERTYSAVEFAILED _HRESULT_TYPEDEF_(0x80110437L) +#define COMADMIN_E_OBJECTEXISTS _HRESULT_TYPEDEF_(0x80110438L) +#define COMADMIN_E_COMPONENTEXISTS _HRESULT_TYPEDEF_(0x80110439L) +#define COMADMIN_E_REGFILE_CORRUPT _HRESULT_TYPEDEF_(0x8011043BL) +#define COMADMIN_E_PROPERTY_OVERFLOW _HRESULT_TYPEDEF_(0x8011043CL) +#define COMADMIN_E_NOTINREGISTRY _HRESULT_TYPEDEF_(0x8011043EL) +#define COMADMIN_E_OBJECTNOTPOOLABLE _HRESULT_TYPEDEF_(0x8011043FL) +#define COMADMIN_E_APPLID_MATCHES_CLSID _HRESULT_TYPEDEF_(0x80110446L) +#define COMADMIN_E_ROLE_DOES_NOT_EXIST _HRESULT_TYPEDEF_(0x80110447L) +#define COMADMIN_E_START_APP_NEEDS_COMPONENTS _HRESULT_TYPEDEF_(0x80110448L) +#define COMADMIN_E_REQUIRES_DIFFERENT_PLATFORM _HRESULT_TYPEDEF_(0x80110449L) +#define COMADMIN_E_CAN_NOT_EXPORT_APP_PROXY _HRESULT_TYPEDEF_(0x8011044AL) +#define COMADMIN_E_CAN_NOT_START_APP _HRESULT_TYPEDEF_(0x8011044BL) +#define COMADMIN_E_CAN_NOT_EXPORT_SYS_APP _HRESULT_TYPEDEF_(0x8011044CL) +#define COMADMIN_E_CANT_SUBSCRIBE_TO_COMPONENT _HRESULT_TYPEDEF_(0x8011044DL) +#define COMADMIN_E_EVENTCLASS_CANT_BE_SUBSCRIBER _HRESULT_TYPEDEF_(0x8011044EL) +#define COMADMIN_E_LIB_APP_PROXY_INCOMPATIBLE _HRESULT_TYPEDEF_(0x8011044FL) +#define COMADMIN_E_BASE_PARTITION_ONLY _HRESULT_TYPEDEF_(0x80110450L) +#define COMADMIN_E_START_APP_DISABLED _HRESULT_TYPEDEF_(0x80110451L) +#define COMADMIN_E_CAT_DUPLICATE_PARTITION_NAME _HRESULT_TYPEDEF_(0x80110457L) +#define COMADMIN_E_CAT_INVALID_PARTITION_NAME _HRESULT_TYPEDEF_(0x80110458L) +#define COMADMIN_E_CAT_PARTITION_IN_USE _HRESULT_TYPEDEF_(0x80110459L) +#define COMADMIN_E_FILE_PARTITION_DUPLICATE_FILES _HRESULT_TYPEDEF_(0x8011045AL) +#define COMADMIN_E_CAT_IMPORTED_COMPONENTS_NOT_ALLOWED _HRESULT_TYPEDEF_(0x8011045BL) +#define COMADMIN_E_AMBIGUOUS_APPLICATION_NAME _HRESULT_TYPEDEF_(0x8011045CL) +#define COMADMIN_E_AMBIGUOUS_PARTITION_NAME _HRESULT_TYPEDEF_(0x8011045DL) +#define COMADMIN_E_REGDB_NOTINITIALIZED _HRESULT_TYPEDEF_(0x80110472L) +#define COMADMIN_E_REGDB_NOTOPEN _HRESULT_TYPEDEF_(0x80110473L) +#define COMADMIN_E_REGDB_SYSTEMERR _HRESULT_TYPEDEF_(0x80110474L) +#define COMADMIN_E_REGDB_ALREADYRUNNING _HRESULT_TYPEDEF_(0x80110475L) +#define COMADMIN_E_MIG_VERSIONNOTSUPPORTED _HRESULT_TYPEDEF_(0x80110480L) +#define COMADMIN_E_MIG_SCHEMANOTFOUND _HRESULT_TYPEDEF_(0x80110481L) +#define COMADMIN_E_CAT_BITNESSMISMATCH _HRESULT_TYPEDEF_(0x80110482L) +#define COMADMIN_E_CAT_UNACCEPTABLEBITNESS _HRESULT_TYPEDEF_(0x80110483L) +#define COMADMIN_E_CAT_WRONGAPPBITNESS _HRESULT_TYPEDEF_(0x80110484L) +#define COMADMIN_E_CAT_PAUSE_RESUME_NOT_SUPPORTED _HRESULT_TYPEDEF_(0x80110485L) +#define COMADMIN_E_CAT_SERVERFAULT _HRESULT_TYPEDEF_(0x80110486L) +#define COMQC_E_APPLICATION_NOT_QUEUED _HRESULT_TYPEDEF_(0x80110600L) +#define COMQC_E_NO_QUEUEABLE_INTERFACES _HRESULT_TYPEDEF_(0x80110601L) +#define COMQC_E_QUEUING_SERVICE_NOT_AVAILABLE _HRESULT_TYPEDEF_(0x80110602L) +#define COMQC_E_NO_IPERSISTSTREAM _HRESULT_TYPEDEF_(0x80110603L) +#define COMQC_E_BAD_MESSAGE _HRESULT_TYPEDEF_(0x80110604L) +#define COMQC_E_UNAUTHENTICATED _HRESULT_TYPEDEF_(0x80110605L) +#define COMQC_E_UNTRUSTED_ENQUEUER _HRESULT_TYPEDEF_(0x80110606L) +#define MSDTC_E_DUPLICATE_RESOURCE _HRESULT_TYPEDEF_(0x80110701L) +#define COMADMIN_E_OBJECT_PARENT_MISSING _HRESULT_TYPEDEF_(0x80110808L) +#define COMADMIN_E_OBJECT_DOES_NOT_EXIST _HRESULT_TYPEDEF_(0x80110809L) +#define COMADMIN_E_APP_NOT_RUNNING _HRESULT_TYPEDEF_(0x8011080AL) +#define COMADMIN_E_INVALID_PARTITION _HRESULT_TYPEDEF_(0x8011080BL) +#define COMADMIN_E_SVCAPP_NOT_POOLABLE_OR_RECYCLABLE _HRESULT_TYPEDEF_(0x8011080DL) +#define COMADMIN_E_USER_IN_SET _HRESULT_TYPEDEF_(0x8011080EL) +#define COMADMIN_E_CANTRECYCLELIBRARYAPPS _HRESULT_TYPEDEF_(0x8011080FL) +#define COMADMIN_E_CANTRECYCLESERVICEAPPS _HRESULT_TYPEDEF_(0x80110811L) +#define COMADMIN_E_PROCESSALREADYRECYCLED _HRESULT_TYPEDEF_(0x80110812L) +#define COMADMIN_E_PAUSEDPROCESSMAYNOTBERECYCLED _HRESULT_TYPEDEF_(0x80110813L) +#define COMADMIN_E_CANTMAKEINPROCSERVICE _HRESULT_TYPEDEF_(0x80110814L) +#define COMADMIN_E_PROGIDINUSEBYCLSID _HRESULT_TYPEDEF_(0x80110815L) +#define COMADMIN_E_DEFAULT_PARTITION_NOT_IN_SET _HRESULT_TYPEDEF_(0x80110816L) +#define COMADMIN_E_RECYCLEDPROCESSMAYNOTBEPAUSED _HRESULT_TYPEDEF_(0x80110817L) +#define COMADMIN_E_PARTITION_ACCESSDENIED _HRESULT_TYPEDEF_(0x80110818L) +#define COMADMIN_E_PARTITION_MSI_ONLY _HRESULT_TYPEDEF_(0x80110819L) +#define COMADMIN_E_LEGACYCOMPS_NOT_ALLOWED_IN_1_0_FORMAT _HRESULT_TYPEDEF_(0x8011081AL) +#define COMADMIN_E_LEGACYCOMPS_NOT_ALLOWED_IN_NONBASE_PARTITIONS _HRESULT_TYPEDEF_(0x8011081BL) +#define COMADMIN_E_COMP_MOVE_SOURCE _HRESULT_TYPEDEF_(0x8011081CL) +#define COMADMIN_E_COMP_MOVE_DEST _HRESULT_TYPEDEF_(0x8011081DL) +#define COMADMIN_E_COMP_MOVE_PRIVATE _HRESULT_TYPEDEF_(0x8011081EL) +#define COMADMIN_E_BASEPARTITION_REQUIRED_IN_SET _HRESULT_TYPEDEF_(0x8011081FL) +#define COMADMIN_E_CANNOT_ALIAS_EVENTCLASS _HRESULT_TYPEDEF_(0x80110820L) +#define COMADMIN_E_PRIVATE_ACCESSDENIED _HRESULT_TYPEDEF_(0x80110821L) +#define COMADMIN_E_SAFERINVALID _HRESULT_TYPEDEF_(0x80110822L) +#define COMADMIN_E_REGISTRY_ACCESSDENIED _HRESULT_TYPEDEF_(0x80110823L) +#define COMADMIN_E_PARTITIONS_DISABLED _HRESULT_TYPEDEF_(0x80110824L) +#endif /* _WINERROR_ */ diff --git a/05/tcc-final-old/win32/include/winapi/wingdi.h b/05/tcc-final-old/win32/include/winapi/wingdi.h new file mode 100644 index 0000000..63d3891 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/wingdi.h @@ -0,0 +1,4080 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINGDI_ +#define _WINGDI_ + +#define WINGDIAPI DECLSPEC_IMPORT +#define WINSPOOLAPI DECLSPEC_IMPORT + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WINVER +#define WINVER 0x0502 +#endif + +#ifndef NOGDI +#ifndef NORASTEROPS +#define R2_BLACK 1 +#define R2_NOTMERGEPEN 2 +#define R2_MASKNOTPEN 3 +#define R2_NOTCOPYPEN 4 +#define R2_MASKPENNOT 5 +#define R2_NOT 6 +#define R2_XORPEN 7 +#define R2_NOTMASKPEN 8 +#define R2_MASKPEN 9 +#define R2_NOTXORPEN 10 +#define R2_NOP 11 +#define R2_MERGENOTPEN 12 +#define R2_COPYPEN 13 +#define R2_MERGEPENNOT 14 +#define R2_MERGEPEN 15 +#define R2_WHITE 16 +#define R2_LAST 16 + +#define SRCCOPY (DWORD)0x00CC0020 +#define SRCPAINT (DWORD)0x00EE0086 +#define SRCAND (DWORD)0x008800C6 +#define SRCINVERT (DWORD)0x00660046 +#define SRCERASE (DWORD)0x00440328 +#define NOTSRCCOPY (DWORD)0x00330008 +#define NOTSRCERASE (DWORD)0x001100A6 +#define MERGECOPY (DWORD)0x00C000CA +#define MERGEPAINT (DWORD)0x00BB0226 +#define PATCOPY (DWORD)0x00F00021 +#define PATPAINT (DWORD)0x00FB0A09 +#define PATINVERT (DWORD)0x005A0049 +#define DSTINVERT (DWORD)0x00550009 +#define BLACKNESS (DWORD)0x00000042 +#define WHITENESS (DWORD)0x00FF0062 +#define NOMIRRORBITMAP (DWORD)0x80000000 +#define CAPTUREBLT (DWORD)0x40000000 +#define MAKEROP4(fore,back) (DWORD)((((back) << 8) & 0xFF000000) | (fore)) +#endif + +#define GDI_ERROR (0xFFFFFFFFL) +#define HGDI_ERROR (LongToHandle(0xFFFFFFFFL)) + +#define ERROR 0 +#define NULLREGION 1 +#define SIMPLEREGION 2 +#define COMPLEXREGION 3 +#define RGN_ERROR ERROR + +#define RGN_AND 1 +#define RGN_OR 2 +#define RGN_XOR 3 +#define RGN_DIFF 4 +#define RGN_COPY 5 +#define RGN_MIN RGN_AND +#define RGN_MAX RGN_COPY + +#define BLACKONWHITE 1 +#define WHITEONBLACK 2 +#define COLORONCOLOR 3 +#define HALFTONE 4 +#define MAXSTRETCHBLTMODE 4 + +#define STRETCH_ANDSCANS BLACKONWHITE +#define STRETCH_ORSCANS WHITEONBLACK +#define STRETCH_DELETESCANS COLORONCOLOR +#define STRETCH_HALFTONE HALFTONE + +#define ALTERNATE 1 +#define WINDING 2 +#define POLYFILL_LAST 2 + +#define LAYOUT_RTL 0x00000001 +#define LAYOUT_BTT 0x00000002 +#define LAYOUT_VBH 0x00000004 +#define LAYOUT_ORIENTATIONMASK (LAYOUT_RTL | LAYOUT_BTT | LAYOUT_VBH) +#define LAYOUT_BITMAPORIENTATIONPRESERVED 0x00000008 + +#define TA_NOUPDATECP 0 +#define TA_UPDATECP 1 + +#define TA_LEFT 0 +#define TA_RIGHT 2 +#define TA_CENTER 6 + +#define TA_TOP 0 +#define TA_BOTTOM 8 +#define TA_BASELINE 24 +#define TA_RTLREADING 256 +#define TA_MASK (TA_BASELINE+TA_CENTER+TA_UPDATECP+TA_RTLREADING) + +#define VTA_BASELINE TA_BASELINE +#define VTA_LEFT TA_BOTTOM +#define VTA_RIGHT TA_TOP +#define VTA_CENTER TA_CENTER +#define VTA_BOTTOM TA_RIGHT +#define VTA_TOP TA_LEFT + +#define ETO_OPAQUE 0x0002 +#define ETO_CLIPPED 0x0004 +#define ETO_GLYPH_INDEX 0x0010 +#define ETO_RTLREADING 0x0080 +#define ETO_NUMERICSLOCAL 0x0400 +#define ETO_NUMERICSLATIN 0x0800 +#define ETO_IGNORELANGUAGE 0x1000 +#define ETO_PDY 0x2000 + +#define ASPECT_FILTERING 0x0001 + +#define DCB_RESET 0x0001 +#define DCB_ACCUMULATE 0x0002 +#define DCB_DIRTY DCB_ACCUMULATE +#define DCB_SET (DCB_RESET | DCB_ACCUMULATE) +#define DCB_ENABLE 0x0004 +#define DCB_DISABLE 0x0008 + +#ifndef NOMETAFILE + +#define META_SETBKCOLOR 0x0201 +#define META_SETBKMODE 0x0102 +#define META_SETMAPMODE 0x0103 +#define META_SETROP2 0x0104 +#define META_SETRELABS 0x0105 +#define META_SETPOLYFILLMODE 0x0106 +#define META_SETSTRETCHBLTMODE 0x0107 +#define META_SETTEXTCHAREXTRA 0x0108 +#define META_SETTEXTCOLOR 0x0209 +#define META_SETTEXTJUSTIFICATION 0x020A +#define META_SETWINDOWORG 0x020B +#define META_SETWINDOWEXT 0x020C +#define META_SETVIEWPORTORG 0x020D +#define META_SETVIEWPORTEXT 0x020E +#define META_OFFSETWINDOWORG 0x020F +#define META_SCALEWINDOWEXT 0x0410 +#define META_OFFSETVIEWPORTORG 0x0211 +#define META_SCALEVIEWPORTEXT 0x0412 +#define META_LINETO 0x0213 +#define META_MOVETO 0x0214 +#define META_EXCLUDECLIPRECT 0x0415 +#define META_INTERSECTCLIPRECT 0x0416 +#define META_ARC 0x0817 +#define META_ELLIPSE 0x0418 +#define META_FLOODFILL 0x0419 +#define META_PIE 0x081A +#define META_RECTANGLE 0x041B +#define META_ROUNDRECT 0x061C +#define META_PATBLT 0x061D +#define META_SAVEDC 0x001E +#define META_SETPIXEL 0x041F +#define META_OFFSETCLIPRGN 0x0220 +#define META_TEXTOUT 0x0521 +#define META_BITBLT 0x0922 +#define META_STRETCHBLT 0x0B23 +#define META_POLYGON 0x0324 +#define META_POLYLINE 0x0325 +#define META_ESCAPE 0x0626 +#define META_RESTOREDC 0x0127 +#define META_FILLREGION 0x0228 +#define META_FRAMEREGION 0x0429 +#define META_INVERTREGION 0x012A +#define META_PAINTREGION 0x012B +#define META_SELECTCLIPREGION 0x012C +#define META_SELECTOBJECT 0x012D +#define META_SETTEXTALIGN 0x012E +#define META_CHORD 0x0830 +#define META_SETMAPPERFLAGS 0x0231 +#define META_EXTTEXTOUT 0x0a32 +#define META_SETDIBTODEV 0x0d33 +#define META_SELECTPALETTE 0x0234 +#define META_REALIZEPALETTE 0x0035 +#define META_ANIMATEPALETTE 0x0436 +#define META_SETPALENTRIES 0x0037 +#define META_POLYPOLYGON 0x0538 +#define META_RESIZEPALETTE 0x0139 +#define META_DIBBITBLT 0x0940 +#define META_DIBSTRETCHBLT 0x0b41 +#define META_DIBCREATEPATTERNBRUSH 0x0142 +#define META_STRETCHDIB 0x0f43 +#define META_EXTFLOODFILL 0x0548 +#define META_SETLAYOUT 0x0149 +#define META_DELETEOBJECT 0x01f0 +#define META_CREATEPALETTE 0x00f7 +#define META_CREATEPATTERNBRUSH 0x01F9 +#define META_CREATEPENINDIRECT 0x02FA +#define META_CREATEFONTINDIRECT 0x02FB +#define META_CREATEBRUSHINDIRECT 0x02FC +#define META_CREATEREGION 0x06FF + + typedef struct _DRAWPATRECT { + POINT ptPosition; + POINT ptSize; + WORD wStyle; + WORD wPattern; + } DRAWPATRECT,*PDRAWPATRECT; +#endif + +#define NEWFRAME 1 +#define ABORTDOC 2 +#define NEXTBAND 3 +#define SETCOLORTABLE 4 +#define GETCOLORTABLE 5 +#define FLUSHOUTPUT 6 +#define DRAFTMODE 7 +#define QUERYESCSUPPORT 8 +#define SETABORTPROC 9 +#define STARTDOC 10 +#define ENDDOC 11 +#define GETPHYSPAGESIZE 12 +#define GETPRINTINGOFFSET 13 +#define GETSCALINGFACTOR 14 +#define MFCOMMENT 15 +#define GETPENWIDTH 16 +#define SETCOPYCOUNT 17 +#define SELECTPAPERSOURCE 18 +#define DEVICEDATA 19 +#define PASSTHROUGH 19 +#define GETTECHNOLGY 20 +#define GETTECHNOLOGY 20 +#define SETLINECAP 21 +#define SETLINEJOIN 22 +#define SETMITERLIMIT 23 +#define BANDINFO 24 +#define DRAWPATTERNRECT 25 +#define GETVECTORPENSIZE 26 +#define GETVECTORBRUSHSIZE 27 +#define ENABLEDUPLEX 28 +#define GETSETPAPERBINS 29 +#define GETSETPRINTORIENT 30 +#define ENUMPAPERBINS 31 +#define SETDIBSCALING 32 +#define EPSPRINTING 33 +#define ENUMPAPERMETRICS 34 +#define GETSETPAPERMETRICS 35 +#define POSTSCRIPT_DATA 37 +#define POSTSCRIPT_IGNORE 38 +#define MOUSETRAILS 39 +#define GETDEVICEUNITS 42 + +#define GETEXTENDEDTEXTMETRICS 256 +#define GETEXTENTTABLE 257 +#define GETPAIRKERNTABLE 258 +#define GETTRACKKERNTABLE 259 +#define EXTTEXTOUT 512 +#define GETFACENAME 513 +#define DOWNLOADFACE 514 +#define ENABLERELATIVEWIDTHS 768 +#define ENABLEPAIRKERNING 769 +#define SETKERNTRACK 770 +#define SETALLJUSTVALUES 771 +#define SETCHARSET 772 + +#define STRETCHBLT 2048 +#define METAFILE_DRIVER 2049 +#define GETSETSCREENPARAMS 3072 +#define QUERYDIBSUPPORT 3073 +#define BEGIN_PATH 4096 +#define CLIP_TO_PATH 4097 +#define END_PATH 4098 +#define EXT_DEVICE_CAPS 4099 +#define RESTORE_CTM 4100 +#define SAVE_CTM 4101 +#define SET_ARC_DIRECTION 4102 +#define SET_BACKGROUND_COLOR 4103 +#define SET_POLY_MODE 4104 +#define SET_SCREEN_ANGLE 4105 +#define SET_SPREAD 4106 +#define TRANSFORM_CTM 4107 +#define SET_CLIP_BOX 4108 +#define SET_BOUNDS 4109 +#define SET_MIRROR_MODE 4110 +#define OPENCHANNEL 4110 +#define DOWNLOADHEADER 4111 +#define CLOSECHANNEL 4112 +#define POSTSCRIPT_PASSTHROUGH 4115 +#define ENCAPSULATED_POSTSCRIPT 4116 + +#define POSTSCRIPT_IDENTIFY 4117 +#define POSTSCRIPT_INJECTION 4118 + +#define CHECKJPEGFORMAT 4119 +#define CHECKPNGFORMAT 4120 + +#define GET_PS_FEATURESETTING 4121 + +#define SPCLPASSTHROUGH2 4568 + +#define PSIDENT_GDICENTRIC 0 +#define PSIDENT_PSCENTRIC 1 + + typedef struct _PSINJECTDATA { + DWORD DataBytes; + WORD InjectionPoint; + WORD PageNumber; + } PSINJECTDATA,*PPSINJECTDATA; + +#define PSINJECT_BEGINSTREAM 1 +#define PSINJECT_PSADOBE 2 +#define PSINJECT_PAGESATEND 3 +#define PSINJECT_PAGES 4 + +#define PSINJECT_DOCNEEDEDRES 5 +#define PSINJECT_DOCSUPPLIEDRES 6 +#define PSINJECT_PAGEORDER 7 +#define PSINJECT_ORIENTATION 8 +#define PSINJECT_BOUNDINGBOX 9 +#define PSINJECT_DOCUMENTPROCESSCOLORS 10 + +#define PSINJECT_COMMENTS 11 +#define PSINJECT_BEGINDEFAULTS 12 +#define PSINJECT_ENDDEFAULTS 13 +#define PSINJECT_BEGINPROLOG 14 +#define PSINJECT_ENDPROLOG 15 +#define PSINJECT_BEGINSETUP 16 +#define PSINJECT_ENDSETUP 17 +#define PSINJECT_TRAILER 18 +#define PSINJECT_EOF 19 +#define PSINJECT_ENDSTREAM 20 +#define PSINJECT_DOCUMENTPROCESSCOLORSATEND 21 + +#define PSINJECT_PAGENUMBER 100 +#define PSINJECT_BEGINPAGESETUP 101 +#define PSINJECT_ENDPAGESETUP 102 +#define PSINJECT_PAGETRAILER 103 +#define PSINJECT_PLATECOLOR 104 + +#define PSINJECT_SHOWPAGE 105 +#define PSINJECT_PAGEBBOX 106 +#define PSINJECT_ENDPAGECOMMENTS 107 + +#define PSINJECT_VMSAVE 200 +#define PSINJECT_VMRESTORE 201 + +#define FEATURESETTING_NUP 0 +#define FEATURESETTING_OUTPUT 1 +#define FEATURESETTING_PSLEVEL 2 +#define FEATURESETTING_CUSTPAPER 3 +#define FEATURESETTING_MIRROR 4 +#define FEATURESETTING_NEGATIVE 5 +#define FEATURESETTING_PROTOCOL 6 + +#define FEATURESETTING_PRIVATE_BEGIN 0x1000 +#define FEATURESETTING_PRIVATE_END 0x1FFF + + typedef struct _PSFEATURE_OUTPUT { + WINBOOL bPageIndependent; + WINBOOL bSetPageDevice; + } PSFEATURE_OUTPUT,*PPSFEATURE_OUTPUT; + + typedef struct _PSFEATURE_CUSTPAPER { + LONG lOrientation; + LONG lWidth; + LONG lHeight; + LONG lWidthOffset; + LONG lHeightOffset; + } PSFEATURE_CUSTPAPER,*PPSFEATURE_CUSTPAPER; + +#define PSPROTOCOL_ASCII 0 +#define PSPROTOCOL_BCP 1 +#define PSPROTOCOL_TBCP 2 +#define PSPROTOCOL_BINARY 3 + +#define QDI_SETDIBITS 1 +#define QDI_GETDIBITS 2 +#define QDI_DIBTOSCREEN 4 +#define QDI_STRETCHDIB 8 + +#define SP_NOTREPORTED 0x4000 +#define SP_ERROR (-1) +#define SP_APPABORT (-2) +#define SP_USERABORT (-3) +#define SP_OUTOFDISK (-4) +#define SP_OUTOFMEMORY (-5) + +#define PR_JOBSTATUS 0x0000 + +#define OBJ_PEN 1 +#define OBJ_BRUSH 2 +#define OBJ_DC 3 +#define OBJ_METADC 4 +#define OBJ_PAL 5 +#define OBJ_FONT 6 +#define OBJ_BITMAP 7 +#define OBJ_REGION 8 +#define OBJ_METAFILE 9 +#define OBJ_MEMDC 10 +#define OBJ_EXTPEN 11 +#define OBJ_ENHMETADC 12 +#define OBJ_ENHMETAFILE 13 +#define OBJ_COLORSPACE 14 + +#define MWT_IDENTITY 1 +#define MWT_LEFTMULTIPLY 2 +#define MWT_RIGHTMULTIPLY 3 + +#define MWT_MIN MWT_IDENTITY +#define MWT_MAX MWT_RIGHTMULTIPLY + +#define _XFORM_ + typedef struct tagXFORM { + FLOAT eM11; + FLOAT eM12; + FLOAT eM21; + FLOAT eM22; + FLOAT eDx; + FLOAT eDy; + } XFORM,*PXFORM,*LPXFORM; + + typedef struct tagBITMAP { + LONG bmType; + LONG bmWidth; + LONG bmHeight; + LONG bmWidthBytes; + WORD bmPlanes; + WORD bmBitsPixel; + LPVOID bmBits; + } BITMAP,*PBITMAP,*NPBITMAP,*LPBITMAP; + +#include + typedef struct tagRGBTRIPLE { + BYTE rgbtBlue; + BYTE rgbtGreen; + BYTE rgbtRed; + } RGBTRIPLE; +#include + + typedef struct tagRGBQUAD { + BYTE rgbBlue; + BYTE rgbGreen; + BYTE rgbRed; + BYTE rgbReserved; + } RGBQUAD; + typedef RGBQUAD *LPRGBQUAD; + +#define CS_ENABLE 0x00000001L +#define CS_DISABLE 0x00000002L +#define CS_DELETE_TRANSFORM 0x00000003L + +//!__TINYC__: #define LCS_SIGNATURE 'PSOC' +//!__TINYC__: #define LCS_sRGB 'sRGB' +//!__TINYC__: #define LCS_WINDOWS_COLOR_SPACE 'Win ' + + typedef LONG LCSCSTYPE; +#define LCS_CALIBRATED_RGB 0x00000000L + + typedef LONG LCSGAMUTMATCH; +#define LCS_GM_BUSINESS 0x00000001L +#define LCS_GM_GRAPHICS 0x00000002L +#define LCS_GM_IMAGES 0x00000004L +#define LCS_GM_ABS_COLORIMETRIC 0x00000008L + +#define CM_OUT_OF_GAMUT 255 +#define CM_IN_GAMUT 0 + +#define ICM_ADDPROFILE 1 +#define ICM_DELETEPROFILE 2 +#define ICM_QUERYPROFILE 3 +#define ICM_SETDEFAULTPROFILE 4 +#define ICM_REGISTERICMATCHER 5 +#define ICM_UNREGISTERICMATCHER 6 +#define ICM_QUERYMATCH 7 + +#define GetKValue(cmyk) ((BYTE)(cmyk)) +#define GetYValue(cmyk) ((BYTE)((cmyk)>> 8)) +#define GetMValue(cmyk) ((BYTE)((cmyk)>>16)) +#define GetCValue(cmyk) ((BYTE)((cmyk)>>24)) + +#define CMYK(c,m,y,k) ((COLORREF)((((BYTE)(k)|((WORD)((BYTE)(y))<<8))|(((DWORD)(BYTE)(m))<<16))|(((DWORD)(BYTE)(c))<<24))) + + typedef long FXPT16DOT16,*LPFXPT16DOT16; + typedef long FXPT2DOT30,*LPFXPT2DOT30; + + typedef struct tagCIEXYZ { + FXPT2DOT30 ciexyzX; + FXPT2DOT30 ciexyzY; + FXPT2DOT30 ciexyzZ; + } CIEXYZ; + typedef CIEXYZ *LPCIEXYZ; + + typedef struct tagICEXYZTRIPLE { + CIEXYZ ciexyzRed; + CIEXYZ ciexyzGreen; + CIEXYZ ciexyzBlue; + } CIEXYZTRIPLE; + + typedef CIEXYZTRIPLE *LPCIEXYZTRIPLE; + + typedef struct tagLOGCOLORSPACEA { + DWORD lcsSignature; + DWORD lcsVersion; + DWORD lcsSize; + LCSCSTYPE lcsCSType; + LCSGAMUTMATCH lcsIntent; + CIEXYZTRIPLE lcsEndpoints; + DWORD lcsGammaRed; + DWORD lcsGammaGreen; + DWORD lcsGammaBlue; + CHAR lcsFilename[MAX_PATH]; + } LOGCOLORSPACEA,*LPLOGCOLORSPACEA; + + typedef struct tagLOGCOLORSPACEW { + DWORD lcsSignature; + DWORD lcsVersion; + DWORD lcsSize; + LCSCSTYPE lcsCSType; + LCSGAMUTMATCH lcsIntent; + CIEXYZTRIPLE lcsEndpoints; + DWORD lcsGammaRed; + DWORD lcsGammaGreen; + DWORD lcsGammaBlue; + WCHAR lcsFilename[MAX_PATH]; + } LOGCOLORSPACEW,*LPLOGCOLORSPACEW; + +#ifdef UNICODE + typedef LOGCOLORSPACEW LOGCOLORSPACE; + typedef LPLOGCOLORSPACEW LPLOGCOLORSPACE; +#else + typedef LOGCOLORSPACEA LOGCOLORSPACE; + typedef LPLOGCOLORSPACEA LPLOGCOLORSPACE; +#endif + + typedef struct tagBITMAPCOREHEADER { + DWORD bcSize; + WORD bcWidth; + WORD bcHeight; + WORD bcPlanes; + WORD bcBitCount; + } BITMAPCOREHEADER,*LPBITMAPCOREHEADER,*PBITMAPCOREHEADER; + + typedef struct tagBITMAPINFOHEADER { + DWORD biSize; + LONG biWidth; + LONG biHeight; + WORD biPlanes; + WORD biBitCount; + DWORD biCompression; + DWORD biSizeImage; + LONG biXPelsPerMeter; + LONG biYPelsPerMeter; + DWORD biClrUsed; + DWORD biClrImportant; + } BITMAPINFOHEADER,*LPBITMAPINFOHEADER,*PBITMAPINFOHEADER; + + typedef struct { + DWORD bV4Size; + LONG bV4Width; + LONG bV4Height; + WORD bV4Planes; + WORD bV4BitCount; + DWORD bV4V4Compression; + DWORD bV4SizeImage; + LONG bV4XPelsPerMeter; + LONG bV4YPelsPerMeter; + DWORD bV4ClrUsed; + DWORD bV4ClrImportant; + DWORD bV4RedMask; + DWORD bV4GreenMask; + DWORD bV4BlueMask; + DWORD bV4AlphaMask; + DWORD bV4CSType; + CIEXYZTRIPLE bV4Endpoints; + DWORD bV4GammaRed; + DWORD bV4GammaGreen; + DWORD bV4GammaBlue; + } BITMAPV4HEADER,*LPBITMAPV4HEADER,*PBITMAPV4HEADER; + + typedef struct { + DWORD bV5Size; + LONG bV5Width; + LONG bV5Height; + WORD bV5Planes; + WORD bV5BitCount; + DWORD bV5Compression; + DWORD bV5SizeImage; + LONG bV5XPelsPerMeter; + LONG bV5YPelsPerMeter; + DWORD bV5ClrUsed; + DWORD bV5ClrImportant; + DWORD bV5RedMask; + DWORD bV5GreenMask; + DWORD bV5BlueMask; + DWORD bV5AlphaMask; + DWORD bV5CSType; + CIEXYZTRIPLE bV5Endpoints; + DWORD bV5GammaRed; + DWORD bV5GammaGreen; + DWORD bV5GammaBlue; + DWORD bV5Intent; + DWORD bV5ProfileData; + DWORD bV5ProfileSize; + DWORD bV5Reserved; + } BITMAPV5HEADER,*LPBITMAPV5HEADER,*PBITMAPV5HEADER; + +//!__TINYC__: #define PROFILE_LINKED 'LINK' +//!__TINYC__: #define PROFILE_EMBEDDED 'MBED' + +#define BI_RGB 0L +#define BI_RLE8 1L +#define BI_RLE4 2L +#define BI_BITFIELDS 3L +#define BI_JPEG 4L +#define BI_PNG 5L + + typedef struct tagBITMAPINFO { + BITMAPINFOHEADER bmiHeader; + RGBQUAD bmiColors[1]; + } BITMAPINFO,*LPBITMAPINFO,*PBITMAPINFO; + + typedef struct tagBITMAPCOREINFO { + BITMAPCOREHEADER bmciHeader; + RGBTRIPLE bmciColors[1]; + } BITMAPCOREINFO,*LPBITMAPCOREINFO,*PBITMAPCOREINFO; + +#include + typedef struct tagBITMAPFILEHEADER { + WORD bfType; + DWORD bfSize; + WORD bfReserved1; + WORD bfReserved2; + DWORD bfOffBits; + } BITMAPFILEHEADER,*LPBITMAPFILEHEADER,*PBITMAPFILEHEADER; +#include + +#define MAKEPOINTS(l) (*((POINTS *)&(l))) + +#ifndef NOFONTSIG + typedef struct tagFONTSIGNATURE { + DWORD fsUsb[4]; + DWORD fsCsb[2]; + } FONTSIGNATURE,*PFONTSIGNATURE,*LPFONTSIGNATURE; + + typedef struct tagCHARSETINFO { + UINT ciCharset; + UINT ciACP; + FONTSIGNATURE fs; + } CHARSETINFO,*PCHARSETINFO,*NPCHARSETINFO,*LPCHARSETINFO; + +#define TCI_SRCCHARSET 1 +#define TCI_SRCCODEPAGE 2 +#define TCI_SRCFONTSIG 3 +#define TCI_SRCLOCALE 0x1000 + + typedef struct tagLOCALESIGNATURE { + DWORD lsUsb[4]; + DWORD lsCsbDefault[2]; + DWORD lsCsbSupported[2]; + } LOCALESIGNATURE,*PLOCALESIGNATURE,*LPLOCALESIGNATURE; +#endif + + +#ifndef NOMETAFILE + typedef struct tagHANDLETABLE { + HGDIOBJ objectHandle[1]; + } HANDLETABLE,*PHANDLETABLE,*LPHANDLETABLE; + + typedef struct tagMETARECORD { + DWORD rdSize; + WORD rdFunction; + WORD rdParm[1]; + } METARECORD; + typedef struct tagMETARECORD UNALIGNED *PMETARECORD; + typedef struct tagMETARECORD UNALIGNED *LPMETARECORD; + + typedef struct tagMETAFILEPICT { + LONG mm; + LONG xExt; + LONG yExt; + HMETAFILE hMF; + } METAFILEPICT,*LPMETAFILEPICT; + +#include + typedef struct tagMETAHEADER { + WORD mtType; + WORD mtHeaderSize; + WORD mtVersion; + DWORD mtSize; + WORD mtNoObjects; + DWORD mtMaxRecord; + WORD mtNoParameters; + } METAHEADER; + typedef struct tagMETAHEADER UNALIGNED *PMETAHEADER; + typedef struct tagMETAHEADER UNALIGNED *LPMETAHEADER; + +#include + + typedef struct tagENHMETARECORD { + DWORD iType; + DWORD nSize; + DWORD dParm[1]; + } ENHMETARECORD,*PENHMETARECORD,*LPENHMETARECORD; + + typedef struct tagENHMETAHEADER { + DWORD iType; + DWORD nSize; + RECTL rclBounds; + RECTL rclFrame; + DWORD dSignature; + DWORD nVersion; + DWORD nBytes; + DWORD nRecords; + WORD nHandles; + WORD sReserved; + DWORD nDescription; + DWORD offDescription; + DWORD nPalEntries; + SIZEL szlDevice; + SIZEL szlMillimeters; + DWORD cbPixelFormat; + DWORD offPixelFormat; + DWORD bOpenGL; + SIZEL szlMicrometers; + } ENHMETAHEADER,*PENHMETAHEADER,*LPENHMETAHEADER; +#endif + +#ifndef NOTEXTMETRIC +#define TMPF_FIXED_PITCH 0x01 +#define TMPF_VECTOR 0x02 +#define TMPF_DEVICE 0x08 +#define TMPF_TRUETYPE 0x04 + +#ifdef UNICODE + typedef WCHAR BCHAR; +#else + typedef BYTE BCHAR; +#endif + +#ifndef _TEXTMETRIC_DEFINED +#define _TEXTMETRIC_DEFINED + typedef struct tagTEXTMETRICA { + LONG tmHeight; + LONG tmAscent; + LONG tmDescent; + LONG tmInternalLeading; + LONG tmExternalLeading; + LONG tmAveCharWidth; + LONG tmMaxCharWidth; + LONG tmWeight; + LONG tmOverhang; + LONG tmDigitizedAspectX; + LONG tmDigitizedAspectY; + BYTE tmFirstChar; + BYTE tmLastChar; + BYTE tmDefaultChar; + BYTE tmBreakChar; + BYTE tmItalic; + BYTE tmUnderlined; + BYTE tmStruckOut; + BYTE tmPitchAndFamily; + BYTE tmCharSet; + } TEXTMETRICA,*PTEXTMETRICA,*NPTEXTMETRICA,*LPTEXTMETRICA; + + typedef struct tagTEXTMETRICW { + LONG tmHeight; + LONG tmAscent; + LONG tmDescent; + LONG tmInternalLeading; + LONG tmExternalLeading; + LONG tmAveCharWidth; + LONG tmMaxCharWidth; + LONG tmWeight; + LONG tmOverhang; + LONG tmDigitizedAspectX; + LONG tmDigitizedAspectY; + WCHAR tmFirstChar; + WCHAR tmLastChar; + WCHAR tmDefaultChar; + WCHAR tmBreakChar; + BYTE tmItalic; + BYTE tmUnderlined; + BYTE tmStruckOut; + BYTE tmPitchAndFamily; + BYTE tmCharSet; + } TEXTMETRICW,*PTEXTMETRICW,*NPTEXTMETRICW,*LPTEXTMETRICW; +#ifdef UNICODE + typedef TEXTMETRICW TEXTMETRIC; + typedef PTEXTMETRICW PTEXTMETRIC; + typedef NPTEXTMETRICW NPTEXTMETRIC; + typedef LPTEXTMETRICW LPTEXTMETRIC; +#else + typedef TEXTMETRICA TEXTMETRIC; + typedef PTEXTMETRICA PTEXTMETRIC; + typedef NPTEXTMETRICA NPTEXTMETRIC; + typedef LPTEXTMETRICA LPTEXTMETRIC; +#endif +#endif + +#define NTM_REGULAR 0x00000040L +#define NTM_BOLD 0x00000020L +#define NTM_ITALIC 0x00000001L + +#define NTM_NONNEGATIVE_AC 0x00010000 +#define NTM_PS_OPENTYPE 0x00020000 +#define NTM_TT_OPENTYPE 0x00040000 +#define NTM_MULTIPLEMASTER 0x00080000 +#define NTM_TYPE1 0x00100000 +#define NTM_DSIG 0x00200000 + +#include + typedef struct tagNEWTEXTMETRICA { + LONG tmHeight; + LONG tmAscent; + LONG tmDescent; + LONG tmInternalLeading; + LONG tmExternalLeading; + LONG tmAveCharWidth; + LONG tmMaxCharWidth; + LONG tmWeight; + LONG tmOverhang; + LONG tmDigitizedAspectX; + LONG tmDigitizedAspectY; + BYTE tmFirstChar; + BYTE tmLastChar; + BYTE tmDefaultChar; + BYTE tmBreakChar; + BYTE tmItalic; + BYTE tmUnderlined; + BYTE tmStruckOut; + BYTE tmPitchAndFamily; + BYTE tmCharSet; + DWORD ntmFlags; + UINT ntmSizeEM; + UINT ntmCellHeight; + UINT ntmAvgWidth; + } NEWTEXTMETRICA,*PNEWTEXTMETRICA,*NPNEWTEXTMETRICA,*LPNEWTEXTMETRICA; + + typedef struct tagNEWTEXTMETRICW { + LONG tmHeight; + LONG tmAscent; + LONG tmDescent; + LONG tmInternalLeading; + LONG tmExternalLeading; + LONG tmAveCharWidth; + LONG tmMaxCharWidth; + LONG tmWeight; + LONG tmOverhang; + LONG tmDigitizedAspectX; + LONG tmDigitizedAspectY; + WCHAR tmFirstChar; + WCHAR tmLastChar; + WCHAR tmDefaultChar; + WCHAR tmBreakChar; + BYTE tmItalic; + BYTE tmUnderlined; + BYTE tmStruckOut; + BYTE tmPitchAndFamily; + BYTE tmCharSet; + DWORD ntmFlags; + UINT ntmSizeEM; + UINT ntmCellHeight; + UINT ntmAvgWidth; + } NEWTEXTMETRICW,*PNEWTEXTMETRICW,*NPNEWTEXTMETRICW,*LPNEWTEXTMETRICW; +#ifdef UNICODE + typedef NEWTEXTMETRICW NEWTEXTMETRIC; + typedef PNEWTEXTMETRICW PNEWTEXTMETRIC; + typedef NPNEWTEXTMETRICW NPNEWTEXTMETRIC; + typedef LPNEWTEXTMETRICW LPNEWTEXTMETRIC; +#else + typedef NEWTEXTMETRICA NEWTEXTMETRIC; + typedef PNEWTEXTMETRICA PNEWTEXTMETRIC; + typedef NPNEWTEXTMETRICA NPNEWTEXTMETRIC; + typedef LPNEWTEXTMETRICA LPNEWTEXTMETRIC; +#endif +#include + + typedef struct tagNEWTEXTMETRICEXA { + NEWTEXTMETRICA ntmTm; + FONTSIGNATURE ntmFontSig; + } NEWTEXTMETRICEXA; + + typedef struct tagNEWTEXTMETRICEXW { + NEWTEXTMETRICW ntmTm; + FONTSIGNATURE ntmFontSig; + } NEWTEXTMETRICEXW; +#ifdef UNICODE + typedef NEWTEXTMETRICEXW NEWTEXTMETRICEX; +#else + typedef NEWTEXTMETRICEXA NEWTEXTMETRICEX; +#endif +#endif + + typedef struct tagPELARRAY { + LONG paXCount; + LONG paYCount; + LONG paXExt; + LONG paYExt; + BYTE paRGBs; + } PELARRAY,*PPELARRAY,*NPPELARRAY,*LPPELARRAY; + + typedef struct tagLOGBRUSH { + UINT lbStyle; + COLORREF lbColor; + ULONG_PTR lbHatch; + } LOGBRUSH,*PLOGBRUSH,*NPLOGBRUSH,*LPLOGBRUSH; + + typedef struct tagLOGBRUSH32 { + UINT lbStyle; + COLORREF lbColor; + ULONG lbHatch; + } LOGBRUSH32,*PLOGBRUSH32,*NPLOGBRUSH32,*LPLOGBRUSH32; + + typedef LOGBRUSH PATTERN; + typedef PATTERN *PPATTERN; + typedef PATTERN *NPPATTERN; + typedef PATTERN *LPPATTERN; + + typedef struct tagLOGPEN { + UINT lopnStyle; + POINT lopnWidth; + COLORREF lopnColor; + } LOGPEN,*PLOGPEN,*NPLOGPEN,*LPLOGPEN; + + typedef struct tagEXTLOGPEN { + DWORD elpPenStyle; + DWORD elpWidth; + UINT elpBrushStyle; + COLORREF elpColor; + ULONG_PTR elpHatch; + DWORD elpNumEntries; + DWORD elpStyleEntry[1]; + } EXTLOGPEN,*PEXTLOGPEN,*NPEXTLOGPEN,*LPEXTLOGPEN; + +#ifndef _PALETTEENTRY_DEFINED +#define _PALETTEENTRY_DEFINED + typedef struct tagPALETTEENTRY { + BYTE peRed; + BYTE peGreen; + BYTE peBlue; + BYTE peFlags; + } PALETTEENTRY,*PPALETTEENTRY,*LPPALETTEENTRY; +#endif + +#ifndef _LOGPALETTE_DEFINED +#define _LOGPALETTE_DEFINED + + typedef struct tagLOGPALETTE { + WORD palVersion; + WORD palNumEntries; + PALETTEENTRY palPalEntry[1]; + } LOGPALETTE,*PLOGPALETTE,*NPLOGPALETTE,*LPLOGPALETTE; +#endif + +#define LF_FACESIZE 32 + + typedef struct tagLOGFONTA { + LONG lfHeight; + LONG lfWidth; + LONG lfEscapement; + LONG lfOrientation; + LONG lfWeight; + BYTE lfItalic; + BYTE lfUnderline; + BYTE lfStrikeOut; + BYTE lfCharSet; + BYTE lfOutPrecision; + BYTE lfClipPrecision; + BYTE lfQuality; + BYTE lfPitchAndFamily; + CHAR lfFaceName[LF_FACESIZE]; + } LOGFONTA,*PLOGFONTA,*NPLOGFONTA,*LPLOGFONTA; + + typedef struct tagLOGFONTW { + LONG lfHeight; + LONG lfWidth; + LONG lfEscapement; + LONG lfOrientation; + LONG lfWeight; + BYTE lfItalic; + BYTE lfUnderline; + BYTE lfStrikeOut; + BYTE lfCharSet; + BYTE lfOutPrecision; + BYTE lfClipPrecision; + BYTE lfQuality; + BYTE lfPitchAndFamily; + WCHAR lfFaceName[LF_FACESIZE]; + } LOGFONTW,*PLOGFONTW,*NPLOGFONTW,*LPLOGFONTW; +#ifdef UNICODE + typedef LOGFONTW LOGFONT; + typedef PLOGFONTW PLOGFONT; + typedef NPLOGFONTW NPLOGFONT; + typedef LPLOGFONTW LPLOGFONT; +#else + typedef LOGFONTA LOGFONT; + typedef PLOGFONTA PLOGFONT; + typedef NPLOGFONTA NPLOGFONT; + typedef LPLOGFONTA LPLOGFONT; +#endif + +#define LF_FULLFACESIZE 64 + + typedef struct tagENUMLOGFONTA { + LOGFONTA elfLogFont; + BYTE elfFullName[LF_FULLFACESIZE]; + BYTE elfStyle[LF_FACESIZE]; + } ENUMLOGFONTA,*LPENUMLOGFONTA; + + typedef struct tagENUMLOGFONTW { + LOGFONTW elfLogFont; + WCHAR elfFullName[LF_FULLFACESIZE]; + WCHAR elfStyle[LF_FACESIZE]; + } ENUMLOGFONTW,*LPENUMLOGFONTW; +#ifdef UNICODE + typedef ENUMLOGFONTW ENUMLOGFONT; + typedef LPENUMLOGFONTW LPENUMLOGFONT; +#else + typedef ENUMLOGFONTA ENUMLOGFONT; + typedef LPENUMLOGFONTA LPENUMLOGFONT; +#endif + + typedef struct tagENUMLOGFONTEXA { + LOGFONTA elfLogFont; + BYTE elfFullName[LF_FULLFACESIZE]; + BYTE elfStyle[LF_FACESIZE]; + BYTE elfScript[LF_FACESIZE]; + } ENUMLOGFONTEXA,*LPENUMLOGFONTEXA; + + typedef struct tagENUMLOGFONTEXW { + LOGFONTW elfLogFont; + WCHAR elfFullName[LF_FULLFACESIZE]; + WCHAR elfStyle[LF_FACESIZE]; + WCHAR elfScript[LF_FACESIZE]; + } ENUMLOGFONTEXW,*LPENUMLOGFONTEXW; +#ifdef UNICODE + typedef ENUMLOGFONTEXW ENUMLOGFONTEX; + typedef LPENUMLOGFONTEXW LPENUMLOGFONTEX; +#else + typedef ENUMLOGFONTEXA ENUMLOGFONTEX; + typedef LPENUMLOGFONTEXA LPENUMLOGFONTEX; +#endif + +#define OUT_DEFAULT_PRECIS 0 +#define OUT_STRING_PRECIS 1 +#define OUT_CHARACTER_PRECIS 2 +#define OUT_STROKE_PRECIS 3 +#define OUT_TT_PRECIS 4 +#define OUT_DEVICE_PRECIS 5 +#define OUT_RASTER_PRECIS 6 +#define OUT_TT_ONLY_PRECIS 7 +#define OUT_OUTLINE_PRECIS 8 +#define OUT_SCREEN_OUTLINE_PRECIS 9 +#define OUT_PS_ONLY_PRECIS 10 + +#define CLIP_DEFAULT_PRECIS 0 +#define CLIP_CHARACTER_PRECIS 1 +#define CLIP_STROKE_PRECIS 2 +#define CLIP_MASK 0xf +#define CLIP_LH_ANGLES (1<<4) +#define CLIP_TT_ALWAYS (2<<4) +#define CLIP_DFA_DISABLE (4<<4) +#define CLIP_EMBEDDED (8<<4) + +#define DEFAULT_QUALITY 0 +#define DRAFT_QUALITY 1 +#define PROOF_QUALITY 2 +#define NONANTIALIASED_QUALITY 3 +#define ANTIALIASED_QUALITY 4 + +#define CLEARTYPE_QUALITY 5 +#define CLEARTYPE_NATURAL_QUALITY 6 + +#define DEFAULT_PITCH 0 +#define FIXED_PITCH 1 +#define VARIABLE_PITCH 2 +#define MONO_FONT 8 + +#define ANSI_CHARSET 0 +#define DEFAULT_CHARSET 1 +#define SYMBOL_CHARSET 2 +#define SHIFTJIS_CHARSET 128 +#define HANGEUL_CHARSET 129 +#define HANGUL_CHARSET 129 +#define GB2312_CHARSET 134 +#define CHINESEBIG5_CHARSET 136 +#define OEM_CHARSET 255 +#define JOHAB_CHARSET 130 +#define HEBREW_CHARSET 177 +#define ARABIC_CHARSET 178 +#define GREEK_CHARSET 161 +#define TURKISH_CHARSET 162 +#define VIETNAMESE_CHARSET 163 +#define THAI_CHARSET 222 +#define EASTEUROPE_CHARSET 238 +#define RUSSIAN_CHARSET 204 + +#define MAC_CHARSET 77 +#define BALTIC_CHARSET 186 + +#define FS_LATIN1 0x00000001L +#define FS_LATIN2 0x00000002L +#define FS_CYRILLIC 0x00000004L +#define FS_GREEK 0x00000008L +#define FS_TURKISH 0x00000010L +#define FS_HEBREW 0x00000020L +#define FS_ARABIC 0x00000040L +#define FS_BALTIC 0x00000080L +#define FS_VIETNAMESE 0x00000100L +#define FS_THAI 0x00010000L +#define FS_JISJAPAN 0x00020000L +#define FS_CHINESESIMP 0x00040000L +#define FS_WANSUNG 0x00080000L +#define FS_CHINESETRAD 0x00100000L +#define FS_JOHAB 0x00200000L +#define FS_SYMBOL 0x80000000L + +#define FF_DONTCARE (0<<4) +#define FF_ROMAN (1<<4) + +#define FF_SWISS (2<<4) + +#define FF_MODERN (3<<4) + +#define FF_SCRIPT (4<<4) +#define FF_DECORATIVE (5<<4) + +#define FW_DONTCARE 0 +#define FW_THIN 100 +#define FW_EXTRALIGHT 200 +#define FW_LIGHT 300 +#define FW_NORMAL 400 +#define FW_MEDIUM 500 +#define FW_SEMIBOLD 600 +#define FW_BOLD 700 +#define FW_EXTRABOLD 800 +#define FW_HEAVY 900 + +#define FW_ULTRALIGHT FW_EXTRALIGHT +#define FW_REGULAR FW_NORMAL +#define FW_DEMIBOLD FW_SEMIBOLD +#define FW_ULTRABOLD FW_EXTRABOLD +#define FW_BLACK FW_HEAVY + +#define PANOSE_COUNT 10 +#define PAN_FAMILYTYPE_INDEX 0 +#define PAN_SERIFSTYLE_INDEX 1 +#define PAN_WEIGHT_INDEX 2 +#define PAN_PROPORTION_INDEX 3 +#define PAN_CONTRAST_INDEX 4 +#define PAN_STROKEVARIATION_INDEX 5 +#define PAN_ARMSTYLE_INDEX 6 +#define PAN_LETTERFORM_INDEX 7 +#define PAN_MIDLINE_INDEX 8 +#define PAN_XHEIGHT_INDEX 9 + +#define PAN_CULTURE_LATIN 0 + + typedef struct tagPANOSE { + BYTE bFamilyType; + BYTE bSerifStyle; + BYTE bWeight; + BYTE bProportion; + BYTE bContrast; + BYTE bStrokeVariation; + BYTE bArmStyle; + BYTE bLetterform; + BYTE bMidline; + BYTE bXHeight; + } PANOSE,*LPPANOSE; + +#define PAN_ANY 0 +#define PAN_NO_FIT 1 + +#define PAN_FAMILY_TEXT_DISPLAY 2 +#define PAN_FAMILY_SCRIPT 3 +#define PAN_FAMILY_DECORATIVE 4 +#define PAN_FAMILY_PICTORIAL 5 + +#define PAN_SERIF_COVE 2 +#define PAN_SERIF_OBTUSE_COVE 3 +#define PAN_SERIF_SQUARE_COVE 4 +#define PAN_SERIF_OBTUSE_SQUARE_COVE 5 +#define PAN_SERIF_SQUARE 6 +#define PAN_SERIF_THIN 7 +#define PAN_SERIF_BONE 8 +#define PAN_SERIF_EXAGGERATED 9 +#define PAN_SERIF_TRIANGLE 10 +#define PAN_SERIF_NORMAL_SANS 11 +#define PAN_SERIF_OBTUSE_SANS 12 +#define PAN_SERIF_PERP_SANS 13 +#define PAN_SERIF_FLARED 14 +#define PAN_SERIF_ROUNDED 15 + +#define PAN_WEIGHT_VERY_LIGHT 2 +#define PAN_WEIGHT_LIGHT 3 +#define PAN_WEIGHT_THIN 4 +#define PAN_WEIGHT_BOOK 5 +#define PAN_WEIGHT_MEDIUM 6 +#define PAN_WEIGHT_DEMI 7 +#define PAN_WEIGHT_BOLD 8 +#define PAN_WEIGHT_HEAVY 9 +#define PAN_WEIGHT_BLACK 10 +#define PAN_WEIGHT_NORD 11 + +#define PAN_PROP_OLD_STYLE 2 +#define PAN_PROP_MODERN 3 +#define PAN_PROP_EVEN_WIDTH 4 +#define PAN_PROP_EXPANDED 5 +#define PAN_PROP_CONDENSED 6 +#define PAN_PROP_VERY_EXPANDED 7 +#define PAN_PROP_VERY_CONDENSED 8 +#define PAN_PROP_MONOSPACED 9 + +#define PAN_CONTRAST_NONE 2 +#define PAN_CONTRAST_VERY_LOW 3 +#define PAN_CONTRAST_LOW 4 +#define PAN_CONTRAST_MEDIUM_LOW 5 +#define PAN_CONTRAST_MEDIUM 6 +#define PAN_CONTRAST_MEDIUM_HIGH 7 +#define PAN_CONTRAST_HIGH 8 +#define PAN_CONTRAST_VERY_HIGH 9 + +#define PAN_STROKE_GRADUAL_DIAG 2 +#define PAN_STROKE_GRADUAL_TRAN 3 +#define PAN_STROKE_GRADUAL_VERT 4 +#define PAN_STROKE_GRADUAL_HORZ 5 +#define PAN_STROKE_RAPID_VERT 6 +#define PAN_STROKE_RAPID_HORZ 7 +#define PAN_STROKE_INSTANT_VERT 8 + +#define PAN_STRAIGHT_ARMS_HORZ 2 +#define PAN_STRAIGHT_ARMS_WEDGE 3 +#define PAN_STRAIGHT_ARMS_VERT 4 +#define PAN_STRAIGHT_ARMS_SINGLE_SERIF 5 +#define PAN_STRAIGHT_ARMS_DOUBLE_SERIF 6 +#define PAN_BENT_ARMS_HORZ 7 +#define PAN_BENT_ARMS_WEDGE 8 +#define PAN_BENT_ARMS_VERT 9 +#define PAN_BENT_ARMS_SINGLE_SERIF 10 +#define PAN_BENT_ARMS_DOUBLE_SERIF 11 + +#define PAN_LETT_NORMAL_CONTACT 2 +#define PAN_LETT_NORMAL_WEIGHTED 3 +#define PAN_LETT_NORMAL_BOXED 4 +#define PAN_LETT_NORMAL_FLATTENED 5 +#define PAN_LETT_NORMAL_ROUNDED 6 +#define PAN_LETT_NORMAL_OFF_CENTER 7 +#define PAN_LETT_NORMAL_SQUARE 8 +#define PAN_LETT_OBLIQUE_CONTACT 9 +#define PAN_LETT_OBLIQUE_WEIGHTED 10 +#define PAN_LETT_OBLIQUE_BOXED 11 +#define PAN_LETT_OBLIQUE_FLATTENED 12 +#define PAN_LETT_OBLIQUE_ROUNDED 13 +#define PAN_LETT_OBLIQUE_OFF_CENTER 14 +#define PAN_LETT_OBLIQUE_SQUARE 15 + +#define PAN_MIDLINE_STANDARD_TRIMMED 2 +#define PAN_MIDLINE_STANDARD_POINTED 3 +#define PAN_MIDLINE_STANDARD_SERIFED 4 +#define PAN_MIDLINE_HIGH_TRIMMED 5 +#define PAN_MIDLINE_HIGH_POINTED 6 +#define PAN_MIDLINE_HIGH_SERIFED 7 +#define PAN_MIDLINE_CONSTANT_TRIMMED 8 +#define PAN_MIDLINE_CONSTANT_POINTED 9 +#define PAN_MIDLINE_CONSTANT_SERIFED 10 +#define PAN_MIDLINE_LOW_TRIMMED 11 +#define PAN_MIDLINE_LOW_POINTED 12 +#define PAN_MIDLINE_LOW_SERIFED 13 + +#define PAN_XHEIGHT_CONSTANT_SMALL 2 +#define PAN_XHEIGHT_CONSTANT_STD 3 +#define PAN_XHEIGHT_CONSTANT_LARGE 4 +#define PAN_XHEIGHT_DUCKING_SMALL 5 +#define PAN_XHEIGHT_DUCKING_STD 6 +#define PAN_XHEIGHT_DUCKING_LARGE 7 + +#define ELF_VENDOR_SIZE 4 + + typedef struct tagEXTLOGFONTA { + LOGFONTA elfLogFont; + BYTE elfFullName[LF_FULLFACESIZE]; + BYTE elfStyle[LF_FACESIZE]; + DWORD elfVersion; + DWORD elfStyleSize; + DWORD elfMatch; + DWORD elfReserved; + BYTE elfVendorId[ELF_VENDOR_SIZE]; + DWORD elfCulture; + PANOSE elfPanose; + } EXTLOGFONTA,*PEXTLOGFONTA,*NPEXTLOGFONTA,*LPEXTLOGFONTA; + + typedef struct tagEXTLOGFONTW { + LOGFONTW elfLogFont; + WCHAR elfFullName[LF_FULLFACESIZE]; + WCHAR elfStyle[LF_FACESIZE]; + DWORD elfVersion; + DWORD elfStyleSize; + DWORD elfMatch; + DWORD elfReserved; + BYTE elfVendorId[ELF_VENDOR_SIZE]; + DWORD elfCulture; + PANOSE elfPanose; + } EXTLOGFONTW,*PEXTLOGFONTW,*NPEXTLOGFONTW,*LPEXTLOGFONTW; +#ifdef UNICODE + typedef EXTLOGFONTW EXTLOGFONT; + typedef PEXTLOGFONTW PEXTLOGFONT; + typedef NPEXTLOGFONTW NPEXTLOGFONT; + typedef LPEXTLOGFONTW LPEXTLOGFONT; +#else + typedef EXTLOGFONTA EXTLOGFONT; + typedef PEXTLOGFONTA PEXTLOGFONT; + typedef NPEXTLOGFONTA NPEXTLOGFONT; + typedef LPEXTLOGFONTA LPEXTLOGFONT; +#endif + +#define ELF_VERSION 0 +#define ELF_CULTURE_LATIN 0 + +#define RASTER_FONTTYPE 0x0001 +#define DEVICE_FONTTYPE 0x002 +#define TRUETYPE_FONTTYPE 0x004 + +#define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))) +#define PALETTERGB(r,g,b) (0x02000000 | RGB(r,g,b)) +#define PALETTEINDEX(i) ((COLORREF)(0x01000000 | (DWORD)(WORD)(i))) + +#define PC_RESERVED 0x01 +#define PC_EXPLICIT 0x02 +#define PC_NOCOLLAPSE 0x04 + +#define GetRValue(rgb) (LOBYTE(rgb)) +#define GetGValue(rgb) (LOBYTE(((WORD)(rgb)) >> 8)) +#define GetBValue(rgb) (LOBYTE((rgb)>>16)) + +#define TRANSPARENT 1 +#define OPAQUE 2 +#define BKMODE_LAST 2 + +#define GM_COMPATIBLE 1 +#define GM_ADVANCED 2 +#define GM_LAST 2 + +#define PT_CLOSEFIGURE 0x01 +#define PT_LINETO 0x02 +#define PT_BEZIERTO 0x04 +#define PT_MOVETO 0x06 + +#define MM_TEXT 1 +#define MM_LOMETRIC 2 +#define MM_HIMETRIC 3 +#define MM_LOENGLISH 4 +#define MM_HIENGLISH 5 +#define MM_TWIPS 6 +#define MM_ISOTROPIC 7 +#define MM_ANISOTROPIC 8 + +#define MM_MIN MM_TEXT +#define MM_MAX MM_ANISOTROPIC +#define MM_MAX_FIXEDSCALE MM_TWIPS + +#define ABSOLUTE 1 +#define RELATIVE 2 + +#define WHITE_BRUSH 0 +#define LTGRAY_BRUSH 1 +#define GRAY_BRUSH 2 +#define DKGRAY_BRUSH 3 +#define BLACK_BRUSH 4 +#define NULL_BRUSH 5 +#define HOLLOW_BRUSH NULL_BRUSH +#define WHITE_PEN 6 +#define BLACK_PEN 7 +#define NULL_PEN 8 +#define OEM_FIXED_FONT 10 +#define ANSI_FIXED_FONT 11 +#define ANSI_VAR_FONT 12 +#define SYSTEM_FONT 13 +#define DEVICE_DEFAULT_FONT 14 +#define DEFAULT_PALETTE 15 +#define SYSTEM_FIXED_FONT 16 + +#define DEFAULT_GUI_FONT 17 + +#define DC_BRUSH 18 +#define DC_PEN 19 + +#define STOCK_LAST 19 + +#define CLR_INVALID 0xFFFFFFFF + +#define BS_SOLID 0 +#define BS_NULL 1 +#define BS_HOLLOW BS_NULL +#define BS_HATCHED 2 +#define BS_PATTERN 3 +#define BS_INDEXED 4 +#define BS_DIBPATTERN 5 +#define BS_DIBPATTERNPT 6 +#define BS_PATTERN8X8 7 +#define BS_DIBPATTERN8X8 8 +#define BS_MONOPATTERN 9 + +#define HS_HORIZONTAL 0 +#define HS_VERTICAL 1 +#define HS_FDIAGONAL 2 +#define HS_BDIAGONAL 3 +#define HS_CROSS 4 +#define HS_DIAGCROSS 5 + +#define PS_SOLID 0 +#define PS_DASH 1 +#define PS_DOT 2 +#define PS_DASHDOT 3 +#define PS_DASHDOTDOT 4 +#define PS_NULL 5 +#define PS_INSIDEFRAME 6 +#define PS_USERSTYLE 7 +#define PS_ALTERNATE 8 +#define PS_STYLE_MASK 0x0000000F + +#define PS_ENDCAP_ROUND 0x00000000 +#define PS_ENDCAP_SQUARE 0x00000100 +#define PS_ENDCAP_FLAT 0x00000200 +#define PS_ENDCAP_MASK 0x00000F00 + +#define PS_JOIN_ROUND 0x00000000 +#define PS_JOIN_BEVEL 0x00001000 +#define PS_JOIN_MITER 0x00002000 +#define PS_JOIN_MASK 0x0000F000 + +#define PS_COSMETIC 0x00000000 +#define PS_GEOMETRIC 0x00010000 +#define PS_TYPE_MASK 0x000F0000 + +#define AD_COUNTERCLOCKWISE 1 +#define AD_CLOCKWISE 2 + +#define DRIVERVERSION 0 +#define TECHNOLOGY 2 +#define HORZSIZE 4 +#define VERTSIZE 6 +#define HORZRES 8 +#define VERTRES 10 +#define BITSPIXEL 12 +#define PLANES 14 +#define NUMBRUSHES 16 +#define NUMPENS 18 +#define NUMMARKERS 20 +#define NUMFONTS 22 +#define NUMCOLORS 24 +#define PDEVICESIZE 26 +#define CURVECAPS 28 +#define LINECAPS 30 +#define POLYGONALCAPS 32 +#define TEXTCAPS 34 +#define CLIPCAPS 36 +#define RASTERCAPS 38 +#define ASPECTX 40 +#define ASPECTY 42 +#define ASPECTXY 44 + +#define LOGPIXELSX 88 +#define LOGPIXELSY 90 + +#define SIZEPALETTE 104 +#define NUMRESERVED 106 +#define COLORRES 108 + +#define PHYSICALWIDTH 110 +#define PHYSICALHEIGHT 111 +#define PHYSICALOFFSETX 112 +#define PHYSICALOFFSETY 113 +#define SCALINGFACTORX 114 +#define SCALINGFACTORY 115 + +#define VREFRESH 116 + +#define DESKTOPVERTRES 117 + +#define DESKTOPHORZRES 118 + +#define BLTALIGNMENT 119 + +#define SHADEBLENDCAPS 120 +#define COLORMGMTCAPS 121 + +#ifndef NOGDICAPMASKS +#define DT_PLOTTER 0 +#define DT_RASDISPLAY 1 +#define DT_RASPRINTER 2 +#define DT_RASCAMERA 3 +#define DT_CHARSTREAM 4 +#define DT_METAFILE 5 +#define DT_DISPFILE 6 + +#define CC_NONE 0 +#define CC_CIRCLES 1 +#define CC_PIE 2 +#define CC_CHORD 4 +#define CC_ELLIPSES 8 +#define CC_WIDE 16 +#define CC_STYLED 32 +#define CC_WIDESTYLED 64 +#define CC_INTERIORS 128 +#define CC_ROUNDRECT 256 + +#define LC_NONE 0 +#define LC_POLYLINE 2 +#define LC_MARKER 4 +#define LC_POLYMARKER 8 +#define LC_WIDE 16 +#define LC_STYLED 32 +#define LC_WIDESTYLED 64 +#define LC_INTERIORS 128 + +#define PC_NONE 0 +#define PC_POLYGON 1 +#define PC_RECTANGLE 2 +#define PC_WINDPOLYGON 4 +#define PC_TRAPEZOID 4 +#define PC_SCANLINE 8 +#define PC_WIDE 16 +#define PC_STYLED 32 +#define PC_WIDESTYLED 64 +#define PC_INTERIORS 128 +#define PC_POLYPOLYGON 256 +#define PC_PATHS 512 + +#define CP_NONE 0 +#define CP_RECTANGLE 1 +#define CP_REGION 2 + +#define TC_OP_CHARACTER 0x00000001 +#define TC_OP_STROKE 0x00000002 +#define TC_CP_STROKE 0x00000004 +#define TC_CR_90 0x00000008 +#define TC_CR_ANY 0x00000010 +#define TC_SF_X_YINDEP 0x00000020 +#define TC_SA_DOUBLE 0x00000040 +#define TC_SA_INTEGER 0x00000080 +#define TC_SA_CONTIN 0x00000100 +#define TC_EA_DOUBLE 0x00000200 +#define TC_IA_ABLE 0x00000400 +#define TC_UA_ABLE 0x00000800 +#define TC_SO_ABLE 0x00001000 +#define TC_RA_ABLE 0x00002000 +#define TC_VA_ABLE 0x00004000 +#define TC_RESERVED 0x00008000 +#define TC_SCROLLBLT 0x00010000 +#endif + +#define RC_NONE +#define RC_BITBLT 1 +#define RC_BANDING 2 +#define RC_SCALING 4 +#define RC_BITMAP64 8 +#define RC_GDI20_OUTPUT 0x0010 +#define RC_GDI20_STATE 0x0020 +#define RC_SAVEBITMAP 0x0040 +#define RC_DI_BITMAP 0x0080 +#define RC_PALETTE 0x0100 +#define RC_DIBTODEV 0x0200 +#define RC_BIGFONT 0x0400 +#define RC_STRETCHBLT 0x0800 +#define RC_FLOODFILL 0x1000 +#define RC_STRETCHDIB 0x2000 +#define RC_OP_DX_OUTPUT 0x4000 +#define RC_DEVBITS 0x8000 + +#define SB_NONE 0x00000000 +#define SB_CONST_ALPHA 0x00000001 +#define SB_PIXEL_ALPHA 0x00000002 +#define SB_PREMULT_ALPHA 0x00000004 + +#define SB_GRAD_RECT 0x00000010 +#define SB_GRAD_TRI 0x00000020 + +#define CM_NONE 0x00000000 +#define CM_DEVICE_ICM 0x00000001 +#define CM_GAMMA_RAMP 0x00000002 +#define CM_CMYK_COLOR 0x00000004 + +#define DIB_RGB_COLORS 0 +#define DIB_PAL_COLORS 1 + +#define SYSPAL_ERROR 0 +#define SYSPAL_STATIC 1 +#define SYSPAL_NOSTATIC 2 +#define SYSPAL_NOSTATIC256 3 + +#define CBM_INIT 0x04L + +#define FLOODFILLBORDER 0 +#define FLOODFILLSURFACE 1 + +#define CCHDEVICENAME 32 + +#define CCHFORMNAME 32 + + typedef struct _devicemodeA { + BYTE dmDeviceName[CCHDEVICENAME]; + WORD dmSpecVersion; + WORD dmDriverVersion; + WORD dmSize; + WORD dmDriverExtra; + DWORD dmFields; + union { + struct { + short dmOrientation; + short dmPaperSize; + short dmPaperLength; + short dmPaperWidth; + short dmScale; + short dmCopies; + short dmDefaultSource; + short dmPrintQuality; + }; + struct { + POINTL dmPosition; + DWORD dmDisplayOrientation; + DWORD dmDisplayFixedOutput; + }; + }; + short dmColor; + short dmDuplex; + short dmYResolution; + short dmTTOption; + short dmCollate; + BYTE dmFormName[CCHFORMNAME]; + WORD dmLogPixels; + DWORD dmBitsPerPel; + DWORD dmPelsWidth; + DWORD dmPelsHeight; + union { + DWORD dmDisplayFlags; + DWORD dmNup; + }; + DWORD dmDisplayFrequency; + DWORD dmICMMethod; + DWORD dmICMIntent; + DWORD dmMediaType; + DWORD dmDitherType; + DWORD dmReserved1; + DWORD dmReserved2; + DWORD dmPanningWidth; + DWORD dmPanningHeight; + } DEVMODEA,*PDEVMODEA,*NPDEVMODEA,*LPDEVMODEA; + + typedef struct _devicemodeW { + WCHAR dmDeviceName[CCHDEVICENAME]; + WORD dmSpecVersion; + WORD dmDriverVersion; + WORD dmSize; + WORD dmDriverExtra; + DWORD dmFields; + union { + struct { + short dmOrientation; + short dmPaperSize; + short dmPaperLength; + short dmPaperWidth; + short dmScale; + short dmCopies; + short dmDefaultSource; + short dmPrintQuality; + }; + struct { + POINTL dmPosition; + DWORD dmDisplayOrientation; + DWORD dmDisplayFixedOutput; + }; + }; + short dmColor; + short dmDuplex; + short dmYResolution; + short dmTTOption; + short dmCollate; + WCHAR dmFormName[CCHFORMNAME]; + WORD dmLogPixels; + DWORD dmBitsPerPel; + DWORD dmPelsWidth; + DWORD dmPelsHeight; + union { + DWORD dmDisplayFlags; + DWORD dmNup; + }; + DWORD dmDisplayFrequency; + DWORD dmICMMethod; + DWORD dmICMIntent; + DWORD dmMediaType; + DWORD dmDitherType; + DWORD dmReserved1; + DWORD dmReserved2; + DWORD dmPanningWidth; + DWORD dmPanningHeight; + } DEVMODEW,*PDEVMODEW,*NPDEVMODEW,*LPDEVMODEW; +#ifdef UNICODE + typedef DEVMODEW DEVMODE; + typedef PDEVMODEW PDEVMODE; + typedef NPDEVMODEW NPDEVMODE; + typedef LPDEVMODEW LPDEVMODE; +#else + typedef DEVMODEA DEVMODE; + typedef PDEVMODEA PDEVMODE; + typedef NPDEVMODEA NPDEVMODE; + typedef LPDEVMODEA LPDEVMODE; +#endif + +#define DM_SPECVERSION 0x0401 + +#define DM_ORIENTATION 0x00000001L +#define DM_PAPERSIZE 0x00000002L +#define DM_PAPERLENGTH 0x00000004L +#define DM_PAPERWIDTH 0x00000008L +#define DM_SCALE 0x00000010L +#define DM_POSITION 0x00000020L +#define DM_NUP 0x00000040L +#define DM_DISPLAYORIENTATION 0x00000080L +#define DM_COPIES 0x00000100L +#define DM_DEFAULTSOURCE 0x00000200L +#define DM_PRINTQUALITY 0x00000400L +#define DM_COLOR 0x00000800L +#define DM_DUPLEX 0x00001000L +#define DM_YRESOLUTION 0x00002000L +#define DM_TTOPTION 0x00004000L +#define DM_COLLATE 0x00008000L +#define DM_FORMNAME 0x00010000L +#define DM_LOGPIXELS 0x00020000L +#define DM_BITSPERPEL 0x00040000L +#define DM_PELSWIDTH 0x00080000L +#define DM_PELSHEIGHT 0x00100000L +#define DM_DISPLAYFLAGS 0x00200000L +#define DM_DISPLAYFREQUENCY 0x00400000L +#define DM_ICMMETHOD 0x00800000L +#define DM_ICMINTENT 0x01000000L +#define DM_MEDIATYPE 0x02000000L +#define DM_DITHERTYPE 0x04000000L +#define DM_PANNINGWIDTH 0x08000000L +#define DM_PANNINGHEIGHT 0x10000000L +#define DM_DISPLAYFIXEDOUTPUT 0x20000000L + +#define DMORIENT_PORTRAIT 1 +#define DMORIENT_LANDSCAPE 2 + +#define DMPAPER_FIRST DMPAPER_LETTER +#define DMPAPER_LETTER 1 +#define DMPAPER_LETTERSMALL 2 +#define DMPAPER_TABLOID 3 +#define DMPAPER_LEDGER 4 +#define DMPAPER_LEGAL 5 +#define DMPAPER_STATEMENT 6 +#define DMPAPER_EXECUTIVE 7 +#define DMPAPER_A3 8 +#define DMPAPER_A4 9 +#define DMPAPER_A4SMALL 10 +#define DMPAPER_A5 11 +#define DMPAPER_B4 12 +#define DMPAPER_B5 13 +#define DMPAPER_FOLIO 14 +#define DMPAPER_QUARTO 15 +#define DMPAPER_10X14 16 +#define DMPAPER_11X17 17 +#define DMPAPER_NOTE 18 +#define DMPAPER_ENV_9 19 +#define DMPAPER_ENV_10 20 +#define DMPAPER_ENV_11 21 +#define DMPAPER_ENV_12 22 +#define DMPAPER_ENV_14 23 +#define DMPAPER_CSHEET 24 +#define DMPAPER_DSHEET 25 +#define DMPAPER_ESHEET 26 +#define DMPAPER_ENV_DL 27 +#define DMPAPER_ENV_C5 28 +#define DMPAPER_ENV_C3 29 +#define DMPAPER_ENV_C4 30 +#define DMPAPER_ENV_C6 31 +#define DMPAPER_ENV_C65 32 +#define DMPAPER_ENV_B4 33 +#define DMPAPER_ENV_B5 34 +#define DMPAPER_ENV_B6 35 +#define DMPAPER_ENV_ITALY 36 +#define DMPAPER_ENV_MONARCH 37 +#define DMPAPER_ENV_PERSONAL 38 +#define DMPAPER_FANFOLD_US 39 +#define DMPAPER_FANFOLD_STD_GERMAN 40 +#define DMPAPER_FANFOLD_LGL_GERMAN 41 +#define DMPAPER_ISO_B4 42 +#define DMPAPER_JAPANESE_POSTCARD 43 +#define DMPAPER_9X11 44 +#define DMPAPER_10X11 45 +#define DMPAPER_15X11 46 +#define DMPAPER_ENV_INVITE 47 +#define DMPAPER_RESERVED_48 48 +#define DMPAPER_RESERVED_49 49 +#define DMPAPER_LETTER_EXTRA 50 +#define DMPAPER_LEGAL_EXTRA 51 +#define DMPAPER_TABLOID_EXTRA 52 +#define DMPAPER_A4_EXTRA 53 +#define DMPAPER_LETTER_TRANSVERSE 54 +#define DMPAPER_A4_TRANSVERSE 55 +#define DMPAPER_LETTER_EXTRA_TRANSVERSE 56 +#define DMPAPER_A_PLUS 57 +#define DMPAPER_B_PLUS 58 +#define DMPAPER_LETTER_PLUS 59 +#define DMPAPER_A4_PLUS 60 +#define DMPAPER_A5_TRANSVERSE 61 +#define DMPAPER_B5_TRANSVERSE 62 +#define DMPAPER_A3_EXTRA 63 +#define DMPAPER_A5_EXTRA 64 +#define DMPAPER_B5_EXTRA 65 +#define DMPAPER_A2 66 +#define DMPAPER_A3_TRANSVERSE 67 +#define DMPAPER_A3_EXTRA_TRANSVERSE 68 +#define DMPAPER_DBL_JAPANESE_POSTCARD 69 +#define DMPAPER_A6 70 +#define DMPAPER_JENV_KAKU2 71 +#define DMPAPER_JENV_KAKU3 72 +#define DMPAPER_JENV_CHOU3 73 +#define DMPAPER_JENV_CHOU4 74 +#define DMPAPER_LETTER_ROTATED 75 +#define DMPAPER_A3_ROTATED 76 +#define DMPAPER_A4_ROTATED 77 +#define DMPAPER_A5_ROTATED 78 +#define DMPAPER_B4_JIS_ROTATED 79 +#define DMPAPER_B5_JIS_ROTATED 80 +#define DMPAPER_JAPANESE_POSTCARD_ROTATED 81 +#define DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED 82 +#define DMPAPER_A6_ROTATED 83 +#define DMPAPER_JENV_KAKU2_ROTATED 84 +#define DMPAPER_JENV_KAKU3_ROTATED 85 +#define DMPAPER_JENV_CHOU3_ROTATED 86 +#define DMPAPER_JENV_CHOU4_ROTATED 87 +#define DMPAPER_B6_JIS 88 +#define DMPAPER_B6_JIS_ROTATED 89 +#define DMPAPER_12X11 90 +#define DMPAPER_JENV_YOU4 91 +#define DMPAPER_JENV_YOU4_ROTATED 92 +#define DMPAPER_P16K 93 +#define DMPAPER_P32K 94 +#define DMPAPER_P32KBIG 95 +#define DMPAPER_PENV_1 96 +#define DMPAPER_PENV_2 97 +#define DMPAPER_PENV_3 98 +#define DMPAPER_PENV_4 99 +#define DMPAPER_PENV_5 100 +#define DMPAPER_PENV_6 101 +#define DMPAPER_PENV_7 102 +#define DMPAPER_PENV_8 103 +#define DMPAPER_PENV_9 104 +#define DMPAPER_PENV_10 105 +#define DMPAPER_P16K_ROTATED 106 +#define DMPAPER_P32K_ROTATED 107 +#define DMPAPER_P32KBIG_ROTATED 108 +#define DMPAPER_PENV_1_ROTATED 109 +#define DMPAPER_PENV_2_ROTATED 110 +#define DMPAPER_PENV_3_ROTATED 111 +#define DMPAPER_PENV_4_ROTATED 112 +#define DMPAPER_PENV_5_ROTATED 113 +#define DMPAPER_PENV_6_ROTATED 114 +#define DMPAPER_PENV_7_ROTATED 115 +#define DMPAPER_PENV_8_ROTATED 116 +#define DMPAPER_PENV_9_ROTATED 117 +#define DMPAPER_PENV_10_ROTATED 118 + +#define DMPAPER_LAST DMPAPER_PENV_10_ROTATED + +#define DMPAPER_USER 256 + +#define DMBIN_FIRST DMBIN_UPPER +#define DMBIN_UPPER 1 +#define DMBIN_ONLYONE 1 +#define DMBIN_LOWER 2 +#define DMBIN_MIDDLE 3 +#define DMBIN_MANUAL 4 +#define DMBIN_ENVELOPE 5 +#define DMBIN_ENVMANUAL 6 +#define DMBIN_AUTO 7 +#define DMBIN_TRACTOR 8 +#define DMBIN_SMALLFMT 9 +#define DMBIN_LARGEFMT 10 +#define DMBIN_LARGECAPACITY 11 +#define DMBIN_CASSETTE 14 +#define DMBIN_FORMSOURCE 15 +#define DMBIN_LAST DMBIN_FORMSOURCE + +#define DMBIN_USER 256 + +#define DMRES_DRAFT (-1) +#define DMRES_LOW (-2) +#define DMRES_MEDIUM (-3) +#define DMRES_HIGH (-4) + +#define DMCOLOR_MONOCHROME 1 +#define DMCOLOR_COLOR 2 + +#define DMDUP_SIMPLEX 1 +#define DMDUP_VERTICAL 2 +#define DMDUP_HORIZONTAL 3 + +#define DMTT_BITMAP 1 +#define DMTT_DOWNLOAD 2 +#define DMTT_SUBDEV 3 +#define DMTT_DOWNLOAD_OUTLINE 4 + +#define DMCOLLATE_FALSE 0 +#define DMCOLLATE_TRUE 1 + +#define DMDO_DEFAULT 0 +#define DMDO_90 1 +#define DMDO_180 2 +#define DMDO_270 3 + +#define DMDFO_DEFAULT 0 +#define DMDFO_STRETCH 1 +#define DMDFO_CENTER 2 + +#define DMDISPLAYFLAGS_TEXTMODE 0x00000004 + +#define DMNUP_SYSTEM 1 +#define DMNUP_ONEUP 2 + +#define DMICMMETHOD_NONE 1 +#define DMICMMETHOD_SYSTEM 2 +#define DMICMMETHOD_DRIVER 3 +#define DMICMMETHOD_DEVICE 4 + +#define DMICMMETHOD_USER 256 + +#define DMICM_SATURATE 1 +#define DMICM_CONTRAST 2 +#define DMICM_COLORIMETRIC 3 +#define DMICM_ABS_COLORIMETRIC 4 + +#define DMICM_USER 256 + +#define DMMEDIA_STANDARD 1 +#define DMMEDIA_TRANSPARENCY 2 +#define DMMEDIA_GLOSSY 3 + +#define DMMEDIA_USER 256 + +#define DMDITHER_NONE 1 +#define DMDITHER_COARSE 2 +#define DMDITHER_FINE 3 +#define DMDITHER_LINEART 4 +#define DMDITHER_ERRORDIFFUSION 5 +#define DMDITHER_RESERVED6 6 +#define DMDITHER_RESERVED7 7 +#define DMDITHER_RESERVED8 8 +#define DMDITHER_RESERVED9 9 +#define DMDITHER_GRAYSCALE 10 + +#define DMDITHER_USER 256 + + typedef struct _DISPLAY_DEVICEA { + DWORD cb; + CHAR DeviceName[32]; + CHAR DeviceString[128]; + DWORD StateFlags; + CHAR DeviceID[128]; + CHAR DeviceKey[128]; + } DISPLAY_DEVICEA,*PDISPLAY_DEVICEA,*LPDISPLAY_DEVICEA; + typedef struct _DISPLAY_DEVICEW { + DWORD cb; + WCHAR DeviceName[32]; + WCHAR DeviceString[128]; + DWORD StateFlags; + WCHAR DeviceID[128]; + WCHAR DeviceKey[128]; + } DISPLAY_DEVICEW,*PDISPLAY_DEVICEW,*LPDISPLAY_DEVICEW; +#ifdef UNICODE + typedef DISPLAY_DEVICEW DISPLAY_DEVICE; + typedef PDISPLAY_DEVICEW PDISPLAY_DEVICE; + typedef LPDISPLAY_DEVICEW LPDISPLAY_DEVICE; +#else + typedef DISPLAY_DEVICEA DISPLAY_DEVICE; + typedef PDISPLAY_DEVICEA PDISPLAY_DEVICE; + typedef LPDISPLAY_DEVICEA LPDISPLAY_DEVICE; +#endif + +#define DISPLAY_DEVICE_ATTACHED_TO_DESKTOP 0x00000001 +#define DISPLAY_DEVICE_MULTI_DRIVER 0x00000002 +#define DISPLAY_DEVICE_PRIMARY_DEVICE 0x00000004 +#define DISPLAY_DEVICE_MIRRORING_DRIVER 0x00000008 +#define DISPLAY_DEVICE_VGA_COMPATIBLE 0x00000010 +#define DISPLAY_DEVICE_REMOVABLE 0x00000020 +#define DISPLAY_DEVICE_MODESPRUNED 0x08000000 +#define DISPLAY_DEVICE_REMOTE 0x04000000 +#define DISPLAY_DEVICE_DISCONNECT 0x02000000 + +#define DISPLAY_DEVICE_ACTIVE 0x00000001 +#define DISPLAY_DEVICE_ATTACHED 0x00000002 + +#define RDH_RECTANGLES 1 + + typedef struct _RGNDATAHEADER { + DWORD dwSize; + DWORD iType; + DWORD nCount; + DWORD nRgnSize; + RECT rcBound; + } RGNDATAHEADER,*PRGNDATAHEADER; + + typedef struct _RGNDATA { + RGNDATAHEADER rdh; + char Buffer[1]; + } RGNDATA,*PRGNDATA,*NPRGNDATA,*LPRGNDATA; + +#define SYSRGN 4 + + typedef struct _ABC { + int abcA; + UINT abcB; + int abcC; + } ABC,*PABC,*NPABC,*LPABC; + + typedef struct _ABCFLOAT { + FLOAT abcfA; + FLOAT abcfB; + FLOAT abcfC; + } ABCFLOAT,*PABCFLOAT,*NPABCFLOAT,*LPABCFLOAT; + +#ifndef NOTEXTMETRIC + + typedef struct _OUTLINETEXTMETRICA { + UINT otmSize; + TEXTMETRICA otmTextMetrics; + BYTE otmFiller; + PANOSE otmPanoseNumber; + UINT otmfsSelection; + UINT otmfsType; + int otmsCharSlopeRise; + int otmsCharSlopeRun; + int otmItalicAngle; + UINT otmEMSquare; + int otmAscent; + int otmDescent; + UINT otmLineGap; + UINT otmsCapEmHeight; + UINT otmsXHeight; + RECT otmrcFontBox; + int otmMacAscent; + int otmMacDescent; + UINT otmMacLineGap; + UINT otmusMinimumPPEM; + POINT otmptSubscriptSize; + POINT otmptSubscriptOffset; + POINT otmptSuperscriptSize; + POINT otmptSuperscriptOffset; + UINT otmsStrikeoutSize; + int otmsStrikeoutPosition; + int otmsUnderscoreSize; + int otmsUnderscorePosition; + PSTR otmpFamilyName; + PSTR otmpFaceName; + PSTR otmpStyleName; + PSTR otmpFullName; + } OUTLINETEXTMETRICA,*POUTLINETEXTMETRICA,*NPOUTLINETEXTMETRICA,*LPOUTLINETEXTMETRICA; + + typedef struct _OUTLINETEXTMETRICW { + UINT otmSize; + TEXTMETRICW otmTextMetrics; + BYTE otmFiller; + PANOSE otmPanoseNumber; + UINT otmfsSelection; + UINT otmfsType; + int otmsCharSlopeRise; + int otmsCharSlopeRun; + int otmItalicAngle; + UINT otmEMSquare; + int otmAscent; + int otmDescent; + UINT otmLineGap; + UINT otmsCapEmHeight; + UINT otmsXHeight; + RECT otmrcFontBox; + int otmMacAscent; + int otmMacDescent; + UINT otmMacLineGap; + UINT otmusMinimumPPEM; + POINT otmptSubscriptSize; + POINT otmptSubscriptOffset; + POINT otmptSuperscriptSize; + POINT otmptSuperscriptOffset; + UINT otmsStrikeoutSize; + int otmsStrikeoutPosition; + int otmsUnderscoreSize; + int otmsUnderscorePosition; + PSTR otmpFamilyName; + PSTR otmpFaceName; + PSTR otmpStyleName; + PSTR otmpFullName; + } OUTLINETEXTMETRICW,*POUTLINETEXTMETRICW,*NPOUTLINETEXTMETRICW,*LPOUTLINETEXTMETRICW; +#ifdef UNICODE + typedef OUTLINETEXTMETRICW OUTLINETEXTMETRIC; + typedef POUTLINETEXTMETRICW POUTLINETEXTMETRIC; + typedef NPOUTLINETEXTMETRICW NPOUTLINETEXTMETRIC; + typedef LPOUTLINETEXTMETRICW LPOUTLINETEXTMETRIC; +#else + typedef OUTLINETEXTMETRICA OUTLINETEXTMETRIC; + typedef POUTLINETEXTMETRICA POUTLINETEXTMETRIC; + typedef NPOUTLINETEXTMETRICA NPOUTLINETEXTMETRIC; + typedef LPOUTLINETEXTMETRICA LPOUTLINETEXTMETRIC; +#endif +#endif + + typedef struct tagPOLYTEXTA { + int x; + int y; + UINT n; + LPCSTR lpstr; + UINT uiFlags; + RECT rcl; + int *pdx; + } POLYTEXTA,*PPOLYTEXTA,*NPPOLYTEXTA,*LPPOLYTEXTA; + + typedef struct tagPOLYTEXTW { + int x; + int y; + UINT n; + LPCWSTR lpstr; + UINT uiFlags; + RECT rcl; + int *pdx; + } POLYTEXTW,*PPOLYTEXTW,*NPPOLYTEXTW,*LPPOLYTEXTW; +#ifdef UNICODE + typedef POLYTEXTW POLYTEXT; + typedef PPOLYTEXTW PPOLYTEXT; + typedef NPPOLYTEXTW NPPOLYTEXT; + typedef LPPOLYTEXTW LPPOLYTEXT; +#else + typedef POLYTEXTA POLYTEXT; + typedef PPOLYTEXTA PPOLYTEXT; + typedef NPPOLYTEXTA NPPOLYTEXT; + typedef LPPOLYTEXTA LPPOLYTEXT; +#endif + + typedef struct _FIXED { + WORD fract; + short value; + } FIXED; + + typedef struct _MAT2 { + FIXED eM11; + FIXED eM12; + FIXED eM21; + FIXED eM22; + } MAT2,*LPMAT2; + + typedef struct _GLYPHMETRICS { + UINT gmBlackBoxX; + UINT gmBlackBoxY; + POINT gmptGlyphOrigin; + short gmCellIncX; + short gmCellIncY; + } GLYPHMETRICS,*LPGLYPHMETRICS; + +#define GGO_METRICS 0 +#define GGO_BITMAP 1 +#define GGO_NATIVE 2 +#define GGO_BEZIER 3 + +#define GGO_GRAY2_BITMAP 4 +#define GGO_GRAY4_BITMAP 5 +#define GGO_GRAY8_BITMAP 6 +#define GGO_GLYPH_INDEX 0x0080 +#define GGO_UNHINTED 0x0100 + +#define TT_POLYGON_TYPE 24 + +#define TT_PRIM_LINE 1 +#define TT_PRIM_QSPLINE 2 +#define TT_PRIM_CSPLINE 3 + + typedef struct tagPOINTFX { + FIXED x; + FIXED y; + } POINTFX,*LPPOINTFX; + + typedef struct tagTTPOLYCURVE { + WORD wType; + WORD cpfx; + POINTFX apfx[1]; + } TTPOLYCURVE,*LPTTPOLYCURVE; + + typedef struct tagTTPOLYGONHEADER { + DWORD cb; + DWORD dwType; + POINTFX pfxStart; + } TTPOLYGONHEADER,*LPTTPOLYGONHEADER; + +#define GCP_DBCS 0x0001 +#define GCP_REORDER 0x0002 +#define GCP_USEKERNING 0x0008 +#define GCP_GLYPHSHAPE 0x0010 +#define GCP_LIGATE 0x0020 + +#define GCP_DIACRITIC 0x0100 +#define GCP_KASHIDA 0x0400 +#define GCP_ERROR 0x8000 +#define FLI_MASK 0x103B + +#define GCP_JUSTIFY 0x00010000L + +#define FLI_GLYPHS 0x00040000L +#define GCP_CLASSIN 0x00080000L +#define GCP_MAXEXTENT 0x00100000L +#define GCP_JUSTIFYIN 0x00200000L +#define GCP_DISPLAYZWG 0x00400000L +#define GCP_SYMSWAPOFF 0x00800000L +#define GCP_NUMERICOVERRIDE 0x01000000L +#define GCP_NEUTRALOVERRIDE 0x02000000L +#define GCP_NUMERICSLATIN 0x04000000L +#define GCP_NUMERICSLOCAL 0x08000000L + +#define GCPCLASS_LATIN 1 +#define GCPCLASS_HEBREW 2 +#define GCPCLASS_ARABIC 2 +#define GCPCLASS_NEUTRAL 3 +#define GCPCLASS_LOCALNUMBER 4 +#define GCPCLASS_LATINNUMBER 5 +#define GCPCLASS_LATINNUMERICTERMINATOR 6 +#define GCPCLASS_LATINNUMERICSEPARATOR 7 +#define GCPCLASS_NUMERICSEPARATOR 8 +#define GCPCLASS_PREBOUNDLTR 0x80 +#define GCPCLASS_PREBOUNDRTL 0x40 +#define GCPCLASS_POSTBOUNDLTR 0x20 +#define GCPCLASS_POSTBOUNDRTL 0x10 + +#define GCPGLYPH_LINKBEFORE 0x8000 +#define GCPGLYPH_LINKAFTER 0x4000 + + typedef struct tagGCP_RESULTSA { + DWORD lStructSize; + LPSTR lpOutString; + UINT *lpOrder; + int *lpDx; + int *lpCaretPos; + LPSTR lpClass; + LPWSTR lpGlyphs; + UINT nGlyphs; + int nMaxFit; + } GCP_RESULTSA,*LPGCP_RESULTSA; + typedef struct tagGCP_RESULTSW { + DWORD lStructSize; + LPWSTR lpOutString; + UINT *lpOrder; + int *lpDx; + int *lpCaretPos; + LPSTR lpClass; + LPWSTR lpGlyphs; + UINT nGlyphs; + int nMaxFit; + } GCP_RESULTSW,*LPGCP_RESULTSW; +#ifdef UNICODE + typedef GCP_RESULTSW GCP_RESULTS; + typedef LPGCP_RESULTSW LPGCP_RESULTS; +#else + typedef GCP_RESULTSA GCP_RESULTS; + typedef LPGCP_RESULTSA LPGCP_RESULTS; +#endif + + typedef struct _RASTERIZER_STATUS { + short nSize; + short wFlags; + short nLanguageID; + } RASTERIZER_STATUS,*LPRASTERIZER_STATUS; + +#define TT_AVAILABLE 0x0001 +#define TT_ENABLED 0x0002 + + typedef struct tagPIXELFORMATDESCRIPTOR { + WORD nSize; + WORD nVersion; + DWORD dwFlags; + BYTE iPixelType; + BYTE cColorBits; + BYTE cRedBits; + BYTE cRedShift; + BYTE cGreenBits; + BYTE cGreenShift; + BYTE cBlueBits; + BYTE cBlueShift; + BYTE cAlphaBits; + BYTE cAlphaShift; + BYTE cAccumBits; + BYTE cAccumRedBits; + BYTE cAccumGreenBits; + BYTE cAccumBlueBits; + BYTE cAccumAlphaBits; + BYTE cDepthBits; + BYTE cStencilBits; + BYTE cAuxBuffers; + BYTE iLayerType; + BYTE bReserved; + DWORD dwLayerMask; + DWORD dwVisibleMask; + DWORD dwDamageMask; + } PIXELFORMATDESCRIPTOR,*PPIXELFORMATDESCRIPTOR,*LPPIXELFORMATDESCRIPTOR; + +#define PFD_TYPE_RGBA 0 +#define PFD_TYPE_COLORINDEX 1 + +#define PFD_MAIN_PLANE 0 +#define PFD_OVERLAY_PLANE 1 +#define PFD_UNDERLAY_PLANE (-1) + +#define PFD_DOUBLEBUFFER 0x00000001 +#define PFD_STEREO 0x00000002 +#define PFD_DRAW_TO_WINDOW 0x00000004 +#define PFD_DRAW_TO_BITMAP 0x00000008 +#define PFD_SUPPORT_GDI 0x00000010 +#define PFD_SUPPORT_OPENGL 0x00000020 +#define PFD_GENERIC_FORMAT 0x00000040 +#define PFD_NEED_PALETTE 0x00000080 +#define PFD_NEED_SYSTEM_PALETTE 0x00000100 +#define PFD_SWAP_EXCHANGE 0x00000200 +#define PFD_SWAP_COPY 0x00000400 +#define PFD_SWAP_LAYER_BUFFERS 0x00000800 +#define PFD_GENERIC_ACCELERATED 0x00001000 +#define PFD_SUPPORT_DIRECTDRAW 0x00002000 + +#define PFD_DEPTH_DONTCARE 0x20000000 +#define PFD_DOUBLEBUFFER_DONTCARE 0x40000000 +#define PFD_STEREO_DONTCARE 0x80000000 + +#ifndef NOTEXTMETRIC + typedef int (CALLBACK *OLDFONTENUMPROCA)(CONST LOGFONTA *,CONST TEXTMETRICA *,DWORD,LPARAM); + typedef int (CALLBACK *OLDFONTENUMPROCW)(CONST LOGFONTW *,CONST TEXTMETRICW *,DWORD,LPARAM); +#ifdef UNICODE +#define OLDFONTENUMPROC OLDFONTENUMPROCW +#else +#define OLDFONTENUMPROC OLDFONTENUMPROCA +#endif +#else + typedef int (CALLBACK *OLDFONTENUMPROCA)(CONST LOGFONTA *,CONST VOID *,DWORD,LPARAM); + typedef int (CALLBACK *OLDFONTENUMPROCW)(CONST LOGFONTW *,CONST VOID *,DWORD,LPARAM); +#ifdef UNICODE +#define OLDFONTENUMPROC OLDFONTENUMPROCW +#else +#define OLDFONTENUMPROC OLDFONTENUMPROCA +#endif +#endif + + typedef OLDFONTENUMPROCA FONTENUMPROCA; + typedef OLDFONTENUMPROCW FONTENUMPROCW; +#ifdef UNICODE + typedef FONTENUMPROCW FONTENUMPROC; +#else + typedef FONTENUMPROCA FONTENUMPROC; +#endif + + typedef int (CALLBACK *GOBJENUMPROC)(LPVOID,LPARAM); + typedef VOID (CALLBACK *LINEDDAPROC)(int,int,LPARAM); + +#ifdef UNICODE +#define AddFontResource AddFontResourceW +#define CopyMetaFile CopyMetaFileW +#define CreateDC CreateDCW +#define CreateFontIndirect CreateFontIndirectW +#define CreateFont CreateFontW +#define CreateIC CreateICW +#define CreateMetaFile CreateMetaFileW +#define CreateScalableFontResource CreateScalableFontResourceW +#else +#define AddFontResource AddFontResourceA +#define CopyMetaFile CopyMetaFileA +#define CreateDC CreateDCA +#define CreateFontIndirect CreateFontIndirectA +#define CreateFont CreateFontA +#define CreateIC CreateICA +#define CreateMetaFile CreateMetaFileA +#define CreateScalableFontResource CreateScalableFontResourceA +#endif + + WINGDIAPI int WINAPI AddFontResourceA(LPCSTR); + WINGDIAPI int WINAPI AddFontResourceW(LPCWSTR); + WINGDIAPI WINBOOL WINAPI AnimatePalette(HPALETTE hPal,UINT iStartIndex,UINT cEntries,CONST PALETTEENTRY *ppe); + WINGDIAPI WINBOOL WINAPI Arc(HDC hdc,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4); + WINGDIAPI WINBOOL WINAPI BitBlt(HDC hdc,int x,int y,int cx,int cy,HDC hdcSrc,int x1,int y1,DWORD rop); + WINGDIAPI WINBOOL WINAPI CancelDC(HDC hdc); + WINGDIAPI WINBOOL WINAPI Chord(HDC hdc,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4); + WINGDIAPI int WINAPI ChoosePixelFormat(HDC hdc,CONST PIXELFORMATDESCRIPTOR *ppfd); + WINGDIAPI HMETAFILE WINAPI CloseMetaFile(HDC hdc); + WINGDIAPI int WINAPI CombineRgn(HRGN hrgnDst,HRGN hrgnSrc1,HRGN hrgnSrc2,int iMode); + WINGDIAPI HMETAFILE WINAPI CopyMetaFileA(HMETAFILE,LPCSTR); + WINGDIAPI HMETAFILE WINAPI CopyMetaFileW(HMETAFILE,LPCWSTR); + WINGDIAPI HBITMAP WINAPI CreateBitmap(int nWidth,int nHeight,UINT nPlanes,UINT nBitCount,CONST VOID *lpBits); + WINGDIAPI HBITMAP WINAPI CreateBitmapIndirect(CONST BITMAP *pbm); + WINGDIAPI HBRUSH WINAPI CreateBrushIndirect(CONST LOGBRUSH *plbrush); + WINGDIAPI HBITMAP WINAPI CreateCompatibleBitmap(HDC hdc,int cx,int cy); + WINGDIAPI HBITMAP WINAPI CreateDiscardableBitmap(HDC hdc,int cx,int cy); + WINGDIAPI HDC WINAPI CreateCompatibleDC(HDC hdc); + WINGDIAPI HDC WINAPI CreateDCA(LPCSTR pwszDriver,LPCSTR pwszDevice,LPCSTR pszPort,CONST DEVMODEA *pdm); + WINGDIAPI HDC WINAPI CreateDCW(LPCWSTR pwszDriver,LPCWSTR pwszDevice,LPCWSTR pszPort,CONST DEVMODEW *pdm); + WINGDIAPI HBITMAP WINAPI CreateDIBitmap(HDC hdc,CONST BITMAPINFOHEADER *pbmih,DWORD flInit,CONST VOID *pjBits,CONST BITMAPINFO *pbmi,UINT iUsage); + WINGDIAPI HBRUSH WINAPI CreateDIBPatternBrush(HGLOBAL h,UINT iUsage); + WINGDIAPI HBRUSH WINAPI CreateDIBPatternBrushPt(CONST VOID *lpPackedDIB,UINT iUsage); + WINGDIAPI HRGN WINAPI CreateEllipticRgn(int x1,int y1,int x2,int y2); + WINGDIAPI HRGN WINAPI CreateEllipticRgnIndirect(CONST RECT *lprect); + WINGDIAPI HFONT WINAPI CreateFontIndirectA(CONST LOGFONTA *lplf); + WINGDIAPI HFONT WINAPI CreateFontIndirectW(CONST LOGFONTW *lplf); + WINGDIAPI HFONT WINAPI CreateFontA(int cHeight,int cWidth,int cEscapement,int cOrientation,int cWeight,DWORD bItalic,DWORD bUnderline,DWORD bStrikeOut,DWORD iCharSet,DWORD iOutPrecision,DWORD iClipPrecision,DWORD iQuality,DWORD iPitchAndFamily,LPCSTR pszFaceName); + WINGDIAPI HFONT WINAPI CreateFontW(int cHeight,int cWidth,int cEscapement,int cOrientation,int cWeight,DWORD bItalic,DWORD bUnderline,DWORD bStrikeOut,DWORD iCharSet,DWORD iOutPrecision,DWORD iClipPrecision,DWORD iQuality,DWORD iPitchAndFamily,LPCWSTR pszFaceName); + WINGDIAPI HBRUSH WINAPI CreateHatchBrush(int iHatch,COLORREF color); + WINGDIAPI HDC WINAPI CreateICA(LPCSTR pszDriver,LPCSTR pszDevice,LPCSTR pszPort,CONST DEVMODEA *pdm); + WINGDIAPI HDC WINAPI CreateICW(LPCWSTR pszDriver,LPCWSTR pszDevice,LPCWSTR pszPort,CONST DEVMODEW *pdm); + WINGDIAPI HDC WINAPI CreateMetaFileA(LPCSTR pszFile); + WINGDIAPI HDC WINAPI CreateMetaFileW(LPCWSTR pszFile); + WINGDIAPI HPALETTE WINAPI CreatePalette(CONST LOGPALETTE *plpal); + WINGDIAPI HPEN WINAPI CreatePen(int iStyle,int cWidth,COLORREF color); + WINGDIAPI HPEN WINAPI CreatePenIndirect(CONST LOGPEN *plpen); + WINGDIAPI HRGN WINAPI CreatePolyPolygonRgn(CONST POINT *pptl,CONST INT *pc,int cPoly,int iMode); + WINGDIAPI HBRUSH WINAPI CreatePatternBrush(HBITMAP hbm); + WINGDIAPI HRGN WINAPI CreateRectRgn(int x1,int y1,int x2,int y2); + WINGDIAPI HRGN WINAPI CreateRectRgnIndirect(CONST RECT *lprect); + WINGDIAPI HRGN WINAPI CreateRoundRectRgn(int x1,int y1,int x2,int y2,int w,int h); + WINGDIAPI WINBOOL WINAPI CreateScalableFontResourceA(DWORD fdwHidden,LPCSTR lpszFont,LPCSTR lpszFile,LPCSTR lpszPath); + WINGDIAPI WINBOOL WINAPI CreateScalableFontResourceW(DWORD fdwHidden,LPCWSTR lpszFont,LPCWSTR lpszFile,LPCWSTR lpszPath); + WINGDIAPI HBRUSH WINAPI CreateSolidBrush(COLORREF color); + WINGDIAPI WINBOOL WINAPI DeleteDC(HDC hdc); + WINGDIAPI WINBOOL WINAPI DeleteMetaFile(HMETAFILE hmf); + WINGDIAPI WINBOOL WINAPI DeleteObject(HGDIOBJ ho); + WINGDIAPI int WINAPI DescribePixelFormat(HDC hdc,int iPixelFormat,UINT nBytes,LPPIXELFORMATDESCRIPTOR ppfd); + + typedef UINT (CALLBACK *LPFNDEVMODE)(HWND,HMODULE,LPDEVMODE,LPSTR,LPSTR,LPDEVMODE,LPSTR,UINT); + typedef DWORD (CALLBACK *LPFNDEVCAPS)(LPSTR,LPSTR,UINT,LPSTR,LPDEVMODE); + +#define DM_UPDATE 1 +#define DM_COPY 2 +#define DM_PROMPT 4 +#define DM_MODIFY 8 + +#define DM_IN_BUFFER DM_MODIFY +#define DM_IN_PROMPT DM_PROMPT +#define DM_OUT_BUFFER DM_COPY +#define DM_OUT_DEFAULT DM_UPDATE + +#define DC_FIELDS 1 +#define DC_PAPERS 2 +#define DC_PAPERSIZE 3 +#define DC_MINEXTENT 4 +#define DC_MAXEXTENT 5 +#define DC_BINS 6 +#define DC_DUPLEX 7 +#define DC_SIZE 8 +#define DC_EXTRA 9 +#define DC_VERSION 10 +#define DC_DRIVER 11 +#define DC_BINNAMES 12 +#define DC_ENUMRESOLUTIONS 13 +#define DC_FILEDEPENDENCIES 14 +#define DC_TRUETYPE 15 +#define DC_PAPERNAMES 16 +#define DC_ORIENTATION 17 +#define DC_COPIES 18 +#define DC_BINADJUST 19 +#define DC_EMF_COMPLIANT 20 +#define DC_DATATYPE_PRODUCED 21 +#define DC_COLLATE 22 +#define DC_MANUFACTURER 23 +#define DC_MODEL 24 +#define DC_PERSONALITY 25 +#define DC_PRINTRATE 26 +#define DC_PRINTRATEUNIT 27 +#define PRINTRATEUNIT_PPM 1 +#define PRINTRATEUNIT_CPS 2 +#define PRINTRATEUNIT_LPM 3 +#define PRINTRATEUNIT_IPM 4 +#define DC_PRINTERMEM 28 +#define DC_MEDIAREADY 29 +#define DC_STAPLE 30 +#define DC_PRINTRATEPPM 31 +#define DC_COLORDEVICE 32 +#define DC_NUP 33 +#define DC_MEDIATYPENAMES 34 +#define DC_MEDIATYPES 35 + +#define DCTT_BITMAP 0x0000001L +#define DCTT_DOWNLOAD 0x0000002L +#define DCTT_SUBDEV 0x0000004L +#define DCTT_DOWNLOAD_OUTLINE 0x0000008L + +#define DCBA_FACEUPNONE 0x0000 +#define DCBA_FACEUPCENTER 0x0001 +#define DCBA_FACEUPLEFT 0x0002 +#define DCBA_FACEUPRIGHT 0x0003 +#define DCBA_FACEDOWNNONE 0x0100 +#define DCBA_FACEDOWNCENTER 0x0101 +#define DCBA_FACEDOWNLEFT 0x0102 +#define DCBA_FACEDOWNRIGHT 0x0103 + +#ifdef UNICODE +#define DeviceCapabilities DeviceCapabilitiesW +#define EnumFontFamiliesEx EnumFontFamiliesExW +#define EnumFontFamilies EnumFontFamiliesW +#define EnumFonts EnumFontsW +#define GetCharWidth GetCharWidthW +#define GetCharWidth32 GetCharWidth32W +#define GetCharWidthFloat GetCharWidthFloatW +#define GetCharABCWidths GetCharABCWidthsW +#define GetCharABCWidthsFloat GetCharABCWidthsFloatW +#define GetGlyphOutline GetGlyphOutlineW +#define GetMetaFile GetMetaFileW +#else +#define DeviceCapabilities DeviceCapabilitiesA +#define EnumFontFamiliesEx EnumFontFamiliesExA +#define EnumFontFamilies EnumFontFamiliesA +#define EnumFonts EnumFontsA +#define GetCharWidth GetCharWidthA +#define GetCharWidth32 GetCharWidth32A +#define GetCharWidthFloat GetCharWidthFloatA +#define GetCharABCWidths GetCharABCWidthsA +#define GetCharABCWidthsFloat GetCharABCWidthsFloatA +#define GetGlyphOutline GetGlyphOutlineA +#define GetMetaFile GetMetaFileA +#endif + + WINSPOOLAPI int WINAPI DeviceCapabilitiesA(LPCSTR pDevice,LPCSTR pPort,WORD fwCapability,LPSTR pOutput,CONST DEVMODEA *pDevMode); + WINSPOOLAPI int WINAPI DeviceCapabilitiesW(LPCWSTR pDevice,LPCWSTR pPort,WORD fwCapability,LPWSTR pOutput,CONST DEVMODEW *pDevMode); + WINGDIAPI int WINAPI DrawEscape(HDC hdc,int iEscape,int cjIn,LPCSTR lpIn); + WINGDIAPI WINBOOL WINAPI Ellipse(HDC hdc,int left,int top,int right,int bottom); + WINGDIAPI int WINAPI EnumFontFamiliesExA(HDC hdc,LPLOGFONTA lpLogfont,FONTENUMPROCA lpProc,LPARAM lParam,DWORD dwFlags); + WINGDIAPI int WINAPI EnumFontFamiliesExW(HDC hdc,LPLOGFONTW lpLogfont,FONTENUMPROCW lpProc,LPARAM lParam,DWORD dwFlags); + WINGDIAPI int WINAPI EnumFontFamiliesA(HDC hdc,LPCSTR lpLogfont,FONTENUMPROCA lpProc,LPARAM lParam); + WINGDIAPI int WINAPI EnumFontFamiliesW(HDC hdc,LPCWSTR lpLogfont,FONTENUMPROCW lpProc,LPARAM lParam); + WINGDIAPI int WINAPI EnumFontsA(HDC hdc,LPCSTR lpLogfont,FONTENUMPROCA lpProc,LPARAM lParam); + WINGDIAPI int WINAPI EnumFontsW(HDC hdc,LPCWSTR lpLogfont,FONTENUMPROCW lpProc,LPARAM lParam); + WINGDIAPI int WINAPI EnumObjects(HDC hdc,int nType,GOBJENUMPROC lpFunc,LPARAM lParam); + WINGDIAPI WINBOOL WINAPI EqualRgn(HRGN hrgn1,HRGN hrgn2); + WINGDIAPI int WINAPI Escape(HDC hdc,int iEscape,int cjIn,LPCSTR pvIn,LPVOID pvOut); + WINGDIAPI int WINAPI ExtEscape(HDC hdc,int iEscape,int cjInput,LPCSTR lpInData,int cjOutput,LPSTR lpOutData); + WINGDIAPI int WINAPI ExcludeClipRect(HDC hdc,int left,int top,int right,int bottom); + WINGDIAPI HRGN WINAPI ExtCreateRegion(CONST XFORM *lpx,DWORD nCount,CONST RGNDATA *lpData); + WINGDIAPI WINBOOL WINAPI ExtFloodFill(HDC hdc,int x,int y,COLORREF color,UINT type); + WINGDIAPI WINBOOL WINAPI FillRgn(HDC hdc,HRGN hrgn,HBRUSH hbr); + WINGDIAPI WINBOOL WINAPI FloodFill(HDC hdc,int x,int y,COLORREF color); + WINGDIAPI WINBOOL WINAPI FrameRgn(HDC hdc,HRGN hrgn,HBRUSH hbr,int w,int h); + WINGDIAPI int WINAPI GetROP2(HDC hdc); + WINGDIAPI WINBOOL WINAPI GetAspectRatioFilterEx(HDC hdc,LPSIZE lpsize); + WINGDIAPI COLORREF WINAPI GetBkColor(HDC hdc); + WINGDIAPI COLORREF WINAPI GetDCBrushColor(HDC hdc); + WINGDIAPI COLORREF WINAPI GetDCPenColor(HDC hdc); + WINGDIAPI int WINAPI GetBkMode(HDC hdc); + WINGDIAPI LONG WINAPI GetBitmapBits(HBITMAP hbit,LONG cb,LPVOID lpvBits); + WINGDIAPI WINBOOL WINAPI GetBitmapDimensionEx(HBITMAP hbit,LPSIZE lpsize); + WINGDIAPI UINT WINAPI GetBoundsRect(HDC hdc,LPRECT lprect,UINT flags); + WINGDIAPI WINBOOL WINAPI GetBrushOrgEx(HDC hdc,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI GetCharWidthA(HDC hdc,UINT iFirst,UINT iLast,LPINT lpBuffer); + WINGDIAPI WINBOOL WINAPI GetCharWidthW(HDC hdc,UINT iFirst,UINT iLast,LPINT lpBuffer); + WINGDIAPI WINBOOL WINAPI GetCharWidth32A(HDC hdc,UINT iFirst,UINT iLast,LPINT lpBuffer); + WINGDIAPI WINBOOL WINAPI GetCharWidth32W(HDC hdc,UINT iFirst,UINT iLast,LPINT lpBuffer); + WINGDIAPI WINBOOL WINAPI GetCharWidthFloatA(HDC hdc,UINT iFirst,UINT iLast,PFLOAT lpBuffer); + WINGDIAPI WINBOOL WINAPI GetCharWidthFloatW(HDC hdc,UINT iFirst,UINT iLast,PFLOAT lpBuffer); + WINGDIAPI WINBOOL WINAPI GetCharABCWidthsA(HDC hdc,UINT wFirst,UINT wLast,LPABC lpABC); + WINGDIAPI WINBOOL WINAPI GetCharABCWidthsW(HDC hdc,UINT wFirst,UINT wLast,LPABC lpABC); + WINGDIAPI WINBOOL WINAPI GetCharABCWidthsFloatA(HDC hdc,UINT iFirst,UINT iLast,LPABCFLOAT lpABC); + WINGDIAPI WINBOOL WINAPI GetCharABCWidthsFloatW(HDC hdc,UINT iFirst,UINT iLast,LPABCFLOAT lpABC); + WINGDIAPI int WINAPI GetClipBox(HDC hdc,LPRECT lprect); + WINGDIAPI int WINAPI GetClipRgn(HDC hdc,HRGN hrgn); + WINGDIAPI int WINAPI GetMetaRgn(HDC hdc,HRGN hrgn); + WINGDIAPI HGDIOBJ WINAPI GetCurrentObject(HDC hdc,UINT type); + WINGDIAPI WINBOOL WINAPI GetCurrentPositionEx(HDC hdc,LPPOINT lppt); + WINGDIAPI int WINAPI GetDeviceCaps(HDC hdc,int index); + WINGDIAPI int WINAPI GetDIBits(HDC hdc,HBITMAP hbm,UINT start,UINT cLines,LPVOID lpvBits,LPBITMAPINFO lpbmi,UINT usage); + WINGDIAPI DWORD WINAPI GetFontData (HDC hdc,DWORD dwTable,DWORD dwOffset,PVOID pvBuffer,DWORD cjBuffer); + WINGDIAPI DWORD WINAPI GetGlyphOutlineA(HDC hdc,UINT uChar,UINT fuFormat,LPGLYPHMETRICS lpgm,DWORD cjBuffer,LPVOID pvBuffer,CONST MAT2 *lpmat2); + WINGDIAPI DWORD WINAPI GetGlyphOutlineW(HDC hdc,UINT uChar,UINT fuFormat,LPGLYPHMETRICS lpgm,DWORD cjBuffer,LPVOID pvBuffer,CONST MAT2 *lpmat2); + WINGDIAPI int WINAPI GetGraphicsMode(HDC hdc); + WINGDIAPI int WINAPI GetMapMode(HDC hdc); + WINGDIAPI UINT WINAPI GetMetaFileBitsEx(HMETAFILE hMF,UINT cbBuffer,LPVOID lpData); + WINGDIAPI HMETAFILE WINAPI GetMetaFileA(LPCSTR lpName); + WINGDIAPI HMETAFILE WINAPI GetMetaFileW(LPCWSTR lpName); + WINGDIAPI COLORREF WINAPI GetNearestColor(HDC hdc,COLORREF color); + WINGDIAPI UINT WINAPI GetNearestPaletteIndex(HPALETTE h,COLORREF color); + WINGDIAPI DWORD WINAPI GetObjectType(HGDIOBJ h); + +#ifndef NOTEXTMETRIC +#ifdef UNICODE +#define GetOutlineTextMetrics GetOutlineTextMetricsW +#else +#define GetOutlineTextMetrics GetOutlineTextMetricsA +#endif + + WINGDIAPI UINT WINAPI GetOutlineTextMetricsA(HDC hdc,UINT cjCopy,LPOUTLINETEXTMETRICA potm); + WINGDIAPI UINT WINAPI GetOutlineTextMetricsW(HDC hdc,UINT cjCopy,LPOUTLINETEXTMETRICW potm); +#endif + +#ifdef UNICODE +#define GetTextExtentPoint GetTextExtentPointW +#define GetTextExtentPoint32 GetTextExtentPoint32W +#define GetTextExtentExPoint GetTextExtentExPointW +#define GetCharacterPlacement GetCharacterPlacementW +#else +#define GetTextExtentPoint GetTextExtentPointA +#define GetTextExtentPoint32 GetTextExtentPoint32A +#define GetTextExtentExPoint GetTextExtentExPointA +#define GetCharacterPlacement GetCharacterPlacementA +#endif + + WINGDIAPI UINT WINAPI GetPaletteEntries(HPALETTE hpal,UINT iStart,UINT cEntries,LPPALETTEENTRY pPalEntries); + WINGDIAPI COLORREF WINAPI GetPixel(HDC hdc,int x,int y); + WINGDIAPI int WINAPI GetPixelFormat(HDC hdc); + WINGDIAPI int WINAPI GetPolyFillMode(HDC hdc); + WINGDIAPI WINBOOL WINAPI GetRasterizerCaps(LPRASTERIZER_STATUS lpraststat,UINT cjBytes); + WINGDIAPI int WINAPI GetRandomRgn (HDC hdc,HRGN hrgn,INT i); + WINGDIAPI DWORD WINAPI GetRegionData(HRGN hrgn,DWORD nCount,LPRGNDATA lpRgnData); + WINGDIAPI int WINAPI GetRgnBox(HRGN hrgn,LPRECT lprc); + WINGDIAPI HGDIOBJ WINAPI GetStockObject(int i); + WINGDIAPI int WINAPI GetStretchBltMode(HDC hdc); + WINGDIAPI UINT WINAPI GetSystemPaletteEntries(HDC hdc,UINT iStart,UINT cEntries,LPPALETTEENTRY pPalEntries); + WINGDIAPI UINT WINAPI GetSystemPaletteUse(HDC hdc); + WINGDIAPI int WINAPI GetTextCharacterExtra(HDC hdc); + WINGDIAPI UINT WINAPI GetTextAlign(HDC hdc); + WINGDIAPI COLORREF WINAPI GetTextColor(HDC hdc); + WINGDIAPI WINBOOL WINAPI GetTextExtentPointA(HDC hdc,LPCSTR lpString,int c,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI GetTextExtentPointW(HDC hdc,LPCWSTR lpString,int c,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI GetTextExtentPoint32A(HDC hdc,LPCSTR lpString,int c,LPSIZE psizl); + WINGDIAPI WINBOOL WINAPI GetTextExtentPoint32W(HDC hdc,LPCWSTR lpString,int c,LPSIZE psizl); + WINGDIAPI WINBOOL WINAPI GetTextExtentExPointA(HDC hdc,LPCSTR lpszString,int cchString,int nMaxExtent,LPINT lpnFit,LPINT lpnDx,LPSIZE lpSize); + WINGDIAPI WINBOOL WINAPI GetTextExtentExPointW(HDC hdc,LPCWSTR lpszString,int cchString,int nMaxExtent,LPINT lpnFit,LPINT lpnDx,LPSIZE lpSize); + WINGDIAPI int WINAPI GetTextCharset(HDC hdc); + WINGDIAPI int WINAPI GetTextCharsetInfo(HDC hdc,LPFONTSIGNATURE lpSig,DWORD dwFlags); + WINGDIAPI WINBOOL WINAPI TranslateCharsetInfo(DWORD *lpSrc,LPCHARSETINFO lpCs,DWORD dwFlags); + WINGDIAPI DWORD WINAPI GetFontLanguageInfo(HDC hdc); + WINGDIAPI DWORD WINAPI GetCharacterPlacementA(HDC hdc,LPCSTR lpString,int nCount,int nMexExtent,LPGCP_RESULTSA lpResults,DWORD dwFlags); + WINGDIAPI DWORD WINAPI GetCharacterPlacementW(HDC hdc,LPCWSTR lpString,int nCount,int nMexExtent,LPGCP_RESULTSW lpResults,DWORD dwFlags); + + typedef struct tagWCRANGE { + WCHAR wcLow; + USHORT cGlyphs; + } WCRANGE,*PWCRANGE,*LPWCRANGE; + + typedef struct tagGLYPHSET { + DWORD cbThis; + DWORD flAccel; + DWORD cGlyphsSupported; + DWORD cRanges; + WCRANGE ranges[1]; + } GLYPHSET,*PGLYPHSET,*LPGLYPHSET; + +#define GS_8BIT_INDICES 0x00000001 + +#define GGI_MARK_NONEXISTING_GLYPHS 0X0001 + +#ifdef UNICODE +#define GetGlyphIndices GetGlyphIndicesW +#else +#define GetGlyphIndices GetGlyphIndicesA +#endif + + WINGDIAPI DWORD WINAPI GetFontUnicodeRanges(HDC hdc,LPGLYPHSET lpgs); + WINGDIAPI DWORD WINAPI GetGlyphIndicesA(HDC hdc,LPCSTR lpstr,int c,LPWORD pgi,DWORD fl); + WINGDIAPI DWORD WINAPI GetGlyphIndicesW(HDC hdc,LPCWSTR lpstr,int c,LPWORD pgi,DWORD fl); + WINGDIAPI WINBOOL WINAPI GetTextExtentPointI(HDC hdc,LPWORD pgiIn,int cgi,LPSIZE psize); + WINGDIAPI WINBOOL WINAPI GetTextExtentExPointI (HDC hdc,LPWORD lpwszString,int cwchString,int nMaxExtent,LPINT lpnFit,LPINT lpnDx,LPSIZE lpSize); + WINGDIAPI WINBOOL WINAPI GetCharWidthI(HDC hdc,UINT giFirst,UINT cgi,LPWORD pgi,LPINT piWidths); + WINGDIAPI WINBOOL WINAPI GetCharABCWidthsI(HDC hdc,UINT giFirst,UINT cgi,LPWORD pgi,LPABC pabc); + +#define STAMP_DESIGNVECTOR (0x8000000 + 'd' + ('v' << 8)) +#define STAMP_AXESLIST (0x8000000 + 'a' + ('l' << 8)) +#define MM_MAX_NUMAXES 16 + + typedef struct tagDESIGNVECTOR { + DWORD dvReserved; + DWORD dvNumAxes; + LONG dvValues[MM_MAX_NUMAXES]; + } DESIGNVECTOR,*PDESIGNVECTOR,*LPDESIGNVECTOR; + +#ifdef UNICODE +#define AddFontResourceEx AddFontResourceExW +#define RemoveFontResourceEx RemoveFontResourceExW +#else +#define AddFontResourceEx AddFontResourceExA +#define RemoveFontResourceEx RemoveFontResourceExA +#endif + + WINGDIAPI int WINAPI AddFontResourceExA(LPCSTR name,DWORD fl,PVOID res); + WINGDIAPI int WINAPI AddFontResourceExW(LPCWSTR name,DWORD fl,PVOID res); + WINGDIAPI WINBOOL WINAPI RemoveFontResourceExA(LPCSTR name,DWORD fl,PVOID pdv); + WINGDIAPI WINBOOL WINAPI RemoveFontResourceExW(LPCWSTR name,DWORD fl,PVOID pdv); + WINGDIAPI HANDLE WINAPI AddFontMemResourceEx(PVOID pFileView,DWORD cjSize,PVOID pvResrved,DWORD *pNumFonts); + WINGDIAPI WINBOOL WINAPI RemoveFontMemResourceEx(HANDLE h); + +#define FR_PRIVATE 0x10 +#define FR_NOT_ENUM 0x20 + +#define MM_MAX_AXES_NAMELEN 16 + + typedef struct tagAXISINFOA { + LONG axMinValue; + LONG axMaxValue; + BYTE axAxisName[MM_MAX_AXES_NAMELEN]; + } AXISINFOA,*PAXISINFOA,*LPAXISINFOA; + + typedef struct tagAXISINFOW { + LONG axMinValue; + LONG axMaxValue; + WCHAR axAxisName[MM_MAX_AXES_NAMELEN]; + } AXISINFOW,*PAXISINFOW,*LPAXISINFOW; +#ifdef UNICODE + typedef AXISINFOW AXISINFO; + typedef PAXISINFOW PAXISINFO; + typedef LPAXISINFOW LPAXISINFO; +#else + typedef AXISINFOA AXISINFO; + typedef PAXISINFOA PAXISINFO; + typedef LPAXISINFOA LPAXISINFO; +#endif + + typedef struct tagAXESLISTA { + DWORD axlReserved; + DWORD axlNumAxes; + AXISINFOA axlAxisInfo[MM_MAX_NUMAXES]; + } AXESLISTA,*PAXESLISTA,*LPAXESLISTA; + + typedef struct tagAXESLISTW { + DWORD axlReserved; + DWORD axlNumAxes; + AXISINFOW axlAxisInfo[MM_MAX_NUMAXES]; + } AXESLISTW,*PAXESLISTW,*LPAXESLISTW; +#ifdef UNICODE + typedef AXESLISTW AXESLIST; + typedef PAXESLISTW PAXESLIST; + typedef LPAXESLISTW LPAXESLIST; +#else + typedef AXESLISTA AXESLIST; + typedef PAXESLISTA PAXESLIST; + typedef LPAXESLISTA LPAXESLIST; +#endif + + typedef struct tagENUMLOGFONTEXDVA { + ENUMLOGFONTEXA elfEnumLogfontEx; + DESIGNVECTOR elfDesignVector; + } ENUMLOGFONTEXDVA,*PENUMLOGFONTEXDVA,*LPENUMLOGFONTEXDVA; + + typedef struct tagENUMLOGFONTEXDVW { + ENUMLOGFONTEXW elfEnumLogfontEx; + DESIGNVECTOR elfDesignVector; + } ENUMLOGFONTEXDVW,*PENUMLOGFONTEXDVW,*LPENUMLOGFONTEXDVW; +#ifdef UNICODE + typedef ENUMLOGFONTEXDVW ENUMLOGFONTEXDV; + typedef PENUMLOGFONTEXDVW PENUMLOGFONTEXDV; + typedef LPENUMLOGFONTEXDVW LPENUMLOGFONTEXDV; +#else + typedef ENUMLOGFONTEXDVA ENUMLOGFONTEXDV; + typedef PENUMLOGFONTEXDVA PENUMLOGFONTEXDV; + typedef LPENUMLOGFONTEXDVA LPENUMLOGFONTEXDV; +#endif + +#ifdef UNICODE +#define CreateFontIndirectEx CreateFontIndirectExW +#else +#define CreateFontIndirectEx CreateFontIndirectExA +#endif + + WINGDIAPI HFONT WINAPI CreateFontIndirectExA(CONST ENUMLOGFONTEXDVA *); + WINGDIAPI HFONT WINAPI CreateFontIndirectExW(CONST ENUMLOGFONTEXDVW *); + +#ifndef NOTEXTMETRIC + typedef struct tagENUMTEXTMETRICA { + NEWTEXTMETRICEXA etmNewTextMetricEx; + AXESLISTA etmAxesList; + } ENUMTEXTMETRICA,*PENUMTEXTMETRICA,*LPENUMTEXTMETRICA; + typedef struct tagENUMTEXTMETRICW + { + NEWTEXTMETRICEXW etmNewTextMetricEx; + AXESLISTW etmAxesList; + } ENUMTEXTMETRICW,*PENUMTEXTMETRICW,*LPENUMTEXTMETRICW; +#ifdef UNICODE + typedef ENUMTEXTMETRICW ENUMTEXTMETRIC; + typedef PENUMTEXTMETRICW PENUMTEXTMETRIC; + typedef LPENUMTEXTMETRICW LPENUMTEXTMETRIC; +#else + typedef ENUMTEXTMETRICA ENUMTEXTMETRIC; + typedef PENUMTEXTMETRICA PENUMTEXTMETRIC; + typedef LPENUMTEXTMETRICA LPENUMTEXTMETRIC; +#endif +#endif + +#ifdef UNICODE +#define ResetDC ResetDCW +#define RemoveFontResource RemoveFontResourceW +#else +#define ResetDC ResetDCA +#define RemoveFontResource RemoveFontResourceA +#endif + + WINGDIAPI WINBOOL WINAPI GetViewportExtEx(HDC hdc,LPSIZE lpsize); + WINGDIAPI WINBOOL WINAPI GetViewportOrgEx(HDC hdc,LPPOINT lppoint); + WINGDIAPI WINBOOL WINAPI GetWindowExtEx(HDC hdc,LPSIZE lpsize); + WINGDIAPI WINBOOL WINAPI GetWindowOrgEx(HDC hdc,LPPOINT lppoint); + WINGDIAPI int WINAPI IntersectClipRect(HDC hdc,int left,int top,int right,int bottom); + WINGDIAPI WINBOOL WINAPI InvertRgn(HDC hdc,HRGN hrgn); + WINGDIAPI WINBOOL WINAPI LineDDA(int xStart,int yStart,int xEnd,int yEnd,LINEDDAPROC lpProc,LPARAM data); + WINGDIAPI WINBOOL WINAPI LineTo(HDC hdc,int x,int y); + WINGDIAPI WINBOOL WINAPI MaskBlt(HDC hdcDest,int xDest,int yDest,int width,int height,HDC hdcSrc,int xSrc,int ySrc,HBITMAP hbmMask,int xMask,int yMask,DWORD rop); + WINGDIAPI WINBOOL WINAPI PlgBlt(HDC hdcDest,CONST POINT *lpPoint,HDC hdcSrc,int xSrc,int ySrc,int width,int height,HBITMAP hbmMask,int xMask,int yMask); + WINGDIAPI int WINAPI OffsetClipRgn(HDC hdc,int x,int y); + WINGDIAPI int WINAPI OffsetRgn(HRGN hrgn,int x,int y); + WINGDIAPI WINBOOL WINAPI PatBlt(HDC hdc,int x,int y,int w,int h,DWORD rop); + WINGDIAPI WINBOOL WINAPI Pie(HDC hdc,int left,int top,int right,int bottom,int xr1,int yr1,int xr2,int yr2); + WINGDIAPI WINBOOL WINAPI PlayMetaFile(HDC hdc,HMETAFILE hmf); + WINGDIAPI WINBOOL WINAPI PaintRgn(HDC hdc,HRGN hrgn); + WINGDIAPI WINBOOL WINAPI PolyPolygon(HDC hdc,CONST POINT *apt,CONST INT *asz,int csz); + WINGDIAPI WINBOOL WINAPI PtInRegion(HRGN hrgn,int x,int y); + WINGDIAPI WINBOOL WINAPI PtVisible(HDC hdc,int x,int y); + WINGDIAPI WINBOOL WINAPI RectInRegion(HRGN hrgn,CONST RECT *lprect); + WINGDIAPI WINBOOL WINAPI RectVisible(HDC hdc,CONST RECT *lprect); + WINGDIAPI WINBOOL WINAPI Rectangle(HDC hdc,int left,int top,int right,int bottom); + WINGDIAPI WINBOOL WINAPI RestoreDC(HDC hdc,int nSavedDC); + WINGDIAPI HDC WINAPI ResetDCA(HDC hdc,CONST DEVMODEA *lpdm); + WINGDIAPI HDC WINAPI ResetDCW(HDC hdc,CONST DEVMODEW *lpdm); + WINGDIAPI UINT WINAPI RealizePalette(HDC hdc); + WINGDIAPI WINBOOL WINAPI RemoveFontResourceA(LPCSTR lpFileName); + WINGDIAPI WINBOOL WINAPI RemoveFontResourceW(LPCWSTR lpFileName); + WINGDIAPI WINBOOL WINAPI RoundRect(HDC hdc,int left,int top,int right,int bottom,int width,int height); + WINGDIAPI WINBOOL WINAPI ResizePalette(HPALETTE hpal,UINT n); + WINGDIAPI int WINAPI SaveDC(HDC hdc); + WINGDIAPI int WINAPI SelectClipRgn(HDC hdc,HRGN hrgn); + WINGDIAPI int WINAPI ExtSelectClipRgn(HDC hdc,HRGN hrgn,int mode); + WINGDIAPI int WINAPI SetMetaRgn(HDC hdc); + WINGDIAPI HGDIOBJ WINAPI SelectObject(HDC hdc,HGDIOBJ h); + WINGDIAPI HPALETTE WINAPI SelectPalette(HDC hdc,HPALETTE hPal,WINBOOL bForceBkgd); + WINGDIAPI COLORREF WINAPI SetBkColor(HDC hdc,COLORREF color); + WINGDIAPI COLORREF WINAPI SetDCBrushColor(HDC hdc,COLORREF color); + WINGDIAPI COLORREF WINAPI SetDCPenColor(HDC hdc,COLORREF color); + WINGDIAPI int WINAPI SetBkMode(HDC hdc,int mode); + WINGDIAPI LONG WINAPI SetBitmapBits(HBITMAP hbm,DWORD cb,CONST VOID *pvBits); + WINGDIAPI UINT WINAPI SetBoundsRect(HDC hdc,CONST RECT *lprect,UINT flags); + WINGDIAPI int WINAPI SetDIBits(HDC hdc,HBITMAP hbm,UINT start,UINT cLines,CONST VOID *lpBits,CONST BITMAPINFO *lpbmi,UINT ColorUse); + WINGDIAPI int WINAPI SetDIBitsToDevice(HDC hdc,int xDest,int yDest,DWORD w,DWORD h,int xSrc,int ySrc,UINT StartScan,UINT cLines,CONST VOID *lpvBits,CONST BITMAPINFO *lpbmi,UINT ColorUse); + WINGDIAPI DWORD WINAPI SetMapperFlags(HDC hdc,DWORD flags); + WINGDIAPI int WINAPI SetGraphicsMode(HDC hdc,int iMode); + WINGDIAPI int WINAPI SetMapMode(HDC hdc,int iMode); + WINGDIAPI DWORD WINAPI SetLayout(HDC hdc,DWORD l); + WINGDIAPI DWORD WINAPI GetLayout(HDC hdc); + WINGDIAPI HMETAFILE WINAPI SetMetaFileBitsEx(UINT cbBuffer,CONST BYTE *lpData); + WINGDIAPI UINT WINAPI SetPaletteEntries(HPALETTE hpal,UINT iStart,UINT cEntries,CONST PALETTEENTRY *pPalEntries); + WINGDIAPI COLORREF WINAPI SetPixel(HDC hdc,int x,int y,COLORREF color); + WINGDIAPI WINBOOL WINAPI SetPixelV(HDC hdc,int x,int y,COLORREF color); + WINGDIAPI WINBOOL WINAPI SetPixelFormat(HDC hdc,int format,CONST PIXELFORMATDESCRIPTOR *ppfd); + WINGDIAPI int WINAPI SetPolyFillMode(HDC hdc,int mode); + WINGDIAPI WINBOOL WINAPI StretchBlt(HDC hdcDest,int xDest,int yDest,int wDest,int hDest,HDC hdcSrc,int xSrc,int ySrc,int wSrc,int hSrc,DWORD rop); + WINGDIAPI WINBOOL WINAPI SetRectRgn(HRGN hrgn,int left,int top,int right,int bottom); + WINGDIAPI int WINAPI StretchDIBits(HDC hdc,int xDest,int yDest,int DestWidth,int DestHeight,int xSrc,int ySrc,int SrcWidth,int SrcHeight,CONST VOID *lpBits,CONST BITMAPINFO *lpbmi,UINT iUsage,DWORD rop); + WINGDIAPI int WINAPI SetROP2(HDC hdc,int rop2); + WINGDIAPI int WINAPI SetStretchBltMode(HDC hdc,int mode); + WINGDIAPI UINT WINAPI SetSystemPaletteUse(HDC hdc,UINT use); + WINGDIAPI int WINAPI SetTextCharacterExtra(HDC hdc,int extra); + WINGDIAPI COLORREF WINAPI SetTextColor(HDC hdc,COLORREF color); + WINGDIAPI UINT WINAPI SetTextAlign(HDC hdc,UINT align); + WINGDIAPI WINBOOL WINAPI SetTextJustification(HDC hdc,int extra,int count); + WINGDIAPI WINBOOL WINAPI UpdateColors(HDC hdc); + + typedef USHORT COLOR16; + + typedef struct _TRIVERTEX { + LONG x; + LONG y; + COLOR16 Red; + COLOR16 Green; + COLOR16 Blue; + COLOR16 Alpha; + } TRIVERTEX,*PTRIVERTEX,*LPTRIVERTEX; + + typedef struct _GRADIENT_TRIANGLE { + ULONG Vertex1; + ULONG Vertex2; + ULONG Vertex3; + } GRADIENT_TRIANGLE,*PGRADIENT_TRIANGLE,*LPGRADIENT_TRIANGLE; + + typedef struct _GRADIENT_RECT { + ULONG UpperLeft; + ULONG LowerRight; + } GRADIENT_RECT,*PGRADIENT_RECT,*LPGRADIENT_RECT; + + typedef struct _BLENDFUNCTION { + BYTE BlendOp; + BYTE BlendFlags; + BYTE SourceConstantAlpha; + BYTE AlphaFormat; + } BLENDFUNCTION,*PBLENDFUNCTION; + +#define AC_SRC_OVER 0x00 +#define AC_SRC_ALPHA 0x01 + + WINGDIAPI WINBOOL WINAPI AlphaBlend(HDC hdcDest,int xoriginDest,int yoriginDest,int wDest,int hDest,HDC hdcSrc,int xoriginSrc,int yoriginSrc,int wSrc,int hSrc,BLENDFUNCTION ftn); + WINGDIAPI WINBOOL WINAPI TransparentBlt(HDC hdcDest,int xoriginDest,int yoriginDest,int wDest,int hDest,HDC hdcSrc,int xoriginSrc,int yoriginSrc,int wSrc,int hSrc,UINT crTransparent); + +#define GRADIENT_FILL_RECT_H 0x00000000 +#define GRADIENT_FILL_RECT_V 0x00000001 +#define GRADIENT_FILL_TRIANGLE 0x00000002 +#define GRADIENT_FILL_OP_FLAG 0x000000ff + + WINGDIAPI WINBOOL WINAPI GradientFill(HDC hdc,PTRIVERTEX pVertex,ULONG nVertex,PVOID pMesh,ULONG nMesh,ULONG ulMode); + +#ifndef NOMETAFILE + +#ifdef UNICODE +#define CopyEnhMetaFile CopyEnhMetaFileW +#define CreateEnhMetaFile CreateEnhMetaFileW +#define GetEnhMetaFile GetEnhMetaFileW +#define GetEnhMetaFileDescription GetEnhMetaFileDescriptionW +#else +#define CopyEnhMetaFile CopyEnhMetaFileA +#define CreateEnhMetaFile CreateEnhMetaFileA +#define GetEnhMetaFile GetEnhMetaFileA +#define GetEnhMetaFileDescription GetEnhMetaFileDescriptionA +#endif + + WINGDIAPI WINBOOL WINAPI PlayMetaFileRecord(HDC hdc,LPHANDLETABLE lpHandleTable,LPMETARECORD lpMR,UINT noObjs); + + typedef int (CALLBACK *MFENUMPROC)(HDC hdc,HANDLETABLE *lpht,METARECORD *lpMR,int nObj,LPARAM param); + + WINGDIAPI WINBOOL WINAPI EnumMetaFile(HDC hdc,HMETAFILE hmf,MFENUMPROC proc,LPARAM param); + + typedef int (CALLBACK *ENHMFENUMPROC)(HDC hdc,HANDLETABLE *lpht,CONST ENHMETARECORD *lpmr,int hHandles,LPARAM data); + + WINGDIAPI HENHMETAFILE WINAPI CloseEnhMetaFile(HDC hdc); + WINGDIAPI HENHMETAFILE WINAPI CopyEnhMetaFileA(HENHMETAFILE hEnh,LPCSTR lpFileName); + WINGDIAPI HENHMETAFILE WINAPI CopyEnhMetaFileW(HENHMETAFILE hEnh,LPCWSTR lpFileName); + WINGDIAPI HDC WINAPI CreateEnhMetaFileA(HDC hdc,LPCSTR lpFilename,CONST RECT *lprc,LPCSTR lpDesc); + WINGDIAPI HDC WINAPI CreateEnhMetaFileW(HDC hdc,LPCWSTR lpFilename,CONST RECT *lprc,LPCWSTR lpDesc); + WINGDIAPI WINBOOL WINAPI DeleteEnhMetaFile(HENHMETAFILE hmf); + WINGDIAPI WINBOOL WINAPI EnumEnhMetaFile(HDC hdc,HENHMETAFILE hmf,ENHMFENUMPROC proc,LPVOID param,CONST RECT *lpRect); + WINGDIAPI HENHMETAFILE WINAPI GetEnhMetaFileA(LPCSTR lpName); + WINGDIAPI HENHMETAFILE WINAPI GetEnhMetaFileW(LPCWSTR lpName); + WINGDIAPI UINT WINAPI GetEnhMetaFileBits(HENHMETAFILE hEMF,UINT nSize,LPBYTE lpData); + WINGDIAPI UINT WINAPI GetEnhMetaFileDescriptionA(HENHMETAFILE hemf,UINT cchBuffer,LPSTR lpDescription); + WINGDIAPI UINT WINAPI GetEnhMetaFileDescriptionW(HENHMETAFILE hemf,UINT cchBuffer,LPWSTR lpDescription); + WINGDIAPI UINT WINAPI GetEnhMetaFileHeader(HENHMETAFILE hemf,UINT nSize,LPENHMETAHEADER lpEnhMetaHeader); + WINGDIAPI UINT WINAPI GetEnhMetaFilePaletteEntries(HENHMETAFILE hemf,UINT nNumEntries,LPPALETTEENTRY lpPaletteEntries); + WINGDIAPI UINT WINAPI GetEnhMetaFilePixelFormat(HENHMETAFILE hemf,UINT cbBuffer,PIXELFORMATDESCRIPTOR *ppfd); + WINGDIAPI UINT WINAPI GetWinMetaFileBits(HENHMETAFILE hemf,UINT cbData16,LPBYTE pData16,INT iMapMode,HDC hdcRef); + WINGDIAPI WINBOOL WINAPI PlayEnhMetaFile(HDC hdc,HENHMETAFILE hmf,CONST RECT *lprect); + WINGDIAPI WINBOOL WINAPI PlayEnhMetaFileRecord(HDC hdc,LPHANDLETABLE pht,CONST ENHMETARECORD *pmr,UINT cht); + WINGDIAPI HENHMETAFILE WINAPI SetEnhMetaFileBits(UINT nSize,CONST BYTE *pb); + WINGDIAPI HENHMETAFILE WINAPI SetWinMetaFileBits(UINT nSize,CONST BYTE *lpMeta16Data,HDC hdcRef,CONST METAFILEPICT *lpMFP); + WINGDIAPI WINBOOL WINAPI GdiComment(HDC hdc,UINT nSize,CONST BYTE *lpData); +#endif + +#ifndef NOTEXTMETRIC +#ifdef UNICODE +#define GetTextMetrics GetTextMetricsW +#else +#define GetTextMetrics GetTextMetricsA +#endif + + WINGDIAPI WINBOOL WINAPI GetTextMetricsA(HDC hdc,LPTEXTMETRICA lptm); + WINGDIAPI WINBOOL WINAPI GetTextMetricsW(HDC hdc,LPTEXTMETRICW lptm); +#endif + + typedef struct tagDIBSECTION { + BITMAP dsBm; + BITMAPINFOHEADER dsBmih; + DWORD dsBitfields[3]; + HANDLE dshSection; + DWORD dsOffset; + } DIBSECTION,*LPDIBSECTION,*PDIBSECTION; + + WINGDIAPI WINBOOL WINAPI AngleArc(HDC hdc,int x,int y,DWORD r,FLOAT StartAngle,FLOAT SweepAngle); + WINGDIAPI WINBOOL WINAPI PolyPolyline(HDC hdc,CONST POINT *apt,CONST DWORD *asz,DWORD csz); + WINGDIAPI WINBOOL WINAPI GetWorldTransform(HDC hdc,LPXFORM lpxf); + WINGDIAPI WINBOOL WINAPI SetWorldTransform(HDC hdc,CONST XFORM *lpxf); + WINGDIAPI WINBOOL WINAPI ModifyWorldTransform(HDC hdc,CONST XFORM *lpxf,DWORD mode); + WINGDIAPI WINBOOL WINAPI CombineTransform(LPXFORM lpxfOut,CONST XFORM *lpxf1,CONST XFORM *lpxf2); + WINGDIAPI HBITMAP WINAPI CreateDIBSection(HDC hdc,CONST BITMAPINFO *lpbmi,UINT usage,VOID **ppvBits,HANDLE hSection,DWORD offset); + WINGDIAPI UINT WINAPI GetDIBColorTable(HDC hdc,UINT iStart,UINT cEntries,RGBQUAD *prgbq); + WINGDIAPI UINT WINAPI SetDIBColorTable(HDC hdc,UINT iStart,UINT cEntries,CONST RGBQUAD *prgbq); + +#define CA_NEGATIVE 0x0001 +#define CA_LOG_FILTER 0x0002 + +#define ILLUMINANT_DEVICE_DEFAULT 0 +#define ILLUMINANT_A 1 +#define ILLUMINANT_B 2 +#define ILLUMINANT_C 3 +#define ILLUMINANT_D50 4 +#define ILLUMINANT_D55 5 +#define ILLUMINANT_D65 6 +#define ILLUMINANT_D75 7 +#define ILLUMINANT_F2 8 +#define ILLUMINANT_MAX_INDEX ILLUMINANT_F2 + +#define ILLUMINANT_TUNGSTEN ILLUMINANT_A +#define ILLUMINANT_DAYLIGHT ILLUMINANT_C +#define ILLUMINANT_FLUORESCENT ILLUMINANT_F2 +#define ILLUMINANT_NTSC ILLUMINANT_C + +#define RGB_GAMMA_MIN (WORD)02500 +#define RGB_GAMMA_MAX (WORD)65000 + +#define REFERENCE_WHITE_MIN (WORD)6000 +#define REFERENCE_WHITE_MAX (WORD)10000 +#define REFERENCE_BLACK_MIN (WORD)0 +#define REFERENCE_BLACK_MAX (WORD)4000 + +#define COLOR_ADJ_MIN (SHORT)-100 +#define COLOR_ADJ_MAX (SHORT)100 + + typedef struct tagCOLORADJUSTMENT { + WORD caSize; + WORD caFlags; + WORD caIlluminantIndex; + WORD caRedGamma; + WORD caGreenGamma; + WORD caBlueGamma; + WORD caReferenceBlack; + WORD caReferenceWhite; + SHORT caContrast; + SHORT caBrightness; + SHORT caColorfulness; + SHORT caRedGreenTint; + } COLORADJUSTMENT,*PCOLORADJUSTMENT,*LPCOLORADJUSTMENT; + + WINGDIAPI WINBOOL WINAPI SetColorAdjustment(HDC hdc,CONST COLORADJUSTMENT *lpca); + WINGDIAPI WINBOOL WINAPI GetColorAdjustment(HDC hdc,LPCOLORADJUSTMENT lpca); + WINGDIAPI HPALETTE WINAPI CreateHalftonePalette(HDC hdc); + + typedef WINBOOL (CALLBACK *ABORTPROC)(HDC,int); + + typedef struct _DOCINFOA { + int cbSize; + LPCSTR lpszDocName; + LPCSTR lpszOutput; + LPCSTR lpszDatatype; + DWORD fwType; + } DOCINFOA,*LPDOCINFOA; + + typedef struct _DOCINFOW { + int cbSize; + LPCWSTR lpszDocName; + LPCWSTR lpszOutput; + LPCWSTR lpszDatatype; + DWORD fwType; + } DOCINFOW,*LPDOCINFOW; + +#ifdef UNICODE + typedef DOCINFOW DOCINFO; + typedef LPDOCINFOW LPDOCINFO; +#else + typedef DOCINFOA DOCINFO; + typedef LPDOCINFOA LPDOCINFO; +#endif + +#define DI_APPBANDING 0x00000001 +#define DI_ROPS_READ_DESTINATION 0x00000002 + +#ifdef UNICODE +#define StartDoc StartDocW +#define GetObject GetObjectW +#define TextOut TextOutW +#define ExtTextOut ExtTextOutW +#define PolyTextOut PolyTextOutW +#define GetTextFace GetTextFaceW +#else +#define StartDoc StartDocA +#define GetObject GetObjectA +#define TextOut TextOutA +#define ExtTextOut ExtTextOutA +#define PolyTextOut PolyTextOutA +#define GetTextFace GetTextFaceA +#endif + + WINGDIAPI int WINAPI StartDocA(HDC hdc,CONST DOCINFOA *lpdi); + WINGDIAPI int WINAPI StartDocW(HDC hdc,CONST DOCINFOW *lpdi); + WINGDIAPI int WINAPI EndDoc(HDC hdc); + WINGDIAPI int WINAPI StartPage(HDC hdc); + WINGDIAPI int WINAPI EndPage(HDC hdc); + WINGDIAPI int WINAPI AbortDoc(HDC hdc); + WINGDIAPI int WINAPI SetAbortProc(HDC hdc,ABORTPROC proc); + WINGDIAPI WINBOOL WINAPI AbortPath(HDC hdc); + WINGDIAPI WINBOOL WINAPI ArcTo(HDC hdc,int left,int top,int right,int bottom,int xr1,int yr1,int xr2,int yr2); + WINGDIAPI WINBOOL WINAPI BeginPath(HDC hdc); + WINGDIAPI WINBOOL WINAPI CloseFigure(HDC hdc); + WINGDIAPI WINBOOL WINAPI EndPath(HDC hdc); + WINGDIAPI WINBOOL WINAPI FillPath(HDC hdc); + WINGDIAPI WINBOOL WINAPI FlattenPath(HDC hdc); + WINGDIAPI int WINAPI GetPath(HDC hdc,LPPOINT apt,LPBYTE aj,int cpt); + WINGDIAPI HRGN WINAPI PathToRegion(HDC hdc); + WINGDIAPI WINBOOL WINAPI PolyDraw(HDC hdc,CONST POINT *apt,CONST BYTE *aj,int cpt); + WINGDIAPI WINBOOL WINAPI SelectClipPath(HDC hdc,int mode); + WINGDIAPI int WINAPI SetArcDirection(HDC hdc,int dir); + WINGDIAPI WINBOOL WINAPI SetMiterLimit(HDC hdc,FLOAT limit,PFLOAT old); + WINGDIAPI WINBOOL WINAPI StrokeAndFillPath(HDC hdc); + WINGDIAPI WINBOOL WINAPI StrokePath(HDC hdc); + WINGDIAPI WINBOOL WINAPI WidenPath(HDC hdc); + WINGDIAPI HPEN WINAPI ExtCreatePen(DWORD iPenStyle,DWORD cWidth,CONST LOGBRUSH *plbrush,DWORD cStyle,CONST DWORD *pstyle); + WINGDIAPI WINBOOL WINAPI GetMiterLimit(HDC hdc,PFLOAT plimit); + WINGDIAPI int WINAPI GetArcDirection(HDC hdc); + WINGDIAPI int WINAPI GetObjectA(HANDLE h,int c,LPVOID pv); + WINGDIAPI int WINAPI GetObjectW(HANDLE h,int c,LPVOID pv); + WINGDIAPI WINBOOL WINAPI MoveToEx(HDC hdc,int x,int y,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI TextOutA(HDC hdc,int x,int y,LPCSTR lpString,int c); + WINGDIAPI WINBOOL WINAPI TextOutW(HDC hdc,int x,int y,LPCWSTR lpString,int c); + WINGDIAPI WINBOOL WINAPI ExtTextOutA(HDC hdc,int x,int y,UINT options,CONST RECT *lprect,LPCSTR lpString,UINT c,CONST INT *lpDx); + WINGDIAPI WINBOOL WINAPI ExtTextOutW(HDC hdc,int x,int y,UINT options,CONST RECT *lprect,LPCWSTR lpString,UINT c,CONST INT *lpDx); + WINGDIAPI WINBOOL WINAPI PolyTextOutA(HDC hdc,CONST POLYTEXTA *ppt,int nstrings); + WINGDIAPI WINBOOL WINAPI PolyTextOutW(HDC hdc,CONST POLYTEXTW *ppt,int nstrings); + WINGDIAPI HRGN WINAPI CreatePolygonRgn(CONST POINT *pptl,int cPoint,int iMode); + WINGDIAPI WINBOOL WINAPI DPtoLP(HDC hdc,LPPOINT lppt,int c); + WINGDIAPI WINBOOL WINAPI LPtoDP(HDC hdc,LPPOINT lppt,int c); + WINGDIAPI WINBOOL WINAPI Polygon(HDC hdc,CONST POINT *apt,int cpt); + WINGDIAPI WINBOOL WINAPI Polyline(HDC hdc,CONST POINT *apt,int cpt); + WINGDIAPI WINBOOL WINAPI PolyBezier(HDC hdc,CONST POINT *apt,DWORD cpt); + WINGDIAPI WINBOOL WINAPI PolyBezierTo(HDC hdc,CONST POINT *apt,DWORD cpt); + WINGDIAPI WINBOOL WINAPI PolylineTo(HDC hdc,CONST POINT *apt,DWORD cpt); + WINGDIAPI WINBOOL WINAPI SetViewportExtEx(HDC hdc,int x,int y,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI SetViewportOrgEx(HDC hdc,int x,int y,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI SetWindowExtEx(HDC hdc,int x,int y,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI SetWindowOrgEx(HDC hdc,int x,int y,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI OffsetViewportOrgEx(HDC hdc,int x,int y,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI OffsetWindowOrgEx(HDC hdc,int x,int y,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI ScaleViewportExtEx(HDC hdc,int xn,int dx,int yn,int yd,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI ScaleWindowExtEx(HDC hdc,int xn,int xd,int yn,int yd,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI SetBitmapDimensionEx(HBITMAP hbm,int w,int h,LPSIZE lpsz); + WINGDIAPI WINBOOL WINAPI SetBrushOrgEx(HDC hdc,int x,int y,LPPOINT lppt); + WINGDIAPI int WINAPI GetTextFaceA(HDC hdc,int c,LPSTR lpName); + WINGDIAPI int WINAPI GetTextFaceW(HDC hdc,int c,LPWSTR lpName); + +#define FONTMAPPER_MAX 10 + + typedef struct tagKERNINGPAIR { + WORD wFirst; + WORD wSecond; + int iKernAmount; + } KERNINGPAIR,*LPKERNINGPAIR; + +#ifdef UNICODE +#define GetKerningPairs GetKerningPairsW +#else +#define GetKerningPairs GetKerningPairsA +#endif + + WINGDIAPI DWORD WINAPI GetKerningPairsA(HDC hdc,DWORD nPairs,LPKERNINGPAIR lpKernPair); + WINGDIAPI DWORD WINAPI GetKerningPairsW(HDC hdc,DWORD nPairs,LPKERNINGPAIR lpKernPair); + WINGDIAPI WINBOOL WINAPI GetDCOrgEx(HDC hdc,LPPOINT lppt); + WINGDIAPI WINBOOL WINAPI FixBrushOrgEx(HDC hdc,int x,int y,LPPOINT ptl); + WINGDIAPI WINBOOL WINAPI UnrealizeObject(HGDIOBJ h); + WINGDIAPI WINBOOL WINAPI GdiFlush(); + WINGDIAPI DWORD WINAPI GdiSetBatchLimit(DWORD dw); + WINGDIAPI DWORD WINAPI GdiGetBatchLimit(); + +#define ICM_OFF 1 +#define ICM_ON 2 +#define ICM_QUERY 3 +#define ICM_DONE_OUTSIDEDC 4 + + typedef int (CALLBACK *ICMENUMPROCA)(LPSTR,LPARAM); + typedef int (CALLBACK *ICMENUMPROCW)(LPWSTR,LPARAM); + +#ifdef UNICODE +#define ICMENUMPROC ICMENUMPROCW +#define EnumICMProfiles EnumICMProfilesW +#define UpdateICMRegKey UpdateICMRegKeyW +#define GetLogColorSpace GetLogColorSpaceW +#define CreateColorSpace CreateColorSpaceW +#define GetICMProfile GetICMProfileW +#define SetICMProfile SetICMProfileW +#else +#define ICMENUMPROC ICMENUMPROCA +#define EnumICMProfiles EnumICMProfilesA +#define UpdateICMRegKey UpdateICMRegKeyA +#define GetLogColorSpace GetLogColorSpaceA +#define CreateColorSpace CreateColorSpaceA +#define GetICMProfile GetICMProfileA +#define SetICMProfile SetICMProfileA +#endif + + WINGDIAPI int WINAPI SetICMMode(HDC hdc,int mode); + WINGDIAPI WINBOOL WINAPI CheckColorsInGamut(HDC hdc,LPVOID lpRGBTriple,LPVOID dlpBuffer,DWORD nCount); + WINGDIAPI HCOLORSPACE WINAPI GetColorSpace(HDC hdc); + WINGDIAPI WINBOOL WINAPI GetLogColorSpaceA(HCOLORSPACE hColorSpace,LPLOGCOLORSPACEA lpBuffer,DWORD nSize); + WINGDIAPI WINBOOL WINAPI GetLogColorSpaceW(HCOLORSPACE hColorSpace,LPLOGCOLORSPACEW lpBuffer,DWORD nSize); + WINGDIAPI HCOLORSPACE WINAPI CreateColorSpaceA(LPLOGCOLORSPACEA lplcs); + WINGDIAPI HCOLORSPACE WINAPI CreateColorSpaceW(LPLOGCOLORSPACEW lplcs); + WINGDIAPI HCOLORSPACE WINAPI SetColorSpace(HDC hdc,HCOLORSPACE hcs); + WINGDIAPI WINBOOL WINAPI DeleteColorSpace(HCOLORSPACE hcs); + WINGDIAPI WINBOOL WINAPI GetICMProfileA(HDC hdc,LPDWORD pBufSize,LPSTR pszFilename); + WINGDIAPI WINBOOL WINAPI GetICMProfileW(HDC hdc,LPDWORD pBufSize,LPWSTR pszFilename); + WINGDIAPI WINBOOL WINAPI SetICMProfileA(HDC hdc,LPSTR lpFileName); + WINGDIAPI WINBOOL WINAPI SetICMProfileW(HDC hdc,LPWSTR lpFileName); + WINGDIAPI WINBOOL WINAPI GetDeviceGammaRamp(HDC hdc,LPVOID lpRamp); + WINGDIAPI WINBOOL WINAPI SetDeviceGammaRamp(HDC hdc,LPVOID lpRamp); + WINGDIAPI WINBOOL WINAPI ColorMatchToTarget(HDC hdc,HDC hdcTarget,DWORD action); + WINGDIAPI int WINAPI EnumICMProfilesA(HDC hdc,ICMENUMPROCA proc,LPARAM param); + WINGDIAPI int WINAPI EnumICMProfilesW(HDC hdc,ICMENUMPROCW proc,LPARAM param); + WINGDIAPI WINBOOL WINAPI UpdateICMRegKeyA(DWORD reserved,LPSTR lpszCMID,LPSTR lpszFileName,UINT command); + WINGDIAPI WINBOOL WINAPI UpdateICMRegKeyW(DWORD reserved,LPWSTR lpszCMID,LPWSTR lpszFileName,UINT command); + WINGDIAPI WINBOOL WINAPI ColorCorrectPalette(HDC hdc,HPALETTE hPal,DWORD deFirst,DWORD num); + +#ifndef NOMETAFILE + +#define ENHMETA_SIGNATURE 0x464D4520 +#define ENHMETA_STOCK_OBJECT 0x80000000 + +#define EMR_HEADER 1 +#define EMR_POLYBEZIER 2 +#define EMR_POLYGON 3 +#define EMR_POLYLINE 4 +#define EMR_POLYBEZIERTO 5 +#define EMR_POLYLINETO 6 +#define EMR_POLYPOLYLINE 7 +#define EMR_POLYPOLYGON 8 +#define EMR_SETWINDOWEXTEX 9 +#define EMR_SETWINDOWORGEX 10 +#define EMR_SETVIEWPORTEXTEX 11 +#define EMR_SETVIEWPORTORGEX 12 +#define EMR_SETBRUSHORGEX 13 +#define EMR_EOF 14 +#define EMR_SETPIXELV 15 +#define EMR_SETMAPPERFLAGS 16 +#define EMR_SETMAPMODE 17 +#define EMR_SETBKMODE 18 +#define EMR_SETPOLYFILLMODE 19 +#define EMR_SETROP2 20 +#define EMR_SETSTRETCHBLTMODE 21 +#define EMR_SETTEXTALIGN 22 +#define EMR_SETCOLORADJUSTMENT 23 +#define EMR_SETTEXTCOLOR 24 +#define EMR_SETBKCOLOR 25 +#define EMR_OFFSETCLIPRGN 26 +#define EMR_MOVETOEX 27 +#define EMR_SETMETARGN 28 +#define EMR_EXCLUDECLIPRECT 29 +#define EMR_INTERSECTCLIPRECT 30 +#define EMR_SCALEVIEWPORTEXTEX 31 +#define EMR_SCALEWINDOWEXTEX 32 +#define EMR_SAVEDC 33 +#define EMR_RESTOREDC 34 +#define EMR_SETWORLDTRANSFORM 35 +#define EMR_MODIFYWORLDTRANSFORM 36 +#define EMR_SELECTOBJECT 37 +#define EMR_CREATEPEN 38 +#define EMR_CREATEBRUSHINDIRECT 39 +#define EMR_DELETEOBJECT 40 +#define EMR_ANGLEARC 41 +#define EMR_ELLIPSE 42 +#define EMR_RECTANGLE 43 +#define EMR_ROUNDRECT 44 +#define EMR_ARC 45 +#define EMR_CHORD 46 +#define EMR_PIE 47 +#define EMR_SELECTPALETTE 48 +#define EMR_CREATEPALETTE 49 +#define EMR_SETPALETTEENTRIES 50 +#define EMR_RESIZEPALETTE 51 +#define EMR_REALIZEPALETTE 52 +#define EMR_EXTFLOODFILL 53 +#define EMR_LINETO 54 +#define EMR_ARCTO 55 +#define EMR_POLYDRAW 56 +#define EMR_SETARCDIRECTION 57 +#define EMR_SETMITERLIMIT 58 +#define EMR_BEGINPATH 59 +#define EMR_ENDPATH 60 +#define EMR_CLOSEFIGURE 61 +#define EMR_FILLPATH 62 +#define EMR_STROKEANDFILLPATH 63 +#define EMR_STROKEPATH 64 +#define EMR_FLATTENPATH 65 +#define EMR_WIDENPATH 66 +#define EMR_SELECTCLIPPATH 67 +#define EMR_ABORTPATH 68 + +#define EMR_GDICOMMENT 70 +#define EMR_FILLRGN 71 +#define EMR_FRAMERGN 72 +#define EMR_INVERTRGN 73 +#define EMR_PAINTRGN 74 +#define EMR_EXTSELECTCLIPRGN 75 +#define EMR_BITBLT 76 +#define EMR_STRETCHBLT 77 +#define EMR_MASKBLT 78 +#define EMR_PLGBLT 79 +#define EMR_SETDIBITSTODEVICE 80 +#define EMR_STRETCHDIBITS 81 +#define EMR_EXTCREATEFONTINDIRECTW 82 +#define EMR_EXTTEXTOUTA 83 +#define EMR_EXTTEXTOUTW 84 +#define EMR_POLYBEZIER16 85 +#define EMR_POLYGON16 86 +#define EMR_POLYLINE16 87 +#define EMR_POLYBEZIERTO16 88 +#define EMR_POLYLINETO16 89 +#define EMR_POLYPOLYLINE16 90 +#define EMR_POLYPOLYGON16 91 +#define EMR_POLYDRAW16 92 +#define EMR_CREATEMONOBRUSH 93 +#define EMR_CREATEDIBPATTERNBRUSHPT 94 +#define EMR_EXTCREATEPEN 95 +#define EMR_POLYTEXTOUTA 96 +#define EMR_POLYTEXTOUTW 97 + +#define EMR_SETICMMODE 98 +#define EMR_CREATECOLORSPACE 99 +#define EMR_SETCOLORSPACE 100 +#define EMR_DELETECOLORSPACE 101 +#define EMR_GLSRECORD 102 +#define EMR_GLSBOUNDEDRECORD 103 +#define EMR_PIXELFORMAT 104 +#define EMR_RESERVED_105 105 +#define EMR_RESERVED_106 106 +#define EMR_RESERVED_107 107 +#define EMR_RESERVED_108 108 +#define EMR_RESERVED_109 109 +#define EMR_RESERVED_110 110 +#define EMR_COLORCORRECTPALETTE 111 +#define EMR_SETICMPROFILEA 112 +#define EMR_SETICMPROFILEW 113 +#define EMR_ALPHABLEND 114 +#define EMR_SETLAYOUT 115 +#define EMR_TRANSPARENTBLT 116 +#define EMR_RESERVED_117 117 +#define EMR_GRADIENTFILL 118 +#define EMR_RESERVED_119 119 +#define EMR_RESERVED_120 120 +#define EMR_COLORMATCHTOTARGETW 121 +#define EMR_CREATECOLORSPACEW 122 + +#define EMR_MIN 1 + +#define EMR_MAX 122 + + typedef struct tagEMR { + DWORD iType; + DWORD nSize; + } EMR,*PEMR; + + typedef struct tagEMRTEXT { + POINTL ptlReference; + DWORD nChars; + DWORD offString; + DWORD fOptions; + RECTL rcl; + DWORD offDx; + } EMRTEXT,*PEMRTEXT; + + typedef struct tagABORTPATH { + EMR emr; + } EMRABORTPATH,*PEMRABORTPATH,EMRBEGINPATH,*PEMRBEGINPATH,EMRENDPATH,*PEMRENDPATH,EMRCLOSEFIGURE,*PEMRCLOSEFIGURE,EMRFLATTENPATH,*PEMRFLATTENPATH,EMRWIDENPATH,*PEMRWIDENPATH,EMRSETMETARGN,*PEMRSETMETARGN,EMRSAVEDC,*PEMRSAVEDC,EMRREALIZEPALETTE,*PEMRREALIZEPALETTE; + + typedef struct tagEMRSELECTCLIPPATH { + EMR emr; + DWORD iMode; + } EMRSELECTCLIPPATH,*PEMRSELECTCLIPPATH,EMRSETBKMODE,*PEMRSETBKMODE,EMRSETMAPMODE,*PEMRSETMAPMODE,EMRSETLAYOUT,*PEMRSETLAYOUT, + EMRSETPOLYFILLMODE,*PEMRSETPOLYFILLMODE,EMRSETROP2,*PEMRSETROP2,EMRSETSTRETCHBLTMODE,*PEMRSETSTRETCHBLTMODE,EMRSETICMMODE, + *PEMRSETICMMODE,EMRSETTEXTALIGN,*PEMRSETTEXTALIGN; + + typedef struct tagEMRSETMITERLIMIT { + EMR emr; + FLOAT eMiterLimit; + } EMRSETMITERLIMIT,*PEMRSETMITERLIMIT; + + typedef struct tagEMRRESTOREDC { + EMR emr; + LONG iRelative; + } EMRRESTOREDC,*PEMRRESTOREDC; + + typedef struct tagEMRSETARCDIRECTION { + EMR emr; + DWORD iArcDirection; + + } EMRSETARCDIRECTION,*PEMRSETARCDIRECTION; + + typedef struct tagEMRSETMAPPERFLAGS { + EMR emr; + DWORD dwFlags; + } EMRSETMAPPERFLAGS,*PEMRSETMAPPERFLAGS; + + typedef struct tagEMRSETTEXTCOLOR { + EMR emr; + COLORREF crColor; + } EMRSETBKCOLOR,*PEMRSETBKCOLOR,EMRSETTEXTCOLOR,*PEMRSETTEXTCOLOR; + + typedef struct tagEMRSELECTOBJECT { + EMR emr; + DWORD ihObject; + } EMRSELECTOBJECT,*PEMRSELECTOBJECT,EMRDELETEOBJECT,*PEMRDELETEOBJECT; + + typedef struct tagEMRSELECTPALETTE { + EMR emr; + DWORD ihPal; + } EMRSELECTPALETTE,*PEMRSELECTPALETTE; + + typedef struct tagEMRRESIZEPALETTE { + EMR emr; + DWORD ihPal; + DWORD cEntries; + } EMRRESIZEPALETTE,*PEMRRESIZEPALETTE; + + typedef struct tagEMRSETPALETTEENTRIES { + EMR emr; + DWORD ihPal; + DWORD iStart; + DWORD cEntries; + PALETTEENTRY aPalEntries[1]; + } EMRSETPALETTEENTRIES,*PEMRSETPALETTEENTRIES; + + typedef struct tagEMRSETCOLORADJUSTMENT { + EMR emr; + COLORADJUSTMENT ColorAdjustment; + } EMRSETCOLORADJUSTMENT,*PEMRSETCOLORADJUSTMENT; + + typedef struct tagEMRGDICOMMENT { + EMR emr; + DWORD cbData; + BYTE Data[1]; + } EMRGDICOMMENT,*PEMRGDICOMMENT; + + typedef struct tagEMREOF { + EMR emr; + DWORD nPalEntries; + DWORD offPalEntries; + DWORD nSizeLast; + } EMREOF,*PEMREOF; + + typedef struct tagEMRLINETO { + EMR emr; + POINTL ptl; + } EMRLINETO,*PEMRLINETO,EMRMOVETOEX,*PEMRMOVETOEX; + + typedef struct tagEMROFFSETCLIPRGN { + EMR emr; + POINTL ptlOffset; + } EMROFFSETCLIPRGN,*PEMROFFSETCLIPRGN; + + typedef struct tagEMRFILLPATH { + EMR emr; + RECTL rclBounds; + } EMRFILLPATH,*PEMRFILLPATH,EMRSTROKEANDFILLPATH,*PEMRSTROKEANDFILLPATH,EMRSTROKEPATH,*PEMRSTROKEPATH; + + typedef struct tagEMREXCLUDECLIPRECT { + EMR emr; + RECTL rclClip; + } EMREXCLUDECLIPRECT,*PEMREXCLUDECLIPRECT,EMRINTERSECTCLIPRECT,*PEMRINTERSECTCLIPRECT; + + typedef struct tagEMRSETVIEWPORTORGEX { + EMR emr; + POINTL ptlOrigin; + } EMRSETVIEWPORTORGEX,*PEMRSETVIEWPORTORGEX,EMRSETWINDOWORGEX,*PEMRSETWINDOWORGEX,EMRSETBRUSHORGEX,*PEMRSETBRUSHORGEX; + + typedef struct tagEMRSETVIEWPORTEXTEX { + EMR emr; + SIZEL szlExtent; + } EMRSETVIEWPORTEXTEX,*PEMRSETVIEWPORTEXTEX,EMRSETWINDOWEXTEX,*PEMRSETWINDOWEXTEX; + + typedef struct tagEMRSCALEVIEWPORTEXTEX { + EMR emr; + LONG xNum; + LONG xDenom; + LONG yNum; + LONG yDenom; + } EMRSCALEVIEWPORTEXTEX,*PEMRSCALEVIEWPORTEXTEX,EMRSCALEWINDOWEXTEX,*PEMRSCALEWINDOWEXTEX; + + typedef struct tagEMRSETWORLDTRANSFORM { + EMR emr; + XFORM xform; + } EMRSETWORLDTRANSFORM,*PEMRSETWORLDTRANSFORM; + + typedef struct tagEMRMODIFYWORLDTRANSFORM { + EMR emr; + XFORM xform; + DWORD iMode; + } EMRMODIFYWORLDTRANSFORM,*PEMRMODIFYWORLDTRANSFORM; + + typedef struct tagEMRSETPIXELV { + EMR emr; + POINTL ptlPixel; + COLORREF crColor; + } EMRSETPIXELV,*PEMRSETPIXELV; + + typedef struct tagEMREXTFLOODFILL { + EMR emr; + POINTL ptlStart; + COLORREF crColor; + DWORD iMode; + } EMREXTFLOODFILL,*PEMREXTFLOODFILL; + + typedef struct tagEMRELLIPSE { + EMR emr; + RECTL rclBox; + } EMRELLIPSE,*PEMRELLIPSE,EMRRECTANGLE,*PEMRRECTANGLE; + + typedef struct tagEMRROUNDRECT { + EMR emr; + RECTL rclBox; + SIZEL szlCorner; + } EMRROUNDRECT,*PEMRROUNDRECT; + + typedef struct tagEMRARC { + EMR emr; + RECTL rclBox; + POINTL ptlStart; + POINTL ptlEnd; + } EMRARC,*PEMRARC,EMRARCTO,*PEMRARCTO,EMRCHORD,*PEMRCHORD,EMRPIE,*PEMRPIE; + + typedef struct tagEMRANGLEARC { + EMR emr; + POINTL ptlCenter; + DWORD nRadius; + FLOAT eStartAngle; + FLOAT eSweepAngle; + } EMRANGLEARC,*PEMRANGLEARC; + + typedef struct tagEMRPOLYLINE { + EMR emr; + RECTL rclBounds; + DWORD cptl; + POINTL aptl[1]; + } EMRPOLYLINE,*PEMRPOLYLINE,EMRPOLYBEZIER,*PEMRPOLYBEZIER,EMRPOLYGON,*PEMRPOLYGON,EMRPOLYBEZIERTO,*PEMRPOLYBEZIERTO,EMRPOLYLINETO,*PEMRPOLYLINETO; + + typedef struct tagEMRPOLYLINE16 { + EMR emr; + RECTL rclBounds; + DWORD cpts; + POINTS apts[1]; + } EMRPOLYLINE16,*PEMRPOLYLINE16,EMRPOLYBEZIER16,*PEMRPOLYBEZIER16,EMRPOLYGON16,*PEMRPOLYGON16,EMRPOLYBEZIERTO16,*PEMRPOLYBEZIERTO16,EMRPOLYLINETO16,*PEMRPOLYLINETO16; + + typedef struct tagEMRPOLYDRAW { + EMR emr; + RECTL rclBounds; + DWORD cptl; + POINTL aptl[1]; + BYTE abTypes[1]; + } EMRPOLYDRAW,*PEMRPOLYDRAW; + + typedef struct tagEMRPOLYDRAW16 { + EMR emr; + RECTL rclBounds; + DWORD cpts; + POINTS apts[1]; + BYTE abTypes[1]; + } EMRPOLYDRAW16,*PEMRPOLYDRAW16; + + typedef struct tagEMRPOLYPOLYLINE { + EMR emr; + RECTL rclBounds; + DWORD nPolys; + DWORD cptl; + DWORD aPolyCounts[1]; + POINTL aptl[1]; + } EMRPOLYPOLYLINE,*PEMRPOLYPOLYLINE,EMRPOLYPOLYGON,*PEMRPOLYPOLYGON; + + typedef struct tagEMRPOLYPOLYLINE16 { + EMR emr; + RECTL rclBounds; + DWORD nPolys; + DWORD cpts; + DWORD aPolyCounts[1]; + POINTS apts[1]; + } EMRPOLYPOLYLINE16,*PEMRPOLYPOLYLINE16,EMRPOLYPOLYGON16,*PEMRPOLYPOLYGON16; + + typedef struct tagEMRINVERTRGN { + EMR emr; + RECTL rclBounds; + DWORD cbRgnData; + BYTE RgnData[1]; + } EMRINVERTRGN,*PEMRINVERTRGN,EMRPAINTRGN,*PEMRPAINTRGN; + + typedef struct tagEMRFILLRGN { + EMR emr; + RECTL rclBounds; + DWORD cbRgnData; + DWORD ihBrush; + BYTE RgnData[1]; + } EMRFILLRGN,*PEMRFILLRGN; + + typedef struct tagEMRFRAMERGN { + EMR emr; + RECTL rclBounds; + DWORD cbRgnData; + DWORD ihBrush; + SIZEL szlStroke; + BYTE RgnData[1]; + } EMRFRAMERGN,*PEMRFRAMERGN; + + typedef struct tagEMREXTSELECTCLIPRGN { + EMR emr; + DWORD cbRgnData; + DWORD iMode; + BYTE RgnData[1]; + } EMREXTSELECTCLIPRGN,*PEMREXTSELECTCLIPRGN; + + typedef struct tagEMREXTTEXTOUTA { + EMR emr; + RECTL rclBounds; + DWORD iGraphicsMode; + FLOAT exScale; + FLOAT eyScale; + EMRTEXT emrtext; + } EMREXTTEXTOUTA,*PEMREXTTEXTOUTA,EMREXTTEXTOUTW,*PEMREXTTEXTOUTW; + + typedef struct tagEMRPOLYTEXTOUTA { + EMR emr; + RECTL rclBounds; + DWORD iGraphicsMode; + FLOAT exScale; + FLOAT eyScale; + LONG cStrings; + EMRTEXT aemrtext[1]; + } EMRPOLYTEXTOUTA,*PEMRPOLYTEXTOUTA,EMRPOLYTEXTOUTW,*PEMRPOLYTEXTOUTW; + + typedef struct tagEMRBITBLT { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG cxDest; + LONG cyDest; + DWORD dwRop; + LONG xSrc; + LONG ySrc; + XFORM xformSrc; + COLORREF crBkColorSrc; + DWORD iUsageSrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + } EMRBITBLT,*PEMRBITBLT; + + typedef struct tagEMRSTRETCHBLT { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG cxDest; + LONG cyDest; + DWORD dwRop; + LONG xSrc; + LONG ySrc; + XFORM xformSrc; + COLORREF crBkColorSrc; + DWORD iUsageSrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + LONG cxSrc; + LONG cySrc; + } EMRSTRETCHBLT,*PEMRSTRETCHBLT; + + typedef struct tagEMRMASKBLT { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG cxDest; + LONG cyDest; + DWORD dwRop; + LONG xSrc; + LONG ySrc; + XFORM xformSrc; + COLORREF crBkColorSrc; + DWORD iUsageSrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + LONG xMask; + LONG yMask; + DWORD iUsageMask; + DWORD offBmiMask; + DWORD cbBmiMask; + DWORD offBitsMask; + DWORD cbBitsMask; + } EMRMASKBLT,*PEMRMASKBLT; + + typedef struct tagEMRPLGBLT { + EMR emr; + RECTL rclBounds; + POINTL aptlDest[3]; + LONG xSrc; + LONG ySrc; + LONG cxSrc; + LONG cySrc; + XFORM xformSrc; + COLORREF crBkColorSrc; + DWORD iUsageSrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + LONG xMask; + LONG yMask; + DWORD iUsageMask; + DWORD offBmiMask; + DWORD cbBmiMask; + DWORD offBitsMask; + DWORD cbBitsMask; + } EMRPLGBLT,*PEMRPLGBLT; + + typedef struct tagEMRSETDIBITSTODEVICE { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG xSrc; + LONG ySrc; + LONG cxSrc; + LONG cySrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + DWORD iUsageSrc; + DWORD iStartScan; + DWORD cScans; + } EMRSETDIBITSTODEVICE,*PEMRSETDIBITSTODEVICE; + + typedef struct tagEMRSTRETCHDIBITS { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG xSrc; + LONG ySrc; + LONG cxSrc; + LONG cySrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + DWORD iUsageSrc; + DWORD dwRop; + LONG cxDest; + LONG cyDest; + } EMRSTRETCHDIBITS,*PEMRSTRETCHDIBITS; + + typedef struct tagEMREXTCREATEFONTINDIRECTW { + EMR emr; + DWORD ihFont; + EXTLOGFONTW elfw; + } EMREXTCREATEFONTINDIRECTW,*PEMREXTCREATEFONTINDIRECTW; + + typedef struct tagEMRCREATEPALETTE { + EMR emr; + DWORD ihPal; + LOGPALETTE lgpl; + } EMRCREATEPALETTE,*PEMRCREATEPALETTE; + + typedef struct tagEMRCREATEPEN { + EMR emr; + DWORD ihPen; + LOGPEN lopn; + } EMRCREATEPEN,*PEMRCREATEPEN; + + typedef struct tagEMREXTCREATEPEN { + EMR emr; + DWORD ihPen; + DWORD offBmi; + DWORD cbBmi; + DWORD offBits; + DWORD cbBits; + EXTLOGPEN elp; + } EMREXTCREATEPEN,*PEMREXTCREATEPEN; + + typedef struct tagEMRCREATEBRUSHINDIRECT { + EMR emr; + DWORD ihBrush; + LOGBRUSH32 lb; + } EMRCREATEBRUSHINDIRECT,*PEMRCREATEBRUSHINDIRECT; + + typedef struct tagEMRCREATEMONOBRUSH { + EMR emr; + DWORD ihBrush; + DWORD iUsage; + DWORD offBmi; + DWORD cbBmi; + DWORD offBits; + DWORD cbBits; + } EMRCREATEMONOBRUSH,*PEMRCREATEMONOBRUSH; + + typedef struct tagEMRCREATEDIBPATTERNBRUSHPT { + EMR emr; + DWORD ihBrush; + DWORD iUsage; + DWORD offBmi; + DWORD cbBmi; + DWORD offBits; + DWORD cbBits; + } EMRCREATEDIBPATTERNBRUSHPT,*PEMRCREATEDIBPATTERNBRUSHPT; + + typedef struct tagEMRFORMAT { + DWORD dSignature; + DWORD nVersion; + DWORD cbData; + DWORD offData; + } EMRFORMAT,*PEMRFORMAT; + + typedef struct tagEMRGLSRECORD { + EMR emr; + DWORD cbData; + BYTE Data[1]; + } EMRGLSRECORD,*PEMRGLSRECORD; + + typedef struct tagEMRGLSBOUNDEDRECORD { + EMR emr; + RECTL rclBounds; + DWORD cbData; + BYTE Data[1]; + } EMRGLSBOUNDEDRECORD,*PEMRGLSBOUNDEDRECORD; + + typedef struct tagEMRPIXELFORMAT { + EMR emr; + PIXELFORMATDESCRIPTOR pfd; + } EMRPIXELFORMAT,*PEMRPIXELFORMAT; + + typedef struct tagEMRCREATECOLORSPACE { + EMR emr; + DWORD ihCS; + LOGCOLORSPACEA lcs; + } EMRCREATECOLORSPACE,*PEMRCREATECOLORSPACE; + + typedef struct tagEMRSETCOLORSPACE { + EMR emr; + DWORD ihCS; + } EMRSETCOLORSPACE,*PEMRSETCOLORSPACE,EMRSELECTCOLORSPACE,*PEMRSELECTCOLORSPACE,EMRDELETECOLORSPACE,*PEMRDELETECOLORSPACE; + + typedef struct tagEMREXTESCAPE { + EMR emr; + INT iEscape; + INT cbEscData; + BYTE EscData[1]; + } EMREXTESCAPE,*PEMREXTESCAPE,EMRDRAWESCAPE,*PEMRDRAWESCAPE; + + typedef struct tagEMRNAMEDESCAPE { + EMR emr; + INT iEscape; + INT cbDriver; + INT cbEscData; + BYTE EscData[1]; + } EMRNAMEDESCAPE,*PEMRNAMEDESCAPE; + +#define SETICMPROFILE_EMBEDED 0x00000001 + + typedef struct tagEMRSETICMPROFILE { + EMR emr; + DWORD dwFlags; + DWORD cbName; + DWORD cbData; + BYTE Data[1]; + } EMRSETICMPROFILE,*PEMRSETICMPROFILE,EMRSETICMPROFILEA,*PEMRSETICMPROFILEA,EMRSETICMPROFILEW,*PEMRSETICMPROFILEW; + +#define CREATECOLORSPACE_EMBEDED 0x00000001 + + typedef struct tagEMRCREATECOLORSPACEW { + EMR emr; + DWORD ihCS; + LOGCOLORSPACEW lcs; + DWORD dwFlags; + DWORD cbData; + BYTE Data[1]; + } EMRCREATECOLORSPACEW,*PEMRCREATECOLORSPACEW; + +#define COLORMATCHTOTARGET_EMBEDED 0x00000001 + + typedef struct tagCOLORMATCHTOTARGET { + EMR emr; + DWORD dwAction; + DWORD dwFlags; + DWORD cbName; + DWORD cbData; + BYTE Data[1]; + } EMRCOLORMATCHTOTARGET,*PEMRCOLORMATCHTOTARGET; + + typedef struct tagCOLORCORRECTPALETTE { + EMR emr; + DWORD ihPalette; + DWORD nFirstEntry; + DWORD nPalEntries; + DWORD nReserved; + } EMRCOLORCORRECTPALETTE,*PEMRCOLORCORRECTPALETTE; + + typedef struct tagEMRALPHABLEND { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG cxDest; + LONG cyDest; + DWORD dwRop; + LONG xSrc; + LONG ySrc; + XFORM xformSrc; + COLORREF crBkColorSrc; + DWORD iUsageSrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + LONG cxSrc; + LONG cySrc; + } EMRALPHABLEND,*PEMRALPHABLEND; + + typedef struct tagEMRGRADIENTFILL { + EMR emr; + RECTL rclBounds; + DWORD nVer; + DWORD nTri; + ULONG ulMode; + TRIVERTEX Ver[1]; + } EMRGRADIENTFILL,*PEMRGRADIENTFILL; + + typedef struct tagEMRTRANSPARENTBLT { + EMR emr; + RECTL rclBounds; + LONG xDest; + LONG yDest; + LONG cxDest; + LONG cyDest; + DWORD dwRop; + LONG xSrc; + LONG ySrc; + XFORM xformSrc; + COLORREF crBkColorSrc; + DWORD iUsageSrc; + DWORD offBmiSrc; + DWORD cbBmiSrc; + DWORD offBitsSrc; + DWORD cbBitsSrc; + LONG cxSrc; + LONG cySrc; + } EMRTRANSPARENTBLT,*PEMRTRANSPARENTBLT; + +#define GDICOMMENT_IDENTIFIER 0x43494447 +#define GDICOMMENT_WINDOWS_METAFILE 0x80000001 +#define GDICOMMENT_BEGINGROUP 0x00000002 +#define GDICOMMENT_ENDGROUP 0x00000003 +#define GDICOMMENT_MULTIFORMATS 0x40000004 +#define EPS_SIGNATURE 0x46535045 +#define GDICOMMENT_UNICODE_STRING 0x00000040 +#define GDICOMMENT_UNICODE_END 0x00000080 +#endif + +#ifdef UNICODE +#define wglUseFontBitmaps wglUseFontBitmapsW +#else +#define wglUseFontBitmaps wglUseFontBitmapsA +#endif + + WINGDIAPI WINBOOL WINAPI wglCopyContext(HGLRC,HGLRC,UINT); + WINGDIAPI HGLRC WINAPI wglCreateContext(HDC); + WINGDIAPI HGLRC WINAPI wglCreateLayerContext(HDC,int); + WINGDIAPI WINBOOL WINAPI wglDeleteContext(HGLRC); + WINGDIAPI HGLRC WINAPI wglGetCurrentContext(VOID); + WINGDIAPI HDC WINAPI wglGetCurrentDC(VOID); + WINGDIAPI PROC WINAPI wglGetProcAddress(LPCSTR); + WINGDIAPI WINBOOL WINAPI wglMakeCurrent(HDC,HGLRC); + WINGDIAPI WINBOOL WINAPI wglShareLists(HGLRC,HGLRC); + WINGDIAPI WINBOOL WINAPI wglUseFontBitmapsA(HDC,DWORD,DWORD,DWORD); + WINGDIAPI WINBOOL WINAPI wglUseFontBitmapsW(HDC,DWORD,DWORD,DWORD); + WINGDIAPI WINBOOL WINAPI SwapBuffers(HDC); + + typedef struct _POINTFLOAT { + FLOAT x; + FLOAT y; + } POINTFLOAT,*PPOINTFLOAT; + + typedef struct _GLYPHMETRICSFLOAT { + FLOAT gmfBlackBoxX; + FLOAT gmfBlackBoxY; + POINTFLOAT gmfptGlyphOrigin; + FLOAT gmfCellIncX; + FLOAT gmfCellIncY; + } GLYPHMETRICSFLOAT,*PGLYPHMETRICSFLOAT,*LPGLYPHMETRICSFLOAT; + +#define WGL_FONT_LINES 0 +#define WGL_FONT_POLYGONS 1 + +#ifdef UNICODE +#define wglUseFontOutlines wglUseFontOutlinesW +#else +#define wglUseFontOutlines wglUseFontOutlinesA +#endif + + WINGDIAPI WINBOOL WINAPI wglUseFontOutlinesA(HDC,DWORD,DWORD,DWORD,FLOAT,FLOAT,int,LPGLYPHMETRICSFLOAT); + WINGDIAPI WINBOOL WINAPI wglUseFontOutlinesW(HDC,DWORD,DWORD,DWORD,FLOAT,FLOAT,int,LPGLYPHMETRICSFLOAT); + + typedef struct tagLAYERPLANEDESCRIPTOR { + WORD nSize; + WORD nVersion; + DWORD dwFlags; + BYTE iPixelType; + BYTE cColorBits; + BYTE cRedBits; + BYTE cRedShift; + BYTE cGreenBits; + BYTE cGreenShift; + BYTE cBlueBits; + BYTE cBlueShift; + BYTE cAlphaBits; + BYTE cAlphaShift; + BYTE cAccumBits; + BYTE cAccumRedBits; + BYTE cAccumGreenBits; + BYTE cAccumBlueBits; + BYTE cAccumAlphaBits; + BYTE cDepthBits; + BYTE cStencilBits; + BYTE cAuxBuffers; + BYTE iLayerPlane; + BYTE bReserved; + COLORREF crTransparent; + } LAYERPLANEDESCRIPTOR,*PLAYERPLANEDESCRIPTOR,*LPLAYERPLANEDESCRIPTOR; + +#define LPD_DOUBLEBUFFER 0x00000001 +#define LPD_STEREO 0x00000002 +#define LPD_SUPPORT_GDI 0x00000010 +#define LPD_SUPPORT_OPENGL 0x00000020 +#define LPD_SHARE_DEPTH 0x00000040 +#define LPD_SHARE_STENCIL 0x00000080 +#define LPD_SHARE_ACCUM 0x00000100 +#define LPD_SWAP_EXCHANGE 0x00000200 +#define LPD_SWAP_COPY 0x00000400 +#define LPD_TRANSPARENT 0x00001000 + +#define LPD_TYPE_RGBA 0 +#define LPD_TYPE_COLORINDEX 1 + +#define WGL_SWAP_MAIN_PLANE 0x00000001 +#define WGL_SWAP_OVERLAY1 0x00000002 +#define WGL_SWAP_OVERLAY2 0x00000004 +#define WGL_SWAP_OVERLAY3 0x00000008 +#define WGL_SWAP_OVERLAY4 0x00000010 +#define WGL_SWAP_OVERLAY5 0x00000020 +#define WGL_SWAP_OVERLAY6 0x00000040 +#define WGL_SWAP_OVERLAY7 0x00000080 +#define WGL_SWAP_OVERLAY8 0x00000100 +#define WGL_SWAP_OVERLAY9 0x00000200 +#define WGL_SWAP_OVERLAY10 0x00000400 +#define WGL_SWAP_OVERLAY11 0x00000800 +#define WGL_SWAP_OVERLAY12 0x00001000 +#define WGL_SWAP_OVERLAY13 0x00002000 +#define WGL_SWAP_OVERLAY14 0x00004000 +#define WGL_SWAP_OVERLAY15 0x00008000 +#define WGL_SWAP_UNDERLAY1 0x00010000 +#define WGL_SWAP_UNDERLAY2 0x00020000 +#define WGL_SWAP_UNDERLAY3 0x00040000 +#define WGL_SWAP_UNDERLAY4 0x00080000 +#define WGL_SWAP_UNDERLAY5 0x00100000 +#define WGL_SWAP_UNDERLAY6 0x00200000 +#define WGL_SWAP_UNDERLAY7 0x00400000 +#define WGL_SWAP_UNDERLAY8 0x00800000 +#define WGL_SWAP_UNDERLAY9 0x01000000 +#define WGL_SWAP_UNDERLAY10 0x02000000 +#define WGL_SWAP_UNDERLAY11 0x04000000 +#define WGL_SWAP_UNDERLAY12 0x08000000 +#define WGL_SWAP_UNDERLAY13 0x10000000 +#define WGL_SWAP_UNDERLAY14 0x20000000 +#define WGL_SWAP_UNDERLAY15 0x40000000 + + WINGDIAPI WINBOOL WINAPI wglDescribeLayerPlane(HDC,int,int,UINT,LPLAYERPLANEDESCRIPTOR); + WINGDIAPI int WINAPI wglSetLayerPaletteEntries(HDC,int,int,int,CONST COLORREF *); + WINGDIAPI int WINAPI wglGetLayerPaletteEntries(HDC,int,int,int,COLORREF *); + WINGDIAPI WINBOOL WINAPI wglRealizeLayerPalette(HDC,int,WINBOOL); + WINGDIAPI WINBOOL WINAPI wglSwapLayerBuffers(HDC,UINT); + + typedef struct _WGLSWAP { + HDC hdc; + UINT uiFlags; + } WGLSWAP,*PWGLSWAP,*LPWGLSWAP; + +#define WGL_SWAPMULTIPLE_MAX 16 + + WINGDIAPI DWORD WINAPI wglSwapMultipleBuffers(UINT,CONST WGLSWAP *); +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/winnt.h b/05/tcc-final-old/win32/include/winapi/winnt.h new file mode 100644 index 0000000..4cf685d --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/winnt.h @@ -0,0 +1,5835 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINNT_ +#define _WINNT_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#define ANYSIZE_ARRAY 1 + +//gr #include + +#define RESTRICTED_POINTER + +#ifndef __CRT_UNALIGNED +#define __CRT_UNALIGNED +#endif + +#if defined(__ia64__) || defined(__x86_64) +#define UNALIGNED __CRT_UNALIGNED +#ifdef _WIN64 +#define UNALIGNED64 __CRT_UNALIGNED +#else +#define UNALIGNED64 +#endif +#else +#define UNALIGNED +#define UNALIGNED64 +#endif + +#if !defined(I_X86_) && !defined(_IA64_) && !defined(_AMD64_) && (defined(_X86_) && !defined(__x86_64)) +#define I_X86_ +#endif + +#if !defined(I_X86_) && !defined(_IA64_) && !defined(_AMD64_) && defined(__x86_64) +#define _AMD64_ +#endif + +#if !defined(I_X86_) && !(defined(_X86_) && !defined(__x86_64)) && !defined(_AMD64_) && defined(__ia64__) +#if !defined(_IA64_) +#define _IA64_ +#endif +#endif + + +#ifdef _WIN64 +#define MAX_NATURAL_ALIGNMENT sizeof(ULONGLONG) +#define MEMORY_ALLOCATION_ALIGNMENT 16 +#else +#define MAX_NATURAL_ALIGNMENT sizeof(DWORD) +#define MEMORY_ALLOCATION_ALIGNMENT 8 +#endif + +#ifdef __cplusplus +#define TYPE_ALIGNMENT(t) __alignof__ (t) +#else +#define TYPE_ALIGNMENT(t) FIELD_OFFSET(struct { char x; t test; },test) +#endif + +#ifdef _WIN64 +#ifdef _AMD64_ +#define PROBE_ALIGNMENT(_s) TYPE_ALIGNMENT(DWORD) +#elif defined(_IA64_) +#define PROBE_ALIGNMENT(_s) (TYPE_ALIGNMENT(_s) > TYPE_ALIGNMENT(DWORD) ? TYPE_ALIGNMENT(_s) : TYPE_ALIGNMENT(DWORD)) +#else +#error No Target Architecture +#endif +#define PROBE_ALIGNMENT32(_s) TYPE_ALIGNMENT(DWORD) +#else +#define PROBE_ALIGNMENT(_s) TYPE_ALIGNMENT(DWORD) +#endif + +#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1] + +#include + +#if defined(_X86_) || defined(__ia64__) || defined(__x86_64) +#define DECLSPEC_IMPORT __declspec(dllimport) +#else +#define DECLSPEC_IMPORT +#endif + +#ifndef DECLSPEC_NORETURN +#define DECLSPEC_NORETURN __declspec(noreturn) +#endif + +#ifndef DECLSPEC_ALIGN +#define DECLSPEC_ALIGN(x) __attribute__ ((aligned(x))) +#endif + +#ifndef SYSTEM_CACHE_ALIGNMENT_SIZE +#if defined(_AMD64_) || defined(I_X86_) +#define SYSTEM_CACHE_ALIGNMENT_SIZE 64 +#else +#define SYSTEM_CACHE_ALIGNMENT_SIZE 128 +#endif +#endif + +#ifndef DECLSPEC_CACHEALIGN +#define DECLSPEC_CACHEALIGN DECLSPEC_ALIGN(SYSTEM_CACHE_ALIGNMENT_SIZE) +#endif + +#ifndef DECLSPEC_UUID +#define DECLSPEC_UUID(x) +#endif + +#ifndef DECLSPEC_NOVTABLE +#define DECLSPEC_NOVTABLE +#endif + +#ifndef DECLSPEC_SELECTANY +#define DECLSPEC_SELECTANY __declspec(selectany) +#endif + +#ifndef NOP_FUNCTION +#define NOP_FUNCTION (void)0 +#endif + +#ifndef DECLSPEC_NOINLINE +#define DECLSPEC_NOINLINE +#endif + +#ifndef FORCEINLINE +#define FORCEINLINE static __inline__ +#endif + +#ifndef DECLSPEC_DEPRECATED +#define DECLSPEC_DEPRECATED __declspec(deprecated) +#define DEPRECATE_SUPPORTED +#endif + +#define DECLSPEC_DEPRECATED_DDK +#define PRAGMA_DEPRECATED_DDK 0 + + typedef void *PVOID; + typedef void *PVOID64; + +#define NTAPI __stdcall +#define NTSYSAPI DECLSPEC_IMPORT +#define NTSYSCALLAPI DECLSPEC_IMPORT + +#ifndef VOID +#define VOID void + typedef char CHAR; + typedef short SHORT; + typedef long LONG; +#endif + + typedef wchar_t WCHAR; + typedef WCHAR *PWCHAR,*LPWCH,*PWCH; + typedef CONST WCHAR *LPCWCH,*PCWCH; + typedef WCHAR *NWPSTR,*LPWSTR,*PWSTR; + typedef PWSTR *PZPWSTR; + typedef CONST PWSTR *PCZPWSTR; + typedef WCHAR UNALIGNED *LPUWSTR,*PUWSTR; + typedef CONST WCHAR *LPCWSTR,*PCWSTR; + typedef PCWSTR *PZPCWSTR; + typedef CONST WCHAR UNALIGNED *LPCUWSTR,*PCUWSTR; + typedef CHAR *PCHAR,*LPCH,*PCH; + typedef CONST CHAR *LPCCH,*PCCH; + typedef CHAR *NPSTR,*LPSTR,*PSTR; + typedef PSTR *PZPSTR; + typedef CONST PSTR *PCZPSTR; + typedef CONST CHAR *LPCSTR,*PCSTR; + typedef PCSTR *PZPCSTR; + +#ifdef UNICODE +#ifndef _TCHAR_DEFINED +#define _TCHAR_DEFINED + typedef WCHAR TCHAR,*PTCHAR; + typedef WCHAR TBYTE ,*PTBYTE; +#endif + + typedef LPWSTR LPTCH,PTCH; + typedef LPWSTR PTSTR,LPTSTR; + typedef LPCWSTR PCTSTR,LPCTSTR; + typedef LPUWSTR PUTSTR,LPUTSTR; + typedef LPCUWSTR PCUTSTR,LPCUTSTR; + typedef LPWSTR LP; +#define __TEXT(quote) L##quote +#else +#ifndef _TCHAR_DEFINED +#define _TCHAR_DEFINED + typedef char TCHAR,*PTCHAR; + typedef unsigned char TBYTE ,*PTBYTE; +#endif + + typedef LPSTR LPTCH,PTCH; + typedef LPSTR PTSTR,LPTSTR,PUTSTR,LPUTSTR; + typedef LPCSTR PCTSTR,LPCTSTR,PCUTSTR,LPCUTSTR; +#define __TEXT(quote) quote +#endif + +#define TEXT(quote) __TEXT(quote) + + typedef SHORT *PSHORT; + typedef LONG *PLONG; + + typedef void *HANDLE; +#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name + typedef HANDLE *PHANDLE; + + typedef BYTE FCHAR; + typedef WORD FSHORT; + typedef DWORD FLONG; + +#ifndef _HRESULT_DEFINED +#define _HRESULT_DEFINED + typedef LONG HRESULT; +#endif + +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C extern +#endif + +#define STDMETHODCALLTYPE WINAPI +#define STDMETHODVCALLTYPE __cdecl +#define STDAPICALLTYPE WINAPI +#define STDAPIVCALLTYPE __cdecl +#define STDAPI EXTERN_C HRESULT WINAPI +#define STDAPI_(type) EXTERN_C type WINAPI +#define STDMETHODIMP HRESULT WINAPI +#define STDMETHODIMP_(type) type WINAPI +#define STDAPIV EXTERN_C HRESULT STDAPIVCALLTYPE +#define STDAPIV_(type) EXTERN_C type STDAPIVCALLTYPE +#define STDMETHODIMPV HRESULT STDMETHODVCALLTYPE +#define STDMETHODIMPV_(type) type STDMETHODVCALLTYPE + + typedef char CCHAR; +#ifndef _LCID_DEFINED +#define _LCID_DEFINED +typedef DWORD LCID; +#endif + typedef PDWORD PLCID; +#ifndef _LANGID_DEFINED +#define _LANGID_DEFINED + typedef WORD LANGID; +#endif +#define APPLICATION_ERROR_MASK 0x20000000 +#define ERROR_SEVERITY_SUCCESS 0x00000000 +#define ERROR_SEVERITY_INFORMATIONAL 0x40000000 +#define ERROR_SEVERITY_WARNING 0x80000000 +#define ERROR_SEVERITY_ERROR 0xC0000000 + +#ifdef __ia64__ + __declspec(align(16)) +#endif + typedef struct _FLOAT128 { + __int64 LowPart; + __int64 HighPart; + } FLOAT128; + + typedef FLOAT128 *PFLOAT128; + +#define _ULONGLONG_ +#if((!(defined(_X86_) && !defined(__x86_64)) || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 64))) + typedef __int64 LONGLONG; + typedef unsigned __int64 ULONGLONG; + +#define MAXLONGLONG (0x7fffffffffffffff) +#else + + typedef double LONGLONG; + typedef double ULONGLONG; +#endif + + typedef LONGLONG *PLONGLONG; + typedef ULONGLONG *PULONGLONG; + + typedef LONGLONG USN; + + typedef union _LARGE_INTEGER { + struct { + DWORD LowPart; + LONG HighPart; + }; + struct { + DWORD LowPart; + LONG HighPart; + } u; + LONGLONG QuadPart; + } LARGE_INTEGER; + + typedef LARGE_INTEGER *PLARGE_INTEGER; + + typedef union _ULARGE_INTEGER { + struct { + DWORD LowPart; + DWORD HighPart; + }; + struct { + DWORD LowPart; + DWORD HighPart; + } u; + ULONGLONG QuadPart; + } ULARGE_INTEGER; + + typedef ULARGE_INTEGER *PULARGE_INTEGER; + + typedef struct _LUID { + DWORD LowPart; + LONG HighPart; + } LUID,*PLUID; + +#define _DWORDLONG_ + typedef ULONGLONG DWORDLONG; + typedef DWORDLONG *PDWORDLONG; + +#ifdef RC_INVOKED +#define Int32x32To64(a,b) ((LONGLONG)((LONG)(a)) *(LONGLONG)((LONG)(b))) +#define UInt32x32To64(a,b) ((ULONGLONG)((DWORD)(a)) *(ULONGLONG)((DWORD)(b))) +#define Int64ShrlMod32(a,b) ((ULONGLONG)(a) >> (b)) +#elif (defined(_X86_) && !defined(__x86_64)) +#define Int32x32To64(a,b) (LONGLONG)((LONGLONG)(LONG)(a) *(LONG)(b)) +#define UInt32x32To64(a,b) (ULONGLONG)((ULONGLONG)(DWORD)(a) *(DWORD)(b)) +#define Int64ShrlMod32(a,b) ((DWORDLONG)(a)>>(b)) +#elif defined(__ia64__) || defined(__x86_64) +#define Int32x32To64(a,b) ((LONGLONG)((LONG)(a)) *(LONGLONG)((LONG)(b))) +#define UInt32x32To64(a,b) ((ULONGLONG)((DWORD)(a)) *(ULONGLONG)((DWORD)(b))) +#define Int64ShrlMod32(a,b) ((ULONGLONG)(a) >> (b)) +#else +#error Must define a target architecture. +#endif + +#define Int64ShraMod32(a,b) ((LONGLONG)(a) >> (b)) +#define Int64ShllMod32(a,b) ((ULONGLONG)(a) << (b)) + +#ifdef __cplusplus + extern "C" { +#endif + +#ifdef __x86_64 + +#define RotateLeft8 _rotl8 +#define RotateLeft16 _rotl16 +#define RotateRight8 _rotr8 +#define RotateRight16 _rotr16 + + unsigned char __cdecl _rotl8(unsigned char Value,unsigned char Shift); + unsigned short __cdecl _rotl16(unsigned short Value,unsigned char Shift); + unsigned char __cdecl _rotr8(unsigned char Value,unsigned char Shift); + unsigned short __cdecl _rotr16(unsigned short Value,unsigned char Shift); +#endif + +#define RotateLeft32 _rotl +#define RotateLeft64 _rotl64 +#define RotateRight32 _rotr +#define RotateRight64 _rotr64 + + unsigned int __cdecl _rotl(unsigned int Value,int Shift); + unsigned __int64 __cdecl _rotl64(unsigned __int64 Value,int Shift); + unsigned int __cdecl _rotr(unsigned int Value,int Shift); + unsigned __int64 __cdecl _rotr64(unsigned __int64 Value,int Shift); +#ifdef __cplusplus + } +#endif + +#define ANSI_NULL ((CHAR)0) +#define UNICODE_NULL ((WCHAR)0) +#define UNICODE_STRING_MAX_BYTES ((WORD) 65534) +#define UNICODE_STRING_MAX_CHARS (32767) + +#ifndef _BOOLEAN_ +#define _BOOLEAN_ + typedef BYTE BOOLEAN; +#endif + typedef BOOLEAN *PBOOLEAN; + + typedef struct _LIST_ENTRY { + struct _LIST_ENTRY *Flink; + struct _LIST_ENTRY *Blink; + } LIST_ENTRY,*PLIST_ENTRY,*RESTRICTED_POINTER PRLIST_ENTRY; + + typedef struct _SINGLE_LIST_ENTRY { + struct _SINGLE_LIST_ENTRY *Next; + } SINGLE_LIST_ENTRY,*PSINGLE_LIST_ENTRY; + + typedef struct LIST_ENTRY32 { + DWORD Flink; + DWORD Blink; + } LIST_ENTRY32; + typedef LIST_ENTRY32 *PLIST_ENTRY32; + + typedef struct LIST_ENTRY64 { + ULONGLONG Flink; + ULONGLONG Blink; + } LIST_ENTRY64; + typedef LIST_ENTRY64 *PLIST_ENTRY64; + +#include + +#ifndef __OBJECTID_DEFINED +#define __OBJECTID_DEFINED + typedef struct _OBJECTID { + GUID Lineage; + DWORD Uniquifier; + } OBJECTID; +#endif + +#define MINCHAR 0x80 +#define MAXCHAR 0x7f +#define MINSHORT 0x8000 +#define MAXSHORT 0x7fff +#define MINLONG 0x80000000 +#define MAXLONG 0x7fffffff +#define MAXBYTE 0xff +#define MAXWORD 0xffff +#define MAXDWORD 0xffffffff + +#define FIELD_OFFSET(type,field) ((LONG)(LONG_PTR)&(((type *)0)->field)) +#define RTL_FIELD_SIZE(type,field) (sizeof(((type *)0)->field)) +#define RTL_SIZEOF_THROUGH_FIELD(type,field) (FIELD_OFFSET(type,field) + RTL_FIELD_SIZE(type,field)) +#define RTL_CONTAINS_FIELD(Struct,Size,Field) ((((PCHAR)(&(Struct)->Field)) + sizeof((Struct)->Field)) <= (((PCHAR)(Struct))+(Size))) +#define RTL_NUMBER_OF_V1(A) (sizeof(A)/sizeof((A)[0])) +#define RTL_NUMBER_OF_V2(A) RTL_NUMBER_OF_V1(A) + +#ifdef ENABLE_RTL_NUMBER_OF_V2 +#define RTL_NUMBER_OF(A) RTL_NUMBER_OF_V2(A) +#else +#define RTL_NUMBER_OF(A) RTL_NUMBER_OF_V1(A) +#endif + +#define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A) +#define _ARRAYSIZE(A) RTL_NUMBER_OF_V1(A) + +#define RTL_FIELD_TYPE(type,field) (((type*)0)->field) +#define RTL_NUMBER_OF_FIELD(type,field) (RTL_NUMBER_OF(RTL_FIELD_TYPE(type,field))) +#define RTL_PADDING_BETWEEN_FIELDS(T,F1,F2) ((FIELD_OFFSET(T,F2) > FIELD_OFFSET(T,F1)) ? (FIELD_OFFSET(T,F2) - FIELD_OFFSET(T,F1) - RTL_FIELD_SIZE(T,F1)) : (FIELD_OFFSET(T,F1) - FIELD_OFFSET(T,F2) - RTL_FIELD_SIZE(T,F2))) + +#ifdef __cplusplus +#define RTL_CONST_CAST(type) const_cast +#else +#define RTL_CONST_CAST(type) (type) +#endif + +#define RTL_BITS_OF(sizeOfArg) (sizeof(sizeOfArg) *8) +#define RTL_BITS_OF_FIELD(type,field) (RTL_BITS_OF(RTL_FIELD_TYPE(type,field))) +#define CONTAINING_RECORD(address,type,field) ((type *)((PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field))) + +#define VER_SERVER_NT 0x80000000 +#define VER_WORKSTATION_NT 0x40000000 +#define VER_SUITE_SMALLBUSINESS 0x00000001 +#define VER_SUITE_ENTERPRISE 0x00000002 +#define VER_SUITE_BACKOFFICE 0x00000004 +#define VER_SUITE_COMMUNICATIONS 0x00000008 +#define VER_SUITE_TERMINAL 0x00000010 +#define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020 +#define VER_SUITE_EMBEDDEDNT 0x00000040 +#define VER_SUITE_DATACENTER 0x00000080 +#define VER_SUITE_SINGLEUSERTS 0x00000100 +#define VER_SUITE_PERSONAL 0x00000200 +#define VER_SUITE_BLADE 0x00000400 +#define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800 +#define VER_SUITE_SECURITY_APPLIANCE 0x00001000 +#define VER_SUITE_STORAGE_SERVER 0x00002000 +#define VER_SUITE_COMPUTE_SERVER 0x00004000 + +#define PRODUCT_UNDEFINED 0x0 + +#define PRODUCT_ULTIMATE 0x1 +#define PRODUCT_HOME_BASIC 0x2 +#define PRODUCT_HOME_PREMIUM 0x3 +#define PRODUCT_ENTERPRISE 0x4 +#define PRODUCT_HOME_BASIC_N 0x5 +#define PRODUCT_BUSINESS 0x6 +#define PRODUCT_STANDARD_SERVER 0x7 +#define PRODUCT_DATACENTER_SERVER 0x8 +#define PRODUCT_SMALLBUSINESS_SERVER 0x9 +#define PRODUCT_ENTERPRISE_SERVER 0xa +#define PRODUCT_STARTER 0xb +#define PRODUCT_DATACENTER_SERVER_CORE 0xc +#define PRODUCT_STANDARD_SERVER_CORE 0xd +#define PRODUCT_ENTERPRISE_SERVER_CORE 0xe +#define PRODUCT_ENTERPRISE_SERVER_IA64 0xf +#define PRODUCT_BUSINESS_N 0x10 +#define PRODUCT_WEB_SERVER 0x11 +#define PRODUCT_CLUSTER_SERVER 0x12 +#define PRODUCT_HOME_SERVER 0x13 +#define PRODUCT_STORAGE_EXPRESS_SERVER 0x14 +#define PRODUCT_STORAGE_STANDARD_SERVER 0x15 +#define PRODUCT_STORAGE_WORKGROUP_SERVER 0x16 +#define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x17 +#define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x18 +#define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x19 + +#define PRODUCT_UNLICENSED 0xabcdabcd + +#define LANG_NEUTRAL 0x00 +#define LANG_INVARIANT 0x7f + +#define LANG_AFRIKAANS 0x36 +#define LANG_ALBANIAN 0x1c +#define LANG_ALSATIAN 0x84 +#define LANG_AMHARIC 0x5e +#define LANG_ARABIC 0x01 +#define LANG_ARMENIAN 0x2b +#define LANG_ASSAMESE 0x4d +#define LANG_AZERI 0x2c +#define LANG_BASHKIR 0x6d +#define LANG_BASQUE 0x2d +#define LANG_BELARUSIAN 0x23 +#define LANG_BENGALI 0x45 +#define LANG_BRETON 0x7e +#define LANG_BOSNIAN 0x1a +#define LANG_BOSNIAN_NEUTRAL 0x781a +#define LANG_BULGARIAN 0x02 +#define LANG_CATALAN 0x03 +#define LANG_CHINESE 0x04 +#define LANG_CHINESE_SIMPLIFIED 0x04 +#define LANG_CHINESE_TRADITIONAL 0x7c04 +#define LANG_CORSICAN 0x83 +#define LANG_CROATIAN 0x1a +#define LANG_CZECH 0x05 +#define LANG_DANISH 0x06 +#define LANG_DARI 0x8c +#define LANG_DIVEHI 0x65 +#define LANG_DUTCH 0x13 +#define LANG_ENGLISH 0x09 +#define LANG_ESTONIAN 0x25 +#define LANG_FAEROESE 0x38 +#define LANG_FARSI 0x29 +#define LANG_FILIPINO 0x64 +#define LANG_FINNISH 0x0b +#define LANG_FRENCH 0x0c +#define LANG_FRISIAN 0x62 +#define LANG_GALICIAN 0x56 +#define LANG_GEORGIAN 0x37 +#define LANG_GERMAN 0x07 +#define LANG_GREEK 0x08 +#define LANG_GREENLANDIC 0x6f +#define LANG_GUJARATI 0x47 +#define LANG_HAUSA 0x68 +#define LANG_HEBREW 0x0d +#define LANG_HINDI 0x39 +#define LANG_HUNGARIAN 0x0e +#define LANG_ICELANDIC 0x0f +#define LANG_IGBO 0x70 +#define LANG_INDONESIAN 0x21 +#define LANG_INUKTITUT 0x5d +#define LANG_IRISH 0x3c +#define LANG_ITALIAN 0x10 +#define LANG_JAPANESE 0x11 +#define LANG_KANNADA 0x4b +#define LANG_KASHMIRI 0x60 +#define LANG_KAZAK 0x3f +#define LANG_KHMER 0x53 +#define LANG_KICHE 0x86 +#define LANG_KINYARWANDA 0x87 +#define LANG_KONKANI 0x57 +#define LANG_KOREAN 0x12 +#define LANG_KYRGYZ 0x40 +#define LANG_LAO 0x54 +#define LANG_LATVIAN 0x26 +#define LANG_LITHUANIAN 0x27 +#define LANG_LOWER_SORBIAN 0x2e +#define LANG_LUXEMBOURGISH 0x6e +#define LANG_MACEDONIAN 0x2f +#define LANG_MALAY 0x3e +#define LANG_MALAYALAM 0x4c +#define LANG_MALTESE 0x3a +#define LANG_MANIPURI 0x58 +#define LANG_MAORI 0x81 +#define LANG_MAPUDUNGUN 0x7a +#define LANG_MARATHI 0x4e +#define LANG_MOHAWK 0x7c +#define LANG_MONGOLIAN 0x50 +#define LANG_NEPALI 0x61 +#define LANG_NORWEGIAN 0x14 +#define LANG_OCCITAN 0x82 +#define LANG_ORIYA 0x48 +#define LANG_PASHTO 0x63 +#define LANG_PERSIAN 0x29 +#define LANG_POLISH 0x15 +#define LANG_PORTUGUESE 0x16 +#define LANG_PUNJABI 0x46 +#define LANG_QUECHUA 0x6b +#define LANG_ROMANIAN 0x18 +#define LANG_RUSSIAN 0x19 +#define LANG_SAMI 0x3b +#define LANG_ROMANSH 0x17 +#define LANG_SANSKRIT 0x4f +#define LANG_SERBIAN 0x1a +#define LANG_SERBIAN_NEUTRAL 0x7c1a +#define LANG_SINDHI 0x59 +#define LANG_SINHALESE 0x5b +#define LANG_SLOVAK 0x1b +#define LANG_SLOVENIAN 0x24 +#define LANG_SOTHO 0x6c +#define LANG_SPANISH 0x0a +#define LANG_SWAHILI 0x41 +#define LANG_SWEDISH 0x1d +#define LANG_SYRIAC 0x5a +#define LANG_TAJIK 0x28 +#define LANG_TAMAZIGHT 0x5f +#define LANG_TAMIL 0x49 +#define LANG_TATAR 0x44 +#define LANG_TELUGU 0x4a +#define LANG_THAI 0x1e +#define LANG_TIBETAN 0x51 +#define LANG_TIGRIGNA 0x73 +#define LANG_TSWANA 0x32 +#define LANG_TURKISH 0x1f +#define LANG_TURKMEN 0x42 +#define LANG_UIGHUR 0x80 +#define LANG_UKRAINIAN 0x22 +#define LANG_UPPER_SORBIAN 0x2e +#define LANG_URDU 0x20 +#define LANG_UZBEK 0x43 +#define LANG_VIETNAMESE 0x2a +#define LANG_WELSH 0x52 +#define LANG_WOLOF 0x88 +#define LANG_XHOSA 0x34 +#define LANG_YAKUT 0x85 +#define LANG_YI 0x78 +#define LANG_YORUBA 0x6a +#define LANG_ZULU 0x35 + +#define SUBLANG_NEUTRAL 0x0 +#define SUBLANG_DEFAULT 0x1 +#define SUBLANG_SYS_DEFAULT 0x2 +#define SUBLANG_CUSTOM_DEFAULT 0x3 +#define SUBLANG_CUSTOM_UNSPECIFIED 0x4 +#define SUBLANG_UI_CUSTOM_DEFAULT 0x5 + +#define SUBLANG_ARABIC_SAUDI_ARABIA 0x01 +#define SUBLANG_ARABIC_IRAQ 0x02 +#define SUBLANG_ARABIC_EGYPT 0x03 +#define SUBLANG_ARABIC_LIBYA 0x04 +#define SUBLANG_ARABIC_ALGERIA 0x05 +#define SUBLANG_ARABIC_MOROCCO 0x06 +#define SUBLANG_ARABIC_TUNISIA 0x07 +#define SUBLANG_ARABIC_OMAN 0x08 +#define SUBLANG_ARABIC_YEMEN 0x09 +#define SUBLANG_ARABIC_SYRIA 0x0a +#define SUBLANG_ARABIC_JORDAN 0x0b +#define SUBLANG_ARABIC_LEBANON 0x0c +#define SUBLANG_ARABIC_KUWAIT 0x0d +#define SUBLANG_ARABIC_UAE 0x0e +#define SUBLANG_ARABIC_BAHRAIN 0x0f +#define SUBLANG_ARABIC_QATAR 0x10 +#define SUBLANG_AZERI_LATIN 0x01 +#define SUBLANG_AZERI_CYRILLIC 0x02 +#define SUBLANG_CHINESE_TRADITIONAL 0x01 +#define SUBLANG_CHINESE_SIMPLIFIED 0x02 +#define SUBLANG_CHINESE_HONGKONG 0x03 +#define SUBLANG_CHINESE_SINGAPORE 0x04 +#define SUBLANG_CHINESE_MACAU 0x05 +#define SUBLANG_DUTCH 0x01 +#define SUBLANG_DUTCH_BELGIAN 0x02 +#define SUBLANG_ENGLISH_US 0x01 +#define SUBLANG_ENGLISH_UK 0x02 +#define SUBLANG_ENGLISH_AUS 0x03 +#define SUBLANG_ENGLISH_CAN 0x04 +#define SUBLANG_ENGLISH_NZ 0x05 +#define SUBLANG_ENGLISH_EIRE 0x06 +#define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07 +#define SUBLANG_ENGLISH_JAMAICA 0x08 +#define SUBLANG_ENGLISH_CARIBBEAN 0x09 +#define SUBLANG_ENGLISH_BELIZE 0x0a +#define SUBLANG_ENGLISH_TRINIDAD 0x0b +#define SUBLANG_ENGLISH_ZIMBABWE 0x0c +#define SUBLANG_ENGLISH_PHILIPPINES 0x0d +#define SUBLANG_FRENCH 0x01 +#define SUBLANG_FRENCH_BELGIAN 0x02 +#define SUBLANG_FRENCH_CANADIAN 0x03 +#define SUBLANG_FRENCH_SWISS 0x04 +#define SUBLANG_FRENCH_LUXEMBOURG 0x05 +#define SUBLANG_FRENCH_MONACO 0x06 +#define SUBLANG_GERMAN 0x01 +#define SUBLANG_GERMAN_SWISS 0x02 +#define SUBLANG_GERMAN_AUSTRIAN 0x03 +#define SUBLANG_GERMAN_LUXEMBOURG 0x04 +#define SUBLANG_GERMAN_LIECHTENSTEIN 0x05 +#define SUBLANG_ITALIAN 0x01 +#define SUBLANG_ITALIAN_SWISS 0x02 +#define SUBLANG_KASHMIRI_SASIA 0x02 +#define SUBLANG_KASHMIRI_INDIA 0x02 +#define SUBLANG_KOREAN 0x01 +#define SUBLANG_LITHUANIAN 0x01 +#define SUBLANG_MALAY_MALAYSIA 0x01 +#define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02 +#define SUBLANG_NEPALI_INDIA 0x02 +#define SUBLANG_NORWEGIAN_BOKMAL 0x01 +#define SUBLANG_NORWEGIAN_NYNORSK 0x02 +#define SUBLANG_PORTUGUESE 0x02 +#define SUBLANG_PORTUGUESE_BRAZILIAN 0x01 +#define SUBLANG_SERBIAN_LATIN 0x02 +#define SUBLANG_SERBIAN_CYRILLIC 0x03 +#define SUBLANG_SPANISH 0x01 +#define SUBLANG_SPANISH_MEXICAN 0x02 +#define SUBLANG_SPANISH_MODERN 0x03 +#define SUBLANG_SPANISH_GUATEMALA 0x04 +#define SUBLANG_SPANISH_COSTA_RICA 0x05 +#define SUBLANG_SPANISH_PANAMA 0x06 +#define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07 +#define SUBLANG_SPANISH_VENEZUELA 0x08 +#define SUBLANG_SPANISH_COLOMBIA 0x09 +#define SUBLANG_SPANISH_PERU 0x0a +#define SUBLANG_SPANISH_ARGENTINA 0x0b +#define SUBLANG_SPANISH_ECUADOR 0x0c +#define SUBLANG_SPANISH_CHILE 0x0d +#define SUBLANG_SPANISH_URUGUAY 0x0e +#define SUBLANG_SPANISH_PARAGUAY 0x0f +#define SUBLANG_SPANISH_BOLIVIA 0x10 +#define SUBLANG_SPANISH_EL_SALVADOR 0x11 +#define SUBLANG_SPANISH_HONDURAS 0x12 +#define SUBLANG_SPANISH_NICARAGUA 0x13 +#define SUBLANG_SPANISH_PUERTO_RICO 0x14 +#define SUBLANG_SWEDISH 0x01 +#define SUBLANG_SWEDISH_FINLAND 0x02 +#define SUBLANG_URDU_PAKISTAN 0x01 +#define SUBLANG_URDU_INDIA 0x02 +#define SUBLANG_UZBEK_LATIN 0x01 +#define SUBLANG_UZBEK_CYRILLIC 0x02 + +#define SORT_DEFAULT 0x0 +#define SORT_INVARIANT_MATH 0x1 + +#define SORT_JAPANESE_XJIS 0x0 +#define SORT_JAPANESE_UNICODE 0x1 +#define SORT_JAPANESE_RADICALSTROKE 0x4 + +#define SORT_CHINESE_BIG5 0x0 +#define SORT_CHINESE_PRCP 0x0 +#define SORT_CHINESE_UNICODE 0x1 +#define SORT_CHINESE_PRC 0x2 +#define SORT_CHINESE_BOPOMOFO 0x3 + +#define SORT_KOREAN_KSC 0x0 +#define SORT_KOREAN_UNICODE 0x1 + +#define SORT_GERMAN_PHONE_BOOK 0x1 + +#define SORT_HUNGARIAN_DEFAULT 0x0 +#define SORT_HUNGARIAN_TECHNICAL 0x1 + +#define SORT_GEORGIAN_TRADITIONAL 0x0 +#define SORT_GEORGIAN_MODERN 0x1 + +#define MAKELANGID(p,s) ((((WORD)(s)) << 10) | (WORD)(p)) +#define PRIMARYLANGID(lgid) ((WORD)(lgid) & 0x3ff) +#define SUBLANGID(lgid) ((WORD)(lgid) >> 10) + +#define NLS_VALID_LOCALE_MASK 0x000fffff + +#define MAKELCID(lgid,srtid) ((DWORD)((((DWORD)((WORD)(srtid))) << 16) | ((DWORD)((WORD)(lgid))))) +#define MAKESORTLCID(lgid,srtid,ver) ((DWORD)((MAKELCID(lgid,srtid)) | (((DWORD)((WORD)(ver))) << 20))) +#define LANGIDFROMLCID(lcid) ((WORD)(lcid)) +#define SORTIDFROMLCID(lcid) ((WORD)((((DWORD)(lcid)) >> 16) & 0xf)) +#define SORTVERSIONFROMLCID(lcid) ((WORD)((((DWORD)(lcid)) >> 20) & 0xf)) + +#define LOCALE_NAME_MAX_LENGTH 85 +#define LANG_SYSTEM_DEFAULT (MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT)) +#define LANG_USER_DEFAULT (MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)) + +#define LOCALE_SYSTEM_DEFAULT (MAKELCID(LANG_SYSTEM_DEFAULT,SORT_DEFAULT)) +#define LOCALE_USER_DEFAULT (MAKELCID(LANG_USER_DEFAULT,SORT_DEFAULT)) + +#define LOCALE_NEUTRAL (MAKELCID(MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),SORT_DEFAULT)) + +#define LOCALE_CUSTOM_DEFAULT (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_DEFAULT), SORT_DEFAULT)) +#define LOCALE_CUSTOM_UNSPECIFIED (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_CUSTOM_UNSPECIFIED), SORT_DEFAULT)) +#define LOCALE_CUSTOM_UI_DEFAULT (MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_UI_CUSTOM_DEFAULT), SORT_DEFAULT)) + +#define LOCALE_INVARIANT (MAKELCID(MAKELANGID(LANG_INVARIANT,SUBLANG_NEUTRAL),SORT_DEFAULT)) + +#define UNREFERENCED_PARAMETER(P) (P) +#define DBG_UNREFERENCED_PARAMETER(P) (P) +#define DBG_UNREFERENCED_LOCAL_VARIABLE(V) (V) + +#define DEFAULT_UNREACHABLE + +#ifndef WIN32_NO_STATUS +#define STATUS_WAIT_0 ((DWORD)0x00000000L) +#define STATUS_ABANDONED_WAIT_0 ((DWORD)0x00000080L) +#define STATUS_USER_APC ((DWORD)0x000000C0L) +#define STATUS_TIMEOUT ((DWORD)0x00000102L) +#define STATUS_PENDING ((DWORD)0x00000103L) +#define DBG_EXCEPTION_HANDLED ((DWORD)0x00010001L) +#define DBG_CONTINUE ((DWORD)0x00010002L) +#define STATUS_SEGMENT_NOTIFICATION ((DWORD)0x40000005L) +#define DBG_TERMINATE_THREAD ((DWORD)0x40010003L) +#define DBG_TERMINATE_PROCESS ((DWORD)0x40010004L) +#define DBG_CONTROL_C ((DWORD)0x40010005L) +#define DBG_CONTROL_BREAK ((DWORD)0x40010008L) +#define DBG_COMMAND_EXCEPTION ((DWORD)0x40010009L) +#define STATUS_GUARD_PAGE_VIOLATION ((DWORD)0x80000001L) +#define STATUS_DATATYPE_MISALIGNMENT ((DWORD)0x80000002L) +#define STATUS_BREAKPOINT ((DWORD)0x80000003L) +#define STATUS_SINGLE_STEP ((DWORD)0x80000004L) +#define DBG_EXCEPTION_NOT_HANDLED ((DWORD)0x80010001L) +#define STATUS_ACCESS_VIOLATION ((DWORD)0xC0000005L) +#define STATUS_IN_PAGE_ERROR ((DWORD)0xC0000006L) +#define STATUS_INVALID_HANDLE ((DWORD)0xC0000008L) +#define STATUS_NO_MEMORY ((DWORD)0xC0000017L) +#define STATUS_ILLEGAL_INSTRUCTION ((DWORD)0xC000001DL) +#define STATUS_NONCONTINUABLE_EXCEPTION ((DWORD)0xC0000025L) +#define STATUS_INVALID_DISPOSITION ((DWORD)0xC0000026L) +#define STATUS_ARRAY_BOUNDS_EXCEEDED ((DWORD)0xC000008CL) +#define STATUS_FLOAT_DENORMAL_OPERAND ((DWORD)0xC000008DL) +#define STATUS_FLOAT_DIVIDE_BY_ZERO ((DWORD)0xC000008EL) +#define STATUS_FLOAT_INEXACT_RESULT ((DWORD)0xC000008FL) +#define STATUS_FLOAT_INVALID_OPERATION ((DWORD)0xC0000090L) +#define STATUS_FLOAT_OVERFLOW ((DWORD)0xC0000091L) +#define STATUS_FLOAT_STACK_CHECK ((DWORD)0xC0000092L) +#define STATUS_FLOAT_UNDERFLOW ((DWORD)0xC0000093L) +#define STATUS_INTEGER_DIVIDE_BY_ZERO ((DWORD)0xC0000094L) +#define STATUS_INTEGER_OVERFLOW ((DWORD)0xC0000095L) +#define STATUS_PRIVILEGED_INSTRUCTION ((DWORD)0xC0000096L) +#define STATUS_STACK_OVERFLOW ((DWORD)0xC00000FDL) +#define STATUS_CONTROL_C_EXIT ((DWORD)0xC000013AL) +#define STATUS_FLOAT_MULTIPLE_FAULTS ((DWORD)0xC00002B4L) +#define STATUS_FLOAT_MULTIPLE_TRAPS ((DWORD)0xC00002B5L) +#define STATUS_REG_NAT_CONSUMPTION ((DWORD)0xC00002C9L) +#define STATUS_SXS_EARLY_DEACTIVATION ((DWORD)0xC015000FL) +#define STATUS_SXS_INVALID_DEACTIVATION ((DWORD)0xC0150010L) +#endif + +#define MAXIMUM_WAIT_OBJECTS 64 +#define MAXIMUM_SUSPEND_COUNT MAXCHAR + + typedef ULONG_PTR KSPIN_LOCK; + typedef KSPIN_LOCK *PKSPIN_LOCK; + +#ifdef _AMD64_ + +#if defined(__x86_64) && !defined(RC_INVOKED) + +#ifdef __cplusplus + extern "C" { +#endif + +#define BitTest _bittest +#define BitTestAndComplement _bittestandcomplement +#define BitTestAndSet _bittestandset +#define BitTestAndReset _bittestandreset +#define InterlockedBitTestAndSet _interlockedbittestandset +#define InterlockedBitTestAndReset _interlockedbittestandreset +#define BitTest64 _bittest64 +#define BitTestAndComplement64 _bittestandcomplement64 +#define BitTestAndSet64 _bittestandset64 +#define BitTestAndReset64 _bittestandreset64 +#define InterlockedBitTestAndSet64 _interlockedbittestandset64 +#define InterlockedBitTestAndReset64 _interlockedbittestandreset64 + + __CRT_INLINE BOOLEAN _bittest(LONG const *Base,LONG Offset) { + int old = 0; + __asm__ __volatile__("btl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittestandcomplement(LONG *Base,LONG Offset) { + int old = 0; + __asm__ __volatile__("btcl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN InterlockedBitTestAndComplement(LONG *Base,LONG Bit) { + int old = 0; + __asm__ __volatile__("lock ; btcl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Bit)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittestandset(LONG *Base,LONG Offset) { + int old = 0; + __asm__ __volatile__("btsl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittestandreset(LONG *Base,LONG Offset) { + int old = 0; + __asm__ __volatile__("btrl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _interlockedbittestandset(LONG *Base,LONG Offset) { + int old = 0; + __asm__ __volatile__("lock ; btsl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _interlockedbittestandreset(LONG *Base,LONG Offset) { + int old = 0; + __asm__ __volatile__("lock ; btrl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittest64(LONG64 const *Base,LONG64 Offset) { + int old = 0; + __asm__ __volatile__("btq %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittestandcomplement64(LONG64 *Base,LONG64 Offset) { + int old = 0; + __asm__ __volatile__("btcq %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittestandset64(LONG64 *Base,LONG64 Offset) { + int old = 0; + __asm__ __volatile__("btsq %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _bittestandreset64(LONG64 *Base,LONG64 Offset) { + int old = 0; + __asm__ __volatile__("btrq %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _interlockedbittestandset64(LONG64 *Base,LONG64 Offset) { + int old = 0; + __asm__ __volatile__("lock ; btsq %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } + __CRT_INLINE BOOLEAN _interlockedbittestandreset64(LONG64 *Base,LONG64 Offset) { + int old = 0; + __asm__ __volatile__("lock ; btrq %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long long *) Base)) + :"Ir" (Offset)); + return (BOOLEAN) (old!=0); + } +#define BitScanForward _BitScanForward +#define BitScanReverse _BitScanReverse +#define BitScanForward64 _BitScanForward64 +#define BitScanReverse64 _BitScanReverse64 + + __CRT_INLINE BOOLEAN _BitScanForward(DWORD *Index,DWORD Mask) { + __asm__ __volatile__("bsfl %1,%0" : "=r" (Mask),"=m" ((*(volatile long *)Index))); + return Mask!=0; + } + __CRT_INLINE BOOLEAN _BitScanReverse(DWORD *Index,DWORD Mask) { + __asm__ __volatile__("bsrl %1,%0" : "=r" (Mask),"=m" ((*(volatile long *)Index))); + return Mask!=0; + } + __CRT_INLINE BOOLEAN _BitScanForward64(DWORD *Index,DWORD64 Mask) { + __asm__ __volatile__("bsfq %1,%0" : "=r" (Mask),"=m" ((*(volatile long long *)Index))); + return Mask!=0; + } + __CRT_INLINE BOOLEAN _BitScanReverse64(DWORD *Index,DWORD64 Mask) { + __asm__ __volatile__("bsrq %1,%0" : "=r" (Mask),"=m" ((*(volatile long long *)Index))); + return Mask!=0; + } + +#define InterlockedIncrement16 _InterlockedIncrement16 +#define InterlockedDecrement16 _InterlockedDecrement16 +#define InterlockedCompareExchange16 _InterlockedCompareExchange16 + +#define InterlockedAnd _InterlockedAnd +#define InterlockedOr _InterlockedOr +#define InterlockedXor _InterlockedXor +#define InterlockedIncrement _InterlockedIncrement +#define InterlockedIncrementAcquire InterlockedIncrement +#define InterlockedIncrementRelease InterlockedIncrement +#define InterlockedDecrement _InterlockedDecrement +#define InterlockedDecrementAcquire InterlockedDecrement +#define InterlockedDecrementRelease InterlockedDecrement +#define InterlockedAdd _InterlockedAdd +#define InterlockedExchange _InterlockedExchange +#define InterlockedExchangeAdd _InterlockedExchangeAdd +#define InterlockedCompareExchange _InterlockedCompareExchange +#define InterlockedCompareExchangeAcquire InterlockedCompareExchange +#define InterlockedCompareExchangeRelease InterlockedCompareExchange + +#define InterlockedAnd64 _InterlockedAnd64 +#define InterlockedAndAffinity InterlockedAnd64 +#define InterlockedOr64 _InterlockedOr64 +#define InterlockedOrAffinity InterlockedOr64 +#define InterlockedXor64 _InterlockedXor64 +#define InterlockedIncrement64 _InterlockedIncrement64 +#define InterlockedDecrement64 _InterlockedDecrement64 +#define InterlockedAdd64 _InterlockedAdd64 +#define InterlockedExchange64 _InterlockedExchange64 +#define InterlockedExchangeAcquire64 InterlockedExchange64 +#define InterlockedExchangeAdd64 _InterlockedExchangeAdd64 +#define InterlockedCompareExchange64 _InterlockedCompareExchange64 +#define InterlockedCompareExchangeAcquire64 InterlockedCompareExchange64 +#define InterlockedCompareExchangeRelease64 InterlockedCompareExchange64 + +#define InterlockedExchangePointer _InterlockedExchangePointer +#define InterlockedCompareExchangePointer _InterlockedCompareExchangePointer +#define InterlockedCompareExchangePointerAcquire _InterlockedCompareExchangePointer +#define InterlockedCompareExchangePointerRelease _InterlockedCompareExchangePointer + +#define InterlockedExchangeAddSizeT(a,b) InterlockedExchangeAdd64((LONG64 *)a,b) +#define InterlockedIncrementSizeT(a) InterlockedIncrement64((LONG64 *)a) +#define InterlockedDecrementSizeT(a) InterlockedDecrement64((LONG64 *)a) + + __CRT_INLINE SHORT InterlockedIncrement16(SHORT volatile *Addend) { + unsigned char c; + unsigned char s; + __asm__ __volatile__( + "lock ; addw $1,%0; sete %1 ; sets %2" + :"=m" (*Addend), "=qm" (c), "=qm" (s) + :"m" (*Addend) : "memory"); + return (c != 0 ? 0 : (s != 0 ? -1 : 1)); + } + __CRT_INLINE SHORT InterlockedDecrement16(SHORT volatile *Addend) { + unsigned char c; + unsigned char s; + __asm__ __volatile__( + "lock ; subw $1,%0; sete %1 ; sets %2" + :"=m" (*Addend), "=qm" (c), "=qm" (s) + :"m" (*Addend) : "memory"); + return (c != 0 ? 0 : (s != 0 ? -1 : 1)); + } + __CRT_INLINE SHORT InterlockedCompareExchange16(SHORT volatile *Destination,SHORT ExChange,SHORT Comperand) { + SHORT prev; + __asm__ __volatile__("lock ; cmpxchgw %w1,%2" + :"=a"(prev) + :"q"(ExChange), "m"(*Destination), "0"(Comperand) + : "memory"); + return prev; + } + __CRT_INLINE LONG InterlockedAnd(LONG volatile *Destination,LONG Value) { + __asm__ __volatile__("lock ; andl %0,%1" + : :"r"(Value),"m"(*Destination) + : "memory"); + return *Destination; + } + __CRT_INLINE LONG InterlockedOr(LONG volatile *Destination,LONG Value) { + __asm__ __volatile__("lock ; orl %0,%1" + : : "r"(Value),"m"(*Destination) : "memory"); + return *Destination; + } + __CRT_INLINE LONG InterlockedXor(LONG volatile *Destination,LONG Value) { + __asm__ __volatile__("lock ; xorl %0,%1" + : : "r"(Value),"m"(*Destination) : "memory"); + return *Destination; + } + // $$$$ + __CRT_INLINE LONG64 InterlockedAnd64(LONG64 volatile *Destination,LONG64 Value) { + __asm__ __volatile__("lock ; andq %0,%1" + : : "r"(Value),"m"(*Destination) : "memory"); + return *Destination; + } + __CRT_INLINE LONG64 InterlockedOr64(LONG64 volatile *Destination,LONG64 Value) { + __asm__ __volatile__("lock ; orq %0,%1" + : : "r"(Value),"m"(*Destination) : "memory"); + return *Destination; + } + __CRT_INLINE LONG64 InterlockedXor64(LONG64 volatile *Destination,LONG64 Value) { + __asm__ __volatile__("lock ; xorq %0,%1" + : : "r"(Value),"m"(*Destination) : "memory"); + return *Destination; + } + __CRT_INLINE LONG InterlockedIncrement(LONG volatile *Addend) { + unsigned char c; + unsigned char s; + __asm__ __volatile__( + "lock ; addl $1,%0; sete %1 ; sets %2" + :"=m" (*Addend), "=qm" (c), "=qm" (s) + :"m" (*Addend) : "memory"); + return (c != 0 ? 0 : (s != 0 ? -1 : 1)); + } + __CRT_INLINE LONG InterlockedDecrement(LONG volatile *Addend) { + unsigned char c; + unsigned char s; + __asm__ __volatile__( + "lock ; subl $1,%0; sete %1 ; sets %2" + :"=m" (*Addend), "=qm" (c), "=qm" (s) + :"m" (*Addend) : "memory"); + return (c != 0 ? 0 : (s != 0 ? -1 : 1)); + } + __CRT_INLINE LONG InterlockedExchange(LONG volatile *Target,LONG Value) { + __asm__ __volatile("lock ; xchgl %0,%1" + : "=r"(Value) + : "m"(*Target),"0"(Value) + : "memory"); + return Value; + } + LONG InterlockedExchangeAdd(LONG volatile *Addend,LONG Value); + +#ifndef _X86AMD64_ + __CRT_INLINE LONG InterlockedAdd(LONG volatile *Addend,LONG Value) { return InterlockedExchangeAdd(Addend,Value) + Value; } +#endif + __CRT_INLINE LONG InterlockedCompareExchange(LONG volatile *Destination,LONG ExChange,LONG Comperand) { + LONG prev; + __asm__ __volatile__("lock ; cmpxchgl %1,%2" : "=a" (prev) : "q" (ExChange),"m" (*Destination), "0" (Comperand) : "memory"); + return prev; + } + __CRT_INLINE LONG64 InterlockedIncrement64(LONG64 volatile *Addend) { + unsigned char c; + unsigned char s; + __asm__ __volatile__( + "lock ; addq $1,%0; sete %1 ; sets %2" + :"=m" (*Addend), "=qm" (c), "=qm" (s) + :"m" (*Addend) : "memory"); + return (c != 0 ? 0 : (s != 0 ? -1 : 1)); + } + __CRT_INLINE LONG64 InterlockedDecrement64(LONG64 volatile *Addend) { + unsigned char c; + unsigned char s; + __asm__ __volatile__( + "lock ; subq $1,%0; sete %1 ; sets %2" + :"=m" (*Addend), "=qm" (c), "=qm" (s) + :"m" (*Addend) : "memory"); + return (c != 0 ? 0 : (s != 0 ? -1 : 1)); + } + __CRT_INLINE LONG64 InterlockedExchange64(LONG64 volatile *Target,LONG64 Value) { + __asm__ __volatile("lock ; xchgq %0,%1" + : "=r"(Value) + : "m"(*Target),"0"(Value) + : "memory"); + return Value; + } + LONG64 InterlockedExchangeAdd64(LONG64 volatile *Addend,LONG64 Value); + +#ifndef _X86AMD64_ + __CRT_INLINE LONG64 InterlockedAdd64(LONG64 volatile *Addend,LONG64 Value) { return InterlockedExchangeAdd64(Addend,Value) + Value; } +#endif + + __CRT_INLINE LONG64 InterlockedCompareExchange64(LONG64 volatile *Destination,LONG64 ExChange,LONG64 Comperand) { + LONG64 prev; + __asm__ __volatile__("lock ; cmpxchgq %1,%2" : "=a" (prev) : "q" (ExChange),"m" (*Destination), "0" (Comperand) : "memory"); + return prev; + } + __CRT_INLINE PVOID InterlockedCompareExchangePointer(PVOID volatile *Destination,PVOID ExChange,PVOID Comperand) { + PVOID prev; + __asm__ __volatile__("lock ; cmpxchgq %1,%2" : "=a" (prev) : "q" (ExChange),"m" (*Destination), "0" (Comperand) : "memory"); + return prev; + } + __CRT_INLINE PVOID InterlockedExchangePointer(PVOID volatile *Target,PVOID Value) { + __asm__ __volatile("lock ; xchgq %0,%1" + : "=r"(Value) + : "m"(*Target),"0"(Value) + : "memory"); + return Value; + } + +#define CacheLineFlush(Address) _mm_clflush(Address) + + VOID _ReadWriteBarrier(VOID); + +#define FastFence __faststorefence +#define LoadFence _mm_lfence +#define MemoryFence _mm_mfence +#define StoreFence _mm_sfence + + VOID __faststorefence(VOID); + VOID _m_prefetchw(volatile CONST VOID *Source); + +//!__TINYC__: #include + +#define YieldProcessor _mm_pause +#define MemoryBarrier __faststorefence +#define PreFetchCacheLine(l,a) _mm_prefetch((CHAR CONST *) a,l) +#define PrefetchForWrite(p) _m_prefetchw(p) +#define ReadForWriteAccess(p) (_m_prefetchw(p),*(p)) + +#define PF_TEMPORAL_LEVEL_1 _MM_HINT_T0 +#define PF_TEMPORAL_LEVEL_2 _MM_HINT_T1 +#define PF_TEMPORAL_LEVEL_3 _MM_HINT_T2 +#define PF_NON_TEMPORAL_LEVEL_ALL _MM_HINT_NTA + +#define ReadMxCsr _mm_getcsr +#define WriteMxCsr _mm_setcsr + + VOID __int2c(VOID); + +#define DbgRaiseAssertionFailure() __int2c() +#define GetCallersEflags() __getcallerseflags() + + unsigned __int32 __getcallerseflags(VOID); + +#define GetSegmentLimit __segmentlimit + + DWORD __segmentlimit(DWORD Selector); + +#define ReadTimeStampCounter() __rdtsc() + + DWORD64 __rdtsc(VOID); + VOID __movsb(PBYTE Destination,BYTE const *Source,SIZE_T Count); + VOID __movsw(PWORD Destination,WORD const *Source,SIZE_T Count); + VOID __movsd(PDWORD Destination,DWORD const *Source,SIZE_T Count); + VOID __movsq(PDWORD64 Destination,DWORD64 const *Source,SIZE_T Count); + VOID __stosb(PBYTE Destination,BYTE Value,SIZE_T Count); + VOID __stosw(PWORD Destination,WORD Value,SIZE_T Count); + VOID __stosd(PDWORD Destination,DWORD Value,SIZE_T Count); + VOID __stosq(PDWORD64 Destination,DWORD64 Value,SIZE_T Count); + +#define MultiplyHigh __mulh +#define UnsignedMultiplyHigh __umulh + + LONGLONG MultiplyHigh(LONGLONG Multiplier,LONGLONG Multiplicand); + ULONGLONG UnsignedMultiplyHigh(ULONGLONG Multiplier,ULONGLONG Multiplicand); + +#define ShiftLeft128 __shiftleft128 +#define ShiftRight128 __shiftright128 + + DWORD64 ShiftLeft128(DWORD64 LowPart,DWORD64 HighPart,BYTE Shift); + DWORD64 ShiftRight128(DWORD64 LowPart,DWORD64 HighPart,BYTE Shift); + +#define Multiply128 _mul128 + + LONG64 Multiply128(LONG64 Multiplier,LONG64 Multiplicand,LONG64 *HighProduct); + +#define UnsignedMultiply128 _umul128 + + DWORD64 UnsignedMultiply128(DWORD64 Multiplier,DWORD64 Multiplicand,DWORD64 *HighProduct); + + __CRT_INLINE LONG64 MultiplyExtract128(LONG64 Multiplier,LONG64 Multiplicand,BYTE Shift) { + LONG64 extractedProduct; + LONG64 highProduct; + LONG64 lowProduct; + lowProduct = Multiply128(Multiplier,Multiplicand,&highProduct); + extractedProduct = (LONG64)ShiftRight128((LONG64)lowProduct,(LONG64)highProduct,Shift); + return extractedProduct; + } + + __CRT_INLINE DWORD64 UnsignedMultiplyExtract128(DWORD64 Multiplier,DWORD64 Multiplicand,BYTE Shift) { + DWORD64 extractedProduct; + DWORD64 highProduct; + DWORD64 lowProduct; + lowProduct = UnsignedMultiply128(Multiplier,Multiplicand,&highProduct); + extractedProduct = ShiftRight128(lowProduct,highProduct,Shift); + return extractedProduct; + } + + __CRT_INLINE BYTE __readgsbyte(DWORD Offset) { + BYTE ret; + __asm__ volatile ("movb %%gs:%1,%0" + : "=r" (ret) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + return ret; + } + __CRT_INLINE WORD __readgsword(DWORD Offset) { + WORD ret; + __asm__ volatile ("movw %%gs:%1,%0" + : "=r" (ret) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + return ret; + } + __CRT_INLINE DWORD __readgsdword(DWORD Offset) { + DWORD ret; + __asm__ volatile ("movl %%gs:%1,%0" + : "=r" (ret) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + return ret; + } + __CRT_INLINE DWORD64 __readgsqword(DWORD Offset) { + void *ret; + __asm__ volatile ("movq %%gs:%1,%0" + : "=r" (ret) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + return (DWORD64) ret; + } + __CRT_INLINE VOID __writegsbyte(DWORD Offset,BYTE Data) { + __asm__ volatile ("movb %0,%%gs:%1" + : "=r" (Data) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + } + __CRT_INLINE VOID __writegsword(DWORD Offset,WORD Data) { + __asm__ volatile ("movw %0,%%gs:%1" + : "=r" (Data) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + } + __CRT_INLINE VOID __writegsdword(DWORD Offset,DWORD Data) { + __asm__ volatile ("movl %0,%%gs:%1" + : "=r" (Data) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + } + __CRT_INLINE VOID __writegsqword(DWORD Offset,DWORD64 Data) { + __asm__ volatile ("movq %0,%%gs:%1" + : "=r" (Data) ,"=m" ((*(volatile long *) (DWORD64) Offset))); + } + +#ifdef __cplusplus + } +#endif +#endif + +#define EXCEPTION_READ_FAULT 0 +#define EXCEPTION_WRITE_FAULT 1 +#define EXCEPTION_EXECUTE_FAULT 8 + +#if !defined(RC_INVOKED) + +#define CONTEXT_AMD64 0x100000 + +#define CONTEXT_CONTROL (CONTEXT_AMD64 | 0x1L) +#define CONTEXT_INTEGER (CONTEXT_AMD64 | 0x2L) +#define CONTEXT_SEGMENTS (CONTEXT_AMD64 | 0x4L) +#define CONTEXT_FLOATING_POINT (CONTEXT_AMD64 | 0x8L) +#define CONTEXT_DEBUG_REGISTERS (CONTEXT_AMD64 | 0x10L) + +#define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT) +#define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS) + +#define CONTEXT_EXCEPTION_ACTIVE 0x8000000 +#define CONTEXT_SERVICE_ACTIVE 0x10000000 +#define CONTEXT_EXCEPTION_REQUEST 0x40000000 +#define CONTEXT_EXCEPTION_REPORTING 0x80000000 +#endif + +#define INITIAL_MXCSR 0x1f80 +#define INITIAL_FPCSR 0x027f + + typedef DECLSPEC_ALIGN(16) struct _M128A { + ULONGLONG Low; + LONGLONG High; + } M128A,*PM128A; + + typedef struct _XMM_SAVE_AREA32 { + WORD ControlWord; + WORD StatusWord; + BYTE TagWord; + BYTE Reserved1; + WORD ErrorOpcode; + DWORD ErrorOffset; + WORD ErrorSelector; + WORD Reserved2; + DWORD DataOffset; + WORD DataSelector; + WORD Reserved3; + DWORD MxCsr; + DWORD MxCsr_Mask; + M128A FloatRegisters[8]; + M128A XmmRegisters[16]; + BYTE Reserved4[96]; + } XMM_SAVE_AREA32,*PXMM_SAVE_AREA32; + +#define LEGACY_SAVE_AREA_LENGTH sizeof(XMM_SAVE_AREA32) + + typedef DECLSPEC_ALIGN(16) struct _CONTEXT { + DWORD64 P1Home; + DWORD64 P2Home; + DWORD64 P3Home; + DWORD64 P4Home; + DWORD64 P5Home; + DWORD64 P6Home; + DWORD ContextFlags; + DWORD MxCsr; + WORD SegCs; + WORD SegDs; + WORD SegEs; + WORD SegFs; + WORD SegGs; + WORD SegSs; + DWORD EFlags; + DWORD64 Dr0; + DWORD64 Dr1; + DWORD64 Dr2; + DWORD64 Dr3; + DWORD64 Dr6; + DWORD64 Dr7; + DWORD64 Rax; + DWORD64 Rcx; + DWORD64 Rdx; + DWORD64 Rbx; + DWORD64 Rsp; + DWORD64 Rbp; + DWORD64 Rsi; + DWORD64 Rdi; + DWORD64 R8; + DWORD64 R9; + DWORD64 R10; + DWORD64 R11; + DWORD64 R12; + DWORD64 R13; + DWORD64 R14; + DWORD64 R15; + DWORD64 Rip; + union { + XMM_SAVE_AREA32 FltSave; + XMM_SAVE_AREA32 FloatSave; + struct { + M128A Header[2]; + M128A Legacy[8]; + M128A Xmm0; + M128A Xmm1; + M128A Xmm2; + M128A Xmm3; + M128A Xmm4; + M128A Xmm5; + M128A Xmm6; + M128A Xmm7; + M128A Xmm8; + M128A Xmm9; + M128A Xmm10; + M128A Xmm11; + M128A Xmm12; + M128A Xmm13; + M128A Xmm14; + M128A Xmm15; + }; + }; + M128A VectorRegister[26]; + DWORD64 VectorControl; + DWORD64 DebugControl; + DWORD64 LastBranchToRip; + DWORD64 LastBranchFromRip; + DWORD64 LastExceptionToRip; + DWORD64 LastExceptionFromRip; + } CONTEXT,*PCONTEXT; + +#define RUNTIME_FUNCTION_INDIRECT 0x1 + + typedef struct _RUNTIME_FUNCTION { + DWORD BeginAddress; + DWORD EndAddress; + DWORD UnwindData; + } RUNTIME_FUNCTION,*PRUNTIME_FUNCTION; + + typedef PRUNTIME_FUNCTION (*PGET_RUNTIME_FUNCTION_CALLBACK)(DWORD64 ControlPc,PVOID Context); + typedef DWORD (*POUT_OF_PROCESS_FUNCTION_TABLE_CALLBACK)(HANDLE Process,PVOID TableAddress,PDWORD Entries,PRUNTIME_FUNCTION *Functions); + +#define OUT_OF_PROCESS_FUNCTION_TABLE_CALLBACK_EXPORT_NAME "OutOfProcessFunctionTableCallback" + + NTSYSAPI VOID __cdecl RtlRestoreContext (PCONTEXT ContextRecord,struct _EXCEPTION_RECORD *ExceptionRecord); + NTSYSAPI BOOLEAN __cdecl RtlAddFunctionTable(PRUNTIME_FUNCTION FunctionTable,DWORD EntryCount,DWORD64 BaseAddress); + NTSYSAPI BOOLEAN __cdecl RtlInstallFunctionTableCallback(DWORD64 TableIdentifier,DWORD64 BaseAddress,DWORD Length,PGET_RUNTIME_FUNCTION_CALLBACK Callback,PVOID Context,PCWSTR OutOfProcessCallbackDll); + NTSYSAPI BOOLEAN __cdecl RtlDeleteFunctionTable(PRUNTIME_FUNCTION FunctionTable); +#endif + +#ifdef I_X86_ +#if(defined(_X86_) && !defined(__x86_64)) && !defined(RC_INVOKED) +#ifdef __cplusplus + extern "C" { +#endif + + __CRT_INLINE BOOLEAN InterlockedBitTestAndSet(LONG *Base,LONG Bit) { + int old = 0; + __asm__ __volatile__("lock ; btsl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Bit)); + return (BOOLEAN) (old!=0); + } + + __CRT_INLINE BOOLEAN InterlockedBitTestAndReset(LONG *Base,LONG Bit) { + int old = 0; + __asm__ __volatile__("lock ; btrl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Bit)); + return (BOOLEAN) (old!=0); + } + + __CRT_INLINE BOOLEAN InterlockedBitTestAndComplement(LONG *Base,LONG Bit) { + int old = 0; + __asm__ __volatile__("lock ; btcl %2,%1\n\tsbbl %0,%0 " + :"=r" (old),"=m" ((*(volatile long *) Base)) + :"Ir" (Bit)); + return (BOOLEAN) (old!=0); + } + +#ifdef _PREFIX_ + BYTE __readfsbyte(DWORD Offset); + WORD __readfsword(DWORD Offset); + DWORD __readfsdword(DWORD Offset); + VOID __writefsbyte(DWORD Offset,BYTE Data); + VOID __writefsword(DWORD Offset,WORD Data); + VOID __writefsdword(DWORD Offset,DWORD Data); +#endif + +#ifdef __cplusplus + } +#endif +#endif + +#if(defined(_X86_) && !defined(__x86_64)) + __CRT_INLINE VOID MemoryBarrier(VOID) { + LONG Barrier; + __asm__ __volatile__("xchgl %eax,%0 " + :"=r" (Barrier)); + } +#define YieldProcessor() __asm__ __volatile__("rep nop "); + +#define PreFetchCacheLine(l,a) +#define ReadForWriteAccess(p) (*(p)) + +#define PF_TEMPORAL_LEVEL_1 +#define PF_NON_TEMPORAL_LEVEL_ALL + + __CRT_INLINE VOID DbgRaiseAssertionFailure(void) { + __asm__ __volatile__("int 0x2c "); + } + PVOID GetCurrentFiber(void); + __CRT_INLINE PVOID GetCurrentFiber(void) + { + void *ret; + __asm__ volatile ("movl %%fs:0x10,%0" + : "=r" (ret)); + return ret; + } + PVOID GetFiberData(void); + __CRT_INLINE PVOID GetFiberData(void) + { + void *ret; + __asm__ volatile ("movl %%fs:0x10,%0\n" + "movl (%0),%0" + : "=r" (ret)); + return ret; + } +#endif + +#define EXCEPTION_READ_FAULT 0 +#define EXCEPTION_WRITE_FAULT 1 +#define EXCEPTION_EXECUTE_FAULT 8 + +#define SIZE_OF_80387_REGISTERS 80 + +#if !defined(RC_INVOKED) + +#define CONTEXT_i386 0x00010000 +#define CONTEXT_i486 0x00010000 + +#define CONTEXT_CONTROL (CONTEXT_i386 | 0x00000001L) +#define CONTEXT_INTEGER (CONTEXT_i386 | 0x00000002L) +#define CONTEXT_SEGMENTS (CONTEXT_i386 | 0x00000004L) +#define CONTEXT_FLOATING_POINT (CONTEXT_i386 | 0x00000008L) +#define CONTEXT_DEBUG_REGISTERS (CONTEXT_i386 | 0x00000010L) +#define CONTEXT_EXTENDED_REGISTERS (CONTEXT_i386 | 0x00000020L) + +#define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS) + +#define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS | CONTEXT_EXTENDED_REGISTERS) +#endif + +#define MAXIMUM_SUPPORTED_EXTENSION 512 + + typedef struct _FLOATING_SAVE_AREA { + DWORD ControlWord; + DWORD StatusWord; + DWORD TagWord; + DWORD ErrorOffset; + DWORD ErrorSelector; + DWORD DataOffset; + DWORD DataSelector; + BYTE RegisterArea[SIZE_OF_80387_REGISTERS]; + DWORD Cr0NpxState; + } FLOATING_SAVE_AREA; + + typedef FLOATING_SAVE_AREA *PFLOATING_SAVE_AREA; + + typedef struct _CONTEXT { + DWORD ContextFlags; + DWORD Dr0; + DWORD Dr1; + DWORD Dr2; + DWORD Dr3; + DWORD Dr6; + DWORD Dr7; + FLOATING_SAVE_AREA FloatSave; + DWORD SegGs; + DWORD SegFs; + DWORD SegEs; + DWORD SegDs; + + DWORD Edi; + DWORD Esi; + DWORD Ebx; + DWORD Edx; + DWORD Ecx; + DWORD Eax; + DWORD Ebp; + DWORD Eip; + DWORD SegCs; + DWORD EFlags; + DWORD Esp; + DWORD SegSs; + BYTE ExtendedRegisters[MAXIMUM_SUPPORTED_EXTENSION]; + } CONTEXT; + + typedef CONTEXT *PCONTEXT; +#endif + +#ifndef _LDT_ENTRY_DEFINED +#define _LDT_ENTRY_DEFINED + + typedef struct _LDT_ENTRY { + WORD LimitLow; + WORD BaseLow; + union { + struct { + BYTE BaseMid; + BYTE Flags1; + BYTE Flags2; + BYTE BaseHi; + } Bytes; + struct { + DWORD BaseMid : 8; + DWORD Type : 5; + DWORD Dpl : 2; + DWORD Pres : 1; + DWORD LimitHi : 4; + DWORD Sys : 1; + DWORD Reserved_0 : 1; + DWORD Default_Big : 1; + DWORD Granularity : 1; + DWORD BaseHi : 8; + } Bits; + } HighWord; + } LDT_ENTRY,*PLDT_ENTRY; +#endif + +#if defined(__ia64__) && !defined(RC_INVOKED) + +#ifdef __cplusplus + extern "C" { +#endif + + BOOLEAN BitScanForward64(DWORD *Index,DWORD64 Mask); + BOOLEAN BitScanReverse64(DWORD *Index,DWORD64 Mask); + +#ifdef __cplusplus + } +#endif +#endif + +#if !defined(GENUTIL) && !defined(_GENIA64_) && defined(_IA64_) + + void *_cdecl _rdteb(void); +#ifdef __ia64__ + +#define NtCurrentTeb() ((struct _TEB *)_rdteb()) +#define GetCurrentFiber() (((PNT_TIB)NtCurrentTeb())->FiberData) +#define GetFiberData() (*(PVOID *)(GetCurrentFiber())) + +#ifdef __cplusplus + extern "C" { +#endif + + void __break(int); + void __yield(void); + void __mf(void); + void __lfetch(int Level,VOID CONST *Address); + void __lfetchfault(int Level,VOID CONST *Address); + void __lfetch_excl(int Level,VOID CONST *Address); + void __lfetchfault_excl(int Level,VOID CONST *Address); + +#define MD_LFHINT_NONE 0x00 +#define MD_LFHINT_NT1 0x01 +#define MD_LFHINT_NT2 0x02 +#define MD_LFHINT_NTA 0x03 + +#ifdef __cplusplus + } +#endif + +#define YieldProcessor __yield +#define MemoryBarrier __mf +#define PreFetchCacheLine __lfetch +#define ReadForWriteAccess(p) (*(p)) +#define DbgRaiseAssertionFailure() __break(ASSERT_BREAKPOINT) + +#define PF_TEMPORAL_LEVEL_1 MD_LFHINT_NONE +#define PF_NON_TEMPORAL_LEVEL_ALL MD_LFHINT_NTA + +#define UnsignedMultiplyHigh __UMULH + + ULONGLONG UnsignedMultiplyHigh(ULONGLONG Multiplier,ULONGLONG Multiplicand); +#else + struct _TEB *NtCurrentTeb(void); +#endif +#endif + +#ifdef _IA64_ + +#define EXCEPTION_READ_FAULT 0 +#define EXCEPTION_WRITE_FAULT 1 +#define EXCEPTION_EXECUTE_FAULT 2 + +#if !defined(RC_INVOKED) + +#define CONTEXT_IA64 0x00080000 + +#define CONTEXT_CONTROL (CONTEXT_IA64 | 0x00000001L) +#define CONTEXT_LOWER_FLOATING_POINT (CONTEXT_IA64 | 0x00000002L) +#define CONTEXT_HIGHER_FLOATING_POINT (CONTEXT_IA64 | 0x00000004L) +#define CONTEXT_INTEGER (CONTEXT_IA64 | 0x00000008L) +#define CONTEXT_DEBUG (CONTEXT_IA64 | 0x00000010L) +#define CONTEXT_IA32_CONTROL (CONTEXT_IA64 | 0x00000020L) + +#define CONTEXT_FLOATING_POINT (CONTEXT_LOWER_FLOATING_POINT | CONTEXT_HIGHER_FLOATING_POINT) +#define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_FLOATING_POINT | CONTEXT_INTEGER | CONTEXT_IA32_CONTROL) +#define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_FLOATING_POINT | CONTEXT_INTEGER | CONTEXT_DEBUG | CONTEXT_IA32_CONTROL) + +#define CONTEXT_EXCEPTION_ACTIVE 0x8000000 +#define CONTEXT_SERVICE_ACTIVE 0x10000000 +#define CONTEXT_EXCEPTION_REQUEST 0x40000000 +#define CONTEXT_EXCEPTION_REPORTING 0x80000000 +#endif + + typedef struct _CONTEXT { + DWORD ContextFlags; + DWORD Fill1[3]; + ULONGLONG DbI0; + ULONGLONG DbI1; + ULONGLONG DbI2; + ULONGLONG DbI3; + ULONGLONG DbI4; + ULONGLONG DbI5; + ULONGLONG DbI6; + ULONGLONG DbI7; + ULONGLONG DbD0; + ULONGLONG DbD1; + ULONGLONG DbD2; + ULONGLONG DbD3; + ULONGLONG DbD4; + ULONGLONG DbD5; + ULONGLONG DbD6; + ULONGLONG DbD7; + FLOAT128 FltS0; + FLOAT128 FltS1; + FLOAT128 FltS2; + FLOAT128 FltS3; + FLOAT128 FltT0; + FLOAT128 FltT1; + FLOAT128 FltT2; + FLOAT128 FltT3; + FLOAT128 FltT4; + FLOAT128 FltT5; + FLOAT128 FltT6; + FLOAT128 FltT7; + FLOAT128 FltT8; + FLOAT128 FltT9; + FLOAT128 FltS4; + FLOAT128 FltS5; + FLOAT128 FltS6; + FLOAT128 FltS7; + FLOAT128 FltS8; + FLOAT128 FltS9; + FLOAT128 FltS10; + FLOAT128 FltS11; + FLOAT128 FltS12; + FLOAT128 FltS13; + FLOAT128 FltS14; + FLOAT128 FltS15; + FLOAT128 FltS16; + FLOAT128 FltS17; + FLOAT128 FltS18; + FLOAT128 FltS19; + FLOAT128 FltF32; + FLOAT128 FltF33; + FLOAT128 FltF34; + FLOAT128 FltF35; + FLOAT128 FltF36; + FLOAT128 FltF37; + FLOAT128 FltF38; + FLOAT128 FltF39; + FLOAT128 FltF40; + FLOAT128 FltF41; + FLOAT128 FltF42; + FLOAT128 FltF43; + FLOAT128 FltF44; + FLOAT128 FltF45; + FLOAT128 FltF46; + FLOAT128 FltF47; + FLOAT128 FltF48; + FLOAT128 FltF49; + FLOAT128 FltF50; + FLOAT128 FltF51; + FLOAT128 FltF52; + FLOAT128 FltF53; + FLOAT128 FltF54; + FLOAT128 FltF55; + FLOAT128 FltF56; + FLOAT128 FltF57; + FLOAT128 FltF58; + FLOAT128 FltF59; + FLOAT128 FltF60; + FLOAT128 FltF61; + FLOAT128 FltF62; + FLOAT128 FltF63; + FLOAT128 FltF64; + FLOAT128 FltF65; + FLOAT128 FltF66; + FLOAT128 FltF67; + FLOAT128 FltF68; + FLOAT128 FltF69; + FLOAT128 FltF70; + FLOAT128 FltF71; + FLOAT128 FltF72; + FLOAT128 FltF73; + FLOAT128 FltF74; + FLOAT128 FltF75; + FLOAT128 FltF76; + FLOAT128 FltF77; + FLOAT128 FltF78; + FLOAT128 FltF79; + FLOAT128 FltF80; + FLOAT128 FltF81; + FLOAT128 FltF82; + FLOAT128 FltF83; + FLOAT128 FltF84; + FLOAT128 FltF85; + FLOAT128 FltF86; + FLOAT128 FltF87; + FLOAT128 FltF88; + FLOAT128 FltF89; + FLOAT128 FltF90; + FLOAT128 FltF91; + FLOAT128 FltF92; + FLOAT128 FltF93; + FLOAT128 FltF94; + FLOAT128 FltF95; + FLOAT128 FltF96; + FLOAT128 FltF97; + FLOAT128 FltF98; + FLOAT128 FltF99; + FLOAT128 FltF100; + FLOAT128 FltF101; + FLOAT128 FltF102; + FLOAT128 FltF103; + FLOAT128 FltF104; + FLOAT128 FltF105; + FLOAT128 FltF106; + FLOAT128 FltF107; + FLOAT128 FltF108; + FLOAT128 FltF109; + FLOAT128 FltF110; + FLOAT128 FltF111; + FLOAT128 FltF112; + FLOAT128 FltF113; + FLOAT128 FltF114; + FLOAT128 FltF115; + FLOAT128 FltF116; + FLOAT128 FltF117; + FLOAT128 FltF118; + FLOAT128 FltF119; + FLOAT128 FltF120; + FLOAT128 FltF121; + FLOAT128 FltF122; + FLOAT128 FltF123; + FLOAT128 FltF124; + FLOAT128 FltF125; + FLOAT128 FltF126; + FLOAT128 FltF127; + ULONGLONG StFPSR; + ULONGLONG IntGp; + ULONGLONG IntT0; + ULONGLONG IntT1; + ULONGLONG IntS0; + ULONGLONG IntS1; + ULONGLONG IntS2; + ULONGLONG IntS3; + ULONGLONG IntV0; + ULONGLONG IntT2; + ULONGLONG IntT3; + ULONGLONG IntT4; + ULONGLONG IntSp; + ULONGLONG IntTeb; + ULONGLONG IntT5; + ULONGLONG IntT6; + ULONGLONG IntT7; + ULONGLONG IntT8; + ULONGLONG IntT9; + ULONGLONG IntT10; + ULONGLONG IntT11; + ULONGLONG IntT12; + ULONGLONG IntT13; + ULONGLONG IntT14; + ULONGLONG IntT15; + ULONGLONG IntT16; + ULONGLONG IntT17; + ULONGLONG IntT18; + ULONGLONG IntT19; + ULONGLONG IntT20; + ULONGLONG IntT21; + ULONGLONG IntT22; + ULONGLONG IntNats; + ULONGLONG Preds; + ULONGLONG BrRp; + ULONGLONG BrS0; + ULONGLONG BrS1; + ULONGLONG BrS2; + ULONGLONG BrS3; + ULONGLONG BrS4; + ULONGLONG BrT0; + ULONGLONG BrT1; + ULONGLONG ApUNAT; + ULONGLONG ApLC; + ULONGLONG ApEC; + ULONGLONG ApCCV; + ULONGLONG ApDCR; + ULONGLONG RsPFS; + ULONGLONG RsBSP; + ULONGLONG RsBSPSTORE; + ULONGLONG RsRSC; + ULONGLONG RsRNAT; + ULONGLONG StIPSR; + ULONGLONG StIIP; + ULONGLONG StIFS; + ULONGLONG StFCR; + ULONGLONG Eflag; + ULONGLONG SegCSD; + ULONGLONG SegSSD; + ULONGLONG Cflag; + ULONGLONG StFSR; + ULONGLONG StFIR; + ULONGLONG StFDR; + ULONGLONG UNUSEDPACK; + } CONTEXT,*PCONTEXT; + + typedef struct _PLABEL_DESCRIPTOR { + ULONGLONG EntryPoint; + ULONGLONG GlobalPointer; + } PLABEL_DESCRIPTOR,*PPLABEL_DESCRIPTOR; + + typedef struct _RUNTIME_FUNCTION { + DWORD BeginAddress; + DWORD EndAddress; + DWORD UnwindInfoAddress; + } RUNTIME_FUNCTION,*PRUNTIME_FUNCTION; + + typedef PRUNTIME_FUNCTION (*PGET_RUNTIME_FUNCTION_CALLBACK)(DWORD64 ControlPc,PVOID Context); + typedef DWORD (*POUT_OF_PROCESS_FUNCTION_TABLE_CALLBACK)(HANDLE Process,PVOID TableAddress,PDWORD Entries,PRUNTIME_FUNCTION *Functions); + +#define OUT_OF_PROCESS_FUNCTION_TABLE_CALLBACK_EXPORT_NAME "OutOfProcessFunctionTableCallback" + + BOOLEAN RtlAddFunctionTable(PRUNTIME_FUNCTION FunctionTable,DWORD EntryCount,ULONGLONG BaseAddress,ULONGLONG TargetGp); + BOOLEAN RtlInstallFunctionTableCallback(DWORD64 TableIdentifier,DWORD64 BaseAddress,DWORD Length,DWORD64 TargetGp,PGET_RUNTIME_FUNCTION_CALLBACK Callback,PVOID Context,PCWSTR OutOfProcessCallbackDll); + BOOLEAN RtlDeleteFunctionTable(PRUNTIME_FUNCTION FunctionTable); + VOID RtlRestoreContext (PCONTEXT ContextRecord,struct _EXCEPTION_RECORD *ExceptionRecord); + VOID __jump_unwind(ULONGLONG TargetMsFrame,ULONGLONG TargetBsFrame,ULONGLONG TargetPc); +#endif + +#define EXCEPTION_NONCONTINUABLE 0x1 +#define EXCEPTION_MAXIMUM_PARAMETERS 15 + + typedef struct _EXCEPTION_RECORD { + DWORD ExceptionCode; + DWORD ExceptionFlags; + struct _EXCEPTION_RECORD *ExceptionRecord; + PVOID ExceptionAddress; + DWORD NumberParameters; + ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; + } EXCEPTION_RECORD; + + typedef EXCEPTION_RECORD *PEXCEPTION_RECORD; + + typedef struct _EXCEPTION_RECORD32 { + DWORD ExceptionCode; + DWORD ExceptionFlags; + DWORD ExceptionRecord; + DWORD ExceptionAddress; + DWORD NumberParameters; + DWORD ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; + } EXCEPTION_RECORD32,*PEXCEPTION_RECORD32; + + typedef struct _EXCEPTION_RECORD64 { + DWORD ExceptionCode; + DWORD ExceptionFlags; + DWORD64 ExceptionRecord; + DWORD64 ExceptionAddress; + DWORD NumberParameters; + DWORD __unusedAlignment; + DWORD64 ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; + } EXCEPTION_RECORD64,*PEXCEPTION_RECORD64; + + typedef struct _EXCEPTION_POINTERS { + PEXCEPTION_RECORD ExceptionRecord; + PCONTEXT ContextRecord; + } EXCEPTION_POINTERS,*PEXCEPTION_POINTERS; + +#ifdef __x86_64 + + typedef EXCEPTION_DISPOSITION NTAPI EXCEPTION_ROUTINE (struct _EXCEPTION_RECORD *ExceptionRecord, PVOID EstablisherFrame, struct _CONTEXT *ContextRecord, PVOID DispatcherContext); +#ifndef __PEXCEPTION_ROUTINE_DEFINED +#define __PEXCEPTION_ROUTINE_DEFINED + typedef EXCEPTION_ROUTINE *PEXCEPTION_ROUTINE; +#endif + + /* http://msdn.microsoft.com/en-us/library/ms680597(VS.85).aspx */ + +#define UNWIND_HISTORY_TABLE_SIZE 12 + + typedef struct _UNWIND_HISTORY_TABLE_ENTRY { + ULONG64 ImageBase; + PRUNTIME_FUNCTION FunctionEntry; + } UNWIND_HISTORY_TABLE_ENTRY, *PUNWIND_HISTORY_TABLE_ENTRY; + +#define UNWIND_HISTORY_TABLE_NONE 0 +#define UNWIND_HISTORY_TABLE_GLOBAL 1 +#define UNWIND_HISTORY_TABLE_LOCAL 2 + + typedef struct _UNWIND_HISTORY_TABLE { + ULONG Count; + UCHAR Search; + ULONG64 LowAddress; + ULONG64 HighAddress; + UNWIND_HISTORY_TABLE_ENTRY Entry[UNWIND_HISTORY_TABLE_SIZE]; + } UNWIND_HISTORY_TABLE, *PUNWIND_HISTORY_TABLE; + + /* http://msdn.microsoft.com/en-us/library/b6sf5kbd(VS.80).aspx */ + + struct _DISPATCHER_CONTEXT; + typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT; + typedef struct _DISPATCHER_CONTEXT *PDISPATCHER_CONTEXT; + + struct _DISPATCHER_CONTEXT { + ULONG64 ControlPc; + ULONG64 ImageBase; + PRUNTIME_FUNCTION FunctionEntry; + ULONG64 EstablisherFrame; + ULONG64 TargetIp; + PCONTEXT ContextRecord; + PEXCEPTION_ROUTINE LanguageHandler; + PVOID HandlerData; + /* http://www.nynaeve.net/?p=99 */ + PUNWIND_HISTORY_TABLE HistoryTable; + ULONG ScopeIndex; + ULONG Fill0; + }; + + /* http://msdn.microsoft.com/en-us/library/ms680617(VS.85).aspx */ + + typedef struct _KNONVOLATILE_CONTEXT_POINTERS + { + PM128A FloatingContext[16]; + PULONG64 IntegerContext[16]; + } KNONVOLATILE_CONTEXT_POINTERS, *PKNONVOLATILE_CONTEXT_POINTERS; +#endif /* defined(__x86_64) */ + + typedef PVOID PACCESS_TOKEN; + typedef PVOID PSECURITY_DESCRIPTOR; + typedef PVOID PSID; + + typedef DWORD ACCESS_MASK; + typedef ACCESS_MASK *PACCESS_MASK; + +#define DELETE (0x00010000L) +#define READ_CONTROL (0x00020000L) +#define WRITE_DAC (0x00040000L) +#define WRITE_OWNER (0x00080000L) +#define SYNCHRONIZE (0x00100000L) + +#define STANDARD_RIGHTS_REQUIRED (0x000F0000L) +#define STANDARD_RIGHTS_READ (READ_CONTROL) +#define STANDARD_RIGHTS_WRITE (READ_CONTROL) +#define STANDARD_RIGHTS_EXECUTE (READ_CONTROL) +#define STANDARD_RIGHTS_ALL (0x001F0000L) + +#define SPECIFIC_RIGHTS_ALL (0x0000FFFFL) + +#define ACCESS_SYSTEM_SECURITY (0x01000000L) + +#define MAXIMUM_ALLOWED (0x02000000L) + +#define GENERIC_READ (0x80000000L) +#define GENERIC_WRITE (0x40000000L) +#define GENERIC_EXECUTE (0x20000000L) +#define GENERIC_ALL (0x10000000L) + + typedef struct _GENERIC_MAPPING { + ACCESS_MASK GenericRead; + ACCESS_MASK GenericWrite; + ACCESS_MASK GenericExecute; + ACCESS_MASK GenericAll; + } GENERIC_MAPPING; + typedef GENERIC_MAPPING *PGENERIC_MAPPING; + +#include + + typedef struct _LUID_AND_ATTRIBUTES { + LUID Luid; + DWORD Attributes; + } LUID_AND_ATTRIBUTES,*PLUID_AND_ATTRIBUTES; + typedef LUID_AND_ATTRIBUTES LUID_AND_ATTRIBUTES_ARRAY[ANYSIZE_ARRAY]; + typedef LUID_AND_ATTRIBUTES_ARRAY *PLUID_AND_ATTRIBUTES_ARRAY; + +#include + +#ifndef SID_IDENTIFIER_AUTHORITY_DEFINED +#define SID_IDENTIFIER_AUTHORITY_DEFINED + typedef struct _SID_IDENTIFIER_AUTHORITY { + BYTE Value[6]; + } SID_IDENTIFIER_AUTHORITY,*PSID_IDENTIFIER_AUTHORITY; +#endif + +#ifndef SID_DEFINED +#define SID_DEFINED + typedef struct _SID { + BYTE Revision; + BYTE SubAuthorityCount; + SID_IDENTIFIER_AUTHORITY IdentifierAuthority; + DWORD SubAuthority[ANYSIZE_ARRAY]; + } SID,*PISID; +#endif + +#define SID_REVISION (1) +#define SID_MAX_SUB_AUTHORITIES (15) +#define SID_RECOMMENDED_SUB_AUTHORITIES (1) + +#define SECURITY_MAX_SID_SIZE (sizeof(SID) - sizeof(DWORD) + (SID_MAX_SUB_AUTHORITIES *sizeof(DWORD))) + + typedef enum _SID_NAME_USE { + SidTypeUser = 1,SidTypeGroup,SidTypeDomain,SidTypeAlias,SidTypeWellKnownGroup,SidTypeDeletedAccount,SidTypeInvalid,SidTypeUnknown,SidTypeComputer + } SID_NAME_USE,*PSID_NAME_USE; + + typedef struct _SID_AND_ATTRIBUTES { + PSID Sid; + DWORD Attributes; + } SID_AND_ATTRIBUTES,*PSID_AND_ATTRIBUTES; + + typedef SID_AND_ATTRIBUTES SID_AND_ATTRIBUTES_ARRAY[ANYSIZE_ARRAY]; + typedef SID_AND_ATTRIBUTES_ARRAY *PSID_AND_ATTRIBUTES_ARRAY; + +#define SECURITY_NULL_SID_AUTHORITY {0,0,0,0,0,0} +#define SECURITY_WORLD_SID_AUTHORITY {0,0,0,0,0,1} +#define SECURITY_LOCAL_SID_AUTHORITY {0,0,0,0,0,2} +#define SECURITY_CREATOR_SID_AUTHORITY {0,0,0,0,0,3} +#define SECURITY_NON_UNIQUE_AUTHORITY {0,0,0,0,0,4} +#define SECURITY_RESOURCE_MANAGER_AUTHORITY {0,0,0,0,0,9} + +#define SECURITY_NULL_RID (0x00000000L) +#define SECURITY_WORLD_RID (0x00000000L) +#define SECURITY_LOCAL_RID (0x00000000L) + +#define SECURITY_CREATOR_OWNER_RID (0x00000000L) +#define SECURITY_CREATOR_GROUP_RID (0x00000001L) + +#define SECURITY_CREATOR_OWNER_SERVER_RID (0x00000002L) +#define SECURITY_CREATOR_GROUP_SERVER_RID (0x00000003L) + +#define SECURITY_NT_AUTHORITY {0,0,0,0,0,5} + +#define SECURITY_DIALUP_RID (0x00000001L) +#define SECURITY_NETWORK_RID (0x00000002L) +#define SECURITY_BATCH_RID (0x00000003L) +#define SECURITY_INTERACTIVE_RID (0x00000004L) +#define SECURITY_LOGON_IDS_RID (0x00000005L) +#define SECURITY_LOGON_IDS_RID_COUNT (3L) +#define SECURITY_SERVICE_RID (0x00000006L) +#define SECURITY_ANONYMOUS_LOGON_RID (0x00000007L) +#define SECURITY_PROXY_RID (0x00000008L) +#define SECURITY_ENTERPRISE_CONTROLLERS_RID (0x00000009L) +#define SECURITY_SERVER_LOGON_RID SECURITY_ENTERPRISE_CONTROLLERS_RID +#define SECURITY_PRINCIPAL_SELF_RID (0x0000000AL) +#define SECURITY_AUTHENTICATED_USER_RID (0x0000000BL) +#define SECURITY_RESTRICTED_CODE_RID (0x0000000CL) +#define SECURITY_TERMINAL_SERVER_RID (0x0000000DL) +#define SECURITY_REMOTE_LOGON_RID (0x0000000EL) +#define SECURITY_THIS_ORGANIZATION_RID (0x0000000FL) +#define SECURITY_IUSER_RID (0x00000011L) + +#define SECURITY_LOCAL_SYSTEM_RID (0x00000012L) +#define SECURITY_LOCAL_SERVICE_RID (0x00000013L) +#define SECURITY_NETWORK_SERVICE_RID (0x00000014L) + +#define SECURITY_NT_NON_UNIQUE (0x00000015L) +#define SECURITY_NT_NON_UNIQUE_SUB_AUTH_COUNT (3L) + +#define SECURITY_ENTERPRISE_READONLY_CONTROLLERS_RID (0x00000016L) + +#define SECURITY_BUILTIN_DOMAIN_RID (0x00000020L) +#define SECURITY_WRITE_RESTRICTED_CODE_RID (0x00000021L) + +#define SECURITY_PACKAGE_BASE_RID (0x00000040L) +#define SECURITY_PACKAGE_RID_COUNT (2L) +#define SECURITY_PACKAGE_NTLM_RID (0x0000000AL) +#define SECURITY_PACKAGE_SCHANNEL_RID (0x0000000EL) +#define SECURITY_PACKAGE_DIGEST_RID (0x00000015L) + +#define SECURITY_SERVICE_ID_BASE_RID (0x00000050L) +#define SECURITY_SERVICE_ID_RID_COUNT (6L) + +#define SECURITY_RESERVED_ID_BASE_RID (0x00000051L) + +#define SECURITY_MAX_ALWAYS_FILTERED (0x000003E7L) +#define SECURITY_MIN_NEVER_FILTERED (0x000003E8L) + +#define SECURITY_OTHER_ORGANIZATION_RID (0x000003E8L) + +#define FOREST_USER_RID_MAX (0x000001F3L) + +#define DOMAIN_USER_RID_ADMIN (0x000001F4L) +#define DOMAIN_USER_RID_GUEST (0x000001F5L) +#define DOMAIN_USER_RID_KRBTGT (0x000001F6L) + +#define DOMAIN_USER_RID_MAX (0x000003E7L) + +#define DOMAIN_GROUP_RID_ADMINS (0x00000200L) +#define DOMAIN_GROUP_RID_USERS (0x00000201L) +#define DOMAIN_GROUP_RID_GUESTS (0x00000202L) +#define DOMAIN_GROUP_RID_COMPUTERS (0x00000203L) +#define DOMAIN_GROUP_RID_CONTROLLERS (0x00000204L) +#define DOMAIN_GROUP_RID_CERT_ADMINS (0x00000205L) +#define DOMAIN_GROUP_RID_SCHEMA_ADMINS (0x00000206L) +#define DOMAIN_GROUP_RID_ENTERPRISE_ADMINS (0x00000207L) +#define DOMAIN_GROUP_RID_POLICY_ADMINS (0x00000208L) +#define DOMAIN_GROUP_RID_READONLY_CONTROLLERS (0x00000209L) + +#define DOMAIN_ALIAS_RID_ADMINS (0x00000220L) +#define DOMAIN_ALIAS_RID_USERS (0x00000221L) +#define DOMAIN_ALIAS_RID_GUESTS (0x00000222L) +#define DOMAIN_ALIAS_RID_POWER_USERS (0x00000223L) + +#define DOMAIN_ALIAS_RID_ACCOUNT_OPS (0x00000224L) +#define DOMAIN_ALIAS_RID_SYSTEM_OPS (0x00000225L) +#define DOMAIN_ALIAS_RID_PRINT_OPS (0x00000226L) +#define DOMAIN_ALIAS_RID_BACKUP_OPS (0x00000227L) + +#define DOMAIN_ALIAS_RID_REPLICATOR (0x00000228L) +#define DOMAIN_ALIAS_RID_RAS_SERVERS (0x00000229L) +#define DOMAIN_ALIAS_RID_PREW2KCOMPACCESS (0x0000022AL) +#define DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS (0x0000022BL) +#define DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS (0x0000022CL) +#define DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS (0x0000022DL) + +#define DOMAIN_ALIAS_RID_MONITORING_USERS (0x0000022EL) +#define DOMAIN_ALIAS_RID_LOGGING_USERS (0x0000022FL) +#define DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS (0x00000230L) +#define DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS (0x00000231L) +#define DOMAIN_ALIAS_RID_DCOM_USERS (0x00000232L) + +#define DOMAIN_ALIAS_RID_IUSERS (0x00000238L) +#define DOMAIN_ALIAS_RID_CRYPTO_OPERATORS (0x00000239L) +#define DOMAIN_ALIAS_RID_CACHEABLE_PRINCIPALS_GROUP (0x0000023BL) +#define DOMAIN_ALIAS_RID_NON_CACHEABLE_PRINCIPALS_GROUP (0x0000023CL) +#define DOMAIN_ALIAS_RID_EVENT_LOG_READERS_GROUP (0x0000023DL) + +#define SECURITY_MANDATORY_LABEL_AUTHORITY {0,0,0,0,0,16} +#define SECURITY_MANDATORY_UNTRUSTED_RID (0x00000000L) +#define SECURITY_MANDATORY_LOW_RID (0x00001000L) +#define SECURITY_MANDATORY_MEDIUM_RID (0x00002000L) +#define SECURITY_MANDATORY_HIGH_RID (0x00003000L) +#define SECURITY_MANDATORY_SYSTEM_RID (0x00004000L) +#define SECURITY_MANDATORY_PROTECTED_PROCESS_RID (0x00005000L) + +#define SECURITY_MANDATORY_MAXIMUM_USER_RID SECURITY_MANDATORY_SYSTEM_RID + +#define MANDATORY_LEVEL_TO_MANDATORY_RID(IL) (IL * 0x1000) + + typedef enum { + WinNullSid = 0,WinWorldSid = 1,WinLocalSid = 2,WinCreatorOwnerSid = 3,WinCreatorGroupSid = 4,WinCreatorOwnerServerSid = 5,WinCreatorGroupServerSid = 6,WinNtAuthoritySid = 7,WinDialupSid = 8,WinNetworkSid = 9,WinBatchSid = 10,WinInteractiveSid = 11,WinServiceSid = 12,WinAnonymousSid = 13,WinProxySid = 14,WinEnterpriseControllersSid = 15,WinSelfSid = 16,WinAuthenticatedUserSid = 17,WinRestrictedCodeSid = 18,WinTerminalServerSid = 19,WinRemoteLogonIdSid = 20,WinLogonIdsSid = 21,WinLocalSystemSid = 22,WinLocalServiceSid = 23,WinNetworkServiceSid = 24,WinBuiltinDomainSid = 25,WinBuiltinAdministratorsSid = 26,WinBuiltinUsersSid = 27,WinBuiltinGuestsSid = 28,WinBuiltinPowerUsersSid = 29,WinBuiltinAccountOperatorsSid = 30,WinBuiltinSystemOperatorsSid = 31,WinBuiltinPrintOperatorsSid = 32,WinBuiltinBackupOperatorsSid = 33,WinBuiltinReplicatorSid = 34,WinBuiltinPreWindows2000CompatibleAccessSid = 35,WinBuiltinRemoteDesktopUsersSid = 36,WinBuiltinNetworkConfigurationOperatorsSid = 37,WinAccountAdministratorSid = 38,WinAccountGuestSid = 39,WinAccountKrbtgtSid = 40,WinAccountDomainAdminsSid = 41,WinAccountDomainUsersSid = 42,WinAccountDomainGuestsSid = 43,WinAccountComputersSid = 44,WinAccountControllersSid = 45,WinAccountCertAdminsSid = 46,WinAccountSchemaAdminsSid = 47,WinAccountEnterpriseAdminsSid = 48,WinAccountPolicyAdminsSid = 49,WinAccountRasAndIasServersSid = 50,WinNTLMAuthenticationSid = 51,WinDigestAuthenticationSid = 52,WinSChannelAuthenticationSid = 53,WinThisOrganizationSid = 54,WinOtherOrganizationSid = 55,WinBuiltinIncomingForestTrustBuildersSid = 56,WinBuiltinPerfMonitoringUsersSid = 57,WinBuiltinPerfLoggingUsersSid = 58,WinBuiltinAuthorizationAccessSid = 59,WinBuiltinTerminalServerLicenseServersSid = 60,WinBuiltinDCOMUsersSid = 61 + } WELL_KNOWN_SID_TYPE; + +#define SYSTEM_LUID { 0x3E7,0x0 } +#define ANONYMOUS_LOGON_LUID { 0x3e6,0x0 } +#define LOCALSERVICE_LUID { 0x3e5,0x0 } +#define NETWORKSERVICE_LUID { 0x3e4,0x0 } +#define IUSER_LUID { 0x3e3, 0x0 } + +#define SE_GROUP_MANDATORY (0x00000001L) +#define SE_GROUP_ENABLED_BY_DEFAULT (0x00000002L) +#define SE_GROUP_ENABLED (0x00000004L) +#define SE_GROUP_OWNER (0x00000008L) +#define SE_GROUP_USE_FOR_DENY_ONLY (0x00000010L) +#define SE_GROUP_INTEGRITY (0x00000020L) +#define SE_GROUP_INTEGRITY_ENABLED (0x00000040L) +#define SE_GROUP_LOGON_ID (0xC0000000L) +#define SE_GROUP_RESOURCE (0x20000000L) + +#define ACL_REVISION (2) +#define ACL_REVISION_DS (4) + +#define ACL_REVISION1 (1) +#define MIN_ACL_REVISION ACL_REVISION2 +#define ACL_REVISION2 (2) +#define ACL_REVISION3 (3) +#define ACL_REVISION4 (4) +#define MAX_ACL_REVISION ACL_REVISION4 + + typedef struct _ACL { + BYTE AclRevision; + BYTE Sbz1; + WORD AclSize; + WORD AceCount; + WORD Sbz2; + } ACL; + typedef ACL *PACL; + + typedef struct _ACE_HEADER { + BYTE AceType; + BYTE AceFlags; + WORD AceSize; + } ACE_HEADER; + typedef ACE_HEADER *PACE_HEADER; + +#define ACCESS_MIN_MS_ACE_TYPE (0x0) +#define ACCESS_ALLOWED_ACE_TYPE (0x0) +#define ACCESS_DENIED_ACE_TYPE (0x1) +#define SYSTEM_AUDIT_ACE_TYPE (0x2) +#define SYSTEM_ALARM_ACE_TYPE (0x3) +#define ACCESS_MAX_MS_V2_ACE_TYPE (0x3) + +#define ACCESS_ALLOWED_COMPOUND_ACE_TYPE (0x4) +#define ACCESS_MAX_MS_V3_ACE_TYPE (0x4) + +#define ACCESS_MIN_MS_OBJECT_ACE_TYPE (0x5) +#define ACCESS_ALLOWED_OBJECT_ACE_TYPE (0x5) +#define ACCESS_DENIED_OBJECT_ACE_TYPE (0x6) +#define SYSTEM_AUDIT_OBJECT_ACE_TYPE (0x7) +#define SYSTEM_ALARM_OBJECT_ACE_TYPE (0x8) +#define ACCESS_MAX_MS_OBJECT_ACE_TYPE (0x8) + +#define ACCESS_MAX_MS_V4_ACE_TYPE (0x8) +#define ACCESS_MAX_MS_ACE_TYPE (0x8) + +#define ACCESS_ALLOWED_CALLBACK_ACE_TYPE (0x9) +#define ACCESS_DENIED_CALLBACK_ACE_TYPE (0xA) +#define ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE (0xB) +#define ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE (0xC) +#define SYSTEM_AUDIT_CALLBACK_ACE_TYPE (0xD) +#define SYSTEM_ALARM_CALLBACK_ACE_TYPE (0xE) +#define SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE (0xF) +#define SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE (0x10) + +#define SYSTEM_MANDATORY_LABEL_ACE_TYPE (0x11) +#define ACCESS_MAX_MS_V5_ACE_TYPE (0x11) + +#define OBJECT_INHERIT_ACE (0x1) +#define CONTAINER_INHERIT_ACE (0x2) +#define NO_PROPAGATE_INHERIT_ACE (0x4) +#define INHERIT_ONLY_ACE (0x8) +#define INHERITED_ACE (0x10) +#define VALID_INHERIT_FLAGS (0x1F) + +#define SUCCESSFUL_ACCESS_ACE_FLAG (0x40) +#define FAILED_ACCESS_ACE_FLAG (0x80) + + typedef struct _ACCESS_ALLOWED_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + } ACCESS_ALLOWED_ACE; + + typedef ACCESS_ALLOWED_ACE *PACCESS_ALLOWED_ACE; + + typedef struct _ACCESS_DENIED_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + } ACCESS_DENIED_ACE; + typedef ACCESS_DENIED_ACE *PACCESS_DENIED_ACE; + + typedef struct _SYSTEM_AUDIT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + } SYSTEM_AUDIT_ACE; + typedef SYSTEM_AUDIT_ACE *PSYSTEM_AUDIT_ACE; + + typedef struct _SYSTEM_ALARM_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + } SYSTEM_ALARM_ACE; + typedef SYSTEM_ALARM_ACE *PSYSTEM_ALARM_ACE; + + typedef struct _ACCESS_ALLOWED_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + } ACCESS_ALLOWED_OBJECT_ACE,*PACCESS_ALLOWED_OBJECT_ACE; + + typedef struct _ACCESS_DENIED_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + } ACCESS_DENIED_OBJECT_ACE,*PACCESS_DENIED_OBJECT_ACE; + + typedef struct _SYSTEM_AUDIT_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + } SYSTEM_AUDIT_OBJECT_ACE,*PSYSTEM_AUDIT_OBJECT_ACE; + + typedef struct _SYSTEM_ALARM_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + } SYSTEM_ALARM_OBJECT_ACE,*PSYSTEM_ALARM_OBJECT_ACE; + + typedef struct _ACCESS_ALLOWED_CALLBACK_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + + } ACCESS_ALLOWED_CALLBACK_ACE,*PACCESS_ALLOWED_CALLBACK_ACE; + + typedef struct _ACCESS_DENIED_CALLBACK_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + + } ACCESS_DENIED_CALLBACK_ACE,*PACCESS_DENIED_CALLBACK_ACE; + + typedef struct _SYSTEM_AUDIT_CALLBACK_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + + } SYSTEM_AUDIT_CALLBACK_ACE,*PSYSTEM_AUDIT_CALLBACK_ACE; + + typedef struct _SYSTEM_ALARM_CALLBACK_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD SidStart; + + } SYSTEM_ALARM_CALLBACK_ACE,*PSYSTEM_ALARM_CALLBACK_ACE; + + typedef struct _ACCESS_ALLOWED_CALLBACK_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + + } ACCESS_ALLOWED_CALLBACK_OBJECT_ACE,*PACCESS_ALLOWED_CALLBACK_OBJECT_ACE; + + typedef struct _ACCESS_DENIED_CALLBACK_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + + } ACCESS_DENIED_CALLBACK_OBJECT_ACE,*PACCESS_DENIED_CALLBACK_OBJECT_ACE; + + typedef struct _SYSTEM_AUDIT_CALLBACK_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + + } SYSTEM_AUDIT_CALLBACK_OBJECT_ACE,*PSYSTEM_AUDIT_CALLBACK_OBJECT_ACE; + + typedef struct _SYSTEM_ALARM_CALLBACK_OBJECT_ACE { + ACE_HEADER Header; + ACCESS_MASK Mask; + DWORD Flags; + GUID ObjectType; + GUID InheritedObjectType; + DWORD SidStart; + + } SYSTEM_ALARM_CALLBACK_OBJECT_ACE,*PSYSTEM_ALARM_CALLBACK_OBJECT_ACE; + +#define ACE_OBJECT_TYPE_PRESENT 0x1 +#define ACE_INHERITED_OBJECT_TYPE_PRESENT 0x2 + + typedef enum _ACL_INFORMATION_CLASS { + AclRevisionInformation = 1,AclSizeInformation + } ACL_INFORMATION_CLASS; + + typedef struct _ACL_REVISION_INFORMATION { + DWORD AclRevision; + } ACL_REVISION_INFORMATION; + typedef ACL_REVISION_INFORMATION *PACL_REVISION_INFORMATION; + + typedef struct _ACL_SIZE_INFORMATION { + DWORD AceCount; + DWORD AclBytesInUse; + DWORD AclBytesFree; + } ACL_SIZE_INFORMATION; + typedef ACL_SIZE_INFORMATION *PACL_SIZE_INFORMATION; + +#define SECURITY_DESCRIPTOR_REVISION (1) +#define SECURITY_DESCRIPTOR_REVISION1 (1) + +#define SECURITY_DESCRIPTOR_MIN_LENGTH (sizeof(SECURITY_DESCRIPTOR)) + + typedef WORD SECURITY_DESCRIPTOR_CONTROL,*PSECURITY_DESCRIPTOR_CONTROL; + +#define SE_OWNER_DEFAULTED (0x0001) +#define SE_GROUP_DEFAULTED (0x0002) +#define SE_DACL_PRESENT (0x0004) +#define SE_DACL_DEFAULTED (0x0008) +#define SE_SACL_PRESENT (0x0010) +#define SE_SACL_DEFAULTED (0x0020) +#define SE_DACL_AUTO_INHERIT_REQ (0x0100) +#define SE_SACL_AUTO_INHERIT_REQ (0x0200) +#define SE_DACL_AUTO_INHERITED (0x0400) +#define SE_SACL_AUTO_INHERITED (0x0800) +#define SE_DACL_PROTECTED (0x1000) +#define SE_SACL_PROTECTED (0x2000) +#define SE_RM_CONTROL_VALID (0x4000) +#define SE_SELF_RELATIVE (0x8000) + + typedef struct _SECURITY_DESCRIPTOR_RELATIVE { + BYTE Revision; + BYTE Sbz1; + SECURITY_DESCRIPTOR_CONTROL Control; + DWORD Owner; + DWORD Group; + DWORD Sacl; + DWORD Dacl; + } SECURITY_DESCRIPTOR_RELATIVE,*PISECURITY_DESCRIPTOR_RELATIVE; + + typedef struct _SECURITY_DESCRIPTOR { + BYTE Revision; + BYTE Sbz1; + SECURITY_DESCRIPTOR_CONTROL Control; + PSID Owner; + PSID Group; + PACL Sacl; + PACL Dacl; + + } SECURITY_DESCRIPTOR,*PISECURITY_DESCRIPTOR; + + typedef struct _OBJECT_TYPE_LIST { + WORD Level; + WORD Sbz; + GUID *ObjectType; + } OBJECT_TYPE_LIST,*POBJECT_TYPE_LIST; + +#define ACCESS_OBJECT_GUID 0 +#define ACCESS_PROPERTY_SET_GUID 1 +#define ACCESS_PROPERTY_GUID 2 + +#define ACCESS_MAX_LEVEL 4 + + typedef enum _AUDIT_EVENT_TYPE { + AuditEventObjectAccess,AuditEventDirectoryServiceAccess + } AUDIT_EVENT_TYPE,*PAUDIT_EVENT_TYPE; + +#define AUDIT_ALLOW_NO_PRIVILEGE 0x1 + +#define ACCESS_DS_SOURCE_A "DS" +#define ACCESS_DS_SOURCE_W L"DS" +#define ACCESS_DS_OBJECT_TYPE_NAME_A "Directory Service Object" +#define ACCESS_DS_OBJECT_TYPE_NAME_W L"Directory Service Object" + +#define SE_PRIVILEGE_ENABLED_BY_DEFAULT (0x00000001L) +#define SE_PRIVILEGE_ENABLED (0x00000002L) +#define SE_PRIVILEGE_REMOVED (0X00000004L) +#define SE_PRIVILEGE_USED_FOR_ACCESS (0x80000000L) + +#define PRIVILEGE_SET_ALL_NECESSARY (1) + + typedef struct _PRIVILEGE_SET { + DWORD PrivilegeCount; + DWORD Control; + LUID_AND_ATTRIBUTES Privilege[ANYSIZE_ARRAY]; + } PRIVILEGE_SET,*PPRIVILEGE_SET; + +#define SE_CREATE_TOKEN_NAME TEXT("SeCreateTokenPrivilege") +#define SE_ASSIGNPRIMARYTOKEN_NAME TEXT("SeAssignPrimaryTokenPrivilege") +#define SE_LOCK_MEMORY_NAME TEXT("SeLockMemoryPrivilege") +#define SE_INCREASE_QUOTA_NAME TEXT("SeIncreaseQuotaPrivilege") +#define SE_UNSOLICITED_INPUT_NAME TEXT("SeUnsolicitedInputPrivilege") +#define SE_MACHINE_ACCOUNT_NAME TEXT("SeMachineAccountPrivilege") +#define SE_TCB_NAME TEXT("SeTcbPrivilege") +#define SE_SECURITY_NAME TEXT("SeSecurityPrivilege") +#define SE_TAKE_OWNERSHIP_NAME TEXT("SeTakeOwnershipPrivilege") +#define SE_LOAD_DRIVER_NAME TEXT("SeLoadDriverPrivilege") +#define SE_SYSTEM_PROFILE_NAME TEXT("SeSystemProfilePrivilege") +#define SE_SYSTEMTIME_NAME TEXT("SeSystemtimePrivilege") +#define SE_PROF_SINGLE_PROCESS_NAME TEXT("SeProfileSingleProcessPrivilege") +#define SE_INC_BASE_PRIORITY_NAME TEXT("SeIncreaseBasePriorityPrivilege") +#define SE_CREATE_PAGEFILE_NAME TEXT("SeCreatePagefilePrivilege") +#define SE_CREATE_PERMANENT_NAME TEXT("SeCreatePermanentPrivilege") +#define SE_BACKUP_NAME TEXT("SeBackupPrivilege") +#define SE_RESTORE_NAME TEXT("SeRestorePrivilege") +#define SE_SHUTDOWN_NAME TEXT("SeShutdownPrivilege") +#define SE_DEBUG_NAME TEXT("SeDebugPrivilege") +#define SE_AUDIT_NAME TEXT("SeAuditPrivilege") +#define SE_SYSTEM_ENVIRONMENT_NAME TEXT("SeSystemEnvironmentPrivilege") +#define SE_CHANGE_NOTIFY_NAME TEXT("SeChangeNotifyPrivilege") +#define SE_REMOTE_SHUTDOWN_NAME TEXT("SeRemoteShutdownPrivilege") +#define SE_UNDOCK_NAME TEXT("SeUndockPrivilege") +#define SE_SYNC_AGENT_NAME TEXT("SeSyncAgentPrivilege") +#define SE_ENABLE_DELEGATION_NAME TEXT("SeEnableDelegationPrivilege") +#define SE_MANAGE_VOLUME_NAME TEXT("SeManageVolumePrivilege") +#define SE_IMPERSONATE_NAME TEXT("SeImpersonatePrivilege") +#define SE_CREATE_GLOBAL_NAME TEXT("SeCreateGlobalPrivilege") + + typedef enum _SECURITY_IMPERSONATION_LEVEL { + SecurityAnonymous,SecurityIdentification,SecurityImpersonation,SecurityDelegation + } SECURITY_IMPERSONATION_LEVEL,*PSECURITY_IMPERSONATION_LEVEL; + +#define SECURITY_MAX_IMPERSONATION_LEVEL SecurityDelegation +#define SECURITY_MIN_IMPERSONATION_LEVEL SecurityAnonymous +#define DEFAULT_IMPERSONATION_LEVEL SecurityImpersonation +#define VALID_IMPERSONATION_LEVEL(L) (((L) >= SECURITY_MIN_IMPERSONATION_LEVEL) && ((L) <= SECURITY_MAX_IMPERSONATION_LEVEL)) + +#define TOKEN_ASSIGN_PRIMARY (0x0001) +#define TOKEN_DUPLICATE (0x0002) +#define TOKEN_IMPERSONATE (0x0004) +#define TOKEN_QUERY (0x0008) +#define TOKEN_QUERY_SOURCE (0x0010) +#define TOKEN_ADJUST_PRIVILEGES (0x0020) +#define TOKEN_ADJUST_GROUPS (0x0040) +#define TOKEN_ADJUST_DEFAULT (0x0080) +#define TOKEN_ADJUST_SESSIONID (0x0100) + +#define TOKEN_ALL_ACCESS_P (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_QUERY_SOURCE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT) +#define TOKEN_ALL_ACCESS (TOKEN_ALL_ACCESS_P | TOKEN_ADJUST_SESSIONID) +#define TOKEN_READ (STANDARD_RIGHTS_READ | TOKEN_QUERY) + +#define TOKEN_WRITE (STANDARD_RIGHTS_WRITE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT) + +#define TOKEN_EXECUTE (STANDARD_RIGHTS_EXECUTE) + + typedef enum _TOKEN_TYPE { + TokenPrimary = 1,TokenImpersonation + } TOKEN_TYPE; + typedef TOKEN_TYPE *PTOKEN_TYPE; + + typedef enum _TOKEN_INFORMATION_CLASS { + TokenUser = 1,TokenGroups,TokenPrivileges,TokenOwner,TokenPrimaryGroup,TokenDefaultDacl,TokenSource,TokenType,TokenImpersonationLevel, + TokenStatistics,TokenRestrictedSids,TokenSessionId,TokenGroupsAndPrivileges,TokenSessionReference,TokenSandBoxInert,TokenAuditPolicy, + TokenOrigin,MaxTokenInfoClass + } TOKEN_INFORMATION_CLASS,*PTOKEN_INFORMATION_CLASS; + + typedef struct _TOKEN_USER { + SID_AND_ATTRIBUTES User; + } TOKEN_USER,*PTOKEN_USER; + + typedef struct _TOKEN_GROUPS { + DWORD GroupCount; + SID_AND_ATTRIBUTES Groups[ANYSIZE_ARRAY]; + } TOKEN_GROUPS,*PTOKEN_GROUPS; + + typedef struct _TOKEN_PRIVILEGES { + DWORD PrivilegeCount; + LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]; + } TOKEN_PRIVILEGES,*PTOKEN_PRIVILEGES; + + typedef struct _TOKEN_OWNER { + PSID Owner; + } TOKEN_OWNER,*PTOKEN_OWNER; + + typedef struct _TOKEN_PRIMARY_GROUP { + PSID PrimaryGroup; + } TOKEN_PRIMARY_GROUP,*PTOKEN_PRIMARY_GROUP; + + typedef struct _TOKEN_DEFAULT_DACL { + PACL DefaultDacl; + } TOKEN_DEFAULT_DACL,*PTOKEN_DEFAULT_DACL; + + typedef struct _TOKEN_GROUPS_AND_PRIVILEGES { + DWORD SidCount; + DWORD SidLength; + PSID_AND_ATTRIBUTES Sids; + DWORD RestrictedSidCount; + DWORD RestrictedSidLength; + PSID_AND_ATTRIBUTES RestrictedSids; + DWORD PrivilegeCount; + DWORD PrivilegeLength; + PLUID_AND_ATTRIBUTES Privileges; + LUID AuthenticationId; + } TOKEN_GROUPS_AND_PRIVILEGES,*PTOKEN_GROUPS_AND_PRIVILEGES; + +#define TOKEN_AUDIT_SUCCESS_INCLUDE 0x1 +#define TOKEN_AUDIT_SUCCESS_EXCLUDE 0x2 +#define TOKEN_AUDIT_FAILURE_INCLUDE 0x4 +#define TOKEN_AUDIT_FAILURE_EXCLUDE 0x8 + +#define VALID_AUDIT_POLICY_BITS (TOKEN_AUDIT_SUCCESS_INCLUDE | TOKEN_AUDIT_SUCCESS_EXCLUDE | TOKEN_AUDIT_FAILURE_INCLUDE | TOKEN_AUDIT_FAILURE_EXCLUDE) +#define VALID_TOKEN_AUDIT_POLICY_ELEMENT(P) ((((P).PolicyMask & ~VALID_AUDIT_POLICY_BITS)==0) && ((P).Category <= AuditEventMaxType)) + + typedef struct _TOKEN_AUDIT_POLICY_ELEMENT { + DWORD Category; + DWORD PolicyMask; + } TOKEN_AUDIT_POLICY_ELEMENT,*PTOKEN_AUDIT_POLICY_ELEMENT; + + typedef struct _TOKEN_AUDIT_POLICY { + DWORD PolicyCount; + TOKEN_AUDIT_POLICY_ELEMENT Policy[ANYSIZE_ARRAY]; + } TOKEN_AUDIT_POLICY,*PTOKEN_AUDIT_POLICY; + +#define PER_USER_AUDITING_POLICY_SIZE(p) (sizeof(TOKEN_AUDIT_POLICY) + (((p)->PolicyCount > ANYSIZE_ARRAY) ? (sizeof(TOKEN_AUDIT_POLICY_ELEMENT) *((p)->PolicyCount - ANYSIZE_ARRAY)) : 0)) +#define PER_USER_AUDITING_POLICY_SIZE_BY_COUNT(C) (sizeof(TOKEN_AUDIT_POLICY) + (((C) > ANYSIZE_ARRAY) ? (sizeof(TOKEN_AUDIT_POLICY_ELEMENT) *((C) - ANYSIZE_ARRAY)) : 0)) + +#define TOKEN_SOURCE_LENGTH 8 + + typedef struct _TOKEN_SOURCE { + CHAR SourceName[TOKEN_SOURCE_LENGTH]; + LUID SourceIdentifier; + } TOKEN_SOURCE,*PTOKEN_SOURCE; + + typedef struct _TOKEN_STATISTICS { + LUID TokenId; + LUID AuthenticationId; + LARGE_INTEGER ExpirationTime; + TOKEN_TYPE TokenType; + SECURITY_IMPERSONATION_LEVEL ImpersonationLevel; + DWORD DynamicCharged; + DWORD DynamicAvailable; + DWORD GroupCount; + DWORD PrivilegeCount; + LUID ModifiedId; + } TOKEN_STATISTICS,*PTOKEN_STATISTICS; + + typedef struct _TOKEN_CONTROL { + LUID TokenId; + LUID AuthenticationId; + LUID ModifiedId; + TOKEN_SOURCE TokenSource; + } TOKEN_CONTROL,*PTOKEN_CONTROL; + + typedef struct _TOKEN_ORIGIN { + LUID OriginatingLogonSession; + } TOKEN_ORIGIN,*PTOKEN_ORIGIN; + +#define SECURITY_DYNAMIC_TRACKING (TRUE) +#define SECURITY_STATIC_TRACKING (FALSE) + + typedef BOOLEAN SECURITY_CONTEXT_TRACKING_MODE,*PSECURITY_CONTEXT_TRACKING_MODE; + + typedef struct _SECURITY_QUALITY_OF_SERVICE { + DWORD Length; + SECURITY_IMPERSONATION_LEVEL ImpersonationLevel; + SECURITY_CONTEXT_TRACKING_MODE ContextTrackingMode; + BOOLEAN EffectiveOnly; + } SECURITY_QUALITY_OF_SERVICE,*PSECURITY_QUALITY_OF_SERVICE; + + typedef struct _SE_IMPERSONATION_STATE { + PACCESS_TOKEN Token; + BOOLEAN CopyOnOpen; + BOOLEAN EffectiveOnly; + SECURITY_IMPERSONATION_LEVEL Level; + } SE_IMPERSONATION_STATE,*PSE_IMPERSONATION_STATE; + +#define DISABLE_MAX_PRIVILEGE 0x1 +#define SANDBOX_INERT 0x2 + + typedef DWORD SECURITY_INFORMATION,*PSECURITY_INFORMATION; + +#define OWNER_SECURITY_INFORMATION (0x00000001L) +#define GROUP_SECURITY_INFORMATION (0x00000002L) +#define DACL_SECURITY_INFORMATION (0x00000004L) +#define SACL_SECURITY_INFORMATION (0x00000008L) + +#define PROTECTED_DACL_SECURITY_INFORMATION (0x80000000L) +#define PROTECTED_SACL_SECURITY_INFORMATION (0x40000000L) +#define UNPROTECTED_DACL_SECURITY_INFORMATION (0x20000000L) +#define UNPROTECTED_SACL_SECURITY_INFORMATION (0x10000000L) + +#define PROCESS_TERMINATE (0x0001) +#define PROCESS_CREATE_THREAD (0x0002) +#define PROCESS_SET_SESSIONID (0x0004) +#define PROCESS_VM_OPERATION (0x0008) +#define PROCESS_VM_READ (0x0010) +#define PROCESS_VM_WRITE (0x0020) +#define PROCESS_DUP_HANDLE (0x0040) +#define PROCESS_CREATE_PROCESS (0x0080) +#define PROCESS_SET_QUOTA (0x0100) +#define PROCESS_SET_INFORMATION (0x0200) +#define PROCESS_QUERY_INFORMATION (0x0400) +#define PROCESS_SUSPEND_RESUME (0x0800) +#define PROCESS_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF) + +#ifdef _WIN64 +#define MAXIMUM_PROCESSORS 64 +#else +#define MAXIMUM_PROCESSORS 32 +#endif + +#define THREAD_TERMINATE (0x0001) +#define THREAD_SUSPEND_RESUME (0x0002) +#define THREAD_GET_CONTEXT (0x0008) +#define THREAD_SET_CONTEXT (0x0010) +#define THREAD_SET_INFORMATION (0x0020) +#define THREAD_QUERY_INFORMATION (0x0040) +#define THREAD_SET_THREAD_TOKEN (0x0080) +#define THREAD_IMPERSONATE (0x0100) +#define THREAD_DIRECT_IMPERSONATION (0x0200) + +#define THREAD_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3FF) + +#define JOB_OBJECT_ASSIGN_PROCESS (0x0001) +#define JOB_OBJECT_SET_ATTRIBUTES (0x0002) +#define JOB_OBJECT_QUERY (0x0004) +#define JOB_OBJECT_TERMINATE (0x0008) +#define JOB_OBJECT_SET_SECURITY_ATTRIBUTES (0x0010) +#define JOB_OBJECT_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1F) + + typedef struct _JOB_SET_ARRAY { + HANDLE JobHandle; + DWORD MemberLevel; + DWORD Flags; + } JOB_SET_ARRAY,*PJOB_SET_ARRAY; + +#define FLS_MAXIMUM_AVAILABLE 128 +#define TLS_MINIMUM_AVAILABLE 64 + +#ifndef _NT_TIB_DEFINED +#define _NT_TIB_DEFINED + typedef struct _NT_TIB { + struct _EXCEPTION_REGISTRATION_RECORD *ExceptionList; + PVOID StackBase; + PVOID StackLimit; + PVOID SubSystemTib; + union { + PVOID FiberData; + DWORD Version; + }; + PVOID ArbitraryUserPointer; + struct _NT_TIB *Self; + } NT_TIB; + typedef NT_TIB *PNT_TIB; +#endif + + typedef struct _NT_TIB32 { + DWORD ExceptionList; + DWORD StackBase; + DWORD StackLimit; + DWORD SubSystemTib; + union { + DWORD FiberData; + DWORD Version; + }; + DWORD ArbitraryUserPointer; + DWORD Self; + } NT_TIB32,*PNT_TIB32; + + typedef struct _NT_TIB64 { + DWORD64 ExceptionList; + DWORD64 StackBase; + DWORD64 StackLimit; + DWORD64 SubSystemTib; + union { + DWORD64 FiberData; + DWORD Version; + }; + DWORD64 ArbitraryUserPointer; + DWORD64 Self; + } NT_TIB64,*PNT_TIB64; + +#if !defined(I_X86_) && !defined(_IA64_) && !defined(_AMD64_) +#define WX86 +#endif + +#define THREAD_BASE_PRIORITY_LOWRT 15 +#define THREAD_BASE_PRIORITY_MAX 2 +#define THREAD_BASE_PRIORITY_MIN (-2) +#define THREAD_BASE_PRIORITY_IDLE (-15) + + typedef struct _QUOTA_LIMITS { + SIZE_T PagedPoolLimit; + SIZE_T NonPagedPoolLimit; + SIZE_T MinimumWorkingSetSize; + SIZE_T MaximumWorkingSetSize; + SIZE_T PagefileLimit; + LARGE_INTEGER TimeLimit; + } QUOTA_LIMITS,*PQUOTA_LIMITS; + +#define QUOTA_LIMITS_HARDWS_MIN_ENABLE 0x00000001 +#define QUOTA_LIMITS_HARDWS_MIN_DISABLE 0x00000002 +#define QUOTA_LIMITS_HARDWS_MAX_ENABLE 0x00000004 +#define QUOTA_LIMITS_HARDWS_MAX_DISABLE 0x00000008 + + typedef struct _QUOTA_LIMITS_EX { + SIZE_T PagedPoolLimit; + SIZE_T NonPagedPoolLimit; + SIZE_T MinimumWorkingSetSize; + SIZE_T MaximumWorkingSetSize; + SIZE_T PagefileLimit; + LARGE_INTEGER TimeLimit; + SIZE_T Reserved1; + SIZE_T Reserved2; + SIZE_T Reserved3; + SIZE_T Reserved4; + DWORD Flags; + DWORD Reserved5; + } QUOTA_LIMITS_EX,*PQUOTA_LIMITS_EX; + + typedef struct _IO_COUNTERS { + ULONGLONG ReadOperationCount; + ULONGLONG WriteOperationCount; + ULONGLONG OtherOperationCount; + ULONGLONG ReadTransferCount; + ULONGLONG WriteTransferCount; + ULONGLONG OtherTransferCount; + } IO_COUNTERS; + typedef IO_COUNTERS *PIO_COUNTERS; + + typedef struct _JOBOBJECT_BASIC_ACCOUNTING_INFORMATION { + LARGE_INTEGER TotalUserTime; + LARGE_INTEGER TotalKernelTime; + LARGE_INTEGER ThisPeriodTotalUserTime; + LARGE_INTEGER ThisPeriodTotalKernelTime; + DWORD TotalPageFaultCount; + DWORD TotalProcesses; + DWORD ActiveProcesses; + DWORD TotalTerminatedProcesses; + } JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,*PJOBOBJECT_BASIC_ACCOUNTING_INFORMATION; + + typedef struct _JOBOBJECT_BASIC_LIMIT_INFORMATION { + LARGE_INTEGER PerProcessUserTimeLimit; + LARGE_INTEGER PerJobUserTimeLimit; + DWORD LimitFlags; + SIZE_T MinimumWorkingSetSize; + SIZE_T MaximumWorkingSetSize; + DWORD ActiveProcessLimit; + ULONG_PTR Affinity; + DWORD PriorityClass; + DWORD SchedulingClass; + } JOBOBJECT_BASIC_LIMIT_INFORMATION,*PJOBOBJECT_BASIC_LIMIT_INFORMATION; + + typedef struct _JOBOBJECT_EXTENDED_LIMIT_INFORMATION { + JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation; + IO_COUNTERS IoInfo; + SIZE_T ProcessMemoryLimit; + SIZE_T JobMemoryLimit; + SIZE_T PeakProcessMemoryUsed; + SIZE_T PeakJobMemoryUsed; + } JOBOBJECT_EXTENDED_LIMIT_INFORMATION,*PJOBOBJECT_EXTENDED_LIMIT_INFORMATION; + + typedef struct _JOBOBJECT_BASIC_PROCESS_ID_LIST { + DWORD NumberOfAssignedProcesses; + DWORD NumberOfProcessIdsInList; + ULONG_PTR ProcessIdList[1]; + } JOBOBJECT_BASIC_PROCESS_ID_LIST,*PJOBOBJECT_BASIC_PROCESS_ID_LIST; + + typedef struct _JOBOBJECT_BASIC_UI_RESTRICTIONS { + DWORD UIRestrictionsClass; + } JOBOBJECT_BASIC_UI_RESTRICTIONS,*PJOBOBJECT_BASIC_UI_RESTRICTIONS; + + typedef struct _JOBOBJECT_SECURITY_LIMIT_INFORMATION { + DWORD SecurityLimitFlags; + HANDLE JobToken; + PTOKEN_GROUPS SidsToDisable; + PTOKEN_PRIVILEGES PrivilegesToDelete; + PTOKEN_GROUPS RestrictedSids; + } JOBOBJECT_SECURITY_LIMIT_INFORMATION,*PJOBOBJECT_SECURITY_LIMIT_INFORMATION; + + typedef struct _JOBOBJECT_END_OF_JOB_TIME_INFORMATION { + DWORD EndOfJobTimeAction; + } JOBOBJECT_END_OF_JOB_TIME_INFORMATION,*PJOBOBJECT_END_OF_JOB_TIME_INFORMATION; + + typedef struct _JOBOBJECT_ASSOCIATE_COMPLETION_PORT { + PVOID CompletionKey; + HANDLE CompletionPort; + } JOBOBJECT_ASSOCIATE_COMPLETION_PORT,*PJOBOBJECT_ASSOCIATE_COMPLETION_PORT; + + typedef struct _JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION { + JOBOBJECT_BASIC_ACCOUNTING_INFORMATION BasicInfo; + IO_COUNTERS IoInfo; + } JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION,*PJOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION; + + typedef struct _JOBOBJECT_JOBSET_INFORMATION { + DWORD MemberLevel; + } JOBOBJECT_JOBSET_INFORMATION,*PJOBOBJECT_JOBSET_INFORMATION; + +#define JOB_OBJECT_TERMINATE_AT_END_OF_JOB 0 +#define JOB_OBJECT_POST_AT_END_OF_JOB 1 + +#define JOB_OBJECT_MSG_END_OF_JOB_TIME 1 +#define JOB_OBJECT_MSG_END_OF_PROCESS_TIME 2 +#define JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT 3 +#define JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO 4 +#define JOB_OBJECT_MSG_NEW_PROCESS 6 +#define JOB_OBJECT_MSG_EXIT_PROCESS 7 +#define JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS 8 +#define JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT 9 +#define JOB_OBJECT_MSG_JOB_MEMORY_LIMIT 10 + +#define JOB_OBJECT_LIMIT_WORKINGSET 0x00000001 +#define JOB_OBJECT_LIMIT_PROCESS_TIME 0x00000002 +#define JOB_OBJECT_LIMIT_JOB_TIME 0x00000004 +#define JOB_OBJECT_LIMIT_ACTIVE_PROCESS 0x00000008 +#define JOB_OBJECT_LIMIT_AFFINITY 0x00000010 +#define JOB_OBJECT_LIMIT_PRIORITY_CLASS 0x00000020 +#define JOB_OBJECT_LIMIT_PRESERVE_JOB_TIME 0x00000040 +#define JOB_OBJECT_LIMIT_SCHEDULING_CLASS 0x00000080 + +#define JOB_OBJECT_LIMIT_PROCESS_MEMORY 0x00000100 +#define JOB_OBJECT_LIMIT_JOB_MEMORY 0x00000200 +#define JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION 0x00000400 +#define JOB_OBJECT_LIMIT_BREAKAWAY_OK 0x00000800 +#define JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK 0x00001000 +#define JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 0x00002000 + +#define JOB_OBJECT_LIMIT_RESERVED2 0x00004000 +#define JOB_OBJECT_LIMIT_RESERVED3 0x00008000 +#define JOB_OBJECT_LIMIT_RESERVED4 0x00010000 +#define JOB_OBJECT_LIMIT_RESERVED5 0x00020000 +#define JOB_OBJECT_LIMIT_RESERVED6 0x00040000 + +#define JOB_OBJECT_LIMIT_VALID_FLAGS 0x0007ffff + +#define JOB_OBJECT_BASIC_LIMIT_VALID_FLAGS 0x000000ff +#define JOB_OBJECT_EXTENDED_LIMIT_VALID_FLAGS 0x00003fff +#define JOB_OBJECT_RESERVED_LIMIT_VALID_FLAGS 0x0007ffff + +#define JOB_OBJECT_UILIMIT_NONE 0x00000000 + +#define JOB_OBJECT_UILIMIT_HANDLES 0x00000001 +#define JOB_OBJECT_UILIMIT_READCLIPBOARD 0x00000002 +#define JOB_OBJECT_UILIMIT_WRITECLIPBOARD 0x00000004 +#define JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS 0x00000008 +#define JOB_OBJECT_UILIMIT_DISPLAYSETTINGS 0x00000010 +#define JOB_OBJECT_UILIMIT_GLOBALATOMS 0x00000020 +#define JOB_OBJECT_UILIMIT_DESKTOP 0x00000040 +#define JOB_OBJECT_UILIMIT_EXITWINDOWS 0x00000080 + +#define JOB_OBJECT_UILIMIT_ALL 0x000000FF + +#define JOB_OBJECT_UI_VALID_FLAGS 0x000000FF + +#define JOB_OBJECT_SECURITY_NO_ADMIN 0x00000001 +#define JOB_OBJECT_SECURITY_RESTRICTED_TOKEN 0x00000002 +#define JOB_OBJECT_SECURITY_ONLY_TOKEN 0x00000004 +#define JOB_OBJECT_SECURITY_FILTER_TOKENS 0x00000008 + +#define JOB_OBJECT_SECURITY_VALID_FLAGS 0x0000000f + + typedef enum _JOBOBJECTINFOCLASS { + JobObjectBasicAccountingInformation = 1,JobObjectBasicLimitInformation,JobObjectBasicProcessIdList,JobObjectBasicUIRestrictions, + JobObjectSecurityLimitInformation,JobObjectEndOfJobTimeInformation,JobObjectAssociateCompletionPortInformation, + JobObjectBasicAndIoAccountingInformation,JobObjectExtendedLimitInformation,JobObjectJobSetInformation,MaxJobObjectInfoClass + } JOBOBJECTINFOCLASS; + +#define EVENT_MODIFY_STATE 0x0002 +#define EVENT_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|0x3) + +#define MUTANT_QUERY_STATE 0x0001 + +#define MUTANT_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE| MUTANT_QUERY_STATE) +#define SEMAPHORE_MODIFY_STATE 0x0002 +#define SEMAPHORE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|0x3) + +#define TIMER_QUERY_STATE 0x0001 +#define TIMER_MODIFY_STATE 0x0002 + +#define TIMER_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE| TIMER_QUERY_STATE|TIMER_MODIFY_STATE) + +#define TIME_ZONE_ID_UNKNOWN 0 +#define TIME_ZONE_ID_STANDARD 1 +#define TIME_ZONE_ID_DAYLIGHT 2 + + typedef enum _LOGICAL_PROCESSOR_RELATIONSHIP { + RelationProcessorCore,RelationNumaNode,RelationCache + } LOGICAL_PROCESSOR_RELATIONSHIP; + +#define LTP_PC_SMT 0x1 + + typedef enum _PROCESSOR_CACHE_TYPE { + CacheUnified,CacheInstruction,CacheData,CacheTrace + } PROCESSOR_CACHE_TYPE; + +#define CACHE_FULLY_ASSOCIATIVE 0xFF + + typedef struct _CACHE_DESCRIPTOR { + BYTE Level; + BYTE Associativity; + WORD LineSize; + DWORD Size; + PROCESSOR_CACHE_TYPE Type; + } CACHE_DESCRIPTOR,*PCACHE_DESCRIPTOR; + + typedef struct _SYSTEM_LOGICAL_PROCESSOR_INFORMATION { + ULONG_PTR ProcessorMask; + LOGICAL_PROCESSOR_RELATIONSHIP Relationship; + union { + struct { + BYTE Flags; + } ProcessorCore; + struct { + DWORD NodeNumber; + } NumaNode; + CACHE_DESCRIPTOR Cache; + ULONGLONG Reserved[2]; + }; + } SYSTEM_LOGICAL_PROCESSOR_INFORMATION,*PSYSTEM_LOGICAL_PROCESSOR_INFORMATION; + +#define PROCESSOR_INTEL_386 386 +#define PROCESSOR_INTEL_486 486 +#define PROCESSOR_INTEL_PENTIUM 586 +#define PROCESSOR_INTEL_IA64 2200 +#define PROCESSOR_AMD_X8664 8664 +#define PROCESSOR_MIPS_R4000 4000 +#define PROCESSOR_ALPHA_21064 21064 +#define PROCESSOR_PPC_601 601 +#define PROCESSOR_PPC_603 603 +#define PROCESSOR_PPC_604 604 +#define PROCESSOR_PPC_620 620 +#define PROCESSOR_HITACHI_SH3 10003 +#define PROCESSOR_HITACHI_SH3E 10004 +#define PROCESSOR_HITACHI_SH4 10005 +#define PROCESSOR_MOTOROLA_821 821 +#define PROCESSOR_SHx_SH3 103 +#define PROCESSOR_SHx_SH4 104 +#define PROCESSOR_STRONGARM 2577 +#define PROCESSOR_ARM720 1824 +#define PROCESSOR_ARM820 2080 +#define PROCESSOR_ARM920 2336 +#define PROCESSOR_ARM_7TDMI 70001 +#define PROCESSOR_OPTIL 0x494f + +#define PROCESSOR_ARCHITECTURE_INTEL 0 +#define PROCESSOR_ARCHITECTURE_MIPS 1 +#define PROCESSOR_ARCHITECTURE_ALPHA 2 +#define PROCESSOR_ARCHITECTURE_PPC 3 +#define PROCESSOR_ARCHITECTURE_SHX 4 +#define PROCESSOR_ARCHITECTURE_ARM 5 +#define PROCESSOR_ARCHITECTURE_IA64 6 +#define PROCESSOR_ARCHITECTURE_ALPHA64 7 +#define PROCESSOR_ARCHITECTURE_MSIL 8 +#define PROCESSOR_ARCHITECTURE_AMD64 9 +#define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10 + +#define PROCESSOR_ARCHITECTURE_UNKNOWN 0xFFFF + +#define PF_FLOATING_POINT_PRECISION_ERRATA 0 +#define PF_FLOATING_POINT_EMULATED 1 +#define PF_COMPARE_EXCHANGE_DOUBLE 2 +#define PF_MMX_INSTRUCTIONS_AVAILABLE 3 +#define PF_PPC_MOVEMEM_64BIT_OK 4 +#define PF_ALPHA_BYTE_INSTRUCTIONS 5 +#define PF_XMMI_INSTRUCTIONS_AVAILABLE 6 +#define PF_3DNOW_INSTRUCTIONS_AVAILABLE 7 +#define PF_RDTSC_INSTRUCTION_AVAILABLE 8 +#define PF_PAE_ENABLED 9 +#define PF_XMMI64_INSTRUCTIONS_AVAILABLE 10 +#define PF_SSE_DAZ_MODE_AVAILABLE 11 +#define PF_NX_ENABLED 12 + + typedef struct _MEMORY_BASIC_INFORMATION { + PVOID BaseAddress; + PVOID AllocationBase; + DWORD AllocationProtect; + SIZE_T RegionSize; + DWORD State; + DWORD Protect; + DWORD Type; + } MEMORY_BASIC_INFORMATION,*PMEMORY_BASIC_INFORMATION; + + typedef struct _MEMORY_BASIC_INFORMATION32 { + DWORD BaseAddress; + DWORD AllocationBase; + DWORD AllocationProtect; + DWORD RegionSize; + DWORD State; + DWORD Protect; + DWORD Type; + } MEMORY_BASIC_INFORMATION32,*PMEMORY_BASIC_INFORMATION32; + + typedef DECLSPEC_ALIGN(16) struct _MEMORY_BASIC_INFORMATION64 { + ULONGLONG BaseAddress; + ULONGLONG AllocationBase; + DWORD AllocationProtect; + DWORD __alignment1; + ULONGLONG RegionSize; + DWORD State; + DWORD Protect; + DWORD Type; + DWORD __alignment2; + } MEMORY_BASIC_INFORMATION64,*PMEMORY_BASIC_INFORMATION64; + +#define SECTION_QUERY 0x0001 +#define SECTION_MAP_WRITE 0x0002 +#define SECTION_MAP_READ 0x0004 +#define SECTION_MAP_EXECUTE 0x0008 +#define SECTION_EXTEND_SIZE 0x0010 +#define SECTION_MAP_EXECUTE_EXPLICIT 0x0020 + +#define SECTION_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SECTION_QUERY| SECTION_MAP_WRITE | SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_EXTEND_SIZE) +#define PAGE_NOACCESS 0x01 +#define PAGE_READONLY 0x02 +#define PAGE_READWRITE 0x04 +#define PAGE_WRITECOPY 0x08 +#define PAGE_EXECUTE 0x10 +#define PAGE_EXECUTE_READ 0x20 +#define PAGE_EXECUTE_READWRITE 0x40 +#define PAGE_EXECUTE_WRITECOPY 0x80 +#define PAGE_GUARD 0x100 +#define PAGE_NOCACHE 0x200 +#define PAGE_WRITECOMBINE 0x400 +#define MEM_COMMIT 0x1000 +#define MEM_RESERVE 0x2000 +#define MEM_DECOMMIT 0x4000 +#define MEM_RELEASE 0x8000 +#define MEM_FREE 0x10000 +#define MEM_PRIVATE 0x20000 +#define MEM_MAPPED 0x40000 +#define MEM_RESET 0x80000 +#define MEM_TOP_DOWN 0x100000 +#define MEM_WRITE_WATCH 0x200000 +#define MEM_PHYSICAL 0x400000 +#define MEM_LARGE_PAGES 0x20000000 +#define MEM_4MB_PAGES 0x80000000 +#define SEC_FILE 0x800000 +#define SEC_IMAGE 0x1000000 +#define SEC_RESERVE 0x4000000 +#define SEC_COMMIT 0x8000000 +#define SEC_NOCACHE 0x10000000 +#define SEC_LARGE_PAGES 0x80000000 +#define MEM_IMAGE SEC_IMAGE +#define WRITE_WATCH_FLAG_RESET 0x01 + +#define FILE_READ_DATA (0x0001) +#define FILE_LIST_DIRECTORY (0x0001) + +#define FILE_WRITE_DATA (0x0002) +#define FILE_ADD_FILE (0x0002) + +#define FILE_APPEND_DATA (0x0004) +#define FILE_ADD_SUBDIRECTORY (0x0004) +#define FILE_CREATE_PIPE_INSTANCE (0x0004) + +#define FILE_READ_EA (0x0008) + +#define FILE_WRITE_EA (0x0010) + +#define FILE_EXECUTE (0x0020) +#define FILE_TRAVERSE (0x0020) + +#define FILE_DELETE_CHILD (0x0040) + +#define FILE_READ_ATTRIBUTES (0x0080) + +#define FILE_WRITE_ATTRIBUTES (0x0100) + +#define FILE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF) +#define FILE_GENERIC_READ (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE) +#define FILE_GENERIC_WRITE (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE) +#define FILE_GENERIC_EXECUTE (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE) + +#define FILE_SHARE_READ 0x00000001 +#define FILE_SHARE_WRITE 0x00000002 +#define FILE_SHARE_DELETE 0x00000004 +#define FILE_ATTRIBUTE_READONLY 0x00000001 +#define FILE_ATTRIBUTE_HIDDEN 0x00000002 +#define FILE_ATTRIBUTE_SYSTEM 0x00000004 +#define FILE_ATTRIBUTE_DIRECTORY 0x00000010 +#define FILE_ATTRIBUTE_ARCHIVE 0x00000020 +#define FILE_ATTRIBUTE_DEVICE 0x00000040 +#define FILE_ATTRIBUTE_NORMAL 0x00000080 +#define FILE_ATTRIBUTE_TEMPORARY 0x00000100 +#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200 +#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400 +#define FILE_ATTRIBUTE_COMPRESSED 0x00000800 +#define FILE_ATTRIBUTE_OFFLINE 0x00001000 +#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000 +#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000 +#define FILE_NOTIFY_CHANGE_FILE_NAME 0x00000001 +#define FILE_NOTIFY_CHANGE_DIR_NAME 0x00000002 +#define FILE_NOTIFY_CHANGE_ATTRIBUTES 0x00000004 +#define FILE_NOTIFY_CHANGE_SIZE 0x00000008 +#define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010 +#define FILE_NOTIFY_CHANGE_LAST_ACCESS 0x00000020 +#define FILE_NOTIFY_CHANGE_CREATION 0x00000040 +#define FILE_NOTIFY_CHANGE_SECURITY 0x00000100 +#define FILE_ACTION_ADDED 0x00000001 +#define FILE_ACTION_REMOVED 0x00000002 +#define FILE_ACTION_MODIFIED 0x00000003 +#define FILE_ACTION_RENAMED_OLD_NAME 0x00000004 +#define FILE_ACTION_RENAMED_NEW_NAME 0x00000005 +#define MAILSLOT_NO_MESSAGE ((DWORD)-1) +#define MAILSLOT_WAIT_FOREVER ((DWORD)-1) +#define FILE_CASE_SENSITIVE_SEARCH 0x00000001 +#define FILE_CASE_PRESERVED_NAMES 0x00000002 +#define FILE_UNICODE_ON_DISK 0x00000004 +#define FILE_PERSISTENT_ACLS 0x00000008 +#define FILE_FILE_COMPRESSION 0x00000010 +#define FILE_VOLUME_QUOTAS 0x00000020 +#define FILE_SUPPORTS_SPARSE_FILES 0x00000040 +#define FILE_SUPPORTS_REPARSE_POINTS 0x00000080 +#define FILE_SUPPORTS_REMOTE_STORAGE 0x00000100 +#define FILE_VOLUME_IS_COMPRESSED 0x00008000 +#define FILE_SUPPORTS_OBJECT_IDS 0x00010000 +#define FILE_SUPPORTS_ENCRYPTION 0x00020000 +#define FILE_NAMED_STREAMS 0x00040000 +#define FILE_READ_ONLY_VOLUME 0x00080000 + + typedef struct _FILE_NOTIFY_INFORMATION { + DWORD NextEntryOffset; + DWORD Action; + DWORD FileNameLength; + WCHAR FileName[1]; + } FILE_NOTIFY_INFORMATION,*PFILE_NOTIFY_INFORMATION; + + typedef union _FILE_SEGMENT_ELEMENT { + PVOID64 Buffer; + ULONGLONG Alignment; + }FILE_SEGMENT_ELEMENT,*PFILE_SEGMENT_ELEMENT; + + typedef struct _REPARSE_GUID_DATA_BUFFER { + DWORD ReparseTag; + WORD ReparseDataLength; + WORD Reserved; + GUID ReparseGuid; + struct { + BYTE DataBuffer[1]; + } GenericReparseBuffer; + } REPARSE_GUID_DATA_BUFFER,*PREPARSE_GUID_DATA_BUFFER; + +#define REPARSE_GUID_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_GUID_DATA_BUFFER,GenericReparseBuffer) + +#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE (16 *1024) + +#define IO_REPARSE_TAG_RESERVED_ZERO (0) +#define IO_REPARSE_TAG_RESERVED_ONE (1) + +#define IO_REPARSE_TAG_RESERVED_RANGE IO_REPARSE_TAG_RESERVED_ONE + +#define IsReparseTagMicrosoft(_tag) (((_tag) & 0x80000000)) +#define IsReparseTagNameSurrogate(_tag) (((_tag) & 0x20000000)) + +#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) +#define IO_REPARSE_TAG_HSM (0xC0000004L) +#define IO_REPARSE_TAG_SIS (0x80000007L) +#define IO_REPARSE_TAG_DFS (0x8000000AL) +#define IO_REPARSE_TAG_FILTER_MANAGER (0x8000000BL) +#define IO_COMPLETION_MODIFY_STATE 0x0002 +#define IO_COMPLETION_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SYNCHRONIZE|0x3) +#define DUPLICATE_CLOSE_SOURCE 0x00000001 +#define DUPLICATE_SAME_ACCESS 0x00000002 + + typedef enum _SYSTEM_POWER_STATE { + PowerSystemUnspecified = 0,PowerSystemWorking = 1,PowerSystemSleeping1 = 2,PowerSystemSleeping2 = 3,PowerSystemSleeping3 = 4,PowerSystemHibernate = 5,PowerSystemShutdown = 6,PowerSystemMaximum = 7 + } SYSTEM_POWER_STATE,*PSYSTEM_POWER_STATE; + +#define POWER_SYSTEM_MAXIMUM 7 + + typedef enum { + PowerActionNone = 0,PowerActionReserved,PowerActionSleep,PowerActionHibernate,PowerActionShutdown,PowerActionShutdownReset,PowerActionShutdownOff,PowerActionWarmEject + } POWER_ACTION,*PPOWER_ACTION; + + typedef enum _DEVICE_POWER_STATE { + PowerDeviceUnspecified = 0,PowerDeviceD0,PowerDeviceD1,PowerDeviceD2,PowerDeviceD3,PowerDeviceMaximum + } DEVICE_POWER_STATE,*PDEVICE_POWER_STATE; + +#define ES_SYSTEM_REQUIRED ((DWORD)0x00000001) +#define ES_DISPLAY_REQUIRED ((DWORD)0x00000002) +#define ES_USER_PRESENT ((DWORD)0x00000004) +#define ES_CONTINUOUS ((DWORD)0x80000000) + + typedef DWORD EXECUTION_STATE; + + typedef enum { + LT_DONT_CARE,LT_LOWEST_LATENCY + } LATENCY_TIME; + +#define PDCAP_D0_SUPPORTED 0x00000001 +#define PDCAP_D1_SUPPORTED 0x00000002 +#define PDCAP_D2_SUPPORTED 0x00000004 +#define PDCAP_D3_SUPPORTED 0x00000008 +#define PDCAP_WAKE_FROM_D0_SUPPORTED 0x00000010 +#define PDCAP_WAKE_FROM_D1_SUPPORTED 0x00000020 +#define PDCAP_WAKE_FROM_D2_SUPPORTED 0x00000040 +#define PDCAP_WAKE_FROM_D3_SUPPORTED 0x00000080 +#define PDCAP_WARM_EJECT_SUPPORTED 0x00000100 + + typedef struct CM_Power_Data_s { + DWORD PD_Size; + DEVICE_POWER_STATE PD_MostRecentPowerState; + DWORD PD_Capabilities; + DWORD PD_D1Latency; + DWORD PD_D2Latency; + DWORD PD_D3Latency; + DEVICE_POWER_STATE PD_PowerStateMapping[POWER_SYSTEM_MAXIMUM]; + SYSTEM_POWER_STATE PD_DeepestSystemWake; + } CM_POWER_DATA,*PCM_POWER_DATA; + + typedef enum { + SystemPowerPolicyAc,SystemPowerPolicyDc,VerifySystemPolicyAc,VerifySystemPolicyDc,SystemPowerCapabilities,SystemBatteryState,SystemPowerStateHandler,ProcessorStateHandler,SystemPowerPolicyCurrent,AdministratorPowerPolicy,SystemReserveHiberFile,ProcessorInformation,SystemPowerInformation,ProcessorStateHandler2,LastWakeTime,LastSleepTime,SystemExecutionState,SystemPowerStateNotifyHandler,ProcessorPowerPolicyAc,ProcessorPowerPolicyDc,VerifyProcessorPowerPolicyAc,VerifyProcessorPowerPolicyDc,ProcessorPowerPolicyCurrent,SystemPowerStateLogging,SystemPowerLoggingEntry + } POWER_INFORMATION_LEVEL; + + typedef struct { + DWORD Granularity; + DWORD Capacity; + } BATTERY_REPORTING_SCALE,*PBATTERY_REPORTING_SCALE; + + typedef struct { + POWER_ACTION Action; + DWORD Flags; + DWORD EventCode; + } POWER_ACTION_POLICY,*PPOWER_ACTION_POLICY; + +#define POWER_ACTION_QUERY_ALLOWED 0x00000001 +#define POWER_ACTION_UI_ALLOWED 0x00000002 +#define POWER_ACTION_OVERRIDE_APPS 0x00000004 +#define POWER_ACTION_LIGHTEST_FIRST 0x10000000 +#define POWER_ACTION_LOCK_CONSOLE 0x20000000 +#define POWER_ACTION_DISABLE_WAKES 0x40000000 +#define POWER_ACTION_CRITICAL 0x80000000 + +#define POWER_LEVEL_USER_NOTIFY_TEXT 0x00000001 +#define POWER_LEVEL_USER_NOTIFY_SOUND 0x00000002 +#define POWER_LEVEL_USER_NOTIFY_EXEC 0x00000004 +#define POWER_USER_NOTIFY_BUTTON 0x00000008 +#define POWER_USER_NOTIFY_SHUTDOWN 0x00000010 +#define POWER_FORCE_TRIGGER_RESET 0x80000000 + + typedef struct { + BOOLEAN Enable; + BYTE Spare[3]; + DWORD BatteryLevel; + POWER_ACTION_POLICY PowerPolicy; + SYSTEM_POWER_STATE MinSystemState; + } SYSTEM_POWER_LEVEL,*PSYSTEM_POWER_LEVEL; + +#define NUM_DISCHARGE_POLICIES 4 +#define DISCHARGE_POLICY_CRITICAL 0 +#define DISCHARGE_POLICY_LOW 1 + +#define PO_THROTTLE_NONE 0 +#define PO_THROTTLE_CONSTANT 1 +#define PO_THROTTLE_DEGRADE 2 +#define PO_THROTTLE_ADAPTIVE 3 +#define PO_THROTTLE_MAXIMUM 4 + + typedef struct _SYSTEM_POWER_POLICY { + DWORD Revision; + POWER_ACTION_POLICY PowerButton; + POWER_ACTION_POLICY SleepButton; + POWER_ACTION_POLICY LidClose; + SYSTEM_POWER_STATE LidOpenWake; + DWORD Reserved; + POWER_ACTION_POLICY Idle; + DWORD IdleTimeout; + BYTE IdleSensitivity; + BYTE DynamicThrottle; + BYTE Spare2[2]; + SYSTEM_POWER_STATE MinSleep; + SYSTEM_POWER_STATE MaxSleep; + SYSTEM_POWER_STATE ReducedLatencySleep; + DWORD WinLogonFlags; + DWORD Spare3; + DWORD DozeS4Timeout; + DWORD BroadcastCapacityResolution; + SYSTEM_POWER_LEVEL DischargePolicy[NUM_DISCHARGE_POLICIES]; + DWORD VideoTimeout; + BOOLEAN VideoDimDisplay; + DWORD VideoReserved[3]; + DWORD SpindownTimeout; + BOOLEAN OptimizeForPower; + BYTE FanThrottleTolerance; + BYTE ForcedThrottle; + BYTE MinThrottle; + POWER_ACTION_POLICY OverThrottled; + } SYSTEM_POWER_POLICY,*PSYSTEM_POWER_POLICY; + + typedef struct _PROCESSOR_POWER_POLICY_INFO { + DWORD TimeCheck; + DWORD DemoteLimit; + DWORD PromoteLimit; + BYTE DemotePercent; + BYTE PromotePercent; + BYTE Spare[2]; + DWORD AllowDemotion:1; + DWORD AllowPromotion:1; + DWORD Reserved:30; + } PROCESSOR_POWER_POLICY_INFO,*PPROCESSOR_POWER_POLICY_INFO; + + typedef struct _PROCESSOR_POWER_POLICY { + DWORD Revision; + BYTE DynamicThrottle; + BYTE Spare[3]; + DWORD DisableCStates:1; + DWORD Reserved:31; + DWORD PolicyCount; + PROCESSOR_POWER_POLICY_INFO Policy[3]; + } PROCESSOR_POWER_POLICY,*PPROCESSOR_POWER_POLICY; + + typedef struct _ADMINISTRATOR_POWER_POLICY { + SYSTEM_POWER_STATE MinSleep; + SYSTEM_POWER_STATE MaxSleep; + DWORD MinVideoTimeout; + DWORD MaxVideoTimeout; + DWORD MinSpindownTimeout; + DWORD MaxSpindownTimeout; + } ADMINISTRATOR_POWER_POLICY,*PADMINISTRATOR_POWER_POLICY; + + typedef struct { + BOOLEAN PowerButtonPresent; + BOOLEAN SleepButtonPresent; + BOOLEAN LidPresent; + BOOLEAN SystemS1; + BOOLEAN SystemS2; + BOOLEAN SystemS3; + BOOLEAN SystemS4; + BOOLEAN SystemS5; + BOOLEAN HiberFilePresent; + BOOLEAN FullWake; + BOOLEAN VideoDimPresent; + BOOLEAN ApmPresent; + BOOLEAN UpsPresent; + BOOLEAN ThermalControl; + BOOLEAN ProcessorThrottle; + BYTE ProcessorMinThrottle; + BYTE ProcessorMaxThrottle; + BYTE spare2[4]; + BOOLEAN DiskSpinDown; + BYTE spare3[8]; + BOOLEAN SystemBatteriesPresent; + BOOLEAN BatteriesAreShortTerm; + BATTERY_REPORTING_SCALE BatteryScale[3]; + SYSTEM_POWER_STATE AcOnLineWake; + SYSTEM_POWER_STATE SoftLidWake; + SYSTEM_POWER_STATE RtcWake; + SYSTEM_POWER_STATE MinDeviceWakeState; + SYSTEM_POWER_STATE DefaultLowLatencyWake; + } SYSTEM_POWER_CAPABILITIES,*PSYSTEM_POWER_CAPABILITIES; + + typedef struct { + BOOLEAN AcOnLine; + BOOLEAN BatteryPresent; + BOOLEAN Charging; + BOOLEAN Discharging; + BOOLEAN Spare1[4]; + DWORD MaxCapacity; + DWORD RemainingCapacity; + DWORD Rate; + DWORD EstimatedTime; + DWORD DefaultAlert1; + DWORD DefaultAlert2; + } SYSTEM_BATTERY_STATE,*PSYSTEM_BATTERY_STATE; + +#include "pshpack4.h" + +#define IMAGE_DOS_SIGNATURE 0x5A4D +#define IMAGE_OS2_SIGNATURE 0x454E +#define IMAGE_OS2_SIGNATURE_LE 0x454C +#define IMAGE_VXD_SIGNATURE 0x454C +#define IMAGE_NT_SIGNATURE 0x00004550 + +#include "pshpack2.h" + + typedef struct _IMAGE_DOS_HEADER { + WORD e_magic; + WORD e_cblp; + WORD e_cp; + WORD e_crlc; + WORD e_cparhdr; + WORD e_minalloc; + WORD e_maxalloc; + WORD e_ss; + WORD e_sp; + WORD e_csum; + WORD e_ip; + WORD e_cs; + WORD e_lfarlc; + WORD e_ovno; + WORD e_res[4]; + WORD e_oemid; + WORD e_oeminfo; + WORD e_res2[10]; + LONG e_lfanew; + } IMAGE_DOS_HEADER,*PIMAGE_DOS_HEADER; + + typedef struct _IMAGE_OS2_HEADER { + WORD ne_magic; + CHAR ne_ver; + CHAR ne_rev; + WORD ne_enttab; + WORD ne_cbenttab; + LONG ne_crc; + WORD ne_flags; + WORD ne_autodata; + WORD ne_heap; + WORD ne_stack; + LONG ne_csip; + LONG ne_sssp; + WORD ne_cseg; + WORD ne_cmod; + WORD ne_cbnrestab; + WORD ne_segtab; + WORD ne_rsrctab; + WORD ne_restab; + WORD ne_modtab; + WORD ne_imptab; + LONG ne_nrestab; + WORD ne_cmovent; + WORD ne_align; + WORD ne_cres; + BYTE ne_exetyp; + BYTE ne_flagsothers; + WORD ne_pretthunks; + WORD ne_psegrefbytes; + WORD ne_swaparea; + WORD ne_expver; + } IMAGE_OS2_HEADER,*PIMAGE_OS2_HEADER; + + typedef struct _IMAGE_VXD_HEADER { + WORD e32_magic; + BYTE e32_border; + BYTE e32_worder; + DWORD e32_level; + WORD e32_cpu; + WORD e32_os; + DWORD e32_ver; + DWORD e32_mflags; + DWORD e32_mpages; + DWORD e32_startobj; + DWORD e32_eip; + DWORD e32_stackobj; + DWORD e32_esp; + DWORD e32_pagesize; + DWORD e32_lastpagesize; + DWORD e32_fixupsize; + DWORD e32_fixupsum; + DWORD e32_ldrsize; + DWORD e32_ldrsum; + DWORD e32_objtab; + DWORD e32_objcnt; + DWORD e32_objmap; + DWORD e32_itermap; + DWORD e32_rsrctab; + DWORD e32_rsrccnt; + DWORD e32_restab; + DWORD e32_enttab; + DWORD e32_dirtab; + DWORD e32_dircnt; + DWORD e32_fpagetab; + DWORD e32_frectab; + DWORD e32_impmod; + DWORD e32_impmodcnt; + DWORD e32_impproc; + DWORD e32_pagesum; + DWORD e32_datapage; + DWORD e32_preload; + DWORD e32_nrestab; + DWORD e32_cbnrestab; + DWORD e32_nressum; + DWORD e32_autodata; + DWORD e32_debuginfo; + DWORD e32_debuglen; + DWORD e32_instpreload; + DWORD e32_instdemand; + DWORD e32_heapsize; + BYTE e32_res3[12]; + DWORD e32_winresoff; + DWORD e32_winreslen; + WORD e32_devid; + WORD e32_ddkver; + } IMAGE_VXD_HEADER,*PIMAGE_VXD_HEADER; + +#include "poppack.h" + + typedef struct _IMAGE_FILE_HEADER { + WORD Machine; + WORD NumberOfSections; + DWORD TimeDateStamp; + DWORD PointerToSymbolTable; + DWORD NumberOfSymbols; + WORD SizeOfOptionalHeader; + WORD Characteristics; + } IMAGE_FILE_HEADER,*PIMAGE_FILE_HEADER; + +#define IMAGE_SIZEOF_FILE_HEADER 20 + +#define IMAGE_FILE_RELOCS_STRIPPED 0x0001 +#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 +#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 +#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 +#define IMAGE_FILE_AGGRESIVE_WS_TRIM 0x0010 +#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020 +#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080 +#define IMAGE_FILE_32BIT_MACHINE 0x0100 +#define IMAGE_FILE_DEBUG_STRIPPED 0x0200 +#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400 +#define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800 +#define IMAGE_FILE_SYSTEM 0x1000 +#define IMAGE_FILE_DLL 0x2000 +#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000 +#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000 + +#define IMAGE_FILE_MACHINE_UNKNOWN 0 +#define IMAGE_FILE_MACHINE_I386 0x014c +#define IMAGE_FILE_MACHINE_R3000 0x0162 +#define IMAGE_FILE_MACHINE_R4000 0x0166 +#define IMAGE_FILE_MACHINE_R10000 0x0168 +#define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169 +#define IMAGE_FILE_MACHINE_ALPHA 0x0184 +#define IMAGE_FILE_MACHINE_SH3 0x01a2 +#define IMAGE_FILE_MACHINE_SH3DSP 0x01a3 +#define IMAGE_FILE_MACHINE_SH3E 0x01a4 +#define IMAGE_FILE_MACHINE_SH4 0x01a6 +#define IMAGE_FILE_MACHINE_SH5 0x01a8 +#define IMAGE_FILE_MACHINE_ARM 0x01c0 +#define IMAGE_FILE_MACHINE_THUMB 0x01c2 +#define IMAGE_FILE_MACHINE_AM33 0x01d3 +#define IMAGE_FILE_MACHINE_POWERPC 0x01F0 +#define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 +#define IMAGE_FILE_MACHINE_IA64 0x0200 +#define IMAGE_FILE_MACHINE_MIPS16 0x0266 +#define IMAGE_FILE_MACHINE_ALPHA64 0x0284 +#define IMAGE_FILE_MACHINE_MIPSFPU 0x0366 +#define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466 +#define IMAGE_FILE_MACHINE_AXP64 IMAGE_FILE_MACHINE_ALPHA64 +#define IMAGE_FILE_MACHINE_TRICORE 0x0520 +#define IMAGE_FILE_MACHINE_CEF 0x0CEF +#define IMAGE_FILE_MACHINE_EBC 0x0EBC +#define IMAGE_FILE_MACHINE_AMD64 0x8664 +#define IMAGE_FILE_MACHINE_M32R 0x9041 +#define IMAGE_FILE_MACHINE_CEE 0xC0EE + + typedef struct _IMAGE_DATA_DIRECTORY { + DWORD VirtualAddress; + DWORD Size; + } IMAGE_DATA_DIRECTORY,*PIMAGE_DATA_DIRECTORY; + +#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16 + + typedef struct _IMAGE_OPTIONAL_HEADER { + + WORD Magic; + BYTE MajorLinkerVersion; + BYTE MinorLinkerVersion; + DWORD SizeOfCode; + DWORD SizeOfInitializedData; + DWORD SizeOfUninitializedData; + DWORD AddressOfEntryPoint; + DWORD BaseOfCode; + DWORD BaseOfData; + DWORD ImageBase; + DWORD SectionAlignment; + DWORD FileAlignment; + WORD MajorOperatingSystemVersion; + WORD MinorOperatingSystemVersion; + WORD MajorImageVersion; + WORD MinorImageVersion; + WORD MajorSubsystemVersion; + WORD MinorSubsystemVersion; + DWORD Win32VersionValue; + DWORD SizeOfImage; + DWORD SizeOfHeaders; + DWORD CheckSum; + WORD Subsystem; + WORD DllCharacteristics; + DWORD SizeOfStackReserve; + DWORD SizeOfStackCommit; + DWORD SizeOfHeapReserve; + DWORD SizeOfHeapCommit; + DWORD LoaderFlags; + DWORD NumberOfRvaAndSizes; + IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; + } IMAGE_OPTIONAL_HEADER32,*PIMAGE_OPTIONAL_HEADER32; + + typedef struct _IMAGE_ROM_OPTIONAL_HEADER { + WORD Magic; + BYTE MajorLinkerVersion; + BYTE MinorLinkerVersion; + DWORD SizeOfCode; + DWORD SizeOfInitializedData; + DWORD SizeOfUninitializedData; + DWORD AddressOfEntryPoint; + DWORD BaseOfCode; + DWORD BaseOfData; + DWORD BaseOfBss; + DWORD GprMask; + DWORD CprMask[4]; + DWORD GpValue; + } IMAGE_ROM_OPTIONAL_HEADER,*PIMAGE_ROM_OPTIONAL_HEADER; + + typedef struct _IMAGE_OPTIONAL_HEADER64 { + WORD Magic; + BYTE MajorLinkerVersion; + BYTE MinorLinkerVersion; + DWORD SizeOfCode; + DWORD SizeOfInitializedData; + DWORD SizeOfUninitializedData; + DWORD AddressOfEntryPoint; + DWORD BaseOfCode; + ULONGLONG ImageBase; + DWORD SectionAlignment; + DWORD FileAlignment; + WORD MajorOperatingSystemVersion; + WORD MinorOperatingSystemVersion; + WORD MajorImageVersion; + WORD MinorImageVersion; + WORD MajorSubsystemVersion; + WORD MinorSubsystemVersion; + DWORD Win32VersionValue; + DWORD SizeOfImage; + DWORD SizeOfHeaders; + DWORD CheckSum; + WORD Subsystem; + WORD DllCharacteristics; + ULONGLONG SizeOfStackReserve; + ULONGLONG SizeOfStackCommit; + ULONGLONG SizeOfHeapReserve; + ULONGLONG SizeOfHeapCommit; + DWORD LoaderFlags; + DWORD NumberOfRvaAndSizes; + IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; + } IMAGE_OPTIONAL_HEADER64,*PIMAGE_OPTIONAL_HEADER64; + +#define IMAGE_SIZEOF_ROM_OPTIONAL_HEADER 56 +#define IMAGE_SIZEOF_STD_OPTIONAL_HEADER 28 +#define IMAGE_SIZEOF_NT_OPTIONAL32_HEADER 224 +#define IMAGE_SIZEOF_NT_OPTIONAL64_HEADER 240 + +#define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b +#define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b +#define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107 + +#ifdef _WIN64 + typedef IMAGE_OPTIONAL_HEADER64 IMAGE_OPTIONAL_HEADER; + typedef PIMAGE_OPTIONAL_HEADER64 PIMAGE_OPTIONAL_HEADER; +#define IMAGE_SIZEOF_NT_OPTIONAL_HEADER IMAGE_SIZEOF_NT_OPTIONAL64_HEADER +#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC +#else + typedef IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER; + typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER; +#define IMAGE_SIZEOF_NT_OPTIONAL_HEADER IMAGE_SIZEOF_NT_OPTIONAL32_HEADER +#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC +#endif + + typedef struct _IMAGE_NT_HEADERS64 { + DWORD Signature; + IMAGE_FILE_HEADER FileHeader; + IMAGE_OPTIONAL_HEADER64 OptionalHeader; + } IMAGE_NT_HEADERS64,*PIMAGE_NT_HEADERS64; + + typedef struct _IMAGE_NT_HEADERS { + DWORD Signature; + IMAGE_FILE_HEADER FileHeader; + IMAGE_OPTIONAL_HEADER32 OptionalHeader; + } IMAGE_NT_HEADERS32,*PIMAGE_NT_HEADERS32; + + typedef struct _IMAGE_ROM_HEADERS { + IMAGE_FILE_HEADER FileHeader; + IMAGE_ROM_OPTIONAL_HEADER OptionalHeader; + } IMAGE_ROM_HEADERS,*PIMAGE_ROM_HEADERS; + +#ifdef _WIN64 + typedef IMAGE_NT_HEADERS64 IMAGE_NT_HEADERS; + typedef PIMAGE_NT_HEADERS64 PIMAGE_NT_HEADERS; +#else + typedef IMAGE_NT_HEADERS32 IMAGE_NT_HEADERS; + typedef PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS; +#endif + +#define IMAGE_FIRST_SECTION(ntheader) ((PIMAGE_SECTION_HEADER) ((ULONG_PTR)ntheader + FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader) + ((PIMAGE_NT_HEADERS)(ntheader))->FileHeader.SizeOfOptionalHeader)) + +#define IMAGE_SUBSYSTEM_UNKNOWN 0 +#define IMAGE_SUBSYSTEM_NATIVE 1 +#define IMAGE_SUBSYSTEM_WINDOWS_GUI 2 +#define IMAGE_SUBSYSTEM_WINDOWS_CUI 3 +#define IMAGE_SUBSYSTEM_OS2_CUI 5 +#define IMAGE_SUBSYSTEM_POSIX_CUI 7 +#define IMAGE_SUBSYSTEM_NATIVE_WINDOWS 8 +#define IMAGE_SUBSYSTEM_WINDOWS_CE_GUI 9 +#define IMAGE_SUBSYSTEM_EFI_APPLICATION 10 +#define IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11 +#define IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER 12 +#define IMAGE_SUBSYSTEM_EFI_ROM 13 +#define IMAGE_SUBSYSTEM_XBOX 14 + +#define IMAGE_DLLCHARACTERISTICS_NO_ISOLATION 0x0200 +#define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x0400 +#define IMAGE_DLLCHARACTERISTICS_NO_BIND 0x0800 +#define IMAGE_DLLCHARACTERISTICS_WDM_DRIVER 0x2000 +#define IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE 0x8000 + +#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 +#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 +#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 +#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 +#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 +#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 +#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 + +#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 7 +#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 +#define IMAGE_DIRECTORY_ENTRY_TLS 9 +#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 +#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 +#define IMAGE_DIRECTORY_ENTRY_IAT 12 +#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 +#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 + + typedef struct ANON_OBJECT_HEADER { + WORD Sig1; + WORD Sig2; + WORD Version; + WORD Machine; + DWORD TimeDateStamp; + CLSID ClassID; + DWORD SizeOfData; + } ANON_OBJECT_HEADER; + +#define IMAGE_SIZEOF_SHORT_NAME 8 + + typedef struct _IMAGE_SECTION_HEADER { + BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; + union { + DWORD PhysicalAddress; + DWORD VirtualSize; + } Misc; + DWORD VirtualAddress; + DWORD SizeOfRawData; + DWORD PointerToRawData; + DWORD PointerToRelocations; + DWORD PointerToLinenumbers; + WORD NumberOfRelocations; + WORD NumberOfLinenumbers; + DWORD Characteristics; + } IMAGE_SECTION_HEADER,*PIMAGE_SECTION_HEADER; + +#define IMAGE_SIZEOF_SECTION_HEADER 40 + +#define IMAGE_SCN_TYPE_NO_PAD 0x00000008 + +#define IMAGE_SCN_CNT_CODE 0x00000020 +#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 +#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 +#define IMAGE_SCN_LNK_OTHER 0x00000100 +#define IMAGE_SCN_LNK_INFO 0x00000200 +#define IMAGE_SCN_LNK_REMOVE 0x00000800 +#define IMAGE_SCN_LNK_COMDAT 0x00001000 +#define IMAGE_SCN_NO_DEFER_SPEC_EXC 0x00004000 +#define IMAGE_SCN_GPREL 0x00008000 +#define IMAGE_SCN_MEM_FARDATA 0x00008000 +#define IMAGE_SCN_MEM_PURGEABLE 0x00020000 +#define IMAGE_SCN_MEM_16BIT 0x00020000 +#define IMAGE_SCN_MEM_LOCKED 0x00040000 +#define IMAGE_SCN_MEM_PRELOAD 0x00080000 + +#define IMAGE_SCN_ALIGN_1BYTES 0x00100000 +#define IMAGE_SCN_ALIGN_2BYTES 0x00200000 +#define IMAGE_SCN_ALIGN_4BYTES 0x00300000 +#define IMAGE_SCN_ALIGN_8BYTES 0x00400000 +#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 +#define IMAGE_SCN_ALIGN_32BYTES 0x00600000 +#define IMAGE_SCN_ALIGN_64BYTES 0x00700000 +#define IMAGE_SCN_ALIGN_128BYTES 0x00800000 +#define IMAGE_SCN_ALIGN_256BYTES 0x00900000 +#define IMAGE_SCN_ALIGN_512BYTES 0x00A00000 +#define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000 +#define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000 +#define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000 +#define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000 + +#define IMAGE_SCN_ALIGN_MASK 0x00F00000 + +#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 +#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 +#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 +#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 +#define IMAGE_SCN_MEM_SHARED 0x10000000 +#define IMAGE_SCN_MEM_EXECUTE 0x20000000 +#define IMAGE_SCN_MEM_READ 0x40000000 +#define IMAGE_SCN_MEM_WRITE 0x80000000 + +#define IMAGE_SCN_SCALE_INDEX 0x00000001 + +#include "pshpack2.h" + + typedef struct _IMAGE_SYMBOL { + union { + BYTE ShortName[8]; + struct { + DWORD Short; + DWORD Long; + } Name; + DWORD LongName[2]; + } N; + DWORD Value; + SHORT SectionNumber; + WORD Type; + BYTE StorageClass; + BYTE NumberOfAuxSymbols; + } IMAGE_SYMBOL; + typedef IMAGE_SYMBOL UNALIGNED *PIMAGE_SYMBOL; + +#define IMAGE_SIZEOF_SYMBOL 18 + +#define IMAGE_SYM_UNDEFINED (SHORT)0 +#define IMAGE_SYM_ABSOLUTE (SHORT)-1 +#define IMAGE_SYM_DEBUG (SHORT)-2 +#define IMAGE_SYM_SECTION_MAX 0xFEFF + +#define IMAGE_SYM_TYPE_NULL 0x0000 +#define IMAGE_SYM_TYPE_VOID 0x0001 +#define IMAGE_SYM_TYPE_CHAR 0x0002 +#define IMAGE_SYM_TYPE_SHORT 0x0003 +#define IMAGE_SYM_TYPE_INT 0x0004 +#define IMAGE_SYM_TYPE_LONG 0x0005 +#define IMAGE_SYM_TYPE_FLOAT 0x0006 +#define IMAGE_SYM_TYPE_DOUBLE 0x0007 +#define IMAGE_SYM_TYPE_STRUCT 0x0008 +#define IMAGE_SYM_TYPE_UNION 0x0009 +#define IMAGE_SYM_TYPE_ENUM 0x000A +#define IMAGE_SYM_TYPE_MOE 0x000B +#define IMAGE_SYM_TYPE_BYTE 0x000C +#define IMAGE_SYM_TYPE_WORD 0x000D +#define IMAGE_SYM_TYPE_UINT 0x000E +#define IMAGE_SYM_TYPE_DWORD 0x000F +#define IMAGE_SYM_TYPE_PCODE 0x8000 + +#define IMAGE_SYM_DTYPE_NULL 0 +#define IMAGE_SYM_DTYPE_POINTER 1 +#define IMAGE_SYM_DTYPE_FUNCTION 2 +#define IMAGE_SYM_DTYPE_ARRAY 3 + +#define IMAGE_SYM_CLASS_END_OF_FUNCTION (BYTE)-1 +#define IMAGE_SYM_CLASS_NULL 0x0000 +#define IMAGE_SYM_CLASS_AUTOMATIC 0x0001 +#define IMAGE_SYM_CLASS_EXTERNAL 0x0002 +#define IMAGE_SYM_CLASS_STATIC 0x0003 +#define IMAGE_SYM_CLASS_REGISTER 0x0004 +#define IMAGE_SYM_CLASS_EXTERNAL_DEF 0x0005 +#define IMAGE_SYM_CLASS_LABEL 0x0006 +#define IMAGE_SYM_CLASS_UNDEFINED_LABEL 0x0007 +#define IMAGE_SYM_CLASS_MEMBER_OF_STRUCT 0x0008 +#define IMAGE_SYM_CLASS_ARGUMENT 0x0009 +#define IMAGE_SYM_CLASS_STRUCT_TAG 0x000A +#define IMAGE_SYM_CLASS_MEMBER_OF_UNION 0x000B +#define IMAGE_SYM_CLASS_UNION_TAG 0x000C +#define IMAGE_SYM_CLASS_TYPE_DEFINITION 0x000D +#define IMAGE_SYM_CLASS_UNDEFINED_STATIC 0x000E +#define IMAGE_SYM_CLASS_ENUM_TAG 0x000F +#define IMAGE_SYM_CLASS_MEMBER_OF_ENUM 0x0010 +#define IMAGE_SYM_CLASS_REGISTER_PARAM 0x0011 +#define IMAGE_SYM_CLASS_BIT_FIELD 0x0012 +#define IMAGE_SYM_CLASS_FAR_EXTERNAL 0x0044 +#define IMAGE_SYM_CLASS_BLOCK 0x0064 +#define IMAGE_SYM_CLASS_FUNCTION 0x0065 +#define IMAGE_SYM_CLASS_END_OF_STRUCT 0x0066 +#define IMAGE_SYM_CLASS_FILE 0x0067 +#define IMAGE_SYM_CLASS_SECTION 0x0068 +#define IMAGE_SYM_CLASS_WEAK_EXTERNAL 0x0069 +#define IMAGE_SYM_CLASS_CLR_TOKEN 0x006B + +#define N_BTMASK 0x000F +#define N_TMASK 0x0030 +#define N_TMASK1 0x00C0 +#define N_TMASK2 0x00F0 +#define N_BTSHFT 4 +#define N_TSHIFT 2 + +#define BTYPE(x) ((x) & N_BTMASK) + +#ifndef ISPTR +#define ISPTR(x) (((x) & N_TMASK)==(IMAGE_SYM_DTYPE_POINTER << N_BTSHFT)) +#endif + +#ifndef ISFCN +#define ISFCN(x) (((x) & N_TMASK)==(IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT)) +#endif + +#ifndef ISARY +#define ISARY(x) (((x) & N_TMASK)==(IMAGE_SYM_DTYPE_ARRAY << N_BTSHFT)) +#endif + +#ifndef ISTAG +#define ISTAG(x) ((x)==IMAGE_SYM_CLASS_STRUCT_TAG || (x)==IMAGE_SYM_CLASS_UNION_TAG || (x)==IMAGE_SYM_CLASS_ENUM_TAG) +#endif + +#ifndef INCREF +#define INCREF(x) ((((x)&~N_BTMASK)<>N_TSHIFT)&~N_BTMASK)|((x)&N_BTMASK)) +#endif + + typedef union _IMAGE_AUX_SYMBOL { + struct { + DWORD TagIndex; + union { + struct { + WORD Linenumber; + WORD Size; + } LnSz; + DWORD TotalSize; + } Misc; + union { + struct { + DWORD PointerToLinenumber; + DWORD PointerToNextFunction; + } Function; + struct { + WORD Dimension[4]; + } Array; + } FcnAry; + WORD TvIndex; + } Sym; + struct { + BYTE Name[IMAGE_SIZEOF_SYMBOL]; + } File; + struct { + DWORD Length; + WORD NumberOfRelocations; + WORD NumberOfLinenumbers; + DWORD CheckSum; + SHORT Number; + BYTE Selection; + } Section; + } IMAGE_AUX_SYMBOL; + typedef IMAGE_AUX_SYMBOL UNALIGNED *PIMAGE_AUX_SYMBOL; + +#define IMAGE_SIZEOF_AUX_SYMBOL 18 + + typedef enum IMAGE_AUX_SYMBOL_TYPE { + IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF = 1 + } IMAGE_AUX_SYMBOL_TYPE; + +#include + + typedef struct IMAGE_AUX_SYMBOL_TOKEN_DEF { + BYTE bAuxType; + BYTE bReserved; + DWORD SymbolTableIndex; + BYTE rgbReserved[12]; + } IMAGE_AUX_SYMBOL_TOKEN_DEF; + + typedef IMAGE_AUX_SYMBOL_TOKEN_DEF UNALIGNED *PIMAGE_AUX_SYMBOL_TOKEN_DEF; + +#include + +#define IMAGE_COMDAT_SELECT_NODUPLICATES 1 +#define IMAGE_COMDAT_SELECT_ANY 2 +#define IMAGE_COMDAT_SELECT_SAME_SIZE 3 +#define IMAGE_COMDAT_SELECT_EXACT_MATCH 4 +#define IMAGE_COMDAT_SELECT_ASSOCIATIVE 5 +#define IMAGE_COMDAT_SELECT_LARGEST 6 +#define IMAGE_COMDAT_SELECT_NEWEST 7 + +#define IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY 1 +#define IMAGE_WEAK_EXTERN_SEARCH_LIBRARY 2 +#define IMAGE_WEAK_EXTERN_SEARCH_ALIAS 3 + + typedef struct _IMAGE_RELOCATION { + union { + DWORD VirtualAddress; + DWORD RelocCount; + }; + DWORD SymbolTableIndex; + WORD Type; + } IMAGE_RELOCATION; + typedef IMAGE_RELOCATION UNALIGNED *PIMAGE_RELOCATION; + +#define IMAGE_SIZEOF_RELOCATION 10 + +#define IMAGE_REL_I386_ABSOLUTE 0x0000 +#define IMAGE_REL_I386_DIR16 0x0001 +#define IMAGE_REL_I386_REL16 0x0002 +#define IMAGE_REL_I386_DIR32 0x0006 +#define IMAGE_REL_I386_DIR32NB 0x0007 +#define IMAGE_REL_I386_SEG12 0x0009 +#define IMAGE_REL_I386_SECTION 0x000A +#define IMAGE_REL_I386_SECREL 0x000B +#define IMAGE_REL_I386_TOKEN 0x000C +#define IMAGE_REL_I386_SECREL7 0x000D +#define IMAGE_REL_I386_REL32 0x0014 + +#define IMAGE_REL_MIPS_ABSOLUTE 0x0000 +#define IMAGE_REL_MIPS_REFHALF 0x0001 +#define IMAGE_REL_MIPS_REFWORD 0x0002 +#define IMAGE_REL_MIPS_JMPADDR 0x0003 +#define IMAGE_REL_MIPS_REFHI 0x0004 +#define IMAGE_REL_MIPS_REFLO 0x0005 +#define IMAGE_REL_MIPS_GPREL 0x0006 +#define IMAGE_REL_MIPS_LITERAL 0x0007 +#define IMAGE_REL_MIPS_SECTION 0x000A +#define IMAGE_REL_MIPS_SECREL 0x000B +#define IMAGE_REL_MIPS_SECRELLO 0x000C +#define IMAGE_REL_MIPS_SECRELHI 0x000D +#define IMAGE_REL_MIPS_TOKEN 0x000E +#define IMAGE_REL_MIPS_JMPADDR16 0x0010 +#define IMAGE_REL_MIPS_REFWORDNB 0x0022 +#define IMAGE_REL_MIPS_PAIR 0x0025 + +#define IMAGE_REL_ALPHA_ABSOLUTE 0x0000 +#define IMAGE_REL_ALPHA_REFLONG 0x0001 +#define IMAGE_REL_ALPHA_REFQUAD 0x0002 +#define IMAGE_REL_ALPHA_GPREL32 0x0003 +#define IMAGE_REL_ALPHA_LITERAL 0x0004 +#define IMAGE_REL_ALPHA_LITUSE 0x0005 +#define IMAGE_REL_ALPHA_GPDISP 0x0006 +#define IMAGE_REL_ALPHA_BRADDR 0x0007 +#define IMAGE_REL_ALPHA_HINT 0x0008 +#define IMAGE_REL_ALPHA_INLINE_REFLONG 0x0009 +#define IMAGE_REL_ALPHA_REFHI 0x000A +#define IMAGE_REL_ALPHA_REFLO 0x000B +#define IMAGE_REL_ALPHA_PAIR 0x000C +#define IMAGE_REL_ALPHA_MATCH 0x000D +#define IMAGE_REL_ALPHA_SECTION 0x000E +#define IMAGE_REL_ALPHA_SECREL 0x000F +#define IMAGE_REL_ALPHA_REFLONGNB 0x0010 +#define IMAGE_REL_ALPHA_SECRELLO 0x0011 +#define IMAGE_REL_ALPHA_SECRELHI 0x0012 +#define IMAGE_REL_ALPHA_REFQ3 0x0013 +#define IMAGE_REL_ALPHA_REFQ2 0x0014 +#define IMAGE_REL_ALPHA_REFQ1 0x0015 +#define IMAGE_REL_ALPHA_GPRELLO 0x0016 +#define IMAGE_REL_ALPHA_GPRELHI 0x0017 + +#define IMAGE_REL_PPC_ABSOLUTE 0x0000 +#define IMAGE_REL_PPC_ADDR64 0x0001 +#define IMAGE_REL_PPC_ADDR32 0x0002 +#define IMAGE_REL_PPC_ADDR24 0x0003 +#define IMAGE_REL_PPC_ADDR16 0x0004 +#define IMAGE_REL_PPC_ADDR14 0x0005 +#define IMAGE_REL_PPC_REL24 0x0006 +#define IMAGE_REL_PPC_REL14 0x0007 +#define IMAGE_REL_PPC_TOCREL16 0x0008 +#define IMAGE_REL_PPC_TOCREL14 0x0009 +#define IMAGE_REL_PPC_ADDR32NB 0x000A +#define IMAGE_REL_PPC_SECREL 0x000B +#define IMAGE_REL_PPC_SECTION 0x000C +#define IMAGE_REL_PPC_IFGLUE 0x000D +#define IMAGE_REL_PPC_IMGLUE 0x000E +#define IMAGE_REL_PPC_SECREL16 0x000F +#define IMAGE_REL_PPC_REFHI 0x0010 +#define IMAGE_REL_PPC_REFLO 0x0011 +#define IMAGE_REL_PPC_PAIR 0x0012 +#define IMAGE_REL_PPC_SECRELLO 0x0013 +#define IMAGE_REL_PPC_SECRELHI 0x0014 +#define IMAGE_REL_PPC_GPREL 0x0015 +#define IMAGE_REL_PPC_TOKEN 0x0016 +#define IMAGE_REL_PPC_TYPEMASK 0x00FF +#define IMAGE_REL_PPC_NEG 0x0100 +#define IMAGE_REL_PPC_BRTAKEN 0x0200 +#define IMAGE_REL_PPC_BRNTAKEN 0x0400 +#define IMAGE_REL_PPC_TOCDEFN 0x0800 + +#define IMAGE_REL_SH3_ABSOLUTE 0x0000 +#define IMAGE_REL_SH3_DIRECT16 0x0001 +#define IMAGE_REL_SH3_DIRECT32 0x0002 +#define IMAGE_REL_SH3_DIRECT8 0x0003 +#define IMAGE_REL_SH3_DIRECT8_WORD 0x0004 +#define IMAGE_REL_SH3_DIRECT8_LONG 0x0005 +#define IMAGE_REL_SH3_DIRECT4 0x0006 +#define IMAGE_REL_SH3_DIRECT4_WORD 0x0007 +#define IMAGE_REL_SH3_DIRECT4_LONG 0x0008 +#define IMAGE_REL_SH3_PCREL8_WORD 0x0009 +#define IMAGE_REL_SH3_PCREL8_LONG 0x000A +#define IMAGE_REL_SH3_PCREL12_WORD 0x000B +#define IMAGE_REL_SH3_STARTOF_SECTION 0x000C +#define IMAGE_REL_SH3_SIZEOF_SECTION 0x000D +#define IMAGE_REL_SH3_SECTION 0x000E +#define IMAGE_REL_SH3_SECREL 0x000F +#define IMAGE_REL_SH3_DIRECT32_NB 0x0010 +#define IMAGE_REL_SH3_GPREL4_LONG 0x0011 +#define IMAGE_REL_SH3_TOKEN 0x0012 + +#define IMAGE_REL_SHM_PCRELPT 0x0013 +#define IMAGE_REL_SHM_REFLO 0x0014 +#define IMAGE_REL_SHM_REFHALF 0x0015 +#define IMAGE_REL_SHM_RELLO 0x0016 +#define IMAGE_REL_SHM_RELHALF 0x0017 +#define IMAGE_REL_SHM_PAIR 0x0018 + +#define IMAGE_REL_SH_NOMODE 0x8000 + +#define IMAGE_REL_ARM_ABSOLUTE 0x0000 +#define IMAGE_REL_ARM_ADDR32 0x0001 +#define IMAGE_REL_ARM_ADDR32NB 0x0002 +#define IMAGE_REL_ARM_BRANCH24 0x0003 +#define IMAGE_REL_ARM_BRANCH11 0x0004 +#define IMAGE_REL_ARM_TOKEN 0x0005 +#define IMAGE_REL_ARM_GPREL12 0x0006 +#define IMAGE_REL_ARM_GPREL7 0x0007 +#define IMAGE_REL_ARM_BLX24 0x0008 +#define IMAGE_REL_ARM_BLX11 0x0009 +#define IMAGE_REL_ARM_SECTION 0x000E +#define IMAGE_REL_ARM_SECREL 0x000F + +#define IMAGE_REL_AM_ABSOLUTE 0x0000 +#define IMAGE_REL_AM_ADDR32 0x0001 +#define IMAGE_REL_AM_ADDR32NB 0x0002 +#define IMAGE_REL_AM_CALL32 0x0003 +#define IMAGE_REL_AM_FUNCINFO 0x0004 +#define IMAGE_REL_AM_REL32_1 0x0005 +#define IMAGE_REL_AM_REL32_2 0x0006 +#define IMAGE_REL_AM_SECREL 0x0007 +#define IMAGE_REL_AM_SECTION 0x0008 +#define IMAGE_REL_AM_TOKEN 0x0009 + +#define IMAGE_REL_AMD64_ABSOLUTE 0x0000 +#define IMAGE_REL_AMD64_ADDR64 0x0001 +#define IMAGE_REL_AMD64_ADDR32 0x0002 +#define IMAGE_REL_AMD64_ADDR32NB 0x0003 +#define IMAGE_REL_AMD64_REL32 0x0004 +#define IMAGE_REL_AMD64_REL32_1 0x0005 +#define IMAGE_REL_AMD64_REL32_2 0x0006 +#define IMAGE_REL_AMD64_REL32_3 0x0007 +#define IMAGE_REL_AMD64_REL32_4 0x0008 +#define IMAGE_REL_AMD64_REL32_5 0x0009 +#define IMAGE_REL_AMD64_SECTION 0x000A +#define IMAGE_REL_AMD64_SECREL 0x000B +#define IMAGE_REL_AMD64_SECREL7 0x000C +#define IMAGE_REL_AMD64_TOKEN 0x000D +#define IMAGE_REL_AMD64_SREL32 0x000E +#define IMAGE_REL_AMD64_PAIR 0x000F +#define IMAGE_REL_AMD64_SSPAN32 0x0010 + +#define IMAGE_REL_IA64_ABSOLUTE 0x0000 +#define IMAGE_REL_IA64_IMM14 0x0001 +#define IMAGE_REL_IA64_IMM22 0x0002 +#define IMAGE_REL_IA64_IMM64 0x0003 +#define IMAGE_REL_IA64_DIR32 0x0004 +#define IMAGE_REL_IA64_DIR64 0x0005 +#define IMAGE_REL_IA64_PCREL21B 0x0006 +#define IMAGE_REL_IA64_PCREL21M 0x0007 +#define IMAGE_REL_IA64_PCREL21F 0x0008 +#define IMAGE_REL_IA64_GPREL22 0x0009 +#define IMAGE_REL_IA64_LTOFF22 0x000A +#define IMAGE_REL_IA64_SECTION 0x000B +#define IMAGE_REL_IA64_SECREL22 0x000C +#define IMAGE_REL_IA64_SECREL64I 0x000D +#define IMAGE_REL_IA64_SECREL32 0x000E + +#define IMAGE_REL_IA64_DIR32NB 0x0010 +#define IMAGE_REL_IA64_SREL14 0x0011 +#define IMAGE_REL_IA64_SREL22 0x0012 +#define IMAGE_REL_IA64_SREL32 0x0013 +#define IMAGE_REL_IA64_UREL32 0x0014 +#define IMAGE_REL_IA64_PCREL60X 0x0015 +#define IMAGE_REL_IA64_PCREL60B 0x0016 +#define IMAGE_REL_IA64_PCREL60F 0x0017 +#define IMAGE_REL_IA64_PCREL60I 0x0018 +#define IMAGE_REL_IA64_PCREL60M 0x0019 +#define IMAGE_REL_IA64_IMMGPREL64 0x001A +#define IMAGE_REL_IA64_TOKEN 0x001B +#define IMAGE_REL_IA64_GPREL32 0x001C +#define IMAGE_REL_IA64_ADDEND 0x001F + +#define IMAGE_REL_CEF_ABSOLUTE 0x0000 +#define IMAGE_REL_CEF_ADDR32 0x0001 +#define IMAGE_REL_CEF_ADDR64 0x0002 +#define IMAGE_REL_CEF_ADDR32NB 0x0003 +#define IMAGE_REL_CEF_SECTION 0x0004 +#define IMAGE_REL_CEF_SECREL 0x0005 +#define IMAGE_REL_CEF_TOKEN 0x0006 + +#define IMAGE_REL_CEE_ABSOLUTE 0x0000 +#define IMAGE_REL_CEE_ADDR32 0x0001 +#define IMAGE_REL_CEE_ADDR64 0x0002 +#define IMAGE_REL_CEE_ADDR32NB 0x0003 +#define IMAGE_REL_CEE_SECTION 0x0004 +#define IMAGE_REL_CEE_SECREL 0x0005 +#define IMAGE_REL_CEE_TOKEN 0x0006 + +#define IMAGE_REL_M32R_ABSOLUTE 0x0000 +#define IMAGE_REL_M32R_ADDR32 0x0001 +#define IMAGE_REL_M32R_ADDR32NB 0x0002 +#define IMAGE_REL_M32R_ADDR24 0x0003 +#define IMAGE_REL_M32R_GPREL16 0x0004 +#define IMAGE_REL_M32R_PCREL24 0x0005 +#define IMAGE_REL_M32R_PCREL16 0x0006 +#define IMAGE_REL_M32R_PCREL8 0x0007 +#define IMAGE_REL_M32R_REFHALF 0x0008 +#define IMAGE_REL_M32R_REFHI 0x0009 +#define IMAGE_REL_M32R_REFLO 0x000A +#define IMAGE_REL_M32R_PAIR 0x000B +#define IMAGE_REL_M32R_SECTION 0x000C +#define IMAGE_REL_M32R_SECREL32 0x000D +#define IMAGE_REL_M32R_TOKEN 0x000E + +#define EXT_IMM64(Value,Address,Size,InstPos,ValPos) Value |= (((ULONGLONG)((*(Address) >> InstPos) & (((ULONGLONG)1 << Size) - 1))) << ValPos) +#define INS_IMM64(Value,Address,Size,InstPos,ValPos) *(PDWORD)Address = (*(PDWORD)Address & ~(((1 << Size) - 1) << InstPos)) | ((DWORD)((((ULONGLONG)Value >> ValPos) & (((ULONGLONG)1 << Size) - 1))) << InstPos) + +#define EMARCH_ENC_I17_IMM7B_INST_WORD_X 3 +#define EMARCH_ENC_I17_IMM7B_SIZE_X 7 +#define EMARCH_ENC_I17_IMM7B_INST_WORD_POS_X 4 +#define EMARCH_ENC_I17_IMM7B_VAL_POS_X 0 + +#define EMARCH_ENC_I17_IMM9D_INST_WORD_X 3 +#define EMARCH_ENC_I17_IMM9D_SIZE_X 9 +#define EMARCH_ENC_I17_IMM9D_INST_WORD_POS_X 18 +#define EMARCH_ENC_I17_IMM9D_VAL_POS_X 7 + +#define EMARCH_ENC_I17_IMM5C_INST_WORD_X 3 +#define EMARCH_ENC_I17_IMM5C_SIZE_X 5 +#define EMARCH_ENC_I17_IMM5C_INST_WORD_POS_X 13 +#define EMARCH_ENC_I17_IMM5C_VAL_POS_X 16 + +#define EMARCH_ENC_I17_IC_INST_WORD_X 3 +#define EMARCH_ENC_I17_IC_SIZE_X 1 +#define EMARCH_ENC_I17_IC_INST_WORD_POS_X 12 +#define EMARCH_ENC_I17_IC_VAL_POS_X 21 + +#define EMARCH_ENC_I17_IMM41a_INST_WORD_X 1 +#define EMARCH_ENC_I17_IMM41a_SIZE_X 10 +#define EMARCH_ENC_I17_IMM41a_INST_WORD_POS_X 14 +#define EMARCH_ENC_I17_IMM41a_VAL_POS_X 22 + +#define EMARCH_ENC_I17_IMM41b_INST_WORD_X 1 +#define EMARCH_ENC_I17_IMM41b_SIZE_X 8 +#define EMARCH_ENC_I17_IMM41b_INST_WORD_POS_X 24 +#define EMARCH_ENC_I17_IMM41b_VAL_POS_X 32 + +#define EMARCH_ENC_I17_IMM41c_INST_WORD_X 2 +#define EMARCH_ENC_I17_IMM41c_SIZE_X 23 +#define EMARCH_ENC_I17_IMM41c_INST_WORD_POS_X 0 +#define EMARCH_ENC_I17_IMM41c_VAL_POS_X 40 + +#define EMARCH_ENC_I17_SIGN_INST_WORD_X 3 +#define EMARCH_ENC_I17_SIGN_SIZE_X 1 +#define EMARCH_ENC_I17_SIGN_INST_WORD_POS_X 27 +#define EMARCH_ENC_I17_SIGN_VAL_POS_X 63 + +#define X3_OPCODE_INST_WORD_X 3 +#define X3_OPCODE_SIZE_X 4 +#define X3_OPCODE_INST_WORD_POS_X 28 +#define X3_OPCODE_SIGN_VAL_POS_X 0 + +#define X3_I_INST_WORD_X 3 +#define X3_I_SIZE_X 1 +#define X3_I_INST_WORD_POS_X 27 +#define X3_I_SIGN_VAL_POS_X 59 + +#define X3_D_WH_INST_WORD_X 3 +#define X3_D_WH_SIZE_X 3 +#define X3_D_WH_INST_WORD_POS_X 24 +#define X3_D_WH_SIGN_VAL_POS_X 0 + +#define X3_IMM20_INST_WORD_X 3 +#define X3_IMM20_SIZE_X 20 +#define X3_IMM20_INST_WORD_POS_X 4 +#define X3_IMM20_SIGN_VAL_POS_X 0 + +#define X3_IMM39_1_INST_WORD_X 2 +#define X3_IMM39_1_SIZE_X 23 +#define X3_IMM39_1_INST_WORD_POS_X 0 +#define X3_IMM39_1_SIGN_VAL_POS_X 36 + +#define X3_IMM39_2_INST_WORD_X 1 +#define X3_IMM39_2_SIZE_X 16 +#define X3_IMM39_2_INST_WORD_POS_X 16 +#define X3_IMM39_2_SIGN_VAL_POS_X 20 + +#define X3_P_INST_WORD_X 3 +#define X3_P_SIZE_X 4 +#define X3_P_INST_WORD_POS_X 0 +#define X3_P_SIGN_VAL_POS_X 0 + +#define X3_TMPLT_INST_WORD_X 0 +#define X3_TMPLT_SIZE_X 4 +#define X3_TMPLT_INST_WORD_POS_X 0 +#define X3_TMPLT_SIGN_VAL_POS_X 0 + +#define X3_BTYPE_QP_INST_WORD_X 2 +#define X3_BTYPE_QP_SIZE_X 9 +#define X3_BTYPE_QP_INST_WORD_POS_X 23 +#define X3_BTYPE_QP_INST_VAL_POS_X 0 + +#define X3_EMPTY_INST_WORD_X 1 +#define X3_EMPTY_SIZE_X 2 +#define X3_EMPTY_INST_WORD_POS_X 14 +#define X3_EMPTY_INST_VAL_POS_X 0 + + typedef struct _IMAGE_LINENUMBER { + union { + DWORD SymbolTableIndex; + DWORD VirtualAddress; + } Type; + WORD Linenumber; + } IMAGE_LINENUMBER; + typedef IMAGE_LINENUMBER UNALIGNED *PIMAGE_LINENUMBER; + +#define IMAGE_SIZEOF_LINENUMBER 6 + +#include "poppack.h" + + typedef struct _IMAGE_BASE_RELOCATION { + DWORD VirtualAddress; + DWORD SizeOfBlock; + + } IMAGE_BASE_RELOCATION; + typedef IMAGE_BASE_RELOCATION UNALIGNED *PIMAGE_BASE_RELOCATION; + +#define IMAGE_SIZEOF_BASE_RELOCATION 8 + +#define IMAGE_REL_BASED_ABSOLUTE 0 +#define IMAGE_REL_BASED_HIGH 1 +#define IMAGE_REL_BASED_LOW 2 +#define IMAGE_REL_BASED_HIGHLOW 3 +#define IMAGE_REL_BASED_HIGHADJ 4 +#define IMAGE_REL_BASED_MIPS_JMPADDR 5 +#define IMAGE_REL_BASED_MIPS_JMPADDR16 9 +#define IMAGE_REL_BASED_IA64_IMM64 9 +#define IMAGE_REL_BASED_DIR64 10 + +#define IMAGE_ARCHIVE_START_SIZE 8 +#define IMAGE_ARCHIVE_START "!\n" +#define IMAGE_ARCHIVE_END "`\n" +#define IMAGE_ARCHIVE_PAD "\n" +#define IMAGE_ARCHIVE_LINKER_MEMBER "/ " +#define IMAGE_ARCHIVE_LONGNAMES_MEMBER "// " + + typedef struct _IMAGE_ARCHIVE_MEMBER_HEADER { + BYTE Name[16]; + BYTE Date[12]; + BYTE UserID[6]; + BYTE GroupID[6]; + BYTE Mode[8]; + BYTE Size[10]; + BYTE EndHeader[2]; + } IMAGE_ARCHIVE_MEMBER_HEADER,*PIMAGE_ARCHIVE_MEMBER_HEADER; + +#define IMAGE_SIZEOF_ARCHIVE_MEMBER_HDR 60 + + typedef struct _IMAGE_EXPORT_DIRECTORY { + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD Name; + DWORD Base; + DWORD NumberOfFunctions; + DWORD NumberOfNames; + DWORD AddressOfFunctions; + DWORD AddressOfNames; + DWORD AddressOfNameOrdinals; + } IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY; + + typedef struct _IMAGE_IMPORT_BY_NAME { + WORD Hint; + BYTE Name[1]; + } IMAGE_IMPORT_BY_NAME,*PIMAGE_IMPORT_BY_NAME; + +#include "pshpack8.h" + + typedef struct _IMAGE_THUNK_DATA64 { + union { + ULONGLONG ForwarderString; + ULONGLONG Function; + ULONGLONG Ordinal; + ULONGLONG AddressOfData; + } u1; + } IMAGE_THUNK_DATA64; + typedef IMAGE_THUNK_DATA64 *PIMAGE_THUNK_DATA64; + +#include "poppack.h" + + typedef struct _IMAGE_THUNK_DATA32 { + union { + DWORD ForwarderString; + DWORD Function; + DWORD Ordinal; + DWORD AddressOfData; + } u1; + } IMAGE_THUNK_DATA32; + typedef IMAGE_THUNK_DATA32 *PIMAGE_THUNK_DATA32; + +#define IMAGE_ORDINAL_FLAG64 0x8000000000000000ull +#define IMAGE_ORDINAL_FLAG32 0x80000000 +#define IMAGE_ORDINAL64(Ordinal) (Ordinal & 0xffffull) +#define IMAGE_ORDINAL32(Ordinal) (Ordinal & 0xffff) +#define IMAGE_SNAP_BY_ORDINAL64(Ordinal) ((Ordinal & IMAGE_ORDINAL_FLAG64)!=0) +#define IMAGE_SNAP_BY_ORDINAL32(Ordinal) ((Ordinal & IMAGE_ORDINAL_FLAG32)!=0) + + typedef VOID + (NTAPI *PIMAGE_TLS_CALLBACK)(PVOID DllHandle,DWORD Reason,PVOID Reserved); + + typedef struct _IMAGE_TLS_DIRECTORY64 { + ULONGLONG StartAddressOfRawData; + ULONGLONG EndAddressOfRawData; + ULONGLONG AddressOfIndex; + ULONGLONG AddressOfCallBacks; + DWORD SizeOfZeroFill; + DWORD Characteristics; + } IMAGE_TLS_DIRECTORY64; + typedef IMAGE_TLS_DIRECTORY64 *PIMAGE_TLS_DIRECTORY64; + + typedef struct _IMAGE_TLS_DIRECTORY32 { + DWORD StartAddressOfRawData; + DWORD EndAddressOfRawData; + DWORD AddressOfIndex; + DWORD AddressOfCallBacks; + DWORD SizeOfZeroFill; + DWORD Characteristics; + } IMAGE_TLS_DIRECTORY32; + typedef IMAGE_TLS_DIRECTORY32 *PIMAGE_TLS_DIRECTORY32; + +#ifdef _WIN64 +#define IMAGE_ORDINAL_FLAG IMAGE_ORDINAL_FLAG64 +#define IMAGE_ORDINAL(Ordinal) IMAGE_ORDINAL64(Ordinal) + typedef IMAGE_THUNK_DATA64 IMAGE_THUNK_DATA; + typedef PIMAGE_THUNK_DATA64 PIMAGE_THUNK_DATA; +#define IMAGE_SNAP_BY_ORDINAL(Ordinal) IMAGE_SNAP_BY_ORDINAL64(Ordinal) + typedef IMAGE_TLS_DIRECTORY64 IMAGE_TLS_DIRECTORY; + typedef PIMAGE_TLS_DIRECTORY64 PIMAGE_TLS_DIRECTORY; +#else +#define IMAGE_ORDINAL_FLAG IMAGE_ORDINAL_FLAG32 +#define IMAGE_ORDINAL(Ordinal) IMAGE_ORDINAL32(Ordinal) + typedef IMAGE_THUNK_DATA32 IMAGE_THUNK_DATA; + typedef PIMAGE_THUNK_DATA32 PIMAGE_THUNK_DATA; +#define IMAGE_SNAP_BY_ORDINAL(Ordinal) IMAGE_SNAP_BY_ORDINAL32(Ordinal) + typedef IMAGE_TLS_DIRECTORY32 IMAGE_TLS_DIRECTORY; + typedef PIMAGE_TLS_DIRECTORY32 PIMAGE_TLS_DIRECTORY; +#endif + + typedef struct _IMAGE_IMPORT_DESCRIPTOR { + union { + DWORD Characteristics; + DWORD OriginalFirstThunk; + }; + DWORD TimeDateStamp; + + DWORD ForwarderChain; + DWORD Name; + DWORD FirstThunk; + } IMAGE_IMPORT_DESCRIPTOR; + typedef IMAGE_IMPORT_DESCRIPTOR UNALIGNED *PIMAGE_IMPORT_DESCRIPTOR; + + typedef struct _IMAGE_BOUND_IMPORT_DESCRIPTOR { + DWORD TimeDateStamp; + WORD OffsetModuleName; + WORD NumberOfModuleForwarderRefs; + } IMAGE_BOUND_IMPORT_DESCRIPTOR,*PIMAGE_BOUND_IMPORT_DESCRIPTOR; + + typedef struct _IMAGE_BOUND_FORWARDER_REF { + DWORD TimeDateStamp; + WORD OffsetModuleName; + WORD Reserved; + } IMAGE_BOUND_FORWARDER_REF,*PIMAGE_BOUND_FORWARDER_REF; + + typedef struct _IMAGE_RESOURCE_DIRECTORY { + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + WORD NumberOfNamedEntries; + WORD NumberOfIdEntries; + } IMAGE_RESOURCE_DIRECTORY,*PIMAGE_RESOURCE_DIRECTORY; + +#define IMAGE_RESOURCE_NAME_IS_STRING 0x80000000 +#define IMAGE_RESOURCE_DATA_IS_DIRECTORY 0x80000000 + + typedef struct _IMAGE_RESOURCE_DIRECTORY_ENTRY { + union { + struct { + DWORD NameOffset:31; + DWORD NameIsString:1; + }; + DWORD Name; + WORD Id; + }; + union { + DWORD OffsetToData; + struct { + DWORD OffsetToDirectory:31; + DWORD DataIsDirectory:1; + }; + }; + } IMAGE_RESOURCE_DIRECTORY_ENTRY,*PIMAGE_RESOURCE_DIRECTORY_ENTRY; + + typedef struct _IMAGE_RESOURCE_DIRECTORY_STRING { + WORD Length; + CHAR NameString[1]; + } IMAGE_RESOURCE_DIRECTORY_STRING,*PIMAGE_RESOURCE_DIRECTORY_STRING; + + typedef struct _IMAGE_RESOURCE_DIR_STRING_U { + WORD Length; + WCHAR NameString[1]; + } IMAGE_RESOURCE_DIR_STRING_U,*PIMAGE_RESOURCE_DIR_STRING_U; + + typedef struct _IMAGE_RESOURCE_DATA_ENTRY { + DWORD OffsetToData; + DWORD Size; + DWORD CodePage; + DWORD Reserved; + } IMAGE_RESOURCE_DATA_ENTRY,*PIMAGE_RESOURCE_DATA_ENTRY; + + typedef struct { + DWORD Size; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD GlobalFlagsClear; + DWORD GlobalFlagsSet; + DWORD CriticalSectionDefaultTimeout; + DWORD DeCommitFreeBlockThreshold; + DWORD DeCommitTotalFreeThreshold; + DWORD LockPrefixTable; + DWORD MaximumAllocationSize; + DWORD VirtualMemoryThreshold; + DWORD ProcessHeapFlags; + DWORD ProcessAffinityMask; + WORD CSDVersion; + WORD Reserved1; + DWORD EditList; + DWORD SecurityCookie; + DWORD SEHandlerTable; + DWORD SEHandlerCount; + } IMAGE_LOAD_CONFIG_DIRECTORY32,*PIMAGE_LOAD_CONFIG_DIRECTORY32; + + typedef struct { + DWORD Size; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD GlobalFlagsClear; + DWORD GlobalFlagsSet; + DWORD CriticalSectionDefaultTimeout; + ULONGLONG DeCommitFreeBlockThreshold; + ULONGLONG DeCommitTotalFreeThreshold; + ULONGLONG LockPrefixTable; + ULONGLONG MaximumAllocationSize; + ULONGLONG VirtualMemoryThreshold; + ULONGLONG ProcessAffinityMask; + DWORD ProcessHeapFlags; + WORD CSDVersion; + WORD Reserved1; + ULONGLONG EditList; + ULONGLONG SecurityCookie; + ULONGLONG SEHandlerTable; + ULONGLONG SEHandlerCount; + } IMAGE_LOAD_CONFIG_DIRECTORY64,*PIMAGE_LOAD_CONFIG_DIRECTORY64; + +#ifdef _WIN64 + typedef IMAGE_LOAD_CONFIG_DIRECTORY64 IMAGE_LOAD_CONFIG_DIRECTORY; + typedef PIMAGE_LOAD_CONFIG_DIRECTORY64 PIMAGE_LOAD_CONFIG_DIRECTORY; +#else + typedef IMAGE_LOAD_CONFIG_DIRECTORY32 IMAGE_LOAD_CONFIG_DIRECTORY; + typedef PIMAGE_LOAD_CONFIG_DIRECTORY32 PIMAGE_LOAD_CONFIG_DIRECTORY; +#endif + + typedef struct _IMAGE_CE_RUNTIME_FUNCTION_ENTRY { + DWORD FuncStart; + DWORD PrologLen : 8; + DWORD FuncLen : 22; + DWORD ThirtyTwoBit : 1; + DWORD ExceptionFlag : 1; + } IMAGE_CE_RUNTIME_FUNCTION_ENTRY,*PIMAGE_CE_RUNTIME_FUNCTION_ENTRY; + + typedef struct _IMAGE_ALPHA64_RUNTIME_FUNCTION_ENTRY { + ULONGLONG BeginAddress; + ULONGLONG EndAddress; + ULONGLONG ExceptionHandler; + ULONGLONG HandlerData; + ULONGLONG PrologEndAddress; + } IMAGE_ALPHA64_RUNTIME_FUNCTION_ENTRY,*PIMAGE_ALPHA64_RUNTIME_FUNCTION_ENTRY; + + typedef struct _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY { + DWORD BeginAddress; + DWORD EndAddress; + DWORD ExceptionHandler; + DWORD HandlerData; + DWORD PrologEndAddress; + } IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY,*PIMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY; + + typedef struct _IMAGE_RUNTIME_FUNCTION_ENTRY { + DWORD BeginAddress; + DWORD EndAddress; + DWORD UnwindInfoAddress; + } _IMAGE_RUNTIME_FUNCTION_ENTRY,*_PIMAGE_RUNTIME_FUNCTION_ENTRY; + + typedef _IMAGE_RUNTIME_FUNCTION_ENTRY IMAGE_IA64_RUNTIME_FUNCTION_ENTRY; + typedef _PIMAGE_RUNTIME_FUNCTION_ENTRY PIMAGE_IA64_RUNTIME_FUNCTION_ENTRY; + + typedef _IMAGE_RUNTIME_FUNCTION_ENTRY IMAGE_RUNTIME_FUNCTION_ENTRY; + typedef _PIMAGE_RUNTIME_FUNCTION_ENTRY PIMAGE_RUNTIME_FUNCTION_ENTRY; + + typedef struct _IMAGE_DEBUG_DIRECTORY { + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD Type; + DWORD SizeOfData; + DWORD AddressOfRawData; + DWORD PointerToRawData; + } IMAGE_DEBUG_DIRECTORY,*PIMAGE_DEBUG_DIRECTORY; + +#define IMAGE_DEBUG_TYPE_UNKNOWN 0 +#define IMAGE_DEBUG_TYPE_COFF 1 +#define IMAGE_DEBUG_TYPE_CODEVIEW 2 +#define IMAGE_DEBUG_TYPE_FPO 3 +#define IMAGE_DEBUG_TYPE_MISC 4 +#define IMAGE_DEBUG_TYPE_EXCEPTION 5 +#define IMAGE_DEBUG_TYPE_FIXUP 6 +#define IMAGE_DEBUG_TYPE_OMAP_TO_SRC 7 +#define IMAGE_DEBUG_TYPE_OMAP_FROM_SRC 8 +#define IMAGE_DEBUG_TYPE_BORLAND 9 +#define IMAGE_DEBUG_TYPE_RESERVED10 10 +#define IMAGE_DEBUG_TYPE_CLSID 11 + + typedef struct _IMAGE_COFF_SYMBOLS_HEADER { + DWORD NumberOfSymbols; + DWORD LvaToFirstSymbol; + DWORD NumberOfLinenumbers; + DWORD LvaToFirstLinenumber; + DWORD RvaToFirstByteOfCode; + DWORD RvaToLastByteOfCode; + DWORD RvaToFirstByteOfData; + DWORD RvaToLastByteOfData; + } IMAGE_COFF_SYMBOLS_HEADER,*PIMAGE_COFF_SYMBOLS_HEADER; + +#define FRAME_FPO 0 +#define FRAME_TRAP 1 +#define FRAME_TSS 2 +#define FRAME_NONFPO 3 + + typedef struct _FPO_DATA { + DWORD ulOffStart; + DWORD cbProcSize; + DWORD cdwLocals; + WORD cdwParams; + WORD cbProlog : 8; + WORD cbRegs : 3; + WORD fHasSEH : 1; + WORD fUseBP : 1; + WORD reserved : 1; + WORD cbFrame : 2; + } FPO_DATA,*PFPO_DATA; +#define SIZEOF_RFPO_DATA 16 + +#define IMAGE_DEBUG_MISC_EXENAME 1 + + typedef struct _IMAGE_DEBUG_MISC { + DWORD DataType; + DWORD Length; + BOOLEAN Unicode; + BYTE Reserved[3]; + BYTE Data[1]; + } IMAGE_DEBUG_MISC,*PIMAGE_DEBUG_MISC; + + typedef struct _IMAGE_FUNCTION_ENTRY { + DWORD StartingAddress; + DWORD EndingAddress; + DWORD EndOfPrologue; + } IMAGE_FUNCTION_ENTRY,*PIMAGE_FUNCTION_ENTRY; + + typedef struct _IMAGE_FUNCTION_ENTRY64 { + ULONGLONG StartingAddress; + ULONGLONG EndingAddress; + union { + ULONGLONG EndOfPrologue; + ULONGLONG UnwindInfoAddress; + }; + } IMAGE_FUNCTION_ENTRY64,*PIMAGE_FUNCTION_ENTRY64; + + typedef struct _IMAGE_SEPARATE_DEBUG_HEADER { + WORD Signature; + WORD Flags; + WORD Machine; + WORD Characteristics; + DWORD TimeDateStamp; + DWORD CheckSum; + DWORD ImageBase; + DWORD SizeOfImage; + DWORD NumberOfSections; + DWORD ExportedNamesSize; + DWORD DebugDirectorySize; + DWORD SectionAlignment; + DWORD Reserved[2]; + } IMAGE_SEPARATE_DEBUG_HEADER,*PIMAGE_SEPARATE_DEBUG_HEADER; + + typedef struct _NON_PAGED_DEBUG_INFO { + WORD Signature; + WORD Flags; + DWORD Size; + WORD Machine; + WORD Characteristics; + DWORD TimeDateStamp; + DWORD CheckSum; + DWORD SizeOfImage; + ULONGLONG ImageBase; + + } NON_PAGED_DEBUG_INFO,*PNON_PAGED_DEBUG_INFO; + +#define IMAGE_SEPARATE_DEBUG_SIGNATURE 0x4944 +#define NON_PAGED_DEBUG_SIGNATURE 0x494E + +#define IMAGE_SEPARATE_DEBUG_FLAGS_MASK 0x8000 +#define IMAGE_SEPARATE_DEBUG_MISMATCH 0x8000 + + typedef struct _ImageArchitectureHeader { + unsigned int AmaskValue: 1; + int Adummy1 :7; + unsigned int AmaskShift: 8; + int Adummy2 :16; + DWORD FirstEntryRVA; + } IMAGE_ARCHITECTURE_HEADER,*PIMAGE_ARCHITECTURE_HEADER; + + typedef struct _ImageArchitectureEntry { + DWORD FixupInstRVA; + DWORD NewInst; + } IMAGE_ARCHITECTURE_ENTRY,*PIMAGE_ARCHITECTURE_ENTRY; + +#include "poppack.h" + +#define IMPORT_OBJECT_HDR_SIG2 0xffff + + typedef struct IMPORT_OBJECT_HEADER { + WORD Sig1; + WORD Sig2; + WORD Version; + WORD Machine; + DWORD TimeDateStamp; + DWORD SizeOfData; + union { + WORD Ordinal; + WORD Hint; + }; + WORD Type : 2; + WORD NameType : 3; + WORD Reserved : 11; + } IMPORT_OBJECT_HEADER; + + typedef enum IMPORT_OBJECT_TYPE { + IMPORT_OBJECT_CODE = 0,IMPORT_OBJECT_DATA = 1,IMPORT_OBJECT_CONST = 2 + } IMPORT_OBJECT_TYPE; + + typedef enum IMPORT_OBJECT_NAME_TYPE { + IMPORT_OBJECT_ORDINAL = 0,IMPORT_OBJECT_NAME = 1,IMPORT_OBJECT_NAME_NO_PREFIX = 2,IMPORT_OBJECT_NAME_UNDECORATE = 3 + } IMPORT_OBJECT_NAME_TYPE; + +#ifndef __IMAGE_COR20_HEADER_DEFINED__ +#define __IMAGE_COR20_HEADER_DEFINED__ + typedef enum ReplacesCorHdrNumericDefines { + COMIMAGE_FLAGS_ILONLY =0x00000001,COMIMAGE_FLAGS_32BITREQUIRED =0x00000002,COMIMAGE_FLAGS_IL_LIBRARY =0x00000004, + COMIMAGE_FLAGS_STRONGNAMESIGNED =0x00000008,COMIMAGE_FLAGS_TRACKDEBUGDATA =0x00010000,COR_VERSION_MAJOR_V2 =2, + COR_VERSION_MAJOR =COR_VERSION_MAJOR_V2,COR_VERSION_MINOR =0,COR_DELETED_NAME_LENGTH =8,COR_VTABLEGAP_NAME_LENGTH =8, + NATIVE_TYPE_MAX_CB =1,COR_ILMETHOD_SECT_SMALL_MAX_DATASIZE=0xFF,IMAGE_COR_MIH_METHODRVA =0x01,IMAGE_COR_MIH_EHRVA =0x02, + IMAGE_COR_MIH_BASICBLOCK =0x08,COR_VTABLE_32BIT =0x01,COR_VTABLE_64BIT =0x02,COR_VTABLE_FROM_UNMANAGED =0x04, + COR_VTABLE_CALL_MOST_DERIVED =0x10,IMAGE_COR_EATJ_THUNK_SIZE =32,MAX_CLASS_NAME =1024,MAX_PACKAGE_NAME =1024 + } ReplacesCorHdrNumericDefines; + + typedef struct IMAGE_COR20_HEADER { + DWORD cb; + WORD MajorRuntimeVersion; + WORD MinorRuntimeVersion; + IMAGE_DATA_DIRECTORY MetaData; + DWORD Flags; + DWORD EntryPointToken; + IMAGE_DATA_DIRECTORY Resources; + IMAGE_DATA_DIRECTORY StrongNameSignature; + IMAGE_DATA_DIRECTORY CodeManagerTable; + IMAGE_DATA_DIRECTORY VTableFixups; + IMAGE_DATA_DIRECTORY ExportAddressTableJumps; + IMAGE_DATA_DIRECTORY ManagedNativeHeader; + } IMAGE_COR20_HEADER,*PIMAGE_COR20_HEADER; +#endif + +#if defined (__x86_64) + NTSYSAPI PRUNTIME_FUNCTION NTAPI RtlLookupFunctionEntry (DWORD64 ControlPc, PDWORD64 ImageBase, PUNWIND_HISTORY_TABLE HistoryTable); + NTSYSAPI VOID NTAPI RtlUnwindEx (PVOID TargetFrame, PVOID TargetIp, PEXCEPTION_RECORD ExceptionRecord, PVOID ReturnValue, PCONTEXT ContextRecord, PUNWIND_HISTORY_TABLE HistoryTable); +#endif + +#include + +#ifndef _SLIST_HEADER_ +#define _SLIST_HEADER_ + +#ifdef _WIN64 + typedef struct _SLIST_ENTRY *PSLIST_ENTRY; + typedef DECLSPEC_ALIGN(16) struct _SLIST_ENTRY { + PSLIST_ENTRY Next; + } SLIST_ENTRY; +#else + +#define SLIST_ENTRY SINGLE_LIST_ENTRY +#define _SLIST_ENTRY _SINGLE_LIST_ENTRY +#define PSLIST_ENTRY PSINGLE_LIST_ENTRY +#endif + +#if defined(_WIN64) + + typedef DECLSPEC_ALIGN(16) struct _SLIST_HEADER { + ULONGLONG Alignment; + ULONGLONG Region; + } SLIST_HEADER; + + typedef struct _SLIST_HEADER *PSLIST_HEADER; +#else + + typedef union _SLIST_HEADER { + ULONGLONG Alignment; + struct { + SLIST_ENTRY Next; + WORD Depth; + WORD Sequence; + }; + } SLIST_HEADER,*PSLIST_HEADER; +#endif +#endif + + NTSYSAPI VOID NTAPI RtlInitializeSListHead(PSLIST_HEADER ListHead); + NTSYSAPI PSLIST_ENTRY NTAPI RtlFirstEntrySList(const SLIST_HEADER *ListHead); + NTSYSAPI PSLIST_ENTRY NTAPI RtlInterlockedPopEntrySList(PSLIST_HEADER ListHead); + NTSYSAPI PSLIST_ENTRY NTAPI RtlInterlockedPushEntrySList(PSLIST_HEADER ListHead,PSLIST_ENTRY ListEntry); + NTSYSAPI PSLIST_ENTRY NTAPI RtlInterlockedFlushSList(PSLIST_HEADER ListHead); + NTSYSAPI WORD NTAPI RtlQueryDepthSList(PSLIST_HEADER ListHead); + +#define HEAP_NO_SERIALIZE 0x00000001 +#define HEAP_GROWABLE 0x00000002 +#define HEAP_GENERATE_EXCEPTIONS 0x00000004 +#define HEAP_ZERO_MEMORY 0x00000008 +#define HEAP_REALLOC_IN_PLACE_ONLY 0x00000010 +#define HEAP_TAIL_CHECKING_ENABLED 0x00000020 +#define HEAP_FREE_CHECKING_ENABLED 0x00000040 +#define HEAP_DISABLE_COALESCE_ON_FREE 0x00000080 +#define HEAP_CREATE_ALIGN_16 0x00010000 +#define HEAP_CREATE_ENABLE_TRACING 0x00020000 +#define HEAP_CREATE_ENABLE_EXECUTE 0x00040000 +#define HEAP_MAXIMUM_TAG 0x0FFF +#define HEAP_PSEUDO_TAG_FLAG 0x8000 +#define HEAP_TAG_SHIFT 18 +#define HEAP_MAKE_TAG_FLAGS(b,o) ((DWORD)((b) + ((o) << 18))) + + NTSYSAPI VOID NTAPI RtlCaptureContext(PCONTEXT ContextRecord); + +#define IS_TEXT_UNICODE_ASCII16 0x0001 +#define IS_TEXT_UNICODE_REVERSE_ASCII16 0x0010 + +#define IS_TEXT_UNICODE_STATISTICS 0x0002 +#define IS_TEXT_UNICODE_REVERSE_STATISTICS 0x0020 + +#define IS_TEXT_UNICODE_CONTROLS 0x0004 +#define IS_TEXT_UNICODE_REVERSE_CONTROLS 0x0040 + +#define IS_TEXT_UNICODE_SIGNATURE 0x0008 +#define IS_TEXT_UNICODE_REVERSE_SIGNATURE 0x0080 + +#define IS_TEXT_UNICODE_ILLEGAL_CHARS 0x0100 +#define IS_TEXT_UNICODE_ODD_LENGTH 0x0200 +#define IS_TEXT_UNICODE_DBCS_LEADBYTE 0x0400 +#define IS_TEXT_UNICODE_NULL_BYTES 0x1000 + +#define IS_TEXT_UNICODE_UNICODE_MASK 0x000F +#define IS_TEXT_UNICODE_REVERSE_MASK 0x00F0 +#define IS_TEXT_UNICODE_NOT_UNICODE_MASK 0x0F00 +#define IS_TEXT_UNICODE_NOT_ASCII_MASK 0xF000 + +#define COMPRESSION_FORMAT_NONE (0x0000) +#define COMPRESSION_FORMAT_DEFAULT (0x0001) +#define COMPRESSION_FORMAT_LZNT1 (0x0002) +#define COMPRESSION_ENGINE_STANDARD (0x0000) +#define COMPRESSION_ENGINE_MAXIMUM (0x0100) +#define COMPRESSION_ENGINE_HIBER (0x0200) + +#if _DBG_MEMCPY_INLINE_ && !defined(_MEMCPY_INLINE_) && !defined(_CRTBLD) +#define _MEMCPY_INLINE_ + __CRT_INLINE PVOID __cdecl memcpy_inline(void *dst,const void *src,size_t size) { + if(((char *)dst > (char *)src) && ((char *)dst < ((char *)src + size))) { + __debugbreak(); + } + return memcpy(dst,src,size); + } +#define memcpy memcpy_inline +#endif + + NTSYSAPI SIZE_T NTAPI RtlCompareMemory(const VOID *Source1,const VOID *Source2,SIZE_T Length); + +#define RtlEqualMemory(Destination,Source,Length) (!memcmp((Destination),(Source),(Length))) +#define RtlMoveMemory(Destination,Source,Length) memmove((Destination),(Source),(Length)) +#define RtlCopyMemory(Destination,Source,Length) memcpy((Destination),(Source),(Length)) +#define RtlFillMemory(Destination,Length,Fill) memset((Destination),(Fill),(Length)) +#define RtlZeroMemory(Destination,Length) memset((Destination),0,(Length)) + + __CRT_INLINE PVOID RtlSecureZeroMemory(PVOID ptr,SIZE_T cnt) { + volatile char *vptr =(volatile char *)ptr; +#ifdef __x86_64 + __stosb((PBYTE)((DWORD64)vptr),0,cnt); +#else + while(cnt) { + *vptr = 0; + vptr++; + cnt--; + } +#endif + return ptr; + } + + typedef struct _MESSAGE_RESOURCE_ENTRY { + WORD Length; + WORD Flags; + BYTE Text[1]; + } MESSAGE_RESOURCE_ENTRY,*PMESSAGE_RESOURCE_ENTRY; + +#define MESSAGE_RESOURCE_UNICODE 0x0001 + + typedef struct _MESSAGE_RESOURCE_BLOCK { + DWORD LowId; + DWORD HighId; + DWORD OffsetToEntries; + } MESSAGE_RESOURCE_BLOCK,*PMESSAGE_RESOURCE_BLOCK; + + typedef struct _MESSAGE_RESOURCE_DATA { + DWORD NumberOfBlocks; + MESSAGE_RESOURCE_BLOCK Blocks[1]; + } MESSAGE_RESOURCE_DATA,*PMESSAGE_RESOURCE_DATA; + + typedef struct _OSVERSIONINFOA { + DWORD dwOSVersionInfoSize; + DWORD dwMajorVersion; + DWORD dwMinorVersion; + DWORD dwBuildNumber; + DWORD dwPlatformId; + CHAR szCSDVersion[128]; + } OSVERSIONINFOA,*POSVERSIONINFOA,*LPOSVERSIONINFOA; + + typedef struct _OSVERSIONINFOW { + DWORD dwOSVersionInfoSize; + DWORD dwMajorVersion; + DWORD dwMinorVersion; + DWORD dwBuildNumber; + DWORD dwPlatformId; + WCHAR szCSDVersion[128]; + } OSVERSIONINFOW,*POSVERSIONINFOW,*LPOSVERSIONINFOW,RTL_OSVERSIONINFOW,*PRTL_OSVERSIONINFOW; + +#ifdef UNICODE + typedef OSVERSIONINFOW OSVERSIONINFO; + typedef POSVERSIONINFOW POSVERSIONINFO; + typedef LPOSVERSIONINFOW LPOSVERSIONINFO; +#else + typedef OSVERSIONINFOA OSVERSIONINFO; + typedef POSVERSIONINFOA POSVERSIONINFO; + typedef LPOSVERSIONINFOA LPOSVERSIONINFO; +#endif + + typedef struct _OSVERSIONINFOEXA { + DWORD dwOSVersionInfoSize; + DWORD dwMajorVersion; + DWORD dwMinorVersion; + DWORD dwBuildNumber; + DWORD dwPlatformId; + CHAR szCSDVersion[128]; + WORD wServicePackMajor; + WORD wServicePackMinor; + WORD wSuiteMask; + BYTE wProductType; + BYTE wReserved; + } OSVERSIONINFOEXA,*POSVERSIONINFOEXA,*LPOSVERSIONINFOEXA; + + typedef struct _OSVERSIONINFOEXW { + DWORD dwOSVersionInfoSize; + DWORD dwMajorVersion; + DWORD dwMinorVersion; + DWORD dwBuildNumber; + DWORD dwPlatformId; + WCHAR szCSDVersion[128]; + WORD wServicePackMajor; + WORD wServicePackMinor; + WORD wSuiteMask; + BYTE wProductType; + BYTE wReserved; + } OSVERSIONINFOEXW,*POSVERSIONINFOEXW,*LPOSVERSIONINFOEXW,RTL_OSVERSIONINFOEXW,*PRTL_OSVERSIONINFOEXW; +#ifdef UNICODE + typedef OSVERSIONINFOEXW OSVERSIONINFOEX; + typedef POSVERSIONINFOEXW POSVERSIONINFOEX; + typedef LPOSVERSIONINFOEXW LPOSVERSIONINFOEX; +#else + typedef OSVERSIONINFOEXA OSVERSIONINFOEX; + typedef POSVERSIONINFOEXA POSVERSIONINFOEX; + typedef LPOSVERSIONINFOEXA LPOSVERSIONINFOEX; +#endif + +#define VER_EQUAL 1 +#define VER_GREATER 2 +#define VER_GREATER_EQUAL 3 +#define VER_LESS 4 +#define VER_LESS_EQUAL 5 +#define VER_AND 6 +#define VER_OR 7 + +#define VER_CONDITION_MASK 7 +#define VER_NUM_BITS_PER_CONDITION_MASK 3 + +#define VER_MINORVERSION 0x0000001 +#define VER_MAJORVERSION 0x0000002 +#define VER_BUILDNUMBER 0x0000004 +#define VER_PLATFORMID 0x0000008 +#define VER_SERVICEPACKMINOR 0x0000010 +#define VER_SERVICEPACKMAJOR 0x0000020 +#define VER_SUITENAME 0x0000040 +#define VER_PRODUCT_TYPE 0x0000080 + +#define VER_NT_WORKSTATION 0x0000001 +#define VER_NT_DOMAIN_CONTROLLER 0x0000002 +#define VER_NT_SERVER 0x0000003 + +#define VER_PLATFORM_WIN32s 0 +#define VER_PLATFORM_WIN32_WINDOWS 1 +#define VER_PLATFORM_WIN32_NT 2 + +#define VER_SET_CONDITION(_m_,_t_,_c_) ((_m_)=VerSetConditionMask((_m_),(_t_),(_c_))) + + NTSYSAPI ULONGLONG NTAPI VerSetConditionMask(ULONGLONG ConditionMask,DWORD TypeMask,BYTE Condition); + + typedef struct _RTL_CRITICAL_SECTION_DEBUG { + WORD Type; + WORD CreatorBackTraceIndex; + struct _RTL_CRITICAL_SECTION *CriticalSection; + LIST_ENTRY ProcessLocksList; + DWORD EntryCount; + DWORD ContentionCount; + DWORD Spare[2]; + } RTL_CRITICAL_SECTION_DEBUG,*PRTL_CRITICAL_SECTION_DEBUG,RTL_RESOURCE_DEBUG,*PRTL_RESOURCE_DEBUG; + +#define RTL_CRITSECT_TYPE 0 +#define RTL_RESOURCE_TYPE 1 + + typedef struct _RTL_CRITICAL_SECTION { + PRTL_CRITICAL_SECTION_DEBUG DebugInfo; + LONG LockCount; + LONG RecursionCount; + HANDLE OwningThread; + HANDLE LockSemaphore; + ULONG_PTR SpinCount; + } RTL_CRITICAL_SECTION,*PRTL_CRITICAL_SECTION; + + typedef VOID (NTAPI *RTL_VERIFIER_DLL_LOAD_CALLBACK) (PWSTR DllName,PVOID DllBase,SIZE_T DllSize,PVOID Reserved); + typedef VOID (NTAPI *RTL_VERIFIER_DLL_UNLOAD_CALLBACK) (PWSTR DllName,PVOID DllBase,SIZE_T DllSize,PVOID Reserved); + typedef VOID (NTAPI *RTL_VERIFIER_NTDLLHEAPFREE_CALLBACK)(PVOID AllocationBase,SIZE_T AllocationSize); + + typedef struct _RTL_VERIFIER_THUNK_DESCRIPTOR { + PCHAR ThunkName; + PVOID ThunkOldAddress; + PVOID ThunkNewAddress; + } RTL_VERIFIER_THUNK_DESCRIPTOR,*PRTL_VERIFIER_THUNK_DESCRIPTOR; + + typedef struct _RTL_VERIFIER_DLL_DESCRIPTOR { + PWCHAR DllName; + DWORD DllFlags; + PVOID DllAddress; + PRTL_VERIFIER_THUNK_DESCRIPTOR DllThunks; + } RTL_VERIFIER_DLL_DESCRIPTOR,*PRTL_VERIFIER_DLL_DESCRIPTOR; + + typedef struct _RTL_VERIFIER_PROVIDER_DESCRIPTOR { + DWORD Length; + PRTL_VERIFIER_DLL_DESCRIPTOR ProviderDlls; + RTL_VERIFIER_DLL_LOAD_CALLBACK ProviderDllLoadCallback; + RTL_VERIFIER_DLL_UNLOAD_CALLBACK ProviderDllUnloadCallback; + PWSTR VerifierImage; + DWORD VerifierFlags; + DWORD VerifierDebug; + PVOID RtlpGetStackTraceAddress; + PVOID RtlpDebugPageHeapCreate; + PVOID RtlpDebugPageHeapDestroy; + RTL_VERIFIER_NTDLLHEAPFREE_CALLBACK ProviderNtdllHeapFreeCallback; + } RTL_VERIFIER_PROVIDER_DESCRIPTOR,*PRTL_VERIFIER_PROVIDER_DESCRIPTOR; + +#define RTL_VRF_FLG_FULL_PAGE_HEAP 0x00000001 +#define RTL_VRF_FLG_RESERVED_DONOTUSE 0x00000002 +#define RTL_VRF_FLG_HANDLE_CHECKS 0x00000004 +#define RTL_VRF_FLG_STACK_CHECKS 0x00000008 +#define RTL_VRF_FLG_APPCOMPAT_CHECKS 0x00000010 +#define RTL_VRF_FLG_TLS_CHECKS 0x00000020 +#define RTL_VRF_FLG_DIRTY_STACKS 0x00000040 +#define RTL_VRF_FLG_RPC_CHECKS 0x00000080 +#define RTL_VRF_FLG_COM_CHECKS 0x00000100 +#define RTL_VRF_FLG_DANGEROUS_APIS 0x00000200 +#define RTL_VRF_FLG_RACE_CHECKS 0x00000400 +#define RTL_VRF_FLG_DEADLOCK_CHECKS 0x00000800 +#define RTL_VRF_FLG_FIRST_CHANCE_EXCEPTION_CHECKS 0x00001000 +#define RTL_VRF_FLG_VIRTUAL_MEM_CHECKS 0x00002000 +#define RTL_VRF_FLG_ENABLE_LOGGING 0x00004000 +#define RTL_VRF_FLG_FAST_FILL_HEAP 0x00008000 +#define RTL_VRF_FLG_VIRTUAL_SPACE_TRACKING 0x00010000 +#define RTL_VRF_FLG_ENABLED_SYSTEM_WIDE 0x00020000 +#define RTL_VRF_FLG_MISCELLANEOUS_CHECKS 0x00020000 +#define RTL_VRF_FLG_LOCK_CHECKS 0x00040000 + +#define APPLICATION_VERIFIER_INTERNAL_ERROR 0x80000000 +#define APPLICATION_VERIFIER_INTERNAL_WARNING 0x40000000 +#define APPLICATION_VERIFIER_NO_BREAK 0x20000000 +#define APPLICATION_VERIFIER_CONTINUABLE_BREAK 0x10000000 + +#define APPLICATION_VERIFIER_UNKNOWN_ERROR 0x0001 +#define APPLICATION_VERIFIER_ACCESS_VIOLATION 0x0002 +#define APPLICATION_VERIFIER_UNSYNCHRONIZED_ACCESS 0x0003 +#define APPLICATION_VERIFIER_EXTREME_SIZE_REQUEST 0x0004 +#define APPLICATION_VERIFIER_BAD_HEAP_HANDLE 0x0005 +#define APPLICATION_VERIFIER_SWITCHED_HEAP_HANDLE 0x0006 +#define APPLICATION_VERIFIER_DOUBLE_FREE 0x0007 +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK 0x0008 +#define APPLICATION_VERIFIER_DESTROY_PROCESS_HEAP 0x0009 +#define APPLICATION_VERIFIER_UNEXPECTED_EXCEPTION 0x000A +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_EXCEPTION_RAISED_FOR_HEADER 0x000B +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_EXCEPTION_RAISED_FOR_PROBING 0x000C +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_HEADER 0x000D +#define APPLICATION_VERIFIER_CORRUPTED_FREED_HEAP_BLOCK 0x000E +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_SUFFIX 0x000F +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_START_STAMP 0x0010 +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_END_STAMP 0x0011 +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_BLOCK_PREFIX 0x0012 +#define APPLICATION_VERIFIER_FIRST_CHANCE_ACCESS_VIOLATION 0x0013 +#define APPLICATION_VERIFIER_CORRUPTED_HEAP_LIST 0x0014 + +#define APPLICATION_VERIFIER_TERMINATE_THREAD_CALL 0x0100 +#define APPLICATION_VERIFIER_STACK_OVERFLOW 0x0101 +#define APPLICATION_VERIFIER_INVALID_EXIT_PROCESS_CALL 0x0102 + +#define APPLICATION_VERIFIER_EXIT_THREAD_OWNS_LOCK 0x0200 +#define APPLICATION_VERIFIER_LOCK_IN_UNLOADED_DLL 0x0201 +#define APPLICATION_VERIFIER_LOCK_IN_FREED_HEAP 0x0202 +#define APPLICATION_VERIFIER_LOCK_DOUBLE_INITIALIZE 0x0203 +#define APPLICATION_VERIFIER_LOCK_IN_FREED_MEMORY 0x0204 +#define APPLICATION_VERIFIER_LOCK_CORRUPTED 0x0205 +#define APPLICATION_VERIFIER_LOCK_INVALID_OWNER 0x0206 +#define APPLICATION_VERIFIER_LOCK_INVALID_RECURSION_COUNT 0x0207 +#define APPLICATION_VERIFIER_LOCK_INVALID_LOCK_COUNT 0x0208 +#define APPLICATION_VERIFIER_LOCK_OVER_RELEASED 0x0209 +#define APPLICATION_VERIFIER_LOCK_NOT_INITIALIZED 0x0210 +#define APPLICATION_VERIFIER_LOCK_ALREADY_INITIALIZED 0x0211 +#define APPLICATION_VERIFIER_LOCK_IN_FREED_VMEM 0x0212 +#define APPLICATION_VERIFIER_LOCK_IN_UNMAPPED_MEM 0x0213 +#define APPLICATION_VERIFIER_THREAD_NOT_LOCK_OWNER 0x0214 + +#define APPLICATION_VERIFIER_INVALID_HANDLE 0x0300 +#define APPLICATION_VERIFIER_INVALID_TLS_VALUE 0x0301 +#define APPLICATION_VERIFIER_INCORRECT_WAIT_CALL 0x0302 +#define APPLICATION_VERIFIER_NULL_HANDLE 0x0303 +#define APPLICATION_VERIFIER_WAIT_IN_DLLMAIN 0x0304 + +#define APPLICATION_VERIFIER_COM_ERROR 0x0400 +#define APPLICATION_VERIFIER_COM_API_IN_DLLMAIN 0x0401 +#define APPLICATION_VERIFIER_COM_UNHANDLED_EXCEPTION 0x0402 +#define APPLICATION_VERIFIER_COM_UNBALANCED_COINIT 0x0403 +#define APPLICATION_VERIFIER_COM_UNBALANCED_OLEINIT 0x0404 +#define APPLICATION_VERIFIER_COM_UNBALANCED_SWC 0x0405 +#define APPLICATION_VERIFIER_COM_NULL_DACL 0x0406 +#define APPLICATION_VERIFIER_COM_UNSAFE_IMPERSONATION 0x0407 +#define APPLICATION_VERIFIER_COM_SMUGGLED_WRAPPER 0x0408 +#define APPLICATION_VERIFIER_COM_SMUGGLED_PROXY 0x0409 +#define APPLICATION_VERIFIER_COM_CF_SUCCESS_WITH_NULL 0x040A +#define APPLICATION_VERIFIER_COM_GCO_SUCCESS_WITH_NULL 0x040B +#define APPLICATION_VERIFIER_COM_OBJECT_IN_FREED_MEMORY 0x040C +#define APPLICATION_VERIFIER_COM_OBJECT_IN_UNLOADED_DLL 0x040D +#define APPLICATION_VERIFIER_COM_VTBL_IN_FREED_MEMORY 0x040E +#define APPLICATION_VERIFIER_COM_VTBL_IN_UNLOADED_DLL 0x040F +#define APPLICATION_VERIFIER_COM_HOLDING_LOCKS_ON_CALL 0x0410 + +#define APPLICATION_VERIFIER_RPC_ERROR 0x0500 + +#define APPLICATION_VERIFIER_INVALID_FREEMEM 0x0600 +#define APPLICATION_VERIFIER_INVALID_ALLOCMEM 0x0601 +#define APPLICATION_VERIFIER_INVALID_MAPVIEW 0x0602 +#define APPLICATION_VERIFIER_PROBE_INVALID_ADDRESS 0x0603 +#define APPLICATION_VERIFIER_PROBE_FREE_MEM 0x0604 +#define APPLICATION_VERIFIER_PROBE_GUARD_PAGE 0x0605 +#define APPLICATION_VERIFIER_PROBE_NULL 0x0606 +#define APPLICATION_VERIFIER_PROBE_INVALID_START_OR_SIZE 0x0607 +#define APPLICATION_VERIFIER_SIZE_HEAP_UNEXPECTED_EXCEPTION 0x0618 + +#define VERIFIER_STOP(Code,Msg,P1,S1,P2,S2,P3,S3,P4,S4) { RtlApplicationVerifierStop ((Code),(Msg),(ULONG_PTR)(P1),(S1),(ULONG_PTR)(P2),(S2),(ULONG_PTR)(P3),(S3),(ULONG_PTR)(P4),(S4)); } + + VOID NTAPI RtlApplicationVerifierStop(ULONG_PTR Code,PSTR Message,ULONG_PTR Param1,PSTR Description1,ULONG_PTR Param2,PSTR Description2,ULONG_PTR Param3,PSTR Description3,ULONG_PTR Param4,PSTR Description4); + + typedef LONG (NTAPI *PVECTORED_EXCEPTION_HANDLER)(struct _EXCEPTION_POINTERS *ExceptionInfo); +#define SEF_DACL_AUTO_INHERIT 0x01 +#define SEF_SACL_AUTO_INHERIT 0x02 +#define SEF_DEFAULT_DESCRIPTOR_FOR_OBJECT 0x04 +#define SEF_AVOID_PRIVILEGE_CHECK 0x08 +#define SEF_AVOID_OWNER_CHECK 0x10 +#define SEF_DEFAULT_OWNER_FROM_PARENT 0x20 +#define SEF_DEFAULT_GROUP_FROM_PARENT 0x40 + + typedef enum _HEAP_INFORMATION_CLASS { + HeapCompatibilityInformation + } HEAP_INFORMATION_CLASS; + + NTSYSAPI DWORD NTAPI RtlSetHeapInformation(PVOID HeapHandle,HEAP_INFORMATION_CLASS HeapInformationClass,PVOID HeapInformation,SIZE_T HeapInformationLength); + NTSYSAPI DWORD NTAPI RtlQueryHeapInformation(PVOID HeapHandle,HEAP_INFORMATION_CLASS HeapInformationClass,PVOID HeapInformation,SIZE_T HeapInformationLength,PSIZE_T ReturnLength); + DWORD NTAPI RtlMultipleAllocateHeap(PVOID HeapHandle,DWORD Flags,SIZE_T Size,DWORD Count,PVOID *Array); + DWORD NTAPI RtlMultipleFreeHeap(PVOID HeapHandle,DWORD Flags,DWORD Count,PVOID *Array); + +#define WT_EXECUTEDEFAULT 0x00000000 +#define WT_EXECUTEINIOTHREAD 0x00000001 +#define WT_EXECUTEINUITHREAD 0x00000002 +#define WT_EXECUTEINWAITTHREAD 0x00000004 +#define WT_EXECUTEONLYONCE 0x00000008 +#define WT_EXECUTEINTIMERTHREAD 0x00000020 +#define WT_EXECUTELONGFUNCTION 0x00000010 +#define WT_EXECUTEINPERSISTENTIOTHREAD 0x00000040 +#define WT_EXECUTEINPERSISTENTTHREAD 0x00000080 +#define WT_TRANSFER_IMPERSONATION 0x00000100 +#define WT_SET_MAX_THREADPOOL_THREADS(Flags,Limit) ((Flags) |= (Limit)<<16) + typedef VOID (NTAPI *WAITORTIMERCALLBACKFUNC)(PVOID,BOOLEAN); + typedef VOID (NTAPI *WORKERCALLBACKFUNC)(PVOID); + typedef VOID (NTAPI *APC_CALLBACK_FUNCTION)(DWORD ,PVOID,PVOID); + typedef + VOID + (NTAPI *PFLS_CALLBACK_FUNCTION)(PVOID lpFlsData); +#define WT_EXECUTEINLONGTHREAD 0x00000010 +#define WT_EXECUTEDELETEWAIT 0x00000008 + + typedef enum _ACTIVATION_CONTEXT_INFO_CLASS { + ActivationContextBasicInformation = 1,ActivationContextDetailedInformation = 2,AssemblyDetailedInformationInActivationContext = 3,FileInformationInAssemblyOfAssemblyInActivationContext = 4,MaxActivationContextInfoClass,AssemblyDetailedInformationInActivationContxt = 3,FileInformationInAssemblyOfAssemblyInActivationContxt = 4 + } ACTIVATION_CONTEXT_INFO_CLASS; + +#define ACTIVATIONCONTEXTINFOCLASS ACTIVATION_CONTEXT_INFO_CLASS + + typedef struct _ACTIVATION_CONTEXT_QUERY_INDEX { + DWORD ulAssemblyIndex; + DWORD ulFileIndexInAssembly; + } ACTIVATION_CONTEXT_QUERY_INDEX,*PACTIVATION_CONTEXT_QUERY_INDEX; + + typedef const struct _ACTIVATION_CONTEXT_QUERY_INDEX *PCACTIVATION_CONTEXT_QUERY_INDEX; + +#define ACTIVATION_CONTEXT_PATH_TYPE_NONE (1) +#define ACTIVATION_CONTEXT_PATH_TYPE_WIN32_FILE (2) +#define ACTIVATION_CONTEXT_PATH_TYPE_URL (3) +#define ACTIVATION_CONTEXT_PATH_TYPE_ASSEMBLYREF (4) + + typedef struct _ASSEMBLY_FILE_DETAILED_INFORMATION { + DWORD ulFlags; + DWORD ulFilenameLength; + DWORD ulPathLength; + + PCWSTR lpFileName; + PCWSTR lpFilePath; + } ASSEMBLY_FILE_DETAILED_INFORMATION,*PASSEMBLY_FILE_DETAILED_INFORMATION; + typedef const ASSEMBLY_FILE_DETAILED_INFORMATION *PCASSEMBLY_FILE_DETAILED_INFORMATION; + +#define _ASSEMBLY_DLL_REDIRECTION_DETAILED_INFORMATION _ASSEMBLY_FILE_DETAILED_INFORMATION +#define ASSEMBLY_DLL_REDIRECTION_DETAILED_INFORMATION ASSEMBLY_FILE_DETAILED_INFORMATION +#define PASSEMBLY_DLL_REDIRECTION_DETAILED_INFORMATION PASSEMBLY_FILE_DETAILED_INFORMATION +#define PCASSEMBLY_DLL_REDIRECTION_DETAILED_INFORMATION PCASSEMBLY_FILE_DETAILED_INFORMATION + + typedef struct _ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION { + DWORD ulFlags; + DWORD ulEncodedAssemblyIdentityLength; + DWORD ulManifestPathType; + DWORD ulManifestPathLength; + LARGE_INTEGER liManifestLastWriteTime; + DWORD ulPolicyPathType; + DWORD ulPolicyPathLength; + LARGE_INTEGER liPolicyLastWriteTime; + DWORD ulMetadataSatelliteRosterIndex; + DWORD ulManifestVersionMajor; + DWORD ulManifestVersionMinor; + DWORD ulPolicyVersionMajor; + DWORD ulPolicyVersionMinor; + DWORD ulAssemblyDirectoryNameLength; + PCWSTR lpAssemblyEncodedAssemblyIdentity; + PCWSTR lpAssemblyManifestPath; + PCWSTR lpAssemblyPolicyPath; + PCWSTR lpAssemblyDirectoryName; + DWORD ulFileCount; + } ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION,*PACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION; + + typedef const struct _ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *PCACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION; + + typedef struct _ACTIVATION_CONTEXT_DETAILED_INFORMATION { + DWORD dwFlags; + DWORD ulFormatVersion; + DWORD ulAssemblyCount; + DWORD ulRootManifestPathType; + DWORD ulRootManifestPathChars; + DWORD ulRootConfigurationPathType; + DWORD ulRootConfigurationPathChars; + DWORD ulAppDirPathType; + DWORD ulAppDirPathChars; + PCWSTR lpRootManifestPath; + PCWSTR lpRootConfigurationPath; + PCWSTR lpAppDirPath; + } ACTIVATION_CONTEXT_DETAILED_INFORMATION,*PACTIVATION_CONTEXT_DETAILED_INFORMATION; + + typedef const struct _ACTIVATION_CONTEXT_DETAILED_INFORMATION *PCACTIVATION_CONTEXT_DETAILED_INFORMATION; + +#define DLL_PROCESS_ATTACH 1 +#define DLL_THREAD_ATTACH 2 +#define DLL_THREAD_DETACH 3 +#define DLL_PROCESS_DETACH 0 +#define DLL_PROCESS_VERIFIER 4 + +#define EVENTLOG_SEQUENTIAL_READ 0x0001 +#define EVENTLOG_SEEK_READ 0x0002 +#define EVENTLOG_FORWARDS_READ 0x0004 +#define EVENTLOG_BACKWARDS_READ 0x0008 + +#define EVENTLOG_SUCCESS 0x0000 +#define EVENTLOG_ERROR_TYPE 0x0001 +#define EVENTLOG_WARNING_TYPE 0x0002 +#define EVENTLOG_INFORMATION_TYPE 0x0004 +#define EVENTLOG_AUDIT_SUCCESS 0x0008 +#define EVENTLOG_AUDIT_FAILURE 0x0010 + +#define EVENTLOG_START_PAIRED_EVENT 0x0001 +#define EVENTLOG_END_PAIRED_EVENT 0x0002 +#define EVENTLOG_END_ALL_PAIRED_EVENTS 0x0004 +#define EVENTLOG_PAIRED_EVENT_ACTIVE 0x0008 +#define EVENTLOG_PAIRED_EVENT_INACTIVE 0x0010 + + typedef struct _EVENTLOGRECORD { + DWORD Length; + DWORD Reserved; + DWORD RecordNumber; + DWORD TimeGenerated; + DWORD TimeWritten; + DWORD EventID; + WORD EventType; + WORD NumStrings; + WORD EventCategory; + WORD ReservedFlags; + DWORD ClosingRecordNumber; + DWORD StringOffset; + DWORD UserSidLength; + DWORD UserSidOffset; + DWORD DataLength; + DWORD DataOffset; + } EVENTLOGRECORD,*PEVENTLOGRECORD; + +#define MAXLOGICALLOGNAMESIZE 256 + + typedef struct _EVENTSFORLOGFILE{ + DWORD ulSize; + WCHAR szLogicalLogFile[MAXLOGICALLOGNAMESIZE]; + DWORD ulNumRecords; + EVENTLOGRECORD pEventLogRecords[]; + } EVENTSFORLOGFILE,*PEVENTSFORLOGFILE; + + typedef struct _PACKEDEVENTINFO{ + DWORD ulSize; + DWORD ulNumEventsForLogFile; + DWORD ulOffsets[]; + } PACKEDEVENTINFO,*PPACKEDEVENTINFO; + +#define KEY_QUERY_VALUE (0x0001) +#define KEY_SET_VALUE (0x0002) +#define KEY_CREATE_SUB_KEY (0x0004) +#define KEY_ENUMERATE_SUB_KEYS (0x0008) +#define KEY_NOTIFY (0x0010) +#define KEY_CREATE_LINK (0x0020) +#define KEY_WOW64_32KEY (0x0200) +#define KEY_WOW64_64KEY (0x0100) +#define KEY_WOW64_RES (0x0300) + +#define KEY_READ ((STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~SYNCHRONIZE)) +#define KEY_WRITE ((STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~SYNCHRONIZE)) +#define KEY_EXECUTE ((KEY_READ) & (~SYNCHRONIZE)) +#define KEY_ALL_ACCESS ((STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~SYNCHRONIZE)) +#define REG_OPTION_RESERVED (0x00000000L) + +#define REG_OPTION_NON_VOLATILE (0x00000000L) +#define REG_OPTION_VOLATILE (0x00000001L) +#define REG_OPTION_CREATE_LINK (0x00000002L) +#define REG_OPTION_BACKUP_RESTORE (0x00000004L) +#define REG_OPTION_OPEN_LINK (0x00000008L) +#define REG_LEGAL_OPTION (REG_OPTION_RESERVED | REG_OPTION_NON_VOLATILE | REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK | REG_OPTION_BACKUP_RESTORE | REG_OPTION_OPEN_LINK) +#define REG_CREATED_NEW_KEY (0x00000001L) +#define REG_OPENED_EXISTING_KEY (0x00000002L) +#define REG_STANDARD_FORMAT 1 +#define REG_LATEST_FORMAT 2 +#define REG_NO_COMPRESSION 4 +#define REG_WHOLE_HIVE_VOLATILE (0x00000001L) +#define REG_REFRESH_HIVE (0x00000002L) +#define REG_NO_LAZY_FLUSH (0x00000004L) +#define REG_FORCE_RESTORE (0x00000008L) +#define REG_FORCE_UNLOAD 1 + +#define REG_NOTIFY_CHANGE_NAME (0x00000001L) +#define REG_NOTIFY_CHANGE_ATTRIBUTES (0x00000002L) +#define REG_NOTIFY_CHANGE_LAST_SET (0x00000004L) +#define REG_NOTIFY_CHANGE_SECURITY (0x00000008L) + +#define REG_LEGAL_CHANGE_FILTER (REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_ATTRIBUTES | REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_SECURITY) + +#define REG_NONE (0) +#define REG_SZ (1) +#define REG_EXPAND_SZ (2) + +#define REG_BINARY (3) +#define REG_DWORD (4) +#define REG_DWORD_LITTLE_ENDIAN (4) +#define REG_DWORD_BIG_ENDIAN (5) +#define REG_LINK (6) +#define REG_MULTI_SZ (7) +#define REG_RESOURCE_LIST (8) +#define REG_FULL_RESOURCE_DESCRIPTOR (9) +#define REG_RESOURCE_REQUIREMENTS_LIST (10) +#define REG_QWORD (11) +#define REG_QWORD_LITTLE_ENDIAN (11) + +#define SERVICE_KERNEL_DRIVER 0x00000001 +#define SERVICE_FILE_SYSTEM_DRIVER 0x00000002 +#define SERVICE_ADAPTER 0x00000004 +#define SERVICE_RECOGNIZER_DRIVER 0x00000008 + +#define SERVICE_DRIVER (SERVICE_KERNEL_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_RECOGNIZER_DRIVER) + +#define SERVICE_WIN32_OWN_PROCESS 0x00000010 +#define SERVICE_WIN32_SHARE_PROCESS 0x00000020 +#define SERVICE_WIN32 (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS) + +#define SERVICE_INTERACTIVE_PROCESS 0x00000100 + +#define SERVICE_TYPE_ALL (SERVICE_WIN32 | SERVICE_ADAPTER | SERVICE_DRIVER | SERVICE_INTERACTIVE_PROCESS) + +#define SERVICE_BOOT_START 0x00000000 +#define SERVICE_SYSTEM_START 0x00000001 +#define SERVICE_AUTO_START 0x00000002 +#define SERVICE_DEMAND_START 0x00000003 +#define SERVICE_DISABLED 0x00000004 + +#define SERVICE_ERROR_IGNORE 0x00000000 +#define SERVICE_ERROR_NORMAL 0x00000001 +#define SERVICE_ERROR_SEVERE 0x00000002 +#define SERVICE_ERROR_CRITICAL 0x00000003 + + typedef enum _CM_SERVICE_NODE_TYPE { + DriverType = SERVICE_KERNEL_DRIVER,FileSystemType = SERVICE_FILE_SYSTEM_DRIVER,Win32ServiceOwnProcess = SERVICE_WIN32_OWN_PROCESS, + Win32ServiceShareProcess = SERVICE_WIN32_SHARE_PROCESS,AdapterType = SERVICE_ADAPTER,RecognizerType = SERVICE_RECOGNIZER_DRIVER + } SERVICE_NODE_TYPE; + + typedef enum _CM_SERVICE_LOAD_TYPE { + BootLoad = SERVICE_BOOT_START,SystemLoad = SERVICE_SYSTEM_START,AutoLoad = SERVICE_AUTO_START,DemandLoad = SERVICE_DEMAND_START, + DisableLoad = SERVICE_DISABLED + } SERVICE_LOAD_TYPE; + + typedef enum _CM_ERROR_CONTROL_TYPE { + IgnoreError = SERVICE_ERROR_IGNORE,NormalError = SERVICE_ERROR_NORMAL,SevereError = SERVICE_ERROR_SEVERE,CriticalError = SERVICE_ERROR_CRITICAL + } SERVICE_ERROR_TYPE; + +#define TAPE_ERASE_SHORT 0L +#define TAPE_ERASE_LONG 1L + + typedef struct _TAPE_ERASE { + DWORD Type; + BOOLEAN Immediate; + } TAPE_ERASE,*PTAPE_ERASE; + +#define TAPE_LOAD 0L +#define TAPE_UNLOAD 1L +#define TAPE_TENSION 2L +#define TAPE_LOCK 3L +#define TAPE_UNLOCK 4L +#define TAPE_FORMAT 5L + + typedef struct _TAPE_PREPARE { + DWORD Operation; + BOOLEAN Immediate; + } TAPE_PREPARE,*PTAPE_PREPARE; + +#define TAPE_SETMARKS 0L +#define TAPE_FILEMARKS 1L +#define TAPE_SHORT_FILEMARKS 2L +#define TAPE_LONG_FILEMARKS 3L + + typedef struct _TAPE_WRITE_MARKS { + DWORD Type; + DWORD Count; + BOOLEAN Immediate; + } TAPE_WRITE_MARKS,*PTAPE_WRITE_MARKS; + +#define TAPE_ABSOLUTE_POSITION 0L +#define TAPE_LOGICAL_POSITION 1L +#define TAPE_PSEUDO_LOGICAL_POSITION 2L + + typedef struct _TAPE_GET_POSITION { + DWORD Type; + DWORD Partition; + LARGE_INTEGER Offset; + } TAPE_GET_POSITION,*PTAPE_GET_POSITION; + +#define TAPE_REWIND 0L +#define TAPE_ABSOLUTE_BLOCK 1L +#define TAPE_LOGICAL_BLOCK 2L +#define TAPE_PSEUDO_LOGICAL_BLOCK 3L +#define TAPE_SPACE_END_OF_DATA 4L +#define TAPE_SPACE_RELATIVE_BLOCKS 5L +#define TAPE_SPACE_FILEMARKS 6L +#define TAPE_SPACE_SEQUENTIAL_FMKS 7L +#define TAPE_SPACE_SETMARKS 8L +#define TAPE_SPACE_SEQUENTIAL_SMKS 9L + + typedef struct _TAPE_SET_POSITION { + DWORD Method; + DWORD Partition; + LARGE_INTEGER Offset; + BOOLEAN Immediate; + } TAPE_SET_POSITION,*PTAPE_SET_POSITION; + +#define TAPE_DRIVE_FIXED 0x00000001 +#define TAPE_DRIVE_SELECT 0x00000002 +#define TAPE_DRIVE_INITIATOR 0x00000004 + +#define TAPE_DRIVE_ERASE_SHORT 0x00000010 +#define TAPE_DRIVE_ERASE_LONG 0x00000020 +#define TAPE_DRIVE_ERASE_BOP_ONLY 0x00000040 +#define TAPE_DRIVE_ERASE_IMMEDIATE 0x00000080 + +#define TAPE_DRIVE_TAPE_CAPACITY 0x00000100 +#define TAPE_DRIVE_TAPE_REMAINING 0x00000200 +#define TAPE_DRIVE_FIXED_BLOCK 0x00000400 +#define TAPE_DRIVE_VARIABLE_BLOCK 0x00000800 + +#define TAPE_DRIVE_WRITE_PROTECT 0x00001000 +#define TAPE_DRIVE_EOT_WZ_SIZE 0x00002000 + +#define TAPE_DRIVE_ECC 0x00010000 +#define TAPE_DRIVE_COMPRESSION 0x00020000 +#define TAPE_DRIVE_PADDING 0x00040000 +#define TAPE_DRIVE_REPORT_SMKS 0x00080000 + +#define TAPE_DRIVE_GET_ABSOLUTE_BLK 0x00100000 +#define TAPE_DRIVE_GET_LOGICAL_BLK 0x00200000 +#define TAPE_DRIVE_SET_EOT_WZ_SIZE 0x00400000 + +#define TAPE_DRIVE_EJECT_MEDIA 0x01000000 +#define TAPE_DRIVE_CLEAN_REQUESTS 0x02000000 +#define TAPE_DRIVE_SET_CMP_BOP_ONLY 0x04000000 + +#define TAPE_DRIVE_RESERVED_BIT 0x80000000 + +#define TAPE_DRIVE_LOAD_UNLOAD 0x80000001 +#define TAPE_DRIVE_TENSION 0x80000002 +#define TAPE_DRIVE_LOCK_UNLOCK 0x80000004 +#define TAPE_DRIVE_REWIND_IMMEDIATE 0x80000008 + +#define TAPE_DRIVE_SET_BLOCK_SIZE 0x80000010 +#define TAPE_DRIVE_LOAD_UNLD_IMMED 0x80000020 +#define TAPE_DRIVE_TENSION_IMMED 0x80000040 +#define TAPE_DRIVE_LOCK_UNLK_IMMED 0x80000080 + +#define TAPE_DRIVE_SET_ECC 0x80000100 +#define TAPE_DRIVE_SET_COMPRESSION 0x80000200 +#define TAPE_DRIVE_SET_PADDING 0x80000400 +#define TAPE_DRIVE_SET_REPORT_SMKS 0x80000800 + +#define TAPE_DRIVE_ABSOLUTE_BLK 0x80001000 +#define TAPE_DRIVE_ABS_BLK_IMMED 0x80002000 +#define TAPE_DRIVE_LOGICAL_BLK 0x80004000 +#define TAPE_DRIVE_LOG_BLK_IMMED 0x80008000 + +#define TAPE_DRIVE_END_OF_DATA 0x80010000 +#define TAPE_DRIVE_RELATIVE_BLKS 0x80020000 +#define TAPE_DRIVE_FILEMARKS 0x80040000 +#define TAPE_DRIVE_SEQUENTIAL_FMKS 0x80080000 + +#define TAPE_DRIVE_SETMARKS 0x80100000 +#define TAPE_DRIVE_SEQUENTIAL_SMKS 0x80200000 +#define TAPE_DRIVE_REVERSE_POSITION 0x80400000 +#define TAPE_DRIVE_SPACE_IMMEDIATE 0x80800000 + +#define TAPE_DRIVE_WRITE_SETMARKS 0x81000000 +#define TAPE_DRIVE_WRITE_FILEMARKS 0x82000000 +#define TAPE_DRIVE_WRITE_SHORT_FMKS 0x84000000 +#define TAPE_DRIVE_WRITE_LONG_FMKS 0x88000000 + +#define TAPE_DRIVE_WRITE_MARK_IMMED 0x90000000 +#define TAPE_DRIVE_FORMAT 0xA0000000 +#define TAPE_DRIVE_FORMAT_IMMEDIATE 0xC0000000 +#define TAPE_DRIVE_HIGH_FEATURES 0x80000000 + + typedef struct _TAPE_GET_DRIVE_PARAMETERS { + BOOLEAN ECC; + BOOLEAN Compression; + BOOLEAN DataPadding; + BOOLEAN ReportSetmarks; + DWORD DefaultBlockSize; + DWORD MaximumBlockSize; + DWORD MinimumBlockSize; + DWORD MaximumPartitionCount; + DWORD FeaturesLow; + DWORD FeaturesHigh; + DWORD EOTWarningZoneSize; + } TAPE_GET_DRIVE_PARAMETERS,*PTAPE_GET_DRIVE_PARAMETERS; + + typedef struct _TAPE_SET_DRIVE_PARAMETERS { + BOOLEAN ECC; + BOOLEAN Compression; + BOOLEAN DataPadding; + BOOLEAN ReportSetmarks; + DWORD EOTWarningZoneSize; + } TAPE_SET_DRIVE_PARAMETERS,*PTAPE_SET_DRIVE_PARAMETERS; + + typedef struct _TAPE_GET_MEDIA_PARAMETERS { + LARGE_INTEGER Capacity; + LARGE_INTEGER Remaining; + DWORD BlockSize; + DWORD PartitionCount; + BOOLEAN WriteProtected; + } TAPE_GET_MEDIA_PARAMETERS,*PTAPE_GET_MEDIA_PARAMETERS; + + typedef struct _TAPE_SET_MEDIA_PARAMETERS { + DWORD BlockSize; + } TAPE_SET_MEDIA_PARAMETERS,*PTAPE_SET_MEDIA_PARAMETERS; + +#define TAPE_FIXED_PARTITIONS 0L +#define TAPE_SELECT_PARTITIONS 1L +#define TAPE_INITIATOR_PARTITIONS 2L + + typedef struct _TAPE_CREATE_PARTITION { + DWORD Method; + DWORD Count; + DWORD Size; + } TAPE_CREATE_PARTITION,*PTAPE_CREATE_PARTITION; + +#define TAPE_QUERY_DRIVE_PARAMETERS 0L +#define TAPE_QUERY_MEDIA_CAPACITY 1L +#define TAPE_CHECK_FOR_DRIVE_PROBLEM 2L +#define TAPE_QUERY_IO_ERROR_DATA 3L +#define TAPE_QUERY_DEVICE_ERROR_DATA 4L + + typedef struct _TAPE_WMI_OPERATIONS { + DWORD Method; + DWORD DataBufferSize; + PVOID DataBuffer; + } TAPE_WMI_OPERATIONS,*PTAPE_WMI_OPERATIONS; + + typedef enum _TAPE_DRIVE_PROBLEM_TYPE { + TapeDriveProblemNone,TapeDriveReadWriteWarning,TapeDriveReadWriteError,TapeDriveReadWarning,TapeDriveWriteWarning,TapeDriveReadError,TapeDriveWriteError,TapeDriveHardwareError,TapeDriveUnsupportedMedia,TapeDriveScsiConnectionError,TapeDriveTimetoClean,TapeDriveCleanDriveNow,TapeDriveMediaLifeExpired,TapeDriveSnappedTape + } TAPE_DRIVE_PROBLEM_TYPE; + +#if defined(__x86_64) + __CRT_INLINE struct _TEB *NtCurrentTeb(VOID) { return (struct _TEB *)__readgsqword(FIELD_OFFSET(NT_TIB,Self)); } + __CRT_INLINE PVOID GetCurrentFiber(VOID) { return(PVOID)__readgsqword(FIELD_OFFSET(NT_TIB,FiberData)); } + __CRT_INLINE PVOID GetFiberData(VOID) { + return *(PVOID *)GetCurrentFiber(); + } +#endif + +#if(defined(_X86_) && !defined(__x86_64)) +#define PcTeb 0x18 + __CRT_INLINE struct _TEB *NtCurrentTeb(void) { + struct _TEB *ret; + __asm__ volatile ("movl %%fs:0x18,%0" + : "=r" (ret)); + return ret; + } +#endif + +#define ACTIVATION_CONTEXT_SECTION_ASSEMBLY_INFORMATION (1) +#define ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION (2) +#define ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION (3) +#define ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION (4) +#define ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION (5) +#define ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION (6) +#define ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION (7) +#define ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE (8) +#define ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES (9) +#define ACTIVATION_CONTEXT_SECTION_APPLICATION_SETTINGS (10) + +#ifdef __cplusplus + } +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/winreg.h b/05/tcc-final-old/win32/include/winapi/winreg.h new file mode 100644 index 0000000..f158d28 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/winreg.h @@ -0,0 +1,272 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINREG_ +#define _WINREG_ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WINVER +#define WINVER 0x0502 +#endif + +#define RRF_RT_REG_NONE 0x00000001 +#define RRF_RT_REG_SZ 0x00000002 +#define RRF_RT_REG_EXPAND_SZ 0x00000004 +#define RRF_RT_REG_BINARY 0x00000008 +#define RRF_RT_REG_DWORD 0x00000010 +#define RRF_RT_REG_MULTI_SZ 0x00000020 +#define RRF_RT_REG_QWORD 0x00000040 + +#define RRF_RT_DWORD (RRF_RT_REG_BINARY | RRF_RT_REG_DWORD) +#define RRF_RT_QWORD (RRF_RT_REG_BINARY | RRF_RT_REG_QWORD) +#define RRF_RT_ANY 0x0000ffff + +#define RRF_NOEXPAND 0x10000000 +#define RRF_ZEROONFAILURE 0x20000000 + + typedef ACCESS_MASK REGSAM; + +#define HKEY_CLASSES_ROOT ((HKEY) (ULONG_PTR)((LONG)0x80000000)) +#define HKEY_CURRENT_USER ((HKEY) (ULONG_PTR)((LONG)0x80000001)) +#define HKEY_LOCAL_MACHINE ((HKEY) (ULONG_PTR)((LONG)0x80000002)) +#define HKEY_USERS ((HKEY) (ULONG_PTR)((LONG)0x80000003)) +#define HKEY_PERFORMANCE_DATA ((HKEY) (ULONG_PTR)((LONG)0x80000004)) +#define HKEY_PERFORMANCE_TEXT ((HKEY) (ULONG_PTR)((LONG)0x80000050)) +#define HKEY_PERFORMANCE_NLSTEXT ((HKEY) (ULONG_PTR)((LONG)0x80000060)) +#define HKEY_CURRENT_CONFIG ((HKEY) (ULONG_PTR)((LONG)0x80000005)) +#define HKEY_DYN_DATA ((HKEY) (ULONG_PTR)((LONG)0x80000006)) + +#define REG_SECURE_CONNECTION 1 + +#ifndef _PROVIDER_STRUCTS_DEFINED +#define _PROVIDER_STRUCTS_DEFINED + +#define PROVIDER_KEEPS_VALUE_LENGTH 0x1 + struct val_context { + int valuelen; + LPVOID value_context; + LPVOID val_buff_ptr; + }; + + typedef struct val_context *PVALCONTEXT; + + typedef struct pvalueA { + LPSTR pv_valuename; + int pv_valuelen; + LPVOID pv_value_context; + DWORD pv_type; + }PVALUEA,*PPVALUEA; + + typedef struct pvalueW { + LPWSTR pv_valuename; + int pv_valuelen; + LPVOID pv_value_context; + DWORD pv_type; + }PVALUEW,*PPVALUEW; + +#ifdef UNICODE + typedef PVALUEW PVALUE; + typedef PPVALUEW PPVALUE; +#else + typedef PVALUEA PVALUE; + typedef PPVALUEA PPVALUE; +#endif + + typedef DWORD __cdecl QUERYHANDLER(LPVOID keycontext,PVALCONTEXT val_list,DWORD num_vals,LPVOID outputbuffer,DWORD *total_outlen,DWORD input_blen); + + typedef QUERYHANDLER *PQUERYHANDLER; + + typedef struct provider_info { + PQUERYHANDLER pi_R0_1val; + PQUERYHANDLER pi_R0_allvals; + PQUERYHANDLER pi_R3_1val; + PQUERYHANDLER pi_R3_allvals; + DWORD pi_flags; + LPVOID pi_key_context; + } REG_PROVIDER; + + typedef struct provider_info *PPROVIDER; + + typedef struct value_entA { + LPSTR ve_valuename; + DWORD ve_valuelen; + DWORD_PTR ve_valueptr; + DWORD ve_type; + } VALENTA,*PVALENTA; + + typedef struct value_entW { + LPWSTR ve_valuename; + DWORD ve_valuelen; + DWORD_PTR ve_valueptr; + DWORD ve_type; + } VALENTW,*PVALENTW; + +#ifdef UNICODE + typedef VALENTW VALENT; + typedef PVALENTW PVALENT; +#else + typedef VALENTA VALENT; + typedef PVALENTA PVALENT; +#endif +#endif + +#define WIN31_CLASS NULL + +#ifdef UNICODE +#define RegConnectRegistry RegConnectRegistryW +#define RegConnectRegistryEx RegConnectRegistryExW +#define RegCreateKey RegCreateKeyW +#define RegCreateKeyEx RegCreateKeyExW +#define RegDeleteKey RegDeleteKeyW +#define RegDeleteKeyEx RegDeleteKeyExW +#define RegDeleteValue RegDeleteValueW +#define RegEnumKey RegEnumKeyW +#define RegEnumKeyEx RegEnumKeyExW +#define RegEnumValue RegEnumValueW +#define RegLoadKey RegLoadKeyW +#define RegOpenKey RegOpenKeyW +#define RegOpenKeyEx RegOpenKeyExW +#define RegQueryInfoKey RegQueryInfoKeyW +#define RegQueryValue RegQueryValueW +#define RegQueryMultipleValues RegQueryMultipleValuesW +#define RegQueryValueEx RegQueryValueExW +#define RegReplaceKey RegReplaceKeyW +#define RegRestoreKey RegRestoreKeyW +#define RegSaveKey RegSaveKeyW +#define RegSetValue RegSetValueW +#define RegSetValueEx RegSetValueExW +#define RegUnLoadKey RegUnLoadKeyW +#define RegGetValue RegGetValueW +#define InitiateSystemShutdown InitiateSystemShutdownW +#define AbortSystemShutdown AbortSystemShutdownW +#else +#define RegConnectRegistry RegConnectRegistryA +#define RegConnectRegistryEx RegConnectRegistryExA +#define RegCreateKey RegCreateKeyA +#define RegCreateKeyEx RegCreateKeyExA +#define RegDeleteKey RegDeleteKeyA +#define RegDeleteKeyEx RegDeleteKeyExA +#define RegDeleteValue RegDeleteValueA +#define RegEnumKey RegEnumKeyA +#define RegEnumKeyEx RegEnumKeyExA +#define RegEnumValue RegEnumValueA +#define RegLoadKey RegLoadKeyA +#define RegOpenKey RegOpenKeyA +#define RegOpenKeyEx RegOpenKeyExA +#define RegQueryInfoKey RegQueryInfoKeyA +#define RegQueryValue RegQueryValueA +#define RegQueryMultipleValues RegQueryMultipleValuesA +#define RegQueryValueEx RegQueryValueExA +#define RegReplaceKey RegReplaceKeyA +#define RegRestoreKey RegRestoreKeyA +#define RegSaveKey RegSaveKeyA +#define RegSetValue RegSetValueA +#define RegSetValueEx RegSetValueExA +#define RegUnLoadKey RegUnLoadKeyA +#define RegGetValue RegGetValueA +#define InitiateSystemShutdown InitiateSystemShutdownA +#define AbortSystemShutdown AbortSystemShutdownA +#endif + + WINADVAPI LONG WINAPI RegCloseKey(HKEY hKey); + WINADVAPI LONG WINAPI RegOverridePredefKey(HKEY hKey,HKEY hNewHKey); + WINADVAPI LONG WINAPI RegOpenUserClassesRoot(HANDLE hToken,DWORD dwOptions,REGSAM samDesired,PHKEY phkResult); + WINADVAPI LONG WINAPI RegOpenCurrentUser(REGSAM samDesired,PHKEY phkResult); + WINADVAPI LONG WINAPI RegDisablePredefinedCache(); + WINADVAPI LONG WINAPI RegConnectRegistryA(LPCSTR lpMachineName,HKEY hKey,PHKEY phkResult); + WINADVAPI LONG WINAPI RegConnectRegistryW(LPCWSTR lpMachineName,HKEY hKey,PHKEY phkResult); + WINADVAPI LONG WINAPI RegConnectRegistryExA(LPCSTR lpMachineName,HKEY hKey,ULONG Flags,PHKEY phkResult); + WINADVAPI LONG WINAPI RegConnectRegistryExW(LPCWSTR lpMachineName,HKEY hKey,ULONG Flags,PHKEY phkResult); + WINADVAPI LONG WINAPI RegCreateKeyA(HKEY hKey,LPCSTR lpSubKey,PHKEY phkResult); + WINADVAPI LONG WINAPI RegCreateKeyW(HKEY hKey,LPCWSTR lpSubKey,PHKEY phkResult); + WINADVAPI LONG WINAPI RegCreateKeyExA(HKEY hKey,LPCSTR lpSubKey,DWORD Reserved,LPSTR lpClass,DWORD dwOptions,REGSAM samDesired,LPSECURITY_ATTRIBUTES lpSecurityAttributes,PHKEY phkResult,LPDWORD lpdwDisposition); + WINADVAPI LONG WINAPI RegCreateKeyExW(HKEY hKey,LPCWSTR lpSubKey,DWORD Reserved,LPWSTR lpClass,DWORD dwOptions,REGSAM samDesired,LPSECURITY_ATTRIBUTES lpSecurityAttributes,PHKEY phkResult,LPDWORD lpdwDisposition); + WINADVAPI LONG WINAPI RegDeleteKeyA(HKEY hKey,LPCSTR lpSubKey); + WINADVAPI LONG WINAPI RegDeleteKeyW(HKEY hKey,LPCWSTR lpSubKey); + WINADVAPI LONG WINAPI RegDeleteKeyExA(HKEY hKey,LPCSTR lpSubKey,REGSAM samDesired,DWORD Reserved); + WINADVAPI LONG WINAPI RegDeleteKeyExW(HKEY hKey,LPCWSTR lpSubKey,REGSAM samDesired,DWORD Reserved); + WINADVAPI LONG WINAPI RegDisableReflectionKey(HKEY hBase); + WINADVAPI LONG WINAPI RegEnableReflectionKey(HKEY hBase); + WINADVAPI LONG WINAPI RegQueryReflectionKey(HKEY hBase,WINBOOL *bIsReflectionDisabled); + WINADVAPI LONG WINAPI RegDeleteValueA(HKEY hKey,LPCSTR lpValueName); + WINADVAPI LONG WINAPI RegDeleteValueW(HKEY hKey,LPCWSTR lpValueName); + WINADVAPI LONG WINAPI RegEnumKeyA(HKEY hKey,DWORD dwIndex,LPSTR lpName,DWORD cchName); + WINADVAPI LONG WINAPI RegEnumKeyW(HKEY hKey,DWORD dwIndex,LPWSTR lpName,DWORD cchName); + WINADVAPI LONG WINAPI RegEnumKeyExA(HKEY hKey,DWORD dwIndex,LPSTR lpName,LPDWORD lpcchName,LPDWORD lpReserved,LPSTR lpClass,LPDWORD lpcchClass,PFILETIME lpftLastWriteTime); + WINADVAPI LONG WINAPI RegEnumKeyExW(HKEY hKey,DWORD dwIndex,LPWSTR lpName,LPDWORD lpcchName,LPDWORD lpReserved,LPWSTR lpClass,LPDWORD lpcchClass,PFILETIME lpftLastWriteTime); + WINADVAPI LONG WINAPI RegEnumValueA(HKEY hKey,DWORD dwIndex,LPSTR lpValueName,LPDWORD lpcchValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData); + WINADVAPI LONG WINAPI RegEnumValueW(HKEY hKey,DWORD dwIndex,LPWSTR lpValueName,LPDWORD lpcchValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData); + WINADVAPI LONG WINAPI RegFlushKey(HKEY hKey); + WINADVAPI LONG WINAPI RegGetKeySecurity(HKEY hKey,SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor,LPDWORD lpcbSecurityDescriptor); + WINADVAPI LONG WINAPI RegLoadKeyA(HKEY hKey,LPCSTR lpSubKey,LPCSTR lpFile); + WINADVAPI LONG WINAPI RegLoadKeyW(HKEY hKey,LPCWSTR lpSubKey,LPCWSTR lpFile); + WINADVAPI LONG WINAPI RegNotifyChangeKeyValue(HKEY hKey,WINBOOL bWatchSubtree,DWORD dwNotifyFilter,HANDLE hEvent,WINBOOL fAsynchronous); + WINADVAPI LONG WINAPI RegOpenKeyA(HKEY hKey,LPCSTR lpSubKey,PHKEY phkResult); + WINADVAPI LONG WINAPI RegOpenKeyW(HKEY hKey,LPCWSTR lpSubKey,PHKEY phkResult); + WINADVAPI LONG WINAPI RegOpenKeyExA(HKEY hKey,LPCSTR lpSubKey,DWORD ulOptions,REGSAM samDesired,PHKEY phkResult); + WINADVAPI LONG WINAPI RegOpenKeyExW(HKEY hKey,LPCWSTR lpSubKey,DWORD ulOptions,REGSAM samDesired,PHKEY phkResult); + WINADVAPI LONG WINAPI RegQueryInfoKeyA(HKEY hKey,LPSTR lpClass,LPDWORD lpcchClass,LPDWORD lpReserved,LPDWORD lpcSubKeys,LPDWORD lpcbMaxSubKeyLen,LPDWORD lpcbMaxClassLen,LPDWORD lpcValues,LPDWORD lpcbMaxValueNameLen,LPDWORD lpcbMaxValueLen,LPDWORD lpcbSecurityDescriptor,PFILETIME lpftLastWriteTime); + WINADVAPI LONG WINAPI RegQueryInfoKeyW(HKEY hKey,LPWSTR lpClass,LPDWORD lpcchClass,LPDWORD lpReserved,LPDWORD lpcSubKeys,LPDWORD lpcbMaxSubKeyLen,LPDWORD lpcbMaxClassLen,LPDWORD lpcValues,LPDWORD lpcbMaxValueNameLen,LPDWORD lpcbMaxValueLen,LPDWORD lpcbSecurityDescriptor,PFILETIME lpftLastWriteTime); + WINADVAPI LONG WINAPI RegQueryValueA(HKEY hKey,LPCSTR lpSubKey,LPSTR lpData,PLONG lpcbData); + WINADVAPI LONG WINAPI RegQueryValueW(HKEY hKey,LPCWSTR lpSubKey,LPWSTR lpData,PLONG lpcbData); + WINADVAPI LONG WINAPI RegQueryMultipleValuesA(HKEY hKey,PVALENTA val_list,DWORD num_vals,LPSTR lpValueBuf,LPDWORD ldwTotsize); + WINADVAPI LONG WINAPI RegQueryMultipleValuesW(HKEY hKey,PVALENTW val_list,DWORD num_vals,LPWSTR lpValueBuf,LPDWORD ldwTotsize); + WINADVAPI LONG WINAPI RegQueryValueExA(HKEY hKey,LPCSTR lpValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData); + WINADVAPI LONG WINAPI RegQueryValueExW(HKEY hKey,LPCWSTR lpValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData); + WINADVAPI LONG WINAPI RegReplaceKeyA(HKEY hKey,LPCSTR lpSubKey,LPCSTR lpNewFile,LPCSTR lpOldFile); + WINADVAPI LONG WINAPI RegReplaceKeyW(HKEY hKey,LPCWSTR lpSubKey,LPCWSTR lpNewFile,LPCWSTR lpOldFile); + WINADVAPI LONG WINAPI RegRestoreKeyA(HKEY hKey,LPCSTR lpFile,DWORD dwFlags); + WINADVAPI LONG WINAPI RegRestoreKeyW(HKEY hKey,LPCWSTR lpFile,DWORD dwFlags); + WINADVAPI LONG WINAPI RegSaveKeyA(HKEY hKey,LPCSTR lpFile,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINADVAPI LONG WINAPI RegSaveKeyW(HKEY hKey,LPCWSTR lpFile,LPSECURITY_ATTRIBUTES lpSecurityAttributes); + WINADVAPI LONG WINAPI RegSetKeySecurity(HKEY hKey,SECURITY_INFORMATION SecurityInformation,PSECURITY_DESCRIPTOR pSecurityDescriptor); + WINADVAPI LONG WINAPI RegSetValueA(HKEY hKey,LPCSTR lpSubKey,DWORD dwType,LPCSTR lpData,DWORD cbData); + WINADVAPI LONG WINAPI RegSetValueW(HKEY hKey,LPCWSTR lpSubKey,DWORD dwType,LPCWSTR lpData,DWORD cbData); + WINADVAPI LONG WINAPI RegSetValueExA(HKEY hKey,LPCSTR lpValueName,DWORD Reserved,DWORD dwType,CONST BYTE *lpData,DWORD cbData); + WINADVAPI LONG WINAPI RegSetValueExW(HKEY hKey,LPCWSTR lpValueName,DWORD Reserved,DWORD dwType,CONST BYTE *lpData,DWORD cbData); + WINADVAPI LONG WINAPI RegUnLoadKeyA(HKEY hKey,LPCSTR lpSubKey); + WINADVAPI LONG WINAPI RegUnLoadKeyW(HKEY hKey,LPCWSTR lpSubKey); + WINADVAPI LONG WINAPI RegGetValueA(HKEY hkey,LPCSTR lpSubKey,LPCSTR lpValue,DWORD dwFlags,LPDWORD pdwType,PVOID pvData,LPDWORD pcbData); + WINADVAPI LONG WINAPI RegGetValueW(HKEY hkey,LPCWSTR lpSubKey,LPCWSTR lpValue,DWORD dwFlags,LPDWORD pdwType,PVOID pvData,LPDWORD pcbData); + WINADVAPI WINBOOL WINAPI InitiateSystemShutdownA(LPSTR lpMachineName,LPSTR lpMessage,DWORD dwTimeout,WINBOOL bForceAppsClosed,WINBOOL bRebootAfterShutdown); + WINADVAPI WINBOOL WINAPI InitiateSystemShutdownW(LPWSTR lpMachineName,LPWSTR lpMessage,DWORD dwTimeout,WINBOOL bForceAppsClosed,WINBOOL bRebootAfterShutdown); + WINADVAPI WINBOOL WINAPI AbortSystemShutdownA(LPSTR lpMachineName); + WINADVAPI WINBOOL WINAPI AbortSystemShutdownW(LPWSTR lpMachineName); + +//gr #include + +#define REASON_SWINSTALL SHTDN_REASON_MAJOR_SOFTWARE|SHTDN_REASON_MINOR_INSTALLATION +#define REASON_HWINSTALL SHTDN_REASON_MAJOR_HARDWARE|SHTDN_REASON_MINOR_INSTALLATION +#define REASON_SERVICEHANG SHTDN_REASON_MAJOR_SOFTWARE|SHTDN_REASON_MINOR_HUNG +#define REASON_UNSTABLE SHTDN_REASON_MAJOR_SYSTEM|SHTDN_REASON_MINOR_UNSTABLE +#define REASON_SWHWRECONF SHTDN_REASON_MAJOR_SOFTWARE|SHTDN_REASON_MINOR_RECONFIG +#define REASON_OTHER SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER +#define REASON_UNKNOWN SHTDN_REASON_UNKNOWN +#define REASON_LEGACY_API SHTDN_REASON_LEGACY_API +#define REASON_PLANNED_FLAG SHTDN_REASON_FLAG_PLANNED + +#define MAX_SHUTDOWN_TIMEOUT (10*365*24*60*60) + +#ifdef UNICODE +#define InitiateSystemShutdownEx InitiateSystemShutdownExW +#define RegSaveKeyEx RegSaveKeyExW +#else +#define InitiateSystemShutdownEx InitiateSystemShutdownExA +#define RegSaveKeyEx RegSaveKeyExA +#endif + + WINADVAPI WINBOOL WINAPI InitiateSystemShutdownExA(LPSTR lpMachineName,LPSTR lpMessage,DWORD dwTimeout,WINBOOL bForceAppsClosed,WINBOOL bRebootAfterShutdown,DWORD dwReason); + WINADVAPI WINBOOL WINAPI InitiateSystemShutdownExW(LPWSTR lpMachineName,LPWSTR lpMessage,DWORD dwTimeout,WINBOOL bForceAppsClosed,WINBOOL bRebootAfterShutdown,DWORD dwReason); + WINADVAPI LONG WINAPI RegSaveKeyExA(HKEY hKey,LPCSTR lpFile,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD Flags); + WINADVAPI LONG WINAPI RegSaveKeyExW(HKEY hKey,LPCWSTR lpFile,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD Flags); + WINADVAPI LONG WINAPI Wow64Win32ApiEntry (DWORD dwFuncNumber,DWORD dwFlag,DWORD dwRes); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/winuser.h b/05/tcc-final-old/win32/include/winapi/winuser.h new file mode 100644 index 0000000..4cd9ffb --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/winuser.h @@ -0,0 +1,5651 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _WINUSER_ +#define _WINUSER_ + +#define WINUSERAPI DECLSPEC_IMPORT + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WINVER +#define WINVER 0x0502 +#endif + +#include + +#ifndef NOUSER + typedef HANDLE HDWP; + typedef VOID MENUTEMPLATEA; + typedef VOID MENUTEMPLATEW; + typedef PVOID LPMENUTEMPLATEA; + typedef PVOID LPMENUTEMPLATEW; + +#ifdef UNICODE + typedef MENUTEMPLATEW MENUTEMPLATE; + typedef LPMENUTEMPLATEW LPMENUTEMPLATE; +#else + typedef MENUTEMPLATEA MENUTEMPLATE; + typedef LPMENUTEMPLATEA LPMENUTEMPLATE; +#endif + + typedef LRESULT (CALLBACK *WNDPROC)(HWND,UINT,WPARAM,LPARAM); + typedef INT_PTR (CALLBACK *DLGPROC)(HWND,UINT,WPARAM,LPARAM); + typedef VOID (CALLBACK *TIMERPROC)(HWND,UINT,UINT_PTR,DWORD); + typedef WINBOOL (CALLBACK *GRAYSTRINGPROC)(HDC,LPARAM,int); + typedef WINBOOL (CALLBACK *WNDENUMPROC)(HWND,LPARAM); + typedef LRESULT (CALLBACK *HOOKPROC)(int code,WPARAM wParam,LPARAM lParam); + typedef VOID (CALLBACK *SENDASYNCPROC)(HWND,UINT,ULONG_PTR,LRESULT); + typedef WINBOOL (CALLBACK *PROPENUMPROCA)(HWND,LPCSTR,HANDLE); + typedef WINBOOL (CALLBACK *PROPENUMPROCW)(HWND,LPCWSTR,HANDLE); + typedef WINBOOL (CALLBACK *PROPENUMPROCEXA)(HWND,LPSTR,HANDLE,ULONG_PTR); + typedef WINBOOL (CALLBACK *PROPENUMPROCEXW)(HWND,LPWSTR,HANDLE,ULONG_PTR); + typedef int (CALLBACK *EDITWORDBREAKPROCA)(LPSTR lpch,int ichCurrent,int cch,int code); + typedef int (CALLBACK *EDITWORDBREAKPROCW)(LPWSTR lpch,int ichCurrent,int cch,int code); + typedef WINBOOL (CALLBACK *DRAWSTATEPROC)(HDC hdc,LPARAM lData,WPARAM wData,int cx,int cy); + +#ifdef UNICODE + typedef PROPENUMPROCW PROPENUMPROC; + typedef PROPENUMPROCEXW PROPENUMPROCEX; + typedef EDITWORDBREAKPROCW EDITWORDBREAKPROC; +#else + typedef PROPENUMPROCA PROPENUMPROC; + typedef PROPENUMPROCEXA PROPENUMPROCEX; + typedef EDITWORDBREAKPROCA EDITWORDBREAKPROC; +#endif + + typedef WINBOOL (CALLBACK *NAMEENUMPROCA)(LPSTR,LPARAM); + typedef WINBOOL (CALLBACK *NAMEENUMPROCW)(LPWSTR,LPARAM); + typedef NAMEENUMPROCA WINSTAENUMPROCA; + typedef NAMEENUMPROCA DESKTOPENUMPROCA; + typedef NAMEENUMPROCW WINSTAENUMPROCW; + typedef NAMEENUMPROCW DESKTOPENUMPROCW; + +#ifdef UNICODE + typedef WINSTAENUMPROCW WINSTAENUMPROC; + typedef DESKTOPENUMPROCW DESKTOPENUMPROC; +#else + typedef WINSTAENUMPROCA WINSTAENUMPROC; + typedef DESKTOPENUMPROCA DESKTOPENUMPROC; +#endif + +#define IS_INTRESOURCE(_r) ((((ULONG_PTR)(_r)) >> 16)==0) +#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i)))) +#define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i)))) +#ifdef UNICODE +#define MAKEINTRESOURCE MAKEINTRESOURCEW +#else +#define MAKEINTRESOURCE MAKEINTRESOURCEA +#endif + +#ifndef NORESOURCE + +#define RT_CURSOR MAKEINTRESOURCE(1) +#define RT_BITMAP MAKEINTRESOURCE(2) +#define RT_ICON MAKEINTRESOURCE(3) +#define RT_MENU MAKEINTRESOURCE(4) +#define RT_DIALOG MAKEINTRESOURCE(5) +#define RT_STRING MAKEINTRESOURCE(6) +#define RT_FONTDIR MAKEINTRESOURCE(7) +#define RT_FONT MAKEINTRESOURCE(8) +#define RT_ACCELERATOR MAKEINTRESOURCE(9) +#define RT_RCDATA MAKEINTRESOURCE(10) +#define RT_MESSAGETABLE MAKEINTRESOURCE(11) + +#define DIFFERENCE 11 +#define RT_GROUP_CURSOR MAKEINTRESOURCE((ULONG_PTR)RT_CURSOR + DIFFERENCE) +#define RT_GROUP_ICON MAKEINTRESOURCE((ULONG_PTR)RT_ICON + DIFFERENCE) +#define RT_VERSION MAKEINTRESOURCE(16) +#define RT_DLGINCLUDE MAKEINTRESOURCE(17) +#define RT_PLUGPLAY MAKEINTRESOURCE(19) +#define RT_VXD MAKEINTRESOURCE(20) +#define RT_ANICURSOR MAKEINTRESOURCE(21) +#define RT_ANIICON MAKEINTRESOURCE(22) +#define RT_HTML MAKEINTRESOURCE(23) +#ifdef RC_INVOKED +#define RT_MANIFEST 24 +#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1 +#define ISOLATIONAWARE_MANIFEST_RESOURCE_ID 2 +#define ISOLATIONAWARE_NOSTATICIMPORT_MANIFEST_RESOURCE_ID 3 +#define MINIMUM_RESERVED_MANIFEST_RESOURCE_ID 1 +#define MAXIMUM_RESERVED_MANIFEST_RESOURCE_ID 16 +#else +#define RT_MANIFEST MAKEINTRESOURCE(24) +#define CREATEPROCESS_MANIFEST_RESOURCE_ID MAKEINTRESOURCE(1) +#define ISOLATIONAWARE_MANIFEST_RESOURCE_ID MAKEINTRESOURCE(2) +#define ISOLATIONAWARE_NOSTATICIMPORT_MANIFEST_RESOURCE_ID MAKEINTRESOURCE(3) +#define MINIMUM_RESERVED_MANIFEST_RESOURCE_ID MAKEINTRESOURCE(1) +#define MAXIMUM_RESERVED_MANIFEST_RESOURCE_ID MAKEINTRESOURCE(16) +#endif +#endif + +#ifdef UNICODE +#define wvsprintf wvsprintfW +#define wsprintf wsprintfW +#else +#define wvsprintf wvsprintfA +#define wsprintf wsprintfA +#endif + + WINUSERAPI int WINAPI wvsprintfA(LPSTR,LPCSTR,va_list arglist); + WINUSERAPI int WINAPI wvsprintfW(LPWSTR,LPCWSTR,va_list arglist); + WINUSERAPI int WINAPIV wsprintfA(LPSTR,LPCSTR,...); + WINUSERAPI int WINAPIV wsprintfW(LPWSTR,LPCWSTR,...); + +#define SETWALLPAPER_DEFAULT ((LPWSTR)-1) + +#ifndef NOSCROLL +#define SB_HORZ 0 +#define SB_VERT 1 +#define SB_CTL 2 +#define SB_BOTH 3 + +#define SB_LINEUP 0 +#define SB_LINELEFT 0 +#define SB_LINEDOWN 1 +#define SB_LINERIGHT 1 +#define SB_PAGEUP 2 +#define SB_PAGELEFT 2 +#define SB_PAGEDOWN 3 +#define SB_PAGERIGHT 3 +#define SB_THUMBPOSITION 4 +#define SB_THUMBTRACK 5 +#define SB_TOP 6 +#define SB_LEFT 6 +#define SB_BOTTOM 7 +#define SB_RIGHT 7 +#define SB_ENDSCROLL 8 +#endif + +#ifndef NOSHOWWINDOW +#define SW_HIDE 0 +#define SW_SHOWNORMAL 1 +#define SW_NORMAL 1 +#define SW_SHOWMINIMIZED 2 +#define SW_SHOWMAXIMIZED 3 +#define SW_MAXIMIZE 3 +#define SW_SHOWNOACTIVATE 4 +#define SW_SHOW 5 +#define SW_MINIMIZE 6 +#define SW_SHOWMINNOACTIVE 7 +#define SW_SHOWNA 8 +#define SW_RESTORE 9 +#define SW_SHOWDEFAULT 10 +#define SW_FORCEMINIMIZE 11 +#define SW_MAX 11 + +#define HIDE_WINDOW 0 +#define SHOW_OPENWINDOW 1 +#define SHOW_ICONWINDOW 2 +#define SHOW_FULLSCREEN 3 +#define SHOW_OPENNOACTIVATE 4 + +#define SW_PARENTCLOSING 1 +#define SW_OTHERZOOM 2 +#define SW_PARENTOPENING 3 +#define SW_OTHERUNZOOM 4 +#endif + +#define AW_HOR_POSITIVE 0x00000001 +#define AW_HOR_NEGATIVE 0x00000002 +#define AW_VER_POSITIVE 0x00000004 +#define AW_VER_NEGATIVE 0x00000008 +#define AW_CENTER 0x00000010 +#define AW_HIDE 0x00010000 +#define AW_ACTIVATE 0x00020000 +#define AW_SLIDE 0x00040000 +#define AW_BLEND 0x00080000 + +#define KF_EXTENDED 0x0100 +#define KF_DLGMODE 0x0800 +#define KF_MENUMODE 0x1000 +#define KF_ALTDOWN 0x2000 +#define KF_REPEAT 0x4000 +#define KF_UP 0x8000 + +#ifndef NOVIRTUALKEYCODES + +#define VK_LBUTTON 0x01 +#define VK_RBUTTON 0x02 +#define VK_CANCEL 0x03 +#define VK_MBUTTON 0x04 +#define VK_XBUTTON1 0x05 +#define VK_XBUTTON2 0x06 +#define VK_BACK 0x08 +#define VK_TAB 0x09 +#define VK_CLEAR 0x0C +#define VK_RETURN 0x0D +#define VK_SHIFT 0x10 +#define VK_CONTROL 0x11 +#define VK_MENU 0x12 +#define VK_PAUSE 0x13 +#define VK_CAPITAL 0x14 +#define VK_KANA 0x15 +#define VK_HANGEUL 0x15 +#define VK_HANGUL 0x15 +#define VK_JUNJA 0x17 +#define VK_FINAL 0x18 +#define VK_HANJA 0x19 +#define VK_KANJI 0x19 +#define VK_ESCAPE 0x1B +#define VK_CONVERT 0x1C +#define VK_NONCONVERT 0x1D +#define VK_ACCEPT 0x1E +#define VK_MODECHANGE 0x1F +#define VK_SPACE 0x20 +#define VK_PRIOR 0x21 +#define VK_NEXT 0x22 +#define VK_END 0x23 +#define VK_HOME 0x24 +#define VK_LEFT 0x25 +#define VK_UP 0x26 +#define VK_RIGHT 0x27 +#define VK_DOWN 0x28 +#define VK_SELECT 0x29 +#define VK_PRINT 0x2A +#define VK_EXECUTE 0x2B +#define VK_SNAPSHOT 0x2C +#define VK_INSERT 0x2D +#define VK_DELETE 0x2E +#define VK_HELP 0x2F + +#define VK_LWIN 0x5B +#define VK_RWIN 0x5C +#define VK_APPS 0x5D +#define VK_SLEEP 0x5F +#define VK_NUMPAD0 0x60 +#define VK_NUMPAD1 0x61 +#define VK_NUMPAD2 0x62 +#define VK_NUMPAD3 0x63 +#define VK_NUMPAD4 0x64 +#define VK_NUMPAD5 0x65 +#define VK_NUMPAD6 0x66 +#define VK_NUMPAD7 0x67 +#define VK_NUMPAD8 0x68 +#define VK_NUMPAD9 0x69 +#define VK_MULTIPLY 0x6A +#define VK_ADD 0x6B +#define VK_SEPARATOR 0x6C +#define VK_SUBTRACT 0x6D +#define VK_DECIMAL 0x6E +#define VK_DIVIDE 0x6F +#define VK_F1 0x70 +#define VK_F2 0x71 +#define VK_F3 0x72 +#define VK_F4 0x73 +#define VK_F5 0x74 +#define VK_F6 0x75 +#define VK_F7 0x76 +#define VK_F8 0x77 +#define VK_F9 0x78 +#define VK_F10 0x79 +#define VK_F11 0x7A +#define VK_F12 0x7B +#define VK_F13 0x7C +#define VK_F14 0x7D +#define VK_F15 0x7E +#define VK_F16 0x7F +#define VK_F17 0x80 +#define VK_F18 0x81 +#define VK_F19 0x82 +#define VK_F20 0x83 +#define VK_F21 0x84 +#define VK_F22 0x85 +#define VK_F23 0x86 +#define VK_F24 0x87 +#define VK_NUMLOCK 0x90 +#define VK_SCROLL 0x91 +#define VK_OEM_NEC_EQUAL 0x92 +#define VK_OEM_FJ_JISHO 0x92 +#define VK_OEM_FJ_MASSHOU 0x93 +#define VK_OEM_FJ_TOUROKU 0x94 +#define VK_OEM_FJ_LOYA 0x95 +#define VK_OEM_FJ_ROYA 0x96 +#define VK_LSHIFT 0xA0 +#define VK_RSHIFT 0xA1 +#define VK_LCONTROL 0xA2 +#define VK_RCONTROL 0xA3 +#define VK_LMENU 0xA4 +#define VK_RMENU 0xA5 +#define VK_BROWSER_BACK 0xA6 +#define VK_BROWSER_FORWARD 0xA7 +#define VK_BROWSER_REFRESH 0xA8 +#define VK_BROWSER_STOP 0xA9 +#define VK_BROWSER_SEARCH 0xAA +#define VK_BROWSER_FAVORITES 0xAB +#define VK_BROWSER_HOME 0xAC +#define VK_VOLUME_MUTE 0xAD +#define VK_VOLUME_DOWN 0xAE +#define VK_VOLUME_UP 0xAF +#define VK_MEDIA_NEXT_TRACK 0xB0 +#define VK_MEDIA_PREV_TRACK 0xB1 +#define VK_MEDIA_STOP 0xB2 +#define VK_MEDIA_PLAY_PAUSE 0xB3 +#define VK_LAUNCH_MAIL 0xB4 +#define VK_LAUNCH_MEDIA_SELECT 0xB5 +#define VK_LAUNCH_APP1 0xB6 +#define VK_LAUNCH_APP2 0xB7 +#define VK_OEM_1 0xBA +#define VK_OEM_PLUS 0xBB +#define VK_OEM_COMMA 0xBC +#define VK_OEM_MINUS 0xBD +#define VK_OEM_PERIOD 0xBE +#define VK_OEM_2 0xBF +#define VK_OEM_3 0xC0 +#define VK_OEM_4 0xDB +#define VK_OEM_5 0xDC +#define VK_OEM_6 0xDD +#define VK_OEM_7 0xDE +#define VK_OEM_8 0xDF +#define VK_OEM_AX 0xE1 +#define VK_OEM_102 0xE2 +#define VK_ICO_HELP 0xE3 +#define VK_ICO_00 0xE4 +#define VK_PROCESSKEY 0xE5 +#define VK_ICO_CLEAR 0xE6 +#define VK_PACKET 0xE7 +#define VK_OEM_RESET 0xE9 +#define VK_OEM_JUMP 0xEA +#define VK_OEM_PA1 0xEB +#define VK_OEM_PA2 0xEC +#define VK_OEM_PA3 0xED +#define VK_OEM_WSCTRL 0xEE +#define VK_OEM_CUSEL 0xEF +#define VK_OEM_ATTN 0xF0 +#define VK_OEM_FINISH 0xF1 +#define VK_OEM_COPY 0xF2 +#define VK_OEM_AUTO 0xF3 +#define VK_OEM_ENLW 0xF4 +#define VK_OEM_BACKTAB 0xF5 +#define VK_ATTN 0xF6 +#define VK_CRSEL 0xF7 +#define VK_EXSEL 0xF8 +#define VK_EREOF 0xF9 +#define VK_PLAY 0xFA +#define VK_ZOOM 0xFB +#define VK_NONAME 0xFC +#define VK_PA1 0xFD +#define VK_OEM_CLEAR 0xFE +#endif + +#ifndef NOWH + +#define WH_MIN (-1) +#define WH_MSGFILTER (-1) +#define WH_JOURNALRECORD 0 +#define WH_JOURNALPLAYBACK 1 +#define WH_KEYBOARD 2 +#define WH_GETMESSAGE 3 +#define WH_CALLWNDPROC 4 +#define WH_CBT 5 +#define WH_SYSMSGFILTER 6 +#define WH_MOUSE 7 +#define WH_HARDWARE 8 +#define WH_DEBUG 9 +#define WH_SHELL 10 +#define WH_FOREGROUNDIDLE 11 +#define WH_CALLWNDPROCRET 12 + +#define WH_KEYBOARD_LL 13 +#define WH_MOUSE_LL 14 + +#define WH_MAX 14 + +#define WH_MINHOOK WH_MIN +#define WH_MAXHOOK WH_MAX + +#define HC_ACTION 0 +#define HC_GETNEXT 1 +#define HC_SKIP 2 +#define HC_NOREMOVE 3 +#define HC_NOREM HC_NOREMOVE +#define HC_SYSMODALON 4 +#define HC_SYSMODALOFF 5 + +#define HCBT_MOVESIZE 0 +#define HCBT_MINMAX 1 +#define HCBT_QS 2 +#define HCBT_CREATEWND 3 +#define HCBT_DESTROYWND 4 +#define HCBT_ACTIVATE 5 +#define HCBT_CLICKSKIPPED 6 +#define HCBT_KEYSKIPPED 7 +#define HCBT_SYSCOMMAND 8 +#define HCBT_SETFOCUS 9 + + typedef struct tagCBT_CREATEWNDA { + struct tagCREATESTRUCTA *lpcs; + HWND hwndInsertAfter; + } CBT_CREATEWNDA,*LPCBT_CREATEWNDA; + + typedef struct tagCBT_CREATEWNDW { + struct tagCREATESTRUCTW *lpcs; + HWND hwndInsertAfter; + } CBT_CREATEWNDW,*LPCBT_CREATEWNDW; +#ifdef UNICODE + typedef CBT_CREATEWNDW CBT_CREATEWND; + typedef LPCBT_CREATEWNDW LPCBT_CREATEWND; +#else + typedef CBT_CREATEWNDA CBT_CREATEWND; + typedef LPCBT_CREATEWNDA LPCBT_CREATEWND; +#endif + + typedef struct tagCBTACTIVATESTRUCT + { + WINBOOL fMouse; + HWND hWndActive; + } CBTACTIVATESTRUCT,*LPCBTACTIVATESTRUCT; + + typedef struct tagWTSSESSION_NOTIFICATION { + DWORD cbSize; + DWORD dwSessionId; + + } WTSSESSION_NOTIFICATION,*PWTSSESSION_NOTIFICATION; + +#define WTS_CONSOLE_CONNECT 0x1 +#define WTS_CONSOLE_DISCONNECT 0x2 +#define WTS_REMOTE_CONNECT 0x3 +#define WTS_REMOTE_DISCONNECT 0x4 +#define WTS_SESSION_LOGON 0x5 +#define WTS_SESSION_LOGOFF 0x6 +#define WTS_SESSION_LOCK 0x7 +#define WTS_SESSION_UNLOCK 0x8 +#define WTS_SESSION_REMOTE_CONTROL 0x9 + +#define MSGF_DIALOGBOX 0 +#define MSGF_MESSAGEBOX 1 +#define MSGF_MENU 2 +#define MSGF_SCROLLBAR 5 +#define MSGF_NEXTWINDOW 6 +#define MSGF_MAX 8 +#define MSGF_USER 4096 + +#define HSHELL_WINDOWCREATED 1 +#define HSHELL_WINDOWDESTROYED 2 +#define HSHELL_ACTIVATESHELLWINDOW 3 + +#define HSHELL_WINDOWACTIVATED 4 +#define HSHELL_GETMINRECT 5 +#define HSHELL_REDRAW 6 +#define HSHELL_TASKMAN 7 +#define HSHELL_LANGUAGE 8 +#define HSHELL_SYSMENU 9 +#define HSHELL_ENDTASK 10 +#define HSHELL_ACCESSIBILITYSTATE 11 +#define HSHELL_APPCOMMAND 12 +#define HSHELL_WINDOWREPLACED 13 +#define HSHELL_WINDOWREPLACING 14 +#define HSHELL_HIGHBIT 0x8000 +#define HSHELL_FLASH (HSHELL_REDRAW|HSHELL_HIGHBIT) +#define HSHELL_RUDEAPPACTIVATED (HSHELL_WINDOWACTIVATED|HSHELL_HIGHBIT) + +#define ACCESS_STICKYKEYS 0x0001 +#define ACCESS_FILTERKEYS 0x0002 +#define ACCESS_MOUSEKEYS 0x0003 + +#define APPCOMMAND_BROWSER_BACKWARD 1 +#define APPCOMMAND_BROWSER_FORWARD 2 +#define APPCOMMAND_BROWSER_REFRESH 3 +#define APPCOMMAND_BROWSER_STOP 4 +#define APPCOMMAND_BROWSER_SEARCH 5 +#define APPCOMMAND_BROWSER_FAVORITES 6 +#define APPCOMMAND_BROWSER_HOME 7 +#define APPCOMMAND_VOLUME_MUTE 8 +#define APPCOMMAND_VOLUME_DOWN 9 +#define APPCOMMAND_VOLUME_UP 10 +#define APPCOMMAND_MEDIA_NEXTTRACK 11 +#define APPCOMMAND_MEDIA_PREVIOUSTRACK 12 +#define APPCOMMAND_MEDIA_STOP 13 +#define APPCOMMAND_MEDIA_PLAY_PAUSE 14 +#define APPCOMMAND_LAUNCH_MAIL 15 +#define APPCOMMAND_LAUNCH_MEDIA_SELECT 16 +#define APPCOMMAND_LAUNCH_APP1 17 +#define APPCOMMAND_LAUNCH_APP2 18 +#define APPCOMMAND_BASS_DOWN 19 +#define APPCOMMAND_BASS_BOOST 20 +#define APPCOMMAND_BASS_UP 21 +#define APPCOMMAND_TREBLE_DOWN 22 +#define APPCOMMAND_TREBLE_UP 23 +#define APPCOMMAND_MICROPHONE_VOLUME_MUTE 24 +#define APPCOMMAND_MICROPHONE_VOLUME_DOWN 25 +#define APPCOMMAND_MICROPHONE_VOLUME_UP 26 +#define APPCOMMAND_HELP 27 +#define APPCOMMAND_FIND 28 +#define APPCOMMAND_NEW 29 +#define APPCOMMAND_OPEN 30 +#define APPCOMMAND_CLOSE 31 +#define APPCOMMAND_SAVE 32 +#define APPCOMMAND_PRINT 33 +#define APPCOMMAND_UNDO 34 +#define APPCOMMAND_REDO 35 +#define APPCOMMAND_COPY 36 +#define APPCOMMAND_CUT 37 +#define APPCOMMAND_PASTE 38 +#define APPCOMMAND_REPLY_TO_MAIL 39 +#define APPCOMMAND_FORWARD_MAIL 40 +#define APPCOMMAND_SEND_MAIL 41 +#define APPCOMMAND_SPELL_CHECK 42 +#define APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE 43 +#define APPCOMMAND_MIC_ON_OFF_TOGGLE 44 +#define APPCOMMAND_CORRECTION_LIST 45 +#define APPCOMMAND_MEDIA_PLAY 46 +#define APPCOMMAND_MEDIA_PAUSE 47 +#define APPCOMMAND_MEDIA_RECORD 48 +#define APPCOMMAND_MEDIA_FAST_FORWARD 49 +#define APPCOMMAND_MEDIA_REWIND 50 +#define APPCOMMAND_MEDIA_CHANNEL_UP 51 +#define APPCOMMAND_MEDIA_CHANNEL_DOWN 52 + +#define FAPPCOMMAND_MOUSE 0x8000 +#define FAPPCOMMAND_KEY 0 +#define FAPPCOMMAND_OEM 0x1000 +#define FAPPCOMMAND_MASK 0xF000 + +#define GET_APPCOMMAND_LPARAM(lParam) ((short)(HIWORD(lParam) & ~FAPPCOMMAND_MASK)) +#define GET_DEVICE_LPARAM(lParam) ((WORD)(HIWORD(lParam) & FAPPCOMMAND_MASK)) +#define GET_MOUSEORKEY_LPARAM GET_DEVICE_LPARAM +#define GET_FLAGS_LPARAM(lParam) (LOWORD(lParam)) +#define GET_KEYSTATE_LPARAM(lParam) GET_FLAGS_LPARAM(lParam) + + typedef struct { + HWND hwnd; + RECT rc; + } SHELLHOOKINFO,*LPSHELLHOOKINFO; + + typedef struct tagEVENTMSG { + UINT message; + UINT paramL; + UINT paramH; + DWORD time; + HWND hwnd; + } EVENTMSG,*PEVENTMSGMSG,*NPEVENTMSGMSG,*LPEVENTMSGMSG; + + typedef struct tagEVENTMSG *PEVENTMSG,*NPEVENTMSG,*LPEVENTMSG; + + typedef struct tagCWPSTRUCT { + LPARAM lParam; + WPARAM wParam; + UINT message; + HWND hwnd; + } CWPSTRUCT,*PCWPSTRUCT,*NPCWPSTRUCT,*LPCWPSTRUCT; + + typedef struct tagCWPRETSTRUCT { + LRESULT lResult; + LPARAM lParam; + WPARAM wParam; + UINT message; + HWND hwnd; + } CWPRETSTRUCT,*PCWPRETSTRUCT,*NPCWPRETSTRUCT,*LPCWPRETSTRUCT; + +#define LLKHF_EXTENDED (KF_EXTENDED >> 8) +#define LLKHF_INJECTED 0x00000010 +#define LLKHF_ALTDOWN (KF_ALTDOWN >> 8) +#define LLKHF_UP (KF_UP >> 8) + +#define LLMHF_INJECTED 0x00000001 + + typedef struct tagKBDLLHOOKSTRUCT { + DWORD vkCode; + DWORD scanCode; + DWORD flags; + DWORD time; + ULONG_PTR dwExtraInfo; + } KBDLLHOOKSTRUCT,*LPKBDLLHOOKSTRUCT,*PKBDLLHOOKSTRUCT; + + typedef struct tagMSLLHOOKSTRUCT { + POINT pt; + DWORD mouseData; + DWORD flags; + DWORD time; + ULONG_PTR dwExtraInfo; + } MSLLHOOKSTRUCT,*LPMSLLHOOKSTRUCT,*PMSLLHOOKSTRUCT; + + typedef struct tagDEBUGHOOKINFO { + DWORD idThread; + DWORD idThreadInstaller; + LPARAM lParam; + WPARAM wParam; + int code; + } DEBUGHOOKINFO,*PDEBUGHOOKINFO,*NPDEBUGHOOKINFO,*LPDEBUGHOOKINFO; + + typedef struct tagMOUSEHOOKSTRUCT { + POINT pt; + HWND hwnd; + UINT wHitTestCode; + ULONG_PTR dwExtraInfo; + } MOUSEHOOKSTRUCT,*LPMOUSEHOOKSTRUCT,*PMOUSEHOOKSTRUCT; + +#ifdef __cplusplus + typedef struct tagMOUSEHOOKSTRUCTEX : public tagMOUSEHOOKSTRUCT { + DWORD mouseData; + } MOUSEHOOKSTRUCTEX,*LPMOUSEHOOKSTRUCTEX,*PMOUSEHOOKSTRUCTEX; +#else + typedef struct tagMOUSEHOOKSTRUCTEX { + MOUSEHOOKSTRUCT _unnamed; + DWORD mouseData; + } MOUSEHOOKSTRUCTEX,*LPMOUSEHOOKSTRUCTEX,*PMOUSEHOOKSTRUCTEX; +#endif + + typedef struct tagHARDWAREHOOKSTRUCT { + HWND hwnd; + UINT message; + WPARAM wParam; + LPARAM lParam; + } HARDWAREHOOKSTRUCT,*LPHARDWAREHOOKSTRUCT,*PHARDWAREHOOKSTRUCT; +#endif + +#define HKL_PREV 0 +#define HKL_NEXT 1 + +#define KLF_ACTIVATE 0x00000001 +#define KLF_SUBSTITUTE_OK 0x00000002 +#define KLF_REORDER 0x00000008 +#define KLF_REPLACELANG 0x00000010 +#define KLF_NOTELLSHELL 0x00000080 +#define KLF_SETFORPROCESS 0x00000100 +#define KLF_SHIFTLOCK 0x00010000 +#define KLF_RESET 0x40000000 + +#define INPUTLANGCHANGE_SYSCHARSET 0x0001 +#define INPUTLANGCHANGE_FORWARD 0x0002 +#define INPUTLANGCHANGE_BACKWARD 0x0004 + +#define KL_NAMELENGTH 9 + +#ifdef UNICODE +#define LoadKeyboardLayout LoadKeyboardLayoutW +#define GetKeyboardLayoutName GetKeyboardLayoutNameW +#else +#define LoadKeyboardLayout LoadKeyboardLayoutA +#define GetKeyboardLayoutName GetKeyboardLayoutNameA +#endif + + WINUSERAPI HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID,UINT Flags); + WINUSERAPI HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID,UINT Flags); + WINUSERAPI HKL WINAPI ActivateKeyboardLayout(HKL hkl,UINT Flags); + WINUSERAPI int WINAPI ToUnicodeEx(UINT wVirtKey,UINT wScanCode,CONST BYTE *lpKeyState,LPWSTR pwszBuff,int cchBuff,UINT wFlags,HKL dwhkl); + WINUSERAPI WINBOOL WINAPI UnloadKeyboardLayout(HKL hkl); + WINUSERAPI WINBOOL WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID); + WINUSERAPI WINBOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID); + WINUSERAPI int WINAPI GetKeyboardLayoutList(int nBuff,HKL *lpList); + WINUSERAPI HKL WINAPI GetKeyboardLayout(DWORD idThread); + + typedef struct tagMOUSEMOVEPOINT { + int x; + int y; + DWORD time; + ULONG_PTR dwExtraInfo; + } MOUSEMOVEPOINT,*PMOUSEMOVEPOINT,*LPMOUSEMOVEPOINT; + +#define GMMP_USE_DISPLAY_POINTS 1 +#define GMMP_USE_HIGH_RESOLUTION_POINTS 2 + + WINUSERAPI int WINAPI GetMouseMovePointsEx(UINT cbSize,LPMOUSEMOVEPOINT lppt,LPMOUSEMOVEPOINT lpptBuf,int nBufPoints,DWORD resolution); + +#ifndef NODESKTOP + +#define DESKTOP_READOBJECTS 0x0001L +#define DESKTOP_CREATEWINDOW 0x0002L +#define DESKTOP_CREATEMENU 0x0004L +#define DESKTOP_HOOKCONTROL 0x0008L +#define DESKTOP_JOURNALRECORD 0x0010L +#define DESKTOP_JOURNALPLAYBACK 0x0020L +#define DESKTOP_ENUMERATE 0x0040L +#define DESKTOP_WRITEOBJECTS 0x0080L +#define DESKTOP_SWITCHDESKTOP 0x0100L + +#define DF_ALLOWOTHERACCOUNTHOOK 0x0001L + +#ifdef _WINGDI_ +#ifndef NOGDI +#ifdef UNICODE +#define CreateDesktop CreateDesktopW +#else +#define CreateDesktop CreateDesktopA +#endif + + WINUSERAPI HDESK WINAPI CreateDesktopA(LPCSTR lpszDesktop,LPCSTR lpszDevice,LPDEVMODEA pDevmode,DWORD dwFlags,ACCESS_MASK dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa); + WINUSERAPI HDESK WINAPI CreateDesktopW(LPCWSTR lpszDesktop,LPCWSTR lpszDevice,LPDEVMODEW pDevmode,DWORD dwFlags,ACCESS_MASK dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa); +#endif +#endif + +#ifdef UNICODE +#define OpenDesktop OpenDesktopW +#define EnumDesktops EnumDesktopsW +#else +#define OpenDesktop OpenDesktopA +#define EnumDesktops EnumDesktopsA +#endif + + WINUSERAPI HDESK WINAPI OpenDesktopA(LPCSTR lpszDesktop,DWORD dwFlags,WINBOOL fInherit,ACCESS_MASK dwDesiredAccess); + WINUSERAPI HDESK WINAPI OpenDesktopW(LPCWSTR lpszDesktop,DWORD dwFlags,WINBOOL fInherit,ACCESS_MASK dwDesiredAccess); + WINUSERAPI HDESK WINAPI OpenInputDesktop(DWORD dwFlags,WINBOOL fInherit,ACCESS_MASK dwDesiredAccess); + WINUSERAPI WINBOOL WINAPI EnumDesktopsA(HWINSTA hwinsta,DESKTOPENUMPROCA lpEnumFunc,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI EnumDesktopsW(HWINSTA hwinsta,DESKTOPENUMPROCW lpEnumFunc,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI EnumDesktopWindows(HDESK hDesktop,WNDENUMPROC lpfn,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI SwitchDesktop(HDESK hDesktop); + WINUSERAPI WINBOOL WINAPI SetThreadDesktop(HDESK hDesktop); + WINUSERAPI WINBOOL WINAPI CloseDesktop(HDESK hDesktop); + WINUSERAPI HDESK WINAPI GetThreadDesktop(DWORD dwThreadId); +#endif + +#ifndef NOWINDOWSTATION +#define WINSTA_ENUMDESKTOPS 0x0001L +#define WINSTA_READATTRIBUTES 0x0002L +#define WINSTA_ACCESSCLIPBOARD 0x0004L +#define WINSTA_CREATEDESKTOP 0x0008L +#define WINSTA_WRITEATTRIBUTES 0x0010L +#define WINSTA_ACCESSGLOBALATOMS 0x0020L +#define WINSTA_EXITWINDOWS 0x0040L +#define WINSTA_ENUMERATE 0x0100L +#define WINSTA_READSCREEN 0x0200L +#define WINSTA_ALL_ACCESS (WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES | WINSTA_ACCESSCLIPBOARD | WINSTA_CREATEDESKTOP | WINSTA_WRITEATTRIBUTES | WINSTA_ACCESSGLOBALATOMS | WINSTA_EXITWINDOWS | WINSTA_ENUMERATE | WINSTA_READSCREEN) + +#define CWF_CREATE_ONLY 0x0001L + +#define WSF_VISIBLE 0x0001L + +#ifdef UNICODE +#define CreateWindowStation CreateWindowStationW +#define OpenWindowStation OpenWindowStationW +#define EnumWindowStations EnumWindowStationsW +#else +#define CreateWindowStation CreateWindowStationA +#define OpenWindowStation OpenWindowStationA +#define EnumWindowStations EnumWindowStationsA +#endif + + WINUSERAPI HWINSTA WINAPI CreateWindowStationA(LPCSTR lpwinsta,DWORD dwFlags,ACCESS_MASK dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa); + WINUSERAPI HWINSTA WINAPI CreateWindowStationW(LPCWSTR lpwinsta,DWORD dwFlags,ACCESS_MASK dwDesiredAccess,LPSECURITY_ATTRIBUTES lpsa); + WINUSERAPI HWINSTA WINAPI OpenWindowStationA(LPCSTR lpszWinSta,WINBOOL fInherit,ACCESS_MASK dwDesiredAccess); + WINUSERAPI HWINSTA WINAPI OpenWindowStationW(LPCWSTR lpszWinSta,WINBOOL fInherit,ACCESS_MASK dwDesiredAccess); + WINUSERAPI WINBOOL WINAPI EnumWindowStationsA(WINSTAENUMPROCA lpEnumFunc,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI EnumWindowStationsW(WINSTAENUMPROCW lpEnumFunc,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI CloseWindowStation(HWINSTA hWinSta); + WINUSERAPI WINBOOL WINAPI SetProcessWindowStation(HWINSTA hWinSta); + WINUSERAPI HWINSTA WINAPI GetProcessWindowStation(VOID); +#endif + +#ifndef NOSECURITY + WINUSERAPI WINBOOL WINAPI SetUserObjectSecurity(HANDLE hObj,PSECURITY_INFORMATION pSIRequested,PSECURITY_DESCRIPTOR pSID); + WINUSERAPI WINBOOL WINAPI GetUserObjectSecurity(HANDLE hObj,PSECURITY_INFORMATION pSIRequested,PSECURITY_DESCRIPTOR pSID,DWORD nLength,LPDWORD lpnLengthNeeded); + +#define UOI_FLAGS 1 +#define UOI_NAME 2 +#define UOI_TYPE 3 +#define UOI_USER_SID 4 + + typedef struct tagUSEROBJECTFLAGS { + WINBOOL fInherit; + WINBOOL fReserved; + DWORD dwFlags; + } USEROBJECTFLAGS,*PUSEROBJECTFLAGS; + +#ifdef UNICODE +#define GetUserObjectInformation GetUserObjectInformationW +#define SetUserObjectInformation SetUserObjectInformationW +#else +#define GetUserObjectInformation GetUserObjectInformationA +#define SetUserObjectInformation SetUserObjectInformationA +#endif + + WINUSERAPI WINBOOL WINAPI GetUserObjectInformationA(HANDLE hObj,int nIndex,PVOID pvInfo,DWORD nLength,LPDWORD lpnLengthNeeded); + WINUSERAPI WINBOOL WINAPI GetUserObjectInformationW(HANDLE hObj,int nIndex,PVOID pvInfo,DWORD nLength,LPDWORD lpnLengthNeeded); + WINUSERAPI WINBOOL WINAPI SetUserObjectInformationA(HANDLE hObj,int nIndex,PVOID pvInfo,DWORD nLength); + WINUSERAPI WINBOOL WINAPI SetUserObjectInformationW(HANDLE hObj,int nIndex,PVOID pvInfo,DWORD nLength); +#endif + + typedef struct tagWNDCLASSEXA { + UINT cbSize; + UINT style; + WNDPROC lpfnWndProc; + int cbClsExtra; + int cbWndExtra; + HINSTANCE hInstance; + HICON hIcon; + HCURSOR hCursor; + HBRUSH hbrBackground; + LPCSTR lpszMenuName; + LPCSTR lpszClassName; + HICON hIconSm; + } WNDCLASSEXA,*PWNDCLASSEXA,*NPWNDCLASSEXA,*LPWNDCLASSEXA; + + typedef struct tagWNDCLASSEXW { + UINT cbSize; + UINT style; + WNDPROC lpfnWndProc; + int cbClsExtra; + int cbWndExtra; + HINSTANCE hInstance; + HICON hIcon; + HCURSOR hCursor; + HBRUSH hbrBackground; + LPCWSTR lpszMenuName; + LPCWSTR lpszClassName; + + HICON hIconSm; + } WNDCLASSEXW,*PWNDCLASSEXW,*NPWNDCLASSEXW,*LPWNDCLASSEXW; + +#ifdef UNICODE + typedef WNDCLASSEXW WNDCLASSEX; + typedef PWNDCLASSEXW PWNDCLASSEX; + typedef NPWNDCLASSEXW NPWNDCLASSEX; + typedef LPWNDCLASSEXW LPWNDCLASSEX; +#else + typedef WNDCLASSEXA WNDCLASSEX; + typedef PWNDCLASSEXA PWNDCLASSEX; + typedef NPWNDCLASSEXA NPWNDCLASSEX; + typedef LPWNDCLASSEXA LPWNDCLASSEX; +#endif + + typedef struct tagWNDCLASSA { + UINT style; + WNDPROC lpfnWndProc; + int cbClsExtra; + int cbWndExtra; + HINSTANCE hInstance; + HICON hIcon; + HCURSOR hCursor; + HBRUSH hbrBackground; + LPCSTR lpszMenuName; + LPCSTR lpszClassName; + } WNDCLASSA,*PWNDCLASSA,*NPWNDCLASSA,*LPWNDCLASSA; + + typedef struct tagWNDCLASSW { + UINT style; + WNDPROC lpfnWndProc; + int cbClsExtra; + int cbWndExtra; + HINSTANCE hInstance; + HICON hIcon; + HCURSOR hCursor; + HBRUSH hbrBackground; + LPCWSTR lpszMenuName; + LPCWSTR lpszClassName; + } WNDCLASSW,*PWNDCLASSW,*NPWNDCLASSW,*LPWNDCLASSW; + +#ifdef UNICODE + typedef WNDCLASSW WNDCLASS; + typedef PWNDCLASSW PWNDCLASS; + typedef NPWNDCLASSW NPWNDCLASS; + typedef LPWNDCLASSW LPWNDCLASS; +#else + typedef WNDCLASSA WNDCLASS; + typedef PWNDCLASSA PWNDCLASS; + typedef NPWNDCLASSA NPWNDCLASS; + typedef LPWNDCLASSA LPWNDCLASS; +#endif + + WINUSERAPI WINBOOL WINAPI IsHungAppWindow(HWND hwnd); + WINUSERAPI VOID WINAPI DisableProcessWindowsGhosting(VOID); + +#ifndef NOMSG + typedef struct tagMSG { + HWND hwnd; + UINT message; + WPARAM wParam; + LPARAM lParam; + DWORD time; + POINT pt; + } MSG,*PMSG,*NPMSG,*LPMSG; + +#define POINTSTOPOINT(pt,pts) { (pt).x = (LONG)(SHORT)LOWORD(*(LONG*)&pts); (pt).y = (LONG)(SHORT)HIWORD(*(LONG*)&pts); } + +#define POINTTOPOINTS(pt) (MAKELONG((short)((pt).x),(short)((pt).y))) +#define MAKEWPARAM(l,h) ((WPARAM)(DWORD)MAKELONG(l,h)) +#define MAKELPARAM(l,h) ((LPARAM)(DWORD)MAKELONG(l,h)) +#define MAKELRESULT(l,h) ((LRESULT)(DWORD)MAKELONG(l,h)) +#endif + +#ifndef NOWINOFFSETS +#define GWL_WNDPROC (-4) +#define GWL_HINSTANCE (-6) +#define GWL_HWNDPARENT (-8) +#define GWL_STYLE (-16) +#define GWL_EXSTYLE (-20) +#define GWL_USERDATA (-21) +#define GWL_ID (-12) + +#ifdef _WIN64 +#undef GWL_WNDPROC +#undef GWL_HINSTANCE +#undef GWL_HWNDPARENT +#undef GWL_USERDATA +#endif + +#define GWLP_WNDPROC (-4) +#define GWLP_HINSTANCE (-6) +#define GWLP_HWNDPARENT (-8) +#define GWLP_USERDATA (-21) +#define GWLP_ID (-12) + +#define GCL_MENUNAME (-8) +#define GCL_HBRBACKGROUND (-10) +#define GCL_HCURSOR (-12) +#define GCL_HICON (-14) +#define GCL_HMODULE (-16) +#define GCL_CBWNDEXTRA (-18) +#define GCL_CBCLSEXTRA (-20) +#define GCL_WNDPROC (-24) +#define GCL_STYLE (-26) +#define GCW_ATOM (-32) +#define GCL_HICONSM (-34) + +#ifdef _WIN64 + +#undef GCL_MENUNAME +#undef GCL_HBRBACKGROUND +#undef GCL_HCURSOR +#undef GCL_HICON +#undef GCL_HMODULE +#undef GCL_WNDPROC +#undef GCL_HICONSM +#endif + +#define GCLP_MENUNAME (-8) +#define GCLP_HBRBACKGROUND (-10) +#define GCLP_HCURSOR (-12) +#define GCLP_HICON (-14) +#define GCLP_HMODULE (-16) +#define GCLP_WNDPROC (-24) +#define GCLP_HICONSM (-34) +#endif + +#ifndef NOWINMESSAGES + +#define WM_NULL 0x0000 +#define WM_CREATE 0x0001 +#define WM_DESTROY 0x0002 +#define WM_MOVE 0x0003 +#define WM_SIZE 0x0005 + +#define WM_ACTIVATE 0x0006 + +#define WA_INACTIVE 0 +#define WA_ACTIVE 1 +#define WA_CLICKACTIVE 2 + +#define WM_SETFOCUS 0x0007 +#define WM_KILLFOCUS 0x0008 +#define WM_ENABLE 0x000A +#define WM_SETREDRAW 0x000B +#define WM_SETTEXT 0x000C +#define WM_GETTEXT 0x000D +#define WM_GETTEXTLENGTH 0x000E +#define WM_PAINT 0x000F +#define WM_CLOSE 0x0010 +#ifndef _WIN32_WCE +#define WM_QUERYENDSESSION 0x0011 +#define WM_QUERYOPEN 0x0013 +#define WM_ENDSESSION 0x0016 +#endif +#define WM_QUIT 0x0012 +#define WM_ERASEBKGND 0x0014 +#define WM_SYSCOLORCHANGE 0x0015 +#define WM_SHOWWINDOW 0x0018 +#define WM_WININICHANGE 0x001A +#define WM_SETTINGCHANGE WM_WININICHANGE +#define WM_DEVMODECHANGE 0x001B +#define WM_ACTIVATEAPP 0x001C +#define WM_FONTCHANGE 0x001D +#define WM_TIMECHANGE 0x001E +#define WM_CANCELMODE 0x001F +#define WM_SETCURSOR 0x0020 +#define WM_MOUSEACTIVATE 0x0021 +#define WM_CHILDACTIVATE 0x0022 +#define WM_QUEUESYNC 0x0023 + +#define WM_GETMINMAXINFO 0x0024 + + typedef struct tagMINMAXINFO { + POINT ptReserved; + POINT ptMaxSize; + POINT ptMaxPosition; + POINT ptMinTrackSize; + POINT ptMaxTrackSize; + } MINMAXINFO,*PMINMAXINFO,*LPMINMAXINFO; + +#define WM_PAINTICON 0x0026 +#define WM_ICONERASEBKGND 0x0027 +#define WM_NEXTDLGCTL 0x0028 +#define WM_SPOOLERSTATUS 0x002A +#define WM_DRAWITEM 0x002B +#define WM_MEASUREITEM 0x002C +#define WM_DELETEITEM 0x002D +#define WM_VKEYTOITEM 0x002E +#define WM_CHARTOITEM 0x002F +#define WM_SETFONT 0x0030 +#define WM_GETFONT 0x0031 +#define WM_SETHOTKEY 0x0032 +#define WM_GETHOTKEY 0x0033 +#define WM_QUERYDRAGICON 0x0037 +#define WM_COMPAREITEM 0x0039 +#ifndef _WIN32_WCE +#define WM_GETOBJECT 0x003D +#endif +#define WM_COMPACTING 0x0041 +#define WM_COMMNOTIFY 0x0044 +#define WM_WINDOWPOSCHANGING 0x0046 +#define WM_WINDOWPOSCHANGED 0x0047 + +#define WM_POWER 0x0048 + +#define PWR_OK 1 +#define PWR_FAIL (-1) +#define PWR_SUSPENDREQUEST 1 +#define PWR_SUSPENDRESUME 2 +#define PWR_CRITICALRESUME 3 + +#define WM_COPYDATA 0x004A +#define WM_CANCELJOURNAL 0x004B + + typedef struct tagCOPYDATASTRUCT { + ULONG_PTR dwData; + DWORD cbData; + PVOID lpData; + } COPYDATASTRUCT,*PCOPYDATASTRUCT; + + typedef struct tagMDINEXTMENU { + HMENU hmenuIn; + HMENU hmenuNext; + HWND hwndNext; + } MDINEXTMENU,*PMDINEXTMENU,*LPMDINEXTMENU; + +#define WM_NOTIFY 0x004E +#define WM_INPUTLANGCHANGEREQUEST 0x0050 +#define WM_INPUTLANGCHANGE 0x0051 +#define WM_TCARD 0x0052 +#define WM_HELP 0x0053 +#define WM_USERCHANGED 0x0054 +#define WM_NOTIFYFORMAT 0x0055 + +#define NFR_ANSI 1 +#define NFR_UNICODE 2 +#define NF_QUERY 3 +#define NF_REQUERY 4 + +#define WM_CONTEXTMENU 0x007B +#define WM_STYLECHANGING 0x007C +#define WM_STYLECHANGED 0x007D +#define WM_DISPLAYCHANGE 0x007E +#define WM_GETICON 0x007F +#define WM_SETICON 0x0080 + +#define WM_NCCREATE 0x0081 +#define WM_NCDESTROY 0x0082 +#define WM_NCCALCSIZE 0x0083 +#define WM_NCHITTEST 0x0084 +#define WM_NCPAINT 0x0085 +#define WM_NCACTIVATE 0x0086 +#define WM_GETDLGCODE 0x0087 +#ifndef _WIN32_WCE +#define WM_SYNCPAINT 0x0088 +#endif +#define WM_NCMOUSEMOVE 0x00A0 +#define WM_NCLBUTTONDOWN 0x00A1 +#define WM_NCLBUTTONUP 0x00A2 +#define WM_NCLBUTTONDBLCLK 0x00A3 +#define WM_NCRBUTTONDOWN 0x00A4 +#define WM_NCRBUTTONUP 0x00A5 +#define WM_NCRBUTTONDBLCLK 0x00A6 +#define WM_NCMBUTTONDOWN 0x00A7 +#define WM_NCMBUTTONUP 0x00A8 +#define WM_NCMBUTTONDBLCLK 0x00A9 + +#define WM_NCXBUTTONDOWN 0x00AB +#define WM_NCXBUTTONUP 0x00AC +#define WM_NCXBUTTONDBLCLK 0x00AD +#define WM_INPUT 0x00FF +#define WM_KEYFIRST 0x0100 +#define WM_KEYDOWN 0x0100 +#define WM_KEYUP 0x0101 +#define WM_CHAR 0x0102 +#define WM_DEADCHAR 0x0103 +#define WM_SYSKEYDOWN 0x0104 +#define WM_SYSKEYUP 0x0105 +#define WM_SYSCHAR 0x0106 +#define WM_SYSDEADCHAR 0x0107 +#define WM_UNICHAR 0x0109 +#define WM_KEYLAST 0x0109 +#define UNICODE_NOCHAR 0xFFFF +#define WM_IME_STARTCOMPOSITION 0x010D +#define WM_IME_ENDCOMPOSITION 0x010E +#define WM_IME_COMPOSITION 0x010F +#define WM_IME_KEYLAST 0x010F +#define WM_INITDIALOG 0x0110 +#define WM_COMMAND 0x0111 +#define WM_SYSCOMMAND 0x0112 +#define WM_TIMER 0x0113 +#define WM_HSCROLL 0x0114 +#define WM_VSCROLL 0x0115 +#define WM_INITMENU 0x0116 +#define WM_INITMENUPOPUP 0x0117 +#define WM_MENUSELECT 0x011F +#define WM_MENUCHAR 0x0120 +#define WM_ENTERIDLE 0x0121 +#ifndef _WIN32_WCE +#define WM_MENURBUTTONUP 0x0122 +#define WM_MENUDRAG 0x0123 +#define WM_MENUGETOBJECT 0x0124 +#define WM_UNINITMENUPOPUP 0x0125 +#define WM_MENUCOMMAND 0x0126 + +#ifndef _WIN32_WCE +#define WM_CHANGEUISTATE 0x0127 +#define WM_UPDATEUISTATE 0x0128 +#define WM_QUERYUISTATE 0x0129 + +#define UIS_SET 1 +#define UIS_CLEAR 2 +#define UIS_INITIALIZE 3 + +#define UISF_HIDEFOCUS 0x1 +#define UISF_HIDEACCEL 0x2 +#define UISF_ACTIVE 0x4 +#endif +#endif + +#define WM_CTLCOLORMSGBOX 0x0132 +#define WM_CTLCOLOREDIT 0x0133 +#define WM_CTLCOLORLISTBOX 0x0134 +#define WM_CTLCOLORBTN 0x0135 +#define WM_CTLCOLORDLG 0x0136 +#define WM_CTLCOLORSCROLLBAR 0x0137 +#define WM_CTLCOLORSTATIC 0x0138 +#define MN_GETHMENU 0x01E1 + +#define WM_MOUSEFIRST 0x0200 +#define WM_MOUSEMOVE 0x0200 +#define WM_LBUTTONDOWN 0x0201 +#define WM_LBUTTONUP 0x0202 +#define WM_LBUTTONDBLCLK 0x0203 +#define WM_RBUTTONDOWN 0x0204 +#define WM_RBUTTONUP 0x0205 +#define WM_RBUTTONDBLCLK 0x0206 +#define WM_MBUTTONDOWN 0x0207 +#define WM_MBUTTONUP 0x0208 +#define WM_MBUTTONDBLCLK 0x0209 +#define WM_MOUSEWHEEL 0x020A +#define WM_XBUTTONDOWN 0x020B +#define WM_XBUTTONUP 0x020C +#define WM_XBUTTONDBLCLK 0x020D +#define WM_MOUSELAST 0x020D + +#define WHEEL_DELTA 120 +#define GET_WHEEL_DELTA_WPARAM(wParam) ((short)HIWORD(wParam)) + +#define WHEEL_PAGESCROLL (UINT_MAX) + +#define GET_KEYSTATE_WPARAM(wParam) (LOWORD(wParam)) +#define GET_NCHITTEST_WPARAM(wParam) ((short)LOWORD(wParam)) +#define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam)) + +#define XBUTTON1 0x0001 +#define XBUTTON2 0x0002 + +#define WM_PARENTNOTIFY 0x0210 +#define WM_ENTERMENULOOP 0x0211 +#define WM_EXITMENULOOP 0x0212 + +#define WM_NEXTMENU 0x0213 +#define WM_SIZING 0x0214 +#define WM_CAPTURECHANGED 0x0215 +#define WM_MOVING 0x0216 + +#define WM_POWERBROADCAST 0x0218 + +#ifndef _WIN32_WCE +#define PBT_APMQUERYSUSPEND 0x0000 +#define PBT_APMQUERYSTANDBY 0x0001 + +#define PBT_APMQUERYSUSPENDFAILED 0x0002 +#define PBT_APMQUERYSTANDBYFAILED 0x0003 + +#define PBT_APMSUSPEND 0x0004 +#define PBT_APMSTANDBY 0x0005 + +#define PBT_APMRESUMECRITICAL 0x0006 +#define PBT_APMRESUMESUSPEND 0x0007 +#define PBT_APMRESUMESTANDBY 0x0008 + +#define PBTF_APMRESUMEFROMFAILURE 0x00000001 + +#define PBT_APMBATTERYLOW 0x0009 +#define PBT_APMPOWERSTATUSCHANGE 0x000A + +#define PBT_APMOEMEVENT 0x000B +#define PBT_APMRESUMEAUTOMATIC 0x0012 +#endif + +#define WM_DEVICECHANGE 0x0219 + +#define WM_MDICREATE 0x0220 +#define WM_MDIDESTROY 0x0221 +#define WM_MDIACTIVATE 0x0222 +#define WM_MDIRESTORE 0x0223 +#define WM_MDINEXT 0x0224 +#define WM_MDIMAXIMIZE 0x0225 +#define WM_MDITILE 0x0226 +#define WM_MDICASCADE 0x0227 +#define WM_MDIICONARRANGE 0x0228 +#define WM_MDIGETACTIVE 0x0229 + +#define WM_MDISETMENU 0x0230 +#define WM_ENTERSIZEMOVE 0x0231 +#define WM_EXITSIZEMOVE 0x0232 +#define WM_DROPFILES 0x0233 +#define WM_MDIREFRESHMENU 0x0234 + +#define WM_IME_SETCONTEXT 0x0281 +#define WM_IME_NOTIFY 0x0282 +#define WM_IME_CONTROL 0x0283 +#define WM_IME_COMPOSITIONFULL 0x0284 +#define WM_IME_SELECT 0x0285 +#define WM_IME_CHAR 0x0286 +#define WM_IME_REQUEST 0x0288 +#define WM_IME_KEYDOWN 0x0290 +#define WM_IME_KEYUP 0x0291 + +#define WM_MOUSEHOVER 0x02A1 +#define WM_MOUSELEAVE 0x02A3 +#define WM_NCMOUSEHOVER 0x02A0 +#define WM_NCMOUSELEAVE 0x02A2 +#define WM_WTSSESSION_CHANGE 0x02B1 +#define WM_TABLET_FIRST 0x02c0 +#define WM_TABLET_LAST 0x02df +#define WM_CUT 0x0300 +#define WM_COPY 0x0301 +#define WM_PASTE 0x0302 +#define WM_CLEAR 0x0303 +#define WM_UNDO 0x0304 +#define WM_RENDERFORMAT 0x0305 +#define WM_RENDERALLFORMATS 0x0306 +#define WM_DESTROYCLIPBOARD 0x0307 +#define WM_DRAWCLIPBOARD 0x0308 +#define WM_PAINTCLIPBOARD 0x0309 +#define WM_VSCROLLCLIPBOARD 0x030A +#define WM_SIZECLIPBOARD 0x030B +#define WM_ASKCBFORMATNAME 0x030C +#define WM_CHANGECBCHAIN 0x030D +#define WM_HSCROLLCLIPBOARD 0x030E +#define WM_QUERYNEWPALETTE 0x030F +#define WM_PALETTEISCHANGING 0x0310 +#define WM_PALETTECHANGED 0x0311 +#define WM_HOTKEY 0x0312 +#define WM_PRINT 0x0317 +#define WM_PRINTCLIENT 0x0318 +#define WM_APPCOMMAND 0x0319 +#define WM_THEMECHANGED 0x031A +#define WM_HANDHELDFIRST 0x0358 +#define WM_HANDHELDLAST 0x035F +#define WM_AFXFIRST 0x0360 +#define WM_AFXLAST 0x037F +#define WM_PENWINFIRST 0x0380 +#define WM_PENWINLAST 0x038F +#define WM_APP 0x8000 +#define WM_USER 0x0400 + +#define WMSZ_LEFT 1 +#define WMSZ_RIGHT 2 +#define WMSZ_TOP 3 +#define WMSZ_TOPLEFT 4 +#define WMSZ_TOPRIGHT 5 +#define WMSZ_BOTTOM 6 +#define WMSZ_BOTTOMLEFT 7 +#define WMSZ_BOTTOMRIGHT 8 + +#ifndef NONCMESSAGES + +#define HTERROR (-2) +#define HTTRANSPARENT (-1) +#define HTNOWHERE 0 +#define HTCLIENT 1 +#define HTCAPTION 2 +#define HTSYSMENU 3 +#define HTGROWBOX 4 +#define HTSIZE HTGROWBOX +#define HTMENU 5 +#define HTHSCROLL 6 +#define HTVSCROLL 7 +#define HTMINBUTTON 8 +#define HTMAXBUTTON 9 +#define HTLEFT 10 +#define HTRIGHT 11 +#define HTTOP 12 +#define HTTOPLEFT 13 +#define HTTOPRIGHT 14 +#define HTBOTTOM 15 +#define HTBOTTOMLEFT 16 +#define HTBOTTOMRIGHT 17 +#define HTBORDER 18 +#define HTREDUCE HTMINBUTTON +#define HTZOOM HTMAXBUTTON +#define HTSIZEFIRST HTLEFT +#define HTSIZELAST HTBOTTOMRIGHT +#define HTOBJECT 19 +#define HTCLOSE 20 +#define HTHELP 21 + +#define SMTO_NORMAL 0x0000 +#define SMTO_BLOCK 0x0001 +#define SMTO_ABORTIFHUNG 0x0002 +#define SMTO_NOTIMEOUTIFNOTHUNG 0x0008 +#endif + +#define MA_ACTIVATE 1 +#define MA_ACTIVATEANDEAT 2 +#define MA_NOACTIVATE 3 +#define MA_NOACTIVATEANDEAT 4 + +#define ICON_SMALL 0 +#define ICON_BIG 1 +#define ICON_SMALL2 2 + +#ifdef UNICODE +#define RegisterWindowMessage RegisterWindowMessageW +#else +#define RegisterWindowMessage RegisterWindowMessageA +#endif + + WINUSERAPI UINT WINAPI RegisterWindowMessageA(LPCSTR lpString); + WINUSERAPI UINT WINAPI RegisterWindowMessageW(LPCWSTR lpString); + +#define SIZE_RESTORED 0 +#define SIZE_MINIMIZED 1 +#define SIZE_MAXIMIZED 2 +#define SIZE_MAXSHOW 3 +#define SIZE_MAXHIDE 4 + +#define SIZENORMAL SIZE_RESTORED +#define SIZEICONIC SIZE_MINIMIZED +#define SIZEFULLSCREEN SIZE_MAXIMIZED +#define SIZEZOOMSHOW SIZE_MAXSHOW +#define SIZEZOOMHIDE SIZE_MAXHIDE + + typedef struct tagWINDOWPOS { + HWND hwnd; + HWND hwndInsertAfter; + int x; + int y; + int cx; + int cy; + UINT flags; + } WINDOWPOS,*LPWINDOWPOS,*PWINDOWPOS; + + typedef struct tagNCCALCSIZE_PARAMS { + RECT rgrc[3]; + PWINDOWPOS lppos; + } NCCALCSIZE_PARAMS,*LPNCCALCSIZE_PARAMS; + +#define WVR_ALIGNTOP 0x0010 +#define WVR_ALIGNLEFT 0x0020 +#define WVR_ALIGNBOTTOM 0x0040 +#define WVR_ALIGNRIGHT 0x0080 +#define WVR_HREDRAW 0x0100 +#define WVR_VREDRAW 0x0200 +#define WVR_REDRAW (WVR_HREDRAW | WVR_VREDRAW) +#define WVR_VALIDRECTS 0x0400 + +#ifndef NOKEYSTATES + +#define MK_LBUTTON 0x0001 +#define MK_RBUTTON 0x0002 +#define MK_SHIFT 0x0004 +#define MK_CONTROL 0x0008 +#define MK_MBUTTON 0x0010 +#define MK_XBUTTON1 0x0020 +#define MK_XBUTTON2 0x0040 +#endif + +#ifndef NOTRACKMOUSEEVENT +#define TME_HOVER 0x00000001 +#define TME_LEAVE 0x00000002 +#define TME_NONCLIENT 0x00000010 +#define TME_QUERY 0x40000000 +#define TME_CANCEL 0x80000000 + +#define HOVER_DEFAULT 0xFFFFFFFF +#endif + + typedef struct tagTRACKMOUSEEVENT { + DWORD cbSize; + DWORD dwFlags; + HWND hwndTrack; + DWORD dwHoverTime; + } TRACKMOUSEEVENT,*LPTRACKMOUSEEVENT; + + WINUSERAPI WINBOOL WINAPI TrackMouseEvent(LPTRACKMOUSEEVENT lpEventTrack); +#endif + +#ifndef NOWINSTYLES + +#define WS_OVERLAPPED 0x00000000L +#define WS_POPUP 0x80000000L +#define WS_CHILD 0x40000000L +#define WS_MINIMIZE 0x20000000L +#define WS_VISIBLE 0x10000000L +#define WS_DISABLED 0x08000000L +#define WS_CLIPSIBLINGS 0x04000000L +#define WS_CLIPCHILDREN 0x02000000L +#define WS_MAXIMIZE 0x01000000L +#define WS_CAPTION 0x00C00000L +#define WS_BORDER 0x00800000L +#define WS_DLGFRAME 0x00400000L +#define WS_VSCROLL 0x00200000L +#define WS_HSCROLL 0x00100000L +#define WS_SYSMENU 0x00080000L +#define WS_THICKFRAME 0x00040000L +#define WS_GROUP 0x00020000L +#define WS_TABSTOP 0x00010000L +#define WS_MINIMIZEBOX 0x00020000L +#define WS_MAXIMIZEBOX 0x00010000L +#define WS_TILED WS_OVERLAPPED +#define WS_ICONIC WS_MINIMIZE +#define WS_SIZEBOX WS_THICKFRAME +#define WS_TILEDWINDOW WS_OVERLAPPEDWINDOW +#define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX) +#define WS_POPUPWINDOW (WS_POPUP | WS_BORDER | WS_SYSMENU) +#define WS_CHILDWINDOW (WS_CHILD) + +#define WS_EX_DLGMODALFRAME 0x00000001L +#define WS_EX_NOPARENTNOTIFY 0x00000004L +#define WS_EX_TOPMOST 0x00000008L +#define WS_EX_ACCEPTFILES 0x00000010L +#define WS_EX_TRANSPARENT 0x00000020L +#define WS_EX_MDICHILD 0x00000040L +#define WS_EX_TOOLWINDOW 0x00000080L +#define WS_EX_WINDOWEDGE 0x00000100L +#define WS_EX_CLIENTEDGE 0x00000200L +#define WS_EX_CONTEXTHELP 0x00000400L +#define WS_EX_RIGHT 0x00001000L +#define WS_EX_LEFT 0x00000000L +#define WS_EX_RTLREADING 0x00002000L +#define WS_EX_LTRREADING 0x00000000L +#define WS_EX_LEFTSCROLLBAR 0x00004000L +#define WS_EX_RIGHTSCROLLBAR 0x00000000L +#define WS_EX_CONTROLPARENT 0x00010000L +#define WS_EX_STATICEDGE 0x00020000L +#define WS_EX_APPWINDOW 0x00040000L +#define WS_EX_OVERLAPPEDWINDOW (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE) +#define WS_EX_PALETTEWINDOW (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST) +#define WS_EX_LAYERED 0x00080000 +#define WS_EX_NOINHERITLAYOUT 0x00100000L +#define WS_EX_LAYOUTRTL 0x00400000L +#define WS_EX_COMPOSITED 0x02000000L +#define WS_EX_NOACTIVATE 0x08000000L + +#define CS_VREDRAW 0x0001 +#define CS_HREDRAW 0x0002 +#define CS_DBLCLKS 0x0008 +#define CS_OWNDC 0x0020 +#define CS_CLASSDC 0x0040 +#define CS_PARENTDC 0x0080 +#define CS_NOCLOSE 0x0200 +#define CS_SAVEBITS 0x0800 +#define CS_BYTEALIGNCLIENT 0x1000 +#define CS_BYTEALIGNWINDOW 0x2000 +#define CS_GLOBALCLASS 0x4000 +#define CS_IME 0x00010000 +#define CS_DROPSHADOW 0x00020000 +#endif + +#define PRF_CHECKVISIBLE 0x00000001L +#define PRF_NONCLIENT 0x00000002L +#define PRF_CLIENT 0x00000004L +#define PRF_ERASEBKGND 0x00000008L +#define PRF_CHILDREN 0x00000010L +#define PRF_OWNED 0x00000020L + +#define BDR_RAISEDOUTER 0x0001 +#define BDR_SUNKENOUTER 0x0002 +#define BDR_RAISEDINNER 0x0004 +#define BDR_SUNKENINNER 0x0008 + +#define BDR_OUTER (BDR_RAISEDOUTER | BDR_SUNKENOUTER) +#define BDR_INNER (BDR_RAISEDINNER | BDR_SUNKENINNER) +#define BDR_RAISED (BDR_RAISEDOUTER | BDR_RAISEDINNER) +#define BDR_SUNKEN (BDR_SUNKENOUTER | BDR_SUNKENINNER) + +#define EDGE_RAISED (BDR_RAISEDOUTER | BDR_RAISEDINNER) +#define EDGE_SUNKEN (BDR_SUNKENOUTER | BDR_SUNKENINNER) +#define EDGE_ETCHED (BDR_SUNKENOUTER | BDR_RAISEDINNER) +#define EDGE_BUMP (BDR_RAISEDOUTER | BDR_SUNKENINNER) + +#define BF_LEFT 0x0001 +#define BF_TOP 0x0002 +#define BF_RIGHT 0x0004 +#define BF_BOTTOM 0x0008 + +#define BF_TOPLEFT (BF_TOP | BF_LEFT) +#define BF_TOPRIGHT (BF_TOP | BF_RIGHT) +#define BF_BOTTOMLEFT (BF_BOTTOM | BF_LEFT) +#define BF_BOTTOMRIGHT (BF_BOTTOM | BF_RIGHT) +#define BF_RECT (BF_LEFT | BF_TOP | BF_RIGHT | BF_BOTTOM) + +#define BF_DIAGONAL 0x0010 + +#define BF_DIAGONAL_ENDTOPRIGHT (BF_DIAGONAL | BF_TOP | BF_RIGHT) +#define BF_DIAGONAL_ENDTOPLEFT (BF_DIAGONAL | BF_TOP | BF_LEFT) +#define BF_DIAGONAL_ENDBOTTOMLEFT (BF_DIAGONAL | BF_BOTTOM | BF_LEFT) +#define BF_DIAGONAL_ENDBOTTOMRIGHT (BF_DIAGONAL | BF_BOTTOM | BF_RIGHT) + +#define BF_MIDDLE 0x0800 +#define BF_SOFT 0x1000 +#define BF_ADJUST 0x2000 +#define BF_FLAT 0x4000 +#define BF_MONO 0x8000 + + WINUSERAPI WINBOOL WINAPI DrawEdge(HDC hdc,LPRECT qrc,UINT edge,UINT grfFlags); + +#define DFC_CAPTION 1 +#define DFC_MENU 2 +#define DFC_SCROLL 3 +#define DFC_BUTTON 4 +#define DFC_POPUPMENU 5 + +#define DFCS_CAPTIONCLOSE 0x0000 +#define DFCS_CAPTIONMIN 0x0001 +#define DFCS_CAPTIONMAX 0x0002 +#define DFCS_CAPTIONRESTORE 0x0003 +#define DFCS_CAPTIONHELP 0x0004 + +#define DFCS_MENUARROW 0x0000 +#define DFCS_MENUCHECK 0x0001 +#define DFCS_MENUBULLET 0x0002 +#define DFCS_MENUARROWRIGHT 0x0004 +#define DFCS_SCROLLUP 0x0000 +#define DFCS_SCROLLDOWN 0x0001 +#define DFCS_SCROLLLEFT 0x0002 +#define DFCS_SCROLLRIGHT 0x0003 +#define DFCS_SCROLLCOMBOBOX 0x0005 +#define DFCS_SCROLLSIZEGRIP 0x0008 +#define DFCS_SCROLLSIZEGRIPRIGHT 0x0010 + +#define DFCS_BUTTONCHECK 0x0000 +#define DFCS_BUTTONRADIOIMAGE 0x0001 +#define DFCS_BUTTONRADIOMASK 0x0002 +#define DFCS_BUTTONRADIO 0x0004 +#define DFCS_BUTTON3STATE 0x0008 +#define DFCS_BUTTONPUSH 0x0010 + +#define DFCS_INACTIVE 0x0100 +#define DFCS_PUSHED 0x0200 +#define DFCS_CHECKED 0x0400 + +#define DFCS_TRANSPARENT 0x0800 +#define DFCS_HOT 0x1000 + +#define DFCS_ADJUSTRECT 0x2000 +#define DFCS_FLAT 0x4000 +#define DFCS_MONO 0x8000 + + WINUSERAPI WINBOOL WINAPI DrawFrameControl(HDC,LPRECT,UINT,UINT); + +#define DC_ACTIVE 0x0001 +#define DC_SMALLCAP 0x0002 +#define DC_ICON 0x0004 +#define DC_TEXT 0x0008 +#define DC_INBUTTON 0x0010 +#define DC_GRADIENT 0x0020 +#define DC_BUTTONS 0x1000 + + WINUSERAPI WINBOOL WINAPI DrawCaption(HWND hwnd,HDC hdc,CONST RECT *lprect,UINT flags); + +#define IDANI_OPEN 1 +#define IDANI_CAPTION 3 + + WINUSERAPI WINBOOL WINAPI DrawAnimatedRects(HWND hwnd,int idAni,CONST RECT *lprcFrom,CONST RECT *lprcTo); + +#ifndef NOCLIPBOARD + +#define CF_TEXT 1 +#define CF_BITMAP 2 +#define CF_METAFILEPICT 3 +#define CF_SYLK 4 +#define CF_DIF 5 +#define CF_TIFF 6 +#define CF_OEMTEXT 7 +#define CF_DIB 8 +#define CF_PALETTE 9 +#define CF_PENDATA 10 +#define CF_RIFF 11 +#define CF_WAVE 12 +#define CF_UNICODETEXT 13 +#define CF_ENHMETAFILE 14 +#define CF_HDROP 15 +#define CF_LOCALE 16 +#define CF_DIBV5 17 +#define CF_MAX 18 + +#define CF_OWNERDISPLAY 0x0080 +#define CF_DSPTEXT 0x0081 +#define CF_DSPBITMAP 0x0082 +#define CF_DSPMETAFILEPICT 0x0083 +#define CF_DSPENHMETAFILE 0x008E + +#define CF_PRIVATEFIRST 0x0200 +#define CF_PRIVATELAST 0x02FF + +#define CF_GDIOBJFIRST 0x0300 +#define CF_GDIOBJLAST 0x03FF +#endif + +#define FVIRTKEY TRUE +#define FNOINVERT 0x02 +#define FSHIFT 0x04 +#define FCONTROL 0x08 +#define FALT 0x10 + + typedef struct tagACCEL { + BYTE fVirt; + WORD key; + WORD cmd; + } ACCEL,*LPACCEL; + + typedef struct tagPAINTSTRUCT { + HDC hdc; + WINBOOL fErase; + RECT rcPaint; + WINBOOL fRestore; + WINBOOL fIncUpdate; + BYTE rgbReserved[32]; + } PAINTSTRUCT,*PPAINTSTRUCT,*NPPAINTSTRUCT,*LPPAINTSTRUCT; + + typedef struct tagCREATESTRUCTA { + LPVOID lpCreateParams; + HINSTANCE hInstance; + HMENU hMenu; + HWND hwndParent; + int cy; + int cx; + int y; + int x; + LONG style; + LPCSTR lpszName; + LPCSTR lpszClass; + DWORD dwExStyle; + } CREATESTRUCTA,*LPCREATESTRUCTA; + + typedef struct tagCREATESTRUCTW { + LPVOID lpCreateParams; + HINSTANCE hInstance; + HMENU hMenu; + HWND hwndParent; + int cy; + int cx; + int y; + int x; + LONG style; + LPCWSTR lpszName; + LPCWSTR lpszClass; + DWORD dwExStyle; + } CREATESTRUCTW,*LPCREATESTRUCTW; + +#ifdef UNICODE + typedef CREATESTRUCTW CREATESTRUCT; + typedef LPCREATESTRUCTW LPCREATESTRUCT; +#else + typedef CREATESTRUCTA CREATESTRUCT; + typedef LPCREATESTRUCTA LPCREATESTRUCT; +#endif + + typedef struct tagWINDOWPLACEMENT { + UINT length; + UINT flags; + UINT showCmd; + POINT ptMinPosition; + POINT ptMaxPosition; + RECT rcNormalPosition; + } WINDOWPLACEMENT; + typedef WINDOWPLACEMENT *PWINDOWPLACEMENT,*LPWINDOWPLACEMENT; + +#define WPF_SETMINPOSITION 0x0001 +#define WPF_RESTORETOMAXIMIZED 0x0002 +#define WPF_ASYNCWINDOWPLACEMENT 0x0004 + + typedef struct tagNMHDR { + HWND hwndFrom; + UINT_PTR idFrom; + UINT code; + } NMHDR; + + typedef NMHDR *LPNMHDR; + + typedef struct tagSTYLESTRUCT { + DWORD styleOld; + DWORD styleNew; + } STYLESTRUCT,*LPSTYLESTRUCT; + +#define ODT_MENU 1 +#define ODT_LISTBOX 2 +#define ODT_COMBOBOX 3 +#define ODT_BUTTON 4 +#define ODT_STATIC 5 + +#define ODA_DRAWENTIRE 0x0001 +#define ODA_SELECT 0x0002 +#define ODA_FOCUS 0x0004 + +#define ODS_SELECTED 0x0001 +#define ODS_GRAYED 0x0002 +#define ODS_DISABLED 0x0004 +#define ODS_CHECKED 0x0008 +#define ODS_FOCUS 0x0010 +#define ODS_DEFAULT 0x0020 +#define ODS_COMBOBOXEDIT 0x1000 +#define ODS_HOTLIGHT 0x0040 +#define ODS_INACTIVE 0x0080 +#define ODS_NOACCEL 0x0100 +#define ODS_NOFOCUSRECT 0x0200 + + typedef struct tagMEASUREITEMSTRUCT { + UINT CtlType; + UINT CtlID; + UINT itemID; + UINT itemWidth; + UINT itemHeight; + ULONG_PTR itemData; + } MEASUREITEMSTRUCT,*PMEASUREITEMSTRUCT,*LPMEASUREITEMSTRUCT; + + typedef struct tagDRAWITEMSTRUCT { + UINT CtlType; + UINT CtlID; + UINT itemID; + UINT itemAction; + UINT itemState; + HWND hwndItem; + HDC hDC; + RECT rcItem; + ULONG_PTR itemData; + } DRAWITEMSTRUCT,*PDRAWITEMSTRUCT,*LPDRAWITEMSTRUCT; + + typedef struct tagDELETEITEMSTRUCT { + UINT CtlType; + UINT CtlID; + UINT itemID; + HWND hwndItem; + ULONG_PTR itemData; + } DELETEITEMSTRUCT,*PDELETEITEMSTRUCT,*LPDELETEITEMSTRUCT; + + typedef struct tagCOMPAREITEMSTRUCT { + UINT CtlType; + UINT CtlID; + HWND hwndItem; + UINT itemID1; + ULONG_PTR itemData1; + UINT itemID2; + ULONG_PTR itemData2; + DWORD dwLocaleId; + } COMPAREITEMSTRUCT,*PCOMPAREITEMSTRUCT,*LPCOMPAREITEMSTRUCT; + +#ifndef NOMSG +#ifdef UNICODE +#define GetMessage GetMessageW +#define DispatchMessage DispatchMessageW +#define PeekMessage PeekMessageW +#else +#define GetMessage GetMessageA +#define DispatchMessage DispatchMessageA +#define PeekMessage PeekMessageA +#endif + + WINUSERAPI WINBOOL WINAPI GetMessageA(LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax); + WINUSERAPI WINBOOL WINAPI GetMessageW(LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax); + WINUSERAPI WINBOOL WINAPI TranslateMessage(CONST MSG *lpMsg); + WINUSERAPI LRESULT WINAPI DispatchMessageA(CONST MSG *lpMsg); + WINUSERAPI LRESULT WINAPI DispatchMessageW(CONST MSG *lpMsg); + WINUSERAPI WINBOOL WINAPI SetMessageQueue(int cMessagesMax); + WINUSERAPI WINBOOL WINAPI PeekMessageA(LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax,UINT wRemoveMsg); + WINUSERAPI WINBOOL WINAPI PeekMessageW(LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax,UINT wRemoveMsg); + +#define PM_NOREMOVE 0x0000 +#define PM_REMOVE 0x0001 +#define PM_NOYIELD 0x0002 +#define PM_QS_INPUT (QS_INPUT << 16) +#define PM_QS_POSTMESSAGE ((QS_POSTMESSAGE | QS_HOTKEY | QS_TIMER) << 16) +#define PM_QS_PAINT (QS_PAINT << 16) +#define PM_QS_SENDMESSAGE (QS_SENDMESSAGE << 16) +#endif + + WINUSERAPI WINBOOL WINAPI RegisterHotKey(HWND hWnd,int id,UINT fsModifiers,UINT vk); + WINUSERAPI WINBOOL WINAPI UnregisterHotKey(HWND hWnd,int id); + +#define MOD_ALT 0x0001 +#define MOD_CONTROL 0x0002 +#define MOD_SHIFT 0x0004 +#define MOD_WIN 0x0008 + +#define IDHOT_SNAPWINDOW (-1) +#define IDHOT_SNAPDESKTOP (-2) + +#ifdef WIN_INTERNAL +#ifndef LSTRING +#define NOLSTRING +#endif +#ifndef LFILEIO +#define NOLFILEIO +#endif +#endif + +#define ENDSESSION_LOGOFF 0x80000000 + +#define EWX_LOGOFF 0 +#define EWX_SHUTDOWN 0x00000001 +#define EWX_REBOOT 0x00000002 +#define EWX_FORCE 0x00000004 +#define EWX_POWEROFF 0x00000008 +#define EWX_FORCEIFHUNG 0x00000010 + +#define ExitWindows(dwReserved,Code) ExitWindowsEx(EWX_LOGOFF,0xFFFFFFFF) + +#ifdef UNICODE +#define SendMessage SendMessageW +#define SendMessageTimeout SendMessageTimeoutW +#define SendNotifyMessage SendNotifyMessageW +#define SendMessageCallback SendMessageCallbackW +#else +#define SendMessage SendMessageA +#define SendMessageTimeout SendMessageTimeoutA +#define SendNotifyMessage SendNotifyMessageA +#define SendMessageCallback SendMessageCallbackA +#endif + + WINUSERAPI WINBOOL WINAPI ExitWindowsEx(UINT uFlags,DWORD dwReason); + WINUSERAPI WINBOOL WINAPI SwapMouseButton(WINBOOL fSwap); + WINUSERAPI DWORD WINAPI GetMessagePos(VOID); + WINUSERAPI LONG WINAPI GetMessageTime(VOID); + WINUSERAPI LPARAM WINAPI GetMessageExtraInfo(VOID); + WINUSERAPI WINBOOL WINAPI IsWow64Message(VOID); + WINUSERAPI LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam); + WINUSERAPI LRESULT WINAPI SendMessageA(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI SendMessageW(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI SendMessageTimeoutA(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,UINT fuFlags,UINT uTimeout,PDWORD_PTR lpdwResult); + WINUSERAPI LRESULT WINAPI SendMessageTimeoutW(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,UINT fuFlags,UINT uTimeout,PDWORD_PTR lpdwResult); + WINUSERAPI WINBOOL WINAPI SendNotifyMessageA(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI SendNotifyMessageW(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI SendMessageCallbackA(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,SENDASYNCPROC lpResultCallBack,ULONG_PTR dwData); + WINUSERAPI WINBOOL WINAPI SendMessageCallbackW(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,SENDASYNCPROC lpResultCallBack,ULONG_PTR dwData); + + typedef struct { + UINT cbSize; + HDESK hdesk; + HWND hwnd; + LUID luid; + } BSMINFO,*PBSMINFO; + +#ifdef UNICODE +#define BroadcastSystemMessageEx BroadcastSystemMessageExW +#define BroadcastSystemMessage BroadcastSystemMessageW +#else +#define BroadcastSystemMessageEx BroadcastSystemMessageExA +#define BroadcastSystemMessage BroadcastSystemMessageA +#endif + + WINUSERAPI long WINAPI BroadcastSystemMessageExA(DWORD flags,LPDWORD lpInfo,UINT Msg,WPARAM wParam,LPARAM lParam,PBSMINFO pbsmInfo); + WINUSERAPI long WINAPI BroadcastSystemMessageExW(DWORD flags,LPDWORD lpInfo,UINT Msg,WPARAM wParam,LPARAM lParam,PBSMINFO pbsmInfo); + WINUSERAPI long WINAPI BroadcastSystemMessageA(DWORD flags,LPDWORD lpInfo,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI long WINAPI BroadcastSystemMessageW(DWORD flags,LPDWORD lpInfo,UINT Msg,WPARAM wParam,LPARAM lParam); + +#define BSM_ALLCOMPONENTS 0x00000000 +#define BSM_VXDS 0x00000001 +#define BSM_NETDRIVER 0x00000002 +#define BSM_INSTALLABLEDRIVERS 0x00000004 +#define BSM_APPLICATIONS 0x00000008 +#define BSM_ALLDESKTOPS 0x00000010 + +#define BSF_QUERY 0x00000001 +#define BSF_IGNORECURRENTTASK 0x00000002 +#define BSF_FLUSHDISK 0x00000004 +#define BSF_NOHANG 0x00000008 +#define BSF_POSTMESSAGE 0x00000010 +#define BSF_FORCEIFHUNG 0x00000020 +#define BSF_NOTIMEOUTIFNOTHUNG 0x00000040 +#define BSF_ALLOWSFW 0x00000080 +#define BSF_SENDNOTIFYMESSAGE 0x00000100 +#define BSF_RETURNHDESK 0x00000200 +#define BSF_LUID 0x00000400 + +#define BROADCAST_QUERY_DENY 0x424D5144 + + typedef PVOID HDEVNOTIFY; + typedef HDEVNOTIFY *PHDEVNOTIFY; + +#define DEVICE_NOTIFY_WINDOW_HANDLE 0x00000000 +#define DEVICE_NOTIFY_SERVICE_HANDLE 0x00000001 +#define DEVICE_NOTIFY_ALL_INTERFACE_CLASSES 0x00000004 + +#ifdef UNICODE +#define RegisterDeviceNotification RegisterDeviceNotificationW +#define PostMessage PostMessageW +#define PostThreadMessage PostThreadMessageW +#define PostAppMessage PostAppMessageW +#define DefWindowProc DefWindowProcW +#define CallWindowProc CallWindowProcW +#define RegisterClass RegisterClassW +#define UnregisterClass UnregisterClassW +#define GetClassInfo GetClassInfoW +#define RegisterClassEx RegisterClassExW +#define GetClassInfoEx GetClassInfoExW +#else +#define RegisterDeviceNotification RegisterDeviceNotificationA +#define PostMessage PostMessageA +#define PostThreadMessage PostThreadMessageA +#define PostAppMessage PostAppMessageA +#define DefWindowProc DefWindowProcA +#define CallWindowProc CallWindowProcA +#define RegisterClass RegisterClassA +#define UnregisterClass UnregisterClassA +#define GetClassInfo GetClassInfoA +#define RegisterClassEx RegisterClassExA +#define GetClassInfoEx GetClassInfoExA +#endif + + WINUSERAPI HDEVNOTIFY WINAPI RegisterDeviceNotificationA(HANDLE hRecipient,LPVOID NotificationFilter,DWORD Flags); + WINUSERAPI HDEVNOTIFY WINAPI RegisterDeviceNotificationW(HANDLE hRecipient,LPVOID NotificationFilter,DWORD Flags); + WINUSERAPI WINBOOL WINAPI UnregisterDeviceNotification(HDEVNOTIFY Handle); + WINUSERAPI WINBOOL WINAPI PostMessageA(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI PostMessageW(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI PostThreadMessageA(DWORD idThread,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI PostThreadMessageW(DWORD idThread,UINT Msg,WPARAM wParam,LPARAM lParam); +#define PostAppMessageA(idThread,wMsg,wParam,lParam) PostThreadMessageA((DWORD)idThread,wMsg,wParam,lParam) +#define PostAppMessageW(idThread,wMsg,wParam,lParam) PostThreadMessageW((DWORD)idThread,wMsg,wParam,lParam) + +#define HWND_BROADCAST ((HWND)0xffff) +#define HWND_MESSAGE ((HWND)-3) + + WINUSERAPI WINBOOL WINAPI AttachThreadInput(DWORD idAttach,DWORD idAttachTo,WINBOOL fAttach); + WINUSERAPI WINBOOL WINAPI ReplyMessage(LRESULT lResult); + WINUSERAPI WINBOOL WINAPI WaitMessage(VOID); + WINUSERAPI DWORD WINAPI WaitForInputIdle(HANDLE hProcess,DWORD dwMilliseconds); + WINUSERAPI LRESULT WINAPI DefWindowProcA(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI DefWindowProcW(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI VOID WINAPI PostQuitMessage(int nExitCode); + WINUSERAPI LRESULT WINAPI CallWindowProcA(WNDPROC lpPrevWndFunc,HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI CallWindowProcW(WNDPROC lpPrevWndFunc,HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI InSendMessage(VOID); + WINUSERAPI DWORD WINAPI InSendMessageEx(LPVOID lpReserved); + +#define ISMEX_NOSEND 0x00000000 +#define ISMEX_SEND 0x00000001 +#define ISMEX_NOTIFY 0x00000002 +#define ISMEX_CALLBACK 0x00000004 +#define ISMEX_REPLIED 0x00000008 + + WINUSERAPI UINT WINAPI GetDoubleClickTime(VOID); + WINUSERAPI WINBOOL WINAPI SetDoubleClickTime(UINT); + WINUSERAPI ATOM WINAPI RegisterClassA(CONST WNDCLASSA *lpWndClass); + WINUSERAPI ATOM WINAPI RegisterClassW(CONST WNDCLASSW *lpWndClass); + WINUSERAPI WINBOOL WINAPI UnregisterClassA(LPCSTR lpClassName,HINSTANCE hInstance); + WINUSERAPI WINBOOL WINAPI UnregisterClassW(LPCWSTR lpClassName,HINSTANCE hInstance); + WINUSERAPI WINBOOL WINAPI GetClassInfoA(HINSTANCE hInstance,LPCSTR lpClassName,LPWNDCLASSA lpWndClass); + WINUSERAPI WINBOOL WINAPI GetClassInfoW(HINSTANCE hInstance,LPCWSTR lpClassName,LPWNDCLASSW lpWndClass); + WINUSERAPI ATOM WINAPI RegisterClassExA(CONST WNDCLASSEXA *); + WINUSERAPI ATOM WINAPI RegisterClassExW(CONST WNDCLASSEXW *); + WINUSERAPI WINBOOL WINAPI GetClassInfoExA(HINSTANCE hInstance,LPCSTR lpszClass,LPWNDCLASSEXA lpwcx); + WINUSERAPI WINBOOL WINAPI GetClassInfoExW(HINSTANCE hInstance,LPCWSTR lpszClass,LPWNDCLASSEXW lpwcx); + +#define CW_USEDEFAULT ((int)0x80000000) + +#define HWND_DESKTOP ((HWND)0) + + typedef BOOLEAN (WINAPI *PREGISTERCLASSNAMEW)(LPCWSTR); + +#ifdef UNICODE +#define CreateWindowEx CreateWindowExW +#define CreateWindow CreateWindowW +#else +#define CreateWindowEx CreateWindowExA +#define CreateWindow CreateWindowA +#endif + + WINUSERAPI HWND WINAPI CreateWindowExA(DWORD dwExStyle,LPCSTR lpClassName,LPCSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam); + WINUSERAPI HWND WINAPI CreateWindowExW(DWORD dwExStyle,LPCWSTR lpClassName,LPCWSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam); +#define CreateWindowA(lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam) CreateWindowExA(0L,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam) +#define CreateWindowW(lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam) CreateWindowExW(0L,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam) + WINUSERAPI WINBOOL WINAPI IsWindow(HWND hWnd); + WINUSERAPI WINBOOL WINAPI IsMenu(HMENU hMenu); + WINUSERAPI WINBOOL WINAPI IsChild(HWND hWndParent,HWND hWnd); + WINUSERAPI WINBOOL WINAPI DestroyWindow(HWND hWnd); + WINUSERAPI WINBOOL WINAPI ShowWindow(HWND hWnd,int nCmdShow); + WINUSERAPI WINBOOL WINAPI AnimateWindow(HWND hWnd,DWORD dwTime,DWORD dwFlags); + +#if defined(_WINGDI_) && !defined(NOGDI) + WINUSERAPI WINBOOL WINAPI UpdateLayeredWindow(HWND hWnd,HDC hdcDst,POINT *pptDst,SIZE *psize,HDC hdcSrc,POINT *pptSrc,COLORREF crKey,BLENDFUNCTION *pblend,DWORD dwFlags); + + typedef struct tagUPDATELAYEREDWINDOWINFO { + DWORD cbSize; + HDC hdcDst; + POINT CONST *pptDst; + SIZE CONST *psize; + HDC hdcSrc; + POINT CONST *pptSrc; + COLORREF crKey; + BLENDFUNCTION CONST *pblend; + DWORD dwFlags; + RECT CONST *prcDirty; + } UPDATELAYEREDWINDOWINFO,*PUPDATELAYEREDWINDOWINFO; + + WINUSERAPI WINBOOL WINAPI UpdateLayeredWindowIndirect(HWND hWnd,UPDATELAYEREDWINDOWINFO CONST *pULWInfo); + WINUSERAPI WINBOOL WINAPI GetLayeredWindowAttributes(HWND hwnd,COLORREF *pcrKey,BYTE *pbAlpha,DWORD *pdwFlags); + +#define PW_CLIENTONLY 0x00000001 + + WINUSERAPI WINBOOL WINAPI PrintWindow(HWND hwnd,HDC hdcBlt,UINT nFlags); + WINUSERAPI WINBOOL WINAPI SetLayeredWindowAttributes(HWND hwnd,COLORREF crKey,BYTE bAlpha,DWORD dwFlags); + +#define LWA_COLORKEY 0x00000001 +#define LWA_ALPHA 0x00000002 + +#define ULW_COLORKEY 0x00000001 +#define ULW_ALPHA 0x00000002 +#define ULW_OPAQUE 0x00000004 + +#define ULW_EX_NORESIZE 0x00000008 + + WINUSERAPI WINBOOL WINAPI ShowWindowAsync(HWND hWnd,int nCmdShow); + WINUSERAPI WINBOOL WINAPI FlashWindow(HWND hWnd,WINBOOL bInvert); + + typedef struct { + UINT cbSize; + HWND hwnd; + DWORD dwFlags; + UINT uCount; + DWORD dwTimeout; + } FLASHWINFO,*PFLASHWINFO; + + WINUSERAPI WINBOOL WINAPI FlashWindowEx(PFLASHWINFO pfwi); + +#define FLASHW_STOP 0 +#define FLASHW_CAPTION 0x00000001 +#define FLASHW_TRAY 0x00000002 +#define FLASHW_ALL (FLASHW_CAPTION | FLASHW_TRAY) +#define FLASHW_TIMER 0x00000004 +#define FLASHW_TIMERNOFG 0x0000000C + + WINUSERAPI WINBOOL WINAPI ShowOwnedPopups(HWND hWnd,WINBOOL fShow); + WINUSERAPI WINBOOL WINAPI OpenIcon(HWND hWnd); + WINUSERAPI WINBOOL WINAPI CloseWindow(HWND hWnd); + WINUSERAPI WINBOOL WINAPI MoveWindow(HWND hWnd,int X,int Y,int nWidth,int nHeight,WINBOOL bRepaint); + WINUSERAPI WINBOOL WINAPI SetWindowPos(HWND hWnd,HWND hWndInsertAfter,int X,int Y,int cx,int cy,UINT uFlags); + WINUSERAPI WINBOOL WINAPI GetWindowPlacement(HWND hWnd,WINDOWPLACEMENT *lpwndpl); + WINUSERAPI WINBOOL WINAPI SetWindowPlacement(HWND hWnd,CONST WINDOWPLACEMENT *lpwndpl); + +#ifndef NODEFERWINDOWPOS + WINUSERAPI HDWP WINAPI BeginDeferWindowPos(int nNumWindows); + WINUSERAPI HDWP WINAPI DeferWindowPos(HDWP hWinPosInfo,HWND hWnd,HWND hWndInsertAfter,int x,int y,int cx,int cy,UINT uFlags); + WINUSERAPI WINBOOL WINAPI EndDeferWindowPos(HDWP hWinPosInfo); +#endif + + WINUSERAPI WINBOOL WINAPI IsWindowVisible(HWND hWnd); + WINUSERAPI WINBOOL WINAPI IsIconic(HWND hWnd); + WINUSERAPI WINBOOL WINAPI AnyPopup(VOID); + WINUSERAPI WINBOOL WINAPI BringWindowToTop(HWND hWnd); + WINUSERAPI WINBOOL WINAPI IsZoomed(HWND hWnd); + +#define SWP_NOSIZE 0x0001 +#define SWP_NOMOVE 0x0002 +#define SWP_NOZORDER 0x0004 +#define SWP_NOREDRAW 0x0008 +#define SWP_NOACTIVATE 0x0010 +#define SWP_FRAMECHANGED 0x0020 +#define SWP_SHOWWINDOW 0x0040 +#define SWP_HIDEWINDOW 0x0080 +#define SWP_NOCOPYBITS 0x0100 +#define SWP_NOOWNERZORDER 0x0200 +#define SWP_NOSENDCHANGING 0x0400 + +#define SWP_DRAWFRAME SWP_FRAMECHANGED +#define SWP_NOREPOSITION SWP_NOOWNERZORDER +#define SWP_DEFERERASE 0x2000 +#define SWP_ASYNCWINDOWPOS 0x4000 + +#define HWND_TOP ((HWND)0) +#define HWND_BOTTOM ((HWND)1) +#define HWND_TOPMOST ((HWND)-1) +#define HWND_NOTOPMOST ((HWND)-2) + +#ifndef NOCTLMGR + +#include + + typedef struct { + DWORD style; + DWORD dwExtendedStyle; + WORD cdit; + short x; + short y; + short cx; + short cy; + } DLGTEMPLATE; + + typedef DLGTEMPLATE *LPDLGTEMPLATEA; + typedef DLGTEMPLATE *LPDLGTEMPLATEW; + +#ifdef UNICODE + typedef LPDLGTEMPLATEW LPDLGTEMPLATE; +#else + typedef LPDLGTEMPLATEA LPDLGTEMPLATE; +#endif + + typedef CONST DLGTEMPLATE *LPCDLGTEMPLATEA; + typedef CONST DLGTEMPLATE *LPCDLGTEMPLATEW; + +#ifdef UNICODE + typedef LPCDLGTEMPLATEW LPCDLGTEMPLATE; +#else + typedef LPCDLGTEMPLATEA LPCDLGTEMPLATE; +#endif + + typedef struct { + DWORD style; + DWORD dwExtendedStyle; + short x; + short y; + short cx; + short cy; + WORD id; + } DLGITEMTEMPLATE; + + typedef DLGITEMTEMPLATE *PDLGITEMTEMPLATEA; + typedef DLGITEMTEMPLATE *PDLGITEMTEMPLATEW; + +#ifdef UNICODE + typedef PDLGITEMTEMPLATEW PDLGITEMTEMPLATE; +#else + typedef PDLGITEMTEMPLATEA PDLGITEMTEMPLATE; +#endif + + typedef DLGITEMTEMPLATE *LPDLGITEMTEMPLATEA; + typedef DLGITEMTEMPLATE *LPDLGITEMTEMPLATEW; + +#ifdef UNICODE + typedef LPDLGITEMTEMPLATEW LPDLGITEMTEMPLATE; +#else + typedef LPDLGITEMTEMPLATEA LPDLGITEMTEMPLATE; +#endif + +#include + +#ifdef UNICODE +#define CreateDialogParam CreateDialogParamW +#define CreateDialogIndirectParam CreateDialogIndirectParamW +#define CreateDialog CreateDialogW +#define CreateDialogIndirect CreateDialogIndirectW +#define DialogBoxParam DialogBoxParamW +#define DialogBoxIndirectParam DialogBoxIndirectParamW +#define DialogBox DialogBoxW +#define DialogBoxIndirect DialogBoxIndirectW +#define SetDlgItemText SetDlgItemTextW +#define GetDlgItemText GetDlgItemTextW +#define SendDlgItemMessage SendDlgItemMessageW +#define DefDlgProc DefDlgProcW +#else +#define CreateDialogParam CreateDialogParamA +#define CreateDialogIndirectParam CreateDialogIndirectParamA +#define CreateDialog CreateDialogA +#define CreateDialogIndirect CreateDialogIndirectA +#define DialogBoxParam DialogBoxParamA +#define DialogBoxIndirectParam DialogBoxIndirectParamA +#define DialogBox DialogBoxA +#define DialogBoxIndirect DialogBoxIndirectA +#define SetDlgItemText SetDlgItemTextA +#define GetDlgItemText GetDlgItemTextA +#define SendDlgItemMessage SendDlgItemMessageA +#define DefDlgProc DefDlgProcA +#endif + + WINUSERAPI HWND WINAPI CreateDialogParamA(HINSTANCE hInstance,LPCSTR lpTemplateName,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); + WINUSERAPI HWND WINAPI CreateDialogParamW(HINSTANCE hInstance,LPCWSTR lpTemplateName,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); + WINUSERAPI HWND WINAPI CreateDialogIndirectParamA(HINSTANCE hInstance,LPCDLGTEMPLATEA lpTemplate,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); + WINUSERAPI HWND WINAPI CreateDialogIndirectParamW(HINSTANCE hInstance,LPCDLGTEMPLATEW lpTemplate,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); +#define CreateDialogA(hInstance,lpName,hWndParent,lpDialogFunc) CreateDialogParamA(hInstance,lpName,hWndParent,lpDialogFunc,0L) +#define CreateDialogW(hInstance,lpName,hWndParent,lpDialogFunc) CreateDialogParamW(hInstance,lpName,hWndParent,lpDialogFunc,0L) +#define CreateDialogIndirectA(hInstance,lpTemplate,hWndParent,lpDialogFunc) CreateDialogIndirectParamA(hInstance,lpTemplate,hWndParent,lpDialogFunc,0L) +#define CreateDialogIndirectW(hInstance,lpTemplate,hWndParent,lpDialogFunc) CreateDialogIndirectParamW(hInstance,lpTemplate,hWndParent,lpDialogFunc,0L) + WINUSERAPI INT_PTR WINAPI DialogBoxParamA(HINSTANCE hInstance,LPCSTR lpTemplateName,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); + WINUSERAPI INT_PTR WINAPI DialogBoxParamW(HINSTANCE hInstance,LPCWSTR lpTemplateName,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); + WINUSERAPI INT_PTR WINAPI DialogBoxIndirectParamA(HINSTANCE hInstance,LPCDLGTEMPLATEA hDialogTemplate,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); + WINUSERAPI INT_PTR WINAPI DialogBoxIndirectParamW(HINSTANCE hInstance,LPCDLGTEMPLATEW hDialogTemplate,HWND hWndParent,DLGPROC lpDialogFunc,LPARAM dwInitParam); +#define DialogBoxA(hInstance,lpTemplate,hWndParent,lpDialogFunc) DialogBoxParamA(hInstance,lpTemplate,hWndParent,lpDialogFunc,0L) +#define DialogBoxW(hInstance,lpTemplate,hWndParent,lpDialogFunc) DialogBoxParamW(hInstance,lpTemplate,hWndParent,lpDialogFunc,0L) +#define DialogBoxIndirectA(hInstance,lpTemplate,hWndParent,lpDialogFunc) DialogBoxIndirectParamA(hInstance,lpTemplate,hWndParent,lpDialogFunc,0L) +#define DialogBoxIndirectW(hInstance,lpTemplate,hWndParent,lpDialogFunc) DialogBoxIndirectParamW(hInstance,lpTemplate,hWndParent,lpDialogFunc,0L) + WINUSERAPI WINBOOL WINAPI EndDialog(HWND hDlg,INT_PTR nResult); + WINUSERAPI HWND WINAPI GetDlgItem(HWND hDlg,int nIDDlgItem); + WINUSERAPI WINBOOL WINAPI SetDlgItemInt(HWND hDlg,int nIDDlgItem,UINT uValue,WINBOOL bSigned); + WINUSERAPI UINT WINAPI GetDlgItemInt(HWND hDlg,int nIDDlgItem,WINBOOL *lpTranslated,WINBOOL bSigned); + WINUSERAPI WINBOOL WINAPI SetDlgItemTextA(HWND hDlg,int nIDDlgItem,LPCSTR lpString); + WINUSERAPI WINBOOL WINAPI SetDlgItemTextW(HWND hDlg,int nIDDlgItem,LPCWSTR lpString); + WINUSERAPI UINT WINAPI GetDlgItemTextA(HWND hDlg,int nIDDlgItem,LPSTR lpString,int cchMax); + WINUSERAPI UINT WINAPI GetDlgItemTextW(HWND hDlg,int nIDDlgItem,LPWSTR lpString,int cchMax); + WINUSERAPI WINBOOL WINAPI CheckDlgButton(HWND hDlg,int nIDButton,UINT uCheck); + WINUSERAPI WINBOOL WINAPI CheckRadioButton(HWND hDlg,int nIDFirstButton,int nIDLastButton,int nIDCheckButton); + WINUSERAPI UINT WINAPI IsDlgButtonChecked(HWND hDlg,int nIDButton); + WINUSERAPI LRESULT WINAPI SendDlgItemMessageA(HWND hDlg,int nIDDlgItem,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI SendDlgItemMessageW(HWND hDlg,int nIDDlgItem,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI HWND WINAPI GetNextDlgGroupItem(HWND hDlg,HWND hCtl,WINBOOL bPrevious); + WINUSERAPI HWND WINAPI GetNextDlgTabItem(HWND hDlg,HWND hCtl,WINBOOL bPrevious); + WINUSERAPI int WINAPI GetDlgCtrlID(HWND hWnd); + WINUSERAPI long WINAPI GetDialogBaseUnits(VOID); + WINUSERAPI LRESULT WINAPI DefDlgProcA(HWND hDlg,UINT Msg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI DefDlgProcW(HWND hDlg,UINT Msg,WPARAM wParam,LPARAM lParam); + +#define DLGWINDOWEXTRA 30 +#endif + +#ifndef NOMSG + +#ifdef UNICODE +#define CallMsgFilter CallMsgFilterW +#else +#define CallMsgFilter CallMsgFilterA +#endif + + WINUSERAPI WINBOOL WINAPI CallMsgFilterA(LPMSG lpMsg,int nCode); + WINUSERAPI WINBOOL WINAPI CallMsgFilterW(LPMSG lpMsg,int nCode); +#endif + +#ifndef NOCLIPBOARD + +#ifdef UNICODE +#define RegisterClipboardFormat RegisterClipboardFormatW +#define GetClipboardFormatName GetClipboardFormatNameW +#else +#define RegisterClipboardFormat RegisterClipboardFormatA +#define GetClipboardFormatName GetClipboardFormatNameA +#endif + + WINUSERAPI WINBOOL WINAPI OpenClipboard(HWND hWndNewOwner); + WINUSERAPI WINBOOL WINAPI CloseClipboard(VOID); + WINUSERAPI DWORD WINAPI GetClipboardSequenceNumber(VOID); + WINUSERAPI HWND WINAPI GetClipboardOwner(VOID); + WINUSERAPI HWND WINAPI SetClipboardViewer(HWND hWndNewViewer); + WINUSERAPI HWND WINAPI GetClipboardViewer(VOID); + WINUSERAPI WINBOOL WINAPI ChangeClipboardChain(HWND hWndRemove,HWND hWndNewNext); + WINUSERAPI HANDLE WINAPI SetClipboardData(UINT uFormat,HANDLE hMem); + WINUSERAPI HANDLE WINAPI GetClipboardData(UINT uFormat); + WINUSERAPI UINT WINAPI RegisterClipboardFormatA(LPCSTR lpszFormat); + WINUSERAPI UINT WINAPI RegisterClipboardFormatW(LPCWSTR lpszFormat); + WINUSERAPI int WINAPI CountClipboardFormats(VOID); + WINUSERAPI UINT WINAPI EnumClipboardFormats(UINT format); + WINUSERAPI int WINAPI GetClipboardFormatNameA(UINT format,LPSTR lpszFormatName,int cchMaxCount); + WINUSERAPI int WINAPI GetClipboardFormatNameW(UINT format,LPWSTR lpszFormatName,int cchMaxCount); + WINUSERAPI WINBOOL WINAPI EmptyClipboard(VOID); + WINUSERAPI WINBOOL WINAPI IsClipboardFormatAvailable(UINT format); + WINUSERAPI int WINAPI GetPriorityClipboardFormat(UINT *paFormatPriorityList,int cFormats); + WINUSERAPI HWND WINAPI GetOpenClipboardWindow(VOID); +#endif + +#ifdef UNICODE +#define CharToOem CharToOemW +#define OemToChar OemToCharW +#define CharToOemBuff CharToOemBuffW +#define OemToCharBuff OemToCharBuffW +#define CharUpper CharUpperW +#define CharUpperBuff CharUpperBuffW +#define CharLower CharLowerW +#define CharLowerBuff CharLowerBuffW +#define CharNext CharNextW +#define CharPrev CharPrevW +#else +#define CharToOem CharToOemA +#define OemToChar OemToCharA +#define CharToOemBuff CharToOemBuffA +#define OemToCharBuff OemToCharBuffA +#define CharUpper CharUpperA +#define CharUpperBuff CharUpperBuffA +#define CharLower CharLowerA +#define CharLowerBuff CharLowerBuffA +#define CharNext CharNextA +#define CharPrev CharPrevA +#endif + + WINUSERAPI WINBOOL WINAPI CharToOemA(LPCSTR lpszSrc,LPSTR lpszDst); + WINUSERAPI WINBOOL WINAPI CharToOemW(LPCWSTR lpszSrc,LPSTR lpszDst); + WINUSERAPI WINBOOL WINAPI OemToCharA(LPCSTR lpszSrc,LPSTR lpszDst); + WINUSERAPI WINBOOL WINAPI OemToCharW(LPCSTR lpszSrc,LPWSTR lpszDst); + WINUSERAPI WINBOOL WINAPI CharToOemBuffA(LPCSTR lpszSrc,LPSTR lpszDst,DWORD cchDstLength); + WINUSERAPI WINBOOL WINAPI CharToOemBuffW(LPCWSTR lpszSrc,LPSTR lpszDst,DWORD cchDstLength); + WINUSERAPI WINBOOL WINAPI OemToCharBuffA(LPCSTR lpszSrc,LPSTR lpszDst,DWORD cchDstLength); + WINUSERAPI WINBOOL WINAPI OemToCharBuffW(LPCSTR lpszSrc,LPWSTR lpszDst,DWORD cchDstLength); + WINUSERAPI LPSTR WINAPI CharUpperA(LPSTR lpsz); + WINUSERAPI LPWSTR WINAPI CharUpperW(LPWSTR lpsz); + WINUSERAPI DWORD WINAPI CharUpperBuffA(LPSTR lpsz,DWORD cchLength); + WINUSERAPI DWORD WINAPI CharUpperBuffW(LPWSTR lpsz,DWORD cchLength); + WINUSERAPI LPSTR WINAPI CharLowerA(LPSTR lpsz); + WINUSERAPI LPWSTR WINAPI CharLowerW(LPWSTR lpsz); + WINUSERAPI DWORD WINAPI CharLowerBuffA(LPSTR lpsz,DWORD cchLength); + WINUSERAPI DWORD WINAPI CharLowerBuffW(LPWSTR lpsz,DWORD cchLength); + WINUSERAPI LPSTR WINAPI CharNextA(LPCSTR lpsz); + WINUSERAPI LPWSTR WINAPI CharNextW(LPCWSTR lpsz); + WINUSERAPI LPSTR WINAPI CharPrevA(LPCSTR lpszStart,LPCSTR lpszCurrent); + WINUSERAPI LPWSTR WINAPI CharPrevW(LPCWSTR lpszStart,LPCWSTR lpszCurrent); + WINUSERAPI LPSTR WINAPI CharNextExA(WORD CodePage,LPCSTR lpCurrentChar,DWORD dwFlags); + WINUSERAPI LPSTR WINAPI CharPrevExA(WORD CodePage,LPCSTR lpStart,LPCSTR lpCurrentChar,DWORD dwFlags); + +#define AnsiToOem CharToOemA +#define OemToAnsi OemToCharA +#define AnsiToOemBuff CharToOemBuffA +#define OemToAnsiBuff OemToCharBuffA +#define AnsiUpper CharUpperA +#define AnsiUpperBuff CharUpperBuffA +#define AnsiLower CharLowerA +#define AnsiLowerBuff CharLowerBuffA +#define AnsiNext CharNextA +#define AnsiPrev CharPrevA + +#ifndef NOLANGUAGE + +#ifdef UNICODE +#define IsCharAlpha IsCharAlphaW +#define IsCharAlphaNumeric IsCharAlphaNumericW +#define IsCharUpper IsCharUpperW +#define IsCharLower IsCharLowerW +#else +#define IsCharAlpha IsCharAlphaA +#define IsCharAlphaNumeric IsCharAlphaNumericA +#define IsCharUpper IsCharUpperA +#define IsCharLower IsCharLowerA +#endif + + WINUSERAPI WINBOOL WINAPI IsCharAlphaA(CHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharAlphaW(WCHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharAlphaNumericA(CHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharAlphaNumericW(WCHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharUpperA(CHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharUpperW(WCHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharLowerA(CHAR ch); + WINUSERAPI WINBOOL WINAPI IsCharLowerW(WCHAR ch); +#endif + +#ifdef UNICODE +#define GetKeyNameText GetKeyNameTextW +#define VkKeyScan VkKeyScanW +#define VkKeyScanEx VkKeyScanExW +#else +#define GetKeyNameText GetKeyNameTextA +#define VkKeyScan VkKeyScanA +#define VkKeyScanEx VkKeyScanExA +#endif + + WINUSERAPI HWND WINAPI SetFocus(HWND hWnd); + WINUSERAPI HWND WINAPI GetActiveWindow(VOID); + WINUSERAPI HWND WINAPI GetFocus(VOID); + WINUSERAPI UINT WINAPI GetKBCodePage(VOID); + WINUSERAPI SHORT WINAPI GetKeyState(int nVirtKey); + WINUSERAPI SHORT WINAPI GetAsyncKeyState(int vKey); + WINUSERAPI WINBOOL WINAPI GetKeyboardState(PBYTE lpKeyState); + WINUSERAPI WINBOOL WINAPI SetKeyboardState(LPBYTE lpKeyState); + WINUSERAPI int WINAPI GetKeyNameTextA(LONG lParam,LPSTR lpString,int cchSize); + WINUSERAPI int WINAPI GetKeyNameTextW(LONG lParam,LPWSTR lpString,int cchSize); + WINUSERAPI int WINAPI GetKeyboardType(int nTypeFlag); + WINUSERAPI int WINAPI ToAscii(UINT uVirtKey,UINT uScanCode,CONST BYTE *lpKeyState,LPWORD lpChar,UINT uFlags); + WINUSERAPI int WINAPI ToAsciiEx(UINT uVirtKey,UINT uScanCode,CONST BYTE *lpKeyState,LPWORD lpChar,UINT uFlags,HKL dwhkl); + WINUSERAPI int WINAPI ToUnicode(UINT wVirtKey,UINT wScanCode,CONST BYTE *lpKeyState,LPWSTR pwszBuff,int cchBuff,UINT wFlags); + WINUSERAPI DWORD WINAPI OemKeyScan(WORD wOemChar); + WINUSERAPI SHORT WINAPI VkKeyScanA(CHAR ch); + WINUSERAPI SHORT WINAPI VkKeyScanW(WCHAR ch); + WINUSERAPI SHORT WINAPI VkKeyScanExA(CHAR ch,HKL dwhkl); + WINUSERAPI SHORT WINAPI VkKeyScanExW(WCHAR ch,HKL dwhkl); + +#define KEYEVENTF_EXTENDEDKEY 0x0001 +#define KEYEVENTF_KEYUP 0x0002 +#define KEYEVENTF_UNICODE 0x0004 +#define KEYEVENTF_SCANCODE 0x0008 + + WINUSERAPI VOID WINAPI keybd_event(BYTE bVk,BYTE bScan,DWORD dwFlags,ULONG_PTR dwExtraInfo); + +#define MOUSEEVENTF_MOVE 0x0001 +#define MOUSEEVENTF_LEFTDOWN 0x0002 +#define MOUSEEVENTF_LEFTUP 0x0004 +#define MOUSEEVENTF_RIGHTDOWN 0x0008 +#define MOUSEEVENTF_RIGHTUP 0x0010 +#define MOUSEEVENTF_MIDDLEDOWN 0x0020 +#define MOUSEEVENTF_MIDDLEUP 0x0040 +#define MOUSEEVENTF_XDOWN 0x0080 +#define MOUSEEVENTF_XUP 0x0100 +#define MOUSEEVENTF_WHEEL 0x0800 +#define MOUSEEVENTF_VIRTUALDESK 0x4000 +#define MOUSEEVENTF_ABSOLUTE 0x8000 + + WINUSERAPI VOID WINAPI mouse_event(DWORD dwFlags,DWORD dx,DWORD dy,DWORD dwData,ULONG_PTR dwExtraInfo); + + typedef struct tagMOUSEINPUT { + LONG dx; + LONG dy; + DWORD mouseData; + DWORD dwFlags; + DWORD time; + ULONG_PTR dwExtraInfo; + } MOUSEINPUT,*PMOUSEINPUT,*LPMOUSEINPUT; + + typedef struct tagKEYBDINPUT { + WORD wVk; + WORD wScan; + DWORD dwFlags; + DWORD time; + ULONG_PTR dwExtraInfo; + } KEYBDINPUT,*PKEYBDINPUT,*LPKEYBDINPUT; + + typedef struct tagHARDWAREINPUT { + DWORD uMsg; + WORD wParamL; + WORD wParamH; + } HARDWAREINPUT,*PHARDWAREINPUT,*LPHARDWAREINPUT; + +#define INPUT_MOUSE 0 +#define INPUT_KEYBOARD 1 +#define INPUT_HARDWARE 2 + + typedef struct tagINPUT { + DWORD type; + union { + MOUSEINPUT mi; + KEYBDINPUT ki; + HARDWAREINPUT hi; + }; + } INPUT,*PINPUT,*LPINPUT; + + WINUSERAPI UINT WINAPI SendInput(UINT cInputs,LPINPUT pInputs,int cbSize); + + typedef struct tagLASTINPUTINFO { + UINT cbSize; + DWORD dwTime; + } LASTINPUTINFO,*PLASTINPUTINFO; + +#ifdef UNICODE +#define MapVirtualKey MapVirtualKeyW +#define MapVirtualKeyEx MapVirtualKeyExW +#else +#define MapVirtualKey MapVirtualKeyA +#define MapVirtualKeyEx MapVirtualKeyExA +#endif + + WINUSERAPI WINBOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii); + WINUSERAPI UINT WINAPI MapVirtualKeyA(UINT uCode,UINT uMapType); + WINUSERAPI UINT WINAPI MapVirtualKeyW(UINT uCode,UINT uMapType); + WINUSERAPI UINT WINAPI MapVirtualKeyExA(UINT uCode,UINT uMapType,HKL dwhkl); + WINUSERAPI UINT WINAPI MapVirtualKeyExW(UINT uCode,UINT uMapType,HKL dwhkl); + WINUSERAPI WINBOOL WINAPI GetInputState(VOID); + WINUSERAPI DWORD WINAPI GetQueueStatus(UINT flags); + WINUSERAPI HWND WINAPI GetCapture(VOID); + WINUSERAPI HWND WINAPI SetCapture(HWND hWnd); + WINUSERAPI WINBOOL WINAPI ReleaseCapture(VOID); + WINUSERAPI DWORD WINAPI MsgWaitForMultipleObjects(DWORD nCount,CONST HANDLE *pHandles,WINBOOL fWaitAll,DWORD dwMilliseconds,DWORD dwWakeMask); + WINUSERAPI DWORD WINAPI MsgWaitForMultipleObjectsEx(DWORD nCount,CONST HANDLE *pHandles,DWORD dwMilliseconds,DWORD dwWakeMask,DWORD dwFlags); + +#define MWMO_WAITALL 0x0001 +#define MWMO_ALERTABLE 0x0002 +#define MWMO_INPUTAVAILABLE 0x0004 + +#define QS_KEY 0x0001 +#define QS_MOUSEMOVE 0x0002 +#define QS_MOUSEBUTTON 0x0004 +#define QS_POSTMESSAGE 0x0008 +#define QS_TIMER 0x0010 +#define QS_PAINT 0x0020 +#define QS_SENDMESSAGE 0x0040 +#define QS_HOTKEY 0x0080 +#define QS_ALLPOSTMESSAGE 0x0100 +#define QS_RAWINPUT 0x0400 +#define QS_MOUSE (QS_MOUSEMOVE | QS_MOUSEBUTTON) +#define QS_INPUT (QS_MOUSE | QS_KEY | QS_RAWINPUT) +#define QS_ALLEVENTS (QS_INPUT | QS_POSTMESSAGE | QS_TIMER | QS_PAINT | QS_HOTKEY) +#define QS_ALLINPUT (QS_INPUT | QS_POSTMESSAGE | QS_TIMER | QS_PAINT | QS_HOTKEY | QS_SENDMESSAGE) + +#define USER_TIMER_MAXIMUM 0x7FFFFFFF +#define USER_TIMER_MINIMUM 0x0000000A + +#ifdef UNICODE +#define LoadAccelerators LoadAcceleratorsW +#define CreateAcceleratorTable CreateAcceleratorTableW +#define CopyAcceleratorTable CopyAcceleratorTableW +#else +#define LoadAccelerators LoadAcceleratorsA +#define CreateAcceleratorTable CreateAcceleratorTableA +#define CopyAcceleratorTable CopyAcceleratorTableA +#endif + + WINUSERAPI UINT_PTR WINAPI SetTimer(HWND hWnd,UINT_PTR nIDEvent,UINT uElapse,TIMERPROC lpTimerFunc); + WINUSERAPI WINBOOL WINAPI KillTimer(HWND hWnd,UINT_PTR uIDEvent); + WINUSERAPI WINBOOL WINAPI IsWindowUnicode(HWND hWnd); + WINUSERAPI WINBOOL WINAPI EnableWindow(HWND hWnd,WINBOOL bEnable); + WINUSERAPI WINBOOL WINAPI IsWindowEnabled(HWND hWnd); + WINUSERAPI HACCEL WINAPI LoadAcceleratorsA(HINSTANCE hInstance,LPCSTR lpTableName); + WINUSERAPI HACCEL WINAPI LoadAcceleratorsW(HINSTANCE hInstance,LPCWSTR lpTableName); + WINUSERAPI HACCEL WINAPI CreateAcceleratorTableA(LPACCEL paccel,int cAccel); + WINUSERAPI HACCEL WINAPI CreateAcceleratorTableW(LPACCEL paccel,int cAccel); + WINUSERAPI WINBOOL WINAPI DestroyAcceleratorTable(HACCEL hAccel); + WINUSERAPI int WINAPI CopyAcceleratorTableA(HACCEL hAccelSrc,LPACCEL lpAccelDst,int cAccelEntries); + WINUSERAPI int WINAPI CopyAcceleratorTableW(HACCEL hAccelSrc,LPACCEL lpAccelDst,int cAccelEntries); + +#ifndef NOMSG + +#ifdef UNICODE +#define TranslateAccelerator TranslateAcceleratorW +#else +#define TranslateAccelerator TranslateAcceleratorA +#endif + + WINUSERAPI int WINAPI TranslateAcceleratorA(HWND hWnd,HACCEL hAccTable,LPMSG lpMsg); + WINUSERAPI int WINAPI TranslateAcceleratorW(HWND hWnd,HACCEL hAccTable,LPMSG lpMsg); +#endif + +#ifndef NOSYSMETRICS + +#define SM_CXSCREEN 0 +#define SM_CYSCREEN 1 +#define SM_CXVSCROLL 2 +#define SM_CYHSCROLL 3 +#define SM_CYCAPTION 4 +#define SM_CXBORDER 5 +#define SM_CYBORDER 6 +#define SM_CXDLGFRAME 7 +#define SM_CYDLGFRAME 8 +#define SM_CYVTHUMB 9 +#define SM_CXHTHUMB 10 +#define SM_CXICON 11 +#define SM_CYICON 12 +#define SM_CXCURSOR 13 +#define SM_CYCURSOR 14 +#define SM_CYMENU 15 +#define SM_CXFULLSCREEN 16 +#define SM_CYFULLSCREEN 17 +#define SM_CYKANJIWINDOW 18 +#define SM_MOUSEPRESENT 19 +#define SM_CYVSCROLL 20 +#define SM_CXHSCROLL 21 +#define SM_DEBUG 22 +#define SM_SWAPBUTTON 23 +#define SM_RESERVED1 24 +#define SM_RESERVED2 25 +#define SM_RESERVED3 26 +#define SM_RESERVED4 27 +#define SM_CXMIN 28 +#define SM_CYMIN 29 +#define SM_CXSIZE 30 +#define SM_CYSIZE 31 +#define SM_CXFRAME 32 +#define SM_CYFRAME 33 +#define SM_CXMINTRACK 34 +#define SM_CYMINTRACK 35 +#define SM_CXDOUBLECLK 36 +#define SM_CYDOUBLECLK 37 +#define SM_CXICONSPACING 38 +#define SM_CYICONSPACING 39 +#define SM_MENUDROPALIGNMENT 40 +#define SM_PENWINDOWS 41 +#define SM_DBCSENABLED 42 +#define SM_CMOUSEBUTTONS 43 + +#define SM_CXFIXEDFRAME SM_CXDLGFRAME +#define SM_CYFIXEDFRAME SM_CYDLGFRAME +#define SM_CXSIZEFRAME SM_CXFRAME +#define SM_CYSIZEFRAME SM_CYFRAME + +#define SM_SECURE 44 +#define SM_CXEDGE 45 +#define SM_CYEDGE 46 +#define SM_CXMINSPACING 47 +#define SM_CYMINSPACING 48 +#define SM_CXSMICON 49 +#define SM_CYSMICON 50 +#define SM_CYSMCAPTION 51 +#define SM_CXSMSIZE 52 +#define SM_CYSMSIZE 53 +#define SM_CXMENUSIZE 54 +#define SM_CYMENUSIZE 55 +#define SM_ARRANGE 56 +#define SM_CXMINIMIZED 57 +#define SM_CYMINIMIZED 58 +#define SM_CXMAXTRACK 59 +#define SM_CYMAXTRACK 60 +#define SM_CXMAXIMIZED 61 +#define SM_CYMAXIMIZED 62 +#define SM_NETWORK 63 +#define SM_CLEANBOOT 67 +#define SM_CXDRAG 68 +#define SM_CYDRAG 69 +#define SM_SHOWSOUNDS 70 +#define SM_CXMENUCHECK 71 +#define SM_CYMENUCHECK 72 +#define SM_SLOWMACHINE 73 +#define SM_MIDEASTENABLED 74 +#define SM_MOUSEWHEELPRESENT 75 +#define SM_XVIRTUALSCREEN 76 +#define SM_YVIRTUALSCREEN 77 +#define SM_CXVIRTUALSCREEN 78 +#define SM_CYVIRTUALSCREEN 79 +#define SM_CMONITORS 80 +#define SM_SAMEDISPLAYFORMAT 81 +#define SM_IMMENABLED 82 +#define SM_CXFOCUSBORDER 83 +#define SM_CYFOCUSBORDER 84 +#define SM_TABLETPC 86 +#define SM_MEDIACENTER 87 +#define SM_STARTER 88 +#define SM_SERVERR2 89 +#define SM_CMETRICS 90 +#define SM_REMOTESESSION 0x1000 +#define SM_SHUTTINGDOWN 0x2000 +#define SM_REMOTECONTROL 0x2001 +#define SM_CARETBLINKINGENABLED 0x2002 + + WINUSERAPI int WINAPI GetSystemMetrics(int nIndex); +#endif + +#ifndef NOMENUS + +#ifdef UNICODE +#define LoadMenu LoadMenuW +#define LoadMenuIndirect LoadMenuIndirectW +#define ChangeMenu ChangeMenuW +#define GetMenuString GetMenuStringW +#define InsertMenu InsertMenuW +#define AppendMenu AppendMenuW +#define ModifyMenu ModifyMenuW +#else +#define LoadMenu LoadMenuA +#define LoadMenuIndirect LoadMenuIndirectA +#define ChangeMenu ChangeMenuA +#define GetMenuString GetMenuStringA +#define InsertMenu InsertMenuA +#define AppendMenu AppendMenuA +#define ModifyMenu ModifyMenuA +#endif + + WINUSERAPI HMENU WINAPI LoadMenuA(HINSTANCE hInstance,LPCSTR lpMenuName); + WINUSERAPI HMENU WINAPI LoadMenuW(HINSTANCE hInstance,LPCWSTR lpMenuName); + WINUSERAPI HMENU WINAPI LoadMenuIndirectA(CONST MENUTEMPLATEA *lpMenuTemplate); + WINUSERAPI HMENU WINAPI LoadMenuIndirectW(CONST MENUTEMPLATEW *lpMenuTemplate); + WINUSERAPI HMENU WINAPI GetMenu(HWND hWnd); + WINUSERAPI WINBOOL WINAPI SetMenu(HWND hWnd,HMENU hMenu); + WINUSERAPI WINBOOL WINAPI ChangeMenuA(HMENU hMenu,UINT cmd,LPCSTR lpszNewItem,UINT cmdInsert,UINT flags); + WINUSERAPI WINBOOL WINAPI ChangeMenuW(HMENU hMenu,UINT cmd,LPCWSTR lpszNewItem,UINT cmdInsert,UINT flags); + WINUSERAPI WINBOOL WINAPI HiliteMenuItem(HWND hWnd,HMENU hMenu,UINT uIDHiliteItem,UINT uHilite); + WINUSERAPI int WINAPI GetMenuStringA(HMENU hMenu,UINT uIDItem,LPSTR lpString,int cchMax,UINT flags); + WINUSERAPI int WINAPI GetMenuStringW(HMENU hMenu,UINT uIDItem,LPWSTR lpString,int cchMax,UINT flags); + WINUSERAPI UINT WINAPI GetMenuState(HMENU hMenu,UINT uId,UINT uFlags); + WINUSERAPI WINBOOL WINAPI DrawMenuBar(HWND hWnd); + +#define PMB_ACTIVE 0x00000001 + + WINUSERAPI HMENU WINAPI GetSystemMenu(HWND hWnd,WINBOOL bRevert); + WINUSERAPI HMENU WINAPI CreateMenu(VOID); + WINUSERAPI HMENU WINAPI CreatePopupMenu(VOID); + WINUSERAPI WINBOOL WINAPI DestroyMenu(HMENU hMenu); + WINUSERAPI DWORD WINAPI CheckMenuItem(HMENU hMenu,UINT uIDCheckItem,UINT uCheck); + WINUSERAPI WINBOOL WINAPI EnableMenuItem(HMENU hMenu,UINT uIDEnableItem,UINT uEnable); + WINUSERAPI HMENU WINAPI GetSubMenu(HMENU hMenu,int nPos); + WINUSERAPI UINT WINAPI GetMenuItemID(HMENU hMenu,int nPos); + WINUSERAPI int WINAPI GetMenuItemCount(HMENU hMenu); + WINUSERAPI WINBOOL WINAPI InsertMenuA(HMENU hMenu,UINT uPosition,UINT uFlags,UINT_PTR uIDNewItem,LPCSTR lpNewItem); + WINUSERAPI WINBOOL WINAPI InsertMenuW(HMENU hMenu,UINT uPosition,UINT uFlags,UINT_PTR uIDNewItem,LPCWSTR lpNewItem); + WINUSERAPI WINBOOL WINAPI AppendMenuA(HMENU hMenu,UINT uFlags,UINT_PTR uIDNewItem,LPCSTR lpNewItem); + WINUSERAPI WINBOOL WINAPI AppendMenuW(HMENU hMenu,UINT uFlags,UINT_PTR uIDNewItem,LPCWSTR lpNewItem); + WINUSERAPI WINBOOL WINAPI ModifyMenuA(HMENU hMnu,UINT uPosition,UINT uFlags,UINT_PTR uIDNewItem,LPCSTR lpNewItem); + WINUSERAPI WINBOOL WINAPI ModifyMenuW(HMENU hMnu,UINT uPosition,UINT uFlags,UINT_PTR uIDNewItem,LPCWSTR lpNewItem); + WINUSERAPI WINBOOL WINAPI RemoveMenu(HMENU hMenu,UINT uPosition,UINT uFlags); + WINUSERAPI WINBOOL WINAPI DeleteMenu(HMENU hMenu,UINT uPosition,UINT uFlags); + WINUSERAPI WINBOOL WINAPI SetMenuItemBitmaps(HMENU hMenu,UINT uPosition,UINT uFlags,HBITMAP hBitmapUnchecked,HBITMAP hBitmapChecked); + WINUSERAPI LONG WINAPI GetMenuCheckMarkDimensions(VOID); + WINUSERAPI WINBOOL WINAPI TrackPopupMenu(HMENU hMenu,UINT uFlags,int x,int y,int nReserved,HWND hWnd,CONST RECT *prcRect); + +#define MNC_IGNORE 0 +#define MNC_CLOSE 1 +#define MNC_EXECUTE 2 +#define MNC_SELECT 3 + + typedef struct tagTPMPARAMS { + UINT cbSize; + RECT rcExclude; + } TPMPARAMS; + + typedef TPMPARAMS *LPTPMPARAMS; + + WINUSERAPI WINBOOL WINAPI TrackPopupMenuEx(HMENU,UINT,int,int,HWND,LPTPMPARAMS); + +#define MNS_NOCHECK 0x80000000 +#define MNS_MODELESS 0x40000000 +#define MNS_DRAGDROP 0x20000000 +#define MNS_AUTODISMISS 0x10000000 +#define MNS_NOTIFYBYPOS 0x08000000 +#define MNS_CHECKORBMP 0x04000000 + +#define MIM_MAXHEIGHT 0x00000001 +#define MIM_BACKGROUND 0x00000002 +#define MIM_HELPID 0x00000004 +#define MIM_MENUDATA 0x00000008 +#define MIM_STYLE 0x00000010 +#define MIM_APPLYTOSUBMENUS 0x80000000 + + typedef struct tagMENUINFO { + DWORD cbSize; + DWORD fMask; + DWORD dwStyle; + UINT cyMax; + HBRUSH hbrBack; + DWORD dwContextHelpID; + ULONG_PTR dwMenuData; + } MENUINFO,*LPMENUINFO; + + typedef MENUINFO CONST *LPCMENUINFO; + + WINUSERAPI WINBOOL WINAPI GetMenuInfo(HMENU,LPMENUINFO); + WINUSERAPI WINBOOL WINAPI SetMenuInfo(HMENU,LPCMENUINFO); + WINUSERAPI WINBOOL WINAPI EndMenu(VOID); + +#define MND_CONTINUE 0 +#define MND_ENDMENU 1 + + typedef struct tagMENUGETOBJECTINFO { + DWORD dwFlags; + UINT uPos; + HMENU hmenu; + PVOID riid; + PVOID pvObj; + } MENUGETOBJECTINFO,*PMENUGETOBJECTINFO; + +#define MNGOF_TOPGAP 0x00000001 +#define MNGOF_BOTTOMGAP 0x00000002 + +#define MNGO_NOINTERFACE 0x00000000 +#define MNGO_NOERROR 0x00000001 + +#define MIIM_STATE 0x00000001 +#define MIIM_ID 0x00000002 +#define MIIM_SUBMENU 0x00000004 +#define MIIM_CHECKMARKS 0x00000008 +#define MIIM_TYPE 0x00000010 +#define MIIM_DATA 0x00000020 + +#define MIIM_STRING 0x00000040 +#define MIIM_BITMAP 0x00000080 +#define MIIM_FTYPE 0x00000100 + +#define HBMMENU_CALLBACK ((HBITMAP) -1) +#define HBMMENU_SYSTEM ((HBITMAP) 1) +#define HBMMENU_MBAR_RESTORE ((HBITMAP) 2) +#define HBMMENU_MBAR_MINIMIZE ((HBITMAP) 3) +#define HBMMENU_MBAR_CLOSE ((HBITMAP) 5) +#define HBMMENU_MBAR_CLOSE_D ((HBITMAP) 6) +#define HBMMENU_MBAR_MINIMIZE_D ((HBITMAP) 7) +#define HBMMENU_POPUP_CLOSE ((HBITMAP) 8) +#define HBMMENU_POPUP_RESTORE ((HBITMAP) 9) +#define HBMMENU_POPUP_MAXIMIZE ((HBITMAP) 10) +#define HBMMENU_POPUP_MINIMIZE ((HBITMAP) 11) + + typedef struct tagMENUITEMINFOA { + UINT cbSize; + UINT fMask; + UINT fType; + UINT fState; + UINT wID; + HMENU hSubMenu; + HBITMAP hbmpChecked; + HBITMAP hbmpUnchecked; + ULONG_PTR dwItemData; + LPSTR dwTypeData; + UINT cch; + HBITMAP hbmpItem; + } MENUITEMINFOA,*LPMENUITEMINFOA; + + typedef struct tagMENUITEMINFOW { + UINT cbSize; + UINT fMask; + UINT fType; + UINT fState; + UINT wID; + HMENU hSubMenu; + HBITMAP hbmpChecked; + HBITMAP hbmpUnchecked; + ULONG_PTR dwItemData; + LPWSTR dwTypeData; + UINT cch; + HBITMAP hbmpItem; + } MENUITEMINFOW,*LPMENUITEMINFOW; + +#ifdef UNICODE + typedef MENUITEMINFOW MENUITEMINFO; + typedef LPMENUITEMINFOW LPMENUITEMINFO; +#else + typedef MENUITEMINFOA MENUITEMINFO; + typedef LPMENUITEMINFOA LPMENUITEMINFO; +#endif + typedef MENUITEMINFOA CONST *LPCMENUITEMINFOA; + typedef MENUITEMINFOW CONST *LPCMENUITEMINFOW; +#ifdef UNICODE + typedef LPCMENUITEMINFOW LPCMENUITEMINFO; +#else + typedef LPCMENUITEMINFOA LPCMENUITEMINFO; +#endif + +#ifdef UNICODE +#define InsertMenuItem InsertMenuItemW +#define GetMenuItemInfo GetMenuItemInfoW +#define SetMenuItemInfo SetMenuItemInfoW +#else +#define InsertMenuItem InsertMenuItemA +#define GetMenuItemInfo GetMenuItemInfoA +#define SetMenuItemInfo SetMenuItemInfoA +#endif + + WINUSERAPI WINBOOL WINAPI InsertMenuItemA(HMENU hmenu,UINT item,WINBOOL fByPosition,LPCMENUITEMINFOA lpmi); + WINUSERAPI WINBOOL WINAPI InsertMenuItemW(HMENU hmenu,UINT item,WINBOOL fByPosition,LPCMENUITEMINFOW lpmi); + WINUSERAPI WINBOOL WINAPI GetMenuItemInfoA(HMENU hmenu,UINT item,WINBOOL fByPosition,LPMENUITEMINFOA lpmii); + WINUSERAPI WINBOOL WINAPI GetMenuItemInfoW(HMENU hmenu,UINT item,WINBOOL fByPosition,LPMENUITEMINFOW lpmii); + WINUSERAPI WINBOOL WINAPI SetMenuItemInfoA(HMENU hmenu,UINT item,WINBOOL fByPositon,LPCMENUITEMINFOA lpmii); + WINUSERAPI WINBOOL WINAPI SetMenuItemInfoW(HMENU hmenu,UINT item,WINBOOL fByPositon,LPCMENUITEMINFOW lpmii); + +#define GMDI_USEDISABLED 0x0001L +#define GMDI_GOINTOPOPUPS 0x0002L + + WINUSERAPI UINT WINAPI GetMenuDefaultItem(HMENU hMenu,UINT fByPos,UINT gmdiFlags); + WINUSERAPI WINBOOL WINAPI SetMenuDefaultItem(HMENU hMenu,UINT uItem,UINT fByPos); + WINUSERAPI WINBOOL WINAPI GetMenuItemRect(HWND hWnd,HMENU hMenu,UINT uItem,LPRECT lprcItem); + WINUSERAPI int WINAPI MenuItemFromPoint(HWND hWnd,HMENU hMenu,POINT ptScreen); + +#define TPM_LEFTBUTTON 0x0000L +#define TPM_RIGHTBUTTON 0x0002L +#define TPM_LEFTALIGN 0x0000L +#define TPM_CENTERALIGN 0x0004L +#define TPM_RIGHTALIGN 0x0008L +#define TPM_TOPALIGN 0x0000L +#define TPM_VCENTERALIGN 0x0010L +#define TPM_BOTTOMALIGN 0x0020L + +#define TPM_HORIZONTAL 0x0000L +#define TPM_VERTICAL 0x0040L +#define TPM_NONOTIFY 0x0080L +#define TPM_RETURNCMD 0x0100L +#define TPM_RECURSE 0x0001L +#define TPM_HORPOSANIMATION 0x0400L +#define TPM_HORNEGANIMATION 0x0800L +#define TPM_VERPOSANIMATION 0x1000L +#define TPM_VERNEGANIMATION 0x2000L +#define TPM_NOANIMATION 0x4000L +#define TPM_LAYOUTRTL 0x8000L +#endif + + typedef struct tagDROPSTRUCT { + HWND hwndSource; + HWND hwndSink; + DWORD wFmt; + ULONG_PTR dwData; + POINT ptDrop; + DWORD dwControlData; + } DROPSTRUCT,*PDROPSTRUCT,*LPDROPSTRUCT; + +#define DOF_EXECUTABLE 0x8001 +#define DOF_DOCUMENT 0x8002 +#define DOF_DIRECTORY 0x8003 +#define DOF_MULTIPLE 0x8004 +#define DOF_PROGMAN 0x0001 +#define DOF_SHELLDATA 0x0002 + +#define DO_DROPFILE 0x454C4946L +#define DO_PRINTFILE 0x544E5250L + + WINUSERAPI DWORD WINAPI DragObject(HWND hwndParent,HWND hwndFrom,UINT fmt,ULONG_PTR data,HCURSOR hcur); + WINUSERAPI WINBOOL WINAPI DragDetect(HWND hwnd,POINT pt); + WINUSERAPI WINBOOL WINAPI DrawIcon(HDC hDC,int X,int Y,HICON hIcon); + +#ifndef NODRAWTEXT + +#define DT_TOP 0x00000000 +#define DT_LEFT 0x00000000 +#define DT_CENTER 0x00000001 +#define DT_RIGHT 0x00000002 +#define DT_VCENTER 0x00000004 +#define DT_BOTTOM 0x00000008 +#define DT_WORDBREAK 0x00000010 +#define DT_SINGLELINE 0x00000020 +#define DT_EXPANDTABS 0x00000040 +#define DT_TABSTOP 0x00000080 +#define DT_NOCLIP 0x00000100 +#define DT_EXTERNALLEADING 0x00000200 +#define DT_CALCRECT 0x00000400 +#define DT_NOPREFIX 0x00000800 +#define DT_INTERNAL 0x00001000 + +#define DT_EDITCONTROL 0x00002000 +#define DT_PATH_ELLIPSIS 0x00004000 +#define DT_END_ELLIPSIS 0x00008000 +#define DT_MODIFYSTRING 0x00010000 +#define DT_RTLREADING 0x00020000 +#define DT_WORD_ELLIPSIS 0x00040000 +#define DT_NOFULLWIDTHCHARBREAK 0x00080000 +#define DT_HIDEPREFIX 0x00100000 +#define DT_PREFIXONLY 0x00200000 + + typedef struct tagDRAWTEXTPARAMS { + UINT cbSize; + int iTabLength; + int iLeftMargin; + int iRightMargin; + UINT uiLengthDrawn; + } DRAWTEXTPARAMS,*LPDRAWTEXTPARAMS; + +#ifdef UNICODE +#define DrawText DrawTextW +#define DrawTextEx DrawTextExW +#else +#define DrawText DrawTextA +#define DrawTextEx DrawTextExA +#endif + + WINUSERAPI int WINAPI DrawTextA(HDC hdc,LPCSTR lpchText,int cchText,LPRECT lprc,UINT format); + WINUSERAPI int WINAPI DrawTextW(HDC hdc,LPCWSTR lpchText,int cchText,LPRECT lprc,UINT format); + WINUSERAPI int WINAPI DrawTextExA(HDC hdc,LPSTR lpchText,int cchText,LPRECT lprc,UINT format,LPDRAWTEXTPARAMS lpdtp); + WINUSERAPI int WINAPI DrawTextExW(HDC hdc,LPWSTR lpchText,int cchText,LPRECT lprc,UINT format,LPDRAWTEXTPARAMS lpdtp); +#endif + +#ifdef UNICODE +#define GrayString GrayStringW +#define DrawState DrawStateW +#define TabbedTextOut TabbedTextOutW +#define GetTabbedTextExtent GetTabbedTextExtentW +#else +#define GrayString GrayStringA +#define DrawState DrawStateA +#define TabbedTextOut TabbedTextOutA +#define GetTabbedTextExtent GetTabbedTextExtentA +#endif + + WINUSERAPI WINBOOL WINAPI GrayStringA(HDC hDC,HBRUSH hBrush,GRAYSTRINGPROC lpOutputFunc,LPARAM lpData,int nCount,int X,int Y,int nWidth,int nHeight); + WINUSERAPI WINBOOL WINAPI GrayStringW(HDC hDC,HBRUSH hBrush,GRAYSTRINGPROC lpOutputFunc,LPARAM lpData,int nCount,int X,int Y,int nWidth,int nHeight); + +#define DST_COMPLEX 0x0000 +#define DST_TEXT 0x0001 +#define DST_PREFIXTEXT 0x0002 +#define DST_ICON 0x0003 +#define DST_BITMAP 0x0004 + +#define DSS_NORMAL 0x0000 +#define DSS_UNION 0x0010 +#define DSS_DISABLED 0x0020 +#define DSS_MONO 0x0080 +#define DSS_HIDEPREFIX 0x0200 +#define DSS_PREFIXONLY 0x0400 +#define DSS_RIGHT 0x8000 + + WINUSERAPI WINBOOL WINAPI DrawStateA(HDC hdc,HBRUSH hbrFore,DRAWSTATEPROC qfnCallBack,LPARAM lData,WPARAM wData,int x,int y,int cx,int cy,UINT uFlags); + WINUSERAPI WINBOOL WINAPI DrawStateW(HDC hdc,HBRUSH hbrFore,DRAWSTATEPROC qfnCallBack,LPARAM lData,WPARAM wData,int x,int y,int cx,int cy,UINT uFlags); + WINUSERAPI LONG WINAPI TabbedTextOutA(HDC hdc,int x,int y,LPCSTR lpString,int chCount,int nTabPositions,CONST INT *lpnTabStopPositions,int nTabOrigin); + WINUSERAPI LONG WINAPI TabbedTextOutW(HDC hdc,int x,int y,LPCWSTR lpString,int chCount,int nTabPositions,CONST INT *lpnTabStopPositions,int nTabOrigin); + WINUSERAPI DWORD WINAPI GetTabbedTextExtentA(HDC hdc,LPCSTR lpString,int chCount,int nTabPositions,CONST INT *lpnTabStopPositions); + WINUSERAPI DWORD WINAPI GetTabbedTextExtentW(HDC hdc,LPCWSTR lpString,int chCount,int nTabPositions,CONST INT *lpnTabStopPositions); + WINUSERAPI WINBOOL WINAPI UpdateWindow(HWND hWnd); + WINUSERAPI HWND WINAPI SetActiveWindow(HWND hWnd); + WINUSERAPI HWND WINAPI GetForegroundWindow(VOID); + WINUSERAPI WINBOOL WINAPI PaintDesktop(HDC hdc); + WINUSERAPI VOID WINAPI SwitchToThisWindow(HWND hwnd,WINBOOL fUnknown); + WINUSERAPI WINBOOL WINAPI SetForegroundWindow(HWND hWnd); + WINUSERAPI WINBOOL WINAPI AllowSetForegroundWindow(DWORD dwProcessId); + +#define ASFW_ANY ((DWORD)-1) + + WINUSERAPI WINBOOL WINAPI LockSetForegroundWindow(UINT uLockCode); + +#define LSFW_LOCK 1 +#define LSFW_UNLOCK 2 + + WINUSERAPI HWND WINAPI WindowFromDC(HDC hDC); + WINUSERAPI HDC WINAPI GetDC(HWND hWnd); + WINUSERAPI HDC WINAPI GetDCEx(HWND hWnd,HRGN hrgnClip,DWORD flags); + +#define DCX_WINDOW 0x00000001L +#define DCX_CACHE 0x00000002L +#define DCX_NORESETATTRS 0x00000004L +#define DCX_CLIPCHILDREN 0x00000008L +#define DCX_CLIPSIBLINGS 0x00000010L +#define DCX_PARENTCLIP 0x00000020L +#define DCX_EXCLUDERGN 0x00000040L +#define DCX_INTERSECTRGN 0x00000080L +#define DCX_EXCLUDEUPDATE 0x00000100L +#define DCX_INTERSECTUPDATE 0x00000200L +#define DCX_LOCKWINDOWUPDATE 0x00000400L + +#define DCX_VALIDATE 0x00200000L + + WINUSERAPI HDC WINAPI GetWindowDC(HWND hWnd); + WINUSERAPI int WINAPI ReleaseDC(HWND hWnd,HDC hDC); + WINUSERAPI HDC WINAPI BeginPaint(HWND hWnd,LPPAINTSTRUCT lpPaint); + WINUSERAPI WINBOOL WINAPI EndPaint(HWND hWnd,CONST PAINTSTRUCT *lpPaint); + WINUSERAPI WINBOOL WINAPI GetUpdateRect(HWND hWnd,LPRECT lpRect,WINBOOL bErase); + WINUSERAPI int WINAPI GetUpdateRgn(HWND hWnd,HRGN hRgn,WINBOOL bErase); + WINUSERAPI int WINAPI SetWindowRgn(HWND hWnd,HRGN hRgn,WINBOOL bRedraw); + WINUSERAPI int WINAPI GetWindowRgn(HWND hWnd,HRGN hRgn); + WINUSERAPI int WINAPI GetWindowRgnBox(HWND hWnd,LPRECT lprc); + WINUSERAPI int WINAPI ExcludeUpdateRgn(HDC hDC,HWND hWnd); + WINUSERAPI WINBOOL WINAPI InvalidateRect(HWND hWnd,CONST RECT *lpRect,WINBOOL bErase); + WINUSERAPI WINBOOL WINAPI ValidateRect(HWND hWnd,CONST RECT *lpRect); + WINUSERAPI WINBOOL WINAPI InvalidateRgn(HWND hWnd,HRGN hRgn,WINBOOL bErase); + WINUSERAPI WINBOOL WINAPI ValidateRgn(HWND hWnd,HRGN hRgn); + WINUSERAPI WINBOOL WINAPI RedrawWindow(HWND hWnd,CONST RECT *lprcUpdate,HRGN hrgnUpdate,UINT flags); + +#define RDW_INVALIDATE 0x0001 +#define RDW_INTERNALPAINT 0x0002 +#define RDW_ERASE 0x0004 + +#define RDW_VALIDATE 0x0008 +#define RDW_NOINTERNALPAINT 0x0010 +#define RDW_NOERASE 0x0020 + +#define RDW_NOCHILDREN 0x0040 +#define RDW_ALLCHILDREN 0x0080 + +#define RDW_UPDATENOW 0x0100 +#define RDW_ERASENOW 0x0200 + +#define RDW_FRAME 0x0400 +#define RDW_NOFRAME 0x0800 + + WINUSERAPI WINBOOL WINAPI LockWindowUpdate(HWND hWndLock); + WINUSERAPI WINBOOL WINAPI ScrollWindow(HWND hWnd,int XAmount,int YAmount,CONST RECT *lpRect,CONST RECT *lpClipRect); + WINUSERAPI WINBOOL WINAPI ScrollDC(HDC hDC,int dx,int dy,CONST RECT *lprcScroll,CONST RECT *lprcClip,HRGN hrgnUpdate,LPRECT lprcUpdate); + WINUSERAPI int WINAPI ScrollWindowEx(HWND hWnd,int dx,int dy,CONST RECT *prcScroll,CONST RECT *prcClip,HRGN hrgnUpdate,LPRECT prcUpdate,UINT flags); + +#define SW_SCROLLCHILDREN 0x0001 +#define SW_INVALIDATE 0x0002 +#define SW_ERASE 0x0004 +#define SW_SMOOTHSCROLL 0x0010 + +#ifndef NOSCROLL + WINUSERAPI int WINAPI SetScrollPos(HWND hWnd,int nBar,int nPos,WINBOOL bRedraw); + WINUSERAPI int WINAPI GetScrollPos(HWND hWnd,int nBar); + WINUSERAPI WINBOOL WINAPI SetScrollRange(HWND hWnd,int nBar,int nMinPos,int nMaxPos,WINBOOL bRedraw); + WINUSERAPI WINBOOL WINAPI GetScrollRange(HWND hWnd,int nBar,LPINT lpMinPos,LPINT lpMaxPos); + WINUSERAPI WINBOOL WINAPI ShowScrollBar(HWND hWnd,int wBar,WINBOOL bShow); + WINUSERAPI WINBOOL WINAPI EnableScrollBar(HWND hWnd,UINT wSBflags,UINT wArrows); + +#define ESB_ENABLE_BOTH 0x0000 +#define ESB_DISABLE_BOTH 0x0003 + +#define ESB_DISABLE_LEFT 0x0001 +#define ESB_DISABLE_RIGHT 0x0002 + +#define ESB_DISABLE_UP 0x0001 +#define ESB_DISABLE_DOWN 0x0002 + +#define ESB_DISABLE_LTUP ESB_DISABLE_LEFT +#define ESB_DISABLE_RTDN ESB_DISABLE_RIGHT +#endif + +#ifdef UNICODE +#define SetProp SetPropW +#define GetProp GetPropW +#define RemoveProp RemovePropW +#define EnumPropsEx EnumPropsExW +#define EnumProps EnumPropsW +#define SetWindowText SetWindowTextW +#define GetWindowText GetWindowTextW +#define GetWindowTextLength GetWindowTextLengthW +#else +#define SetProp SetPropA +#define GetProp GetPropA +#define RemoveProp RemovePropA +#define EnumPropsEx EnumPropsExA +#define EnumProps EnumPropsA +#define SetWindowText SetWindowTextA +#define GetWindowText GetWindowTextA +#define GetWindowTextLength GetWindowTextLengthA +#endif + + WINUSERAPI WINBOOL WINAPI SetPropA(HWND hWnd,LPCSTR lpString,HANDLE hData); + WINUSERAPI WINBOOL WINAPI SetPropW(HWND hWnd,LPCWSTR lpString,HANDLE hData); + WINUSERAPI HANDLE WINAPI GetPropA(HWND hWnd,LPCSTR lpString); + WINUSERAPI HANDLE WINAPI GetPropW(HWND hWnd,LPCWSTR lpString); + WINUSERAPI HANDLE WINAPI RemovePropA(HWND hWnd,LPCSTR lpString); + WINUSERAPI HANDLE WINAPI RemovePropW(HWND hWnd,LPCWSTR lpString); + WINUSERAPI int WINAPI EnumPropsExA(HWND hWnd,PROPENUMPROCEXA lpEnumFunc,LPARAM lParam); + WINUSERAPI int WINAPI EnumPropsExW(HWND hWnd,PROPENUMPROCEXW lpEnumFunc,LPARAM lParam); + WINUSERAPI int WINAPI EnumPropsA(HWND hWnd,PROPENUMPROCA lpEnumFunc); + WINUSERAPI int WINAPI EnumPropsW(HWND hWnd,PROPENUMPROCW lpEnumFunc); + WINUSERAPI WINBOOL WINAPI SetWindowTextA(HWND hWnd,LPCSTR lpString); + WINUSERAPI WINBOOL WINAPI SetWindowTextW(HWND hWnd,LPCWSTR lpString); + WINUSERAPI int WINAPI GetWindowTextA(HWND hWnd,LPSTR lpString,int nMaxCount); + WINUSERAPI int WINAPI GetWindowTextW(HWND hWnd,LPWSTR lpString,int nMaxCount); + WINUSERAPI int WINAPI GetWindowTextLengthA(HWND hWnd); + WINUSERAPI int WINAPI GetWindowTextLengthW(HWND hWnd); + WINUSERAPI WINBOOL WINAPI GetClientRect(HWND hWnd,LPRECT lpRect); + WINUSERAPI WINBOOL WINAPI GetWindowRect(HWND hWnd,LPRECT lpRect); + WINUSERAPI WINBOOL WINAPI AdjustWindowRect(LPRECT lpRect,DWORD dwStyle,WINBOOL bMenu); + WINUSERAPI WINBOOL WINAPI AdjustWindowRectEx(LPRECT lpRect,DWORD dwStyle,WINBOOL bMenu,DWORD dwExStyle); + +#define HELPINFO_WINDOW 0x0001 +#define HELPINFO_MENUITEM 0x0002 + + typedef struct tagHELPINFO { + UINT cbSize; + int iContextType; + int iCtrlId; + HANDLE hItemHandle; + DWORD_PTR dwContextId; + POINT MousePos; + } HELPINFO,*LPHELPINFO; + + WINUSERAPI WINBOOL WINAPI SetWindowContextHelpId(HWND,DWORD); + WINUSERAPI DWORD WINAPI GetWindowContextHelpId(HWND); + WINUSERAPI WINBOOL WINAPI SetMenuContextHelpId(HMENU,DWORD); + WINUSERAPI DWORD WINAPI GetMenuContextHelpId(HMENU); + +#ifndef NOMB + +#define MB_OK 0x00000000L +#define MB_OKCANCEL 0x00000001L +#define MB_ABORTRETRYIGNORE 0x00000002L +#define MB_YESNOCANCEL 0x00000003L +#define MB_YESNO 0x00000004L +#define MB_RETRYCANCEL 0x00000005L +#define MB_CANCELTRYCONTINUE 0x00000006L +#define MB_ICONHAND 0x00000010L +#define MB_ICONQUESTION 0x00000020L +#define MB_ICONEXCLAMATION 0x00000030L +#define MB_ICONASTERISK 0x00000040L +#define MB_USERICON 0x00000080L +#define MB_ICONWARNING MB_ICONEXCLAMATION +#define MB_ICONERROR MB_ICONHAND +#define MB_ICONINFORMATION MB_ICONASTERISK +#define MB_ICONSTOP MB_ICONHAND +#define MB_DEFBUTTON1 0x00000000L +#define MB_DEFBUTTON2 0x00000100L +#define MB_DEFBUTTON3 0x00000200L +#define MB_DEFBUTTON4 0x00000300L +#define MB_APPLMODAL 0x00000000L +#define MB_SYSTEMMODAL 0x00001000L +#define MB_TASKMODAL 0x00002000L +#define MB_HELP 0x00004000L +#define MB_NOFOCUS 0x00008000L +#define MB_SETFOREGROUND 0x00010000L +#define MB_DEFAULT_DESKTOP_ONLY 0x00020000L +#define MB_TOPMOST 0x00040000L +#define MB_RIGHT 0x00080000L +#define MB_RTLREADING 0x00100000L +#define MB_SERVICE_NOTIFICATION 0x00200000L +#define MB_SERVICE_NOTIFICATION_NT3X 0x00040000L +#define MB_TYPEMASK 0x0000000FL +#define MB_ICONMASK 0x000000F0L +#define MB_DEFMASK 0x00000F00L +#define MB_MODEMASK 0x00003000L +#define MB_MISCMASK 0x0000C000L + +#ifdef UNICODE +#define MessageBox MessageBoxW +#define MessageBoxEx MessageBoxExW +#else +#define MessageBox MessageBoxA +#define MessageBoxEx MessageBoxExA +#endif + + WINUSERAPI int WINAPI MessageBoxA(HWND hWnd,LPCSTR lpText,LPCSTR lpCaption,UINT uType); + WINUSERAPI int WINAPI MessageBoxW(HWND hWnd,LPCWSTR lpText,LPCWSTR lpCaption,UINT uType); + WINUSERAPI int WINAPI MessageBoxExA(HWND hWnd,LPCSTR lpText,LPCSTR lpCaption,UINT uType,WORD wLanguageId); + WINUSERAPI int WINAPI MessageBoxExW(HWND hWnd,LPCWSTR lpText,LPCWSTR lpCaption,UINT uType,WORD wLanguageId); + + typedef VOID (CALLBACK *MSGBOXCALLBACK)(LPHELPINFO lpHelpInfo); + + typedef struct tagMSGBOXPARAMSA { + UINT cbSize; + HWND hwndOwner; + HINSTANCE hInstance; + LPCSTR lpszText; + LPCSTR lpszCaption; + DWORD dwStyle; + LPCSTR lpszIcon; + DWORD_PTR dwContextHelpId; + MSGBOXCALLBACK lpfnMsgBoxCallback; + DWORD dwLanguageId; + } MSGBOXPARAMSA,*PMSGBOXPARAMSA,*LPMSGBOXPARAMSA; + + typedef struct tagMSGBOXPARAMSW { + UINT cbSize; + HWND hwndOwner; + HINSTANCE hInstance; + LPCWSTR lpszText; + LPCWSTR lpszCaption; + DWORD dwStyle; + LPCWSTR lpszIcon; + DWORD_PTR dwContextHelpId; + MSGBOXCALLBACK lpfnMsgBoxCallback; + DWORD dwLanguageId; + } MSGBOXPARAMSW,*PMSGBOXPARAMSW,*LPMSGBOXPARAMSW; + +#ifdef UNICODE + typedef MSGBOXPARAMSW MSGBOXPARAMS; + typedef PMSGBOXPARAMSW PMSGBOXPARAMS; + typedef LPMSGBOXPARAMSW LPMSGBOXPARAMS; +#else + typedef MSGBOXPARAMSA MSGBOXPARAMS; + typedef PMSGBOXPARAMSA PMSGBOXPARAMS; + typedef LPMSGBOXPARAMSA LPMSGBOXPARAMS; +#endif + +#ifdef UNICODE +#define MessageBoxIndirect MessageBoxIndirectW +#else +#define MessageBoxIndirect MessageBoxIndirectA +#endif + + WINUSERAPI int WINAPI MessageBoxIndirectA(CONST MSGBOXPARAMSA *lpmbp); + WINUSERAPI int WINAPI MessageBoxIndirectW(CONST MSGBOXPARAMSW *lpmbp); + WINUSERAPI WINBOOL WINAPI MessageBeep(UINT uType); +#endif + + WINUSERAPI int WINAPI ShowCursor(WINBOOL bShow); + WINUSERAPI WINBOOL WINAPI SetCursorPos(int X,int Y); + WINUSERAPI HCURSOR WINAPI SetCursor(HCURSOR hCursor); + WINUSERAPI WINBOOL WINAPI GetCursorPos(LPPOINT lpPoint); + WINUSERAPI WINBOOL WINAPI ClipCursor(CONST RECT *lpRect); + WINUSERAPI WINBOOL WINAPI GetClipCursor(LPRECT lpRect); + WINUSERAPI HCURSOR WINAPI GetCursor(VOID); + WINUSERAPI WINBOOL WINAPI CreateCaret(HWND hWnd,HBITMAP hBitmap,int nWidth,int nHeight); + WINUSERAPI UINT WINAPI GetCaretBlinkTime(VOID); + WINUSERAPI WINBOOL WINAPI SetCaretBlinkTime(UINT uMSeconds); + WINUSERAPI WINBOOL WINAPI DestroyCaret(VOID); + WINUSERAPI WINBOOL WINAPI HideCaret(HWND hWnd); + WINUSERAPI WINBOOL WINAPI ShowCaret(HWND hWnd); + WINUSERAPI WINBOOL WINAPI SetCaretPos(int X,int Y); + WINUSERAPI WINBOOL WINAPI GetCaretPos(LPPOINT lpPoint); + WINUSERAPI WINBOOL WINAPI ClientToScreen(HWND hWnd,LPPOINT lpPoint); + WINUSERAPI WINBOOL WINAPI ScreenToClient(HWND hWnd,LPPOINT lpPoint); + WINUSERAPI int WINAPI MapWindowPoints(HWND hWndFrom,HWND hWndTo,LPPOINT lpPoints,UINT cPoints); + WINUSERAPI HWND WINAPI WindowFromPoint(POINT Point); + WINUSERAPI HWND WINAPI ChildWindowFromPoint(HWND hWndParent,POINT Point); + +#define CWP_ALL 0x0000 +#define CWP_SKIPINVISIBLE 0x0001 +#define CWP_SKIPDISABLED 0x0002 +#define CWP_SKIPTRANSPARENT 0x0004 + + WINUSERAPI HWND WINAPI ChildWindowFromPointEx(HWND hwnd,POINT pt,UINT flags); + +#ifndef NOCOLOR + +#define CTLCOLOR_MSGBOX 0 +#define CTLCOLOR_EDIT 1 +#define CTLCOLOR_LISTBOX 2 +#define CTLCOLOR_BTN 3 +#define CTLCOLOR_DLG 4 +#define CTLCOLOR_SCROLLBAR 5 +#define CTLCOLOR_STATIC 6 +#define CTLCOLOR_MAX 7 + +#define COLOR_SCROLLBAR 0 +#define COLOR_BACKGROUND 1 +#define COLOR_ACTIVECAPTION 2 +#define COLOR_INACTIVECAPTION 3 +#define COLOR_MENU 4 +#define COLOR_WINDOW 5 +#define COLOR_WINDOWFRAME 6 +#define COLOR_MENUTEXT 7 +#define COLOR_WINDOWTEXT 8 +#define COLOR_CAPTIONTEXT 9 +#define COLOR_ACTIVEBORDER 10 +#define COLOR_INACTIVEBORDER 11 +#define COLOR_APPWORKSPACE 12 +#define COLOR_HIGHLIGHT 13 +#define COLOR_HIGHLIGHTTEXT 14 +#define COLOR_BTNFACE 15 +#define COLOR_BTNSHADOW 16 +#define COLOR_GRAYTEXT 17 +#define COLOR_BTNTEXT 18 +#define COLOR_INACTIVECAPTIONTEXT 19 +#define COLOR_BTNHIGHLIGHT 20 + +#define COLOR_3DDKSHADOW 21 +#define COLOR_3DLIGHT 22 +#define COLOR_INFOTEXT 23 +#define COLOR_INFOBK 24 + +#define COLOR_HOTLIGHT 26 +#define COLOR_GRADIENTACTIVECAPTION 27 +#define COLOR_GRADIENTINACTIVECAPTION 28 +#define COLOR_MENUHILIGHT 29 +#define COLOR_MENUBAR 30 + +#define COLOR_DESKTOP COLOR_BACKGROUND +#define COLOR_3DFACE COLOR_BTNFACE +#define COLOR_3DSHADOW COLOR_BTNSHADOW +#define COLOR_3DHIGHLIGHT COLOR_BTNHIGHLIGHT +#define COLOR_3DHILIGHT COLOR_BTNHIGHLIGHT +#define COLOR_BTNHILIGHT COLOR_BTNHIGHLIGHT + + WINUSERAPI DWORD WINAPI GetSysColor(int nIndex); + WINUSERAPI HBRUSH WINAPI GetSysColorBrush(int nIndex); + WINUSERAPI WINBOOL WINAPI SetSysColors(int cElements,CONST INT *lpaElements,CONST COLORREF *lpaRgbValues); +#endif + + WINUSERAPI WINBOOL WINAPI DrawFocusRect(HDC hDC,CONST RECT *lprc); + WINUSERAPI int WINAPI FillRect(HDC hDC,CONST RECT *lprc,HBRUSH hbr); + WINUSERAPI int WINAPI FrameRect(HDC hDC,CONST RECT *lprc,HBRUSH hbr); + WINUSERAPI WINBOOL WINAPI InvertRect(HDC hDC,CONST RECT *lprc); + WINUSERAPI WINBOOL WINAPI SetRect(LPRECT lprc,int xLeft,int yTop,int xRight,int yBottom); + WINUSERAPI WINBOOL WINAPI SetRectEmpty(LPRECT lprc); + WINUSERAPI WINBOOL WINAPI CopyRect(LPRECT lprcDst,CONST RECT *lprcSrc); + WINUSERAPI WINBOOL WINAPI InflateRect(LPRECT lprc,int dx,int dy); + WINUSERAPI WINBOOL WINAPI IntersectRect(LPRECT lprcDst,CONST RECT *lprcSrc1,CONST RECT *lprcSrc2); + WINUSERAPI WINBOOL WINAPI UnionRect(LPRECT lprcDst,CONST RECT *lprcSrc1,CONST RECT *lprcSrc2); + WINUSERAPI WINBOOL WINAPI SubtractRect(LPRECT lprcDst,CONST RECT *lprcSrc1,CONST RECT *lprcSrc2); + WINUSERAPI WINBOOL WINAPI OffsetRect(LPRECT lprc,int dx,int dy); + WINUSERAPI WINBOOL WINAPI IsRectEmpty(CONST RECT *lprc); + WINUSERAPI WINBOOL WINAPI EqualRect(CONST RECT *lprc1,CONST RECT *lprc2); + WINUSERAPI WINBOOL WINAPI PtInRect(CONST RECT *lprc,POINT pt); + +#ifndef NOWINOFFSETS + +#ifdef UNICODE +#define GetWindowLong GetWindowLongW +#define SetWindowLong SetWindowLongW +#else +#define GetWindowLong GetWindowLongA +#define SetWindowLong SetWindowLongA +#endif + + WINUSERAPI WORD WINAPI GetWindowWord(HWND hWnd,int nIndex); + WINUSERAPI WORD WINAPI SetWindowWord(HWND hWnd,int nIndex,WORD wNewWord); + WINUSERAPI LONG WINAPI GetWindowLongA(HWND hWnd,int nIndex); + WINUSERAPI LONG WINAPI GetWindowLongW(HWND hWnd,int nIndex); + WINUSERAPI LONG WINAPI SetWindowLongA(HWND hWnd,int nIndex,LONG dwNewLong); + WINUSERAPI LONG WINAPI SetWindowLongW(HWND hWnd,int nIndex,LONG dwNewLong); + +#ifdef _WIN64 + +#ifdef UNICODE +#define GetWindowLongPtr GetWindowLongPtrW +#define SetWindowLongPtr SetWindowLongPtrW +#else +#define GetWindowLongPtr GetWindowLongPtrA +#define SetWindowLongPtr SetWindowLongPtrA +#endif + + WINUSERAPI LONG_PTR WINAPI GetWindowLongPtrA(HWND hWnd,int nIndex); + WINUSERAPI LONG_PTR WINAPI GetWindowLongPtrW(HWND hWnd,int nIndex); + WINUSERAPI LONG_PTR WINAPI SetWindowLongPtrA(HWND hWnd,int nIndex,LONG_PTR dwNewLong); + WINUSERAPI LONG_PTR WINAPI SetWindowLongPtrW(HWND hWnd,int nIndex,LONG_PTR dwNewLong); +#else + +#ifdef UNICODE +#define GetWindowLongPtr GetWindowLongPtrW +#define SetWindowLongPtr SetWindowLongPtrW +#else +#define GetWindowLongPtr GetWindowLongPtrA +#define SetWindowLongPtr SetWindowLongPtrA +#endif + +#define GetWindowLongPtrA GetWindowLongA +#define GetWindowLongPtrW GetWindowLongW +#define SetWindowLongPtrA SetWindowLongA +#define SetWindowLongPtrW SetWindowLongW +#endif + +#ifdef UNICODE +#define GetClassLong GetClassLongW +#define SetClassLong SetClassLongW +#else +#define GetClassLong GetClassLongA +#define SetClassLong SetClassLongA +#endif + + WINUSERAPI WORD WINAPI GetClassWord(HWND hWnd,int nIndex); + WINUSERAPI WORD WINAPI SetClassWord(HWND hWnd,int nIndex,WORD wNewWord); + WINUSERAPI DWORD WINAPI GetClassLongA(HWND hWnd,int nIndex); + WINUSERAPI DWORD WINAPI GetClassLongW(HWND hWnd,int nIndex); + WINUSERAPI DWORD WINAPI SetClassLongA(HWND hWnd,int nIndex,LONG dwNewLong); + WINUSERAPI DWORD WINAPI SetClassLongW(HWND hWnd,int nIndex,LONG dwNewLong); + +#ifdef _WIN64 + +#ifdef UNICODE +#define GetClassLongPtr GetClassLongPtrW +#define SetClassLongPtr SetClassLongPtrW +#else +#define GetClassLongPtr GetClassLongPtrA +#define SetClassLongPtr SetClassLongPtrA +#endif + + WINUSERAPI ULONG_PTR WINAPI GetClassLongPtrA(HWND hWnd,int nIndex); + WINUSERAPI ULONG_PTR WINAPI GetClassLongPtrW(HWND hWnd,int nIndex); + WINUSERAPI ULONG_PTR WINAPI SetClassLongPtrA(HWND hWnd,int nIndex,LONG_PTR dwNewLong); + WINUSERAPI ULONG_PTR WINAPI SetClassLongPtrW(HWND hWnd,int nIndex,LONG_PTR dwNewLong); +#else +#ifdef UNICODE +#define GetClassLongPtr GetClassLongPtrW +#define SetClassLongPtr SetClassLongPtrW +#else +#define GetClassLongPtr GetClassLongPtrA +#define SetClassLongPtr SetClassLongPtrA +#endif + +#define GetClassLongPtrA GetClassLongA +#define GetClassLongPtrW GetClassLongW +#define SetClassLongPtrA SetClassLongA +#define SetClassLongPtrW SetClassLongW +#endif +#endif + +#ifdef UNICODE +#define FindWindow FindWindowW +#define FindWindowEx FindWindowExW +#define GetClassName GetClassNameW +#else +#define FindWindow FindWindowA +#define FindWindowEx FindWindowExA +#define GetClassName GetClassNameA +#endif + + WINUSERAPI WINBOOL WINAPI GetProcessDefaultLayout(DWORD *pdwDefaultLayout); + WINUSERAPI WINBOOL WINAPI SetProcessDefaultLayout(DWORD dwDefaultLayout); + WINUSERAPI HWND WINAPI GetDesktopWindow(VOID); + WINUSERAPI HWND WINAPI GetParent(HWND hWnd); + WINUSERAPI HWND WINAPI SetParent(HWND hWndChild,HWND hWndNewParent); + WINUSERAPI WINBOOL WINAPI EnumChildWindows(HWND hWndParent,WNDENUMPROC lpEnumFunc,LPARAM lParam); + WINUSERAPI HWND WINAPI FindWindowA(LPCSTR lpClassName,LPCSTR lpWindowName); + WINUSERAPI HWND WINAPI FindWindowW(LPCWSTR lpClassName,LPCWSTR lpWindowName); + WINUSERAPI HWND WINAPI FindWindowExA(HWND hWndParent,HWND hWndChildAfter,LPCSTR lpszClass,LPCSTR lpszWindow); + WINUSERAPI HWND WINAPI FindWindowExW(HWND hWndParent,HWND hWndChildAfter,LPCWSTR lpszClass,LPCWSTR lpszWindow); + WINUSERAPI HWND WINAPI GetShellWindow(VOID); + WINUSERAPI WINBOOL WINAPI RegisterShellHookWindow(HWND hwnd); + WINUSERAPI WINBOOL WINAPI DeregisterShellHookWindow(HWND hwnd); + WINUSERAPI WINBOOL WINAPI EnumWindows(WNDENUMPROC lpEnumFunc,LPARAM lParam); + WINUSERAPI WINBOOL WINAPI EnumThreadWindows(DWORD dwThreadId,WNDENUMPROC lpfn,LPARAM lParam); + +#define EnumTaskWindows(hTask,lpfn,lParam) EnumThreadWindows(HandleToUlong(hTask),lpfn,lParam) + + WINUSERAPI int WINAPI GetClassNameA(HWND hWnd,LPSTR lpClassName,int nMaxCount); + WINUSERAPI int WINAPI GetClassNameW(HWND hWnd,LPWSTR lpClassName,int nMaxCount); + WINUSERAPI HWND WINAPI GetTopWindow(HWND hWnd); + +#define GetNextWindow(hWnd,wCmd) GetWindow(hWnd,wCmd) +#define GetSysModalWindow() (NULL) +#define SetSysModalWindow(hWnd) (NULL) + + WINUSERAPI DWORD WINAPI GetWindowThreadProcessId(HWND hWnd,LPDWORD lpdwProcessId); + WINUSERAPI WINBOOL WINAPI IsGUIThread(WINBOOL bConvert); + +#define GetWindowTask(hWnd) ((HANDLE)(DWORD_PTR)GetWindowThreadProcessId(hWnd,NULL)) + + WINUSERAPI HWND WINAPI GetLastActivePopup(HWND hWnd); + +#define GW_HWNDFIRST 0 +#define GW_HWNDLAST 1 +#define GW_HWNDNEXT 2 +#define GW_HWNDPREV 3 +#define GW_OWNER 4 +#define GW_CHILD 5 +#define GW_ENABLEDPOPUP 6 +#define GW_MAX 6 + + WINUSERAPI HWND WINAPI GetWindow(HWND hWnd,UINT uCmd); + +#ifndef NOWH + +#ifdef UNICODE +#define SetWindowsHook SetWindowsHookW +#define SetWindowsHookEx SetWindowsHookExW +#else +#define SetWindowsHook SetWindowsHookA +#define SetWindowsHookEx SetWindowsHookExA +#endif + + WINUSERAPI HHOOK WINAPI SetWindowsHookA(int nFilterType,HOOKPROC pfnFilterProc); + WINUSERAPI HHOOK WINAPI SetWindowsHookW(int nFilterType,HOOKPROC pfnFilterProc); + WINUSERAPI WINBOOL WINAPI UnhookWindowsHook(int nCode,HOOKPROC pfnFilterProc); + WINUSERAPI HHOOK WINAPI SetWindowsHookExA(int idHook,HOOKPROC lpfn,HINSTANCE hmod,DWORD dwThreadId); + WINUSERAPI HHOOK WINAPI SetWindowsHookExW(int idHook,HOOKPROC lpfn,HINSTANCE hmod,DWORD dwThreadId); + WINUSERAPI WINBOOL WINAPI UnhookWindowsHookEx(HHOOK hhk); + WINUSERAPI LRESULT WINAPI CallNextHookEx(HHOOK hhk,int nCode,WPARAM wParam,LPARAM lParam); +#define DefHookProc(nCode,wParam,lParam,phhk) CallNextHookEx(*phhk,nCode,wParam,lParam) +#endif + +#ifndef NOMENUS + +#define MF_INSERT 0x00000000L +#define MF_CHANGE 0x00000080L +#define MF_APPEND 0x00000100L +#define MF_DELETE 0x00000200L +#define MF_REMOVE 0x00001000L +#define MF_BYCOMMAND 0x00000000L +#define MF_BYPOSITION 0x00000400L +#define MF_SEPARATOR 0x00000800L +#define MF_ENABLED 0x00000000L +#define MF_GRAYED 0x00000001L +#define MF_DISABLED 0x00000002L +#define MF_UNCHECKED 0x00000000L +#define MF_CHECKED 0x00000008L +#define MF_USECHECKBITMAPS 0x00000200L +#define MF_STRING 0x00000000L +#define MF_BITMAP 0x00000004L +#define MF_OWNERDRAW 0x00000100L +#define MF_POPUP 0x00000010L +#define MF_MENUBARBREAK 0x00000020L +#define MF_MENUBREAK 0x00000040L +#define MF_UNHILITE 0x00000000L +#define MF_HILITE 0x00000080L +#define MF_DEFAULT 0x00001000L +#define MF_SYSMENU 0x00002000L +#define MF_HELP 0x00004000L +#define MF_RIGHTJUSTIFY 0x00004000L +#define MF_MOUSESELECT 0x00008000L +#define MF_END 0x00000080L + +#define MFT_STRING MF_STRING +#define MFT_BITMAP MF_BITMAP +#define MFT_MENUBARBREAK MF_MENUBARBREAK +#define MFT_MENUBREAK MF_MENUBREAK +#define MFT_OWNERDRAW MF_OWNERDRAW +#define MFT_RADIOCHECK 0x00000200L +#define MFT_SEPARATOR MF_SEPARATOR +#define MFT_RIGHTORDER 0x00002000L +#define MFT_RIGHTJUSTIFY MF_RIGHTJUSTIFY + +#define MFS_GRAYED 0x00000003L +#define MFS_DISABLED MFS_GRAYED +#define MFS_CHECKED MF_CHECKED +#define MFS_HILITE MF_HILITE +#define MFS_ENABLED MF_ENABLED +#define MFS_UNCHECKED MF_UNCHECKED +#define MFS_UNHILITE MF_UNHILITE +#define MFS_DEFAULT MF_DEFAULT + + WINUSERAPI WINBOOL WINAPI CheckMenuRadioItem(HMENU hmenu,UINT first,UINT last,UINT check,UINT flags); + + typedef struct { + WORD versionNumber; + WORD offset; + } MENUITEMTEMPLATEHEADER,*PMENUITEMTEMPLATEHEADER; + + typedef struct { + WORD mtOption; + WORD mtID; + WCHAR mtString[1]; + } MENUITEMTEMPLATE,*PMENUITEMTEMPLATE; +#define MF_END 0x00000080L +#endif + +#ifndef NOSYSCOMMANDS + +#define SC_SIZE 0xF000 +#define SC_MOVE 0xF010 +#define SC_MINIMIZE 0xF020 +#define SC_MAXIMIZE 0xF030 +#define SC_NEXTWINDOW 0xF040 +#define SC_PREVWINDOW 0xF050 +#define SC_CLOSE 0xF060 +#define SC_VSCROLL 0xF070 +#define SC_HSCROLL 0xF080 +#define SC_MOUSEMENU 0xF090 +#define SC_KEYMENU 0xF100 +#define SC_ARRANGE 0xF110 +#define SC_RESTORE 0xF120 +#define SC_TASKLIST 0xF130 +#define SC_SCREENSAVE 0xF140 +#define SC_HOTKEY 0xF150 +#define SC_DEFAULT 0xF160 +#define SC_MONITORPOWER 0xF170 +#define SC_CONTEXTHELP 0xF180 +#define SC_SEPARATOR 0xF00F +#define SC_ICON SC_MINIMIZE +#define SC_ZOOM SC_MAXIMIZE +#endif + +#ifdef UNICODE +#define LoadBitmap LoadBitmapW +#define LoadCursor LoadCursorW +#define LoadCursorFromFile LoadCursorFromFileW +#else +#define LoadBitmap LoadBitmapA +#define LoadCursor LoadCursorA +#define LoadCursorFromFile LoadCursorFromFileA +#endif + + WINUSERAPI HBITMAP WINAPI LoadBitmapA(HINSTANCE hInstance,LPCSTR lpBitmapName); + WINUSERAPI HBITMAP WINAPI LoadBitmapW(HINSTANCE hInstance,LPCWSTR lpBitmapName); + WINUSERAPI HCURSOR WINAPI LoadCursorA(HINSTANCE hInstance,LPCSTR lpCursorName); + WINUSERAPI HCURSOR WINAPI LoadCursorW(HINSTANCE hInstance,LPCWSTR lpCursorName); + WINUSERAPI HCURSOR WINAPI LoadCursorFromFileA(LPCSTR lpFileName); + WINUSERAPI HCURSOR WINAPI LoadCursorFromFileW(LPCWSTR lpFileName); + WINUSERAPI HCURSOR WINAPI CreateCursor(HINSTANCE hInst,int xHotSpot,int yHotSpot,int nWidth,int nHeight,CONST VOID *pvANDPlane,CONST VOID *pvXORPlane); + WINUSERAPI WINBOOL WINAPI DestroyCursor(HCURSOR hCursor); + +#define CopyCursor(pcur) ((HCURSOR)CopyIcon((HICON)(pcur))) + +#define IDC_ARROW MAKEINTRESOURCE(32512) +#define IDC_IBEAM MAKEINTRESOURCE(32513) +#define IDC_WAIT MAKEINTRESOURCE(32514) +#define IDC_CROSS MAKEINTRESOURCE(32515) +#define IDC_UPARROW MAKEINTRESOURCE(32516) +#define IDC_SIZE MAKEINTRESOURCE(32640) +#define IDC_ICON MAKEINTRESOURCE(32641) +#define IDC_SIZENWSE MAKEINTRESOURCE(32642) +#define IDC_SIZENESW MAKEINTRESOURCE(32643) +#define IDC_SIZEWE MAKEINTRESOURCE(32644) +#define IDC_SIZENS MAKEINTRESOURCE(32645) +#define IDC_SIZEALL MAKEINTRESOURCE(32646) +#define IDC_NO MAKEINTRESOURCE(32648) +#define IDC_HAND MAKEINTRESOURCE(32649) +#define IDC_APPSTARTING MAKEINTRESOURCE(32650) +#define IDC_HELP MAKEINTRESOURCE(32651) + + WINUSERAPI WINBOOL WINAPI SetSystemCursor(HCURSOR hcur,DWORD id); + + typedef struct _ICONINFO { + WINBOOL fIcon; + DWORD xHotspot; + DWORD yHotspot; + HBITMAP hbmMask; + HBITMAP hbmColor; + } ICONINFO; + typedef ICONINFO *PICONINFO; + +#ifdef UNICODE +#define LoadIcon LoadIconW +#define PrivateExtractIcons PrivateExtractIconsW +#else +#define LoadIcon LoadIconA +#define PrivateExtractIcons PrivateExtractIconsA +#endif + + WINUSERAPI HICON WINAPI LoadIconA(HINSTANCE hInstance,LPCSTR lpIconName); + WINUSERAPI HICON WINAPI LoadIconW(HINSTANCE hInstance,LPCWSTR lpIconName); + WINUSERAPI UINT WINAPI PrivateExtractIconsA(LPCSTR szFileName,int nIconIndex,int cxIcon,int cyIcon,HICON *phicon,UINT *piconid,UINT nIcons,UINT flags); + WINUSERAPI UINT WINAPI PrivateExtractIconsW(LPCWSTR szFileName,int nIconIndex,int cxIcon,int cyIcon,HICON *phicon,UINT *piconid,UINT nIcons,UINT flags); + WINUSERAPI HICON WINAPI CreateIcon(HINSTANCE hInstance,int nWidth,int nHeight,BYTE cPlanes,BYTE cBitsPixel,CONST BYTE *lpbANDbits,CONST BYTE *lpbXORbits); + WINUSERAPI WINBOOL WINAPI DestroyIcon(HICON hIcon); + WINUSERAPI int WINAPI LookupIconIdFromDirectory(PBYTE presbits,WINBOOL fIcon); + WINUSERAPI int WINAPI LookupIconIdFromDirectoryEx(PBYTE presbits,WINBOOL fIcon,int cxDesired,int cyDesired,UINT Flags); + WINUSERAPI HICON WINAPI CreateIconFromResource(PBYTE presbits,DWORD dwResSize,WINBOOL fIcon,DWORD dwVer); + WINUSERAPI HICON WINAPI CreateIconFromResourceEx(PBYTE presbits,DWORD dwResSize,WINBOOL fIcon,DWORD dwVer,int cxDesired,int cyDesired,UINT Flags); + + typedef struct tagCURSORSHAPE { + int xHotSpot; + int yHotSpot; + int cx; + int cy; + int cbWidth; + BYTE Planes; + BYTE BitsPixel; + } CURSORSHAPE,*LPCURSORSHAPE; + +#define IMAGE_BITMAP 0 +#define IMAGE_ICON 1 +#define IMAGE_CURSOR 2 +#define IMAGE_ENHMETAFILE 3 + +#define LR_DEFAULTCOLOR 0x0000 +#define LR_MONOCHROME 0x0001 +#define LR_COLOR 0x0002 +#define LR_COPYRETURNORG 0x0004 +#define LR_COPYDELETEORG 0x0008 +#define LR_LOADFROMFILE 0x0010 +#define LR_LOADTRANSPARENT 0x0020 +#define LR_DEFAULTSIZE 0x0040 +#define LR_VGACOLOR 0x0080 +#define LR_LOADMAP3DCOLORS 0x1000 +#define LR_CREATEDIBSECTION 0x2000 +#define LR_COPYFROMRESOURCE 0x4000 +#define LR_SHARED 0x8000 + +#ifdef UNICODE +#define LoadImage LoadImageW +#else +#define LoadImage LoadImageA +#endif + + WINUSERAPI HANDLE WINAPI LoadImageA(HINSTANCE hInst,LPCSTR name,UINT type,int cx,int cy,UINT fuLoad); + WINUSERAPI HANDLE WINAPI LoadImageW(HINSTANCE hInst,LPCWSTR name,UINT type,int cx,int cy,UINT fuLoad); + WINUSERAPI HANDLE WINAPI CopyImage(HANDLE h,UINT type,int cx,int cy,UINT flags); + +#define DI_MASK 0x0001 +#define DI_IMAGE 0x0002 +#define DI_NORMAL 0x0003 +#define DI_COMPAT 0x0004 +#define DI_DEFAULTSIZE 0x0008 +#define DI_NOMIRROR 0x0010 + + WINUSERAPI WINBOOL WINAPI DrawIconEx(HDC hdc,int xLeft,int yTop,HICON hIcon,int cxWidth,int cyWidth,UINT istepIfAniCur,HBRUSH hbrFlickerFreeDraw,UINT diFlags); + WINUSERAPI HICON WINAPI CreateIconIndirect(PICONINFO piconinfo); + WINUSERAPI HICON WINAPI CopyIcon(HICON hIcon); + WINUSERAPI WINBOOL WINAPI GetIconInfo(HICON hIcon,PICONINFO piconinfo); + +#define RES_ICON 1 +#define RES_CURSOR 2 + +#ifdef OEMRESOURCE + +#define OBM_CLOSE 32754 +#define OBM_UPARROW 32753 +#define OBM_DNARROW 32752 +#define OBM_RGARROW 32751 +#define OBM_LFARROW 32750 +#define OBM_REDUCE 32749 +#define OBM_ZOOM 32748 +#define OBM_RESTORE 32747 +#define OBM_REDUCED 32746 +#define OBM_ZOOMD 32745 +#define OBM_RESTORED 32744 +#define OBM_UPARROWD 32743 +#define OBM_DNARROWD 32742 +#define OBM_RGARROWD 32741 +#define OBM_LFARROWD 32740 +#define OBM_MNARROW 32739 +#define OBM_COMBO 32738 +#define OBM_UPARROWI 32737 +#define OBM_DNARROWI 32736 +#define OBM_RGARROWI 32735 +#define OBM_LFARROWI 32734 + +#define OBM_OLD_CLOSE 32767 +#define OBM_SIZE 32766 +#define OBM_OLD_UPARROW 32765 +#define OBM_OLD_DNARROW 32764 +#define OBM_OLD_RGARROW 32763 +#define OBM_OLD_LFARROW 32762 +#define OBM_BTSIZE 32761 +#define OBM_CHECK 32760 +#define OBM_CHECKBOXES 32759 +#define OBM_BTNCORNERS 32758 +#define OBM_OLD_REDUCE 32757 +#define OBM_OLD_ZOOM 32756 +#define OBM_OLD_RESTORE 32755 + +#define OCR_NORMAL 32512 +#define OCR_IBEAM 32513 +#define OCR_WAIT 32514 +#define OCR_CROSS 32515 +#define OCR_UP 32516 +#define OCR_SIZE 32640 +#define OCR_ICON 32641 +#define OCR_SIZENWSE 32642 +#define OCR_SIZENESW 32643 +#define OCR_SIZEWE 32644 +#define OCR_SIZENS 32645 +#define OCR_SIZEALL 32646 +#define OCR_ICOCUR 32647 +#define OCR_NO 32648 +#define OCR_HAND 32649 +#define OCR_APPSTARTING 32650 + +#define OIC_SAMPLE 32512 +#define OIC_HAND 32513 +#define OIC_QUES 32514 +#define OIC_BANG 32515 +#define OIC_NOTE 32516 +#define OIC_WINLOGO 32517 +#define OIC_WARNING OIC_BANG +#define OIC_ERROR OIC_HAND +#define OIC_INFORMATION OIC_NOTE +#endif + +#define ORD_LANGDRIVER 1 + +#ifndef NOICONS + +#ifdef RC_INVOKED +#define IDI_APPLICATION 32512 +#define IDI_HAND 32513 +#define IDI_QUESTION 32514 +#define IDI_EXCLAMATION 32515 +#define IDI_ASTERISK 32516 +#define IDI_WINLOGO 32517 +#else +#define IDI_APPLICATION MAKEINTRESOURCE(32512) +#define IDI_HAND MAKEINTRESOURCE(32513) +#define IDI_QUESTION MAKEINTRESOURCE(32514) +#define IDI_EXCLAMATION MAKEINTRESOURCE(32515) +#define IDI_ASTERISK MAKEINTRESOURCE(32516) +#define IDI_WINLOGO MAKEINTRESOURCE(32517) +#endif + +#define IDI_WARNING IDI_EXCLAMATION +#define IDI_ERROR IDI_HAND +#define IDI_INFORMATION IDI_ASTERISK +#endif + +#ifdef UNICODE +#define LoadString LoadStringW +#else +#define LoadString LoadStringA +#endif + + WINUSERAPI int WINAPI LoadStringA(HINSTANCE hInstance,UINT uID,LPSTR lpBuffer,int cchBufferMax); + WINUSERAPI int WINAPI LoadStringW(HINSTANCE hInstance,UINT uID,LPWSTR lpBuffer,int cchBufferMax); + +#define IDOK 1 +#define IDCANCEL 2 +#define IDABORT 3 +#define IDRETRY 4 +#define IDIGNORE 5 +#define IDYES 6 +#define IDNO 7 +#define IDCLOSE 8 +#define IDHELP 9 +#define IDTRYAGAIN 10 +#define IDCONTINUE 11 + +#ifndef IDTIMEOUT +#define IDTIMEOUT 32000 +#endif + +#ifndef NOCTLMGR + +#ifndef NOWINSTYLES +#define ES_LEFT 0x0000L +#define ES_CENTER 0x0001L +#define ES_RIGHT 0x0002L +#define ES_MULTILINE 0x0004L +#define ES_UPPERCASE 0x0008L +#define ES_LOWERCASE 0x0010L +#define ES_PASSWORD 0x0020L +#define ES_AUTOVSCROLL 0x0040L +#define ES_AUTOHSCROLL 0x0080L +#define ES_NOHIDESEL 0x0100L +#define ES_OEMCONVERT 0x0400L +#define ES_READONLY 0x0800L +#define ES_WANTRETURN 0x1000L +#define ES_NUMBER 0x2000L +#endif + +#define EN_SETFOCUS 0x0100 +#define EN_KILLFOCUS 0x0200 +#define EN_CHANGE 0x0300 +#define EN_UPDATE 0x0400 +#define EN_ERRSPACE 0x0500 +#define EN_MAXTEXT 0x0501 +#define EN_HSCROLL 0x0601 +#define EN_VSCROLL 0x0602 +#define EN_ALIGN_LTR_EC 0x0700 +#define EN_ALIGN_RTL_EC 0x0701 + +#define EC_LEFTMARGIN 0x0001 +#define EC_RIGHTMARGIN 0x0002 +#define EC_USEFONTINFO 0xffff + +#define EMSIS_COMPOSITIONSTRING 0x0001 + +#define EIMES_GETCOMPSTRATONCE 0x0001 +#define EIMES_CANCELCOMPSTRINFOCUS 0x0002 +#define EIMES_COMPLETECOMPSTRKILLFOCUS 0x0004 + +#ifndef NOWINMESSAGES + +#define EM_GETSEL 0x00B0 +#define EM_SETSEL 0x00B1 +#define EM_GETRECT 0x00B2 +#define EM_SETRECT 0x00B3 +#define EM_SETRECTNP 0x00B4 +#define EM_SCROLL 0x00B5 +#define EM_LINESCROLL 0x00B6 +#define EM_SCROLLCARET 0x00B7 +#define EM_GETMODIFY 0x00B8 +#define EM_SETMODIFY 0x00B9 +#define EM_GETLINECOUNT 0x00BA +#define EM_LINEINDEX 0x00BB +#define EM_SETHANDLE 0x00BC +#define EM_GETHANDLE 0x00BD +#define EM_GETTHUMB 0x00BE +#define EM_LINELENGTH 0x00C1 +#define EM_REPLACESEL 0x00C2 +#define EM_GETLINE 0x00C4 +#define EM_LIMITTEXT 0x00C5 +#define EM_CANUNDO 0x00C6 +#define EM_UNDO 0x00C7 +#define EM_FMTLINES 0x00C8 +#define EM_LINEFROMCHAR 0x00C9 +#define EM_SETTABSTOPS 0x00CB +#define EM_SETPASSWORDCHAR 0x00CC +#define EM_EMPTYUNDOBUFFER 0x00CD +#define EM_GETFIRSTVISIBLELINE 0x00CE +#define EM_SETREADONLY 0x00CF +#define EM_SETWORDBREAKPROC 0x00D0 +#define EM_GETWORDBREAKPROC 0x00D1 +#define EM_GETPASSWORDCHAR 0x00D2 +#define EM_SETMARGINS 0x00D3 +#define EM_GETMARGINS 0x00D4 +#define EM_SETLIMITTEXT EM_LIMITTEXT +#define EM_GETLIMITTEXT 0x00D5 +#define EM_POSFROMCHAR 0x00D6 +#define EM_CHARFROMPOS 0x00D7 +#define EM_SETIMESTATUS 0x00D8 +#define EM_GETIMESTATUS 0x00D9 +#endif + +#define WB_LEFT 0 +#define WB_RIGHT 1 +#define WB_ISDELIMITER 2 + +#define BS_PUSHBUTTON 0x00000000L +#define BS_DEFPUSHBUTTON 0x00000001L +#define BS_CHECKBOX 0x00000002L +#define BS_AUTOCHECKBOX 0x00000003L +#define BS_RADIOBUTTON 0x00000004L +#define BS_3STATE 0x00000005L +#define BS_AUTO3STATE 0x00000006L +#define BS_GROUPBOX 0x00000007L +#define BS_USERBUTTON 0x00000008L +#define BS_AUTORADIOBUTTON 0x00000009L +#define BS_PUSHBOX 0x0000000AL +#define BS_OWNERDRAW 0x0000000BL +#define BS_TYPEMASK 0x0000000FL +#define BS_LEFTTEXT 0x00000020L +#define BS_TEXT 0x00000000L +#define BS_ICON 0x00000040L +#define BS_BITMAP 0x00000080L +#define BS_LEFT 0x00000100L +#define BS_RIGHT 0x00000200L +#define BS_CENTER 0x00000300L +#define BS_TOP 0x00000400L +#define BS_BOTTOM 0x00000800L +#define BS_VCENTER 0x00000C00L +#define BS_PUSHLIKE 0x00001000L +#define BS_MULTILINE 0x00002000L +#define BS_NOTIFY 0x00004000L +#define BS_FLAT 0x00008000L +#define BS_RIGHTBUTTON BS_LEFTTEXT + +#define BN_CLICKED 0 +#define BN_PAINT 1 +#define BN_HILITE 2 +#define BN_UNHILITE 3 +#define BN_DISABLE 4 +#define BN_DOUBLECLICKED 5 +#define BN_PUSHED BN_HILITE +#define BN_UNPUSHED BN_UNHILITE +#define BN_DBLCLK BN_DOUBLECLICKED +#define BN_SETFOCUS 6 +#define BN_KILLFOCUS 7 + +#define BM_GETCHECK 0x00F0 +#define BM_SETCHECK 0x00F1 +#define BM_GETSTATE 0x00F2 +#define BM_SETSTATE 0x00F3 +#define BM_SETSTYLE 0x00F4 +#define BM_CLICK 0x00F5 +#define BM_GETIMAGE 0x00F6 +#define BM_SETIMAGE 0x00F7 + +#define BST_UNCHECKED 0x0000 +#define BST_CHECKED 0x0001 +#define BST_INDETERMINATE 0x0002 +#define BST_PUSHED 0x0004 +#define BST_FOCUS 0x0008 + +#define SS_LEFT 0x00000000L +#define SS_CENTER 0x00000001L +#define SS_RIGHT 0x00000002L +#define SS_ICON 0x00000003L +#define SS_BLACKRECT 0x00000004L +#define SS_GRAYRECT 0x00000005L +#define SS_WHITERECT 0x00000006L +#define SS_BLACKFRAME 0x00000007L +#define SS_GRAYFRAME 0x00000008L +#define SS_WHITEFRAME 0x00000009L +#define SS_USERITEM 0x0000000AL +#define SS_SIMPLE 0x0000000BL +#define SS_LEFTNOWORDWRAP 0x0000000CL +#define SS_OWNERDRAW 0x0000000DL +#define SS_BITMAP 0x0000000EL +#define SS_ENHMETAFILE 0x0000000FL +#define SS_ETCHEDHORZ 0x00000010L +#define SS_ETCHEDVERT 0x00000011L +#define SS_ETCHEDFRAME 0x00000012L +#define SS_TYPEMASK 0x0000001FL +#define SS_REALSIZECONTROL 0x00000040L +#define SS_NOPREFIX 0x00000080L +#define SS_NOTIFY 0x00000100L +#define SS_CENTERIMAGE 0x00000200L +#define SS_RIGHTJUST 0x00000400L +#define SS_REALSIZEIMAGE 0x00000800L +#define SS_SUNKEN 0x00001000L +#define SS_EDITCONTROL 0x00002000L +#define SS_ENDELLIPSIS 0x00004000L +#define SS_PATHELLIPSIS 0x00008000L +#define SS_WORDELLIPSIS 0x0000C000L +#define SS_ELLIPSISMASK 0x0000C000L + +#ifndef NOWINMESSAGES + +#define STM_SETICON 0x0170 +#define STM_GETICON 0x0171 +#define STM_SETIMAGE 0x0172 +#define STM_GETIMAGE 0x0173 +#define STN_CLICKED 0 +#define STN_DBLCLK 1 +#define STN_ENABLE 2 +#define STN_DISABLE 3 +#define STM_MSGMAX 0x0174 +#endif + +#define WC_DIALOG (MAKEINTATOM(0x8002)) + +#define DWL_MSGRESULT 0 +#define DWL_DLGPROC 4 +#define DWL_USER 8 + +#ifdef _WIN64 + +#undef DWL_MSGRESULT +#undef DWL_DLGPROC +#undef DWL_USER +#endif + +#define DWLP_MSGRESULT 0 +#define DWLP_DLGPROC DWLP_MSGRESULT + sizeof(LRESULT) +#define DWLP_USER DWLP_DLGPROC + sizeof(DLGPROC) + +#ifndef NOMSG + +#ifdef UNICODE +#define IsDialogMessage IsDialogMessageW +#else +#define IsDialogMessage IsDialogMessageA +#endif + + WINUSERAPI WINBOOL WINAPI IsDialogMessageA(HWND hDlg,LPMSG lpMsg); + WINUSERAPI WINBOOL WINAPI IsDialogMessageW(HWND hDlg,LPMSG lpMsg); +#endif + +#ifdef UNICODE +#define DlgDirList DlgDirListW +#define DlgDirSelectEx DlgDirSelectExW +#define DlgDirListComboBox DlgDirListComboBoxW +#define DlgDirSelectComboBoxEx DlgDirSelectComboBoxExW +#else +#define DlgDirList DlgDirListA +#define DlgDirSelectEx DlgDirSelectExA +#define DlgDirListComboBox DlgDirListComboBoxA +#define DlgDirSelectComboBoxEx DlgDirSelectComboBoxExA +#endif + + WINUSERAPI WINBOOL WINAPI MapDialogRect(HWND hDlg,LPRECT lpRect); + WINUSERAPI int WINAPI DlgDirListA(HWND hDlg,LPSTR lpPathSpec,int nIDListBox,int nIDStaticPath,UINT uFileType); + WINUSERAPI int WINAPI DlgDirListW(HWND hDlg,LPWSTR lpPathSpec,int nIDListBox,int nIDStaticPath,UINT uFileType); + +#define DDL_READWRITE 0x0000 +#define DDL_READONLY 0x0001 +#define DDL_HIDDEN 0x0002 +#define DDL_SYSTEM 0x0004 +#define DDL_DIRECTORY 0x0010 +#define DDL_ARCHIVE 0x0020 + +#define DDL_POSTMSGS 0x2000 +#define DDL_DRIVES 0x4000 +#define DDL_EXCLUSIVE 0x8000 + + WINUSERAPI WINBOOL WINAPI DlgDirSelectExA(HWND hwndDlg,LPSTR lpString,int chCount,int idListBox); + WINUSERAPI WINBOOL WINAPI DlgDirSelectExW(HWND hwndDlg,LPWSTR lpString,int chCount,int idListBox); + WINUSERAPI int WINAPI DlgDirListComboBoxA(HWND hDlg,LPSTR lpPathSpec,int nIDComboBox,int nIDStaticPath,UINT uFiletype); + WINUSERAPI int WINAPI DlgDirListComboBoxW(HWND hDlg,LPWSTR lpPathSpec,int nIDComboBox,int nIDStaticPath,UINT uFiletype); + WINUSERAPI WINBOOL WINAPI DlgDirSelectComboBoxExA(HWND hwndDlg,LPSTR lpString,int cchOut,int idComboBox); + WINUSERAPI WINBOOL WINAPI DlgDirSelectComboBoxExW(HWND hwndDlg,LPWSTR lpString,int cchOut,int idComboBox); + +#define DS_ABSALIGN 0x01L +#define DS_SYSMODAL 0x02L +#define DS_LOCALEDIT 0x20L +#define DS_SETFONT 0x40L +#define DS_MODALFRAME 0x80L +#define DS_NOIDLEMSG 0x100L +#define DS_SETFOREGROUND 0x200L + +#define DS_3DLOOK 0x0004L +#define DS_FIXEDSYS 0x0008L +#define DS_NOFAILCREATE 0x0010L +#define DS_CONTROL 0x0400L +#define DS_CENTER 0x0800L +#define DS_CENTERMOUSE 0x1000L +#define DS_CONTEXTHELP 0x2000L + +#define DS_SHELLFONT (DS_SETFONT | DS_FIXEDSYS) + +#if(_WIN32_WCE >= 0x0500) +#define DS_USEPIXELS 0x8000L +#endif + +#define DM_GETDEFID (WM_USER+0) +#define DM_SETDEFID (WM_USER+1) +#define DM_REPOSITION (WM_USER+2) + +#define DC_HASDEFID 0x534B + +#define DLGC_WANTARROWS 0x0001 +#define DLGC_WANTTAB 0x0002 +#define DLGC_WANTALLKEYS 0x0004 +#define DLGC_WANTMESSAGE 0x0004 +#define DLGC_HASSETSEL 0x0008 +#define DLGC_DEFPUSHBUTTON 0x0010 +#define DLGC_UNDEFPUSHBUTTON 0x0020 +#define DLGC_RADIOBUTTON 0x0040 +#define DLGC_WANTCHARS 0x0080 +#define DLGC_STATIC 0x0100 +#define DLGC_BUTTON 0x2000 + +#define LB_CTLCODE 0L + +#define LB_OKAY 0 +#define LB_ERR (-1) +#define LB_ERRSPACE (-2) + +#define LBN_ERRSPACE (-2) +#define LBN_SELCHANGE 1 +#define LBN_DBLCLK 2 +#define LBN_SELCANCEL 3 +#define LBN_SETFOCUS 4 +#define LBN_KILLFOCUS 5 + +#ifndef NOWINMESSAGES + +#define LB_ADDSTRING 0x0180 +#define LB_INSERTSTRING 0x0181 +#define LB_DELETESTRING 0x0182 +#define LB_SELITEMRANGEEX 0x0183 +#define LB_RESETCONTENT 0x0184 +#define LB_SETSEL 0x0185 +#define LB_SETCURSEL 0x0186 +#define LB_GETSEL 0x0187 +#define LB_GETCURSEL 0x0188 +#define LB_GETTEXT 0x0189 +#define LB_GETTEXTLEN 0x018A +#define LB_GETCOUNT 0x018B +#define LB_SELECTSTRING 0x018C +#define LB_DIR 0x018D +#define LB_GETTOPINDEX 0x018E +#define LB_FINDSTRING 0x018F +#define LB_GETSELCOUNT 0x0190 +#define LB_GETSELITEMS 0x0191 +#define LB_SETTABSTOPS 0x0192 +#define LB_GETHORIZONTALEXTENT 0x0193 +#define LB_SETHORIZONTALEXTENT 0x0194 +#define LB_SETCOLUMNWIDTH 0x0195 +#define LB_ADDFILE 0x0196 +#define LB_SETTOPINDEX 0x0197 +#define LB_GETITEMRECT 0x0198 +#define LB_GETITEMDATA 0x0199 +#define LB_SETITEMDATA 0x019A +#define LB_SELITEMRANGE 0x019B +#define LB_SETANCHORINDEX 0x019C +#define LB_GETANCHORINDEX 0x019D +#define LB_SETCARETINDEX 0x019E +#define LB_GETCARETINDEX 0x019F +#define LB_SETITEMHEIGHT 0x01A0 +#define LB_GETITEMHEIGHT 0x01A1 +#define LB_FINDSTRINGEXACT 0x01A2 +#define LB_SETLOCALE 0x01A5 +#define LB_GETLOCALE 0x01A6 +#define LB_SETCOUNT 0x01A7 +#define LB_INITSTORAGE 0x01A8 +#define LB_ITEMFROMPOINT 0x01A9 +#if(_WIN32_WCE >= 0x0400) +#define LB_MULTIPLEADDSTRING 0x01B1 +#endif +#define LB_GETLISTBOXINFO 0x01B2 +#define LB_MSGMAX 0x01B3 +#endif + +#ifndef NOWINSTYLES + +#define LBS_NOTIFY 0x0001L +#define LBS_SORT 0x0002L +#define LBS_NOREDRAW 0x0004L +#define LBS_MULTIPLESEL 0x0008L +#define LBS_OWNERDRAWFIXED 0x0010L +#define LBS_OWNERDRAWVARIABLE 0x0020L +#define LBS_HASSTRINGS 0x0040L +#define LBS_USETABSTOPS 0x0080L +#define LBS_NOINTEGRALHEIGHT 0x0100L +#define LBS_MULTICOLUMN 0x0200L +#define LBS_WANTKEYBOARDINPUT 0x0400L +#define LBS_EXTENDEDSEL 0x0800L +#define LBS_DISABLENOSCROLL 0x1000L +#define LBS_NODATA 0x2000L +#define LBS_NOSEL 0x4000L +#define LBS_COMBOBOX 0x8000L + +#define LBS_STANDARD (LBS_NOTIFY | LBS_SORT | WS_VSCROLL | WS_BORDER) +#endif + +#define CB_OKAY 0 +#define CB_ERR (-1) +#define CB_ERRSPACE (-2) + +#define CBN_ERRSPACE (-1) +#define CBN_SELCHANGE 1 +#define CBN_DBLCLK 2 +#define CBN_SETFOCUS 3 +#define CBN_KILLFOCUS 4 +#define CBN_EDITCHANGE 5 +#define CBN_EDITUPDATE 6 +#define CBN_DROPDOWN 7 +#define CBN_CLOSEUP 8 +#define CBN_SELENDOK 9 +#define CBN_SELENDCANCEL 10 + +#ifndef NOWINSTYLES + +#define CBS_SIMPLE 0x0001L +#define CBS_DROPDOWN 0x0002L +#define CBS_DROPDOWNLIST 0x0003L +#define CBS_OWNERDRAWFIXED 0x0010L +#define CBS_OWNERDRAWVARIABLE 0x0020L +#define CBS_AUTOHSCROLL 0x0040L +#define CBS_OEMCONVERT 0x0080L +#define CBS_SORT 0x0100L +#define CBS_HASSTRINGS 0x0200L +#define CBS_NOINTEGRALHEIGHT 0x0400L +#define CBS_DISABLENOSCROLL 0x0800L +#define CBS_UPPERCASE 0x2000L +#define CBS_LOWERCASE 0x4000L +#endif + +#ifndef NOWINMESSAGES +#define CB_GETEDITSEL 0x0140 +#define CB_LIMITTEXT 0x0141 +#define CB_SETEDITSEL 0x0142 +#define CB_ADDSTRING 0x0143 +#define CB_DELETESTRING 0x0144 +#define CB_DIR 0x0145 +#define CB_GETCOUNT 0x0146 +#define CB_GETCURSEL 0x0147 +#define CB_GETLBTEXT 0x0148 +#define CB_GETLBTEXTLEN 0x0149 +#define CB_INSERTSTRING 0x014A +#define CB_RESETCONTENT 0x014B +#define CB_FINDSTRING 0x014C +#define CB_SELECTSTRING 0x014D +#define CB_SETCURSEL 0x014E +#define CB_SHOWDROPDOWN 0x014F +#define CB_GETITEMDATA 0x0150 +#define CB_SETITEMDATA 0x0151 +#define CB_GETDROPPEDCONTROLRECT 0x0152 +#define CB_SETITEMHEIGHT 0x0153 +#define CB_GETITEMHEIGHT 0x0154 +#define CB_SETEXTENDEDUI 0x0155 +#define CB_GETEXTENDEDUI 0x0156 +#define CB_GETDROPPEDSTATE 0x0157 +#define CB_FINDSTRINGEXACT 0x0158 +#define CB_SETLOCALE 0x0159 +#define CB_GETLOCALE 0x015A +#define CB_GETTOPINDEX 0x015b +#define CB_SETTOPINDEX 0x015c +#define CB_GETHORIZONTALEXTENT 0x015d +#define CB_SETHORIZONTALEXTENT 0x015e +#define CB_GETDROPPEDWIDTH 0x015f +#define CB_SETDROPPEDWIDTH 0x0160 +#define CB_INITSTORAGE 0x0161 +#if(_WIN32_WCE >= 0x0400) +#define CB_MULTIPLEADDSTRING 0x0163 +#endif +#define CB_GETCOMBOBOXINFO 0x0164 +#define CB_MSGMAX 0x0165 +#endif + +#ifndef NOWINSTYLES + +#define SBS_HORZ 0x0000L +#define SBS_VERT 0x0001L +#define SBS_TOPALIGN 0x0002L +#define SBS_LEFTALIGN 0x0002L +#define SBS_BOTTOMALIGN 0x0004L +#define SBS_RIGHTALIGN 0x0004L +#define SBS_SIZEBOXTOPLEFTALIGN 0x0002L +#define SBS_SIZEBOXBOTTOMRIGHTALIGN 0x0004L +#define SBS_SIZEBOX 0x0008L +#define SBS_SIZEGRIP 0x0010L +#endif + +#ifndef NOWINMESSAGES +#define SBM_SETPOS 0x00E0 +#define SBM_GETPOS 0x00E1 +#define SBM_SETRANGE 0x00E2 +#define SBM_SETRANGEREDRAW 0x00E6 +#define SBM_GETRANGE 0x00E3 +#define SBM_ENABLE_ARROWS 0x00E4 +#define SBM_SETSCROLLINFO 0x00E9 +#define SBM_GETSCROLLINFO 0x00EA +#define SBM_GETSCROLLBARINFO 0x00EB + +#define SIF_RANGE 0x0001 +#define SIF_PAGE 0x0002 +#define SIF_POS 0x0004 +#define SIF_DISABLENOSCROLL 0x0008 +#define SIF_TRACKPOS 0x0010 +#define SIF_ALL (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS) + + typedef struct tagSCROLLINFO { + UINT cbSize; + UINT fMask; + int nMin; + int nMax; + UINT nPage; + int nPos; + int nTrackPos; + } SCROLLINFO,*LPSCROLLINFO; + typedef SCROLLINFO CONST *LPCSCROLLINFO; + + WINUSERAPI int WINAPI SetScrollInfo(HWND hwnd,int nBar,LPCSCROLLINFO lpsi,WINBOOL redraw); + WINUSERAPI WINBOOL WINAPI GetScrollInfo(HWND hwnd,int nBar,LPSCROLLINFO lpsi); +#endif +#endif + +#ifndef NOMDI + +#define MDIS_ALLCHILDSTYLES 0x0001 + +#define MDITILE_VERTICAL 0x0000 +#define MDITILE_HORIZONTAL 0x0001 +#define MDITILE_SKIPDISABLED 0x0002 +#define MDITILE_ZORDER 0x0004 + + typedef struct tagMDICREATESTRUCTA { + LPCSTR szClass; + LPCSTR szTitle; + HANDLE hOwner; + int x; + int y; + int cx; + int cy; + DWORD style; + LPARAM lParam; + } MDICREATESTRUCTA,*LPMDICREATESTRUCTA; + + typedef struct tagMDICREATESTRUCTW { + LPCWSTR szClass; + LPCWSTR szTitle; + HANDLE hOwner; + int x; + int y; + int cx; + int cy; + DWORD style; + LPARAM lParam; + } MDICREATESTRUCTW,*LPMDICREATESTRUCTW; + +#ifdef UNICODE + typedef MDICREATESTRUCTW MDICREATESTRUCT; + typedef LPMDICREATESTRUCTW LPMDICREATESTRUCT; +#else + typedef MDICREATESTRUCTA MDICREATESTRUCT; + typedef LPMDICREATESTRUCTA LPMDICREATESTRUCT; +#endif + + typedef struct tagCLIENTCREATESTRUCT { + HANDLE hWindowMenu; + UINT idFirstChild; + } CLIENTCREATESTRUCT,*LPCLIENTCREATESTRUCT; + +#ifdef UNICODE +#define DefFrameProc DefFrameProcW +#define DefMDIChildProc DefMDIChildProcW +#define CreateMDIWindow CreateMDIWindowW +#else +#define DefFrameProc DefFrameProcA +#define DefMDIChildProc DefMDIChildProcA +#define CreateMDIWindow CreateMDIWindowA +#endif + + WINUSERAPI LRESULT WINAPI DefFrameProcA(HWND hWnd,HWND hWndMDIClient,UINT uMsg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI DefFrameProcW(HWND hWnd,HWND hWndMDIClient,UINT uMsg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI DefMDIChildProcA(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); + WINUSERAPI LRESULT WINAPI DefMDIChildProcW(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); + +#ifndef NOMSG + WINUSERAPI WINBOOL WINAPI TranslateMDISysAccel(HWND hWndClient,LPMSG lpMsg); +#endif + + WINUSERAPI UINT WINAPI ArrangeIconicWindows(HWND hWnd); + WINUSERAPI HWND WINAPI CreateMDIWindowA(LPCSTR lpClassName,LPCSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HINSTANCE hInstance,LPARAM lParam); + WINUSERAPI HWND WINAPI CreateMDIWindowW(LPCWSTR lpClassName,LPCWSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HINSTANCE hInstance,LPARAM lParam); + WINUSERAPI WORD WINAPI TileWindows(HWND hwndParent,UINT wHow,CONST RECT *lpRect,UINT cKids,const HWND *lpKids); + WINUSERAPI WORD WINAPI CascadeWindows(HWND hwndParent,UINT wHow,CONST RECT *lpRect,UINT cKids,const HWND *lpKids); +#endif +#endif + +#ifndef NOHELP + + typedef DWORD HELPPOLY; + typedef struct tagMULTIKEYHELPA { + DWORD mkSize; + CHAR mkKeylist; + CHAR szKeyphrase[1]; + } MULTIKEYHELPA,*PMULTIKEYHELPA,*LPMULTIKEYHELPA; + + typedef struct tagMULTIKEYHELPW { + DWORD mkSize; + WCHAR mkKeylist; + WCHAR szKeyphrase[1]; + } MULTIKEYHELPW,*PMULTIKEYHELPW,*LPMULTIKEYHELPW; + +#ifdef UNICODE + typedef MULTIKEYHELPW MULTIKEYHELP; + typedef PMULTIKEYHELPW PMULTIKEYHELP; + typedef LPMULTIKEYHELPW LPMULTIKEYHELP; +#else + typedef MULTIKEYHELPA MULTIKEYHELP; + typedef PMULTIKEYHELPA PMULTIKEYHELP; + typedef LPMULTIKEYHELPA LPMULTIKEYHELP; +#endif + + typedef struct tagHELPWININFOA { + int wStructSize; + int x; + int y; + int dx; + int dy; + int wMax; + CHAR rgchMember[2]; + } HELPWININFOA,*PHELPWININFOA,*LPHELPWININFOA; + + typedef struct tagHELPWININFOW { + int wStructSize; + int x; + int y; + int dx; + int dy; + int wMax; + WCHAR rgchMember[2]; + } HELPWININFOW,*PHELPWININFOW,*LPHELPWININFOW; + +#ifdef UNICODE + typedef HELPWININFOW HELPWININFO; + typedef PHELPWININFOW PHELPWININFO; + typedef LPHELPWININFOW LPHELPWININFO; +#else + typedef HELPWININFOA HELPWININFO; + typedef PHELPWININFOA PHELPWININFO; + typedef LPHELPWININFOA LPHELPWININFO; +#endif + +#define HELP_CONTEXT 0x0001L +#define HELP_QUIT 0x0002L +#define HELP_INDEX 0x0003L +#define HELP_CONTENTS 0x0003L +#define HELP_HELPONHELP 0x0004L +#define HELP_SETINDEX 0x0005L +#define HELP_SETCONTENTS 0x0005L +#define HELP_CONTEXTPOPUP 0x0008L +#define HELP_FORCEFILE 0x0009L +#define HELP_KEY 0x0101L +#define HELP_COMMAND 0x0102L +#define HELP_PARTIALKEY 0x0105L +#define HELP_MULTIKEY 0x0201L +#define HELP_SETWINPOS 0x0203L +#define HELP_CONTEXTMENU 0x000a +#define HELP_FINDER 0x000b +#define HELP_WM_HELP 0x000c +#define HELP_SETPOPUP_POS 0x000d + +#define HELP_TCARD 0x8000 +#define HELP_TCARD_DATA 0x0010 +#define HELP_TCARD_OTHER_CALLER 0x0011 + +#define IDH_NO_HELP 28440 +#define IDH_MISSING_CONTEXT 28441 +#define IDH_GENERIC_HELP_BUTTON 28442 +#define IDH_OK 28443 +#define IDH_CANCEL 28444 +#define IDH_HELP 28445 + +#ifdef UNICODE +#define WinHelp WinHelpW +#else +#define WinHelp WinHelpA +#endif + + WINUSERAPI WINBOOL WINAPI WinHelpA(HWND hWndMain,LPCSTR lpszHelp,UINT uCommand,ULONG_PTR dwData); + WINUSERAPI WINBOOL WINAPI WinHelpW(HWND hWndMain,LPCWSTR lpszHelp,UINT uCommand,ULONG_PTR dwData); +#endif + +#define GR_GDIOBJECTS 0 +#define GR_USEROBJECTS 1 + + WINUSERAPI DWORD WINAPI GetGuiResources(HANDLE hProcess,DWORD uiFlags); + +#ifndef NOSYSPARAMSINFO + +#define SPI_GETBEEP 0x0001 +#define SPI_SETBEEP 0x0002 +#define SPI_GETMOUSE 0x0003 +#define SPI_SETMOUSE 0x0004 +#define SPI_GETBORDER 0x0005 +#define SPI_SETBORDER 0x0006 +#define SPI_GETKEYBOARDSPEED 0x000A +#define SPI_SETKEYBOARDSPEED 0x000B +#define SPI_LANGDRIVER 0x000C +#define SPI_ICONHORIZONTALSPACING 0x000D +#define SPI_GETSCREENSAVETIMEOUT 0x000E +#define SPI_SETSCREENSAVETIMEOUT 0x000F +#define SPI_GETSCREENSAVEACTIVE 0x0010 +#define SPI_SETSCREENSAVEACTIVE 0x0011 +#define SPI_GETGRIDGRANULARITY 0x0012 +#define SPI_SETGRIDGRANULARITY 0x0013 +#define SPI_SETDESKWALLPAPER 0x0014 +#define SPI_SETDESKPATTERN 0x0015 +#define SPI_GETKEYBOARDDELAY 0x0016 +#define SPI_SETKEYBOARDDELAY 0x0017 +#define SPI_ICONVERTICALSPACING 0x0018 +#define SPI_GETICONTITLEWRAP 0x0019 +#define SPI_SETICONTITLEWRAP 0x001A +#define SPI_GETMENUDROPALIGNMENT 0x001B +#define SPI_SETMENUDROPALIGNMENT 0x001C +#define SPI_SETDOUBLECLKWIDTH 0x001D +#define SPI_SETDOUBLECLKHEIGHT 0x001E +#define SPI_GETICONTITLELOGFONT 0x001F +#define SPI_SETDOUBLECLICKTIME 0x0020 +#define SPI_SETMOUSEBUTTONSWAP 0x0021 +#define SPI_SETICONTITLELOGFONT 0x0022 +#define SPI_GETFASTTASKSWITCH 0x0023 +#define SPI_SETFASTTASKSWITCH 0x0024 +#define SPI_SETDRAGFULLWINDOWS 0x0025 +#define SPI_GETDRAGFULLWINDOWS 0x0026 +#define SPI_GETNONCLIENTMETRICS 0x0029 +#define SPI_SETNONCLIENTMETRICS 0x002A +#define SPI_GETMINIMIZEDMETRICS 0x002B +#define SPI_SETMINIMIZEDMETRICS 0x002C +#define SPI_GETICONMETRICS 0x002D +#define SPI_SETICONMETRICS 0x002E +#define SPI_SETWORKAREA 0x002F +#define SPI_GETWORKAREA 0x0030 +#define SPI_SETPENWINDOWS 0x0031 + +#define SPI_GETHIGHCONTRAST 0x0042 +#define SPI_SETHIGHCONTRAST 0x0043 +#define SPI_GETKEYBOARDPREF 0x0044 +#define SPI_SETKEYBOARDPREF 0x0045 +#define SPI_GETSCREENREADER 0x0046 +#define SPI_SETSCREENREADER 0x0047 +#define SPI_GETANIMATION 0x0048 +#define SPI_SETANIMATION 0x0049 +#define SPI_GETFONTSMOOTHING 0x004A +#define SPI_SETFONTSMOOTHING 0x004B +#define SPI_SETDRAGWIDTH 0x004C +#define SPI_SETDRAGHEIGHT 0x004D +#define SPI_SETHANDHELD 0x004E +#define SPI_GETLOWPOWERTIMEOUT 0x004F +#define SPI_GETPOWEROFFTIMEOUT 0x0050 +#define SPI_SETLOWPOWERTIMEOUT 0x0051 +#define SPI_SETPOWEROFFTIMEOUT 0x0052 +#define SPI_GETLOWPOWERACTIVE 0x0053 +#define SPI_GETPOWEROFFACTIVE 0x0054 +#define SPI_SETLOWPOWERACTIVE 0x0055 +#define SPI_SETPOWEROFFACTIVE 0x0056 +#define SPI_SETCURSORS 0x0057 +#define SPI_SETICONS 0x0058 +#define SPI_GETDEFAULTINPUTLANG 0x0059 +#define SPI_SETDEFAULTINPUTLANG 0x005A +#define SPI_SETLANGTOGGLE 0x005B +#define SPI_GETWINDOWSEXTENSION 0x005C +#define SPI_SETMOUSETRAILS 0x005D +#define SPI_GETMOUSETRAILS 0x005E +#define SPI_SETSCREENSAVERRUNNING 0x0061 +#define SPI_SCREENSAVERRUNNING SPI_SETSCREENSAVERRUNNING +#define SPI_GETFILTERKEYS 0x0032 +#define SPI_SETFILTERKEYS 0x0033 +#define SPI_GETTOGGLEKEYS 0x0034 +#define SPI_SETTOGGLEKEYS 0x0035 +#define SPI_GETMOUSEKEYS 0x0036 +#define SPI_SETMOUSEKEYS 0x0037 +#define SPI_GETSHOWSOUNDS 0x0038 +#define SPI_SETSHOWSOUNDS 0x0039 +#define SPI_GETSTICKYKEYS 0x003A +#define SPI_SETSTICKYKEYS 0x003B +#define SPI_GETACCESSTIMEOUT 0x003C +#define SPI_SETACCESSTIMEOUT 0x003D +#define SPI_GETSERIALKEYS 0x003E +#define SPI_SETSERIALKEYS 0x003F +#define SPI_GETSOUNDSENTRY 0x0040 +#define SPI_SETSOUNDSENTRY 0x0041 +#define SPI_GETSNAPTODEFBUTTON 0x005F +#define SPI_SETSNAPTODEFBUTTON 0x0060 +#define SPI_GETMOUSEHOVERWIDTH 0x0062 +#define SPI_SETMOUSEHOVERWIDTH 0x0063 +#define SPI_GETMOUSEHOVERHEIGHT 0x0064 +#define SPI_SETMOUSEHOVERHEIGHT 0x0065 +#define SPI_GETMOUSEHOVERTIME 0x0066 +#define SPI_SETMOUSEHOVERTIME 0x0067 +#define SPI_GETWHEELSCROLLLINES 0x0068 +#define SPI_SETWHEELSCROLLLINES 0x0069 +#define SPI_GETMENUSHOWDELAY 0x006A +#define SPI_SETMENUSHOWDELAY 0x006B +#define SPI_GETSHOWIMEUI 0x006E +#define SPI_SETSHOWIMEUI 0x006F +#define SPI_GETMOUSESPEED 0x0070 +#define SPI_SETMOUSESPEED 0x0071 +#define SPI_GETSCREENSAVERRUNNING 0x0072 +#define SPI_GETDESKWALLPAPER 0x0073 + +#define SPI_GETACTIVEWINDOWTRACKING 0x1000 +#define SPI_SETACTIVEWINDOWTRACKING 0x1001 +#define SPI_GETMENUANIMATION 0x1002 +#define SPI_SETMENUANIMATION 0x1003 +#define SPI_GETCOMBOBOXANIMATION 0x1004 +#define SPI_SETCOMBOBOXANIMATION 0x1005 +#define SPI_GETLISTBOXSMOOTHSCROLLING 0x1006 +#define SPI_SETLISTBOXSMOOTHSCROLLING 0x1007 +#define SPI_GETGRADIENTCAPTIONS 0x1008 +#define SPI_SETGRADIENTCAPTIONS 0x1009 +#define SPI_GETKEYBOARDCUES 0x100A +#define SPI_SETKEYBOARDCUES 0x100B +#define SPI_GETMENUUNDERLINES SPI_GETKEYBOARDCUES +#define SPI_SETMENUUNDERLINES SPI_SETKEYBOARDCUES +#define SPI_GETACTIVEWNDTRKZORDER 0x100C +#define SPI_SETACTIVEWNDTRKZORDER 0x100D +#define SPI_GETHOTTRACKING 0x100E +#define SPI_SETHOTTRACKING 0x100F +#define SPI_GETMENUFADE 0x1012 +#define SPI_SETMENUFADE 0x1013 +#define SPI_GETSELECTIONFADE 0x1014 +#define SPI_SETSELECTIONFADE 0x1015 +#define SPI_GETTOOLTIPANIMATION 0x1016 +#define SPI_SETTOOLTIPANIMATION 0x1017 +#define SPI_GETTOOLTIPFADE 0x1018 +#define SPI_SETTOOLTIPFADE 0x1019 +#define SPI_GETCURSORSHADOW 0x101A +#define SPI_SETCURSORSHADOW 0x101B +#define SPI_GETMOUSESONAR 0x101C +#define SPI_SETMOUSESONAR 0x101D +#define SPI_GETMOUSECLICKLOCK 0x101E +#define SPI_SETMOUSECLICKLOCK 0x101F +#define SPI_GETMOUSEVANISH 0x1020 +#define SPI_SETMOUSEVANISH 0x1021 +#define SPI_GETFLATMENU 0x1022 +#define SPI_SETFLATMENU 0x1023 +#define SPI_GETDROPSHADOW 0x1024 +#define SPI_SETDROPSHADOW 0x1025 +#define SPI_GETBLOCKSENDINPUTRESETS 0x1026 +#define SPI_SETBLOCKSENDINPUTRESETS 0x1027 +#define SPI_GETUIEFFECTS 0x103E +#define SPI_SETUIEFFECTS 0x103F +#define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000 +#define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001 +#define SPI_GETACTIVEWNDTRKTIMEOUT 0x2002 +#define SPI_SETACTIVEWNDTRKTIMEOUT 0x2003 +#define SPI_GETFOREGROUNDFLASHCOUNT 0x2004 +#define SPI_SETFOREGROUNDFLASHCOUNT 0x2005 +#define SPI_GETCARETWIDTH 0x2006 +#define SPI_SETCARETWIDTH 0x2007 +#define SPI_GETMOUSECLICKLOCKTIME 0x2008 +#define SPI_SETMOUSECLICKLOCKTIME 0x2009 +#define SPI_GETFONTSMOOTHINGTYPE 0x200A +#define SPI_SETFONTSMOOTHINGTYPE 0x200B + +#define FE_FONTSMOOTHINGSTANDARD 0x0001 +#define FE_FONTSMOOTHINGCLEARTYPE 0x0002 +#define FE_FONTSMOOTHINGDOCKING 0x8000 + +#define SPI_GETFONTSMOOTHINGCONTRAST 0x200C +#define SPI_SETFONTSMOOTHINGCONTRAST 0x200D +#define SPI_GETFOCUSBORDERWIDTH 0x200E +#define SPI_SETFOCUSBORDERWIDTH 0x200F +#define SPI_GETFOCUSBORDERHEIGHT 0x2010 +#define SPI_SETFOCUSBORDERHEIGHT 0x2011 +#define SPI_GETFONTSMOOTHINGORIENTATION 0x2012 +#define SPI_SETFONTSMOOTHINGORIENTATION 0x2013 + +#define FE_FONTSMOOTHINGORIENTATIONBGR 0x0000 +#define FE_FONTSMOOTHINGORIENTATIONRGB 0x0001 + +#define SPIF_UPDATEINIFILE 0x0001 +#define SPIF_SENDWININICHANGE 0x0002 +#define SPIF_SENDCHANGE SPIF_SENDWININICHANGE + +#define METRICS_USEDEFAULT -1 +#ifdef _WINGDI_ +#ifndef NOGDI + typedef struct tagNONCLIENTMETRICSA { + UINT cbSize; + int iBorderWidth; + int iScrollWidth; + int iScrollHeight; + int iCaptionWidth; + int iCaptionHeight; + LOGFONTA lfCaptionFont; + int iSmCaptionWidth; + int iSmCaptionHeight; + LOGFONTA lfSmCaptionFont; + int iMenuWidth; + int iMenuHeight; + LOGFONTA lfMenuFont; + LOGFONTA lfStatusFont; + LOGFONTA lfMessageFont; + } NONCLIENTMETRICSA,*PNONCLIENTMETRICSA,*LPNONCLIENTMETRICSA; + + typedef struct tagNONCLIENTMETRICSW { + UINT cbSize; + int iBorderWidth; + int iScrollWidth; + int iScrollHeight; + int iCaptionWidth; + int iCaptionHeight; + LOGFONTW lfCaptionFont; + int iSmCaptionWidth; + int iSmCaptionHeight; + LOGFONTW lfSmCaptionFont; + int iMenuWidth; + int iMenuHeight; + LOGFONTW lfMenuFont; + LOGFONTW lfStatusFont; + LOGFONTW lfMessageFont; + } NONCLIENTMETRICSW,*PNONCLIENTMETRICSW,*LPNONCLIENTMETRICSW; + +#ifdef UNICODE + typedef NONCLIENTMETRICSW NONCLIENTMETRICS; + typedef PNONCLIENTMETRICSW PNONCLIENTMETRICS; + typedef LPNONCLIENTMETRICSW LPNONCLIENTMETRICS; +#else + typedef NONCLIENTMETRICSA NONCLIENTMETRICS; + typedef PNONCLIENTMETRICSA PNONCLIENTMETRICS; + typedef LPNONCLIENTMETRICSA LPNONCLIENTMETRICS; +#endif +#endif +#endif + +#define ARW_BOTTOMLEFT 0x0000L +#define ARW_BOTTOMRIGHT 0x0001L +#define ARW_TOPLEFT 0x0002L +#define ARW_TOPRIGHT 0x0003L +#define ARW_STARTMASK 0x0003L +#define ARW_STARTRIGHT 0x0001L +#define ARW_STARTTOP 0x0002L + +#define ARW_LEFT 0x0000L +#define ARW_RIGHT 0x0000L +#define ARW_UP 0x0004L +#define ARW_DOWN 0x0004L +#define ARW_HIDE 0x0008L + + typedef struct tagMINIMIZEDMETRICS { + UINT cbSize; + int iWidth; + int iHorzGap; + int iVertGap; + int iArrange; + } MINIMIZEDMETRICS,*PMINIMIZEDMETRICS,*LPMINIMIZEDMETRICS; + +#ifdef _WINGDI_ +#ifndef NOGDI + typedef struct tagICONMETRICSA { + UINT cbSize; + int iHorzSpacing; + int iVertSpacing; + int iTitleWrap; + LOGFONTA lfFont; + } ICONMETRICSA,*PICONMETRICSA,*LPICONMETRICSA; + + typedef struct tagICONMETRICSW { + UINT cbSize; + int iHorzSpacing; + int iVertSpacing; + int iTitleWrap; + LOGFONTW lfFont; + } ICONMETRICSW,*PICONMETRICSW,*LPICONMETRICSW; + +#ifdef UNICODE + typedef ICONMETRICSW ICONMETRICS; + typedef PICONMETRICSW PICONMETRICS; + typedef LPICONMETRICSW LPICONMETRICS; +#else + typedef ICONMETRICSA ICONMETRICS; + typedef PICONMETRICSA PICONMETRICS; + typedef LPICONMETRICSA LPICONMETRICS; +#endif +#endif +#endif + + typedef struct tagANIMATIONINFO { + UINT cbSize; + int iMinAnimate; + } ANIMATIONINFO,*LPANIMATIONINFO; + + typedef struct tagSERIALKEYSA { + UINT cbSize; + DWORD dwFlags; + LPSTR lpszActivePort; + LPSTR lpszPort; + UINT iBaudRate; + UINT iPortState; + UINT iActive; + } SERIALKEYSA,*LPSERIALKEYSA; + + typedef struct tagSERIALKEYSW { + UINT cbSize; + DWORD dwFlags; + LPWSTR lpszActivePort; + LPWSTR lpszPort; + UINT iBaudRate; + UINT iPortState; + UINT iActive; + } SERIALKEYSW,*LPSERIALKEYSW; + +#ifdef UNICODE + typedef SERIALKEYSW SERIALKEYS; + typedef LPSERIALKEYSW LPSERIALKEYS; +#else + typedef SERIALKEYSA SERIALKEYS; + typedef LPSERIALKEYSA LPSERIALKEYS; +#endif + +#define SERKF_SERIALKEYSON 0x00000001 +#define SERKF_AVAILABLE 0x00000002 +#define SERKF_INDICATOR 0x00000004 + + typedef struct tagHIGHCONTRASTA { + UINT cbSize; + DWORD dwFlags; + LPSTR lpszDefaultScheme; + } HIGHCONTRASTA,*LPHIGHCONTRASTA; + + typedef struct tagHIGHCONTRASTW { + UINT cbSize; + DWORD dwFlags; + LPWSTR lpszDefaultScheme; + } HIGHCONTRASTW,*LPHIGHCONTRASTW; + +#ifdef UNICODE + typedef HIGHCONTRASTW HIGHCONTRAST; + typedef LPHIGHCONTRASTW LPHIGHCONTRAST; +#else + typedef HIGHCONTRASTA HIGHCONTRAST; + typedef LPHIGHCONTRASTA LPHIGHCONTRAST; +#endif + +#define HCF_HIGHCONTRASTON 0x00000001 +#define HCF_AVAILABLE 0x00000002 +#define HCF_HOTKEYACTIVE 0x00000004 +#define HCF_CONFIRMHOTKEY 0x00000008 +#define HCF_HOTKEYSOUND 0x00000010 +#define HCF_INDICATOR 0x00000020 +#define HCF_HOTKEYAVAILABLE 0x00000040 +#define HCF_LOGONDESKTOP 0x00000100 +#define HCF_DEFAULTDESKTOP 0x00000200 + +#define CDS_UPDATEREGISTRY 0x00000001 +#define CDS_TEST 0x00000002 +#define CDS_FULLSCREEN 0x00000004 +#define CDS_GLOBAL 0x00000008 +#define CDS_SET_PRIMARY 0x00000010 +#define CDS_VIDEOPARAMETERS 0x00000020 +#define CDS_RESET 0x40000000 +#define CDS_NORESET 0x10000000 + +//gr #include + +#define DISP_CHANGE_SUCCESSFUL 0 +#define DISP_CHANGE_RESTART 1 +#define DISP_CHANGE_FAILED -1 +#define DISP_CHANGE_BADMODE -2 +#define DISP_CHANGE_NOTUPDATED -3 +#define DISP_CHANGE_BADFLAGS -4 +#define DISP_CHANGE_BADPARAM -5 +#define DISP_CHANGE_BADDUALVIEW -6 + +#ifdef _WINGDI_ +#ifndef NOGDI + +#ifdef UNICODE +#define ChangeDisplaySettings ChangeDisplaySettingsW +#define ChangeDisplaySettingsEx ChangeDisplaySettingsExW +#define EnumDisplaySettings EnumDisplaySettingsW +#define EnumDisplaySettingsEx EnumDisplaySettingsExW +#define EnumDisplayDevices EnumDisplayDevicesW +#else +#define ChangeDisplaySettings ChangeDisplaySettingsA +#define ChangeDisplaySettingsEx ChangeDisplaySettingsExA +#define EnumDisplaySettings EnumDisplaySettingsA +#define EnumDisplaySettingsEx EnumDisplaySettingsExA +#define EnumDisplayDevices EnumDisplayDevicesA +#endif + + WINUSERAPI LONG WINAPI ChangeDisplaySettingsA(LPDEVMODEA lpDevMode,DWORD dwFlags); + WINUSERAPI LONG WINAPI ChangeDisplaySettingsW(LPDEVMODEW lpDevMode,DWORD dwFlags); + WINUSERAPI LONG WINAPI ChangeDisplaySettingsExA(LPCSTR lpszDeviceName,LPDEVMODEA lpDevMode,HWND hwnd,DWORD dwflags,LPVOID lParam); + WINUSERAPI LONG WINAPI ChangeDisplaySettingsExW(LPCWSTR lpszDeviceName,LPDEVMODEW lpDevMode,HWND hwnd,DWORD dwflags,LPVOID lParam); + +#define ENUM_CURRENT_SETTINGS ((DWORD)-1) +#define ENUM_REGISTRY_SETTINGS ((DWORD)-2) + + WINUSERAPI WINBOOL WINAPI EnumDisplaySettingsA(LPCSTR lpszDeviceName,DWORD iModeNum,LPDEVMODEA lpDevMode); + WINUSERAPI WINBOOL WINAPI EnumDisplaySettingsW(LPCWSTR lpszDeviceName,DWORD iModeNum,LPDEVMODEW lpDevMode); + WINUSERAPI WINBOOL WINAPI EnumDisplaySettingsExA(LPCSTR lpszDeviceName,DWORD iModeNum,LPDEVMODEA lpDevMode,DWORD dwFlags); + WINUSERAPI WINBOOL WINAPI EnumDisplaySettingsExW(LPCWSTR lpszDeviceName,DWORD iModeNum,LPDEVMODEW lpDevMode,DWORD dwFlags); + +#define EDS_RAWMODE 0x00000002 + + WINUSERAPI WINBOOL WINAPI EnumDisplayDevicesA(LPCSTR lpDevice,DWORD iDevNum,PDISPLAY_DEVICEA lpDisplayDevice,DWORD dwFlags); + WINUSERAPI WINBOOL WINAPI EnumDisplayDevicesW(LPCWSTR lpDevice,DWORD iDevNum,PDISPLAY_DEVICEW lpDisplayDevice,DWORD dwFlags); +#endif +#endif + +#ifdef UNICODE +#define SystemParametersInfo SystemParametersInfoW +#else +#define SystemParametersInfo SystemParametersInfoA +#endif + + WINUSERAPI WINBOOL WINAPI SystemParametersInfoA(UINT uiAction,UINT uiParam,PVOID pvParam,UINT fWinIni); + WINUSERAPI WINBOOL WINAPI SystemParametersInfoW(UINT uiAction,UINT uiParam,PVOID pvParam,UINT fWinIni); +#endif + + typedef struct tagFILTERKEYS { + UINT cbSize; + DWORD dwFlags; + DWORD iWaitMSec; + DWORD iDelayMSec; + DWORD iRepeatMSec; + DWORD iBounceMSec; + } FILTERKEYS,*LPFILTERKEYS; + +#define FKF_FILTERKEYSON 0x00000001 +#define FKF_AVAILABLE 0x00000002 +#define FKF_HOTKEYACTIVE 0x00000004 +#define FKF_CONFIRMHOTKEY 0x00000008 +#define FKF_HOTKEYSOUND 0x00000010 +#define FKF_INDICATOR 0x00000020 +#define FKF_CLICKON 0x00000040 + + typedef struct tagSTICKYKEYS { + UINT cbSize; + DWORD dwFlags; + } STICKYKEYS,*LPSTICKYKEYS; + +#define SKF_STICKYKEYSON 0x00000001 +#define SKF_AVAILABLE 0x00000002 +#define SKF_HOTKEYACTIVE 0x00000004 +#define SKF_CONFIRMHOTKEY 0x00000008 +#define SKF_HOTKEYSOUND 0x00000010 +#define SKF_INDICATOR 0x00000020 +#define SKF_AUDIBLEFEEDBACK 0x00000040 +#define SKF_TRISTATE 0x00000080 +#define SKF_TWOKEYSOFF 0x00000100 +#define SKF_LALTLATCHED 0x10000000 +#define SKF_LCTLLATCHED 0x04000000 +#define SKF_LSHIFTLATCHED 0x01000000 +#define SKF_RALTLATCHED 0x20000000 +#define SKF_RCTLLATCHED 0x08000000 +#define SKF_RSHIFTLATCHED 0x02000000 +#define SKF_LWINLATCHED 0x40000000 +#define SKF_RWINLATCHED 0x80000000 +#define SKF_LALTLOCKED 0x00100000 +#define SKF_LCTLLOCKED 0x00040000 +#define SKF_LSHIFTLOCKED 0x00010000 +#define SKF_RALTLOCKED 0x00200000 +#define SKF_RCTLLOCKED 0x00080000 +#define SKF_RSHIFTLOCKED 0x00020000 +#define SKF_LWINLOCKED 0x00400000 +#define SKF_RWINLOCKED 0x00800000 + + typedef struct tagMOUSEKEYS { + UINT cbSize; + DWORD dwFlags; + DWORD iMaxSpeed; + DWORD iTimeToMaxSpeed; + DWORD iCtrlSpeed; + DWORD dwReserved1; + DWORD dwReserved2; + } MOUSEKEYS,*LPMOUSEKEYS; + +#define MKF_MOUSEKEYSON 0x00000001 +#define MKF_AVAILABLE 0x00000002 +#define MKF_HOTKEYACTIVE 0x00000004 +#define MKF_CONFIRMHOTKEY 0x00000008 +#define MKF_HOTKEYSOUND 0x00000010 +#define MKF_INDICATOR 0x00000020 +#define MKF_MODIFIERS 0x00000040 +#define MKF_REPLACENUMBERS 0x00000080 +#define MKF_LEFTBUTTONSEL 0x10000000 +#define MKF_RIGHTBUTTONSEL 0x20000000 +#define MKF_LEFTBUTTONDOWN 0x01000000 +#define MKF_RIGHTBUTTONDOWN 0x02000000 +#define MKF_MOUSEMODE 0x80000000 + + typedef struct tagACCESSTIMEOUT { + UINT cbSize; + DWORD dwFlags; + DWORD iTimeOutMSec; + } ACCESSTIMEOUT,*LPACCESSTIMEOUT; + +#define ATF_TIMEOUTON 0x00000001 +#define ATF_ONOFFFEEDBACK 0x00000002 + +#define SSGF_NONE 0 +#define SSGF_DISPLAY 3 + +#define SSTF_NONE 0 +#define SSTF_CHARS 1 +#define SSTF_BORDER 2 +#define SSTF_DISPLAY 3 + +#define SSWF_NONE 0 +#define SSWF_TITLE 1 +#define SSWF_WINDOW 2 +#define SSWF_DISPLAY 3 +#define SSWF_CUSTOM 4 + + typedef struct tagSOUNDSENTRYA { + UINT cbSize; + DWORD dwFlags; + DWORD iFSTextEffect; + DWORD iFSTextEffectMSec; + DWORD iFSTextEffectColorBits; + DWORD iFSGrafEffect; + DWORD iFSGrafEffectMSec; + DWORD iFSGrafEffectColor; + DWORD iWindowsEffect; + DWORD iWindowsEffectMSec; + LPSTR lpszWindowsEffectDLL; + DWORD iWindowsEffectOrdinal; + } SOUNDSENTRYA,*LPSOUNDSENTRYA; + + typedef struct tagSOUNDSENTRYW { + UINT cbSize; + DWORD dwFlags; + DWORD iFSTextEffect; + DWORD iFSTextEffectMSec; + DWORD iFSTextEffectColorBits; + DWORD iFSGrafEffect; + DWORD iFSGrafEffectMSec; + DWORD iFSGrafEffectColor; + DWORD iWindowsEffect; + DWORD iWindowsEffectMSec; + LPWSTR lpszWindowsEffectDLL; + DWORD iWindowsEffectOrdinal; + } SOUNDSENTRYW,*LPSOUNDSENTRYW; + +#ifdef UNICODE + typedef SOUNDSENTRYW SOUNDSENTRY; + typedef LPSOUNDSENTRYW LPSOUNDSENTRY; +#else + typedef SOUNDSENTRYA SOUNDSENTRY; + typedef LPSOUNDSENTRYA LPSOUNDSENTRY; +#endif + +#define SSF_SOUNDSENTRYON 0x00000001 +#define SSF_AVAILABLE 0x00000002 +#define SSF_INDICATOR 0x00000004 + + typedef struct tagTOGGLEKEYS { + UINT cbSize; + DWORD dwFlags; + } TOGGLEKEYS,*LPTOGGLEKEYS; + +#define TKF_TOGGLEKEYSON 0x00000001 +#define TKF_AVAILABLE 0x00000002 +#define TKF_HOTKEYACTIVE 0x00000004 +#define TKF_CONFIRMHOTKEY 0x00000008 +#define TKF_HOTKEYSOUND 0x00000010 +#define TKF_INDICATOR 0x00000020 + + WINUSERAPI VOID WINAPI SetDebugErrorLevel(DWORD dwLevel); + +#define SLE_ERROR 0x00000001 +#define SLE_MINORERROR 0x00000002 +#define SLE_WARNING 0x00000003 + + WINUSERAPI VOID WINAPI SetLastErrorEx(DWORD dwErrCode,DWORD dwType); + WINUSERAPI int WINAPI InternalGetWindowText(HWND hWnd,LPWSTR pString,int cchMaxCount); + +#ifdef WINNT + WINUSERAPI WINBOOL WINAPI EndTask(HWND hWnd,WINBOOL fShutDown,WINBOOL fForce); +#endif + +#define MONITOR_DEFAULTTONULL 0x00000000 +#define MONITOR_DEFAULTTOPRIMARY 0x00000001 +#define MONITOR_DEFAULTTONEAREST 0x00000002 + + WINUSERAPI HMONITOR WINAPI MonitorFromPoint(POINT pt,DWORD dwFlags); + WINUSERAPI HMONITOR WINAPI MonitorFromRect(LPCRECT lprc,DWORD dwFlags); + WINUSERAPI HMONITOR WINAPI MonitorFromWindow(HWND hwnd,DWORD dwFlags); + +#define MONITORINFOF_PRIMARY 0x00000001 + +#ifndef CCHDEVICENAME +#define CCHDEVICENAME 32 +#endif + + typedef struct tagMONITORINFO { + DWORD cbSize; + RECT rcMonitor; + RECT rcWork; + DWORD dwFlags; + } MONITORINFO,*LPMONITORINFO; + +#ifdef __cplusplus + typedef struct tagMONITORINFOEXA : public tagMONITORINFO { + CHAR szDevice[CCHDEVICENAME]; + } MONITORINFOEXA,*LPMONITORINFOEXA; + + typedef struct tagMONITORINFOEXW : public tagMONITORINFO { + WCHAR szDevice[CCHDEVICENAME]; + } MONITORINFOEXW,*LPMONITORINFOEXW; + +#ifdef UNICODE + typedef MONITORINFOEXW MONITORINFOEX; + typedef LPMONITORINFOEXW LPMONITORINFOEX; +#else + typedef MONITORINFOEXA MONITORINFOEX; + typedef LPMONITORINFOEXA LPMONITORINFOEX; +#endif +#else + typedef struct tagMONITORINFOEXA { + MONITORINFO mi; + CHAR szDevice[CCHDEVICENAME]; + } MONITORINFOEXA,*LPMONITORINFOEXA; + + typedef struct tagMONITORINFOEXW { + MONITORINFO mi; + WCHAR szDevice[CCHDEVICENAME]; + } MONITORINFOEXW,*LPMONITORINFOEXW; +#ifdef UNICODE + typedef MONITORINFOEXW MONITORINFOEX; + typedef LPMONITORINFOEXW LPMONITORINFOEX; +#else + typedef MONITORINFOEXA MONITORINFOEX; + typedef LPMONITORINFOEXA LPMONITORINFOEX; +#endif +#endif + +#ifdef UNICODE +#define GetMonitorInfo GetMonitorInfoW +#else +#define GetMonitorInfo GetMonitorInfoA +#endif + + WINUSERAPI WINBOOL WINAPI GetMonitorInfoA(HMONITOR hMonitor,LPMONITORINFO lpmi); + WINUSERAPI WINBOOL WINAPI GetMonitorInfoW(HMONITOR hMonitor,LPMONITORINFO lpmi); + + typedef WINBOOL (CALLBACK *MONITORENUMPROC)(HMONITOR,HDC,LPRECT,LPARAM); + + WINUSERAPI WINBOOL WINAPI EnumDisplayMonitors(HDC hdc,LPCRECT lprcClip,MONITORENUMPROC lpfnEnum,LPARAM dwData); + +#ifndef NOWINABLE + WINUSERAPI VOID WINAPI NotifyWinEvent(DWORD event,HWND hwnd,LONG idObject,LONG idChild); + + typedef VOID (CALLBACK *WINEVENTPROC)(HWINEVENTHOOK hWinEventHook,DWORD event,HWND hwnd,LONG idObject,LONG idChild,DWORD idEventThread,DWORD dwmsEventTime); + + WINUSERAPI HWINEVENTHOOK WINAPI SetWinEventHook(DWORD eventMin,DWORD eventMax,HMODULE hmodWinEventProc,WINEVENTPROC pfnWinEventProc,DWORD idProcess,DWORD idThread,DWORD dwFlags); + WINUSERAPI WINBOOL WINAPI IsWinEventHookInstalled(DWORD event); + +#define WINEVENT_OUTOFCONTEXT 0x0000 +#define WINEVENT_SKIPOWNTHREAD 0x0001 +#define WINEVENT_SKIPOWNPROCESS 0x0002 +#define WINEVENT_INCONTEXT 0x0004 + + WINUSERAPI WINBOOL WINAPI UnhookWinEvent(HWINEVENTHOOK hWinEventHook); + +#define CHILDID_SELF 0 +#define INDEXID_OBJECT 0 +#define INDEXID_CONTAINER 0 + +#define OBJID_WINDOW ((LONG)0x00000000) +#define OBJID_SYSMENU ((LONG)0xFFFFFFFF) +#define OBJID_TITLEBAR ((LONG)0xFFFFFFFE) +#define OBJID_MENU ((LONG)0xFFFFFFFD) +#define OBJID_CLIENT ((LONG)0xFFFFFFFC) +#define OBJID_VSCROLL ((LONG)0xFFFFFFFB) +#define OBJID_HSCROLL ((LONG)0xFFFFFFFA) +#define OBJID_SIZEGRIP ((LONG)0xFFFFFFF9) +#define OBJID_CARET ((LONG)0xFFFFFFF8) +#define OBJID_CURSOR ((LONG)0xFFFFFFF7) +#define OBJID_ALERT ((LONG)0xFFFFFFF6) +#define OBJID_SOUND ((LONG)0xFFFFFFF5) +#define OBJID_QUERYCLASSNAMEIDX ((LONG)0xFFFFFFF4) +#define OBJID_NATIVEOM ((LONG)0xFFFFFFF0) + +#define EVENT_MIN 0x00000001 +#define EVENT_MAX 0x7FFFFFFF + +#define EVENT_SYSTEM_SOUND 0x0001 +#define EVENT_SYSTEM_ALERT 0x0002 +#define EVENT_SYSTEM_FOREGROUND 0x0003 +#define EVENT_SYSTEM_MENUSTART 0x0004 +#define EVENT_SYSTEM_MENUEND 0x0005 +#define EVENT_SYSTEM_MENUPOPUPSTART 0x0006 +#define EVENT_SYSTEM_MENUPOPUPEND 0x0007 +#define EVENT_SYSTEM_CAPTURESTART 0x0008 +#define EVENT_SYSTEM_CAPTUREEND 0x0009 +#define EVENT_SYSTEM_MOVESIZESTART 0x000A +#define EVENT_SYSTEM_MOVESIZEEND 0x000B +#define EVENT_SYSTEM_CONTEXTHELPSTART 0x000C +#define EVENT_SYSTEM_CONTEXTHELPEND 0x000D +#define EVENT_SYSTEM_DRAGDROPSTART 0x000E +#define EVENT_SYSTEM_DRAGDROPEND 0x000F +#define EVENT_SYSTEM_DIALOGSTART 0x0010 +#define EVENT_SYSTEM_DIALOGEND 0x0011 +#define EVENT_SYSTEM_SCROLLINGSTART 0x0012 +#define EVENT_SYSTEM_SCROLLINGEND 0x0013 +#define EVENT_SYSTEM_SWITCHSTART 0x0014 +#define EVENT_SYSTEM_SWITCHEND 0x0015 +#define EVENT_SYSTEM_MINIMIZESTART 0x0016 +#define EVENT_SYSTEM_MINIMIZEEND 0x0017 + +#define EVENT_CONSOLE_CARET 0x4001 +#define EVENT_CONSOLE_UPDATE_REGION 0x4002 +#define EVENT_CONSOLE_UPDATE_SIMPLE 0x4003 +#define EVENT_CONSOLE_UPDATE_SCROLL 0x4004 +#define EVENT_CONSOLE_LAYOUT 0x4005 +#define EVENT_CONSOLE_START_APPLICATION 0x4006 +#define EVENT_CONSOLE_END_APPLICATION 0x4007 + +#define CONSOLE_APPLICATION_16BIT 0x0001 + +#define CONSOLE_CARET_SELECTION 0x0001 +#define CONSOLE_CARET_VISIBLE 0x0002 + +#define EVENT_OBJECT_CREATE 0x8000 +#define EVENT_OBJECT_DESTROY 0x8001 +#define EVENT_OBJECT_SHOW 0x8002 +#define EVENT_OBJECT_HIDE 0x8003 +#define EVENT_OBJECT_REORDER 0x8004 + +#define EVENT_OBJECT_FOCUS 0x8005 +#define EVENT_OBJECT_SELECTION 0x8006 +#define EVENT_OBJECT_SELECTIONADD 0x8007 +#define EVENT_OBJECT_SELECTIONREMOVE 0x8008 +#define EVENT_OBJECT_SELECTIONWITHIN 0x8009 + +#define EVENT_OBJECT_STATECHANGE 0x800A + +#define EVENT_OBJECT_LOCATIONCHANGE 0x800B + +#define EVENT_OBJECT_NAMECHANGE 0x800C +#define EVENT_OBJECT_DESCRIPTIONCHANGE 0x800D +#define EVENT_OBJECT_VALUECHANGE 0x800E +#define EVENT_OBJECT_PARENTCHANGE 0x800F +#define EVENT_OBJECT_HELPCHANGE 0x8010 +#define EVENT_OBJECT_DEFACTIONCHANGE 0x8011 +#define EVENT_OBJECT_ACCELERATORCHANGE 0x8012 + +#define SOUND_SYSTEM_STARTUP 1 +#define SOUND_SYSTEM_SHUTDOWN 2 +#define SOUND_SYSTEM_BEEP 3 +#define SOUND_SYSTEM_ERROR 4 +#define SOUND_SYSTEM_QUESTION 5 +#define SOUND_SYSTEM_WARNING 6 +#define SOUND_SYSTEM_INFORMATION 7 +#define SOUND_SYSTEM_MAXIMIZE 8 +#define SOUND_SYSTEM_MINIMIZE 9 +#define SOUND_SYSTEM_RESTOREUP 10 +#define SOUND_SYSTEM_RESTOREDOWN 11 +#define SOUND_SYSTEM_APPSTART 12 +#define SOUND_SYSTEM_FAULT 13 +#define SOUND_SYSTEM_APPEND 14 +#define SOUND_SYSTEM_MENUCOMMAND 15 +#define SOUND_SYSTEM_MENUPOPUP 16 +#define CSOUND_SYSTEM 16 + +#define ALERT_SYSTEM_INFORMATIONAL 1 +#define ALERT_SYSTEM_WARNING 2 +#define ALERT_SYSTEM_ERROR 3 +#define ALERT_SYSTEM_QUERY 4 +#define ALERT_SYSTEM_CRITICAL 5 +#define CALERT_SYSTEM 6 + + typedef struct tagGUITHREADINFO { + DWORD cbSize; + DWORD flags; + HWND hwndActive; + HWND hwndFocus; + HWND hwndCapture; + HWND hwndMenuOwner; + HWND hwndMoveSize; + HWND hwndCaret; + RECT rcCaret; + } GUITHREADINFO,*PGUITHREADINFO,*LPGUITHREADINFO; + +#define GUI_CARETBLINKING 0x00000001 +#define GUI_INMOVESIZE 0x00000002 +#define GUI_INMENUMODE 0x00000004 +#define GUI_SYSTEMMENUMODE 0x00000008 +#define GUI_POPUPMENUMODE 0x00000010 +#define GUI_16BITTASK 0x00000020 + +#ifdef UNICODE +#define GetWindowModuleFileName GetWindowModuleFileNameW +#else +#define GetWindowModuleFileName GetWindowModuleFileNameA +#endif + + WINUSERAPI WINBOOL WINAPI GetGUIThreadInfo(DWORD idThread,PGUITHREADINFO pgui); + WINUSERAPI UINT WINAPI GetWindowModuleFileNameA(HWND hwnd,LPSTR pszFileName,UINT cchFileNameMax); + WINUSERAPI UINT WINAPI GetWindowModuleFileNameW(HWND hwnd,LPWSTR pszFileName,UINT cchFileNameMax); + +#ifndef NO_STATE_FLAGS +#define STATE_SYSTEM_UNAVAILABLE 0x00000001 +#define STATE_SYSTEM_SELECTED 0x00000002 +#define STATE_SYSTEM_FOCUSED 0x00000004 +#define STATE_SYSTEM_PRESSED 0x00000008 +#define STATE_SYSTEM_CHECKED 0x00000010 +#define STATE_SYSTEM_MIXED 0x00000020 +#define STATE_SYSTEM_INDETERMINATE STATE_SYSTEM_MIXED +#define STATE_SYSTEM_READONLY 0x00000040 +#define STATE_SYSTEM_HOTTRACKED 0x00000080 +#define STATE_SYSTEM_DEFAULT 0x00000100 +#define STATE_SYSTEM_EXPANDED 0x00000200 +#define STATE_SYSTEM_COLLAPSED 0x00000400 +#define STATE_SYSTEM_BUSY 0x00000800 +#define STATE_SYSTEM_FLOATING 0x00001000 +#define STATE_SYSTEM_MARQUEED 0x00002000 +#define STATE_SYSTEM_ANIMATED 0x00004000 +#define STATE_SYSTEM_INVISIBLE 0x00008000 +#define STATE_SYSTEM_OFFSCREEN 0x00010000 +#define STATE_SYSTEM_SIZEABLE 0x00020000 +#define STATE_SYSTEM_MOVEABLE 0x00040000 +#define STATE_SYSTEM_SELFVOICING 0x00080000 +#define STATE_SYSTEM_FOCUSABLE 0x00100000 +#define STATE_SYSTEM_SELECTABLE 0x00200000 +#define STATE_SYSTEM_LINKED 0x00400000 +#define STATE_SYSTEM_TRAVERSED 0x00800000 +#define STATE_SYSTEM_MULTISELECTABLE 0x01000000 +#define STATE_SYSTEM_EXTSELECTABLE 0x02000000 +#define STATE_SYSTEM_ALERT_LOW 0x04000000 +#define STATE_SYSTEM_ALERT_MEDIUM 0x08000000 +#define STATE_SYSTEM_ALERT_HIGH 0x10000000 +#define STATE_SYSTEM_PROTECTED 0x20000000 +#define STATE_SYSTEM_VALID 0x3FFFFFFF +#endif + +#define CCHILDREN_TITLEBAR 5 +#define CCHILDREN_SCROLLBAR 5 + + typedef struct tagCURSORINFO { + DWORD cbSize; + DWORD flags; + HCURSOR hCursor; + POINT ptScreenPos; + } CURSORINFO,*PCURSORINFO,*LPCURSORINFO; + +#define CURSOR_SHOWING 0x00000001 + + WINUSERAPI WINBOOL WINAPI GetCursorInfo(PCURSORINFO pci); + + typedef struct tagWINDOWINFO { + DWORD cbSize; + RECT rcWindow; + RECT rcClient; + DWORD dwStyle; + DWORD dwExStyle; + DWORD dwWindowStatus; + UINT cxWindowBorders; + UINT cyWindowBorders; + ATOM atomWindowType; + WORD wCreatorVersion; + } WINDOWINFO,*PWINDOWINFO,*LPWINDOWINFO; + +#define WS_ACTIVECAPTION 0x0001 + + WINUSERAPI WINBOOL WINAPI GetWindowInfo(HWND hwnd,PWINDOWINFO pwi); + + typedef struct tagTITLEBARINFO { + DWORD cbSize; + RECT rcTitleBar; + DWORD rgstate[CCHILDREN_TITLEBAR + 1]; + } TITLEBARINFO,*PTITLEBARINFO,*LPTITLEBARINFO; + + WINUSERAPI WINBOOL WINAPI GetTitleBarInfo(HWND hwnd,PTITLEBARINFO pti); + + typedef struct tagMENUBARINFO { + DWORD cbSize; + RECT rcBar; + HMENU hMenu; + HWND hwndMenu; + WINBOOL fBarFocused:1; + WINBOOL fFocused:1; + } MENUBARINFO,*PMENUBARINFO,*LPMENUBARINFO; + + WINUSERAPI WINBOOL WINAPI GetMenuBarInfo(HWND hwnd,LONG idObject,LONG idItem,PMENUBARINFO pmbi); + + typedef struct tagSCROLLBARINFO { + DWORD cbSize; + RECT rcScrollBar; + int dxyLineButton; + int xyThumbTop; + int xyThumbBottom; + int reserved; + DWORD rgstate[CCHILDREN_SCROLLBAR + 1]; + } SCROLLBARINFO,*PSCROLLBARINFO,*LPSCROLLBARINFO; + + WINUSERAPI WINBOOL WINAPI GetScrollBarInfo(HWND hwnd,LONG idObject,PSCROLLBARINFO psbi); + + typedef struct tagCOMBOBOXINFO { + DWORD cbSize; + RECT rcItem; + RECT rcButton; + DWORD stateButton; + HWND hwndCombo; + HWND hwndItem; + HWND hwndList; + } COMBOBOXINFO,*PCOMBOBOXINFO,*LPCOMBOBOXINFO; + + WINUSERAPI WINBOOL WINAPI GetComboBoxInfo(HWND hwndCombo,PCOMBOBOXINFO pcbi); + +#define GA_PARENT 1 +#define GA_ROOT 2 +#define GA_ROOTOWNER 3 + + WINUSERAPI HWND WINAPI GetAncestor(HWND hwnd,UINT gaFlags); + WINUSERAPI HWND WINAPI RealChildWindowFromPoint(HWND hwndParent,POINT ptParentClientCoords); + WINUSERAPI UINT WINAPI RealGetWindowClassA(HWND hwnd,LPSTR ptszClassName,UINT cchClassNameMax); + WINUSERAPI UINT WINAPI RealGetWindowClassW(HWND hwnd,LPWSTR ptszClassName,UINT cchClassNameMax); +#ifdef UNICODE +#define RealGetWindowClass RealGetWindowClassW +#else +#define RealGetWindowClass RealGetWindowClassA +#endif + + typedef struct tagALTTABINFO { + DWORD cbSize; + int cItems; + int cColumns; + int cRows; + int iColFocus; + int iRowFocus; + int cxItem; + int cyItem; + POINT ptStart; + } ALTTABINFO,*PALTTABINFO,*LPALTTABINFO; + +#ifdef UNICODE +#define GetAltTabInfo GetAltTabInfoW +#else +#define GetAltTabInfo GetAltTabInfoA +#endif + + WINUSERAPI WINBOOL WINAPI GetAltTabInfoA(HWND hwnd,int iItem,PALTTABINFO pati,LPSTR pszItemText,UINT cchItemText); + WINUSERAPI WINBOOL WINAPI GetAltTabInfoW(HWND hwnd,int iItem,PALTTABINFO pati,LPWSTR pszItemText,UINT cchItemText); + WINUSERAPI DWORD WINAPI GetListBoxInfo(HWND hwnd); +#endif + + WINUSERAPI WINBOOL WINAPI LockWorkStation(VOID); + WINUSERAPI WINBOOL WINAPI UserHandleGrantAccess(HANDLE hUserHandle,HANDLE hJob,WINBOOL bGrant); + + DECLARE_HANDLE(HRAWINPUT); + +#define GET_RAWINPUT_CODE_WPARAM(wParam) ((wParam) & 0xff) + +#define RIM_INPUT 0 +#define RIM_INPUTSINK 1 + + typedef struct tagRAWINPUTHEADER { + DWORD dwType; + DWORD dwSize; + HANDLE hDevice; + WPARAM wParam; + } RAWINPUTHEADER,*PRAWINPUTHEADER,*LPRAWINPUTHEADER; + +#define RIM_TYPEMOUSE 0 +#define RIM_TYPEKEYBOARD 1 +#define RIM_TYPEHID 2 + + typedef struct tagRAWMOUSE { + USHORT usFlags; + union { + ULONG ulButtons; + struct { + USHORT usButtonFlags; + USHORT usButtonData; + }; + }; + ULONG ulRawButtons; + LONG lLastX; + LONG lLastY; + ULONG ulExtraInformation; + } RAWMOUSE,*PRAWMOUSE,*LPRAWMOUSE; + +#define RI_MOUSE_LEFT_BUTTON_DOWN 0x0001 +#define RI_MOUSE_LEFT_BUTTON_UP 0x0002 +#define RI_MOUSE_RIGHT_BUTTON_DOWN 0x0004 +#define RI_MOUSE_RIGHT_BUTTON_UP 0x0008 +#define RI_MOUSE_MIDDLE_BUTTON_DOWN 0x0010 +#define RI_MOUSE_MIDDLE_BUTTON_UP 0x0020 + +#define RI_MOUSE_BUTTON_1_DOWN RI_MOUSE_LEFT_BUTTON_DOWN +#define RI_MOUSE_BUTTON_1_UP RI_MOUSE_LEFT_BUTTON_UP +#define RI_MOUSE_BUTTON_2_DOWN RI_MOUSE_RIGHT_BUTTON_DOWN +#define RI_MOUSE_BUTTON_2_UP RI_MOUSE_RIGHT_BUTTON_UP +#define RI_MOUSE_BUTTON_3_DOWN RI_MOUSE_MIDDLE_BUTTON_DOWN +#define RI_MOUSE_BUTTON_3_UP RI_MOUSE_MIDDLE_BUTTON_UP + +#define RI_MOUSE_BUTTON_4_DOWN 0x0040 +#define RI_MOUSE_BUTTON_4_UP 0x0080 +#define RI_MOUSE_BUTTON_5_DOWN 0x0100 +#define RI_MOUSE_BUTTON_5_UP 0x0200 + +#define RI_MOUSE_WHEEL 0x0400 + +#define MOUSE_MOVE_RELATIVE 0 +#define MOUSE_MOVE_ABSOLUTE 1 +#define MOUSE_VIRTUAL_DESKTOP 0x02 +#define MOUSE_ATTRIBUTES_CHANGED 0x04 + + typedef struct tagRAWKEYBOARD { + USHORT MakeCode; + USHORT Flags; + USHORT Reserved; + USHORT VKey; + UINT Message; + ULONG ExtraInformation; + } RAWKEYBOARD,*PRAWKEYBOARD,*LPRAWKEYBOARD; + +#define KEYBOARD_OVERRUN_MAKE_CODE 0xFF + +#define RI_KEY_MAKE 0 +#define RI_KEY_BREAK 1 +#define RI_KEY_E0 2 +#define RI_KEY_E1 4 +#define RI_KEY_TERMSRV_SET_LED 8 +#define RI_KEY_TERMSRV_SHADOW 0x10 + + typedef struct tagRAWHID { + DWORD dwSizeHid; + DWORD dwCount; + BYTE bRawData[1]; + } RAWHID,*PRAWHID,*LPRAWHID; + + typedef struct tagRAWINPUT { + RAWINPUTHEADER header; + union { + RAWMOUSE mouse; + RAWKEYBOARD keyboard; + RAWHID hid; + } data; + } RAWINPUT,*PRAWINPUT,*LPRAWINPUT; + +#ifdef _WIN64 +#define RAWINPUT_ALIGN(x) (((x) + sizeof(QWORD) - 1) & ~(sizeof(QWORD) - 1)) +#else +#define RAWINPUT_ALIGN(x) (((x) + sizeof(DWORD) - 1) & ~(sizeof(DWORD) - 1)) +#endif + +#define NEXTRAWINPUTBLOCK(ptr) ((PRAWINPUT)RAWINPUT_ALIGN((ULONG_PTR)((PBYTE)(ptr) + (ptr)->header.dwSize))) + +#define RID_INPUT 0x10000003 +#define RID_HEADER 0x10000005 + + WINUSERAPI UINT WINAPI GetRawInputData(HRAWINPUT hRawInput,UINT uiCommand,LPVOID pData,PUINT pcbSize,UINT cbSizeHeader); + +#define RIDI_PREPARSEDDATA 0x20000005 +#define RIDI_DEVICENAME 0x20000007 +#define RIDI_DEVICEINFO 0x2000000b + + typedef struct tagRID_DEVICE_INFO_MOUSE { + DWORD dwId; + DWORD dwNumberOfButtons; + DWORD dwSampleRate; + } RID_DEVICE_INFO_MOUSE,*PRID_DEVICE_INFO_MOUSE; + + typedef struct tagRID_DEVICE_INFO_KEYBOARD { + DWORD dwType; + DWORD dwSubType; + DWORD dwKeyboardMode; + DWORD dwNumberOfFunctionKeys; + DWORD dwNumberOfIndicators; + DWORD dwNumberOfKeysTotal; + } RID_DEVICE_INFO_KEYBOARD,*PRID_DEVICE_INFO_KEYBOARD; + + typedef struct tagRID_DEVICE_INFO_HID { + DWORD dwVendorId; + DWORD dwProductId; + DWORD dwVersionNumber; + USHORT usUsagePage; + USHORT usUsage; + } RID_DEVICE_INFO_HID,*PRID_DEVICE_INFO_HID; + + typedef struct tagRID_DEVICE_INFO { + DWORD cbSize; + DWORD dwType; + union { + RID_DEVICE_INFO_MOUSE mouse; + RID_DEVICE_INFO_KEYBOARD keyboard; + RID_DEVICE_INFO_HID hid; + }; + } RID_DEVICE_INFO,*PRID_DEVICE_INFO,*LPRID_DEVICE_INFO; + +#ifdef UNICODE +#define GetRawInputDeviceInfo GetRawInputDeviceInfoW +#else +#define GetRawInputDeviceInfo GetRawInputDeviceInfoA +#endif + + WINUSERAPI UINT WINAPI GetRawInputDeviceInfoA(HANDLE hDevice,UINT uiCommand,LPVOID pData,PUINT pcbSize); + WINUSERAPI UINT WINAPI GetRawInputDeviceInfoW(HANDLE hDevice,UINT uiCommand,LPVOID pData,PUINT pcbSize); + WINUSERAPI UINT WINAPI GetRawInputBuffer(PRAWINPUT pData,PUINT pcbSize,UINT cbSizeHeader); + + typedef struct tagRAWINPUTDEVICE { + USHORT usUsagePage; + USHORT usUsage; + DWORD dwFlags; + HWND hwndTarget; + } RAWINPUTDEVICE,*PRAWINPUTDEVICE,*LPRAWINPUTDEVICE; + + typedef CONST RAWINPUTDEVICE *PCRAWINPUTDEVICE; + +#define RIDEV_REMOVE 0x00000001 +#define RIDEV_EXCLUDE 0x00000010 +#define RIDEV_PAGEONLY 0x00000020 +#define RIDEV_NOLEGACY 0x00000030 +#define RIDEV_INPUTSINK 0x00000100 +#define RIDEV_CAPTUREMOUSE 0x00000200 +#define RIDEV_NOHOTKEYS 0x00000200 +#define RIDEV_APPKEYS 0x00000400 +#define RIDEV_EXMODEMASK 0x000000F0 +#define RIDEV_EXMODE(mode) ((mode) & RIDEV_EXMODEMASK) + + WINUSERAPI WINBOOL WINAPI RegisterRawInputDevices(PCRAWINPUTDEVICE pRawInputDevices,UINT uiNumDevices,UINT cbSize); + WINUSERAPI UINT WINAPI GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices,PUINT puiNumDevices,UINT cbSize); + + typedef struct tagRAWINPUTDEVICELIST { + HANDLE hDevice; + DWORD dwType; + } RAWINPUTDEVICELIST,*PRAWINPUTDEVICELIST; + + WINUSERAPI UINT WINAPI GetRawInputDeviceList(PRAWINPUTDEVICELIST pRawInputDeviceList,PUINT puiNumDevices,UINT cbSize); + WINUSERAPI LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput,INT nInput,UINT cbSizeHeader); + +#endif /* NOUSER */ + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/include/winapi/winver.h b/05/tcc-final-old/win32/include/winapi/winver.h new file mode 100644 index 0000000..5c0f036 --- /dev/null +++ b/05/tcc-final-old/win32/include/winapi/winver.h @@ -0,0 +1,160 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef VER_H +#define VER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define VS_FILE_INFO RT_VERSION +#define VS_VERSION_INFO 1 +#define VS_USER_DEFINED 100 + +#define VS_FFI_SIGNATURE 0xFEEF04BDL +#define VS_FFI_STRUCVERSION 0x00010000L +#define VS_FFI_FILEFLAGSMASK 0x0000003FL + +#define VS_FF_DEBUG 0x00000001L +#define VS_FF_PRERELEASE 0x00000002L +#define VS_FF_PATCHED 0x00000004L +#define VS_FF_PRIVATEBUILD 0x00000008L +#define VS_FF_INFOINFERRED 0x00000010L +#define VS_FF_SPECIALBUILD 0x00000020L + +#define VOS_UNKNOWN 0x00000000L +#define VOS_DOS 0x00010000L +#define VOS_OS216 0x00020000L +#define VOS_OS232 0x00030000L +#define VOS_NT 0x00040000L +#define VOS_WINCE 0x00050000L + +#define VOS__BASE 0x00000000L +#define VOS__WINDOWS16 0x00000001L +#define VOS__PM16 0x00000002L +#define VOS__PM32 0x00000003L +#define VOS__WINDOWS32 0x00000004L + +#define VOS_DOS_WINDOWS16 0x00010001L +#define VOS_DOS_WINDOWS32 0x00010004L +#define VOS_OS216_PM16 0x00020002L +#define VOS_OS232_PM32 0x00030003L +#define VOS_NT_WINDOWS32 0x00040004L + +#define VFT_UNKNOWN 0x00000000L +#define VFT_APP 0x00000001L +#define VFT_DLL 0x00000002L +#define VFT_DRV 0x00000003L +#define VFT_FONT 0x00000004L +#define VFT_VXD 0x00000005L +#define VFT_STATIC_LIB 0x00000007L + +#define VFT2_UNKNOWN 0x00000000L +#define VFT2_DRV_PRINTER 0x00000001L +#define VFT2_DRV_KEYBOARD 0x00000002L +#define VFT2_DRV_LANGUAGE 0x00000003L +#define VFT2_DRV_DISPLAY 0x00000004L +#define VFT2_DRV_MOUSE 0x00000005L +#define VFT2_DRV_NETWORK 0x00000006L +#define VFT2_DRV_SYSTEM 0x00000007L +#define VFT2_DRV_INSTALLABLE 0x00000008L +#define VFT2_DRV_SOUND 0x00000009L +#define VFT2_DRV_COMM 0x0000000AL +#define VFT2_DRV_INPUTMETHOD 0x0000000BL +#define VFT2_DRV_VERSIONED_PRINTER 0x0000000CL + +#define VFT2_FONT_RASTER 0x00000001L +#define VFT2_FONT_VECTOR 0x00000002L +#define VFT2_FONT_TRUETYPE 0x00000003L + +#define VFFF_ISSHAREDFILE 0x0001 + +#define VFF_CURNEDEST 0x0001 +#define VFF_FILEINUSE 0x0002 +#define VFF_BUFFTOOSMALL 0x0004 + +#define VIFF_FORCEINSTALL 0x0001 +#define VIFF_DONTDELETEOLD 0x0002 + +#define VIF_TEMPFILE 0x00000001L +#define VIF_MISMATCH 0x00000002L +#define VIF_SRCOLD 0x00000004L + +#define VIF_DIFFLANG 0x00000008L +#define VIF_DIFFCODEPG 0x00000010L +#define VIF_DIFFTYPE 0x00000020L + +#define VIF_WRITEPROT 0x00000040L +#define VIF_FILEINUSE 0x00000080L +#define VIF_OUTOFSPACE 0x00000100L +#define VIF_ACCESSVIOLATION 0x00000200L +#define VIF_SHARINGVIOLATION 0x00000400L +#define VIF_CANNOTCREATE 0x00000800L +#define VIF_CANNOTDELETE 0x00001000L +#define VIF_CANNOTRENAME 0x00002000L +#define VIF_CANNOTDELETECUR 0x00004000L +#define VIF_OUTOFMEMORY 0x00008000L + +#define VIF_CANNOTREADSRC 0x00010000L +#define VIF_CANNOTREADDST 0x00020000L + +#define VIF_BUFFTOOSMALL 0x00040000L +#define VIF_CANNOTLOADLZ32 0x00080000L +#define VIF_CANNOTLOADCABINET 0x00100000L + +#ifndef RC_INVOKED + + typedef struct tagVS_FIXEDFILEINFO + { + DWORD dwSignature; + DWORD dwStrucVersion; + DWORD dwFileVersionMS; + DWORD dwFileVersionLS; + DWORD dwProductVersionMS; + DWORD dwProductVersionLS; + DWORD dwFileFlagsMask; + DWORD dwFileFlags; + DWORD dwFileOS; + DWORD dwFileType; + DWORD dwFileSubtype; + DWORD dwFileDateMS; + DWORD dwFileDateLS; + } VS_FIXEDFILEINFO; + +#ifdef UNICODE +#define VerFindFile VerFindFileW +#define VerInstallFile VerInstallFileW +#define GetFileVersionInfoSize GetFileVersionInfoSizeW +#define GetFileVersionInfo GetFileVersionInfoW +#define VerLanguageName VerLanguageNameW +#define VerQueryValue VerQueryValueW +#else +#define VerFindFile VerFindFileA +#define VerInstallFile VerInstallFileA +#define GetFileVersionInfoSize GetFileVersionInfoSizeA +#define GetFileVersionInfo GetFileVersionInfoA +#define VerLanguageName VerLanguageNameA +#define VerQueryValue VerQueryValueA +#endif + + DWORD WINAPI VerFindFileA(DWORD uFlags,LPSTR szFileName,LPSTR szWinDir,LPSTR szAppDir,LPSTR szCurDir,PUINT lpuCurDirLen,LPSTR szDestDir,PUINT lpuDestDirLen); + DWORD WINAPI VerFindFileW(DWORD uFlags,LPWSTR szFileName,LPWSTR szWinDir,LPWSTR szAppDir,LPWSTR szCurDir,PUINT lpuCurDirLen,LPWSTR szDestDir,PUINT lpuDestDirLen); + DWORD WINAPI VerInstallFileA(DWORD uFlags,LPSTR szSrcFileName,LPSTR szDestFileName,LPSTR szSrcDir,LPSTR szDestDir,LPSTR szCurDir,LPSTR szTmpFile,PUINT lpuTmpFileLen); + DWORD WINAPI VerInstallFileW(DWORD uFlags,LPWSTR szSrcFileName,LPWSTR szDestFileName,LPWSTR szSrcDir,LPWSTR szDestDir,LPWSTR szCurDir,LPWSTR szTmpFile,PUINT lpuTmpFileLen); + DWORD WINAPI GetFileVersionInfoSizeA(LPCSTR lptstrFilename,LPDWORD lpdwHandle); + DWORD WINAPI GetFileVersionInfoSizeW(LPCWSTR lptstrFilename,LPDWORD lpdwHandle); + WINBOOL WINAPI GetFileVersionInfoA(LPCSTR lptstrFilename,DWORD dwHandle,DWORD dwLen,LPVOID lpData); + WINBOOL WINAPI GetFileVersionInfoW(LPCWSTR lptstrFilename,DWORD dwHandle,DWORD dwLen,LPVOID lpData); + DWORD WINAPI VerLanguageNameA(DWORD wLang,LPSTR szLang,DWORD nSize); + DWORD WINAPI VerLanguageNameW(DWORD wLang,LPWSTR szLang,DWORD nSize); + WINBOOL WINAPI VerQueryValueA(const LPVOID pBlock,LPSTR lpSubBlock,LPVOID *lplpBuffer,PUINT puLen); + WINBOOL WINAPI VerQueryValueW(const LPVOID pBlock,LPWSTR lpSubBlock,LPVOID *lplpBuffer,PUINT puLen); +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/05/tcc-final-old/win32/lib/chkstk.S b/05/tcc-final-old/win32/lib/chkstk.S new file mode 100644 index 0000000..ec5c07f --- /dev/null +++ b/05/tcc-final-old/win32/lib/chkstk.S @@ -0,0 +1,191 @@ +/* ---------------------------------------------- */ +/* chkstk86.s */ + +/* ---------------------------------------------- */ +#ifndef __x86_64__ +/* ---------------------------------------------- */ + +.globl __chkstk + +__chkstk: + xchg (%esp),%ebp /* store ebp, get ret.addr */ + push %ebp /* push ret.addr */ + lea 4(%esp),%ebp /* setup frame ptr */ + push %ecx /* save ecx */ + mov %ebp,%ecx +P0: + sub $4096,%ecx + test %eax,(%ecx) + sub $4096,%eax + cmp $4096,%eax + jge P0 + sub %eax,%ecx + test %eax,(%ecx) + + mov %esp,%eax + mov %ecx,%esp + mov (%eax),%ecx /* restore ecx */ + jmp *4(%eax) + +/* ---------------------------------------------- */ +#else +/* ---------------------------------------------- */ + +.globl __chkstk + +__chkstk: + xchg (%rsp),%rbp /* store ebp, get ret.addr */ + push %rbp /* push ret.addr */ + lea 8(%rsp),%rbp /* setup frame ptr */ + push %rcx /* save ecx */ + mov %rbp,%rcx + movslq %eax,%rax +P0: + sub $4096,%rcx + test %rax,(%rcx) + sub $4096,%rax + cmp $4096,%rax + jge P0 + sub %rax,%rcx + test %rax,(%rcx) + + mov %rsp,%rax + mov %rcx,%rsp + mov (%rax),%rcx /* restore ecx */ + jmp *8(%rax) + +/* ---------------------------------------------- */ +/* setjmp/longjmp support */ + +.globl tinyc_getbp +tinyc_getbp: + mov %rbp,%rax + ret + +/* ---------------------------------------------- */ +#endif +/* ---------------------------------------------- */ + + +/* ---------------------------------------------- */ +#ifndef __x86_64__ +/* ---------------------------------------------- */ + +/* + int _except_handler3( + PEXCEPTION_RECORD exception_record, + PEXCEPTION_REGISTRATION registration, + PCONTEXT context, + PEXCEPTION_REGISTRATION dispatcher + ); + + int __cdecl _XcptFilter( + unsigned long xcptnum, + PEXCEPTION_POINTERS pxcptinfoptrs + ); + + struct _sehrec { + void *esp; // 0 + void *exception_pointers; // 1 + void *prev; // 2 + void *handler; // 3 + void *scopetable; // 4 + int trylevel; // 5 + void *ebp // 6 + }; + + // this is what the assembler code below means: + __try + { + // ... + } + __except (_XcptFilter(GetExceptionCode(), GetExceptionInformation())) + { + exit(GetExceptionCode()); + } +*/ + +.globl _exception_info +_exception_info: + mov 1*4-24(%ebp),%eax + ret + +.globl _exception_code +_exception_code: + call _exception_info + mov (%eax),%eax + mov (%eax),%eax + ret + +seh_filter: + call _exception_info + push %eax + call _exception_code + push %eax + call _XcptFilter + add $ 8,%esp + ret + +seh_except: + mov 0*4-24(%ebp),%esp + call _exception_code + push %eax + call _exit + +// msvcrt wants scopetables aligned and in read-only segment (using .text) +.align 4 +seh_scopetable: + .long -1 + .long seh_filter + .long seh_except + +seh_handler: + jmp _except_handler3 + +.globl ___try__ +___try__: +.globl __try__ +__try__: + push %ebp + mov 8(%esp),%ebp + +// void *esp; + lea 12(%esp),%eax + mov %eax,0*4(%ebp) + +// void *exception_pointers; + xor %eax,%eax + mov %eax,1*4(%ebp) + +// void *prev; + mov %fs:0,%eax + mov %eax,2*4(%ebp) + +// void *handler; + mov $ seh_handler,%eax + mov %eax,3*4(%ebp) + +// void *scopetable; + mov $ seh_scopetable,%eax + mov %eax,4*4(%ebp) + +// int trylevel; + xor %eax,%eax + mov %eax,5*4(%ebp) + +// register new SEH + lea 2*4(%ebp),%eax + mov %eax,%fs:0 + + pop %ebp + ret + +/* ---------------------------------------------- */ +#else +/* ---------------------------------------------- */ + +/* SEH on x86-64 not implemented */ + +/* ---------------------------------------------- */ +#endif +/* ---------------------------------------------- */ diff --git a/05/tcc-final-old/win32/lib/crt1.c b/05/tcc-final-old/win32/lib/crt1.c new file mode 100644 index 0000000..0e04fc0 --- /dev/null +++ b/05/tcc-final-old/win32/lib/crt1.c @@ -0,0 +1,79 @@ +// ============================================= +// crt1.c + +// _UNICODE for tchar.h, UNICODE for API +#include + +#include +#include + +#define _UNKNOWN_APP 0 +#define _CONSOLE_APP 1 +#define _GUI_APP 2 + +#define _MCW_PC 0x00030000 // Precision Control +#define _PC_24 0x00020000 // 24 bits +#define _PC_53 0x00010000 // 53 bits +#define _PC_64 0x00000000 // 64 bits + +#ifdef _UNICODE +#define __tgetmainargs __wgetmainargs +#define _tstart _wstart +#define _tmain wmain +#define _runtmain _runwmain +#else +#define __tgetmainargs __getmainargs +#define _tstart _start +#define _tmain main +#define _runtmain _runmain +#endif + +typedef struct { int newmode; } _startupinfo; +int __cdecl __tgetmainargs(int *pargc, _TCHAR ***pargv, _TCHAR ***penv, int globb, _startupinfo*); +void __cdecl __set_app_type(int apptype); +unsigned int __cdecl _controlfp(unsigned int new_value, unsigned int mask); +extern int _tmain(int argc, _TCHAR * argv[], _TCHAR * env[]); + +/* Allow command-line globbing with "int _dowildcard = 1;" in the user source */ +int _dowildcard; + +void _tstart(void) +{ + __TRY__ + _startupinfo start_info = {0}; + + // Sets the current application type + __set_app_type(_CONSOLE_APP); + + // Set default FP precision to 53 bits (8-byte double) + // _MCW_PC (Precision control) is not supported on ARM +#if defined __i386__ || defined __x86_64__ + _controlfp(_PC_53, _MCW_PC); +#endif + + __tgetmainargs( &__argc, &__targv, &_tenviron, _dowildcard, &start_info); + exit(_tmain(__argc, __targv, _tenviron)); +} + +int _runtmain(int argc, /* as tcc passed in */ char **argv) +{ +#ifdef UNICODE + _startupinfo start_info = {0}; + + __tgetmainargs(&__argc, &__targv, &_tenviron, _dowildcard, &start_info); + /* may be wrong when tcc has received wildcards (*.c) */ + if (argc < __argc) { + __targv += __argc - argc; + __argc = argc; + } +#else + __argc = argc; + __targv = argv; +#endif +#if defined __i386__ || defined __x86_64__ + _controlfp(_PC_53, _MCW_PC); +#endif + return _tmain(__argc, __targv, _tenviron); +} + +// ============================================= diff --git a/05/tcc-final-old/win32/lib/crt1w.c b/05/tcc-final-old/win32/lib/crt1w.c new file mode 100644 index 0000000..2b8bbc8 --- /dev/null +++ b/05/tcc-final-old/win32/lib/crt1w.c @@ -0,0 +1,3 @@ +#define _UNICODE 1 +#define UNICODE 1 +#include "crt1.c" diff --git a/05/tcc-final-old/win32/lib/dllcrt1.c b/05/tcc-final-old/win32/lib/dllcrt1.c new file mode 100644 index 0000000..ba1dbd0 --- /dev/null +++ b/05/tcc-final-old/win32/lib/dllcrt1.c @@ -0,0 +1,13 @@ +//+--------------------------------------------------------------------------- + +#include + +BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved); + +BOOL WINAPI _dllstart(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) +{ + BOOL bRet; + bRet = DllMain (hDll, dwReason, lpReserved); + return bRet; +} + diff --git a/05/tcc-final-old/win32/lib/dllmain.c b/05/tcc-final-old/win32/lib/dllmain.c new file mode 100644 index 0000000..2c25b98 --- /dev/null +++ b/05/tcc-final-old/win32/lib/dllmain.c @@ -0,0 +1,9 @@ +//+--------------------------------------------------------------------------- + +#include + +BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) +{ + return TRUE; +} + diff --git a/05/tcc-final-old/win32/lib/gdi32.def b/05/tcc-final-old/win32/lib/gdi32.def new file mode 100644 index 0000000..02766da --- /dev/null +++ b/05/tcc-final-old/win32/lib/gdi32.def @@ -0,0 +1,337 @@ +LIBRARY gdi32.dll + +EXPORTS +AbortDoc +AbortPath +AddFontResourceA +AddFontResourceW +AngleArc +AnimatePalette +Arc +ArcTo +BeginPath +BitBlt +ByeByeGDI +CancelDC +CheckColorsInGamut +ChoosePixelFormat +Chord +CloseEnhMetaFile +CloseFigure +CloseMetaFile +ColorCorrectPalette +ColorMatchToTarget +CombineRgn +CombineTransform +CopyEnhMetaFileA +CopyEnhMetaFileW +CopyMetaFileA +CopyMetaFileW +CreateBitmap +CreateBitmapIndirect +CreateBrushIndirect +CreateColorSpaceA +CreateColorSpaceW +CreateCompatibleBitmap +CreateCompatibleDC +CreateDCA +CreateDCW +CreateDIBPatternBrush +CreateDIBPatternBrushPt +CreateDIBSection +CreateDIBitmap +CreateDiscardableBitmap +CreateEllipticRgn +CreateEllipticRgnIndirect +CreateEnhMetaFileA +CreateEnhMetaFileW +CreateFontA +CreateFontIndirectA +CreateFontIndirectW +CreateFontW +CreateHalftonePalette +CreateHatchBrush +CreateICA +CreateICW +CreateMetaFileA +CreateMetaFileW +CreatePalette +CreatePatternBrush +CreatePen +CreatePenIndirect +CreatePolyPolygonRgn +CreatePolygonRgn +CreateRectRgn +CreateRectRgnIndirect +CreateRoundRectRgn +CreateScalableFontResourceA +CreateScalableFontResourceW +CreateSolidBrush +DPtoLP +DeleteColorSpace +DeleteDC +DeleteEnhMetaFile +DeleteMetaFile +DeleteObject +DescribePixelFormat +DeviceCapabilitiesEx +DeviceCapabilitiesExA +DeviceCapabilitiesExW +DrawEscape +Ellipse +EnableEUDC +EndDoc +EndPage +EndPath +EnumEnhMetaFile +EnumFontFamiliesA +EnumFontFamiliesExA +EnumFontFamiliesExW +EnumFontFamiliesW +EnumFontsA +EnumFontsW +EnumICMProfilesA +EnumICMProfilesW +EnumMetaFile +EnumObjects +EqualRgn +Escape +ExcludeClipRect +ExtCreatePen +ExtCreateRegion +ExtEscape +ExtFloodFill +ExtSelectClipRgn +ExtTextOutA +ExtTextOutW +FillPath +FillRgn +FixBrushOrgEx +FlattenPath +FloodFill +FrameRgn +GdiComment +GdiFlush +GdiGetBatchLimit +GdiPlayDCScript +GdiPlayJournal +GdiPlayScript +GdiSetBatchLimit +GetArcDirection +GetAspectRatioFilterEx +GetBitmapBits +GetBitmapDimensionEx +GetBkColor +GetBkMode +GetBoundsRect +GetBrushOrgEx +GetCharABCWidthsA +GetCharABCWidthsFloatA +GetCharABCWidthsFloatW +GetCharABCWidthsW +GetCharWidth32A +GetCharWidth32W +GetCharWidthA +GetCharWidthFloatA +GetCharWidthFloatW +GetCharWidthW +GetCharacterPlacementA +GetCharacterPlacementW +GetClipBox +GetClipRgn +GetColorAdjustment +GetColorSpace +GetCurrentObject +GetCurrentPositionEx +GetDCOrgEx +GetDIBColorTable +GetDIBits +GetDeviceCaps +GetDeviceGammaRamp +GetEnhMetaFileA +GetEnhMetaFileBits +GetEnhMetaFileDescriptionA +GetEnhMetaFileDescriptionW +GetEnhMetaFileHeader +GetEnhMetaFilePaletteEntries +GetEnhMetaFileW +GetFontData +GetFontLanguageInfo +GetFontResourceInfo +GetGlyphOutline +GetGlyphOutlineA +GetGlyphOutlineW +GetGraphicsMode +GetICMProfileA +GetICMProfileW +GetKerningPairs +GetKerningPairsA +GetKerningPairsW +GetLayout +GetLogColorSpaceA +GetLogColorSpaceW +GetMapMode +GetMetaFileA +GetMetaFileBitsEx +GetMetaFileW +GetMetaRgn +GetMiterLimit +GetNearestColor +GetNearestPaletteIndex +GetObjectA +GetObjectType +GetObjectW +GetOutlineTextMetricsA +GetOutlineTextMetricsW +GetPaletteEntries +GetPath +GetPixel +GetPixelFormat +GetPolyFillMode +GetROP2 +GetRandomRgn +GetRasterizerCaps +GetRegionData +GetRgnBox +GetStockObject +GetStretchBltMode +GetSystemPaletteEntries +GetSystemPaletteUse +GetTextAlign +GetTextCharacterExtra +GetTextCharset +GetTextCharsetInfo +GetTextColor +GetTextExtentExPointA +GetTextExtentExPointW +GetTextExtentPoint32A +GetTextExtentPoint32W +GetTextExtentPointA +GetTextExtentPointW +GetTextFaceA +GetTextFaceW +GetTextMetricsA +GetTextMetricsW +GetViewportExtEx +GetViewportOrgEx +GetWinMetaFileBits +GetWindowExtEx +GetWindowOrgEx +GetWorldTransform +IntersectClipRect +InvertRgn +LPtoDP +LineDDA +LineTo +MaskBlt +ModifyWorldTransform +MoveToEx +OffsetClipRgn +OffsetRgn +OffsetViewportOrgEx +OffsetWindowOrgEx +PaintRgn +PatBlt +PathToRegion +Pie +PlayEnhMetaFile +PlayEnhMetaFileRecord +PlayMetaFile +PlayMetaFileRecord +PlgBlt +PolyBezier +PolyBezierTo +PolyDraw +PolyPolygon +PolyPolyline +PolyTextOutA +PolyTextOutW +Polygon +Polyline +PolylineTo +PtInRegion +PtVisible +RealizePalette +RectInRegion +RectVisible +Rectangle +RemoveFontResourceA +RemoveFontResourceW +ResetDCA +ResetDCW +ResizePalette +RestoreDC +RoundRect +SaveDC +ScaleViewportExtEx +ScaleWindowExtEx +SelectClipPath +SelectClipRgn +SelectObject +SelectPalette +SetAbortProc +SetArcDirection +SetBitmapBits +SetBitmapDimensionEx +SetBkColor +SetBkMode +SetBoundsRect +SetBrushOrgEx +SetColorAdjustment +SetColorSpace +SetDIBColorTable +SetDIBits +SetDIBitsToDevice +SetDeviceGammaRamp +SetEnhMetaFileBits +SetFontEnumeration +SetGraphicsMode +SetICMMode +SetICMProfileA +SetICMProfileW +SetLayout +SetMagicColors +SetMapMode +SetMapperFlags +SetMetaFileBitsEx +SetMetaRgn +SetMiterLimit +SetObjectOwner +SetPaletteEntries +SetPixel +SetPixelFormat +SetPixelV +SetPolyFillMode +SetROP2 +SetRectRgn +SetStretchBltMode +SetSystemPaletteUse +SetTextAlign +SetTextCharacterExtra +SetTextColor +SetTextJustification +SetViewportExtEx +SetViewportOrgEx +SetWinMetaFileBits +SetWindowExtEx +SetWindowOrgEx +SetWorldTransform +StartDocA +StartDocW +StartPage +StretchBlt +StretchDIBits +StrokeAndFillPath +StrokePath +SwapBuffers +TextOutA +TextOutW +TranslateCharsetInfo +UnrealizeObject +UpdateColors +UpdateICMRegKeyA +UpdateICMRegKeyW +WidenPath +gdiPlaySpoolStream +pfnRealizePalette +pfnSelectPalette diff --git a/05/tcc-final-old/win32/lib/kernel32.def b/05/tcc-final-old/win32/lib/kernel32.def new file mode 100644 index 0000000..f03e17b --- /dev/null +++ b/05/tcc-final-old/win32/lib/kernel32.def @@ -0,0 +1,770 @@ +LIBRARY kernel32.dll + +EXPORTS +AddAtomA +AddAtomW +AllocConsole +AllocLSCallback +AllocSLCallback +AreFileApisANSI +BackupRead +BackupSeek +BackupWrite +Beep +BeginUpdateResourceA +BeginUpdateResourceW +BuildCommDCBA +BuildCommDCBAndTimeoutsA +BuildCommDCBAndTimeoutsW +BuildCommDCBW +CallNamedPipeA +CallNamedPipeW +Callback12 +Callback16 +Callback20 +Callback24 +Callback28 +Callback32 +Callback36 +Callback4 +Callback40 +Callback44 +Callback48 +Callback52 +Callback56 +Callback60 +Callback64 +Callback8 +CancelDeviceWakeupRequest +CancelIo +CancelWaitableTimer +ClearCommBreak +ClearCommError +CloseHandle +CloseProfileUserMapping +CloseSystemHandle +CommConfigDialogA +CommConfigDialogW +CompareFileTime +CompareStringA +CompareStringW +ConnectNamedPipe +ContinueDebugEvent +ConvertDefaultLocale +ConvertThreadToFiber +ConvertToGlobalHandle +CopyFileA +CopyFileExA +CopyFileExW +CopyFileW +CreateConsoleScreenBuffer +CreateDirectoryA +CreateDirectoryExA +CreateDirectoryExW +CreateDirectoryW +CreateEventA +CreateEventW +CreateFiber +CreateFileA +CreateFileMappingA +CreateFileMappingW +CreateFileW +CreateIoCompletionPort +CreateKernelThread +CreateMailslotA +CreateMailslotW +CreateMutexA +CreateMutexW +CreateNamedPipeA +CreateNamedPipeW +CreatePipe +CreateProcessA +CreateProcessW +CreateRemoteThread +CreateSemaphoreA +CreateSemaphoreW +CreateSocketHandle +CreateTapePartition +CreateThread +CreateToolhelp32Snapshot +CreateWaitableTimerA +CreateWaitableTimerW +DebugActiveProcess +DebugBreak +DefineDosDeviceA +DefineDosDeviceW +DeleteAtom +DeleteCriticalSection +DeleteFiber +DeleteFileA +DeleteFileW +DeviceIoControl +DisableThreadLibraryCalls +DisconnectNamedPipe +DosDateTimeToFileTime +DuplicateHandle +EndUpdateResourceA +EndUpdateResourceW +EnterCriticalSection +EnumCalendarInfoA +EnumCalendarInfoExA +EnumCalendarInfoExW +EnumCalendarInfoW +EnumDateFormatsA +EnumDateFormatsExA +EnumDateFormatsExW +EnumDateFormatsW +EnumLanguageGroupLocalesA +EnumLanguageGroupLocalesW +EnumResourceLanguagesA +EnumResourceLanguagesW +EnumResourceNamesA +EnumResourceNamesW +EnumResourceTypesA +EnumResourceTypesW +EnumSystemCodePagesA +EnumSystemCodePagesW +EnumSystemGeoID +EnumSystemLanguageGroupsA +EnumSystemLanguageGroupsW +EnumSystemLocalesA +EnumSystemLocalesW +EnumTimeFormatsA +EnumTimeFormatsW +EnumUILanguagesA +EnumUILanguagesW +EraseTape +EscapeCommFunction +ExitProcess +ExitThread +ExpandEnvironmentStringsA +ExpandEnvironmentStringsW +FT_Exit0 +FT_Exit12 +FT_Exit16 +FT_Exit20 +FT_Exit24 +FT_Exit28 +FT_Exit32 +FT_Exit36 +FT_Exit4 +FT_Exit40 +FT_Exit44 +FT_Exit48 +FT_Exit52 +FT_Exit56 +FT_Exit8 +FT_Prolog +FT_Thunk +FatalAppExitA +FatalAppExitW +FatalExit +FileTimeToDosDateTime +FileTimeToLocalFileTime +FileTimeToSystemTime +FillConsoleOutputAttribute +FillConsoleOutputCharacterA +FillConsoleOutputCharacterW +FindAtomA +FindAtomW +FindClose +FindCloseChangeNotification +FindFirstChangeNotificationA +FindFirstChangeNotificationW +FindFirstFileA +FindFirstFileExA +FindFirstFileExW +FindFirstFileW +FindNextChangeNotification +FindNextFileA +FindNextFileW +FindResourceA +FindResourceExA +FindResourceExW +FindResourceW +FlushConsoleInputBuffer +FlushFileBuffers +FlushInstructionCache +FlushViewOfFile +FoldStringA +FoldStringW +FormatMessageA +FormatMessageW +FreeConsole +FreeEnvironmentStringsA +FreeEnvironmentStringsW +FreeLSCallback +FreeLibrary +FreeLibraryAndExitThread +FreeResource +FreeSLCallback +GenerateConsoleCtrlEvent +GetACP +GetAtomNameA +GetAtomNameW +GetBinaryType +GetBinaryTypeA +GetBinaryTypeW +GetCPInfo +GetCPInfoExA +GetCPInfoExW +GetCalendarInfoA +GetCalendarInfoW +GetCommConfig +GetCommMask +GetCommModemStatus +GetCommProperties +GetCommState +GetCommTimeouts +GetCommandLineA +GetCommandLineW +GetCompressedFileSizeA +GetCompressedFileSizeW +GetComputerNameA +GetComputerNameW +GetConsoleCP +GetConsoleCursorInfo +GetConsoleMode +GetConsoleOutputCP +GetConsoleScreenBufferInfo +GetConsoleTitleA +GetConsoleTitleW +GetCurrencyFormatA +GetCurrencyFormatW +GetCurrentDirectoryA +GetCurrentDirectoryW +GetCurrentProcess +GetCurrentProcessId +GetCurrentThread +GetCurrentThreadId +GetDateFormatA +GetDateFormatW +GetDaylightFlag +GetDefaultCommConfigA +GetDefaultCommConfigW +GetDevicePowerState +GetDiskFreeSpaceA +GetDiskFreeSpaceExA +GetDiskFreeSpaceExW +GetDiskFreeSpaceW +GetDriveTypeA +GetDriveTypeW +GetEnvironmentStrings +GetEnvironmentStringsA +GetEnvironmentStringsW +GetEnvironmentVariableA +GetEnvironmentVariableW +GetErrorMode +GetExitCodeProcess +GetExitCodeThread +GetFileAttributesA +GetFileAttributesExA +GetFileAttributesExW +GetFileAttributesW +GetFileInformationByHandle +GetFileSize +GetFileTime +GetFileType +GetFullPathNameA +GetFullPathNameW +GetGeoInfoA +GetGeoInfoW +GetHandleContext +GetHandleInformation +GetLSCallbackTarget +GetLSCallbackTemplate +GetLargestConsoleWindowSize +GetLastError +GetLocalTime +GetLocaleInfoA +GetLocaleInfoW +GetLogicalDriveStringsA +GetLogicalDriveStringsW +GetLogicalDrives +GetLongPathNameA +GetLongPathNameW +GetMailslotInfo +GetModuleFileNameA +GetModuleFileNameW +GetModuleHandleA +GetModuleHandleW +GetModuleHandleExA +GetModuleHandleExW +GetNamedPipeHandleStateA +GetNamedPipeHandleStateW +GetNamedPipeInfo +GetNumberFormatA +GetNumberFormatW +GetNumberOfConsoleInputEvents +GetNumberOfConsoleMouseButtons +GetOEMCP +GetOverlappedResult +GetPriorityClass +GetPrivateProfileIntA +GetPrivateProfileIntW +GetPrivateProfileSectionA +GetPrivateProfileSectionNamesA +GetPrivateProfileSectionNamesW +GetPrivateProfileSectionW +GetPrivateProfileStringA +GetPrivateProfileStringW +GetPrivateProfileStructA +GetPrivateProfileStructW +GetProcAddress +GetProcessAffinityMask +GetProcessFlags +GetProcessHeap +GetProcessHeaps +GetProcessPriorityBoost +GetProcessShutdownParameters +GetProcessTimes +GetProcessVersion +GetProcessWorkingSetSize +GetProductName +GetProfileIntA +GetProfileIntW +GetProfileSectionA +GetProfileSectionW +GetProfileStringA +GetProfileStringW +GetQueuedCompletionStatus +GetSLCallbackTarget +GetSLCallbackTemplate +GetShortPathNameA +GetShortPathNameW +GetStartupInfoA +GetStartupInfoW +GetStdHandle +GetStringTypeA +GetStringTypeExA +GetStringTypeExW +GetStringTypeW +GetSystemDefaultLCID +GetSystemDefaultLangID +GetSystemDefaultUILanguage +GetSystemDirectoryA +GetSystemDirectoryW +GetSystemInfo +GetSystemPowerStatus +GetSystemTime +GetSystemTimeAdjustment +GetSystemTimeAsFileTime +GetTapeParameters +GetTapePosition +GetTapeStatus +GetTempFileNameA +GetTempFileNameW +GetTempPathA +GetTempPathW +GetThreadContext +GetThreadLocale +GetThreadPriority +GetThreadPriorityBoost +GetThreadSelectorEntry +GetThreadTimes +GetTickCount +GetTimeFormatA +GetTimeFormatW +GetTimeZoneInformation +GetUserDefaultLCID +GetUserDefaultLangID +GetUserDefaultUILanguage +GetUserGeoID +GetVersion +GetVersionExA +GetVersionExW +GetVolumeInformationA +GetVolumeInformationW +GetWindowsDirectoryA +GetWindowsDirectoryW +GetWriteWatch +GlobalAddAtomA +GlobalAddAtomW +GlobalAlloc +GlobalCompact +GlobalDeleteAtom +GlobalFindAtomA +GlobalFindAtomW +GlobalFix +GlobalFlags +GlobalFree +GlobalGetAtomNameA +GlobalGetAtomNameW +GlobalHandle +GlobalLock +GlobalMemoryStatus +GlobalReAlloc +GlobalSize +GlobalUnWire +GlobalUnfix +GlobalUnlock +GlobalWire +Heap32First +Heap32ListFirst +Heap32ListNext +Heap32Next +HeapAlloc +HeapCompact +HeapCreate +HeapDestroy +HeapFree +HeapLock +HeapReAlloc +HeapSetFlags +HeapSize +HeapUnlock +HeapValidate +HeapWalk +InitAtomTable +InitializeCriticalSection +InitializeCriticalSectionAndSpinCount +InterlockedCompareExchange +InterlockedDecrement +InterlockedExchange +InterlockedExchangeAdd +InterlockedIncrement +InvalidateNLSCache +IsBadCodePtr +IsBadHugeReadPtr +IsBadHugeWritePtr +IsBadReadPtr +IsBadStringPtrA +IsBadStringPtrW +IsBadWritePtr +IsDBCSLeadByte +IsDBCSLeadByteEx +IsDebuggerPresent +IsLSCallback +IsProcessorFeaturePresent +IsSLCallback +IsSystemResumeAutomatic +IsValidCodePage +IsValidLanguageGroup +IsValidLocale +K32Thk1632Epilog +K32Thk1632Prolog +K32_NtCreateFile +K32_RtlNtStatusToDosError +LCMapStringA +LCMapStringW +LeaveCriticalSection +LoadLibraryA +LoadLibraryExA +LoadLibraryExW +LoadLibraryW +LoadModule +LoadResource +LocalAlloc +LocalCompact +LocalFileTimeToFileTime +LocalFlags +LocalFree +LocalHandle +LocalLock +LocalReAlloc +LocalShrink +LocalSize +LocalUnlock +LockFile +LockFileEx +LockResource +MakeCriticalSectionGlobal +MapHInstLS +MapHInstLS_PN +MapHInstSL +MapHInstSL_PN +MapHModuleLS +MapHModuleSL +MapLS +MapSL +MapSLFix +MapViewOfFile +MapViewOfFileEx +Module32First +Module32Next +MoveFileA +MoveFileExA +MoveFileExW +MoveFileW +MulDiv +MultiByteToWideChar +NotifyNLSUserCache +OpenEventA +OpenEventW +OpenFile +OpenFileMappingA +OpenFileMappingW +OpenMutexA +OpenMutexW +OpenProcess +OpenProfileUserMapping +OpenSemaphoreA +OpenSemaphoreW +OpenThread +OpenVxDHandle +OpenWaitableTimerA +OpenWaitableTimerW +OutputDebugStringA +OutputDebugStringW +PeekConsoleInputA +PeekConsoleInputW +PeekNamedPipe +PostQueuedCompletionStatus +PrepareTape +Process32First +Process32Next +PulseEvent +PurgeComm +QT_Thunk +QueryDosDeviceA +QueryDosDeviceW +QueryNumberOfEventLogRecords +QueryOldestEventLogRecord +QueryPerformanceCounter +QueryPerformanceFrequency +QueueUserAPC +RaiseException +ReadConsoleA +ReadConsoleInputA +ReadConsoleInputW +ReadConsoleOutputA +ReadConsoleOutputAttribute +ReadConsoleOutputCharacterA +ReadConsoleOutputCharacterW +ReadConsoleOutputW +ReadConsoleW +ReadDirectoryChangesW +ReadFile +ReadFileEx +ReadFileScatter +ReadProcessMemory +RegisterServiceProcess +RegisterSysMsgHandler +ReinitializeCriticalSection +ReleaseMutex +ReleaseSemaphore +RemoveDirectoryA +RemoveDirectoryW +RequestDeviceWakeup +RequestWakeupLatency +ResetEvent +ResetNLSUserInfoCache +ResetWriteWatch +ResumeThread +RtlAddFunctionTable +RtlDeleteFunctionTable +RtlFillMemory +RtlInstallFunctionTableCallback +RtlMoveMemory +RtlUnwind +RtlUnwindEx +RtlZeroMemory +SMapLS +SMapLS_IP_EBP_12 +SMapLS_IP_EBP_16 +SMapLS_IP_EBP_20 +SMapLS_IP_EBP_24 +SMapLS_IP_EBP_28 +SMapLS_IP_EBP_32 +SMapLS_IP_EBP_36 +SMapLS_IP_EBP_40 +SMapLS_IP_EBP_8 +SUnMapLS +SUnMapLS_IP_EBP_12 +SUnMapLS_IP_EBP_16 +SUnMapLS_IP_EBP_20 +SUnMapLS_IP_EBP_24 +SUnMapLS_IP_EBP_28 +SUnMapLS_IP_EBP_32 +SUnMapLS_IP_EBP_36 +SUnMapLS_IP_EBP_40 +SUnMapLS_IP_EBP_8 +ScrollConsoleScreenBufferA +ScrollConsoleScreenBufferW +SearchPathA +SearchPathW +SetCalendarInfoA +SetCalendarInfoW +SetCommBreak +SetCommConfig +SetCommMask +SetCommState +SetCommTimeouts +SetComputerNameA +SetComputerNameW +SetConsoleActiveScreenBuffer +SetConsoleCP +SetConsoleCtrlHandler +SetConsoleCursorInfo +SetConsoleCursorPosition +SetConsoleMode +SetConsoleOutputCP +SetConsoleScreenBufferSize +SetConsoleTextAttribute +SetConsoleTitleA +SetConsoleTitleW +SetConsoleWindowInfo +SetCriticalSectionSpinCount +SetCurrentDirectoryA +SetCurrentDirectoryW +SetDaylightFlag +SetDefaultCommConfigA +SetDefaultCommConfigW +SetEndOfFile +SetEnvironmentVariableA +SetEnvironmentVariableW +SetErrorMode +SetEvent +SetFileApisToANSI +SetFileApisToOEM +SetFileAttributesA +SetFileAttributesW +SetFilePointer +SetFilePointerEx +SetFileTime +SetHandleContext +SetHandleCount +SetHandleInformation +SetLastError +SetLocalTime +SetLocaleInfoA +SetLocaleInfoW +SetMailslotInfo +SetMessageWaitingIndicator +SetNamedPipeHandleState +SetPriorityClass +SetProcessAffinityMask +SetProcessPriorityBoost +SetProcessShutdownParameters +SetProcessWorkingSetSize +SetStdHandle +SetSystemPowerState +SetSystemTime +SetSystemTimeAdjustment +SetTapeParameters +SetTapePosition +SetThreadAffinityMask +SetThreadContext +SetThreadExecutionState +SetThreadIdealProcessor +SetThreadLocale +SetThreadPriority +SetThreadPriorityBoost +SetTimeZoneInformation +SetUnhandledExceptionFilter +SetUserGeoID +SetVolumeLabelA +SetVolumeLabelW +SetWaitableTimer +SetupComm +SignalObjectAndWait +SignalSysMsgHandlers +SizeofResource +Sleep +SleepEx +SuspendThread +SwitchToFiber +SwitchToThread +SystemTimeToFileTime +SystemTimeToTzSpecificLocalTime +TerminateProcess +TerminateThread +Thread32First +Thread32Next +ThunkConnect32 +TlsAlloc +TlsAllocInternal +TlsFree +TlsFreeInternal +TlsGetValue +TlsSetValue +Toolhelp32ReadProcessMemory +TransactNamedPipe +TransmitCommChar +TryEnterCriticalSection +UTRegister +UTUnRegister +UnMapLS +UnMapSLFixArray +UnhandledExceptionFilter +UninitializeCriticalSection +UnlockFile +UnlockFileEx +UnmapViewOfFile +UpdateResourceA +UpdateResourceW +VerLanguageNameA +VerLanguageNameW +VirtualAlloc +VirtualAllocEx +VirtualFree +VirtualFreeEx +VirtualLock +VirtualProtect +VirtualProtectEx +VirtualQuery +VirtualQueryEx +VirtualUnlock +WaitCommEvent +WaitForDebugEvent +WaitForMultipleObjects +WaitForMultipleObjectsEx +WaitForSingleObject +WaitForSingleObjectEx +WaitNamedPipeA +WaitNamedPipeW +WideCharToMultiByte +WinExec +WriteConsoleA +WriteConsoleInputA +WriteConsoleInputW +WriteConsoleOutputA +WriteConsoleOutputAttribute +WriteConsoleOutputCharacterA +WriteConsoleOutputCharacterW +WriteConsoleOutputW +WriteConsoleW +WriteFile +WriteFileEx +WriteFileGather +WritePrivateProfileSectionA +WritePrivateProfileSectionW +WritePrivateProfileStringA +WritePrivateProfileStringW +WritePrivateProfileStructA +WritePrivateProfileStructW +WriteProcessMemory +WriteProfileSectionA +WriteProfileSectionW +WriteProfileStringA +WriteProfileStringW +WriteTapemark +_DebugOut +_DebugPrintf +_hread +_hwrite +_lclose +_lcreat +_llseek +_lopen +_lread +_lwrite +dprintf +lstrcat +lstrcatA +lstrcatW +lstrcmp +lstrcmpA +lstrcmpW +lstrcmpi +lstrcmpiA +lstrcmpiW +lstrcpy +lstrcpyA +lstrcpyW +lstrcpyn +lstrcpynA +lstrcpynW +lstrlen +lstrlenA +lstrlenW diff --git a/05/tcc-final-old/win32/lib/msvcrt.def b/05/tcc-final-old/win32/lib/msvcrt.def new file mode 100644 index 0000000..742acb8 --- /dev/null +++ b/05/tcc-final-old/win32/lib/msvcrt.def @@ -0,0 +1,1399 @@ +LIBRARY msvcrt.dll + +EXPORTS +$I10_OUTPUT +??0__non_rtti_object@@QAE@ABV0@@Z +??0__non_rtti_object@@QAE@PBD@Z +??0bad_cast@@AAE@PBQBD@Z +??0bad_cast@@QAE@ABQBD@Z +??0bad_cast@@QAE@ABV0@@Z +??0bad_cast@@QAE@PBD@Z +??0bad_typeid@@QAE@ABV0@@Z +??0bad_typeid@@QAE@PBD@Z +??0exception@@QAE@ABQBD@Z +??0exception@@QAE@ABQBDH@Z +??0exception@@QAE@ABV0@@Z +??0exception@@QAE@XZ +??1__non_rtti_object@@UAE@XZ +??1bad_cast@@UAE@XZ +??1bad_typeid@@UAE@XZ +??1exception@@UAE@XZ +??1type_info@@UAE@XZ +??2@YAPAXI@Z +??2@YAPAXIHPBDH@Z +??3@YAXPAX@Z +??4__non_rtti_object@@QAEAAV0@ABV0@@Z +??4bad_cast@@QAEAAV0@ABV0@@Z +??4bad_typeid@@QAEAAV0@ABV0@@Z +??4exception@@QAEAAV0@ABV0@@Z +??8type_info@@QBEHABV0@@Z +??9type_info@@QBEHABV0@@Z +??_7__non_rtti_object@@6B@ +??_7bad_cast@@6B@ +??_7bad_typeid@@6B@ +??_7exception@@6B@ +??_E__non_rtti_object@@UAEPAXI@Z +??_Ebad_cast@@UAEPAXI@Z +??_Ebad_typeid@@UAEPAXI@Z +??_Eexception@@UAEPAXI@Z +??_Fbad_cast@@QAEXXZ +??_Fbad_typeid@@QAEXXZ +??_G__non_rtti_object@@UAEPAXI@Z +??_Gbad_cast@@UAEPAXI@Z +??_Gbad_typeid@@UAEPAXI@Z +??_Gexception@@UAEPAXI@Z +??_U@YAPAXI@Z +??_U@YAPAXIHPBDH@Z +??_V@YAXPAX@Z +?_query_new_handler@@YAP6AHI@ZXZ +?_query_new_mode@@YAHXZ +?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z +?_set_new_mode@@YAHH@Z +?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z +?before@type_info@@QBEHABV1@@Z +?name@type_info@@QBEPBDXZ +?raw_name@type_info@@QBEPBDXZ +?set_new_handler@@YAP6AXXZP6AXXZ@Z +?set_terminate@@YAP6AXXZP6AXXZ@Z +?set_unexpected@@YAP6AXXZP6AXXZ@Z +?terminate@@YAXXZ +?unexpected@@YAXXZ +?what@exception@@UBEPBDXZ +_CIacos +_CIasin +_CIatan +_CIatan2 +_CIcos +_CIcosh +_CIexp +_CIfmod +_CIlog +_CIlog10 +_CIpow +_CIsin +_CIsinh +_CIsqrt +_CItan +_CItanh +_CrtCheckMemory +_CrtDbgBreak +_CrtDbgReport +_CrtDbgReportV +_CrtDbgReportW +_CrtDbgReportWV +_CrtDoForAllClientObjects +_CrtDumpMemoryLeaks +_CrtIsMemoryBlock +_CrtIsValidHeapPointer +_CrtIsValidPointer +_CrtMemCheckpoint +_CrtMemDifference +_CrtMemDumpAllObjectsSince +_CrtMemDumpStatistics +_CrtReportBlockType +_CrtSetAllocHook +_CrtSetBreakAlloc +_CrtSetDbgBlockType +_CrtSetDbgFlag +_CrtSetDumpClient +_CrtSetReportFile +_CrtSetReportHook +_CrtSetReportHook2 +_CrtSetReportMode +_CxxThrowException +_EH_prolog +_Getdays +_Getmonths +_Gettnames +_HUGE +_Strftime +_XcptFilter +__CppXcptFilter +__CxxCallUnwindDelDtor +__CxxCallUnwindDtor +__CxxCallUnwindVecDtor +__CxxDetectRethrow +__CxxExceptionFilter +__CxxFrameHandler +__CxxFrameHandler2 +__CxxFrameHandler3 +__CxxLongjmpUnwind +__CxxQueryExceptionSize +__CxxRegisterExceptionObject +__CxxUnregisterExceptionObject +__DestructExceptionObject +__RTCastToVoid +__RTDynamicCast +__RTtypeid +__STRINGTOLD +___lc_codepage_func +___lc_collate_cp_func +___lc_handle_func +___mb_cur_max_func +___setlc_active_func +___unguarded_readlc_active_add_func +__argc +__argv +__badioinfo +__crtCompareStringA +__crtCompareStringW +__crtGetLocaleInfoW +__crtGetStringTypeW +__crtLCMapStringA +__crtLCMapStringW +__daylight +__dllonexit +__doserrno +__dstbias +__fpecode +__getmainargs +__initenv +__iob_func +__isascii +__iscsym +__iscsymf +__lc_codepage +__lc_collate_cp +__lc_handle +__lconv_init +__libm_sse2_acos +__libm_sse2_acosf +__libm_sse2_asin +__libm_sse2_asinf +__libm_sse2_atan +__libm_sse2_atan2 +__libm_sse2_atanf +__libm_sse2_cos +__libm_sse2_cosf +__libm_sse2_exp +__libm_sse2_expf +__libm_sse2_log +__libm_sse2_log10 +__libm_sse2_log10f +__libm_sse2_logf +__libm_sse2_pow +__libm_sse2_powf +__libm_sse2_sin +__libm_sse2_sinf +__libm_sse2_tan +__libm_sse2_tanf +__mb_cur_max +__p___argc +__p___argv +__p___initenv +__p___mb_cur_max +__p___wargv +__p___winitenv +__p__acmdln +__p__amblksiz +__p__commode +__p__daylight +__p__dstbias +__p__environ +__p__fileinfo +__p__fmode +__p__iob +__p__mbcasemap +__p__mbctype +__p__osver +__p__pctype +__p__pgmptr +__p__pwctype +__p__timezone +__p__tzname +__p__wcmdln +__p__wenviron +__p__winmajor +__p__winminor +__p__winver +__p__wpgmptr +__pctype_func +__pioinfo +__pwctype_func +__pxcptinfoptrs +__set_app_type +__setlc_active +__setusermatherr +__strncnt +__threadhandle +__threadid +__toascii +__unDName +__unDNameEx +__uncaught_exception +__unguarded_readlc_active +__wargv +__wcserror +__wcserror_s +__wcsncnt +__wgetmainargs +__winitenv +_abnormal_termination +_abs64 +_access +_access_s +_acmdln +_adj_fdiv_m16i +_adj_fdiv_m32 +_adj_fdiv_m32i +_adj_fdiv_m64 +_adj_fdiv_r +_adj_fdivr_m16i +_adj_fdivr_m32 +_adj_fdivr_m32i +_adj_fdivr_m64 +_adj_fpatan +_adj_fprem +_adj_fprem1 +_adj_fptan +_adjust_fdiv +_aexit_rtn +_aligned_free +_aligned_free_dbg +_aligned_malloc +_aligned_malloc_dbg +_aligned_offset_malloc +_aligned_offset_malloc_dbg +_aligned_offset_realloc +_aligned_offset_realloc_dbg +_aligned_realloc +_aligned_realloc_dbg +_amsg_exit +_assert +_atodbl +_atodbl_l +_atof_l +_atoflt_l +_atoi64 +_atoi64_l +_atoi_l +_atol_l +_atoldbl +_atoldbl_l +_beep +_beginthread +_beginthreadex +_c_exit +_cabs +_callnewh +_calloc_dbg +_cexit +_cgets +_cgets_s +_cgetws +_cgetws_s +_chdir +_chdrive +_chgsign +_chkesp +_chmod +_chsize +_chsize_s +_chvalidator +_chvalidator_l +_clearfp +_close +_commit +_commode +_control87 +_controlfp +_controlfp_s +_copysign +_cprintf +_cprintf_l +_cprintf_p +_cprintf_p_l +_cprintf_s +_cprintf_s_l +_cputs +_cputws +_creat +_crtAssertBusy +_crtBreakAlloc +_crtDbgFlag +_cscanf +_cscanf_l +_cscanf_s +_cscanf_s_l +_ctime32 +_ctime32_s +_ctime64 +_ctime64_s +_ctype +_cwait +_cwprintf +_cwprintf_l +_cwprintf_p +_cwprintf_p_l +_cwprintf_s +_cwprintf_s_l +_cwscanf +_cwscanf_l +_cwscanf_s +_cwscanf_s_l +_daylight +_difftime32 +_difftime64 +_dstbias +_dup +_dup2 +_ecvt +_ecvt_s +_endthread +_endthreadex +_environ +_eof +_errno +_except_handler2 +_except_handler3 +_except_handler4_common +_execl +_execle +_execlp +_execlpe +_execv +_execve +_execvp +_execvpe +_exit +_expand +_expand_dbg +_fcloseall +_fcvt +_fcvt_s +_fdopen +_fgetchar +_fgetwchar +_filbuf +_fileinfo +_filelength +_filelengthi64 +_fileno +_findclose +_findfirst +_findfirst64 +_findfirsti64 +_findnext +_findnext64 +_findnexti64 +_finite +_flsbuf +_flushall +_fmode +_fpclass +_fpieee_flt +_fpreset +_fprintf_l +_fprintf_p +_fprintf_p_l +_fprintf_s_l +_fputchar +_fputwchar +_free_dbg +_freea +_freea_s +_fscanf_l +_fscanf_s_l +_fseeki64 +_fsopen +_fstat +_fstat64 +_fstati64 +_ftime +_ftime32 +_ftime32_s +_ftime64 +_ftime64_s +_ftol +_ftol2 +_ftol2_sse +_ftol2_sse_excpt +_fullpath +_fullpath_dbg +_futime +_futime32 +_futime64 +_fwprintf_l +_fwprintf_p +_fwprintf_p_l +_fwprintf_s_l +_fwscanf_l +_fwscanf_s_l +_gcvt +_gcvt_s +_get_doserrno +_get_environ +_get_errno +_get_fileinfo +_get_fmode +_get_heap_handle +_get_osfhandle +_get_osplatform +_get_osver +_get_output_format +_get_pgmptr +_get_sbh_threshold +_get_wenviron +_get_winmajor +_get_winminor +_get_winver +_get_wpgmptr +_getch +_getche +_getcwd +_getdcwd +_getdiskfree +_getdllprocaddr +_getdrive +_getdrives +_getmaxstdio +_getmbcp +_getpid +_getsystime +_getw +_getwch +_getwche +_getws +_global_unwind2 +_gmtime32 +_gmtime32_s +_gmtime64 +_gmtime64_s +_heapadd +_heapchk +_heapmin +_heapset +_heapused +_heapwalk +_hypot +_i64toa +_i64toa_s +_i64tow +_i64tow_s +_initterm +_initterm_e +_inp +_inpd +_inpw +_invalid_parameter +_iob +_isalnum_l +_isalpha_l +_isatty +_iscntrl_l +_isctype +_isctype_l +_isdigit_l +_isgraph_l +_isleadbyte_l +_islower_l +_ismbbalnum +_ismbbalnum_l +_ismbbalpha +_ismbbalpha_l +_ismbbgraph +_ismbbgraph_l +_ismbbkalnum +_ismbbkalnum_l +_ismbbkana +_ismbbkana_l +_ismbbkprint +_ismbbkprint_l +_ismbbkpunct +_ismbbkpunct_l +_ismbblead +_ismbblead_l +_ismbbprint +_ismbbprint_l +_ismbbpunct +_ismbbpunct_l +_ismbbtrail +_ismbbtrail_l +_ismbcalnum +_ismbcalnum_l +_ismbcalpha +_ismbcalpha_l +_ismbcdigit +_ismbcdigit_l +_ismbcgraph +_ismbcgraph_l +_ismbchira +_ismbchira_l +_ismbckata +_ismbckata_l +_ismbcl0 +_ismbcl0_l +_ismbcl1 +_ismbcl1_l +_ismbcl2 +_ismbcl2_l +_ismbclegal +_ismbclegal_l +_ismbclower +_ismbclower_l +_ismbcprint +_ismbcprint_l +_ismbcpunct +_ismbcpunct_l +_ismbcspace +_ismbcspace_l +_ismbcsymbol +_ismbcsymbol_l +_ismbcupper +_ismbcupper_l +_ismbslead +_ismbslead_l +_ismbstrail +_ismbstrail_l +_isnan +_isprint_l +_isspace_l +_isupper_l +_iswalnum_l +_iswalpha_l +_iswcntrl_l +_iswctype_l +_iswdigit_l +_iswgraph_l +_iswlower_l +_iswprint_l +_iswpunct_l +_iswspace_l +_iswupper_l +_iswxdigit_l +_isxdigit_l +_itoa +_itoa_s +_itow +_itow_s +_j0 +_j1 +_jn +_kbhit +_lfind +_lfind_s +_loaddll +_local_unwind2 +_local_unwind4 +_localtime32 +_localtime32_s +_localtime64 +_localtime64_s +_lock +_locking +_logb +_longjmpex +_lrotl +_lrotr +_lsearch +_lsearch_s +_lseek +_lseeki64 +_ltoa +_ltoa_s +_ltow +_ltow_s +_makepath +_makepath_s +_malloc_dbg +_mbbtombc +_mbbtombc_l +_mbbtype +_mbcasemap +_mbccpy +_mbccpy_l +_mbccpy_s +_mbccpy_s_l +_mbcjistojms +_mbcjistojms_l +_mbcjmstojis +_mbcjmstojis_l +_mbclen +_mbclen_l +_mbctohira +_mbctohira_l +_mbctokata +_mbctokata_l +_mbctolower +_mbctolower_l +_mbctombb +_mbctombb_l +_mbctoupper +_mbctoupper_l +_mbctype +_mblen_l +_mbsbtype +_mbsbtype_l +_mbscat +_mbscat_s +_mbscat_s_l +_mbschr +_mbschr_l +_mbscmp +_mbscmp_l +_mbscoll +_mbscoll_l +_mbscpy +_mbscpy_s +_mbscpy_s_l +_mbscspn +_mbscspn_l +_mbsdec +_mbsdec_l +_mbsdup +_mbsicmp +_mbsicmp_l +_mbsicoll +_mbsicoll_l +_mbsinc +_mbsinc_l +_mbslen +_mbslen_l +_mbslwr +_mbslwr_l +_mbslwr_s +_mbslwr_s_l +_mbsnbcat +_mbsnbcat_l +_mbsnbcat_s +_mbsnbcat_s_l +_mbsnbcmp +_mbsnbcmp_l +_mbsnbcnt +_mbsnbcnt_l +_mbsnbcoll +_mbsnbcoll_l +_mbsnbcpy +_mbsnbcpy_l +_mbsnbcpy_s +_mbsnbcpy_s_l +_mbsnbicmp +_mbsnbicmp_l +_mbsnbicoll +_mbsnbicoll_l +_mbsnbset +_mbsnbset_l +_mbsnbset_s +_mbsnbset_s_l +_mbsncat +_mbsncat_l +_mbsncat_s +_mbsncat_s_l +_mbsnccnt +_mbsnccnt_l +_mbsncmp +_mbsncmp_l +_mbsncoll +_mbsncoll_l +_mbsncpy +_mbsncpy_l +_mbsncpy_s +_mbsncpy_s_l +_mbsnextc +_mbsnextc_l +_mbsnicmp +_mbsnicmp_l +_mbsnicoll +_mbsnicoll_l +_mbsninc +_mbsninc_l +_mbsnlen +_mbsnlen_l +_mbsnset +_mbsnset_l +_mbsnset_s +_mbsnset_s_l +_mbspbrk +_mbspbrk_l +_mbsrchr +_mbsrchr_l +_mbsrev +_mbsrev_l +_mbsset +_mbsset_l +_mbsset_s +_mbsset_s_l +_mbsspn +_mbsspn_l +_mbsspnp +_mbsspnp_l +_mbsstr +_mbsstr_l +_mbstok +_mbstok_l +_mbstok_s +_mbstok_s_l +_mbstowcs_l +_mbstowcs_s_l +_mbstrlen +_mbstrlen_l +_mbstrnlen +_mbstrnlen_l +_mbsupr +_mbsupr_l +_mbsupr_s +_mbsupr_s_l +_mbtowc_l +_memccpy +_memicmp +_memicmp_l +_mkdir +_mkgmtime +_mkgmtime32 +_mkgmtime64 +_mktemp +_mktemp_s +_mktime32 +_mktime64 +_msize +_msize_debug +_nextafter +_onexit +_open +_open_osfhandle +_osplatform +_osver +_outp +_outpd +_outpw +_pclose +_pctype +_pgmptr +_pipe +_popen +_printf_l +_printf_p +_printf_p_l +_printf_s_l +_purecall +_putch +_putenv +_putenv_s +_putw +_putwch +_putws +_pwctype +_read +_realloc_dbg +_resetstkoflw +_rmdir +_rmtmp +_rotl +_rotl64 +_rotr +_rotr64 +_safe_fdiv +_safe_fdivr +_safe_fprem +_safe_fprem1 +_scalb +_scanf_l +_scanf_s_l +_scprintf +_scprintf_l +_scprintf_p_l +_scwprintf +_scwprintf_l +_scwprintf_p_l +_searchenv +_searchenv_s +_seh_longjmp_unwind +_seh_longjmp_unwind4 +_set_SSE2_enable +_set_controlfp +_set_doserrno +_set_errno +_set_error_mode +_set_fileinfo +_set_fmode +_set_output_format +_set_sbh_threshold +_seterrormode +_setjmp +_setjmp3 +_setmaxstdio +_setmbcp +_setmode +_setsystime +_sleep +_snprintf +_snprintf_c +_snprintf_c_l +_snprintf_l +_snprintf_s +_snprintf_s_l +_snscanf +_snscanf_l +_snscanf_s +_snscanf_s_l +_snwprintf +_snwprintf_l +_snwprintf_s +_snwprintf_s_l +_snwscanf +_snwscanf_l +_snwscanf_s +_snwscanf_s_l +_sopen +_sopen_s +_spawnl +_spawnle +_spawnlp +_spawnlpe +_spawnv +_spawnve +_spawnvp +_spawnvpe +_splitpath +_splitpath_s +_sprintf_l +_sprintf_p_l +_sprintf_s_l +_sscanf_l +_sscanf_s_l +_stat +_stat64 +_stati64 +_statusfp +_strcmpi +_strcoll_l +_strdate +_strdate_s +_strdup +_strdup_dbg +_strerror +_strerror_s +_stricmp +_stricmp_l +_stricoll +_stricoll_l +_strlwr +_strlwr_l +_strlwr_s +_strlwr_s_l +_strncoll +_strncoll_l +_strnicmp +_strnicmp_l +_strnicoll +_strnicoll_l +_strnset +_strnset_s +_strrev +_strset +_strset_s +_strtime +_strtime_s +_strtod_l +_strtoi64 +_strtoi64_l +_strtol_l +_strtoui64 +_strtoui64_l +_strtoul_l +_strupr +_strupr_l +_strupr_s +_strupr_s_l +_strxfrm_l +_swab +_swprintf +_swprintf_c +_swprintf_c_l +_swprintf_p_l +_swprintf_s_l +_swscanf_l +_swscanf_s_l +_sys_errlist +_sys_nerr +_tell +_telli64 +_tempnam +_tempnam_dbg +_time32 +_time64 +_timezone +_tolower +_tolower_l +_toupper +_toupper_l +_towlower_l +_towupper_l +_tzname +_tzset +_ui64toa +_ui64toa_s +_ui64tow +_ui64tow_s +_ultoa +_ultoa_s +_ultow +_ultow_s +_umask +_umask_s +_ungetch +_ungetwch +_unlink +_unloaddll +_unlock +_utime +_utime32 +_utime64 +_vcprintf +_vcprintf_l +_vcprintf_p +_vcprintf_p_l +_vcprintf_s +_vcprintf_s_l +_vcwprintf +_vcwprintf_l +_vcwprintf_p +_vcwprintf_p_l +_vcwprintf_s +_vcwprintf_s_l +_vfprintf_l +_vfprintf_p +_vfprintf_p_l +_vfprintf_s_l +_vfwprintf_l +_vfwprintf_p +_vfwprintf_p_l +_vfwprintf_s_l +_vprintf_l +_vprintf_p +_vprintf_p_l +_vprintf_s_l +_vscprintf +_vscprintf_l +_vscprintf_p_l +_vscwprintf +_vscwprintf_l +_vscwprintf_p_l +_vsnprintf +_vsnprintf_c +_vsnprintf_c_l +_vsnprintf_l +_vsnprintf_s +_vsnprintf_s_l +_vsnwprintf +_vsnwprintf_l +_vsnwprintf_s +_vsnwprintf_s_l +_vsprintf_l +_vsprintf_p +_vsprintf_p_l +_vsprintf_s_l +_vswprintf +_vswprintf_c +_vswprintf_c_l +_vswprintf_l +_vswprintf_p_l +_vswprintf_s_l +_vwprintf_l +_vwprintf_p +_vwprintf_p_l +_vwprintf_s_l +_waccess +_waccess_s +_wasctime +_wasctime_s +_wassert +_wchdir +_wchmod +_wcmdln +_wcreat +_wcscoll_l +_wcsdup +_wcsdup_dbg +_wcserror +_wcserror_s +_wcsftime_l +_wcsicmp +_wcsicmp_l +_wcsicoll +_wcsicoll_l +_wcslwr +_wcslwr_l +_wcslwr_s +_wcslwr_s_l +_wcsncoll +_wcsncoll_l +_wcsnicmp +_wcsnicmp_l +_wcsnicoll +_wcsnicoll_l +_wcsnset +_wcsnset_s +_wcsrev +_wcsset +_wcsset_s +_wcstoi64 +_wcstoi64_l +_wcstol_l +_wcstombs_l +_wcstombs_s_l +_wcstoui64 +_wcstoui64_l +_wcstoul_l +_wcsupr +_wcsupr_l +_wcsupr_s +_wcsupr_s_l +_wcsxfrm_l +_wctime +_wctime32 +_wctime32_s +_wctime64 +_wctime64_s +_wctomb_l +_wctomb_s_l +_wctype +_wenviron +_wexecl +_wexecle +_wexeclp +_wexeclpe +_wexecv +_wexecve +_wexecvp +_wexecvpe +_wfdopen +_wfindfirst +_wfindfirst64 +_wfindfirsti64 +_wfindnext +_wfindnext64 +_wfindnexti64 +_wfopen +_wfopen_s +_wfreopen +_wfreopen_s +_wfsopen +_wfullpath +_wfullpath_dbg +_wgetcwd +_wgetdcwd +_wgetenv +_wgetenv_s +_winmajor +_winminor +_winput_s +_winver +_wmakepath +_wmakepath_s +_wmkdir +_wmktemp +_wmktemp_s +_wopen +_woutput_s +_wperror +_wpgmptr +_wpopen +_wprintf_l +_wprintf_p +_wprintf_p_l +_wprintf_s_l +_wputenv +_wputenv_s +_wremove +_wrename +_write +_wrmdir +_wscanf_l +_wscanf_s_l +_wsearchenv +_wsearchenv_s +_wsetlocale +_wsopen +_wsopen_s +_wspawnl +_wspawnle +_wspawnlp +_wspawnlpe +_wspawnv +_wspawnve +_wspawnvp +_wspawnvpe +_wsplitpath +_wsplitpath_s +_wstat +_wstat64 +_wstati64 +_wstrdate +_wstrdate_s +_wstrtime +_wstrtime_s +_wsystem +_wtempnam +_wtempnam_dbg +_wtmpnam +_wtmpnam_s +_wtof +_wtof_l +_wtoi +_wtoi64 +_wtoi64_l +_wtoi_l +_wtol +_wtol_l +_wunlink +_wutime +_wutime32 +_wutime64 +_y0 +_y1 +_yn +abort +abs +acos +asctime +asctime_s +asin +atan +atan2 +atexit +atof +atoi +atol +bsearch +bsearch_s +btowc +calloc +ceil +clearerr +clearerr_s +clock +cos +cosh +ctime +difftime +div +exit +exp +fabs +fclose +feof +ferror +fflush +fgetc +fgetpos +fgets +fgetwc +fgetws +floor +fmod +fopen +fopen_s +fprintf +fprintf_s +fputc +fputs +fputwc +fputws +fread +free +freopen +freopen_s +frexp +fscanf +fscanf_s +fseek +fsetpos +ftell +fwprintf +fwprintf_s +fwrite +fwscanf +fwscanf_s +getc +getchar +getenv +getenv_s +gets +getwc +getwchar +gmtime +is_wctype +isalnum +isalpha +iscntrl +isdigit +isgraph +isleadbyte +islower +isprint +ispunct +isspace +isupper +iswalnum +iswalpha +iswascii +iswcntrl +iswctype +iswdigit +iswgraph +iswlower +iswprint +iswpunct +iswspace +iswupper +iswxdigit +isxdigit +labs +ldexp +ldiv +localeconv +localtime +log +log10 +longjmp +malloc +mblen +mbrlen +mbrtowc +mbsdup_dbg +mbsrtowcs +mbsrtowcs_s +mbstowcs +mbstowcs_s +mbtowc +memchr +memcmp +memcpy +memcpy_s +memmove +memmove_s +memset +mktime +modf +perror +pow +printf +printf_s +putc +putchar +puts +putwc +putwchar +qsort +qsort_s +raise +rand +rand_s +realloc +remove +rename +rewind +scanf +scanf_s +setbuf +setlocale +setvbuf +signal +sin +sinh +sprintf +sprintf_s +sqrt +srand +sscanf +sscanf_s +strcat +strcat_s +strchr +strcmp +strcoll +strcpy +strcpy_s +strcspn +strerror +strerror_s +strftime +strlen +strncat +strncat_s +strncmp +strncpy +strncpy_s +strnlen +strpbrk +strrchr +strspn +strstr +strtod +strtok +strtok_s +strtol +strtoul +strxfrm +swprintf +swprintf_s +swscanf +swscanf_s +system +tan +tanh +time +tmpfile +tmpfile_s +tmpnam +tmpnam_s +tolower +toupper +towlower +towupper +ungetc +ungetwc +utime +vfprintf +vfprintf_s +vfwprintf +vfwprintf_s +vprintf +vprintf_s +vsnprintf +vsprintf +vsprintf_s +vswprintf +vswprintf_s +vwprintf +vwprintf_s +wcrtomb +wcrtomb_s +wcscat +wcscat_s +wcschr +wcscmp +wcscoll +wcscpy +wcscpy_s +wcscspn +wcsftime +wcslen +wcsncat +wcsncat_s +wcsncmp +wcsncpy +wcsncpy_s +wcsnlen +wcspbrk +wcsrchr +wcsrtombs +wcsrtombs_s +wcsspn +wcsstr +wcstod +wcstok +wcstok_s +wcstol +wcstombs +wcstombs_s +wcstoul +wcsxfrm +wctob +wctomb +wctomb_s +wprintf +wprintf_s +wscanf +wscanf_s diff --git a/05/tcc-final-old/win32/lib/user32.def b/05/tcc-final-old/win32/lib/user32.def new file mode 100644 index 0000000..a034dac --- /dev/null +++ b/05/tcc-final-old/win32/lib/user32.def @@ -0,0 +1,658 @@ +LIBRARY user32.dll + +EXPORTS +ActivateKeyboardLayout +AdjustWindowRect +AdjustWindowRectEx +AlignRects +AllowSetForegroundWindow +AnimateWindow +AnyPopup +AppendMenuA +AppendMenuW +ArrangeIconicWindows +AttachThreadInput +BeginDeferWindowPos +BeginPaint +BlockInput +BringWindowToTop +BroadcastSystemMessage +BroadcastSystemMessageA +BroadcastSystemMessageW +CalcChildScroll +CallMsgFilter +CallMsgFilterA +CallMsgFilterW +CallNextHookEx +CallWindowProcA +CallWindowProcW +CascadeChildWindows +CascadeWindows +ChangeClipboardChain +ChangeDisplaySettingsA +ChangeDisplaySettingsExA +ChangeDisplaySettingsExW +ChangeDisplaySettingsW +ChangeMenuA +ChangeMenuW +CharLowerA +CharLowerBuffA +CharLowerBuffW +CharLowerW +CharNextA +CharNextExA +CharNextExW +CharNextW +CharPrevA +CharPrevExA +CharPrevExW +CharPrevW +CharToOemA +CharToOemBuffA +CharToOemBuffW +CharToOemW +CharUpperA +CharUpperBuffA +CharUpperBuffW +CharUpperW +CheckDlgButton +CheckMenuItem +CheckMenuRadioItem +CheckRadioButton +ChildWindowFromPoint +ChildWindowFromPointEx +ClientThreadConnect +ClientToScreen +ClipCursor +CloseClipboard +CloseDesktop +CloseWindow +CloseWindowStation +CopyAcceleratorTableA +CopyAcceleratorTableW +CopyIcon +CopyImage +CopyRect +CountClipboardFormats +CreateAcceleratorTableA +CreateAcceleratorTableW +CreateCaret +CreateCursor +CreateDesktopA +CreateDesktopW +CreateDialogIndirectParamA +CreateDialogIndirectParamW +CreateDialogParamA +CreateDialogParamW +CreateIcon +CreateIconFromResource +CreateIconFromResourceEx +CreateIconIndirect +CreateMDIWindowA +CreateMDIWindowW +CreateMenu +CreatePopupMenu +CreateWindowExA +CreateWindowExW +CreateWindowStationA +CreateWindowStationW +DdeAbandonTransaction +DdeAccessData +DdeAddData +DdeClientTransaction +DdeCmpStringHandles +DdeConnect +DdeConnectList +DdeCreateDataHandle +DdeCreateStringHandleA +DdeCreateStringHandleW +DdeDisconnect +DdeDisconnectList +DdeEnableCallback +DdeFreeDataHandle +DdeFreeStringHandle +DdeGetData +DdeGetLastError +DdeImpersonateClient +DdeInitializeA +DdeInitializeW +DdeKeepStringHandle +DdeNameService +DdePostAdvise +DdeQueryConvInfo +DdeQueryNextServer +DdeQueryStringA +DdeQueryStringW +DdeReconnect +DdeSetQualityOfService +DdeSetUserHandle +DdeUnaccessData +DdeUninitialize +DefDlgProcA +DefDlgProcW +DefFrameProcA +DefFrameProcW +DefMDIChildProcA +DefMDIChildProcW +DefWindowProcA +DefWindowProcW +DeferWindowPos +DeleteMenu +DestroyAcceleratorTable +DestroyCaret +DestroyCursor +DestroyIcon +DestroyMenu +DestroyWindow +DialogBoxIndirectParamA +DialogBoxIndirectParamW +DialogBoxParamA +DialogBoxParamW +DispatchMessageA +DispatchMessageW +DlgDirListA +DlgDirListComboBoxA +DlgDirListComboBoxW +DlgDirListW +DlgDirSelectComboBoxExA +DlgDirSelectComboBoxExW +DlgDirSelectExA +DlgDirSelectExW +DragDetect +DragObject +DrawAnimatedRects +DrawCaption +DrawCaptionTempA +DrawCaptionTempW +DrawEdge +DrawFocusRect +DrawFrame +DrawFrameControl +DrawIcon +DrawIconEx +DrawMenuBar +DrawMenuBarTemp +DrawStateA +DrawStateW +DrawTextA +DrawTextExA +DrawTextExW +DrawTextW +EditWndProc +EmptyClipboard +EnableMenuItem +EnableScrollBar +EnableWindow +EndDeferWindowPos +EndDialog +EndMenu +EndPaint +EndTask +EnumChildWindows +EnumClipboardFormats +EnumDesktopWindows +EnumDesktopsA +EnumDesktopsW +EnumDisplayDevicesA +EnumDisplayDevicesW +EnumDisplayMonitors +EnumDisplaySettingsA +EnumDisplaySettingsExA +EnumDisplaySettingsExW +EnumDisplaySettingsW +EnumPropsA +EnumPropsExA +EnumPropsExW +EnumPropsW +EnumThreadWindows +EnumWindowStationsA +EnumWindowStationsW +EnumWindows +EqualRect +ExcludeUpdateRgn +ExitWindowsEx +FillRect +FindWindowA +FindWindowExA +FindWindowExW +FindWindowW +FlashWindow +FlashWindowEx +FrameRect +FreeDDElParam +GetActiveWindow +GetAltTabInfo +GetAncestor +GetAsyncKeyState +GetCapture +GetCaretBlinkTime +GetCaretPos +GetClassInfoA +GetClassInfoExA +GetClassInfoExW +GetClassInfoW +GetClassLongA +GetClassLongW +GetClassNameA +GetClassNameW +GetClassWord +GetClientRect +GetClipCursor +GetClipboardData +GetClipboardFormatNameA +GetClipboardFormatNameW +GetClipboardOwner +GetClipboardSequenceNumber +GetClipboardViewer +GetComboBoxInfo +GetCursor +GetCursorInfo +GetCursorPos +GetDC +GetDCEx +GetDesktopWindow +GetDialogBaseUnits +GetDlgCtrlID +GetDlgItem +GetDlgItemInt +GetDlgItemTextA +GetDlgItemTextW +GetDoubleClickTime +GetFocus +GetForegroundWindow +GetGUIThreadInfo +GetGuiResources +GetIconInfo +GetInputDesktop +GetInputState +GetInternalWindowPos +GetKBCodePage +GetKeyNameTextA +GetKeyNameTextW +GetKeyState +GetKeyboardLayout +GetKeyboardLayoutList +GetKeyboardLayoutNameA +GetKeyboardLayoutNameW +GetKeyboardState +GetKeyboardType +GetLastActivePopup +GetListBoxInfo +GetMenu +GetMenuBarInfo +GetMenuCheckMarkDimensions +GetMenuContextHelpId +GetMenuDefaultItem +GetMenuInfo +GetMenuItemCount +GetMenuItemID +GetMenuItemInfoA +GetMenuItemInfoW +GetMenuItemRect +GetMenuState +GetMenuStringA +GetMenuStringW +GetMessageA +GetMessageExtraInfo +GetMessagePos +GetMessageTime +GetMessageW +GetMonitorInfoA +GetMonitorInfoW +GetMouseMovePoints +GetMouseMovePointsEx +GetNextDlgGroupItem +GetNextDlgTabItem +GetNextQueueWindow +GetOpenClipboardWindow +GetParent +GetPriorityClipboardFormat +GetProcessDefaultLayout +GetProcessWindowStation +GetPropA +GetPropW +GetQueueStatus +GetScrollBarInfo +GetScrollInfo +GetScrollPos +GetScrollRange +GetShellWindow +GetSubMenu +GetSysColor +GetSysColorBrush +GetSystemMenu +GetSystemMetrics +GetTabbedTextExtentA +GetTabbedTextExtentW +GetThreadDesktop +GetTitleBarInfo +GetTopWindow +GetUpdateRect +GetUpdateRgn +GetUserObjectInformationA +GetUserObjectInformationW +GetUserObjectSecurity +GetWindow +GetWindowContextHelpId +GetWindowDC +GetWindowInfo +GetWindowLongPtrA +GetWindowLongPtrW +SetWindowLongPtrA +SetWindowLongPtrW +GetWindowLongA +GetWindowLongW +GetWindowModuleFileNameA +GetWindowModuleFileNameW +GetWindowPlacement +GetWindowRect +GetWindowRgn +GetWindowTextA +GetWindowTextLengthA +GetWindowTextLengthW +GetWindowTextW +GetWindowThreadProcessId +GetWindowWord +GrayStringA +GrayStringW +HasSystemSleepStarted +HideCaret +HiliteMenuItem +IMPGetIMEA +IMPGetIMEW +IMPQueryIMEA +IMPQueryIMEW +IMPSetIMEA +IMPSetIMEW +ImpersonateDdeClientWindow +InSendMessage +InSendMessageEx +InflateRect +InitSharedTable +InitTask +InsertMenuA +InsertMenuItemA +InsertMenuItemW +InsertMenuW +InternalGetWindowText +IntersectRect +InvalidateRect +InvalidateRgn +InvertRect +IsCharAlphaA +IsCharAlphaNumericA +IsCharAlphaNumericW +IsCharAlphaW +IsCharLowerA +IsCharLowerW +IsCharUpperA +IsCharUpperW +IsChild +IsClipboardFormatAvailable +IsDialogMessage +IsDialogMessageA +IsDialogMessageW +IsDlgButtonChecked +IsHungThread +IsIconic +IsMenu +IsRectEmpty +IsWindow +IsWindowEnabled +IsWindowUnicode +IsWindowVisible +IsZoomed +KillTimer +LoadAcceleratorsA +LoadAcceleratorsW +LoadBitmapA +LoadBitmapW +LoadCursorA +LoadCursorFromFileA +LoadCursorFromFileW +LoadCursorW +LoadIconA +LoadIconW +LoadImageA +LoadImageW +LoadKeyboardLayoutA +LoadKeyboardLayoutW +LoadMenuA +LoadMenuIndirectA +LoadMenuIndirectW +LoadMenuW +LoadStringA +LoadStringW +LockSetForegroundWindow +LockWindowStation +LockWindowUpdate +LookupIconIdFromDirectory +LookupIconIdFromDirectoryEx +MapDialogRect +MapVirtualKeyA +MapVirtualKeyExA +MapVirtualKeyExW +MapVirtualKeyW +MapWindowPoints +MenuItemFromPoint +MessageBeep +MessageBoxA +MessageBoxExA +MessageBoxExW +MessageBoxIndirectA +MessageBoxIndirectW +MessageBoxW +ModifyAccess +ModifyMenuA +ModifyMenuW +MonitorFromPoint +MonitorFromRect +MonitorFromWindow +MoveWindow +MsgWaitForMultipleObjects +MsgWaitForMultipleObjectsEx +NotifyWinEvent +OemKeyScan +OemToCharA +OemToCharBuffA +OemToCharBuffW +OemToCharW +OffsetRect +OpenClipboard +OpenDesktopA +OpenDesktopW +OpenIcon +OpenInputDesktop +OpenWindowStationA +OpenWindowStationW +PackDDElParam +PaintDesktop +PeekMessageA +PeekMessageW +PlaySoundEvent +PostMessageA +PostMessageW +PostQuitMessage +PostThreadMessageA +PostThreadMessageW +PtInRect +RealChildWindowFromPoint +RealGetWindowClass +RedrawWindow +RegisterClassA +RegisterClassExA +RegisterClassExW +RegisterClassW +RegisterClipboardFormatA +RegisterClipboardFormatW +RegisterDeviceNotificationA +RegisterDeviceNotificationW +RegisterHotKey +RegisterLogonProcess +RegisterNetworkCapabilities +RegisterSystemThread +RegisterTasklist +RegisterWindowMessageA +RegisterWindowMessageW +ReleaseCapture +ReleaseDC +RemoveMenu +RemovePropA +RemovePropW +ReplyMessage +ReuseDDElParam +ScreenToClient +ScrollDC +ScrollWindow +ScrollWindowEx +SendDlgItemMessageA +SendDlgItemMessageW +SendIMEMessageExA +SendIMEMessageExW +SendInput +SendMessageA +SendMessageCallbackA +SendMessageCallbackW +SendMessageTimeoutA +SendMessageTimeoutW +SendMessageW +SendNotifyMessageA +SendNotifyMessageW +SetActiveWindow +SetCapture +SetCaretBlinkTime +SetCaretPos +SetClassLongA +SetClassLongW +SetClassWord +SetClipboardData +SetClipboardViewer +SetCursor +SetCursorPos +SetDebugErrorLevel +SetDeskWallpaper +SetDesktopBitmap +SetDlgItemInt +SetDlgItemTextA +SetDlgItemTextW +SetDoubleClickTime +SetFocus +SetForegroundWindow +SetInternalWindowPos +SetKeyboardState +SetLastErrorEx +SetLogonNotifyWindow +SetMenu +SetMenuContextHelpId +SetMenuDefaultItem +SetMenuInfo +SetMenuItemBitmaps +SetMenuItemInfoA +SetMenuItemInfoW +SetMessageExtraInfo +SetMessageQueue +SetParent +SetProcessDefaultLayout +SetProcessWindowStation +SetPropA +SetPropW +SetRect +SetRectEmpty +SetScrollInfo +SetScrollPos +SetScrollRange +SetShellWindow +SetSysColors +SetSysColorsTemp +SetSystemCursor +SetThreadDesktop +SetTimer +SetUserObjectInformationA +SetUserObjectInformationW +SetUserObjectSecurity +SetWinEventHook +SetWindowContextHelpId +SetWindowFullScreenState +SetWindowLongA +SetWindowLongW +SetWindowPlacement +SetWindowPos +SetWindowRgn +SetWindowTextA +SetWindowTextW +SetWindowWord +SetWindowsHookA +SetWindowsHookExA +SetWindowsHookExW +SetWindowsHookW +ShowCaret +ShowCursor +ShowOwnedPopups +ShowScrollBar +ShowWindow +ShowWindowAsync +SubtractRect +SwapMouseButton +SwitchDesktop +SwitchToThisWindow +SysErrorBox +SystemParametersInfoA +SystemParametersInfoW +TabbedTextOutA +TabbedTextOutW +TileChildWindows +TileWindows +ToAscii +ToAsciiEx +ToUnicode +ToUnicodeEx +TrackMouseEvent +TrackPopupMenu +TrackPopupMenuEx +TranslateAccelerator +TranslateAcceleratorA +TranslateAcceleratorW +TranslateMDISysAccel +TranslateMessage +UnhookWinEvent +UnhookWindowsHook +UnhookWindowsHookEx +UnionRect +UnloadKeyboardLayout +UnlockWindowStation +UnpackDDElParam +UnregisterClassA +UnregisterClassW +UnregisterDeviceNotification +UnregisterHotKey +UpdateWindow +UserClientDllInitialize +UserIsSystemResumeAutomatic +UserSetDeviceHoldState +UserSignalProc +UserTickleTimer +ValidateRect +ValidateRgn +VkKeyScanA +VkKeyScanExA +VkKeyScanExW +VkKeyScanW +WINNLSEnableIME +WINNLSGetEnableStatus +WINNLSGetIMEHotkey +WNDPROC_CALLBACK +WaitForInputIdle +WaitMessage +WinHelpA +WinHelpW +WinOldAppHackoMatic +WindowFromDC +WindowFromPoint +YieldTask +_SetProcessDefaultLayout +keybd_event +mouse_event +wsprintfA +wsprintfW +wvsprintfA +wvsprintfW diff --git a/05/tcc-final-old/win32/lib/wincrt1.c b/05/tcc-final-old/win32/lib/wincrt1.c new file mode 100644 index 0000000..ce3a63f --- /dev/null +++ b/05/tcc-final-old/win32/lib/wincrt1.c @@ -0,0 +1,75 @@ +//+--------------------------------------------------------------------------- + +// _UNICODE for tchar.h, UNICODE for API +#include + +#include +#include + +#define __UNKNOWN_APP 0 +#define __CONSOLE_APP 1 +#define __GUI_APP 2 +void __set_app_type(int); +void _controlfp(unsigned a, unsigned b); + +#ifdef _UNICODE +#define __tgetmainargs __wgetmainargs +#define _twinstart _wwinstart +#define _runtwinmain _runwwinmain +int APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int); +#else +#define __tgetmainargs __getmainargs +#define _twinstart _winstart +#define _runtwinmain _runwinmain +#endif + +typedef struct { int newmode; } _startupinfo; +int __cdecl __tgetmainargs(int *pargc, _TCHAR ***pargv, _TCHAR ***penv, int globb, _startupinfo*); + +static int go_winmain(TCHAR *arg1) +{ + STARTUPINFO si; + _TCHAR *szCmd, *p; + int fShow; + + GetStartupInfo(&si); + if (si.dwFlags & STARTF_USESHOWWINDOW) + fShow = si.wShowWindow; + else + fShow = SW_SHOWDEFAULT; + + szCmd = NULL, p = GetCommandLine(); + if (arg1) + szCmd = _tcsstr(p, arg1); + if (NULL == szCmd) + szCmd = _tcsdup(__T("")); + else if (szCmd > p && szCmd[-1] == __T('"')) + --szCmd; +#if defined __i386__ || defined __x86_64__ + _controlfp(0x10000, 0x30000); +#endif + return _tWinMain(GetModuleHandle(NULL), NULL, szCmd, fShow); +} + +int _twinstart(void) +{ + __TRY__ + _startupinfo start_info_con = {0}; + __set_app_type(__GUI_APP); + __tgetmainargs(&__argc, &__targv, &_tenviron, 0, &start_info_con); + exit(go_winmain(__argc > 1 ? __targv[1] : NULL)); +} + +int _runtwinmain(int argc, /* as tcc passed in */ char **argv) +{ +#ifdef UNICODE + _startupinfo start_info = {0}; + __tgetmainargs(&__argc, &__targv, &_tenviron, 0, &start_info); + /* may be wrong when tcc has received wildcards (*.c) */ + if (argc < __argc) + __targv += __argc - argc, __argc = argc; +#else + __argc = argc, __targv = argv; +#endif + return go_winmain(__argc > 1 ? __targv[1] : NULL); +} diff --git a/05/tcc-final-old/win32/lib/wincrt1w.c b/05/tcc-final-old/win32/lib/wincrt1w.c new file mode 100644 index 0000000..a7d349e --- /dev/null +++ b/05/tcc-final-old/win32/lib/wincrt1w.c @@ -0,0 +1,3 @@ +#define _UNICODE 1 +#define UNICODE 1 +#include "wincrt1.c" diff --git a/05/tcc-final-old/win32/tcc-win32.txt b/05/tcc-final-old/win32/tcc-win32.txt new file mode 100644 index 0000000..ab73007 --- /dev/null +++ b/05/tcc-final-old/win32/tcc-win32.txt @@ -0,0 +1,168 @@ + + TinyCC + ====== + + This file contains specific information for usage of TinyCC + under MS-Windows. See tcc-doc.html to have all the features. + + + Installation from the binary ZIP package: + ----------------------------------------- + Unzip the package to a directory of your choice. + + + Set the system PATH: + -------------------- + To be able to invoke the compiler from everywhere on your computer by + just typing "tcc", please add the directory containing tcc.exe to your + system PATH. + + + Include and library search paths + -------------------------------- + On windows, the standard "include" and "lib" directories are searched + relatively from the location of the executables (tcc.exe, libtcc.dll). + + + Examples: + --------- + Open a console window (DOS box) and 'cd' to the examples directory. + + For the 'Fibonacci' example type: + + tcc fib.c + + For the 'Hello Windows' GUI example type: + + tcc hello_win.c + + For the 'Hello DLL' example type + + tcc -shared dll.c + tcc -impdef dll.dll (optional) + tcc hello_dll.c dll.def + + + Using libtcc as JIT compiler in your program + -------------------------------------------- + Check out the 'libtcc_test' example: + + - Running it from source: + tcc -I libtcc libtcc/libtcc.def -run examples/libtcc_test.c + + - Compiling with TCC: + tcc examples/libtcc_test.c -I libtcc libtcc/libtcc.def + + - Compiling with MinGW: + gcc examples/libtcc_test.c -I libtcc libtcc.dll -o libtcc_test.exe + + - Compiling with MSVC: + lib /def:libtcc\libtcc.def /out:libtcc.lib + cl /MD examples/libtcc_test.c -I libtcc libtcc.lib + + + Import Definition Files: + ------------------------ + To link with Windows system DLLs, TCC uses import definition + files (.def) instead of libraries. + + The now built-in 'tiny_impdef' program may be used to make + additional .def files for any DLL. For example + + tcc -impdef [-v] opengl32.dll [-o opengl32.def] + + Put opengl32.def into the tcc/lib directory. Specify -lopengl32 at + the TCC commandline to link a program that uses opengl32.dll. + + + Header Files: + ------------- + The system header files (except _mingw.h) are from the MinGW + distribution: + + http://www.mingw.org/ + + From the windows headers, only a minimal set is included. If you need + more, get MinGW's "w32api" package. Extract the files from "include" + into your "tcc/include/winapi" directory. + + + Resource Files: + --------------- + TCC can link windows resources in coff format as generated by MinGW's + windres.exe. For example: + + windres -O coff app.rc -o appres.o + tcc app.c appres.o -o app.exe + + + Tiny Libmaker: + -------------- + The now built-in tiny_libmaker tool by Timovj Lahde can be used as + 'ar' replacement to make a library from several object files: + + tcc -ar [rcsv] library objectfiles ... + + + Compilation from source: + ------------------------ + * You can use the MinGW and MSYS tools available at + http://www.mingw.org + http://www.mingw-w64.org + http://www.msys2.org + + Untar the TCC archive and type in the MSYS shell: + ./configure [--prefix installpath] + make + make install + + The default install location is c:\Program Files\tcc + + Cygwin can be used too with its mingw cross-compiler installed: + ./configure --cross-prefix=i686-w64-mingw32- + (the prefix may vary) + + * Alternatively you can compile TCC with just GCC from MinGW using + > build-tcc.bat (from the win32 directory) + + Also MSVC can be used with the "VSTools Developer Command Prompt": + > build-tcc.bat -c cl + + or with an existing tcc (needs to be in a different directory) + > build-tcc.bat -c some-tcc-dir\tcc.exe + + Also you can copy/install everything into another directory: + > build-tcc.bat -i + + Limitations: + ------------ + - On the object file level, currently TCC supports only the ELF format, + not COFF as used by MinGW and MSVC. It is not possible to exchange + object files or libraries between TCC and these compilers. + + However libraries for TCC from objects by TCC can be made using + tcc -ar lib.a files.o ,,, + + - No leading underscore is generated in the ELF symbols. + + Documentation and License: + -------------------------- + TCC is distributed under the GNU Lesser General Public License. (See + COPYING file or http://www.gnu.org/licenses/lgpl-2.1.html) + + TinyCC homepage is at: + + http://fabrice.bellard.free.fr/tcc/ + + + WinAPI Help and 3rd-party tools: + -------------------------------- + The Windows API documentation (Win95) in a single .hlp file is + available on the lcc-win32 site as "win32hlp.exe" or from other + locations as "win32hlp_big.zip". + + A nice RAD tool to create windows resources (dialog boxes etc.) is + "ResEd", available at the RadASM website. + + + --- grischka diff --git a/05/tcc-final-old/x86_64-asm.h b/05/tcc-final-old/x86_64-asm.h new file mode 100644 index 0000000..cb9eb16 --- /dev/null +++ b/05/tcc-final-old/x86_64-asm.h @@ -0,0 +1,525 @@ + DEF_ASM_OP0(clc, 0xf8) /* must be first OP0 */ + DEF_ASM_OP0(cld, 0xfc) + DEF_ASM_OP0(cli, 0xfa) + DEF_ASM_OP0(clts, 0x0f06) + DEF_ASM_OP0(cmc, 0xf5) + DEF_ASM_OP0(lahf, 0x9f) + DEF_ASM_OP0(sahf, 0x9e) + DEF_ASM_OP0(pushfq, 0x9c) + DEF_ASM_OP0(popfq, 0x9d) + DEF_ASM_OP0(pushf, 0x9c) + DEF_ASM_OP0(popf, 0x9d) + DEF_ASM_OP0(stc, 0xf9) + DEF_ASM_OP0(std, 0xfd) + DEF_ASM_OP0(sti, 0xfb) + DEF_ASM_OP0(aaa, 0x37) + DEF_ASM_OP0(aas, 0x3f) + DEF_ASM_OP0(daa, 0x27) + DEF_ASM_OP0(das, 0x2f) + DEF_ASM_OP0(aad, 0xd50a) + DEF_ASM_OP0(aam, 0xd40a) + DEF_ASM_OP0(cbw, 0x6698) + DEF_ASM_OP0(cwd, 0x6699) + DEF_ASM_OP0(cwde, 0x98) + DEF_ASM_OP0(cdq, 0x99) + DEF_ASM_OP0(cbtw, 0x6698) + DEF_ASM_OP0(cwtl, 0x98) + DEF_ASM_OP0(cwtd, 0x6699) + DEF_ASM_OP0(cltd, 0x99) + DEF_ASM_OP0(cqto, 0x4899) + DEF_ASM_OP0(int3, 0xcc) + DEF_ASM_OP0(into, 0xce) + DEF_ASM_OP0(iret, 0xcf) + DEF_ASM_OP0(rsm, 0x0faa) + DEF_ASM_OP0(hlt, 0xf4) + DEF_ASM_OP0(wait, 0x9b) + DEF_ASM_OP0(nop, 0x90) + DEF_ASM_OP0(pause, 0xf390) + DEF_ASM_OP0(xlat, 0xd7) + + /* strings */ +ALT(DEF_ASM_OP0L(cmpsb, 0xa6, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(scmpb, 0xa6, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(insb, 0x6c, 0, OPC_BWL)) +ALT(DEF_ASM_OP0L(outsb, 0x6e, 0, OPC_BWL)) + +ALT(DEF_ASM_OP0L(lodsb, 0xac, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(slodb, 0xac, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(movsb, 0xa4, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(smovb, 0xa4, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(scasb, 0xae, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(sscab, 0xae, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(stosb, 0xaa, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(sstob, 0xaa, 0, OPC_BWLX)) + + /* bits */ + +ALT(DEF_ASM_OP2(bsfw, 0x0fbc, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(bsrw, 0x0fbd, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) + +ALT(DEF_ASM_OP2(btw, 0x0fa3, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btw, 0x0fba, 4, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btsw, 0x0fab, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btsw, 0x0fba, 5, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btrw, 0x0fb3, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btrw, 0x0fba, 6, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btcw, 0x0fbb, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btcw, 0x0fba, 7, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + + /* prefixes */ + DEF_ASM_OP0(lock, 0xf0) + DEF_ASM_OP0(rep, 0xf3) + DEF_ASM_OP0(repe, 0xf3) + DEF_ASM_OP0(repz, 0xf3) + DEF_ASM_OP0(repne, 0xf2) + DEF_ASM_OP0(repnz, 0xf2) + + DEF_ASM_OP0(invd, 0x0f08) + DEF_ASM_OP0(wbinvd, 0x0f09) + DEF_ASM_OP0(cpuid, 0x0fa2) + DEF_ASM_OP0(wrmsr, 0x0f30) + DEF_ASM_OP0(rdtsc, 0x0f31) + DEF_ASM_OP0(rdmsr, 0x0f32) + DEF_ASM_OP0(rdpmc, 0x0f33) + + DEF_ASM_OP0(syscall, 0x0f05) + DEF_ASM_OP0(sysret, 0x0f07) + DEF_ASM_OP0L(sysretq, 0x480f07, 0, 0) + DEF_ASM_OP0(ud2, 0x0f0b) + + /* NOTE: we took the same order as gas opcode definition order */ +/* Right now we can't express the fact that 0xa1/0xa3 can't use $eax and a + 32 bit moffset as operands. +ALT(DEF_ASM_OP2(movb, 0xa0, 0, OPC_BWLX, OPT_ADDR, OPT_EAX)) +ALT(DEF_ASM_OP2(movb, 0xa2, 0, OPC_BWLX, OPT_EAX, OPT_ADDR)) */ +ALT(DEF_ASM_OP2(movb, 0x88, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0x8a, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +/* The moves are special: the 0xb8 form supports IM64 (the only insn that + does) with REG64. It doesn't support IM32 with REG64, it would use + the full movabs form (64bit immediate). For IM32->REG64 we prefer + the 0xc7 opcode. So disallow all 64bit forms and code the rest by hand. */ +ALT(DEF_ASM_OP2(movb, 0xb0, 0, OPC_REG | OPC_BWLX, OPT_IM, OPT_REG)) +ALT(DEF_ASM_OP2(mov, 0xb8, 0, OPC_REG, OPT_IM64, OPT_REG64)) +ALT(DEF_ASM_OP2(movq, 0xb8, 0, OPC_REG, OPT_IM64, OPT_REG64)) +ALT(DEF_ASM_OP2(movb, 0xc6, 0, OPC_MODRM | OPC_BWLX, OPT_IM, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP2(movw, 0x8c, 0, OPC_MODRM | OPC_WLX, OPT_SEG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(movw, 0x8e, 0, OPC_MODRM | OPC_WLX, OPT_EA | OPT_REG, OPT_SEG)) + +ALT(DEF_ASM_OP2(movw, 0x0f20, 0, OPC_MODRM | OPC_WLX, OPT_CR, OPT_REG64)) +ALT(DEF_ASM_OP2(movw, 0x0f21, 0, OPC_MODRM | OPC_WLX, OPT_DB, OPT_REG64)) +ALT(DEF_ASM_OP2(movw, 0x0f22, 0, OPC_MODRM | OPC_WLX, OPT_REG64, OPT_CR)) +ALT(DEF_ASM_OP2(movw, 0x0f23, 0, OPC_MODRM | OPC_WLX, OPT_REG64, OPT_DB)) + +ALT(DEF_ASM_OP2(movsbw, 0x660fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG16)) +ALT(DEF_ASM_OP2(movsbl, 0x0fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movsbq, 0x0fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(movswl, 0x0fbf, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movswq, 0x0fbf, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG)) +ALT(DEF_ASM_OP2(movslq, 0x63, 0, OPC_MODRM, OPT_REG32 | OPT_EA, OPT_REG)) +ALT(DEF_ASM_OP2(movzbw, 0x0fb6, 0, OPC_MODRM | OPC_WLX, OPT_REG8 | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(movzwl, 0x0fb7, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movzwq, 0x0fb7, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG)) + +ALT(DEF_ASM_OP1(pushq, 0x6a, 0, 0, OPT_IM8S)) +ALT(DEF_ASM_OP1(push, 0x6a, 0, 0, OPT_IM8S)) +ALT(DEF_ASM_OP1(pushw, 0x666a, 0, 0, OPT_IM8S)) +ALT(DEF_ASM_OP1(pushw, 0x50, 0, OPC_REG | OPC_WLX, OPT_REG64)) +ALT(DEF_ASM_OP1(pushw, 0x50, 0, OPC_REG | OPC_WLX, OPT_REG16)) +ALT(DEF_ASM_OP1(pushw, 0xff, 6, OPC_MODRM | OPC_WLX, OPT_REG64 | OPT_EA)) +ALT(DEF_ASM_OP1(pushw, 0x6668, 0, 0, OPT_IM16)) +ALT(DEF_ASM_OP1(pushw, 0x68, 0, OPC_WLX, OPT_IM32)) +ALT(DEF_ASM_OP1(pushw, 0x06, 0, OPC_WLX, OPT_SEG)) + +ALT(DEF_ASM_OP1(popw, 0x58, 0, OPC_REG | OPC_WLX, OPT_REG64)) +ALT(DEF_ASM_OP1(popw, 0x58, 0, OPC_REG | OPC_WLX, OPT_REG16)) +ALT(DEF_ASM_OP1(popw, 0x8f, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP1(popw, 0x07, 0, OPC_WLX, OPT_SEG)) + +ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WLX, OPT_REGW, OPT_EAX)) +ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WLX, OPT_EAX, OPT_REGW)) +ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) + +ALT(DEF_ASM_OP2(inb, 0xe4, 0, OPC_BWL, OPT_IM8, OPT_EAX)) +ALT(DEF_ASM_OP1(inb, 0xe4, 0, OPC_BWL, OPT_IM8)) +ALT(DEF_ASM_OP2(inb, 0xec, 0, OPC_BWL, OPT_DX, OPT_EAX)) +ALT(DEF_ASM_OP1(inb, 0xec, 0, OPC_BWL, OPT_DX)) + +ALT(DEF_ASM_OP2(outb, 0xe6, 0, OPC_BWL, OPT_EAX, OPT_IM8)) +ALT(DEF_ASM_OP1(outb, 0xe6, 0, OPC_BWL, OPT_IM8)) +ALT(DEF_ASM_OP2(outb, 0xee, 0, OPC_BWL, OPT_EAX, OPT_DX)) +ALT(DEF_ASM_OP1(outb, 0xee, 0, OPC_BWL, OPT_DX)) + +ALT(DEF_ASM_OP2(leaw, 0x8d, 0, OPC_MODRM | OPC_WLX, OPT_EA, OPT_REG)) + +ALT(DEF_ASM_OP2(les, 0xc4, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lds, 0xc5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lss, 0x0fb2, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lfs, 0x0fb4, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lgs, 0x0fb5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) + + /* arith */ +ALT(DEF_ASM_OP2(addb, 0x00, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) /* XXX: use D bit ? */ +ALT(DEF_ASM_OP2(addb, 0x02, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(addb, 0x04, 0, OPC_ARITH | OPC_BWLX, OPT_IM, OPT_EAX)) +ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(addb, 0x80, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(testb, 0xa8, 0, OPC_BWLX, OPT_IM, OPT_EAX)) +ALT(DEF_ASM_OP2(testb, 0xf6, 0, OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP1(incb, 0xfe, 0, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(decb, 0xfe, 1, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP1(notb, 0xf6, 2, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(negb, 0xf6, 3, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP1(mulb, 0xf6, 4, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(imulb, 0xf6, 5, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP2(imulw, 0x0faf, 0, OPC_MODRM | OPC_WLX, OPT_REG | OPT_EA, OPT_REG)) +ALT(DEF_ASM_OP3(imulw, 0x6b, 0, OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(imulw, 0x6b, 0, OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_REGW)) +ALT(DEF_ASM_OP3(imulw, 0x69, 0, OPC_MODRM | OPC_WLX, OPT_IMW, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(imulw, 0x69, 0, OPC_MODRM | OPC_WLX, OPT_IMW, OPT_REGW)) + +ALT(DEF_ASM_OP1(divb, 0xf6, 6, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP2(divb, 0xf6, 6, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA, OPT_EAX)) +ALT(DEF_ASM_OP1(idivb, 0xf6, 7, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP2(idivb, 0xf6, 7, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA, OPT_EAX)) + + /* shifts */ +ALT(DEF_ASM_OP2(rolb, 0xc0, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_IM8, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(rolb, 0xd2, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_CL, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP1(rolb, 0xd0, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP3(shldw, 0x0fa4, 0, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WLX, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shrdw, 0x0fac, 0, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WLX, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_EA | OPT_REGW)) + +ALT(DEF_ASM_OP1(call, 0xff, 2, OPC_MODRM, OPT_INDIR)) +ALT(DEF_ASM_OP1(call, 0xe8, 0, 0, OPT_DISP)) +ALT(DEF_ASM_OP1(jmp, 0xff, 4, OPC_MODRM, OPT_INDIR)) +ALT(DEF_ASM_OP1(jmp, 0xeb, 0, 0, OPT_DISP8)) + +ALT(DEF_ASM_OP1(lcall, 0xff, 3, OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(ljmp, 0xff, 5, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(ljmpw, 0x66ff, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(ljmpl, 0xff, 5, OPC_MODRM, OPT_EA) + +ALT(DEF_ASM_OP1(int, 0xcd, 0, 0, OPT_IM8)) +ALT(DEF_ASM_OP1(seto, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA)) +ALT(DEF_ASM_OP1(setob, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA)) + DEF_ASM_OP2(enter, 0xc8, 0, 0, OPT_IM16, OPT_IM8) + DEF_ASM_OP0(leave, 0xc9) + DEF_ASM_OP0(ret, 0xc3) + DEF_ASM_OP0(retq, 0xc3) +ALT(DEF_ASM_OP1(retq, 0xc2, 0, 0, OPT_IM16)) +ALT(DEF_ASM_OP1(ret, 0xc2, 0, 0, OPT_IM16)) + DEF_ASM_OP0(lret, 0xcb) +ALT(DEF_ASM_OP1(lret, 0xca, 0, 0, OPT_IM16)) + +ALT(DEF_ASM_OP1(jo, 0x70, 0, OPC_TEST, OPT_DISP8)) + DEF_ASM_OP1(loopne, 0xe0, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loopnz, 0xe0, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loope, 0xe1, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loopz, 0xe1, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loop, 0xe2, 0, 0, OPT_DISP8) + DEF_ASM_OP1(jecxz, 0x67e3, 0, 0, OPT_DISP8) + + /* float */ + /* specific fcomp handling */ +ALT(DEF_ASM_OP0L(fcomp, 0xd8d9, 0, 0)) + +ALT(DEF_ASM_OP1(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST)) +ALT(DEF_ASM_OP2(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0)) +ALT(DEF_ASM_OP2(fadd, 0xdcc0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP2(fmul, 0xdcc8, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP0L(fadd, 0xdec1, 0, OPC_FARITH)) +ALT(DEF_ASM_OP1(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST)) +ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0)) +ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP0L(faddp, 0xdec1, 0, OPC_FARITH)) +ALT(DEF_ASM_OP1(fadds, 0xd8, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(fiaddl, 0xda, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(faddl, 0xdc, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(fiadds, 0xde, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) + + DEF_ASM_OP0(fucompp, 0xdae9) + DEF_ASM_OP0(ftst, 0xd9e4) + DEF_ASM_OP0(fxam, 0xd9e5) + DEF_ASM_OP0(fld1, 0xd9e8) + DEF_ASM_OP0(fldl2t, 0xd9e9) + DEF_ASM_OP0(fldl2e, 0xd9ea) + DEF_ASM_OP0(fldpi, 0xd9eb) + DEF_ASM_OP0(fldlg2, 0xd9ec) + DEF_ASM_OP0(fldln2, 0xd9ed) + DEF_ASM_OP0(fldz, 0xd9ee) + + DEF_ASM_OP0(f2xm1, 0xd9f0) + DEF_ASM_OP0(fyl2x, 0xd9f1) + DEF_ASM_OP0(fptan, 0xd9f2) + DEF_ASM_OP0(fpatan, 0xd9f3) + DEF_ASM_OP0(fxtract, 0xd9f4) + DEF_ASM_OP0(fprem1, 0xd9f5) + DEF_ASM_OP0(fdecstp, 0xd9f6) + DEF_ASM_OP0(fincstp, 0xd9f7) + DEF_ASM_OP0(fprem, 0xd9f8) + DEF_ASM_OP0(fyl2xp1, 0xd9f9) + DEF_ASM_OP0(fsqrt, 0xd9fa) + DEF_ASM_OP0(fsincos, 0xd9fb) + DEF_ASM_OP0(frndint, 0xd9fc) + DEF_ASM_OP0(fscale, 0xd9fd) + DEF_ASM_OP0(fsin, 0xd9fe) + DEF_ASM_OP0(fcos, 0xd9ff) + DEF_ASM_OP0(fchs, 0xd9e0) + DEF_ASM_OP0(fabs, 0xd9e1) + DEF_ASM_OP0(fninit, 0xdbe3) + DEF_ASM_OP0(fnclex, 0xdbe2) + DEF_ASM_OP0(fnop, 0xd9d0) + DEF_ASM_OP0(fwait, 0x9b) + + /* fp load */ + DEF_ASM_OP1(fld, 0xd9c0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fldl, 0xd9c0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(flds, 0xd9, 0, OPC_MODRM, OPT_EA) +ALT(DEF_ASM_OP1(fldl, 0xdd, 0, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(fildl, 0xdb, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fildq, 0xdf, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fildll, 0xdf, 5, OPC_MODRM,OPT_EA) + DEF_ASM_OP1(fldt, 0xdb, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fbld, 0xdf, 4, OPC_MODRM, OPT_EA) + + /* fp store */ + DEF_ASM_OP1(fst, 0xddd0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fstl, 0xddd0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fsts, 0xd9, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fstps, 0xd9, 3, OPC_MODRM, OPT_EA) +ALT(DEF_ASM_OP1(fstl, 0xdd, 2, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(fstpl, 0xdd, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fist, 0xdf, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistp, 0xdf, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistl, 0xdb, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistpl, 0xdb, 3, OPC_MODRM, OPT_EA) + + DEF_ASM_OP1(fstp, 0xddd8, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fistpq, 0xdf, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistpll, 0xdf, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fstpt, 0xdb, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fbstp, 0xdf, 6, OPC_MODRM, OPT_EA) + + /* exchange */ + DEF_ASM_OP0(fxch, 0xd9c9) +ALT(DEF_ASM_OP1(fxch, 0xd9c8, 0, OPC_REG, OPT_ST)) + + /* misc FPU */ + DEF_ASM_OP1(fucom, 0xdde0, 0, OPC_REG, OPT_ST ) + DEF_ASM_OP1(fucomp, 0xdde8, 0, OPC_REG, OPT_ST ) + + DEF_ASM_OP0L(finit, 0xdbe3, 0, OPC_FWAIT) + DEF_ASM_OP1(fldcw, 0xd9, 5, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fnstcw, 0xd9, 7, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fstcw, 0xd9, 7, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP0(fnstsw, 0xdfe0) +ALT(DEF_ASM_OP1(fnstsw, 0xdfe0, 0, 0, OPT_EAX )) +ALT(DEF_ASM_OP1(fnstsw, 0xdd, 7, OPC_MODRM, OPT_EA )) + DEF_ASM_OP1(fstsw, 0xdfe0, 0, OPC_FWAIT, OPT_EAX ) +ALT(DEF_ASM_OP0L(fstsw, 0xdfe0, 0, OPC_FWAIT)) +ALT(DEF_ASM_OP1(fstsw, 0xdd, 7, OPC_MODRM | OPC_FWAIT, OPT_EA )) + DEF_ASM_OP0L(fclex, 0xdbe2, 0, OPC_FWAIT) + DEF_ASM_OP1(fnstenv, 0xd9, 6, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fstenv, 0xd9, 6, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP1(fldenv, 0xd9, 4, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fnsave, 0xdd, 6, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fsave, 0xdd, 6, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP1(frstor, 0xdd, 4, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(ffree, 0xddc0, 4, OPC_REG, OPT_ST ) + DEF_ASM_OP1(ffreep, 0xdfc0, 4, OPC_REG, OPT_ST ) + DEF_ASM_OP1(fxsave, 0x0fae, 0, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fxrstor, 0x0fae, 1, OPC_MODRM, OPT_EA ) + /* The *q forms of fxrstor/fxsave use a REX prefix. + If the operand would use extended registers we would have to modify + it instead of generating a second one. Currently that's no + problem with TCC, we don't use extended registers. */ + DEF_ASM_OP1(fxsaveq, 0x0fae, 0, OPC_MODRM | OPC_48, OPT_EA ) + DEF_ASM_OP1(fxrstorq, 0x0fae, 1, OPC_MODRM | OPC_48, OPT_EA ) + + /* segments */ + DEF_ASM_OP2(arpl, 0x63, 0, OPC_MODRM, OPT_REG16, OPT_REG16 | OPT_EA) +ALT(DEF_ASM_OP2(larw, 0x0f02, 0, OPC_MODRM | OPC_WLX, OPT_REG | OPT_EA, OPT_REG)) + DEF_ASM_OP1(lgdt, 0x0f01, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lgdtq, 0x0f01, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lidt, 0x0f01, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lidtq, 0x0f01, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lldt, 0x0f00, 2, OPC_MODRM, OPT_EA | OPT_REG) + DEF_ASM_OP1(lmsw, 0x0f01, 6, OPC_MODRM, OPT_EA | OPT_REG) +ALT(DEF_ASM_OP2(lslw, 0x0f03, 0, OPC_MODRM | OPC_WLX, OPT_EA | OPT_REG, OPT_REG)) + DEF_ASM_OP1(ltr, 0x0f00, 3, OPC_MODRM, OPT_EA | OPT_REG16) + DEF_ASM_OP1(sgdt, 0x0f01, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sgdtq, 0x0f01, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sidt, 0x0f01, 1, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sidtq, 0x0f01, 1, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sldt, 0x0f00, 0, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(smsw, 0x0f01, 4, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(str, 0x0f00, 1, OPC_MODRM, OPT_REG32 | OPT_EA) +ALT(DEF_ASM_OP1(str, 0x660f00, 1, OPC_MODRM, OPT_REG16)) +ALT(DEF_ASM_OP1(str, 0x0f00, 1, OPC_MODRM | OPC_48, OPT_REG64)) + DEF_ASM_OP1(verr, 0x0f00, 4, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(verw, 0x0f00, 5, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP0L(swapgs, 0x0f01, 7, OPC_MODRM) + + /* 486 */ + /* bswap can't be applied to 16bit regs */ + DEF_ASM_OP1(bswap, 0x0fc8, 0, OPC_REG, OPT_REG32 ) + DEF_ASM_OP1(bswapl, 0x0fc8, 0, OPC_REG, OPT_REG32 ) + DEF_ASM_OP1(bswapq, 0x0fc8, 0, OPC_REG | OPC_48, OPT_REG64 ) + +ALT(DEF_ASM_OP2(xaddb, 0x0fc0, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_REG | OPT_EA )) +ALT(DEF_ASM_OP2(cmpxchgb, 0x0fb0, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_REG | OPT_EA )) + DEF_ASM_OP1(invlpg, 0x0f01, 7, OPC_MODRM, OPT_EA ) + + /* pentium */ + DEF_ASM_OP1(cmpxchg8b, 0x0fc7, 1, OPC_MODRM, OPT_EA ) + + /* AMD 64 */ + DEF_ASM_OP1(cmpxchg16b, 0x0fc7, 1, OPC_MODRM | OPC_48, OPT_EA ) + + /* pentium pro */ +ALT(DEF_ASM_OP2(cmovo, 0x0f40, 0, OPC_MODRM | OPC_TEST | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) + + DEF_ASM_OP2(fcmovb, 0xdac0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmove, 0xdac8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovbe, 0xdad0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovu, 0xdad8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnb, 0xdbc0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovne, 0xdbc8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnbe, 0xdbd0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnu, 0xdbd8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + + DEF_ASM_OP2(fucomi, 0xdbe8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcomi, 0xdbf0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fucomip, 0xdfe8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcomip, 0xdff0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + + /* mmx */ + DEF_ASM_OP0(emms, 0x0f77) /* must be last OP0 */ + DEF_ASM_OP2(movd, 0x0f6e, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_MMXSSE ) + /* movd shouldn't accept REG64, but AMD64 spec uses it for 32 and 64 bit + moves, so let's be compatible. */ +ALT(DEF_ASM_OP2(movd, 0x0f6e, 0, OPC_MODRM, OPT_EA | OPT_REG64, OPT_MMXSSE )) +ALT(DEF_ASM_OP2(movq, 0x0f6e, 0, OPC_MODRM | OPC_48, OPT_REG64, OPT_MMXSSE )) +ALT(DEF_ASM_OP2(movq, 0x0f6f, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )) +ALT(DEF_ASM_OP2(movd, 0x0f7e, 0, OPC_MODRM, OPT_MMXSSE, OPT_EA | OPT_REG32 )) +ALT(DEF_ASM_OP2(movd, 0x0f7e, 0, OPC_MODRM, OPT_MMXSSE, OPT_EA | OPT_REG64 )) +ALT(DEF_ASM_OP2(movq, 0x0f7f, 0, OPC_MODRM, OPT_MMX, OPT_EA | OPT_MMX )) +ALT(DEF_ASM_OP2(movq, 0x660fd6, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_SSE )) +ALT(DEF_ASM_OP2(movq, 0xf30f7e, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE )) +ALT(DEF_ASM_OP2(movq, 0x0f7e, 0, OPC_MODRM, OPT_MMXSSE, OPT_EA | OPT_REG64 )) + + DEF_ASM_OP2(packssdw, 0x0f6b, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(packsswb, 0x0f63, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(packuswb, 0x0f67, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddb, 0x0ffc, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddw, 0x0ffd, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddd, 0x0ffe, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddsb, 0x0fec, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddsw, 0x0fed, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddusb, 0x0fdc, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddusw, 0x0fdd, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pand, 0x0fdb, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pandn, 0x0fdf, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqb, 0x0f74, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqw, 0x0f75, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqd, 0x0f76, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtb, 0x0f64, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtw, 0x0f65, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtd, 0x0f66, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmaddwd, 0x0ff5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmulhw, 0x0fe5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmullw, 0x0fd5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(por, 0x0feb, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psllw, 0x0ff1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psllw, 0x0f71, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(pslld, 0x0ff2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(pslld, 0x0f72, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psllq, 0x0ff3, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psllq, 0x0f73, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psraw, 0x0fe1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psraw, 0x0f71, 4, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrad, 0x0fe2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrad, 0x0f72, 4, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrlw, 0x0fd1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrlw, 0x0f71, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrld, 0x0fd2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrld, 0x0f72, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrlq, 0x0fd3, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psubb, 0x0ff8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubw, 0x0ff9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubd, 0x0ffa, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubsb, 0x0fe8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubsw, 0x0fe9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubusb, 0x0fd8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubusw, 0x0fd9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhbw, 0x0f68, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhwd, 0x0f69, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhdq, 0x0f6a, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpcklbw, 0x0f60, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpcklwd, 0x0f61, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckldq, 0x0f62, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + + /* sse */ + DEF_ASM_OP2(movups, 0x0f10, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movups, 0x0f11, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(movaps, 0x0f28, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movaps, 0x0f29, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(movhps, 0x0f16, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movhps, 0x0f17, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(addps, 0x0f58, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(cvtpi2ps, 0x0f2a, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_SSE ) + DEF_ASM_OP2(cvtps2pi, 0x0f2d, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_MMX ) + DEF_ASM_OP2(cvttps2pi, 0x0f2c, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_MMX ) + DEF_ASM_OP2(divps, 0x0f5e, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(maxps, 0x0f5f, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(minps, 0x0f5d, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(mulps, 0x0f59, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pavgb, 0x0fe0, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pavgw, 0x0fe3, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pmaxsw, 0x0fee, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmaxub, 0x0fde, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pminsw, 0x0fea, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pminub, 0x0fda, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(rcpss, 0x0f53, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(rsqrtps, 0x0f52, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(sqrtps, 0x0f51, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(subps, 0x0f5c, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + + DEF_ASM_OP1(prefetchnta, 0x0f18, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(prefetcht0, 0x0f18, 1, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(prefetcht1, 0x0f18, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(prefetcht2, 0x0f18, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(prefetchw, 0x0f0d, 1, OPC_MODRM, OPT_EA) + DEF_ASM_OP0L(lfence, 0x0fae, 5, OPC_MODRM) + DEF_ASM_OP0L(mfence, 0x0fae, 6, OPC_MODRM) + DEF_ASM_OP0L(sfence, 0x0fae, 7, OPC_MODRM) + DEF_ASM_OP1(clflush, 0x0fae, 7, OPC_MODRM, OPT_EA) +#undef ALT +#undef DEF_ASM_OP0 +#undef DEF_ASM_OP0L +#undef DEF_ASM_OP1 +#undef DEF_ASM_OP2 +#undef DEF_ASM_OP3 diff --git a/05/tcc-final-old/x86_64-gen.c b/05/tcc-final-old/x86_64-gen.c new file mode 100644 index 0000000..e33a38a --- /dev/null +++ b/05/tcc-final-old/x86_64-gen.c @@ -0,0 +1,2258 @@ +/* + * x86-64 code generator for TCC + * + * Copyright (c) 2008 Shinichiro Hamaji + * + * Based on i386-gen.c by Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +/* number of available registers */ +#define NB_REGS 25 +#define NB_ASM_REGS 16 +#define CONFIG_TCC_ASM + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_RAX 0x0004 +#define RC_RCX 0x0008 +#define RC_RDX 0x0010 +#define RC_ST0 0x0080 /* only for long double */ +#define RC_R8 0x0100 +#define RC_R9 0x0200 +#define RC_R10 0x0400 +#define RC_R11 0x0800 +#define RC_XMM0 0x1000 +#define RC_XMM1 0x2000 +#define RC_XMM2 0x4000 +#define RC_XMM3 0x8000 +#define RC_XMM4 0x10000 +#define RC_XMM5 0x20000 +#define RC_XMM6 0x40000 +#define RC_XMM7 0x80000 +#define RC_IRET RC_RAX /* function return: integer register */ +#define RC_LRET RC_RDX /* function return: second integer register */ +#define RC_FRET RC_XMM0 /* function return: float register */ +#define RC_QRET RC_XMM1 /* function return: second float register */ + +/* pretty names for the registers */ +enum { + TREG_RAX = 0, + TREG_RCX = 1, + TREG_RDX = 2, + TREG_RSP = 4, + TREG_RSI = 6, + TREG_RDI = 7, + + TREG_R8 = 8, + TREG_R9 = 9, + TREG_R10 = 10, + TREG_R11 = 11, + + TREG_XMM0 = 16, + TREG_XMM1 = 17, + TREG_XMM2 = 18, + TREG_XMM3 = 19, + TREG_XMM4 = 20, + TREG_XMM5 = 21, + TREG_XMM6 = 22, + TREG_XMM7 = 23, + + TREG_ST0 = 24, + + TREG_MEM = 0x20 +}; + +#define REX_BASE(reg) (((reg) >> 3) & 1) +#define REG_VALUE(reg) ((reg) & 7) + +/* return registers for function */ +#define REG_IRET TREG_RAX /* single word int return register */ +#define REG_LRET TREG_RDX /* second word return register (for long long) */ +#define REG_FRET TREG_XMM0 /* float return register */ +#define REG_QRET TREG_XMM1 /* second float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +#define INVERT_FUNC_PARAMS + +/* pointer size, in bytes */ +#define PTR_SIZE 8 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 16 +#define LDOUBLE_ALIGN 16 +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 16 + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#include "tcc.h" +#include + +ST_DATA const int reg_classes[NB_REGS] = { + /* eax */ RC_INT | RC_RAX, + /* ecx */ RC_INT | RC_RCX, + /* edx */ RC_INT | RC_RDX, + 0, + 0, + 0, + 0, + 0, + RC_R8, + RC_R9, + RC_R10, + RC_R11, + 0, + 0, + 0, + 0, + /* xmm0 */ RC_FLOAT | RC_XMM0, + /* xmm1 */ RC_FLOAT | RC_XMM1, + /* xmm2 */ RC_FLOAT | RC_XMM2, + /* xmm3 */ RC_FLOAT | RC_XMM3, + /* xmm4 */ RC_FLOAT | RC_XMM4, + /* xmm5 */ RC_FLOAT | RC_XMM5, + /* xmm6 an xmm7 are included so gv() can be used on them, + but they are not tagged with RC_FLOAT because they are + callee saved on Windows */ + RC_XMM6, + RC_XMM7, + /* st0 */ RC_ST0 +}; + +static unsigned long func_sub_sp_offset; +static int func_ret_sub; + +/* XXX: make it faster ? */ +ST_FUNC void g(int c) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 1; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; + ind = ind1; +} + +ST_FUNC void o(unsigned int c) +{ + while (c) { + g(c); + c = c >> 8; + } +} + +ST_FUNC void gen_le16(int v) +{ + g(v); + g(v >> 8); +} + +ST_FUNC void gen_le32(int c) +{ + g(c); + g(c >> 8); + g(c >> 16); + g(c >> 24); +} + +ST_FUNC void gen_le64(int64_t c) +{ + g(c); + g(c >> 8); + g(c >> 16); + g(c >> 24); + g(c >> 32); + g(c >> 40); + g(c >> 48); + g(c >> 56); +} + +static void orex(int ll, int r, int r2, int b) +{ + if ((r & VT_VALMASK) >= VT_CONST) + r = 0; + if ((r2 & VT_VALMASK) >= VT_CONST) + r2 = 0; + if (ll || REX_BASE(r) || REX_BASE(r2)) + o(0x40 | REX_BASE(r) | (REX_BASE(r2) << 2) | (ll << 3)); + o(b); +} + +/* output a symbol and patch all calls to it */ +ST_FUNC void gsym_addr(int t, int a) +{ + while (t) { + unsigned char *ptr = cur_text_section->data + t; + uint32_t n = read32le(ptr); /* next value */ + write32le(ptr, a - t - 4); + t = n; + } +} + +void gsym(int t) +{ + gsym_addr(t, ind); +} + + +static int is64_type(int t) +{ + return ((t & VT_BTYPE) == VT_PTR || + (t & VT_BTYPE) == VT_FUNC || + (t & VT_BTYPE) == VT_LLONG); +} + +/* instruction + 4 bytes data. Return the address of the data */ +static int oad(int c, int s) +{ + int t; + if (nocode_wanted) + return s; + o(c); + t = ind; + gen_le32(s); + return t; +} + +/* generate jmp to a label */ +#define gjmp2(instr,lbl) oad(instr,lbl) + +ST_FUNC void gen_addr32(int r, Sym *sym, int c) +{ + if (r & VT_SYM) + greloca(cur_text_section, sym, ind, R_X86_64_32S, c), c=0; + gen_le32(c); +} + +/* output constant with relocation if 'r & VT_SYM' is true */ +ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c) +{ + if (r & VT_SYM) + greloca(cur_text_section, sym, ind, R_X86_64_64, c), c=0; + gen_le64(c); +} + +/* output constant with relocation if 'r & VT_SYM' is true */ +ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) +{ + if (r & VT_SYM) + greloca(cur_text_section, sym, ind, R_X86_64_PC32, c-4), c=4; + gen_le32(c-4); +} + +/* output got address with relocation */ +static void gen_gotpcrel(int r, Sym *sym, int c) +{ +#ifdef TCC_TARGET_PE + tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n", + get_tok_str(sym->v, NULL), c, r, + cur_text_section->data[ind-3], + cur_text_section->data[ind-2], + cur_text_section->data[ind-1] + ); +#endif + greloca(cur_text_section, sym, ind, R_X86_64_GOTPCREL, -4); + gen_le32(0); + if (c) { + /* we use add c, %xxx for displacement */ + orex(1, r, 0, 0x81); + o(0xc0 + REG_VALUE(r)); + gen_le32(c); + } +} + +static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got) +{ + op_reg = REG_VALUE(op_reg) << 3; + if ((r & VT_VALMASK) == VT_CONST) { + /* constant memory reference */ + if (!(r & VT_SYM)) { + /* Absolute memory reference */ + o(0x04 | op_reg); /* [sib] | destreg */ + oad(0x25, c); /* disp32 */ + } else { + o(0x05 | op_reg); /* (%rip)+disp32 | destreg */ + if (is_got) { + gen_gotpcrel(r, sym, c); + } else { + gen_addrpc32(r, sym, c); + } + } + } else if ((r & VT_VALMASK) == VT_LOCAL) { + /* currently, we use only ebp as base */ + if (c == (char)c) { + /* short reference */ + o(0x45 | op_reg); + g(c); + } else { + oad(0x85 | op_reg, c); + } + } else if ((r & VT_VALMASK) >= TREG_MEM) { + if (c) { + g(0x80 | op_reg | REG_VALUE(r)); + gen_le32(c); + } else { + g(0x00 | op_reg | REG_VALUE(r)); + } + } else { + g(0x00 | op_reg | REG_VALUE(r)); + } +} + +/* generate a modrm reference. 'op_reg' contains the additional 3 + opcode bits */ +static void gen_modrm(int op_reg, int r, Sym *sym, int c) +{ + gen_modrm_impl(op_reg, r, sym, c, 0); +} + +/* generate a modrm reference. 'op_reg' contains the additional 3 + opcode bits */ +static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c) +{ + int is_got; + is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC); + orex(1, r, op_reg, opcode); + gen_modrm_impl(op_reg, r, sym, c, is_got); +} + + +/* load 'r' from value 'sv' */ +void load(int r, SValue *sv) +{ + int v, t, ft, fc, fr; + SValue v1; + +#ifdef TCC_TARGET_PE + SValue v2; + sv = pe_getimport(sv, &v2); +#endif + + fr = sv->r; + ft = sv->type.t & ~VT_DEFSIGN; + fc = sv->c.i; + if (fc != sv->c.i && (fr & VT_SYM)) + tcc_error("64 bit addend in load"); + + ft &= ~(VT_VOLATILE | VT_CONSTANT); + +#ifndef TCC_TARGET_PE + /* we use indirect access via got */ + if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) && + (fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) { + /* use the result register as a temporal register */ + int tr = r | TREG_MEM; + if (is_float(ft)) { + /* we cannot use float registers as a temporal register */ + tr = get_reg(RC_INT) | TREG_MEM; + } + gen_modrm64(0x8b, tr, fr, sv->sym, 0); + + /* load from the temporal register */ + fr = tr | VT_LVAL; + } +#endif + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + int b, ll; + if (v == VT_LLOCAL) { + v1.type.t = VT_PTR; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + fr = r; + if (!(reg_classes[fr] & (RC_INT|RC_R11))) + fr = get_reg(RC_INT); + load(fr, &v1); + } + if (fc != sv->c.i) { + /* If the addends doesn't fit into a 32bit signed + we must use a 64bit move. We've checked above + that this doesn't have a sym associated. */ + v1.type.t = VT_LLONG; + v1.r = VT_CONST; + v1.c.i = sv->c.i; + fr = r; + if (!(reg_classes[fr] & (RC_INT|RC_R11))) + fr = get_reg(RC_INT); + load(fr, &v1); + fc = 0; + } + ll = 0; + /* Like GCC we can load from small enough properly sized + structs and unions as well. + XXX maybe move to generic operand handling, but should + occur only with asm, so tccasm.c might also be a better place */ + if ((ft & VT_BTYPE) == VT_STRUCT) { + int align; + switch (type_size(&sv->type, &align)) { + case 1: ft = VT_BYTE; break; + case 2: ft = VT_SHORT; break; + case 4: ft = VT_INT; break; + case 8: ft = VT_LLONG; break; + default: + tcc_error("invalid aggregate type for register load"); + break; + } + } + if ((ft & VT_BTYPE) == VT_FLOAT) { + b = 0x6e0f66; + r = REG_VALUE(r); /* movd */ + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + b = 0x7e0ff3; /* movq */ + r = REG_VALUE(r); + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + b = 0xdb, r = 5; /* fldt */ + } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) { + b = 0xbe0f; /* movsbl */ + } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) { + b = 0xb60f; /* movzbl */ + } else if ((ft & VT_TYPE) == VT_SHORT) { + b = 0xbf0f; /* movswl */ + } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) { + b = 0xb70f; /* movzwl */ + } else { + assert(((ft & VT_BTYPE) == VT_INT) + || ((ft & VT_BTYPE) == VT_LLONG) + || ((ft & VT_BTYPE) == VT_PTR) + || ((ft & VT_BTYPE) == VT_FUNC) + ); + ll = is64_type(ft); + b = 0x8b; + } + if (ll) { + gen_modrm64(b, r, fr, sv->sym, fc); + } else { + orex(ll, fr, r, b); + gen_modrm(r, fr, sv->sym, fc); + } + } else { + if (v == VT_CONST) { + if (fr & VT_SYM) { +#ifdef TCC_TARGET_PE + orex(1,0,r,0x8d); + o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */ + gen_addrpc32(fr, sv->sym, fc); +#else + if (sv->sym->type.t & VT_STATIC) { + orex(1,0,r,0x8d); + o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */ + gen_addrpc32(fr, sv->sym, fc); + } else { + orex(1,0,r,0x8b); + o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */ + gen_gotpcrel(r, sv->sym, fc); + } +#endif + } else if (is64_type(ft)) { + orex(1,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */ + gen_le64(sv->c.i); + } else { + orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */ + gen_le32(fc); + } + } else if (v == VT_LOCAL) { + orex(1,0,r,0x8d); /* lea xxx(%ebp), r */ + gen_modrm(r, VT_LOCAL, sv->sym, fc); + } else if (v == VT_CMP) { + orex(0,r,0,0); + if ((fc & ~0x100) != TOK_NE) + oad(0xb8 + REG_VALUE(r), 0); /* mov $0, r */ + else + oad(0xb8 + REG_VALUE(r), 1); /* mov $1, r */ + if (fc & 0x100) + { + /* This was a float compare. If the parity bit is + set the result was unordered, meaning false for everything + except TOK_NE, and true for TOK_NE. */ + fc &= ~0x100; + o(0x037a + (REX_BASE(r) << 8)); + } + orex(0,r,0, 0x0f); /* setxx %br */ + o(fc); + o(0xc0 + REG_VALUE(r)); + } else if (v == VT_JMP || v == VT_JMPI) { + t = v & 1; + orex(0,r,0,0); + oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */ + o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */ + gsym(fc); + orex(0,r,0,0); + oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */ + } else if (v != r) { + if ((r >= TREG_XMM0) && (r <= TREG_XMM7)) { + if (v == TREG_ST0) { + /* gen_cvt_ftof(VT_DOUBLE); */ + o(0xf0245cdd); /* fstpl -0x10(%rsp) */ + /* movsd -0x10(%rsp),%xmmN */ + o(0x100ff2); + o(0x44 + REG_VALUE(r)*8); /* %xmmN */ + o(0xf024); + } else { + assert((v >= TREG_XMM0) && (v <= TREG_XMM7)); + if ((ft & VT_BTYPE) == VT_FLOAT) { + o(0x100ff3); + } else { + assert((ft & VT_BTYPE) == VT_DOUBLE); + o(0x100ff2); + } + o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8); + } + } else if (r == TREG_ST0) { + assert((v >= TREG_XMM0) && (v <= TREG_XMM7)); + /* gen_cvt_ftof(VT_LDOUBLE); */ + /* movsd %xmmN,-0x10(%rsp) */ + o(0x110ff2); + o(0x44 + REG_VALUE(r)*8); /* %xmmN */ + o(0xf024); + o(0xf02444dd); /* fldl -0x10(%rsp) */ + } else { + orex(1,r,v, 0x89); + o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */ + } + } + } +} + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue *v) +{ + int fr, bt, ft, fc; + int op64 = 0; + /* store the REX prefix in this variable when PIC is enabled */ + int pic = 0; + +#ifdef TCC_TARGET_PE + SValue v2; + v = pe_getimport(v, &v2); +#endif + + fr = v->r & VT_VALMASK; + ft = v->type.t; + fc = v->c.i; + if (fc != v->c.i && (fr & VT_SYM)) + tcc_error("64 bit addend in store"); + ft &= ~(VT_VOLATILE | VT_CONSTANT); + bt = ft & VT_BTYPE; + +#ifndef TCC_TARGET_PE + /* we need to access the variable via got */ + if (fr == VT_CONST && (v->r & VT_SYM)) { + /* mov xx(%rip), %r11 */ + o(0x1d8b4c); + gen_gotpcrel(TREG_R11, v->sym, v->c.i); + pic = is64_type(bt) ? 0x49 : 0x41; + } +#endif + + /* XXX: incorrect if float reg to reg */ + if (bt == VT_FLOAT) { + o(0x66); + o(pic); + o(0x7e0f); /* movd */ + r = REG_VALUE(r); + } else if (bt == VT_DOUBLE) { + o(0x66); + o(pic); + o(0xd60f); /* movq */ + r = REG_VALUE(r); + } else if (bt == VT_LDOUBLE) { + o(0xc0d9); /* fld %st(0) */ + o(pic); + o(0xdb); /* fstpt */ + r = 7; + } else { + if (bt == VT_SHORT) + o(0x66); + o(pic); + if (bt == VT_BYTE || bt == VT_BOOL) + orex(0, 0, r, 0x88); + else if (is64_type(bt)) + op64 = 0x89; + else + orex(0, 0, r, 0x89); + } + if (pic) { + /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */ + if (op64) + o(op64); + o(3 + (r << 3)); + } else if (op64) { + if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) { + gen_modrm64(op64, r, v->r, v->sym, fc); + } else if (fr != r) { + /* XXX: don't we really come here? */ + abort(); + o(0xc0 + fr + r * 8); /* mov r, fr */ + } + } else { + if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) { + gen_modrm(r, v->r, v->sym, fc); + } else if (fr != r) { + /* XXX: don't we really come here? */ + abort(); + o(0xc0 + fr + r * 8); /* mov r, fr */ + } + } +} + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && + ((vtop->r & VT_SYM) || (vtop->c.i-4) == (int)(vtop->c.i-4))) { + /* constant case */ + if (vtop->r & VT_SYM) { + /* relocation case */ +#ifdef TCC_TARGET_PE + greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PC32, (int)(vtop->c.i-4)); +#else + greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4)); +#endif + } else { + /* put an empty PC32 relocation */ + put_elf_reloca(symtab_section, cur_text_section, + ind + 1, R_X86_64_PC32, 0, (int)(vtop->c.i-4)); + } + oad(0xe8 + is_jmp, 0); /* call/jmp im */ + } else { + /* otherwise, indirect call */ + r = TREG_R11; + load(r, vtop); + o(0x41); /* REX */ + o(0xff); /* call/jmp *r */ + o(0xd0 + REG_VALUE(r) + (is_jmp << 4)); + } +} + +#if defined(CONFIG_TCC_BCHECK) +#ifndef TCC_TARGET_PE +static addr_t func_bound_offset; +static unsigned long func_bound_ind; +#endif + +static void gen_static_call(int v) +{ + Sym *sym = external_global_sym(v, &func_old_type, 0); + oad(0xe8, 0); + greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4); +} + +/* generate a bounded pointer addition */ +ST_FUNC void gen_bounded_ptr_add(void) +{ + /* save all temporary registers */ + save_regs(0); + + /* prepare fast x86_64 function call */ + gv(RC_RAX); + o(0xc68948); // mov %rax,%rsi ## second arg in %rsi, this must be size + vtop--; + + gv(RC_RAX); + o(0xc78948); // mov %rax,%rdi ## first arg in %rdi, this must be ptr + vtop--; + + /* do a fast function call */ + gen_static_call(TOK___bound_ptr_add); + + /* returned pointer is in rax */ + vtop++; + vtop->r = TREG_RAX | VT_BOUNDED; + + + /* relocation offset of the bounding function call point */ + vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela))); +} + +/* patch pointer addition in vtop so that pointer dereferencing is + also tested */ +ST_FUNC void gen_bounded_ptr_deref(void) +{ + addr_t func; + int size, align; + ElfW(Rela) *rel; + Sym *sym; + + size = 0; + /* XXX: put that code in generic part of tcc */ + if (!is_float(vtop->type.t)) { + if (vtop->r & VT_LVAL_BYTE) + size = 1; + else if (vtop->r & VT_LVAL_SHORT) + size = 2; + } + if (!size) + size = type_size(&vtop->type, &align); + switch(size) { + case 1: func = TOK___bound_ptr_indir1; break; + case 2: func = TOK___bound_ptr_indir2; break; + case 4: func = TOK___bound_ptr_indir4; break; + case 8: func = TOK___bound_ptr_indir8; break; + case 12: func = TOK___bound_ptr_indir12; break; + case 16: func = TOK___bound_ptr_indir16; break; + default: + tcc_error("unhandled size when dereferencing bounded pointer"); + func = 0; + break; + } + + sym = external_global_sym(func, &func_old_type, 0); + if (!sym->c) + put_extern_sym(sym, NULL, 0, 0); + + /* patch relocation */ + /* XXX: find a better solution ? */ + + rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i); + rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info)); +} +#endif + +#ifdef TCC_TARGET_PE + +#define REGN 4 +static const uint8_t arg_regs[REGN] = { + TREG_RCX, TREG_RDX, TREG_R8, TREG_R9 +}; + +/* Prepare arguments in R10 and R11 rather than RCX and RDX + because gv() will not ever use these */ +static int arg_prepare_reg(int idx) { + if (idx == 0 || idx == 1) + /* idx=0: r10, idx=1: r11 */ + return idx + 10; + else + return arg_regs[idx]; +} + +static int func_scratch, func_alloca; + +/* Generate function call. The function address is pushed first, then + all the parameters in call order. This functions pops all the + parameters and the function address. */ + +static void gen_offs_sp(int b, int r, int d) +{ + orex(1,0,r & 0x100 ? 0 : r, b); + if (d == (char)d) { + o(0x2444 | (REG_VALUE(r) << 3)); + g(d); + } else { + o(0x2484 | (REG_VALUE(r) << 3)); + gen_le32(d); + } +} + +static int using_regs(int size) +{ + return !(size > 8 || (size & (size - 1))); +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) +{ + int size, align; + *ret_align = 1; // Never have to re-align return values for x86-64 + *regsize = 8; + size = type_size(vt, &align); + if (!using_regs(size)) + return 0; + if (size == 8) + ret->t = VT_LLONG; + else if (size == 4) + ret->t = VT_INT; + else if (size == 2) + ret->t = VT_SHORT; + else + ret->t = VT_BYTE; + ret->ref = NULL; + return 1; +} + +static int is_sse_float(int t) { + int bt; + bt = t & VT_BTYPE; + return bt == VT_DOUBLE || bt == VT_FLOAT; +} + +static int gfunc_arg_size(CType *type) { + int align; + if (type->t & (VT_ARRAY|VT_BITFIELD)) + return 8; + return type_size(type, &align); +} + +void gfunc_call(int nb_args) +{ + int size, r, args_size, i, d, bt, struct_size; + int arg; + + args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE; + arg = nb_args; + + /* for struct arguments, we need to call memcpy and the function + call breaks register passing arguments we are preparing. + So, we process arguments which will be passed by stack first. */ + struct_size = args_size; + for(i = 0; i < nb_args; i++) { + SValue *sv; + + --arg; + sv = &vtop[-i]; + bt = (sv->type.t & VT_BTYPE); + size = gfunc_arg_size(&sv->type); + + if (using_regs(size)) + continue; /* arguments smaller than 8 bytes passed in registers or on stack */ + + if (bt == VT_STRUCT) { + /* align to stack align size */ + size = (size + 15) & ~15; + /* generate structure store */ + r = get_reg(RC_INT); + gen_offs_sp(0x8d, r, struct_size); + struct_size += size; + + /* generate memcpy call */ + vset(&sv->type, r | VT_LVAL, 0); + vpushv(sv); + vstore(); + --vtop; + } else if (bt == VT_LDOUBLE) { + gv(RC_ST0); + gen_offs_sp(0xdb, 0x107, struct_size); + struct_size += 16; + } + } + + if (func_scratch < struct_size) + func_scratch = struct_size; + + arg = nb_args; + struct_size = args_size; + + for(i = 0; i < nb_args; i++) { + --arg; + bt = (vtop->type.t & VT_BTYPE); + + size = gfunc_arg_size(&vtop->type); + if (!using_regs(size)) { + /* align to stack align size */ + size = (size + 15) & ~15; + if (arg >= REGN) { + d = get_reg(RC_INT); + gen_offs_sp(0x8d, d, struct_size); + gen_offs_sp(0x89, d, arg*8); + } else { + d = arg_prepare_reg(arg); + gen_offs_sp(0x8d, d, struct_size); + } + struct_size += size; + } else { + if (is_sse_float(vtop->type.t)) { + if (tcc_state->nosse) + tcc_error("SSE disabled"); + gv(RC_XMM0); /* only use one float register */ + if (arg >= REGN) { + /* movq %xmm0, j*8(%rsp) */ + gen_offs_sp(0xd60f66, 0x100, arg*8); + } else { + /* movaps %xmm0, %xmmN */ + o(0x280f); + o(0xc0 + (arg << 3)); + d = arg_prepare_reg(arg); + /* mov %xmm0, %rxx */ + o(0x66); + orex(1,d,0, 0x7e0f); + o(0xc0 + REG_VALUE(d)); + } + } else { + if (bt == VT_STRUCT) { + vtop->type.ref = NULL; + vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT + : size > 1 ? VT_SHORT : VT_BYTE; + } + + r = gv(RC_INT); + if (arg >= REGN) { + gen_offs_sp(0x89, r, arg*8); + } else { + d = arg_prepare_reg(arg); + orex(1,d,r,0x89); /* mov */ + o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d)); + } + } + } + vtop--; + } + save_regs(0); + + /* Copy R10 and R11 into RCX and RDX, respectively */ + if (nb_args > 0) { + o(0xd1894c); /* mov %r10, %rcx */ + if (nb_args > 1) { + o(0xda894c); /* mov %r11, %rdx */ + } + } + + gcall_or_jmp(0); + + if ((vtop->r & VT_SYM) && vtop->sym->v == TOK_alloca) { + /* need to add the "func_scratch" area after alloca */ + o(0x0548), gen_le32(func_alloca), func_alloca = ind - 4; + } + + /* other compilers don't clear the upper bits when returning char/short */ + bt = vtop->type.ref->type.t & (VT_BTYPE | VT_UNSIGNED); + if (bt == (VT_BYTE | VT_UNSIGNED)) + o(0xc0b60f); /* movzbl %al, %eax */ + else if (bt == VT_BYTE) + o(0xc0be0f); /* movsbl %al, %eax */ + else if (bt == VT_SHORT) + o(0x98); /* cwtl */ + else if (bt == (VT_SHORT | VT_UNSIGNED)) + o(0xc0b70f); /* movzbl %al, %eax */ +#if 0 /* handled in gen_cast() */ + else if (bt == VT_INT) + o(0x9848); /* cltq */ + else if (bt == (VT_INT | VT_UNSIGNED)) + o(0xc089); /* mov %eax,%eax */ +#endif + vtop--; +} + + +#define FUNC_PROLOG_SIZE 11 + +/* generate function prolog of type 't' */ +void gfunc_prolog(CType *func_type) +{ + int addr, reg_param_index, bt, size; + Sym *sym; + CType *type; + + func_ret_sub = 0; + func_scratch = 0; + func_alloca = 0; + loc = 0; + + addr = PTR_SIZE * 2; + ind += FUNC_PROLOG_SIZE; + func_sub_sp_offset = ind; + reg_param_index = 0; + + sym = func_type->ref; + + /* if the function returns a structure, then add an + implicit pointer parameter */ + func_vt = sym->type; + func_var = (sym->f.func_type == FUNC_ELLIPSIS); + size = gfunc_arg_size(&func_vt); + if (!using_regs(size)) { + gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); + func_vc = addr; + reg_param_index++; + addr += 8; + } + + /* define parameters */ + while ((sym = sym->next) != NULL) { + type = &sym->type; + bt = type->t & VT_BTYPE; + size = gfunc_arg_size(type); + if (!using_regs(size)) { + if (reg_param_index < REGN) { + gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); + } + sym_push(sym->v & ~SYM_FIELD, type, VT_LLOCAL | VT_LVAL, addr); + } else { + if (reg_param_index < REGN) { + /* save arguments passed by register */ + if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) { + if (tcc_state->nosse) + tcc_error("SSE disabled"); + o(0xd60f66); /* movq */ + gen_modrm(reg_param_index, VT_LOCAL, NULL, addr); + } else { + gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); + } + } + sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr); + } + addr += 8; + reg_param_index++; + } + + while (reg_param_index < REGN) { + if (func_type->ref->f.func_type == FUNC_ELLIPSIS) { + gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); + addr += 8; + } + reg_param_index++; + } +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + int v, saved_ind; + + o(0xc9); /* leave */ + if (func_ret_sub == 0) { + o(0xc3); /* ret */ + } else { + o(0xc2); /* ret n */ + g(func_ret_sub); + g(func_ret_sub >> 8); + } + + saved_ind = ind; + ind = func_sub_sp_offset - FUNC_PROLOG_SIZE; + /* align local size to word & save local variables */ + func_scratch = (func_scratch + 15) & -16; + v = (func_scratch + -loc + 15) & -16; + + if (v >= 4096) { + Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0); + oad(0xb8, v); /* mov stacksize, %eax */ + oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */ + greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4); + o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */ + } else { + o(0xe5894855); /* push %rbp, mov %rsp, %rbp */ + o(0xec8148); /* sub rsp, stacksize */ + gen_le32(v); + } + + /* add the "func_scratch" area after each alloca seen */ + while (func_alloca) { + unsigned char *ptr = cur_text_section->data + func_alloca; + func_alloca = read32le(ptr); + write32le(ptr, func_scratch); + } + + cur_text_section->data_offset = saved_ind; + pe_add_unwind_data(ind, saved_ind, v); + ind = cur_text_section->data_offset; +} + +#else + +static void gadd_sp(int val) +{ + if (val == (char)val) { + o(0xc48348); + g(val); + } else { + oad(0xc48148, val); /* add $xxx, %rsp */ + } +} + +typedef enum X86_64_Mode { + x86_64_mode_none, + x86_64_mode_memory, + x86_64_mode_integer, + x86_64_mode_sse, + x86_64_mode_x87 +} X86_64_Mode; + +static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) +{ + if (a == b) + return a; + else if (a == x86_64_mode_none) + return b; + else if (b == x86_64_mode_none) + return a; + else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory)) + return x86_64_mode_memory; + else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer)) + return x86_64_mode_integer; + else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87)) + return x86_64_mode_memory; + else + return x86_64_mode_sse; +} + +static X86_64_Mode classify_x86_64_inner(CType *ty) +{ + X86_64_Mode mode; + Sym *f; + + switch (ty->t & VT_BTYPE) { + case VT_VOID: return x86_64_mode_none; + + case VT_INT: + case VT_BYTE: + case VT_SHORT: + case VT_LLONG: + case VT_BOOL: + case VT_PTR: + case VT_FUNC: + return x86_64_mode_integer; + + case VT_FLOAT: + case VT_DOUBLE: return x86_64_mode_sse; + + case VT_LDOUBLE: return x86_64_mode_x87; + + case VT_STRUCT: + f = ty->ref; + + mode = x86_64_mode_none; + for (f = f->next; f; f = f->next) + mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type)); + + return mode; + } + assert(0); + return 0; +} + +static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count) +{ + X86_64_Mode mode; + int size, align, ret_t = 0; + + if (ty->t & (VT_BITFIELD|VT_ARRAY)) { + *psize = 8; + *palign = 8; + *reg_count = 1; + ret_t = ty->t; + mode = x86_64_mode_integer; + } else { + size = type_size(ty, &align); + *psize = (size + 7) & ~7; + *palign = (align + 7) & ~7; + + if (size > 16) { + mode = x86_64_mode_memory; + } else { + mode = classify_x86_64_inner(ty); + switch (mode) { + case x86_64_mode_integer: + if (size > 8) { + *reg_count = 2; + ret_t = VT_QLONG; + } else { + *reg_count = 1; + ret_t = (size > 4) ? VT_LLONG : VT_INT; + } + break; + + case x86_64_mode_x87: + *reg_count = 1; + ret_t = VT_LDOUBLE; + break; + + case x86_64_mode_sse: + if (size > 8) { + *reg_count = 2; + ret_t = VT_QFLOAT; + } else { + *reg_count = 1; + ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT; + } + break; + default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/ + } + } + } + + if (ret) { + ret->ref = NULL; + ret->t = ret_t; + } + + return mode; +} + +ST_FUNC int classify_x86_64_va_arg(CType *ty) +{ + /* This definition must be synced with stdarg.h */ + enum __va_arg_type { + __va_gen_reg, __va_float_reg, __va_stack + }; + int size, align, reg_count; + X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &align, ®_count); + switch (mode) { + default: return __va_stack; + case x86_64_mode_integer: return __va_gen_reg; + case x86_64_mode_sse: return __va_float_reg; + } +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) +{ + int size, align, reg_count; + *ret_align = 1; // Never have to re-align return values for x86-64 + *regsize = 8; + return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) != x86_64_mode_memory); +} + +#define REGN 6 +static const uint8_t arg_regs[REGN] = { + TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9 +}; + +static int arg_prepare_reg(int idx) { + if (idx == 2 || idx == 3) + /* idx=2: r10, idx=3: r11 */ + return idx + 8; + else + return arg_regs[idx]; +} + +/* Generate function call. The function address is pushed first, then + all the parameters in call order. This functions pops all the + parameters and the function address. */ +void gfunc_call(int nb_args) +{ + X86_64_Mode mode; + CType type; + int size, align, r, args_size, stack_adjust, i, reg_count; + int nb_reg_args = 0; + int nb_sse_args = 0; + int sse_reg, gen_reg; + char _onstack[nb_args], *onstack = _onstack; + + /* calculate the number of integer/float register arguments, remember + arguments to be passed via stack (in onstack[]), and also remember + if we have to align the stack pointer to 16 (onstack[i] == 2). Needs + to be done in a left-to-right pass over arguments. */ + stack_adjust = 0; + for(i = nb_args - 1; i >= 0; i--) { + mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, ®_count); + if (mode == x86_64_mode_sse && nb_sse_args + reg_count <= 8) { + nb_sse_args += reg_count; + onstack[i] = 0; + } else if (mode == x86_64_mode_integer && nb_reg_args + reg_count <= REGN) { + nb_reg_args += reg_count; + onstack[i] = 0; + } else if (mode == x86_64_mode_none) { + onstack[i] = 0; + } else { + if (align == 16 && (stack_adjust &= 15)) { + onstack[i] = 2; + stack_adjust = 0; + } else + onstack[i] = 1; + stack_adjust += size; + } + } + + if (nb_sse_args && tcc_state->nosse) + tcc_error("SSE disabled but floating point arguments passed"); + + /* fetch cpu flag before generating any code */ + if (vtop >= vstack && (vtop->r & VT_VALMASK) == VT_CMP) + gv(RC_INT); + + /* for struct arguments, we need to call memcpy and the function + call breaks register passing arguments we are preparing. + So, we process arguments which will be passed by stack first. */ + gen_reg = nb_reg_args; + sse_reg = nb_sse_args; + args_size = 0; + stack_adjust &= 15; + for (i = 0; i < nb_args;) { + mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, ®_count); + if (!onstack[i]) { + ++i; + continue; + } + /* Possibly adjust stack to align SSE boundary. We're processing + args from right to left while allocating happens left to right + (stack grows down), so the adjustment needs to happen _after_ + an argument that requires it. */ + if (stack_adjust) { + o(0x50); /* push %rax; aka sub $8,%rsp */ + args_size += 8; + stack_adjust = 0; + } + if (onstack[i] == 2) + stack_adjust = 1; + + vrotb(i+1); + + switch (vtop->type.t & VT_BTYPE) { + case VT_STRUCT: + /* allocate the necessary size on stack */ + o(0x48); + oad(0xec81, size); /* sub $xxx, %rsp */ + /* generate structure store */ + r = get_reg(RC_INT); + orex(1, r, 0, 0x89); /* mov %rsp, r */ + o(0xe0 + REG_VALUE(r)); + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + break; + + case VT_LDOUBLE: + gv(RC_ST0); + oad(0xec8148, size); /* sub $xxx, %rsp */ + o(0x7cdb); /* fstpt 0(%rsp) */ + g(0x24); + g(0x00); + break; + + case VT_FLOAT: + case VT_DOUBLE: + assert(mode == x86_64_mode_sse); + r = gv(RC_FLOAT); + o(0x50); /* push $rax */ + /* movq %xmmN, (%rsp) */ + o(0xd60f66); + o(0x04 + REG_VALUE(r)*8); + o(0x24); + break; + + default: + assert(mode == x86_64_mode_integer); + /* simple type */ + /* XXX: implicit cast ? */ + r = gv(RC_INT); + orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */ + break; + } + args_size += size; + + vpop(); + --nb_args; + onstack++; + } + + /* XXX This should be superfluous. */ + save_regs(0); /* save used temporary registers */ + + /* then, we prepare register passing arguments. + Note that we cannot set RDX and RCX in this loop because gv() + may break these temporary registers. Let's use R10 and R11 + instead of them */ + assert(gen_reg <= REGN); + assert(sse_reg <= 8); + for(i = 0; i < nb_args; i++) { + mode = classify_x86_64_arg(&vtop->type, &type, &size, &align, ®_count); + /* Alter stack entry type so that gv() knows how to treat it */ + vtop->type = type; + if (mode == x86_64_mode_sse) { + if (reg_count == 2) { + sse_reg -= 2; + gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */ + if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */ + /* movaps %xmm0, %xmmN */ + o(0x280f); + o(0xc0 + (sse_reg << 3)); + /* movaps %xmm1, %xmmN */ + o(0x280f); + o(0xc1 + ((sse_reg+1) << 3)); + } + } else { + assert(reg_count == 1); + --sse_reg; + /* Load directly to register */ + gv(RC_XMM0 << sse_reg); + } + } else if (mode == x86_64_mode_integer) { + /* simple type */ + /* XXX: implicit cast ? */ + int d; + gen_reg -= reg_count; + r = gv(RC_INT); + d = arg_prepare_reg(gen_reg); + orex(1,d,r,0x89); /* mov */ + o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d)); + if (reg_count == 2) { + d = arg_prepare_reg(gen_reg+1); + orex(1,d,vtop->r2,0x89); /* mov */ + o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d)); + } + } + vtop--; + } + assert(gen_reg == 0); + assert(sse_reg == 0); + + /* We shouldn't have many operands on the stack anymore, but the + call address itself is still there, and it might be in %eax + (or edx/ecx) currently, which the below writes would clobber. + So evict all remaining operands here. */ + save_regs(0); + + /* Copy R10 and R11 into RDX and RCX, respectively */ + if (nb_reg_args > 2) { + o(0xd2894c); /* mov %r10, %rdx */ + if (nb_reg_args > 3) { + o(0xd9894c); /* mov %r11, %rcx */ + } + } + + if (vtop->type.ref->f.func_type != FUNC_NEW) /* implies FUNC_OLD or FUNC_ELLIPSIS */ + oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */ + gcall_or_jmp(0); + if (args_size) + gadd_sp(args_size); + vtop--; +} + + +#define FUNC_PROLOG_SIZE 11 + +static void push_arg_reg(int i) { + loc -= 8; + gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc); +} + +/* generate function prolog of type 't' */ +void gfunc_prolog(CType *func_type) +{ + X86_64_Mode mode; + int i, addr, align, size, reg_count; + int param_addr = 0, reg_param_index, sse_param_index; + Sym *sym; + CType *type; + + sym = func_type->ref; + addr = PTR_SIZE * 2; + loc = 0; + ind += FUNC_PROLOG_SIZE; + func_sub_sp_offset = ind; + func_ret_sub = 0; + + if (sym->f.func_type == FUNC_ELLIPSIS) { + int seen_reg_num, seen_sse_num, seen_stack_size; + seen_reg_num = seen_sse_num = 0; + /* frame pointer and return address */ + seen_stack_size = PTR_SIZE * 2; + /* count the number of seen parameters */ + sym = func_type->ref; + while ((sym = sym->next) != NULL) { + type = &sym->type; + mode = classify_x86_64_arg(type, NULL, &size, &align, ®_count); + switch (mode) { + default: + stack_arg: + seen_stack_size = ((seen_stack_size + align - 1) & -align) + size; + break; + + case x86_64_mode_integer: + if (seen_reg_num + reg_count > REGN) + goto stack_arg; + seen_reg_num += reg_count; + break; + + case x86_64_mode_sse: + if (seen_sse_num + reg_count > 8) + goto stack_arg; + seen_sse_num += reg_count; + break; + } + } + + loc -= 16; + /* movl $0x????????, -0x10(%rbp) */ + o(0xf045c7); + gen_le32(seen_reg_num * 8); + /* movl $0x????????, -0xc(%rbp) */ + o(0xf445c7); + gen_le32(seen_sse_num * 16 + 48); + /* movl $0x????????, -0x8(%rbp) */ + o(0xf845c7); + gen_le32(seen_stack_size); + + /* save all register passing arguments */ + for (i = 0; i < 8; i++) { + loc -= 16; + if (!tcc_state->nosse) { + o(0xd60f66); /* movq */ + gen_modrm(7 - i, VT_LOCAL, NULL, loc); + } + /* movq $0, loc+8(%rbp) */ + o(0x85c748); + gen_le32(loc + 8); + gen_le32(0); + } + for (i = 0; i < REGN; i++) { + push_arg_reg(REGN-1-i); + } + } + + sym = func_type->ref; + reg_param_index = 0; + sse_param_index = 0; + + /* if the function returns a structure, then add an + implicit pointer parameter */ + func_vt = sym->type; + mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, ®_count); + if (mode == x86_64_mode_memory) { + push_arg_reg(reg_param_index); + func_vc = loc; + reg_param_index++; + } + /* define parameters */ + while ((sym = sym->next) != NULL) { + type = &sym->type; + mode = classify_x86_64_arg(type, NULL, &size, &align, ®_count); + switch (mode) { + case x86_64_mode_sse: + if (tcc_state->nosse) + tcc_error("SSE disabled but floating point arguments used"); + if (sse_param_index + reg_count <= 8) { + /* save arguments passed by register */ + loc -= reg_count * 8; + param_addr = loc; + for (i = 0; i < reg_count; ++i) { + o(0xd60f66); /* movq */ + gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8); + ++sse_param_index; + } + } else { + addr = (addr + align - 1) & -align; + param_addr = addr; + addr += size; + } + break; + + case x86_64_mode_memory: + case x86_64_mode_x87: + addr = (addr + align - 1) & -align; + param_addr = addr; + addr += size; + break; + + case x86_64_mode_integer: { + if (reg_param_index + reg_count <= REGN) { + /* save arguments passed by register */ + loc -= reg_count * 8; + param_addr = loc; + for (i = 0; i < reg_count; ++i) { + gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8); + ++reg_param_index; + } + } else { + addr = (addr + align - 1) & -align; + param_addr = addr; + addr += size; + } + break; + } + default: break; /* nothing to be done for x86_64_mode_none */ + } + sym_push(sym->v & ~SYM_FIELD, type, + VT_LOCAL | VT_LVAL, param_addr); + } + +#ifdef CONFIG_TCC_BCHECK + /* leave some room for bound checking code */ + if (tcc_state->do_bounds_check) { + func_bound_offset = lbounds_section->data_offset; + func_bound_ind = ind; + oad(0xb8, 0); /* lbound section pointer */ + o(0xc78948); /* mov %rax,%rdi ## first arg in %rdi, this must be ptr */ + oad(0xb8, 0); /* call to function */ + } +#endif +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + int v, saved_ind; + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check + && func_bound_offset != lbounds_section->data_offset) + { + addr_t saved_ind; + addr_t *bounds_ptr; + Sym *sym_data; + + /* add end of table info */ + bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t)); + *bounds_ptr = 0; + + /* generate bound local allocation */ + sym_data = get_sym_ref(&char_pointer_type, lbounds_section, + func_bound_offset, lbounds_section->data_offset); + saved_ind = ind; + ind = func_bound_ind; + greloca(cur_text_section, sym_data, ind + 1, R_X86_64_64, 0); + ind = ind + 5 + 3; + gen_static_call(TOK___bound_local_new); + ind = saved_ind; + + /* generate bound check local freeing */ + o(0x5250); /* save returned value, if any */ + greloca(cur_text_section, sym_data, ind + 1, R_X86_64_64, 0); + oad(0xb8, 0); /* mov xxx, %rax */ + o(0xc78948); /* mov %rax,%rdi # first arg in %rdi, this must be ptr */ + gen_static_call(TOK___bound_local_delete); + o(0x585a); /* restore returned value, if any */ + } +#endif + o(0xc9); /* leave */ + if (func_ret_sub == 0) { + o(0xc3); /* ret */ + } else { + o(0xc2); /* ret n */ + g(func_ret_sub); + g(func_ret_sub >> 8); + } + /* align local size to word & save local variables */ + v = (-loc + 15) & -16; + saved_ind = ind; + ind = func_sub_sp_offset - FUNC_PROLOG_SIZE; + o(0xe5894855); /* push %rbp, mov %rsp, %rbp */ + o(0xec8148); /* sub rsp, stacksize */ + gen_le32(v); + ind = saved_ind; +} + +#endif /* not PE */ + +/* generate a jump to a label */ +int gjmp(int t) +{ + return gjmp2(0xe9, t); +} + +/* generate a jump to a fixed address */ +void gjmp_addr(int a) +{ + int r; + r = a - ind - 2; + if (r == (char)r) { + g(0xeb); + g(r); + } else { + oad(0xe9, a - ind - 5); + } +} + +ST_FUNC void gtst_addr(int inv, int a) +{ + int v = vtop->r & VT_VALMASK; + if (v == VT_CMP) { + inv ^= (vtop--)->c.i; + a -= ind + 2; + if (a == (char)a) { + g(inv - 32); + g(a); + } else { + g(0x0f); + oad(inv - 16, a - 4); + } + } else if ((v & ~1) == VT_JMP) { + if ((v & 1) != inv) { + gjmp_addr(a); + gsym(vtop->c.i); + } else { + gsym(vtop->c.i); + o(0x05eb); + gjmp_addr(a); + } + vtop--; + } +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +ST_FUNC int gtst(int inv, int t) +{ + int v = vtop->r & VT_VALMASK; + + if (nocode_wanted) { + ; + } else if (v == VT_CMP) { + /* fast case : can jump directly since flags are set */ + if (vtop->c.i & 0x100) + { + /* This was a float compare. If the parity flag is set + the result was unordered. For anything except != this + means false and we don't jump (anding both conditions). + For != this means true (oring both). + Take care about inverting the test. We need to jump + to our target if the result was unordered and test wasn't NE, + otherwise if unordered we don't want to jump. */ + vtop->c.i &= ~0x100; + if (inv == (vtop->c.i == TOK_NE)) + o(0x067a); /* jp +6 */ + else + { + g(0x0f); + t = gjmp2(0x8a, t); /* jp t */ + } + } + g(0x0f); + t = gjmp2((vtop->c.i - 16) ^ inv, t); + } else if (v == VT_JMP || v == VT_JMPI) { + /* && or || optimization */ + if ((v & 1) == inv) { + /* insert vtop->c jump list in t */ + uint32_t n1, n = vtop->c.i; + if (n) { + while ((n1 = read32le(cur_text_section->data + n))) + n = n1; + write32le(cur_text_section->data + n, t); + t = vtop->c.i; + } + } else { + t = gjmp(t); + gsym(vtop->c.i); + } + } + vtop--; + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + int r, fr, opc, c; + int ll, uu, cc; + + ll = is64_type(vtop[-1].type.t); + uu = (vtop[-1].type.t & VT_UNSIGNED) != 0; + cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; + + switch(op) { + case '+': + case TOK_ADDC1: /* add with carry generation */ + opc = 0; + gen_op8: + if (cc && (!ll || (int)vtop->c.i == vtop->c.i)) { + /* constant case */ + vswap(); + r = gv(RC_INT); + vswap(); + c = vtop->c.i; + if (c == (char)c) { + /* XXX: generate inc and dec for smaller code ? */ + orex(ll, r, 0, 0x83); + o(0xc0 | (opc << 3) | REG_VALUE(r)); + g(c); + } else { + orex(ll, r, 0, 0x81); + oad(0xc0 | (opc << 3) | REG_VALUE(r), c); + } + } else { + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + orex(ll, r, fr, (opc << 3) | 0x01); + o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8); + } + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) { + vtop->r = VT_CMP; + vtop->c.i = op; + } + break; + case '-': + case TOK_SUBC1: /* sub with carry generation */ + opc = 5; + goto gen_op8; + case TOK_ADDC2: /* add with carry use */ + opc = 2; + goto gen_op8; + case TOK_SUBC2: /* sub with carry use */ + opc = 3; + goto gen_op8; + case '&': + opc = 4; + goto gen_op8; + case '^': + opc = 6; + goto gen_op8; + case '|': + opc = 1; + goto gen_op8; + case '*': + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + orex(ll, fr, r, 0xaf0f); /* imul fr, r */ + o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8); + vtop--; + break; + case TOK_SHL: + opc = 4; + goto gen_shift; + case TOK_SHR: + opc = 5; + goto gen_shift; + case TOK_SAR: + opc = 7; + gen_shift: + opc = 0xc0 | (opc << 3); + if (cc) { + /* constant case */ + vswap(); + r = gv(RC_INT); + vswap(); + orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */ + o(opc | REG_VALUE(r)); + g(vtop->c.i & (ll ? 63 : 31)); + } else { + /* we generate the shift in ecx */ + gv2(RC_INT, RC_RCX); + r = vtop[-1].r; + orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */ + o(opc | REG_VALUE(r)); + } + vtop--; + break; + case TOK_UDIV: + case TOK_UMOD: + uu = 1; + goto divmod; + case '/': + case '%': + case TOK_PDIV: + uu = 0; + divmod: + /* first operand must be in eax */ + /* XXX: need better constraint for second operand */ + gv2(RC_RAX, RC_RCX); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + save_reg(TREG_RDX); + orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */ + orex(ll, fr, 0, 0xf7); /* div fr, %eax */ + o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr)); + if (op == '%' || op == TOK_UMOD) + r = TREG_RDX; + else + r = TREG_RAX; + vtop->r = r; + break; + default: + opc = 7; + goto gen_op8; + } +} + +void gen_opl(int op) +{ + gen_opi(op); +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +/* XXX: need to use ST1 too */ +void gen_opf(int op) +{ + int a, ft, fc, swapped, r; + int float_type = + (vtop->type.t & VT_BTYPE) == VT_LDOUBLE ? RC_ST0 : RC_FLOAT; + + /* convert constants to memory references */ + if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + vswap(); + gv(float_type); + vswap(); + } + if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) + gv(float_type); + + /* must put at least one value in the floating point register */ + if ((vtop[-1].r & VT_LVAL) && + (vtop[0].r & VT_LVAL)) { + vswap(); + gv(float_type); + vswap(); + } + swapped = 0; + /* swap the stack if needed so that t1 is the register and t2 is + the memory reference */ + if (vtop[-1].r & VT_LVAL) { + vswap(); + swapped = 1; + } + if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + if (op >= TOK_ULT && op <= TOK_GT) { + /* load on stack second operand */ + load(TREG_ST0, vtop); + save_reg(TREG_RAX); /* eax is used by FP comparison code */ + if (op == TOK_GE || op == TOK_GT) + swapped = !swapped; + else if (op == TOK_EQ || op == TOK_NE) + swapped = 0; + if (swapped) + o(0xc9d9); /* fxch %st(1) */ + if (op == TOK_EQ || op == TOK_NE) + o(0xe9da); /* fucompp */ + else + o(0xd9de); /* fcompp */ + o(0xe0df); /* fnstsw %ax */ + if (op == TOK_EQ) { + o(0x45e480); /* and $0x45, %ah */ + o(0x40fC80); /* cmp $0x40, %ah */ + } else if (op == TOK_NE) { + o(0x45e480); /* and $0x45, %ah */ + o(0x40f480); /* xor $0x40, %ah */ + op = TOK_NE; + } else if (op == TOK_GE || op == TOK_LE) { + o(0x05c4f6); /* test $0x05, %ah */ + op = TOK_EQ; + } else { + o(0x45c4f6); /* test $0x45, %ah */ + op = TOK_EQ; + } + vtop--; + vtop->r = VT_CMP; + vtop->c.i = op; + } else { + /* no memory reference possible for long double operations */ + load(TREG_ST0, vtop); + swapped = !swapped; + + switch(op) { + default: + case '+': + a = 0; + break; + case '-': + a = 4; + if (swapped) + a++; + break; + case '*': + a = 1; + break; + case '/': + a = 6; + if (swapped) + a++; + break; + } + ft = vtop->type.t; + fc = vtop->c.i; + o(0xde); /* fxxxp %st, %st(1) */ + o(0xc1 + (a << 3)); + vtop--; + } + } else { + if (op >= TOK_ULT && op <= TOK_GT) { + /* if saved lvalue, then we must reload it */ + r = vtop->r; + fc = vtop->c.i; + if ((r & VT_VALMASK) == VT_LLOCAL) { + SValue v1; + r = get_reg(RC_INT); + v1.type.t = VT_PTR; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + load(r, &v1); + fc = 0; + } + + if (op == TOK_EQ || op == TOK_NE) { + swapped = 0; + } else { + if (op == TOK_LE || op == TOK_LT) + swapped = !swapped; + if (op == TOK_LE || op == TOK_GE) { + op = 0x93; /* setae */ + } else { + op = 0x97; /* seta */ + } + } + + if (swapped) { + gv(RC_FLOAT); + vswap(); + } + assert(!(vtop[-1].r & VT_LVAL)); + + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) + o(0x66); + if (op == TOK_EQ || op == TOK_NE) + o(0x2e0f); /* ucomisd */ + else + o(0x2f0f); /* comisd */ + + if (vtop->r & VT_LVAL) { + gen_modrm(vtop[-1].r, r, vtop->sym, fc); + } else { + o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8); + } + + vtop--; + vtop->r = VT_CMP; + vtop->c.i = op | 0x100; + } else { + assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE); + switch(op) { + default: + case '+': + a = 0; + break; + case '-': + a = 4; + break; + case '*': + a = 1; + break; + case '/': + a = 6; + break; + } + ft = vtop->type.t; + fc = vtop->c.i; + assert((ft & VT_BTYPE) != VT_LDOUBLE); + + r = vtop->r; + /* if saved lvalue, then we must reload it */ + if ((vtop->r & VT_VALMASK) == VT_LLOCAL) { + SValue v1; + r = get_reg(RC_INT); + v1.type.t = VT_PTR; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + load(r, &v1); + fc = 0; + } + + assert(!(vtop[-1].r & VT_LVAL)); + if (swapped) { + assert(vtop->r & VT_LVAL); + gv(RC_FLOAT); + vswap(); + } + + if ((ft & VT_BTYPE) == VT_DOUBLE) { + o(0xf2); + } else { + o(0xf3); + } + o(0x0f); + o(0x58 + a); + + if (vtop->r & VT_LVAL) { + gen_modrm(vtop[-1].r, r, vtop->sym, fc); + } else { + o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8); + } + + vtop--; + } + } +} + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +void gen_cvt_itof(int t) +{ + if ((t & VT_BTYPE) == VT_LDOUBLE) { + save_reg(TREG_ST0); + gv(RC_INT); + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + /* signed long long to float/double/long double (unsigned case + is handled generically) */ + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x242cdf); /* fildll (%rsp) */ + o(0x08c48348); /* add $8, %rsp */ + } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == + (VT_INT | VT_UNSIGNED)) { + /* unsigned int to float/double/long double */ + o(0x6a); /* push $0 */ + g(0x00); + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x242cdf); /* fildll (%rsp) */ + o(0x10c48348); /* add $16, %rsp */ + } else { + /* int to float/double/long double */ + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x2404db); /* fildl (%rsp) */ + o(0x08c48348); /* add $8, %rsp */ + } + vtop->r = TREG_ST0; + } else { + int r = get_reg(RC_FLOAT); + gv(RC_INT); + o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0)); + if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == + (VT_INT | VT_UNSIGNED) || + (vtop->type.t & VT_BTYPE) == VT_LLONG) { + o(0x48); /* REX */ + } + o(0x2a0f); + o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */ + vtop->r = r; + } +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ + int ft, bt, tbt; + + ft = vtop->type.t; + bt = ft & VT_BTYPE; + tbt = t & VT_BTYPE; + + if (bt == VT_FLOAT) { + gv(RC_FLOAT); + if (tbt == VT_DOUBLE) { + o(0x140f); /* unpcklps */ + o(0xc0 + REG_VALUE(vtop->r)*9); + o(0x5a0f); /* cvtps2pd */ + o(0xc0 + REG_VALUE(vtop->r)*9); + } else if (tbt == VT_LDOUBLE) { + save_reg(RC_ST0); + /* movss %xmm0,-0x10(%rsp) */ + o(0x110ff3); + o(0x44 + REG_VALUE(vtop->r)*8); + o(0xf024); + o(0xf02444d9); /* flds -0x10(%rsp) */ + vtop->r = TREG_ST0; + } + } else if (bt == VT_DOUBLE) { + gv(RC_FLOAT); + if (tbt == VT_FLOAT) { + o(0x140f66); /* unpcklpd */ + o(0xc0 + REG_VALUE(vtop->r)*9); + o(0x5a0f66); /* cvtpd2ps */ + o(0xc0 + REG_VALUE(vtop->r)*9); + } else if (tbt == VT_LDOUBLE) { + save_reg(RC_ST0); + /* movsd %xmm0,-0x10(%rsp) */ + o(0x110ff2); + o(0x44 + REG_VALUE(vtop->r)*8); + o(0xf024); + o(0xf02444dd); /* fldl -0x10(%rsp) */ + vtop->r = TREG_ST0; + } + } else { + int r; + gv(RC_ST0); + r = get_reg(RC_FLOAT); + if (tbt == VT_DOUBLE) { + o(0xf0245cdd); /* fstpl -0x10(%rsp) */ + /* movsd -0x10(%rsp),%xmm0 */ + o(0x100ff2); + o(0x44 + REG_VALUE(r)*8); + o(0xf024); + vtop->r = r; + } else if (tbt == VT_FLOAT) { + o(0xf0245cd9); /* fstps -0x10(%rsp) */ + /* movss -0x10(%rsp),%xmm0 */ + o(0x100ff3); + o(0x44 + REG_VALUE(r)*8); + o(0xf024); + vtop->r = r; + } + } +} + +/* convert fp to int 't' type */ +void gen_cvt_ftoi(int t) +{ + int ft, bt, size, r; + ft = vtop->type.t; + bt = ft & VT_BTYPE; + if (bt == VT_LDOUBLE) { + gen_cvt_ftof(VT_DOUBLE); + bt = VT_DOUBLE; + } + + gv(RC_FLOAT); + if (t != VT_INT) + size = 8; + else + size = 4; + + r = get_reg(RC_INT); + if (bt == VT_FLOAT) { + o(0xf3); + } else if (bt == VT_DOUBLE) { + o(0xf2); + } else { + assert(0); + } + orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */ + o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8); + vtop->r = r; +} + +/* computed goto support */ +void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* Save the stack pointer onto the stack and return the location of its address */ +ST_FUNC void gen_vla_sp_save(int addr) { + /* mov %rsp,addr(%rbp)*/ + gen_modrm64(0x89, TREG_RSP, VT_LOCAL, NULL, addr); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + gen_modrm64(0x8b, TREG_RSP, VT_LOCAL, NULL, addr); +} + +#ifdef TCC_TARGET_PE +/* Save result of gen_vla_alloc onto the stack */ +ST_FUNC void gen_vla_result(int addr) { + /* mov %rax,addr(%rbp)*/ + gen_modrm64(0x89, TREG_RAX, VT_LOCAL, NULL, addr); +} +#endif + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { +#ifdef TCC_TARGET_PE + /* alloca does more than just adjust %rsp on Windows */ + vpush_global_sym(&func_old_type, TOK_alloca); + vswap(); /* Move alloca ref past allocation size */ + gfunc_call(1); +#else + int r; + r = gv(RC_INT); /* allocation size */ + /* sub r,%rsp */ + o(0x2b48); + o(0xe0 | REG_VALUE(r)); + /* We align to 16 bytes rather than align */ + /* and ~15, %rsp */ + o(0xf0e48348); + vpop(); +#endif +} + + +/* end of x86-64 code generator */ +/*************************************************************/ +#endif /* ! TARGET_DEFS_ONLY */ +/******************************************************/ diff --git a/05/tcc-final-old/x86_64-link.c b/05/tcc-final-old/x86_64-link.c new file mode 100644 index 0000000..a96144c --- /dev/null +++ b/05/tcc-final-old/x86_64-link.c @@ -0,0 +1,298 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_X86_64 + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_X86_64_32S +#define R_DATA_PTR R_X86_64_64 +#define R_JMP_SLOT R_X86_64_JUMP_SLOT +#define R_GLOB_DAT R_X86_64_GLOB_DAT +#define R_COPY R_X86_64_COPY +#define R_RELATIVE R_X86_64_RELATIVE + +#define R_NUM R_X86_64_NUM + +#define ELF_START_ADDR 0x400000 +#define ELF_PAGE_SIZE 0x200000 + +#define PCRELATIVE_DLLPLT 1 +#define RELOCATE_DLLPLT 1 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_X86_64_32: + case R_X86_64_32S: + case R_X86_64_64: + case R_X86_64_GOTPC32: + case R_X86_64_GOTPC64: + case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + case R_X86_64_REX_GOTPCRELX: + case R_X86_64_GOTTPOFF: + case R_X86_64_GOT32: + case R_X86_64_GOT64: + case R_X86_64_GLOB_DAT: + case R_X86_64_COPY: + case R_X86_64_RELATIVE: + case R_X86_64_GOTOFF64: + return 0; + + case R_X86_64_PC32: + case R_X86_64_PC64: + case R_X86_64_PLT32: + case R_X86_64_PLTOFF64: + case R_X86_64_JUMP_SLOT: + return 1; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_X86_64_GLOB_DAT: + case R_X86_64_JUMP_SLOT: + case R_X86_64_COPY: + case R_X86_64_RELATIVE: + return NO_GOTPLT_ENTRY; + + /* The following relocs wouldn't normally need GOT or PLT + slots, but we need them for simplicity in the link + editor part. See our caller for comments. */ + case R_X86_64_32: + case R_X86_64_32S: + case R_X86_64_64: + case R_X86_64_PC32: + case R_X86_64_PC64: + return AUTO_GOTPLT_ENTRY; + + case R_X86_64_GOTTPOFF: + return BUILD_GOT_ONLY; + + case R_X86_64_GOT32: + case R_X86_64_GOT64: + case R_X86_64_GOTPC32: + case R_X86_64_GOTPC64: + case R_X86_64_GOTOFF64: + case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + case R_X86_64_REX_GOTPCRELX: + case R_X86_64_PLT32: + case R_X86_64_PLTOFF64: + return ALWAYS_GOTPLT_ENTRY; + } + + tcc_error ("Unknown relocation type: %d", reloc_type); + return -1; +} + +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + int modrm; + unsigned plt_offset, relofs; + + modrm = 0x25; + + /* empty PLT: create PLT0 entry that pushes the library identifier + (GOT + PTR_SIZE) and jumps to ld.so resolution routine + (GOT + 2 * PTR_SIZE) */ + if (plt->data_offset == 0) { + p = section_ptr_add(plt, 16); + p[0] = 0xff; /* pushl got + PTR_SIZE */ + p[1] = modrm + 0x10; + write32le(p + 2, PTR_SIZE); + p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */ + p[7] = modrm; + write32le(p + 8, PTR_SIZE * 2); + } + plt_offset = plt->data_offset; + + /* The PLT slot refers to the relocation entry it needs via offset. + The reloc entry is created below, so its offset is the current + data_offset */ + relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0; + + /* Jump to GOT entry where ld.so initially put the address of ip + 4 */ + p = section_ptr_add(plt, 16); + p[0] = 0xff; /* jmp *(got + x) */ + p[1] = modrm; + write32le(p + 2, got_offset); + p[6] = 0x68; /* push $xxx */ + /* On x86-64, the relocation is referred to by _index_ */ + write32le(p + 7, relofs / sizeof (ElfW_Rel)); + p[11] = 0xe9; /* jmp plt_start */ + write32le(p + 12, -(plt->data_offset)); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + int x = s1->got->sh_addr - s1->plt->sh_addr - 6; + add32le(p + 2, x); + add32le(p + 8, x - 6); + p += 16; + while (p < p_end) { + add32le(p + 2, x + s1->plt->data - p); + p += 16; + } + } +} + +static ElfW_Rel *qrel; /* ptr to next reloc entry reused */ + +void relocate_init(Section *sr) +{ + qrel = (ElfW_Rel *) sr->data; +} + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + int sym_index, esym_index; + + sym_index = ELFW(R_SYM)(rel->r_info); + + switch (type) { + case R_X86_64_64: + if (s1->output_type == TCC_OUTPUT_DLL) { + esym_index = s1->sym_attrs[sym_index].dyn_index; + qrel->r_offset = rel->r_offset; + if (esym_index) { + qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64); + qrel->r_addend = rel->r_addend; + qrel++; + break; + } else { + qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE); + qrel->r_addend = read64le(ptr) + val; + qrel++; + } + } + add64le(ptr, val); + break; + case R_X86_64_32: + case R_X86_64_32S: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* XXX: this logic may depend on TCC's codegen + now TCC uses R_X86_64_32 even for a 64bit pointer */ + qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE); + /* Use sign extension! */ + qrel->r_addend = (int)read32le(ptr) + val; + qrel++; + } + add32le(ptr, val); + break; + + case R_X86_64_PC32: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* DLL relocation */ + esym_index = s1->sym_attrs[sym_index].dyn_index; + if (esym_index) { + qrel->r_offset = rel->r_offset; + qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32); + /* Use sign extension! */ + qrel->r_addend = (int)read32le(ptr) + rel->r_addend; + qrel++; + break; + } + } + goto plt32pc32; + + case R_X86_64_PLT32: + /* fallthrough: val already holds the PLT slot address */ + + plt32pc32: + { + long long diff; + diff = (long long)val - addr; + if (diff < -2147483648LL || diff > 2147483647LL) { + tcc_error("internal error: relocation failed"); + } + add32le(ptr, diff); + } + break; + + case R_X86_64_PLTOFF64: + add64le(ptr, val - s1->got->sh_addr + rel->r_addend); + break; + + case R_X86_64_PC64: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* DLL relocation */ + esym_index = s1->sym_attrs[sym_index].dyn_index; + if (esym_index) { + qrel->r_offset = rel->r_offset; + qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64); + qrel->r_addend = read64le(ptr) + rel->r_addend; + qrel++; + break; + } + } + add64le(ptr, val - addr); + break; + + case R_X86_64_GLOB_DAT: + case R_X86_64_JUMP_SLOT: + /* They don't need addend */ + write64le(ptr, val - rel->r_addend); + break; + case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + case R_X86_64_REX_GOTPCRELX: + add32le(ptr, s1->got->sh_addr - addr + + s1->sym_attrs[sym_index].got_offset - 4); + break; + case R_X86_64_GOTPC32: + add32le(ptr, s1->got->sh_addr - addr + rel->r_addend); + break; + case R_X86_64_GOTPC64: + add64le(ptr, s1->got->sh_addr - addr + rel->r_addend); + break; + case R_X86_64_GOTTPOFF: + add32le(ptr, val - s1->got->sh_addr); + break; + case R_X86_64_GOT32: + /* we load the got offset */ + add32le(ptr, s1->sym_attrs[sym_index].got_offset); + break; + case R_X86_64_GOT64: + /* we load the got offset */ + add64le(ptr, s1->sym_attrs[sym_index].got_offset); + break; + case R_X86_64_GOTOFF64: + add64le(ptr, val - s1->got->sh_addr); + break; + case R_X86_64_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + break; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/.gitignore b/05/tcc-final/.gitignore new file mode 100644 index 0000000..3bff153 --- /dev/null +++ b/05/tcc-final/.gitignore @@ -0,0 +1,64 @@ +*~ +\#* +.#* +*.o +*.a +*.exe +*.dll +*.obj +*.pdb +*.lib +*.exp +*.log +*.bz2 +*.zip +.gdb_history +a.out +tcc_g +tcc +*-tcc +libtcc*.def + +config*.h +*_.h +config*.mak +config.texi +conftest* +c2str +tags +TAGS +tcc.1 +*.pod +tcc-doc.html +tcc-doc.info + +win32/doc +win32/examples/libtcc_test.c +win32/libtcc +win32/lib/32 +win32/lib/64 +win32/include/float.h +win32/include/stdalign.h +win32/include/stdarg.h +win32/include/stdbool.h +win32/include/stddef.h +win32/include/stdnoreturn.h +win32/include/varargs.h +win32/include/tcclib.h + +tests/tcctest[1234] +tests/tcctest.gcc +tests/*.out* +tests/*.ref +tests/*.txt +tests/*.gcc +tests/*-cc* +tests/*-tcc* +tests/libtcc_test +tests/libtcc_test_mt +tests/asm-c-connect +tests/asm-c-connect-sep +tests/vla_test +tests/hello +tests/tests2/fred.txt +libtcc.dylib diff --git a/05/tcc-final/COPYING b/05/tcc-final/COPYING new file mode 100644 index 0000000..223ede7 --- /dev/null +++ b/05/tcc-final/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/05/tcc-final/Changelog b/05/tcc-final/Changelog new file mode 100644 index 0000000..17f5bde --- /dev/null +++ b/05/tcc-final/Changelog @@ -0,0 +1,439 @@ +Version 0.9.27: + +User interface: +- -x[c|a|n] filetype option (Sergey Korshunoff) +- -P[1], -dD, -dM preprocessor options (Sergey Korshunoff) +- -Wl,-(no-)whole-archive linker option (Reuben Thomas) +- -mms-bitfields option (David Mertens) +- -include option (Michael Matz) +- -mno-sse on x86-64 disables use of SSE instructions +- @listfile support (Vlad Vissoultchev) +- tcc -ar/-impdef - formerly tiny_xxx tools integrated (grischka) +- CPATH, C_INCLUDE_PATH and LIBRARY_PATH environment variables support + (Andrew Aladjev, Urs Janssen) + +Platforms: +- new AARCH64 (arm64) target (Edmund Grimley Evans) +- vastly improved support for ARM hard float calling convention + (Thomas Preud'homme, Daniel Glöckner) +- provide a runtime library for ARM (Thomas Preud'homme) +- many x86_64 ABI fixes incl. XMM register passing and tests (James Lyon) +- ABI tests with native compiler using libtcc (James Lyon) +- UNICODE startup code supports wmain and wWinMain (YX Hao) +- shared libraries for x86_64 (Michael Matz) +- Bootstrap native Windows 32/64 compiler using Cygwin+gcc (Christian Jullien) + +Features: +- VLA (variable length array) improved (James Lyon, Pip Cet) +- import functions by ordinal in .def files on windows (YX Hao) +- x86/x86_64 assembler much improved (Michael Matz) +- simple dead code suppression (Edmund Grimley Evans, Michael Matz, grischka) +- implement round/fmin/fmax etc. math on windows (Avi Halachmi) +- #pragma once support (Sergey Korshunoff, Vlad Vissoultchev, ...) +- switch/case code improved (Zdenek Pavlas) +- ~15% faster by TinyAlloc fast memory allocator (Vlad Vissoultchev) +- standard conforming (and GCC compatible) struct initialization + (Michael Matz) +- bit-field layout made compatible with GCC (Michael Matz) +- UTF8 in string literals supported (Zdenek Pavlas) +_ _Generic(...) supported (Matthias Gatto) + +Licensing: +- TinyCC partly relicensed to MIT license (See RELICENSING file). + +version 0.9.26: + +User interface: +- -MD/-MF (automatically generate dependencies for make) +- -pthread option (same as -D_REENTRANT -lpthread) (Henry Kroll III) +- -m32/-m64 to re-exec cross compiler (Henry Kroll III) +- -Wl, Mimic all GNU -option forms supported by ld (Kirill Smelkov) +- new LIBTCCAPI tcc_set_options() (grischka) + +Platforms: +- Many improvements for x86-64 target (Shinichiro Hamaji, Michael Matz, grischka) +- x86-64 assembler (Frederic Feret) +- Many improvements for ARM target (Daniel Glöckner, Thomas Preud'homme) +- Support WinCE PE ARM (Timo VJ Lahde) +- Support ARM hardfloat calling convention (Thomas Preud'homme) +- Support SELinux (Security-Enhanced Linux) (Henry Kroll III) +- Support Debian GNU/kFreeBSD kernels (Pierre Chifflier) +- Support GNU/Hurd kernels (Thomas Preud'homme) +- Support OSX (tcc -run only) (Milutin Jovanovic) +- Support multiarch configuration (Thomas Preud'homme) +- Support out-of-tree build (Akim Demaille) + +Features: +- C99 variable length arrays (Thomas Preud'homme & Joe Soroka) +- Asm labels for variables and functions (Thomas Preud'homme) +- STT_GNU_IFUNC (Indirect functions as externals) (Thomas Preud'homme) +- More tests (tests2) (Milutin Jovanovic) + +version 0.9.25: + +- first support for x86-64 target (Shinichiro Hamaji) +- support µClibc +- split tcc.c into tcc.h libtcc.c tccpp.c tccgen.c tcc.c +- improved preprocess output with linenumbers and spaces preserved +- tcc_relocate now copies code into user buffer +- fix bitfields with non-int types and in unions +- improve ARM cross-compiling (Daniel Glöckner) +- link stabstr sections from multiple objects +- better (still limited) support for multiple TCCStates + +version 0.9.24: + +- added verbosity levels -v, -vv, -vvv +- Accept standard input as an inputstream (Hanzac Chen) +- Support c89 compilers other than gcc (Hanzac Chen) +- -soname linker option (Marc Andre Tanner) +- Just warn about unknown directives, ignore quotes in #error/#warning +- Define __STDC_VERSION__=199901L (477) +- Switch to newer tccpe.c (includes support for resources) +- Handle backslashes within #include/#error/#warning +- Import changesets (part 4) 428,457,460,467: defines for openbsd etc. +- Use _WIN32 for a windows hosted tcc and define it for the PE target, + otherwise define __unix / __linux (Detlef Riekenberg) +- Import changesets (part 3) 409,410: ARM EABI by Daniel Glöckner +- Some in-between fixes: + TCC -E no longer hangs with macro calls involving newlines. + (next_nomacro1 now advances the read-pointer with TOK_LINEFEED) + Global cast (int g_i = 1LL;) no longer crashes tcc. + (nocode_wanted is initially 1, and only 0 for gen_function) + On win32 now tcc.exe finds 'include' & 'lib' even if itself is in 'bin'. + (new function w32_tcc_lib_path removes 'bin' if detected) + Added quick build batch file for mingw (win32/build-tcc.bat) + Last added case label optimization (455) produced wrong code. Reverted. + +- Import more changesets from Rob Landley's fork (part 2): + 487: Handle long long constants in gen_opic() (Rob Landley) + 484: Handle parentheses within __attribute__((...)) (Rob Landley) + 480: Remove a goto in decl_initializer_alloc (Rob Landley) + 475: Fix dereferences in inline assembly output (Joshua Phillips) + 474: Cast ptrs to ints of different sizes correctly (Joshua Phillips) + 473: Fix size of structs with empty array member (Joshua Phillips) + 470: No warning for && and || with mixed pointers/integers (Rob Landley) + 469: Fix symbol visibility problems in the linker (Vincent Pit) + 468: Allow && and || involving pointer arguments (Rob Landley) + 455: Optimize case labels with no code in between (Zdenek Pavlas) + 450: Implement alloca for x86 (grischka) + 415: Parse unicode escape sequences (Axel Liljencrantz) + 407: Add a simple va_copy() in stdarg.h (Hasso Tepper) + 400: Allow typedef names as symbols (Dave Dodge) + +- Import some changesets from Rob Landley's fork (part 1): + 462: Use LGPL with bcheck.c and il-gen.c + 458: Fix global compound literals (in unary: case '&':) (Andrew Johnson) + 456: Use return code from tcc_output_file in main() (Michael Somos) + 442: Fix indirections with function pointers (***fn)() (grischka) + 441: Fix LL left shift in libtcc1.c:__shldi3 (grischka) + 440: Pass structures and function ptrs through ?: (grischka) + 439: Keep rvalue in bit assignment (bit2 = bit1 = x) (grischka) + 438: Degrade nonportable pointer assignment to warning (grischka) + 437: Call 'saveregs()' before jumping with logical and/or/not (grischka) + 435: Put local static variables into global memory (grischka) + 432/434: Cast double and ptr to bool (grischka) + 420: Zero pad x87 tenbyte long doubles (Felix Nawothnig) + 417: Make 'sizeof' unsigned (Rob Landley) + 397: Fix save_reg for longlongs (Daniel Glöckner) + 396: Fix "invalid relocation entry" problem on ubuntu - (Bernhard Fischer) + +- ignore AS_NEEDED ld command +- mark executable sections as executable when running in memory +- added support for win32 wchar_t (Filip Navara) +- segment override prefix support (Filip Navara) +- normalized slashes in paths (Filip Navara) +- windows style fastcall (Filip Navara) +- support for empty input register section in asm (Filip Navara) +- anonymous union/struct support (Filip Navara) +- fixed parsing of function parameters +- workaround for function pointers in conditional expressions (Dave Dodge) +- initial '-E' option support to use the C preprocessor alone +- discard type qualifiers when comparing function parameters (Dave Dodge) +- Bug fix: A long long value used as a test expression ignores the + upper 32 bits at runtime (Dave Dodge) +- fixed multiple concatenation of PPNUM tokens (initial patch by Dave Dodge) +- fixed multiple typedef specifiers handling +- fixed sign extension in some type conversions (Dave Dodge) + +version 0.9.23: + +- initial PE executable format for windows version (grischka) +- '#pragma pack' support (grischka) +- '#include_next' support (Bernhard Fischer) +- ignore '-pipe' option +- added -f[no-]leading-underscore +- preprocessor function macro parsing fix (grischka) + +version 0.9.22: + +- simple memory optimisations: kernel compilation is 30% faster +- linker symbol definitions fixes +- gcc 3.4 fixes +- fixed value stack full error +- 'packed' attribute support for variables and structure fields +- ignore 'const' and 'volatile' in function prototypes +- allow '_Bool' in bit fields + +version 0.9.21: + +- ARM target support (Daniel Glöckner) +- added '-funsigned-char, '-fsigned-char' and + '-Wimplicit-function-declaration' +- fixed assignment of const struct in struct +- line comment fix (reported by Bertram Felgenhauer) +- initial TMS320C67xx target support (TK) +- win32 configure +- regparm() attribute +- many built-in assembler fixes +- added '.org', '.fill' and '.previous' assembler directives +- '-fno-common' option +- '-Ttext' linker option +- section alignment fixes +- bit fields fixes +- do not generate code for unused inline functions +- '-oformat' linker option. +- added 'binary' output format. + +version 0.9.20: + +- added '-w' option +- added '.gnu.linkonce' ELF sections support +- fixed libc linking when running in memory (avoid 'stat' function + errors). +- extended '-run' option to be able to give several arguments to a C + script. + +version 0.9.19: + +- "alacarte" linking (Dave Long) +- simpler function call +- more strict type checks +- added 'const' and 'volatile' support and associated warnings +- added -Werror, -Wunsupported, -Wwrite-strings, -Wall. +- added __builtin_types_compatible_p() and __builtin_constant_p() +- chars support in assembler (Dave Long) +- .string, .globl, .section, .text, .data and .bss asm directive + support (Dave Long) +- man page generated from tcc-doc.texi +- fixed macro argument substitution +- fixed zero argument macro parsing +- changed license to LGPL +- added -rdynamic option support + +version 0.9.18: + +- header fix (time.h) +- fixed inline asm without operand case +- fixed 'default:' or 'case x:' with '}' after (incorrect C construct accepted + by gcc) +- added 'A' inline asm constraint. + +version 0.9.17: + +- PLT generation fix +- tcc doc fixes (Peter Lund) +- struct parse fix (signaled by Pedro A. Aranda Gutierrez) +- better _Bool lvalue support (signaled by Alex Measday) +- function parameters must be converted to pointers (signaled by Neil Brown) +- sanitized string and character constant parsing +- fixed comment parse (signaled by Damian M Gryski) +- fixed macro function bug (signaled by Philippe Ribet) +- added configure (initial patch by Mitchell N Charity) +- added '-run' and '-v' options (initial patch by vlindos) +- added real date report in __DATE__ and __TIME__ macros + +version 0.9.16: + +- added assembler language support +- added GCC inline asm() support +- fixed multiple variable definitions : uninitialized variables are + created as COMMON symbols. +- optimized macro processing +- added GCC statement expressions support +- added GCC local labels support +- fixed array declaration in old style function parameters +- support casts in static structure initializations +- added various __xxx[__] keywords for GCC compatibility +- ignore __extension__ GCC in an expression or in a type (still not perfect) +- added '? :' GCC extension support + +version 0.9.15: + +- compilation fixes for glibc 2.2, gcc 2.95.3 and gcc 3.2. +- FreeBSD compile fixes. Makefile patches still missing (Carl Drougge). +- fixed file type guessing if '.' is in the path. +- fixed tcc_compile_string() +- add a dummy page in ELF files to fix RX/RW accesses (pageexec at + freemail dot hu). + +version 0.9.14: + +- added #warning. error message if invalid preprocessing directive. +- added CType structure to ease typing (faster parse). +- suppressed secondary hash tables (faster parse). +- rewrote parser by optimizing common cases (faster parse). +- fixed signed long long comparisons. +- fixed 'int a(), b();' declaration case. +- fixed structure init without '{}'. +- correct alignment support in structures. +- empty structures support. +- gcc testsuite now supported. +- output only warning if implicit integer/pointer conversions. +- added static bitfield init. + +version 0.9.13: + +- correct preprocessing token pasting (## operator) in all cases (added + preprocessing number token). +- fixed long long register spill. +- fixed signed long long '>>'. +- removed memory leaks. +- better error handling : processing can continue on link errors. A + custom callback can be added to display error messages. Most + errors do not call exit() now. +- ignore -O, -W, -m and -f options +- added old style function declarations +- added GCC __alignof__ support. +- added GCC typeof support. +- added GCC computed gotos support. +- added stack backtrace in runtime error message. Improved runtime + error position display. + +version 0.9.12: + +- more fixes for || and && handling. +- improved '? :' type handling. +- fixed bound checking generation with structures +- force '#endif' to be in same file as matching '#if' +- #include file optimization with '#ifndef #endif' construct detection +- macro handling optimization +- added tcc_relocate() and tcc_get_symbol() in libtcc. + +version 0.9.11: + +- stdarg.h fix for double type (thanks to Philippe Ribet). +- correct white space characters and added MSDOS newline support. +- fixed invalid implicit function call type declaration. +- special macros such as __LINE__ are defined if tested with defined(). +- fixed '!' operator with relocated address. +- added symbol + offset relocation (fixes some static variable initializers) +- '-l' option can be specified anywhere. '-c' option yields default + output name. added '-r' option for relocatable output. +- fixed '\nnn' octal parsing. +- fixed local extern variables declarations. + +version 0.9.10: + +- fixed lvalue type when saved in local stack. +- fixed '#include' syntax when using macros. +- fixed '#line' bug. +- removed size limit on strings. Unified string constants handling + with variable declarations. +- added correct support for '\xX' in wchar_t strings. +- added support for bound checking in generated executables +- fixed -I include order. +- fixed incorrect function displayed in runtime error. + +version 0.9.9: + +- fixed preprocessor expression parsing for #if/#elif. +- relocated debug info (.stab section). +- relocated bounds info (.bounds section). +- fixed cast to char of char constants ('\377' is -1 instead of 255) +- fixed implicit cast for unary plus. +- strings and '__func__' have now 'char[]' type instead of 'char *' + (fixes sizeof() return value). +- added __start_xxx and __stop_xxx symbols in linker. +- better DLL creation support (option -shared begins to work). +- ELF sections and hash tables are resized dynamically. +- executables and DLLs are stripped by default. + +version 0.9.8: + +- First version of full ELF linking support (generate objects, static + executable, dynamic executable, dynamic libraries). Dynamic library + support is not finished (need PIC support in compiler and some + patches in symbol exporting). +- First version of ELF loader for object (.o) and archive (.a) files. +- Support of simple GNU ld scripts (GROUP and FILE commands) +- Separated runtime library and bound check code from TCC (smaller + compiler core). +- fixed register reload in float compare. +- fixed implicit char/short to int casting. +- allow array type for address of ('&') operator. +- fixed unused || or && result. +- added GCC style variadic macro support. +- optimized bound checking code for array access. +- tcc includes are now in $(prefix)/lib/tcc/include. +- more command line options - more consistent handling of multiple + input files. +- added tcc man page (thanks to Cyril Bouthors). +- uClibc Makefile update +- converted documentation to texinfo format. +- added developper's guide in documentation. + +version 0.9.7: + +- added library API for easy dynamic compilation (see libtcc.h - first + draft). +- fixed long long register spill bug. +- fixed '? :' register spill bug. + +version 0.9.6: + +- added floating point constant propagation (fixes negative floating + point constants bug). + +version 0.9.5: + + - uClibc patches (submitted by Alfonso Martone). + - error reporting fix + - added CONFIG_TCC_BCHECK to get smaller code if needed. + +version 0.9.4: + + - windows port (currently cannot use -g, -b and dll functions). + - faster and simpler I/O handling. + - '-D' option works in all cases. + - preprocessor fixes (#elif and empty macro args) + - floating point fixes + - first code for CIL generation (does not work yet) + +version 0.9.3: + + - better and smaller code generator. + - full ISOC99 64 bit 'long long' support. + - full 32 bit 'float', 64 bit 'double' and 96 bit 'long double' support. + - added '-U' option. + - added assembly sections support. + - even faster startup time by mmaping sections instead of mallocing them. + - added GNUC __attribute__ keyword support (currently supports + 'section' and 'aligned' attributes). + - added ELF file output (only usable for debugging now) + - added debug symbol generation (STAB format). + - added integrated runtime error analysis ('-g' option: print clear + run time error messages instead of "Segmentation fault"). + - added first version of tiny memory and bound checker ('-b' option). + +version 0.9.2: + + - even faster parsing. + - various syntax parsing fixes. + - fixed external relocation handling for variables or functions pointers. + - better function pointers type handling. + - can compile multiple files (-i option). + - ANSI C bit fields are supported. + - beginning of float/double/long double support. + - beginning of long long support. + +version 0.9.1: + + - full ISOC99 initializers handling. + - compound literals. + - structures handle in assignments and as function param or return value. + - wide chars and strings. + - macro bug fix + +version 0.9: + - initial version. diff --git a/05/tcc-final/CodingStyle b/05/tcc-final/CodingStyle new file mode 100644 index 0000000..93d1324 --- /dev/null +++ b/05/tcc-final/CodingStyle @@ -0,0 +1,71 @@ + +In general, use the same coding style as the surrounding code. + +However, do not make any unnecessary changes as that complicates +the VCS (git) history and makes it harder to merge patches. So +do not modify code just to make it conform to a coding style. + + Indentation + +Turn on a "fill tabs with spaces" option in your editor. + +Remove tabs and trailing spaces from any lines that are modified. + +Note that some files are indented with 2 spaces (when they +have large indentation) while most are indented with 4 spaces. + + Language + +TCC is mostly implemented in C90. Do not use any non-C90 features +that are not already in use. + +Non-C90 features currently in use, as revealed by +./configure --extra-cflags="-std=c90 -Wpedantic": + +- long long (including "LL" constants) +- inline +- very long string constants +- assignment between function pointer and 'void *' +- "//" comments +- empty macro arguments (DEF_ASMTEST in i386-tok.h) +- unnamed struct and union fields (in struct Sym), a C11 feature + + Testing + +A simple "make test" is sufficient for some simple changes. However, +before committing a change consider performing some of the following +additional tests: + +- Build and run "make test" on several architectures. + +- Build with ./configure --enable-cross. + +- If the generation of relocations has been changed, try compiling + with TCC and linking with GCC/Clang. If the linker has been + modified, try compiling with GCC/Clang and linking with TCC. + +- Test with ASan/UBSan to detect memory corruption and undefined behaviour: + +make clean +./configure +make +make test +cp libtcc.a libtcc.a.hide + +make clean +./configure --extra-cflags="-fsanitize=address,undefined -g" +make +cp libtcc.a.hide libtcc.a +make test + +- Test with Valgrind to detect some uses of uninitialised values: + +make clean +./configure +make +# On Intel, because Valgrind does floating-point arithmetic differently: +( cd tests && gcc -I.. tcctest.c && valgrind -q ./a.out > test.ref ) +make test TCC="valgrind -q --leak-check=full `pwd`/tcc -B`pwd` -I`pwd`" + + (Because of how VLAs are implemented, invalid reads are expected + with 79_vla_continue.) diff --git a/05/tcc-final/Makefile b/05/tcc-final/Makefile new file mode 100644 index 0000000..211fb49 --- /dev/null +++ b/05/tcc-final/Makefile @@ -0,0 +1,489 @@ +# -------------------------------------------------------------------------- +# +# Tiny C Compiler Makefile +# + +ifndef TOP + TOP = . + INCLUDED = no +endif + +ifeq ($(findstring $(MAKECMDGOALS),clean distclean),) + include $(TOP)/config.mak +endif + +ifeq (-$(GCC_MAJOR)-$(findstring $(GCC_MINOR),56789)-,-4--) + CFLAGS += -D_FORTIFY_SOURCE=0 +endif + +LIBTCC = libtcc.a +LIBTCC1 = libtcc1.a +LINK_LIBTCC = +LIBS = +CFLAGS += -I$(TOP) +CFLAGS += $(CPPFLAGS) +VPATH = $(TOPSRC) + +ifdef CONFIG_WIN32 + CFG = -win + ifneq ($(CONFIG_static),yes) + LIBTCC = libtcc$(DLLSUF) + LIBTCCDEF = libtcc.def + endif + NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32) +else + CFG = -unx + LIBS=-lm -lpthread + ifneq ($(CONFIG_ldl),no) + LIBS+=-ldl + endif + # make libtcc as static or dynamic library? + ifeq ($(CONFIG_static),no) + LIBTCC=libtcc$(DLLSUF) + export LD_LIBRARY_PATH := $(CURDIR)/$(TOP) + ifneq ($(CONFIG_rpath),no) + ifndef CONFIG_OSX + LINK_LIBTCC += -Wl,-rpath,"$(libdir)" + else + # macOS doesn't support env-vars libdir out of the box - which we need for + # `make test' when libtcc.dylib is used (configure --disable-static), so + # we bake a relative path into the binary. $libdir is used after install. + LINK_LIBTCC += -Wl,-rpath,"@executable_path/$(TOP)" -Wl,-rpath,"$(libdir)" + DYLIBVER += -current_version $(VERSION) + DYLIBVER += -compatibility_version $(VERSION) + endif + endif + endif + NATIVE_TARGET = $(ARCH) + ifdef CONFIG_OSX + NATIVE_TARGET = $(ARCH)-osx + ifneq ($(CC_NAME),tcc) + LDFLAGS += -flat_namespace -undefined warning + endif + export MACOSX_DEPLOYMENT_TARGET := 10.6 + endif +endif + +# run local version of tcc with local libraries and includes +TCCFLAGS-unx = -B$(TOP) -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP) +TCCFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP) -L$(TOP) +TCCFLAGS = $(TCCFLAGS$(CFG)) +TCC = $(TOP)/tcc$(EXESUF) $(TCCFLAGS) + +CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC -DTCC_PROFILE +LIBS_P = $(LIBS) +LDFLAGS_P = $(LDFLAGS) + +CONFIG_$(ARCH) = yes +NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386 +NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64 +NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE +NATIVE_DEFINES_$(CONFIG_OSX) += -DTCC_TARGET_MACHO +NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC +NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL +NATIVE_DEFINES_$(CONFIG_libgcc) += -DCONFIG_USE_LIBGCC +NATIVE_DEFINES_$(CONFIG_selinux) += -DHAVE_SELINUX +NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM +NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT +NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI +NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP +NATIVE_DEFINES_$(CONFIG_arm64) += -DTCC_TARGET_ARM64 +NATIVE_DEFINES_$(CONFIG_riscv64) += -DTCC_TARGET_RISCV64 +NATIVE_DEFINES_$(CONFIG_BSD) += -DTARGETOS_$(TARGETOS) +NATIVE_DEFINES_$(CONFIG_pie) += -DCONFIG_TCC_PIE +NATIVE_DEFINES_no_$(CONFIG_bcheck) += -DCONFIG_TCC_BCHECK=0 +NATIVE_DEFINES_no_$(CONFIG_backtrace) += -DCONFIG_TCC_BACKTRACE=0 +NATIVE_DEFINES += $(NATIVE_DEFINES_yes) $(NATIVE_DEFINES_no_no) + +DEF-i386 = -DTCC_TARGET_I386 +DEF-i386-win32 = -DTCC_TARGET_I386 -DTCC_TARGET_PE +DEF-i386-OpenBSD = $(DEF-i386) -DTARGETOS_OpenBSD +DEF-x86_64 = -DTCC_TARGET_X86_64 +DEF-x86_64-win32 = -DTCC_TARGET_X86_64 -DTCC_TARGET_PE +DEF-x86_64-osx = -DTCC_TARGET_X86_64 -DTCC_TARGET_MACHO +DEF-arm-fpa = -DTCC_TARGET_ARM +DEF-arm-fpa-ld = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12 +DEF-arm-vfp = -DTCC_TARGET_ARM -DTCC_ARM_VFP +DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI +DEF-arm-eabihf = $(DEF-arm-eabi) -DTCC_ARM_HARDFLOAT +DEF-arm = $(DEF-arm-eabihf) +DEF-arm-NetBSD = $(DEF-arm-eabihf) -DTARGETOS_NetBSD +DEF-arm-wince = $(DEF-arm-eabihf) -DTCC_TARGET_PE +DEF-arm64 = -DTCC_TARGET_ARM64 +DEF-arm64-osx = $(DEF-arm64) -DTCC_TARGET_MACHO +DEF-arm64-FreeBSD = $(DEF-arm64) -DTARGETOS_FreeBSD +DEF-arm64-NetBSD = $(DEF-arm64) -DTARGETOS_NetBSD +DEF-arm64-OpenBSD = $(DEF-arm64) -DTARGETOS_OpenBSD +DEF-riscv64 = -DTCC_TARGET_RISCV64 +DEF-c67 = -DTCC_TARGET_C67 -w # disable warnigs +DEF-x86_64-FreeBSD = $(DEF-x86_64) -DTARGETOS_FreeBSD +DEF-x86_64-NetBSD = $(DEF-x86_64) -DTARGETOS_NetBSD +DEF-x86_64-OpenBSD = $(DEF-x86_64) -DTARGETOS_OpenBSD + +DEF-$(NATIVE_TARGET) = $(NATIVE_DEFINES) + +ifeq ($(INCLUDED),no) +# -------------------------------------------------------------------------- +# running top Makefile + +PROGS = tcc$(EXESUF) +TCCLIBS = $(LIBTCCDEF) $(LIBTCC) $(LIBTCC1) +TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info + +all: $(PROGS) $(TCCLIBS) $(TCCDOCS) + +# cross compiler targets to build +TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67 +TCC_X += riscv64 arm64-osx +# TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi + +# cross libtcc1.a targets to build +LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince +LIBTCC1_X += riscv64 arm64-osx + +PROGS_CROSS = $(foreach X,$(TCC_X),$X-tcc$(EXESUF)) +LIBTCC1_CROSS = $(foreach X,$(LIBTCC1_X),$X-libtcc1.a) + +# build cross compilers & libs +cross: $(LIBTCC1_CROSS) $(PROGS_CROSS) + +# build specific cross compiler & lib +cross-%: %-tcc$(EXESUF) %-libtcc1.a ; + +install: ; @$(MAKE) --no-print-directory install$(CFG) +install-strip: ; @$(MAKE) --no-print-directory install$(CFG) CONFIG_strip=yes +uninstall: ; @$(MAKE) --no-print-directory uninstall$(CFG) + +ifdef CONFIG_cross +all : cross +endif + +# -------------------------------------------- + +T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown) +X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-) + +DEFINES += $(DEF-$T) $(DEF-all) +DEFINES += $(if $(ROOT-$T),-DCONFIG_SYSROOT="\"$(ROOT-$T)\"") +DEFINES += $(if $(CRT-$T),-DCONFIG_TCC_CRTPREFIX="\"$(CRT-$T)\"") +DEFINES += $(if $(LIB-$T),-DCONFIG_TCC_LIBPATHS="\"$(LIB-$T)\"") +DEFINES += $(if $(INC-$T),-DCONFIG_TCC_SYSINCLUDEPATHS="\"$(INC-$T)\"") +DEFINES += $(DEF-$(or $(findstring win,$T),unx)) + +ifneq ($(X),) +ifeq ($(CONFIG_WIN32),yes) +DEF-win += -DTCC_LIBTCC1="\"$(X)libtcc1.a\"" +DEF-unx += -DTCC_LIBTCC1="\"lib/$(X)libtcc1.a\"" +else +DEF-all += -DTCC_LIBTCC1="\"$(X)libtcc1.a\"" +DEF-win += -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" +endif +endif + +# include custom configuration (see make help) +-include config-extra.mak + +CORE_FILES = tcc.c tcctools.c libtcc.c tccpp.c tccgen.c tccdbg.c tccelf.c tccasm.c tccrun.c +CORE_FILES += tcc.h config.h libtcc.h tcctok.h +i386_FILES = $(CORE_FILES) i386-gen.c i386-link.c i386-asm.c i386-asm.h i386-tok.h +i386-win32_FILES = $(i386_FILES) tccpe.c +x86_64_FILES = $(CORE_FILES) x86_64-gen.c x86_64-link.c i386-asm.c x86_64-asm.h +x86_64-win32_FILES = $(x86_64_FILES) tccpe.c +x86_64-osx_FILES = $(x86_64_FILES) tccmacho.c +arm_FILES = $(CORE_FILES) arm-gen.c arm-link.c arm-asm.c arm-tok.h +arm-wince_FILES = $(arm_FILES) tccpe.c +arm-eabihf_FILES = $(arm_FILES) +arm-fpa_FILES = $(arm_FILES) +arm-fpa-ld_FILES = $(arm_FILES) +arm-vfp_FILES = $(arm_FILES) +arm-eabi_FILES = $(arm_FILES) +arm-eabihf_FILES = $(arm_FILES) +arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c arm64-asm.c +arm64-osx_FILES = $(arm64_FILES) tccmacho.c +c67_FILES = $(CORE_FILES) c67-gen.c c67-link.c tcccoff.c +riscv64_FILES = $(CORE_FILES) riscv64-gen.c riscv64-link.c riscv64-asm.c + +TCCDEFS_H$(subst yes,,$(CONFIG_predefs)) = tccdefs_.h + +# libtcc sources +LIBTCC_SRC = $(filter-out tcc.c tcctools.c,$(filter %.c,$($T_FILES))) + +ifeq ($(ONE_SOURCE),yes) +LIBTCC_OBJ = $(X)libtcc.o +LIBTCC_INC = $($T_FILES) +TCC_FILES = $(X)tcc.o +tcc.o : DEFINES += -DONE_SOURCE=0 +$(X)tcc.o $(X)libtcc.o : $(TCCDEFS_H) +else +LIBTCC_OBJ = $(patsubst %.c,$(X)%.o,$(LIBTCC_SRC)) +LIBTCC_INC = $(filter %.h %-gen.c %-link.c,$($T_FILES)) +TCC_FILES = $(X)tcc.o $(LIBTCC_OBJ) +$(TCC_FILES) : DEFINES += -DONE_SOURCE=0 +$(X)tccpp.o : $(TCCDEFS_H) +endif + +GITHASH := $(shell git rev-parse >/dev/null 2>&1 && git rev-parse --short HEAD || echo no) +ifneq ($(GITHASH),no) +DEF_GITHASH := -DTCC_GITHASH="\"$(shell git rev-parse --abbrev-ref HEAD):$(GITHASH)$(shell git diff --quiet || echo '-mod')\"" +endif + +ifeq ($(CONFIG_debug),yes) +CFLAGS += -g +LDFLAGS += -g +else +ifndef CONFIG_OSX +LDFLAGS += -s +endif +endif + +# convert "include/tccdefs.h" to "tccdefs_.h" +%_.h : include/%.h conftest.c + $S$(CC) -DC2STR $(filter %.c,$^) -o c2str.exe && ./c2str.exe $< $@ + +# target specific object rule +$(X)%.o : %.c $(LIBTCC_INC) + $S$(CC) -o $@ -c $< $(DEFINES) $(CFLAGS) + +# additional dependencies +$(X)tcc.o : tcctools.c +$(X)tcc.o : DEFINES += $(DEF_GITHASH) + +# Host Tiny C Compiler +tcc$(EXESUF): tcc.o $(LIBTCC) + $S$(CC) -o $@ $^ $(LIBS) $(LDFLAGS) $(LINK_LIBTCC) + +# Cross Tiny C Compilers +# (the TCCDEFS_H dependency is only necessary for parallel makes, +# ala 'make -j x86_64-tcc i386-tcc tcc', which would create multiple +# c2str.exe and tccdefs_.h files in parallel, leading to access errors. +# This forces it to be made only once. Make normally tracks multiple paths +# to the same goals and only remakes it once, but that doesn't work over +# sub-makes like in this target) +%-tcc$(EXESUF): $(TCCDEFS_H) FORCE + @$(MAKE) --no-print-directory $@ CROSS_TARGET=$* ONE_SOURCE=$(or $(ONE_SOURCE),yes) + +$(CROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES) + $S$(CC) -o $@ $^ $(LIBS) $(LDFLAGS) + +# profiling version +tcc_p$(EXESUF): $($T_FILES) + $S$(CC) -o $@ $< $(DEFINES) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P) + +# static libtcc library +libtcc.a: $(LIBTCC_OBJ) + $S$(AR) rcs $@ $^ + +# dynamic libtcc library +libtcc.so: $(LIBTCC_OBJ) + $S$(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDFLAGS) + +libtcc.so: CFLAGS+=-fPIC +libtcc.so: LDFLAGS+=-fPIC + +# OSX dynamic libtcc library +libtcc.dylib: $(LIBTCC_OBJ) + $S$(CC) -dynamiclib $(DYLIBVER) -install_name @rpath/$@ -o $@ $^ $(LDFLAGS) + +# OSX libtcc.dylib (without rpath/ prefix) +libtcc.osx: $(LIBTCC_OBJ) + $S$(CC) -shared -install_name libtcc.dylib -o libtcc.dylib $^ $(LDFLAGS) + +# windows dynamic libtcc library +libtcc.dll : $(LIBTCC_OBJ) + $S$(CC) -shared -o $@ $^ $(LDFLAGS) +libtcc.dll : DEFINES += -DLIBTCC_AS_DLL + +# import file for windows libtcc.dll +libtcc.def : libtcc.dll tcc$(EXESUF) + $S$(XTCC) -impdef $< -o $@ +XTCC ?= ./tcc$(EXESUF) + +# TinyCC runtime libraries +libtcc1.a : tcc$(EXESUF) FORCE + @$(MAKE) -C lib + +# Cross libtcc1.a +%-libtcc1.a : %-tcc$(EXESUF) FORCE + @$(MAKE) -C lib CROSS_TARGET=$* + +.PRECIOUS: %-libtcc1.a +FORCE: + +run-if = $(if $(shell which $1),$S $1 $2) +S = $(if $(findstring yes,$(SILENT)),@$(info * $@)) + +# -------------------------------------------------------------------------- +# documentation and man page +tcc-doc.html: tcc-doc.texi + $(call run-if,makeinfo,--no-split --html --number-sections -o $@ $<) + +tcc-doc.info: tcc-doc.texi + $(call run-if,makeinfo,$< || true) + +tcc.1 : tcc-doc.pod + $(call run-if,pod2man,--section=1 --center="Tiny C Compiler" \ + --release="$(VERSION)" $< >$@ && rm -f $<) +%.pod : %.texi + $(call run-if,perl,$(TOPSRC)/texi2pod.pl $< $@) + +doc : $(TCCDOCS) + +# -------------------------------------------------------------------------- +# install + +INSTALL = install -m644 +INSTALLBIN = install -m755 $(STRIP_$(CONFIG_strip)) +STRIP_yes = -s + +LIBTCC1_W = $(filter %-win32-libtcc1.a %-wince-libtcc1.a,$(LIBTCC1_CROSS)) +LIBTCC1_U = $(filter-out $(LIBTCC1_W),$(LIBTCC1_CROSS)) +IB = $(if $1,$(IM) mkdir -p $2 && $(INSTALLBIN) $1 $2) +IBw = $(call IB,$(wildcard $1),$2) +IF = $(if $1,$(IM) mkdir -p $2 && $(INSTALL) $1 $2) +IFw = $(call IF,$(wildcard $1),$2) +IR = $(IM) mkdir -p $2 && cp -r $1/. $2 +IM = $(info -> $2 : $1)@ + +B_O = bcheck.o bt-exe.o bt-log.o bt-dll.o + +# install progs & libs +install-unx: + $(call IBw,$(PROGS) $(PROGS_CROSS),"$(bindir)") + $(call IFw,$(LIBTCC1) $(B_O) $(LIBTCC1_U),"$(tccdir)") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include") + $(call $(if $(findstring .so,$(LIBTCC)),IBw,IFw),$(LIBTCC),"$(libdir)") + $(call IF,$(TOPSRC)/libtcc.h,"$(includedir)") + $(call IFw,tcc.1,"$(mandir)/man1") + $(call IFw,tcc-doc.info,"$(infodir)") + $(call IFw,tcc-doc.html,"$(docdir)") +ifneq "$(wildcard $(LIBTCC1_W))" "" + $(call IFw,$(TOPSRC)/win32/lib/*.def $(LIBTCC1_W),"$(tccdir)/win32/lib") + $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/win32/include") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/win32/include") +endif + +# uninstall +uninstall-unx: + @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS),"$(bindir)/$P") + @rm -fv "$(libdir)/libtcc.a" "$(libdir)/libtcc.so" "$(libdir)/libtcc.dylib" "$(includedir)/libtcc.h" + @rm -fv "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info" + @rm -fv "$(docdir)/tcc-doc.html" + @rm -frv "$(tccdir)" + +# install progs & libs on windows +install-win: + $(call IBw,$(PROGS) $(PROGS_CROSS) $(subst libtcc.a,,$(LIBTCC)),"$(bindir)") + $(call IF,$(TOPSRC)/win32/lib/*.def,"$(tccdir)/lib") + $(call IFw,libtcc1.a $(B_O) $(LIBTCC1_W),"$(tccdir)/lib") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include") + $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/include") + $(call IR,$(TOPSRC)/win32/examples,"$(tccdir)/examples") + $(call IF,$(TOPSRC)/tests/libtcc_test.c,"$(tccdir)/examples") + $(call IFw,$(TOPSRC)/libtcc.h $(subst .dll,.def,$(LIBTCC)),"$(libdir)") + $(call IFw,$(TOPSRC)/win32/tcc-win32.txt tcc-doc.html,"$(docdir)") +ifneq "$(wildcard $(LIBTCC1_U))" "" + $(call IFw,$(LIBTCC1_U),"$(tccdir)/lib") + $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/lib/include") +endif + +# the msys-git shell works to configure && make except it does not have install +ifeq ($(CONFIG_WIN32)-$(shell which install || echo no),yes-no) +install-win : INSTALL = cp +install-win : INSTALLBIN = cp +endif + +# uninstall on windows +uninstall-win: + @rm -fv $(foreach P,libtcc.dll $(PROGS) *-tcc.exe,"$(bindir)"/$P) + @rm -fr $(foreach P,doc examples include lib libtcc,"$(tccdir)/$P"/*) + @rm -frv $(foreach P,doc examples include lib libtcc,"$(tccdir)/$P") + +# -------------------------------------------------------------------------- +# other stuff + +TAGFILES = *.[ch] include/*.h lib/*.[chS] +tags : ; ctags $(TAGFILES) +# cannot have both tags and TAGS on windows +ETAGS : ; etags $(TAGFILES) + +# create release tarball from *current* git branch (including tcc-doc.html +# and converting two files to CRLF) +TCC-VERSION = tcc-$(VERSION) +TCC-VERSION = tinycc-mob-$(shell git rev-parse --short=7 HEAD) +tar: tcc-doc.html + mkdir -p $(TCC-VERSION) + ( cd $(TCC-VERSION) && git --git-dir ../.git checkout -f ) + cp tcc-doc.html $(TCC-VERSION) + for f in tcc-win32.txt build-tcc.bat ; do \ + cat win32/$$f | sed 's,\(.*\),\1\r,g' > $(TCC-VERSION)/win32/$$f ; \ + done + tar cjf $(TCC-VERSION).tar.bz2 $(TCC-VERSION) + rm -rf $(TCC-VERSION) + git reset + +config.mak: + $(if $(wildcard $@),,@echo "Please run ./configure." && exit 1) + +# run all tests +test: + @$(MAKE) -C tests +# run test(s) from tests2 subdir (see make help) +tests2.%: + @$(MAKE) -C tests/tests2 $@ + +testspp.%: + @$(MAKE) -C tests/pp $@ + +clean: + @rm -f tcc$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF) tags ETAGS *.pod + @rm -f *.o *.a *.so* *.out *.log lib*.def *.exe *.dll a.out *.dylib *_.h + @$(MAKE) -s -C lib $@ + @$(MAKE) -s -C tests $@ + +distclean: clean + @rm -fv config.h config.mak config.texi + @rm -fv $(TCCDOCS) + +.PHONY: all clean test tar tags ETAGS doc distclean install uninstall FORCE + +help: + @echo "make" + @echo " build native compiler (from separate objects)" + @echo "" + @echo "make cross" + @echo " build cross compilers (from one source)" + @echo "" + @echo "make ONE_SOURCE=no/yes SILENT=no/yes" + @echo " force building from separate/one object(s), less/more silently" + @echo "" + @echo "make cross-TARGET" + @echo " build one specific cross compiler for 'TARGET'. Currently supported:" + @echo " $(wordlist 1,6,$(TCC_X))" + @echo " $(wordlist 7,99,$(TCC_X))" + @echo "" + @echo "make test" + @echo " run all tests" + @echo "" + @echo "make tests2.all / make tests2.37 / make tests2.37+" + @echo " run all/single test(s) from tests2, optionally update .expect" + @echo "" + @echo "make testspp.all / make testspp.17" + @echo " run all/single test(s) from tests/pp" + @echo "" + @echo "Other supported make targets:" + @echo " install install-strip doc clean tags ETAGS tar distclean help" + @echo "" + @echo "Custom configuration:" + @echo " The makefile includes a file 'config-extra.mak' if it is present." + @echo " This file may contain some custom configuration. For example:" + @echo " NATIVE_DEFINES += -D..." + @echo " Or for example to configure the search paths for a cross-compiler" + @echo " that expects the linux files in /i386-linux:" + @echo " ROOT-i386 = {B}/i386-linux" + @echo " CRT-i386 = {B}/i386-linux/usr/lib" + @echo " LIB-i386 = {B}/i386-linux/lib:{B}/i386-linux/usr/lib" + @echo " INC-i386 = {B}/lib/include:{B}/i386-linux/usr/include" + @echo " DEF-i386 += -D__linux__" + +# -------------------------------------------------------------------------- +endif # ($(INCLUDED),no) diff --git a/05/tcc-final/README b/05/tcc-final/README new file mode 100644 index 0000000..809dd8d --- /dev/null +++ b/05/tcc-final/README @@ -0,0 +1,96 @@ +Tiny C Compiler - C Scripting Everywhere - The Smallest ANSI C compiler +----------------------------------------------------------------------- + +Features: +-------- + +- SMALL! You can compile and execute C code everywhere, for example on + rescue disks. + +- FAST! tcc generates optimized x86 code. No byte code + overhead. Compile, assemble and link about 7 times faster than 'gcc + -O0'. + +- UNLIMITED! Any C dynamic library can be used directly. TCC is + heading toward full ISOC99 compliance. TCC can of course compile + itself. + +- SAFE! tcc includes an optional memory and bound checker. Bound + checked code can be mixed freely with standard code. + +- Compile and execute C source directly. No linking or assembly + necessary. Full C preprocessor included. + +- C script supported : just add '#!/usr/local/bin/tcc -run' at the first + line of your C source, and execute it directly from the command + line. + +Documentation: +------------- + +1) Installation on a i386/x86_64/arm/aarch64/riscv64 + Linux/macOS/FreeBSD/NetBSD/OpenBSD hosts. + + ./configure + make + make test + make install + + Notes: For FreeBSD, NetBSD and OpenBSD, gmake should be used instead of make. + For Windows read tcc-win32.txt. + +makeinfo must be installed to compile the doc. By default, tcc is +installed in /usr/local/bin. ./configure --help shows configuration +options. + + +2) Introduction + +We assume here that you know ANSI C. Look at the example ex1.c to know +what the programs look like. + +The include file can be used if you want a small basic libc +include support (especially useful for floppy disks). Of course, you +can also use standard headers, although they are slower to compile. + +You can begin your C script with '#!/usr/local/bin/tcc -run' on the first +line and set its execute bits (chmod a+x your_script). Then, you can +launch the C code as a shell or perl script :-) The command line +arguments are put in 'argc' and 'argv' of the main functions, as in +ANSI C. + +3) Examples + +ex1.c: simplest example (hello world). Can also be launched directly +as a script: './ex1.c'. + +ex2.c: more complicated example: find a number with the four +operations given a list of numbers (benchmark). + +ex3.c: compute fibonacci numbers (benchmark). + +ex4.c: more complicated: X11 program. Very complicated test in fact +because standard headers are being used ! As for ex1.c, can also be launched +directly as a script: './ex4.c'. + +ex5.c: 'hello world' with standard glibc headers. + +tcc.c: TCC can of course compile itself. Used to check the code +generator. + +tcctest.c: auto test for TCC which tests many subtle possible bugs. Used +when doing 'make test'. + +4) Full Documentation + +Please read tcc-doc.html to have all the features of TCC. + +Additional information is available for the Windows port in tcc-win32.txt. + +License: +------- + +TCC is distributed under the GNU Lesser General Public License (see +COPYING file). + +Fabrice Bellard. diff --git a/05/tcc-final/RELICENSING b/05/tcc-final/RELICENSING new file mode 100644 index 0000000..6c69612 --- /dev/null +++ b/05/tcc-final/RELICENSING @@ -0,0 +1,63 @@ + + Relicensing TinyCC + ------------------ + + The authors listed below hereby confirm their agreement to relicense TinyCC + including their past contributions under the following terms: + + + * 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. + + + Author (name) I agree (YES/NO) Files/Features (optional) + ------------------------------------------------------------------------------ + Adam Sampson YES makefiles + Daniel Glöckner NO arm-gen.c + Daniel Glöckner YES not arm-gen.c + Danny Milosavljevic YES arm-asm.c riscv64-asm.c + Edmund Grimley Evans YES arm64 + Fabrice Bellard YES original author + Frédéric Féret YES x86 64/16 bit asm + grischka YES tccpe.c + Henry Kroll YES + Herman ten Brugge YES + Joe Soroka YES + Kirill Smelkov YES + mingodad YES + Pip Cet YES + Shinichiro Hamaji YES x86_64-gen.c + Vincent Lefèvre YES + Thomas Preud'homme YES arm-gen.c + Timo VJ Lähde (Timppa) ? tiny_libmaker.c + TK ? tcccoff.c c67-gen.c + Tyge Løvset YES tgmath.h, Windows tcc_libm.h math.h + Urs Janssen YES + waddlesplash YES + Christian Jullien YES Windows Cygwin build and tests + + + ------------------------------------------------------------------------------ + + Please add yourself to the list above (rsp. replace the question mark) + and (after fetching the latest version) commit to the "mob" branch with + commit message: + + Relicensing TinyCC + + Thanks. diff --git a/05/tcc-final/TODO b/05/tcc-final/TODO new file mode 100644 index 0000000..d810088 --- /dev/null +++ b/05/tcc-final/TODO @@ -0,0 +1,100 @@ +TODO list: + +Bugs: + +- i386 fastcall is mostly wrong +- FPU st(0) is left unclean (kwisatz haderach). Incompatible with + optimized gcc/msc code +- see transparent union pb in /urs/include/sys/socket.h +- precise behaviour of typeof with arrays ? (__put_user macro) + but should suffice for most cases) +- handle '? x, y : z' in unsized variable initialization (',' is + considered incorrectly as separator in preparser) +- transform functions to function pointers in function parameters + (net/ipv4/ip_output.c) +- fix function pointer type display +- check section alignment in C +- fix invalid cast in comparison 'if (v == (int8_t)v)' +- finish varargs.h support (gcc 3.2 testsuite issue) +- fix static functions declared inside block +- fix multiple unions init +- make libtcc fully reentrant (except for the compilation stage itself). +- struct/union/enum definitions in nested scopes (see also Debian bug #770657) +- __STDC_IEC_559__: float f(void) { static float x = 0.0 / 0.0; return x; } +- memory may be leaked after errors (longjmp). + +Portability: + +- it is assumed that int is 32-bit and sizeof(int) == 4 +- int is used when host or target size_t would make more sense +- TCC handles target floating-point (fp) values using the host's fp + arithmetic, which is simple and fast but may lead to exceptions + and inaccuracy and wrong representations when cross-compiling + +Linking: + +- static linking (-static) does not work + +Bound checking: + +- fix bound exit on RedHat 7.3 +- setjmp is not supported properly in bound checking. +- fix bound check code with '&' on local variables (currently done + only for local arrays). +- bound checking and float/long long/struct copy code. bound + checking and symbol + offset optimization + +Missing features: + +- disable-asm and disable-bcheck options +- __builtin_expect() +- atexit (Nigel Horne) +- C99: add complex types (gcc 3.2 testsuite issue) +- postfix compound literals (see 20010124-1.c) +- interactive mode / integrated debugger + +Optimizations: + +- suppress specific anonymous symbol handling +- more parse optimizations (=even faster compilation) +- memory alloc optimizations (=even faster compilation) +- optimize VT_LOCAL + const +- better local variables handling (needed for other targets) + +Not critical: + +- C99: fix multiple compound literals inits in blocks (ISOC99 + normative example - only relevant when using gotos! -> must add + boolean variable to tell if compound literal was already + initialized). +- add PowerPC generator and improve codegen for RISC (need + to suppress VT_LOCAL and use a base register instead). +- fix preprocessor symbol redefinition +- add portable byte code generator and interpreter for other + unsupported architectures. +- C++: variable declaration in for, minimal 'class' support. +- win32: __intxx. use resolve for bchecked malloc et al. + check exception code (exception filter func). +- handle void (__attribute__() *ptr)() +- VLAs are implemented in a way that is not compatible with signals: + http://lists.gnu.org/archive/html/tinycc-devel/2015-11/msg00018.html + +Fixed (probably): + +- bug with defines: + #define spin_lock(lock) do { } while (0) + #define wq_spin_lock spin_lock + #define TEST() wq_spin_lock(a) +- typedefs can be structure fields +- see bugfixes.diff + improvement.diff from Daniel Glockner +- long long constant evaluation +- add alloca() +- gcc '-E' option. +- #include_next support for /usr/include/limits ? +- function pointers/lvalues in ? : (linux kernel net/core/dev.c) +- win32: add __stdcall, check GetModuleHandle for dlls. +- macro substitution with nested definitions (ShangHongzhang) +- with "-run" and libtcc, a PLT is now built. +- '-E' option was improved +- packed attribute is now supported +- ARM and ARM64 code generators have been added. diff --git a/05/tcc-final/USES b/05/tcc-final/USES new file mode 100644 index 0000000..6d7a2d1 --- /dev/null +++ b/05/tcc-final/USES @@ -0,0 +1,20 @@ +The following software are known to use or support tcc builds. +Feel free to complete this list (*). + +Name Short Description +---- ----------------- +bigz An infinite precision Z & Q library. +gawk GNU awk. +gmp Library for arbitrary precision arithmetic. +gnumake GNU makefile. +mpfr Multiple-precision floating-point library. +mpc Complex floating-point library with exact rounding. +mpv A free, open source, and cross-platform media player. +openlisp ISLISP ISO/IEC 13816 Lisp interpreter and compiler. +s-nail BSD Mail/POSIX mailx: send and receive Internet mail. +sqlite Embbedable SQL engine. +st Simple Terminal. +tcc Tiny CC which compiles itself. +zlib Lossless data-compression library. + +(*) This list is ordered by name. diff --git a/05/tcc-final/VERSION b/05/tcc-final/VERSION new file mode 100644 index 0000000..9a54223 --- /dev/null +++ b/05/tcc-final/VERSION @@ -0,0 +1 @@ +0.9.27 diff --git a/05/tcc-final/arm-asm.c b/05/tcc-final/arm-asm.c new file mode 100644 index 0000000..a6dec4c --- /dev/null +++ b/05/tcc-final/arm-asm.c @@ -0,0 +1,3235 @@ +/* + * ARM specific functions for TCC assembler + * + * Copyright (c) 2001, 2002 Fabrice Bellard + * Copyright (c) 2020 Danny Milosavljevic + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +#define CONFIG_TCC_ASM +#define NB_ASM_REGS 16 + +ST_FUNC void g(int c); +ST_FUNC void gen_le16(int c); +ST_FUNC void gen_le32(int c); + +/*************************************************************/ +#else +/*************************************************************/ + +#define USING_GLOBALS +#include "tcc.h" + +enum { + OPT_REG32, + OPT_REGSET32, + OPT_IM8, + OPT_IM8N, + OPT_IM32, + OPT_VREG32, + OPT_VREG64, +}; +#define OP_REG32 (1 << OPT_REG32) +#define OP_VREG32 (1 << OPT_VREG32) +#define OP_VREG64 (1 << OPT_VREG64) +#define OP_REG (OP_REG32 | OP_VREG32 | OP_VREG64) +#define OP_IM32 (1 << OPT_IM32) +#define OP_IM8 (1 << OPT_IM8) +#define OP_IM8N (1 << OPT_IM8N) +#define OP_REGSET32 (1 << OPT_REGSET32) + +typedef struct Operand { + uint32_t type; + union { + uint8_t reg; + uint16_t regset; + ExprValue e; + }; +} Operand; + +/* Read the VFP register referred to by token T. + If OK, returns its number. + If not OK, returns -1. */ +static int asm_parse_vfp_regvar(int t, int double_precision) +{ + if (double_precision) { + if (t >= TOK_ASM_d0 && t <= TOK_ASM_d15) + return t - TOK_ASM_d0; + } else { + if (t >= TOK_ASM_s0 && t <= TOK_ASM_s31) + return t - TOK_ASM_s0; + } + return -1; +} + +/* Parse a text containing operand and store the result in OP */ +static void parse_operand(TCCState *s1, Operand *op) +{ + ExprValue e; + int8_t reg; + uint16_t regset = 0; + + op->type = 0; + + if (tok == '{') { // regset literal + next(); // skip '{' + while (tok != '}' && tok != TOK_EOF) { + reg = asm_parse_regvar(tok); + if (reg == -1) { + expect("register"); + return; + } else + next(); // skip register name + + if ((1 << reg) < regset) + tcc_warning("registers will be processed in ascending order by hardware--but are not specified in ascending order here"); + regset |= 1 << reg; + if (tok != ',') + break; + next(); // skip ',' + } + if (tok != '}') + expect("'}'"); + next(); // skip '}' + if (regset == 0) { + // ARM instructions don't support empty regset. + tcc_error("empty register list is not supported"); + } else { + op->type = OP_REGSET32; + op->regset = regset; + } + return; + } else if ((reg = asm_parse_regvar(tok)) != -1) { + next(); // skip register name + op->type = OP_REG32; + op->reg = (uint8_t) reg; + return; + } else if ((reg = asm_parse_vfp_regvar(tok, 0)) != -1) { + next(); // skip register name + op->type = OP_VREG32; + op->reg = (uint8_t) reg; + return; + } else if ((reg = asm_parse_vfp_regvar(tok, 1)) != -1) { + next(); // skip register name + op->type = OP_VREG64; + op->reg = (uint8_t) reg; + return; + } else if (tok == '#' || tok == '$') { + /* constant value */ + next(); // skip '#' or '$' + } + asm_expr(s1, &e); + op->type = OP_IM32; + op->e = e; + if (!op->e.sym) { + if ((int) op->e.v < 0 && (int) op->e.v >= -255) + op->type = OP_IM8N; + else if (op->e.v == (uint8_t)op->e.v) + op->type = OP_IM8; + } else + expect("operand"); +} + +/* XXX: make it faster ? */ +ST_FUNC void g(int c) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 1; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; + ind = ind1; +} + +ST_FUNC void gen_le16 (int i) +{ + g(i); + g(i>>8); +} + +ST_FUNC void gen_le32 (int i) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 4; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind++] = i & 0xFF; + cur_text_section->data[ind++] = (i >> 8) & 0xFF; + cur_text_section->data[ind++] = (i >> 16) & 0xFF; + cur_text_section->data[ind++] = (i >> 24) & 0xFF; +} + +ST_FUNC void gen_expr32(ExprValue *pe) +{ + gen_le32(pe->v); +} + +static uint32_t condition_code_of_token(int token) { + if (token < TOK_ASM_nopeq) { + expect("condition-enabled instruction"); + return 0; + } else + return (token - TOK_ASM_nopeq) & 15; +} + +static void asm_emit_opcode(int token, uint32_t opcode) { + gen_le32((condition_code_of_token(token) << 28) | opcode); +} + +static void asm_emit_unconditional_opcode(uint32_t opcode) { + gen_le32(opcode); +} + +static void asm_emit_coprocessor_opcode(uint32_t high_nibble, uint8_t cp_number, uint8_t cp_opcode, uint8_t cp_destination_register, uint8_t cp_n_operand_register, uint8_t cp_m_operand_register, uint8_t cp_opcode2, int inter_processor_transfer) +{ + uint32_t opcode = 0xe000000; + if (inter_processor_transfer) + opcode |= 1 << 4; + //assert(cp_opcode < 16); + opcode |= cp_opcode << 20; + //assert(cp_n_operand_register < 16); + opcode |= cp_n_operand_register << 16; + //assert(cp_destination_register < 16); + opcode |= cp_destination_register << 12; + //assert(cp_number < 16); + opcode |= cp_number << 8; + //assert(cp_information < 8); + opcode |= cp_opcode2 << 5; + //assert(cp_m_operand_register < 16); + opcode |= cp_m_operand_register; + asm_emit_unconditional_opcode((high_nibble << 28) | opcode); +} + +static void asm_nullary_opcode(int token) +{ + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_nopeq: + asm_emit_opcode(token, 0xd << 21); // mov r0, r0 + break; + case TOK_ASM_wfeeq: + asm_emit_opcode(token, 0x320f002); + case TOK_ASM_wfieq: + asm_emit_opcode(token, 0x320f003); + break; + default: + expect("nullary instruction"); + } +} + +static void asm_unary_opcode(TCCState *s1, int token) +{ + Operand op; + parse_operand(s1, &op); + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_swieq: + case TOK_ASM_svceq: + if (op.type != OP_IM8) + expect("immediate 8-bit unsigned integer"); + else { + /* Note: Dummy operand (ignored by processor): ARM ref documented 0...255, ARM instruction set documented 24 bit */ + asm_emit_opcode(token, (0xf << 24) | op.e.v); + } + break; + default: + expect("unary instruction"); + } +} + +static void asm_binary_opcode(TCCState *s1, int token) +{ + Operand ops[2]; + Operand rotation; + uint32_t encoded_rotation = 0; + uint64_t amount; + parse_operand(s1, &ops[0]); + if (tok == ',') + next(); + else + expect("','"); + parse_operand(s1, &ops[1]); + if (ops[0].type != OP_REG32) { + expect("(destination operand) register"); + return; + } + + if (ops[0].reg == 15) { + tcc_error("'%s' does not support 'pc' as operand", get_tok_str(token, NULL)); + return; + } + + if (ops[0].reg == 13) + tcc_warning("Using 'sp' as operand with '%s' is deprecated by ARM", get_tok_str(token, NULL)); + + if (ops[1].type != OP_REG32) { + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_movteq: + case TOK_ASM_movweq: + if (ops[1].type == OP_IM8 || ops[1].type == OP_IM8N || ops[1].type == OP_IM32) { + if (ops[1].e.v >= 0 && ops[1].e.v <= 0xFFFF) { + uint16_t immediate_value = ops[1].e.v; + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_movteq: + asm_emit_opcode(token, 0x3400000 | (ops[0].reg << 12) | (immediate_value & 0xF000) << 4 | (immediate_value & 0xFFF)); + break; + case TOK_ASM_movweq: + asm_emit_opcode(token, 0x3000000 | (ops[0].reg << 12) | (immediate_value & 0xF000) << 4 | (immediate_value & 0xFFF)); + break; + } + } else + expect("(source operand) immediate 16 bit value"); + } else + expect("(source operand) immediate"); + break; + default: + expect("(source operand) register"); + } + return; + } + + if (ops[1].reg == 15) { + tcc_error("'%s' does not support 'pc' as operand", get_tok_str(token, NULL)); + return; + } + + if (ops[1].reg == 13) + tcc_warning("Using 'sp' as operand with '%s' is deprecated by ARM", get_tok_str(token, NULL)); + + if (tok == ',') { + next(); // skip ',' + if (tok == TOK_ASM_ror) { + next(); // skip 'ror' + parse_operand(s1, &rotation); + if (rotation.type != OP_IM8) { + expect("immediate value for rotation"); + return; + } else { + amount = rotation.e.v; + switch (amount) { + case 8: + encoded_rotation = 1 << 10; + break; + case 16: + encoded_rotation = 2 << 10; + break; + case 24: + encoded_rotation = 3 << 10; + break; + default: + expect("'8' or '16' or '24'"); + return; + } + } + } + } + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_clzeq: + if (encoded_rotation) + tcc_error("clz does not support rotation"); + asm_emit_opcode(token, 0x16f0f10 | (ops[0].reg << 12) | ops[1].reg); + break; + case TOK_ASM_sxtbeq: + asm_emit_opcode(token, 0x6af0070 | (ops[0].reg << 12) | ops[1].reg | encoded_rotation); + break; + case TOK_ASM_sxtheq: + asm_emit_opcode(token, 0x6bf0070 | (ops[0].reg << 12) | ops[1].reg | encoded_rotation); + break; + case TOK_ASM_uxtbeq: + asm_emit_opcode(token, 0x6ef0070 | (ops[0].reg << 12) | ops[1].reg | encoded_rotation); + break; + case TOK_ASM_uxtheq: + asm_emit_opcode(token, 0x6ff0070 | (ops[0].reg << 12) | ops[1].reg | encoded_rotation); + break; + default: + expect("binary instruction"); + } +} + +static void asm_coprocessor_opcode(TCCState *s1, int token) { + uint8_t coprocessor; + Operand opcode1; + Operand opcode2; + uint8_t registers[3]; + unsigned int i; + uint8_t high_nibble; + uint8_t mrc = 0; + + if (tok >= TOK_ASM_p0 && tok <= TOK_ASM_p15) { + coprocessor = tok - TOK_ASM_p0; + next(); + } else { + expect("'p'"); + return; + } + + if (tok == ',') + next(); + else + expect("','"); + + parse_operand(s1, &opcode1); + if (opcode1.type != OP_IM8 || opcode1.e.v > 15) { + tcc_error("opcode1 of instruction '%s' must be an immediate value between 0 and 15", get_tok_str(token, NULL)); + return; + } + + for (i = 0; i < 3; ++i) { + if (tok == ',') + next(); + else + expect("','"); + if (i == 0 && token != TOK_ASM_cdp2 && (ARM_INSTRUCTION_GROUP(token) == TOK_ASM_mrceq || ARM_INSTRUCTION_GROUP(token) == TOK_ASM_mcreq)) { + if (tok >= TOK_ASM_r0 && tok <= TOK_ASM_r15) { + registers[i] = tok - TOK_ASM_r0; + next(); + } else { + expect("'r'"); + return; + } + } else { + if (tok >= TOK_ASM_c0 && tok <= TOK_ASM_c15) { + registers[i] = tok - TOK_ASM_c0; + next(); + } else { + expect("'c'"); + return; + } + } + } + if (tok == ',') { + next(); + parse_operand(s1, &opcode2); + } else { + opcode2.type = OP_IM8; + opcode2.e.v = 0; + } + if (opcode2.type != OP_IM8 || opcode2.e.v > 15) { + tcc_error("opcode2 of instruction '%s' must be an immediate value between 0 and 15", get_tok_str(token, NULL)); + return; + } + + if (token == TOK_ASM_cdp2) { + high_nibble = 0xF; + asm_emit_coprocessor_opcode(high_nibble, coprocessor, opcode1.e.v, registers[0], registers[1], registers[2], opcode2.e.v, 0); + return; + } else + high_nibble = condition_code_of_token(token); + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_cdpeq: + asm_emit_coprocessor_opcode(high_nibble, coprocessor, opcode1.e.v, registers[0], registers[1], registers[2], opcode2.e.v, 0); + break; + case TOK_ASM_mrceq: + // opcode1 encoding changes! highest and lowest bit gone. + mrc = 1; + /* fallthrough */ + case TOK_ASM_mcreq: + // opcode1 encoding changes! highest and lowest bit gone. + if (opcode1.e.v > 7) { + tcc_error("opcode1 of instruction '%s' must be an immediate value between 0 and 7", get_tok_str(token, NULL)); + return; + } + asm_emit_coprocessor_opcode(high_nibble, coprocessor, (opcode1.e.v << 1) | mrc, registers[0], registers[1], registers[2], opcode2.e.v, 1); + break; + default: + expect("known instruction"); + } +} + +/* data processing and single data transfer instructions only */ +#define ENCODE_RN(register_index) ((register_index) << 16) +#define ENCODE_RD(register_index) ((register_index) << 12) +#define ENCODE_SET_CONDITION_CODES (1 << 20) + +/* Note: For data processing instructions, "1" means immediate. + Note: For single data transfer instructions, "0" means immediate. */ +#define ENCODE_IMMEDIATE_FLAG (1 << 25) + +#define ENCODE_BARREL_SHIFTER_SHIFT_BY_REGISTER (1 << 4) +#define ENCODE_BARREL_SHIFTER_MODE_LSL (0 << 5) +#define ENCODE_BARREL_SHIFTER_MODE_LSR (1 << 5) +#define ENCODE_BARREL_SHIFTER_MODE_ASR (2 << 5) +#define ENCODE_BARREL_SHIFTER_MODE_ROR (3 << 5) +#define ENCODE_BARREL_SHIFTER_REGISTER(register_index) ((register_index) << 8) +#define ENCODE_BARREL_SHIFTER_IMMEDIATE(value) ((value) << 7) + +static void asm_block_data_transfer_opcode(TCCState *s1, int token) +{ + uint32_t opcode; + int op0_exclam = 0; + Operand ops[2]; + int nb_ops = 1; + parse_operand(s1, &ops[0]); + if (tok == '!') { + op0_exclam = 1; + next(); // skip '!' + } + if (tok == ',') { + next(); // skip comma + parse_operand(s1, &ops[1]); + ++nb_ops; + } + if (nb_ops < 1) { + expect("at least one operand"); + return; + } else if (ops[nb_ops - 1].type != OP_REGSET32) { + expect("(last operand) register list"); + return; + } + + // block data transfer: 1 0 0 P U S W L << 20 (general case): + // operands: + // Rn: bits 19...16 base register + // Register List: bits 15...0 + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_pusheq: // TODO: Optimize 1-register case to: str ?, [sp, #-4]! + // Instruction: 1 I=0 P=1 U=0 S=0 W=1 L=0 << 20, op 1101 + // operands: + // Rn: base register + // Register List: bits 15...0 + if (nb_ops != 1) + expect("exactly one operand"); + else + asm_emit_opcode(token, (0x92d << 16) | ops[0].regset); // TODO: base register ? + break; + case TOK_ASM_popeq: // TODO: Optimize 1-register case to: ldr ?, [sp], #4 + // Instruction: 1 I=0 P=0 U=1 S=0 W=0 L=1 << 20, op 1101 + // operands: + // Rn: base register + // Register List: bits 15...0 + if (nb_ops != 1) + expect("exactly one operand"); + else + asm_emit_opcode(token, (0x8bd << 16) | ops[0].regset); // TODO: base register ? + break; + case TOK_ASM_stmdaeq: + case TOK_ASM_ldmdaeq: + case TOK_ASM_stmeq: + case TOK_ASM_ldmeq: + case TOK_ASM_stmiaeq: + case TOK_ASM_ldmiaeq: + case TOK_ASM_stmdbeq: + case TOK_ASM_ldmdbeq: + case TOK_ASM_stmibeq: + case TOK_ASM_ldmibeq: + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_stmdaeq: // post-decrement store + opcode = 0x80 << 20; + break; + case TOK_ASM_ldmdaeq: // post-decrement load + opcode = 0x81 << 20; + break; + case TOK_ASM_stmeq: // post-increment store + case TOK_ASM_stmiaeq: // post-increment store + opcode = 0x88 << 20; + break; + case TOK_ASM_ldmeq: // post-increment load + case TOK_ASM_ldmiaeq: // post-increment load + opcode = 0x89 << 20; + break; + case TOK_ASM_stmdbeq: // pre-decrement store + opcode = 0x90 << 20; + break; + case TOK_ASM_ldmdbeq: // pre-decrement load + opcode = 0x91 << 20; + break; + case TOK_ASM_stmibeq: // pre-increment store + opcode = 0x98 << 20; + break; + case TOK_ASM_ldmibeq: // pre-increment load + opcode = 0x99 << 20; + break; + default: + tcc_error("internal error: This place should not be reached (fallback in asm_block_data_transfer_opcode)"); + } + // operands: + // Rn: first operand + // Register List: lower bits + if (nb_ops != 2) + expect("exactly two operands"); + else if (ops[0].type != OP_REG32) + expect("(first operand) register"); + else { + if (op0_exclam) + opcode |= 1 << 21; // writeback + asm_emit_opcode(token, opcode | ENCODE_RN(ops[0].reg) | ops[1].regset); + } + break; + default: + expect("block data transfer instruction"); + } +} + +/* Parses shift directive and returns the parts that would have to be set in the opcode because of it. + Does not encode the actual shift amount. + It's not an error if there is no shift directive. + + NB_SHIFT: will be set to 1 iff SHIFT is filled. Note that for rrx, there's no need to fill SHIFT. + SHIFT: will be filled in with the shift operand to use, if any. */ +static uint32_t asm_parse_optional_shift(TCCState* s1, int* nb_shift, Operand* shift) +{ + uint32_t opcode = 0; + *nb_shift = 0; + switch (tok) { + case TOK_ASM_asl: + case TOK_ASM_lsl: + case TOK_ASM_asr: + case TOK_ASM_lsr: + case TOK_ASM_ror: + switch (tok) { + case TOK_ASM_asl: + /* fallthrough */ + case TOK_ASM_lsl: + opcode = ENCODE_BARREL_SHIFTER_MODE_LSL; + break; + case TOK_ASM_asr: + opcode = ENCODE_BARREL_SHIFTER_MODE_ASR; + break; + case TOK_ASM_lsr: + opcode = ENCODE_BARREL_SHIFTER_MODE_LSR; + break; + case TOK_ASM_ror: + opcode = ENCODE_BARREL_SHIFTER_MODE_ROR; + break; + } + next(); + parse_operand(s1, shift); + *nb_shift = 1; + break; + case TOK_ASM_rrx: + next(); + opcode = ENCODE_BARREL_SHIFTER_MODE_ROR; + break; + } + return opcode; +} + +static uint32_t asm_encode_shift(Operand* shift) +{ + uint64_t amount; + uint32_t operands = 0; + switch (shift->type) { + case OP_REG32: + if (shift->reg == 15) + tcc_error("r15 cannot be used as a shift count"); + else { + operands = ENCODE_BARREL_SHIFTER_SHIFT_BY_REGISTER; + operands |= ENCODE_BARREL_SHIFTER_REGISTER(shift->reg); + } + break; + case OP_IM8: + amount = shift->e.v; + if (amount > 0 && amount < 32) + operands = ENCODE_BARREL_SHIFTER_IMMEDIATE(amount); + else + tcc_error("shift count out of range"); + break; + default: + tcc_error("unknown shift amount"); + } + return operands; +} + +static void asm_data_processing_opcode(TCCState *s1, int token) +{ + Operand ops[3]; + int nb_ops; + Operand shift = {0}; + int nb_shift = 0; + uint32_t operands = 0; + + /* modulo 16 entries per instruction for the different condition codes */ + uint32_t opcode_idx = (ARM_INSTRUCTION_GROUP(token) - TOK_ASM_andeq) >> 4; + uint32_t opcode_nos = opcode_idx >> 1; // without "s"; "OpCode" in ARM docs + + for (nb_ops = 0; nb_ops < sizeof(ops)/sizeof(ops[0]); ) { + if (tok == TOK_ASM_asl || tok == TOK_ASM_lsl || tok == TOK_ASM_lsr || tok == TOK_ASM_asr || tok == TOK_ASM_ror || tok == TOK_ASM_rrx) + break; + parse_operand(s1, &ops[nb_ops]); + ++nb_ops; + if (tok != ',') + break; + next(); // skip ',' + } + if (tok == ',') + next(); + operands |= asm_parse_optional_shift(s1, &nb_shift, &shift); + if (nb_ops < 2) + expect("at least two operands"); + else if (nb_ops == 2) { + memcpy(&ops[2], &ops[1], sizeof(ops[1])); // move ops[2] + memcpy(&ops[1], &ops[0], sizeof(ops[0])); // ops[1] was implicit + nb_ops = 3; + } else if (nb_ops == 3) { + if (opcode_nos == 0xd || opcode_nos == 0xf || opcode_nos == 0xa || opcode_nos == 0xb || opcode_nos == 0x8 || opcode_nos == 0x9) { // mov, mvn, cmp, cmn, tst, teq + tcc_error("'%s' cannot be used with three operands", get_tok_str(token, NULL)); + return; + } + } + if (nb_ops != 3) { + expect("two or three operands"); + return; + } else { + uint32_t opcode = 0; + uint32_t immediate_value; + uint8_t half_immediate_rotation; + if (nb_shift && shift.type == OP_REG32) { + if ((ops[0].type == OP_REG32 && ops[0].reg == 15) || + (ops[1].type == OP_REG32 && ops[1].reg == 15)) { + tcc_error("Using the 'pc' register in data processing instructions that have a register-controlled shift is not implemented by ARM"); + return; + } + } + + // data processing (general case): + // operands: + // Rn: bits 19...16 (first operand) + // Rd: bits 15...12 (destination) + // Operand2: bits 11...0 (second operand); depending on I that's either a register or an immediate + // operator: + // bits 24...21: "OpCode"--see below + + /* operations in the token list are ordered by opcode */ + opcode = opcode_nos << 21; // drop "s" + if (ops[0].type != OP_REG32) + expect("(destination operand) register"); + else if (opcode_nos == 0xa || opcode_nos == 0xb || opcode_nos == 0x8 || opcode_nos == 0x9) // cmp, cmn, tst, teq + operands |= ENCODE_SET_CONDITION_CODES; // force S set, otherwise it's a completely different instruction. + else + operands |= ENCODE_RD(ops[0].reg); + if (ops[1].type != OP_REG32) + expect("(first source operand) register"); + else if (!(opcode_nos == 0xd || opcode_nos == 0xf)) // not: mov, mvn (those have only one source operand) + operands |= ENCODE_RN(ops[1].reg); + switch (ops[2].type) { + case OP_REG32: + operands |= ops[2].reg; + break; + case OP_IM8: + case OP_IM32: + operands |= ENCODE_IMMEDIATE_FLAG; + immediate_value = ops[2].e.v; + for (half_immediate_rotation = 0; half_immediate_rotation < 16; ++half_immediate_rotation) { + if (immediate_value >= 0x00 && immediate_value < 0x100) + break; + // rotate left by two + immediate_value = ((immediate_value & 0x3FFFFFFF) << 2) | ((immediate_value & 0xC0000000) >> 30); + } + if (half_immediate_rotation >= 16) { + /* fallthrough */ + } else { + operands |= immediate_value; + operands |= half_immediate_rotation << 8; + break; + } + case OP_IM8N: // immediate negative value + operands |= ENCODE_IMMEDIATE_FLAG; + immediate_value = ops[2].e.v; + /* Instruction swapping: + 0001 = EOR - Rd:= Op1 EOR Op2 -> difficult + 0011 = RSB - Rd:= Op2 - Op1 -> difficult + 0111 = RSC - Rd:= Op2 - Op1 + C -> difficult + 1000 = TST - CC on: Op1 AND Op2 -> difficult + 1001 = TEQ - CC on: Op1 EOR Op2 -> difficult + 1100 = ORR - Rd:= Op1 OR Op2 -> difficult + */ + switch (opcode_nos) { + case 0x0: // AND - Rd:= Op1 AND Op2 + opcode = 0xe << 21; // BIC + immediate_value = ~immediate_value; + break; + case 0x2: // SUB - Rd:= Op1 - Op2 + opcode = 0x4 << 21; // ADD + immediate_value = -immediate_value; + break; + case 0x4: // ADD - Rd:= Op1 + Op2 + opcode = 0x2 << 21; // SUB + immediate_value = -immediate_value; + break; + case 0x5: // ADC - Rd:= Op1 + Op2 + C + opcode = 0x6 << 21; // SBC + immediate_value = ~immediate_value; + break; + case 0x6: // SBC - Rd:= Op1 - Op2 + C + opcode = 0x5 << 21; // ADC + immediate_value = ~immediate_value; + break; + case 0xa: // CMP - CC on: Op1 - Op2 + opcode = 0xb << 21; // CMN + immediate_value = -immediate_value; + break; + case 0xb: // CMN - CC on: Op1 + Op2 + opcode = 0xa << 21; // CMP + immediate_value = -immediate_value; + break; + case 0xd: // MOV - Rd:= Op2 + opcode = 0xf << 21; // MVN + immediate_value = ~immediate_value; + break; + case 0xe: // BIC - Rd:= Op1 AND NOT Op2 + opcode = 0x0 << 21; // AND + immediate_value = ~immediate_value; + break; + case 0xf: // MVN - Rd:= NOT Op2 + opcode = 0xd << 21; // MOV + immediate_value = ~immediate_value; + break; + default: + tcc_error("cannot use '%s' with a negative immediate value", get_tok_str(token, NULL)); + } + for (half_immediate_rotation = 0; half_immediate_rotation < 16; ++half_immediate_rotation) { + if (immediate_value >= 0x00 && immediate_value < 0x100) + break; + // rotate left by two + immediate_value = ((immediate_value & 0x3FFFFFFF) << 2) | ((immediate_value & 0xC0000000) >> 30); + } + if (half_immediate_rotation >= 16) { + immediate_value = ops[2].e.v; + tcc_error("immediate value 0x%X cannot be encoded into ARM immediate", (unsigned) immediate_value); + return; + } + operands |= immediate_value; + operands |= half_immediate_rotation << 8; + break; + default: + expect("(second source operand) register or immediate value"); + } + + if (nb_shift) { + if (operands & ENCODE_IMMEDIATE_FLAG) + tcc_error("immediate rotation not implemented"); + else + operands |= asm_encode_shift(&shift); + } + + /* S=0 and S=1 entries alternate one after another, in that order */ + opcode |= (opcode_idx & 1) ? ENCODE_SET_CONDITION_CODES : 0; + asm_emit_opcode(token, opcode | operands); + } +} + +static void asm_shift_opcode(TCCState *s1, int token) +{ + Operand ops[3]; + int nb_ops; + int definitely_neutral = 0; + uint32_t opcode = 0xd << 21; // MOV + uint32_t operands = 0; + + for (nb_ops = 0; nb_ops < sizeof(ops)/sizeof(ops[0]); ++nb_ops) { + parse_operand(s1, &ops[nb_ops]); + if (tok != ',') { + ++nb_ops; + break; + } + next(); // skip ',' + } + if (nb_ops < 2) { + expect("at least two operands"); + return; + } + + if (ops[0].type != OP_REG32) { + expect("(destination operand) register"); + return; + } else + operands |= ENCODE_RD(ops[0].reg); + + if (nb_ops == 2) { + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_rrxseq: + opcode |= ENCODE_SET_CONDITION_CODES; + /* fallthrough */ + case TOK_ASM_rrxeq: + if (ops[1].type == OP_REG32) { + operands |= ops[1].reg; + operands |= ENCODE_BARREL_SHIFTER_MODE_ROR; + asm_emit_opcode(token, opcode | operands); + } else + tcc_error("(first source operand) register"); + return; + default: + memcpy(&ops[2], &ops[1], sizeof(ops[1])); // move ops[2] + memcpy(&ops[1], &ops[0], sizeof(ops[0])); // ops[1] was implicit + nb_ops = 3; + } + } + if (nb_ops != 3) { + expect("two or three operands"); + return; + } + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_lslseq: + case TOK_ASM_lsrseq: + case TOK_ASM_asrseq: + case TOK_ASM_rorseq: + opcode |= ENCODE_SET_CONDITION_CODES; + break; + } + + switch (ops[1].type) { + case OP_REG32: + operands |= ops[1].reg; + break; + case OP_IM8: + operands |= ENCODE_IMMEDIATE_FLAG; + operands |= ops[1].e.v; + tcc_error("Using an immediate value as the source operand is not possible with '%s' instruction on ARM", get_tok_str(token, NULL)); + return; + } + + switch (ops[2].type) { + case OP_REG32: + if ((ops[0].type == OP_REG32 && ops[0].reg == 15) || + (ops[1].type == OP_REG32 && ops[1].reg == 15)) { + tcc_error("Using the 'pc' register in data processing instructions that have a register-controlled shift is not implemented by ARM"); + } + operands |= asm_encode_shift(&ops[2]); + break; + case OP_IM8: + if (ops[2].e.v) + operands |= asm_encode_shift(&ops[2]); + else + definitely_neutral = 1; + break; + } + + if (!definitely_neutral) switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_lslseq: + case TOK_ASM_lsleq: + operands |= ENCODE_BARREL_SHIFTER_MODE_LSL; + break; + case TOK_ASM_lsrseq: + case TOK_ASM_lsreq: + operands |= ENCODE_BARREL_SHIFTER_MODE_LSR; + break; + case TOK_ASM_asrseq: + case TOK_ASM_asreq: + operands |= ENCODE_BARREL_SHIFTER_MODE_ASR; + break; + case TOK_ASM_rorseq: + case TOK_ASM_roreq: + operands |= ENCODE_BARREL_SHIFTER_MODE_ROR; + break; + default: + expect("shift instruction"); + return; + } + asm_emit_opcode(token, opcode | operands); +} + +static void asm_multiplication_opcode(TCCState *s1, int token) +{ + Operand ops[4]; + int nb_ops = 0; + uint32_t opcode = 0x90; + + for (nb_ops = 0; nb_ops < sizeof(ops)/sizeof(ops[0]); ++nb_ops) { + parse_operand(s1, &ops[nb_ops]); + if (tok != ',') { + ++nb_ops; + break; + } + next(); // skip ',' + } + if (nb_ops < 2) + expect("at least two operands"); + else if (nb_ops == 2) { + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_mulseq: + case TOK_ASM_muleq: + memcpy(&ops[2], &ops[0], sizeof(ops[1])); // ARM is actually like this! + break; + default: + expect("at least three operands"); + return; + } + nb_ops = 3; + } + + // multiply (special case): + // operands: + // Rd: bits 19...16 + // Rm: bits 3...0 + // Rs: bits 11...8 + // Rn: bits 15...12 + + if (ops[0].type == OP_REG32) + opcode |= ops[0].reg << 16; + else + expect("(destination operand) register"); + if (ops[1].type == OP_REG32) + opcode |= ops[1].reg; + else + expect("(first source operand) register"); + if (ops[2].type == OP_REG32) + opcode |= ops[2].reg << 8; + else + expect("(second source operand) register"); + if (nb_ops > 3) { + if (ops[3].type == OP_REG32) + opcode |= ops[3].reg << 12; + else + expect("(third source operand) register"); + } + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_mulseq: + opcode |= 1 << 20; // Status + /* fallthrough */ + case TOK_ASM_muleq: + if (nb_ops != 3) + expect("three operands"); + else { + asm_emit_opcode(token, opcode); + } + break; + case TOK_ASM_mlaseq: + opcode |= 1 << 20; // Status + /* fallthrough */ + case TOK_ASM_mlaeq: + if (nb_ops != 4) + expect("four operands"); + else { + opcode |= 1 << 21; // Accumulate + asm_emit_opcode(token, opcode); + } + break; + default: + expect("known multiplication instruction"); + } +} + +static void asm_long_multiplication_opcode(TCCState *s1, int token) +{ + Operand ops[4]; + int nb_ops = 0; + uint32_t opcode = 0x90 | (1 << 23); + + for (nb_ops = 0; nb_ops < sizeof(ops)/sizeof(ops[0]); ++nb_ops) { + parse_operand(s1, &ops[nb_ops]); + if (tok != ',') { + ++nb_ops; + break; + } + next(); // skip ',' + } + if (nb_ops != 4) { + expect("four operands"); + return; + } + + // long multiply (special case): + // operands: + // RdLo: bits 15...12 + // RdHi: bits 19...16 + // Rs: bits 11...8 + // Rm: bits 3...0 + + if (ops[0].type == OP_REG32) + opcode |= ops[0].reg << 12; + else + expect("(destination lo accumulator) register"); + if (ops[1].type == OP_REG32) + opcode |= ops[1].reg << 16; + else + expect("(destination hi accumulator) register"); + if (ops[2].type == OP_REG32) + opcode |= ops[2].reg; + else + expect("(first source operand) register"); + if (ops[3].type == OP_REG32) + opcode |= ops[3].reg << 8; + else + expect("(second source operand) register"); + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_smullseq: + opcode |= 1 << 20; // Status + /* fallthrough */ + case TOK_ASM_smulleq: + opcode |= 1 << 22; // signed + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_umullseq: + opcode |= 1 << 20; // Status + /* fallthrough */ + case TOK_ASM_umulleq: + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_smlalseq: + opcode |= 1 << 20; // Status + /* fallthrough */ + case TOK_ASM_smlaleq: + opcode |= 1 << 22; // signed + opcode |= 1 << 21; // Accumulate + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_umlalseq: + opcode |= 1 << 20; // Status + /* fallthrough */ + case TOK_ASM_umlaleq: + opcode |= 1 << 21; // Accumulate + asm_emit_opcode(token, opcode); + break; + default: + expect("known long multiplication instruction"); + } +} + +static void asm_single_data_transfer_opcode(TCCState *s1, int token) +{ + Operand ops[3]; + Operand strex_operand; + Operand shift; + int nb_shift = 0; + int exclam = 0; + int closed_bracket = 0; + int op2_minus = 0; + uint32_t opcode = 0; + // Note: ldr r0, [r4, #4] ; simple offset: r0 = *(int*)(r4+4); r4 unchanged + // Note: ldr r0, [r4, #4]! ; pre-indexed: r0 = *(int*)(r4+4); r4 = r4+4 + // Note: ldr r0, [r4], #4 ; post-indexed: r0 = *(int*)(r4+0); r4 = r4+4 + + parse_operand(s1, &ops[0]); + if (ops[0].type == OP_REG32) + opcode |= ENCODE_RD(ops[0].reg); + else { + expect("(destination operand) register"); + return; + } + if (tok != ',') + expect("at least two arguments"); + else + next(); // skip ',' + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_strexbeq: + case TOK_ASM_strexeq: + parse_operand(s1, &strex_operand); + if (strex_operand.type != OP_REG32) { + expect("register"); + return; + } + if (tok != ',') + expect("at least three arguments"); + else + next(); // skip ',' + break; + } + + if (tok != '[') + expect("'['"); + else + next(); // skip '[' + + parse_operand(s1, &ops[1]); + if (ops[1].type == OP_REG32) + opcode |= ENCODE_RN(ops[1].reg); + else { + expect("(first source operand) register"); + return; + } + if (tok == ']') { + next(); + closed_bracket = 1; + // exclam = 1; // implicit in hardware; don't do it in software + } + if (tok == ',') { + next(); // skip ',' + if (tok == '-') { + op2_minus = 1; + next(); + } + parse_operand(s1, &ops[2]); + if (ops[2].type == OP_REG32) { + if (ops[2].reg == 15) { + tcc_error("Using 'pc' for register offset in '%s' is not implemented by ARM", get_tok_str(token, NULL)); + return; + } + if (tok == ',') { + next(); + opcode |= asm_parse_optional_shift(s1, &nb_shift, &shift); + if (opcode == 0) + expect("shift directive, or no comma"); + } + } + } else { + // end of input expression in brackets--assume 0 offset + ops[2].type = OP_IM8; + ops[2].e.v = 0; + opcode |= 1 << 24; // add offset before transfer + } + if (!closed_bracket) { + if (tok != ']') + expect("']'"); + else + next(); // skip ']' + opcode |= 1 << 24; // add offset before transfer + if (tok == '!') { + exclam = 1; + next(); // skip '!' + } + } + + // single data transfer: 0 1 I P U B W L << 20 (general case): + // operands: + // Rd: destination operand [ok] + // Rn: first source operand [ok] + // Operand2: bits 11...0 [ok] + // I: immediate operand? [ok] + // P: Pre/post indexing is PRE: Add offset before transfer [ok] + // U: Up/down is up? (*adds* offset to base) [ok] + // B: Byte/word is byte? [ok] + // W: Write address back into base? [ok] + // L: Load/store is load? [ok] + if (exclam) + opcode |= 1 << 21; // write offset back into register + + if (ops[2].type == OP_IM32 || ops[2].type == OP_IM8 || ops[2].type == OP_IM8N) { + int v = ops[2].e.v; + if (op2_minus) + tcc_error("minus before '#' not supported for immediate values"); + if (v >= 0) { + opcode |= 1 << 23; // up + if (v >= 0x1000) + tcc_error("offset out of range for '%s'", get_tok_str(token, NULL)); + else + opcode |= v; + } else { // down + if (v <= -0x1000) + tcc_error("offset out of range for '%s'", get_tok_str(token, NULL)); + else + opcode |= -v; + } + } else if (ops[2].type == OP_REG32) { + if (!op2_minus) + opcode |= 1 << 23; // up + opcode |= ENCODE_IMMEDIATE_FLAG; /* if set, it means it's NOT immediate */ + opcode |= ops[2].reg; + } else + expect("register"); + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_strbeq: + opcode |= 1 << 22; // B + /* fallthrough */ + case TOK_ASM_streq: + opcode |= 1 << 26; // Load/Store + if (nb_shift) + opcode |= asm_encode_shift(&shift); + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_ldrbeq: + opcode |= 1 << 22; // B + /* fallthrough */ + case TOK_ASM_ldreq: + opcode |= 1 << 20; // L + opcode |= 1 << 26; // Load/Store + if (nb_shift) + opcode |= asm_encode_shift(&shift); + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_strexbeq: + opcode |= 1 << 22; // B + /* fallthrough */ + case TOK_ASM_strexeq: + if ((opcode & 0xFFF) || nb_shift) { + tcc_error("neither offset nor shift allowed with 'strex'"); + return; + } else if (opcode & ENCODE_IMMEDIATE_FLAG) { // if set, it means it's NOT immediate + tcc_error("offset not allowed with 'strex'"); + return; + } + if ((opcode & (1 << 24)) == 0) { // add offset after transfer + tcc_error("adding offset after transfer not allowed with 'strex'"); + return; + } + + opcode |= 0xf90; // Used to mean: barrel shifter is enabled, barrel shift register is r15, mode is LSL + opcode |= strex_operand.reg; + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_ldrexbeq: + opcode |= 1 << 22; // B + /* fallthrough */ + case TOK_ASM_ldrexeq: + if ((opcode & 0xFFF) || nb_shift) { + tcc_error("neither offset nor shift allowed with 'ldrex'"); + return; + } else if (opcode & ENCODE_IMMEDIATE_FLAG) { // if set, it means it's NOT immediate + tcc_error("offset not allowed with 'ldrex'"); + return; + } + if ((opcode & (1 << 24)) == 0) { // add offset after transfer + tcc_error("adding offset after transfer not allowed with 'ldrex'"); + return; + } + opcode |= 1 << 20; // L + opcode |= 0x00f; + opcode |= 0xf90; // Used to mean: barrel shifter is enabled, barrel shift register is r15, mode is LSL + asm_emit_opcode(token, opcode); + break; + default: + expect("data transfer instruction"); + } +} + +// Note: Only call this using a VFP register if you know exactly what you are doing (i.e. cp_number is 10 or 11 and you are doing a vmov) +static void asm_emit_coprocessor_data_transfer(uint32_t high_nibble, uint8_t cp_number, uint8_t CRd, const Operand* Rn, const Operand* offset, int offset_minus, int preincrement, int writeback, int long_transfer, int load) { + uint32_t opcode = 0x0; + opcode |= 1 << 26; // Load/Store + opcode |= 1 << 27; // coprocessor + + if (long_transfer) + opcode |= 1 << 22; // long transfer + + if (load) + opcode |= 1 << 20; // L + + opcode |= cp_number << 8; + + //assert(CRd < 16); + opcode |= ENCODE_RD(CRd); + + if (Rn->type != OP_REG32) { + expect("register"); + return; + } + //assert(Rn->reg < 16); + opcode |= ENCODE_RN(Rn->reg); + if (preincrement) + opcode |= 1 << 24; // add offset before transfer + + if (writeback) + opcode |= 1 << 21; // write offset back into register + + if (offset->type == OP_IM8 || offset->type == OP_IM8N || offset->type == OP_IM32) { + int v = offset->e.v; + if (offset_minus) + tcc_error("minus before '#' not supported for immediate values"); + if (offset->type == OP_IM8N || v < 0) + v = -v; + else + opcode |= 1 << 23; // up + if (v & 3) { + tcc_error("immediate offset must be a multiple of 4"); + return; + } + v >>= 2; + if (v > 255) { + tcc_error("immediate offset must be between -1020 and 1020"); + return; + } + opcode |= v; + } else if (offset->type == OP_REG32) { + if (!offset_minus) + opcode |= 1 << 23; // up + opcode |= ENCODE_IMMEDIATE_FLAG; /* if set, it means it's NOT immediate */ + opcode |= offset->reg; + tcc_error("Using register offset to register address is not possible here"); + return; + } else if (offset->type == OP_VREG64) { + opcode |= 16; + opcode |= offset->reg; + } else + expect("immediate or register"); + + asm_emit_unconditional_opcode((high_nibble << 28) | opcode); +} + +// Almost exactly the same as asm_single_data_transfer_opcode. +// Difference: Offsets are smaller and multiples of 4; no shifts, no STREX, ENCODE_IMMEDIATE_FLAG is inverted again. +static void asm_coprocessor_data_transfer_opcode(TCCState *s1, int token) +{ + Operand ops[3]; + uint8_t coprocessor; + uint8_t coprocessor_destination_register; + int preincrement = 0; + int exclam = 0; + int closed_bracket = 0; + int op2_minus = 0; + int long_transfer = 0; + // Note: ldc p1, c0, [r4, #4] ; simple offset: r0 = *(int*)(r4+4); r4 unchanged + // Note: ldc p2, c0, [r4, #4]! ; pre-indexed: r0 = *(int*)(r4+4); r4 = r4+4 + // Note: ldc p3, c0, [r4], #4 ; post-indexed: r0 = *(int*)(r4+0); r4 = r4+4 + + if (tok >= TOK_ASM_p0 && tok <= TOK_ASM_p15) { + coprocessor = tok - TOK_ASM_p0; + next(); + } else { + expect("'c'"); + return; + } + + if (tok == ',') + next(); + else + expect("','"); + + if (tok >= TOK_ASM_c0 && tok <= TOK_ASM_c15) { + coprocessor_destination_register = tok - TOK_ASM_c0; + next(); + } else { + expect("'c'"); + return; + } + + if (tok == ',') + next(); + else + expect("','"); + + if (tok != '[') + expect("'['"); + else + next(); // skip '[' + + parse_operand(s1, &ops[1]); + if (ops[1].type != OP_REG32) { + expect("(first source operand) register"); + return; + } + if (tok == ']') { + next(); + closed_bracket = 1; + // exclam = 1; // implicit in hardware; don't do it in software + } + if (tok == ',') { + next(); // skip ',' + if (tok == '-') { + op2_minus = 1; + next(); + } + parse_operand(s1, &ops[2]); + if (ops[2].type == OP_REG32) { + if (ops[2].reg == 15) { + tcc_error("Using 'pc' for register offset in '%s' is not implemented by ARM", get_tok_str(token, NULL)); + return; + } + } else if (ops[2].type == OP_VREG64) { + tcc_error("'%s' does not support VFP register operand", get_tok_str(token, NULL)); + return; + } + } else { + // end of input expression in brackets--assume 0 offset + ops[2].type = OP_IM8; + ops[2].e.v = 0; + preincrement = 1; // add offset before transfer + } + if (!closed_bracket) { + if (tok != ']') + expect("']'"); + else + next(); // skip ']' + preincrement = 1; // add offset before transfer + if (tok == '!') { + exclam = 1; + next(); // skip '!' + } + } + + // TODO: Support options. + + if (token == TOK_ASM_ldc2 || token == TOK_ASM_stc2 || token == TOK_ASM_ldc2l || token == TOK_ASM_stc2l) { + switch (token) { + case TOK_ASM_ldc2l: + long_transfer = 1; // long transfer + /* fallthrough */ + case TOK_ASM_ldc2: + asm_emit_coprocessor_data_transfer(0xF, coprocessor, coprocessor_destination_register, &ops[1], &ops[2], op2_minus, preincrement, exclam, long_transfer, 1); + break; + case TOK_ASM_stc2l: + long_transfer = 1; // long transfer + /* fallthrough */ + case TOK_ASM_stc2: + asm_emit_coprocessor_data_transfer(0xF, coprocessor, coprocessor_destination_register, &ops[1], &ops[2], op2_minus, preincrement, exclam, long_transfer, 0); + break; + } + } else switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_stcleq: + long_transfer = 1; + /* fallthrough */ + case TOK_ASM_stceq: + asm_emit_coprocessor_data_transfer(condition_code_of_token(token), coprocessor, coprocessor_destination_register, &ops[1], &ops[2], op2_minus, preincrement, exclam, long_transfer, 0); + break; + case TOK_ASM_ldcleq: + long_transfer = 1; + /* fallthrough */ + case TOK_ASM_ldceq: + asm_emit_coprocessor_data_transfer(condition_code_of_token(token), coprocessor, coprocessor_destination_register, &ops[1], &ops[2], op2_minus, preincrement, exclam, long_transfer, 1); + break; + default: + expect("coprocessor data transfer instruction"); + } +} + +#if defined(TCC_ARM_VFP) +#define CP_SINGLE_PRECISION_FLOAT 10 +#define CP_DOUBLE_PRECISION_FLOAT 11 + +static void asm_floating_point_single_data_transfer_opcode(TCCState *s1, int token) +{ + Operand ops[3]; + uint8_t coprocessor = 0; + uint8_t coprocessor_destination_register = 0; + int long_transfer = 0; + // Note: vldr p1, c0, [r4, #4] ; simple offset: r0 = *(int*)(r4+4); r4 unchanged + // Note: Not allowed: vldr p2, c0, [r4, #4]! ; pre-indexed: r0 = *(int*)(r4+4); r4 = r4+4 + // Note: Not allowed: vldr p3, c0, [r4], #4 ; post-indexed: r0 = *(int*)(r4+0); r4 = r4+4 + + parse_operand(s1, &ops[0]); + if (ops[0].type == OP_VREG32) { + coprocessor = CP_SINGLE_PRECISION_FLOAT; + coprocessor_destination_register = ops[0].reg; + long_transfer = coprocessor_destination_register & 1; + coprocessor_destination_register >>= 1; + } else if (ops[0].type == OP_VREG64) { + coprocessor = CP_DOUBLE_PRECISION_FLOAT; + coprocessor_destination_register = ops[0].reg; + next(); + } else { + expect("floating point register"); + return; + } + + if (tok == ',') + next(); + else + expect("','"); + + if (tok != '[') + expect("'['"); + else + next(); // skip '[' + + parse_operand(s1, &ops[1]); + if (ops[1].type != OP_REG32) { + expect("(first source operand) register"); + return; + } + if (tok == ',') { + next(); // skip ',' + parse_operand(s1, &ops[2]); + if (ops[2].type != OP_IM8 && ops[2].type != OP_IM8N) { + expect("immediate offset"); + return; + } + } else { + // end of input expression in brackets--assume 0 offset + ops[2].type = OP_IM8; + ops[2].e.v = 0; + } + if (tok != ']') + expect("']'"); + else + next(); // skip ']' + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vldreq: + asm_emit_coprocessor_data_transfer(condition_code_of_token(token), coprocessor, coprocessor_destination_register, &ops[1], &ops[2], 0, 1, 0, long_transfer, 1); + break; + case TOK_ASM_vstreq: + asm_emit_coprocessor_data_transfer(condition_code_of_token(token), coprocessor, coprocessor_destination_register, &ops[1], &ops[2], 0, 1, 0, long_transfer, 0); + break; + default: + expect("floating point data transfer instruction"); + } +} + +static void asm_floating_point_block_data_transfer_opcode(TCCState *s1, int token) +{ + uint8_t coprocessor = 0; + int first_regset_register; + int last_regset_register; + uint8_t regset_item_count; + uint8_t extra_register_bit = 0; + int op0_exclam = 0; + int load = 0; + int preincrement = 0; + Operand ops[1]; + Operand offset; + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vpusheq: + case TOK_ASM_vpopeq: + ops[0].type = OP_REG32; + ops[0].reg = 13; // sp + op0_exclam = 1; + break; + default: + parse_operand(s1, &ops[0]); + if (tok == '!') { + op0_exclam = 1; + next(); // skip '!' + } + if (tok == ',') + next(); // skip comma + else { + expect("','"); + return; + } + } + + if (tok != '{') { + expect("'{'"); + return; + } + next(); // skip '{' + first_regset_register = asm_parse_vfp_regvar(tok, 1); + if ((first_regset_register = asm_parse_vfp_regvar(tok, 1)) != -1) { + coprocessor = CP_DOUBLE_PRECISION_FLOAT; + next(); + } else if ((first_regset_register = asm_parse_vfp_regvar(tok, 0)) != -1) { + coprocessor = CP_SINGLE_PRECISION_FLOAT; + next(); + } else { + expect("floating-point register"); + return; + } + + if (tok == '-') { + next(); + if ((last_regset_register = asm_parse_vfp_regvar(tok, coprocessor == CP_DOUBLE_PRECISION_FLOAT)) != -1) + next(); + else { + expect("floating-point register"); + return; + } + } else + last_regset_register = first_regset_register; + + if (last_regset_register < first_regset_register) { + tcc_error("registers will be processed in ascending order by hardware--but are not specified in ascending order here"); + return; + } + if (tok != '}') { + expect("'}'"); + return; + } + next(); // skip '}' + + // Note: 0 (one down) is not implemented by us regardless. + regset_item_count = last_regset_register - first_regset_register + 1; + if (coprocessor == CP_DOUBLE_PRECISION_FLOAT) + regset_item_count <<= 1; + else { + extra_register_bit = first_regset_register & 1; + first_regset_register >>= 1; + } + offset.type = OP_IM8; + offset.e.v = regset_item_count << 2; + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vstmeq: // post-increment store + case TOK_ASM_vstmiaeq: // post-increment store + break; + case TOK_ASM_vpopeq: + case TOK_ASM_vldmeq: // post-increment load + case TOK_ASM_vldmiaeq: // post-increment load + load = 1; + break; + case TOK_ASM_vldmdbeq: // pre-decrement load + load = 1; + /* fallthrough */ + case TOK_ASM_vpusheq: + case TOK_ASM_vstmdbeq: // pre-decrement store + offset.type = OP_IM8N; + offset.e.v = -offset.e.v; + preincrement = 1; + break; + default: + expect("floating point block data transfer instruction"); + return; + } + if (ops[0].type != OP_REG32) + expect("(first operand) register"); + else if (ops[0].reg == 15) + tcc_error("'%s' does not support 'pc' as operand", get_tok_str(token, NULL)); + else if (!op0_exclam && ARM_INSTRUCTION_GROUP(token) != TOK_ASM_vldmeq && ARM_INSTRUCTION_GROUP(token) != TOK_ASM_vldmiaeq && ARM_INSTRUCTION_GROUP(token) != TOK_ASM_vstmeq && ARM_INSTRUCTION_GROUP(token) != TOK_ASM_vstmiaeq) + tcc_error("first operand of '%s' should have an exclamation mark", get_tok_str(token, NULL)); + else + asm_emit_coprocessor_data_transfer(condition_code_of_token(token), coprocessor, first_regset_register, &ops[0], &offset, 0, preincrement, op0_exclam, extra_register_bit, load); +} + +#define VMOV_FRACTIONAL_DIGITS 7 +#define VMOV_ONE 10000000 /* pow(10, VMOV_FRACTIONAL_DIGITS) */ + +static uint32_t vmov_parse_fractional_part(const char* s) +{ + uint32_t result = 0; + int i; + for (i = 0; i < VMOV_FRACTIONAL_DIGITS; ++i) { + char c = *s; + result *= 10; + if (c >= '0' && c <= '9') { + result += (c - '0'); + ++s; + } + } + if (*s) + expect("decimal numeral"); + return result; +} + +static int vmov_linear_approx_index(uint32_t beginning, uint32_t end, uint32_t value) +{ + int i; + uint32_t k; + uint32_t xvalue; + + k = (end - beginning)/16; + for (xvalue = beginning, i = 0; i < 16; ++i, xvalue += k) { + if (value == xvalue) + return i; + } + //assert(0); + return -1; +} + +static uint32_t vmov_parse_immediate_value() { + uint32_t value; + unsigned long integral_value; + const char *p; + + if (tok != TOK_PPNUM) { + expect("immediate value"); + return 0; + } + p = tokc.str.data; + errno = 0; + integral_value = strtoul(p, (char **)&p, 0); + + if (errno || integral_value >= 32) { + tcc_error("invalid floating-point immediate value"); + return 0; + } + + value = (uint32_t) integral_value * VMOV_ONE; + if (*p == '.') { + ++p; + value += vmov_parse_fractional_part(p); + } + next(); + return value; +} + +static uint8_t vmov_encode_immediate_value(uint32_t value) +{ + uint32_t limit; + uint32_t end = 0; + uint32_t beginning = 0; + int r = -1; + int n; + int i; + + limit = 32 * VMOV_ONE; + for (i = 0; i < 8; ++i) { + if (value < limit) { + end = limit; + limit >>= 1; + beginning = limit; + r = i; + } else + limit >>= 1; + } + if (r == -1 || value < beginning || value > end) { + tcc_error("invalid decimal number for vmov: %d", value); + return 0; + } + n = vmov_linear_approx_index(beginning, end, value); + return n | (((3 - r) & 0x7) << 4); +} + +// Not standalone. +static void asm_floating_point_immediate_data_processing_opcode_tail(TCCState *s1, int token, uint8_t coprocessor, uint8_t CRd) { + uint8_t opcode1 = 0; + uint8_t opcode2 = 0; + uint8_t operands[3] = {0, 0, 0}; + uint32_t immediate_value = 0; + int op_minus = 0; + uint8_t code; + + operands[0] = CRd; + + if (tok == '#' || tok == '$') { + next(); + } + if (tok == '-') { + op_minus = 1; + next(); + } + immediate_value = vmov_parse_immediate_value(); + + opcode1 = 11; // "Other" instruction + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vcmpeq_f32: + case TOK_ASM_vcmpeq_f64: + opcode2 = 2; + operands[1] = 5; + if (immediate_value) { + expect("Immediate value 0"); + return; + } + break; + case TOK_ASM_vcmpeeq_f32: + case TOK_ASM_vcmpeeq_f64: + opcode2 = 6; + operands[1] = 5; + if (immediate_value) { + expect("Immediate value 0"); + return; + } + break; + case TOK_ASM_vmoveq_f32: + case TOK_ASM_vmoveq_f64: + opcode2 = 0; + if (op_minus) + operands[1] = 0x8; + else + operands[1] = 0x0; + code = vmov_encode_immediate_value(immediate_value); + operands[1] |= code >> 4; + operands[2] = code & 0xF; + break; + default: + expect("known floating point with immediate instruction"); + return; + } + + if (coprocessor == CP_SINGLE_PRECISION_FLOAT) { + if (operands[0] & 1) + opcode1 |= 4; + operands[0] >>= 1; + } + + asm_emit_coprocessor_opcode(condition_code_of_token(token), coprocessor, opcode1, operands[0], operands[1], operands[2], opcode2, 0); +} + +static void asm_floating_point_reg_arm_reg_transfer_opcode_tail(TCCState *s1, int token, int coprocessor, int nb_arm_regs, int nb_ops, Operand ops[3]) { + uint8_t opcode1 = 0; + uint8_t opcode2 = 0; + switch (coprocessor) { + case CP_SINGLE_PRECISION_FLOAT: + // "vmov.f32 r2, s3" or "vmov.f32 s3, r2" + if (nb_ops != 2 || nb_arm_regs != 1) { + tcc_error("vmov.f32 only implemented for one VFP register operand and one ARM register operands"); + return; + } + if (ops[0].type != OP_REG32) { // determine mode: load or store + // need to swap operands 0 and 1 + memcpy(&ops[2], &ops[1], sizeof(ops[2])); + memcpy(&ops[1], &ops[0], sizeof(ops[1])); + memcpy(&ops[0], &ops[2], sizeof(ops[0])); + } else + opcode1 |= 1; + + if (ops[1].type == OP_VREG32) { + if (ops[1].reg & 1) + opcode2 |= 4; + ops[1].reg >>= 1; + } + + if (ops[0].type == OP_VREG32) { + if (ops[0].reg & 1) + opcode1 |= 4; + ops[0].reg >>= 1; + } + + asm_emit_coprocessor_opcode(condition_code_of_token(token), coprocessor, opcode1, ops[0].reg, (ops[1].type == OP_IM8) ? ops[1].e.v : ops[1].reg, 0x10, opcode2, 0); + break; + case CP_DOUBLE_PRECISION_FLOAT: + if (nb_ops != 3 || nb_arm_regs != 2) { + tcc_error("vmov.f32 only implemented for one VFP register operand and two ARM register operands"); + return; + } + // Determine whether it's a store into a VFP register (vmov "d1, r2, r3") rather than "vmov r2, r3, d1" + if (ops[0].type == OP_VREG64) { + if (ops[2].type == OP_REG32) { + Operand temp; + // need to rotate operand list to the left + memcpy(&temp, &ops[0], sizeof(temp)); + memcpy(&ops[0], &ops[1], sizeof(ops[0])); + memcpy(&ops[1], &ops[2], sizeof(ops[1])); + memcpy(&ops[2], &temp, sizeof(ops[2])); + } else { + tcc_error("vmov.f64 only implemented for one VFP register operand and two ARM register operands"); + return; + } + } else if (ops[0].type != OP_REG32 || ops[1].type != OP_REG32 || ops[2].type != OP_VREG64) { + tcc_error("vmov.f64 only implemented for one VFP register operand and two ARM register operands"); + return; + } else { + opcode1 |= 1; + } + asm_emit_coprocessor_data_transfer(condition_code_of_token(token), coprocessor, ops[0].reg, &ops[1], &ops[2], 0, 0, 0, 1, opcode1); + break; + default: + tcc_internal_error("unknown coprocessor"); + } +} + +static void asm_floating_point_vcvt_data_processing_opcode(TCCState *s1, int token) { + uint8_t coprocessor = 0; + Operand ops[3]; + uint8_t opcode1 = 11; + uint8_t opcode2 = 2; + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vcvtreq_s32_f64: + case TOK_ASM_vcvtreq_u32_f64: + case TOK_ASM_vcvteq_s32_f64: + case TOK_ASM_vcvteq_u32_f64: + case TOK_ASM_vcvteq_f64_s32: + case TOK_ASM_vcvteq_f64_u32: + case TOK_ASM_vcvteq_f32_f64: + coprocessor = CP_DOUBLE_PRECISION_FLOAT; + break; + case TOK_ASM_vcvtreq_s32_f32: + case TOK_ASM_vcvtreq_u32_f32: + case TOK_ASM_vcvteq_s32_f32: + case TOK_ASM_vcvteq_u32_f32: + case TOK_ASM_vcvteq_f32_s32: + case TOK_ASM_vcvteq_f32_u32: + case TOK_ASM_vcvteq_f64_f32: + coprocessor = CP_SINGLE_PRECISION_FLOAT; + break; + default: + tcc_error("Unknown coprocessor for instruction '%s'", get_tok_str(token, NULL)); + return; + } + + parse_operand(s1, &ops[0]); + ops[1].type = OP_IM8; + ops[1].e.v = 8; + /* floating-point -> integer */ + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vcvtreq_s32_f32: + case TOK_ASM_vcvtreq_s32_f64: + case TOK_ASM_vcvteq_s32_f32: + case TOK_ASM_vcvteq_s32_f64: + ops[1].e.v |= 1; // signed + /* fall through */ + case TOK_ASM_vcvteq_u32_f32: + case TOK_ASM_vcvteq_u32_f64: + case TOK_ASM_vcvtreq_u32_f32: + case TOK_ASM_vcvtreq_u32_f64: + ops[1].e.v |= 4; // to_integer (opc2) + break; + /* floating-point size conversion */ + case TOK_ASM_vcvteq_f64_f32: + case TOK_ASM_vcvteq_f32_f64: + ops[1].e.v = 7; + break; + } + + if (tok == ',') + next(); + else + expect("','"); + parse_operand(s1, &ops[2]); + + switch (ARM_INSTRUCTION_GROUP(token)) { + /* floating-point -> integer */ + case TOK_ASM_vcvteq_s32_f32: + case TOK_ASM_vcvteq_s32_f64: + case TOK_ASM_vcvteq_u32_f32: + case TOK_ASM_vcvteq_u32_f64: + opcode2 |= 4; // round_zero + break; + + /* integer -> floating-point */ + case TOK_ASM_vcvteq_f64_s32: + case TOK_ASM_vcvteq_f32_s32: + opcode2 |= 4; // signed--special + break; + + /* floating-point size conversion */ + case TOK_ASM_vcvteq_f64_f32: + case TOK_ASM_vcvteq_f32_f64: + opcode2 |= 4; // always set + break; + } + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vcvteq_f64_u32: + case TOK_ASM_vcvteq_f64_s32: + case TOK_ASM_vcvteq_f64_f32: + if (ops[0].type == OP_VREG64 && ops[2].type == OP_VREG32) { + } else { + expect("d, s"); + return; + } + break; + default: + if (coprocessor == CP_SINGLE_PRECISION_FLOAT) { + if (ops[0].type == OP_VREG32 && ops[2].type == OP_VREG32) { + } else { + expect("s, s"); + return; + } + } else if (coprocessor == CP_DOUBLE_PRECISION_FLOAT) { + if (ops[0].type == OP_VREG32 && ops[2].type == OP_VREG64) { + } else { + expect("s, d"); + return; + } + } + } + + if (ops[2].type == OP_VREG32) { + if (ops[2].reg & 1) + opcode2 |= 1; + ops[2].reg >>= 1; + } + if (ops[0].type == OP_VREG32) { + if (ops[0].reg & 1) + opcode1 |= 4; + ops[0].reg >>= 1; + } + asm_emit_coprocessor_opcode(condition_code_of_token(token), coprocessor, opcode1, ops[0].reg, (ops[1].type == OP_IM8) ? ops[1].e.v : ops[1].reg, (ops[2].type == OP_IM8) ? ops[2].e.v : ops[2].reg, opcode2, 0); +} + +static void asm_floating_point_data_processing_opcode(TCCState *s1, int token) { + uint8_t coprocessor = CP_SINGLE_PRECISION_FLOAT; + uint8_t opcode1 = 0; + uint8_t opcode2 = 0; // (0 || 2) | register selection + Operand ops[3]; + uint8_t nb_ops = 0; + int vmov = 0; + int nb_arm_regs = 0; + +/* TODO: + Instruction opcode opcode2 Reason + ============================================================= + - 1?00 ?1? Undefined + VFNMS 1?01 ?0? Must be unconditional + VFNMA 1?01 ?1? Must be unconditional + VFMA 1?10 ?0? Must be unconditional + VFMS 1?10 ?1? Must be unconditional + + VMOV Fd, Fm + VMOV Sn, Sm, Rd, Rn + VMOV Rd, Rn, Sn, Sm + VMOV Dn[0], Rd + VMOV Rd, Dn[0] + VMOV Dn[1], Rd + VMOV Rd, Dn[1] +*/ + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vmlaeq_f64: + case TOK_ASM_vmlseq_f64: + case TOK_ASM_vnmlseq_f64: + case TOK_ASM_vnmlaeq_f64: + case TOK_ASM_vmuleq_f64: + case TOK_ASM_vnmuleq_f64: + case TOK_ASM_vaddeq_f64: + case TOK_ASM_vsubeq_f64: + case TOK_ASM_vdiveq_f64: + case TOK_ASM_vnegeq_f64: + case TOK_ASM_vabseq_f64: + case TOK_ASM_vsqrteq_f64: + case TOK_ASM_vcmpeq_f64: + case TOK_ASM_vcmpeeq_f64: + case TOK_ASM_vmoveq_f64: + coprocessor = CP_DOUBLE_PRECISION_FLOAT; + } + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vmoveq_f32: + case TOK_ASM_vmoveq_f64: + vmov = 1; + break; + } + + for (nb_ops = 0; nb_ops < 3; ) { + // Note: Necessary because parse_operand can't parse decimal numerals. + if (nb_ops == 1 && (tok == '#' || tok == '$' || tok == TOK_PPNUM || tok == '-')) { + asm_floating_point_immediate_data_processing_opcode_tail(s1, token, coprocessor, ops[0].reg); + return; + } + parse_operand(s1, &ops[nb_ops]); + if (vmov && ops[nb_ops].type == OP_REG32) { + ++nb_arm_regs; + } else if (ops[nb_ops].type == OP_VREG32) { + if (coprocessor != CP_SINGLE_PRECISION_FLOAT) { + expect("'s'"); + return; + } + } else if (ops[nb_ops].type == OP_VREG64) { + if (coprocessor != CP_DOUBLE_PRECISION_FLOAT) { + expect("'d'"); + return; + } + } else { + expect("floating point register"); + return; + } + ++nb_ops; + if (tok == ',') + next(); + else + break; + } + + if (nb_arm_regs == 0) { + if (nb_ops == 2) { // implicit + memcpy(&ops[2], &ops[1], sizeof(ops[1])); // move ops[2] + memcpy(&ops[1], &ops[0], sizeof(ops[0])); // ops[1] was implicit + nb_ops = 3; + } + if (nb_ops < 3) { + tcc_error("Not enough operands for '%s' (%u)", get_tok_str(token, NULL), nb_ops); + return; + } + } + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vmlaeq_f32: + case TOK_ASM_vmlaeq_f64: + opcode1 = 0; + opcode2 = 0; + break; + case TOK_ASM_vmlseq_f32: + case TOK_ASM_vmlseq_f64: + opcode1 = 0; + opcode2 = 2; + break; + case TOK_ASM_vnmlseq_f32: + case TOK_ASM_vnmlseq_f64: + opcode1 = 1; + opcode2 = 0; + break; + case TOK_ASM_vnmlaeq_f32: + case TOK_ASM_vnmlaeq_f64: + opcode1 = 1; + opcode2 = 2; + break; + case TOK_ASM_vmuleq_f32: + case TOK_ASM_vmuleq_f64: + opcode1 = 2; + opcode2 = 0; + break; + case TOK_ASM_vnmuleq_f32: + case TOK_ASM_vnmuleq_f64: + opcode1 = 2; + opcode2 = 2; + break; + case TOK_ASM_vaddeq_f32: + case TOK_ASM_vaddeq_f64: + opcode1 = 3; + opcode2 = 0; + break; + case TOK_ASM_vsubeq_f32: + case TOK_ASM_vsubeq_f64: + opcode1 = 3; + opcode2 = 2; + break; + case TOK_ASM_vdiveq_f32: + case TOK_ASM_vdiveq_f64: + opcode1 = 8; + opcode2 = 0; + break; + case TOK_ASM_vnegeq_f32: + case TOK_ASM_vnegeq_f64: + opcode1 = 11; // Other" instruction + opcode2 = 2; + ops[1].type = OP_IM8; + ops[1].e.v = 1; + break; + case TOK_ASM_vabseq_f32: + case TOK_ASM_vabseq_f64: + opcode1 = 11; // "Other" instruction + opcode2 = 6; + ops[1].type = OP_IM8; + ops[1].e.v = 0; + break; + case TOK_ASM_vsqrteq_f32: + case TOK_ASM_vsqrteq_f64: + opcode1 = 11; // "Other" instruction + opcode2 = 6; + ops[1].type = OP_IM8; + ops[1].e.v = 1; + break; + case TOK_ASM_vcmpeq_f32: + case TOK_ASM_vcmpeq_f64: + opcode1 = 11; // "Other" instruction + opcode2 = 2; + ops[1].type = OP_IM8; + ops[1].e.v = 4; + break; + case TOK_ASM_vcmpeeq_f32: + case TOK_ASM_vcmpeeq_f64: + opcode1 = 11; // "Other" instruction + opcode2 = 6; + ops[1].type = OP_IM8; + ops[1].e.v = 4; + break; + case TOK_ASM_vmoveq_f32: + case TOK_ASM_vmoveq_f64: + if (nb_arm_regs > 0) { // vmov.f32 r2, s3 or similar + asm_floating_point_reg_arm_reg_transfer_opcode_tail(s1, token, coprocessor, nb_arm_regs, nb_ops, ops); + return; + } else { + opcode1 = 11; // "Other" instruction + opcode2 = 2; + ops[1].type = OP_IM8; + ops[1].e.v = 0; + } + break; + default: + expect("known floating point instruction"); + return; + } + + if (coprocessor == CP_SINGLE_PRECISION_FLOAT) { + if (ops[2].type == OP_VREG32) { + if (ops[2].reg & 1) + opcode2 |= 1; + ops[2].reg >>= 1; + } + + if (ops[1].type == OP_VREG32) { + if (ops[1].reg & 1) + opcode2 |= 4; + ops[1].reg >>= 1; + } + + if (ops[0].type == OP_VREG32) { + if (ops[0].reg & 1) + opcode1 |= 4; + ops[0].reg >>= 1; + } + } + + asm_emit_coprocessor_opcode(condition_code_of_token(token), coprocessor, opcode1, ops[0].reg, (ops[1].type == OP_IM8) ? ops[1].e.v : ops[1].reg, (ops[2].type == OP_IM8) ? ops[2].e.v : ops[2].reg, opcode2, 0); +} + +static int asm_parse_vfp_status_regvar(int t) +{ + switch (t) { + case TOK_ASM_fpsid: + return 0; + case TOK_ASM_fpscr: + return 1; + case TOK_ASM_fpexc: + return 8; + default: + return -1; + } +} + +static void asm_floating_point_status_register_opcode(TCCState* s1, int token) +{ + uint8_t coprocessor = CP_SINGLE_PRECISION_FLOAT; + uint8_t opcode; + int vfp_sys_reg = -1; + Operand arm_operand; + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_vmrseq: + opcode = 0xf; + if (tok == TOK_ASM_apsr_nzcv) { + arm_operand.type = OP_REG32; + arm_operand.reg = 15; // not PC + next(); // skip apsr_nzcv + } else { + parse_operand(s1, &arm_operand); + if (arm_operand.type == OP_REG32 && arm_operand.reg == 15) { + tcc_error("'%s' does not support 'pc' as operand", get_tok_str(token, NULL)); + return; + } + } + + if (tok != ',') + expect("','"); + else + next(); // skip ',' + vfp_sys_reg = asm_parse_vfp_status_regvar(tok); + next(); // skip vfp sys reg + if (arm_operand.type == OP_REG32 && arm_operand.reg == 15 && vfp_sys_reg != 1) { + tcc_error("'%s' only supports the variant 'vmrs apsr_nzcv, fpscr' here", get_tok_str(token, NULL)); + return; + } + break; + case TOK_ASM_vmsreq: + opcode = 0xe; + vfp_sys_reg = asm_parse_vfp_status_regvar(tok); + next(); // skip vfp sys reg + if (tok != ',') + expect("','"); + else + next(); // skip ',' + parse_operand(s1, &arm_operand); + if (arm_operand.type == OP_REG32 && arm_operand.reg == 15) { + tcc_error("'%s' does not support 'pc' as operand", get_tok_str(token, NULL)); + return; + } + break; + default: + expect("floating point status register instruction"); + return; + } + if (vfp_sys_reg == -1) { + expect("VFP system register"); + return; + } + if (arm_operand.type != OP_REG32) { + expect("ARM register"); + return; + } + asm_emit_coprocessor_opcode(condition_code_of_token(token), coprocessor, opcode, arm_operand.reg, vfp_sys_reg, 0x10, 0, 0); +} + +#endif + +static void asm_misc_single_data_transfer_opcode(TCCState *s1, int token) +{ + Operand ops[3]; + int exclam = 0; + int closed_bracket = 0; + int op2_minus = 0; + uint32_t opcode = (1 << 7) | (1 << 4); + + /* Note: + The argument syntax is exactly the same as in arm_single_data_transfer_opcode, except that there's no STREX argument form. + The main difference between this function and asm_misc_single_data_transfer_opcode is that the immediate values here must be smaller. + Also, the combination (P=0, W=1) is unpredictable here. + The immediate flag has moved to bit index 22--and its meaning has flipped. + The immediate value itself has been split into two parts: one at bits 11...8, one at bits 3...0 + bit 26 (Load/Store instruction) is unset here. + bits 7 and 4 are set here. */ + + // Here: 0 0 0 P U I W L << 20 + // [compare single data transfer: 0 1 I P U B W L << 20] + + parse_operand(s1, &ops[0]); + if (ops[0].type == OP_REG32) + opcode |= ENCODE_RD(ops[0].reg); + else { + expect("(destination operand) register"); + return; + } + if (tok != ',') + expect("at least two arguments"); + else + next(); // skip ',' + + if (tok != '[') + expect("'['"); + else + next(); // skip '[' + + parse_operand(s1, &ops[1]); + if (ops[1].type == OP_REG32) + opcode |= ENCODE_RN(ops[1].reg); + else { + expect("(first source operand) register"); + return; + } + if (tok == ']') { + next(); + closed_bracket = 1; + // exclam = 1; // implicit in hardware; don't do it in software + } + if (tok == ',') { + next(); // skip ',' + if (tok == '-') { + op2_minus = 1; + next(); + } + parse_operand(s1, &ops[2]); + } else { + // end of input expression in brackets--assume 0 offset + ops[2].type = OP_IM8; + ops[2].e.v = 0; + opcode |= 1 << 24; // add offset before transfer + } + if (!closed_bracket) { + if (tok != ']') + expect("']'"); + else + next(); // skip ']' + opcode |= 1 << 24; // add offset before transfer + if (tok == '!') { + exclam = 1; + next(); // skip '!' + } + } + + if (exclam) { + if ((opcode & (1 << 24)) == 0) { + tcc_error("result of '%s' would be unpredictable here", get_tok_str(token, NULL)); + return; + } + opcode |= 1 << 21; // write offset back into register + } + + if (ops[2].type == OP_IM32 || ops[2].type == OP_IM8 || ops[2].type == OP_IM8N) { + int v = ops[2].e.v; + if (op2_minus) + tcc_error("minus before '#' not supported for immediate values"); + if (v >= 0) { + opcode |= 1 << 23; // up + if (v >= 0x100) + tcc_error("offset out of range for '%s'", get_tok_str(token, NULL)); + else { + // bits 11...8: immediate hi nibble + // bits 3...0: immediate lo nibble + opcode |= (v & 0xF0) << 4; + opcode |= v & 0xF; + } + } else { // down + if (v <= -0x100) + tcc_error("offset out of range for '%s'", get_tok_str(token, NULL)); + else { + v = -v; + // bits 11...8: immediate hi nibble + // bits 3...0: immediate lo nibble + opcode |= (v & 0xF0) << 4; + opcode |= v & 0xF; + } + } + opcode |= 1 << 22; // not ENCODE_IMMEDIATE_FLAG; + } else if (ops[2].type == OP_REG32) { + if (!op2_minus) + opcode |= 1 << 23; // up + opcode |= ops[2].reg; + } else + expect("register"); + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_ldrsheq: + opcode |= 1 << 5; // halfword, not byte + /* fallthrough */ + case TOK_ASM_ldrsbeq: + opcode |= 1 << 6; // sign extend + opcode |= 1 << 20; // L + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_ldrheq: + opcode |= 1 << 5; // halfword, not byte + opcode |= 1 << 20; // L + asm_emit_opcode(token, opcode); + break; + case TOK_ASM_strheq: + opcode |= 1 << 5; // halfword, not byte + asm_emit_opcode(token, opcode); + break; + } +} + +/* Note: almost dupe of encbranch in arm-gen.c */ +static uint32_t encbranchoffset(int pos, int addr, int fail) +{ + addr-=pos+8; + addr/=4; + if(addr>=0x7fffff || addr<-0x800000) { + if(fail) + tcc_error("branch offset is too far"); + return 0; + } + return /*not 0x0A000000|*/(addr&0xffffff); +} + +static void asm_branch_opcode(TCCState *s1, int token) +{ + int jmp_disp = 0; + Operand op; + ExprValue e; + ElfSym *esym; + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_beq: + case TOK_ASM_bleq: + asm_expr(s1, &e); + esym = elfsym(e.sym); + if (!esym || esym->st_shndx != cur_text_section->sh_num) { + tcc_error("invalid branch target"); + return; + } + jmp_disp = encbranchoffset(ind, e.v + esym->st_value, 1); + break; + default: + parse_operand(s1, &op); + break; + } + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_beq: + asm_emit_opcode(token, (0xa << 24) | (jmp_disp & 0xffffff)); + break; + case TOK_ASM_bleq: + asm_emit_opcode(token, (0xb << 24) | (jmp_disp & 0xffffff)); + break; + case TOK_ASM_bxeq: + if (op.type != OP_REG32) + expect("register"); + else + asm_emit_opcode(token, (0x12fff1 << 4) | op.reg); + break; + case TOK_ASM_blxeq: + if (op.type != OP_REG32) + expect("register"); + else + asm_emit_opcode(token, (0x12fff3 << 4) | op.reg); + break; + default: + expect("branch instruction"); + } +} + +ST_FUNC void asm_opcode(TCCState *s1, int token) +{ + while (token == TOK_LINEFEED) { + next(); + token = tok; + } + if (token == TOK_EOF) + return; + if (token < TOK_ASM_nopeq) { // no condition code + switch (token) { + case TOK_ASM_cdp2: + asm_coprocessor_opcode(s1, token); + return; + case TOK_ASM_ldc2: + case TOK_ASM_ldc2l: + case TOK_ASM_stc2: + case TOK_ASM_stc2l: + asm_coprocessor_data_transfer_opcode(s1, token); + return; + default: + expect("instruction"); + return; + } + } + + switch (ARM_INSTRUCTION_GROUP(token)) { + case TOK_ASM_pusheq: + case TOK_ASM_popeq: + case TOK_ASM_stmdaeq: + case TOK_ASM_ldmdaeq: + case TOK_ASM_stmeq: + case TOK_ASM_ldmeq: + case TOK_ASM_stmiaeq: + case TOK_ASM_ldmiaeq: + case TOK_ASM_stmdbeq: + case TOK_ASM_ldmdbeq: + case TOK_ASM_stmibeq: + case TOK_ASM_ldmibeq: + asm_block_data_transfer_opcode(s1, token); + return; + case TOK_ASM_nopeq: + case TOK_ASM_wfeeq: + case TOK_ASM_wfieq: + asm_nullary_opcode(token); + return; + case TOK_ASM_swieq: + case TOK_ASM_svceq: + asm_unary_opcode(s1, token); + return; + case TOK_ASM_beq: + case TOK_ASM_bleq: + case TOK_ASM_bxeq: + case TOK_ASM_blxeq: + asm_branch_opcode(s1, token); + return; + case TOK_ASM_clzeq: + case TOK_ASM_sxtbeq: + case TOK_ASM_sxtheq: + case TOK_ASM_uxtbeq: + case TOK_ASM_uxtheq: + case TOK_ASM_movteq: + case TOK_ASM_movweq: + asm_binary_opcode(s1, token); + return; + + case TOK_ASM_ldreq: + case TOK_ASM_ldrbeq: + case TOK_ASM_streq: + case TOK_ASM_strbeq: + case TOK_ASM_ldrexeq: + case TOK_ASM_ldrexbeq: + case TOK_ASM_strexeq: + case TOK_ASM_strexbeq: + asm_single_data_transfer_opcode(s1, token); + return; + + case TOK_ASM_ldrheq: + case TOK_ASM_ldrsheq: + case TOK_ASM_ldrsbeq: + case TOK_ASM_strheq: + asm_misc_single_data_transfer_opcode(s1, token); + return; + + case TOK_ASM_andeq: + case TOK_ASM_eoreq: + case TOK_ASM_subeq: + case TOK_ASM_rsbeq: + case TOK_ASM_addeq: + case TOK_ASM_adceq: + case TOK_ASM_sbceq: + case TOK_ASM_rsceq: + case TOK_ASM_tsteq: + case TOK_ASM_teqeq: + case TOK_ASM_cmpeq: + case TOK_ASM_cmneq: + case TOK_ASM_orreq: + case TOK_ASM_moveq: + case TOK_ASM_biceq: + case TOK_ASM_mvneq: + case TOK_ASM_andseq: + case TOK_ASM_eorseq: + case TOK_ASM_subseq: + case TOK_ASM_rsbseq: + case TOK_ASM_addseq: + case TOK_ASM_adcseq: + case TOK_ASM_sbcseq: + case TOK_ASM_rscseq: +// case TOK_ASM_tstseq: +// case TOK_ASM_teqseq: +// case TOK_ASM_cmpseq: +// case TOK_ASM_cmnseq: + case TOK_ASM_orrseq: + case TOK_ASM_movseq: + case TOK_ASM_bicseq: + case TOK_ASM_mvnseq: + asm_data_processing_opcode(s1, token); + return; + + case TOK_ASM_lsleq: + case TOK_ASM_lslseq: + case TOK_ASM_lsreq: + case TOK_ASM_lsrseq: + case TOK_ASM_asreq: + case TOK_ASM_asrseq: + case TOK_ASM_roreq: + case TOK_ASM_rorseq: + case TOK_ASM_rrxseq: + case TOK_ASM_rrxeq: + asm_shift_opcode(s1, token); + return; + + case TOK_ASM_muleq: + case TOK_ASM_mulseq: + case TOK_ASM_mlaeq: + case TOK_ASM_mlaseq: + asm_multiplication_opcode(s1, token); + return; + + case TOK_ASM_smulleq: + case TOK_ASM_smullseq: + case TOK_ASM_umulleq: + case TOK_ASM_umullseq: + case TOK_ASM_smlaleq: + case TOK_ASM_smlalseq: + case TOK_ASM_umlaleq: + case TOK_ASM_umlalseq: + asm_long_multiplication_opcode(s1, token); + return; + + case TOK_ASM_cdpeq: + case TOK_ASM_mcreq: + case TOK_ASM_mrceq: + asm_coprocessor_opcode(s1, token); + return; + + case TOK_ASM_ldceq: + case TOK_ASM_ldcleq: + case TOK_ASM_stceq: + case TOK_ASM_stcleq: + asm_coprocessor_data_transfer_opcode(s1, token); + return; + +#if defined(TCC_ARM_VFP) + case TOK_ASM_vldreq: + case TOK_ASM_vstreq: + asm_floating_point_single_data_transfer_opcode(s1, token); + return; + + case TOK_ASM_vmlaeq_f32: + case TOK_ASM_vmlseq_f32: + case TOK_ASM_vnmlseq_f32: + case TOK_ASM_vnmlaeq_f32: + case TOK_ASM_vmuleq_f32: + case TOK_ASM_vnmuleq_f32: + case TOK_ASM_vaddeq_f32: + case TOK_ASM_vsubeq_f32: + case TOK_ASM_vdiveq_f32: + case TOK_ASM_vnegeq_f32: + case TOK_ASM_vabseq_f32: + case TOK_ASM_vsqrteq_f32: + case TOK_ASM_vcmpeq_f32: + case TOK_ASM_vcmpeeq_f32: + case TOK_ASM_vmoveq_f32: + case TOK_ASM_vmlaeq_f64: + case TOK_ASM_vmlseq_f64: + case TOK_ASM_vnmlseq_f64: + case TOK_ASM_vnmlaeq_f64: + case TOK_ASM_vmuleq_f64: + case TOK_ASM_vnmuleq_f64: + case TOK_ASM_vaddeq_f64: + case TOK_ASM_vsubeq_f64: + case TOK_ASM_vdiveq_f64: + case TOK_ASM_vnegeq_f64: + case TOK_ASM_vabseq_f64: + case TOK_ASM_vsqrteq_f64: + case TOK_ASM_vcmpeq_f64: + case TOK_ASM_vcmpeeq_f64: + case TOK_ASM_vmoveq_f64: + asm_floating_point_data_processing_opcode(s1, token); + return; + + case TOK_ASM_vcvtreq_s32_f32: + case TOK_ASM_vcvtreq_s32_f64: + case TOK_ASM_vcvteq_s32_f32: + case TOK_ASM_vcvteq_s32_f64: + case TOK_ASM_vcvtreq_u32_f32: + case TOK_ASM_vcvtreq_u32_f64: + case TOK_ASM_vcvteq_u32_f32: + case TOK_ASM_vcvteq_u32_f64: + case TOK_ASM_vcvteq_f64_s32: + case TOK_ASM_vcvteq_f32_s32: + case TOK_ASM_vcvteq_f64_u32: + case TOK_ASM_vcvteq_f32_u32: + case TOK_ASM_vcvteq_f64_f32: + case TOK_ASM_vcvteq_f32_f64: + asm_floating_point_vcvt_data_processing_opcode(s1, token); + return; + + case TOK_ASM_vpusheq: + case TOK_ASM_vpopeq: + case TOK_ASM_vldmeq: + case TOK_ASM_vldmiaeq: + case TOK_ASM_vldmdbeq: + case TOK_ASM_vstmeq: + case TOK_ASM_vstmiaeq: + case TOK_ASM_vstmdbeq: + asm_floating_point_block_data_transfer_opcode(s1, token); + return; + + case TOK_ASM_vmsreq: + case TOK_ASM_vmrseq: + asm_floating_point_status_register_opcode(s1, token); + return; +#endif + + default: + expect("known instruction"); + } +} + +ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier) +{ + int r, reg, size, val; + char buf[64]; + + r = sv->r; + if ((r & VT_VALMASK) == VT_CONST) { + if (!(r & VT_LVAL) && modifier != 'c' && modifier != 'n' && + modifier != 'P') + cstr_ccat(add_str, '#'); + if (r & VT_SYM) { + const char *name = get_tok_str(sv->sym->v, NULL); + if (sv->sym->v >= SYM_FIRST_ANOM) { + /* In case of anonymous symbols ("L.42", used + for static data labels) we can't find them + in the C symbol table when later looking up + this name. So enter them now into the asm label + list when we still know the symbol. */ + get_asm_sym(tok_alloc(name, strlen(name))->tok, sv->sym); + } + if (tcc_state->leading_underscore) + cstr_ccat(add_str, '_'); + cstr_cat(add_str, name, -1); + if ((uint32_t) sv->c.i == 0) + goto no_offset; + cstr_ccat(add_str, '+'); + } + val = sv->c.i; + if (modifier == 'n') + val = -val; + snprintf(buf, sizeof(buf), "%d", (int) sv->c.i); + cstr_cat(add_str, buf, -1); + no_offset:; + } else if ((r & VT_VALMASK) == VT_LOCAL) { + snprintf(buf, sizeof(buf), "[fp,#%d]", (int) sv->c.i); + cstr_cat(add_str, buf, -1); + } else if (r & VT_LVAL) { + reg = r & VT_VALMASK; + if (reg >= VT_CONST) + tcc_internal_error(""); + snprintf(buf, sizeof(buf), "[%s]", + get_tok_str(TOK_ASM_r0 + reg, NULL)); + cstr_cat(add_str, buf, -1); + } else { + /* register case */ + reg = r & VT_VALMASK; + if (reg >= VT_CONST) + tcc_internal_error(""); + + /* choose register operand size */ + if ((sv->type.t & VT_BTYPE) == VT_BYTE || + (sv->type.t & VT_BTYPE) == VT_BOOL) + size = 1; + else if ((sv->type.t & VT_BTYPE) == VT_SHORT) + size = 2; + else + size = 4; + + if (modifier == 'b') { + size = 1; + } else if (modifier == 'w') { + size = 2; + } else if (modifier == 'k') { + size = 4; + } + + switch (size) { + default: + reg = TOK_ASM_r0 + reg; + break; + } + snprintf(buf, sizeof(buf), "%s", get_tok_str(reg, NULL)); + cstr_cat(add_str, buf, -1); + } +} + +/* generate prolog and epilog code for asm statement */ +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg) +{ + uint8_t regs_allocated[NB_ASM_REGS]; + ASMOperand *op; + int i, reg; + uint32_t saved_regset = 0; + + // TODO: Check non-E ABI. + // Note: Technically, r13 (sp) is also callee-saved--but that does not matter yet + static const uint8_t reg_saved[] = { 4, 5, 6, 7, 8, 9 /* Note: sometimes special reg "sb" */ , 10, 11 }; + + /* mark all used registers */ + memcpy(regs_allocated, clobber_regs, sizeof(regs_allocated)); + for(i = 0; i < nb_operands;i++) { + op = &operands[i]; + if (op->reg >= 0) + regs_allocated[op->reg] = 1; + } + for(i = 0; i < sizeof(reg_saved)/sizeof(reg_saved[0]); i++) { + reg = reg_saved[i]; + if (regs_allocated[reg]) + saved_regset |= 1 << reg; + } + + if (!is_output) { // prolog + /* generate reg save code */ + if (saved_regset) + gen_le32(0xe92d0000 | saved_regset); // push {...} + + /* generate load code */ + for(i = 0; i < nb_operands; i++) { + op = &operands[i]; + if (op->reg >= 0) { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && + op->is_memory) { + /* memory reference case (for both input and + output cases) */ + SValue sv; + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; + sv.type.t = VT_PTR; + load(op->reg, &sv); + } else if (i >= nb_outputs || op->is_rw) { // not write-only + /* load value in register */ + load(op->reg, op->vt); + if (op->is_llong) + tcc_error("long long not implemented"); + } + } + } + } else { // epilog + /* generate save code */ + for(i = 0 ; i < nb_outputs; i++) { + op = &operands[i]; + if (op->reg >= 0) { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) { + if (!op->is_memory) { + SValue sv; + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL; + sv.type.t = VT_PTR; + load(out_reg, &sv); + + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | out_reg; + store(op->reg, &sv); + } + } else { + store(op->reg, op->vt); + if (op->is_llong) + tcc_error("long long not implemented"); + } + } + } + + /* generate reg restore code */ + if (saved_regset) + gen_le32(0xe8bd0000 | saved_regset); // pop {...} + } +} + +/* return the constraint priority (we allocate first the lowest + numbered constraints) */ +static inline int constraint_priority(const char *str) +{ + int priority, c, pr; + + /* we take the lowest priority */ + priority = 0; + for(;;) { + c = *str; + if (c == '\0') + break; + str++; + switch(c) { + case 'l': // in ARM mode, that's an alias for 'r' [ARM]. + case 'r': // register [general] + case 'p': // valid memory address for load,store [general] + pr = 3; + break; + case 'M': // integer constant for shifts [ARM] + case 'I': // integer valid for data processing instruction immediate + case 'J': // integer in range -4095...4095 + + case 'i': // immediate integer operand, including symbolic constants [general] + case 'm': // memory operand [general] + case 'g': // general-purpose-register, memory, immediate integer [general] + pr = 4; + break; + default: + tcc_error("unknown constraint '%c'", c); + pr = 0; + } + if (pr > priority) + priority = pr; + } + return priority; +} + +static const char *skip_constraint_modifiers(const char *p) +{ + /* Constraint modifier: + = Operand is written to by this instruction + + Operand is both read and written to by this instruction + % Instruction is commutative for this operand and the following operand. + + Per-alternative constraint modifier: + & Operand is clobbered before the instruction is done using the input operands + */ + while (*p == '=' || *p == '&' || *p == '+' || *p == '%') + p++; + return p; +} + +#define REG_OUT_MASK 0x01 +#define REG_IN_MASK 0x02 + +#define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask) + +ST_FUNC void asm_compute_constraints(ASMOperand *operands, + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg) +{ + /* overall format: modifier, then ,-seperated list of alternatives; all operands for a single instruction must have the same number of alternatives */ + /* TODO: Simple constraints + whitespace ignored + o memory operand that is offsetable + V memory but not offsetable + < memory operand with autodecrement addressing is allowed. Restrictions apply. + > memory operand with autoincrement addressing is allowed. Restrictions apply. + n immediate integer operand with a known numeric value + E immediate floating operand (const_double) is allowed, but only if target=host + F immediate floating operand (const_double or const_vector) is allowed + s immediate integer operand whose value is not an explicit integer + X any operand whatsoever + 0...9 (postfix); (can also be more than 1 digit number); an operand that matches the specified operand number is allowed + */ + + /* TODO: ARM constraints: + k the stack pointer register + G the floating-point constant 0.0 + Q memory reference where the exact address is in a single register ("m" is preferable for asm statements) + R an item in the constant pool + S symbol in the text segment of the current file +[ Uv memory reference suitable for VFP load/store insns (reg+constant offset)] +[ Uy memory reference suitable for iWMMXt load/store instructions] + Uq memory reference suitable for the ARMv4 ldrsb instruction + */ + ASMOperand *op; + int sorted_op[MAX_ASM_OPERANDS]; + int i, j, k, p1, p2, tmp, reg, c, reg_mask; + const char *str; + uint8_t regs_allocated[NB_ASM_REGS]; + + /* init fields */ + for (i = 0; i < nb_operands; i++) { + op = &operands[i]; + op->input_index = -1; + op->ref_index = -1; + op->reg = -1; + op->is_memory = 0; + op->is_rw = 0; + } + /* compute constraint priority and evaluate references to output + constraints if input constraints */ + for (i = 0; i < nb_operands; i++) { + op = &operands[i]; + str = op->constraint; + str = skip_constraint_modifiers(str); + if (isnum(*str) || *str == '[') { + /* this is a reference to another constraint */ + k = find_constraint(operands, nb_operands, str, NULL); + if ((unsigned) k >= i || i < nb_outputs) + tcc_error("invalid reference in constraint %d ('%s')", + i, str); + op->ref_index = k; + if (operands[k].input_index >= 0) + tcc_error("cannot reference twice the same operand"); + operands[k].input_index = i; + op->priority = 5; + } else if ((op->vt->r & VT_VALMASK) == VT_LOCAL + && op->vt->sym + && (reg = op->vt->sym->r & VT_VALMASK) < VT_CONST) { + op->priority = 1; + op->reg = reg; + } else { + op->priority = constraint_priority(str); + } + } + + /* sort operands according to their priority */ + for (i = 0; i < nb_operands; i++) + sorted_op[i] = i; + for (i = 0; i < nb_operands - 1; i++) { + for (j = i + 1; j < nb_operands; j++) { + p1 = operands[sorted_op[i]].priority; + p2 = operands[sorted_op[j]].priority; + if (p2 < p1) { + tmp = sorted_op[i]; + sorted_op[i] = sorted_op[j]; + sorted_op[j] = tmp; + } + } + } + + for (i = 0; i < NB_ASM_REGS; i++) { + if (clobber_regs[i]) + regs_allocated[i] = REG_IN_MASK | REG_OUT_MASK; + else + regs_allocated[i] = 0; + } + /* sp cannot be used */ + regs_allocated[13] = REG_IN_MASK | REG_OUT_MASK; + /* fp cannot be used yet */ + regs_allocated[11] = REG_IN_MASK | REG_OUT_MASK; + + /* allocate registers and generate corresponding asm moves */ + for (i = 0; i < nb_operands; i++) { + j = sorted_op[i]; + op = &operands[j]; + str = op->constraint; + /* no need to allocate references */ + if (op->ref_index >= 0) + continue; + /* select if register is used for output, input or both */ + if (op->input_index >= 0) { + reg_mask = REG_IN_MASK | REG_OUT_MASK; + } else if (j < nb_outputs) { + reg_mask = REG_OUT_MASK; + } else { + reg_mask = REG_IN_MASK; + } + if (op->reg >= 0) { + if (is_reg_allocated(op->reg)) + tcc_error + ("asm regvar requests register that's taken already"); + reg = op->reg; + goto reg_found; + } + try_next: + c = *str++; + switch (c) { + case '=': // Operand is written-to + goto try_next; + case '+': // Operand is both READ and written-to + op->is_rw = 1; + /* FALL THRU */ + case '&': // Operand is clobbered before the instruction is done using the input operands + if (j >= nb_outputs) + tcc_error("'%c' modifier can only be applied to outputs", + c); + reg_mask = REG_IN_MASK | REG_OUT_MASK; + goto try_next; + case 'l': // In non-thumb mode, alias for 'r'--otherwise r0-r7 [ARM] + case 'r': // general-purpose register + case 'p': // loadable/storable address + /* any general register */ + for (reg = 0; reg <= 8; reg++) { + if (!is_reg_allocated(reg)) + goto reg_found; + } + goto try_next; + reg_found: + /* now we can reload in the register */ + op->is_llong = 0; + op->reg = reg; + regs_allocated[reg] |= reg_mask; + break; + case 'I': // integer that is valid as an data processing instruction immediate (0...255, rotated by a multiple of two) + case 'J': // integer in the range -4095 to 4095 [ARM] + case 'K': // integer that satisfies constraint I when inverted (one's complement) + case 'L': // integer that satisfies constraint I when inverted (two's complement) + case 'i': // immediate integer operand, including symbolic constants + if (!((op->vt->r & (VT_VALMASK | VT_LVAL)) == VT_CONST)) + goto try_next; + break; + case 'M': // integer in the range 0 to 32 + if (! + ((op->vt->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == + VT_CONST)) + goto try_next; + break; + case 'm': // memory operand + case 'g': + /* nothing special to do because the operand is already in + memory, except if the pointer itself is stored in a + memory variable (VT_LLOCAL case) */ + /* XXX: fix constant case */ + /* if it is a reference to a memory zone, it must lie + in a register, so we reserve the register in the + input registers and a load will be generated + later */ + if (j < nb_outputs || c == 'm') { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) { + /* any general register */ + for (reg = 0; reg <= 8; reg++) { + if (!(regs_allocated[reg] & REG_IN_MASK)) + goto reg_found1; + } + goto try_next; + reg_found1: + /* now we can reload in the register */ + regs_allocated[reg] |= REG_IN_MASK; + op->reg = reg; + op->is_memory = 1; + } + } + break; + default: + tcc_error("asm constraint %d ('%s') could not be satisfied", + j, op->constraint); + break; + } + /* if a reference is present for that operand, we assign it too */ + if (op->input_index >= 0) { + operands[op->input_index].reg = op->reg; + operands[op->input_index].is_llong = op->is_llong; + } + } + + /* compute out_reg. It is used to store outputs registers to memory + locations references by pointers (VT_LLOCAL case) */ + *pout_reg = -1; + for (i = 0; i < nb_operands; i++) { + op = &operands[i]; + if (op->reg >= 0 && + (op->vt->r & VT_VALMASK) == VT_LLOCAL && !op->is_memory) { + for (reg = 0; reg <= 8; reg++) { + if (!(regs_allocated[reg] & REG_OUT_MASK)) + goto reg_found2; + } + tcc_error("could not find free output register for reloading"); + reg_found2: + *pout_reg = reg; + break; + } + } + + /* print sorted constraints */ +#ifdef ASM_DEBUG + for (i = 0; i < nb_operands; i++) { + j = sorted_op[i]; + op = &operands[j]; + printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n", + j, + op->id ? get_tok_str(op->id, NULL) : "", + op->constraint, op->vt->r, op->reg); + } + if (*pout_reg >= 0) + printf("out_reg=%d\n", *pout_reg); +#endif +} + +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str) +{ + int reg; + TokenSym *ts; + + if (!strcmp(str, "memory") || + !strcmp(str, "cc") || + !strcmp(str, "flags")) + return; + ts = tok_alloc(str, strlen(str)); + reg = asm_parse_regvar(ts->tok); + if (reg == -1) { + tcc_error("invalid clobber register '%s'", str); + } + clobber_regs[reg] = 1; +} + +/* If T refers to a register then return the register number and type. + Otherwise return -1. */ +ST_FUNC int asm_parse_regvar (int t) +{ + if (t >= TOK_ASM_r0 && t <= TOK_ASM_pc) { /* register name */ + switch (t) { + case TOK_ASM_fp: + return TOK_ASM_r11 - TOK_ASM_r0; + case TOK_ASM_ip: + return TOK_ASM_r12 - TOK_ASM_r0; + case TOK_ASM_sp: + return TOK_ASM_r13 - TOK_ASM_r0; + case TOK_ASM_lr: + return TOK_ASM_r14 - TOK_ASM_r0; + case TOK_ASM_pc: + return TOK_ASM_r15 - TOK_ASM_r0; + default: + return t - TOK_ASM_r0; + } + } else + return -1; +} + +/*************************************************************/ +#endif /* ndef TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/arm-gen.c b/05/tcc-final/arm-gen.c new file mode 100644 index 0000000..e0d98d0 --- /dev/null +++ b/05/tcc-final/arm-gen.c @@ -0,0 +1,2391 @@ +/* + * ARMv4 code generator for TCC + * + * Copyright (c) 2003 Daniel Glöckner + * Copyright (c) 2012 Thomas Preud'homme + * + * Based on i386-gen.c by Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +#if defined(TCC_ARM_EABI) && !defined(TCC_ARM_VFP) +#error "Currently TinyCC only supports float computation with VFP instructions" +#endif + +/* number of available registers */ +#ifdef TCC_ARM_VFP +#define NB_REGS 13 +#else +#define NB_REGS 9 +#endif + +#ifndef TCC_CPU_VERSION +# define TCC_CPU_VERSION 5 +#endif + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_R0 0x0004 +#define RC_R1 0x0008 +#define RC_R2 0x0010 +#define RC_R3 0x0020 +#define RC_R12 0x0040 +#define RC_F0 0x0080 +#define RC_F1 0x0100 +#define RC_F2 0x0200 +#define RC_F3 0x0400 +#ifdef TCC_ARM_VFP +#define RC_F4 0x0800 +#define RC_F5 0x1000 +#define RC_F6 0x2000 +#define RC_F7 0x4000 +#endif +#define RC_IRET RC_R0 /* function return: integer register */ +#define RC_IRE2 RC_R1 /* function return: second integer register */ +#define RC_FRET RC_F0 /* function return: float register */ + +/* pretty names for the registers */ +enum { + TREG_R0 = 0, + TREG_R1, + TREG_R2, + TREG_R3, + TREG_R12, + TREG_F0, + TREG_F1, + TREG_F2, + TREG_F3, +#ifdef TCC_ARM_VFP + TREG_F4, + TREG_F5, + TREG_F6, + TREG_F7, +#endif + TREG_SP = 13, + TREG_LR, +}; + +#ifdef TCC_ARM_VFP +#define T2CPR(t) (((t) & VT_BTYPE) != VT_FLOAT ? 0x100 : 0) +#endif + +/* return registers for function */ +#define REG_IRET TREG_R0 /* single word int return register */ +#define REG_IRE2 TREG_R1 /* second word return register (for long long) */ +#define REG_FRET TREG_F0 /* float return register */ + +#ifdef TCC_ARM_EABI +#define TOK___divdi3 TOK___aeabi_ldivmod +#define TOK___moddi3 TOK___aeabi_ldivmod +#define TOK___udivdi3 TOK___aeabi_uldivmod +#define TOK___umoddi3 TOK___aeabi_uldivmod +#endif + +/* defined if function parameters must be evaluated in reverse order */ +#define INVERT_FUNC_PARAMS + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#ifdef TCC_ARM_VFP +#define LDOUBLE_SIZE 8 +#endif + +#ifndef LDOUBLE_SIZE +#define LDOUBLE_SIZE 8 +#endif + +#ifdef TCC_ARM_EABI +#define LDOUBLE_ALIGN 8 +#else +#define LDOUBLE_ALIGN 4 +#endif + +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 8 + +#define CHAR_IS_UNSIGNED + +#ifdef TCC_ARM_HARDFLOAT +# define ARM_FLOAT_ABI ARM_HARD_FLOAT +#else +# define ARM_FLOAT_ABI ARM_SOFTFP_FLOAT +#endif + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#define USING_GLOBALS +#include "tcc.h" + +ST_DATA const char * const target_machine_defs = + "__arm__\0" + "__arm\0" + "arm\0" + "__arm_elf__\0" + "__arm_elf\0" + "arm_elf\0" + "__ARM_ARCH_4__\0" + "__ARMEL__\0" + "__APCS_32__\0" +#if defined TCC_ARM_EABI + "__ARM_EABI__\0" +#endif + ; + +enum float_abi float_abi; + +ST_DATA const int reg_classes[NB_REGS] = { + /* r0 */ RC_INT | RC_R0, + /* r1 */ RC_INT | RC_R1, + /* r2 */ RC_INT | RC_R2, + /* r3 */ RC_INT | RC_R3, + /* r12 */ RC_INT | RC_R12, + /* f0 */ RC_FLOAT | RC_F0, + /* f1 */ RC_FLOAT | RC_F1, + /* f2 */ RC_FLOAT | RC_F2, + /* f3 */ RC_FLOAT | RC_F3, +#ifdef TCC_ARM_VFP + /* d4/s8 */ RC_FLOAT | RC_F4, +/* d5/s10 */ RC_FLOAT | RC_F5, +/* d6/s12 */ RC_FLOAT | RC_F6, +/* d7/s14 */ RC_FLOAT | RC_F7, +#endif +}; + +static int func_sub_sp_offset, last_itod_magic; +static int leaffunc; + +#if defined(CONFIG_TCC_BCHECK) +static addr_t func_bound_offset; +static unsigned long func_bound_ind; +ST_DATA int func_bound_add_epilog; +#endif + +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) +static CType float_type, double_type, func_float_type, func_double_type; +ST_FUNC void arm_init(struct TCCState *s) +{ + float_type.t = VT_FLOAT; + double_type.t = VT_DOUBLE; + func_float_type.t = VT_FUNC; + func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD); + func_double_type.t = VT_FUNC; + func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD); + + float_abi = s->float_abi; +#ifndef TCC_ARM_HARDFLOAT +// XXX: Works on OpenBSD +// # warning "soft float ABI currently not supported: default to softfp" +#endif +} +#else +#define func_float_type func_old_type +#define func_double_type func_old_type +#define func_ldouble_type func_old_type +ST_FUNC void arm_init(struct TCCState *s) +{ +#if 0 +#if !defined (TCC_ARM_VFP) + tcc_warning("Support for FPA is deprecated and will be removed in next" + " release"); +#endif +#if !defined (TCC_ARM_EABI) + tcc_warning("Support for OABI is deprecated and will be removed in next" + " release"); +#endif +#endif +} +#endif + +#define CHECK_R(r) ((r) >= TREG_R0 && (r) <= TREG_LR) + +static int two2mask(int a,int b) { + if (!CHECK_R(a) || !CHECK_R(b)) + tcc_error("compiler error! registers %i,%i is not valid",a,b); + return (reg_classes[a]|reg_classes[b])&~(RC_INT|RC_FLOAT); +} + +static int regmask(int r) { + if (!CHECK_R(r)) + tcc_error("compiler error! register %i is not valid",r); + return reg_classes[r]&~(RC_INT|RC_FLOAT); +} + +/******************************************************/ + +#if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP) +const char *default_elfinterp(struct TCCState *s) +{ + if (s->float_abi == ARM_HARD_FLOAT) + return "/lib/ld-linux-armhf.so.3"; + else + return "/lib/ld-linux.so.3"; +} +#endif + +void o(uint32_t i) +{ + /* this is a good place to start adding big-endian support*/ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 4; + if (!cur_text_section) + tcc_error("compiler error! This happens f.ex. if the compiler\n" + "can't evaluate constant expressions outside of a function."); + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind++] = i&255; + i>>=8; + cur_text_section->data[ind++] = i&255; + i>>=8; + cur_text_section->data[ind++] = i&255; + i>>=8; + cur_text_section->data[ind++] = i; +} + +static uint32_t stuff_const(uint32_t op, uint32_t c) +{ + int try_neg=0; + uint32_t nc = 0, negop = 0; + + switch(op&0x1F00000) + { + case 0x800000: //add + case 0x400000: //sub + try_neg=1; + negop=op^0xC00000; + nc=-c; + break; + case 0x1A00000: //mov + case 0x1E00000: //mvn + try_neg=1; + negop=op^0x400000; + nc=~c; + break; + case 0x200000: //xor + if(c==~0) + return (op&0xF010F000)|((op>>16)&0xF)|0x1E00000; + break; + case 0x0: //and + if(c==~0) + return (op&0xF010F000)|((op>>16)&0xF)|0x1A00000; + case 0x1C00000: //bic + try_neg=1; + negop=op^0x1C00000; + nc=~c; + break; + case 0x1800000: //orr + if(c==~0) + return (op&0xFFF0FFFF)|0x1E00000; + break; + } + do { + uint32_t m; + int i; + if(c<256) /* catch undefined <<32 */ + return op|c; + for(i=2;i<32;i+=2) { + m=(0xff>>i)|(0xff<<(32-i)); + if(!(c&~m)) + return op|(i<<7)|(c<>(32-i)); + } + op=negop; + c=nc; + } while(try_neg--); + return 0; +} + + +//only add,sub +void stuff_const_harder(uint32_t op, uint32_t v) { + uint32_t x; + x=stuff_const(op,v); + if(x) + o(x); + else { + uint32_t a[16], nv, no, o2, n2; + int i,j,k; + a[0]=0xff; + o2=(op&0xfff0ffff)|((op&0xf000)<<4);; + for(i=1;i<16;i++) + a[i]=(a[i-1]>>2)|(a[i-1]<<30); + for(i=0;i<12;i++) + for(j=i<4?i+12:15;j>=i+4;j--) + if((v&(a[i]|a[j]))==v) { + o(stuff_const(op,v&a[i])); + o(stuff_const(o2,v&a[j])); + return; + } + no=op^0xC00000; + n2=o2^0xC00000; + nv=-v; + for(i=0;i<12;i++) + for(j=i<4?i+12:15;j>=i+4;j--) + if((nv&(a[i]|a[j]))==nv) { + o(stuff_const(no,nv&a[i])); + o(stuff_const(n2,nv&a[j])); + return; + } + for(i=0;i<8;i++) + for(j=i+4;j<12;j++) + for(k=i<4?i+12:15;k>=j+4;k--) + if((v&(a[i]|a[j]|a[k]))==v) { + o(stuff_const(op,v&a[i])); + o(stuff_const(o2,v&a[j])); + o(stuff_const(o2,v&a[k])); + return; + } + no=op^0xC00000; + nv=-v; + for(i=0;i<8;i++) + for(j=i+4;j<12;j++) + for(k=i<4?i+12:15;k>=j+4;k--) + if((nv&(a[i]|a[j]|a[k]))==nv) { + o(stuff_const(no,nv&a[i])); + o(stuff_const(n2,nv&a[j])); + o(stuff_const(n2,nv&a[k])); + return; + } + o(stuff_const(op,v&a[0])); + o(stuff_const(o2,v&a[4])); + o(stuff_const(o2,v&a[8])); + o(stuff_const(o2,v&a[12])); + } +} + +uint32_t encbranch(int pos, int addr, int fail) +{ + addr-=pos+8; + addr/=4; + if(addr>=0x1000000 || addr<-0x1000000) { + if(fail) + tcc_error("FIXME: function bigger than 32MB"); + return 0; + } + return 0x0A000000|(addr&0xffffff); +} + +int decbranch(int pos) +{ + int x; + x=*(uint32_t *)(cur_text_section->data + pos); + x&=0x00ffffff; + if(x&0x800000) + x-=0x1000000; + return x*4+pos+8; +} + +/* output a symbol and patch all calls to it */ +void gsym_addr(int t, int a) +{ + uint32_t *x; + int lt; + while(t) { + x=(uint32_t *)(cur_text_section->data + t); + t=decbranch(lt=t); + if(a==lt+4) + *x=0xE1A00000; // nop + else { + *x &= 0xff000000; + *x |= encbranch(lt,a,1); + } + } +} + +#ifdef TCC_ARM_VFP +static uint32_t vfpr(int r) +{ + if(rTREG_F7) + tcc_error("compiler error! register %i is no vfp register",r); + return r - TREG_F0; +} +#else +static uint32_t fpr(int r) +{ + if(rTREG_F3) + tcc_error("compiler error! register %i is no fpa register",r); + return r - TREG_F0; +} +#endif + +static uint32_t intr(int r) +{ + if(r == TREG_R12) + return 12; + if(r >= TREG_R0 && r <= TREG_R3) + return r - TREG_R0; + if (!(r >= TREG_SP && r <= TREG_LR)) + tcc_error("compiler error! register %i is no int register",r); + return r + (13 - TREG_SP); +} + +static void calcaddr(uint32_t *base, int *off, int *sgn, int maxoff, unsigned shift) +{ + if(*off>maxoff || *off&((1<r & VT_SYM) + greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32); + o(sv->c.i); +#else + if(sv->r & VT_SYM) { + if (sv->sym->type.t & VT_STATIC) { + greloc(cur_text_section, sv->sym, ind, R_ARM_REL32); + o(sv->c.i - 12); + o(0xe080000f | (intr(r)<<12) | (intr(r)<<16)); // add rx,rx,pc + } + else { + greloc(cur_text_section, sv->sym, ind, R_ARM_GOT_PREL); + o(-12); + o(0xe080000f | (intr(r)<<12) | (intr(r)<<16)); // add rx,rx,pc + o(0xe5900000 | (intr(r)<<12) | (intr(r)<<16)); // ldr rx,[rx] + if (sv->c.i) + stuff_const_harder(0xe2800000 | (intr(r)<<12) | (intr(r)<<16), + sv->c.i); + } + } + else + o(sv->c.i); +#endif +} + +/* load 'r' from value 'sv' */ +void load(int r, SValue *sv) +{ + int v, ft, fc, fr, sign; + uint32_t op; + SValue v1; + + fr = sv->r; + ft = sv->type.t; + fc = sv->c.i; + + if(fc>=0) + sign=0; + else { + sign=1; + fc=-fc; + } + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + uint32_t base = 0xB; // fp + if(v == VT_LLOCAL) { + v1.type.t = VT_PTR; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = sv->c.i; + load(TREG_LR, &v1); + base = 14; /* lr */ + fc=sign=0; + v=VT_LOCAL; + } else if(v == VT_CONST) { + v1.type.t = VT_PTR; + v1.r = fr&~VT_LVAL; + v1.c.i = sv->c.i; + v1.sym=sv->sym; + load(TREG_LR, &v1); + base = 14; /* lr */ + fc=sign=0; + v=VT_LOCAL; + } else if(v < VT_CONST) { + base=intr(v); + fc=sign=0; + v=VT_LOCAL; + } + if(v == VT_LOCAL) { + if(is_float(ft)) { + calcaddr(&base,&fc,&sign,1020,2); +#ifdef TCC_ARM_VFP + op=0xED100A00; /* flds */ + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x100; /* flds -> fldd */ + o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16)); +#else + op=0xED100100; + if(!sign) + op|=0x800000; +#if LDOUBLE_SIZE == 8 + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x8000; +#else + if ((ft & VT_BTYPE) == VT_DOUBLE) + op|=0x8000; + else if ((ft & VT_BTYPE) == VT_LDOUBLE) + op|=0x400000; +#endif + o(op|(fpr(r)<<12)|(fc>>2)|(base<<16)); +#endif + } else if((ft & (VT_BTYPE|VT_UNSIGNED)) == VT_BYTE + || (ft & VT_BTYPE) == VT_SHORT) { + calcaddr(&base,&fc,&sign,255,0); + op=0xE1500090; + if ((ft & VT_BTYPE) == VT_SHORT) + op|=0x20; + if ((ft & VT_UNSIGNED) == 0) + op|=0x40; + if(!sign) + op|=0x800000; + o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf)); + } else { + calcaddr(&base,&fc,&sign,4095,0); + op=0xE5100000; + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) == VT_BYTE || (ft & VT_BTYPE) == VT_BOOL) + op|=0x400000; + o(op|(intr(r)<<12)|fc|(base<<16)); + } + return; + } + } else { + if (v == VT_CONST) { + op=stuff_const(0xE3A00000|(intr(r)<<12),sv->c.i); + if (fr & VT_SYM || !op) + load_value(sv, r); + else + o(op); + return; + } else if (v == VT_LOCAL) { + op=stuff_const(0xE28B0000|(intr(r)<<12),sv->c.i); + if (fr & VT_SYM || !op) { + load_value(sv, r); + o(0xE08B0000|(intr(r)<<12)|intr(r)); + } else + o(op); + return; + } else if(v == VT_CMP) { + o(mapcc(sv->c.i)|0x3A00001|(intr(r)<<12)); + o(mapcc(negcc(sv->c.i))|0x3A00000|(intr(r)<<12)); + return; + } else if (v == VT_JMP || v == VT_JMPI) { + int t; + t = v & 1; + o(0xE3A00000|(intr(r)<<12)|t); + o(0xEA000000); + gsym(sv->c.i); + o(0xE3A00000|(intr(r)<<12)|(t^1)); + return; + } else if (v < VT_CONST) { + if(is_float(ft)) +#ifdef TCC_ARM_VFP + o(0xEEB00A40|(vfpr(r)<<12)|vfpr(v)|T2CPR(ft)); /* fcpyX */ +#else + o(0xEE008180|(fpr(r)<<12)|fpr(v)); +#endif + else + o(0xE1A00000|(intr(r)<<12)|intr(v)); + return; + } + } + tcc_error("load unimplemented!"); +} + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue *sv) +{ + SValue v1; + int v, ft, fc, fr, sign; + uint32_t op; + + fr = sv->r; + ft = sv->type.t; + fc = sv->c.i; + + if(fc>=0) + sign=0; + else { + sign=1; + fc=-fc; + } + + v = fr & VT_VALMASK; + if (fr & VT_LVAL || fr == VT_LOCAL) { + uint32_t base = 0xb; /* fp */ + if(v < VT_CONST) { + base=intr(v); + v=VT_LOCAL; + fc=sign=0; + } else if(v == VT_CONST) { + v1.type.t = ft; + v1.r = fr&~VT_LVAL; + v1.c.i = sv->c.i; + v1.sym=sv->sym; + load(TREG_LR, &v1); + base = 14; /* lr */ + fc=sign=0; + v=VT_LOCAL; + } + if(v == VT_LOCAL) { + if(is_float(ft)) { + calcaddr(&base,&fc,&sign,1020,2); +#ifdef TCC_ARM_VFP + op=0xED000A00; /* fsts */ + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x100; /* fsts -> fstd */ + o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16)); +#else + op=0xED000100; + if(!sign) + op|=0x800000; +#if LDOUBLE_SIZE == 8 + if ((ft & VT_BTYPE) != VT_FLOAT) + op|=0x8000; +#else + if ((ft & VT_BTYPE) == VT_DOUBLE) + op|=0x8000; + if ((ft & VT_BTYPE) == VT_LDOUBLE) + op|=0x400000; +#endif + o(op|(fpr(r)<<12)|(fc>>2)|(base<<16)); +#endif + return; + } else if((ft & VT_BTYPE) == VT_SHORT) { + calcaddr(&base,&fc,&sign,255,0); + op=0xE14000B0; + if(!sign) + op|=0x800000; + o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf)); + } else { + calcaddr(&base,&fc,&sign,4095,0); + op=0xE5000000; + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) == VT_BYTE || (ft & VT_BTYPE) == VT_BOOL) + op|=0x400000; + o(op|(intr(r)<<12)|fc|(base<<16)); + } + return; + } + } + tcc_error("store unimplemented"); +} + +static void gadd_sp(int val) +{ + stuff_const_harder(0xE28DD000,val); +} + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + uint32_t x; + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + /* constant case */ + if(vtop->r & VT_SYM){ + x=encbranch(ind,ind+vtop->c.i,0); + if(x) { + /* relocation case */ + greloc(cur_text_section, vtop->sym, ind, R_ARM_PC24); + o(x|(is_jmp?0xE0000000:0xE1000000)); + } else { + r = TREG_LR; + load_value(vtop, r); + if(is_jmp) + o(0xE1A0F000 | intr(r)); // mov pc, r + else + o(0xe12fff30 | intr(r)); // blx r + } + }else{ + if(!is_jmp) + o(0xE28FE004); // add lr,pc,#4 + o(0xE51FF004); // ldr pc,[pc,#-4] + o(vtop->c.i); + } + } else { + /* otherwise, indirect call */ +#ifdef CONFIG_TCC_BCHECK + vtop->r &= ~VT_MUSTBOUND; +#endif + r = gv(RC_INT); + if(!is_jmp) + o(0xE1A0E00F); // mov lr,pc + o(0xE1A0F000|intr(r)); // mov pc,r + } +} + +#if defined(CONFIG_TCC_BCHECK) + +static void gen_bounds_call(int v) +{ + Sym *sym = external_helper_sym(v); + + greloc(cur_text_section, sym, ind, R_ARM_PC24); + o(0xebfffffe); +} + +static void gen_bounds_prolog(void) +{ + /* leave some room for bound checking code */ + func_bound_offset = lbounds_section->data_offset; + func_bound_ind = ind; + func_bound_add_epilog = 0; + o(0xe1a00000); /* ld r0,lbounds_section->data_offset */ + o(0xe1a00000); + o(0xe1a00000); + o(0xe1a00000); + o(0xe1a00000); /* call __bound_local_new */ +} + +static void gen_bounds_epilog(void) +{ + addr_t saved_ind; + addr_t *bounds_ptr; + Sym *sym_data; + int offset_modified = func_bound_offset != lbounds_section->data_offset; + + if (!offset_modified && !func_bound_add_epilog) + return; + + /* add end of table info */ + bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t)); + *bounds_ptr = 0; + + sym_data = get_sym_ref(&char_pointer_type, lbounds_section, + func_bound_offset, PTR_SIZE); + + /* generate bound local allocation */ + if (offset_modified) { + saved_ind = ind; + ind = func_bound_ind; + o(0xe59f0000); /* ldr r0, [pc] */ + o(0xea000000); /* b $+4 */ + greloc(cur_text_section, sym_data, ind, R_ARM_REL32); + o(-12); /* lbounds_section->data_offset */ + o(0xe080000f); /* add r0,r0,pc */ + gen_bounds_call(TOK___bound_local_new); + ind = saved_ind; + } + + /* generate bound check local freeing */ + o(0xe92d0003); /* push {r0,r1} */ + o(0xed2d0b04); /* vpush {d0,d1} */ + o(0xe59f0000); /* ldr r0, [pc] */ + o(0xea000000); /* b $+4 */ + greloc(cur_text_section, sym_data, ind, R_ARM_REL32); + o(-12); /* lbounds_section->data_offset */ + o(0xe080000f); /* add r0,r0,pc */ + gen_bounds_call(TOK___bound_local_delete); + o(0xecbd0b04); /* vpop {d0,d1} */ + o(0xe8bd0003); /* pop {r0,r1} */ +} +#endif + +static int unalias_ldbl(int btype) +{ +#if LDOUBLE_SIZE == 8 + if (btype == VT_LDOUBLE) + btype = VT_DOUBLE; +#endif + return btype; +} + +/* Return whether a structure is an homogeneous float aggregate or not. + The answer is true if all the elements of the structure are of the same + primitive float type and there is less than 4 elements. + + type: the type corresponding to the structure to be tested */ +static int is_hgen_float_aggr(CType *type) +{ + if ((type->t & VT_BTYPE) == VT_STRUCT) { + struct Sym *ref; + int btype, nb_fields = 0; + + ref = type->ref->next; + if (ref) { + btype = unalias_ldbl(ref->type.t & VT_BTYPE); + if (btype == VT_FLOAT || btype == VT_DOUBLE) { + for(; ref && btype == unalias_ldbl(ref->type.t & VT_BTYPE); ref = ref->next, nb_fields++); + return !ref && nb_fields <= 4; + } + } + } + return 0; +} + +struct avail_regs { + signed char avail[3]; /* 3 holes max with only float and double alignments */ + int first_hole; /* first available hole */ + int last_hole; /* last available hole (none if equal to first_hole) */ + int first_free_reg; /* next free register in the sequence, hole excluded */ +}; + +/* Find suitable registers for a VFP Co-Processor Register Candidate (VFP CPRC + param) according to the rules described in the procedure call standard for + the ARM architecture (AAPCS). If found, the registers are assigned to this + VFP CPRC parameter. Registers are allocated in sequence unless a hole exists + and the parameter is a single float. + + avregs: opaque structure to keep track of available VFP co-processor regs + align: alignment constraints for the param, as returned by type_size() + size: size of the parameter, as returned by type_size() */ +int assign_vfpreg(struct avail_regs *avregs, int align, int size) +{ + int first_reg = 0; + + if (avregs->first_free_reg == -1) + return -1; + if (align >> 3) { /* double alignment */ + first_reg = avregs->first_free_reg; + /* alignment constraint not respected so use next reg and record hole */ + if (first_reg & 1) + avregs->avail[avregs->last_hole++] = first_reg++; + } else { /* no special alignment (float or array of float) */ + /* if single float and a hole is available, assign the param to it */ + if (size == 4 && avregs->first_hole != avregs->last_hole) + return avregs->avail[avregs->first_hole++]; + else + first_reg = avregs->first_free_reg; + } + if (first_reg + size / 4 <= 16) { + avregs->first_free_reg = first_reg + size / 4; + return first_reg; + } + avregs->first_free_reg = -1; + return -1; +} + +/* Returns whether all params need to be passed in core registers or not. + This is the case for function part of the runtime ABI. */ +int floats_in_core_regs(SValue *sval) +{ + if (!sval->sym) + return 0; + + switch (sval->sym->v) { + case TOK___floatundisf: + case TOK___floatundidf: + case TOK___fixunssfdi: + case TOK___fixunsdfdi: +#ifndef TCC_ARM_VFP + case TOK___fixunsxfdi: +#endif + case TOK___floatdisf: + case TOK___floatdidf: + case TOK___fixsfdi: + case TOK___fixdfdi: + return 1; + + default: + return 0; + } +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) { +#ifdef TCC_ARM_EABI + int size, align; + size = type_size(vt, &align); + if (float_abi == ARM_HARD_FLOAT && !variadic && + (is_float(vt->t) || is_hgen_float_aggr(vt))) { + *ret_align = 8; + *regsize = 8; + ret->ref = NULL; + ret->t = VT_DOUBLE; + return (size + 7) >> 3; + } else if (size > 0 && size <= 4) { + *ret_align = 4; + *regsize = 4; + ret->ref = NULL; + ret->t = VT_INT; + return 1; + } else + return 0; +#else + return 0; +#endif +} + +/* Parameters are classified according to how they are copied to their final + destination for the function call. Because the copying is performed class + after class according to the order in the union below, it is important that + some constraints about the order of the members of this union are respected: + - CORE_STRUCT_CLASS must come after STACK_CLASS; + - CORE_CLASS must come after STACK_CLASS, CORE_STRUCT_CLASS and + VFP_STRUCT_CLASS; + - VFP_STRUCT_CLASS must come after VFP_CLASS. + See the comment for the main loop in copy_params() for the reason. */ +enum reg_class { + STACK_CLASS = 0, + CORE_STRUCT_CLASS, + VFP_CLASS, + VFP_STRUCT_CLASS, + CORE_CLASS, + NB_CLASSES +}; + +struct param_plan { + int start; /* first reg or addr used depending on the class */ + int end; /* last reg used or next free addr depending on the class */ + SValue *sval; /* pointer to SValue on the value stack */ + struct param_plan *prev; /* previous element in this class */ +}; + +struct plan { + struct param_plan *pplans; /* array of all the param plans */ + struct param_plan *clsplans[NB_CLASSES]; /* per class lists of param plans */ + int nb_plans; +}; + +static void add_param_plan(struct plan* plan, int cls, int start, int end, SValue *v) +{ + struct param_plan *p = &plan->pplans[plan->nb_plans++]; + p->prev = plan->clsplans[cls]; + plan->clsplans[cls] = p; + p->start = start, p->end = end, p->sval = v; +} + +/* Assign parameters to registers and stack with alignment according to the + rules in the procedure call standard for the ARM architecture (AAPCS). + The overall assignment is recorded in an array of per parameter structures + called parameter plans. The parameter plans are also further organized in a + number of linked lists, one per class of parameter (see the comment for the + definition of union reg_class). + + nb_args: number of parameters of the function for which a call is generated + float_abi: float ABI in use for this function call + plan: the structure where the overall assignment is recorded + todo: a bitmap that record which core registers hold a parameter + + Returns the amount of stack space needed for parameter passing + + Note: this function allocated an array in plan->pplans with tcc_malloc. It + is the responsibility of the caller to free this array once used (ie not + before copy_params). */ +static int assign_regs(int nb_args, int float_abi, struct plan *plan, int *todo) +{ + int i, size, align; + int ncrn /* next core register number */, nsaa /* next stacked argument address*/; + struct avail_regs avregs = {{0}}; + + ncrn = nsaa = 0; + *todo = 0; + + for(i = nb_args; i-- ;) { + int j, start_vfpreg = 0; + CType type = vtop[-i].type; + type.t &= ~VT_ARRAY; + size = type_size(&type, &align); + size = (size + 3) & ~3; + align = (align + 3) & ~3; + switch(vtop[-i].type.t & VT_BTYPE) { + case VT_STRUCT: + case VT_FLOAT: + case VT_DOUBLE: + case VT_LDOUBLE: + if (float_abi == ARM_HARD_FLOAT) { + int is_hfa = 0; /* Homogeneous float aggregate */ + + if (is_float(vtop[-i].type.t) + || (is_hfa = is_hgen_float_aggr(&vtop[-i].type))) { + int end_vfpreg; + + start_vfpreg = assign_vfpreg(&avregs, align, size); + end_vfpreg = start_vfpreg + ((size - 1) >> 2); + if (start_vfpreg >= 0) { + add_param_plan(plan, is_hfa ? VFP_STRUCT_CLASS : VFP_CLASS, + start_vfpreg, end_vfpreg, &vtop[-i]); + continue; + } else + break; + } + } + ncrn = (ncrn + (align-1)/4) & ~((align/4) - 1); + if (ncrn + size/4 <= 4 || (ncrn < 4 && start_vfpreg != -1)) { + /* The parameter is allocated both in core register and on stack. As + * such, it can be of either class: it would either be the last of + * CORE_STRUCT_CLASS or the first of STACK_CLASS. */ + for (j = ncrn; j < 4 && j < ncrn + size / 4; j++) + *todo|=(1< 4) + nsaa = (ncrn - 4) * 4; + } else { + ncrn = 4; + break; + } + continue; + default: + if (ncrn < 4) { + int is_long = (vtop[-i].type.t & VT_BTYPE) == VT_LLONG; + + if (is_long) { + ncrn = (ncrn + 1) & -2; + if (ncrn == 4) + break; + } + add_param_plan(plan, CORE_CLASS, ncrn, ncrn + is_long, &vtop[-i]); + ncrn += 1 + is_long; + continue; + } + } + nsaa = (nsaa + (align - 1)) & ~(align - 1); + add_param_plan(plan, STACK_CLASS, nsaa, nsaa + size, &vtop[-i]); + nsaa += size; /* size already rounded up before */ + } + return nsaa; +} + +/* Copy parameters to their final destination (core reg, VFP reg or stack) for + function call. + + nb_args: number of parameters the function take + plan: the overall assignment plan for parameters + todo: a bitmap indicating what core reg will hold a parameter + + Returns the number of SValue added by this function on the value stack */ +static int copy_params(int nb_args, struct plan *plan, int todo) +{ + int size, align, r, i, nb_extra_sval = 0; + struct param_plan *pplan; + int pass = 0; + + /* Several constraints require parameters to be copied in a specific order: + - structures are copied to the stack before being loaded in a reg; + - floats loaded to an odd numbered VFP reg are first copied to the + preceding even numbered VFP reg and then moved to the next VFP reg. + + It is thus important that: + - structures assigned to core regs must be copied after parameters + assigned to the stack but before structures assigned to VFP regs because + a structure can lie partly in core registers and partly on the stack; + - parameters assigned to the stack and all structures be copied before + parameters assigned to a core reg since copying a parameter to the stack + require using a core reg; + - parameters assigned to VFP regs be copied before structures assigned to + VFP regs as the copy might use an even numbered VFP reg that already + holds part of a structure. */ +again: + for(i = 0; i < NB_CLASSES; i++) { + for(pplan = plan->clsplans[i]; pplan; pplan = pplan->prev) { + + if (pass + && (i != CORE_CLASS || pplan->sval->r < VT_CONST)) + continue; + + vpushv(pplan->sval); + pplan->sval->r = pplan->sval->r2 = VT_CONST; /* disable entry */ + switch(i) { + case STACK_CLASS: + case CORE_STRUCT_CLASS: + case VFP_STRUCT_CLASS: + if ((pplan->sval->type.t & VT_BTYPE) == VT_STRUCT) { + int padding = 0; + size = type_size(&pplan->sval->type, &align); + /* align to stack align size */ + size = (size + 3) & ~3; + if (i == STACK_CLASS && pplan->prev) + padding = pplan->start - pplan->prev->end; + size += padding; /* Add padding if any */ + /* allocate the necessary size on stack */ + gadd_sp(-size); + /* generate structure store */ + r = get_reg(RC_INT); + o(0xE28D0000|(intr(r)<<12)|padding); /* add r, sp, padding */ + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + /* XXX: optimize. Save all register because memcpy can use them */ + o(0xED2D0A00|(0&1)<<22|(0>>1)<<12|16); /* vpush {s0-s15} */ + vstore(); /* memcpy to current sp + potential padding */ + o(0xECBD0A00|(0&1)<<22|(0>>1)<<12|16); /* vpop {s0-s15} */ + + /* Homogeneous float aggregate are loaded to VFP registers + immediately since there is no way of loading data in multiple + non consecutive VFP registers as what is done for other + structures (see the use of todo). */ + if (i == VFP_STRUCT_CLASS) { + int first = pplan->start, nb = pplan->end - first + 1; + /* vpop.32 {pplan->start, ..., pplan->end} */ + o(0xECBD0A00|(first&1)<<22|(first>>1)<<12|nb); + /* No need to write the register used to a SValue since VFP regs + cannot be used for gcall_or_jmp */ + } + } else { + if (is_float(pplan->sval->type.t)) { +#ifdef TCC_ARM_VFP + r = vfpr(gv(RC_FLOAT)) << 12; + if ((pplan->sval->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else { + size = 8; + r |= 0x101; /* vpush.32 -> vpush.64 */ + } + o(0xED2D0A01 + r); /* vpush */ +#else + r = fpr(gv(RC_FLOAT)) << 12; + if ((pplan->sval->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else if ((pplan->sval->type.t & VT_BTYPE) == VT_DOUBLE) + size = 8; + else + size = LDOUBLE_SIZE; + + if (size == 12) + r |= 0x400000; + else if(size == 8) + r|=0x8000; + + o(0xED2D0100|r|(size>>2)); /* some kind of vpush for FPA */ +#endif + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + size=4; + if ((pplan->sval->type.t & VT_BTYPE) == VT_LLONG) { + lexpand(); + size = 8; + r = gv(RC_INT); + o(0xE52D0004|(intr(r)<<12)); /* push r */ + vtop--; + } + r = gv(RC_INT); + o(0xE52D0004|(intr(r)<<12)); /* push r */ + } + if (i == STACK_CLASS && pplan->prev) + gadd_sp(pplan->prev->end - pplan->start); /* Add padding if any */ + } + break; + + case VFP_CLASS: + gv(regmask(TREG_F0 + (pplan->start >> 1))); + if (pplan->start & 1) { /* Must be in upper part of double register */ + o(0xEEF00A40|((pplan->start>>1)<<12)|(pplan->start>>1)); /* vmov.f32 s(n+1), sn */ + vtop->r = VT_CONST; /* avoid being saved on stack by gv for next float */ + } + break; + + case CORE_CLASS: + if ((pplan->sval->type.t & VT_BTYPE) == VT_LLONG) { + lexpand(); + gv(regmask(pplan->end)); + pplan->sval->r2 = vtop->r; + vtop--; + } + gv(regmask(pplan->start)); + /* Mark register as used so that gcall_or_jmp use another one + (regs >=4 are free as never used to pass parameters) */ + pplan->sval->r = vtop->r; + break; + } + vtop--; + } + } + + /* second pass to restore registers that were saved on stack by accident. + Maybe redundant after the "lvalue_save" patch in tccgen.c:gv() */ + if (++pass < 2) + goto again; + + /* Manually free remaining registers since next parameters are loaded + * manually, without the help of gv(int). */ + save_regs(nb_args); + + if(todo) { + o(0xE8BD0000|todo); /* pop {todo} */ + for(pplan = plan->clsplans[CORE_STRUCT_CLASS]; pplan; pplan = pplan->prev) { + int r; + pplan->sval->r = pplan->start; + /* An SValue can only pin 2 registers at best (r and r2) but a structure + can occupy more than 2 registers. Thus, we need to push on the value + stack some fake parameter to have on SValue for each registers used + by a structure (r2 is not used). */ + for (r = pplan->start + 1; r <= pplan->end; r++) { + if (todo & (1 << r)) { + nb_extra_sval++; + vpushi(0); + vtop->r = r; + } + } + } + } + return nb_extra_sval; +} + +/* Generate function call. The function address is pushed first, then + all the parameters in call order. This functions pops all the + parameters and the function address. */ +void gfunc_call(int nb_args) +{ + int r, args_size; + int def_float_abi = float_abi; + int todo; + struct plan plan; +#ifdef TCC_ARM_EABI + int variadic; +#endif + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gbound_args(nb_args); +#endif + +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT) { + variadic = (vtop[-nb_args].type.ref->f.func_type == FUNC_ELLIPSIS); + if (variadic || floats_in_core_regs(&vtop[-nb_args])) + float_abi = ARM_SOFTFP_FLOAT; + } +#endif + /* cannot let cpu flags if other instruction are generated. Also avoid leaving + VT_JMP anywhere except on the top of the stack because it would complicate + the code generator. */ + r = vtop->r & VT_VALMASK; + if (r == VT_CMP || (r & ~1) == VT_JMP) + gv(RC_INT); + + memset(&plan, 0, sizeof plan); + if (nb_args) + plan.pplans = tcc_malloc(nb_args * sizeof(*plan.pplans)); + + args_size = assign_regs(nb_args, float_abi, &plan, &todo); + +#ifdef TCC_ARM_EABI + if (args_size & 7) { /* Stack must be 8 byte aligned at fct call for EABI */ + args_size = (args_size + 7) & ~7; + o(0xE24DD004); /* sub sp, sp, #4 */ + } +#endif + + nb_args += copy_params(nb_args, &plan, todo); + tcc_free(plan.pplans); + + /* Move fct SValue on top as required by gcall_or_jmp */ + vrotb(nb_args + 1); + gcall_or_jmp(0); + if (args_size) + gadd_sp(args_size); /* pop all parameters passed on the stack */ +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) + if(float_abi == ARM_SOFTFP_FLOAT && is_float(vtop->type.ref->type.t)) { + if((vtop->type.ref->type.t & VT_BTYPE) == VT_FLOAT) { + o(0xEE000A10); /*vmov s0, r0 */ + } else { + o(0xEE000B10); /* vmov.32 d0[0], r0 */ + o(0xEE201B10); /* vmov.32 d0[1], r1 */ + } + } +#endif + vtop -= nb_args + 1; /* Pop all params and fct address from value stack */ + leaffunc = 0; /* we are calling a function, so we aren't in a leaf function */ + float_abi = def_float_abi; +} + +/* generate function prolog of type 't' */ +void gfunc_prolog(Sym *func_sym) +{ + CType *func_type = &func_sym->type; + Sym *sym,*sym2; + int n, nf, size, align, rs, struct_ret = 0; + int addr, pn, sn; /* pn=core, sn=stack */ + CType ret_type; + +#ifdef TCC_ARM_EABI + struct avail_regs avregs = {{0}}; +#endif + + sym = func_type->ref; + + n = nf = 0; + if ((func_vt.t & VT_BTYPE) == VT_STRUCT && + !gfunc_sret(&func_vt, func_var, &ret_type, &align, &rs)) + { + n++; + struct_ret = 1; + func_vc = 12; /* Offset from fp of the place to store the result */ + } + for(sym2 = sym->next; sym2 && (n < 4 || nf < 16); sym2 = sym2->next) { + size = type_size(&sym2->type, &align); +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT && !func_var && + (is_float(sym2->type.t) || is_hgen_float_aggr(&sym2->type))) { + int tmpnf = assign_vfpreg(&avregs, align, size); + tmpnf += (size + 3) / 4; + nf = (tmpnf > nf) ? tmpnf : nf; + } else +#endif + if (n < 4) + n += (size + 3) / 4; + } + o(0xE1A0C00D); /* mov ip,sp */ + if (func_var) + n=4; + if (n) { + if(n>4) + n=4; +#ifdef TCC_ARM_EABI + n=(n+1)&-2; +#endif + o(0xE92D0000|((1<16) + nf=16; + nf=(nf+1)&-2; /* nf => HARDFLOAT => EABI */ + o(0xED2D0A00|nf); /* save s0-s15 on stack if needed */ + } + o(0xE92D5800); /* save fp, ip, lr */ + o(0xE1A0B00D); /* mov fp, sp */ + func_sub_sp_offset = ind; + o(0xE1A00000); /* nop, leave space for stack adjustment in epilog */ + +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT) { + func_vc += nf * 4; + memset(&avregs, 0, sizeof avregs); + } +#endif + pn = struct_ret, sn = 0; + while ((sym = sym->next)) { + CType *type; + type = &sym->type; + size = type_size(type, &align); + size = (size + 3) >> 2; + align = (align + 3) & ~3; +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT && !func_var && (is_float(sym->type.t) + || is_hgen_float_aggr(&sym->type))) { + int fpn = assign_vfpreg(&avregs, align, size << 2); + if (fpn >= 0) + addr = fpn * 4; + else + goto from_stack; + } else +#endif + if (pn < 4) { +#ifdef TCC_ARM_EABI + pn = (pn + (align-1)/4) & -(align/4); +#endif + addr = (nf + pn) * 4; + pn += size; + if (!sn && pn > 4) + sn = (pn - 4); + } else { +#ifdef TCC_ARM_EABI +from_stack: + sn = (sn + (align-1)/4) & -(align/4); +#endif + addr = (n + nf + sn) * 4; + sn += size; + } + sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, + addr + 12); + } + last_itod_magic=0; + leaffunc = 1; + loc = 0; +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gen_bounds_prolog(); +#endif +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + uint32_t x; + int diff; + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gen_bounds_epilog(); +#endif + /* Copy float return value to core register if base standard is used and + float computation is made with VFP */ +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) + if ((float_abi == ARM_SOFTFP_FLOAT || func_var) && is_float(func_vt.t)) { + if((func_vt.t & VT_BTYPE) == VT_FLOAT) + o(0xEE100A10); /* fmrs r0, s0 */ + else { + o(0xEE100B10); /* fmrdl r0, d0 */ + o(0xEE301B10); /* fmrdh r1, d0 */ + } + } +#endif + o(0xE89BA800); /* restore fp, sp, pc */ + diff = (-loc + 3) & -4; +#ifdef TCC_ARM_EABI + if(!leaffunc) + diff = ((diff + 11) & -8) - 4; +#endif + if(diff > 0) { + x=stuff_const(0xE24BD000, diff); /* sub sp,fp,# */ + if(x) + *(uint32_t *)(cur_text_section->data + func_sub_sp_offset) = x; + else { + int addr; + addr=ind; + o(0xE59FC004); /* ldr ip,[pc+4] */ + o(0xE04BD00C); /* sub sp,fp,ip */ + o(0xE1A0F00E); /* mov pc,lr */ + o(diff); + *(uint32_t *)(cur_text_section->data + func_sub_sp_offset) = 0xE1000000|encbranch(func_sub_sp_offset,addr,1); + } + } +} + +ST_FUNC void gen_fill_nops(int bytes) +{ + if ((bytes & 3)) + tcc_error("alignment of code section not multiple of 4"); + while (bytes > 0) { + o(0xE1A00000); + bytes -= 4; + } +} + +/* generate a jump to a label */ +ST_FUNC int gjmp(int t) +{ + int r; + if (nocode_wanted) + return t; + r=ind; + o(0xE0000000|encbranch(r,t,1)); + return r; +} + +/* generate a jump to a fixed address */ +ST_FUNC void gjmp_addr(int a) +{ + gjmp(a); +} + +ST_FUNC int gjmp_cond(int op, int t) +{ + int r; + if (nocode_wanted) + return t; + r=ind; + op=mapcc(op); + op|=encbranch(r,t,1); + o(op); + return r; +} + +ST_FUNC int gjmp_append(int n, int t) +{ + uint32_t *x; + int p,lp; + if(n) { + p = n; + do { + p = decbranch(lp=p); + } while(p); + x = (uint32_t *)(cur_text_section->data + lp); + *x &= 0xff000000; + *x |= encbranch(lp,t,1); + t = n; + } + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + int c, func = 0; + uint32_t opc = 0, r, fr; + unsigned short retreg = REG_IRET; + + c=0; + switch(op) { + case '+': + opc = 0x8; + c=1; + break; + case TOK_ADDC1: /* add with carry generation */ + opc = 0x9; + c=1; + break; + case '-': + opc = 0x4; + c=1; + break; + case TOK_SUBC1: /* sub with carry generation */ + opc = 0x5; + c=1; + break; + case TOK_ADDC2: /* add with carry use */ + opc = 0xA; + c=1; + break; + case TOK_SUBC2: /* sub with carry use */ + opc = 0xC; + c=1; + break; + case '&': + opc = 0x0; + c=1; + break; + case '^': + opc = 0x2; + c=1; + break; + case '|': + opc = 0x18; + c=1; + break; + case '*': + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + o(0xE0000090|(intr(r)<<16)|(intr(r)<<8)|intr(fr)); + return; + case TOK_SHL: + opc = 0; + c=2; + break; + case TOK_SHR: + opc = 1; + c=2; + break; + case TOK_SAR: + opc = 2; + c=2; + break; + case '/': + case TOK_PDIV: + func=TOK___divsi3; + c=3; + break; + case TOK_UDIV: + func=TOK___udivsi3; + c=3; + break; + case '%': +#ifdef TCC_ARM_EABI + func=TOK___aeabi_idivmod; + retreg=REG_IRE2; +#else + func=TOK___modsi3; +#endif + c=3; + break; + case TOK_UMOD: +#ifdef TCC_ARM_EABI + func=TOK___aeabi_uidivmod; + retreg=REG_IRE2; +#else + func=TOK___umodsi3; +#endif + c=3; + break; + case TOK_UMULL: + gv2(RC_INT, RC_INT); + r=intr(vtop[-1].r2=get_reg(RC_INT)); + c=vtop[-1].r; + vtop[-1].r=get_reg_ex(RC_INT,regmask(c)); + vtop--; + o(0xE0800090|(r<<16)|(intr(vtop->r)<<12)|(intr(c)<<8)|intr(vtop[1].r)); + return; + default: + opc = 0x15; + c=1; + break; + } + switch(c) { + case 1: + if((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + if(opc == 4 || opc == 5 || opc == 0xc) { + vswap(); + opc|=2; // sub -> rsb + } + } + if ((vtop->r & VT_VALMASK) == VT_CMP || + (vtop->r & (VT_VALMASK & ~1)) == VT_JMP) + gv(RC_INT); + vswap(); + c=intr(gv(RC_INT)); + vswap(); + opc=0xE0000000|(opc<<20); + if((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + uint32_t x; + x=stuff_const(opc|0x2000000|(c<<16),vtop->c.i); + if(x) { + if ((x & 0xfff00000) == 0xe3500000) // cmp rx,#c + o(x); + else { + r=intr(vtop[-1].r=get_reg_ex(RC_INT,regmask(vtop[-1].r))); + o(x|(r<<12)); + } + goto done; + } + } + fr=intr(gv(RC_INT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + c=intr(gv(RC_INT)); + vswap(); + } +#endif + if ((opc & 0xfff00000) == 0xe1500000) // cmp rx,ry + o(opc|(c<<16)|fr); + else { + r=intr(vtop[-1].r=get_reg_ex(RC_INT,two2mask(vtop->r,vtop[-1].r))); + o(opc|(c<<16)|(r<<12)|fr); + } +done: + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) + vset_VT_CMP(op); + break; + case 2: + opc=0xE1A00000|(opc<<5); + if ((vtop->r & VT_VALMASK) == VT_CMP || + (vtop->r & (VT_VALMASK & ~1)) == VT_JMP) + gv(RC_INT); + vswap(); + r=intr(gv(RC_INT)); + vswap(); + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + fr=intr(vtop[-1].r=get_reg_ex(RC_INT,regmask(vtop[-1].r))); + c = vtop->c.i & 0x1f; + o(opc|r|(c<<7)|(fr<<12)); + } else { + fr=intr(gv(RC_INT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=intr(gv(RC_INT)); + vswap(); + } +#endif + c=intr(vtop[-1].r=get_reg_ex(RC_INT,two2mask(vtop->r,vtop[-1].r))); + o(opc|r|(c<<12)|(fr<<8)|0x10); + } + vtop--; + break; + case 3: + vpush_helper_func(func); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = retreg; + break; + default: + tcc_error("gen_opi %i unimplemented!",op); + } +} + +#ifdef TCC_ARM_VFP +static int is_zero(int i) +{ + if((vtop[i].r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + if (vtop[i].type.t == VT_FLOAT) + return (vtop[i].c.f == 0.f); + else if (vtop[i].type.t == VT_DOUBLE) + return (vtop[i].c.d == 0.0); + return (vtop[i].c.ld == 0.l); +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + * two operands are guaranteed to have the same floating point type */ +void gen_opf(int op) +{ + uint32_t x; + int fneg=0,r; + x=0xEE000A00|T2CPR(vtop->type.t); + switch(op) { + case '+': + if(is_zero(-1)) + vswap(); + if(is_zero(0)) { + vtop--; + return; + } + x|=0x300000; + break; + case '-': + x|=0x300040; + if(is_zero(0)) { + vtop--; + return; + } + if(is_zero(-1)) { + x|=0x810000; /* fsubX -> fnegX */ + vswap(); + vtop--; + fneg=1; + } + break; + case '*': + x|=0x200000; + break; + case '/': + x|=0x800000; + break; + default: + if(op < TOK_ULT || op > TOK_GT) { + tcc_error("unknown fp op %x!",op); + return; + } + if(is_zero(-1)) { + vswap(); + switch(op) { + case TOK_LT: op=TOK_GT; break; + case TOK_GE: op=TOK_ULE; break; + case TOK_LE: op=TOK_GE; break; + case TOK_GT: op=TOK_ULT; break; + } + } + x|=0xB40040; /* fcmpX */ + if(op!=TOK_EQ && op!=TOK_NE) + x|=0x80; /* fcmpX -> fcmpeX */ + if(is_zero(0)) { + vtop--; + o(x|0x10000|(vfpr(gv(RC_FLOAT))<<12)); /* fcmp(e)X -> fcmp(e)zX */ + } else { + gv2(RC_FLOAT,RC_FLOAT); + x|=vfpr(vtop[0].r); + o(x|(vfpr(vtop[-1].r) << 12)); + vtop--; + } + o(0xEEF1FA10); /* fmstat */ + + switch(op) { + case TOK_LE: op=TOK_ULE; break; + case TOK_LT: op=TOK_ULT; break; + case TOK_UGE: op=TOK_GE; break; + case TOK_UGT: op=TOK_GT; break; + } + vset_VT_CMP(op); + return; + } + r=gv(RC_FLOAT); + x|=vfpr(r); + r=regmask(r); + if(!fneg) { + int r2; + vswap(); + r2=gv(RC_FLOAT); + x|=vfpr(r2)<<16; + r|=regmask(r2); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=gv(RC_FLOAT); + vswap(); + x=(x&~0xf)|vfpr(r); + } +#endif + } + vtop->r=get_reg_ex(RC_FLOAT,r); + if(!fneg) + vtop--; + o(x|(vfpr(vtop->r)<<12)); +} + +#else +static uint32_t is_fconst() +{ + long double f; + uint32_t r; + if((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + if (vtop->type.t == VT_FLOAT) + f = vtop->c.f; + else if (vtop->type.t == VT_DOUBLE) + f = vtop->c.d; + else + f = vtop->c.ld; + if(!ieee_finite(f)) + return 0; + r=0x8; + if(f<0.0) { + r=0x18; + f=-f; + } + if(f==0.0) + return r; + if(f==1.0) + return r|1; + if(f==2.0) + return r|2; + if(f==3.0) + return r|3; + if(f==4.0) + return r|4; + if(f==5.0) + return r|5; + if(f==0.5) + return r|6; + if(f==10.0) + return r|7; + return 0; +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +void gen_opf(int op) +{ + uint32_t x, r, r2, c1, c2; + //fputs("gen_opf\n",stderr); + vswap(); + c1 = is_fconst(); + vswap(); + c2 = is_fconst(); + x=0xEE000100; +#if LDOUBLE_SIZE == 8 + if ((vtop->type.t & VT_BTYPE) != VT_FLOAT) + x|=0x80; +#else + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) + x|=0x80; + else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) + x|=0x80000; +#endif + switch(op) + { + case '+': + if(!c2) { + vswap(); + c2=c1; + } + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + if(c2) { + if(c2>0xf) + x|=0x200000; // suf + r2=c2&0xf; + } else { + r2=fpr(gv(RC_FLOAT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } +#endif + } + break; + case '-': + if(c2) { + if(c2<=0xf) + x|=0x200000; // suf + r2=c2&0xf; + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } else if(c1 && c1<=0xf) { + x|=0x300000; // rsf + r2=c1; + r=fpr(gv(RC_FLOAT)); + vswap(); + } else { + x|=0x200000; // suf + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + r2=fpr(gv(RC_FLOAT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } +#endif + } + break; + case '*': + if(!c2 || c2>0xf) { + vswap(); + c2=c1; + } + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + if(c2 && c2<=0xf) + r2=c2; + else { + r2=fpr(gv(RC_FLOAT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } +#endif + } + x|=0x100000; // muf + break; + case '/': + if(c2 && c2<=0xf) { + x|=0x400000; // dvf + r2=c2; + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } else if(c1 && c1<=0xf) { + x|=0x500000; // rdf + r2=c1; + r=fpr(gv(RC_FLOAT)); + vswap(); + } else { + x|=0x400000; // dvf + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + r2=fpr(gv(RC_FLOAT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } +#endif + } + break; + default: + if(op >= TOK_ULT && op <= TOK_GT) { + x|=0xd0f110; // cmfe +/* bug (intention?) in Linux FPU emulator + doesn't set carry if equal */ + switch(op) { + case TOK_ULT: + case TOK_UGE: + case TOK_ULE: + case TOK_UGT: + tcc_error("unsigned comparison on floats?"); + break; + case TOK_LT: + op=TOK_Nset; + break; + case TOK_LE: + op=TOK_ULE; /* correct in unordered case only if AC bit in FPSR set */ + break; + case TOK_EQ: + case TOK_NE: + x&=~0x400000; // cmfe -> cmf + break; + } + if(c1 && !c2) { + c2=c1; + vswap(); + switch(op) { + case TOK_Nset: + op=TOK_GT; + break; + case TOK_GE: + op=TOK_ULE; + break; + case TOK_ULE: + op=TOK_GE; + break; + case TOK_GT: + op=TOK_Nset; + break; + } + } + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + if(c2) { + if(c2>0xf) + x|=0x200000; + r2=c2&0xf; + } else { + r2=fpr(gv(RC_FLOAT)); +#ifdef CONFIG_TCC_BCHECK + if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) { + vswap(); + r=fpr(gv(RC_FLOAT)); + vswap(); + } +#endif + } + --vtop; + vset_VT_CMP(op); + ++vtop; + } else { + tcc_error("unknown fp op %x!",op); + return; + } + } + if(vtop[-1].r == VT_CMP) + c1=15; + else { + c1=vtop->r; + if(r2&0x8) + c1=vtop[-1].r; + vtop[-1].r=get_reg_ex(RC_FLOAT,two2mask(vtop[-1].r,c1)); + c1=fpr(vtop[-1].r); + } + vtop--; + o(x|(r<<16)|(c1<<12)|r2); +} +#endif + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +ST_FUNC void gen_cvt_itof(int t) +{ + uint32_t r, r2; + int bt; + bt=vtop->type.t & VT_BTYPE; + if(bt == VT_INT || bt == VT_SHORT || bt == VT_BYTE) { +#ifndef TCC_ARM_VFP + uint32_t dsize = 0; +#endif + r=intr(gv(RC_INT)); +#ifdef TCC_ARM_VFP + r2=vfpr(vtop->r=get_reg(RC_FLOAT)); + o(0xEE000A10|(r<<12)|(r2<<16)); /* fmsr */ + r2|=r2<<12; + if(!(vtop->type.t & VT_UNSIGNED)) + r2|=0x80; /* fuitoX -> fsituX */ + o(0xEEB80A40|r2|T2CPR(t)); /* fYitoX*/ +#else + r2=fpr(vtop->r=get_reg(RC_FLOAT)); + if((t & VT_BTYPE) != VT_FLOAT) + dsize=0x80; /* flts -> fltd */ + o(0xEE000110|dsize|(r2<<16)|(r<<12)); /* flts */ + if((vtop->type.t & (VT_UNSIGNED|VT_BTYPE)) == (VT_UNSIGNED|VT_INT)) { + uint32_t off = 0; + o(0xE3500000|(r<<12)); /* cmp */ + r=fpr(get_reg(RC_FLOAT)); + if(last_itod_magic) { + off=ind+8-last_itod_magic; + off/=4; + if(off>255) + off=0; + } + o(0xBD1F0100|(r<<12)|off); /* ldflts */ + if(!off) { + o(0xEA000000); /* b */ + last_itod_magic=ind; + o(0x4F800000); /* 4294967296.0f */ + } + o(0xBE000100|dsize|(r2<<16)|(r2<<12)|r); /* adflt */ + } +#endif + return; + } else if(bt == VT_LLONG) { + int func; + CType *func_type = 0; + if((t & VT_BTYPE) == VT_FLOAT) { + func_type = &func_float_type; + if(vtop->type.t & VT_UNSIGNED) + func=TOK___floatundisf; + else + func=TOK___floatdisf; +#if LDOUBLE_SIZE != 8 + } else if((t & VT_BTYPE) == VT_LDOUBLE) { + func_type = &func_ldouble_type; + if(vtop->type.t & VT_UNSIGNED) + func=TOK___floatundixf; + else + func=TOK___floatdixf; + } else if((t & VT_BTYPE) == VT_DOUBLE) { +#else + } else if((t & VT_BTYPE) == VT_DOUBLE || (t & VT_BTYPE) == VT_LDOUBLE) { +#endif + func_type = &func_double_type; + if(vtop->type.t & VT_UNSIGNED) + func=TOK___floatundidf; + else + func=TOK___floatdidf; + } + if(func_type) { + vpushsym(func_type, external_helper_sym(func)); + vswap(); + gfunc_call(1); + vpushi(0); + vtop->r=TREG_F0; + return; + } + } + tcc_error("unimplemented gen_cvt_itof %x!",vtop->type.t); +} + +/* convert fp to int 't' type */ +void gen_cvt_ftoi(int t) +{ + uint32_t r, r2; + int u, func = 0; + u=t&VT_UNSIGNED; + t&=VT_BTYPE; + r2=vtop->type.t & VT_BTYPE; + if(t==VT_INT) { +#ifdef TCC_ARM_VFP + r=vfpr(gv(RC_FLOAT)); + u=u?0:0x10000; + o(0xEEBC0AC0|(r<<12)|r|T2CPR(r2)|u); /* ftoXizY */ + r2=intr(vtop->r=get_reg(RC_INT)); + o(0xEE100A10|(r<<16)|(r2<<12)); + return; +#else + if(u) { + if(r2 == VT_FLOAT) + func=TOK___fixunssfsi; +#if LDOUBLE_SIZE != 8 + else if(r2 == VT_LDOUBLE) + func=TOK___fixunsxfsi; + else if(r2 == VT_DOUBLE) +#else + else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE) +#endif + func=TOK___fixunsdfsi; + } else { + r=fpr(gv(RC_FLOAT)); + r2=intr(vtop->r=get_reg(RC_INT)); + o(0xEE100170|(r2<<12)|r); + return; + } +#endif + } else if(t == VT_LLONG) { // unsigned handled in gen_cvt_ftoi1 + if(r2 == VT_FLOAT) + func=TOK___fixsfdi; +#if LDOUBLE_SIZE != 8 + else if(r2 == VT_LDOUBLE) + func=TOK___fixxfdi; + else if(r2 == VT_DOUBLE) +#else + else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE) +#endif + func=TOK___fixdfdi; + } + if(func) { + vpush_helper_func(func); + vswap(); + gfunc_call(1); + vpushi(0); + if(t == VT_LLONG) + vtop->r2 = REG_IRE2; + vtop->r = REG_IRET; + return; + } + tcc_error("unimplemented gen_cvt_ftoi!"); +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ +#ifdef TCC_ARM_VFP + if(((vtop->type.t & VT_BTYPE) == VT_FLOAT) != ((t & VT_BTYPE) == VT_FLOAT)) { + uint32_t r = vfpr(gv(RC_FLOAT)); + o(0xEEB70AC0|(r<<12)|r|T2CPR(vtop->type.t)); + } +#else + /* all we have to do on i386 and FPA ARM is to put the float in a register */ + gv(RC_FLOAT); +#endif +} + +/* increment tcov counter */ +ST_FUNC void gen_increment_tcov (SValue *sv) +{ + int r1, r2; + + vpushv(sv); + vtop->r = r1 = get_reg(RC_INT); + r2 = get_reg(RC_INT); + o(0xE59F0000 | (intr(r1)<<12)); // ldr r1,[pc] + o(0xEA000000); // b $+4 + greloc(cur_text_section, sv->sym, ind, R_ARM_REL32); + o(-12); + o(0xe080000f | (intr(r1)<<16) | (intr(r1)<<12)); // add r1,r1,pc + o(0xe5900000 | (intr(r1)<<16) | (intr(r2)<<12)); // ldr r2, [r1] + o(0xe2900001 | (intr(r2)<<16) | (intr(r2)<<12)); // adds r2, r2, #1 + o(0xe5800000 | (intr(r1)<<16) | (intr(r2)<<12)); // str r2, [r1] + o(0xe2800004 | (intr(r1)<<16) | (intr(r1)<<12)); // add r1, r1, #4 + o(0xe5900000 | (intr(r1)<<16) | (intr(r2)<<12)); // ldr r2, [r1] + o(0xe2a00000 | (intr(r2)<<16) | (intr(r2)<<12)); // adc r2, r2, #0 + o(0xe5800000 | (intr(r1)<<16) | (intr(r2)<<12)); // str r2, [r1] + vpop(); +} + +/* computed goto support */ +void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* Save the stack pointer onto the stack and return the location of its address */ +ST_FUNC void gen_vla_sp_save(int addr) { + SValue v; + v.type.t = VT_PTR; + v.r = VT_LOCAL | VT_LVAL; + v.c.i = addr; + store(TREG_SP, &v); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + SValue v; + v.type.t = VT_PTR; + v.r = VT_LOCAL | VT_LVAL; + v.c.i = addr; + load(TREG_SP, &v); +} + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { + int r; +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + vpushv(vtop); +#endif + r = intr(gv(RC_INT)); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + o(0xe2800001 | (r<<16)|(r<<12)); /* add r,r,#1 */ +#endif + o(0xE04D0000|(r<<12)|r); /* sub r, sp, r */ +#ifdef TCC_ARM_EABI + if (align < 8) + align = 8; +#else + if (align < 4) + align = 4; +#endif + if (align & (align - 1)) + tcc_error("alignment is not a power of 2: %i", align); + o(stuff_const(0xE3C0D000|(r<<16), align - 1)); /* bic sp, r, #align-1 */ + vpop(); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) { + vpushi(0); + vtop->r = TREG_R0; + o(0xe1a0000d | (vtop->r << 12)); // mov r0,sp + vswap(); + vpush_helper_func(TOK___bound_new_region); + vrott(3); + gfunc_call(2); + func_bound_add_epilog = 1; + } +#endif +} + +/* end of ARM code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final/arm-link.c b/05/tcc-final/arm-link.c new file mode 100644 index 0000000..18fc428 --- /dev/null +++ b/05/tcc-final/arm-link.c @@ -0,0 +1,444 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_ARM + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_ARM_ABS32 +#define R_DATA_PTR R_ARM_ABS32 +#define R_JMP_SLOT R_ARM_JUMP_SLOT +#define R_GLOB_DAT R_ARM_GLOB_DAT +#define R_COPY R_ARM_COPY +#define R_RELATIVE R_ARM_RELATIVE + +#define R_NUM R_ARM_NUM + +#define ELF_START_ADDR 0x00010000 +#define ELF_PAGE_SIZE 0x10000 + +#define PCRELATIVE_DLLPLT 1 +#define RELOCATE_DLLPLT 1 + +enum float_abi { + ARM_SOFTFP_FLOAT, + ARM_HARD_FLOAT, +}; + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +#ifdef NEED_RELOC_TYPE +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_ARM_MOVT_ABS: + case R_ARM_MOVW_ABS_NC: + case R_ARM_THM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + case R_ARM_ABS32: + case R_ARM_REL32: + case R_ARM_GOTPC: + case R_ARM_GOTOFF: + case R_ARM_GOT32: + case R_ARM_GOT_PREL: + case R_ARM_COPY: + case R_ARM_GLOB_DAT: + case R_ARM_NONE: + case R_ARM_TARGET1: + case R_ARM_MOVT_PREL: + case R_ARM_MOVW_PREL_NC: + return 0; + + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_PLT32: + case R_ARM_THM_PC22: + case R_ARM_THM_JUMP24: + case R_ARM_PREL31: + case R_ARM_V4BX: + case R_ARM_JUMP_SLOT: + return 1; + } + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_ARM_NONE: + case R_ARM_COPY: + case R_ARM_GLOB_DAT: + case R_ARM_JUMP_SLOT: + return NO_GOTPLT_ENTRY; + + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_PLT32: + case R_ARM_THM_PC22: + case R_ARM_THM_JUMP24: + case R_ARM_MOVT_ABS: + case R_ARM_MOVW_ABS_NC: + case R_ARM_THM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + case R_ARM_PREL31: + case R_ARM_ABS32: + case R_ARM_REL32: + case R_ARM_V4BX: + case R_ARM_TARGET1: + case R_ARM_MOVT_PREL: + case R_ARM_MOVW_PREL_NC: + return AUTO_GOTPLT_ENTRY; + + case R_ARM_GOTPC: + case R_ARM_GOTOFF: + return BUILD_GOT_ONLY; + + case R_ARM_GOT32: + case R_ARM_GOT_PREL: + return ALWAYS_GOTPLT_ENTRY; + } + return -1; +} + +#ifdef NEED_BUILD_GOT +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + unsigned plt_offset; + + /* when building a DLL, GOT entry accesses must be done relative to + start of GOT (see x86_64 example above) */ + + /* empty PLT: create PLT0 entry that push address of call site and + jump to ld.so resolution routine (GOT + 8) */ + if (plt->data_offset == 0) { + p = section_ptr_add(plt, 20); + write32le(p, 0xe52de004); /* push {lr} */ + write32le(p+4, 0xe59fe004); /* ldr lr, [pc, #4] */ + write32le(p+8, 0xe08fe00e); /* add lr, pc, lr */ + write32le(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */ + /* p+16 is set in relocate_plt */ + } + plt_offset = plt->data_offset; + + if (attr->plt_thumb_stub) { + p = section_ptr_add(plt, 4); + write32le(p, 0x4778); /* bx pc */ + write32le(p+2, 0x46c0); /* nop */ + } + p = section_ptr_add(plt, 16); + /* save GOT offset for relocate_plt */ + write32le(p + 4, got_offset); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + int x = s1->got->sh_addr - s1->plt->sh_addr - 12; + write32le(s1->plt->data + 16, x - 4); + p += 20; + while (p < p_end) { + unsigned off = x + read32le(p + 4) + (s1->plt->data - p) + 4; + if (read32le(p) == 0x46c04778) /* PLT Thumb stub present */ + p += 4; + write32le(p, 0xe28fc200 | ((off >> 28) & 0xf)); // add ip, pc, #0xN0000000 + write32le(p + 4, 0xe28cc600 | ((off >> 20) & 0xff)); // add ip, pc, #0xNN00000 + write32le(p + 8, 0xe28cca00 | ((off >> 12) & 0xff)); // add ip, ip, #0xNN000 + write32le(p + 12, 0xe5bcf000 | (off & 0xfff)); // ldr pc, [ip, #0xNNN]! + p += 16; + } + } + + if (s1->plt->reloc) { + ElfW_Rel *rel; + p = s1->got->data; + for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) { + write32le(p + rel->r_offset, s1->plt->sh_addr); + } + } +} +#endif +#endif + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + ElfW(Sym) *sym; + int sym_index, esym_index; + + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + + switch(type) { + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_PLT32: + { + int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko; + x = (*(int *) ptr) & 0xffffff; +#ifdef DEBUG_RELOC + printf ("reloc %d: x=0x%x val=0x%x ", type, x, val); +#endif + (*(int *)ptr) &= 0xff000000; + if (x & 0x800000) + x -= 0x1000000; + x <<= 2; + blx_avail = (TCC_CPU_VERSION >= 5); + is_thumb = val & 1; + is_bl = (*(unsigned *) ptr) >> 24 == 0xeb; + is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl)); + x += val - addr; +#ifdef DEBUG_RELOC + printf (" newx=0x%x name=%s\n", x, + (char *) symtab_section->link->data + sym->st_name); +#endif + h = x & 2; + th_ko = (x & 3) && (!blx_avail || !is_call); + if (th_ko || x >= 0x2000000 || x < -0x2000000) + tcc_error("can't relocate value at %x,%d",addr, type); + x >>= 2; + x &= 0xffffff; + /* Only reached if blx is avail and it is a call */ + if (is_thumb) { + x |= h << 24; + (*(int *)ptr) = 0xfa << 24; /* bl -> blx */ + } + (*(int *) ptr) |= x; + } + return; + /* Since these relocations only concern Thumb-2 and blx instruction was + introduced before Thumb-2, we can assume blx is available and not + guard its use */ + case R_ARM_THM_PC22: + case R_ARM_THM_JUMP24: + { + int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11; + int to_thumb, is_call, to_plt, blx_bit = 1 << 12; + Section *plt; + + /* weak reference */ + if (sym->st_shndx == SHN_UNDEF && + ELFW(ST_BIND)(sym->st_info) == STB_WEAK) + return; + + /* Get initial offset */ + hi = (*(uint16_t *)ptr); + lo = (*(uint16_t *)(ptr+2)); + s = (hi >> 10) & 1; + j1 = (lo >> 13) & 1; + j2 = (lo >> 11) & 1; + i1 = (j1 ^ s) ^ 1; + i2 = (j2 ^ s) ^ 1; + imm10 = hi & 0x3ff; + imm11 = lo & 0x7ff; + x = (s << 24) | (i1 << 23) | (i2 << 22) | + (imm10 << 12) | (imm11 << 1); + if (x & 0x01000000) + x -= 0x02000000; + + /* Relocation infos */ + to_thumb = val & 1; + plt = s1->plt; + to_plt = (val >= plt->sh_addr) && + (val < plt->sh_addr + plt->data_offset); + is_call = (type == R_ARM_THM_PC22); + + if (!to_thumb && !to_plt && !is_call) { + int index; + uint8_t *p; + char *name, buf[1024]; + Section *text; + + name = (char *) symtab_section->link->data + sym->st_name; + text = s1->sections[sym->st_shndx]; + /* Modify reloc to target a thumb stub to switch to ARM */ + snprintf(buf, sizeof(buf), "%s_from_thumb", name); + index = put_elf_sym(symtab_section, + text->data_offset + 1, + sym->st_size, sym->st_info, 0, + sym->st_shndx, buf); + to_thumb = 1; + val = text->data_offset + 1; + rel->r_info = ELFW(R_INFO)(index, type); + /* Create a thumb stub function to switch to ARM mode */ + put_elf_reloc(symtab_section, text, + text->data_offset + 4, R_ARM_JUMP24, + sym_index); + p = section_ptr_add(text, 8); + write32le(p, 0x4778); /* bx pc */ + write32le(p+2, 0x46c0); /* nop */ + write32le(p+4, 0xeafffffe); /* b $sym */ + } + + /* Compute final offset */ + x += val - addr; + if (!to_thumb && is_call) { + blx_bit = 0; /* bl -> blx */ + x = (x + 3) & -4; /* Compute offset from aligned PC */ + } + + /* Check that relocation is possible + * offset must not be out of range + * if target is to be entered in arm mode: + - bit 1 must not set + - instruction must be a call (bl) or a jump to PLT */ + if (!to_thumb || x >= 0x1000000 || x < -0x1000000) + if (to_thumb || (val & 2) || (!is_call && !to_plt)) + tcc_error("can't relocate value at %x,%d",addr, type); + + /* Compute and store final offset */ + s = (x >> 24) & 1; + i1 = (x >> 23) & 1; + i2 = (x >> 22) & 1; + j1 = s ^ (i1 ^ 1); + j2 = s ^ (i2 ^ 1); + imm10 = (x >> 12) & 0x3ff; + imm11 = (x >> 1) & 0x7ff; + (*(uint16_t *)ptr) = (uint16_t) ((hi & 0xf800) | + (s << 10) | imm10); + (*(uint16_t *)(ptr+2)) = (uint16_t) ((lo & 0xc000) | + (j1 << 13) | blx_bit | (j2 << 11) | + imm11); + } + return; + case R_ARM_MOVT_ABS: + case R_ARM_MOVW_ABS_NC: + { + int x, imm4, imm12; + if (type == R_ARM_MOVT_ABS) + val >>= 16; + imm12 = val & 0xfff; + imm4 = (val >> 12) & 0xf; + x = (imm4 << 16) | imm12; + if (type == R_ARM_THM_MOVT_ABS) + *(int *)ptr |= x; + else + *(int *)ptr += x; + } + return; + case R_ARM_MOVT_PREL: + case R_ARM_MOVW_PREL_NC: + { + int insn = *(int *)ptr; + int addend = ((insn >> 4) & 0xf000) | (insn & 0xfff); + + addend = (addend ^ 0x8000) - 0x8000; + val += addend - addr; + if (type == R_ARM_MOVT_PREL) + val >>= 16; + *(int *)ptr = (insn & 0xfff0f000) | + ((val & 0xf000) << 4) | (val & 0xfff); + } + return; + case R_ARM_THM_MOVT_ABS: + case R_ARM_THM_MOVW_ABS_NC: + { + int x, i, imm4, imm3, imm8; + if (type == R_ARM_THM_MOVT_ABS) + val >>= 16; + imm8 = val & 0xff; + imm3 = (val >> 8) & 0x7; + i = (val >> 11) & 1; + imm4 = (val >> 12) & 0xf; + x = (imm3 << 28) | (imm8 << 16) | (i << 10) | imm4; + if (type == R_ARM_THM_MOVT_ABS) + *(int *)ptr |= x; + else + *(int *)ptr += x; + } + return; + case R_ARM_PREL31: + { + int x; + x = (*(int *)ptr) & 0x7fffffff; + (*(int *)ptr) &= 0x80000000; + x = (x * 2) / 2; + x += val - addr; + if((x^(x>>1))&0x40000000) + tcc_error("can't relocate value at %x,%d",addr, type); + (*(int *)ptr) |= x & 0x7fffffff; + } + return; + case R_ARM_ABS32: + case R_ARM_TARGET1: + if (s1->output_type == TCC_OUTPUT_DLL) { + esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index; + qrel->r_offset = rel->r_offset; + if (esym_index) { + qrel->r_info = ELFW(R_INFO)(esym_index, R_ARM_ABS32); + qrel++; + return; + } else { + qrel->r_info = ELFW(R_INFO)(0, R_ARM_RELATIVE); + qrel++; + } + } + *(int *)ptr += val; + return; + case R_ARM_REL32: + *(int *)ptr += val - addr; + return; + case R_ARM_GOTPC: + *(int *)ptr += s1->got->sh_addr - addr; + return; + case R_ARM_GOTOFF: + *(int *)ptr += val - s1->got->sh_addr; + return; + case R_ARM_GOT32: + /* we load the got offset */ + *(int *)ptr += get_sym_attr(s1, sym_index, 0)->got_offset; + return; + case R_ARM_GOT_PREL: + /* we load the pc relative got offset */ + *(int *)ptr += s1->got->sh_addr + + get_sym_attr(s1, sym_index, 0)->got_offset - + addr; + return; + case R_ARM_COPY: + return; + case R_ARM_V4BX: + /* trade Thumb support for ARMv4 support */ + if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10) + *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */ + return; + case R_ARM_GLOB_DAT: + case R_ARM_JUMP_SLOT: + *(addr_t *)ptr = val; + return; + case R_ARM_NONE: + /* Nothing to do. Normally used to indicate a dependency + on a certain symbol (like for exception handling under EABI). */ + return; + case R_ARM_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + return; + default: + fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n", + type, (unsigned)addr, ptr, (unsigned)val); + return; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/arm-tok.h b/05/tcc-final/arm-tok.h new file mode 100644 index 0000000..297b105 --- /dev/null +++ b/05/tcc-final/arm-tok.h @@ -0,0 +1,383 @@ +/* ------------------------------------------------------------------ */ +/* WARNING: relative order of tokens is important. */ + +/* register */ + + DEF_ASM(r0) + DEF_ASM(r1) + DEF_ASM(r2) + DEF_ASM(r3) + DEF_ASM(r4) + DEF_ASM(r5) + DEF_ASM(r6) + DEF_ASM(r7) + DEF_ASM(r8) + DEF_ASM(r9) + DEF_ASM(r10) + DEF_ASM(r11) /* fp */ + DEF_ASM(r12) /* ip[c] */ + DEF_ASM(r13) /* sp */ + DEF_ASM(r14) /* lr */ + DEF_ASM(r15) /* pc */ + +/* register macros */ + + DEF_ASM(fp) /* alias for r11 */ + DEF_ASM(ip) /* alias for r12 */ + DEF_ASM(sp) /* alias for r13 */ + DEF_ASM(lr) /* alias for r14 */ + DEF_ASM(pc) /* alias for r15 */ + + /* coprocessors */ + + DEF_ASM(p0) + DEF_ASM(p1) + DEF_ASM(p2) + DEF_ASM(p3) + DEF_ASM(p4) + DEF_ASM(p5) + DEF_ASM(p6) + DEF_ASM(p7) + DEF_ASM(p8) + DEF_ASM(p9) + DEF_ASM(p10) + DEF_ASM(p11) + DEF_ASM(p12) + DEF_ASM(p13) + DEF_ASM(p14) + DEF_ASM(p15) + + /* coprocessor registers */ + + DEF_ASM(c0) + DEF_ASM(c1) + DEF_ASM(c2) + DEF_ASM(c3) + DEF_ASM(c4) + DEF_ASM(c5) + DEF_ASM(c6) + DEF_ASM(c7) + DEF_ASM(c8) + DEF_ASM(c9) + DEF_ASM(c10) + DEF_ASM(c11) + DEF_ASM(c12) + DEF_ASM(c13) + DEF_ASM(c14) + DEF_ASM(c15) + + /* single-precision VFP registers */ + + DEF_ASM(s0) + DEF_ASM(s1) + DEF_ASM(s2) + DEF_ASM(s3) + DEF_ASM(s4) + DEF_ASM(s5) + DEF_ASM(s6) + DEF_ASM(s7) + DEF_ASM(s8) + DEF_ASM(s9) + DEF_ASM(s10) + DEF_ASM(s11) + DEF_ASM(s12) + DEF_ASM(s13) + DEF_ASM(s14) + DEF_ASM(s15) + DEF_ASM(s16) + DEF_ASM(s17) + DEF_ASM(s18) + DEF_ASM(s19) + DEF_ASM(s20) + DEF_ASM(s21) + DEF_ASM(s22) + DEF_ASM(s23) + DEF_ASM(s24) + DEF_ASM(s25) + DEF_ASM(s26) + DEF_ASM(s27) + DEF_ASM(s28) + DEF_ASM(s29) + DEF_ASM(s30) + DEF_ASM(s31) + + /* double-precision VFP registers */ + + DEF_ASM(d0) + DEF_ASM(d1) + DEF_ASM(d2) + DEF_ASM(d3) + DEF_ASM(d4) + DEF_ASM(d5) + DEF_ASM(d6) + DEF_ASM(d7) + DEF_ASM(d8) + DEF_ASM(d9) + DEF_ASM(d10) + DEF_ASM(d11) + DEF_ASM(d12) + DEF_ASM(d13) + DEF_ASM(d14) + DEF_ASM(d15) + + /* VFP status registers */ + + DEF_ASM(fpsid) + DEF_ASM(fpscr) + DEF_ASM(fpexc) + + /* VFP magical ARM register */ + + DEF_ASM(apsr_nzcv) + + /* data processing directives */ + + DEF_ASM(asl) + + /* instructions that have no condition code */ + + DEF_ASM(cdp2) + DEF_ASM(ldc2) + DEF_ASM(ldc2l) + DEF_ASM(stc2) + DEF_ASM(stc2l) + +#define ARM_INSTRUCTION_GROUP(tok) ((((tok) - TOK_ASM_nopeq) & 0xFFFFFFF0) + TOK_ASM_nopeq) + +/* Note: condition code is 4 bits */ +#define DEF_ASM_CONDED(x) \ + DEF(TOK_ASM_ ## x ## eq, #x "eq") \ + DEF(TOK_ASM_ ## x ## ne, #x "ne") \ + DEF(TOK_ASM_ ## x ## cs, #x "cs") \ + DEF(TOK_ASM_ ## x ## cc, #x "cc") \ + DEF(TOK_ASM_ ## x ## mi, #x "mi") \ + DEF(TOK_ASM_ ## x ## pl, #x "pl") \ + DEF(TOK_ASM_ ## x ## vs, #x "vs") \ + DEF(TOK_ASM_ ## x ## vc, #x "vc") \ + DEF(TOK_ASM_ ## x ## hi, #x "hi") \ + DEF(TOK_ASM_ ## x ## ls, #x "ls") \ + DEF(TOK_ASM_ ## x ## ge, #x "ge") \ + DEF(TOK_ASM_ ## x ## lt, #x "lt") \ + DEF(TOK_ASM_ ## x ## gt, #x "gt") \ + DEF(TOK_ASM_ ## x ## le, #x "le") \ + DEF(TOK_ASM_ ## x, #x) \ + DEF(TOK_ASM_ ## x ## rsvd, #x "rsvd") + +/* Note: condition code is 4 bits */ +#define DEF_ASM_CONDED_WITH_SUFFIX(x, y) \ + DEF(TOK_ASM_ ## x ## eq ## _ ## y, #x "eq." #y) \ + DEF(TOK_ASM_ ## x ## ne ## _ ## y, #x "ne." #y) \ + DEF(TOK_ASM_ ## x ## cs ## _ ## y, #x "cs." #y) \ + DEF(TOK_ASM_ ## x ## cc ## _ ## y, #x "cc." #y) \ + DEF(TOK_ASM_ ## x ## mi ## _ ## y, #x "mi." #y) \ + DEF(TOK_ASM_ ## x ## pl ## _ ## y, #x "pl." #y) \ + DEF(TOK_ASM_ ## x ## vs ## _ ## y, #x "vs." #y) \ + DEF(TOK_ASM_ ## x ## vc ## _ ## y, #x "vc." #y) \ + DEF(TOK_ASM_ ## x ## hi ## _ ## y, #x "hi." #y) \ + DEF(TOK_ASM_ ## x ## ls ## _ ## y, #x "ls." #y) \ + DEF(TOK_ASM_ ## x ## ge ## _ ## y, #x "ge." #y) \ + DEF(TOK_ASM_ ## x ## lt ## _ ## y, #x "lt." #y) \ + DEF(TOK_ASM_ ## x ## gt ## _ ## y, #x "gt." #y) \ + DEF(TOK_ASM_ ## x ## le ## _ ## y, #x "le." #y) \ + DEF(TOK_ASM_ ## x ## _ ## y, #x "." #y) \ + DEF(TOK_ASM_ ## x ## rsvd ## _ ## y, #x "rsvd." #y) + +#define DEF_ASM_CONDED_VFP_F32_F64(x) \ + DEF_ASM_CONDED_WITH_SUFFIX(x, f32) \ + DEF_ASM_CONDED_WITH_SUFFIX(x, f64) + +#define DEF_ASM_CONDED_WITH_TWO_SUFFIXES(x, y, z) \ + DEF(TOK_ASM_ ## x ## eq ## _ ## y ## _ ## z, #x "eq." #y "." #z) \ + DEF(TOK_ASM_ ## x ## ne ## _ ## y ## _ ## z, #x "ne." #y "." #z) \ + DEF(TOK_ASM_ ## x ## cs ## _ ## y ## _ ## z, #x "cs." #y "." #z) \ + DEF(TOK_ASM_ ## x ## cc ## _ ## y ## _ ## z, #x "cc." #y "." #z) \ + DEF(TOK_ASM_ ## x ## mi ## _ ## y ## _ ## z, #x "mi." #y "." #z) \ + DEF(TOK_ASM_ ## x ## pl ## _ ## y ## _ ## z, #x "pl." #y "." #z) \ + DEF(TOK_ASM_ ## x ## vs ## _ ## y ## _ ## z, #x "vs." #y "." #z) \ + DEF(TOK_ASM_ ## x ## vc ## _ ## y ## _ ## z, #x "vc." #y "." #z) \ + DEF(TOK_ASM_ ## x ## hi ## _ ## y ## _ ## z, #x "hi." #y "." #z) \ + DEF(TOK_ASM_ ## x ## ls ## _ ## y ## _ ## z, #x "ls." #y "." #z) \ + DEF(TOK_ASM_ ## x ## ge ## _ ## y ## _ ## z, #x "ge." #y "." #z) \ + DEF(TOK_ASM_ ## x ## lt ## _ ## y ## _ ## z, #x "lt." #y "." #z) \ + DEF(TOK_ASM_ ## x ## gt ## _ ## y ## _ ## z, #x "gt." #y "." #z) \ + DEF(TOK_ASM_ ## x ## le ## _ ## y ## _ ## z, #x "le." #y "." #z) \ + DEF(TOK_ASM_ ## x ## _ ## y ## _ ## z, #x "." #y "." #z) \ + DEF(TOK_ASM_ ## x ## rsvd ## _ ## y ## _ ## z, #x "rsvd." #y "." #z) + +/* Note: add new tokens after nop (MUST always use DEF_ASM_CONDED) */ + + DEF_ASM_CONDED(nop) + DEF_ASM_CONDED(wfe) + DEF_ASM_CONDED(wfi) + DEF_ASM_CONDED(swi) + DEF_ASM_CONDED(svc) + + /* misc */ + DEF_ASM_CONDED(clz) + + /* size conversion */ + + DEF_ASM_CONDED(sxtb) + DEF_ASM_CONDED(sxth) + DEF_ASM_CONDED(uxtb) + DEF_ASM_CONDED(uxth) + DEF_ASM_CONDED(movt) + DEF_ASM_CONDED(movw) + + /* multiplication */ + + DEF_ASM_CONDED(mul) + DEF_ASM_CONDED(muls) + DEF_ASM_CONDED(mla) + DEF_ASM_CONDED(mlas) + DEF_ASM_CONDED(smull) + DEF_ASM_CONDED(smulls) + DEF_ASM_CONDED(umull) + DEF_ASM_CONDED(umulls) + DEF_ASM_CONDED(smlal) + DEF_ASM_CONDED(smlals) + DEF_ASM_CONDED(umlal) + DEF_ASM_CONDED(umlals) + + /* load/store */ + + DEF_ASM_CONDED(ldr) + DEF_ASM_CONDED(ldrb) + DEF_ASM_CONDED(str) + DEF_ASM_CONDED(strb) + DEF_ASM_CONDED(ldrex) + DEF_ASM_CONDED(ldrexb) + DEF_ASM_CONDED(strex) + DEF_ASM_CONDED(strexb) + DEF_ASM_CONDED(ldrh) + DEF_ASM_CONDED(ldrsh) + DEF_ASM_CONDED(ldrsb) + DEF_ASM_CONDED(strh) + + DEF_ASM_CONDED(stmda) + DEF_ASM_CONDED(ldmda) + DEF_ASM_CONDED(stm) + DEF_ASM_CONDED(ldm) + DEF_ASM_CONDED(stmia) + DEF_ASM_CONDED(ldmia) + DEF_ASM_CONDED(stmdb) + DEF_ASM_CONDED(ldmdb) + DEF_ASM_CONDED(stmib) + DEF_ASM_CONDED(ldmib) + + DEF_ASM_CONDED(ldc) + DEF_ASM_CONDED(ldcl) + DEF_ASM_CONDED(stc) + DEF_ASM_CONDED(stcl) + + /* instruction macros */ + + DEF_ASM_CONDED(push) + DEF_ASM_CONDED(pop) + + /* branches */ + + DEF_ASM_CONDED(b) + DEF_ASM_CONDED(bl) + DEF_ASM_CONDED(bx) + DEF_ASM_CONDED(blx) + + /* data processing instructions; order is important */ + + DEF_ASM_CONDED(and) + DEF_ASM_CONDED(ands) + DEF_ASM_CONDED(eor) + DEF_ASM_CONDED(eors) + DEF_ASM_CONDED(sub) + DEF_ASM_CONDED(subs) + DEF_ASM_CONDED(rsb) + DEF_ASM_CONDED(rsbs) + DEF_ASM_CONDED(add) + DEF_ASM_CONDED(adds) + DEF_ASM_CONDED(adc) + DEF_ASM_CONDED(adcs) + DEF_ASM_CONDED(sbc) + DEF_ASM_CONDED(sbcs) + DEF_ASM_CONDED(rsc) + DEF_ASM_CONDED(rscs) + DEF_ASM_CONDED(tst) + DEF_ASM_CONDED(tsts) // necessary here--but not useful to the user + DEF_ASM_CONDED(teq) + DEF_ASM_CONDED(teqs) // necessary here--but not useful to the user + DEF_ASM_CONDED(cmp) + DEF_ASM_CONDED(cmps) // necessary here--but not useful to the user + DEF_ASM_CONDED(cmn) + DEF_ASM_CONDED(cmns) // necessary here--but not useful to the user + DEF_ASM_CONDED(orr) + DEF_ASM_CONDED(orrs) + DEF_ASM_CONDED(mov) + DEF_ASM_CONDED(movs) + DEF_ASM_CONDED(bic) + DEF_ASM_CONDED(bics) + DEF_ASM_CONDED(mvn) + DEF_ASM_CONDED(mvns) + + DEF_ASM_CONDED(lsl) + DEF_ASM_CONDED(lsls) + DEF_ASM_CONDED(lsr) + DEF_ASM_CONDED(lsrs) + DEF_ASM_CONDED(asr) + DEF_ASM_CONDED(asrs) + DEF_ASM_CONDED(ror) + DEF_ASM_CONDED(rors) + DEF_ASM_CONDED(rrx) + DEF_ASM_CONDED(rrxs) + + DEF_ASM_CONDED(cdp) + DEF_ASM_CONDED(mcr) + DEF_ASM_CONDED(mrc) + + // Floating point high-level instructions + + DEF_ASM_CONDED(vldr) + DEF_ASM_CONDED(vstr) + + DEF_ASM_CONDED_VFP_F32_F64(vmla) + DEF_ASM_CONDED_VFP_F32_F64(vmls) + DEF_ASM_CONDED_VFP_F32_F64(vnmls) + DEF_ASM_CONDED_VFP_F32_F64(vnmla) + DEF_ASM_CONDED_VFP_F32_F64(vmul) + DEF_ASM_CONDED_VFP_F32_F64(vnmul) + DEF_ASM_CONDED_VFP_F32_F64(vadd) + DEF_ASM_CONDED_VFP_F32_F64(vsub) + DEF_ASM_CONDED_VFP_F32_F64(vdiv) + DEF_ASM_CONDED_VFP_F32_F64(vneg) + DEF_ASM_CONDED_VFP_F32_F64(vabs) + DEF_ASM_CONDED_VFP_F32_F64(vsqrt) + DEF_ASM_CONDED_VFP_F32_F64(vcmp) + DEF_ASM_CONDED_VFP_F32_F64(vcmpe) + DEF_ASM_CONDED_VFP_F32_F64(vmov) + + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, s32, f64) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, s32, f32) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, u32, f64) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvtr, u32, f32) + + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, s32, f64) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, s32, f32) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, u32, f64) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, u32, f32) + + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f64, s32) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f32, s32) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f64, u32) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f32, u32) + + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f64, f32) + DEF_ASM_CONDED_WITH_TWO_SUFFIXES(vcvt, f32, f64) + + DEF_ASM_CONDED(vpush) + DEF_ASM_CONDED(vpop) + DEF_ASM_CONDED(vldm) + DEF_ASM_CONDED(vldmia) + DEF_ASM_CONDED(vldmdb) + DEF_ASM_CONDED(vstm) + DEF_ASM_CONDED(vstmia) + DEF_ASM_CONDED(vstmdb) + DEF_ASM_CONDED(vmsr) + DEF_ASM_CONDED(vmrs) diff --git a/05/tcc-final/arm64-asm.c b/05/tcc-final/arm64-asm.c new file mode 100644 index 0000000..a97fd64 --- /dev/null +++ b/05/tcc-final/arm64-asm.c @@ -0,0 +1,94 @@ +/*************************************************************/ +/* + * ARM64 dummy assembler for TCC + * + */ + +#ifdef TARGET_DEFS_ONLY + +#define CONFIG_TCC_ASM +#define NB_ASM_REGS 16 + +ST_FUNC void g(int c); +ST_FUNC void gen_le16(int c); +ST_FUNC void gen_le32(int c); + +/*************************************************************/ +#else +/*************************************************************/ +#define USING_GLOBALS +#include "tcc.h" + +static void asm_error(void) +{ + tcc_error("ARM asm not implemented."); +} + +/* XXX: make it faster ? */ +ST_FUNC void g(int c) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 1; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; + ind = ind1; +} + +ST_FUNC void gen_le16 (int i) +{ + g(i); + g(i>>8); +} + +ST_FUNC void gen_le32 (int i) +{ + gen_le16(i); + gen_le16(i>>16); +} + +ST_FUNC void gen_expr32(ExprValue *pe) +{ + gen_le32(pe->v); +} + +ST_FUNC void asm_opcode(TCCState *s1, int opcode) +{ + asm_error(); +} + +ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier) +{ + asm_error(); +} + +/* generate prolog and epilog code for asm statement */ +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg) +{ +} + +ST_FUNC void asm_compute_constraints(ASMOperand *operands, + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg) +{ +} + +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str) +{ + asm_error(); +} + +ST_FUNC int asm_parse_regvar (int t) +{ + asm_error(); + return -1; +} + +/*************************************************************/ +#endif /* ndef TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/arm64-gen.c b/05/tcc-final/arm64-gen.c new file mode 100644 index 0000000..8232f2a --- /dev/null +++ b/05/tcc-final/arm64-gen.c @@ -0,0 +1,2141 @@ +/* + * A64 code generator for TCC + * + * Copyright (c) 2014-2015 Edmund Grimley Evans + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. This file is offered as-is, + * without any warranty. + */ + +#ifdef TARGET_DEFS_ONLY + +// Number of registers available to allocator: +#define NB_REGS 28 // x0-x18, x30, v0-v7 + +#define TREG_R(x) (x) // x = 0..18 +#define TREG_R30 19 +#define TREG_F(x) (x + 20) // x = 0..7 + +// Register classes sorted from more general to more precise: +#define RC_INT (1 << 0) +#define RC_FLOAT (1 << 1) +#define RC_R(x) (1 << (2 + (x))) // x = 0..18 +#define RC_R30 (1 << 21) +#define RC_F(x) (1 << (22 + (x))) // x = 0..7 + +#define RC_IRET (RC_R(0)) // int return register class +#define RC_FRET (RC_F(0)) // float return register class + +#define REG_IRET (TREG_R(0)) // int return register number +#define REG_FRET (TREG_F(0)) // float return register number + +#define PTR_SIZE 8 + +#define LDOUBLE_SIZE 16 +#define LDOUBLE_ALIGN 16 + +#define MAX_ALIGN 16 + +#define CHAR_IS_UNSIGNED + +/* define if return values need to be extended explicitely + at caller side (for interfacing with non-TCC compilers) */ +#define PROMOTE_RET +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#define USING_GLOBALS +#include "tcc.h" +#include + +ST_DATA const char * const target_machine_defs = +#if defined(__APPLE__) + "__aarch64__\0" + "__arm64__\0" +#else + "__aarch64__\0" +#endif + ; + +ST_DATA const int reg_classes[NB_REGS] = { + RC_INT | RC_R(0), + RC_INT | RC_R(1), + RC_INT | RC_R(2), + RC_INT | RC_R(3), + RC_INT | RC_R(4), + RC_INT | RC_R(5), + RC_INT | RC_R(6), + RC_INT | RC_R(7), + RC_INT | RC_R(8), + RC_INT | RC_R(9), + RC_INT | RC_R(10), + RC_INT | RC_R(11), + RC_INT | RC_R(12), + RC_INT | RC_R(13), + RC_INT | RC_R(14), + RC_INT | RC_R(15), + RC_INT | RC_R(16), + RC_INT | RC_R(17), + RC_INT | RC_R(18), + RC_R30, // not in RC_INT as we make special use of x30 + RC_FLOAT | RC_F(0), + RC_FLOAT | RC_F(1), + RC_FLOAT | RC_F(2), + RC_FLOAT | RC_F(3), + RC_FLOAT | RC_F(4), + RC_FLOAT | RC_F(5), + RC_FLOAT | RC_F(6), + RC_FLOAT | RC_F(7) +}; + +#if defined(CONFIG_TCC_BCHECK) +static addr_t func_bound_offset; +static unsigned long func_bound_ind; +ST_DATA int func_bound_add_epilog; +#endif + +#define IS_FREG(x) ((x) >= TREG_F(0)) + +static uint32_t intr(int r) +{ + assert(TREG_R(0) <= r && r <= TREG_R30); + return r < TREG_R30 ? r : 30; +} + +static uint32_t fltr(int r) +{ + assert(TREG_F(0) <= r && r <= TREG_F(7)); + return r - TREG_F(0); +} + +// Add an instruction to text section: +ST_FUNC void o(unsigned int c) +{ + int ind1 = ind + 4; + if (nocode_wanted) + return; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + write32le(cur_text_section->data + ind, c); + ind = ind1; +} + +static int arm64_encode_bimm64(uint64_t x) +{ + int neg = x & 1; + int rep, pos, len; + + if (neg) + x = ~x; + if (!x) + return -1; + + if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1))) + rep = 2, x &= ((uint64_t)1 << 2) - 1; + else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1))) + rep = 4, x &= ((uint64_t)1 << 4) - 1; + else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1))) + rep = 8, x &= ((uint64_t)1 << 8) - 1; + else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1))) + rep = 16, x &= ((uint64_t)1 << 16) - 1; + else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1))) + rep = 32, x &= ((uint64_t)1 << 32) - 1; + else + rep = 64; + + pos = 0; + if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32; + if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16; + if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8; + if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4; + if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2; + if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1; + + len = 0; + if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32; + if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16; + if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8; + if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4; + if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2; + if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1; + + if (x) + return -1; + if (neg) { + pos = (pos + len) & (rep - 1); + len = rep - len; + } + return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) | + ((rep - pos) & (rep - 1)) << 6 | (len - 1)); +} + +static uint32_t arm64_movi(int r, uint64_t x) +{ + uint64_t m = 0xffff; + int e; + if (!(x & ~m)) + return 0x52800000 | r | x << 5; // movz w(r),#(x) + if (!(x & ~(m << 16))) + return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16 + if (!(x & ~(m << 32))) + return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32 + if (!(x & ~(m << 48))) + return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48 + if ((x & ~m) == m << 16) + return (0x12800000 | r | + (~x << 5 & 0x1fffe0)); // movn w(r),#(~x) + if ((x & ~(m << 16)) == m) + return (0x12a00000 | r | + (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16 + if (!~(x | m)) + return (0x92800000 | r | + (~x << 5 & 0x1fffe0)); // movn x(r),#(~x) + if (!~(x | m << 16)) + return (0x92a00000 | r | + (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16 + if (!~(x | m << 32)) + return (0x92c00000 | r | + (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32 + if (!~(x | m << 48)) + return (0x92e00000 | r | + (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32 + if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0) + return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x) + if ((e = arm64_encode_bimm64(x)) >= 0) + return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x) + return 0; +} + +static void arm64_movimm(int r, uint64_t x) +{ + uint32_t i; + if ((i = arm64_movi(r, x))) + o(i); // a single MOV + else { + // MOVZ/MOVN and 1-3 MOVKs + int z = 0, m = 0; + uint32_t mov1 = 0xd2800000; // movz + uint64_t x1 = x; + for (i = 0; i < 64; i += 16) { + z += !(x >> i & 0xffff); + m += !(~x >> i & 0xffff); + } + if (m > z) { + x1 = ~x; + mov1 = 0x92800000; // movn + } + for (i = 0; i < 64; i += 16) + if (x1 >> i & 0xffff) { + o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17); + // movz/movn x(r),#(*),lsl #(i) + break; + } + for (i += 16; i < 64; i += 16) + if (x1 >> i & 0xffff) + o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17); + // movk x(r),#(*),lsl #(i) + } +} + +// Patch all branches in list pointed to by t to branch to a: +ST_FUNC void gsym_addr(int t_, int a_) +{ + uint32_t t = t_; + uint32_t a = a_; + while (t) { + unsigned char *ptr = cur_text_section->data + t; + uint32_t next = read32le(ptr); + if (a - t + 0x8000000 >= 0x10000000) + tcc_error("branch out of range"); + write32le(ptr, (a - t == 4 ? 0xd503201f : // nop + 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b + t = next; + } +} + +static int arm64_type_size(int t) +{ + /* + * case values are in increasing order (from 1 to 11). + * which 'may' help compiler optimizers. See tcc.h + */ + switch (t & VT_BTYPE) { + case VT_BYTE: return 0; + case VT_SHORT: return 1; + case VT_INT: return 2; + case VT_LLONG: return 3; + case VT_PTR: return 3; + case VT_FUNC: return 3; + case VT_STRUCT: return 3; + case VT_FLOAT: return 2; + case VT_DOUBLE: return 3; + case VT_LDOUBLE: return 4; + case VT_BOOL: return 0; + } + assert(0); + return 0; +} + +static void arm64_spoff(int reg, uint64_t off) +{ + uint32_t sub = off >> 63; + if (sub) + off = -off; + if (off < 4096) + o(0x910003e0 | sub << 30 | reg | off << 10); + // (add|sub) x(reg),sp,#(off) + else { + arm64_movimm(30, off); // use x30 for offset + o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30 + } +} + +/* invert 0: return value to use for store/load */ +/* invert 1: return value to use for arm64_sym */ +static uint64_t arm64_check_offset(int invert, int sz_, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz)) || + (off < 256 || -off <= 256)) + return invert ? off : 0ul; + else if ((off & ((uint32_t)0xfff << sz))) + return invert ? off & ((uint32_t)0xfff << sz) + : off & ~((uint32_t)0xfff << sz); + else if (off & 0x1ff) + return invert ? off & 0x1ff : off & ~0x1ff; + else + return invert ? 0ul : off; +} + +static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (sz >= 2) + sg = 0; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x39400000 | dst | bas << 5 | off << (10 - sz) | + (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)] + else if (off < 256 || -off <= 256) + o(0x38400000 | dst | bas << 5 | (off & 511) << 12 | + (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | + (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30] + } +} + +static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x3d400000 | dst | bas << 5 | off << (10 - sz) | + (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)] + else if (off < 256 || -off <= 256) + o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 | + (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 | + sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30] + } +} + +static void arm64_ldrs(int reg_, int size) +{ + uint32_t reg = reg_; + // Use x30 for intermediate value in some cases. + switch (size) { + default: assert(0); break; + case 0: + /* Can happen with zero size structs */ + break; + case 1: + arm64_ldrx(0, 0, reg, reg, 0); + break; + case 2: + arm64_ldrx(0, 1, reg, reg, 0); + break; + case 3: + arm64_ldrx(0, 1, 30, reg, 0); + arm64_ldrx(0, 0, reg, reg, 2); + o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16 + break; + case 4: + arm64_ldrx(0, 2, reg, reg, 0); + break; + case 5: + arm64_ldrx(0, 2, 30, reg, 0); + arm64_ldrx(0, 0, reg, reg, 4); + o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32 + break; + case 6: + arm64_ldrx(0, 2, 30, reg, 0); + arm64_ldrx(0, 1, reg, reg, 4); + o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32 + break; + case 7: + arm64_ldrx(0, 2, 30, reg, 0); + arm64_ldrx(0, 2, reg, reg, 3); + o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8 + o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32 + break; + case 8: + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 9: + arm64_ldrx(0, 0, reg + 1, reg, 8); + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 10: + arm64_ldrx(0, 1, reg + 1, reg, 8); + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 11: + arm64_ldrx(0, 2, reg + 1, reg, 7); + o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 12: + arm64_ldrx(0, 2, reg + 1, reg, 8); + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 13: + arm64_ldrx(0, 3, reg + 1, reg, 5); + o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 14: + arm64_ldrx(0, 3, reg + 1, reg, 6); + o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 15: + arm64_ldrx(0, 3, reg + 1, reg, 7); + o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8 + arm64_ldrx(0, 3, reg, reg, 0); + break; + case 16: + o(0xa9400000 | reg | (reg+1) << 10 | reg << 5); + // ldp x(reg),x(reg+1),[x(reg)] + break; + } +} + +static void arm64_strx(int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30); + // str(*) x(dst),[x(bas],#(off)] + else if (off < 256 || -off <= 256) + o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30); + // stur(*) x(dst),[x(bas],#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30); + // str(*) x(dst),[x(bas),x30] + } +} + +static void arm64_strv(int sz_, int dst, int bas, uint64_t off) +{ + uint32_t sz = sz_; + if (!(off & ~((uint32_t)0xfff << sz))) + o(0x3d000000 | dst | bas << 5 | off << (10 - sz) | + (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)] + else if (off < 256 || -off <= 256) + o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 | + (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)] + else { + arm64_movimm(30, off); // use x30 for offset + o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 | + sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30] + } +} + +static void arm64_sym(int r, Sym *sym, unsigned long addend) +{ + greloca(cur_text_section, sym, ind, R_AARCH64_ADR_GOT_PAGE, 0); + o(0x90000000 | r); // adrp xr, #sym + greloca(cur_text_section, sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0); + o(0xf9400000 | r | (r << 5)); // ld xr,[xr, #sym] + if (addend) { + // add xr, xr, #addend + if (addend & 0xffful) + o(0x91000000 | r | r << 5 | (addend & 0xfff) << 10); + if (addend > 0xffful) { + // add xr, xr, #addend, lsl #12 + if (addend & 0xfff000ul) + o(0x91400000 | r | r << 5 | ((addend >> 12) & 0xfff) << 10); + if (addend > 0xfffffful) { + /* very unlikely */ + int t = r ? 0 : 1; + o(0xf81f0fe0 | t); /* str xt, [sp, #-16]! */ + arm64_movimm(t, addend & ~0xfffffful); // use xt for addent + o(0x91000000 | r | (t << 5)); /* add xr, xt, #0 */ + o(0xf84107e0 | t); /* ldr xt, [sp], #16 */ + } + } + } +} + +static void arm64_load_cmp(int r, SValue *sv); + +ST_FUNC void load(int r, SValue *sv) +{ + int svtt = sv->type.t; + int svr = sv->r & ~VT_BOUNDED; + int svrv = svr & VT_VALMASK; + uint64_t svcul = (uint32_t)sv->c.i; + svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul; + + if (svr == (VT_LOCAL | VT_LVAL)) { + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), 29, svcul); + return; + } + + if (svr == (VT_CONST | VT_LVAL)) { + if (sv->sym) + arm64_sym(30, sv->sym, // use x30 for address + arm64_check_offset(0, arm64_type_size(svtt), sv->c.i)); + else + arm64_movimm (30, sv->c.i); + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), sv->c.i)); + else + arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), sv->c.i)); + return; + } + + if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) { + if ((svtt & VT_BTYPE) != VT_VOID) { + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), intr(svrv), 0); + } + return; + } + + if (svr == (VT_CONST | VT_LVAL | VT_SYM)) { + arm64_sym(30, sv->sym, // use x30 for address + arm64_check_offset(0, arm64_type_size(svtt), svcul)); + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), svcul)); + else + arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), svcul)); + return; + } + + if (svr == (VT_CONST | VT_SYM)) { + arm64_sym(intr(r), sv->sym, svcul); + return; + } + + if (svr == VT_CONST) { + if ((svtt & VT_BTYPE) != VT_VOID) + arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ? + sv->c.i : (uint32_t)svcul); + return; + } + + if (svr < VT_CONST) { + if (IS_FREG(r) && IS_FREG(svr)) + if (svtt == VT_LDOUBLE) + o(0x4ea01c00 | fltr(r) | fltr(svr) << 5); + // mov v(r).16b,v(svr).16b + else + o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr) + else if (!IS_FREG(r) && !IS_FREG(svr)) + o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr) + else + assert(0); + return; + } + + if (svr == VT_LOCAL) { + if (-svcul < 0x1000) + o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#... + else { + arm64_movimm(30, -svcul); // use x30 for offset + o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30 + } + return; + } + + if (svr == VT_JMP || svr == VT_JMPI) { + int t = (svr == VT_JMPI); + arm64_movimm(intr(r), t); + o(0x14000002); // b .+8 + gsym(svcul); + arm64_movimm(intr(r), t ^ 1); + return; + } + + if (svr == (VT_LLOCAL | VT_LVAL)) { + arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset + if (IS_FREG(r)) + arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0); + else + arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt), + intr(r), 30, 0); + return; + } + + if (svr == VT_CMP) { + arm64_load_cmp(r, sv); + return; + } + + printf("load(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul); + assert(0); +} + +ST_FUNC void store(int r, SValue *sv) +{ + int svtt = sv->type.t; + int svr = sv->r & ~VT_BOUNDED; + int svrv = svr & VT_VALMASK; + uint64_t svcul = (uint32_t)sv->c.i; + svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul; + + if (svr == (VT_LOCAL | VT_LVAL)) { + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul); + else + arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul); + return; + } + + if (svr == (VT_CONST | VT_LVAL)) { + if (sv->sym) + arm64_sym(30, sv->sym, // use x30 for address + arm64_check_offset(0, arm64_type_size(svtt), sv->c.i)); + else + arm64_movimm (30, sv->c.i); + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), sv->c.i)); + else + arm64_strx(arm64_type_size(svtt), intr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), sv->c.i)); + return; + } + + if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) { + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0); + else + arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0); + return; + } + + if (svr == (VT_CONST | VT_LVAL | VT_SYM)) { + arm64_sym(30, sv->sym, // use x30 for address + arm64_check_offset(0, arm64_type_size(svtt), svcul)); + if (IS_FREG(r)) + arm64_strv(arm64_type_size(svtt), fltr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), svcul)); + else + arm64_strx(arm64_type_size(svtt), intr(r), 30, + arm64_check_offset(1, arm64_type_size(svtt), svcul)); + return; + } + + printf("store(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul); + assert(0); +} + +static void arm64_gen_bl_or_b(int b) +{ + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) { + greloca(cur_text_section, vtop->sym, ind, + b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0); + o(0x14000000 | (uint32_t)!b << 31); // b/bl . + } + else { +#ifdef CONFIG_TCC_BCHECK + vtop->r &= ~VT_MUSTBOUND; +#endif + o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr + } +} + +#if defined(CONFIG_TCC_BCHECK) + +static void gen_bounds_call(int v) +{ + Sym *sym = external_helper_sym(v); + + greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0); + o(0x94000000); // bl +} + +static void gen_bounds_prolog(void) +{ + /* leave some room for bound checking code */ + func_bound_offset = lbounds_section->data_offset; + func_bound_ind = ind; + func_bound_add_epilog = 0; + o(0xd503201f); /* nop -> mov x0, lbound section pointer */ + o(0xd503201f); + o(0xd503201f); + o(0xd503201f); /* nop -> call __bound_local_new */ +} + +static void gen_bounds_epilog(void) +{ + addr_t saved_ind; + addr_t *bounds_ptr; + Sym *sym_data; + int offset_modified = func_bound_offset != lbounds_section->data_offset; + + if (!offset_modified && !func_bound_add_epilog) + return; + + /* add end of table info */ + bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t)); + *bounds_ptr = 0; + + sym_data = get_sym_ref(&char_pointer_type, lbounds_section, + func_bound_offset, PTR_SIZE); + + /* generate bound local allocation */ + if (offset_modified) { + saved_ind = ind; + ind = func_bound_ind; + greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0); + o(0x90000000 | 0); // adrp x0, #sym_data + greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0); + o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data] + gen_bounds_call(TOK___bound_local_new); + ind = saved_ind; + } + + /* generate bound check local freeing */ + o(0xa9bf07e0); /* stp x0, x1, [sp, #-16]! */ + o(0x3c9f0fe0); /* str q0, [sp, #-16]! */ + greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0); + o(0x90000000 | 0); // adrp x0, #sym_data + greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0); + o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data] + gen_bounds_call(TOK___bound_local_delete); + o(0x3cc107e0); /* ldr q0, [sp], #16 */ + o(0xa8c107e0); /* ldp x0, x1, [sp], #16 */ +} +#endif + +static int arm64_hfa_aux(CType *type, int *fsize, int num) +{ + if (is_float(type->t)) { + int a, n = type_size(type, &a); + if (num >= 4 || (*fsize && *fsize != n)) + return -1; + *fsize = n; + return num + 1; + } + else if ((type->t & VT_BTYPE) == VT_STRUCT) { + int is_struct = 0; // rather than union + Sym *field; + for (field = type->ref->next; field; field = field->next) + if (field->c) { + is_struct = 1; + break; + } + if (is_struct) { + int num0 = num; + for (field = type->ref->next; field; field = field->next) { + if (field->c != (num - num0) * *fsize) + return -1; + num = arm64_hfa_aux(&field->type, fsize, num); + if (num == -1) + return -1; + } + if (type->ref->c != (num - num0) * *fsize) + return -1; + return num; + } + else { // union + int num0 = num; + for (field = type->ref->next; field; field = field->next) { + int num1 = arm64_hfa_aux(&field->type, fsize, num0); + if (num1 == -1) + return -1; + num = num1 < num ? num : num1; + } + if (type->ref->c != (num - num0) * *fsize) + return -1; + return num; + } + } + else if ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR)) { + int num1; + if (!type->ref->c) + return num; + num1 = arm64_hfa_aux(&type->ref->type, fsize, num); + if (num1 == -1 || (num1 != num && type->ref->c > 4)) + return -1; + num1 = num + type->ref->c * (num1 - num); + if (num1 > 4) + return -1; + return num1; + } + return -1; +} + +static int arm64_hfa(CType *type, unsigned *fsize) +{ + if ((type->t & VT_BTYPE) == VT_STRUCT || + ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR))) { + int sz = 0; + int n = arm64_hfa_aux(type, &sz, 0); + if (0 < n && n <= 4) { + if (fsize) + *fsize = sz; + return n; + } + } + return 0; +} + +static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a) +{ + int nx = 0; // next integer register + int nv = 0; // next vector register + unsigned long ns = 32; // next stack offset + int i; + + for (i = 0; i < n; i++) { + int hfa = arm64_hfa(type[i], 0); + int size, align; + + if ((type[i]->t & VT_ARRAY) || + (type[i]->t & VT_BTYPE) == VT_FUNC) + size = align = 8; + else + size = type_size(type[i], &align); + + if (hfa) + // B.2 + ; + else if (size > 16) { + // B.3: replace with pointer + if (nx < 8) + a[i] = nx++ << 1 | 1; + else { + ns = (ns + 7) & ~7; + a[i] = ns | 1; + ns += 8; + } + continue; + } + else if ((type[i]->t & VT_BTYPE) == VT_STRUCT) + // B.4 + size = (size + 7) & ~7; + + // C.1 + if (is_float(type[i]->t) && nv < 8) { + a[i] = 16 + (nv++ << 1); + continue; + } + + // C.2 + if (hfa && nv + hfa <= 8) { + a[i] = 16 + (nv << 1); + nv += hfa; + continue; + } + + // C.3 + if (hfa) { + nv = 8; + size = (size + 7) & ~7; + } + + // C.4 + if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) { + ns = (ns + 7) & ~7; + ns = (ns + align - 1) & -align; + } + + // C.5 + if ((type[i]->t & VT_BTYPE) == VT_FLOAT) + size = 8; + + // C.6 + if (hfa || is_float(type[i]->t)) { + a[i] = ns; + ns += size; + continue; + } + + // C.7 + if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) { + a[i] = nx++ << 1; + continue; + } + + // C.8 + if (align == 16) + nx = (nx + 1) & ~1; + + // C.9 + if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) { + a[i] = nx << 1; + nx += 2; + continue; + } + + // C.10 + if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) { + a[i] = nx << 1; + nx += (size + 7) >> 3; + continue; + } + + // C.11 + nx = 8; + + // C.12 + ns = (ns + 7) & ~7; + ns = (ns + align - 1) & -align; + + // C.13 + if ((type[i]->t & VT_BTYPE) == VT_STRUCT) { + a[i] = ns; + ns += size; + continue; + } + + // C.14 + if (size < 8) + size = 8; + + // C.15 + a[i] = ns; + ns += size; + } + + return ns - 32; +} + +static unsigned long arm64_pcs(int n, CType **type, unsigned long *a) +{ + unsigned long stack; + + // Return type: + if ((type[0]->t & VT_BTYPE) == VT_VOID) + a[0] = -1; + else { + arm64_pcs_aux(1, type, a); + assert(a[0] == 0 || a[0] == 1 || a[0] == 16); + } + + // Argument types: + stack = arm64_pcs_aux(n, type + 1, a + 1); + + if (0) { + int i; + for (i = 0; i <= n; i++) { + if (!i) + printf("arm64_pcs return: "); + else + printf("arm64_pcs arg %d: ", i); + if (a[i] == (unsigned long)-1) + printf("void\n"); + else if (a[i] == 1 && !i) + printf("X8 pointer\n"); + else if (a[i] < 16) + printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : ""); + else if (a[i] < 32) + printf("V%lu\n", a[i] / 2 - 8); + else + printf("stack %lu%s\n", + (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : ""); + } + } + + return stack; +} + +ST_FUNC void gfunc_call(int nb_args) +{ + CType *return_type; + CType **t; + unsigned long *a, *a1; + unsigned long stack; + int i; + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gbound_args(nb_args); +#endif + + return_type = &vtop[-nb_args].type.ref->type; + if ((return_type->t & VT_BTYPE) == VT_STRUCT) + --nb_args; + + t = tcc_malloc((nb_args + 1) * sizeof(*t)); + a = tcc_malloc((nb_args + 1) * sizeof(*a)); + a1 = tcc_malloc((nb_args + 1) * sizeof(*a1)); + + t[0] = return_type; + for (i = 0; i < nb_args; i++) + t[nb_args - i] = &vtop[-i].type; + + stack = arm64_pcs(nb_args, t, a); + + // Allocate space for structs replaced by pointer: + for (i = nb_args; i; i--) + if (a[i] & 1) { + SValue *arg = &vtop[i - nb_args]; + int align, size = type_size(&arg->type, &align); + assert((arg->type.t & VT_BTYPE) == VT_STRUCT); + stack = (stack + align - 1) & -align; + a1[i] = stack; + stack += size; + } + + stack = (stack + 15) >> 4 << 4; + + /* fetch cpu flag before generating any code */ + if ((vtop->r & VT_VALMASK) == VT_CMP) + gv(RC_INT); + + if (stack >= 0x1000000) // 16Mb + tcc_error("stack size too big %lu", stack); + if (stack & 0xfff) + o(0xd10003ff | (stack & 0xfff) << 10); // sub sp,sp,#(n) + if (stack >> 12) + o(0xd14003ff | (stack >> 12) << 10); + + // First pass: set all values on stack + for (i = nb_args; i; i--) { + vpushv(vtop - nb_args + i); + + if (a[i] & 1) { + // struct replaced by pointer + int r = get_reg(RC_INT); + arm64_spoff(intr(r), a1[i]); + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + if (a[i] >= 32) { + // pointer on stack + r = get_reg(RC_INT); + arm64_spoff(intr(r), a1[i]); + arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1); + } + } + else if (a[i] >= 32) { + // value on stack + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + int r = get_reg(RC_INT); + arm64_spoff(intr(r), a[i] - 32); + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + } + else if (is_float(vtop->type.t)) { + gv(RC_FLOAT); + arm64_strv(arm64_type_size(vtop[0].type.t), + fltr(vtop[0].r), 31, a[i] - 32); + } + else { + gv(RC_INT); + arm64_strx(arm64_type_size(vtop[0].type.t), + intr(vtop[0].r), 31, a[i] - 32); + } + } + + --vtop; + } + + // Second pass: assign values to registers + for (i = nb_args; i; i--, vtop--) { + if (a[i] < 16 && !(a[i] & 1)) { + // value in general-purpose registers + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + int align, size = type_size(&vtop->type, &align); + if (size) { + vtop->type.t = VT_PTR; + gaddrof(); + gv(RC_R(a[i] / 2)); + arm64_ldrs(a[i] / 2, size); + } + } + else + gv(RC_R(a[i] / 2)); + } + else if (a[i] < 16) + // struct replaced by pointer in register + arm64_spoff(a[i] / 2, a1[i]); + else if (a[i] < 32) { + // value in floating-point registers + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz); + vtop->type.t = VT_PTR; + gaddrof(); + gv(RC_R30); + for (j = 0; j < n; j++) + o(0x3d4003c0 | + (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 | + (a[i] / 2 - 8 + j) | + j << 10); // ldr ([sdq])(*),[x30,#(j * sz)] + } + else + gv(RC_F(a[i] / 2 - 8)); + } + } + + if ((return_type->t & VT_BTYPE) == VT_STRUCT) { + if (a[0] == 1) { + // indirect return: set x8 and discard the stack value + gv(RC_R(8)); + --vtop; + } + else + // return in registers: keep the address for after the call + vswap(); + } + + save_regs(0); + arm64_gen_bl_or_b(0); + --vtop; + if (stack & 0xfff) + o(0x910003ff | (stack & 0xfff) << 10); // add sp,sp,#(n) + if (stack >> 12) + o(0x914003ff | (stack >> 12) << 10); + + { + int rt = return_type->t; + int bt = rt & VT_BTYPE; + if (bt == VT_STRUCT && !(a[0] & 1)) { + // A struct was returned in registers, so write it out: + gv(RC_R(8)); + --vtop; + if (a[0] == 0) { + int align, size = type_size(return_type, &align); + assert(size <= 16); + if (size > 8) + o(0xa9000500); // stp x0,x1,[x8] + else if (size) + arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0); + + } + else if (a[0] == 16) { + uint32_t j, sz, n = arm64_hfa(return_type, &sz); + for (j = 0; j < n; j++) + o(0x3d000100 | + (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 | + (a[i] / 2 - 8 + j) | + j << 10); // str ([sdq])(*),[x8,#(j * sz)] + } + } + } + + tcc_free(a1); + tcc_free(a); + tcc_free(t); +} + +static unsigned long arm64_func_va_list_stack; +static int arm64_func_va_list_gr_offs; +static int arm64_func_va_list_vr_offs; +static int arm64_func_sub_sp_offset; + +ST_FUNC void gfunc_prolog(Sym *func_sym) +{ + CType *func_type = &func_sym->type; + int n = 0; + int i = 0; + Sym *sym; + CType **t; + unsigned long *a; + int use_x8 = 0; + int last_int = 0; + int last_float = 0; + + func_vc = 144; // offset of where x8 is stored + + for (sym = func_type->ref; sym; sym = sym->next) + ++n; + t = n ? tcc_malloc(n * sizeof(*t)) : NULL; + a = n ? tcc_malloc(n * sizeof(*a)) : NULL; + + for (sym = func_type->ref; sym; sym = sym->next) + t[i++] = &sym->type; + + arm64_func_va_list_stack = arm64_pcs(n - 1, t, a); + + if (func_sym->type.ref->f.func_type == FUNC_ELLIPSIS) { + use_x8 = 1; + last_int = 4; + last_float = 4; + } + if (a && a[0] == 1) + use_x8 = 1; + for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) { + if (a[i] == 1) + use_x8 = 1; + if (a[i] < 16) { + int last, align, size = type_size(&sym->type, &align); + last = a[i] / 4 + 1 + (size - 1) / 8; + last_int = last > last_int ? last : last_int; + } + else if (a[i] < 32) { + int last, hfa = arm64_hfa(&sym->type, 0); + last = a[i] / 4 - 3 + (hfa ? hfa - 1 : 0); + last_float = last > last_float ? last : last_float; + } + } + + last_int = last_int > 4 ? 4 : last_int; + last_float = last_float > 4 ? 4 : last_float; + + o(0xa9b27bfd); // stp x29,x30,[sp,#-224]! + for (i = 0; i < last_float; i++) + // stp q0,q1,[sp,#16], stp q2,q3,[sp,#48] + // stp q4,q5,[sp,#80], stp q6,q7,[sp,#112] + o(0xad0087e0 + i * 0x10000 + (i << 11) + (i << 1)); + if (use_x8) + o(0xa90923e8); // stp x8,x8,[sp,#144] + for (i = 0; i < last_int; i++) + // stp x0,x1,[sp,#160], stp x2,x3,[sp,#176] + // stp x4,x5,[sp,#192], stp x6,x7,[sp,#208] + o(0xa90a07e0 + i * 0x10000 + (i << 11) + (i << 1)); + + arm64_func_va_list_gr_offs = -64; + arm64_func_va_list_vr_offs = -128; + + for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) { + int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 : + a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 : + 224 + ((a[i] - 32) >> 1 << 1)); + sym_push(sym->v & ~SYM_FIELD, &sym->type, + (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL, + off); + + if (a[i] < 16) { + int align, size = type_size(&sym->type, &align); + arm64_func_va_list_gr_offs = (a[i] / 2 - 7 + + (!(a[i] & 1) && size > 8)) * 8; + } + else if (a[i] < 32) { + uint32_t hfa = arm64_hfa(&sym->type, 0); + arm64_func_va_list_vr_offs = (a[i] / 2 - 16 + + (hfa ? hfa : 1)) * 16; + } + + // HFAs of float and double need to be written differently: + if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) { + uint32_t j, sz, k = arm64_hfa(&sym->type, &sz); + if (sz < 16) + for (j = 0; j < k; j++) { + o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 | + ((a[i] - 16) / 2 + j) | (off / sz + j) << 10); + // str ([sdq])(*),[sp,#(j * sz)] + } + } + } + + tcc_free(a); + tcc_free(t); + + o(0x910003fd); // mov x29,sp + arm64_func_sub_sp_offset = ind; + // In gfunc_epilog these will be replaced with code to decrement SP: + o(0xd503201f); // nop + o(0xd503201f); // nop + loc = 0; +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gen_bounds_prolog(); +#endif +} + +ST_FUNC void gen_va_start(void) +{ + int r; + --vtop; // we don't need the "arg" + gaddrof(); + r = intr(gv(RC_INT)); + + if (arm64_func_va_list_stack) { + //xx could use add (immediate) here + arm64_movimm(30, arm64_func_va_list_stack + 224); + o(0x8b1e03be); // add x30,x29,x30 + } + else + o(0x910383be); // add x30,x29,#224 + o(0xf900001e | r << 5); // str x30,[x(r)] + + if (arm64_func_va_list_gr_offs) { + if (arm64_func_va_list_stack) + o(0x910383be); // add x30,x29,#224 + o(0xf900041e | r << 5); // str x30,[x(r),#8] + } + + if (arm64_func_va_list_vr_offs) { + o(0x910243be); // add x30,x29,#144 + o(0xf900081e | r << 5); // str x30,[x(r),#16] + } + + arm64_movimm(30, arm64_func_va_list_gr_offs); + o(0xb900181e | r << 5); // str w30,[x(r),#24] + + arm64_movimm(30, arm64_func_va_list_vr_offs); + o(0xb9001c1e | r << 5); // str w30,[x(r),#28] + + --vtop; +} + +ST_FUNC void gen_va_arg(CType *t) +{ + int align, size = type_size(t, &align); + unsigned fsize, hfa = arm64_hfa(t, &fsize); + uint32_t r0, r1; + + if (is_float(t->t)) { + hfa = 1; + fsize = size; + } + + gaddrof(); + r0 = intr(gv(RC_INT)); + r1 = get_reg(RC_INT); + vtop[0].r = r1 | VT_LVAL; + r1 = intr(r1); + + if (!hfa) { + uint32_t n = size > 16 ? 8 : (size + 7) & -8; + o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs + if (align == 16) { + assert(0); // this path untested but needed for __uint128_t + o(0x11003fde); // add w30,w30,#15 + o(0x121c6fde); // and w30,w30,#-16 + } + o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n) + o(0x540000ad); // b.le .+20 + o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack + o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n) + o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack + o(0x14000004); // b .+16 + o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs + o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top + o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw + if (size > 16) + o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)] + } + else { + uint32_t rsz = hfa << 4; + uint32_t ssz = (size + 7) & -(uint32_t)8; + uint32_t b1, b2; + o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs + o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz) + b1 = ind; o(0x5400000d); // b.le lab1 + o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack + if (fsize == 16) { + o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15 + o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16 + } + o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz) + o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack + b2 = ind; o(0x14000000); // b lab2 + // lab1: + write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3); + o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs + o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top + if (hfa == 1 || fsize == 16) + o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw + else { + // We need to change the layout of this HFA. + // Get some space on the stack using global variable "loc": + loc = (loc - size) & -(uint32_t)align; + o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw + arm64_movimm(r1, loc); + o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1) + o(0x4c402bdc | (uint32_t)fsize << 7 | + (uint32_t)(hfa == 2) << 15 | + (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30] + o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 | + (uint32_t)(hfa != 2) << 13 | + (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)] + } + // lab2: + write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2); + } +} + +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, + int *align, int *regsize) +{ + return 0; +} + +ST_FUNC void gfunc_return(CType *func_type) +{ + CType *t = func_type; + unsigned long a; + + arm64_pcs(0, &t, &a); + switch (a) { + case -1: + break; + case 0: + if ((func_type->t & VT_BTYPE) == VT_STRUCT) { + int align, size = type_size(func_type, &align); + gaddrof(); + gv(RC_R(0)); + arm64_ldrs(0, size); + } + else + gv(RC_IRET); + break; + case 1: { + CType type = *func_type; + mk_pointer(&type); + vset(&type, VT_LOCAL | VT_LVAL, func_vc); + indir(); + vswap(); + vstore(); + break; + } + case 16: + if ((func_type->t & VT_BTYPE) == VT_STRUCT) { + uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz); + gaddrof(); + gv(RC_R(0)); + for (j = 0; j < n; j++) + o(0x3d400000 | + (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 | + j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)] + } + else + gv(RC_FRET); + break; + default: + assert(0); + } + vtop--; +} + +ST_FUNC void gfunc_epilog(void) +{ +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gen_bounds_epilog(); +#endif + + if (loc) { + // Insert instructions to subtract size of stack frame from SP. + unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset; + uint64_t diff = (-loc + 15) & ~15; + if (!(diff >> 24)) { + if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff) + write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10); + if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12 + write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10); + } + else { + // In this case we may subtract more than necessary, + // but always less than 17/16 of what we were aiming for. + int i = 0; + int j = 0; + while (diff >> 20) { + diff = (diff + 0xffff) >> 16; + ++i; + } + while (diff >> 16) { + diff = (diff + 1) >> 1; + ++j; + } + write32le(ptr, 0xd2800010 | diff << 5 | i << 21); + // mov x16,#(diff),lsl #(16 * i) + write32le(ptr + 4, 0xcb3063ff | j << 10); + // sub sp,sp,x16,lsl #(j) + } + } + o(0x910003bf); // mov sp,x29 + o(0xa8ce7bfd); // ldp x29,x30,[sp],#224 + + o(0xd65f03c0); // ret +} + +ST_FUNC void gen_fill_nops(int bytes) +{ + if ((bytes & 3)) + tcc_error("alignment of code section not multiple of 4"); + while (bytes > 0) { + o(0xd503201f); // nop + bytes -= 4; + } +} + +// Generate forward branch to label: +ST_FUNC int gjmp(int t) +{ + int r = ind; + if (nocode_wanted) + return t; + o(t); + return r; +} + +// Generate branch to known address: +ST_FUNC void gjmp_addr(int a) +{ + assert(a - ind + 0x8000000 < 0x10000000); + o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff)); +} + +ST_FUNC int gjmp_append(int n, int t) +{ + void *p; + /* insert vtop->c jump list in t */ + if (n) { + uint32_t n1 = n, n2; + while ((n2 = read32le(p = cur_text_section->data + n1))) + n1 = n2; + write32le(p, t); + t = n; + } + return t; +} + +void arm64_vset_VT_CMP(int op) +{ + if (op >= TOK_ULT && op <= TOK_GT) { + vtop->cmp_r = vtop->r; + vset_VT_CMP(0x80); + } +} + +static void arm64_gen_opil(int op, uint32_t l); + +static void arm64_load_cmp(int r, SValue *sv) +{ + sv->r = sv->cmp_r; + if (sv->c.i & 1) { + vpushi(1); + arm64_gen_opil('^', 0); + } + if (r != sv->r) { + load(r, sv); + sv->r = r; + } +} + +ST_FUNC int gjmp_cond(int op, int t) +{ + int bt = vtop->type.t & VT_BTYPE; + + int inv = op & 1; + vtop->r = vtop->cmp_r; + + if (bt == VT_LDOUBLE) { + uint32_t a, b, f = fltr(gv(RC_FLOAT)); + a = get_reg(RC_INT); + vpushi(0); + vtop[0].r = a; + b = get_reg(RC_INT); + a = intr(a); + b = intr(b); + o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0] + o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1] + o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1 + o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8 + --vtop; + } + else if (bt == VT_FLOAT || bt == VT_DOUBLE) { + uint32_t a = fltr(gv(RC_FLOAT)); + o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp + o(0x54000040 | !!inv); // b.eq/b.ne .+8 + } + else { + uint32_t ll = (bt == VT_PTR || bt == VT_LLONG); + uint32_t a = intr(gv(RC_INT)); + o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8 + } + return gjmp(t); +} + +static int arm64_iconst(uint64_t *val, SValue *sv) +{ + if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) + return 0; + if (val) { + int t = sv->type.t; + int bt = t & VT_BTYPE; + *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i : + (uint32_t)sv->c.i | + (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000))); + } + return 1; +} + +static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val, + uint32_t x, uint32_t a) +{ + if (op == '-' && !rev) { + val = -val; + op = '+'; + } + val = l ? val : (uint32_t)val; + + switch (op) { + + case '+': { + uint32_t s = l ? val >> 63 : val >> 31; + val = s ? -val : val; + val = l ? val : (uint32_t)val; + if (!(val & ~(uint64_t)0xfff)) + o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10); + else if (!(val & ~(uint64_t)0xfff000)) + o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10); + else { + arm64_movimm(30, val); // use x30 + o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5); + } + return 1; + } + + case '-': + if (!val) + o(0x4b0003e0 | l << 31 | x | a << 16); // neg + else if (val == (l ? (uint64_t)-1 : (uint32_t)-1)) + o(0x2a2003e0 | l << 31 | x | a << 16); // mvn + else { + arm64_movimm(30, val); // use x30 + o(0x4b0003c0 | l << 31 | x | a << 16); // sub + } + return 1; + + case '^': + if (val == -1 || (val == 0xffffffff && !l)) { + o(0x2a2003e0 | l << 31 | x | a << 16); // mvn + return 1; + } + // fall through + case '&': + case '|': { + int e = arm64_encode_bimm64(l ? val : val | val << 32); + if (e < 0) + return 0; + o((op == '&' ? 0x12000000 : + op == '|' ? 0x32000000 : 0x52000000) | + l << 31 | x | a << 5 | (uint32_t)e << 10); + return 1; + } + + case TOK_SAR: + case TOK_SHL: + case TOK_SHR: { + uint32_t n = 32 << l; + val = val & (n - 1); + if (rev) + return 0; + if (!val) { + // tcc_warning("shift count >= width of type"); + o(0x2a0003e0 | l << 31 | a << 16); + return 1; + } + else if (op == TOK_SHL) + o(0x53000000 | l << 31 | l << 22 | x | a << 5 | + (n - val) << 16 | (n - 1 - val) << 10); // lsl + else + o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 | + x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr + return 1; + } + + } + return 0; +} + +static void arm64_gen_opil(int op, uint32_t l) +{ + uint32_t x, a, b; + + // Special treatment for operations with a constant operand: + { + uint64_t val; + int rev = 1; + + if (arm64_iconst(0, &vtop[0])) { + vswap(); + rev = 0; + } + if (arm64_iconst(&val, &vtop[-1])) { + gv(RC_INT); + a = intr(vtop[0].r); + --vtop; + x = get_reg(RC_INT); + ++vtop; + if (arm64_gen_opic(op, l, rev, val, intr(x), a)) { + vtop[0].r = x; + vswap(); + --vtop; + return; + } + } + if (!rev) + vswap(); + } + + gv2(RC_INT, RC_INT); + assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST); + a = intr(vtop[-1].r); + b = intr(vtop[0].r); + vtop -= 2; + x = get_reg(RC_INT); + ++vtop; + vtop[0].r = x; + x = intr(x); + + switch (op) { + case '%': + // Use x30 for quotient: + o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv + o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 | + b << 16 | a << 10); // msub + break; + case '&': + o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and + break; + case '*': + o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul + break; + case '+': + o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add + break; + case '-': + o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub + break; + case '/': + o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv + break; + case '^': + o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor + break; + case '|': + o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr + break; + case TOK_EQ: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f17e0 | x); // cset wA,eq + break; + case TOK_GE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fb7e0 | x); // cset wA,ge + break; + case TOK_GT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fd7e0 | x); // cset wA,gt + break; + case TOK_LE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fc7e0 | x); // cset wA,le + break; + case TOK_LT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9fa7e0 | x); // cset wA,lt + break; + case TOK_NE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f07e0 | x); // cset wA,ne + break; + case TOK_SAR: + o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr + break; + case TOK_SHL: + o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl + break; + case TOK_SHR: + o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr + break; + case TOK_UDIV: + case TOK_PDIV: + o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv + break; + case TOK_UGE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f37e0 | x); // cset wA,cs + break; + case TOK_UGT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f97e0 | x); // cset wA,hi + break; + case TOK_ULT: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f27e0 | x); // cset wA,cc + break; + case TOK_ULE: + o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp + o(0x1a9f87e0 | x); // cset wA,ls + break; + case TOK_UMOD: + // Use x30 for quotient: + o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv + o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 | + b << 16 | a << 10); // msub + break; + default: + assert(0); + } +} + +ST_FUNC void gen_opi(int op) +{ + arm64_gen_opil(op, 0); + arm64_vset_VT_CMP(op); +} + +ST_FUNC void gen_opl(int op) +{ + arm64_gen_opil(op, 1); + arm64_vset_VT_CMP(op); +} + +ST_FUNC void gen_opf(int op) +{ + uint32_t x, a, b, dbl; + + if (vtop[0].type.t == VT_LDOUBLE) { + CType type = vtop[0].type; + int func = 0; + int cond = -1; + switch (op) { + case '*': func = TOK___multf3; break; + case '+': func = TOK___addtf3; break; + case '-': func = TOK___subtf3; break; + case '/': func = TOK___divtf3; break; + case TOK_EQ: func = TOK___eqtf2; cond = 1; break; + case TOK_NE: func = TOK___netf2; cond = 0; break; + case TOK_LT: func = TOK___lttf2; cond = 10; break; + case TOK_GE: func = TOK___getf2; cond = 11; break; + case TOK_LE: func = TOK___letf2; cond = 12; break; + case TOK_GT: func = TOK___gttf2; cond = 13; break; + default: assert(0); break; + } + vpush_helper_func(func); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = cond < 0 ? REG_FRET : REG_IRET; + if (cond < 0) + vtop->type = type; + else { + o(0x7100001f); // cmp w0,#0 + o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond) + } + return; + } + + dbl = vtop[0].type.t != VT_FLOAT; + gv2(RC_FLOAT, RC_FLOAT); + assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST); + a = fltr(vtop[-1].r); + b = fltr(vtop[0].r); + vtop -= 2; + switch (op) { + case TOK_EQ: case TOK_NE: + case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT: + x = get_reg(RC_INT); + ++vtop; + vtop[0].r = x; + x = intr(x); + break; + default: + x = get_reg(RC_FLOAT); + ++vtop; + vtop[0].r = x; + x = fltr(x); + break; + } + + switch (op) { + case '*': + o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul + break; + case '+': + o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd + break; + case '-': + o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub + break; + case '/': + o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv + break; + case TOK_EQ: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f17e0 | x); // cset w(x),eq + break; + case TOK_GE: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9fb7e0 | x); // cset w(x),ge + break; + case TOK_GT: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9fd7e0 | x); // cset w(x),gt + break; + case TOK_LE: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f87e0 | x); // cset w(x),ls + break; + case TOK_LT: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f57e0 | x); // cset w(x),mi + break; + case TOK_NE: + o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp + o(0x1a9f07e0 | x); // cset w(x),ne + break; + default: + assert(0); + } + arm64_vset_VT_CMP(op); +} + +// Generate sign extension from 32 to 64 bits: +ST_FUNC void gen_cvt_sxtw(void) +{ + uint32_t r = intr(gv(RC_INT)); + o(0x93407c00 | r | r << 5); // sxtw x(r),w(r) +} + +/* char/short to int conversion */ +ST_FUNC void gen_cvt_csti(int t) +{ + int r = intr(gv(RC_INT)); + o(0x13001c00 + | ((t & VT_BTYPE) == VT_SHORT) << 13 + | (uint32_t)!!(t & VT_UNSIGNED) << 30 + | r | r << 5); // [su]xt[bh] w(r),w(r) +} + +ST_FUNC void gen_cvt_itof(int t) +{ + if (t == VT_LDOUBLE) { + int f = vtop->type.t; + int func = (f & VT_BTYPE) == VT_LLONG ? + (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) : + (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf); + vpush_helper_func(func); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->type.t = t; + vtop->r = REG_FRET; + return; + } + else { + int d, n = intr(gv(RC_INT)); + int s = !(vtop->type.t & VT_UNSIGNED); + uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG); + --vtop; + d = get_reg(RC_FLOAT); + ++vtop; + vtop[0].r = d; + o(0x1e220000 | (uint32_t)!s << 16 | + (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) | + l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n) + } +} + +ST_FUNC void gen_cvt_ftoi(int t) +{ + if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + int func = (t & VT_BTYPE) == VT_LLONG ? + (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) : + (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi); + vpush_helper_func(func); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->type.t = t; + vtop->r = REG_IRET; + return; + } + else { + int d, n = fltr(gv(RC_FLOAT)); + uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT); + --vtop; + d = get_reg(RC_INT); + ++vtop; + vtop[0].r = d; + o(0x1e380000 | + (uint32_t)!!(t & VT_UNSIGNED) << 16 | + (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) | + l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n) + } +} + +ST_FUNC void gen_cvt_ftof(int t) +{ + int f = vtop[0].type.t & VT_BTYPE; + assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE); + assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE); + if (t == f) + return; + + if (t == VT_LDOUBLE || f == VT_LDOUBLE) { + int func = (t == VT_LDOUBLE) ? + (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) : + (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2); + vpush_helper_func(func); + vrott(2); + gfunc_call(1); + vpushi(0); + vtop->type.t = t; + vtop->r = REG_FRET; + } + else { + int x, a; + gv(RC_FLOAT); + assert(vtop[0].r < VT_CONST); + a = fltr(vtop[0].r); + --vtop; + x = get_reg(RC_FLOAT); + ++vtop; + vtop[0].r = x; + x = fltr(x); + + if (f == VT_FLOAT) + o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a) + else + o(0x1e624000 | x | a << 5); // fcvt s(x),d(a) + } +} + +/* increment tcov counter */ +ST_FUNC void gen_increment_tcov (SValue *sv) +{ + int r1, r2; + + vpushv(sv); + vtop->r = r1 = get_reg(RC_INT); + r2 = get_reg(RC_INT); + greloca(cur_text_section, sv->sym, ind, R_AARCH64_ADR_GOT_PAGE, 0); + o(0x90000000 | r1); // adrp r1, #sym + greloca(cur_text_section, sv->sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0); + o(0xf9400000 | r1 | (r1 << 5)); // ld xr,[xr, #sym] + o(0xf9400000 | (intr(r1)<<5) | intr(r2)); // ldr r2, [r1] + o(0x91000400 | (intr(r2)<<5) | intr(r2)); // add r2, r2, #1 + o(0xf9000000 | (intr(r1)<<5) | intr(r2)); // str r2, [r1] + vpop(); +} + +ST_FUNC void ggoto(void) +{ + arm64_gen_bl_or_b(1); + --vtop; +} + +ST_FUNC void gen_clear_cache(void) +{ + uint32_t beg, end, dsz, isz, p, lab1, b1; + gv2(RC_INT, RC_INT); + vpushi(0); + vtop->r = get_reg(RC_INT); + vpushi(0); + vtop->r = get_reg(RC_INT); + vpushi(0); + vtop->r = get_reg(RC_INT); + beg = intr(vtop[-4].r); // x0 + end = intr(vtop[-3].r); // x1 + dsz = intr(vtop[-2].r); // x2 + isz = intr(vtop[-1].r); // x3 + p = intr(vtop[0].r); // x4 + vtop -= 5; + + o(0xd53b0020 | isz); // mrs x(isz),ctr_el0 + o(0x52800080 | p); // mov w(p),#4 + o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4 + o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz) + o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15 + o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz) + o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1 + o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p) + b1 = ind; o(0x14000000); // b + lab1 = ind; + o(0xd50b7b20 | p); // dc cvau,x(p) + o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz) + write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2); + o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end) + o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1 + o(0xd5033b9f); // dsb ish + o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1 + o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p) + b1 = ind; o(0x14000000); // b + lab1 = ind; + o(0xd50b7520 | p); // ic ivau,x(p) + o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz) + write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2); + o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end) + o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1 + o(0xd5033b9f); // dsb ish + o(0xd5033fdf); // isb +} + +ST_FUNC void gen_vla_sp_save(int addr) { + uint32_t r = intr(get_reg(RC_INT)); + o(0x910003e0 | r); // mov x(r),sp + arm64_strx(3, r, 29, addr); +} + +ST_FUNC void gen_vla_sp_restore(int addr) { + // Use x30 because this function can be called when there + // is a live return value in x0 but there is nothing on + // the value stack to prevent get_reg from returning x0. + uint32_t r = 30; + arm64_ldrx(0, 3, r, 29, addr); + o(0x9100001f | r << 5); // mov sp,x(r) +} + +ST_FUNC void gen_vla_alloc(CType *type, int align) { + uint32_t r; +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + vpushv(vtop); +#endif + r = intr(gv(RC_INT)); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) + o(0x91004000 | r | r << 5); // add x(r),x(r),#15+1 + else +#endif + o(0x91003c00 | r | r << 5); // add x(r),x(r),#15 + o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15 + o(0xcb2063ff | r << 16); // sub sp,sp,x(r) + vpop(); +#if defined(CONFIG_TCC_BCHECK) + if (tcc_state->do_bounds_check) { + vpushi(0); + vtop->r = TREG_R(0); + o(0x910003e0 | vtop->r); // mov r0,sp + vswap(); + vpush_helper_func(TOK___bound_new_region); + vrott(3); + gfunc_call(2); + func_bound_add_epilog = 1; + } +#endif +} + +/* end of A64 code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final/arm64-link.c b/05/tcc-final/arm64-link.c new file mode 100644 index 0000000..baf13fe --- /dev/null +++ b/05/tcc-final/arm64-link.c @@ -0,0 +1,322 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_AARCH64 + +#define R_DATA_32 R_AARCH64_ABS32 +#define R_DATA_PTR R_AARCH64_ABS64 +#define R_JMP_SLOT R_AARCH64_JUMP_SLOT +#define R_GLOB_DAT R_AARCH64_GLOB_DAT +#define R_COPY R_AARCH64_COPY +#define R_RELATIVE R_AARCH64_RELATIVE + +#define R_NUM R_AARCH64_NUM + +#define ELF_START_ADDR 0x00400000 +#define ELF_PAGE_SIZE 0x10000 + +#define PCRELATIVE_DLLPLT 1 +#define RELOCATE_DLLPLT 1 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +#ifdef NEED_RELOC_TYPE +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_AARCH64_ABS32: + case R_AARCH64_ABS64: + case R_AARCH64_PREL32: + case R_AARCH64_MOVW_UABS_G0_NC: + case R_AARCH64_MOVW_UABS_G1_NC: + case R_AARCH64_MOVW_UABS_G2_NC: + case R_AARCH64_MOVW_UABS_G3: + case R_AARCH64_ADR_PREL_PG_HI21: + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_ADR_GOT_PAGE: + case R_AARCH64_LD64_GOT_LO12_NC: + case R_AARCH64_LDST128_ABS_LO12_NC: + case R_AARCH64_LDST64_ABS_LO12_NC: + case R_AARCH64_LDST32_ABS_LO12_NC: + case R_AARCH64_LDST16_ABS_LO12_NC: + case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_GLOB_DAT: + case R_AARCH64_COPY: + return 0; + + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: + case R_AARCH64_JUMP_SLOT: + return 1; + } + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_AARCH64_PREL32: + case R_AARCH64_MOVW_UABS_G0_NC: + case R_AARCH64_MOVW_UABS_G1_NC: + case R_AARCH64_MOVW_UABS_G2_NC: + case R_AARCH64_MOVW_UABS_G3: + case R_AARCH64_ADR_PREL_PG_HI21: + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_LDST128_ABS_LO12_NC: + case R_AARCH64_LDST64_ABS_LO12_NC: + case R_AARCH64_LDST32_ABS_LO12_NC: + case R_AARCH64_LDST16_ABS_LO12_NC: + case R_AARCH64_LDST8_ABS_LO12_NC: + case R_AARCH64_GLOB_DAT: + case R_AARCH64_JUMP_SLOT: + case R_AARCH64_COPY: + return NO_GOTPLT_ENTRY; + + case R_AARCH64_ABS32: + case R_AARCH64_ABS64: + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: + return AUTO_GOTPLT_ENTRY; + + case R_AARCH64_ADR_GOT_PAGE: + case R_AARCH64_LD64_GOT_LO12_NC: + return ALWAYS_GOTPLT_ENTRY; + } + return -1; +} + +#ifdef NEED_BUILD_GOT +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + unsigned plt_offset; + + if (plt->data_offset == 0) { + section_ptr_add(plt, 32); + } + plt_offset = plt->data_offset; + + p = section_ptr_add(plt, 16); + write32le(p, got_offset); + write32le(p + 4, (uint64_t) got_offset >> 32); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + uint64_t plt = s1->plt->sh_addr; + uint64_t got = s1->got->sh_addr + 16; + uint64_t off = (got >> 12) - (plt >> 12); + if ((off + ((uint32_t)1 << 20)) >> 21) + tcc_error("Failed relocating PLT (off=0x%lx, got=0x%lx, plt=0x%lx)", (long)off, (long)got, (long)plt); + write32le(p, 0xa9bf7bf0); // stp x16,x30,[sp,#-16]! + write32le(p + 4, (0x90000010 | // adrp x16,... + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + write32le(p + 8, (0xf9400211 | // ldr x17,[x16,#...] + (got & 0xff8) << 7)); + write32le(p + 12, (0x91000210 | // add x16,x16,#... + (got & 0xfff) << 10)); + write32le(p + 16, 0xd61f0220); // br x17 + write32le(p + 20, 0xd503201f); // nop + write32le(p + 24, 0xd503201f); // nop + write32le(p + 28, 0xd503201f); // nop + p += 32; + got = s1->got->sh_addr; + while (p < p_end) { + uint64_t pc = plt + (p - s1->plt->data); + uint64_t addr = got + read64le(p); + uint64_t off = (addr >> 12) - (pc >> 12); + if ((off + ((uint32_t)1 << 20)) >> 21) + tcc_error("Failed relocating PLT (off=0x%lx, addr=0x%lx, pc=0x%lx)", (long)off, (long)addr, (long)pc); + write32le(p, (0x90000010 | // adrp x16,... + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + write32le(p + 4, (0xf9400211 | // ldr x17,[x16,#...] + (addr & 0xff8) << 7)); + write32le(p + 8, (0x91000210 | // add x16,x16,#... + (addr & 0xfff) << 10)); + write32le(p + 12, 0xd61f0220); // br x17 + p += 16; + } + } + + if (s1->plt->reloc) { + ElfW_Rel *rel; + p = s1->got->data; + for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) { + write64le(p + rel->r_offset, s1->plt->sh_addr); + } + } +} +#endif +#endif + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + int sym_index = ELFW(R_SYM)(rel->r_info), esym_index; +#ifdef DEBUG_RELOC + ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; +#endif + + switch(type) { + case R_AARCH64_ABS64: + if (s1->output_type == TCC_OUTPUT_DLL) { + esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index; + qrel->r_offset = rel->r_offset; + if (esym_index) { + qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_ABS64); + qrel->r_addend = rel->r_addend; + qrel++; + break; + } else { + qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE); + qrel->r_addend = read64le(ptr) + val; + qrel++; + } + } + add64le(ptr, val); + return; + case R_AARCH64_ABS32: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* XXX: this logic may depend on TCC's codegen + now TCC uses R_AARCH64_RELATIVE even for a 64bit pointer */ + qrel->r_offset = rel->r_offset; + qrel->r_info = ELFW(R_INFO)(0, R_AARCH64_RELATIVE); + /* Use sign extension! */ + qrel->r_addend = (int)read32le(ptr) + val; + qrel++; + } + add32le(ptr, val); + return; + case R_AARCH64_PREL32: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* DLL relocation */ + esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index; + if (esym_index) { + qrel->r_offset = rel->r_offset; + qrel->r_info = ELFW(R_INFO)(esym_index, R_AARCH64_PREL32); + /* Use sign extension! */ + qrel->r_addend = (int)read32le(ptr) + rel->r_addend; + qrel++; + break; + } + } + write32le(ptr, val - addr); + return; + case R_AARCH64_MOVW_UABS_G0_NC: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val & 0xffff) << 5)); + return; + case R_AARCH64_MOVW_UABS_G1_NC: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val >> 16 & 0xffff) << 5)); + return; + case R_AARCH64_MOVW_UABS_G2_NC: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val >> 32 & 0xffff) << 5)); + return; + case R_AARCH64_MOVW_UABS_G3: + write32le(ptr, ((read32le(ptr) & 0xffe0001f) | + (val >> 48 & 0xffff) << 5)); + return; + case R_AARCH64_ADR_PREL_PG_HI21: { + uint64_t off = (val >> 12) - (addr >> 12); + if ((off + ((uint64_t)1 << 20)) >> 21) + tcc_error("R_AARCH64_ADR_PREL_PG_HI21 relocation failed"); + write32le(ptr, ((read32le(ptr) & 0x9f00001f) | + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + return; + } + case R_AARCH64_ADD_ABS_LO12_NC: + case R_AARCH64_LDST8_ABS_LO12_NC: + write32le(ptr, ((read32le(ptr) & 0xffc003ff) | + (val & 0xfff) << 10)); + return; + case R_AARCH64_LDST16_ABS_LO12_NC: + write32le(ptr, ((read32le(ptr) & 0xffc003ff) | + (val & 0xffe) << 9)); + return; + case R_AARCH64_LDST32_ABS_LO12_NC: + write32le(ptr, ((read32le(ptr) & 0xffc003ff) | + (val & 0xffc) << 8)); + return; + case R_AARCH64_LDST64_ABS_LO12_NC: + write32le(ptr, ((read32le(ptr) & 0xffc003ff) | + (val & 0xff8) << 7)); + return; + case R_AARCH64_LDST128_ABS_LO12_NC: + write32le(ptr, ((read32le(ptr) & 0xffc003ff) | + (val & 0xff0) << 6)); + return; + case R_AARCH64_JUMP26: + case R_AARCH64_CALL26: +#ifdef DEBUG_RELOC + printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, val, + (char *) symtab_section->link->data + sym->st_name); +#endif + if (((val - addr) + ((uint64_t)1 << 27)) & ~(uint64_t)0xffffffc) + tcc_error("R_AARCH64_(JUMP|CALL)26 relocation failed" + " (val=%lx, addr=%lx)", (long)val, (long)addr); + write32le(ptr, (0x14000000 | + (uint32_t)(type == R_AARCH64_CALL26) << 31 | + ((val - addr) >> 2 & 0x3ffffff))); + return; + case R_AARCH64_ADR_GOT_PAGE: { + uint64_t off = + (((s1->got->sh_addr + + get_sym_attr(s1, sym_index, 0)->got_offset) >> 12) - (addr >> 12)); + if ((off + ((uint64_t)1 << 20)) >> 21) + tcc_error("R_AARCH64_ADR_GOT_PAGE relocation failed"); + write32le(ptr, ((read32le(ptr) & 0x9f00001f) | + (off & 0x1ffffc) << 3 | (off & 3) << 29)); + return; + } + case R_AARCH64_LD64_GOT_LO12_NC: + write32le(ptr, + ((read32le(ptr) & 0xfff803ff) | + ((s1->got->sh_addr + + get_sym_attr(s1, sym_index, 0)->got_offset) & 0xff8) << 7)); + return; + case R_AARCH64_COPY: + return; + case R_AARCH64_GLOB_DAT: + case R_AARCH64_JUMP_SLOT: + /* They don't need addend */ +#ifdef DEBUG_RELOC + printf ("reloc %d @ 0x%lx: val=0x%lx name=%s\n", type, addr, + val - rel->r_addend, + (char *) symtab_section->link->data + sym->st_name); +#endif + write64le(ptr, val - rel->r_addend); + return; + case R_AARCH64_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + return; + default: + fprintf(stderr, "FIXME: handle reloc type %x at %x [%p] to %x\n", + type, (unsigned)addr, ptr, (unsigned)val); + return; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/c67-gen.c b/05/tcc-final/c67-gen.c new file mode 100644 index 0000000..9490a27 --- /dev/null +++ b/05/tcc-final/c67-gen.c @@ -0,0 +1,2543 @@ +/* + * TMS320C67xx code generator for TCC + * + * Copyright (c) 2001, 2002 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +/* #define ASSEMBLY_LISTING_C67 */ + +/* number of available registers */ +#define NB_REGS 24 + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_EAX 0x0004 +#define RC_ST0 0x0008 +#define RC_ECX 0x0010 +#define RC_EDX 0x0020 +#define RC_INT_BSIDE 0x00000040 /* generic integer register on b side */ +#define RC_C67_A4 0x00000100 +#define RC_C67_A5 0x00000200 +#define RC_C67_B4 0x00000400 +#define RC_C67_B5 0x00000800 +#define RC_C67_A6 0x00001000 +#define RC_C67_A7 0x00002000 +#define RC_C67_B6 0x00004000 +#define RC_C67_B7 0x00008000 +#define RC_C67_A8 0x00010000 +#define RC_C67_A9 0x00020000 +#define RC_C67_B8 0x00040000 +#define RC_C67_B9 0x00080000 +#define RC_C67_A10 0x00100000 +#define RC_C67_A11 0x00200000 +#define RC_C67_B10 0x00400000 +#define RC_C67_B11 0x00800000 +#define RC_C67_A12 0x01000000 +#define RC_C67_A13 0x02000000 +#define RC_C67_B12 0x04000000 +#define RC_C67_B13 0x08000000 +#define RC_IRET RC_C67_A4 /* function return: integer register */ +#define RC_IRE2 RC_C67_A5 /* function return: second integer register */ +#define RC_FRET RC_C67_A4 /* function return: float register */ + +/* pretty names for the registers */ +enum { + TREG_EAX = 0, // really A2 + TREG_ECX, // really A3 + TREG_EDX, // really B0 + TREG_ST0, // really B1 + TREG_C67_A4, + TREG_C67_A5, + TREG_C67_B4, + TREG_C67_B5, + TREG_C67_A6, + TREG_C67_A7, + TREG_C67_B6, + TREG_C67_B7, + TREG_C67_A8, + TREG_C67_A9, + TREG_C67_B8, + TREG_C67_B9, + TREG_C67_A10, + TREG_C67_A11, + TREG_C67_B10, + TREG_C67_B11, + TREG_C67_A12, + TREG_C67_A13, + TREG_C67_B12, + TREG_C67_B13, +}; + +/* return registers for function */ +#define REG_IRET TREG_C67_A4 /* single word int return register */ +#define REG_IRE2 TREG_C67_A5 /* second word return register (for long long) */ +#define REG_FRET TREG_C67_A4 /* float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +/* #define INVERT_FUNC_PARAMS */ + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 12 +#define LDOUBLE_ALIGN 4 +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 8 + +#undef CONFIG_TCC_BCHECK + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#define USING_GLOBALS +#include "tcc.h" + +ST_DATA const char * const target_machine_defs = + "__C67__\0" + ; + +ST_DATA const int reg_classes[NB_REGS] = { + /* eax */ RC_INT | RC_FLOAT | RC_EAX, + // only allow even regs for floats (allow for doubles) + /* ecx */ RC_INT | RC_ECX, + /* edx */ RC_INT | RC_INT_BSIDE | RC_FLOAT | RC_EDX, + // only allow even regs for floats (allow for doubles) + /* st0 */ RC_INT | RC_INT_BSIDE | RC_ST0, + /* A4 */ RC_C67_A4, + /* A5 */ RC_C67_A5, + /* B4 */ RC_C67_B4, + /* B5 */ RC_C67_B5, + /* A6 */ RC_C67_A6, + /* A7 */ RC_C67_A7, + /* B6 */ RC_C67_B6, + /* B7 */ RC_C67_B7, + /* A8 */ RC_C67_A8, + /* A9 */ RC_C67_A9, + /* B8 */ RC_C67_B8, + /* B9 */ RC_C67_B9, + /* A10 */ RC_C67_A10, + /* A11 */ RC_C67_A11, + /* B10 */ RC_C67_B10, + /* B11 */ RC_C67_B11, + /* A12 */ RC_C67_A10, + /* A13 */ RC_C67_A11, + /* B12 */ RC_C67_B10, + /* B13 */ RC_C67_B11 +}; + +// although tcc thinks it is passing parameters on the stack, +// the C67 really passes up to the first 10 params in special +// regs or regs pairs (for 64 bit params). So keep track of +// the stack offsets so we can translate to the appropriate +// reg (pair) + +#define NoCallArgsPassedOnStack 10 +int NoOfCurFuncArgs; +int TranslateStackToReg[NoCallArgsPassedOnStack]; +int ParamLocOnStack[NoCallArgsPassedOnStack]; +int TotalBytesPushedOnStack; + +#ifndef FALSE +# define FALSE 0 +# define TRUE 1 +#endif + +#undef BOOL +#define BOOL int + +#define ALWAYS_ASSERT(x) \ +do {\ + if (!(x))\ + tcc_error("internal compiler error file at %s:%d", __FILE__, __LINE__);\ +} while (0) + +/******************************************************/ +static unsigned long func_sub_sp_offset; +static int func_ret_sub; + +static BOOL C67_invert_test; +static int C67_compare_reg; + +#ifdef ASSEMBLY_LISTING_C67 +FILE *f = NULL; +#endif + +void C67_g(int c) +{ + int ind1; + if (nocode_wanted) + return; +#ifdef ASSEMBLY_LISTING_C67 + fprintf(f, " %08X", c); +#endif + ind1 = ind + 4; + if (ind1 > (int) cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c & 0xff; + cur_text_section->data[ind + 1] = (c >> 8) & 0xff; + cur_text_section->data[ind + 2] = (c >> 16) & 0xff; + cur_text_section->data[ind + 3] = (c >> 24) & 0xff; + ind = ind1; +} + + +/* output a symbol and patch all calls to it */ +void gsym_addr(int t, int a) +{ + int n, *ptr; + while (t) { + ptr = (int *) (cur_text_section->data + t); + { + Sym *sym; + + // extract 32 bit address from MVKH/MVKL + n = ((*ptr >> 7) & 0xffff); + n |= ((*(ptr + 1) >> 7) & 0xffff) << 16; + + // define a label that will be relocated + + sym = get_sym_ref(&char_pointer_type, cur_text_section, a, 0); + greloc(cur_text_section, sym, t, R_C60LO16); + greloc(cur_text_section, sym, t + 4, R_C60HI16); + + // clear out where the pointer was + + *ptr &= ~(0xffff << 7); + *(ptr + 1) &= ~(0xffff << 7); + } + t = n; + } +} + +// these are regs that tcc doesn't really know about, +// but assign them unique values so the mapping routines +// can distinguish them + +#define C67_A0 105 +#define C67_SP 106 +#define C67_B3 107 +#define C67_FP 108 +#define C67_B2 109 +#define C67_CREG_ZERO -1 /* Special code for no condition reg test */ + + +int ConvertRegToRegClass(int r) +{ + // only works for A4-B13 + + return RC_C67_A4 << (r - TREG_C67_A4); +} + + +// map TCC reg to C67 reg number + +int C67_map_regn(int r) +{ + if (r == 0) // normal tcc regs + return 0x2; // A2 + else if (r == 1) // normal tcc regs + return 3; // A3 + else if (r == 2) // normal tcc regs + return 0; // B0 + else if (r == 3) // normal tcc regs + return 1; // B1 + else if (r >= TREG_C67_A4 && r <= TREG_C67_B13) // these form a pattern of alt pairs + return (((r & 0xfffffffc) >> 1) | (r & 1)) + 2; + else if (r == C67_A0) + return 0; // set to A0 (offset reg) + else if (r == C67_B2) + return 2; // set to B2 (offset reg) + else if (r == C67_B3) + return 3; // set to B3 (return address reg) + else if (r == C67_SP) + return 15; // set to SP (B15) (offset reg) + else if (r == C67_FP) + return 15; // set to FP (A15) (offset reg) + else if (r == C67_CREG_ZERO) + return 0; // Special code for no condition reg test + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + +// mapping from tcc reg number to +// C67 register to condition code field +// +// valid condition code regs are: +// +// tcc reg 2 ->B0 -> 1 +// tcc reg 3 ->B1 -> 2 +// tcc reg 0 -> A2 -> 5 +// tcc reg 1 -> A3 -> X +// tcc reg B2 -> 3 + +int C67_map_regc(int r) +{ + if (r == 0) // normal tcc regs + return 0x5; + else if (r == 2) // normal tcc regs + return 0x1; + else if (r == 3) // normal tcc regs + return 0x2; + else if (r == C67_B2) // normal tcc regs + return 0x3; + else if (r == C67_CREG_ZERO) + return 0; // Special code for no condition reg test + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + + +// map TCC reg to C67 reg side A or B + +int C67_map_regs(int r) +{ + if (r == 0) // normal tcc regs + return 0x0; + else if (r == 1) // normal tcc regs + return 0x0; + else if (r == 2) // normal tcc regs + return 0x1; + else if (r == 3) // normal tcc regs + return 0x1; + else if (r >= TREG_C67_A4 && r <= TREG_C67_B13) // these form a pattern of alt pairs + return (r & 2) >> 1; + else if (r == C67_A0) + return 0; // set to A side + else if (r == C67_B2) + return 1; // set to B side + else if (r == C67_B3) + return 1; // set to B side + else if (r == C67_SP) + return 0x1; // set to SP (B15) B side + else if (r == C67_FP) + return 0x0; // set to FP (A15) A side + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + +int C67_map_S12(char *s) +{ + if (strstr(s, ".S1") != NULL) + return 0; + else if (strcmp(s, ".S2")) + return 1; + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + +int C67_map_D12(char *s) +{ + if (strstr(s, ".D1") != NULL) + return 0; + else if (strcmp(s, ".D2")) + return 1; + else + ALWAYS_ASSERT(FALSE); + + return 0; +} + + + +void C67_asm(const char *s, int a, int b, int c) +{ + BOOL xpath; + +#ifdef ASSEMBLY_LISTING_C67 + if (!f) { + f = fopen("TCC67_out.txt", "wt"); + } + fprintf(f, "%04X ", ind); +#endif + + if (strstr(s, "MVKL") == s) { + C67_g((C67_map_regn(b) << 23) | + ((a & 0xffff) << 7) | (0x0a << 2) | (C67_map_regs(b) << 1)); + } else if (strstr(s, "MVKH") == s) { + C67_g((C67_map_regn(b) << 23) | + (((a >> 16) & 0xffff) << 7) | + (0x1a << 2) | (C67_map_regs(b) << 1)); + } else if (strstr(s, "STW.D SP POST DEC") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //SP B15 + (2 << 13) | //ucst5 (must keep 8 byte boundary !!) + (0xa << 9) | //mode a = post dec ucst + (0 << 8) | //r (LDDW bit 0) + (1 << 7) | //y D1/D2 use B side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STB.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STH.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STB.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STH.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STW.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STW.D *") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STH.D *") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (5 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STB.D *") == s) { + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (3 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "STW.D +*") == s) { + ALWAYS_ASSERT(c < 32); + C67_g((C67_map_regn(a) << 23) | //src + (C67_map_regn(b) << 18) | //base reg A0 + (c << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(b) << 7) | //y D1/D2 base reg side + (7 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of src + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D SP PRE INC") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg B15 + (2 << 13) | //ucst5 (must keep 8 byte boundary) + (9 << 9) | //mode 9 = pre inc ucst5 + (0 << 8) | //r (LDDW bit 0) + (1 << 7) | //y D1/D2 B side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDDW.D SP PRE INC") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg B15 + (1 << 13) | //ucst5 (must keep 8 byte boundary) + (9 << 9) | //mode 9 = pre inc ucst5 + (1 << 8) | //r (LDDW bit 1) + (1 << 7) | //y D1/D2 B side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDDW.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (1 << 8) | //r (LDDW bit 1) + (0 << 7) | //y D1/D2 A side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDH.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (4 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDB.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (2 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDHU.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (0 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDBU.D *+SP[A0]") == s) { + C67_g((C67_map_regn(a) << 23) | //dst + (15 << 18) | //base reg A15 + (0 << 13) | //offset reg A0 + (5 << 9) | //mode 5 = pos offset, base reg + off reg + (0 << 8) | //r (LDDW bit 0) + (0 << 7) | //y D1/D2 A side + (1 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(a) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDDW.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (1 << 8) | //r (LDDW bit 1) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDH.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (4 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDB.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (2 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDHU.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (0 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDBU.D *") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (0 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (1 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "LDW.D +*") == s) { + C67_g((C67_map_regn(b) << 23) | //dst + (C67_map_regn(a) << 18) | //base reg A15 + (1 << 13) | //cst5 + (1 << 9) | //mode 1 = pos cst offset + (0 << 8) | //r (LDDW bit 0) + (C67_map_regs(a) << 7) | //y D1/D2 src side + (6 << 4) | //ldst 3=STB, 5=STH 5, 7=STW, 6=LDW 4=LDH 2=LDB 0=LDHU 1=LDBU + (1 << 2) | //opcode + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "CMPLTSP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x3a << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGTSP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x39 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPEQSP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x38 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } + + else if (strstr(s, "CMPLTDP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x2a << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGTDP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x29 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPEQDP") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x28 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPLT") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x57 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGT") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x47 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPEQ") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x53 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPLTU") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x5f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "CMPGTU") == s) { + xpath = C67_map_regs(a) ^ C67_map_regs(b); + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x use cross path for src2 + (0x4f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side for reg c + (0 << 0)); //parallel + } else if (strstr(s, "B DISP") == s) { + C67_g((0 << 29) | //creg + (0 << 28) | //z + (a << 7) | //cnst + (0x4 << 2) | //opcode fixed + (0 << 1) | //S0/S1 + (0 << 0)); //parallel + } else if (strstr(s, "B.") == s) { + xpath = C67_map_regs(c) ^ 1; + + C67_g((C67_map_regc(b) << 29) | //creg + (a << 28) | //inv + (0 << 23) | //dst + (C67_map_regn(c) << 18) | //src2 + (0 << 13) | // + (xpath << 12) | //x cross path if !B side + (0xd << 6) | //opcode + (0x8 << 2) | //opcode fixed + (1 << 1) | //must be S2 + (0 << 0)); //parallel + } else if (strstr(s, "MV.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 (cst5) + (xpath << 12) | //x cross path if opposite sides + (0x2 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SPTRUNC.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0xb << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "DPTRUNC.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x1 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTSP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x4a << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTSPU.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x49 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x39 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "INTDPU.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x3b << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SPDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (0 << 13) | //src1 NA + (xpath << 12) | //x cross path if opposite sides + (0x2 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "DPSP.L") == s) { + ALWAYS_ASSERT(C67_map_regs(b) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + ((C67_map_regn(b) + 1) << 18) | //src2 WEIRD CPU must specify odd reg for some reason + (0 << 13) | //src1 NA + (0 << 12) | //x cross path if opposite sides + (0x9 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "ADD.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x3 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SUB.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x7 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "OR.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x7f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "AND.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x7b << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "XOR.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x6f << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "ADDSP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x10 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "ADDDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x18 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SUBSP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x11 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SUBDP.L") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x19 << 5) | //opcode + (0x6 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "MPYSP.M") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x1c << 7) | //opcode + (0x0 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "MPYDP.M") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 (possible x path) + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x0e << 7) | //opcode + (0x0 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "MPYI.M") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(a) == C67_map_regs(c)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 (cst5) + (xpath << 12) | //x cross path if opposite sides + (0x4 << 7) | //opcode + (0x0 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SHR.S") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x37 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SHRU.S") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x27 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "SHL.S") == s) { + xpath = C67_map_regs(b) ^ C67_map_regs(c); + + ALWAYS_ASSERT(C67_map_regs(c) == C67_map_regs(a)); + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(c) << 23) | //dst + (C67_map_regn(b) << 18) | //src2 + (C67_map_regn(a) << 13) | //src1 + (xpath << 12) | //x cross path if opposite sides + (0x33 << 6) | //opcode + (0x8 << 2) | //opcode fixed + (C67_map_regs(c) << 1) | //side of dest + (0 << 0)); //parallel + } else if (strstr(s, "||ADDK") == s) { + xpath = 0; // no xpath required just use the side of the src/dst + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(b) << 23) | //dst + (a << 07) | //scst16 + (0x14 << 2) | //opcode fixed + (C67_map_regs(b) << 1) | //side of dst + (1 << 0)); //parallel + } else if (strstr(s, "ADDK") == s) { + xpath = 0; // no xpath required just use the side of the src/dst + + C67_g((0 << 29) | //creg + (0 << 28) | //inv + (C67_map_regn(b) << 23) | //dst + (a << 07) | //scst16 + (0x14 << 2) | //opcode fixed + (C67_map_regs(b) << 1) | //side of dst + (0 << 0)); //parallel + } else if (strstr(s, "NOP") == s) { + C67_g(((a - 1) << 13) | //no of cycles + (0 << 0)); //parallel + } else + ALWAYS_ASSERT(FALSE); + +#ifdef ASSEMBLY_LISTING_C67 + fprintf(f, " %s %d %d %d\n", s, a, b, c); +#endif + +} + +//r=reg to load, fr=from reg, symbol for relocation, constant + +void C67_MVKL(int r, int fc) +{ + C67_asm("MVKL.", fc, r, 0); +} + +void C67_MVKH(int r, int fc) +{ + C67_asm("MVKH.", fc, r, 0); +} + +void C67_STB_SP_A0(int r) +{ + C67_asm("STB.D *+SP[A0]", r, 0, 0); // STB r,*+SP[A0] +} + +void C67_STH_SP_A0(int r) +{ + C67_asm("STH.D *+SP[A0]", r, 0, 0); // STH r,*+SP[A0] +} + +void C67_STW_SP_A0(int r) +{ + C67_asm("STW.D *+SP[A0]", r, 0, 0); // STW r,*+SP[A0] +} + +void C67_STB_PTR(int r, int r2) +{ + C67_asm("STB.D *", r, r2, 0); // STB r, *r2 +} + +void C67_STH_PTR(int r, int r2) +{ + C67_asm("STH.D *", r, r2, 0); // STH r, *r2 +} + +void C67_STW_PTR(int r, int r2) +{ + C67_asm("STW.D *", r, r2, 0); // STW r, *r2 +} + +void C67_STW_PTR_PRE_INC(int r, int r2, int n) +{ + C67_asm("STW.D +*", r, r2, n); // STW r, *+r2 +} + +void C67_PUSH(int r) +{ + C67_asm("STW.D SP POST DEC", r, 0, 0); // STW r,*SP-- +} + +void C67_LDW_SP_A0(int r) +{ + C67_asm("LDW.D *+SP[A0]", r, 0, 0); // LDW *+SP[A0],r +} + +void C67_LDDW_SP_A0(int r) +{ + C67_asm("LDDW.D *+SP[A0]", r, 0, 0); // LDDW *+SP[A0],r +} + +void C67_LDH_SP_A0(int r) +{ + C67_asm("LDH.D *+SP[A0]", r, 0, 0); // LDH *+SP[A0],r +} + +void C67_LDB_SP_A0(int r) +{ + C67_asm("LDB.D *+SP[A0]", r, 0, 0); // LDB *+SP[A0],r +} + +void C67_LDHU_SP_A0(int r) +{ + C67_asm("LDHU.D *+SP[A0]", r, 0, 0); // LDHU *+SP[A0],r +} + +void C67_LDBU_SP_A0(int r) +{ + C67_asm("LDBU.D *+SP[A0]", r, 0, 0); // LDBU *+SP[A0],r +} + +void C67_LDW_PTR(int r, int r2) +{ + C67_asm("LDW.D *", r, r2, 0); // LDW *r,r2 +} + +void C67_LDDW_PTR(int r, int r2) +{ + C67_asm("LDDW.D *", r, r2, 0); // LDDW *r,r2 +} + +void C67_LDH_PTR(int r, int r2) +{ + C67_asm("LDH.D *", r, r2, 0); // LDH *r,r2 +} + +void C67_LDB_PTR(int r, int r2) +{ + C67_asm("LDB.D *", r, r2, 0); // LDB *r,r2 +} + +void C67_LDHU_PTR(int r, int r2) +{ + C67_asm("LDHU.D *", r, r2, 0); // LDHU *r,r2 +} + +void C67_LDBU_PTR(int r, int r2) +{ + C67_asm("LDBU.D *", r, r2, 0); // LDBU *r,r2 +} + +void C67_LDW_PTR_PRE_INC(int r, int r2) +{ + C67_asm("LDW.D +*", r, r2, 0); // LDW *+r,r2 +} + +void C67_POP(int r) +{ + C67_asm("LDW.D SP PRE INC", r, 0, 0); // LDW *++SP,r +} + +void C67_POP_DW(int r) +{ + C67_asm("LDDW.D SP PRE INC", r, 0, 0); // LDDW *++SP,r +} + +void C67_CMPLT(int s1, int s2, int dst) +{ + C67_asm("CMPLT.L1", s1, s2, dst); +} + +void C67_CMPGT(int s1, int s2, int dst) +{ + C67_asm("CMPGT.L1", s1, s2, dst); +} + +void C67_CMPEQ(int s1, int s2, int dst) +{ + C67_asm("CMPEQ.L1", s1, s2, dst); +} + +void C67_CMPLTU(int s1, int s2, int dst) +{ + C67_asm("CMPLTU.L1", s1, s2, dst); +} + +void C67_CMPGTU(int s1, int s2, int dst) +{ + C67_asm("CMPGTU.L1", s1, s2, dst); +} + + +void C67_CMPLTSP(int s1, int s2, int dst) +{ + C67_asm("CMPLTSP.S1", s1, s2, dst); +} + +void C67_CMPGTSP(int s1, int s2, int dst) +{ + C67_asm("CMPGTSP.S1", s1, s2, dst); +} + +void C67_CMPEQSP(int s1, int s2, int dst) +{ + C67_asm("CMPEQSP.S1", s1, s2, dst); +} + +void C67_CMPLTDP(int s1, int s2, int dst) +{ + C67_asm("CMPLTDP.S1", s1, s2, dst); +} + +void C67_CMPGTDP(int s1, int s2, int dst) +{ + C67_asm("CMPGTDP.S1", s1, s2, dst); +} + +void C67_CMPEQDP(int s1, int s2, int dst) +{ + C67_asm("CMPEQDP.S1", s1, s2, dst); +} + + +void C67_IREG_B_REG(int inv, int r1, int r2) // [!R] B r2 +{ + C67_asm("B.S2", inv, r1, r2); +} + + +// call with how many 32 bit words to skip +// (0 would branch to the branch instruction) + +void C67_B_DISP(int disp) // B +2 Branch with constant displacement +{ + // Branch point is relative to the 8 word fetch packet + // + // we will assume the text section always starts on an 8 word (32 byte boundary) + // + // so add in how many words into the fetch packet the branch is + + + C67_asm("B DISP", disp + ((ind & 31) >> 2), 0, 0); +} + +void C67_NOP(int n) +{ + C67_asm("NOP", n, 0, 0); +} + +void C67_ADDK(int n, int r) +{ + ALWAYS_ASSERT(abs(n) < 32767); + + C67_asm("ADDK", n, r, 0); +} + +void C67_ADDK_PARALLEL(int n, int r) +{ + ALWAYS_ASSERT(abs(n) < 32767); + + C67_asm("||ADDK", n, r, 0); +} + +void C67_Adjust_ADDK(int *inst, int n) +{ + ALWAYS_ASSERT(abs(n) < 32767); + + *inst = (*inst & (~(0xffff << 7))) | ((n & 0xffff) << 7); +} + +void C67_MV(int r, int v) +{ + C67_asm("MV.L", 0, r, v); +} + + +void C67_DPTRUNC(int r, int v) +{ + C67_asm("DPTRUNC.L", 0, r, v); +} + +void C67_SPTRUNC(int r, int v) +{ + C67_asm("SPTRUNC.L", 0, r, v); +} + +void C67_INTSP(int r, int v) +{ + C67_asm("INTSP.L", 0, r, v); +} + +void C67_INTDP(int r, int v) +{ + C67_asm("INTDP.L", 0, r, v); +} + +void C67_INTSPU(int r, int v) +{ + C67_asm("INTSPU.L", 0, r, v); +} + +void C67_INTDPU(int r, int v) +{ + C67_asm("INTDPU.L", 0, r, v); +} + +void C67_SPDP(int r, int v) +{ + C67_asm("SPDP.L", 0, r, v); +} + +void C67_DPSP(int r, int v) // note regs must be on the same side +{ + C67_asm("DPSP.L", 0, r, v); +} + +void C67_ADD(int r, int v) +{ + C67_asm("ADD.L", v, r, v); +} + +void C67_SUB(int r, int v) +{ + C67_asm("SUB.L", v, r, v); +} + +void C67_AND(int r, int v) +{ + C67_asm("AND.L", v, r, v); +} + +void C67_OR(int r, int v) +{ + C67_asm("OR.L", v, r, v); +} + +void C67_XOR(int r, int v) +{ + C67_asm("XOR.L", v, r, v); +} + +void C67_ADDSP(int r, int v) +{ + C67_asm("ADDSP.L", v, r, v); +} + +void C67_SUBSP(int r, int v) +{ + C67_asm("SUBSP.L", v, r, v); +} + +void C67_MPYSP(int r, int v) +{ + C67_asm("MPYSP.M", v, r, v); +} + +void C67_ADDDP(int r, int v) +{ + C67_asm("ADDDP.L", v, r, v); +} + +void C67_SUBDP(int r, int v) +{ + C67_asm("SUBDP.L", v, r, v); +} + +void C67_MPYDP(int r, int v) +{ + C67_asm("MPYDP.M", v, r, v); +} + +void C67_MPYI(int r, int v) +{ + C67_asm("MPYI.M", v, r, v); +} + +void C67_SHL(int r, int v) +{ + C67_asm("SHL.S", r, v, v); +} + +void C67_SHRU(int r, int v) +{ + C67_asm("SHRU.S", r, v, v); +} + +void C67_SHR(int r, int v) +{ + C67_asm("SHR.S", r, v, v); +} + + + +/* load 'r' from value 'sv' */ +void load(int r, SValue * sv) +{ + int v, t, ft, fc, fr, size = 0, element; + BOOL Unsigned = FALSE; + SValue v1; + + fr = sv->r; + ft = sv->type.t; + fc = sv->c.i; + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + if (v == VT_LLOCAL) { + v1.type.t = VT_INT; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + load(r, &v1); + fr = r; + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + tcc_error("long double not supported"); + } else if ((ft & VT_TYPE) == VT_BYTE) { + size = 1; + } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) { + size = 1; + Unsigned = TRUE; + } else if ((ft & VT_TYPE) == VT_SHORT) { + size = 2; + } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) { + size = 2; + Unsigned = TRUE; + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + size = 8; + } else { + size = 4; + } + + // check if fc is a positive reference on the stack, + // if it is tcc is referencing what it thinks is a parameter + // on the stack, so check if it is really in a register. + + + if (v == VT_LOCAL && fc > 0) { + int stack_pos = 8; + + for (t = 0; t < NoCallArgsPassedOnStack; t++) { + if (fc == stack_pos) + break; + + stack_pos += TranslateStackToReg[t]; + } + + // param has been pushed on stack, get it like a local var + + fc = ParamLocOnStack[t] - 8; + } + + if ((fr & VT_VALMASK) < VT_CONST) // check for pure indirect + { + if (size == 1) { + if (Unsigned) + C67_LDBU_PTR(v, r); // LDBU *v,r + else + C67_LDB_PTR(v, r); // LDB *v,r + } else if (size == 2) { + if (Unsigned) + C67_LDHU_PTR(v, r); // LDHU *v,r + else + C67_LDH_PTR(v, r); // LDH *v,r + } else if (size == 4) { + C67_LDW_PTR(v, r); // LDW *v,r + } else if (size == 8) { + C67_LDDW_PTR(v, r); // LDDW *v,r + } + + C67_NOP(4); // NOP 4 + return; + } else if (fr & VT_SYM) { + greloc(cur_text_section, sv->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sv->sym, ind + 4, R_C60HI16); + + + C67_MVKL(C67_A0, fc); //r=reg to load, constant + C67_MVKH(C67_A0, fc); //r=reg to load, constant + + + if (size == 1) { + if (Unsigned) + C67_LDBU_PTR(C67_A0, r); // LDBU *A0,r + else + C67_LDB_PTR(C67_A0, r); // LDB *A0,r + } else if (size == 2) { + if (Unsigned) + C67_LDHU_PTR(C67_A0, r); // LDHU *A0,r + else + C67_LDH_PTR(C67_A0, r); // LDH *A0,r + } else if (size == 4) { + C67_LDW_PTR(C67_A0, r); // LDW *A0,r + } else if (size == 8) { + C67_LDDW_PTR(C67_A0, r); // LDDW *A0,r + } + + C67_NOP(4); // NOP 4 + return; + } else { + element = size; + + // divide offset in bytes to create element index + C67_MVKL(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + C67_MVKH(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + + if (size == 1) { + if (Unsigned) + C67_LDBU_SP_A0(r); // LDBU r, SP[A0] + else + C67_LDB_SP_A0(r); // LDB r, SP[A0] + } else if (size == 2) { + if (Unsigned) + C67_LDHU_SP_A0(r); // LDHU r, SP[A0] + else + C67_LDH_SP_A0(r); // LDH r, SP[A0] + } else if (size == 4) { + C67_LDW_SP_A0(r); // LDW r, SP[A0] + } else if (size == 8) { + C67_LDDW_SP_A0(r); // LDDW r, SP[A0] + } + + + C67_NOP(4); // NOP 4 + return; + } + } else { + if (v == VT_CONST) { + if (fr & VT_SYM) { + greloc(cur_text_section, sv->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sv->sym, ind + 4, R_C60HI16); + } + C67_MVKL(r, fc); //r=reg to load, constant + C67_MVKH(r, fc); //r=reg to load, constant + } else if (v == VT_LOCAL) { + C67_MVKL(r, fc + 8); //r=reg to load, constant C67 stack points to next free + C67_MVKH(r, fc + 8); //r=reg to load, constant + C67_ADD(C67_FP, r); // MV v,r v -> r + } else if (v == VT_CMP) { + C67_MV(C67_compare_reg, r); // MV v,r v -> r + } else if (v == VT_JMP || v == VT_JMPI) { + t = v & 1; + C67_B_DISP(4); // Branch with constant displacement, skip over this branch, load, nop, load + C67_MVKL(r, t); // r=reg to load, 0 or 1 (do this while branching) + C67_NOP(4); // NOP 4 + gsym(fc); // modifies other branches to branch here + C67_MVKL(r, t ^ 1); // r=reg to load, 0 or 1 + } else if (v != r) { + C67_MV(v, r); // MV v,r v -> r + + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_MV(v + 1, r + 1); // MV v,r v -> r + } + } +} + + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue * v) +{ + int fr, bt, ft, fc, size, t, element; + + ft = v->type.t; + fc = v->c.i; + fr = v->r & VT_VALMASK; + bt = ft & VT_BTYPE; + /* XXX: incorrect if float reg to reg */ + + if (bt == VT_LDOUBLE) { + tcc_error("long double not supported"); + } else { + if (bt == VT_SHORT) + size = 2; + else if (bt == VT_BYTE) + size = 1; + else if (bt == VT_DOUBLE) + size = 8; + else + size = 4; + + if ((v->r & VT_VALMASK) == VT_CONST) { + /* constant memory reference */ + + if (v->r & VT_SYM) { + greloc(cur_text_section, v->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, v->sym, ind + 4, R_C60HI16); + } + C67_MVKL(C67_A0, fc); //r=reg to load, constant + C67_MVKH(C67_A0, fc); //r=reg to load, constant + + if (size == 1) + C67_STB_PTR(r, C67_A0); // STB r, *A0 + else if (size == 2) + C67_STH_PTR(r, C67_A0); // STH r, *A0 + else if (size == 4 || size == 8) + C67_STW_PTR(r, C67_A0); // STW r, *A0 + + if (size == 8) + C67_STW_PTR_PRE_INC(r + 1, C67_A0, 1); // STW r, *+A0[1] + } else if ((v->r & VT_VALMASK) == VT_LOCAL) { + // check case of storing to passed argument that + // tcc thinks is on the stack but for C67 is + // passed as a reg. However it may have been + // saved to the stack, if that reg was required + // for a call to a child function + + if (fc > 0) // argument ?? + { + // walk through sizes and figure which param + + int stack_pos = 8; + + for (t = 0; t < NoCallArgsPassedOnStack; t++) { + if (fc == stack_pos) + break; + + stack_pos += TranslateStackToReg[t]; + } + + // param has been pushed on stack, get it like a local var + fc = ParamLocOnStack[t] - 8; + } + + if (size == 8) + element = 4; + else + element = size; + + // divide offset in bytes to create word index + C67_MVKL(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + C67_MVKH(C67_A0, (fc / element) + 8 / element); //r=reg to load, constant + + + + if (size == 1) + C67_STB_SP_A0(r); // STB r, SP[A0] + else if (size == 2) + C67_STH_SP_A0(r); // STH r, SP[A0] + else if (size == 4 || size == 8) + C67_STW_SP_A0(r); // STW r, SP[A0] + + if (size == 8) { + C67_ADDK(1, C67_A0); // ADDK 1,A0 + C67_STW_SP_A0(r + 1); // STW r, SP[A0] + } + } else { + if (size == 1) + C67_STB_PTR(r, fr); // STB r, *fr + else if (size == 2) + C67_STH_PTR(r, fr); // STH r, *fr + else if (size == 4 || size == 8) + C67_STW_PTR(r, fr); // STW r, *fr + + if (size == 8) { + C67_STW_PTR_PRE_INC(r + 1, fr, 1); // STW r, *+fr[1] + } + } + } +} + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + Sym *sym; + + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + /* constant case */ + if (vtop->r & VT_SYM) { + /* relocation case */ + + // get add into A0, then start the jump B3 + + greloc(cur_text_section, vtop->sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, vtop->sym, ind + 4, R_C60HI16); + + C67_MVKL(C67_A0, 0); //r=reg to load, constant + C67_MVKH(C67_A0, 0); //r=reg to load, constant + C67_IREG_B_REG(0, C67_CREG_ZERO, C67_A0); // B.S2x A0 + + if (is_jmp) { + C67_NOP(5); // simple jump, just put NOP + } else { + // Call, must load return address into B3 during delay slots + + sym = get_sym_ref(&char_pointer_type, cur_text_section, ind + 12, 0); // symbol for return address + greloc(cur_text_section, sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sym, ind + 4, R_C60HI16); + C67_MVKL(C67_B3, 0); //r=reg to load, constant + C67_MVKH(C67_B3, 0); //r=reg to load, constant + C67_NOP(3); // put remaining NOPs + } + } else { + /* put an empty PC32 relocation */ + ALWAYS_ASSERT(FALSE); + } + } else { + /* otherwise, indirect call */ + r = gv(RC_INT); + C67_IREG_B_REG(0, C67_CREG_ZERO, r); // B.S2x r + + if (is_jmp) { + C67_NOP(5); // simple jump, just put NOP + } else { + // Call, must load return address into B3 during delay slots + + sym = get_sym_ref(&char_pointer_type, cur_text_section, ind + 12, 0); // symbol for return address + greloc(cur_text_section, sym, ind, R_C60LO16); // rem the inst need to be patched + greloc(cur_text_section, sym, ind + 4, R_C60HI16); + C67_MVKL(C67_B3, 0); //r=reg to load, constant + C67_MVKH(C67_B3, 0); //r=reg to load, constant + C67_NOP(3); // put remaining NOPs + } + } +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) { + *ret_align = 1; // Never have to re-align return values for x86-64 + return 0; +} + +/* generate function call with address in (vtop->t, vtop->c) and free function + context. Stack entry is popped */ +void gfunc_call(int nb_args) +{ + int i, r, size = 0; + int args_sizes[NoCallArgsPassedOnStack]; + + if (nb_args > NoCallArgsPassedOnStack) { + tcc_error("more than 10 function params not currently supported"); + // handle more than 10, put some on the stack + } + + for (i = 0; i < nb_args; i++) { + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + ALWAYS_ASSERT(FALSE); + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + + + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + tcc_error("long long not supported"); + } else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + tcc_error("long double not supported"); + } else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) { + size = 8; + } else { + size = 4; + } + + // put the parameter into the corresponding reg (pair) + + r = gv(RC_C67_A4 << (2 * i)); + + // must put on stack because with 1 pass compiler , no way to tell + // if an up coming nested call might overwrite these regs + + C67_PUSH(r); + + if (size == 8) { + C67_STW_PTR_PRE_INC(r + 1, C67_SP, 3); // STW r, *+SP[3] (go back and put the other) + } + args_sizes[i] = size; + } + vtop--; + } + // POP all the params on the stack into registers for the + // immediate call (in reverse order) + + for (i = nb_args - 1; i >= 0; i--) { + + if (args_sizes[i] == 8) + C67_POP_DW(TREG_C67_A4 + i * 2); + else + C67_POP(TREG_C67_A4 + i * 2); + } + gcall_or_jmp(0); + vtop--; +} + + +// to be compatible with Code Composer for the C67 +// the first 10 parameters must be passed in registers +// (pairs for 64 bits) starting wit; A4:A5, then B4:B5 and +// ending with B12:B13. +// +// When a call is made, if the caller has its parameters +// in regs A4-B13 these must be saved before/as the call +// parameters are loaded and restored upon return (or if/when needed). + +/* generate function prolog of type 't' */ +void gfunc_prolog(Sym *func_sym) +{ + CType *func_type = &func_sym->type; + int addr, align, size, func_call, i; + Sym *sym; + CType *type; + + sym = func_type->ref; + func_call = sym->f.func_call; + addr = 8; + /* if the function returns a structure, then add an + implicit pointer parameter */ + if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { + func_vc = addr; + addr += 4; + } + + NoOfCurFuncArgs = 0; + + /* define parameters */ + while ((sym = sym->next) != NULL) { + type = &sym->type; + sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr); + size = type_size(type, &align); + size = (size + 3) & ~3; + + // keep track of size of arguments so + // we can translate where tcc thinks they + // are on the stack into the appropriate reg + + TranslateStackToReg[NoOfCurFuncArgs] = size; + NoOfCurFuncArgs++; + +#ifdef FUNC_STRUCT_PARAM_AS_PTR + /* structs are passed as pointer */ + if ((type->t & VT_BTYPE) == VT_STRUCT) { + size = 4; + } +#endif + addr += size; + } + func_ret_sub = 0; + /* pascal type call ? */ + if (func_call == FUNC_STDCALL) + func_ret_sub = addr - 8; + + C67_MV(C67_FP, C67_A0); // move FP -> A0 + C67_MV(C67_SP, C67_FP); // move SP -> FP + + // place all the args passed in regs onto the stack + + loc = 0; + for (i = 0; i < NoOfCurFuncArgs; i++) { + + ParamLocOnStack[i] = loc; // remember where the param is + loc += -8; + + C67_PUSH(TREG_C67_A4 + i * 2); + + if (TranslateStackToReg[i] == 8) { + C67_STW_PTR_PRE_INC(TREG_C67_A4 + i * 2 + 1, C67_SP, 3); // STW r, *+SP[1] (go back and put the other) + } + } + + TotalBytesPushedOnStack = -loc; + + func_sub_sp_offset = ind; // remember where we put the stack instruction + C67_ADDK(0, C67_SP); // ADDK.L2 loc,SP (just put zero temporarily) + + C67_PUSH(C67_A0); + C67_PUSH(C67_B3); +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + { + int local = (-loc + 7) & -8; // stack must stay aligned to 8 bytes for LDDW instr + C67_POP(C67_B3); + C67_NOP(4); // NOP wait for load + C67_IREG_B_REG(0, C67_CREG_ZERO, C67_B3); // B.S2 B3 + C67_POP(C67_FP); + C67_ADDK(local, C67_SP); // ADDK.L2 loc,SP + C67_Adjust_ADDK((int *) (cur_text_section->data + + func_sub_sp_offset), + -local + TotalBytesPushedOnStack); + C67_NOP(3); // NOP + } +} + +ST_FUNC void gen_fill_nops(int bytes) +{ + if ((bytes & 3)) + tcc_error("alignment of code section not multiple of 4"); + while (bytes > 0) { + C67_NOP(4); + bytes -= 4; + } +} + +/* generate a jump to a label */ +int gjmp(int t) +{ + int ind1 = ind; + if (nocode_wanted) + return t; + + C67_MVKL(C67_A0, t); //r=reg to load, constant + C67_MVKH(C67_A0, t); //r=reg to load, constant + C67_IREG_B_REG(0, C67_CREG_ZERO, C67_A0); // [!R] B.S2x A0 + C67_NOP(5); + return ind1; +} + +/* generate a jump to a fixed address */ +void gjmp_addr(int a) +{ + Sym *sym; + // I guess this routine is used for relative short + // local jumps, for now just handle it as the general + // case + + // define a label that will be relocated + + sym = get_sym_ref(&char_pointer_type, cur_text_section, a, 0); + greloc(cur_text_section, sym, ind, R_C60LO16); + greloc(cur_text_section, sym, ind + 4, R_C60HI16); + + gjmp(0); // place a zero there later the symbol will be added to it +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +ST_FUNC int gjmp_cond(int op, int t) +{ + int ind1; + int inv = op & 1; + if (nocode_wanted) + return t; + + /* fast case : can jump directly since flags are set */ + // C67 uses B2 sort of as flags register + ind1 = ind; + C67_MVKL(C67_A0, t); //r=reg to load, constant + C67_MVKH(C67_A0, t); //r=reg to load, constant + + if (C67_compare_reg != TREG_EAX && // check if not already in a conditional test reg + C67_compare_reg != TREG_EDX && + C67_compare_reg != TREG_ST0 && C67_compare_reg != C67_B2) { + C67_MV(C67_compare_reg, C67_B2); + C67_compare_reg = C67_B2; + } + + C67_IREG_B_REG(C67_invert_test ^ inv, C67_compare_reg, C67_A0); // [!R] B.S2x A0 + C67_NOP(5); + t = ind1; //return where we need to patch + + return t; +} + +ST_FUNC int gjmp_append(int n0, int t) +{ + if (n0) { + int n = n0, *p; + /* insert vtop->c jump list in t */ + + // I guess the idea is to traverse to the + // null at the end of the list and store t + // there + while (n != 0) { + p = (int *) (cur_text_section->data + n); + + // extract 32 bit address from MVKH/MVKL + n = ((*p >> 7) & 0xffff); + n |= ((*(p + 1) >> 7) & 0xffff) << 16; + } + *p |= (t & 0xffff) << 7; + *(p + 1) |= ((t >> 16) & 0xffff) << 7; + t = n0; + } + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + int r, fr, opc, t; + + switch (op) { + case '+': + case TOK_ADDC1: /* add with carry generation */ + opc = 0; + gen_op8: + + +// C67 can't do const compares, must load into a reg +// so just go to gv2 directly - tktk + + + + if (op >= TOK_ULT && op <= TOK_GT) + gv2(RC_INT_BSIDE, RC_INT); // make sure r (src1) is on the B Side of CPU + else + gv2(RC_INT, RC_INT); + + r = vtop[-1].r; + fr = vtop[0].r; + + C67_compare_reg = C67_B2; + + + if (op == TOK_LT) { + C67_CMPLT(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_GE) { + C67_CMPLT(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_GT) { + C67_CMPGT(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_LE) { + C67_CMPGT(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_EQ) { + C67_CMPEQ(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_NE) { + C67_CMPEQ(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_ULT) { + C67_CMPLTU(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_UGE) { + C67_CMPLTU(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == TOK_UGT) { + C67_CMPGTU(r, fr, C67_B2); + C67_invert_test = FALSE; + } else if (op == TOK_ULE) { + C67_CMPGTU(r, fr, C67_B2); + C67_invert_test = TRUE; + } else if (op == '+') + C67_ADD(fr, r); // ADD r,fr,r + else if (op == '-') + C67_SUB(fr, r); // SUB r,fr,r + else if (op == '&') + C67_AND(fr, r); // AND r,fr,r + else if (op == '|') + C67_OR(fr, r); // OR r,fr,r + else if (op == '^') + C67_XOR(fr, r); // XOR r,fr,r + else + ALWAYS_ASSERT(FALSE); + + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) + vset_VT_CMP(0x80); + break; + case '-': + case TOK_SUBC1: /* sub with carry generation */ + opc = 5; + goto gen_op8; + case TOK_ADDC2: /* add with carry use */ + opc = 2; + goto gen_op8; + case TOK_SUBC2: /* sub with carry use */ + opc = 3; + goto gen_op8; + case '&': + opc = 4; + goto gen_op8; + case '^': + opc = 6; + goto gen_op8; + case '|': + opc = 1; + goto gen_op8; + case '*': + case TOK_UMULL: + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_MPYI(fr, r); // 32 bit multiply fr,r,fr + C67_NOP(8); // NOP 8 for worst case + break; + case TOK_SHL: + gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_SHL(fr, r); // arithmetic/logical shift + break; + + case TOK_SHR: + gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_SHRU(fr, r); // logical shift + break; + + case TOK_SAR: + gv2(RC_INT_BSIDE, RC_INT_BSIDE); // shift amount must be on same side as dst + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + C67_SHR(fr, r); // arithmetic shift + break; + + case '/': + t = TOK__divi; + call_func: + vswap(); + /* call generic idiv function */ + vpush_helper_func(t); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = REG_IRET; + vtop->r2 = VT_CONST; + break; + case TOK_UDIV: + case TOK_PDIV: + t = TOK__divu; + goto call_func; + case '%': + t = TOK__remi; + goto call_func; + case TOK_UMOD: + t = TOK__remu; + goto call_func; + + default: + opc = 7; + goto gen_op8; + } +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +/* XXX: need to use ST1 too */ +void gen_opf(int op) +{ + int ft, fc, fr, r; + + if (op >= TOK_ULT && op <= TOK_GT) + gv2(RC_EDX, RC_EAX); // make sure src2 is on b side + else + gv2(RC_FLOAT, RC_FLOAT); // make sure src2 is on b side + + ft = vtop->type.t; + fc = vtop->c.i; + r = vtop->r; + fr = vtop[-1].r; + + + if ((ft & VT_BTYPE) == VT_LDOUBLE) + tcc_error("long doubles not supported"); + + if (op >= TOK_ULT && op <= TOK_GT) { + + r = vtop[-1].r; + fr = vtop[0].r; + + C67_compare_reg = C67_B2; + + if (op == TOK_LT) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPLTDP(r, fr, C67_B2); + else + C67_CMPLTSP(r, fr, C67_B2); + + C67_invert_test = FALSE; + } else if (op == TOK_GE) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPLTDP(r, fr, C67_B2); + else + C67_CMPLTSP(r, fr, C67_B2); + + C67_invert_test = TRUE; + } else if (op == TOK_GT) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPGTDP(r, fr, C67_B2); + else + C67_CMPGTSP(r, fr, C67_B2); + + C67_invert_test = FALSE; + } else if (op == TOK_LE) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPGTDP(r, fr, C67_B2); + else + C67_CMPGTSP(r, fr, C67_B2); + + C67_invert_test = TRUE; + } else if (op == TOK_EQ) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPEQDP(r, fr, C67_B2); + else + C67_CMPEQSP(r, fr, C67_B2); + + C67_invert_test = FALSE; + } else if (op == TOK_NE) { + if ((ft & VT_BTYPE) == VT_DOUBLE) + C67_CMPEQDP(r, fr, C67_B2); + else + C67_CMPEQSP(r, fr, C67_B2); + + C67_invert_test = TRUE; + } else { + ALWAYS_ASSERT(FALSE); + } + vset_VT_CMP(0x80); + } else { + if (op == '+') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + C67_ADDDP(r, fr); // ADD fr,r,fr + C67_NOP(6); + } else { + C67_ADDSP(r, fr); // ADD fr,r,fr + C67_NOP(3); + } + vtop--; + } else if (op == '-') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + C67_SUBDP(r, fr); // SUB fr,r,fr + C67_NOP(6); + } else { + C67_SUBSP(r, fr); // SUB fr,r,fr + C67_NOP(3); + } + vtop--; + } else if (op == '*') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + C67_MPYDP(r, fr); // MPY fr,r,fr + C67_NOP(9); + } else { + C67_MPYSP(r, fr); // MPY fr,r,fr + C67_NOP(3); + } + vtop--; + } else if (op == '/') { + if ((ft & VT_BTYPE) == VT_DOUBLE) { + // must call intrinsic DP floating point divide + vswap(); + /* call generic idiv function */ + vpush_helper_func(TOK__divd); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = REG_FRET; + vtop->r2 = REG_IRE2; + + } else { + // must call intrinsic SP floating point divide + vswap(); + /* call generic idiv function */ + vpush_helper_func(TOK__divf); + vrott(3); + gfunc_call(2); + vpushi(0); + vtop->r = REG_FRET; + vtop->r2 = VT_CONST; + } + } else + ALWAYS_ASSERT(FALSE); + + + } +} + + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +void gen_cvt_itof(int t) +{ + int r; + + gv(RC_INT); + r = vtop->r; + + if ((t & VT_BTYPE) == VT_DOUBLE) { + if (t & VT_UNSIGNED) + C67_INTDPU(r, r); + else + C67_INTDP(r, r); + + C67_NOP(4); + vtop->type.t = VT_DOUBLE; + } else { + if (t & VT_UNSIGNED) + C67_INTSPU(r, r); + else + C67_INTSP(r, r); + C67_NOP(3); + vtop->type.t = VT_FLOAT; + } + +} + +/* convert fp to int 't' type */ +/* XXX: handle long long case */ +void gen_cvt_ftoi(int t) +{ + int r; + + gv(RC_FLOAT); + r = vtop->r; + + if (t != VT_INT) + tcc_error("long long not supported"); + else { + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) { + C67_DPTRUNC(r, r); + C67_NOP(3); + } else { + C67_SPTRUNC(r, r); + C67_NOP(3); + } + + vtop->type.t = VT_INT; + + } +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ + int r, r2; + + if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE && + (t & VT_BTYPE) == VT_FLOAT) { + // convert double to float + + gv(RC_FLOAT); // get it in a register pair + + r = vtop->r; + + C67_DPSP(r, r); // convert it to SP same register + C67_NOP(3); + + vtop->type.t = VT_FLOAT; + vtop->r2 = VT_CONST; // set this as unused + } else if ((vtop->type.t & VT_BTYPE) == VT_FLOAT && + (t & VT_BTYPE) == VT_DOUBLE) { + // convert float to double + + gv(RC_FLOAT); // get it in a register + + r = vtop->r; + + if (r == TREG_EAX) { // make sure the paired reg is avail + r2 = get_reg(RC_ECX); + } else if (r == TREG_EDX) { + r2 = get_reg(RC_ST0); + } else { + ALWAYS_ASSERT(FALSE); + r2 = 0; /* avoid warning */ + } + + C67_SPDP(r, r); // convert it to DP same register + C67_NOP(1); + + vtop->type.t = VT_DOUBLE; + vtop->r2 = r2; // set this as unused + } else { + ALWAYS_ASSERT(FALSE); + } +} + +/* computed goto support */ +void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* Save the stack pointer onto the stack and return the location of its address */ +ST_FUNC void gen_vla_sp_save(int addr) { + tcc_error("variable length arrays unsupported for this target"); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + tcc_error("variable length arrays unsupported for this target"); +} + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { + tcc_error("variable length arrays unsupported for this target"); +} + +/* end of C67 code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final/c67-link.c b/05/tcc-final/c67-link.c new file mode 100644 index 0000000..514689c --- /dev/null +++ b/05/tcc-final/c67-link.c @@ -0,0 +1,125 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_C60 + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_C60_32 +#define R_DATA_PTR R_C60_32 +#define R_JMP_SLOT R_C60_JMP_SLOT +#define R_GLOB_DAT R_C60_GLOB_DAT +#define R_COPY R_C60_COPY +#define R_RELATIVE R_C60_RELATIVE + +#define R_NUM R_C60_NUM + +#define ELF_START_ADDR 0x00000400 +#define ELF_PAGE_SIZE 0x1000 + +#define PCRELATIVE_DLLPLT 0 +#define RELOCATE_DLLPLT 0 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_C60_32: + case R_C60LO16: + case R_C60HI16: + case R_C60_GOT32: + case R_C60_GOTOFF: + case R_C60_GOTPC: + case R_C60_COPY: + return 0; + + case R_C60_PLT32: + return 1; + } + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_C60_32: + case R_C60LO16: + case R_C60HI16: + case R_C60_COPY: + return NO_GOTPLT_ENTRY; + + case R_C60_GOTOFF: + case R_C60_GOTPC: + return BUILD_GOT_ONLY; + + case R_C60_PLT32: + case R_C60_GOT32: + return ALWAYS_GOTPLT_ENTRY; + } + return -1; +} + +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + tcc_error("C67 got not implemented"); + return 0; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (p < p_end) { + /* XXX: TODO */ + while (p < p_end) { + /* XXX: TODO */ + } + } +} + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + switch(type) { + case R_C60_32: + *(int *)ptr += val; + break; + case R_C60LO16: + { + uint32_t orig; + + /* put the low 16 bits of the absolute address add to what is + already there */ + orig = ((*(int *)(ptr )) >> 7) & 0xffff; + orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16; + + /* patch both at once - assumes always in pairs Low - High */ + *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | + (((val+orig) & 0xffff) << 7); + *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | + ((((val+orig)>>16) & 0xffff) << 7); + } + break; + case R_C60HI16: + break; + default: + fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n", + type, (unsigned) addr, ptr, (unsigned) val); + break; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/coff.h b/05/tcc-final/coff.h new file mode 100644 index 0000000..e8e6185 --- /dev/null +++ b/05/tcc-final/coff.h @@ -0,0 +1,446 @@ +/**************************************************************************/ +/* COFF.H */ +/* COFF data structures and related definitions used by the linker */ +/**************************************************************************/ + +/*------------------------------------------------------------------------*/ +/* COFF FILE HEADER */ +/*------------------------------------------------------------------------*/ +struct filehdr { + unsigned short f_magic; /* magic number */ + unsigned short f_nscns; /* number of sections */ + long f_timdat; /* time & date stamp */ + long f_symptr; /* file pointer to symtab */ + long f_nsyms; /* number of symtab entries */ + unsigned short f_opthdr; /* sizeof(optional hdr) */ + unsigned short f_flags; /* flags */ + unsigned short f_TargetID; /* for C6x = 0x0099 */ + }; + +/*------------------------------------------------------------------------*/ +/* File header flags */ +/*------------------------------------------------------------------------*/ +#define F_RELFLG 0x01 /* relocation info stripped from file */ +#define F_EXEC 0x02 /* file is executable (no unresolved refs) */ +#define F_LNNO 0x04 /* line numbers stripped from file */ +#define F_LSYMS 0x08 /* local symbols stripped from file */ +#define F_GSP10 0x10 /* 34010 version */ +#define F_GSP20 0x20 /* 34020 version */ +#define F_SWABD 0x40 /* bytes swabbed (in names) */ +#define F_AR16WR 0x80 /* byte ordering of an AR16WR (PDP-11) */ +#define F_LITTLE 0x100 /* byte ordering of an AR32WR (vax) */ +#define F_BIG 0x200 /* byte ordering of an AR32W (3B, maxi) */ +#define F_PATCH 0x400 /* contains "patch" list in optional header */ +#define F_NODF 0x400 + +#define F_VERSION (F_GSP10 | F_GSP20) +#define F_BYTE_ORDER (F_LITTLE | F_BIG) +#define FILHDR struct filehdr + +/* #define FILHSZ sizeof(FILHDR) */ +#define FILHSZ 22 /* above rounds to align on 4 bytes which causes problems */ + +#define COFF_C67_MAGIC 0x00c2 + +/*------------------------------------------------------------------------*/ +/* Macros to recognize magic numbers */ +/*------------------------------------------------------------------------*/ +#define ISMAGIC(x) (((unsigned short)(x))==(unsigned short)magic) +#define ISARCHIVE(x) ((((unsigned short)(x))==(unsigned short)ARTYPE)) +#define BADMAGIC(x) (((unsigned short)(x) & 0x8080) && !ISMAGIC(x)) + + +/*------------------------------------------------------------------------*/ +/* OPTIONAL FILE HEADER */ +/*------------------------------------------------------------------------*/ +typedef struct aouthdr { + short magic; /* see magic.h */ + short vstamp; /* version stamp */ + long tsize; /* text size in bytes, padded to FW bdry*/ + long dsize; /* initialized data " " */ + long bsize; /* uninitialized data " " */ + long entrypt; /* entry pt. */ + long text_start; /* base of text used for this file */ + long data_start; /* base of data used for this file */ +} AOUTHDR; + +#define AOUTSZ sizeof(AOUTHDR) + +/*----------------------------------------------------------------------*/ +/* When a UNIX aout header is to be built in the optional header, */ +/* the following magic numbers can appear in that header: */ +/* */ +/* AOUT1MAGIC : default : readonly sharable text segment */ +/* AOUT2MAGIC: : writable text segment */ +/* PAGEMAGIC : : configured for paging */ +/*----------------------------------------------------------------------*/ +#define AOUT1MAGIC 0410 +#define AOUT2MAGIC 0407 +#define PAGEMAGIC 0413 + + +/*------------------------------------------------------------------------*/ +/* COMMON ARCHIVE FILE STRUCTURES */ +/* */ +/* ARCHIVE File Organization: */ +/* _______________________________________________ */ +/* |__________ARCHIVE_MAGIC_STRING_______________| */ +/* |__________ARCHIVE_FILE_MEMBER_1______________| */ +/* | | */ +/* | Archive File Header "ar_hdr" | */ +/* |.............................................| */ +/* | Member Contents | */ +/* | 1. External symbol directory | */ +/* | 2. Text file | */ +/* |_____________________________________________| */ +/* |________ARCHIVE_FILE_MEMBER_2________________| */ +/* | "ar_hdr" | */ +/* |.............................................| */ +/* | Member Contents (.o or text file) | */ +/* |_____________________________________________| */ +/* | . . . | */ +/* | . . . | */ +/* | . . . | */ +/* |_____________________________________________| */ +/* |________ARCHIVE_FILE_MEMBER_n________________| */ +/* | "ar_hdr" | */ +/* |.............................................| */ +/* | Member Contents | */ +/* |_____________________________________________| */ +/* */ +/*------------------------------------------------------------------------*/ + +#define COFF_ARMAG "!\n" +#define SARMAG 8 +#define ARFMAG "`\n" + +struct ar_hdr /* archive file member header - printable ascii */ +{ + char ar_name[16]; /* file member name - `/' terminated */ + char ar_date[12]; /* file member date - decimal */ + char ar_uid[6]; /* file member user id - decimal */ + char ar_gid[6]; /* file member group id - decimal */ + char ar_mode[8]; /* file member mode - octal */ + char ar_size[10]; /* file member size - decimal */ + char ar_fmag[2]; /* ARFMAG - string to end header */ +}; + + +/*------------------------------------------------------------------------*/ +/* SECTION HEADER */ +/*------------------------------------------------------------------------*/ +struct scnhdr { + char s_name[8]; /* section name */ + long s_paddr; /* physical address */ + long s_vaddr; /* virtual address */ + long s_size; /* section size */ + long s_scnptr; /* file ptr to raw data for section */ + long s_relptr; /* file ptr to relocation */ + long s_lnnoptr; /* file ptr to line numbers */ + unsigned int s_nreloc; /* number of relocation entries */ + unsigned int s_nlnno; /* number of line number entries */ + unsigned int s_flags; /* flags */ + unsigned short s_reserved; /* reserved byte */ + unsigned short s_page; /* memory page id */ + }; + +#define SCNHDR struct scnhdr +#define SCNHSZ sizeof(SCNHDR) + +/*------------------------------------------------------------------------*/ +/* Define constants for names of "special" sections */ +/*------------------------------------------------------------------------*/ +/* #define _TEXT ".text" */ +#define _DATA ".data" +#define _BSS ".bss" +#define _CINIT ".cinit" +#define _TV ".tv" + +/*------------------------------------------------------------------------*/ +/* The low 4 bits of s_flags is used as a section "type" */ +/*------------------------------------------------------------------------*/ +#define STYP_REG 0x00 /* "regular" : allocated, relocated, loaded */ +#define STYP_DSECT 0x01 /* "dummy" : not allocated, relocated, not loaded */ +#define STYP_NOLOAD 0x02 /* "noload" : allocated, relocated, not loaded */ +#define STYP_GROUP 0x04 /* "grouped" : formed of input sections */ +#define STYP_PAD 0x08 /* "padding" : not allocated, not relocated, loaded */ +#define STYP_COPY 0x10 /* "copy" : used for C init tables - + not allocated, relocated, + loaded; reloc & lineno + entries processed normally */ +#define STYP_TEXT 0x20 /* section contains text only */ +#define STYP_DATA 0x40 /* section contains data only */ +#define STYP_BSS 0x80 /* section contains bss only */ + +#define STYP_ALIGN 0x100 /* align flag passed by old version assemblers */ +#define ALIGN_MASK 0x0F00 /* part of s_flags that is used for align vals */ +#define ALIGNSIZE(x) (1 << ((x & ALIGN_MASK) >> 8)) + + +/*------------------------------------------------------------------------*/ +/* RELOCATION ENTRIES */ +/*------------------------------------------------------------------------*/ +struct reloc +{ + long r_vaddr; /* (virtual) address of reference */ + short r_symndx; /* index into symbol table */ + unsigned short r_disp; /* additional bits for address calculation */ + unsigned short r_type; /* relocation type */ +}; + +#define RELOC struct reloc +#define RELSZ 10 /* sizeof(RELOC) */ + +/*--------------------------------------------------------------------------*/ +/* define all relocation types */ +/*--------------------------------------------------------------------------*/ + +#define R_ABS 0 /* absolute address - no relocation */ +#define R_DIR16 01 /* UNUSED */ +#define R_REL16 02 /* UNUSED */ +#define R_DIR24 04 /* UNUSED */ +#define R_REL24 05 /* 24 bits, direct */ +#define R_DIR32 06 /* UNUSED */ +#define R_RELBYTE 017 /* 8 bits, direct */ +#define R_RELWORD 020 /* 16 bits, direct */ +#define R_RELLONG 021 /* 32 bits, direct */ +#define R_PCRBYTE 022 /* 8 bits, PC-relative */ +#define R_PCRWORD 023 /* 16 bits, PC-relative */ +#define R_PCRLONG 024 /* 32 bits, PC-relative */ +#define R_OCRLONG 030 /* GSP: 32 bits, one's complement direct */ +#define R_GSPPCR16 031 /* GSP: 16 bits, PC relative (in words) */ +#define R_GSPOPR32 032 /* GSP: 32 bits, direct big-endian */ +#define R_PARTLS16 040 /* Brahma: 16 bit offset of 24 bit address*/ +#define R_PARTMS8 041 /* Brahma: 8 bit page of 24 bit address */ +#define R_PARTLS7 050 /* DSP: 7 bit offset of 16 bit address */ +#define R_PARTMS9 051 /* DSP: 9 bit page of 16 bit address */ +#define R_REL13 052 /* DSP: 13 bits, direct */ + + +/*------------------------------------------------------------------------*/ +/* LINE NUMBER ENTRIES */ +/*------------------------------------------------------------------------*/ +struct lineno +{ + union + { + long l_symndx ; /* sym. table index of function name + iff l_lnno == 0 */ + long l_paddr ; /* (physical) address of line number */ + } l_addr ; + unsigned short l_lnno ; /* line number */ +}; + +#define LINENO struct lineno +#define LINESZ 6 /* sizeof(LINENO) */ + + +/*------------------------------------------------------------------------*/ +/* STORAGE CLASSES */ +/*------------------------------------------------------------------------*/ +#define C_EFCN -1 /* physical end of function */ +#define C_NULL 0 +#define C_AUTO 1 /* automatic variable */ +#define C_EXT 2 /* external symbol */ +#define C_STAT 3 /* static */ +#define C_REG 4 /* register variable */ +#define C_EXTDEF 5 /* external definition */ +#define C_LABEL 6 /* label */ +#define C_ULABEL 7 /* undefined label */ +#define C_MOS 8 /* member of structure */ +#define C_ARG 9 /* function argument */ +#define C_STRTAG 10 /* structure tag */ +#define C_MOU 11 /* member of union */ +#define C_UNTAG 12 /* union tag */ +#define C_TPDEF 13 /* type definition */ +#define C_USTATIC 14 /* undefined static */ +#define C_ENTAG 15 /* enumeration tag */ +#define C_MOE 16 /* member of enumeration */ +#define C_REGPARM 17 /* register parameter */ +#define C_FIELD 18 /* bit field */ + +#define C_BLOCK 100 /* ".bb" or ".eb" */ +#define C_FCN 101 /* ".bf" or ".ef" */ +#define C_EOS 102 /* end of structure */ +#define C_FILE 103 /* file name */ +#define C_LINE 104 /* dummy sclass for line number entry */ +#define C_ALIAS 105 /* duplicate tag */ +#define C_HIDDEN 106 /* special storage class for external */ + /* symbols in dmert public libraries */ + +/*------------------------------------------------------------------------*/ +/* SYMBOL TABLE ENTRIES */ +/*------------------------------------------------------------------------*/ + +#define SYMNMLEN 8 /* Number of characters in a symbol name */ +#define FILNMLEN 14 /* Number of characters in a file name */ +#define DIMNUM 4 /* Number of array dimensions in auxiliary entry */ + + +struct syment +{ + union + { + char _n_name[SYMNMLEN]; /* old COFF version */ + struct + { + long _n_zeroes; /* new == 0 */ + long _n_offset; /* offset into string table */ + } _n_n; + char *_n_nptr[2]; /* allows for overlaying */ + } _n; + long n_value; /* value of symbol */ + short n_scnum; /* section number */ + unsigned short n_type; /* type and derived type */ + char n_sclass; /* storage class */ + char n_numaux; /* number of aux. entries */ +}; + +#define n_name _n._n_name +#define n_nptr _n._n_nptr[1] +#define n_zeroes _n._n_n._n_zeroes +#define n_offset _n._n_n._n_offset + +/*------------------------------------------------------------------------*/ +/* Relocatable symbols have a section number of the */ +/* section in which they are defined. Otherwise, section */ +/* numbers have the following meanings: */ +/*------------------------------------------------------------------------*/ +#define N_UNDEF 0 /* undefined symbol */ +#define N_ABS -1 /* value of symbol is absolute */ +#define N_DEBUG -2 /* special debugging symbol */ +#define N_TV (unsigned short)-3 /* needs transfer vector (preload) */ +#define P_TV (unsigned short)-4 /* needs transfer vector (postload) */ + + +/*------------------------------------------------------------------------*/ +/* The fundamental type of a symbol packed into the low */ +/* 4 bits of the word. */ +/*------------------------------------------------------------------------*/ +#define _EF ".ef" + +#define T_NULL 0 /* no type info */ +#define T_ARG 1 /* function argument (only used by compiler) */ +#define T_CHAR 2 /* character */ +#define T_SHORT 3 /* short integer */ +#define T_INT 4 /* integer */ +#define T_LONG 5 /* long integer */ +#define T_FLOAT 6 /* floating point */ +#define T_DOUBLE 7 /* double word */ +#define T_STRUCT 8 /* structure */ +#define T_UNION 9 /* union */ +#define T_ENUM 10 /* enumeration */ +#define T_MOE 11 /* member of enumeration */ +#define T_UCHAR 12 /* unsigned character */ +#define T_USHORT 13 /* unsigned short */ +#define T_UINT 14 /* unsigned integer */ +#define T_ULONG 15 /* unsigned long */ + +/*------------------------------------------------------------------------*/ +/* derived types are: */ +/*------------------------------------------------------------------------*/ +#define DT_NON 0 /* no derived type */ +#define DT_PTR 1 /* pointer */ +#define DT_FCN 2 /* function */ +#define DT_ARY 3 /* array */ + +#define MKTYPE(basic, d1,d2,d3,d4,d5,d6) \ + ((basic) | ((d1) << 4) | ((d2) << 6) | ((d3) << 8) |\ + ((d4) << 10) | ((d5) << 12) | ((d6) << 14)) + +/*------------------------------------------------------------------------*/ +/* type packing constants and macros */ +/*------------------------------------------------------------------------*/ +#define N_BTMASK_COFF 017 +#define N_TMASK_COFF 060 +#define N_TMASK1_COFF 0300 +#define N_TMASK2_COFF 0360 +#define N_BTSHFT_COFF 4 +#define N_TSHIFT_COFF 2 + +#define BTYPE_COFF(x) ((x) & N_BTMASK_COFF) +#define ISINT(x) (((x) >= T_CHAR && (x) <= T_LONG) || \ + ((x) >= T_UCHAR && (x) <= T_ULONG) || (x) == T_ENUM) +#define ISFLT_COFF(x) ((x) == T_DOUBLE || (x) == T_FLOAT) +#define ISPTR_COFF(x) (((x) & N_TMASK_COFF) == (DT_PTR << N_BTSHFT_COFF)) +#define ISFCN_COFF(x) (((x) & N_TMASK_COFF) == (DT_FCN << N_BTSHFT_COFF)) +#define ISARY_COFF(x) (((x) & N_TMASK_COFF) == (DT_ARY << N_BTSHFT_COFF)) +#define ISTAG_COFF(x) ((x)==C_STRTAG || (x)==C_UNTAG || (x)==C_ENTAG) + +#define INCREF_COFF(x) ((((x)&~N_BTMASK_COFF)<>N_TSHIFT_COFF)&~N_BTMASK_COFF)|((x)&N_BTMASK_COFF)) + + +/*------------------------------------------------------------------------*/ +/* AUXILIARY SYMBOL ENTRY */ +/*------------------------------------------------------------------------*/ +union auxent +{ + struct + { + long x_tagndx; /* str, un, or enum tag indx */ + union + { + struct + { + unsigned short x_lnno; /* declaration line number */ + unsigned short x_size; /* str, union, array size */ + } x_lnsz; + long x_fsize; /* size of function */ + } x_misc; + union + { + struct /* if ISFCN, tag, or .bb */ + { + long x_lnnoptr; /* ptr to fcn line # */ + long x_endndx; /* entry ndx past block end */ + } x_fcn; + struct /* if ISARY, up to 4 dimen. */ + { + unsigned short x_dimen[DIMNUM]; + } x_ary; + } x_fcnary; + unsigned short x_regcount; /* number of registers used by func */ + } x_sym; + struct + { + char x_fname[FILNMLEN]; + } x_file; + struct + { + long x_scnlen; /* section length */ + unsigned short x_nreloc; /* number of relocation entries */ + unsigned short x_nlinno; /* number of line numbers */ + } x_scn; +}; + +#define SYMENT struct syment +#define SYMESZ 18 /* sizeof(SYMENT) */ + +#define AUXENT union auxent +#define AUXESZ 18 /* sizeof(AUXENT) */ + +/*------------------------------------------------------------------------*/ +/* NAMES OF "SPECIAL" SYMBOLS */ +/*------------------------------------------------------------------------*/ +#define _STEXT ".text" +#define _ETEXT "etext" +#define _SDATA ".data" +#define _EDATA "edata" +#define _SBSS ".bss" +#define _END "end" +#define _CINITPTR "cinit" + +/*--------------------------------------------------------------------------*/ +/* ENTRY POINT SYMBOLS */ +/*--------------------------------------------------------------------------*/ +#define _START "_start" +#define _MAIN "_main" + /* _CSTART "_c_int00" (defined in params.h) */ + + +#define _TVORIG "_tvorig" +#define _TORIGIN "_torigin" +#define _DORIGIN "_dorigin" + +#define _SORIGIN "_sorigin" diff --git a/05/tcc-final/configure b/05/tcc-final/configure new file mode 100755 index 0000000..cd96ea3 --- /dev/null +++ b/05/tcc-final/configure @@ -0,0 +1,574 @@ +#!/bin/sh +# +# tcc configure script (c) 2003 Fabrice Bellard + +# set temporary file name +# if test ! -z "$TMPDIR" ; then +# TMPDIR1="${TMPDIR}" +# elif test ! -z "$TEMPDIR" ; then +# TMPDIR1="${TEMPDIR}" +# else +# TMPDIR1="/tmp" +# fi +# +# bashism: TMPN="${TMPDIR1}/tcc-conf-${RANDOM}-$$-${RANDOM}.c" + +TMPN="./conftest-$$" +TMPH=$TMPN.h + +# default parameters +prefix="" +execprefix="" +bindir="" +libdir="" +tccdir="" +includedir="" +mandir="" +infodir="" +sysroot="" +cross_prefix="" +test -z "$CC" && CC="gcc" +test -z "$cc" && cc="$CC" +ar="ar" +bigendian="no" +mingw32="no" +LIBSUF=".a" +EXESUF="" +DLLSUF=".so" +tcc_usrinclude="" +tcc_sysincludepaths="" +tcc_libpaths="" +tcc_crtprefix="" +tcc_elfinterp="" +triplet= +tcc_lddir= +confvars= +suggest="yes" +gcc_major=0 +gcc_minor=0 +cc_name="$cc" +ar_set= +darwin= +cpu= +cpuver= +dwarf= + +# OS specific +cpu_sys=`uname -m` +targetos=`uname` + +case $targetos in + Darwin) + darwin=yes + confvars="$confvars OSX dll=no" + cc=`which cc` + cc=`readlink $cc || echo clang` + tcc_usrinclude="`xcrun --show-sdk-path`/usr/include" + DLLSUF=".dylib" + ;; + Windows_NT|MINGW*|MSYS*|CYGWIN*) + mingw32=yes + targetos=WIN32 + ;; + DragonFly|OpenBSD|FreeBSD|NetBSD) + confvars="$confvars BSD ldl=no" + ;; + *) + ;; +esac + +# find source path +source_path=${0%configure} +source_path=${source_path%/} +source_path_used="yes" +if test -z "$source_path" -o "$source_path" = "." ; then + source_path=`pwd` + source_path_used="no" +fi + +for opt do + eval opt=\"$opt\" + case "$opt" in + --prefix=*) prefix=`echo $opt | cut -d '=' -f 2-` + ;; + --exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2-` + ;; + --tccdir=*) tccdir=`echo $opt | cut -d '=' -f 2-` + ;; + --bindir=*) bindir=`echo $opt | cut -d '=' -f 2-` + ;; + --libdir=*) libdir=`echo $opt | cut -d '=' -f 2-` + ;; + --includedir=*) includedir=`echo $opt | cut -d '=' -f 2-` + ;; + --sharedir=*) sharedir=`echo $opt | cut -d '=' -f 2-` + ;; + --mandir=*) mandir=`echo $opt | cut -d '=' -f 2-` + ;; + --infodir=*) infodir=`echo $opt | cut -d '=' -f 2-` + ;; + --docdir=*) docdir=`echo $opt | cut -d '=' -f 2-` + ;; + --sysroot=*) sysroot=`echo $opt | cut -d '=' -f 2-` + ;; + --source-path=*) source_path=`echo $opt | cut -d '=' -f 2-` + ;; + --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2-` + ;; + --cc=*) cc=`echo $opt | cut -d '=' -f 2-` + ;; + --ar=*) ar=`echo $opt | cut -d '=' -f 2-` ; ar_set="yes" + ;; + --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}" + ;; + --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}" + ;; + --extra-libs=*) extralibs="${opt#--extra-libs=}" + ;; + --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2-` + ;; + --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2-` + ;; + --crtprefix=*) tcc_crtprefix=`echo $opt | cut -d '=' -f 2-` + ;; + --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2-` + ;; + --triplet=*) triplet=`echo $opt | cut -d '=' -f 2-` + ;; + --cpu=*) cpu=`echo $opt | cut -d '=' -f 2-` + ;; + --dwarf=*) dwarf=`echo $opt | cut -d '=' -f 2-` + ;; + --enable-cross) confvars="$confvars cross" + ;; + --disable-static) confvars="$confvars static=no" + ;; + --enable-static) confvars="$confvars static" + ;; + --disable-rpath) confvars="$confvars rpath=no" + ;; + --debug) confvars="$confvars debug" + ;; + --with-libgcc) confvars="$confvars libgcc" + ;; + --with-selinux) confvars="$confvars selinux" + ;; + --config-mingw32*) mingw32=$(echo "$opt=yes" | cut -d '=' -f 2) + ;; + --config-*) confvars="$confvars ${opt#--config-}"; suggest="no" + ;; + --help|-h) show_help="yes" + ;; + *) echo "configure: WARNING: unrecognized option $opt" + ;; + esac +done + +cc="${cross_prefix}${cc}" +ar="${cross_prefix}${ar}" + +# Checking for CFLAGS +test -z "$CFLAGS" && CFLAGS="-Wall -O2" + +# on OSX M1 with --cpu=x86_64, build a tcc to run under rosetta entirely +if test "$darwin" = "yes" -a "$cpu" = "x86_64" -a "$cpu_sys" = "arm64"; then + CFLAGS="$CFLAGS -arch $cpu" + LDFLAGS="$LDFLAGS -arch $cpu" +fi + +# checking for cpu +test -z "$cpu" && cpu="$cpu_sys" + +case "$cpu" in + x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386) + cpu="i386" + ;; + x86_64|amd64|x86-64) + cpu="x86_64" + ;; + evbarm) + case "`uname -p`" in + aarch64|arm64) + cpu="arm64" + ;; + earmv*) + cpu="arm" + ;; + esac + ;; + aarch64|arm64|evbarm) + cpu="arm64" + ;; + arm*) + case "$cpu" in + arm|armv4l) + cpuver=4 + ;; + armv5tel|armv5tejl) + cpuver=5 + ;; + armv6j|armv6l) + cpuver=6 + ;; + armv7|armv7a|armv7l) + cpuver=7 + ;; + esac + cpu="arm" + ;; + alpha) + cpu="alpha" + ;; + "Power Macintosh"|ppc|ppc64) + cpu="ppc" + ;; + mips) + cpu="mips" + ;; + s390) + cpu="s390" + ;; + riscv64) + cpu="riscv64" + ;; + *) + echo "Unsupported CPU" + exit 1 + ;; +esac + +if test "$mingw32" = "yes" ; then + if test "$source_path_used" = "no"; then + source_path="." + fi + test -z "$prefix" && prefix="C:/Program Files/tcc" + test -z "$tccdir" && tccdir="${prefix}" && tccdir_auto="yes" + test -z "$bindir" && bindir="${tccdir}" + test -z "$docdir" && docdir="${tccdir}/doc" + test -z "$libdir" && libdir="${tccdir}/libtcc" + confvars="$confvars WIN32" + LIBSUF=".lib" + EXESUF=".exe" + DLLSUF=".dll" +else + if test -z "$prefix" ; then + prefix="/usr/local" + fi + if test -z "$sharedir" ; then + sharedir="${prefix}/share" + fi + if test x"$execprefix" = x""; then + execprefix="${prefix}" + fi + if test x"$libdir" = x""; then + libdir="${execprefix}/lib" + fi + if test x"$bindir" = x""; then + bindir="${execprefix}/bin" + fi + if test x"$docdir" = x""; then + docdir="${sharedir}/doc" + fi + if test x"$mandir" = x""; then + mandir="${sharedir}/man" + fi + if test x"$infodir" = x""; then + infodir="${sharedir}/info" + fi + if test x"$tccdir" = x""; then + tccdir="${libdir}/tcc" + fi + if test x"$includedir" = x""; then + includedir="${prefix}/include" + fi +fi # mingw32 + +if test x"$show_help" = "xyes" ; then +cat << EOF +Usage: configure [options] +Options: [defaults in brackets after descriptions] + +Standard options: + --help print this message + --prefix=PREFIX install in PREFIX [$prefix] + --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX + [same as prefix] + --bindir=DIR user executables in DIR [EPREFIX/bin] + --libdir=DIR object code libraries in DIR [EPREFIX/lib] + --tccdir=DIR installation directory [EPREFIX/lib/tcc] + --includedir=DIR C header files in DIR [PREFIX/include] + --sharedir=DIR documentation root DIR [PREFIX/share] + --docdir=DIR documentation in DIR [SHAREDIR/doc/tcc] + --mandir=DIR man documentation in DIR [SHAREDIR/man] + --infodir=DIR info documentation in DIR [SHAREDIR/info] + +Advanced options (experts only): + --source-path=PATH path of source code [$source_path] + --cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix] + --sysroot=PREFIX prepend PREFIX to library/include paths [$sysroot] + --cc=CC use C compiler CC [$cc] + --ar=AR create archives using AR [$ar] + --extra-cflags= specify compiler flags [$CFLAGS] + --extra-ldflags= specify linker options [$LDFLAGS] + --cpu=CPU CPU [$cpu] + + --debug include debug info with resulting binaries + --disable-static make libtcc.so instead of libtcc.a + --enable-static make libtcc.a instead of libtcc.dll (win32) + --disable-rpath disable use of -rpath with libtcc.so + --with-libgcc use libgcc_s.so.1 instead of libtcc1.a + --with-selinux use mmap for executable memory (tcc -run) + --enable-cross build cross compilers (see also 'make help') + + --sysincludepaths=... specify system include paths, colon separated + --libpaths=... specify system library paths, colon separated + --crtprefix=... specify locations of crt?.o, colon separated + --elfinterp=... specify elf interpreter + --triplet=... specify system library/include directory triplet + + --config-uClibc,-musl enable system specific configurations + --config-mingw32 build on windows using msys, busybox, etc. + --config-backtrace=no disable stack backtraces (with -run or -bt) + --config-bcheck=no disable bounds checker (-b) + --config-predefs=no do not compile tccdefs.h, instead just include + --dwarf=x Use dwarf debug info instead of stabs (x=2..5) +EOF +exit 1 +fi + +if test -z "$cross_prefix" ; then + CONFTEST=./conftest$EXESUF + if ! $cc -o $CONFTEST $source_path/conftest.c 2>/dev/null ; then + echo "configure: error: '$cc' failed to compile conftest.c." + else + cc_name="$($CONFTEST compiler)" + gcc_major="$($CONFTEST version)" + gcc_minor="$($CONFTEST minor)" + bigendian="$($CONFTEST bigendian)" + _triplet="$($CONFTEST triplet)" + fi + if test "$mingw32" = "no" ; then + + if test -z "$triplet"; then + if test -n "$_triplet" -a -f "/usr/lib/$_triplet/crti.o" ; then + triplet="$_triplet" + fi + fi + + if test -z "$triplet"; then + if test $cpu = "x86_64" -o $cpu = "arm64" -o $cpu = "riscv64" ; then + if test -f "/usr/lib64/crti.o" ; then + tcc_lddir="lib64" + fi + fi + fi + + if test "$cpu" = "arm" ; then + if test "${triplet%eabihf}" != "$triplet" ; then + confvars="$confvars arm_eabihf arm_vfp" + elif test "${triplet%eabi}" != "$triplet" ; then + confvars="$confvars arm_eabi arm_vfp" + elif test "${_triplet%eabihf}" != "$_triplet" ; then + confvars="$confvars arm_eabihf arm_vfp" + elif test "${_triplet%eabi}" != "$_triplet" ; then + confvars="$confvars arm_eabi arm_vfp" + fi + if grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then + confvars="$confvars arm_vfp" + fi + fi + + if test "$suggest" = "yes"; then + if test -f "/lib/ld-uClibc.so.0" ; then + echo "Perhaps you want ./configure --config-uClibc" + fi + if test -f "/lib/ld-musl-$cpu.so.1"; then + echo "Perhaps you want ./configure --config-musl" + fi + fi + else # mingw32 = yes + if test "$cc_name" = "gcc"; then + # avoid mingw dependencies such as 'libgcc_s_dw2-1.dll' + test -z "$LDFLAGS" && LDFLAGS="-static" + fi + fi +else + # if cross compiling, cannot launch a program, so make a static guess + case $cpu in + ppc|mips|s390) bigendian=yes;; + esac +fi + +if test "$bigendian" = "yes" ; then + confvars="$confvars BIGENDIAN" +fi + +# a final configuration tuning +if test "$cc_name" != "tcc"; then + OPT1="-Wdeclaration-after-statement -fno-strict-aliasing" + # we want -Wno- but gcc does not always reject unknown -Wno- options + OPT2="-Wpointer-sign -Wsign-compare -Wunused-result -Wformat-truncation" + OPT2="$OPT2 -Wstringop-truncation" + if test "$cc_name" = "clang"; then + OPT1="$OPT1 -fheinous-gnu-extensions" + OPT2="$OPT2 -Wstring-plus-int" + fi + $cc $OPT1 $OPT2 -o a.out -c -xc - < /dev/null > cc_msg.txt 2>&1 + for o in $OPT1; do # enable these options + if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS $o"; fi + done + for o in $OPT2; do # disable these options + if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS -Wno-${o#-W*}"; fi + done + # cat cc_msg.txt + # echo $CFLAGS + rm -f cc_msg.txt a.out +else # cc is tcc + test "$ar_set" || ar="$cc -ar" +fi + +fcho() { if test -n "$2"; then echo "$1$2"; fi } + +fcho "Binary directory " "$bindir" +fcho "TinyCC directory " "$tccdir" +fcho "Library directory " "$libdir" +fcho "Include directory " "$includedir" +fcho "Manual directory " "$mandir" +fcho "Info directory " "$infodir" +fcho "Doc directory " "$docdir" +fcho "Target root prefix " "$sysroot" +fcho "/usr/include dir " "$tcc_usrinclude" +echo "Source path $source_path" +echo "C compiler $cc ($gcc_major.$gcc_minor)" +echo "Target OS $targetos" +echo "CPU $cpu" +fcho "Triplet " "$triplet" +fcho "Config " "${confvars# }" +echo "Creating config.mak and config.h" + +cat >config.mak <> $TMPH + echo "# define $1 $v" >> $TMPH + echo "#endif" >> $TMPH + fi +} + +print_mak() { + local v="$2" + if test -n "$v"; then + test "$3" = "num" || v="\"\\\"$v\\\"\"" + echo "NATIVE_DEFINES+=-D$1=$v" >> config.mak + fi +} + +echo "/* Automatically generated by configure - do not modify */" > $TMPH + +print_inc CONFIG_SYSROOT "$sysroot" +test "$tccdir_auto" = "yes" || print_inc CONFIG_TCCDIR "$tccdir" +print_inc DWARF_VERSION "$dwarf" num +print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude" +print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths" +print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths" +print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix" +print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp" +print_mak CONFIG_LDDIR "$tcc_lddir" +print_mak CONFIG_TRIPLET "$triplet" +print_mak TCC_CPU_VERSION "$cpuver" num + +echo "ARCH=$cpu" >> config.mak +echo "TARGETOS=$targetos" >> config.mak + +predefs="1" +for v in $confvars ; do + test "$v" = "predefs=no" && predefs="" + if test "${v%=*}" = "$v"; then + echo "CONFIG_$v=yes" >> config.mak + else + echo "CONFIG_$v" >> config.mak + fi +done +print_inc CONFIG_TCC_PREDEFS "$predefs" num + +version=`head $source_path/VERSION` +echo "VERSION = $version" >> config.mak +echo "#define TCC_VERSION \"$version\"" >> $TMPH +echo "@set VERSION $version" > config.texi + +if test "$source_path_used" = "yes" ; then + case $source_path in + /*) echo "TOPSRC=$source_path";; + *) echo "TOPSRC=\$(TOP)/$source_path";; + esac >>config.mak +else + echo 'TOPSRC=$(TOP)' >>config.mak +fi +cat >>$TMPH </dev/null 2>&1 +if test $? -ne 0 ; then + mv -f $TMPH config.h +else + echo "config.h is unchanged" +fi + +rm -f $TMPN* $CONFTEST + +# --------------------------------------------------------------------------- +# build tree in object directory if source path is different from current one + +fn_makelink() +{ + tgt=$1/$2 + case $2 in + */*) dn=${2%/*} + test -d $dn || mkdir -p $dn + case $1 in + /*) ;; + *) while test $dn ; do + tgt=../$tgt; dn=${dn#${dn%%/*}}; dn=${dn#/} + done + ;; + esac + ;; + esac + + ln -sfn $tgt $2 || ( echo "ln failed. Using cp instead."; cp -f $1/$2 $2 ) +} + +if test "$source_path_used" = "yes" ; then + FILES="Makefile lib/Makefile tests/Makefile tests/tests2/Makefile tests/pp/Makefile" + for f in $FILES ; do + fn_makelink $source_path $f + done +fi + +# --------------------------------------------------------------------------- diff --git a/05/tcc-final/dwarf.h b/05/tcc-final/dwarf.h new file mode 100644 index 0000000..c961bc3 --- /dev/null +++ b/05/tcc-final/dwarf.h @@ -0,0 +1,1046 @@ +/* This file defines standard DWARF types, structures, and macros. + Copyright (C) 2000-2011, 2014, 2016, 2017, 2018 Red Hat, Inc. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifndef _DWARF_H +#define _DWARF_H 1 + +/* DWARF Unit Header Types. */ +enum + { + DW_UT_compile = 0x01, + DW_UT_type = 0x02, + DW_UT_partial = 0x03, + DW_UT_skeleton = 0x04, + DW_UT_split_compile = 0x05, + DW_UT_split_type = 0x06, + + DW_UT_lo_user = 0x80, + DW_UT_hi_user = 0xff + }; + +/* DWARF tags. */ +enum + { + DW_TAG_array_type = 0x01, + DW_TAG_class_type = 0x02, + DW_TAG_entry_point = 0x03, + DW_TAG_enumeration_type = 0x04, + DW_TAG_formal_parameter = 0x05, + /* 0x06 reserved. */ + /* 0x07 reserved. */ + DW_TAG_imported_declaration = 0x08, + /* 0x09 reserved. */ + DW_TAG_label = 0x0a, + DW_TAG_lexical_block = 0x0b, + /* 0x0c reserved. */ + DW_TAG_member = 0x0d, + /* 0x0e reserved. */ + DW_TAG_pointer_type = 0x0f, + DW_TAG_reference_type = 0x10, + DW_TAG_compile_unit = 0x11, + DW_TAG_string_type = 0x12, + DW_TAG_structure_type = 0x13, + /* 0x14 reserved. */ + DW_TAG_subroutine_type = 0x15, + DW_TAG_typedef = 0x16, + DW_TAG_union_type = 0x17, + DW_TAG_unspecified_parameters = 0x18, + DW_TAG_variant = 0x19, + DW_TAG_common_block = 0x1a, + DW_TAG_common_inclusion = 0x1b, + DW_TAG_inheritance = 0x1c, + DW_TAG_inlined_subroutine = 0x1d, + DW_TAG_module = 0x1e, + DW_TAG_ptr_to_member_type = 0x1f, + DW_TAG_set_type = 0x20, + DW_TAG_subrange_type = 0x21, + DW_TAG_with_stmt = 0x22, + DW_TAG_access_declaration = 0x23, + DW_TAG_base_type = 0x24, + DW_TAG_catch_block = 0x25, + DW_TAG_const_type = 0x26, + DW_TAG_constant = 0x27, + DW_TAG_enumerator = 0x28, + DW_TAG_file_type = 0x29, + DW_TAG_friend = 0x2a, + DW_TAG_namelist = 0x2b, + DW_TAG_namelist_item = 0x2c, + DW_TAG_packed_type = 0x2d, + DW_TAG_subprogram = 0x2e, + DW_TAG_template_type_parameter = 0x2f, + DW_TAG_template_value_parameter = 0x30, + DW_TAG_thrown_type = 0x31, + DW_TAG_try_block = 0x32, + DW_TAG_variant_part = 0x33, + DW_TAG_variable = 0x34, + DW_TAG_volatile_type = 0x35, + DW_TAG_dwarf_procedure = 0x36, + DW_TAG_restrict_type = 0x37, + DW_TAG_interface_type = 0x38, + DW_TAG_namespace = 0x39, + DW_TAG_imported_module = 0x3a, + DW_TAG_unspecified_type = 0x3b, + DW_TAG_partial_unit = 0x3c, + DW_TAG_imported_unit = 0x3d, + /* 0x3e reserved. Was DW_TAG_mutable_type. */ + DW_TAG_condition = 0x3f, + DW_TAG_shared_type = 0x40, + DW_TAG_type_unit = 0x41, + DW_TAG_rvalue_reference_type = 0x42, + DW_TAG_template_alias = 0x43, + DW_TAG_coarray_type = 0x44, + DW_TAG_generic_subrange = 0x45, + DW_TAG_dynamic_type = 0x46, + DW_TAG_atomic_type = 0x47, + DW_TAG_call_site = 0x48, + DW_TAG_call_site_parameter = 0x49, + DW_TAG_skeleton_unit = 0x4a, + DW_TAG_immutable_type = 0x4b, + + DW_TAG_lo_user = 0x4080, + + DW_TAG_MIPS_loop = 0x4081, + DW_TAG_format_label = 0x4101, + DW_TAG_function_template = 0x4102, + DW_TAG_class_template = 0x4103, + + DW_TAG_GNU_BINCL = 0x4104, + DW_TAG_GNU_EINCL = 0x4105, + + DW_TAG_GNU_template_template_param = 0x4106, + DW_TAG_GNU_template_parameter_pack = 0x4107, + DW_TAG_GNU_formal_parameter_pack = 0x4108, + DW_TAG_GNU_call_site = 0x4109, + DW_TAG_GNU_call_site_parameter = 0x410a, + + DW_TAG_hi_user = 0xffff + }; + + +/* Children determination encodings. */ +enum + { + DW_CHILDREN_no = 0, + DW_CHILDREN_yes = 1 + }; + + +/* DWARF attributes encodings. */ +enum + { + DW_AT_sibling = 0x01, + DW_AT_location = 0x02, + DW_AT_name = 0x03, + /* 0x04 reserved. */ + /* 0x05 reserved. */ + /* 0x06 reserved. */ + /* 0x07 reserved. */ + /* 0x08 reserved. */ + DW_AT_ordering = 0x09, + /* 0x0a reserved. */ + DW_AT_byte_size = 0x0b, + DW_AT_bit_offset = 0x0c, /* Deprecated in DWARF4. */ + DW_AT_bit_size = 0x0d, + /* 0x0e reserved. */ + /* 0x0f reserved. */ + DW_AT_stmt_list = 0x10, + DW_AT_low_pc = 0x11, + DW_AT_high_pc = 0x12, + DW_AT_language = 0x13, + /* 0x14 reserved. */ + DW_AT_discr = 0x15, + DW_AT_discr_value = 0x16, + DW_AT_visibility = 0x17, + DW_AT_import = 0x18, + DW_AT_string_length = 0x19, + DW_AT_common_reference = 0x1a, + DW_AT_comp_dir = 0x1b, + DW_AT_const_value = 0x1c, + DW_AT_containing_type = 0x1d, + DW_AT_default_value = 0x1e, + /* 0x1f reserved. */ + DW_AT_inline = 0x20, + DW_AT_is_optional = 0x21, + DW_AT_lower_bound = 0x22, + /* 0x23 reserved. */ + /* 0x24 reserved. */ + DW_AT_producer = 0x25, + /* 0x26 reserved. */ + DW_AT_prototyped = 0x27, + /* 0x28 reserved. */ + /* 0x29 reserved. */ + DW_AT_return_addr = 0x2a, + /* 0x2b reserved. */ + DW_AT_start_scope = 0x2c, + /* 0x2d reserved. */ + DW_AT_bit_stride = 0x2e, + DW_AT_upper_bound = 0x2f, + /* 0x30 reserved. */ + DW_AT_abstract_origin = 0x31, + DW_AT_accessibility = 0x32, + DW_AT_address_class = 0x33, + DW_AT_artificial = 0x34, + DW_AT_base_types = 0x35, + DW_AT_calling_convention = 0x36, + DW_AT_count = 0x37, + DW_AT_data_member_location = 0x38, + DW_AT_decl_column = 0x39, + DW_AT_decl_file = 0x3a, + DW_AT_decl_line = 0x3b, + DW_AT_declaration = 0x3c, + DW_AT_discr_list = 0x3d, + DW_AT_encoding = 0x3e, + DW_AT_external = 0x3f, + DW_AT_frame_base = 0x40, + DW_AT_friend = 0x41, + DW_AT_identifier_case = 0x42, + DW_AT_macro_info = 0x43, /* Deprecated in DWARF5. */ + DW_AT_namelist_item = 0x44, + DW_AT_priority = 0x45, + DW_AT_segment = 0x46, + DW_AT_specification = 0x47, + DW_AT_static_link = 0x48, + DW_AT_type = 0x49, + DW_AT_use_location = 0x4a, + DW_AT_variable_parameter = 0x4b, + DW_AT_virtuality = 0x4c, + DW_AT_vtable_elem_location = 0x4d, + DW_AT_allocated = 0x4e, + DW_AT_associated = 0x4f, + DW_AT_data_location = 0x50, + DW_AT_byte_stride = 0x51, + DW_AT_entry_pc = 0x52, + DW_AT_use_UTF8 = 0x53, + DW_AT_extension = 0x54, + DW_AT_ranges = 0x55, + DW_AT_trampoline = 0x56, + DW_AT_call_column = 0x57, + DW_AT_call_file = 0x58, + DW_AT_call_line = 0x59, + DW_AT_description = 0x5a, + DW_AT_binary_scale = 0x5b, + DW_AT_decimal_scale = 0x5c, + DW_AT_small = 0x5d, + DW_AT_decimal_sign = 0x5e, + DW_AT_digit_count = 0x5f, + DW_AT_picture_string = 0x60, + DW_AT_mutable = 0x61, + DW_AT_threads_scaled = 0x62, + DW_AT_explicit = 0x63, + DW_AT_object_pointer = 0x64, + DW_AT_endianity = 0x65, + DW_AT_elemental = 0x66, + DW_AT_pure = 0x67, + DW_AT_recursive = 0x68, + DW_AT_signature = 0x69, + DW_AT_main_subprogram = 0x6a, + DW_AT_data_bit_offset = 0x6b, + DW_AT_const_expr = 0x6c, + DW_AT_enum_class = 0x6d, + DW_AT_linkage_name = 0x6e, + DW_AT_string_length_bit_size = 0x6f, + DW_AT_string_length_byte_size = 0x70, + DW_AT_rank = 0x71, + DW_AT_str_offsets_base = 0x72, + DW_AT_addr_base = 0x73, + DW_AT_rnglists_base = 0x74, + /* 0x75 reserved. */ + DW_AT_dwo_name = 0x76, + DW_AT_reference = 0x77, + DW_AT_rvalue_reference = 0x78, + DW_AT_macros = 0x79, + DW_AT_call_all_calls = 0x7a, + DW_AT_call_all_source_calls = 0x7b, + DW_AT_call_all_tail_calls = 0x7c, + DW_AT_call_return_pc = 0x7d, + DW_AT_call_value = 0x7e, + DW_AT_call_origin = 0x7f, + DW_AT_call_parameter = 0x80, + DW_AT_call_pc = 0x81, + DW_AT_call_tail_call = 0x82, + DW_AT_call_target = 0x83, + DW_AT_call_target_clobbered = 0x84, + DW_AT_call_data_location = 0x85, + DW_AT_call_data_value = 0x86, + DW_AT_noreturn = 0x87, + DW_AT_alignment = 0x88, + DW_AT_export_symbols = 0x89, + DW_AT_deleted = 0x8a, + DW_AT_defaulted = 0x8b, + DW_AT_loclists_base = 0x8c, + + DW_AT_lo_user = 0x2000, + + DW_AT_MIPS_fde = 0x2001, + DW_AT_MIPS_loop_begin = 0x2002, + DW_AT_MIPS_tail_loop_begin = 0x2003, + DW_AT_MIPS_epilog_begin = 0x2004, + DW_AT_MIPS_loop_unroll_factor = 0x2005, + DW_AT_MIPS_software_pipeline_depth = 0x2006, + DW_AT_MIPS_linkage_name = 0x2007, + DW_AT_MIPS_stride = 0x2008, + DW_AT_MIPS_abstract_name = 0x2009, + DW_AT_MIPS_clone_origin = 0x200a, + DW_AT_MIPS_has_inlines = 0x200b, + DW_AT_MIPS_stride_byte = 0x200c, + DW_AT_MIPS_stride_elem = 0x200d, + DW_AT_MIPS_ptr_dopetype = 0x200e, + DW_AT_MIPS_allocatable_dopetype = 0x200f, + DW_AT_MIPS_assumed_shape_dopetype = 0x2010, + DW_AT_MIPS_assumed_size = 0x2011, + + /* GNU extensions. */ + DW_AT_sf_names = 0x2101, + DW_AT_src_info = 0x2102, + DW_AT_mac_info = 0x2103, + DW_AT_src_coords = 0x2104, + DW_AT_body_begin = 0x2105, + DW_AT_body_end = 0x2106, + DW_AT_GNU_vector = 0x2107, + DW_AT_GNU_guarded_by = 0x2108, + DW_AT_GNU_pt_guarded_by = 0x2109, + DW_AT_GNU_guarded = 0x210a, + DW_AT_GNU_pt_guarded = 0x210b, + DW_AT_GNU_locks_excluded = 0x210c, + DW_AT_GNU_exclusive_locks_required = 0x210d, + DW_AT_GNU_shared_locks_required = 0x210e, + DW_AT_GNU_odr_signature = 0x210f, + DW_AT_GNU_template_name = 0x2110, + DW_AT_GNU_call_site_value = 0x2111, + DW_AT_GNU_call_site_data_value = 0x2112, + DW_AT_GNU_call_site_target = 0x2113, + DW_AT_GNU_call_site_target_clobbered = 0x2114, + DW_AT_GNU_tail_call = 0x2115, + DW_AT_GNU_all_tail_call_sites = 0x2116, + DW_AT_GNU_all_call_sites = 0x2117, + DW_AT_GNU_all_source_call_sites = 0x2118, + DW_AT_GNU_locviews = 0x2137, + DW_AT_GNU_entry_view = 0x2138, + DW_AT_GNU_macros = 0x2119, + DW_AT_GNU_deleted = 0x211a, + /* GNU Debug Fission extensions. */ + DW_AT_GNU_dwo_name = 0x2130, + DW_AT_GNU_dwo_id = 0x2131, + DW_AT_GNU_ranges_base = 0x2132, + DW_AT_GNU_addr_base = 0x2133, + DW_AT_GNU_pubnames = 0x2134, + DW_AT_GNU_pubtypes = 0x2135, + + /* https://gcc.gnu.org/wiki/DW_AT_GNU_numerator_denominator */ + DW_AT_GNU_numerator = 0x2303, + DW_AT_GNU_denominator = 0x2304, + /* https://gcc.gnu.org/wiki/DW_AT_GNU_bias */ + DW_AT_GNU_bias = 0x2305, + + DW_AT_hi_user = 0x3fff + }; + +/* Old unofficially attribute names. Should not be used. + Will not appear in known-dwarf.h */ + +/* DWARF1 array subscripts and element data types. */ +#define DW_AT_subscr_data 0x0a +/* DWARF1 enumeration literals. */ +#define DW_AT_element_list 0x0f +/* DWARF1 reference for variable to member structure, class or union. */ +#define DW_AT_member 0x14 + +/* DWARF form encodings. */ +enum + { + DW_FORM_addr = 0x01, + DW_FORM_block2 = 0x03, + DW_FORM_block4 = 0x04, + DW_FORM_data2 = 0x05, + DW_FORM_data4 = 0x06, + DW_FORM_data8 = 0x07, + DW_FORM_string = 0x08, + DW_FORM_block = 0x09, + DW_FORM_block1 = 0x0a, + DW_FORM_data1 = 0x0b, + DW_FORM_flag = 0x0c, + DW_FORM_sdata = 0x0d, + DW_FORM_strp = 0x0e, + DW_FORM_udata = 0x0f, + DW_FORM_ref_addr = 0x10, + DW_FORM_ref1 = 0x11, + DW_FORM_ref2 = 0x12, + DW_FORM_ref4 = 0x13, + DW_FORM_ref8 = 0x14, + DW_FORM_ref_udata = 0x15, + DW_FORM_indirect = 0x16, + DW_FORM_sec_offset = 0x17, + DW_FORM_exprloc = 0x18, + DW_FORM_flag_present = 0x19, + DW_FORM_strx = 0x1a, + DW_FORM_addrx = 0x1b, + DW_FORM_ref_sup4 = 0x1c, + DW_FORM_strp_sup = 0x1d, + DW_FORM_data16 = 0x1e, + DW_FORM_line_strp = 0x1f, + DW_FORM_ref_sig8 = 0x20, + DW_FORM_implicit_const = 0x21, + DW_FORM_loclistx = 0x22, + DW_FORM_rnglistx = 0x23, + DW_FORM_ref_sup8 = 0x24, + DW_FORM_strx1 = 0x25, + DW_FORM_strx2 = 0x26, + DW_FORM_strx3 = 0x27, + DW_FORM_strx4 = 0x28, + DW_FORM_addrx1 = 0x29, + DW_FORM_addrx2 = 0x2a, + DW_FORM_addrx3 = 0x2b, + DW_FORM_addrx4 = 0x2c, + + /* GNU Debug Fission extensions. */ + DW_FORM_GNU_addr_index = 0x1f01, + DW_FORM_GNU_str_index = 0x1f02, + + DW_FORM_GNU_ref_alt = 0x1f20, /* offset in alternate .debuginfo. */ + DW_FORM_GNU_strp_alt = 0x1f21 /* offset in alternate .debug_str. */ + }; + + +/* DWARF location operation encodings. */ +enum + { + DW_OP_addr = 0x03, /* Constant address. */ + DW_OP_deref = 0x06, + DW_OP_const1u = 0x08, /* Unsigned 1-byte constant. */ + DW_OP_const1s = 0x09, /* Signed 1-byte constant. */ + DW_OP_const2u = 0x0a, /* Unsigned 2-byte constant. */ + DW_OP_const2s = 0x0b, /* Signed 2-byte constant. */ + DW_OP_const4u = 0x0c, /* Unsigned 4-byte constant. */ + DW_OP_const4s = 0x0d, /* Signed 4-byte constant. */ + DW_OP_const8u = 0x0e, /* Unsigned 8-byte constant. */ + DW_OP_const8s = 0x0f, /* Signed 8-byte constant. */ + DW_OP_constu = 0x10, /* Unsigned LEB128 constant. */ + DW_OP_consts = 0x11, /* Signed LEB128 constant. */ + DW_OP_dup = 0x12, + DW_OP_drop = 0x13, + DW_OP_over = 0x14, + DW_OP_pick = 0x15, /* 1-byte stack index. */ + DW_OP_swap = 0x16, + DW_OP_rot = 0x17, + DW_OP_xderef = 0x18, + DW_OP_abs = 0x19, + DW_OP_and = 0x1a, + DW_OP_div = 0x1b, + DW_OP_minus = 0x1c, + DW_OP_mod = 0x1d, + DW_OP_mul = 0x1e, + DW_OP_neg = 0x1f, + DW_OP_not = 0x20, + DW_OP_or = 0x21, + DW_OP_plus = 0x22, + DW_OP_plus_uconst = 0x23, /* Unsigned LEB128 addend. */ + DW_OP_shl = 0x24, + DW_OP_shr = 0x25, + DW_OP_shra = 0x26, + DW_OP_xor = 0x27, + DW_OP_bra = 0x28, /* Signed 2-byte constant. */ + DW_OP_eq = 0x29, + DW_OP_ge = 0x2a, + DW_OP_gt = 0x2b, + DW_OP_le = 0x2c, + DW_OP_lt = 0x2d, + DW_OP_ne = 0x2e, + DW_OP_skip = 0x2f, /* Signed 2-byte constant. */ + DW_OP_lit0 = 0x30, /* Literal 0. */ + DW_OP_lit1 = 0x31, /* Literal 1. */ + DW_OP_lit2 = 0x32, /* Literal 2. */ + DW_OP_lit3 = 0x33, /* Literal 3. */ + DW_OP_lit4 = 0x34, /* Literal 4. */ + DW_OP_lit5 = 0x35, /* Literal 5. */ + DW_OP_lit6 = 0x36, /* Literal 6. */ + DW_OP_lit7 = 0x37, /* Literal 7. */ + DW_OP_lit8 = 0x38, /* Literal 8. */ + DW_OP_lit9 = 0x39, /* Literal 9. */ + DW_OP_lit10 = 0x3a, /* Literal 10. */ + DW_OP_lit11 = 0x3b, /* Literal 11. */ + DW_OP_lit12 = 0x3c, /* Literal 12. */ + DW_OP_lit13 = 0x3d, /* Literal 13. */ + DW_OP_lit14 = 0x3e, /* Literal 14. */ + DW_OP_lit15 = 0x3f, /* Literal 15. */ + DW_OP_lit16 = 0x40, /* Literal 16. */ + DW_OP_lit17 = 0x41, /* Literal 17. */ + DW_OP_lit18 = 0x42, /* Literal 18. */ + DW_OP_lit19 = 0x43, /* Literal 19. */ + DW_OP_lit20 = 0x44, /* Literal 20. */ + DW_OP_lit21 = 0x45, /* Literal 21. */ + DW_OP_lit22 = 0x46, /* Literal 22. */ + DW_OP_lit23 = 0x47, /* Literal 23. */ + DW_OP_lit24 = 0x48, /* Literal 24. */ + DW_OP_lit25 = 0x49, /* Literal 25. */ + DW_OP_lit26 = 0x4a, /* Literal 26. */ + DW_OP_lit27 = 0x4b, /* Literal 27. */ + DW_OP_lit28 = 0x4c, /* Literal 28. */ + DW_OP_lit29 = 0x4d, /* Literal 29. */ + DW_OP_lit30 = 0x4e, /* Literal 30. */ + DW_OP_lit31 = 0x4f, /* Literal 31. */ + DW_OP_reg0 = 0x50, /* Register 0. */ + DW_OP_reg1 = 0x51, /* Register 1. */ + DW_OP_reg2 = 0x52, /* Register 2. */ + DW_OP_reg3 = 0x53, /* Register 3. */ + DW_OP_reg4 = 0x54, /* Register 4. */ + DW_OP_reg5 = 0x55, /* Register 5. */ + DW_OP_reg6 = 0x56, /* Register 6. */ + DW_OP_reg7 = 0x57, /* Register 7. */ + DW_OP_reg8 = 0x58, /* Register 8. */ + DW_OP_reg9 = 0x59, /* Register 9. */ + DW_OP_reg10 = 0x5a, /* Register 10. */ + DW_OP_reg11 = 0x5b, /* Register 11. */ + DW_OP_reg12 = 0x5c, /* Register 12. */ + DW_OP_reg13 = 0x5d, /* Register 13. */ + DW_OP_reg14 = 0x5e, /* Register 14. */ + DW_OP_reg15 = 0x5f, /* Register 15. */ + DW_OP_reg16 = 0x60, /* Register 16. */ + DW_OP_reg17 = 0x61, /* Register 17. */ + DW_OP_reg18 = 0x62, /* Register 18. */ + DW_OP_reg19 = 0x63, /* Register 19. */ + DW_OP_reg20 = 0x64, /* Register 20. */ + DW_OP_reg21 = 0x65, /* Register 21. */ + DW_OP_reg22 = 0x66, /* Register 22. */ + DW_OP_reg23 = 0x67, /* Register 24. */ + DW_OP_reg24 = 0x68, /* Register 24. */ + DW_OP_reg25 = 0x69, /* Register 25. */ + DW_OP_reg26 = 0x6a, /* Register 26. */ + DW_OP_reg27 = 0x6b, /* Register 27. */ + DW_OP_reg28 = 0x6c, /* Register 28. */ + DW_OP_reg29 = 0x6d, /* Register 29. */ + DW_OP_reg30 = 0x6e, /* Register 30. */ + DW_OP_reg31 = 0x6f, /* Register 31. */ + DW_OP_breg0 = 0x70, /* Base register 0. */ + DW_OP_breg1 = 0x71, /* Base register 1. */ + DW_OP_breg2 = 0x72, /* Base register 2. */ + DW_OP_breg3 = 0x73, /* Base register 3. */ + DW_OP_breg4 = 0x74, /* Base register 4. */ + DW_OP_breg5 = 0x75, /* Base register 5. */ + DW_OP_breg6 = 0x76, /* Base register 6. */ + DW_OP_breg7 = 0x77, /* Base register 7. */ + DW_OP_breg8 = 0x78, /* Base register 8. */ + DW_OP_breg9 = 0x79, /* Base register 9. */ + DW_OP_breg10 = 0x7a, /* Base register 10. */ + DW_OP_breg11 = 0x7b, /* Base register 11. */ + DW_OP_breg12 = 0x7c, /* Base register 12. */ + DW_OP_breg13 = 0x7d, /* Base register 13. */ + DW_OP_breg14 = 0x7e, /* Base register 14. */ + DW_OP_breg15 = 0x7f, /* Base register 15. */ + DW_OP_breg16 = 0x80, /* Base register 16. */ + DW_OP_breg17 = 0x81, /* Base register 17. */ + DW_OP_breg18 = 0x82, /* Base register 18. */ + DW_OP_breg19 = 0x83, /* Base register 19. */ + DW_OP_breg20 = 0x84, /* Base register 20. */ + DW_OP_breg21 = 0x85, /* Base register 21. */ + DW_OP_breg22 = 0x86, /* Base register 22. */ + DW_OP_breg23 = 0x87, /* Base register 23. */ + DW_OP_breg24 = 0x88, /* Base register 24. */ + DW_OP_breg25 = 0x89, /* Base register 25. */ + DW_OP_breg26 = 0x8a, /* Base register 26. */ + DW_OP_breg27 = 0x8b, /* Base register 27. */ + DW_OP_breg28 = 0x8c, /* Base register 28. */ + DW_OP_breg29 = 0x8d, /* Base register 29. */ + DW_OP_breg30 = 0x8e, /* Base register 30. */ + DW_OP_breg31 = 0x8f, /* Base register 31. */ + DW_OP_regx = 0x90, /* Unsigned LEB128 register. */ + DW_OP_fbreg = 0x91, /* Signed LEB128 offset. */ + DW_OP_bregx = 0x92, /* ULEB128 register followed by SLEB128 off. */ + DW_OP_piece = 0x93, /* ULEB128 size of piece addressed. */ + DW_OP_deref_size = 0x94, /* 1-byte size of data retrieved. */ + DW_OP_xderef_size = 0x95, /* 1-byte size of data retrieved. */ + DW_OP_nop = 0x96, + DW_OP_push_object_address = 0x97, + DW_OP_call2 = 0x98, + DW_OP_call4 = 0x99, + DW_OP_call_ref = 0x9a, + DW_OP_form_tls_address = 0x9b,/* TLS offset to address in current thread */ + DW_OP_call_frame_cfa = 0x9c,/* CFA as determined by CFI. */ + DW_OP_bit_piece = 0x9d, /* ULEB128 size and ULEB128 offset in bits. */ + DW_OP_implicit_value = 0x9e, /* DW_FORM_block follows opcode. */ + DW_OP_stack_value = 0x9f, /* No operands, special like DW_OP_piece. */ + + DW_OP_implicit_pointer = 0xa0, + DW_OP_addrx = 0xa1, + DW_OP_constx = 0xa2, + DW_OP_entry_value = 0xa3, + DW_OP_const_type = 0xa4, + DW_OP_regval_type = 0xa5, + DW_OP_deref_type = 0xa6, + DW_OP_xderef_type = 0xa7, + DW_OP_convert = 0xa8, + DW_OP_reinterpret = 0xa9, + + /* GNU extensions. */ + DW_OP_GNU_push_tls_address = 0xe0, + DW_OP_GNU_uninit = 0xf0, + DW_OP_GNU_encoded_addr = 0xf1, + DW_OP_GNU_implicit_pointer = 0xf2, + DW_OP_GNU_entry_value = 0xf3, + DW_OP_GNU_const_type = 0xf4, + DW_OP_GNU_regval_type = 0xf5, + DW_OP_GNU_deref_type = 0xf6, + DW_OP_GNU_convert = 0xf7, + DW_OP_GNU_reinterpret = 0xf9, + DW_OP_GNU_parameter_ref = 0xfa, + + /* GNU Debug Fission extensions. */ + DW_OP_GNU_addr_index = 0xfb, + DW_OP_GNU_const_index = 0xfc, + + DW_OP_GNU_variable_value = 0xfd, + + DW_OP_lo_user = 0xe0, /* Implementation-defined range start. */ + DW_OP_hi_user = 0xff /* Implementation-defined range end. */ + }; + + +/* DWARF base type encodings. */ +enum + { + DW_ATE_void = 0x0, + DW_ATE_address = 0x1, + DW_ATE_boolean = 0x2, + DW_ATE_complex_float = 0x3, + DW_ATE_float = 0x4, + DW_ATE_signed = 0x5, + DW_ATE_signed_char = 0x6, + DW_ATE_unsigned = 0x7, + DW_ATE_unsigned_char = 0x8, + DW_ATE_imaginary_float = 0x9, + DW_ATE_packed_decimal = 0xa, + DW_ATE_numeric_string = 0xb, + DW_ATE_edited = 0xc, + DW_ATE_signed_fixed = 0xd, + DW_ATE_unsigned_fixed = 0xe, + DW_ATE_decimal_float = 0xf, + DW_ATE_UTF = 0x10, + DW_ATE_UCS = 0x11, + DW_ATE_ASCII = 0x12, + + DW_ATE_lo_user = 0x80, + DW_ATE_hi_user = 0xff + }; + + +/* DWARF decimal sign encodings. */ +enum + { + DW_DS_unsigned = 1, + DW_DS_leading_overpunch = 2, + DW_DS_trailing_overpunch = 3, + DW_DS_leading_separate = 4, + DW_DS_trailing_separate = 5, + }; + + +/* DWARF endianity encodings. */ +enum + { + DW_END_default = 0, + DW_END_big = 1, + DW_END_little = 2, + + DW_END_lo_user = 0x40, + DW_END_hi_user = 0xff + }; + + +/* DWARF accessibility encodings. */ +enum + { + DW_ACCESS_public = 1, + DW_ACCESS_protected = 2, + DW_ACCESS_private = 3 + }; + + +/* DWARF visibility encodings. */ +enum + { + DW_VIS_local = 1, + DW_VIS_exported = 2, + DW_VIS_qualified = 3 + }; + + +/* DWARF virtuality encodings. */ +enum + { + DW_VIRTUALITY_none = 0, + DW_VIRTUALITY_virtual = 1, + DW_VIRTUALITY_pure_virtual = 2 + }; + + +/* DWARF language encodings. */ +enum + { + DW_LANG_C89 = 0x0001, /* ISO C:1989 */ + DW_LANG_C = 0x0002, /* C */ + DW_LANG_Ada83 = 0x0003, /* ISO Ada:1983 */ + DW_LANG_C_plus_plus = 0x0004, /* ISO C++:1998 */ + DW_LANG_Cobol74 = 0x0005, /* ISO Cobol:1974 */ + DW_LANG_Cobol85 = 0x0006, /* ISO Cobol:1985 */ + DW_LANG_Fortran77 = 0x0007, /* ISO FORTRAN 77 */ + DW_LANG_Fortran90 = 0x0008, /* ISO Fortran 90 */ + DW_LANG_Pascal83 = 0x0009, /* ISO Pascal:1983 */ + DW_LANG_Modula2 = 0x000a, /* ISO Modula-2:1996 */ + DW_LANG_Java = 0x000b, /* Java */ + DW_LANG_C99 = 0x000c, /* ISO C:1999 */ + DW_LANG_Ada95 = 0x000d, /* ISO Ada:1995 */ + DW_LANG_Fortran95 = 0x000e, /* ISO Fortran 95 */ + DW_LANG_PLI = 0x000f, /* ISO PL/1:1976 */ + DW_LANG_ObjC = 0x0010, /* Objective-C */ + DW_LANG_ObjC_plus_plus = 0x0011, /* Objective-C++ */ + DW_LANG_UPC = 0x0012, /* Unified Parallel C */ + DW_LANG_D = 0x0013, /* D */ + DW_LANG_Python = 0x0014, /* Python */ + DW_LANG_OpenCL = 0x0015, /* OpenCL */ + DW_LANG_Go = 0x0016, /* Go */ + DW_LANG_Modula3 = 0x0017, /* Modula-3 */ + DW_LANG_Haskell = 0x0018, /* Haskell */ + DW_LANG_C_plus_plus_03 = 0x0019, /* ISO C++:2003 */ + DW_LANG_C_plus_plus_11 = 0x001a, /* ISO C++:2011 */ + DW_LANG_OCaml = 0x001b, /* OCaml */ + DW_LANG_Rust = 0x001c, /* Rust */ + DW_LANG_C11 = 0x001d, /* ISO C:2011 */ + DW_LANG_Swift = 0x001e, /* Swift */ + DW_LANG_Julia = 0x001f, /* Julia */ + DW_LANG_Dylan = 0x0020, /* Dylan */ + DW_LANG_C_plus_plus_14 = 0x0021, /* ISO C++:2014 */ + DW_LANG_Fortran03 = 0x0022, /* ISO/IEC 1539-1:2004 */ + DW_LANG_Fortran08 = 0x0023, /* ISO/IEC 1539-1:2010 */ + DW_LANG_RenderScript = 0x0024, /* RenderScript Kernal Language */ + DW_LANG_BLISS = 0x0025, /* BLISS */ + + DW_LANG_lo_user = 0x8000, + DW_LANG_Mips_Assembler = 0x8001, /* Assembler */ + DW_LANG_hi_user = 0xffff + }; + +/* Old (typo) '1' != 'I'. */ +#define DW_LANG_PL1 DW_LANG_PLI + +/* DWARF identifier case encodings. */ +enum + { + DW_ID_case_sensitive = 0, + DW_ID_up_case = 1, + DW_ID_down_case = 2, + DW_ID_case_insensitive = 3 + }; + + +/* DWARF calling conventions encodings. + Used as values of DW_AT_calling_convention for subroutines + (normal, program or nocall) or structures, unions and class types + (normal, reference or value). */ +enum + { + DW_CC_normal = 0x1, + DW_CC_program = 0x2, + DW_CC_nocall = 0x3, + DW_CC_pass_by_reference = 0x4, + DW_CC_pass_by_value = 0x5, + DW_CC_lo_user = 0x40, + DW_CC_hi_user = 0xff + }; + + +/* DWARF inline encodings. */ +enum + { + DW_INL_not_inlined = 0, + DW_INL_inlined = 1, + DW_INL_declared_not_inlined = 2, + DW_INL_declared_inlined = 3 + }; + + +/* DWARF ordering encodings. */ +enum + { + DW_ORD_row_major = 0, + DW_ORD_col_major = 1 + }; + + +/* DWARF discriminant descriptor encodings. */ +enum + { + DW_DSC_label = 0, + DW_DSC_range = 1 + }; + +/* DWARF defaulted member function encodings. */ +enum + { + DW_DEFAULTED_no = 0, + DW_DEFAULTED_in_class = 1, + DW_DEFAULTED_out_of_class = 2 + }; + +/* DWARF line content descriptions. */ +enum + { + DW_LNCT_path = 0x1, + DW_LNCT_directory_index = 0x2, + DW_LNCT_timestamp = 0x3, + DW_LNCT_size = 0x4, + DW_LNCT_MD5 = 0x5, + DW_LNCT_lo_user = 0x2000, + DW_LNCT_hi_user = 0x3fff + }; + +/* DWARF standard opcode encodings. */ +enum + { + DW_LNS_copy = 1, + DW_LNS_advance_pc = 2, + DW_LNS_advance_line = 3, + DW_LNS_set_file = 4, + DW_LNS_set_column = 5, + DW_LNS_negate_stmt = 6, + DW_LNS_set_basic_block = 7, + DW_LNS_const_add_pc = 8, + DW_LNS_fixed_advance_pc = 9, + DW_LNS_set_prologue_end = 10, + DW_LNS_set_epilogue_begin = 11, + DW_LNS_set_isa = 12 + }; + + +/* DWARF extended opcode encodings. */ +enum + { + DW_LNE_end_sequence = 1, + DW_LNE_set_address = 2, + DW_LNE_define_file = 3, + DW_LNE_set_discriminator = 4, + + DW_LNE_lo_user = 128, + + DW_LNE_NVIDIA_inlined_call = 144, + DW_LNE_NVIDIA_set_function_name = 145, + + DW_LNE_hi_user = 255 + }; + + +/* DWARF macinfo type encodings. */ +enum + { + DW_MACINFO_define = 1, + DW_MACINFO_undef = 2, + DW_MACINFO_start_file = 3, + DW_MACINFO_end_file = 4, + DW_MACINFO_vendor_ext = 255 + }; + + +/* DWARF debug_macro type encodings. */ +enum + { + DW_MACRO_define = 0x01, + DW_MACRO_undef = 0x02, + DW_MACRO_start_file = 0x03, + DW_MACRO_end_file = 0x04, + DW_MACRO_define_strp = 0x05, + DW_MACRO_undef_strp = 0x06, + DW_MACRO_import = 0x07, + DW_MACRO_define_sup = 0x08, + DW_MACRO_undef_sup = 0x09, + DW_MACRO_import_sup = 0x0a, + DW_MACRO_define_strx = 0x0b, + DW_MACRO_undef_strx = 0x0c, + DW_MACRO_lo_user = 0xe0, + DW_MACRO_hi_user = 0xff + }; + +/* Old GNU extension names for DWARF5 debug_macro type encodings. + There are no equivalents for the supplementary object file (sup) + and indirect string references (strx). */ +#define DW_MACRO_GNU_define DW_MACRO_define +#define DW_MACRO_GNU_undef DW_MACRO_undef +#define DW_MACRO_GNU_start_file DW_MACRO_start_file +#define DW_MACRO_GNU_end_file DW_MACRO_end_file +#define DW_MACRO_GNU_define_indirect DW_MACRO_define_strp +#define DW_MACRO_GNU_undef_indirect DW_MACRO_undef_strp +#define DW_MACRO_GNU_transparent_include DW_MACRO_import +#define DW_MACRO_GNU_lo_user DW_MACRO_lo_user +#define DW_MACRO_GNU_hi_user DW_MACRO_hi_user + + +/* Range list entry encoding. */ +enum + { + DW_RLE_end_of_list = 0x0, + DW_RLE_base_addressx = 0x1, + DW_RLE_startx_endx = 0x2, + DW_RLE_startx_length = 0x3, + DW_RLE_offset_pair = 0x4, + DW_RLE_base_address = 0x5, + DW_RLE_start_end = 0x6, + DW_RLE_start_length = 0x7 + }; + + +/* Location list entry encoding. */ +enum + { + DW_LLE_end_of_list = 0x0, + DW_LLE_base_addressx = 0x1, + DW_LLE_startx_endx = 0x2, + DW_LLE_startx_length = 0x3, + DW_LLE_offset_pair = 0x4, + DW_LLE_default_location = 0x5, + DW_LLE_base_address = 0x6, + DW_LLE_start_end = 0x7, + DW_LLE_start_length = 0x8 + }; + + +/* GNU DebugFission list entry encodings (.debug_loc.dwo). */ +enum + { + DW_LLE_GNU_end_of_list_entry = 0x0, + DW_LLE_GNU_base_address_selection_entry = 0x1, + DW_LLE_GNU_start_end_entry = 0x2, + DW_LLE_GNU_start_length_entry = 0x3 + }; + +/* DWARF5 package file section identifiers. */ +enum + { + DW_SECT_INFO = 1, + /* Reserved = 2, */ + DW_SECT_ABBREV = 3, + DW_SECT_LINE = 4, + DW_SECT_LOCLISTS = 5, + DW_SECT_STR_OFFSETS = 6, + DW_SECT_MACRO = 7, + DW_SECT_RNGLISTS = 8, + }; + + +/* DWARF call frame instruction encodings. */ +enum + { + DW_CFA_advance_loc = 0x40, + DW_CFA_offset = 0x80, + DW_CFA_restore = 0xc0, + DW_CFA_extended = 0, + + DW_CFA_nop = 0x00, + DW_CFA_set_loc = 0x01, + DW_CFA_advance_loc1 = 0x02, + DW_CFA_advance_loc2 = 0x03, + DW_CFA_advance_loc4 = 0x04, + DW_CFA_offset_extended = 0x05, + DW_CFA_restore_extended = 0x06, + DW_CFA_undefined = 0x07, + DW_CFA_same_value = 0x08, + DW_CFA_register = 0x09, + DW_CFA_remember_state = 0x0a, + DW_CFA_restore_state = 0x0b, + DW_CFA_def_cfa = 0x0c, + DW_CFA_def_cfa_register = 0x0d, + DW_CFA_def_cfa_offset = 0x0e, + DW_CFA_def_cfa_expression = 0x0f, + DW_CFA_expression = 0x10, + DW_CFA_offset_extended_sf = 0x11, + DW_CFA_def_cfa_sf = 0x12, + DW_CFA_def_cfa_offset_sf = 0x13, + DW_CFA_val_offset = 0x14, + DW_CFA_val_offset_sf = 0x15, + DW_CFA_val_expression = 0x16, + + DW_CFA_low_user = 0x1c, + DW_CFA_MIPS_advance_loc8 = 0x1d, + DW_CFA_GNU_window_save = 0x2d, + DW_CFA_AARCH64_negate_ra_state = 0x2d, + DW_CFA_GNU_args_size = 0x2e, + DW_CFA_GNU_negative_offset_extended = 0x2f, + DW_CFA_high_user = 0x3f + }; + +/* ID indicating CIE as opposed to FDE in .debug_frame. */ +enum + { + DW_CIE_ID_32 = 0xffffffffU, /* In 32-bit format CIE header. */ + DW_CIE_ID_64 = 0xffffffffffffffffULL /* In 64-bit format CIE header. */ + }; + + +/* Information for GNU unwind information. */ +enum + { + DW_EH_PE_absptr = 0x00, + DW_EH_PE_omit = 0xff, + + /* FDE data encoding. */ + DW_EH_PE_uleb128 = 0x01, + DW_EH_PE_udata2 = 0x02, + DW_EH_PE_udata4 = 0x03, + DW_EH_PE_udata8 = 0x04, + DW_EH_PE_sleb128 = 0x09, + DW_EH_PE_sdata2 = 0x0a, + DW_EH_PE_sdata4 = 0x0b, + DW_EH_PE_sdata8 = 0x0c, + DW_EH_PE_signed = 0x08, + + /* FDE flags. */ + DW_EH_PE_pcrel = 0x10, + DW_EH_PE_textrel = 0x20, + DW_EH_PE_datarel = 0x30, + DW_EH_PE_funcrel = 0x40, + DW_EH_PE_aligned = 0x50, + + DW_EH_PE_indirect = 0x80 + }; + + +/* DWARF XXX. */ +#define DW_ADDR_none 0 + +/* Section 7.2.2 of the DWARF3 specification defines a range of escape + codes that can appear in the length field of certain DWARF structures. + + These defines enumerate the minimum and maximum values of this range. + Currently only the maximum value is used (to indicate that 64-bit + values are going to be used in the dwarf data that accompanies the + structure). The other values are reserved. + + Note: There is a typo in DWARF3 spec (published Dec 20, 2005). In + sections 7.4, 7.5.1, 7.19, 7.20 the minimum escape code is referred to + as 0xffffff00 whereas in fact it should be 0xfffffff0. */ +#define DWARF3_LENGTH_MIN_ESCAPE_CODE 0xfffffff0u +#define DWARF3_LENGTH_MAX_ESCAPE_CODE 0xffffffffu +#define DWARF3_LENGTH_64_BIT DWARF3_LENGTH_MAX_ESCAPE_CODE + +#endif /* dwarf.h */ diff --git a/05/tcc-final/elf.h b/05/tcc-final/elf.h new file mode 100644 index 0000000..19271e5 --- /dev/null +++ b/05/tcc-final/elf.h @@ -0,0 +1,3319 @@ +/* This file defines standard ELF types, structures, and macros. + Copyright (C) 1995-2012 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _ELF_H +#define _ELF_H 1 + +#ifndef _WIN32 +#include +#else +#ifndef __int8_t_defined +#define __int8_t_defined +typedef signed char int8_t; +typedef short int int16_t; +typedef int int32_t; +typedef long long int int64_t; +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long int uint64_t; +#endif +#endif + +/* Standard ELF types. */ + +/* Type for a 16-bit quantity. */ +typedef uint16_t Elf32_Half; +typedef uint16_t Elf64_Half; + +/* Types for signed and unsigned 32-bit quantities. */ +typedef uint32_t Elf32_Word; +typedef int32_t Elf32_Sword; +typedef uint32_t Elf64_Word; +typedef int32_t Elf64_Sword; + +/* Types for signed and unsigned 64-bit quantities. */ +typedef uint64_t Elf32_Xword; +typedef int64_t Elf32_Sxword; +typedef uint64_t Elf64_Xword; +typedef int64_t Elf64_Sxword; + +/* Type of addresses. */ +typedef uint32_t Elf32_Addr; +typedef uint64_t Elf64_Addr; + +/* Type of file offsets. */ +typedef uint32_t Elf32_Off; +typedef uint64_t Elf64_Off; + +/* Type for section indices, which are 16-bit quantities. */ +typedef uint16_t Elf32_Section; +typedef uint16_t Elf64_Section; + +/* Type for version symbol information. */ +typedef Elf32_Half Elf32_Versym; +typedef Elf64_Half Elf64_Versym; + + +/* The ELF file header. This appears at the start of every ELF file. */ + +#define EI_NIDENT (16) + +typedef struct +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf32_Half e_type; /* Object file type */ + Elf32_Half e_machine; /* Architecture */ + Elf32_Word e_version; /* Object file version */ + Elf32_Addr e_entry; /* Entry point virtual address */ + Elf32_Off e_phoff; /* Program header table file offset */ + Elf32_Off e_shoff; /* Section header table file offset */ + Elf32_Word e_flags; /* Processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size in bytes */ + Elf32_Half e_phentsize; /* Program header table entry size */ + Elf32_Half e_phnum; /* Program header table entry count */ + Elf32_Half e_shentsize; /* Section header table entry size */ + Elf32_Half e_shnum; /* Section header table entry count */ + Elf32_Half e_shstrndx; /* Section header string table index */ +} Elf32_Ehdr; + +typedef struct +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ +} Elf64_Ehdr; + +/* Fields in the e_ident array. The EI_* macros are indices into the + array. The macros under each EI_* macro are the values the byte + may have. */ + +#define EI_MAG0 0 /* File identification byte 0 index */ +#define ELFMAG0 0x7f /* Magic number byte 0 */ + +#define EI_MAG1 1 /* File identification byte 1 index */ +#define ELFMAG1 'E' /* Magic number byte 1 */ + +#define EI_MAG2 2 /* File identification byte 2 index */ +#define ELFMAG2 'L' /* Magic number byte 2 */ + +#define EI_MAG3 3 /* File identification byte 3 index */ +#define ELFMAG3 'F' /* Magic number byte 3 */ + +/* Conglomeration of the identification bytes, for easy testing as a word. */ +#define ELFMAG "\177ELF" +#define SELFMAG 4 + +#define EI_CLASS 4 /* File class byte index */ +#define ELFCLASSNONE 0 /* Invalid class */ +#define ELFCLASS32 1 /* 32-bit objects */ +#define ELFCLASS64 2 /* 64-bit objects */ +#define ELFCLASSNUM 3 + +#define EI_DATA 5 /* Data encoding byte index */ +#define ELFDATANONE 0 /* Invalid data encoding */ +#define ELFDATA2LSB 1 /* 2's complement, little endian */ +#define ELFDATA2MSB 2 /* 2's complement, big endian */ +#define ELFDATANUM 3 + +#define EI_VERSION 6 /* File version byte index */ + /* Value must be EV_CURRENT */ + +#define EI_OSABI 7 /* OS ABI identification */ +#define ELFOSABI_NONE 0 /* UNIX System V ABI */ +#define ELFOSABI_SYSV 0 /* Alias. */ +#define ELFOSABI_HPUX 1 /* HP-UX */ +#define ELFOSABI_NETBSD 2 /* NetBSD. */ +#define ELFOSABI_GNU 3 /* Object uses GNU ELF extensions. */ +#define ELFOSABI_LINUX ELFOSABI_GNU /* Compatibility alias. */ +#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ +#define ELFOSABI_AIX 7 /* IBM AIX. */ +#define ELFOSABI_IRIX 8 /* SGI Irix. */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD. */ +#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto. */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD. */ +#define ELFOSABI_OPENVMS 13 +#define ELFOSABI_NSK 14 /* Hewlett-Packard Non-Stop Kernel. */ +#define ELFOSABI_AROS 15 /* Amiga Research OS. */ +#define ELFOSABI_FENIXOS 16 /* FenixOS. */ +#define ELFOSABI_ARM_AEABI 64 /* ARM EABI. */ +#define ELFOSABI_C6000_LINUX 65 /* Linux TMS320C6000. */ +#define ELFOSABI_ARM 97 /* ARM */ +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ + +#define EI_ABIVERSION 8 /* ABI version */ + +#define EI_PAD 9 /* Byte index of padding bytes */ + +/* Legal values for e_type (object file type). */ + +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* Relocatable file */ +#define ET_EXEC 2 /* Executable file */ +#define ET_DYN 3 /* Shared object file */ +#define ET_CORE 4 /* Core file */ +#define ET_NUM 5 /* Number of defined types */ +#define ET_LOOS 0xfe00 /* OS-specific range start */ +#define ET_HIOS 0xfeff /* OS-specific range end */ +#define ET_LOPROC 0xff00 /* Processor-specific range start */ +#define ET_HIPROC 0xffff /* Processor-specific range end */ + +/* Legal values for e_machine (architecture). */ + +#define EM_NONE 0 /* No machine */ +#define EM_M32 1 /* AT&T WE 32100 */ +#define EM_SPARC 2 /* SUN SPARC */ +#define EM_386 3 /* Intel 80386 */ +#define EM_68K 4 /* Motorola m68k family */ +#define EM_88K 5 /* Motorola m88k family */ +#define EM_860 7 /* Intel 80860 */ +#define EM_MIPS 8 /* MIPS R3000 big-endian */ +#define EM_S370 9 /* IBM System/370 */ +#define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ + +#define EM_PARISC 15 /* HPPA */ +#define EM_VPP500 17 /* Fujitsu VPP500 */ +#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ +#define EM_960 19 /* Intel 80960 */ +#define EM_PPC 20 /* PowerPC */ +#define EM_PPC64 21 /* PowerPC 64-bit */ +#define EM_S390 22 /* IBM S390 */ + +#define EM_V800 36 /* NEC V800 series */ +#define EM_FR20 37 /* Fujitsu FR20 */ +#define EM_RH32 38 /* TRW RH-32 */ +#define EM_RCE 39 /* Motorola RCE */ +#define EM_ARM 40 /* ARM */ +#define EM_FAKE_ALPHA 41 /* Digital Alpha */ +#define EM_SH 42 /* Hitachi SH */ +#define EM_SPARCV9 43 /* SPARC v9 64-bit */ +#define EM_TRICORE 44 /* Siemens Tricore */ +#define EM_ARC 45 /* Argonaut RISC Core */ +#define EM_H8_300 46 /* Hitachi H8/300 */ +#define EM_H8_300H 47 /* Hitachi H8/300H */ +#define EM_H8S 48 /* Hitachi H8S */ +#define EM_H8_500 49 /* Hitachi H8/500 */ +#define EM_IA_64 50 /* Intel Merced */ +#define EM_MIPS_X 51 /* Stanford MIPS-X */ +#define EM_COLDFIRE 52 /* Motorola Coldfire */ +#define EM_68HC12 53 /* Motorola M68HC12 */ +#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/ +#define EM_PCP 55 /* Siemens PCP */ +#define EM_NCPU 56 /* Sony nCPU embedded RISC */ +#define EM_NDR1 57 /* Denso NDR1 microprocessor */ +#define EM_STARCORE 58 /* Motorola Start*Core processor */ +#define EM_ME16 59 /* Toyota ME16 processor */ +#define EM_ST100 60 /* STMicroelectronic ST100 processor */ +#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/ +#define EM_X86_64 62 /* AMD x86-64 architecture */ +#define EM_PDSP 63 /* Sony DSP Processor */ + +#define EM_FX66 66 /* Siemens FX66 microcontroller */ +#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ +#define EM_ST7 68 /* STMicroelectronics ST7 8 bit mc */ +#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ +#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ +#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ +#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ +#define EM_SVX 73 /* Silicon Graphics SVx */ +#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ +#define EM_VAX 75 /* Digital VAX */ +#define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ +#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */ +#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ +#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ +#define EM_MMIX 80 /* Donald Knuth's educational 64-bit processor */ +#define EM_HUANY 81 /* Harvard University machine-independent object files */ +#define EM_PRISM 82 /* SiTera Prism */ +#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ +#define EM_FR30 84 /* Fujitsu FR30 */ +#define EM_D10V 85 /* Mitsubishi D10V */ +#define EM_D30V 86 /* Mitsubishi D30V */ +#define EM_V850 87 /* NEC v850 */ +#define EM_M32R 88 /* Mitsubishi M32R */ +#define EM_MN10300 89 /* Matsushita MN10300 */ +#define EM_MN10200 90 /* Matsushita MN10200 */ +#define EM_PJ 91 /* picoJava */ +#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ +#define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */ +#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ +#define EM_AARCH64 183 /* ARM AARCH64 */ +#define EM_TILEPRO 188 /* Tilera TILEPro */ +#define EM_TILEGX 191 /* Tilera TILE-Gx */ +#define EM_RISCV 243 /* RISC-V */ +#define EM_NUM 253 + +/* If it is necessary to assign new unofficial EM_* values, please + pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the + chances of collision with official or non-GNU unofficial values. */ + +#define EM_ALPHA 0x9026 +#define EM_C60 0x9c60 + +/* Legal values for e_version (version). */ + +#define EV_NONE 0 /* Invalid ELF version */ +#define EV_CURRENT 1 /* Current version */ +#define EV_NUM 2 + +/* Section header. */ + +typedef struct +{ + Elf32_Word sh_name; /* Section name (string tbl index) */ + Elf32_Word sh_type; /* Section type */ + Elf32_Word sh_flags; /* Section flags */ + Elf32_Addr sh_addr; /* Section virtual addr at execution */ + Elf32_Off sh_offset; /* Section file offset */ + Elf32_Word sh_size; /* Section size in bytes */ + Elf32_Word sh_link; /* Link to another section */ + Elf32_Word sh_info; /* Additional section information */ + Elf32_Word sh_addralign; /* Section alignment */ + Elf32_Word sh_entsize; /* Entry size if section holds table */ +} Elf32_Shdr; + +typedef struct +{ + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ +} Elf64_Shdr; + +/* Special section indices. */ + +#define SHN_UNDEF 0 /* Undefined section */ +#define SHN_LORESERVE 0xff00 /* Start of reserved indices */ +#define SHN_LOPROC 0xff00 /* Start of processor-specific */ +#define SHN_BEFORE 0xff00 /* Order section before all others + (Solaris). */ +#define SHN_AFTER 0xff01 /* Order section after all others + (Solaris). */ +#define SHN_HIPROC 0xff1f /* End of processor-specific */ +#define SHN_LOOS 0xff20 /* Start of OS-specific */ +#define SHN_HIOS 0xff3f /* End of OS-specific */ +#define SHN_ABS 0xfff1 /* Associated symbol is absolute */ +#define SHN_COMMON 0xfff2 /* Associated symbol is common */ +#define SHN_XINDEX 0xffff /* Index is in extra table. */ +#define SHN_HIRESERVE 0xffff /* End of reserved indices */ + +/* Legal values for sh_type (section type). */ + +#define SHT_NULL 0 /* Section header table entry unused */ +#define SHT_PROGBITS 1 /* Program data */ +#define SHT_SYMTAB 2 /* Symbol table */ +#define SHT_STRTAB 3 /* String table */ +#define SHT_RELA 4 /* Relocation entries with addends */ +#define SHT_HASH 5 /* Symbol hash table */ +#define SHT_DYNAMIC 6 /* Dynamic linking information */ +#define SHT_NOTE 7 /* Notes */ +#define SHT_NOBITS 8 /* Program space with no data (bss) */ +#define SHT_REL 9 /* Relocation entries, no addends */ +#define SHT_SHLIB 10 /* Reserved */ +#define SHT_DYNSYM 11 /* Dynamic linker symbol table */ +#define SHT_INIT_ARRAY 14 /* Array of constructors */ +#define SHT_FINI_ARRAY 15 /* Array of destructors */ +#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ +#define SHT_GROUP 17 /* Section group */ +#define SHT_SYMTAB_SHNDX 18 /* Extended section indices */ +#define SHT_NUM 19 /* Number of defined types. */ +#define SHT_LOOS 0x60000000 /* Start OS-specific. */ +#define SHT_GNU_ATTRIBUTES 0x6ffffff5 /* Object attributes. */ +#define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table. */ +#define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */ +#define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */ +#define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */ +#define SHT_SUNW_move 0x6ffffffa +#define SHT_SUNW_COMDAT 0x6ffffffb +#define SHT_SUNW_syminfo 0x6ffffffc +#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */ +#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */ +#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */ +#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */ +#define SHT_HIOS 0x6fffffff /* End OS-specific type */ +#define SHT_LOPROC 0x70000000 /* Start of processor-specific */ +#define SHT_HIPROC 0x7fffffff /* End of processor-specific */ +#define SHT_LOUSER 0x80000000 /* Start of application-specific */ +#define SHT_HIUSER 0x8fffffff /* End of application-specific */ + +/* Legal values for sh_flags (section flags). */ + +#define SHF_WRITE (1 << 0) /* Writable */ +#define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ +#define SHF_EXECINSTR (1 << 2) /* Executable */ +#define SHF_MERGE (1 << 4) /* Might be merged */ +#define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */ +#define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */ +#define SHF_LINK_ORDER (1 << 7) /* Preserve order after combining */ +#define SHF_OS_NONCONFORMING (1 << 8) /* Non-standard OS specific handling + required */ +#define SHF_GROUP (1 << 9) /* Section is member of a group. */ +#define SHF_TLS (1 << 10) /* Section hold thread-local data. */ +#define SHF_COMPRESSED (1 << 11) /* Section with compressed data. */ +#define SHF_MASKOS 0x0ff00000 /* OS-specific. */ +#define SHF_MASKPROC 0xf0000000 /* Processor-specific */ +#define SHF_ORDERED (1 << 30) /* Special ordering requirement + (Solaris). */ +#define SHF_EXCLUDE (1U << 31) /* Section is excluded unless + referenced or allocated (Solaris).*/ + +/* Section group handling. */ +#define GRP_COMDAT 0x1 /* Mark group as COMDAT. */ + +/* Symbol table entry. */ + +typedef struct +{ + Elf32_Word st_name; /* Symbol name (string tbl index) */ + Elf32_Addr st_value; /* Symbol value */ + Elf32_Word st_size; /* Symbol size */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf32_Section st_shndx; /* Section index */ +} Elf32_Sym; + +typedef struct +{ + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ +} Elf64_Sym; + +/* The syminfo section if available contains additional information about + every dynamic symbol. */ + +typedef struct +{ + Elf32_Half si_boundto; /* Direct bindings, symbol bound to */ + Elf32_Half si_flags; /* Per symbol flags */ +} Elf32_Syminfo; + +typedef struct +{ + Elf64_Half si_boundto; /* Direct bindings, symbol bound to */ + Elf64_Half si_flags; /* Per symbol flags */ +} Elf64_Syminfo; + +/* Possible values for si_boundto. */ +#define SYMINFO_BT_SELF 0xffff /* Symbol bound to self */ +#define SYMINFO_BT_PARENT 0xfffe /* Symbol bound to parent */ +#define SYMINFO_BT_LOWRESERVE 0xff00 /* Beginning of reserved entries */ + +/* Possible bitmasks for si_flags. */ +#define SYMINFO_FLG_DIRECT 0x0001 /* Direct bound symbol */ +#define SYMINFO_FLG_PASSTHRU 0x0002 /* Pass-thru symbol for translator */ +#define SYMINFO_FLG_COPY 0x0004 /* Symbol is a copy-reloc */ +#define SYMINFO_FLG_LAZYLOAD 0x0008 /* Symbol bound to object to be lazy + loaded */ +/* Syminfo version values. */ +#define SYMINFO_NONE 0 +#define SYMINFO_CURRENT 1 +#define SYMINFO_NUM 2 + + +/* How to extract and insert information held in the st_info field. */ + +#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) +#define ELF32_ST_TYPE(val) ((val) & 0xf) +#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) + +/* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field. */ +#define ELF64_ST_BIND(val) ELF32_ST_BIND (val) +#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) +#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) + +/* Legal values for ST_BIND subfield of st_info (symbol binding). */ + +#define STB_LOCAL 0 /* Local symbol */ +#define STB_GLOBAL 1 /* Global symbol */ +#define STB_WEAK 2 /* Weak symbol */ +#define STB_NUM 3 /* Number of defined types. */ +#define STB_LOOS 10 /* Start of OS-specific */ +#define STB_GNU_UNIQUE 10 /* Unique symbol. */ +#define STB_HIOS 12 /* End of OS-specific */ +#define STB_LOPROC 13 /* Start of processor-specific */ +#define STB_HIPROC 15 /* End of processor-specific */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_NOTYPE 0 /* Symbol type is unspecified */ +#define STT_OBJECT 1 /* Symbol is a data object */ +#define STT_FUNC 2 /* Symbol is a code object */ +#define STT_SECTION 3 /* Symbol associated with a section */ +#define STT_FILE 4 /* Symbol's name is file name */ +#define STT_COMMON 5 /* Symbol is a common data object */ +#define STT_TLS 6 /* Symbol is thread-local data object*/ +#define STT_NUM 7 /* Number of defined types. */ +#define STT_LOOS 10 /* Start of OS-specific */ +#define STT_GNU_IFUNC 10 /* Symbol is indirect code object */ +#define STT_HIOS 12 /* End of OS-specific */ +#define STT_LOPROC 13 /* Start of processor-specific */ +#define STT_HIPROC 15 /* End of processor-specific */ + + +/* Symbol table indices are found in the hash buckets and chain table + of a symbol hash table section. This special index value indicates + the end of a chain, meaning no further symbols are found in that bucket. */ + +#define STN_UNDEF 0 /* End of a chain. */ + + +/* How to extract and insert information held in the st_other field. */ + +#define ELF32_ST_VISIBILITY(o) ((o) & 0x03) + +/* For ELF64 the definitions are the same. */ +#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) + +/* Symbol visibility specification encoded in the st_other field. */ +#define STV_DEFAULT 0 /* Default symbol visibility rules */ +#define STV_INTERNAL 1 /* Processor specific hidden class */ +#define STV_HIDDEN 2 /* Sym unavailable in other modules */ +#define STV_PROTECTED 3 /* Not preemptible, not exported */ + + +/* Relocation table entry without addend (in section of type SHT_REL). */ + +typedef struct +{ + Elf32_Addr r_offset; /* Address */ + Elf32_Word r_info; /* Relocation type and symbol index */ +} Elf32_Rel; + +/* I have seen two different definitions of the Elf64_Rel and + Elf64_Rela structures, so we'll leave them out until Novell (or + whoever) gets their act together. */ +/* The following, at least, is used on Sparc v9, MIPS, and Alpha. */ + +typedef struct +{ + Elf64_Addr r_offset; /* Address */ + Elf64_Xword r_info; /* Relocation type and symbol index */ +} Elf64_Rel; + +/* Relocation table entry with addend (in section of type SHT_RELA). */ + +typedef struct +{ + Elf32_Addr r_offset; /* Address */ + Elf32_Word r_info; /* Relocation type and symbol index */ + Elf32_Sword r_addend; /* Addend */ +} Elf32_Rela; + +typedef struct +{ + Elf64_Addr r_offset; /* Address */ + Elf64_Xword r_info; /* Relocation type and symbol index */ + Elf64_Sxword r_addend; /* Addend */ +} Elf64_Rela; + +/* How to extract and insert information held in the r_info field. */ + +#define ELF32_R_SYM(val) ((val) >> 8) +#define ELF32_R_TYPE(val) ((val) & 0xff) +#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) + +#define ELF64_R_SYM(i) ((i) >> 32) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) +#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type)) + +/* Program segment header. */ + +typedef struct +{ + Elf32_Word p_type; /* Segment type */ + Elf32_Off p_offset; /* Segment file offset */ + Elf32_Addr p_vaddr; /* Segment virtual address */ + Elf32_Addr p_paddr; /* Segment physical address */ + Elf32_Word p_filesz; /* Segment size in file */ + Elf32_Word p_memsz; /* Segment size in memory */ + Elf32_Word p_flags; /* Segment flags */ + Elf32_Word p_align; /* Segment alignment */ +} Elf32_Phdr; + +typedef struct +{ + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ +} Elf64_Phdr; + +/* Special value for e_phnum. This indicates that the real number of + program headers is too large to fit into e_phnum. Instead the real + value is in the field sh_info of section 0. */ + +#define PN_XNUM 0xffff + +/* Legal values for p_type (segment type). */ + +#define PT_NULL 0 /* Program header table entry unused */ +#define PT_LOAD 1 /* Loadable program segment */ +#define PT_DYNAMIC 2 /* Dynamic linking information */ +#define PT_INTERP 3 /* Program interpreter */ +#define PT_NOTE 4 /* Auxiliary information */ +#define PT_SHLIB 5 /* Reserved */ +#define PT_PHDR 6 /* Entry for header table itself */ +#define PT_TLS 7 /* Thread-local storage segment */ +#define PT_NUM 8 /* Number of defined types */ +#define PT_LOOS 0x60000000 /* Start of OS-specific */ +#define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */ +#define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */ +#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */ +#define PT_LOSUNW 0x6ffffffa +#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */ +#define PT_SUNWSTACK 0x6ffffffb /* Stack segment */ +#define PT_HISUNW 0x6fffffff +#define PT_HIOS 0x6fffffff /* End of OS-specific */ +#define PT_LOPROC 0x70000000 /* Start of processor-specific */ +#define PT_HIPROC 0x7fffffff /* End of processor-specific */ + +/* Legal values for p_flags (segment flags). */ + +#define PF_X (1 << 0) /* Segment is executable */ +#define PF_W (1 << 1) /* Segment is writable */ +#define PF_R (1 << 2) /* Segment is readable */ +#define PF_MASKOS 0x0ff00000 /* OS-specific */ +#define PF_MASKPROC 0xf0000000 /* Processor-specific */ + +/* Legal values for note segment descriptor types for core files. */ + +#define NT_PRSTATUS 1 /* Contains copy of prstatus struct */ +#define NT_FPREGSET 2 /* Contains copy of fpregset struct */ +#define NT_PRPSINFO 3 /* Contains copy of prpsinfo struct */ +#define NT_PRXREG 4 /* Contains copy of prxregset struct */ +#define NT_TASKSTRUCT 4 /* Contains copy of task structure */ +#define NT_PLATFORM 5 /* String from sysinfo(SI_PLATFORM) */ +#define NT_AUXV 6 /* Contains copy of auxv array */ +#define NT_GWINDOWS 7 /* Contains copy of gwindows struct */ +#define NT_ASRS 8 /* Contains copy of asrset struct */ +#define NT_PSTATUS 10 /* Contains copy of pstatus struct */ +#define NT_PSINFO 13 /* Contains copy of psinfo struct */ +#define NT_PRCRED 14 /* Contains copy of prcred struct */ +#define NT_UTSNAME 15 /* Contains copy of utsname struct */ +#define NT_LWPSTATUS 16 /* Contains copy of lwpstatus struct */ +#define NT_LWPSINFO 17 /* Contains copy of lwpinfo struct */ +#define NT_PRFPXREG 20 /* Contains copy of fprxregset struct */ +#define NT_PRXFPREG 0x46e62b7f /* Contains copy of user_fxsr_struct */ +#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ +#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ +#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ +#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ +#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ +#define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ +#define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ +#define NT_S390_TIMER 0x301 /* s390 timer register */ +#define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ +#define NT_S390_TODPREG 0x303 /* s390 TOD programmable register */ +#define NT_S390_CTRS 0x304 /* s390 control registers */ +#define NT_S390_PREFIX 0x305 /* s390 prefix register */ +#define NT_S390_LAST_BREAK 0x306 /* s390 breaking event address */ +#define NT_S390_SYSTEM_CALL 0x307 /* s390 system call restart data */ +#define NT_ARM_VFP 0x400 /* ARM VFP/NEON registers */ +#define NT_ARM_TLS 0x401 /* ARM TLS register */ +#define NT_ARM_HW_BREAK 0x402 /* ARM hardware breakpoint registers */ +#define NT_ARM_HW_WATCH 0x403 /* ARM hardware watchpoint registers */ + +/* Legal values for the note segment descriptor types for object files. */ + +#define NT_VERSION 1 /* Contains a version string. */ + + +/* Dynamic section entry. */ + +typedef struct +{ + Elf32_Sword d_tag; /* Dynamic entry type */ + union + { + Elf32_Word d_val; /* Integer value */ + Elf32_Addr d_ptr; /* Address value */ + } d_un; +} Elf32_Dyn; + +typedef struct +{ + Elf64_Sxword d_tag; /* Dynamic entry type */ + union + { + Elf64_Xword d_val; /* Integer value */ + Elf64_Addr d_ptr; /* Address value */ + } d_un; +} Elf64_Dyn; + +/* Legal values for d_tag (dynamic entry type). */ + +#define DT_NULL 0 /* Marks end of dynamic section */ +#define DT_NEEDED 1 /* Name of needed library */ +#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */ +#define DT_PLTGOT 3 /* Processor defined value */ +#define DT_HASH 4 /* Address of symbol hash table */ +#define DT_STRTAB 5 /* Address of string table */ +#define DT_SYMTAB 6 /* Address of symbol table */ +#define DT_RELA 7 /* Address of Rela relocs */ +#define DT_RELASZ 8 /* Total size of Rela relocs */ +#define DT_RELAENT 9 /* Size of one Rela reloc */ +#define DT_STRSZ 10 /* Size of string table */ +#define DT_SYMENT 11 /* Size of one symbol table entry */ +#define DT_INIT 12 /* Address of init function */ +#define DT_FINI 13 /* Address of termination function */ +#define DT_SONAME 14 /* Name of shared object */ +#define DT_RPATH 15 /* Library search path (deprecated) */ +#define DT_SYMBOLIC 16 /* Start symbol search here */ +#define DT_REL 17 /* Address of Rel relocs */ +#define DT_RELSZ 18 /* Total size of Rel relocs */ +#define DT_RELENT 19 /* Size of one Rel reloc */ +#define DT_PLTREL 20 /* Type of reloc in PLT */ +#define DT_DEBUG 21 /* For debugging; unspecified */ +#define DT_TEXTREL 22 /* Reloc might modify .text */ +#define DT_JMPREL 23 /* Address of PLT relocs */ +#define DT_BIND_NOW 24 /* Process relocations of object */ +#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ +#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ +#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ +#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ +#define DT_RUNPATH 29 /* Library search path */ +#define DT_FLAGS 30 /* Flags for the object being loaded */ +#define DT_ENCODING 32 /* Start of encoded range */ +#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/ +#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */ +#define DT_NUM 34 /* Number used */ +#define DT_LOOS 0x6000000d /* Start of OS-specific */ +#define DT_HIOS 0x6ffff000 /* End of OS-specific */ +#define DT_LOPROC 0x70000000 /* Start of processor-specific */ +#define DT_HIPROC 0x7fffffff /* End of processor-specific */ +#define DT_PROCNUM DT_MIPS_NUM /* Most used by any processor */ + +/* DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the + Dyn.d_un.d_val field of the Elf*_Dyn structure. This follows Sun's + approach. */ +#define DT_VALRNGLO 0x6ffffd00 +#define DT_GNU_PRELINKED 0x6ffffdf5 /* Prelinking timestamp */ +#define DT_GNU_CONFLICTSZ 0x6ffffdf6 /* Size of conflict section */ +#define DT_GNU_LIBLISTSZ 0x6ffffdf7 /* Size of library list */ +#define DT_CHECKSUM 0x6ffffdf8 +#define DT_PLTPADSZ 0x6ffffdf9 +#define DT_MOVEENT 0x6ffffdfa +#define DT_MOVESZ 0x6ffffdfb +#define DT_FEATURE_1 0x6ffffdfc /* Feature selection (DTF_*). */ +#define DT_POSFLAG_1 0x6ffffdfd /* Flags for DT_* entries, effecting + the following DT_* entry. */ +#define DT_SYMINSZ 0x6ffffdfe /* Size of syminfo table (in bytes) */ +#define DT_SYMINENT 0x6ffffdff /* Entry size of syminfo */ +#define DT_VALRNGHI 0x6ffffdff +#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag)) /* Reverse order! */ +#define DT_VALNUM 12 + +/* DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the + Dyn.d_un.d_ptr field of the Elf*_Dyn structure. + + If any adjustment is made to the ELF object after it has been + built these entries will need to be adjusted. */ +#define DT_ADDRRNGLO 0x6ffffe00 +#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */ +#define DT_TLSDESC_PLT 0x6ffffef6 +#define DT_TLSDESC_GOT 0x6ffffef7 +#define DT_GNU_CONFLICT 0x6ffffef8 /* Start of conflict section */ +#define DT_GNU_LIBLIST 0x6ffffef9 /* Library list */ +#define DT_CONFIG 0x6ffffefa /* Configuration information. */ +#define DT_DEPAUDIT 0x6ffffefb /* Dependency auditing. */ +#define DT_AUDIT 0x6ffffefc /* Object auditing. */ +#define DT_PLTPAD 0x6ffffefd /* PLT padding. */ +#define DT_MOVETAB 0x6ffffefe /* Move table. */ +#define DT_SYMINFO 0x6ffffeff /* Syminfo table. */ +#define DT_ADDRRNGHI 0x6ffffeff +#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag)) /* Reverse order! */ +#define DT_ADDRNUM 11 + +/* The versioning entry types. The next are defined as part of the + GNU extension. */ +#define DT_VERSYM 0x6ffffff0 + +#define DT_RELACOUNT 0x6ffffff9 +#define DT_RELCOUNT 0x6ffffffa + +/* These were chosen by Sun. */ +#define DT_FLAGS_1 0x6ffffffb /* State flags, see DF_1_* below. */ +#define DT_VERDEF 0x6ffffffc /* Address of version definition + table */ +#define DT_VERDEFNUM 0x6ffffffd /* Number of version definitions */ +#define DT_VERNEED 0x6ffffffe /* Address of table with needed + versions */ +#define DT_VERNEEDNUM 0x6fffffff /* Number of needed versions */ +#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */ +#define DT_VERSIONTAGNUM 16 + +/* Sun added these machine-independent extensions in the "processor-specific" + range. Be compatible. */ +#define DT_AUXILIARY 0x7ffffffd /* Shared object to load before self */ +#define DT_FILTER 0x7fffffff /* Shared object to get values from */ +#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1) +#define DT_EXTRANUM 3 + +/* Values of `d_un.d_val' in the DT_FLAGS entry. */ +#define DF_ORIGIN 0x00000001 /* Object may use DF_ORIGIN */ +#define DF_SYMBOLIC 0x00000002 /* Symbol resolutions starts here */ +#define DF_TEXTREL 0x00000004 /* Object contains text relocations */ +#define DF_BIND_NOW 0x00000008 /* No lazy binding for this object */ +#define DF_STATIC_TLS 0x00000010 /* Module uses the static TLS model */ + +/* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1 + entry in the dynamic section. */ +#define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */ +#define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */ +#define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */ +#define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object.*/ +#define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime.*/ +#define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object*/ +#define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */ +#define DF_1_ORIGIN 0x00000080 /* $ORIGIN must be handled. */ +#define DF_1_DIRECT 0x00000100 /* Direct binding enabled. */ +#define DF_1_TRANS 0x00000200 +#define DF_1_INTERPOSE 0x00000400 /* Object is used to interpose. */ +#define DF_1_NODEFLIB 0x00000800 /* Ignore default lib search path. */ +#define DF_1_NODUMP 0x00001000 /* Object can't be dldump'ed. */ +#define DF_1_CONFALT 0x00002000 /* Configuration alternative created.*/ +#define DF_1_ENDFILTEE 0x00004000 /* Filtee terminates filters search. */ +#define DF_1_DISPRELDNE 0x00008000 /* Disp reloc applied at build time. */ +#define DF_1_DISPRELPND 0x00010000 /* Disp reloc applied at run-time. */ +#define DF_1_NODIRECT 0x00020000 /* Object has no-direct binding. */ +#define DF_1_IGNMULDEF 0x00040000 +#define DF_1_NOKSYMS 0x00080000 +#define DF_1_NOHDR 0x00100000 +#define DF_1_EDITED 0x00200000 /* Object is modified after built. */ +#define DF_1_NORELOC 0x00400000 +#define DF_1_SYMINTPOSE 0x00800000 /* Object has individual interposers. */ +#define DF_1_GLOBAUDIT 0x01000000 /* Global auditing required. */ +#define DF_1_SINGLETON 0x02000000 /* Singleton symbols are used. */ + +/* Flags for the feature selection in DT_FEATURE_1. */ +#define DTF_1_PARINIT 0x00000001 +#define DTF_1_CONFEXP 0x00000002 + +/* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */ +#define DF_P1_LAZYLOAD 0x00000001 /* Lazyload following object. */ +#define DF_P1_GROUPPERM 0x00000002 /* Symbols from next object are not + generally available. */ + +/* Version definition sections. */ + +typedef struct +{ + Elf32_Half vd_version; /* Version revision */ + Elf32_Half vd_flags; /* Version information */ + Elf32_Half vd_ndx; /* Version Index */ + Elf32_Half vd_cnt; /* Number of associated aux entries */ + Elf32_Word vd_hash; /* Version name hash value */ + Elf32_Word vd_aux; /* Offset in bytes to verdaux array */ + Elf32_Word vd_next; /* Offset in bytes to next verdef + entry */ +} Elf32_Verdef; + +typedef struct +{ + Elf64_Half vd_version; /* Version revision */ + Elf64_Half vd_flags; /* Version information */ + Elf64_Half vd_ndx; /* Version Index */ + Elf64_Half vd_cnt; /* Number of associated aux entries */ + Elf64_Word vd_hash; /* Version name hash value */ + Elf64_Word vd_aux; /* Offset in bytes to verdaux array */ + Elf64_Word vd_next; /* Offset in bytes to next verdef + entry */ +} Elf64_Verdef; + + +/* Legal values for vd_version (version revision). */ +#define VER_DEF_NONE 0 /* No version */ +#define VER_DEF_CURRENT 1 /* Current version */ +#define VER_DEF_NUM 2 /* Given version number */ + +/* Legal values for vd_flags (version information flags). */ +#define VER_FLG_BASE 0x1 /* Version definition of file itself */ +#define VER_FLG_WEAK 0x2 /* Weak version identifier */ + +/* Versym symbol index values. */ +#define VER_NDX_LOCAL 0 /* Symbol is local. */ +#define VER_NDX_GLOBAL 1 /* Symbol is global. */ +#define VER_NDX_LORESERVE 0xff00 /* Beginning of reserved entries. */ +#define VER_NDX_ELIMINATE 0xff01 /* Symbol is to be eliminated. */ + +/* Auxiliary version information. */ + +typedef struct +{ + Elf32_Word vda_name; /* Version or dependency names */ + Elf32_Word vda_next; /* Offset in bytes to next verdaux + entry */ +} Elf32_Verdaux; + +typedef struct +{ + Elf64_Word vda_name; /* Version or dependency names */ + Elf64_Word vda_next; /* Offset in bytes to next verdaux + entry */ +} Elf64_Verdaux; + + +/* Version dependency section. */ + +typedef struct +{ + Elf32_Half vn_version; /* Version of structure */ + Elf32_Half vn_cnt; /* Number of associated aux entries */ + Elf32_Word vn_file; /* Offset of filename for this + dependency */ + Elf32_Word vn_aux; /* Offset in bytes to vernaux array */ + Elf32_Word vn_next; /* Offset in bytes to next verneed + entry */ +} Elf32_Verneed; + +typedef struct +{ + Elf64_Half vn_version; /* Version of structure */ + Elf64_Half vn_cnt; /* Number of associated aux entries */ + Elf64_Word vn_file; /* Offset of filename for this + dependency */ + Elf64_Word vn_aux; /* Offset in bytes to vernaux array */ + Elf64_Word vn_next; /* Offset in bytes to next verneed + entry */ +} Elf64_Verneed; + + +/* Legal values for vn_version (version revision). */ +#define VER_NEED_NONE 0 /* No version */ +#define VER_NEED_CURRENT 1 /* Current version */ +#define VER_NEED_NUM 2 /* Given version number */ + +/* Auxiliary needed version information. */ + +typedef struct +{ + Elf32_Word vna_hash; /* Hash value of dependency name */ + Elf32_Half vna_flags; /* Dependency specific information */ + Elf32_Half vna_other; /* Unused */ + Elf32_Word vna_name; /* Dependency name string offset */ + Elf32_Word vna_next; /* Offset in bytes to next vernaux + entry */ +} Elf32_Vernaux; + +typedef struct +{ + Elf64_Word vna_hash; /* Hash value of dependency name */ + Elf64_Half vna_flags; /* Dependency specific information */ + Elf64_Half vna_other; /* Unused */ + Elf64_Word vna_name; /* Dependency name string offset */ + Elf64_Word vna_next; /* Offset in bytes to next vernaux + entry */ +} Elf64_Vernaux; + + +/* Legal values for vna_flags. */ +#define VER_FLG_WEAK 0x2 /* Weak version identifier */ + + +/* Auxiliary vector. */ + +/* This vector is normally only used by the program interpreter. The + usual definition in an ABI supplement uses the name auxv_t. The + vector is not usually defined in a standard file, but it + can't hurt. We rename it to avoid conflicts. The sizes of these + types are an arrangement between the exec server and the program + interpreter, so we don't fully specify them here. */ + +typedef struct +{ + uint32_t a_type; /* Entry type */ + union + { + uint32_t a_val; /* Integer value */ + /* We use to have pointer elements added here. We cannot do that, + though, since it does not work when using 32-bit definitions + on 64-bit platforms and vice versa. */ + } a_un; +} Elf32_auxv_t; + +typedef struct +{ + uint64_t a_type; /* Entry type */ + union + { + uint64_t a_val; /* Integer value */ + /* We use to have pointer elements added here. We cannot do that, + though, since it does not work when using 32-bit definitions + on 64-bit platforms and vice versa. */ + } a_un; +} Elf64_auxv_t; + +/* Legal values for a_type (entry type). */ + +#define AT_NULL 0 /* End of vector */ +#define AT_IGNORE 1 /* Entry should be ignored */ +#define AT_EXECFD 2 /* File descriptor of program */ +#define AT_PHDR 3 /* Program headers for program */ +#define AT_PHENT 4 /* Size of program header entry */ +#define AT_PHNUM 5 /* Number of program headers */ +#define AT_PAGESZ 6 /* System page size */ +#define AT_BASE 7 /* Base address of interpreter */ +#define AT_FLAGS 8 /* Flags */ +#define AT_ENTRY 9 /* Entry point of program */ +#define AT_NOTELF 10 /* Program is not ELF */ +#define AT_UID 11 /* Real uid */ +#define AT_EUID 12 /* Effective uid */ +#define AT_GID 13 /* Real gid */ +#define AT_EGID 14 /* Effective gid */ +#define AT_CLKTCK 17 /* Frequency of times() */ + +/* Some more special a_type values describing the hardware. */ +#define AT_PLATFORM 15 /* String identifying platform. */ +#define AT_HWCAP 16 /* Machine dependent hints about + processor capabilities. */ + +/* This entry gives some information about the FPU initialization + performed by the kernel. */ +#define AT_FPUCW 18 /* Used FPU control word. */ + +/* Cache block sizes. */ +#define AT_DCACHEBSIZE 19 /* Data cache block size. */ +#define AT_ICACHEBSIZE 20 /* Instruction cache block size. */ +#define AT_UCACHEBSIZE 21 /* Unified cache block size. */ + +/* A special ignored value for PPC, used by the kernel to control the + interpretation of the AUXV. Must be > 16. */ +#define AT_IGNOREPPC 22 /* Entry should be ignored. */ + +#define AT_SECURE 23 /* Boolean, was exec setuid-like? */ + +#define AT_BASE_PLATFORM 24 /* String identifying real platforms.*/ + +#define AT_RANDOM 25 /* Address of 16 random bytes. */ + +#define AT_EXECFN 31 /* Filename of executable. */ + +/* Pointer to the global system page used for system calls and other + nice things. */ +#define AT_SYSINFO 32 +#define AT_SYSINFO_EHDR 33 + +/* Shapes of the caches. Bits 0-3 contains associativity; bits 4-7 contains + log2 of line size; mask those to get cache size. */ +#define AT_L1I_CACHESHAPE 34 +#define AT_L1D_CACHESHAPE 35 +#define AT_L2_CACHESHAPE 36 +#define AT_L3_CACHESHAPE 37 + +/* Note section contents. Each entry in the note section begins with + a header of a fixed form. */ + +typedef struct +{ + Elf32_Word n_namesz; /* Length of the note's name. */ + Elf32_Word n_descsz; /* Length of the note's descriptor. */ + Elf32_Word n_type; /* Type of the note. */ +} Elf32_Nhdr; + +typedef struct +{ + Elf64_Word n_namesz; /* Length of the note's name. */ + Elf64_Word n_descsz; /* Length of the note's descriptor. */ + Elf64_Word n_type; /* Type of the note. */ +} Elf64_Nhdr; + +/* Known names of notes. */ + +/* Solaris entries in the note section have this name. */ +#define ELF_NOTE_SOLARIS "SUNW Solaris" + +/* Note entries for GNU systems have this name. */ +#define ELF_NOTE_GNU "GNU" + + +/* Defined types of notes for Solaris. */ + +/* Value of descriptor (one word) is desired pagesize for the binary. */ +#define ELF_NOTE_PAGESIZE_HINT 1 + + +/* Defined note types for GNU systems. */ + +/* ABI information. The descriptor consists of words: + word 0: OS descriptor + word 1: major version of the ABI + word 2: minor version of the ABI + word 3: subminor version of the ABI +*/ +#define NT_GNU_ABI_TAG 1 +#define ELF_NOTE_ABI NT_GNU_ABI_TAG /* Old name. */ + +/* Known OSes. These values can appear in word 0 of an + NT_GNU_ABI_TAG note section entry. */ +#define ELF_NOTE_OS_LINUX 0 +#define ELF_NOTE_OS_GNU 1 +#define ELF_NOTE_OS_SOLARIS2 2 +#define ELF_NOTE_OS_FREEBSD 3 + +/* Synthetic hwcap information. The descriptor begins with two words: + word 0: number of entries + word 1: bitmask of enabled entries + Then follow variable-length entries, one byte followed by a + '\0'-terminated hwcap name string. The byte gives the bit + number to test if enabled, (1U << bit) & bitmask. */ +#define NT_GNU_HWCAP 2 + +/* Build ID bits as generated by ld --build-id. + The descriptor consists of any nonzero number of bytes. */ +#define NT_GNU_BUILD_ID 3 + +/* Version note generated by GNU gold containing a version string. */ +#define NT_GNU_GOLD_VERSION 4 + + +/* Move records. */ +typedef struct +{ + Elf32_Xword m_value; /* Symbol value. */ + Elf32_Word m_info; /* Size and index. */ + Elf32_Word m_poffset; /* Symbol offset. */ + Elf32_Half m_repeat; /* Repeat count. */ + Elf32_Half m_stride; /* Stride info. */ +} Elf32_Move; + +typedef struct +{ + Elf64_Xword m_value; /* Symbol value. */ + Elf64_Xword m_info; /* Size and index. */ + Elf64_Xword m_poffset; /* Symbol offset. */ + Elf64_Half m_repeat; /* Repeat count. */ + Elf64_Half m_stride; /* Stride info. */ +} Elf64_Move; + +/* Macro to construct move records. */ +#define ELF32_M_SYM(info) ((info) >> 8) +#define ELF32_M_SIZE(info) ((unsigned char) (info)) +#define ELF32_M_INFO(sym, size) (((sym) << 8) + (unsigned char) (size)) + +#define ELF64_M_SYM(info) ELF32_M_SYM (info) +#define ELF64_M_SIZE(info) ELF32_M_SIZE (info) +#define ELF64_M_INFO(sym, size) ELF32_M_INFO (sym, size) + + +/* Motorola 68k specific definitions. */ + +/* Values for Elf32_Ehdr.e_flags. */ +#define EF_CPU32 0x00810000 + +/* m68k relocs. */ + +#define R_68K_NONE 0 /* No reloc */ +#define R_68K_32 1 /* Direct 32 bit */ +#define R_68K_16 2 /* Direct 16 bit */ +#define R_68K_8 3 /* Direct 8 bit */ +#define R_68K_PC32 4 /* PC relative 32 bit */ +#define R_68K_PC16 5 /* PC relative 16 bit */ +#define R_68K_PC8 6 /* PC relative 8 bit */ +#define R_68K_GOT32 7 /* 32 bit PC relative GOT entry */ +#define R_68K_GOT16 8 /* 16 bit PC relative GOT entry */ +#define R_68K_GOT8 9 /* 8 bit PC relative GOT entry */ +#define R_68K_GOT32O 10 /* 32 bit GOT offset */ +#define R_68K_GOT16O 11 /* 16 bit GOT offset */ +#define R_68K_GOT8O 12 /* 8 bit GOT offset */ +#define R_68K_PLT32 13 /* 32 bit PC relative PLT address */ +#define R_68K_PLT16 14 /* 16 bit PC relative PLT address */ +#define R_68K_PLT8 15 /* 8 bit PC relative PLT address */ +#define R_68K_PLT32O 16 /* 32 bit PLT offset */ +#define R_68K_PLT16O 17 /* 16 bit PLT offset */ +#define R_68K_PLT8O 18 /* 8 bit PLT offset */ +#define R_68K_COPY 19 /* Copy symbol at runtime */ +#define R_68K_GLOB_DAT 20 /* Create GOT entry */ +#define R_68K_JMP_SLOT 21 /* Create PLT entry */ +#define R_68K_RELATIVE 22 /* Adjust by program base */ +#define R_68K_TLS_GD32 25 /* 32 bit GOT offset for GD */ +#define R_68K_TLS_GD16 26 /* 16 bit GOT offset for GD */ +#define R_68K_TLS_GD8 27 /* 8 bit GOT offset for GD */ +#define R_68K_TLS_LDM32 28 /* 32 bit GOT offset for LDM */ +#define R_68K_TLS_LDM16 29 /* 16 bit GOT offset for LDM */ +#define R_68K_TLS_LDM8 30 /* 8 bit GOT offset for LDM */ +#define R_68K_TLS_LDO32 31 /* 32 bit module-relative offset */ +#define R_68K_TLS_LDO16 32 /* 16 bit module-relative offset */ +#define R_68K_TLS_LDO8 33 /* 8 bit module-relative offset */ +#define R_68K_TLS_IE32 34 /* 32 bit GOT offset for IE */ +#define R_68K_TLS_IE16 35 /* 16 bit GOT offset for IE */ +#define R_68K_TLS_IE8 36 /* 8 bit GOT offset for IE */ +#define R_68K_TLS_LE32 37 /* 32 bit offset relative to + static TLS block */ +#define R_68K_TLS_LE16 38 /* 16 bit offset relative to + static TLS block */ +#define R_68K_TLS_LE8 39 /* 8 bit offset relative to + static TLS block */ +#define R_68K_TLS_DTPMOD32 40 /* 32 bit module number */ +#define R_68K_TLS_DTPREL32 41 /* 32 bit module-relative offset */ +#define R_68K_TLS_TPREL32 42 /* 32 bit TP-relative offset */ +/* Keep this the last entry. */ +#define R_68K_NUM 43 + +/* Intel 80386 specific definitions. */ + +/* i386 relocs. */ + +#define R_386_NONE 0 /* No reloc */ +#define R_386_32 1 /* Direct 32 bit */ +#define R_386_PC32 2 /* PC relative 32 bit */ +#define R_386_GOT32 3 /* 32 bit GOT entry */ +#define R_386_PLT32 4 /* 32 bit PLT address */ +#define R_386_COPY 5 /* Copy symbol at runtime */ +#define R_386_GLOB_DAT 6 /* Create GOT entry */ +#define R_386_JMP_SLOT 7 /* Create PLT entry */ +#define R_386_RELATIVE 8 /* Adjust by program base */ +#define R_386_GOTOFF 9 /* 32 bit offset to GOT */ +#define R_386_GOTPC 10 /* 32 bit PC relative offset to GOT */ +#define R_386_32PLT 11 +#define R_386_TLS_TPOFF 14 /* Offset in static TLS block */ +#define R_386_TLS_IE 15 /* Address of GOT entry for static TLS + block offset */ +#define R_386_TLS_GOTIE 16 /* GOT entry for static TLS block + offset */ +#define R_386_TLS_LE 17 /* Offset relative to static TLS + block */ +#define R_386_TLS_GD 18 /* Direct 32 bit for GNU version of + general dynamic thread local data */ +#define R_386_TLS_LDM 19 /* Direct 32 bit for GNU version of + local dynamic thread local data + in LE code */ +#define R_386_16 20 +#define R_386_PC16 21 +#define R_386_8 22 +#define R_386_PC8 23 +#define R_386_TLS_GD_32 24 /* Direct 32 bit for general dynamic + thread local data */ +#define R_386_TLS_GD_PUSH 25 /* Tag for pushl in GD TLS code */ +#define R_386_TLS_GD_CALL 26 /* Relocation for call to + __tls_get_addr() */ +#define R_386_TLS_GD_POP 27 /* Tag for popl in GD TLS code */ +#define R_386_TLS_LDM_32 28 /* Direct 32 bit for local dynamic + thread local data in LE code */ +#define R_386_TLS_LDM_PUSH 29 /* Tag for pushl in LDM TLS code */ +#define R_386_TLS_LDM_CALL 30 /* Relocation for call to + __tls_get_addr() in LDM code */ +#define R_386_TLS_LDM_POP 31 /* Tag for popl in LDM TLS code */ +#define R_386_TLS_LDO_32 32 /* Offset relative to TLS block */ +#define R_386_TLS_IE_32 33 /* GOT entry for negated static TLS + block offset */ +#define R_386_TLS_LE_32 34 /* Negated offset relative to static + TLS block */ +#define R_386_TLS_DTPMOD32 35 /* ID of module containing symbol */ +#define R_386_TLS_DTPOFF32 36 /* Offset in TLS block */ +#define R_386_TLS_TPOFF32 37 /* Negated offset in static TLS block */ +/* 38? */ +#define R_386_TLS_GOTDESC 39 /* GOT offset for TLS descriptor. */ +#define R_386_TLS_DESC_CALL 40 /* Marker of call through TLS + descriptor for + relaxation. */ +#define R_386_TLS_DESC 41 /* TLS descriptor containing + pointer to code and to + argument, returning the TLS + offset for the symbol. */ +#define R_386_IRELATIVE 42 /* Adjust indirectly by program base */ +#define R_386_GOT32X 43 /* 32 bit GOT entry, relaxable */ +/* Keep this the last entry. */ +#define R_386_NUM 44 + +/* SUN SPARC specific definitions. */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_SPARC_REGISTER 13 /* Global register reserved to app. */ + +/* Values for Elf64_Ehdr.e_flags. */ + +#define EF_SPARCV9_MM 3 +#define EF_SPARCV9_TSO 0 +#define EF_SPARCV9_PSO 1 +#define EF_SPARCV9_RMO 2 +#define EF_SPARC_LEDATA 0x800000 /* little endian data */ +#define EF_SPARC_EXT_MASK 0xFFFF00 +#define EF_SPARC_32PLUS 0x000100 /* generic V8+ features */ +#define EF_SPARC_SUN_US1 0x000200 /* Sun UltraSPARC1 extensions */ +#define EF_SPARC_HAL_R1 0x000400 /* HAL R1 extensions */ +#define EF_SPARC_SUN_US3 0x000800 /* Sun UltraSPARCIII extensions */ + +/* SPARC relocs. */ + +#define R_SPARC_NONE 0 /* No reloc */ +#define R_SPARC_8 1 /* Direct 8 bit */ +#define R_SPARC_16 2 /* Direct 16 bit */ +#define R_SPARC_32 3 /* Direct 32 bit */ +#define R_SPARC_DISP8 4 /* PC relative 8 bit */ +#define R_SPARC_DISP16 5 /* PC relative 16 bit */ +#define R_SPARC_DISP32 6 /* PC relative 32 bit */ +#define R_SPARC_WDISP30 7 /* PC relative 30 bit shifted */ +#define R_SPARC_WDISP22 8 /* PC relative 22 bit shifted */ +#define R_SPARC_HI22 9 /* High 22 bit */ +#define R_SPARC_22 10 /* Direct 22 bit */ +#define R_SPARC_13 11 /* Direct 13 bit */ +#define R_SPARC_LO10 12 /* Truncated 10 bit */ +#define R_SPARC_GOT10 13 /* Truncated 10 bit GOT entry */ +#define R_SPARC_GOT13 14 /* 13 bit GOT entry */ +#define R_SPARC_GOT22 15 /* 22 bit GOT entry shifted */ +#define R_SPARC_PC10 16 /* PC relative 10 bit truncated */ +#define R_SPARC_PC22 17 /* PC relative 22 bit shifted */ +#define R_SPARC_WPLT30 18 /* 30 bit PC relative PLT address */ +#define R_SPARC_COPY 19 /* Copy symbol at runtime */ +#define R_SPARC_GLOB_DAT 20 /* Create GOT entry */ +#define R_SPARC_JMP_SLOT 21 /* Create PLT entry */ +#define R_SPARC_RELATIVE 22 /* Adjust by program base */ +#define R_SPARC_UA32 23 /* Direct 32 bit unaligned */ + +/* Additional Sparc64 relocs. */ + +#define R_SPARC_PLT32 24 /* Direct 32 bit ref to PLT entry */ +#define R_SPARC_HIPLT22 25 /* High 22 bit PLT entry */ +#define R_SPARC_LOPLT10 26 /* Truncated 10 bit PLT entry */ +#define R_SPARC_PCPLT32 27 /* PC rel 32 bit ref to PLT entry */ +#define R_SPARC_PCPLT22 28 /* PC rel high 22 bit PLT entry */ +#define R_SPARC_PCPLT10 29 /* PC rel trunc 10 bit PLT entry */ +#define R_SPARC_10 30 /* Direct 10 bit */ +#define R_SPARC_11 31 /* Direct 11 bit */ +#define R_SPARC_64 32 /* Direct 64 bit */ +#define R_SPARC_OLO10 33 /* 10bit with secondary 13bit addend */ +#define R_SPARC_HH22 34 /* Top 22 bits of direct 64 bit */ +#define R_SPARC_HM10 35 /* High middle 10 bits of ... */ +#define R_SPARC_LM22 36 /* Low middle 22 bits of ... */ +#define R_SPARC_PC_HH22 37 /* Top 22 bits of pc rel 64 bit */ +#define R_SPARC_PC_HM10 38 /* High middle 10 bit of ... */ +#define R_SPARC_PC_LM22 39 /* Low middle 22 bits of ... */ +#define R_SPARC_WDISP16 40 /* PC relative 16 bit shifted */ +#define R_SPARC_WDISP19 41 /* PC relative 19 bit shifted */ +#define R_SPARC_GLOB_JMP 42 /* was part of v9 ABI but was removed */ +#define R_SPARC_7 43 /* Direct 7 bit */ +#define R_SPARC_5 44 /* Direct 5 bit */ +#define R_SPARC_6 45 /* Direct 6 bit */ +#define R_SPARC_DISP64 46 /* PC relative 64 bit */ +#define R_SPARC_PLT64 47 /* Direct 64 bit ref to PLT entry */ +#define R_SPARC_HIX22 48 /* High 22 bit complemented */ +#define R_SPARC_LOX10 49 /* Truncated 11 bit complemented */ +#define R_SPARC_H44 50 /* Direct high 12 of 44 bit */ +#define R_SPARC_M44 51 /* Direct mid 22 of 44 bit */ +#define R_SPARC_L44 52 /* Direct low 10 of 44 bit */ +#define R_SPARC_REGISTER 53 /* Global register usage */ +#define R_SPARC_UA64 54 /* Direct 64 bit unaligned */ +#define R_SPARC_UA16 55 /* Direct 16 bit unaligned */ +#define R_SPARC_TLS_GD_HI22 56 +#define R_SPARC_TLS_GD_LO10 57 +#define R_SPARC_TLS_GD_ADD 58 +#define R_SPARC_TLS_GD_CALL 59 +#define R_SPARC_TLS_LDM_HI22 60 +#define R_SPARC_TLS_LDM_LO10 61 +#define R_SPARC_TLS_LDM_ADD 62 +#define R_SPARC_TLS_LDM_CALL 63 +#define R_SPARC_TLS_LDO_HIX22 64 +#define R_SPARC_TLS_LDO_LOX10 65 +#define R_SPARC_TLS_LDO_ADD 66 +#define R_SPARC_TLS_IE_HI22 67 +#define R_SPARC_TLS_IE_LO10 68 +#define R_SPARC_TLS_IE_LD 69 +#define R_SPARC_TLS_IE_LDX 70 +#define R_SPARC_TLS_IE_ADD 71 +#define R_SPARC_TLS_LE_HIX22 72 +#define R_SPARC_TLS_LE_LOX10 73 +#define R_SPARC_TLS_DTPMOD32 74 +#define R_SPARC_TLS_DTPMOD64 75 +#define R_SPARC_TLS_DTPOFF32 76 +#define R_SPARC_TLS_DTPOFF64 77 +#define R_SPARC_TLS_TPOFF32 78 +#define R_SPARC_TLS_TPOFF64 79 +#define R_SPARC_GOTDATA_HIX22 80 +#define R_SPARC_GOTDATA_LOX10 81 +#define R_SPARC_GOTDATA_OP_HIX22 82 +#define R_SPARC_GOTDATA_OP_LOX10 83 +#define R_SPARC_GOTDATA_OP 84 +#define R_SPARC_H34 85 +#define R_SPARC_SIZE32 86 +#define R_SPARC_SIZE64 87 +#define R_SPARC_WDISP10 88 +#define R_SPARC_JMP_IREL 248 +#define R_SPARC_IRELATIVE 249 +#define R_SPARC_GNU_VTINHERIT 250 +#define R_SPARC_GNU_VTENTRY 251 +#define R_SPARC_REV32 252 +/* Keep this the last entry. */ +#define R_SPARC_NUM 253 + +/* For Sparc64, legal values for d_tag of Elf64_Dyn. */ + +#define DT_SPARC_REGISTER 0x70000001 +#define DT_SPARC_NUM 2 + +/* MIPS R3000 specific definitions. */ + +/* Legal values for e_flags field of Elf32_Ehdr. */ + +#define EF_MIPS_NOREORDER 1 /* A .noreorder directive was used */ +#define EF_MIPS_PIC 2 /* Contains PIC code */ +#define EF_MIPS_CPIC 4 /* Uses PIC calling sequence */ +#define EF_MIPS_XGOT 8 +#define EF_MIPS_64BIT_WHIRL 16 +#define EF_MIPS_ABI2 32 +#define EF_MIPS_ABI_ON32 64 +#define EF_MIPS_ARCH 0xf0000000 /* MIPS architecture level */ + +/* Legal values for MIPS architecture level. */ + +#define EF_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ +#define EF_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ +#define EF_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ +#define EF_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ +#define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ +#define EF_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */ +#define EF_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */ + +/* The following are non-official names and should not be used. */ + +#define E_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ +#define E_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ +#define E_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ +#define E_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ +#define E_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ +#define E_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */ +#define E_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */ + +/* Special section indices. */ + +#define SHN_MIPS_ACOMMON 0xff00 /* Allocated common symbols */ +#define SHN_MIPS_TEXT 0xff01 /* Allocated test symbols. */ +#define SHN_MIPS_DATA 0xff02 /* Allocated data symbols. */ +#define SHN_MIPS_SCOMMON 0xff03 /* Small common symbols */ +#define SHN_MIPS_SUNDEFINED 0xff04 /* Small undefined symbols */ + +/* Legal values for sh_type field of Elf32_Shdr. */ + +#define SHT_MIPS_LIBLIST 0x70000000 /* Shared objects used in link */ +#define SHT_MIPS_MSYM 0x70000001 +#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicting symbols */ +#define SHT_MIPS_GPTAB 0x70000003 /* Global data area sizes */ +#define SHT_MIPS_UCODE 0x70000004 /* Reserved for SGI/MIPS compilers */ +#define SHT_MIPS_DEBUG 0x70000005 /* MIPS ECOFF debugging information*/ +#define SHT_MIPS_REGINFO 0x70000006 /* Register usage information */ +#define SHT_MIPS_PACKAGE 0x70000007 +#define SHT_MIPS_PACKSYM 0x70000008 +#define SHT_MIPS_RELD 0x70000009 +#define SHT_MIPS_IFACE 0x7000000b +#define SHT_MIPS_CONTENT 0x7000000c +#define SHT_MIPS_OPTIONS 0x7000000d /* Miscellaneous options. */ +#define SHT_MIPS_SHDR 0x70000010 +#define SHT_MIPS_FDESC 0x70000011 +#define SHT_MIPS_EXTSYM 0x70000012 +#define SHT_MIPS_DENSE 0x70000013 +#define SHT_MIPS_PDESC 0x70000014 +#define SHT_MIPS_LOCSYM 0x70000015 +#define SHT_MIPS_AUXSYM 0x70000016 +#define SHT_MIPS_OPTSYM 0x70000017 +#define SHT_MIPS_LOCSTR 0x70000018 +#define SHT_MIPS_LINE 0x70000019 +#define SHT_MIPS_RFDESC 0x7000001a +#define SHT_MIPS_DELTASYM 0x7000001b +#define SHT_MIPS_DELTAINST 0x7000001c +#define SHT_MIPS_DELTACLASS 0x7000001d +#define SHT_MIPS_DWARF 0x7000001e /* DWARF debugging information. */ +#define SHT_MIPS_DELTADECL 0x7000001f +#define SHT_MIPS_SYMBOL_LIB 0x70000020 +#define SHT_MIPS_EVENTS 0x70000021 /* Event section. */ +#define SHT_MIPS_TRANSLATE 0x70000022 +#define SHT_MIPS_PIXIE 0x70000023 +#define SHT_MIPS_XLATE 0x70000024 +#define SHT_MIPS_XLATE_DEBUG 0x70000025 +#define SHT_MIPS_WHIRL 0x70000026 +#define SHT_MIPS_EH_REGION 0x70000027 +#define SHT_MIPS_XLATE_OLD 0x70000028 +#define SHT_MIPS_PDR_EXCEPTION 0x70000029 + +/* Legal values for sh_flags field of Elf32_Shdr. */ + +#define SHF_MIPS_GPREL 0x10000000 /* Must be part of global data area */ +#define SHF_MIPS_MERGE 0x20000000 +#define SHF_MIPS_ADDR 0x40000000 +#define SHF_MIPS_STRINGS 0x80000000 +#define SHF_MIPS_NOSTRIP 0x08000000 +#define SHF_MIPS_LOCAL 0x04000000 +#define SHF_MIPS_NAMES 0x02000000 +#define SHF_MIPS_NODUPE 0x01000000 + + +/* Symbol tables. */ + +/* MIPS specific values for `st_other'. */ +#define STO_MIPS_DEFAULT 0x0 +#define STO_MIPS_INTERNAL 0x1 +#define STO_MIPS_HIDDEN 0x2 +#define STO_MIPS_PROTECTED 0x3 +#define STO_MIPS_PLT 0x8 +#define STO_MIPS_SC_ALIGN_UNUSED 0xff + +/* MIPS specific values for `st_info'. */ +#define STB_MIPS_SPLIT_COMMON 13 + +/* Entries found in sections of type SHT_MIPS_GPTAB. */ + +typedef union +{ + struct + { + Elf32_Word gt_current_g_value; /* -G value used for compilation */ + Elf32_Word gt_unused; /* Not used */ + } gt_header; /* First entry in section */ + struct + { + Elf32_Word gt_g_value; /* If this value were used for -G */ + Elf32_Word gt_bytes; /* This many bytes would be used */ + } gt_entry; /* Subsequent entries in section */ +} Elf32_gptab; + +/* Entry found in sections of type SHT_MIPS_REGINFO. */ + +typedef struct +{ + Elf32_Word ri_gprmask; /* General registers used */ + Elf32_Word ri_cprmask[4]; /* Coprocessor registers used */ + Elf32_Sword ri_gp_value; /* $gp register value */ +} Elf32_RegInfo; + +/* Entries found in sections of type SHT_MIPS_OPTIONS. */ + +typedef struct +{ + unsigned char kind; /* Determines interpretation of the + variable part of descriptor. */ + unsigned char size; /* Size of descriptor, including header. */ + Elf32_Section section; /* Section header index of section affected, + 0 for global options. */ + Elf32_Word info; /* Kind-specific information. */ +} Elf_Options; + +/* Values for `kind' field in Elf_Options. */ + +#define ODK_NULL 0 /* Undefined. */ +#define ODK_REGINFO 1 /* Register usage information. */ +#define ODK_EXCEPTIONS 2 /* Exception processing options. */ +#define ODK_PAD 3 /* Section padding options. */ +#define ODK_HWPATCH 4 /* Hardware workarounds performed */ +#define ODK_FILL 5 /* record the fill value used by the linker. */ +#define ODK_TAGS 6 /* reserve space for desktop tools to write. */ +#define ODK_HWAND 7 /* HW workarounds. 'AND' bits when merging. */ +#define ODK_HWOR 8 /* HW workarounds. 'OR' bits when merging. */ + +/* Values for `info' in Elf_Options for ODK_EXCEPTIONS entries. */ + +#define OEX_FPU_MIN 0x1f /* FPE's which MUST be enabled. */ +#define OEX_FPU_MAX 0x1f00 /* FPE's which MAY be enabled. */ +#define OEX_PAGE0 0x10000 /* page zero must be mapped. */ +#define OEX_SMM 0x20000 /* Force sequential memory mode? */ +#define OEX_FPDBUG 0x40000 /* Force floating point debug mode? */ +#define OEX_PRECISEFP OEX_FPDBUG +#define OEX_DISMISS 0x80000 /* Dismiss invalid address faults? */ + +#define OEX_FPU_INVAL 0x10 +#define OEX_FPU_DIV0 0x08 +#define OEX_FPU_OFLO 0x04 +#define OEX_FPU_UFLO 0x02 +#define OEX_FPU_INEX 0x01 + +/* Masks for `info' in Elf_Options for an ODK_HWPATCH entry. */ + +#define OHW_R4KEOP 0x1 /* R4000 end-of-page patch. */ +#define OHW_R8KPFETCH 0x2 /* may need R8000 prefetch patch. */ +#define OHW_R5KEOP 0x4 /* R5000 end-of-page patch. */ +#define OHW_R5KCVTL 0x8 /* R5000 cvt.[ds].l bug. clean=1. */ + +#define OPAD_PREFIX 0x1 +#define OPAD_POSTFIX 0x2 +#define OPAD_SYMBOL 0x4 + +/* Entry found in `.options' section. */ + +typedef struct +{ + Elf32_Word hwp_flags1; /* Extra flags. */ + Elf32_Word hwp_flags2; /* Extra flags. */ +} Elf_Options_Hw; + +/* Masks for `info' in ElfOptions for ODK_HWAND and ODK_HWOR entries. */ + +#define OHWA0_R4KEOP_CHECKED 0x00000001 +#define OHWA1_R4KEOP_CLEAN 0x00000002 + +/* MIPS relocs. */ + +#define R_MIPS_NONE 0 /* No reloc */ +#define R_MIPS_16 1 /* Direct 16 bit */ +#define R_MIPS_32 2 /* Direct 32 bit */ +#define R_MIPS_REL32 3 /* PC relative 32 bit */ +#define R_MIPS_26 4 /* Direct 26 bit shifted */ +#define R_MIPS_HI16 5 /* High 16 bit */ +#define R_MIPS_LO16 6 /* Low 16 bit */ +#define R_MIPS_GPREL16 7 /* GP relative 16 bit */ +#define R_MIPS_LITERAL 8 /* 16 bit literal entry */ +#define R_MIPS_GOT16 9 /* 16 bit GOT entry */ +#define R_MIPS_PC16 10 /* PC relative 16 bit */ +#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */ +#define R_MIPS_GPREL32 12 /* GP relative 32 bit */ + +#define R_MIPS_SHIFT5 16 +#define R_MIPS_SHIFT6 17 +#define R_MIPS_64 18 +#define R_MIPS_GOT_DISP 19 +#define R_MIPS_GOT_PAGE 20 +#define R_MIPS_GOT_OFST 21 +#define R_MIPS_GOT_HI16 22 +#define R_MIPS_GOT_LO16 23 +#define R_MIPS_SUB 24 +#define R_MIPS_INSERT_A 25 +#define R_MIPS_INSERT_B 26 +#define R_MIPS_DELETE 27 +#define R_MIPS_HIGHER 28 +#define R_MIPS_HIGHEST 29 +#define R_MIPS_CALL_HI16 30 +#define R_MIPS_CALL_LO16 31 +#define R_MIPS_SCN_DISP 32 +#define R_MIPS_REL16 33 +#define R_MIPS_ADD_IMMEDIATE 34 +#define R_MIPS_PJUMP 35 +#define R_MIPS_RELGOT 36 +#define R_MIPS_JALR 37 +#define R_MIPS_TLS_DTPMOD32 38 /* Module number 32 bit */ +#define R_MIPS_TLS_DTPREL32 39 /* Module-relative offset 32 bit */ +#define R_MIPS_TLS_DTPMOD64 40 /* Module number 64 bit */ +#define R_MIPS_TLS_DTPREL64 41 /* Module-relative offset 64 bit */ +#define R_MIPS_TLS_GD 42 /* 16 bit GOT offset for GD */ +#define R_MIPS_TLS_LDM 43 /* 16 bit GOT offset for LDM */ +#define R_MIPS_TLS_DTPREL_HI16 44 /* Module-relative offset, high 16 bits */ +#define R_MIPS_TLS_DTPREL_LO16 45 /* Module-relative offset, low 16 bits */ +#define R_MIPS_TLS_GOTTPREL 46 /* 16 bit GOT offset for IE */ +#define R_MIPS_TLS_TPREL32 47 /* TP-relative offset, 32 bit */ +#define R_MIPS_TLS_TPREL64 48 /* TP-relative offset, 64 bit */ +#define R_MIPS_TLS_TPREL_HI16 49 /* TP-relative offset, high 16 bits */ +#define R_MIPS_TLS_TPREL_LO16 50 /* TP-relative offset, low 16 bits */ +#define R_MIPS_GLOB_DAT 51 +#define R_MIPS_COPY 126 +#define R_MIPS_JUMP_SLOT 127 +/* Keep this the last entry. */ +#define R_MIPS_NUM 128 + +/* Legal values for p_type field of Elf32_Phdr. */ + +#define PT_MIPS_REGINFO 0x70000000 /* Register usage information */ +#define PT_MIPS_RTPROC 0x70000001 /* Runtime procedure table. */ +#define PT_MIPS_OPTIONS 0x70000002 + +/* Special program header types. */ + +#define PF_MIPS_LOCAL 0x10000000 + +/* Legal values for d_tag field of Elf32_Dyn. */ + +#define DT_MIPS_RLD_VERSION 0x70000001 /* Runtime linker interface version */ +#define DT_MIPS_TIME_STAMP 0x70000002 /* Timestamp */ +#define DT_MIPS_ICHECKSUM 0x70000003 /* Checksum */ +#define DT_MIPS_IVERSION 0x70000004 /* Version string (string tbl index) */ +#define DT_MIPS_FLAGS 0x70000005 /* Flags */ +#define DT_MIPS_BASE_ADDRESS 0x70000006 /* Base address */ +#define DT_MIPS_MSYM 0x70000007 +#define DT_MIPS_CONFLICT 0x70000008 /* Address of CONFLICT section */ +#define DT_MIPS_LIBLIST 0x70000009 /* Address of LIBLIST section */ +#define DT_MIPS_LOCAL_GOTNO 0x7000000a /* Number of local GOT entries */ +#define DT_MIPS_CONFLICTNO 0x7000000b /* Number of CONFLICT entries */ +#define DT_MIPS_LIBLISTNO 0x70000010 /* Number of LIBLIST entries */ +#define DT_MIPS_SYMTABNO 0x70000011 /* Number of DYNSYM entries */ +#define DT_MIPS_UNREFEXTNO 0x70000012 /* First external DYNSYM */ +#define DT_MIPS_GOTSYM 0x70000013 /* First GOT entry in DYNSYM */ +#define DT_MIPS_HIPAGENO 0x70000014 /* Number of GOT page table entries */ +#define DT_MIPS_RLD_MAP 0x70000016 /* Address of run time loader map. */ +#define DT_MIPS_DELTA_CLASS 0x70000017 /* Delta C++ class definition. */ +#define DT_MIPS_DELTA_CLASS_NO 0x70000018 /* Number of entries in + DT_MIPS_DELTA_CLASS. */ +#define DT_MIPS_DELTA_INSTANCE 0x70000019 /* Delta C++ class instances. */ +#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a /* Number of entries in + DT_MIPS_DELTA_INSTANCE. */ +#define DT_MIPS_DELTA_RELOC 0x7000001b /* Delta relocations. */ +#define DT_MIPS_DELTA_RELOC_NO 0x7000001c /* Number of entries in + DT_MIPS_DELTA_RELOC. */ +#define DT_MIPS_DELTA_SYM 0x7000001d /* Delta symbols that Delta + relocations refer to. */ +#define DT_MIPS_DELTA_SYM_NO 0x7000001e /* Number of entries in + DT_MIPS_DELTA_SYM. */ +#define DT_MIPS_DELTA_CLASSSYM 0x70000020 /* Delta symbols that hold the + class declaration. */ +#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 /* Number of entries in + DT_MIPS_DELTA_CLASSSYM. */ +#define DT_MIPS_CXX_FLAGS 0x70000022 /* Flags indicating for C++ flavor. */ +#define DT_MIPS_PIXIE_INIT 0x70000023 +#define DT_MIPS_SYMBOL_LIB 0x70000024 +#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025 +#define DT_MIPS_LOCAL_GOTIDX 0x70000026 +#define DT_MIPS_HIDDEN_GOTIDX 0x70000027 +#define DT_MIPS_PROTECTED_GOTIDX 0x70000028 +#define DT_MIPS_OPTIONS 0x70000029 /* Address of .options. */ +#define DT_MIPS_INTERFACE 0x7000002a /* Address of .interface. */ +#define DT_MIPS_DYNSTR_ALIGN 0x7000002b +#define DT_MIPS_INTERFACE_SIZE 0x7000002c /* Size of the .interface section. */ +#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d /* Address of rld_text_rsolve + function stored in GOT. */ +#define DT_MIPS_PERF_SUFFIX 0x7000002e /* Default suffix of dso to be added + by rld on dlopen() calls. */ +#define DT_MIPS_COMPACT_SIZE 0x7000002f /* (O32)Size of compact rel section. */ +#define DT_MIPS_GP_VALUE 0x70000030 /* GP value for aux GOTs. */ +#define DT_MIPS_AUX_DYNAMIC 0x70000031 /* Address of aux .dynamic. */ +/* The address of .got.plt in an executable using the new non-PIC ABI. */ +#define DT_MIPS_PLTGOT 0x70000032 +/* The base of the PLT in an executable using the new non-PIC ABI if that + PLT is writable. For a non-writable PLT, this is omitted or has a zero + value. */ +#define DT_MIPS_RWPLT 0x70000034 +#define DT_MIPS_NUM 0x35 + +/* Legal values for DT_MIPS_FLAGS Elf32_Dyn entry. */ + +#define RHF_NONE 0 /* No flags */ +#define RHF_QUICKSTART (1 << 0) /* Use quickstart */ +#define RHF_NOTPOT (1 << 1) /* Hash size not power of 2 */ +#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) /* Ignore LD_LIBRARY_PATH */ +#define RHF_NO_MOVE (1 << 3) +#define RHF_SGI_ONLY (1 << 4) +#define RHF_GUARANTEE_INIT (1 << 5) +#define RHF_DELTA_C_PLUS_PLUS (1 << 6) +#define RHF_GUARANTEE_START_INIT (1 << 7) +#define RHF_PIXIE (1 << 8) +#define RHF_DEFAULT_DELAY_LOAD (1 << 9) +#define RHF_REQUICKSTART (1 << 10) +#define RHF_REQUICKSTARTED (1 << 11) +#define RHF_CORD (1 << 12) +#define RHF_NO_UNRES_UNDEF (1 << 13) +#define RHF_RLD_ORDER_SAFE (1 << 14) + +/* Entries found in sections of type SHT_MIPS_LIBLIST. */ + +typedef struct +{ + Elf32_Word l_name; /* Name (string table index) */ + Elf32_Word l_time_stamp; /* Timestamp */ + Elf32_Word l_checksum; /* Checksum */ + Elf32_Word l_version; /* Interface version */ + Elf32_Word l_flags; /* Flags */ +} Elf32_Lib; + +typedef struct +{ + Elf64_Word l_name; /* Name (string table index) */ + Elf64_Word l_time_stamp; /* Timestamp */ + Elf64_Word l_checksum; /* Checksum */ + Elf64_Word l_version; /* Interface version */ + Elf64_Word l_flags; /* Flags */ +} Elf64_Lib; + + +/* Legal values for l_flags. */ + +#define LL_NONE 0 +#define LL_EXACT_MATCH (1 << 0) /* Require exact match */ +#define LL_IGNORE_INT_VER (1 << 1) /* Ignore interface version */ +#define LL_REQUIRE_MINOR (1 << 2) +#define LL_EXPORTS (1 << 3) +#define LL_DELAY_LOAD (1 << 4) +#define LL_DELTA (1 << 5) + +/* Entries found in sections of type SHT_MIPS_CONFLICT. */ + +typedef Elf32_Addr Elf32_Conflict; + + +/* HPPA specific definitions. */ + +/* Legal values for e_flags field of Elf32_Ehdr. */ + +#define EF_PARISC_TRAPNIL 0x00010000 /* Trap nil pointer dereference. */ +#define EF_PARISC_EXT 0x00020000 /* Program uses arch. extensions. */ +#define EF_PARISC_LSB 0x00040000 /* Program expects little endian. */ +#define EF_PARISC_WIDE 0x00080000 /* Program expects wide mode. */ +#define EF_PARISC_NO_KABP 0x00100000 /* No kernel assisted branch + prediction. */ +#define EF_PARISC_LAZYSWAP 0x00400000 /* Allow lazy swapping. */ +#define EF_PARISC_ARCH 0x0000ffff /* Architecture version. */ + +/* Defined values for `e_flags & EF_PARISC_ARCH' are: */ + +#define EFA_PARISC_1_0 0x020b /* PA-RISC 1.0 big-endian. */ +#define EFA_PARISC_1_1 0x0210 /* PA-RISC 1.1 big-endian. */ +#define EFA_PARISC_2_0 0x0214 /* PA-RISC 2.0 big-endian. */ + +/* Additional section indices. */ + +#define SHN_PARISC_ANSI_COMMON 0xff00 /* Section for tentatively declared + symbols in ANSI C. */ +#define SHN_PARISC_HUGE_COMMON 0xff01 /* Common blocks in huge model. */ + +/* Legal values for sh_type field of Elf32_Shdr. */ + +#define SHT_PARISC_EXT 0x70000000 /* Contains product specific ext. */ +#define SHT_PARISC_UNWIND 0x70000001 /* Unwind information. */ +#define SHT_PARISC_DOC 0x70000002 /* Debug info for optimized code. */ + +/* Legal values for sh_flags field of Elf32_Shdr. */ + +#define SHF_PARISC_SHORT 0x20000000 /* Section with short addressing. */ +#define SHF_PARISC_HUGE 0x40000000 /* Section far from gp. */ +#define SHF_PARISC_SBP 0x80000000 /* Static branch prediction code. */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_PARISC_MILLICODE 13 /* Millicode function entry point. */ + +#define STT_HP_OPAQUE (STT_LOOS + 0x1) +#define STT_HP_STUB (STT_LOOS + 0x2) + +/* HPPA relocs. */ + +#define R_PARISC_NONE 0 /* No reloc. */ +#define R_PARISC_DIR32 1 /* Direct 32-bit reference. */ +#define R_PARISC_DIR21L 2 /* Left 21 bits of eff. address. */ +#define R_PARISC_DIR17R 3 /* Right 17 bits of eff. address. */ +#define R_PARISC_DIR17F 4 /* 17 bits of eff. address. */ +#define R_PARISC_DIR14R 6 /* Right 14 bits of eff. address. */ +#define R_PARISC_PCREL32 9 /* 32-bit rel. address. */ +#define R_PARISC_PCREL21L 10 /* Left 21 bits of rel. address. */ +#define R_PARISC_PCREL17R 11 /* Right 17 bits of rel. address. */ +#define R_PARISC_PCREL17F 12 /* 17 bits of rel. address. */ +#define R_PARISC_PCREL14R 14 /* Right 14 bits of rel. address. */ +#define R_PARISC_DPREL21L 18 /* Left 21 bits of rel. address. */ +#define R_PARISC_DPREL14R 22 /* Right 14 bits of rel. address. */ +#define R_PARISC_GPREL21L 26 /* GP-relative, left 21 bits. */ +#define R_PARISC_GPREL14R 30 /* GP-relative, right 14 bits. */ +#define R_PARISC_LTOFF21L 34 /* LT-relative, left 21 bits. */ +#define R_PARISC_LTOFF14R 38 /* LT-relative, right 14 bits. */ +#define R_PARISC_SECREL32 41 /* 32 bits section rel. address. */ +#define R_PARISC_SEGBASE 48 /* No relocation, set segment base. */ +#define R_PARISC_SEGREL32 49 /* 32 bits segment rel. address. */ +#define R_PARISC_PLTOFF21L 50 /* PLT rel. address, left 21 bits. */ +#define R_PARISC_PLTOFF14R 54 /* PLT rel. address, right 14 bits. */ +#define R_PARISC_LTOFF_FPTR32 57 /* 32 bits LT-rel. function pointer. */ +#define R_PARISC_LTOFF_FPTR21L 58 /* LT-rel. fct ptr, left 21 bits. */ +#define R_PARISC_LTOFF_FPTR14R 62 /* LT-rel. fct ptr, right 14 bits. */ +#define R_PARISC_FPTR64 64 /* 64 bits function address. */ +#define R_PARISC_PLABEL32 65 /* 32 bits function address. */ +#define R_PARISC_PLABEL21L 66 /* Left 21 bits of fdesc address. */ +#define R_PARISC_PLABEL14R 70 /* Right 14 bits of fdesc address. */ +#define R_PARISC_PCREL64 72 /* 64 bits PC-rel. address. */ +#define R_PARISC_PCREL22F 74 /* 22 bits PC-rel. address. */ +#define R_PARISC_PCREL14WR 75 /* PC-rel. address, right 14 bits. */ +#define R_PARISC_PCREL14DR 76 /* PC rel. address, right 14 bits. */ +#define R_PARISC_PCREL16F 77 /* 16 bits PC-rel. address. */ +#define R_PARISC_PCREL16WF 78 /* 16 bits PC-rel. address. */ +#define R_PARISC_PCREL16DF 79 /* 16 bits PC-rel. address. */ +#define R_PARISC_DIR64 80 /* 64 bits of eff. address. */ +#define R_PARISC_DIR14WR 83 /* 14 bits of eff. address. */ +#define R_PARISC_DIR14DR 84 /* 14 bits of eff. address. */ +#define R_PARISC_DIR16F 85 /* 16 bits of eff. address. */ +#define R_PARISC_DIR16WF 86 /* 16 bits of eff. address. */ +#define R_PARISC_DIR16DF 87 /* 16 bits of eff. address. */ +#define R_PARISC_GPREL64 88 /* 64 bits of GP-rel. address. */ +#define R_PARISC_GPREL14WR 91 /* GP-rel. address, right 14 bits. */ +#define R_PARISC_GPREL14DR 92 /* GP-rel. address, right 14 bits. */ +#define R_PARISC_GPREL16F 93 /* 16 bits GP-rel. address. */ +#define R_PARISC_GPREL16WF 94 /* 16 bits GP-rel. address. */ +#define R_PARISC_GPREL16DF 95 /* 16 bits GP-rel. address. */ +#define R_PARISC_LTOFF64 96 /* 64 bits LT-rel. address. */ +#define R_PARISC_LTOFF14WR 99 /* LT-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF14DR 100 /* LT-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF16F 101 /* 16 bits LT-rel. address. */ +#define R_PARISC_LTOFF16WF 102 /* 16 bits LT-rel. address. */ +#define R_PARISC_LTOFF16DF 103 /* 16 bits LT-rel. address. */ +#define R_PARISC_SECREL64 104 /* 64 bits section rel. address. */ +#define R_PARISC_SEGREL64 112 /* 64 bits segment rel. address. */ +#define R_PARISC_PLTOFF14WR 115 /* PLT-rel. address, right 14 bits. */ +#define R_PARISC_PLTOFF14DR 116 /* PLT-rel. address, right 14 bits. */ +#define R_PARISC_PLTOFF16F 117 /* 16 bits LT-rel. address. */ +#define R_PARISC_PLTOFF16WF 118 /* 16 bits PLT-rel. address. */ +#define R_PARISC_PLTOFF16DF 119 /* 16 bits PLT-rel. address. */ +#define R_PARISC_LTOFF_FPTR64 120 /* 64 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR14WR 123 /* LT-rel. fct. ptr., right 14 bits. */ +#define R_PARISC_LTOFF_FPTR14DR 124 /* LT-rel. fct. ptr., right 14 bits. */ +#define R_PARISC_LTOFF_FPTR16F 125 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR16WF 126 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR16DF 127 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LORESERVE 128 +#define R_PARISC_COPY 128 /* Copy relocation. */ +#define R_PARISC_IPLT 129 /* Dynamic reloc, imported PLT */ +#define R_PARISC_EPLT 130 /* Dynamic reloc, exported PLT */ +#define R_PARISC_TPREL32 153 /* 32 bits TP-rel. address. */ +#define R_PARISC_TPREL21L 154 /* TP-rel. address, left 21 bits. */ +#define R_PARISC_TPREL14R 158 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF_TP21L 162 /* LT-TP-rel. address, left 21 bits. */ +#define R_PARISC_LTOFF_TP14R 166 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP14F 167 /* 14 bits LT-TP-rel. address. */ +#define R_PARISC_TPREL64 216 /* 64 bits TP-rel. address. */ +#define R_PARISC_TPREL14WR 219 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_TPREL14DR 220 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_TPREL16F 221 /* 16 bits TP-rel. address. */ +#define R_PARISC_TPREL16WF 222 /* 16 bits TP-rel. address. */ +#define R_PARISC_TPREL16DF 223 /* 16 bits TP-rel. address. */ +#define R_PARISC_LTOFF_TP64 224 /* 64 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP14WR 227 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP14DR 228 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP16F 229 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP16WF 230 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP16DF 231 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_GNU_VTENTRY 232 +#define R_PARISC_GNU_VTINHERIT 233 +#define R_PARISC_TLS_GD21L 234 /* GD 21-bit left. */ +#define R_PARISC_TLS_GD14R 235 /* GD 14-bit right. */ +#define R_PARISC_TLS_GDCALL 236 /* GD call to __t_g_a. */ +#define R_PARISC_TLS_LDM21L 237 /* LD module 21-bit left. */ +#define R_PARISC_TLS_LDM14R 238 /* LD module 14-bit right. */ +#define R_PARISC_TLS_LDMCALL 239 /* LD module call to __t_g_a. */ +#define R_PARISC_TLS_LDO21L 240 /* LD offset 21-bit left. */ +#define R_PARISC_TLS_LDO14R 241 /* LD offset 14-bit right. */ +#define R_PARISC_TLS_DTPMOD32 242 /* DTP module 32-bit. */ +#define R_PARISC_TLS_DTPMOD64 243 /* DTP module 64-bit. */ +#define R_PARISC_TLS_DTPOFF32 244 /* DTP offset 32-bit. */ +#define R_PARISC_TLS_DTPOFF64 245 /* DTP offset 32-bit. */ +#define R_PARISC_TLS_LE21L R_PARISC_TPREL21L +#define R_PARISC_TLS_LE14R R_PARISC_TPREL14R +#define R_PARISC_TLS_IE21L R_PARISC_LTOFF_TP21L +#define R_PARISC_TLS_IE14R R_PARISC_LTOFF_TP14R +#define R_PARISC_TLS_TPREL32 R_PARISC_TPREL32 +#define R_PARISC_TLS_TPREL64 R_PARISC_TPREL64 +#define R_PARISC_HIRESERVE 255 + +/* Legal values for p_type field of Elf32_Phdr/Elf64_Phdr. */ + +#define PT_HP_TLS (PT_LOOS + 0x0) +#define PT_HP_CORE_NONE (PT_LOOS + 0x1) +#define PT_HP_CORE_VERSION (PT_LOOS + 0x2) +#define PT_HP_CORE_KERNEL (PT_LOOS + 0x3) +#define PT_HP_CORE_COMM (PT_LOOS + 0x4) +#define PT_HP_CORE_PROC (PT_LOOS + 0x5) +#define PT_HP_CORE_LOADABLE (PT_LOOS + 0x6) +#define PT_HP_CORE_STACK (PT_LOOS + 0x7) +#define PT_HP_CORE_SHM (PT_LOOS + 0x8) +#define PT_HP_CORE_MMF (PT_LOOS + 0x9) +#define PT_HP_PARALLEL (PT_LOOS + 0x10) +#define PT_HP_FASTBIND (PT_LOOS + 0x11) +#define PT_HP_OPT_ANNOT (PT_LOOS + 0x12) +#define PT_HP_HSL_ANNOT (PT_LOOS + 0x13) +#define PT_HP_STACK (PT_LOOS + 0x14) + +#define PT_PARISC_ARCHEXT 0x70000000 +#define PT_PARISC_UNWIND 0x70000001 + +/* Legal values for p_flags field of Elf32_Phdr/Elf64_Phdr. */ + +#define PF_PARISC_SBP 0x08000000 + +#define PF_HP_PAGE_SIZE 0x00100000 +#define PF_HP_FAR_SHARED 0x00200000 +#define PF_HP_NEAR_SHARED 0x00400000 +#define PF_HP_CODE 0x01000000 +#define PF_HP_MODIFY 0x02000000 +#define PF_HP_LAZYSWAP 0x04000000 +#define PF_HP_SBP 0x08000000 + + +/* Alpha specific definitions. */ + +/* Legal values for e_flags field of Elf64_Ehdr. */ + +#define EF_ALPHA_32BIT 1 /* All addresses must be < 2GB. */ +#define EF_ALPHA_CANRELAX 2 /* Relocations for relaxing exist. */ + +/* Legal values for sh_type field of Elf64_Shdr. */ + +/* These two are primarily concerned with ECOFF debugging info. */ +#define SHT_ALPHA_DEBUG 0x70000001 +#define SHT_ALPHA_REGINFO 0x70000002 + +/* Legal values for sh_flags field of Elf64_Shdr. */ + +#define SHF_ALPHA_GPREL 0x10000000 + +/* Legal values for st_other field of Elf64_Sym. */ +#define STO_ALPHA_NOPV 0x80 /* No PV required. */ +#define STO_ALPHA_STD_GPLOAD 0x88 /* PV only used for initial ldgp. */ + +/* Alpha relocs. */ + +#define R_ALPHA_NONE 0 /* No reloc */ +#define R_ALPHA_REFLONG 1 /* Direct 32 bit */ +#define R_ALPHA_REFQUAD 2 /* Direct 64 bit */ +#define R_ALPHA_GPREL32 3 /* GP relative 32 bit */ +#define R_ALPHA_LITERAL 4 /* GP relative 16 bit w/optimization */ +#define R_ALPHA_LITUSE 5 /* Optimization hint for LITERAL */ +#define R_ALPHA_GPDISP 6 /* Add displacement to GP */ +#define R_ALPHA_BRADDR 7 /* PC+4 relative 23 bit shifted */ +#define R_ALPHA_HINT 8 /* PC+4 relative 16 bit shifted */ +#define R_ALPHA_SREL16 9 /* PC relative 16 bit */ +#define R_ALPHA_SREL32 10 /* PC relative 32 bit */ +#define R_ALPHA_SREL64 11 /* PC relative 64 bit */ +#define R_ALPHA_GPRELHIGH 17 /* GP relative 32 bit, high 16 bits */ +#define R_ALPHA_GPRELLOW 18 /* GP relative 32 bit, low 16 bits */ +#define R_ALPHA_GPREL16 19 /* GP relative 16 bit */ +#define R_ALPHA_COPY 24 /* Copy symbol at runtime */ +#define R_ALPHA_GLOB_DAT 25 /* Create GOT entry */ +#define R_ALPHA_JMP_SLOT 26 /* Create PLT entry */ +#define R_ALPHA_RELATIVE 27 /* Adjust by program base */ +#define R_ALPHA_TLS_GD_HI 28 +#define R_ALPHA_TLSGD 29 +#define R_ALPHA_TLS_LDM 30 +#define R_ALPHA_DTPMOD64 31 +#define R_ALPHA_GOTDTPREL 32 +#define R_ALPHA_DTPREL64 33 +#define R_ALPHA_DTPRELHI 34 +#define R_ALPHA_DTPRELLO 35 +#define R_ALPHA_DTPREL16 36 +#define R_ALPHA_GOTTPREL 37 +#define R_ALPHA_TPREL64 38 +#define R_ALPHA_TPRELHI 39 +#define R_ALPHA_TPRELLO 40 +#define R_ALPHA_TPREL16 41 +/* Keep this the last entry. */ +#define R_ALPHA_NUM 46 + +/* Magic values of the LITUSE relocation addend. */ +#define LITUSE_ALPHA_ADDR 0 +#define LITUSE_ALPHA_BASE 1 +#define LITUSE_ALPHA_BYTOFF 2 +#define LITUSE_ALPHA_JSR 3 +#define LITUSE_ALPHA_TLS_GD 4 +#define LITUSE_ALPHA_TLS_LDM 5 + +/* Legal values for d_tag of Elf64_Dyn. */ +#define DT_ALPHA_PLTRO (DT_LOPROC + 0) +#define DT_ALPHA_NUM 1 + +/* PowerPC specific declarations */ + +/* Values for Elf32/64_Ehdr.e_flags. */ +#define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */ + +/* Cygnus local bits below */ +#define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/ +#define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib + flag */ + +/* PowerPC relocations defined by the ABIs */ +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 /* 32bit absolute address */ +#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */ +#define R_PPC_ADDR16 3 /* 16bit absolute address */ +#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */ +#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */ +#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */ +#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */ +#define R_PPC_ADDR14_BRTAKEN 8 +#define R_PPC_ADDR14_BRNTAKEN 9 +#define R_PPC_REL24 10 /* PC relative 26 bit */ +#define R_PPC_REL14 11 /* PC relative 16 bit */ +#define R_PPC_REL14_BRTAKEN 12 +#define R_PPC_REL14_BRNTAKEN 13 +#define R_PPC_GOT16 14 +#define R_PPC_GOT16_LO 15 +#define R_PPC_GOT16_HI 16 +#define R_PPC_GOT16_HA 17 +#define R_PPC_PLTREL24 18 +#define R_PPC_COPY 19 +#define R_PPC_GLOB_DAT 20 +#define R_PPC_JMP_SLOT 21 +#define R_PPC_RELATIVE 22 +#define R_PPC_LOCAL24PC 23 +#define R_PPC_UADDR32 24 +#define R_PPC_UADDR16 25 +#define R_PPC_REL32 26 +#define R_PPC_PLT32 27 +#define R_PPC_PLTREL32 28 +#define R_PPC_PLT16_LO 29 +#define R_PPC_PLT16_HI 30 +#define R_PPC_PLT16_HA 31 +#define R_PPC_SDAREL16 32 +#define R_PPC_SECTOFF 33 +#define R_PPC_SECTOFF_LO 34 +#define R_PPC_SECTOFF_HI 35 +#define R_PPC_SECTOFF_HA 36 + +/* PowerPC relocations defined for the TLS access ABI. */ +#define R_PPC_TLS 67 /* none (sym+add)@tls */ +#define R_PPC_DTPMOD32 68 /* word32 (sym+add)@dtpmod */ +#define R_PPC_TPREL16 69 /* half16* (sym+add)@tprel */ +#define R_PPC_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */ +#define R_PPC_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */ +#define R_PPC_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */ +#define R_PPC_TPREL32 73 /* word32 (sym+add)@tprel */ +#define R_PPC_DTPREL16 74 /* half16* (sym+add)@dtprel */ +#define R_PPC_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */ +#define R_PPC_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */ +#define R_PPC_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */ +#define R_PPC_DTPREL32 78 /* word32 (sym+add)@dtprel */ +#define R_PPC_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */ +#define R_PPC_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */ +#define R_PPC_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */ +#define R_PPC_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */ +#define R_PPC_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */ +#define R_PPC_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */ +#define R_PPC_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */ +#define R_PPC_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */ +#define R_PPC_GOT_TPREL16 87 /* half16* (sym+add)@got@tprel */ +#define R_PPC_GOT_TPREL16_LO 88 /* half16 (sym+add)@got@tprel@l */ +#define R_PPC_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */ +#define R_PPC_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */ +#define R_PPC_GOT_DTPREL16 91 /* half16* (sym+add)@got@dtprel */ +#define R_PPC_GOT_DTPREL16_LO 92 /* half16* (sym+add)@got@dtprel@l */ +#define R_PPC_GOT_DTPREL16_HI 93 /* half16* (sym+add)@got@dtprel@h */ +#define R_PPC_GOT_DTPREL16_HA 94 /* half16* (sym+add)@got@dtprel@ha */ + +/* The remaining relocs are from the Embedded ELF ABI, and are not + in the SVR4 ELF ABI. */ +#define R_PPC_EMB_NADDR32 101 +#define R_PPC_EMB_NADDR16 102 +#define R_PPC_EMB_NADDR16_LO 103 +#define R_PPC_EMB_NADDR16_HI 104 +#define R_PPC_EMB_NADDR16_HA 105 +#define R_PPC_EMB_SDAI16 106 +#define R_PPC_EMB_SDA2I16 107 +#define R_PPC_EMB_SDA2REL 108 +#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */ +#define R_PPC_EMB_MRKREF 110 +#define R_PPC_EMB_RELSEC16 111 +#define R_PPC_EMB_RELST_LO 112 +#define R_PPC_EMB_RELST_HI 113 +#define R_PPC_EMB_RELST_HA 114 +#define R_PPC_EMB_BIT_FLD 115 +#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */ + +/* Diab tool relocations. */ +#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */ +#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */ +#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */ +#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */ +#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */ +#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */ + +/* GNU extension to support local ifunc. */ +#define R_PPC_IRELATIVE 248 + +/* GNU relocs used in PIC code sequences. */ +#define R_PPC_REL16 249 /* half16 (sym+add-.) */ +#define R_PPC_REL16_LO 250 /* half16 (sym+add-.)@l */ +#define R_PPC_REL16_HI 251 /* half16 (sym+add-.)@h */ +#define R_PPC_REL16_HA 252 /* half16 (sym+add-.)@ha */ + +/* This is a phony reloc to handle any old fashioned TOC16 references + that may still be in object files. */ +#define R_PPC_TOC16 255 + +/* PowerPC specific values for the Dyn d_tag field. */ +#define DT_PPC_GOT (DT_LOPROC + 0) +#define DT_PPC_NUM 1 + +/* PowerPC64 relocations defined by the ABIs */ +#define R_PPC64_NONE R_PPC_NONE +#define R_PPC64_ADDR32 R_PPC_ADDR32 /* 32bit absolute address */ +#define R_PPC64_ADDR24 R_PPC_ADDR24 /* 26bit address, word aligned */ +#define R_PPC64_ADDR16 R_PPC_ADDR16 /* 16bit absolute address */ +#define R_PPC64_ADDR16_LO R_PPC_ADDR16_LO /* lower 16bits of address */ +#define R_PPC64_ADDR16_HI R_PPC_ADDR16_HI /* high 16bits of address. */ +#define R_PPC64_ADDR16_HA R_PPC_ADDR16_HA /* adjusted high 16bits. */ +#define R_PPC64_ADDR14 R_PPC_ADDR14 /* 16bit address, word aligned */ +#define R_PPC64_ADDR14_BRTAKEN R_PPC_ADDR14_BRTAKEN +#define R_PPC64_ADDR14_BRNTAKEN R_PPC_ADDR14_BRNTAKEN +#define R_PPC64_REL24 R_PPC_REL24 /* PC-rel. 26 bit, word aligned */ +#define R_PPC64_REL14 R_PPC_REL14 /* PC relative 16 bit */ +#define R_PPC64_REL14_BRTAKEN R_PPC_REL14_BRTAKEN +#define R_PPC64_REL14_BRNTAKEN R_PPC_REL14_BRNTAKEN +#define R_PPC64_GOT16 R_PPC_GOT16 +#define R_PPC64_GOT16_LO R_PPC_GOT16_LO +#define R_PPC64_GOT16_HI R_PPC_GOT16_HI +#define R_PPC64_GOT16_HA R_PPC_GOT16_HA + +#define R_PPC64_COPY R_PPC_COPY +#define R_PPC64_GLOB_DAT R_PPC_GLOB_DAT +#define R_PPC64_JMP_SLOT R_PPC_JMP_SLOT +#define R_PPC64_RELATIVE R_PPC_RELATIVE + +#define R_PPC64_UADDR32 R_PPC_UADDR32 +#define R_PPC64_UADDR16 R_PPC_UADDR16 +#define R_PPC64_REL32 R_PPC_REL32 +#define R_PPC64_PLT32 R_PPC_PLT32 +#define R_PPC64_PLTREL32 R_PPC_PLTREL32 +#define R_PPC64_PLT16_LO R_PPC_PLT16_LO +#define R_PPC64_PLT16_HI R_PPC_PLT16_HI +#define R_PPC64_PLT16_HA R_PPC_PLT16_HA + +#define R_PPC64_SECTOFF R_PPC_SECTOFF +#define R_PPC64_SECTOFF_LO R_PPC_SECTOFF_LO +#define R_PPC64_SECTOFF_HI R_PPC_SECTOFF_HI +#define R_PPC64_SECTOFF_HA R_PPC_SECTOFF_HA +#define R_PPC64_ADDR30 37 /* word30 (S + A - P) >> 2 */ +#define R_PPC64_ADDR64 38 /* doubleword64 S + A */ +#define R_PPC64_ADDR16_HIGHER 39 /* half16 #higher(S + A) */ +#define R_PPC64_ADDR16_HIGHERA 40 /* half16 #highera(S + A) */ +#define R_PPC64_ADDR16_HIGHEST 41 /* half16 #highest(S + A) */ +#define R_PPC64_ADDR16_HIGHESTA 42 /* half16 #highesta(S + A) */ +#define R_PPC64_UADDR64 43 /* doubleword64 S + A */ +#define R_PPC64_REL64 44 /* doubleword64 S + A - P */ +#define R_PPC64_PLT64 45 /* doubleword64 L + A */ +#define R_PPC64_PLTREL64 46 /* doubleword64 L + A - P */ +#define R_PPC64_TOC16 47 /* half16* S + A - .TOC */ +#define R_PPC64_TOC16_LO 48 /* half16 #lo(S + A - .TOC.) */ +#define R_PPC64_TOC16_HI 49 /* half16 #hi(S + A - .TOC.) */ +#define R_PPC64_TOC16_HA 50 /* half16 #ha(S + A - .TOC.) */ +#define R_PPC64_TOC 51 /* doubleword64 .TOC */ +#define R_PPC64_PLTGOT16 52 /* half16* M + A */ +#define R_PPC64_PLTGOT16_LO 53 /* half16 #lo(M + A) */ +#define R_PPC64_PLTGOT16_HI 54 /* half16 #hi(M + A) */ +#define R_PPC64_PLTGOT16_HA 55 /* half16 #ha(M + A) */ + +#define R_PPC64_ADDR16_DS 56 /* half16ds* (S + A) >> 2 */ +#define R_PPC64_ADDR16_LO_DS 57 /* half16ds #lo(S + A) >> 2 */ +#define R_PPC64_GOT16_DS 58 /* half16ds* (G + A) >> 2 */ +#define R_PPC64_GOT16_LO_DS 59 /* half16ds #lo(G + A) >> 2 */ +#define R_PPC64_PLT16_LO_DS 60 /* half16ds #lo(L + A) >> 2 */ +#define R_PPC64_SECTOFF_DS 61 /* half16ds* (R + A) >> 2 */ +#define R_PPC64_SECTOFF_LO_DS 62 /* half16ds #lo(R + A) >> 2 */ +#define R_PPC64_TOC16_DS 63 /* half16ds* (S + A - .TOC.) >> 2 */ +#define R_PPC64_TOC16_LO_DS 64 /* half16ds #lo(S + A - .TOC.) >> 2 */ +#define R_PPC64_PLTGOT16_DS 65 /* half16ds* (M + A) >> 2 */ +#define R_PPC64_PLTGOT16_LO_DS 66 /* half16ds #lo(M + A) >> 2 */ + +/* PowerPC64 relocations defined for the TLS access ABI. */ +#define R_PPC64_TLS 67 /* none (sym+add)@tls */ +#define R_PPC64_DTPMOD64 68 /* doubleword64 (sym+add)@dtpmod */ +#define R_PPC64_TPREL16 69 /* half16* (sym+add)@tprel */ +#define R_PPC64_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */ +#define R_PPC64_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */ +#define R_PPC64_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */ +#define R_PPC64_TPREL64 73 /* doubleword64 (sym+add)@tprel */ +#define R_PPC64_DTPREL16 74 /* half16* (sym+add)@dtprel */ +#define R_PPC64_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */ +#define R_PPC64_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */ +#define R_PPC64_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */ +#define R_PPC64_DTPREL64 78 /* doubleword64 (sym+add)@dtprel */ +#define R_PPC64_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */ +#define R_PPC64_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */ +#define R_PPC64_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */ +#define R_PPC64_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */ +#define R_PPC64_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */ +#define R_PPC64_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */ +#define R_PPC64_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */ +#define R_PPC64_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */ +#define R_PPC64_GOT_TPREL16_DS 87 /* half16ds* (sym+add)@got@tprel */ +#define R_PPC64_GOT_TPREL16_LO_DS 88 /* half16ds (sym+add)@got@tprel@l */ +#define R_PPC64_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */ +#define R_PPC64_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */ +#define R_PPC64_GOT_DTPREL16_DS 91 /* half16ds* (sym+add)@got@dtprel */ +#define R_PPC64_GOT_DTPREL16_LO_DS 92 /* half16ds (sym+add)@got@dtprel@l */ +#define R_PPC64_GOT_DTPREL16_HI 93 /* half16 (sym+add)@got@dtprel@h */ +#define R_PPC64_GOT_DTPREL16_HA 94 /* half16 (sym+add)@got@dtprel@ha */ +#define R_PPC64_TPREL16_DS 95 /* half16ds* (sym+add)@tprel */ +#define R_PPC64_TPREL16_LO_DS 96 /* half16ds (sym+add)@tprel@l */ +#define R_PPC64_TPREL16_HIGHER 97 /* half16 (sym+add)@tprel@higher */ +#define R_PPC64_TPREL16_HIGHERA 98 /* half16 (sym+add)@tprel@highera */ +#define R_PPC64_TPREL16_HIGHEST 99 /* half16 (sym+add)@tprel@highest */ +#define R_PPC64_TPREL16_HIGHESTA 100 /* half16 (sym+add)@tprel@highesta */ +#define R_PPC64_DTPREL16_DS 101 /* half16ds* (sym+add)@dtprel */ +#define R_PPC64_DTPREL16_LO_DS 102 /* half16ds (sym+add)@dtprel@l */ +#define R_PPC64_DTPREL16_HIGHER 103 /* half16 (sym+add)@dtprel@higher */ +#define R_PPC64_DTPREL16_HIGHERA 104 /* half16 (sym+add)@dtprel@highera */ +#define R_PPC64_DTPREL16_HIGHEST 105 /* half16 (sym+add)@dtprel@highest */ +#define R_PPC64_DTPREL16_HIGHESTA 106 /* half16 (sym+add)@dtprel@highesta */ + +/* GNU extension to support local ifunc. */ +#define R_PPC64_JMP_IREL 247 +#define R_PPC64_IRELATIVE 248 +#define R_PPC64_REL16 249 /* half16 (sym+add-.) */ +#define R_PPC64_REL16_LO 250 /* half16 (sym+add-.)@l */ +#define R_PPC64_REL16_HI 251 /* half16 (sym+add-.)@h */ +#define R_PPC64_REL16_HA 252 /* half16 (sym+add-.)@ha */ + +/* PowerPC64 specific values for the Dyn d_tag field. */ +#define DT_PPC64_GLINK (DT_LOPROC + 0) +#define DT_PPC64_OPD (DT_LOPROC + 1) +#define DT_PPC64_OPDSZ (DT_LOPROC + 2) +#define DT_PPC64_NUM 3 + + +/* ARM specific declarations */ + +/* Processor specific flags for the ELF header e_flags field. */ +#define EF_ARM_RELEXEC 0x01 +#define EF_ARM_HASENTRY 0x02 +#define EF_ARM_INTERWORK 0x04 +#define EF_ARM_APCS_26 0x08 +#define EF_ARM_APCS_FLOAT 0x10 +#define EF_ARM_PIC 0x20 +#define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */ +#define EF_ARM_NEW_ABI 0x80 +#define EF_ARM_OLD_ABI 0x100 +#define EF_ARM_SOFT_FLOAT 0x200 +#define EF_ARM_VFP_FLOAT 0x400 +#define EF_ARM_MAVERICK_FLOAT 0x800 + +#define EF_ARM_ABI_FLOAT_SOFT 0x200 /* NB conflicts with EF_ARM_SOFT_FLOAT */ +#define EF_ARM_ABI_FLOAT_HARD 0x400 /* NB conflicts with EF_ARM_VFP_FLOAT */ + + +/* Other constants defined in the ARM ELF spec. version B-01. */ +/* NB. These conflict with values defined above. */ +#define EF_ARM_SYMSARESORTED 0x04 +#define EF_ARM_DYNSYMSUSESEGIDX 0x08 +#define EF_ARM_MAPSYMSFIRST 0x10 +#define EF_ARM_EABIMASK 0XFF000000 + +/* Constants defined in AAELF. */ +#define EF_ARM_BE8 0x00800000 +#define EF_ARM_LE8 0x00400000 + +#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK) +#define EF_ARM_EABI_UNKNOWN 0x00000000 +#define EF_ARM_EABI_VER1 0x01000000 +#define EF_ARM_EABI_VER2 0x02000000 +#define EF_ARM_EABI_VER3 0x03000000 +#define EF_ARM_EABI_VER4 0x04000000 +#define EF_ARM_EABI_VER5 0x05000000 + +/* Additional symbol types for Thumb. */ +#define STT_ARM_TFUNC STT_LOPROC /* A Thumb function. */ +#define STT_ARM_16BIT STT_HIPROC /* A Thumb label. */ + +/* ARM-specific values for sh_flags */ +#define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */ +#define SHF_ARM_COMDEF 0x80000000 /* Section may be multiply defined + in the input to a link step. */ + +/* ARM-specific program header flags */ +#define PF_ARM_SB 0x10000000 /* Segment contains the location + addressed by the static base. */ +#define PF_ARM_PI 0x20000000 /* Position-independent segment. */ +#define PF_ARM_ABS 0x40000000 /* Absolute segment. */ + +/* Processor specific values for the Phdr p_type field. */ +#define PT_ARM_EXIDX (PT_LOPROC + 1) /* ARM unwind segment. */ + +/* Processor specific values for the Shdr sh_type field. */ +#define SHT_ARM_EXIDX (SHT_LOPROC + 1) /* ARM unwind section. */ +#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) /* Preemption details. */ +#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) /* ARM attributes section. */ + + +/* AArch64 relocs. */ + +#define R_AARCH64_NONE 0 /* No relocation. */ +#define R_AARCH64_ABS64 257 /* Direct 64 bit. */ +#define R_AARCH64_ABS32 258 /* Direct 32 bit. */ +#define R_AARCH64_ABS16 259 /* Direct 16-bit. */ +#define R_AARCH64_PREL64 260 /* PC-relative 64-bit. */ +#define R_AARCH64_PREL32 261 /* PC-relative 32-bit. */ +#define R_AARCH64_PREL16 262 /* PC-relative 16-bit. */ +#define R_AARCH64_MOVW_UABS_G0 263 /* Dir. MOVZ imm. from bits 15:0. */ +#define R_AARCH64_MOVW_UABS_G0_NC 264 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_UABS_G1 265 /* Dir. MOVZ imm. from bits 31:16. */ +#define R_AARCH64_MOVW_UABS_G1_NC 266 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_UABS_G2 267 /* Dir. MOVZ imm. from bits 47:32. */ +#define R_AARCH64_MOVW_UABS_G2_NC 268 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_UABS_G3 269 /* Dir. MOV{K,Z} imm. from 63:48. */ +#define R_AARCH64_MOVW_SABS_G0 270 /* Dir. MOV{N,Z} imm. from 15:0. */ +#define R_AARCH64_MOVW_SABS_G1 271 /* Dir. MOV{N,Z} imm. from 31:16. */ +#define R_AARCH64_MOVW_SABS_G2 272 /* Dir. MOV{N,Z} imm. from 47:32. */ +#define R_AARCH64_LD_PREL_LO19 273 /* PC-rel. LD imm. from bits 20:2. */ +#define R_AARCH64_ADR_PREL_LO21 274 /* PC-rel. ADR imm. from bits 20:0. */ +#define R_AARCH64_ADR_PREL_PG_HI21 275 /* Page-rel. ADRP imm. from 32:12. */ +#define R_AARCH64_ADR_PREL_PG_HI21_NC 276 /* Likewise; no overflow check. */ +#define R_AARCH64_ADD_ABS_LO12_NC 277 /* Dir. ADD imm. from bits 11:0. */ +#define R_AARCH64_LDST8_ABS_LO12_NC 278 /* Likewise for LD/ST; no check. */ +#define R_AARCH64_TSTBR14 279 /* PC-rel. TBZ/TBNZ imm. from 15:2. */ +#define R_AARCH64_CONDBR19 280 /* PC-rel. cond. br. imm. from 20:2. */ +#define R_AARCH64_JUMP26 282 /* PC-rel. B imm. from bits 27:2. */ +#define R_AARCH64_CALL26 283 /* Likewise for CALL. */ +#define R_AARCH64_LDST16_ABS_LO12_NC 284 /* Dir. ADD imm. from bits 11:1. */ +#define R_AARCH64_LDST32_ABS_LO12_NC 285 /* Likewise for bits 11:2. */ +#define R_AARCH64_LDST64_ABS_LO12_NC 286 /* Likewise for bits 11:3. */ +#define R_AARCH64_MOVW_PREL_G0 287 /* PC-rel. MOV{N,Z} imm. from 15:0. */ +#define R_AARCH64_MOVW_PREL_G0_NC 288 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_PREL_G1 289 /* PC-rel. MOV{N,Z} imm. from 31:16. */ +#define R_AARCH64_MOVW_PREL_G1_NC 290 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_PREL_G2 291 /* PC-rel. MOV{N,Z} imm. from 47:32. */ +#define R_AARCH64_MOVW_PREL_G2_NC 292 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_PREL_G3 293 /* PC-rel. MOV{N,Z} imm. from 63:48. */ +#define R_AARCH64_LDST128_ABS_LO12_NC 299 /* Dir. ADD imm. from bits 11:4. */ +#define R_AARCH64_MOVW_GOTOFF_G0 300 /* GOT-rel. off. MOV{N,Z} imm. 15:0. */ +#define R_AARCH64_MOVW_GOTOFF_G0_NC 301 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_GOTOFF_G1 302 /* GOT-rel. o. MOV{N,Z} imm. 31:16. */ +#define R_AARCH64_MOVW_GOTOFF_G1_NC 303 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_GOTOFF_G2 304 /* GOT-rel. o. MOV{N,Z} imm. 47:32. */ +#define R_AARCH64_MOVW_GOTOFF_G2_NC 305 /* Likewise for MOVK; no check. */ +#define R_AARCH64_MOVW_GOTOFF_G3 306 /* GOT-rel. o. MOV{N,Z} imm. 63:48. */ +#define R_AARCH64_GOTREL64 307 /* GOT-relative 64-bit. */ +#define R_AARCH64_GOTREL32 308 /* GOT-relative 32-bit. */ +#define R_AARCH64_GOT_LD_PREL19 309 /* PC-rel. GOT off. load imm. 20:2. */ +#define R_AARCH64_LD64_GOTOFF_LO15 310 /* GOT-rel. off. LD/ST imm. 14:3. */ +#define R_AARCH64_ADR_GOT_PAGE 311 /* P-page-rel. GOT off. ADRP 32:12. */ +#define R_AARCH64_LD64_GOT_LO12_NC 312 /* Dir. GOT off. LD/ST imm. 11:3. */ +#define R_AARCH64_LD64_GOTPAGE_LO15 313 /* GOT-page-rel. GOT off. LD/ST 14:3 */ +#define R_AARCH64_TLSGD_ADR_PREL21 512 /* PC-relative ADR imm. 20:0. */ +#define R_AARCH64_TLSGD_ADR_PAGE21 513 /* page-rel. ADRP imm. 32:12. */ +#define R_AARCH64_TLSGD_ADD_LO12_NC 514 /* direct ADD imm. from 11:0. */ +#define R_AARCH64_TLSGD_MOVW_G1 515 /* GOT-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSGD_MOVW_G0_NC 516 /* GOT-rel. MOVK imm. 15:0. */ +#define R_AARCH64_TLSLD_ADR_PREL21 517 /* Like 512; local dynamic model. */ +#define R_AARCH64_TLSLD_ADR_PAGE21 518 /* Like 513; local dynamic model. */ +#define R_AARCH64_TLSLD_ADD_LO12_NC 519 /* Like 514; local dynamic model. */ +#define R_AARCH64_TLSLD_MOVW_G1 520 /* Like 515; local dynamic model. */ +#define R_AARCH64_TLSLD_MOVW_G0_NC 521 /* Like 516; local dynamic model. */ +#define R_AARCH64_TLSLD_LD_PREL19 522 /* TLS PC-rel. load imm. 20:2. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G2 523 /* TLS DTP-rel. MOV{N,Z} 47:32. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G1 524 /* TLS DTP-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC 525 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G0 526 /* TLS DTP-rel. MOV{N,Z} 15:0. */ +#define R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC 527 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLD_ADD_DTPREL_HI12 528 /* DTP-rel. ADD imm. from 23:12. */ +#define R_AARCH64_TLSLD_ADD_DTPREL_LO12 529 /* DTP-rel. ADD imm. from 11:0. */ +#define R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC 530 /* Likewise; no ovfl. check. */ +#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12 531 /* DTP-rel. LD/ST imm. 11:0. */ +#define R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC 532 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12 533 /* DTP-rel. LD/ST imm. 11:1. */ +#define R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC 534 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12 535 /* DTP-rel. LD/ST imm. 11:2. */ +#define R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC 536 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12 537 /* DTP-rel. LD/ST imm. 11:3. */ +#define R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC 538 /* Likewise; no check. */ +#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G1 539 /* GOT-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC 540 /* GOT-rel. MOVK 15:0. */ +#define R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 541 /* Page-rel. ADRP 32:12. */ +#define R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC 542 /* Direct LD off. 11:3. */ +#define R_AARCH64_TLSIE_LD_GOTTPREL_PREL19 543 /* PC-rel. load imm. 20:2. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G2 544 /* TLS TP-rel. MOV{N,Z} 47:32. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G1 545 /* TLS TP-rel. MOV{N,Z} 31:16. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G1_NC 546 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G0 547 /* TLS TP-rel. MOV{N,Z} 15:0. */ +#define R_AARCH64_TLSLE_MOVW_TPREL_G0_NC 548 /* Likewise; MOVK; no check. */ +#define R_AARCH64_TLSLE_ADD_TPREL_HI12 549 /* TP-rel. ADD imm. 23:12. */ +#define R_AARCH64_TLSLE_ADD_TPREL_LO12 550 /* TP-rel. ADD imm. 11:0. */ +#define R_AARCH64_TLSLE_ADD_TPREL_LO12_NC 551 /* Likewise; no ovfl. check. */ +#define R_AARCH64_TLSLE_LDST8_TPREL_LO12 552 /* TP-rel. LD/ST off. 11:0. */ +#define R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC 553 /* Likewise; no ovfl. check. */ +#define R_AARCH64_TLSLE_LDST16_TPREL_LO12 554 /* TP-rel. LD/ST off. 11:1. */ +#define R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC 555 /* Likewise; no check. */ +#define R_AARCH64_TLSLE_LDST32_TPREL_LO12 556 /* TP-rel. LD/ST off. 11:2. */ +#define R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC 557 /* Likewise; no check. */ +#define R_AARCH64_TLSLE_LDST64_TPREL_LO12 558 /* TP-rel. LD/ST off. 11:3. */ +#define R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC 559 /* Likewise; no check. */ +#define R_AARCH64_TLSDESC_LD_PREL19 560 /* PC-rel. load immediate 20:2. */ +#define R_AARCH64_TLSDESC_ADR_PREL21 561 /* PC-rel. ADR immediate 20:0. */ +#define R_AARCH64_TLSDESC_ADR_PAGE21 562 /* Page-rel. ADRP imm. 32:12. */ +#define R_AARCH64_TLSDESC_LD64_LO12 563 /* Direct LD off. from 11:3. */ +#define R_AARCH64_TLSDESC_ADD_LO12 564 /* Direct ADD imm. from 11:0. */ +#define R_AARCH64_TLSDESC_OFF_G1 565 /* GOT-rel. MOV{N,Z} imm. 31:16. */ +#define R_AARCH64_TLSDESC_OFF_G0_NC 566 /* GOT-rel. MOVK imm. 15:0; no ck. */ +#define R_AARCH64_TLSDESC_LDR 567 /* Relax LDR. */ +#define R_AARCH64_TLSDESC_ADD 568 /* Relax ADD. */ +#define R_AARCH64_TLSDESC_CALL 569 /* Relax BLR. */ +#define R_AARCH64_TLSLE_LDST128_TPREL_LO12 570 /* TP-rel. LD/ST off. 11:4. */ +#define R_AARCH64_TLSLE_LDST128_TPREL_LO12_NC 571 /* Likewise; no check. */ +#define R_AARCH64_TLSLD_LDST128_DTPREL_LO12 572 /* DTP-rel. LD/ST imm. 11:4. */ +#define R_AARCH64_TLSLD_LDST128_DTPREL_LO12_NC 573 /* Likewise; no check. */ +#define R_AARCH64_COPY 1024 /* Copy symbol at runtime. */ +#define R_AARCH64_GLOB_DAT 1025 /* Create GOT entry. */ +#define R_AARCH64_JUMP_SLOT 1026 /* Create PLT entry. */ +#define R_AARCH64_RELATIVE 1027 /* Adjust by program base. */ +#define R_AARCH64_TLS_DTPMOD64 1028 /* Module number, 64 bit. */ +#define R_AARCH64_TLS_DTPREL64 1029 /* Module-relative offset, 64 bit. */ +#define R_AARCH64_TLS_TPREL64 1030 /* TP-relative offset, 64 bit. */ +#define R_AARCH64_TLSDESC 1031 /* TLS Descriptor. */ +#define R_AARCH64_IRELATIVE 1032 /* STT_GNU_IFUNC relocation. */ +/* Keep this the last entry. */ +#define R_AARCH64_NUM 1033 + +/* ARM relocs. */ + +#define R_ARM_NONE 0 /* No reloc */ +#define R_ARM_PC24 1 /* PC relative 26 bit branch */ +#define R_ARM_ABS32 2 /* Direct 32 bit */ +#define R_ARM_REL32 3 /* PC relative 32 bit */ +#define R_ARM_PC13 4 +#define R_ARM_ABS16 5 /* Direct 16 bit */ +#define R_ARM_ABS12 6 /* Direct 12 bit */ +#define R_ARM_THM_ABS5 7 +#define R_ARM_ABS8 8 /* Direct 8 bit */ +#define R_ARM_SBREL32 9 +#define R_ARM_THM_PC22 10 +#define R_ARM_THM_PC8 11 +#define R_ARM_AMP_VCALL9 12 +#define R_ARM_SWI24 13 /* Obsolete static relocation. */ +#define R_ARM_TLS_DESC 13 /* Dynamic relocation. */ +#define R_ARM_THM_SWI8 14 +#define R_ARM_XPC25 15 +#define R_ARM_THM_XPC22 16 +#define R_ARM_TLS_DTPMOD32 17 /* ID of module containing symbol */ +#define R_ARM_TLS_DTPOFF32 18 /* Offset in TLS block */ +#define R_ARM_TLS_TPOFF32 19 /* Offset in static TLS block */ +#define R_ARM_COPY 20 /* Copy symbol at runtime */ +#define R_ARM_GLOB_DAT 21 /* Create GOT entry */ +#define R_ARM_JUMP_SLOT 22 /* Create PLT entry */ +#define R_ARM_RELATIVE 23 /* Adjust by program base */ +#define R_ARM_GOTOFF 24 /* 32 bit offset to GOT */ +#define R_ARM_GOTPC 25 /* 32 bit PC relative offset to GOT */ +#define R_ARM_GOT32 26 /* 32 bit GOT entry */ +#define R_ARM_PLT32 27 /* 32 bit PLT address */ +#define R_ARM_CALL 28 +#define R_ARM_JUMP24 29 +#define R_ARM_THM_JUMP24 30 +#define R_ARM_BASE_ABS 31 /* Adjust by program base. */ +#define R_ARM_ALU_PCREL_7_0 32 +#define R_ARM_ALU_PCREL_15_8 33 +#define R_ARM_ALU_PCREL_23_15 34 +#define R_ARM_LDR_SBREL_11_0 35 +#define R_ARM_ALU_SBREL_19_12 36 +#define R_ARM_ALU_SBREL_27_20 37 +#define R_ARM_TARGET1 38 +#define R_ARM_SBREL31 39 /* Program base relative. */ +#define R_ARM_V4BX 40 +#define R_ARM_TARGET2 41 +#define R_ARM_PREL31 42 +#define R_ARM_MOVW_ABS_NC 43 +#define R_ARM_MOVT_ABS 44 +#define R_ARM_MOVW_PREL_NC 45 /* PC relative 16-bit (MOVW). */ +#define R_ARM_MOVT_PREL 46 /* PC relative (MOVT). */ +#define R_ARM_THM_MOVW_ABS_NC 47 +#define R_ARM_THM_MOVT_ABS 48 +/* Values from 49 to 89 are not yet used/handled by tcc. */ +#define R_ARM_TLS_GOTDESC 90 +#define R_ARM_TLS_CALL 91 +#define R_ARM_TLS_DESCSEQ 92 +#define R_ARM_THM_TLS_CALL 93 +#define R_ARM_GOT_PREL 96 +#define R_ARM_GNU_VTENTRY 100 +#define R_ARM_GNU_VTINHERIT 101 +#define R_ARM_THM_PC11 102 /* thumb unconditional branch */ +#define R_ARM_THM_PC9 103 /* thumb conditional branch */ +#define R_ARM_TLS_GD32 104 /* PC-rel 32 bit for global dynamic + thread local data */ +#define R_ARM_TLS_LDM32 105 /* PC-rel 32 bit for local dynamic + thread local data */ +#define R_ARM_TLS_LDO32 106 /* 32 bit offset relative to TLS + block */ +#define R_ARM_TLS_IE32 107 /* PC-rel 32 bit for GOT entry of + static TLS block offset */ +#define R_ARM_TLS_LE32 108 /* 32 bit offset relative to static + TLS block */ +#define R_ARM_THM_TLS_DESCSEQ 129 +#define R_ARM_IRELATIVE 160 +#define R_ARM_RXPC25 249 +#define R_ARM_RSBREL32 250 +#define R_ARM_THM_RPC22 251 +#define R_ARM_RREL32 252 +#define R_ARM_RABS22 253 +#define R_ARM_RPC24 254 +#define R_ARM_RBASE 255 +/* Keep this the last entry. */ +#define R_ARM_NUM 256 + +/* TMS320C67xx specific declarations */ + +/* XXX: no ELF standard yet*/ + +/* TMS320C67xx relocs. */ +#define R_C60_32 1 +#define R_C60_GOT32 3 /* 32 bit GOT entry */ +#define R_C60_PLT32 4 /* 32 bit PLT address */ +#define R_C60_COPY 5 /* Copy symbol at runtime */ +#define R_C60_GLOB_DAT 6 /* Create GOT entry */ +#define R_C60_JMP_SLOT 7 /* Create PLT entry */ +#define R_C60_RELATIVE 8 /* Adjust by program base */ +#define R_C60_GOTOFF 9 /* 32 bit offset to GOT */ +#define R_C60_GOTPC 10 /* 32 bit PC relative offset to GOT */ + +#define R_C60LO16 0x54 /* low 16 bit MVKL embedded */ +#define R_C60HI16 0x55 /* high 16 bit MVKH embedded */ +/* Keep this the last entry. */ +#define R_C60_NUM 0x56 + +/* IA-64 specific declarations. */ + +/* Processor specific flags for the Ehdr e_flags field. */ +#define EF_IA_64_MASKOS 0x0000000f /* os-specific flags */ +#define EF_IA_64_ABI64 0x00000010 /* 64-bit ABI */ +#define EF_IA_64_ARCH 0xff000000 /* arch. version mask */ + +/* Processor specific values for the Phdr p_type field. */ +#define PT_IA_64_ARCHEXT (PT_LOPROC + 0) /* arch extension bits */ +#define PT_IA_64_UNWIND (PT_LOPROC + 1) /* ia64 unwind bits */ +#define PT_IA_64_HP_OPT_ANOT (PT_LOOS + 0x12) +#define PT_IA_64_HP_HSL_ANOT (PT_LOOS + 0x13) +#define PT_IA_64_HP_STACK (PT_LOOS + 0x14) + +/* Processor specific flags for the Phdr p_flags field. */ +#define PF_IA_64_NORECOV 0x80000000 /* spec insns w/o recovery */ + +/* Processor specific values for the Shdr sh_type field. */ +#define SHT_IA_64_EXT (SHT_LOPROC + 0) /* extension bits */ +#define SHT_IA_64_UNWIND (SHT_LOPROC + 1) /* unwind bits */ + +/* Processor specific flags for the Shdr sh_flags field. */ +#define SHF_IA_64_SHORT 0x10000000 /* section near gp */ +#define SHF_IA_64_NORECOV 0x20000000 /* spec insns w/o recovery */ + +/* Processor specific values for the Dyn d_tag field. */ +#define DT_IA_64_PLT_RESERVE (DT_LOPROC + 0) +#define DT_IA_64_NUM 1 + +/* IA-64 relocations. */ +#define R_IA64_NONE 0x00 /* none */ +#define R_IA64_IMM14 0x21 /* symbol + addend, add imm14 */ +#define R_IA64_IMM22 0x22 /* symbol + addend, add imm22 */ +#define R_IA64_IMM64 0x23 /* symbol + addend, mov imm64 */ +#define R_IA64_DIR32MSB 0x24 /* symbol + addend, data4 MSB */ +#define R_IA64_DIR32LSB 0x25 /* symbol + addend, data4 LSB */ +#define R_IA64_DIR64MSB 0x26 /* symbol + addend, data8 MSB */ +#define R_IA64_DIR64LSB 0x27 /* symbol + addend, data8 LSB */ +#define R_IA64_GPREL22 0x2a /* @gprel(sym + add), add imm22 */ +#define R_IA64_GPREL64I 0x2b /* @gprel(sym + add), mov imm64 */ +#define R_IA64_GPREL32MSB 0x2c /* @gprel(sym + add), data4 MSB */ +#define R_IA64_GPREL32LSB 0x2d /* @gprel(sym + add), data4 LSB */ +#define R_IA64_GPREL64MSB 0x2e /* @gprel(sym + add), data8 MSB */ +#define R_IA64_GPREL64LSB 0x2f /* @gprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF22 0x32 /* @ltoff(sym + add), add imm22 */ +#define R_IA64_LTOFF64I 0x33 /* @ltoff(sym + add), mov imm64 */ +#define R_IA64_PLTOFF22 0x3a /* @pltoff(sym + add), add imm22 */ +#define R_IA64_PLTOFF64I 0x3b /* @pltoff(sym + add), mov imm64 */ +#define R_IA64_PLTOFF64MSB 0x3e /* @pltoff(sym + add), data8 MSB */ +#define R_IA64_PLTOFF64LSB 0x3f /* @pltoff(sym + add), data8 LSB */ +#define R_IA64_FPTR64I 0x43 /* @fptr(sym + add), mov imm64 */ +#define R_IA64_FPTR32MSB 0x44 /* @fptr(sym + add), data4 MSB */ +#define R_IA64_FPTR32LSB 0x45 /* @fptr(sym + add), data4 LSB */ +#define R_IA64_FPTR64MSB 0x46 /* @fptr(sym + add), data8 MSB */ +#define R_IA64_FPTR64LSB 0x47 /* @fptr(sym + add), data8 LSB */ +#define R_IA64_PCREL60B 0x48 /* @pcrel(sym + add), brl */ +#define R_IA64_PCREL21B 0x49 /* @pcrel(sym + add), ptb, call */ +#define R_IA64_PCREL21M 0x4a /* @pcrel(sym + add), chk.s */ +#define R_IA64_PCREL21F 0x4b /* @pcrel(sym + add), fchkf */ +#define R_IA64_PCREL32MSB 0x4c /* @pcrel(sym + add), data4 MSB */ +#define R_IA64_PCREL32LSB 0x4d /* @pcrel(sym + add), data4 LSB */ +#define R_IA64_PCREL64MSB 0x4e /* @pcrel(sym + add), data8 MSB */ +#define R_IA64_PCREL64LSB 0x4f /* @pcrel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_FPTR22 0x52 /* @ltoff(@fptr(s+a)), imm22 */ +#define R_IA64_LTOFF_FPTR64I 0x53 /* @ltoff(@fptr(s+a)), imm64 */ +#define R_IA64_LTOFF_FPTR32MSB 0x54 /* @ltoff(@fptr(s+a)), data4 MSB */ +#define R_IA64_LTOFF_FPTR32LSB 0x55 /* @ltoff(@fptr(s+a)), data4 LSB */ +#define R_IA64_LTOFF_FPTR64MSB 0x56 /* @ltoff(@fptr(s+a)), data8 MSB */ +#define R_IA64_LTOFF_FPTR64LSB 0x57 /* @ltoff(@fptr(s+a)), data8 LSB */ +#define R_IA64_SEGREL32MSB 0x5c /* @segrel(sym + add), data4 MSB */ +#define R_IA64_SEGREL32LSB 0x5d /* @segrel(sym + add), data4 LSB */ +#define R_IA64_SEGREL64MSB 0x5e /* @segrel(sym + add), data8 MSB */ +#define R_IA64_SEGREL64LSB 0x5f /* @segrel(sym + add), data8 LSB */ +#define R_IA64_SECREL32MSB 0x64 /* @secrel(sym + add), data4 MSB */ +#define R_IA64_SECREL32LSB 0x65 /* @secrel(sym + add), data4 LSB */ +#define R_IA64_SECREL64MSB 0x66 /* @secrel(sym + add), data8 MSB */ +#define R_IA64_SECREL64LSB 0x67 /* @secrel(sym + add), data8 LSB */ +#define R_IA64_REL32MSB 0x6c /* data 4 + REL */ +#define R_IA64_REL32LSB 0x6d /* data 4 + REL */ +#define R_IA64_REL64MSB 0x6e /* data 8 + REL */ +#define R_IA64_REL64LSB 0x6f /* data 8 + REL */ +#define R_IA64_LTV32MSB 0x74 /* symbol + addend, data4 MSB */ +#define R_IA64_LTV32LSB 0x75 /* symbol + addend, data4 LSB */ +#define R_IA64_LTV64MSB 0x76 /* symbol + addend, data8 MSB */ +#define R_IA64_LTV64LSB 0x77 /* symbol + addend, data8 LSB */ +#define R_IA64_PCREL21BI 0x79 /* @pcrel(sym + add), 21bit inst */ +#define R_IA64_PCREL22 0x7a /* @pcrel(sym + add), 22bit inst */ +#define R_IA64_PCREL64I 0x7b /* @pcrel(sym + add), 64bit inst */ +#define R_IA64_IPLTMSB 0x80 /* dynamic reloc, imported PLT, MSB */ +#define R_IA64_IPLTLSB 0x81 /* dynamic reloc, imported PLT, LSB */ +#define R_IA64_COPY 0x84 /* copy relocation */ +#define R_IA64_SUB 0x85 /* Addend and symbol difference */ +#define R_IA64_LTOFF22X 0x86 /* LTOFF22, relaxable. */ +#define R_IA64_LDXMOV 0x87 /* Use of LTOFF22X. */ +#define R_IA64_TPREL14 0x91 /* @tprel(sym + add), imm14 */ +#define R_IA64_TPREL22 0x92 /* @tprel(sym + add), imm22 */ +#define R_IA64_TPREL64I 0x93 /* @tprel(sym + add), imm64 */ +#define R_IA64_TPREL64MSB 0x96 /* @tprel(sym + add), data8 MSB */ +#define R_IA64_TPREL64LSB 0x97 /* @tprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_TPREL22 0x9a /* @ltoff(@tprel(s+a)), imm2 */ +#define R_IA64_DTPMOD64MSB 0xa6 /* @dtpmod(sym + add), data8 MSB */ +#define R_IA64_DTPMOD64LSB 0xa7 /* @dtpmod(sym + add), data8 LSB */ +#define R_IA64_LTOFF_DTPMOD22 0xaa /* @ltoff(@dtpmod(sym + add)), imm22 */ +#define R_IA64_DTPREL14 0xb1 /* @dtprel(sym + add), imm14 */ +#define R_IA64_DTPREL22 0xb2 /* @dtprel(sym + add), imm22 */ +#define R_IA64_DTPREL64I 0xb3 /* @dtprel(sym + add), imm64 */ +#define R_IA64_DTPREL32MSB 0xb4 /* @dtprel(sym + add), data4 MSB */ +#define R_IA64_DTPREL32LSB 0xb5 /* @dtprel(sym + add), data4 LSB */ +#define R_IA64_DTPREL64MSB 0xb6 /* @dtprel(sym + add), data8 MSB */ +#define R_IA64_DTPREL64LSB 0xb7 /* @dtprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_DTPREL22 0xba /* @ltoff(@dtprel(s+a)), imm22 */ + +/* SH specific declarations */ + +/* Processor specific flags for the ELF header e_flags field. */ +#define EF_SH_MACH_MASK 0x1f +#define EF_SH_UNKNOWN 0x0 +#define EF_SH1 0x1 +#define EF_SH2 0x2 +#define EF_SH3 0x3 +#define EF_SH_DSP 0x4 +#define EF_SH3_DSP 0x5 +#define EF_SH4AL_DSP 0x6 +#define EF_SH3E 0x8 +#define EF_SH4 0x9 +#define EF_SH2E 0xb +#define EF_SH4A 0xc +#define EF_SH2A 0xd +#define EF_SH4_NOFPU 0x10 +#define EF_SH4A_NOFPU 0x11 +#define EF_SH4_NOMMU_NOFPU 0x12 +#define EF_SH2A_NOFPU 0x13 +#define EF_SH3_NOMMU 0x14 +#define EF_SH2A_SH4_NOFPU 0x15 +#define EF_SH2A_SH3_NOFPU 0x16 +#define EF_SH2A_SH4 0x17 +#define EF_SH2A_SH3E 0x18 + +/* SH relocs. */ +#define R_SH_NONE 0 +#define R_SH_DIR32 1 +#define R_SH_REL32 2 +#define R_SH_DIR8WPN 3 +#define R_SH_IND12W 4 +#define R_SH_DIR8WPL 5 +#define R_SH_DIR8WPZ 6 +#define R_SH_DIR8BP 7 +#define R_SH_DIR8W 8 +#define R_SH_DIR8L 9 +#define R_SH_SWITCH16 25 +#define R_SH_SWITCH32 26 +#define R_SH_USES 27 +#define R_SH_COUNT 28 +#define R_SH_ALIGN 29 +#define R_SH_CODE 30 +#define R_SH_DATA 31 +#define R_SH_LABEL 32 +#define R_SH_SWITCH8 33 +#define R_SH_GNU_VTINHERIT 34 +#define R_SH_GNU_VTENTRY 35 +#define R_SH_TLS_GD_32 144 +#define R_SH_TLS_LD_32 145 +#define R_SH_TLS_LDO_32 146 +#define R_SH_TLS_IE_32 147 +#define R_SH_TLS_LE_32 148 +#define R_SH_TLS_DTPMOD32 149 +#define R_SH_TLS_DTPOFF32 150 +#define R_SH_TLS_TPOFF32 151 +#define R_SH_GOT32 160 +#define R_SH_PLT32 161 +#define R_SH_COPY 162 +#define R_SH_GLOB_DAT 163 +#define R_SH_JMP_SLOT 164 +#define R_SH_RELATIVE 165 +#define R_SH_GOTOFF 166 +#define R_SH_GOTPC 167 +/* Keep this the last entry. */ +#define R_SH_NUM 256 + +/* S/390 specific definitions. */ + +/* Valid values for the e_flags field. */ + +#define EF_S390_HIGH_GPRS 0x00000001 /* High GPRs kernel facility needed. */ + +/* Additional s390 relocs */ + +#define R_390_NONE 0 /* No reloc. */ +#define R_390_8 1 /* Direct 8 bit. */ +#define R_390_12 2 /* Direct 12 bit. */ +#define R_390_16 3 /* Direct 16 bit. */ +#define R_390_32 4 /* Direct 32 bit. */ +#define R_390_PC32 5 /* PC relative 32 bit. */ +#define R_390_GOT12 6 /* 12 bit GOT offset. */ +#define R_390_GOT32 7 /* 32 bit GOT offset. */ +#define R_390_PLT32 8 /* 32 bit PC relative PLT address. */ +#define R_390_COPY 9 /* Copy symbol at runtime. */ +#define R_390_GLOB_DAT 10 /* Create GOT entry. */ +#define R_390_JMP_SLOT 11 /* Create PLT entry. */ +#define R_390_RELATIVE 12 /* Adjust by program base. */ +#define R_390_GOTOFF32 13 /* 32 bit offset to GOT. */ +#define R_390_GOTPC 14 /* 32 bit PC relative offset to GOT. */ +#define R_390_GOT16 15 /* 16 bit GOT offset. */ +#define R_390_PC16 16 /* PC relative 16 bit. */ +#define R_390_PC16DBL 17 /* PC relative 16 bit shifted by 1. */ +#define R_390_PLT16DBL 18 /* 16 bit PC rel. PLT shifted by 1. */ +#define R_390_PC32DBL 19 /* PC relative 32 bit shifted by 1. */ +#define R_390_PLT32DBL 20 /* 32 bit PC rel. PLT shifted by 1. */ +#define R_390_GOTPCDBL 21 /* 32 bit PC rel. GOT shifted by 1. */ +#define R_390_64 22 /* Direct 64 bit. */ +#define R_390_PC64 23 /* PC relative 64 bit. */ +#define R_390_GOT64 24 /* 64 bit GOT offset. */ +#define R_390_PLT64 25 /* 64 bit PC relative PLT address. */ +#define R_390_GOTENT 26 /* 32 bit PC rel. to GOT entry >> 1. */ +#define R_390_GOTOFF16 27 /* 16 bit offset to GOT. */ +#define R_390_GOTOFF64 28 /* 64 bit offset to GOT. */ +#define R_390_GOTPLT12 29 /* 12 bit offset to jump slot. */ +#define R_390_GOTPLT16 30 /* 16 bit offset to jump slot. */ +#define R_390_GOTPLT32 31 /* 32 bit offset to jump slot. */ +#define R_390_GOTPLT64 32 /* 64 bit offset to jump slot. */ +#define R_390_GOTPLTENT 33 /* 32 bit rel. offset to jump slot. */ +#define R_390_PLTOFF16 34 /* 16 bit offset from GOT to PLT. */ +#define R_390_PLTOFF32 35 /* 32 bit offset from GOT to PLT. */ +#define R_390_PLTOFF64 36 /* 16 bit offset from GOT to PLT. */ +#define R_390_TLS_LOAD 37 /* Tag for load insn in TLS code. */ +#define R_390_TLS_GDCALL 38 /* Tag for function call in general + dynamic TLS code. */ +#define R_390_TLS_LDCALL 39 /* Tag for function call in local + dynamic TLS code. */ +#define R_390_TLS_GD32 40 /* Direct 32 bit for general dynamic + thread local data. */ +#define R_390_TLS_GD64 41 /* Direct 64 bit for general dynamic + thread local data. */ +#define R_390_TLS_GOTIE12 42 /* 12 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_GOTIE32 43 /* 32 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_GOTIE64 44 /* 64 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_LDM32 45 /* Direct 32 bit for local dynamic + thread local data in LE code. */ +#define R_390_TLS_LDM64 46 /* Direct 64 bit for local dynamic + thread local data in LE code. */ +#define R_390_TLS_IE32 47 /* 32 bit address of GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_IE64 48 /* 64 bit address of GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_IEENT 49 /* 32 bit rel. offset to GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_LE32 50 /* 32 bit negated offset relative to + static TLS block. */ +#define R_390_TLS_LE64 51 /* 64 bit negated offset relative to + static TLS block. */ +#define R_390_TLS_LDO32 52 /* 32 bit offset relative to TLS + block. */ +#define R_390_TLS_LDO64 53 /* 64 bit offset relative to TLS + block. */ +#define R_390_TLS_DTPMOD 54 /* ID of module containing symbol. */ +#define R_390_TLS_DTPOFF 55 /* Offset in TLS block. */ +#define R_390_TLS_TPOFF 56 /* Negated offset in static TLS + block. */ +#define R_390_20 57 /* Direct 20 bit. */ +#define R_390_GOT20 58 /* 20 bit GOT offset. */ +#define R_390_GOTPLT20 59 /* 20 bit offset to jump slot. */ +#define R_390_TLS_GOTIE20 60 /* 20 bit GOT offset for static TLS + block offset. */ +#define R_390_IRELATIVE 61 /* STT_GNU_IFUNC relocation. */ +/* Keep this the last entry. */ +#define R_390_NUM 62 + + +/* CRIS relocations. */ +#define R_CRIS_NONE 0 +#define R_CRIS_8 1 +#define R_CRIS_16 2 +#define R_CRIS_32 3 +#define R_CRIS_8_PCREL 4 +#define R_CRIS_16_PCREL 5 +#define R_CRIS_32_PCREL 6 +#define R_CRIS_GNU_VTINHERIT 7 +#define R_CRIS_GNU_VTENTRY 8 +#define R_CRIS_COPY 9 +#define R_CRIS_GLOB_DAT 10 +#define R_CRIS_JUMP_SLOT 11 +#define R_CRIS_RELATIVE 12 +#define R_CRIS_16_GOT 13 +#define R_CRIS_32_GOT 14 +#define R_CRIS_16_GOTPLT 15 +#define R_CRIS_32_GOTPLT 16 +#define R_CRIS_32_GOTREL 17 +#define R_CRIS_32_PLT_GOTREL 18 +#define R_CRIS_32_PLT_PCREL 19 + +#define R_CRIS_NUM 20 + + +/* AMD x86-64 relocations. */ +#define R_X86_64_NONE 0 /* No reloc */ +#define R_X86_64_64 1 /* Direct 64 bit */ +#define R_X86_64_PC32 2 /* PC relative 32 bit signed */ +#define R_X86_64_GOT32 3 /* 32 bit GOT entry */ +#define R_X86_64_PLT32 4 /* 32 bit PLT address */ +#define R_X86_64_COPY 5 /* Copy symbol at runtime */ +#define R_X86_64_GLOB_DAT 6 /* Create GOT entry */ +#define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */ +#define R_X86_64_RELATIVE 8 /* Adjust by program base */ +#define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative + offset to GOT */ +#define R_X86_64_32 10 /* Direct 32 bit zero extended */ +#define R_X86_64_32S 11 /* Direct 32 bit sign extended */ +#define R_X86_64_16 12 /* Direct 16 bit zero extended */ +#define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */ +#define R_X86_64_8 14 /* Direct 8 bit sign extended */ +#define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */ +#define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */ +#define R_X86_64_DTPOFF64 17 /* Offset in module's TLS block */ +#define R_X86_64_TPOFF64 18 /* Offset in initial TLS block */ +#define R_X86_64_TLSGD 19 /* 32 bit signed PC relative offset + to two GOT entries for GD symbol */ +#define R_X86_64_TLSLD 20 /* 32 bit signed PC relative offset + to two GOT entries for LD symbol */ +#define R_X86_64_DTPOFF32 21 /* Offset in TLS block */ +#define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset + to GOT entry for IE symbol */ +#define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */ +#define R_X86_64_PC64 24 /* PC relative 64 bit */ +#define R_X86_64_GOTOFF64 25 /* 64 bit offset to GOT */ +#define R_X86_64_GOTPC32 26 /* 32 bit signed pc relative + offset to GOT */ +#define R_X86_64_GOT64 27 /* 64-bit GOT entry offset */ +#define R_X86_64_GOTPCREL64 28 /* 64-bit PC relative offset + to GOT entry */ +#define R_X86_64_GOTPC64 29 /* 64-bit PC relative offset to GOT */ +#define R_X86_64_GOTPLT64 30 /* like GOT64, says PLT entry needed */ +#define R_X86_64_PLTOFF64 31 /* 64-bit GOT relative offset + to PLT entry */ +#define R_X86_64_SIZE32 32 /* Size of symbol plus 32-bit addend */ +#define R_X86_64_SIZE64 33 /* Size of symbol plus 64-bit addend */ +#define R_X86_64_GOTPC32_TLSDESC 34 /* GOT offset for TLS descriptor. */ +#define R_X86_64_TLSDESC_CALL 35 /* Marker for call through TLS + descriptor. */ +#define R_X86_64_TLSDESC 36 /* TLS descriptor. */ +#define R_X86_64_IRELATIVE 37 /* Adjust indirectly by program base */ +#define R_X86_64_RELATIVE64 38 /* 64-bit adjust by program base */ +#define R_X86_64_GOTPCRELX 41 /* like GOTPCREL, but optionally with + linker optimizations */ +#define R_X86_64_REX_GOTPCRELX 42 /* like GOTPCRELX, but a REX prefix + is present */ + +#define R_X86_64_NUM 43 + +/* x86-64 sh_type values. */ +#define SHT_X86_64_UNWIND 0x70000001 /* Unwind information. */ + +/* AM33 relocations. */ +#define R_MN10300_NONE 0 /* No reloc. */ +#define R_MN10300_32 1 /* Direct 32 bit. */ +#define R_MN10300_16 2 /* Direct 16 bit. */ +#define R_MN10300_8 3 /* Direct 8 bit. */ +#define R_MN10300_PCREL32 4 /* PC-relative 32-bit. */ +#define R_MN10300_PCREL16 5 /* PC-relative 16-bit signed. */ +#define R_MN10300_PCREL8 6 /* PC-relative 8-bit signed. */ +#define R_MN10300_GNU_VTINHERIT 7 /* Ancient C++ vtable garbage... */ +#define R_MN10300_GNU_VTENTRY 8 /* ... collection annotation. */ +#define R_MN10300_24 9 /* Direct 24 bit. */ +#define R_MN10300_GOTPC32 10 /* 32-bit PCrel offset to GOT. */ +#define R_MN10300_GOTPC16 11 /* 16-bit PCrel offset to GOT. */ +#define R_MN10300_GOTOFF32 12 /* 32-bit offset from GOT. */ +#define R_MN10300_GOTOFF24 13 /* 24-bit offset from GOT. */ +#define R_MN10300_GOTOFF16 14 /* 16-bit offset from GOT. */ +#define R_MN10300_PLT32 15 /* 32-bit PCrel to PLT entry. */ +#define R_MN10300_PLT16 16 /* 16-bit PCrel to PLT entry. */ +#define R_MN10300_GOT32 17 /* 32-bit offset to GOT entry. */ +#define R_MN10300_GOT24 18 /* 24-bit offset to GOT entry. */ +#define R_MN10300_GOT16 19 /* 16-bit offset to GOT entry. */ +#define R_MN10300_COPY 20 /* Copy symbol at runtime. */ +#define R_MN10300_GLOB_DAT 21 /* Create GOT entry. */ +#define R_MN10300_JMP_SLOT 22 /* Create PLT entry. */ +#define R_MN10300_RELATIVE 23 /* Adjust by program base. */ +#define R_MN10300_TLS_GD 24 /* 32-bit offset for global dynamic. */ +#define R_MN10300_TLS_LD 25 /* 32-bit offset for local dynamic. */ +#define R_MN10300_TLS_LDO 26 /* Module-relative offset. */ +#define R_MN10300_TLS_GOTIE 27 /* GOT offset for static TLS block + offset. */ +#define R_MN10300_TLS_IE 28 /* GOT address for static TLS block + offset. */ +#define R_MN10300_TLS_LE 29 /* Offset relative to static TLS + block. */ +#define R_MN10300_TLS_DTPMOD 30 /* ID of module containing symbol. */ +#define R_MN10300_TLS_DTPOFF 31 /* Offset in module TLS block. */ +#define R_MN10300_TLS_TPOFF 32 /* Offset in static TLS block. */ +#define R_MN10300_SYM_DIFF 33 /* Adjustment for next reloc as needed + by linker relaxation. */ +#define R_MN10300_ALIGN 34 /* Alignment requirement for linker + relaxation. */ +#define R_MN10300_NUM 35 + + +/* M32R relocs. */ +#define R_M32R_NONE 0 /* No reloc. */ +#define R_M32R_16 1 /* Direct 16 bit. */ +#define R_M32R_32 2 /* Direct 32 bit. */ +#define R_M32R_24 3 /* Direct 24 bit. */ +#define R_M32R_10_PCREL 4 /* PC relative 10 bit shifted. */ +#define R_M32R_18_PCREL 5 /* PC relative 18 bit shifted. */ +#define R_M32R_26_PCREL 6 /* PC relative 26 bit shifted. */ +#define R_M32R_HI16_ULO 7 /* High 16 bit with unsigned low. */ +#define R_M32R_HI16_SLO 8 /* High 16 bit with signed low. */ +#define R_M32R_LO16 9 /* Low 16 bit. */ +#define R_M32R_SDA16 10 /* 16 bit offset in SDA. */ +#define R_M32R_GNU_VTINHERIT 11 +#define R_M32R_GNU_VTENTRY 12 +/* M32R relocs use SHT_RELA. */ +#define R_M32R_16_RELA 33 /* Direct 16 bit. */ +#define R_M32R_32_RELA 34 /* Direct 32 bit. */ +#define R_M32R_24_RELA 35 /* Direct 24 bit. */ +#define R_M32R_10_PCREL_RELA 36 /* PC relative 10 bit shifted. */ +#define R_M32R_18_PCREL_RELA 37 /* PC relative 18 bit shifted. */ +#define R_M32R_26_PCREL_RELA 38 /* PC relative 26 bit shifted. */ +#define R_M32R_HI16_ULO_RELA 39 /* High 16 bit with unsigned low */ +#define R_M32R_HI16_SLO_RELA 40 /* High 16 bit with signed low */ +#define R_M32R_LO16_RELA 41 /* Low 16 bit */ +#define R_M32R_SDA16_RELA 42 /* 16 bit offset in SDA */ +#define R_M32R_RELA_GNU_VTINHERIT 43 +#define R_M32R_RELA_GNU_VTENTRY 44 +#define R_M32R_REL32 45 /* PC relative 32 bit. */ + +#define R_M32R_GOT24 48 /* 24 bit GOT entry */ +#define R_M32R_26_PLTREL 49 /* 26 bit PC relative to PLT shifted */ +#define R_M32R_COPY 50 /* Copy symbol at runtime */ +#define R_M32R_GLOB_DAT 51 /* Create GOT entry */ +#define R_M32R_JMP_SLOT 52 /* Create PLT entry */ +#define R_M32R_RELATIVE 53 /* Adjust by program base */ +#define R_M32R_GOTOFF 54 /* 24 bit offset to GOT */ +#define R_M32R_GOTPC24 55 /* 24 bit PC relative offset to GOT */ +#define R_M32R_GOT16_HI_ULO 56 /* High 16 bit GOT entry with unsigned + low */ +#define R_M32R_GOT16_HI_SLO 57 /* High 16 bit GOT entry with signed + low */ +#define R_M32R_GOT16_LO 58 /* Low 16 bit GOT entry */ +#define R_M32R_GOTPC_HI_ULO 59 /* High 16 bit PC relative offset to + GOT with unsigned low */ +#define R_M32R_GOTPC_HI_SLO 60 /* High 16 bit PC relative offset to + GOT with signed low */ +#define R_M32R_GOTPC_LO 61 /* Low 16 bit PC relative offset to + GOT */ +#define R_M32R_GOTOFF_HI_ULO 62 /* High 16 bit offset to GOT + with unsigned low */ +#define R_M32R_GOTOFF_HI_SLO 63 /* High 16 bit offset to GOT + with signed low */ +#define R_M32R_GOTOFF_LO 64 /* Low 16 bit offset to GOT */ +#define R_M32R_NUM 256 /* Keep this the last entry. */ + + +/* TILEPro relocations. */ +#define R_TILEPRO_NONE 0 /* No reloc */ +#define R_TILEPRO_32 1 /* Direct 32 bit */ +#define R_TILEPRO_16 2 /* Direct 16 bit */ +#define R_TILEPRO_8 3 /* Direct 8 bit */ +#define R_TILEPRO_32_PCREL 4 /* PC relative 32 bit */ +#define R_TILEPRO_16_PCREL 5 /* PC relative 16 bit */ +#define R_TILEPRO_8_PCREL 6 /* PC relative 8 bit */ +#define R_TILEPRO_LO16 7 /* Low 16 bit */ +#define R_TILEPRO_HI16 8 /* High 16 bit */ +#define R_TILEPRO_HA16 9 /* High 16 bit, adjusted */ +#define R_TILEPRO_COPY 10 /* Copy relocation */ +#define R_TILEPRO_GLOB_DAT 11 /* Create GOT entry */ +#define R_TILEPRO_JMP_SLOT 12 /* Create PLT entry */ +#define R_TILEPRO_RELATIVE 13 /* Adjust by program base */ +#define R_TILEPRO_BROFF_X1 14 /* X1 pipe branch offset */ +#define R_TILEPRO_JOFFLONG_X1 15 /* X1 pipe jump offset */ +#define R_TILEPRO_JOFFLONG_X1_PLT 16 /* X1 pipe jump offset to PLT */ +#define R_TILEPRO_IMM8_X0 17 /* X0 pipe 8-bit */ +#define R_TILEPRO_IMM8_Y0 18 /* Y0 pipe 8-bit */ +#define R_TILEPRO_IMM8_X1 19 /* X1 pipe 8-bit */ +#define R_TILEPRO_IMM8_Y1 20 /* Y1 pipe 8-bit */ +#define R_TILEPRO_MT_IMM15_X1 21 /* X1 pipe mtspr */ +#define R_TILEPRO_MF_IMM15_X1 22 /* X1 pipe mfspr */ +#define R_TILEPRO_IMM16_X0 23 /* X0 pipe 16-bit */ +#define R_TILEPRO_IMM16_X1 24 /* X1 pipe 16-bit */ +#define R_TILEPRO_IMM16_X0_LO 25 /* X0 pipe low 16-bit */ +#define R_TILEPRO_IMM16_X1_LO 26 /* X1 pipe low 16-bit */ +#define R_TILEPRO_IMM16_X0_HI 27 /* X0 pipe high 16-bit */ +#define R_TILEPRO_IMM16_X1_HI 28 /* X1 pipe high 16-bit */ +#define R_TILEPRO_IMM16_X0_HA 29 /* X0 pipe high 16-bit, adjusted */ +#define R_TILEPRO_IMM16_X1_HA 30 /* X1 pipe high 16-bit, adjusted */ +#define R_TILEPRO_IMM16_X0_PCREL 31 /* X0 pipe PC relative 16 bit */ +#define R_TILEPRO_IMM16_X1_PCREL 32 /* X1 pipe PC relative 16 bit */ +#define R_TILEPRO_IMM16_X0_LO_PCREL 33 /* X0 pipe PC relative low 16 bit */ +#define R_TILEPRO_IMM16_X1_LO_PCREL 34 /* X1 pipe PC relative low 16 bit */ +#define R_TILEPRO_IMM16_X0_HI_PCREL 35 /* X0 pipe PC relative high 16 bit */ +#define R_TILEPRO_IMM16_X1_HI_PCREL 36 /* X1 pipe PC relative high 16 bit */ +#define R_TILEPRO_IMM16_X0_HA_PCREL 37 /* X0 pipe PC relative ha() 16 bit */ +#define R_TILEPRO_IMM16_X1_HA_PCREL 38 /* X1 pipe PC relative ha() 16 bit */ +#define R_TILEPRO_IMM16_X0_GOT 39 /* X0 pipe 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT 40 /* X1 pipe 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_LO 41 /* X0 pipe low 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_LO 42 /* X1 pipe low 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_HI 43 /* X0 pipe high 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_HI 44 /* X1 pipe high 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_HA 45 /* X0 pipe ha() 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_HA 46 /* X1 pipe ha() 16-bit GOT offset */ +#define R_TILEPRO_MMSTART_X0 47 /* X0 pipe mm "start" */ +#define R_TILEPRO_MMEND_X0 48 /* X0 pipe mm "end" */ +#define R_TILEPRO_MMSTART_X1 49 /* X1 pipe mm "start" */ +#define R_TILEPRO_MMEND_X1 50 /* X1 pipe mm "end" */ +#define R_TILEPRO_SHAMT_X0 51 /* X0 pipe shift amount */ +#define R_TILEPRO_SHAMT_X1 52 /* X1 pipe shift amount */ +#define R_TILEPRO_SHAMT_Y0 53 /* Y0 pipe shift amount */ +#define R_TILEPRO_SHAMT_Y1 54 /* Y1 pipe shift amount */ +#define R_TILEPRO_DEST_IMM8_X1 55 /* X1 pipe destination 8-bit */ +/* Relocs 56-59 are currently not defined. */ +#define R_TILEPRO_TLS_GD_CALL 60 /* "jal" for TLS GD */ +#define R_TILEPRO_IMM8_X0_TLS_GD_ADD 61 /* X0 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_X1_TLS_GD_ADD 62 /* X1 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_Y0_TLS_GD_ADD 63 /* Y0 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_Y1_TLS_GD_ADD 64 /* Y1 pipe "addi" for TLS GD */ +#define R_TILEPRO_TLS_IE_LOAD 65 /* "lw_tls" for TLS IE */ +#define R_TILEPRO_IMM16_X0_TLS_GD 66 /* X0 pipe 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD 67 /* X1 pipe 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_LO 68 /* X0 pipe low 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_LO 69 /* X1 pipe low 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_HI 70 /* X0 pipe high 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_HI 71 /* X1 pipe high 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_HA 72 /* X0 pipe ha() 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_HA 73 /* X1 pipe ha() 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE 74 /* X0 pipe 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE 75 /* X1 pipe 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_LO 76 /* X0 pipe low 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_LO 77 /* X1 pipe low 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_HI 78 /* X0 pipe high 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_HI 79 /* X1 pipe high 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_HA 80 /* X0 pipe ha() 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_HA 81 /* X1 pipe ha() 16-bit TLS IE offset */ +#define R_TILEPRO_TLS_DTPMOD32 82 /* ID of module containing symbol */ +#define R_TILEPRO_TLS_DTPOFF32 83 /* Offset in TLS block */ +#define R_TILEPRO_TLS_TPOFF32 84 /* Offset in static TLS block */ +#define R_TILEPRO_IMM16_X0_TLS_LE 85 /* X0 pipe 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE 86 /* X1 pipe 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_LO 87 /* X0 pipe low 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_LO 88 /* X1 pipe low 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_HI 89 /* X0 pipe high 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_HI 90 /* X1 pipe high 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_HA 91 /* X0 pipe ha() 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_HA 92 /* X1 pipe ha() 16-bit TLS LE offset */ + +#define R_TILEPRO_GNU_VTINHERIT 128 /* GNU C++ vtable hierarchy */ +#define R_TILEPRO_GNU_VTENTRY 129 /* GNU C++ vtable member usage */ + +#define R_TILEPRO_NUM 130 + + +/* TILE-Gx relocations. */ +#define R_TILEGX_NONE 0 /* No reloc */ +#define R_TILEGX_64 1 /* Direct 64 bit */ +#define R_TILEGX_32 2 /* Direct 32 bit */ +#define R_TILEGX_16 3 /* Direct 16 bit */ +#define R_TILEGX_8 4 /* Direct 8 bit */ +#define R_TILEGX_64_PCREL 5 /* PC relative 64 bit */ +#define R_TILEGX_32_PCREL 6 /* PC relative 32 bit */ +#define R_TILEGX_16_PCREL 7 /* PC relative 16 bit */ +#define R_TILEGX_8_PCREL 8 /* PC relative 8 bit */ +#define R_TILEGX_HW0 9 /* hword 0 16-bit */ +#define R_TILEGX_HW1 10 /* hword 1 16-bit */ +#define R_TILEGX_HW2 11 /* hword 2 16-bit */ +#define R_TILEGX_HW3 12 /* hword 3 16-bit */ +#define R_TILEGX_HW0_LAST 13 /* last hword 0 16-bit */ +#define R_TILEGX_HW1_LAST 14 /* last hword 1 16-bit */ +#define R_TILEGX_HW2_LAST 15 /* last hword 2 16-bit */ +#define R_TILEGX_COPY 16 /* Copy relocation */ +#define R_TILEGX_GLOB_DAT 17 /* Create GOT entry */ +#define R_TILEGX_JMP_SLOT 18 /* Create PLT entry */ +#define R_TILEGX_RELATIVE 19 /* Adjust by program base */ +#define R_TILEGX_BROFF_X1 20 /* X1 pipe branch offset */ +#define R_TILEGX_JUMPOFF_X1 21 /* X1 pipe jump offset */ +#define R_TILEGX_JUMPOFF_X1_PLT 22 /* X1 pipe jump offset to PLT */ +#define R_TILEGX_IMM8_X0 23 /* X0 pipe 8-bit */ +#define R_TILEGX_IMM8_Y0 24 /* Y0 pipe 8-bit */ +#define R_TILEGX_IMM8_X1 25 /* X1 pipe 8-bit */ +#define R_TILEGX_IMM8_Y1 26 /* Y1 pipe 8-bit */ +#define R_TILEGX_DEST_IMM8_X1 27 /* X1 pipe destination 8-bit */ +#define R_TILEGX_MT_IMM14_X1 28 /* X1 pipe mtspr */ +#define R_TILEGX_MF_IMM14_X1 29 /* X1 pipe mfspr */ +#define R_TILEGX_MMSTART_X0 30 /* X0 pipe mm "start" */ +#define R_TILEGX_MMEND_X0 31 /* X0 pipe mm "end" */ +#define R_TILEGX_SHAMT_X0 32 /* X0 pipe shift amount */ +#define R_TILEGX_SHAMT_X1 33 /* X1 pipe shift amount */ +#define R_TILEGX_SHAMT_Y0 34 /* Y0 pipe shift amount */ +#define R_TILEGX_SHAMT_Y1 35 /* Y1 pipe shift amount */ +#define R_TILEGX_IMM16_X0_HW0 36 /* X0 pipe hword 0 */ +#define R_TILEGX_IMM16_X1_HW0 37 /* X1 pipe hword 0 */ +#define R_TILEGX_IMM16_X0_HW1 38 /* X0 pipe hword 1 */ +#define R_TILEGX_IMM16_X1_HW1 39 /* X1 pipe hword 1 */ +#define R_TILEGX_IMM16_X0_HW2 40 /* X0 pipe hword 2 */ +#define R_TILEGX_IMM16_X1_HW2 41 /* X1 pipe hword 2 */ +#define R_TILEGX_IMM16_X0_HW3 42 /* X0 pipe hword 3 */ +#define R_TILEGX_IMM16_X1_HW3 43 /* X1 pipe hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_LAST 44 /* X0 pipe last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST 45 /* X1 pipe last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST 46 /* X0 pipe last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST 47 /* X1 pipe last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST 48 /* X0 pipe last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST 49 /* X1 pipe last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_PCREL 50 /* X0 pipe PC relative hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_PCREL 51 /* X1 pipe PC relative hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_PCREL 52 /* X0 pipe PC relative hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_PCREL 53 /* X1 pipe PC relative hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_PCREL 54 /* X0 pipe PC relative hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_PCREL 55 /* X1 pipe PC relative hword 2 */ +#define R_TILEGX_IMM16_X0_HW3_PCREL 56 /* X0 pipe PC relative hword 3 */ +#define R_TILEGX_IMM16_X1_HW3_PCREL 57 /* X1 pipe PC relative hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_PCREL 58 /* X0 pipe PC-rel last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST_PCREL 59 /* X1 pipe PC-rel last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST_PCREL 60 /* X0 pipe PC-rel last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST_PCREL 61 /* X1 pipe PC-rel last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST_PCREL 62 /* X0 pipe PC-rel last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST_PCREL 63 /* X1 pipe PC-rel last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_GOT 64 /* X0 pipe hword 0 GOT offset */ +#define R_TILEGX_IMM16_X1_HW0_GOT 65 /* X1 pipe hword 0 GOT offset */ +#define R_TILEGX_IMM16_X0_HW0_PLT_PCREL 66 /* X0 pipe PC-rel PLT hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_PLT_PCREL 67 /* X1 pipe PC-rel PLT hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_PLT_PCREL 68 /* X0 pipe PC-rel PLT hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_PLT_PCREL 69 /* X1 pipe PC-rel PLT hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_PLT_PCREL 70 /* X0 pipe PC-rel PLT hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_PLT_PCREL 71 /* X1 pipe PC-rel PLT hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_GOT 72 /* X0 pipe last hword 0 GOT offset */ +#define R_TILEGX_IMM16_X1_HW0_LAST_GOT 73 /* X1 pipe last hword 0 GOT offset */ +#define R_TILEGX_IMM16_X0_HW1_LAST_GOT 74 /* X0 pipe last hword 1 GOT offset */ +#define R_TILEGX_IMM16_X1_HW1_LAST_GOT 75 /* X1 pipe last hword 1 GOT offset */ +#define R_TILEGX_IMM16_X0_HW3_PLT_PCREL 76 /* X0 pipe PC-rel PLT hword 3 */ +#define R_TILEGX_IMM16_X1_HW3_PLT_PCREL 77 /* X1 pipe PC-rel PLT hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_TLS_GD 78 /* X0 pipe hword 0 TLS GD offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_GD 79 /* X1 pipe hword 0 TLS GD offset */ +#define R_TILEGX_IMM16_X0_HW0_TLS_LE 80 /* X0 pipe hword 0 TLS LE offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_LE 81 /* X1 pipe hword 0 TLS LE offset */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE 82 /* X0 pipe last hword 0 LE off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE 83 /* X1 pipe last hword 0 LE off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE 84 /* X0 pipe last hword 1 LE off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE 85 /* X1 pipe last hword 1 LE off */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD 86 /* X0 pipe last hword 0 GD off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD 87 /* X1 pipe last hword 0 GD off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD 88 /* X0 pipe last hword 1 GD off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD 89 /* X1 pipe last hword 1 GD off */ +/* Relocs 90-91 are currently not defined. */ +#define R_TILEGX_IMM16_X0_HW0_TLS_IE 92 /* X0 pipe hword 0 TLS IE offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_IE 93 /* X1 pipe hword 0 TLS IE offset */ +#define R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL 94 /* X0 pipe PC-rel PLT last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL 95 /* X1 pipe PC-rel PLT last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL 96 /* X0 pipe PC-rel PLT last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL 97 /* X1 pipe PC-rel PLT last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL 98 /* X0 pipe PC-rel PLT last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL 99 /* X1 pipe PC-rel PLT last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE 100 /* X0 pipe last hword 0 IE off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE 101 /* X1 pipe last hword 0 IE off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE 102 /* X0 pipe last hword 1 IE off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE 103 /* X1 pipe last hword 1 IE off */ +/* Relocs 104-105 are currently not defined. */ +#define R_TILEGX_TLS_DTPMOD64 106 /* 64-bit ID of symbol's module */ +#define R_TILEGX_TLS_DTPOFF64 107 /* 64-bit offset in TLS block */ +#define R_TILEGX_TLS_TPOFF64 108 /* 64-bit offset in static TLS block */ +#define R_TILEGX_TLS_DTPMOD32 109 /* 32-bit ID of symbol's module */ +#define R_TILEGX_TLS_DTPOFF32 110 /* 32-bit offset in TLS block */ +#define R_TILEGX_TLS_TPOFF32 111 /* 32-bit offset in static TLS block */ +#define R_TILEGX_TLS_GD_CALL 112 /* "jal" for TLS GD */ +#define R_TILEGX_IMM8_X0_TLS_GD_ADD 113 /* X0 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_X1_TLS_GD_ADD 114 /* X1 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_Y0_TLS_GD_ADD 115 /* Y0 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_Y1_TLS_GD_ADD 116 /* Y1 pipe "addi" for TLS GD */ +#define R_TILEGX_TLS_IE_LOAD 117 /* "ld_tls" for TLS IE */ +#define R_TILEGX_IMM8_X0_TLS_ADD 118 /* X0 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_X1_TLS_ADD 119 /* X1 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_Y0_TLS_ADD 120 /* Y0 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_Y1_TLS_ADD 121 /* Y1 pipe "addi" for TLS GD/IE */ + +#define R_TILEGX_GNU_VTINHERIT 128 /* GNU C++ vtable hierarchy */ +#define R_TILEGX_GNU_VTENTRY 129 /* GNU C++ vtable member usage */ + +#define R_TILEGX_NUM 130 + +/* RISC-V ELF Flags */ +#define EF_RISCV_RVC 0x0001 +#define EF_RISCV_FLOAT_ABI 0x0006 +#define EF_RISCV_FLOAT_ABI_SOFT 0x0000 +#define EF_RISCV_FLOAT_ABI_SINGLE 0x0002 +#define EF_RISCV_FLOAT_ABI_DOUBLE 0x0004 +#define EF_RISCV_FLOAT_ABI_QUAD 0x0006 + +/* RISC-V relocations. */ +#define R_RISCV_NONE 0 +#define R_RISCV_32 1 +#define R_RISCV_64 2 +#define R_RISCV_RELATIVE 3 +#define R_RISCV_COPY 4 +#define R_RISCV_JUMP_SLOT 5 +#define R_RISCV_TLS_DTPMOD32 6 +#define R_RISCV_TLS_DTPMOD64 7 +#define R_RISCV_TLS_DTPREL32 8 +#define R_RISCV_TLS_DTPREL64 9 +#define R_RISCV_TLS_TPREL32 10 +#define R_RISCV_TLS_TPREL64 11 +#define R_RISCV_BRANCH 16 +#define R_RISCV_JAL 17 +#define R_RISCV_CALL 18 +#define R_RISCV_CALL_PLT 19 +#define R_RISCV_GOT_HI20 20 +#define R_RISCV_TLS_GOT_HI20 21 +#define R_RISCV_TLS_GD_HI20 22 +#define R_RISCV_PCREL_HI20 23 +#define R_RISCV_PCREL_LO12_I 24 +#define R_RISCV_PCREL_LO12_S 25 +#define R_RISCV_HI20 26 +#define R_RISCV_LO12_I 27 +#define R_RISCV_LO12_S 28 +#define R_RISCV_TPREL_HI20 29 +#define R_RISCV_TPREL_LO12_I 30 +#define R_RISCV_TPREL_LO12_S 31 +#define R_RISCV_TPREL_ADD 32 +#define R_RISCV_ADD8 33 +#define R_RISCV_ADD16 34 +#define R_RISCV_ADD32 35 +#define R_RISCV_ADD64 36 +#define R_RISCV_SUB8 37 +#define R_RISCV_SUB16 38 +#define R_RISCV_SUB32 39 +#define R_RISCV_SUB64 40 +#define R_RISCV_GNU_VTINHERIT 41 +#define R_RISCV_GNU_VTENTRY 42 +#define R_RISCV_ALIGN 43 +#define R_RISCV_RVC_BRANCH 44 +#define R_RISCV_RVC_JUMP 45 +#define R_RISCV_RVC_LUI 46 +#define R_RISCV_GPREL_I 47 +#define R_RISCV_GPREL_S 48 +#define R_RISCV_TPREL_I 49 +#define R_RISCV_TPREL_S 50 +#define R_RISCV_RELAX 51 +#define R_RISCV_SUB6 52 +#define R_RISCV_SET6 53 +#define R_RISCV_SET8 54 +#define R_RISCV_SET16 55 +#define R_RISCV_SET32 56 +#define R_RISCV_32_PCREL 57 + +#define R_RISCV_NUM 58 + + +#endif /* elf.h */ diff --git a/05/tcc-final/examples/ex1.c b/05/tcc-final/examples/ex1.c new file mode 100755 index 0000000..3d2a3e1 --- /dev/null +++ b/05/tcc-final/examples/ex1.c @@ -0,0 +1,8 @@ +#!/usr/local/bin/tcc -run +#include + +int main() +{ + printf("Hello World\n"); + return 0; +} diff --git a/05/tcc-final/examples/ex2.c b/05/tcc-final/examples/ex2.c new file mode 100644 index 0000000..d415e39 --- /dev/null +++ b/05/tcc-final/examples/ex2.c @@ -0,0 +1,98 @@ +#include +#include + +#define N 20 + +int nb_num; +int tab[N]; +int stack_ptr; +int stack_op[N]; +int stack_res[60]; +int result; + +int find(int n, int i1, int a, int b, int op) +{ + int i, j; + int c; + + if (stack_ptr >= 0) { + stack_res[3*stack_ptr] = a; + stack_op[stack_ptr] = op; + stack_res[3*stack_ptr+1] = b; + stack_res[3*stack_ptr+2] = n; + if (n == result) + return 1; + tab[i1] = n; + } + + for(i=0;i + +int fib(n) +{ + if (n <= 2) + return 1; + else + return fib(n-1) + fib(n-2); +} + +int main(int argc, char **argv) +{ + int n; + if (argc < 2) { + printf("usage: fib n\n" + "Compute nth Fibonacci number\n"); + return 1; + } + + n = atoi(argv[1]); + printf("fib(%d) = %d\n", n, fib(n, 2)); + return 0; +} diff --git a/05/tcc-final/examples/ex4.c b/05/tcc-final/examples/ex4.c new file mode 100755 index 0000000..f92c0da --- /dev/null +++ b/05/tcc-final/examples/ex4.c @@ -0,0 +1,26 @@ +#!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11 +#include +#include +#include + +/* Yes, TCC can use X11 too ! */ + +int main(int argc, char **argv) +{ + Display *display; + Screen *screen; + + display = XOpenDisplay(""); + if (!display) { + fprintf(stderr, "Could not open X11 display\n"); + exit(1); + } + printf("X11 display opened.\n"); + screen = XScreenOfDisplay(display, 0); + printf("width = %d\nheight = %d\ndepth = %d\n", + screen->width, + screen->height, + screen->root_depth); + XCloseDisplay(display); + return 0; +} diff --git a/05/tcc-final/examples/ex5.c b/05/tcc-final/examples/ex5.c new file mode 100644 index 0000000..156425e --- /dev/null +++ b/05/tcc-final/examples/ex5.c @@ -0,0 +1,8 @@ +#include +#include + +int main() +{ + printf("Hello World\n"); + return 0; +} diff --git a/05/tcc-final/i386-asm.c b/05/tcc-final/i386-asm.c new file mode 100644 index 0000000..ebdfe03 --- /dev/null +++ b/05/tcc-final/i386-asm.c @@ -0,0 +1,1727 @@ +/* + * i386 specific functions for TCC assembler + * + * Copyright (c) 2001, 2002 Fabrice Bellard + * Copyright (c) 2009 Frédéric Feret (x86_64 support) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define USING_GLOBALS +#include "tcc.h" + +#define MAX_OPERANDS 3 + +#define TOK_ASM_first TOK_ASM_clc +#define TOK_ASM_last TOK_ASM_emms +#define TOK_ASM_alllast TOK_ASM_subps + +#define OPC_B 0x01 /* only used with OPC_WL */ +#define OPC_WL 0x02 /* accepts w, l or no suffix */ +#define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */ +#define OPC_REG 0x04 /* register is added to opcode */ +#define OPC_MODRM 0x08 /* modrm encoding */ + +#define OPCT_MASK 0x70 +#define OPC_FWAIT 0x10 /* add fwait opcode */ +#define OPC_SHIFT 0x20 /* shift opcodes */ +#define OPC_ARITH 0x30 /* arithmetic opcodes */ +#define OPC_FARITH 0x40 /* FPU arithmetic opcodes */ +#define OPC_TEST 0x50 /* test opcodes */ +#define OPCT_IS(v,i) (((v) & OPCT_MASK) == (i)) + +#define OPC_0F 0x100 /* Is secondary map (0x0f prefix) */ +#define OPC_48 0x200 /* Always has REX prefix */ +#ifdef TCC_TARGET_X86_64 +# define OPC_WLQ 0x1000 /* accepts w, l, q or no suffix */ +# define OPC_BWLQ (OPC_B | OPC_WLQ) /* accepts b, w, l, q or no suffix */ +# define OPC_WLX OPC_WLQ +# define OPC_BWLX OPC_BWLQ +#else +# define OPC_WLX OPC_WL +# define OPC_BWLX OPC_BWL +#endif + +#define OPC_GROUP_SHIFT 13 + +/* in order to compress the operand type, we use specific operands and + we or only with EA */ +enum { + OPT_REG8=0, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_REG16, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_REG32, /* warning: value is hardcoded from TOK_ASM_xxx */ +#ifdef TCC_TARGET_X86_64 + OPT_REG64, /* warning: value is hardcoded from TOK_ASM_xxx */ +#endif + OPT_MMX, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_SSE, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_CR, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_TR, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_DB, /* warning: value is hardcoded from TOK_ASM_xxx */ + OPT_SEG, + OPT_ST, +#ifdef TCC_TARGET_X86_64 + OPT_REG8_LOW, /* %spl,%bpl,%sil,%dil, encoded like ah,ch,dh,bh, but + with REX prefix, not used in insn templates */ +#endif + OPT_IM8, + OPT_IM8S, + OPT_IM16, + OPT_IM32, +#ifdef TCC_TARGET_X86_64 + OPT_IM64, +#endif + OPT_EAX, /* %al, %ax, %eax or %rax register */ + OPT_ST0, /* %st(0) register */ + OPT_CL, /* %cl register */ + OPT_DX, /* %dx register */ + OPT_ADDR, /* OP_EA with only offset */ + OPT_INDIR, /* *(expr) */ + /* composite types */ + OPT_COMPOSITE_FIRST, + OPT_IM, /* IM8 | IM16 | IM32 */ + OPT_REG, /* REG8 | REG16 | REG32 | REG64 */ + OPT_REGW, /* REG16 | REG32 | REG64 */ + OPT_IMW, /* IM16 | IM32 */ + OPT_MMXSSE, /* MMX | SSE */ + OPT_DISP, /* Like OPT_ADDR, but emitted as displacement (for jumps) */ + OPT_DISP8, /* Like OPT_ADDR, but only 8bit (short jumps) */ + /* can be ored with any OPT_xxx */ + OPT_EA = 0x80 +}; + +#define OP_REG8 (1 << OPT_REG8) +#define OP_REG16 (1 << OPT_REG16) +#define OP_REG32 (1 << OPT_REG32) +#define OP_MMX (1 << OPT_MMX) +#define OP_SSE (1 << OPT_SSE) +#define OP_CR (1 << OPT_CR) +#define OP_TR (1 << OPT_TR) +#define OP_DB (1 << OPT_DB) +#define OP_SEG (1 << OPT_SEG) +#define OP_ST (1 << OPT_ST) +#define OP_IM8 (1 << OPT_IM8) +#define OP_IM8S (1 << OPT_IM8S) +#define OP_IM16 (1 << OPT_IM16) +#define OP_IM32 (1 << OPT_IM32) +#define OP_EAX (1 << OPT_EAX) +#define OP_ST0 (1 << OPT_ST0) +#define OP_CL (1 << OPT_CL) +#define OP_DX (1 << OPT_DX) +#define OP_ADDR (1 << OPT_ADDR) +#define OP_INDIR (1 << OPT_INDIR) +#ifdef TCC_TARGET_X86_64 +# define OP_REG64 (1 << OPT_REG64) +# define OP_REG8_LOW (1 << OPT_REG8_LOW) +# define OP_IM64 (1 << OPT_IM64) +# define OP_EA32 (OP_EA << 1) +#else +# define OP_REG64 0 +# define OP_REG8_LOW 0 +# define OP_IM64 0 +# define OP_EA32 0 +#endif + +#define OP_EA 0x40000000 +#define OP_REG (OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64) + +#ifdef TCC_TARGET_X86_64 +# define TREG_XAX TREG_RAX +# define TREG_XCX TREG_RCX +# define TREG_XDX TREG_RDX +#else +# define TREG_XAX TREG_EAX +# define TREG_XCX TREG_ECX +# define TREG_XDX TREG_EDX +#endif + +typedef struct ASMInstr { + uint16_t sym; + uint16_t opcode; + uint16_t instr_type; + uint8_t nb_ops; + uint8_t op_type[MAX_OPERANDS]; /* see OP_xxx */ +} ASMInstr; + +typedef struct Operand { + uint32_t type; + int8_t reg; /* register, -1 if none */ + int8_t reg2; /* second register, -1 if none */ + uint8_t shift; + ExprValue e; +} Operand; + +static const uint8_t reg_to_size[9] = { +/* + [OP_REG8] = 0, + [OP_REG16] = 1, + [OP_REG32] = 2, +#ifdef TCC_TARGET_X86_64 + [OP_REG64] = 3, +#endif +*/ + 0, 0, 1, 0, 2, 0, 0, 0, 3 +}; + +#define NB_TEST_OPCODES 30 + +static const uint8_t test_bits[NB_TEST_OPCODES] = { + 0x00, /* o */ + 0x01, /* no */ + 0x02, /* b */ + 0x02, /* c */ + 0x02, /* nae */ + 0x03, /* nb */ + 0x03, /* nc */ + 0x03, /* ae */ + 0x04, /* e */ + 0x04, /* z */ + 0x05, /* ne */ + 0x05, /* nz */ + 0x06, /* be */ + 0x06, /* na */ + 0x07, /* nbe */ + 0x07, /* a */ + 0x08, /* s */ + 0x09, /* ns */ + 0x0a, /* p */ + 0x0a, /* pe */ + 0x0b, /* np */ + 0x0b, /* po */ + 0x0c, /* l */ + 0x0c, /* nge */ + 0x0d, /* nl */ + 0x0d, /* ge */ + 0x0e, /* le */ + 0x0e, /* ng */ + 0x0f, /* nle */ + 0x0f, /* g */ +}; + +static const uint8_t segment_prefixes[] = { + 0x26, /* es */ + 0x2e, /* cs */ + 0x36, /* ss */ + 0x3e, /* ds */ + 0x64, /* fs */ + 0x65 /* gs */ +}; + +static const ASMInstr asm_instrs[] = { +#define ALT(x) x +/* This removes a 0x0f in the second byte */ +#define O(o) ((uint64_t) ((((o) & 0xff00) == 0x0f00) ? ((((o) >> 8) & ~0xff) | ((o) & 0xff)) : (o))) +/* This constructs instr_type from opcode, type and group. */ +#define T(o,i,g) ((i) | ((g) << OPC_GROUP_SHIFT) | ((((o) & 0xff00) == 0x0f00) ? OPC_0F : 0)) +#define DEF_ASM_OP0(name, opcode) +#define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 0, { 0 } }, +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 1, { op0 }}, +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 2, { op0, op1 }}, +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) { TOK_ASM_ ## name, O(opcode), T(opcode, instr_type, group), 3, { op0, op1, op2 }}, +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif + /* last operation */ + { 0, }, +}; + +static const uint16_t op0_codes[] = { +#define ALT(x) +#define DEF_ASM_OP0(x, opcode) opcode, +#define DEF_ASM_OP0L(name, opcode, group, instr_type) +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif +}; + +static inline int get_reg_shift(TCCState *s1) +{ + int shift, v; + v = asm_int_expr(s1); + switch(v) { + case 1: + shift = 0; + break; + case 2: + shift = 1; + break; + case 4: + shift = 2; + break; + case 8: + shift = 3; + break; + default: + expect("1, 2, 4 or 8 constant"); + shift = 0; + break; + } + return shift; +} + +#ifdef TCC_TARGET_X86_64 +static int asm_parse_numeric_reg(int t, unsigned int *type) +{ + int reg = -1; + if (t >= TOK_IDENT && t < tok_ident) { + const char *s = table_ident[t - TOK_IDENT]->str; + char c; + *type = OP_REG64; + if (*s == 'c') { + s++; + *type = OP_CR; + } + if (*s++ != 'r') + return -1; + /* Don't allow leading '0'. */ + if ((c = *s++) >= '1' && c <= '9') + reg = c - '0'; + else + return -1; + if ((c = *s) >= '0' && c <= '5') + s++, reg = reg * 10 + c - '0'; + if (reg > 15) + return -1; + if ((c = *s) == 0) + ; + else if (*type != OP_REG64) + return -1; + else if (c == 'b' && !s[1]) + *type = OP_REG8; + else if (c == 'w' && !s[1]) + *type = OP_REG16; + else if (c == 'd' && !s[1]) + *type = OP_REG32; + else + return -1; + } + return reg; +} +#endif + +static int asm_parse_reg(unsigned int *type) +{ + int reg = 0; + *type = 0; + if (tok != '%') + goto error_32; + next(); + if (tok >= TOK_ASM_eax && tok <= TOK_ASM_edi) { + reg = tok - TOK_ASM_eax; + *type = OP_REG32; +#ifdef TCC_TARGET_X86_64 + } else if (tok >= TOK_ASM_rax && tok <= TOK_ASM_rdi) { + reg = tok - TOK_ASM_rax; + *type = OP_REG64; + } else if (tok == TOK_ASM_rip) { + reg = -2; /* Probably should use different escape code. */ + *type = OP_REG64; + } else if ((reg = asm_parse_numeric_reg(tok, type)) >= 0 + && (*type == OP_REG32 || *type == OP_REG64)) { + ; +#endif + } else { + error_32: + expect("register"); + } + next(); + return reg; +} + +static void parse_operand(TCCState *s1, Operand *op) +{ + ExprValue e; + int reg, indir; + const char *p; + + indir = 0; + if (tok == '*') { + next(); + indir = OP_INDIR; + } + + if (tok == '%') { + next(); + if (tok >= TOK_ASM_al && tok <= TOK_ASM_db7) { + reg = tok - TOK_ASM_al; + op->type = 1 << (reg >> 3); /* WARNING: do not change constant order */ + op->reg = reg & 7; + if ((op->type & OP_REG) && op->reg == TREG_XAX) + op->type |= OP_EAX; + else if (op->type == OP_REG8 && op->reg == TREG_XCX) + op->type |= OP_CL; + else if (op->type == OP_REG16 && op->reg == TREG_XDX) + op->type |= OP_DX; + } else if (tok >= TOK_ASM_dr0 && tok <= TOK_ASM_dr7) { + op->type = OP_DB; + op->reg = tok - TOK_ASM_dr0; + } else if (tok >= TOK_ASM_es && tok <= TOK_ASM_gs) { + op->type = OP_SEG; + op->reg = tok - TOK_ASM_es; + } else if (tok == TOK_ASM_st) { + op->type = OP_ST; + op->reg = 0; + next(); + if (tok == '(') { + next(); + if (tok != TOK_PPNUM) + goto reg_error; + p = tokc.str.data; + reg = p[0] - '0'; + if ((unsigned)reg >= 8 || p[1] != '\0') + goto reg_error; + op->reg = reg; + next(); + skip(')'); + } + if (op->reg == 0) + op->type |= OP_ST0; + goto no_skip; +#ifdef TCC_TARGET_X86_64 + } else if (tok >= TOK_ASM_spl && tok <= TOK_ASM_dil) { + op->type = OP_REG8 | OP_REG8_LOW; + op->reg = 4 + tok - TOK_ASM_spl; + } else if ((op->reg = asm_parse_numeric_reg(tok, &op->type)) >= 0) { + ; +#endif + } else { + reg_error: + tcc_error("unknown register %%%s", get_tok_str(tok, &tokc)); + } + next(); + no_skip: ; + } else if (tok == '$') { + /* constant value */ + next(); + asm_expr(s1, &e); + op->type = OP_IM32; + op->e = e; + if (!op->e.sym) { + if (op->e.v == (uint8_t)op->e.v) + op->type |= OP_IM8; + if (op->e.v == (int8_t)op->e.v) + op->type |= OP_IM8S; + if (op->e.v == (uint16_t)op->e.v) + op->type |= OP_IM16; +#ifdef TCC_TARGET_X86_64 + if (op->e.v != (int32_t)op->e.v && op->e.v != (uint32_t)op->e.v) + op->type = OP_IM64; +#endif + } + } else { + /* address(reg,reg2,shift) with all variants */ + op->type = OP_EA; + op->reg = -1; + op->reg2 = -1; + op->shift = 0; + if (tok != '(') { + asm_expr(s1, &e); + op->e = e; + } else { + next(); + if (tok == '%') { + unget_tok('('); + op->e.v = 0; + op->e.sym = NULL; + } else { + /* bracketed offset expression */ + asm_expr(s1, &e); + if (tok != ')') + expect(")"); + next(); + op->e.v = e.v; + op->e.sym = e.sym; + } + op->e.pcrel = 0; + } + if (tok == '(') { + unsigned int type = 0; + next(); + if (tok != ',') { + op->reg = asm_parse_reg(&type); + } + if (tok == ',') { + next(); + if (tok != ',') { + op->reg2 = asm_parse_reg(&type); + } + if (tok == ',') { + next(); + op->shift = get_reg_shift(s1); + } + } + if (type & OP_REG32) + op->type |= OP_EA32; + skip(')'); + } + if (op->reg == -1 && op->reg2 == -1) + op->type |= OP_ADDR; + } + op->type |= indir; +} + +/* XXX: unify with C code output ? */ +ST_FUNC void gen_expr32(ExprValue *pe) +{ + if (pe->pcrel) + /* If PC-relative, always set VT_SYM, even without symbol, + so as to force a relocation to be emitted. */ + gen_addrpc32(VT_SYM, pe->sym, pe->v); + else + gen_addr32(pe->sym ? VT_SYM : 0, pe->sym, pe->v); +} + +#ifdef TCC_TARGET_X86_64 +ST_FUNC void gen_expr64(ExprValue *pe) +{ + gen_addr64(pe->sym ? VT_SYM : 0, pe->sym, pe->v); +} +#endif + +/* XXX: unify with C code output ? */ +static void gen_disp32(ExprValue *pe) +{ + Sym *sym = pe->sym; + ElfSym *esym = elfsym(sym); + if (esym && esym->st_shndx == cur_text_section->sh_num) { + /* same section: we can output an absolute value. Note + that the TCC compiler behaves differently here because + it always outputs a relocation to ease (future) code + elimination in the linker */ + gen_le32(pe->v + esym->st_value - ind - 4); + } else { + if (sym && sym->type.t == VT_VOID) { + sym->type.t = VT_FUNC; + sym->type.ref = NULL; + } + gen_addrpc32(VT_SYM, sym, pe->v); + } +} + +/* generate the modrm operand */ +static inline int asm_modrm(int reg, Operand *op) +{ + int mod, reg1, reg2, sib_reg1; + + if (op->type & (OP_REG | OP_MMX | OP_SSE)) { + g(0xc0 + (reg << 3) + op->reg); + } else if (op->reg == -1 && op->reg2 == -1) { + /* displacement only */ +#ifdef TCC_TARGET_X86_64 + g(0x04 + (reg << 3)); + g(0x25); +#else + g(0x05 + (reg << 3)); +#endif + gen_expr32(&op->e); +#ifdef TCC_TARGET_X86_64 + } else if (op->reg == -2) { + ExprValue *pe = &op->e; + g(0x05 + (reg << 3)); + gen_addrpc32(pe->sym ? VT_SYM : 0, pe->sym, pe->v); + return ind; +#endif + } else { + sib_reg1 = op->reg; + /* fist compute displacement encoding */ + if (sib_reg1 == -1) { + sib_reg1 = 5; + mod = 0x00; + } else if (op->e.v == 0 && !op->e.sym && op->reg != 5) { + mod = 0x00; + } else if (op->e.v == (int8_t)op->e.v && !op->e.sym) { + mod = 0x40; + } else { + mod = 0x80; + } + /* compute if sib byte needed */ + reg1 = op->reg; + if (op->reg2 != -1) + reg1 = 4; + g(mod + (reg << 3) + reg1); + if (reg1 == 4) { + /* add sib byte */ + reg2 = op->reg2; + if (reg2 == -1) + reg2 = 4; /* indicate no index */ + g((op->shift << 6) + (reg2 << 3) + sib_reg1); + } + /* add offset */ + if (mod == 0x40) { + g(op->e.v); + } else if (mod == 0x80 || op->reg == -1) { + gen_expr32(&op->e); + } + } + return 0; +} + +#ifdef TCC_TARGET_X86_64 +#define REX_W 0x48 +#define REX_R 0x44 +#define REX_X 0x42 +#define REX_B 0x41 + +static void asm_rex(int width64, Operand *ops, int nb_ops, int *op_type, + int regi, int rmi) +{ + unsigned char rex = width64 ? 0x48 : 0; + int saw_high_8bit = 0; + int i; + if (rmi == -1) { + /* No mod/rm byte, but we might have a register op nevertheless + (we will add it to the opcode later). */ + for(i = 0; i < nb_ops; i++) { + if (op_type[i] & (OP_REG | OP_ST)) { + if (ops[i].reg >= 8) { + rex |= REX_B; + ops[i].reg -= 8; + } else if (ops[i].type & OP_REG8_LOW) + rex |= 0x40; + else if (ops[i].type & OP_REG8 && ops[i].reg >= 4) + /* An 8 bit reg >= 4 without REG8 is ah/ch/dh/bh */ + saw_high_8bit = ops[i].reg; + break; + } + } + } else { + if (regi != -1) { + if (ops[regi].reg >= 8) { + rex |= REX_R; + ops[regi].reg -= 8; + } else if (ops[regi].type & OP_REG8_LOW) + rex |= 0x40; + else if (ops[regi].type & OP_REG8 && ops[regi].reg >= 4) + /* An 8 bit reg >= 4 without REG8 is ah/ch/dh/bh */ + saw_high_8bit = ops[regi].reg; + } + if (ops[rmi].type & (OP_REG | OP_MMX | OP_SSE | OP_CR | OP_EA)) { + if (ops[rmi].reg >= 8) { + rex |= REX_B; + ops[rmi].reg -= 8; + } else if (ops[rmi].type & OP_REG8_LOW) + rex |= 0x40; + else if (ops[rmi].type & OP_REG8 && ops[rmi].reg >= 4) + /* An 8 bit reg >= 4 without REG8 is ah/ch/dh/bh */ + saw_high_8bit = ops[rmi].reg; + } + if (ops[rmi].type & OP_EA && ops[rmi].reg2 >= 8) { + rex |= REX_X; + ops[rmi].reg2 -= 8; + } + } + if (rex) { + if (saw_high_8bit) + tcc_error("can't encode register %%%ch when REX prefix is required", + "acdb"[saw_high_8bit-4]); + g(rex); + } +} +#endif + + +static void maybe_print_stats (void) +{ + static int already; + + if (0 && !already) + /* print stats about opcodes */ + { + const struct ASMInstr *pa; + int freq[4]; + int op_vals[500]; + int nb_op_vals, i, j; + + already = 1; + nb_op_vals = 0; + memset(freq, 0, sizeof(freq)); + for(pa = asm_instrs; pa->sym != 0; pa++) { + freq[pa->nb_ops]++; + //for(i=0;inb_ops;i++) { + for(j=0;jop_type[i] == op_vals[j]) + if (pa->instr_type == op_vals[j]) + goto found; + } + //op_vals[nb_op_vals++] = pa->op_type[i]; + op_vals[nb_op_vals++] = pa->instr_type; + found: ; + //} + } + for(i=0;i= TOK_ASM_wait && opcode <= TOK_ASM_repnz) + unget_tok(';'); + + /* get operands */ + pop = ops; + nb_ops = 0; + seg_prefix = 0; + alltypes = 0; + for(;;) { + if (tok == ';' || tok == TOK_LINEFEED) + break; + if (nb_ops >= MAX_OPERANDS) { + tcc_error("incorrect number of operands"); + } + parse_operand(s1, pop); + if (tok == ':') { + if (pop->type != OP_SEG || seg_prefix) + tcc_error("incorrect prefix"); + seg_prefix = segment_prefixes[pop->reg]; + next(); + parse_operand(s1, pop); + if (!(pop->type & OP_EA)) { + tcc_error("segment prefix must be followed by memory reference"); + } + } + pop++; + nb_ops++; + if (tok != ',') + break; + next(); + } + + s = 0; /* avoid warning */ + +again: + /* optimize matching by using a lookup table (no hashing is needed + !) */ + for(pa = asm_instrs; pa->sym != 0; pa++) { + int it = pa->instr_type & OPCT_MASK; + s = 0; + if (it == OPC_FARITH) { + v = opcode - pa->sym; + if (!((unsigned)v < 8 * 6 && (v % 6) == 0)) + continue; + } else if (it == OPC_ARITH) { + if (!(opcode >= pa->sym && opcode < pa->sym + 8*NBWLX)) + continue; + s = (opcode - pa->sym) % NBWLX; + if ((pa->instr_type & OPC_BWLX) == OPC_WLX) + { + /* We need to reject the xxxb opcodes that we accepted above. + Note that pa->sym for WLX opcodes is the 'w' token, + to get the 'b' token subtract one. */ + if (((opcode - pa->sym + 1) % NBWLX) == 0) + continue; + s++; + } + } else if (it == OPC_SHIFT) { + if (!(opcode >= pa->sym && opcode < pa->sym + 7*NBWLX)) + continue; + s = (opcode - pa->sym) % NBWLX; + } else if (it == OPC_TEST) { + if (!(opcode >= pa->sym && opcode < pa->sym + NB_TEST_OPCODES)) + continue; + /* cmovxx is a test opcode but accepts multiple sizes. + The suffixes aren't encoded in the table, instead we + simply force size autodetection always and deal with suffixed + variants below when we don't find e.g. "cmovzl". */ + if (pa->instr_type & OPC_WLX) + s = NBWLX - 1; + } else if (pa->instr_type & OPC_B) { +#ifdef TCC_TARGET_X86_64 + /* Some instructions don't have the full size but only + bwl form. insb e.g. */ + if ((pa->instr_type & OPC_WLQ) != OPC_WLQ + && !(opcode >= pa->sym && opcode < pa->sym + NBWLX-1)) + continue; +#endif + if (!(opcode >= pa->sym && opcode < pa->sym + NBWLX)) + continue; + s = opcode - pa->sym; + } else if (pa->instr_type & OPC_WLX) { + if (!(opcode >= pa->sym && opcode < pa->sym + NBWLX-1)) + continue; + s = opcode - pa->sym + 1; + } else { + if (pa->sym != opcode) + continue; + } + if (pa->nb_ops != nb_ops) + continue; +#ifdef TCC_TARGET_X86_64 + /* Special case for moves. Selecting the IM64->REG64 form + should only be done if we really have an >32bit imm64, and that + is hardcoded. Ignore it here. */ + if (pa->opcode == 0xb0 && ops[0].type != OP_IM64 + && (ops[1].type & OP_REG) == OP_REG64 + && !(pa->instr_type & OPC_0F)) + continue; +#endif + /* now decode and check each operand */ + alltypes = 0; + for(i = 0; i < nb_ops; i++) { + int op1, op2; + op1 = pa->op_type[i]; + op2 = op1 & 0x1f; + switch(op2) { + case OPT_IM: + v = OP_IM8 | OP_IM16 | OP_IM32; + break; + case OPT_REG: + v = OP_REG8 | OP_REG16 | OP_REG32 | OP_REG64; + break; + case OPT_REGW: + v = OP_REG16 | OP_REG32 | OP_REG64; + break; + case OPT_IMW: + v = OP_IM16 | OP_IM32; + break; + case OPT_MMXSSE: + v = OP_MMX | OP_SSE; + break; + case OPT_DISP: + case OPT_DISP8: + v = OP_ADDR; + break; + default: + v = 1 << op2; + break; + } + if (op1 & OPT_EA) + v |= OP_EA; + op_type[i] = v; + if ((ops[i].type & v) == 0) + goto next; + alltypes |= ops[i].type; + } + (void)alltypes; /* maybe unused */ + /* all is matching ! */ + break; + next: ; + } + if (pa->sym == 0) { + if (opcode >= TOK_ASM_first && opcode <= TOK_ASM_last) { + int b; + b = op0_codes[opcode - TOK_ASM_first]; + if (b & 0xff00) + g(b >> 8); + g(b); + return; + } else if (opcode <= TOK_ASM_alllast) { + tcc_error("bad operand with opcode '%s'", + get_tok_str(opcode, NULL)); + } else { + /* Special case for cmovcc, we accept size suffixes but ignore + them, but we don't want them to blow up our tables. */ + TokenSym *ts = table_ident[opcode - TOK_IDENT]; + if (ts->len >= 6 + && strchr("wlq", ts->str[ts->len-1]) + && !memcmp(ts->str, "cmov", 4)) { + opcode = tok_alloc(ts->str, ts->len-1)->tok; + goto again; + } + tcc_error("unknown opcode '%s'", ts->str); + } + } + /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */ + autosize = NBWLX-1; +#ifdef TCC_TARGET_X86_64 + /* XXX the autosize should rather be zero, to not have to adjust this + all the time. */ + if ((pa->instr_type & OPC_BWLQ) == OPC_B) + autosize = NBWLX-2; +#endif + if (s == autosize) { + /* Check for register operands providing hints about the size. + Start from the end, i.e. destination operands. This matters + only for opcodes accepting different sized registers, lar and lsl + are such opcodes. */ + for(i = nb_ops - 1; s == autosize && i >= 0; i--) { + if ((ops[i].type & OP_REG) && !(op_type[i] & (OP_CL | OP_DX))) + s = reg_to_size[ops[i].type & OP_REG]; + } + if (s == autosize) { + if ((opcode == TOK_ASM_push || opcode == TOK_ASM_pop) && + (ops[0].type & (OP_SEG | OP_IM8S | OP_IM32))) + s = 2; + else if ((opcode == TOK_ASM_push || opcode == TOK_ASM_pop) && + (ops[0].type & OP_EA)) + s = NBWLX - 2; + else + tcc_error("cannot infer opcode suffix"); + } + } + +#ifdef TCC_TARGET_X86_64 + /* Generate addr32 prefix if needed */ + for(i = 0; i < nb_ops; i++) { + if (ops[i].type & OP_EA32) { + g(0x67); + break; + } + } +#endif + /* generate data16 prefix if needed */ + p66 = 0; + if (s == 1) + p66 = 1; + else { + /* accepting mmx+sse in all operands --> needs 0x66 to + switch to sse mode. Accepting only sse in an operand --> is + already SSE insn and needs 0x66/f2/f3 handling. */ + for (i = 0; i < nb_ops; i++) + if ((op_type[i] & (OP_MMX | OP_SSE)) == (OP_MMX | OP_SSE) + && ops[i].type & OP_SSE) + p66 = 1; + } + if (p66) + g(0x66); +#ifdef TCC_TARGET_X86_64 + rex64 = 0; + if (pa->instr_type & OPC_48) + rex64 = 1; + else if (s == 3 || (alltypes & OP_REG64)) { + /* generate REX prefix */ + int default64 = 0; + for(i = 0; i < nb_ops; i++) { + if (op_type[i] == OP_REG64 && pa->opcode != 0xb8) { + /* If only 64bit regs are accepted in one operand + this is a default64 instruction without need for + REX prefixes, except for movabs(0xb8). */ + default64 = 1; + break; + } + } + /* XXX find better encoding for the default64 instructions. */ + if (((opcode != TOK_ASM_push && opcode != TOK_ASM_pop + && opcode != TOK_ASM_pushw && opcode != TOK_ASM_pushl + && opcode != TOK_ASM_pushq && opcode != TOK_ASM_popw + && opcode != TOK_ASM_popl && opcode != TOK_ASM_popq + && opcode != TOK_ASM_call && opcode != TOK_ASM_jmp)) + && !default64) + rex64 = 1; + } +#endif + + /* now generates the operation */ + if (OPCT_IS(pa->instr_type, OPC_FWAIT)) + g(0x9b); + if (seg_prefix) + g(seg_prefix); + + v = pa->opcode; + if (pa->instr_type & OPC_0F) + v = ((v & ~0xff) << 8) | 0x0f00 | (v & 0xff); + if ((v == 0x69 || v == 0x6b) && nb_ops == 2) { + /* kludge for imul $im, %reg */ + nb_ops = 3; + ops[2] = ops[1]; + op_type[2] = op_type[1]; + } else if (v == 0xcd && ops[0].e.v == 3 && !ops[0].e.sym) { + v--; /* int $3 case */ + nb_ops = 0; + } else if ((v == 0x06 || v == 0x07)) { + if (ops[0].reg >= 4) { + /* push/pop %fs or %gs */ + v = 0x0fa0 + (v - 0x06) + ((ops[0].reg - 4) << 3); + } else { + v += ops[0].reg << 3; + } + nb_ops = 0; + } else if (v <= 0x05) { + /* arith case */ + v += ((opcode - TOK_ASM_addb) / NBWLX) << 3; + } else if ((pa->instr_type & (OPCT_MASK | OPC_MODRM)) == OPC_FARITH) { + /* fpu arith case */ + v += ((opcode - pa->sym) / 6) << 3; + } + + /* search which operand will be used for modrm */ + modrm_index = -1; + modreg_index = -1; + if (pa->instr_type & OPC_MODRM) { + if (!nb_ops) { + /* A modrm opcode without operands is a special case (e.g. mfence). + It has a group and acts as if there's an register operand 0 + (ax). */ + i = 0; + ops[i].type = OP_REG; + ops[i].reg = 0; + goto modrm_found; + } + /* first look for an ea operand */ + for(i = 0;i < nb_ops; i++) { + if (op_type[i] & OP_EA) + goto modrm_found; + } + /* then if not found, a register or indirection (shift instructions) */ + for(i = 0;i < nb_ops; i++) { + if (op_type[i] & (OP_REG | OP_MMX | OP_SSE | OP_INDIR)) + goto modrm_found; + } +#ifdef ASM_DEBUG + tcc_error("bad op table"); +#endif + modrm_found: + modrm_index = i; + /* if a register is used in another operand then it is + used instead of group */ + for(i = 0;i < nb_ops; i++) { + int t = op_type[i]; + if (i != modrm_index && + (t & (OP_REG | OP_MMX | OP_SSE | OP_CR | OP_TR | OP_DB | OP_SEG))) { + modreg_index = i; + break; + } + } + } +#ifdef TCC_TARGET_X86_64 + asm_rex (rex64, ops, nb_ops, op_type, modreg_index, modrm_index); +#endif + + if (pa->instr_type & OPC_REG) { + /* mov $im, %reg case */ + if (v == 0xb0 && s >= 1) + v += 7; + for(i = 0; i < nb_ops; i++) { + if (op_type[i] & (OP_REG | OP_ST)) { + v += ops[i].reg; + break; + } + } + } + if (pa->instr_type & OPC_B) + v += s >= 1; + if (nb_ops == 1 && pa->op_type[0] == OPT_DISP8) { + ElfSym *esym; + int jmp_disp; + + /* see if we can really generate the jump with a byte offset */ + esym = elfsym(ops[0].e.sym); + if (!esym || esym->st_shndx != cur_text_section->sh_num) + goto no_short_jump; + jmp_disp = ops[0].e.v + esym->st_value - ind - 2 - (v >= 0xff); + if (jmp_disp == (int8_t)jmp_disp) { + /* OK to generate jump */ + ops[0].e.sym = 0; + ops[0].e.v = jmp_disp; + op_type[0] = OP_IM8S; + } else { + no_short_jump: + /* long jump will be allowed. need to modify the + opcode slightly */ + if (v == 0xeb) /* jmp */ + v = 0xe9; + else if (v == 0x70) /* jcc */ + v += 0x0f10; + else + tcc_error("invalid displacement"); + } + } + if (OPCT_IS(pa->instr_type, OPC_TEST)) + v += test_bits[opcode - pa->sym]; + op1 = v >> 16; + if (op1) + g(op1); + op1 = (v >> 8) & 0xff; + if (op1) + g(op1); + g(v); + + if (OPCT_IS(pa->instr_type, OPC_SHIFT)) { + reg = (opcode - pa->sym) / NBWLX; + if (reg == 6) + reg = 7; + } else if (OPCT_IS(pa->instr_type, OPC_ARITH)) { + reg = (opcode - pa->sym) / NBWLX; + } else if (OPCT_IS(pa->instr_type, OPC_FARITH)) { + reg = (opcode - pa->sym) / 6; + } else { + reg = (pa->instr_type >> OPC_GROUP_SHIFT) & 7; + } + + pc = 0; + if (pa->instr_type & OPC_MODRM) { + /* if a register is used in another operand then it is + used instead of group */ + if (modreg_index >= 0) + reg = ops[modreg_index].reg; + pc = asm_modrm(reg, &ops[modrm_index]); + } + + /* emit constants */ +#ifndef TCC_TARGET_X86_64 + if (!(pa->instr_type & OPC_0F) + && (pa->opcode == 0x9a || pa->opcode == 0xea)) { + /* ljmp or lcall kludge */ + gen_expr32(&ops[1].e); + if (ops[0].e.sym) + tcc_error("cannot relocate"); + gen_le16(ops[0].e.v); + return; + } +#endif + for(i = 0;i < nb_ops; i++) { + v = op_type[i]; + if (v & (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64 | OP_IM8S | OP_ADDR)) { + /* if multiple sizes are given it means we must look + at the op size */ + if ((v | OP_IM8 | OP_IM64) == (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM64)) { + if (s == 0) + v = OP_IM8; + else if (s == 1) + v = OP_IM16; + else if (s == 2 || (v & OP_IM64) == 0) + v = OP_IM32; + else + v = OP_IM64; + } + + if ((v & (OP_IM8 | OP_IM8S | OP_IM16)) && ops[i].e.sym) + tcc_error("cannot relocate"); + + if (v & (OP_IM8 | OP_IM8S)) { + g(ops[i].e.v); + } else if (v & OP_IM16) { + gen_le16(ops[i].e.v); +#ifdef TCC_TARGET_X86_64 + } else if (v & OP_IM64) { + gen_expr64(&ops[i].e); +#endif + } else if (pa->op_type[i] == OPT_DISP || pa->op_type[i] == OPT_DISP8) { + gen_disp32(&ops[i].e); + } else { + gen_expr32(&ops[i].e); + } + } + } + + /* after immediate operands, adjust pc-relative address */ + if (pc) + add32le(cur_text_section->data + pc - 4, pc - ind); +} + +/* return the constraint priority (we allocate first the lowest + numbered constraints) */ +static inline int constraint_priority(const char *str) +{ + int priority, c, pr; + + /* we take the lowest priority */ + priority = 0; + for(;;) { + c = *str; + if (c == '\0') + break; + str++; + switch(c) { + case 'A': + pr = 0; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'S': + case 'D': + pr = 1; + break; + case 'q': + pr = 2; + break; + case 'r': + case 'R': + case 'p': + pr = 3; + break; + case 'N': + case 'M': + case 'I': + case 'e': + case 'i': + case 'm': + case 'g': + pr = 4; + break; + default: + tcc_error("unknown constraint '%c'", c); + pr = 0; + } + if (pr > priority) + priority = pr; + } + return priority; +} + +static const char *skip_constraint_modifiers(const char *p) +{ + while (*p == '=' || *p == '&' || *p == '+' || *p == '%') + p++; + return p; +} + +/* If T (a token) is of the form "%reg" returns the register + number and type, otherwise return -1. */ +ST_FUNC int asm_parse_regvar (int t) +{ + const char *s; + Operand op; + if (t < TOK_IDENT || (t & SYM_FIELD)) + return -1; + s = table_ident[t - TOK_IDENT]->str; + if (s[0] != '%') + return -1; + t = tok_alloc_const(s + 1); + unget_tok(t); + unget_tok('%'); + parse_operand(tcc_state, &op); + /* Accept only integer regs for now. */ + if (op.type & OP_REG) + return op.reg; + else + return -1; +} + +#define REG_OUT_MASK 0x01 +#define REG_IN_MASK 0x02 + +#define is_reg_allocated(reg) (regs_allocated[reg] & reg_mask) + +ST_FUNC void asm_compute_constraints(ASMOperand *operands, + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg) +{ + ASMOperand *op; + int sorted_op[MAX_ASM_OPERANDS]; + int i, j, k, p1, p2, tmp, reg, c, reg_mask; + const char *str; + uint8_t regs_allocated[NB_ASM_REGS]; + + /* init fields */ + for(i=0;iinput_index = -1; + op->ref_index = -1; + op->reg = -1; + op->is_memory = 0; + op->is_rw = 0; + } + /* compute constraint priority and evaluate references to output + constraints if input constraints */ + for(i=0;iconstraint; + str = skip_constraint_modifiers(str); + if (isnum(*str) || *str == '[') { + /* this is a reference to another constraint */ + k = find_constraint(operands, nb_operands, str, NULL); + if ((unsigned)k >= i || i < nb_outputs) + tcc_error("invalid reference in constraint %d ('%s')", + i, str); + op->ref_index = k; + if (operands[k].input_index >= 0) + tcc_error("cannot reference twice the same operand"); + operands[k].input_index = i; + op->priority = 5; + } else if ((op->vt->r & VT_VALMASK) == VT_LOCAL + && op->vt->sym + && (reg = op->vt->sym->r & VT_VALMASK) < VT_CONST) { + op->priority = 1; + op->reg = reg; + } else { + op->priority = constraint_priority(str); + } + } + + /* sort operands according to their priority */ + for(i=0;iconstraint; + /* no need to allocate references */ + if (op->ref_index >= 0) + continue; + /* select if register is used for output, input or both */ + if (op->input_index >= 0) { + reg_mask = REG_IN_MASK | REG_OUT_MASK; + } else if (j < nb_outputs) { + reg_mask = REG_OUT_MASK; + } else { + reg_mask = REG_IN_MASK; + } + if (op->reg >= 0) { + if (is_reg_allocated(op->reg)) + tcc_error("asm regvar requests register that's taken already"); + reg = op->reg; + goto reg_found; + } + try_next: + c = *str++; + switch(c) { + case '=': + goto try_next; + case '+': + op->is_rw = 1; + /* FALL THRU */ + case '&': + if (j >= nb_outputs) + tcc_error("'%c' modifier can only be applied to outputs", c); + reg_mask = REG_IN_MASK | REG_OUT_MASK; + goto try_next; + case 'A': + /* allocate both eax and edx */ + if (is_reg_allocated(TREG_XAX) || + is_reg_allocated(TREG_XDX)) + goto try_next; + op->is_llong = 1; + op->reg = TREG_XAX; + regs_allocated[TREG_XAX] |= reg_mask; + regs_allocated[TREG_XDX] |= reg_mask; + break; + case 'a': + reg = TREG_XAX; + goto alloc_reg; + case 'b': + reg = 3; + goto alloc_reg; + case 'c': + reg = TREG_XCX; + goto alloc_reg; + case 'd': + reg = TREG_XDX; + goto alloc_reg; + case 'S': + reg = 6; + goto alloc_reg; + case 'D': + reg = 7; + alloc_reg: + if (is_reg_allocated(reg)) + goto try_next; + goto reg_found; + case 'q': + /* eax, ebx, ecx or edx */ + for(reg = 0; reg < 4; reg++) { + if (!is_reg_allocated(reg)) + goto reg_found; + } + goto try_next; + case 'r': + case 'R': + case 'p': /* A general address, for x86(64) any register is acceptable*/ + /* any general register */ + for(reg = 0; reg < 8; reg++) { + if (!is_reg_allocated(reg)) + goto reg_found; + } + goto try_next; + reg_found: + /* now we can reload in the register */ + op->is_llong = 0; + op->reg = reg; + regs_allocated[reg] |= reg_mask; + break; + case 'e': + case 'i': + if (!((op->vt->r & (VT_VALMASK | VT_LVAL)) == VT_CONST)) + goto try_next; + break; + case 'I': + case 'N': + case 'M': + if (!((op->vt->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)) + goto try_next; + break; + case 'm': + case 'g': + /* nothing special to do because the operand is already in + memory, except if the pointer itself is stored in a + memory variable (VT_LLOCAL case) */ + /* XXX: fix constant case */ + /* if it is a reference to a memory zone, it must lie + in a register, so we reserve the register in the + input registers and a load will be generated + later */ + if (j < nb_outputs || c == 'm') { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) { + /* any general register */ + for(reg = 0; reg < 8; reg++) { + if (!(regs_allocated[reg] & REG_IN_MASK)) + goto reg_found1; + } + goto try_next; + reg_found1: + /* now we can reload in the register */ + regs_allocated[reg] |= REG_IN_MASK; + op->reg = reg; + op->is_memory = 1; + } + } + break; + default: + tcc_error("asm constraint %d ('%s') could not be satisfied", + j, op->constraint); + break; + } + /* if a reference is present for that operand, we assign it too */ + if (op->input_index >= 0) { + operands[op->input_index].reg = op->reg; + operands[op->input_index].is_llong = op->is_llong; + } + } + + /* compute out_reg. It is used to store outputs registers to memory + locations references by pointers (VT_LLOCAL case) */ + *pout_reg = -1; + for(i=0;ireg >= 0 && + (op->vt->r & VT_VALMASK) == VT_LLOCAL && + !op->is_memory) { + for(reg = 0; reg < 8; reg++) { + if (!(regs_allocated[reg] & REG_OUT_MASK)) + goto reg_found2; + } + tcc_error("could not find free output register for reloading"); + reg_found2: + *pout_reg = reg; + break; + } + } + + /* print sorted constraints */ +#ifdef ASM_DEBUG + for(i=0;iid ? get_tok_str(op->id, NULL) : "", + op->constraint, + op->vt->r, + op->reg); + } + if (*pout_reg >= 0) + printf("out_reg=%d\n", *pout_reg); +#endif +} + +ST_FUNC void subst_asm_operand(CString *add_str, + SValue *sv, int modifier) +{ + int r, reg, size, val; + char buf[64]; + + r = sv->r; + if ((r & VT_VALMASK) == VT_CONST) { + if (!(r & VT_LVAL) && modifier != 'c' && modifier != 'n' && + modifier != 'P') + cstr_ccat(add_str, '$'); + if (r & VT_SYM) { + const char *name = get_tok_str(sv->sym->v, NULL); + if (sv->sym->v >= SYM_FIRST_ANOM) { + /* In case of anonymous symbols ("L.42", used + for static data labels) we can't find them + in the C symbol table when later looking up + this name. So enter them now into the asm label + list when we still know the symbol. */ + get_asm_sym(tok_alloc_const(name), sv->sym); + } + if (tcc_state->leading_underscore) + cstr_ccat(add_str, '_'); + cstr_cat(add_str, name, -1); + if ((uint32_t)sv->c.i == 0) + goto no_offset; + cstr_ccat(add_str, '+'); + } + val = sv->c.i; + if (modifier == 'n') + val = -val; + snprintf(buf, sizeof(buf), "%d", (int)sv->c.i); + cstr_cat(add_str, buf, -1); + no_offset:; +#ifdef TCC_TARGET_X86_64 + if (r & VT_LVAL) + cstr_cat(add_str, "(%rip)", -1); +#endif + } else if ((r & VT_VALMASK) == VT_LOCAL) { +#ifdef TCC_TARGET_X86_64 + snprintf(buf, sizeof(buf), "%d(%%rbp)", (int)sv->c.i); +#else + snprintf(buf, sizeof(buf), "%d(%%ebp)", (int)sv->c.i); +#endif + cstr_cat(add_str, buf, -1); + } else if (r & VT_LVAL) { + reg = r & VT_VALMASK; + if (reg >= VT_CONST) + tcc_internal_error(""); + snprintf(buf, sizeof(buf), "(%%%s)", +#ifdef TCC_TARGET_X86_64 + get_tok_str(TOK_ASM_rax + reg, NULL) +#else + get_tok_str(TOK_ASM_eax + reg, NULL) +#endif + ); + cstr_cat(add_str, buf, -1); + } else { + /* register case */ + reg = r & VT_VALMASK; + if (reg >= VT_CONST) + tcc_internal_error(""); + + /* choose register operand size */ + if ((sv->type.t & VT_BTYPE) == VT_BYTE || + (sv->type.t & VT_BTYPE) == VT_BOOL) + size = 1; + else if ((sv->type.t & VT_BTYPE) == VT_SHORT) + size = 2; +#ifdef TCC_TARGET_X86_64 + else if ((sv->type.t & VT_BTYPE) == VT_LLONG || + (sv->type.t & VT_BTYPE) == VT_PTR) + size = 8; +#endif + else + size = 4; + if (size == 1 && reg >= 4) + size = 4; + + if (modifier == 'b') { + if (reg >= 4) + tcc_error("cannot use byte register"); + size = 1; + } else if (modifier == 'h') { + if (reg >= 4) + tcc_error("cannot use byte register"); + size = -1; + } else if (modifier == 'w') { + size = 2; + } else if (modifier == 'k') { + size = 4; +#ifdef TCC_TARGET_X86_64 + } else if (modifier == 'q') { + size = 8; +#endif + } + + switch(size) { + case -1: + reg = TOK_ASM_ah + reg; + break; + case 1: + reg = TOK_ASM_al + reg; + break; + case 2: + reg = TOK_ASM_ax + reg; + break; + default: + reg = TOK_ASM_eax + reg; + break; +#ifdef TCC_TARGET_X86_64 + case 8: + reg = TOK_ASM_rax + reg; + break; +#endif + } + snprintf(buf, sizeof(buf), "%%%s", get_tok_str(reg, NULL)); + cstr_cat(add_str, buf, -1); + } +} + +/* generate prolog and epilog code for asm statement */ +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg) +{ + uint8_t regs_allocated[NB_ASM_REGS]; + ASMOperand *op; + int i, reg; + + /* Strictly speaking %Xbp and %Xsp should be included in the + call-preserved registers, but currently it doesn't matter. */ +#ifdef TCC_TARGET_X86_64 +#ifdef TCC_TARGET_PE + static const uint8_t reg_saved[] = { 3, 6, 7, 12, 13, 14, 15 }; +#else + static const uint8_t reg_saved[] = { 3, 12, 13, 14, 15 }; +#endif +#else + static const uint8_t reg_saved[] = { 3, 6, 7 }; +#endif + + /* mark all used registers */ + memcpy(regs_allocated, clobber_regs, sizeof(regs_allocated)); + for(i = 0; i < nb_operands;i++) { + op = &operands[i]; + if (op->reg >= 0) + regs_allocated[op->reg] = 1; + } + if (!is_output) { + /* generate reg save code */ + for(i = 0; i < sizeof(reg_saved)/sizeof(reg_saved[0]); i++) { + reg = reg_saved[i]; + if (regs_allocated[reg]) { + if (reg >= 8) + g(0x41), reg-=8; + g(0x50 + reg); + } + } + + /* generate load code */ + for(i = 0; i < nb_operands; i++) { + op = &operands[i]; + if (op->reg >= 0) { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL && + op->is_memory) { + /* memory reference case (for both input and + output cases) */ + SValue sv; + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL; + sv.type.t = VT_PTR; + load(op->reg, &sv); + } else if (i >= nb_outputs || op->is_rw) { + /* load value in register */ + load(op->reg, op->vt); + if (op->is_llong) { + SValue sv; + sv = *op->vt; + sv.c.i += 4; + load(TREG_XDX, &sv); + } + } + } + } + } else { + /* generate save code */ + for(i = 0 ; i < nb_outputs; i++) { + op = &operands[i]; + if (op->reg >= 0) { + if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) { + if (!op->is_memory) { + SValue sv; + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL; + sv.type.t = VT_PTR; + load(out_reg, &sv); + + sv = *op->vt; + sv.r = (sv.r & ~VT_VALMASK) | out_reg; + store(op->reg, &sv); + } + } else { + store(op->reg, op->vt); + if (op->is_llong) { + SValue sv; + sv = *op->vt; + sv.c.i += 4; + store(TREG_XDX, &sv); + } + } + } + } + /* generate reg restore code */ + for(i = sizeof(reg_saved)/sizeof(reg_saved[0]) - 1; i >= 0; i--) { + reg = reg_saved[i]; + if (regs_allocated[reg]) { + if (reg >= 8) + g(0x41), reg-=8; + g(0x58 + reg); + } + } + } +} + +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str) +{ + int reg; +#ifdef TCC_TARGET_X86_64 + unsigned int type; +#endif + + if (!strcmp(str, "memory") || + !strcmp(str, "cc") || + !strcmp(str, "flags")) + return; + reg = tok_alloc_const(str); + if (reg >= TOK_ASM_eax && reg <= TOK_ASM_edi) { + reg -= TOK_ASM_eax; + } else if (reg >= TOK_ASM_ax && reg <= TOK_ASM_di) { + reg -= TOK_ASM_ax; +#ifdef TCC_TARGET_X86_64 + } else if (reg >= TOK_ASM_rax && reg <= TOK_ASM_rdi) { + reg -= TOK_ASM_rax; + } else if ((reg = asm_parse_numeric_reg(reg, &type)) >= 0) { + ; +#endif + } else { + tcc_error("invalid clobber register '%s'", str); + } + clobber_regs[reg] = 1; +} diff --git a/05/tcc-final/i386-asm.h b/05/tcc-final/i386-asm.h new file mode 100644 index 0000000..dfc5183 --- /dev/null +++ b/05/tcc-final/i386-asm.h @@ -0,0 +1,482 @@ + DEF_ASM_OP0(clc, 0xf8) /* must be first OP0 */ + DEF_ASM_OP0(cld, 0xfc) + DEF_ASM_OP0(cli, 0xfa) + DEF_ASM_OP0(clts, 0x0f06) + DEF_ASM_OP0(cmc, 0xf5) + DEF_ASM_OP0(lahf, 0x9f) + DEF_ASM_OP0(sahf, 0x9e) + DEF_ASM_OP0(pusha, 0x60) + DEF_ASM_OP0(popa, 0x61) + DEF_ASM_OP0(pushfl, 0x9c) + DEF_ASM_OP0(popfl, 0x9d) + DEF_ASM_OP0(pushf, 0x9c) + DEF_ASM_OP0(popf, 0x9d) + DEF_ASM_OP0(stc, 0xf9) + DEF_ASM_OP0(std, 0xfd) + DEF_ASM_OP0(sti, 0xfb) + DEF_ASM_OP0(aaa, 0x37) + DEF_ASM_OP0(aas, 0x3f) + DEF_ASM_OP0(daa, 0x27) + DEF_ASM_OP0(das, 0x2f) + DEF_ASM_OP0(aad, 0xd50a) + DEF_ASM_OP0(aam, 0xd40a) + DEF_ASM_OP0(cbw, 0x6698) + DEF_ASM_OP0(cwd, 0x6699) + DEF_ASM_OP0(cwde, 0x98) + DEF_ASM_OP0(cdq, 0x99) + DEF_ASM_OP0(cbtw, 0x6698) + DEF_ASM_OP0(cwtl, 0x98) + DEF_ASM_OP0(cwtd, 0x6699) + DEF_ASM_OP0(cltd, 0x99) + DEF_ASM_OP0(int3, 0xcc) + DEF_ASM_OP0(into, 0xce) + DEF_ASM_OP0(iret, 0xcf) + DEF_ASM_OP0(rsm, 0x0faa) + DEF_ASM_OP0(hlt, 0xf4) + DEF_ASM_OP0(nop, 0x90) + DEF_ASM_OP0(pause, 0xf390) + DEF_ASM_OP0(xlat, 0xd7) + + /* strings */ +ALT(DEF_ASM_OP0L(cmpsb, 0xa6, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(scmpb, 0xa6, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(insb, 0x6c, 0, OPC_BWL)) +ALT(DEF_ASM_OP0L(outsb, 0x6e, 0, OPC_BWL)) + +ALT(DEF_ASM_OP0L(lodsb, 0xac, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(slodb, 0xac, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(movsb, 0xa4, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(smovb, 0xa4, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(scasb, 0xae, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(sscab, 0xae, 0, OPC_BWLX)) + +ALT(DEF_ASM_OP0L(stosb, 0xaa, 0, OPC_BWLX)) +ALT(DEF_ASM_OP0L(sstob, 0xaa, 0, OPC_BWLX)) + + /* bits */ + +ALT(DEF_ASM_OP2(bsfw, 0x0fbc, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(bsrw, 0x0fbd, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) + +ALT(DEF_ASM_OP2(btw, 0x0fa3, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btw, 0x0fba, 4, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btsw, 0x0fab, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btsw, 0x0fba, 5, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btrw, 0x0fb3, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btrw, 0x0fba, 6, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + +ALT(DEF_ASM_OP2(btcw, 0x0fbb, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP2(btcw, 0x0fba, 7, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW | OPT_EA)) + + /* prefixes */ + DEF_ASM_OP0(wait, 0x9b) + DEF_ASM_OP0(fwait, 0x9b) + DEF_ASM_OP0(aword, 0x67) + DEF_ASM_OP0(addr16, 0x67) + ALT(DEF_ASM_OP0(word, 0x66)) + DEF_ASM_OP0(data16, 0x66) + DEF_ASM_OP0(lock, 0xf0) + DEF_ASM_OP0(rep, 0xf3) + DEF_ASM_OP0(repe, 0xf3) + DEF_ASM_OP0(repz, 0xf3) + DEF_ASM_OP0(repne, 0xf2) + DEF_ASM_OP0(repnz, 0xf2) + + DEF_ASM_OP0(invd, 0x0f08) + DEF_ASM_OP0(wbinvd, 0x0f09) + DEF_ASM_OP0(cpuid, 0x0fa2) + DEF_ASM_OP0(wrmsr, 0x0f30) + DEF_ASM_OP0(rdtsc, 0x0f31) + DEF_ASM_OP0(rdmsr, 0x0f32) + DEF_ASM_OP0(rdpmc, 0x0f33) + DEF_ASM_OP0(ud2, 0x0f0b) + + /* NOTE: we took the same order as gas opcode definition order */ +ALT(DEF_ASM_OP2(movb, 0xa0, 0, OPC_BWLX, OPT_ADDR, OPT_EAX)) +ALT(DEF_ASM_OP2(movb, 0xa2, 0, OPC_BWLX, OPT_EAX, OPT_ADDR)) +ALT(DEF_ASM_OP2(movb, 0x88, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0x8a, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0xb0, 0, OPC_REG | OPC_BWLX, OPT_IM, OPT_REG)) +ALT(DEF_ASM_OP2(movb, 0xc6, 0, OPC_MODRM | OPC_BWLX, OPT_IM, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP2(movw, 0x8c, 0, OPC_MODRM | OPC_WLX, OPT_SEG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(movw, 0x8e, 0, OPC_MODRM | OPC_WLX, OPT_EA | OPT_REG, OPT_SEG)) + +ALT(DEF_ASM_OP2(movw, 0x0f20, 0, OPC_MODRM | OPC_WLX, OPT_CR, OPT_REG32)) +ALT(DEF_ASM_OP2(movw, 0x0f21, 0, OPC_MODRM | OPC_WLX, OPT_DB, OPT_REG32)) +ALT(DEF_ASM_OP2(movw, 0x0f24, 0, OPC_MODRM | OPC_WLX, OPT_TR, OPT_REG32)) +ALT(DEF_ASM_OP2(movw, 0x0f22, 0, OPC_MODRM | OPC_WLX, OPT_REG32, OPT_CR)) +ALT(DEF_ASM_OP2(movw, 0x0f23, 0, OPC_MODRM | OPC_WLX, OPT_REG32, OPT_DB)) +ALT(DEF_ASM_OP2(movw, 0x0f26, 0, OPC_MODRM | OPC_WLX, OPT_REG32, OPT_TR)) + +ALT(DEF_ASM_OP2(movsbl, 0x0fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movsbw, 0x660fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG16)) +ALT(DEF_ASM_OP2(movswl, 0x0fbf, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(movzbw, 0x0fb6, 0, OPC_MODRM | OPC_WLX, OPT_REG8 | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(movzwl, 0x0fb7, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32)) + +ALT(DEF_ASM_OP1(pushw, 0x50, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(pushw, 0xff, 6, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP1(pushw, 0x6a, 0, OPC_WLX, OPT_IM8S)) +ALT(DEF_ASM_OP1(pushw, 0x68, 0, OPC_WLX, OPT_IM32)) +ALT(DEF_ASM_OP1(pushw, 0x06, 0, OPC_WLX, OPT_SEG)) + +ALT(DEF_ASM_OP1(popw, 0x58, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(popw, 0x8f, 0, OPC_MODRM | OPC_WLX, OPT_REGW | OPT_EA)) +ALT(DEF_ASM_OP1(popw, 0x07, 0, OPC_WLX, OPT_SEG)) + +ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WLX, OPT_REGW, OPT_EAX)) +ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WLX, OPT_EAX, OPT_REGW)) +ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) + +ALT(DEF_ASM_OP2(inb, 0xe4, 0, OPC_BWL, OPT_IM8, OPT_EAX)) +ALT(DEF_ASM_OP1(inb, 0xe4, 0, OPC_BWL, OPT_IM8)) +ALT(DEF_ASM_OP2(inb, 0xec, 0, OPC_BWL, OPT_DX, OPT_EAX)) +ALT(DEF_ASM_OP1(inb, 0xec, 0, OPC_BWL, OPT_DX)) + +ALT(DEF_ASM_OP2(outb, 0xe6, 0, OPC_BWL, OPT_EAX, OPT_IM8)) +ALT(DEF_ASM_OP1(outb, 0xe6, 0, OPC_BWL, OPT_IM8)) +ALT(DEF_ASM_OP2(outb, 0xee, 0, OPC_BWL, OPT_EAX, OPT_DX)) +ALT(DEF_ASM_OP1(outb, 0xee, 0, OPC_BWL, OPT_DX)) + +ALT(DEF_ASM_OP2(leaw, 0x8d, 0, OPC_MODRM | OPC_WLX, OPT_EA, OPT_REG)) + +ALT(DEF_ASM_OP2(les, 0xc4, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lds, 0xc5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lss, 0x0fb2, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lfs, 0x0fb4, 0, OPC_MODRM, OPT_EA, OPT_REG32)) +ALT(DEF_ASM_OP2(lgs, 0x0fb5, 0, OPC_MODRM, OPT_EA, OPT_REG32)) + + /* arith */ +ALT(DEF_ASM_OP2(addb, 0x00, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) /* XXX: use D bit ? */ +ALT(DEF_ASM_OP2(addb, 0x02, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(addb, 0x04, 0, OPC_ARITH | OPC_BWLX, OPT_IM, OPT_EAX)) +ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(addb, 0x80, 0, OPC_ARITH | OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWLX, OPT_EA | OPT_REG, OPT_REG)) +ALT(DEF_ASM_OP2(testb, 0xa8, 0, OPC_BWLX, OPT_IM, OPT_EAX)) +ALT(DEF_ASM_OP2(testb, 0xf6, 0, OPC_MODRM | OPC_BWLX, OPT_IM, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP1(incw, 0x40, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(incb, 0xfe, 0, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(decw, 0x48, 0, OPC_REG | OPC_WLX, OPT_REGW)) +ALT(DEF_ASM_OP1(decb, 0xfe, 1, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP1(notb, 0xf6, 2, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(negb, 0xf6, 3, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP1(mulb, 0xf6, 4, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP1(imulb, 0xf6, 5, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) + +ALT(DEF_ASM_OP2(imulw, 0x0faf, 0, OPC_MODRM | OPC_WLX, OPT_REG | OPT_EA, OPT_REG)) +ALT(DEF_ASM_OP3(imulw, 0x6b, 0, OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(imulw, 0x6b, 0, OPC_MODRM | OPC_WLX, OPT_IM8S, OPT_REGW)) +ALT(DEF_ASM_OP3(imulw, 0x69, 0, OPC_MODRM | OPC_WLX, OPT_IMW, OPT_REGW | OPT_EA, OPT_REGW)) +ALT(DEF_ASM_OP2(imulw, 0x69, 0, OPC_MODRM | OPC_WLX, OPT_IMW, OPT_REGW)) + +ALT(DEF_ASM_OP1(divb, 0xf6, 6, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP2(divb, 0xf6, 6, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA, OPT_EAX)) +ALT(DEF_ASM_OP1(idivb, 0xf6, 7, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA)) +ALT(DEF_ASM_OP2(idivb, 0xf6, 7, OPC_MODRM | OPC_BWLX, OPT_REG | OPT_EA, OPT_EAX)) + + /* shifts */ +ALT(DEF_ASM_OP2(rolb, 0xc0, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_IM8, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP2(rolb, 0xd2, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_CL, OPT_EA | OPT_REG)) +ALT(DEF_ASM_OP1(rolb, 0xd0, 0, OPC_MODRM | OPC_BWLX | OPC_SHIFT, OPT_EA | OPT_REG)) + +ALT(DEF_ASM_OP3(shldw, 0x0fa4, 0, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WLX, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shrdw, 0x0fac, 0, OPC_MODRM | OPC_WLX, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP3(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WLX, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW)) +ALT(DEF_ASM_OP2(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WLX, OPT_REGW, OPT_EA | OPT_REGW)) + +ALT(DEF_ASM_OP1(call, 0xff, 2, OPC_MODRM, OPT_INDIR)) +ALT(DEF_ASM_OP1(call, 0xe8, 0, 0, OPT_DISP)) +ALT(DEF_ASM_OP1(jmp, 0xff, 4, OPC_MODRM, OPT_INDIR)) +ALT(DEF_ASM_OP1(jmp, 0xeb, 0, 0, OPT_DISP8)) + +ALT(DEF_ASM_OP2(lcall, 0x9a, 0, 0, OPT_IM16, OPT_IM32)) +ALT(DEF_ASM_OP1(lcall, 0xff, 3, OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP2(ljmp, 0xea, 0, 0, OPT_IM16, OPT_IM32)) +ALT(DEF_ASM_OP1(ljmp, 0xff, 5, OPC_MODRM, OPT_EA)) + +ALT(DEF_ASM_OP1(int, 0xcd, 0, 0, OPT_IM8)) +ALT(DEF_ASM_OP1(seto, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA)) +ALT(DEF_ASM_OP1(setob, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA)) + DEF_ASM_OP2(enter, 0xc8, 0, 0, OPT_IM16, OPT_IM8) + DEF_ASM_OP0(leave, 0xc9) + DEF_ASM_OP0(ret, 0xc3) + DEF_ASM_OP0(retl,0xc3) +ALT(DEF_ASM_OP1(retl,0xc2, 0, 0, OPT_IM16)) +ALT(DEF_ASM_OP1(ret, 0xc2, 0, 0, OPT_IM16)) + DEF_ASM_OP0(lret, 0xcb) +ALT(DEF_ASM_OP1(lret, 0xca, 0, 0, OPT_IM16)) + +ALT(DEF_ASM_OP1(jo, 0x70, 0, OPC_TEST, OPT_DISP8)) + DEF_ASM_OP1(loopne, 0xe0, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loopnz, 0xe0, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loope, 0xe1, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loopz, 0xe1, 0, 0, OPT_DISP8) + DEF_ASM_OP1(loop, 0xe2, 0, 0, OPT_DISP8) + DEF_ASM_OP1(jecxz, 0xe3, 0, 0, OPT_DISP8) + + /* float */ + /* specific fcomp handling */ +ALT(DEF_ASM_OP0L(fcomp, 0xd8d9, 0, 0)) + +ALT(DEF_ASM_OP1(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST)) +ALT(DEF_ASM_OP2(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0)) +ALT(DEF_ASM_OP2(fadd, 0xdcc0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP2(fmul, 0xdcc8, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP0L(fadd, 0xdec1, 0, OPC_FARITH)) +ALT(DEF_ASM_OP1(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST)) +ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0)) +ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST)) +ALT(DEF_ASM_OP0L(faddp, 0xdec1, 0, OPC_FARITH)) +ALT(DEF_ASM_OP1(fadds, 0xd8, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(fiaddl, 0xda, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(faddl, 0xdc, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) +ALT(DEF_ASM_OP1(fiadds, 0xde, 0, OPC_FARITH | OPC_MODRM, OPT_EA)) + + DEF_ASM_OP0(fucompp, 0xdae9) + DEF_ASM_OP0(ftst, 0xd9e4) + DEF_ASM_OP0(fxam, 0xd9e5) + DEF_ASM_OP0(fld1, 0xd9e8) + DEF_ASM_OP0(fldl2t, 0xd9e9) + DEF_ASM_OP0(fldl2e, 0xd9ea) + DEF_ASM_OP0(fldpi, 0xd9eb) + DEF_ASM_OP0(fldlg2, 0xd9ec) + DEF_ASM_OP0(fldln2, 0xd9ed) + DEF_ASM_OP0(fldz, 0xd9ee) + + DEF_ASM_OP0(f2xm1, 0xd9f0) + DEF_ASM_OP0(fyl2x, 0xd9f1) + DEF_ASM_OP0(fptan, 0xd9f2) + DEF_ASM_OP0(fpatan, 0xd9f3) + DEF_ASM_OP0(fxtract, 0xd9f4) + DEF_ASM_OP0(fprem1, 0xd9f5) + DEF_ASM_OP0(fdecstp, 0xd9f6) + DEF_ASM_OP0(fincstp, 0xd9f7) + DEF_ASM_OP0(fprem, 0xd9f8) + DEF_ASM_OP0(fyl2xp1, 0xd9f9) + DEF_ASM_OP0(fsqrt, 0xd9fa) + DEF_ASM_OP0(fsincos, 0xd9fb) + DEF_ASM_OP0(frndint, 0xd9fc) + DEF_ASM_OP0(fscale, 0xd9fd) + DEF_ASM_OP0(fsin, 0xd9fe) + DEF_ASM_OP0(fcos, 0xd9ff) + DEF_ASM_OP0(fchs, 0xd9e0) + DEF_ASM_OP0(fabs, 0xd9e1) + DEF_ASM_OP0(fninit, 0xdbe3) + DEF_ASM_OP0(fnclex, 0xdbe2) + DEF_ASM_OP0(fnop, 0xd9d0) + + /* fp load */ + DEF_ASM_OP1(fld, 0xd9c0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fldl, 0xd9c0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(flds, 0xd9, 0, OPC_MODRM, OPT_EA) +ALT(DEF_ASM_OP1(fldl, 0xdd, 0, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(fildl, 0xdb, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fildq, 0xdf, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fildll, 0xdf, 5, OPC_MODRM,OPT_EA) + DEF_ASM_OP1(fldt, 0xdb, 5, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fbld, 0xdf, 4, OPC_MODRM, OPT_EA) + + /* fp store */ + DEF_ASM_OP1(fst, 0xddd0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fstl, 0xddd0, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fsts, 0xd9, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fstps, 0xd9, 3, OPC_MODRM, OPT_EA) +ALT(DEF_ASM_OP1(fstl, 0xdd, 2, OPC_MODRM, OPT_EA)) + DEF_ASM_OP1(fstpl, 0xdd, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fist, 0xdf, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistp, 0xdf, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistl, 0xdb, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistpl, 0xdb, 3, OPC_MODRM, OPT_EA) + + DEF_ASM_OP1(fstp, 0xddd8, 0, OPC_REG, OPT_ST) + DEF_ASM_OP1(fistpq, 0xdf, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fistpll, 0xdf, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fstpt, 0xdb, 7, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(fbstp, 0xdf, 6, OPC_MODRM, OPT_EA) + + /* exchange */ + DEF_ASM_OP0(fxch, 0xd9c9) +ALT(DEF_ASM_OP1(fxch, 0xd9c8, 0, OPC_REG, OPT_ST)) + + /* misc FPU */ + DEF_ASM_OP1(fucom, 0xdde0, 0, OPC_REG, OPT_ST ) + DEF_ASM_OP1(fucomp, 0xdde8, 0, OPC_REG, OPT_ST ) + + DEF_ASM_OP0L(finit, 0xdbe3, 0, OPC_FWAIT) + DEF_ASM_OP1(fldcw, 0xd9, 5, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fnstcw, 0xd9, 7, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fstcw, 0xd9, 7, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP0(fnstsw, 0xdfe0) +ALT(DEF_ASM_OP1(fnstsw, 0xdfe0, 0, 0, OPT_EAX )) +ALT(DEF_ASM_OP1(fnstsw, 0xdd, 7, OPC_MODRM, OPT_EA )) + DEF_ASM_OP1(fstsw, 0xdfe0, 0, OPC_FWAIT, OPT_EAX ) +ALT(DEF_ASM_OP0L(fstsw, 0xdfe0, 0, OPC_FWAIT)) +ALT(DEF_ASM_OP1(fstsw, 0xdd, 7, OPC_MODRM | OPC_FWAIT, OPT_EA )) + DEF_ASM_OP0L(fclex, 0xdbe2, 0, OPC_FWAIT) + DEF_ASM_OP1(fnstenv, 0xd9, 6, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fstenv, 0xd9, 6, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP1(fldenv, 0xd9, 4, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fnsave, 0xdd, 6, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fsave, 0xdd, 6, OPC_MODRM | OPC_FWAIT, OPT_EA ) + DEF_ASM_OP1(frstor, 0xdd, 4, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(ffree, 0xddc0, 4, OPC_REG, OPT_ST ) + DEF_ASM_OP1(ffreep, 0xdfc0, 4, OPC_REG, OPT_ST ) + DEF_ASM_OP1(fxsave, 0x0fae, 0, OPC_MODRM, OPT_EA ) + DEF_ASM_OP1(fxrstor, 0x0fae, 1, OPC_MODRM, OPT_EA ) + + /* segments */ + DEF_ASM_OP2(arpl, 0x63, 0, OPC_MODRM, OPT_REG16, OPT_REG16 | OPT_EA) +ALT(DEF_ASM_OP2(larw, 0x0f02, 0, OPC_MODRM | OPC_WLX, OPT_REG | OPT_EA, OPT_REG)) + DEF_ASM_OP1(lgdt, 0x0f01, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lidt, 0x0f01, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(lldt, 0x0f00, 2, OPC_MODRM, OPT_EA | OPT_REG) + DEF_ASM_OP1(lmsw, 0x0f01, 6, OPC_MODRM, OPT_EA | OPT_REG) +ALT(DEF_ASM_OP2(lslw, 0x0f03, 0, OPC_MODRM | OPC_WLX, OPT_EA | OPT_REG, OPT_REG)) + DEF_ASM_OP1(ltr, 0x0f00, 3, OPC_MODRM, OPT_EA | OPT_REG) + DEF_ASM_OP1(sgdt, 0x0f01, 0, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sidt, 0x0f01, 1, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(sldt, 0x0f00, 0, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(smsw, 0x0f01, 4, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(str, 0x0f00, 1, OPC_MODRM, OPT_REG16| OPT_EA) + DEF_ASM_OP1(verr, 0x0f00, 4, OPC_MODRM, OPT_REG | OPT_EA) + DEF_ASM_OP1(verw, 0x0f00, 5, OPC_MODRM, OPT_REG | OPT_EA) + + /* 486 */ + DEF_ASM_OP1(bswap, 0x0fc8, 0, OPC_REG, OPT_REG32 ) +ALT(DEF_ASM_OP2(xaddb, 0x0fc0, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_REG | OPT_EA )) +ALT(DEF_ASM_OP2(cmpxchgb, 0x0fb0, 0, OPC_MODRM | OPC_BWLX, OPT_REG, OPT_REG | OPT_EA )) + DEF_ASM_OP1(invlpg, 0x0f01, 7, OPC_MODRM, OPT_EA ) + + DEF_ASM_OP2(boundl, 0x62, 0, OPC_MODRM, OPT_REG32, OPT_EA) + DEF_ASM_OP2(boundw, 0x6662, 0, OPC_MODRM, OPT_REG16, OPT_EA) + + /* pentium */ + DEF_ASM_OP1(cmpxchg8b, 0x0fc7, 1, OPC_MODRM, OPT_EA ) + + /* pentium pro */ +ALT(DEF_ASM_OP2(cmovo, 0x0f40, 0, OPC_MODRM | OPC_TEST | OPC_WLX, OPT_REGW | OPT_EA, OPT_REGW)) + DEF_ASM_OP2(fcmovb, 0xdac0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmove, 0xdac8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovbe, 0xdad0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovu, 0xdad8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnb, 0xdbc0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovne, 0xdbc8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnbe, 0xdbd0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcmovnu, 0xdbd8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + + DEF_ASM_OP2(fucomi, 0xdbe8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcomi, 0xdbf0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fucomip, 0xdfe8, 0, OPC_REG, OPT_ST, OPT_ST0 ) + DEF_ASM_OP2(fcomip, 0xdff0, 0, OPC_REG, OPT_ST, OPT_ST0 ) + + /* mmx */ + DEF_ASM_OP0(emms, 0x0f77) /* must be last OP0 */ + DEF_ASM_OP2(movd, 0x0f6e, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_MMXSSE ) + DEF_ASM_OP2(movq, 0x0f6f, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) +ALT(DEF_ASM_OP2(movd, 0x0f7e, 0, OPC_MODRM, OPT_MMXSSE, OPT_EA | OPT_REG32 )) +ALT(DEF_ASM_OP2(movq, 0x0f7f, 0, OPC_MODRM, OPT_MMX, OPT_EA | OPT_MMX )) +ALT(DEF_ASM_OP2(movq, 0x660fd6, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_SSE )) +ALT(DEF_ASM_OP2(movq, 0xf30f7e, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE )) + + DEF_ASM_OP2(packssdw, 0x0f6b, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(packsswb, 0x0f63, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(packuswb, 0x0f67, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddb, 0x0ffc, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddw, 0x0ffd, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddd, 0x0ffe, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddsb, 0x0fec, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddsw, 0x0fed, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddusb, 0x0fdc, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(paddusw, 0x0fdd, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pand, 0x0fdb, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pandn, 0x0fdf, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqb, 0x0f74, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqw, 0x0f75, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpeqd, 0x0f76, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtb, 0x0f64, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtw, 0x0f65, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pcmpgtd, 0x0f66, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmaddwd, 0x0ff5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmulhw, 0x0fe5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmullw, 0x0fd5, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(por, 0x0feb, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psllw, 0x0ff1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psllw, 0x0f71, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(pslld, 0x0ff2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(pslld, 0x0f72, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psllq, 0x0ff3, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psllq, 0x0f73, 6, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psraw, 0x0fe1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psraw, 0x0f71, 4, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrad, 0x0fe2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrad, 0x0f72, 4, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrlw, 0x0fd1, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrlw, 0x0f71, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrld, 0x0fd2, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrld, 0x0f72, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psrlq, 0x0fd3, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) +ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMXSSE )) + DEF_ASM_OP2(psubb, 0x0ff8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubw, 0x0ff9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubd, 0x0ffa, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubsb, 0x0fe8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubsw, 0x0fe9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubusb, 0x0fd8, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(psubusw, 0x0fd9, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhbw, 0x0f68, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhwd, 0x0f69, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckhdq, 0x0f6a, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpcklbw, 0x0f60, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpcklwd, 0x0f61, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(punpckldq, 0x0f62, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + + /* sse */ + DEF_ASM_OP1(ldmxcsr, 0x0fae, 2, OPC_MODRM, OPT_EA) + DEF_ASM_OP1(stmxcsr, 0x0fae, 3, OPC_MODRM, OPT_EA) + DEF_ASM_OP2(movups, 0x0f10, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movups, 0x0f11, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(movaps, 0x0f28, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movaps, 0x0f29, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(movhps, 0x0f16, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_SSE ) +ALT(DEF_ASM_OP2(movhps, 0x0f17, 0, OPC_MODRM, OPT_SSE, OPT_EA | OPT_REG32 )) + DEF_ASM_OP2(addps, 0x0f58, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(cvtpi2ps, 0x0f2a, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_SSE ) + DEF_ASM_OP2(cvtps2pi, 0x0f2d, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_MMX ) + DEF_ASM_OP2(cvttps2pi, 0x0f2c, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_MMX ) + DEF_ASM_OP2(divps, 0x0f5e, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(maxps, 0x0f5f, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(minps, 0x0f5d, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(mulps, 0x0f59, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pavgb, 0x0fe0, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pavgw, 0x0fe3, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(pmaxsw, 0x0fee, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pmaxub, 0x0fde, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pminsw, 0x0fea, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(pminub, 0x0fda, 0, OPC_MODRM, OPT_EA | OPT_MMXSSE, OPT_MMXSSE ) + DEF_ASM_OP2(rcpss, 0x0f53, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(rsqrtps, 0x0f52, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(sqrtps, 0x0f51, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + DEF_ASM_OP2(subps, 0x0f5c, 0, OPC_MODRM, OPT_EA | OPT_SSE, OPT_SSE ) + +#undef ALT +#undef DEF_ASM_OP0 +#undef DEF_ASM_OP0L +#undef DEF_ASM_OP1 +#undef DEF_ASM_OP2 +#undef DEF_ASM_OP3 diff --git a/05/tcc-final/i386-gen.c b/05/tcc-final/i386-gen.c new file mode 100644 index 0000000..62bc2ad --- /dev/null +++ b/05/tcc-final/i386-gen.c @@ -0,0 +1,1140 @@ +/* + * X86 code generator for TCC + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef TARGET_DEFS_ONLY + +/* number of available registers */ +#define NB_REGS 5 +#define NB_ASM_REGS 8 +#define CONFIG_TCC_ASM + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_INT 0x0001 /* generic integer register */ +#define RC_FLOAT 0x0002 /* generic float register */ +#define RC_EAX 0x0004 +#define RC_ST0 0x0008 +#define RC_ECX 0x0010 +#define RC_EDX 0x0020 +#define RC_EBX 0x0040 + +#define RC_IRET RC_EAX /* function return: integer register */ +#define RC_IRE2 RC_EDX /* function return: second integer register */ +#define RC_FRET RC_ST0 /* function return: float register */ + +/* pretty names for the registers */ +enum { + TREG_EAX = 0, + TREG_ECX, + TREG_EDX, + TREG_EBX, + TREG_ST0, + TREG_ESP = 4 +}; + +/* return registers for function */ +#define REG_IRET TREG_EAX /* single word int return register */ +#define REG_IRE2 TREG_EDX /* second word return register (for long long) */ +#define REG_FRET TREG_ST0 /* float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +#define INVERT_FUNC_PARAMS + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 12 +#define LDOUBLE_ALIGN 4 +/* maximum alignment (for aligned attribute support) */ +#define MAX_ALIGN 8 + +/* define if return values need to be extended explicitely + at caller side (for interfacing with non-TCC compilers) */ +#define PROMOTE_RET + +/******************************************************/ +#else /* ! TARGET_DEFS_ONLY */ +/******************************************************/ +#define USING_GLOBALS +#include "tcc.h" + +ST_DATA const char * const target_machine_defs = + "__i386__\0" + "__i386\0" + ; + +/* define to 1/0 to [not] have EBX as 4th register */ +#define USE_EBX 0 + +ST_DATA const int reg_classes[NB_REGS] = { + /* eax */ RC_INT | RC_EAX, + /* ecx */ RC_INT | RC_ECX, + /* edx */ RC_INT | RC_EDX, + /* ebx */ (RC_INT | RC_EBX) * USE_EBX, + /* st0 */ RC_FLOAT | RC_ST0, +}; + +static unsigned long func_sub_sp_offset; +static int func_ret_sub; +#ifdef CONFIG_TCC_BCHECK +static addr_t func_bound_offset; +static unsigned long func_bound_ind; +ST_DATA int func_bound_add_epilog; +static void gen_bounds_prolog(void); +static void gen_bounds_epilog(void); +#endif + +/* XXX: make it faster ? */ +ST_FUNC void g(int c) +{ + int ind1; + if (nocode_wanted) + return; + ind1 = ind + 1; + if (ind1 > cur_text_section->data_allocated) + section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; + ind = ind1; +} + +ST_FUNC void o(unsigned int c) +{ + while (c) { + g(c); + c = c >> 8; + } +} + +ST_FUNC void gen_le16(int v) +{ + g(v); + g(v >> 8); +} + +ST_FUNC void gen_le32(int c) +{ + g(c); + g(c >> 8); + g(c >> 16); + g(c >> 24); +} + +/* output a symbol and patch all calls to it */ +ST_FUNC void gsym_addr(int t, int a) +{ + while (t) { + unsigned char *ptr = cur_text_section->data + t; + uint32_t n = read32le(ptr); /* next value */ + write32le(ptr, a - t - 4); + t = n; + } +} + +/* instruction + 4 bytes data. Return the address of the data */ +static int oad(int c, int s) +{ + int t; + if (nocode_wanted) + return s; + o(c); + t = ind; + gen_le32(s); + return t; +} + +ST_FUNC void gen_fill_nops(int bytes) +{ + while (bytes--) + g(0x90); +} + +/* generate jmp to a label */ +#define gjmp2(instr,lbl) oad(instr,lbl) + +/* output constant with relocation if 'r & VT_SYM' is true */ +ST_FUNC void gen_addr32(int r, Sym *sym, int c) +{ + if (r & VT_SYM) + greloc(cur_text_section, sym, ind, R_386_32); + gen_le32(c); +} + +ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) +{ + if (r & VT_SYM) + greloc(cur_text_section, sym, ind, R_386_PC32); + gen_le32(c - 4); +} + +/* generate a modrm reference. 'op_reg' contains the additional 3 + opcode bits */ +static void gen_modrm(int op_reg, int r, Sym *sym, int c) +{ + op_reg = op_reg << 3; + if ((r & VT_VALMASK) == VT_CONST) { + /* constant memory reference */ + o(0x05 | op_reg); + gen_addr32(r, sym, c); + } else if ((r & VT_VALMASK) == VT_LOCAL) { + /* currently, we use only ebp as base */ + if (c == (char)c) { + /* short reference */ + o(0x45 | op_reg); + g(c); + } else { + oad(0x85 | op_reg, c); + } + } else { + g(0x00 | op_reg | (r & VT_VALMASK)); + } +} + +/* load 'r' from value 'sv' */ +ST_FUNC void load(int r, SValue *sv) +{ + int v, t, ft, fc, fr; + SValue v1; + +#ifdef TCC_TARGET_PE + SValue v2; + sv = pe_getimport(sv, &v2); +#endif + + fr = sv->r; + ft = sv->type.t & ~VT_DEFSIGN; + fc = sv->c.i; + + ft &= ~(VT_VOLATILE | VT_CONSTANT); + + v = fr & VT_VALMASK; + if (fr & VT_LVAL) { + if (v == VT_LLOCAL) { + v1.type.t = VT_INT; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + v1.sym = NULL; + fr = r; + if (!(reg_classes[fr] & RC_INT)) + fr = get_reg(RC_INT); + load(fr, &v1); + } + if ((ft & VT_BTYPE) == VT_FLOAT) { + o(0xd9); /* flds */ + r = 0; + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + o(0xdd); /* fldl */ + r = 0; + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + o(0xdb); /* fldt */ + r = 5; + } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) { + o(0xbe0f); /* movsbl */ + } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) { + o(0xb60f); /* movzbl */ + } else if ((ft & VT_TYPE) == VT_SHORT) { + o(0xbf0f); /* movswl */ + } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) { + o(0xb70f); /* movzwl */ + } else { + o(0x8b); /* movl */ + } + gen_modrm(r, fr, sv->sym, fc); + } else { + if (v == VT_CONST) { + o(0xb8 + r); /* mov $xx, r */ + gen_addr32(fr, sv->sym, fc); + } else if (v == VT_LOCAL) { + if (fc) { + o(0x8d); /* lea xxx(%ebp), r */ + gen_modrm(r, VT_LOCAL, sv->sym, fc); + } else { + o(0x89); + o(0xe8 + r); /* mov %ebp, r */ + } + } else if (v == VT_CMP) { + o(0x0f); /* setxx %br */ + o(fc); + o(0xc0 + r); + o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */ + } else if (v == VT_JMP || v == VT_JMPI) { + t = v & 1; + oad(0xb8 + r, t); /* mov $1, r */ + o(0x05eb); /* jmp after */ + gsym(fc); + oad(0xb8 + r, t ^ 1); /* mov $0, r */ + } else if (v != r) { + o(0x89); + o(0xc0 + r + v * 8); /* mov v, r */ + } + } +} + +/* store register 'r' in lvalue 'v' */ +ST_FUNC void store(int r, SValue *v) +{ + int fr, bt, ft, fc; + +#ifdef TCC_TARGET_PE + SValue v2; + v = pe_getimport(v, &v2); +#endif + + ft = v->type.t; + fc = v->c.i; + fr = v->r & VT_VALMASK; + ft &= ~(VT_VOLATILE | VT_CONSTANT); + bt = ft & VT_BTYPE; + /* XXX: incorrect if float reg to reg */ + if (bt == VT_FLOAT) { + o(0xd9); /* fsts */ + r = 2; + } else if (bt == VT_DOUBLE) { + o(0xdd); /* fstpl */ + r = 2; + } else if (bt == VT_LDOUBLE) { + o(0xc0d9); /* fld %st(0) */ + o(0xdb); /* fstpt */ + r = 7; + } else { + if (bt == VT_SHORT) + o(0x66); + if (bt == VT_BYTE || bt == VT_BOOL) + o(0x88); + else + o(0x89); + } + if (fr == VT_CONST || + fr == VT_LOCAL || + (v->r & VT_LVAL)) { + gen_modrm(r, v->r, v->sym, fc); + } else if (fr != r) { + o(0xc0 + fr + r * 8); /* mov r, fr */ + } +} + +static void gadd_sp(int val) +{ + if (val == (char)val) { + o(0xc483); + g(val); + } else { + oad(0xc481, val); /* add $xxx, %esp */ + } +} + +#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE +static void gen_static_call(int v) +{ + Sym *sym; + + sym = external_helper_sym(v); + oad(0xe8, -4); + greloc(cur_text_section, sym, ind-4, R_386_PC32); +} +#endif + +/* 'is_jmp' is '1' if it is a jump */ +static void gcall_or_jmp(int is_jmp) +{ + int r; + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) { + /* constant and relocation case */ + greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32); + oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */ + } else { + /* otherwise, indirect call */ + r = gv(RC_INT); + o(0xff); /* call/jmp *r */ + o(0xd0 + r + (is_jmp << 4)); + } +} + +static const uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX }; +static const uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize) +{ +#if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD + int size, align; + *ret_align = 1; // Never have to re-align return values for x86 + *regsize = 4; + size = type_size(vt, &align); + if (size > 8 || (size & (size - 1))) + return 0; + if (size == 8) + ret->t = VT_LLONG; + else if (size == 4) + ret->t = VT_INT; + else if (size == 2) + ret->t = VT_SHORT; + else + ret->t = VT_BYTE; + ret->ref = NULL; + return 1; +#else + *ret_align = 1; // Never have to re-align return values for x86 + return 0; +#endif +} + +/* Generate function call. The function address is pushed first, then + all the parameters in call order. This functions pops all the + parameters and the function address. */ +ST_FUNC void gfunc_call(int nb_args) +{ + int size, align, r, args_size, i, func_call; + Sym *func_sym; + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gbound_args(nb_args); +#endif + + args_size = 0; + for(i = 0;i < nb_args; i++) { + if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { + size = type_size(&vtop->type, &align); + /* align to stack align size */ + size = (size + 3) & ~3; + /* allocate the necessary size on stack */ +#ifdef TCC_TARGET_PE + if (size >= 4096) { + r = get_reg(RC_EAX); + oad(0x68, size); // push size + /* cannot call normal 'alloca' with bound checking */ + gen_static_call(tok_alloc_const("__alloca")); + gadd_sp(4); + } else +#endif + { + oad(0xec81, size); /* sub $xxx, %esp */ + /* generate structure store */ + r = get_reg(RC_INT); + o(0xe089 + (r << 8)); /* mov %esp, r */ + } + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); + args_size += size; + } else if (is_float(vtop->type.t)) { + gv(RC_FLOAT); /* only one float register */ + if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) + size = 8; + else + size = 12; + oad(0xec81, size); /* sub $xxx, %esp */ + if (size == 12) + o(0x7cdb); + else + o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */ + g(0x24); + g(0x00); + args_size += size; + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + r = gv(RC_INT); + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + size = 8; + o(0x50 + vtop->r2); /* push r */ + } else { + size = 4; + } + o(0x50 + r); /* push r */ + args_size += size; + } + vtop--; + } + save_regs(0); /* save used temporary registers */ + func_sym = vtop->type.ref; + func_call = func_sym->f.func_call; + /* fast call case */ + if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) || + func_call == FUNC_FASTCALLW) { + int fastcall_nb_regs; + const uint8_t *fastcall_regs_ptr; + if (func_call == FUNC_FASTCALLW) { + fastcall_regs_ptr = fastcallw_regs; + fastcall_nb_regs = 2; + } else { + fastcall_regs_ptr = fastcall_regs; + fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1; + } + for(i = 0;i < fastcall_nb_regs; i++) { + if (args_size <= 0) + break; + o(0x58 + fastcall_regs_ptr[i]); /* pop r */ + /* XXX: incorrect for struct/floats */ + args_size -= 4; + } + } +#if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD + else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT) + args_size -= 4; +#endif + + gcall_or_jmp(0); + + if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW) + gadd_sp(args_size); + vtop--; +} + +#ifdef TCC_TARGET_PE +#define FUNC_PROLOG_SIZE (10 + USE_EBX) +#else +#define FUNC_PROLOG_SIZE (9 + USE_EBX) +#endif + +/* generate function prolog of type 't' */ +ST_FUNC void gfunc_prolog(Sym *func_sym) +{ + CType *func_type = &func_sym->type; + int addr, align, size, func_call, fastcall_nb_regs; + int param_index, param_addr; + const uint8_t *fastcall_regs_ptr; + Sym *sym; + CType *type; + + sym = func_type->ref; + func_call = sym->f.func_call; + addr = 8; + loc = 0; + func_vc = 0; + + if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) { + fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1; + fastcall_regs_ptr = fastcall_regs; + } else if (func_call == FUNC_FASTCALLW) { + fastcall_nb_regs = 2; + fastcall_regs_ptr = fastcallw_regs; + } else { + fastcall_nb_regs = 0; + fastcall_regs_ptr = NULL; + } + param_index = 0; + + ind += FUNC_PROLOG_SIZE; + func_sub_sp_offset = ind; + /* if the function returns a structure, then add an + implicit pointer parameter */ +#if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD + size = type_size(&func_vt,&align); + if (((func_vt.t & VT_BTYPE) == VT_STRUCT) + && (size > 8 || (size & (size - 1)))) { +#else + if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { +#endif + /* XXX: fastcall case ? */ + func_vc = addr; + addr += 4; + param_index++; + } + /* define parameters */ + while ((sym = sym->next) != NULL) { + type = &sym->type; + size = type_size(type, &align); + size = (size + 3) & ~3; +#ifdef FUNC_STRUCT_PARAM_AS_PTR + /* structs are passed as pointer */ + if ((type->t & VT_BTYPE) == VT_STRUCT) { + size = 4; + } +#endif + if (param_index < fastcall_nb_regs) { + /* save FASTCALL register */ + loc -= 4; + o(0x89); /* movl */ + gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc); + param_addr = loc; + } else { + param_addr = addr; + addr += size; + } + sym_push(sym->v & ~SYM_FIELD, type, + VT_LOCAL | VT_LVAL, param_addr); + param_index++; + } + func_ret_sub = 0; + /* pascal type call or fastcall ? */ + if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW) + func_ret_sub = addr - 8; +#if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD + else if (func_vc) + func_ret_sub = 4; +#endif + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gen_bounds_prolog(); +#endif +} + +/* generate function epilog */ +ST_FUNC void gfunc_epilog(void) +{ + addr_t v, saved_ind; + +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check) + gen_bounds_epilog(); +#endif + + /* align local size to word & save local variables */ + v = (-loc + 3) & -4; + +#if USE_EBX + o(0x8b); + gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4)); +#endif + + o(0xc9); /* leave */ + if (func_ret_sub == 0) { + o(0xc3); /* ret */ + } else { + o(0xc2); /* ret n */ + g(func_ret_sub); + g(func_ret_sub >> 8); + } + saved_ind = ind; + ind = func_sub_sp_offset - FUNC_PROLOG_SIZE; +#ifdef TCC_TARGET_PE + if (v >= 4096) { + oad(0xb8, v); /* mov stacksize, %eax */ + gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */ + } else +#endif + { + o(0xe58955); /* push %ebp, mov %esp, %ebp */ + o(0xec81); /* sub esp, stacksize */ + gen_le32(v); +#ifdef TCC_TARGET_PE + o(0x90); /* adjust to FUNC_PROLOG_SIZE */ +#endif + } + o(0x53 * USE_EBX); /* push ebx */ + ind = saved_ind; +} + +/* generate a jump to a label */ +ST_FUNC int gjmp(int t) +{ + return gjmp2(0xe9, t); +} + +/* generate a jump to a fixed address */ +ST_FUNC void gjmp_addr(int a) +{ + int r; + r = a - ind - 2; + if (r == (char)r) { + g(0xeb); + g(r); + } else { + oad(0xe9, a - ind - 5); + } +} + +#if 0 +/* generate a jump to a fixed address */ +ST_FUNC void gjmp_cond_addr(int a, int op) +{ + int r = a - ind - 2; + if (r == (char)r) + g(op - 32), g(r); + else + g(0x0f), gjmp2(op - 16, r - 4); +} +#endif + +ST_FUNC int gjmp_append(int n, int t) +{ + void *p; + /* insert vtop->c jump list in t */ + if (n) { + uint32_t n1 = n, n2; + while ((n2 = read32le(p = cur_text_section->data + n1))) + n1 = n2; + write32le(p, t); + t = n; + } + return t; +} + +ST_FUNC int gjmp_cond(int op, int t) +{ + g(0x0f); + t = gjmp2(op - 16, t); + return t; +} + +ST_FUNC void gen_opi(int op) +{ + int r, fr, opc, c; + + switch(op) { + case '+': + case TOK_ADDC1: /* add with carry generation */ + opc = 0; + gen_op8: + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + /* constant case */ + vswap(); + r = gv(RC_INT); + vswap(); + c = vtop->c.i; + if (c == (char)c) { + /* generate inc and dec for smaller code */ + if ((c == 1 || c == -1) && (op == '+' || op == '-')) { + opc = (c == 1) ^ (op == '+'); + o (0x40 | (opc << 3) | r); // inc,dec + } else { + o(0x83); + o(0xc0 | (opc << 3) | r); + g(c); + } + } else { + o(0x81); + oad(0xc0 | (opc << 3) | r, c); + } + } else { + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + o((opc << 3) | 0x01); + o(0xc0 + r + fr * 8); + } + vtop--; + if (op >= TOK_ULT && op <= TOK_GT) + vset_VT_CMP(op); + break; + case '-': + case TOK_SUBC1: /* sub with carry generation */ + opc = 5; + goto gen_op8; + case TOK_ADDC2: /* add with carry use */ + opc = 2; + goto gen_op8; + case TOK_SUBC2: /* sub with carry use */ + opc = 3; + goto gen_op8; + case '&': + opc = 4; + goto gen_op8; + case '^': + opc = 6; + goto gen_op8; + case '|': + opc = 1; + goto gen_op8; + case '*': + gv2(RC_INT, RC_INT); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + o(0xaf0f); /* imul fr, r */ + o(0xc0 + fr + r * 8); + break; + case TOK_SHL: + opc = 4; + goto gen_shift; + case TOK_SHR: + opc = 5; + goto gen_shift; + case TOK_SAR: + opc = 7; + gen_shift: + opc = 0xc0 | (opc << 3); + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + /* constant case */ + vswap(); + r = gv(RC_INT); + vswap(); + c = vtop->c.i & 0x1f; + o(0xc1); /* shl/shr/sar $xxx, r */ + o(opc | r); + g(c); + } else { + /* we generate the shift in ecx */ + gv2(RC_INT, RC_ECX); + r = vtop[-1].r; + o(0xd3); /* shl/shr/sar %cl, r */ + o(opc | r); + } + vtop--; + break; + case '/': + case TOK_UDIV: + case TOK_PDIV: + case '%': + case TOK_UMOD: + case TOK_UMULL: + /* first operand must be in eax */ + /* XXX: need better constraint for second operand */ + gv2(RC_EAX, RC_ECX); + r = vtop[-1].r; + fr = vtop[0].r; + vtop--; + save_reg(TREG_EDX); + /* save EAX too if used otherwise */ + save_reg_upstack(TREG_EAX, 1); + if (op == TOK_UMULL) { + o(0xf7); /* mul fr */ + o(0xe0 + fr); + vtop->r2 = TREG_EDX; + r = TREG_EAX; + } else { + if (op == TOK_UDIV || op == TOK_UMOD) { + o(0xf7d231); /* xor %edx, %edx, div fr, %eax */ + o(0xf0 + fr); + } else { + o(0xf799); /* cltd, idiv fr, %eax */ + o(0xf8 + fr); + } + if (op == '%' || op == TOK_UMOD) + r = TREG_EDX; + else + r = TREG_EAX; + } + vtop->r = r; + break; + default: + opc = 7; + goto gen_op8; + } +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +/* XXX: need to use ST1 too */ +ST_FUNC void gen_opf(int op) +{ + int a, ft, fc, swapped, r; + + if (op == TOK_NEG) { /* unary minus */ + gv(RC_FLOAT); + o(0xe0d9); /* fchs */ + return; + } + + /* convert constants to memory references */ + if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + vswap(); + gv(RC_FLOAT); + vswap(); + } + if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) + gv(RC_FLOAT); + + /* must put at least one value in the floating point register */ + if ((vtop[-1].r & VT_LVAL) && + (vtop[0].r & VT_LVAL)) { + vswap(); + gv(RC_FLOAT); + vswap(); + } + swapped = 0; + /* swap the stack if needed so that t1 is the register and t2 is + the memory reference */ + if (vtop[-1].r & VT_LVAL) { + vswap(); + swapped = 1; + } + if (op >= TOK_ULT && op <= TOK_GT) { + /* load on stack second operand */ + load(TREG_ST0, vtop); + save_reg(TREG_EAX); /* eax is used by FP comparison code */ + if (op == TOK_GE || op == TOK_GT) + swapped = !swapped; + else if (op == TOK_EQ || op == TOK_NE) + swapped = 0; + if (swapped) + o(0xc9d9); /* fxch %st(1) */ + if (op == TOK_EQ || op == TOK_NE) + o(0xe9da); /* fucompp */ + else + o(0xd9de); /* fcompp */ + o(0xe0df); /* fnstsw %ax */ + if (op == TOK_EQ) { + o(0x45e480); /* and $0x45, %ah */ + o(0x40fC80); /* cmp $0x40, %ah */ + } else if (op == TOK_NE) { + o(0x45e480); /* and $0x45, %ah */ + o(0x40f480); /* xor $0x40, %ah */ + op = TOK_NE; + } else if (op == TOK_GE || op == TOK_LE) { + o(0x05c4f6); /* test $0x05, %ah */ + op = TOK_EQ; + } else { + o(0x45c4f6); /* test $0x45, %ah */ + op = TOK_EQ; + } + vtop--; + vset_VT_CMP(op); + } else { + /* no memory reference possible for long double operations */ + if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) { + load(TREG_ST0, vtop); + swapped = !swapped; + } + + switch(op) { + default: + case '+': + a = 0; + break; + case '-': + a = 4; + if (swapped) + a++; + break; + case '*': + a = 1; + break; + case '/': + a = 6; + if (swapped) + a++; + break; + } + ft = vtop->type.t; + fc = vtop->c.i; + if ((ft & VT_BTYPE) == VT_LDOUBLE) { + o(0xde); /* fxxxp %st, %st(1) */ + o(0xc1 + (a << 3)); + } else { + /* if saved lvalue, then we must reload it */ + r = vtop->r; + if ((r & VT_VALMASK) == VT_LLOCAL) { + SValue v1; + r = get_reg(RC_INT); + v1.type.t = VT_INT; + v1.r = VT_LOCAL | VT_LVAL; + v1.c.i = fc; + v1.sym = NULL; + load(r, &v1); + fc = 0; + } + + if ((ft & VT_BTYPE) == VT_DOUBLE) + o(0xdc); + else + o(0xd8); + gen_modrm(a, r, vtop->sym, fc); + } + vtop--; + } +} + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +ST_FUNC void gen_cvt_itof(int t) +{ + save_reg(TREG_ST0); + gv(RC_INT); + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { + /* signed long long to float/double/long double (unsigned case + is handled generically) */ + o(0x50 + vtop->r2); /* push r2 */ + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x242cdf); /* fildll (%esp) */ + o(0x08c483); /* add $8, %esp */ + vtop->r2 = VT_CONST; + } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == + (VT_INT | VT_UNSIGNED)) { + /* unsigned int to float/double/long double */ + o(0x6a); /* push $0 */ + g(0x00); + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x242cdf); /* fildll (%esp) */ + o(0x08c483); /* add $8, %esp */ + } else { + /* int to float/double/long double */ + o(0x50 + (vtop->r & VT_VALMASK)); /* push r */ + o(0x2404db); /* fildl (%esp) */ + o(0x04c483); /* add $4, %esp */ + } + vtop->r2 = VT_CONST; + vtop->r = TREG_ST0; +} + +/* convert fp to int 't' type */ +ST_FUNC void gen_cvt_ftoi(int t) +{ + int bt = vtop->type.t & VT_BTYPE; + if (bt == VT_FLOAT) + vpush_helper_func(TOK___fixsfdi); + else if (bt == VT_LDOUBLE) + vpush_helper_func(TOK___fixxfdi); + else + vpush_helper_func(TOK___fixdfdi); + vswap(); + gfunc_call(1); + vpushi(0); + vtop->r = REG_IRET; + if ((t & VT_BTYPE) == VT_LLONG) + vtop->r2 = REG_IRE2; +} + +/* convert from one floating point type to another */ +ST_FUNC void gen_cvt_ftof(int t) +{ + /* all we have to do on i386 is to put the float in a register */ + gv(RC_FLOAT); +} + +/* char/short to int conversion */ +ST_FUNC void gen_cvt_csti(int t) +{ + int r, sz, xl; + r = gv(RC_INT); + sz = !(t & VT_UNSIGNED); + xl = (t & VT_BTYPE) == VT_SHORT; + o(0xc0b60f /* mov[sz] %a[xl], %eax */ + | (sz << 3 | xl) << 8 + | (r << 3 | r) << 16 + ); +} + +/* increment tcov counter */ +ST_FUNC void gen_increment_tcov (SValue *sv) +{ + o(0x0583); /* addl $1, xxx */ + greloc(cur_text_section, sv->sym, ind, R_386_32); + gen_le32(0); + o(1); + o(0x1583); /* addcl $0, xxx */ + greloc(cur_text_section, sv->sym, ind, R_386_32); + gen_le32(4); + g(0); +} + +/* computed goto support */ +ST_FUNC void ggoto(void) +{ + gcall_or_jmp(1); + vtop--; +} + +/* bound check support functions */ +#ifdef CONFIG_TCC_BCHECK + +static void gen_bounds_prolog(void) +{ + /* leave some room for bound checking code */ + func_bound_offset = lbounds_section->data_offset; + func_bound_ind = ind; + func_bound_add_epilog = 0; + oad(0xb8, 0); /* lbound section pointer */ + oad(0xb8, 0); /* call to function */ +} + +static void gen_bounds_epilog(void) +{ + addr_t saved_ind; + addr_t *bounds_ptr; + Sym *sym_data; + int offset_modified = func_bound_offset != lbounds_section->data_offset; + + if (!offset_modified && !func_bound_add_epilog) + return; + + /* add end of table info */ + bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t)); + *bounds_ptr = 0; + + sym_data = get_sym_ref(&char_pointer_type, lbounds_section, + func_bound_offset, PTR_SIZE); + + /* generate bound local allocation */ + if (offset_modified) { + saved_ind = ind; + ind = func_bound_ind; + greloc(cur_text_section, sym_data, ind + 1, R_386_32); + ind = ind + 5; + gen_static_call(TOK___bound_local_new); + ind = saved_ind; + } + + /* generate bound check local freeing */ + o(0x5250); /* save returned value, if any */ + greloc(cur_text_section, sym_data, ind + 1, R_386_32); + oad(0xb8, 0); /* mov %eax, xxx */ + gen_static_call(TOK___bound_local_delete); + o(0x585a); /* restore returned value, if any */ +} +#endif + +/* Save the stack pointer onto the stack */ +ST_FUNC void gen_vla_sp_save(int addr) { + /* mov %esp,addr(%ebp)*/ + o(0x89); + gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr); +} + +/* Restore the SP from a location on the stack */ +ST_FUNC void gen_vla_sp_restore(int addr) { + o(0x8b); + gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr); +} + +/* Subtract from the stack pointer, and push the resulting value onto the stack */ +ST_FUNC void gen_vla_alloc(CType *type, int align) { + int use_call = 0; + +#if defined(CONFIG_TCC_BCHECK) + use_call = tcc_state->do_bounds_check; +#endif +#ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */ + use_call = 1; +#endif + if (use_call) + { + vpush_helper_func(TOK_alloca); + vswap(); /* Move alloca ref past allocation size */ + gfunc_call(1); + } + else { + int r; + r = gv(RC_INT); /* allocation size */ + /* sub r,%rsp */ + o(0x2b); + o(0xe0 | r); + /* We align to 16 bytes rather than align */ + /* and ~15, %esp */ + o(0xf0e483); + vpop(); + } +} + +/* end of X86 code generator */ +/*************************************************************/ +#endif +/*************************************************************/ diff --git a/05/tcc-final/i386-link.c b/05/tcc-final/i386-link.c new file mode 100644 index 0000000..11581f5 --- /dev/null +++ b/05/tcc-final/i386-link.c @@ -0,0 +1,325 @@ +#ifdef TARGET_DEFS_ONLY + +#define EM_TCC_TARGET EM_386 + +/* relocation type for 32 bit data relocation */ +#define R_DATA_32 R_386_32 +#define R_DATA_PTR R_386_32 +#define R_JMP_SLOT R_386_JMP_SLOT +#define R_GLOB_DAT R_386_GLOB_DAT +#define R_COPY R_386_COPY +#define R_RELATIVE R_386_RELATIVE + +#define R_NUM R_386_NUM + +#define ELF_START_ADDR 0x08048000 +#define ELF_PAGE_SIZE 0x1000 + +#define PCRELATIVE_DLLPLT 0 +#define RELOCATE_DLLPLT 1 + +#else /* !TARGET_DEFS_ONLY */ + +#include "tcc.h" + +#ifdef NEED_RELOC_TYPE +/* Returns 1 for a code relocation, 0 for a data relocation. For unknown + relocations, returns -1. */ +int code_reloc (int reloc_type) +{ + switch (reloc_type) { + case R_386_RELATIVE: + case R_386_16: + case R_386_32: + case R_386_GOTPC: + case R_386_GOTOFF: + case R_386_GOT32: + case R_386_GOT32X: + case R_386_GLOB_DAT: + case R_386_COPY: + case R_386_TLS_GD: + case R_386_TLS_LDM: + case R_386_TLS_LDO_32: + case R_386_TLS_LE: + return 0; + + case R_386_PC16: + case R_386_PC32: + case R_386_PLT32: + case R_386_JMP_SLOT: + return 1; + } + return -1; +} + +/* Returns an enumerator to describe whether and when the relocation needs a + GOT and/or PLT entry to be created. See tcc.h for a description of the + different values. */ +int gotplt_entry_type (int reloc_type) +{ + switch (reloc_type) { + case R_386_RELATIVE: + case R_386_16: + case R_386_GLOB_DAT: + case R_386_JMP_SLOT: + case R_386_COPY: + return NO_GOTPLT_ENTRY; + + case R_386_32: + /* This relocations shouldn't normally need GOT or PLT + slots if it weren't for simplicity in the code generator. + See our caller for comments. */ + return AUTO_GOTPLT_ENTRY; + + case R_386_PC16: + case R_386_PC32: + return AUTO_GOTPLT_ENTRY; + + case R_386_GOTPC: + case R_386_GOTOFF: + return BUILD_GOT_ONLY; + + case R_386_GOT32: + case R_386_GOT32X: + case R_386_PLT32: + case R_386_TLS_GD: + case R_386_TLS_LDM: + case R_386_TLS_LDO_32: + case R_386_TLS_LE: + return ALWAYS_GOTPLT_ENTRY; + } + return -1; +} + +#ifdef NEED_BUILD_GOT +ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr) +{ + Section *plt = s1->plt; + uint8_t *p; + int modrm; + unsigned plt_offset, relofs; + + /* on i386 if we build a DLL, we add a %ebx offset */ + if (s1->output_type == TCC_OUTPUT_DLL) + modrm = 0xa3; + else + modrm = 0x25; + + /* empty PLT: create PLT0 entry that pushes the library identifier + (GOT + PTR_SIZE) and jumps to ld.so resolution routine + (GOT + 2 * PTR_SIZE) */ + if (plt->data_offset == 0) { + p = section_ptr_add(plt, 16); + p[0] = 0xff; /* pushl got + PTR_SIZE */ + p[1] = modrm + 0x10; + write32le(p + 2, PTR_SIZE); + p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */ + p[7] = modrm; + write32le(p + 8, PTR_SIZE * 2); + } + plt_offset = plt->data_offset; + + /* The PLT slot refers to the relocation entry it needs via offset. + The reloc entry is created below, so its offset is the current + data_offset */ + relofs = s1->plt->reloc ? s1->plt->reloc->data_offset : 0; + + /* Jump to GOT entry where ld.so initially put the address of ip + 4 */ + p = section_ptr_add(plt, 16); + p[0] = 0xff; /* jmp *(got + x) */ + p[1] = modrm; + write32le(p + 2, got_offset); + p[6] = 0x68; /* push $xxx */ + write32le(p + 7, relofs - sizeof (ElfW_Rel)); + p[11] = 0xe9; /* jmp plt_start */ + write32le(p + 12, -(plt->data_offset)); + return plt_offset; +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + + if (s1->output_type != TCC_OUTPUT_DLL && p < p_end) { + add32le(p + 2, s1->got->sh_addr); + add32le(p + 8, s1->got->sh_addr); + p += 16; + while (p < p_end) { + add32le(p + 2, s1->got->sh_addr); + p += 16; + } + } + + if (s1->plt->reloc) { + ElfW_Rel *rel; + int x = s1->plt->sh_addr + 16 + 6; + p = s1->got->data; + for_each_elem(s1->plt->reloc, 0, rel, ElfW_Rel) { + write32le(p + rel->r_offset, x); + x += 16; + } + } +} +#endif +#endif + +void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val) +{ + int sym_index, esym_index; + + sym_index = ELFW(R_SYM)(rel->r_info); + + switch (type) { + case R_386_32: + if (s1->output_type == TCC_OUTPUT_DLL) { + esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index; + qrel->r_offset = rel->r_offset; + if (esym_index) { + qrel->r_info = ELFW(R_INFO)(esym_index, R_386_32); + qrel++; + return; + } else { + qrel->r_info = ELFW(R_INFO)(0, R_386_RELATIVE); + qrel++; + } + } + add32le(ptr, val); + return; + case R_386_PC32: + if (s1->output_type == TCC_OUTPUT_DLL) { + /* DLL relocation */ + esym_index = get_sym_attr(s1, sym_index, 0)->dyn_index; + if (esym_index) { + qrel->r_offset = rel->r_offset; + qrel->r_info = ELFW(R_INFO)(esym_index, R_386_PC32); + qrel++; + return; + } + } + add32le(ptr, val - addr); + return; + case R_386_PLT32: + add32le(ptr, val - addr); + return; + case R_386_GLOB_DAT: + case R_386_JMP_SLOT: + write32le(ptr, val); + return; + case R_386_GOTPC: + add32le(ptr, s1->got->sh_addr - addr); + return; + case R_386_GOTOFF: + add32le(ptr, val - s1->got->sh_addr); + return; + case R_386_GOT32: + case R_386_GOT32X: + /* we load the got offset */ + add32le(ptr, get_sym_attr(s1, sym_index, 0)->got_offset); + return; + case R_386_16: + if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) { + output_file: + tcc_error("can only produce 16-bit binary files"); + } + write16le(ptr, read16le(ptr) + val); + return; + case R_386_PC16: + if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) + goto output_file; + write16le(ptr, read16le(ptr) + val - addr); + return; + case R_386_RELATIVE: +#ifdef TCC_TARGET_PE + add32le(ptr, val - s1->pe_imagebase); +#endif + /* do nothing */ + return; + case R_386_COPY: + /* This relocation must copy initialized data from the library + to the program .bss segment. Currently made like for ARM + (to remove noise of default case). Is this true? + */ + return; + case R_386_TLS_GD: + { + static const unsigned char expect[] = { + /* lea 0(,%ebx,1),%eax */ + 0x8d, 0x04, 0x1d, 0x00, 0x00, 0x00, 0x00, + /* call __tls_get_addr@PLT */ + 0xe8, 0xfc, 0xff, 0xff, 0xff }; + static const unsigned char replace[] = { + /* mov %gs:0,%eax */ + 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, + /* sub 0,%eax */ + 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 }; + + if (memcmp (ptr-3, expect, sizeof(expect)) == 0) { + ElfW(Sym) *sym; + Section *sec; + int32_t x; + + memcpy(ptr-3, replace, sizeof(replace)); + rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + sec = s1->sections[sym->st_shndx]; + x = sym->st_value - sec->sh_addr - sec->data_offset; + add32le(ptr + 5, -x); + } + else + tcc_error("unexpected R_386_TLS_GD pattern"); + } + return; + case R_386_TLS_LDM: + { + static const unsigned char expect[] = { + /* lea 0(%ebx),%eax */ + 0x8d, 0x83, 0x00, 0x00, 0x00, 0x00, + /* call __tls_get_addr@PLT */ + 0xe8, 0xfc, 0xff, 0xff, 0xff }; + static const unsigned char replace[] = { + /* mov %gs:0,%eax */ + 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, + /* nop */ + 0x90, + /* lea 0(%esi,%eiz,1),%esi */ + 0x8d, 0x74, 0x26, 0x00 }; + + if (memcmp (ptr-2, expect, sizeof(expect)) == 0) { + memcpy(ptr-2, replace, sizeof(replace)); + rel[1].r_info = ELFW(R_INFO)(0, R_386_NONE); + } + else + tcc_error("unexpected R_386_TLS_LDM pattern"); + } + return; + case R_386_TLS_LDO_32: + case R_386_TLS_LE: + { + ElfW(Sym) *sym; + Section *sec; + int32_t x; + + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + sec = s1->sections[sym->st_shndx]; + x = val - sec->sh_addr - sec->data_offset; + add32le(ptr, x); + } + return; + case R_386_NONE: + return; + default: + fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n", + type, (unsigned)addr, ptr, (unsigned)val); + return; + } +} + +#endif /* !TARGET_DEFS_ONLY */ diff --git a/05/tcc-final/i386-tok.h b/05/tcc-final/i386-tok.h new file mode 100644 index 0000000..e3ef2c7 --- /dev/null +++ b/05/tcc-final/i386-tok.h @@ -0,0 +1,329 @@ +/* ------------------------------------------------------------------ */ +/* WARNING: relative order of tokens is important. */ + +#define DEF_BWL(x) \ + DEF(TOK_ASM_ ## x ## b, #x "b") \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x, #x) +#define DEF_WL(x) \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x, #x) +#ifdef TCC_TARGET_X86_64 +# define DEF_BWLQ(x) \ + DEF(TOK_ASM_ ## x ## b, #x "b") \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x ## q, #x "q") \ + DEF(TOK_ASM_ ## x, #x) +# define DEF_WLQ(x) \ + DEF(TOK_ASM_ ## x ## w, #x "w") \ + DEF(TOK_ASM_ ## x ## l, #x "l") \ + DEF(TOK_ASM_ ## x ## q, #x "q") \ + DEF(TOK_ASM_ ## x, #x) +# define DEF_BWLX DEF_BWLQ +# define DEF_WLX DEF_WLQ +/* number of sizes + 1 */ +# define NBWLX 5 +#else +# define DEF_BWLX DEF_BWL +# define DEF_WLX DEF_WL +/* number of sizes + 1 */ +# define NBWLX 4 +#endif + +#define DEF_FP1(x) \ + DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \ + DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \ + DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \ + DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s") + +#define DEF_FP(x) \ + DEF(TOK_ASM_ ## f ## x, "f" #x ) \ + DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \ + DEF_FP1(x) + +#define DEF_ASMTEST(x,suffix) \ + DEF_ASM(x ## o ## suffix) \ + DEF_ASM(x ## no ## suffix) \ + DEF_ASM(x ## b ## suffix) \ + DEF_ASM(x ## c ## suffix) \ + DEF_ASM(x ## nae ## suffix) \ + DEF_ASM(x ## nb ## suffix) \ + DEF_ASM(x ## nc ## suffix) \ + DEF_ASM(x ## ae ## suffix) \ + DEF_ASM(x ## e ## suffix) \ + DEF_ASM(x ## z ## suffix) \ + DEF_ASM(x ## ne ## suffix) \ + DEF_ASM(x ## nz ## suffix) \ + DEF_ASM(x ## be ## suffix) \ + DEF_ASM(x ## na ## suffix) \ + DEF_ASM(x ## nbe ## suffix) \ + DEF_ASM(x ## a ## suffix) \ + DEF_ASM(x ## s ## suffix) \ + DEF_ASM(x ## ns ## suffix) \ + DEF_ASM(x ## p ## suffix) \ + DEF_ASM(x ## pe ## suffix) \ + DEF_ASM(x ## np ## suffix) \ + DEF_ASM(x ## po ## suffix) \ + DEF_ASM(x ## l ## suffix) \ + DEF_ASM(x ## nge ## suffix) \ + DEF_ASM(x ## nl ## suffix) \ + DEF_ASM(x ## ge ## suffix) \ + DEF_ASM(x ## le ## suffix) \ + DEF_ASM(x ## ng ## suffix) \ + DEF_ASM(x ## nle ## suffix) \ + DEF_ASM(x ## g ## suffix) + +/* ------------------------------------------------------------------ */ +/* register */ + DEF_ASM(al) + DEF_ASM(cl) + DEF_ASM(dl) + DEF_ASM(bl) + DEF_ASM(ah) + DEF_ASM(ch) + DEF_ASM(dh) + DEF_ASM(bh) + DEF_ASM(ax) + DEF_ASM(cx) + DEF_ASM(dx) + DEF_ASM(bx) + DEF_ASM(sp) + DEF_ASM(bp) + DEF_ASM(si) + DEF_ASM(di) + DEF_ASM(eax) + DEF_ASM(ecx) + DEF_ASM(edx) + DEF_ASM(ebx) + DEF_ASM(esp) + DEF_ASM(ebp) + DEF_ASM(esi) + DEF_ASM(edi) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(rax) + DEF_ASM(rcx) + DEF_ASM(rdx) + DEF_ASM(rbx) + DEF_ASM(rsp) + DEF_ASM(rbp) + DEF_ASM(rsi) + DEF_ASM(rdi) +#endif + DEF_ASM(mm0) + DEF_ASM(mm1) + DEF_ASM(mm2) + DEF_ASM(mm3) + DEF_ASM(mm4) + DEF_ASM(mm5) + DEF_ASM(mm6) + DEF_ASM(mm7) + DEF_ASM(xmm0) + DEF_ASM(xmm1) + DEF_ASM(xmm2) + DEF_ASM(xmm3) + DEF_ASM(xmm4) + DEF_ASM(xmm5) + DEF_ASM(xmm6) + DEF_ASM(xmm7) + DEF_ASM(cr0) + DEF_ASM(cr1) + DEF_ASM(cr2) + DEF_ASM(cr3) + DEF_ASM(cr4) + DEF_ASM(cr5) + DEF_ASM(cr6) + DEF_ASM(cr7) + DEF_ASM(tr0) + DEF_ASM(tr1) + DEF_ASM(tr2) + DEF_ASM(tr3) + DEF_ASM(tr4) + DEF_ASM(tr5) + DEF_ASM(tr6) + DEF_ASM(tr7) + DEF_ASM(db0) + DEF_ASM(db1) + DEF_ASM(db2) + DEF_ASM(db3) + DEF_ASM(db4) + DEF_ASM(db5) + DEF_ASM(db6) + DEF_ASM(db7) + DEF_ASM(dr0) + DEF_ASM(dr1) + DEF_ASM(dr2) + DEF_ASM(dr3) + DEF_ASM(dr4) + DEF_ASM(dr5) + DEF_ASM(dr6) + DEF_ASM(dr7) + DEF_ASM(es) + DEF_ASM(cs) + DEF_ASM(ss) + DEF_ASM(ds) + DEF_ASM(fs) + DEF_ASM(gs) + DEF_ASM(st) + DEF_ASM(rip) + +#ifdef TCC_TARGET_X86_64 + /* The four low parts of sp/bp/si/di that exist only on + x86-64 (encoding aliased to ah,ch,dh,dh when not using REX). */ + DEF_ASM(spl) + DEF_ASM(bpl) + DEF_ASM(sil) + DEF_ASM(dil) +#endif + /* generic two operands */ + DEF_BWLX(mov) + + DEF_BWLX(add) + DEF_BWLX(or) + DEF_BWLX(adc) + DEF_BWLX(sbb) + DEF_BWLX(and) + DEF_BWLX(sub) + DEF_BWLX(xor) + DEF_BWLX(cmp) + + /* unary ops */ + DEF_BWLX(inc) + DEF_BWLX(dec) + DEF_BWLX(not) + DEF_BWLX(neg) + DEF_BWLX(mul) + DEF_BWLX(imul) + DEF_BWLX(div) + DEF_BWLX(idiv) + + DEF_BWLX(xchg) + DEF_BWLX(test) + + /* shifts */ + DEF_BWLX(rol) + DEF_BWLX(ror) + DEF_BWLX(rcl) + DEF_BWLX(rcr) + DEF_BWLX(shl) + DEF_BWLX(shr) + DEF_BWLX(sar) + + DEF_WLX(shld) + DEF_WLX(shrd) + + DEF_ASM(pushw) + DEF_ASM(pushl) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(pushq) +#endif + DEF_ASM(push) + + DEF_ASM(popw) + DEF_ASM(popl) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(popq) +#endif + DEF_ASM(pop) + + DEF_BWL(in) + DEF_BWL(out) + + DEF_WLX(movzb) + DEF_ASM(movzwl) + DEF_ASM(movsbw) + DEF_ASM(movsbl) + DEF_ASM(movswl) +#ifdef TCC_TARGET_X86_64 + DEF_ASM(movsbq) + DEF_ASM(movswq) + DEF_ASM(movzwq) + DEF_ASM(movslq) +#endif + + DEF_WLX(lea) + + DEF_ASM(les) + DEF_ASM(lds) + DEF_ASM(lss) + DEF_ASM(lfs) + DEF_ASM(lgs) + + DEF_ASM(call) + DEF_ASM(jmp) + DEF_ASM(lcall) + DEF_ASM(ljmp) + + DEF_ASMTEST(j,) + + DEF_ASMTEST(set,) + DEF_ASMTEST(set,b) + DEF_ASMTEST(cmov,) + + DEF_WLX(bsf) + DEF_WLX(bsr) + DEF_WLX(bt) + DEF_WLX(bts) + DEF_WLX(btr) + DEF_WLX(btc) + + DEF_WLX(lar) + DEF_WLX(lsl) + + /* generic FP ops */ + DEF_FP(add) + DEF_FP(mul) + + DEF_ASM(fcom) + DEF_ASM(fcom_1) /* non existent op, just to have a regular table */ + DEF_FP1(com) + + DEF_FP(comp) + DEF_FP(sub) + DEF_FP(subr) + DEF_FP(div) + DEF_FP(divr) + + DEF_BWLX(xadd) + DEF_BWLX(cmpxchg) + + /* string ops */ + DEF_BWLX(cmps) + DEF_BWLX(scmp) + DEF_BWL(ins) + DEF_BWL(outs) + DEF_BWLX(lods) + DEF_BWLX(slod) + DEF_BWLX(movs) + DEF_BWLX(smov) + DEF_BWLX(scas) + DEF_BWLX(ssca) + DEF_BWLX(stos) + DEF_BWLX(ssto) + + /* generic asm ops */ +#define ALT(x) +#define DEF_ASM_OP0(name, opcode) DEF_ASM(name) +#define DEF_ASM_OP0L(name, opcode, group, instr_type) +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif + +#define ALT(x) +#define DEF_ASM_OP0(name, opcode) +#define DEF_ASM_OP0L(name, opcode, group, instr_type) DEF_ASM(name) +#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) DEF_ASM(name) +#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) DEF_ASM(name) +#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) DEF_ASM(name) +#ifdef TCC_TARGET_X86_64 +# include "x86_64-asm.h" +#else +# include "i386-asm.h" +#endif diff --git a/05/tcc-final/il-gen.c b/05/tcc-final/il-gen.c new file mode 100644 index 0000000..bb670cc --- /dev/null +++ b/05/tcc-final/il-gen.c @@ -0,0 +1,657 @@ +/* + * CIL code generator for TCC + * + * Copyright (c) 2002 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#error this code has bit-rotted since 2003 + +/* number of available registers */ +#define NB_REGS 3 + +/* a register can belong to several classes. The classes must be + sorted from more general to more precise (see gv2() code which does + assumptions on it). */ +#define RC_ST 0x0001 /* any stack entry */ +#define RC_ST0 0x0002 /* top of stack */ +#define RC_ST1 0x0004 /* top - 1 */ + +#define RC_INT RC_ST +#define RC_FLOAT RC_ST +#define RC_IRET RC_ST0 /* function return: integer register */ +#define RC_LRET RC_ST0 /* function return: second integer register */ +#define RC_FRET RC_ST0 /* function return: float register */ + +/* pretty names for the registers */ +enum { + REG_ST0 = 0, + REG_ST1, + REG_ST2, +}; + +const int reg_classes[NB_REGS] = { + /* ST0 */ RC_ST | RC_ST0, + /* ST1 */ RC_ST | RC_ST1, + /* ST2 */ RC_ST, +}; + +/* return registers for function */ +#define REG_IRET REG_ST0 /* single word int return register */ +#define REG_LRET REG_ST0 /* second word return register (for long long) */ +#define REG_FRET REG_ST0 /* float return register */ + +/* defined if function parameters must be evaluated in reverse order */ +/* #define INVERT_FUNC_PARAMS */ + +/* defined if structures are passed as pointers. Otherwise structures + are directly pushed on stack. */ +/* #define FUNC_STRUCT_PARAM_AS_PTR */ + +/* pointer size, in bytes */ +#define PTR_SIZE 4 + +/* long double size and alignment, in bytes */ +#define LDOUBLE_SIZE 8 +#define LDOUBLE_ALIGN 8 + +/* function call context */ +typedef struct GFuncContext { + int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */ +} GFuncContext; + +/******************************************************/ +/* opcode definitions */ + +#define IL_OP_PREFIX 0xFE + +enum ILOPCodes { +#define OP(name, str, n) IL_OP_ ## name = n, +#include "il-opcodes.h" +#undef OP +}; + +char *il_opcodes_str[] = { +#define OP(name, str, n) [n] = str, +#include "il-opcodes.h" +#undef OP +}; + +/******************************************************/ + +/* arguments variable numbers start from there */ +#define ARG_BASE 0x70000000 + +static FILE *il_outfile; + +static void out_byte(int c) +{ + *(char *)ind++ = c; +} + +static void out_le32(int c) +{ + out_byte(c); + out_byte(c >> 8); + out_byte(c >> 16); + out_byte(c >> 24); +} + +static void init_outfile(void) +{ + if (!il_outfile) { + il_outfile = stdout; + fprintf(il_outfile, + ".assembly extern mscorlib\n" + "{\n" + ".ver 1:0:2411:0\n" + "}\n\n"); + } +} + +static void out_op1(int op) +{ + if (op & 0x100) + out_byte(IL_OP_PREFIX); + out_byte(op & 0xff); +} + +/* output an opcode with prefix */ +static void out_op(int op) +{ + out_op1(op); + fprintf(il_outfile, " %s\n", il_opcodes_str[op]); +} + +static void out_opb(int op, int c) +{ + out_op1(op); + out_byte(c); + fprintf(il_outfile, " %s %d\n", il_opcodes_str[op], c); +} + +static void out_opi(int op, int c) +{ + out_op1(op); + out_le32(c); + fprintf(il_outfile, " %s 0x%x\n", il_opcodes_str[op], c); +} + +/* XXX: not complete */ +static void il_type_to_str(char *buf, int buf_size, + int t, const char *varstr) +{ + int bt; + Sym *s, *sa; + char buf1[256]; + const char *tstr; + + t = t & VT_TYPE; + bt = t & VT_BTYPE; + buf[0] = '\0'; + if (t & VT_UNSIGNED) + pstrcat(buf, buf_size, "unsigned "); + switch(bt) { + case VT_VOID: + tstr = "void"; + goto add_tstr; + case VT_BOOL: + tstr = "bool"; + goto add_tstr; + case VT_BYTE: + tstr = "int8"; + goto add_tstr; + case VT_SHORT: + tstr = "int16"; + goto add_tstr; + case VT_ENUM: + case VT_INT: + case VT_LONG: + tstr = "int32"; + goto add_tstr; + case VT_LLONG: + tstr = "int64"; + goto add_tstr; + case VT_FLOAT: + tstr = "float32"; + goto add_tstr; + case VT_DOUBLE: + case VT_LDOUBLE: + tstr = "float64"; + add_tstr: + pstrcat(buf, buf_size, tstr); + break; + case VT_STRUCT: + tcc_error("structures not handled yet"); + break; + case VT_FUNC: + s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); + il_type_to_str(buf, buf_size, s->t, varstr); + pstrcat(buf, buf_size, "("); + sa = s->next; + while (sa != NULL) { + il_type_to_str(buf1, sizeof(buf1), sa->t, NULL); + pstrcat(buf, buf_size, buf1); + sa = sa->next; + if (sa) + pstrcat(buf, buf_size, ", "); + } + pstrcat(buf, buf_size, ")"); + goto no_var; + case VT_PTR: + s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); + pstrcpy(buf1, sizeof(buf1), "*"); + if (varstr) + pstrcat(buf1, sizeof(buf1), varstr); + il_type_to_str(buf, buf_size, s->t, buf1); + goto no_var; + } + if (varstr) { + pstrcat(buf, buf_size, " "); + pstrcat(buf, buf_size, varstr); + } + no_var: ; +} + + +/* patch relocation entry with value 'val' */ +void greloc_patch1(Reloc *p, int val) +{ +} + +/* output a symbol and patch all calls to it */ +void gsym_addr(t, a) +{ +} + +/* output jump and return symbol */ +static int out_opj(int op, int c) +{ + out_op1(op); + out_le32(0); + if (c == 0) { + c = ind - (int)cur_text_section->data; + } + fprintf(il_outfile, " %s L%d\n", il_opcodes_str[op], c); + return c; +} + +void gsym(int t) +{ + fprintf(il_outfile, "L%d:\n", t); +} + +/* load 'r' from value 'sv' */ +void load(int r, SValue *sv) +{ + int v, fc, ft; + + v = sv->r & VT_VALMASK; + fc = sv->c.i; + ft = sv->t; + + if (sv->r & VT_LVAL) { + if (v == VT_LOCAL) { + if (fc >= ARG_BASE) { + fc -= ARG_BASE; + if (fc >= 0 && fc <= 4) { + out_op(IL_OP_LDARG_0 + fc); + } else if (fc <= 0xff) { + out_opb(IL_OP_LDARG_S, fc); + } else { + out_opi(IL_OP_LDARG, fc); + } + } else { + if (fc >= 0 && fc <= 4) { + out_op(IL_OP_LDLOC_0 + fc); + } else if (fc <= 0xff) { + out_opb(IL_OP_LDLOC_S, fc); + } else { + out_opi(IL_OP_LDLOC, fc); + } + } + } else if (v == VT_CONST) { + /* XXX: handle globals */ + out_opi(IL_OP_LDSFLD, 0); + } else { + if ((ft & VT_BTYPE) == VT_FLOAT) { + out_op(IL_OP_LDIND_R4); + } else if ((ft & VT_BTYPE) == VT_DOUBLE) { + out_op(IL_OP_LDIND_R8); + } else if ((ft & VT_BTYPE) == VT_LDOUBLE) { + out_op(IL_OP_LDIND_R8); + } else if ((ft & VT_TYPE) == VT_BYTE) + out_op(IL_OP_LDIND_I1); + else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) + out_op(IL_OP_LDIND_U1); + else if ((ft & VT_TYPE) == VT_SHORT) + out_op(IL_OP_LDIND_I2); + else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) + out_op(IL_OP_LDIND_U2); + else + out_op(IL_OP_LDIND_I4); + } + } else { + if (v == VT_CONST) { + /* XXX: handle globals */ + if (fc >= -1 && fc <= 8) { + out_op(IL_OP_LDC_I4_M1 + fc + 1); + } else { + out_opi(IL_OP_LDC_I4, fc); + } + } else if (v == VT_LOCAL) { + if (fc >= ARG_BASE) { + fc -= ARG_BASE; + if (fc <= 0xff) { + out_opb(IL_OP_LDARGA_S, fc); + } else { + out_opi(IL_OP_LDARGA, fc); + } + } else { + if (fc <= 0xff) { + out_opb(IL_OP_LDLOCA_S, fc); + } else { + out_opi(IL_OP_LDLOCA, fc); + } + } + } else { + /* XXX: do it */ + } + } +} + +/* store register 'r' in lvalue 'v' */ +void store(int r, SValue *sv) +{ + int v, fc, ft; + + v = sv->r & VT_VALMASK; + fc = sv->c.i; + ft = sv->t; + if (v == VT_LOCAL) { + if (fc >= ARG_BASE) { + fc -= ARG_BASE; + /* XXX: check IL arg store semantics */ + if (fc <= 0xff) { + out_opb(IL_OP_STARG_S, fc); + } else { + out_opi(IL_OP_STARG, fc); + } + } else { + if (fc >= 0 && fc <= 4) { + out_op(IL_OP_STLOC_0 + fc); + } else if (fc <= 0xff) { + out_opb(IL_OP_STLOC_S, fc); + } else { + out_opi(IL_OP_STLOC, fc); + } + } + } else if (v == VT_CONST) { + /* XXX: handle globals */ + out_opi(IL_OP_STSFLD, 0); + } else { + if ((ft & VT_BTYPE) == VT_FLOAT) + out_op(IL_OP_STIND_R4); + else if ((ft & VT_BTYPE) == VT_DOUBLE) + out_op(IL_OP_STIND_R8); + else if ((ft & VT_BTYPE) == VT_LDOUBLE) + out_op(IL_OP_STIND_R8); + else if ((ft & VT_BTYPE) == VT_BYTE) + out_op(IL_OP_STIND_I1); + else if ((ft & VT_BTYPE) == VT_SHORT) + out_op(IL_OP_STIND_I2); + else + out_op(IL_OP_STIND_I4); + } +} + +/* start function call and return function call context */ +void gfunc_start(GFuncContext *c, int func_call) +{ + c->func_call = func_call; +} + +/* push function parameter which is in (vtop->t, vtop->c). Stack entry + is then popped. */ +void gfunc_param(GFuncContext *c) +{ + if ((vtop->t & VT_BTYPE) == VT_STRUCT) { + tcc_error("structures passed as value not handled yet"); + } else { + /* simply push on stack */ + gv(RC_ST0); + } + vtop--; +} + +/* generate function call with address in (vtop->t, vtop->c) and free function + context. Stack entry is popped */ +void gfunc_call(GFuncContext *c) +{ + char buf[1024]; + + if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { + /* XXX: more info needed from tcc */ + il_type_to_str(buf, sizeof(buf), vtop->t, "xxx"); + fprintf(il_outfile, " call %s\n", buf); + } else { + /* indirect call */ + gv(RC_INT); + il_type_to_str(buf, sizeof(buf), vtop->t, NULL); + fprintf(il_outfile, " calli %s\n", buf); + } + vtop--; +} + +/* generate function prolog of type 't' */ +void gfunc_prolog(int t) +{ + int addr, u, func_call; + Sym *sym; + char buf[1024]; + + init_outfile(); + + /* XXX: pass function name to gfunc_prolog */ + il_type_to_str(buf, sizeof(buf), t, funcname); + fprintf(il_outfile, ".method static %s il managed\n", buf); + fprintf(il_outfile, "{\n"); + /* XXX: cannot do better now */ + fprintf(il_outfile, " .maxstack %d\n", NB_REGS); + fprintf(il_outfile, " .locals (int32, int32, int32, int32, int32, int32, int32, int32)\n"); + + if (!strcmp(funcname, "main")) + fprintf(il_outfile, " .entrypoint\n"); + + sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT); + func_call = sym->r; + + addr = ARG_BASE; + /* if the function returns a structure, then add an + implicit pointer parameter */ + func_vt = sym->t; + func_var = (sym->c == FUNC_ELLIPSIS); + if ((func_vt & VT_BTYPE) == VT_STRUCT) { + func_vc = addr; + addr++; + } + /* define parameters */ + while ((sym = sym->next) != NULL) { + u = sym->t; + sym_push(sym->v & ~SYM_FIELD, u, + VT_LOCAL | lvalue_type(sym->type.t), addr); + addr++; + } +} + +/* generate function epilog */ +void gfunc_epilog(void) +{ + out_op(IL_OP_RET); + fprintf(il_outfile, "}\n\n"); +} + +/* generate a jump to a label */ +int gjmp(int t) +{ + return out_opj(IL_OP_BR, t); +} + +/* generate a jump to a fixed address */ +void gjmp_addr(int a) +{ + /* XXX: handle syms */ + out_opi(IL_OP_BR, a); +} + +/* generate a test. set 'inv' to invert test. Stack entry is popped */ +int gtst(int inv, int t) +{ + int v, *p, c; + + v = vtop->r & VT_VALMASK; + if (v == VT_CMP) { + c = vtop->c.i ^ inv; + switch(c) { + case TOK_EQ: + c = IL_OP_BEQ; + break; + case TOK_NE: + c = IL_OP_BNE_UN; + break; + case TOK_LT: + c = IL_OP_BLT; + break; + case TOK_LE: + c = IL_OP_BLE; + break; + case TOK_GT: + c = IL_OP_BGT; + break; + case TOK_GE: + c = IL_OP_BGE; + break; + case TOK_ULT: + c = IL_OP_BLT_UN; + break; + case TOK_ULE: + c = IL_OP_BLE_UN; + break; + case TOK_UGT: + c = IL_OP_BGT_UN; + break; + case TOK_UGE: + c = IL_OP_BGE_UN; + break; + } + t = out_opj(c, t); + } else if (v == VT_JMP || v == VT_JMPI) { + /* && or || optimization */ + if ((v & 1) == inv) { + /* insert vtop->c jump list in t */ + p = &vtop->c.i; + while (*p != 0) + p = (int *)*p; + *p = t; + t = vtop->c.i; + } else { + t = gjmp(t); + gsym(vtop->c.i); + } + } + vtop--; + return t; +} + +/* generate an integer binary operation */ +void gen_opi(int op) +{ + gv2(RC_ST1, RC_ST0); + switch(op) { + case '+': + out_op(IL_OP_ADD); + goto std_op; + case '-': + out_op(IL_OP_SUB); + goto std_op; + case '&': + out_op(IL_OP_AND); + goto std_op; + case '^': + out_op(IL_OP_XOR); + goto std_op; + case '|': + out_op(IL_OP_OR); + goto std_op; + case '*': + out_op(IL_OP_MUL); + goto std_op; + case TOK_SHL: + out_op(IL_OP_SHL); + goto std_op; + case TOK_SHR: + out_op(IL_OP_SHR_UN); + goto std_op; + case TOK_SAR: + out_op(IL_OP_SHR); + goto std_op; + case '/': + case TOK_PDIV: + out_op(IL_OP_DIV); + goto std_op; + case TOK_UDIV: + out_op(IL_OP_DIV_UN); + goto std_op; + case '%': + out_op(IL_OP_REM); + goto std_op; + case TOK_UMOD: + out_op(IL_OP_REM_UN); + std_op: + vtop--; + vtop[0].r = REG_ST0; + break; + case TOK_EQ: + case TOK_NE: + case TOK_LT: + case TOK_LE: + case TOK_GT: + case TOK_GE: + case TOK_ULT: + case TOK_ULE: + case TOK_UGT: + case TOK_UGE: + vtop--; + vtop[0].r = VT_CMP; + vtop[0].c.i = op; + break; + } +} + +/* generate a floating point operation 'v = t1 op t2' instruction. The + two operands are guaranteed to have the same floating point type */ +void gen_opf(int op) +{ + /* same as integer */ + gen_opi(op); +} + +/* convert integers to fp 't' type. Must handle 'int', 'unsigned int' + and 'long long' cases. */ +void gen_cvt_itof(int t) +{ + gv(RC_ST0); + if (t == VT_FLOAT) + out_op(IL_OP_CONV_R4); + else + out_op(IL_OP_CONV_R8); +} + +/* convert fp to int 't' type */ +/* XXX: handle long long case */ +void gen_cvt_ftoi(int t) +{ + gv(RC_ST0); + switch(t) { + case VT_INT | VT_UNSIGNED: + out_op(IL_OP_CONV_U4); + break; + case VT_LLONG: + out_op(IL_OP_CONV_I8); + break; + case VT_LLONG | VT_UNSIGNED: + out_op(IL_OP_CONV_U8); + break; + default: + out_op(IL_OP_CONV_I4); + break; + } +} + +/* convert from one floating point type to another */ +void gen_cvt_ftof(int t) +{ + gv(RC_ST0); + if (t == VT_FLOAT) { + out_op(IL_OP_CONV_R4); + } else { + out_op(IL_OP_CONV_R8); + } +} + +/* end of CIL code generator */ +/*************************************************************/ + diff --git a/05/tcc-final/il-opcodes.h b/05/tcc-final/il-opcodes.h new file mode 100644 index 0000000..d53ffb2 --- /dev/null +++ b/05/tcc-final/il-opcodes.h @@ -0,0 +1,251 @@ +/* + * CIL opcode definition + * + * Copyright (c) 2002 Fabrice Bellard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +OP(NOP, "nop", 0x00) +OP(BREAK, "break", 0x01) +OP(LDARG_0, "ldarg.0", 0x02) +OP(LDARG_1, "ldarg.1", 0x03) +OP(LDARG_2, "ldarg.2", 0x04) +OP(LDARG_3, "ldarg.3", 0x05) +OP(LDLOC_0, "ldloc.0", 0x06) +OP(LDLOC_1, "ldloc.1", 0x07) +OP(LDLOC_2, "ldloc.2", 0x08) +OP(LDLOC_3, "ldloc.3", 0x09) +OP(STLOC_0, "stloc.0", 0x0a) +OP(STLOC_1, "stloc.1", 0x0b) +OP(STLOC_2, "stloc.2", 0x0c) +OP(STLOC_3, "stloc.3", 0x0d) +OP(LDARG_S, "ldarg.s", 0x0e) +OP(LDARGA_S, "ldarga.s", 0x0f) +OP(STARG_S, "starg.s", 0x10) +OP(LDLOC_S, "ldloc.s", 0x11) +OP(LDLOCA_S, "ldloca.s", 0x12) +OP(STLOC_S, "stloc.s", 0x13) +OP(LDNULL, "ldnull", 0x14) +OP(LDC_I4_M1, "ldc.i4.m1", 0x15) +OP(LDC_I4_0, "ldc.i4.0", 0x16) +OP(LDC_I4_1, "ldc.i4.1", 0x17) +OP(LDC_I4_2, "ldc.i4.2", 0x18) +OP(LDC_I4_3, "ldc.i4.3", 0x19) +OP(LDC_I4_4, "ldc.i4.4", 0x1a) +OP(LDC_I4_5, "ldc.i4.5", 0x1b) +OP(LDC_I4_6, "ldc.i4.6", 0x1c) +OP(LDC_I4_7, "ldc.i4.7", 0x1d) +OP(LDC_I4_8, "ldc.i4.8", 0x1e) +OP(LDC_I4_S, "ldc.i4.s", 0x1f) +OP(LDC_I4, "ldc.i4", 0x20) +OP(LDC_I8, "ldc.i8", 0x21) +OP(LDC_R4, "ldc.r4", 0x22) +OP(LDC_R8, "ldc.r8", 0x23) +OP(LDPTR, "ldptr", 0x24) +OP(DUP, "dup", 0x25) +OP(POP, "pop", 0x26) +OP(JMP, "jmp", 0x27) +OP(CALL, "call", 0x28) +OP(CALLI, "calli", 0x29) +OP(RET, "ret", 0x2a) +OP(BR_S, "br.s", 0x2b) +OP(BRFALSE_S, "brfalse.s", 0x2c) +OP(BRTRUE_S, "brtrue.s", 0x2d) +OP(BEQ_S, "beq.s", 0x2e) +OP(BGE_S, "bge.s", 0x2f) +OP(BGT_S, "bgt.s", 0x30) +OP(BLE_S, "ble.s", 0x31) +OP(BLT_S, "blt.s", 0x32) +OP(BNE_UN_S, "bne.un.s", 0x33) +OP(BGE_UN_S, "bge.un.s", 0x34) +OP(BGT_UN_S, "bgt.un.s", 0x35) +OP(BLE_UN_S, "ble.un.s", 0x36) +OP(BLT_UN_S, "blt.un.s", 0x37) +OP(BR, "br", 0x38) +OP(BRFALSE, "brfalse", 0x39) +OP(BRTRUE, "brtrue", 0x3a) +OP(BEQ, "beq", 0x3b) +OP(BGE, "bge", 0x3c) +OP(BGT, "bgt", 0x3d) +OP(BLE, "ble", 0x3e) +OP(BLT, "blt", 0x3f) +OP(BNE_UN, "bne.un", 0x40) +OP(BGE_UN, "bge.un", 0x41) +OP(BGT_UN, "bgt.un", 0x42) +OP(BLE_UN, "ble.un", 0x43) +OP(BLT_UN, "blt.un", 0x44) +OP(SWITCH, "switch", 0x45) +OP(LDIND_I1, "ldind.i1", 0x46) +OP(LDIND_U1, "ldind.u1", 0x47) +OP(LDIND_I2, "ldind.i2", 0x48) +OP(LDIND_U2, "ldind.u2", 0x49) +OP(LDIND_I4, "ldind.i4", 0x4a) +OP(LDIND_U4, "ldind.u4", 0x4b) +OP(LDIND_I8, "ldind.i8", 0x4c) +OP(LDIND_I, "ldind.i", 0x4d) +OP(LDIND_R4, "ldind.r4", 0x4e) +OP(LDIND_R8, "ldind.r8", 0x4f) +OP(LDIND_REF, "ldind.ref", 0x50) +OP(STIND_REF, "stind.ref", 0x51) +OP(STIND_I1, "stind.i1", 0x52) +OP(STIND_I2, "stind.i2", 0x53) +OP(STIND_I4, "stind.i4", 0x54) +OP(STIND_I8, "stind.i8", 0x55) +OP(STIND_R4, "stind.r4", 0x56) +OP(STIND_R8, "stind.r8", 0x57) +OP(ADD, "add", 0x58) +OP(SUB, "sub", 0x59) +OP(MUL, "mul", 0x5a) +OP(DIV, "div", 0x5b) +OP(DIV_UN, "div.un", 0x5c) +OP(REM, "rem", 0x5d) +OP(REM_UN, "rem.un", 0x5e) +OP(AND, "and", 0x5f) +OP(OR, "or", 0x60) +OP(XOR, "xor", 0x61) +OP(SHL, "shl", 0x62) +OP(SHR, "shr", 0x63) +OP(SHR_UN, "shr.un", 0x64) +OP(NEG, "neg", 0x65) +OP(NOT, "not", 0x66) +OP(CONV_I1, "conv.i1", 0x67) +OP(CONV_I2, "conv.i2", 0x68) +OP(CONV_I4, "conv.i4", 0x69) +OP(CONV_I8, "conv.i8", 0x6a) +OP(CONV_R4, "conv.r4", 0x6b) +OP(CONV_R8, "conv.r8", 0x6c) +OP(CONV_U4, "conv.u4", 0x6d) +OP(CONV_U8, "conv.u8", 0x6e) +OP(CALLVIRT, "callvirt", 0x6f) +OP(CPOBJ, "cpobj", 0x70) +OP(LDOBJ, "ldobj", 0x71) +OP(LDSTR, "ldstr", 0x72) +OP(NEWOBJ, "newobj", 0x73) +OP(CASTCLASS, "castclass", 0x74) +OP(ISINST, "isinst", 0x75) +OP(CONV_R_UN, "conv.r.un", 0x76) +OP(ANN_DATA_S, "ann.data.s", 0x77) +OP(UNBOX, "unbox", 0x79) +OP(THROW, "throw", 0x7a) +OP(LDFLD, "ldfld", 0x7b) +OP(LDFLDA, "ldflda", 0x7c) +OP(STFLD, "stfld", 0x7d) +OP(LDSFLD, "ldsfld", 0x7e) +OP(LDSFLDA, "ldsflda", 0x7f) +OP(STSFLD, "stsfld", 0x80) +OP(STOBJ, "stobj", 0x81) +OP(CONV_OVF_I1_UN, "conv.ovf.i1.un", 0x82) +OP(CONV_OVF_I2_UN, "conv.ovf.i2.un", 0x83) +OP(CONV_OVF_I4_UN, "conv.ovf.i4.un", 0x84) +OP(CONV_OVF_I8_UN, "conv.ovf.i8.un", 0x85) +OP(CONV_OVF_U1_UN, "conv.ovf.u1.un", 0x86) +OP(CONV_OVF_U2_UN, "conv.ovf.u2.un", 0x87) +OP(CONV_OVF_U4_UN, "conv.ovf.u4.un", 0x88) +OP(CONV_OVF_U8_UN, "conv.ovf.u8.un", 0x89) +OP(CONV_OVF_I_UN, "conv.ovf.i.un", 0x8a) +OP(CONV_OVF_U_UN, "conv.ovf.u.un", 0x8b) +OP(BOX, "box", 0x8c) +OP(NEWARR, "newarr", 0x8d) +OP(LDLEN, "ldlen", 0x8e) +OP(LDELEMA, "ldelema", 0x8f) +OP(LDELEM_I1, "ldelem.i1", 0x90) +OP(LDELEM_U1, "ldelem.u1", 0x91) +OP(LDELEM_I2, "ldelem.i2", 0x92) +OP(LDELEM_U2, "ldelem.u2", 0x93) +OP(LDELEM_I4, "ldelem.i4", 0x94) +OP(LDELEM_U4, "ldelem.u4", 0x95) +OP(LDELEM_I8, "ldelem.i8", 0x96) +OP(LDELEM_I, "ldelem.i", 0x97) +OP(LDELEM_R4, "ldelem.r4", 0x98) +OP(LDELEM_R8, "ldelem.r8", 0x99) +OP(LDELEM_REF, "ldelem.ref", 0x9a) +OP(STELEM_I, "stelem.i", 0x9b) +OP(STELEM_I1, "stelem.i1", 0x9c) +OP(STELEM_I2, "stelem.i2", 0x9d) +OP(STELEM_I4, "stelem.i4", 0x9e) +OP(STELEM_I8, "stelem.i8", 0x9f) +OP(STELEM_R4, "stelem.r4", 0xa0) +OP(STELEM_R8, "stelem.r8", 0xa1) +OP(STELEM_REF, "stelem.ref", 0xa2) +OP(CONV_OVF_I1, "conv.ovf.i1", 0xb3) +OP(CONV_OVF_U1, "conv.ovf.u1", 0xb4) +OP(CONV_OVF_I2, "conv.ovf.i2", 0xb5) +OP(CONV_OVF_U2, "conv.ovf.u2", 0xb6) +OP(CONV_OVF_I4, "conv.ovf.i4", 0xb7) +OP(CONV_OVF_U4, "conv.ovf.u4", 0xb8) +OP(CONV_OVF_I8, "conv.ovf.i8", 0xb9) +OP(CONV_OVF_U8, "conv.ovf.u8", 0xba) +OP(REFANYVAL, "refanyval", 0xc2) +OP(CKFINITE, "ckfinite", 0xc3) +OP(MKREFANY, "mkrefany", 0xc6) +OP(ANN_CALL, "ann.call", 0xc7) +OP(ANN_CATCH, "ann.catch", 0xc8) +OP(ANN_DEAD, "ann.dead", 0xc9) +OP(ANN_HOISTED, "ann.hoisted", 0xca) +OP(ANN_HOISTED_CALL, "ann.hoisted.call", 0xcb) +OP(ANN_LAB, "ann.lab", 0xcc) +OP(ANN_DEF, "ann.def", 0xcd) +OP(ANN_REF_S, "ann.ref.s", 0xce) +OP(ANN_PHI, "ann.phi", 0xcf) +OP(LDTOKEN, "ldtoken", 0xd0) +OP(CONV_U2, "conv.u2", 0xd1) +OP(CONV_U1, "conv.u1", 0xd2) +OP(CONV_I, "conv.i", 0xd3) +OP(CONV_OVF_I, "conv.ovf.i", 0xd4) +OP(CONV_OVF_U, "conv.ovf.u", 0xd5) +OP(ADD_OVF, "add.ovf", 0xd6) +OP(ADD_OVF_UN, "add.ovf.un", 0xd7) +OP(MUL_OVF, "mul.ovf", 0xd8) +OP(MUL_OVF_UN, "mul.ovf.un", 0xd9) +OP(SUB_OVF, "sub.ovf", 0xda) +OP(SUB_OVF_UN, "sub.ovf.un", 0xdb) +OP(ENDFINALLY, "endfinally", 0xdc) +OP(LEAVE, "leave", 0xdd) +OP(LEAVE_S, "leave.s", 0xde) +OP(STIND_I, "stind.i", 0xdf) +OP(CONV_U, "conv.u", 0xe0) + +/* prefix instructions. we use an opcode >= 256 to ease coding */ + +OP(ARGLIST, "arglist", 0x100) +OP(CEQ, "ceq", 0x101) +OP(CGT, "cgt", 0x102) +OP(CGT_UN, "cgt.un", 0x103) +OP(CLT, "clt", 0x104) +OP(CLT_UN, "clt.un", 0x105) +OP(LDFTN, "ldftn", 0x106) +OP(LDVIRTFTN, "ldvirtftn", 0x107) +OP(JMPI, "jmpi", 0x108) +OP(LDARG, "ldarg", 0x109) +OP(LDARGA, "ldarga", 0x10a) +OP(STARG, "starg", 0x10b) +OP(LDLOC, "ldloc", 0x10c) +OP(LDLOCA, "ldloca", 0x10d) +OP(STLOC, "stloc", 0x10e) +OP(LOCALLOC, "localloc", 0x10f) +OP(ENDFILTER, "endfilter", 0x111) +OP(UNALIGNED, "unaligned", 0x112) +OP(VOLATILE, "volatile", 0x113) +OP(TAIL, "tail", 0x114) +OP(INITOBJ, "initobj", 0x115) +OP(ANN_LIVE, "ann.live", 0x116) +OP(CPBLK, "cpblk", 0x117) +OP(INITBLK, "initblk", 0x118) +OP(ANN_REF, "ann.ref", 0x119) +OP(RETHROW, "rethrow", 0x11a) +OP(SIZEOF, "sizeof", 0x11c) +OP(REFANYTYPE, "refanytype", 0x11d) +OP(ANN_DATA, "ann.data", 0x122) +OP(ANN_ARG, "ann.arg", 0x123) diff --git a/05/tcc-final/include/float.h b/05/tcc-final/include/float.h new file mode 100644 index 0000000..24b7410 --- /dev/null +++ b/05/tcc-final/include/float.h @@ -0,0 +1,75 @@ +#ifndef _FLOAT_H_ +#define _FLOAT_H_ + +#define FLT_RADIX 2 + +/* IEEE float */ +#define FLT_MANT_DIG 24 +#define FLT_DIG 6 +#define FLT_ROUNDS 1 +#define FLT_EPSILON 1.19209290e-07F +#define FLT_MIN_EXP (-125) +#define FLT_MIN 1.17549435e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_EXP 128 +#define FLT_MAX 3.40282347e+38F +#define FLT_MAX_10_EXP 38 + +/* IEEE double */ +#define DBL_MANT_DIG 53 +#define DBL_DIG 15 +#define DBL_EPSILON 2.2204460492503131e-16 +#define DBL_MIN_EXP (-1021) +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_EXP 1024 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_MAX_10_EXP 308 + +/* horrible intel long double */ +#if defined __i386__ || defined __x86_64__ + +#define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define LDBL_EPSILON 1.08420217248550443401e-19L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176502e+4932L +#define LDBL_MAX_10_EXP 4932 +#define DECIMAL_DIG 21 + +#elif defined __aarch64__ || defined __riscv +/* + * Use values from: + * gcc -dM -E -xc /dev/null | grep LDBL | sed -e "s/__//g" + */ +#define LDBL_MANT_DIG 113 +#define LDBL_DIG 33 +#define LDBL_EPSILON 1.92592994438723585305597794258492732e-34L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626267781732175260e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176508575932662800702e+4932L +#define LDBL_MAX_EXP 16384 +#define DECIMAL_DIG 36 + +#else + +/* same as IEEE double */ +#define LDBL_MANT_DIG 53 +#define LDBL_DIG 15 +#define LDBL_EPSILON 2.2204460492503131e-16L +#define LDBL_MIN_EXP (-1021) +#define LDBL_MIN 2.2250738585072014e-308L +#define LDBL_MIN_10_EXP (-307) +#define LDBL_MAX_EXP 1024 +#define LDBL_MAX 1.7976931348623157e+308L +#define LDBL_MAX_10_EXP 308 +#define DECIMAL_DIG 17 + +#endif + +#endif /* _FLOAT_H_ */ diff --git a/05/tcc-final/include/stdalign.h b/05/tcc-final/include/stdalign.h new file mode 100644 index 0000000..ae46c34 --- /dev/null +++ b/05/tcc-final/include/stdalign.h @@ -0,0 +1,16 @@ +#ifndef _STDALIGN_H +#define _STDALIGN_H + +#if __STDC_VERSION__ < 201112L && (defined(__GNUC__) || defined(__TINYC__)) +# define _Alignas(t) __attribute__((__aligned__(t))) +# define _Alignof(t) __alignof__(t) +#endif + +#define alignas _Alignas +#define alignof _Alignof + +#define __alignas_is_defined 1 +#define __alignof_is_defined 1 + +#endif /* _STDALIGN_H */ + diff --git a/05/tcc-final/include/stdarg.h b/05/tcc-final/include/stdarg.h new file mode 100644 index 0000000..aa784da --- /dev/null +++ b/05/tcc-final/include/stdarg.h @@ -0,0 +1,14 @@ +#ifndef _STDARG_H +#define _STDARG_H + +typedef __builtin_va_list va_list; +#define va_start __builtin_va_start +#define va_arg __builtin_va_arg +#define va_copy __builtin_va_copy +#define va_end __builtin_va_end + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED + +#endif /* _STDARG_H */ diff --git a/05/tcc-final/include/stdatomic.h b/05/tcc-final/include/stdatomic.h new file mode 100644 index 0000000..f00c62f --- /dev/null +++ b/05/tcc-final/include/stdatomic.h @@ -0,0 +1,137 @@ +/* This file is derived from clang's stdatomic.h */ + +/*===---- stdatomic.h - Standard header for atomic types and operations -----=== + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * + *===-----------------------------------------------------------------------=== + */ + +#ifndef _STDATOMIC_H +#define _STDATOMIC_H + +#include +#include + +#define __ATOMIC_RELAXED 0 +#define __ATOMIC_CONSUME 1 +#define __ATOMIC_ACQUIRE 2 +#define __ATOMIC_RELEASE 3 +#define __ATOMIC_ACQ_REL 4 +#define __ATOMIC_SEQ_CST 5 + +/* Memory ordering */ +typedef enum { + memory_order_relaxed = __ATOMIC_RELAXED, + memory_order_consume = __ATOMIC_CONSUME, + memory_order_acquire = __ATOMIC_ACQUIRE, + memory_order_release = __ATOMIC_RELEASE, + memory_order_acq_rel = __ATOMIC_ACQ_REL, + memory_order_seq_cst = __ATOMIC_SEQ_CST, +} memory_order; + +/* Atomic typedefs */ +typedef _Atomic(_Bool) atomic_bool; +typedef _Atomic(char) atomic_char; +typedef _Atomic(signed char) atomic_schar; +typedef _Atomic(unsigned char) atomic_uchar; +typedef _Atomic(short) atomic_short; +typedef _Atomic(unsigned short) atomic_ushort; +typedef _Atomic(int) atomic_int; +typedef _Atomic(unsigned int) atomic_uint; +typedef _Atomic(long) atomic_long; +typedef _Atomic(unsigned long) atomic_ulong; +typedef _Atomic(long long) atomic_llong; +typedef _Atomic(unsigned long long) atomic_ullong; +typedef _Atomic(uint_least16_t) atomic_char16_t; +typedef _Atomic(uint_least32_t) atomic_char32_t; +typedef _Atomic(wchar_t) atomic_wchar_t; +typedef _Atomic(int_least8_t) atomic_int_least8_t; +typedef _Atomic(uint_least8_t) atomic_uint_least8_t; +typedef _Atomic(int_least16_t) atomic_int_least16_t; +typedef _Atomic(uint_least16_t) atomic_uint_least16_t; +typedef _Atomic(int_least32_t) atomic_int_least32_t; +typedef _Atomic(uint_least32_t) atomic_uint_least32_t; +typedef _Atomic(int_least64_t) atomic_int_least64_t; +typedef _Atomic(uint_least64_t) atomic_uint_least64_t; +typedef _Atomic(int_fast8_t) atomic_int_fast8_t; +typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t; +typedef _Atomic(int_fast16_t) atomic_int_fast16_t; +typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t; +typedef _Atomic(int_fast32_t) atomic_int_fast32_t; +typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t; +typedef _Atomic(int_fast64_t) atomic_int_fast64_t; +typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t; +typedef _Atomic(intptr_t) atomic_intptr_t; +typedef _Atomic(uintptr_t) atomic_uintptr_t; +typedef _Atomic(size_t) atomic_size_t; +typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t; +typedef _Atomic(intmax_t) atomic_intmax_t; +typedef _Atomic(uintmax_t) atomic_uintmax_t; + +/* Atomic flag */ +typedef struct { + atomic_bool value; +} atomic_flag; + +#define ATOMIC_FLAG_INIT {0} + +#define atomic_flag_test_and_set(object) \ + __atomic_exchange(&(object)->value, 1, __ATOMIC_SEQ_CST) +#define atomic_flag_test_and_set_explicit(object, order) \ + __atomic_exchange(&(object)->value, 1, order) + +#define atomic_flag_clear(object) \ + __atomic_store(&(object)->value, 0, __ATOMIC_SEQ_CST) +#define atomic_flag_clear_explicit(object, order) \ + __atomic_store(&(object)->value, 0, order) + +/* Generic routines */ +#define atomic_init(object, desired) \ + __atomic_store(object, desired, __ATOMIC_RELAXED) + +#define atomic_store(object, desired) \ + __atomic_store(object, desired, __ATOMIC_SEQ_CST) +#define atomic_store_explicit __atomic_store + +#define atomic_load(object) \ + __atomic_load(object, __ATOMIC_SEQ_CST) +#define atomic_load_explicit __atomic_load + +#define atomic_exchange(object, desired) \ + __atomic_exchange(object, desired, __ATOMIC_SEQ_CST) +#define atomic_exchange_explicit __atomic_exchange + +#define atomic_compare_exchange_strong(object, expected, desired) \ + __atomic_compare_exchange(object, expected, desired, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) +#define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \ + __atomic_compare_exchange(object, expected, desired, 0, success, failure) + +#define atomic_compare_exchange_weak(object, expected, desired) \ + __atomic_compare_exchange(object, expected, desired, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) +#define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \ + __atomic_compare_exchange(object, expected, desired, 1, success, failure) + +#define atomic_fetch_add(object, operand) \ + __atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_add_explicit __atomic_fetch_add + +#define atomic_fetch_sub(object, operand) \ + __atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_explicit __atomic_fetch_sub + +#define atomic_fetch_or(object, operand) \ + __atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_or_explicit __atomic_fetch_or + +#define atomic_fetch_xor(object, operand) \ + __atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_explicit __atomic_fetch_xor + +#define atomic_fetch_and(object, operand) \ + __atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_and_explicit __atomic_fetch_and + +#endif /* _STDATOMIC_H */ diff --git a/05/tcc-final/include/stdbool.h b/05/tcc-final/include/stdbool.h new file mode 100644 index 0000000..d2ee446 --- /dev/null +++ b/05/tcc-final/include/stdbool.h @@ -0,0 +1,11 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +/* ISOC99 boolean */ + +#define bool _Bool +#define true 1 +#define false 0 +#define __bool_true_false_are_defined 1 + +#endif /* _STDBOOL_H */ diff --git a/05/tcc-final/include/stddef.h b/05/tcc-final/include/stddef.h new file mode 100644 index 0000000..da9b9e0 --- /dev/null +++ b/05/tcc-final/include/stddef.h @@ -0,0 +1,41 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +typedef __SIZE_TYPE__ size_t; +typedef __PTRDIFF_TYPE__ ssize_t; +typedef __WCHAR_TYPE__ wchar_t; +typedef __PTRDIFF_TYPE__ ptrdiff_t; +typedef __PTRDIFF_TYPE__ intptr_t; +typedef __SIZE_TYPE__ uintptr_t; + +#if __STDC_VERSION__ >= 201112L +typedef union { long long __ll; long double __ld; } max_align_t; +#endif + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#undef offsetof +#define offsetof(type, field) ((size_t)&((type *)0)->field) + +#if defined __i386__ || defined __x86_64__ +void *alloca(size_t size); +#endif + +#endif + +/* Older glibc require a wint_t from (when requested + by __need_wint_t, as otherwise stddef.h isn't allowed to + define this type). Note that this must be outside the normal + _STDDEF_H guard, so that it works even when we've included the file + already (without requiring wint_t). Some other libs define _WINT_T + if they've already provided that type, so we can use that as guard. + TCC defines __WINT_TYPE__ for us. */ +#if defined (__need_wint_t) +#ifndef _WINT_T +#define _WINT_T +typedef __WINT_TYPE__ wint_t; +#endif +#undef __need_wint_t +#endif diff --git a/05/tcc-final/include/stdnoreturn.h b/05/tcc-final/include/stdnoreturn.h new file mode 100644 index 0000000..4d580ea --- /dev/null +++ b/05/tcc-final/include/stdnoreturn.h @@ -0,0 +1,7 @@ +#ifndef _STDNORETURN_H +#define _STDNORETURN_H + +/* ISOC11 noreturn */ +#define noreturn _Noreturn + +#endif /* _STDNORETURN_H */ diff --git a/05/tcc-final/include/tccdefs.h b/05/tcc-final/include/tccdefs.h new file mode 100644 index 0000000..83e3c38 --- /dev/null +++ b/05/tcc-final/include/tccdefs.h @@ -0,0 +1,296 @@ +/* tccdefs.h + + Nothing is defined before this file except target machine, target os + and the few things related to option settings in tccpp.c:tcc_predefs(). + + This file is either included at runtime as is, or converted and + included as C-strings at compile-time (depending on CONFIG_TCC_PREDEFS). + + Note that line indent matters: + + - in lines starting at column 1, platform macros are replaced by + corresponding TCC target compile-time macros. See conftest.c for + the list of platform macros supported in lines starting at column 1. + + - only lines indented >= 4 are actually included into the executable, + check tccdefs_.h. +*/ + +#if __SIZEOF_POINTER__ == 4 + /* 32bit systems. */ +#if defined TARGETOS_OpenBSD + #define __SIZE_TYPE__ unsigned long + #define __PTRDIFF_TYPE__ long +#else + #define __SIZE_TYPE__ unsigned int + #define __PTRDIFF_TYPE__ int +#endif + #define __ILP32__ 1 + #define __INT64_TYPE__ long long +#elif __SIZEOF_LONG__ == 4 + /* 64bit Windows. */ + #define __SIZE_TYPE__ unsigned long long + #define __PTRDIFF_TYPE__ long long + #define __LLP64__ 1 + #define __INT64_TYPE__ long long +#else + /* Other 64bit systems. */ + #define __SIZE_TYPE__ unsigned long + #define __PTRDIFF_TYPE__ long + #define __LP64__ 1 +# if defined __linux__ + #define __INT64_TYPE__ long +# else /* APPLE, BSD */ + #define __INT64_TYPE__ long long +# endif +#endif + #define __SIZEOF_INT__ 4 + #define __INT_MAX__ 0x7fffffff +#if __SIZEOF_LONG__ == 4 + #define __LONG_MAX__ 0x7fffffffL +#else + #define __LONG_MAX__ 0x7fffffffffffffffL +#endif + #define __SIZEOF_LONG_LONG__ 8 + #define __LONG_LONG_MAX__ 0x7fffffffffffffffLL + #define __CHAR_BIT__ 8 + #define __ORDER_LITTLE_ENDIAN__ 1234 + #define __ORDER_BIG_ENDIAN__ 4321 + #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#if defined _WIN32 + #define __WCHAR_TYPE__ unsigned short + #define __WINT_TYPE__ unsigned short +#elif defined __linux__ + #define __WCHAR_TYPE__ int + #define __WINT_TYPE__ unsigned int +#else + #define __WCHAR_TYPE__ int + #define __WINT_TYPE__ int +#endif + + #if __STDC_VERSION__ == 201112L + # define __STDC_NO_ATOMICS__ 1 + # define __STDC_NO_COMPLEX__ 1 + # define __STDC_NO_THREADS__ 1 +#if !defined _WIN32 + # define __STDC_UTF_16__ 1 + # define __STDC_UTF_32__ 1 +#endif + #endif + +#if defined _WIN32 + #define __declspec(x) __attribute__((x)) + #define __cdecl + +#elif defined __FreeBSD__ + #define __GNUC__ 9 + #define __GNUC_MINOR__ 3 + #define __GNUC_PATCHLEVEL__ 0 + #define __GNUC_STDC_INLINE__ 1 + #define __NO_TLS 1 + #define __RUNETYPE_INTERNAL 1 +# if __SIZEOF_POINTER__ == 8 + /* FIXME, __int128_t is used by setjump */ + #define __int128_t struct { unsigned char _dummy[16] __attribute((aligned(16))); } + #define __SIZEOF_SIZE_T__ 8 + #define __SIZEOF_PTRDIFF_T__ 8 +#else + #define __SIZEOF_SIZE_T__ 4 + #define __SIZEOF_PTRDIFF_T__ 4 +# endif + +#elif defined __FreeBSD_kernel__ + +#elif defined __NetBSD__ + #define __GNUC__ 4 + #define __GNUC_MINOR__ 1 + #define __GNUC_PATCHLEVEL__ 0 + #define _Pragma(x) + #define __ELF__ 1 +#if defined __aarch64__ + #define _LOCORE /* avoids usage of __asm */ +#endif + +#elif defined __OpenBSD__ + #define __GNUC__ 4 + #define _ANSI_LIBRARY 1 + +#elif defined __APPLE__ + /* emulate APPLE-GCC to make libc's headerfiles compile: */ + #define __GNUC__ 4 /* darwin emits warning on GCC<4 */ + #define __APPLE_CC__ 1 /* for */ + #define __LITTLE_ENDIAN__ 1 + #define _DONT_USE_CTYPE_INLINE_ 1 + /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */ + #define __FINITE_MATH_ONLY__ 1 + #define _FORTIFY_SOURCE 0 + +#else + /* Linux */ + +#endif + /* Some derived integer types needed to get stdint.h to compile correctly on some platforms */ +#ifndef __NetBSD__ + #define __UINTPTR_TYPE__ unsigned __PTRDIFF_TYPE__ + #define __INTPTR_TYPE__ __PTRDIFF_TYPE__ +#endif + #define __INT32_TYPE__ int + +#if !defined _WIN32 + /* glibc defines */ + #define __REDIRECT(name, proto, alias) name proto __asm__ (#alias) + #define __REDIRECT_NTH(name, proto, alias) name proto __asm__ (#alias) __THROW +#endif + + /* skip __builtin... with -E */ + #ifndef __TCC_PP__ + + #define __builtin_offsetof(type, field) ((__SIZE_TYPE__)&((type*)0)->field) + #define __builtin_extract_return_addr(x) x +#if !defined __linux__ && !defined _WIN32 + /* used by math.h */ + #define __builtin_huge_val() 1e500 + #define __builtin_huge_valf() 1e50f + #define __builtin_huge_vall() 1e5000L +# if defined __APPLE__ + #define __builtin_nanf(ignored_string) __nan() + /* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */ + #define __builtin_flt_rounds() 1 + /* used by _fd_def.h */ + #define __builtin_bzero(p, ignored_size) bzero(p, sizeof(*(p))) +# else + #define __builtin_nanf(ignored_string) (0.0F/0.0F) +# endif +#endif + + /* __builtin_va_list */ +#if defined __x86_64__ +#if !defined _WIN32 + /* GCC compatible definition of va_list. */ + /* This should be in sync with the declaration in our lib/libtcc1.c */ + typedef struct { + unsigned gp_offset, fp_offset; + union { + unsigned overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; + } __builtin_va_list[1]; + + void *__va_arg(__builtin_va_list ap, int arg_type, int size, int align); + #define __builtin_va_start(ap, last) \ + (*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24)) + #define __builtin_va_arg(ap, t) \ + (*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t)))) + #define __builtin_va_copy(dest, src) (*(dest) = *(src)) + +#else /* _WIN64 */ + typedef char *__builtin_va_list; + #define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \ + ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8)) +#endif + +#elif defined __arm__ + typedef char *__builtin_va_list; + #define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) + #define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) + #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)) + #define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) + +#elif defined __aarch64__ + typedef struct { + void *__stack, *__gr_top, *__vr_top; + int __gr_offs, __vr_offs; + } __builtin_va_list; + +#elif defined __riscv + typedef char *__builtin_va_list; + #define __va_reg_size (__riscv_xlen >> 3) + #define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \ + & -(__alignof__(type))) + #define __builtin_va_arg(ap,type) (*(sizeof(type) > (2*__va_reg_size) ? *(type **)((ap += __va_reg_size) - __va_reg_size) : (ap = (va_list)(_tcc_align(ap,type) + (sizeof(type)+__va_reg_size - 1)& -__va_reg_size), (type *)(ap - ((sizeof(type)+ __va_reg_size - 1)& -__va_reg_size))))) + +#else /* __i386__ */ + typedef char *__builtin_va_list; + #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)) + #define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3))) + +#endif + #define __builtin_va_end(ap) (void)(ap) + #ifndef __builtin_va_copy + # define __builtin_va_copy(dest, src) (dest) = (src) + #endif + + /* TCC BBUILTIN AND BOUNDS ALIASES */ + #ifdef __leading_underscore + # define __RENAME(X) __asm__("_"X) + #else + # define __RENAME(X) __asm__(X) + #endif + + #ifdef __BOUNDS_CHECKING_ON + # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME("__bound_"#name); + # define __BOUND(ret,name,params) ret name params __RENAME("__bound_"#name); + #else + # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME(#name); + # define __BOUND(ret,name,params) + #endif +#ifdef _WIN32 + #define __BOTH __BOUND + #define __BUILTIN(ret,name,params) +#else + #define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params) + #define __BUILTIN(ret,name,params) ret __builtin_##name params __RENAME(#name); +#endif + + __BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__)) + __BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__)) + __BOTH(void*, memset, (void *, int, __SIZE_TYPE__)) + __BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__)) + __BOTH(__SIZE_TYPE__, strlen, (const char *)) + __BOTH(char*, strcpy, (char *, const char *)) + __BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__)) + __BOTH(int, strcmp, (const char*, const char*)) + __BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__)) + __BOTH(char*, strcat, (char*, const char*)) + __BOTH(char*, strchr, (const char*, int)) + __BOTH(char*, strdup, (const char*)) +#if defined __ARM_EABI__ + __BOUND(void*,__aeabi_memcpy,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memmove,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memmove4,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memmove8,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memset,(void*,int,__SIZE_TYPE__)) +#endif + +#if defined __linux__ || defined __APPLE__ // HAVE MALLOC_REDIR + #define __MAYBE_REDIR __BUILTIN +#else + #define __MAYBE_REDIR __BOTH +#endif + __MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__)) + __MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__)) + __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__)) + __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__)) + __MAYBE_REDIR(void, free, (void*)) +#if defined __i386__ || defined __x86_64__ + __BOTH(void*, alloca, (__SIZE_TYPE__)) +#else + __BUILTIN(void*, alloca, (__SIZE_TYPE__)) +#endif + __BUILTIN(void, abort, (void)) + __BOUND(void, longjmp, ()) +#if !defined _WIN32 + __BOUND(void*, mmap, ()) + __BOUND(int, munmap, ()) +#endif + #undef __BUILTINBC + #undef __BUILTIN + #undef __BOUND + #undef __BOTH + #undef __MAYBE_REDIR + #undef __RENAME + + #endif /* ndef __TCC_PP__ */ diff --git a/05/tcc-final/include/tgmath.h b/05/tcc-final/include/tgmath.h new file mode 100644 index 0000000..5d3e357 --- /dev/null +++ b/05/tcc-final/include/tgmath.h @@ -0,0 +1,89 @@ +/* + * ISO C Standard: 7.22 Type-generic math + */ + +#ifndef _TGMATH_H +#define _TGMATH_H + +#include + +#ifndef __cplusplus +#define __tgmath_real(x, F) \ + _Generic ((x), float: F##f, long double: F##l, default: F)(x) +#define __tgmath_real_2_1(x, y, F) \ + _Generic ((x), float: F##f, long double: F##l, default: F)(x, y) +#define __tgmath_real_2(x, y, F) \ + _Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y) +#define __tgmath_real_3_2(x, y, z, F) \ + _Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y, z) +#define __tgmath_real_3(x, y, z, F) \ + _Generic ((x)+(y)+(z), float: F##f, long double: F##l, default: F)(x, y, z) + +/* Functions defined in both and (7.22p4) */ +#define acos(z) __tgmath_real(z, acos) +#define asin(z) __tgmath_real(z, asin) +#define atan(z) __tgmath_real(z, atan) +#define acosh(z) __tgmath_real(z, acosh) +#define asinh(z) __tgmath_real(z, asinh) +#define atanh(z) __tgmath_real(z, atanh) +#define cos(z) __tgmath_real(z, cos) +#define sin(z) __tgmath_real(z, sin) +#define tan(z) __tgmath_real(z, tan) +#define cosh(z) __tgmath_real(z, cosh) +#define sinh(z) __tgmath_real(z, sinh) +#define tanh(z) __tgmath_real(z, tanh) +#define exp(z) __tgmath_real(z, exp) +#define log(z) __tgmath_real(z, log) +#define pow(z1,z2) __tgmath_real_2(z1, z2, pow) +#define sqrt(z) __tgmath_real(z, sqrt) +#define fabs(z) __tgmath_real(z, fabs) + +/* Functions defined in only (7.22p5) */ +#define atan2(x,y) __tgmath_real_2(x, y, atan2) +#define cbrt(x) __tgmath_real(x, cbrt) +#define ceil(x) __tgmath_real(x, ceil) +#define copysign(x,y) __tgmath_real_2(x, y, copysign) +#define erf(x) __tgmath_real(x, erf) +#define erfc(x) __tgmath_real(x, erfc) +#define exp2(x) __tgmath_real(x, exp2) +#define expm1(x) __tgmath_real(x, expm1) +#define fdim(x,y) __tgmath_real_2(x, y, fdim) +#define floor(x) __tgmath_real(x, floor) +#define fma(x,y,z) __tgmath_real_3(x, y, z, fma) +#define fmax(x,y) __tgmath_real_2(x, y, fmax) +#define fmin(x,y) __tgmath_real_2(x, y, fmin) +#define fmod(x,y) __tgmath_real_2(x, y, fmod) +#define frexp(x,y) __tgmath_real_2_1(x, y, frexp) +#define hypot(x,y) __tgmath_real_2(x, y, hypot) +#define ilogb(x) __tgmath_real(x, ilogb) +#define ldexp(x,y) __tgmath_real_2_1(x, y, ldexp) +#define lgamma(x) __tgmath_real(x, lgamma) +#define llrint(x) __tgmath_real(x, llrint) +#define llround(x) __tgmath_real(x, llround) +#define log10(x) __tgmath_real(x, log10) +#define log1p(x) __tgmath_real(x, log1p) +#define log2(x) __tgmath_real(x, log2) +#define logb(x) __tgmath_real(x, logb) +#define lrint(x) __tgmath_real(x, lrint) +#define lround(x) __tgmath_real(x, lround) +#define nearbyint(x) __tgmath_real(x, nearbyint) +#define nextafter(x,y) __tgmath_real_2(x, y, nextafter) +#define nexttoward(x,y) __tgmath_real_2(x, y, nexttoward) +#define remainder(x,y) __tgmath_real_2(x, y, remainder) +#define remquo(x,y,z) __tgmath_real_3_2(x, y, z, remquo) +#define rint(x) __tgmath_real(x, rint) +#define round(x) __tgmath_real(x, round) +#define scalbln(x,y) __tgmath_real_2_1(x, y, scalbln) +#define scalbn(x,y) __tgmath_real_2_1(x, y, scalbn) +#define tgamma(x) __tgmath_real(x, tgamma) +#define trunc(x) __tgmath_real(x, trunc) + +/* Functions defined in only (7.22p6) +#define carg(z) __tgmath_cplx_only(z, carg) +#define cimag(z) __tgmath_cplx_only(z, cimag) +#define conj(z) __tgmath_cplx_only(z, conj) +#define cproj(z) __tgmath_cplx_only(z, cproj) +#define creal(z) __tgmath_cplx_only(z, creal) +*/ +#endif /* __cplusplus */ +#endif /* _TGMATH_H */ diff --git a/05/tcc-final/include/varargs.h b/05/tcc-final/include/varargs.h new file mode 100644 index 0000000..d614366 --- /dev/null +++ b/05/tcc-final/include/varargs.h @@ -0,0 +1,12 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _VARARGS_H +#define _VARARGS_H + +#error "TinyCC no longer implements ." +#error "Revise your code to use ." + +#endif diff --git a/05/tcc-final/lib/Makefile b/05/tcc-final/lib/Makefile new file mode 100644 index 0000000..74722b6 --- /dev/null +++ b/05/tcc-final/lib/Makefile @@ -0,0 +1,94 @@ +# +# Tiny C Compiler Makefile for libtcc1.a +# + +TOP = .. +include $(TOP)/Makefile +VPATH = $(TOPSRC)/lib $(TOPSRC)/win32/lib +T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown) +X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-) + +XTCC ?= $(TOP)/$(X)tcc$(EXESUF) +XCC = $(XTCC) +XAR = $(XTCC) -ar +XFLAGS-unx = -B$(TOPSRC) +XFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include +XFLAGS = $(XFLAGS$(XCFG)) -I$(TOP) +XCFG = $(or $(findstring -win,$T),-unx) +S = $(if $(findstring yes,$(SILENT)),@$(info * $@)) + +# in order to use gcc, type: make -libtcc1-usegcc=yes +arm-libtcc1-usegcc ?= no + +# This makes bounds checking 40%..60% faster. +#x86_64-libtcc1-usegcc=yes +#i386-libtcc1-usegcc=yes + +ifeq "$($(T)-libtcc1-usegcc)" "yes" + XCC = $(CC) + XAR = $(AR) + XFLAGS = $(CFLAGS) -fPIC -gdwarf -fno-omit-frame-pointer -Wno-unused-function -Wno-unused-variable +endif + +ifneq ($(CONFIG_backtrace),no) +# only for native compiler +ifneq ($(CONFIG_bcheck),no) +$(X)BCHECK_O = bcheck.o +endif +$(X)BT_O = bt-exe.o bt-log.o +$(X)B_O = $(BCHECK_O) bt-exe.o bt-log.o bt-dll.o +endif +$(X)BT_O += tcov.o + +DSO_O = dsohandle.o + +I386_O = libtcc1.o alloca.o alloca-bt.o $(BT_O) stdatomic.o +X86_64_O = libtcc1.o alloca.o alloca-bt.o $(BT_O) stdatomic.o +ARM_O = libtcc1.o armeabi.o alloca.o armflush.o fetch_and_add.o $(BT_O) +ARM64_O = lib-arm64.o fetch_and_add.o $(BT_O) +RISCV64_O = lib-arm64.o fetch_and_add.o $(BT_O) +WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o + +OBJ-i386 = $(I386_O) $(BCHECK_O) $(DSO_O) +OBJ-x86_64 = $(X86_64_O) va_list.o $(BCHECK_O) $(DSO_O) +OBJ-x86_64-osx = $(X86_64_O) va_list.o $(BCHECK_O) +OBJ-i386-win32 = $(I386_O) chkstk.o $(B_O) $(WIN_O) +OBJ-x86_64-win32 = $(X86_64_O) chkstk.o $(B_O) $(WIN_O) +OBJ-arm64 = $(ARM64_O) $(BCHECK_O) $(DSO_O) +OBJ-arm64-osx = $(ARM64_O) va_list.o +OBJ-arm = $(ARM_O) $(BCHECK_O) $(DSO_O) +OBJ-arm-fpa = $(ARM_O) $(DSO_O) +OBJ-arm-fpa-ld = $(ARM_O) $(DSO_O) +OBJ-arm-vfp = $(ARM_O) $(DSO_O) +OBJ-arm-eabi = $(ARM_O) $(DSO_O) +OBJ-arm-eabihf = $(ARM_O) $(DSO_O) +OBJ-arm-wince = $(ARM_O) $(WIN_O) +OBJ-riscv64 = $(RISCV64_O) $(BCHECK_O) $(DSO_O) + +OBJ-extra = $(filter $(B_O),$(OBJ-$T)) +OBJ-libtcc1 = $(addprefix $(X),$(filter-out $(OBJ-extra),$(OBJ-$T))) + +ALL = $(addprefix $(TOP)/,$(X)libtcc1.a $(OBJ-extra)) + +all: $(ALL) + +$(TOP)/$(X)libtcc1.a : $(OBJ-libtcc1) + $S$(XAR) rcs $@ $^ + +$(X)%.o : %.c + $S$(XCC) -c $< -o $@ $(XFLAGS) + +$(X)%.o : %.S + $S$(XCC) -c $< -o $@ $(XFLAGS) + +$(TOP)/%.o : %.c + $S$(XCC) -c $< -o $@ $(XFLAGS) + +$(TOP)/bcheck.o : XFLAGS += -g $(if $(CONFIG_musl),-DTCC_MUSL) +$(TOP)/bt-exe.o : $(TOP)/tccrun.c + +$(X)crt1w.o : crt1.c +$(X)wincrt1w.o : wincrt1.c + +clean : + rm -f *.a *.o $(ALL) diff --git a/05/tcc-final/lib/alloca-bt.S b/05/tcc-final/lib/alloca-bt.S new file mode 100644 index 0000000..c161488 --- /dev/null +++ b/05/tcc-final/lib/alloca-bt.S @@ -0,0 +1,96 @@ +/* ---------------------------------------------- */ +/* alloca-bt.S */ + +#ifdef __leading_underscore +# define _(s) _##s +#else +# define _(s) s +#endif + +/* ---------------------------------------------- */ +#if defined __i386__ + +.globl _(__bound_alloca) +_(__bound_alloca): + pop %edx + pop %eax + mov %eax, %ecx + add $3+1,%eax + and $-4,%eax + jz p6 + +#ifdef _WIN32 +p4: + cmp $4096,%eax + jbe p5 + test %eax,-4096(%esp) + sub $4096,%esp + sub $4096,%eax + jmp p4 + +p5: +#endif + + sub %eax,%esp + mov %esp,%eax + + push %edx + push %eax + push %ecx + push %eax + call _(__bound_new_region) + add $8, %esp + pop %eax + pop %edx + +p6: + push %edx + push %edx + ret + +/* ---------------------------------------------- */ +#elif defined __x86_64__ + +.globl _(__bound_alloca) +_(__bound_alloca): +#ifdef _WIN32 + inc %rcx # add one extra to separate regions + jmp _(alloca) +.globl _(__bound_alloca_nr) +_(__bound_alloca_nr): + dec %rcx + push %rax + mov %rcx,%rdx + mov %rax,%rcx + sub $32,%rsp + call _(__bound_new_region) + add $32,%rsp + pop %rax + ret +#else + pop %rdx + mov %rdi,%rax + mov %rax,%rsi # size, a second parm to the __bound_new_region + + add $15 + 1,%rax # add one extra to separate regions + and $-16,%rax + jz p3 + + + sub %rax,%rsp + mov %rsp,%rdi # pointer, a first parm to the __bound_new_region + mov %rsp,%rax + + push %rdx + push %rax + call _(__bound_new_region) + pop %rax + pop %rdx + +p3: + push %rdx + ret +#endif + +/* ---------------------------------------------- */ +#endif diff --git a/05/tcc-final/lib/alloca.S b/05/tcc-final/lib/alloca.S new file mode 100644 index 0000000..6ebafd7 --- /dev/null +++ b/05/tcc-final/lib/alloca.S @@ -0,0 +1,85 @@ +/* ---------------------------------------------- */ +/* alloca.S */ + +#ifdef __leading_underscore +# define _(s) _##s +#else +# define _(s) s +#endif + +/* ---------------------------------------------- */ +#if defined __i386__ + +.globl _(alloca), _(__alloca) +_(alloca): +_(__alloca): + push %ebp + mov %esp,%ebp + mov 8(%ebp),%eax + add $3,%eax + and $-4,%eax +#ifdef _WIN32 + jmp .+16 #p2 +p1: + sub $4096,%esp + sub $4096,%eax + test %eax,(%esp) +p2: + cmp $4096,%eax + jae p1 +#endif + sub %eax,%esp + mov 4(%ebp),%eax + mov 0(%ebp),%ebp + add $8,%esp + push %eax + lea 8(%esp),%eax + ret + +/* ---------------------------------------------- */ +#elif defined __x86_64__ + +.globl _(alloca) +_(alloca): + pop %rdx +#ifdef _WIN32 + mov %rcx,%rax +#else + mov %rdi,%rax +#endif + add $15,%rax + and $-16,%rax + jz p3 + +#ifdef _WIN32 +p1: + cmp $4096,%rax + jbe p2 + test %rax,-4096(%rsp) + sub $4096,%rsp + sub $4096,%rax + jmp p1 +p2: +#endif + sub %rax,%rsp + mov %rsp,%rax +p3: + push %rdx + ret + +/* ---------------------------------------------- */ +#elif defined __arm__ + + .text + .align 2 + .global alloca + .type alloca, %function +alloca: + rsb sp, r0, sp + bic sp, sp, #7 + mov r0, sp + mov pc, lr + .size alloca, .-alloca + +/* ---------------------------------------------- */ +#endif diff --git a/05/tcc-final/lib/armeabi.c b/05/tcc-final/lib/armeabi.c new file mode 100644 index 0000000..1bccbd3 --- /dev/null +++ b/05/tcc-final/lib/armeabi.c @@ -0,0 +1,537 @@ +/* TCC ARM runtime EABI + Copyright (C) 2013 Thomas Preud'homme + +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 __TINYC__ +#define INT_MIN (-2147483647 - 1) +#define INT_MAX 2147483647 +#define UINT_MAX 0xffffffff +#define LONG_MIN (-2147483647L - 1) +#define LONG_MAX 2147483647L +#define ULONG_MAX 0xffffffffUL +#define LLONG_MAX 9223372036854775807LL +#define LLONG_MIN (-9223372036854775807LL - 1) +#define ULLONG_MAX 0xffffffffffffffffULL +#else +#include +#endif + +/* We rely on the little endianness and EABI calling convention for this to + work */ + +typedef struct double_unsigned_struct { + unsigned low; + unsigned high; +} double_unsigned_struct; + +typedef struct unsigned_int_struct { + unsigned low; + int high; +} unsigned_int_struct; + +#define REGS_RETURN(name, type) \ + void name ## _return(type ret) {} + + +/* Float helper functions */ + +#define FLOAT_EXP_BITS 8 +#define FLOAT_FRAC_BITS 23 + +#define DOUBLE_EXP_BITS 11 +#define DOUBLE_FRAC_BITS 52 + +#define ONE_EXP(type) ((1 << (type ## _EXP_BITS - 1)) - 1) + +REGS_RETURN(unsigned_int_struct, unsigned_int_struct) +REGS_RETURN(double_unsigned_struct, double_unsigned_struct) + +/* float -> integer: (sign) 1.fraction x 2^(exponent - exp_for_one) */ + + +/* float to [unsigned] long long conversion */ +#define DEFINE__AEABI_F2XLZ(name, with_sign) \ +void __aeabi_ ## name(unsigned val) \ +{ \ + int exp, high_shift, sign; \ + double_unsigned_struct ret; \ + \ + /* compute sign */ \ + sign = val >> 31; \ + \ + /* compute real exponent */ \ + exp = val >> FLOAT_FRAC_BITS; \ + exp &= (1 << FLOAT_EXP_BITS) - 1; \ + exp -= ONE_EXP(FLOAT); \ + \ + /* undefined behavior if truncated value cannot be represented */ \ + if (with_sign) { \ + if (exp > 62) /* |val| too big, double cannot represent LLONG_MAX */ \ + return; \ + } else { \ + if ((sign && exp >= 0) || exp > 63) /* if val < 0 || val too big */ \ + return; \ + } \ + \ + val &= (1 << FLOAT_FRAC_BITS) - 1; \ + if (exp >= 32) { \ + ret.high = 1 << (exp - 32); \ + if (exp - 32 >= FLOAT_FRAC_BITS) { \ + ret.high |= val << (exp - 32 - FLOAT_FRAC_BITS); \ + ret.low = 0; \ + } else { \ + high_shift = FLOAT_FRAC_BITS - (exp - 32); \ + ret.high |= val >> high_shift; \ + ret.low = val << (32 - high_shift); \ + } \ + } else { \ + ret.high = 0; \ + ret.low = 1 << exp; \ + if (exp > FLOAT_FRAC_BITS) \ + ret.low |= val << (exp - FLOAT_FRAC_BITS); \ + else \ + ret.low |= val >> (FLOAT_FRAC_BITS - exp); \ + } \ + \ + /* encode negative integer using 2's complement */ \ + if (with_sign && sign) { \ + ret.low = ~ret.low; \ + ret.high = ~ret.high; \ + if (ret.low == UINT_MAX) { \ + ret.low = 0; \ + ret.high++; \ + } else \ + ret.low++; \ + } \ + \ + double_unsigned_struct_return(ret); \ +} + +/* float to unsigned long long conversion */ +DEFINE__AEABI_F2XLZ(f2ulz, 0) + +/* float to long long conversion */ +DEFINE__AEABI_F2XLZ(f2lz, 1) + +/* double to [unsigned] long long conversion */ +#define DEFINE__AEABI_D2XLZ(name, with_sign) \ +void __aeabi_ ## name(double_unsigned_struct val) \ +{ \ + int exp, high_shift, sign; \ + double_unsigned_struct ret; \ + \ + /* compute sign */ \ + sign = val.high >> 31; \ + \ + /* compute real exponent */ \ + exp = (val.high >> (DOUBLE_FRAC_BITS - 32)); \ + exp &= (1 << DOUBLE_EXP_BITS) - 1; \ + exp -= ONE_EXP(DOUBLE); \ + \ + /* undefined behavior if truncated value cannot be represented */ \ + if (with_sign) { \ + if (exp > 62) /* |val| too big, double cannot represent LLONG_MAX */ \ + return; \ + } else { \ + if ((sign && exp >= 0) || exp > 63) /* if val < 0 || val too big */ \ + return; \ + } \ + \ + val.high &= (1 << (DOUBLE_FRAC_BITS - 32)) - 1; \ + if (exp >= 32) { \ + ret.high = 1 << (exp - 32); \ + if (exp >= DOUBLE_FRAC_BITS) { \ + high_shift = exp - DOUBLE_FRAC_BITS; \ + ret.high |= val.high << high_shift; \ + ret.high |= val.low >> (32 - high_shift); \ + ret.low = val.low << high_shift; \ + } else { \ + high_shift = DOUBLE_FRAC_BITS - exp; \ + ret.high |= val.high >> high_shift; \ + ret.low = val.high << (32 - high_shift); \ + ret.low |= val.low >> high_shift; \ + } \ + } else { \ + ret.high = 0; \ + ret.low = 1 << exp; \ + if (exp > DOUBLE_FRAC_BITS - 32) { \ + high_shift = exp - DOUBLE_FRAC_BITS - 32; \ + ret.low |= val.high << high_shift; \ + ret.low |= val.low >> (32 - high_shift); \ + } else \ + ret.low |= val.high >> (DOUBLE_FRAC_BITS - 32 - exp); \ + } \ + \ + /* encode negative integer using 2's complement */ \ + if (with_sign && sign) { \ + ret.low = ~ret.low; \ + ret.high = ~ret.high; \ + if (ret.low == UINT_MAX) { \ + ret.low = 0; \ + ret.high++; \ + } else \ + ret.low++; \ + } \ + \ + double_unsigned_struct_return(ret); \ +} + +/* double to unsigned long long conversion */ +DEFINE__AEABI_D2XLZ(d2ulz, 0) + +/* double to long long conversion */ +DEFINE__AEABI_D2XLZ(d2lz, 1) + +/* long long to float conversion */ +#define DEFINE__AEABI_XL2F(name, with_sign) \ +unsigned __aeabi_ ## name(unsigned long long v) \ +{ \ + int s /* shift */, flb /* first lost bit */, sign = 0; \ + unsigned p = 0 /* power */, ret; \ + double_unsigned_struct val; \ + \ + /* fraction in negative float is encoded in 1's complement */ \ + if (with_sign && (v & (1ULL << 63))) { \ + sign = 1; \ + v = ~v + 1; \ + } \ + val.low = v; \ + val.high = v >> 32; \ + /* fill fraction bits */ \ + for (s = 31, p = 1 << 31; p && !(val.high & p); s--, p >>= 1); \ + if (p) { \ + ret = val.high & (p - 1); \ + if (s < FLOAT_FRAC_BITS) { \ + ret <<= FLOAT_FRAC_BITS - s; \ + ret |= val.low >> (32 - (FLOAT_FRAC_BITS - s)); \ + flb = (val.low >> (32 - (FLOAT_FRAC_BITS - s - 1))) & 1; \ + } else { \ + flb = (ret >> (s - FLOAT_FRAC_BITS - 1)) & 1; \ + ret >>= s - FLOAT_FRAC_BITS; \ + } \ + s += 32; \ + } else { \ + for (s = 31, p = 1 << 31; p && !(val.low & p); s--, p >>= 1); \ + if (p) { \ + ret = val.low & (p - 1); \ + if (s <= FLOAT_FRAC_BITS) { \ + ret <<= FLOAT_FRAC_BITS - s; \ + flb = 0; \ + } else { \ + flb = (ret >> (s - FLOAT_FRAC_BITS - 1)) & 1; \ + ret >>= s - FLOAT_FRAC_BITS; \ + } \ + } else \ + return 0; \ + } \ + if (flb) \ + ret++; \ + \ + /* fill exponent bits */ \ + ret |= (s + ONE_EXP(FLOAT)) << FLOAT_FRAC_BITS; \ + \ + /* fill sign bit */ \ + ret |= sign << 31; \ + \ + return ret; \ +} + +/* unsigned long long to float conversion */ +DEFINE__AEABI_XL2F(ul2f, 0) + +/* long long to float conversion */ +DEFINE__AEABI_XL2F(l2f, 1) + +/* long long to double conversion */ +#define __AEABI_XL2D(name, with_sign) \ +void __aeabi_ ## name(unsigned long long v) \ +{ \ + int s /* shift */, high_shift, sign = 0; \ + unsigned tmp, p = 0; \ + double_unsigned_struct val, ret; \ + \ + /* fraction in negative float is encoded in 1's complement */ \ + if (with_sign && (v & (1ULL << 63))) { \ + sign = 1; \ + v = ~v + 1; \ + } \ + val.low = v; \ + val.high = v >> 32; \ + \ + /* fill fraction bits */ \ + for (s = 31, p = 1 << 31; p && !(val.high & p); s--, p >>= 1); \ + if (p) { \ + tmp = val.high & (p - 1); \ + if (s < DOUBLE_FRAC_BITS - 32) { \ + high_shift = DOUBLE_FRAC_BITS - 32 - s; \ + ret.high = tmp << high_shift; \ + ret.high |= val.low >> (32 - high_shift); \ + ret.low = val.low << high_shift; \ + } else { \ + high_shift = s - (DOUBLE_FRAC_BITS - 32); \ + ret.high = tmp >> high_shift; \ + ret.low = tmp << (32 - high_shift); \ + ret.low |= val.low >> high_shift; \ + if ((val.low >> (high_shift - 1)) & 1) { \ + if (ret.low == UINT_MAX) { \ + ret.high++; \ + ret.low = 0; \ + } else \ + ret.low++; \ + } \ + } \ + s += 32; \ + } else { \ + for (s = 31, p = 1 << 31; p && !(val.low & p); s--, p >>= 1); \ + if (p) { \ + tmp = val.low & (p - 1); \ + if (s <= DOUBLE_FRAC_BITS - 32) { \ + high_shift = DOUBLE_FRAC_BITS - 32 - s; \ + ret.high = tmp << high_shift; \ + ret.low = 0; \ + } else { \ + high_shift = s - (DOUBLE_FRAC_BITS - 32); \ + ret.high = tmp >> high_shift; \ + ret.low = tmp << (32 - high_shift); \ + } \ + } else { \ + ret.high = ret.low = 0; \ + double_unsigned_struct_return(ret); \ + } \ + } \ + \ + /* fill exponent bits */ \ + ret.high |= (s + ONE_EXP(DOUBLE)) << (DOUBLE_FRAC_BITS - 32); \ + \ + /* fill sign bit */ \ + ret.high |= sign << 31; \ + \ + double_unsigned_struct_return(ret); \ +} + +/* unsigned long long to double conversion */ +__AEABI_XL2D(ul2d, 0) + +/* long long to double conversion */ +__AEABI_XL2D(l2d, 1) + + +/* Long long helper functions */ + +/* TODO: add error in case of den == 0 (see §4.3.1 and §4.3.2) */ + +#define define_aeabi_xdivmod_signed_type(basetype, type) \ +typedef struct type { \ + basetype quot; \ + unsigned basetype rem; \ +} type + +#define define_aeabi_xdivmod_unsigned_type(basetype, type) \ +typedef struct type { \ + basetype quot; \ + basetype rem; \ +} type + +#define AEABI_UXDIVMOD(name,type, rettype, typemacro) \ +static inline rettype aeabi_ ## name (type num, type den) \ +{ \ + rettype ret; \ + type quot = 0; \ + \ + /* Increase quotient while it is less than numerator */ \ + while (num >= den) { \ + type q = 1; \ + \ + /* Find closest power of two */ \ + while ((q << 1) * den <= num && q * den <= typemacro ## _MAX / 2) \ + q <<= 1; \ + \ + /* Compute difference between current quotient and numerator */ \ + num -= q * den; \ + quot += q; \ + } \ + ret.quot = quot; \ + ret.rem = num; \ + return ret; \ +} + +#define __AEABI_XDIVMOD(name, type, uiname, rettype, urettype, typemacro) \ +void __aeabi_ ## name(type numerator, type denominator) \ +{ \ + unsigned type num, den; \ + urettype uxdiv_ret; \ + rettype ret; \ + \ + if (numerator >= 0) \ + num = numerator; \ + else \ + num = 0 - numerator; \ + if (denominator >= 0) \ + den = denominator; \ + else \ + den = 0 - denominator; \ + uxdiv_ret = aeabi_ ## uiname(num, den); \ + /* signs differ */ \ + if ((numerator & typemacro ## _MIN) != (denominator & typemacro ## _MIN)) \ + ret.quot = 0 - uxdiv_ret.quot; \ + else \ + ret.quot = uxdiv_ret.quot; \ + if (numerator < 0) \ + ret.rem = 0 - uxdiv_ret.rem; \ + else \ + ret.rem = uxdiv_ret.rem; \ + \ + rettype ## _return(ret); \ +} + +define_aeabi_xdivmod_signed_type(long long, lldiv_t); +define_aeabi_xdivmod_unsigned_type(unsigned long long, ulldiv_t); +define_aeabi_xdivmod_signed_type(int, idiv_t); +define_aeabi_xdivmod_unsigned_type(unsigned, uidiv_t); + +REGS_RETURN(lldiv_t, lldiv_t) +REGS_RETURN(ulldiv_t, ulldiv_t) +REGS_RETURN(idiv_t, idiv_t) +REGS_RETURN(uidiv_t, uidiv_t) + +AEABI_UXDIVMOD(uldivmod, unsigned long long, ulldiv_t, ULLONG) + +__AEABI_XDIVMOD(ldivmod, long long, uldivmod, lldiv_t, ulldiv_t, LLONG) + +void __aeabi_uldivmod(unsigned long long num, unsigned long long den) +{ + ulldiv_t_return(aeabi_uldivmod(num, den)); +} + +void __aeabi_llsl(double_unsigned_struct val, int shift) +{ + double_unsigned_struct ret; + + if (shift >= 32) { + val.high = val.low; + val.low = 0; + shift -= 32; + } + if (shift > 0) { + ret.low = val.low << shift; + ret.high = (val.high << shift) | (val.low >> (32 - shift)); + double_unsigned_struct_return(ret); + return; + } + double_unsigned_struct_return(val); +} + +#define aeabi_lsr(val, shift, fill, type) \ + type ## _struct ret; \ + \ + if (shift >= 32) { \ + val.low = val.high; \ + val.high = fill; \ + shift -= 32; \ + } \ + if (shift > 0) { \ + ret.high = val.high >> shift; \ + ret.low = (val.high << (32 - shift)) | (val.low >> shift); \ + type ## _struct_return(ret); \ + return; \ + } \ + type ## _struct_return(val); + +void __aeabi_llsr(double_unsigned_struct val, int shift) +{ + aeabi_lsr(val, shift, 0, double_unsigned); +} + +void __aeabi_lasr(unsigned_int_struct val, int shift) +{ + aeabi_lsr(val, shift, val.high >> 31, unsigned_int); +} + + +/* Integer division functions */ + +AEABI_UXDIVMOD(uidivmod, unsigned, uidiv_t, UINT) + +int __aeabi_idiv(int numerator, int denominator) +{ + unsigned num, den; + uidiv_t ret; + + if (numerator >= 0) + num = numerator; + else + num = 0 - numerator; + if (denominator >= 0) + den = denominator; + else + den = 0 - denominator; + ret = aeabi_uidivmod(num, den); + if ((numerator & INT_MIN) != (denominator & INT_MIN)) /* signs differ */ + ret.quot *= -1; + return ret.quot; +} + +unsigned __aeabi_uidiv(unsigned num, unsigned den) +{ + return aeabi_uidivmod(num, den).quot; +} + +__AEABI_XDIVMOD(idivmod, int, uidivmod, idiv_t, uidiv_t, INT) + +void __aeabi_uidivmod(unsigned num, unsigned den) +{ + uidiv_t_return(aeabi_uidivmod(num, den)); +} + +/* Some targets do not have all eabi calls (OpenBSD) */ +typedef __SIZE_TYPE__ size_t; +extern void *memcpy(void *dest, const void *src, size_t n); +extern void *memmove(void *dest, const void *src, size_t n); +extern void *memset(void *s, int c, size_t n); + +void * +__aeabi_memcpy (void *dest, const void *src, size_t n) +{ + return memcpy (dest, src, n); +} + +void * +__aeabi_memmove (void *dest, const void *src, size_t n) +{ + return memmove (dest, src, n); +} + +void * +__aeabi_memmove4 (void *dest, const void *src, size_t n) +{ + return memmove (dest, src, n); +} + +void * +__aeabi_memmove8 (void *dest, const void *src, size_t n) +{ + return memmove (dest, src, n); +} + +void * +__aeabi_memset (void *s, size_t n, int c) +{ + return memset (s, c, n); +} diff --git a/05/tcc-final/lib/armflush.c b/05/tcc-final/lib/armflush.c new file mode 100644 index 0000000..c379e43 --- /dev/null +++ b/05/tcc-final/lib/armflush.c @@ -0,0 +1,51 @@ +/* armflush.c - flush the instruction cache + + __clear_cache is used in tccrun.c, It is a built-in + intrinsic with gcc. However tcc in order to compile + itself needs this function */ + +#ifdef __TINYC__ + +/* syscall wrapper */ +unsigned _tccsyscall(unsigned syscall_nr, ...); + +/* arm-tcc supports only fake asm currently */ +__asm__( + ".global _tccsyscall\n" + "_tccsyscall:\n" + "push {r7, lr}\n\t" + "mov r7, r0\n\t" + "mov r0, r1\n\t" + "mov r1, r2\n\t" + "mov r2, r3\n\t" + "svc #0\n\t" + "pop {r7, pc}" + ); + +/* from unistd.h: */ +#if defined(__thumb__) || defined(__ARM_EABI__) +# define __NR_SYSCALL_BASE 0x0 +#else +# define __NR_SYSCALL_BASE 0x900000 +#endif +#define __ARM_NR_BASE (__NR_SYSCALL_BASE+0x0f0000) +#define __ARM_NR_cacheflush (__ARM_NR_BASE+2) + +#define syscall _tccsyscall + +#else + +#define _GNU_SOURCE +#include +#include +#include + +#endif + +/* Flushing for tccrun */ +void __clear_cache(void *beginning, void *end) +{ +/* __ARM_NR_cacheflush is kernel private and should not be used in user space. + * However, there is no ARM asm parser in tcc so we use it for now */ + syscall(__ARM_NR_cacheflush, beginning, end, 0); +} diff --git a/05/tcc-final/lib/bcheck.c b/05/tcc-final/lib/bcheck.c new file mode 100644 index 0000000..729f0b5 --- /dev/null +++ b/05/tcc-final/lib/bcheck.c @@ -0,0 +1,2226 @@ +/* + * Tiny C Memory and bounds checker + * + * Copyright (c) 2002 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include +#include +#include +#include + +#if !defined(__FreeBSD__) \ + && !defined(__FreeBSD_kernel__) \ + && !defined(__DragonFly__) \ + && !defined(__OpenBSD__) \ + && !defined(__APPLE__) \ + && !defined(__NetBSD__) +#include +#endif + +#if !defined(_WIN32) +#include +#include +#endif + +#define BOUND_DEBUG (1) +#define BOUND_STATISTIC (1) + +#if BOUND_DEBUG + #define dprintf(a...) if (print_calls) fprintf(a) +#else + #define dprintf(a...) +#endif + +#ifdef __attribute__ + /* an __attribute__ macro is defined in the system headers */ + #undef __attribute__ +#endif +#define FASTCALL __attribute__((regparm(3))) + +#ifdef _WIN32 +# define DLL_EXPORT __declspec(dllexport) +#else +# define DLL_EXPORT +#endif + +#if defined(__FreeBSD__) \ + || defined(__FreeBSD_kernel__) \ + || defined(__DragonFly__) \ + || defined(__OpenBSD__) \ + || defined(__NetBSD__) \ + || defined(__dietlibc__) + +#include +#define INIT_SEM() +#define EXIT_SEM() +#define WAIT_SEM() +#define POST_SEM() +#define TRY_SEM() +#define HAVE_MEMALIGN (0) +#define MALLOC_REDIR (0) +#define HAVE_PTHREAD_CREATE (0) +#define HAVE_CTYPE (0) +#define HAVE_ERRNO (0) +#define HAVE_SIGNAL (0) +#define HAVE_SIGACTION (0) +#define HAVE_FORK (0) +#define HAVE_TLS_FUNC (0) +#define HAVE_TLS_VAR (0) + +#elif defined(_WIN32) + +#include +#include +static CRITICAL_SECTION bounds_sem; +#define INIT_SEM() InitializeCriticalSection(&bounds_sem) +#define EXIT_SEM() DeleteCriticalSection(&bounds_sem) +#define WAIT_SEM() EnterCriticalSection(&bounds_sem) +#define POST_SEM() LeaveCriticalSection(&bounds_sem) +#define TRY_SEM() TryEnterCriticalSection(&bounds_sem) +#define HAVE_MEMALIGN (0) +#define MALLOC_REDIR (0) +#define HAVE_PTHREAD_CREATE (0) +#define HAVE_CTYPE (0) +#define HAVE_ERRNO (0) +#define HAVE_SIGNAL (1) +#define HAVE_SIGACTION (0) +#define HAVE_FORK (0) +#define HAVE_TLS_FUNC (1) +#define HAVE_TLS_VAR (0) + +#else + +#define __USE_GNU /* get RTLD_NEXT */ +#include +#include +#include +#include +#include +#include +#ifdef __APPLE__ +#include +static dispatch_semaphore_t bounds_sem; +#define INIT_SEM() bounds_sem = dispatch_semaphore_create(1) +#define EXIT_SEM() dispatch_release(*(dispatch_object_t*)&bounds_sem) +#define WAIT_SEM() if (use_sem) dispatch_semaphore_wait(bounds_sem, DISPATCH_TIME_FOREVER) +#define POST_SEM() if (use_sem) dispatch_semaphore_signal(bounds_sem) +#define TRY_SEM() if (use_sem) dispatch_semaphore_wait(bounds_sem, DISPATCH_TIME_NOW) +#elif 0 +#include +static sem_t bounds_sem; +#define INIT_SEM() sem_init (&bounds_sem, 0, 1) +#define EXIT_SEM() sem_destroy (&bounds_sem) +#define WAIT_SEM() if (use_sem) while (sem_wait (&bounds_sem) < 0 \ + && errno == EINTR) +#define POST_SEM() if (use_sem) sem_post (&bounds_sem) +#define TRY_SEM() if (use_sem) while (sem_trywait (&bounds_sem) < 0 \ + && errno == EINTR) +#elif 0 +static pthread_mutex_t bounds_mtx; +#define INIT_SEM() pthread_mutex_init (&bounds_mtx, NULL) +#define EXIT_SEM() pthread_mutex_destroy (&bounds_mtx) +#define WAIT_SEM() if (use_sem) pthread_mutex_lock (&bounds_mtx) +#define POST_SEM() if (use_sem) pthread_mutex_unlock (&bounds_mtx) +#define TRY_SEM() if (use_sem) pthread_mutex_trylock (&bounds_mtx) +#else +static pthread_spinlock_t bounds_spin; +/* about 25% faster then semaphore. */ +#define INIT_SEM() pthread_spin_init (&bounds_spin, 0) +#define EXIT_SEM() pthread_spin_destroy (&bounds_spin) +#define WAIT_SEM() if (use_sem) pthread_spin_lock (&bounds_spin) +#define POST_SEM() if (use_sem) pthread_spin_unlock (&bounds_spin) +#define TRY_SEM() if (use_sem) pthread_spin_trylock (&bounds_spin) +#endif +#define HAVE_MEMALIGN (1) +#define MALLOC_REDIR (1) +#define HAVE_PTHREAD_CREATE (1) +#define HAVE_CTYPE (1) +#define HAVE_ERRNO (1) +#define HAVE_SIGNAL (1) +#define HAVE_SIGACTION (1) +#define HAVE_FORK (1) +#if !defined(__APPLE__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) +#define HAVE_TLS_FUNC (0) +#define HAVE_TLS_VAR (1) +#else +#define HAVE_TLS_FUNC (1) +#define HAVE_TLS_VAR (0) +#endif +#ifdef TCC_MUSL +# undef HAVE_CTYPE +#endif +#endif + +#if MALLOC_REDIR +static void *(*malloc_redir) (size_t); +static void *(*calloc_redir) (size_t, size_t); +static void (*free_redir) (void *); +static void *(*realloc_redir) (void *, size_t); +static unsigned int pool_index; +static unsigned char __attribute__((aligned(16))) initial_pool[256]; +#endif +#if HAVE_MEMALIGN +static void *(*memalign_redir) (size_t, size_t); +#endif +#if HAVE_PTHREAD_CREATE +static int (*pthread_create_redir) (pthread_t *thread, + const pthread_attr_t *attr, + void *(*start_routine)(void *), void *arg); +#endif +#if HAVE_SIGNAL +typedef void (*bound_sig)(int); +static bound_sig (*signal_redir) (int signum, bound_sig handler); +#endif +#if HAVE_SIGACTION +static int (*sigaction_redir) (int signum, const struct sigaction *act, + struct sigaction *oldact); +#endif +#if HAVE_FORK +static int (*fork_redir) (void); +#endif + +#define TCC_TYPE_NONE (0) +#define TCC_TYPE_MALLOC (1) +#define TCC_TYPE_CALLOC (2) +#define TCC_TYPE_REALLOC (3) +#define TCC_TYPE_MEMALIGN (4) +#define TCC_TYPE_STRDUP (5) + +/* this pointer is generated when bound check is incorrect */ +#define INVALID_POINTER ((void *)(-2)) + +typedef struct tree_node Tree; +struct tree_node { + Tree * left, * right; + size_t start; + size_t size; + unsigned char type; + unsigned char is_invalid; /* true if pointers outside region are invalid */ +}; + +typedef struct alloca_list_struct { + size_t fp; + void *p; + size_t size; + struct alloca_list_struct *next; +} alloca_list_type; + +#if defined(_WIN32) +#define BOUND_TID_TYPE DWORD +#define BOUND_GET_TID(id) id = GetCurrentThreadId() +#elif defined(__OpenBSD__) +#define BOUND_TID_TYPE pid_t +#define BOUND_GET_TID(id) id = syscall (SYS_getthrid) +#elif defined(__FreeBSD__) +#define BOUND_TID_TYPE pid_t +#define BOUND_GET_TID(id) syscall (SYS_thr_self, &id) +#elif defined(__NetBSD__) +#define BOUND_TID_TYPE pid_t +#define BOUND_GET_TID(id) id = syscall (SYS__lwp_self) +#elif defined(__linux__) +#define BOUND_TID_TYPE pid_t +#define BOUND_GET_TID(id) id = syscall (SYS_gettid) +#else +#define BOUND_TID_TYPE int +#define BOUND_GET_TID(id) id = 0 +#endif + +typedef struct jmp_list_struct { + void *penv; + size_t fp; + size_t end_fp; + BOUND_TID_TYPE tid; + struct jmp_list_struct *next; +} jmp_list_type; + +#define BOUND_STATISTIC_SPLAY (0) +static Tree * splay (size_t addr, Tree *t); +static Tree * splay_end (size_t addr, Tree *t); +static Tree * splay_insert(size_t addr, size_t size, Tree * t); +static Tree * splay_delete(size_t addr, Tree *t); +void splay_printtree(Tree * t, int d); + +/* external interface */ +void __bounds_checking (int no_check); +void __bound_checking_lock (void); +void __bound_checking_unlock (void); +void __bound_never_fatal (int no_check); +DLL_EXPORT void * __bound_ptr_add(void *p, size_t offset); +DLL_EXPORT void * __bound_ptr_indir1(void *p, size_t offset); +DLL_EXPORT void * __bound_ptr_indir2(void *p, size_t offset); +DLL_EXPORT void * __bound_ptr_indir4(void *p, size_t offset); +DLL_EXPORT void * __bound_ptr_indir8(void *p, size_t offset); +DLL_EXPORT void * __bound_ptr_indir12(void *p, size_t offset); +DLL_EXPORT void * __bound_ptr_indir16(void *p, size_t offset); +DLL_EXPORT void FASTCALL __bound_local_new(void *p1); +DLL_EXPORT void FASTCALL __bound_local_delete(void *p1); +void __bound_init(size_t *, int); +void __bound_main_arg(int argc, char **argv, char **envp); +void __bound_exit(void); +void __bound_exit_dll(size_t *); +#if !defined(_WIN32) +void *__bound_mmap (void *start, size_t size, int prot, int flags, int fd, + off_t offset); +int __bound_munmap (void *start, size_t size); +DLL_EXPORT void __bound_siglongjmp(jmp_buf env, int val); +#endif +DLL_EXPORT void __bound_new_region(void *p, size_t size); +DLL_EXPORT void __bound_setjmp(jmp_buf env); +DLL_EXPORT void __bound_longjmp(jmp_buf env, int val); +DLL_EXPORT void *__bound_memcpy(void *dst, const void *src, size_t size); +DLL_EXPORT int __bound_memcmp(const void *s1, const void *s2, size_t size); +DLL_EXPORT void *__bound_memmove(void *dst, const void *src, size_t size); +DLL_EXPORT void *__bound_memset(void *dst, int c, size_t size); +DLL_EXPORT int __bound_strlen(const char *s); +DLL_EXPORT char *__bound_strcpy(char *dst, const char *src); +DLL_EXPORT char *__bound_strncpy(char *dst, const char *src, size_t n); +DLL_EXPORT int __bound_strcmp(const char *s1, const char *s2); +DLL_EXPORT int __bound_strncmp(const char *s1, const char *s2, size_t n); +DLL_EXPORT char *__bound_strcat(char *dest, const char *src); +DLL_EXPORT char *__bound_strchr(const char *string, int ch); +DLL_EXPORT char *__bound_strdup(const char *s); + +#if defined(__arm__) && defined(__ARM_EABI__) +DLL_EXPORT void *__bound___aeabi_memcpy(void *dst, const void *src, size_t size); +DLL_EXPORT void *__bound___aeabi_memmove(void *dst, const void *src, size_t size); +DLL_EXPORT void *__bound___aeabi_memmove4(void *dst, const void *src, size_t size); +DLL_EXPORT void *__bound___aeabi_memmove8(void *dst, const void *src, size_t size); +DLL_EXPORT void *__bound___aeabi_memset(void *dst, int c, size_t size); +DLL_EXPORT void *__aeabi_memcpy(void *dst, const void *src, size_t size); +DLL_EXPORT void *__aeabi_memmove(void *dst, const void *src, size_t size); +DLL_EXPORT void *__aeabi_memmove4(void *dst, const void *src, size_t size); +DLL_EXPORT void *__aeabi_memmove8(void *dst, const void *src, size_t size); +DLL_EXPORT void *__aeabi_memset(void *dst, int c, size_t size); +#endif + +#if MALLOC_REDIR +#define BOUND_MALLOC(a) malloc_redir(a) +#define BOUND_MEMALIGN(a,b) memalign_redir(a,b) +#define BOUND_FREE(a) free_redir(a) +#define BOUND_REALLOC(a,b) realloc_redir(a,b) +#define BOUND_CALLOC(a,b) calloc_redir(a,b) +#else +#define BOUND_MALLOC(a) malloc(a) +#define BOUND_MEMALIGN(a,b) memalign(a,b) +#define BOUND_FREE(a) free(a) +#define BOUND_REALLOC(a,b) realloc(a,b) +#define BOUND_CALLOC(a,b) calloc(a,b) +DLL_EXPORT void *__bound_malloc(size_t size, const void *caller); +DLL_EXPORT void *__bound_memalign(size_t size, size_t align, const void *caller); +DLL_EXPORT void __bound_free(void *ptr, const void *caller); +DLL_EXPORT void *__bound_realloc(void *ptr, size_t size, const void *caller); +DLL_EXPORT void *__bound_calloc(size_t nmemb, size_t size); +#endif + +#define FREE_REUSE_SIZE (100) +static unsigned int free_reuse_index; +static void *free_reuse_list[FREE_REUSE_SIZE]; + +static Tree *tree = NULL; +#define TREE_REUSE (1) +#if TREE_REUSE +static Tree *tree_free_list; +#endif +static alloca_list_type *alloca_list; +static jmp_list_type *jmp_list; + +static unsigned char inited; +static unsigned char print_warn_ptr_add; +static unsigned char print_calls; +static unsigned char print_heap; +static unsigned char print_statistic; +static unsigned char no_strdup; +static unsigned char use_sem; +static int never_fatal; +#if HAVE_TLS_FUNC +#if defined(_WIN32) +static int no_checking = 0; +static DWORD no_checking_key; +#define NO_CHECKING_CHECK() if (!p) { \ + p = (int *) LocalAlloc(LPTR, sizeof(int)); \ + if (!p) bound_alloc_error("tls malloc"); \ + *p = 0; \ + TlsSetValue(no_checking_key, p); \ + } +#define NO_CHECKING_GET() ({ int *p = TlsGetValue(no_checking_key); \ + NO_CHECKING_CHECK(); \ + *p; \ + }) +#define NO_CHECKING_SET(v) { int *p = TlsGetValue(no_checking_key); \ + NO_CHECKING_CHECK(); \ + *p = v; \ + } +#else +static int no_checking = 0; +static pthread_key_t no_checking_key; +#define NO_CHECKING_CHECK() if (!p) { \ + p = (int *) BOUND_MALLOC(sizeof(int)); \ + if (!p) bound_alloc_error("tls malloc"); \ + *p = 0; \ + pthread_setspecific(no_checking_key, p); \ + } +#define NO_CHECKING_GET() ({ int *p = pthread_getspecific(no_checking_key); \ + NO_CHECKING_CHECK(); \ + *p; \ + }) +#define NO_CHECKING_SET(v) { int *p = pthread_getspecific(no_checking_key); \ + NO_CHECKING_CHECK(); \ + *p = v; \ + } +#endif +#elif HAVE_TLS_VAR +static __thread int no_checking = 0; +#define NO_CHECKING_GET() no_checking +#define NO_CHECKING_SET(v) no_checking = v +#else +static int no_checking = 0; +#define NO_CHECKING_GET() no_checking +#define NO_CHECKING_SET(v) no_checking = v +#endif +static char exec[100]; + +#if BOUND_STATISTIC +static unsigned long long bound_ptr_add_count; +static unsigned long long bound_ptr_indir1_count; +static unsigned long long bound_ptr_indir2_count; +static unsigned long long bound_ptr_indir4_count; +static unsigned long long bound_ptr_indir8_count; +static unsigned long long bound_ptr_indir12_count; +static unsigned long long bound_ptr_indir16_count; +static unsigned long long bound_local_new_count; +static unsigned long long bound_local_delete_count; +static unsigned long long bound_malloc_count; +static unsigned long long bound_calloc_count; +static unsigned long long bound_realloc_count; +static unsigned long long bound_free_count; +static unsigned long long bound_memalign_count; +static unsigned long long bound_mmap_count; +static unsigned long long bound_munmap_count; +static unsigned long long bound_alloca_count; +static unsigned long long bound_setjmp_count; +static unsigned long long bound_longjmp_count; +static unsigned long long bound_mempcy_count; +static unsigned long long bound_memcmp_count; +static unsigned long long bound_memmove_count; +static unsigned long long bound_memset_count; +static unsigned long long bound_strlen_count; +static unsigned long long bound_strcpy_count; +static unsigned long long bound_strncpy_count; +static unsigned long long bound_strcmp_count; +static unsigned long long bound_strncmp_count; +static unsigned long long bound_strcat_count; +static unsigned long long bound_strchr_count; +static unsigned long long bound_strdup_count; +static unsigned long long bound_not_found; +#define INCR_COUNT(x) ++x +#else +#define INCR_COUNT(x) +#endif +#if BOUND_STATISTIC_SPLAY +static unsigned long long bound_splay; +static unsigned long long bound_splay_end; +static unsigned long long bound_splay_insert; +static unsigned long long bound_splay_delete; +#define INCR_COUNT_SPLAY(x) ++x +#else +#define INCR_COUNT_SPLAY(x) +#endif + +int tcc_backtrace(const char *fmt, ...); + +/* print a bound error message */ +#define bound_warning(...) \ + do { \ + WAIT_SEM (); \ + tcc_backtrace("^bcheck.c^BCHECK: " __VA_ARGS__); \ + POST_SEM (); \ + } while (0) + +#define bound_error(...) \ + do { \ + bound_warning(__VA_ARGS__); \ + if (never_fatal == 0) \ + exit(255); \ + } while (0) + +static void bound_alloc_error(const char *s) +{ + fprintf(stderr,"FATAL: %s\n",s); + exit (1); +} + +static void bound_not_found_warning(const char *file, const char *function, + void *ptr) +{ + dprintf(stderr, "%s%s, %s(): Not found %p\n", exec, file, function, ptr); +} + +static void fetch_and_add(int* variable, int value) +{ +#if defined __i386__ || defined __x86_64__ + __asm__ volatile("lock; addl %0, %1" + : "+r" (value), "+m" (*variable) // input+output + : // No input-only + : "memory" + ); +#elif defined __arm__ + extern void fetch_and_add_arm(int* variable, int value); + fetch_and_add_arm(variable, value); +#elif defined __aarch64__ + extern void fetch_and_add_arm64(int* variable, int value); + fetch_and_add_arm64(variable, value); +#elif defined __riscv + extern void fetch_and_add_riscv64(int* variable, int value); + fetch_and_add_riscv64(variable, value); +#else + *variable += value; +#endif +} + +/* enable/disable checking. This can be used in signal handlers. */ +void __bounds_checking (int no_check) +{ +#if HAVE_TLS_FUNC || HAVE_TLS_VAR + NO_CHECKING_SET(NO_CHECKING_GET() + no_check); +#else + fetch_and_add (&no_checking, no_check); +#endif +} + +void __bound_checking_lock(void) +{ + WAIT_SEM (); +} + +void __bound_checking_unlock(void) +{ + POST_SEM (); +} + +/* enable/disable checking. This can be used in signal handlers. */ +void __bound_never_fatal (int neverfatal) +{ + fetch_and_add (&never_fatal, neverfatal); +} + +/* return '(p + offset)' for pointer arithmetic (a pointer can reach + the end of a region in this case */ +void * __bound_ptr_add(void *p, size_t offset) +{ + size_t addr = (size_t)p; + + if (NO_CHECKING_GET()) + return p + offset; + + dprintf(stderr, "%s, %s(): %p 0x%lx\n", + __FILE__, __FUNCTION__, p, (unsigned long)offset); + + WAIT_SEM (); + INCR_COUNT(bound_ptr_add_count); + if (tree) { + addr -= tree->start; + if (addr >= tree->size) { + addr = (size_t)p; + tree = splay (addr, tree); + addr -= tree->start; + } + if (addr >= tree->size) { + addr = (size_t)p; + tree = splay_end (addr, tree); + addr -= tree->start; + } + if (addr <= tree->size) { + if (tree->is_invalid || addr + offset > tree->size) { + POST_SEM (); + if (print_warn_ptr_add) + bound_warning("%p is outside of the region", p + offset); + if (never_fatal <= 0) + return INVALID_POINTER; /* return an invalid pointer */ + return p + offset; + } + } + else if (p) { /* Allow NULL + offset. offsetoff is using it. */ + INCR_COUNT(bound_not_found); + POST_SEM (); + bound_not_found_warning (__FILE__, __FUNCTION__, p); + return p + offset; + } + } + POST_SEM (); + return p + offset; +} + +/* return '(p + offset)' for pointer indirection (the resulting must + be strictly inside the region */ +#define BOUND_PTR_INDIR(dsize) \ +void * __bound_ptr_indir ## dsize (void *p, size_t offset) \ +{ \ + size_t addr = (size_t)p; \ + \ + if (NO_CHECKING_GET()) \ + return p + offset; \ + \ + dprintf(stderr, "%s, %s(): %p 0x%lx\n", \ + __FILE__, __FUNCTION__, p, (unsigned long)offset); \ + WAIT_SEM (); \ + INCR_COUNT(bound_ptr_indir ## dsize ## _count); \ + if (tree) { \ + addr -= tree->start; \ + if (addr >= tree->size) { \ + addr = (size_t)p; \ + tree = splay (addr, tree); \ + addr -= tree->start; \ + } \ + if (addr >= tree->size) { \ + addr = (size_t)p; \ + tree = splay_end (addr, tree); \ + addr -= tree->start; \ + } \ + if (addr <= tree->size) { \ + if (tree->is_invalid || addr + offset + dsize > tree->size) { \ + POST_SEM (); \ + bound_warning("%p is outside of the region", p + offset); \ + if (never_fatal <= 0) \ + return INVALID_POINTER; /* return an invalid pointer */ \ + return p + offset; \ + } \ + } \ + else { \ + INCR_COUNT(bound_not_found); \ + POST_SEM (); \ + bound_not_found_warning (__FILE__, __FUNCTION__, p); \ + return p + offset; \ + } \ + } \ + POST_SEM (); \ + return p + offset; \ +} + +BOUND_PTR_INDIR(1) +BOUND_PTR_INDIR(2) +BOUND_PTR_INDIR(4) +BOUND_PTR_INDIR(8) +BOUND_PTR_INDIR(12) +BOUND_PTR_INDIR(16) + +#if defined(__GNUC__) && (__GNUC__ >= 6) +/* + * At least gcc 6.2 complains when __builtin_frame_address is used with + * nonzero argument. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wframe-address" +#endif + +/* return the frame pointer of the caller */ +#define GET_CALLER_FP(fp)\ +{\ + fp = (size_t)__builtin_frame_address(1);\ +} + +/* called when entering a function to add all the local regions */ +void FASTCALL __bound_local_new(void *p1) +{ + size_t addr, fp, *p = p1; + + if (NO_CHECKING_GET()) + return; + GET_CALLER_FP(fp); + dprintf(stderr, "%s, %s(): p1=%p fp=%p\n", + __FILE__, __FUNCTION__, p, (void *)fp); + WAIT_SEM (); + while ((addr = p[0])) { + INCR_COUNT(bound_local_new_count); + tree = splay_insert(addr + fp, p[1], tree); + p += 2; + } + POST_SEM (); +#if BOUND_DEBUG + if (print_calls) { + p = p1; + while ((addr = p[0])) { + dprintf(stderr, "%s, %s(): %p 0x%lx\n", + __FILE__, __FUNCTION__, + (void *) (addr + fp), (unsigned long) p[1]); + p += 2; + } + } +#endif +} + +/* called when leaving a function to delete all the local regions */ +void FASTCALL __bound_local_delete(void *p1) +{ + size_t addr, fp, *p = p1; + + if (NO_CHECKING_GET()) + return; + GET_CALLER_FP(fp); + dprintf(stderr, "%s, %s(): p1=%p fp=%p\n", + __FILE__, __FUNCTION__, p, (void *)fp); + WAIT_SEM (); + while ((addr = p[0])) { + INCR_COUNT(bound_local_delete_count); + tree = splay_delete(addr + fp, tree); + p += 2; + } + if (alloca_list) { + alloca_list_type *last = NULL; + alloca_list_type *cur = alloca_list; + + do { + if (cur->fp == fp) { + if (last) + last->next = cur->next; + else + alloca_list = cur->next; + tree = splay_delete ((size_t) cur->p, tree); + dprintf(stderr, "%s, %s(): remove alloca/vla %p\n", + __FILE__, __FUNCTION__, cur->p); + BOUND_FREE (cur); + cur = last ? last->next : alloca_list; + } + else { + last = cur; + cur = cur->next; + } + } while (cur); + } + if (jmp_list) { + jmp_list_type *last = NULL; + jmp_list_type *cur = jmp_list; + + do { + if (cur->fp == fp) { + if (last) + last->next = cur->next; + else + jmp_list = cur->next; + dprintf(stderr, "%s, %s(): remove setjmp %p\n", + __FILE__, __FUNCTION__, cur->penv); + BOUND_FREE (cur); + cur = last ? last->next : jmp_list; + } + else { + last = cur; + cur = cur->next; + } + } while (cur); + } + + POST_SEM (); +#if BOUND_DEBUG + if (print_calls) { + p = p1; + while ((addr = p[0])) { + if (addr != 1) { + dprintf(stderr, "%s, %s(): %p 0x%lx\n", + __FILE__, __FUNCTION__, + (void *) (addr + fp), (unsigned long) p[1]); + } + p+= 2; + } + } +#endif +} + +/* used by alloca */ +void __bound_new_region(void *p, size_t size) +{ + size_t fp; + alloca_list_type *last; + alloca_list_type *cur; + alloca_list_type *new; + + if (NO_CHECKING_GET()) + return; + + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, p, (unsigned long)size); + GET_CALLER_FP (fp); + new = BOUND_MALLOC (sizeof (alloca_list_type)); + WAIT_SEM (); + INCR_COUNT(bound_alloca_count); + last = NULL; + cur = alloca_list; + while (cur) { +#if defined(__i386__) || (defined(__arm__) && !defined(__ARM_EABI__)) + int align = 4; +#elif defined(__arm__) + int align = 8; +#else + int align = 16; +#endif + void *cure = (void *)((char *)cur->p + ((cur->size + align) & -align)); + void *pe = (void *)((char *)p + ((size + align) & -align)); + if (cur->fp == fp && ((cur->p <= p && cure > p) || + (p <= cur->p && pe > cur->p))) { + if (last) + last->next = cur->next; + else + alloca_list = cur->next; + tree = splay_delete((size_t)cur->p, tree); + break; + } + last = cur; + cur = cur->next; + } + tree = splay_insert((size_t)p, size, tree); + if (new) { + new->fp = fp; + new->p = p; + new->size = size; + new->next = alloca_list; + alloca_list = new; + } + POST_SEM (); + if (cur) { + dprintf(stderr, "%s, %s(): remove alloca/vla %p\n", + __FILE__, __FUNCTION__, cur->p); + BOUND_FREE (cur); + } +} + +void __bound_setjmp(jmp_buf env) +{ + jmp_list_type *jl; + void *e = (void *) env; + + if (NO_CHECKING_GET() == 0) { + dprintf(stderr, "%s, %s(): %p\n", __FILE__, __FUNCTION__, e); + WAIT_SEM (); + INCR_COUNT(bound_setjmp_count); + jl = jmp_list; + while (jl) { + if (jl->penv == e) + break; + jl = jl->next; + } + if (jl == NULL) { + jl = BOUND_MALLOC (sizeof (jmp_list_type)); + if (jl) { + jl->penv = e; + jl->next = jmp_list; + jmp_list = jl; + } + } + if (jl) { + size_t fp; + + GET_CALLER_FP (fp); + jl->fp = fp; + jl->end_fp = (size_t)__builtin_frame_address(0); + BOUND_GET_TID(jl->tid); + } + POST_SEM (); + } +} + +static void __bound_long_jump(jmp_buf env, int val, int sig, const char *func) +{ + jmp_list_type *jl; + void *e; + BOUND_TID_TYPE tid; + + if (NO_CHECKING_GET() == 0) { + e = (void *)env; + BOUND_GET_TID(tid); + dprintf(stderr, "%s, %s(): %p\n", __FILE__, func, e); + WAIT_SEM(); + INCR_COUNT(bound_longjmp_count); + jl = jmp_list; + while (jl) { + if (jl->penv == e && jl->tid == tid) { + size_t start_fp = (size_t)__builtin_frame_address(0); + size_t end_fp = jl->end_fp; + jmp_list_type *cur = jmp_list; + jmp_list_type *last = NULL; + + while (cur->penv != e || cur->tid != tid) { + if (cur->tid == tid) { + dprintf(stderr, "%s, %s(): remove setjmp %p\n", + __FILE__, func, cur->penv); + if (last) + last->next = cur->next; + else + jmp_list = cur->next; + BOUND_FREE (cur); + cur = last ? last->next : jmp_list; + } + else { + last = cur; + cur = cur->next; + } + } + for (;;) { + Tree *t = tree; + alloca_list_type *last; + alloca_list_type *cur; + + while (t && (t->start < start_fp || t->start > end_fp)) + if (t->start < start_fp) + t = t->right; + else + t = t->left; + if (t == NULL) + break; + last = NULL; + cur = alloca_list; + while (cur) { + if ((size_t) cur->p == t->start) { + dprintf(stderr, "%s, %s(): remove alloca/vla %p\n", + __FILE__, func, cur->p); + if (last) + last->next = cur->next; + else + alloca_list = cur->next; + BOUND_FREE (cur); + break; + } + last = cur; + cur = cur->next; + } + dprintf(stderr, "%s, %s(): delete %p\n", + __FILE__, func, (void *) t->start); + tree = splay_delete(t->start, tree); + } + break; + } + jl = jl->next; + } + POST_SEM(); + } +#if !defined(_WIN32) + sig ? siglongjmp(env, val) : +#endif + longjmp (env, val); +} + +void __bound_longjmp(jmp_buf env, int val) +{ + __bound_long_jump(env,val, 0, __FUNCTION__); +} + +#if !defined(_WIN32) +void __bound_siglongjmp(jmp_buf env, int val) +{ + __bound_long_jump(env,val, 1, __FUNCTION__); +} +#endif + +#if defined(__GNUC__) && (__GNUC__ >= 6) +#pragma GCC diagnostic pop +#endif + +void __bound_init(size_t *p, int mode) +{ + dprintf(stderr, "%s, %s(): start %s\n", __FILE__, __FUNCTION__, + mode < 0 ? "lazy" : mode == 0 ? "normal use" : "for -run"); + + if (inited) { + WAIT_SEM(); + goto add_bounds; + } + inited = 1; + +#if HAVE_TLS_FUNC +#if defined(_WIN32) + no_checking_key = TlsAlloc(); + TlsSetValue(no_checking_key, &no_checking); +#else + pthread_key_create(&no_checking_key, NULL); + pthread_setspecific(no_checking_key, &no_checking); +#endif +#endif + NO_CHECKING_SET(1); + + print_warn_ptr_add = getenv ("TCC_BOUNDS_WARN_POINTER_ADD") != NULL; + print_calls = getenv ("TCC_BOUNDS_PRINT_CALLS") != NULL; + print_heap = getenv ("TCC_BOUNDS_PRINT_HEAP") != NULL; + print_statistic = getenv ("TCC_BOUNDS_PRINT_STATISTIC") != NULL; + never_fatal = getenv ("TCC_BOUNDS_NEVER_FATAL") != NULL; + + INIT_SEM (); + +#if MALLOC_REDIR + { + void *addr = mode > 0 ? RTLD_DEFAULT : RTLD_NEXT; + + /* tcc -run required RTLD_DEFAULT. Normal usage requires RTLD_NEXT, + but using RTLD_NEXT with -run segfaults on MacOS in dyld as the + generated code segment isn't registered with dyld and hence the + caller image of dlsym isn't known to it */ + *(void **) (&malloc_redir) = dlsym (addr, "malloc"); + if (malloc_redir == NULL) { + dprintf(stderr, "%s, %s(): use RTLD_DEFAULT\n", + __FILE__, __FUNCTION__); + addr = RTLD_DEFAULT; + *(void **) (&malloc_redir) = dlsym (addr, "malloc"); + } + *(void **) (&calloc_redir) = dlsym (addr, "calloc"); + *(void **) (&free_redir) = dlsym (addr, "free"); + *(void **) (&realloc_redir) = dlsym (addr, "realloc"); + *(void **) (&memalign_redir) = dlsym (addr, "memalign"); + dprintf(stderr, "%s, %s(): malloc_redir %p\n", + __FILE__, __FUNCTION__, malloc_redir); + dprintf(stderr, "%s, %s(): free_redir %p\n", + __FILE__, __FUNCTION__, free_redir); + dprintf(stderr, "%s, %s(): realloc_redir %p\n", + __FILE__, __FUNCTION__, realloc_redir); + dprintf(stderr, "%s, %s(): memalign_redir %p\n", + __FILE__, __FUNCTION__, memalign_redir); + if (malloc_redir == NULL || free_redir == NULL) + bound_alloc_error ("Cannot redirect malloc/free"); +#if HAVE_PTHREAD_CREATE + *(void **) (&pthread_create_redir) = dlsym (addr, "pthread_create"); + dprintf(stderr, "%s, %s(): pthread_create_redir %p\n", + __FILE__, __FUNCTION__, pthread_create_redir); + if (pthread_create_redir == NULL) + bound_alloc_error ("Cannot redirect pthread_create"); +#endif +#if HAVE_SIGNAL + *(void **) (&signal_redir) = dlsym (addr, "signal"); + dprintf(stderr, "%s, %s(): signal_redir %p\n", + __FILE__, __FUNCTION__, signal_redir); + if (signal_redir == NULL) + bound_alloc_error ("Cannot redirect signal"); +#endif +#if HAVE_SIGACTION + *(void **) (&sigaction_redir) = dlsym (addr, "sigaction"); + dprintf(stderr, "%s, %s(): sigaction_redir %p\n", + __FILE__, __FUNCTION__, sigaction_redir); + if (sigaction_redir == NULL) + bound_alloc_error ("Cannot redirect sigaction"); +#endif +#if HAVE_FORK + *(void **) (&fork_redir) = dlsym (addr, "fork"); + dprintf(stderr, "%s, %s(): fork_redir %p\n", + __FILE__, __FUNCTION__, fork_redir); + if (fork_redir == NULL) + bound_alloc_error ("Cannot redirect fork"); +#endif + } +#endif + +#ifdef __linux__ + { + FILE *fp; + unsigned char found; + unsigned long start; + unsigned long end; + unsigned long ad = + (unsigned long) __builtin_return_address(0); + char line[1000]; + + /* Display exec name. Usefull when a lot of code is compiled with tcc */ + fp = fopen ("/proc/self/comm", "r"); + if (fp) { + memset (exec, 0, sizeof(exec)); + fread (exec, 1, sizeof(exec) - 2, fp); + if (strchr(exec,'\n')) + *strchr(exec,'\n') = '\0'; + strcat (exec, ":"); + fclose (fp); + } + /* check if dlopen is used (is threre a better way?) */ + found = 0; + fp = fopen ("/proc/self/maps", "r"); + if (fp) { + while (fgets (line, sizeof(line), fp)) { + if (sscanf (line, "%lx-%lx", &start, &end) == 2 && + ad >= start && ad < end) { + found = 1; + break; + } + if (strstr (line,"[heap]")) + break; + } + fclose (fp); + } + if (found == 0) { + use_sem = 1; + no_strdup = 1; + } + } +#endif + + WAIT_SEM (); + +#if HAVE_CTYPE +#ifdef __APPLE__ + tree = splay_insert((size_t) &_DefaultRuneLocale, + sizeof (_DefaultRuneLocale), tree); +#else + /* XXX: Does not work if locale is changed */ + tree = splay_insert((size_t) __ctype_b_loc(), + sizeof (unsigned short *), tree); + tree = splay_insert((size_t) (*__ctype_b_loc() - 128), + 384 * sizeof (unsigned short), tree); + tree = splay_insert((size_t) __ctype_tolower_loc(), + sizeof (__int32_t *), tree); + tree = splay_insert((size_t) (*__ctype_tolower_loc() - 128), + 384 * sizeof (__int32_t), tree); + tree = splay_insert((size_t) __ctype_toupper_loc(), + sizeof (__int32_t *), tree); + tree = splay_insert((size_t) (*__ctype_toupper_loc() - 128), + 384 * sizeof (__int32_t), tree); +#endif +#endif +#if HAVE_ERRNO + tree = splay_insert((size_t) (&errno), sizeof (int), tree); +#endif + +add_bounds: + if (!p) + goto no_bounds; + + /* add all static bound check values */ + while (p[0] != 0) { + tree = splay_insert(p[0], p[1], tree); +#if BOUND_DEBUG + if (print_calls) { + dprintf(stderr, "%s, %s(): static var %p 0x%lx\n", + __FILE__, __FUNCTION__, + (void *) p[0], (unsigned long) p[1]); + } +#endif + p += 2; + } +no_bounds: + + POST_SEM (); + NO_CHECKING_SET(0); + dprintf(stderr, "%s, %s(): end\n\n", __FILE__, __FUNCTION__); +} + +void +#if (defined(__GLIBC__) && (__GLIBC_MINOR__ >= 4)) || defined(_WIN32) +__attribute__((constructor)) +#endif +__bound_main_arg(int argc, char **argv, char **envp) +{ + __bound_init (0, -1); + if (argc && argv) { + int i; + + WAIT_SEM (); + for (i = 0; i < argc; i++) + tree = splay_insert((size_t) argv[i], strlen (argv[i]) + 1, tree); + tree = splay_insert((size_t) argv, (argc + 1) * sizeof(char *), tree); + POST_SEM (); +#if BOUND_DEBUG + if (print_calls) { + for (i = 0; i < argc; i++) + dprintf(stderr, "%s, %s(): arg %p 0x%lx\n", + __FILE__, __FUNCTION__, + argv[i], (unsigned long)(strlen (argv[i]) + 1)); + dprintf(stderr, "%s, %s(): argv %p %d\n", + __FILE__, __FUNCTION__, argv, + (int)((argc + 1) * sizeof(char *))); + } +#endif + } + + if (envp && *envp) { + char **p = envp; + + WAIT_SEM (); + while (*p) { + tree = splay_insert((size_t) *p, strlen (*p) + 1, tree); + ++p; + } + tree = splay_insert((size_t) envp, (++p - envp) * sizeof(char *), tree); + POST_SEM (); +#if BOUND_DEBUG + if (print_calls) { + p = envp; + while (*p) { + dprintf(stderr, "%s, %s(): env %p 0x%lx\n", + __FILE__, __FUNCTION__, + *p, (unsigned long)(strlen (*p) + 1)); + ++p; + } + dprintf(stderr, "%s, %s(): environ %p %d\n", + __FILE__, __FUNCTION__, envp, + (int)((++p - envp) * sizeof(char *))); + } +#endif + } +} + +void __attribute__((destructor)) __bound_exit(void) +{ + int i; + static const char * const alloc_type[] = { + "", "malloc", "calloc", "realloc", "memalign", "strdup" + }; + + dprintf(stderr, "%s, %s():\n", __FILE__, __FUNCTION__); + + if (inited) { +#if !defined(_WIN32) && !defined(__APPLE__) && !defined TCC_MUSL && \ + !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__) + if (print_heap) { + extern void __libc_freeres (void); + __libc_freeres (); + } +#endif + + NO_CHECKING_SET(1); + + TRY_SEM (); + while (alloca_list) { + alloca_list_type *next = alloca_list->next; + + tree = splay_delete ((size_t) alloca_list->p, tree); + BOUND_FREE (alloca_list); + alloca_list = next; + } + while (jmp_list) { + jmp_list_type *next = jmp_list->next; + + BOUND_FREE (jmp_list); + jmp_list = next; + } + for (i = 0; i < FREE_REUSE_SIZE; i++) { + if (free_reuse_list[i]) { + tree = splay_delete ((size_t) free_reuse_list[i], tree); + BOUND_FREE (free_reuse_list[i]); + } + } + while (tree) { + if (print_heap && tree->type != 0) + fprintf (stderr, "%s, %s(): %s found size %lu\n", + __FILE__, __FUNCTION__, alloc_type[tree->type], + (unsigned long) tree->size); + tree = splay_delete (tree->start, tree); + } +#if TREE_REUSE + while (tree_free_list) { + Tree *next = tree_free_list->left; + BOUND_FREE (tree_free_list); + tree_free_list = next; + } +#endif + POST_SEM (); + EXIT_SEM (); +#if HAVE_TLS_FUNC +#if defined(_WIN32) + TlsFree(no_checking_key); +#else + pthread_key_delete(no_checking_key); +#endif +#endif + inited = 0; + if (print_statistic) { +#if BOUND_STATISTIC + fprintf (stderr, "bound_ptr_add_count %llu\n", bound_ptr_add_count); + fprintf (stderr, "bound_ptr_indir1_count %llu\n", bound_ptr_indir1_count); + fprintf (stderr, "bound_ptr_indir2_count %llu\n", bound_ptr_indir2_count); + fprintf (stderr, "bound_ptr_indir4_count %llu\n", bound_ptr_indir4_count); + fprintf (stderr, "bound_ptr_indir8_count %llu\n", bound_ptr_indir8_count); + fprintf (stderr, "bound_ptr_indir12_count %llu\n", bound_ptr_indir12_count); + fprintf (stderr, "bound_ptr_indir16_count %llu\n", bound_ptr_indir16_count); + fprintf (stderr, "bound_local_new_count %llu\n", bound_local_new_count); + fprintf (stderr, "bound_local_delete_count %llu\n", bound_local_delete_count); + fprintf (stderr, "bound_malloc_count %llu\n", bound_malloc_count); + fprintf (stderr, "bound_calloc_count %llu\n", bound_calloc_count); + fprintf (stderr, "bound_realloc_count %llu\n", bound_realloc_count); + fprintf (stderr, "bound_free_count %llu\n", bound_free_count); + fprintf (stderr, "bound_memalign_count %llu\n", bound_memalign_count); + fprintf (stderr, "bound_mmap_count %llu\n", bound_mmap_count); + fprintf (stderr, "bound_munmap_count %llu\n", bound_munmap_count); + fprintf (stderr, "bound_alloca_count %llu\n", bound_alloca_count); + fprintf (stderr, "bound_setjmp_count %llu\n", bound_setjmp_count); + fprintf (stderr, "bound_longjmp_count %llu\n", bound_longjmp_count); + fprintf (stderr, "bound_mempcy_count %llu\n", bound_mempcy_count); + fprintf (stderr, "bound_memcmp_count %llu\n", bound_memcmp_count); + fprintf (stderr, "bound_memmove_count %llu\n", bound_memmove_count); + fprintf (stderr, "bound_memset_count %llu\n", bound_memset_count); + fprintf (stderr, "bound_strlen_count %llu\n", bound_strlen_count); + fprintf (stderr, "bound_strcpy_count %llu\n", bound_strcpy_count); + fprintf (stderr, "bound_strncpy_count %llu\n", bound_strncpy_count); + fprintf (stderr, "bound_strcmp_count %llu\n", bound_strcmp_count); + fprintf (stderr, "bound_strncmp_count %llu\n", bound_strncmp_count); + fprintf (stderr, "bound_strcat_count %llu\n", bound_strcat_count); + fprintf (stderr, "bound_strchr_count %llu\n", bound_strchr_count); + fprintf (stderr, "bound_strdup_count %llu\n", bound_strdup_count); + fprintf (stderr, "bound_not_found %llu\n", bound_not_found); +#endif +#if BOUND_STATISTIC_SPLAY + fprintf (stderr, "bound_splay %llu\n", bound_splay); + fprintf (stderr, "bound_splay_end %llu\n", bound_splay_end); + fprintf (stderr, "bound_splay_insert %llu\n", bound_splay_insert); + fprintf (stderr, "bound_splay_delete %llu\n", bound_splay_delete); +#endif + } + } +} + +void __bound_exit_dll(size_t *p) +{ + dprintf(stderr, "%s, %s()\n", __FILE__, __FUNCTION__); + + if (p) { + WAIT_SEM (); + while (p[0] != 0) { + tree = splay_delete(p[0], tree); +#if BOUND_DEBUG + if (print_calls) { + dprintf(stderr, "%s, %s(): remove static var %p 0x%lx\n", + __FILE__, __FUNCTION__, + (void *) p[0], (unsigned long) p[1]); + } +#endif + p += 2; + } + POST_SEM (); + } +} + +#if HAVE_PTHREAD_CREATE +typedef struct { + void *(*start_routine) (void *); + void *arg; + sigset_t old_mask; +} bound_thread_create_type; + +static void *bound_thread_create(void *bdata) +{ + bound_thread_create_type *data = (bound_thread_create_type *) bdata; + void *retval; +#if HAVE_TLS_FUNC + int *p = (int *) BOUND_MALLOC(sizeof(int)); + + if (!p) bound_alloc_error("bound_thread_create malloc"); + *p = 0; + pthread_setspecific(no_checking_key, p); +#endif + pthread_sigmask(SIG_SETMASK, &data->old_mask, NULL); + retval = data->start_routine(data->arg); +#if HAVE_TLS_FUNC + pthread_setspecific(no_checking_key, NULL); + BOUND_FREE (p); +#endif + BOUND_FREE (data); + return retval; +} + +int pthread_create(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine) (void *), void *arg) +{ + int retval; + bound_thread_create_type *data; + sigset_t mask; + sigset_t old_mask; + + use_sem = 1; + dprintf (stderr, "%s, %s()\n", __FILE__, __FUNCTION__); + sigfillset(&mask); + pthread_sigmask(SIG_SETMASK, &mask, &old_mask); + data = (bound_thread_create_type *) BOUND_MALLOC(sizeof(bound_thread_create_type)); + if (!data) bound_alloc_error("bound_thread_create malloc"); + data->start_routine = start_routine; + data->arg = arg; + data->old_mask = old_mask; + retval = pthread_create_redir(thread, attr, bound_thread_create, data); + pthread_sigmask(SIG_SETMASK, &old_mask, NULL); + return retval; +} +#endif + +#if HAVE_SIGNAL || HAVE_SIGACTION +typedef union { +#if HAVE_SIGNAL + bound_sig signal_handler; +#endif +#if HAVE_SIGACTION + void (*sig_handler)(int); + void (*sig_sigaction)(int, siginfo_t *, void *); +#endif +} bound_sig_type; + +static unsigned char bound_sig_used[NSIG]; +static bound_sig_type bound_sig_data[NSIG]; +#endif + +#if HAVE_SIGNAL +static void signal_handler(int sig) +{ + __bounds_checking(1); + bound_sig_data[sig].signal_handler(sig); + __bounds_checking(-1); +} + +bound_sig signal(int signum, bound_sig handler) +{ + bound_sig retval; + + dprintf (stderr, "%s, %s() %d %p\n", __FILE__, __FUNCTION__, + signum, handler); + retval = signal_redir(signum, handler ? signal_handler : handler); + if (retval != SIG_ERR) { + if (bound_sig_used[signum]) + retval = bound_sig_data[signum].signal_handler; + if (handler) { + bound_sig_used[signum] = 1; + bound_sig_data[signum].signal_handler = handler; + } + } + return retval; +} +#endif + +#if HAVE_SIGACTION +static void sig_handler(int sig) +{ + __bounds_checking(1); + bound_sig_data[sig].sig_handler(sig); + __bounds_checking(-1); +} + +static void sig_sigaction(int sig, siginfo_t *info, void *ucontext) +{ + __bounds_checking(1); + bound_sig_data[sig].sig_sigaction(sig, info, ucontext); + __bounds_checking(-1); +} + +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) +{ + int retval; + struct sigaction nact, oact; + + dprintf (stderr, "%s, %s() %d %p %p\n", __FILE__, __FUNCTION__, + signum, act, oldact); + + if (sigaction_redir == NULL) + __bound_init(0,-1); + + if (act) { + nact = *act; + if (nact.sa_flags & SA_SIGINFO) + nact.sa_sigaction = sig_sigaction; + else + nact.sa_handler = sig_handler; + retval = sigaction_redir(signum, &nact, &oact); + } + else + retval = sigaction_redir(signum, act, &oact); + if (retval >= 0) { + if (bound_sig_used[signum]) { + if (oact.sa_flags & SA_SIGINFO) + oact.sa_sigaction = bound_sig_data[signum].sig_sigaction; + else + oact.sa_handler = bound_sig_data[signum].sig_handler; + } + if (oldact) { + *oldact = oact; + } + if (act) { + bound_sig_used[signum] = 1; + if (act->sa_flags & SA_SIGINFO) + bound_sig_data[signum].sig_sigaction = act->sa_sigaction; + else + bound_sig_data[signum].sig_handler = act->sa_handler; + } + } + return retval; +} +#endif + +#if HAVE_FORK +pid_t fork(void) +{ + pid_t retval; + + WAIT_SEM(); + retval = (*fork_redir)(); + if (retval == 0) + INIT_SEM(); + else + POST_SEM(); + return retval; +} +#endif + +#if MALLOC_REDIR +void *malloc(size_t size) +#else +void *__bound_malloc(size_t size, const void *caller) +#endif +{ + void *ptr; + +#if MALLOC_REDIR + /* This will catch the first dlsym call from __bound_init */ + if (malloc_redir == NULL) { + __bound_init (0, -1); + if (malloc_redir == NULL) { + ptr = &initial_pool[pool_index]; + pool_index = (pool_index + size + 15) & ~15; + if (pool_index >= sizeof (initial_pool)) + bound_alloc_error ("initial memory pool too small"); + dprintf (stderr, "%s, %s(): initial %p, 0x%lx\n", + __FILE__, __FUNCTION__, ptr, (unsigned long)size); + return ptr; + } + } +#endif + /* we allocate one more byte to ensure the regions will be + separated by at least one byte. With the glibc malloc, it may + be in fact not necessary */ + ptr = BOUND_MALLOC (size + 1); + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, ptr, (unsigned long)size); + + if (inited && NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_malloc_count); + + if (ptr) { + tree = splay_insert ((size_t) ptr, size ? size : size + 1, tree); + if (tree && tree->start == (size_t) ptr) + tree->type = TCC_TYPE_MALLOC; + } + POST_SEM (); + } + return ptr; +} + +#if MALLOC_REDIR +void *memalign(size_t size, size_t align) +#else +void *__bound_memalign(size_t size, size_t align, const void *caller) +#endif +{ + void *ptr; + +#if HAVE_MEMALIGN + /* we allocate one more byte to ensure the regions will be + separated by at least one byte. With the glibc malloc, it may + be in fact not necessary */ + ptr = BOUND_MEMALIGN(size + 1, align); +#else + if (align > 4) { + /* XXX: handle it ? */ + ptr = NULL; + } else { + /* we suppose that malloc aligns to at least four bytes */ + ptr = BOUND_MALLOC(size + 1); + } +#endif + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, ptr, (unsigned long)size); + + if (NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_memalign_count); + + if (ptr) { + tree = splay_insert((size_t) ptr, size ? size : size + 1, tree); + if (tree && tree->start == (size_t) ptr) + tree->type = TCC_TYPE_MEMALIGN; + } + POST_SEM (); + } + return ptr; +} + +#if MALLOC_REDIR +void free(void *ptr) +#else +void __bound_free(void *ptr, const void *caller) +#endif +{ + size_t addr = (size_t) ptr; + void *p; + + if (ptr == NULL || tree == NULL +#if MALLOC_REDIR + || ((unsigned char *) ptr >= &initial_pool[0] && + (unsigned char *) ptr < &initial_pool[sizeof(initial_pool)]) +#endif + ) + return; + + dprintf(stderr, "%s, %s(): %p\n", __FILE__, __FUNCTION__, ptr); + + if (inited && NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_free_count); + tree = splay (addr, tree); + if (tree->start == addr) { + if (tree->is_invalid) { + POST_SEM (); + bound_error("freeing invalid region"); + return; + } + tree->is_invalid = 1; + memset (ptr, 0x5a, tree->size); + p = free_reuse_list[free_reuse_index]; + free_reuse_list[free_reuse_index] = ptr; + free_reuse_index = (free_reuse_index + 1) % FREE_REUSE_SIZE; + if (p) + tree = splay_delete((size_t)p, tree); + ptr = p; + } + POST_SEM (); + } + BOUND_FREE (ptr); +} + +#if MALLOC_REDIR +void *realloc(void *ptr, size_t size) +#else +void *__bound_realloc(void *ptr, size_t size, const void *caller) +#endif +{ + void *new_ptr; + + if (size == 0) { +#if MALLOC_REDIR + free(ptr); +#else + __bound_free(ptr, caller); +#endif + return NULL; + } + + new_ptr = BOUND_REALLOC (ptr, size + 1); + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, new_ptr, (unsigned long)size); + + if (NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_realloc_count); + + if (ptr) + tree = splay_delete ((size_t) ptr, tree); + if (new_ptr) { + tree = splay_insert ((size_t) new_ptr, size ? size : size + 1, tree); + if (tree && tree->start == (size_t) new_ptr) + tree->type = TCC_TYPE_REALLOC; + } + POST_SEM (); + } + return new_ptr; +} + +#if MALLOC_REDIR +void *calloc(size_t nmemb, size_t size) +#else +void *__bound_calloc(size_t nmemb, size_t size) +#endif +{ + void *ptr; + + size *= nmemb; +#if MALLOC_REDIR + /* This will catch the first dlsym call from __bound_init */ + if (malloc_redir == NULL) { + __bound_init (0, -1); + if (malloc_redir == NULL) { + ptr = &initial_pool[pool_index]; + pool_index = (pool_index + size + 15) & ~15; + if (pool_index >= sizeof (initial_pool)) + bound_alloc_error ("initial memory pool too small"); + dprintf (stderr, "%s, %s(): initial %p, 0x%lx\n", + __FILE__, __FUNCTION__, ptr, (unsigned long)size); + memset (ptr, 0, size); + return ptr; + } + } +#endif + ptr = BOUND_MALLOC(size + 1); + dprintf (stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, ptr, (unsigned long)size); + + if (ptr) { + memset (ptr, 0, size); + if (NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_calloc_count); + tree = splay_insert ((size_t) ptr, size ? size : size + 1, tree); + if (tree && tree->start == (size_t) ptr) + tree->type = TCC_TYPE_CALLOC; + POST_SEM (); + } + } + return ptr; +} + +#if !defined(_WIN32) +void *__bound_mmap (void *start, size_t size, int prot, + int flags, int fd, off_t offset) +{ + void *result; + + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, start, (unsigned long)size); + result = mmap (start, size, prot, flags, fd, offset); + if (result && NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_mmap_count); + tree = splay_insert((size_t)result, size, tree); + POST_SEM (); + } + return result; +} + +int __bound_munmap (void *start, size_t size) +{ + int result; + + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, start, (unsigned long)size); + if (start && NO_CHECKING_GET() == 0) { + WAIT_SEM (); + INCR_COUNT(bound_munmap_count); + tree = splay_delete ((size_t) start, tree); + POST_SEM (); + } + result = munmap (start, size); + return result; +} +#endif + +/* some useful checked functions */ + +/* check that (p ... p + size - 1) lies inside 'p' region, if any */ +static void __bound_check(const void *p, size_t size, const char *function) +{ + if (size != 0 && __bound_ptr_add((void *)p, size) == INVALID_POINTER) { + bound_error("invalid pointer %p, size 0x%lx in %s", + p, (unsigned long)size, function); + } +} + +static int check_overlap (const void *p1, size_t n1, + const void *p2, size_t n2, + const char *function) +{ + const void *p1e = (const void *) ((const char *) p1 + n1); + const void *p2e = (const void *) ((const char *) p2 + n2); + + if (NO_CHECKING_GET() == 0 && n1 != 0 && n2 !=0 && + ((p1 <= p2 && p1e > p2) || /* p1----p2====p1e----p2e */ + (p2 <= p1 && p2e > p1))) { /* p2----p1====p2e----p1e */ + bound_error("overlapping regions %p(0x%lx), %p(0x%lx) in %s", + p1, (unsigned long)n1, p2, (unsigned long)n2, function); + return never_fatal < 0; + } + return 0; +} + +void *__bound_memcpy(void *dest, const void *src, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_mempcy_count); + __bound_check(dest, n, "memcpy dest"); + __bound_check(src, n, "memcpy src"); + if (check_overlap(dest, n, src, n, "memcpy")) + return dest; + return memcpy(dest, src, n); +} + +int __bound_memcmp(const void *s1, const void *s2, size_t n) +{ + const unsigned char *u1 = (const unsigned char *) s1; + const unsigned char *u2 = (const unsigned char *) s2; + int retval = 0; + + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, s1, s2, (unsigned long)n); + INCR_COUNT(bound_memcmp_count); + for (;;) { + if ((ssize_t) --n == -1) + break; + else if (*u1 != *u2) { + retval = *u1++ - *u2++; + break; + } + ++u1; + ++u2; + } + __bound_check(s1, (const void *)u1 - s1, "memcmp s1"); + __bound_check(s2, (const void *)u2 - s2, "memcmp s2"); + return retval; +} + +void *__bound_memmove(void *dest, const void *src, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_memmove_count); + __bound_check(dest, n, "memmove dest"); + __bound_check(src, n, "memmove src"); + return memmove(dest, src, n); +} + +void *__bound_memset(void *s, int c, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %d, 0x%lx\n", + __FILE__, __FUNCTION__, s, c, (unsigned long)n); + INCR_COUNT(bound_memset_count); + __bound_check(s, n, "memset"); + return memset(s, c, n); +} + +#if defined(__arm__) && defined(__ARM_EABI__) +void *__bound___aeabi_memcpy(void *dest, const void *src, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_mempcy_count); + __bound_check(dest, n, "memcpy dest"); + __bound_check(src, n, "memcpy src"); + if (check_overlap(dest, n, src, n, "memcpy")) + return dest; + return __aeabi_memcpy(dest, src, n); +} + +void *__bound___aeabi_memmove(void *dest, const void *src, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_memmove_count); + __bound_check(dest, n, "memmove dest"); + __bound_check(src, n, "memmove src"); + return __aeabi_memmove(dest, src, n); +} + +void *__bound___aeabi_memmove4(void *dest, const void *src, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_memmove_count); + __bound_check(dest, n, "memmove dest"); + __bound_check(src, n, "memmove src"); + return __aeabi_memmove4(dest, src, n); +} + +void *__bound___aeabi_memmove8(void *dest, const void *src, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_memmove_count); + __bound_check(dest, n, "memmove dest"); + __bound_check(src, n, "memmove src"); + return __aeabi_memmove8(dest, src, n); +} + +void *__bound___aeabi_memset(void *s, int c, size_t n) +{ + dprintf(stderr, "%s, %s(): %p, %d, 0x%lx\n", + __FILE__, __FUNCTION__, s, c, (unsigned long)n); + INCR_COUNT(bound_memset_count); + __bound_check(s, n, "memset"); + return __aeabi_memset(s, c, n); +} +#endif + +int __bound_strlen(const char *s) +{ + const char *p = s; + + dprintf(stderr, "%s, %s(): %p\n", + __FILE__, __FUNCTION__, s); + INCR_COUNT(bound_strlen_count); + while (*p++); + __bound_check(s, p - s, "strlen"); + return (p - s) - 1; +} + +char *__bound_strcpy(char *dest, const char *src) +{ + size_t len; + const char *p = src; + + dprintf(stderr, "%s, %s(): %p, %p\n", + __FILE__, __FUNCTION__, dest, src); + INCR_COUNT(bound_strcpy_count); + while (*p++); + len = p - src; + __bound_check(dest, len, "strcpy dest"); + __bound_check(src, len, "strcpy src"); + if (check_overlap(dest, len, src, len, "strcpy")) + return dest; + return strcpy (dest, src); +} + +char *__bound_strncpy(char *dest, const char *src, size_t n) +{ + size_t len = n; + const char *p = src; + + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, dest, src, (unsigned long)n); + INCR_COUNT(bound_strncpy_count); + while (len-- && *p++); + len = p - src; + __bound_check(dest, len, "strncpy dest"); + __bound_check(src, len, "strncpy src"); + if (check_overlap(dest, len, src, len, "strncpy")) + return dest; + return strncpy(dest, src, n); +} + +int __bound_strcmp(const char *s1, const char *s2) +{ + const unsigned char *u1 = (const unsigned char *) s1; + const unsigned char *u2 = (const unsigned char *) s2; + + dprintf(stderr, "%s, %s(): %p, %p\n", + __FILE__, __FUNCTION__, s1, s2); + INCR_COUNT(bound_strcmp_count); + while (*u1 && *u1 == *u2) { + ++u1; + ++u2; + } + __bound_check(s1, ((const char *)u1 - s1) + 1, "strcmp s1"); + __bound_check(s2, ((const char *)u2 - s2) + 1, "strcmp s2"); + return *u1 - *u2; +} + +int __bound_strncmp(const char *s1, const char *s2, size_t n) +{ + const unsigned char *u1 = (const unsigned char *) s1; + const unsigned char *u2 = (const unsigned char *) s2; + int retval = 0; + + dprintf(stderr, "%s, %s(): %p, %p, 0x%lx\n", + __FILE__, __FUNCTION__, s1, s2, (unsigned long)n); + INCR_COUNT(bound_strncmp_count); + do { + if ((ssize_t) --n == -1) + break; + else if (*u1 != *u2) { + retval = *u1++ - *u2++; + break; + } + ++u2; + } while (*u1++); + __bound_check(s1, (const char *)u1 - s1, "strncmp s1"); + __bound_check(s2, (const char *)u2 - s2, "strncmp s2"); + return retval; +} + +char *__bound_strcat(char *dest, const char *src) +{ + char *r = dest; + const char *s = src; + + dprintf(stderr, "%s, %s(): %p, %p\n", + __FILE__, __FUNCTION__, dest, src); + INCR_COUNT(bound_strcat_count); + while (*dest++); + while (*src++); + __bound_check(r, (dest - r) + (src - s) - 1, "strcat dest"); + __bound_check(s, src - s, "strcat src"); + if (check_overlap(r, (dest - r) + (src - s) - 1, s, src - s, "strcat")) + return dest; + return strcat(r, s); +} + +char *__bound_strchr(const char *s, int c) +{ + const unsigned char *str = (const unsigned char *) s; + unsigned char ch = c; + + dprintf(stderr, "%s, %s(): %p, %d\n", + __FILE__, __FUNCTION__, s, ch); + INCR_COUNT(bound_strchr_count); + while (*str) { + if (*str == ch) + break; + ++str; + } + __bound_check(s, ((const char *)str - s) + 1, "strchr"); + return *str == ch ? (char *) str : NULL; +} + +char *__bound_strdup(const char *s) +{ + const char *p = s; + char *new; + + INCR_COUNT(bound_strdup_count); + while (*p++); + __bound_check(s, p - s, "strdup"); + new = BOUND_MALLOC ((p - s) + 1); + dprintf(stderr, "%s, %s(): %p, 0x%lx\n", + __FILE__, __FUNCTION__, new, (unsigned long)(p -s)); + if (new) { + if (NO_CHECKING_GET() == 0 && no_strdup == 0) { + WAIT_SEM (); + tree = splay_insert((size_t)new, p - s, tree); + if (tree && tree->start == (size_t) new) + tree->type = TCC_TYPE_STRDUP; + POST_SEM (); + } + memcpy (new, s, p - s); + } + return new; +} + +/* + An implementation of top-down splaying with sizes + D. Sleator , January 1994. + + This extends top-down-splay.c to maintain a size field in each node. + This is the number of nodes in the subtree rooted there. This makes + it possible to efficiently compute the rank of a key. (The rank is + the number of nodes to the left of the given key.) It it also + possible to quickly find the node of a given rank. Both of these + operations are illustrated in the code below. The remainder of this + introduction is taken from top-down-splay.c. + + "Splay trees", or "self-adjusting search trees" are a simple and + efficient data structure for storing an ordered set. The data + structure consists of a binary tree, with no additional fields. It + allows searching, insertion, deletion, deletemin, deletemax, + splitting, joining, and many other operations, all with amortized + logarithmic performance. Since the trees adapt to the sequence of + requests, their performance on real access patterns is typically even + better. Splay trees are described in a number of texts and papers + [1,2,3,4]. + + The code here is adapted from simple top-down splay, at the bottom of + page 669 of [2]. It can be obtained via anonymous ftp from + spade.pc.cs.cmu.edu in directory /usr/sleator/public. + + The chief modification here is that the splay operation works even if the + item being splayed is not in the tree, and even if the tree root of the + tree is NULL. So the line: + + t = splay(i, t); + + causes it to search for item with key i in the tree rooted at t. If it's + there, it is splayed to the root. If it isn't there, then the node put + at the root is the last one before NULL that would have been reached in a + normal binary search for i. (It's a neighbor of i in the tree.) This + allows many other operations to be easily implemented, as shown below. + + [1] "Data Structures and Their Algorithms", Lewis and Denenberg, + Harper Collins, 1991, pp 243-251. + [2] "Self-adjusting Binary Search Trees" Sleator and Tarjan, + JACM Volume 32, No 3, July 1985, pp 652-686. + [3] "Data Structure and Algorithm Analysis", Mark Weiss, + Benjamin Cummins, 1992, pp 119-130. + [4] "Data Structures, Algorithms, and Performance", Derick Wood, + Addison-Wesley, 1993, pp 367-375 +*/ + +/* Code adapted for tcc */ + +#define compare(start,tstart,tsize) (start < tstart ? -1 : \ + start >= tstart+tsize ? 1 : 0) + +static Tree * splay (size_t addr, Tree *t) +/* Splay using the key start (which may or may not be in the tree.) */ +/* The starting root is t, and the tree used is defined by rat */ +{ + Tree N, *l, *r, *y; + int comp; + + INCR_COUNT_SPLAY(bound_splay); + if (t == NULL) return t; + N.left = N.right = NULL; + l = r = &N; + + for (;;) { + comp = compare(addr, t->start, t->size); + if (comp < 0) { + y = t->left; + if (y == NULL) break; + if (compare(addr, y->start, y->size) < 0) { + t->left = y->right; /* rotate right */ + y->right = t; + t = y; + if (t->left == NULL) break; + } + r->left = t; /* link right */ + r = t; + t = t->left; + } else if (comp > 0) { + y = t->right; + if (y == NULL) break; + if (compare(addr, y->start, y->size) > 0) { + t->right = y->left; /* rotate left */ + y->left = t; + t = y; + if (t->right == NULL) break; + } + l->right = t; /* link left */ + l = t; + t = t->right; + } else { + break; + } + } + l->right = t->left; /* assemble */ + r->left = t->right; + t->left = N.right; + t->right = N.left; + + return t; +} + +#define compare_end(start,tend) (start < tend ? -1 : \ + start > tend ? 1 : 0) + +static Tree * splay_end (size_t addr, Tree *t) +/* Splay using the key start (which may or may not be in the tree.) */ +/* The starting root is t, and the tree used is defined by rat */ +{ + Tree N, *l, *r, *y; + int comp; + + INCR_COUNT_SPLAY(bound_splay_end); + if (t == NULL) return t; + N.left = N.right = NULL; + l = r = &N; + + for (;;) { + comp = compare_end(addr, t->start + t->size); + if (comp < 0) { + y = t->left; + if (y == NULL) break; + if (compare_end(addr, y->start + y->size) < 0) { + t->left = y->right; /* rotate right */ + y->right = t; + t = y; + if (t->left == NULL) break; + } + r->left = t; /* link right */ + r = t; + t = t->left; + } else if (comp > 0) { + y = t->right; + if (y == NULL) break; + if (compare_end(addr, y->start + y->size) > 0) { + t->right = y->left; /* rotate left */ + y->left = t; + t = y; + if (t->right == NULL) break; + } + l->right = t; /* link left */ + l = t; + t = t->right; + } else { + break; + } + } + l->right = t->left; /* assemble */ + r->left = t->right; + t->left = N.right; + t->right = N.left; + + return t; +} + +static Tree * splay_insert(size_t addr, size_t size, Tree * t) +/* Insert key start into the tree t, if it is not already there. */ +/* Return a pointer to the resulting tree. */ +{ + Tree * new; + + INCR_COUNT_SPLAY(bound_splay_insert); + if (t != NULL) { + t = splay(addr,t); + if (compare(addr, t->start, t->size)==0) { + return t; /* it's already there */ + } + } +#if TREE_REUSE + if (tree_free_list) { + new = tree_free_list; + tree_free_list = new->left; + } + else +#endif + { + new = (Tree *) BOUND_MALLOC (sizeof (Tree)); + } + if (new == NULL) { + bound_alloc_error("not enough memory for bound checking code"); + } + else { + if (t == NULL) { + new->left = new->right = NULL; + } else if (compare(addr, t->start, t->size) < 0) { + new->left = t->left; + new->right = t; + t->left = NULL; + } else { + new->right = t->right; + new->left = t; + t->right = NULL; + } + new->start = addr; + new->size = size; + new->type = TCC_TYPE_NONE; + new->is_invalid = 0; + } + return new; +} + +#define compare_destroy(start,tstart) (start < tstart ? -1 : \ + start > tstart ? 1 : 0) + +static Tree * splay_delete(size_t addr, Tree *t) +/* Deletes addr from the tree if it's there. */ +/* Return a pointer to the resulting tree. */ +{ + Tree * x; + + INCR_COUNT_SPLAY(bound_splay_delete); + if (t==NULL) return NULL; + t = splay(addr,t); + if (compare_destroy(addr, t->start) == 0) { /* found it */ + if (t->left == NULL) { + x = t->right; + } else { + x = splay(addr, t->left); + x->right = t->right; + } +#if TREE_REUSE + t->left = tree_free_list; + tree_free_list = t; +#else + BOUND_FREE(t); +#endif + return x; + } else { + return t; /* It wasn't there */ + } +} + +void splay_printtree(Tree * t, int d) +{ + int i; + if (t == NULL) return; + splay_printtree(t->right, d+1); + for (i=0; istart, (unsigned long) t->size, + (unsigned)t->type, (unsigned)t->is_invalid); + splay_printtree(t->left, d+1); +} diff --git a/05/tcc-final/lib/bt-dll.c b/05/tcc-final/lib/bt-dll.c new file mode 100644 index 0000000..7c62cef --- /dev/null +++ b/05/tcc-final/lib/bt-dll.c @@ -0,0 +1,74 @@ +/* ------------------------------------------------------------- */ +/* stubs for calling bcheck functions from a dll. */ + +#include +#include + +#define REDIR_ALL \ + REDIR(__bt_init) \ + REDIR(__bt_exit) \ + REDIR(tcc_backtrace) \ + \ + REDIR(__bound_ptr_add) \ + REDIR(__bound_ptr_indir1) \ + REDIR(__bound_ptr_indir2) \ + REDIR(__bound_ptr_indir4) \ + REDIR(__bound_ptr_indir8) \ + REDIR(__bound_ptr_indir12) \ + REDIR(__bound_ptr_indir16) \ + REDIR(__bound_local_new) \ + REDIR(__bound_local_delete) \ + REDIR(__bound_new_region) \ + \ + REDIR(__bound_free) \ + REDIR(__bound_malloc) \ + REDIR(__bound_realloc) \ + REDIR(__bound_memcpy) \ + REDIR(__bound_memcmp) \ + REDIR(__bound_memmove) \ + REDIR(__bound_memset) \ + REDIR(__bound_strlen) \ + REDIR(__bound_strcpy) \ + REDIR(__bound_strncpy) \ + REDIR(__bound_strcmp) \ + REDIR(__bound_strncmp) \ + REDIR(__bound_strcat) \ + REDIR(__bound_strchr) \ + REDIR(__bound_strdup) + +#ifdef __leading_underscore +#define _(s) "_"#s +#else +#define _(s) #s +#endif + +#define REDIR(s) void *s; +static struct { REDIR_ALL } all_ptrs; +#undef REDIR +#define REDIR(s) #s"\0" +static const char all_names[] = REDIR_ALL; +#undef REDIR +#define REDIR(s) __asm__(".global " _(s) ";" _(s) ": jmp *%0" : : "m" (all_ptrs.s) ); +static void all_jmps() { REDIR_ALL } +#undef REDIR + +void __bt_init_dll(int bcheck) +{ + const char *s = all_names; + void **p = (void**)&all_ptrs; + do { + *p = (void*)GetProcAddress(GetModuleHandle(NULL), (char*)s); + if (NULL == *p) { + char buf[100]; + sprintf(buf, + "Error: function '%s()' not found in executable. " + "(Need -bt or -b for linking the exe.)", s); + if (GetStdHandle(STD_ERROR_HANDLE)) + fprintf(stderr, "TCC/BCHECK: %s\n", buf), fflush(stderr); + else + MessageBox(NULL, buf, "TCC/BCHECK", MB_ICONERROR); + ExitProcess(1); + } + s = strchr(s,'\0') + 1, ++p; + } while (*s && (bcheck || p < &all_ptrs.__bound_ptr_add)); +} diff --git a/05/tcc-final/lib/bt-exe.c b/05/tcc-final/lib/bt-exe.c new file mode 100644 index 0000000..f758476 --- /dev/null +++ b/05/tcc-final/lib/bt-exe.c @@ -0,0 +1,73 @@ +/* ------------------------------------------------------------- */ +/* for linking rt_printline and the signal/exception handler + from tccrun.c into executables. */ + +#define CONFIG_TCC_BACKTRACE_ONLY +#define ONE_SOURCE 0 +#include "../tccrun.c" + +int (*__rt_error)(void*, void*, const char *, va_list); +__attribute__((weak)) void __bound_checking_lock(void); +__attribute__((weak)) void __bound_checking_unlock(void); + +#ifndef _WIN32 +# define __declspec(n) +#endif + +__declspec(dllexport) +void __bt_init(rt_context *p, int num_callers) +{ + __attribute__((weak)) int main(); + __attribute__((weak)) void __bound_init(void*, int); + struct rt_context *rc = &g_rtctxt; + //fprintf(stderr, "__bt_init %d %p %p\n", num_callers, p->stab_sym, p->bounds_start), fflush(stderr); + /* call __bound_init here due to redirection of sigaction */ + /* needed to add global symbols */ + if (p->bounds_start) { + __bound_init(p->bounds_start, -1); + __bound_checking_lock(); + } + if (num_callers) { + memcpy(rc, p, offsetof(rt_context, next)); + rc->num_callers = num_callers - 1; + rc->top_func = main; + __rt_error = _rt_error; + set_exception_handler(); + } else { + p->next = rc->next, rc->next = p; + } + if (p->bounds_start) + __bound_checking_unlock(); +} + +__declspec(dllexport) +void __bt_exit(rt_context *p) +{ + __attribute__((weak)) void __bound_exit_dll(void*); + struct rt_context *rc = &g_rtctxt; + + if (p->bounds_start) { + __bound_exit_dll(p->bounds_start); + __bound_checking_lock(); + } + while (rc) { + if (rc->next == p) { + rc->next = rc->next->next; + break; + } + rc = rc->next; + } + if (p->bounds_start) + __bound_checking_unlock(); +} + +/* copy a string and truncate it. */ +ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s) +{ + int l = strlen(s); + if (l >= buf_size) + l = buf_size - 1; + memcpy(buf, s, l); + buf[l] = 0; + return buf; +} diff --git a/05/tcc-final/lib/bt-log.c b/05/tcc-final/lib/bt-log.c new file mode 100644 index 0000000..d767f08 --- /dev/null +++ b/05/tcc-final/lib/bt-log.c @@ -0,0 +1,37 @@ +/* ------------------------------------------------------------- */ +/* function to get a stack backtrace on demand with a message */ + +#include +#include +#include + +int (*__rt_error)(void*, void*, const char *, va_list); + +#ifdef _WIN32 +# define DLL_EXPORT __declspec(dllexport) +#else +# define DLL_EXPORT +#endif + +DLL_EXPORT int tcc_backtrace(const char *fmt, ...) +{ + va_list ap; + int ret; + + if (__rt_error) { + void *fp = __builtin_frame_address(1); + void *ip = __builtin_return_address(0); + va_start(ap, fmt); + ret = __rt_error(fp, ip, fmt, ap); + va_end(ap); + } else { + const char *p; + if (fmt[0] == '^' && (p = strchr(fmt + 1, fmt[0]))) + fmt = p + 1; + va_start(ap, fmt); + ret = vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"), fflush(stderr); + } + return ret; +} diff --git a/05/tcc-final/lib/dsohandle.c b/05/tcc-final/lib/dsohandle.c new file mode 100644 index 0000000..0993dbc --- /dev/null +++ b/05/tcc-final/lib/dsohandle.c @@ -0,0 +1 @@ +void * __dso_handle __attribute((visibility("hidden"))) = &__dso_handle; diff --git a/05/tcc-final/lib/fetch_and_add.S b/05/tcc-final/lib/fetch_and_add.S new file mode 100644 index 0000000..ec96a03 --- /dev/null +++ b/05/tcc-final/lib/fetch_and_add.S @@ -0,0 +1,67 @@ +/* ---------------------------------------------- */ +#if defined __arm__ + + .text + .align 2 + .global fetch_and_add_arm + .type fetch_and_add_arm, %function +fetch_and_add_arm: + mcr p15, #0, r0, c7, c10, #5 +.L0: + ldrex r3, [r0] + add r3, r3, r1 + strex r2, r3, [r0] + cmp r2, #0 + bne .L0 + mcr p15, #0, r0, c7, c10, #5 + bx lr + .size fetch_and_add_arm, .-fetch_and_add_arm + +/* ---------------------------------------------- */ +#elif defined __aarch64__ + + .text + .align 2 + .global fetch_and_add_arm64 + .type fetch_and_add_arm64, %function +fetch_and_add_arm64: +#ifdef __TINYC__ + .int 0x885f7c02 + .int 0x0b010042 + .int 0x8803fc02 + .int 0x35ffffa3 + .int 0xd5033bbf + .int 0xd65f03c0 +#else + ldxr w2, [x0] + add w2, w2, w1 + stlxr w3, w2, [x0] + cbnz w3, fetch_and_add_arm64 + dmb ish + ret +#endif + + .size fetch_and_add_arm64, .-fetch_and_add_arm64 + +/* ---------------------------------------------- */ +#elif defined __riscv + + .text + .align 2 + .global fetch_and_add_riscv64 + .type fetch_and_add_riscv64, %function +fetch_and_add_riscv64: +#ifdef __TINYC__ + .int 0x0f50000f + .int 0x004b5202f + .short 0x8082 +#else + fence iorw,ow + amoadd.w.aq zero,a1,0(a0) + ret +#endif + + .size fetch_and_add_riscv64, .-fetch_and_add_riscv64 + +/* ---------------------------------------------- */ +#endif diff --git a/05/tcc-final/lib/lib-arm64.c b/05/tcc-final/lib/lib-arm64.c new file mode 100644 index 0000000..33df509 --- /dev/null +++ b/05/tcc-final/lib/lib-arm64.c @@ -0,0 +1,677 @@ +/* + * TCC runtime library for arm64. + * + * Copyright (c) 2015 Edmund Grimley Evans + * + * Copying and distribution of this file, with or without modification, + * are permitted in any medium without royalty provided the copyright + * notice and this notice are preserved. This file is offered as-is, + * without any warranty. + */ + +#ifdef __TINYC__ +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned uint32_t; +typedef long long int64_t; +typedef unsigned long long uint64_t; +void *memcpy(void*,void*,__SIZE_TYPE__); +#else +#include +#include +#endif + +#ifndef __riscv +void __clear_cache(void *beg, void *end) +{ + __arm64_clear_cache(beg, end); +} +#endif + +typedef struct { + uint64_t x0, x1; +} u128_t; + +static long double f3_zero(int sgn) +{ + long double f; + u128_t x = { 0, (uint64_t)sgn << 63 }; + memcpy(&f, &x, 16); + return f; +} + +static long double f3_infinity(int sgn) +{ + long double f; + u128_t x = { 0, (uint64_t)sgn << 63 | 0x7fff000000000000 }; + memcpy(&f, &x, 16); + return f; +} + +static long double f3_NaN(void) +{ + long double f; +#if 0 + // ARM's default NaN usually has just the top fraction bit set: + u128_t x = { 0, 0x7fff800000000000 }; +#else + // GCC's library sets all fraction bits: + u128_t x = { -1, 0x7fffffffffffffff }; +#endif + memcpy(&f, &x, 16); + return f; +} + +static int fp3_convert_NaN(long double *f, int sgn, u128_t mnt) +{ + u128_t x = { mnt.x0, + mnt.x1 | 0x7fff800000000000 | (uint64_t)sgn << 63 }; + memcpy(f, &x, 16); + return 1; +} + +static int fp3_detect_NaNs(long double *f, + int a_sgn, int a_exp, u128_t a, + int b_sgn, int b_exp, u128_t b) +{ + // Detect signalling NaNs: + if (a_exp == 32767 && (a.x0 | a.x1 << 16) && !(a.x1 >> 47 & 1)) + return fp3_convert_NaN(f, a_sgn, a); + if (b_exp == 32767 && (b.x0 | b.x1 << 16) && !(b.x1 >> 47 & 1)) + return fp3_convert_NaN(f, b_sgn, b); + + // Detect quiet NaNs: + if (a_exp == 32767 && (a.x0 | a.x1 << 16)) + return fp3_convert_NaN(f, a_sgn, a); + if (b_exp == 32767 && (b.x0 | b.x1 << 16)) + return fp3_convert_NaN(f, b_sgn, b); + + return 0; +} + +static void f3_unpack(int *sgn, int32_t *exp, u128_t *mnt, long double f) +{ + u128_t x; + memcpy(&x, &f, 16); + *sgn = x.x1 >> 63; + *exp = x.x1 >> 48 & 32767; + x.x1 = x.x1 << 16 >> 16; + if (*exp) + x.x1 |= (uint64_t)1 << 48; + else + *exp = 1; + *mnt = x; +} + +static u128_t f3_normalise(int32_t *exp, u128_t mnt) +{ + int sh; + if (!(mnt.x0 | mnt.x1)) + return mnt; + if (!mnt.x1) { + mnt.x1 = mnt.x0; + mnt.x0 = 0; + *exp -= 64; + } + for (sh = 32; sh; sh >>= 1) { + if (!(mnt.x1 >> (64 - sh))) { + mnt.x1 = mnt.x1 << sh | mnt.x0 >> (64 - sh); + mnt.x0 = mnt.x0 << sh; + *exp -= sh; + } + } + return mnt; +} + +static u128_t f3_sticky_shift(int32_t sh, u128_t x) +{ + if (sh >= 128) { + x.x0 = !!(x.x0 | x.x1); + x.x1 = 0; + return x; + } + if (sh >= 64) { + x.x0 = x.x1 | !!x.x0; + x.x1 = 0; + sh -= 64; + } + if (sh > 0) { + x.x0 = x.x0 >> sh | x.x1 << (64 - sh) | !!(x.x0 << (64 - sh)); + x.x1 = x.x1 >> sh; + } + return x; +} + +static long double f3_round(int sgn, int32_t exp, u128_t x) +{ + long double f; + int error; + + if (exp > 0) { + x = f3_sticky_shift(13, x); + } + else { + x = f3_sticky_shift(14 - exp, x); + exp = 0; + } + + error = x.x0 & 3; + x.x0 = x.x0 >> 2 | x.x1 << 62; + x.x1 = x.x1 >> 2; + + if (error == 3 || ((error == 2) & (x.x0 & 1))) { + if (!++x.x0) { + ++x.x1; + if (x.x1 == (uint64_t)1 << 48) + exp = 1; + else if (x.x1 == (uint64_t)1 << 49) { + ++exp; + x.x0 = x.x0 >> 1 | x.x1 << 63; + x.x1 = x.x1 >> 1; + } + } + } + + if (exp >= 32767) + return f3_infinity(sgn); + + x.x1 = x.x1 << 16 >> 16 | (uint64_t)exp << 48 | (uint64_t)sgn << 63; + memcpy(&f, &x, 16); + return f; +} + +static long double f3_add(long double fa, long double fb, int neg) +{ + u128_t a, b, x; + int32_t a_exp, b_exp, x_exp; + int a_sgn, b_sgn, x_sgn; + long double fx; + + f3_unpack(&a_sgn, &a_exp, &a, fa); + f3_unpack(&b_sgn, &b_exp, &b, fb); + + if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b)) + return fx; + + b_sgn ^= neg; + + // Handle infinities and zeroes: + if (a_exp == 32767 && b_exp == 32767 && a_sgn != b_sgn) + return f3_NaN(); + if (a_exp == 32767) + return f3_infinity(a_sgn); + if (b_exp == 32767) + return f3_infinity(b_sgn); + if (!(a.x0 | a.x1 | b.x0 | b.x1)) + return f3_zero(a_sgn & b_sgn); + + a.x1 = a.x1 << 3 | a.x0 >> 61; + a.x0 = a.x0 << 3; + b.x1 = b.x1 << 3 | b.x0 >> 61; + b.x0 = b.x0 << 3; + + if (a_exp <= b_exp) { + a = f3_sticky_shift(b_exp - a_exp, a); + a_exp = b_exp; + } + else { + b = f3_sticky_shift(a_exp - b_exp, b); + b_exp = a_exp; + } + + x_sgn = a_sgn; + x_exp = a_exp; + if (a_sgn == b_sgn) { + x.x0 = a.x0 + b.x0; + x.x1 = a.x1 + b.x1 + (x.x0 < a.x0); + } + else { + x.x0 = a.x0 - b.x0; + x.x1 = a.x1 - b.x1 - (x.x0 > a.x0); + if (x.x1 >> 63) { + x_sgn ^= 1; + x.x0 = -x.x0; + x.x1 = -x.x1 - !!x.x0; + } + } + + if (!(x.x0 | x.x1)) + return f3_zero(0); + + x = f3_normalise(&x_exp, x); + + return f3_round(x_sgn, x_exp + 12, x); +} + +long double __addtf3(long double a, long double b) +{ + return f3_add(a, b, 0); +} + +long double __subtf3(long double a, long double b) +{ + return f3_add(a, b, 1); +} + +long double __multf3(long double fa, long double fb) +{ + u128_t a, b, x; + int32_t a_exp, b_exp, x_exp; + int a_sgn, b_sgn, x_sgn; + long double fx; + + f3_unpack(&a_sgn, &a_exp, &a, fa); + f3_unpack(&b_sgn, &b_exp, &b, fb); + + if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b)) + return fx; + + // Handle infinities and zeroes: + if ((a_exp == 32767 && !(b.x0 | b.x1)) || + (b_exp == 32767 && !(a.x0 | a.x1))) + return f3_NaN(); + if (a_exp == 32767 || b_exp == 32767) + return f3_infinity(a_sgn ^ b_sgn); + if (!(a.x0 | a.x1) || !(b.x0 | b.x1)) + return f3_zero(a_sgn ^ b_sgn); + + a = f3_normalise(&a_exp, a); + b = f3_normalise(&b_exp, b); + + x_sgn = a_sgn ^ b_sgn; + x_exp = a_exp + b_exp - 16352; + + { + // Convert to base (1 << 30), discarding bottom 6 bits, which are zero, + // so there are (32, 30, 30, 30) bits in (a3, a2, a1, a0): + uint64_t a0 = a.x0 << 28 >> 34; + uint64_t b0 = b.x0 << 28 >> 34; + uint64_t a1 = a.x0 >> 36 | a.x1 << 62 >> 34; + uint64_t b1 = b.x0 >> 36 | b.x1 << 62 >> 34; + uint64_t a2 = a.x1 << 32 >> 34; + uint64_t b2 = b.x1 << 32 >> 34; + uint64_t a3 = a.x1 >> 32; + uint64_t b3 = b.x1 >> 32; + // Use 16 small multiplications and additions that do not overflow: + uint64_t x0 = a0 * b0; + uint64_t x1 = (x0 >> 30) + a0 * b1 + a1 * b0; + uint64_t x2 = (x1 >> 30) + a0 * b2 + a1 * b1 + a2 * b0; + uint64_t x3 = (x2 >> 30) + a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0; + uint64_t x4 = (x3 >> 30) + a1 * b3 + a2 * b2 + a3 * b1; + uint64_t x5 = (x4 >> 30) + a2 * b3 + a3 * b2; + uint64_t x6 = (x5 >> 30) + a3 * b3; + // We now have (64, 30, 30, ...) bits in (x6, x5, x4, ...). + // Take the top 128 bits, setting bottom bit if any lower bits were set: + uint64_t y0 = (x5 << 34 | x4 << 34 >> 30 | x3 << 34 >> 60 | + !!(x3 << 38 | (x2 | x1 | x0) << 34)); + uint64_t y1 = x6; + // Top bit may be zero. Renormalise: + if (!(y1 >> 63)) { + y1 = y1 << 1 | y0 >> 63; + y0 = y0 << 1; + --x_exp; + } + x.x0 = y0; + x.x1 = y1; + } + + return f3_round(x_sgn, x_exp, x); +} + +long double __divtf3(long double fa, long double fb) +{ + u128_t a, b, x; + int32_t a_exp, b_exp, x_exp; + int a_sgn, b_sgn, x_sgn, i; + long double fx; + + f3_unpack(&a_sgn, &a_exp, &a, fa); + f3_unpack(&b_sgn, &b_exp, &b, fb); + + if (fp3_detect_NaNs(&fx, a_sgn, a_exp, a, b_sgn, b_exp, b)) + return fx; + + // Handle infinities and zeroes: + if ((a_exp == 32767 && b_exp == 32767) || + (!(a.x0 | a.x1) && !(b.x0 | b.x1))) + return f3_NaN(); + if (a_exp == 32767 || !(b.x0 | b.x1)) + return f3_infinity(a_sgn ^ b_sgn); + if (!(a.x0 | a.x1) || b_exp == 32767) + return f3_zero(a_sgn ^ b_sgn); + + a = f3_normalise(&a_exp, a); + b = f3_normalise(&b_exp, b); + + x_sgn = a_sgn ^ b_sgn; + x_exp = a_exp - b_exp + 16395; + + a.x0 = a.x0 >> 1 | a.x1 << 63; + a.x1 = a.x1 >> 1; + b.x0 = b.x0 >> 1 | b.x1 << 63; + b.x1 = b.x1 >> 1; + x.x0 = 0; + x.x1 = 0; + for (i = 0; i < 116; i++) { + x.x1 = x.x1 << 1 | x.x0 >> 63; + x.x0 = x.x0 << 1; + if (a.x1 > b.x1 || (a.x1 == b.x1 && a.x0 >= b.x0)) { + a.x1 = a.x1 - b.x1 - (a.x0 < b.x0); + a.x0 = a.x0 - b.x0; + x.x0 |= 1; + } + a.x1 = a.x1 << 1 | a.x0 >> 63; + a.x0 = a.x0 << 1; + } + x.x0 |= !!(a.x0 | a.x1); + + x = f3_normalise(&x_exp, x); + + return f3_round(x_sgn, x_exp, x); +} + +long double __extendsftf2(float f) +{ + long double fx; + u128_t x; + uint32_t a; + uint64_t aa; + memcpy(&a, &f, 4); + aa = a; + x.x0 = 0; + if (!(a << 1)) + x.x1 = aa << 32; + else if (a << 1 >> 24 == 255) + x.x1 = (0x7fff000000000000 | aa >> 31 << 63 | aa << 41 >> 16 | + (uint64_t)!!(a << 9) << 47); + else if (a << 1 >> 24 == 0) { + uint64_t adj = 0; + while (!(a << 1 >> 1 >> (23 - adj))) + adj++; + x.x1 = aa >> 31 << 63 | (16256 - adj + 1) << 48 | aa << adj << 41 >> 16; + } else + x.x1 = (aa >> 31 << 63 | ((aa >> 23 & 255) + 16256) << 48 | + aa << 41 >> 16); + memcpy(&fx, &x, 16); + return fx; +} + +long double __extenddftf2(double f) +{ + long double fx; + u128_t x; + uint64_t a; + memcpy(&a, &f, 8); + x.x0 = a << 60; + if (!(a << 1)) + x.x1 = a; + else if (a << 1 >> 53 == 2047) + x.x1 = (0x7fff000000000000 | a >> 63 << 63 | a << 12 >> 16 | + (uint64_t)!!(a << 12) << 47); + else if (a << 1 >> 53 == 0) { + uint64_t adj = 0; + while (!(a << 1 >> 1 >> (52 - adj))) + adj++; + x.x0 <<= adj; + x.x1 = a >> 63 << 63 | (15360 - adj + 1) << 48 | a << adj << 12 >> 16; + } else + x.x1 = a >> 63 << 63 | ((a >> 52 & 2047) + 15360) << 48 | a << 12 >> 16; + memcpy(&fx, &x, 16); + return fx; +} + +float __trunctfsf2(long double f) +{ + u128_t mnt; + int32_t exp; + int sgn; + uint32_t x; + float fx; + + f3_unpack(&sgn, &exp, &mnt, f); + + if (exp == 32767 && (mnt.x0 | mnt.x1 << 16)) + x = 0x7fc00000 | (uint32_t)sgn << 31 | (mnt.x1 >> 25 & 0x007fffff); + else if (exp > 16510) + x = 0x7f800000 | (uint32_t)sgn << 31; + else if (exp < 16233) + x = (uint32_t)sgn << 31; + else { + exp -= 16257; + x = mnt.x1 >> 23 | !!(mnt.x0 | mnt.x1 << 41); + if (exp < 0) { + x = x >> -exp | !!(x << (32 + exp)); + exp = 0; + } + if ((x & 3) == 3 || (x & 7) == 6) + x += 4; + x = ((x >> 2) + (exp << 23)) | (uint32_t)sgn << 31; + } + memcpy(&fx, &x, 4); + return fx; +} + +double __trunctfdf2(long double f) +{ + u128_t mnt; + int32_t exp; + int sgn; + uint64_t x; + double fx; + + f3_unpack(&sgn, &exp, &mnt, f); + + if (exp == 32767 && (mnt.x0 | mnt.x1 << 16)) + x = (0x7ff8000000000000 | (uint64_t)sgn << 63 | + mnt.x1 << 16 >> 12 | mnt.x0 >> 60); + else if (exp > 17406) + x = 0x7ff0000000000000 | (uint64_t)sgn << 63; + else if (exp < 15308) + x = (uint64_t)sgn << 63; + else { + exp -= 15361; + x = mnt.x1 << 6 | mnt.x0 >> 58 | !!(mnt.x0 << 6); + if (exp < 0) { + x = x >> -exp | !!(x << (64 + exp)); + exp = 0; + } + if ((x & 3) == 3 || (x & 7) == 6) + x += 4; + x = ((x >> 2) + ((uint64_t)exp << 52)) | (uint64_t)sgn << 63; + } + memcpy(&fx, &x, 8); + return fx; +} + +int32_t __fixtfsi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + int32_t x; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_exp < 16369) + return 0; + if (a_exp > 16413) + return a_sgn ? -0x80000000 : 0x7fffffff; + x = a.x1 >> (16431 - a_exp); + return a_sgn ? -x : x; +} + +int64_t __fixtfdi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + int64_t x; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_exp < 16383) + return 0; + if (a_exp > 16445) + return a_sgn ? -0x8000000000000000 : 0x7fffffffffffffff; + x = (a.x1 << 15 | a.x0 >> 49) >> (16446 - a_exp); + return a_sgn ? -x : x; +} + +uint32_t __fixunstfsi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_sgn || a_exp < 16369) + return 0; + if (a_exp > 16414) + return -1; + return a.x1 >> (16431 - a_exp); +} + +uint64_t __fixunstfdi(long double fa) +{ + u128_t a; + int32_t a_exp; + int a_sgn; + f3_unpack(&a_sgn, &a_exp, &a, fa); + if (a_sgn || a_exp < 16383) + return 0; + if (a_exp > 16446) + return -1; + return (a.x1 << 15 | a.x0 >> 49) >> (16446 - a_exp); +} + +long double __floatsitf(int32_t a) +{ + int sgn = 0; + int exp = 16414; + uint32_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + if (a < 0) { + sgn = 1; + mnt = -mnt; + } + for (i = 16; i; i >>= 1) + if (!(mnt >> (32 - i))) { + mnt <<= i; + exp -= i; + } + x.x1 = ((uint64_t)sgn << 63 | (uint64_t)exp << 48 | + (uint64_t)(mnt << 1) << 16); + } + memcpy(&f, &x, 16); + return f; +} + +long double __floatditf(int64_t a) +{ + int sgn = 0; + int exp = 16446; + uint64_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + if (a < 0) { + sgn = 1; + mnt = -mnt; + } + for (i = 32; i; i >>= 1) + if (!(mnt >> (64 - i))) { + mnt <<= i; + exp -= i; + } + x.x0 = mnt << 49; + x.x1 = (uint64_t)sgn << 63 | (uint64_t)exp << 48 | mnt << 1 >> 16; + } + memcpy(&f, &x, 16); + return f; +} + +long double __floatunsitf(uint32_t a) +{ + int exp = 16414; + uint32_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + for (i = 16; i; i >>= 1) + if (!(mnt >> (32 - i))) { + mnt <<= i; + exp -= i; + } + x.x1 = (uint64_t)exp << 48 | (uint64_t)(mnt << 1) << 16; + } + memcpy(&f, &x, 16); + return f; +} + +long double __floatunditf(uint64_t a) +{ + int exp = 16446; + uint64_t mnt = a; + u128_t x = { 0, 0 }; + long double f; + int i; + if (a) { + for (i = 32; i; i >>= 1) + if (!(mnt >> (64 - i))) { + mnt <<= i; + exp -= i; + } + x.x0 = mnt << 49; + x.x1 = (uint64_t)exp << 48 | mnt << 1 >> 16; + } + memcpy(&f, &x, 16); + return f; +} + +static int f3_cmp(long double fa, long double fb) +{ + u128_t a, b; + memcpy(&a, &fa, 16); + memcpy(&b, &fb, 16); + return (!(a.x0 | a.x1 << 1 | b.x0 | b.x1 << 1) ? 0 : + ((a.x1 << 1 >> 49 == 0x7fff && (a.x0 | a.x1 << 16)) || + (b.x1 << 1 >> 49 == 0x7fff && (b.x0 | b.x1 << 16))) ? 2 : + a.x1 >> 63 != b.x1 >> 63 ? (int)(b.x1 >> 63) - (int)(a.x1 >> 63) : + a.x1 < b.x1 ? (int)(a.x1 >> 63 << 1) - 1 : + a.x1 > b.x1 ? 1 - (int)(a.x1 >> 63 << 1) : + a.x0 < b.x0 ? (int)(a.x1 >> 63 << 1) - 1 : + b.x0 < a.x0 ? 1 - (int)(a.x1 >> 63 << 1) : 0); +} + +int __eqtf2(long double a, long double b) +{ + return !!f3_cmp(a, b); +} + +int __netf2(long double a, long double b) +{ + return !!f3_cmp(a, b); +} + +int __lttf2(long double a, long double b) +{ + return f3_cmp(a, b); +} + +int __letf2(long double a, long double b) +{ + return f3_cmp(a, b); +} + +int __gttf2(long double a, long double b) +{ + return -f3_cmp(b, a); +} + +int __getf2(long double a, long double b) +{ + return -f3_cmp(b, a); +} diff --git a/05/tcc-final/lib/libtcc1.c b/05/tcc-final/lib/libtcc1.c new file mode 100644 index 0000000..ae94af1 --- /dev/null +++ b/05/tcc-final/lib/libtcc1.c @@ -0,0 +1,641 @@ +/* TCC runtime library. + Parts of this code are (c) 2002 Fabrice Bellard + + Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc. + +This file is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +In addition to the permissions in the GNU General Public License, the +Free Software Foundation gives you unlimited permission to link the +compiled version of this file into combinations with other programs, +and to distribute those combinations without any restriction coming +from the use of this file. (The General Public License restrictions +do apply in other respects; for example, they cover modification of +the file, and distribution when not linked into a combine +executable.) + +This file is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING. If not, write to +the Free Software Foundation, 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +*/ + +#define W_TYPE_SIZE 32 +#define BITS_PER_UNIT 8 + +typedef int Wtype; +typedef unsigned int UWtype; +typedef unsigned int USItype; +typedef long long DWtype; +typedef unsigned long long UDWtype; + +struct DWstruct { + Wtype low, high; +}; + +typedef union +{ + struct DWstruct s; + DWtype ll; +} DWunion; + +typedef long double XFtype; +#define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT) +#define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE) + +/* the following deal with IEEE single-precision numbers */ +#define EXCESS 126 +#define SIGNBIT 0x80000000 +#define HIDDEN (1 << 23) +#define SIGN(fp) ((fp) & SIGNBIT) +#define EXP(fp) (((fp) >> 23) & 0xFF) +#define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN) +#define PACK(s,e,m) ((s) | ((e) << 23) | (m)) + +/* the following deal with IEEE double-precision numbers */ +#define EXCESSD 1022 +#define HIDDEND (1 << 20) +#define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF) +#define SIGND(fp) ((fp.l.upper) & SIGNBIT) +#define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \ + (fp.l.lower >> 22)) +#define HIDDEND_LL ((long long)1 << 52) +#define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL) +#define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m)) + +/* the following deal with x86 long double-precision numbers */ +#define EXCESSLD 16382 +#define EXPLD(fp) (fp.l.upper & 0x7fff) +#define SIGNLD(fp) ((fp.l.upper) & 0x8000) + +/* only for x86 */ +union ldouble_long { + long double ld; + struct { + unsigned long long lower; + unsigned short upper; + } l; +}; + +union double_long { + double d; +#if 1 + struct { + unsigned int lower; + int upper; + } l; +#else + struct { + int upper; + unsigned int lower; + } l; +#endif + long long ll; +}; + +union float_long { + float f; + unsigned int l; +}; + +/* XXX: we don't support several builtin supports for now */ +#if !defined __x86_64__ && !defined __arm__ + +/* XXX: use gcc/tcc intrinsic ? */ +#if defined __i386__ +#define sub_ddmmss(sh, sl, ah, al, bh, bl) \ + __asm__ ("subl %5,%1\n\tsbbl %3,%0" \ + : "=r" ((USItype) (sh)), \ + "=&r" ((USItype) (sl)) \ + : "0" ((USItype) (ah)), \ + "g" ((USItype) (bh)), \ + "1" ((USItype) (al)), \ + "g" ((USItype) (bl))) +#define umul_ppmm(w1, w0, u, v) \ + __asm__ ("mull %3" \ + : "=a" ((USItype) (w0)), \ + "=d" ((USItype) (w1)) \ + : "%0" ((USItype) (u)), \ + "rm" ((USItype) (v))) +#define udiv_qrnnd(q, r, n1, n0, dv) \ + __asm__ ("divl %4" \ + : "=a" ((USItype) (q)), \ + "=d" ((USItype) (r)) \ + : "0" ((USItype) (n0)), \ + "1" ((USItype) (n1)), \ + "rm" ((USItype) (dv))) +#define count_leading_zeros(count, x) \ + do { \ + USItype __cbtmp; \ + __asm__ ("bsrl %1,%0" \ + : "=r" (__cbtmp) : "rm" ((USItype) (x))); \ + (count) = __cbtmp ^ 31; \ + } while (0) +#else +#error unsupported CPU type +#endif + +/* most of this code is taken from libgcc2.c from gcc */ + +static UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp) +{ + DWunion ww; + DWunion nn, dd; + DWunion rr; + UWtype d0, d1, n0, n1, n2; + UWtype q0, q1; + UWtype b, bm; + + nn.ll = n; + dd.ll = d; + + d0 = dd.s.low; + d1 = dd.s.high; + n0 = nn.s.low; + n1 = nn.s.high; + +#if !defined(UDIV_NEEDS_NORMALIZATION) + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + udiv_qrnnd (q1, n1, 0, n1, d0); + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0. */ + } + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = 0; + *rp = rr.ll; + } + } + +#else /* UDIV_NEEDS_NORMALIZATION */ + + if (d1 == 0) + { + if (d0 > n1) + { + /* 0q = nn / 0D */ + + count_leading_zeros (bm, d0); + + if (bm != 0) + { + /* Normalize, i.e. make the most significant bit of the + denominator set. */ + + d0 = d0 << bm; + n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm)); + n0 = n0 << bm; + } + + udiv_qrnnd (q0, n0, n1, n0, d0); + q1 = 0; + + /* Remainder in n0 >> bm. */ + } + else + { + /* qq = NN / 0d */ + + if (d0 == 0) + d0 = 1 / d0; /* Divide intentionally by zero. */ + + count_leading_zeros (bm, d0); + + if (bm == 0) + { + /* From (n1 >= d0) /\ (the most significant bit of d0 is set), + conclude (the most significant bit of n1 is set) /\ (the + leading quotient digit q1 = 1). + + This special case is necessary, not an optimization. + (Shifts counts of W_TYPE_SIZE are undefined.) */ + + n1 -= d0; + q1 = 1; + } + else + { + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q1, n1, n2, n1, d0); + } + + /* n1 != d0... */ + + udiv_qrnnd (q0, n0, n1, n0, d0); + + /* Remainder in n0 >> bm. */ + } + + if (rp != 0) + { + rr.s.low = n0 >> bm; + rr.s.high = 0; + *rp = rr.ll; + } + } +#endif /* UDIV_NEEDS_NORMALIZATION */ + + else + { + if (d1 > n1) + { + /* 00 = nn / DD */ + + q0 = 0; + q1 = 0; + + /* Remainder in n1n0. */ + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + /* 0q = NN / dd */ + + count_leading_zeros (bm, d1); + if (bm == 0) + { + /* From (n1 >= d1) /\ (the most significant bit of d1 is set), + conclude (the most significant bit of n1 is set) /\ (the + quotient digit q0 = 0 or 1). + + This special case is necessary, not an optimization. */ + + /* The condition on the next line takes advantage of that + n1 >= d1 (true due to program flow). */ + if (n1 > d1 || n0 >= d0) + { + q0 = 1; + sub_ddmmss (n1, n0, n1, n0, d1, d0); + } + else + q0 = 0; + + q1 = 0; + + if (rp != 0) + { + rr.s.low = n0; + rr.s.high = n1; + *rp = rr.ll; + } + } + else + { + UWtype m1, m0; + /* Normalize. */ + + b = W_TYPE_SIZE - bm; + + d1 = (d1 << bm) | (d0 >> b); + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + udiv_qrnnd (q0, n1, n2, n1, d1); + umul_ppmm (m1, m0, q0, d0); + + if (m1 > n1 || (m1 == n1 && m0 > n0)) + { + q0--; + sub_ddmmss (m1, m0, m1, m0, d1, d0); + } + + q1 = 0; + + /* Remainder in (n1n0 - m1m0) >> bm. */ + if (rp != 0) + { + sub_ddmmss (n1, n0, n1, n0, m1, m0); + rr.s.low = (n1 << b) | (n0 >> bm); + rr.s.high = n1 >> bm; + *rp = rr.ll; + } + } + } + } + + ww.s.low = q0; + ww.s.high = q1; + return ww.ll; +} + +#define __negdi2(a) (-(a)) + +long long __divdi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) { + c = ~c; + vv.ll = __negdi2 (vv.ll); + } + w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0); + if (c) + w = __negdi2 (w); + return w; +} + +long long __moddi3(long long u, long long v) +{ + int c = 0; + DWunion uu, vv; + DWtype w; + + uu.ll = u; + vv.ll = v; + + if (uu.s.high < 0) { + c = ~c; + uu.ll = __negdi2 (uu.ll); + } + if (vv.s.high < 0) + vv.ll = __negdi2 (vv.ll); + + __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) &w); + if (c) + w = __negdi2 (w); + return w; +} + +unsigned long long __udivdi3(unsigned long long u, unsigned long long v) +{ + return __udivmoddi4 (u, v, (UDWtype *) 0); +} + +unsigned long long __umoddi3(unsigned long long u, unsigned long long v) +{ + UDWtype w; + + __udivmoddi4 (u, v, &w); + return w; +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashrdi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = u.s.high >> (b - 32); + u.s.high = u.s.high >> 31; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +unsigned long long __lshrdi3(unsigned long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.low = (unsigned)u.s.high >> (b - 32); + u.s.high = 0; + } else if (b != 0) { + u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b)); + u.s.high = (unsigned)u.s.high >> b; + } + return u.ll; +#else + return a >> b; +#endif +} + +/* XXX: fix tcc's code generator to do this instead */ +long long __ashldi3(long long a, int b) +{ +#ifdef __TINYC__ + DWunion u; + u.ll = a; + if (b >= 32) { + u.s.high = (unsigned)u.s.low << (b - 32); + u.s.low = 0; + } else if (b != 0) { + u.s.high = ((unsigned)u.s.high << b) | ((unsigned)u.s.low >> (32 - b)); + u.s.low = (unsigned)u.s.low << b; + } + return u.ll; +#else + return a << b; +#endif +} + +#endif /* !__x86_64__ */ + +/* XXX: fix tcc's code generator to do this instead */ +float __floatundisf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (float)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (float)r; + } +} + +double __floatundidf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (double)r; + } +} + +long double __floatundixf(unsigned long long a) +{ + DWunion uu; + XFtype r; + + uu.ll = a; + if (uu.s.high >= 0) { + return (long double)uu.ll; + } else { + r = (XFtype)uu.ll; + r += 18446744073709551616.0; + return (long double)r; + } +} + +unsigned long long __fixunssfdi (float a1) +{ + register union float_long fl1; + register int exp; + register unsigned long long l; + + fl1.f = a1; + + if (fl1.l == 0) + return (0); + + exp = EXP (fl1.l) - EXCESS - 24; + l = MANT(fl1.l); + + if (exp >= 41) + return 1ULL << 63; + else if (exp >= 0) + l <<= exp; + else if (exp >= -23) + l >>= -exp; + else + return 0; + if (SIGN(fl1.l)) + l = (unsigned long long)-l; + return l; +} + +long long __fixsfdi (float a1) +{ + long long ret; int s; + ret = __fixunssfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +unsigned long long __fixunsdfdi (double a1) +{ + register union double_long dl1; + register int exp; + register unsigned long long l; + + dl1.d = a1; + + if (dl1.ll == 0) + return (0); + + exp = EXPD (dl1) - EXCESSD - 53; + l = MANTD_LL(dl1); + + if (exp >= 12) + return 1ULL << 63; /* overflow result (like gcc, somewhat) */ + else if (exp >= 0) + l <<= exp; + else if (exp >= -52) + l >>= -exp; + else + return 0; + if (SIGND(dl1)) + l = (unsigned long long)-l; + return l; +} + +long long __fixdfdi (double a1) +{ + long long ret; int s; + ret = __fixunsdfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +#ifndef __arm__ +unsigned long long __fixunsxfdi (long double a1) +{ + register union ldouble_long dl1; + register int exp; + register unsigned long long l; + + dl1.ld = a1; + + if (dl1.l.lower == 0 && dl1.l.upper == 0) + return (0); + + exp = EXPLD (dl1) - EXCESSLD - 64; + l = dl1.l.lower; + if (exp > 0) + return 1ULL << 63; + if (exp < -63) + return 0; + l >>= -exp; + if (SIGNLD(dl1)) + l = (unsigned long long)-l; + return l; +} + +long long __fixxfdi (long double a1) +{ + long long ret; int s; + ret = __fixunsxfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} +#endif /* !ARM */ + +#if defined __x86_64__ +/* float constants used for unary minus operation */ +const float __mzerosf = -0.0; +const double __mzerodf = -0.0; +#endif + +#if defined _WIN64 +/* MSVC x64 intrinsic */ +void __faststorefence(void) +{ + __asm__("lock; orl $0,(%rsp)"); +} +#endif diff --git a/05/tcc-final/lib/stdatomic.c b/05/tcc-final/lib/stdatomic.c new file mode 100644 index 0000000..eea7085 --- /dev/null +++ b/05/tcc-final/lib/stdatomic.c @@ -0,0 +1,124 @@ +// for libtcc1, avoid including files that are not part of tcc +// #include +#define uint8_t unsigned char +#define uint16_t unsigned short +#define uint32_t unsigned int +#define uint64_t unsigned long long +#define bool _Bool +#define true 1 +#define __ATOMIC_RELAXED 0 +#define __ATOMIC_CONSUME 1 +#define __ATOMIC_ACQUIRE 2 +#define __ATOMIC_RELEASE 3 +#define __ATOMIC_ACQ_REL 4 +#define __ATOMIC_SEQ_CST 5 + +#define ATOMIC_X86_COMPARE_EXCHANGE(TYPE, MODE, SUFFIX) \ + bool __atomic_compare_exchange_##MODE \ + (volatile void *atom, void *ref, TYPE xchg, \ + bool weak, int success_memorder, int failure_memorder) \ + { \ + TYPE rv; \ + TYPE cmp = *(TYPE *)ref; \ + asm volatile( \ + "lock cmpxchg" SUFFIX " %2,%1\n" \ + : "=a" (rv), "+m" (*(TYPE *)atom) \ + : "q" (xchg), "0" (cmp) \ + : "memory" \ + ); \ + *(TYPE *)ref = rv; \ + return (rv == cmp); \ + } + +#define ATOMIC_X86_LOAD(TYPE, MODE) \ + TYPE __atomic_load_##MODE(const volatile void *atom, int memorder) \ + { \ + return *(volatile TYPE *)atom; \ + } + +#define ATOMIC_X86_STORE(TYPE, MODE) \ + void __atomic_store_##MODE(volatile void *atom, TYPE value, int memorder) \ + { \ + *(volatile TYPE *)atom = value; \ + } + +/* Some tcc targets set __GNUC__ */ +#if defined(__GNUC__) && !defined(__TINYC__) +#define ATOMIC_LOAD(t,a,b,c) t b; __atomic_load((t *)a, (t *)&b, c) +#define COMPARE_EXCHANGE(t,a,b,c,d,e,f) __atomic_compare_exchange((t *)a,b,&c,d,e,f) +#else +#define ATOMIC_LOAD(t,a,b,c) t b = __atomic_load((t *)a, c) +#define COMPARE_EXCHANGE(t,a,b,c,d,e,f) __atomic_compare_exchange((t *)a,b,c,d,e,f) +#endif + +#define ATOMIC_GEN_OP(TYPE, MODE, NAME, OP) \ + TYPE __atomic_##NAME##_##MODE(volatile void *atom, TYPE value, int memorder) \ + { \ + TYPE xchg; \ + ATOMIC_LOAD(TYPE, atom, cmp, __ATOMIC_RELAXED); \ + do { \ + xchg = (OP); \ + } while (!COMPARE_EXCHANGE(TYPE, atom, &cmp, xchg, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); \ + return cmp; \ + } + +#define ATOMIC_EXCHANGE(TYPE, MODE) \ + ATOMIC_GEN_OP(TYPE, MODE, exchange, value) +#define ATOMIC_FETCH_ADD(TYPE, MODE) \ + ATOMIC_GEN_OP(TYPE, MODE, fetch_add, (cmp + value)) +#define ATOMIC_FETCH_SUB(TYPE, MODE) \ + ATOMIC_GEN_OP(TYPE, MODE, fetch_sub, (cmp - value)) +#define ATOMIC_FETCH_AND(TYPE, MODE) \ + ATOMIC_GEN_OP(TYPE, MODE, fetch_and, (cmp & value)) +#define ATOMIC_FETCH_OR(TYPE, MODE) \ + ATOMIC_GEN_OP(TYPE, MODE, fetch_or, (cmp | value)) +#define ATOMIC_FETCH_XOR(TYPE, MODE) \ + ATOMIC_GEN_OP(TYPE, MODE, fetch_xor, (cmp ^ value)) + +ATOMIC_X86_STORE(uint8_t, 1) +ATOMIC_X86_STORE(uint16_t, 2) +ATOMIC_X86_STORE(uint32_t, 4) + +ATOMIC_X86_LOAD(uint8_t, 1) +ATOMIC_X86_LOAD(uint16_t, 2) +ATOMIC_X86_LOAD(uint32_t, 4) + +ATOMIC_X86_COMPARE_EXCHANGE(uint8_t, 1, "b") +ATOMIC_X86_COMPARE_EXCHANGE(uint16_t, 2, "w") +ATOMIC_X86_COMPARE_EXCHANGE(uint32_t, 4, "l") + +ATOMIC_EXCHANGE(uint8_t, 1) +ATOMIC_EXCHANGE(uint16_t, 2) +ATOMIC_EXCHANGE(uint32_t, 4) + +ATOMIC_FETCH_ADD(uint8_t, 1) +ATOMIC_FETCH_ADD(uint16_t, 2) +ATOMIC_FETCH_ADD(uint32_t, 4) + +ATOMIC_FETCH_SUB(uint8_t, 1) +ATOMIC_FETCH_SUB(uint16_t, 2) +ATOMIC_FETCH_SUB(uint32_t, 4) + +ATOMIC_FETCH_AND(uint8_t, 1) +ATOMIC_FETCH_AND(uint16_t, 2) +ATOMIC_FETCH_AND(uint32_t, 4) + +ATOMIC_FETCH_OR(uint8_t, 1) +ATOMIC_FETCH_OR(uint16_t, 2) +ATOMIC_FETCH_OR(uint32_t, 4) + +ATOMIC_FETCH_XOR(uint8_t, 1) +ATOMIC_FETCH_XOR(uint16_t, 2) +ATOMIC_FETCH_XOR(uint32_t, 4) + +#if defined __x86_64__ +ATOMIC_X86_STORE(uint64_t, 8) +ATOMIC_X86_LOAD(uint64_t, 8) +ATOMIC_X86_COMPARE_EXCHANGE(uint64_t, 8, "q") +ATOMIC_EXCHANGE(uint64_t, 8) +ATOMIC_FETCH_ADD(uint64_t, 8) +ATOMIC_FETCH_SUB(uint64_t, 8) +ATOMIC_FETCH_AND(uint64_t, 8) +ATOMIC_FETCH_OR(uint64_t, 8) +ATOMIC_FETCH_XOR(uint64_t, 8) +#endif diff --git a/05/tcc-final/lib/tcov.c b/05/tcc-final/lib/tcov.c new file mode 100644 index 0000000..e5b9f22 --- /dev/null +++ b/05/tcc-final/lib/tcov.c @@ -0,0 +1,428 @@ +#include +#include +#include +#include +#ifndef _WIN32 +#include +#include +#else +#include +#include +#endif + +/* section layout (all little endian): + 32bit offset to executable/so file name + filename \0 + function name \0 + align to 64 bits + 64bit function start line + 64bits end_line(28bits) / start_line(28bits) / flag=0xff(8bits) + 64bits counter + \0 + \0 + \0 + executable/so file name \0 + */ + +typedef struct tcov_line { + unsigned int fline; + unsigned int lline; + unsigned long long count; +} tcov_line; + +typedef struct tcov_function { + char *function; + unsigned int first_line; + unsigned int n_line; + unsigned int m_line; + tcov_line *line; +} tcov_function; + +typedef struct tcov_file { + char *filename; + unsigned int n_func; + unsigned int m_func; + tcov_function *func; + struct tcov_file *next; +} tcov_file; + +static FILE *open_tcov_file (char *cov_filename) +{ + int fd; +#ifndef _WIN32 + struct flock lock; + + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; + lock.l_start = 0; + lock.l_len = 0; /* Until EOF. */ + lock.l_pid = getpid (); +#endif + fd = open (cov_filename, O_RDWR | O_CREAT, 0666); + if (fd < 0) + return NULL; + +#ifndef _WIN32 + while (fcntl (fd, F_SETLKW, &lock) && errno == EINTR) + continue; +#else + { + OVERLAPPED overlapped = { 0 }; + LockFileEx((HANDLE)_get_osfhandle(fd), LOCKFILE_EXCLUSIVE_LOCK, + 0, 1, 0, &overlapped); + } +#endif + + return fdopen (fd, "r+"); +} + +static unsigned long long get_value(unsigned char *p, int size) +{ + unsigned long long value = 0; + + p += size; + while (size--) + value = (value << 8) | *--p; + return value; +} + +static int sort_func (const void *p, const void *q) +{ + const tcov_function *pp = (const tcov_function *) p; + const tcov_function *pq = (const tcov_function *) q; + + return pp->first_line > pq->first_line ? 1 : + pp->first_line < pq->first_line ? -1 : 0; +} + +static int sort_line (const void *p, const void *q) +{ + const tcov_line *pp = (const tcov_line *) p; + const tcov_line *pq = (const tcov_line *) q; + + return pp->fline > pq->fline ? 1 : + pp->fline < pq->fline ? -1 : + pp->count < pq->count ? 1 : + pp->count > pq->count ? -1 : 0; +} + +/* sort to let inline functions work */ +static tcov_file *sort_test_coverage (unsigned char *p) +{ + int i, j, k; + unsigned char *start = p; + tcov_file *file = NULL; + tcov_file *nfile; + + p += 4; + while (*p) { + char *filename = (char *)p; + size_t len = strlen (filename); + + nfile = file; + while (nfile) { + if (strcmp (nfile->filename, filename) == 0) + break; + nfile = nfile->next; + } + if (nfile == NULL) { + nfile = malloc (sizeof(tcov_file)); + if (nfile == NULL) { + fprintf (stderr, "Malloc error test_coverage\n"); + return file; + } + nfile->filename = filename; + nfile->n_func = 0; + nfile->m_func = 0; + nfile->func = NULL; + nfile->next = NULL; + if (file == NULL) + file = nfile; + else { + tcov_file *lfile = file; + + while (lfile->next) + lfile = lfile->next; + lfile->next = nfile; + } + } + p += len + 1; + while (*p) { + int i; + char *function = (char *)p; + tcov_function *func; + + p += strlen (function) + 1; + p += -(p - start) & 7; + for (i = 0; i < nfile->n_func; i++) { + func = &nfile->func[i]; + if (strcmp (func->function, function) == 0) + break; + } + if (i == nfile->n_func) { + if (nfile->n_func >= nfile->m_func) { + nfile->m_func = nfile->m_func == 0 ? 4 : nfile->m_func * 2; + nfile->func = realloc (nfile->func, + nfile->m_func * + sizeof (tcov_function)); + if (nfile->func == NULL) { + fprintf (stderr, "Realloc error test_coverage\n"); + return file; + } + } + func = &nfile->func[nfile->n_func++]; + func->function = function; + func->first_line = get_value (p, 8); + func->n_line = 0; + func->m_line = 0; + func->line = NULL; + } + p += 8; + while (*p) { + tcov_line *line; + unsigned long long val; + + if (func->n_line >= func->m_line) { + func->m_line = func->m_line == 0 ? 4 : func->m_line * 2; + func->line = realloc (func->line, + func->m_line * sizeof (tcov_line)); + if (func->line == NULL) { + fprintf (stderr, "Realloc error test_coverage\n"); + return file; + } + } + line = &func->line[func->n_line++]; + val = get_value (p, 8); + line->fline = (val >> 8) & 0xfffffffULL; + line->lline = val >> 36; + line->count = get_value (p + 8, 8); + p += 16; + } + p++; + } + p++; + } + nfile = file; + while (nfile) { + qsort (nfile->func, nfile->n_func, sizeof (tcov_function), sort_func); + for (i = 0; i < nfile->n_func; i++) { + tcov_function *func = &nfile->func[i]; + qsort (func->line, func->n_line, sizeof (tcov_line), sort_line); + } + nfile = nfile->next; + } + return file; +} + +/* merge with previous tcov file */ +static void merge_test_coverage (tcov_file *file, FILE *fp, + unsigned int *pruns) +{ + unsigned int runs; + char *p; + char str[10000]; + + *pruns = 1; + if (fp == NULL) + return; + if (fgets(str, sizeof(str), fp) && + (p = strrchr (str, ':')) && + (sscanf (p + 1, "%u", &runs) == 1)) + *pruns = runs + 1; + while (file) { + int i; + size_t len = strlen (file->filename); + + while (fgets(str, sizeof(str), fp) && + (p = strstr(str, "0:File:")) == NULL); + if ((p = strstr(str, "0:File:")) == NULL || + strncmp (p + strlen("0:File:"), file->filename, len) != 0 || + p[strlen("0:File:") + len] != ' ') + break; + for (i = 0; i < file->n_func; i++) { + int j; + tcov_function *func = &file->func[i]; + unsigned int next_zero = 0; + unsigned int curline = 0; + + for (j = 0; j < func->n_line; j++) { + tcov_line *line = &func->line[j]; + unsigned int fline = line->fline; + unsigned long long count; + unsigned int tmp; + char c; + + while (curline < fline && + fgets(str, sizeof(str), fp)) + if ((p = strchr(str, ':')) && + sscanf (p + 1, "%u", &tmp) == 1) + curline = tmp; + if (sscanf (str, "%llu%c\n", &count, &c) == 2) { + if (next_zero == 0) + line->count += count; + next_zero = c == '*'; + } + } + } + file = file->next; + } +} + +/* store tcov data in file */ +void __store_test_coverage (unsigned char * p) +{ + int i, j; + unsigned int files; + unsigned int funcs; + unsigned int blocks; + unsigned int blocks_run; + unsigned int runs; + char *cov_filename = (char *)p + get_value (p, 4); + FILE *fp; + char *q; + tcov_file *file; + tcov_file *nfile; + tcov_function *func; + + fp = open_tcov_file (cov_filename); + if (fp == NULL) { + fprintf (stderr, "Cannot create coverage file: %s\n", cov_filename); + return; + } + file = sort_test_coverage (p); + merge_test_coverage (file, fp, &runs); + fseek (fp, 0, SEEK_SET); + fprintf (fp, " -: 0:Runs:%u\n", runs); + files = 0; + funcs = 0; + blocks = 0; + blocks_run = 0; + nfile = file; + while (nfile) { + files++; + for (i = 0; i < nfile->n_func; i++) { + func = &nfile->func[i]; + funcs++; + for (j = 0; j < func->n_line; j++) { + blocks++; + blocks_run += func->line[j].count != 0; + } + } + nfile = nfile->next; + } + if (blocks == 0) + blocks = 1; + fprintf (fp, " -: 0:All:%s Files:%u Functions:%u %.02f%%\n", + cov_filename, files, funcs, 100.0 * (double) blocks_run / blocks); + nfile = file; + while (nfile) { + FILE *src = fopen (nfile->filename, "r"); + unsigned int curline = 1; + char str[10000]; + + if (src == NULL) + goto next; + funcs = 0; + blocks = 0; + blocks_run = 0; + for (i = 0; i < nfile->n_func; i++) { + func = &nfile->func[i]; + funcs++; + for (j = 0; j < func->n_line; j++) { + blocks++; + blocks_run += func->line[j].count != 0; + } + } + if (blocks == 0) + blocks = 1; + fprintf (fp, " -: 0:File:%s Functions:%u %.02f%%\n", + nfile->filename, funcs, 100.0 * (double) blocks_run / blocks); + for (i = 0; i < nfile->n_func; i++) { + func = &nfile->func[i]; + + while (curline < func->first_line) + if (fgets(str, sizeof(str), src)) + fprintf (fp, " -:%5u:%s", curline++, str); + blocks = 0; + blocks_run = 0; + for (j = 0; j < func->n_line; j++) { + blocks++; + blocks_run += func->line[j].count != 0; + } + if (blocks == 0) + blocks = 1; + fprintf (fp, " -: 0:Function:%s %.02f%%\n", + func->function, 100.0 * (double) blocks_run / blocks); +#if 0 + for (j = 0; j < func->n_line; j++) { + unsigned int fline = func->line[j].fline; + unsigned int lline = func->line[j].lline; + unsigned long long count = func->line[j].count; + + fprintf (fp, "%u %u %llu\n", fline, lline, count); + } +#endif + for (j = 0; j < func->n_line;) { + unsigned int fline = func->line[j].fline; + unsigned int lline = func->line[j].lline; + unsigned long long count = func->line[j].count; + unsigned int has_zero = 0; + unsigned int same_line = fline == lline; + + j++; + while (j < func->n_line) { + unsigned int nfline = func->line[j].fline; + unsigned int nlline = func->line[j].lline; + unsigned long long ncount = func->line[j].count; + + if (fline == nfline) { + if (ncount == 0) + has_zero = 1; + else if (ncount > count) + count = ncount; + same_line = nfline == nlline; + lline = nlline; + j++; + } + else + break; + } + if (same_line) + lline++; + + while (curline < fline) + if (fgets(str, sizeof(str), src)) + fprintf (fp, " -:%5u:%s", curline++, str); + while (curline < lline && + fgets(str, sizeof(str), src)) { + if (count == 0) + fprintf (fp, " #####:%5u:%s", + curline, str); + else if (has_zero) + fprintf (fp, "%8llu*:%5u:%s", + count, curline, str); + else + fprintf (fp, "%9llu:%5u:%s", + count, curline, str); + curline++; + } + } + } + while (fgets(str, sizeof(str), src)) + fprintf (fp, " -:%5u:%s", curline++, str); + fclose (src); +next: + nfile = nfile->next; + } + while (file) { + for (i = 0; i < file->n_func; i++) { + func = &file->func[i]; + free (func->line); + } + free (file->func); + nfile = file; + file = file->next; + free (nfile); + } + fclose (fp); +} diff --git a/05/tcc-final/lib/va_list.c b/05/tcc-final/lib/va_list.c new file mode 100644 index 0000000..1fb5512 --- /dev/null +++ b/05/tcc-final/lib/va_list.c @@ -0,0 +1,67 @@ +/* va_list.c - tinycc support for va_list on X86_64 */ + +#if defined __x86_64__ + +/* Avoid include files, they may not be available when cross compiling */ +extern void abort(void); + +/* This should be in sync with our include/stdarg.h */ +enum __va_arg_type { + __va_gen_reg, __va_float_reg, __va_stack +}; + +/* GCC compatible definition of va_list. */ +/*predefined by TCC (tcc_predefs.h): +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __builtin_va_list[1]; +*/ + +extern void *memcpy(void *dest, const void *src, unsigned long n); + +void *__va_arg(__builtin_va_list ap, + int arg_type, + int size, int align) +{ + size = (size + 7) & ~7; + align = (align + 7) & ~7; + switch ((enum __va_arg_type)arg_type) { + case __va_gen_reg: + if (ap->gp_offset + size <= 48) { + ap->gp_offset += size; + return ap->reg_save_area + ap->gp_offset - size; + } + goto use_overflow_area; + + case __va_float_reg: + if (ap->fp_offset < 128 + 48) { + ap->fp_offset += 16; + if (size == 8) + return ap->reg_save_area + ap->fp_offset - 16; + if (ap->fp_offset < 128 + 48) { + memcpy(ap->reg_save_area + ap->fp_offset - 8, + ap->reg_save_area + ap->fp_offset, 8); + ap->fp_offset += 16; + return ap->reg_save_area + ap->fp_offset - 32; + } + } + goto use_overflow_area; + + case __va_stack: + use_overflow_area: + ap->overflow_arg_area += size; + ap->overflow_arg_area = (char*)((long long)(ap->overflow_arg_area + align - 1) & -align); + return ap->overflow_arg_area - size; + + default: /* should never happen */ + abort(); + return 0; + } +} +#endif diff --git a/05/tcc-final/libtcc.c b/05/tcc-final/libtcc.c new file mode 100644 index 0000000..59f846c --- /dev/null +++ b/05/tcc-final/libtcc.c @@ -0,0 +1,2049 @@ +/* + * TCC - Tiny C Compiler + * + * Copyright (c) 2001-2004 Fabrice Bellard + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if !defined ONE_SOURCE || ONE_SOURCE +#include "tccpp.c" +#include "tccgen.c" +#include "tccdbg.c" +#include "tccasm.c" +#include "tccelf.c" +#include "tccrun.c" +#ifdef TCC_TARGET_I386 +#include "i386-gen.c" +#include "i386-link.c" +#include "i386-asm.c" +#elif defined(TCC_TARGET_ARM) +#include "arm-gen.c" +#include "arm-link.c" +#include "arm-asm.c" +#elif defined(TCC_TARGET_ARM64) +#include "arm64-gen.c" +#include "arm64-link.c" +#include "arm-asm.c" +#elif defined(TCC_TARGET_C67) +#include "c67-gen.c" +#include "c67-link.c" +#include "tcccoff.c" +#elif defined(TCC_TARGET_X86_64) +#include "x86_64-gen.c" +#include "x86_64-link.c" +#include "i386-asm.c" +#elif defined(TCC_TARGET_RISCV64) +#include "riscv64-gen.c" +#include "riscv64-link.c" +#include "riscv64-asm.c" +#else +#error unknown target +#endif +#ifdef TCC_TARGET_PE +#include "tccpe.c" +#endif +#ifdef TCC_TARGET_MACHO +#include "tccmacho.c" +#endif +#endif /* ONE_SOURCE */ + +#include "tcc.h" + +/********************************************************/ +/* global variables */ + +/* XXX: get rid of this ASAP (or maybe not) */ +ST_DATA struct TCCState *tcc_state; +TCC_SEM(static tcc_compile_sem); + +#ifdef MEM_DEBUG +static int nb_states; +#endif + +/********************************************************/ +#ifdef _WIN32 +ST_FUNC char *normalize_slashes(char *path) +{ + char *p; + for (p = path; *p; ++p) + if (*p == '\\') + *p = '/'; + return path; +} + +#if defined LIBTCC_AS_DLL && !defined CONFIG_TCCDIR +static HMODULE tcc_module; +BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) +{ + if (DLL_PROCESS_ATTACH == dwReason) + tcc_module = hDll; + return TRUE; +} +#else +#define tcc_module NULL /* NULL means executable itself */ +#endif + +#ifndef CONFIG_TCCDIR +/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */ +static inline char *config_tccdir_w32(char *path) +{ + char *p; + GetModuleFileName(tcc_module, path, MAX_PATH); + p = tcc_basename(normalize_slashes(strlwr(path))); + if (p > path) + --p; + *p = 0; + return path; +} +#define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH)) +#endif + +#ifdef TCC_TARGET_PE +static void tcc_add_systemdir(TCCState *s) +{ + char buf[1000]; + GetSystemDirectory(buf, sizeof buf); + tcc_add_library_path(s, normalize_slashes(buf)); +} +#endif +#endif + +/********************************************************/ +#if CONFIG_TCC_SEMLOCK +#if defined _WIN32 +ST_FUNC void wait_sem(TCCSem *p) +{ + if (!p->init) + InitializeCriticalSection(&p->cr), p->init = 1; + EnterCriticalSection(&p->cr); +} +ST_FUNC void post_sem(TCCSem *p) +{ + LeaveCriticalSection(&p->cr); +} +#elif defined __APPLE__ +/* Half-compatible MacOS doesn't have non-shared (process local) + semaphores. Use the dispatch framework for lightweight locks. */ +ST_FUNC void wait_sem(TCCSem *p) +{ + if (!p->init) + p->sem = dispatch_semaphore_create(1), p->init = 1; + dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER); +} +ST_FUNC void post_sem(TCCSem *p) +{ + dispatch_semaphore_signal(p->sem); +} +#else +ST_FUNC void wait_sem(TCCSem *p) +{ + if (!p->init) + sem_init(&p->sem, 0, 1), p->init = 1; + while (sem_wait(&p->sem) < 0 && errno == EINTR); +} +ST_FUNC void post_sem(TCCSem *p) +{ + sem_post(&p->sem); +} +#endif +#endif + +PUB_FUNC void tcc_enter_state(TCCState *s1) +{ + if (s1->error_set_jmp_enabled) + return; + WAIT_SEM(&tcc_compile_sem); + tcc_state = s1; +} + +PUB_FUNC void tcc_exit_state(TCCState *s1) +{ + if (s1->error_set_jmp_enabled) + return; + tcc_state = NULL; + POST_SEM(&tcc_compile_sem); +} + +/********************************************************/ +/* copy a string and truncate it. */ +ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s) +{ + char *q, *q_end; + int c; + + if (buf_size > 0) { + q = buf; + q_end = buf + buf_size - 1; + while (q < q_end) { + c = *s++; + if (c == '\0') + break; + *q++ = c; + } + *q = '\0'; + } + return buf; +} + +/* strcat and truncate. */ +ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s) +{ + size_t len; + len = strlen(buf); + if (len < buf_size) + pstrcpy(buf + len, buf_size - len, s); + return buf; +} + +ST_FUNC char *pstrncpy(char *out, const char *in, size_t num) +{ + memcpy(out, in, num); + out[num] = '\0'; + return out; +} + +/* extract the basename of a file */ +PUB_FUNC char *tcc_basename(const char *name) +{ + char *p = strchr(name, 0); + while (p > name && !IS_DIRSEP(p[-1])) + --p; + return p; +} + +/* extract extension part of a file + * + * (if no extension, return pointer to end-of-string) + */ +PUB_FUNC char *tcc_fileextension (const char *name) +{ + char *b = tcc_basename(name); + char *e = strrchr(b, '.'); + return e ? e : strchr(b, 0); +} + +ST_FUNC char *tcc_load_text(int fd) +{ + int len = lseek(fd, 0, SEEK_END); + char *buf = load_data(fd, 0, len + 1); + buf[len] = 0; + return buf; +} + +/********************************************************/ +/* memory management */ + +#undef free +#undef malloc +#undef realloc + +#ifndef MEM_DEBUG + +PUB_FUNC void tcc_free(void *ptr) +{ + free(ptr); +} + +PUB_FUNC void *tcc_malloc(unsigned long size) +{ + void *ptr; + ptr = malloc(size); + if (!ptr && size) + _tcc_error("memory full (malloc)"); + return ptr; +} + +PUB_FUNC void *tcc_mallocz(unsigned long size) +{ + void *ptr; + ptr = tcc_malloc(size); + if (size) + memset(ptr, 0, size); + return ptr; +} + +PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size) +{ + void *ptr1; + ptr1 = realloc(ptr, size); + if (!ptr1 && size) + _tcc_error("memory full (realloc)"); + return ptr1; +} + +PUB_FUNC char *tcc_strdup(const char *str) +{ + char *ptr; + ptr = tcc_malloc(strlen(str) + 1); + strcpy(ptr, str); + return ptr; +} + +#else + +#define MEM_DEBUG_MAGIC1 0xFEEDDEB1 +#define MEM_DEBUG_MAGIC2 0xFEEDDEB2 +#define MEM_DEBUG_MAGIC3 0xFEEDDEB3 +#define MEM_DEBUG_FILE_LEN 40 +#define MEM_DEBUG_CHECK3(header) \ + ((mem_debug_header_t*)((char*)header + header->size))->magic3 +#define MEM_USER_PTR(header) \ + ((char *)header + offsetof(mem_debug_header_t, magic3)) +#define MEM_HEADER_PTR(ptr) \ + (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3)) + +struct mem_debug_header { + unsigned magic1; + unsigned size; + struct mem_debug_header *prev; + struct mem_debug_header *next; + int line_num; + char file_name[MEM_DEBUG_FILE_LEN + 1]; + unsigned magic2; + ALIGNED(16) unsigned char magic3[4]; +}; + +typedef struct mem_debug_header mem_debug_header_t; + +static mem_debug_header_t *mem_debug_chain; +static unsigned mem_cur_size; +static unsigned mem_max_size; + +static mem_debug_header_t *malloc_check(void *ptr, const char *msg) +{ + mem_debug_header_t * header = MEM_HEADER_PTR(ptr); + if (header->magic1 != MEM_DEBUG_MAGIC1 || + header->magic2 != MEM_DEBUG_MAGIC2 || + read32le(MEM_DEBUG_CHECK3(header)) != MEM_DEBUG_MAGIC3 || + header->size == (unsigned)-1) { + fprintf(stderr, "%s check failed\n", msg); + if (header->magic1 == MEM_DEBUG_MAGIC1) + fprintf(stderr, "%s:%u: block allocated here.\n", + header->file_name, header->line_num); + exit(1); + } + return header; +} + +PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line) +{ + int ofs; + mem_debug_header_t *header; + + header = malloc(sizeof(mem_debug_header_t) + size); + if (!header) + _tcc_error("memory full (malloc)"); + + header->magic1 = MEM_DEBUG_MAGIC1; + header->magic2 = MEM_DEBUG_MAGIC2; + header->size = size; + write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3); + header->line_num = line; + ofs = strlen(file) - MEM_DEBUG_FILE_LEN; + strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN); + header->file_name[MEM_DEBUG_FILE_LEN] = 0; + + header->next = mem_debug_chain; + header->prev = NULL; + if (header->next) + header->next->prev = header; + mem_debug_chain = header; + + mem_cur_size += size; + if (mem_cur_size > mem_max_size) + mem_max_size = mem_cur_size; + + return MEM_USER_PTR(header); +} + +PUB_FUNC void tcc_free_debug(void *ptr) +{ + mem_debug_header_t *header; + if (!ptr) + return; + header = malloc_check(ptr, "tcc_free"); + mem_cur_size -= header->size; + header->size = (unsigned)-1; + if (header->next) + header->next->prev = header->prev; + if (header->prev) + header->prev->next = header->next; + if (header == mem_debug_chain) + mem_debug_chain = header->next; + free(header); +} + +PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line) +{ + void *ptr; + ptr = tcc_malloc_debug(size,file,line); + memset(ptr, 0, size); + return ptr; +} + +PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line) +{ + mem_debug_header_t *header; + int mem_debug_chain_update = 0; + if (!ptr) + return tcc_malloc_debug(size, file, line); + header = malloc_check(ptr, "tcc_realloc"); + mem_cur_size -= header->size; + mem_debug_chain_update = (header == mem_debug_chain); + header = realloc(header, sizeof(mem_debug_header_t) + size); + if (!header) + _tcc_error("memory full (realloc)"); + header->size = size; + write32le(MEM_DEBUG_CHECK3(header), MEM_DEBUG_MAGIC3); + if (header->next) + header->next->prev = header; + if (header->prev) + header->prev->next = header; + if (mem_debug_chain_update) + mem_debug_chain = header; + mem_cur_size += size; + if (mem_cur_size > mem_max_size) + mem_max_size = mem_cur_size; + return MEM_USER_PTR(header); +} + +PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line) +{ + char *ptr; + ptr = tcc_malloc_debug(strlen(str) + 1, file, line); + strcpy(ptr, str); + return ptr; +} + +PUB_FUNC void tcc_memcheck(void) +{ + if (mem_cur_size) { + mem_debug_header_t *header = mem_debug_chain; + fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n", + mem_cur_size, mem_max_size); + while (header) { + fprintf(stderr, "%s:%u: error: %u bytes leaked\n", + header->file_name, header->line_num, header->size); + header = header->next; + } +#if MEM_DEBUG-0 == 2 + exit(2); +#endif + } +} +#endif /* MEM_DEBUG */ + +#define free(p) use_tcc_free(p) +#define malloc(s) use_tcc_malloc(s) +#define realloc(p, s) use_tcc_realloc(p, s) + +/********************************************************/ +/* dynarrays */ + +ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data) +{ + int nb, nb_alloc; + void **pp; + + nb = *nb_ptr; + pp = *(void ***)ptab; + /* every power of two we double array size */ + if ((nb & (nb - 1)) == 0) { + if (!nb) + nb_alloc = 1; + else + nb_alloc = nb * 2; + pp = tcc_realloc(pp, nb_alloc * sizeof(void *)); + *(void***)ptab = pp; + } + pp[nb++] = data; + *nb_ptr = nb; +} + +ST_FUNC void dynarray_reset(void *pp, int *n) +{ + void **p; + for (p = *(void***)pp; *n; ++p, --*n) + if (*p) + tcc_free(*p); + tcc_free(*(void**)pp); + *(void**)pp = NULL; +} + +static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in) +{ + const char *p; + do { + int c; + CString str; + + cstr_new(&str); + for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) { + if (c == '{' && p[1] && p[2] == '}') { + c = p[1], p += 2; + if (c == 'B') + cstr_cat(&str, s->tcc_lib_path, -1); + if (c == 'f' && file) { + /* substitute current file's dir */ + const char *f = file->true_filename; + const char *b = tcc_basename(f); + if (b > f) + cstr_cat(&str, f, b - f - 1); + else + cstr_cat(&str, ".", 1); + } + } else { + cstr_ccat(&str, c); + } + } + if (str.size) { + cstr_ccat(&str, '\0'); + dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data)); + } + cstr_free(&str); + in = p+1; + } while (*p); +} + +/********************************************************/ +/* warning / error */ + +/* warn_... option bits */ +#define WARN_ON 1 /* warning is on (-Woption) */ +#define WARN_ERR 2 /* warning is an error (-Werror=option) */ +#define WARN_NOE 4 /* warning is not an error (-Wno-error=option) */ + +/* error1() modes */ +enum { ERROR_WARN, ERROR_NOABORT, ERROR_ERROR }; + +static void error1(int mode, const char *fmt, va_list ap) +{ + BufferedFile **pf, *f; + TCCState *s1 = tcc_state; + CString cs; + + cstr_new(&cs); + + if (s1 == NULL) + /* can happen only if called from tcc_malloc(): 'out of memory' */ + goto no_file; + + tcc_exit_state(s1); + + if (mode == ERROR_WARN) { + if (s1->warn_error) + mode = ERROR_ERROR; + if (s1->warn_num) { + /* handle tcc_warning_c(warn_option)(fmt, ...) */ + int wopt = *(&s1->warn_none + s1->warn_num); + s1->warn_num = 0; + if (0 == (wopt & WARN_ON)) + return; + if (wopt & WARN_ERR) + mode = ERROR_ERROR; + if (wopt & WARN_NOE) + mode = ERROR_WARN; + } + if (s1->warn_none) + return; + } + + f = NULL; + if (s1->error_set_jmp_enabled) { /* we're called while parsing a file */ + /* use upper file if inline ":asm:" or token ":paste:" */ + for (f = file; f && f->filename[0] == ':'; f = f->prev) + ; + } + if (f) { + for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++) + cstr_printf(&cs, "In file included from %s:%d:\n", + (*pf)->filename, (*pf)->line_num); + cstr_printf(&cs, "%s:%d: ", + f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL)); + } else if (s1->current_filename) { + cstr_printf(&cs, "%s: ", s1->current_filename); + } + +no_file: + if (0 == cs.size) + cstr_printf(&cs, "tcc: "); + cstr_printf(&cs, mode == ERROR_WARN ? "warning: " : "error: "); + cstr_vprintf(&cs, fmt, ap); + if (!s1 || !s1->error_func) { + /* default case: stderr */ + if (s1 && s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout) + printf("\n"); /* print a newline during tcc -E */ + fflush(stdout); /* flush -v output */ + fprintf(stderr, "%s\n", (char*)cs.data); + fflush(stderr); /* print error/warning now (win32) */ + } else { + s1->error_func(s1->error_opaque, (char*)cs.data); + } + cstr_free(&cs); + if (s1) { + if (mode != ERROR_WARN) + s1->nb_errors++; + if (mode != ERROR_ERROR) + return; + if (s1->error_set_jmp_enabled) + longjmp(s1->error_jmp_buf, 1); + } + exit(1); +} + +LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc error_func) +{ + s->error_opaque = error_opaque; + s->error_func = error_func; +} + +LIBTCCAPI TCCErrorFunc tcc_get_error_func(TCCState *s) +{ + return s->error_func; +} + +LIBTCCAPI void *tcc_get_error_opaque(TCCState *s) +{ + return s->error_opaque; +} + +/* error without aborting current compilation */ +PUB_FUNC void _tcc_error_noabort(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + error1(ERROR_NOABORT, fmt, ap); + va_end(ap); +} + +PUB_FUNC void _tcc_error(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + for (;;) error1(ERROR_ERROR, fmt, ap); +} + +PUB_FUNC void _tcc_warning(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + error1(ERROR_WARN, fmt, ap); + va_end(ap); +} + +/********************************************************/ +/* I/O layer */ + +ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen) +{ + BufferedFile *bf; + int buflen = initlen ? initlen : IO_BUF_SIZE; + + bf = tcc_mallocz(sizeof(BufferedFile) + buflen); + bf->buf_ptr = bf->buffer; + bf->buf_end = bf->buffer + initlen; + bf->buf_end[0] = CH_EOB; /* put eob symbol */ + pstrcpy(bf->filename, sizeof(bf->filename), filename); +#ifdef _WIN32 + normalize_slashes(bf->filename); +#endif + bf->true_filename = bf->filename; + bf->line_num = 1; + bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; + bf->fd = -1; + bf->prev = file; + file = bf; + tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; +} + +ST_FUNC void tcc_close(void) +{ + TCCState *s1 = tcc_state; + BufferedFile *bf = file; + if (bf->fd > 0) { + close(bf->fd); + total_lines += bf->line_num; + } + if (bf->true_filename != bf->filename) + tcc_free(bf->true_filename); + file = bf->prev; + tcc_free(bf); +} + +static int _tcc_open(TCCState *s1, const char *filename) +{ + int fd; + if (strcmp(filename, "-") == 0) + fd = 0, filename = ""; + else + fd = open(filename, O_RDONLY | O_BINARY); + if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3) + printf("%s %*s%s\n", fd < 0 ? "nf":"->", + (int)(s1->include_stack_ptr - s1->include_stack), "", filename); + return fd; +} + +ST_FUNC int tcc_open(TCCState *s1, const char *filename) +{ + int fd = _tcc_open(s1, filename); + if (fd < 0) + return -1; + tcc_open_bf(s1, filename, 0); + file->fd = fd; + return 0; +} + +/* compile the file opened in 'file'. Return non zero if errors. */ +static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) +{ + /* Here we enter the code section where we use the global variables for + parsing and code generation (tccpp.c, tccgen.c, -gen.c). + Other threads need to wait until we're done. + + Alternatively we could use thread local storage for those global + variables, which may or may not have advantages */ + + tcc_enter_state(s1); + s1->error_set_jmp_enabled = 1; + + if (setjmp(s1->error_jmp_buf) == 0) { + s1->nb_errors = 0; + + if (fd == -1) { + int len = strlen(str); + tcc_open_bf(s1, "", len); + memcpy(file->buffer, str, len); + } else { + tcc_open_bf(s1, str, 0); + file->fd = fd; + } + + tccelf_begin_file(s1); + preprocess_start(s1, filetype); + tccgen_init(s1); + if (s1->output_type == TCC_OUTPUT_PREPROCESS) { + tcc_preprocess(s1); + } else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) { + tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP)); + } else { + tccgen_compile(s1); + } + } + tccgen_finish(s1); + preprocess_end(s1); + + s1->error_set_jmp_enabled = 0; + tcc_exit_state(s1); + + tccelf_end_file(s1); + return s1->nb_errors != 0 ? -1 : 0; +} + +LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str) +{ + return tcc_compile(s, s->filetype, str, -1); +} + +/* define a preprocessor symbol. value can be NULL, sym can be "sym=val" */ +LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) +{ + const char *eq; + if (NULL == (eq = strchr(sym, '='))) + eq = strchr(sym, 0); + if (NULL == value) + value = *eq ? eq + 1 : "1"; + cstr_printf(&s1->cmdline_defs, "#define %.*s %s\n", (int)(eq-sym), sym, value); +} + +/* undefine a preprocessor symbol */ +LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym) +{ + cstr_printf(&s1->cmdline_defs, "#undef %s\n", sym); +} + + +LIBTCCAPI TCCState *tcc_new(void) +{ + TCCState *s; + + s = tcc_mallocz(sizeof(TCCState)); + if (!s) + return NULL; +#ifdef MEM_DEBUG + ++nb_states; +#endif + +#undef gnu_ext + + s->gnu_ext = 1; + s->tcc_ext = 1; + s->nocommon = 1; + s->dollars_in_identifiers = 1; /*on by default like in gcc/clang*/ + s->cversion = 199901; /* default unless -std=c11 is supplied */ + s->warn_implicit_function_declaration = 1; + s->warn_discarded_qualifiers = 1; + s->ms_extensions = 1; + +#ifdef CHAR_IS_UNSIGNED + s->char_is_unsigned = 1; +#endif +#ifdef TCC_TARGET_I386 + s->seg_size = 32; +#endif + /* enable this if you want symbols with leading underscore on windows: */ +#if defined TCC_TARGET_MACHO /* || defined TCC_TARGET_PE */ + s->leading_underscore = 1; +#endif +#ifdef TCC_TARGET_ARM + s->float_abi = ARM_FLOAT_ABI; +#endif + + s->ppfp = stdout; + /* might be used in error() before preprocess_start() */ + s->include_stack_ptr = s->include_stack; + + tccelf_new(s); + + tcc_set_lib_path(s, CONFIG_TCCDIR); + return s; +} + +LIBTCCAPI void tcc_delete(TCCState *s1) +{ + /* free sections */ + tccelf_delete(s1); + + /* free library paths */ + dynarray_reset(&s1->library_paths, &s1->nb_library_paths); + dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths); + + /* free include paths */ + dynarray_reset(&s1->include_paths, &s1->nb_include_paths); + dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths); + + tcc_free(s1->tcc_lib_path); + tcc_free(s1->soname); + tcc_free(s1->rpath); + tcc_free(s1->elf_entryname); + tcc_free(s1->init_symbol); + tcc_free(s1->fini_symbol); + tcc_free(s1->outfile); + tcc_free(s1->deps_outfile); + dynarray_reset(&s1->files, &s1->nb_files); + dynarray_reset(&s1->target_deps, &s1->nb_target_deps); + dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs); + dynarray_reset(&s1->argv, &s1->argc); + cstr_free(&s1->cmdline_defs); + cstr_free(&s1->cmdline_incl); +#ifdef TCC_IS_NATIVE + /* free runtime memory */ + tcc_run_free(s1); +#endif + tcc_free(s1->dState); + tcc_free(s1); +#ifdef MEM_DEBUG + if (0 == --nb_states) + tcc_memcheck(); +#endif +} + +LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) +{ + s->output_type = output_type; + + if (!s->nostdinc) { + /* default include paths */ + /* -isystem paths have already been handled */ + tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS); + } +#ifdef CONFIG_TCC_BCHECK + if (s->do_bounds_check) { + /* if bound checking, then add corresponding sections */ + tccelf_bounds_new(s); + } +#endif + if (s->do_debug) { + /* add debug sections */ + tcc_debug_new(s); + } + if (output_type == TCC_OUTPUT_OBJ) { + /* always elf for objects */ + s->output_format = TCC_OUTPUT_FORMAT_ELF; + return 0; + } + + tcc_add_library_path(s, CONFIG_TCC_LIBPATHS); + +#ifdef TCC_TARGET_PE +# ifdef _WIN32 + /* allow linking with system dll's directly */ + tcc_add_systemdir(s); +# endif + /* target PE has its own startup code in libtcc1.a */ + return 0; + +#elif defined TCC_TARGET_MACHO +# ifdef TCC_IS_NATIVE + tcc_add_macos_sdkpath(s); +# endif + /* Mach-O with LC_MAIN doesn't need any crt startup code. */ + return 0; + +#else + /* paths for crt objects */ + tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX); + /* add libc crt1/crti objects */ + if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) && + !s->nostdlib) { +#if TARGETOS_OpenBSD + if (output_type != TCC_OUTPUT_DLL) + tcc_add_crt(s, "crt0.o"); + if (output_type == TCC_OUTPUT_DLL) + tcc_add_crt(s, "crtbeginS.o"); + else + tcc_add_crt(s, "crtbegin.o"); +#elif TARGETOS_FreeBSD + if (output_type != TCC_OUTPUT_DLL) + tcc_add_crt(s, "crt1.o"); + tcc_add_crt(s, "crti.o"); + if (s->static_link) + tcc_add_crt(s, "crtbeginT.o"); + else if (output_type == TCC_OUTPUT_DLL) + tcc_add_crt(s, "crtbeginS.o"); + else + tcc_add_crt(s, "crtbegin.o"); +#elif TARGETOS_NetBSD + if (output_type != TCC_OUTPUT_DLL) + tcc_add_crt(s, "crt0.o"); + tcc_add_crt(s, "crti.o"); + if (s->static_link) + tcc_add_crt(s, "crtbeginT.o"); + else if (output_type == TCC_OUTPUT_DLL) + tcc_add_crt(s, "crtbeginS.o"); + else + tcc_add_crt(s, "crtbegin.o"); +#else + if (output_type != TCC_OUTPUT_DLL) + tcc_add_crt(s, "crt1.o"); + tcc_add_crt(s, "crti.o"); +#endif + } + return 0; +#endif +} + +LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname) +{ + tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname); + return 0; +} + +LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname) +{ + tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname); + return 0; +} + +ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname) +{ + DLLReference *ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname)); + strcpy(ref->name, dllname); + dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref); + return ref; +} + +/* OpenBSD: choose latest from libxxx.so.x.y versions */ +#if defined TARGETOS_OpenBSD && !defined _WIN32 +#include +static int tcc_glob_so(TCCState *s1, const char *pattern, char *buf, int size) +{ + const char *star; + glob_t g; + char *p; + int i, v, v1, v2, v3; + + star = strchr(pattern, '*'); + if (!star || glob(pattern, 0, NULL, &g)) + return -1; + for (v = -1, i = 0; i < g.gl_pathc; ++i) { + p = g.gl_pathv[i]; + if (2 != sscanf(p + (star - pattern), "%d.%d.%d", &v1, &v2, &v3)) + continue; + if ((v1 = v1 * 1000 + v2) > v) + v = v1, pstrcpy(buf, size, p); + } + globfree(&g); + return v; +} +#endif + +ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) +{ + int fd, ret = -1; + +#if defined TARGETOS_OpenBSD && !defined _WIN32 + char buf[1024]; + if (tcc_glob_so(s1, filename, buf, sizeof buf) >= 0) + filename = buf; +#endif + + /* open the file */ + fd = _tcc_open(s1, filename); + if (fd < 0) { + if (flags & AFF_PRINT_ERROR) + tcc_error_noabort("file '%s' not found", filename); + return ret; + } + + s1->current_filename = filename; + if (flags & AFF_TYPE_BIN) { + ElfW(Ehdr) ehdr; + int obj_type; + + obj_type = tcc_object_type(fd, &ehdr); + lseek(fd, 0, SEEK_SET); + + switch (obj_type) { + + case AFF_BINTYPE_REL: + ret = tcc_load_object_file(s1, fd, 0); + break; + + case AFF_BINTYPE_AR: + ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE)); + break; + +#ifdef TCC_TARGET_PE + default: + ret = pe_load_file(s1, fd, filename); + goto check_success; + +#elif defined TCC_TARGET_MACHO + case AFF_BINTYPE_DYN: + case_dyn_or_tbd: + if (s1->output_type == TCC_OUTPUT_MEMORY) { +#ifdef TCC_IS_NATIVE + void* dl; + const char* soname = filename; + if (obj_type != AFF_BINTYPE_DYN) + soname = macho_tbd_soname(filename); + dl = dlopen(soname, RTLD_GLOBAL | RTLD_LAZY); + if (dl) + tcc_add_dllref(s1, soname)->handle = dl, ret = 0; + if (filename != soname) + tcc_free((void *)soname); +#endif + } else if (obj_type == AFF_BINTYPE_DYN) { + ret = macho_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); + } else { + ret = macho_load_tbd(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); + } + break; + default: + { + const char *ext = tcc_fileextension(filename); + if (!strcmp(ext, ".tbd")) + goto case_dyn_or_tbd; + if (!strcmp(ext, ".dylib")) { + obj_type = AFF_BINTYPE_DYN; + goto case_dyn_or_tbd; + } + goto check_success; + } + +#else /* unix */ + case AFF_BINTYPE_DYN: + if (s1->output_type == TCC_OUTPUT_MEMORY) { +#ifdef TCC_IS_NATIVE + void* dl = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY); + if (dl) + tcc_add_dllref(s1, filename)->handle = dl, ret = 0; +#endif + } else + ret = tcc_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); + break; + + default: + /* as GNU ld, consider it is an ld script if not recognized */ + ret = tcc_load_ldscript(s1, fd); + goto check_success; + +#endif /* pe / macos / unix */ + +check_success: + if (ret < 0) + tcc_error_noabort("%s: unrecognized file type", filename); + break; + +#ifdef TCC_TARGET_COFF + case AFF_BINTYPE_C67: + ret = tcc_load_coff(s1, fd); + break; +#endif + } + close(fd); + } else { + /* update target deps */ + dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename)); + ret = tcc_compile(s1, flags, filename, fd); + } + s1->current_filename = NULL; + return ret; +} + +LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename) +{ + int filetype = s->filetype; + if (0 == (filetype & AFF_TYPE_MASK)) { + /* use a file extension to detect a filetype */ + const char *ext = tcc_fileextension(filename); + if (ext[0]) { + ext++; + if (!strcmp(ext, "S")) + filetype = AFF_TYPE_ASMPP; + else if (!strcmp(ext, "s")) + filetype = AFF_TYPE_ASM; + else if (!PATHCMP(ext, "c") + || !PATHCMP(ext, "h") + || !PATHCMP(ext, "i")) + filetype = AFF_TYPE_C; + else + filetype |= AFF_TYPE_BIN; + } else { + filetype = AFF_TYPE_C; + } + } + return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR); +} + +LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname) +{ + tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname); + return 0; +} + +static int tcc_add_library_internal(TCCState *s, const char *fmt, + const char *filename, int flags, char **paths, int nb_paths) +{ + char buf[1024]; + int i; + + for(i = 0; i < nb_paths; i++) { + snprintf(buf, sizeof(buf), fmt, paths[i], filename); + if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0) + return 0; + } + return -1; +} + +#ifndef TCC_TARGET_MACHO +/* find and load a dll. Return non zero if not found */ +ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags) +{ + return tcc_add_library_internal(s, "%s/%s", filename, flags, + s->library_paths, s->nb_library_paths); +} +#endif + +#if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO +ST_FUNC int tcc_add_crt(TCCState *s1, const char *filename) +{ + if (-1 == tcc_add_library_internal(s1, "%s/%s", + filename, 0, s1->crt_paths, s1->nb_crt_paths)) + tcc_error_noabort("file '%s' not found", filename); + return 0; +} +#endif + +/* the library name is the same as the argument of the '-l' option */ +LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) +{ +#if defined TCC_TARGET_PE + static const char * const libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL }; + const char * const *pp = s->static_link ? libs + 4 : libs; +#elif defined TCC_TARGET_MACHO + static const char * const libs[] = { "%s/lib%s.dylib", "%s/lib%s.tbd", "%s/lib%s.a", NULL }; + const char * const *pp = s->static_link ? libs + 2 : libs; +#elif defined TARGETOS_OpenBSD + static const char * const libs[] = { "%s/lib%s.so.*", "%s/lib%s.a", NULL }; + const char * const *pp = s->static_link ? libs + 1 : libs; +#else + static const char * const libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL }; + const char * const *pp = s->static_link ? libs + 1 : libs; +#endif + int flags = s->filetype & AFF_WHOLE_ARCHIVE; + while (*pp) { + if (0 == tcc_add_library_internal(s, *pp, + libraryname, flags, s->library_paths, s->nb_library_paths)) + return 0; + ++pp; + } + return -1; +} + +PUB_FUNC int tcc_add_library_err(TCCState *s1, const char *libname) +{ + int ret = tcc_add_library(s1, libname); + if (ret < 0) + tcc_error_noabort("library '%s' not found", libname); + return ret; +} + +/* handle #pragma comment(lib,) */ +ST_FUNC void tcc_add_pragma_libs(TCCState *s1) +{ + int i; + for (i = 0; i < s1->nb_pragma_libs; i++) + tcc_add_library_err(s1, s1->pragma_libs[i]); +} + +LIBTCCAPI int tcc_add_symbol(TCCState *s1, const char *name, const void *val) +{ +#ifdef TCC_TARGET_PE + /* On x86_64 'val' might not be reachable with a 32bit offset. + So it is handled here as if it were in a DLL. */ + pe_putimport(s1, 0, name, (uintptr_t)val); +#else + char buf[256]; + if (s1->leading_underscore) { + buf[0] = '_'; + pstrcpy(buf + 1, sizeof(buf) - 1, name); + name = buf; + } + set_global_sym(s1, name, NULL, (addr_t)(uintptr_t)val); /* NULL: SHN_ABS */ +#endif + return 0; +} + +LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path) +{ + tcc_free(s->tcc_lib_path); + s->tcc_lib_path = tcc_strdup(path); +} + +/********************************************************/ +/* options parser */ + +static int strstart(const char *val, const char **str) +{ + const char *p, *q; + p = *str; + q = val; + while (*q) { + if (*p != *q) + return 0; + p++; + q++; + } + *str = p; + return 1; +} + +/* Like strstart, but automatically takes into account that ld options can + * + * - start with double or single dash (e.g. '--soname' or '-soname') + * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so' + * or '-Wl,-soname=x.so') + * + * you provide `val` always in 'option[=]' form (no leading -) + */ +static int link_option(const char *str, const char *val, const char **ptr) +{ + const char *p, *q; + int ret; + + /* there should be 1 or 2 dashes */ + if (*str++ != '-') + return 0; + if (*str == '-') + str++; + + /* then str & val should match (potentially up to '=') */ + p = str; + q = val; + + ret = 1; + if (q[0] == '?') { + ++q; + if (strstart("no-", &p)) + ret = -1; + } + + while (*q != '\0' && *q != '=') { + if (*p != *q) + return 0; + p++; + q++; + } + + /* '=' near eos means ',' or '=' is ok */ + if (*q == '=') { + if (*p == 0) + *ptr = p; + if (*p != ',' && *p != '=') + return 0; + p++; + } else if (*p) { + return 0; + } + *ptr = p; + return ret; +} + +static const char *skip_linker_arg(const char **str) +{ + const char *s1 = *str; + const char *s2 = strchr(s1, ','); + *str = s2 ? s2++ : (s2 = s1 + strlen(s1)); + return s2; +} + +static void copy_linker_arg(char **pp, const char *s, int sep) +{ + const char *q = s; + char *p = *pp; + int l = 0; + if (p && sep) + p[l = strlen(p)] = sep, ++l; + skip_linker_arg(&q); + pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s); +} + +/* set linker options */ +static int tcc_set_linker(TCCState *s, const char *option) +{ + TCCState *s1 = s; + while (*option) { + + const char *p = NULL; + char *end = NULL; + int ignoring = 0; + int ret; + + if (link_option(option, "Bsymbolic", &p)) { + s->symbolic = 1; + } else if (link_option(option, "nostdlib", &p)) { + s->nostdlib = 1; + } else if (link_option(option, "e=", &p) + || link_option(option, "entry=", &p)) { + copy_linker_arg(&s->elf_entryname, p, 0); + } else if (link_option(option, "fini=", &p)) { + copy_linker_arg(&s->fini_symbol, p, 0); + ignoring = 1; + } else if (link_option(option, "image-base=", &p) + || link_option(option, "Ttext=", &p)) { + s->text_addr = strtoull(p, &end, 16); + s->has_text_addr = 1; + } else if (link_option(option, "init=", &p)) { + copy_linker_arg(&s->init_symbol, p, 0); + ignoring = 1; + } else if (link_option(option, "oformat=", &p)) { +#if defined(TCC_TARGET_PE) + if (strstart("pe-", &p)) { +#elif PTR_SIZE == 8 + if (strstart("elf64-", &p)) { +#else + if (strstart("elf32-", &p)) { +#endif + s->output_format = TCC_OUTPUT_FORMAT_ELF; + } else if (!strcmp(p, "binary")) { + s->output_format = TCC_OUTPUT_FORMAT_BINARY; +#ifdef TCC_TARGET_COFF + } else if (!strcmp(p, "coff")) { + s->output_format = TCC_OUTPUT_FORMAT_COFF; +#endif + } else + goto err; + + } else if (link_option(option, "as-needed", &p)) { + ignoring = 1; + } else if (link_option(option, "O", &p)) { + ignoring = 1; + } else if (link_option(option, "export-all-symbols", &p)) { + s->rdynamic = 1; + } else if (link_option(option, "export-dynamic", &p)) { + s->rdynamic = 1; + } else if (link_option(option, "rpath=", &p)) { + copy_linker_arg(&s->rpath, p, ':'); + } else if (link_option(option, "enable-new-dtags", &p)) { + s->enable_new_dtags = 1; + } else if (link_option(option, "section-alignment=", &p)) { + s->section_align = strtoul(p, &end, 16); + } else if (link_option(option, "soname=", &p)) { + copy_linker_arg(&s->soname, p, 0); +#ifdef TCC_TARGET_PE + } else if (link_option(option, "large-address-aware", &p)) { + s->pe_characteristics |= 0x20; + } else if (link_option(option, "file-alignment=", &p)) { + s->pe_file_align = strtoul(p, &end, 16); + } else if (link_option(option, "stack=", &p)) { + s->pe_stack_size = strtoul(p, &end, 10); + } else if (link_option(option, "subsystem=", &p)) { +#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) + if (!strcmp(p, "native")) { + s->pe_subsystem = 1; + } else if (!strcmp(p, "console")) { + s->pe_subsystem = 3; + } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) { + s->pe_subsystem = 2; + } else if (!strcmp(p, "posix")) { + s->pe_subsystem = 7; + } else if (!strcmp(p, "efiapp")) { + s->pe_subsystem = 10; + } else if (!strcmp(p, "efiboot")) { + s->pe_subsystem = 11; + } else if (!strcmp(p, "efiruntime")) { + s->pe_subsystem = 12; + } else if (!strcmp(p, "efirom")) { + s->pe_subsystem = 13; +#elif defined(TCC_TARGET_ARM) + if (!strcmp(p, "wince")) { + s->pe_subsystem = 9; +#endif + } else + goto err; +#endif + } else if (ret = link_option(option, "?whole-archive", &p), ret) { + if (ret > 0) + s->filetype |= AFF_WHOLE_ARCHIVE; + else + s->filetype &= ~AFF_WHOLE_ARCHIVE; + } else if (link_option(option, "z=", &p)) { + ignoring = 1; + } else if (p) { + return 0; + } else { + err: + tcc_error("unsupported linker option '%s'", option); + } + if (ignoring) + tcc_warning_c(warn_unsupported)("unsupported linker option '%s'", option); + option = skip_linker_arg(&p); + } + return 1; +} + +typedef struct TCCOption { + const char *name; + uint16_t index; + uint16_t flags; +} TCCOption; + +enum { + TCC_OPTION_ignored = 0, + TCC_OPTION_HELP, + TCC_OPTION_HELP2, + TCC_OPTION_v, + TCC_OPTION_I, + TCC_OPTION_D, + TCC_OPTION_U, + TCC_OPTION_P, + TCC_OPTION_L, + TCC_OPTION_B, + TCC_OPTION_l, + TCC_OPTION_bench, + TCC_OPTION_bt, + TCC_OPTION_b, + TCC_OPTION_ba, + TCC_OPTION_g, + TCC_OPTION_c, + TCC_OPTION_dumpversion, + TCC_OPTION_d, + TCC_OPTION_static, + TCC_OPTION_std, + TCC_OPTION_shared, + TCC_OPTION_soname, + TCC_OPTION_o, + TCC_OPTION_r, + TCC_OPTION_Wl, + TCC_OPTION_Wp, + TCC_OPTION_W, + TCC_OPTION_O, + TCC_OPTION_mfloat_abi, + TCC_OPTION_m, + TCC_OPTION_f, + TCC_OPTION_isystem, + TCC_OPTION_iwithprefix, + TCC_OPTION_include, + TCC_OPTION_nostdinc, + TCC_OPTION_nostdlib, + TCC_OPTION_print_search_dirs, + TCC_OPTION_rdynamic, + TCC_OPTION_pthread, + TCC_OPTION_run, + TCC_OPTION_w, + TCC_OPTION_E, + TCC_OPTION_M, + TCC_OPTION_MD, + TCC_OPTION_MF, + TCC_OPTION_MM, + TCC_OPTION_MMD, + TCC_OPTION_x, + TCC_OPTION_ar, + TCC_OPTION_impdef, +}; + +#define TCC_OPTION_HAS_ARG 0x0001 +#define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */ + +static const TCCOption tcc_options[] = { + { "h", TCC_OPTION_HELP, 0 }, + { "-help", TCC_OPTION_HELP, 0 }, + { "?", TCC_OPTION_HELP, 0 }, + { "hh", TCC_OPTION_HELP2, 0 }, + { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "-version", TCC_OPTION_v, 0 }, /* handle as verbose, also prints version*/ + { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG }, + { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG }, + { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG }, + { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG }, + { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG }, + { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG }, + { "bench", TCC_OPTION_bench, 0 }, +#ifdef CONFIG_TCC_BACKTRACE + { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, +#endif +#ifdef CONFIG_TCC_BCHECK + { "b", TCC_OPTION_b, 0 }, +#endif + { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "c", TCC_OPTION_c, 0 }, + { "dumpversion", TCC_OPTION_dumpversion, 0}, + { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "static", TCC_OPTION_static, 0 }, + { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "shared", TCC_OPTION_shared, 0 }, + { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG }, + { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG }, + { "pthread", TCC_OPTION_pthread, 0}, + { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "rdynamic", TCC_OPTION_rdynamic, 0 }, + { "r", TCC_OPTION_r, 0 }, + { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, +#ifdef TCC_TARGET_ARM + { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG }, +#endif + { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, + { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG }, + { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG }, + { "nostdinc", TCC_OPTION_nostdinc, 0 }, + { "nostdlib", TCC_OPTION_nostdlib, 0 }, + { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 }, + { "w", TCC_OPTION_w, 0 }, + { "E", TCC_OPTION_E, 0}, + { "M", TCC_OPTION_M, 0}, + { "MD", TCC_OPTION_MD, 0}, + { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG }, + { "MM", TCC_OPTION_MM, 0}, + { "MMD", TCC_OPTION_MMD, 0}, + { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG }, + { "ar", TCC_OPTION_ar, 0}, +#ifdef TCC_TARGET_PE + { "impdef", TCC_OPTION_impdef, 0}, +#endif + /* ignored (silently, except after -Wunsupported) */ + { "arch", 0, TCC_OPTION_HAS_ARG}, + { "C", 0, 0 }, + { "-param", 0, TCC_OPTION_HAS_ARG }, + { "pedantic", 0, 0 }, + { "pipe", 0, 0 }, + { "s", 0, 0 }, + { "traditional", 0, 0 }, + { NULL, 0, 0 }, +}; + +typedef struct FlagDef { + uint16_t offset; + uint16_t flags; + const char *name; +} FlagDef; + +#define WD_ALL 0x0001 /* warning is activated when using -Wall */ +#define FD_INVERT 0x0002 /* invert value before storing */ + +static const FlagDef options_W[] = { + { offsetof(TCCState, warn_all), WD_ALL, "all" }, + { offsetof(TCCState, warn_error), 0, "error" }, + { offsetof(TCCState, warn_write_strings), 0, "write-strings" }, + { offsetof(TCCState, warn_unsupported), 0, "unsupported" }, + { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL, "implicit-function-declaration" }, + { offsetof(TCCState, warn_discarded_qualifiers), WD_ALL, "discarded-qualifiers" }, + { 0, 0, NULL } +}; + +static const FlagDef options_f[] = { + { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" }, + { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" }, + { offsetof(TCCState, nocommon), FD_INVERT, "common" }, + { offsetof(TCCState, leading_underscore), 0, "leading-underscore" }, + { offsetof(TCCState, ms_extensions), 0, "ms-extensions" }, + { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" }, + { offsetof(TCCState, test_coverage), 0, "test-coverage" }, + { 0, 0, NULL } +}; + +static const FlagDef options_m[] = { + { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" }, +#ifdef TCC_TARGET_X86_64 + { offsetof(TCCState, nosse), FD_INVERT, "sse" }, +#endif + { 0, 0, NULL } +}; + +static int set_flag(TCCState *s, const FlagDef *flags, const char *name) +{ + int value, mask, ret; + const FlagDef *p; + const char *r; + unsigned char *f; + + r = name, value = !strstart("no-", &r), mask = 0; + + /* when called with options_W, look for -W[no-]error=
    _0r6R{wSLd&i z?XsI*o5mw+c>+Y8HluySw(xN`$Zi-jdxo10x{cP^ z!|}uM+Pv9YRIsE-2_%*S8VlRD%RLQTFYNOp#l}oGr7|tbOkzcpoad{P$K~r$wyT4a z(e2XORs5Tg)<0TZWRMmgDkv5wHWNe`g=8uSWy^IF4w(#^N)CeBP}$Ic#k^f!_uoC? zCaK_L2`;HV92glm;wje_7$wKJ#eqjC?Uy)&B*e3Y3Ou)r+aY zAm?I1OX(h2WPIX{XNOmKk)DJMvdM90cY8ZIXAGmEKnI;j;ZEp&P^>v-RcHpf zATFca3}wPr!6@lE|z-=3h}Rp)AEAwxwh z4IIJ?bnW{$%hWU6fPtkY9v?k#@A@`Y1 z7@=*h($U04YSZr#Zm3r!hE1Mi0oiZJ;Gwib+-EI<@vW4_Q~iVtrIfVmLf++=K(9@^ z+Nx-DoN_ltx~dKt)rzc9+2pOHMVWBX;&A&E7xKoH$HzB23Y$kt=+>@$p%dl3bhbhj zJCvY)(<{^kujjze(gAP#giwrzO;{+VVN}! zeIBeUxH}lr%19;ocpo01-#zL4o<0F`JkL;{Hit9?;S>>2N3-jm;RLJA0UGmwxi*8A zJ3b|5-{^8fKG@v(oWdknB08f7sz?4ULNIWxJk@zK9GNDI8<6)1=^Yx%OUp^rxcJBC64Dt7WB&#j06f z!3*yorZhVm;2mbK$KI2WdCc1lj6UE!EzVFtLbPZnB|Bp^4;(x(O%MCIh0L*pE zGkT%eGyM!fs2;;)_vL0ZsUdSa;*@XUq3QTvYj^F45Xht}?MXBuYmUt(jCsgOI~y9Y zP%vi^RK2VfIWwT}=C|_HFVg@zzfsn0p$mRjFi&y>C?`&^6$C%L4LA9O`{Eh)} z)v2S;H039%Z>(v>dIrw@9+%CncLn6tsLM8!-IM??&6Z?)Vx z;&g0<$7kI*Jhu(YV|+muGSowPUq17F0oq)(SmJ+&+ix7Ug9N{E@Rl0rw`0NtIiQHz z)XpJC8=5OvOT0<&`YwyY&pjvU)lMl*+ukQ67eb8ml`sY>`6C=gLLr_mo2x~+D;#eX zJB7}>1HtHtOCJGSxFQDRY&`MQ3ZO+JvwT#my!Sk^ANQ&HrjG3AHgw^T?(T~1NNsR- zmEcUOQgv!orVUfQ12OyG9djt%e)SqHL+|Mm+II1`wJZu!zAmyY;F0lb(T^^1`7q{^ zh7b&m);5(tI2BFo(>RUO_yTYuXvL?a{Dv|ut@Qg#MCaSp=A9VE68)qQq1J$?%{8|j z(rhRt*ho4@f!X?wrf)p7$$)M%Jj~aheadp|Wb1Mk^PBrMu`(Qv0JG-GsZArXm!f?! zw)$ATY_Mfhb)1S=wXmo5=8;}=qX&GlltN!aY%zdM-ef`sXb|;QP)=KUKS-cM$Ro1# z@^5rj*Lzyw@S5heVk~q`HqZQmnM;^o;uJE(+Lb(PE2S%Vwv}O)^+@z8C|Z#vaF`7V z_YMWSXd1Th^Y_^MgG~wwmPYBMDO5{pb(untt2h?hL!sZ6QqYtamULNRXaLOd38Ui;0KhT8}Ifq;L+p>#Smj$TiP z>e!M!+OmIWU&%$qe?eNzZ-18NC20A@u0sq z3(90TC;As{X_3M?KMn?2T+$+7_cbj7#4X@&ju#LgVDJCJ_>1D2B3V|0znV9s6A?k# zS=C!s@3dCmv;}w`gMiuz{tRtWgd-p8P$TKp{VTl*R!&`q$s?-;+I%&?G1L36PI_w)V7ppo36zw6C6N zh^?cTrK78Zg>Aj<|4ee_wvHAKlD;=XRKfu0YH;DVK!F{<>s06ZEQ=G+V(6(&lJ;f@ zr;JOI^l!UCC-PdW)!eDg9vEQ+fD2sYrMe;D-_=XgCT^<*@ux;#Fgt1|r+WKY_lkg{ z5B4G|31?$1Ag9S9YT!U>3)KgyJbdZ8n*XWwHG`1S;?w*K6v^Ug%mc)d{k;I{cm^M%O=RjH{(sT*-;qok^s&FGKUeXXA*PqznojZN*3 zkDAwRd*!lar4MgaTCt6~Lx084FVEX!-OHvWP%darFB6L2o1deWDqvPGt-<1@VE!a^ zhvExf%fw86M74EJm~|lYUY7RLlH4l=x)ZS&*RymcP4gv>o-Qx4w+qF}#<&+gp18>9QFY2L+!EJNs== z&$`+=*fpMTMXEo#jZ<(vA$ave&$*#1MVnO1;;wX-S)ZD@I>_?=3i6?UczW{ccrT=5 z5Ud-~i#vQGQ<+}MpWtZYfLJX-!hE4}CQ{AL3O*+Ay&7r5eVO`Hvr`RwTj(|%rI62} z$`gEZr(Eu3p|YqAnIjtR8-P6A(wy5mP=z{DH33?qkw1h@*NzYX57t%9ql9Fyu@7|A z3Jj&h0)F-iCbbf_4-=*fNN@)D1gI4KRp00RXVPqV0BejWCuaM&pmFBh#T}UA!z6mg zCv`hKhXn=q2+|RklY2wcLc{bN8b7#=8I>?pLXY}(9D61bTd`b8T~LJf7J#-}aU?DQ z9kB2$w{s>U#}bRe$p#z;#{hq;RHz@QjE|ydHE4AOVU4|10v^g@BYk0FoZBM0Ds3dQ z8Z)(Jw+7%AT-6i2$p1#UzLIsWnr3ToKVd4sTu>dtGFM21oqfv^G3X`O(`}79uTsDjWPX9B%Z&^1nQ}J1{?0omt@xWbF;=* z#E=q;JbjPXcjFrzf8I1cUG4(%&tY)zZsE}Ka3F1$Mk;8jddyTkN>5P0!$vYx`Y4Nm zKC7p0WcNeeKKVpd76xdt*U^mu*?55P^OoOLngIp_0-6KqQR*aMe|TS1#+f3i=FT)F z9LYT%#$9X3{Oj;L)832eoeG30C#iK@+hoINU^S9_EysCDI8_@;=`1>O6SzRtkn_e< zK|pzf370dP1{CnSl!z5-iwtNe}D_6+yqMQ0--K$;suCF|5r(uOhXi3waNO11ZjcX@G_4-+n3g?rgx~j!YE(r*q z-4l$qbDFHTa|H=QiA1W8<|Q|4w|&cka8~YhjnFJ--E(70HU1h}fHufVA`$j+HO(To zgR7uo^}JoVT}~J)ieWuPK0CFs;Dlp^w9ayww0IhR3V|RgMUyxg5PwX2=?(X$7y1I| zdAr4)gyn*arDMNbUOgli%sF<|7zg&LvmA^0OQY>^dJ}DF=Ce%plVycDP7;Gn>_+3T z^{~-YBO{eCi!#CN(gsr}N#L0jLTp1Uo74F4jYoto>S>bD?C^N1#_G;MgTuQ{-f2{| zZZK%xa_BlDNP#+_O9gK5*{w;3QEH)rN9hP0pg0U{c(^gqR3%B_)HNfn)8oywqjkp= zEA?@)ZG&}`KbyMo^Wi8wBy;A5nn%GD!=^GxLG@p31o5L53~3s3SdP{A!_jzUe70 z-_O0jLtk5fwrBZkhZ4i+T5HKp_|BCe1^N`sqA)uyZ`fCz^$hg5M z)R-3FF^g^#TVPH_ic|t@gBMftN|HSsm9DCrL}@yNUAtWz5x=)y^RB{p#A z@H61;Xe`hzCad^A7+6l^ZQyCj$n#{PA@xzvPPMGlp>6Swo5tQdR`R6yHEqR&ip_ zRT;RNhf$@`6lY_mN)1J}wHiZF0{DCuYYfT&Qnr)0LK}T%A*6=0$ga%*c0<#2%toce zOu$mvbUJ}LKQXq3#p!3daC{-*EI^Pa%k;%l$taIac1Fr!D1w!K3q~iXDKbnrL;Bsh zW62acSrey29`f8}2^%YRk0nZ1=#OS_oW2=yZaxxgIt35WH9ptDIcpl?ifaGLT>7 zKmD9UJ^+h5-@{nvLD!swf%J_HhUkEHwvP`APi%Db710!mNlNQ=#~Dit-+PL({ul7* zh9t5y?#~y!Fe}+Bu-7YV z#^waP`3M1=ZO7b#bGt)h#+Yu$=HD#(nLX|8=?)3nELn*s%*xX!p+p5K=9QM45~^!$G`1Z`C-N`2fwrZXBe)WqJMaxp>^k|!T;;)yn~uxwmwW~B1n)H zx*}4gh9Zd6AW9MG(t;S2AVrEu6I7}QNDI;euRxG4AOb2%3+1I4N4JUpU!EnrCCExq#6R z`|jGSm!Ts5cAk7*7979&SHKU@h}8C)uDE`eY~QHktH@8oS9*3sF!~NmCRKp?>l${B#YPme zj415NuMA($Nbw>_yg?}q7yaB;?N)wpWT3eOC-3VDX3ugU^d!fY5#)|Sn0G!A{1`Uf z)o)RkRCim-nY$dQ?*q=ih0S#C)7TQM`Vdgy-jjj6il4N51o0;k0|Y%&DB{Eb{(@bn zx!Bgn2^EA7NFTS7lr+SMYK1O@Gh3pMm-a<6$Mu?K?kAvo49-e&F7|wHjp?|EX6B{a z5+cCX+jH!OkPWC|z6H~dEP-Q-1 zW~Bsk-U_2mafM5jDTnZh1P(aS!%1{ap867b!_EX)Kfw=XI%6o>anstPQQ0eRH{&@W z%GO~SzWAdY1lDVQ)I6&8#nfSk=-pYAnFLZX@bfmB@LKdLm(5c%bLkvuCk;}hB5U^| z_r&)5Gj7}q1cRBF6K8udZG7xyFSFs=mvC|u`Wip|#-6srvu+aQnDnNd)_($G7@C*> zF{TAN7Jvn0whJs}&DqTE5;sOj@V^yn^aCAcX&tI>W zmwd;k`Sp$+z#6kXLWCAg;OyiYZTI4^edY4T4dDt!AW?M4}f1iUFBo0@t{5pm94{N%h^K ze>H4Jv>IgAO=qbF4Fv%!TY4BNF)cU0+7qvyemZ$9NbKP^#x*f3^Ke&bR0q4lExw{( z^T@osWU2VNq~V*nQSwe_I7OQ9z~AI(OfJKk+D@geZ$y(PTqB#L57av23-I7^=h&*P z(in9GFv5Ijdii+Q^Ff$CvM8g$`o`!n+ThORpg6%Ar9s|}I}RLxppFm#u``rb~j56v`t^23Sutcotovng+9_mZ&wg>>_ z$z$pcK0L4Z__5I9&c@f4BPng!C)DF;b@iFYRt9fDT~BsT>?bvd4lF1`6ydd>+eOPPbjz3L z5E$ZA7{8g_B$g=i9Q^UAzQlo;(1`q?yg9Fdnl~`*``4Y6)1ZS$*8!WTfU+7etJ7%l zj+sn=;Km*D{_a=&2@6Tq((4lG1L_YIWUb0`3v3b6J!v-SsTOZi6;5$P%@Ak7%q#D> z+4UrMP9rQ>VCRbTVR<|+bi4_53T^UvrUw>@;b*)PJWjLP=e(4Xe~0*vQCvS86Y;#u zH(t1|w))f+e3a~4#aZ!=18)TT)?JH5K_I{T=BsN6Q52cOMQpFU5Pu+>vM@FtL=U=G?D1JJ!K>e%HlQfdN$voiEdq}CrWdt>=B*^o5TTp%ujOtD0}sT0{kXd_m<9 z_sT0H8X~h7X)~zHCG~FXE{S~l!Q>JiFW)`>m%dByL#Fuh_9oS^bn^`4VdJQ?Y~>lR zI`|~-3#&kxPSAp;)c(NoQ7fxoX>Dk8eX;^3<@1?v8yMyibu|{-VmC3<;E?9u+Ug!u z_@hqk)5SWir5A(|jL(P|)ok-^5?d1B@tMs^(Lm9qF&WR1lOgCa)5a);KC<6d&qO5! zXh1=4{-E{zp8a}xRla~nm-0~ZC}HK5U&wB#9|kR{62qdja9^+Jl$KCsGC^9zT-Ru& z#qqS5z>IZ63B@0lzKj!>4~mY)oEYvMANIU5s7Dp5l_S0*u{`HuWa(Vkvh-xp*6@C0 z>l+&vP!M;tka&GNd)>uU#PMR2nxSVAv|mY!5bNl_tpQ3JhP^o;Fg;G-Q6VwM*TEUr z-Z`dkbTW_(eEXeE07uf;R@~l^*tm7c@Y}%CaTUbScGEuFeE>L;cWDt2M`yIhcJrHq zcan0Yy%{_VV&?_0-IrP7Uo-0B0Xj_m|8F-+KqQ(!3Z%4wbVwQE z|MGD?-NnQ7S7U}9K!8JT;z^|QP+@m5OD;6F?6?mLZhSXx7C+1A zcY;@+=$#$t)x?fX4@mliNRZQ)2cvc)wGaA7;Ba-#{OEH-cS9ha1?o)g>}SFlL95{T zkDx88ybuC)JF`ev79_s5$|p5)tg-%I(2gX4nH%hJt5C)v@!aY2jYGzl8oQ#Zvy!vQ z&5_ln?-FCHlS^S17AY<@)zw+H?_zCH#)twh&y-U0n%G7cL}@Zi-Nx1xQ5}p5_C>-_ z=!L6rLh`Vk(lho)&-jHd#IK~uZ8QelxI^zfv3Nh7_cI#EbhIYfVef3?D@N@3J N;!Xd`D=`/dev/null || \ + PATH=/sbin:/usr/sbin/ command -v chroot 2>/dev/null || \ + echo chroot \ +) +MAKEFLAGS += --warn-undefined-variables +MAKEFLAGS += --no-builtin-rules +.PHONY: all all-at-once all-with-make clean-stage clean deepclean iso \ + verify-all-pkgs-checksums verify-pkgs-checksums update-pkgs-checksums \ + verify-all-nix-stage4-checksums verify-all-nix-stage5-checksums \ + verify-all-nix-plain-checksums verify-nix-plain-checksums \ + me-suffer +NPROC ?= 1 # for inner make invocations, one can pass -j# this way +USE_CCACHE ?= 0 # for faster iterative debugging only +USE_NIX_CACHE ?= 0 # for faster iterative debugging only +USE_DISORDERFS ?= 0 # for more thorough reproducibility testing +SAVE_MISMATCHING_BUILD_TREES ?= 0 # for more thorough reproducibility testing + +SOURCE_DATE_EPOCH ?= $(shell date '--date=01 Jan 1970 00:00:00 UTC' +%s) +TAR := tar +TAR_REPR = $(TAR) --sort=name '--mtime=@$(SOURCE_DATE_EPOCH)' \ + --owner=0 --group=0 --numeric-owner \ + --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime + +downloads/%: + @echo "### Makefile: downloading required $*..." + ONLY="$*" ./download.sh + [[ -e downloads/$* ]] + +################################################################################ + +stage/protosrc: seed.sh +stage/protosrc: recipes/1-stage1/syscall.h +stage/protosrc: recipes/1-stage1/seed.host-executed.sh +stage/protosrc: downloads/musl-1.2.4.tar.gz +stage/protosrc: downloads/busybox-1.36.1.tar.bz2 +stage/protosrc: downloads/tinycc-mob-af1abf1.tar.gz + env DESTDIR=stage recipes/1-stage1/seed.host-executed.sh + +NIXPKGS_HASH=21f524672f25f8c3e7a0b5775e6505fee8fe43ce +TCC_CHECKSUM=05aad934985939e9997127e93d63d6a94c88739313c496f10a90176688cc9167 +tcc-seed: + @echo '### Makefile: you are supposed to supply a trusted tcc-seed' + @echo '### Makefile: since you have not, building one from nixpkgs...' + cat $$(nix build "nixpkgs/${NIXPKGS_HASH}#pkgsStatic.tinycc.out" \ + --no-link --print-out-paths)/bin/tcc > tcc-seed + chmod +x tcc-seed + sha256sum -c <<<"$(TCC_CHECKSUM) tcc-seed" + @echo '### Makefile: using tcc-seed built with nix' + +# Stage 0 is special in that there are no sources, we just pack up tcc-seed +pkgs/0-tcc-seed.pkg: tcc-seed + @echo '### Makefile: special stage 0: just packing up tcc-seed...' + mkdir -p tmp/build/0-tcc-seed/store pkgs + cp tcc-seed tmp/build/0-tcc-seed/store/0-tcc-seed + $(TAR_REPR) -Izstd -cf pkgs/0-tcc-seed.pkg -C tmp/build/0-tcc-seed \ + store/0-tcc-seed + rm -rf tmp/build/0-tcc-seed + @echo '### Makefile: successfully packed up pkgs/0-tcc-seed.pkg' + +# Stage 1 is special in that: +# * we patch up some sources on the host +# * we have no shell and execute 1-stage1.c with tcc-seed +pkgs/1-stage1.pkg: pkgs/0-tcc-seed.pkg +pkgs/1-stage1.pkg: recipes/1-stage1.c +pkgs/1-stage1.pkg: recipes/1-stage1/seed.host-executed.sh +pkgs/1-stage1.pkg: recipes/1-stage1/syscall.h +pkgs/1-stage1.pkg: recipes/1-stage1/protobusybox.c +pkgs/1-stage1.pkg: recipes/1-stage1/protobusybox.h +pkgs/1-stage1.pkg: recipes/1-stage1/hello.c +pkgs/1-stage1.pkg: downloads/musl-1.2.4.tar.gz +pkgs/1-stage1.pkg: downloads/tinycc-mob-af1abf1.tar.gz +pkgs/1-stage1.pkg: downloads/busybox-1.36.1.tar.bz2 +pkgs/1-stage1.pkg: + @echo "### Makefile: creating temporary builddir tmp/build/1-stage1..." + rm -rf tmp/build/1-stage1 + DISORDER=$(USE_DISORDERFS) helpers/builddir create tmp/build/1-stage1 + @echo "### Makefile: injecting dependencies..." + helpers/inject tmp/build/1-stage1 $^ + @echo "### Makefile: seeding special stage 1 (and patching sources)..." + DESTDIR=tmp/build/1-stage1 recipes/1-stage1/seed.host-executed.sh + @echo "### Makefile: special stage 1: executing stage1.c with tcc-seed" + DISORDER=$(USE_DISORDERFS) helpers/builddir pre-build tmp/build/1-stage1 + env -i unshare -nr $(CHROOT) ./tmp/build/1-stage1 \ + /store/0-tcc-seed -nostdinc -nostdlib -Werror \ + -run recipes/1-stage1.c + DISORDER=$(USE_DISORDERFS) \ + helpers/builddir post-build tmp/build/1-stage1 + $(TAR_REPR) -Izstd -cf pkgs/1-stage1.pkg -C tmp/build/1-stage1 \ + store/1-stage1 + DISORDER=$(USE_DISORDERFS) helpers/builddir remove tmp/build/1-stage1 + @echo "### Makefile: 1-stage1 has been built as pkgs/1-stage1.pkg" + +# Consequent stages split up into packages have it simpler: +pkgs/%.pkg: recipes/%.sh + @echo "### Makefile: creating a temporary build area tmp/build/$*..." + if ! rm -rf "tmp/build/$*" 2>/dev/null; then \ + chmod -R +w "tmp/build/$*"; \ + rm -rf "tmp/build/$*"; \ + fi + [ ! -e "tmp/build/$*" ] + DISORDER=$(USE_DISORDERFS) helpers/builddir create "tmp/build/$*" + helpers/inject "tmp/build/$*" $^ +ifeq ($(USE_CCACHE), 1) + @echo "### Makefile: unpacking ccache from previous builds $*..." + mkdir -p "tmp/build/$*/ccache" + [[ ! -e "tmp/ccache/$*.tar.zstd" ]] || \ + tar -Izstd -xf "tmp/ccache/$*.tar.zstd" -C "tmp/build/$*/ccache" + ln -sf /store/_2a0-ccache/wrap-available "tmp/build/$*/ccache/setup" + ln -sf /store/_2a0-ccache/bin "tmp/build/$*/ccache/bin" +endif +ifeq ($(USE_NIX_CACHE), 1) + @echo "### Makefile: unpacking nix store and db from previous build..." + if [[ $* =~ .*-using-nix ]] && [[ -e "pkgs/$*.pkg" ]]; then \ + mkdir "tmp/build/$*/prev/"; \ + tar --strip-components=2 \ + -xf "pkgs/$*.pkg" -C "tmp/build/$*/prev/"; \ + fi +endif + DISORDER=$(USE_DISORDERFS) helpers/builddir pre-build "tmp/build/$*" + @echo "### Makefile: building $* ..." + env \ + DESTDIR="./tmp/build/$*" \ + NPROC="$(NPROC)" \ + SOURCE_DATE_EPOCH="$(SOURCE_DATE_EPOCH)" \ + ./helpers/chroot "/recipes/$*.sh" + DISORDER=$(USE_DISORDERFS) helpers/builddir post-build "tmp/build/$*" + @echo "### Makefile: packing up $* ..." + $(TAR_REPR) -Izstd -cf "pkgs/$*.pkg" -C "tmp/build/$*" "store/$*" +ifeq ($(USE_CCACHE), 1) + @echo "### Makefile: packing up $* ccache cache..." + if [[ -e "tmp/build/$*/store/_2a0-ccache/bin/ccache" ]]; then \ + mkdir -p tmp/ccache; \ + unshare -nr $(CHROOT) "tmp/build/$*" \ + /store/_2a0-ccache/bin/ccache -sz; \ + $(TAR_REPR) -Izstd -cf "tmp/ccache/$*.tar.zstd" \ + -C "tmp/build/$*/ccache" .; \ + fi + rm -rf "tmp/build/$*/store/_2a0-ccache" + rm -rf "tmp/build/$*/ccache" +endif +ifeq ($(SAVE_MISMATCHING_BUILD_TREES), 1) + computed_csum=$$(zstd -qcd "pkgs/$*.pkg" | sha256sum); \ + computed_csum=$$(<<<$$computed_csum tr ' ' '\t' | cut -f1); \ + if ! grep -q "$$computed_csum pkgs/$*" verify.pkgs.sha256; then \ + short_csum=$$(<<<$$computed_csum head -c7); \ + echo "### Makefile: packing up $* buildtree"; \ + mkdir -p trees; \ + $(TAR_REPR) -Izstd -cf "trees/$*-$$short_csum.pkg" \ + -C "tmp/build/$*" .; \ + fi +endif + @echo "### Makefile: cleaning up after $*" + DISORDER=$(USE_DISORDERFS) helpers/builddir remove "tmp/build/$*" + @echo "### Makefile: $* has been built as pkgs/$*.pkg" + +# Dependency graph: + +pkgs/2a0-static-gnumake.pkg: pkgs/1-stage1.pkg +pkgs/2a0-static-gnumake.pkg: downloads/make-4.4.1.tar.gz + +ifeq ($(USE_CCACHE), 1) +pkgs/_2a0-ccache.pkg: pkgs/1-stage1.pkg +pkgs/_2a0-ccache.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/_2a0-ccache.pkg: downloads/ccache-3.7.12.tar.xz +endif + +pkgs/2/01-gnumake.pkg: pkgs/1-stage1.pkg +pkgs/2/01-gnumake.pkg: pkgs/2/00-intermediate-gnumake.pkg +pkgs/2/01-gnumake.pkg: downloads/make-4.4.1.tar.gz + +pkgs/2a1-static-binutils.pkg: pkgs/1-stage1.pkg +pkgs/2a1-static-binutils.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a1-static-binutils.pkg: downloads/binutils-2.39.tar.xz + +pkgs/2a2-static-gnugcc4-c.pkg: pkgs/1-stage1.pkg +pkgs/2a2-static-gnugcc4-c.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a2-static-gnugcc4-c.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a2-static-gnugcc4-c.pkg: downloads/gmp-4.3.2.tar.xz +pkgs/2a2-static-gnugcc4-c.pkg: downloads/mpfr-2.4.2.tar.xz +pkgs/2a2-static-gnugcc4-c.pkg: downloads/mpc-0.8.1.tar.gz +pkgs/2a2-static-gnugcc4-c.pkg: downloads/gcc-4.7.4.tar.bz2 + +pkgs/2a3-intermediate-musl.pkg: pkgs/1-stage1.pkg +pkgs/2a3-intermediate-musl.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a3-intermediate-musl.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a3-intermediate-musl.pkg: pkgs/2a2-static-gnugcc4-c.pkg +pkgs/2a3-intermediate-musl.pkg: downloads/musl-1.2.4.tar.gz + +pkgs/2a4-gnugcc4-cpp.pkg: pkgs/1-stage1.pkg +pkgs/2a4-gnugcc4-cpp.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a4-gnugcc4-cpp.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a4-gnugcc4-cpp.pkg: pkgs/2a2-static-gnugcc4-c.pkg +pkgs/2a4-gnugcc4-cpp.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2a4-gnugcc4-cpp.pkg: downloads/gmp-4.3.2.tar.xz +pkgs/2a4-gnugcc4-cpp.pkg: downloads/mpfr-2.4.2.tar.xz +pkgs/2a4-gnugcc4-cpp.pkg: downloads/mpc-0.8.1.tar.gz +pkgs/2a4-gnugcc4-cpp.pkg: downloads/gcc-4.7.4.tar.bz2 + +pkgs/2a5-gnugcc10.pkg: pkgs/1-stage1.pkg +pkgs/2a5-gnugcc10.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a5-gnugcc10.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a5-gnugcc10.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2a5-gnugcc10.pkg: pkgs/2a4-gnugcc4-cpp.pkg +pkgs/2a5-gnugcc10.pkg: downloads/gcc-10.5.0.tar.xz +pkgs/2a5-gnugcc10.pkg: downloads/gmp-6.1.0.tar.xz +pkgs/2a5-gnugcc10.pkg: downloads/mpc-1.0.3.tar.gz +pkgs/2a5-gnugcc10.pkg: downloads/mpfr-3.1.4.tar.xz +pkgs/2a5-gnugcc10.pkg: downloads/isl-0.18.tar.bz2 + +pkgs/2a6-linux-headers.pkg: pkgs/1-stage1.pkg +pkgs/2a6-linux-headers.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a6-linux-headers.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a6-linux-headers.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2a6-linux-headers.pkg: pkgs/2a5-gnugcc10.pkg +pkgs/2a6-linux-headers.pkg: downloads/linux-6.4.12.tar.xz + +pkgs/2a7-cmake.pkg: pkgs/1-stage1.pkg +pkgs/2a7-cmake.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a7-cmake.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a7-cmake.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2a7-cmake.pkg: pkgs/2a5-gnugcc10.pkg +pkgs/2a7-cmake.pkg: pkgs/2a6-linux-headers.pkg +pkgs/2a7-cmake.pkg: downloads/cmake-3.27.4.tar.gz + +pkgs/2a8-python.pkg: pkgs/1-stage1.pkg +pkgs/2a8-python.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a8-python.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a8-python.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2a8-python.pkg: pkgs/2a5-gnugcc10.pkg +pkgs/2a8-python.pkg: downloads/Python-3.12.0.tar.xz + +pkgs/2a9-intermediate-clang.pkg: pkgs/1-stage1.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a5-gnugcc10.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a6-linux-headers.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a7-cmake.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/2a8-python.pkg +pkgs/2a9-intermediate-clang.pkg: downloads/llvm-project-17.0.1.src.tar.xz + +pkgs/2b0-musl.pkg: pkgs/1-stage1.pkg +pkgs/2b0-musl.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2b0-musl.pkg: pkgs/2a1-static-binutils.pkg +pkgs/2b0-musl.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2b0-musl.pkg: pkgs/2a9-intermediate-clang.pkg +pkgs/2b0-musl.pkg: downloads/musl-1.2.4.tar.gz + +pkgs/2b1-clang.pkg: pkgs/1-stage1.pkg +pkgs/2b1-clang.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2b1-clang.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/2b1-clang.pkg: pkgs/2a6-linux-headers.pkg +pkgs/2b1-clang.pkg: pkgs/2a7-cmake.pkg +pkgs/2b1-clang.pkg: pkgs/2a8-python.pkg +pkgs/2b1-clang.pkg: pkgs/2a9-intermediate-clang.pkg +pkgs/2b1-clang.pkg: pkgs/2b0-musl.pkg +pkgs/2b1-clang.pkg: downloads/llvm-project-17.0.1.src.tar.xz + +pkgs/2b2-busybox.pkg: pkgs/1-stage1.pkg +pkgs/2b2-busybox.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2b2-busybox.pkg: pkgs/2b0-musl.pkg +pkgs/2b2-busybox.pkg: pkgs/2b1-clang.pkg +pkgs/2b2-busybox.pkg: pkgs/2a6-linux-headers.pkg +pkgs/2b2-busybox.pkg: downloads/busybox-1.36.1.tar.bz2 + +pkgs/2b3-gnumake.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/2b3-gnumake.pkg: pkgs/2b0-musl.pkg +pkgs/2b3-gnumake.pkg: pkgs/2b1-clang.pkg +pkgs/2b3-gnumake.pkg: pkgs/2b2-busybox.pkg +pkgs/2b3-gnumake.pkg: downloads/make-4.4.1.tar.gz + +pkgs/3a-sqlite.pkg: pkgs/2b0-musl.pkg +pkgs/3a-sqlite.pkg: pkgs/2b1-clang.pkg +pkgs/3a-sqlite.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-sqlite.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-sqlite.pkg: downloads/sqlite-autoconf-3430000.tar.gz + +pkgs/3a-boost.pkg: pkgs/2b0-musl.pkg +pkgs/3a-boost.pkg: pkgs/2b1-clang.pkg +pkgs/3a-boost.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-boost.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-boost.pkg: pkgs/2a6-linux-headers.pkg +pkgs/3a-boost.pkg: downloads/boost_1_83_0.tar.bz2 + +pkgs/3a-mbedtls.pkg: pkgs/2b0-musl.pkg +pkgs/3a-mbedtls.pkg: pkgs/2b1-clang.pkg +pkgs/3a-mbedtls.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-mbedtls.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-mbedtls.pkg: downloads/mbedtls-3.4.1.tar.gz + +pkgs/3a-pkg-config.pkg: pkgs/2b0-musl.pkg +pkgs/3a-pkg-config.pkg: pkgs/2b1-clang.pkg +pkgs/3a-pkg-config.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-pkg-config.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-pkg-config.pkg: downloads/pkg-config-0.29.2.tar.gz + +pkgs/3a-curl.pkg: pkgs/2b0-musl.pkg +pkgs/3a-curl.pkg: pkgs/2b1-clang.pkg +pkgs/3a-curl.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-curl.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-curl.pkg: pkgs/3a-mbedtls.pkg +pkgs/3a-curl.pkg: pkgs/3a-pkg-config.pkg +pkgs/3a-curl.pkg: downloads/curl-8.2.1.tar.xz + +pkgs/3a-editline.pkg: pkgs/2b0-musl.pkg +pkgs/3a-editline.pkg: pkgs/2b1-clang.pkg +pkgs/3a-editline.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-editline.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-editline.pkg: downloads/editline-1.17.1.tar.xz + +pkgs/3a-brotli.pkg: pkgs/2b0-musl.pkg +pkgs/3a-brotli.pkg: pkgs/2b1-clang.pkg +pkgs/3a-brotli.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-brotli.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-brotli.pkg: downloads/brotli-1.0.9.tar.gz + +pkgs/3a-gnugperf.pkg: pkgs/2b0-musl.pkg +pkgs/3a-gnugperf.pkg: pkgs/2b1-clang.pkg +pkgs/3a-gnugperf.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-gnugperf.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-gnugperf.pkg: downloads/gperf-3.1.tar.gz + +pkgs/3a-seccomp.pkg: pkgs/2b0-musl.pkg +pkgs/3a-seccomp.pkg: pkgs/2b1-clang.pkg +pkgs/3a-seccomp.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-seccomp.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-seccomp.pkg: pkgs/3a-gnugperf.pkg +pkgs/3a-seccomp.pkg: pkgs/2a6-linux-headers.pkg +pkgs/3a-seccomp.pkg: downloads/libseccomp-2.5.4.tar.gz + +pkgs/3a-libarchive.pkg: pkgs/2b0-musl.pkg +pkgs/3a-libarchive.pkg: pkgs/2b1-clang.pkg +pkgs/3a-libarchive.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-libarchive.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-libarchive.pkg: pkgs/3a-pkg-config.pkg +pkgs/3a-libarchive.pkg: downloads/libarchive-3.7.1.tar.xz + +pkgs/3a-libsodium.pkg: pkgs/2b0-musl.pkg +pkgs/3a-libsodium.pkg: pkgs/2b1-clang.pkg +pkgs/3a-libsodium.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-libsodium.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-libsodium.pkg: pkgs/3a-pkg-config.pkg +pkgs/3a-libsodium.pkg: downloads/libsodium-1.0.18.tar.gz + +pkgs/3a-lowdown.pkg: pkgs/2b0-musl.pkg +pkgs/3a-lowdown.pkg: pkgs/2b1-clang.pkg +pkgs/3a-lowdown.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-lowdown.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-lowdown.pkg: downloads/lowdown-1.0.2.tar.gz + +pkgs/3a-nlohmann-json.pkg: pkgs/2b0-musl.pkg +pkgs/3a-nlohmann-json.pkg: pkgs/2b1-clang.pkg +pkgs/3a-nlohmann-json.pkg: pkgs/2b2-busybox.pkg +pkgs/3a-nlohmann-json.pkg: pkgs/2b3-gnumake.pkg +pkgs/3a-nlohmann-json.pkg: downloads/nlohmann-json-3.11.2.tar.xz + +pkgs/3b-busybox-static.pkg: pkgs/2b0-musl.pkg +pkgs/3b-busybox-static.pkg: pkgs/2b1-clang.pkg +pkgs/3b-busybox-static.pkg: pkgs/2b2-busybox.pkg +pkgs/3b-busybox-static.pkg: pkgs/2b3-gnumake.pkg +pkgs/3b-busybox-static.pkg: pkgs/2a6-linux-headers.pkg +pkgs/3b-busybox-static.pkg: downloads/busybox-1.36.1.tar.bz2 + +pkgs/3b-tinycc-static.pkg: pkgs/2b0-musl.pkg +pkgs/3b-tinycc-static.pkg: pkgs/2b1-clang.pkg +pkgs/3b-tinycc-static.pkg: pkgs/2b2-busybox.pkg +pkgs/3b-tinycc-static.pkg: pkgs/2b3-gnumake.pkg +pkgs/3b-tinycc-static.pkg: downloads/tinycc-mob-af1abf1.tar.gz + +pkgs/3b-nix.pkg: pkgs/2b0-musl.pkg +pkgs/3b-nix.pkg: pkgs/2b1-clang.pkg +pkgs/3b-nix.pkg: pkgs/2b2-busybox.pkg +pkgs/3b-nix.pkg: pkgs/2b3-gnumake.pkg +pkgs/3b-nix.pkg: pkgs/2a6-linux-headers.pkg +pkgs/3b-nix.pkg: pkgs/3a-sqlite.pkg +pkgs/3b-nix.pkg: pkgs/3a-boost.pkg +pkgs/3b-nix.pkg: pkgs/3a-pkg-config.pkg +pkgs/3b-nix.pkg: pkgs/3a-curl.pkg +pkgs/3b-nix.pkg: pkgs/3a-editline.pkg +pkgs/3b-nix.pkg: pkgs/3a-brotli.pkg +pkgs/3b-nix.pkg: pkgs/3a-seccomp.pkg +pkgs/3b-nix.pkg: pkgs/3a-libarchive.pkg +pkgs/3b-nix.pkg: pkgs/3a-libsodium.pkg +pkgs/3b-nix.pkg: pkgs/3a-lowdown.pkg +pkgs/3b-nix.pkg: pkgs/3a-nlohmann-json.pkg +pkgs/3b-nix.pkg: pkgs/3b-busybox-static.pkg +pkgs/3b-nix.pkg: downloads/queue.h +pkgs/3b-nix.pkg: downloads/nix-2.17.0-zilched.tar.xz + +pkgs/4-rebootstrap-using-nix.pkg: pkgs/2b0-musl.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/2b1-clang.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/2b2-busybox.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-boost.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-pkg-config.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-sqlite.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-curl.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-editline.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-brotli.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-seccomp.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-libarchive.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-libsodium.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-lowdown.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3a-nlohmann-json.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3b-tinycc-static.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3b-busybox-static.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/3b-nix.pkg +### +pkgs/4-rebootstrap-using-nix.pkg: stage/protosrc +### +pkgs/4-rebootstrap-using-nix.pkg: recipes/1-stage1.c +pkgs/4-rebootstrap-using-nix.pkg: recipes/1-stage1/seed.host-executed.sh +pkgs/4-rebootstrap-using-nix.pkg: recipes/1-stage1/syscall.h +pkgs/4-rebootstrap-using-nix.pkg: recipes/1-stage1/protobusybox.c +pkgs/4-rebootstrap-using-nix.pkg: recipes/1-stage1/protobusybox.h +pkgs/4-rebootstrap-using-nix.pkg: recipes/1-stage1/hello.c +### +pkgs/4-rebootstrap-using-nix.pkg: default.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/1-stage1.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a0-static-gnumake.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a1-static-binutils.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a2-static-gnugcc4-c.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a3-intermediate-musl.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a4-gnugcc4-cpp.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a5-gnugcc10.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a6-linux-headers.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a7-cmake.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a8-python.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2a9-intermediate-clang.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2b0-musl.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2b1-clang.nix +pkgs/4-rebootstrap-using-nix.pkg: using-nix/2b2-busybox.nix +### +pkgs/4-rebootstrap-using-nix.pkg: downloads/make-4.4.1.tar.gz +pkgs/4-rebootstrap-using-nix.pkg: downloads/binutils-2.39.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/gcc-4.7.4.tar.bz2 +pkgs/4-rebootstrap-using-nix.pkg: downloads/gmp-4.3.2.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/mpfr-2.4.2.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/mpc-0.8.1.tar.gz +pkgs/4-rebootstrap-using-nix.pkg: downloads/musl-1.2.4.tar.gz +pkgs/4-rebootstrap-using-nix.pkg: downloads/gcc-10.5.0.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/gmp-6.1.0.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/mpfr-3.1.4.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/mpc-1.0.3.tar.gz +pkgs/4-rebootstrap-using-nix.pkg: downloads/isl-0.18.tar.bz2 +pkgs/4-rebootstrap-using-nix.pkg: downloads/linux-6.4.12.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/cmake-3.27.4.tar.gz +pkgs/4-rebootstrap-using-nix.pkg: downloads/Python-3.12.0.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/llvm-project-17.0.1.src.tar.xz +pkgs/4-rebootstrap-using-nix.pkg: downloads/busybox-1.36.1.tar.bz2 + +pkgs/5-go-beyond-using-nix.pkg: pkgs/2b0-musl.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/2b1-clang.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/2b2-busybox.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-boost.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-pkg-config.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-sqlite.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-curl.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-editline.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-brotli.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-seccomp.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-libarchive.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-libsodium.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-lowdown.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3a-nlohmann-json.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3b-tinycc-static.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3b-busybox-static.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/3b-nix.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/4-rebootstrap-using-nix.pkg +### +pkgs/5-go-beyond-using-nix.pkg: stage/protosrc +### +pkgs/5-go-beyond-using-nix.pkg: recipes/1-stage1.c +pkgs/5-go-beyond-using-nix.pkg: recipes/1-stage1/seed.host-executed.sh +pkgs/5-go-beyond-using-nix.pkg: recipes/1-stage1/syscall.h +pkgs/5-go-beyond-using-nix.pkg: recipes/1-stage1/protobusybox.c +pkgs/5-go-beyond-using-nix.pkg: recipes/1-stage1/protobusybox.h +pkgs/5-go-beyond-using-nix.pkg: recipes/1-stage1/hello.c +### +pkgs/5-go-beyond-using-nix.pkg: default.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/1-stage1.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a0-static-gnumake.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a1-static-binutils.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a2-static-gnugcc4-c.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a3-intermediate-musl.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a4-gnugcc4-cpp.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a5-gnugcc10.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a6-linux-headers.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a7-cmake.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a8-python.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2a9-intermediate-clang.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2b0-musl.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2b1-clang.nix +pkgs/5-go-beyond-using-nix.pkg: using-nix/2b2-busybox.nix +### +pkgs/5-go-beyond-using-nix.pkg: downloads/make-4.4.1.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/binutils-2.39.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/gcc-4.7.4.tar.bz2 +pkgs/5-go-beyond-using-nix.pkg: downloads/gmp-4.3.2.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/mpfr-2.4.2.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/mpc-0.8.1.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/musl-1.2.4.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/gcc-10.5.0.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/gmp-6.1.0.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/mpfr-3.1.4.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/mpc-1.0.3.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/isl-0.18.tar.bz2 +pkgs/5-go-beyond-using-nix.pkg: downloads/linux-6.4.12.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/cmake-3.27.4.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/Python-3.11.5.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/llvm-project-17.0.1.src.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/curl-8.2.1.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/mbedtls-3.4.1.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/boost_1_83_0.tar.bz2 +pkgs/5-go-beyond-using-nix.pkg: downloads/editline-1.17.1.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/brotli-1.0.9.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/gperf-3.1.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/libsodium-1.0.18.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/libarchive-3.7.1.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/lowdown-1.0.2.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/libseccomp-2.5.4.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/nlohmann-json-3.11.2.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/nix-2.17.0-zilched.tar.xz +pkgs/5-go-beyond-using-nix.pkg: flake.nix +pkgs/5-go-beyond-using-nix.pkg: downloads/queue.h +pkgs/5-go-beyond-using-nix.pkg: downloads/ZilchOS-core-2023.10.1.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/limine-5.20230830.0.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/patchelf-0.18.0.tar.bz2 +pkgs/5-go-beyond-using-nix.pkg: downloads/pkg-config-0.29.2.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/sqlite-autoconf-3430000.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/bison-3.8.2.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/m4-1.4.19.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/flex-2.6.4.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/mtools-4.0.43.tar.bz2 +pkgs/5-go-beyond-using-nix.pkg: downloads/xorriso-1.5.6.pl02.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/nasm-2.16.01.tar.xz +pkgs/5-go-beyond-using-nix.pkg: downloads/zstd-1.5.5.tar.gz +pkgs/5-go-beyond-using-nix.pkg: downloads/cacert-2023-08-22.pem + +iso: ZilchOS-core.iso + +ZilchOS-core.iso: pkgs/5-go-beyond-using-nix.pkg + tar --strip-components=2 -xf pkgs/5-go-beyond-using-nix.pkg \ + store/5-go-beyond-using-nix/ZilchOS-core.iso + sha256sum -c <<<"$(ISO_CHECKSUM) ZilchOS-core.iso" + +################################################################################ + +# Separate one for tests to help readability of the above + +pkgs/_1.test.pkg: pkgs/1-stage1.pkg + +pkgs/_2a3.test.pkg: pkgs/1-stage1.pkg +pkgs/_2a3.test.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/_2a3.test.pkg: pkgs/2a1-static-binutils.pkg +pkgs/_2a3.test.pkg: pkgs/2a2-static-gnugcc4-c.pkg +pkgs/_2a3.test.pkg: pkgs/2a3-intermediate-musl.pkg + +pkgs/_2a4.test.pkg: pkgs/1-stage1.pkg +pkgs/_2a4.test.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/_2a4.test.pkg: pkgs/2a1-static-binutils.pkg +pkgs/_2a4.test.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/_2a4.test.pkg: pkgs/2a4-gnugcc4-cpp.pkg + +pkgs/_2a5.test.pkg: pkgs/1-stage1.pkg +pkgs/_2a5.test.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/_2a5.test.pkg: pkgs/2a1-static-binutils.pkg +pkgs/_2a5.test.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/_2a5.test.pkg: pkgs/2a5-gnugcc10.pkg + +pkgs/_2a9.test.pkg: pkgs/1-stage1.pkg +pkgs/_2a9.test.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/_2a9.test.pkg: pkgs/2a3-intermediate-musl.pkg +pkgs/_2a9.test.pkg: pkgs/2a9-intermediate-clang.pkg + +pkgs/_2b1.test.pkg: pkgs/1-stage1.pkg +pkgs/_2b1.test.pkg: pkgs/2a0-static-gnumake.pkg +pkgs/_2b1.test.pkg: pkgs/2b0-musl.pkg +pkgs/_2b1.test.pkg: pkgs/2b1-clang.pkg + +pkgs/_3b.test.pkg: pkgs/2b0-musl.pkg +pkgs/_3b.test.pkg: pkgs/2b1-clang.pkg +pkgs/_3b.test.pkg: pkgs/2b2-busybox.pkg +pkgs/_3b.test.pkg: pkgs/3a-boost.pkg +pkgs/_3b.test.pkg: pkgs/3a-pkg-config.pkg +pkgs/_3b.test.pkg: pkgs/3a-sqlite.pkg +pkgs/_3b.test.pkg: pkgs/3a-curl.pkg +pkgs/_3b.test.pkg: pkgs/3a-editline.pkg +pkgs/_3b.test.pkg: pkgs/3a-brotli.pkg +pkgs/_3b.test.pkg: pkgs/3a-seccomp.pkg +pkgs/_3b.test.pkg: pkgs/3a-libarchive.pkg +pkgs/_3b.test.pkg: pkgs/3a-libsodium.pkg +pkgs/_3b.test.pkg: pkgs/3a-lowdown.pkg +pkgs/_3b.test.pkg: pkgs/3b-nix.pkg + +all-tests: pkgs/_1.test.pkg +all-tests: pkgs/_2a3.test.pkg +all-tests: pkgs/_2a4.test.pkg +all-tests: pkgs/_2a5.test.pkg +all-tests: pkgs/_2a9.test.pkg +all-tests: pkgs/_2b1.test.pkg +all-tests: pkgs/_3b.test.pkg + +################################################################################ + +.PHONY: all-pkgs +all-pkgs: pkgs/0-tcc-seed.pkg +all-pkgs: pkgs/1-stage1.pkg +all-pkgs: pkgs/2a0-static-gnumake.pkg +all-pkgs: pkgs/2a1-static-binutils.pkg +all-pkgs: pkgs/2a2-static-gnugcc4-c.pkg +all-pkgs: pkgs/2a3-intermediate-musl.pkg +all-pkgs: pkgs/2a4-gnugcc4-cpp.pkg +all-pkgs: pkgs/2a5-gnugcc10.pkg +all-pkgs: pkgs/2a6-linux-headers.pkg +all-pkgs: pkgs/2a7-cmake.pkg +all-pkgs: pkgs/2a8-python.pkg +all-pkgs: pkgs/2a9-intermediate-clang.pkg +all-pkgs: pkgs/2b0-musl.pkg +all-pkgs: pkgs/2b1-clang.pkg +all-pkgs: pkgs/2b2-busybox.pkg +all-pkgs: pkgs/2b3-gnumake.pkg +all-pkgs: pkgs/3a-sqlite.pkg +all-pkgs: pkgs/3a-boost.pkg +all-pkgs: pkgs/3a-mbedtls.pkg +all-pkgs: pkgs/3a-pkg-config.pkg +all-pkgs: pkgs/3a-curl.pkg +all-pkgs: pkgs/3a-editline.pkg +all-pkgs: pkgs/3a-brotli.pkg +all-pkgs: pkgs/3a-gnugperf.pkg +all-pkgs: pkgs/3a-seccomp.pkg +all-pkgs: pkgs/3a-libarchive.pkg +all-pkgs: pkgs/3a-libsodium.pkg +all-pkgs: pkgs/3a-lowdown.pkg +all-pkgs: pkgs/3a-nlohmann-json.pkg +all-pkgs: pkgs/3b-busybox-static.pkg +all-pkgs: pkgs/3b-tinycc-static.pkg +all-pkgs: pkgs/3b-nix.pkg +all-pkgs: pkgs/4-rebootstrap-using-nix.pkg +all-pkgs: pkgs/5-go-beyond-using-nix.pkg + +################################################################################ + +ifeq ($(USE_CCACHE), 1) +pkgs/2a1-static-binutils.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a2-static-gnugcc4-c.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a3-intermediate-musl.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a4-gnugcc4-cpp.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a5-gnugcc10.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a6-linux-headers.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a7-cmake.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a8-python.pkg: pkgs/_2a0-ccache.pkg +pkgs/2a9-intermediate-clang.pkg: pkgs/_2a0-ccache.pkg +pkgs/2b0-musl.pkg: pkgs/_2a0-ccache.pkg +pkgs/2b1-clang.pkg: pkgs/_2a0-ccache.pkg +pkgs/2b2-busybox.pkg: pkgs/_2a0-ccache.pkg +pkgs/2b3-gnumake.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-sqlite.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-boost.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-mbedtls.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-pkg-config.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-curl.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-editline.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-brotli.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-gnugperf.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-seccomp.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-libarchive.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-libsodium.pkg: pkgs/_2a0-ccache.pkg +pkgs/3a-lowdown.pkg: pkgs/_2a0-ccache.pkg +pkgs/3b-busybox-static.pkg: pkgs/_2a0-ccache.pkg +pkgs/3b-tinycc-static.pkg: pkgs/_2a0-ccache.pkg +pkgs/3b-nix.pkg: pkgs/_2a0-ccache.pkg +pkgs/4-rebootstrap-using-nix.pkg: pkgs/_2a0-ccache.pkg +pkgs/5-go-beyond-using-nix.pkg: pkgs/_2a0-ccache.pkg +endif + +################################################################################ + +.PHONY: verify-pkgs-checksums verify-all-pkgs-checksums update-pkgs-checksums +verify-pkgs-checksums: + @status=true; \ + while read expected_csum pkgname; do \ + pkg=$${pkgname%%.tar}.pkg; \ + if [[ ! -e "$$pkg" ]]; then \ + status=false; \ + echo "MISSING $$pkgname"; \ + continue; \ + fi; \ + computed_csum=$$(zstd -qcd "$$pkg" | sha256sum); \ + computed_csum=$$(<<<$$computed_csum tr ' ' '\t' | cut -f1); \ + short_csum=$$(<<<$$computed_csum head -c7); \ + if [[ "$$pkg" == pkgs/0-tcc-seed.pkg ]]; then \ + if ! sha256sum -c <<<"$(TCC_CHECKSUM) tcc-seed" \ + >/dev/null; then \ + echo "$$short_csum pkgs/0.tar CUSTOM"; \ + continue; \ + fi; \ + fi; \ + if make -sq "$$pkg"; then \ + dated=''; \ + else \ + status=false; \ + dated=" OUTDATED"; \ + fi; \ + if [[ "$$expected_csum" == "$$computed_csum" ]]; then \ + echo "$$short_csum $$pkgname OK$$dated"; \ + else \ + status=false; \ + echo "$$short_csum $$pkgname NOT OK$$dated"; \ + echo " computed: $$computed_csum"; \ + echo " expected: $$expected_csum"; \ + fi; \ + done < verify.pkgs.sha256; $$status\ + +verify-all-pkgs-checksums: all-pkgs + $(MAKE) verify-pkgs-checksums + +update-pkgs-checksums: + @:> verify.pkgs.sha256 + @find pkgs | grep '\.pkg$$' | grep -v '\/_' | sort | \ + while read p; do \ + name=$${p%%.pkg}.tar; \ + csum=$$(zstd -qcd "$$p" | sha256sum | tr ' ' '\t' | cut -f1); \ + short_csum=$$(<<<$$csum head -c7); \ + echo "$$csum $$name" >> verify.pkgs.sha256; \ + echo "$$short_csum $$name"; \ + done +verify.pkgs.sha256: all-pkgs update-pkgs-checksums + +nix-checksums-stage4: pkgs/4-rebootstrap-using-nix.pkg + tar tf pkgs/4-rebootstrap-using-nix.pkg store \ + | grep -E 'store/[a-z0-9]{32}-[^/]*/?$$' \ + | sed -E 's|.*/([a-z0-9]{32}-[^/]*)/?|\1|' \ + | sort \ + > nix-checksums-stage4 + +nix-checksums-stage5: pkgs/5-go-beyond-using-nix.pkg + tar Oxf pkgs/5-go-beyond-using-nix.pkg \ + store/5-go-beyond-using-nix/hashes \ + > nix-checksums-stage5 + +verify-all-nix-stage4-checksums: nix-checksums-stage4 verify.nix + @status=true; \ + while IFS=" " read ref_hash pkg; do \ + if grep -Fq "$$ref_hash-bootstrap" nix-checksums-stage4; then \ + echo " $$ref_hash $$pkg"; \ + else \ + status=false; \ + echo "! $$ref_hash $$pkg MISSING"; \ + fi; \ + done < verify.nix; $$status + +verify-all-nix-stage5-checksums: downloads/ZilchOS-core-2023.10.1.tar.gz +verify-all-nix-stage5-checksums: nix-checksums-stage5 verify.nix + @status=true; \ + tar -Oxf downloads/ZilchOS-core-2023.10.1.tar.gz \ + --wildcards */.maint/hashes | \ + while IFS=' ' read ref_hash pkg; do \ + stage5_hash=$$(grep " $$pkg$$" nix-checksums-stage5 \ + | sed -E 's|^([a-z0-9]{32}) .*|\1|'); \ + if [[ "$$ref_hash" == "$$stage5_hash" ]]; then \ + echo " $$ref_hash $$pkg"; \ + else \ + status=false; \ + echo "- $$ref_hash $$pkg"; \ + echo "+ $$stage5_hash $$pkg"; \ + fi; \ + done; $$status +NIX_BUILD_X = nix build --no-warn-dirty --option substitute false --no-link +verify-nix-plain-checksums: verify.nix + @status=true; \ + while IFS=" " read ref_hash pkg; do \ + plain_hash=$$($(NIX_BUILD_X) ".#$$pkg" --print-out-paths \ + | sed -E 's|.*/([a-z0-9]{32})-.*|\1|'); \ + if [[ "$$ref_hash" == "$$plain_hash" ]]; then \ + echo " $$ref_hash $$pkg"; \ + else \ + status=false; \ + echo "- $$ref_hash $$pkg"; \ + echo "+ $$plain_hash $$pkg"; \ + fi; \ + done < verify.nix; $$status + +verify-all-nix-plain-checksums: verify.nix + @$(NIX_BUILD_X) '.#toolchain' '.#libc' '.#musl' + @$(MAKE) verify-nix-plain-checksums + +################################################################################ + +clean-tmp: + @echo "### Makefile: removing tmp, keeping stage, pkgs and downloads..." + rm -rf tmp + +clean-stage: + @echo "### Makefile: removing stage, keeping tmp, pkgs and downloads..." + rm -rf stage + +clean: + @echo "### Makefile: removing stage, tmp, pkgs, iso, keeping downloads..." + rm -rf stage tmp pkgs \ + nix-checksums-stage4 nix-checksums-stage5 \ + nix-checksums-stage5-custom \ + ZilchOS-core.iso ZilchOS-core-raw.iso + +deepclean: + @echo "### Makefile: removing stage, tmp, pkgs, iso and downloads..." + rm -rf stage tmp pkgs downloads \ + nix-checksums-stage4 nix-checksums-stage5 \ + nix-checksums-stage5-custom \ + ZilchOS-core.iso ZilchOS-core-raw.iso diff --git a/06/README.md b/06/README.md new file mode 100644 index 0000000..769549a --- /dev/null +++ b/06/README.md @@ -0,0 +1,212 @@ +# `bootstrap-from-tcc` + +## What + +Bootstrap a modern toolchain for [ZilchOS Core](https://github.com/ZilchOS/core) +starting from a random trusted statically linked seed tcc +(+ trusted kernel on trusted hardware, of course). + +~320 KB binary + gigs of sources = a modern Clang + musl toolchain, +usable for building much more serious stuff. + +My goal is to beeline for bootstrapping Nix package manager, +then bootstrap a usable toolchain using Nix. +But even if you don't care about Nix, +this repo might be of some interest for minimal binary seed bootstrappers. + +Separate packages aren't just dumped into `/`, they're properly managed, +each one residing in its own prefix under `/store`. + +`x86_64`-only for now, possibly forever. + +## Why + +I wanted to build a minimal distro to understand NixOS better, +so I decided to have a decent trusted binary core bootstrap as well. + +Could be of use for bootstrapping other distributions. + +I'm aware of https://savannah.nongnu.org/projects/stage0 which does even better, +but I'm not as hardcore as them, so, let's start small. + +## How + +### In brief + +Compiler chain so far (`recipes`): +input TinyCC -> stable TinyCC -> GNU GCC 4 -> GNU GCC 10 -> -> Clang + +`recipes/1-stage1.c` is the most fun, since we don't have libc yet. + +Then I build Nix and start the entire bootstrapping chain all over again, +but now using that Nix I've built (`using-nix`). + +### Outlined bootstrap order + +* stage 0: seeded binary `tcc` +* stage 1 (`recipes/1-stage1.c` / `using-nix/1-stage1.nix`, no libc): + * `libtcc1` + * `protomusl` + * `tcc` + * `libtcc1` + * `protomusl` + * `tcc` that is gonna be the final one + * `libtcc1` + * `protomusl` + * `tcc` that we build just to prove the finality of the previous one + * `protobusybox` +* stage 2 "compiler ascension" part (`recipes/2a*.sh` / `using-nix/2a*.nix`): + * `gnumake` + * `binutils` + * `gnugcc4` + * `musl` + * `gnugcc4` + * `gnugcc10` + * `linux-headers` + * `cmake` + * `python` + * `clang` +* stage 2 "build with the new compiler" part (`recipes/2b*.sh`): + * `musl` + * `clang` + * `busybox` + * `gnumake` +* stage 3 "dependencies of useful stuff" (`recipes/3a*.sh`): ??? + * `sqlite` + * `boost` + * `mbedtls` + * `pkg-config` + * `curl` + * `editline` + * `brotli` + * `gnugperf` + * `seccomp` + * `libarchive` + * `libsodium` + * `lowdown` +* stage 3 "useful stuff" (`recipes/3b*.sh`): ??? + * `busybox-static` + * `tinycc-static` (1st time only) + * `nix` + +Then repeat stage 1 and most of stage 2 all over again, but under Nix. +The final exports of this flake are musl, clang toolchain and a busybox +that ZilchOS Core later bootstraps from. + +* stage 4 "rebootstrap with nix" (`recipes/4-rebootstrap-using-nix.sh`): + build the toolchain again from scratch, but using nix (`using-nix/`) + +* stage 5 "go beyond using nix" (`recipes/5-go-beyond-using-nix.sh`): + build some stale version of [ZilchOS Core](https://github.com/ZilchOS/core) + with the nix we've built, culminating with a bootable ZilchOS ISO. + +### In more detail + +given: + +* **statically linked target tcc (`tcc-seed`, you have to provide it)** +* host `unshare` for `chroot`ing and isolation +* host `wget`, `tar`, `gzip`, `bzip2`, `sha256sum`, `tar` + for optional convenient fetching of source files in stage 0 +* host `sed` to preprocess the sources needed for stage 1, unfortunately +* a replacement for `musl/arch/x86_64/syscall_arch.h` that works with tcc + (`syscall.h`) +* a bunch of sources to execute along the way + +`download.sh" downloads a ton of sources, scraping hashes/URLs from recipes + +`seed.sh` seeds: + +* unpacks sources into the stage area +* FIXME: uses host `sed`/`rm` for preprocessing stage 1 source code, + unfortunately +* copies `seed-tcc`, the only starting binary, into the stage area + +At the end of it we obtain +a ton of sources and a single externally seeded `tcc` binary. + +`recipes/1-stage1.c`, executed with `tcc -run`: + +* compiles a `libtcc1.a` from `tinycc` sources +* compiles a protomusl `libc.a` and others from `musl` sources +* compiles the first `tcc` that comes from our sources +* recompiles `libtcc1.a` from `tinycc` sources +* recompiles protomusl `libc.a` and others from `musl` sources +* recompiles our `tcc` with our `tcc` +* recompiles `libtcc1.a` from `tinycc` sources just in case +* recompiles protomusl `libc.a` and others from `musl` sources just in case +* compiles and links standalone applets out of busybox, notably `ash` +* copies over protomusl include files +* recompiles `tcc` once again + and verifies that we've previously reached a stability point + +At the end of stage 1 we have, all linked statically: + +* a `tcc` (+ `libtcc` + `libtcc1`) that recompiles to same binary +* a protomusl `libc.a` (+ crt stuff) +* select standalone busybox applets, most notably `ash` + +`stage2.sh`, executed with protobusybox `ash`: + +* Performs 'compiler ascension' from tcc to GNU GCC 4: + * `gnumake`, intermediate, built without make + * `gnumake`, statically linked + * `binutils`, statically linked + * `gnugcc4`, statically linked + * `musl`, now a shared library as well + * `gnugcc4` with C++ support and linking to a shared musl + * `gnugcc10` + * `linux-headers` (clang & cmake dependency) + * `cmake` (clang dependency) + * `python` (clang dependency, presumably) + * `clang`, intermediate, 2-stage + +* Recompile the world with clang, free of GNU runtime libs: + * `musl`, final + * `clang`, final + * `busybox`, final + * `gnumake`, final + +* Build a bunch of Nix dependencies +* Build Nix + +* Start over, build toolchain, build ZilchOS Core + +### Building options + +There are three major ways to build it. + +If you have Nix and want to skip the first half that bootstraps Nix, +you can just `nix build`, but what's the fun in taking shortcuts =). +You'll need `experimental-features = nix-command flakes ca-derivations`. + +If you want to do a full bootstrap with all imaginable speedups enabled, +try something to the tune of +`make all-pkgs all-tests verify-all-pkgs-checksums -j2 NPROC=$(nproc) USE_CCACHE=1 USE_NIX_CACHE=1`. +Dependencies: host GNU Make, host zstd, basic host stuff like sed and bash, +a target TinyCC you supply or Nix to build you one. +This is the recommended way, especially shining when you +iteratively debug reproducibility-unrelated build problems. +Consider mounting `tmp/build` as tmpfs with 8G size. + +Finally, the least-dependency way is `NPROC=$(nproc) ./build.sh`. +This one doesn't even need GNU Make or zstd, +but there are zero intermediate checkpoints, you always start all over. +Very impractical, this is for increased portability only. + +### Reproducibility + +Reproducibility is deeply cared about, +but it's a constant struggle and one cannot foresee everything. + +Hashes are checked for intermediate steps for both `make` and `nix` builds. +`raw` builds only verify the resulting ZilchOS Core ISO built during stage5. +I try to build on different machines and note down the results in `git notes`. +Commits require a specific (but adjustable) amount of successful +`make`, `raw` and `nix` before getting into the main branch. +Refer to `make all-with-make`, `make all-raw` and `make all-with-nix` +to see what exactly is being tested. + +For hard mode, you can try `USE_DISORDERFS`. + +For ZilchOS/core, see `.maint/hashes` and `.maint/tools/hashes`. diff --git a/06/build-using-nix.sh b/06/build-using-nix.sh new file mode 100755 index 0000000..21d25e1 --- /dev/null +++ b/06/build-using-nix.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +# this is entirely optional and for development purposes only +# just use `nix-build` and you should be fine + +# build with ccache and without /bin/sh present +# your nix needs experimental-options = ca-derivations +# and you need root access / to be a trusted user + +set -ue + +CCACHE_HOST=/var/cache/ccache +mkdir -p $CCACHE_HOST/data +sudo chgrp nixbld $CCACHE_HOST $CCACHE_HOST/data +sudo chmod g+ws $CCACHE_HOST $CCACHE_HOST/data + +if [[ ! -e $CCACHE_HOST/bin/ccache ]]; then + nix build 'nixpkgs#pkgsStatic.ccache' --out-link $CCACHE_HOST/result + mkdir -p $CCACHE_HOST/bin + cp --reflink=auto $CCACHE_HOST/result/bin/ccache $CCACHE_HOST/bin/ccache + rm $CCACHE_HOST/result +fi + +[[ -e $CCACHE_HOST/setup ]] || cat > $CCACHE_HOST/setup <<\EOF +mkdir -p .ccache-wrappers +for prefix in '' x86_64-linux- x86_64-linux-musl- x86_64-linux-unknown-; do + for name in cc c++ gcc g++ clang clang++ tcc; do + if command -v $prefix$name; then + ln -s /ccache/bin/ccache .ccache-wrappers/$prefix$name + fi + done +done +export PATH="$(pwd)/.ccache-wrappers:/ccache/bin:$PATH" +export CCACHE_DIR="/ccache/data/$1" +export CCACHE_COMPILERCHECK=content +export CCACHE_SLOPPINESS=include_file_ctime,include_file_mtime +export CCACHE_MAXSIZE=0 +export CCACHE_UMASK=005 +export CCACHE_NOHASHDIR=1 +export CCACHE_BASEDIR="$(pwd)" +EOF +chmod +x $CCACHE_HOST/setup + +nix-build -A stage1.protomusl +nix-build -A stage1.protobusybox +nix-build -A stage1.tinycc +sudo env "NIX_CONFIG=sandbox-paths = /ccache=$CCACHE_HOST" nix-build "$@" diff --git a/06/build.sh b/06/build.sh new file mode 100755 index 0000000..5f896b7 --- /dev/null +++ b/06/build.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# This is the straigthforward bootstrapping way. +# Prepare a directory with the initial TinyCC compiler, and a ton of sources. +# Then exec into it and let it bootstrap itself. + +# You can refer to the Makefile for a more refined, totally optional approach +# with incremental builds, better build isolation etc. + +set -uex + +export NPROC=${NPROC:-${1:-1}} + +cp ../05/tcc-final/tcc tcc-seed + +if [[ ! -e tcc-seed ]]; then + echo 'You need to supply a statically linked TinyCC as `tcc-seed`.' + echo -n 'You can `./compile-tcc-seed-with-nix.sh` ' + echo 'if you have `nix` and trust in me.' + exit 1 +fi + +# Create a stage directory +mkdir -p stage + +# Download all the required source files +./download.sh + +# Inject initial tcc and our scripts; pre-unpack and patch stage 1 sources, +# in a separate file because it makes sense to run it separately sometimes. +./seed.sh + +# Exec into stage1.c inside stage with env unset, +# without network, with EUID=EGID=0 and with /dev/null being a /dev/null. +# See helpers/chroot and helpers/chroot-inner for more explanations +# Alternatively, you can chroot if you're not a fan of user namespaces. +MOUNT=$(command -v mount) +if [[ -e /run/wrappers/bin/mount.real ]]; then # NixOS wrapper might be buggy + MOUNT=$(cat /run/wrappers/bin/mount.real) +fi +MKDIR=$(command -v mkdir) +CHROOT=$(command -v chroot) + +exec env -i "NPROC=$NPROC" unshare -nrm bash -uexs < stage/dev/null + $MOUNT --bind /dev/null stage/dev/null + + exec $CHROOT stage \ + /store/0-tcc-seed -I /protosrc/tinycc/include -nostdinc -nostdlib -Werror -run \ + -DCHAINLOAD='"/recipes/all-past-stage1.sh"' \ + /recipes/1-stage1.c +EOF + +# There's no next step, +# upon completion stage1.c will exec into recipes/all-past-stage1.sh diff --git a/06/compile-tcc-seed-with-nix.sh b/06/compile-tcc-seed-with-nix.sh new file mode 100755 index 0000000..7054e28 --- /dev/null +++ b/06/compile-tcc-seed-with-nix.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -uexo pipefail + +NIXPKGS_HASH=21f524672f25f8c3e7a0b5775e6505fee8fe43ce +TCC_CHECKSUM=05aad934985939e9997127e93d63d6a94c88739313c496f10a90176688cc9167 +TCC=$(nix build "nixpkgs/$NIXPKGS_HASH#pkgsStatic.tinycc.out" \ + --no-link --print-out-paths) +cat $TCC/bin/tcc > tcc-seed +chmod +x tcc-seed +S="$TCC_CHECKSUM tcc-seed" +sha256sum tcc-seed +sha256sum -c <<<$S diff --git a/06/default.nix b/06/default.nix new file mode 100644 index 0000000..f4c36d9 --- /dev/null +++ b/06/default.nix @@ -0,0 +1,144 @@ +let + # stage 0 + + # these two use nixpkgs, but are fixed-output derivations with no dependencies + tcc-seed = (import ./using-nix/0.nix).tinycc; + protosrc = (import ./using-nix/0.nix).protosrc; + # in bootstrapping builds, + # 0.nix is different and they're not coming from nixpkgs, + # see recipes/4-rebootstrap-using-nix.sh + + # stage 1 + + stage1 = (import ./using-nix/1-stage1.nix) { + inherit tcc-seed protosrc; + recipesStage1ExtrasPath = ./recipes/1-stage1; + stage1cPath = ./recipes/1-stage1.c; + }; # multioutput, offers .protobusybox, .protomusl and .tinycc + + # stage 2 + + mkCaDerivation = args: derivation (args // { + system = "x86_64-linux"; + __contentAddressed = true; + outputHashAlgo = "sha256"; outputHashMode = "recursive"; + }); + + mkDerivationStage2 = + {name, script, buildInputPaths, extra ? {}}: mkCaDerivation { + inherit name; + builder = "${stage1.protobusybox}/bin/ash"; + args = [ "-uexc" ( + '' + export PATH=${builtins.concatStringsSep ":" buildInputPaths} + + if [ -e /ccache/setup ]; then + . /ccache/setup bootstrap-from-tcc/${name} + fi + + unpack() (tar --strip-components=1 -xf "$@") + + if [ -n "$NIX_BUILD_CORES" ] && [ "$NIX_BUILD_CORES" != 0 ]; then + NPROC=$NIX_BUILD_CORES + elif [ "$NIX_BUILD_CORES" == 0 ] && [ -r /proc/cpuinfo ]; then + NPROC=$(grep -c processor /proc/cpuinfo) + else + NPROC=1 + fi + '' + script + ) ]; + } // extra; + + fetchurl = { url, sha256 }: derivation { + name = builtins.baseNameOf url; + inherit url; + urls = [ url ]; + unpack = false; + + builder = "builtin:fetchurl"; + system = "builtin"; + outputHashMode = "flat"; outputHashAlgo = "sha256"; + preferLocalBuild = true; + outputHash = sha256; + }; + + static-gnumake = (import using-nix/2a0-static-gnumake.nix) { + inherit fetchurl mkDerivationStage2 stage1; + }; + + static-binutils = (import using-nix/2a1-static-binutils.nix) { + inherit fetchurl mkDerivationStage2 stage1 static-gnumake; + }; + + static-gnugcc4-c = (import using-nix/2a2-static-gnugcc4-c.nix) { + inherit fetchurl mkDerivationStage2 stage1 static-gnumake static-binutils; + }; + + intermediate-musl = (import using-nix/2a3-intermediate-musl.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils static-gnugcc4-c; + }; + + gnugcc4-cpp = (import using-nix/2a4-gnugcc4-cpp.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils static-gnugcc4-c; + inherit intermediate-musl; + }; + + gnugcc10 = (import using-nix/2a5-gnugcc10.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils gnugcc4-cpp intermediate-musl; + }; + + linux-headers = (import using-nix/2a6-linux-headers.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils gnugcc10; + }; + + cmake = (import using-nix/2a7-cmake.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils gnugcc10 linux-headers; + }; + + python = (import using-nix/2a8-python.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils gnugcc10; + }; + + intermediate-clang = (import using-nix/2a9-intermediate-clang.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake static-binutils intermediate-musl gnugcc10; + inherit linux-headers cmake python; + }; + + musl = (import using-nix/2b0-musl.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake intermediate-clang; + }; + + clang = (import using-nix/2b1-clang.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake musl intermediate-clang; + inherit linux-headers cmake python; + }; + + busybox = (import using-nix/2b2-busybox.nix) { + inherit fetchurl mkDerivationStage2; + inherit stage1 static-gnumake musl clang linux-headers; + }; + +in + { + # exposed just because; don't rely on these + inherit protosrc tcc-seed; + inherit stage1; + inherit static-gnumake static-binutils static-gnugcc4-c; + inherit intermediate-musl gnugcc4-cpp gnugcc10; + inherit linux-headers cmake python intermediate-clang; + inherit musl clang; + + # public interface: + libc = musl; # some libc that TODO: doesn't depend on anything else + toolchain = clang; # some modern C/C++ compiler targeting this libc + busybox = busybox; # a freebie busybox TODO: depending on just libc + } diff --git a/06/download.sh b/06/download.sh new file mode 100755 index 0000000..5e352fb --- /dev/null +++ b/06/download.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash + +# Receives files as arguments, scans them for lines like +# #> FETCH dd16fb1d67bfab79a72f5e8390735c49e3e8e70b4945a15ab1f81ddb78658fb3 +# #> FROM http://ftp.gnu.org/gnu/make/make-4.4.1.tar.gz +# #> AS make.tar.gz +# or +# #local = "/downloads/make.tar.gz"; +# url = "http://ftp.gnu.org/gnu/make/make-4.4.1.tar.gz"; +# sha256 = "dd16fb1d67bfab79a72f5e8390735c49e3e8e70b4945a15ab1f81ddb78658fb3"; +# downloads to ./downloads if file's not present there yet, verifies hash, +# copies file over to $DESTDIR. + +set -ue + +: ${DESTDIR:=stage/downloads} # final location of putting the file +: ${ONLY:=all} # allow to limit to just a single file +mkdir -p downloads # first cached there, so that stage can be cleaned freely + +fetch() { + hash=$1; url=$2; filename=${3:-$(basename "$url")} + if [[ -e "downloads/$filename" ]]; then + pushd downloads >/dev/null + echo "$hash $filename" | sha256sum -c + popd >/dev/null + else + mkdir -p downloads/.tmp$$ + pushd downloads/.tmp$$ >/dev/null + wget -nv --show-progress "$url" -O "$filename" + echo "$hash $filename" | sha256sum -c --quiet + popd >/dev/null + mv "downloads/.tmp$$/$filename" downloads/ + rm -d downloads/.tmp$$ + fi + if [[ "${DESTDIR:-}" != downloads ]]; then + mkdir -p "$DESTDIR" + cp -a --reflink=auto "downloads/$filename" \ + "$DESTDIR/$filename" + fi +} + +REGEX_MAGIC='^#> ' +REGEX_FETCH='^#> FETCH' +REGEX_FROM='^#> FROM' +REGEX_AS='^#> AS' +NIX_REGEX_FETCH='^[[:blank:]]*sha256[[:blank:]]*=[[:blank:]]*"(.*)";$' +NIX_REGEX_FROM='^[[:blank:]]*url[[:blank:]]*=[[:blank:]]*"(.*)";$' +NIX_REGEX_AS='^[[:blank:]]*#[[:blank:]]local[[:blank:]]*=[[:blank:]]*/downloads/(.*);$' +process_commands_in() { + hash=''; url=''; filename='' + while read -r line; do + if [[ "$line" =~ $REGEX_MAGIC ]]; then + if [[ "$line" =~ $REGEX_FETCH ]]; then + hash="${line##"#> FETCH"}" + elif [[ "$line" =~ $REGEX_FROM ]]; then + url="${line##'#> FROM '}" + elif [[ "$line" =~ $REGEX_AS ]]; then + filename="${line##'#> AS '}" + else + echo "### $0: malformed line '$line' in '$1'" + exit 2 + fi + elif [[ "$line" =~ $NIX_REGEX_FETCH ]]; then + hash=${BASH_REMATCH[1]} + elif [[ "$line" =~ $NIX_REGEX_FROM ]]; then + url=${BASH_REMATCH[1]} + elif [[ "$line" =~ $NIX_REGEX_AS ]]; then + filename=${BASH_REMATCH[1]} + else + if [[ -n "$hash" && -n "$url" ]]; then + filename=${filename:-$(basename $url)} + if [[ "$ONLY" == all || \ + "$ONLY" == "$filename" ]]; then + fetch "$hash" "$url" "$filename" + fi + fi + hash=''; url=''; filename='' + fi + done < $1 +} + +[[ $# == 0 ]] && files='recipes/*.sh recipes/*/*.sh' || files="$@" +for f in $files; do + process_commands_in $f +done diff --git a/06/flake.nix b/06/flake.nix new file mode 100644 index 0000000..360ce1b --- /dev/null +++ b/06/flake.nix @@ -0,0 +1,15 @@ +{ + description = "bootstrap-from-tcc"; + + outputs = { self }: + let + allPkgs = (import ./default.nix); + in + { + packages.x86_64-linux = allPkgs; + + # TODO: expose only the most usable outputs + # TODO: solve fetching: https://discourse.nixos.org/t/17105 + hydraJobs = builtins.mapAttrs (_: drv: { x86_64-linux = drv; }) allPkgs; + }; +} diff --git a/06/helpers/builddir b/06/helpers/builddir new file mode 100755 index 0000000..c8f68b7 --- /dev/null +++ b/06/helpers/builddir @@ -0,0 +1,70 @@ +#!/bin/sh + +: ${DISORDER=0} + +set -uex + +verb=$1 +front=$2 +back=$front-back + +[ "tmp/build/${front#tmp/build/}" = "$front" ] +[ "tmp/build/${back#tmp/build/}" = "$back" ] + +remove_hard() { + if [ -e "$1" ]; then + sudo umount "$1" 2>/dev/null || true + if ! rm -rf "$1" 2>/dev/null; then + chmod -R +w "$1" + rm -rf "$1" + fi + fi +} + +if [ "$verb" = create ]; then + echo "### helpers/builddir: creating $front..." + remove_hard "$front"; remove_hard "$back" + mkdir -p "$front" + if [ "$DISORDER" = 1 ]; then + sudo umount "$back" || true + rm -rf "$back" + mkdir -p "$front" "$back" + sudo mount -o size=16G -t tmpfs tmpfs "$back" + sudo mount --bind "$back" "$front" + [ $(findmnt -no FSTYPE "$front") = tmpfs ] + fi +elif [ "$verb" = pre-build ]; then + [ -e "$front" ] + if [ "$DISORDER" = 1 ]; then + echo "### helpers/builddir: disordering $front..." + [ -e "$back" ] + [ $(findmnt -no FSTYPE "$back") = tmpfs ] + sudo umount "$front" + sudo disorderfs --shuffle-dirents=yes \ + -o allow_other --multi-user=yes \ + "$back" "$front" + [ $(findmnt -no FSTYPE "$front") = fuse.disorderfs ] + fi +elif [ "$verb" = post-build ]; then + if [ "$DISORDER" = 1 ]; then + echo "### helpers/builddir: ordering $front..." + [ ! -e "$front" ] && [ ! -e "$back" ] + [ $(findmnt -no FSTYPE "$front") = fuse.disorderfs ] + [ $(findmnt -no FSTYPE "$back") = tmpfs ] + sudo umount "$front" + sudo mount --bind "$back" "$front" + fi +elif [ "$verb" = remove ]; then + echo "### helpers/builddir: removing $front..." + if [ "$DISORDER" = 1 ]; then + [ $(findmnt -no FSTYPE "$back") = tmpfs ] + sudo umount "$front" + sudo umount "$back" + rm -d "$front" "$back" + else + remove_hard "$front"; remove_hard "$back" + fi + [ ! -e "$front" ] && [ ! -e "$back" ] +else + exit 9 +fi diff --git a/06/helpers/cheat b/06/helpers/cheat new file mode 100755 index 0000000..a8edb7f --- /dev/null +++ b/06/helpers/cheat @@ -0,0 +1,49 @@ +#!/bin/sh +set -ue + +NIXPKGS=nixpkgs/21f524672f25f8c3e7a0b5775e6505fee8fe43ce +: ${DESTDIR:=stage} + +mkdir -p $DESTDIR/cheat + +if [ ! -e $DESTDIR/cheat/make ]; then + nix build "$NIXPKGS#pkgsStatic.gnumake" + cp result/bin/make $DESTDIR/cheat/make + rm result +fi + +if [ ! -e $DESTDIR/cheat/bash ]; then + nix build "$NIXPKGS#pkgsStatic.bash" + cp result/bin/bash $DESTDIR/cheat/bash + rm result +fi + +if [ ! -e $DESTDIR/cheat/strace ]; then + nix build "$NIXPKGS#pkgsStatic.strace" + cp result/bin/strace $DESTDIR/cheat/ + rm result +fi + +if [ ! -e $DESTDIR/cheat/busybox ]; then + nix build "$NIXPKGS#pkgsStatic.busybox" + cp result/bin/busybox $DESTDIR/cheat/busybox + for f in $(ls result/bin/); do + [ $(basename $f) = busybox ] || + ln -s /cheat/busybox $DESTDIR/cheat/$(basename $f) + done + rm result +fi + +if [ -z "$@" ]; then + _PATH='' + for bindir in $DESTDIR/store/*/bin $DESTDIR/store/*/*/bin; do + _PATH="${bindir##$DESTDIR}:$_PATH" + done + LIBC=$(find $DESTDIR/store/*/lib/libc.so | tail -n1) + if [ -n "$LIBC" ] && [ ! -h $DESTDIR/cheat/ldd ]; then + ln -s "${LIBC##$DESTDIR}" $DESTDIR/cheat/ldd + fi + helpers/chroot /cheat/env "PATH=$_PATH" /cheat/ash +else + helpers/chroot "$@" +fi diff --git a/06/helpers/chroot b/06/helpers/chroot new file mode 100755 index 0000000..b8cdd65 --- /dev/null +++ b/06/helpers/chroot @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +# Run a command chrooted inside $DESTDIR w/o network, with /dev/null, outline: +# +# [helpers/chroot, outer script] +# / unshare +# | -n # without network +# | -r # with EUID=EGID=0 +# \ -m # separate mount namespace +# [helpers/chroot-inner, this script] +# mount --bind /dev/null $DESTDIR/dev/null # unprivileged /dev/null! +# && +# env -i # with env unset +# chroot $DESTDIR # unprivileged chroot! + +set -uex + +exec unshare -nrm helpers/chroot-inner "$@" diff --git a/06/helpers/chroot-inner b/06/helpers/chroot-inner new file mode 100755 index 0000000..e13ae2b --- /dev/null +++ b/06/helpers/chroot-inner @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +# Run a command chrooted inside $DESTDIR w/o network, with /dev/null, outline: +# +# [helpers/chroot, outer script] +# unshare +# -n # without network +# -r # with EUID=EGID=0 +# -m # separate mount namespace +# [helpers/chroot-inner, this script] +# / mount --bind /dev/null $DESTDIR/dev/null # unprivileged /dev/null! +# | && +# | env -i # with env unset +# \ chroot $DESTDIR # unprivileged chroot! + +set -uex + +: ${DESTDIR:=stage} +: ${NPROC:=1} +: ${SOURCE_DATE_EPOCH:=0} + +CHROOT=$(command -v chroot) +if [[ ! -x "$CHROOT" ]]; then + if [[ -x /sbin/chroot ]]; then + CHROOT=/sbin/chroot + elif [[ -x /usr/sbin/chroot ]]; then + CHROOT=/usr/sbin/chroot + fi +fi + +if [[ -e /run/wrappers/bin/mount.real ]]; then + MOUNT=$(cat /run/wrappers/bin/mount.real) +else + MOUNT=mount +fi +mkdir -p "$DESTDIR/dev"; :> "$DESTDIR/dev/null" +$MOUNT --bind /dev/null "$DESTDIR/dev/null" + +exec env -i "NPROC=$NPROC" "SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH" \ + $CHROOT "$DESTDIR" "$@" diff --git a/06/helpers/inject b/06/helpers/inject new file mode 100755 index 0000000..1a3beb6 --- /dev/null +++ b/06/helpers/inject @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -uex + +DESTDIR=$1 +shift + +for arg; do + if [[ "$arg" =~ .*\.pkg ]]; then + # That one's for package dependencies + tar -xf "$arg" -Izstd -C "$DESTDIR" + else + # That one's for copying over downloads and sources + if [[ ! -e "./$DESTDIR/$arg" || "$arg" -nt "./$DESTDIR/$arg" ]] + then + echo "Copying $arg into $DESTDIR..." + mkdir -p "$DESTDIR/$(dirname "$arg")" + cp -a --reflink=auto "$arg" "$DESTDIR/$arg" + fi + fi +done diff --git a/06/helpers/maint/build-custom-stage5 b/06/helpers/maint/build-custom-stage5 new file mode 100755 index 0000000..470b955 --- /dev/null +++ b/06/helpers/maint/build-custom-stage5 @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +set -uexo pipefail + +CUSTOM_CORE_DIR=$1 + +# Determine where to start from +if [[ -e pkgs/custom-stage5.pkg ]]; then + BASE=pkgs/custom-stage5.pkg +elif [[ -e pkgs/5-go-beyond-using-nix.pkg ]]; then + BASE=pkgs/5-go-beyond-using-nix.pkg +elif [[ -e pkgs/4-rebootstrap-using-nix.pkg ]]; then + BASE=pkgs/4-rebootstrap-using-nix.pkg +else + [[ "${USE_CCACHE-}" = 1 ]] && FLAGS='USE_CCACHE=1' || FLAGS='' + [[ -n "${NPROC-}" ]] && FLAGS="$FLAGS NPROC=$NPROC" || FLAGS='' + make $FLAGS pkgs/5-go-beyond-using-nix.pkg + BASE=pkgs/5-rebootstrap-using-nix.pkg +fi + +# Inject stage5 pkg dependencies +DESTDIR=tmp/build/custom-stage5 +helpers/builddir create "$DESTDIR" +grep -E '^pkgs/5-go-beyond-using-nix.pkg:' Makefile \ + | grep -v '^pkgs/5-go-beyond-using-nix.pkg: downloads/' \ + | grep -v 'pkgs/5-go-beyond-using-nix.pkg: pkgs/_2a0-ccache.pkg$' \ + | sed 's|^pkgs/5-go-beyond-using-nix.pkg: ||' \ + | while IFS= read -r dep; do helpers/inject "$DESTDIR" "$dep"; done +helpers/inject "$DESTDIR" recipes/5-go-beyond-using-nix.sh + +# Inject BASE and its nixdb +helpers/inject "$DESTDIR" "$BASE" +mkdir -p "$DESTDIR/prev/" +tar --strip-components=2 -xf $BASE -C "$DESTDIR/prev/" +rm -f "$DESTDIR/store/5-go-beyond-using-nix/ZilchOS-core.iso" + +# Inject ccache data, all stage5 ccache data we can find +if [[ "${USE_CCACHE-}" = 1 ]]; then + helpers/inject "$DESTDIR" pkgs/_2a0-ccache.pkg + unshare -nr chroot "$DESTDIR" /store/_2a0-ccache/bin/ccache -z + if [[ -e tmp/ccache/5-go-beyond-using-nix.tar.zstd ]]; then + tar -Izstd -xf tmp/ccache/5-go-beyond-using-nix.tar.zstd \ + -C "$DESTDIR/ccache" + fi + if [[ -e tmp/ccache/custom-stage5.tar.zstd ]]; then + tar -Izstd -xf tmp/ccache/custom-stage5.tar.zstd \ + -C "$DESTDIR/ccache" + fi + ln -sf /store/_2a0-ccache/wrap-available "$DESTDIR/ccache/setup" + ln -sf /store/_2a0-ccache/bin "$DESTDIR/ccache/bin" +fi +helpers/builddir pre-build "$DESTDIR" + +# Inject custom ZilchOS Core code +DESTCORE="$DESTDIR/tmp/5-go-beyond-using-nix/ZilchOS-core" +mkdir -p "$DESTCORE" +rm -rf "$DESTCORE" # TODO: REMOVE +cp -r "$CUSTOM_CORE_DIR" "$DESTCORE" +rm -rf "$DESTCORE/.git" +[[ -e $DESTCORE/flake.nix ]] +hashes=$(cat $CUSTOM_CORE_DIR/.maint/hashes) + +# Inject custom required downloads +DESTDIR="$DESTDIR/downloads" ./download.sh $(find $DESTCORE -name '*.nix') + +DESTDIR="$DESTDIR" NPROC="${NPROC-}" \ + ./helpers/chroot /recipes/5-go-beyond-using-nix.sh + +cat tmp/build/custom-stage5/store/5-go-beyond-using-nix/hashes \ + > nix-checksums-stage5-custom + +# Collect results back +tar -Izstd -cf "pkgs/custom-stage5.pkg" \ + -C "$DESTDIR" "store/5-go-beyond-using-nix" + +# Collect ccache back +if [[ "${USE_CCACHE-}" = 1 ]]; then + unshare -nr chroot "$DESTDIR" /store/_2a0-ccache/bin/ccache -sz + tar -Izstd -cf "tmp/ccache/custom-stage5.tar.zstd" \ + -C "$DESTDIR/ccache" . +fi + +helpers/builddir post-build "$DESTDIR" +helpers/builddir remove "$DESTDIR" + +set +x +status=true +while IFS=' ' read ref_hash pkg; do + stage5_hash=$(grep " $pkg$" nix-checksums-stage5-custom \ + | sed -E 's|^([a-z0-9]{32}) .*|\1|'); + if [[ "$ref_hash" == "$stage5_hash" ]]; then + echo " $ref_hash $pkg"; + else + status=false; + echo "- $ref_hash $pkg"; + echo "+ $stage5_hash $pkg"; + fi +done <<<"$hashes"; $status diff --git a/06/helpers/maint/is-tested-enough b/06/helpers/maint/is-tested-enough new file mode 100755 index 0000000..faf61f9 --- /dev/null +++ b/06/helpers/maint/is-tested-enough @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +set -ueo pipefail + +COMMIT=${1-HEAD} + +commit=$(git show -s --no-notes "$COMMIT") +echo "$commit" +if grep -q '^\s*Builds-required: none$' <<<"$commit"; then + echo '---' + echo '`Builds-required: none` in commit message, skipping commit' + exit 0 +fi +echo '---' + +if grep -q '^\s*Builds-required: ' <<<"$commit"; then + verification_line=$(grep '^\s*Builds-required:' <<<"$commit") + verspec=$(sed 's|^\s*Builds-required:||' <<<"$verification_line") + if ! grep -Eq '^ make=[0-9]+ raw=[0-9]+ nix=[0-9]+$' <<<"$verspec"; then + echo 'Malformed `Builds-required:` line' >&2 + exit 7 + fi + required_make=$(sed -E 's|.* make=([0-9]+).*|\1|' <<<"$verspec") + (( required_make >= 0 )) + required_raw=$(sed -E 's|.* raw=([0-9]+).*|\1|' <<<"$verspec") + (( required_raw >= 0 )) + required_nix=$(sed -E 's|.* nix=([0-9]+).*|\1|' <<<"$verspec") + (( required_nix >= 0 )) +else + required_make=2 + required_raw=1 + required_nix=2 +fi + +git fetch origin refs/notes/commits:refs/notes/commits +notes=$(git notes show "$COMMIT" \ + | grep ^Built-on: \ + | grep -v USE_CCACHE \ + | sort -u) +using_make=$(grep -Fw how=make <<<"$notes" | wc -l) || true +using_raw=$(grep -Fw how=raw <<<"$notes" | wc -l) || true +using_nix=$(grep -Fw how=nix <<<"$notes" | wc -l) || true +echo "$notes" + +status=true +text="" +if (( using_make < required_make )); then + text+="Not enough how=make commits: $using_make < $required_make\n" + status=false +fi +if (( using_raw < required_raw )); then + text+="Not enough how=raw commits: $using_raw < $required_raw\n" + status=false +fi +if (( using_nix < required_nix )); then + text+="Not enough how=nix commits: $using_nix < $required_nix\n" + status=false +fi + +echo '---' +echo "${using_make} out of ${required_make} required how=make builds" +echo "${using_raw} out of ${required_raw} required how=raw builds" +echo "${using_nix} out of ${required_nix} required how=nix builds" +if ! $status; then + echo -ne "---\n$text" +fi +$status diff --git a/06/helpers/maint/notes b/06/helpers/maint/notes new file mode 100755 index 0000000..41a9956 --- /dev/null +++ b/06/helpers/maint/notes @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -ueo pipefail +if [[ $# == 0 || "$1" == 'show' ]]; then + exec git tree --show-notes --decorate=short --color=always \ + | grep -v '^|\s*$' \ + | grep -v 'Notes:$' \ + | sed 's/ Built-on: /^ Built-on: /' \ + | less +elif [[ "$1" == 'mark-built' ]] && (( $# >= 3 )) ; then + commit=$2; hostname=$3; how=$4; shift 4; extra="$@" + git fetch origin refs/notes/commits:refs/notes/commits + if ! grep -Eqx 'how=(raw|make|nix)' <<<$how; then + echo 'how must be one of: raw, make, nix' >/dev/stderr + exit 1 + fi + msg="Built-on: $hostname $how $extra" + if grep -Fqx "$msg" <(git notes show "$commit" 2>/dev/null); then + echo "$commit is already marked with '$msg'" >/dev/stderr + exit + fi + git notes append -m "$msg" "$commit" + git push origin refs/notes/commits +else + echo 'Usage: ' >/dev/stderr + echo ' helpers/notes [show]' >/dev/stderr + echo -n ' helpers/notes mark-built ' >/dev/stderr + echo ' how=[raw|make|nix] ' >/dev/stderr + exit 1 +fi diff --git a/06/helpers/maint/release b/06/helpers/maint/release new file mode 100755 index 0000000..f5e2a39 --- /dev/null +++ b/06/helpers/maint/release @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +set -uex + +# check that two extra files have not been modified + +seeder_path=$(nix eval --impure --expr '"${recipes/1-stage1/seed.host-executed.sh}"') +grep "stage1_seeder_reference = $seeder_path;$" using-nix/0.nix +syscall_h_path=$(nix eval --impure --expr '"${recipes/1-stage1/syscall.h}"') +grep "syscall_h_reference = $syscall_h_path;$" using-nix/0.nix + + +# check that both downloads in using-nix/0-prebuilt refer to the same tag + +lines=$(grep 'seeding-files-r[0-9]*' using-nix/0-prebuilt.nix \ + | sed 's/.*r\([0-9][0-9][0-9]\).*/\1/') +sort <<<"$lines" +[[ $(wc -l <<<"$lines") == 2 ]] +[[ $(sort <<<"$lines" | uniq | wc -l) == 1 ]] + + +# build nars, calculate hashes, check both 0-from-nixpkgs and 0-prebuilt + +[[ ! -L result* ]] + + +tinycc_=$(nix-build using-nix/0-from-nixpkgs.nix -A tinycc --no-out-link) +tinycc=$(nix-build using-nix/0-from-nixpkgs.nix -A tinycc --no-out-link --check) +[[ "$tinycc_" == "$tinycc" ]] +nix store dump-path $tinycc > tinycc-liberated.nar +tinycc_hash=$(nix hash file tinycc-liberated.nar) +grep "outputHash = \"$tinycc_hash\";$" using-nix/0-from-nixpkgs.nix +grep "sha256 = \"$tinycc_hash\";$" using-nix/0-prebuilt.nix + +protosrc_=$(nix-build using-nix/0-from-nixpkgs.nix -A protosrc --no-out-link) +protosrc=$(nix-build using-nix/0-from-nixpkgs.nix -A protosrc --no-out-link \ + --check) +[[ "$protosrc_" == "$protosrc" ]] +nix store dump-path $protosrc > protosrc.nar +protosrc_hash=$(nix hash file protosrc.nar) +grep "outputHash = \"$protosrc_hash\";$" using-nix/0-from-nixpkgs.nix +grep "sha256 = \"$protosrc_hash\";$" using-nix/0-prebuilt.nix + + +[[ ! -L result* ]] + + +# print release notes + +set +x + +cat <<\EOF +protosrc (patched sources of stage1 tinycc/protomusl/protobusybox) has to come from somewhere, and there are three main options: build as part of bootstrap, build using nixpkgs or take them from here. + +You don't need these files neither when you do the whole bootstrap-Nix-included route using ./build.sh/make, nor when you can afford pulling nixpkgs to build them. But there's at least a case for building in Hydra under restricted-eval mode where you can neither inject stuff externally nor IFD. These allow kickstarting Hydra builds as long as recipes/1-stage1/seed.host-executed.sh and recipes/1-stage1/syscall.h weren't modified. + +See using-nix/0.nix for more explanations of all three seeding options, using-nix/0-from-nixpkgs for how these were built. + +EOF + +echo "$tinycc_hash" tinycc-liberated.nar +echo "$protosrc_hash" protosrc.nar diff --git a/06/recipes/0-tcc-seed/seed.host-executed.sh b/06/recipes/0-tcc-seed/seed.host-executed.sh new file mode 100755 index 0000000..59d660f --- /dev/null +++ b/06/recipes/0-tcc-seed/seed.host-executed.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -uex + +cp tcc-seed stage/store/0-tcc-seed diff --git a/06/recipes/1-stage1.c b/06/recipes/1-stage1.c new file mode 100644 index 0000000..b78c079 --- /dev/null +++ b/06/recipes/1-stage1.c @@ -0,0 +1,933 @@ +// SPDX-FileCopyrightText: 2021 Alexander Sosedkin +// SPDX-License-Identifier: MIT + +// syscalls (x86_64) /////////////////////////////////////////////////////////// + +#ifndef INSIDE_NIX +#include "1-stage1/syscall.h" +#define TCC_SEED "/store/0-tcc-seed" +#define PROTOSRC "/protosrc" +#define RECIPES_STAGE1 "/recipes/1-stage1" +#define TMP_STAGE1 "/tmp/1-stage1" +#define STORE_PROTOBUSYBOX "/store/1-stage1/protobusybox" +#define STORE_PROTOMUSL "/store/1-stage1/protomusl" +#define STORE_TINYCC "/store/1-stage1/tinycc" +#define EXTRA_HELLO_ARGS +#else +#include "syscall.h" +#define EXTRA_HELLO_ARGS \ + "-DRECIPES_STAGE1=\"" RECIPES_STAGE1"\"", +#endif + +#define SYS_write 1 +#define SYS_open 2 +#define SYS_fork 57 +#define SYS_execve 59 +#define SYS_exit 60 +#define SYS_wait4 61 +#define SYS_getdents 78 +#define SYS_mkdir 83 + +long write(int fd, const void* buf, long cnt) { + return __syscall3(SYS_write, fd, (long) buf, cnt); +} + +int execve(const char* fname, + const char* const argv[], const char* const envp[]) { + return __syscall3(SYS_execve, (long) fname, (long) argv, (long) envp); +} + +void exit(int code) { __syscall1(SYS_exit, code); }; + +int fork() { return __syscall0(SYS_fork); } + +int wait4(int pid, int* status, int options, void* rusage) { + return __syscall4( + SYS_wait4, + pid, (long) status, options, (long) rusage + ); +} + +int mkdir(const char *pathname, unsigned mode) { + return __syscall2(SYS_mkdir, (long) pathname, mode); +} + +int open(const char *pathname, int flags, int mode) { + return __syscall3(SYS_open, (long) pathname, flags, mode); +} + +struct linux_dirent { + long d_ino; + long d_off; + unsigned short d_reclen; + char d_name[]; +}; +int getdents(unsigned int fd, struct linux_dirent *dirp, + unsigned int count) { + return __syscall3(SYS_getdents, fd, (long) dirp, count); +} + + +// random defines ////////////////////////////////////////////////////////////// + +#define NULL ((void*) 0) +#define STDOUT 1 +#define STDERR 2 +#define O_RDONLY 0 +#define O_DIRECTORY 0200000 +#define DT_REG 8 + + +// basic QoL /////////////////////////////////////////////////////////////////// + +unsigned strlen(const char* s) { + unsigned l; + for (l = 0; s[l]; l++); + return l; +} + +int write_(int fd, const char* msg) { + return write(fd, msg, strlen(msg)); +} + +#define __quote(x) #x +#define _quote(x) __quote(x) +// real assert calls abort() -> 128 + SIGABRT = 134 +#define assert(v) \ + while (!(v)) { \ + write_(STDERR, "Assertion "); \ + write_(STDERR, _quote(v)); write_(STDERR, " failed at "); \ + write_(STDERR, __FILE__); write_(STDERR, ":"); \ + write_(STDERR, __func__); write_(STDERR, ":"); \ + write_(STDERR, _quote(__LINE__)); write_(STDERR, "!\n"); \ + exit(134); \ + } + +void err(const char* msg) { assert(write_(STDERR, msg) == strlen(msg)); } + +void log_begin_line(const char* msg) { + assert(write_(STDOUT, "### 1-stage1.c: ") == 16); + assert(write_(STDOUT, msg) == strlen(msg)); +} +void log_continue_line(const char* msg) { + assert(write_(STDOUT, msg) == strlen(msg)); +} +void log_end_line() { assert(write_(STDOUT, "\n") == 1); } +void log(const char* msg) { log_begin_line(msg); log_end_line(); }; + + +// more library function substitutes /////////////////////////////////////////// + +void memset(char* ptr, int with, long len) { + long i; + for (i = 0; i < len; i++) + ptr[i] = with; +} + +char* strcpy(char* dest, const char* src) { + while (*src) + *dest++ = *src++; + *dest = 0; + return dest; +} + +int strcmp(const char* a, const char* b) { + for (; *a && *b; a++, b++) + if (*a != *b) + return (*a < *b) ? -1 : 1; + return !a && !b; +} + + +// my convenience functions: mkdir'ing ///////////////////////////////////////// + + +void mkreqdirs(const char* path) { + char b[128], *p; + strcpy(b, path); + for (p = b + 1; *p; p++) + if (*p == '/') { *p = '\0'; mkdir(b, 0777); *p = '/'; } +} +void mkreqdirs_at(const char* at, const char* subpath) { + char b[128], *p; + p = strcpy(b, at); + p = strcpy(strcpy(strcpy(p, "/"), subpath), "/"); + mkreqdirs(b); +} +#define mkdirs_at(at, args...) \ + do { \ + const char* const* p; \ + for (p = (char*[]) { "/", ## args, NULL }; *p; p++) \ + mkreqdirs_at(at, *p); \ + } while (0) + + +// my convenience functions: fork + exec /////////////////////////////////////// + +int run_(const char* cmd, const char* const args[], const char* const env[]) { + int pid, status, termsig; + if (pid = fork()) { + assert(wait4(pid, &status, 0, NULL) == pid); + termsig = status & 0x7f; // WTERMSIG + if (!termsig) { + return (status & 0xff00) >> 8; // WEXITSTATUS + } else { + err("child has been killed"); + exit(termsig); + } + } else { + exit(execve(cmd, args, env)); + } + return 0; // unreacheable +} + +#define run(expected_retcode, first_arg, args...) \ + do { \ + const char const* __env[] = {NULL}; \ + const char const* __args[] = {(first_arg), ##args, NULL}; \ + int __i; \ + log_begin_line("run() running: "); \ + for(__i = 0; __args[__i]; __i++) { \ + log_continue_line(__args[__i]); \ + log_continue_line(" "); \ + } \ + log_end_line(); \ + assert(run_(first_arg, __args, __env) == (expected_retcode)); \ + } while (0) +#define run0(first_arg, args...) run(0, (first_arg), ## args) + + +// my convenience functions: dynamic args accumulation / command execution ///// + +struct args_accumulator { + char* pointers[4096]; + char storage[262144]; + char** ptr_curr; + char* char_curr; +}; +void _aa_init(struct args_accumulator* aa) { + aa->char_curr = aa->storage; + aa->ptr_curr = aa->pointers; + *aa->ptr_curr = NULL; +} +void aa_append(struct args_accumulator* aa, const char* new_arg) { + *aa->ptr_curr = aa->char_curr; + *++aa->ptr_curr = NULL; + aa->char_curr = strcpy(aa->char_curr, new_arg); + aa->char_curr++; +} +void _aa_extend_from_arr(struct args_accumulator* aa, const char* const* p) { + for (; *p; p++) + aa_append(aa, *p); +} +void aa_extend_from(struct args_accumulator* aa, const void* from) { + // Cheat a little with void* to accept + // both struct args_accumulator* and null-terminated string arrays. + // Qualifiers could be stricter, but then declaring get cumbersome. + _aa_extend_from_arr(aa, (const char* const*) from); +} +#define aa_extend(aa_ptr, args...) \ + _aa_extend_from_arr(aa_ptr, (const char*[]) { NULL, ## args, NULL } + 1) +#define aa_init(aa_ptr, args...) \ + do { _aa_init(aa_ptr); aa_extend(aa_ptr, ## args); } while (0) +void aa_sort(struct args_accumulator* aa) { + int changes; + char **p, **n, *t; + if (!aa->pointers[0]) + return; + if (!aa->pointers[1]) + return; + do { + changes = 0; + for (p = aa->pointers, n = p + 1; *n; p++, n++) { + if (strcmp(*p, *n) > 0) { + t = *p; *p = *n; *n = t; + changes = 1; + } + } + } while (changes); +} +int aa_run(const struct args_accumulator* aa) { + int i; + log_begin_line("aa_run() running: "); + for (i = 0; aa->pointers[i]; i++) { + log_continue_line(aa->pointers[i]); + log_continue_line(" "); + } + log_end_line(STDOUT, "\n"); + return run_(aa->pointers[0], aa->pointers, (char*[]) { NULL }); +} +#define aa_run0(aa_ptr) do { assert(aa_run(aa_ptr) == 0); } while (0) + + +// my convenience functions: compiling whole directories worth of files //////// + +_Bool is_compileable(char* fname) { + int i = 0; + while (fname[i]) + i++; + if (i > 2) + if (fname[i - 2] == '.') + if (fname[i - 1] == 'c' || fname[i-1] == 's') + return 1; + return 0; +} + +void aa_extend_from_dir(struct args_accumulator* aa_out, + unsigned short keep_components, const char* dir_path) { + struct args_accumulator aa; + char d_buf[256]; + char buf[256]; + const char* prefix; + char* out_subpath; + struct linux_dirent* d; + int fd, r; + char d_type; + + aa_init(&aa); + + prefix = dir_path + strlen(dir_path); + while (keep_components) { + while (*prefix != '/') + prefix--; + keep_components--; + prefix += keep_components ? -1 : 1; + } + + fd = open(dir_path, O_RDONLY | O_DIRECTORY, 0); + assert(fd != -1); + + while (1) { + d = (struct linux_dirent*) d_buf; + r = getdents(fd, d, 256); + assert(r != -1); + if (!r) + break; + while ((char*) d - d_buf < r) { + d_type = *((char*) d + d->d_reclen - 1); + if ((d_type == DT_REG || !d_type) + && is_compileable(d->d_name)) { + out_subpath = strcpy(buf, prefix); + out_subpath = strcpy(out_subpath, "/"); + out_subpath = strcpy(out_subpath, d->d_name); + aa_append(&aa, buf); + } + d = (struct linux_dirent*) ((char*) d + d->d_reclen); + } + } + aa_sort(&aa); // iteration order isn't guaranteed, make stable + aa_extend_from(aa_out, &aa); +} + + +void mass_compile(const char* cc, const void* compile_args, + const char* in_dir_path, const void* fnames /* NULL=auto */, + const char* out_obj_dir_path, const char* out_lib_file_path) { + // qualifiers could've been stricter + // const void* could be struct args_accumulator*, + // NULL-terminated arrays or even just NULLs for fnames + struct args_accumulator aa, aa_link, aa_sources; + char in_file_path_buf[128], out_file_path_buf[128]; + char* in_file_path; + char* out_file_path; + char** p; + + aa_init(&aa_sources); + if (!fnames) + aa_extend_from_dir(&aa_sources, 0, in_dir_path); + else + aa_extend_from(&aa_sources, fnames); + + mkreqdirs(out_lib_file_path); + aa_init(&aa_link, cc, "-ar", "rc", out_lib_file_path); + + for (p = (char**) &aa_sources; *p; p++) { + in_file_path = strcpy(in_file_path_buf, in_dir_path); + in_file_path = strcpy(in_file_path, "/"); + in_file_path = strcpy(in_file_path, *p); + + out_file_path = strcpy(out_file_path_buf, out_obj_dir_path); + out_file_path = strcpy(out_file_path, "/"); + out_file_path = strcpy(out_file_path, *p); + out_file_path = strcpy(out_file_path, ".o"); + mkreqdirs(out_file_path_buf); + + aa_init(&aa, cc); + aa_extend_from(&aa, compile_args); + aa_extend(&aa, "-c", in_file_path_buf, "-o", out_file_path_buf); + aa_run0(&aa); + + aa_append(&aa_link, out_file_path_buf); + } + aa_run0(&aa_link); +} + + +// Kinda boring parts ////////////////////////////////////////////////////////// + +#define TCC_ARGS_NOSTD "-nostdlib", "-nostdinc", "-I", "/protosrc/tinycc/include" + + +void sanity_test() { + struct args_accumulator aa1, aa2; + + log("sanity-testing run()..."); + log("* testing run() -> retcode 0..."); + run0(TCC_SEED, "--help"); + log("* testing run() -> retcode 1..."); + run(1, TCC_SEED, "-ar", "--help"); + log("run() seems to work OK"); + + log("sanity-testing args accumulator..."); + log("* testing aa_append, aa_extend, aa_sort and aa_run0..."); + aa_init(&aa1); + aa_init(&aa2); + aa_append(&aa1, TCC_SEED); + aa_append(&aa2, "-ar"); + aa_extend(&aa2, "help-must-precede-ar", "--help"); + aa_sort(&aa2); + aa_extend_from(&aa1, &aa2); + assert(!strcmp(((char**) &aa1)[0], TCC_SEED)); + assert(!strcmp(((char**) &aa1)[1], "--help")); + assert(!strcmp(((char**) &aa1)[2], "-ar")); + assert(!strcmp(((char**) &aa1)[3], "help-must-precede-ar")); + assert(NULL == ((char**) &aa1)[4]); + aa_run0(&aa1); + + log("* testing aa_multi and aa_run for 1..."); + aa_init(&aa1, TCC_SEED, "-ar", "--help"); + assert(aa_run(&aa1) == 1); +} + + +// Interesting parts: libtcc1 ////////////////////////////////////////////////// + + +void compile_libtcc1_1st_time_nostd(const char* cc) { + log("compiling our first libtcc1.a..."); + mass_compile(cc, (char* []) { TCC_ARGS_NOSTD, "-DTCC_MUSL", NULL }, + PROTOSRC"/tinycc/lib", (char* []) { + "libtcc1.c", "alloca.S", + "dsohandle.c", "stdatomic.c", "va_list.c", + 0}, + TMP_STAGE1"/tinycc/libtcc1", + STORE_TINYCC"/lib/libtcc1.a"); +} // see also compile_libtcc1 far below + + +// Interesting parts: protomusl //////////////////////////////////////////////// + +#define PROTOMUSL_EXTRA_CFLAGS \ + "-std=c99", \ + "-D_XOPEN_SOURCE=700" +#define PROTOMUSL_INTERNAL_INCLUDES \ + "-I" PROTOSRC"/protomusl/src/include", \ + "-I" PROTOSRC"/protomusl/arch/x86_64", \ + "-I" PROTOSRC"/protomusl/host-generated/sed1", \ + "-I" PROTOSRC"/protomusl/host-generated/sed2", \ + "-I" PROTOSRC"/protomusl/arch/generic", \ + "-I" PROTOSRC"/protomusl/src/internal", \ + "-I" PROTOSRC"/protomusl/include" +#define PROTOMUSL_NOSTD_LDFLAGS_PRE \ + "-static", \ + STORE_PROTOMUSL"/lib/crt1.o", \ + STORE_PROTOMUSL"/lib/crti.o" +#define PROTOMUSL_NOSTD_LDFLAGS_POST \ + STORE_PROTOMUSL"/lib/libc.a", \ + STORE_PROTOMUSL"/lib/crtn.o" +#define PROTOMUSL_INCLUDES \ + "-I" PROTOSRC"/protomusl/include", \ + "-I" PROTOSRC"/protomusl/arch/x86_64", \ + "-I" PROTOSRC"/protomusl/arch/generic", \ + "-I" PROTOSRC"/protomusl/host-generated/sed1", \ + "-I" PROTOSRC"/protomusl/host-generated/sed2" + +void compile_protomusl(const char* cc) { + struct args_accumulator aa; + aa_init(&aa); + + log("compiling part of musl (protomusl)..."); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/conf"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/ctype"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/dirent"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/env"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/errno"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/exit"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/fcntl"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/fenv"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/internal"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/ldso"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/legacy"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/linux"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/locale"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/malloc"); + aa_extend_from_dir(&aa, 2, PROTOSRC"/protomusl/src/malloc/mallocng"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/math"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/misc"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/mman"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/multibyte"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/network"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/passwd"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/prng"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/process"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/regex"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/select"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/setjmp"); + aa_extend_from_dir(&aa, 2, PROTOSRC"/protomusl/src/setjmp/x86_64"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/signal"); + aa_extend_from_dir(&aa, 2, PROTOSRC"/protomusl/src/signal/x86_64"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/stat"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/stdio"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/stdlib"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/string"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/temp"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/termios"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/thread"); + aa_extend_from_dir(&aa, 2, PROTOSRC"/protomusl/src/thread/x86_64"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/time"); + aa_extend_from_dir(&aa, 1, PROTOSRC"/protomusl/src/unistd"); + mass_compile(cc, (char*[]) { + TCC_ARGS_NOSTD, + PROTOMUSL_EXTRA_CFLAGS, + PROTOMUSL_INTERNAL_INCLUDES, + 0}, + PROTOSRC"/protomusl/src", &aa, + TMP_STAGE1"/protomusl", + STORE_PROTOMUSL"/lib/libc.a"); + + log("compiling crt bits of protomusl..."); + run0(cc, TCC_ARGS_NOSTD, PROTOMUSL_INTERNAL_INCLUDES, "-DCRT", + "-c", PROTOSRC"/protomusl/crt/crt1.c", + "-o", STORE_PROTOMUSL"/lib/crt1.o"); + run0(cc, TCC_ARGS_NOSTD, "-DCRT", + "-c", PROTOSRC"/protomusl/crt/crti.c", + "-o", STORE_PROTOMUSL"/lib/crti.o"); + run0(cc, TCC_ARGS_NOSTD, "-DCRT", + "-c", PROTOSRC"/protomusl/crt/crtn.c", + "-o", STORE_PROTOMUSL"/lib/crtn.o"); +} + +void test_example_1st_time_nostd(const char* cc) { + log("linking an example (1st time)..."); + run0(cc, TCC_ARGS_NOSTD, PROTOMUSL_INCLUDES, + PROTOMUSL_NOSTD_LDFLAGS_PRE, + RECIPES_STAGE1"/hello.c", EXTRA_HELLO_ARGS + PROTOMUSL_NOSTD_LDFLAGS_POST, + STORE_TINYCC"/lib/libtcc1.a", + "-o", TMP_STAGE1"/protomusl-hello"); + + log("executing an example..."); + run(42, TMP_STAGE1"/protomusl-hello"); +} + + +// Interesting parts: recompiling tcc ////////////////////////////////////////// + +void compile_libtcc1(const char* cc) { + log("recompiling libtcc1.a..."); + mass_compile(cc, (char*[]) { "-DTCC_MUSL", PROTOMUSL_INCLUDES, 0}, + PROTOSRC"/tinycc/lib", (char*[]) { + "libtcc1.c", "alloca.S", + "dsohandle.c", "stdatomic.c", "va_list.c", + // now we can compile more + "tcov.c", "alloca-bt.S", + // bcheck.c is excluded, as it references __FILE__ + 0}, + TMP_STAGE1"/tinycc/libtcc1", + STORE_TINYCC"/lib/libtcc1.a"); +} + +#define TCC_CFLAGS \ + "-I"PROTOSRC"/tinycc", \ + "-I"PROTOSRC"/tinycc/include", \ + "-I"TMP_STAGE1"/tinycc/gen", \ + "-DTCC_VERSION=\"mob-af1abf1\"", \ + "-DTCC_GITHASH=\"mob:af1abf1\"", \ + "-DTCC_TARGET_X86_64", \ + "-DTCC_MUSL", \ + "-DONE_SOURCE=0", \ + "-DCONFIG_TCCDIR=\""STORE_TINYCC"/lib\"", \ + "-DCONFIG_TCC_SYSINCLUDEPATHS="\ + "\""STORE_PROTOMUSL"/include\"", \ + "-DCONFIG_TCC_LIBPATHS=\""STORE_PROTOMUSL"/lib\"", \ + "-DCONFIG_TCC_CRTPREFIX=\""STORE_PROTOMUSL"/lib\"", \ + "-DCONFIG_TCC_ELFINTERP=\"/sorry/not/yet\"", \ + "-DCONFIG_TCC_PREDEFS=1" + +void compile_tcc_1st_time_nostd(const char* cc) { + log("compiling tcc's conftest..."); + mkdirs_at(TMP_STAGE1"/tinycc", "gen", "lib", "bin"); + mkdirs_at(STORE_TINYCC"", "lib", "bin"); + run0(cc, TCC_ARGS_NOSTD, PROTOMUSL_INCLUDES, + PROTOMUSL_NOSTD_LDFLAGS_PRE, + "-DC2STR", PROTOSRC"/tinycc/conftest.c", + PROTOMUSL_NOSTD_LDFLAGS_POST, + STORE_TINYCC"/lib/libtcc1.a", + "-o", TMP_STAGE1"/tinycc/conftest" + ); + log("generating tccdefs_.h with conftest..."); + run0(TMP_STAGE1"/tinycc/conftest", + PROTOSRC"/tinycc/include/tccdefs.h", + TMP_STAGE1"/tinycc/gen/tccdefs_.h"); + + log("compiling libtcc..."); + mass_compile(cc, (char*[]) { + TCC_ARGS_NOSTD, + PROTOMUSL_INCLUDES, + TCC_CFLAGS, + 0}, + PROTOSRC"/tinycc", (char*[]) { + "libtcc.c", "tccpp.c", "tccgen.c", "tccelf.c", + "tccdbg.c", "tccasm.c", "tccrun.c", + "x86_64-gen.c", "x86_64-link.c", "i386-asm.c", + 0}, + TMP_STAGE1"/tinycc/libtcc", + STORE_TINYCC"/lib/libtcc.a"); + run0(cc, TCC_ARGS_NOSTD, PROTOMUSL_INCLUDES, TCC_CFLAGS, + PROTOMUSL_NOSTD_LDFLAGS_PRE, + PROTOSRC"/tinycc/tcc.c", + STORE_TINYCC"/lib/libtcc.a", + PROTOMUSL_NOSTD_LDFLAGS_POST, + STORE_TINYCC"/lib/libtcc1.a", + "-o", STORE_TINYCC"/bin/tcc"); + run0(STORE_TINYCC"/bin/tcc", "-print-search-dirs"); +} + + +void compile_tcc(const char* cc) { + log("recompiling libtcc..."); + mass_compile(cc, (char*[]) { PROTOMUSL_INCLUDES, TCC_CFLAGS, 0}, + PROTOSRC"/tinycc", (char*[]) { + "libtcc.c", "tccpp.c", "tccgen.c", "tccelf.c", + "tccdbg.c", "tccasm.c", "tccrun.c", + "x86_64-gen.c", "x86_64-link.c", "i386-asm.c", + 0}, + TMP_STAGE1"/tinycc/libtcc", + STORE_TINYCC"/lib/libtcc.a"); + run0(cc, PROTOMUSL_INCLUDES, TCC_CFLAGS, "-static", + PROTOSRC"/tinycc/tcc.c", + STORE_TINYCC"/lib/libtcc.a", + "-o", STORE_TINYCC"/bin/tcc"); +} + +void test_example_intermediate(const char* cc) { + log("linking an example (our tcc, includes not installed)..."); + run0(cc, PROTOMUSL_INCLUDES, "-static", + RECIPES_STAGE1"/hello.c", EXTRA_HELLO_ARGS + "-o", TMP_STAGE1"/protomusl-hello"); + + log("executing an example..."); + run(42, TMP_STAGE1"/protomusl-hello"); +} + +void test_example_final(const char* cc_wrapper) { + log("linking an example (wrapped tcc, includes installed)..."); + run0(cc_wrapper, RECIPES_STAGE1"/hello.c", EXTRA_HELLO_ARGS + "-o", TMP_STAGE1"/protomusl-hello"); + + log("executing an example..."); + run(42, TMP_STAGE1"/protomusl-hello"); +} + + +// Interesting parts: hacky standalone busybox applets ///////////////////////// + +void compile_standalone_busybox_applets(const char* cc) { + log("compiling protolibbb..."); + mass_compile(cc, (char*[]) { + PROTOMUSL_INCLUDES, + "-I"PROTOSRC"/protobusybox/include/", + "-I"PROTOSRC"/protobusybox/libbb/", + "-include", RECIPES_STAGE1"/protobusybox.h", + 0}, + PROTOSRC"/protobusybox/libbb", (char*[]) { + "ask_confirmation.c", + "auto_string.c", + "bb_cat.c", + "bb_getgroups.c", + "bb_pwd.c", + "bb_strtonum.c", + "compare_string_array.c", + "concat_path_file.c", + "concat_subpath_file.c", + "copy_file.c", + "copyfd.c", + "crc32.c", + "default_error_retval.c", + "dump.c", + "endofname.c", + "executable.c", + "fclose_nonstdin.c", + "fflush_stdout_and_exit.c", + "full_write.c", + "get_last_path_component.c", + "get_line_from_file.c", + "getopt32.c", + "inode_hash.c", + "isdirectory.c", + "isqrt.c", + "last_char_is.c", + "llist.c", + "make_directory.c", + "messages.c", + "mode_string.c", + "parse_mode.c", + "perror_msg.c", + "perror_nomsg_and_die.c", + "printable_string.c", + "process_escape_sequence.c", + "procps.c", + "ptr_to_globals.c", + "read.c", + "read_printf.c", + "recursive_action.c", + "remove_file.c", + "safe_poll.c", + "safe_strncpy.c", + "safe_write.c", + "signals.c", + "single_argv.c", + "skip_whitespace.c", + "sysconf.c", + "time.c", + "u_signal_names.c", + "verror_msg.c", + "vfork_daemon_rexec.c", + "wfopen.c", + "wfopen_input.c", + "xatonum.c", + "xfunc_die.c", + "xfuncs.c", + "xfuncs_printf.c", + "xgetcwd.c", + "xreadlink.c", + "xrealloc_vector.c", + "xregcomp.c", + 0}, + TMP_STAGE1"/protobusybox/libbb", + TMP_STAGE1"/protobusybox/libbb.a"); + + + log("compiling standalone protobusybox applets..."); + mkreqdirs(STORE_PROTOBUSYBOX"/bin/"); + #define compile_applet(applet_name, files...) \ + run0(cc, PROTOMUSL_INCLUDES, \ + "-D__GNUC__=2", "-D__GNUC_MINOR__=7", \ + "-static", \ + "-I"PROTOSRC"/protobusybox/include", \ + "-include", RECIPES_STAGE1"/protobusybox.h", \ + "-DAPPLET_MAIN=" applet_name "_main", \ + RECIPES_STAGE1"/protobusybox.c", \ + ## files, \ + TMP_STAGE1"/protobusybox/libbb.a", \ + "-o", STORE_PROTOBUSYBOX"/bin/" applet_name); + compile_applet("echo", PROTOSRC"/protobusybox/coreutils/echo.c") + run0(STORE_PROTOBUSYBOX"/bin/echo", + "Hello from protobusybox!"); + + compile_applet("ash", + PROTOSRC"/protobusybox/shell/shell_common.c", + PROTOSRC"/protobusybox/shell/ash_ptr_hack.c", + PROTOSRC"/protobusybox/shell/math.c", + PROTOSRC"/protobusybox/coreutils/printf.c", + PROTOSRC"/protobusybox/coreutils/test_ptr_hack.c", + PROTOSRC"/protobusybox/coreutils/test.c", + PROTOSRC"/protobusybox/shell/ash.c") + run(42, STORE_PROTOBUSYBOX"/bin/ash", "-c", + "printf 'Hello from ash!\n'; exit 42"); + + compile_applet("basename", + PROTOSRC"/protobusybox/coreutils/basename.c") + compile_applet("cat", PROTOSRC"/protobusybox/coreutils/cat.c") + compile_applet("chmod", PROTOSRC"/protobusybox/coreutils/chmod.c") + compile_applet("cp", + PROTOSRC"/protobusybox/coreutils/libcoreutils/cp_mv_stat.c", + PROTOSRC"/protobusybox/coreutils/cp.c"); + compile_applet("cut", PROTOSRC"/protobusybox/coreutils/cut.c"); + compile_applet("dirname", PROTOSRC"/protobusybox/coreutils/dirname.c") + compile_applet("env", PROTOSRC"/protobusybox/coreutils/env.c"); + compile_applet("expr", PROTOSRC"/protobusybox/coreutils/expr.c"); + compile_applet("head", PROTOSRC"/protobusybox/coreutils/head.c"); + compile_applet("install", PROTOSRC"/protobusybox/coreutils/install.c"); + compile_applet("ln", PROTOSRC"/protobusybox/coreutils/ln.c"); + compile_applet("ls", PROTOSRC"/protobusybox/coreutils/ls.c"); + compile_applet("mkdir", PROTOSRC"/protobusybox/coreutils/mkdir.c"); + compile_applet("mktemp", PROTOSRC"/protobusybox/coreutils/mktemp.c"); + compile_applet("mv", + PROTOSRC"/protobusybox/coreutils/libcoreutils/cp_mv_stat.c", + PROTOSRC"/protobusybox/coreutils/mv.c"); + compile_applet("od", PROTOSRC"/protobusybox/coreutils/od.c"); + compile_applet("pwd", PROTOSRC"/protobusybox/coreutils/pwd.c"); + compile_applet("rm", PROTOSRC"/protobusybox/coreutils/rm.c"); + compile_applet("rmdir", PROTOSRC"/protobusybox/coreutils/rmdir.c"); + compile_applet("sleep", PROTOSRC"/protobusybox/coreutils/sleep.c"); + compile_applet("sort", PROTOSRC"/protobusybox/coreutils/sort.c"); + compile_applet("touch", PROTOSRC"/protobusybox/coreutils/touch.c"); + compile_applet("tr", PROTOSRC"/protobusybox/coreutils/tr.c"); + compile_applet("true", PROTOSRC"/protobusybox/coreutils/true.c"); + compile_applet("uname", PROTOSRC"/protobusybox/coreutils/uname.c"); + compile_applet("uniq", PROTOSRC"/protobusybox/coreutils/uniq.c"); + compile_applet("wc", PROTOSRC"/protobusybox/coreutils/wc.c"); + + #define LIBARCHIVE PROTOSRC"/protobusybox/archival/libarchive" + compile_applet("tar", + LIBARCHIVE "/data_align.c", + LIBARCHIVE "/data_extract_all.c", + LIBARCHIVE "/data_extract_to_stdout.c", + LIBARCHIVE "/data_skip.c", + LIBARCHIVE "/decompress_bunzip2.c", + LIBARCHIVE "/decompress_gunzip.c", + LIBARCHIVE "/decompress_unxz.c", + LIBARCHIVE "/filter_accept_all.c", + LIBARCHIVE "/filter_accept_reject_list.c", + LIBARCHIVE "/find_list_entry.c", + LIBARCHIVE "/get_header_tar.c", + LIBARCHIVE "/header_list.c", + LIBARCHIVE "/header_skip.c", + LIBARCHIVE "/header_verbose_list.c", + LIBARCHIVE "/init_handle.c", + LIBARCHIVE "/open_transformer.c", + LIBARCHIVE "/seek_by_jump.c", + LIBARCHIVE "/seek_by_read.c", + LIBARCHIVE "/unsafe_prefix.c", + LIBARCHIVE "/unsafe_symlink_target.c", + PROTOSRC"/protobusybox/archival/chksum_and_xwrite_tar_header.c", + PROTOSRC"/protobusybox/archival/tar.c"); + + compile_applet("bzip2", PROTOSRC"/protobusybox/archival/bzip2.c", + PROTOSRC"/protobusybox/archival/bbunzip.c", + LIBARCHIVE "/decompress_bunzip2.c", + LIBARCHIVE "/decompress_gunzip.c", + LIBARCHIVE "/decompress_unxz.c", + LIBARCHIVE "/open_transformer.c" + ); + + compile_applet("awk", PROTOSRC"/protobusybox/editors/awk.c"); + compile_applet("cmp", PROTOSRC"/protobusybox/editors/cmp.c"); + compile_applet("diff", PROTOSRC"/protobusybox/editors/diff.c"); + compile_applet("sed", PROTOSRC"/protobusybox/editors/sed.c"); + + compile_applet("grep", PROTOSRC"/protobusybox/findutils/grep.c"); + compile_applet("find", PROTOSRC"/protobusybox/findutils/find.c"); + compile_applet("xargs", PROTOSRC"/protobusybox/findutils/xargs.c"); +} + + +// Little things we'll do now when we have a shell ///////////////////////////// + +void verify_tcc_stability(void) { + run0(STORE_PROTOBUSYBOX"/bin/cp", + STORE_TINYCC"/bin/tcc", TMP_STAGE1"/tcc-bak"); + compile_tcc(STORE_TINYCC"/bin/tcc"); + run0(STORE_PROTOBUSYBOX"/bin/diff", + STORE_TINYCC"/bin/tcc", TMP_STAGE1"/tcc-bak"); +} + +void tweak_output_in_store(void) { + run0(STORE_PROTOBUSYBOX"/bin/ash", "-uexvc", + ":> "TMP_STAGE1"/empty.c\n" + STORE_TINYCC"/bin/tcc -c "TMP_STAGE1"/empty.c " + "-o "TMP_STAGE1"/empty.o\n" + STORE_TINYCC"/bin/tcc -ar "TMP_STAGE1"/empty.a " + TMP_STAGE1"/empty.o\n" + STORE_PROTOBUSYBOX"/bin/cp "TMP_STAGE1"/empty.a " + STORE_PROTOMUSL"/lib/libm.a\n" + STORE_PROTOBUSYBOX"/bin/cp "TMP_STAGE1"/empty.a " + STORE_PROTOMUSL"/lib/libpthread.a\n" + + STORE_PROTOBUSYBOX"/bin/rm -rf " + STORE_PROTOMUSL"/include\n" + STORE_PROTOBUSYBOX"/bin/mkdir -p " + STORE_PROTOMUSL"/include/bits\n" + STORE_PROTOBUSYBOX"/bin/cp -rf " + PROTOSRC"/protomusl/host-generated/sed1/bits " + PROTOSRC"/protomusl/host-generated/sed2/bits " + PROTOSRC"/protomusl/arch/generic/* " + PROTOSRC"/protomusl/arch/x86_64/* " + PROTOSRC"/protomusl/include/* " + STORE_PROTOMUSL"/include/\n" + ); +} + + +void wrap_tcc_tools(void) { + #define EXECTCC "#!"STORE_PROTOBUSYBOX"/bin/ash\n" \ + "exec "STORE_TINYCC"/bin/tcc" + #define PASSTHROUGH "\\\"\\$@\\\"" // i.e., \"\$@\", i.e, "$@" + run0(STORE_PROTOBUSYBOX"/bin/ash", "-uexvc", + "PATH="STORE_PROTOBUSYBOX"/bin\n" + "mkdir -p "STORE_TINYCC"/wrappers\n" + "cd "STORE_TINYCC"/wrappers\n" + "_CPP_ARGS=\"-I"STORE_PROTOMUSL"/include\"\n" + "_LD_ARGS='-static'\n" + "echo -e \"" EXECTCC " $_LD_ARGS " PASSTHROUGH"\" > cc\n" + "echo -e \"" EXECTCC " -E $_CPP_ARGS " PASSTHROUGH"\" > cpp\n" + "echo -e \"" EXECTCC " $_LD_ARGS " PASSTHROUGH"\" > ld\n" + "echo -e \"" EXECTCC " -ar " PASSTHROUGH "\" > ar\n" + "chmod +x cc cpp ld ar\n" + ); +} + + +// The main plot /////////////////////////////////////////////////////////////// + +int _start() { + struct args_accumulator aa_cmd; + struct args_accumulator aa_link_objs; + + log("hello from stage1!"); +#ifdef INSIDE_NIX + log("executed inside nix"); + log("TCC_SEED=" TCC_SEED); + log("PROTOSRC=" PROTOSRC); + log("RECIPES_STAGE1=" RECIPES_STAGE1); + log("TMP_STAGE1=" TMP_STAGE1); + log("STORE_PROTOBUSYBOX=" STORE_PROTOBUSYBOX); + log("STORE_PROTOMUSL=" STORE_PROTOMUSL); + log("STORE_TINYCC=" STORE_TINYCC); +#endif + + log("creating directories..."); + mkdirs_at("/", + TMP_STAGE1, STORE_PROTOBUSYBOX, STORE_PROTOMUSL, STORE_TINYCC); + + sanity_test(); + + // starting with the seeded TCC + compile_libtcc1_1st_time_nostd(TCC_SEED); + compile_protomusl(TCC_SEED); + test_example_1st_time_nostd(TCC_SEED); + + // build the first TCC that comes from our sources + compile_tcc_1st_time_nostd(TCC_SEED); + test_example_intermediate(STORE_TINYCC"/bin/tcc"); + // rebuild everything with it + compile_libtcc1(STORE_TINYCC"/bin/tcc"); + compile_protomusl(STORE_TINYCC"/bin/tcc"); + test_example_intermediate(STORE_TINYCC"/bin/tcc"); + + // this is the final tcc we'll build, should not be worth repeating + compile_tcc(STORE_TINYCC"/bin/tcc"); + // recompile everything else with the final tcc (could be an overkill) + compile_libtcc1(STORE_TINYCC"/bin/tcc"); + compile_protomusl(STORE_TINYCC"/bin/tcc"); + test_example_intermediate(STORE_TINYCC"/bin/tcc"); + + compile_standalone_busybox_applets(STORE_TINYCC"/bin/tcc"); + + verify_tcc_stability(); + tweak_output_in_store(); + wrap_tcc_tools(); + test_example_final(STORE_TINYCC"/wrappers/cc"); + + log("done"); + +#ifndef CHAINLOAD + return 0; +#else + assert(execve(CHAINLOAD, (char*[]) {CHAINLOAD, 0}, NULL)); + + log("could not exec into next stages (ok when building with make)"); + return 99; +#endif +} diff --git a/06/recipes/1-stage1/hello.c b/06/recipes/1-stage1/hello.c new file mode 100644 index 0000000..e1e8aa7 --- /dev/null +++ b/06/recipes/1-stage1/hello.c @@ -0,0 +1,21 @@ +#include + +#ifndef RECIPES_STAGE1 +#define RECIPES_STAGE1 "/recipes/1-stage1" +#endif +#define SOURCE_PATH RECIPES_STAGE1"/hello.c" + +int main(int argc, char** argv) { + printf("Hello world!\n2*2=%d\n", 2*2); + + printf("Own source (%s):\n", SOURCE_PATH); + FILE* f = fopen(SOURCE_PATH, "r"); + while (!feof(f)) { + fputc(fgetc(f), stdout); + } + if (ferror(f)) + return 99; + fclose(f); + + return 42; +} diff --git a/06/recipes/1-stage1/protobusybox.c b/06/recipes/1-stage1/protobusybox.c new file mode 100644 index 0000000..47f4389 --- /dev/null +++ b/06/recipes/1-stage1/protobusybox.c @@ -0,0 +1,46 @@ +extern int APPLET_MAIN(int argc, char** argv); // templated in + +#include +#include + +#include +static inline int *get_perrno(void) { return &errno; } +int *const bb_errno; + +char bb_common_bufsiz1[1024]; + +const char *applet_name; +int _argc; +const char **_argv; + +int main(int argc, char** argv) { + int** bb_errno_ptr = &((int*) bb_errno); + *bb_errno_ptr = ((int*) get_perrno()); + asm volatile ("":::"memory"); // counter optimizations + + _argc = argc; _argv = argv; + + applet_name = strrchr(argv[0], '/') \ + ? strrchr(argv[0], '/') + 1 \ + : argv[0]; + return APPLET_MAIN(argc, argv); +} + +void bb_show_usage(void) { + int i; + write(2 /* STDERR */, "protobusybox's show_usage stub\n", 31); + write(2 /* STDERR */, "ho help for you, sorry. argv[]: \n", 33); + for (i < 0; i < _argc; i++) { + write(2 /* STDERR */, "* `", 3); + write(2 /* STDERR */, _argv[i], strlen(_argv[i])); + write(2 /* STDERR */, "`\n", 2); + } + exit(1); +} + +// appletlib replacement +unsigned string_array_len(char **argv) { + unsigned i; + for (i = 0; argv[i]; i++); + return i; +} diff --git a/06/recipes/1-stage1/protobusybox.h b/06/recipes/1-stage1/protobusybox.h new file mode 100644 index 0000000..2729b35 --- /dev/null +++ b/06/recipes/1-stage1/protobusybox.h @@ -0,0 +1,240 @@ +#define NUM_APPLETS 1 +#define BB_GLOBAL_CONST +#define BB_VER "1.36.1" +#define AUTOCONF_TIMESTAMP +#define _GNU_SOURCE + +extern char bb_common_bufsiz1[]; +#define setup_common_bufsiz() ((void)0) +enum { COMMON_BUFSIZE = 1024 }; + +#define CONFIG_BUSYBOX_EXEC_PATH "/proc/self/exe" +#define CONFIG_FEATURE_COPYBUF_KB 256 +#define CONFIG_FEATURE_EDITING_MAX_LEN 1024 +#define CONFIG_GZIP_FAST 2 +#define CONFIG_UNAME_OSNAME "Linux" + +#define ENABLE_ASH_ALIAS 0 +#define ENABLE_ASH_BASH_COMPAT 0 +#define ENABLE_ASH_CMDCMD 1 +#define ENABLE_ASH_ECHO 0 +#define ENABLE_ASH_GETOPTS 1 +#define ENABLE_ASH_JOB_CONTROL 0 +#define ENABLE_ASH_MAIL 0 +#define ENABLE_ASH_PRINTF 1 +#define ENABLE_ASH_SLEEP 0 +#define ENABLE_ASH_TEST 1 +#define ENABLE_BB_ARCH 1 +#define ENABLE_BUNZIP2 0 +#define ENABLE_BZCAT 0 +#define ENABLE_DEBUG 0 +#define ENABLE_DESKTOP 1 +#define ENABLE_EGREP 0 +#define ENABLE_FEATURE_ALLOW_EXEC 0 +#define ENABLE_FEATURE_AWK_LIBM 0 +#define ENABLE_FEATURE_BZIP2_DECOMPRESS 1 +#define ENABLE_FEATURE_CLEAN_UP 0 +#define ENABLE_FEATURE_CP_REFLINK 0 +#define ENABLE_FEATURE_CROND_D 0 +#define ENABLE_FEATURE_CUT_REGEX 0 +#define ENABLE_FEATURE_EDITING 0 +#define ENABLE_FEATURE_FANCY_ECHO 1 +#define ENABLE_FEATURE_FIND_NOT 1 +#define ENABLE_FEATURE_FIND_PAREN 1 +#define ENABLE_FEATURE_FIND_PRUNE 1 +#define ENABLE_FEATURE_FIND_TYPE 1 +#define ENABLE_FEATURE_GZIP_DECOMPRESS 1 +#define ENABLE_FEATURE_HUMAN_READABLE 0 +#define ENABLE_FEATURE_LS_COLOR 0 +#define ENABLE_FEATURE_LS_FILETYPES 0 +#define ENABLE_FEATURE_LS_FOLLOWLINKS 0 +#define ENABLE_FEATURE_LS_RECURSIVE 0 +#define ENABLE_FEATURE_LS_SORTFILES 0 +#define ENABLE_FEATURE_LS_TIMESTAMPS 1 +#define ENABLE_FEATURE_LS_WIDTH 0 +#define ENABLE_FEATURE_NON_POSIX_CP 0 +#define ENABLE_FEATURE_PRESERVE_HARDLINKS 0 +#define ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS 0 +#define ENABLE_FEATURE_SEAMLESS_BZ2 1 +#define ENABLE_FEATURE_SEAMLESS_GZ 1 +#define ENABLE_FEATURE_SEAMLESS_LZMA 0 +#define ENABLE_FEATURE_SEAMLESS_XZ 1 +#define ENABLE_FEATURE_SEAMLESS_Z 0 +#define ENABLE_FEATURE_SHOW_THREADS 0 +#define ENABLE_FEATURE_SH_MATH 1 +#define ENABLE_FEATURE_SH_READ_FRAC 0 +#define ENABLE_FEATURE_SYSLOG 0 +#define ENABLE_FEATURE_TAR_AUTODETECT 1 +#define ENABLE_FEATURE_TAR_CREATE 1 +#define ENABLE_FEATURE_TAR_GNU_EXTENSIONS 1 +#define ENABLE_FEATURE_TAR_LONG_OPTIONS 1 +#define ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY 1 +#define ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY 0 +#define ENABLE_FEATURE_TOPMEM 1 +#define ENABLE_FEATURE_TOP_SMP_PROCESS 0 +#define ENABLE_FEATURE_TOUCH_SUSV3 1 +#define ENABLE_FEATURE_TR_CLASSES 0 +#define ENABLE_FEATURE_TR_EQUIV 0 +#define ENABLE_FEATURE_USE_SENDFILE 0 +#define ENABLE_FEATURE_VERBOSE 0 +#define ENABLE_FGREP 0 +#define ENABLE_FTPD 0 +#define ENABLE_HUSH_PRINTF 0 +#define ENABLE_HUSH_TEST 0 +#define ENABLE_KILLALL 0 +#define ENABLE_LOCALE_SUPPORT 1 +#define ENABLE_LONG_OPTS 1 +#define ENABLE_LZCAT 0 +#define ENABLE_PGREP 0 +#define ENABLE_PIDOF 0 +#define ENABLE_PKILL 0 +#define ENABLE_SELINUX 0 +#define ENABLE_SESTATUS 0 +#define ENABLE_TAR 1 +#define ENABLE_TEST1 0 +#define ENABLE_TEST2 0 +#define ENABLE_UNAME 0 +#define ENABLE_UNLZMA 0 +#define ENABLE_ZCAT 0 + +#define IF_AR(...) +#define IF_ASH_ALIAS(...) +#define IF_ASH_BASH_COMPAT(...) +#define IF_ASH_EXPAND_PRMT(...) +#define IF_ASH_HELP(...) +#define IF_ASH_OPTIMIZE_FOR_SIZE(...) +#define IF_BASENAME(...) +#define IF_BUNZIP2(...) +#define IF_BZCAT(...) +#define IF_BZIP2(...) __VA_ARGS__ +#define IF_CAT(...) +#define IF_CHGRP(...) +#define IF_CHMOD(...) +#define IF_CHOWN(...) +#define IF_CHROOT(...) +#define IF_CHVT(...) +#define IF_CKSUM(...) +#define IF_CLEAR(...) +#define IF_CPIO(...) +#define IF_DEALLOCVT(...) +#define IF_DESKTOP(...) __VA_ARGS__ +#define IF_DPKG(...) +#define IF_DPKG_DEB(...) +#define IF_DUMPKMAP(...) +#define IF_ECHO(...) __VA_ARGS__ +#define IF_EXTRA_COMPAT(...) +#define IF_FEATURE_AWK_GNU_EXTENSIONS(...) +#define IF_FEATURE_BZIP2_DECOMPRESS(...) __VA_ARGS__ +#define IF_FEATURE_CATN(...) +#define IF_FEATURE_CATV(...) +#define IF_FEATURE_CP_REFLINK(...) +#define IF_FEATURE_CUT_REGEX(...) +#define IF_FEATURE_FIND_AMIN(...) +#define IF_FEATURE_FIND_ATIME(...) +#define IF_FEATURE_FIND_CMIN(...) +#define IF_FEATURE_FIND_CONTEXT(...) +#define IF_FEATURE_FIND_CTIME(...) +#define IF_FEATURE_FIND_DELETE(...) +#define IF_FEATURE_FIND_DEPTH(...) +#define IF_FEATURE_FIND_EMPTY(...) +#define IF_FEATURE_FIND_EXEC(...) +#define IF_FEATURE_FIND_EXECUTABLE(...) +#define IF_FEATURE_FIND_EXEC_PLUS(...) +#define IF_FEATURE_FIND_GROUP(...) +#define IF_FEATURE_FIND_INUM(...) +#define IF_FEATURE_FIND_LINKS(...) +#define IF_FEATURE_FIND_MAXDEPTH(...) +#define IF_FEATURE_FIND_MMIN(...) +#define IF_FEATURE_FIND_MTIME(...) +#define IF_FEATURE_FIND_NEWER(...) +#define IF_FEATURE_FIND_NOT(...) __VA_ARGS__ +#define IF_FEATURE_FIND_PAREN(...) __VA_ARGS__ +#define IF_FEATURE_FIND_PATH(...) +#define IF_FEATURE_FIND_PERM(...) +#define IF_FEATURE_FIND_PRINT0(...) +#define IF_FEATURE_FIND_PRUNE(...) __VA_ARGS__ +#define IF_FEATURE_FIND_QUIT(...) +#define IF_FEATURE_FIND_REGEX(...) +#define IF_FEATURE_FIND_SAMEFILE(...) +#define IF_FEATURE_FIND_SIZE(...) +#define IF_FEATURE_FIND_TYPE(...) __VA_ARGS__ +#define IF_FEATURE_FIND_USER(...) +#define IF_FEATURE_FIND_XDEV(...) +#define IF_FEATURE_GREP_CONTEXT(...) +#define IF_FEATURE_GZIP_DECOMPRESS(...) __VA_ARGS__ +#define IF_FEATURE_HUMAN_READABLE(...) +#define IF_FEATURE_INSTALL_LONG_OPTIONS(...) +#define IF_FEATURE_LS_COLOR(...) +#define IF_FEATURE_LS_FILETYPES(...) +#define IF_FEATURE_LS_FOLLOWLINKS(...) +#define IF_FEATURE_LS_RECURSIVE(...) +#define IF_FEATURE_LS_SORTFILES(...) +#define IF_FEATURE_LS_TIMESTAMPS(...) __VA_ARGS__ +#define IF_FEATURE_LS_WIDTH(...) +#define IF_FEATURE_SEAMLESS_BZ2(...) __VA_ARGS__ +#define IF_FEATURE_SEAMLESS_GZ(...) __VA_ARGS__ +#define IF_FEATURE_SEAMLESS_LZMA(...) +#define IF_FEATURE_SEAMLESS_XZ(...) __VA_ARGS__ +#define IF_FEATURE_SEAMLESS_Z(...) +#define IF_FEATURE_SHOW_THREADS(...) +#define IF_FEATURE_SH_MATH(...) __VA_ARGS__ +#define IF_FEATURE_TAR_AUTODETECT(...) __VA_ARGS__ +#define IF_FEATURE_TAR_CREATE(...) __VA_ARGS__ +#define IF_FEATURE_TAR_FROM(...) __VA_ARGS__ +#define IF_FEATURE_TAR_LONG_OPTIONS(...) __VA_ARGS__ +#define IF_FEATURE_TAR_NOPRESERVE_TIME(...) __VA_ARGS__ +#define IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(...) __VA_ARGS__ +#define IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(...) +#define IF_FEATURE_TAR_TO_COMMAND(...) +#define IF_FEATURE_TIMEZONE(...) +#define IF_FEATURE_TOUCH_SUSV3(...) __VA_ARGS__ +#define IF_FEATURE_VERBOSE(...) +#define IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(...) +#define IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(...) +#define IF_FEATURE_XARGS_SUPPORT_PARALLEL(...) +#define IF_FEATURE_XARGS_SUPPORT_QUOTES(...) +#define IF_FEATURE_XARGS_SUPPORT_REPL_STR(...) +#define IF_FEATURE_XARGS_SUPPORT_TERMOPT(...) +#define IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(...) +#define IF_FGCONSOLE(...) +#define IF_GUNZIP(...) __VA_ARGS__ +#define IF_GZIP(...) __VA_ARGS__ +#define IF_KBD_MODE(...) +#define IF_LOADFONT(...) +#define IF_LOADKMAP(...) +#define IF_LONG_OPTS(...) __VA_ARGS__ +#define IF_LS(...) __VA_ARGS__ +#define IF_LZCAT(...) +#define IF_LZMA(...) +#define IF_LZOP(...) +#define IF_LZOPCAT(...) +#define IF_NOT_ASH_OPTIMIZE_FOR_SIZE(...) __VA_ARGS__ +#define IF_NOT_DESKTOP(...) +#define IF_NOT_FEATURE_FIND_MAXDEPTH(...) __VA_ARGS__ +#define IF_NOT_FEATURE_TAR_CREATE(...) +#define IF_NOT_FEATURE_TOUCH_SUSV3(...) +#define IF_OPENVT(...) +#define IF_PRINTF(...) __VA_ARGS__ +#define IF_RESET(...) +#define IF_RESIZE(...) +#define IF_RPM(...) +#define IF_RPM2CPIO(...) +#define IF_SELINUX(...) +#define IF_SETCONSOLE(...) +#define IF_SETFONT(...) +#define IF_SETKEYCODES(...) +#define IF_SETLOGCONS(...) +#define IF_SHELL_ASH(...) __VA_ARGS__ +#define IF_SHELL_HUSH(...) +#define IF_SHOWKEY(...) +#define IF_SLEEP(...) +#define IF_TAR(...) +#define IF_UNAME(...) __VA_ARGS__ +#define IF_UNCOMPRESS(...) +#define IF_UNLZMA(...) +#define IF_UNLZOP(...) +#define IF_UNXZ(...) __VA_ARGS__ +#define IF_UNZIP(...) +#define IF_XZ(...) +#define IF_XZCAT(...) +#define IF_ZCAT(...) diff --git a/06/recipes/1-stage1/seed.host-executed.sh b/06/recipes/1-stage1/seed.host-executed.sh new file mode 100755 index 0000000..62125a3 --- /dev/null +++ b/06/recipes/1-stage1/seed.host-executed.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash + +# 1st stage is special +# in that we don't have any semblance of a system to start with, +# meaning we can't even unpack sources, let alone patch them or something. +# For stage 1, we pre-unpack sources on the host and then fix them up with +# host's sed. + +#> FETCH 7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039 +#> FROM http://musl.libc.org/releases/musl-1.2.4.tar.gz + +#> FETCH f5a71d05664340ae46cda9579c6079a0f2fa809d24386d284f0d091e4d576a4e +#> FROM https://github.com/TinyCC/tinycc/archive/af1abf1f45d45b34f0b02437f559f4dfdba7d23c.tar.gz +#> AS tinycc-mob-af1abf1.tar.gz + +#> FETCH b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314 +#> FROM https://busybox.net/downloads/busybox-1.36.1.tar.bz2 + +set -ueo pipefail +TGT="$DESTDIR/tmp/1-stage1" + +echo "### $0: unpacking protomusl sources..." +mkdir -p "$DESTDIR/protosrc/protomusl" +tar --strip-components=1 -xzf downloads/musl-1.2.4.tar.gz \ + -C "$DESTDIR/protosrc/protomusl" + +echo "### $0: unpacking tinycc sources..." +mkdir -p "$DESTDIR/protosrc/tinycc" +tar --strip-components=1 -xzf downloads/tinycc-mob-af1abf1.tar.gz \ + -C $DESTDIR/protosrc/tinycc + +echo "### $0: unpacking protobusybox sources..." +mkdir -p "$DESTDIR/protosrc/protobusybox" +tar --strip-components=1 -xjf downloads/busybox-1.36.1.tar.bz2 \ + -C "$DESTDIR/protosrc/protobusybox" + +echo "### $0: patching up protomusl stage 1 sources..." +# original syscall_arch.h is not tcc-compatible, our syscall.h is dual-role +cp recipes/1-stage1/syscall.h \ + "$DESTDIR/protosrc/protomusl/arch/x86_64/syscall_arch.h" +pushd "$DESTDIR/protosrc/protomusl/" >/dev/null + # eliminiate a source path reference + sed -i 's/__FILE__/"__FILE__"/' include/assert.h + # two files have to be generated with host sed + mkdir -p host-generated/{sed1,sed2}/bits + sed -f ./tools/mkalltypes.sed \ + ./arch/x86_64/bits/alltypes.h.in \ + ./include/alltypes.h.in \ + > host-generated/sed1/bits/alltypes.h + sed -n -e s/__NR_/SYS_/p \ + < arch/x86_64/bits/syscall.h.in \ + > host-generated/sed2/bits/syscall.h + # more frivolous patching + echo '#define VERSION "1.2.2"' > src/internal/version.h + sed -i 's/@PLT//' src/signal/x86_64/sigsetjmp.s + rm -f src/signal/restore.c # *BIG URGH* + rm -f src/thread/clone.c # *BIG URGH #2* + rm -f src/thread/__set_thread_area.c # possible double-define + rm -f src/thread/__unmapself.c # double-define + rm -f src/math/sqrtl.c # tcc-incompatible + rm -f src/math/{acoshl,acosl,asinhl,asinl,hypotl}.c # sqrtl dep + sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \ + src/stdio/popen.c src/process/system.c + sed -i 's|execl("/bin/sh", "sh", "-c",|execlp("sh", "-c",|'\ + src/misc/wordexp.c +popd >/dev/null + +echo "### $0: patching up tinycc stage 1 sources..." +pushd "$DESTDIR/protosrc/tinycc" >/dev/null + :> config.h + # eliminate a source path reference + sed -i 's/__FILE__/"__FILE__"/' tcc.h + # don't hardcode paths + sed -i 's/SHN_ABS, filename);/SHN_ABS, "FILE stub");/' tccdbg.c + # break a circular dependency + sed -i 's/abort();//' lib/va_list.c +popd >/dev/null + +echo "### $0: patching up protobusybox stage 1 sources..." +pushd "$DESTDIR/protosrc/protobusybox" >/dev/null + :> include/NUM_APPLETS.h + :> include/common_bufsiz.h + # eliminate a source path reference + sed -i 's/__FILE__/"__FILE__"/' miscutils/fbsplash.c include/libbb.h + # already fixed in an unreleased version + sed -i 's/extern struct test_statics \*const test_ptr_to_statics/extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics/' coreutils/test.c +popd >/dev/null + +echo "### $0: done" diff --git a/06/recipes/1-stage1/syscall.h b/06/recipes/1-stage1/syscall.h new file mode 100644 index 0000000..4426819 --- /dev/null +++ b/06/recipes/1-stage1/syscall.h @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: 2021 Alexander Sosedkin +// SPDX-License-Identifier: MIT + + +// constants/macro for this file to serve as a drop-in replacement +// for musl-1.2.2's arch/x86_64/syscall_arch.h + +#define __SYSCALL_LL_E(x) (x) +#define __SYSCALL_LL_O(x) (x) + +#define VDSO_USEFUL +#define VDSO_CGT_SYM "__vdso_clock_gettime" +#define VDSO_CGT_VER "LINUX_2.6" +#define VDSO_GETCPU_SYM "__vdso_getcpu" +#define VDSO_GETCPU_VER "LINUX_2.6" + +#define IPC_64 0 + + +// a different, tcc-compatible implementation of syscall invocations functions + +static long __syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6); +asm ( + //".globl __syscall6;" + ".type __syscall6, @function;" + "__syscall6:;" + "movq %rdi, %rax;" + "movq %rsi, %rdi;" + "movq %rdx, %rsi;" + "movq %rcx, %rdx;" + "movq %r8, %r10;" + "movq %r9, %r8;" + "movq 8(%rsp),%r9;" + "syscall;" + "ret" +); + +static __inline long __syscall5(long n, long a1, long a2, long a3, long a4, long a5) { + return __syscall6(n, a1, a2, a3, a4, a5, 0); +} + +static __inline long __syscall4(long n, long a1, long a2, long a3, long a4) { + return __syscall6(n, a1, a2, a3, a4, 0, 0); +} + +static __inline long __syscall3(long n, long a1, long a2, long a3) { + return __syscall6(n, a1, a2, a3, 0, 0, 0); +} + +static __inline long __syscall2(long n, long a1, long a2) { + return __syscall6(n, a1, a2, 0, 0, 0, 0); +} + +static __inline long __syscall1(long n, long a1) { + return __syscall6(n, a1, 0, 0, 0, 0, 0); +} + +static __inline long __syscall0(long n) { + return __syscall6(n, 0, 0, 0, 0, 0, 0); +} diff --git a/06/recipes/2a0-static-gnumake.sh b/06/recipes/2a0-static-gnumake.sh new file mode 100755 index 0000000..94e43e7 --- /dev/null +++ b/06/recipes/2a0-static-gnumake.sh @@ -0,0 +1,54 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH dd16fb1d67bfab79a72f5e8390735c49e3e8e70b4945a15ab1f81ddb78658fb3 +#> FROM http://ftp.gnu.org/gnu/make/make-4.4.1.tar.gz + +set -uex + +export PATH=/store/1-stage1/tinycc/wrappers:/store/1-stage1/protobusybox/bin + +mkdir -p /store/2a0-static-gnumake /tmp/2a0-static-gnumake +cd /tmp/2a0-static-gnumake + +echo "### $0: unpacking static GNU Make sources..." +tar --strip-components=1 -xf /downloads/make-4.4.1.tar.gz + +echo "### $0: fixing up static GNU Make sources..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + src/job.c build-aux/install-sh po/Makefile.in.in +# this is part of stdlib, no idea how it's supposed to not clash +rm src/getopt.h +for f in src/getopt.c src/getopt1.c; do :> $f; done +for f in lib/fnmatch.c lib/glob.c lib/xmalloc.c lib/error.c; do :> $f; done +# embrace chaos +shuffle_comment='\/\* Handle shuffle mode argument. \*\/' +shuffle_default='if (!shuffle_mode) shuffle_mode = xstrdup(\"random\");' +sed -i "s|$shuffle_comment|$shuffle_comment\n$shuffle_default|" src/main.c +grep 'if (!shuffle_mode) shuffle_mode = xstrdup("random");' src/main.c + +echo "### $0: building static GNU Make..." +ash ./configure \ + --build x86_64-linux \ + --disable-dependency-tracking \ + --prefix=/store/2a0-static-gnumake \ + CONFIG_SHELL='/store/1-stage1/protobusybox/bin/ash' \ + SHELL='/store/1-stage1/protobusybox/bin/ash' +ash ./build.sh + +echo "### $0: testing static GNU Make by remaking it with itself..." +mv make make-intermediate +./make-intermediate -j $NPROC clean +./make-intermediate -j $NPROC CFLAGS=-O2 + +echo "### $0: installing static GNU Make..." +./make -j $NPROC install + +echo "### $0: creating a wrapper that respects \$SHELL..." +# FIXME: patch make to use getenv? +mkdir /store/2a0-static-gnumake/wrappers; cd /store/2a0-static-gnumake/wrappers +echo "#!/store/1-stage1/protobusybox/bin/ash" > make +echo "exec /store/2a0-static-gnumake/bin/make SHELL=\$SHELL \"\$@\"" \ >> make +chmod +x make + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a0 /store/2a0-static-gnumake ) diff --git a/06/recipes/2a1-static-binutils.sh b/06/recipes/2a1-static-binutils.sh new file mode 100755 index 0000000..d5a6e85 --- /dev/null +++ b/06/recipes/2a1-static-binutils.sh @@ -0,0 +1,47 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH 645c25f563b8adc0a81dbd6a41cffbf4d37083a382e02d5d3df4f65c09516d00 +#> FROM https://ftp.gnu.org/gnu/binutils/binutils-2.39.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/1-stage1/tinycc/wrappers" +export PATH="$PATH:/store/2a0-static-gnumake/bin" + +mkdir -p /tmp/2a1-static-binutils; cd /tmp/2a1-static-binutils +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking binutils sources..." +tar --strip-components=1 -xf /downloads/binutils-2.39.tar.xz + +echo "### $0: building static binutils..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + missing install-sh mkinstalldirs +mkdir aliases +ln -s /store/1-stage1/protobusybox/bin/true aliases/makeinfo +PATH="/tmp/2a1-static-binutils/aliases:$PATH" +export lt_cv_sys_max_cmd_len=32768 +# see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 +sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh + +ash configure \ + CONFIG_SHELL=/store/1-stage1/protobusybox/bin/ash \ + SHELL=/store/1-stage1/protobusybox/bin/ash \ + CFLAGS='-O2 -D__LITTLE_ENDIAN__=1' \ + CFLAGS_FOR_TARGET=-O2 \ + MAKEINFO=/store/1-stage1/protobusybox/bin/true \ + --disable-gprofng \ + --enable-deterministic-archives \ + --host x86_64-linux --build x86_64-linux \ + --prefix=/store/2a1-static-binutils +make -j $NPROC all-libiberty all-gas all-bfd all-libctf all-zlib all-gprof +make all-ld # race condition on ld/.deps/ldwrite.Po, serialize +make -j $NPROC + +echo "### $0: installing static binutils..." +make -j $NPROC install +rm /store/2a1-static-binutils/lib/*.la # broken, reference builddir + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a1 /store/2a1-static-binutils ) diff --git a/06/recipes/2a2-static-gnugcc4-c.sh b/06/recipes/2a2-static-gnugcc4-c.sh new file mode 100755 index 0000000..3ca528f --- /dev/null +++ b/06/recipes/2a2-static-gnugcc4-c.sh @@ -0,0 +1,71 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH f69eff1bc3d15d4e59011d587c57462a8d3d32cf2378d32d30d008a42a863325 +#> FROM https://gmplib.org/download/gmp/archive/gmp-4.3.2.tar.xz + +#> FETCH d7271bbfbc9ddf387d3919df8318cd7192c67b232919bfa1cb3202d07843da1b +#> FROM https://www.mpfr.org/mpfr-2.4.2/mpfr-2.4.2.tar.xz + +#> FETCH e664603757251fd8a352848276497a4c79b7f8b21fd8aedd5cc0598a38fee3e4 +#> FROM http://www.multiprecision.org/downloads/mpc-0.8.1.tar.gz + +#> FETCH 92e61c6dc3a0a449e62d72a38185fda550168a86702dea07125ebd3ec3996282 +#> FROM https://ftp.gnu.org/gnu/gcc/gcc-4.7.4/gcc-4.7.4.tar.bz2 + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/1-stage1/tinycc/wrappers" +export PATH="$PATH:/store/2a0-static-gnumake/bin" + +mkdir -p /tmp/2a2-static-gnugcc4-c; cd /tmp/2a2-static-gnugcc4-c +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/1-stage1/protobusybox/bin/ash aliases/sh +export PATH="/tmp/2a2-static-gnugcc4-c/aliases:$PATH" + +echo "### $0: unpacking GNU GCC sources..." +mkdir mpfr mpc gmp +tar --strip-components=1 -xf /downloads/gcc-4.7.4.tar.bz2 +tar --strip-components=1 -xf /downloads/mpfr-2.4.2.tar.xz -C mpfr +tar --strip-components=1 -xf /downloads/mpc-0.8.1.tar.gz -C mpc +tar --strip-components=1 -xf /downloads/gmp-4.3.2.tar.xz -C gmp + +echo "### $0: building static GNU GCC 4 (statically linked, C only)" +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + missing move-if-change mkdep mkinstalldirs symlink-tree \ + gcc/genmultilib */*.sh gcc/exec-tool.in \ + install-sh */install-sh +sed -i 's|^\(\s*\)sh |\1/store/1-stage1/protobusybox/bin/ash |' \ + Makefile* */Makefile* +sed -i 's|LIBGCC2_DEBUG_CFLAGS = -g|LIBGCC2_DEBUG_CFLAGS = |' \ + libgcc/Makefile.in +# see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 +sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh */ltmain.sh +ash configure \ + CONFIG_SHELL='/store/1-stage1/protobusybox/bin/ash' \ + SHELL='/store/1-stage1/protobusybox/bin/ash' \ + CFLAGS=-O2 CFLAGS_FOR_TARGET=-O2 \ + --with-sysroot=/store/1-stage1/protomusl \ + --with-native-system-header-dir=/include \ + --with-build-time-tools=/store/2a1-static-binutils/bin \ + --prefix=/store/2a2-static-gnugcc4-c \ + --enable-languages=c \ + --disable-bootstrap \ + --disable-libquadmath --disable-decimal-float --disable-fixed-point \ + --disable-lto \ + --disable-libgomp \ + --disable-multilib \ + --disable-multiarch \ + --disable-libmudflap \ + --disable-libssp \ + --disable-nls \ + --host x86_64-linux --build x86_64-linux +make -j $NPROC + +echo "### $0: installing static GNU GCC 4 (statically linked, C only)" +make -j $NPROC install + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a2 /store/2a2-static-gnugcc4-c ) diff --git a/06/recipes/2a3-intermediate-musl.sh b/06/recipes/2a3-intermediate-musl.sh new file mode 100755 index 0000000..afe7d73 --- /dev/null +++ b/06/recipes/2a3-intermediate-musl.sh @@ -0,0 +1,36 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH 7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039 +#> FROM http://musl.libc.org/releases/musl-1.2.4.tar.gz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a2-static-gnugcc4-c/bin" + +mkdir -p /tmp/2a3-intermediate-musl; cd /tmp/2a3-intermediate-musl +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking musl sources..." +tar --strip-components=1 -xf /downloads/musl-1.2.4.tar.gz + +echo "### $0: building musl with GNU GCC..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + tools/*.sh \ +# patch popen/system to search in PATH instead of hardcoding /bin/sh +sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \ + src/stdio/popen.c src/process/system.c +sed -i 's|execl("/bin/sh", "sh", "-c",|execlp("sh", "-c",|'\ + src/misc/wordexp.c +# eliminiate a source path reference +sed -i 's/__FILE__/"__FILE__"/' include/assert.h +ash ./configure --prefix=/store/2a3-intermediate-musl +make -j $NPROC + +echo "### $0: installing musl..." +make -j $NPROC install + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a3 /store/2a3-intermediate-musl ) diff --git a/06/recipes/2a4-gnugcc4-cpp.sh b/06/recipes/2a4-gnugcc4-cpp.sh new file mode 100755 index 0000000..d30a974 --- /dev/null +++ b/06/recipes/2a4-gnugcc4-cpp.sh @@ -0,0 +1,97 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH f69eff1bc3d15d4e59011d587c57462a8d3d32cf2378d32d30d008a42a863325 +#> FROM https://gmplib.org/download/gmp/archive/gmp-4.3.2.tar.xz + +#> FETCH d7271bbfbc9ddf387d3919df8318cd7192c67b232919bfa1cb3202d07843da1b +#> FROM https://www.mpfr.org/mpfr-2.4.2/mpfr-2.4.2.tar.xz + +#> FETCH e664603757251fd8a352848276497a4c79b7f8b21fd8aedd5cc0598a38fee3e4 +#> FROM http://www.multiprecision.org/downloads/mpc-0.8.1.tar.gz + +#> FETCH 92e61c6dc3a0a449e62d72a38185fda550168a86702dea07125ebd3ec3996282 +#> FROM https://ftp.gnu.org/gnu/gcc/gcc-4.7.4/gcc-4.7.4.tar.bz2 + +set -uex + +export PATH='/store/_2a0-ccache/wrappers/c' # may or may not exist +export PATH="$PATH:/store/1-stage1/protobusybox/bin" +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a2-static-gnugcc4-c/bin" + +mkdir -p /tmp/2a4-gnugcc4-cpp; cd /tmp/2a4-gnugcc4-cpp +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/1-stage1/protobusybox/bin/ash aliases/sh +export PATH="/tmp/2a4-gnugcc4-cpp/aliases:$PATH" + +echo "### $0: creating wrappers that make previous GNU GCC target new musl..." +SYSROOT=/store/2a3-intermediate-musl +export _SYSROOT="--sysroot $SYSROOT" +export _LDFLAG="--dynamic-linker=$SYSROOT/lib/libc.so" +export _NEWINC="-I$SYSROOT/include" +export _REALCC="-I$SYSROOT/include" +mkdir wrappers +echo '#!/store/1-stage1/protobusybox/bin/ash' > wrappers/cc +echo '#!/store/1-stage1/protobusybox/bin/ash' > wrappers/cpp +echo '#!/store/1-stage1/protobusybox/bin/ash' > wrappers/ld +echo 'exec gcc $_SYSROOT -Wl,$_LDFLAG "$@"' >> wrappers/cc +echo 'exec /store/2a2-static-gnugcc4-c/bin/cpp $_NEWINC "$@"' >> wrappers/cpp +echo 'exec /store/2a1-static-binutils/bin/ld $_LDFLAG "$@"' >> wrappers/ld +chmod +x wrappers/cc wrappers/cpp wrappers/ld +export PATH="/tmp/2a4-gnugcc4-cpp/wrappers:$PATH" + +echo "### $0: unpacking GNU GCC 4 sources..." +mkdir mpfr mpc gmp +tar --strip-components=1 -xf /downloads/gcc-4.7.4.tar.bz2 +tar --strip-components=1 -xf /downloads/mpfr-2.4.2.tar.xz -C mpfr +tar --strip-components=1 -xf /downloads/mpc-0.8.1.tar.gz -C mpc +tar --strip-components=1 -xf /downloads/gmp-4.3.2.tar.xz -C gmp + +echo "### $0: fixing up GNU GCC 4 sources..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + missing move-if-change mkdep mkinstalldirs symlink-tree \ + gcc/genmultilib */*.sh gcc/exec-tool.in \ + install-sh */install-sh +sed -i 's|^\(\s*\)sh |\1/store/1-stage1/protobusybox/bin/ash |' \ + Makefile* */Makefile* +sed -i 's|LIBGCC2_DEBUG_CFLAGS = -g|LIBGCC2_DEBUG_CFLAGS = |' \ + libgcc/Makefile.in +sed -i "s|/lib64/ld-linux-x86-64.so.2|$SYSROOT/lib/libc.so|" \ + gcc/config/i386/linux64.h +sed -i 's|"os/gnu-linux"|"os/generic"|' libstdc++-v3/configure.host +# see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 +sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh */ltmain.sh + +echo "### $0: building GNU GCC 4 (dynamically linked, with C++ support)" +ash configure \ + cache_file=nonex \ + CONFIG_SHELL='/store/1-stage1/protobusybox/bin/ash' \ + SHELL='/store/1-stage1/protobusybox/bin/ash' \ + CC=cc CPP=cpp LD=ld \ + CFLAGS=-O2 CFLAGS_FOR_TARGET=-O2 \ + --with-sysroot=$SYSROOT \ + --with-native-system-header-dir=/include \ + --with-build-time-tools=/store/2a1-static-binutils/bin \ + --prefix=/store/2a4-gnugcc4-cpp \ + --with-specs='%{!static:%x{-rpath=/store/2a4-gnugcc4-cpp/lib64}}' \ + --enable-languages=c,c++ \ + --disable-bootstrap \ + --disable-libquadmath --disable-decimal-float --disable-fixed-point \ + --disable-lto \ + --disable-libgomp \ + --disable-multilib \ + --disable-multiarch \ + --disable-libmudflap \ + --disable-libssp \ + --disable-nls \ + --disable-libitm \ + --host x86_64-linux --build x86_64-linux +make -j $NPROC +echo "### $0: installing GNU GCC 4 (dynamically linked, with C++ support)" +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a4 /store/2a4-gnugcc4-cpp ) diff --git a/06/recipes/2a5-gnugcc10.sh b/06/recipes/2a5-gnugcc10.sh new file mode 100755 index 0000000..cbc4b6a --- /dev/null +++ b/06/recipes/2a5-gnugcc10.sh @@ -0,0 +1,90 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH 68dadacce515b0f8a54f510edf07c1b636492bcdb8e8d54c56eb216225d16989 +#> FROM https://gmplib.org/download/gmp/gmp-6.1.0.tar.xz + +#> FETCH 761413b16d749c53e2bfd2b1dfaa3b027b0e793e404b90b5fbaeef60af6517f5 +#> FROM https://www.mpfr.org/mpfr-3.1.4/mpfr-3.1.4.tar.xz + +#> FETCH 617decc6ea09889fb08ede330917a00b16809b8db88c29c31bfbb49cbf88ecc3 +#> FROM http://www.multiprecision.org/downloads/mpc-1.0.3.tar.gz + +#> FETCH 6b8b0fd7f81d0a957beb3679c81bbb34ccc7568d5682844d8924424a0dadcb1b +#> FROM http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 + +#> FETCH 25109543fdf46f397c347b5d8b7a2c7e5694a5a51cce4b9c6e1ea8a71ca307c1 +#> FROM https://ftp.gnu.org/gnu/gcc/gcc-10.5.0/gcc-10.5.0.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a4-gnugcc4-cpp/bin" + +mkdir -p /tmp/2a5-gnugcc10; cd /tmp/2a5-gnugcc10 +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/1-stage1/protobusybox/bin/ash aliases/sh +export PATH="/tmp/2a5-gnugcc10/aliases:$PATH" + +SYSROOT=/store/2a3-intermediate-musl + +echo "### $0: unpacking GNU GCC 10 sources..." +mkdir gmp mpfr mpc isl +tar --strip-components=1 -xf /downloads/gcc-10.5.0.tar.xz +tar --strip-components=1 -xf /downloads/gmp-6.1.0.tar.xz -C gmp +tar --strip-components=1 -xf /downloads/mpfr-3.1.4.tar.xz -C mpfr +tar --strip-components=1 -xf /downloads/mpc-1.0.3.tar.gz -C mpc +tar --strip-components=1 -xf /downloads/isl-0.18.tar.bz2 -C isl + +echo "### $0: fixing up GNU GCC 10 sources..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + missing move-if-change mkdep mkinstalldirs symlink-tree install-sh \ + gcc/exec-tool.in libgcc/mkheader.sh +sed -i 's|^\(\s*\)sh |\1/store/1-stage1/protobusybox/bin/ash |' \ + libgcc/Makefile.in +sed -i 's|LIBGCC2_DEBUG_CFLAGS = -g|LIBGCC2_DEBUG_CFLAGS = |' \ + libgcc/Makefile.in +sed -i "s|/lib/ld-musl-x86_64.so.1|$SYSROOT/lib/libc.so|" \ + gcc/config/i386/linux64.h +sed -i 's|m64=../lib64|m64=../lib|' gcc/config/i386/t-linux64 +sed -i 's|"os/gnu-linux"|"os/generic"|' libstdc++-v3/configure.host +# see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 +sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh */ltmain.sh + +echo "### $0: building GNU GCC 10" +ash configure \ + CONFIG_SHELL='/store/1-stage1/protobusybox/bin/ash' \ + SHELL='/store/1-stage1/protobusybox/bin/ash' \ + CFLAGS=-O2 CXX_FLAGS=-O2 \ + CFLAGS_FOR_TARGET=-O2 CXXFLAGS_FOR_TARGET=-O2 \ + --with-sysroot=$SYSROOT \ + --with-native-system-header-dir=/include \ + --with-build-time-tools=/store/2a1-static-binutils/bin \ + --prefix=/store/2a5-gnugcc10 \ + --with-specs='%{!static:%x{-rpath=/store/2a5-gnugcc10/lib}}' \ + --enable-languages=c,c++ \ + --disable-bootstrap \ + --disable-libquadmath --disable-decimal-float --disable-fixed-point \ + --disable-lto \ + --disable-libgomp \ + --disable-multilib \ + --disable-multiarch \ + --disable-libmudflap \ + --disable-libssp \ + --disable-nls \ + --disable-libitm \ + --disable-libsanitizer \ + --disable-cet \ + --disable-gnu-unique-object \ + --disable-gcov \ + --disable-checking \ + --host x86_64-linux-musl --build x86_64-linux-musl +make -j $NPROC +echo "### $0: installing GNU GCC 10" +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a5 /store/2a5-gnugcc10 ) diff --git a/06/recipes/2a6-linux-headers.sh b/06/recipes/2a6-linux-headers.sh new file mode 100755 index 0000000..c074bd9 --- /dev/null +++ b/06/recipes/2a6-linux-headers.sh @@ -0,0 +1,36 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH cca91be956fe081f8f6da72034cded96fe35a50be4bfb7e103e354aa2159a674 +#> FROM https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.12.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a5-gnugcc10/bin" + +mkdir -p /tmp/2a6-linux-headers; cd /tmp/2a6-linux-headers +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking Linux sources..." +tar --strip-components=1 -xf /downloads/linux-6.4.12.tar.xz \ + linux-6.4.12/Makefile \ + linux-6.4.12/arch/x86 \ + linux-6.4.12/include \ + linux-6.4.12/scripts \ + linux-6.4.12/tools + +echo "### $0: building Linux headers..." +make -j $NPROC \ + CONFIG_SHELL=/store/1-stage1/protobusybox/bin/ash \ + CC=gcc HOSTCC=gcc ARCH=x86_64 \ + headers + +echo "### $0: installing Linux headers..." +mkdir -p /store/2a6-linux-headers/ +find usr/include -name '.*' | xargs rm +cp -rv usr/include /store/2a6-linux-headers/ + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a6 /store/2a6-linux-headers ) diff --git a/06/recipes/2a7-cmake.sh b/06/recipes/2a7-cmake.sh new file mode 100755 index 0000000..f99d3b8 --- /dev/null +++ b/06/recipes/2a7-cmake.sh @@ -0,0 +1,48 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH 0a905ca8635ca81aa152e123bdde7e54cbe764fdd9a70d62af44cad8b92967af +#> FROM https://github.com/Kitware/CMake/releases/download/v3.27.4/cmake-3.27.4.tar.gz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin/' +export PATH="$PATH:/store/2a0-static-gnumake/wrappers" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a5-gnugcc10/bin" + +export SHELL=/store/1-stage1/protobusybox/bin/ash + +mkdir -p /tmp/2a7-cmake; cd /tmp/2a7-cmake +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking CMake sources..." +tar --strip-components=1 -xf /downloads/cmake-3.27.4.tar.gz + +echo "### $0: fixing up CMake sources..." +sed -i "s|/bin/sh|$SHELL|" bootstrap +sed -i 's|__FILE__|"__FILE__"|' \ + Source/CPack/IFW/cmCPackIFWCommon.h \ + Source/CPack/cmCPack*.h \ + Source/cmCTest.h + +echo "### $0: bundling libraries..." +# poor man's static linking, a way for cmake to be self-contained later +mkdir -p /store/2a7-cmake/bundled-runtime +cp -H /store/2a5-gnugcc10/lib/libstdc++.so.6 /store/2a7-cmake/bundled-runtime/ +cp -H /store/2a5-gnugcc10/lib/libgcc_s.so.1 /store/2a7-cmake/bundled-runtime/ + +echo "### $0: building CMake..." +ash configure \ + CFLAGS="-DCPU_SETSIZE=128 -D_GNU_SOURCE" \ + CXXFLAGS="-isystem /store/2a6-linux-headers/include" \ + LDFLAGS="-Wl,-rpath /store/2a7-cmake/bundled-runtime" \ + --prefix=/store/2a7-cmake \ + --parallel=$NPROC \ + -- \ + -DCMAKE_USE_OPENSSL=OFF +make -j $NPROC +echo "### $0: installing CMake..." +make -j $NPROC install/strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a7 /store/2a7-cmake ) diff --git a/06/recipes/2a8-python.sh b/06/recipes/2a8-python.sh new file mode 100755 index 0000000..fe15f7e --- /dev/null +++ b/06/recipes/2a8-python.sh @@ -0,0 +1,70 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH 795c34f44df45a0e9b9710c8c71c15c671871524cd412ca14def212e8ccb155d +#> FROM https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin/' +export PATH="$PATH:/store/2a0-static-gnumake/wrappers" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a5-gnugcc10/bin" + +export SHELL=/store/1-stage1/protobusybox/bin/ash + +mkdir -p /tmp/2a8-python; cd /tmp/2a8-python +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/1-stage1/protobusybox/bin/ash aliases/sh +export PATH="/tmp/2a8-python/aliases:$PATH" + +echo "### $0: unpacking CPython sources..." +tar --strip-components=1 -xf /downloads/Python-3.12.0.tar.xz + +echo "### $0: fixing up CPython sources..." +sed -i "s|/bin/sh|$SHELL|" configure install-sh +sed -i 's|ac_sys_system=`uname -s`|ac_sys_system=Linux|' configure +# the precompiled pyc files aren't reproducible, +# but it's not like I need to waste time on them anyway. +# break their generation +mv Lib/compileall.py Lib/compileall.py.bak +echo 'import sys; sys.exit(0)' > Lib/compileall.py; chmod +x Lib/compileall.py +sed -i 's|__FILE__|"__FILE__"|' \ + Python/errors.c \ + Include/pyerrors.h \ + Include/cpython/object.h \ + Modules/pyexpat.c +sed -i 's|TIME __TIME__|TIME "xx:xx:xx"|' Modules/getbuildinfo.c +sed -i 's|DATE __DATE__|DATE "xx/xx/xx"|' Modules/getbuildinfo.c +# different build path length leads to different wrapping. avoid +sed -i 's|vars, stream=f|vars, stream=f, width=2**24|' Lib/sysconfig.py + +echo "### $0: building CPython..." +mkdir -p /store/2a8-python/lib +ash configure \ + ac_cv_broken_sem_getvalue=yes \ + ac_cv_posix_semaphores_enabled=no \ + OPT='-DNDEBUG -fwrapv -O3 -Wall' \ + LDFLAGS='-Wl,-rpath /store/2a8-python/lib' \ + --without-static-libpython \ + --build x86_64-linux-musl \ + --prefix=/store/2a8-python \ + --enable-shared \ + --with-ensurepip=no +# ensure reproducibility in case of no /dev/shm +grep 'define POSIX_SEMAPHORES_NOT_ENABLED 1' pyconfig.h +grep 'define HAVE_BROKEN_SEM_GETVALUE 1' pyconfig.h +make -j $NPROC + +echo "### $0: installing CPython..." +make -j $NPROC install +# strip builddir mentions +sed -i "s|/tmp/2a8-python|...|g" \ + /store/2a8-python/lib/python3.*/_sysconfigdata__*.py \ + /store/2a8-python/lib/python3.*/config-3.*-x86_64-linux-musl/Makefile +# restore compileall just in case +cat Lib/compileall.py.bak > /store/2a8-python/lib/python3.12/compileall.py + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a8 /store/2a8-python ) diff --git a/06/recipes/2a9-intermediate-clang.sh b/06/recipes/2a9-intermediate-clang.sh new file mode 100755 index 0000000..c036504 --- /dev/null +++ b/06/recipes/2a9-intermediate-clang.sh @@ -0,0 +1,142 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH b0e42aafc01ece2ca2b42e3526f54bebc4b1f1dc8de6e34f46a0446a13e882b9 +#> FROM https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.1/llvm-project-17.0.1.src.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin/' +export PATH="$PATH:/store/2a0-static-gnumake/wrappers" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a5-gnugcc10/bin" +export PATH="$PATH:/store/2a7-cmake/bin" +export PATH="$PATH:/store/2a8-python/bin" + +export SHELL=/store/1-stage1/protobusybox/bin/ash +GCC_PATH=/store/2a5-gnugcc10 + +mkdir -p /tmp/2a9-intermediate-clang; cd /tmp/2a9-intermediate-clang +# clang's cmake configuration should pick up ccache automatically from PATH +#if [ -e /ccache/setup ]; then . /ccache/setup; fi +export PATH="$PATH:/ccache/bin" +command -v ccache && USE_CCACHE=YES || USE_CCACHE=NO + +echo "### $0: preparing future sysroot..." +OUT=/store/2a9-intermediate-clang +SYSROOT=$OUT/sysroot +mkdir -p $SYSROOT/lib $SYSROOT/include +ln -s /store/2a3-intermediate-musl/lib/* $SYSROOT/lib/ +ln -s /store/2a3-intermediate-musl/include/* $SYSROOT/include/ + +echo "### $0: unpacking LLVM/Clang sources..." +tar --strip-components=1 -xf /downloads/llvm-project-17.0.1.src.tar.xz + +echo "### $0: fixing up LLVM/Clang sources..." +sed -i "s|COMMAND sh|COMMAND $SHELL|" \ + llvm/cmake/modules/GetHostTriple.cmake clang/CMakeLists.txt +echo 'echo x86_64-unknown-linux-musl' > llvm/cmake/config.guess +LOADER=/store/2a3-intermediate-musl/lib/libc.so +sed -i "s|/lib/ld-musl-\" + ArchName + \".so.1|$LOADER|" \ + clang/lib/Driver/ToolChains/Linux.cpp +BEGINEND='const bool HasCRTBeginEndFiles' +sed -i "s|${BEGINEND} =|${BEGINEND} = false; ${BEGINEND}_unused =|" \ + clang/lib/Driver/ToolChains/Gnu.cpp +REL_ORIGIN='_install_rpath \"\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}\"' +sed -i "s|_install_rpath \"\\\\\$ORIGIN/..|_install_rpath \"$OUT|" \ + llvm/cmake/modules/AddLLVM.cmake +sed -i 's|numShards = 32;|numShards = 1;|' lld/*/SyntheticSections.* +sed -i 's|numShards = 256;|numShards = 1;|' lld/*/ICF.cpp +sed -i 's|__FILE__|"__FILE__"|' compiler-rt/lib/builtins/int_util.h +sed -i 's|"@LLVM_SRC_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in +sed -i 's|"@LLVM_OBJ_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in + +echo "### $0: building LLVM/Clang (stage 1)..." +export LD_LIBRARY_PATH='/store/2a5-gnugcc10/lib' + +EXTRA_INCL='/tmp/2a9-intermediate-clang/extra_includes' +mkdir -p $EXTRA_INCL +cp clang/lib/Headers/*intrin*.h $EXTRA_INCL/ +cp clang/lib/Headers/mm_malloc.h $EXTRA_INCL/ +[ -e $EXTRA_INCL/immintrin.h ] + +BOTH_STAGES_OPTS='' +add_opt() { + BOTH_STAGES_OPTS="$BOTH_STAGES_OPTS -D$1 -DBOOTSTRAP_$1" +} +add_opt CMAKE_BUILD_TYPE=MinSizeRel +add_opt LLVM_OPTIMIZED_TABLEGEN=YES +add_opt LLVM_CCACHE_BUILD=$USE_CCACHE +add_opt DEFAULT_SYSROOT=$SYSROOT +add_opt CMAKE_INSTALL_PREFIX=$OUT +add_opt LLVM_INSTALL_BINUTILS_SYMLINKS=YES +add_opt LLVM_INSTALL_CCTOOLS_SYMLINKS=YES +add_opt CMAKE_INSTALL_DO_STRIP=YES +add_opt LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=YES +add_opt LLVM_TARGET_ARCH=X86 +add_opt LLVM_TARGETS_TO_BUILD=Native +add_opt LLVM_BUILTIN_TARGETS=x86_64-unknown-linux-musl +add_opt LLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl +add_opt LLVM_HOST_TRIPLE=x86_64-unknown-linux-musl +add_opt COMPILER_RT_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl +add_opt LLVM_APPEND_VC_REV=NO +add_opt LLVM_INCLUDE_TESTS=NO +add_opt LLVM_INCLUDE_EXAMPLES=NO +add_opt LLVM_INCLUDE_BENCHMARKS=NO +add_opt LLVM_ENABLE_BACKTRACES=NO +add_opt LLVM_ENABLE_EH=YES +add_opt LLVM_ENABLE_RTTI=YES +add_opt CLANG_ENABLE_ARCMT=NO +add_opt CLANG_ENABLE_STATIC_ANALYZER=NO +add_opt COMPILER_RT_BUILD_SANITIZERS=NO +add_opt COMPILER_RT_BUILD_XRAY=NO +add_opt COMPILER_RT_BUILD_LIBFUZZER=NO +add_opt COMPILER_RT_BUILD_PROFILE=NO +add_opt COMPILER_RT_BUILD_MEMPROF=NO +add_opt COMPILER_RT_BUILD_ORC=NO +add_opt COMPILER_RT_USE_BUILTINS_LIBRARY=YES +add_opt CLANG_DEFAULT_CXX_STDLIB=libc++ +add_opt CLANG_DEFAULT_LINKER=lld +add_opt CLANG_DEFAULT_RTLIB=compiler-rt +add_opt LIBCXX_HAS_MUSL_LIBC=YES +add_opt LIBCXX_USE_COMPILER_RT=YES +add_opt LIBCXX_INCLUDE_BENCHMARKS=NO +add_opt LIBCXX_CXX_ABI=libcxxabi +add_opt LIBCXX_ADDITIONAL_COMPILE_FLAGS=-I/store/2a6-linux-headers/include +add_opt LIBCXXABI_USE_COMPILER_RT=YES +add_opt LIBCXXABI_USE_LLVM_UNWINDER=YES +add_opt LLVM_INSTALL_TOOLCHAIN_ONLY=YES +add_opt LIBUNWIND_USE_COMPILER_RT=YES +add_opt LLVM_ENABLE_THREADS=NO + +cmake -S llvm -B build -G 'Unix Makefiles' \ + -DLLVM_ENABLE_PROJECTS='clang;lld' \ + -DLLVM_ENABLE_RUNTIMES='compiler-rt;libcxx;libcxxabi;libunwind' \ + -DGCC_INSTALL_PREFIX=$GCC_PATH \ + -DCMAKE_C_FLAGS=--sysroot=$SYSROOT \ + "-DBOOTSTRAP_CMAKE_C_FLAGS=-isystem $EXTRA_INCL" \ + "-DBOOTSTRAP_CMAKE_CXX_FLAGS=-isystem $EXTRA_INCL" \ + -DCLANG_ENABLE_BOOTSTRAP=YES $BOTH_STAGES_OPTS + +make -C build -j $NPROC clang lld runtimes + +echo "### $0: building LLVM/Clang (stage 2)..." +NEW_LIB_DIR="$(pwd)/build/lib/x86_64-unknown-linux-musl" +export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$NEW_LIB_DIR" +make -C build -j $NPROC stage2 + +echo "### $0: installing LLVM/Clang..." +make -C build -j $NPROC stage2-install +ln -s $OUT/lib/x86_64-unknown-linux-musl/* $OUT/lib/ + +echo "### $0: setting up generic names..." +mkdir $OUT/bin/generic-names +ln -s $OUT/bin/clang $OUT/bin/generic-names/cc +ln -s $OUT/bin/clang++ $OUT/bin/generic-names/c++ +ln -s $OUT/bin/clang-cpp $OUT/bin/generic-names/cpp + +echo "### $0: mixing new stuff into sysroot..." +ln -s $OUT/lib/* $OUT/sysroot/lib/ + +echo "### $0: NOT checking for build path leaks - see _2a9.test.sh" diff --git a/06/recipes/2b0-musl.sh b/06/recipes/2b0-musl.sh new file mode 100755 index 0000000..52faa6a --- /dev/null +++ b/06/recipes/2b0-musl.sh @@ -0,0 +1,36 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH 7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039 +#> FROM http://musl.libc.org/releases/musl-1.2.4.tar.gz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a9-intermediate-clang/bin/generic-names" + +mkdir -p /tmp/2b0-musl; cd /tmp/2b0-musl +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking musl sources..." +tar --strip-components=1 -xf /downloads/musl-1.2.4.tar.gz + +echo "### $0: building musl..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' \ + tools/*.sh \ +# patch popen/system to search in PATH instead of hardcoding /bin/sh +sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \ + src/stdio/popen.c src/process/system.c +# avoid absolute path references +sed -i 's/__FILE__/__FILE_NAME__/' include/assert.h +ash ./configure --prefix=/store/2b0-musl CFLAGS=-O2 +make -j $NPROC + +echo "### $0: installing musl..." +make -j $NPROC install +mkdir /store/2b0-musl/bin +ln -s /store/2b0-musl/lib/libc.so /store/2b0-musl/bin/ldd + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2b0 /store/2b0-musl ) diff --git a/06/recipes/2b1-clang.sh b/06/recipes/2b1-clang.sh new file mode 100755 index 0000000..6c05336 --- /dev/null +++ b/06/recipes/2b1-clang.sh @@ -0,0 +1,153 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH b0e42aafc01ece2ca2b42e3526f54bebc4b1f1dc8de6e34f46a0446a13e882b9 +#> FROM https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.1/llvm-project-17.0.1.src.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin/' +export PATH="$PATH:/store/2a0-static-gnumake/wrappers" +export PATH="$PATH:/store/2a7-cmake/bin" +export PATH="$PATH:/store/2a8-python/bin" +# 2a9-intermediate-clang intentionally not added to $PATH to prevent confusion + +export SHELL=/store/1-stage1/protobusybox/bin/ash +PREV_CLANG=/store/2a9-intermediate-clang + +mkdir -p /tmp/2b1-clang; cd /tmp/2b1-clang +# clang's cmake configuration should pick up ccache automatically from PATH +#if [ -e /ccache/setup ]; then . /ccache/setup; fi +export PATH="$PATH:/ccache/bin" +command -v ccache && USE_CCACHE=YES || USE_CCACHE=NO + +echo "### $0: preparing future sysroot..." +OUT=/store/2b1-clang +SYSROOT=$OUT/sysroot +mkdir -p $SYSROOT/lib $SYSROOT/include +ln -s /store/2b0-musl/lib/* $SYSROOT/lib/ +ln -s /store/2b0-musl/include/* $SYSROOT/include/ + +echo "### $0: unpacking LLVM/Clang sources..." +tar --strip-components=1 -xf /downloads/llvm-project-17.0.1.src.tar.xz + +echo "### $0: fixing up LLVM/Clang sources..." +sed -i "s|COMMAND sh|COMMAND $SHELL|" \ + llvm/cmake/modules/GetHostTriple.cmake clang/CMakeLists.txt +echo 'echo x86_64-unknown-linux-musl' > llvm/cmake/config.guess +LOADER=/store/2b0-musl/lib/libc.so +sed -i "s|/lib/ld-musl-\" + ArchName + \".so.1|$LOADER|" \ + clang/lib/Driver/ToolChains/Linux.cpp +BEGINEND='const bool HasCRTBeginEndFiles' +sed -i "s|${BEGINEND} =|${BEGINEND} = false; ${BEGINEND}_unused =|" \ + clang/lib/Driver/ToolChains/Gnu.cpp +REL_ORIGIN='_install_rpath \"\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}\"' +sed -i "s|_install_rpath \"\\\\\$ORIGIN/..|_install_rpath \"$OUT|" \ + llvm/cmake/modules/AddLLVM.cmake +sed -i 's|numShards = 32;|numShards = 1;|' lld/*/SyntheticSections.* +sed -i 's|numShards = 256;|numShards = 1;|' lld/*/ICF.cpp +sed -i 's|__FILE__|__FILE_NAME__|' compiler-rt/lib/builtins/int_util.h +sed -i 's|"@LLVM_SRC_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in +sed -i 's|"@LLVM_OBJ_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in + +echo "### $0: building LLVM/Clang..." +export LD_LIBRARY_PATH="/store/2b0-musl/lib:$PREV_CLANG/lib" +export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/tmp/2b1-clang/build/lib" # libLLVM + +EXTRA_INCL='/tmp/2b1-clang/extra_includes' +mkdir -p $EXTRA_INCL +cp clang/lib/Headers/*intrin*.h $EXTRA_INCL/ +cp clang/lib/Headers/mm_malloc.h $EXTRA_INCL/ +[ -e $EXTRA_INCL/immintrin.h ] + +OPTS='' +add_opt() { + OPTS="$OPTS -D$1" +} +add_opt CMAKE_BUILD_TYPE=Release +add_opt LLVM_OPTIMIZED_TABLEGEN=YES +add_opt LLVM_CCACHE_BUILD=$USE_CCACHE +add_opt DEFAULT_SYSROOT=$SYSROOT +add_opt CMAKE_INSTALL_PREFIX=$OUT +add_opt LLVM_INSTALL_BINUTILS_SYMLINKS=YES +add_opt LLVM_INSTALL_CCTOOLS_SYMLINKS=YES +add_opt CMAKE_INSTALL_DO_STRIP=YES +add_opt LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=YES +add_opt LLVM_TARGET_ARCH=X86 +add_opt LLVM_TARGETS_TO_BUILD=Native +add_opt LLVM_BUILTIN_TARGETS=x86_64-unknown-linux-musl +add_opt LLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl +add_opt LLVM_HOST_TRIPLE=x86_64-unknown-linux-musl +add_opt COMPILER_RT_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl +add_opt LLVM_APPEND_VC_REV=NO +add_opt LLVM_INCLUDE_TESTS=NO +add_opt LLVM_INCLUDE_EXAMPLES=NO +add_opt LLVM_INCLUDE_BENCHMARKS=NO +add_opt LLVM_ENABLE_BACKTRACES=NO +add_opt LLVM_ENABLE_EH=YES +add_opt LLVM_ENABLE_RTTI=YES +add_opt CLANG_ENABLE_ARCMT=NO +add_opt CLANG_ENABLE_STATIC_ANALYZER=NO +add_opt COMPILER_RT_BUILD_SANITIZERS=NO +add_opt COMPILER_RT_BUILD_XRAY=NO +add_opt COMPILER_RT_BUILD_LIBFUZZER=NO +add_opt COMPILER_RT_BUILD_PROFILE=NO +add_opt COMPILER_RT_BUILD_MEMPROF=NO +add_opt COMPILER_RT_BUILD_ORC=NO +add_opt COMPILER_RT_USE_BUILTINS_LIBRARY=YES +add_opt CLANG_DEFAULT_CXX_STDLIB=libc++ +add_opt CLANG_DEFAULT_LINKER=lld +add_opt CLANG_DEFAULT_RTLIB=compiler-rt +add_opt LIBCXX_HAS_MUSL_LIBC=YES +add_opt LIBCXX_USE_COMPILER_RT=YES +add_opt LIBCXX_INCLUDE_BENCHMARKS=NO +add_opt LIBCXX_CXX_ABI=libcxxabi +add_opt LIBCXXABI_USE_COMPILER_RT=YES +add_opt LIBCXXABI_USE_LLVM_UNWINDER=YES +add_opt LIBCXX_ADDITIONAL_COMPILE_FLAGS=-I/store/2a6-linux-headers/include +add_opt LLVM_INSTALL_TOOLCHAIN_ONLY=YES +add_opt LIBUNWIND_USE_COMPILER_RT=YES +add_opt LLVM_ENABLE_THREADS=NO + +REWRITE="-ffile-prefix-map=$(pwd)=/builddir/" +CFLAGS="--sysroot=$SYSROOT -I$EXTRA_INCL $REWRITE" +LDFLAGS="-Wl,--dynamic-linker=$LOADER" +cmake -S llvm -B build -G 'Unix Makefiles' \ + -DCMAKE_ASM_COMPILER=$PREV_CLANG/bin/clang \ + -DCMAKE_C_COMPILER=$PREV_CLANG/bin/clang \ + -DCMAKE_CXX_COMPILER=$PREV_CLANG/bin/clang++ \ + -DLLVM_ENABLE_PROJECTS='clang;lld' \ + -DLLVM_ENABLE_RUNTIMES='compiler-rt;libcxx;libcxxabi;libunwind' \ + -DCMAKE_C_FLAGS="$CFLAGS" \ + -DCMAKE_CXX_FLAGS="$CFLAGS" \ + -DCMAKE_C_LINK_FLAGS="$LDFLAGS" \ + -DCMAKE_CXX_LINK_FLAGS="$LDFLAGS" \ + -DLLVM_BUILD_LLVM_DYLIB=YES \ + -DLLVM_LINK_LLVM_DYLIB=YES \ + -DCLANG_LINK_LLVM_DYLIB=YES \ + $OPTS + +make -C build -j $NPROC clang lld runtimes + +echo "### $0: installing LLVM/Clang..." +make -C build -j $NPROC install/strip +ln -s $OUT/lib/x86_64-unknown-linux-musl/* $OUT/lib/ + +echo "### $0: setting up generic names..." +ln -s $OUT/bin/clang $OUT/bin/cc +ln -s $OUT/bin/clang++ $OUT/bin/c++ +ln -s $OUT/bin/clang-cpp $OUT/bin/cpp +ln -s $OUT/bin/lld $OUT/bin/ld + +echo "### $0: HACK making linux target work..." +# FIXME boost wants it at lib/clang/17/lib/linux/libclang_rt.builtins-x86_64.a +OUTLIB=$OUT/lib/clang/17/lib +ln -s $OUTLIB/x86_64-unknown-linux-musl $OUTLIB/linux +ln -s $OUTLIB/x86_64-unknown-linux-musl/libclang_rt.builtins.a \ + $OUTLIB/x86_64-unknown-linux-musl/libclang_rt.builtins-x86_64.a + +echo "### $0: mixing new stuff into sysroot..." +ln -s $OUT/lib/* $OUT/sysroot/lib/ + +echo "### $0: NOT checking for build path leaks - see _2b1.test.sh" diff --git a/06/recipes/2b2-busybox.sh b/06/recipes/2b2-busybox.sh new file mode 100755 index 0000000..4b48394 --- /dev/null +++ b/06/recipes/2b2-busybox.sh @@ -0,0 +1,48 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314 +#> FROM https://busybox.net/downloads/busybox-1.36.1.tar.bz2 + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2b1-clang/bin" + +mkdir -p /tmp/2b2-busybox; cd /tmp/2b2-busybox +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/1-stage1/protobusybox/bin/ash aliases/sh +export PATH="/tmp/2b2-busybox/aliases:$PATH" + +echo "### $0: unpacking busybox sources..." +tar --strip-components=1 -xf /downloads/busybox-1.36.1.tar.bz2 + +echo "### $0: configuring busybox..." +BUSYBOX_FLAGS='CONFIG_SHELL=/store/1-stage1/protobusybox/bin/ash' +BUSYBOX_FLAGS="$BUSYBOX_FLAGS CC=cc HOSTCC=cc" +BUSYBOX_FLAGS="$BUSYBOX_FLAGS KCONFIG_NOTIMESTAMP=y" +BUSYBOX_CFLAGS='CFLAGS=-O2 -isystem /store/2a6-linux-headers/include' +echo -e '#!/store/1-stage1/protobusybox/bin/ash\nprintf 9999' \ + > scripts/gcc-version.sh +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|g' \ + scripts/gen_build_files.sh \ + scripts/mkconfigs scripts/embedded_scripts scripts/trylink \ + scripts/generate_BUFSIZ.sh \ + applets/usage_compressed applets/busybox.mkscripts applets/install.sh +make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" defconfig +sed -i 's|CONFIG_INSTALL_NO_USR=y|CONFIG_INSTALL_NO_USR=n|' .config +sed -i 's|CONFIG_FEATURE_COMPRESS_USAGE=y|CONFIG_FEATURE_COMPRESS_USAGE=n|' \ + .config + +echo "### $0: building busybox..." +make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" busybox busybox.links +sed -i 's|^/usr/s\?bin/|/bin/|' busybox.links + +echo "### $0: installing busybox..." +make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" \ + install CONFIG_PREFIX=/store/2b2-busybox + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2b2 /store/2b2-busybox ) diff --git a/06/recipes/2b3-gnumake.sh b/06/recipes/2b3-gnumake.sh new file mode 100755 index 0000000..b9d563c --- /dev/null +++ b/06/recipes/2b3-gnumake.sh @@ -0,0 +1,45 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH dd16fb1d67bfab79a72f5e8390735c49e3e8e70b4945a15ab1f81ddb78658fb3 +#> FROM http://ftp.gnu.org/gnu/make/make-4.4.1.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2b1-clang/bin" + +mkdir -p /tmp/2b3-gnumake; cd /tmp/2b3-gnumake +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking GNU Make sources..." +tar --strip-components=1 -xf /downloads/make-4.4.1.tar.gz + +echo "### $0: fixing up GNU Make sources..." +# embrace chaos +shuffle_comment='\/\* Handle shuffle mode argument. \*\/' +shuffle_default='if (!shuffle_mode) shuffle_mode = xstrdup(\"random\");' +sed -i "s|$shuffle_comment|$shuffle_comment\n$shuffle_default|" src/main.c +grep 'if (!shuffle_mode) shuffle_mode = xstrdup("random");' src/main.c + +echo "### $0: building GNU Make..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' build-aux/install-sh +ash ./configure \ + CONFIG_SHELL=ash SHELL=ash MAKEINFO=true \ + --build x86_64-linux \ + --prefix=/store/2b3-gnumake \ + --disable-dependency-tracking +make -j $NPROC CFLAGS=-O2 + +echo "### $0: installing GNU Make with itself to test it..." +./make -j $NPROC SHELL=ash install-strip + +echo "### $0: creating a wrapper that respects \$SHELL..." +# FIXME: patch make to use getenv? +mkdir /store/2b3-gnumake/wrappers; cd /store/2b3-gnumake/wrappers +echo "#!/store/2b2-busybox/bin/ash" > make +echo "exec /store/2b3-gnumake/bin/make SHELL=\$SHELL \"\$@\"" \ >> make +chmod +x make + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2b3 /store/2b3-gnumake ) diff --git a/06/recipes/3a-boost.sh b/06/recipes/3a-boost.sh new file mode 100755 index 0000000..4927a2d --- /dev/null +++ b/06/recipes/3a-boost.sh @@ -0,0 +1,50 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 6478edfe2f3305127cffe8caf73ea0176c53769f4bf1585be237eb30798c3b8e +#> FROM https://archives.boost.io/release/1.83.0/source/boost_1_83_0.tar.bz2 + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" +export LD_LIBRARY_PATH=/store/2b1-clang/lib + +mkdir -p /tmp/3a-boost; cd /tmp/3a-boost +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/2b2-busybox/bin/ash aliases/sh +export PATH="/tmp/3a-boost/aliases:$PATH" + +echo "### $0: unpacking Boost sources..." +tar --strip-components=1 -xf /downloads/boost_1_83_0.tar.bz2 + +echo "### $0: patching up Boost sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' \ + bootstrap.sh +sed -i 's|/usr/bin/env sh|/store/2b2-busybox/bin/ash|' \ + tools/build/src/engine/build.sh +sed -i 's|/bin/sh|sh|' \ + tools/build/src/engine/execunix.cpp \ + boost/process/detail/posix/shell_path.hpp +EXTRA_INCL='/tmp/3a-boost/extra_includes' +mkdir -p $EXTRA_INCL +cp /store/2b1-clang/lib/clang/17/include/*intrin*.h $EXTRA_INCL/ +cp /store/2b1-clang/lib/clang/17/include/mm_malloc.h $EXTRA_INCL/ +cp /store/2b1-clang/lib/clang/17/include/unwind.h $EXTRA_INCL/ + +echo "### $0: building Boost..." +ash bootstrap.sh +./b2 -j $NPROC \ + include=/store/2a6-linux-headers/include \ + include=$EXTRA_INCL \ + include=/store/2b1-clang/include/x86_64-unknown-linux-musl/c++/v1 \ + --with-context --with-thread --with-system + +echo "### $0: installing Boost..." +./b2 install --prefix=/store/3a-boost \ + --with-context --with-thread --with-system + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-boost ) diff --git a/06/recipes/3a-brotli.sh b/06/recipes/3a-brotli.sh new file mode 100755 index 0000000..16ccbdf --- /dev/null +++ b/06/recipes/3a-brotli.sh @@ -0,0 +1,46 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH f9e8d81d0405ba66d181529af42a3354f838c939095ff99930da6aa9cdf6fe46 +#> FROM https://github.com/google/brotli/archive/refs/tags/v1.0.9.tar.gz +#> AS brotli-1.0.9.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" + +mkdir -p /tmp/3a-brotli; cd /tmp/3a-brotli +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking brotli sources..." +tar --strip-components=1 -xf /downloads/brotli-1.0.9.tar.gz + +echo "### $0: building brotli..." +ash configure --prefix=/store/3a-brotli --help #--disable-dependency-tracking +CFLAGS='-fPIC' +CFLAGS="$CFLAGS -DBROTLICOMMON_SHARED_COMPILATION" +CFLAGS="$CFLAGS -DBROTLI_SHARED_COMPILATION" +make -j $NPROC lib CFLAGS="$CFLAGS" +clang -shared bin/obj/c/common/*.o -o libbrotlicommon.so +clang -shared bin/obj/c/enc/*.o libbrotlicommon.so -o libbrotlienc.so +clang -shared bin/obj/c/dec/*.o libbrotlicommon.so -o libbrotlidec.so + +echo "### $0: installing brotli..." +mkdir -p /store/3a-brotli/lib /store/3a-brotli/include +cp libbrotlicommon.so libbrotlienc.so libbrotlidec.so /store/3a-brotli/lib/ +cp -r c/include/brotli /store/3a-brotli/include/ +mkdir -p /store/3a-brotli/lib/pkgconfig +for l in common enc dec; do + sed < scripts/libbrotli${l}.pc.in \ + -e 's|@PACKAGE_VERSION@|1.0.9|g' \ + -e 's|@prefix@|/store/3a-brotli|g' \ + -e 's|@exec_prefix@|/store/3a-brotli/bin|g' \ + -e 's|@includedir@|/store/3a-brotli/include|g' \ + -e 's|@libdir@|/store/3a-brotli/lib|g' \ + -e 's|-R|-Wl,-rpath=|g' \ + > /store/3a-brotli/lib/pkgconfig/libbrotli${l}.pc +done + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-brotli ) diff --git a/06/recipes/3a-curl.sh b/06/recipes/3a-curl.sh new file mode 100755 index 0000000..632d3b8 --- /dev/null +++ b/06/recipes/3a-curl.sh @@ -0,0 +1,31 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH dd322f6bd0a20e6cebdfd388f69e98c3d183bed792cf4713c8a7ef498cba4894 +#> FROM https://curl.se/download/curl-8.2.1.tar.xz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" +export PATH="$PATH:/store/3a-pkg-config/bin" + +mkdir -p /tmp/3a-curl; cd /tmp/3a-curl +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking curl sources..." +tar --strip-components=1 -xf /downloads/curl-8.2.1.tar.xz + +echo "### $0: building curl..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' configure install-sh + +ash configure --prefix=/store/3a-curl \ + --with-mbedtls=/store/3a-mbedtls \ + --disable-dependency-tracking +make -j $NPROC + +echo "### $0: installing curl..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-curl ) diff --git a/06/recipes/3a-editline.sh b/06/recipes/3a-editline.sh new file mode 100755 index 0000000..41240bb --- /dev/null +++ b/06/recipes/3a-editline.sh @@ -0,0 +1,29 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH df223b3333a545fddbc67b49ded3d242c66fadf7a04beb3ada20957fcd1ffc0e +#> FROM https://github.com/troglobit/editline/releases/download/1.17.1/editline-1.17.1.tar.xz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" + +mkdir -p /tmp/3a-editline; cd /tmp/3a-editline +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking editline sources..." +tar --strip-components=1 -xf /downloads/editline-1.17.1.tar.xz + +echo "### $0: fixing up editline sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' configure aux/install-sh + +echo "### $0: building editline..." +ash configure --prefix=/store/3a-editline --disable-dependency-tracking +make -j $NPROC + +echo "### $0: installing editline..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-editline ) diff --git a/06/recipes/3a-gnugperf.sh b/06/recipes/3a-gnugperf.sh new file mode 100755 index 0000000..6928e0e --- /dev/null +++ b/06/recipes/3a-gnugperf.sh @@ -0,0 +1,33 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 588546b945bba4b70b6a3a616e80b4ab466e3f33024a352fc2198112cdbb3ae2 +#> FROM http://ftp.gnu.org/pub/gnu/gperf/gperf-3.1.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" + +mkdir -p /tmp/3a-gnugperf; cd /tmp/3a-gnugperf +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking GNU gperf sources..." +tar --strip-components=1 -xf /downloads/gperf-3.1.tar.gz + +echo "### $0: patching up GNU gperf sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' \ + configure lib/configure src/configure tests/configure doc/configure \ + Makefile.in src/Makefile.in doc/Makefile.in + +echo "### $0: building GNU gperf..." +REWRITE="-ffile-prefix-map=$(pwd)=/builddir/" +ash configure --prefix=/store/3a-gnugperf \ + CFLAGS=$REWRITE CXXFLAGS="$REWRITE -Wno-register" +make -j $NPROC + +echo "### $0: installing GNU gperf..." +make -j $NPROC install + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-gnugperf ) diff --git a/06/recipes/3a-libarchive.sh b/06/recipes/3a-libarchive.sh new file mode 100755 index 0000000..4ebe643 --- /dev/null +++ b/06/recipes/3a-libarchive.sh @@ -0,0 +1,33 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH b17403ce670ff18d8e06fea05a9ea9accf70678c88f1b9392a2e29b51127895f +#> FROM http://libarchive.org/downloads/libarchive-3.7.1.tar.xz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" +export PATH="$PATH:/store/3a-pkg-config/bin" + +mkdir -p /tmp/3a-libarchive; cd /tmp/3a-libarchive +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking libarchive sources..." +tar --strip-components=1 -xf /downloads/libarchive-3.7.1.tar.xz + +echo "### $0: fixing up libarchive sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' \ + configure build/autoconf/install-sh + +echo "### $0: building libarchive..." +ash configure --prefix=/store/3a-libarchive \ + --disable-dependency-tracking \ + --without-openssl +make -j $NPROC + +echo "### $0: installing libarchive..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-libarchive ) diff --git a/06/recipes/3a-libsodium.sh b/06/recipes/3a-libsodium.sh new file mode 100755 index 0000000..ae80ed8 --- /dev/null +++ b/06/recipes/3a-libsodium.sh @@ -0,0 +1,34 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 6f504490b342a4f8a4c4a02fc9b866cbef8622d5df4e5452b46be121e46636c1 +#> FROM https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gz + + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" +export PATH="$PATH:/store/3a-pkg-config/bin" +export PKG_CONFIG_PATH='/store/3a-openssl/lib64/pkgconfig' + +mkdir -p /tmp/3a-libsodium; cd /tmp/3a-libsodium +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking libsodium sources..." +tar --strip-components=1 -xf /downloads/libsodium-1.0.18.tar.gz + +echo "### $0: fixing up libsodium sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' \ + configure build-aux/install-sh + +echo "### $0: building libsodium..." +ash configure --prefix=/store/3a-libsodium \ + --disable-dependency-tracking +make -j $NPROC + +echo "### $0: installing libsodium..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-libsodium ) diff --git a/06/recipes/3a-lowdown.sh b/06/recipes/3a-lowdown.sh new file mode 100755 index 0000000..2cfd514 --- /dev/null +++ b/06/recipes/3a-lowdown.sh @@ -0,0 +1,33 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 049b7883874f8a8e528dc7c4ed7b27cf7ceeb9ecf8fe71c3a8d51d574fddf84b +#> FROM https://github.com/kristapsdz/lowdown/archive/refs/tags/VERSION_1_0_2.tar.gz +#> AS lowdown-1.0.2.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/wrappers" +export PATH="$PATH:/store/3a-pkg-config/bin" +export PKG_CONFIG_PATH='/store/3a-openssl/lib64/pkgconfig' +export SHELL='/store/2b2-busybox/bin/ash' + +mkdir -p /tmp/3a-lowdown; cd /tmp/3a-lowdown +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking lowdown sources..." +tar --strip-components=1 -xf /downloads/lowdown-1.0.2.tar.gz + +echo "### $0: fixing up lowdown sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' configure + +echo "### $0: building lowdown..." +ash configure PREFIX=/store/3a-lowdown +make -j $NPROC CFLAGS=-ffile-prefix-map=$(pwd)=/builddir/ + +echo "### $0: installing lowdown..." +make -j $NPROC install_shared + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-lowdown ) diff --git a/06/recipes/3a-mbedtls.sh b/06/recipes/3a-mbedtls.sh new file mode 100755 index 0000000..5fa5413 --- /dev/null +++ b/06/recipes/3a-mbedtls.sh @@ -0,0 +1,32 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH a420fcf7103e54e775c383e3751729b8fb2dcd087f6165befd13f28315f754f5 +#> FROM https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/v3.4.1.tar.gz +#> AS mbedtls-3.4.1.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/wrappers" +export SHELL=/store/2b2-busybox/bin/ash + +mkdir -p /tmp/3a-mbedtls; cd /tmp/3a-mbedtls +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking mbedtls sources..." +tar --strip-components=1 -xf /downloads/mbedtls-3.4.1.tar.gz + +echo "### $0: fixing up mbedtls sources..." +sed -i 's|^DESTDIR=.*|DESTDIR=/store/3a-mbedtls|' Makefile +sed -i 's|programs: lib mbedtls_test|programs: lib|' Makefile +sed -i 's|install: no_test|install: lib|' Makefile + +echo "### $0: building mbedtls..." +make -j $NPROC lib + +echo "### $0: installing mbedtls..." +make -j $NPROC install + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-mbedtls ) diff --git a/06/recipes/3a-nlohmann-json.sh b/06/recipes/3a-nlohmann-json.sh new file mode 100755 index 0000000..37f0545 --- /dev/null +++ b/06/recipes/3a-nlohmann-json.sh @@ -0,0 +1,29 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 8c4b26bf4b422252e13f332bc5e388ec0ab5c3443d24399acb675e68278d341f +#> FROM https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz +#> AS nlohmann-json-3.11.2.tar.xz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" + +mkdir -p /tmp/3a-nlohmann-json; cd /tmp/3a-nlohmann-json + +echo "### $0: unpacking nlohmann-json sources..." +tar --strip-components=1 -xf /downloads/nlohmann-json-3.11.2.tar.xz + +echo "### $0: installing nlohmann-json..." +mkdir /store/3a-nlohmann-json +cp -rv include /store/3a-nlohmann-json +mkdir -p /store/3a-nlohmann-json/lib/pkgconfig +sed < cmake/pkg-config.pc.in \ + -e 's|${PROJECT_NAME}|nlohmann_json|' \ + -e 's|${PROJECT_VERSION}|3.11.2|' \ + -e 's|${CMAKE_INSTALL_FULL_INCLUDEDIR}|/store/3a-nlohmann-json/include|' \ + > /store/3a-nlohmann-json/lib/pkgconfig/nlohmann_json.pc + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-nlohmann-json ) diff --git a/06/recipes/3a-pkg-config.sh b/06/recipes/3a-pkg-config.sh new file mode 100755 index 0000000..399006b --- /dev/null +++ b/06/recipes/3a-pkg-config.sh @@ -0,0 +1,32 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 6fc69c01688c9458a57eb9a1664c9aba372ccda420a02bf4429fe610e7e7d591 +#> FROM https://pkgconfig.freedesktop.org/releases/pkg-config-0.29.2.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" + +mkdir -p /tmp/3a-pkg-config; cd /tmp/3a-pkg-config +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking pkg-config sources..." +tar --strip-components=1 -xf /downloads/pkg-config-0.29.2.tar.gz + +echo "### $0: patching pkg-config..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' \ + configure glib/configure \ + install-sh glib/install-sh + +echo "### $0: building pkg-config..." +ash configure --prefix=/store/3a-pkg-config --with-internal-glib \ + CFLAGS=-Wno-int-conversion +make -j $NPROC + +echo "### $0: installing pkg-config..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-pkg-config ) diff --git a/06/recipes/3a-seccomp.sh b/06/recipes/3a-seccomp.sh new file mode 100755 index 0000000..1840f8f --- /dev/null +++ b/06/recipes/3a-seccomp.sh @@ -0,0 +1,31 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH d82902400405cf0068574ef3dc1fe5f5926207543ba1ae6f8e7a1576351dcbdb +#> FROM https://github.com/seccomp/libseccomp/releases/download/v2.5.4/libseccomp-2.5.4.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" +export PATH="$PATH:/store/3a-gnugperf/bin" + +mkdir -p /tmp/3a-seccomp; cd /tmp/3a-seccomp +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking seccomp sources..." +tar --strip-components=1 -xf /downloads/libseccomp-2.5.4.tar.gz + +echo "### $0: patching up seccomp sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' configure build-aux/install-sh + +echo "### $0: building seccomp..." +ash configure --prefix=/store/3a-seccomp --disable-dependency-tracking \ + CFLAGS=-I/store/2a6-linux-headers/include +make -j $NPROC + +echo "### $0: installing seccomp..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-seccomp ) diff --git a/06/recipes/3a-sqlite.sh b/06/recipes/3a-sqlite.sh new file mode 100755 index 0000000..5907762 --- /dev/null +++ b/06/recipes/3a-sqlite.sh @@ -0,0 +1,28 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH 49008dbf3afc04d4edc8ecfc34e4ead196973034293c997adad2f63f01762ae1 +#> FROM https://sqlite.org/2023/sqlite-autoconf-3430000.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" + +mkdir -p /tmp/3a-sqlite; cd /tmp/3a-sqlite +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking SQLite archive..." +tar --strip-components=1 -xf /downloads/sqlite-autoconf-3430000.tar.gz + +echo "### $0: building SQLite..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' configure install-sh + +ash configure --prefix=/store/3a-sqlite +make -j $NPROC + +echo "### $0: installing SQLite..." +make -j $NPROC install-strip + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3a /store/3a-sqlite ) diff --git a/06/recipes/3b-busybox-static.sh b/06/recipes/3b-busybox-static.sh new file mode 100755 index 0000000..c2ae42c --- /dev/null +++ b/06/recipes/3b-busybox-static.sh @@ -0,0 +1,49 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314 +#> FROM https://busybox.net/downloads/busybox-1.36.1.tar.bz2 + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/wrappers" + +mkdir -p /tmp/3b-busybox-static; cd /tmp/3b-busybox-static +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: aliasing ash to sh..." +mkdir aliases; ln -s /store/2b2-busybox/bin/ash aliases/sh +export PATH="/tmp/3b-busybox-static/aliases:$PATH" + +echo "### $0: unpacking busybox sources..." +tar --strip-components=1 -xf /downloads/busybox-1.36.1.tar.bz2 + +echo "### $0: configuring busybox..." +BUSYBOX_FLAGS='CONFIG_SHELL=/store/2b2-busybox/bin/ash' +BUSYBOX_FLAGS='SHELL=/store/2b2-busybox/bin/ash' +BUSYBOX_FLAGS="$BUSYBOX_FLAGS CC=cc HOSTCC=cc" +BUSYBOX_FLAGS="$BUSYBOX_FLAGS KCONFIG_NOTIMESTAMP=y" +BUSYBOX_CFLAGS='CFLAGS=-O2 -isystem /store/2a6-linux-headers/include' +echo -e '#!/store/2b2-busybox/bin/ash\nprintf 9999' \ + > scripts/gcc-version.sh +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|g' \ + scripts/gen_build_files.sh \ + scripts/mkconfigs scripts/embedded_scripts scripts/trylink \ + scripts/generate_BUFSIZ.sh \ + applets/usage_compressed applets/busybox.mkscripts applets/install.sh +make -j $NPROC $BUSYBOX_FLAGS defconfig +sed -i 's|CONFIG_INSTALL_NO_USR=y|CONFIG_INSTALL_NO_USR=n|' .config +sed -i 's|CONFIG_FEATURE_SHARED_BUSYBOX=y|CONFIG_FEATURE_SHARED_BUSYBOX=n|' \ + .config + +echo "### $0: building busybox..." +make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" busybox busybox.links +sed -i 's|^/usr/s\?bin/|/bin/|' busybox.links + +echo "### $0: installing busybox..." +make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" \ + install CONFIG_PREFIX=/store/3b-busybox-static + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3b /store/3b-busybox-static ) diff --git a/06/recipes/3b-nix.sh b/06/recipes/3b-nix.sh new file mode 100755 index 0000000..71a6a49 --- /dev/null +++ b/06/recipes/3b-nix.sh @@ -0,0 +1,100 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH f3f8016621cf3971e0768404f05b89d4a7fc1911dddae5a9a7ed4bf62519302c +#> FROM https://github.com/ZilchOS/nix/releases/download/nix-2.17.0-zilched/nix-2.17.0-zilched.tar.xz + +#> FETCH 3659cd137c320991a78413dd370a92fd18e0a8bc36d017d554f08677a37d7d5a +#> FROM https://raw.githubusercontent.com/somasis/musl-compat/c12ea3af4e6ee53158a175d992049c2148db5ff6/include/sys/queue.h + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/wrappers" +export PATH="$PATH:/store/3a-pkg-config/bin" +export PATH="$PATH:/store/3a-lowdown/bin" + +export SHELL='/store/2b2-busybox/bin/ash' + +#export PKG_CONFIG_PATH='/store/3a-openssl/lib64/pkgconfig' +export PKG_CONFIG_PATH='' +#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-bzip2/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-sqlite/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-curl/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-editline/lib/pkgconfig" +#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-xz/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-brotli/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-seccomp/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libarchive/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libsodium/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-lowdown/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-nlohmann-json/lib/pkgconfig" +#LIBDIRS="$(pkg-config --variable=libdir openssl)" +LIBDIRS="" +#LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir bzip2)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir sqlite3)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libeditline)" +#LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir liblzma)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libbrotlicommon)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libseccomp)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libarchive)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libsodium)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir lowdown)" +export LD_LIBRARY_PATH=$LIBDIRS + +export BOOST_ROOT=/store/3a-boost/include + +mkdir -p /tmp/3b-nix; cd /tmp/3b-nix +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking Nix sources..." +tar --strip-components=1 \ + -xf /downloads/nix-2.17.0-zilched.tar.xz + +echo "### $0: copying queue.h..." +mkdir -p compat-includes/sys +cp /downloads/queue.h compat-includes/sys/ + +echo "### $0: stubbing out commands..." +mkdir stubs; export PATH="$(pwd)/stubs:$PATH" +ln -s /store/2b2-busybox/bin/true stubs/jq +ln -s /store/2b2-busybox/bin/true stubs/expr +ln -s /store/2b2-busybox/bin/ash stubs/bash + +echo "### $0: patching up Nix sources..." +sed -i 's|/bin/sh|/store/2b2-busybox/bin/ash|' configure +sed -i 's|/bin/sh|${stdenv.busybox}/bin/ash|' configure +# avoid an expression confusing ash +nl configure | grep 7217 | tee configure-problematic-line +grep -F "'X\(//\)$'" configure-problematic-line +sed -i '7217d' configure +nl configure | grep 7217 | tee configure-problematic-line +! grep -F "'X\(//\)$'" configure-problematic-line +# replace the declare confusing ash +sed -i 's|declare \$name=.*|:|' configure + +echo "### $0: building Nix..." +PCDEPS='libbrotlicommon libbrotlienc libbrotlidec sqlite3 libseccomp lowdown' +PCDEPS="$PCDEPS nlohmann_json" +INC="-I/store/2a6-linux-headers/include -I$(pwd)/compat-includes" +REWRITE="-ffile-prefix-map=$(pwd)=/builddir/" +export CFLAGS="$(pkg-config --cflags $PCDEPS) $INC $REWRITE" +export CXXFLAGS="$CFLAGS" +export GLOBAL_CXXFLAGS="$CFLAGS" +export LDFLAGS="$(pkg-config --libs $PCDEPS) -L/store/3a-boost/lib -v" +ash configure --prefix=/store/3b-nix \ + --with-boost=$BOOST_ROOT \ + --disable-doc-gen \ + --disable-gc \ + --disable-cpuid \ + --disable-gtest \ + --with-sandbox-shell=/store/3b-busybox-static/bin/busybox +sed -i "s|\${prefix}|/store/3b-nix|g" config.status +sed -i "s|\${exec_prefix}|/store/3b-nix|g" config.status +make -j $NPROC V=1 + +echo "### $0: installing Nix..." +make -j $NPROC install + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3b /store/3b-nix ) diff --git a/06/recipes/3b-tinycc-static.sh b/06/recipes/3b-tinycc-static.sh new file mode 100755 index 0000000..562a430 --- /dev/null +++ b/06/recipes/3b-tinycc-static.sh @@ -0,0 +1,42 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH f5a71d05664340ae46cda9579c6079a0f2fa809d24386d284f0d091e4d576a4e +#> FROM https://github.com/TinyCC/tinycc/archive/af1abf1f45d45b34f0b02437f559f4dfdba7d23c.tar.gz +#> AS tinycc-mob-af1abf1.tar.gz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/wrappers" +export SHELL="/store/2b2-busybox/bin/ash" + +mkdir -p /tmp/3b-tinycc-static; cd /tmp/3b-tinycc-static +if [ -e /ccache/setup ]; then . /ccache/setup; fi + +echo "### $0: unpacking TinyCC sources..." +tar --strip-components=1 -xf /downloads/tinycc-mob-af1abf1.tar.gz + +#echo "### $0: fixing up TinyCC sources..." +sed -i "s|^VERSION = .*|VERSION = mob-af1abf1|" configure +sed -i "s|^GITHASH := .*|GITHASH = mob:af1abf1|" configure + +echo "### $0: configuring TinyCC..." +$SHELL configure \ + --prefix=/store/3b-tinycc-static \ + --cc=cc \ + --extra-cflags="-O3 -static" \ + --extra-ldflags="-static" \ + --enable-static \ + --config-musl + +echo "### $0: building TinyCC..." + +make -j $NPROC tcc + +echo "### $0: installing TinyCC..." +mkdir -p /store/3b-tinycc-static/bin +cp tcc /store/3b-tinycc-static/bin/ + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/3b /store/3b-tinycc-static ) diff --git a/06/recipes/3b-zig.sh b/06/recipes/3b-zig.sh new file mode 100644 index 0000000..6e6c472 --- /dev/null +++ b/06/recipes/3b-zig.sh @@ -0,0 +1,14 @@ +#!/store/2b2-busybox/bin/ash + +#> FETCH +#> FROM https://github.com/ziglang/zig-bootstrap/archive/refs/tags/0.14.0.tar.gz +#> AS zig-bootstrap-0.14.0.tar.gz + +mkdir -p /tmp/3a-zig; cd /tmp/3a-zig + +echo "### $0: unpacking zig-boostrap sources..." +tar --strip-components=1 -xf /downloads/zig-bootstrap-0.14.0.tar.gz + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/2b1-clang/bin" +export PATH="$PATH:/store/2b3-gnumake/bin" diff --git a/06/recipes/4-rebootstrap-using-nix.sh b/06/recipes/4-rebootstrap-using-nix.sh new file mode 100755 index 0000000..1d89aea --- /dev/null +++ b/06/recipes/4-rebootstrap-using-nix.sh @@ -0,0 +1,98 @@ +#!/store/2b2-busybox/bin/ash + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/3a-pkg-config/bin" +export PATH="$PATH:/store/3a-sqlite/bin" +export PATH="$PATH:/store/3b-nix/bin" + +export SHELL='/store/2b2-busybox/bin/ash' + +export PKG_CONFIG_PATH='' +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-sqlite/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-curl/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-editline/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-seccomp/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libarchive/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libsodium/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-lowdown/lib/pkgconfig" +LIBDIRS='' +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir sqlite3)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libcurl)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libeditline)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libseccomp)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libarchive)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libsodium)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir lowdown)" +LIBDIRS="$LIBDIRS:/store/3a-boost/lib" +LIBDIRS="$LIBDIRS:/store/2b1-clang/lib" +export LD_LIBRARY_PATH=$LIBDIRS + +mkdir -p /tmp/4-rebootstrap-using-nix; cd /tmp/4-rebootstrap-using-nix + +echo "### $0: preparing stuff for nix to work..." +mkdir -p /dev/pts +mount -t devpts devpts /dev/pts +ln -s /dev/pts/ptmx /dev/ptmx + +echo "### $0: faking lots of stuff for nix to work..." +mkdir shelter +export HOME=/tmp/4-rebootstrap-using-nix/shelter +export USER=notauser +echo 'oh come on' >/dev/urandom + +echo "### $0: fixing up paths to shell..." +cp -a --reflink=auto /using-nix /default.nix /recipes ./ +sed -i 's|/bin/sh|/store/3b-busybox-static/bin/ash|' using-nix/1-stage1.nix + +echo "### $0: pointing to local downloads..." +sed -i 's| url =| #remote_url =|' using-nix/*.nix +sed -i 's|# local = \(.*\);|url = "file://\1";|' using-nix/*.nix + +if [ -e /prev/nix/store ] && [ -e /prev/nix-db.tar ]; then + echo "### $0: restoring nix store & db from previous build..." + mkdir -p /nix + cp -a --reflink=auto /prev/nix/store /nix + tar -xf /prev/nix-db.tar -C / + sqlite3 /nix/var/nix/db/db.sqlite \ + < /nix/var/nix/db/db.sqlite.dump + rm /nix/var/nix/db/db.sqlite.dump +fi + +echo "### $0: writing a 0.nix that simply injects what we've built..." +# Makefile bootstrap injects it as /stage/protosrc, regular --- as /protosrc +[ -e /protosrc ] && PROTOSRC="/protosrc" || PROTOSRC=/stage/protosrc +echo "{ tinycc = /store/3b-tinycc-static/bin/tcc; protosrc = $PROTOSRC; }" \ + > using-nix/0.nix + +echo "### $0: rebuilding everything using nix..." +nix-build \ + --extra-experimental-features ca-derivations \ + --option build-users-group '' \ + --option compress-build-log false \ + --no-substitute \ + --cores $NPROC \ + --keep-failed \ + -vvv \ + default.nix +rm -f /dev/urandom +rm /dev/ptmx +umount /dev/pts +umount /dev/pts || true +rm -r /dev/pts +rm -r shelter +rm -rf /build + +# this one is special wrt how the results are saved, see Makefile/USE_NIX_CACHE +echo "### $0: exporting resulting /nix/store (reproducible)..." +mkdir -p /store/4-rebootstrap-using-nix/nix +cp -a --reflink=auto /nix/store /store/4-rebootstrap-using-nix/nix/ + +echo "### $0: exporting /nix/var/nix/db to restore it (non-reproducible)..." +cp /nix/var/nix/db/db.sqlite db.sqlite +sqlite3 db.sqlite 'UPDATE ValidPaths SET registrationTime = 1;' +sqlite3 db.sqlite .dump > /nix/var/nix/db/db.sqlite.dump +tar --exclude nix/var/nix/db/db.sqlite \ + -cf /store/4-rebootstrap-using-nix/nix-db.tar /nix/var/nix/db +rm /nix/var/nix/db/db.sqlite.dump diff --git a/06/recipes/5-go-beyond-using-nix.sh b/06/recipes/5-go-beyond-using-nix.sh new file mode 100755 index 0000000..66133e3 --- /dev/null +++ b/06/recipes/5-go-beyond-using-nix.sh @@ -0,0 +1,203 @@ +#!/store/2b2-busybox/bin/ash +#> FETCH 10fa524294f58c805411ddd6e5522c02a0b69ad14e036b141cc80fb53a3ef1a0 +#> FROM https://github.com/ZilchOS/core/archive/2023.10.1.tar.gz +#> AS ZilchOS-core-2023.10.1.tar.gz + +#> FETCH ddd417f9caab3ef0f3031b938815a5c33367c3a50c09830138d208bd3126c98f +#> FROM https://github.com/limine-bootloader/limine/releases/download/v5.20230830.0/limine-5.20230830.0.tar.xz + +#> FETCH 1952b2a782ba576279c211ee942e341748fdb44997f704dd53def46cd055470b +#> FROM https://github.com/NixOS/patchelf/releases/download/0.18.0/patchelf-0.18.0.tar.bz2 + +#> FETCH 9bba0214ccf7f1079c5d59210045227bcf619519840ebfa80cd3849cff5a5bf2 +#> FROM https://ftp.gnu.org/gnu/bison/bison-3.8.2.tar.xz + +#> FETCH 63aede5c6d33b6d9b13511cd0be2cac046f2e70fd0a07aa9573a04a82783af96 +#> FROM https://ftp.gnu.org/gnu/m4/m4-1.4.19.tar.xz + +#> FETCH e87aae032bf07c26f85ac0ed3250998c37621d95f8bd748b31f15b33c45ee995 +#> FROM https://github.com/westes/flex/files/981163/flex-2.6.4.tar.gz + +#> FETCH 541e179665dc4e272b9602f2074243591a157da89cc47064da8c5829dbd2b339 +#> FROM http://ftp.gnu.org/gnu/mtools/mtools-4.0.43.tar.bz2 + +#> FETCH 786f9f5df9865cc5b0c1fecee3d2c0f5e04cab8c9a859bd1c9c7ccd4964fdae1 +#> FROM https://www.gnu.org/software/xorriso/xorriso-1.5.6.pl02.tar.gz + +#> FETCH 9c4396cc829cfae319a6e2615202e82aad41372073482fce286fac78646d3ee4 +#> FROM https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-1.5.5.tar.gz + +#> FETCH 23c2469e2a568362a62eecf1b49ed90a15621e6fa30e29947ded3436422de9b9 +#> FROM https://curl.se/ca/cacert-2023-08-22.pem + +#> FETCH 85cd12e9cf1d6d5a45f17f7afe1cebe7ee628d3282281c492e86adf636defa3f +#> FROM https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tar.xz + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/3a-pkg-config/bin" +export PATH="$PATH:/store/3a-sqlite/bin" +export PATH="$PATH:/store/3b-nix/bin" + +export SHELL='/store/2b2-busybox/bin/ash' + +export PKG_CONFIG_PATH='' +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-sqlite/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-curl/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-editline/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-seccomp/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libarchive/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libsodium/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-lowdown/lib/pkgconfig" +LIBDIRS='' +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir sqlite3)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libcurl)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libeditline)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libseccomp)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libarchive)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libsodium)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir lowdown)" +LIBDIRS="$LIBDIRS:/store/3a-boost/lib" +LIBDIRS="$LIBDIRS:/store/2b1-clang/lib" +export LD_LIBRARY_PATH=$LIBDIRS + +mkdir -p /tmp/5-go-beyond-using-nix; cd /tmp/5-go-beyond-using-nix + +echo "### $0: preparing stuff for nix to work..." +mkdir -p /dev/pts +mount -t devpts devpts /dev/pts +ln -s /dev/pts/ptmx /dev/ptmx + +echo "### $0: faking lots of stuff for nix to work..." +mkdir shelter +export HOME=/tmp/5-go-beyond-using-nix/shelter +export USER=notauser +echo 'oh come on' >/dev/urandom +printf '\0\0\0\0\0\0\0\0\0\0' > 10x0 +cat 10x0 10x0 10x0 10x0 10x0 10x0 10x0 10x0 10x0 10x0 > 100x0 +cat 100x0 100x0 100x0 100x0 100x0 100x0 100x0 100x0 100x0 100x0 > 1Kx0 +cat 1Kx0 1Kx0 1Kx0 1Kx0 1Kx0 1Kx0 1Kx0 1Kx0 1Kx0 1Kx0 > 10Kx0 +cat 10Kx0 10Kx0 10Kx0 10Kx0 10Kx0 10Kx0 10Kx0 10Kx0 10Kx0 10Kx0 > 100Kx0 +cat 100Kx0 100Kx0 100Kx0 100Kx0 100Kx0 100Kx0 100Kx0 100Kx0 100Kx0 100Kx0 \ + > 1Mx0 +cat 1Mx0 1Mx0 1Mx0 1Mx0 1Mx0 1Mx0 1Mx0 1Mx0 1Mx0 1Mx0 > 10Mx0 +mv 10Mx0 /dev/zero +rm *x0 + +echo "### $0: fixing up paths to shell..." +sed -i 's|/bin/sh|/store/3b-busybox-static/bin/ash|' /using-nix/1-stage1.nix + +if [ -e /prev/nix/store ] && [ -e /prev/nix-db.tar ]; then + echo "### $0: restoring nix store & db from previous build..." + mkdir -p /nix + mv /prev/nix/store /nix + tar -xf /prev/nix-db.tar -C / + sqlite3 /nix/var/nix/db/db.sqlite \ + < /nix/var/nix/db/db.sqlite.dump + rm /nix/var/nix/db/db.sqlite.dump +elif [ ! -e /nix/store ]; then + echo "### $0: restoring nix store & db from previous stage..." + mkdir -p /nix + cp -a /store/4-rebootstrap-using-nix/nix/store /nix/ + tar -xf /store/4-rebootstrap-using-nix/nix-db.tar -C / + sqlite3 /nix/var/nix/db/db.sqlite \ + < /nix/var/nix/db/db.sqlite.dump + rm /nix/var/nix/db/db.sqlite.dump +fi + + +echo "### $0: creating a ZilchOS/bootstrap flake..." +mkdir ZilchOS-bootstrap +cp -r /flake.nix /default.nix /using-nix /recipes \ + ZilchOS-bootstrap/ + +echo "### $0: pointing to local files..." +sed -i 's| url =| #remote_url =|' ZilchOS-bootstrap/using-nix/*.nix +sed -i 's|# local = \(.*\);|url = "file://\1";|' ZilchOS-bootstrap/using-nix/*.nix +echo "### $0: writing a 0.nix that simply injects what we've built..." +# Makefile bootstrap injects it as /stage/protosrc, regular --- as /protosrc +[ -e /protosrc ] && PROTOSRC="/protosrc" || PROTOSRC=/stage/protosrc +echo "{ tinycc = /store/3b-tinycc-static/bin/tcc; protosrc = $PROTOSRC; }" \ + > ZilchOS-bootstrap/using-nix/0.nix + +if [[ ! -e ZilchOS-core ]]; then + echo "### $0: unpacking ZilchOS/core archive..." + mkdir ZilchOS-core + tar -xf /downloads/ZilchOS-core-2023.10.1.tar.gz --strip-components=1 \ + -C ZilchOS-core +fi +[[ -e ZilchOS-core/flake.nix ]] +cd ZilchOS-core +nix flake lock \ + --extra-experimental-features 'ca-derivations flakes nix-command' \ + --update-input bootstrap-from-tcc \ + --override-input bootstrap-from-tcc path:../ZilchOS-bootstrap +pwd +cd .. +ls -l ZilchOS-core + +echo "### $0: pointing to local downloads..." +sed -i 's| url =| #remote_url =|' \ + ZilchOS-core/*/*.nix ZilchOS-core/*/*/*.nix +sed -i 's|# local = \(.*\);|url = "file://\1";|' \ + ZilchOS-core/*/*.nix ZilchOS-core/*/*/*.nix + +if [ -e /ccache/setup ]; then + echo "### $0: configuring ccache..." + export CCACHE_COMPILERCHECK=content + export CCACHE_SLOPPINESS=include_file_ctime,include_file_mtime + export CCACHE_MAXSIZE=0 + export CCACHE_DIR=/ccache + MAYBE_CCACHE='ccachedPackages.' +else + MAYBE_CCACHE='' +fi + +echo "### $0: building ZilchOS/core using nix..." +mkdir -p /store/5-go-beyond-using-nix +: > /store/5-go-beyond-using-nix/hashes +while IFS=' ' read -r _unused_old_hash pkg; do + # can't have sandbox, need deterministic build paths + nix build \ + --extra-experimental-features 'ca-derivations flakes nix-command' \ + --option build-users-group '' \ + --option compress-build-log false \ + --no-substitute \ + --cores $NPROC \ + --keep-failed \ + --show-trace \ + -L \ + -vvv \ + -o .tmp \ + "./ZilchOS-core#${MAYBE_CCACHE}${pkg}" + new_path=$(readlink .tmp*) + new_hash=$(echo $new_path | sed -E 's|.*/([a-z0-9]{32})-.*|\1|') + if [ "$pkg" != 'live-cd^iso' ]; then rm .tmp*; fi + echo "$new_hash $pkg" >> /store/5-go-beyond-using-nix/hashes +done < ./ZilchOS-core/.maint/hashes +sha256sum .tmp*-iso # the last one is the iso +rm -f /dev/urandom +rm -f /dev/zero +rm /dev/ptmx +umount /dev/pts +umount /dev/pts || true +rm -r /dev/pts +rm -r shelter +rm -rf /build + +# this one is special wrt how the results are saved, see Makefile/USE_NIX_CACHE +echo "### $0: exporting resulting /nix/store (reproducible)..." +mkdir -p /store/5-go-beyond-using-nix/nix +cp -a --reflink=auto /nix/store /store/5-go-beyond-using-nix/nix/ + +echo "### $0: exporting /nix/var/nix/db to restore it (non-reproducible)..." +cp /nix/var/nix/db/db.sqlite db.sqlite +sqlite3 db.sqlite 'UPDATE ValidPaths SET registrationTime = 1;' +sqlite3 db.sqlite .dump > /nix/var/nix/db/db.sqlite.dump +tar --exclude nix/var/nix/db/db.sqlite \ + -cf /store/5-go-beyond-using-nix/nix-db.tar /nix/var/nix/db +rm /nix/var/nix/db/db.sqlite.dump + +echo "### $0: exporting the iso as well..." +cat .tmp*-iso > /store/5-go-beyond-using-nix/ZilchOS-core.iso diff --git a/06/recipes/_1.test.sh b/06/recipes/_1.test.sh new file mode 100755 index 0000000..06c45b3 --- /dev/null +++ b/06/recipes/_1.test.sh @@ -0,0 +1,40 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export PATH=/store/1-stage1/tinycc/wrappers:/store/1-stage1/protobusybox/bin + +mkdir -p /tmp/_1.test; cd /tmp/_1.test + + +echo "### $0: checking that /protosrc has not leaked into outputs..." +! grep -rF /protosrc /store/1-stage1 + +echo "### $0: checking compilation..." +cat > va_test.c <<\EOF +#include +int main(int _, char* argv[]) { printf("%sargs\n", argv[1]); return 0; } +EOF + +cat va_test.c +cc -o va_test.o va_test.c +cc -o va_test va_test.c +( ! grep /store/2a3-intermediate-musl/lib/libc.so va_test.o va_test ) +( ! grep ld-linux va_test.o va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: checking that we've got bzip2..." + +hello=$(echo hello | bzip2 -1 | bzip2 -d) +[ "$hello" == hello ] + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/_1.test . ) +( ! grep -rF /tmp/1-stage1 . ) +( ! grep -rF /store/1-stage1 . ) +( ! grep -rF va_test . ) +( ! grep -rF /tmp/1-stage1 /store/1-stage1 ) +( ! grep -rF /tmp/_1.test /store/1-stage1 ) + +touch /store/_1.test # indicator of successful completion diff --git a/06/recipes/_2a0-ccache.sh b/06/recipes/_2a0-ccache.sh new file mode 100755 index 0000000..e78c8d7 --- /dev/null +++ b/06/recipes/_2a0-ccache.sh @@ -0,0 +1,65 @@ +#!/store/1-stage1/protobusybox/bin/ash + +#> FETCH a02f4e8360dc6618bc494ca35b0ae21cea080f804a4898eab1ad3fcd108eb400 +#> FROM https://github.com/ccache/ccache/releases/download/v3.7.12/ccache-3.7.12.tar.xz + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/1-stage1/tinycc/wrappers" +export PATH="$PATH:/store/2a0-static-gnumake/bin" + +echo "### $0: unpacking ccache sources..." +mkdir -p /tmp/_2a0-ccache; cd /tmp/_2a0-ccache +tar --strip-components=1 -xf /downloads/ccache-3.7.12.tar.xz + +echo "### $0: building ccache..." +sed -i 's|/bin/sh|/store/1-stage1/protobusybox/bin/ash|' configure +ash configure \ + --host x86_64-linux --build x86_64-linux \ + --prefix=/store/_2a0-ccache +make -j $NPROC + +echo "### $0: installing ccache..." +make -j $NPROC install + +cat > /store/_2a0-ccache/wrap-available <<\EOF +mkdir -p .ccache-wrappers +for prefix in '' x86_64-linux- x86_64-linux-musl- x86_64-linux-unknown-; do + for name in cc c++ gcc g++ clang clang++ tcc; do + if command -v $prefix$name; then + ln -s /store/_2a0-ccache/bin/ccache \ + .ccache-wrappers/$prefix$name + fi + done +done +pwd +export PATH="$(pwd)/.ccache-wrappers:$PATH" +EOF +chmod +x /store/_2a0-ccache/wrap-available + +. /store/_2a0-ccache/wrap-available + +mkdir /store/_2a0-ccache/etc +cat > /store/_2a0-ccache/etc/ccache.conf <<\EOF +cache_dir = /ccache +compiler_check = content +compression = false +sloppiness = include_file_ctime,include_file_mtime +max_size = 0 +EOF +export PATH="/store/_2a0-ccache/wrappers/cc-only:$PATH" + +echo "### $0: testing ccache on itself..." +/store/_2a0-ccache/bin/ccache -z +/store/_2a0-ccache/bin/ccache -s > _stats; cat _stats +grep '^cache miss 0$' _stats +grep '^cache hit rate 0.00 %$' _stats +ash configure --host x86_64-linux --build x86_64-linux CC=cc +make -j $NPROC -B +/store/_2a0-ccache/bin/ccache -z +make -j $NPROC -B +/store/_2a0-ccache/bin/ccache -s > _stats; cat _stats +grep '^cache miss 0$' _stats +grep '^cache hit rate 100.00 %' _stats +/store/_2a0-ccache/bin/ccache -z diff --git a/06/recipes/_2a3.test.sh b/06/recipes/_2a3.test.sh new file mode 100755 index 0000000..33b5ba4 --- /dev/null +++ b/06/recipes/_2a3.test.sh @@ -0,0 +1,37 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export PATH='/store/2a0-static-gnumake/bin' +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a2-static-gnugcc4-c/bin" +export PATH="$PATH:/store/1-stage1/protobusybox/bin" + +mkdir -p /tmp/_2a3.test; cd /tmp/_2a3.test + +echo "### $0: preparing..." +cat > va_test.c <<\EOF +#include +int main(int _, char* argv[]) { printf("%sargs\n", argv[1]); return 0; } +EOF + +cat va_test.c + +echo "### $0: testing (dynamic)..." +SYSROOT=/store/2a3-intermediate-musl +make va_test \ + CC=gcc \ + LDFLAGS="-Wl,--dynamic-linker=$SYSROOT/lib/libc.so --sysroot $SYSROOT" +grep /store/2a3-intermediate-musl/lib/libc.so va_test +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (static)..." +make -B va_test CC=gcc LDFLAGS="-static --sysroot $SYSROOT" +( ! grep /store/2a3-intermediate-musl/lib/libc.so va_test ) +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +touch /store/_2a3.test # indicator of successful completion diff --git a/06/recipes/_2a4.test.sh b/06/recipes/_2a4.test.sh new file mode 100755 index 0000000..3e14775 --- /dev/null +++ b/06/recipes/_2a4.test.sh @@ -0,0 +1,46 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export PATH='/store/2a0-static-gnumake/bin' +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a4-gnugcc4-cpp/bin" +export PATH="$PATH:/store/1-stage1/protobusybox/bin" + +mkdir -p /tmp/_2a4.test; cd /tmp/_2a4.test + +echo "### $0: preparing..." +cat > va_test.c <<\EOF +#include +int main(int _, char* argv[]) { printf("%sargs\n", argv[1]); return 0; } +EOF + +cat va_test.c + +echo "### $0: testing (dynamic)..." +make va_test CC=gcc # neither linker nor sysroot need to be specified now +grep /store/2a3-intermediate-musl/lib/libc.so va_test +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (static)..." +make -B va_test CC=gcc LDFLAGS=-static # no specifying sysroot anymore +( ! grep /store/2a3-intermediate-musl/lib/libc.so va_test ) +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (dynamic C++)..." +cat > cpp_test.cpp <<\EOF +#include +using namespace std; +int main() { cout << "this is c+" << "+" << endl; return 0; } +EOF +make cpp_test +grep /store/2a3-intermediate-musl/lib/libc.so cpp_test +( ! grep ld-linux cpp_test ) +./cpp_test +[ "$(./cpp_test)" == 'this is c++' ] + +touch /store/_2a4.test # indicator of successful completion diff --git a/06/recipes/_2a5.test.sh b/06/recipes/_2a5.test.sh new file mode 100755 index 0000000..2811964 --- /dev/null +++ b/06/recipes/_2a5.test.sh @@ -0,0 +1,46 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a1-static-binutils/bin" +export PATH="$PATH:/store/2a5-gnugcc10/bin" + +mkdir -p /tmp/_2a5.test; cd /tmp/_2a5.test + +echo "### $0: preparing..." +cat > va_test.c <<\EOF +#include +int main(int _, char* argv[]) { printf("%sargs\n", argv[1]); return 0; } +EOF + +cat va_test.c + +echo "### $0: testing (dynamic)..." +make va_test CC=gcc +grep /store/2a3-intermediate-musl/lib/libc.so va_test +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (static)..." +make -B va_test CC=gcc LDFLAGS=-static +( ! grep /store/2a3-intermediate-musl/lib/libc.so va_test ) +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (dynamic C++)..." +cat > cpp_test.cpp <<\EOF +#include +using namespace std; +int main() { cout << "this is c+" << "+" << endl; return 0; } +EOF +make cpp_test +grep /store/2a3-intermediate-musl/lib/libc.so cpp_test +( ! grep ld-linux cpp_test ) +./cpp_test +[ "$(./cpp_test)" == 'this is c++' ] + +touch /store/_2a5.test # indicator of successful completion diff --git a/06/recipes/_2a9.test.sh b/06/recipes/_2a9.test.sh new file mode 100755 index 0000000..e12ad29 --- /dev/null +++ b/06/recipes/_2a9.test.sh @@ -0,0 +1,49 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2a9-intermediate-clang/bin/generic-names" + +mkdir -p /tmp/_2a9.test; cd /tmp/_2a9.test + +echo "### $0: preparing..." +cat > va_test.c <<\EOF +#include +int main(int _, char* argv[]) { printf("%sargs\n", argv[1]); return 0; } +EOF + +cat va_test.c + +echo "### $0: testing (dynamic)..." +make va_test +grep /store/2a3-intermediate-musl/lib/libc.so va_test +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (static)..." +make -B va_test LDFLAGS=-static +( ! grep libc.so va_test ) +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (dynamic C++)..." +cat > cpp_test.cpp <<\EOF +#include +using namespace std; +int main() { cout << "this is c+" << "+" << endl; return 0; } +EOF +# FIXME flags! +make cpp_test CXX=c++ LDFLAGS='-rpath /store/2a9-intermediate-clang/lib' +grep /store/2a3-intermediate-musl/lib/libc.so cpp_test +( ! grep ld-linux cpp_test ) +./cpp_test +[ "$(./cpp_test)" == 'this is c++' ] + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2a9 /store/2a9-intermediate-clang ) + +touch /store/_2a9.test # indicator of successful completion diff --git a/06/recipes/_2b1.test.sh b/06/recipes/_2b1.test.sh new file mode 100755 index 0000000..15377d1 --- /dev/null +++ b/06/recipes/_2b1.test.sh @@ -0,0 +1,51 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export PATH='/store/1-stage1/protobusybox/bin' +export PATH="$PATH:/store/2a0-static-gnumake/bin" +export PATH="$PATH:/store/2b1-clang/bin" + +mkdir -p /tmp/_2b1.test; cd /tmp/_2b1.test + +echo "### $0: preparing..." +cat > va_test.c <<\EOF +#include +int main(int _, char* argv[]) { printf("%sargs\n", argv[1]); return 0; } +EOF + +cat va_test.c + +echo "### $0: testing (dynamic)..." +make va_test +grep /store/2b0-musl/lib/libc.so va_test +( ! grep /store/2a3-intermediate-musl/lib/libc.so va_test ) +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (static)..." +make -B va_test LDFLAGS=-static +( ! grep libc.so va_test ) +( ! grep ld-linux va_test ) +./va_test var +[ "$(./va_test var)" == varargs ] + +echo "### $0: testing (dynamic C++)..." +cat > cpp_test.cpp <<\EOF +#include +using namespace std; +int main() { cout << "this is c+" << "+" << endl; return 0; } +EOF +# FIXME flags! +make cpp_test CXX=c++ LDFLAGS='-rpath /store/2b1-clang/lib' +grep /store/2b0-musl/lib/libc.so cpp_test +( ! grep /store/2a3-intermediate-musl/lib/libc.so cpp_test ) +( ! grep ld-linux cpp_test ) +./cpp_test +[ "$(./cpp_test)" == 'this is c++' ] + +echo "### $0: checking for build path leaks..." +( ! grep -rF /tmp/2b1 /store/2b1-clang ) + +touch /store/_2b1.test # indicator of successful completion diff --git a/06/recipes/_3b.test.sh b/06/recipes/_3b.test.sh new file mode 100755 index 0000000..bfc1af7 --- /dev/null +++ b/06/recipes/_3b.test.sh @@ -0,0 +1,54 @@ +#!/store/2b2-busybox/bin/ash + +set -uex + +export PATH='/store/2b2-busybox/bin' +export PATH="$PATH:/store/3a-pkg-config/bin" +export PATH="$PATH:/store/3b-nix/bin" + +export SHELL='/store/2b2-busybox/bin/ash' + +export PKG_CONFIG_PATH='' +#export PKG_CONFIG_PATH='/store/3a-openssl/lib64/pkgconfig' +#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-bzip2/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-sqlite/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-curl/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-editline/lib/pkgconfig" +#export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-xz/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-seccomp/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libarchive/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-libsodium/lib/pkgconfig" +export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/store/3a-lowdown/lib/pkgconfig" +LIBDIRS='' +#LIBDIRS="$(pkg-config --variable=libdir openssl)" +#LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir bzip2)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir sqlite3)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libcurl)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libeditline)" +#LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir liblzma)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libseccomp)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libarchive)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir libsodium)" +LIBDIRS="$LIBDIRS:$(pkg-config --variable=libdir lowdown)" +LIBDIRS="$LIBDIRS:/store/3a-boost/lib" +LIBDIRS="$LIBDIRS:/store/2b1-clang/lib" +export LD_LIBRARY_PATH=$LIBDIRS + +mkdir -p /tmp/_3b.test; cd /tmp/_3b.test + +echo "### $0: faking lots of stuff for nix to work..." +mkdir shelter +export HOME=/tmp/_3b.test/shelter +export USER=notauser +echo 'oh come on' >/dev/urandom + +echo "### $0: testing that derivation assumes a known input hash..." +nix repl > known-drv-hash.output <<\EOF + # see https://nixos.org/guides/nix-pills/our-first-derivation.html + derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } +EOF +grep -Fx '«derivation /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv»' \ + known-drv-hash.output + +rm -f /dev/urandom +touch /store/_3b.test # indicator of successful completion diff --git a/06/recipes/all-past-stage1.sh b/06/recipes/all-past-stage1.sh new file mode 100755 index 0000000..5853a31 --- /dev/null +++ b/06/recipes/all-past-stage1.sh @@ -0,0 +1,39 @@ +#!/store/1-stage1/protobusybox/bin/ash + +set -uex + +export SOURCE_DATE_EPOCH=0 + +/recipes/2a0-static-gnumake.sh +/recipes/2a1-static-binutils.sh +/recipes/2a2-static-gnugcc4-c.sh +/recipes/2a3-intermediate-musl.sh +/recipes/2a4-gnugcc4-cpp.sh +/recipes/2a5-gnugcc10.sh +/recipes/2a6-linux-headers.sh +/recipes/2a7-cmake.sh +/recipes/2a8-python.sh +/recipes/2a9-intermediate-clang.sh +/recipes/2b0-musl.sh +/recipes/2b1-clang.sh +/recipes/2b2-busybox.sh +/recipes/2b3-gnumake.sh +/recipes/3a-sqlite.sh +/recipes/3a-boost.sh +/recipes/3a-mbedtls.sh +/recipes/3a-pkg-config.sh +/recipes/3a-curl.sh +/recipes/3a-editline.sh +/recipes/3a-brotli.sh +/recipes/3a-gnugperf.sh +/recipes/3a-seccomp.sh +/recipes/3a-libarchive.sh +/recipes/3a-libsodium.sh +/recipes/3a-lowdown.sh +/recipes/3a-nlohmann-json.sh +/recipes/3b-busybox-static.sh +/recipes/3b-tinycc-static.sh +/recipes/3b-zig.sh +# /recipes/3b-nix.sh +# /recipes/4-rebootstrap-using-nix.sh +# /recipes/5-go-beyond-using-nix.sh diff --git a/06/seed.sh b/06/seed.sh new file mode 100755 index 0000000..fde76df --- /dev/null +++ b/06/seed.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -uex + +if [[ ! -e ./tcc-seed ]]; then + echo 'You need to supply a statically linked TinyCC as `tcc-seed`.' + echo -n 'You can `./compile-tcc-seed-with-nix.sh` ' + echo 'if you have `nix` and trust in me.' + exit 1 +fi + +rm -rf stage +mkdir -p stage/store +cp -raL --reflink=auto downloads recipes default.nix flake.nix stage/ + +# I'm too lazy to pass it through stage1 +sed -i "s|\$NPROC|$NPROC|" stage/recipes/*.sh + +DESTDIR=stage recipes/0-tcc-seed/seed.host-executed.sh # copy tcc-seed +DESTDIR=stage recipes/1-stage1/seed.host-executed.sh # unpack stage1 sources +# Everything past stage1 will unpack sources from downloads/ all by itself +# all the way until +cp -r using-nix stage/ diff --git a/06/using-nix/0-from-nixpkgs.nix b/06/using-nix/0-from-nixpkgs.nix new file mode 100644 index 0000000..71ebe41 --- /dev/null +++ b/06/using-nix/0-from-nixpkgs.nix @@ -0,0 +1,77 @@ +# This is to support building protosrc/tinycc from nixpkgs, see comment in 0.nix + +let + nixpkgs = import (builtins.fetchTarball { + name = "pinned-nixpkgs"; + url = "https://github.com/nixos/nixpkgs/archive/21f524672f25f8c3e7a0b5775e6505fee8fe43ce.tar.gz"; + sha256 = "sha256:00pwazjld0bj2sp33gwiz1h8krkyf2nyid7injv5cqz5bz5jjw99"; + }) { system = "x86_64-linux"; }; + + tinycc-unliberated = nixpkgs.pkgsStatic.tinycc; + + tinycc-liberated = derivation { + name = "tinycc-liberated"; + builder = "/bin/sh"; + args = [ "-uexc" '' + ${nixpkgs.pkgs.gnused}/bin/sed \ + 's|/nix/store/.\{32\}-|!nix!store/................................-|g' \ + < ${tinycc-unliberated}/bin/tcc \ + > $out + ! ${nixpkgs.pkgs.gnugrep}/bin/grep -i /nix/store $out + ${nixpkgs.pkgs.coreutils}/bin/chmod +x $out + '']; + allowedReferences = [ ]; + allowedRequisites = [ ]; + system = "x86_64-linux"; + __contentAddressed = true; + outputHashAlgo = "sha256"; outputHashMode = "recursive"; + outputHash = "sha256-oqeOU6SFYDwpdIj8MjcQ+bMuU63CHyoV9NYdyPLFxEQ="; + }; + + source-tarball-musl = builtins.fetchurl { + url = "http://musl.libc.org/releases/musl-1.2.4.tar.gz"; + sha256 = "7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039"; + }; + + source-tarball-busybox = builtins.fetchurl { + url = "https://busybox.net/downloads/busybox-1.36.1.tar.bz2"; + sha256 = "b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314"; + }; + + source-tarball-tinycc = builtins.fetchurl { + url = "https://github.com/TinyCC/tinycc/archive/af1abf1f45d45b34f0b02437f559f4dfdba7d23c.tar.gz"; + sha256 = "sha256:0kkaax6iw28d9wl6sf14kn0gmwm0g5h9qmx9rm3awh23cq2iv9zm"; + }; + + protosrc = derivation { + name = "protosrc"; + builder = "/bin/sh"; + args = [ "-uexc" '' + PATH=${nixpkgs.coreutils}/bin + PATH=$PATH:${nixpkgs.gnused}/bin + PATH=$PATH:${nixpkgs.gnutar}/bin + PATH=$PATH:${nixpkgs.gzip}/bin + PATH=$PATH:${nixpkgs.bzip2}/bin + export PATH + mkdir downloads/ + cp ${source-tarball-musl} downloads/musl-1.2.4.tar.gz + cp ${source-tarball-busybox} downloads/busybox-1.36.1.tar.bz2 + cp ${source-tarball-tinycc} downloads/tinycc-mob-af1abf1.tar.gz + mkdir -p recipes + cp -r ${../recipes/1-stage1} recipes/1-stage1 + DESTDIR=$out ${nixpkgs.bash}/bin/bash \ + ${../recipes/1-stage1/seed.host-executed.sh} + mv $out/protosrc/* $out/; rm -d $out/protosrc + '']; + allowedReferences = [ ]; + allowedRequisites = [ ]; + system = "x86_64-linux"; + __contentAddressed = true; + outputHashAlgo = "sha256"; outputHashMode = "recursive"; + outputHash = "sha256-upUZTTumJgBY16waF6L8ZeWbflSuQL9TMmwLw0YEDqM="; + }; +in + { + tinycc = tinycc-liberated; + inherit protosrc; + } diff --git a/06/using-nix/0-prebuilt.nix b/06/using-nix/0-prebuilt.nix new file mode 100644 index 0000000..952e20e --- /dev/null +++ b/06/using-nix/0-prebuilt.nix @@ -0,0 +1,28 @@ +# This is to prefetch protosrc/tinycc from github, see comment in 0.nix + +let + fetchTarball = { name, url, sha256 }: derivation { + inherit name url; + urls = [ url ]; + unpack = true; + + builder = "builtin:fetchurl"; + system = "builtin"; + outputHashMode = "recursive"; outputHashAlgo = "sha256"; + preferLocalBuild = true; + outputHash = sha256; + }; +in + { + protosrc = fetchTarball { + name = "protosrc"; + url = "https://github.com/ZilchOS/bootstrap-from-tcc/releases/download/seeding-files-r004/protosrc.nar"; + sha256 = "sha256-upUZTTumJgBY16waF6L8ZeWbflSuQL9TMmwLw0YEDqM="; + }; + + tinycc = fetchTarball { + name = "tinycc-liberated"; + url = "https://github.com/ZilchOS/bootstrap-from-tcc/releases/download/seeding-files-r004/tinycc-liberated.nar"; + sha256 = "sha256-oqeOU6SFYDwpdIj8MjcQ+bMuU63CHyoV9NYdyPLFxEQ="; + }; + } diff --git a/06/using-nix/0.nix b/06/using-nix/0.nix new file mode 100644 index 0000000..9852426 --- /dev/null +++ b/06/using-nix/0.nix @@ -0,0 +1,35 @@ +# Where do tcc-seed and protosrc come from if you build with Nix? + +# When building with `make` or `build.sh` you'll have tcc-seed and protosrc +# long long before you have Nix, +# so there's no question of where to take them from, you just inject'em. +# In this case this file isn't used at all and a simpler 0.nix is generated, +# see recipes/4-rebootstrap-using-nix.sh + +# But not everyone wants to go the full bootstrap route. +# This file is for when you already have Nix and want to jump into the middle, +# starting from the second, `using-nix` half of the bootstrap. +# Cases like hydra or flake-building. + +# One option is to build them using nixpkgs (see 0-from-nixpkgs.nix), +# but then you need nixpkgs, IFD and stuff. + +# Alternatively we could download them prebuilt from github:ZilchOS, +# but then there's the question of falling back to another method +# when recipes/1-stage1/seed.host-executed.sh or recipes/1-stage1/syscall.h +# are updated. + +# Here's one weird combined approach: + +let + and = builtins.all (x: x); + syscall_h_ours = "${../recipes/1-stage1/syscall.h}"; + syscall_h_reference = "/nix/store/678g5j997qzp0srprfg4gqqxcp8mr3g9-syscall.h"; + syscall_h_is_unmodified = (syscall_h_ours == syscall_h_reference); + stage1_seeder_ours = "${../recipes/1-stage1/seed.host-executed.sh}"; + stage1_seeder_reference = "/nix/store/qv4rmbdclws5nrx3m1vw1pb98qacw226-seed.host-executed.sh"; + stage1_seeder_is_unmodified = (stage1_seeder_ours == stage1_seeder_reference); +in + if (and [ syscall_h_is_unmodified stage1_seeder_is_unmodified ]) + then import ./0-prebuilt.nix + else import ./0-from-nixpkgs.nix diff --git a/06/using-nix/1-stage1.nix b/06/using-nix/1-stage1.nix new file mode 100644 index 0000000..4ae0b7c --- /dev/null +++ b/06/using-nix/1-stage1.nix @@ -0,0 +1,26 @@ +{ tcc-seed, protosrc, recipesStage1ExtrasPath, stage1cPath }: + +derivation { + name = "bootstrap-1-stage1"; + builder = "/bin/sh"; # purely to pass $vars, which is silly + args = [ "-c" '' + ${tcc-seed} \ + -nostdinc -nostdlib -Werror \ + -I${recipesStage1ExtrasPath} \ + -DINSIDE_NIX \ + -DPROTOSRC='"'${protosrc}'"' \ + -DTCC_SEED='"'${tcc-seed}'"' \ + -DRECIPES_STAGE1='"'${recipesStage1ExtrasPath}'"' \ + -DTMP_STAGE1='"'$TMPDIR/tmp'"' \ + -DSTORE_PROTOBUSYBOX='"'$protobusybox/'"' \ + -DSTORE_PROTOMUSL='"'$protomusl'"' \ + -DSTORE_TINYCC='"'$tinycc'"' \ + -run ${stage1cPath} + '']; + outputs = [ "protobusybox" "protomusl" "tinycc" ]; + allowedReferences = [ "protobusybox" "protomusl" "tinycc" ]; + allowedRequisites = [ "protobusybox" "protomusl" "tinycc" ]; + system = "x86_64-linux"; + __contentAddressed = true; + outputHashAlgo = "sha256"; outputHashMode = "recursive"; +} diff --git a/06/using-nix/2a0-static-gnumake.nix b/06/using-nix/2a0-static-gnumake.nix new file mode 100644 index 0000000..b32448d --- /dev/null +++ b/06/using-nix/2a0-static-gnumake.nix @@ -0,0 +1,70 @@ +{ fetchurl, mkDerivationStage2, stage1 }: + +let + source-tarball-gnumake = fetchurl { + # local = /downloads/make-4.4.1.tar.gz; + url = "http://ftp.gnu.org/gnu/make/make-4.4.1.tar.gz"; + sha256 = "dd16fb1d67bfab79a72f5e8390735c49e3e8e70b4945a15ab1f81ddb78658fb3"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a0-static-gnumake"; + buildInputPaths = [ + "${stage1.tinycc}/wrappers" + "${stage1.protobusybox}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # unpack: + unpack ${source-tarball-gnumake} + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' \ + src/job.c build-aux/install-sh po/Makefile.in.in + # this is part of stdlib, no idea how it's supposed to not clash + rm src/getopt.h + for f in src/getopt.c src/getopt1.c lib/fnmatch.c; do :> $f; done + for f in lib/glob.c lib/xmalloc.c lib/error.c; do :> $f; done + # embrace chaos + shuffle_comment='\/\* Handle shuffle mode argument. \*\/' + shuffle_default='if (!shuffle_mode) shuffle_mode = xstrdup(\"random\");' + sed -i "s|$shuffle_comment|$shuffle_comment\n$shuffle_default|" \ + src/main.c + grep 'if (!shuffle_mode) shuffle_mode = xstrdup("random");' src/main.c + # configure: + ash ./configure \ + --build x86_64-linux \ + --disable-dependency-tracking \ + --prefix=$out \ + CONFIG_SHELL='${stage1.protobusybox}/bin/ash' \ + SHELL='${stage1.protobusybox}/bin/ash' + # bootstrap build: + ash ./build.sh + # test static GNU Make by remaking it with itself: + mv make make-intermediate + ./make-intermediate -j $NPROC clean + ./make-intermediate -j $NPROC + # reconfigure: + ash ./configure \ + --build x86_64-linux \ + --disable-dependency-tracking \ + --prefix=$out \ + CONFIG_SHELL='${stage1.protobusybox}/bin/ash' \ + SHELL='${stage1.protobusybox}/bin/ash' + # rebuild: + ash ./build.sh + # test: + mv make make-intermediate + ./make-intermediate -j $NPROC clean + ./make-intermediate -j $NPROC CFLAGS=-O2 + # install: + ./make -j $NPROC install + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + # wrap: + # FIXME: patch make to use getenv? + mkdir -p $out/wrappers; cd $out/wrappers + echo "#!${stage1.protobusybox}/bin/ash" > make + echo "exec $out/bin/make SHELL=\$SHELL \"\$@\"" \ >> make + chmod +x make + ''; + } diff --git a/06/using-nix/2a1-static-binutils.nix b/06/using-nix/2a1-static-binutils.nix new file mode 100644 index 0000000..e98189b --- /dev/null +++ b/06/using-nix/2a1-static-binutils.nix @@ -0,0 +1,56 @@ +{ fetchurl, mkDerivationStage2, stage1, static-gnumake }: + +let + source-tarball-binutils = fetchurl { + # local = /downloads/binutils-2.39.tar.xz; + url = "https://ftp.gnu.org/gnu/binutils/binutils-2.39.tar.xz"; + sha256 = "645c25f563b8adc0a81dbd6a41cffbf4d37083a382e02d5d3df4f65c09516d00"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a1-static-binutils"; + buildInputPaths = [ + "${stage1.tinycc}/wrappers" + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # unpack: + unpack ${source-tarball-binutils} + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' \ + missing install-sh mkinstalldirs + # see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 + sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh + sed -i 's|__FILE__|"__FILE__"|' \ + ld/*.c ld/*.h bfd/*.* libctf/*.* opcodes/*.* + sed -i 's| -g | |' ld/Makefile* + # alias makeinfo to true + mkdir aliases + ln -s ${stage1.protobusybox}/bin/true aliases/makeinfo + PATH="$(pwd)/aliases/:$PATH" + # configure: + export lt_cv_sys_max_cmd_len=32768 + export ac_cv_func_strncmp_works=no + ash ./configure \ + CONFIG_SHELL='${stage1.protobusybox}/bin/ash' \ + SHELL='${stage1.protobusybox}/bin/ash' \ + CFLAGS='-O2 -D__LITTLE_ENDIAN__=1' \ + CFLAGS_FOR_TARGET=-O2 \ + --enable-deterministic-archives \ + --disable-gprofng \ + --host x86_64-linux --build x86_64-linux \ + --prefix=$out + # build: + make -j $NPROC \ + all-libiberty all-gas all-bfd all-libctf all-zlib all-gprof + make all-ld # race condition on ld/.deps/ldwrite.Po, serialize + make -j $NPROC + # install: + make -j $NPROC install + rm $out/lib/*.la # broken, reference builddir + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a2-static-gnugcc4-c.nix b/06/using-nix/2a2-static-gnugcc4-c.nix new file mode 100644 index 0000000..45dea0e --- /dev/null +++ b/06/using-nix/2a2-static-gnugcc4-c.nix @@ -0,0 +1,85 @@ +{ fetchurl, mkDerivationStage2, stage1, static-gnumake, static-binutils }: + +let + source-tarball-gcc = fetchurl { + # local = /downloads/gcc-4.7.4.tar.bz2; + url = "https://ftp.gnu.org/gnu/gcc/gcc-4.7.4/gcc-4.7.4.tar.bz2"; + sha256 = "92e61c6dc3a0a449e62d72a38185fda550168a86702dea07125ebd3ec3996282"; + }; + source-tarball-gmp = fetchurl { + # local = /downloads/gmp-4.3.2.tar.xz; + url = "https://gmplib.org/download/gmp/archive/gmp-4.3.2.tar.xz"; + sha256 = "f69eff1bc3d15d4e59011d587c57462a8d3d32cf2378d32d30d008a42a863325"; + }; + source-tarball-mpfr = fetchurl { + # local = /downloads/mpfr-2.4.2.tar.xz; + url = "https://www.mpfr.org/mpfr-2.4.2/mpfr-2.4.2.tar.xz"; + sha256 = "d7271bbfbc9ddf387d3919df8318cd7192c67b232919bfa1cb3202d07843da1b"; + }; + source-tarball-mpc = fetchurl { + # local = /downloads/mpc-0.8.1.tar.gz; + url = "http://www.multiprecision.org/downloads/mpc-0.8.1.tar.gz"; + sha256 = "e664603757251fd8a352848276497a4c79b7f8b21fd8aedd5cc0598a38fee3e4"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a2-static-gnugcc4-c"; + buildInputPaths = [ + "${stage1.tinycc}/wrappers" + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # alias ash to sh: + mkdir aliases; ln -s ${stage1.protobusybox}/bin/ash aliases/sh + export PATH="$(pwd)/aliases:$PATH" + # unpack: + unpack ${source-tarball-gcc} + mkdir mpfr mpc gmp + unpack ${source-tarball-mpfr} -C mpfr + unpack ${source-tarball-mpc} -C mpc + unpack ${source-tarball-gmp} -C gmp + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' \ + missing move-if-change mkdep mkinstalldirs symlink-tree \ + gcc/genmultilib */*.sh gcc/exec-tool.in \ + install-sh */install-sh + sed -i 's|^\(\s*\)sh |\1${stage1.protobusybox}/bin/ash |' \ + Makefile* */Makefile* + sed -i 's|LIBGCC2_DEBUG_CFLAGS = -g|LIBGCC2_DEBUG_CFLAGS = |' \ + libgcc/Makefile.in + # see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 + sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh */ltmain.sh + sed -i 's|#define HAVE_HOST_CORE2 1||' mpfr/configure + # configure: + export ac_cv_func_strncmp_works=no + export ac_cv_func_alloca_works=no + export ac_cv_prog_make_make_set=no + ash configure \ + CONFIG_SHELL='${stage1.protobusybox}/bin/ash' \ + SHELL='${stage1.protobusybox}/bin/ash' \ + CFLAGS=-O2 CFLAGS_FOR_TARGET=-O2 \ + --with-sysroot=${stage1.protomusl} \ + --with-native-system-header-dir=/include \ + --with-build-time-tools=${static-binutils}/bin \ + --prefix=$out \ + --enable-languages=c \ + --disable-bootstrap \ + --disable-libquadmath --disable-decimal-float --disable-fixed-point \ + --disable-lto \ + --disable-libgomp \ + --disable-multilib \ + --disable-multiarch \ + --disable-libmudflap \ + --disable-libssp \ + --disable-nls \ + --host x86_64-linux --build x86_64-linux + # build: + make -j $NPROC + # install: + make -j $NPROC install + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a3-intermediate-musl.nix b/06/using-nix/2a3-intermediate-musl.nix new file mode 100644 index 0000000..794bf22 --- /dev/null +++ b/06/using-nix/2a3-intermediate-musl.nix @@ -0,0 +1,42 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, static-binutils, static-gnugcc4-c }: + +let + source-tarball-musl = fetchurl { + # local = /downloads/musl-1.2.4.tar.gz; + url = "http://musl.libc.org/releases/musl-1.2.4.tar.gz"; + sha256 = "7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a3-intermediate-musl"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + "${static-binutils}/bin" + "${static-gnugcc4-c}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # unpack: + unpack ${source-tarball-musl} + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' \ + tools/*.sh \ + # patch popen/system to search in PATH instead of hardcoding /bin/sh + sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \ + src/stdio/popen.c src/process/system.c + sed -i 's|execl("/bin/sh", "sh", "-c",|execlp("sh", "-c",|'\ + src/misc/wordexp.c + # eliminate a source path reference + sed -i 's/__FILE__/"__FILE__"/' include/assert.h + # configure: + ash ./configure --prefix=$out + # build: + make -j $NPROC + # install: + make -j $NPROC install + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a4-gnugcc4-cpp.nix b/06/using-nix/2a4-gnugcc4-cpp.nix new file mode 100644 index 0000000..038ab20 --- /dev/null +++ b/06/using-nix/2a4-gnugcc4-cpp.nix @@ -0,0 +1,113 @@ +{ fetchurl, mkDerivationStage2 +, stage1 +, static-gnumake, static-binutils, static-gnugcc4-c, intermediate-musl }: + +let + source-tarball-gcc = fetchurl { + # local = /downloads/gcc-4.7.4.tar.bz2; + url = "https://ftp.gnu.org/gnu/gcc/gcc-4.7.4/gcc-4.7.4.tar.bz2"; + sha256 = "92e61c6dc3a0a449e62d72a38185fda550168a86702dea07125ebd3ec3996282"; + }; + source-tarball-gmp = fetchurl { + # local = /downloads/gmp-4.3.2.tar.xz; + url = "https://gmplib.org/download/gmp/archive/gmp-4.3.2.tar.xz"; + sha256 = "f69eff1bc3d15d4e59011d587c57462a8d3d32cf2378d32d30d008a42a863325"; + }; + source-tarball-mpfr = fetchurl { + # local = /downloads/mpfr-2.4.2.tar.xz; + url = "https://www.mpfr.org/mpfr-2.4.2/mpfr-2.4.2.tar.xz"; + sha256 = "d7271bbfbc9ddf387d3919df8318cd7192c67b232919bfa1cb3202d07843da1b"; + }; + source-tarball-mpc = fetchurl { + # local = /downloads/mpc-0.8.1.tar.gz; + url = "http://www.multiprecision.org/downloads/mpc-0.8.1.tar.gz"; + sha256 = "e664603757251fd8a352848276497a4c79b7f8b21fd8aedd5cc0598a38fee3e4"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a4-gnugcc4-cpp"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + "${static-binutils}/bin" + "${static-gnugcc4-c}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # alias ash to sh: + mkdir aliases; ln -s ${stage1.protobusybox}/bin/ash aliases/sh + export PATH="$(pwd)/aliases:$PATH" + # create wrappers that make previous GNU GCC target new musl: + SYSROOT=${intermediate-musl} + export _SYSROOT="--sysroot $SYSROOT" + export _LDFLAG="--dynamic-linker=$SYSROOT/lib/libc.so" + export _NEWINC="-I$SYSROOT/include" + export _REALCC="-I$SYSROOT/include" + mkdir wrappers + echo '#!${stage1.protobusybox}/bin/ash' > wrappers/cc + echo '#!${stage1.protobusybox}/bin/ash' > wrappers/cpp + echo '#!${stage1.protobusybox}/bin/ash' > wrappers/ld + echo 'exec gcc $_SYSROOT -Wl,$_LDFLAG "$@"' >> wrappers/cc + echo 'exec ${static-gnugcc4-c}/bin/cpp $_NEWINC "$@"' \ + >> wrappers/cpp + echo 'exec ${static-binutils}/bin/ld $_LDFLAG "$@"' \ + >> wrappers/ld + chmod +x wrappers/cc wrappers/cpp wrappers/ld + export PATH="$(pwd)/wrappers:$PATH" + # unpack: + unpack ${source-tarball-gcc} + mkdir mpfr mpc gmp + unpack ${source-tarball-mpfr} -C mpfr + unpack ${source-tarball-mpc} -C mpc + unpack ${source-tarball-gmp} -C gmp + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' \ + missing move-if-change mkdep mkinstalldirs symlink-tree \ + gcc/genmultilib */*.sh gcc/exec-tool.in \ + install-sh */install-sh + sed -i 's|^\(\s*\)sh |\1${stage1.protobusybox}/bin/ash |' \ + Makefile* */Makefile* + sed -i "s|/lib64/ld-linux-x86-64.so.2|$SYSROOT/lib/libc.so|" \ + gcc/config/i386/linux64.h + sed -i 's|"os/gnu-linux"|"os/generic"|' libstdc++-v3/configure.host + sed -i 's|LIBGCC2_DEBUG_CFLAGS = -g|LIBGCC2_DEBUG_CFLAGS = |' \ + libgcc/Makefile.in + # see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 + sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh */ltmain.sh + sed -i 's|#define HAVE_HOST_CORE2 1||' mpfr/configure + # configure: + export ac_cv_func_strncmp_works=no + export ac_cv_func_alloca_works=no + export ac_cv_prog_make_make_set=no + export glibcxx_cv_random_tr1=no + ash configure \ + cache_file=nonex \ + CONFIG_SHELL='${stage1.protobusybox}/bin/ash' \ + SHELL='${stage1.protobusybox}/bin/ash' \ + CC=cc CPP=cpp LD=ld \ + CFLAGS=-O2 CFLAGS_FOR_TARGET=-O2 \ + --with-sysroot=$SYSROOT \ + --with-native-system-header-dir=/include \ + --with-build-time-tools=${static-binutils}/bin \ + --prefix=$out \ + --with-specs='%{!static:%x{-rpath=$out/lib64}}' \ + --enable-languages=c,c++ \ + --disable-bootstrap \ + --disable-libquadmath --disable-decimal-float --disable-fixed-point \ + --disable-lto \ + --disable-libgomp \ + --disable-multilib \ + --disable-multiarch \ + --disable-libmudflap \ + --disable-libssp \ + --disable-nls \ + --disable-libitm \ + --host x86_64-linux --build x86_64-linux + # build: + make -j $NPROC + # install: + make -j $NPROC install + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a5-gnugcc10.nix b/06/using-nix/2a5-gnugcc10.nix new file mode 100644 index 0000000..3b71b23 --- /dev/null +++ b/06/using-nix/2a5-gnugcc10.nix @@ -0,0 +1,104 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, static-binutils, intermediate-musl, gnugcc4-cpp }: + +let + source-tarball-gcc = fetchurl { + # local = /downloads/gcc-10.5.0.tar.xz; + url = "https://ftp.gnu.org/gnu/gcc/gcc-10.5.0/gcc-10.5.0.tar.xz"; + sha256 = "25109543fdf46f397c347b5d8b7a2c7e5694a5a51cce4b9c6e1ea8a71ca307c1"; + }; + source-tarball-gmp = fetchurl { + # local = /downloads/gmp-6.1.0.tar.xz; + url = "https://gmplib.org/download/gmp/gmp-6.1.0.tar.xz"; + sha256 = "68dadacce515b0f8a54f510edf07c1b636492bcdb8e8d54c56eb216225d16989"; + }; + source-tarball-mpfr = fetchurl { + # local = /downloads/mpfr-3.1.4.tar.xz; + url = "https://www.mpfr.org/mpfr-3.1.4/mpfr-3.1.4.tar.xz"; + sha256 = "761413b16d749c53e2bfd2b1dfaa3b027b0e793e404b90b5fbaeef60af6517f5"; + }; + source-tarball-mpc = fetchurl { + # local = /downloads/mpc-1.0.3.tar.gz; + url = "http://www.multiprecision.org/downloads/mpc-1.0.3.tar.gz"; + sha256 = "617decc6ea09889fb08ede330917a00b16809b8db88c29c31bfbb49cbf88ecc3"; + }; + source-tarball-isl = fetchurl { + # local = /downloads/isl-0.18.tar.bz2; + url = "http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2"; + sha256 = "6b8b0fd7f81d0a957beb3679c81bbb34ccc7568d5682844d8924424a0dadcb1b"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a5-gnugcc10"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + "${static-binutils}/bin" + "${gnugcc4-cpp}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # alias ash to sh: + mkdir aliases; ln -s ${stage1.protobusybox}/bin/ash aliases/sh + export PATH="$(pwd)/aliases:$PATH" + # unpack: + unpack ${source-tarball-gcc} + mkdir mpfr mpc gmp isl + unpack ${source-tarball-mpfr} -C mpfr + unpack ${source-tarball-mpc} -C mpc + unpack ${source-tarball-gmp} -C gmp + unpack ${source-tarball-isl} -C isl + # fixup: + SYSROOT=${intermediate-musl} + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' \ + missing move-if-change mkdep mkinstalldirs symlink-tree install-sh \ + gcc/exec-tool.in gcc/genmultilib libgcc/mkheader.sh + sed -i 's|^\(\s*\)sh |\1${stage1.protobusybox}/bin/ash |' \ + libgcc/Makefile.in + sed -i "s|/lib/ld-musl-x86_64.so.1|$SYSROOT/lib/libc.so|" \ + gcc/config/i386/linux64.h + sed -i 's|m64=../lib64|m64=../lib|' gcc/config/i386/t-linux64 + sed -i 's|"os/gnu-linux"|"os/generic"|' libstdc++-v3/configure.host + sed -i 's|LIBGCC2_DEBUG_CFLAGS = -g|LIBGCC2_DEBUG_CFLAGS = |' \ + libgcc/Makefile.in + # see libtool's 74c8993c178a1386ea5e2363a01d919738402f30 + sed -i 's/| \$NL2SP/| sort | $NL2SP/' ltmain.sh */ltmain.sh + # configure: + export ac_cv_func_strncmp_works=no + export ac_cv_prog_make_make_set=no + export glibcxx_cv_dev_random=no + ash configure \ + CONFIG_SHELL='${stage1.protobusybox}/bin/ash' \ + SHELL='${stage1.protobusybox}/bin/ash' \ + CFLAGS=-O2 CXXFLAGS=-O2 \ + CFLAGS_FOR_TARGET=-O2 CXXFLAGS_FOR_TARGET=-O2 \ + --with-sysroot=$SYSROOT \ + --with-native-system-header-dir=/include \ + --with-build-time-tools=${static-binutils}/bin \ + --prefix=$out \ + --with-specs='%{!static:%x{-rpath=$out/lib}}' \ + --enable-languages=c,c++ \ + --disable-bootstrap \ + --disable-libquadmath --disable-decimal-float --disable-fixed-point \ + --disable-lto \ + --disable-libgomp \ + --disable-multilib \ + --disable-multiarch \ + --disable-libmudflap \ + --disable-libssp \ + --disable-nls \ + --disable-libitm \ + --disable-libsanitizer \ + --disable-cet \ + --disable-gnu-unique-object \ + --disable-gcov \ + --disable-checking \ + --host x86_64-linux-musl --build x86_64-linux-musl + # build: + make -j $NPROC + # install: + make -j $NPROC install + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a6-linux-headers.nix b/06/using-nix/2a6-linux-headers.nix new file mode 100644 index 0000000..72344fa --- /dev/null +++ b/06/using-nix/2a6-linux-headers.nix @@ -0,0 +1,39 @@ +{ fetchurl, mkDerivationStage2, stage1, static-gnumake, static-binutils, gnugcc10 }: + +let + source-tarball-linux = fetchurl { + # local = /downloads/linux-6.4.12.tar.xz; + url = "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.12.tar.xz"; + sha256 = "cca91be956fe081f8f6da72034cded96fe35a50be4bfb7e103e354aa2159a674"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a6-linux-headers"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + "${static-binutils}/bin" + "${gnugcc10}/bin" + ]; + script = '' + # unpack: + mkdir build-dir; cd build-dir + unpack ${source-tarball-linux} \ + linux-6.4.12/Makefile \ + linux-6.4.12/arch/x86 \ + linux-6.4.12/include \ + linux-6.4.12/scripts \ + linux-6.4.12/tools + # build: + make -j $NPROC \ + CONFIG_SHELL=${stage1.protobusybox}/bin/ash \ + CC=gcc HOSTCC=gcc ARCH=x86_64 \ + headers + # install: + find usr/include -name '.*' | xargs rm + mkdir -p $out + cp -rv usr/include $out/ + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a7-cmake.nix b/06/using-nix/2a7-cmake.nix new file mode 100644 index 0000000..90d3706 --- /dev/null +++ b/06/using-nix/2a7-cmake.nix @@ -0,0 +1,51 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, static-binutils, gnugcc10, linux-headers }: + +let + source-tarball-cmake = fetchurl { + # local = /downloads/cmake-3.27.4.tar.gz; + url = "https://github.com/Kitware/CMake/releases/download/v3.27.4/cmake-3.27.4.tar.gz"; + sha256 = "0a905ca8635ca81aa152e123bdde7e54cbe764fdd9a70d62af44cad8b92967af"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a7-cmake"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/wrappers" + "${static-binutils}/bin" + "${gnugcc10}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + export SHELL=${stage1.protobusybox}/bin/ash + # unpack: + unpack ${source-tarball-cmake} + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' bootstrap + sed -i 's|__FILE__|"__FILE__"|' \ + Source/CPack/IFW/cmCPackIFWCommon.h \ + Source/CPack/cmCPack*.h \ + Source/cmCTest.h + # bundle libraries: + # poor man's static linking, a way for cmake to be self-contained later + mkdir -p $out/bundled-runtime + cp -H ${gnugcc10}/lib/libstdc++.so.6 $out/bundled-runtime/ + cp -H ${gnugcc10}/lib/libgcc_s.so.1 $out/bundled-runtime/ + # configure: + ash configure \ + CFLAGS="-DCPU_SETSIZE=128 -D_GNU_SOURCE" \ + CXXFLAGS="-isystem ${linux-headers}/include" \ + LDFLAGS="-Wl,-rpath $out/bundled-runtime" \ + --prefix=$out \ + --parallel=$NPROC \ + -- \ + -DCMAKE_USE_OPENSSL=OFF + # build: + make -j $NPROC + # install: + make -j $NPROC install/strip + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2a8-python.nix b/06/using-nix/2a8-python.nix new file mode 100644 index 0000000..f8ae622 --- /dev/null +++ b/06/using-nix/2a8-python.nix @@ -0,0 +1,74 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, static-binutils, gnugcc10 }: + +let + source-tarball-python = fetchurl { + # local = /downloads/Python-3.12.0.tar.xz; + url = "https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tar.xz"; + sha256 = "795c34f44df45a0e9b9710c8c71c15c671871524cd412ca14def212e8ccb155d"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a8-python"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/wrappers" + "${static-binutils}/bin" + "${gnugcc10}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + export SHELL=${stage1.protobusybox}/bin/ash + # alias ash to sh: + mkdir aliases; ln -s ${stage1.protobusybox}/bin/ash aliases/sh + export PATH="$(pwd)/aliases:$PATH" + # unpack: + unpack ${source-tarball-python} + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' configure install-sh + sed -i 's|ac_sys_system=`uname -s`|ac_sys_system=Linux|' configure + # the precompiled pyc files aren't reproducible, + # but it's not like I need to waste time on them anyway. + # break their generation + mv Lib/compileall.py Lib/compileall.py.bak + echo 'import sys; sys.exit(0)' > Lib/compileall.py + chmod +x Lib/compileall.py + sed -i 's|__FILE__|"__FILE__"|' \ + Python/errors.c \ + Include/pyerrors.h \ + Include/cpython/object.h \ + Modules/pyexpat.c + sed -i 's|TIME __TIME__|TIME "xx:xx:xx"|' Modules/getbuildinfo.c + sed -i 's|DATE __DATE__|DATE "xx/xx/xx"|' Modules/getbuildinfo.c + # different build path length leads to different wrapping. avoid + sed -i 's|vars, stream=f|vars, stream=f, width=2**24|' Lib/sysconfig.py + # configure: + mkdir -p $out/lib + ash configure \ + ac_cv_broken_sem_getvalue=yes \ + ac_cv_posix_semaphores_enabled=no \ + OPT='-DNDEBUG -fwrapv -O3 -Wall' \ + LDFLAGS="-Wl,-rpath $out/lib" \ + --without-static-libpython \ + --build x86_64-linux-musl \ + --prefix=$out \ + --enable-shared \ + --with-ensurepip=no + # ensure reproducibility in case of no /dev/shm + grep 'define POSIX_SEMAPHORES_NOT_ENABLED 1' pyconfig.h + grep 'define HAVE_BROKEN_SEM_GETVALUE 1' pyconfig.h + # build: + make -j $NPROC + # install: + make -j $NPROC install + # strip builddir mentions: + sed -i "s|$(pwd)|...|g" \ + $out/lib/python3.*/_sysconfigdata__*.py \ + $out/lib/python3.*/config-3.*-x86_64-linux-musl/Makefile + # restore compileall just in case + cat Lib/compileall.py.bak > $out/lib/python3.12/compileall.py + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } + diff --git a/06/using-nix/2a9-intermediate-clang.nix b/06/using-nix/2a9-intermediate-clang.nix new file mode 100644 index 0000000..7d3696d --- /dev/null +++ b/06/using-nix/2a9-intermediate-clang.nix @@ -0,0 +1,145 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, static-binutils, intermediate-musl, gnugcc10 +, linux-headers, cmake, python}: + +let + source-tarball-llvm = fetchurl { + # local = /downloads/llvm-project-17.0.1.src.tar.xz; + url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.1/llvm-project-17.0.1.src.tar.xz"; + sha256 = "b0e42aafc01ece2ca2b42e3526f54bebc4b1f1dc8de6e34f46a0446a13e882b9"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2a9-intermediate-clang"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/wrappers" + "${static-binutils}/bin" + "${gnugcc10}/bin" + "${cmake}/bin" + "${python}/bin" + ]; + script = '' + # Shared libs are not relinked on install. Instead, their rpath + # is erased with RPATH_SET: `Set runtime path of + # "/nix/store/.../lib/x86_64-unknown-linux-musl/libc++.so.1.0" to ""` + # One (hacky) workaround to that is using a constant-len build-dir. + build_dir=build-dir; expr "$(pwd)/$build_dir)" '<=' 128 + while ! echo "$(pwd)/$build_dir" | wc -c | grep -Fqx 128; do + build_dir="$build_dir." + done; expr "$(echo $(pwd)/$build_dir | wc -c)" '==' 128 + mkdir $build_dir; cd $build_dir + export SHELL=${stage1.protobusybox}/bin/ash + # llvm cmake configuration should pick up ccache automatically from PATH + export PATH="$PATH:/ccache/bin" + command -v ccache && USE_CCACHE=YES || USE_CCACHE=NO + # prepare future sysroot: + SYSROOT=$out/sysroot + mkdir -p $SYSROOT/lib $SYSROOT/include + ln -s ${intermediate-musl}/lib/* $SYSROOT/lib/ + ln -s ${intermediate-musl}/include/* $SYSROOT/include/ + # unpack: + unpack ${source-tarball-llvm} + # fixup: + sed -i "s|COMMAND sh|COMMAND ${stage1.protobusybox}/bin/ash|" \ + llvm/cmake/modules/GetHostTriple.cmake clang/CMakeLists.txt + echo 'echo x86_64-unknown-linux-musl' > llvm/cmake/config.guess + LOADER=${intermediate-musl}/lib/libc.so + sed -i "s|/lib/ld-musl-\" + ArchName + \".so.1|$LOADER|" \ + clang/lib/Driver/ToolChains/Linux.cpp + BEGINEND='const bool HasCRTBeginEndFiles' + sed -i "s|$BEGINEND =|$BEGINEND = false; ''${BEGINEND}_unused =|" \ + clang/lib/Driver/ToolChains/Gnu.cpp + REL_ORIGIN='_install_rpath \"\$ORIGIN/../lib''${LLVM_LIBDIR_SUFFIX}\"' + sed -i "s|_install_rpath \"\\\\\$ORIGIN/..|_install_rpath \"$out|" \ + llvm/cmake/modules/AddLLVM.cmake + sed -i 's|numShards = 32;|numShards = 1;|' lld/*/SyntheticSections.* + sed -i 's|numShards = 256;|numShards = 1;|' lld/*/ICF.cpp + sed -i 's|__FILE__|"__FILE__"|' \ + libcxx/src/verbose_abort.cpp \ + libcxxabi/src/abort_message.cpp \ + compiler-rt/lib/builtins/int_util.h + sed -i 's|"@LLVM_SRC_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in + sed -i 's|"@LLVM_OBJ_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in + # figure out includes: + EXTRA_INCL="$(pwd)/extra_includes" + mkdir -p $EXTRA_INCL + cp clang/lib/Headers/*intrin*.h $EXTRA_INCL/ + cp clang/lib/Headers/mm_malloc.h $EXTRA_INCL/ + [ -e $EXTRA_INCL/immintrin.h ] + # configure: + export LD_LIBRARY_PATH=${gnugcc10}/lib + BOTH_STAGES_OPTS="" + add_opt() { + BOTH_STAGES_OPTS="$BOTH_STAGES_OPTS -D$1 -DBOOTSTRAP_$1" + } + add_opt CMAKE_BUILD_TYPE=MinSizeRel + add_opt LLVM_OPTIMIZED_TABLEGEN=YES + add_opt LLVM_CCACHE_BUILD=$USE_CCACHE + add_opt DEFAULT_SYSROOT=$SYSROOT + add_opt CMAKE_INSTALL_PREFIX=$out + add_opt LLVM_INSTALL_BINUTILS_SYMLINKS=YES + add_opt LLVM_INSTALL_CCTOOLS_SYMLINKS=YES + add_opt CMAKE_INSTALL_DO_STRIP=YES + add_opt LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=YES + add_opt LLVM_TARGET_ARCH=X86 + add_opt LLVM_TARGETS_TO_BUILD=Native + add_opt LLVM_BUILTIN_TARGETS=x86_64-unknown-linux-musl + add_opt LLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl + add_opt LLVM_HOST_TRIPLE=x86_64-unknown-linux-musl + add_opt COMPILER_RT_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl + add_opt LLVM_APPEND_VC_REV=NO + add_opt LLVM_INCLUDE_TESTS=NO + add_opt LLVM_INCLUDE_EXAMPLES=NO + add_opt LLVM_INCLUDE_BENCHMARKS=NO + add_opt LLVM_ENABLE_BACKTRACES=NO + add_opt LLVM_ENABLE_EH=YES + add_opt LLVM_ENABLE_RTTI=YES + add_opt CLANG_ENABLE_ARCMT=NO + add_opt CLANG_ENABLE_STATIC_ANALYZER=NO + add_opt COMPILER_RT_BUILD_SANITIZERS=NO + add_opt COMPILER_RT_BUILD_XRAY=NO + add_opt COMPILER_RT_BUILD_LIBFUZZER=NO + add_opt COMPILER_RT_BUILD_PROFILE=NO + add_opt COMPILER_RT_BUILD_MEMPROF=NO + add_opt COMPILER_RT_BUILD_ORC=NO + add_opt COMPILER_RT_USE_BUILTINS_LIBRARY=YES + add_opt CLANG_DEFAULT_CXX_STDLIB=libc++ + add_opt CLANG_DEFAULT_LINKER=lld + add_opt CLANG_DEFAULT_RTLIB=compiler-rt + add_opt LIBCXX_HAS_MUSL_LIBC=YES + add_opt LIBCXX_USE_COMPILER_RT=YES + add_opt LIBCXX_INCLUDE_BENCHMARKS=NO + add_opt LIBCXX_CXX_ABI=libcxxabi + add_opt LIBCXXABI_USE_COMPILER_RT=YES + add_opt LIBCXXABI_USE_LLVM_UNWINDER=YES + add_opt LIBCXX_ADDITIONAL_COMPILE_FLAGS=-I${linux-headers}/include + add_opt LLVM_INSTALL_TOOLCHAIN_ONLY=YES + add_opt LIBUNWIND_USE_COMPILER_RT=YES + add_opt LLVM_ENABLE_THREADS=NO + cmake -S llvm -B build -G 'Unix Makefiles' \ + -DLLVM_ENABLE_PROJECTS='clang;lld' \ + -DLLVM_ENABLE_RUNTIMES='compiler-rt;libcxx;libcxxabi;libunwind' \ + -DGCC_INSTALL_PREFIX=${gnugcc10} \ + "-DBOOTSTRAP_CMAKE_C_FLAGS=-isystem $EXTRA_INCL" \ + "-DBOOTSTRAP_CMAKE_CXX_FLAGS=-isystem $EXTRA_INCL" \ + -DCLANG_ENABLE_BOOTSTRAP=YES $BOTH_STAGES_OPTS + # build (stage1): + make -C build -j $NPROC clang lld runtimes + # build/install (stage2): + NEW_LIB_DIR="$(pwd)/build/lib/x86_64-unknown-linux-musl" + export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$NEW_LIB_DIR" + make -C build -j $NPROC stage2 stage2-install + ln -s $out/lib/x86_64-unknown-linux-musl/* $out/lib/ + mkdir -p $out/bin/generic-names + ln -s $out/bin/clang $out/bin/generic-names/cc + ln -s $out/bin/clang++ $out/bin/generic-names/c++ + ln -s $out/bin/clang-cpp $out/bin/generic-names/cpp + # mix new stuff into sysroot + ln -s $out/lib/* $out/sysroot/lib/ + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + } diff --git a/06/using-nix/2b0-musl.nix b/06/using-nix/2b0-musl.nix new file mode 100644 index 0000000..ac74359 --- /dev/null +++ b/06/using-nix/2b0-musl.nix @@ -0,0 +1,45 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, intermediate-clang }: + +let + source-tarball-musl = fetchurl { + # local = /downloads/musl-1.2.4.tar.gz; + url = "http://musl.libc.org/releases/musl-1.2.4.tar.gz"; + sha256 = "7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2b0-musl"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + "${intermediate-clang}/bin" + "${intermediate-clang}/bin/generic-names" + ]; + script = '' + # unpack: + mkdir build-dir; cd build-dir + unpack ${source-tarball-musl} + # fixup: + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|' tools/*.sh \ + # patch popen/system to search in PATH instead of hardcoding /bin/sh + sed -i 's|posix_spawn(&pid, "/bin/sh",|posix_spawnp(\&pid, "sh",|' \ + src/stdio/popen.c src/process/system.c + sed -i 's|execl("/bin/sh", "sh", "-c",|execlp("sh", "-c",|'\ + src/misc/wordexp.c + # avoid absolute path references + sed -i 's/__FILE__/__FILE_NAME__/' include/assert.h + # configure: + ash ./configure --prefix=$out CFLAGS=-O2 + # build: + make -j $NPROC + # install: + make -j $NPROC install + mkdir $out/bin + ln -s $out/lib/libc.so $out/bin/ldd + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + extra.allowedRequisites = [ "out" ]; + extra.allowedReferences = [ "out" ]; + } diff --git a/06/using-nix/2b1-clang.nix b/06/using-nix/2b1-clang.nix new file mode 100644 index 0000000..e7bf2be --- /dev/null +++ b/06/using-nix/2b1-clang.nix @@ -0,0 +1,157 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, musl, intermediate-clang +, linux-headers, cmake, python}: + +let + source-tarball-llvm = fetchurl { + # local = /downloads/llvm-project-17.0.1.src.tar.xz; + url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.1/llvm-project-17.0.1.src.tar.xz"; + sha256 = "b0e42aafc01ece2ca2b42e3526f54bebc4b1f1dc8de6e34f46a0446a13e882b9"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2b1-clang"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/wrappers" + "${cmake}/bin" + "${python}/bin" + # 2a9-intermediate-clang intentionally not added to $PATH + # to prevent confusion + ]; + script = '' + # Shared libs are not relinked on install. Instead, their rpath + # is erased with RPATH_SET: `Set runtime path of + # "/nix/store/.../lib/x86_64-unknown-linux-musl/libc++.so.1.0" to ""` + # One (hacky) workaround to that is using a constant-len build-dir. + build_dir=build-dir; expr "$(pwd)/$build_dir)" '<=' 128 + while ! echo "$(pwd)/$build_dir" | wc -c | grep -Fqx 128; do + build_dir="$build_dir." + done; expr "$(echo $(pwd)/$build_dir | wc -c)" '==' 128 + mkdir $build_dir; cd $build_dir + export SHELL=${stage1.protobusybox}/bin/ash + # llvm cmake configuration should pick up ccache automatically from PATH + export PATH="$PATH:/ccache/bin" + command -v ccache && USE_CCACHE=YES || USE_CCACHE=NO + # prepare future sysroot: + SYSROOT=$out/sysroot + mkdir -p $SYSROOT/lib $SYSROOT/include + ln -s ${musl}/lib/* $SYSROOT/lib/ + ln -s ${musl}/include/* $SYSROOT/include/ + # unpack: + unpack ${source-tarball-llvm} + # fixup: + sed -i "s|COMMAND sh|COMMAND ${stage1.protobusybox}/bin/ash|" \ + llvm/cmake/modules/GetHostTriple.cmake clang/CMakeLists.txt + echo 'echo x86_64-unknown-linux-musl' > llvm/cmake/config.guess + LOADER=${musl}/lib/libc.so + sed -i "s|/lib/ld-musl-\" + ArchName + \".so.1|$LOADER|" \ + clang/lib/Driver/ToolChains/Linux.cpp + BEGINEND='const bool HasCRTBeginEndFiles' + sed -i "s|$BEGINEND =|$BEGINEND = false; ''${BEGINEND}_unused =|" \ + clang/lib/Driver/ToolChains/Gnu.cpp + REL_ORIGIN='_install_rpath \"\$ORIGIN/../lib''${LLVM_LIBDIR_SUFFIX}\"' + sed -i "s|_install_rpath \"\\\\\$ORIGIN/..|_install_rpath \"$out|" \ + llvm/cmake/modules/AddLLVM.cmake + sed -i 's|numShards = 32;|numShards = 1;|' lld/*/SyntheticSections.* + sed -i 's|numShards = 256;|numShards = 1;|' lld/*/ICF.cpp + sed -i 's|__FILE__|"__FILE__"|' \ + libcxx/src/verbose_abort.cpp \ + libcxxabi/src/abort_message.cpp \ + compiler-rt/lib/builtins/int_util.h + sed -i 's|"@LLVM_SRC_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in + sed -i 's|"@LLVM_OBJ_ROOT@"|"REDACTED"|' \ + llvm/tools/llvm-config/BuildVariables.inc.in + # figure out includes: + EXTRA_INCL="$(pwd)/extra_includes" + mkdir -p $EXTRA_INCL + cp clang/lib/Headers/*intrin*.h $EXTRA_INCL/ + cp clang/lib/Headers/mm_malloc.h $EXTRA_INCL/ + [ -e $EXTRA_INCL/immintrin.h ] + # configure: + export LD_LIBRARY_PATH="${musl}/lib:${intermediate-clang}/lib" + export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$(pwd)/build/lib" # libLLVM + OPTS="" + add_opt() { + OPTS="$OPTS -D$1" + } + add_opt CMAKE_BUILD_TYPE=Release + add_opt LLVM_OPTIMIZED_TABLEGEN=YES + add_opt LLVM_CCACHE_BUILD=$USE_CCACHE + add_opt DEFAULT_SYSROOT=$SYSROOT + add_opt CMAKE_INSTALL_PREFIX=$out + add_opt LLVM_INSTALL_BINUTILS_SYMLINKS=YES + add_opt LLVM_INSTALL_CCTOOLS_SYMLINKS=YES + add_opt CMAKE_INSTALL_DO_STRIP=YES + add_opt LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=YES + add_opt LLVM_TARGET_ARCH=X86 + add_opt LLVM_TARGETS_TO_BUILD=Native + add_opt LLVM_BUILTIN_TARGETS=x86_64-unknown-linux-musl + add_opt LLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl + add_opt LLVM_HOST_TRIPLE=x86_64-unknown-linux-musl + add_opt COMPILER_RT_DEFAULT_TARGET_TRIPLE=x86_64-unknown-linux-musl + add_opt LLVM_APPEND_VC_REV=NO + add_opt LLVM_INCLUDE_TESTS=NO + add_opt LLVM_INCLUDE_EXAMPLES=NO + add_opt LLVM_INCLUDE_BENCHMARKS=NO + add_opt LLVM_ENABLE_BACKTRACES=NO + add_opt LLVM_ENABLE_EH=YES + add_opt LLVM_ENABLE_RTTI=YES + add_opt CLANG_ENABLE_ARCMT=NO + add_opt CLANG_ENABLE_STATIC_ANALYZER=NO + add_opt COMPILER_RT_BUILD_SANITIZERS=NO + add_opt COMPILER_RT_BUILD_XRAY=NO + add_opt COMPILER_RT_BUILD_LIBFUZZER=NO + add_opt COMPILER_RT_BUILD_PROFILE=NO + add_opt COMPILER_RT_BUILD_MEMPROF=NO + add_opt COMPILER_RT_BUILD_ORC=NO + add_opt COMPILER_RT_USE_BUILTINS_LIBRARY=YES + add_opt CLANG_DEFAULT_CXX_STDLIB=libc++ + add_opt CLANG_DEFAULT_LINKER=lld + add_opt CLANG_DEFAULT_RTLIB=compiler-rt + add_opt LIBCXX_HAS_MUSL_LIBC=YES + add_opt LIBCXX_USE_COMPILER_RT=YES + add_opt LIBCXX_INCLUDE_BENCHMARKS=NO + add_opt LIBCXX_CXX_ABI=libcxxabi + add_opt LIBCXX_ADDITIONAL_COMPILE_FLAGS=-I${linux-headers}/include + add_opt LIBCXXABI_USE_COMPILER_RT=YES + add_opt LIBCXXABI_USE_LLVM_UNWINDER=YES + add_opt LLVM_INSTALL_TOOLCHAIN_ONLY=YES + add_opt LIBUNWIND_USE_COMPILER_RT=YES + add_opt LLVM_ENABLE_THREADS=NO + REWRITE="-ffile-prefix-map=$(pwd)=/builddir/" + CFLAGS="--sysroot=$SYSROOT -I$EXTRA_INCL $REWRITE" + LDFLAGS="-Wl,--dynamic-linker=$LOADER" + cmake -S llvm -B build -G 'Unix Makefiles' \ + -DCMAKE_ASM_COMPILER=${intermediate-clang}/bin/clang \ + -DCMAKE_C_COMPILER=${intermediate-clang}/bin/clang \ + -DCMAKE_CXX_COMPILER=${intermediate-clang}/bin/clang++ \ + -DLLVM_ENABLE_PROJECTS='clang;lld' \ + -DLLVM_ENABLE_RUNTIMES='compiler-rt;libcxx;libcxxabi;libunwind' \ + -DCMAKE_C_FLAGS="$CFLAGS" \ + -DCMAKE_CXX_FLAGS="$CFLAGS" \ + -DCMAKE_C_LINK_FLAGS="$LDFLAGS" \ + -DCMAKE_CXX_LINK_FLAGS="$LDFLAGS" \ + -DLLVM_BUILD_LLVM_DYLIB=YES \ + -DLLVM_LINK_LLVM_DYLIB=YES \ + -DCLANG_LINK_LLVM_DYLIB=YES \ + $OPTS + # build: + make -C build -j $NPROC + # install: + make -C build install/strip + ln -s $out/lib/x86_64-unknown-linux-musl/* $out/lib/ + mkdir -p $out/bin + ln -s $out/bin/clang $out/bin/cc + ln -s $out/bin/clang++ $out/bin/c++ + ln -s $out/bin/clang-cpp $out/bin/cpp + ln -s $out/bin/lld $out/bin/ld + # mix new stuff into sysroot: + ln -s $out/lib/* $out/sysroot/lib/ + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + extra.allowedRequisites = [ "out" musl ]; + extra.allowedReferences = [ "out" musl ]; + } diff --git a/06/using-nix/2b2-busybox.nix b/06/using-nix/2b2-busybox.nix new file mode 100644 index 0000000..78dc2b1 --- /dev/null +++ b/06/using-nix/2b2-busybox.nix @@ -0,0 +1,52 @@ +{ fetchurl, mkDerivationStage2 +, stage1, static-gnumake, musl, clang, linux-headers }: + +let + source-tarball-busybox = fetchurl { + # local = /downloads/busybox-1.36.1.tar.bz2; + url = "https://busybox.net/downloads/busybox-1.36.1.tar.bz2"; + sha256 = "b8cc24c9574d809e7279c3be349795c5d5ceb6fdf19ca709f80cde50e47de314"; + }; +in + mkDerivationStage2 { + name = "bootstrap-2b2-busybox"; + buildInputPaths = [ + "${stage1.protobusybox}/bin" + "${static-gnumake}/bin" + "${clang}/bin" + ]; + script = '' + mkdir build-dir; cd build-dir + # alias ash to sh: + mkdir aliases; ln -s ${stage1.protobusybox}/bin/ash aliases/sh + export PATH="$(pwd)/aliases:$PATH" + # unpack: + unpack ${source-tarball-busybox} + # fixup: + echo -e '#!${stage1.protobusybox}/bin/ash\nprintf 9999' \ + > scripts/gcc-version.sh + sed -i 's|/bin/sh|${stage1.protobusybox}/bin/ash|g' \ + scripts/gen_build_files.sh \ + scripts/mkconfigs scripts/embedded_scripts scripts/trylink \ + scripts/generate_BUFSIZ.sh \ + applets/usage_compressed applets/busybox.mkscripts applets/install.sh + # configure: + echo "### $0: configuring busybox..." + BUSYBOX_FLAGS='CONFIG_SHELL=${stage1.protobusybox}/bin/ash' + BUSYBOX_FLAGS="$BUSYBOX_FLAGS CC=cc HOSTCC=cc" + BUSYBOX_FLAGS="$BUSYBOX_FLAGS KCONFIG_NOTIMESTAMP=y" + BUSYBOX_CFLAGS="CFLAGS=-I${linux-headers}/include" + make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" defconfig + sed -i 's|CONFIG_INSTALL_NO_USR=y|CONFIG_INSTALL_NO_USR=n|' .config + # build: + make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" busybox busybox.links + sed -i 's|^/usr/s\?bin/|/bin/|' busybox.links + # install: + make -j $NPROC $BUSYBOX_FLAGS "$BUSYBOX_CFLAGS" \ + install CONFIG_PREFIX=$out + # check for build path leaks: + ( ! grep -rF $(pwd) $out ) + ''; + extra.allowedRequisites = [ "out" musl clang ]; + extra.allowedReferences = [ "out" musl clang ]; + } diff --git a/06/verify.nix b/06/verify.nix new file mode 100644 index 0000000..177e6ed --- /dev/null +++ b/06/verify.nix @@ -0,0 +1,16 @@ +amhpqf61g19sw80gcxkx9i385b497hzh stage1^tinycc +r5jb6ydqlphvh33kqynvnxbqqrsy9vlm stage1^protobusybox +45fx2ls8ikc8whr03plslhjir844f7ax stage1^protomusl +xapj0c7basdkkacrng50ayxy9xbklaj2 static-gnumake +xc3db7c3rpfqpp11y2ms4v5cgkq7dzyj static-binutils +x58bd1awpbmd2iaxwcmb269anvhb9bbm static-gnugcc4-c +mhbvszfvvryvnd46xawvsbd0bnbpmw9v intermediate-musl +ghh0kin5896hc5wqcll4s0axgn540jgy gnugcc4-cpp +6vnz5a78ahy1762s5wbvaq3jifsnf18f gnugcc10 +wgg3p19hv7ji265gcm6mclrlqxays1v0 linux-headers +mdavw6g21ysjrp9wxidl9vpbfy2hqqdj cmake +3ivma9103vx1192kqkw6vvbcg161gvz1 python +z3qlri49m9v9zhf1f5pyfbm5jdbk9wiz intermediate-clang +5hasqad9ih2fmn6ma9vww2jdjqjgzvxs musl +cr1bxa6hjz1baygh7kdwmhgxvypqcvag clang +2f7xfn3x1bgpy1wdrnn48z3c1bagwggh busybox diff --git a/06/verify.pkgs.sha256 b/06/verify.pkgs.sha256 new file mode 100644 index 0000000..1a623c7 --- /dev/null +++ b/06/verify.pkgs.sha256 @@ -0,0 +1,32 @@ +da4da9d48bb65e68a8095efb3f439e5b92bddd360e60578219932f813eb05ec1 pkgs/0-tcc-seed.tar +8347995cd00d16f1564d19ed045aaf9c7c500c417574d28dde7c2254a089d36a pkgs/1-stage1.tar +c825a49166b6a382002136d0cd3510477c2e50b55172af6679db2090debdf520 pkgs/2a0-static-gnumake.tar +ac3ca80644a351c75c5ac7bb03c0c8d6b33fdb677e11663fef247c670d81edc4 pkgs/2a1-static-binutils.tar +86915ff0e41e21086a71a89729bb75cb3ddf6e32f7a06f3ea1fb66e4c8cada33 pkgs/2a2-static-gnugcc4-c.tar +bf7d15658210e0fa8a8d1b5ea44c16af1b8a5d5223475812e4a395542440b98d pkgs/2a3-intermediate-musl.tar +2d9759bcba6170d9cf8a750df3db94746d7e778f3414a72aaaa570508219ebf3 pkgs/2a4-gnugcc4-cpp.tar +b2028c82e05dc8858f2c5ccb7b93eae169f9f397ef4b6fe755ad3f1e6c4c857d pkgs/2a5-gnugcc10.tar +d5703b484fe95c7c8a617f29329cc87c0ad852a8ff2597e167c6766e0ce5980a pkgs/2a6-linux-headers.tar +969031fdf9f7df5c5039e5d04b5a9cc76928f3558097e40555157a13e08ee6c0 pkgs/2a7-cmake.tar +a4b89542ef2ae8af348df98c7bbb8aafa1692dec65a46e1e58be5b47b819a749 pkgs/2a8-python.tar +236f41af85e539484f4845a5cf8cb751f5a957bae2b4a1ff13c35bacbbcd8276 pkgs/2a9-intermediate-clang.tar +89e7aa57ab169b4ce6498294b67926d0d09a9cf7f6d108d958f0d207c546930e pkgs/2b0-musl.tar +f804d4d19a1437eda38dfc532407f21524a99897906f0df9b51a2bf2d1b192d4 pkgs/2b1-clang.tar +15b9751b6f102007243250d95a58e686c8d1564d92d1c5708be2b657019453a9 pkgs/2b2-busybox.tar +c9c5a533f70c10dc659e79638d24b00a35aac51d875b85fec21a8f0da6507217 pkgs/2b3-gnumake.tar +2f05779ae73ef59950b1ded356da891a9a5e0d02f153567d9e85fa1cb926966b pkgs/3a-boost.tar +1fbde76654b4879687a02435470afbc248e7d4f6717f01432ed9cf7205161287 pkgs/3a-brotli.tar +60e4454d2e9b7d127af0e4e1967cc4eccdd386480c2e9eae32f1c5ee7e7d442d pkgs/3a-curl.tar +0e19d7d952ff9a4a3971a804e20463e422ec9a5de54438deda69065d7e67bf79 pkgs/3a-editline.tar +442bde8dafed0d7b5571394e13a4f8bbf1d4e0dffa41306d19e73e8e32854820 pkgs/3a-gnugperf.tar +04eeff2024984df8204dd8718bf4db00c1223a6d9d4dc08591be13594127a293 pkgs/3a-libarchive.tar +0fa4c6c75e2974a4a6b8cb61ebc419130ac866f0da4539412fb2bc1e6252be58 pkgs/3a-libsodium.tar +6f1be37aa2fa121e90ccb49308634165f96e2eb03d841f563245bae7736c83d7 pkgs/3a-lowdown.tar +3d7282686a98e6a5e2b3655ae02c1aae1dc1d3c20ac53fadef90cd8408a4eed7 pkgs/3a-mbedtls.tar +3e3796d38d685334cc6d88c79e573257b8ce834308a793571f45e4d657330f3f pkgs/3a-nlohmann-json.tar +47e7dbb47b13f0348e0403ea6ac70c45e4ddb1536f3e05ef2b777c8b7fc8b027 pkgs/3a-pkg-config.tar +fb353622c346831fdea82d74801e27bcda482ad72221d9be51a5372fc1234daf pkgs/3a-seccomp.tar +73e2efe1845ca550762ddcce2103164839c02ed6777356af833c594a2a1e1d76 pkgs/3a-sqlite.tar +846525ebdfb81858963530cae2977b7fcb29381112119059690e7f916ce8c397 pkgs/3b-busybox-static.tar +8df17ee6e1e7f19f7118d17cd9ff7f25dbc4b05b9f9bbab9b78f40e1b4667672 pkgs/3b-nix.tar +c9d0fb338b81db4184f84eb1f71687391edded2e937dec77ac9f353fa526e70d pkgs/3b-tinycc-static.tar diff --git a/BOOSTRAP.md b/BOOSTRAP.md new file mode 100644 index 0000000..00a7bf1 --- /dev/null +++ b/BOOSTRAP.md @@ -0,0 +1,241 @@ +# boostrapping a (Linux x86-64) C compiler + +Compilers nowadays are written in languages like C, which themselves need to be +compiled. But then, you need a C compiler to compile your C compiler! Of course, +the very first C compiler was not written in C. +First, people made assemblers, then simple programming languages, +then, eventually, it was possible to make a C compiler. +In this repository, we'll explore how that's done. Each directory here +is a "stage" in the process. The first one, `00`, is a hand-written +executable, and the last one, `05`, is a C compiler. Each directory has its own +README explaining what's going on. + +You can run `bootstrap.sh` to run through and test every stage. +To get HTML versions of all README pages, run `make`. + +Note that the executables produced in this series will only run on +64-bit Linux, because each OS/architecture combination would need its own separate +executable. + +## table of contents + +- [stage 00](00/README.md) - a program converting a text file with +hexadecimal digit pairs to a binary file. +- [stage 01](01/README.md) - a language with comments, and 2-character +command codes. +- [stage 02](02/README.md) - a language with labels +- [stage 03](03/README.md) - a language with longer labels, better error messages, and less register manipulation +- [stage 04](04/README.md) - a language with nice functions and local variables +- [stage 04a](04a/README.md) - (interlude) a simple preprocessor +- [stage 05](05/README.md) - a C compiler capable of compiling TCC +- [stage 06](06/README.md) - an interpreter capable of executing zig + +## prerequisite knowledge + +If you want to follow along with this series, you'll probably want to know about: + +- number bases -- if a number is preceded by 0x, 0o, or 0b in this series, that +means hexadecimal/octal/binary respectively. So 0xff = FF hexadecimal = 255 +decimal. +- bits, bytes, kilobytes, etc. +- bitwise operations (not, or, and, xor, left shift, right shift) +- 2's complement +- ASCII, null-terminated strings +- how pointers work +- how floating-point numbers work +- what a compiler is +- what an executable file is +- what a system call is +- what a CPU is +- what a CPU architecture is +- what a CPU register is +- what the (call) stack is + +If you're unfamiliar with x86-64 assembly, you should take a look at the instruction list below. + +## principles + +- as simple as possible + +Bootstrapping a compiler is not an easy task, so we're trying to make it as easy +as possible. We don't even necessarily need a standard-compliant C compiler, we +only need enough to compile someone else's C compiler. Specifically, we'll be +using [tcc](https://bellard.org/tcc/) since it's written (mostly) in C89. + +- efficiency is not a concern + +We will create big and slow executables, and that's okay. It doesn't really +matter if compiling TCC takes 30 as opposed to 0.01 seconds; once +we compile it with itself, we should get the same executable either way. + +## reflections on trusting trust + +In 1984, Ken Thompson wrote the well-known article +[Reflections on Trusting Trust](http://users.ece.cmu.edu/~ganger/712.fall02/papers/p761-thompson.pdf). +This is one of the inspirations for this project. A brief summary is: +it's possible to create a malicious C compiler which will +replicate its own malicious functionalities (e.g. detecting password-checking +routines to make them also accept another password the attacker knows) when used +to compile other C compilers. For all we know, such a compiler was used to +compile gcc, say, and so all programs around today could be compromised. Of +course, this is practically definitely not the case, but it's still an +interesting experiment to try to create a fully trustable compiler. This +project can't necessarily even do that though, because the Linux kernel, which +we depend on, is compiled from C, so we can't fully trust *it*. To +create a *fully* trustable compiler, you'd need to manually write +an operating system to a USB key with a circuit or something, +assuming you trust your CPU... +I'll leave that to someone else. + +## instruction set + +x86-64 has a *gigantic* instruction set. The manual for it is over 2,000 pages +long! To make things simpler, we will only use a small subset. + +Here are all the instructions we'll be using. If you're not familiar with +x86-64 assembly, you might want to look over these. + +x86-64 has 16 integer registers: rax, rbx, rcx, rdx, rsp, rbp, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15. +We will almost entirely be using the first 8 of these. +al refers to the bottom 8 bits of rax, likewise with bl, cl, dl; +ax refers to the bottom 16 bits of rax, likewise with bx, cx, dx; +eax refers to the bottom 32 bits of rax, likewise with ebx, ecx, edx. + +x86-64 also has 16 floating-point registers: xmm0 through xmm15. We'll only be using +xmm0 and xmm1. These registers can hold either four 32-bit floating-point numbers (`float`s) or +two 64-bit floating-point numbers (`double`s), but we'll only be using them to hold either one +`float` or one `double`. + +In the table below, `IMM64` means a 64-bit *immediate* (a constant number). +`rdx:rax` refers to the 128-bit number you get by combining `rdx` and `rax`. + +``` +ax bx cx dx sp bp si di +0 3 1 2 4 5 6 7 + +┌──────────────────────┬───────────────────┬────────────────────────────────────────┐ +│ Instruction │ Encoding │ Description │ +├──────────────────────┼───────────────────┼────────────────────────────────────────┤ +│ mov rax, IMM64 │ 48 b8 IMM64 │ set rax to the 64-bit value IMM64 │ +│ mov rbx, IMM64 │ 48 bb IMM64 │ set rbx to the 64-bit value IMM64 │ +| add rax, IMM32 | 48 05 IMM32 | add IMM32 (signed) to rax | +│ xor eax, eax │ 31 c0 │ set rax to 0 (shorter than mov rax, 0) │ +│ xor edx, edx │ 31 d2 │ set rdx to 0 │ +│ mov RDEST, RSRC │ 48 89 (DEST|SRC<<3|0xc0) │ set register DEST to current │ +│ │ │ value of register SRC │ +│ mov r8, rax │ 49 89 c0 │ set r8 to rax (only used for syscalls) │ +│ mov r9, rax │ 49 89 c1 │ set r9 to rax (only used for syscalls) │ +│ mov r10, rax │ 49 89 c2 │ set r10 to rax (only used for syscalls)│ +| movsx rax, al | 48 0f be c0 | sign-extend al to rax | +| movsx rax, ax | 48 0f bf c0 | sign-extend ax to rax | +| movsx rax, eax | 48 63 c0 | sign-extend eax to rax | +| movzx rax, al | 48 0f b6 c0 | zero-extend al to rax | +| movzx rax, ax | 48 0f b7 c0 | zero-extend ax to rax | +| mov eax, eax | 89 c0 | zero-extend eax to rax | +│ xchg rax, rbx │ 48 93 │ exchange the values of rax and rbx │ +│ mov [rbx], rax │ 48 89 03 │ store rax as 8 bytes at address rbx │ +│ mov rax, [rbx] │ 48 8b 03 │ load 8 bytes from address rbx into rax │ +│ mov [rbx], eax │ 89 03 │ store eax as 4 bytes at address rbx │ +│ mov eax, [rbx] │ 8b 03 │ load 4 bytes from address rbx into eax │ +│ mov [rbx], ax │ 66 89 03 │ store ax as 2 bytes at address rbx │ +│ mov ax, [rbx] │ 66 8b 03 │ load 2 bytes from address rbx into eax │ +│ mov [rbx], al │ 88 03 │ store al as 1 byte at address rbx │ +│ mov al, [rbx] │ 8a 03 │ load 1 byte from address rbx into al │ +│ mov rax, [rbp+IMM32] │ 48 8b 85 IMM32 │ load 8 bytes from address rbp+IMM32 │ +│ │ │ into rax (note: IMM32 may be negative) │ +│ mov rax, [rsp+IMM32] │ 48 8b 84 24 IMM32 │ load 8 bytes from rsp+IMM32 into rax │ +│ mov [rbp+IMM32], rax │ 48 89 85 IMM32 │ store rax in 8 bytes at rbp+IMM32 │ +│ mov [rsp+IMM32], rax │ 48 89 84 24 IMM32 │ store rax in 8 bytes at rsp+IMM32 │ +│ mov [rsp], rbp │ 48 89 2c 24 │ store rbp in 8 bytes at rsp │ +│ mov rbp, [rsp] │ 48 8b 2c 24 │ load 8 bytes from rsp into rbp │ +│ lea rax, [rbp+IMM32] │ 48 8d 85 IMM32 │ set rax to rbp+IMM32 │ +│ lea rsp, [rbp+IMM32] │ 48 8d a5 IMM32 │ set rsp to rbp+IMM32 │ +| int3 | cc | raise trap signal -useful for debugging| +| movsq | 48 a5 | copy 8 bytes from rsi to rdi | +| rep movsb | f3 a4 | copy rcx bytes from rsi to rdi | +│ push rax │ 50 │ push rax onto the stack │ +│ pop rax │ 58 │ pop a value off the stack into rax │ +│ neg rax │ 48 f7 d8 │ set rax to -rax │ +│ add rax, rbx │ 48 01 d8 │ add rbx to rax │ +│ sub rax, rbx │ 48 29 d8 │ subtract rbx from rax │ +│ imul rbx │ 48 f7 eb │ set rdx:rax to rax * rbx (signed) │ +│ cqo │ 48 99 │ sign-extend rax to rdx:rax | +│ idiv rbx │ 48 f7 fb │ divide rdx:rax by rbx (signed); put │ +│ │ │ quotient in rax, remainder in rdx │ +│ mul rbx │ 48 f7 e3 │ like imul, but unsigned │ +│ div rbx │ 48 f7 f3 │ like idiv, but unsigned │ +│ not rax │ 48 f7 d0 │ set rax to ~rax (bitwise not) │ +│ and rax, rbx │ 48 21 d8 │ set rax to rax & rbx (bitwise and) │ +│ or rax, rbx │ 48 09 d8 │ set rax to rax | rbx (bitwise or) │ +│ xor rax, rbx │ 48 31 d8 │ set rax to rax ^ rbx (bitwise xor) │ +│ shl rax, cl │ 48 d3 e0 │ set rax to rax << cl (left shift) │ +│ shl rax, IMM8 │ 48 c1 e0 IMM8 │ set rax to rax << IMM8 │ +│ shr rax, cl │ 48 d3 e8 │ set rax to rax >> cl (unsigned) │ +│ shr rax, IMM8 │ 48 c1 e8 IMM8 │ set rax to rax >> IMM8 (unsigned) │ +│ sar rax, cl │ 48 d3 f8 │ set rax to rax >> cl (signed) │ +│ sar rax, IMM8 │ 48 c1 f8 IMM8 │ set rax to rax >> IMM8 (signed) │ +│ sub rsp, IMM32 │ 48 81 ec IMM32 │ subtract IMM32 from rsp │ +│ add rsp, IMM32 │ 48 81 c4 IMM32 │ add IMM32 to rsp │ +│ cmp rax, rbx │ 48 39 d8 │ compare rax with rbx (see je, jl, etc.)│ +│ test rax, rax │ 48 85 c0 │ equivalent to cmp rax, 0 │ +│ jmp IMM32 │ e9 IMM32 │ jump to offset IMM32 from here │ +│ je IMM32 │ 0f 84 IMM32 │ jump to IMM32 if equal │ +│ jne IMM32 │ 0f 85 IMM32 │ jump if not equal │ +│ jl IMM32 │ 0f 8c IMM32 │ jump if less than │ +│ jg IMM32 │ 0f 8f IMM32 │ jump if greater than │ +│ jle IMM32 │ 0f 8e IMM32 │ jump if less than or equal to │ +│ jge IMM32 │ 0f 8d IMM32 │ jump if greater than or equal to │ +│ jb IMM32 │ 0f 82 IMM32 │ jump if "below" (like jl but unsigned) │ +│ ja IMM32 │ 0f 87 IMM32 │ jump if "above" (like jg but unsigned) │ +│ jbe IMM32 │ 0f 86 IMM32 │ jump if below or equal to │ +│ jae IMM32 │ 0f 83 IMM32 │ jump if above or equal to │ +│ sete al │ 0f 94 c0 │ set al to 1 if equal; 0 otherwise │ +│ setne al │ 0f 95 c0 │ set al to 1 if not equal │ +│ setl al │ 0f 9c c0 │ set al to 1 if less than │ +│ setg al │ 0f 9f c0 │ set al to 1 if greater than │ +│ setle al │ 0f 9e c0 │ set al to 1 if less than or equal to │ +│ setge al │ 0f 9d c0 │ set al to 1 if greater than or equal to│ +│ setb al │ 0f 92 c0 │ set al to 1 if below │ +│ seta al │ 0f 97 c0 │ set al to 1 if above │ +│ setbe al │ 0f 96 c0 │ set al to 1 if below or equal to │ +│ setae al │ 0f 93 c0 │ set al to 1 if above or equal to │ +| movq rax, xmm0 | 66 48 0f 7e c0 | set rax to xmm0 | +| movq xmm0, rax | 66 48 0f 6e c0 | set xmm0 to rax | +| movq xmm1, rax | 66 48 0f 6e c8 | set xmm1 to rax | +| movq xmm1, xmm0 | f3 0f 7e c8 | set xmm1 to xmm0 | +| cvtss2sd xmm0, xmm0 | f3 0f 5a c0 | convert xmm0 from float to double | +| cvtsd2ss xmm0, xmm0 | f2 0f 5a c0 | convert xmm0 from double to float | +| cvttsd2si rax, xmm0 | f2 48 0f 2c c0 | convert double in xmm0 to int in rax | +| cvtsi2sd xmm0, rax | f2 48 0f 2a c0 | convert int in rax to double in xmm0 | +| comisd xmm0, xmm1 | 66 0f 2f c1 | compare xmm0 and xmm1 | +| addsd xmm0, xmm1 | f2 0f 58 c1 | add xmm1 to xmm0 | +| subsd xmm0, xmm1 | f2 0f 5c c1 | subtract xmm1 from xmm0 | +| mulsd xmm0, xmm1 | f2 0f 59 c1 | multiply xmm0 by xmm1 | +| divsd xmm0, xmm1 | f2 0f 5e c1 | divide xmm0 by xmm1 | +│ call rax │ ff d0 │ call the function at address rax │ +│ ret │ c3 │ return from function │ +│ syscall │ 0f 05 │ execute a system call │ +│ nop │ 90 │ do nothing │ +└──────────────────────┴───────────────────┴────────────────────────────────────────┘ + +SYSCALLS +Arguments are passed in + rdi, rsi, rdx, r10, r8, r9 +The return value is placed in rax. +The values of rsp, rbp and rbx are preserved, but other registers might change. +``` + +## license + +This does not apply to tcc's or musl's source code. + +``` +This project is in the public domain. Any copyright protections from any law +are forfeited by the author(s). No warranty is provided, and the author(s) +shall not be held liable in connection with it. +``` + +## contributing + +If you notice a mistake/want to clarify something, you can submit a pull request +via GitHub, or email `pommicket at pommicket.com`. diff --git a/README.md b/README.md index 00a7bf1..6f63bff 100644 --- a/README.md +++ b/README.md @@ -1,241 +1,5 @@ -# boostrapping a (Linux x86-64) C compiler +## Documentation -Compilers nowadays are written in languages like C, which themselves need to be -compiled. But then, you need a C compiler to compile your C compiler! Of course, -the very first C compiler was not written in C. -First, people made assemblers, then simple programming languages, -then, eventually, it was possible to make a C compiler. -In this repository, we'll explore how that's done. Each directory here -is a "stage" in the process. The first one, `00`, is a hand-written -executable, and the last one, `05`, is a C compiler. Each directory has its own -README explaining what's going on. +- [TinyCC Boostrap (stages 1-5)](./BOOSTRAP.md) +- [TinyCC -> Clang (stage 6)](./06/README.md) -You can run `bootstrap.sh` to run through and test every stage. -To get HTML versions of all README pages, run `make`. - -Note that the executables produced in this series will only run on -64-bit Linux, because each OS/architecture combination would need its own separate -executable. - -## table of contents - -- [stage 00](00/README.md) - a program converting a text file with -hexadecimal digit pairs to a binary file. -- [stage 01](01/README.md) - a language with comments, and 2-character -command codes. -- [stage 02](02/README.md) - a language with labels -- [stage 03](03/README.md) - a language with longer labels, better error messages, and less register manipulation -- [stage 04](04/README.md) - a language with nice functions and local variables -- [stage 04a](04a/README.md) - (interlude) a simple preprocessor -- [stage 05](05/README.md) - a C compiler capable of compiling TCC -- [stage 06](06/README.md) - an interpreter capable of executing zig - -## prerequisite knowledge - -If you want to follow along with this series, you'll probably want to know about: - -- number bases -- if a number is preceded by 0x, 0o, or 0b in this series, that -means hexadecimal/octal/binary respectively. So 0xff = FF hexadecimal = 255 -decimal. -- bits, bytes, kilobytes, etc. -- bitwise operations (not, or, and, xor, left shift, right shift) -- 2's complement -- ASCII, null-terminated strings -- how pointers work -- how floating-point numbers work -- what a compiler is -- what an executable file is -- what a system call is -- what a CPU is -- what a CPU architecture is -- what a CPU register is -- what the (call) stack is - -If you're unfamiliar with x86-64 assembly, you should take a look at the instruction list below. - -## principles - -- as simple as possible - -Bootstrapping a compiler is not an easy task, so we're trying to make it as easy -as possible. We don't even necessarily need a standard-compliant C compiler, we -only need enough to compile someone else's C compiler. Specifically, we'll be -using [tcc](https://bellard.org/tcc/) since it's written (mostly) in C89. - -- efficiency is not a concern - -We will create big and slow executables, and that's okay. It doesn't really -matter if compiling TCC takes 30 as opposed to 0.01 seconds; once -we compile it with itself, we should get the same executable either way. - -## reflections on trusting trust - -In 1984, Ken Thompson wrote the well-known article -[Reflections on Trusting Trust](http://users.ece.cmu.edu/~ganger/712.fall02/papers/p761-thompson.pdf). -This is one of the inspirations for this project. A brief summary is: -it's possible to create a malicious C compiler which will -replicate its own malicious functionalities (e.g. detecting password-checking -routines to make them also accept another password the attacker knows) when used -to compile other C compilers. For all we know, such a compiler was used to -compile gcc, say, and so all programs around today could be compromised. Of -course, this is practically definitely not the case, but it's still an -interesting experiment to try to create a fully trustable compiler. This -project can't necessarily even do that though, because the Linux kernel, which -we depend on, is compiled from C, so we can't fully trust *it*. To -create a *fully* trustable compiler, you'd need to manually write -an operating system to a USB key with a circuit or something, -assuming you trust your CPU... -I'll leave that to someone else. - -## instruction set - -x86-64 has a *gigantic* instruction set. The manual for it is over 2,000 pages -long! To make things simpler, we will only use a small subset. - -Here are all the instructions we'll be using. If you're not familiar with -x86-64 assembly, you might want to look over these. - -x86-64 has 16 integer registers: rax, rbx, rcx, rdx, rsp, rbp, rsi, rdi, r8, r9, r10, r11, r12, r13, r14, r15. -We will almost entirely be using the first 8 of these. -al refers to the bottom 8 bits of rax, likewise with bl, cl, dl; -ax refers to the bottom 16 bits of rax, likewise with bx, cx, dx; -eax refers to the bottom 32 bits of rax, likewise with ebx, ecx, edx. - -x86-64 also has 16 floating-point registers: xmm0 through xmm15. We'll only be using -xmm0 and xmm1. These registers can hold either four 32-bit floating-point numbers (`float`s) or -two 64-bit floating-point numbers (`double`s), but we'll only be using them to hold either one -`float` or one `double`. - -In the table below, `IMM64` means a 64-bit *immediate* (a constant number). -`rdx:rax` refers to the 128-bit number you get by combining `rdx` and `rax`. - -``` -ax bx cx dx sp bp si di -0 3 1 2 4 5 6 7 - -┌──────────────────────┬───────────────────┬────────────────────────────────────────┐ -│ Instruction │ Encoding │ Description │ -├──────────────────────┼───────────────────┼────────────────────────────────────────┤ -│ mov rax, IMM64 │ 48 b8 IMM64 │ set rax to the 64-bit value IMM64 │ -│ mov rbx, IMM64 │ 48 bb IMM64 │ set rbx to the 64-bit value IMM64 │ -| add rax, IMM32 | 48 05 IMM32 | add IMM32 (signed) to rax | -│ xor eax, eax │ 31 c0 │ set rax to 0 (shorter than mov rax, 0) │ -│ xor edx, edx │ 31 d2 │ set rdx to 0 │ -│ mov RDEST, RSRC │ 48 89 (DEST|SRC<<3|0xc0) │ set register DEST to current │ -│ │ │ value of register SRC │ -│ mov r8, rax │ 49 89 c0 │ set r8 to rax (only used for syscalls) │ -│ mov r9, rax │ 49 89 c1 │ set r9 to rax (only used for syscalls) │ -│ mov r10, rax │ 49 89 c2 │ set r10 to rax (only used for syscalls)│ -| movsx rax, al | 48 0f be c0 | sign-extend al to rax | -| movsx rax, ax | 48 0f bf c0 | sign-extend ax to rax | -| movsx rax, eax | 48 63 c0 | sign-extend eax to rax | -| movzx rax, al | 48 0f b6 c0 | zero-extend al to rax | -| movzx rax, ax | 48 0f b7 c0 | zero-extend ax to rax | -| mov eax, eax | 89 c0 | zero-extend eax to rax | -│ xchg rax, rbx │ 48 93 │ exchange the values of rax and rbx │ -│ mov [rbx], rax │ 48 89 03 │ store rax as 8 bytes at address rbx │ -│ mov rax, [rbx] │ 48 8b 03 │ load 8 bytes from address rbx into rax │ -│ mov [rbx], eax │ 89 03 │ store eax as 4 bytes at address rbx │ -│ mov eax, [rbx] │ 8b 03 │ load 4 bytes from address rbx into eax │ -│ mov [rbx], ax │ 66 89 03 │ store ax as 2 bytes at address rbx │ -│ mov ax, [rbx] │ 66 8b 03 │ load 2 bytes from address rbx into eax │ -│ mov [rbx], al │ 88 03 │ store al as 1 byte at address rbx │ -│ mov al, [rbx] │ 8a 03 │ load 1 byte from address rbx into al │ -│ mov rax, [rbp+IMM32] │ 48 8b 85 IMM32 │ load 8 bytes from address rbp+IMM32 │ -│ │ │ into rax (note: IMM32 may be negative) │ -│ mov rax, [rsp+IMM32] │ 48 8b 84 24 IMM32 │ load 8 bytes from rsp+IMM32 into rax │ -│ mov [rbp+IMM32], rax │ 48 89 85 IMM32 │ store rax in 8 bytes at rbp+IMM32 │ -│ mov [rsp+IMM32], rax │ 48 89 84 24 IMM32 │ store rax in 8 bytes at rsp+IMM32 │ -│ mov [rsp], rbp │ 48 89 2c 24 │ store rbp in 8 bytes at rsp │ -│ mov rbp, [rsp] │ 48 8b 2c 24 │ load 8 bytes from rsp into rbp │ -│ lea rax, [rbp+IMM32] │ 48 8d 85 IMM32 │ set rax to rbp+IMM32 │ -│ lea rsp, [rbp+IMM32] │ 48 8d a5 IMM32 │ set rsp to rbp+IMM32 │ -| int3 | cc | raise trap signal -useful for debugging| -| movsq | 48 a5 | copy 8 bytes from rsi to rdi | -| rep movsb | f3 a4 | copy rcx bytes from rsi to rdi | -│ push rax │ 50 │ push rax onto the stack │ -│ pop rax │ 58 │ pop a value off the stack into rax │ -│ neg rax │ 48 f7 d8 │ set rax to -rax │ -│ add rax, rbx │ 48 01 d8 │ add rbx to rax │ -│ sub rax, rbx │ 48 29 d8 │ subtract rbx from rax │ -│ imul rbx │ 48 f7 eb │ set rdx:rax to rax * rbx (signed) │ -│ cqo │ 48 99 │ sign-extend rax to rdx:rax | -│ idiv rbx │ 48 f7 fb │ divide rdx:rax by rbx (signed); put │ -│ │ │ quotient in rax, remainder in rdx │ -│ mul rbx │ 48 f7 e3 │ like imul, but unsigned │ -│ div rbx │ 48 f7 f3 │ like idiv, but unsigned │ -│ not rax │ 48 f7 d0 │ set rax to ~rax (bitwise not) │ -│ and rax, rbx │ 48 21 d8 │ set rax to rax & rbx (bitwise and) │ -│ or rax, rbx │ 48 09 d8 │ set rax to rax | rbx (bitwise or) │ -│ xor rax, rbx │ 48 31 d8 │ set rax to rax ^ rbx (bitwise xor) │ -│ shl rax, cl │ 48 d3 e0 │ set rax to rax << cl (left shift) │ -│ shl rax, IMM8 │ 48 c1 e0 IMM8 │ set rax to rax << IMM8 │ -│ shr rax, cl │ 48 d3 e8 │ set rax to rax >> cl (unsigned) │ -│ shr rax, IMM8 │ 48 c1 e8 IMM8 │ set rax to rax >> IMM8 (unsigned) │ -│ sar rax, cl │ 48 d3 f8 │ set rax to rax >> cl (signed) │ -│ sar rax, IMM8 │ 48 c1 f8 IMM8 │ set rax to rax >> IMM8 (signed) │ -│ sub rsp, IMM32 │ 48 81 ec IMM32 │ subtract IMM32 from rsp │ -│ add rsp, IMM32 │ 48 81 c4 IMM32 │ add IMM32 to rsp │ -│ cmp rax, rbx │ 48 39 d8 │ compare rax with rbx (see je, jl, etc.)│ -│ test rax, rax │ 48 85 c0 │ equivalent to cmp rax, 0 │ -│ jmp IMM32 │ e9 IMM32 │ jump to offset IMM32 from here │ -│ je IMM32 │ 0f 84 IMM32 │ jump to IMM32 if equal │ -│ jne IMM32 │ 0f 85 IMM32 │ jump if not equal │ -│ jl IMM32 │ 0f 8c IMM32 │ jump if less than │ -│ jg IMM32 │ 0f 8f IMM32 │ jump if greater than │ -│ jle IMM32 │ 0f 8e IMM32 │ jump if less than or equal to │ -│ jge IMM32 │ 0f 8d IMM32 │ jump if greater than or equal to │ -│ jb IMM32 │ 0f 82 IMM32 │ jump if "below" (like jl but unsigned) │ -│ ja IMM32 │ 0f 87 IMM32 │ jump if "above" (like jg but unsigned) │ -│ jbe IMM32 │ 0f 86 IMM32 │ jump if below or equal to │ -│ jae IMM32 │ 0f 83 IMM32 │ jump if above or equal to │ -│ sete al │ 0f 94 c0 │ set al to 1 if equal; 0 otherwise │ -│ setne al │ 0f 95 c0 │ set al to 1 if not equal │ -│ setl al │ 0f 9c c0 │ set al to 1 if less than │ -│ setg al │ 0f 9f c0 │ set al to 1 if greater than │ -│ setle al │ 0f 9e c0 │ set al to 1 if less than or equal to │ -│ setge al │ 0f 9d c0 │ set al to 1 if greater than or equal to│ -│ setb al │ 0f 92 c0 │ set al to 1 if below │ -│ seta al │ 0f 97 c0 │ set al to 1 if above │ -│ setbe al │ 0f 96 c0 │ set al to 1 if below or equal to │ -│ setae al │ 0f 93 c0 │ set al to 1 if above or equal to │ -| movq rax, xmm0 | 66 48 0f 7e c0 | set rax to xmm0 | -| movq xmm0, rax | 66 48 0f 6e c0 | set xmm0 to rax | -| movq xmm1, rax | 66 48 0f 6e c8 | set xmm1 to rax | -| movq xmm1, xmm0 | f3 0f 7e c8 | set xmm1 to xmm0 | -| cvtss2sd xmm0, xmm0 | f3 0f 5a c0 | convert xmm0 from float to double | -| cvtsd2ss xmm0, xmm0 | f2 0f 5a c0 | convert xmm0 from double to float | -| cvttsd2si rax, xmm0 | f2 48 0f 2c c0 | convert double in xmm0 to int in rax | -| cvtsi2sd xmm0, rax | f2 48 0f 2a c0 | convert int in rax to double in xmm0 | -| comisd xmm0, xmm1 | 66 0f 2f c1 | compare xmm0 and xmm1 | -| addsd xmm0, xmm1 | f2 0f 58 c1 | add xmm1 to xmm0 | -| subsd xmm0, xmm1 | f2 0f 5c c1 | subtract xmm1 from xmm0 | -| mulsd xmm0, xmm1 | f2 0f 59 c1 | multiply xmm0 by xmm1 | -| divsd xmm0, xmm1 | f2 0f 5e c1 | divide xmm0 by xmm1 | -│ call rax │ ff d0 │ call the function at address rax │ -│ ret │ c3 │ return from function │ -│ syscall │ 0f 05 │ execute a system call │ -│ nop │ 90 │ do nothing │ -└──────────────────────┴───────────────────┴────────────────────────────────────────┘ - -SYSCALLS -Arguments are passed in - rdi, rsi, rdx, r10, r8, r9 -The return value is placed in rax. -The values of rsp, rbp and rbx are preserved, but other registers might change. -``` - -## license - -This does not apply to tcc's or musl's source code. - -``` -This project is in the public domain. Any copyright protections from any law -are forfeited by the author(s). No warranty is provided, and the author(s) -shall not be held liable in connection with it. -``` - -## contributing - -If you notice a mistake/want to clarify something, you can submit a pull request -via GitHub, or email `pommicket at pommicket.com`. From 57dde9b71e24df66774774d60a159f3aeafce6dd Mon Sep 17 00:00:00 2001 From: Dawid Sobczak Date: Sat, 19 Apr 2025 09:44:39 +0100 Subject: [PATCH 10/10] fixed rebase --- 05/musl-0.6.0/arch/x86_64/bits/alltypes.h | 468 ++++++++++++++++++++++ 05/musl-0.6.0/include/bits | 1 + 05/musl-final/arch/x86_64/bits/alltypes.h | 468 ++++++++++++++++++++++ 05/musl-final/include/bits | 1 + 4 files changed, 938 insertions(+) create mode 100644 05/musl-0.6.0/arch/x86_64/bits/alltypes.h create mode 120000 05/musl-0.6.0/include/bits create mode 100644 05/musl-final/arch/x86_64/bits/alltypes.h create mode 120000 05/musl-final/include/bits diff --git a/05/musl-0.6.0/arch/x86_64/bits/alltypes.h b/05/musl-0.6.0/arch/x86_64/bits/alltypes.h new file mode 100644 index 0000000..f07dc5c --- /dev/null +++ b/05/musl-0.6.0/arch/x86_64/bits/alltypes.h @@ -0,0 +1,468 @@ + +#include + +#if defined(__NEED_size_t) && !defined(__DEFINED_size_t) +typedef unsigned long size_t; +#define __DEFINED_size_t +#endif + +#if defined(__NEED_ssize_t) && !defined(__DEFINED_ssize_t) +typedef long ssize_t; +#define __DEFINED_ssize_t +#endif + +#if defined(__NEED_ptrdiff_t) && !defined(__DEFINED_ptrdiff_t) +typedef long ptrdiff_t; +#define __DEFINED_ptrdiff_t +#endif + +#if 0 +#if defined(__NEED_va_list) && !defined(__DEFINED_va_list) +typedef __builtin_va_list va_list; +#define __DEFINED_va_list +#endif + +#endif + +#if defined(__NEED_wchar_t) && !defined(__DEFINED_wchar_t) +typedef int wchar_t; +#define __DEFINED_wchar_t +#endif + +#if defined(__NEED_wint_t) && !defined(__DEFINED_wint_t) +typedef int wint_t; +#define __DEFINED_wint_t +#endif + +#if defined(__NEED_wctrans_t) && !defined(__DEFINED_wctrans_t) +typedef int wctrans_t; +#define __DEFINED_wctrans_t +#endif + +#if defined(__NEED_wctype_t) && !defined(__DEFINED_wctype_t) +typedef int wctype_t; +#define __DEFINED_wctype_t +#endif + + +#if defined(__NEED_int8_t) && !defined(__DEFINED_int8_t) +typedef char int8_t; +#define __DEFINED_int8_t +#endif + +#if defined(__NEED_int16_t) && !defined(__DEFINED_int16_t) +typedef short int16_t; +#define __DEFINED_int16_t +#endif + +#if defined(__NEED_int32_t) && !defined(__DEFINED_int32_t) +typedef int int32_t; +#define __DEFINED_int32_t +#endif + +#if defined(__NEED_int64_t) && !defined(__DEFINED_int64_t) +typedef long int64_t; +#define __DEFINED_int64_t +#endif + + +#if defined(__NEED_uint8_t) && !defined(__DEFINED_uint8_t) +typedef unsigned char uint8_t; +#define __DEFINED_uint8_t +#endif + +#if defined(__NEED_uint16_t) && !defined(__DEFINED_uint16_t) +typedef unsigned short uint16_t; +#define __DEFINED_uint16_t +#endif + +#if defined(__NEED_uint32_t) && !defined(__DEFINED_uint32_t) +typedef unsigned int uint32_t; +#define __DEFINED_uint32_t +#endif + +#if defined(__NEED_uint64_t) && !defined(__DEFINED_uint64_t) +typedef unsigned long uint64_t; +#define __DEFINED_uint64_t +#endif + + +#if defined(__NEED___uint8_t) && !defined(__DEFINED___uint8_t) +typedef unsigned char __uint8_t; +#define __DEFINED___uint8_t +#endif + +#if defined(__NEED___uint16_t) && !defined(__DEFINED___uint16_t) +typedef unsigned short __uint16_t; +#define __DEFINED___uint16_t +#endif + +#if defined(__NEED___uint32_t) && !defined(__DEFINED___uint32_t) +typedef unsigned int __uint32_t; +#define __DEFINED___uint32_t +#endif + +#if defined(__NEED___uint64_t) && !defined(__DEFINED___uint64_t) +typedef unsigned long __uint64_t; +#define __DEFINED___uint64_t +#endif + + +#if defined(__NEED_int_least8_t) && !defined(__DEFINED_int_least8_t) +typedef int8_t int_least8_t; +#define __DEFINED_int_least8_t +#endif + +#if defined(__NEED_int_least16_t) && !defined(__DEFINED_int_least16_t) +typedef int16_t int_least16_t; +#define __DEFINED_int_least16_t +#endif + +#if defined(__NEED_int_least32_t) && !defined(__DEFINED_int_least32_t) +typedef int32_t int_least32_t; +#define __DEFINED_int_least32_t +#endif + +#if defined(__NEED_int_least64_t) && !defined(__DEFINED_int_least64_t) +typedef int64_t int_least64_t; +#define __DEFINED_int_least64_t +#endif + + +#if defined(__NEED_uint_least8_t) && !defined(__DEFINED_uint_least8_t) +typedef uint8_t uint_least8_t; +#define __DEFINED_uint_least8_t +#endif + +#if defined(__NEED_uint_least16_t) && !defined(__DEFINED_uint_least16_t) +typedef uint16_t uint_least16_t; +#define __DEFINED_uint_least16_t +#endif + +#if defined(__NEED_uint_least32_t) && !defined(__DEFINED_uint_least32_t) +typedef uint32_t uint_least32_t; +#define __DEFINED_uint_least32_t +#endif + +#if defined(__NEED_uint_least64_t) && !defined(__DEFINED_uint_least64_t) +typedef uint64_t uint_least64_t; +#define __DEFINED_uint_least64_t +#endif + + +#if defined(__NEED_int_fast8_t) && !defined(__DEFINED_int_fast8_t) +typedef int8_t int_fast8_t; +#define __DEFINED_int_fast8_t +#endif + +#if defined(__NEED_int_fast16_t) && !defined(__DEFINED_int_fast16_t) +typedef int int_fast16_t; +#define __DEFINED_int_fast16_t +#endif + +#if defined(__NEED_int_fast32_t) && !defined(__DEFINED_int_fast32_t) +typedef int int_fast32_t; +#define __DEFINED_int_fast32_t +#endif + +#if defined(__NEED_int_fast64_t) && !defined(__DEFINED_int_fast64_t) +typedef int64_t int_fast64_t; +#define __DEFINED_int_fast64_t +#endif + + +#if defined(__NEED_uint_fast8_t) && !defined(__DEFINED_uint_fast8_t) +typedef unsigned char uint_fast8_t; +#define __DEFINED_uint_fast8_t +#endif + +#if defined(__NEED_uint_fast16_t) && !defined(__DEFINED_uint_fast16_t) +typedef unsigned int uint_fast16_t; +#define __DEFINED_uint_fast16_t +#endif + +#if defined(__NEED_uint_fast32_t) && !defined(__DEFINED_uint_fast32_t) +typedef unsigned int uint_fast32_t; +#define __DEFINED_uint_fast32_t +#endif + +#if defined(__NEED_uint_fast64_t) && !defined(__DEFINED_uint_fast64_t) +typedef uint64_t uint_fast64_t; +#define __DEFINED_uint_fast64_t +#endif + + +#if defined(__NEED_intptr_t) && !defined(__DEFINED_intptr_t) +typedef long intptr_t; +#define __DEFINED_intptr_t +#endif + +#if defined(__NEED_uintptr_t) && !defined(__DEFINED_uintptr_t) +typedef unsigned long uintptr_t; +#define __DEFINED_uintptr_t +#endif + + +#if defined(__NEED_intmax_t) && !defined(__DEFINED_intmax_t) +typedef long long intmax_t; +#define __DEFINED_intmax_t +#endif + +#if defined(__NEED_uintmax_t) && !defined(__DEFINED_uintmax_t) +typedef unsigned long long uintmax_t; +#define __DEFINED_uintmax_t +#endif + + +#if defined(__NEED_time_t) && !defined(__DEFINED_time_t) +typedef long time_t; +#define __DEFINED_time_t +#endif + +#if defined(__NEED_useconds_t) && !defined(__DEFINED_useconds_t) +typedef long useconds_t; +#define __DEFINED_useconds_t +#endif + +#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t) +typedef long suseconds_t; +#define __DEFINED_suseconds_t +#endif + +#if defined(__NEED_struct_timeval) && !defined(__DEFINED_struct_timeval) +struct timeval { time_t tv_sec; int tv_usec; }; +#define __DEFINED_struct_timeval +#endif + +#if defined(__NEED_struct_timespec) && !defined(__DEFINED_struct_timespec) +struct timespec { time_t tv_sec; unsigned long tv_nsec; }; +#define __DEFINED_struct_timespec +#endif + + +#if defined(__NEED_pid_t) && !defined(__DEFINED_pid_t) +typedef int pid_t; +#define __DEFINED_pid_t +#endif + +#if defined(__NEED_id_t) && !defined(__DEFINED_id_t) +typedef int id_t; +#define __DEFINED_id_t +#endif + +#if defined(__NEED_uid_t) && !defined(__DEFINED_uid_t) +typedef unsigned int uid_t; +#define __DEFINED_uid_t +#endif + +#if defined(__NEED_gid_t) && !defined(__DEFINED_gid_t) +typedef unsigned int gid_t; +#define __DEFINED_gid_t +#endif + +#if defined(__NEED_key_t) && !defined(__DEFINED_key_t) +typedef int key_t; +#define __DEFINED_key_t +#endif + + +#if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t) +typedef struct __pthread * pthread_t; +#define __DEFINED_pthread_t +#endif + +#if defined(__NEED_pthread_once_t) && !defined(__DEFINED_pthread_once_t) +typedef int pthread_once_t; +#define __DEFINED_pthread_once_t +#endif + +#if defined(__NEED_pthread_key_t) && !defined(__DEFINED_pthread_key_t) +typedef int pthread_key_t; +#define __DEFINED_pthread_key_t +#endif + +#if defined(__NEED_pthread_spinlock_t) && !defined(__DEFINED_pthread_spinlock_t) +typedef int pthread_spinlock_t; +#define __DEFINED_pthread_spinlock_t +#endif + + +#if defined(__NEED_pthread_attr_t) && !defined(__DEFINED_pthread_attr_t) +typedef struct { union { int __i[14]; size_t __s[2]; } __u; } pthread_attr_t; +#define __DEFINED_pthread_attr_t +#endif + +#if defined(__NEED_pthread_mutexattr_t) && !defined(__DEFINED_pthread_mutexattr_t) +typedef unsigned pthread_mutexattr_t; +#define __DEFINED_pthread_mutexattr_t +#endif + +#if defined(__NEED_pthread_condattr_t) && !defined(__DEFINED_pthread_condattr_t) +typedef unsigned pthread_condattr_t; +#define __DEFINED_pthread_condattr_t +#endif + +#if defined(__NEED_pthread_barrierattr_t) && !defined(__DEFINED_pthread_barrierattr_t) +typedef unsigned pthread_barrierattr_t; +#define __DEFINED_pthread_barrierattr_t +#endif + +#if defined(__NEED_pthread_rwlockattr_t) && !defined(__DEFINED_pthread_rwlockattr_t) +typedef struct { unsigned __attr[2]; } pthread_rwlockattr_t; +#define __DEFINED_pthread_rwlockattr_t +#endif + + +#if defined(__NEED_pthread_mutex_t) && !defined(__DEFINED_pthread_mutex_t) +typedef struct { union { int __i[10]; void *__p[1]; } __u; } pthread_mutex_t; +#define __DEFINED_pthread_mutex_t +#endif + +#if defined(__NEED_pthread_cond_t) && !defined(__DEFINED_pthread_cond_t) +typedef struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t; +#define __DEFINED_pthread_cond_t +#endif + +#if defined(__NEED_pthread_rwlock_t) && !defined(__DEFINED_pthread_rwlock_t) +typedef struct { union { int __i[14]; void *__p[1]; } __u; } pthread_rwlock_t; +#define __DEFINED_pthread_rwlock_t +#endif + +#if defined(__NEED_pthread_barrier_t) && !defined(__DEFINED_pthread_barrier_t) +typedef struct { union { int __i[8]; void *__p[1]; } __u; } pthread_barrier_t; +#define __DEFINED_pthread_barrier_t +#endif + + +#if defined(__NEED_off_t) && !defined(__DEFINED_off_t) +typedef long off_t; +#define __DEFINED_off_t +#endif + + +#if defined(__NEED_mode_t) && !defined(__DEFINED_mode_t) +typedef unsigned int mode_t; +#define __DEFINED_mode_t +#endif + + +#if defined(__NEED_nlink_t) && !defined(__DEFINED_nlink_t) +typedef unsigned long nlink_t; +#define __DEFINED_nlink_t +#endif + +#if defined(__NEED_ino_t) && !defined(__DEFINED_ino_t) +typedef unsigned long long ino_t; +#define __DEFINED_ino_t +#endif + +#if defined(__NEED_dev_t) && !defined(__DEFINED_dev_t) +typedef unsigned long dev_t; +#define __DEFINED_dev_t +#endif + +#if defined(__NEED_blksize_t) && !defined(__DEFINED_blksize_t) +typedef long blksize_t; +#define __DEFINED_blksize_t +#endif + +#if defined(__NEED_blkcnt_t) && !defined(__DEFINED_blkcnt_t) +typedef long long blkcnt_t; +#define __DEFINED_blkcnt_t +#endif + +#if defined(__NEED_fsblkcnt_t) && !defined(__DEFINED_fsblkcnt_t) +typedef unsigned long long fsblkcnt_t; +#define __DEFINED_fsblkcnt_t +#endif + +#if defined(__NEED_fsfilcnt_t) && !defined(__DEFINED_fsfilcnt_t) +typedef unsigned long long fsfilcnt_t; +#define __DEFINED_fsfilcnt_t +#endif + + +#if defined(__NEED_timer_t) && !defined(__DEFINED_timer_t) +typedef void * timer_t; +#define __DEFINED_timer_t +#endif + +#if defined(__NEED_clockid_t) && !defined(__DEFINED_clockid_t) +typedef int clockid_t; +#define __DEFINED_clockid_t +#endif + +#if defined(__NEED_clock_t) && !defined(__DEFINED_clock_t) +typedef long clock_t; +#define __DEFINED_clock_t +#endif + + +#if defined(__NEED_sigset_t) && !defined(__DEFINED_sigset_t) +typedef struct { unsigned long __bits[128/sizeof(long)]; } sigset_t; +#define __DEFINED_sigset_t +#endif + +#if 1 +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +typedef struct __siginfo siginfo_t; +#define __DEFINED_siginfo_t +#endif + +#else +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +#define siginfo_t struct __siginfo +#define __DEFINED_siginfo_t +#endif +#endif + +#if defined(__NEED_socklen_t) && !defined(__DEFINED_socklen_t) +typedef unsigned int socklen_t; +#define __DEFINED_socklen_t +#endif + +#if defined(__NEED_sa_family_t) && !defined(__DEFINED_sa_family_t) +typedef unsigned short sa_family_t; +#define __DEFINED_sa_family_t +#endif + +#if defined(__NEED_in_port_t) && !defined(__DEFINED_in_port_t) +typedef unsigned short in_port_t; +#define __DEFINED_in_port_t +#endif + +#if defined(__NEED_in_addr_t) && !defined(__DEFINED_in_addr_t) +typedef unsigned int in_addr_t; +#define __DEFINED_in_addr_t +#endif + +#if defined(__NEED_struct_in_addr) && !defined(__DEFINED_struct_in_addr) +struct in_addr { in_addr_t s_addr; }; +#define __DEFINED_struct_in_addr +#endif + + +#if defined(__NEED_FILE) && !defined(__DEFINED_FILE) +typedef struct __FILE_s FILE; +#define __DEFINED_FILE +#endif + + +#if defined(__NEED_nl_item) && !defined(__DEFINED_nl_item) +typedef int nl_item; +#define __DEFINED_nl_item +#endif + + +#if defined(__NEED_locale_t) && !defined(__DEFINED_locale_t) +typedef struct __locale * locale_t; +#define __DEFINED_locale_t +#endif + + +#if defined(__NEED_struct_iovec) && !defined(__DEFINED_struct_iovec) +struct iovec { void *iov_base; size_t iov_len; }; +#define __DEFINED_struct_iovec +#endif + + diff --git a/05/musl-0.6.0/include/bits b/05/musl-0.6.0/include/bits new file mode 120000 index 0000000..ed2f0a7 --- /dev/null +++ b/05/musl-0.6.0/include/bits @@ -0,0 +1 @@ +../arch/x86_64/bits \ No newline at end of file diff --git a/05/musl-final/arch/x86_64/bits/alltypes.h b/05/musl-final/arch/x86_64/bits/alltypes.h new file mode 100644 index 0000000..f07dc5c --- /dev/null +++ b/05/musl-final/arch/x86_64/bits/alltypes.h @@ -0,0 +1,468 @@ + +#include + +#if defined(__NEED_size_t) && !defined(__DEFINED_size_t) +typedef unsigned long size_t; +#define __DEFINED_size_t +#endif + +#if defined(__NEED_ssize_t) && !defined(__DEFINED_ssize_t) +typedef long ssize_t; +#define __DEFINED_ssize_t +#endif + +#if defined(__NEED_ptrdiff_t) && !defined(__DEFINED_ptrdiff_t) +typedef long ptrdiff_t; +#define __DEFINED_ptrdiff_t +#endif + +#if 0 +#if defined(__NEED_va_list) && !defined(__DEFINED_va_list) +typedef __builtin_va_list va_list; +#define __DEFINED_va_list +#endif + +#endif + +#if defined(__NEED_wchar_t) && !defined(__DEFINED_wchar_t) +typedef int wchar_t; +#define __DEFINED_wchar_t +#endif + +#if defined(__NEED_wint_t) && !defined(__DEFINED_wint_t) +typedef int wint_t; +#define __DEFINED_wint_t +#endif + +#if defined(__NEED_wctrans_t) && !defined(__DEFINED_wctrans_t) +typedef int wctrans_t; +#define __DEFINED_wctrans_t +#endif + +#if defined(__NEED_wctype_t) && !defined(__DEFINED_wctype_t) +typedef int wctype_t; +#define __DEFINED_wctype_t +#endif + + +#if defined(__NEED_int8_t) && !defined(__DEFINED_int8_t) +typedef char int8_t; +#define __DEFINED_int8_t +#endif + +#if defined(__NEED_int16_t) && !defined(__DEFINED_int16_t) +typedef short int16_t; +#define __DEFINED_int16_t +#endif + +#if defined(__NEED_int32_t) && !defined(__DEFINED_int32_t) +typedef int int32_t; +#define __DEFINED_int32_t +#endif + +#if defined(__NEED_int64_t) && !defined(__DEFINED_int64_t) +typedef long int64_t; +#define __DEFINED_int64_t +#endif + + +#if defined(__NEED_uint8_t) && !defined(__DEFINED_uint8_t) +typedef unsigned char uint8_t; +#define __DEFINED_uint8_t +#endif + +#if defined(__NEED_uint16_t) && !defined(__DEFINED_uint16_t) +typedef unsigned short uint16_t; +#define __DEFINED_uint16_t +#endif + +#if defined(__NEED_uint32_t) && !defined(__DEFINED_uint32_t) +typedef unsigned int uint32_t; +#define __DEFINED_uint32_t +#endif + +#if defined(__NEED_uint64_t) && !defined(__DEFINED_uint64_t) +typedef unsigned long uint64_t; +#define __DEFINED_uint64_t +#endif + + +#if defined(__NEED___uint8_t) && !defined(__DEFINED___uint8_t) +typedef unsigned char __uint8_t; +#define __DEFINED___uint8_t +#endif + +#if defined(__NEED___uint16_t) && !defined(__DEFINED___uint16_t) +typedef unsigned short __uint16_t; +#define __DEFINED___uint16_t +#endif + +#if defined(__NEED___uint32_t) && !defined(__DEFINED___uint32_t) +typedef unsigned int __uint32_t; +#define __DEFINED___uint32_t +#endif + +#if defined(__NEED___uint64_t) && !defined(__DEFINED___uint64_t) +typedef unsigned long __uint64_t; +#define __DEFINED___uint64_t +#endif + + +#if defined(__NEED_int_least8_t) && !defined(__DEFINED_int_least8_t) +typedef int8_t int_least8_t; +#define __DEFINED_int_least8_t +#endif + +#if defined(__NEED_int_least16_t) && !defined(__DEFINED_int_least16_t) +typedef int16_t int_least16_t; +#define __DEFINED_int_least16_t +#endif + +#if defined(__NEED_int_least32_t) && !defined(__DEFINED_int_least32_t) +typedef int32_t int_least32_t; +#define __DEFINED_int_least32_t +#endif + +#if defined(__NEED_int_least64_t) && !defined(__DEFINED_int_least64_t) +typedef int64_t int_least64_t; +#define __DEFINED_int_least64_t +#endif + + +#if defined(__NEED_uint_least8_t) && !defined(__DEFINED_uint_least8_t) +typedef uint8_t uint_least8_t; +#define __DEFINED_uint_least8_t +#endif + +#if defined(__NEED_uint_least16_t) && !defined(__DEFINED_uint_least16_t) +typedef uint16_t uint_least16_t; +#define __DEFINED_uint_least16_t +#endif + +#if defined(__NEED_uint_least32_t) && !defined(__DEFINED_uint_least32_t) +typedef uint32_t uint_least32_t; +#define __DEFINED_uint_least32_t +#endif + +#if defined(__NEED_uint_least64_t) && !defined(__DEFINED_uint_least64_t) +typedef uint64_t uint_least64_t; +#define __DEFINED_uint_least64_t +#endif + + +#if defined(__NEED_int_fast8_t) && !defined(__DEFINED_int_fast8_t) +typedef int8_t int_fast8_t; +#define __DEFINED_int_fast8_t +#endif + +#if defined(__NEED_int_fast16_t) && !defined(__DEFINED_int_fast16_t) +typedef int int_fast16_t; +#define __DEFINED_int_fast16_t +#endif + +#if defined(__NEED_int_fast32_t) && !defined(__DEFINED_int_fast32_t) +typedef int int_fast32_t; +#define __DEFINED_int_fast32_t +#endif + +#if defined(__NEED_int_fast64_t) && !defined(__DEFINED_int_fast64_t) +typedef int64_t int_fast64_t; +#define __DEFINED_int_fast64_t +#endif + + +#if defined(__NEED_uint_fast8_t) && !defined(__DEFINED_uint_fast8_t) +typedef unsigned char uint_fast8_t; +#define __DEFINED_uint_fast8_t +#endif + +#if defined(__NEED_uint_fast16_t) && !defined(__DEFINED_uint_fast16_t) +typedef unsigned int uint_fast16_t; +#define __DEFINED_uint_fast16_t +#endif + +#if defined(__NEED_uint_fast32_t) && !defined(__DEFINED_uint_fast32_t) +typedef unsigned int uint_fast32_t; +#define __DEFINED_uint_fast32_t +#endif + +#if defined(__NEED_uint_fast64_t) && !defined(__DEFINED_uint_fast64_t) +typedef uint64_t uint_fast64_t; +#define __DEFINED_uint_fast64_t +#endif + + +#if defined(__NEED_intptr_t) && !defined(__DEFINED_intptr_t) +typedef long intptr_t; +#define __DEFINED_intptr_t +#endif + +#if defined(__NEED_uintptr_t) && !defined(__DEFINED_uintptr_t) +typedef unsigned long uintptr_t; +#define __DEFINED_uintptr_t +#endif + + +#if defined(__NEED_intmax_t) && !defined(__DEFINED_intmax_t) +typedef long long intmax_t; +#define __DEFINED_intmax_t +#endif + +#if defined(__NEED_uintmax_t) && !defined(__DEFINED_uintmax_t) +typedef unsigned long long uintmax_t; +#define __DEFINED_uintmax_t +#endif + + +#if defined(__NEED_time_t) && !defined(__DEFINED_time_t) +typedef long time_t; +#define __DEFINED_time_t +#endif + +#if defined(__NEED_useconds_t) && !defined(__DEFINED_useconds_t) +typedef long useconds_t; +#define __DEFINED_useconds_t +#endif + +#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t) +typedef long suseconds_t; +#define __DEFINED_suseconds_t +#endif + +#if defined(__NEED_struct_timeval) && !defined(__DEFINED_struct_timeval) +struct timeval { time_t tv_sec; int tv_usec; }; +#define __DEFINED_struct_timeval +#endif + +#if defined(__NEED_struct_timespec) && !defined(__DEFINED_struct_timespec) +struct timespec { time_t tv_sec; unsigned long tv_nsec; }; +#define __DEFINED_struct_timespec +#endif + + +#if defined(__NEED_pid_t) && !defined(__DEFINED_pid_t) +typedef int pid_t; +#define __DEFINED_pid_t +#endif + +#if defined(__NEED_id_t) && !defined(__DEFINED_id_t) +typedef int id_t; +#define __DEFINED_id_t +#endif + +#if defined(__NEED_uid_t) && !defined(__DEFINED_uid_t) +typedef unsigned int uid_t; +#define __DEFINED_uid_t +#endif + +#if defined(__NEED_gid_t) && !defined(__DEFINED_gid_t) +typedef unsigned int gid_t; +#define __DEFINED_gid_t +#endif + +#if defined(__NEED_key_t) && !defined(__DEFINED_key_t) +typedef int key_t; +#define __DEFINED_key_t +#endif + + +#if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t) +typedef struct __pthread * pthread_t; +#define __DEFINED_pthread_t +#endif + +#if defined(__NEED_pthread_once_t) && !defined(__DEFINED_pthread_once_t) +typedef int pthread_once_t; +#define __DEFINED_pthread_once_t +#endif + +#if defined(__NEED_pthread_key_t) && !defined(__DEFINED_pthread_key_t) +typedef int pthread_key_t; +#define __DEFINED_pthread_key_t +#endif + +#if defined(__NEED_pthread_spinlock_t) && !defined(__DEFINED_pthread_spinlock_t) +typedef int pthread_spinlock_t; +#define __DEFINED_pthread_spinlock_t +#endif + + +#if defined(__NEED_pthread_attr_t) && !defined(__DEFINED_pthread_attr_t) +typedef struct { union { int __i[14]; size_t __s[2]; } __u; } pthread_attr_t; +#define __DEFINED_pthread_attr_t +#endif + +#if defined(__NEED_pthread_mutexattr_t) && !defined(__DEFINED_pthread_mutexattr_t) +typedef unsigned pthread_mutexattr_t; +#define __DEFINED_pthread_mutexattr_t +#endif + +#if defined(__NEED_pthread_condattr_t) && !defined(__DEFINED_pthread_condattr_t) +typedef unsigned pthread_condattr_t; +#define __DEFINED_pthread_condattr_t +#endif + +#if defined(__NEED_pthread_barrierattr_t) && !defined(__DEFINED_pthread_barrierattr_t) +typedef unsigned pthread_barrierattr_t; +#define __DEFINED_pthread_barrierattr_t +#endif + +#if defined(__NEED_pthread_rwlockattr_t) && !defined(__DEFINED_pthread_rwlockattr_t) +typedef struct { unsigned __attr[2]; } pthread_rwlockattr_t; +#define __DEFINED_pthread_rwlockattr_t +#endif + + +#if defined(__NEED_pthread_mutex_t) && !defined(__DEFINED_pthread_mutex_t) +typedef struct { union { int __i[10]; void *__p[1]; } __u; } pthread_mutex_t; +#define __DEFINED_pthread_mutex_t +#endif + +#if defined(__NEED_pthread_cond_t) && !defined(__DEFINED_pthread_cond_t) +typedef struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t; +#define __DEFINED_pthread_cond_t +#endif + +#if defined(__NEED_pthread_rwlock_t) && !defined(__DEFINED_pthread_rwlock_t) +typedef struct { union { int __i[14]; void *__p[1]; } __u; } pthread_rwlock_t; +#define __DEFINED_pthread_rwlock_t +#endif + +#if defined(__NEED_pthread_barrier_t) && !defined(__DEFINED_pthread_barrier_t) +typedef struct { union { int __i[8]; void *__p[1]; } __u; } pthread_barrier_t; +#define __DEFINED_pthread_barrier_t +#endif + + +#if defined(__NEED_off_t) && !defined(__DEFINED_off_t) +typedef long off_t; +#define __DEFINED_off_t +#endif + + +#if defined(__NEED_mode_t) && !defined(__DEFINED_mode_t) +typedef unsigned int mode_t; +#define __DEFINED_mode_t +#endif + + +#if defined(__NEED_nlink_t) && !defined(__DEFINED_nlink_t) +typedef unsigned long nlink_t; +#define __DEFINED_nlink_t +#endif + +#if defined(__NEED_ino_t) && !defined(__DEFINED_ino_t) +typedef unsigned long long ino_t; +#define __DEFINED_ino_t +#endif + +#if defined(__NEED_dev_t) && !defined(__DEFINED_dev_t) +typedef unsigned long dev_t; +#define __DEFINED_dev_t +#endif + +#if defined(__NEED_blksize_t) && !defined(__DEFINED_blksize_t) +typedef long blksize_t; +#define __DEFINED_blksize_t +#endif + +#if defined(__NEED_blkcnt_t) && !defined(__DEFINED_blkcnt_t) +typedef long long blkcnt_t; +#define __DEFINED_blkcnt_t +#endif + +#if defined(__NEED_fsblkcnt_t) && !defined(__DEFINED_fsblkcnt_t) +typedef unsigned long long fsblkcnt_t; +#define __DEFINED_fsblkcnt_t +#endif + +#if defined(__NEED_fsfilcnt_t) && !defined(__DEFINED_fsfilcnt_t) +typedef unsigned long long fsfilcnt_t; +#define __DEFINED_fsfilcnt_t +#endif + + +#if defined(__NEED_timer_t) && !defined(__DEFINED_timer_t) +typedef void * timer_t; +#define __DEFINED_timer_t +#endif + +#if defined(__NEED_clockid_t) && !defined(__DEFINED_clockid_t) +typedef int clockid_t; +#define __DEFINED_clockid_t +#endif + +#if defined(__NEED_clock_t) && !defined(__DEFINED_clock_t) +typedef long clock_t; +#define __DEFINED_clock_t +#endif + + +#if defined(__NEED_sigset_t) && !defined(__DEFINED_sigset_t) +typedef struct { unsigned long __bits[128/sizeof(long)]; } sigset_t; +#define __DEFINED_sigset_t +#endif + +#if 1 +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +typedef struct __siginfo siginfo_t; +#define __DEFINED_siginfo_t +#endif + +#else +#if defined(__NEED_siginfo_t) && !defined(__DEFINED_siginfo_t) +#define siginfo_t struct __siginfo +#define __DEFINED_siginfo_t +#endif +#endif + +#if defined(__NEED_socklen_t) && !defined(__DEFINED_socklen_t) +typedef unsigned int socklen_t; +#define __DEFINED_socklen_t +#endif + +#if defined(__NEED_sa_family_t) && !defined(__DEFINED_sa_family_t) +typedef unsigned short sa_family_t; +#define __DEFINED_sa_family_t +#endif + +#if defined(__NEED_in_port_t) && !defined(__DEFINED_in_port_t) +typedef unsigned short in_port_t; +#define __DEFINED_in_port_t +#endif + +#if defined(__NEED_in_addr_t) && !defined(__DEFINED_in_addr_t) +typedef unsigned int in_addr_t; +#define __DEFINED_in_addr_t +#endif + +#if defined(__NEED_struct_in_addr) && !defined(__DEFINED_struct_in_addr) +struct in_addr { in_addr_t s_addr; }; +#define __DEFINED_struct_in_addr +#endif + + +#if defined(__NEED_FILE) && !defined(__DEFINED_FILE) +typedef struct __FILE_s FILE; +#define __DEFINED_FILE +#endif + + +#if defined(__NEED_nl_item) && !defined(__DEFINED_nl_item) +typedef int nl_item; +#define __DEFINED_nl_item +#endif + + +#if defined(__NEED_locale_t) && !defined(__DEFINED_locale_t) +typedef struct __locale * locale_t; +#define __DEFINED_locale_t +#endif + + +#if defined(__NEED_struct_iovec) && !defined(__DEFINED_struct_iovec) +struct iovec { void *iov_base; size_t iov_len; }; +#define __DEFINED_struct_iovec +#endif + + diff --git a/05/musl-final/include/bits b/05/musl-final/include/bits new file mode 120000 index 0000000..ed2f0a7 --- /dev/null +++ b/05/musl-final/include/bits @@ -0,0 +1 @@ +../arch/x86_64/bits \ No newline at end of file