Commit graph

52 commits

Author SHA1 Message Date
Mats Sigge
71f359fbdc Move macros from roc_reporting to new roc_error_macros module
The `internal_error!` and `user_error!´ macros can't be used everywhere
when they live in `roc_reporting` due to circular dependencies.
2022-01-23 18:40:04 +01:00
ayazhafiz
4631ff96f5 Initialize stack pointer when it doesn't exist
I'm not sure this is right, but we have to do it somewhere.. and here
seems reasonable?
2021-12-26 17:46:06 -06:00
Folkert de Vries
2f1c648602
Merge pull request #2253 from rtfeldman/wasm-no-block-results
Wasm: fix nested conditionals
2021-12-21 16:35:22 +01:00
Brian Carroll
17dc6668ec Make all Wasm blocks NoResult and always put return value in a local var. 2021-12-21 13:16:15 +00:00
Brian Carroll
51119c8142 Emit less code for ensure_value_has_local 2021-12-21 13:16:15 +00:00
Brian Carroll
6480a720ae Move symbol_layouts to storage.rs 2021-12-20 16:06:39 +00:00
Brian Carroll
ece28f1975 Fix loading code for Zig calling convention 2021-12-19 21:20:06 +00:00
Brian Carroll
f3ca3be507 Add Wasm support for recursive tags, along with GetTagId and UnionAtIndex 2021-12-11 20:33:11 +00:00
Brian Carroll
3f7b3bef87 Merge branch 'trunk' of github.com:rtfeldman/roc into wasm-tags 2021-12-09 09:28:11 +00:00
Brian Carroll
1dda8859c4 Update gen_wasm's error handling to remove Result and use macros consistently 2021-12-09 00:11:44 +00:00
Brian Carroll
1ba654c5b2 Change round_up_to_alignment to a macro to work on more Rust types 2021-12-08 21:11:10 +00:00
Brian Carroll
a08633fd36 Debugging improvements 2021-11-30 10:58:03 +00:00
Brian Carroll
079a8311ec Delete HeapMemory variant of WasmLayout, just treat pointers as Primitive
When I created this (at the very beginning of the Wasm backend),
I didn't really have a clear reason for it. I just thought it might end up
making sense treat heap pointers differently from numbers, somehow.

But the semantic differences between pointers and other numbers is not relevant
to WasmLayout. The semantics are clear from where the Symbol appears in the IR.

Also we were storing heap pointers in locals, for no real reason.
And the fact that it's *different* meant a lot of new cases in match expressions,
to do the exact same thing as Primitives but with a pointless difference.

Until now, we haven't really used this variant in any of our tests.
But the refcount pointer needed it... and everything broke!
2021-11-30 09:57:00 +00:00
Brian Carroll
1f747e971f Clippy & fmt 2021-11-29 01:09:31 +00:00
Brian Carroll
dcd4914ac1 Fix and enable tests involving empty records 2021-11-29 00:57:28 +00:00
Brian Carroll
88bf6bf1b7 Clean up Wasm calling convention code 2021-11-29 00:17:45 +00:00
Folkert
4e6568016d fix comment: rust thought it was code it should execute 2021-11-23 11:56:33 +01:00
Folkert
44fdf8df18 remove unreachable cases 2021-11-22 21:40:44 +01:00
Folkert
78c26c6096 fix returning an aggregate value with ccc 2021-11-22 10:17:17 +01:00
Brian Carroll
5a8c3d4f0f Fix it again, properly this time 2021-11-21 23:49:26 +00:00
Brian Carroll
bd2813f36c Fix loading of 128-bit numbers 2021-11-21 23:46:04 +00:00
Brian Carroll
2e31350010 Logic to load 128-bit numbers 2021-11-21 20:44:16 +00:00
Brian Carroll
03c19b727a String concatenation working in gen_wasm! 2021-11-17 13:38:50 +00:00
Brian Carroll
af896e5d33 Comments 2021-11-17 09:39:16 +00:00
Brian Carroll
e9f920827e Add support for Zig/LLVM "fast calling convention" 2021-11-15 19:20:55 +00:00
Brian Carroll
29d355c4d6 Implement Expr::StructAtIndex for wasm dev backend 2021-11-14 19:06:04 +00:00
Brian Carroll
39263b0ab1 Shorter name: VmSymbolState 2021-11-14 10:57:19 +00:00
Brian Carroll
1b5740cd5d Tidy up lowlevel calls and implement a few more 2021-11-09 14:28:15 +00:00
Brian Carroll
ad9b761fce Move wasm file format code into a submodule 2021-11-03 11:20:16 +00:00
Brian Carroll
973626fe2d Rename FunctionBuilder back to CodeBuilder 2021-10-23 13:48:20 +02:00
Brian Carroll
dbe6d195f7 Delete CodeBuilder 2021-10-23 13:40:10 +02:00
Brian Carroll
74e3239a1c Switch over to function_builder 2021-10-23 13:39:54 +02:00
Brian Carroll
5ea313f256 update gen_wasm to use bumpalo::collections::Vec where possible 2021-10-21 21:14:15 +02:00
Brian Carroll
401f2ececd rename some methods 2021-10-18 21:35:16 +02:00
Brian Carroll
1b97675f1f Refactor the optimisation for immediately-returned Let statements
The original intention was to avoid creating a local when we define
and immediately return a primitive value. But now our default path
does avoids unnecessary locals anyway!

For StackMemory values we do need an optimised path but it's nicer
to just pass a flag to Storage::allocate.
2021-10-18 13:18:10 +02:00
Brian Carroll
6206418b67 Fix clippy warnings 2021-10-14 00:13:47 +02:00
Brian Carroll
5c42455ca7 Rename some values & improve comments 2021-10-13 10:52:51 +02:00
Brian Carroll
472943a068 Refactoring: extract Storage out of WasmBackend 2021-10-12 10:22:38 +01:00
Brian Carroll
d2e01bd10a rename SymbolStorage -> StoredValue 2021-10-12 10:20:17 +01:00
Brian Carroll
3aaafdefe1 Get Join/Jump working with VM storage 2021-10-09 16:46:59 +01:00
Brian Carroll
d81999045a Get Switch statements working with VM storage 2021-10-07 09:14:35 +01:00
Brian Carroll
d6bba482ee Treat the Virtual Machine stack as a form of SymbolStorage
Simulate the behaviour of the stack machine, mark where Symbol values
are created, and track where they are in the stack as we emit instructions.

This will allow us to be smarter with setting and getting locals.
However that's not actually implemented yet! At the moment,
we're still generating the same code as before but in a fancier way.

What's happening:

Let's say we have a function call that takes two arguments [A,B]
and that we are lucky and just happen to have a VM stack of [A,B]
That's great, we _should_ not have to do anything, just emit 'Call'

BUT what we are doing is "load A to the top" and then "load B to the top".
This sounds good but it's actually stupid
We are saying we want A at the top of the stack when we don't!!
We want it right where it is, just beneath the top!

So we are emitting code to bring it from 2nd position to the top.
How do we do that? By inserting a local.set instruction to save it,
and a local.get instruction to load it to the top!

What should be happening:

Check the entire VM stack at once for all the symbols we are trying to load.
If they happen to be there (which is likely given the IR structure) do nothing.
If it's not quite perfect... then emit lots of local.set and local.get. Whatever.

What would be the way to solve this properly?

Build the Wasm instructions as a tree, then serialise it depth-first.
The tree encodes all of the information about what needs to be on the
stack at what point in the program. If a tree node has two children,
it consumes two items from the stack. If you do it this way, all
of the instructions get executed just at the right time.
2021-10-05 21:39:19 +01:00
Brian Carroll
32f79b5ee2 Create CodeBuilder to track Wasm VM stack as we accumulate instructions 2021-10-03 22:50:11 +01:00
Brian Carroll
cc6f83f284 Clearer variant names for StackMemoryLocation 2021-10-03 21:53:18 +01:00
Brian Carroll
2756425eb9 Assert storage matches when copying 2021-10-03 21:52:27 +01:00
Brian Carroll
79a4f2bbf7 Refactor SymbolStorage local_id code, and copy_memory
SymbolStorage had some rather ad-hoc methods for extracting pieces of data.
This change makes that more intentional and organised.

Also cleaned up the API for the related function copy_memory,
as it had lots of positional arguments with the same types.
Created a struct for this just to name them and make the code clearer.
2021-10-02 22:31:48 +01:00
Brian Carroll
6aed70978d Refactor SymbolStorage into two enums instead of one 2021-10-02 16:15:06 +01:00
Brian Carroll
ffa6ff0a62 gen_wasm: Get rid of individual locals for values in stack memory
All values in stack memory can share the stack frame pointer,
with different offsets.

This avoids having to generate initialisation code for individual
pointers we used to have. It should also make things more efficient
when the runtime compiles Wasm to machine code. It can just assign
the stack frame pointer to a single CPU register and leave it there.

The original idea was to make params and local variables work the same.
(A struct param will be passed as a pointer to caller stack memory.)
But now I don't think that's worth it. Some match expressions get more
awkward this way but we might be able to refactor that to be nicer.
2021-10-02 15:12:25 +01:00
Brian Carroll
4aa2452e01 gen_wasm: Change some compiler bugs error handling from Result to panic
Result makes sense where I have something meaningful to say to the user like
"X is not implemented yet". And also for public functions that may interface
with other parts of the project like Backend.

But for private functions internal to gen_wasm, it's just unhelpful to get a stack trace
to where the Result is unwrapped! I want a stack trace to the root cause.
I always end up temporarily rewriting Err("oops") to panic!("oops") and then waiting
for it to recompile.

This feels like a more balanced approach, using each technique where it makes sense.
2021-10-02 13:40:44 +01:00
Brian Carroll
f1b14c14e3 Refactor SymbolStorage to emphasise stack/non-stack params
We don't have much (any) code that cares about the difference between
heap pointers and other primitives, but we have a _lot_ of code that cares
if it's stack memory. So let's encode it that way.
2021-09-30 17:04:16 +01:00