-
Notifications
You must be signed in to change notification settings - Fork 261
Add map support #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yordis
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
yordis:yordis/feat-map-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,093
−20
Draft
Add map support #1562
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -310,6 +310,28 @@ def_instruction! { | |
| ty: TypeId, | ||
| } : [2] => [1], | ||
|
|
||
| /// Lowers a map into a canonical pointer/length pair. | ||
| /// | ||
| /// This operation pops a map value from the stack and pushes pointer | ||
| /// and length. A block is popped from the block stack to lower one | ||
| /// key/value entry into linear memory. | ||
| MapLower { | ||
| key: &'a Type, | ||
| value: &'a Type, | ||
| realloc: Option<&'a str>, | ||
| } : [1] => [2], | ||
|
|
||
| /// Lifts a canonical pointer/length pair into a map. | ||
| /// | ||
| /// This operation consumes pointer and length from the stack. A block | ||
| /// is popped from the block stack and must produce key/value for one | ||
| /// map entry. | ||
| MapLift { | ||
| key: &'a Type, | ||
| value: &'a Type, | ||
| ty: TypeId, | ||
| } : [2] => [1], | ||
|
|
||
| /// Pops all fields for a fixed list off the stack and then composes them | ||
| /// into an array. | ||
| FixedLengthListLift { | ||
|
|
@@ -349,6 +371,14 @@ def_instruction! { | |
| /// This is only used inside of blocks related to lowering lists. | ||
| IterElem { element: &'a Type } : [0] => [1], | ||
|
|
||
| /// Pushes an operand onto the stack representing the current map key | ||
| /// for each map iteration. | ||
| IterMapKey { key: &'a Type } : [0] => [1], | ||
|
|
||
| /// Pushes an operand onto the stack representing the current map value | ||
| /// for each map iteration. | ||
| IterMapValue { value: &'a Type } : [0] => [1], | ||
|
|
||
| /// Pushes an operand onto the stack representing the base pointer of | ||
| /// the next element in a list. | ||
| /// | ||
|
|
@@ -581,6 +611,17 @@ def_instruction! { | |
| element: &'a Type, | ||
| } : [2] => [0], | ||
|
|
||
| /// Used exclusively for guest-code generation this indicates that a | ||
| /// map is being deallocated. The ptr/length are on the stack and are | ||
| /// popped off and used to deallocate the map entry buffer. | ||
| /// | ||
| /// This variant also pops a block off the block stack to be used as | ||
| /// the body of the deallocation loop over map entries. | ||
| GuestDeallocateMap { | ||
| key: &'a Type, | ||
| value: &'a Type, | ||
| } : [2] => [0], | ||
|
|
||
| /// Used exclusively for guest-code generation this indicates that | ||
| /// a variant is being deallocated. The integer discriminant is popped | ||
| /// off the stack as well as `blocks` number of blocks popped from the | ||
|
|
@@ -875,7 +916,7 @@ fn needs_deallocate(resolve: &Resolve, ty: &Type, what: Deallocate) -> bool { | |
| TypeDefKind::Future(_) | TypeDefKind::Stream(_) => what.handles(), | ||
| TypeDefKind::Unknown => unreachable!(), | ||
| TypeDefKind::FixedLengthList(t, _) => needs_deallocate(resolve, t, what), | ||
| TypeDefKind::Map(..) => todo!(), | ||
| TypeDefKind::Map(_, _) => true, | ||
| }, | ||
|
|
||
| Type::Bool | ||
|
|
@@ -1618,7 +1659,25 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| self.lower(ty); | ||
| } | ||
| } | ||
| TypeDefKind::Map(..) => todo!(), | ||
| TypeDefKind::Map(key, value) => { | ||
| let realloc = self.list_realloc(); | ||
| let value_offset = self.bindgen.sizes().field_offsets([key, value])[1].0; | ||
| self.push_block(); | ||
| self.emit(&IterMapKey { key }); | ||
| self.emit(&IterBasePointer); | ||
| let key_addr = self.stack.pop().unwrap(); | ||
| self.write_to_memory(key, key_addr, Default::default()); | ||
| self.emit(&IterMapValue { value }); | ||
| self.emit(&IterBasePointer); | ||
| let value_addr = self.stack.pop().unwrap(); | ||
| self.write_to_memory(value, value_addr, value_offset); | ||
| self.finish_block(0); | ||
| self.emit(&MapLower { | ||
| key, | ||
| value, | ||
| realloc, | ||
| }); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -1819,7 +1878,16 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| id, | ||
| }); | ||
| } | ||
| TypeDefKind::Map(..) => todo!(), | ||
| TypeDefKind::Map(key, value) => { | ||
| let value_offset = self.bindgen.sizes().field_offsets([key, value])[1].0; | ||
| self.push_block(); | ||
| self.emit(&IterBasePointer); | ||
| let entry_addr = self.stack.pop().unwrap(); | ||
| self.read_from_memory(key, entry_addr.clone(), Default::default()); | ||
| self.read_from_memory(value, entry_addr, value_offset); | ||
| self.finish_block(2); | ||
| self.emit(&MapLift { key, value, ty: id }); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -1907,6 +1975,7 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| Type::Id(id) => match &self.resolve.types[id].kind { | ||
| TypeDefKind::Type(t) => self.write_to_memory(t, addr, offset), | ||
| TypeDefKind::List(_) => self.write_list_to_memory(ty, addr, offset), | ||
| TypeDefKind::Map(_, _) => self.write_list_to_memory(ty, addr, offset), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to below, can this have a comment for why |
||
|
|
||
| TypeDefKind::Future(_) | TypeDefKind::Stream(_) | TypeDefKind::Handle(_) => { | ||
| self.lower_and_emit(ty, addr, &I32Store { offset }) | ||
|
|
@@ -2016,7 +2085,6 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| id, | ||
| }); | ||
| } | ||
| TypeDefKind::Map(..) => todo!(), | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -2115,6 +2183,7 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| TypeDefKind::Type(t) => self.read_from_memory(t, addr, offset), | ||
|
|
||
| TypeDefKind::List(_) => self.read_list_from_memory(ty, addr, offset), | ||
| TypeDefKind::Map(_, _) => self.read_list_from_memory(ty, addr, offset), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this have a comment for why |
||
|
|
||
| TypeDefKind::Future(_) | TypeDefKind::Stream(_) | TypeDefKind::Handle(_) => { | ||
| self.emit_and_lift(ty, addr, &I32Load { offset }) | ||
|
|
@@ -2216,7 +2285,6 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| id, | ||
| }); | ||
| } | ||
| TypeDefKind::Map(..) => todo!(), | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -2339,6 +2407,18 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| self.emit(&Instruction::GuestDeallocateList { element }); | ||
| } | ||
|
|
||
| TypeDefKind::Map(key, value) => { | ||
| let value_offset = self.bindgen.sizes().field_offsets([key, value])[1].0; | ||
| self.push_block(); | ||
| self.emit(&IterBasePointer); | ||
| let entry_addr = self.stack.pop().unwrap(); | ||
| self.deallocate_indirect(key, entry_addr.clone(), Default::default(), what); | ||
| self.deallocate_indirect(value, entry_addr, value_offset, what); | ||
| self.finish_block(0); | ||
|
|
||
| self.emit(&Instruction::GuestDeallocateMap { key, value }); | ||
| } | ||
|
|
||
| TypeDefKind::Handle(Handle::Own(_)) | ||
| | TypeDefKind::Future(_) | ||
| | TypeDefKind::Stream(_) | ||
|
|
@@ -2405,7 +2485,6 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| TypeDefKind::Unknown => unreachable!(), | ||
|
|
||
| TypeDefKind::FixedLengthList(..) => todo!(), | ||
| TypeDefKind::Map(..) => todo!(), | ||
| }, | ||
| } | ||
| } | ||
|
|
@@ -2464,6 +2543,17 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| self.deallocate(ty, what); | ||
| } | ||
|
|
||
| TypeDefKind::Map(_, _) => { | ||
| self.stack.push(addr.clone()); | ||
| self.emit(&Instruction::PointerLoad { offset }); | ||
| self.stack.push(addr); | ||
| self.emit(&Instruction::LengthLoad { | ||
| offset: offset + self.bindgen.sizes().align(ty).into(), | ||
| }); | ||
|
|
||
| self.deallocate(ty, what); | ||
| } | ||
|
|
||
| TypeDefKind::Handle(Handle::Own(_)) | ||
| | TypeDefKind::Future(_) | ||
| | TypeDefKind::Stream(_) | ||
|
|
@@ -2527,7 +2617,6 @@ impl<'a, B: Bindgen> Generator<'a, B> { | |
| TypeDefKind::Stream(_) => unreachable!(), | ||
| TypeDefKind::Unknown => unreachable!(), | ||
| TypeDefKind::FixedLengthList(_, _) => {} | ||
| TypeDefKind::Map(..) => todo!(), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to other languages, I don't think this should be added without tests. Unlike other languages, though, I can review this, so mind adding some tests using the C bits?