From e2840a4aa1b6a7a2ca1421d0d6da3e56992e5090 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sun, 17 May 2015 16:11:07 -0700 Subject: Restructure tables for C89 port and smaller size. Changes the data layout of tables slightly so that string keys are prefixed with their size, rather than the size being inline in the table itself. This has a few benefits: 1. inttables shrink a bit, because there is no longer a wasted and unused size field sitting in them. 2. This avoids the need to have a union in the table. This is important for an impending C89 port of upb, since C89 has literally no way of statically initializing a non-first union member. --- tools/dump_cinit.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'tools/dump_cinit.lua') diff --git a/tools/dump_cinit.lua b/tools/dump_cinit.lua index dac2498..973519b 100644 --- a/tools/dump_cinit.lua +++ b/tools/dump_cinit.lua @@ -47,6 +47,20 @@ function handler_types(base) 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] @@ -214,7 +228,13 @@ function Dumper:tabkey(key) if type(key) == "nil" then return "UPB_TABKEY_NONE" elseif type(key) == "string" then - return string.format('UPB_TABKEY_STR("%s")', key) + 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 -- cgit v1.2.3