0

There are packages to scan/parse/typecheck Go code, but what about optimizations?

Suppose we have a program that translates Go to the other target language. If we do it only based on the AST, we will miss all optimizations (unless we apply them by hand). Target environment can have really bad or no optimizer at all, and of course even if it has one, it could not be able to optimize transcompiled code well.

Simply put: what are our options?

Maybe there is some way to compile Go to its assembler with optimizations and then (either of 2): a) translate assembler back to Go and traverse AST as usual?
b) read assembler into some kind of IR structure and use that to emit code?

Thanks in advance.

  • 1
    Assembly / IR is lossy - several different AST representations might finally compile down into the same ASM. For transpiling you'd normally want to carry the intention of the code. This in itself is difficult, Go might transpile clumsily to JavaScript, it's unlikely to output generic code too if you the target was "idiomatic" Rust/C++. This might give you some inspiration: https://github.com/gopherjs/gopherjs – Martin Gallagher Apr 17 '17 at 08:33
  • @MartinGallagher, I believe there are some fundamental optimizations like constant folding, expression strength reduction, etc. API that would be perfect expects a list of selected optimizations and applies them. Then I could select "inlining" and sign that "I know that it will change the result call stack shape". – Iskander Sharipov Apr 17 '17 at 08:37
  • @MartinGallagher btw, thanks for the link and feedback. Do you think that implementing simple optimizations for each Go transcompiler is wasteful or is it unavoidable in some sense? I'm afraid my competence here is pretty limited, advices are welcome. – Iskander Sharipov Apr 17 '17 at 09:04
  • 1
    I think you're correct, straight forward, language agnostic / generic optimisations could be possible (this might be relevant: https://github.com/dominikh/go-tools/blob/master/simple/lint.go). Still it's a challenge; (http://stackoverflow.com/questions/5180914/llvm-ir-back-to-human-readable-source-language) - it's not even trivial for LLVM IR to reverse to different targets. – Martin Gallagher Apr 17 '17 at 09:34

0 Answers0