Live data from Hacker News

Show HN: Compiling C in the browser using WebAssembly

wasmer.io

31–40 of 74 posts

Re: Show HN: Compiling C in the browser using WebAssembly

#32
post #29

Earlier quoted context omitted.

One issue with Wasm is you essentially can't target it with a single-pass compiler, unlike just about any real machine. Wasm can only represent reducible control flow, so you have to pass your control-flow graph through some variation of the Relooper[1,2]. I don't know if upstream tcc can do that (there are apparently some forks?..). [1] http://troubles.md/why-do-we-need-the-relooper-algorithm-aga... [2] https://medi…

> you essentially can't target it with a single-pass compiler, That might be true if your source language has goto, but for other languages that start with structured control flow, it's possible to just carry the structure through and emit Wasm directly from the AST.

This is true. In Theta (https://github.com/ThetaLang/Theta) this is exactly what we do -- no need for more than one pass for the WASM codegen.

Re: Show HN: Compiling C in the browser using WebAssembly

#33
post #29

Earlier quoted context omitted.

One issue with Wasm is you essentially can't target it with a single-pass compiler, unlike just about any real machine. Wasm can only represent reducible control flow, so you have to pass your control-flow graph through some variation of the Relooper[1,2]. I don't know if upstream tcc can do that (there are apparently some forks?..). [1] http://troubles.md/why-do-we-need-the-relooper-algorithm-aga... [2] https://medi…

> you essentially can't target it with a single-pass compiler, That might be true if your source language has goto, but for other languages that start with structured control flow, it's possible to just carry the structure through and emit Wasm directly from the AST.

Sure, I was speaking in the context of C specifically. (In non-simplistic compilers, you may not want to preserve the source structure anyway—e.g. in Scheme or Lua with tail calls all over the place.)

Re: Show HN: Compiling C in the browser using WebAssembly

#34
post #30

If what I want is not an executable but a shared library, does this get me anything? I currently have a use case that uses a server running an emscripten build (using SMODULARIZE and some exports, I suppose it’s not a true dylib)

Importing a wasm module from a wasm module is (non)surprisingly impossible to do -- you have to have a linker, abi and all that.

It is possible provided some care. I was looking into this with WAForth which compiles the wasm and loads it via a host function (ie. it is the hosts responsibility to make it available). I wanted to enable dynamic loading of words from disk which requires some book keeping and shuffling a bunch of bytes around during compilation to write out the bits necessary to have the host do that linking. It isn't impossible to do, just tedious and in my case, having to write it in WAT is a pain.

Re: Show HN: Compiling C in the browser using WebAssembly

#37

Earlier quoted context omitted.

Very interesting idea but I have to say that those goals are not possible with a simple OS, at least by OS definitions of simple :P

The old https://webassembly.sh/ and the new https://wasmer.sh/ came a long way already. All you need is a virtual filesystem of some sort, a way to download, a way to upload, an editor, a compiler, and a VT100 JS library. We already have WASI for the rest. If the JS is too undesired, then perhaps go the old framebuffer graphics mode (e.g. a region of the WASM memory that is interpreted as an ASCII screen, or maybe ev…

The framebuffer idea is used in this wasm doom port: https://github.com/diekmann/wasm-fizzbuzz/tree/main/doom

WASIX already does all the other stuff you mentioned, including in the browser. The one thing it's missing is GUI, mainly because there's no standard GUI interface in POSIX.

Re: Show HN: Compiling C in the browser using WebAssembly

#38

Couldn’t a tcc or similarly simple C compiler be used instead of a 100MB Clang? Where’s the C to wasm compiler hiding?

If all you want to do is compile and run c code in the browser you could run tcc in the blink x86_64 emulator, running in wasm. It would take ~300Kb, less than the js & css used in the average webpage

Re: Show HN: Compiling C in the browser using WebAssembly

#39

Now all this needs is a simple OS running in a browser, that can edit and compile itself, post the resulting binary onto a WebDAV somewhere, and reload itself from there. Then it becomes a fully self-sustaining OS that can live forever in a browser.

Check out Jeff Lindsay's Wanix project: https://wanix.sh/
Post reply on HN