Skip to content
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

Read memory offset, incorporating size of statically allocated pages for string constants #22

Open
RoyalIcing opened this issue Mar 15, 2024 · 4 comments
Assignees
Milestone

Comments

@RoyalIcing
Copy link
Owner

Currently the string constants are just thrown into memory from offset 0xFF. You must write Memory.pages(1) for string constants to even work — this sucks!

What we should do is count how many bytes the string constants take and allocate an exact number of memory pages for them. e.g. 80 KiB of constants should allocate 2 pages. I don’t think we are so memory tight that we have to compress memory, so the left over memory on the last page for constants would just remain unused.

I intend to switch how String pointers are represented (See #7) but we’d still likely put 0x0 nul bytes between each string. This makes raw memory easier to debug, and possibly is useful for interop that expects nul-terminated strings (but I’m still learning whether these are something to avoid like the plague).

@RoyalIcing RoyalIcing added this to the Alpha milestone Mar 15, 2024
@RoyalIcing RoyalIcing self-assigned this Mar 15, 2024
@RoyalIcing
Copy link
Owner Author

RoyalIcing commented Apr 11, 2024

Having done a little investigation this looks difficult. We calculate all the constants when compiling the entire module.

# lib/orb.ex:637
        def __wasm_module__() do
          %{body: body, constants: constants, global_definitions: global_definitions} =
            Orb.Compiler.run(__MODULE__, @wasm_globals)

          Orb.ModuleDefinition.new()
        end       

So if we want to use the constants size at WebAssembly compile time then we have a chicken-egg problem. We use this for SilverOrb.Arena to work out which page offset an arena has allocated. So it needs to know that at compile-time.

I don’t want to compile twice as there’s a risk that state changes between the two and it results into two slightly different compiled outputs. And knowing the size of the constants would be a state change.

@RoyalIcing
Copy link
Owner Author

I'm thinking the easiest way to solve this is to have a WebAssembly global store the constant size. Operations like with arenas can just read that global.

A small pass just before assembly will set the initial value of the global. It only needs to be a read-only global.

@RoyalIcing
Copy link
Owner Author

This is now implemented, and it was much easier than anticipated!

@RoyalIcing
Copy link
Owner Author

SilverOrb.Arena still needs a way to read the page offset it has been allocated.

@RoyalIcing RoyalIcing changed the title Measure all string constants and put aside memory pages for it Read memory offset, incorporating size of statically allocated pages for string constants Jun 13, 2024
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

No branches or pull requests

1 participant