Skip to content

Commit

Permalink
Merge with upstream (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhil authored Mar 11, 2024
2 parents e7e9978 + bbcc271 commit 38bb42a
Show file tree
Hide file tree
Showing 41 changed files with 262 additions and 144 deletions.
45 changes: 33 additions & 12 deletions crates/wasmprinter/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,47 @@ impl<'a, 'b> PrintOperator<'a, 'b> {
// in the case of shadowing, which would be the wrong behavior
// here. All that can be done is to print the index down below
// instead.
let name = name.and_then(|name| {
for other_label in self.label_indices[i as usize..].iter() {
let name_conflict = name.is_some()
&& self.label_indices[i as usize..].iter().any(|other_label| {
let key = (self.state.core.funcs, *other_label);
if let Some(other) = self.state.core.label_names.index_to_name.get(&key) {
if name.name == other.name {
return None;
if name.unwrap().name == other.name {
return true;
}
}
}
Some(name)
});
false
});

match name {
Some(name) => name.write(&mut self.printer.result),
// Only print the name if one is found and there's also no
// name conflict.
Some(name) if !name_conflict => name.write(&mut self.printer.result),

// If there's no name conflict, and we're synthesizing
// names, and this isn't targetting the function itself then
// print a synthesized names.
//
// Note that synthesized label names don't handle the
// function itself, so i==0, branching to a function label,
// is not supported and otherwise labels are offset by 1.
None if !name_conflict && self.printer.name_unnamed && i > 0 => {
write!(self.result(), "$#label{}", i - 1)?
}

None if self.printer.name_unnamed => write!(self.result(), "$#label{i}")?,
_ => {
// Last-ditch resort, we gotta print the index.
write!(self.result(), "{depth}")?;

// If this label has some name also print its pseudo-name as
// `@N` to help match things up in the text format.
None => write!(self.result(), "{depth} (;@{i};)")?,
// Unnamed labels have helpful `@N` labels printed for
// them so also try to print where this index is going
// (label-wise). Don't do this for a name conflict
// though because we wouldn't have printed the numbered
// label, and also don't do it for the function itself
// since the function has no label we can synthesize.
if !name_conflict && i > 0 {
write!(self.result(), " (;@{i};)")?;
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmprinter/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn label_shadowing_block_confusion() {
(func (;0;) (type 0)
block $a
block $a
br 1 (;@1;)
br 1
end
end
)
Expand Down
2 changes: 1 addition & 1 deletion crates/wast/src/core/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a> Parse<'a> for Data<'a> {
let id = parser.parse()?;
let name = parser.parse()?;

let kind = if parser.peek::<&[u8]>()? {
let kind = if parser.peek::<&[u8]>()? || parser.peek::<RParen>()? {
DataKind::Passive

// ... and otherwise we must be attached to a particular memory as well
Expand Down
16 changes: 16 additions & 0 deletions crates/wast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,22 @@ impl Peek for LParen {
}
}

/// A convenience type to use with [`Parser::peek`](crate::parser::Parser::peek)
/// to see if the next token is the end of an s-expression.
pub struct RParen {
_priv: (),
}

impl Peek for RParen {
fn peek(cursor: Cursor<'_>) -> Result<bool> {
cursor.peek_rparen()
}

fn display() -> &'static str {
"right paren"
}
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
14 changes: 14 additions & 0 deletions crates/wit-parser/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ impl Resolve {
params.truncate(0);
params.push(WasmType::Pointer);
indirect_params = true;
} else {
if matches!(
(&func.kind, variant),
(crate::FunctionKind::Method(_), AbiVariant::GuestExport)
) {
// Guest exported methods always receive resource rep as first argument
//
// TODO: Ideally you would distinguish between imported and exported
// resource Handles and then use either I32 or Pointer in abi::push_flat().
// But this contextual information isn't available, yet.
// See https://github.com/bytecodealliance/wasm-tools/pull/1438 for more details.
assert!(matches!(params[0], WasmType::I32));
params[0] = WasmType::Pointer;
}
}

let mut results = Vec::new();
Expand Down
9 changes: 5 additions & 4 deletions crates/wit-parser/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,11 @@ impl Resolve {
for dep in entries {
let path = dep.path();

let pkg = if dep.file_type()?.is_dir() {
// If this entry is a directory then always parse it as an
// `UnresolvedPackage` since it's intentional to not support
// recursive `deps` directories.
let pkg = if dep.file_type()?.is_dir() || path.metadata()?.is_dir() {
// If this entry is a directory or a symlink point to a
// directory then always parse it as an `UnresolvedPackage`
// since it's intentional to not support recursive `deps`
// directories.
UnresolvedPackage::parse_dir(&path)
.with_context(|| format!("failed to parse package: {}", path.display()))?
} else {
Expand Down
1 change: 1 addition & 0 deletions tests/cli/dump/simple.wat
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
(func (local i32) i32.const 0)
(data (i32.const 8) "y")
(data "x")
(data)
(@custom "name-of-section" "content")
)
12 changes: 7 additions & 5 deletions tests/cli/dump/simple.wat.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@
0x60 | 01 7f | 1 locals of type I32
0x62 | 41 00 | i32_const value:0
0x64 | 0b | end
0x65 | 0b 0a | data section
0x67 | 02 | 2 count
0x65 | 0b 0c | data section
0x67 | 03 | 3 count
0x68 | 00 | data memory[0]
0x69 | 41 08 | i32_const value:8
0x6b | 0b | end
0x6c |-------------| ... 1 bytes of data
0x6e | 01 01 | data passive
0x70 |-------------| ... 1 bytes of data
0x71 | 00 17 | custom section
0x73 | 0f 6e 61 6d | name: "name-of-section"
0x71 | 01 00 | data passive
0x73 |-------------| ... 0 bytes of data
0x73 | 00 17 | custom section
0x75 | 0f 6e 61 6d | name: "name-of-section"
| 65 2d 6f 66
| 2d 73 65 63
| 74 69 6f 6e
0x83 |-------------| ... 7 bytes of data
0x85 |-------------| ... 7 bytes of data
34 changes: 34 additions & 0 deletions tests/cli/print-unnamed.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;; RUN: print --name-unnamed %

(module
(func
(block $a
(block ;; unnamed
(block $c
i32.const 0
br_table $a 1 $c
)
)
)
(block $c
(block ;; unnamed
(block $a
i32.const 0
br_table $a 1 $c
)
)
)
(block $a ;; 3
(block $a ;; 2
(block $a ;; 1
i32.const 0
br_table
0 ;; -> a/1
1 ;; -> a/2
2 ;; -> a/3
3 ;; -> function label
)
)
)
)
)
29 changes: 29 additions & 0 deletions tests/cli/print-unnamed.wat.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(module
(type $#type0 (;0;) (func))
(func $#func0 (;0;) (type $#type0)
block $a
block $#label1
block $c
i32.const 0
br_table $a $#label1 $c
end
end
end
block $c
block $#label1
block $a
i32.const 0
br_table $a $#label1 $c
end
end
end
block $a
block $a
block $a
i32.const 0
br_table $a 1 2 3
end
end
end
)
)
2 changes: 1 addition & 1 deletion tests/snapshots/local/br-confusion.wat.print
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(func (;0;) (type 0)
block $a
block $a
br 1 (;@1;)
br 1
end
end
)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/local/br_table_loop_multi.wat.print
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
local.get 1
loop (type 1) (param i32) ;; label = @1
i32.const -458751
br_table 0 (;@1;) 1 (;@0;) 0 (;@1;)
br_table 0 (;@1;) 1 0 (;@1;)
unreachable
end
unreachable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
(type (;3;) (func (param externref)))
(func (;0;) (type 1) (param $r (ref null $t))
local.get $r
br_on_null 0 (;@0;)
br_on_null 0
drop
)
(func (;1;) (type 2) (param $r funcref)
local.get $r
br_on_null 0 (;@0;)
br_on_null 0
drop
)
(func (;2;) (type 3) (param $r externref)
local.get $r
br_on_null 0 (;@0;)
br_on_null 0
drop
)
)
14 changes: 7 additions & 7 deletions tests/snapshots/local/gc/type-subtyping.wast/17.print
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,46 @@
table.get 0
ref.cast (ref $t2)
end
br 0 (;@0;)
br 0
)
(func (;4;) (type 3)
block (result (ref null $t1)) ;; label = @1
i32.const 0
call_indirect (type $t1)
end
br 0 (;@0;)
br 0
)
(func (;5;) (type 3)
block (result (ref null $t1)) ;; label = @1
i32.const 0
call_indirect (type $t2)
end
br 0 (;@0;)
br 0
)
(func (;6;) (type 3)
block (result (ref null $t1)) ;; label = @1
i32.const 1
call_indirect (type $t2)
end
br 0 (;@0;)
br 0
)
(func (;7;) (type 3)
i32.const 0
table.get 0
ref.cast (ref $t1)
br 0 (;@0;)
br 0
)
(func (;8;) (type 3)
i32.const 0
table.get 0
ref.cast (ref $t2)
br 0 (;@0;)
br 0
)
(func (;9;) (type 3)
i32.const 1
table.get 0
ref.cast (ref $t2)
br 0 (;@0;)
br 0
)
(table (;0;) 3 3 funcref)
(export "run" (func 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
loop $l
local.get $i
suspend $yield
br_if 1 (;@0;)
br_if 1
local.get $i
global.get $hook
call_ref $gen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
call $log
local.get $d
i32.eqz
br_if 0 (;@0;)
br_if 0
i32.const 21
call $log
loop $l
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/local/unreachable-valid.wast/0.print
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(func (;0;) (type 0)
f32.const 0x1p+0 (;=1;)
f32.const 0x1p+1 (;=2;)
br 0 (;@0;)
br 0
i32.add
drop
)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/testsuite/binary.wast/108.print
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
i32.const 1
if ;; label = @2
i32.const 1
br_table 2 (;@0;)
br_table 2
end
end
)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/testsuite/br_if.wast/0.print
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
loop ;; label = @1
call $dummy
local.get 0
br_if 1 (;@0;)
br_if 1
end
)
(func (;18;) (type 2) (result i32)
Expand Down
6 changes: 3 additions & 3 deletions tests/snapshots/testsuite/br_table.wast/0.print
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
loop (result i32) ;; label = @1
i32.const 3
i32.const 0
br_table 1 (;@0;) 1 (;@0;)
br_table 1 1
i32.const 1
end
)
Expand All @@ -244,7 +244,7 @@
call $dummy
i32.const 4
i32.const -1
br_table 1 (;@0;) 1 (;@0;) 1 (;@0;)
br_table 1 1 1
i32.const 2
end
)
Expand All @@ -254,7 +254,7 @@
call $dummy
i32.const 5
i32.const 1
br_table 1 (;@0;) 1 (;@0;) 1 (;@0;)
br_table 1 1 1
end
)
(func (;23;) (type 2) (result i32)
Expand Down
Loading

0 comments on commit 38bb42a

Please sign in to comment.