5 ms·
Couldn’t a tcc or similarly simple C compiler be used instead of a 100MB Clang? Where’s the C to wasm compiler hiding?
by marmaduke 2y ago
Couldn’t a tcc or similarly simple C compiler be used instead of a 100MB Clang? Where’s the C to wasm compiler hiding?
- kachapopopow 2y agoclang can target wasm already.
- syrusakbary 2y agoThe whole LLVM toolchain is a bit big. I think we can reduce much more the size. We actually researched on using tcc but unfortunately tcc doesn’t have a wasm backend (for generating wasm output). It would be awesome if they added it!
- fuhsnn 2y agoCheck out https://github.com/tyfkda/xcc https://github.com/tyfkda/xcc, I've only used the native backend, but it's small and fast.
- syrusakbary 2y agoNice! I didn’t know the project. Thanks for sharing!
- mdhb 2y agoThis project is also very much worth checking out. https://cranelift.dev/ https://cranelift.dev/ From the page: Cranelift is a fast, secure, relatively simple and innovative compiler backend. It takes an intermediate representation of a program generated by some frontend and compiles it to executable machine code. Cranelift is meant to be used as a library within an "embedder". It is in successful use by the Wasmtime WebAssembly virtual machine, for just-in-time (JIT) and ahead-of-time (AOT) compilation, and also as an experimental backend for the Rust compiler. Cranelift is an optimizing compiler, but it aims to take a fresh look at which optimizations are necessary. We have explicitly avoided features -- such as advanced alias analysis or use of undefined behavior -- that have historically led to subtle miscompilations in other compilers. Cranelift consists of about 200 thousand lines of code; in contrast, e.g. LLVM consists of over 20 million lines of code, a hundred times larger. This difference also allows Cranelift to be relatively approachable to developers, researchers, auditors and others who wish to understand how it works.
- mananaysiempre 2y agoOne 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-again/ http://troubles.md/why-do-we-need-the-relooper-algorithm-aga... [2] https://medium.com/leaningtech/solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2 https://medium.com/leaningtech/solving-the-structured-contro...
- titzer 2y ago> 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.
- alexdovzhanyn 2y agoThis is true. In Theta (https://github.com/ThetaLang/Theta https://github.com/ThetaLang/Theta) this is exactly what we do -- no need for more than one pass for the WASM codegen.
- mananaysiempre 2y agoSure, 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.)
- RossBencina 2y agoPresumably C's `switch` is also a problem.
- metadat 2y agoYes, I don't recall all the confusing elements and technicalities of what's allowed in Switch statements in C offhand but here are a few brainfscks: https://old.reddit.com/r/C_Programming/comments/16kg48y/mind_blown_by_switch_statement_syntax_and_labels/ https://old.reddit.com/r/C_Programming/comments/16kg48y/mind... https://old.reddit.com/r/programminghorror/comments/ylc7f3/why_do_c_devs_love_switch_statements_so_much/ https://old.reddit.com/r/programminghorror/comments/ylc7f3/w...
- halb 2y agoIf 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
- gnulinux 2y agoI recently wanted to use tcc for a homebaked programming sideproject and was surprised to find it's no longer supported anymore, at least not by Fabrice Bellard. Upstream git still has some light activity but no releases. I wasn't sure how good of an idea it is to rely on it as a code generator.
- stefanos82 2y agoIt's alive and kicking my friend https://repo.or.cz/tinycc.git/shortlog https://repo.or.cz/tinycc.git/shortlog We wait for grischka to decide when to announce a new release https://lists.nongnu.org/archive/html/tinycc-devel/2024-10/msg00000.html https://lists.nongnu.org/archive/html/tinycc-devel/2024-10/m...
- gnulinux 2y agoI see thanks, that's great.