Skip to content
Draft
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
107 changes: 101 additions & 6 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ fn is_prim_type_id(resolve: &Resolve, id: TypeId) -> bool {
| TypeDefKind::Stream(_)
| TypeDefKind::Unknown => false,
TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
TypeDefKind::Map(key, value) => is_prim_type(resolve, key) && is_prim_type(resolve, value),
}
}

Expand Down Expand Up @@ -1174,7 +1174,12 @@ pub fn push_ty_name(resolve: &Resolve, ty: &Type, src: &mut String) {
}
TypeDefKind::Unknown => unreachable!(),
TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
TypeDefKind::Map(key, value) => {
src.push_str("map_");
push_ty_name(resolve, key, src);
src.push_str("_");
push_ty_name(resolve, value, src);
}
}
}
}
Expand Down Expand Up @@ -1383,12 +1388,12 @@ impl Return {
TypeDefKind::Tuple(_)
| TypeDefKind::Record(_)
| TypeDefKind::List(_)
| TypeDefKind::Map(_, _)
| TypeDefKind::Variant(_) => {}

TypeDefKind::Resource => todo!("return_single for resource"),
TypeDefKind::Unknown => unreachable!(),
TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
}

self.retptrs.push(*orig_ty);
Expand Down Expand Up @@ -1735,6 +1740,23 @@ void __wasm_export_{ns}_{snake}_dtor({ns}_{snake}_t* arg) {{
self.finish_typedef_struct(id);
}

fn type_map(&mut self, id: TypeId, _name: &str, key: &Type, value: &Type, docs: &Docs) {
self.src.h_defs("\n");
self.docs(docs, SourceType::HDefs);
let map_ty = self.r#gen.type_name(&Type::Id(id));
let entry_ty = format!("{map_ty}_entry_t");
uwriteln!(self.src.h_defs, "typedef struct {entry_ty} {{");
self.print_ty(SourceType::HDefs, key);
self.src.h_defs(" key;\n");
self.print_ty(SourceType::HDefs, value);
self.src.h_defs(" value;\n");
uwriteln!(self.src.h_defs, "}} {entry_ty};");
self.start_typedef_struct(id);
uwriteln!(self.src.h_defs, "{entry_ty} *ptr;");
self.src.h_defs("size_t len;\n");
self.finish_typedef_struct(id);
}

fn type_fixed_length_list(
&mut self,
_id: TypeId,
Expand Down Expand Up @@ -1844,6 +1866,24 @@ impl<'a> wit_bindgen_core::AnonymousTypeGenerator<'a> for InterfaceGenerator<'a>
self.print_typedef_target(id);
}

fn anonymous_type_map(&mut self, id: TypeId, key: &Type, value: &Type, _docs: &Docs) {
let map_ty = self.r#gen.type_name(&Type::Id(id));
let entry_ty = format!("{map_ty}_entry_t");
uwriteln!(self.src.h_defs, "\ntypedef struct {entry_ty} {{");
self.print_ty(SourceType::HDefs, key);
self.src.h_defs(" key;\n");
self.print_ty(SourceType::HDefs, value);
self.src.h_defs(" value;\n");
uwriteln!(self.src.h_defs, "}} {entry_ty};");
self.src.h_defs("\ntypedef ");
self.src.h_defs("struct {\n");
uwriteln!(self.src.h_defs, "{entry_ty} *ptr;");
self.src.h_defs("size_t len;\n");
self.src.h_defs("}");
self.src.h_defs(" ");
self.print_typedef_target(id);
}

fn anonymous_type_future(&mut self, id: TypeId, _ty: &Option<Type>, _docs: &Docs) {
self.src.h_defs("\ntypedef uint32_t ");
self.print_typedef_target(id);
Expand Down Expand Up @@ -2005,6 +2045,21 @@ impl InterfaceGenerator<'_> {
uwriteln!(self.src.c_helpers, "}}");
}

TypeDefKind::Map(key, value) => {
self.src.c_helpers("size_t map_len = ptr->len;\n");
uwriteln!(self.src.c_helpers, "if (map_len > 0) {{");
let map_ty = self.r#gen.type_name(&Type::Id(id));
let entry_ty = format!("{map_ty}_entry_t");
uwriteln!(self.src.c_helpers, "{entry_ty} *map_ptr = ptr->ptr;");
self.src
.c_helpers("for (size_t i = 0; i < map_len; i++) {\n");
self.free(key, "&map_ptr[i].key");
self.free(value, "&map_ptr[i].value");
self.src.c_helpers("}\n");
uwriteln!(self.src.c_helpers, "free(map_ptr);");
uwriteln!(self.src.c_helpers, "}}");
}

TypeDefKind::Variant(v) => {
self.src.c_helpers("switch ((int32_t) ptr->tag) {\n");
for (i, case) in v.cases.iter().enumerate() {
Expand Down Expand Up @@ -2045,7 +2100,6 @@ impl InterfaceGenerator<'_> {
}
TypeDefKind::Unknown => unreachable!(),
TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
}
if c_helpers_body_start == self.src.c_helpers.len() {
self.src.c_helpers.as_mut_string().truncate(c_helpers_start);
Expand Down Expand Up @@ -2739,7 +2793,9 @@ void {name}_return({return_ty}) {{

TypeDefKind::Unknown => false,
TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
TypeDefKind::Map(key, value) => {
self.contains_droppable_borrow(key) || self.contains_droppable_borrow(value)
}
}
} else {
false
Expand Down Expand Up @@ -3609,6 +3665,11 @@ impl Bindgen for FunctionBindgen<'_, '_> {
results.push(format!("(uint8_t *) ({}).ptr", operands[0]));
results.push(format!("({}).len", operands[0]));
}
Instruction::MapLower { .. } => {
let _body = self.blocks.pop().unwrap();
results.push(format!("(uint8_t *) ({}).ptr", operands[0]));
results.push(format!("({}).len", operands[0]));
}

Instruction::ListLift { element, ty, .. } => {
self.assert_no_droppable_borrows("list", &Type::Id(*ty));
Expand All @@ -3621,7 +3682,19 @@ impl Bindgen for FunctionBindgen<'_, '_> {
list_name, elem_name, operands[0], operands[1]
));
}
Instruction::MapLift { ty, .. } => {
let _body = self.blocks.pop().unwrap();
self.assert_no_droppable_borrows("map", &Type::Id(*ty));
let map_name = self.r#gen.r#gen.type_name(&Type::Id(*ty));
let entry_name = format!("{map_name}_entry_t");
results.push(format!(
"({}) {{ ({}*)({}), ({}) }}",
map_name, entry_name, operands[0], operands[1]
));
}
Instruction::IterElem { .. } => results.push("e".to_string()),
Instruction::IterMapKey { .. } => results.push("map_key".to_string()),
Instruction::IterMapValue { .. } => results.push("map_value".to_string()),
Instruction::IterBasePointer => results.push("base".to_string()),

Instruction::CallWasm { sig, .. } => {
Expand Down Expand Up @@ -3956,6 +4029,28 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(self.src, "free({ptr});");
uwriteln!(self.src, "}}");
}
Instruction::GuestDeallocateMap { key, value } => {
let (body, results) = self.blocks.pop().unwrap();
assert!(results.is_empty());
let len = self.locals.tmp("len");
uwriteln!(self.src, "size_t {len} = {};", operands[1]);
uwriteln!(self.src, "if ({len} > 0) {{");
let ptr = self.locals.tmp("ptr");
uwriteln!(self.src, "uint8_t *{ptr} = {};", operands[0]);
let i = self.locals.tmp("i");
uwriteln!(self.src, "for (size_t {i} = 0; {i} < {len}; {i}++) {{");
let size = self.r#gen.r#gen.sizes.record([*key, *value]).size;
uwriteln!(
self.src,
"uint8_t *base = {ptr} + {i} * {};",
size.format(POINTER_SIZE_EXPRESSION)
);
uwriteln!(self.src, "(void) base;");
uwrite!(self.src, "{body}");
uwriteln!(self.src, "}}");
uwriteln!(self.src, "free({ptr});");
uwriteln!(self.src, "}}");
}

Instruction::Flush { amt } => {
results.extend(operands.iter().take(*amt).cloned());
Expand Down Expand Up @@ -4109,7 +4204,7 @@ pub fn is_arg_by_pointer(resolve: &Resolve, ty: &Type) -> bool {
TypeDefKind::Resource => todo!("is_arg_by_pointer for resource"),
TypeDefKind::Unknown => unreachable!(),
TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
TypeDefKind::Map(..) => true,
},
Type::String => true,
_ => false,
Expand Down
103 changes: 96 additions & 7 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
});
}
},
}
}
Expand Down Expand Up @@ -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 });
}
},
}
}
Expand Down Expand Up @@ -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),

TypeDefKind::Future(_) | TypeDefKind::Stream(_) | TypeDefKind::Handle(_) => {
self.lower_and_emit(ty, addr, &I32Store { offset })
Expand Down Expand Up @@ -2016,7 +2085,6 @@ impl<'a, B: Bindgen> Generator<'a, B> {
id,
});
}
TypeDefKind::Map(..) => todo!(),
},
}
}
Expand Down Expand Up @@ -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),

TypeDefKind::Future(_) | TypeDefKind::Stream(_) | TypeDefKind::Handle(_) => {
self.emit_and_lift(ty, addr, &I32Load { offset })
Expand Down Expand Up @@ -2216,7 +2285,6 @@ impl<'a, B: Bindgen> Generator<'a, B> {
id,
});
}
TypeDefKind::Map(..) => todo!(),
},
}
}
Expand Down Expand Up @@ -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(_)
Expand Down Expand Up @@ -2405,7 +2485,6 @@ impl<'a, B: Bindgen> Generator<'a, B> {
TypeDefKind::Unknown => unreachable!(),

TypeDefKind::FixedLengthList(..) => todo!(),
TypeDefKind::Map(..) => todo!(),
},
}
}
Expand Down Expand Up @@ -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(_)
Expand Down Expand Up @@ -2527,7 +2617,6 @@ impl<'a, B: Bindgen> Generator<'a, B> {
TypeDefKind::Stream(_) => unreachable!(),
TypeDefKind::Unknown => unreachable!(),
TypeDefKind::FixedLengthList(_, _) => {}
TypeDefKind::Map(..) => todo!(),
},
}
}
Expand Down
Loading
Loading