Are there any languages/compilers in existence whi...
# thinking-together
c
Are there any languages/compilers in existence which perform optimization steps while staying in the high level language, and makes these steps visible to the user? For example, instead of compiling from a tail-recursive C function, directly to a loop in assembly, it first optimises to a loop in C, then the translation to ASM is a dumb single-pass compilation. For example, this (contrived) tail call elimination is quite hard to follow if you are expecting function calls to appear in the assembly https://godbolt.org/z/jhecj5 You could have a compiler that first said, "You code is equivalent to this code" https://godbolt.org/z/3v6ezj - which is easier to see why it compiles to those same instructions.
m

https://www.youtube.com/watch?v=Os7FE3J-U5Q

💯 1
👍 1
a
GHC has a couple of intermediate forms that the code goes through. https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/debugging.html
Another interesting thing in GHC are rewrite rules: the programmer can specify how to transform expressions in high-level language into equivalent but faster expressions. https://downloads.haskell.org/~ghc/7.0.3/docs/html/users_guide/rewrite-rules.html https://wiki.haskell.org/GHC/Using_rules
m
to add more, almost all optimization passes on Erlang/Elixir are done one a representation called "core erlang" which is a low level yet human readabble functional language
👍 1
r
Meta-programming in general have been used for this kind of thing for a long time. I often see macros as a way to "add your own user defined compiler stage." There are many examples of lisp macros that try to optimize code.

Here is an example

of C++ templates being used to generate optimized shader code depending on the compile target. To be a shill for my favorite hobby lang for a bit: Nim is interesting on this topic. Nim has lisp style macros, and liberally uses high level rewrite passes to generate optimized code. For example, Nim has the concept of an inline iterator, which gives you convenient python style iterator syntax, but rewrites the code into a more optimal for loop. Async / Await in Nim is entirely built as a macro library. But, as interesting as those examples are, any language with a macro system can accomplish something similar in theory. What is interesting about Nim is that it is purely a "compiler front end" to use compiler terminology. Nim does not produce assembly language. It produces C code that can then be fed into any C compiler. This means the output is fairly high level, and while it's not "pretty" or "elegant", it's inspectable! You can read it and understand what is going on.