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

wasm: implement inline assembly #19686

Merged
merged 16 commits into from Oct 29, 2023
Merged

Conversation

l1mey112
Copy link
Member

@l1mey112 l1mey112 commented Oct 28, 2023

This PR implements support for inline assembly inside the wasm backend. This also allows removal of all __builtins present in the standard library, replacing them with vwasm_* functions instead, implemented with inline assembly. Compile time conditional compilation is also implemented, as well as some v fmt changes.

This PR touches the following modules:

  • import wasm - Minor fixes.
  • import v.gen.wasm - Implementation.
  • import v.pref - wasm arch changes.
  • import v.parser - Parsing wasm in asm blocks.
  • import v.fmt - Proper formatting for arch string.
  • import builtin - Remove wasm builtins, and replace with inline assembly.

🤖 Generated by Copilot at eb1eb54

This pull request adds support for compiling V code to WebAssembly (wasm) as a target operating system. It refactors the wasm memory management functions, improves the wasm code generation and error handling, adds support for inline assembly and comptime evaluation for wasm, and adds tests for the wasm features. It modifies the files vlib/builtin/wasm/builtin.v, vlib/v/gen/wasm/gen.v, vlib/v/parser/parser.v, vlib/v/pref/pref.v, vlib/wasm/instructions.v, and vlib/v/ast/ast.v, and adds the files vlib/builtin/wasm/alloc.v, vlib/v/gen/wasm/mem.v, vlib/v/fmt/asm.v, vlib/v/gen/wasm/comptime.v, and vlib/v/gen/wasm/tests/*.vv*.

🤖 Generated by Copilot at eb1eb54

  • Split builtin.v into builtin.v and alloc.v and implement memory allocation and manipulation functions for wasm using inline assembly (link, link, link, link, link, link)
  • Add wasm32 case to gen_asm method and format assembly statements with wasm literal for wasm architecture (link, link)
  • Add comptime.v file and define functions for comptime evaluation of conditional expressions for wasm using $if directives (link)
  • Modify v_error method to exit with code 1 instead of returning and remove unused code for output modes for wasm (link, link)
  • Replace __heap_base global with hp method that returns heap base global index using wasm module and create global if needed (link, link)
  • Check if if expression is comptime and call comptime_if_expr method for wasm (link)
  • Add case for ast.AsmStmt and call asm_stmt method for wasm in gen_stmt method (link)
  • Wrap type argument with ast.mktyp in new_global method to ensure valid type (link)
  • Define tee method to copy value from stack to variable for inline assembly (link)
  • Add asm.vv file and asm.vv.out file to test inline assembly feature for wasm with various arithmetic, memory, and conversion operations (link, link)
  • Add tests for wasm builtins in builtin.vv file and builtin.vv.out file (link, link)
  • Add comptime.vv file and comptime.vv.out file to test comptime evaluation of conditional expressions for wasm (link, link)
  • Report error if assembly statement is at top level and architecture is wasm32 and add wasm as valid option in error message in parse_asm_stmt method (link, link)
  • Add select and return as v keywords that are also wasm instructions and append them to instruction name in parse_asm_stmt method (link)
  • Handle instruction name with dot and check if token after dot is const and append it to name in parse_asm_stmt method (link)
  • Handle instruction argument as string literal and add it to argument list in parse_asm_stmt method (link)
  • Report error if instruction argument is addressing operand for wasm32 architecture in parse_asm_stmt method (link)
  • Add case for wasm and wasm32 in parse_target_os and os_from_str methods and set compile_defines and arch fields accordingly (link, link)
  • Rename sign_extend32_i64 method to sign_extend32 to match wasm instruction name and add is_signed parameter to cast_trapping method and use different instructions depending on parameter (link, link)

@medvednikov medvednikov merged commit c32b04d into vlang:master Oct 29, 2023
53 checks passed
@medvednikov
Copy link
Member

Great work!

sibkod pushed a commit to sibkod/v that referenced this pull request Oct 30, 2023
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.

None yet

2 participants