function-references icon indicating copy to clipboard operation
function-references copied to clipboard

Tweak binary format for table declarations

Open titzer opened this issue 3 years ago • 1 comments

The new binary format for table types allows 0x40 0x00 as a prefix to a non-nullable reference type to indicate a table that has an explicit element initializer. I think we should tweak this a little.

  1. Avoid 0x40, since that is used for an empty block type already[1]. E.g. use 0x41, which doesn't collide with anything.
  2. For table types, put the constant initializer before the limits, so that a non-nullable reference type with an explicit initializer can be parsed as a single unit. The constexpr could either come before the table type (i.e. right after the sentinel), or be inside the table type.

[1] I was initially confused by the empty block sentinel started showing up here, since Wizard tries to share some type decoding routines for simplicity.

As a bonus, by doing (2) above, we could in theory allow the explicit initializer for local types or block types, or other places where types occur.

titzer avatar Sep 30 '22 18:09 titzer

Re 1: That would be okay, but is there really a collision? 0x40 is not a proper type opcode, but more like an escape code. A decoder can first check for 0x40, and then decode the type separately – which I think is a more efficient factoring anyway, since it avoids populating the internal type representation with a pseudotype that you then have to check for retroactively, and in all other places, too.

Re 2: The primary unit of parsing here is the table type (elemtype+limits), which occurs in other places that do not have initialisers (e.g., table imports). Putting the initialiser inside the table type would clearly be in conflict with that. Putting it before would be inconsistent with the rest of the language – in all other places we know the expected result type of an instruction sequence before we decode it, and decoders may be factored towards that assumption. In either case, to parse type+initialiser as a unit you'd have to give up parsing table types as a unit, which would strike me as a very odd choice with no benefit.

rossberg avatar Oct 04 '22 07:10 rossberg

I suppose that I should just think of 0x40 as the escape for "not a value type" since it indicates an empty block type, and here, an extended global type. For 2, it is only a minor annoyance that I can fix with a few lines of code :-)

titzer avatar Nov 06 '22 16:11 titzer