Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 50 additions & 46 deletions design/mvp/CanonicalABI.md
Original file line number Diff line number Diff line change
Expand Up @@ -2200,11 +2200,15 @@ memory) and a number of [code units]. There are three supported string encodings
in [`canonopt`]: [UTF-8], [UTF-16] and `latin1+utf16`. This last option allows a
*dynamic* choice between [Latin-1] and UTF-16, indicated by the high bit of the
second pointer-sized value (32nd bit on 32-bit memories, 64th bit on 64-bit
memories). String values include their original encoding and length in tagged
code units as a "hint" that enables `store_string` (defined below) to make
better up-front allocation size choices in many cases. Thus, the value produced
by `load_string` isn't simply a Python `str`, but a *tuple* containing a `str`,
the original encoding and the number of source code units.
memories). String values include a "hint" that enables `store_string` (defined
below) to make better up-front allocation size choices in many cases. Thus, the
value produced by `load_string` isn't simply a Python `str`, but a *tuple*
containing a `str`, a `StringHint` and the number of source code units (with
the UTF-16 tag removed). `StringHint` records the source encoding, splitting
`latin1+utf16` into `DYNAMIC_LATIN1` and `DYNAMIC_UTF16` according to the
dynamic choice. Because the hint is independent of the source memory's pointer
size, it can be stored into a memory with a different pointer size (and thus a
different UTF-16 tag bit).

The `MAX_STRING_BYTE_LENGTH` constant limits the byte length of a string when
loading. This limit is low enough to ensure that when storing the string via
Expand All @@ -2214,7 +2218,13 @@ short enough to be stored in 32-bit components. The worst case inflation in
string byte length is by a factor of 2 (e.g. this could occur when converting
from [UTF-8] to [UTF-16]).
```python
String = tuple[str, str, int]
class StringHint(Enum):
UTF8 = 0
UTF16 = 1
DYNAMIC_LATIN1 = 2
DYNAMIC_UTF16 = 3

String = tuple[str, StringHint, int]

def load_string(cx, ptr) -> String:
ptr_size = cx.opts.memory.ptr_size()
Expand All @@ -2234,20 +2244,28 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String:
match cx.opts.string_encoding:
case 'utf8':
alignment = 1
byte_length = tagged_code_units
code_units = tagged_code_units
byte_length = code_units
encoding = 'utf-8'
hint = StringHint.UTF8
case 'utf16':
alignment = 2
byte_length = 2 * tagged_code_units
code_units = tagged_code_units
byte_length = 2 * code_units
encoding = 'utf-16-le'
hint = StringHint.UTF16
case 'latin1+utf16':
alignment = 2
if bool(tagged_code_units & tag):
byte_length = 2 * (tagged_code_units ^ tag)
code_units = tagged_code_units ^ tag
byte_length = 2 * code_units
encoding = 'utf-16-le'
hint = StringHint.DYNAMIC_UTF16
else:
byte_length = tagged_code_units
code_units = tagged_code_units
byte_length = code_units
encoding = 'latin-1'
hint = StringHint.DYNAMIC_LATIN1

trap_if(byte_length > MAX_STRING_BYTE_LENGTH)
trap_if(ptr != align_to(ptr, alignment))
Expand All @@ -2257,7 +2275,7 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String:
except UnicodeError:
trap()

return (s, cx.opts.string_encoding, tagged_code_units)
return (s, hint, code_units)
```

Error context values are lifted directly from the current component instance's
Expand Down Expand Up @@ -2533,12 +2551,11 @@ approach to update the allocation size during the single copy. A blind
`realloc` approach would normally suffer from multiple reallocations per string
(e.g., using the standard doubling-growth strategy). However, as already shown
in `load_string` above, string values come with two useful hints: their
original encoding and number of source [code units]. From this hint data,
`StringHint` and number of source [code units]. From this hint data,
`store_string` can do a much better job minimizing the number of reallocations.

We start with a case analysis to enumerate all the meaningful encoding
combinations, subdividing the `latin1+utf16` encoding into either `latin1` or
`utf16` based on the `utf16_tag` flag set by `load_string`:
We start with a case analysis to enumerate all the meaningful combinations of
destination encoding and source `StringHint`:
```python
def store_string(cx, v: String, ptr):
begin, tagged_code_units = store_string_into_range(cx, v)
Expand All @@ -2547,42 +2564,29 @@ def store_string(cx, v: String, ptr):
store_int(cx, tagged_code_units, ptr + ptr_size, ptr_size)

def store_string_into_range(cx, v: String):
src, src_encoding, src_tagged_code_units = v
tag = utf16_tag(cx)

if src_encoding == 'latin1+utf16':
if bool(src_tagged_code_units & tag):
src_simple_encoding = 'utf16'
src_code_units = src_tagged_code_units ^ tag
else:
src_simple_encoding = 'latin1'
src_code_units = src_tagged_code_units
else:
src_simple_encoding = src_encoding
src_code_units = src_tagged_code_units

src, src_hint, src_code_units = v
match cx.opts.string_encoding:
case 'utf8':
match src_simple_encoding:
case 'utf8' : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8')
case 'utf16' : return store_utf16_to_utf8(cx, src, src_code_units)
case 'latin1' : return store_latin1_to_utf8(cx, src, src_code_units)
match src_hint:
case StringHint.UTF8 : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8')
case StringHint.UTF16 : return store_utf16_to_utf8(cx, src, src_code_units)
case StringHint.DYNAMIC_LATIN1 : return store_latin1_to_utf8(cx, src, src_code_units)
case StringHint.DYNAMIC_UTF16 : return store_utf16_to_utf8(cx, src, src_code_units)
case 'utf16':
match src_simple_encoding:
case 'utf8' : return store_utf8_to_utf16(cx, src, src_code_units)
case 'utf16' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case 'latin1' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
match src_hint:
case StringHint.UTF8 : return store_utf8_to_utf16(cx, src, src_code_units)
case StringHint.UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case StringHint.DYNAMIC_UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case 'latin1+utf16':
match src_encoding:
case 'utf8' : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case 'utf16' : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case 'latin1+utf16' :
match src_simple_encoding:
case 'latin1' : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1')
case 'utf16' : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units)
match src_hint:
case StringHint.UTF8 : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case StringHint.UTF16 : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1')
case StringHint.DYNAMIC_UTF16 : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units)
```

The simplest 4 cases above can compute the exact destination size and then copy
The simplest 5 cases above can compute the exact destination size and then copy
with a simply loop (that possibly inflates Latin-1 to UTF-16 by injecting a 0
byte after every Latin-1 byte).
```python
Expand Down Expand Up @@ -4816,7 +4820,7 @@ def canon_error_context_new(opts, ptr, tagged_code_units):
inst = current_instance()
trap_if(not inst.may_leave)
if DETERMINISTIC_PROFILE or random.randint(0,1):
s = String(('', 'utf8', 0))
s = String(('', StringHint.UTF8, 0))
else:
cx = LiftLowerContext('lift', opts, inst)
s = load_string_from_range(cx, ptr, tagged_code_units)
Expand Down
73 changes: 37 additions & 36 deletions design/mvp/canonical-abi/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,13 @@ def convert_i32_to_char(cx, i):
trap_if(0xD800 <= i <= 0xDFFF)
return chr(i)

String = tuple[str, str, int]
class StringHint(Enum):
UTF8 = 0
UTF16 = 1
DYNAMIC_LATIN1 = 2
DYNAMIC_UTF16 = 3

String = tuple[str, StringHint, int]

def load_string(cx, ptr) -> String:
ptr_size = cx.opts.memory.ptr_size()
Expand All @@ -1354,20 +1360,28 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String:
match cx.opts.string_encoding:
case 'utf8':
alignment = 1
byte_length = tagged_code_units
code_units = tagged_code_units
byte_length = code_units
encoding = 'utf-8'
hint = StringHint.UTF8
case 'utf16':
alignment = 2
byte_length = 2 * tagged_code_units
code_units = tagged_code_units
byte_length = 2 * code_units
encoding = 'utf-16-le'
hint = StringHint.UTF16
case 'latin1+utf16':
alignment = 2
if bool(tagged_code_units & tag):
byte_length = 2 * (tagged_code_units ^ tag)
code_units = tagged_code_units ^ tag
byte_length = 2 * code_units
encoding = 'utf-16-le'
hint = StringHint.DYNAMIC_UTF16
else:
byte_length = tagged_code_units
code_units = tagged_code_units
byte_length = code_units
encoding = 'latin-1'
hint = StringHint.DYNAMIC_LATIN1

trap_if(byte_length > MAX_STRING_BYTE_LENGTH)
trap_if(ptr != align_to(ptr, alignment))
Expand All @@ -1377,7 +1391,7 @@ def load_string_from_range(cx, ptr, tagged_code_units) -> String:
except UnicodeError:
trap()

return (s, cx.opts.string_encoding, tagged_code_units)
return (s, hint, code_units)

def lift_error_context(cx, i):
errctx = cx.inst.handles.get(i)
Expand Down Expand Up @@ -1552,39 +1566,26 @@ def store_string(cx, v: String, ptr):
store_int(cx, tagged_code_units, ptr + ptr_size, ptr_size)

def store_string_into_range(cx, v: String):
src, src_encoding, src_tagged_code_units = v
tag = utf16_tag(cx)

if src_encoding == 'latin1+utf16':
if bool(src_tagged_code_units & tag):
src_simple_encoding = 'utf16'
src_code_units = src_tagged_code_units ^ tag
else:
src_simple_encoding = 'latin1'
src_code_units = src_tagged_code_units
else:
src_simple_encoding = src_encoding
src_code_units = src_tagged_code_units

src, src_hint, src_code_units = v
match cx.opts.string_encoding:
case 'utf8':
match src_simple_encoding:
case 'utf8' : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8')
case 'utf16' : return store_utf16_to_utf8(cx, src, src_code_units)
case 'latin1' : return store_latin1_to_utf8(cx, src, src_code_units)
match src_hint:
case StringHint.UTF8 : return store_string_copy(cx, src, src_code_units, 1, 1, 'utf-8')
case StringHint.UTF16 : return store_utf16_to_utf8(cx, src, src_code_units)
case StringHint.DYNAMIC_LATIN1 : return store_latin1_to_utf8(cx, src, src_code_units)
case StringHint.DYNAMIC_UTF16 : return store_utf16_to_utf8(cx, src, src_code_units)
case 'utf16':
match src_simple_encoding:
case 'utf8' : return store_utf8_to_utf16(cx, src, src_code_units)
case 'utf16' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case 'latin1' : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
match src_hint:
case StringHint.UTF8 : return store_utf8_to_utf16(cx, src, src_code_units)
case StringHint.UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case StringHint.DYNAMIC_UTF16 : return store_string_copy(cx, src, src_code_units, 2, 2, 'utf-16-le')
case 'latin1+utf16':
match src_encoding:
case 'utf8' : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case 'utf16' : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case 'latin1+utf16' :
match src_simple_encoding:
case 'latin1' : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1')
case 'utf16' : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units)
match src_hint:
case StringHint.UTF8 : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case StringHint.UTF16 : return store_string_to_latin1_or_utf16(cx, src, src_code_units)
case StringHint.DYNAMIC_LATIN1 : return store_string_copy(cx, src, src_code_units, 1, 2, 'latin-1')
case StringHint.DYNAMIC_UTF16 : return store_probably_utf16_to_latin1_or_utf16(cx, src, src_code_units)

def store_string_copy(cx, src, src_code_units, dst_code_unit_size, dst_alignment, dst_encoding):
dst_byte_length = dst_code_unit_size * src_code_units
Expand Down Expand Up @@ -2693,7 +2694,7 @@ def canon_error_context_new(opts, ptr, tagged_code_units):
inst = current_instance()
trap_if(not inst.may_leave)
if DETERMINISTIC_PROFILE or random.randint(0,1):
s = String(('', 'utf8', 0))
s = String(('', StringHint.UTF8, 0))
else:
cx = LiftLowerContext('lift', opts, inst)
s = load_string_from_range(cx, ptr, tagged_code_units)
Expand Down
Loading
Loading