Live data from Hacker News

Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

github.com

21–30 of 44 posts

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#21

This is really awesome, good work! We did a similar thing running Clang on the browser, which is now uploaded to WAPM: https://wapm.io/syrusakbary/clang

Excellent, I wonder if using a two year old LLVM fork is still required, wasm support should be in trunk by now.

Running Clang with WASI requires compiling LLVM to WASI first, which unfortunately currently requires a fork (because of the "hackyness" of it, as some syscalls are mocked or certain code is just skipped).

This talk from Ben Smith goes into a bit more depth on what was required to change on LLVM to compile to WASI: https://www.youtube.com/watch?v=5N4b-rU-OAA

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#22

directly when reading this code I cannot help myself but to get into review-mode. It is an awesome example, which i will probably use myself one day. However... inc.cpp:27 Gray is not the average of RGB, it is usally approximated with `Y = 0.299 R + 0.587 G + 0.114 B`.

Marvelous example of the bikeshed effect

That doesn't seem applicable unless there are bigger problems to solve that are being ignored.

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#23
This is really cool! I never looked into WASM before, I'm really happy to see it's not that difficult to build a simple example like this.

One thing that's bothering me about that code is the declaration of `memory` as an uint8_t when it's clearly being used for its address:

    extern uint8_t memory;
And then in a lot of places:

    uint8_t* ptr = &memory;
    ptr[FOO] = BAR;
Declaring `memory` as an array is much more idiomatic C, and generates the exact same assembly:

    extern uint8_t memory[];
And then

    memory[FOO] = BAR;
I just tested it, and it seems to work exactly as one would expect from a plain C linker (so there's no WASM magic I can see). Am I missing something?

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#25

directly when reading this code I cannot help myself but to get into review-mode. It is an awesome example, which i will probably use myself one day. However... inc.cpp:27 Gray is not the average of RGB, it is usally approximated with `Y = 0.299 R + 0.587 G + 0.114 B`.

For future readers, this is the code that is being referred:

https://github.com/ern0/howto-wasm-minimal/blob/master/inc.c...

It should be:

    uint32_t gray = 0.299 * ptr[i] + 0.587 * ptr[i+1] + 0.114 * ptr[i+2];
    ptr[i] = gray;
    ptr[i+1] = gray;
    ptr[i+2] = gray;

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#27

directly when reading this code I cannot help myself but to get into review-mode. It is an awesome example, which i will probably use myself one day. However... inc.cpp:27 Gray is not the average of RGB, it is usally approximated with `Y = 0.299 R + 0.587 G + 0.114 B`.

Also, you must work in linear colour space. In OP's case you would have to apply the inverse sRGB transform, do the calculation, then apply the forward sRGB transform. Best if you just work with linear colour end to end, if you have the memory to spare.

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#28
post #23

This is really cool! I never looked into WASM before, I'm really happy to see it's not that difficult to build a simple example like this. One thing that's bothering me about that code is the declaration of `memory` as an uint8_t when it's clearly being used for its address: extern uint8_t memory; And then in a lot of places: uint8_t* ptr = &memory; ptr[FOO] = BAR; Declaring `memory` as an array is much more idiomati…

Pedantically, it might even be undefined behaviour (not sure, maybe a point of contention, similar to container_of macro). It's unlikely to cause problems, but the way you suggested is better (and can even be accompanied by a length field).

Thank you to the author though! It is a nice little example.

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#30
post #6

Very cool! I've been desperately looking for something similar for Rust. I'm too old for all these magic frameworks and "deployers" :-) I have some Rust code, possibly linking in some objects compiled from C. How do I compile to wasm and deploy without magic? Any pointers?

I made some emulators in Rust as a learning project during the start of the pandemic, and ran into the exact same issue when I wanted to make a wasm version to run in a browser. Eventually, I was able to figure out how to do it, although I do use the 'wasm-pack' Cargo package to assist with it (I think you can get away without it if you're really motivated, you just need to set up the targets and other elements yourself). Basically you define some Rust API to expose whatever you need from your project, then that and the project get compiled into one .wasm binary and some (surprisingly readable) JavaScript "glue" gets generated which allows for easy inclusion into a web page. It works well for code in the std, but I've had issues with 3rd party packages.

It's focused on emulation development, but I wrote a document that describes the process I followed: https://github.com/aquova/chip8-book/blob/master/src/wasm.md

Post reply on HN