Sujet : Re: transpiling to low level C
De : cr88192 (at) *nospam* gmail.com (BGB)
Groupes : comp.lang.cDate : 17. Dec 2024, 07:40:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjr6dn$1j57r$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla Thunderbird
On 12/16/2024 7:19 PM, Keith Thompson wrote:
bart <bc@freeuk.com> writes:
[SNIP]
In that case I've no idea what you were trying to say.
>
When somebody says that 'goto' can emulate any control structure, then
clearly some of them need to be conditional; that is implied.
>
Your reply suggested they you can do away with 'goto', and use
recursive functions, in a scenario where no other control structures
need exist.
>
OK, if this is not for an IL, then it's not a language I would care
for either. Why tie one hand behind your back for no good reason?
I read Janis's post. I saw a suggestion that certain constructs are
*theoretically* unnecessary. I saw no suggestion of any advocacy for
such an approach.
"""
A 'goto' may be used but it isn't strictly *necessary*. What *is*
necessary, though, that is an 'if' (some conditional branch), and
either 'goto' or recursive functions.
"""
While if-call is technically possible, it is likely to result in a significantly higher performance overhead if compared with if-goto.
Say:
if-goto:
can be a single CPU instruction on some targets.
Both RISC-V and BJX2 can do single-instruction compare-and-branch.
if-call:
A whole lot more than 1 instruction...
One may have call/return, stack frame creation, ...
Or, a backend that is very clever about inlining.
Also: if-goto can readily express pretty much every other intra-function control flow construct:
for, while, switch, break, continue, ...
And, is less awkward for a compiler than trying to map control flow to any of the other constructs. Also it is generally the fastest construct for implementing these higher level constructs.
Meanwhile, if-call, good luck... You are also going to need something beyond normal C style variable scoping to make this work acceptably. At best, it is likely to be very awkward and likely to perform poorly.
Though, it is possible that one could make a case for a 3-way branch operator in the IR, say:
if3(x,y) A, B, C
Which does the equivalent of, say:
if(x<y)goto A;
else if(x>y)goto B;
else goto C;
This would be a bit niche, but could be useful for things like implementing "switch()" via binary subdivision.
Though, functionally could decay into normal if-goto in various cases.
...
[...]
So what was your proposal about?
I saw no proposal.