Post AJLGZ66e6kfFMwA1bs by sjb@mstdn.io
 (DIR) More posts by sjb@mstdn.io
 (DIR) Post #AJK8zx054t6E9fQKMy by wolf480pl@mstdn.io
       2022-05-10T23:07:42Z
       
       0 likes, 0 repeats
       
       If you took an interpreter of a programming language, put some code in that language in a constant in the interpreter, made main() call the interpreter on that constant, and then compiled the whole thing with maximum optimization...would that be as performant as having a compiler for your language?
       
 (DIR) Post #AJK9wQ0ROQIauWTIhs by amirouche@social.hyper.dev
       2022-05-10T23:17:35Z
       
       0 likes, 0 repeats
       
       @wolf480pl it depends on the interpreter, some interpreters do or could do dead code elimination.
       
 (DIR) Post #AJKAWRufmR5ZFYxEsC by rook@hulvr.com
       2022-05-10T23:24:46Z
       
       0 likes, 0 repeats
       
       @wolf480pl no, even a first-order compiler would do a better job
       
 (DIR) Post #AJKCWZdPGu6d6e47Qu by sjb@mstdn.io
       2022-05-10T23:46:51Z
       
       0 likes, 0 repeats
       
       @wolf480pl You mean a theoretical "best possible" compiler could do the entire program at compile time (like Turing-complete templates in C++)?Although gcc is clever with constexpr stuff, I doubt it's that clever.
       
 (DIR) Post #AJKhHY9Jbp10jcJ3su by amonakov@mastodon.gamedev.place
       2022-05-11T05:31:49Z
       
       0 likes, 0 repeats
       
       @wolf480pl that would attempt to perform the first Futamura projection!
       
 (DIR) Post #AJKmbz1XtmojeDEdaC by wolf480pl@mstdn.io
       2022-05-11T06:31:11Z
       
       0 likes, 0 repeats
       
       @sjb it's just const propagation, inlining, and loop unrolling, how hard could it be...
       
 (DIR) Post #AJKmiTj2fETIbWg3Bw by wolf480pl@mstdn.io
       2022-05-11T06:32:45Z
       
       0 likes, 0 repeats
       
       @sjb if I can do it with assembly code on paper during an exam, gcc should be able to do it with its IR.
       
 (DIR) Post #AJKmofxA79vsGWEkVs by wolf480pl@mstdn.io
       2022-05-11T06:33:50Z
       
       0 likes, 0 repeats
       
       @rook why? please elaborate
       
 (DIR) Post #AJKnUGNtZL7R7yZV68 by wolf480pl@mstdn.io
       2022-05-11T06:41:23Z
       
       0 likes, 0 repeats
       
       @amonakov yes. Didn't know those exist, thanks for letting me know. Are they useful in practice?
       
 (DIR) Post #AJL1SoaynidO0htZWy by amonakov@mastodon.gamedev.place
       2022-05-11T09:17:33Z
       
       0 likes, 0 repeats
       
       @wolf480pl Afraid not. Maybe in spiritual practice (as a good subject for meditation).
       
 (DIR) Post #AJLC9n1RWb517oP6Rc by sjb@mstdn.io
       2022-05-11T11:17:49Z
       
       0 likes, 0 repeats
       
       @wolf480pl It works until you need a program that gets input from the user at runtime, which won't be available at compile time.Even then, it would be possible to imagine a "parameterised" const propagation where it generates variables for unknowns too, but I seriously doubt it does that.
       
 (DIR) Post #AJLCiLd5MXvgIqUPVA by wolf480pl@mstdn.io
       2022-05-11T11:24:01Z
       
       0 likes, 0 repeats
       
       @sjb but only the interpreted code needs ti be const-propagated.Like, imagine the main loop of the interpreterfor (Instruction insn in code) {    switch (insn.opcode) {        case ADD:            vars[insn.out] = add(vars[insn.in1], vars[insn.in2];             break;        case CALL:            vars[insn.out] = call(vars[insn.fun], …); break;…    }}Once you unroll that loop, it's no longer interpreted code. It's just hella-unoptimized compiled code.
       
 (DIR) Post #AJLD7h3IqlMTz5q1ya by sjb@mstdn.io
       2022-05-11T11:28:29Z
       
       0 likes, 0 repeats
       
       @wolf480pl That assumes the interpreted code is linear.  What if the interpreted code has conditional flow control that depends on run-time input?I'm not saying it's impossible _in theory_ for a compiler to do what you want, just that it's unlikely that any existing ones do it.
       
 (DIR) Post #AJLDNecbeeFP4yxgoK by wolf480pl@mstdn.io
       2022-05-11T11:31:06Z
       
       0 likes, 0 repeats
       
       @sjb case IF:    if (vars[insn.in1]) call(vars[insn.in2]); break;
       
 (DIR) Post #AJLDOrW9C4e1MfW5ei by sjb@mstdn.io
       2022-05-11T11:31:44Z
       
       0 likes, 0 repeats
       
       @wolf480pl Yes and you can't "unroll" call(runtime value)
       
 (DIR) Post #AJLDcu4cUH9rLWKuAq by wolf480pl@mstdn.io
       2022-05-11T11:34:14Z
       
       0 likes, 0 repeats
       
       @sjb the catch is, the eval loop will be called with ither cinstants for subroutines, so the compiler would need to see through that and generate const-propagated versions for each value of the arguments.If there interpreted language has indirevt calls (first class functions or function pointers), it probably won't be able to do that.A JIT would be able to do that at runtime, for hot code  but that's a perk of being a JIT
       
 (DIR) Post #AJLDtIX7JZdtq7U0eG by wolf480pl@mstdn.io
       2022-05-11T11:37:12Z
       
       0 likes, 0 repeats
       
       @sjb it's not an arbitrary runtime value, it's one of the constants, but which one is runtime-dependent.
       
 (DIR) Post #AJLEdf8NcJ87Fx1Lk0 by sjb@mstdn.io
       2022-05-11T11:45:37Z
       
       0 likes, 0 repeats
       
       @wolf480pl RIght, so the ideal case is what you said - it generates a bunch of flow control there.  But I doubt any existing compilers would do that (or even want to do that)
       
 (DIR) Post #AJLFFcaPXQfCMHESbw by sjb@mstdn.io
       2022-05-11T11:48:54Z
       
       0 likes, 0 repeats
       
       @wolf480pl This ends up being a more awkward version of "write a program to turn your language to C and then compile the C".
       
 (DIR) Post #AJLFFd8nTaAY4vJvCy by wolf480pl@mstdn.io
       2022-05-11T11:52:27Z
       
       0 likes, 0 repeats
       
       @sjb no, it's an attempt to figure out what makes a compiler different from an interpreter
       
 (DIR) Post #AJLGZ66e6kfFMwA1bs by sjb@mstdn.io
       2022-05-11T12:07:11Z
       
       0 likes, 0 repeats
       
       @wolf480pl I think that line is blurry because of things like JIT compilers
       
 (DIR) Post #AJLaDrKppVE6Yiwp04 by rook@hulvr.com
       2022-05-11T15:47:20Z
       
       0 likes, 0 repeats
       
       @wolf480pl the short answer is no compiler will do what you're looking for
       
 (DIR) Post #AJLaMc7pkST3ze2bce by wolf480pl@mstdn.io
       2022-05-11T15:49:03Z
       
       0 likes, 0 repeats
       
       @rook you mean none of the compilers that exist currently are capable of doing it, or that there's some fundamental problem making it difficult to implement the required optimization passes in a compiler?
       
 (DIR) Post #AJLblyeNEqab2wv4Gu by rook@hulvr.com
       2022-05-11T16:04:47Z
       
       0 likes, 0 repeats
       
       @wolf480pl yes
       
 (DIR) Post #AJLcC7PXHytFkLRbhA by wolf480pl@mstdn.io
       2022-05-11T16:09:34Z
       
       0 likes, 0 repeats
       
       @rook ok clearly you don't want to elaborate
       
 (DIR) Post #AJLfhZE0E8QuqjjlUO by rook@hulvr.com
       2022-05-11T16:48:50Z
       
       0 likes, 0 repeats
       
       @wolf480pl what I don't want to do is teach a full CS course with a 500 character limitor without getting paid if I'm being honest
       
 (DIR) Post #AJLgoQlGC0bKa786Vs by wolf480pl@mstdn.io
       2022-05-11T17:01:18Z
       
       0 likes, 0 repeats
       
       @rook I've been to one, so hopefully you won't need to. Is the discussed here:https://mstdn.io/@wolf480pl/108283091636718650what you had in mind, or is there more?
       
 (DIR) Post #AJLixxZTm7Pkr2I640 by rook@hulvr.com
       2022-05-11T17:25:23Z
       
       0 likes, 0 repeats
       
       @wolf480pl I think you're confusing your needs with mine.
       
 (DIR) Post #AJLjWjBFGv5SMDXBcO by wolf480pl@mstdn.io
       2022-05-11T17:31:43Z
       
       0 likes, 0 repeats
       
       @rook I kinda assumed you replied for a reason
       
 (DIR) Post #AJLm6VvAFNn1ZFCjUO by rook@hulvr.com
       2022-05-11T18:00:34Z
       
       0 likes, 0 repeats
       
       @wolf480pl It was to answer a specific question. My bad. Don't worry, it won't happen again.