Skip to content
Merged
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
19 changes: 18 additions & 1 deletion crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,13 +1933,30 @@ impl TrampolineCompiler<'_> {
}

Transcode::Utf8ToUtf16
| Transcode::Utf16ToCompactProbablyUtf16
| Transcode::Utf8ToCompactUtf16
| Transcode::Utf16ToCompactUtf16 => {
self.raise_if_transcode_trapped(results[0]);
raw_results.push(self.cast_from_pointer(results[0], to64));
}

Transcode::Utf16ToCompactProbablyUtf16 => {
self.raise_if_transcode_trapped(results[0]);
let mut len = self.cast_from_pointer(results[0], to64);

// The libcall's utf16 tag is not the high bit of a 64-bit
// memory's pointer width, so move it there.
if to64 {
let tag = Transcode::UTF16_TO_COMPACT_PROBABLY_UTF16_TAG;
let tag_bit = self.builder.ins().band_imm_u(len, i64::from(tag));
let untagged = self.builder.ins().bxor(len, tag_bit);
let shift = i64::from(63 - tag.trailing_zeros());
let new_tag_bit = self.builder.ins().ishl_imm_u(tag_bit, shift);
len = self.builder.ins().bor(untagged, new_tag_bit);
}

raw_results.push(len);
}

Transcode::Latin1ToUtf8
| Transcode::Utf16ToUtf8
| Transcode::Utf8ToLatin1
Expand Down
6 changes: 6 additions & 0 deletions crates/environ/src/component/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,12 @@ pub enum Transcode {
}

impl Transcode {
/// The bit that the `Utf16ToCompactProbablyUtf16` host libcall sets in its
/// returned length to indicate utf16. The libcall's trampoline moves it to
/// the high bit of the destination memory's pointer width, which is the
/// guest-visible `latin1+utf16` tag.
pub const UTF16_TO_COMPACT_PROBABLY_UTF16_TAG: u32 = 1 << 31;

/// Get this transcoding's symbol fragment.
pub fn symbol_fragment(&self) -> &'static str {
match self {
Expand Down
31 changes: 24 additions & 7 deletions crates/environ/src/fact/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use wasmtime_component_util::{DiscriminantSize, FlagsSize};
use super::DataModel;

const MAX_STRING_BYTE_LENGTH: u32 = (1 << 31) - 1;
const UTF16_TAG: u32 = 1 << 31;

/// This value is arbitrarily chosen and should be fine to change at any time,
/// it just seemed like a halfway reasonable starting point.
Expand Down Expand Up @@ -1658,15 +1657,15 @@ impl<'a, 'b> Compiler<'a, 'b> {
// Test the tag big to see if this is a utf16 or a latin1 string
// at runtime...
self.instruction(LocalGet(src_str.len.idx));
self.ptr_uconst(src_mem_opts, UTF16_TAG);
self.ptr_utf16_tag(src_mem_opts);
self.ptr_and(src_mem_opts);
self.ptr_if(src_mem_opts, BlockType::Empty);

// In the utf16 block unset the upper bit from the length local
// so further calculations have the right value. Afterwards the
// string transcode proceeds assuming utf16.
self.instruction(LocalGet(src_str.len.idx));
self.ptr_uconst(src_mem_opts, UTF16_TAG);
self.ptr_utf16_tag(src_mem_opts);
self.ptr_xor(src_mem_opts);
self.instruction(LocalSet(src_str.len.idx));

Expand Down Expand Up @@ -2230,7 +2229,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
// source code unit length.
if self.module.tunables.debug_adapter_modules {
self.instruction(LocalGet(dst.len.idx));
self.ptr_uconst(dst_mem_opts, !UTF16_TAG);
self.ptr_utf16_untag_mask(dst_mem_opts);
self.ptr_and(dst_mem_opts);
self.convert_src_len_to_dst(src.len.idx, src_mem_opts.ptr(), dst_mem_opts.ptr());
self.ptr_ne(dst_mem_opts);
Expand All @@ -2239,11 +2238,11 @@ impl<'a, 'b> Compiler<'a, 'b> {
self.instruction(End);
}

// If the UTF16_TAG is set then utf16 was used and the destination
// If the utf16 tag is set then utf16 was used and the destination
// should be appropriately sized. Bail out of the "is this string
// empty" block and fall through otherwise to resizing.
self.instruction(LocalGet(dst.len.idx));
self.ptr_uconst(dst_mem_opts, UTF16_TAG);
self.ptr_utf16_tag(dst_mem_opts);
self.ptr_and(dst_mem_opts);
self.ptr_br_if(dst_mem_opts, 0);

Expand Down Expand Up @@ -2416,7 +2415,7 @@ impl<'a, 'b> Compiler<'a, 'b> {

// Tag the returned pointer as utf16
self.instruction(LocalGet(dst.len.idx));
self.ptr_uconst(dst_mem_opts, UTF16_TAG);
self.ptr_utf16_tag(dst_mem_opts);
self.ptr_or(dst_mem_opts);
self.instruction(LocalSet(dst.len.idx));

Expand Down Expand Up @@ -4302,6 +4301,24 @@ impl<'a, 'b> Compiler<'a, 'b> {
}
}

/// Pushes the `latin1+utf16` utf16 tag for `opts`'s memory.
fn ptr_utf16_tag(&mut self, opts: &LinearMemoryOptions) {
if opts.memory64() {
self.instruction(I64Const(i64::MIN));
} else {
self.instruction(I32Const(i32::MIN));
}
}

/// Pushes the mask that clears `ptr_utf16_tag` for `opts`'s memory.
fn ptr_utf16_untag_mask(&mut self, opts: &LinearMemoryOptions) {
if opts.memory64() {
self.instruction(I64Const(i64::MAX));
} else {
self.instruction(I32Const(i32::MAX));
}
}

fn ptr_iconst(&mut self, opts: &LinearMemoryOptions, val: i32) {
if opts.memory64() {
self.instruction(I64Const(val.into()));
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm/component/libcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::ptr::NonNull;
use core::slice;
use wasmtime_environ::component::*;

const UTF16_TAG: usize = 1 << 31;
const UTF16_TAG: usize = Transcode::UTF16_TO_COMPACT_PROBABLY_UTF16_TAG as usize;

macro_rules! signature {
(@ty size) => (usize);
Expand Down
Loading
Loading