From 7ff8413a0317566d67b5b4abcb6ee2b72e9d465b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 13 Dec 2018 05:40:33 -0800 Subject: Ported some tests. --- tests/pb/test_decoder.cc | 102 +---------------------------------------------- 1 file changed, 2 insertions(+), 100 deletions(-) (limited to 'tests/pb/test_decoder.cc') diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index 49544b2..a931779 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -399,107 +399,8 @@ void reg_str(upb_handlers *h, uint32_t num) { ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } -void AddField(upb_descriptortype_t descriptor_type, const std::string& name, - uint32_t fn, bool repeated, upb::MessageDef* md) { - // TODO: Fluent interface? ie. - // ASSERT(md->AddField(upb::BuildFieldDef() - // .SetName("f_message") - // .SetNumber(UPB_DESCRIPTOR_TYPE_MESSAGE) - // .SetDescriptorType(UPB_DESCRIPTOR_TYPE_MESSAGE) - // .SetMessageSubdef(md.get()))); - upb::reffed_ptr f = upb::FieldDef::New(); - ASSERT(f->set_name(name, NULL)); - ASSERT(f->set_number(fn, NULL)); - f->set_label(repeated ? UPB_LABEL_REPEATED : UPB_LABEL_OPTIONAL); - f->set_descriptor_type(descriptor_type); - ASSERT(md->AddField(f.get(), NULL)); -} - -void AddFieldsForType(upb_descriptortype_t descriptor_type, - const char* basename, upb::MessageDef* md) { - const upb_descriptortype_t t = descriptor_type; - AddField(t, std::string("f_") + basename, t, false, md); - AddField(t, std::string("r_") + basename, rep_fn(t), true, md); -} - -upb::reffed_ptr NewMessageDef() { - upb::reffed_ptr md = upb::MessageDef::New(); - - md->set_full_name("DecoderTest", NULL); - - AddFieldsForType(UPB_DESCRIPTOR_TYPE_DOUBLE, "double", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_FLOAT, "float", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_INT64, "int64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_UINT64, "uint64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_INT32, "int32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_FIXED64, "fixed64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_FIXED32, "fixed32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_BOOL, "bool", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_STRING, "string", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_BYTES, "bytes", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_UINT32, "uint32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SFIXED32, "sfixed32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SFIXED64, "sfixed64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SINT32, "sint32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SINT64, "sint64", md.get()); - - AddField(UPB_DESCRIPTOR_TYPE_STRING, "nop_field", 40, false, md.get()); - - upb::reffed_ptr f = upb::FieldDef::New(); - ASSERT(f->set_name("f_message", NULL)); - ASSERT(f->set_number(UPB_DESCRIPTOR_TYPE_MESSAGE, NULL)); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_MESSAGE); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("r_message", NULL)); - ASSERT(f->set_number(rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE), NULL)); - f->set_label(UPB_LABEL_REPEATED); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_MESSAGE); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("f_group", NULL)); - ASSERT(f->set_number(UPB_DESCRIPTOR_TYPE_GROUP, NULL)); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_GROUP); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("r_group", NULL)); - ASSERT(f->set_number(rep_fn(UPB_DESCRIPTOR_TYPE_GROUP), NULL)); - f->set_label(UPB_LABEL_REPEATED); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_GROUP); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - upb::reffed_ptr e = upb::EnumDef::New(); - ASSERT(e->AddValue("FOO", 1, NULL)); - ASSERT(e->Freeze(NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("f_enum", NULL)); - ASSERT(f->set_number(UPB_DESCRIPTOR_TYPE_ENUM, NULL)); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_ENUM); - ASSERT(f->set_enum_subdef(e.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("r_enum", NULL)); - ASSERT(f->set_number(rep_fn(UPB_DESCRIPTOR_TYPE_ENUM), NULL)); - f->set_label(UPB_LABEL_REPEATED); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_ENUM); - ASSERT(f->set_enum_subdef(e.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - ASSERT(md->Freeze(NULL)); - - return md; -} - upb::reffed_ptr NewHandlers(TestMode mode) { + upb::reffed_ptr h(upb::Handlers::New(NewMessageDef().get())); if (mode == ALL_HANDLERS) { @@ -1277,6 +1178,7 @@ upb::reffed_ptr method = void run_tests(bool use_jit) { upb::reffed_ptr method; upb::reffed_ptr handlers; + upb::SymbolTable symtab; handlers = NewHandlers(test_mode); global_handlers = handlers.get(); -- cgit v1.2.3 From 377871f10403c7b4e1cc6f769b9443b5197aecc8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 16 Dec 2018 14:32:14 -0800 Subject: Got test_decoder working! --- BUILD | 15 + CMakeLists.txt | 1 + build_defs.bzl | 51 ++- tests/pb/test_decoder.cc | 91 +++--- tests/pb/test_decoder.proto | 42 +++ tools/dump_cinit.lua | 749 -------------------------------------------- tools/make_cmakelists.py | 3 + tools/upbc.lua | 91 ------ upb/def.c | 51 ++- upb/def.h | 10 +- upb/upb.h | 1 - upbc/generator.cc | 104 ++++++ 12 files changed, 317 insertions(+), 892 deletions(-) delete mode 100644 tools/dump_cinit.lua delete mode 100644 tools/upbc.lua (limited to 'tests/pb/test_decoder.cc') diff --git a/BUILD b/BUILD index 9b8513d..b3f397a 100644 --- a/BUILD +++ b/BUILD @@ -8,6 +8,7 @@ load( "make_shell_script", "upb_amalgamation", "upb_proto_library", + "upb_proto_reflection_library", ) # C/C++ rules ################################################################## @@ -167,10 +168,24 @@ cc_test( ], ) +proto_library( + name = "test_decoder_proto", + srcs = [ + "tests/pb/test_decoder.proto" + ] +) + +upb_proto_reflection_library( + name = "test_decoder_upbproto", + deps = ["test_decoder_proto"], + upbc = ":protoc-gen-upb", +) + cc_test( name = "test_decoder", srcs = ["tests/pb/test_decoder.cc"], deps = [ + ":test_decoder_upbproto", ":upb_pb", ":upb_test", ], diff --git a/CMakeLists.txt b/CMakeLists.txt index 6133c16..3439aac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,6 +131,7 @@ add_executable(test_decoder tests/pb/test_decoder.cc) add_test(NAME test_decoder COMMAND test_decoder) target_link_libraries(test_decoder + test_decoder_upbproto upb_pb upb_test) add_executable(test_encoder diff --git a/build_defs.bzl b/build_defs.bzl index 8251014..3867976 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -217,7 +217,7 @@ def _remove_up(string): return _remove_suffix(string, ".proto") -def _upb_proto_library_srcs_impl(ctx): +def _upb_proto_srcs_impl(ctx, suffix): sources = [] outs = [] include_dirs = {} @@ -225,14 +225,19 @@ def _upb_proto_library_srcs_impl(ctx): if hasattr(dep, 'proto'): for src in dep.proto.transitive_sources: sources.append(src) - include_dirs[_remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension)] = True - outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + ".upb.h")) - outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + ".upb.c")) - outdir = _remove_suffix(outs[-1].path, _remove_up(src.short_path) + ".upb.c") + include_dir = _remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension) + if include_dir: + include_dirs[include_dir] = True + outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + suffix + ".h")) + outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + suffix + ".c")) + outdir = _remove_suffix(outs[-1].path, _remove_up(src.short_path) + suffix + ".c") source_paths = [d.path for d in sources] include_args = ["-I" + root for root in include_dirs.keys()] + print(source_paths) + print(include_args) + ctx.actions.run( inputs = [ctx.executable.upbc] + sources, outputs = outs, @@ -243,6 +248,12 @@ def _upb_proto_library_srcs_impl(ctx): return [DefaultInfo(files = depset(outs))] +def _upb_proto_library_srcs_impl(ctx): + return _upb_proto_srcs_impl(ctx, ".upb") + +def _upb_proto_reflection_library_srcs_impl(ctx): + return _upb_proto_srcs_impl(ctx, ".upbdefs") + _upb_proto_library_srcs = rule( implementation = _upb_proto_library_srcs_impl, attrs = { @@ -272,3 +283,33 @@ def upb_proto_library(name, deps, upbc): deps = [":upb"], copts = ["-Ibazel-out/k8-fastbuild/bin"], ) + +_upb_proto_reflection_library_srcs = rule( + implementation = _upb_proto_reflection_library_srcs_impl, + attrs = { + "upbc": attr.label( + executable = True, + cfg = "host", + ), + "protoc": attr.label( + executable = True, + cfg = "host", + default = "@com_google_protobuf//:protoc", + ), + "deps": attr.label_list(), + } +) + +def upb_proto_reflection_library(name, deps, upbc): + srcs_rule = name + "_defsrcs.cc" + _upb_proto_reflection_library_srcs( + name = srcs_rule, + upbc = upbc, + deps = deps, + ) + native.cc_library( + name = name, + srcs = [":" + srcs_rule], + deps = [":upb"], + copts = ["-Ibazel-out/k8-fastbuild/bin"], + ) diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index a931779..d0e3fa3 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -36,6 +36,7 @@ #include "tests/test_util.h" #include "tests/upb_test.h" +#include "tests/pb/test_decoder.upbdefs.h" #ifdef AMALGAMATED #include "upb.h" @@ -387,7 +388,6 @@ void reg_subm(upb_handlers *h, uint32_t num) { ASSERT( h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); - ASSERT(upb_handlers_setsubhandlers(h, f, h)); } void reg_str(upb_handlers *h, uint32_t num) { @@ -399,52 +399,60 @@ void reg_str(upb_handlers *h, uint32_t num) { ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } -upb::reffed_ptr NewHandlers(TestMode mode) { - - upb::reffed_ptr h(upb::Handlers::New(NewMessageDef().get())); +struct HandlerRegisterData { + TestMode mode; +}; - if (mode == ALL_HANDLERS) { +void callback(const void *closure, upb_handlers *h) { + const HandlerRegisterData* data = + static_cast(closure); + if (data->mode == ALL_HANDLERS) { h->SetStartMessageHandler(UpbMakeHandler(startmsg)); h->SetEndMessageHandler(UpbMakeHandler(endmsg)); // Register handlers for each type. - reg(h.get(), UPB_DESCRIPTOR_TYPE_DOUBLE); - reg (h.get(), UPB_DESCRIPTOR_TYPE_FLOAT); - reg (h.get(), UPB_DESCRIPTOR_TYPE_INT64); - reg(h.get(), UPB_DESCRIPTOR_TYPE_UINT64); - reg (h.get(), UPB_DESCRIPTOR_TYPE_INT32); - reg(h.get(), UPB_DESCRIPTOR_TYPE_FIXED64); - reg(h.get(), UPB_DESCRIPTOR_TYPE_FIXED32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_BOOL); - reg(h.get(), UPB_DESCRIPTOR_TYPE_UINT32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_ENUM); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED64); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SINT32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SINT64); - - reg_str(h.get(), UPB_DESCRIPTOR_TYPE_STRING); - reg_str(h.get(), UPB_DESCRIPTOR_TYPE_BYTES); - reg_str(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_STRING)); - reg_str(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_BYTES)); + reg(h, UPB_DESCRIPTOR_TYPE_DOUBLE); + reg (h, UPB_DESCRIPTOR_TYPE_FLOAT); + reg (h, UPB_DESCRIPTOR_TYPE_INT64); + reg(h, UPB_DESCRIPTOR_TYPE_UINT64); + reg (h, UPB_DESCRIPTOR_TYPE_INT32); + reg(h, UPB_DESCRIPTOR_TYPE_FIXED64); + reg(h, UPB_DESCRIPTOR_TYPE_FIXED32); + reg (h, UPB_DESCRIPTOR_TYPE_BOOL); + reg(h, UPB_DESCRIPTOR_TYPE_UINT32); + reg (h, UPB_DESCRIPTOR_TYPE_ENUM); + reg (h, UPB_DESCRIPTOR_TYPE_SFIXED32); + reg (h, UPB_DESCRIPTOR_TYPE_SFIXED64); + reg (h, UPB_DESCRIPTOR_TYPE_SINT32); + reg (h, UPB_DESCRIPTOR_TYPE_SINT64); + + reg_str(h, UPB_DESCRIPTOR_TYPE_STRING); + reg_str(h, UPB_DESCRIPTOR_TYPE_BYTES); + reg_str(h, rep_fn(UPB_DESCRIPTOR_TYPE_STRING)); + reg_str(h, rep_fn(UPB_DESCRIPTOR_TYPE_BYTES)); // Register submessage/group handlers that are self-recursive // to this type, eg: message M { optional M m = 1; } - reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_MESSAGE); - reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); - reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_GROUP); - reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); + reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); + reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); + + if (h->message_def()->full_name() == std::string("DecoderTest")) { + reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP); + reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); + } // For NOP_FIELD we register no handlers, so we can pad a proto freely without // changing the output. } - - bool ok = h->Freeze(NULL); - ASSERT(ok); - - return h; } +upb::reffed_ptr NewHandlers(upb::SymbolTable* symtab, + TestMode mode) { + HandlerRegisterData handlerdata; + handlerdata.mode = mode; + return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback, + &handlerdata); +} /* Running of test cases ******************************************************/ @@ -1132,14 +1140,11 @@ upb::reffed_ptr NewMethod( return cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers)); } -void test_emptyhandlers(bool allowjit) { +void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { // Create an empty handlers to make sure that the decoder can handle empty // messages. - upb::reffed_ptr md = upb::MessageDef::New(); - ASSERT(md->set_full_name("Empty", NULL)); - ASSERT(md->Freeze(NULL)); - - upb::reffed_ptr h(upb::Handlers::New(md.get())); + const upb::MessageDef* md = Empty_getmsgdef(symtab); + upb::reffed_ptr h(upb::Handlers::New(md)); bool ok = h->Freeze(NULL); ASSERT(ok); upb::reffed_ptr method = @@ -1178,9 +1183,9 @@ upb::reffed_ptr method = void run_tests(bool use_jit) { upb::reffed_ptr method; upb::reffed_ptr handlers; - upb::SymbolTable symtab; + upb::SymbolTable* symtab = upb::SymbolTable::New(); - handlers = NewHandlers(test_mode); + handlers = NewHandlers(symtab, test_mode); global_handlers = handlers.get(); method = NewMethod(handlers.get(), use_jit); @@ -1191,7 +1196,9 @@ void run_tests(bool use_jit) { test_invalid(); test_valid(); - test_emptyhandlers(use_jit); + test_emptyhandlers(symtab, use_jit); + + upb::SymbolTable::Free(symtab); } void run_test_suite() { diff --git a/tests/pb/test_decoder.proto b/tests/pb/test_decoder.proto index 8197dea..e9fa6ad 100644 --- a/tests/pb/test_decoder.proto +++ b/tests/pb/test_decoder.proto @@ -5,6 +5,8 @@ enum TestEnum { FOO = 1; } +message Empty {} + message DecoderTest { optional double f_double = 1; optional float f_float = 2; @@ -62,6 +64,26 @@ message DecoderTest { optional sfixed64 f_sfixed64 = 16; optional sint32 f_sint32 = 17; optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; } optional group R_group = 536869921 { @@ -82,5 +104,25 @@ message DecoderTest { optional sfixed64 f_sfixed64 = 16; optional sint32 f_sint32 = 17; optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; } } diff --git a/tools/dump_cinit.lua b/tools/dump_cinit.lua deleted file mode 100644 index 93ee12e..0000000 --- a/tools/dump_cinit.lua +++ /dev/null @@ -1,749 +0,0 @@ ---[[ - - Routines for dumping internal data structures into C initializers - that can be compiled into a .o file. - ---]] - -local upbtable = require "upb.table" -local upb = require "upb" -local export = {} - --- A tiny little abstraction that decouples the dump_* functions from --- what they're writing to (appending to a string, writing to file I/O, etc). --- This could possibly matter since naive string building is O(n^2) in the --- number of appends. -function export.str_appender() - local str = "" - local function append(fmt, ...) - str = str .. string.format(fmt, ...) - end - local function get() - return str - end - return append, get -end - -function export.file_appender(file) - local f = file - local function append(fmt, ...) - f:write(string.format(fmt, ...)) - end - return append -end - -function handler_types(base) - local ret = {} - for k, _ in pairs(base) do - if string.find(k, "^" .. "HANDLER_") then - ret[#ret + 1] = k - end - end - return ret -end - -function octchar(num) - assert(num < 8) - local idx = num + 1 -- 1-based index - return string.sub("01234567", idx, idx) -end - -function c_escape(num) - assert(num < 256) - return string.format("\\%s%s%s", - octchar(math.floor(num / 64)), - octchar(math.floor(num / 8) % 8), - octchar(num % 8)); -end - --- const(f, label) -> UPB_LABEL_REPEATED, where f:label() == upb.LABEL_REPEATED -function const(obj, name, base) - local val = obj[name] - base = base or upb - - -- Support both f:label() and f.label. - if type(val) == "function" then - val = val(obj) - end - - for k, v in pairs(base) do - if v == val and string.find(k, "^" .. string.upper(name)) then - return "UPB_" .. k - end - end - assert(false, "Couldn't find UPB_" .. string.upper(name) .. - " constant for value: " .. val) -end - -function sortedkeys(tab) - arr = {} - for key in pairs(tab) do - arr[#arr + 1] = key - end - table.sort(arr) - return arr -end - -function sorted_defs(defs) - local sorted = {} - - for def in defs do - if def.type == deftype then - sorted[#sorted + 1] = def - end - end - - table.sort(sorted, - function(a, b) return a:full_name() < b:full_name() end) - - return sorted -end - -function constlist(pattern) - local ret = {} - for k, v in pairs(upb) do - if string.find(k, "^" .. pattern) then - ret[k] = v - end - end - return ret -end - -function boolstr(val) - if val == true then - return "true" - elseif val == false then - return "false" - else - assert(false, "Bad bool value: " .. tostring(val)) - end -end - ---[[ - - LinkTable: an object that tracks all linkable objects and their offsets to - facilitate linking. - ---]] - -local LinkTable = {} -function LinkTable:new(types) - local linktab = { - types = types, - table = {}, -- ptr -> {type, 0-based offset} - obj_arrays = {} -- Establishes the ordering for each object type - } - for type, _ in pairs(types) do - linktab.obj_arrays[type] = {} - end - setmetatable(linktab, {__index = LinkTable}) -- Inheritance - return linktab -end - --- Adds a new object to the sequence of objects of this type. -function LinkTable:add(objtype, ptr, obj) - obj = obj or ptr - assert(self.table[obj] == nil) - assert(self.types[objtype]) - local arr = self.obj_arrays[objtype] - self.table[ptr] = {objtype, #arr} - arr[#arr + 1] = obj -end - --- Returns a C symbol name for the given objtype and offset. -function LinkTable:csym(objtype, offset) - local typestr = assert(self.types[objtype]) - return string.format("%s[%d]", typestr, offset) -end - --- Returns the address of the given C object. -function LinkTable:addr(obj) - if obj == upbtable.NULL then - return "NULL" - else - local tabent = assert(self.table[obj], "unknown object: " .. tostring(obj)) - return "&" .. self:csym(tabent[1], tabent[2]) - end -end - --- Returns an array declarator indicating how many objects have been added. -function LinkTable:cdecl(objtype) - return self:csym(objtype, #self.obj_arrays[objtype]) -end - -function LinkTable:objs(objtype) - -- Return iterator function, allowing use as: - -- for obj in linktable:objs(type) do - -- -- ... - -- done - local array = self.obj_arrays[objtype] - local i = 0 - return function() - i = i + 1 - if array[i] then return array[i] end - end -end - -function LinkTable:empty(objtype) - return #self.obj_arrays[objtype] == 0 -end - ---[[ - - Dumper: an object that can dump C initializers for several constructs. - Uses a LinkTable to resolve references when necessary. - ---]] - -local Dumper = {} -function Dumper:new(linktab) - local obj = {linktab = linktab} - setmetatable(obj, {__index = Dumper}) -- Inheritance - return obj -end - --- Dumps a upb_tabval, eg: --- UPB_TABVALUE_INIT(5) -function Dumper:_value(val, upbtype) - if type(val) == "nil" then - return "UPB_TABVALUE_EMPTY_INIT" - elseif type(val) == "number" then - -- Use upbtype to disambiguate what kind of number it is. - if upbtype == upbtable.CTYPE_INT32 then - return string.format("UPB_TABVALUE_INT_INIT(%d)", val) - else - -- TODO(haberman): add support for these so we can properly support - -- default values. - error("Unsupported number type " .. upbtype) - end - elseif type(val) == "string" then - return string.format('UPB_TABVALUE_PTR_INIT("%s")', val) - else - -- We take this as an object reference that has an entry in the link table. - return string.format("UPB_TABVALUE_PTR_INIT(%s)", self.linktab:addr(val)) - end -end - --- Dumps a table key. -function Dumper:tabkey(key) - if type(key) == "nil" then - return "UPB_TABKEY_NONE" - elseif type(key) == "string" then - local len = #key - local len1 = c_escape(len % 256) - local len2 = c_escape(math.floor(len / 256) % 256) - local len3 = c_escape(math.floor(len / (256 * 256)) % 256) - local len4 = c_escape(math.floor(len / (256 * 256 * 256)) % 256) - return string.format('UPB_TABKEY_STR("%s", "%s", "%s", "%s", "%s")', - len1, len2, len3, len4, key) - else - return string.format("UPB_TABKEY_NUM(%d)", key) - end -end - --- Dumps a table entry. -function Dumper:tabent(ent) - local key = self:tabkey(ent.key) - local val = self:_value(ent.value, ent.valtype) - local next = self.linktab:addr(ent.next) - return string.format(' {%s, %s, %s},\n', key, val, next) -end - --- Dumps an inttable array entry. This is almost the same as value() above, --- except that nil values have a special value to indicate "empty". -function Dumper:arrayval(val) - if val.val then - return string.format(" %s,\n", self:_value(val.val, val.valtype)) - else - return " UPB_TABVALUE_EMPTY_INIT,\n" - end -end - --- Dumps an initializer for the given strtable/inttable (respectively). Its --- entries must have previously been added to the linktable. -function Dumper:strtable(t) - -- UPB_STRTABLE_INIT(count, mask, type, size_lg2, entries) - return string.format( - "UPB_STRTABLE_INIT(%d, %d, %s, %d, %s)", - t.count, t.mask, const(t, "ctype", upbtable) , t.size_lg2, - self.linktab:addr(t.entries[1].ptr)) -end - -function Dumper:inttable(t) - local lt = assert(self.linktab) - -- UPB_INTTABLE_INIT(count, mask, type, size_lg2, ent, a, asize, acount) - local entries = "NULL" - if #t.entries > 0 then - entries = lt:addr(t.entries[1].ptr) - end - return string.format( - "UPB_INTTABLE_INIT(%d, %d, %s, %d, %s, %s, %d, %d)", - t.count, t.mask, const(t, "ctype", upbtable), t.size_lg2, entries, - lt:addr(t.array[1].ptr), t.array_size, t.array_count) -end - --- A visitor for visiting all tables of a def. Used first to count entries --- and later to dump them. -local function gettables(def) - if def:def_type() == upb.DEF_MSG then - return {int = upbtable.msgdef_itof(def), str = upbtable.msgdef_ntof(def)} - elseif def:def_type() == upb.DEF_ENUM then - return {int = upbtable.enumdef_iton(def), str = upbtable.enumdef_ntoi(def)} - end -end - -local function emit_file_warning(filedef, append) - append('/* This file was generated by upbc (the upb compiler) from the input\n') - append(' * file:\n') - append(' *\n') - append(' * %s\n', filedef:name()) - append(' *\n') - append(' * Do not edit -- your changes will be discarded when the file is\n') - append(' * regenerated. */\n\n') -end - -local function join(...) - return table.concat({...}, ".") -end - -local function split(str) - local ret = {} - for word in string.gmatch(str, "%w+") do - table.insert(ret, word) - end - return ret -end - -local function to_cident(...) - return string.gsub(join(...), "[%./]", "_") -end - -local function to_preproc(...) - return string.upper(to_cident(...)) -end - --- Strips away last path element, ie: --- foo.Bar.Baz -> foo.Bar -local function remove_name(name) - local package_end = 0 - for i=1,string.len(name) do - if string.byte(name, i) == string.byte(".", 1) then - package_end = i - 1 - end - end - return string.sub(name, 1, package_end) -end - -local function start_namespace(package, append) - local package_components = split(package) - for _, component in ipairs(package_components) do - append("namespace %s {\n", component) - end -end - -local function end_namespace(package, append) - local package_components = split(package) - for i=#package_components,1,-1 do - append("} /* namespace %s */\n", package_components[i]) - end -end - -local function well_known_type(m) - local type_map = {} - type_map["google.protobuf.Duration"] = "UPB_WELLKNOWN_DURATION" - type_map["google.protobuf.Timestamp"] = "UPB_WELLKNOWN_TIMESTAMP" - type_map["google.protobuf.Value"] = "UPB_WELLKNOWN_VALUE" - type_map["google.protobuf.ListValue"] = "UPB_WELLKNOWN_LISTVALUE" - type_map["google.protobuf.Struct"] = "UPB_WELLKNOWN_STRUCT" - type_map["google.protobuf.DoubleValue"] = "UPB_WELLKNOWN_DOUBLEVALUE" - type_map["google.protobuf.FloatValue"] = "UPB_WELLKNOWN_FLOATVALUE" - type_map["google.protobuf.Int64Value"] = "UPB_WELLKNOWN_INT64VALUE" - type_map["google.protobuf.UInt64Value"] = "UPB_WELLKNOWN_UINT64VALUE" - type_map["google.protobuf.Int32Value"] = "UPB_WELLKNOWN_INT32VALUE" - type_map["google.protobuf.UInt32Value"] = "UPB_WELLKNOWN_UINT32VALUE" - type_map["google.protobuf.BoolValue"] = "UPB_WELLKNOWN_BOOLVALUE" - type_map["google.protobuf.StringValue"] = "UPB_WELLKNOWN_STRINGVALUE" - type_map["google.protobuf.BytesValue"] = "UPB_WELLKNOWN_BYTESVALUE" - local t = type_map[m:full_name()] - if (t == nil) then - t = "UPB_WELLKNOWN_UNSPECIFIED" - end - return t -end - ---[[ - - Top-level, exported dumper functions - ---]] - -local function dump_defs_c(filedef, append) - local defs = {} - for def in filedef:defs(upb.DEF_ANY) do - defs[#defs + 1] = def - if (def:def_type() == upb.DEF_MSG) then - for field in def:fields() do - defs[#defs + 1] = field - end - end - end - - -- Sort all defs by (type, name). - -- This gives us a linear ordering that we can use to create offsets into - -- shared arrays like REFTABLES, hash table entries, and arrays. - table.sort(defs, function(a, b) - if a:def_type() ~= b:def_type() then - return a:def_type() < b:def_type() - else - return a:full_name() < b:full_name() end - end - ) - - -- Perform pre-pass to build the link table. - local linktab = LinkTable:new{ - [upb.DEF_MSG] = "msgs", - [upb.DEF_FIELD] = "fields", - [upb.DEF_ENUM] = "enums", - intentries = "intentries", - strentries = "strentries", - arrays = "arrays", - } - local reftable_count = 0 - - for _, def in ipairs(defs) do - assert(def:is_frozen(), "can only dump frozen defs.") - linktab:add(def:def_type(), def) - reftable_count = reftable_count + 2 - local tables = gettables(def) - if tables then - for _, e in ipairs(tables.str.entries) do - linktab:add("strentries", e.ptr, e) - end - for _, e in ipairs(tables.int.entries) do - linktab:add("intentries", e.ptr, e) - end - for _, e in ipairs(tables.int.array) do - linktab:add("arrays", e.ptr, e) - end - end - end - - -- Emit forward declarations. - emit_file_warning(filedef, append) - append('#include "upb/def.h"\n') - append('#include "upb/structdefs.int.h"\n\n') - append("static const upb_msgdef %s;\n", linktab:cdecl(upb.DEF_MSG)) - append("static const upb_fielddef %s;\n", linktab:cdecl(upb.DEF_FIELD)) - if not linktab:empty(upb.DEF_ENUM) then - append("static const upb_enumdef %s;\n", linktab:cdecl(upb.DEF_ENUM)) - end - append("static const upb_tabent %s;\n", linktab:cdecl("strentries")) - if not linktab:empty("intentries") then - append("static const upb_tabent %s;\n", linktab:cdecl("intentries")) - end - append("static const upb_tabval %s;\n", linktab:cdecl("arrays")) - append("\n") - append("#ifdef UPB_DEBUG_REFS\n") - append("static upb_inttable reftables[%d];\n", reftable_count) - append("#endif\n") - append("\n") - - -- Emit defs. - local dumper = Dumper:new(linktab) - - local reftable = 0 - - append("static const upb_msgdef %s = {\n", linktab:cdecl(upb.DEF_MSG)) - for m in linktab:objs(upb.DEF_MSG) do - local tables = gettables(m) - -- UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, - -- refs, ref2s) - append(' UPB_MSGDEF_INIT("%s", %d, %d, %s, %s, %s, %s, %s,' .. - ' &reftables[%d], &reftables[%d]),\n', - m:full_name(), - upbtable.msgdef_selector_count(m), - upbtable.msgdef_submsg_field_count(m), - dumper:inttable(tables.int), - dumper:strtable(tables.str), - boolstr(m:_map_entry()), - const(m, "syntax"), - well_known_type(m), - reftable, reftable + 1) - reftable = reftable + 2 - end - append("};\n\n") - - append("static const upb_fielddef %s = {\n", linktab:cdecl(upb.DEF_FIELD)) - for f in linktab:objs(upb.DEF_FIELD) do - local subdef = "NULL" - if f:has_subdef() then - subdef = string.format("(const upb_def*)(%s)", linktab:addr(f:subdef())) - end - local intfmt - if f:type() == upb.TYPE_UINT32 or - f:type() == upb.TYPE_INT32 or - f:type() == upb.TYPE_UINT64 or - f:type() == upb.TYPE_INT64 then - intfmt = const(f, "intfmt") - else - intfmt = "0" - end - -- UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, - -- packed, name, num, msgdef, subdef, selector_base, - -- index, -- default_value) - append(' UPB_FIELDDEF_INIT(%s, %s, %s, %s, %s, %s, %s, "%s", %d, %s, ' .. - '%s, %d, %d, {0},' .. -- TODO: support default value - '&reftables[%d], &reftables[%d]),\n', - const(f, "label"), const(f, "type"), intfmt, - boolstr(f:istagdelim()), boolstr(f:is_extension()), - boolstr(f:lazy()), boolstr(f:packed()), f:name(), f:number(), - linktab:addr(f:containing_type()), subdef, - upbtable.fielddef_selector_base(f), f:index(), - reftable, reftable + 1 - ) - reftable = reftable + 2 - end - append("};\n\n") - - if not linktab:empty(upb.DEF_ENUM) then - append("static const upb_enumdef %s = {\n", linktab:cdecl(upb.DEF_ENUM)) - for e in linktab:objs(upb.DEF_ENUM) do - local tables = gettables(e) - -- UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval) - append(' UPB_ENUMDEF_INIT("%s", %s, %s, %d, ' .. - '&reftables[%d], &reftables[%d]),\n', - e:full_name(), - dumper:strtable(tables.str), - dumper:inttable(tables.int), - --e:default()) - 0, - reftable, reftable + 1) - reftable = reftable + 2 - end - append("};\n\n") - end - - append("static const upb_tabent %s = {\n", linktab:cdecl("strentries")) - for ent in linktab:objs("strentries") do - append(dumper:tabent(ent)) - end - append("};\n\n"); - - if not linktab:empty("intentries") then - append("static const upb_tabent %s = {\n", linktab:cdecl("intentries")) - for ent in linktab:objs("intentries") do - append(dumper:tabent(ent)) - end - append("};\n\n"); - end - - append("static const upb_tabval %s = {\n", linktab:cdecl("arrays")) - for ent in linktab:objs("arrays") do - append(dumper:arrayval(ent)) - end - append("};\n\n"); - - append("#ifdef UPB_DEBUG_REFS\n") - append("static upb_inttable reftables[%d] = {\n", reftable_count) - for i = 1,reftable_count do - append(" UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),\n") - end - append("};\n") - append("#endif\n\n") - - append("static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) {\n") - append(" upb_msgdef_ref(m, owner);\n") - append(" return m;\n") - append("}\n\n") - append("static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) {\n") - append(" upb_enumdef_ref(e, owner);\n") - append(" return e;\n") - append("}\n\n") - - append("/* Public API. */\n") - - for m in linktab:objs(upb.DEF_MSG) do - append("const upb_msgdef *upbdefs_%s_get(const void *owner)" .. - " { return refm(%s, owner); }\n", - to_cident(m:full_name()), linktab:addr(m)) - end - - append("\n") - - for e in linktab:objs(upb.DEF_ENUM) do - append("const upb_enumdef *upbdefs_%s_get(const void *owner)" .. - " { return refe(%s, owner); }\n", - to_cident(e:full_name()), linktab:addr(e)) - end - - return linktab -end - -local function dump_defs_for_type(format, defs, append) - local sorted = sorted_defs(defs) - for _, def in ipairs(sorted) do - append(format, to_cident(def:full_name()), def:full_name()) - end - - append("\n") -end - -local function make_children_map(file) - -- Maps file:package() or msg:full_name() -> children. - local map = {} - for def in file:defs(upb.DEF_ANY) do - local container = remove_name(def:full_name()) - if not map[container] then - map[container] = {} - end - table.insert(map[container], def) - end - - -- Sort all the lists for a consistent ordering. - for name, children in pairs(map) do - table.sort(children, function(a, b) return a:name() < b:name() end) - end - - return map -end - -local print_classes - -local function print_message(def, map, indent, append) - append("\n") - append("%sclass %s : public ::upb::reffed_ptr {\n", - indent, def:name()) - append("%s public:\n", indent) - append("%s %s(const ::upb::MessageDef* m, const void *ref_donor = NULL)\n", - indent, def:name()) - append("%s : reffed_ptr(m, ref_donor) {\n", indent) - append("%s UPB_ASSERT(upbdefs_%s_is(m));\n", indent, to_cident(def:full_name())) - append("%s }\n", indent) - append("\n") - append("%s static %s get() {\n", indent, def:name()) - append("%s const ::upb::MessageDef* m = upbdefs_%s_get(&m);\n", indent, to_cident(def:full_name())) - append("%s return %s(m, &m);\n", indent, def:name()) - append("%s }\n", indent) - -- TODO(haberman): add fields - print_classes(def:full_name(), map, indent .. " ", append) - append("%s};\n", indent) -end - -local function print_enum(def, indent, append) - append("\n") - append("%sclass %s : public ::upb::reffed_ptr {\n", - indent, def:name()) - append("%s public:\n", indent) - append("%s %s(const ::upb::EnumDef* e, const void *ref_donor = NULL)\n", - indent, def:name()) - append("%s : reffed_ptr(e, ref_donor) {\n", indent) - append("%s UPB_ASSERT(upbdefs_%s_is(e));\n", indent, to_cident(def:full_name())) - append("%s }\n", indent) - append("%s static %s get() {\n", indent, def:name()) - append("%s const ::upb::EnumDef* e = upbdefs_%s_get(&e);\n", indent, to_cident(def:full_name())) - append("%s return %s(e, &e);\n", indent, def:name()) - append("%s }\n", indent) - append("%s};\n", indent) -end - -function print_classes(name, map, indent, append) - if not map[name] then - return - end - - for _, def in ipairs(map[name]) do - if def:def_type() == upb.DEF_MSG then - print_message(def, map, indent, append) - elseif def:def_type() == upb.DEF_ENUM then - print_enum(def, indent, append) - else - error("Unknown def type for " .. def:full_name()) - end - end -end - -local function dump_defs_h(file, append, linktab) - local basename_preproc = to_preproc(file:name()) - append("/* This file contains accessors for a set of compiled-in defs.\n") - append(" * Note that unlike Google's protobuf, it does *not* define\n") - append(" * generated classes or any other kind of data structure for\n") - append(" * actually storing protobufs. It only contains *defs* which\n") - append(" * let you reflect over a protobuf *schema*.\n") - append(" */\n") - emit_file_warning(file, append) - append('#ifndef %s_UPB_H_\n', basename_preproc) - append('#define %s_UPB_H_\n\n', basename_preproc) - append('#include "upb/def.h"\n\n') - append('UPB_BEGIN_EXTERN_C\n\n') - - -- Dump C enums for proto enums. - - append("/* MessageDefs: call these functions to get a ref to a msgdef. */\n") - dump_defs_for_type( - "const upb_msgdef *upbdefs_%s_get(const void *owner);\n", - file:defs(upb.DEF_MSG), append) - - append("/* EnumDefs: call these functions to get a ref to an enumdef. */\n") - dump_defs_for_type( - "const upb_enumdef *upbdefs_%s_get(const void *owner);\n", - file:defs(upb.DEF_ENUM), append) - - append("/* Functions to test whether this message is of a certain type. */\n") - dump_defs_for_type( - "UPB_INLINE bool upbdefs_%s_is(const upb_msgdef *m) {\n" .. - " return strcmp(upb_msgdef_fullname(m), \"%s\") == 0;\n}\n", - file:defs(upb.DEF_MSG), append) - - append("/* Functions to test whether this enum is of a certain type. */\n") - dump_defs_for_type( - "UPB_INLINE bool upbdefs_%s_is(const upb_enumdef *e) {\n" .. - " return strcmp(upb_enumdef_fullname(e), \"%s\") == 0;\n}\n", - file:defs(upb.DEF_ENUM), append) - - append("\n") - - -- fields - local fields = {} - - for f in linktab:objs(upb.DEF_FIELD) do - local symname = f:containing_type():full_name() .. "." .. f:name() - fields[#fields + 1] = {to_cident(symname), f} - end - - table.sort(fields, function(a, b) return a[1] < b[1] end) - - append("/* Functions to get a fielddef from a msgdef reference. */\n") - for _, field in ipairs(fields) do - local f = field[2] - local msg_cident = to_cident(f:containing_type():full_name()) - local field_cident = to_cident(f:name()) - append("UPB_INLINE const upb_fielddef *upbdefs_%s_f_%s(const upb_msgdef *m) {" .. - " UPB_ASSERT(upbdefs_%s_is(m));" .. - " return upb_msgdef_itof(m, %d); }\n", - msg_cident, field_cident, msg_cident, f:number()) - end - - append('\nUPB_END_EXTERN_C\n\n') - - -- C++ wrappers. - local children_map = make_children_map(file) - - append("#ifdef __cplusplus\n\n") - append("namespace upbdefs {\n") - start_namespace(file:package(), append) - print_classes(file:package(), children_map, "", append) - append("\n") - end_namespace(file:package(), append) - append("} /* namespace upbdefs */\n\n") - append("#endif /* __cplusplus */\n") - - append("\n") - append('#endif /* %s_UPB_H_ */\n', basename_preproc) -end - -function export.dump_defs(filedef, append_h, append_c) - local linktab = dump_defs_c(filedef, append_c) - dump_defs_h(filedef, append_h, linktab) -end - -return export diff --git a/tools/make_cmakelists.py b/tools/make_cmakelists.py index b8f46b9..2b426b5 100755 --- a/tools/make_cmakelists.py +++ b/tools/make_cmakelists.py @@ -120,6 +120,9 @@ class BuildFileFunctions(object): def upb_proto_library(self, **kwargs): pass + def upb_proto_reflection_library(self, **kwargs): + pass + def genrule(self, **kwargs): pass diff --git a/tools/upbc.lua b/tools/upbc.lua deleted file mode 100644 index 80d2886..0000000 --- a/tools/upbc.lua +++ /dev/null @@ -1,91 +0,0 @@ ---[[ - - The upb compiler. It can write two different kinds of output - files: - - - generated code for a C API (foo.upb.h, foo.upb.c) - - (obsolete): definitions of upb defs. (foo.upbdefs.h, foo.upbdefs.c) - ---]] - -local dump_cinit = require "dump_cinit" -local upb = require "upb" - -local generate_upbdefs = false -local outdir = "." - -i = 1 -while i <= #arg do - argument = arg[i] - if argument.sub(argument, 1, 2) == "--" then - if argument == "--generate-upbdefs" then - generate_upbdefs = true - elseif argument == "--outdir" then - i = i + 1 - outdir = arg[i] - else - print("Unknown flag: " .. argument) - return 1 - end - else - if src then - print("upbc can only handle one input file at a time.") - return 1 - end - src = argument - end - i = i + 1 -end - -if not src then - print("Usage: upbc [--generate-upbdefs] ") - return 1 -end - -function strip_proto(filename) - return string.gsub(filename, '%.proto$','') -end - -local function open(filename) - local full_name = outdir .. "/" .. filename - return assert(io.open(full_name, "w"), "couldn't open " .. full_name) -end - --- Open input/output files. -local f = assert(io.open(src, "r"), "couldn't open input file " .. src) -local descriptor = f:read("*all") -local files = upb.load_descriptor(descriptor) -local symtab = upb.SymbolTable() - -for _, file in ipairs(files) do - symtab:add_file(file) - local outbase = strip_proto(file:name()) - - -- Write upbdefs. - - local hfilename = outbase .. ".upbdefs.h" - local cfilename = outbase .. ".upbdefs.c" - - if os.getenv("UPBC_VERBOSE") then - print("upbc:") - print(string.format(" source file=%s", src)) - print(string.format(" output file base=%s", outbase)) - print(string.format(" hfilename=%s", hfilename)) - print(string.format(" cfilename=%s", cfilename)) - end - - os.execute(string.format("mkdir -p `dirname %s`", outbase)) - - assert(generate_upbdefs) - -- Legacy generated defs. - local hfile = open(hfilename) - local cfile = open(cfilename) - - local happend = dump_cinit.file_appender(hfile) - local cappend = dump_cinit.file_appender(cfile) - - dump_cinit.dump_defs(file, happend, cappend) - - hfile:close() - cfile:close() -end diff --git a/upb/def.c b/upb/def.c index ba6de50..047684e 100644 --- a/upb/def.c +++ b/upb/def.c @@ -643,7 +643,7 @@ uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) { const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { upb_value val; return upb_inttable_lookup32(&m->itof, i, &val) ? - upb_value_getptr(val) : NULL; + upb_value_getconstptr(val) : NULL; } const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, @@ -1128,12 +1128,14 @@ static bool create_fielddef( if (m) { /* direct message field. */ + upb_value v, packed_v; + f = (upb_fielddef*)&m->fields[m->field_count++]; f->msgdef = m; f->is_extension_ = false; - upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); - upb_value v = upb_value_constptr(f); + packed_v = pack_def(f, UPB_DEFTYPE_FIELD); + v = upb_value_constptr(f); if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname); @@ -1580,5 +1582,48 @@ bool upb_symtab_addfile(upb_symtab *s, return ok; } +/* Include here since we want most of this file to be stdio-free. */ +#include + +bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { + /* Since this function should never fail (it would indicate a bug in upb) we + * print errors to stderr instead of returning error status to the user. */ + upb_def_init **deps = init->deps; + google_protobuf_FileDescriptorProto *file; + upb_arena arena; + upb_status status = UPB_STATUS_INIT; + + if (upb_strtable_lookup(&s->files, init->filename, NULL)) { + return true; + } + + for (; *deps; deps++) { + if (!_upb_symtab_loaddefinit(s, *deps)) goto err; + } + + upb_arena_init(&arena); + file = google_protobuf_FileDescriptorProto_parsenew(init->descriptor, &arena); + + if (!file) { + upb_status_seterrf( + &status, + "Failed to parse compiled-in descriptor for file '%s'. This should " + "never happen.", + init->filename); + goto err; + } + + if (!upb_symtab_addfile(s, file, &status)) goto err; + + upb_arena_uninit(&arena); + return true; + +err: + fprintf(stderr, "Error loading compiled-in descriptor: %s\n", + upb_status_errmsg(&status)); + upb_arena_uninit(&arena); + return false; +} + #undef CHK #undef CHK_OOM diff --git a/upb/def.h b/upb/def.h index 4fe6d04..e6fdf21 100644 --- a/upb/def.h +++ b/upb/def.h @@ -659,7 +659,6 @@ const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); - UPB_END_EXTERN_C #ifdef __cplusplus @@ -707,6 +706,15 @@ bool upb_symtab_addfile(upb_symtab *s, const google_protobuf_FileDescriptorProto* file, upb_status *status); +/* For generated code only: loads a generated descriptor. */ +typedef struct upb_def_init { + struct upb_def_init **deps; + const char *filename; + upb_stringview descriptor; +} upb_def_init; + +bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init); + UPB_END_EXTERN_C #ifdef __cplusplus diff --git a/upb/upb.h b/upb/upb.h index 020022b..2fb7a88 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -463,7 +463,6 @@ struct upb_alloc { UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) { UPB_ASSERT(alloc); - UPB_ASSERT(size < 65535); return alloc->func(alloc, NULL, 0, size); } diff --git a/upbc/generator.cc b/upbc/generator.cc index 68996a9..3b4f6ac 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -8,6 +8,7 @@ #include "absl/strings/substitute.h" #include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/io/zero_copy_stream.h" #include "upbc/generator.h" @@ -32,6 +33,14 @@ static std::string SourceFilename(std::string proto_filename) { return StripExtension(proto_filename) + ".upb.c"; } +static std::string DefHeaderFilename(std::string proto_filename) { + return StripExtension(proto_filename) + ".upbdefs.h"; +} + +static std::string DefSourceFilename(std::string proto_filename) { + return StripExtension(proto_filename) + ".upbdefs.c"; +} + class Output { public: Output(protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {} @@ -165,6 +174,10 @@ std::string ToCIdent(absl::string_view str) { return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}}); } +std::string DefInitSymbol(const protobuf::FileDescriptor *file) { + return ToCIdent(file->name()) + "_upbdefinit"; +} + std::string ToPreproc(absl::string_view str) { return absl::AsciiStrToUpper(ToCIdent(str)); } @@ -558,6 +571,91 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) { output("\n"); } +void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) { + output("UPB_INLINE const upb_msgdef *$0_getmsgdef(upb_symtab *s) {\n", + ToCIdent(d->full_name())); + output(" _upb_symtab_loaddefinit(s, &$0);\n", DefInitSymbol(d->file())); + output(" return upb_symtab_lookupmsg(s, \"$0\");\n", d->full_name()); + output("}\n"); + output("\n"); + + for (int i = 0; i < d->nested_type_count(); i++) { + GenerateMessageDefAccessor(d->nested_type(i), output); + } +} + +void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) { + EmitFileWarning(file, output); + + output("extern upb_def_init $0;\n", DefInitSymbol(file)); + + for (int i = 0; i < file->message_type_count(); i++) { + GenerateMessageDefAccessor(file->message_type(i), output); + } +} + +// Escape C++ trigraphs by escaping question marks to \? +std::string EscapeTrigraphs(absl::string_view to_escape) { + return absl::StrReplaceAll(to_escape, {{"?", "\\?"}}); +} + +void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { + EmitFileWarning(file, output); + + output("#include \"upb/def.h\"\n"); + output("\n"); + + for (int i = 0; i < file->dependency_count(); i++) { + output("extern upb_def_init $0;\n", DefInitSymbol(file->dependency(i))); + } + + protobuf::FileDescriptorProto file_proto; + file->CopyTo(&file_proto); + std::string file_data; + file_proto.SerializeToString(&file_data); + + output("static const char descriptor[$0] =\n", file_data.size()); + + { + if (file_data.size() > 65535) { + // Workaround for MSVC: "Error C1091: compiler limit: string exceeds + // 65535 bytes in length". Declare a static array of chars rather than + // use a string literal. Only write 25 bytes per line. + static const int kBytesPerLine = 25; + output("{ "); + for (int i = 0; i < file_data.size();) { + for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) { + output("'$0', ", absl::CEscape(file_data.substr(i, 1))); + } + output("\n"); + } + output("'\\0' }"); // null-terminate + } else { + // Only write 40 bytes per line. + static const int kBytesPerLine = 40; + for (int i = 0; i < file_data.size(); i += kBytesPerLine) { + output( + "\"$0\"\n", + EscapeTrigraphs(absl::CEscape(file_data.substr(i, kBytesPerLine)))); + } + } + output(";\n"); + } + + output("static upb_def_init *deps[$0] = {\n", file->dependency_count() + 1); + for (int i = 0; i < file->dependency_count(); i++) { + output(" $0,\n", DefInitSymbol(file->dependency(i))); + } + output(" NULL\n"); + output("};\n"); + + output("upb_def_init $0 = {\n", DefInitSymbol(file)); + output(" deps,\n"); + output(" \"$0\",\n", file->name()); + output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size()); + output("};\n"); +} + bool Generator::Generate(const protobuf::FileDescriptor* file, const std::string& parameter, protoc::GeneratorContext* context, @@ -568,6 +666,12 @@ bool Generator::Generate(const protobuf::FileDescriptor* file, Output c_output(context->Open(SourceFilename(file->name()))); WriteSource(file, c_output); + Output h_def_output(context->Open(DefHeaderFilename(file->name()))); + WriteDefHeader(file, h_def_output); + + Output c_def_output(context->Open(DefSourceFilename(file->name()))); + WriteDefSource(file, c_def_output); + return true; } -- cgit v1.2.3 From 48863ea0be94ea3d3d61206ad7ce9ead206770fa Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 12 Jan 2019 19:12:57 -0800 Subject: A lot more tests are working now. --- BUILD | 10 --- tests/json/test_json.cc | 36 +++++----- tests/pb/test_decoder.cc | 157 ++++++++++++++++++++----------------------- tests/test_handlers.c | 42 ------------ tests/test_util.h | 10 +-- upb/bindings/stdc++/string.h | 3 +- upb/handlers-inl.h | 2 +- upb/handlers.c | 11 ++- upb/handlers.h | 2 + upb/json/parser.c | 144 ++++++++++++++++++++------------------- upb/json/parser.h | 140 ++++++++++++++++++++------------------ upb/json/parser.rl | 38 ++++++----- upb/json/printer.c | 32 +++++---- upb/json/printer.h | 68 ++++++++----------- upb/pb/decoder.c | 11 ++- upb/pb/decoder.h | 11 +-- upb/pb/encoder.c | 14 ++-- upb/pb/encoder.h | 12 ++-- upb/sink.c | 8 +-- upb/sink.h | 28 ++++++-- 20 files changed, 375 insertions(+), 404 deletions(-) delete mode 100644 tests/test_handlers.c (limited to 'tests/pb/test_decoder.cc') diff --git a/BUILD b/BUILD index ffb5bbd..c6c401c 100644 --- a/BUILD +++ b/BUILD @@ -166,16 +166,6 @@ upb_proto_reflection_library( deps = ["descriptor_proto"], ) -cc_test( - name = "test_handlers", - srcs = ["tests/test_handlers.c"], - deps = [ - ":descriptor_upbproto", - ":upb_pb", - ":upb_test", - ], -) - proto_library( name = "test_decoder_proto", srcs = [ diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index b9b50cd..b0fd3e3 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -144,7 +144,7 @@ class StringSink { } ~StringSink() { } - upb_bytessink* Sink() { return &bytessink_; } + upb_bytessink Sink() { return bytessink_; } const std::string& Data() { return s_; } @@ -169,16 +169,15 @@ class StringSink { void test_json_roundtrip_message(const char* json_src, const char* json_expected, const upb::Handlers* serialize_handlers, - const upb::json::ParserMethod* parser_method, + const upb::json::ParserMethodPtr parser_method, int seam) { VerboseParserEnvironment env(verbose); StringSink data_sink; - upb::json::Printer* printer = upb::json::Printer::Create( + upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create( env.env(), serialize_handlers, data_sink.Sink()); - upb::json::Parser* parser = - upb::json::Parser::Create( - env.env(), parser_method, NULL, printer->input(), false); - env.ResetBytesSink(parser->input()); + upb::json::ParserPtr parser = upb::json::ParserPtr::Create( + env.env(), parser_method, NULL, printer.input(), false); + env.ResetBytesSink(parser.input()); env.Reset(json_src, strlen(json_src), false, false); bool ok = env.Start() && @@ -203,16 +202,16 @@ void test_json_roundtrip_message(const char* json_src, // Starts with a message in JSON format, parses and directly serializes again, // and compares the result. void test_json_roundtrip() { - upb::SymbolTable* symtab = upb::SymbolTable::New(); - upb::HandlerCache* serialize_handlercache = upb::json::Printer::NewCache(false); - upb::json::CodeCache* parse_codecache = upb::json::CodeCache::New(); + upb::SymbolTable symtab; + upb::HandlerCache serialize_handlercache( + upb::json::PrinterPtr::NewCache(false)); + upb::json::CodeCache parse_codecache; - const upb::MessageDef* md = upb_test_json_TestMessage_getmsgdef(symtab); + upb::MessageDefPtr md(upb_test_json_TestMessage_getmsgdef(symtab.ptr())); ASSERT(md); - const upb::Handlers* serialize_handlers = serialize_handlercache->Get(md); - const upb::json::ParserMethod* parser_method = parse_codecache->Get(md); + const upb::Handlers* serialize_handlers = serialize_handlercache.Get(md); + const upb::json::ParserMethodPtr parser_method = parse_codecache.Get(md); ASSERT(serialize_handlers); - ASSERT(parser_method); for (const TestCase* test_case = kTestRoundtripMessages; test_case->input != NULL; test_case++) { @@ -227,9 +226,8 @@ void test_json_roundtrip() { } } - upb::HandlerCache::Free(serialize_handlercache); - serialize_handlercache = upb::json::Printer::NewCache(true); - serialize_handlers = serialize_handlercache->Get(md); + serialize_handlercache = upb::json::PrinterPtr::NewCache(true); + serialize_handlers = serialize_handlercache.Get(md); for (const TestCase* test_case = kTestRoundtripMessagesPreserve; test_case->input != NULL; test_case++) { @@ -243,10 +241,6 @@ void test_json_roundtrip() { serialize_handlers, parser_method, i); } } - - upb::HandlerCache::Free(serialize_handlercache); - upb::json::CodeCache::Free(parse_codecache); - upb::SymbolTable::Free(symtab); } extern "C" { diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index d0e3fa3..ec7a788 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -279,7 +279,7 @@ int* startstr(int* depth, const uint32_t* num, size_t size_hint) { } size_t value_string(int* depth, const uint32_t* num, const char* buf, - size_t n, const upb::BufferHandle* handle) { + size_t n, const upb_bufhandle* handle) { UPB_UNUSED(num); UPB_UNUSED(depth); check_stack_alignment(); @@ -348,13 +348,13 @@ void free_uint32(void *val) { } template -void doreg(upb_handlers *h, uint32_t num) { - const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); +void doreg(upb::HandlersPtr h, uint32_t num) { + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num); ASSERT(f); - ASSERT(h->SetValueHandler(f, UpbBindT(F, new uint32_t(num)))); - if (f->IsSequence()) { - ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); - ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); + ASSERT(h.SetValueHandler(f, UpbBind(F, new uint32_t(num)))); + if (f.IsSequence()) { + ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); + ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); } } @@ -368,7 +368,7 @@ uint32_t rep_fn(uint32_t fn) { #define UNKNOWN_FIELD 666 template -void reg(upb_handlers *h, upb_descriptortype_t type) { +void reg(upb::HandlersPtr h, upb_descriptortype_t type) { // We register both a repeated and a non-repeated field for every type. // For the non-repeated field we make the field number the same as the // type. For the repeated field we make it a function of the type. @@ -376,39 +376,40 @@ void reg(upb_handlers *h, upb_descriptortype_t type) { doreg(h, rep_fn(type)); } -void regseq(upb::Handlers* h, const upb::FieldDef* f, uint32_t num) { - ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); - ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); +void regseq(upb::HandlersPtr h, upb::FieldDefPtr f, uint32_t num) { + ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); + ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); } -void reg_subm(upb_handlers *h, uint32_t num) { - const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); +void reg_subm(upb::HandlersPtr h, uint32_t num) { + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num); ASSERT(f); - if (f->IsSequence()) regseq(h, f, num); + if (f.IsSequence()) regseq(h, f, num); ASSERT( - h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); - ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); + h.SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); + ASSERT(h.SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); } -void reg_str(upb_handlers *h, uint32_t num) { - const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); +void reg_str(upb::HandlersPtr h, uint32_t num) { + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num); ASSERT(f); - if (f->IsSequence()) regseq(h, f, num); - ASSERT(h->SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num)))); - ASSERT(h->SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num)))); - ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); + if (f.IsSequence()) regseq(h, f, num); + ASSERT(h.SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num)))); + ASSERT(h.SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num)))); + ASSERT(h.SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } struct HandlerRegisterData { TestMode mode; }; -void callback(const void *closure, upb_handlers *h) { +void callback(const void *closure, upb::Handlers* h_ptr) { + upb::HandlersPtr h(h_ptr); const HandlerRegisterData* data = static_cast(closure); if (data->mode == ALL_HANDLERS) { - h->SetStartMessageHandler(UpbMakeHandler(startmsg)); - h->SetEndMessageHandler(UpbMakeHandler(endmsg)); + h.SetStartMessageHandler(UpbMakeHandler(startmsg)); + h.SetEndMessageHandler(UpbMakeHandler(endmsg)); // Register handlers for each type. reg(h, UPB_DESCRIPTOR_TYPE_DOUBLE); @@ -436,7 +437,7 @@ void callback(const void *closure, upb_handlers *h) { reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); - if (h->message_def()->full_name() == std::string("DecoderTest")) { + if (h.message_def().full_name() == std::string("DecoderTest")) { reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP); reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); } @@ -446,25 +447,16 @@ void callback(const void *closure, upb_handlers *h) { } } -upb::reffed_ptr NewHandlers(upb::SymbolTable* symtab, - TestMode mode) { - HandlerRegisterData handlerdata; - handlerdata.mode = mode; - return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback, - &handlerdata); -} - /* Running of test cases ******************************************************/ const upb::Handlers *global_handlers; -const upb::pb::DecoderMethod *global_method; - -upb::pb::Decoder* CreateDecoder(upb::Environment* env, - const upb::pb::DecoderMethod* method, - upb::Sink* sink) { - upb::pb::Decoder *ret = upb::pb::Decoder::Create(env, method, sink); - ASSERT(ret != NULL); - ret->set_max_nesting(MAX_NESTING); +upb::pb::DecoderMethodPtr global_method; + +upb::pb::DecoderPtr CreateDecoder(upb::Environment* env, + upb::pb::DecoderMethodPtr method, + upb::Sink sink) { + upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(env, method, sink); + ret.set_max_nesting(MAX_NESTING); return ret; } @@ -479,7 +471,7 @@ uint32_t Hash(const string& proto, const string* expected_output, size_t seam1, return hash; } -void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) { +void CheckBytesParsed(upb::pb::DecoderPtr decoder, size_t ofs) { // We can't have parsed more data than the decoder callback is telling us it // parsed. ASSERT(decoder.BytesParsed() <= ofs); @@ -491,7 +483,7 @@ void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) { } static bool parse(VerboseParserEnvironment* env, - const upb::pb::Decoder& decoder, int bytes) { + upb::pb::DecoderPtr decoder, int bytes) { CheckBytesParsed(decoder, env->ofs()); bool ret = env->ParseBuffer(bytes); if (ret) { @@ -501,11 +493,11 @@ static bool parse(VerboseParserEnvironment* env, return ret; } -void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, +void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, const string& proto, const string* expected_output, size_t i, size_t j, bool may_skip) { env->Reset(proto.c_str(), proto.size(), may_skip, expected_output == NULL); - decoder->Reset(); + decoder.Reset(); testhash = Hash(proto, expected_output, i, j, may_skip); if (filter_hash && testhash != filter_hash) return; @@ -515,7 +507,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, if (filter_hash) { fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash); fprintf(stderr, "JIT on: %s\n", - global_method->is_native() ? "true" : "false"); + global_method.is_native() ? "true" : "false"); fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size()); PrintBinary(proto); fprintf(stderr, "\n"); @@ -534,9 +526,9 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, } bool ok = env->Start() && - parse(env, *decoder, i) && - parse(env, *decoder, j - i) && - parse(env, *decoder, -1) && + parse(env, decoder, i) && + parse(env, decoder, j - i) && + parse(env, decoder, -1) && env->End(); ASSERT(env->CheckConsistency()); @@ -564,8 +556,8 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, void run_decoder(const string& proto, const string* expected_output) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::Decoder *decoder = CreateDecoder(env.env(), global_method, &sink); - env.ResetBytesSink(decoder->input()); + upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + env.ResetBytesSink(decoder.input()); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { do_run_decoder(&env, decoder, proto, expected_output, i, j, true); @@ -883,9 +875,9 @@ void test_valid() { upb::Environment env; env.ReportErrorsTo(&status); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::Decoder* decoder = CreateDecoder(&env, global_method, &sink); + upb::pb::DecoderPtr decoder = CreateDecoder(&env, global_method, sink); output.clear(); - bool ok = upb::BufferSource::PutBuffer("", 0, decoder->input()); + bool ok = upb::PutBuffer(std::string(), decoder.input()); ASSERT(ok); ASSERT(status.ok()); if (test_mode == ALL_HANDLERS) { @@ -1133,23 +1125,22 @@ void test_valid() { run_decoder(buf, &textbuf); } -upb::reffed_ptr NewMethod( - const upb::Handlers* dest_handlers, bool allow_jit) { - upb::pb::CodeCache cache; - cache.set_allow_jit(allow_jit); - return cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers)); -} +void empty_callback(const void *closure, upb::Handlers* h_ptr) {} void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { // Create an empty handlers to make sure that the decoder can handle empty // messages. - const upb::MessageDef* md = Empty_getmsgdef(symtab); - upb::reffed_ptr h(upb::Handlers::New(md)); - bool ok = h->Freeze(NULL); - ASSERT(ok); -upb::reffed_ptr method = - NewMethod(h.get(), allowjit); - ASSERT(method.get()); + HandlerRegisterData handlerdata; + handlerdata.mode = test_mode; + + upb::HandlerCache handler_cache(empty_callback, &handlerdata); + upb::pb::CodeCache pb_code_cache(&handler_cache); + + pb_code_cache.set_allow_jit(allowjit); + + upb::MessageDefPtr md = upb::MessageDefPtr(Empty_getmsgdef(symtab->ptr())); + global_handlers = handler_cache.Get(md); + global_method = pb_code_cache.Get(md); // TODO: also test the case where a message has fields, but the fields are // submessage fields and have no handlers. This also results in a decoder @@ -1169,9 +1160,9 @@ upb::reffed_ptr method = }; for (int i = 0; testdata[i].data; i++) { VerboseParserEnvironment env(filter_hash != 0); - upb::Sink sink(method->dest_handlers(), &closures[0]); - upb::pb::Decoder* decoder = CreateDecoder(env.env(), method.get(), &sink); - env.ResetBytesSink(decoder->input()); + upb::Sink sink(global_method.dest_handlers(), &closures[0]); + upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + env.ResetBytesSink(decoder.input()); env.Reset(testdata[i].data, testdata[i].length, true, false); ASSERT(env.Start()); ASSERT(env.ParseBuffer(-1)); @@ -1181,24 +1172,25 @@ upb::reffed_ptr method = } void run_tests(bool use_jit) { - upb::reffed_ptr method; - upb::reffed_ptr handlers; - upb::SymbolTable* symtab = upb::SymbolTable::New(); + HandlerRegisterData handlerdata; + handlerdata.mode = test_mode; - handlers = NewHandlers(symtab, test_mode); - global_handlers = handlers.get(); + upb::SymbolTable symtab; + upb::HandlerCache handler_cache(callback, &handlerdata); + upb::pb::CodeCache pb_code_cache(&handler_cache); - method = NewMethod(handlers.get(), use_jit); - global_method = method.get(); - ASSERT(use_jit == global_method->is_native()); + pb_code_cache.set_allow_jit(use_jit); + + upb::MessageDefPtr md(DecoderTest_getmsgdef(symtab.ptr())); + global_handlers = handler_cache.Get(md); + global_method = pb_code_cache.Get(md); + ASSERT(use_jit == global_method.is_native()); completed = 0; test_invalid(); test_valid(); - test_emptyhandlers(symtab, use_jit); - - upb::SymbolTable::Free(symtab); + test_emptyhandlers(&symtab, use_jit); } void run_test_suite() { @@ -1218,9 +1210,6 @@ int run_tests(int argc, char *argv[]) { closures[i] = i; } - upb::reffed_ptr method; - upb::reffed_ptr handlers; - // Count tests. count = &total; total = 0; diff --git a/tests/test_handlers.c b/tests/test_handlers.c deleted file mode 100644 index 2b19cab..0000000 --- a/tests/test_handlers.c +++ /dev/null @@ -1,42 +0,0 @@ - -#include -#include -#include "google/protobuf/descriptor.upbdefs.h" -#include "upb/handlers.h" -#include "upb_test.h" - -static bool startmsg(void *c, const void *hd) { - UPB_UNUSED(c); - UPB_UNUSED(hd); - return true; -} - -static void test_error() { - /* Test creating handlers of a static msgdef. */ - upb_symtab *s = upb_symtab_new(); - const upb_msgdef *m = google_protobuf_DescriptorProto_getmsgdef(s); - upb_handlers *h = upb_handlers_new(m, &h); - - /* Attempt to set the same handler twice causes error. */ - ASSERT(upb_ok(upb_handlers_status(h))); - upb_handlers_setstartmsg(h, &startmsg, NULL); - ASSERT(upb_ok(upb_handlers_status(h))); - upb_handlers_setstartmsg(h, &startmsg, NULL); - ASSERT(!upb_ok(upb_handlers_status(h))); - ASSERT(!upb_handlers_freeze(&h, 1, NULL)); - - /* Clearing the error will let us proceed. */ - upb_handlers_clearerr(h); - ASSERT(upb_handlers_freeze(&h, 1, NULL)); - ASSERT(upb_handlers_isfrozen(h)); - - upb_handlers_unref(h, &h); - upb_symtab_free(s); -} - -int run_tests(int argc, char *argv[]) { - UPB_UNUSED(argc); - UPB_UNUSED(argv); - test_error(); - return 0; -} diff --git a/tests/test_util.h b/tests/test_util.h index 1b1ff01..0b5ddd4 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -78,14 +78,14 @@ class VerboseParserEnvironment { if (verbose_) { fprintf(stderr, "Calling start()\n"); } - return sink_->Start(len_, &subc_); + return sink_.Start(len_, &subc_); } bool End() { if (verbose_) { fprintf(stderr, "Calling end()\n"); } - end_ok_ = sink_->End(); + end_ok_ = sink_.End(); end_ok_set_ = true; return end_ok_; @@ -137,7 +137,7 @@ class VerboseParserEnvironment { (unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes)); } - int parsed = sink_->PutBuffer(subc_, buf2, bytes, &global_handle); + int parsed = sink_.PutBuffer(subc_, buf2, bytes, &global_handle); free(buf2); if (verbose_) { @@ -170,7 +170,7 @@ class VerboseParserEnvironment { return true; } - void ResetBytesSink(upb::BytesSink* sink) { + void ResetBytesSink(upb::BytesSink sink) { sink_ = sink; } @@ -181,7 +181,7 @@ class VerboseParserEnvironment { private: upb::Environment env_; - upb::BytesSink* sink_; + upb::BytesSink sink_; const char* buf_; size_t len_; bool verbose_; diff --git a/upb/bindings/stdc++/string.h b/upb/bindings/stdc++/string.h index 4d7a719..55b44cf 100644 --- a/upb/bindings/stdc++/string.h +++ b/upb/bindings/stdc++/string.h @@ -48,11 +48,12 @@ class StringSink { explicit StringSink(T* target) { // TODO(haberman): we need to avoid rebuilding a new handler every time, // but with class globals disallowed for google3 C++ this is tricky. + upb_byteshandler_init(&handler_); FillStringHandler::SetHandler(&handler_); input_.Reset(&handler_, target); } - BytesSink* input() { return &input_; } + BytesSink input() { return input_; } private: upb_byteshandler handler_; diff --git a/upb/handlers-inl.h b/upb/handlers-inl.h index b038e30..5677a4a 100644 --- a/upb/handlers-inl.h +++ b/upb/handlers-inl.h @@ -858,7 +858,7 @@ inline Handler::Handler(F func) : registered_(false), cleanup_data_(func.GetData()), cleanup_func_(func.GetCleanup()) { - upb_handlerattr_sethandlerdata(&attr_, func.GetData()); + attr_.handler_data = func.GetData(); typedef typename ReturnOf::Return Return; typedef typename ConvertParams::Func ConvertedParamsFunc; typedef typename MaybeWrapReturn::Func diff --git a/upb/handlers.c b/upb/handlers.c index fd81b03..ba27b98 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -286,6 +286,10 @@ const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; } +bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { + return upb_handlercache_addcleanup(h->cache, p, func); +} + upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: @@ -470,12 +474,13 @@ void upb_handlercache_free(upb_handlercache *cache) { upb_gfree(cache); } -bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { +bool upb_handlercache_addcleanup(upb_handlercache *c, void *p, + upb_handlerfree *func) { bool ok; - if (upb_inttable_lookupptr(&h->cache->cleanup_, p, NULL)) { + if (upb_inttable_lookupptr(&c->cleanup_, p, NULL)) { return false; } - ok = upb_inttable_insertptr(&h->cache->cleanup_, p, upb_value_fptr(func)); + ok = upb_inttable_insertptr(&c->cleanup_, p, upb_value_fptr(func)); UPB_ASSERT(ok); return true; } diff --git a/upb/handlers.h b/upb/handlers.h index 4558786..44cad18 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -602,6 +602,8 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, void upb_handlercache_free(upb_handlercache *cache); const upb_handlers *upb_handlercache_get(upb_handlercache *cache, const upb_msgdef *md); +bool upb_handlercache_addcleanup(upb_handlercache *h, void *p, + upb_handlerfree *hfree); UPB_END_EXTERN_C diff --git a/upb/json/parser.c b/upb/json/parser.c index 4bc9163..1dac800 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -301,13 +301,13 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, - &frame->sink, p->ignore_json_unknown); + frame->sink, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -383,9 +383,12 @@ static bool check_stack(upb_json_parser *p) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { upb_value v; const upb_json_codecache *cache = p->method->cache; - bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); - const upb_json_parsermethod *method = upb_value_getptr(v); + bool ok; + const upb_json_parsermethod *method; + + ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); UPB_ASSERT(ok); + method = upb_value_getconstptr(v); frame->name_table = &method->name_table; } @@ -1287,7 +1290,7 @@ static bool end_any_stringval(upb_json_parser *p) { } json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); - + return true; } else { upb_status_seterrf( @@ -2416,11 +2419,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2578 "upb/json/parser.rl" +#line 2581 "upb/json/parser.rl" -#line 2424 "upb/json/parser.c" +#line 2427 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2667,7 +2670,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2581 "upb/json/parser.rl" +#line 2584 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2690,7 +2693,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2694 "upb/json/parser.c" +#line 2697 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2765,83 +2768,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2429 "upb/json/parser.rl" +#line 2432 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2431 "upb/json/parser.rl" +#line 2434 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2435 "upb/json/parser.rl" +#line 2438 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2436 "upb/json/parser.rl" +#line 2439 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2442 "upb/json/parser.rl" +#line 2445 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2443 "upb/json/parser.rl" +#line 2446 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2444 "upb/json/parser.rl" +#line 2447 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2450 "upb/json/parser.rl" +#line 2453 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2456 "upb/json/parser.rl" +#line 2459 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2468 "upb/json/parser.rl" +#line 2471 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2469 "upb/json/parser.rl" +#line 2472 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2471 "upb/json/parser.rl" +#line 2474 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2476 "upb/json/parser.rl" +#line 2479 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2477 "upb/json/parser.rl" +#line 2480 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2479 "upb/json/parser.rl" +#line 2482 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2480 "upb/json/parser.rl" +#line 2483 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2482 "upb/json/parser.rl" +#line 2485 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2483 "upb/json/parser.rl" +#line 2486 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2485 "upb/json/parser.rl" +#line 2488 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2490 "upb/json/parser.rl" +#line 2493 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2853,11 +2856,11 @@ _match: } break; case 21: -#line 2501 "upb/json/parser.rl" +#line 2504 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2506 "upb/json/parser.rl" +#line 2509 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2867,11 +2870,11 @@ _match: } break; case 23: -#line 2513 "upb/json/parser.rl" +#line 2516 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2516 "upb/json/parser.rl" +#line 2519 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2881,7 +2884,7 @@ _match: } break; case 25: -#line 2527 "upb/json/parser.rl" +#line 2530 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2891,7 +2894,7 @@ _match: } break; case 26: -#line 2536 "upb/json/parser.rl" +#line 2539 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2901,54 +2904,54 @@ _match: } break; case 27: -#line 2548 "upb/json/parser.rl" +#line 2551 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2552 "upb/json/parser.rl" +#line 2555 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2557 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2558 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2560 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2561 "upb/json/parser.rl" +#line 2564 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2563 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2565 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2567 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2569 "upb/json/parser.rl" +#line 2572 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2570 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2575 "upb/json/parser.rl" +#line 2578 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2952 "upb/json/parser.c" +#line 2955 "upb/json/parser.c" } } @@ -2965,32 +2968,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2427 "upb/json/parser.rl" +#line 2430 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2558 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2563 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2565 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2567 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2570 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2994 "upb/json/parser.c" +#line 2997 "upb/json/parser.c" } } } @@ -2998,7 +3001,7 @@ goto _again;} } _out: {} } -#line 2603 "upb/json/parser.rl" +#line 2606 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3046,13 +3049,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3050 "upb/json/parser.c" +#line 3053 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2650 "upb/json/parser.rl" +#line 2653 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3067,7 +3070,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_msg_field_iter i; upb_alloc *alloc = upb_arena_alloc(&c->arena); - upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); + upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); m->cache = c; @@ -3090,7 +3093,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, size_t len = upb_fielddef_getjsonname(f, NULL, 0); buf = upb_malloc(alloc, len); upb_fielddef_getjsonname(f, buf, len); - upb_strtable_insert3(&m->name_table, buf, len, v, alloc); + upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc); if (strcmp(buf, upb_fielddef_name(f)) != 0) { /* Since the JSON name is different from the regular field name, add an @@ -3109,7 +3112,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_json_parser *upb_json_parser_create(upb_env *env, const upb_json_parsermethod *method, const upb_symtab* symtab, - upb_sink *output, + upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_env_bytesallocated(env); @@ -3125,8 +3128,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, upb_bytessink_reset(&p->input_, &method->input_handler_, p); json_parser_reset(p); - upb_sink_reset(&p->top->sink, output->handlers, output->closure); - p->top->m = upb_handlers_msgdef(output->handlers); + p->top->sink = output; + p->top->m = upb_handlers_msgdef(output.handlers); if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; p->top->any_frame = json_parser_any_frame_new(p); @@ -3146,8 +3149,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, return p; } -upb_bytessink *upb_json_parser_input(upb_json_parser *p) { - return &p->input_; +upb_bytessink upb_json_parser_input(upb_json_parser *p) { + return p->input_; } const upb_byteshandler *upb_json_parsermethod_inputhandler( @@ -3174,21 +3177,22 @@ void upb_json_codecache_free(upb_json_codecache *c) { upb_gfree(c); } -upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, - const upb_msgdef *md) { +const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(&c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { - return upb_value_getptr(v); + return upb_value_getconstptr(v); } m = parsermethod_new(c, md); - v = upb_value_ptr(m); + v = upb_value_constptr(m); if (!m) return NULL; - if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; + if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL; /* Populate parser methods for all submessages, so the name tables will * be available during parsing. */ diff --git a/upb/json/parser.h b/upb/json/parser.h index d5ec396..d1a1471 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -14,17 +14,44 @@ namespace upb { namespace json { class CodeCache; -class Parser; -class ParserMethod; +class ParserPtr; +class ParserMethodPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser) -UPB_DECLARE_TYPE(upb::json::ParserMethod, upb_json_parsermethod) -UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache) +/* upb_json_parsermethod ******************************************************/ -/* upb::json::Parser **********************************************************/ +struct upb_json_parsermethod; +typedef struct upb_json_parsermethod upb_json_parsermethod; + +UPB_BEGIN_EXTERN_C + +const upb_byteshandler* upb_json_parsermethod_inputhandler( + const upb_json_parsermethod* m); + +UPB_END_EXTERN_C + +#ifdef __cplusplus + +class upb::json::ParserMethodPtr { + public: + ParserMethodPtr() : ptr_(nullptr) {} + ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {} + + const upb_json_parsermethod* ptr() const { return ptr_; } + + const BytesHandler* input_handler() const { + return upb_json_parsermethod_inputhandler(ptr()); + } + + private: + const upb_json_parsermethod* ptr_; +}; + +#endif /* __cplusplus */ + +/* upb_json_parser ************************************************************/ /* Preallocation hint: parser won't allocate more bytes than this when first * constructed. This hint may be an overestimate for some build configurations. @@ -32,89 +59,72 @@ UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache) * it may be an underestimate. */ #define UPB_JSON_PARSER_SIZE 5712 +struct upb_json_parser; +typedef struct upb_json_parser upb_json_parser; + +UPB_BEGIN_EXTERN_C + +upb_json_parser* +upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, + const upb_symtab* symtab, upb_sink output, + bool ignore_json_unknown); +upb_bytessink upb_json_parser_input(upb_json_parser* p); + +UPB_END_EXTERN_C + #ifdef __cplusplus /* Parses an incoming BytesStream, pushing the results to the destination * sink. */ -class upb::json::Parser { +class upb::json::ParserPtr { public: - static Parser* Create(Environment* env, const ParserMethod* method, - const SymbolTable* symtab, - Sink* output, bool ignore_json_unknown); + ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} - BytesSink* input(); + static ParserPtr Create(Environment* env, ParserMethodPtr method, + SymbolTable* symtab, Sink output, + bool ignore_json_unknown) { + upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; + return ParserPtr(upb_json_parser_create( + env, method.ptr(), symtab_ptr, output.sink(), ignore_json_unknown)); + } - private: - UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser) -}; - -class upb::json::ParserMethod { - public: - /* The input handlers for this decoder method. */ - const BytesHandler* input_handler() const; + BytesSink input() { return upb_json_parser_input(ptr_); } private: - UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod) + upb_json_parser* ptr_; }; -class upb::json::CodeCache { - public: - static CodeCache* New(); - static void Free(CodeCache* cache); - - /* Returns a DecoderMethod that can push data to the given handlers. - * If a suitable method already exists, it will be returned from the cache. */ - const ParserMethod *Get(const MessageDef* md); +#endif /* __cplusplus */ - private: - UPB_DISALLOW_POD_OPS(CodeCache, upb::json::CodeCache) -}; +/* upb_json_codecache *********************************************************/ -#endif +struct upb_json_codecache; +typedef struct upb_json_codecache upb_json_codecache; UPB_BEGIN_EXTERN_C -upb_json_parser* upb_json_parser_create(upb_env* e, - const upb_json_parsermethod* m, - const upb_symtab* symtab, - upb_sink* output, - bool ignore_json_unknown); -upb_bytessink *upb_json_parser_input(upb_json_parser *p); - -const upb_byteshandler *upb_json_parsermethod_inputhandler( - const upb_json_parsermethod *m); - upb_json_codecache *upb_json_codecache_new(); void upb_json_codecache_free(upb_json_codecache *cache); -upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, - const upb_msgdef* md); +const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, + const upb_msgdef* md); UPB_END_EXTERN_C #ifdef __cplusplus -namespace upb { -namespace json { -inline Parser* Parser::Create(Environment* env, const ParserMethod* method, - const SymbolTable* symtab, - Sink* output, bool ignore_json_unknown) { - return upb_json_parser_create( - env, method, symtab, output, ignore_json_unknown); -} -inline BytesSink* Parser::input() { - return upb_json_parser_input(this); -} - -inline const BytesHandler* ParserMethod::input_handler() const { - return upb_json_parsermethod_inputhandler(this); -} -/* static */ -inline const ParserMethod* CodeCache::Get(const MessageDef* md) { - return upb_json_codecache_get(this, md); -} +class upb::json::CodeCache { + public: + CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {} -} /* namespace json */ -} /* namespace upb */ + /* Returns a DecoderMethod that can push data to the given handlers. + * If a suitable method already exists, it will be returned from the cache. */ + ParserMethodPtr Get(MessageDefPtr md) { + return upb_json_codecache_get(ptr_.get(), md.ptr()); + } + + private: + std::unique_ptr ptr_; +}; #endif diff --git a/upb/json/parser.rl b/upb/json/parser.rl index c2866c9..05a9505 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -299,13 +299,13 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, - &frame->sink, p->ignore_json_unknown); + frame->sink, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -381,9 +381,12 @@ static bool check_stack(upb_json_parser *p) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { upb_value v; const upb_json_codecache *cache = p->method->cache; - bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); - const upb_json_parsermethod *method = upb_value_getptr(v); + bool ok; + const upb_json_parsermethod *method; + + ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); UPB_ASSERT(ok); + method = upb_value_getconstptr(v); frame->name_table = &method->name_table; } @@ -1285,7 +1288,7 @@ static bool end_any_stringval(upb_json_parser *p) { } json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); - + return true; } else { upb_status_seterrf( @@ -2661,7 +2664,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_msg_field_iter i; upb_alloc *alloc = upb_arena_alloc(&c->arena); - upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); + upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); m->cache = c; @@ -2684,7 +2687,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, size_t len = upb_fielddef_getjsonname(f, NULL, 0); buf = upb_malloc(alloc, len); upb_fielddef_getjsonname(f, buf, len); - upb_strtable_insert3(&m->name_table, buf, len, v, alloc); + upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc); if (strcmp(buf, upb_fielddef_name(f)) != 0) { /* Since the JSON name is different from the regular field name, add an @@ -2703,7 +2706,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_json_parser *upb_json_parser_create(upb_env *env, const upb_json_parsermethod *method, const upb_symtab* symtab, - upb_sink *output, + upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_env_bytesallocated(env); @@ -2719,8 +2722,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, upb_bytessink_reset(&p->input_, &method->input_handler_, p); json_parser_reset(p); - upb_sink_reset(&p->top->sink, output->handlers, output->closure); - p->top->m = upb_handlers_msgdef(output->handlers); + p->top->sink = output; + p->top->m = upb_handlers_msgdef(output.handlers); if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; p->top->any_frame = json_parser_any_frame_new(p); @@ -2740,8 +2743,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, return p; } -upb_bytessink *upb_json_parser_input(upb_json_parser *p) { - return &p->input_; +upb_bytessink upb_json_parser_input(upb_json_parser *p) { + return p->input_; } const upb_byteshandler *upb_json_parsermethod_inputhandler( @@ -2768,21 +2771,22 @@ void upb_json_codecache_free(upb_json_codecache *c) { upb_gfree(c); } -upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, - const upb_msgdef *md) { +const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(&c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { - return upb_value_getptr(v); + return upb_value_getconstptr(v); } m = parsermethod_new(c, md); - v = upb_value_ptr(m); + v = upb_value_constptr(m); if (!m) return NULL; - if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; + if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL; /* Populate parser methods for all submessages, so the name tables will * be available during parsing. */ diff --git a/upb/json/printer.c b/upb/json/printer.c index b2c9ebd..83f1a58 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -13,7 +13,7 @@ struct upb_json_printer { upb_sink input_; /* BytesSink closure. */ void *subc_; - upb_bytessink *output_; + upb_bytessink output_; /* We track the depth so that we know when to emit startstr/endstr on the * output. */ @@ -87,7 +87,7 @@ strpc *newstrpc_str(upb_handlers *h, const char * str) { static void print_data( upb_json_printer *p, const char *buf, unsigned int len) { /* TODO: Will need to change if we support pushback from the sink. */ - size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL); + size_t n = upb_bytessink_putbuf(&p->output_, p->subc_, buf, len, NULL); UPB_ASSERT(n == len); } @@ -369,7 +369,7 @@ static bool printer_startmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } start_frame(p); return true; @@ -381,7 +381,7 @@ static bool printer_endmsg(void *closure, const void *handler_data, upb_status * UPB_UNUSED(s); end_frame(p); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } return true; } @@ -770,7 +770,7 @@ static bool printer_startdurationmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } return true; } @@ -828,7 +828,7 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data, print_data(p, "\"", 1); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } UPB_UNUSED(handler_data); @@ -839,7 +839,7 @@ static bool printer_starttimestampmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } return true; } @@ -902,7 +902,7 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data, print_data(p, "\"", 1); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } UPB_UNUSED(handler_data); @@ -914,7 +914,7 @@ static bool printer_startmsg_noframe(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } return true; } @@ -925,7 +925,7 @@ static bool printer_endmsg_noframe( UPB_UNUSED(handler_data); UPB_UNUSED(s); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } return true; } @@ -1253,7 +1253,7 @@ static void json_printer_reset(upb_json_printer *p) { /* Public API *****************************************************************/ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output) { + upb_bytessink output) { #ifndef NDEBUG size_t size_before = upb_env_bytesallocated(e); #endif @@ -1273,12 +1273,16 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, return p; } -upb_sink *upb_json_printer_input(upb_json_printer *p) { - return &p->input_; +upb_sink upb_json_printer_input(upb_json_printer *p) { + return p->input_; } upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) { upb_json_printercache *cache = upb_gmalloc(sizeof(*cache)); + upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache); + cache->preserve_fieldnames = preserve_proto_fieldnames; - return upb_handlercache_new(printer_sethandlers, cache); + upb_handlercache_addcleanup(ret, cache, upb_gfree); + + return ret; } diff --git a/upb/json/printer.h b/upb/json/printer.h index fe9c8f1..a7a37bb 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -12,44 +12,24 @@ #ifdef __cplusplus namespace upb { namespace json { -class Printer; +class PrinterPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) - - -/* upb::json::Printer *********************************************************/ +/* upb_json_printer ***********************************************************/ #define UPB_JSON_PRINTER_SIZE 192 -#ifdef __cplusplus - -/* Prints an incoming stream of data to a BytesSink in JSON format. */ -class upb::json::Printer { - public: - static Printer* Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output); - - /* The input to the printer. */ - Sink* input(); - - static const size_t kSize = UPB_JSON_PRINTER_SIZE; - static upb_handlercache* NewCache(bool preserve_proto_fieldnames); - - private: - UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer) -}; - -#endif +struct upb_json_printer; +typedef struct upb_json_printer upb_json_printer; UPB_BEGIN_EXTERN_C /* Native C API. */ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output); -upb_sink *upb_json_printer_input(upb_json_printer *p); + upb_bytessink output); +upb_sink upb_json_printer_input(upb_json_printer *p); const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, bool preserve_fieldnames, const void *owner); @@ -60,19 +40,29 @@ UPB_END_EXTERN_C #ifdef __cplusplus -namespace upb { -namespace json { -inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output) { - return upb_json_printer_create(env, handlers, output); -} -inline Sink* Printer::input() { return upb_json_printer_input(this); } -inline upb_handlercache* Printer::NewCache(bool preserve_proto_fieldnames) { - return upb_json_printer_newcache(preserve_proto_fieldnames); -} -} /* namespace json */ -} /* namespace upb */ +/* Prints an incoming stream of data to a BytesSink in JSON format. */ +class upb::json::PrinterPtr { + public: + PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {} -#endif + static PrinterPtr Create(Environment *env, const upb::Handlers *handlers, + BytesSink output) { + return PrinterPtr(upb_json_printer_create(env, handlers, output.sink())); + } + + /* The input to the printer. */ + Sink input() { return upb_json_printer_input(ptr_); } + + static const size_t kSize = UPB_JSON_PRINTER_SIZE; + + static HandlerCache NewCache(bool preserve_proto_fieldnames) { + return upb_json_printer_newcache(preserve_proto_fieldnames); + } + + private: + upb_json_printer* ptr_; +}; + +#endif /* __cplusplus */ #endif /* UPB_JSON_TYPED_PRINTER_H_ */ diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index 0cae05b..cd64f72 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -993,7 +993,7 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) { } upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, - upb_sink *sink) { + upb_sink sink) { const size_t default_max_nesting = 64; #ifndef NDEBUG size_t size_before = upb_env_bytesallocated(e); @@ -1017,12 +1017,11 @@ upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, upb_pbdecoder_reset(d); upb_bytessink_reset(&d->input_, &m->input_handler_, d); - UPB_ASSERT(sink); if (d->method_->dest_handlers_) { - if (sink->handlers != d->method_->dest_handlers_) + if (sink.handlers != d->method_->dest_handlers_) return NULL; } - upb_sink_reset(&d->top->sink, sink->handlers, sink->closure); + d->top->sink = sink; /* If this fails, increase the value in decoder.h. */ UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= @@ -1038,8 +1037,8 @@ const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) { return d->method_; } -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) { - return &d->input_; +upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d) { + return d->input_; } size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) { diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 1ffcb7d..ba381f3 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -56,6 +56,7 @@ UPB_END_EXTERN_C * Handlers. */ class upb::pb::DecoderMethodPtr { public: + DecoderMethodPtr() : ptr_(nullptr) {} DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {} const upb_pbdecodermethod* ptr() { return ptr_; } @@ -98,9 +99,9 @@ UPB_BEGIN_EXTERN_C upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *method, - upb_sink *output); + upb_sink output); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); +upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d); uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); @@ -124,8 +125,8 @@ class upb::pb::DecoderPtr { * * The sink must match the given method. */ static DecoderPtr Create(Environment *env, DecoderMethodPtr method, - upb_sink *output) { - return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output)); + upb::Sink output) { + return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output.sink())); } /* Returns the DecoderMethod this decoder is parsing from. */ @@ -134,7 +135,7 @@ class upb::pb::DecoderPtr { } /* The sink on which this decoder receives input. */ - upb_bytessink* input() { return upb_pbdecoder_input(ptr()); } + BytesSink input() { return BytesSink(upb_pbdecoder_input(ptr())); } /* Returns number of bytes successfully parsed. * diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 3497007..1496eba 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -95,7 +95,7 @@ struct upb_pb_encoder { /* Our input and output. */ upb_sink input_; - upb_bytessink *output_; + upb_bytessink output_; /* The "subclosure" -- used as the inner closure as part of the bytessink * protocol. */ @@ -127,7 +127,7 @@ struct upb_pb_encoder { /* TODO(haberman): handle pushback */ static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) { - size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL); + size_t n = upb_bytessink_putbuf(&e->output_, e->subc, buf, len, NULL); UPB_ASSERT(n == len); } @@ -353,7 +353,7 @@ static bool startmsg(void *c, const void *hd) { upb_pb_encoder *e = c; UPB_UNUSED(hd); if (e->depth++ == 0) { - upb_bytessink_start(e->output_, 0, &e->subc); + upb_bytessink_start(&e->output_, 0, &e->subc); } return true; } @@ -363,7 +363,7 @@ static bool endmsg(void *c, const void *hd, upb_status *status) { UPB_UNUSED(hd); UPB_UNUSED(status); if (--e->depth == 0) { - upb_bytessink_end(e->output_); + upb_bytessink_end(&e->output_); } return true; } @@ -527,7 +527,7 @@ upb_handlercache *upb_pb_encoder_newcache() { } upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output) { + upb_bytessink output) { const size_t initial_bufsize = 256; const size_t initial_segbufsize = 16; /* TODO(haberman): make this configurable. */ @@ -556,7 +556,7 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, e->env = env; e->output_ = output; - e->subc = output->closure; + e->subc = output.closure; e->ptr = e->buf; /* If this fails, increase the value in encoder.h. */ @@ -565,4 +565,4 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, return e; } -upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; } +upb_sink upb_pb_encoder_input(upb_pb_encoder *e) { return e->input_; } diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 20ce606..7aa2870 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -30,16 +30,16 @@ class EncoderPtr; * constructed. This hint may be an overestimate for some build configurations. * But if the decoder library is upgraded without recompiling the application, * it may be an underestimate. */ -#define UPB_PB_ENCODER_SIZE 768 +#define UPB_PB_ENCODER_SIZE 784 struct upb_pb_encoder; typedef struct upb_pb_encoder upb_pb_encoder; UPB_BEGIN_EXTERN_C -upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); +upb_sink upb_pb_encoder_input(upb_pb_encoder *p); upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, - upb_bytessink* output); + upb_bytessink output); upb_handlercache *upb_pb_encoder_newcache(); @@ -56,12 +56,12 @@ class upb::pb::EncoderPtr { /* Creates a new encoder in the given environment. The Handlers must have * come from NewHandlers() below. */ static EncoderPtr Create(Environment* env, const Handlers* handlers, - BytesSink* output) { - return EncoderPtr(upb_pb_encoder_create(env, handlers, output->ptr())); + BytesSink output) { + return EncoderPtr(upb_pb_encoder_create(env, handlers, output.sink())); } /* The input to the encoder. */ - upb_sink* input() { return upb_pb_encoder_input(ptr()); } + upb::Sink input() { return upb_pb_encoder_input(ptr()); } /* Creates a new set of handlers for this MessageDef. */ static HandlerCache NewCache() { diff --git a/upb/sink.c b/upb/sink.c index d0197a6..6ef5718 100644 --- a/upb/sink.c +++ b/upb/sink.c @@ -1,17 +1,17 @@ #include "upb/sink.h" -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) { +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) { void *subc; bool ret; upb_bufhandle handle = UPB_BUFHANDLE_INIT; handle.buf = buf; - ret = upb_bytessink_start(sink, len, &subc); + ret = upb_bytessink_start(&sink, len, &subc); if (ret && len != 0) { - ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); + ret = (upb_bytessink_putbuf(&sink, subc, buf, len, &handle) >= len); } if (ret) { - ret = upb_bytessink_end(sink); + ret = upb_bytessink_end(&sink); } return ret; } diff --git a/upb/sink.h b/upb/sink.h index 8cab45d..1359c5e 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -237,6 +237,17 @@ class upb::Sink { /* Constructor with no initialization; must be Reset() before use. */ Sink() {} + Sink(const Sink&) = default; + Sink& operator=(const Sink&) = default; + + Sink(const upb_sink& sink) : sink_(sink) {} + Sink &operator=(const upb_sink &sink) { + sink_ = sink; + return *this; + } + + upb_sink sink() { return sink_; } + /* Constructs a new sink for the given frozen handlers and closure. * * TODO: once the Handlers know the expected closure type, verify that T @@ -406,7 +417,16 @@ class upb::BytesSink { public: BytesSink() {} - upb_bytessink* ptr() { return &sink_; } + BytesSink(const BytesSink&) = default; + BytesSink& operator=(const BytesSink&) = default; + + BytesSink(const upb_bytessink& sink) : sink_(sink) {} + BytesSink &operator=(const upb_bytessink &sink) { + sink_ = sink; + return *this; + } + + upb_bytessink sink() { return sink_; } /* Constructs a new sink for the given frozen handlers and closure. * @@ -444,15 +464,15 @@ class upb::BytesSink { UPB_BEGIN_EXTERN_C -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink); +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink); UPB_END_EXTERN_C #ifdef __cplusplus namespace upb { -template bool PutBuffer(const T& str, upb_bytessink* sink) { - return upb_bufsrc_putbuf(str.c_str(), str.size(), sink); +template bool PutBuffer(const T& str, BytesSink sink) { + return upb_bufsrc_putbuf(str.c_str(), str.size(), sink.sink()); } } -- cgit v1.2.3 From cb26d883d1290ed258e5594454c2ffe0526b13f9 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 14 Jan 2019 10:56:58 -0800 Subject: WIP. --- google/protobuf/descriptor.upb.h | 8 +- tests/conformance_upb.c | 16 +-- tests/json/test_json.cc | 4 +- tests/pb/test_decoder.cc | 14 +-- tests/pb/test_encoder.cc | 9 +- tests/test_cpp.cc | 2 +- tests/test_util.h | 21 ++-- upb/bindings/lua/def.c | 9 +- upb/bindings/lua/msg.c | 13 +- upb/bindings/lua/upb/pb.c | 8 +- upb/decode.h | 8 +- upb/def.c | 41 ++++--- upb/def.h | 37 +++--- upb/encode.h | 8 +- upb/generated_util.h | 3 +- upb/handlers.c | 8 +- upb/handlers.h | 39 +++--- upb/json/parser.c | 259 ++++++++++++++++----------------------- upb/json/parser.h | 37 +++--- upb/json/parser.rl | 153 +++++++++-------------- upb/json/printer.c | 8 +- upb/json/printer.h | 14 ++- upb/msg.h | 28 ++--- upb/msgfactory.h | 12 +- upb/pb/compile_decoder.c | 2 +- upb/pb/decoder.c | 22 ++-- upb/pb/decoder.h | 28 +++-- upb/pb/decoder.int.h | 4 +- upb/pb/encoder.c | 22 ++-- upb/pb/encoder.h | 14 ++- upb/pb/textprinter.c | 26 ++-- upb/pb/textprinter.h | 75 +++++------- upb/sink.c | 70 ----------- upb/sink.h | 33 ++--- upb/table.int.h | 15 --- upb/upb.c | 75 +++++------- upb/upb.h | 161 +++++++++--------------- upbc/generator.cc | 8 +- 38 files changed, 555 insertions(+), 759 deletions(-) (limited to 'tests/pb/test_decoder.cc') diff --git a/google/protobuf/descriptor.upb.h b/google/protobuf/descriptor.upb.h index 32bccc7..7e62be5 100644 --- a/google/protobuf/descriptor.upb.h +++ b/google/protobuf/descriptor.upb.h @@ -16,7 +16,9 @@ #include "upb/decode.h" #include "upb/encode.h" #include "upb/port_def.inc" -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif struct google_protobuf_FileDescriptorSet; struct google_protobuf_FileDescriptorProto; @@ -1668,7 +1670,9 @@ UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_prot } -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #include "upb/port_undef.inc" diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index e0a7d8c..cefac4c 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -130,7 +130,7 @@ void DoTest( } bool DoTestIo() { - upb_arena arena; + upb_arena *arena; upb_alloc *alloc; upb_status status; char *serialized_input; @@ -145,8 +145,8 @@ bool DoTestIo() { return false; } - upb_arena_init(&arena); - alloc = upb_arena_alloc(&arena); + arena = upb_arena_new(); + alloc = upb_arena_alloc(arena); serialized_input = upb_malloc(alloc, input_size); if (!CheckedRead(STDIN_FILENO, serialized_input, input_size)) { @@ -155,24 +155,26 @@ bool DoTestIo() { } request = conformance_ConformanceRequest_parsenew( - upb_stringview_make(serialized_input, input_size), &arena); - response = conformance_ConformanceResponse_new(&arena); + upb_stringview_make(serialized_input, input_size), arena); + response = conformance_ConformanceResponse_new(arena); if (request) { - DoTest(request, response, &arena); + DoTest(request, response, arena); } else { fprintf(stderr, "conformance_upb: parse of ConformanceRequest failed: %s\n", upb_status_errmsg(&status)); } serialized_output = conformance_ConformanceResponse_serialize( - response, &arena, &output_size); + response, arena, &output_size); CheckedWrite(STDOUT_FILENO, &output_size, sizeof(uint32_t)); CheckedWrite(STDOUT_FILENO, serialized_output, output_size); test_count++; + upb_arena_free(arena); + return true; } diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index b0fd3e3..1f7d364 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -174,9 +174,9 @@ void test_json_roundtrip_message(const char* json_src, VerboseParserEnvironment env(verbose); StringSink data_sink; upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create( - env.env(), serialize_handlers, data_sink.Sink()); + env.arena(), serialize_handlers, data_sink.Sink()); upb::json::ParserPtr parser = upb::json::ParserPtr::Create( - env.env(), parser_method, NULL, printer.input(), false); + env.arena(), parser_method, NULL, printer.input(), false); env.ResetBytesSink(parser.input()); env.Reset(json_src, strlen(json_src), false, false); diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index ec7a788..b2dd812 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -452,10 +452,10 @@ void callback(const void *closure, upb::Handlers* h_ptr) { const upb::Handlers *global_handlers; upb::pb::DecoderMethodPtr global_method; -upb::pb::DecoderPtr CreateDecoder(upb::Environment* env, +upb::pb::DecoderPtr CreateDecoder(upb::Arena* arena, upb::pb::DecoderMethodPtr method, upb::Sink sink) { - upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(env, method, sink); + upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(arena, method, sink); ret.set_max_nesting(MAX_NESTING); return ret; } @@ -556,7 +556,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, void run_decoder(const string& proto, const string* expected_output) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + upb::pb::DecoderPtr decoder = CreateDecoder(env.arena(), global_method, sink); env.ResetBytesSink(decoder.input()); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { @@ -872,10 +872,9 @@ void test_valid() { if (!filter_hash || filter_hash == testhash) { testhash = emptyhash; upb::Status status; - upb::Environment env; - env.ReportErrorsTo(&status); + upb::Arena arena; upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(&env, global_method, sink); + upb::pb::DecoderPtr decoder = CreateDecoder(&arena, global_method, sink); output.clear(); bool ok = upb::PutBuffer(std::string(), decoder.input()); ASSERT(ok); @@ -1161,7 +1160,8 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { for (int i = 0; testdata[i].data; i++) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_method.dest_handlers(), &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + upb::pb::DecoderPtr decoder = + CreateDecoder(env.arena(), global_method, sink); env.ResetBytesSink(decoder.input()); env.Reset(testdata[i].data, testdata[i].length, true, false); ASSERT(env.Start()); diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 35c0e1e..7145097 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -24,7 +24,7 @@ void test_pb_roundtrip() { upb::Arena arena; google_protobuf_FileDescriptorSet *set = google_protobuf_FileDescriptorSet_parsenew( - upb_stringview_make(input.c_str(), input.size()), &arena); + upb_stringview_make(input.c_str(), input.size()), arena.ptr()); ASSERT(set); size_t n; const google_protobuf_FileDescriptorProto *const *files = @@ -33,7 +33,7 @@ void test_pb_roundtrip() { upb::Status status; bool ok = symtab.AddFile(files[0], &status); if (!ok) { - fprintf(stderr, "Error building def: %s\n", upb_status_errmsg(&status)); + fprintf(stderr, "Error building def: %s\n", status.error_message()); ASSERT(false); } upb::MessageDefPtr md = @@ -43,13 +43,12 @@ void test_pb_roundtrip() { ASSERT(encoder_handlers); const upb::pb::DecoderMethodPtr method = decoder_cache.Get(md); - upb::InlinedEnvironment<512> env; std::string output; upb::StringSink string_sink(&output); upb::pb::EncoderPtr encoder = - upb::pb::EncoderPtr::Create(&env, encoder_handlers, string_sink.input()); + upb::pb::EncoderPtr::Create(&arena, encoder_handlers, string_sink.input()); upb::pb::DecoderPtr decoder = - upb::pb::DecoderPtr::Create(&env, method, encoder.input()); + upb::pb::DecoderPtr::Create(&arena, method, encoder.input()); ok = upb::PutBuffer(input, decoder.input()); ASSERT(ok); ASSERT(input == output); diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index a5bfbc7..5e9a8dd 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -672,7 +672,7 @@ void DoNothingStringBufHandler(C* closure, const char *buf, size_t len) { } template -void DoNothingEndMessageHandler(C* closure, upb::Status *status) { +void DoNothingEndMessageHandler(C* closure, upb_status *status) { UPB_UNUSED(closure); UPB_UNUSED(status); } diff --git a/tests/test_util.h b/tests/test_util.h index 0b5ddd4..04ca3fb 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -30,15 +30,11 @@ upb_bufhandle global_handle; class VerboseParserEnvironment { public: /* Pass verbose=true to print detailed diagnostics to stderr. */ - VerboseParserEnvironment(bool verbose) : verbose_(verbose) { - env_.SetErrorFunction(&VerboseParserEnvironment::OnError, this); - } + VerboseParserEnvironment(bool verbose) : verbose_(verbose) {} static bool OnError(void *ud, const upb::Status* status) { VerboseParserEnvironment* env = static_cast(ud); - env->saw_error_ = true; - if (env->expect_error_ && env->verbose_) { fprintf(stderr, "Encountered error, as expected: "); } else if (!env->expect_error_) { @@ -56,7 +52,6 @@ class VerboseParserEnvironment { len_ = len; ofs_ = 0; expect_error_ = expect_error; - saw_error_ = false; end_ok_set_ = false; skip_until_ = may_skip ? 0 : -1; skipped_with_null_ = false; @@ -94,12 +89,12 @@ class VerboseParserEnvironment { bool CheckConsistency() { /* If we called end (which we should only do when previous bytes are fully * accepted), then end() should return true iff there were no errors. */ - if (end_ok_set_ && end_ok_ != !saw_error_) { + if (end_ok_set_ && end_ok_ != status_.ok()) { fprintf(stderr, "End() status and saw_error didn't match.\n"); return false; } - if (expect_error_ && !saw_error_) { + if (expect_error_ && status_.ok()) { fprintf(stderr, "Expected error but saw none.\n"); return false; } @@ -158,8 +153,9 @@ class VerboseParserEnvironment { } } - if (saw_error_) + if (!status_.ok()) { return false; + } if (parsed > bytes && skip_until_ >= 0) { skip_until_ = ofs_ + parsed; @@ -175,12 +171,14 @@ class VerboseParserEnvironment { } size_t ofs() { return ofs_; } - upb::Environment* env() { return &env_; } bool SkippedWithNull() { return skipped_with_null_; } + upb::Arena* arena() { return &arena_; } + private: - upb::Environment env_; + upb::Arena arena_; + upb::Status status_; upb::BytesSink sink_; const char* buf_; size_t len_; @@ -188,7 +186,6 @@ class VerboseParserEnvironment { size_t ofs_; void *subc_; bool expect_error_; - bool saw_error_; bool end_ok_; bool end_ok_set_; diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 76510be..c38b6d1 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -15,13 +15,14 @@ #define LUPB_SYMTAB "lupb.symtab" #define LUPB_OBJCACHE "lupb.objcache" -#define CHK(pred) do { \ - upb_status status = UPB_STATUS_INIT; \ - pred; \ +#define CHK(pred) \ + do { \ + upb_status status; \ + upb_status_clear(&status); \ + pred; \ lupb_checkstatus(L, &status); \ } while (0) - /* lupb_wrapper ***************************************************************/ /* Wrappers around upb objects. */ diff --git a/upb/bindings/lua/msg.c b/upb/bindings/lua/msg.c index e983f46..df5a143 100644 --- a/upb/bindings/lua/msg.c +++ b/upb/bindings/lua/msg.c @@ -100,16 +100,21 @@ static void *lupb_newuserdata(lua_State *L, size_t size, const char *type) { * it is an internal memory management detail. Other objects refer to this * object from their userdata to keep the arena-owned data alive. */ +typedef struct { + upb_arena *arena; +} lupb_arena; + upb_arena *lupb_arena_check(lua_State *L, int narg) { - return luaL_checkudata(L, narg, LUPB_ARENA); + lupb_arena *a = luaL_checkudata(L, narg, LUPB_ARENA); + return a ? a->arena : NULL; } int lupb_arena_new(lua_State *L) { - upb_arena *a = lupb_newuserdata(L, sizeof(upb_arena), LUPB_ARENA); + lupb_arena *a = lupb_newuserdata(L, sizeof(lupb_arena), LUPB_ARENA); /* TODO(haberman): use Lua alloc func as block allocator? Would need to * verify that all cases of upb_malloc in msg/table are longjmp-safe. */ - upb_arena_init(a); + a->arena = upb_arena_new(); return 1; } @@ -140,7 +145,7 @@ static void lupb_arena_initsingleton(lua_State *L) { static int lupb_arena_gc(lua_State *L) { upb_arena *a = lupb_arena_check(L, 1); - upb_arena_uninit(a); + upb_arena_free(a); return 0; } diff --git a/upb/bindings/lua/upb/pb.c b/upb/bindings/lua/upb/pb.c index bca2ee8..2edefe0 100644 --- a/upb/bindings/lua/upb/pb.c +++ b/upb/bindings/lua/upb/pb.c @@ -27,17 +27,15 @@ static int lupb_pb_decode(lua_State *L) { static int lupb_pb_encode(lua_State *L) { const upb_msglayout *layout; const upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout); - upb_arena arena; + upb_arena *arena = upb_arena_new(); size_t size; char *result; - upb_arena_init(&arena); - - result = upb_encode(msg, (const void*)layout, &arena, &size); + result = upb_encode(msg, (const void*)layout, arena, &size); /* Free resources before we potentially bail on error. */ lua_pushlstring(L, result, size); - upb_arena_uninit(&arena); + upb_arena_free(arena); /* TODO(haberman): check for error. */ return 1; diff --git a/upb/decode.h b/upb/decode.h index 79774ed..790d7ef 100644 --- a/upb/decode.h +++ b/upb/decode.h @@ -7,10 +7,14 @@ #include "upb/msg.h" -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif bool upb_decode(upb_stringview buf, upb_msg *msg, const upb_msglayout *l); -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_DECODE_H_ */ diff --git a/upb/def.c b/upb/def.c index 27de875..c744138 100644 --- a/upb/def.c +++ b/upb/def.c @@ -125,7 +125,7 @@ static upb_value pack_def(const void *ptr, upb_deftype_t type) { } struct upb_symtab { - upb_arena arena; + upb_arena *arena; upb_strtable syms; /* full_name -> packed def ptr */ upb_strtable files; /* file_name -> upb_filedef* */ }; @@ -224,7 +224,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { fields = upb_gmalloc(n * sizeof(*fields)); if (!fields) { - upb_upberr_setoom(s); + upb_status_setoom(s); return false; } @@ -870,7 +870,7 @@ const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { } void upb_symtab_free(upb_symtab *s) { - upb_arena_uninit(&s->arena); + upb_arena_free(s->arena); upb_gfree(s); } @@ -882,12 +882,12 @@ upb_symtab *upb_symtab_new() { return NULL; } - upb_arena_init(&s->arena); - alloc = upb_arena_alloc(&s->arena); + s->arena = upb_arena_new(); + alloc = upb_arena_alloc(s->arena); if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) || !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) { - upb_arena_uninit(&s->arena); + upb_arena_free(s->arena); upb_gfree(s); s = NULL; } @@ -922,7 +922,7 @@ const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { * to validate important constraints like uniqueness of names and numbers. */ #define CHK(x) if (!(x)) { return false; } -#define CHK_OOM(x) if (!(x)) { upb_upberr_setoom(ctx->status); return false; } +#define CHK_OOM(x) if (!(x)) { upb_status_setoom(ctx->status); return false; } typedef struct { const upb_symtab *symtab; @@ -1632,7 +1632,7 @@ static bool build_filedef( static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, upb_status *status) { const upb_filedef *file = ctx->file; - upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_alloc *alloc = upb_arena_alloc(s->arena); upb_strtable_iter iter; CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name), @@ -1652,9 +1652,9 @@ static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, bool upb_symtab_addfile(upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto, upb_status *status) { - upb_arena tmparena; + upb_arena *tmparena = upb_arena_new(); upb_strtable addtab; - upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_alloc *alloc = upb_arena_alloc(s->arena); upb_filedef *file = upb_malloc(alloc, sizeof(*file)); bool ok; symtab_addctx ctx; @@ -1662,18 +1662,16 @@ bool upb_symtab_addfile(upb_symtab *s, ctx.file = file; ctx.symtab = s; ctx.alloc = alloc; - ctx.tmp = upb_arena_alloc(&tmparena); + ctx.tmp = upb_arena_alloc(tmparena); ctx.addtab = &addtab; ctx.status = status; - upb_arena_init(&tmparena); - ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx, status); - upb_arena_uninit(&tmparena); + upb_arena_free(tmparena); return ok; } @@ -1685,19 +1683,22 @@ bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { * print errors to stderr instead of returning error status to the user. */ upb_def_init **deps = init->deps; google_protobuf_FileDescriptorProto *file; - upb_arena arena; - upb_status status = UPB_STATUS_INIT; + upb_arena *arena; + upb_status status; + + upb_status_clear(&status); if (upb_strtable_lookup(&s->files, init->filename, NULL)) { return true; } + arena = upb_arena_new(); + for (; *deps; deps++) { if (!_upb_symtab_loaddefinit(s, *deps)) goto err; } - upb_arena_init(&arena); - file = google_protobuf_FileDescriptorProto_parsenew(init->descriptor, &arena); + file = google_protobuf_FileDescriptorProto_parsenew(init->descriptor, arena); if (!file) { upb_status_seterrf( @@ -1710,13 +1711,13 @@ bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { if (!upb_symtab_addfile(s, file, &status)) goto err; - upb_arena_uninit(&arena); + upb_arena_free(arena); return true; err: fprintf(stderr, "Error loading compiled-in descriptor: %s\n", upb_status_errmsg(&status)); - upb_arena_uninit(&arena); + upb_arena_free(arena); return false; } diff --git a/upb/def.h b/upb/def.h index fb8a71d..b1cf275 100644 --- a/upb/def.h +++ b/upb/def.h @@ -56,7 +56,9 @@ typedef struct upb_symtab upb_symtab; * protobuf wire format. */ #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_fielddef_fullname(const upb_fielddef *f); upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); @@ -93,9 +95,8 @@ const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); /* Internal only. */ uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A upb_fielddef describes a single field in a message. It is most often * found as a part of a upb_msgdef, but can also stand alone to represent @@ -228,7 +229,9 @@ class upb::FieldDefPtr { /* upb_oneofdef ***************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif typedef upb_inttable_iter upb_oneof_iter; @@ -262,9 +265,8 @@ void upb_oneof_iter_setdone(upb_oneof_iter *iter); bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1, const upb_oneof_iter *iter2); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Class that represents a oneof. */ class upb::OneofDefPtr { @@ -365,7 +367,9 @@ typedef upb_strtable_iter upb_msg_oneof_iter; #define UPB_TIMESTAMP_SECONDS 1 #define UPB_TIMESTAMP_NANOS 2 -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_msgdef_fullname(const upb_msgdef *m); const upb_filedef *upb_msgdef_file(const upb_msgdef *m); @@ -441,9 +445,8 @@ void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter); bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, const upb_msg_oneof_iter *iter2); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Structure that describes a single .proto message type. */ class upb::MessageDefPtr { @@ -711,7 +714,9 @@ class upb::EnumDefPtr { /* upb_filedef ****************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_filedef_name(const upb_filedef *f); const char *upb_filedef_package(const upb_filedef *f); @@ -725,9 +730,8 @@ const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Class that represents a .proto file with some things defined in it. * @@ -773,7 +777,9 @@ class upb::FileDefPtr { /* upb_symtab *****************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_symtab *upb_symtab_new(); void upb_symtab_free(upb_symtab* s); @@ -795,9 +801,8 @@ typedef struct upb_def_init { bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Non-const methods in upb::SymbolTable are NOT thread-safe. */ class upb::SymbolTable { @@ -823,7 +828,7 @@ class upb::SymbolTable { /* Adds the given serialized FileDescriptorProto to the pool. */ bool AddFile(const google_protobuf_FileDescriptorProto *file_proto, Status *status) { - return upb_symtab_addfile(ptr_.get(), file_proto, status); + return upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()); } private: diff --git a/upb/encode.h b/upb/encode.h index 1a451b0..6842777 100644 --- a/upb/encode.h +++ b/upb/encode.h @@ -7,11 +7,15 @@ #include "upb/msg.h" -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena, size_t *size); -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_ENCODE_H_ */ diff --git a/upb/generated_util.h b/upb/generated_util.h index 3989f1e..657280f 100644 --- a/upb/generated_util.h +++ b/upb/generated_util.h @@ -54,10 +54,9 @@ UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, size_t new_size = UPB_MAX(arr->size, 4); size_t old_bytes = arr->size * elem_size; size_t new_bytes; - upb_alloc *alloc = upb_arena_alloc(arr->arena); while (new_size < size) new_size *= 2; new_bytes = new_size * elem_size; - arr->data = upb_realloc(alloc, arr->data, old_bytes, new_bytes); + arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes); if (!arr->data) { return NULL; } diff --git a/upb/handlers.c b/upb/handlers.c index ca978bf..aa23b46 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -390,7 +390,7 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { /* upb_handlercache ***********************************************************/ struct upb_handlercache { - upb_arena arena; + upb_arena *arena; upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ upb_inttable cleanup_; upb_handlers_callback *callback; @@ -407,7 +407,7 @@ const upb_handlers *upb_handlercache_get(upb_handlercache *c, return upb_value_getptr(v); } - h = upb_handlers_new(md, c, &c->arena); + h = upb_handlers_new(md, c, c->arena); v = upb_value_ptr(h); if (!h) return NULL; @@ -442,7 +442,7 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, if (!cache) return NULL; - upb_arena_init(&cache->arena); + cache->arena = upb_arena_new(); cache->callback = callback; cache->closure = closure; @@ -470,7 +470,7 @@ void upb_handlercache_free(upb_handlercache *cache) { upb_inttable_uninit(&cache->tab); upb_inttable_uninit(&cache->cleanup_); - upb_arena_uninit(&cache->arena); + upb_arena_free(cache->arena); upb_gfree(cache); } diff --git a/upb/handlers.h b/upb/handlers.h index 44cad18..764e83e 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -157,7 +157,9 @@ typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, struct upb_handlers; typedef struct upb_handlers upb_handlers; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif /* Mutating accessors. */ const upb_status *upb_handlers_status(upb_handlers *h); @@ -235,9 +237,8 @@ UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); uint32_t upb_handlers_selectorcount(const upb_fielddef *f); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ namespace upb { typedef upb_handlers Handlers; @@ -303,7 +304,9 @@ template class upb::Handler { const upb_handlerattr& attr() const { return attr_; } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Handler) + Handler(const Handler&) = delete; + Handler& operator=(const Handler&) = delete; + FuncPtr handler_; mutable upb_handlerattr attr_; mutable bool registered_; @@ -334,7 +337,8 @@ class upb::HandlersPtr { typedef Handler StartFieldHandler; typedef Handler EndFieldHandler; typedef Handler StartMessageHandler; - typedef Handler EndMessageHandler; + typedef Handler + EndMessageHandler; typedef Handler StartStringHandler; typedef Handler @@ -590,7 +594,9 @@ class upb::HandlersPtr { /* upb_handlercache ***********************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif struct upb_handlercache; typedef struct upb_handlercache upb_handlercache; @@ -605,9 +611,8 @@ const upb_handlers *upb_handlercache_get(upb_handlercache *cache, bool upb_handlercache_addcleanup(upb_handlercache *h, void *p, upb_handlerfree *hfree); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::HandlerCache { public: @@ -631,8 +636,6 @@ class upb::HandlerCache { /* upb_byteshandler ***********************************************************/ -UPB_BEGIN_EXTERN_C - typedef struct { upb_func *func; @@ -665,6 +668,10 @@ UPB_INLINE void upb_byteshandler_init(upb_byteshandler *handler) { *handler = init; } +#ifdef __cplusplus +extern "C" { +#endif + /* Caller must ensure that "d" outlives the handlers. */ bool upb_byteshandler_setstartstr(upb_byteshandler *h, upb_startstr_handlerfunc *func, void *d); @@ -674,16 +681,18 @@ bool upb_byteshandler_setendstr(upb_byteshandler *h, upb_endfield_handlerfunc *func, void *d); #ifdef __cplusplus +} /* extern "C" */ + namespace upb { typedef upb_byteshandler BytesHandler; } #endif -UPB_END_EXTERN_C - /** Message handlers ******************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif /* These are the handlers used internally by upb_msgfactory_getmergehandlers(). * They write scalar data to a known offset from the message pointer. @@ -710,7 +719,9 @@ bool upb_msg_getscalarhandlerdata(const upb_handlers *h, -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #include "upb/handlers-inl.h" diff --git a/upb/json/parser.c b/upb/json/parser.c index 1dac800..a594bfd 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -212,7 +212,7 @@ typedef struct { } upb_jsonparser_frame; struct upb_json_parser { - upb_env *env; + upb_arena *arena; const upb_json_parsermethod *method; upb_bytessink input_; @@ -221,7 +221,7 @@ struct upb_json_parser { upb_jsonparser_frame *top; upb_jsonparser_frame *limit; - upb_status status; + upb_status *status; /* Ragel's internal parsing stack for the parsing state machine. */ int current_state; @@ -259,7 +259,7 @@ struct upb_json_parser { }; struct upb_json_codecache { - upb_arena arena; + upb_arena *arena; upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ }; @@ -277,7 +277,7 @@ static upb_jsonparser_any_frame *json_parser_any_frame_new( upb_json_parser *p) { upb_jsonparser_any_frame *frame; - frame = upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + frame = upb_arena_malloc(p->arena, sizeof(upb_jsonparser_any_frame)); frame->encoder_handlercache = upb_pb_encoder_newcache(); frame->parser_codecache = upb_json_codecache_new(); @@ -301,12 +301,12 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->arena, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, + frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, p->ignore_json_unknown); } @@ -372,8 +372,7 @@ static upb_selector_t parser_getsel(upb_json_parser *p) { static bool check_stack(upb_json_parser *p) { if ((p->top + 1) == p->limit) { - upb_status_seterrmsg(&p->status, "Nesting too deep"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Nesting too deep"); return false; } @@ -468,10 +467,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, char output[3]; if (limit - ptr < 4) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Base64 input for bytes field not a multiple of 4: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -495,10 +493,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, otherchar: if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) || nonbase64(ptr[3]) ) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Non-base64 characters in bytes field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } if (ptr[2] == '=') { uint32_t val; @@ -536,11 +533,10 @@ otherchar: } badpadding: - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Incorrect base64 padding for field: %s (%.*s)", upb_fielddef_name(p->top->f), 4, ptr); - upb_env_reporterror(p->env, &p->status); return false; } @@ -584,10 +580,9 @@ static bool accumulate_realloc(upb_json_parser *p, size_t need) { new_size = saturating_multiply(new_size, 2); } - mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size); + mem = upb_arena_realloc(p->arena, p->accumulate_buf, old_size, new_size); if (!mem) { - upb_status_seterrmsg(&p->status, "Out of memory allocating buffer."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Out of memory allocating buffer."); return false; } @@ -610,8 +605,7 @@ static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, } if (!checked_add(p->accumulated_len, len, &need)) { - upb_status_seterrmsg(&p->status, "Integer overflow."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Integer overflow."); return false; } @@ -689,8 +683,7 @@ static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, switch (p->multipart_state) { case MULTIPART_INACTIVE: upb_status_seterrmsg( - &p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); - upb_env_reporterror(p->env, &p->status); + p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); return false; case MULTIPART_ACCUMULATE: @@ -1055,8 +1048,7 @@ static bool parse_number(upb_json_parser *p, bool is_quoted) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "error parsing number: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing number: %s", buf); multipart_end(p); return false; } @@ -1070,10 +1062,9 @@ static bool parser_putbool(upb_json_parser *p, bool val) { } if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Boolean value specified for non-bool field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1248,10 +1239,9 @@ static bool start_stringval(upb_json_parser *p) { multipart_startaccum(p); return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "String specified for bool or submessage field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -1284,8 +1274,7 @@ static bool end_any_stringval(upb_json_parser *p) { payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); if (payload_type == NULL) { upb_status_seterrf( - &p->status, "Cannot find packed type: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Cannot find packed type: %.*s\n", (int)len, buf); return false; } @@ -1294,8 +1283,7 @@ static bool end_any_stringval(upb_json_parser *p) { return true; } else { upb_status_seterrf( - &p->status, "Invalid type url: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Invalid type url: %.*s\n", (int)len, buf); return false; } } @@ -1347,8 +1335,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { upb_selector_t sel = parser_getsel(p); upb_sink_putint32(&p->top->sink, sel, int_val); } else { - upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); } break; @@ -1365,8 +1352,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { default: UPB_ASSERT(false); - upb_status_seterrmsg(&p->status, "Internal error in JSON decoder"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Internal error in JSON decoder"); ok = false; break; } @@ -1445,25 +1431,22 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(seconds_buf, buf, fraction_start); seconds = strtol(seconds_buf, &end, 10); if (errno == ERANGE || end != seconds_buf + fraction_start) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", seconds_buf); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds > 315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "maximum acceptable value is " "315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds < -315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "minimum acceptable value is " "-315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1472,9 +1455,8 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start); val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1500,7 +1482,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); end_member(p); - /* Continue previous environment */ + /* Continue previous arena */ multipart_startaccum(p); return true; @@ -1530,8 +1512,7 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { /* Parse seconds */ if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { - upb_status_seterrf(&p->status, "error parsing timestamp: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp: %s", buf); return false; } @@ -1564,9 +1545,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { buf = accumulate_getptr(p, &len); if (len > 10) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "error parsing timestamp: at most 9-digit fraction."); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1576,9 +1556,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len + 1) { - upb_status_seterrf(&p->status, "error parsing timestamp nanos: %s", + upb_status_seterrf(p->status, "error parsing timestamp nanos: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1620,8 +1599,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { if (buf[0] != 'Z') { if (sscanf(buf + 1, "%2d:00", &hours) != 1) { - upb_status_seterrf(&p->status, "error parsing timestamp offset"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp offset"); return false; } @@ -1637,10 +1615,9 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { /* Check timestamp boundary */ if (seconds < -62135596800) { - upb_status_seterrf(&p->status, "error parsing timestamp: " + upb_status_seterrf(p->status, "error parsing timestamp: " "minimum acceptable value is " "0001-01-01T00:00:00Z"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1681,8 +1658,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY); if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no key"); return false; } switch (upb_fielddef_type(p->top->f)) { @@ -1705,9 +1681,8 @@ static bool parse_mapentry_key(upb_json_parser *p) { return false; } } else { - upb_status_seterrmsg(&p->status, + upb_status_seterrmsg(p->status, "Map bool key not 'true' or 'false'"); - upb_env_reporterror(p->env, &p->status); return false; } multipart_end(p); @@ -1725,8 +1700,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { break; } default: - upb_status_seterrmsg(&p->status, "Invalid field type for map key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid field type for map key"); return false; } @@ -1785,8 +1759,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */ p->top->mapfield = mapfield; if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no value"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no value"); return false; } @@ -1821,8 +1794,7 @@ static bool end_membername(upb_json_parser *p) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "No such field: %.*s\n", (int)len, buf); return false; } } @@ -1848,14 +1820,13 @@ static bool end_any_membername(upb_json_parser *p) { static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { - upb_status s = UPB_STATUS_INIT; upb_selector_t sel; bool ok; const upb_fielddef *mapfield; UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, &s); + upb_sink_endmsg(&p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ @@ -1949,10 +1920,9 @@ static bool start_subobject(upb_json_parser *p) { return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Object specified for non-message/group field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -2060,10 +2030,9 @@ static bool start_array(upb_json_parser *p) { } if (!upb_fielddef_isseq(p->top->f)) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Array specified for non-repeated field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -2122,12 +2091,7 @@ static void start_object(upb_json_parser *p) { static void end_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_status status; - upb_status_clear(&status); - upb_sink_endmsg(&p->top->sink, &status); - if (!upb_ok(&status)) { - upb_env_reporterror(p->env, &status); - } + upb_sink_endmsg(&p->top->sink, p->status); } } @@ -2146,8 +2110,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { if (json_parser_any_frame_has_value(p->top->any_frame) && !json_parser_any_frame_has_type_url(p->top->any_frame)) { - upb_status_seterrmsg(&p->status, "No valid type url"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "No valid type url"); return false; } @@ -2162,8 +2125,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { p->top->any_frame->before_type_url_end - p->top->any_frame->before_type_url_start); if (p->top->any_frame->before_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "invalid data for well known type."); return false; } p->top->any_frame->before_type_url_start++; @@ -2175,8 +2137,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { (ptr + 1) - p->top->any_frame->after_type_url_start); if (p->top->any_frame->after_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "Invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid data for well known type."); return false; } p->top->any_frame->after_type_url_start++; @@ -2249,7 +2210,6 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { /* Deallocate any parse frame. */ json_parser_any_frame_free(p->top->any_frame); - upb_env_free(p->env, p->top->any_frame); return true; } @@ -2419,11 +2379,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2581 "upb/json/parser.rl" +#line 2541 "upb/json/parser.rl" -#line 2427 "upb/json/parser.c" +#line 2387 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2670,7 +2630,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2584 "upb/json/parser.rl" +#line 2544 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2693,7 +2653,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2697 "upb/json/parser.c" +#line 2657 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2768,83 +2728,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2432 "upb/json/parser.rl" +#line 2392 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2434 "upb/json/parser.rl" +#line 2394 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2438 "upb/json/parser.rl" +#line 2398 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2439 "upb/json/parser.rl" +#line 2399 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2445 "upb/json/parser.rl" +#line 2405 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2446 "upb/json/parser.rl" +#line 2406 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2447 "upb/json/parser.rl" +#line 2407 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2453 "upb/json/parser.rl" +#line 2413 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2459 "upb/json/parser.rl" +#line 2419 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2471 "upb/json/parser.rl" +#line 2431 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2472 "upb/json/parser.rl" +#line 2432 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2474 "upb/json/parser.rl" +#line 2434 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2479 "upb/json/parser.rl" +#line 2439 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2480 "upb/json/parser.rl" +#line 2440 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2482 "upb/json/parser.rl" +#line 2442 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2483 "upb/json/parser.rl" +#line 2443 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2485 "upb/json/parser.rl" +#line 2445 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2486 "upb/json/parser.rl" +#line 2446 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2488 "upb/json/parser.rl" +#line 2448 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2493 "upb/json/parser.rl" +#line 2453 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2856,11 +2816,11 @@ _match: } break; case 21: -#line 2504 "upb/json/parser.rl" +#line 2464 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2509 "upb/json/parser.rl" +#line 2469 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2870,11 +2830,11 @@ _match: } break; case 23: -#line 2516 "upb/json/parser.rl" +#line 2476 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2519 "upb/json/parser.rl" +#line 2479 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2884,7 +2844,7 @@ _match: } break; case 25: -#line 2530 "upb/json/parser.rl" +#line 2490 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2894,7 +2854,7 @@ _match: } break; case 26: -#line 2539 "upb/json/parser.rl" +#line 2499 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2904,54 +2864,54 @@ _match: } break; case 27: -#line 2551 "upb/json/parser.rl" +#line 2511 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2555 "upb/json/parser.rl" +#line 2515 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2560 "upb/json/parser.rl" +#line 2520 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2561 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2563 "upb/json/parser.rl" +#line 2523 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2564 "upb/json/parser.rl" +#line 2524 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2566 "upb/json/parser.rl" +#line 2526 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2568 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2570 "upb/json/parser.rl" +#line 2530 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2572 "upb/json/parser.rl" +#line 2532 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2573 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2578 "upb/json/parser.rl" +#line 2538 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2955 "upb/json/parser.c" +#line 2915 "upb/json/parser.c" } } @@ -2968,32 +2928,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2430 "upb/json/parser.rl" +#line 2390 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2561 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2566 "upb/json/parser.rl" +#line 2526 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2568 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2570 "upb/json/parser.rl" +#line 2530 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2573 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2997 "upb/json/parser.c" +#line 2957 "upb/json/parser.c" } } } @@ -3001,11 +2961,10 @@ goto _again;} } _out: {} } -#line 2606 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" if (p != pe) { - upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); - upb_env_reporterror(parser->env, &parser->status); + upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); } else { capture_suspend(parser, &p); } @@ -3049,26 +3008,25 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3053 "upb/json/parser.c" +#line 3012 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2653 "upb/json/parser.rl" +#line 2612 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); p->multipart_state = MULTIPART_INACTIVE; p->capture = NULL; p->accumulated = NULL; - upb_status_clear(&p->status); } static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, const upb_msgdef *md) { upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); @@ -3109,19 +3067,20 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, /* Public API *****************************************************************/ -upb_json_parser *upb_json_parser_create(upb_env *env, +upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); + const size_t size_before = upb_arena_bytesallocated(arena); #endif - upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser)); + upb_json_parser *p = upb_arena_malloc(arena, sizeof(upb_json_parser)); if (!p) return false; - p->env = env; + p->arena = arena; p->method = method; + p->status = NULL; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; @@ -3143,8 +3102,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->ignore_json_unknown = ignore_json_unknown; /* If this fails, uncomment and increase the value in parser.h. */ - /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= + /* fprintf(stderr, "%zd\n", upb_arena_bytesallocated(arena) - size_before); */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= UPB_JSON_PARSER_SIZE); return p; } @@ -3164,8 +3123,8 @@ upb_json_codecache *upb_json_codecache_new() { c = upb_gmalloc(sizeof(*c)); - upb_arena_init(&c->arena); - alloc = upb_arena_alloc(&c->arena); + c->arena = upb_arena_new(); + alloc = upb_arena_alloc(c->arena); upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); @@ -3173,7 +3132,7 @@ upb_json_codecache *upb_json_codecache_new() { } void upb_json_codecache_free(upb_json_codecache *c) { - upb_arena_uninit(&c->arena); + upb_arena_free(c->arena); upb_gfree(c); } @@ -3182,7 +3141,7 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { return upb_value_getconstptr(v); diff --git a/upb/json/parser.h b/upb/json/parser.h index d1a1471..2a06fcf 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -25,14 +25,15 @@ class ParserMethodPtr; struct upb_json_parsermethod; typedef struct upb_json_parsermethod upb_json_parsermethod; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const upb_byteshandler* upb_json_parsermethod_inputhandler( const upb_json_parsermethod* m); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::json::ParserMethodPtr { public: @@ -62,17 +63,19 @@ class upb::json::ParserMethodPtr { struct upb_json_parser; typedef struct upb_json_parser upb_json_parser; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -upb_json_parser* -upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, - const upb_symtab* symtab, upb_sink output, - bool ignore_json_unknown); +upb_json_parser* upb_json_parser_create(upb_arena* a, + const upb_json_parsermethod* m, + const upb_symtab* symtab, + upb_sink output, + bool ignore_json_unknown); upb_bytessink upb_json_parser_input(upb_json_parser* p); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Parses an incoming BytesStream, pushing the results to the destination * sink. */ @@ -80,12 +83,13 @@ class upb::json::ParserPtr { public: ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} - static ParserPtr Create(Environment* env, ParserMethodPtr method, + static ParserPtr Create(Arena* arena, ParserMethodPtr method, SymbolTable* symtab, Sink output, bool ignore_json_unknown) { upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; - return ParserPtr(upb_json_parser_create( - env, method.ptr(), symtab_ptr, output.sink(), ignore_json_unknown)); + return ParserPtr(upb_json_parser_create(arena->ptr(), method.ptr(), + symtab_ptr, output.sink(), + ignore_json_unknown)); } BytesSink input() { return upb_json_parser_input(ptr_); } @@ -101,16 +105,17 @@ class upb::json::ParserPtr { struct upb_json_codecache; typedef struct upb_json_codecache upb_json_codecache; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_json_codecache *upb_json_codecache_new(); void upb_json_codecache_free(upb_json_codecache *cache); const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, const upb_msgdef* md); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::json::CodeCache { public: diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 05a9505..a117d0c 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -210,7 +210,7 @@ typedef struct { } upb_jsonparser_frame; struct upb_json_parser { - upb_env *env; + upb_arena *arena; const upb_json_parsermethod *method; upb_bytessink input_; @@ -219,7 +219,7 @@ struct upb_json_parser { upb_jsonparser_frame *top; upb_jsonparser_frame *limit; - upb_status status; + upb_status *status; /* Ragel's internal parsing stack for the parsing state machine. */ int current_state; @@ -257,7 +257,7 @@ struct upb_json_parser { }; struct upb_json_codecache { - upb_arena arena; + upb_arena *arena; upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ }; @@ -275,7 +275,7 @@ static upb_jsonparser_any_frame *json_parser_any_frame_new( upb_json_parser *p) { upb_jsonparser_any_frame *frame; - frame = upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + frame = upb_arena_malloc(p->arena, sizeof(upb_jsonparser_any_frame)); frame->encoder_handlercache = upb_pb_encoder_newcache(); frame->parser_codecache = upb_json_codecache_new(); @@ -299,12 +299,12 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->arena, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, + frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, p->ignore_json_unknown); } @@ -370,8 +370,7 @@ static upb_selector_t parser_getsel(upb_json_parser *p) { static bool check_stack(upb_json_parser *p) { if ((p->top + 1) == p->limit) { - upb_status_seterrmsg(&p->status, "Nesting too deep"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Nesting too deep"); return false; } @@ -466,10 +465,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, char output[3]; if (limit - ptr < 4) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Base64 input for bytes field not a multiple of 4: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -493,10 +491,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, otherchar: if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) || nonbase64(ptr[3]) ) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Non-base64 characters in bytes field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } if (ptr[2] == '=') { uint32_t val; @@ -534,11 +531,10 @@ otherchar: } badpadding: - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Incorrect base64 padding for field: %s (%.*s)", upb_fielddef_name(p->top->f), 4, ptr); - upb_env_reporterror(p->env, &p->status); return false; } @@ -582,10 +578,9 @@ static bool accumulate_realloc(upb_json_parser *p, size_t need) { new_size = saturating_multiply(new_size, 2); } - mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size); + mem = upb_arena_realloc(p->arena, p->accumulate_buf, old_size, new_size); if (!mem) { - upb_status_seterrmsg(&p->status, "Out of memory allocating buffer."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Out of memory allocating buffer."); return false; } @@ -608,8 +603,7 @@ static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, } if (!checked_add(p->accumulated_len, len, &need)) { - upb_status_seterrmsg(&p->status, "Integer overflow."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Integer overflow."); return false; } @@ -687,8 +681,7 @@ static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, switch (p->multipart_state) { case MULTIPART_INACTIVE: upb_status_seterrmsg( - &p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); - upb_env_reporterror(p->env, &p->status); + p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); return false; case MULTIPART_ACCUMULATE: @@ -1053,8 +1046,7 @@ static bool parse_number(upb_json_parser *p, bool is_quoted) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "error parsing number: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing number: %s", buf); multipart_end(p); return false; } @@ -1068,10 +1060,9 @@ static bool parser_putbool(upb_json_parser *p, bool val) { } if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Boolean value specified for non-bool field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1246,10 +1237,9 @@ static bool start_stringval(upb_json_parser *p) { multipart_startaccum(p); return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "String specified for bool or submessage field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -1282,8 +1272,7 @@ static bool end_any_stringval(upb_json_parser *p) { payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); if (payload_type == NULL) { upb_status_seterrf( - &p->status, "Cannot find packed type: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Cannot find packed type: %.*s\n", (int)len, buf); return false; } @@ -1292,8 +1281,7 @@ static bool end_any_stringval(upb_json_parser *p) { return true; } else { upb_status_seterrf( - &p->status, "Invalid type url: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Invalid type url: %.*s\n", (int)len, buf); return false; } } @@ -1345,8 +1333,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { upb_selector_t sel = parser_getsel(p); upb_sink_putint32(&p->top->sink, sel, int_val); } else { - upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); } break; @@ -1363,8 +1350,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { default: UPB_ASSERT(false); - upb_status_seterrmsg(&p->status, "Internal error in JSON decoder"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Internal error in JSON decoder"); ok = false; break; } @@ -1443,25 +1429,22 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(seconds_buf, buf, fraction_start); seconds = strtol(seconds_buf, &end, 10); if (errno == ERANGE || end != seconds_buf + fraction_start) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", seconds_buf); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds > 315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "maximum acceptable value is " "315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds < -315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "minimum acceptable value is " "-315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1470,9 +1453,8 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start); val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1498,7 +1480,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); end_member(p); - /* Continue previous environment */ + /* Continue previous arena */ multipart_startaccum(p); return true; @@ -1528,8 +1510,7 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { /* Parse seconds */ if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { - upb_status_seterrf(&p->status, "error parsing timestamp: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp: %s", buf); return false; } @@ -1562,9 +1543,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { buf = accumulate_getptr(p, &len); if (len > 10) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "error parsing timestamp: at most 9-digit fraction."); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1574,9 +1554,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len + 1) { - upb_status_seterrf(&p->status, "error parsing timestamp nanos: %s", + upb_status_seterrf(p->status, "error parsing timestamp nanos: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1618,8 +1597,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { if (buf[0] != 'Z') { if (sscanf(buf + 1, "%2d:00", &hours) != 1) { - upb_status_seterrf(&p->status, "error parsing timestamp offset"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp offset"); return false; } @@ -1635,10 +1613,9 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { /* Check timestamp boundary */ if (seconds < -62135596800) { - upb_status_seterrf(&p->status, "error parsing timestamp: " + upb_status_seterrf(p->status, "error parsing timestamp: " "minimum acceptable value is " "0001-01-01T00:00:00Z"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1679,8 +1656,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY); if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no key"); return false; } switch (upb_fielddef_type(p->top->f)) { @@ -1703,9 +1679,8 @@ static bool parse_mapentry_key(upb_json_parser *p) { return false; } } else { - upb_status_seterrmsg(&p->status, + upb_status_seterrmsg(p->status, "Map bool key not 'true' or 'false'"); - upb_env_reporterror(p->env, &p->status); return false; } multipart_end(p); @@ -1723,8 +1698,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { break; } default: - upb_status_seterrmsg(&p->status, "Invalid field type for map key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid field type for map key"); return false; } @@ -1783,8 +1757,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */ p->top->mapfield = mapfield; if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no value"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no value"); return false; } @@ -1819,8 +1792,7 @@ static bool end_membername(upb_json_parser *p) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "No such field: %.*s\n", (int)len, buf); return false; } } @@ -1846,14 +1818,13 @@ static bool end_any_membername(upb_json_parser *p) { static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { - upb_status s = UPB_STATUS_INIT; upb_selector_t sel; bool ok; const upb_fielddef *mapfield; UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, &s); + upb_sink_endmsg(&p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ @@ -1947,10 +1918,9 @@ static bool start_subobject(upb_json_parser *p) { return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Object specified for non-message/group field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -2058,10 +2028,9 @@ static bool start_array(upb_json_parser *p) { } if (!upb_fielddef_isseq(p->top->f)) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Array specified for non-repeated field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -2120,12 +2089,7 @@ static void start_object(upb_json_parser *p) { static void end_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_status status; - upb_status_clear(&status); - upb_sink_endmsg(&p->top->sink, &status); - if (!upb_ok(&status)) { - upb_env_reporterror(p->env, &status); - } + upb_sink_endmsg(&p->top->sink, p->status); } } @@ -2144,8 +2108,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { if (json_parser_any_frame_has_value(p->top->any_frame) && !json_parser_any_frame_has_type_url(p->top->any_frame)) { - upb_status_seterrmsg(&p->status, "No valid type url"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "No valid type url"); return false; } @@ -2160,8 +2123,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { p->top->any_frame->before_type_url_end - p->top->any_frame->before_type_url_start); if (p->top->any_frame->before_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "invalid data for well known type."); return false; } p->top->any_frame->before_type_url_start++; @@ -2173,8 +2135,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { (ptr + 1) - p->top->any_frame->after_type_url_start); if (p->top->any_frame->after_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "Invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid data for well known type."); return false; } p->top->any_frame->after_type_url_start++; @@ -2247,7 +2208,6 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { /* Deallocate any parse frame. */ json_parser_any_frame_free(p->top->any_frame); - upb_env_free(p->env, p->top->any_frame); return true; } @@ -2605,8 +2565,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, %% write exec; if (p != pe) { - upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); - upb_env_reporterror(parser->env, &parser->status); + upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); } else { capture_suspend(parser, &p); } @@ -2656,13 +2615,12 @@ static void json_parser_reset(upb_json_parser *p) { p->multipart_state = MULTIPART_INACTIVE; p->capture = NULL; p->accumulated = NULL; - upb_status_clear(&p->status); } static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, const upb_msgdef *md) { upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); @@ -2703,19 +2661,20 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, /* Public API *****************************************************************/ -upb_json_parser *upb_json_parser_create(upb_env *env, +upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); + const size_t size_before = upb_arena_bytesallocated(arena); #endif - upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser)); + upb_json_parser *p = upb_arena_malloc(arena, sizeof(upb_json_parser)); if (!p) return false; - p->env = env; + p->arena = arena; p->method = method; + p->status = NULL; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; @@ -2737,8 +2696,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->ignore_json_unknown = ignore_json_unknown; /* If this fails, uncomment and increase the value in parser.h. */ - /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= + /* fprintf(stderr, "%zd\n", upb_arena_bytesallocated(arena) - size_before); */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= UPB_JSON_PARSER_SIZE); return p; } @@ -2758,8 +2717,8 @@ upb_json_codecache *upb_json_codecache_new() { c = upb_gmalloc(sizeof(*c)); - upb_arena_init(&c->arena); - alloc = upb_arena_alloc(&c->arena); + c->arena = upb_arena_new(); + alloc = upb_arena_alloc(c->arena); upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); @@ -2767,7 +2726,7 @@ upb_json_codecache *upb_json_codecache_new() { } void upb_json_codecache_free(upb_json_codecache *c) { - upb_arena_uninit(&c->arena); + upb_arena_free(c->arena); upb_gfree(c); } @@ -2776,7 +2735,7 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { return upb_value_getconstptr(v); diff --git a/upb/json/printer.c b/upb/json/printer.c index 83f1a58..bc18055 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -1252,13 +1252,13 @@ static void json_printer_reset(upb_json_printer *p) { /* Public API *****************************************************************/ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, upb_bytessink output) { #ifndef NDEBUG - size_t size_before = upb_env_bytesallocated(e); + size_t size_before = upb_arena_bytesallocated(a); #endif - upb_json_printer *p = upb_env_malloc(e, sizeof(upb_json_printer)); + upb_json_printer *p = upb_arena_malloc(a, sizeof(upb_json_printer)); if (!p) return NULL; p->output_ = output; @@ -1268,7 +1268,7 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, p->nanos = 0; /* If this fails, increase the value in printer.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <= UPB_JSON_PRINTER_SIZE); return p; } diff --git a/upb/json/printer.h b/upb/json/printer.h index a7a37bb..857ae47 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -24,10 +24,12 @@ class PrinterPtr; struct upb_json_printer; typedef struct upb_json_printer upb_json_printer; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif /* Native C API. */ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, upb_bytessink output); upb_sink upb_json_printer_input(upb_json_printer *p); const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, @@ -36,18 +38,18 @@ const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Prints an incoming stream of data to a BytesSink in JSON format. */ class upb::json::PrinterPtr { public: PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {} - static PrinterPtr Create(Environment *env, const upb::Handlers *handlers, + static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers, BytesSink output) { - return PrinterPtr(upb_json_printer_create(env, handlers, output.sink())); + return PrinterPtr( + upb_json_printer_create(arena->ptr(), handlers, output.sink())); } /* The input to the printer. */ diff --git a/upb/msg.h b/upb/msg.h index 4529478..149b7ab 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -37,18 +37,22 @@ class MessageLayout; #endif -UPB_DECLARE_TYPE(upb::Map, upb_map) -UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter) - -struct upb_array; -typedef struct upb_array upb_array; - /* TODO(haberman): C++ accessors */ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif typedef void upb_msg; +struct upb_array; +typedef struct upb_array upb_array; + +struct upb_map; +typedef struct upb_map upb_map; + +struct upb_mapiter; +typedef struct upb_mapiter upb_mapiter; /** upb_msglayout *************************************************************/ @@ -75,7 +79,6 @@ typedef struct upb_msglayout { bool extendable; } upb_msglayout; - /** upb_stringview ************************************************************/ typedef struct { @@ -103,7 +106,6 @@ UPB_INLINE bool upb_stringview_eql(upb_stringview a, upb_stringview b) { #define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} - /** upb_msgval ****************************************************************/ /* A union representing all possible protobuf values. Used for generic get/set @@ -156,7 +158,6 @@ UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) { return upb_msgval_str(upb_stringview_make(data, size)); } - /** upb_msg *******************************************************************/ /* A upb_msg represents a protobuf message. It always corresponds to a specific @@ -216,7 +217,6 @@ bool upb_msg_clearfield(upb_msg *msg, /* TODO(haberman): copyfrom()/mergefrom()? */ - /** upb_array *****************************************************************/ /* A upb_array stores data for a repeated field. The memory management @@ -236,7 +236,6 @@ upb_msgval upb_array_get(const upb_array *arr, size_t i); bool upb_array_set(upb_array *arr, size_t i, upb_msgval val); - /** upb_map *******************************************************************/ /* A upb_map stores data for a map field. The memory management semantics are @@ -268,7 +267,6 @@ bool upb_map_set(upb_map *map, /* Deletes an entry in the map. Returns true if the key was present. */ bool upb_map_del(upb_map *map, upb_msgval key); - /** upb_mapiter ***************************************************************/ /* For iterating over a map. Map iterators are invalidated by mutations to the @@ -290,6 +288,8 @@ upb_msgval upb_mapiter_value(const upb_mapiter *i); void upb_mapiter_setdone(upb_mapiter *i); bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2); -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_MSG_H_ */ diff --git a/upb/msgfactory.h b/upb/msgfactory.h index 73a26ba..9b3b599 100644 --- a/upb/msgfactory.h +++ b/upb/msgfactory.h @@ -5,10 +5,15 @@ #ifndef UPB_MSGFACTORY_H_ #define UPB_MSGFACTORY_H_ -UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory) - /** upb_msgfactory ************************************************************/ +struct upb_msgfactory; +typedef struct upb_msgfactory upb_msgfactory; + +#ifdef __cplusplus +extern "C" { +#endif + /* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and * upb_visitorplan objects. These are the objects necessary to represent, * populate, and and visit upb_msg objects. @@ -36,5 +41,8 @@ const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f); const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, const upb_msgdef *m); +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_MSGFACTORY_H_ */ diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index e17ca03..ca497ed 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -907,7 +907,7 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) { c->allow_jit = true; c->lazy = false; - upb_arena_init(&c->arena); + c->arena = upb_arena_new(); if (!upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR)) return NULL; return c; diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index cd64f72..5068225 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -99,9 +99,7 @@ static bool in_residual_buf(const upb_pbdecoder *d, const char *p); * benchmarks. */ static void seterr(upb_pbdecoder *d, const char *msg) { - upb_status status = UPB_STATUS_INIT; - upb_status_seterrmsg(&status, msg); - upb_env_reporterror(d->env, &status); + upb_status_seterrmsg(d->status, msg); } void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) { @@ -992,24 +990,24 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) { d->residual_end = d->residual; } -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, +upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, upb_sink sink) { const size_t default_max_nesting = 64; #ifndef NDEBUG - size_t size_before = upb_env_bytesallocated(e); + size_t size_before = upb_arena_bytesallocated(a); #endif - upb_pbdecoder *d = upb_env_malloc(e, sizeof(upb_pbdecoder)); + upb_pbdecoder *d = upb_arena_malloc(a, sizeof(upb_pbdecoder)); if (!d) return NULL; d->method_ = m; - d->callstack = upb_env_malloc(e, callstacksize(d, default_max_nesting)); - d->stack = upb_env_malloc(e, stacksize(d, default_max_nesting)); + d->callstack = upb_arena_malloc(a, callstacksize(d, default_max_nesting)); + d->stack = upb_arena_malloc(a, stacksize(d, default_max_nesting)); if (!d->stack || !d->callstack) { return NULL; } - d->env = e; + d->arena = a; d->limit = d->stack + default_max_nesting - 1; d->stack_size = default_max_nesting; d->status = NULL; @@ -1024,7 +1022,7 @@ upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, d->top->sink = sink; /* If this fails, increase the value in decoder.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <= UPB_PB_DECODER_SIZE); return d; } @@ -1057,7 +1055,7 @@ bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { /* Need to reallocate stack and callstack to accommodate. */ size_t old_size = stacksize(d, d->stack_size); size_t new_size = stacksize(d, max); - void *p = upb_env_realloc(d->env, d->stack, old_size, new_size); + void *p = upb_arena_realloc(d->arena, d->stack, old_size, new_size); if (!p) { return false; } @@ -1065,7 +1063,7 @@ bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { old_size = callstacksize(d, d->stack_size); new_size = callstacksize(d, max); - p = upb_env_realloc(d->env, d->callstack, old_size, new_size); + p = upb_arena_realloc(d->arena, d->callstack, old_size, new_size); if (!p) { return false; } diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index ba381f3..6fcef03 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -40,7 +40,9 @@ class DecoderMethodOptions; struct upb_pbdecodermethod; typedef struct upb_pbdecodermethod upb_pbdecodermethod; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const upb_handlers *upb_pbdecodermethod_desthandlers( const upb_pbdecodermethod *m); @@ -48,9 +50,8 @@ const upb_byteshandler *upb_pbdecodermethod_inputhandler( const upb_pbdecodermethod *m); bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Represents the code to parse a protobuf according to a destination * Handlers. */ @@ -95,9 +96,11 @@ class upb::pb::DecoderMethodPtr { struct upb_pbdecoder; typedef struct upb_pbdecoder upb_pbdecoder; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, +upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena, const upb_pbdecodermethod *method, upb_sink output); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); @@ -107,9 +110,8 @@ size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); void upb_pbdecoder_reset(upb_pbdecoder *d); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A Decoder receives binary protobuf data on its input sink and pushes the * decoded data to its output sink. */ @@ -124,9 +126,10 @@ class upb::pb::DecoderPtr { * must also outlive this decoder. * * The sink must match the given method. */ - static DecoderPtr Create(Environment *env, DecoderMethodPtr method, + static DecoderPtr Create(Arena *arena, DecoderMethodPtr method, upb::Sink output) { - return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output.sink())); + return DecoderPtr( + upb_pbdecoder_create(arena->ptr(), method.ptr(), output.sink())); } /* Returns the DecoderMethod this decoder is parsing from. */ @@ -171,7 +174,9 @@ class upb::pb::DecoderPtr { struct upb_pbcodecache; typedef struct upb_pbcodecache upb_pbcodecache; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest); void upb_pbcodecache_free(upb_pbcodecache *c); @@ -181,9 +186,8 @@ void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy); const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, const upb_msgdef *md); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A class for caching protobuf processing code, whether bytecode for the * interpreted decoder or machine code for the JIT. diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h index 8d464fa..47eb3ed 100644 --- a/upb/pb/decoder.int.h +++ b/upb/pb/decoder.int.h @@ -73,7 +73,7 @@ typedef enum { UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } struct upb_pbcodecache { - upb_arena arena; + upb_arena *arena; upb_handlercache *dest; bool allow_jit; bool lazy; @@ -169,7 +169,7 @@ struct upb_pbdecodermethod { }; struct upb_pbdecoder { - upb_env *env; + upb_arena *arena; /* Our input sink. */ upb_bytessink input_; diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 1496eba..722cc5b 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -91,7 +91,7 @@ typedef struct { } upb_pb_encoder_segment; struct upb_pb_encoder { - upb_env *env; + upb_arena *arena; /* Our input and output. */ upb_sink input_; @@ -150,7 +150,7 @@ static bool reserve(upb_pb_encoder *e, size_t bytes) { new_size *= 2; } - new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size); + new_buf = upb_arena_realloc(e->arena, e->buf, old_size, new_size); if (new_buf == NULL) { return false; @@ -230,7 +230,7 @@ static bool start_delim(upb_pb_encoder *e) { (e->seglimit - e->segbuf) * sizeof(upb_pb_encoder_segment); size_t new_size = old_size * 2; upb_pb_encoder_segment *new_buf = - upb_env_realloc(e->env, e->segbuf, old_size, new_size); + upb_arena_realloc(e->arena, e->segbuf, old_size, new_size); if (new_buf == NULL) { return false; @@ -526,22 +526,22 @@ upb_handlercache *upb_pb_encoder_newcache() { return upb_handlercache_new(newhandlers_callback, NULL); } -upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, +upb_pb_encoder *upb_pb_encoder_create(upb_arena *arena, const upb_handlers *h, upb_bytessink output) { const size_t initial_bufsize = 256; const size_t initial_segbufsize = 16; /* TODO(haberman): make this configurable. */ const size_t stack_size = 64; #ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); + const size_t size_before = upb_arena_bytesallocated(arena); #endif - upb_pb_encoder *e = upb_env_malloc(env, sizeof(upb_pb_encoder)); + upb_pb_encoder *e = upb_arena_malloc(arena, sizeof(upb_pb_encoder)); if (!e) return NULL; - e->buf = upb_env_malloc(env, initial_bufsize); - e->segbuf = upb_env_malloc(env, initial_segbufsize * sizeof(*e->segbuf)); - e->stack = upb_env_malloc(env, stack_size * sizeof(*e->stack)); + e->buf = upb_arena_malloc(arena, initial_bufsize); + e->segbuf = upb_arena_malloc(arena, initial_segbufsize * sizeof(*e->segbuf)); + e->stack = upb_arena_malloc(arena, stack_size * sizeof(*e->stack)); if (!e->buf || !e->segbuf || !e->stack) { return NULL; @@ -554,13 +554,13 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, upb_pb_encoder_reset(e); upb_sink_reset(&e->input_, h, e); - e->env = env; + e->arena = arena; e->output_ = output; e->subc = output.closure; e->ptr = e->buf; /* If this fails, increase the value in encoder.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= UPB_PB_ENCODER_SIZE); return e; } diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 7aa2870..780f60f 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -35,17 +35,18 @@ class EncoderPtr; struct upb_pb_encoder; typedef struct upb_pb_encoder upb_pb_encoder; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_sink upb_pb_encoder_input(upb_pb_encoder *p); -upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, +upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h, upb_bytessink output); upb_handlercache *upb_pb_encoder_newcache(); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" { */ class upb::pb::EncoderPtr { public: @@ -55,9 +56,10 @@ class upb::pb::EncoderPtr { /* Creates a new encoder in the given environment. The Handlers must have * come from NewHandlers() below. */ - static EncoderPtr Create(Environment* env, const Handlers* handlers, + static EncoderPtr Create(Arena* arena, const Handlers* handlers, BytesSink output) { - return EncoderPtr(upb_pb_encoder_create(env, handlers, output.sink())); + return EncoderPtr( + upb_pb_encoder_create(arena->ptr(), handlers, output.sink())); } /* The input to the encoder. */ diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index d1d539d..91d0d55 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -18,7 +18,7 @@ struct upb_textprinter { upb_sink input_; - upb_bytessink *output_; + upb_bytessink output_; int indent_depth_; bool single_line_; void *subc; @@ -35,13 +35,13 @@ static int indent(upb_textprinter *p) { int i; if (!p->single_line_) for (i = 0; i < p->indent_depth_; i++) - upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, " ", 2, NULL); return 0; } static int endfield(upb_textprinter *p) { const char ch = (p->single_line_ ? ' ' : '\n'); - upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, &ch, 1, NULL); return 0; } @@ -60,7 +60,7 @@ static int putescaped(upb_textprinter *p, const char *buf, size_t len, bool is_hex_escape; if (dstend - dst < 4) { - upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, dstbuf, dst - dstbuf, NULL); dst = dstbuf; } @@ -88,7 +88,7 @@ static int putescaped(upb_textprinter *p, const char *buf, size_t len, last_hex_escape = is_hex_escape; } /* Flush remaining data. */ - upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, dstbuf, dst - dstbuf, NULL); return 0; } @@ -114,7 +114,7 @@ bool putf(upb_textprinter *p, const char *fmt, ...) { va_end(args); UPB_ASSERT(written == len); - ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL); + ok = upb_bytessink_putbuf(&p->output_, p->subc, str, len, NULL); upb_gfree(str); return ok; } @@ -126,7 +126,7 @@ static bool textprinter_startmsg(void *c, const void *hd) { upb_textprinter *p = c; UPB_UNUSED(hd); if (p->indent_depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc); + upb_bytessink_start(&p->output_, 0, &p->subc); } return true; } @@ -136,7 +136,7 @@ static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) { UPB_UNUSED(hd); UPB_UNUSED(s); if (p->indent_depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } return true; } @@ -241,7 +241,7 @@ static bool textprinter_endsubmsg(void *closure, const void *handler_data) { UPB_UNUSED(handler_data); p->indent_depth_--; CHECK(indent(p)); - upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, "}", 1, NULL); CHECK(endfield(p)); return true; err: @@ -315,9 +315,9 @@ static void textprinter_reset(upb_textprinter *p, bool single_line) { /* Public API *****************************************************************/ -upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output) { - upb_textprinter *p = upb_env_malloc(env, sizeof(upb_textprinter)); +upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output) { + upb_textprinter *p = upb_arena_malloc(arena, sizeof(upb_textprinter)); if (!p) return NULL; p->output_ = output; @@ -331,7 +331,7 @@ upb_handlercache *upb_textprinter_newcache() { return upb_handlercache_new(&onmreg, NULL); } -upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; } +upb_sink upb_textprinter_input(upb_textprinter *p) { return p->input_; } void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) { p->single_line_ = single_line; diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index 06ff7d5..e59e11d 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -17,60 +17,51 @@ class TextPrinter; } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter) +/* upb_textprinter ************************************************************/ + +struct upb_textprinter; +typedef struct upb_textprinter upb_textprinter; #ifdef __cplusplus +extern "C" { +#endif -class upb::pb::TextPrinter { +/* C API. */ +upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output); +void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); +upb_sink upb_textprinter_input(upb_textprinter *p); +upb_handlercache *upb_textprinter_newcache(); + +#ifdef __cplusplus +} /* extern "C" */ + +class upb::pb::TextPrinterPtr { public: + TextPrinterPtr(upb_textprinter* ptr) : ptr_(ptr) {} + /* The given handlers must have come from NewHandlers(). It must outlive the * TextPrinter. */ - static TextPrinter *Create(Environment *env, const upb::Handlers *handlers, - BytesSink *output); + static TextPrinterPtr *Create(Arena *arena, const upb::Handlers *handlers, + BytesSink output) { + return TextPrinterPtr(upb_textprinter_create(arena, handlers, output)); + } - void SetSingleLineMode(bool single_line); + void SetSingleLineMode(bool single_line) { + upb_textprinter_setsingleline(ptr_, single_line); + } - Sink* input(); + Sink input() { return upb_textprinter_input(ptr_); } /* If handler caching becomes a requirement we can add a code cache as in * decoder.h */ - static HandlerCache* NewCache(); -}; - -#endif - -UPB_BEGIN_EXTERN_C + static HandlerCache NewCache() { + return HandlerCache(upb_textprinter_newcache()); + } -/* C API. */ -upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output); -void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); -upb_sink *upb_textprinter_input(upb_textprinter *p); - -upb_handlercache *upb_textprinter_newcache(); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upb { -namespace pb { -inline TextPrinter *TextPrinter::Create(Environment *env, - const upb::Handlers *handlers, - BytesSink *output) { - return upb_textprinter_create(env, handlers, output); -} -inline void TextPrinter::SetSingleLineMode(bool single_line) { - upb_textprinter_setsingleline(this, single_line); -} -inline Sink* TextPrinter::input() { - return upb_textprinter_input(this); -} -inline HandlerCache* TextPrinter::NewCache() { - return upb_textprinter_newcache(); -} -} /* namespace pb */ -} /* namespace upb */ + private: + upb_textprinter* ptr_; +}; #endif diff --git a/upb/sink.c b/upb/sink.c index 6ef5718..7a7eeb4 100644 --- a/upb/sink.c +++ b/upb/sink.c @@ -15,73 +15,3 @@ bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) { } return ret; } - -struct upb_bufsink { - upb_byteshandler handler; - upb_bytessink sink; - upb_env *env; - char *ptr; - size_t len, size; -}; - -static void *upb_bufsink_start(void *_sink, const void *hd, size_t size_hint) { - upb_bufsink *sink = _sink; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - sink->len = 0; - return sink; -} - -static size_t upb_bufsink_string(void *_sink, const void *hd, const char *ptr, - size_t len, const upb_bufhandle *handle) { - upb_bufsink *sink = _sink; - size_t new_size = sink->size; - - UPB_ASSERT(new_size > 0); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - while (sink->len + len > new_size) { - new_size *= 2; - } - - if (new_size != sink->size) { - sink->ptr = upb_env_realloc(sink->env, sink->ptr, sink->size, new_size); - sink->size = new_size; - } - - memcpy(sink->ptr + sink->len, ptr, len); - sink->len += len; - - return len; -} - -upb_bufsink *upb_bufsink_new(upb_env *env) { - upb_bufsink *sink = upb_env_malloc(env, sizeof(upb_bufsink)); - upb_byteshandler_init(&sink->handler); - upb_byteshandler_setstartstr(&sink->handler, upb_bufsink_start, NULL); - upb_byteshandler_setstring(&sink->handler, upb_bufsink_string, NULL); - - upb_bytessink_reset(&sink->sink, &sink->handler, sink); - - sink->env = env; - sink->size = 32; - sink->ptr = upb_env_malloc(env, sink->size); - sink->len = 0; - - return sink; -} - -void upb_bufsink_free(upb_bufsink *sink) { - upb_env_free(sink->env, sink->ptr); - upb_env_free(sink->env, sink); -} - -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink) { - return &sink->sink; -} - -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len) { - *len = sink->len; - return sink->ptr; -} diff --git a/upb/sink.h b/upb/sink.h index 1855542..d93d966 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -29,7 +29,9 @@ class Sink; /* upb_sink *******************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif typedef struct { const upb_handlers *handlers; @@ -192,9 +194,8 @@ UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { return endsubmsg(s->closure, hd); } -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A upb::Sink is an object that binds a upb::Handlers object to some runtime * state. It represents an endpoint to which data can be sent. @@ -288,7 +289,9 @@ class upb::Sink { * sink->EndMessage(&status); * sink->EndSubMessage(endsubmsg_selector); */ bool StartMessage() { return upb_sink_startmsg(&sink_); } - bool EndMessage(Status* status) { return upb_sink_endmsg(&sink_, status); } + bool EndMessage(upb_status *status) { + return upb_sink_endmsg(&sink_, status); + } /* Putting of individual values. These work for both repeated and * non-repeated fields, but for repeated fields you must wrap them in @@ -489,13 +492,14 @@ class upb::BytesSink { /* upb_bufsrc *****************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ namespace upb { template bool PutBuffer(const T& str, BytesSink sink) { @@ -505,19 +509,4 @@ template bool PutBuffer(const T& str, BytesSink sink) { #endif /* __cplusplus */ -/* upb_bufsink ****************************************************************/ - -/* A class for accumulating output string data in a flat buffer. */ -struct upb_bufsink; -typedef struct upb_bufsink upb_bufsink; - -UPB_BEGIN_EXTERN_C - -upb_bufsink *upb_bufsink_init(upb_env *env); -void upb_bufsink_free(upb_bufsink *sink); -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink); -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len); - -UPB_END_EXTERN_C - #endif diff --git a/upb/table.int.h b/upb/table.int.h index 63dce59..2b86390 100644 --- a/upb/table.int.h +++ b/upb/table.int.h @@ -156,21 +156,6 @@ UPB_INLINE upb_value upb_value_double(double cval) { * initializing a non-first union member. */ typedef uintptr_t upb_tabkey; -#define UPB_TABKEY_NUM(n) n -#define UPB_TABKEY_NONE 0 -/* The preprocessor isn't quite powerful enough to turn the compile-time string - * length into a byte-wise string representation, so code generation needs to - * help it along. - * - * "len1" is the low byte and len4 is the high byte. */ -#ifdef UPB_BIG_ENDIAN -#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ - (uintptr_t)(len4 len3 len2 len1 strval) -#else -#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ - (uintptr_t)(len1 len2 len3 len4 strval) -#endif - UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) { char* mem = (char*)key; if (len) memcpy(len, mem, sizeof(*len)); diff --git a/upb/upb.c b/upb/upb.c index 68e26b0..f56f6c4 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -8,12 +8,6 @@ #include #include "upb/upb.h" -bool upb_dumptostderr(void *closure, const upb_status* status) { - UPB_UNUSED(closure); - fprintf(stderr, "%s\n", upb_status_errmsg(status)); - return false; -} - /* Guarantee null-termination and provide ellipsis truncation. * It may be tempting to "optimize" this by initializing these final * four bytes up-front and then being careful never to overwrite them, @@ -29,24 +23,17 @@ static void nullz(upb_status *status) { void upb_status_clear(upb_status *status) { if (!status) return; - status->ok_ = true; - status->code_ = 0; + status->ok = true; status->msg[0] = '\0'; } -bool upb_ok(const upb_status *status) { return status->ok_; } - -upb_errorspace *upb_status_errspace(const upb_status *status) { - return status->error_space_; -} - -int upb_status_errcode(const upb_status *status) { return status->code_; } +bool upb_ok(const upb_status *status) { return status->ok; } const char *upb_status_errmsg(const upb_status *status) { return status->msg; } void upb_status_seterrmsg(upb_status *status, const char *msg) { if (!status) return; - status->ok_ = false; + status->ok = false; strncpy(status->msg, msg, sizeof(status->msg)); nullz(status); } @@ -60,17 +47,11 @@ void upb_status_seterrf(upb_status *status, const char *fmt, ...) { void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) { if (!status) return; - status->ok_ = false; + status->ok = false; _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args); nullz(status); } -void upb_status_copy(upb_status *to, const upb_status *from) { - if (!to) return; - *to = *from; -} - - /* upb_alloc ******************************************************************/ static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize, @@ -87,7 +68,6 @@ static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize, upb_alloc upb_alloc_global = {&upb_global_allocfunc}; - /* upb_arena ******************************************************************/ /* Be conservative and choose 16 in case anyone is using SSE. */ @@ -115,11 +95,7 @@ struct upb_arena { /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ void *cleanup_head; - - /* For future expansion, since the size of this struct is exposed to users. */ - void *future1; - void *future2; -} upb_arena; +}; typedef struct mem_block { struct mem_block *next; @@ -149,7 +125,6 @@ static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size, /* TODO(haberman): ASAN poison. */ } - static mem_block *upb_arena_allocblock(upb_arena *a, size_t size) { size_t block_size = UPB_MAX(size, a->next_block_size) + sizeof(mem_block); mem_block *block = upb_malloc(a->block_alloc, block_size); @@ -202,7 +177,25 @@ static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize, /* Public Arena API ***********************************************************/ -void upb_arena_init(upb_arena *a) { +upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { + const size_t first_block_overhead = sizeof(upb_arena) + sizeof(mem_block); + upb_arena *a; + bool owned = false; + + if (n < first_block_overhead) { + /* We need to malloc the initial block. */ + n = first_block_overhead + 256; + owned = true; + if (!alloc || !(mem = upb_malloc(alloc, n))) { + return NULL; + } + } + + a = mem; + mem = (char*)mem + sizeof(*a); + n -= sizeof(*a); + upb_arena_addblock(a, mem, n, owned); + a->alloc.func = &upb_arena_doalloc; a->block_alloc = &upb_alloc_global; a->bytes_allocated = 0; @@ -210,21 +203,12 @@ void upb_arena_init(upb_arena *a) { a->max_block_size = 16384; a->cleanup_head = NULL; a->block_head = NULL; -} - -void upb_arena_init2(upb_arena *a, void *mem, size_t size, upb_alloc *alloc) { - upb_arena_init(a); + a->block_alloc = alloc; - if (size > sizeof(mem_block)) { - upb_arena_addblock(a, mem, size, false); - } - - if (alloc) { - a->block_alloc = alloc; - } + return a; } -void upb_arena_uninit(upb_arena *a) { +void upb_arena_free(upb_arena *a) { cleanup_ent *ent = a->cleanup_head; mem_block *block = a->block_head; @@ -236,6 +220,7 @@ void upb_arena_uninit(upb_arena *a) { /* Must do this after running cleanup functions, because this will delete * the memory we store our cleanup entries in! */ while (block) { + /* Load first since we are deleting block. */ mem_block *next = block->next; if (block->owned) { @@ -244,10 +229,6 @@ void upb_arena_uninit(upb_arena *a) { block = next; } - - /* Protect against multiple-uninit. */ - a->cleanup_head = NULL; - a->block_head = NULL; } bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) { diff --git a/upb/upb.h b/upb/upb.h index 4384812..07b0455 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -15,14 +15,11 @@ #include #ifdef __cplusplus +#include namespace upb { -class Allocator; class Arena; -class Environment; -class ErrorSpace; class Status; template class InlinedArena; -template class InlinedEnvironment; } #endif @@ -74,46 +71,14 @@ template class InlinedEnvironment; #error Need implementations of [v]snprintf and va_copy #endif -#if (defined(__cplusplus) && __cplusplus >= 201103L) || \ - defined(__GXX_EXPERIMENTAL_CXX0X__) || \ +#ifdef __cplusplus +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \ (defined(_MSC_VER) && _MSC_VER >= 1900) // C++11 is present #else #error upb requires C++11 for C++ support #endif - -/* UPB_DISALLOW_COPY_AND_ASSIGN() - * UPB_DISALLOW_POD_OPS() - * - * Declare these in the "private" section of a C++ class to forbid copy/assign - * or all POD ops (construct, destruct, copy, assign) on that class. */ -#include -#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ - class_name(const class_name&) = delete; \ - void operator=(const class_name&) = delete; -#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ - class_name() = delete; \ - ~class_name() = delete; \ - UPB_DISALLOW_COPY_AND_ASSIGN(class_name) -#define UPB_ASSERT_STDLAYOUT(type) \ - static_assert(std::is_standard_layout::value, \ - #type " must be standard layout"); - -#ifdef __cplusplus - -#define UPB_BEGIN_EXTERN_C extern "C" { -#define UPB_END_EXTERN_C } -#define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname; - -#else /* !defined(__cplusplus) */ - -#define UPB_BEGIN_EXTERN_C -#define UPB_END_EXTERN_C -#define UPB_DECLARE_TYPE(cppname, cname) \ - struct cname; \ - typedef struct cname cname; - -#endif /* defined(__cplusplus) */ +#endif #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -138,21 +103,26 @@ template class InlinedEnvironment; #define UPB_UNREACHABLE() do { assert(0); } while(0) #endif -/* upb::Status ****************************************************************/ +/* upb_status *****************************************************************/ -/* upb::Status represents a success or failure status and error message. +/* upb_status represents a success or failure status and error message. * It owns no resources and allocates no memory, so it should work * even in OOM situations. */ -UPB_DECLARE_TYPE(upb::Status, upb_status) /* The maximum length of an error message before it will get truncated. */ -#define UPB_STATUS_MAX_MESSAGE 128 +#define UPB_STATUS_MAX_MESSAGE 127 -UPB_BEGIN_EXTERN_C +typedef struct { + bool ok; + char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */ +} upb_status; + +#ifdef __cplusplus +extern "C" { +#endif const char *upb_status_errmsg(const upb_status *status); bool upb_ok(const upb_status *status); -int upb_status_errcode(const upb_status *status); /* Any of the functions that write to a status object allow status to be NULL, * to support use cases where the function's caller does not care about the @@ -161,60 +131,44 @@ void upb_status_clear(upb_status *status); void upb_status_seterrmsg(upb_status *status, const char *msg); void upb_status_seterrf(upb_status *status, const char *fmt, ...); void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args); -void upb_status_copy(upb_status *to, const upb_status *from); -UPB_END_EXTERN_C +UPB_INLINE void upb_status_setoom(upb_status *status) { + upb_status_seterrmsg(status, "out of memory"); +} #ifdef __cplusplus +} /* extern "C" */ class upb::Status { public: - Status() { upb_status_clear(this); } + Status() { upb_status_clear(&status_); } - /* Returns true if there is no error. */ - bool ok() const { return upb_ok(this); } + upb_status* ptr() { return &status_; } - /* Optional error space and code, useful if the caller wants to - * programmatically check the specific kind of error. */ - ErrorSpace* error_space() { return upb_status_errspace(this); } - int error_code() const { return upb_status_errcode(this); } + /* Returns true if there is no error. */ + bool ok() const { return upb_ok(&status_); } - /* The returned string is invalidated by any other call into the status. */ - const char *error_message() const { return upb_status_errmsg(this); } + /* Guaranteed to be NULL-terminated. */ + const char *error_message() const { return upb_status_errmsg(&status_); } /* The error message will be truncated if it is longer than * UPB_STATUS_MAX_MESSAGE-4. */ - void SetErrorMessage(const char* msg) { upb_status_seterrmsg(this, msg); } - void SetFormattedErrorMessage(const char* fmt, ...) { + void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); } + void SetFormattedErrorMessage(const char *fmt, ...) { va_list args; va_start(args, fmt); - upb_status_vseterrf(this, fmt, args); + upb_status_vseterrf(&status_, fmt, args); va_end(args); } /* Resets the status to a successful state with no message. */ - void Clear() { upb_status_clear(this); } - - void CopyFrom(const Status& other) { upb_status_copy(this, &other); } + void Clear() { upb_status_clear(&status_); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Status) -#else -struct upb_status { -#endif - bool ok_; - - /* Specific status code defined by some error space (optional). */ - int code_; - upb_errorspace *error_space_; - - /* TODO(haberman): add file/line of error? */ - - /* Error message; NULL-terminated. */ - char msg[UPB_STATUS_MAX_MESSAGE]; + upb_status status_; }; -#define UPB_STATUS_INIT {true, 0, NULL, {0}} +#endif /* __cplusplus */ /** upb_alloc *****************************************************************/ @@ -291,17 +245,23 @@ UPB_INLINE void upb_gfree(void *ptr) { typedef void upb_cleanup_func(void *ud); struct upb_arena; -typedef upb_arena upb_arena; +typedef struct upb_arena upb_arena; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -upb_arena *upb_arena_new2(void *mem, size_t n, upb_alloc *alloc); +/* Creates an arena from the given initial block (if any -- n may be 0). + * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this + * is a fixed-size arena and cannot grow. */ +upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc); void upb_arena_free(upb_arena *a); bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); size_t upb_arena_bytesallocated(const upb_arena *a); UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } +/* Convenience wrappers around upb_alloc functions. */ UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) { return upb_malloc(upb_arena_alloc(a), size); } @@ -312,52 +272,38 @@ UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize, } UPB_INLINE upb_arena *upb_arena_new() { - return upb_arena_new2(NULL, 0, &upb_alloc_global); + return upb_arena_init(NULL, 0, &upb_alloc_global); } -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::Arena { public: /* A simple arena with no initial memory block and the default allocator. */ - Arena() { upb_arena_init(&arena_); } + Arena() : ptr_(upb_arena_new(), upb_arena_free) {} - upb_arena* ptr() { return &arena_; } - - /* Constructs an arena with the given initial block which allocates blocks - * with the given allocator. The given allocator must outlive the Arena. - * - * If you pass NULL for the allocator it will default to the global allocator - * upb_alloc_global, and NULL/0 for the initial block will cause there to be - * no initial block. */ - Arena(void *mem, size_t len, upb_alloc *a) { - upb_arena_init2(&arena_, mem, len, a); - } - - ~Arena() { upb_arena_uninit(&arena_); } + upb_arena* ptr() { return ptr_.get(); } /* Allows this arena to be used as a generic allocator. * * The arena does not need free() calls so when using Arena as an allocator * it is safe to skip them. However they are no-ops so there is no harm in * calling free() either. */ - upb_alloc *allocator() { return upb_arena_alloc(&arena_); } + upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); } /* Add a cleanup function to run when the arena is destroyed. * Returns false on out-of-memory. */ bool AddCleanup(upb_cleanup_func *func, void *ud) { - return upb_arena_addcleanup(&arena_, func, ud); + return upb_arena_addcleanup(ptr_.get(), func, ud); } /* Total number of bytes that have been allocated. It is undefined what * Realloc() does to &arena_ counter. */ - size_t BytesAllocated() const { return upb_arena_bytesallocated(&arena_); } + size_t BytesAllocated() const { return upb_arena_bytesallocated(ptr_.get()); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Arena) - upb_arena arena_; + std::unique_ptr ptr_; }; #endif @@ -373,13 +319,16 @@ class upb::Arena { template class upb::InlinedArena : public upb::Arena { public: - InlinedArena() : Arena(initial_block_, N, NULL) {} - explicit InlinedArena(Allocator* a) : Arena(initial_block_, N, a) {} + InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {} + + upb_arena* ptr() { return ptr_.get(); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(InlinedArena) + InlinedArena(const InlinedArena*) = delete; + InlinedArena& operator=(const InlinedArena*) = delete; - char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD]; + std::unique_ptr ptr_; + char initial_block_[N]; }; #endif /* __cplusplus */ diff --git a/upbc/generator.cc b/upbc/generator.cc index ddfaa7b..de40eee 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -502,7 +502,9 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { "#include \"upb/decode.h\"\n" "#include \"upb/encode.h\"\n" "#include \"upb/port_def.inc\"\n" - "UPB_BEGIN_EXTERN_C\n\n", + "#ifdef __cplusplus\n" + "extern \"C\" {\n" + "#endif\n\n", ToPreproc(file->name())); // Forward-declare types defined in this file. @@ -548,7 +550,9 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { } output( - "UPB_END_EXTERN_C\n" + "#ifdef __cplusplus\n" + "} /* extern \"C\" */\n" + "#endif\n" "\n" "#include \"upb/port_undef.inc\"\n" "\n" -- cgit v1.2.3 From 754b9f1cfdf59104fa62cfaf99f21e5a817d281b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 03:37:34 -0800 Subject: All tests pass again! --- tests/json/test_json.cc | 2 +- tests/pb/test_decoder.cc | 12 ++--- tests/pb/test_encoder.cc | 2 +- tests/test_util.h | 27 +++++------ upb/json/parser.c | 114 ++++++++++++++++++++++++----------------------- upb/json/parser.h | 9 ++-- upb/json/parser.rl | 8 ++-- upb/pb/decoder.c | 4 +- upb/pb/decoder.h | 8 ++-- 9 files changed, 95 insertions(+), 91 deletions(-) (limited to 'tests/pb/test_decoder.cc') diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 1f7d364..3dc4a6c 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -176,7 +176,7 @@ void test_json_roundtrip_message(const char* json_src, upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create( env.arena(), serialize_handlers, data_sink.Sink()); upb::json::ParserPtr parser = upb::json::ParserPtr::Create( - env.arena(), parser_method, NULL, printer.input(), false); + env.arena(), parser_method, NULL, printer.input(), env.status(), false); env.ResetBytesSink(parser.input()); env.Reset(json_src, strlen(json_src), false, false); diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index b2dd812..3c41801 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -454,8 +454,9 @@ upb::pb::DecoderMethodPtr global_method; upb::pb::DecoderPtr CreateDecoder(upb::Arena* arena, upb::pb::DecoderMethodPtr method, - upb::Sink sink) { - upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(arena, method, sink); + upb::Sink sink, upb::Status* status) { + upb::pb::DecoderPtr ret = + upb::pb::DecoderPtr::Create(arena, method, sink, status); ret.set_max_nesting(MAX_NESTING); return ret; } @@ -556,7 +557,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, void run_decoder(const string& proto, const string* expected_output) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(env.arena(), global_method, sink); + upb::pb::DecoderPtr decoder = CreateDecoder(env.arena(), global_method, sink, env.status()); env.ResetBytesSink(decoder.input()); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { @@ -874,7 +875,8 @@ void test_valid() { upb::Status status; upb::Arena arena; upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(&arena, global_method, sink); + upb::pb::DecoderPtr decoder = + CreateDecoder(&arena, global_method, sink, &status); output.clear(); bool ok = upb::PutBuffer(std::string(), decoder.input()); ASSERT(ok); @@ -1161,7 +1163,7 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_method.dest_handlers(), &closures[0]); upb::pb::DecoderPtr decoder = - CreateDecoder(env.arena(), global_method, sink); + CreateDecoder(env.arena(), global_method, sink, env.status()); env.ResetBytesSink(decoder.input()); env.Reset(testdata[i].data, testdata[i].length, true, false); ASSERT(env.Start()); diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 7145097..a042d3d 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -48,7 +48,7 @@ void test_pb_roundtrip() { upb::pb::EncoderPtr encoder = upb::pb::EncoderPtr::Create(&arena, encoder_handlers, string_sink.input()); upb::pb::DecoderPtr decoder = - upb::pb::DecoderPtr::Create(&arena, method, encoder.input()); + upb::pb::DecoderPtr::Create(&arena, method, encoder.input(), &status); ok = upb::PutBuffer(input, decoder.input()); ASSERT(ok); ASSERT(input == output); diff --git a/tests/test_util.h b/tests/test_util.h index 04ca3fb..680673d 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -32,21 +32,6 @@ class VerboseParserEnvironment { /* Pass verbose=true to print detailed diagnostics to stderr. */ VerboseParserEnvironment(bool verbose) : verbose_(verbose) {} - static bool OnError(void *ud, const upb::Status* status) { - VerboseParserEnvironment* env = static_cast(ud); - - if (env->expect_error_ && env->verbose_) { - fprintf(stderr, "Encountered error, as expected: "); - } else if (!env->expect_error_) { - fprintf(stderr, "Encountered unexpected error: "); - } else { - return false; - } - - fprintf(stderr, "%s\n", status->error_message()); - return false; - } - void Reset(const char *buf, size_t len, bool may_skip, bool expect_error) { buf_ = buf; len_ = len; @@ -99,6 +84,17 @@ class VerboseParserEnvironment { return false; } + if (!status_.ok()) { + if (expect_error_ && verbose_) { + fprintf(stderr, "Encountered error, as expected: %s", + status_.error_message()); + } else if (!expect_error_) { + fprintf(stderr, "Encountered unexpected error: %s", + status_.error_message()); + return false; + } + } + return true; } @@ -175,6 +171,7 @@ class VerboseParserEnvironment { bool SkippedWithNull() { return skipped_with_null_; } upb::Arena* arena() { return &arena_; } + upb::Status* status() { return &status_; } private: upb::Arena arena_; diff --git a/upb/json/parser.c b/upb/json/parser.c index a594bfd..5fe20ef 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -306,8 +306,9 @@ static void json_parser_any_frame_set_payload_type( /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, - frame->sink, p->ignore_json_unknown); + frame->parser = + upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, + p->status, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -2379,11 +2380,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2541 "upb/json/parser.rl" +#line 2542 "upb/json/parser.rl" -#line 2387 "upb/json/parser.c" +#line 2388 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2630,7 +2631,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2544 "upb/json/parser.rl" +#line 2545 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2653,7 +2654,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2657 "upb/json/parser.c" +#line 2658 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2728,83 +2729,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2392 "upb/json/parser.rl" +#line 2393 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2394 "upb/json/parser.rl" +#line 2395 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2398 "upb/json/parser.rl" +#line 2399 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2399 "upb/json/parser.rl" +#line 2400 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2405 "upb/json/parser.rl" +#line 2406 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2406 "upb/json/parser.rl" +#line 2407 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2407 "upb/json/parser.rl" +#line 2408 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2413 "upb/json/parser.rl" +#line 2414 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2419 "upb/json/parser.rl" +#line 2420 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2431 "upb/json/parser.rl" +#line 2432 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2432 "upb/json/parser.rl" +#line 2433 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2434 "upb/json/parser.rl" +#line 2435 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2439 "upb/json/parser.rl" +#line 2440 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2440 "upb/json/parser.rl" +#line 2441 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2442 "upb/json/parser.rl" +#line 2443 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2443 "upb/json/parser.rl" +#line 2444 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2445 "upb/json/parser.rl" +#line 2446 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2446 "upb/json/parser.rl" +#line 2447 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2448 "upb/json/parser.rl" +#line 2449 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2453 "upb/json/parser.rl" +#line 2454 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2816,11 +2817,11 @@ _match: } break; case 21: -#line 2464 "upb/json/parser.rl" +#line 2465 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2469 "upb/json/parser.rl" +#line 2470 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2830,11 +2831,11 @@ _match: } break; case 23: -#line 2476 "upb/json/parser.rl" +#line 2477 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2479 "upb/json/parser.rl" +#line 2480 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2844,7 +2845,7 @@ _match: } break; case 25: -#line 2490 "upb/json/parser.rl" +#line 2491 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2854,7 +2855,7 @@ _match: } break; case 26: -#line 2499 "upb/json/parser.rl" +#line 2500 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2864,54 +2865,54 @@ _match: } break; case 27: -#line 2511 "upb/json/parser.rl" +#line 2512 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2515 "upb/json/parser.rl" +#line 2516 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2520 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2521 "upb/json/parser.rl" +#line 2522 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2523 "upb/json/parser.rl" +#line 2524 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2524 "upb/json/parser.rl" +#line 2525 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2526 "upb/json/parser.rl" +#line 2527 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2528 "upb/json/parser.rl" +#line 2529 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2530 "upb/json/parser.rl" +#line 2531 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2532 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2533 "upb/json/parser.rl" +#line 2534 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2538 "upb/json/parser.rl" +#line 2539 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2915 "upb/json/parser.c" +#line 2916 "upb/json/parser.c" } } @@ -2928,32 +2929,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2390 "upb/json/parser.rl" +#line 2391 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2521 "upb/json/parser.rl" +#line 2522 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2526 "upb/json/parser.rl" +#line 2527 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2528 "upb/json/parser.rl" +#line 2529 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2530 "upb/json/parser.rl" +#line 2531 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2533 "upb/json/parser.rl" +#line 2534 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2957 "upb/json/parser.c" +#line 2958 "upb/json/parser.c" } } } @@ -2961,7 +2962,7 @@ goto _again;} } _out: {} } -#line 2566 "upb/json/parser.rl" +#line 2567 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3008,13 +3009,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3012 "upb/json/parser.c" +#line 3013 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2612 "upb/json/parser.rl" +#line 2613 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3071,6 +3072,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, + upb_status *status, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_arena_bytesallocated(arena); @@ -3080,7 +3082,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, p->arena = arena; p->method = method; - p->status = NULL; + p->status = status; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; diff --git a/upb/json/parser.h b/upb/json/parser.h index 2a06fcf..6f3eaa7 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -71,6 +71,7 @@ upb_json_parser* upb_json_parser_create(upb_arena* a, const upb_json_parsermethod* m, const upb_symtab* symtab, upb_sink output, + upb_status *status, bool ignore_json_unknown); upb_bytessink upb_json_parser_input(upb_json_parser* p); @@ -84,12 +85,12 @@ class upb::json::ParserPtr { ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} static ParserPtr Create(Arena* arena, ParserMethodPtr method, - SymbolTable* symtab, Sink output, + SymbolTable* symtab, Sink output, Status* status, bool ignore_json_unknown) { upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; - return ParserPtr(upb_json_parser_create(arena->ptr(), method.ptr(), - symtab_ptr, output.sink(), - ignore_json_unknown)); + return ParserPtr(upb_json_parser_create( + arena->ptr(), method.ptr(), symtab_ptr, output.sink(), status->ptr(), + ignore_json_unknown)); } BytesSink input() { return upb_json_parser_input(ptr_); } diff --git a/upb/json/parser.rl b/upb/json/parser.rl index a117d0c..6f27630 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -304,8 +304,9 @@ static void json_parser_any_frame_set_payload_type( /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, - frame->sink, p->ignore_json_unknown); + frame->parser = + upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, + p->status, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -2665,6 +2666,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, + upb_status *status, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_arena_bytesallocated(arena); @@ -2674,7 +2676,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, p->arena = arena; p->method = method; - p->status = NULL; + p->status = status; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index 5068225..f1617db 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -991,7 +991,7 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) { } upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, - upb_sink sink) { + upb_sink sink, upb_status *status) { const size_t default_max_nesting = 64; #ifndef NDEBUG size_t size_before = upb_arena_bytesallocated(a); @@ -1010,7 +1010,7 @@ upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, d->arena = a; d->limit = d->stack + default_max_nesting - 1; d->stack_size = default_max_nesting; - d->status = NULL; + d->status = status; upb_pbdecoder_reset(d); upb_bytessink_reset(&d->input_, &m->input_handler_, d); diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 6fcef03..5adfba8 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -102,7 +102,7 @@ extern "C" { upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena, const upb_pbdecodermethod *method, - upb_sink output); + upb_sink output, upb_status *status); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d); uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); @@ -127,9 +127,9 @@ class upb::pb::DecoderPtr { * * The sink must match the given method. */ static DecoderPtr Create(Arena *arena, DecoderMethodPtr method, - upb::Sink output) { - return DecoderPtr( - upb_pbdecoder_create(arena->ptr(), method.ptr(), output.sink())); + upb::Sink output, Status *status) { + return DecoderPtr(upb_pbdecoder_create(arena->ptr(), method.ptr(), + output.sink(), status->ptr())); } /* Returns the DecoderMethod this decoder is parsing from. */ -- cgit v1.2.3 From 84fb01ad0f7301b416e03d97fbffef1a7512e7ea Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 16 Jan 2019 15:53:13 -0800 Subject: Removed the JIT. Nobody was actually using it, and table-driven should achieve 80-90% of the perf. --- BUILD | 29 +- tests/pb/test_decoder.cc | 45 +- third_party/dynasm/LICENSE | 56 -- third_party/dynasm/README.google | 10 - third_party/dynasm/dasm_arm.h | 448 --------- third_party/dynasm/dasm_arm.lua | 949 ------------------- third_party/dynasm/dasm_mips.h | 415 -------- third_party/dynasm/dasm_mips.lua | 959 ------------------- third_party/dynasm/dasm_ppc.h | 411 -------- third_party/dynasm/dasm_ppc.lua | 1230 ------------------------ third_party/dynasm/dasm_proto.h | 83 -- third_party/dynasm/dasm_x64.lua | 12 - third_party/dynasm/dasm_x86.h | 470 ---------- third_party/dynasm/dasm_x86.lua | 1931 -------------------------------------- third_party/dynasm/dynasm.lua | 1084 --------------------- third_party/protobuf | 1 - upb/pb/compile_decoder.c | 38 +- upb/pb/compile_decoder_x64.c | 511 ---------- upb/pb/compile_decoder_x64.dasc | 1150 ----------------------- upb/pb/compile_decoder_x64.h | 1737 ---------------------------------- 20 files changed, 10 insertions(+), 11559 deletions(-) delete mode 100644 third_party/dynasm/LICENSE delete mode 100644 third_party/dynasm/README.google delete mode 100644 third_party/dynasm/dasm_arm.h delete mode 100644 third_party/dynasm/dasm_arm.lua delete mode 100644 third_party/dynasm/dasm_mips.h delete mode 100644 third_party/dynasm/dasm_mips.lua delete mode 100644 third_party/dynasm/dasm_ppc.h delete mode 100644 third_party/dynasm/dasm_ppc.lua delete mode 100644 third_party/dynasm/dasm_proto.h delete mode 100644 third_party/dynasm/dasm_x64.lua delete mode 100644 third_party/dynasm/dasm_x86.h delete mode 100644 third_party/dynasm/dasm_x86.lua delete mode 100644 third_party/dynasm/dynasm.lua delete mode 160000 third_party/protobuf delete mode 100644 upb/pb/compile_decoder_x64.c delete mode 100644 upb/pb/compile_decoder_x64.dasc delete mode 100644 upb/pb/compile_decoder_x64.h (limited to 'tests/pb/test_decoder.cc') diff --git a/BUILD b/BUILD index 5804d36..80daf4f 100644 --- a/BUILD +++ b/BUILD @@ -67,15 +67,7 @@ cc_library( "upb/pb/textprinter.c", "upb/pb/varint.c", "upb/pb/varint.int.h", - ] + select({ - ":k8": [ - "upb/pb/compile_decoder_x64.c", - "upb/pb/compile_decoder_x64.h", - "third_party/dynasm/dasm_proto.h", - "third_party/dynasm/dasm_x86.h", - ], - "//conditions:default": [], - }), + ], hdrs = [ "upb/pb/decoder.h", "upb/pb/encoder.h", @@ -85,10 +77,7 @@ cc_library( "-std=c89", "-pedantic", "-Wno-long-long", - ] + select({ - ":k8": ["-DUPB_USE_JIT_X64"], - "//conditions:default": [], - }), + ], deps = [ ":upb", ], @@ -421,19 +410,6 @@ py_library( srcs = ["tools/staleness_test_lib.py"], ) -genrule( - name = "make_dynasm_decoder", - srcs = [ - "third_party/dynasm/dynasm.lua", - "third_party/dynasm/dasm_x64.lua", - "third_party/dynasm/dasm_x86.lua", - "upb/pb/compile_decoder_x64.dasc", - ], - outs = ["generated/upb/pb/compile_decoder_x64.h"], - cmd = "LUA_PATH=third_party/dynasm/?.lua $(location @lua//:lua) third_party/dynasm/dynasm.lua -c upb/pb/compile_decoder_x64.dasc > $@", - tools = ["@lua"], -) - py_binary( name = "make_cmakelists", srcs = ["tools/make_cmakelists.py"], @@ -514,7 +490,6 @@ generated_file_staleness_test( "google/protobuf/descriptor.upb.h", "tests/json/test.proto.pb", "upb/json/parser.c", - "upb/pb/compile_decoder_x64.h", ], generated_pattern = "generated/%s", ) diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index 3c41801..8012405 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -242,16 +242,8 @@ void indentbuf(string *buf, int depth) { buf->append(2 * depth, ' '); } -void check_stack_alignment() { -#ifdef UPB_USE_JIT_X64 - void *rsp = __builtin_frame_address(0); - ASSERT(((uintptr_t)rsp % 16) == 0); -#endif -} - #define NUMERIC_VALUE_HANDLER(member, ctype, fmt) \ bool value_##member(int* depth, const uint32_t* num, ctype val) { \ - check_stack_alignment(); \ indentbuf(&output, *depth); \ appendf(&output, "%" PRIu32 ":%" fmt "\n", *num, val); \ return true; \ @@ -265,14 +257,12 @@ NUMERIC_VALUE_HANDLER(float, float, "g") NUMERIC_VALUE_HANDLER(double, double, "g") bool value_bool(int* depth, const uint32_t* num, bool val) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":%s\n", *num, val ? "true" : "false"); return true; } int* startstr(int* depth, const uint32_t* num, size_t size_hint) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":(%zu)\"", *num, size_hint); return depth + 1; @@ -282,7 +272,6 @@ size_t value_string(int* depth, const uint32_t* num, const char* buf, size_t n, const upb_bufhandle* handle) { UPB_UNUSED(num); UPB_UNUSED(depth); - check_stack_alignment(); output.append(buf, n); ASSERT(handle == &global_handle); return n; @@ -290,7 +279,6 @@ size_t value_string(int* depth, const uint32_t* num, const char* buf, bool endstr(int* depth, const uint32_t* num) { UPB_UNUSED(num); - check_stack_alignment(); output.append("\n"); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":\"\n", *num); @@ -298,7 +286,6 @@ bool endstr(int* depth, const uint32_t* num) { } int* startsubmsg(int* depth, const uint32_t* num) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":{\n", *num); return depth + 1; @@ -306,14 +293,12 @@ int* startsubmsg(int* depth, const uint32_t* num) { bool endsubmsg(int* depth, const uint32_t* num) { UPB_UNUSED(num); - check_stack_alignment(); indentbuf(&output, *depth); output.append("}\n"); return true; } int* startseq(int* depth, const uint32_t* num) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":[\n", *num); return depth + 1; @@ -321,14 +306,12 @@ int* startseq(int* depth, const uint32_t* num) { bool endseq(int* depth, const uint32_t* num) { UPB_UNUSED(num); - check_stack_alignment(); indentbuf(&output, *depth); output.append("]\n"); return true; } bool startmsg(int* depth) { - check_stack_alignment(); indentbuf(&output, *depth); output.append("<\n"); return true; @@ -336,7 +319,6 @@ bool startmsg(int* depth) { bool endmsg(int* depth, upb_status* status) { UPB_UNUSED(status); - check_stack_alignment(); indentbuf(&output, *depth); output.append(">\n"); return true; @@ -507,8 +489,6 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, if (filter_hash) { fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash); - fprintf(stderr, "JIT on: %s\n", - global_method.is_native() ? "true" : "false"); fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size()); PrintBinary(proto); fprintf(stderr, "\n"); @@ -1128,7 +1108,7 @@ void test_valid() { void empty_callback(const void *closure, upb::Handlers* h_ptr) {} -void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { +void test_emptyhandlers(upb::SymbolTable* symtab) { // Create an empty handlers to make sure that the decoder can handle empty // messages. HandlerRegisterData handlerdata; @@ -1137,8 +1117,6 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { upb::HandlerCache handler_cache(empty_callback, &handlerdata); upb::pb::CodeCache pb_code_cache(&handler_cache); - pb_code_cache.set_allow_jit(allowjit); - upb::MessageDefPtr md = upb::MessageDefPtr(Empty_getmsgdef(symtab->ptr())); global_handlers = handler_cache.Get(md); global_method = pb_code_cache.Get(md); @@ -1173,7 +1151,7 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { } } -void run_tests(bool use_jit) { +void run_tests() { HandlerRegisterData handlerdata; handlerdata.mode = test_mode; @@ -1181,26 +1159,15 @@ void run_tests(bool use_jit) { upb::HandlerCache handler_cache(callback, &handlerdata); upb::pb::CodeCache pb_code_cache(&handler_cache); - pb_code_cache.set_allow_jit(use_jit); - upb::MessageDefPtr md(DecoderTest_getmsgdef(symtab.ptr())); global_handlers = handler_cache.Get(md); global_method = pb_code_cache.Get(md); - ASSERT(use_jit == global_method.is_native()); completed = 0; test_invalid(); test_valid(); - test_emptyhandlers(&symtab, use_jit); -} - -void run_test_suite() { - // Test without/with JIT. - run_tests(false); -#ifdef UPB_USE_JIT_X64 - run_tests(true); -#endif + test_emptyhandlers(&symtab); } extern "C" { @@ -1216,16 +1183,16 @@ int run_tests(int argc, char *argv[]) { count = &total; total = 0; test_mode = COUNT_ONLY; - run_test_suite(); + run_tests(); count = &completed; total *= 2; // NO_HANDLERS, ALL_HANDLERS. test_mode = NO_HANDLERS; - run_test_suite(); + run_tests(); test_mode = ALL_HANDLERS; - run_test_suite(); + run_tests(); printf("All tests passed, %d assertions.\n", num_assertions); return 0; diff --git a/third_party/dynasm/LICENSE b/third_party/dynasm/LICENSE deleted file mode 100644 index 6f2a45b..0000000 --- a/third_party/dynasm/LICENSE +++ /dev/null @@ -1,56 +0,0 @@ -=============================================================================== -LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ - -Copyright (C) 2005-2011 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -[ MIT license: http://www.opensource.org/licenses/mit-license.php ] - -=============================================================================== -[ LuaJIT includes code from Lua 5.1/5.2, which has this license statement: ] - -Copyright (C) 1994-2011 Lua.org, PUC-Rio. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=============================================================================== -[ LuaJIT includes code from dlmalloc, which has this license statement: ] - -This is a version (aka dlmalloc) of malloc/free/realloc written by -Doug Lea and released to the public domain, as explained at -http://creativecommons.org/licenses/publicdomain - -=============================================================================== diff --git a/third_party/dynasm/README.google b/third_party/dynasm/README.google deleted file mode 100644 index dd829a0..0000000 --- a/third_party/dynasm/README.google +++ /dev/null @@ -1,10 +0,0 @@ -URL: http://repo.or.cz/w/luajit-2.0.git/tree/6c05739684527919293e25668589f17c35a7c129:/dynasm -Version: 6c05739684527919293e25668589f17c35a7c129 -License: MIT -License File: LICENSE -Description: -Taken from the larger LuaJIT project, DynASM is a tiny preprocessor and -runtime for generating machine code at runtime. - -Local Modifications: -No modifications. diff --git a/third_party/dynasm/dasm_arm.h b/third_party/dynasm/dasm_arm.h deleted file mode 100644 index b770c2d..0000000 --- a/third_party/dynasm/dasm_arm.h +++ /dev/null @@ -1,448 +0,0 @@ -/* -** DynASM ARM encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "arm" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, - DASM_IMM, DASM_IMM12, DASM_IMM16, DASM_IMML8, DASM_IMML12, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -static int dasm_imm12(unsigned int n) -{ - int i; - for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30)) - if (n <= 255) return (int)(n + (i << 8)); - return -1; -} - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: - case DASM_IMM16: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); - if ((ins & 0x8000)) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - case DASM_IMML8: - case DASM_IMML12: - CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) : - (((-n)>>((ins>>5)&31)) == 0), RANGE_I); - b[pos++] = n; - break; - case DASM_IMM12: - CK(dasm_imm12((unsigned int)n) != -1, RANGE_I); - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMM12: case DASM_IMM16: - case DASM_IMML8: case DASM_IMML12: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048)); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) - 4; - patchrel: - if ((ins & 0x800) == 0) { - CK((n & 3) == 0 && ((n+0x02000000) >> 26) == 0, RANGE_REL); - cp[-1] |= ((n >> 2) & 0x00ffffff); - } else if ((ins & 0x1000)) { - CK((n & 3) == 0 && -256 <= n && n <= 256, RANGE_REL); - goto patchimml8; - } else { - CK((n & 3) == 0 && -4096 <= n && n <= 4096, RANGE_REL); - goto patchimml12; - } - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - case DASM_IMM12: - cp[-1] |= dasm_imm12((unsigned int)n); - break; - case DASM_IMM16: - cp[-1] |= ((n & 0xf000) << 4) | (n & 0x0fff); - break; - case DASM_IMML8: patchimml8: - cp[-1] |= n >= 0 ? (0x00800000 | (n & 0x0f) | ((n & 0xf0) << 4)) : - ((-n & 0x0f) | ((-n & 0xf0) << 4)); - break; - case DASM_IMML12: patchimml12: - cp[-1] |= n >= 0 ? (0x00800000 | n) : (-n); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_arm.lua b/third_party/dynasm/dasm_arm.lua deleted file mode 100644 index cc4fa17..0000000 --- a/third_party/dynasm/dasm_arm.lua +++ /dev/null @@ -1,949 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM ARM module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "arm", - description = "DynASM ARM module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable, rawget = assert, setmetatable, rawget -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub -local concat, sort, insert = table.concat, table.sort, table.insert - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Return 8 digit hex number. -local function tohex(x) - return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0x000fffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - if n <= 0x000fffff then - insert(actlist, pos+1, n) - n = map_action.ESC * 0x10000 - end - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. - --- Ext. register name -> int. name. -local map_archdef = { sp = "r13", lr = "r14", pc = "r15", } - --- Int. register name -> ext. name. -local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", } - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - return map_reg_rev[s] or s -end - -local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, } - -local map_cond = { - eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, - hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, - hs = 2, lo = 3, -} - ------------------------------------------------------------------------------- - --- Template strings for ARM instructions. -local map_op = { - -- Basic data processing instructions. - and_3 = "e0000000DNPs", - eor_3 = "e0200000DNPs", - sub_3 = "e0400000DNPs", - rsb_3 = "e0600000DNPs", - add_3 = "e0800000DNPs", - adc_3 = "e0a00000DNPs", - sbc_3 = "e0c00000DNPs", - rsc_3 = "e0e00000DNPs", - tst_2 = "e1100000NP", - teq_2 = "e1300000NP", - cmp_2 = "e1500000NP", - cmn_2 = "e1700000NP", - orr_3 = "e1800000DNPs", - mov_2 = "e1a00000DPs", - bic_3 = "e1c00000DNPs", - mvn_2 = "e1e00000DPs", - - and_4 = "e0000000DNMps", - eor_4 = "e0200000DNMps", - sub_4 = "e0400000DNMps", - rsb_4 = "e0600000DNMps", - add_4 = "e0800000DNMps", - adc_4 = "e0a00000DNMps", - sbc_4 = "e0c00000DNMps", - rsc_4 = "e0e00000DNMps", - tst_3 = "e1100000NMp", - teq_3 = "e1300000NMp", - cmp_3 = "e1500000NMp", - cmn_3 = "e1700000NMp", - orr_4 = "e1800000DNMps", - mov_3 = "e1a00000DMps", - bic_4 = "e1c00000DNMps", - mvn_3 = "e1e00000DMps", - - lsl_3 = "e1a00000DMws", - lsr_3 = "e1a00020DMws", - asr_3 = "e1a00040DMws", - ror_3 = "e1a00060DMws", - rrx_2 = "e1a00060DMs", - - -- Multiply and multiply-accumulate. - mul_3 = "e0000090NMSs", - mla_4 = "e0200090NMSDs", - umaal_4 = "e0400090DNMSs", -- v6 - mls_4 = "e0600090DNMSs", -- v6T2 - umull_4 = "e0800090DNMSs", - umlal_4 = "e0a00090DNMSs", - smull_4 = "e0c00090DNMSs", - smlal_4 = "e0e00090DNMSs", - - -- Halfword multiply and multiply-accumulate. - smlabb_4 = "e1000080NMSD", -- v5TE - smlatb_4 = "e10000a0NMSD", -- v5TE - smlabt_4 = "e10000c0NMSD", -- v5TE - smlatt_4 = "e10000e0NMSD", -- v5TE - smlawb_4 = "e1200080NMSD", -- v5TE - smulwb_3 = "e12000a0NMS", -- v5TE - smlawt_4 = "e12000c0NMSD", -- v5TE - smulwt_3 = "e12000e0NMS", -- v5TE - smlalbb_4 = "e1400080NMSD", -- v5TE - smlaltb_4 = "e14000a0NMSD", -- v5TE - smlalbt_4 = "e14000c0NMSD", -- v5TE - smlaltt_4 = "e14000e0NMSD", -- v5TE - smulbb_3 = "e1600080NMS", -- v5TE - smultb_3 = "e16000a0NMS", -- v5TE - smulbt_3 = "e16000c0NMS", -- v5TE - smultt_3 = "e16000e0NMS", -- v5TE - - -- Miscellaneous data processing instructions. - clz_2 = "e16f0f10DM", -- v5T - rev_2 = "e6bf0f30DM", -- v6 - rev16_2 = "e6bf0fb0DM", -- v6 - revsh_2 = "e6ff0fb0DM", -- v6 - sel_3 = "e6800fb0DNM", -- v6 - usad8_3 = "e780f010NMS", -- v6 - usada8_4 = "e7800010NMSD", -- v6 - rbit_2 = "e6ff0f30DM", -- v6T2 - movw_2 = "e3000000DW", -- v6T2 - movt_2 = "e3400000DW", -- v6T2 - -- Note: the X encodes width-1, not width. - sbfx_4 = "e7a00050DMvX", -- v6T2 - ubfx_4 = "e7e00050DMvX", -- v6T2 - -- Note: the X encodes the msb field, not the width. - bfc_3 = "e7c0001fDvX", -- v6T2 - bfi_4 = "e7c00010DMvX", -- v6T2 - - -- Packing and unpacking instructions. - pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6 - pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6 - sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6 - sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6 - sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6 - sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6 - sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6 - sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6 - uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6 - uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6 - uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6 - uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6 - uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6 - uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6 - - -- Saturating instructions. - qadd_3 = "e1000050DMN", -- v5TE - qsub_3 = "e1200050DMN", -- v5TE - qdadd_3 = "e1400050DMN", -- v5TE - qdsub_3 = "e1600050DMN", -- v5TE - -- Note: the X for ssat* encodes sat_imm-1, not sat_imm. - ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6 - usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6 - ssat16_3 = "e6a00f30DXM", -- v6 - usat16_3 = "e6e00f30DXM", -- v6 - - -- Parallel addition and subtraction. - sadd16_3 = "e6100f10DNM", -- v6 - sasx_3 = "e6100f30DNM", -- v6 - ssax_3 = "e6100f50DNM", -- v6 - ssub16_3 = "e6100f70DNM", -- v6 - sadd8_3 = "e6100f90DNM", -- v6 - ssub8_3 = "e6100ff0DNM", -- v6 - qadd16_3 = "e6200f10DNM", -- v6 - qasx_3 = "e6200f30DNM", -- v6 - qsax_3 = "e6200f50DNM", -- v6 - qsub16_3 = "e6200f70DNM", -- v6 - qadd8_3 = "e6200f90DNM", -- v6 - qsub8_3 = "e6200ff0DNM", -- v6 - shadd16_3 = "e6300f10DNM", -- v6 - shasx_3 = "e6300f30DNM", -- v6 - shsax_3 = "e6300f50DNM", -- v6 - shsub16_3 = "e6300f70DNM", -- v6 - shadd8_3 = "e6300f90DNM", -- v6 - shsub8_3 = "e6300ff0DNM", -- v6 - uadd16_3 = "e6500f10DNM", -- v6 - uasx_3 = "e6500f30DNM", -- v6 - usax_3 = "e6500f50DNM", -- v6 - usub16_3 = "e6500f70DNM", -- v6 - uadd8_3 = "e6500f90DNM", -- v6 - usub8_3 = "e6500ff0DNM", -- v6 - uqadd16_3 = "e6600f10DNM", -- v6 - uqasx_3 = "e6600f30DNM", -- v6 - uqsax_3 = "e6600f50DNM", -- v6 - uqsub16_3 = "e6600f70DNM", -- v6 - uqadd8_3 = "e6600f90DNM", -- v6 - uqsub8_3 = "e6600ff0DNM", -- v6 - uhadd16_3 = "e6700f10DNM", -- v6 - uhasx_3 = "e6700f30DNM", -- v6 - uhsax_3 = "e6700f50DNM", -- v6 - uhsub16_3 = "e6700f70DNM", -- v6 - uhadd8_3 = "e6700f90DNM", -- v6 - uhsub8_3 = "e6700ff0DNM", -- v6 - - -- Load/store instructions. - str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL", - strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL", - ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL", - ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL", - strh_2 = "e00000b0DL", strh_3 = "e00000b0DL", - ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL", - ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE - ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL", - strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE - ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL", - - ldm_2 = "e8900000nR", ldmia_2 = "e8900000nR", ldmfd_2 = "e8900000nR", - ldmda_2 = "e8100000nR", ldmfa_2 = "e8100000nR", - ldmdb_2 = "e9100000nR", ldmea_2 = "e9100000nR", - ldmib_2 = "e9900000nR", ldmed_2 = "e9900000nR", - stm_2 = "e8800000nR", stmia_2 = "e8800000nR", stmfd_2 = "e8800000nR", - stmda_2 = "e8000000nR", stmfa_2 = "e8000000nR", - stmdb_2 = "e9000000nR", stmea_2 = "e9000000nR", - stmib_2 = "e9800000nR", stmed_2 = "e9800000nR", - pop_1 = "e8bd0000R", push_1 = "e92d0000R", - - -- Branch instructions. - b_1 = "ea000000B", - bl_1 = "eb000000B", - blx_1 = "e12fff30C", - bx_1 = "e12fff10M", - - -- Miscellaneous instructions. - nop_0 = "e1a00000", - mrs_1 = "e10f0000D", - bkpt_1 = "e1200070K", -- v5T - svc_1 = "ef000000T", swi_1 = "ef000000T", - ud_0 = "e7f001f0", - - -- NYI: Advanced SIMD and VFP instructions. - - -- NYI instructions, since I have no need for them right now: - -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh - -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe - -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb - -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2 -} - --- Add mnemonics for "s" variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "s" then - local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2) - t[sub(k, 1, -3).."s"..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r(1?[0-9])$") - if r then - r = tonumber(r) - if r <= 15 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_gpr_pm(expr) - local pm, expr2 = match(expr, "^([+-]?)(.*)$") - return parse_gpr(expr2), (pm == "-") -end - -local function parse_reglist(reglist) - reglist = match(reglist, "^{%s*([^}]*)}$") - if not reglist then werror("register list expected") end - local rr = 0 - for p in gmatch(reglist..",", "%s*([^,]*),") do - local rbit = 2^parse_gpr(gsub(p, "%s+$", "")) - if ((rr - (rr % rbit)) / rbit) % 2 ~= 0 then - werror("duplicate register `"..p.."'") - end - rr = rr + rbit - end - return rr -end - -local function parse_imm(imm, bits, shift, scale, signed) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - if n % 2^scale == 0 then - n = n / 2^scale - if signed then - if n >= 0 then - if n < 2^(bits-1) then return n*2^shift end - else - if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end - end - else - if n >= 0 and n <= 2^bits-1 then return n*2^shift end - end - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_imm12(imm) - local n = tonumber(imm) - if n then - local m = n - for i=0,-15,-1 do - if m >= 0 and m <= 255 and n % 1 == 0 then return m + (i%16) * 256 end - local t = m % 4 - m = (m - t) / 4 + t * 2^30 - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM12", 0, imm) - return 0 - end -end - -local function parse_imm16(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - if n >= 0 and n <= 65535 and n % 1 == 0 then - local t = n % 4096 - return (n - t) * 16 + t - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM16", 32*16, imm) - return 0 - end -end - -local function parse_imm_load(imm, ext) - local n = tonumber(imm) - if n then - if ext then - if n >= -255 and n <= 255 then - local up = 0x00800000 - if n < 0 then n = -n; up = 0 end - return (n-(n%16))*16+(n%16) + up - end - else - if n >= -4095 and n <= 4095 then - if n >= 0 then return n+0x00800000 end - return -n - end - end - werror("out of range immediate `"..imm.."'") - else - waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), imm) - return 0 - end -end - -local function parse_shift(shift, gprok) - if shift == "rrx" then - return 3 * 32 - else - local s, s2 = match(shift, "^(%S+)%s*(.*)$") - s = map_shift[s] - if not s then werror("expected shift operand") end - if sub(s2, 1, 1) == "#" then - return parse_imm(s2, 5, 7, 0, false) + s * 32 - else - if not gprok then werror("expected immediate shift operand") end - return parse_gpr(s2) * 256 + s * 32 + 16 - end - end -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - -local function parse_load(params, nparams, n, op) - local oplo = op % 256 - local ext, ldrd = (oplo ~= 0), (oplo == 208) - local d - if (ldrd or oplo == 240) then - d = ((op - (op % 4096)) / 4096) % 16 - if d % 2 ~= 0 then werror("odd destination register") end - end - local pn = params[n] - local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") - local p2 = params[n+1] - if not p1 then - if not p2 then - if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then - local mode, n, s = parse_label(pn, false) - waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1) - return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0) - end - local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local d, tp = parse_gpr(reg) - if tp then - waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), - format(tp.ctypefmt, tailr)) - return op + d * 65536 + 0x01000000 + (ext and 0x00400000 or 0) - end - end - end - werror("expected address operand") - end - if wb == "!" then op = op + 0x00200000 end - if p2 then - if wb == "!" then werror("bad use of '!'") end - local p3 = params[n+2] - op = op + parse_gpr(p1) * 65536 - local imm = match(p2, "^#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - if p3 then werror("too many parameters") end - op = op + m + (ext and 0x00400000 or 0) - else - local m, neg = parse_gpr_pm(p2) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 then op = op + parse_shift(p3) end - end - else - local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$") - op = op + parse_gpr(p1a) * 65536 + 0x01000000 - if p2 ~= "" then - local imm = match(p2, "^,%s*#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - op = op + m + (ext and 0x00400000 or 0) - else - local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$") - local m, neg = parse_gpr_pm(p2a) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 ~= "" then - if ext then werror("too many parameters") end - op = op + parse_shift(p3) - end - end - else - if wb == "!" then werror("bad use of '!'") end - op = op + (ext and 0x00c00000 or 0x00800000) - end - end - return op -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions. - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "D" then - op = op + parse_gpr(params[n]) * 4096; n = n + 1 - elseif p == "N" then - op = op + parse_gpr(params[n]) * 65536; n = n + 1 - elseif p == "S" then - op = op + parse_gpr(params[n]) * 256; n = n + 1 - elseif p == "M" then - op = op + parse_gpr(params[n]); n = n + 1 - elseif p == "P" then - local imm = match(params[n], "^#(.*)$") - if imm then - op = op + parse_imm12(imm) + 0x02000000 - else - op = op + parse_gpr(params[n]) - end - n = n + 1 - elseif p == "p" then - op = op + parse_shift(params[n], true); n = n + 1 - elseif p == "L" then - op = parse_load(params, nparams, n, op) - elseif p == "B" then - local mode, n, s = parse_label(params[n], false) - waction("REL_"..mode, n, s, 1) - elseif p == "C" then -- blx gpr vs. blx label. - local p = params[n] - if match(p, "^([%w_]+):(r1?[0-9])$") or match(p, "^r(1?[0-9])$") then - op = op + parse_gpr(p) - else - if op < 0xe0000000 then werror("unconditional instruction") end - local mode, n, s = parse_label(p, false) - waction("REL_"..mode, n, s, 1) - op = 0xfa000000 - end - elseif p == "n" then - local r, wb = match(params[n], "^([^!]*)(!?)$") - op = op + parse_gpr(r) * 65536 + (wb == "!" and 0x00200000 or 0) - n = n + 1 - elseif p == "R" then - op = op + parse_reglist(params[n]); n = n + 1 - elseif p == "W" then - op = op + parse_imm16(params[n]); n = n + 1 - elseif p == "v" then - op = op + parse_imm(params[n], 5, 7, 0, false); n = n + 1 - elseif p == "w" then - local imm = match(params[n], "^#(.*)$") - if imm then - op = op + parse_imm(params[n], 5, 7, 0, false); n = n + 1 - else - op = op + parse_gpr(params[n]) * 256 + 16 - end - elseif p == "X" then - op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 - elseif p == "K" then - local imm = tonumber(match(params[n], "^#(.*)$")); n = n + 1 - if not imm or imm % 1 ~= 0 or imm < 0 or imm > 0xffff then - werror("bad immediate operand") - end - local t = imm % 16 - op = op + (imm - t) * 16 + t - elseif p == "T" then - op = op + parse_imm(params[n], 24, 0, 0, false); n = n + 1 - elseif p == "s" then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = function(t, k) - local v = map_coreop[k] - if v then return v end - local cc = sub(k, -4, -3) - local cv = map_cond[cc] - if cv then - local v = rawget(t, sub(k, 1, -5)..sub(k, -2)) - if type(v) == "string" then return format("%x%s", cv, sub(v, 2)) end - end - end }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dasm_mips.h b/third_party/dynasm/dasm_mips.h deleted file mode 100644 index af87d99..0000000 --- a/third_party/dynasm/dasm_mips.h +++ /dev/null @@ -1,415 +0,0 @@ -/* -** DynASM MIPS encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "mips" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n); - if (ins & 2048) - n = n - (int)((char *)cp - base); - else - n = (n + (int)base) & 0x0fffffff; - patchrel: - CK((n & 3) == 0 && - ((n + ((ins & 2048) ? 0x00020000 : 0)) >> - ((ins & 2048) ? 18 : 28)) == 0, RANGE_REL); - cp[-1] |= ((n>>2) & ((ins & 2048) ? 0x0000ffff: 0x03ffffff)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_mips.lua b/third_party/dynasm/dasm_mips.lua deleted file mode 100644 index aa33f0c..0000000 --- a/third_party/dynasm/dasm_mips.lua +++ /dev/null @@ -1,959 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM MIPS module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "mips", - description = "DynASM MIPS module", - version = "1.3.0", - vernum = 10300, - release = "2012-01-23", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Return 8 digit hex number. -local function tohex(x) - return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(0xff000000 + w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n >= 0xff000000 then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp="r29", ra="r31" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r29" then return "sp" - elseif s == "r31" then return "ra" end - return s -end - ------------------------------------------------------------------------------- - --- Template strings for MIPS instructions. -local map_op = { - -- First-level opcodes. - j_1 = "08000000J", - jal_1 = "0c000000J", - b_1 = "10000000B", - beqz_2 = "10000000SB", - beq_3 = "10000000STB", - bnez_2 = "14000000SB", - bne_3 = "14000000STB", - blez_2 = "18000000SB", - bgtz_2 = "1c000000SB", - addi_3 = "20000000TSI", - li_2 = "24000000TI", - addiu_3 = "24000000TSI", - slti_3 = "28000000TSI", - sltiu_3 = "2c000000TSI", - andi_3 = "30000000TSU", - lu_2 = "34000000TU", - ori_3 = "34000000TSU", - xori_3 = "38000000TSU", - lui_2 = "3c000000TU", - beqzl_2 = "50000000SB", - beql_3 = "50000000STB", - bnezl_2 = "54000000SB", - bnel_3 = "54000000STB", - blezl_2 = "58000000SB", - bgtzl_2 = "5c000000SB", - lb_2 = "80000000TO", - lh_2 = "84000000TO", - lwl_2 = "88000000TO", - lw_2 = "8c000000TO", - lbu_2 = "90000000TO", - lhu_2 = "94000000TO", - lwr_2 = "98000000TO", - sb_2 = "a0000000TO", - sh_2 = "a4000000TO", - swl_2 = "a8000000TO", - sw_2 = "ac000000TO", - swr_2 = "b8000000TO", - cache_2 = "bc000000NO", - ll_2 = "c0000000TO", - lwc1_2 = "c4000000HO", - pref_2 = "cc000000NO", - ldc1_2 = "d4000000HO", - sc_2 = "e0000000TO", - swc1_2 = "e4000000HO", - sdc1_2 = "f4000000HO", - - -- Opcode SPECIAL. - nop_0 = "00000000", - sll_3 = "00000000DTA", - movf_2 = "00000001DS", - movf_3 = "00000001DSC", - movt_2 = "00010001DS", - movt_3 = "00010001DSC", - srl_3 = "00000002DTA", - rotr_3 = "00200002DTA", - sra_3 = "00000003DTA", - sllv_3 = "00000004DTS", - srlv_3 = "00000006DTS", - rotrv_3 = "00000046DTS", - srav_3 = "00000007DTS", - jr_1 = "00000008S", - jalr_1 = "0000f809S", - jalr_2 = "00000009DS", - movz_3 = "0000000aDST", - movn_3 = "0000000bDST", - syscall_0 = "0000000c", - syscall_1 = "0000000cY", - break_0 = "0000000d", - break_1 = "0000000dY", - sync_0 = "0000000f", - mfhi_1 = "00000010D", - mthi_1 = "00000011S", - mflo_1 = "00000012D", - mtlo_1 = "00000013S", - mult_2 = "00000018ST", - multu_2 = "00000019ST", - div_2 = "0000001aST", - divu_2 = "0000001bST", - add_3 = "00000020DST", - move_2 = "00000021DS", - addu_3 = "00000021DST", - sub_3 = "00000022DST", - negu_2 = "00000023DT", - subu_3 = "00000023DST", - and_3 = "00000024DST", - or_3 = "00000025DST", - xor_3 = "00000026DST", - not_2 = "00000027DS", - nor_3 = "00000027DST", - slt_3 = "0000002aDST", - sltu_3 = "0000002bDST", - tge_2 = "00000030ST", - tge_3 = "00000030STZ", - tgeu_2 = "00000031ST", - tgeu_3 = "00000031STZ", - tlt_2 = "00000032ST", - tlt_3 = "00000032STZ", - tltu_2 = "00000033ST", - tltu_3 = "00000033STZ", - teq_2 = "00000034ST", - teq_3 = "00000034STZ", - tne_2 = "00000036ST", - tne_3 = "00000036STZ", - - -- Opcode REGIMM. - bltz_2 = "04000000SB", - bgez_2 = "04010000SB", - bltzl_2 = "04020000SB", - bgezl_2 = "04030000SB", - tgei_2 = "04080000SI", - tgeiu_2 = "04090000SI", - tlti_2 = "040a0000SI", - tltiu_2 = "040b0000SI", - teqi_2 = "040c0000SI", - tnei_2 = "040e0000SI", - bltzal_2 = "04100000SB", - bal_1 = "04110000B", - bgezal_2 = "04110000SB", - bltzall_2 = "04120000SB", - bgezall_2 = "04130000SB", - synci_1 = "041f0000O", - - -- Opcode SPECIAL2. - madd_2 = "70000000ST", - maddu_2 = "70000001ST", - mul_3 = "70000002DST", - msub_2 = "70000004ST", - msubu_2 = "70000005ST", - clz_2 = "70000020DS=", - clo_2 = "70000021DS=", - sdbbp_0 = "7000003f", - sdbbp_1 = "7000003fY", - - -- Opcode SPECIAL3. - ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1 - ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1 - wsbh_2 = "7c0000a0DT", - seb_2 = "7c000420DT", - seh_2 = "7c000620DT", - rdhwr_2 = "7c00003bTD", - - -- Opcode COP0. - mfc0_2 = "40000000TD", - mfc0_3 = "40000000TDW", - mtc0_2 = "40800000TD", - mtc0_3 = "40800000TDW", - rdpgpr_2 = "41400000DT", - di_0 = "41606000", - di_1 = "41606000T", - ei_0 = "41606020", - ei_1 = "41606020T", - wrpgpr_2 = "41c00000DT", - tlbr_0 = "42000001", - tlbwi_0 = "42000002", - tlbwr_0 = "42000006", - tlbp_0 = "42000008", - eret_0 = "42000018", - deret_0 = "4200001f", - wait_0 = "42000020", - - -- Opcode COP1. - mfc1_2 = "44000000TG", - cfc1_2 = "44400000TG", - mfhc1_2 = "44600000TG", - mtc1_2 = "44800000TG", - ctc1_2 = "44c00000TG", - mthc1_2 = "44e00000TG", - - bc1f_1 = "45000000B", - bc1f_2 = "45000000CB", - bc1t_1 = "45010000B", - bc1t_2 = "45010000CB", - bc1fl_1 = "45020000B", - bc1fl_2 = "45020000CB", - bc1tl_1 = "45030000B", - bc1tl_2 = "45030000CB", - - ["add.s_3"] = "46000000FGH", - ["sub.s_3"] = "46000001FGH", - ["mul.s_3"] = "46000002FGH", - ["div.s_3"] = "46000003FGH", - ["sqrt.s_2"] = "46000004FG", - ["abs.s_2"] = "46000005FG", - ["mov.s_2"] = "46000006FG", - ["neg.s_2"] = "46000007FG", - ["round.l.s_2"] = "46000008FG", - ["trunc.l.s_2"] = "46000009FG", - ["ceil.l.s_2"] = "4600000aFG", - ["floor.l.s_2"] = "4600000bFG", - ["round.w.s_2"] = "4600000cFG", - ["trunc.w.s_2"] = "4600000dFG", - ["ceil.w.s_2"] = "4600000eFG", - ["floor.w.s_2"] = "4600000fFG", - ["movf.s_2"] = "46000011FG", - ["movf.s_3"] = "46000011FGC", - ["movt.s_2"] = "46010011FG", - ["movt.s_3"] = "46010011FGC", - ["movz.s_3"] = "46000012FGT", - ["movn.s_3"] = "46000013FGT", - ["recip.s_2"] = "46000015FG", - ["rsqrt.s_2"] = "46000016FG", - ["cvt.d.s_2"] = "46000021FG", - ["cvt.w.s_2"] = "46000024FG", - ["cvt.l.s_2"] = "46000025FG", - ["cvt.ps.s_3"] = "46000026FGH", - ["c.f.s_2"] = "46000030GH", - ["c.f.s_3"] = "46000030VGH", - ["c.un.s_2"] = "46000031GH", - ["c.un.s_3"] = "46000031VGH", - ["c.eq.s_2"] = "46000032GH", - ["c.eq.s_3"] = "46000032VGH", - ["c.ueq.s_2"] = "46000033GH", - ["c.ueq.s_3"] = "46000033VGH", - ["c.olt.s_2"] = "46000034GH", - ["c.olt.s_3"] = "46000034VGH", - ["c.ult.s_2"] = "46000035GH", - ["c.ult.s_3"] = "46000035VGH", - ["c.ole.s_2"] = "46000036GH", - ["c.ole.s_3"] = "46000036VGH", - ["c.ule.s_2"] = "46000037GH", - ["c.ule.s_3"] = "46000037VGH", - ["c.sf.s_2"] = "46000038GH", - ["c.sf.s_3"] = "46000038VGH", - ["c.ngle.s_2"] = "46000039GH", - ["c.ngle.s_3"] = "46000039VGH", - ["c.seq.s_2"] = "4600003aGH", - ["c.seq.s_3"] = "4600003aVGH", - ["c.ngl.s_2"] = "4600003bGH", - ["c.ngl.s_3"] = "4600003bVGH", - ["c.lt.s_2"] = "4600003cGH", - ["c.lt.s_3"] = "4600003cVGH", - ["c.nge.s_2"] = "4600003dGH", - ["c.nge.s_3"] = "4600003dVGH", - ["c.le.s_2"] = "4600003eGH", - ["c.le.s_3"] = "4600003eVGH", - ["c.ngt.s_2"] = "4600003fGH", - ["c.ngt.s_3"] = "4600003fVGH", - - ["add.d_3"] = "46200000FGH", - ["sub.d_3"] = "46200001FGH", - ["mul.d_3"] = "46200002FGH", - ["div.d_3"] = "46200003FGH", - ["sqrt.d_2"] = "46200004FG", - ["abs.d_2"] = "46200005FG", - ["mov.d_2"] = "46200006FG", - ["neg.d_2"] = "46200007FG", - ["round.l.d_2"] = "46200008FG", - ["trunc.l.d_2"] = "46200009FG", - ["ceil.l.d_2"] = "4620000aFG", - ["floor.l.d_2"] = "4620000bFG", - ["round.w.d_2"] = "4620000cFG", - ["trunc.w.d_2"] = "4620000dFG", - ["ceil.w.d_2"] = "4620000eFG", - ["floor.w.d_2"] = "4620000fFG", - ["movf.d_2"] = "46200011FG", - ["movf.d_3"] = "46200011FGC", - ["movt.d_2"] = "46210011FG", - ["movt.d_3"] = "46210011FGC", - ["movz.d_3"] = "46200012FGT", - ["movn.d_3"] = "46200013FGT", - ["recip.d_2"] = "46200015FG", - ["rsqrt.d_2"] = "46200016FG", - ["cvt.s.d_2"] = "46200020FG", - ["cvt.w.d_2"] = "46200024FG", - ["cvt.l.d_2"] = "46200025FG", - ["c.f.d_2"] = "46200030GH", - ["c.f.d_3"] = "46200030VGH", - ["c.un.d_2"] = "46200031GH", - ["c.un.d_3"] = "46200031VGH", - ["c.eq.d_2"] = "46200032GH", - ["c.eq.d_3"] = "46200032VGH", - ["c.ueq.d_2"] = "46200033GH", - ["c.ueq.d_3"] = "46200033VGH", - ["c.olt.d_2"] = "46200034GH", - ["c.olt.d_3"] = "46200034VGH", - ["c.ult.d_2"] = "46200035GH", - ["c.ult.d_3"] = "46200035VGH", - ["c.ole.d_2"] = "46200036GH", - ["c.ole.d_3"] = "46200036VGH", - ["c.ule.d_2"] = "46200037GH", - ["c.ule.d_3"] = "46200037VGH", - ["c.sf.d_2"] = "46200038GH", - ["c.sf.d_3"] = "46200038VGH", - ["c.ngle.d_2"] = "46200039GH", - ["c.ngle.d_3"] = "46200039VGH", - ["c.seq.d_2"] = "4620003aGH", - ["c.seq.d_3"] = "4620003aVGH", - ["c.ngl.d_2"] = "4620003bGH", - ["c.ngl.d_3"] = "4620003bVGH", - ["c.lt.d_2"] = "4620003cGH", - ["c.lt.d_3"] = "4620003cVGH", - ["c.nge.d_2"] = "4620003dGH", - ["c.nge.d_3"] = "4620003dVGH", - ["c.le.d_2"] = "4620003eGH", - ["c.le.d_3"] = "4620003eVGH", - ["c.ngt.d_2"] = "4620003fGH", - ["c.ngt.d_3"] = "4620003fVGH", - - ["add.ps_3"] = "46c00000FGH", - ["sub.ps_3"] = "46c00001FGH", - ["mul.ps_3"] = "46c00002FGH", - ["abs.ps_2"] = "46c00005FG", - ["mov.ps_2"] = "46c00006FG", - ["neg.ps_2"] = "46c00007FG", - ["movf.ps_2"] = "46c00011FG", - ["movf.ps_3"] = "46c00011FGC", - ["movt.ps_2"] = "46c10011FG", - ["movt.ps_3"] = "46c10011FGC", - ["movz.ps_3"] = "46c00012FGT", - ["movn.ps_3"] = "46c00013FGT", - ["cvt.s.pu_2"] = "46c00020FG", - ["cvt.s.pl_2"] = "46c00028FG", - ["pll.ps_3"] = "46c0002cFGH", - ["plu.ps_3"] = "46c0002dFGH", - ["pul.ps_3"] = "46c0002eFGH", - ["puu.ps_3"] = "46c0002fFGH", - ["c.f.ps_2"] = "46c00030GH", - ["c.f.ps_3"] = "46c00030VGH", - ["c.un.ps_2"] = "46c00031GH", - ["c.un.ps_3"] = "46c00031VGH", - ["c.eq.ps_2"] = "46c00032GH", - ["c.eq.ps_3"] = "46c00032VGH", - ["c.ueq.ps_2"] = "46c00033GH", - ["c.ueq.ps_3"] = "46c00033VGH", - ["c.olt.ps_2"] = "46c00034GH", - ["c.olt.ps_3"] = "46c00034VGH", - ["c.ult.ps_2"] = "46c00035GH", - ["c.ult.ps_3"] = "46c00035VGH", - ["c.ole.ps_2"] = "46c00036GH", - ["c.ole.ps_3"] = "46c00036VGH", - ["c.ule.ps_2"] = "46c00037GH", - ["c.ule.ps_3"] = "46c00037VGH", - ["c.sf.ps_2"] = "46c00038GH", - ["c.sf.ps_3"] = "46c00038VGH", - ["c.ngle.ps_2"] = "46c00039GH", - ["c.ngle.ps_3"] = "46c00039VGH", - ["c.seq.ps_2"] = "46c0003aGH", - ["c.seq.ps_3"] = "46c0003aVGH", - ["c.ngl.ps_2"] = "46c0003bGH", - ["c.ngl.ps_3"] = "46c0003bVGH", - ["c.lt.ps_2"] = "46c0003cGH", - ["c.lt.ps_3"] = "46c0003cVGH", - ["c.nge.ps_2"] = "46c0003dGH", - ["c.nge.ps_3"] = "46c0003dVGH", - ["c.le.ps_2"] = "46c0003eGH", - ["c.le.ps_3"] = "46c0003eVGH", - ["c.ngt.ps_2"] = "46c0003fGH", - ["c.ngt.ps_3"] = "46c0003fVGH", - - ["cvt.s.w_2"] = "46800020FG", - ["cvt.d.w_2"] = "46800021FG", - - ["cvt.s.l_2"] = "46a00020FG", - ["cvt.d.l_2"] = "46a00021FG", - - -- Opcode COP1X. - lwxc1_2 = "4c000000FX", - ldxc1_2 = "4c000001FX", - luxc1_2 = "4c000005FX", - swxc1_2 = "4c000008FX", - sdxc1_2 = "4c000009FX", - suxc1_2 = "4c00000dFX", - prefx_2 = "4c00000fMX", - ["alnv.ps_4"] = "4c00001eFGHS", - ["madd.s_4"] = "4c000020FRGH", - ["madd.d_4"] = "4c000021FRGH", - ["madd.ps_4"] = "4c000026FRGH", - ["msub.s_4"] = "4c000028FRGH", - ["msub.d_4"] = "4c000029FRGH", - ["msub.ps_4"] = "4c00002eFRGH", - ["nmadd.s_4"] = "4c000030FRGH", - ["nmadd.d_4"] = "4c000031FRGH", - ["nmadd.ps_4"] = "4c000036FRGH", - ["nmsub.s_4"] = "4c000038FRGH", - ["nmsub.d_4"] = "4c000039FRGH", - ["nmsub.ps_4"] = "4c00003eFRGH", -} - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = tonumber(imm) - if n then - if n % 2^scale == 0 then - n = n / 2^scale - if signed then - if n >= 0 then - if n < 2^(bits-1) then return n*2^shift end - else - if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end - end - else - if n >= 0 and n <= 2^bits-1 then return n*2^shift end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^[rf]([1-3]?[0-9])$") or - match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg)*2^21 - local extname = match(imm, "^extern%s+(%S+)$") - if extname then - waction("REL_EXT", map_extern[extname], nil, 1) - return r - else - return r + parse_imm(imm, 16, 0, 0, true) - end - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return r*2^21 - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_index(idx) - local rt, rs = match(idx, "^(.*)%(([%w_:]+)%)$") - if rt then - rt = parse_gpr(rt) - rs = parse_gpr(rs) - return rt*2^16 + rs*2^21 - end - werror("bad index `"..idx.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 2 positions (ins/ext). - if secpos+2 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "D" then - op = op + parse_gpr(params[n]) * 2^11; n = n + 1 - elseif p == "T" then - op = op + parse_gpr(params[n]) * 2^16; n = n + 1 - elseif p == "S" then - op = op + parse_gpr(params[n]) * 2^21; n = n + 1 - elseif p == "F" then - op = op + parse_fpr(params[n]) * 2^6; n = n + 1 - elseif p == "G" then - op = op + parse_fpr(params[n]) * 2^11; n = n + 1 - elseif p == "H" then - op = op + parse_fpr(params[n]) * 2^16; n = n + 1 - elseif p == "R" then - op = op + parse_fpr(params[n]) * 2^21; n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "O" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "X" then - op = op + parse_index(params[n]); n = n + 1 - elseif p == "B" or p == "J" then - local mode, n, s = parse_label(params[n], false) - if p == "B" then n = n + 2048 end - waction("REL_"..mode, n, s, 1) - n = n + 1 - elseif p == "A" then - op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1 - elseif p == "M" then - op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1 - elseif p == "N" then - op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 - elseif p == "C" then - op = op + parse_imm(params[n], 3, 18, 0, false); n = n + 1 - elseif p == "V" then - op = op + parse_imm(params[n], 3, 8, 0, false); n = n + 1 - elseif p == "W" then - op = op + parse_imm(params[n], 3, 0, 0, false); n = n + 1 - elseif p == "Y" then - op = op + parse_imm(params[n], 20, 6, 0, false); n = n + 1 - elseif p == "Z" then - op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1 - elseif p == "=" then - local d = ((op - op % 2^11) / 2^11) % 32 - op = op + d * 2^16 -- Copy D to T for clz, clo. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dasm_ppc.h b/third_party/dynasm/dasm_ppc.h deleted file mode 100644 index bf5957e..0000000 --- a/third_party/dynasm/dasm_ppc.h +++ /dev/null @@ -1,411 +0,0 @@ -/* -** DynASM PPC encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "ppc" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1) - 4; - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base); - patchrel: - CK((n & 3) == 0 && - (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >> - ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL); - cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_ppc.lua b/third_party/dynasm/dasm_ppc.lua deleted file mode 100644 index dc2af69..0000000 --- a/third_party/dynasm/dasm_ppc.lua +++ /dev/null @@ -1,1230 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM PPC module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "ppc", - description = "DynASM PPC module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Return 8 digit hex number. -local function tohex(x) - return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0xffffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp = "r1" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r1" then return "sp" end - return s -end - -local map_cond = { - lt = 0, gt = 1, eq = 2, so = 3, - ge = 4, le = 5, ne = 6, ns = 7, -} - ------------------------------------------------------------------------------- - --- Template strings for PPC instructions. -local map_op = { - tdi_3 = "08000000ARI", - twi_3 = "0c000000ARI", - mulli_3 = "1c000000RRI", - subfic_3 = "20000000RRI", - cmplwi_3 = "28000000XRU", - cmplwi_2 = "28000000-RU", - cmpldi_3 = "28200000XRU", - cmpldi_2 = "28200000-RU", - cmpwi_3 = "2c000000XRI", - cmpwi_2 = "2c000000-RI", - cmpdi_3 = "2c200000XRI", - cmpdi_2 = "2c200000-RI", - addic_3 = "30000000RRI", - ["addic._3"] = "34000000RRI", - addi_3 = "38000000RR0I", - li_2 = "38000000RI", - la_2 = "38000000RD", - addis_3 = "3c000000RR0I", - lis_2 = "3c000000RI", - lus_2 = "3c000000RU", - bc_3 = "40000000AAK", - bcl_3 = "40000001AAK", - bdnz_1 = "42000000K", - bdz_1 = "42400000K", - sc_0 = "44000000", - b_1 = "48000000J", - bl_1 = "48000001J", - rlwimi_5 = "50000000RR~AAA.", - rlwinm_5 = "54000000RR~AAA.", - rlwnm_5 = "5c000000RR~RAA.", - ori_3 = "60000000RR~U", - nop_0 = "60000000", - oris_3 = "64000000RR~U", - xori_3 = "68000000RR~U", - xoris_3 = "6c000000RR~U", - ["andi._3"] = "70000000RR~U", - ["andis._3"] = "74000000RR~U", - lwz_2 = "80000000RD", - lwzu_2 = "84000000RD", - lbz_2 = "88000000RD", - lbzu_2 = "8c000000RD", - stw_2 = "90000000RD", - stwu_2 = "94000000RD", - stb_2 = "98000000RD", - stbu_2 = "9c000000RD", - lhz_2 = "a0000000RD", - lhzu_2 = "a4000000RD", - lha_2 = "a8000000RD", - lhau_2 = "ac000000RD", - sth_2 = "b0000000RD", - sthu_2 = "b4000000RD", - lmw_2 = "b8000000RD", - stmw_2 = "bc000000RD", - lfs_2 = "c0000000FD", - lfsu_2 = "c4000000FD", - lfd_2 = "c8000000FD", - lfdu_2 = "cc000000FD", - stfs_2 = "d0000000FD", - stfsu_2 = "d4000000FD", - stfd_2 = "d8000000FD", - stfdu_2 = "dc000000FD", - ld_2 = "e8000000RD", -- NYI: displacement must be divisible by 4. - ldu_2 = "e8000001RD", - lwa_2 = "e8000002RD", - std_2 = "f8000000RD", - stdu_2 = "f8000001RD", - - -- Primary opcode 19: - mcrf_2 = "4c000000XX", - isync_0 = "4c00012c", - crnor_3 = "4c000042CCC", - crnot_2 = "4c000042CC=", - crandc_3 = "4c000102CCC", - crxor_3 = "4c000182CCC", - crclr_1 = "4c000182C==", - crnand_3 = "4c0001c2CCC", - crand_3 = "4c000202CCC", - creqv_3 = "4c000242CCC", - crset_1 = "4c000242C==", - crorc_3 = "4c000342CCC", - cror_3 = "4c000382CCC", - crmove_2 = "4c000382CC=", - bclr_2 = "4c000020AA", - bclrl_2 = "4c000021AA", - bcctr_2 = "4c000420AA", - bcctrl_2 = "4c000421AA", - blr_0 = "4e800020", - blrl_0 = "4e800021", - bctr_0 = "4e800420", - bctrl_0 = "4e800421", - - -- Primary opcode 31: - cmpw_3 = "7c000000XRR", - cmpw_2 = "7c000000-RR", - cmpd_3 = "7c200000XRR", - cmpd_2 = "7c200000-RR", - tw_3 = "7c000008ARR", - subfc_3 = "7c000010RRR.", - subc_3 = "7c000010RRR~.", - mulhdu_3 = "7c000012RRR.", - addc_3 = "7c000014RRR.", - mulhwu_3 = "7c000016RRR.", - isel_4 = "7c00001eRRRC", - isellt_3 = "7c00001eRRR", - iselgt_3 = "7c00005eRRR", - iseleq_3 = "7c00009eRRR", - mfcr_1 = "7c000026R", - mtcrf_2 = "7c000120GR", - -- NYI: mtocrf, mfocrf - lwarx_3 = "7c000028RR0R", - ldx_3 = "7c00002aRR0R", - lwzx_3 = "7c00002eRR0R", - slw_3 = "7c000030RR~R.", - cntlzw_2 = "7c000034RR~", - sld_3 = "7c000036RR~R.", - and_3 = "7c000038RR~R.", - cmplw_3 = "7c000040XRR", - cmplw_2 = "7c000040-RR", - cmpld_3 = "7c200040XRR", - cmpld_2 = "7c200040-RR", - subf_3 = "7c000050RRR.", - sub_3 = "7c000050RRR~.", - ldux_3 = "7c00006aRR0R", - dcbst_2 = "7c00006c-RR", - lwzux_3 = "7c00006eRR0R", - cntlzd_2 = "7c000074RR~", - andc_3 = "7c000078RR~R.", - td_3 = "7c000088ARR", - mulhd_3 = "7c000092RRR.", - mulhw_3 = "7c000096RRR.", - ldarx_3 = "7c0000a8RR0R", - dcbf_2 = "7c0000ac-RR", - lbzx_3 = "7c0000aeRR0R", - neg_2 = "7c0000d0RR.", - lbzux_3 = "7c0000eeRR0R", - popcntb_2 = "7c0000f4RR~", - not_2 = "7c0000f8RR~%.", - nor_3 = "7c0000f8RR~R.", - subfe_3 = "7c000110RRR.", - sube_3 = "7c000110RRR~.", - adde_3 = "7c000114RRR.", - stdx_3 = "7c00012aRR0R", - stwcx_3 = "7c00012cRR0R.", - stwx_3 = "7c00012eRR0R", - prtyw_2 = "7c000134RR~", - stdux_3 = "7c00016aRR0R", - stwux_3 = "7c00016eRR0R", - prtyd_2 = "7c000174RR~", - subfze_2 = "7c000190RR.", - addze_2 = "7c000194RR.", - stdcx_3 = "7c0001acRR0R.", - stbx_3 = "7c0001aeRR0R", - subfme_2 = "7c0001d0RR.", - mulld_3 = "7c0001d2RRR.", - addme_2 = "7c0001d4RR.", - mullw_3 = "7c0001d6RRR.", - dcbtst_2 = "7c0001ec-RR", - stbux_3 = "7c0001eeRR0R", - add_3 = "7c000214RRR.", - dcbt_2 = "7c00022c-RR", - lhzx_3 = "7c00022eRR0R", - eqv_3 = "7c000238RR~R.", - eciwx_3 = "7c00026cRR0R", - lhzux_3 = "7c00026eRR0R", - xor_3 = "7c000278RR~R.", - mfspefscr_1 = "7c0082a6R", - mfxer_1 = "7c0102a6R", - mflr_1 = "7c0802a6R", - mfctr_1 = "7c0902a6R", - lwax_3 = "7c0002aaRR0R", - lhax_3 = "7c0002aeRR0R", - mftb_1 = "7c0c42e6R", - mftbu_1 = "7c0d42e6R", - lwaux_3 = "7c0002eaRR0R", - lhaux_3 = "7c0002eeRR0R", - sthx_3 = "7c00032eRR0R", - orc_3 = "7c000338RR~R.", - ecowx_3 = "7c00036cRR0R", - sthux_3 = "7c00036eRR0R", - or_3 = "7c000378RR~R.", - mr_2 = "7c000378RR~%.", - divdu_3 = "7c000392RRR.", - divwu_3 = "7c000396RRR.", - mtspefscr_1 = "7c0083a6R", - mtxer_1 = "7c0103a6R", - mtlr_1 = "7c0803a6R", - mtctr_1 = "7c0903a6R", - dcbi_2 = "7c0003ac-RR", - nand_3 = "7c0003b8RR~R.", - divd_3 = "7c0003d2RRR.", - divw_3 = "7c0003d6RRR.", - cmpb_3 = "7c0003f8RR~R.", - mcrxr_1 = "7c000400X", - subfco_3 = "7c000410RRR.", - subco_3 = "7c000410RRR~.", - addco_3 = "7c000414RRR.", - ldbrx_3 = "7c000428RR0R", - lswx_3 = "7c00042aRR0R", - lwbrx_3 = "7c00042cRR0R", - lfsx_3 = "7c00042eFR0R", - srw_3 = "7c000430RR~R.", - srd_3 = "7c000436RR~R.", - subfo_3 = "7c000450RRR.", - subo_3 = "7c000450RRR~.", - lfsux_3 = "7c00046eFR0R", - lswi_3 = "7c0004aaRR0A", - sync_0 = "7c0004ac", - lwsync_0 = "7c2004ac", - ptesync_0 = "7c4004ac", - lfdx_3 = "7c0004aeFR0R", - nego_2 = "7c0004d0RR.", - lfdux_3 = "7c0004eeFR0R", - subfeo_3 = "7c000510RRR.", - subeo_3 = "7c000510RRR~.", - addeo_3 = "7c000514RRR.", - stdbrx_3 = "7c000528RR0R", - stswx_3 = "7c00052aRR0R", - stwbrx_3 = "7c00052cRR0R", - stfsx_3 = "7c00052eFR0R", - stfsux_3 = "7c00056eFR0R", - subfzeo_2 = "7c000590RR.", - addzeo_2 = "7c000594RR.", - stswi_3 = "7c0005aaRR0A", - stfdx_3 = "7c0005aeFR0R", - subfmeo_2 = "7c0005d0RR.", - mulldo_3 = "7c0005d2RRR.", - addmeo_2 = "7c0005d4RR.", - mullwo_3 = "7c0005d6RRR.", - dcba_2 = "7c0005ec-RR", - stfdux_3 = "7c0005eeFR0R", - addo_3 = "7c000614RRR.", - lhbrx_3 = "7c00062cRR0R", - sraw_3 = "7c000630RR~R.", - srad_3 = "7c000634RR~R.", - srawi_3 = "7c000670RR~A.", - eieio_0 = "7c0006ac", - lfiwax_3 = "7c0006aeFR0R", - sthbrx_3 = "7c00072cRR0R", - extsh_2 = "7c000734RR~.", - extsb_2 = "7c000774RR~.", - divduo_3 = "7c000792RRR.", - divwou_3 = "7c000796RRR.", - icbi_2 = "7c0007ac-RR", - stfiwx_3 = "7c0007aeFR0R", - extsw_2 = "7c0007b4RR~.", - divdo_3 = "7c0007d2RRR.", - divwo_3 = "7c0007d6RRR.", - dcbz_2 = "7c0007ec-RR", - - -- Primary opcode 59: - fdivs_3 = "ec000024FFF.", - fsubs_3 = "ec000028FFF.", - fadds_3 = "ec00002aFFF.", - fsqrts_2 = "ec00002cF-F.", - fres_2 = "ec000030F-F.", - fmuls_3 = "ec000032FF-F.", - frsqrtes_2 = "ec000034F-F.", - fmsubs_4 = "ec000038FFFF~.", - fmadds_4 = "ec00003aFFFF~.", - fnmsubs_4 = "ec00003cFFFF~.", - fnmadds_4 = "ec00003eFFFF~.", - - -- Primary opcode 63: - fdiv_3 = "fc000024FFF.", - fsub_3 = "fc000028FFF.", - fadd_3 = "fc00002aFFF.", - fsqrt_2 = "fc00002cF-F.", - fsel_4 = "fc00002eFFFF~.", - fre_2 = "fc000030F-F.", - fmul_3 = "fc000032FF-F.", - frsqrte_2 = "fc000034F-F.", - fmsub_4 = "fc000038FFFF~.", - fmadd_4 = "fc00003aFFFF~.", - fnmsub_4 = "fc00003cFFFF~.", - fnmadd_4 = "fc00003eFFFF~.", - fcmpu_3 = "fc000000XFF", - fcpsgn_3 = "fc000010FFF.", - fcmpo_3 = "fc000040XFF", - mtfsb1_1 = "fc00004cA", - fneg_2 = "fc000050F-F.", - mcrfs_2 = "fc000080XX", - mtfsb0_1 = "fc00008cA", - fmr_2 = "fc000090F-F.", - frsp_2 = "fc000018F-F.", - fctiw_2 = "fc00001cF-F.", - fctiwz_2 = "fc00001eF-F.", - mtfsfi_2 = "fc00010cAA", -- NYI: upshift. - fnabs_2 = "fc000110F-F.", - fabs_2 = "fc000210F-F.", - frin_2 = "fc000310F-F.", - friz_2 = "fc000350F-F.", - frip_2 = "fc000390F-F.", - frim_2 = "fc0003d0F-F.", - mffs_1 = "fc00048eF.", - -- NYI: mtfsf, mtfsb0, mtfsb1. - fctid_2 = "fc00065cF-F.", - fctidz_2 = "fc00065eF-F.", - fcfid_2 = "fc00069cF-F.", - - -- Primary opcode 4, SPE APU extension: - evaddw_3 = "10000200RRR", - evaddiw_3 = "10000202RAR~", - evsubw_3 = "10000204RRR~", - evsubiw_3 = "10000206RAR~", - evabs_2 = "10000208RR", - evneg_2 = "10000209RR", - evextsb_2 = "1000020aRR", - evextsh_2 = "1000020bRR", - evrndw_2 = "1000020cRR", - evcntlzw_2 = "1000020dRR", - evcntlsw_2 = "1000020eRR", - brinc_3 = "1000020fRRR", - evand_3 = "10000211RRR", - evandc_3 = "10000212RRR", - evxor_3 = "10000216RRR", - evor_3 = "10000217RRR", - evmr_2 = "10000217RR=", - evnor_3 = "10000218RRR", - evnot_2 = "10000218RR=", - eveqv_3 = "10000219RRR", - evorc_3 = "1000021bRRR", - evnand_3 = "1000021eRRR", - evsrwu_3 = "10000220RRR", - evsrws_3 = "10000221RRR", - evsrwiu_3 = "10000222RRA", - evsrwis_3 = "10000223RRA", - evslw_3 = "10000224RRR", - evslwi_3 = "10000226RRA", - evrlw_3 = "10000228RRR", - evsplati_2 = "10000229RS", - evrlwi_3 = "1000022aRRA", - evsplatfi_2 = "1000022bRS", - evmergehi_3 = "1000022cRRR", - evmergelo_3 = "1000022dRRR", - evcmpgtu_3 = "10000230XRR", - evcmpgtu_2 = "10000230-RR", - evcmpgts_3 = "10000231XRR", - evcmpgts_2 = "10000231-RR", - evcmpltu_3 = "10000232XRR", - evcmpltu_2 = "10000232-RR", - evcmplts_3 = "10000233XRR", - evcmplts_2 = "10000233-RR", - evcmpeq_3 = "10000234XRR", - evcmpeq_2 = "10000234-RR", - evsel_4 = "10000278RRRW", - evsel_3 = "10000278RRR", - evfsadd_3 = "10000280RRR", - evfssub_3 = "10000281RRR", - evfsabs_2 = "10000284RR", - evfsnabs_2 = "10000285RR", - evfsneg_2 = "10000286RR", - evfsmul_3 = "10000288RRR", - evfsdiv_3 = "10000289RRR", - evfscmpgt_3 = "1000028cXRR", - evfscmpgt_2 = "1000028c-RR", - evfscmplt_3 = "1000028dXRR", - evfscmplt_2 = "1000028d-RR", - evfscmpeq_3 = "1000028eXRR", - evfscmpeq_2 = "1000028e-RR", - evfscfui_2 = "10000290R-R", - evfscfsi_2 = "10000291R-R", - evfscfuf_2 = "10000292R-R", - evfscfsf_2 = "10000293R-R", - evfsctui_2 = "10000294R-R", - evfsctsi_2 = "10000295R-R", - evfsctuf_2 = "10000296R-R", - evfsctsf_2 = "10000297R-R", - evfsctuiz_2 = "10000298R-R", - evfsctsiz_2 = "1000029aR-R", - evfststgt_3 = "1000029cXRR", - evfststgt_2 = "1000029c-RR", - evfststlt_3 = "1000029dXRR", - evfststlt_2 = "1000029d-RR", - evfststeq_3 = "1000029eXRR", - evfststeq_2 = "1000029e-RR", - efsadd_3 = "100002c0RRR", - efssub_3 = "100002c1RRR", - efsabs_2 = "100002c4RR", - efsnabs_2 = "100002c5RR", - efsneg_2 = "100002c6RR", - efsmul_3 = "100002c8RRR", - efsdiv_3 = "100002c9RRR", - efscmpgt_3 = "100002ccXRR", - efscmpgt_2 = "100002cc-RR", - efscmplt_3 = "100002cdXRR", - efscmplt_2 = "100002cd-RR", - efscmpeq_3 = "100002ceXRR", - efscmpeq_2 = "100002ce-RR", - efscfd_2 = "100002cfR-R", - efscfui_2 = "100002d0R-R", - efscfsi_2 = "100002d1R-R", - efscfuf_2 = "100002d2R-R", - efscfsf_2 = "100002d3R-R", - efsctui_2 = "100002d4R-R", - efsctsi_2 = "100002d5R-R", - efsctuf_2 = "100002d6R-R", - efsctsf_2 = "100002d7R-R", - efsctuiz_2 = "100002d8R-R", - efsctsiz_2 = "100002daR-R", - efststgt_3 = "100002dcXRR", - efststgt_2 = "100002dc-RR", - efststlt_3 = "100002ddXRR", - efststlt_2 = "100002dd-RR", - efststeq_3 = "100002deXRR", - efststeq_2 = "100002de-RR", - efdadd_3 = "100002e0RRR", - efdsub_3 = "100002e1RRR", - efdcfuid_2 = "100002e2R-R", - efdcfsid_2 = "100002e3R-R", - efdabs_2 = "100002e4RR", - efdnabs_2 = "100002e5RR", - efdneg_2 = "100002e6RR", - efdmul_3 = "100002e8RRR", - efddiv_3 = "100002e9RRR", - efdctuidz_2 = "100002eaR-R", - efdctsidz_2 = "100002ebR-R", - efdcmpgt_3 = "100002ecXRR", - efdcmpgt_2 = "100002ec-RR", - efdcmplt_3 = "100002edXRR", - efdcmplt_2 = "100002ed-RR", - efdcmpeq_3 = "100002eeXRR", - efdcmpeq_2 = "100002ee-RR", - efdcfs_2 = "100002efR-R", - efdcfui_2 = "100002f0R-R", - efdcfsi_2 = "100002f1R-R", - efdcfuf_2 = "100002f2R-R", - efdcfsf_2 = "100002f3R-R", - efdctui_2 = "100002f4R-R", - efdctsi_2 = "100002f5R-R", - efdctuf_2 = "100002f6R-R", - efdctsf_2 = "100002f7R-R", - efdctuiz_2 = "100002f8R-R", - efdctsiz_2 = "100002faR-R", - efdtstgt_3 = "100002fcXRR", - efdtstgt_2 = "100002fc-RR", - efdtstlt_3 = "100002fdXRR", - efdtstlt_2 = "100002fd-RR", - efdtsteq_3 = "100002feXRR", - efdtsteq_2 = "100002fe-RR", - evlddx_3 = "10000300RR0R", - evldd_2 = "10000301R8", - evldwx_3 = "10000302RR0R", - evldw_2 = "10000303R8", - evldhx_3 = "10000304RR0R", - evldh_2 = "10000305R8", - evlwhex_3 = "10000310RR0R", - evlwhe_2 = "10000311R4", - evlwhoux_3 = "10000314RR0R", - evlwhou_2 = "10000315R4", - evlwhosx_3 = "10000316RR0R", - evlwhos_2 = "10000317R4", - evstddx_3 = "10000320RR0R", - evstdd_2 = "10000321R8", - evstdwx_3 = "10000322RR0R", - evstdw_2 = "10000323R8", - evstdhx_3 = "10000324RR0R", - evstdh_2 = "10000325R8", - evstwhex_3 = "10000330RR0R", - evstwhe_2 = "10000331R4", - evstwhox_3 = "10000334RR0R", - evstwho_2 = "10000335R4", - evstwwex_3 = "10000338RR0R", - evstwwe_2 = "10000339R4", - evstwwox_3 = "1000033cRR0R", - evstwwo_2 = "1000033dR4", - evmhessf_3 = "10000403RRR", - evmhossf_3 = "10000407RRR", - evmheumi_3 = "10000408RRR", - evmhesmi_3 = "10000409RRR", - evmhesmf_3 = "1000040bRRR", - evmhoumi_3 = "1000040cRRR", - evmhosmi_3 = "1000040dRRR", - evmhosmf_3 = "1000040fRRR", - evmhessfa_3 = "10000423RRR", - evmhossfa_3 = "10000427RRR", - evmheumia_3 = "10000428RRR", - evmhesmia_3 = "10000429RRR", - evmhesmfa_3 = "1000042bRRR", - evmhoumia_3 = "1000042cRRR", - evmhosmia_3 = "1000042dRRR", - evmhosmfa_3 = "1000042fRRR", - evmwhssf_3 = "10000447RRR", - evmwlumi_3 = "10000448RRR", - evmwhumi_3 = "1000044cRRR", - evmwhsmi_3 = "1000044dRRR", - evmwhsmf_3 = "1000044fRRR", - evmwssf_3 = "10000453RRR", - evmwumi_3 = "10000458RRR", - evmwsmi_3 = "10000459RRR", - evmwsmf_3 = "1000045bRRR", - evmwhssfa_3 = "10000467RRR", - evmwlumia_3 = "10000468RRR", - evmwhumia_3 = "1000046cRRR", - evmwhsmia_3 = "1000046dRRR", - evmwhsmfa_3 = "1000046fRRR", - evmwssfa_3 = "10000473RRR", - evmwumia_3 = "10000478RRR", - evmwsmia_3 = "10000479RRR", - evmwsmfa_3 = "1000047bRRR", - evmra_2 = "100004c4RR", - evdivws_3 = "100004c6RRR", - evdivwu_3 = "100004c7RRR", - evmwssfaa_3 = "10000553RRR", - evmwumiaa_3 = "10000558RRR", - evmwsmiaa_3 = "10000559RRR", - evmwsmfaa_3 = "1000055bRRR", - evmwssfan_3 = "100005d3RRR", - evmwumian_3 = "100005d8RRR", - evmwsmian_3 = "100005d9RRR", - evmwsmfan_3 = "100005dbRRR", - evmergehilo_3 = "1000022eRRR", - evmergelohi_3 = "1000022fRRR", - evlhhesplatx_3 = "10000308RR0R", - evlhhesplat_2 = "10000309R2", - evlhhousplatx_3 = "1000030cRR0R", - evlhhousplat_2 = "1000030dR2", - evlhhossplatx_3 = "1000030eRR0R", - evlhhossplat_2 = "1000030fR2", - evlwwsplatx_3 = "10000318RR0R", - evlwwsplat_2 = "10000319R4", - evlwhsplatx_3 = "1000031cRR0R", - evlwhsplat_2 = "1000031dR4", - evaddusiaaw_2 = "100004c0RR", - evaddssiaaw_2 = "100004c1RR", - evsubfusiaaw_2 = "100004c2RR", - evsubfssiaaw_2 = "100004c3RR", - evaddumiaaw_2 = "100004c8RR", - evaddsmiaaw_2 = "100004c9RR", - evsubfumiaaw_2 = "100004caRR", - evsubfsmiaaw_2 = "100004cbRR", - evmheusiaaw_3 = "10000500RRR", - evmhessiaaw_3 = "10000501RRR", - evmhessfaaw_3 = "10000503RRR", - evmhousiaaw_3 = "10000504RRR", - evmhossiaaw_3 = "10000505RRR", - evmhossfaaw_3 = "10000507RRR", - evmheumiaaw_3 = "10000508RRR", - evmhesmiaaw_3 = "10000509RRR", - evmhesmfaaw_3 = "1000050bRRR", - evmhoumiaaw_3 = "1000050cRRR", - evmhosmiaaw_3 = "1000050dRRR", - evmhosmfaaw_3 = "1000050fRRR", - evmhegumiaa_3 = "10000528RRR", - evmhegsmiaa_3 = "10000529RRR", - evmhegsmfaa_3 = "1000052bRRR", - evmhogumiaa_3 = "1000052cRRR", - evmhogsmiaa_3 = "1000052dRRR", - evmhogsmfaa_3 = "1000052fRRR", - evmwlusiaaw_3 = "10000540RRR", - evmwlssiaaw_3 = "10000541RRR", - evmwlumiaaw_3 = "10000548RRR", - evmwlsmiaaw_3 = "10000549RRR", - evmheusianw_3 = "10000580RRR", - evmhessianw_3 = "10000581RRR", - evmhessfanw_3 = "10000583RRR", - evmhousianw_3 = "10000584RRR", - evmhossianw_3 = "10000585RRR", - evmhossfanw_3 = "10000587RRR", - evmheumianw_3 = "10000588RRR", - evmhesmianw_3 = "10000589RRR", - evmhesmfanw_3 = "1000058bRRR", - evmhoumianw_3 = "1000058cRRR", - evmhosmianw_3 = "1000058dRRR", - evmhosmfanw_3 = "1000058fRRR", - evmhegumian_3 = "100005a8RRR", - evmhegsmian_3 = "100005a9RRR", - evmhegsmfan_3 = "100005abRRR", - evmhogumian_3 = "100005acRRR", - evmhogsmian_3 = "100005adRRR", - evmhogsmfan_3 = "100005afRRR", - evmwlusianw_3 = "100005c0RRR", - evmwlssianw_3 = "100005c1RRR", - evmwlumianw_3 = "100005c8RRR", - evmwlsmianw_3 = "100005c9RRR", - - -- NYI: some 64 bit PowerPC and Book E instructions: - -- rldicl, rldicr, rldic, rldimi, rldcl, rldcr, sradi, 64 bit ext. add/sub, - -- extended addressing branches, cache management, loads and stores -} - --- Add mnemonics for "." variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "." then - local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2) - t[sub(k, 1, -3).."."..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - --- Add more branch mnemonics. -for cond,c in pairs(map_cond) do - local b1 = "b"..cond - local c1 = (c%4)*0x00010000 + (c < 4 and 0x01000000 or 0) - -- bX[l] - map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" - map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" - map_op[b1.."l_1"] = tohex(0x40800001 + c1).."K" - map_op[b1.."_2"] = tohex(0x40800000 + c1).."-XK" - map_op[b1.."y_2"] = tohex(0x40a00000 + c1).."-XK" - map_op[b1.."l_2"] = tohex(0x40800001 + c1).."-XK" - -- bXlr[l] - map_op[b1.."lr_0"] = tohex(0x4c800020 + c1) - map_op[b1.."lrl_0"] = tohex(0x4c800021 + c1) - map_op[b1.."ctr_0"] = tohex(0x4c800420 + c1) - map_op[b1.."ctrl_0"] = tohex(0x4c800421 + c1) - -- bXctr[l] - map_op[b1.."lr_1"] = tohex(0x4c800020 + c1).."-X" - map_op[b1.."lrl_1"] = tohex(0x4c800021 + c1).."-X" - map_op[b1.."ctr_1"] = tohex(0x4c800420 + c1).."-X" - map_op[b1.."ctrl_1"] = tohex(0x4c800421 + c1).."-X" -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_cr(expr) - local r = match(expr, "^cr([0-7])$") - if r then return tonumber(r) end - werror("bad condition register name `"..expr.."'") -end - -local function parse_cond(expr) - local r, cond = match(expr, "^4%*cr([0-7])%+(%w%w)$") - if r then - r = tonumber(r) - local c = map_cond[cond] - if c and c < 4 then return r*4+c end - end - werror("bad condition bit name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = tonumber(imm) - if n then - if n % 2^scale == 0 then - n = n / 2^scale - if signed then - if n >= 0 then - if n < 2^(bits-1) then return n*2^shift end - else - if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end - end - else - if n >= 0 and n <= 2^bits-1 then return n*2^shift end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^r([1-3]?[0-9])$") or - match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return r*65536 + parse_imm(imm, 16, 0, 0, true) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return r*65536 - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_u5disp(disp, scale) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return r*65536 + parse_imm(imm, 5, 11, scale, false) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) - return r*65536 - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n, rs = 1, 26 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions (rlwinm). - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "R" then - rs = rs - 5; op = op + parse_gpr(params[n]) * 2^rs; n = n + 1 - elseif p == "F" then - rs = rs - 5; op = op + parse_fpr(params[n]) * 2^rs; n = n + 1 - elseif p == "A" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 - elseif p == "S" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, true); n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "D" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "2" then - op = op + parse_u5disp(params[n], 1); n = n + 1 - elseif p == "4" then - op = op + parse_u5disp(params[n], 2); n = n + 1 - elseif p == "8" then - op = op + parse_u5disp(params[n], 3); n = n + 1 - elseif p == "C" then - rs = rs - 5; op = op + parse_cond(params[n]) * 2^rs; n = n + 1 - elseif p == "X" then - rs = rs - 5; op = op + parse_cr(params[n]) * 2^(rs+2); n = n + 1 - elseif p == "W" then - op = op + parse_cr(params[n]); n = n + 1 - elseif p == "G" then - op = op + parse_imm(params[n], 8, 12, 0, false); n = n + 1 - elseif p == "J" or p == "K" then - local mode, n, s = parse_label(params[n], false) - if p == "K" then n = n + 2048 end - waction("REL_"..mode, n, s, 1) - n = n + 1 - elseif p == "0" then - local mm = 2^rs - local t = op % mm - if ((op - t) / mm) % 32 == 0 then werror("cannot use r0") end - elseif p == "=" or p == "%" then - local mm = 2^(rs + (p == "%" and 5 or 0)) - local t = ((op - op % mm) / mm) % 32 - rs = rs - 5 - op = op + t * 2^rs - elseif p == "~" then - local mm = 2^rs - local t1l = op % mm - local t1h = (op - t1l) / mm - local t2l = t1h % 32 - local t2h = (t1h - t2l) / 32 - local t3l = t2h % 32 - op = ((t2h - t3l + t2l)*32 + t3l)*mm + t1l - elseif p == "-" then - rs = rs - 5 - elseif p == "." then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dasm_proto.h b/third_party/dynasm/dasm_proto.h deleted file mode 100644 index 3002811..0000000 --- a/third_party/dynasm/dasm_proto.h +++ /dev/null @@ -1,83 +0,0 @@ -/* -** DynASM encoding engine prototypes. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#ifndef _DASM_PROTO_H -#define _DASM_PROTO_H - -#include -#include - -#define DASM_IDENT "DynASM 1.3.0" -#define DASM_VERSION 10300 /* 1.3.0 */ - -#ifndef Dst_DECL -#define Dst_DECL dasm_State **Dst -#endif - -#ifndef Dst_REF -#define Dst_REF (*Dst) -#endif - -#ifndef DASM_FDEF -#define DASM_FDEF extern -#endif - -#ifndef DASM_M_GROW -#define DASM_M_GROW(ctx, t, p, sz, need) \ - do { \ - size_t _sz = (sz), _need = (need); \ - if (_sz < _need) { \ - if (_sz < 16) _sz = 16; \ - while (_sz < _need) _sz += _sz; \ - (p) = (t *)realloc((p), _sz); \ - if ((p) == NULL) exit(1); \ - (sz) = _sz; \ - } \ - } while(0) -#endif - -#ifndef DASM_M_FREE -#define DASM_M_FREE(ctx, p, sz) free(p) -#endif - -/* Internal DynASM encoder state. */ -typedef struct dasm_State dasm_State; - - -/* Initialize and free DynASM state. */ -DASM_FDEF void dasm_init(Dst_DECL, int maxsection); -DASM_FDEF void dasm_free(Dst_DECL); - -/* Setup global array. Must be called before dasm_setup(). */ -DASM_FDEF void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl); - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -DASM_FDEF void dasm_growpc(Dst_DECL, unsigned int maxpc); - -/* Setup encoder. */ -DASM_FDEF void dasm_setup(Dst_DECL, const void *actionlist); - -/* Feed encoder with actions. Calls are generated by pre-processor. */ -DASM_FDEF void dasm_put(Dst_DECL, int start, ...); - -/* Link sections and return the resulting size. */ -DASM_FDEF int dasm_link(Dst_DECL, size_t *szp); - -/* Encode sections into buffer. */ -DASM_FDEF int dasm_encode(Dst_DECL, void *buffer); - -/* Get PC label offset. */ -DASM_FDEF int dasm_getpclabel(Dst_DECL, unsigned int pc); - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -DASM_FDEF int dasm_checkstep(Dst_DECL, int secmatch); -#else -#define dasm_checkstep(a, b) 0 -#endif - - -#endif /* _DASM_PROTO_H */ diff --git a/third_party/dynasm/dasm_x64.lua b/third_party/dynasm/dasm_x64.lua deleted file mode 100644 index bae72ac..0000000 --- a/third_party/dynasm/dasm_x64.lua +++ /dev/null @@ -1,12 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x64 module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- --- This module just sets 64 bit mode for the combined x86/x64 module. --- All the interesting stuff is there. ------------------------------------------------------------------------------- - -x64 = true -- Using a global is an ugly, but effective solution. -return require("dasm_x86") diff --git a/third_party/dynasm/dasm_x86.h b/third_party/dynasm/dasm_x86.h deleted file mode 100644 index 7c6dcd3..0000000 --- a/third_party/dynasm/dasm_x86.h +++ /dev/null @@ -1,470 +0,0 @@ -/* -** DynASM x86 encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "x86" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. DASM_STOP must be 255. */ -enum { - DASM_DISP = 233, - DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB, - DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC, - DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN, - DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_VREG 0x15000000 -#define DASM_S_UNDEF_L 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned char *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs, mrm = 4; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - int action = *p++; - if (action < DASM_DISP) { - ofs++; - } else if (action <= DASM_REL_A) { - int n = va_arg(ap, int); - b[pos++] = n; - switch (action) { - case DASM_DISP: - if (n == 0) { if ((mrm&7) == 4) mrm = p[-2]; if ((mrm&7) != 5) break; } - case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; - case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */ - case DASM_IMM_D: ofs += 4; break; - case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob; - case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; - case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break; - case DASM_SPACE: p++; ofs += n; break; - case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */ - case DASM_VREG: CK((n&-8) == 0 && (n != 4 || (*p&1) == 0), RANGE_VREG); - if (*p++ == 1 && *p == DASM_DISP) mrm = n; continue; - } - mrm = 4; - } else { - int *pl, n; - switch (action) { - case DASM_REL_LG: - case DASM_IMM_LG: - n = *p++; pl = D->lglabels + n; - if (n <= 246) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl -= 246; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - ofs += 4; /* Maximum offset needed. */ - if (action == DASM_REL_LG || action == DASM_REL_PC) - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_ALIGN: - ofs += *p++; /* Maximum alignment needed (arg is 2**n-1). */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_EXTERN: p += 2; ofs += 4; break; - case DASM_ESC: p++; ofs++; break; - case DASM_MARK: mrm = p[-2]; break; - case DASM_SECTION: - n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n]; - case DASM_STOP: goto stop; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - int op, action = *p++; - switch (action) { - case DASM_REL_LG: p++; op = p[-3]; goto rel_pc; - case DASM_REL_PC: op = p[-2]; rel_pc: { - int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0); - if (shrink) { /* Shrinkable branch opcode? */ - int lofs, lpos = b[pos]; - if (lpos < 0) goto noshrink; /* Ext global? */ - lofs = *DASM_POS2PTR(D, lpos); - if (lpos > pos) { /* Fwd label: add cumulative section offsets. */ - int i; - for (i = secnum; i < DASM_POS2SEC(lpos); i++) - lofs += D->sections[i].ofs; - } else { - lofs -= ofs; /* Bkwd label: unfix offset. */ - } - lofs -= b[pos+1]; /* Short branch ok? */ - if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink; /* Yes. */ - else { noshrink: shrink = 0; } /* No, cannot shrink op. */ - } - b[pos+1] = shrink; - pos += 2; - break; - } - case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++; - case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W: - case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB: - case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break; - case DASM_LABEL_LG: p++; - case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */ - case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */ - case DASM_EXTERN: p += 2; break; - case DASM_ESC: p++; break; - case DASM_MARK: break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#define dasmb(x) *cp++ = (unsigned char)(x) -#ifndef DASM_ALIGNED_WRITES -#define dasmw(x) \ - do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0) -#define dasmd(x) \ - do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0) -#else -#define dasmw(x) do { dasmb(x); dasmb((x)>>8); } while (0) -#define dasmd(x) do { dasmw(x); dasmw((x)>>16); } while (0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - unsigned char *base = (unsigned char *)buffer; - unsigned char *cp = base; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - unsigned char *mark = NULL; - while (1) { - int action = *p++; - int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0; - switch (action) { - case DASM_DISP: if (!mark) mark = cp; { - unsigned char *mm = mark; - if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL; - if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7; - if (mrm != 5) { mm[-1] -= 0x80; break; } } - if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40; - } - case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break; - case DASM_IMM_DB: if (((n+128)&-256) == 0) { - db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb; - } else mark = NULL; - case DASM_IMM_D: wd: dasmd(n); break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL; - case DASM_IMM_W: dasmw(n); break; - case DASM_VREG: { int t = *p++; if (t >= 2) n<<=3; cp[-1] |= n; break; } - case DASM_REL_LG: p++; if (n >= 0) goto rel_pc; - b++; n = (int)(ptrdiff_t)D->globals[-n]; - case DASM_REL_A: rel_a: n -= (int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */ - case DASM_REL_PC: rel_pc: { - int shrink = *b++; - int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; } - n = *pb - ((int)(cp-base) + 4-shrink); - if (shrink == 0) goto wd; - if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb; - goto wb; - } - case DASM_IMM_LG: - p++; if (n < 0) { n = (int)(ptrdiff_t)D->globals[-n]; goto wd; } - case DASM_IMM_PC: { - int *pb = DASM_POS2PTR(D, n); - n = *pb < 0 ? pb[1] : (*pb + (int)(ptrdiff_t)base); - goto wd; - } - case DASM_LABEL_LG: { - int idx = *p++; - if (idx >= 10) - D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n)); - break; - } - case DASM_LABEL_PC: case DASM_SETLABEL: break; - case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; } - case DASM_ALIGN: - n = *p++; - while (((cp-base) & n)) *cp++ = 0x90; /* nop */ - break; - case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd; - case DASM_MARK: mark = cp; break; - case DASM_ESC: action = *p++; - default: *cp++ = action; break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_x86.lua b/third_party/dynasm/dasm_x86.lua deleted file mode 100644 index 3bebb83..0000000 --- a/third_party/dynasm/dasm_x86.lua +++ /dev/null @@ -1,1931 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x86/x64 module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - -local x64 = x64 - --- Module information: -local _info = { - arch = x64 and "x64" or "x86", - description = "DynASM x86/x64 module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub -local concat, sort = table.concat, table.sort - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - -- int arg, 1 buffer pos: - "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB", - -- action arg (1 byte), int arg, 1 buffer pos (reg/num): - "VREG", "SPACE", -- !x64: VREG support NYI. - -- ptrdiff_t arg, 1 buffer pos (address): !x64 - "SETLABEL", "REL_A", - -- action arg (1 byte) or int arg, 2 buffer pos (link, offset): - "REL_LG", "REL_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (link): - "IMM_LG", "IMM_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (offset): - "LABEL_LG", "LABEL_PC", - -- action arg (1 byte), 1 buffer pos (offset): - "ALIGN", - -- action args (2 bytes), no buffer pos. - "EXTERN", - -- action arg (1 byte), no buffer pos. - "ESC", - -- no action arg, no buffer pos. - "MARK", - -- action arg (1 byte), no buffer pos, terminal action: - "SECTION", - -- no args, no buffer pos, terminal action: - "STOP" -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number (dynamically generated below). -local map_action = {} --- First action number. Everything below does not need to be escaped. -local actfirst = 256-#action_names - --- Action list buffer and string (only used to remove dupes). -local actlist = {} -local actstr = "" - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Compute action numbers for action names. -for n,name in ipairs(action_names) do - local num = actfirst + n - 1 - map_action[name] = num -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - local last = actlist[nn] or 255 - actlist[nn] = nil -- Remove last byte. - if nn == 0 then nn = 1 end - out:write("static const unsigned char ", name, "[", nn, "] = {\n") - local s = " " - for n,b in ipairs(actlist) do - s = s..b.."," - if #s >= 75 then - assert(out:write(s, "\n")) - s = " " - end - end - out:write(s, last, "\n};\n\n") -- Add last byte back. -end - ------------------------------------------------------------------------------- - --- Add byte to action list. -local function wputxb(n) - assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, a, num) - wputxb(assert(map_action[action], "bad action name `"..action.."'")) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Add call to embedded DynASM C code. -local function wcall(func, args) - wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true) -end - --- Delete duplicate action list chunks. A tad slow, but so what. -local function dedupechunk(offset) - local al, as = actlist, actstr - local chunk = char(unpack(al, offset+1, #al)) - local orig = find(as, chunk, 1, true) - if orig then - actargs[1] = orig-1 -- Replace with original offset. - for i=offset+1,#al do al[i] = nil end -- Kill dupe. - else - actstr = as..chunk - end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - local offset = actargs[1] - if #actlist == offset then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - dedupechunk(offset) - wcall("put", actargs) -- Add call to dasm_put(). - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped byte. -local function wputb(n) - if n >= actfirst then waction("ESC") end -- Need to escape byte. - wputxb(n) -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 10 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end - local n = next_global - if n > 246 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=10,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=10,next_global-1 do - out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=10,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = -1 -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n < -256 then werror("too many extern labels") end - next_extern = n - 1 - t[name] = n - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("Extern labels:\n") - for i=1,-next_extern-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=1,-next_extern-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = {} -- Ext. register name -> int. name. -local map_reg_rev = {} -- Int. register name -> ext. name. -local map_reg_num = {} -- Int. register name -> register number. -local map_reg_opsize = {} -- Int. register name -> operand size. -local map_reg_valid_base = {} -- Int. register name -> valid base register? -local map_reg_valid_index = {} -- Int. register name -> valid index register? -local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex. -local reg_list = {} -- Canonical list of int. register names. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for _PTx macros). - -local addrsize = x64 and "q" or "d" -- Size for address operands. - --- Helper functions to fill register maps. -local function mkrmap(sz, cl, names) - local cname = format("@%s", sz) - reg_list[#reg_list+1] = cname - map_archdef[cl] = cname - map_reg_rev[cname] = cl - map_reg_num[cname] = -1 - map_reg_opsize[cname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[cname] = true - map_reg_valid_index[cname] = true - end - if names then - for n,name in ipairs(names) do - local iname = format("@%s%x", sz, n-1) - reg_list[#reg_list+1] = iname - map_archdef[name] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = n-1 - map_reg_opsize[iname] = sz - if sz == "b" and n > 4 then map_reg_needrex[iname] = false end - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - for i=0,(x64 and sz ~= "f") and 15 or 7 do - local needrex = sz == "b" and i > 3 - local iname = format("@%s%x%s", sz, i, needrex and "R" or "") - if needrex then map_reg_needrex[iname] = true end - local name - if sz == "o" then name = format("xmm%d", i) - elseif sz == "f" then name = format("st%d", i) - else name = format("r%d%s", i, sz == addrsize and "" or sz) end - map_archdef[name] = iname - if not map_reg_rev[iname] then - reg_list[#reg_list+1] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = i - map_reg_opsize[iname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - reg_list[#reg_list+1] = "" -end - --- Integer registers (qword, dword, word and byte sized). -if x64 then - mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"}) -end -mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}) -mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"}) -mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}) -map_reg_valid_index[map_archdef.esp] = false -if x64 then map_reg_valid_index[map_archdef.rsp] = false end -map_archdef["Ra"] = "@"..addrsize - --- FP registers (internally tword sized, but use "f" as operand size). -mkrmap("f", "Rf") - --- SSE registers (oword sized, but qword and dword accessible). -mkrmap("o", "xmm") - --- Operand size prefixes to codes. -local map_opsize = { - byte = "b", word = "w", dword = "d", qword = "q", oword = "o", tword = "t", - aword = addrsize, -} - --- Operand size code to number. -local map_opsizenum = { - b = 1, w = 2, d = 4, q = 8, o = 16, t = 10, -} - --- Operand size code to name. -local map_opsizename = { - b = "byte", w = "word", d = "dword", q = "qword", o = "oword", t = "tword", - f = "fpword", -} - --- Valid index register scale factors. -local map_xsc = { - ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3, -} - --- Condition codes. -local map_cc = { - o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7, - s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15, - c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7, - pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15, -} - - --- Reverse defines for registers. -function _M.revdef(s) - return gsub(s, "@%w+", map_reg_rev) -end - --- Dump register names and numbers -local function dumpregs(out) - out:write("Register names, sizes and internal numbers:\n") - for _,reg in ipairs(reg_list) do - if reg == "" then - out:write("\n") - else - local name = map_reg_rev[reg] - local num = map_reg_num[reg] - local opsize = map_opsizename[map_reg_opsize[reg]] - out:write(format(" %-5s %-8s %s\n", name, opsize, - num < 0 and "(variable)" or num)) - end - end -end - ------------------------------------------------------------------------------- - --- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC). -local function wputlabel(aprefix, imm, num) - if type(imm) == "number" then - if imm < 0 then - waction("EXTERN") - wputxb(aprefix == "IMM_" and 0 or 1) - imm = -imm-1 - else - waction(aprefix.."LG", nil, num); - end - wputxb(imm) - else - waction(aprefix.."PC", imm, num) - end -end - --- Put signed byte or arg. -local function wputsbarg(n) - if type(n) == "number" then - if n < -128 or n > 127 then - werror("signed immediate byte out of range") - end - if n < 0 then n = n + 256 end - wputb(n) - else waction("IMM_S", n) end -end - --- Put unsigned byte or arg. -local function wputbarg(n) - if type(n) == "number" then - if n < 0 or n > 255 then - werror("unsigned immediate byte out of range") - end - wputb(n) - else waction("IMM_B", n) end -end - --- Put unsigned word or arg. -local function wputwarg(n) - if type(n) == "number" then - if n < 0 or n > 65535 then - werror("unsigned immediate word out of range") - end - local r = n%256; n = (n-r)/256; wputb(r); wputb(n); - else waction("IMM_W", n) end -end - --- Put signed or unsigned dword or arg. -local function wputdarg(n) - local tn = type(n) - if tn == "number" then - if n < 0 then n = n + 4294967296 end - local r = n%256; n = (n-r)/256; wputb(r); - r = n%256; n = (n-r)/256; wputb(r); - r = n%256; n = (n-r)/256; wputb(r); wputb(n); - elseif tn == "table" then - wputlabel("IMM_", n[1], 1) - else - waction("IMM_D", n) - end -end - --- Put operand-size dependent number or arg (defaults to dword). -local function wputszarg(sz, n) - if not sz or sz == "d" or sz == "q" then wputdarg(n) - elseif sz == "w" then wputwarg(n) - elseif sz == "b" then wputbarg(n) - elseif sz == "s" then wputsbarg(n) - else werror("bad operand size") end -end - --- Put multi-byte opcode with operand-size dependent modifications. -local function wputop(sz, op, rex) - local r - if rex ~= 0 and not x64 then werror("bad operand size") end - if sz == "w" then wputb(102) end - -- Needs >32 bit numbers, but only for crc32 eax, word [ebx] - if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end - if op >= 16777216 then r = op % 16777216 wputb((op-r) / 16777216) op = r end - if op >= 65536 then - if rex ~= 0 then - local opc3 = op - op % 256 - if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then - wputb(64 + rex % 16); rex = 0 - end - end - r = op % 65536 wputb((op-r) / 65536) op = r - end - if op >= 256 then - r = op % 256 - local b = (op-r) / 256 - if b == 15 and rex ~= 0 then wputb(64 + rex % 16); rex = 0 end - wputb(b) - op = r - end - if rex ~= 0 then wputb(64 + rex % 16) end - if sz == "b" then op = op - 1 end - wputb(op) -end - --- Put ModRM or SIB formatted byte. -local function wputmodrm(m, s, rm, vs, vrm) - assert(m < 4 and s < 16 and rm < 16, "bad modrm operands") - wputb(64*m + 8*(s%8) + (rm%8)) -end - --- Put ModRM/SIB plus optional displacement. -local function wputmrmsib(t, imark, s, vsreg) - local vreg, vxreg - local reg, xreg = t.reg, t.xreg - if reg and reg < 0 then reg = 0; vreg = t.vreg end - if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end - if s < 0 then s = 0 end - - -- Register mode. - if sub(t.mode, 1, 1) == "r" then - wputmodrm(3, s, reg) - if vsreg then waction("VREG", vsreg); wputxb(2) end - if vreg then waction("VREG", vreg); wputxb(0) end - return - end - - local disp = t.disp - local tdisp = type(disp) - -- No base register? - if not reg then - local riprel = false - if xreg then - -- Indexed mode with index register only. - -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp) - wputmodrm(0, s, 4) - if imark == "I" then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - wputmodrm(t.xsc, xreg, 5) - if vxreg then waction("VREG", vxreg); wputxb(3) end - else - -- Pure 32 bit displacement. - if x64 and tdisp ~= "table" then - wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp) - if imark == "I" then waction("MARK") end - wputmodrm(0, 4, 5) - else - riprel = x64 - wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp) - if imark == "I" then waction("MARK") end - end - if vsreg then waction("VREG", vsreg); wputxb(2) end - end - if riprel then -- Emit rip-relative displacement. - if match("UWSiI", imark) then - werror("NYI: rip-relative displacement followed by immediate") - end - -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f. - wputlabel("REL_", disp[1], 2) - else - wputdarg(disp) - end - return - end - - local m - if tdisp == "number" then -- Check displacement size at assembly time. - if disp == 0 and (reg%8) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too) - if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0] - elseif disp >= -128 and disp <= 127 then m = 1 - else m = 2 end - elseif tdisp == "table" then - m = 2 - end - - -- Index register present or esp as base register: need SIB encoding. - if xreg or (reg%8) == 4 then - wputmodrm(m or 2, s, 4) -- ModRM. - if m == nil or imark == "I" then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB. - if vxreg then waction("VREG", vxreg); wputxb(3) end - if vreg then waction("VREG", vreg); wputxb(1) end - else - wputmodrm(m or 2, s, reg) -- ModRM. - if (imark == "I" and (m == 1 or m == 2)) or - (m == nil and (vsreg or vreg)) then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - if vreg then waction("VREG", vreg); wputxb(1) end - end - - -- Put displacement. - if m == 1 then wputsbarg(disp) - elseif m == 2 then wputdarg(disp) - elseif m == nil then waction("DISP", disp) end -end - ------------------------------------------------------------------------------- - --- Return human-readable operand mode string. -local function opmodestr(op, args) - local m = {} - for i=1,#args do - local a = args[i] - m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?") - end - return op.." "..concat(m, ",") -end - --- Convert number to valid integer or nil. -local function toint(expr) - local n = tonumber(expr) - if n then - if n % 1 ~= 0 or n < -2147483648 or n > 4294967295 then - werror("bad integer number `"..expr.."'") - end - return n - end -end - --- Parse immediate expression. -local function immexpr(expr) - -- &expr (pointer) - if sub(expr, 1, 1) == "&" then - return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2)) - end - - local prefix = sub(expr, 1, 2) - -- =>expr (pc label reference) - if prefix == "=>" then - return "iJ", sub(expr, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "iJ", map_global[sub(expr, 3)] - end - - -- [<>][1-9] (local label reference) - local dir, lnum = match(expr, "^([<>])([1-9])$") - if dir then -- Fwd: 247-255, Bkwd: 1-9. - return "iJ", lnum + (dir == ">" and 246 or 0) - end - - local extname = match(expr, "^extern%s+(%S+)$") - if extname then - return "iJ", map_extern[extname] - end - - -- expr (interpreted as immediate) - return "iI", expr -end - --- Parse displacement expression: +-num, +-expr, +-opsize*num -local function dispexpr(expr) - local disp = expr == "" and 0 or toint(expr) - if disp then return disp end - local c, dispt = match(expr, "^([+-])%s*(.+)$") - if c == "+" then - expr = dispt - elseif not c then - werror("bad displacement expression `"..expr.."'") - end - local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$") - local ops, imm = map_opsize[opsize], toint(tailops) - if ops and imm then - if c == "-" then imm = -imm end - return imm*map_opsizenum[ops] - end - local mode, iexpr = immexpr(dispt) - if mode == "iJ" then - if c == "-" then werror("cannot invert label reference") end - return { iexpr } - end - return expr -- Need to return original signed expression. -end - --- Parse register or type expression. -local function rtexpr(expr) - if not expr then return end - local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - local rnum = map_reg_num[reg] - if not rnum then - werror("type `"..(tname or expr).."' needs a register override") - end - if not map_reg_valid_base[reg] then - werror("bad base register override `"..(map_reg_rev[reg] or reg).."'") - end - return reg, rnum, tp - end - return expr, map_reg_num[expr] -end - --- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }. -local function parseoperand(param) - local t = {} - - local expr = param - local opsize, tailops = match(param, "^(%w+)%s*(.+)$") - if opsize then - t.opsize = map_opsize[opsize] - if t.opsize then expr = tailops end - end - - local br = match(expr, "^%[%s*(.-)%s*%]$") - repeat - if br then - t.mode = "xm" - - -- [disp] - t.disp = toint(br) - if t.disp then - t.mode = x64 and "xm" or "xmO" - break - end - - -- [reg...] - local tp - local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if not t.reg then - -- [expr] - t.mode = x64 and "xm" or "xmO" - t.disp = dispexpr("+"..br) - break - end - - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - - -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr] - local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$") - if xsc then - if not map_reg_valid_index[reg] then - werror("bad index register `"..map_reg_rev[reg].."'") - end - t.xsc = map_xsc[xsc] - t.xreg = t.reg - t.vxreg = t.vreg - t.reg = nil - t.vreg = nil - t.disp = dispexpr(tailsc) - break - end - if not map_reg_valid_base[reg] then - werror("bad base register `"..map_reg_rev[reg].."'") - end - - -- [reg] or [reg+-disp] - t.disp = toint(tailr) or (tailr == "" and 0) - if t.disp then break end - - -- [reg+xreg...] - local xreg, tailx = match(tailr, "^+%s*([@%w_:]+)%s*(.*)$") - xreg, t.xreg, tp = rtexpr(xreg) - if not t.xreg then - -- [reg+-expr] - t.disp = dispexpr(tailr) - break - end - if not map_reg_valid_index[xreg] then - werror("bad index register `"..map_reg_rev[xreg].."'") - end - - if t.xreg == -1 then - t.vxreg, tailx = match(tailx, "^(%b())(.*)$") - if not t.vxreg then werror("bad variable register expression") end - end - - -- [reg+xreg*xsc...] - local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$") - if xsc then - t.xsc = map_xsc[xsc] - tailx = tailsc - end - - -- [...] or [...+-disp] or [...+-expr] - t.disp = dispexpr(tailx) - else - -- imm or opsize*imm - local imm = toint(expr) - if not imm and sub(expr, 1, 1) == "*" and t.opsize then - imm = toint(sub(expr, 2)) - if imm then - imm = imm * map_opsizenum[t.opsize] - t.opsize = nil - end - end - if imm then - if t.opsize then werror("bad operand size override") end - local m = "i" - if imm == 1 then m = m.."1" end - if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end - if imm >= -128 and imm <= 127 then m = m.."S" end - t.imm = imm - t.mode = m - break - end - - local tp - local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if t.reg then - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - -- reg - if tailr == "" then - if t.opsize then werror("bad operand size override") end - t.opsize = map_reg_opsize[reg] - if t.opsize == "f" then - t.mode = t.reg == 0 and "fF" or "f" - else - if reg == "@w4" or (x64 and reg == "@d4") then - wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'")) - end - t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm") - end - t.needrex = map_reg_needrex[reg] - break - end - - -- type[idx], type[idx].field, type->field -> [reg+offset_expr] - if not tp then werror("bad operand `"..param.."'") end - t.mode = "xm" - t.disp = format(tp.ctypefmt, tailr) - else - t.mode, t.imm = immexpr(expr) - if sub(t.mode, -1) == "J" then - if t.opsize and t.opsize ~= addrsize then - werror("bad operand size override") - end - t.opsize = addrsize - end - end - end - until true - return t -end - ------------------------------------------------------------------------------- --- x86 Template String Description --- =============================== --- --- Each template string is a list of [match:]pattern pairs, --- separated by "|". The first match wins. No match means a --- bad or unsupported combination of operand modes or sizes. --- --- The match part and the ":" is omitted if the operation has --- no operands. Otherwise the first N characters are matched --- against the mode strings of each of the N operands. --- --- The mode string for each operand type is (see parseoperand()): --- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl --- FP register: "f", +"F" for st0 --- Index operand: "xm", +"O" for [disp] (pure offset) --- Immediate: "i", +"S" for signed 8 bit, +"1" for 1, --- +"I" for arg, +"P" for pointer --- Any: +"J" for valid jump targets --- --- So a match character "m" (mixed) matches both an integer register --- and an index operand (to be encoded with the ModRM/SIB scheme). --- But "r" matches only a register and "x" only an index operand --- (e.g. for FP memory access operations). --- --- The operand size match string starts right after the mode match --- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty. --- The effective data size of the operation is matched against this list. --- --- If only the regular "b", "w", "d", "q", "t" operand sizes are --- present, then all operands must be the same size. Unspecified sizes --- are ignored, but at least one operand must have a size or the pattern --- won't match (use the "byte", "word", "dword", "qword", "tword" --- operand size overrides. E.g.: mov dword [eax], 1). --- --- If the list has a "1" or "2" prefix, the operand size is taken --- from the respective operand and any other operand sizes are ignored. --- If the list contains only ".", all operand sizes are ignored. --- If the list has a "/" prefix, the concatenated (mixed) operand sizes --- are compared to the match. --- --- E.g. "rrdw" matches for either two dword registers or two word --- registers. "Fx2dq" matches an st0 operand plus an index operand --- pointing to a dword (float) or qword (double). --- --- Every character after the ":" is part of the pattern string: --- Hex chars are accumulated to form the opcode (left to right). --- "n" disables the standard opcode mods --- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q") --- "X" Force REX.W. --- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode. --- "m"/"M" generates ModRM/SIB from the 1st/2nd operand. --- The spare 3 bits are either filled with the last hex digit or --- the result from a previous "r"/"R". The opcode is restored. --- --- All of the following characters force a flush of the opcode: --- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand. --- "S" stores a signed 8 bit immediate from the last operand. --- "U" stores an unsigned 8 bit immediate from the last operand. --- "W" stores an unsigned 16 bit immediate from the last operand. --- "i" stores an operand sized immediate from the last operand. --- "I" dito, but generates an action code to optionally modify --- the opcode (+2) for a signed 8 bit immediate. --- "J" generates one of the REL action codes from the last operand. --- ------------------------------------------------------------------------------- - --- Template strings for x86 instructions. Ordered by first opcode byte. --- Unimplemented opcodes (deliberate omissions) are marked with *. -local map_op = { - -- 00-05: add... - -- 06: *push es - -- 07: *pop es - -- 08-0D: or... - -- 0E: *push cs - -- 0F: two byte opcode prefix - -- 10-15: adc... - -- 16: *push ss - -- 17: *pop ss - -- 18-1D: sbb... - -- 1E: *push ds - -- 1F: *pop ds - -- 20-25: and... - es_0 = "26", - -- 27: *daa - -- 28-2D: sub... - cs_0 = "2E", - -- 2F: *das - -- 30-35: xor... - ss_0 = "36", - -- 37: *aaa - -- 38-3D: cmp... - ds_0 = "3E", - -- 3F: *aas - inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m", - dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m", - push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or - "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i", - pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m", - -- 60: *pusha, *pushad, *pushaw - -- 61: *popa, *popad, *popaw - -- 62: *bound rdw,x - -- 63: x86: *arpl mw,rw - movsxd_2 = x64 and "rm/qd:63rM", - fs_0 = "64", - gs_0 = "65", - o16_0 = "66", - a16_0 = not x64 and "67" or nil, - a32_0 = x64 and "67", - -- 68: push idw - -- 69: imul rdw,mdw,idw - -- 6A: push ib - -- 6B: imul rdw,mdw,S - -- 6C: *insb - -- 6D: *insd, *insw - -- 6E: *outsb - -- 6F: *outsd, *outsw - -- 70-7F: jcc lb - -- 80: add... mb,i - -- 81: add... mdw,i - -- 82: *undefined - -- 83: add... mdw,S - test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi", - -- 86: xchg rb,mb - -- 87: xchg rdw,mdw - -- 88: mov mb,r - -- 89: mov mdw,r - -- 8A: mov r,mb - -- 8B: mov r,mdw - -- 8C: *mov mdw,seg - lea_2 = "rx1dq:8DrM", - -- 8E: *mov seg,mdw - -- 8F: pop mdw - nop_0 = "90", - xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm", - cbw_0 = "6698", - cwde_0 = "98", - cdqe_0 = "4898", - cwd_0 = "6699", - cdq_0 = "99", - cqo_0 = "4899", - -- 9A: *call iw:idw - wait_0 = "9B", - fwait_0 = "9B", - pushf_0 = "9C", - pushfd_0 = not x64 and "9C", - pushfq_0 = x64 and "9C", - popf_0 = "9D", - popfd_0 = not x64 and "9D", - popfq_0 = x64 and "9D", - sahf_0 = "9E", - lahf_0 = "9F", - mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi", - movsb_0 = "A4", - movsw_0 = "66A5", - movsd_0 = "A5", - cmpsb_0 = "A6", - cmpsw_0 = "66A7", - cmpsd_0 = "A7", - -- A8: test Rb,i - -- A9: test Rdw,i - stosb_0 = "AA", - stosw_0 = "66AB", - stosd_0 = "AB", - lodsb_0 = "AC", - lodsw_0 = "66AD", - lodsd_0 = "AD", - scasb_0 = "AE", - scasw_0 = "66AF", - scasd_0 = "AF", - -- B0-B7: mov rb,i - -- B8-BF: mov rdw,i - -- C0: rol... mb,i - -- C1: rol... mdw,i - ret_1 = "i.:nC2W", - ret_0 = "C3", - -- C4: *les rdw,mq - -- C5: *lds rdw,mq - -- C6: mov mb,i - -- C7: mov mdw,i - -- C8: *enter iw,ib - leave_0 = "C9", - -- CA: *retf iw - -- CB: *retf - int3_0 = "CC", - int_1 = "i.:nCDU", - into_0 = "CE", - -- CF: *iret - -- D0: rol... mb,1 - -- D1: rol... mdw,1 - -- D2: rol... mb,cl - -- D3: rol... mb,cl - -- D4: *aam ib - -- D5: *aad ib - -- D6: *salc - -- D7: *xlat - -- D8-DF: floating point ops - -- E0: *loopne - -- E1: *loope - -- E2: *loop - -- E3: *jcxz, *jecxz - -- E4: *in Rb,ib - -- E5: *in Rdw,ib - -- E6: *out ib,Rb - -- E7: *out ib,Rdw - call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J", - jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB - -- EA: *jmp iw:idw - -- EB: jmp ib - -- EC: *in Rb,dx - -- ED: *in Rdw,dx - -- EE: *out dx,Rb - -- EF: *out dx,Rdw - -- F0: *lock - int1_0 = "F1", - repne_0 = "F2", - repnz_0 = "F2", - rep_0 = "F3", - repe_0 = "F3", - repz_0 = "F3", - -- F4: *hlt - cmc_0 = "F5", - -- F6: test... mb,i; div... mb - -- F7: test... mdw,i; div... mdw - clc_0 = "F8", - stc_0 = "F9", - -- FA: *cli - cld_0 = "FC", - std_0 = "FD", - -- FE: inc... mb - -- FF: inc... mdw - - -- misc ops - not_1 = "m:F72m", - neg_1 = "m:F73m", - mul_1 = "m:F74m", - imul_1 = "m:F75m", - div_1 = "m:F76m", - idiv_1 = "m:F77m", - - imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi", - imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi", - - movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:", - movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:", - - bswap_1 = "rqd:0FC8r", - bsf_2 = "rmqdw:0FBCrM", - bsr_2 = "rmqdw:0FBDrM", - bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU", - btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU", - btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU", - bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU", - - rdtsc_0 = "0F31", -- P1+ - cpuid_0 = "0FA2", -- P1+ - - -- floating point ops - fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m", - fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m", - fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m", - - fpop_0 = "DDD8", -- Alias for fstp st0. - - fist_1 = "xw:nDF2m|xd:DB2m", - fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m", - fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m", - - fxch_0 = "D9C9", - fxch_1 = "ff:D9C8r", - fxch_2 = "fFf:D9C8r|Fff:D9C8R", - - fucom_1 = "ff:DDE0r", - fucom_2 = "Fff:DDE0R", - fucomp_1 = "ff:DDE8r", - fucomp_2 = "Fff:DDE8R", - fucomi_1 = "ff:DBE8r", -- P6+ - fucomi_2 = "Fff:DBE8R", -- P6+ - fucomip_1 = "ff:DFE8r", -- P6+ - fucomip_2 = "Fff:DFE8R", -- P6+ - fcomi_1 = "ff:DBF0r", -- P6+ - fcomi_2 = "Fff:DBF0R", -- P6+ - fcomip_1 = "ff:DFF0r", -- P6+ - fcomip_2 = "Fff:DFF0R", -- P6+ - fucompp_0 = "DAE9", - fcompp_0 = "DED9", - - fldcw_1 = "xw:nD95m", - fstcw_1 = "xw:n9BD97m", - fnstcw_1 = "xw:nD97m", - fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m", - fnstsw_1 = "Rw:nDFE0|xw:nDD7m", - fclex_0 = "9BDBE2", - fnclex_0 = "DBE2", - - fnop_0 = "D9D0", - -- D9D1-D9DF: unassigned - - fchs_0 = "D9E0", - fabs_0 = "D9E1", - -- D9E2: unassigned - -- D9E3: unassigned - ftst_0 = "D9E4", - fxam_0 = "D9E5", - -- D9E6: unassigned - -- D9E7: unassigned - fld1_0 = "D9E8", - fldl2t_0 = "D9E9", - fldl2e_0 = "D9EA", - fldpi_0 = "D9EB", - fldlg2_0 = "D9EC", - fldln2_0 = "D9ED", - fldz_0 = "D9EE", - -- D9EF: unassigned - - f2xm1_0 = "D9F0", - fyl2x_0 = "D9F1", - fptan_0 = "D9F2", - fpatan_0 = "D9F3", - fxtract_0 = "D9F4", - fprem1_0 = "D9F5", - fdecstp_0 = "D9F6", - fincstp_0 = "D9F7", - fprem_0 = "D9F8", - fyl2xp1_0 = "D9F9", - fsqrt_0 = "D9FA", - fsincos_0 = "D9FB", - frndint_0 = "D9FC", - fscale_0 = "D9FD", - fsin_0 = "D9FE", - fcos_0 = "D9FF", - - -- SSE, SSE2 - andnpd_2 = "rmo:660F55rM", - andnps_2 = "rmo:0F55rM", - andpd_2 = "rmo:660F54rM", - andps_2 = "rmo:0F54rM", - clflush_1 = "x.:0FAE7m", - cmppd_3 = "rmio:660FC2rMU", - cmpps_3 = "rmio:0FC2rMU", - cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:", - cmpss_3 = "rrio:F30FC2rMU|rxi/od:", - comisd_2 = "rro:660F2FrM|rx/oq:", - comiss_2 = "rro:0F2FrM|rx/od:", - cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:", - cvtdq2ps_2 = "rmo:0F5BrM", - cvtpd2dq_2 = "rmo:F20FE6rM", - cvtpd2ps_2 = "rmo:660F5ArM", - cvtpi2pd_2 = "rx/oq:660F2ArM", - cvtpi2ps_2 = "rx/oq:0F2ArM", - cvtps2dq_2 = "rmo:660F5BrM", - cvtps2pd_2 = "rro:0F5ArM|rx/oq:", - cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:", - cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:", - cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM", - cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM", - cvtss2sd_2 = "rro:F30F5ArM|rx/od:", - cvtss2si_2 = "rr/do:F20F2CrM|rr/qo:|rxd:|rx/qd:", - cvttpd2dq_2 = "rmo:660FE6rM", - cvttps2dq_2 = "rmo:F30F5BrM", - cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:", - cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:", - ldmxcsr_1 = "xd:0FAE2m", - lfence_0 = "0FAEE8", - maskmovdqu_2 = "rro:660FF7rM", - mfence_0 = "0FAEF0", - movapd_2 = "rmo:660F28rM|mro:660F29Rm", - movaps_2 = "rmo:0F28rM|mro:0F29Rm", - movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:", - movdqa_2 = "rmo:660F6FrM|mro:660F7FRm", - movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm", - movhlps_2 = "rro:0F12rM", - movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm", - movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm", - movlhps_2 = "rro:0F16rM", - movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm", - movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm", - movmskpd_2 = "rr/do:660F50rM", - movmskps_2 = "rr/do:0F50rM", - movntdq_2 = "xro:660FE7Rm", - movnti_2 = "xrqd:0FC3Rm", - movntpd_2 = "xro:660F2BRm", - movntps_2 = "xro:0F2BRm", - movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm", - movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm", - movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm", - movupd_2 = "rmo:660F10rM|mro:660F11Rm", - movups_2 = "rmo:0F10rM|mro:0F11Rm", - orpd_2 = "rmo:660F56rM", - orps_2 = "rmo:0F56rM", - packssdw_2 = "rmo:660F6BrM", - packsswb_2 = "rmo:660F63rM", - packuswb_2 = "rmo:660F67rM", - paddb_2 = "rmo:660FFCrM", - paddd_2 = "rmo:660FFErM", - paddq_2 = "rmo:660FD4rM", - paddsb_2 = "rmo:660FECrM", - paddsw_2 = "rmo:660FEDrM", - paddusb_2 = "rmo:660FDCrM", - paddusw_2 = "rmo:660FDDrM", - paddw_2 = "rmo:660FFDrM", - pand_2 = "rmo:660FDBrM", - pandn_2 = "rmo:660FDFrM", - pause_0 = "F390", - pavgb_2 = "rmo:660FE0rM", - pavgw_2 = "rmo:660FE3rM", - pcmpeqb_2 = "rmo:660F74rM", - pcmpeqd_2 = "rmo:660F76rM", - pcmpeqw_2 = "rmo:660F75rM", - pcmpgtb_2 = "rmo:660F64rM", - pcmpgtd_2 = "rmo:660F66rM", - pcmpgtw_2 = "rmo:660F65rM", - pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nrMU", -- Mem op: SSE4.1 only. - pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:", - pmaddwd_2 = "rmo:660FF5rM", - pmaxsw_2 = "rmo:660FEErM", - pmaxub_2 = "rmo:660FDErM", - pminsw_2 = "rmo:660FEArM", - pminub_2 = "rmo:660FDArM", - pmovmskb_2 = "rr/do:660FD7rM", - pmulhuw_2 = "rmo:660FE4rM", - pmulhw_2 = "rmo:660FE5rM", - pmullw_2 = "rmo:660FD5rM", - pmuludq_2 = "rmo:660FF4rM", - por_2 = "rmo:660FEBrM", - prefetchnta_1 = "xb:n0F180m", - prefetcht0_1 = "xb:n0F181m", - prefetcht1_1 = "xb:n0F182m", - prefetcht2_1 = "xb:n0F183m", - psadbw_2 = "rmo:660FF6rM", - pshufd_3 = "rmio:660F70rMU", - pshufhw_3 = "rmio:F30F70rMU", - pshuflw_3 = "rmio:F20F70rMU", - pslld_2 = "rmo:660FF2rM|rio:660F726mU", - pslldq_2 = "rio:660F737mU", - psllq_2 = "rmo:660FF3rM|rio:660F736mU", - psllw_2 = "rmo:660FF1rM|rio:660F716mU", - psrad_2 = "rmo:660FE2rM|rio:660F724mU", - psraw_2 = "rmo:660FE1rM|rio:660F714mU", - psrld_2 = "rmo:660FD2rM|rio:660F722mU", - psrldq_2 = "rio:660F733mU", - psrlq_2 = "rmo:660FD3rM|rio:660F732mU", - psrlw_2 = "rmo:660FD1rM|rio:660F712mU", - psubb_2 = "rmo:660FF8rM", - psubd_2 = "rmo:660FFArM", - psubq_2 = "rmo:660FFBrM", - psubsb_2 = "rmo:660FE8rM", - psubsw_2 = "rmo:660FE9rM", - psubusb_2 = "rmo:660FD8rM", - psubusw_2 = "rmo:660FD9rM", - psubw_2 = "rmo:660FF9rM", - punpckhbw_2 = "rmo:660F68rM", - punpckhdq_2 = "rmo:660F6ArM", - punpckhqdq_2 = "rmo:660F6DrM", - punpckhwd_2 = "rmo:660F69rM", - punpcklbw_2 = "rmo:660F60rM", - punpckldq_2 = "rmo:660F62rM", - punpcklqdq_2 = "rmo:660F6CrM", - punpcklwd_2 = "rmo:660F61rM", - pxor_2 = "rmo:660FEFrM", - rcpps_2 = "rmo:0F53rM", - rcpss_2 = "rro:F30F53rM|rx/od:", - rsqrtps_2 = "rmo:0F52rM", - rsqrtss_2 = "rmo:F30F52rM", - sfence_0 = "0FAEF8", - shufpd_3 = "rmio:660FC6rMU", - shufps_3 = "rmio:0FC6rMU", - stmxcsr_1 = "xd:0FAE3m", - ucomisd_2 = "rro:660F2ErM|rx/oq:", - ucomiss_2 = "rro:0F2ErM|rx/od:", - unpckhpd_2 = "rmo:660F15rM", - unpckhps_2 = "rmo:0F15rM", - unpcklpd_2 = "rmo:660F14rM", - unpcklps_2 = "rmo:0F14rM", - xorpd_2 = "rmo:660F57rM", - xorps_2 = "rmo:0F57rM", - - -- SSE3 ops - fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m", - addsubpd_2 = "rmo:660FD0rM", - addsubps_2 = "rmo:F20FD0rM", - haddpd_2 = "rmo:660F7CrM", - haddps_2 = "rmo:F20F7CrM", - hsubpd_2 = "rmo:660F7DrM", - hsubps_2 = "rmo:F20F7DrM", - lddqu_2 = "rxo:F20FF0rM", - movddup_2 = "rmo:F20F12rM", - movshdup_2 = "rmo:F30F16rM", - movsldup_2 = "rmo:F30F12rM", - - -- SSSE3 ops - pabsb_2 = "rmo:660F381CrM", - pabsd_2 = "rmo:660F381ErM", - pabsw_2 = "rmo:660F381DrM", - palignr_3 = "rmio:660F3A0FrMU", - phaddd_2 = "rmo:660F3802rM", - phaddsw_2 = "rmo:660F3803rM", - phaddw_2 = "rmo:660F3801rM", - phsubd_2 = "rmo:660F3806rM", - phsubsw_2 = "rmo:660F3807rM", - phsubw_2 = "rmo:660F3805rM", - pmaddubsw_2 = "rmo:660F3804rM", - pmulhrsw_2 = "rmo:660F380BrM", - pshufb_2 = "rmo:660F3800rM", - psignb_2 = "rmo:660F3808rM", - psignd_2 = "rmo:660F380ArM", - psignw_2 = "rmo:660F3809rM", - - -- SSE4.1 ops - blendpd_3 = "rmio:660F3A0DrMU", - blendps_3 = "rmio:660F3A0CrMU", - blendvpd_3 = "rmRo:660F3815rM", - blendvps_3 = "rmRo:660F3814rM", - dppd_3 = "rmio:660F3A41rMU", - dpps_3 = "rmio:660F3A40rMU", - extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU", - insertps_3 = "rrio:660F3A41rMU|rxi/od:", - movntdqa_2 = "rmo:660F382ArM", - mpsadbw_3 = "rmio:660F3A42rMU", - packusdw_2 = "rmo:660F382BrM", - pblendvb_3 = "rmRo:660F3810rM", - pblendw_3 = "rmio:660F3A0ErMU", - pcmpeqq_2 = "rmo:660F3829rM", - pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:", - pextrd_3 = "mri/do:660F3A16RmU", - pextrq_3 = "mri/qo:660F3A16RmU", - -- pextrw is SSE2, mem operand is SSE4.1 only - phminposuw_2 = "rmo:660F3841rM", - pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:", - pinsrd_3 = "rmi/od:660F3A22rMU", - pinsrq_3 = "rmi/oq:660F3A22rXMU", - pmaxsb_2 = "rmo:660F383CrM", - pmaxsd_2 = "rmo:660F383DrM", - pmaxud_2 = "rmo:660F383FrM", - pmaxuw_2 = "rmo:660F383ErM", - pminsb_2 = "rmo:660F3838rM", - pminsd_2 = "rmo:660F3839rM", - pminud_2 = "rmo:660F383BrM", - pminuw_2 = "rmo:660F383ArM", - pmovsxbd_2 = "rro:660F3821rM|rx/od:", - pmovsxbq_2 = "rro:660F3822rM|rx/ow:", - pmovsxbw_2 = "rro:660F3820rM|rx/oq:", - pmovsxdq_2 = "rro:660F3825rM|rx/oq:", - pmovsxwd_2 = "rro:660F3823rM|rx/oq:", - pmovsxwq_2 = "rro:660F3824rM|rx/od:", - pmovzxbd_2 = "rro:660F3831rM|rx/od:", - pmovzxbq_2 = "rro:660F3832rM|rx/ow:", - pmovzxbw_2 = "rro:660F3830rM|rx/oq:", - pmovzxdq_2 = "rro:660F3835rM|rx/oq:", - pmovzxwd_2 = "rro:660F3833rM|rx/oq:", - pmovzxwq_2 = "rro:660F3834rM|rx/od:", - pmuldq_2 = "rmo:660F3828rM", - pmulld_2 = "rmo:660F3840rM", - ptest_2 = "rmo:660F3817rM", - roundpd_3 = "rmio:660F3A09rMU", - roundps_3 = "rmio:660F3A08rMU", - roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:", - roundss_3 = "rrio:660F3A0ArMU|rxi/od:", - - -- SSE4.2 ops - crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:", - pcmpestri_3 = "rmio:660F3A61rMU", - pcmpestrm_3 = "rmio:660F3A60rMU", - pcmpgtq_2 = "rmo:660F3837rM", - pcmpistri_3 = "rmio:660F3A63rMU", - pcmpistrm_3 = "rmio:660F3A62rMU", - popcnt_2 = "rmqdw:F30FB8rM", - - -- SSE4a - extrq_2 = "rro:660F79rM", - extrq_3 = "riio:660F780mUU", - insertq_2 = "rro:F20F79rM", - insertq_4 = "rriio:F20F78rMUU", - lzcnt_2 = "rmqdw:F30FBDrM", - movntsd_2 = "xr/qo:nF20F2BRm", - movntss_2 = "xr/do:F30F2BRm", - -- popcnt is also in SSE4.2 -} - ------------------------------------------------------------------------------- - --- Arithmetic ops. -for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3, - ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do - local n8 = n * 8 - map_op[name.."_2"] = format( - "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi", - 1+n8, 3+n8, n, n, 5+n8, n) -end - --- Shift ops. -for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3, - shl = 4, shr = 5, sar = 7, sal = 4 } do - map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n) -end - --- Conditional ops. -for cc,n in pairs(map_cc) do - map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X - map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n) - map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+ -end - --- FP arithmetic ops. -for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3, - sub = 4, subr = 5, div = 6, divr = 7 } do - local nc = 192 + n * 8 - local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8)) - local fn = "f"..name - map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n) - if n == 2 or n == 3 then - map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n) - else - map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n) - map_op[fn.."p_1"] = format("ff:DE%02Xr", nr) - map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr) - end - map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n) -end - --- FP conditional moves. -for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do - local n4 = n % 4 - local nc = 56000 + n4 * 8 + (n-n4) * 64 - map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+ - map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+ -end - --- SSE FP arithmetic ops. -for name,n in pairs{ sqrt = 1, add = 8, mul = 9, - sub = 12, min = 13, div = 14, max = 15 } do - map_op[name.."ps_2"] = format("rmo:0F5%XrM", n) - map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n) - map_op[name.."pd_2"] = format("rmo:660F5%XrM", n) - map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n) -end - ------------------------------------------------------------------------------- - --- Process pattern string. -local function dopattern(pat, args, sz, op, needrex) - local digit, addin - local opcode = 0 - local szov = sz - local narg = 1 - local rex = 0 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 5 positions. - if secpos+5 > maxsecpos then wflush() end - - -- Process each character. - for c in gmatch(pat.."|", ".") do - if match(c, "%x") then -- Hex digit. - digit = byte(c) - 48 - if digit > 48 then digit = digit - 39 - elseif digit > 16 then digit = digit - 7 end - opcode = opcode*16 + digit - addin = nil - elseif c == "n" then -- Disable operand size mods for opcode. - szov = nil - elseif c == "X" then -- Force REX.W. - rex = 8 - elseif c == "r" then -- Merge 1st operand regno. into opcode. - addin = args[1]; opcode = opcode + (addin.reg % 8) - if narg < 2 then narg = 2 end - elseif c == "R" then -- Merge 2nd operand regno. into opcode. - addin = args[2]; opcode = opcode + (addin.reg % 8) - narg = 3 - elseif c == "m" or c == "M" then -- Encode ModRM/SIB. - local s - if addin then - s = addin.reg - opcode = opcode - (s%8) -- Undo regno opcode merge. - else - s = opcode % 16 -- Undo last digit. - opcode = (opcode - s) / 16 - end - local nn = c == "m" and 1 or 2 - local t = args[nn] - if narg <= nn then narg = nn + 1 end - if szov == "q" and rex == 0 then rex = rex + 8 end - if t.reg and t.reg > 7 then rex = rex + 1 end - if t.xreg and t.xreg > 7 then rex = rex + 2 end - if s > 7 then rex = rex + 4 end - if needrex then rex = rex + 16 end - wputop(szov, opcode, rex); opcode = nil - local imark = sub(pat, -1) -- Force a mark (ugly). - -- Put ModRM/SIB with regno/last digit as spare. - wputmrmsib(t, imark, s, addin and addin.vreg) - addin = nil - else - if opcode then -- Flush opcode. - if szov == "q" and rex == 0 then rex = rex + 8 end - if needrex then rex = rex + 16 end - if addin and addin.reg == -1 then - wputop(szov, opcode - 7, rex) - waction("VREG", addin.vreg); wputxb(0) - else - if addin and addin.reg > 7 then rex = rex + 1 end - wputop(szov, opcode, rex) - end - opcode = nil - end - if c == "|" then break end - if c == "o" then -- Offset (pure 32 bit displacement). - wputdarg(args[1].disp); if narg < 2 then narg = 2 end - elseif c == "O" then - wputdarg(args[2].disp); narg = 3 - else - -- Anything else is an immediate operand. - local a = args[narg] - narg = narg + 1 - local mode, imm = a.mode, a.imm - if mode == "iJ" and not match("iIJ", c) then - werror("bad operand size for label") - end - if c == "S" then - wputsbarg(imm) - elseif c == "U" then - wputbarg(imm) - elseif c == "W" then - wputwarg(imm) - elseif c == "i" or c == "I" then - if mode == "iJ" then - wputlabel("IMM_", imm, 1) - elseif mode == "iI" and c == "I" then - waction(sz == "w" and "IMM_WB" or "IMM_DB", imm) - else - wputszarg(sz, imm) - end - elseif c == "J" then - if mode == "iPJ" then - waction("REL_A", imm) -- !x64 (secpos) - else - wputlabel("REL_", imm, 2) - end - else - werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'") - end - end - end - end -end - ------------------------------------------------------------------------------- - --- Mapping of operand modes to short names. Suppress output with '#'. -local map_modename = { - r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm", - f = "stx", F = "st0", J = "lbl", ["1"] = "1", - I = "#", S = "#", O = "#", -} - --- Return a table/string showing all possible operand modes. -local function templatehelp(template, nparams) - if nparams == 0 then return "" end - local t = {} - for tm in gmatch(template, "[^%|]+") do - local s = map_modename[sub(tm, 1, 1)] - s = s..gsub(sub(tm, 2, nparams), ".", function(c) - return ", "..map_modename[c] - end) - if not match(s, "#") then t[#t+1] = s end - end - return t -end - --- Match operand modes against mode match part of template. -local function matchtm(tm, args) - for i=1,#args do - if not match(args[i].mode, sub(tm, i, i)) then return end - end - return true -end - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return templatehelp(template, nparams) end - local args = {} - - -- Zero-operand opcodes have no match part. - if #params == 0 then - dopattern(template, args, "d", params.op, nil) - return - end - - -- Determine common operand size (coerce undefined size) or flag as mixed. - local sz, szmix, needrex - for i,p in ipairs(params) do - args[i] = parseoperand(p) - local nsz = args[i].opsize - if nsz then - if sz and sz ~= nsz then szmix = true else sz = nsz end - end - local nrex = args[i].needrex - if nrex ~= nil then - if needrex == nil then - needrex = nrex - elseif needrex ~= nrex then - werror("bad mix of byte-addressable registers") - end - end - end - - -- Try all match:pattern pairs (separated by '|'). - local gotmatch, lastpat - for tm in gmatch(template, "[^%|]+") do - -- Split off size match (starts after mode match) and pattern string. - local szm, pat = match(tm, "^(.-):(.*)$", #args+1) - if pat == "" then pat = lastpat else lastpat = pat end - if matchtm(tm, args) then - local prefix = sub(szm, 1, 1) - if prefix == "/" then -- Match both operand sizes. - if args[1].opsize == sub(szm, 2, 2) and - args[2].opsize == sub(szm, 3, 3) then - dopattern(pat, args, sz, params.op, needrex) -- Process pattern. - return - end - else -- Match common operand size. - local szp = sz - if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes. - if prefix == "1" then szp = args[1].opsize; szmix = nil - elseif prefix == "2" then szp = args[2].opsize; szmix = nil end - if not szmix and (prefix == "." or match(szm, szp or "#")) then - dopattern(pat, args, szp, params.op, needrex) -- Process pattern. - return - end - end - gotmatch = true - end - end - - local msg = "bad operand mode" - if gotmatch then - if szmix then - msg = "mixed operand size" - else - msg = sz and "bad operand size" or "missing operand size" - end - end - - werror(msg.." in `"..opmodestr(params.op, args).."'") -end - ------------------------------------------------------------------------------- - --- x64-specific opcode for 64 bit immediates and displacements. -if x64 then - function map_op.mov64_2(params) - if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end - if secpos+2 > maxsecpos then wflush() end - local opcode, op64, sz, rex - local op64 = match(params[1], "^%[%s*(.-)%s*%]$") - if op64 then - local a = parseoperand(params[2]) - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa3 - else - op64 = match(params[2], "^%[%s*(.-)%s*%]$") - local a = parseoperand(params[1]) - if op64 then - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa1 - else - if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then - werror("bad operand mode") - end - op64 = params[2] - opcode = 0xb8 + (a.reg%8) -- !x64: no VREG support. - rex = a.reg > 7 and 9 or 8 - end - end - wputop(sz, opcode, rex) - waction("IMM_D", format("(unsigned int)(%s)", op64)) - waction("IMM_D", format("(unsigned int)((%s)>>32)", op64)) - end -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -local function op_data(params) - if not params then return "imm..." end - local sz = sub(params.op, 2, 2) - if sz == "a" then sz = addrsize end - for _,p in ipairs(params) do - local a = parseoperand(p) - if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then - werror("bad mode or size in `"..p.."'") - end - if a.mode == "iJ" then - wputlabel("IMM_", a.imm, 1) - else - wputszarg(sz, a.imm) - end - if secpos+2 > maxsecpos then wflush() end - end -end - -map_op[".byte_*"] = op_data -map_op[".sbyte_*"] = op_data -map_op[".word_*"] = op_data -map_op[".dword_*"] = op_data -map_op[".aword_*"] = op_data - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_2"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end - if secpos+2 > maxsecpos then wflush() end - local a = parseoperand(params[1]) - local mode, imm = a.mode, a.imm - if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then - -- Local label (1: ... 9:) or global label (->global:). - waction("LABEL_LG", nil, 1) - wputxb(imm) - elseif mode == "iJ" then - -- PC label (=>pcexpr:). - waction("LABEL_PC", imm) - else - werror("bad label definition") - end - -- SETLABEL must immediately follow LABEL_LG/LABEL_PC. - local addr = params[2] - if addr then - local a = parseoperand(addr) - if a.mode == "iPJ" then - waction("SETLABEL", a.imm) - else - werror("bad label assignment") - end - end -end -map_op[".label_1"] = map_op[".label_2"] - ------------------------------------------------------------------------------- - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]] - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", nil, 1) - wputxb(align-1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - --- Spacing pseudo-opcode. -map_op[".space_2"] = function(params) - if not params then return "num [, filler]" end - if secpos+1 > maxsecpos then wflush() end - waction("SPACE", params[1]) - local fill = params[2] - if fill then - fill = tonumber(fill) - if not fill or fill < 0 or fill > 255 then werror("bad filler") end - end - wputxb(fill or 0) -end -map_op[".space_1"] = map_op[".space_2"] - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - if reg and not map_reg_valid_base[reg] then - werror("bad base register `"..(map_reg_rev[reg] or reg).."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg and map_reg_rev[tp.reg] or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION") - wputxb(num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpregs(out) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dynasm.lua b/third_party/dynasm/dynasm.lua deleted file mode 100644 index 2ef8164..0000000 --- a/third_party/dynasm/dynasm.lua +++ /dev/null @@ -1,1084 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM. A dynamic assembler for code generation engines. --- Originally designed and implemented for LuaJIT. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See below for full copyright notice. ------------------------------------------------------------------------------- - --- Application information. -local _info = { - name = "DynASM", - description = "A dynamic assembler for code generation engines", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - url = "http://luajit.org/dynasm.html", - license = "MIT", - copyright = [[ -Copyright (C) 2005-2012 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -[ MIT license: http://www.opensource.org/licenses/mit-license.php ] -]], -} - --- Cache library functions. -local type, pairs, ipairs = type, pairs, ipairs -local pcall, error, assert = pcall, error, assert -local _s = string -local sub, match, gmatch, gsub = _s.sub, _s.match, _s.gmatch, _s.gsub -local format, rep, upper = _s.format, _s.rep, _s.upper -local _t = table -local insert, remove, concat, sort = _t.insert, _t.remove, _t.concat, _t.sort -local exit = os.exit -local io = io -local stdin, stdout, stderr = io.stdin, io.stdout, io.stderr - ------------------------------------------------------------------------------- - --- Program options. -local g_opt = {} - --- Global state for current file. -local g_fname, g_curline, g_indent, g_lineno, g_synclineno, g_arch -local g_errcount = 0 - --- Write buffer for output file. -local g_wbuffer, g_capbuffer - ------------------------------------------------------------------------------- - --- Write an output line (or callback function) to the buffer. -local function wline(line, needindent) - local buf = g_capbuffer or g_wbuffer - buf[#buf+1] = needindent and g_indent..line or line - g_synclineno = g_synclineno + 1 -end - --- Write assembler line as a comment, if requestd. -local function wcomment(aline) - if g_opt.comment then - wline(g_opt.comment..aline..g_opt.endcomment, true) - end -end - --- Resync CPP line numbers. -local function wsync() - if g_synclineno ~= g_lineno and g_opt.cpp then - wline("# "..g_lineno..' "'..g_fname..'"') - g_synclineno = g_lineno - end -end - --- Dummy action flush function. Replaced with arch-specific function later. -local function wflush(term) -end - --- Dump all buffered output lines. -local function wdumplines(out, buf) - for _,line in ipairs(buf) do - if type(line) == "string" then - assert(out:write(line, "\n")) - else - -- Special callback to dynamically insert lines after end of processing. - line(out) - end - end -end - ------------------------------------------------------------------------------- - --- Emit an error. Processing continues with next statement. -local function werror(msg) - error(format("%s:%s: error: %s:\n%s", g_fname, g_lineno, msg, g_curline), 0) -end - --- Emit a fatal error. Processing stops. -local function wfatal(msg) - g_errcount = "fatal" - werror(msg) -end - --- Print a warning. Processing continues. -local function wwarn(msg) - stderr:write(format("%s:%s: warning: %s:\n%s\n", - g_fname, g_lineno, msg, g_curline)) -end - --- Print caught error message. But suppress excessive errors. -local function wprinterr(...) - if type(g_errcount) == "number" then - -- Regular error. - g_errcount = g_errcount + 1 - if g_errcount < 21 then -- Seems to be a reasonable limit. - stderr:write(...) - elseif g_errcount == 21 then - stderr:write(g_fname, - ":*: warning: too many errors (suppressed further messages).\n") - end - else - -- Fatal error. - stderr:write(...) - return true -- Stop processing. - end -end - ------------------------------------------------------------------------------- - --- Map holding all option handlers. -local opt_map = {} -local opt_current - --- Print error and exit with error status. -local function opterror(...) - stderr:write("dynasm.lua: ERROR: ", ...) - stderr:write("\n") - exit(1) -end - --- Get option parameter. -local function optparam(args) - local argn = args.argn - local p = args[argn] - if not p then - opterror("missing parameter for option `", opt_current, "'.") - end - args.argn = argn + 1 - return p -end - ------------------------------------------------------------------------------- - --- Core pseudo-opcodes. -local map_coreop = {} --- Dummy opcode map. Replaced by arch-specific map. -local map_op = {} - --- Forward declarations. -local dostmt -local readfile - ------------------------------------------------------------------------------- - --- Map for defines (initially empty, chains to arch-specific map). -local map_def = {} - --- Pseudo-opcode to define a substitution. -map_coreop[".define_2"] = function(params, nparams) - if not params then return nparams == 1 and "name" or "name, subst" end - local name, def = params[1], params[2] or "1" - if not match(name, "^[%a_][%w_]*$") then werror("bad or duplicate define") end - map_def[name] = def -end -map_coreop[".define_1"] = map_coreop[".define_2"] - --- Define a substitution on the command line. -function opt_map.D(args) - local namesubst = optparam(args) - local name, subst = match(namesubst, "^([%a_][%w_]*)=(.*)$") - if name then - map_def[name] = subst - elseif match(namesubst, "^[%a_][%w_]*$") then - map_def[namesubst] = "1" - else - opterror("bad define") - end -end - --- Undefine a substitution on the command line. -function opt_map.U(args) - local name = optparam(args) - if match(name, "^[%a_][%w_]*$") then - map_def[name] = nil - else - opterror("bad define") - end -end - --- Helper for definesubst. -local gotsubst - -local function definesubst_one(word) - local subst = map_def[word] - if subst then gotsubst = word; return subst else return word end -end - --- Iteratively substitute defines. -local function definesubst(stmt) - -- Limit number of iterations. - for i=1,100 do - gotsubst = false - stmt = gsub(stmt, "#?[%w_]+", definesubst_one) - if not gotsubst then break end - end - if gotsubst then wfatal("recursive define involving `"..gotsubst.."'") end - return stmt -end - --- Dump all defines. -local function dumpdefines(out, lvl) - local t = {} - for name in pairs(map_def) do - t[#t+1] = name - end - sort(t) - out:write("Defines:\n") - for _,name in ipairs(t) do - local subst = map_def[name] - if g_arch then subst = g_arch.revdef(subst) end - out:write(format(" %-20s %s\n", name, subst)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Support variables for conditional assembly. -local condlevel = 0 -local condstack = {} - --- Evaluate condition with a Lua expression. Substitutions already performed. -local function cond_eval(cond) - local func, err - if setfenv then - func, err = loadstring("return "..cond, "=expr") - else - -- No globals. All unknown identifiers evaluate to nil. - func, err = load("return "..cond, "=expr", "t", {}) - end - if func then - if setfenv then - setfenv(func, {}) -- No globals. All unknown identifiers evaluate to nil. - end - local ok, res = pcall(func) - if ok then - if res == 0 then return false end -- Oh well. - return not not res - end - err = res - end - wfatal("bad condition: "..err) -end - --- Skip statements until next conditional pseudo-opcode at the same level. -local function stmtskip() - local dostmt_save = dostmt - local lvl = 0 - dostmt = function(stmt) - local op = match(stmt, "^%s*(%S+)") - if op == ".if" then - lvl = lvl + 1 - elseif lvl ~= 0 then - if op == ".endif" then lvl = lvl - 1 end - elseif op == ".elif" or op == ".else" or op == ".endif" then - dostmt = dostmt_save - dostmt(stmt) - end - end -end - --- Pseudo-opcodes for conditional assembly. -map_coreop[".if_1"] = function(params) - if not params then return "condition" end - local lvl = condlevel + 1 - local res = cond_eval(params[1]) - condlevel = lvl - condstack[lvl] = res - if not res then stmtskip() end -end - -map_coreop[".elif_1"] = function(params) - if not params then return "condition" end - if condlevel == 0 then wfatal(".elif without .if") end - local lvl = condlevel - local res = condstack[lvl] - if res then - if res == "else" then wfatal(".elif after .else") end - else - res = cond_eval(params[1]) - if res then - condstack[lvl] = res - return - end - end - stmtskip() -end - -map_coreop[".else_0"] = function(params) - if condlevel == 0 then wfatal(".else without .if") end - local lvl = condlevel - local res = condstack[lvl] - condstack[lvl] = "else" - if res then - if res == "else" then wfatal(".else after .else") end - stmtskip() - end -end - -map_coreop[".endif_0"] = function(params) - local lvl = condlevel - if lvl == 0 then wfatal(".endif without .if") end - condlevel = lvl - 1 -end - --- Check for unfinished conditionals. -local function checkconds() - if g_errcount ~= "fatal" and condlevel ~= 0 then - wprinterr(g_fname, ":*: error: unbalanced conditional\n") - end -end - ------------------------------------------------------------------------------- - --- Search for a file in the given path and open it for reading. -local function pathopen(path, name) - local dirsep = match(package.path, "\\") and "\\" or "/" - for _,p in ipairs(path) do - local fullname = p == "" and name or p..dirsep..name - local fin = io.open(fullname, "r") - if fin then - g_fname = fullname - return fin - end - end -end - --- Include a file. -map_coreop[".include_1"] = function(params) - if not params then return "filename" end - local name = params[1] - -- Save state. Ugly, I know. but upvalues are fast. - local gf, gl, gcl, gi = g_fname, g_lineno, g_curline, g_indent - -- Read the included file. - local fatal = readfile(pathopen(g_opt.include, name) or - wfatal("include file `"..name.."' not found")) - -- Restore state. - g_synclineno = -1 - g_fname, g_lineno, g_curline, g_indent = gf, gl, gcl, gi - if fatal then wfatal("in include file") end -end - --- Make .include and conditionals initially available, too. -map_op[".include_1"] = map_coreop[".include_1"] -map_op[".if_1"] = map_coreop[".if_1"] -map_op[".elif_1"] = map_coreop[".elif_1"] -map_op[".else_0"] = map_coreop[".else_0"] -map_op[".endif_0"] = map_coreop[".endif_0"] - ------------------------------------------------------------------------------- - --- Support variables for macros. -local mac_capture, mac_lineno, mac_name -local mac_active = {} -local mac_list = {} - --- Pseudo-opcode to define a macro. -map_coreop[".macro_*"] = function(mparams) - if not mparams then return "name [, params...]" end - -- Split off and validate macro name. - local name = remove(mparams, 1) - if not name then werror("missing macro name") end - if not (match(name, "^[%a_][%w_%.]*$") or match(name, "^%.[%w_%.]*$")) then - wfatal("bad macro name `"..name.."'") - end - -- Validate macro parameter names. - local mdup = {} - for _,mp in ipairs(mparams) do - if not match(mp, "^[%a_][%w_]*$") then - wfatal("bad macro parameter name `"..mp.."'") - end - if mdup[mp] then wfatal("duplicate macro parameter name `"..mp.."'") end - mdup[mp] = true - end - -- Check for duplicate or recursive macro definitions. - local opname = name.."_"..#mparams - if map_op[opname] or map_op[name.."_*"] then - wfatal("duplicate macro `"..name.."' ("..#mparams.." parameters)") - end - if mac_capture then wfatal("recursive macro definition") end - - -- Enable statement capture. - local lines = {} - mac_lineno = g_lineno - mac_name = name - mac_capture = function(stmt) -- Statement capture function. - -- Stop macro definition with .endmacro pseudo-opcode. - if not match(stmt, "^%s*.endmacro%s*$") then - lines[#lines+1] = stmt - return - end - mac_capture = nil - mac_lineno = nil - mac_name = nil - mac_list[#mac_list+1] = opname - -- Add macro-op definition. - map_op[opname] = function(params) - if not params then return mparams, lines end - -- Protect against recursive macro invocation. - if mac_active[opname] then wfatal("recursive macro invocation") end - mac_active[opname] = true - -- Setup substitution map. - local subst = {} - for i,mp in ipairs(mparams) do subst[mp] = params[i] end - local mcom - if g_opt.maccomment and g_opt.comment then - mcom = " MACRO "..name.." ("..#mparams..")" - wcomment("{"..mcom) - end - -- Loop through all captured statements - for _,stmt in ipairs(lines) do - -- Substitute macro parameters. - local st = gsub(stmt, "[%w_]+", subst) - st = definesubst(st) - st = gsub(st, "%s*%.%.%s*", "") -- Token paste a..b. - if mcom and sub(st, 1, 1) ~= "|" then wcomment(st) end - -- Emit statement. Use a protected call for better diagnostics. - local ok, err = pcall(dostmt, st) - if not ok then - -- Add the captured statement to the error. - wprinterr(err, "\n", g_indent, "| ", stmt, - "\t[MACRO ", name, " (", #mparams, ")]\n") - end - end - if mcom then wcomment("}"..mcom) end - mac_active[opname] = nil - end - end -end - --- An .endmacro pseudo-opcode outside of a macro definition is an error. -map_coreop[".endmacro_0"] = function(params) - wfatal(".endmacro without .macro") -end - --- Dump all macros and their contents (with -PP only). -local function dumpmacros(out, lvl) - sort(mac_list) - out:write("Macros:\n") - for _,opname in ipairs(mac_list) do - local name = sub(opname, 1, -3) - local params, lines = map_op[opname]() - out:write(format(" %-20s %s\n", name, concat(params, ", "))) - if lvl > 1 then - for _,line in ipairs(lines) do - out:write(" |", line, "\n") - end - out:write("\n") - end - end - out:write("\n") -end - --- Check for unfinished macro definitions. -local function checkmacros() - if mac_capture then - wprinterr(g_fname, ":", mac_lineno, - ": error: unfinished .macro `", mac_name ,"'\n") - end -end - ------------------------------------------------------------------------------- - --- Support variables for captures. -local cap_lineno, cap_name -local cap_buffers = {} -local cap_used = {} - --- Start a capture. -map_coreop[".capture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - if cap_name then - wfatal("already capturing to `"..cap_name.."' since line "..cap_lineno) - end - cap_name = name - cap_lineno = g_lineno - -- Create or continue a capture buffer and start the output line capture. - local buf = cap_buffers[name] - if not buf then buf = {}; cap_buffers[name] = buf end - g_capbuffer = buf - g_synclineno = 0 -end - --- Stop a capture. -map_coreop[".endcapture_0"] = function(params) - wflush() - if not cap_name then wfatal(".endcapture without a valid .capture") end - cap_name = nil - cap_lineno = nil - g_capbuffer = nil - g_synclineno = 0 -end - --- Dump a capture buffer. -map_coreop[".dumpcapture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - cap_used[name] = true - wline(function(out) - local buf = cap_buffers[name] - if buf then wdumplines(out, buf) end - end) - g_synclineno = 0 -end - --- Dump all captures and their buffers (with -PP only). -local function dumpcaptures(out, lvl) - out:write("Captures:\n") - for name,buf in pairs(cap_buffers) do - out:write(format(" %-20s %4s)\n", name, "("..#buf)) - if lvl > 1 then - local bar = rep("=", 76) - out:write(" ", bar, "\n") - for _,line in ipairs(buf) do - out:write(" ", line, "\n") - end - out:write(" ", bar, "\n\n") - end - end - out:write("\n") -end - --- Check for unfinished or unused captures. -local function checkcaptures() - if cap_name then - wprinterr(g_fname, ":", cap_lineno, - ": error: unfinished .capture `", cap_name,"'\n") - return - end - for name in pairs(cap_buffers) do - if not cap_used[name] then - wprinterr(g_fname, ":*: error: missing .dumpcapture ", name ,"\n") - end - end -end - ------------------------------------------------------------------------------- - --- Sections names. -local map_sections = {} - --- Pseudo-opcode to define code sections. --- TODO: Data sections, BSS sections. Needs extra C code and API. -map_coreop[".section_*"] = function(params) - if not params then return "name..." end - if #map_sections > 0 then werror("duplicate section definition") end - wflush() - for sn,name in ipairs(params) do - local opname = "."..name.."_0" - if not match(name, "^[%a][%w_]*$") or - map_op[opname] or map_op["."..name.."_*"] then - werror("bad section name `"..name.."'") - end - map_sections[#map_sections+1] = name - wline(format("#define DASM_SECTION_%s\t%d", upper(name), sn-1)) - map_op[opname] = function(params) g_arch.section(sn-1) end - end - wline(format("#define DASM_MAXSECTION\t\t%d", #map_sections)) -end - --- Dump all sections. -local function dumpsections(out, lvl) - out:write("Sections:\n") - for _,name in ipairs(map_sections) do - out:write(format(" %s\n", name)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Load architecture-specific module. -local function loadarch(arch) - if not match(arch, "^[%w_]+$") then return "bad arch name" end - local ok, m_arch = pcall(require, "dasm_"..arch) - if not ok then return "cannot load module: "..m_arch end - g_arch = m_arch - wflush = m_arch.passcb(wline, werror, wfatal, wwarn) - m_arch.setup(arch, g_opt) - map_op, map_def = m_arch.mergemaps(map_coreop, map_def) -end - --- Dump architecture description. -function opt_map.dumparch(args) - local name = optparam(args) - if not g_arch then - local err = loadarch(name) - if err then opterror(err) end - end - - local t = {} - for name in pairs(map_coreop) do t[#t+1] = name end - for name in pairs(map_op) do t[#t+1] = name end - sort(t) - - local out = stdout - local _arch = g_arch._info - out:write(format("%s version %s, released %s, %s\n", - _info.name, _info.version, _info.release, _info.url)) - g_arch.dumparch(out) - - local pseudo = true - out:write("Pseudo-Opcodes:\n") - for _,sname in ipairs(t) do - local name, nparam = match(sname, "^(.+)_([0-9%*])$") - if name then - if pseudo and sub(name, 1, 1) ~= "." then - out:write("\nOpcodes:\n") - pseudo = false - end - local f = map_op[sname] - local s - if nparam ~= "*" then nparam = nparam + 0 end - if nparam == 0 then - s = "" - elseif type(f) == "string" then - s = map_op[".template__"](nil, f, nparam) - else - s = f(nil, nparam) - end - if type(s) == "table" then - for _,s2 in ipairs(s) do - out:write(format(" %-12s %s\n", name, s2)) - end - else - out:write(format(" %-12s %s\n", name, s)) - end - end - end - out:write("\n") - exit(0) -end - --- Pseudo-opcode to set the architecture. --- Only initially available (map_op is replaced when called). -map_op[".arch_1"] = function(params) - if not params then return "name" end - local err = loadarch(params[1]) - if err then wfatal(err) end -end - --- Dummy .arch pseudo-opcode to improve the error report. -map_coreop[".arch_1"] = function(params) - if not params then return "name" end - wfatal("duplicate .arch statement") -end - ------------------------------------------------------------------------------- - --- Dummy pseudo-opcode. Don't confuse '.nop' with 'nop'. -map_coreop[".nop_*"] = function(params) - if not params then return "[ignored...]" end -end - --- Pseudo-opcodes to raise errors. -map_coreop[".error_1"] = function(params) - if not params then return "message" end - werror(params[1]) -end - -map_coreop[".fatal_1"] = function(params) - if not params then return "message" end - wfatal(params[1]) -end - --- Dump all user defined elements. -local function dumpdef(out) - local lvl = g_opt.dumpdef - if lvl == 0 then return end - dumpsections(out, lvl) - dumpdefines(out, lvl) - if g_arch then g_arch.dumpdef(out, lvl) end - dumpmacros(out, lvl) - dumpcaptures(out, lvl) -end - ------------------------------------------------------------------------------- - --- Helper for splitstmt. -local splitlvl - -local function splitstmt_one(c) - if c == "(" then - splitlvl = ")"..splitlvl - elseif c == "[" then - splitlvl = "]"..splitlvl - elseif c == "{" then - splitlvl = "}"..splitlvl - elseif c == ")" or c == "]" or c == "}" then - if sub(splitlvl, 1, 1) ~= c then werror("unbalanced (), [] or {}") end - splitlvl = sub(splitlvl, 2) - elseif splitlvl == "" then - return " \0 " - end - return c -end - --- Split statement into (pseudo-)opcode and params. -local function splitstmt(stmt) - -- Convert label with trailing-colon into .label statement. - local label = match(stmt, "^%s*(.+):%s*$") - if label then return ".label", {label} end - - -- Split at commas and equal signs, but obey parentheses and brackets. - splitlvl = "" - stmt = gsub(stmt, "[,%(%)%[%]{}]", splitstmt_one) - if splitlvl ~= "" then werror("unbalanced () or []") end - - -- Split off opcode. - local op, other = match(stmt, "^%s*([^%s%z]+)%s*(.*)$") - if not op then werror("bad statement syntax") end - - -- Split parameters. - local params = {} - for p in gmatch(other, "%s*(%Z+)%z?") do - params[#params+1] = gsub(p, "%s+$", "") - end - if #params > 16 then werror("too many parameters") end - - params.op = op - return op, params -end - --- Process a single statement. -dostmt = function(stmt) - -- Ignore empty statements. - if match(stmt, "^%s*$") then return end - - -- Capture macro defs before substitution. - if mac_capture then return mac_capture(stmt) end - stmt = definesubst(stmt) - - -- Emit C code without parsing the line. - if sub(stmt, 1, 1) == "|" then - local tail = sub(stmt, 2) - wflush() - if sub(tail, 1, 2) == "//" then wcomment(tail) else wline(tail, true) end - return - end - - -- Split into (pseudo-)opcode and params. - local op, params = splitstmt(stmt) - - -- Get opcode handler (matching # of parameters or generic handler). - local f = map_op[op.."_"..#params] or map_op[op.."_*"] - if not f then - if not g_arch then wfatal("first statement must be .arch") end - -- Improve error report. - for i=0,9 do - if map_op[op.."_"..i] then - werror("wrong number of parameters for `"..op.."'") - end - end - werror("unknown statement `"..op.."'") - end - - -- Call opcode handler or special handler for template strings. - if type(f) == "string" then - map_op[".template__"](params, f) - else - f(params) - end -end - --- Process a single line. -local function doline(line) - if g_opt.flushline then wflush() end - - -- Assembler line? - local indent, aline = match(line, "^(%s*)%|(.*)$") - if not aline then - -- No, plain C code line, need to flush first. - wflush() - wsync() - wline(line, false) - return - end - - g_indent = indent -- Remember current line indentation. - - -- Emit C code (even from macros). Avoids echo and line parsing. - if sub(aline, 1, 1) == "|" then - if not mac_capture then - wsync() - elseif g_opt.comment then - wsync() - wcomment(aline) - end - dostmt(aline) - return - end - - -- Echo assembler line as a comment. - if g_opt.comment then - wsync() - wcomment(aline) - end - - -- Strip assembler comments. - aline = gsub(aline, "//.*$", "") - - -- Split line into statements at semicolons. - if match(aline, ";") then - for stmt in gmatch(aline, "[^;]+") do dostmt(stmt) end - else - dostmt(aline) - end -end - ------------------------------------------------------------------------------- - --- Write DynASM header. -local function dasmhead(out) - out:write(format([[ -/* -** This file has been pre-processed with DynASM. -** %s -** DynASM version %s, DynASM %s version %s -** DO NOT EDIT! The original file is in "%s". -*/ - -#if DASM_VERSION != %d -#error "Version mismatch between DynASM and included encoding engine" -#endif - -]], _info.url, - _info.version, g_arch._info.arch, g_arch._info.version, - g_fname, _info.vernum)) -end - --- Read input file. -readfile = function(fin) - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Process all lines. - for line in fin:lines() do - g_lineno = g_lineno + 1 - g_curline = line - local ok, err = pcall(doline, line) - if not ok and wprinterr(err, "\n") then return true end - end - wflush() - - -- Close input file. - assert(fin == stdin or fin:close()) -end - --- Write output file. -local function writefile(outfile) - local fout - - -- Open output file. - if outfile == nil or outfile == "-" then - fout = stdout - else - fout = assert(io.open(outfile, "w")) - end - - -- Write all buffered lines - wdumplines(fout, g_wbuffer) - - -- Close output file. - assert(fout == stdout or fout:close()) - - -- Optionally dump definitions. - dumpdef(fout == stdout and stderr or stdout) -end - --- Translate an input file to an output file. -local function translate(infile, outfile) - g_wbuffer = {} - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Put header. - wline(dasmhead) - - -- Read input file. - local fin - if infile == "-" then - g_fname = "(stdin)" - fin = stdin - else - g_fname = infile - fin = assert(io.open(infile, "r")) - end - readfile(fin) - - -- Check for errors. - if not g_arch then - wprinterr(g_fname, ":*: error: missing .arch directive\n") - end - checkconds() - checkmacros() - checkcaptures() - - if g_errcount ~= 0 then - stderr:write(g_fname, ":*: info: ", g_errcount, " error", - (type(g_errcount) == "number" and g_errcount > 1) and "s" or "", - " in input file -- no output file generated.\n") - dumpdef(stderr) - exit(1) - end - - -- Write output file. - writefile(outfile) -end - ------------------------------------------------------------------------------- - --- Print help text. -function opt_map.help() - stdout:write("DynASM -- ", _info.description, ".\n") - stdout:write("DynASM ", _info.version, " ", _info.release, " ", _info.url, "\n") - stdout:write[[ - -Usage: dynasm [OPTION]... INFILE.dasc|- - - -h, --help Display this help text. - -V, --version Display version and copyright information. - - -o, --outfile FILE Output file name (default is stdout). - -I, --include DIR Add directory to the include search path. - - -c, --ccomment Use /* */ comments for assembler lines. - -C, --cppcomment Use // comments for assembler lines (default). - -N, --nocomment Suppress assembler lines in output. - -M, --maccomment Show macro expansions as comments (default off). - - -L, --nolineno Suppress CPP line number information in output. - -F, --flushline Flush action list for every line. - - -D NAME[=SUBST] Define a substitution. - -U NAME Undefine a substitution. - - -P, --dumpdef Dump defines, macros, etc. Repeat for more output. - -A, --dumparch ARCH Load architecture ARCH and dump description. -]] - exit(0) -end - --- Print version information. -function opt_map.version() - stdout:write(format("%s version %s, released %s\n%s\n\n%s", - _info.name, _info.version, _info.release, _info.url, _info.copyright)) - exit(0) -end - --- Misc. options. -function opt_map.outfile(args) g_opt.outfile = optparam(args) end -function opt_map.include(args) insert(g_opt.include, 1, optparam(args)) end -function opt_map.ccomment() g_opt.comment = "/*|"; g_opt.endcomment = " */" end -function opt_map.cppcomment() g_opt.comment = "//|"; g_opt.endcomment = "" end -function opt_map.nocomment() g_opt.comment = false end -function opt_map.maccomment() g_opt.maccomment = true end -function opt_map.nolineno() g_opt.cpp = false end -function opt_map.flushline() g_opt.flushline = true end -function opt_map.dumpdef() g_opt.dumpdef = g_opt.dumpdef + 1 end - ------------------------------------------------------------------------------- - --- Short aliases for long options. -local opt_alias = { - h = "help", ["?"] = "help", V = "version", - o = "outfile", I = "include", - c = "ccomment", C = "cppcomment", N = "nocomment", M = "maccomment", - L = "nolineno", F = "flushline", - P = "dumpdef", A = "dumparch", -} - --- Parse single option. -local function parseopt(opt, args) - opt_current = #opt == 1 and "-"..opt or "--"..opt - local f = opt_map[opt] or opt_map[opt_alias[opt]] - if not f then - opterror("unrecognized option `", opt_current, "'. Try `--help'.\n") - end - f(args) -end - --- Parse arguments. -local function parseargs(args) - -- Default options. - g_opt.comment = "//|" - g_opt.endcomment = "" - g_opt.cpp = true - g_opt.dumpdef = 0 - g_opt.include = { "" } - - -- Process all option arguments. - args.argn = 1 - repeat - local a = args[args.argn] - if not a then break end - local lopt, opt = match(a, "^%-(%-?)(.+)") - if not opt then break end - args.argn = args.argn + 1 - if lopt == "" then - -- Loop through short options. - for o in gmatch(opt, ".") do parseopt(o, args) end - else - -- Long option. - parseopt(opt, args) - end - until false - - -- Check for proper number of arguments. - local nargs = #args - args.argn + 1 - if nargs ~= 1 then - if nargs == 0 then - if g_opt.dumpdef > 0 then return dumpdef(stdout) end - end - opt_map.help() - end - - -- Translate a single input file to a single output file - -- TODO: Handle multiple files? - translate(args[args.argn], g_opt.outfile) -end - ------------------------------------------------------------------------------- - --- Add the directory dynasm.lua resides in to the Lua module search path. -local arg = arg -if arg and arg[0] then - local prefix = match(arg[0], "^(.*[/\\])") - if prefix then package.path = prefix.."?.lua;"..package.path end -end - --- Start DynASM. -parseargs{...} - ------------------------------------------------------------------------------- - diff --git a/third_party/protobuf b/third_party/protobuf deleted file mode 160000 index 5aeee3d..0000000 --- a/third_party/protobuf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5aeee3dc910d37f37b75f5a6d1486fe75cb09284 diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index ca497ed..c5d8d9b 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -4,11 +4,6 @@ ** Code to compile a upb::Handlers into bytecode for decoding a protobuf ** according to that specific schema and destination handlers. ** -** Compiling to bytecode is always the first step. If we are using the -** interpreted decoder we leave it as bytecode and interpret that. If we are -** using a JIT decoder we use a code generator to turn the bytecode into native -** code, LLVM IR, etc. -** ** Bytecode definition is in decoder.int.h. */ @@ -37,7 +32,6 @@ static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers, ret->group = group; ret->dest_handlers_ = dest_handlers; - ret->is_native_ = false; /* If we JIT, it will update this later. */ upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64); return ret; @@ -69,9 +63,6 @@ static void freegroup(mgroup *g) { } upb_inttable_uninit(&g->methods); -#ifdef UPB_USE_JIT_X64 - upb_pbdecoder_freejit(g); -#endif upb_gfree(g->bytecode); upb_gfree(g); } @@ -313,7 +304,7 @@ static void putop(compiler *c, int op, ...) { va_end(ap); } -#if defined(UPB_USE_JIT_X64) || defined(UPB_DUMP_BYTECODE) +#if defined(UPB_DUMP_BYTECODE) const char *upb_pbdecoder_getopname(unsigned int op) { #define QUOTE(x) #x @@ -827,31 +818,6 @@ static void set_bytecode_handlers(mgroup *g) { } -/* JIT setup. *****************************************************************/ - -#ifdef UPB_USE_JIT_X64 - -static void sethandlers(mgroup *g, bool allowjit) { - g->jit_code = NULL; - if (allowjit) { - /* Compile byte-code into machine code, create handlers. */ - upb_pbdecoder_jit(g); - } else { - set_bytecode_handlers(g); - } -} - -#else /* UPB_USE_JIT_X64 */ - -static void sethandlers(mgroup *g, bool allowjit) { - /* No JIT compiled in; use bytecode handlers unconditionally. */ - UPB_UNUSED(allowjit); - set_bytecode_handlers(g); -} - -#endif /* UPB_USE_JIT_X64 */ - - /* TODO(haberman): allow this to be constructed for an arbitrary set of dest * handlers and other mgroups (but verify we have a transitive closure). */ const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { @@ -891,7 +857,7 @@ const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { } #endif - sethandlers(g, allowjit); + set_bytecode_handlers(g); return g; } diff --git a/upb/pb/compile_decoder_x64.c b/upb/pb/compile_decoder_x64.c deleted file mode 100644 index 7c716e8..0000000 --- a/upb/pb/compile_decoder_x64.c +++ /dev/null @@ -1,511 +0,0 @@ -/* -** Driver code for the x64 JIT compiler. -*/ - -/* Needed to ensure we get defines like MAP_ANON. */ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include "upb/msg.h" -#include "upb/pb/decoder.h" -#include "upb/pb/decoder.int.h" -#include "upb/pb/varint.int.h" - -/* To debug the JIT: - * - * 1. Uncomment: - * #define UPB_JIT_LOAD_SO - * - * Note: this mode requires that we can shell out to gcc. - * - * 2. Run the test locally. This will load the JIT code by building a - * .so (/tmp/upb-jit-code.so) and using dlopen, so more of the tooling will - * work properly (like GDB). - * - * IF YOU ALSO WANT AUTOMATIC JIT DEBUG OUTPUT: - * - * 3. Run: upb/pb/make-gdb-script.rb > script.gdb. This reads - * /tmp/upb-jit-code.so as input and generates a GDB script that is specific - * to this jit code. - * - * 4. Run: gdb --command=script.gdb --args path/to/test - * This will drop you to a GDB prompt which you can now use normally. - * But when you run the test it will print a message to stdout every time - * the JIT executes assembly for a particular bytecode. Sample output: - * - * X.enterjit bytes=18 - * buf_ofs=1 data_rem=17 delim_rem=-2 X.0x6.OP_PARSE_DOUBLE - * buf_ofs=9 data_rem=9 delim_rem=-10 X.0x7.OP_CHECKDELIM - * buf_ofs=9 data_rem=9 delim_rem=-10 X.0x8.OP_TAG1 - * X.0x3.dispatch.DecoderTest - * X.parse_unknown - * X.0x3.dispatch.DecoderTest - * X.decode_unknown_tag_fallback - * X.exitjit - * - * This output should roughly correspond to the output that the bytecode - * interpreter emits when compiled with UPB_DUMP_BYTECODE (modulo some - * extra JIT-specific output). */ - -/* These defines are necessary for DynASM codegen. - * See dynasm/dasm_proto.h for more info. */ -#define Dst_DECL jitcompiler *jc -#define Dst_REF (jc->dynasm) -#define Dst (jc) - -/* In debug mode, make DynASM do internal checks (must be defined before any - * dasm header is included. */ -#ifndef NDEBUG -#define DASM_CHECKS -#endif - -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif - -typedef struct { - mgroup *group; - uint32_t *pc; - - /* This pointer is allocated by dasm_init() and freed by dasm_free(). */ - struct dasm_State *dynasm; - - /* Maps some key (an arbitrary void*) to a pclabel. - * - * The pclabel represents a location in the generated code -- DynASM exposes - * a pclabel -> (machine code offset) lookup function. - * - * The key can be anything. There are two main kinds of keys: - * - bytecode location -- the void* points to the bytecode instruction - * itself. We can then use this to generate jumps to this instruction. - * - other object (like dispatch table). We use these to represent parts - * of the generated code that do not exactly correspond to a bytecode - * instruction. */ - upb_inttable jmptargets; - -#ifndef NDEBUG - /* Like jmptargets, but members are present in the table when they have had - * define_jmptarget() (as opposed to jmptarget) called. Used to verify that - * define_jmptarget() is called exactly once for every target. - * The value is ignored. */ - upb_inttable jmpdefined; - - /* For checking that two asmlabels aren't defined for the same byte. */ - int lastlabelofs; -#endif - -#ifdef UPB_JIT_LOAD_SO - /* For marking labels that should go into the generated code. - * Maps pclabel -> char* label (string is owned by the table). */ - upb_inttable asmlabels; -#endif - - /* The total number of pclabels currently defined. - * Note that this contains both jmptargets and asmlabels, which both use - * pclabels but for different purposes. */ - uint32_t pclabel_count; - - /* Used by DynASM to store globals. */ - void **globals; -} jitcompiler; - -/* Functions called by codegen. */ -static int jmptarget(jitcompiler *jc, const void *key); -static int define_jmptarget(jitcompiler *jc, const void *key); -static void asmlabel(jitcompiler *jc, const char *fmt, ...); -static int pcofs(jitcompiler* jc); -static int alloc_pclabel(jitcompiler *jc); - -#ifdef UPB_JIT_LOAD_SO -static char *upb_vasprintf(const char *fmt, va_list ap); -static char *upb_asprintf(const char *fmt, ...); -#endif - -#include "third_party/dynasm/dasm_proto.h" -#include "third_party/dynasm/dasm_x86.h" -#include "upb/pb/compile_decoder_x64.h" - -static jitcompiler *newjitcompiler(mgroup *group) { - jitcompiler *jc = malloc(sizeof(jitcompiler)); - jc->group = group; - jc->pclabel_count = 0; - upb_inttable_init(&jc->jmptargets, UPB_CTYPE_UINT32); -#ifndef NDEBUG - jc->lastlabelofs = -1; - upb_inttable_init(&jc->jmpdefined, UPB_CTYPE_BOOL); -#endif -#ifdef UPB_JIT_LOAD_SO - upb_inttable_init(&jc->asmlabels, UPB_CTYPE_PTR); -#endif - jc->globals = malloc(UPB_JIT_GLOBAL__MAX * sizeof(*jc->globals)); - - dasm_init(jc, 1); - dasm_setupglobal(jc, jc->globals, UPB_JIT_GLOBAL__MAX); - dasm_setup(jc, upb_jit_actionlist); - - return jc; -} - -static void freejitcompiler(jitcompiler *jc) { -#ifdef UPB_JIT_LOAD_SO - upb_inttable_iter i; - upb_inttable_begin(&i, &jc->asmlabels); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - free(upb_value_getptr(upb_inttable_iter_value(&i))); - } - upb_inttable_uninit(&jc->asmlabels); -#endif -#ifndef NDEBUG - upb_inttable_uninit(&jc->jmpdefined); -#endif - upb_inttable_uninit(&jc->jmptargets); - dasm_free(jc); - free(jc->globals); - free(jc); -} - -#ifdef UPB_JIT_LOAD_SO - -/* Like sprintf except allocates the string, which is returned and owned by the - * caller. - * - * Like the GNU extension asprintf(), except we abort on error (since this is - * only for debugging). */ -static char *upb_vasprintf(const char *fmt, va_list args) { - /* Run once to get the length of the string. */ - va_list args_copy; - va_copy(args_copy, args); - int len = _upb_vsnprintf(NULL, 0, fmt, args_copy); - va_end(args_copy); - - char *ret = malloc(len + 1); /* + 1 for NULL terminator. */ - if (!ret) abort(); - int written = _upb_vsnprintf(ret, len + 1, fmt, args); - UPB_ASSERT(written == len); - - return ret; -} - -static char *upb_asprintf(const char *fmt, ...) { - va_list args; - va_start(args, fmt); - char *ret = upb_vasprintf(fmt, args); - va_end(args); - return ret; -} - -#endif - -static int alloc_pclabel(jitcompiler *jc) { - int newpc = jc->pclabel_count++; - dasm_growpc(jc, jc->pclabel_count); - return newpc; -} - -static bool try_getjmptarget(jitcompiler *jc, const void *key, int *pclabel) { - upb_value v; - if (upb_inttable_lookupptr(&jc->jmptargets, key, &v)) { - *pclabel = upb_value_getuint32(v); - return true; - } else { - return false; - } -} - -/* Gets the pclabel for this bytecode location's jmptarget. Requires that the - * jmptarget() has been previously defined. */ -static int getjmptarget(jitcompiler *jc, const void *key) { - int pclabel = 0; - bool ok; - - UPB_ASSERT_DEBUGVAR(upb_inttable_lookupptr(&jc->jmpdefined, key, NULL)); - ok = try_getjmptarget(jc, key, &pclabel); - UPB_ASSERT(ok); - return pclabel; -} - -/* Returns a pclabel that serves as a jmp target for the given bytecode pointer. - * This should only be called for code that is jumping to the target; code - * defining the target should use define_jmptarget(). - * - * Creates/allocates a pclabel for this target if one does not exist already. */ -static int jmptarget(jitcompiler *jc, const void *key) { - /* Optimizer sometimes can't figure out that initializing this is unnecessary. - */ - int pclabel = 0; - if (!try_getjmptarget(jc, key, &pclabel)) { - pclabel = alloc_pclabel(jc); - upb_inttable_insertptr(&jc->jmptargets, key, upb_value_uint32(pclabel)); - } - return pclabel; -} - -/* Defines a pclabel associated with the given bytecode location. - * Must be called exactly once by the code that is generating the code for this - * bytecode. - * - * Must be called exactly once before bytecode generation is complete (this is a - * sanity check to make sure the label is defined exactly once). */ -static int define_jmptarget(jitcompiler *jc, const void *key) { -#ifndef NDEBUG - upb_inttable_insertptr(&jc->jmpdefined, key, upb_value_bool(true)); -#endif - return jmptarget(jc, key); -} - -/* Returns a bytecode pc offset relative to the beginning of the group's - * code. */ -static int pcofs(jitcompiler *jc) { - return jc->pc - jc->group->bytecode; -} - -/* Returns a machine code offset corresponding to the given key. - * Requires that this key was defined with define_jmptarget. */ -static int machine_code_ofs(jitcompiler *jc, const void *key) { - int pclabel = getjmptarget(jc, key); - /* Despite its name, this function takes a pclabel and returns the - * corresponding machine code offset. */ - return dasm_getpclabel(jc, pclabel); -} - -/* Returns a machine code offset corresponding to the given method-relative - * bytecode offset. Note that the bytecode offset is relative to the given - * method, but the returned machine code offset is relative to the beginning of - * *all* the machine code. */ -static int machine_code_ofs2(jitcompiler *jc, const upb_pbdecodermethod *method, - int pcofs) { - void *bc_target = jc->group->bytecode + method->code_base.ofs + pcofs; - return machine_code_ofs(jc, bc_target); -} - -/* Given a pcofs relative to this method's base, returns a machine code offset - * relative to jmptarget(dispatch->array) (which is used in jitdispatch as the - * machine code base for dispatch table lookups). */ -uint32_t dispatchofs(jitcompiler *jc, const upb_pbdecodermethod *method, - int pcofs) { - int mc_base = machine_code_ofs(jc, method->dispatch.array); - int mc_target = machine_code_ofs2(jc, method, pcofs); - int ret; - - UPB_ASSERT(mc_base > 0); - UPB_ASSERT(mc_target > 0); - ret = mc_target - mc_base; - UPB_ASSERT(ret > 0); - return ret; -} - -/* Rewrites the dispatch tables into machine code offsets. */ -static void patchdispatch(jitcompiler *jc) { - upb_inttable_iter i; - upb_inttable_begin(&i, &jc->group->methods); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i)); - upb_inttable *dispatch = &method->dispatch; - upb_inttable_iter i2; - - method->is_native_ = true; - - /* Remove DISPATCH_ENDMSG -- only the bytecode interpreter needs it. - * And leaving it around will cause us to find field 0 improperly. */ - upb_inttable_remove(dispatch, DISPATCH_ENDMSG, NULL); - - upb_inttable_begin(&i2, dispatch); - for (; !upb_inttable_done(&i2); upb_inttable_next(&i2)) { - uintptr_t key = upb_inttable_iter_key(&i2); - uint64_t val = upb_value_getuint64(upb_inttable_iter_value(&i2)); - uint64_t newval; - bool ok; - if (key <= UPB_MAX_FIELDNUMBER) { - /* Primary slot. */ - uint64_t ofs; - uint8_t wt1; - uint8_t wt2; - upb_pbdecoder_unpackdispatch(val, &ofs, &wt1, &wt2); - - /* Update offset and repack. */ - ofs = dispatchofs(jc, method, ofs); - newval = upb_pbdecoder_packdispatch(ofs, wt1, wt2); - UPB_ASSERT((int64_t)newval > 0); - } else { - /* Secondary slot. Since we have 64 bits for the value, we use an - * absolute offset. */ - int mcofs = machine_code_ofs2(jc, method, val); - newval = (uint64_t)((char*)jc->group->jit_code + mcofs); - } - ok = upb_inttable_replace(dispatch, key, upb_value_uint64(newval)); - UPB_ASSERT(ok); - } - - /* Update entry point for this method to point at mc base instead of bc - * base. Set this only *after* we have patched the offsets - * (machine_code_ofs2() uses this). */ - method->code_base.ptr = (char*)jc->group->jit_code + machine_code_ofs(jc, method); - - { - upb_byteshandler *h = &method->input_handler_; - upb_byteshandler_setstartstr(h, upb_pbdecoder_startjit, NULL); - upb_byteshandler_setstring(h, jc->group->jit_code, method->code_base.ptr); - upb_byteshandler_setendstr(h, upb_pbdecoder_end, method); - } - } -} - -#ifdef UPB_JIT_LOAD_SO - -static void load_so(jitcompiler *jc) { - /* Dump to a .so file in /tmp and load that, so all the tooling works right - * (for example, debuggers and profilers will see symbol names for the JIT-ted - * code). This is the same goal of the GDB JIT code below, but the GDB JIT - * interface is only used/understood by GDB. Hopefully a standard will - * develop for registering JIT-ted code that all tools will recognize, - * rendering this obsolete. - * - * jc->asmlabels maps: - * pclabel -> char* label - * - * Use this to build mclabels, which maps: - * machine code offset -> char* label - * - * Then we can use mclabels to emit the labels as we iterate over the bytes we - * are outputting. */ - upb_inttable_iter i; - upb_inttable mclabels; - upb_inttable_init(&mclabels, UPB_CTYPE_PTR); - upb_inttable_begin(&i, &jc->asmlabels); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_inttable_insert(&mclabels, - dasm_getpclabel(jc, upb_inttable_iter_key(&i)), - upb_inttable_iter_value(&i)); - } - - /* We write a .s file in text format, as input to the assembler. - * Then we run gcc to turn it into a .so file. - * - * The last "XXXXXX" will be replaced with something randomly generated by - * mkstmemp(). We don't add ".s" to this filename because it makes the string - * processing for mkstemp() and system() more complicated. */ - char s_filename[] = "/tmp/upb-jit-codeXXXXXX"; - int fd = mkstemp(s_filename); - FILE *f; - if (fd >= 0 && (f = fdopen(fd, "wb")) != NULL) { - uint8_t *jit_code = (uint8_t*)jc->group->jit_code; - size_t linelen = 0; - size_t i; - fputs(" .text\n\n", f); - for (i = 0; i < jc->group->jit_size; i++) { - upb_value v; - if (upb_inttable_lookup(&mclabels, i, &v)) { - const char *label = upb_value_getptr(v); - /* "X." makes our JIT syms recognizable as such, which we build into - * other tooling. */ - fprintf(f, "\n\nX.%s:\n", label); - fprintf(f, " .globl X.%s", label); - linelen = 1000; - } - if (linelen >= 77) { - linelen = fprintf(f, "\n .byte %u", jit_code[i]); - } else { - linelen += fprintf(f, ",%u", jit_code[i]); - } - } - fputs("\n", f); - fclose(f); - } else { - fprintf(stderr, "Error opening tmp file for JIT debug output.\n"); - abort(); - } - - /* This is exploitable if you have an adversary on your machine who can write - * to this tmp directory. But this is just for debugging so we don't worry - * too much about that. It shouldn't be prone to races against concurrent - * (non-adversarial) upb JIT's because we used mkstemp(). */ - char *cmd = upb_asprintf("gcc -shared -o %s.so -x assembler %s", s_filename, - s_filename); - if (system(cmd) != 0) { - fprintf(stderr, "Error compiling %s\n", s_filename); - abort(); - } - free(cmd); - - char *so_filename = upb_asprintf("%s.so", s_filename); - - /* Some convenience symlinks. - * This is racy, but just for convenience. */ - int ret; - unlink("/tmp/upb-jit-code.so"); - unlink("/tmp/upb-jit-code.s"); - ret = symlink(s_filename, "/tmp/upb-jit-code.s"); - ret = symlink(so_filename, "/tmp/upb-jit-code.so"); - UPB_UNUSED(ret); // We don't care if this fails. - - jc->group->dl = dlopen(so_filename, RTLD_LAZY); - free(so_filename); - if (!jc->group->dl) { - fprintf(stderr, "Couldn't dlopen(): %s\n", dlerror()); - abort(); - } - - munmap(jc->group->jit_code, jc->group->jit_size); - jc->group->jit_code = dlsym(jc->group->dl, "X.enterjit"); - if (!jc->group->jit_code) { - fprintf(stderr, "Couldn't find enterjit sym\n"); - abort(); - } - - upb_inttable_uninit(&mclabels); -} - -#endif - -void upb_pbdecoder_jit(mgroup *group) { - jitcompiler *jc; - char *jit_code; - int dasm_status; - - group->debug_info = NULL; - group->dl = NULL; - - UPB_ASSERT(group->bytecode); - jc = newjitcompiler(group); - emit_static_asm(jc); - jitbytecode(jc); - - dasm_status = dasm_link(jc, &jc->group->jit_size); - if (dasm_status != DASM_S_OK) { - fprintf(stderr, "DynASM error; returned status: 0x%08x\n", dasm_status); - abort(); - } - - jit_code = mmap(NULL, jc->group->jit_size, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); - dasm_encode(jc, jit_code); - mprotect(jit_code, jc->group->jit_size, PROT_EXEC | PROT_READ); - jc->group->jit_code = (upb_string_handlerfunc *)jit_code; - -#ifdef UPB_JIT_LOAD_SO - load_so(jc); -#endif - - patchdispatch(jc); - - freejitcompiler(jc); - - /* Now the bytecode is no longer needed. */ - free(group->bytecode); - group->bytecode = NULL; -} - -void upb_pbdecoder_freejit(mgroup *group) { - if (!group->jit_code) return; - if (group->dl) { -#ifdef UPB_JIT_LOAD_SO - dlclose(group->dl); -#endif - } else { - munmap((void*)group->jit_code, group->jit_size); - } - free(group->debug_info); -} diff --git a/upb/pb/compile_decoder_x64.dasc b/upb/pb/compile_decoder_x64.dasc deleted file mode 100644 index 7dc1987..0000000 --- a/upb/pb/compile_decoder_x64.dasc +++ /dev/null @@ -1,1150 +0,0 @@ -|// -|// upb - a minimalist implementation of protocol buffers. -|// -|// Copyright (c) 2011-2013 Google Inc. See LICENSE for details. -|// Author: Josh Haberman -|// -|// JIT compiler for upb_pbdecoder on x86-64. Generates machine code from the -|// bytecode generated in compile_decoder.c. -| -|.arch x64 -|.actionlist upb_jit_actionlist -|.globals UPB_JIT_GLOBAL_ -|.globalnames upb_jit_globalnames -| -|// Calling conventions. Note -- this will need to be changed for -|// Windows, which uses a different calling convention! -|.define ARG1_64, rdi -|.define ARG2_8, r6b // DynASM's equivalent to "sil" -- low byte of esi. -|.define ARG2_32, esi -|.define ARG2_64, rsi -|.define ARG3_8, dl -|.define ARG3_32, edx -|.define ARG3_64, rdx -|.define ARG4_64, rcx -|.define ARG5_64, r8 -|.define XMMARG1, xmm0 -| -|// Register allocation / type map. -|// ALL of the code in this file uses these register allocations. -|// When we "call" within this file, we do not use regular calling -|// conventions, but of course when calling to user callbacks we must. -|.define PTR, rbx // DECODER->ptr (unsynced) -|.define DATAEND, r12 // DECODER->data_end (unsynced) -|.define CLOSURE, r13 // FRAME->closure (unsynced) -|.type FRAME, upb_pbdecoder_frame, r14 // DECODER->top (unsynced) -|.type DECODER, upb_pbdecoder, r15 // DECODER (immutable) -|.define DELIMEND, rbp -| -| // Spills unsynced registers back to memory. -|.macro commit_regs -| mov DECODER->top, FRAME -| mov DECODER->ptr, PTR -| mov DECODER->data_end, DATAEND -| // We don't guarantee that delim_end is NULL when out of range like the -| // interpreter does. -| mov DECODER->delim_end, DELIMEND -| sub DELIMEND, DECODER->buf -| add DELIMEND, DECODER->bufstart_ofs -| mov FRAME->end_ofs, DELIMEND -| mov FRAME->sink.closure, CLOSURE -|.endmacro -| -| // Loads unsynced registers from memory back into registers. -|.macro load_regs -| mov FRAME, DECODER->top -| mov PTR, DECODER->ptr -| mov DATAEND, DECODER->data_end -| mov CLOSURE, FRAME->sink.closure -| mov DELIMEND, FRAME->end_ofs -| sub DELIMEND, DECODER->bufstart_ofs -| add DELIMEND, DECODER->buf -|.endmacro -| -| // Calls an external C function at address "addr". -|.macro callp, addr -| mov64 rax, (uintptr_t)addr -| -| // Stack must be 16-byte aligned (x86-64 ABI requires this). -| // -| // OPT: possibly remove this by statically ensuring correct alignment. -| // -| // OPT: use "call rel32" where possible. -| push r12 -| mov r12, rsp -| and rsp, 0xfffffffffffffff0UL // Align stack. -| call rax -| mov rsp, r12 -| pop r12 -|.endmacro -| -|.macro ld64, val -|| { -|| uintptr_t v = (uintptr_t)val; -|| if (v > 0xffffffff) { -| mov64 ARG2_64, v -|| } else if (v) { -| mov ARG2_32, v -|| } else { -| xor ARG2_32, ARG2_32 -|| } -|| } -|.endmacro -| -|.macro load_handler_data, h, arg -| ld64 gethandlerdata(h, arg) -|.endmacro -| -|.macro chkeob, bytes, target -|| if (bytes == 1) { -| cmp PTR, DATAEND -| je target -|| } else { -| mov rcx, DATAEND -| sub rcx, PTR -| cmp rcx, bytes -| jb target -|| } -|.endmacro -| -|.macro chkneob, bytes, target -|| if (bytes == 1) { -| cmp PTR, DATAEND -| jne target -|| } else { -| mov rcx, DATAEND -| sub rcx, PTR -| cmp rcx, bytes -| jae target -|| } -|.endmacro - -|.macro sethas, reg, hasbit -|| if (hasbit >= 0) { -| or byte [reg + ((uint32_t)hasbit / 8)], (1 << ((uint32_t)hasbit % 8)) -|| } -|.endmacro -| -| // Decodes 32-bit varint into rdx, inlining 1 byte. -|.macro dv32 -| chkeob 1, >7 -| movzx edx, byte [PTR] -| test dl, dl -| jns >8 -|7: -| call ->decodev32_fallback -|8: -| add PTR, 1 -|.endmacro - -#define DECODE_EOF -3 - -static upb_func *gethandler(const upb_handlers *h, upb_selector_t sel) { - return h ? upb_handlers_gethandler(h, sel, NULL) : NULL; -} - -/* Defines an "assembly label" for the current code generation offset. - * This label exists *purely* for debugging purposes: it is emitted into - * the .so, and printed as part of JIT debugging output when UPB_JIT_LOAD_SO is - * defined. - * - * We would define this in the .c file except that it conditionally defines a - * pclabel. */ -static void asmlabel(jitcompiler *jc, const char *fmt, ...) { -#ifndef NDEBUG - int ofs = jc->dynasm->section->ofs; - UPB_ASSERT(ofs != jc->lastlabelofs); - jc->lastlabelofs = ofs; -#endif - -#ifndef UPB_JIT_LOAD_SO - UPB_UNUSED(jc); - UPB_UNUSED(fmt); -#else - va_list args; - va_start(args, fmt); - char *str = upb_vasprintf(fmt, args); - va_end(args); - - int pclabel = alloc_pclabel(jc); - /* Normally we would prefer to allocate this inline with the codegen, - * ie. - * |=>asmlabel(...) - * But since we do this conditionally, only when UPB_JIT_LOAD_SO is defined, - * we do it here instead. */ - |=>pclabel: - upb_inttable_insert(&jc->asmlabels, pclabel, upb_value_ptr(str)); -#endif -} - -/* Should only be called when the associated handler is known to exist. */ -static bool alwaysok(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.alwaysok; -} - -static const void *gethandlerdata(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.handler_data; -} - -/* Emit static assembly routines; code that does not vary based on the message - * schema. Since it's not input-dependent, we only need one single copy of it. - * For the moment we generate a single copy per generated handlers. Eventually - * we should generate this code at compile time and link it into the binary so - * we have one copy total. To do that we'll want to be sure that it is within - * 2GB of our JIT code, so that branches between the two are near (rel32). - * - * We'd put this assembly in a .s file directly, but DynASM's ability to - * calculate structure offsets automatically is too useful to pass up (it's way - * more convenient to write DECODER->sink than [rbx + 0x96], especially since - * the latter would have to be changed whenever the structure is updated). */ -static void emit_static_asm(jitcompiler *jc) { - | // Trampolines for entering/exiting the JIT. These are a bit tricky to - | // support full resuming; when we suspend we copy the JIT's portion of - | // the call stack into the upb_pbdecoder and restore it when we resume. - asmlabel(jc, "enterjit"); - |->enterjit: - |1: - | push rbp - | push r15 - | push r14 - | push r13 - | push r12 - | push rbx - | - | mov rbx, ARG2_64 // Preserve JIT method. - | - | mov DECODER, rdi - | callp upb_pbdecoder_resume // Same args as us; reuse regs. - | test eax, eax - | jns >1 - | mov DECODER->saved_rsp, rsp - | mov rax, rbx - | load_regs - | - | // Test whether we have a saved stack to resume. - | mov ARG3_64, DECODER->call_len - | test ARG3_64, ARG3_64 - | jnz >2 - | - | call rax - | - | mov rax, DECODER->size_param - | mov qword DECODER->call_len, 0 - |1: - | pop rbx - | pop r12 - | pop r13 - | pop r14 - | pop r15 - | pop rbp - | ret - | - |2: - | // Resume decoder. - | mov ARG2_64, DECODER->callstack - | sub rsp, ARG3_64 - | mov ARG1_64, rsp - | callp memcpy // Restore stack. - | ret // Return to resumed function (not ->enterjit caller). - | - | // Other code can call this to suspend the JIT. - | // To the calling code, it will appear that the function returns when - | // the JIT resumes, and more buffer space will be available. - | // Args: eax=the value that decode() should return. - asmlabel(jc, "exitjit"); - |->exitjit: - | // Save the stack into DECODER->callstack. - | mov ARG1_64, DECODER->callstack - | mov ARG2_64, rsp - | mov ARG3_64, DECODER->saved_rsp - | sub ARG3_64, rsp - | mov DECODER->call_len, ARG3_64 // Preserve len for next resume. - | mov ebx, eax // Preserve return value across memcpy. - | callp memcpy // Copy stack into decoder. - | mov eax, ebx // This will be our return value. - | - | // Must NOT do this before the memcpy(), otherwise memcpy() will - | // clobber the stack we are trying to save! - | mov rsp, DECODER->saved_rsp - | pop rbx - | pop r12 - | pop r13 - | pop r14 - | pop r15 - | pop rbp - | ret - | - | // Like suspend() in the C decoder, except that the function appears - | // (from the caller's perspective) not to return until the decoder is - | // resumed. - asmlabel(jc, "suspend"); - |->suspend: - | cmp DECODER->ptr, PTR - | je >1 - | mov DECODER->checkpoint, PTR - |1: - | commit_regs - | mov rdi, DECODER - | callp upb_pbdecoder_suspend - | jmp ->exitjit - | - asmlabel(jc, "pushlendelim"); - |->pushlendelim: - |1: - | mov FRAME->sink.closure, CLOSURE - | mov DECODER->checkpoint, PTR - | dv32 - | mov rcx, DELIMEND - | sub rcx, PTR - | sub rcx, rdx - | jb >4 // Len is greater than enclosing message. - | mov FRAME->end_ofs, rcx - | cmp FRAME, DECODER->limit - | je >3 // Stack overflow - | add FRAME, sizeof(upb_pbdecoder_frame) - | mov DELIMEND, PTR - | add DELIMEND, rdx - | mov dword FRAME->groupnum, 0 - | test rcx, rcx - | jz >2 - | mov DATAEND, DECODER->end - | cmp PTR, DELIMEND - | ja >2 - | cmp DELIMEND, DATAEND - | ja >2 - | mov DATAEND, DELIMEND // If DELIMEND >= PTR && DELIMEND < DATAEND - |2: - | ret - |3: - | // Stack overflow error. - | mov PTR, DECODER->checkpoint // Rollback to before the delim len. - | // Prepare seterr args. - | mov ARG1_64, DECODER - | ld64 kPbDecoderStackOverflow - | callp upb_pbdecoder_seterr - | call ->suspend - | jmp <1 - |4: - | // Overextended len. - | mov PTR, DECODER->checkpoint // Rollback to before the delim len. - | // Prepare seterr args. - | mov ARG1_64, DECODER - | ld64 kPbDecoderSubmessageTooLong - | callp upb_pbdecoder_seterr - | call ->suspend - | jmp <1 - | - | // For getting a value that spans a buffer seam. Falls back to C. - |.macro getvalue_slow, func, bytes - | sub rsp, 8 // Need stack space for func to write value to. - |1: - | mov qword [rsp], 0 // For parsing routines that only parse 32 bits. - | mov ARG1_64, DECODER - | mov ARG2_64, rsp - | mov DECODER->checkpoint, PTR - | commit_regs - | callp func - | load_regs - | test eax, eax - | jns >2 - | // Success; return parsed data (in rdx AND xmm0). - | mov rdx, [rsp] - | movsd xmm0, qword [rsp] - | add rsp, 8 - | sub PTR, bytes // Bias our buffer pointer to rejoin the fast-path. - | mov DECODER->ptr, PTR - | ret - |2: - | call ->exitjit // Return eax from decode function. - | jmp <1 - |.endmacro - | - asmlabel(jc, "parse_unknown"); - | // Args: edx=fieldnum, cl=wire type - |->parse_unknown: - | // OPT: handle directly instead of kicking to C. - | // Check for ENDGROUP. - | mov ARG1_64, DECODER - | mov ARG2_32, edx - | movzx ARG3_32, cl - | commit_regs - | callp upb_pbdecoder_skipunknown - | load_regs - | cmp eax, DECODE_ENDGROUP - | jne >1 - | ret // Return eax=DECODE_ENDGROUP, not zero - |1: - | cmp eax, DECODE_OK - | je >1 - | call ->exitjit // Return eax from decode function. - |1: - | xor eax, eax - | ret - | - | // Fallback functions for parsing single values. These are used when the - | // buffer doesn't contain enough remaining data for the fast path. Each - | // primitive type (v32, v64, f32, f64) has two functions: decode & skip. - | // Decode functions return their value in rsi/esi. - | // - | // These functions leave PTR = value_end - fast_path_bytes, so that we can - | // re-join the fast path which will add fast_path_bytes after the callback - | // completes. We also set DECODER->ptr to this value which is a signal to - | // ->suspend that DECODER->checkpoint is up to date. - asmlabel(jc, "skip_decode_f32_fallback"); - |->skipf32_fallback: - |->decodef32_fallback: - | getvalue_slow upb_pbdecoder_decode_f32, 4 - | - asmlabel(jc, "skip_decode_f64_fallback"); - |->skipf64_fallback: - |->decodef64_fallback: - | getvalue_slow upb_pbdecoder_decode_f64, 8 - | - | // Called for varint >= 1 byte. - asmlabel(jc, "skip_decode_v32_fallback"); - |->skipv32_fallback: - |->skipv64_fallback: - | chkeob 16, >1 - | // With at least 16 bytes left, we can do a branch-less SSE version. - | movdqu xmm0, [PTR] - | pmovmskb eax, xmm0 // bits 0-15 are continuation bits, 16-31 are 0. - | not eax - | bsf eax, eax - | cmp al, 10 - | jae ->decode_varint_slow // Error (>10 byte varint). - | add PTR, rax // bsf result is 0-based, so PTR=end-1, as desired. - | ret - | - |1: - | // With fewer than 16 bytes, we have to read byte by byte. - | lea rcx, [PTR + 10] - | mov rax, PTR // Preserve PTR in case of fallback to slow path. - | cmp rcx, DATAEND - | cmova rcx, DATAEND // rcx = MIN(DATAEND, PTR + 10) - |2: - | cmp rax, rcx - | je ->decode_varint_slow - | test byte [rax], 0x80 - | jz >3 - | add rax, 1 - | jmp <2 - |3: - | mov PTR, rax // PTR = varint_end - 1, as desired - | ret - | - | // Returns tag in edx - asmlabel(jc, "decode_unknown_tag_fallback"); - |->decode_unknown_tag_fallback: - | sub rsp, 16 - |1: - | cmp PTR, DELIMEND - | jne >2 - | add rsp, 16 - | xor eax, eax - | ret - |2: - | // OPT: Have a medium-fast path before falling back to _slow. - | mov ARG1_64, DECODER - | mov ARG2_64, rsp - | commit_regs - | callp upb_pbdecoder_decode_varint_slow - | load_regs - | cmp eax, 0 - | jge >3 - | mov edx, [rsp] // Success; return parsed data. - | add rsp, 16 - | ret - |3: - | call ->exitjit // Return eax from decode function. - | jmp <1 - | - | // Called for varint >= 1 byte. - asmlabel(jc, "decode_v32_v64_fallback"); - |->decodev32_fallback: - |->decodev64_fallback: - | chkeob 10, ->decode_varint_slow - | // OPT: do something faster than just calling the C version. - | mov rdi, PTR - | callp upb_vdecode_fast - | test rax, rax - | je ->decode_varint_slow // Unterminated varint. - | mov PTR, rax - | sub PTR, 1 - | mov DECODER->ptr, PTR - | ret - | - asmlabel(jc, "decode_varint_slow"); - |->decode_varint_slow: - | // Slow path: end of buffer or error (varint length >= 10). - | getvalue_slow upb_pbdecoder_decode_varint_slow, 1 - | - | // Args: rsi=expected tag, return=rax (DECODE_{OK,MISMATCH}) - asmlabel(jc, "checktag_fallback"); - |->checktag_fallback: - | sub rsp, 8 - | mov [rsp], rsi // Preserve expected tag. - |1: - | mov ARG1_64, DECODER - | commit_regs - | mov DECODER->checkpoint, PTR - | callp upb_pbdecoder_checktag_slow - | load_regs - | cmp eax, 0 - | jge >2 - | add rsp, 8 - | ret - |2: - | call ->exitjit - | mov rsi, [rsp] - | cmp PTR, DELIMEND - | jne <1 - | mov eax, DECODE_EOF - | add rsp, 8 - | ret - | - | // Args: rsi=upb_inttable, rdx=key, return=rax (-1 if not found). - | // Preserves: rcx, rdx - | // OPT: Could write this in assembly if it's a hotspot. - asmlabel(jc, "hashlookup"); - |->hashlookup: - | push rcx - | push rdx - | sub rsp, 16 - | mov rdi, rsi - | mov rsi, rdx - | mov rdx, rsp - | callp upb_inttable_lookup - | add rsp, 16 - | pop rdx - | pop rcx - | test al, al - | jz >2 // Unknown field. - | mov rax, [rsp-32] // Value from table. - | ret - |2: - | xor rax, rax - | not rax - | ret -} - -static void jitprimitive(jitcompiler *jc, opcode op, - const upb_handlers *h, upb_selector_t sel) { - typedef enum { V32, V64, F32, F64, X } valtype_t; - static valtype_t types[] = { - X, F64, F32, V64, V64, V32, F64, F32, V64, X, X, X, X, V32, V32, F32, F64, - V32, V64 }; - static char fastpath_bytes[] = { 1, 1, 4, 8 }; - const valtype_t vtype = types[op]; - const int fastbytes = fastpath_bytes[vtype]; - upb_func *handler = gethandler(h, sel); - upb_fieldtype_t ftype; - size_t offset; - int32_t hasbit; - - if (handler) { - |1: - | chkneob fastbytes, >3 - |2: - switch (vtype) { - case V32: - | call ->decodev32_fallback - break; - case V64: - | call ->decodev64_fallback - break; - case F32: - | call ->decodef32_fallback - break; - case F64: - | call ->decodef64_fallback - break; - case X: break; - } - | jmp >4 - - /* Fast path decode; for when check_bytes bytes are available. */ - |3: - switch (op) { - case OP_PARSE_SFIXED32: - case OP_PARSE_FIXED32: - | mov edx, dword [PTR] - break; - case OP_PARSE_SFIXED64: - case OP_PARSE_FIXED64: - | mov rdx, qword [PTR] - break; - case OP_PARSE_FLOAT: - | movss xmm0, dword [PTR] - break; - case OP_PARSE_DOUBLE: - | movsd xmm0, qword [PTR] - break; - default: - /* Inline one byte of varint decoding. */ - | movzx edx, byte [PTR] - | test dl, dl - | js <2 // Fallback to slow path for >1 byte varint. - break; - } - - /* Second-stage decode; used for both fast and slow paths */ - /* (only needed for a few types). */ - |4: - switch (op) { - case OP_PARSE_SINT32: - /* 32-bit zig-zag decode. */ - | mov eax, edx - | shr edx, 1 - | and eax, 1 - | neg eax - | xor edx, eax - break; - case OP_PARSE_SINT64: - /* 64-bit zig-zag decode. */ - | mov rax, rdx - | shr rdx, 1 - | and rax, 1 - | neg rax - | xor rdx, rax - break; - case OP_PARSE_BOOL: - | test rdx, rdx - | setne dl - break; - default: break; - } - - /* Call callback (or specialize if we can). */ - if (upb_msg_getscalarhandlerdata(h, sel, &ftype, &offset, &hasbit)) { - switch (ftype) { - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - | mov [CLOSURE + offset], rdx - break; - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_ENUM: - | mov [CLOSURE + offset], edx - break; - case UPB_TYPE_DOUBLE: - | movsd qword [CLOSURE + offset], XMMARG1 - break; - case UPB_TYPE_FLOAT: - | movss dword [CLOSURE + offset], XMMARG1 - break; - case UPB_TYPE_BOOL: - | mov [CLOSURE + offset], dl - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - case UPB_TYPE_MESSAGE: - UPB_ASSERT(false); break; - } - | sethas CLOSURE, hasbit - } else if (handler) { - | mov ARG1_64, CLOSURE - | load_handler_data h, sel - | callp handler - if (!alwaysok(h, sel)) { - | test al, al - | jnz >5 - | call ->suspend - | jmp <1 - |5: - } - } - - /* We do this last so that the checkpoint is not advanced past the user's - * data until the callback has returned success. */ - | add PTR, fastbytes - } else { - /* No handler registered for this value, just skip it. */ - | chkneob fastbytes, >3 - |2: - switch (vtype) { - case V32: - | call ->skipv32_fallback - break; - case V64: - | call ->skipv64_fallback - break; - case F32: - | call ->skipf32_fallback - break; - case F64: - | call ->skipf64_fallback - break; - case X: break; - } - - /* Fast-path skip. */ - |3: - if (vtype == V32 || vtype == V64) { - | test byte [PTR], 0x80 - | jnz <2 - } - | add PTR, fastbytes - } -} - -static void jitdispatch(jitcompiler *jc, - const upb_pbdecodermethod *method) { - /* Lots of room for tweaking/optimization here. */ - - const upb_inttable *dispatch = &method->dispatch; - bool has_hash_entries = (dispatch->t.count > 0); - - /* Whether any of the fields for this message can have two wire types which - * are both valid (packed & non-packed). - * - * OPT: populate this more precisely; not all messages with hash entries have - * this characteristic. */ - bool has_multi_wiretype = has_hash_entries; - - |=>define_jmptarget(jc, &method->dispatch): - |1: - /* Decode the field tag. */ - | mov aword DECODER->checkpoint, PTR - | chkeob 2, >6 - | movzx edx, byte [PTR] - | test dl, dl - | jns >7 // Jump if first byte has no continuation bit. - | movzx ecx, byte [PTR + 1] - | test cl, cl - | js >6 // Jump if second byte has continuation bit. - | // Confirmed two-byte varint. - | shl ecx, 7 - | and edx, 0x7f - | or edx, ecx - | add PTR, 2 - | jmp >8 - |6: - | call ->decode_unknown_tag_fallback - | test eax, eax // Hit DELIMEND? - | jnz >8 - | ret - |7: - | add PTR, 1 - |8: - | mov ecx, edx - | shr edx, 3 - | and cl, 7 - - /* See comment attached to upb_pbdecodermethod.dispatch for layout of the - * dispatch table. */ - |2: - | cmp edx, dispatch->array_size - if (has_hash_entries) { - | jae >7 - } else { - | jae >5 - } - | // OPT: Compact the lookup arr into 32-bit entries. - if ((uintptr_t)dispatch->array > 0x7fffffff) { - | mov64 rax, (uintptr_t)dispatch->array - | mov rax, qword [rax + rdx * 8] - } else { - | mov rax, qword [rdx * 8 + dispatch->array] - } - |3: - | // We take advantage of the fact that non-present entries are stored - | // as -1, which will result in wire types that will never match. - | cmp al, cl - if (has_multi_wiretype) { - | jne >6 - } else { - | jne >5 - } - | shr rax, 16 - | - | // Load the machine code address from the table entry. - | // The table entry is relative to the dispatch->array jmptarget - | // (patchdispatch() took care of this) which is the same as - | // local label "4". The "lea" is really just trying to do - | // lea rax, [>4 + rax] - | // - | // But we can't write that directly for some reason, so we use - | // rdx as a temporary. - | lea rdx, [>4] - |=>define_jmptarget(jc, dispatch->array): - |4: - | add rax, rdx - | ret - | - |5: - | // Field isn't in our table. - | - | // For pushing unknown fields to the unknown field handler. - | mov64 rax, (uintptr_t)method->dest_handlers_ - | mov FRAME->sink.handlers, rax - | - | call ->parse_unknown - | test eax, eax // ENDGROUP? - | jz <1 - | lea rax, [>9] // ENDGROUP; Load address of OP_ENDMSG. - | ret - - if (has_multi_wiretype) { - |6: - | // Primary wire type didn't match, check secondary wire type. - | cmp ah, cl - | jne <5 - | // Secondary wire type is a match, look up fn + UPB_MAX_FIELDNUMBER. - | add rdx, UPB_MAX_FIELDNUMBER - | // This key will never be in the array part, so do a hash lookup. - UPB_ASSERT(has_hash_entries); - | ld64 dispatch - | jmp ->hashlookup // Tail call. - } - - if (has_hash_entries) { - |7: - | // Hash table lookup. - | ld64 dispatch - | call ->hashlookup - | jmp <3 - } -} - -static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, - const upb_pbdecodermethod *method) { - /* Internally we parse unknown fields; if this runs us into DELIMEND we jump - * to the corresponding DELIMEND target (either msg end or repeated field - * end), which we find from the OP_CHECKDELIM which must have necessarily - * preceded us. */ - uint32_t last_instruction = *(jc->pc - 2); - int last_arg = (int32_t)last_instruction >> 8; - uint32_t *delimend = (jc->pc - 1) + last_arg; - const size_t ptr_words = sizeof(void*) / sizeof(uint32_t); - - UPB_ASSERT((last_instruction & 0xff) == OP_CHECKDELIM); - - if (getop(*(jc->pc - 1)) == OP_TAGN) { - jc->pc += ptr_words; - } - - | chkneob n, >1 - - | // OPT: this is way too much fallback code to put here. - | // Reduce and/or move to a separate section to make better icache usage. - | ld64 tag - | call ->checktag_fallback - | cmp eax, DECODE_MISMATCH - | je >3 - | cmp eax, DECODE_EOF - | je =>jmptarget(jc, delimend) - | jmp >5 - - |1: - switch (n) { - case 1: - | cmp byte [PTR], tag - break; - case 2: - | cmp word [PTR], tag - break; - case 3: - | // OPT: Slightly more efficient code, but depends on an extra byte. - | // mov eax, dword [PTR] - | // shl eax, 8 - | // cmp eax, tag << 8 - | cmp word [PTR], (tag & 0xffff) - | jne >2 - | cmp byte [PTR + 2], (tag >> 16) - |2: - break; - case 4: - | cmp dword [PTR], tag - break; - case 5: - | cmp dword [PTR], (tag & 0xffffffff) - | jne >3 - | cmp byte [PTR + 4], (tag >> 32) - } - | je >4 - |3: - if (ofs == 0) { - | call =>jmptarget(jc, &method->dispatch) - | test rax, rax - | jz =>jmptarget(jc, delimend) - | jmp rax - } else { - | jmp =>jmptarget(jc, jc->pc + ofs) - } - |4: - | add PTR, n - |5: -} - -/* Compile the bytecode to x64. */ -static void jitbytecode(jitcompiler *jc) { - upb_pbdecodermethod *method = NULL; - const upb_handlers *h = NULL; - for (jc->pc = jc->group->bytecode; jc->pc < jc->group->bytecode_end; ) { - int32_t instr = *jc->pc; - opcode op = instr & 0xff; - uint32_t arg = instr >> 8; - int32_t longofs = arg; - - if (op != OP_SETDISPATCH) { - /* Skipped for SETDISPATCH because it defines its own asmlabel for the - * dispatch code it emits. */ - asmlabel(jc, "0x%lx.%s", pcofs(jc), upb_pbdecoder_getopname(op)); - - /* Skipped for SETDISPATCH because it should point at the function - * prologue, not the dispatch function that is emitted first. - * TODO: optimize this to only define pclabels that are actually used. */ - |=>define_jmptarget(jc, jc->pc): - } - - jc->pc++; - - switch (op) { - case OP_STARTMSG: { - upb_func *startmsg = gethandler(h, UPB_STARTMSG_SELECTOR); - if (startmsg) { - /* bool startmsg(void *closure, const void *hd) */ - |1: - | mov ARG1_64, CLOSURE - | load_handler_data h, UPB_STARTMSG_SELECTOR - | callp startmsg - if (!alwaysok(h, UPB_STARTMSG_SELECTOR)) { - | test al, al - | jnz >2 - | call ->suspend - | jmp <1 - |2: - } - } else { - | nop - } - break; - } - case OP_ENDMSG: { - upb_func *endmsg = gethandler(h, UPB_ENDMSG_SELECTOR); - |9: - if (endmsg) { - /* bool endmsg(void *closure, const void *hd, upb_status *status) */ - | mov ARG1_64, CLOSURE - | load_handler_data h, UPB_ENDMSG_SELECTOR - | mov ARG3_64, DECODER->status - | callp endmsg - } - break; - } - case OP_SETDISPATCH: { - uint32_t *op_pc = jc->pc - 1; - const char *msgname; - upb_inttable *dispatch; - - /* Load info for new method. */ - memcpy(&dispatch, jc->pc, sizeof(void*)); - jc->pc += sizeof(void*) / sizeof(uint32_t); - /* The OP_SETDISPATCH bytecode contains a pointer that is - * &method->dispatch; we want to go backwards and recover method. */ - method = - (void*)((char*)dispatch - offsetof(upb_pbdecodermethod, dispatch)); - /* May be NULL, in which case no handlers for this message will be found. - * OPT: we should do better by completely skipping the message in this - * case instead of parsing it field by field. We should also do the skip - * in the containing message's code. */ - h = method->dest_handlers_; - msgname = upb_msgdef_fullname(upb_handlers_msgdef(h)); - - /* Emit dispatch code for new method. */ - asmlabel(jc, "0x%lx.dispatch.%s", pcofs(jc), msgname); - jitdispatch(jc, method); - - /* Emit function prologue for new method. */ - asmlabel(jc, "0x%lx.parse.%s", pcofs(jc), msgname); - |=>define_jmptarget(jc, op_pc): - |=>define_jmptarget(jc, method): - | sub rsp, 8 - - break; - } - case OP_PARSE_DOUBLE: - case OP_PARSE_FLOAT: - case OP_PARSE_INT64: - case OP_PARSE_UINT64: - case OP_PARSE_INT32: - case OP_PARSE_FIXED64: - case OP_PARSE_FIXED32: - case OP_PARSE_BOOL: - case OP_PARSE_UINT32: - case OP_PARSE_SFIXED32: - case OP_PARSE_SFIXED64: - case OP_PARSE_SINT32: - case OP_PARSE_SINT64: - jitprimitive(jc, op, h, arg); - break; - case OP_STARTSEQ: - case OP_STARTSUBMSG: - case OP_STARTSTR: { - upb_func *start = gethandler(h, arg); - if (start) { - /* void *startseq(void *closure, const void *hd) - * void *startsubmsg(void *closure, const void *hd) - * void *startstr(void *closure, const void *hd, size_t size_hint) */ - |1: - | mov ARG1_64, CLOSURE - | load_handler_data h, arg - if (op == OP_STARTSTR) { - | mov ARG3_64, DELIMEND - | sub ARG3_64, PTR - } - | callp start - if (!alwaysok(h, arg)) { - | test rax, rax - | jnz >2 - | call ->suspend - | jmp <1 - |2: - } - | mov CLOSURE, rax - } else { - /* TODO: nop is only required because of asmlabel(). */ - | nop - } - break; - } - case OP_ENDSEQ: - case OP_ENDSUBMSG: - case OP_ENDSTR: { - upb_func *end = gethandler(h, arg); - if (end) { - /* bool endseq(void *closure, const void *hd) - * bool endsubmsg(void *closure, const void *hd) - * bool endstr(void *closure, const void *hd) */ - |1: - | mov ARG1_64, CLOSURE - | load_handler_data h, arg - | callp end - if (!alwaysok(h, arg)) { - | test al, al - | jnz >2 - | call ->suspend - | jmp <1 - |2: - } - } else { - /* TODO: nop is only required because of asmlabel(). */ - | nop - } - break; - } - case OP_STRING: { - upb_func *str = gethandler(h, arg); - | cmp PTR, DELIMEND - | je >4 - |1: - | cmp PTR, DATAEND - | jne >2 - | call ->suspend - | jmp <1 - |2: - if (str) { - /* size_t str(void *closure, const void *hd, const char *str, - * size_t n) */ - | mov ARG1_64, CLOSURE - | load_handler_data h, arg - | mov ARG3_64, PTR - | mov ARG4_64, DATAEND - | sub ARG4_64, PTR - | mov ARG5_64, qword DECODER->handle - | callp str - | add PTR, rax - if (!alwaysok(h, arg)) { - | cmp PTR, DATAEND - | je >3 - | call ->strret_fallback - |3: - } - } else { - | mov PTR, DATAEND - } - | cmp PTR, DELIMEND - | jne <1 - |4: - break; - } - case OP_PUSHTAGDELIM: - | mov FRAME->sink.closure, CLOSURE - | // This shouldn't need to be read, because tag-delimited fields - | // shouldn't have an OP_SETDELIM after them. But for the moment - | // non-packed repeated fields do OP_SETDELIM so they can share more - | // code with the packed code-path. If this is changed later, this - | // store can be removed. - | mov qword FRAME->end_ofs, 0 - | cmp FRAME, DECODER->limit - | je ->err - | add FRAME, sizeof(upb_pbdecoder_frame) - | mov dword FRAME->groupnum, arg - break; - case OP_PUSHLENDELIM: - | call ->pushlendelim - break; - case OP_POP: - | sub FRAME, sizeof(upb_pbdecoder_frame) - | mov CLOSURE, FRAME->sink.closure - break; - case OP_SETDELIM: - /* OPT: experiment with testing vs old offset to optimize away. */ - | mov DATAEND, DECODER->end - | add DELIMEND, FRAME->end_ofs - | cmp DELIMEND, DECODER->buf - | jb >1 - | cmp DELIMEND, DATAEND - | ja >1 // OPT: try cmov. - | mov DATAEND, DELIMEND - |1: - break; - case OP_SETBIGGROUPNUM: - | mov dword FRAME->groupnum, *jc->pc++ - break; - case OP_CHECKDELIM: - | cmp DELIMEND, PTR - | je =>jmptarget(jc, jc->pc + longofs) - break; - case OP_CALL: - | call =>jmptarget(jc, jc->pc + longofs) - break; - case OP_BRANCH: - | jmp =>jmptarget(jc, jc->pc + longofs); - break; - case OP_RET: - |9: - | add rsp, 8 - | ret - break; - case OP_TAG1: - jittag(jc, (arg >> 8) & 0xff, 1, (int8_t)arg, method); - break; - case OP_TAG2: - jittag(jc, (arg >> 8) & 0xffff, 2, (int8_t)arg, method); - break; - case OP_TAGN: { - uint64_t tag; - memcpy(&tag, jc->pc, 8); - jittag(jc, tag, arg >> 8, (int8_t)arg, method); - break; - } - case OP_DISPATCH: - | call =>jmptarget(jc, &method->dispatch) - break; - case OP_HALT: - UPB_ASSERT(false); - } - } - - asmlabel(jc, "eof"); - | nop -} diff --git a/upb/pb/compile_decoder_x64.h b/upb/pb/compile_decoder_x64.h deleted file mode 100644 index 4a4dffc..0000000 --- a/upb/pb/compile_decoder_x64.h +++ /dev/null @@ -1,1737 +0,0 @@ -/* -** This file has been pre-processed with DynASM. -** http://luajit.org/dynasm.html -** DynASM version 1.3.0, DynASM x64 version 1.3.0 -** DO NOT EDIT! The original file is in "upb/pb/compile_decoder_x64.dasc". -*/ - -#if DASM_VERSION != 10300 -#error "Version mismatch between DynASM and included encoding engine" -#endif - -# 1 "upb/pb/compile_decoder_x64.dasc" -/*|// */ -/*|// upb - a minimalist implementation of protocol buffers. */ -/*|// */ -/*|// Copyright (c) 2011-2013 Google Inc. See LICENSE for details. */ -/*|// Author: Josh Haberman */ -/*|// */ -/*|// JIT compiler for upb_pbdecoder on x86-64. Generates machine code from the */ -/*|// bytecode generated in compile_decoder.c. */ -/*| */ -/*|.arch x64 */ -/*|.actionlist upb_jit_actionlist */ -static const unsigned char upb_jit_actionlist[2467] = { - 249,255,248,10,248,1,85,65,87,65,86,65,85,65,84,83,72,137,252,243,73,137, - 252,255,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137, - 228,65,92,133,192,15,137,244,247,73,137,167,233,72,137,216,77,139,183,233, - 73,139,159,233,77,139,167,233,77,139,174,233,73,139,174,233,73,43,175,233, - 73,3,175,233,73,139,151,233,72,133,210,15,133,244,248,252,255,208,73,139, - 135,233,73,199,135,233,0,0,0,0,248,1,255,91,65,92,65,93,65,94,65,95,93,195, - 248,2,73,139,183,233,72,41,212,72,137,231,72,184,237,237,65,84,73,137,228, - 72,129,228,239,252,255,208,76,137,228,65,92,195,255,248,11,73,139,191,233, - 72,137,230,73,139,151,233,72,41,226,73,137,151,233,137,195,72,184,237,237, - 65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,137,216,73,139, - 167,233,91,65,92,65,93,65,94,65,95,93,195,255,248,12,73,57,159,233,15,132, - 244,247,73,137,159,233,248,1,77,137,183,233,73,137,159,233,77,137,167,233, - 73,137,175,233,73,43,175,233,73,3,175,233,73,137,174,233,77,137,174,233,76, - 137,252,255,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76, - 137,228,65,92,252,233,244,11,255,248,13,248,1,77,137,174,233,73,137,159,233, - 255,76,57,227,15,132,244,253,255,76,137,225,72,41,217,72,131,252,249,1,15, - 130,244,253,255,15,182,19,132,210,15,137,244,254,248,7,232,244,14,248,8,72, - 131,195,1,72,137,252,233,72,41,217,72,41,209,15,130,244,250,73,137,142,233, - 77,59,183,233,15,132,244,249,73,129,198,239,72,137,221,72,1,213,65,199,134, - 233,0,0,0,0,72,133,201,15,132,244,248,77,139,167,233,72,57,252,235,15,135, - 244,248,76,57,229,15,135,244,248,255,73,137,252,236,248,2,195,248,3,73,139, - 159,233,76,137,252,255,255,72,190,237,237,255,190,237,255,49,252,246,255, - 72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65, - 92,232,244,12,252,233,244,1,248,4,73,139,159,233,76,137,252,255,255,72,184, - 237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,232, - 244,12,252,233,244,1,255,248,15,76,137,252,255,137,214,15,182,209,77,137, - 183,233,73,137,159,233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175, - 233,73,137,174,233,77,137,174,233,72,184,237,237,65,84,73,137,228,72,129, - 228,239,252,255,208,76,137,228,65,92,77,139,183,233,73,139,159,233,77,139, - 167,233,77,139,174,233,73,139,174,233,73,43,175,233,73,3,175,233,129,252, - 248,239,255,15,133,244,247,195,248,1,129,252,248,239,15,132,244,247,232,244, - 11,248,1,49,192,195,255,248,16,248,17,72,131,252,236,8,248,1,72,199,4,36, - 0,0,0,0,76,137,252,255,72,137,230,73,137,159,233,77,137,183,233,73,137,159, - 233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175,233,73,137,174,233, - 77,137,174,233,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208, - 76,137,228,65,92,77,139,183,233,73,139,159,233,77,139,167,233,77,139,174, - 233,73,139,174,233,255,73,43,175,233,73,3,175,233,133,192,15,137,244,248, - 72,139,20,36,252,242,15,16,4,36,72,131,196,8,72,131,252,235,4,73,137,159, - 233,195,248,2,232,244,11,252,233,244,1,255,248,18,248,19,72,131,252,236,8, - 248,1,72,199,4,36,0,0,0,0,76,137,252,255,72,137,230,73,137,159,233,77,137, - 183,233,73,137,159,233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175, - 233,73,137,174,233,77,137,174,233,72,184,237,237,65,84,73,137,228,72,129, - 228,239,252,255,208,76,137,228,65,92,77,139,183,233,73,139,159,233,77,139, - 167,233,77,139,174,233,73,139,174,233,255,73,43,175,233,73,3,175,233,133, - 192,15,137,244,248,72,139,20,36,252,242,15,16,4,36,72,131,196,8,72,131,252, - 235,8,73,137,159,233,195,248,2,232,244,11,252,233,244,1,255,248,20,248,21, - 255,76,57,227,15,132,244,247,255,76,137,225,72,41,217,72,131,252,249,16,15, - 130,244,247,255,252,243,15,111,3,102,15,215,192,252,247,208,15,188,192,60, - 10,15,131,244,22,72,1,195,195,248,1,72,141,139,233,72,137,216,76,57,225,73, - 15,71,204,248,2,72,57,200,15,132,244,22,252,246,0,128,15,132,244,249,72,131, - 192,1,252,233,244,2,248,3,72,137,195,195,255,248,23,72,131,252,236,16,248, - 1,72,57,252,235,15,133,244,248,72,131,196,16,49,192,195,248,2,76,137,252, - 255,72,137,230,77,137,183,233,73,137,159,233,77,137,167,233,73,137,175,233, - 73,43,175,233,73,3,175,233,73,137,174,233,77,137,174,233,72,184,237,237,65, - 84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,77,139,183,233, - 73,139,159,233,77,139,167,233,77,139,174,233,255,73,139,174,233,73,43,175, - 233,73,3,175,233,131,252,248,0,15,141,244,249,139,20,36,72,131,196,16,195, - 248,3,232,244,11,252,233,244,1,255,248,14,248,24,255,76,57,227,15,132,244, - 22,255,76,137,225,72,41,217,72,131,252,249,10,15,130,244,22,255,72,137,223, - 72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65, - 92,72,133,192,15,132,244,22,72,137,195,72,131,252,235,1,73,137,159,233,195, - 255,248,22,72,131,252,236,8,248,1,72,199,4,36,0,0,0,0,76,137,252,255,72,137, - 230,73,137,159,233,77,137,183,233,73,137,159,233,77,137,167,233,73,137,175, - 233,73,43,175,233,73,3,175,233,73,137,174,233,77,137,174,233,72,184,237,237, - 65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,77,139,183,233, - 73,139,159,233,77,139,167,233,77,139,174,233,73,139,174,233,73,43,175,233, - 255,73,3,175,233,133,192,15,137,244,248,72,139,20,36,252,242,15,16,4,36,72, - 131,196,8,72,131,252,235,1,73,137,159,233,195,248,2,232,244,11,252,233,244, - 1,255,248,25,72,131,252,236,8,72,137,52,36,248,1,76,137,252,255,77,137,183, - 233,73,137,159,233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175,233, - 73,137,174,233,77,137,174,233,73,137,159,233,72,184,237,237,65,84,73,137, - 228,72,129,228,239,252,255,208,76,137,228,65,92,77,139,183,233,73,139,159, - 233,77,139,167,233,77,139,174,233,73,139,174,233,73,43,175,233,255,73,3,175, - 233,131,252,248,0,15,141,244,248,72,131,196,8,195,248,2,232,244,11,72,139, - 52,36,72,57,252,235,15,133,244,1,184,237,72,131,196,8,195,255,248,26,81,82, - 72,131,252,236,16,72,137,252,247,72,137,214,72,137,226,72,184,237,237,65, - 84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,72,131,196,16,90, - 89,132,192,15,132,244,248,72,139,68,36,224,195,248,2,72,49,192,72,252,247, - 208,195,255,76,57,227,15,133,244,249,255,76,137,225,72,41,217,72,129,252, - 249,239,15,131,244,249,255,248,2,255,232,244,14,255,232,244,24,255,232,244, - 17,255,232,244,19,255,252,233,244,250,255,248,3,255,139,19,255,72,139,19, - 255,252,243,15,16,3,255,252,242,15,16,3,255,15,182,19,132,210,15,136,244, - 2,255,248,4,255,137,208,209,252,234,131,224,1,252,247,216,49,194,255,72,137, - 208,72,209,252,234,72,131,224,1,72,252,247,216,72,49,194,255,72,133,210,15, - 149,210,255,73,137,149,233,255,65,137,149,233,255,252,242,65,15,17,133,233, - 255,252,243,65,15,17,133,233,255,65,136,149,233,255,65,128,141,233,235,255, - 76,137,252,239,255,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255, - 208,76,137,228,65,92,255,132,192,15,133,244,251,232,244,12,252,233,244,1, - 248,5,255,72,129,195,239,255,232,244,20,255,232,244,21,255,232,244,16,255, - 232,244,18,255,252,246,3,128,15,133,244,2,255,249,248,1,255,76,57,227,15, - 132,244,252,255,76,137,225,72,41,217,72,131,252,249,2,15,130,244,252,255, - 15,182,19,132,210,15,137,244,253,15,182,139,233,132,201,15,136,244,252,193, - 225,7,131,226,127,9,202,72,131,195,2,252,233,244,254,248,6,232,244,23,133, - 192,15,133,244,254,195,248,7,72,131,195,1,248,8,137,209,193,252,234,3,128, - 225,7,255,248,2,129,252,250,239,255,15,131,244,253,255,15,131,244,251,255, - 72,184,237,237,72,139,4,208,255,72,139,4,213,237,255,248,3,56,200,255,15, - 133,244,252,255,15,133,244,251,255,72,193,232,16,72,141,21,244,250,249,248, - 4,72,1,208,195,248,5,72,184,237,237,73,137,134,233,232,244,15,133,192,15, - 132,244,1,72,141,5,244,255,195,255,248,6,56,204,15,133,244,5,72,129,194,239, - 255,252,233,244,26,255,248,7,255,232,244,26,252,233,244,3,255,76,57,227,15, - 133,244,247,255,76,137,225,72,41,217,72,129,252,249,239,15,131,244,247,255, - 232,244,25,129,252,248,239,15,132,244,249,129,252,248,239,15,132,245,252, - 233,244,251,255,128,59,235,255,102,129,59,238,255,102,129,59,238,15,133,244, - 248,128,187,233,235,248,2,255,129,59,239,255,129,59,239,15,133,244,249,128, - 187,233,235,255,15,132,244,250,248,3,255,232,245,72,133,192,15,132,245,252, - 255,224,255,252,233,245,255,248,4,72,129,195,239,248,5,255,248,1,76,137,252, - 239,255,132,192,15,133,244,248,232,244,12,252,233,244,1,248,2,255,144,255, - 248,9,255,73,139,151,233,72,184,237,237,65,84,73,137,228,72,129,228,239,252, - 255,208,76,137,228,65,92,255,249,249,72,131,252,236,8,255,72,137,252,234, - 72,41,218,255,72,133,192,15,133,244,248,232,244,12,252,233,244,1,248,2,255, - 73,137,197,255,72,57,252,235,15,132,244,250,248,1,76,57,227,15,133,244,248, - 232,244,12,252,233,244,1,248,2,255,72,137,218,76,137,225,72,41,217,77,139, - 135,233,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137, - 228,65,92,72,1,195,255,76,57,227,15,132,244,249,232,244,27,248,3,255,76,137, - 227,255,72,57,252,235,15,133,244,1,248,4,255,77,137,174,233,73,199,134,233, - 0,0,0,0,77,59,183,233,15,132,244,28,73,129,198,239,65,199,134,233,237,255, - 232,244,13,255,73,129,252,238,239,77,139,174,233,255,77,139,167,233,73,3, - 174,233,73,59,175,233,15,130,244,247,76,57,229,15,135,244,247,73,137,252, - 236,248,1,255,72,57,221,15,132,245,255,232,245,255,248,9,72,131,196,8,195, - 255 -}; - -# 12 "upb/pb/compile_decoder_x64.dasc" -/*|.globals UPB_JIT_GLOBAL_ */ -enum { - UPB_JIT_GLOBAL_enterjit, - UPB_JIT_GLOBAL_exitjit, - UPB_JIT_GLOBAL_suspend, - UPB_JIT_GLOBAL_pushlendelim, - UPB_JIT_GLOBAL_decodev32_fallback, - UPB_JIT_GLOBAL_parse_unknown, - UPB_JIT_GLOBAL_skipf32_fallback, - UPB_JIT_GLOBAL_decodef32_fallback, - UPB_JIT_GLOBAL_skipf64_fallback, - UPB_JIT_GLOBAL_decodef64_fallback, - UPB_JIT_GLOBAL_skipv32_fallback, - UPB_JIT_GLOBAL_skipv64_fallback, - UPB_JIT_GLOBAL_decode_varint_slow, - UPB_JIT_GLOBAL_decode_unknown_tag_fallback, - UPB_JIT_GLOBAL_decodev64_fallback, - UPB_JIT_GLOBAL_checktag_fallback, - UPB_JIT_GLOBAL_hashlookup, - UPB_JIT_GLOBAL_strret_fallback, - UPB_JIT_GLOBAL_err, - UPB_JIT_GLOBAL__MAX -}; -# 13 "upb/pb/compile_decoder_x64.dasc" -/*|.globalnames upb_jit_globalnames */ -static const char *const upb_jit_globalnames[] = { - "enterjit", - "exitjit", - "suspend", - "pushlendelim", - "decodev32_fallback", - "parse_unknown", - "skipf32_fallback", - "decodef32_fallback", - "skipf64_fallback", - "decodef64_fallback", - "skipv32_fallback", - "skipv64_fallback", - "decode_varint_slow", - "decode_unknown_tag_fallback", - "decodev64_fallback", - "checktag_fallback", - "hashlookup", - "strret_fallback", - "err", - (const char *)0 -}; -# 14 "upb/pb/compile_decoder_x64.dasc" -/*| */ -/*|// Calling conventions. Note -- this will need to be changed for */ -/*|// Windows, which uses a different calling convention! */ -/*|.define ARG1_64, rdi */ -/*|.define ARG2_8, r6b // DynASM's equivalent to "sil" -- low byte of esi. */ -/*|.define ARG2_32, esi */ -/*|.define ARG2_64, rsi */ -/*|.define ARG3_8, dl */ -/*|.define ARG3_32, edx */ -/*|.define ARG3_64, rdx */ -/*|.define ARG4_64, rcx */ -/*|.define ARG5_64, r8 */ -/*|.define XMMARG1, xmm0 */ -/*| */ -/*|// Register allocation / type map. */ -/*|// ALL of the code in this file uses these register allocations. */ -/*|// When we "call" within this file, we do not use regular calling */ -/*|// conventions, but of course when calling to user callbacks we must. */ -/*|.define PTR, rbx // DECODER->ptr (unsynced) */ -/*|.define DATAEND, r12 // DECODER->data_end (unsynced) */ -/*|.define CLOSURE, r13 // FRAME->closure (unsynced) */ -/*|.type FRAME, upb_pbdecoder_frame, r14 // DECODER->top (unsynced) */ -#define Dt1(_V) (int)(ptrdiff_t)&(((upb_pbdecoder_frame *)0)_V) -# 36 "upb/pb/compile_decoder_x64.dasc" -/*|.type DECODER, upb_pbdecoder, r15 // DECODER (immutable) */ -#define Dt2(_V) (int)(ptrdiff_t)&(((upb_pbdecoder *)0)_V) -# 37 "upb/pb/compile_decoder_x64.dasc" -/*|.define DELIMEND, rbp */ -/*| */ -/*| // Spills unsynced registers back to memory. */ -/*|.macro commit_regs */ -/*| mov DECODER->top, FRAME */ -/*| mov DECODER->ptr, PTR */ -/*| mov DECODER->data_end, DATAEND */ -/*| // We don't guarantee that delim_end is NULL when out of range like the */ -/*| // interpreter does. */ -/*| mov DECODER->delim_end, DELIMEND */ -/*| sub DELIMEND, DECODER->buf */ -/*| add DELIMEND, DECODER->bufstart_ofs */ -/*| mov FRAME->end_ofs, DELIMEND */ -/*| mov FRAME->sink.closure, CLOSURE */ -/*|.endmacro */ -/*| */ -/*| // Loads unsynced registers from memory back into registers. */ -/*|.macro load_regs */ -/*| mov FRAME, DECODER->top */ -/*| mov PTR, DECODER->ptr */ -/*| mov DATAEND, DECODER->data_end */ -/*| mov CLOSURE, FRAME->sink.closure */ -/*| mov DELIMEND, FRAME->end_ofs */ -/*| sub DELIMEND, DECODER->bufstart_ofs */ -/*| add DELIMEND, DECODER->buf */ -/*|.endmacro */ -/*| */ -/*| // Calls an external C function at address "addr". */ -/*|.macro callp, addr */ -/*| mov64 rax, (uintptr_t)addr */ -/*| */ -/*| // Stack must be 16-byte aligned (x86-64 ABI requires this). */ -/*| // */ -/*| // OPT: possibly remove this by statically ensuring correct alignment. */ -/*| // */ -/*| // OPT: use "call rel32" where possible. */ -/*| push r12 */ -/*| mov r12, rsp */ -/*| and rsp, 0xfffffffffffffff0UL // Align stack. */ -/*| call rax */ -/*| mov rsp, r12 */ -/*| pop r12 */ -/*|.endmacro */ -/*| */ -/*|.macro ld64, val */ -/*|| { */ -/*|| uintptr_t v = (uintptr_t)val; */ -/*|| if (v > 0xffffffff) { */ -/*| mov64 ARG2_64, v */ -/*|| } else if (v) { */ -/*| mov ARG2_32, v */ -/*|| } else { */ -/*| xor ARG2_32, ARG2_32 */ -/*|| } */ -/*|| } */ -/*|.endmacro */ -/*| */ -/*|.macro load_handler_data, h, arg */ -/*| ld64 gethandlerdata(h, arg) */ -/*|.endmacro */ -/*| */ -/*|.macro chkeob, bytes, target */ -/*|| if (bytes == 1) { */ -/*| cmp PTR, DATAEND */ -/*| je target */ -/*|| } else { */ -/*| mov rcx, DATAEND */ -/*| sub rcx, PTR */ -/*| cmp rcx, bytes */ -/*| jb target */ -/*|| } */ -/*|.endmacro */ -/*| */ -/*|.macro chkneob, bytes, target */ -/*|| if (bytes == 1) { */ -/*| cmp PTR, DATAEND */ -/*| jne target */ -/*|| } else { */ -/*| mov rcx, DATAEND */ -/*| sub rcx, PTR */ -/*| cmp rcx, bytes */ -/*| jae target */ -/*|| } */ -/*|.endmacro */ - -/*|.macro sethas, reg, hasbit */ -/*|| if (hasbit >= 0) { */ -/*| or byte [reg + ((uint32_t)hasbit / 8)], (1 << ((uint32_t)hasbit % 8)) */ -/*|| } */ -/*|.endmacro */ -/*| */ -/*| // Decodes 32-bit varint into rdx, inlining 1 byte. */ -/*|.macro dv32 */ -/*| chkeob 1, >7 */ -/*| movzx edx, byte [PTR] */ -/*| test dl, dl */ -/*| jns >8 */ -/*|7: */ -/*| call ->decodev32_fallback */ -/*|8: */ -/*| add PTR, 1 */ -/*|.endmacro */ - -#define DECODE_EOF -3 - -static upb_func *gethandler(const upb_handlers *h, upb_selector_t sel) { - return h ? upb_handlers_gethandler(h, sel, NULL) : NULL; -} - -/* Defines an "assembly label" for the current code generation offset. - * This label exists *purely* for debugging purposes: it is emitted into - * the .so, and printed as part of JIT debugging output when UPB_JIT_LOAD_SO is - * defined. - * - * We would define this in the .c file except that it conditionally defines a - * pclabel. */ -static void asmlabel(jitcompiler *jc, const char *fmt, ...) { -#ifndef NDEBUG - int ofs = jc->dynasm->section->ofs; - UPB_ASSERT(ofs != jc->lastlabelofs); - jc->lastlabelofs = ofs; -#endif - -#ifndef UPB_JIT_LOAD_SO - UPB_UNUSED(jc); - UPB_UNUSED(fmt); -#else - va_list args; - va_start(args, fmt); - char *str = upb_vasprintf(fmt, args); - va_end(args); - - int pclabel = alloc_pclabel(jc); - /* Normally we would prefer to allocate this inline with the codegen, - * ie. - * |=>asmlabel(...) - * But since we do this conditionally, only when UPB_JIT_LOAD_SO is defined, - * we do it here instead. */ - /*|=>pclabel: */ - dasm_put(Dst, 0, pclabel); -# 176 "upb/pb/compile_decoder_x64.dasc" - upb_inttable_insert(&jc->asmlabels, pclabel, upb_value_ptr(str)); -#endif -} - -/* Should only be called when the associated handler is known to exist. */ -static bool alwaysok(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.alwaysok; -} - -static const void *gethandlerdata(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.handler_data; -} - -/* Emit static assembly routines; code that does not vary based on the message - * schema. Since it's not input-dependent, we only need one single copy of it. - * For the moment we generate a single copy per generated handlers. Eventually - * we should generate this code at compile time and link it into the binary so - * we have one copy total. To do that we'll want to be sure that it is within - * 2GB of our JIT code, so that branches between the two are near (rel32). - * - * We'd put this assembly in a .s file directly, but DynASM's ability to - * calculate structure offsets automatically is too useful to pass up (it's way - * more convenient to write DECODER->sink than [rbx + 0x96], especially since - * the latter would have to be changed whenever the structure is updated). */ -static void emit_static_asm(jitcompiler *jc) { - /*| // Trampolines for entering/exiting the JIT. These are a bit tricky to */ - /*| // support full resuming; when we suspend we copy the JIT's portion of */ - /*| // the call stack into the upb_pbdecoder and restore it when we resume. */ - asmlabel(jc, "enterjit"); - /*|->enterjit: */ - /*|1: */ - /*| push rbp */ - /*| push r15 */ - /*| push r14 */ - /*| push r13 */ - /*| push r12 */ - /*| push rbx */ - /*| */ - /*| mov rbx, ARG2_64 // Preserve JIT method. */ - /*| */ - /*| mov DECODER, rdi */ - /*| callp upb_pbdecoder_resume // Same args as us; reuse regs. */ - /*| test eax, eax */ - /*| jns >1 */ - /*| mov DECODER->saved_rsp, rsp */ - /*| mov rax, rbx */ - /*| load_regs */ - /*| */ - /*| // Test whether we have a saved stack to resume. */ - /*| mov ARG3_64, DECODER->call_len */ - /*| test ARG3_64, ARG3_64 */ - /*| jnz >2 */ - /*| */ - /*| call rax */ - /*| */ - /*| mov rax, DECODER->size_param */ - /*| mov qword DECODER->call_len, 0 */ - /*|1: */ - /*| pop rbx */ - dasm_put(Dst, 2, (unsigned int)((uintptr_t)upb_pbdecoder_resume), (unsigned int)(((uintptr_t)upb_pbdecoder_resume)>>32), 0xfffffffffffffff0UL, Dt2(->saved_rsp), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->call_len), Dt2(->size_param), Dt2(->call_len)); -# 243 "upb/pb/compile_decoder_x64.dasc" - /*| pop r12 */ - /*| pop r13 */ - /*| pop r14 */ - /*| pop r15 */ - /*| pop rbp */ - /*| ret */ - /*| */ - /*|2: */ - /*| // Resume decoder. */ - /*| mov ARG2_64, DECODER->callstack */ - /*| sub rsp, ARG3_64 */ - /*| mov ARG1_64, rsp */ - /*| callp memcpy // Restore stack. */ - /*| ret // Return to resumed function (not ->enterjit caller). */ - /*| */ - /*| // Other code can call this to suspend the JIT. */ - /*| // To the calling code, it will appear that the function returns when */ - /*| // the JIT resumes, and more buffer space will be available. */ - /*| // Args: eax=the value that decode() should return. */ - dasm_put(Dst, 115, Dt2(->callstack), (unsigned int)((uintptr_t)memcpy), (unsigned int)(((uintptr_t)memcpy)>>32), 0xfffffffffffffff0UL); -# 262 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "exitjit"); - /*|->exitjit: */ - /*| // Save the stack into DECODER->callstack. */ - /*| mov ARG1_64, DECODER->callstack */ - /*| mov ARG2_64, rsp */ - /*| mov ARG3_64, DECODER->saved_rsp */ - /*| sub ARG3_64, rsp */ - /*| mov DECODER->call_len, ARG3_64 // Preserve len for next resume. */ - /*| mov ebx, eax // Preserve return value across memcpy. */ - /*| callp memcpy // Copy stack into decoder. */ - /*| mov eax, ebx // This will be our return value. */ - /*| */ - /*| // Must NOT do this before the memcpy(), otherwise memcpy() will */ - /*| // clobber the stack we are trying to save! */ - /*| mov rsp, DECODER->saved_rsp */ - /*| pop rbx */ - /*| pop r12 */ - /*| pop r13 */ - /*| pop r14 */ - /*| pop r15 */ - /*| pop rbp */ - /*| ret */ - /*| */ - /*| // Like suspend() in the C decoder, except that the function appears */ - /*| // (from the caller's perspective) not to return until the decoder is */ - /*| // resumed. */ - dasm_put(Dst, 161, Dt2(->callstack), Dt2(->saved_rsp), Dt2(->call_len), (unsigned int)((uintptr_t)memcpy), (unsigned int)(((uintptr_t)memcpy)>>32), 0xfffffffffffffff0UL, Dt2(->saved_rsp)); -# 288 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "suspend"); - /*|->suspend: */ - /*| cmp DECODER->ptr, PTR */ - /*| je >1 */ - /*| mov DECODER->checkpoint, PTR */ - /*|1: */ - /*| commit_regs */ - /*| mov rdi, DECODER */ - /*| callp upb_pbdecoder_suspend */ - /*| jmp ->exitjit */ - /*| */ - dasm_put(Dst, 222, Dt2(->ptr), Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_suspend), (unsigned int)(((uintptr_t)upb_pbdecoder_suspend)>>32), 0xfffffffffffffff0UL); -# 299 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "pushlendelim"); - /*|->pushlendelim: */ - /*|1: */ - /*| mov FRAME->sink.closure, CLOSURE */ - /*| mov DECODER->checkpoint, PTR */ - /*| dv32 */ - dasm_put(Dst, 300, Dt1(->sink.closure), Dt2(->checkpoint)); - if (1 == 1) { - dasm_put(Dst, 313); - } else { - dasm_put(Dst, 321); - } -# 305 "upb/pb/compile_decoder_x64.dasc" - /*| mov rcx, DELIMEND */ - /*| sub rcx, PTR */ - /*| sub rcx, rdx */ - /*| jb >4 // Len is greater than enclosing message. */ - /*| mov FRAME->end_ofs, rcx */ - /*| cmp FRAME, DECODER->limit */ - /*| je >3 // Stack overflow */ - /*| add FRAME, sizeof(upb_pbdecoder_frame) */ - /*| mov DELIMEND, PTR */ - /*| add DELIMEND, rdx */ - /*| mov dword FRAME->groupnum, 0 */ - /*| test rcx, rcx */ - /*| jz >2 */ - /*| mov DATAEND, DECODER->end */ - /*| cmp PTR, DELIMEND */ - /*| ja >2 */ - /*| cmp DELIMEND, DATAEND */ - /*| ja >2 */ - /*| mov DATAEND, DELIMEND // If DELIMEND >= PTR && DELIMEND < DATAEND */ - dasm_put(Dst, 337, Dt1(->end_ofs), Dt2(->limit), sizeof(upb_pbdecoder_frame), Dt1(->groupnum), Dt2(->end)); -# 324 "upb/pb/compile_decoder_x64.dasc" - /*|2: */ - /*| ret */ - /*|3: */ - /*| // Stack overflow error. */ - /*| mov PTR, DECODER->checkpoint // Rollback to before the delim len. */ - /*| // Prepare seterr args. */ - /*| mov ARG1_64, DECODER */ - /*| ld64 kPbDecoderStackOverflow */ - dasm_put(Dst, 428, Dt2(->checkpoint)); - { - uintptr_t v = (uintptr_t)kPbDecoderStackOverflow; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 332 "upb/pb/compile_decoder_x64.dasc" - /*| callp upb_pbdecoder_seterr */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|4: */ - /*| // Overextended len. */ - /*| mov PTR, DECODER->checkpoint // Rollback to before the delim len. */ - /*| // Prepare seterr args. */ - /*| mov ARG1_64, DECODER */ - /*| ld64 kPbDecoderSubmessageTooLong */ - dasm_put(Dst, 458, (unsigned int)((uintptr_t)upb_pbdecoder_seterr), (unsigned int)(((uintptr_t)upb_pbdecoder_seterr)>>32), 0xfffffffffffffff0UL, Dt2(->checkpoint)); - { - uintptr_t v = (uintptr_t)kPbDecoderSubmessageTooLong; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 341 "upb/pb/compile_decoder_x64.dasc" - /*| callp upb_pbdecoder_seterr */ - /*| call ->suspend */ - /*| jmp <1 */ - /*| */ - /*| // For getting a value that spans a buffer seam. Falls back to C. */ - /*|.macro getvalue_slow, func, bytes */ - /*| sub rsp, 8 // Need stack space for func to write value to. */ - /*|1: */ - /*| mov qword [rsp], 0 // For parsing routines that only parse 32 bits. */ - /*| mov ARG1_64, DECODER */ - /*| mov ARG2_64, rsp */ - /*| mov DECODER->checkpoint, PTR */ - /*| commit_regs */ - /*| callp func */ - /*| load_regs */ - /*| test eax, eax */ - /*| jns >2 */ - /*| // Success; return parsed data (in rdx AND xmm0). */ - /*| mov rdx, [rsp] */ - /*| movsd xmm0, qword [rsp] */ - /*| add rsp, 8 */ - /*| sub PTR, bytes // Bias our buffer pointer to rejoin the fast-path. */ - /*| mov DECODER->ptr, PTR */ - /*| ret */ - /*|2: */ - /*| call ->exitjit // Return eax from decode function. */ - /*| jmp <1 */ - /*|.endmacro */ - /*| */ - dasm_put(Dst, 497, (unsigned int)((uintptr_t)upb_pbdecoder_seterr), (unsigned int)(((uintptr_t)upb_pbdecoder_seterr)>>32), 0xfffffffffffffff0UL); -# 370 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "parse_unknown"); - /*| // Args: edx=fieldnum, cl=wire type */ - /*|->parse_unknown: */ - /*| // OPT: handle directly instead of kicking to C. */ - /*| // Check for ENDGROUP. */ - /*| mov ARG1_64, DECODER */ - /*| mov ARG2_32, edx */ - /*| movzx ARG3_32, cl */ - /*| commit_regs */ - /*| callp upb_pbdecoder_skipunknown */ - /*| load_regs */ - /*| cmp eax, DECODE_ENDGROUP */ - /*| jne >1 */ - dasm_put(Dst, 526, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_skipunknown), (unsigned int)(((uintptr_t)upb_pbdecoder_skipunknown)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf), DECODE_ENDGROUP); -# 383 "upb/pb/compile_decoder_x64.dasc" - /*| ret // Return eax=DECODE_ENDGROUP, not zero */ - /*|1: */ - /*| cmp eax, DECODE_OK */ - /*| je >1 */ - /*| call ->exitjit // Return eax from decode function. */ - /*|1: */ - /*| xor eax, eax */ - /*| ret */ - /*| */ - /*| // Fallback functions for parsing single values. These are used when the */ - /*| // buffer doesn't contain enough remaining data for the fast path. Each */ - /*| // primitive type (v32, v64, f32, f64) has two functions: decode & skip. */ - /*| // Decode functions return their value in rsi/esi. */ - /*| // */ - /*| // These functions leave PTR = value_end - fast_path_bytes, so that we can */ - /*| // re-join the fast path which will add fast_path_bytes after the callback */ - /*| // completes. We also set DECODER->ptr to this value which is a signal to */ - /*| // ->suspend that DECODER->checkpoint is up to date. */ - dasm_put(Dst, 623, DECODE_OK); -# 401 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "skip_decode_f32_fallback"); - /*|->skipf32_fallback: */ - /*|->decodef32_fallback: */ - /*| getvalue_slow upb_pbdecoder_decode_f32, 4 */ - dasm_put(Dst, 647, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_f32), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_f32)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs)); -# 405 "upb/pb/compile_decoder_x64.dasc" - /*| */ - dasm_put(Dst, 751, Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->ptr)); -# 406 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "skip_decode_f64_fallback"); - /*|->skipf64_fallback: */ - /*|->decodef64_fallback: */ - /*| getvalue_slow upb_pbdecoder_decode_f64, 8 */ - dasm_put(Dst, 799, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_f64), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_f64)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs)); -# 410 "upb/pb/compile_decoder_x64.dasc" - /*| */ - /*| // Called for varint >= 1 byte. */ - dasm_put(Dst, 903, Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->ptr)); -# 412 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "skip_decode_v32_fallback"); - /*|->skipv32_fallback: */ - /*|->skipv64_fallback: */ - /*| chkeob 16, >1 */ - dasm_put(Dst, 951); - if (16 == 1) { - dasm_put(Dst, 956); - } else { - dasm_put(Dst, 964); - } -# 416 "upb/pb/compile_decoder_x64.dasc" - /*| // With at least 16 bytes left, we can do a branch-less SSE version. */ - /*| movdqu xmm0, [PTR] */ - /*| pmovmskb eax, xmm0 // bits 0-15 are continuation bits, 16-31 are 0. */ - /*| not eax */ - /*| bsf eax, eax */ - /*| cmp al, 10 */ - /*| jae ->decode_varint_slow // Error (>10 byte varint). */ - /*| add PTR, rax // bsf result is 0-based, so PTR=end-1, as desired. */ - /*| ret */ - /*| */ - /*|1: */ - /*| // With fewer than 16 bytes, we have to read byte by byte. */ - /*| lea rcx, [PTR + 10] */ - /*| mov rax, PTR // Preserve PTR in case of fallback to slow path. */ - /*| cmp rcx, DATAEND */ - /*| cmova rcx, DATAEND // rcx = MIN(DATAEND, PTR + 10) */ - /*|2: */ - /*| cmp rax, rcx */ - /*| je ->decode_varint_slow */ - /*| test byte [rax], 0x80 */ - /*| jz >3 */ - /*| add rax, 1 */ - /*| jmp <2 */ - /*|3: */ - /*| mov PTR, rax // PTR = varint_end - 1, as desired */ - /*| ret */ - /*| */ - /*| // Returns tag in edx */ - dasm_put(Dst, 980, 10); -# 444 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "decode_unknown_tag_fallback"); - /*|->decode_unknown_tag_fallback: */ - /*| sub rsp, 16 */ - /*|1: */ - /*| cmp PTR, DELIMEND */ - /*| jne >2 */ - /*| add rsp, 16 */ - /*| xor eax, eax */ - /*| ret */ - /*|2: */ - /*| // OPT: Have a medium-fast path before falling back to _slow. */ - /*| mov ARG1_64, DECODER */ - /*| mov ARG2_64, rsp */ - /*| commit_regs */ - /*| callp upb_pbdecoder_decode_varint_slow */ - /*| load_regs */ - dasm_put(Dst, 1053, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_varint_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_varint_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure)); -# 460 "upb/pb/compile_decoder_x64.dasc" - /*| cmp eax, 0 */ - /*| jge >3 */ - /*| mov edx, [rsp] // Success; return parsed data. */ - /*| add rsp, 16 */ - /*| ret */ - /*|3: */ - /*| call ->exitjit // Return eax from decode function. */ - /*| jmp <1 */ - /*| */ - /*| // Called for varint >= 1 byte. */ - dasm_put(Dst, 1156, Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf)); -# 470 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "decode_v32_v64_fallback"); - /*|->decodev32_fallback: */ - /*|->decodev64_fallback: */ - /*| chkeob 10, ->decode_varint_slow */ - dasm_put(Dst, 1194); - if (10 == 1) { - dasm_put(Dst, 1199); - } else { - dasm_put(Dst, 1207); - } -# 474 "upb/pb/compile_decoder_x64.dasc" - /*| // OPT: do something faster than just calling the C version. */ - /*| mov rdi, PTR */ - /*| callp upb_vdecode_fast */ - /*| test rax, rax */ - /*| je ->decode_varint_slow // Unterminated varint. */ - /*| mov PTR, rax */ - /*| sub PTR, 1 */ - /*| mov DECODER->ptr, PTR */ - /*| ret */ - /*| */ - dasm_put(Dst, 1223, (unsigned int)((uintptr_t)upb_vdecode_fast), (unsigned int)(((uintptr_t)upb_vdecode_fast)>>32), 0xfffffffffffffff0UL, Dt2(->ptr)); -# 484 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "decode_varint_slow"); - /*|->decode_varint_slow: */ - /*| // Slow path: end of buffer or error (varint length >= 10). */ - /*| getvalue_slow upb_pbdecoder_decode_varint_slow, 1 */ - dasm_put(Dst, 1268, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_varint_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_varint_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs)); -# 488 "upb/pb/compile_decoder_x64.dasc" - /*| */ - /*| // Args: rsi=expected tag, return=rax (DECODE_{OK,MISMATCH}) */ - dasm_put(Dst, 1374, Dt2(->buf), Dt2(->ptr)); -# 490 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "checktag_fallback"); - /*|->checktag_fallback: */ - /*| sub rsp, 8 */ - /*| mov [rsp], rsi // Preserve expected tag. */ - /*|1: */ - /*| mov ARG1_64, DECODER */ - /*| commit_regs */ - /*| mov DECODER->checkpoint, PTR */ - /*| callp upb_pbdecoder_checktag_slow */ - /*| load_regs */ - dasm_put(Dst, 1418, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), Dt2(->checkpoint), (unsigned int)((uintptr_t)upb_pbdecoder_checktag_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_checktag_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs)); -# 500 "upb/pb/compile_decoder_x64.dasc" - /*| cmp eax, 0 */ - /*| jge >2 */ - /*| add rsp, 8 */ - /*| ret */ - /*|2: */ - /*| call ->exitjit */ - /*| mov rsi, [rsp] */ - /*| cmp PTR, DELIMEND */ - /*| jne <1 */ - /*| mov eax, DECODE_EOF */ - /*| add rsp, 8 */ - /*| ret */ - /*| */ - /*| // Args: rsi=upb_inttable, rdx=key, return=rax (-1 if not found). */ - /*| // Preserves: rcx, rdx */ - /*| // OPT: Could write this in assembly if it's a hotspot. */ - dasm_put(Dst, 1517, Dt2(->buf), DECODE_EOF); -# 516 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "hashlookup"); - /*|->hashlookup: */ - /*| push rcx */ - /*| push rdx */ - /*| sub rsp, 16 */ - /*| mov rdi, rsi */ - /*| mov rsi, rdx */ - /*| mov rdx, rsp */ - /*| callp upb_inttable_lookup */ - /*| add rsp, 16 */ - /*| pop rdx */ - /*| pop rcx */ - /*| test al, al */ - /*| jz >2 // Unknown field. */ - /*| mov rax, [rsp-32] // Value from table. */ - /*| ret */ - /*|2: */ - /*| xor rax, rax */ - /*| not rax */ - /*| ret */ - dasm_put(Dst, 1559, (unsigned int)((uintptr_t)upb_inttable_lookup), (unsigned int)(((uintptr_t)upb_inttable_lookup)>>32), 0xfffffffffffffff0UL); -# 536 "upb/pb/compile_decoder_x64.dasc" -} - -static void jitprimitive(jitcompiler *jc, opcode op, - const upb_handlers *h, upb_selector_t sel) { - typedef enum { V32, V64, F32, F64, X } valtype_t; - static valtype_t types[] = { - X, F64, F32, V64, V64, V32, F64, F32, V64, X, X, X, X, V32, V32, F32, F64, - V32, V64 }; - static char fastpath_bytes[] = { 1, 1, 4, 8 }; - const valtype_t vtype = types[op]; - const int fastbytes = fastpath_bytes[vtype]; - upb_func *handler = gethandler(h, sel); - upb_fieldtype_t ftype; - size_t offset; - int32_t hasbit; - - if (handler) { - /*|1: */ - /*| chkneob fastbytes, >3 */ - dasm_put(Dst, 112); - if (fastbytes == 1) { - dasm_put(Dst, 1628); - } else { - dasm_put(Dst, 1636, fastbytes); - } -# 555 "upb/pb/compile_decoder_x64.dasc" - /*|2: */ - dasm_put(Dst, 1652); -# 556 "upb/pb/compile_decoder_x64.dasc" - switch (vtype) { - case V32: - /*| call ->decodev32_fallback */ - dasm_put(Dst, 1655); -# 559 "upb/pb/compile_decoder_x64.dasc" - break; - case V64: - /*| call ->decodev64_fallback */ - dasm_put(Dst, 1659); -# 562 "upb/pb/compile_decoder_x64.dasc" - break; - case F32: - /*| call ->decodef32_fallback */ - dasm_put(Dst, 1663); -# 565 "upb/pb/compile_decoder_x64.dasc" - break; - case F64: - /*| call ->decodef64_fallback */ - dasm_put(Dst, 1667); -# 568 "upb/pb/compile_decoder_x64.dasc" - break; - case X: break; - } - /*| jmp >4 */ - dasm_put(Dst, 1671); -# 572 "upb/pb/compile_decoder_x64.dasc" - - /* Fast path decode; for when check_bytes bytes are available. */ - /*|3: */ - dasm_put(Dst, 1676); -# 575 "upb/pb/compile_decoder_x64.dasc" - switch (op) { - case OP_PARSE_SFIXED32: - case OP_PARSE_FIXED32: - /*| mov edx, dword [PTR] */ - dasm_put(Dst, 1679); -# 579 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_SFIXED64: - case OP_PARSE_FIXED64: - /*| mov rdx, qword [PTR] */ - dasm_put(Dst, 1682); -# 583 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_FLOAT: - /*| movss xmm0, dword [PTR] */ - dasm_put(Dst, 1686); -# 586 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_DOUBLE: - /*| movsd xmm0, qword [PTR] */ - dasm_put(Dst, 1692); -# 589 "upb/pb/compile_decoder_x64.dasc" - break; - default: - /* Inline one byte of varint decoding. */ - /*| movzx edx, byte [PTR] */ - /*| test dl, dl */ - /*| js <2 // Fallback to slow path for >1 byte varint. */ - dasm_put(Dst, 1698); -# 595 "upb/pb/compile_decoder_x64.dasc" - break; - } - - /* Second-stage decode; used for both fast and slow paths */ - /* (only needed for a few types). */ - /*|4: */ - dasm_put(Dst, 1708); -# 601 "upb/pb/compile_decoder_x64.dasc" - switch (op) { - case OP_PARSE_SINT32: - /* 32-bit zig-zag decode. */ - /*| mov eax, edx */ - /*| shr edx, 1 */ - /*| and eax, 1 */ - /*| neg eax */ - /*| xor edx, eax */ - dasm_put(Dst, 1711); -# 609 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_SINT64: - /* 64-bit zig-zag decode. */ - /*| mov rax, rdx */ - /*| shr rdx, 1 */ - /*| and rax, 1 */ - /*| neg rax */ - /*| xor rdx, rax */ - dasm_put(Dst, 1725); -# 617 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_BOOL: - /*| test rdx, rdx */ - /*| setne dl */ - dasm_put(Dst, 1744); -# 621 "upb/pb/compile_decoder_x64.dasc" - break; - default: break; - } - - /* Call callback (or specialize if we can). */ - if (upb_msg_getscalarhandlerdata(h, sel, &ftype, &offset, &hasbit)) { - switch (ftype) { - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - /*| mov [CLOSURE + offset], rdx */ - dasm_put(Dst, 1751, offset); -# 631 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_ENUM: - /*| mov [CLOSURE + offset], edx */ - dasm_put(Dst, 1756, offset); -# 636 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_DOUBLE: - /*| movsd qword [CLOSURE + offset], XMMARG1 */ - dasm_put(Dst, 1761, offset); -# 639 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_FLOAT: - /*| movss dword [CLOSURE + offset], XMMARG1 */ - dasm_put(Dst, 1769, offset); -# 642 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_BOOL: - /*| mov [CLOSURE + offset], dl */ - dasm_put(Dst, 1777, offset); -# 645 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - case UPB_TYPE_MESSAGE: - UPB_ASSERT(false); break; - } - /*| sethas CLOSURE, hasbit */ - if (hasbit >= 0) { - dasm_put(Dst, 1782, ((uint32_t)hasbit / 8), (1 << ((uint32_t)hasbit % 8))); - } -# 652 "upb/pb/compile_decoder_x64.dasc" - } else if (handler) { - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, sel */ - dasm_put(Dst, 1788); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, sel); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 655 "upb/pb/compile_decoder_x64.dasc" - /*| callp handler */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)handler), (unsigned int)(((uintptr_t)handler)>>32), 0xfffffffffffffff0UL); -# 656 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, sel)) { - /*| test al, al */ - /*| jnz >5 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|5: */ - dasm_put(Dst, 1815); -# 662 "upb/pb/compile_decoder_x64.dasc" - } - } - - /* We do this last so that the checkpoint is not advanced past the user's - * data until the callback has returned success. */ - /*| add PTR, fastbytes */ - dasm_put(Dst, 1831, fastbytes); -# 668 "upb/pb/compile_decoder_x64.dasc" - } else { - /* No handler registered for this value, just skip it. */ - /*| chkneob fastbytes, >3 */ - if (fastbytes == 1) { - dasm_put(Dst, 1628); - } else { - dasm_put(Dst, 1636, fastbytes); - } -# 671 "upb/pb/compile_decoder_x64.dasc" - /*|2: */ - dasm_put(Dst, 1652); -# 672 "upb/pb/compile_decoder_x64.dasc" - switch (vtype) { - case V32: - /*| call ->skipv32_fallback */ - dasm_put(Dst, 1836); -# 675 "upb/pb/compile_decoder_x64.dasc" - break; - case V64: - /*| call ->skipv64_fallback */ - dasm_put(Dst, 1840); -# 678 "upb/pb/compile_decoder_x64.dasc" - break; - case F32: - /*| call ->skipf32_fallback */ - dasm_put(Dst, 1844); -# 681 "upb/pb/compile_decoder_x64.dasc" - break; - case F64: - /*| call ->skipf64_fallback */ - dasm_put(Dst, 1848); -# 684 "upb/pb/compile_decoder_x64.dasc" - break; - case X: break; - } - - /* Fast-path skip. */ - /*|3: */ - dasm_put(Dst, 1676); -# 690 "upb/pb/compile_decoder_x64.dasc" - if (vtype == V32 || vtype == V64) { - /*| test byte [PTR], 0x80 */ - /*| jnz <2 */ - dasm_put(Dst, 1852); -# 693 "upb/pb/compile_decoder_x64.dasc" - } - /*| add PTR, fastbytes */ - dasm_put(Dst, 1831, fastbytes); -# 695 "upb/pb/compile_decoder_x64.dasc" - } -} - -static void jitdispatch(jitcompiler *jc, - const upb_pbdecodermethod *method) { - /* Lots of room for tweaking/optimization here. */ - - const upb_inttable *dispatch = &method->dispatch; - bool has_hash_entries = (dispatch->t.count > 0); - - /* Whether any of the fields for this message can have two wire types which - * are both valid (packed & non-packed). - * - * OPT: populate this more precisely; not all messages with hash entries have - * this characteristic. */ - bool has_multi_wiretype = has_hash_entries; - - /*|=>define_jmptarget(jc, &method->dispatch): */ - /*|1: */ - dasm_put(Dst, 1861, define_jmptarget(jc, &method->dispatch)); -# 714 "upb/pb/compile_decoder_x64.dasc" - /* Decode the field tag. */ - /*| mov aword DECODER->checkpoint, PTR */ - /*| chkeob 2, >6 */ - dasm_put(Dst, 308, Dt2(->checkpoint)); - if (2 == 1) { - dasm_put(Dst, 1865); - } else { - dasm_put(Dst, 1873); - } -# 717 "upb/pb/compile_decoder_x64.dasc" - /*| movzx edx, byte [PTR] */ - /*| test dl, dl */ - /*| jns >7 // Jump if first byte has no continuation bit. */ - /*| movzx ecx, byte [PTR + 1] */ - /*| test cl, cl */ - /*| js >6 // Jump if second byte has continuation bit. */ - /*| // Confirmed two-byte varint. */ - /*| shl ecx, 7 */ - /*| and edx, 0x7f */ - /*| or edx, ecx */ - /*| add PTR, 2 */ - /*| jmp >8 */ - /*|6: */ - /*| call ->decode_unknown_tag_fallback */ - /*| test eax, eax // Hit DELIMEND? */ - /*| jnz >8 */ - /*| ret */ - /*|7: */ - /*| add PTR, 1 */ - /*|8: */ - /*| mov ecx, edx */ - /*| shr edx, 3 */ - /*| and cl, 7 */ - dasm_put(Dst, 1889, 1); -# 740 "upb/pb/compile_decoder_x64.dasc" - - /* See comment attached to upb_pbdecodermethod.dispatch for layout of the - * dispatch table. */ - /*|2: */ - /*| cmp edx, dispatch->array_size */ - dasm_put(Dst, 1954, dispatch->array_size); -# 745 "upb/pb/compile_decoder_x64.dasc" - if (has_hash_entries) { - /*| jae >7 */ - dasm_put(Dst, 1961); -# 747 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| jae >5 */ - dasm_put(Dst, 1966); -# 749 "upb/pb/compile_decoder_x64.dasc" - } - /*| // OPT: Compact the lookup arr into 32-bit entries. */ - if ((uintptr_t)dispatch->array > 0x7fffffff) { - /*| mov64 rax, (uintptr_t)dispatch->array */ - /*| mov rax, qword [rax + rdx * 8] */ - dasm_put(Dst, 1971, (unsigned int)((uintptr_t)dispatch->array), (unsigned int)(((uintptr_t)dispatch->array)>>32)); -# 754 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| mov rax, qword [rdx * 8 + dispatch->array] */ - dasm_put(Dst, 1980, dispatch->array); -# 756 "upb/pb/compile_decoder_x64.dasc" - } - /*|3: */ - /*| // We take advantage of the fact that non-present entries are stored */ - /*| // as -1, which will result in wire types that will never match. */ - /*| cmp al, cl */ - dasm_put(Dst, 1986); -# 761 "upb/pb/compile_decoder_x64.dasc" - if (has_multi_wiretype) { - /*| jne >6 */ - dasm_put(Dst, 1991); -# 763 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| jne >5 */ - dasm_put(Dst, 1996); -# 765 "upb/pb/compile_decoder_x64.dasc" - } - /*| shr rax, 16 */ - /*| */ - /*| // Load the machine code address from the table entry. */ - /*| // The table entry is relative to the dispatch->array jmptarget */ - /*| // (patchdispatch() took care of this) which is the same as */ - /*| // local label "4". The "lea" is really just trying to do */ - /*| // lea rax, [>4 + rax] */ - /*| // */ - /*| // But we can't write that directly for some reason, so we use */ - /*| // rdx as a temporary. */ - /*| lea rdx, [>4] */ - /*|=>define_jmptarget(jc, dispatch->array): */ - /*|4: */ - /*| add rax, rdx */ - /*| ret */ - /*| */ - /*|5: */ - /*| // Field isn't in our table. */ - /*| */ - /*| // For pushing unknown fields to the unknown field handler. */ - /*| mov64 rax, (uintptr_t)method->dest_handlers_ */ - /*| mov FRAME->sink.handlers, rax */ - /*| */ - /*| call ->parse_unknown */ - /*| test eax, eax // ENDGROUP? */ - /*| jz <1 */ - /*| lea rax, [>9] // ENDGROUP; Load address of OP_ENDMSG. */ - /*| ret */ - dasm_put(Dst, 2001, define_jmptarget(jc, dispatch->array), (unsigned int)((uintptr_t)method->dest_handlers_), (unsigned int)(((uintptr_t)method->dest_handlers_)>>32), Dt1(->sink.handlers)); -# 794 "upb/pb/compile_decoder_x64.dasc" - - if (has_multi_wiretype) { - /*|6: */ - /*| // Primary wire type didn't match, check secondary wire type. */ - /*| cmp ah, cl */ - /*| jne <5 */ - /*| // Secondary wire type is a match, look up fn + UPB_MAX_FIELDNUMBER. */ - /*| add rdx, UPB_MAX_FIELDNUMBER */ - /*| // This key will never be in the array part, so do a hash lookup. */ - dasm_put(Dst, 2043, UPB_MAX_FIELDNUMBER); -# 803 "upb/pb/compile_decoder_x64.dasc" - UPB_ASSERT(has_hash_entries); - /*| ld64 dispatch */ - { - uintptr_t v = (uintptr_t)dispatch; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 805 "upb/pb/compile_decoder_x64.dasc" - /*| jmp ->hashlookup // Tail call. */ - dasm_put(Dst, 2056); -# 806 "upb/pb/compile_decoder_x64.dasc" - } - - if (has_hash_entries) { - /*|7: */ - /*| // Hash table lookup. */ - /*| ld64 dispatch */ - dasm_put(Dst, 2061); - { - uintptr_t v = (uintptr_t)dispatch; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 812 "upb/pb/compile_decoder_x64.dasc" - /*| call ->hashlookup */ - /*| jmp <3 */ - dasm_put(Dst, 2064); -# 814 "upb/pb/compile_decoder_x64.dasc" - } -} - -static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, - const upb_pbdecodermethod *method) { - /* Internally we parse unknown fields; if this runs us into DELIMEND we jump - * to the corresponding DELIMEND target (either msg end or repeated field - * end), which we find from the OP_CHECKDELIM which must have necessarily - * preceded us. */ - uint32_t last_instruction = *(jc->pc - 2); - int last_arg = (int32_t)last_instruction >> 8; - uint32_t *delimend = (jc->pc - 1) + last_arg; - const size_t ptr_words = sizeof(void*) / sizeof(uint32_t); - - UPB_ASSERT((last_instruction & 0xff) == OP_CHECKDELIM); - - if (getop(*(jc->pc - 1)) == OP_TAGN) { - jc->pc += ptr_words; - } - - /*| chkneob n, >1 */ - if (n == 1) { - dasm_put(Dst, 2072); - } else { - dasm_put(Dst, 2080, n); - } -# 835 "upb/pb/compile_decoder_x64.dasc" - - /*| // OPT: this is way too much fallback code to put here. */ - /*| // Reduce and/or move to a separate section to make better icache usage. */ - /*| ld64 tag */ - { - uintptr_t v = (uintptr_t)tag; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 839 "upb/pb/compile_decoder_x64.dasc" - /*| call ->checktag_fallback */ - /*| cmp eax, DECODE_MISMATCH */ - /*| je >3 */ - /*| cmp eax, DECODE_EOF */ - /*| je =>jmptarget(jc, delimend) */ - /*| jmp >5 */ - dasm_put(Dst, 2096, DECODE_MISMATCH, DECODE_EOF, jmptarget(jc, delimend)); -# 845 "upb/pb/compile_decoder_x64.dasc" - - /*|1: */ - dasm_put(Dst, 112); -# 847 "upb/pb/compile_decoder_x64.dasc" - switch (n) { - case 1: - /*| cmp byte [PTR], tag */ - dasm_put(Dst, 2119, tag); -# 850 "upb/pb/compile_decoder_x64.dasc" - break; - case 2: - /*| cmp word [PTR], tag */ - dasm_put(Dst, 2123, tag); -# 853 "upb/pb/compile_decoder_x64.dasc" - break; - case 3: - /*| // OPT: Slightly more efficient code, but depends on an extra byte. */ - /*| // mov eax, dword [PTR] */ - /*| // shl eax, 8 */ - /*| // cmp eax, tag << 8 */ - /*| cmp word [PTR], (tag & 0xffff) */ - /*| jne >2 */ - /*| cmp byte [PTR + 2], (tag >> 16) */ - /*|2: */ - dasm_put(Dst, 2128, (tag & 0xffff), 2, (tag >> 16)); -# 863 "upb/pb/compile_decoder_x64.dasc" - break; - case 4: - /*| cmp dword [PTR], tag */ - dasm_put(Dst, 2143, tag); -# 866 "upb/pb/compile_decoder_x64.dasc" - break; - case 5: - /*| cmp dword [PTR], (tag & 0xffffffff) */ - /*| jne >3 */ - /*| cmp byte [PTR + 4], (tag >> 32) */ - dasm_put(Dst, 2147, (tag & 0xffffffff), 4, (tag >> 32)); -# 871 "upb/pb/compile_decoder_x64.dasc" - } - /*| je >4 */ - /*|3: */ - dasm_put(Dst, 2159); -# 874 "upb/pb/compile_decoder_x64.dasc" - if (ofs == 0) { - /*| call =>jmptarget(jc, &method->dispatch) */ - /*| test rax, rax */ - /*| jz =>jmptarget(jc, delimend) */ - /*| jmp rax */ - dasm_put(Dst, 2166, jmptarget(jc, &method->dispatch), jmptarget(jc, delimend)); -# 879 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| jmp =>jmptarget(jc, jc->pc + ofs) */ - dasm_put(Dst, 2178, jmptarget(jc, jc->pc + ofs)); -# 881 "upb/pb/compile_decoder_x64.dasc" - } - /*|4: */ - /*| add PTR, n */ - /*|5: */ - dasm_put(Dst, 2182, n); -# 885 "upb/pb/compile_decoder_x64.dasc" -} - -/* Compile the bytecode to x64. */ -static void jitbytecode(jitcompiler *jc) { - upb_pbdecodermethod *method = NULL; - const upb_handlers *h = NULL; - for (jc->pc = jc->group->bytecode; jc->pc < jc->group->bytecode_end; ) { - int32_t instr = *jc->pc; - opcode op = instr & 0xff; - uint32_t arg = instr >> 8; - int32_t longofs = arg; - - if (op != OP_SETDISPATCH) { - /* Skipped for SETDISPATCH because it defines its own asmlabel for the - * dispatch code it emits. */ - asmlabel(jc, "0x%lx.%s", pcofs(jc), upb_pbdecoder_getopname(op)); - - /* Skipped for SETDISPATCH because it should point at the function - * prologue, not the dispatch function that is emitted first. - * TODO: optimize this to only define pclabels that are actually used. */ - /*|=>define_jmptarget(jc, jc->pc): */ - dasm_put(Dst, 0, define_jmptarget(jc, jc->pc)); -# 906 "upb/pb/compile_decoder_x64.dasc" - } - - jc->pc++; - - switch (op) { - case OP_STARTMSG: { - upb_func *startmsg = gethandler(h, UPB_STARTMSG_SELECTOR); - if (startmsg) { - /* bool startmsg(void *closure, const void *hd) */ - /*|1: */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, UPB_STARTMSG_SELECTOR */ - dasm_put(Dst, 2191); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, UPB_STARTMSG_SELECTOR); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 918 "upb/pb/compile_decoder_x64.dasc" - /*| callp startmsg */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)startmsg), (unsigned int)(((uintptr_t)startmsg)>>32), 0xfffffffffffffff0UL); -# 919 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, UPB_STARTMSG_SELECTOR)) { - /*| test al, al */ - /*| jnz >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2198); -# 925 "upb/pb/compile_decoder_x64.dasc" - } - } else { - /*| nop */ - dasm_put(Dst, 2214); -# 928 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_ENDMSG: { - upb_func *endmsg = gethandler(h, UPB_ENDMSG_SELECTOR); - /*|9: */ - dasm_put(Dst, 2216); -# 934 "upb/pb/compile_decoder_x64.dasc" - if (endmsg) { - /* bool endmsg(void *closure, const void *hd, upb_status *status) */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, UPB_ENDMSG_SELECTOR */ - dasm_put(Dst, 1788); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, UPB_ENDMSG_SELECTOR); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 938 "upb/pb/compile_decoder_x64.dasc" - /*| mov ARG3_64, DECODER->status */ - /*| callp endmsg */ - dasm_put(Dst, 2219, Dt2(->status), (unsigned int)((uintptr_t)endmsg), (unsigned int)(((uintptr_t)endmsg)>>32), 0xfffffffffffffff0UL); -# 940 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_SETDISPATCH: { - uint32_t *op_pc = jc->pc - 1; - const char *msgname; - upb_inttable *dispatch; - - /* Load info for new method. */ - memcpy(&dispatch, jc->pc, sizeof(void*)); - jc->pc += sizeof(void*) / sizeof(uint32_t); - /* The OP_SETDISPATCH bytecode contains a pointer that is - * &method->dispatch; we want to go backwards and recover method. */ - method = - (void*)((char*)dispatch - offsetof(upb_pbdecodermethod, dispatch)); - /* May be NULL, in which case no handlers for this message will be found. - * OPT: we should do better by completely skipping the message in this - * case instead of parsing it field by field. We should also do the skip - * in the containing message's code. */ - h = method->dest_handlers_; - msgname = upb_msgdef_fullname(upb_handlers_msgdef(h)); - - /* Emit dispatch code for new method. */ - asmlabel(jc, "0x%lx.dispatch.%s", pcofs(jc), msgname); - jitdispatch(jc, method); - - /* Emit function prologue for new method. */ - asmlabel(jc, "0x%lx.parse.%s", pcofs(jc), msgname); - /*|=>define_jmptarget(jc, op_pc): */ - /*|=>define_jmptarget(jc, method): */ - /*| sub rsp, 8 */ - dasm_put(Dst, 2245, define_jmptarget(jc, op_pc), define_jmptarget(jc, method)); -# 971 "upb/pb/compile_decoder_x64.dasc" - - break; - } - case OP_PARSE_DOUBLE: - case OP_PARSE_FLOAT: - case OP_PARSE_INT64: - case OP_PARSE_UINT64: - case OP_PARSE_INT32: - case OP_PARSE_FIXED64: - case OP_PARSE_FIXED32: - case OP_PARSE_BOOL: - case OP_PARSE_UINT32: - case OP_PARSE_SFIXED32: - case OP_PARSE_SFIXED64: - case OP_PARSE_SINT32: - case OP_PARSE_SINT64: - jitprimitive(jc, op, h, arg); - break; - case OP_STARTSEQ: - case OP_STARTSUBMSG: - case OP_STARTSTR: { - upb_func *start = gethandler(h, arg); - if (start) { - /* void *startseq(void *closure, const void *hd) - * void *startsubmsg(void *closure, const void *hd) - * void *startstr(void *closure, const void *hd, size_t size_hint) */ - /*|1: */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, arg */ - dasm_put(Dst, 2191); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, arg); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 1000 "upb/pb/compile_decoder_x64.dasc" - if (op == OP_STARTSTR) { - /*| mov ARG3_64, DELIMEND */ - /*| sub ARG3_64, PTR */ - dasm_put(Dst, 2253); -# 1003 "upb/pb/compile_decoder_x64.dasc" - } - /*| callp start */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)start), (unsigned int)(((uintptr_t)start)>>32), 0xfffffffffffffff0UL); -# 1005 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, arg)) { - /*| test rax, rax */ - /*| jnz >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2261); -# 1011 "upb/pb/compile_decoder_x64.dasc" - } - /*| mov CLOSURE, rax */ - dasm_put(Dst, 2278); -# 1013 "upb/pb/compile_decoder_x64.dasc" - } else { - /* TODO: nop is only required because of asmlabel(). */ - /*| nop */ - dasm_put(Dst, 2214); -# 1016 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_ENDSEQ: - case OP_ENDSUBMSG: - case OP_ENDSTR: { - upb_func *end = gethandler(h, arg); - if (end) { - /* bool endseq(void *closure, const void *hd) - * bool endsubmsg(void *closure, const void *hd) - * bool endstr(void *closure, const void *hd) */ - /*|1: */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, arg */ - dasm_put(Dst, 2191); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, arg); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 1030 "upb/pb/compile_decoder_x64.dasc" - /*| callp end */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)end), (unsigned int)(((uintptr_t)end)>>32), 0xfffffffffffffff0UL); -# 1031 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, arg)) { - /*| test al, al */ - /*| jnz >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2198); -# 1037 "upb/pb/compile_decoder_x64.dasc" - } - } else { - /* TODO: nop is only required because of asmlabel(). */ - /*| nop */ - dasm_put(Dst, 2214); -# 1041 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_STRING: { - upb_func *str = gethandler(h, arg); - /*| cmp PTR, DELIMEND */ - /*| je >4 */ - /*|1: */ - /*| cmp PTR, DATAEND */ - /*| jne >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2282); -# 1054 "upb/pb/compile_decoder_x64.dasc" - if (str) { - /* size_t str(void *closure, const void *hd, const char *str, - * size_t n) */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, arg */ - dasm_put(Dst, 1788); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, arg); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 1059 "upb/pb/compile_decoder_x64.dasc" - /*| mov ARG3_64, PTR */ - /*| mov ARG4_64, DATAEND */ - /*| sub ARG4_64, PTR */ - /*| mov ARG5_64, qword DECODER->handle */ - /*| callp str */ - /*| add PTR, rax */ - dasm_put(Dst, 2309, Dt2(->handle), (unsigned int)((uintptr_t)str), (unsigned int)(((uintptr_t)str)>>32), 0xfffffffffffffff0UL); -# 1065 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, arg)) { - /*| cmp PTR, DATAEND */ - /*| je >3 */ - /*| call ->strret_fallback */ - /*|3: */ - dasm_put(Dst, 2347); -# 1070 "upb/pb/compile_decoder_x64.dasc" - } - } else { - /*| mov PTR, DATAEND */ - dasm_put(Dst, 2360); -# 1073 "upb/pb/compile_decoder_x64.dasc" - } - /*| cmp PTR, DELIMEND */ - /*| jne <1 */ - /*|4: */ - dasm_put(Dst, 2364); -# 1077 "upb/pb/compile_decoder_x64.dasc" - break; - } - case OP_PUSHTAGDELIM: - /*| mov FRAME->sink.closure, CLOSURE */ - /*| // This shouldn't need to be read, because tag-delimited fields */ - /*| // shouldn't have an OP_SETDELIM after them. But for the moment */ - /*| // non-packed repeated fields do OP_SETDELIM so they can share more */ - /*| // code with the packed code-path. If this is changed later, this */ - /*| // store can be removed. */ - /*| mov qword FRAME->end_ofs, 0 */ - /*| cmp FRAME, DECODER->limit */ - /*| je ->err */ - /*| add FRAME, sizeof(upb_pbdecoder_frame) */ - /*| mov dword FRAME->groupnum, arg */ - dasm_put(Dst, 2375, Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->limit), sizeof(upb_pbdecoder_frame), Dt1(->groupnum), arg); -# 1091 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PUSHLENDELIM: - /*| call ->pushlendelim */ - dasm_put(Dst, 2405); -# 1094 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_POP: - /*| sub FRAME, sizeof(upb_pbdecoder_frame) */ - /*| mov CLOSURE, FRAME->sink.closure */ - dasm_put(Dst, 2409, sizeof(upb_pbdecoder_frame), Dt1(->sink.closure)); -# 1098 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_SETDELIM: - /* OPT: experiment with testing vs old offset to optimize away. */ - /*| mov DATAEND, DECODER->end */ - /*| add DELIMEND, FRAME->end_ofs */ - /*| cmp DELIMEND, DECODER->buf */ - /*| jb >1 */ - /*| cmp DELIMEND, DATAEND */ - /*| ja >1 // OPT: try cmov. */ - /*| mov DATAEND, DELIMEND */ - /*|1: */ - dasm_put(Dst, 2419, Dt2(->end), Dt1(->end_ofs), Dt2(->buf)); -# 1109 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_SETBIGGROUPNUM: - /*| mov dword FRAME->groupnum, *jc->pc++ */ - dasm_put(Dst, 2399, Dt1(->groupnum), *jc->pc++); -# 1112 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_CHECKDELIM: - /*| cmp DELIMEND, PTR */ - /*| je =>jmptarget(jc, jc->pc + longofs) */ - dasm_put(Dst, 2449, jmptarget(jc, jc->pc + longofs)); -# 1116 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_CALL: - /*| call =>jmptarget(jc, jc->pc + longofs) */ - dasm_put(Dst, 2456, jmptarget(jc, jc->pc + longofs)); -# 1119 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_BRANCH: - /*| jmp =>jmptarget(jc, jc->pc + longofs); */ - dasm_put(Dst, 2178, jmptarget(jc, jc->pc + longofs)); -# 1122 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_RET: - /*|9: */ - /*| add rsp, 8 */ - /*| ret */ - dasm_put(Dst, 2459); -# 1127 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_TAG1: - jittag(jc, (arg >> 8) & 0xff, 1, (int8_t)arg, method); - break; - case OP_TAG2: - jittag(jc, (arg >> 8) & 0xffff, 2, (int8_t)arg, method); - break; - case OP_TAGN: { - uint64_t tag; - memcpy(&tag, jc->pc, 8); - jittag(jc, tag, arg >> 8, (int8_t)arg, method); - break; - } - case OP_DISPATCH: - /*| call =>jmptarget(jc, &method->dispatch) */ - dasm_put(Dst, 2456, jmptarget(jc, &method->dispatch)); -# 1142 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_HALT: - UPB_ASSERT(false); - } - } - - asmlabel(jc, "eof"); - /*| nop */ - dasm_put(Dst, 2214); -# 1150 "upb/pb/compile_decoder_x64.dasc" -} -- cgit v1.2.3