<p>From Wikipedia:</p>
<blockquote>
<p>A <a href="https://en.wikipedia.org/wiki/Source-to-source_compiler" rel="noreferrer"><strong>source-to-source translator</strong></a>, source-to-source compiler (S2S
compiler), transcompiler, or transpiler is a type of
translator that takes the source code of a program written in a
programming language as its input and produces an equivalent source
code in the same or a different programming language. A
source-to-source translator converts between programming languages
that operate at approximately the same level of abstraction.</p>
</blockquote>
<p>An example is when you translate code with JS-preprocessors like TypeScript to JavaScript.</p>
<p>Contrast that with compiling to VM bytecode or LLVM IR, (from what I understand) when the compiler will compile the code to some basic bytecode, then, a virtual machine program will run the bytecode, and execute the program. A (huge) example is Java.</p>
<p>So what are the pros and cons of using one instead of another?</p>
<hr />
<p><sub>This question is first asked on the <a href="https://area51.stackexchange.com/proposals/127456/programming-language-design-and-implementation/127472#127472">Definition phase</a>. I got permission to use this <a href="https://chat.stackexchange.com/transcript/message/63589363#63589363">here</a>.</sub></p>

<implementation><intermediate-representation>
What are the pros and cons of transpiling to a high-level language vs compiling to VM bytecode or LLVM IR
<p>Transpiling is an excellent way to bootstrap a compiler, because you get to reuse an existing compiler.</p>
<p>There is more than one way to transpile. The way we bootstrapped the <a href="https://mercurylang.org/" rel="noreferrer">Mercury</a> compiler was to write Prolog code to transpile a subset of Mercury to <em>two</em> closely-related languages: Prolog for execution, and <a href="https://en.wikipedia.org/wiki/G%C3%B6del_(programming_language)" rel="noreferrer">Gödel</a> just to use its type checker. We then rewrote that code in the same subset of Mercury (which, being very like Prolog syntactically, wasn't much work at all), at which point we could start writing the static analysis phases to get Gödel out of the development loop.</p>
<p>One under-appreciated &quot;pro&quot; is that you essentially get a foreign function interface for free, so this can save you having to write a standard library early on. The original reference implementation for C++, called Cfront, was a transpiler to C that was also compatible with C. This C compatibility is arguably one of the reasons why C++ succeeded as well as it did.</p>
<p>There are two very large &quot;cons&quot;:</p>
<ol>
<li>Users of your compiler need another language implementation installed.</li>
<li>It often messes up the user experience for the rest of the software toolchain, such as profiling and debugging.</li>
</ol>
<p>Neither of these are a problem for TypeScript-to-JavaScript, because everyone has a JS implementation already, and tools (e.g. Chrome developer tools) are aware of transpiled languages. Programmers can't avoid knowing that there is JS underneath TS, but the experience isn't as painful as it could be.</p>

CC BY-SA 4.0
2023-05-17T03:20:17.520Z
2023-05-17T03:52:38.530Z
2023-05-17T03:52:38.530Z
Pseudonym
CC BY-SA 4.0
2023-07-13T18:18:26.787Z
Another major "con" is that one will be stuck with corner-case behavioral quirks of the target language.  Of course, the same issue applies when targeting LLVM.
supercat
<p>The main benefit of targeting a higher-level language is that it can be much easier to compile from a high-level language to another one. The more language constructs and features that are shared by the source and target language, the less work it is to translate between them. For example, when compiling a garbage-collected language to another garbage-collected language, there is no need to implement a garbage collector yourself; you get it for free. Likewise for things like dynamic dispatch or exceptions.</p>
<p>Another significant benefit is that when implementing a compiler, we need a strong understanding of the target language. If the target language is one that you have experience writing in, then you will be in a better position compared to having to learn LLVM IR, which is both extraordinarily complicated and not something you would have prior experience with from your own projects.</p>
<p>One downside of targeting a high-level language is that if there is a mismatch between the semantics of the source language and the target language, then the gap may be harder to bridge than just implementing straight into a low-level target language. For example, if your language's control-flow is difficult to express without jumps (consider e.g. labelled <code>break</code> and <code>continue</code> when targeting a language like Python which doesn't have these), then you may have to insert flag variables or apply other program transformations to achieve the correct results, when the same control-flow would be straightforward if you were targeting a bytecode or assembly language which allowed jumps.</p>
<p>Another downside, particularly when targeting a high-level compiled language, is the compiler's performance. If your ultimate goal is to compile to machine code, but you get there via a C++ or Rust compiler which uses LLVM IR itself, then your compiler is going to be slower than one which outputs LLVM IR directly.</p>

CC BY-SA 4.0
2023-05-17T03:21:15.557Z
2023-05-17T03:21:15.557Z
kaya3
# What are the pros and cons of transpiling to a high-level language vs compiling to VM bytecode or LLVM IR
Posted by: username_0 on 2023-05-17
From Wikipedia:

> A [**source-to-source translator**](https://en.wikipedia.org/wiki/Source-to-source_compiler), source-to-source compiler (S2S
> compiler), transcompiler, or transpiler is a type of
> translator that takes the source code of a program written in a
> programming language as its input and produces an equivalent source
> code in the same or a different programming language. A
> source-to-source translator converts between programming languages
> that operate at approximately the same level of abstraction.

An example is when you translate code with JS-preprocessors like TypeScript to JavaScript.

Contrast that with compiling to VM bytecode or LLVM IR, (from what I understand) when the compiler will compile the code to some basic bytecode, then, a virtual machine program will run the bytecode, and execute the program. A (huge) example is Java.

So what are the pros and cons of using one instead of another?

---

This question is first asked on the [Definition phase](https://area51.stackexchange.com/proposals/127456/programming-language-design-and-implementation/127472#127472). I got permission to use this [here](https://chat.stackexchange.com/transcript/message/63589363#63589363).
**Tags:** implementation intermediate-representation
---
## 2 answers
### Answer by username_1 on 2023-05-17. Score: 6
Transpiling is an excellent way to bootstrap a compiler, because you get to reuse an existing compiler.

There is more than one way to transpile. The way we bootstrapped the [Mercury](https://mercurylang.org/) compiler was to write Prolog code to transpile a subset of Mercury to *two* closely-related languages: Prolog for execution, and [Gödel](https://en.wikipedia.org/wiki/G%C3%B6del_(programming_language)) just to use its type checker. We then rewrote that code in the same subset of Mercury (which, being very like Prolog syntactically, wasn't much work at all), at which point we could start writing the static analysis phases to get Gödel out of the development loop.

One under-appreciated "pro" is that you essentially get a foreign function interface for free, so this can save you having to write a standard library early on. The original reference implementation for C++, called Cfront, was a transpiler to C that was also compatible with C. This C compatibility is arguably one of the reasons why C++ succeeded as well as it did.

There are two very large "cons":

1. Users of your compiler need another language implementation installed.
2. It often messes up the user experience for the rest of the software toolchain, such as profiling and debugging.

Neither of these are a problem for TypeScript-to-JavaScript, because everyone has a JS implementation already, and tools (e.g. Chrome developer tools) are aware of transpiled languages. Programmers can't avoid knowing that there is JS underneath TS, but the experience isn't as painful as it could be.
> **Comments:**
> **username_2** (2023-07-13): Another major "con" is that one will be stuck with corner-case behavioral quirks of the target language.  Of course, the same issue applies when targeting LLVM.  
---
### Answer by username_3 on 2023-05-17. Score: 5
The main benefit of targeting a higher-level language is that it can be much easier to compile from a high-level language to another one. The more language constructs and features that are shared by the source and target language, the less work it is to translate between them. For example, when compiling a garbage-collected language to another garbage-collected language, there is no need to implement a garbage collector yourself; you get it for free. Likewise for things like dynamic dispatch or exceptions.

Another significant benefit is that when implementing a compiler, we need a strong understanding of the target language. If the target language is one that you have experience writing in, then you will be in a better position compared to having to learn LLVM IR, which is both extraordinarily complicated and not something you would have prior experience with from your own projects.

One downside of targeting a high-level language is that if there is a mismatch between the semantics of the source language and the target language, then the gap may be harder to bridge than just implementing straight into a low-level target language. For example, if your language's control-flow is difficult to express without jumps (consider e.g. labelled `break` and `continue` when targeting a language like Python which doesn't have these), then you may have to insert flag variables or apply other program transformations to achieve the correct results, when the same control-flow would be straightforward if you were targeting a bytecode or assembly language which allowed jumps.

Another downside, particularly when targeting a high-level compiled language, is the compiler's performance. If your ultimate goal is to compile to machine code, but you get there via a C++ or Rust compiler which uses LLVM IR itself, then your compiler is going to be slower than one which outputs LLVM IR directly.
---
