Sujet : Re: transpiling to low level C
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 17. Dec 2024, 13:04:29
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjrpcd$1nqeb$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 16/12/2024 21:23, Lawrence D'Oliveiro wrote:
On Sun, 15 Dec 2024 17:53:30 -0600, BGB wrote:
As an IL, even C is a little overkill, unless turned into a restricted
subset ...
Why not use WASM as your IL?
Have you tried it? I mean, directly generating WASM from a compiler front-end, not just using somebody else's tool to do so.
WASM is a stack-based language, but one that supposedly doesn't even have branching, although there is a 'br' statement, with some restrictions.
Information about it is quite elusive; it took me 5 minutes to even get examples of what it looks like (and I've seen it before).
C can apparently compile to WASM via Clang, so I tried this program:
void F(void) {
int i=0;
while (i<10000) ++i;
}
which compiled to 128 lines of WASM (technically, some form of 'WAT', as WASM is a binary format). The 60 lines correspondoing to F are shown below, and below that, is my own stack IL code.
So, what do you with your WASM/WAT program once generated? I've no idea, except that WASM is inextricably typed up with with browsers and with JavaScript, in which I have no interest.
With C, you run a compiler; with ASM, an assembler; these formats are well understood.
You can appreciate that it can be easier to devise your own format and your own tools that you understand 100%.
--------------------------------------
F: # @F
.functype F () -> ()
.local i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32
# %bb.0:
global.get __stack_pointer
local.set 0
i32.const 16
local.set 1
local.get 0
local.get 1
i32.sub
local.set 2
i32.const 0
local.set 3
local.get 2
local.get 3
i32.store 12
.LBB0_1: # =>This Inner Loop Header: Depth=1
block
loop # label1:
local.get 2
i32.load 12
local.set 4
i32.const 10000
local.set 5
local.get 4
local.set 6
local.get 5
local.set 7
local.get 6
local.get 7
i32.lt_s
local.set 8
i32.const 1
local.set 9
local.get 8
local.get 9
i32.and
local.set 10
local.get 10
i32.eqz
br_if 1 # 1: down to label0
# %bb.2: # in Loop: Header=BB0_1 Depth=1
local.get 2
i32.load 12
local.set 11
i32.const 1
local.set 12
local.get 11
local.get 12
i32.add
local.set 13
local.get 2
local.get 13
i32.store 12
br 0 # 0: up to label1
.LBB0_3:
end_loop
end_block # label0:
return
end_function
-----------------------------
proc F::
local i32 i.1
load i32 0
store i32 i.1
jump #2
#4:
load u64 &i.1
incrto i32 /1
#2:
load i32 i.1
load i32 10000
jumplt i32 #4
#3:
#1:
retproc
endproc