Readit News logoReadit News
xnacly commented on Poking holes into bytecode with peephole optimisations   xnacly.me/posts/2026/purp... · Posted by u/xnacly
mort96 · 25 days ago
This is cool! Makes me want to implement peephole optimizations in some of my own bytecode interpreters.

Though I'm wondering about the const_binary optimization:

    > This optimisation refers to a binary instruction
    > and both lhs and rhs are created via LoadImm directly
    > in the window beforehand:

        __entry:
                load_imm r0, #2
                load_imm r1, #3
                add r2, r0, r1 ; <-- this is a compile time known result := 2+3=5
                load_imm r1, #4
                load_imm r0, #1
                sub r3, r1, r0 ; <-- this is a compile time known result := 4-1=3
                mul r0, r2, r3

    > In this example add, sub and mul are constant.
    > The optimisation itself applies to all arithmetics.
    > Thus after optimising:

        __entry:
                load_imm r2, #5
                load_imm r3, #3
                mul r0, r2, r3
Is this valid? The first const_binary optimization changes the values of r0 and r1 (before the optimization they're 2 and 3, after the optimization they're unknown). The second const_binary optimization changes the values of r1 and r0 (before the optimization they're 4 and 1, after the optimization they're unknown). An optimizer which analyzes the whole block would be able to see that r0 and r1 are written to before they're read from, but the peephole optimizer only sees the 3 instructions, right?

I would get how this works in a stack based bytecode, as 'push 2; push 3; add' could be replaced with 'push 5' without affecting the machine state after the optimization. But why is the same optimization valid for this register machine? Does purple-garden impose some invariant that a register may be read at most once after being written to?

xnacly · 25 days ago
Hi, no it doesnt, this example of course breaks using temporary registers used after the optimisation. But thats why peephole is a fallback to ir optimisation, since they are only local.

I honestly didnt think of this possibility while building the optimisation. Since constant folding is done as an ir pass i will remove the peephole pass, thanks for noticing :)

xnacly commented on Ask HN: Share your personal website    · Posted by u/susam
Djonckheere · a month ago
Your site is the second one I've seen using the JetBrains Mono typeface -really easy on the eyes, even when set light grey #eff1f5 on a very dark #07080d background.
xnacly · 25 days ago
I love jetbrains mono, together with go mono my favourite fonts
xnacly commented on Poking holes into bytecode with peephole optimisations   xnacly.me/posts/2026/purp... · Posted by u/ibobev
lkey · a month ago
The article is fine, except for the example, and I suppose by extension, the grammar of the lang itself.

The example: 2+3*4-1 should resolve to either LR: 19 RL: -7 PEMDAS: 13

and never this: 15

Who would expect a language where add/sub is more tightly binding than mul? Its feels akin to starting your indexes at 2, it's not illegal, if you remember everything works, but its a true foot gun in a scripting lang.

xnacly · 25 days ago
Im sorry i forgot to highlight the braces around (2+3)*(4-1);

Either way the parser isnt implemented yet and all ast examples for the compiler are hardcoded

xnacly commented on Ask HN: Share your personal website    · Posted by u/susam
xnacly · a month ago
xnacly.me
xnacly commented on Zen-C: Write like a high-level language, run like C   github.com/z-libs/Zen-C... · Posted by u/simonpure
GrowingSideways · a month ago
Why not compile to rust or assembly? C seems like an odd choice.

In fact why not simply write rust to begin with?

xnacly · a month ago
Assembly requires way more work than compiling to, say C. Clang and gcc do a lot of the heavy lifting regarding optimisation, spilling values to the stack, etc
xnacly commented on I'm just having fun   jyn.dev/i-m-just-having-f... · Posted by u/lemper
GaryBluto · 2 months ago
> do better

Can you write replies without turning to the big book of HRisms?

xnacly · 2 months ago
No clue what that is, I dont talk to hr
xnacly commented on I'm just having fun   jyn.dev/i-m-just-having-f... · Posted by u/lemper
xnacly · 2 months ago
what a weird and mean comment, do better
xnacly commented on Programming language speed comparison using Leibniz formula for π   niklas-heer.github.io/spe... · Posted by u/PKop
xnacly · 2 months ago
the rust example is so far off being useful and file io seems completly dumb in this context

u/xnacly

KarmaCake day564April 27, 2022
About
website: xnacly.me

“Arguing that you don’t care about the right to privacy because you have nothing to hide is no different than saying you don’t care about free speech because you have nothing to say.” ~ Edward Snowden

View Original