Skip to content

Generate read & write implementation using macros#1

Merged
CharlieTap merged 2 commits intoCharlieTap:mainfrom
JackThomson2:macro_generation
Nov 13, 2025
Merged

Generate read & write implementation using macros#1
CharlieTap merged 2 commits intoCharlieTap:mainfrom
JackThomson2:macro_generation

Conversation

@JackThomson2
Copy link
Collaborator

@JackThomson2 JackThomson2 commented Nov 4, 2025

Description

Update the read and write functions to be generated by macros reducing the amount of duplicate code and consolidate the implementations.

This also updates the method used for writing different sizes to the data which generate more efficient assembly 1

Notes

This adds paste as a new dependency to cleanly generate the function names but it is considered unmaintained 2 however, this seems to be as the author considers the crate "complete"

I ran integration tests in package, but haven't run with chasm.

It does add unsafe code and we lose length checks which we may need to add back in

@JackThomson2 JackThomson2 force-pushed the macro_generation branch 2 times, most recently from f9b2a41 to 4a56f3f Compare November 4, 2025 17:49
src/macros.rs Outdated
// Safety we assume the params passed are correct
unsafe {
let pointer = self.memory.as_ptr().add(address as usize).cast::<$address_type>();
std::ptr::read_unaligned(pointer) as $read_type
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we lose the from le bytes which I guess may be a problem for the wasm spec.

Maybe we can generate something like:

let pointer = self.memory.as_ptr().add(address as usize).cast::<[u8; 2]>();
u16::from_le_bytes(std::ptr::read_unaligned(pointer)) as i32

Move the creation of the methods into macros to reduce the amount of
duplicate code required.

Update the method used for reads and writes, which generates more
efficient assembly completely bypassing the bounds checks.

Signed-off-by: Jack Thomson <jackathomson@outlook.com>
A macro to reduce the amount of duplication again by calling both
variants of read and write on all the types.

Signed-off-by: Jack Thomson <jackabt@amazon.com>
Copy link
Owner

@CharlieTap CharlieTap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@CharlieTap CharlieTap merged commit 69b5811 into CharlieTap:main Nov 13, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants