Source-to-source compiler
Program execution |
---|
General concepts |
Compilation strategies |
Notable runtimes |
|
Notable compilers & toolchains |
|
Data transformation |
---|
Concepts |
Transformation languages |
Techniques and transforms |
Applications |
Related |
A source-to-source compiler, transcompiler or transpiler is a type of compiler that takes the source code of a program written in one programming language as its input and produces the equivalent source code in another programming language. A source-to-source compiler translates between programming languages that operate at approximately the same level of abstraction, while a traditional compiler translates from a higher level programming language to a lower level programming language. For example, a source-to-source compiler may perform a translation of a program from Pascal to C. An automatic parallelizing compiler will frequently take in a high level language program as an input and then transform the code and annotate it with parallel code annotations (e.g., OpenMP) or language constructs (e.g. Fortran's forall
statements).[1]
Another purpose of source-to-source-compiling is translating legacy code to use the next version of the underlying programming language or an API that breaks backward compatibility. It will perform automatic code refactoring which is useful when the programs to refactor are outside the control of the original implementer (for example, converting programs from Python 2 to Python 3, or converting programs from an old API to the new API) or when the size of the program makes it impractical or time consuming to refactor it by hand.
Transcompilers may either keep translated code as close to the source code as possible to ease development and debugging of the original source code, or may change the structure of the original code so much that the translated code does not look like the source code.[2] There are also debugging utilities that map the transpiled source code back to the original code; for example, the JavaScript Source Map[3] standard allows mapping of the JavaScript code executed by a web browser back to the original source in a transpiled-to-JavaScript language.[4]
Examples of transcompiled languages include Closure Compiler, Coccinelle, CoffeeScript, Dart, Haxe, Nim, TypeScript[5] and Emscripten.[6]
History
One of the earliest programs of this kind was Digital Research's XLT86 in 1981, a program written by Gary Kildall, which translated .ASM source code for the Intel 8080 processor into .A86 source code for the Intel 8086. Using global data flow analysis on 8080 register usage, the translator would also optimize the output for code size and take care of calling conventions, so that CP/M-80 and MP/M-80 programs could be ported to the CP/M-86 and MP/M-86 platforms automatically. XLT86 itself was written in PL/I-80 and was available for CP/M-80 platforms as well as for DEC VMS (for VAX 11/750 or 11/780).[7]
A similar, but much less sophisticated program was TRANS.COM, written by Tim Paterson in 1980 as part of 86-DOS. It could translate some Z80 assembly source code into .ASM source code for the 8086, but supported only a subset of opcodes, registers and modes, often still requiring significant manual correction and rework afterwards. Also it did not carry out any register and jump optimizations.[8][9]
Programming language implementations
The first implementations of some programming languages started as transcompilers, and the default implementation for some of those languages are still transcompilers. In addition to the table below, a CoffeeScript maintainer provides a list of languages that compile to JavaScript.[10]
Source language | Target language | Comment |
---|---|---|
BASIC | C | Transcompiled using BaCon[11] |
BCX | C | |
C | JavaScript | Emscripten |
C++ (originally known as "C with classes") | C | The cfront transcompiler was doing the conversion |
Chapel | C | |
ClojureScript | JavaScript | |
COBOL | C | Transcompiled using OpenCOBOL |
COBOL | Java | Transcompiled using P3COBOL |
CoffeeScript | JavaScript | |
C# | JavaScript | Transcompiled using ScriptSharp[12] |
Cython | C | The resulting code is intended to be compiled to a module for the Python interpreter CPython |
Dart | JavaScript | |
Delphi Web Script / Object Pascal | JavaScript | Transcompiled using Smart Mobile Studio[13] |
ECMAScript 6 | JavaScript | Transcompiled using Babel[14] or traceur-compiler[15] |
Eiffel | C | |
Go | JavaScript | GopherJS |
Groovy | JavaScript | grooscript |
Haskell | JavaScript | Transcompiled using GHCJS |
Haxe | JavaScript, PHP, C++, C#, Python, Java, Lua, and ActionScript | |
Java | Objective-C | J2ObjC[16] |
Java | JavaScript | grooscript, GWT, JSweet, ST-JS |
Java | TypeScript | JSweet |
Java | C | java2c-transcompiler[17] |
JavaScript and/or QML | C++ | QtQuickCompiler |
Julia | C | Julia generates native code and works well without C (or with), but a modified implementation of the Julia's implementation can compile individual functions or whole programs to C.[18][19] |
LESS | CSS | |
Lisaac | C | |
Mirah | Java | |
MoonScript [20] | Lua | |
Nim | C, C++, Objective-C, JavaScript | |
OCaml bytecode | JavaScript | Transcompiled using js_of_ocaml from the Ocsigen project |
PHP | C++ | Transcompiled using HipHop for PHP |
Python | JavaScript | Brython, PyPyJs, Rapydscript, Skulpt, Transcrypt |
Python | C++ | Transcompiled using Nuitka and ShedSkin[21] |
Python | Go | Grumpy |
Ratfor | Fortran | |
Ruby | JavaScript | Opal |
Sass[22] | CSS | |
Scheme | C | Stalin compiler, Chicken Scheme |
TypeScript | JavaScript | |
Vala | C, with additional libraries such as GObject | |
X10 | C++ and Java | |
Xtend[23] | Java |
Porting a codebase
When developers want to switch to a different language while retaining most of an existing codebase, it might be better to use a transcompiler compared to rewriting the whole software by hand. In this case, the code often needs manual correction because the automated translation might not work in all cases.
Tool | Source language | Target language | Comments |
---|---|---|---|
2to3 script | Python 2 | Python 3 | Even though 2to3 does its best at automating the translation process, further manual corrections are often needed. |
Emscripten | LLVM bytecode | JavaScript | This allows running C/C++ codebases in a browser for example |
Google Web Toolkit | Java program that uses a specific API | JavaScript | The Java code is a little bit constrained compared to normal Java code. |
Js_of_ocaml[24] of Ocsigen | OCaml | JavaScript | |
J2Eif[25] | Java | Eiffel | The resulting Eiffel code has classes and structures similar to the Java program but following Eiffel syntax and conventions. |
C2Eif[26] | C | Eiffel | The resulting Eiffel code has classes and structures that try to be as clean as possible. The tool is complete and relies on embedding the C and assembly code if it cannot translate it properly. |
Transcompiler pipelines
A transcompiler pipeline is what results from recursive transcompiling. By stringing together multiple layers of tech, with a transcompile step between each layer, technology can be repeatedly transformed, effectively creating a distributed language independent specification.
Xslt is a general purpose transform tool which can be used between many different technologies, to create such a derivative code pipeline.
See also
- C to HDL
- DMS Software Reengineering Toolkit – a source-to-source compiler framework using explicit pattern-directed rewrite rules
- Language interoperability
- Binary recompiler
- Code generation (compiler)
- Program transformation
- ROSE compiler framework – a source-to-source compiler framework
- Compiler-compiler
- Translation (computing)
- Language binding
- Language-independent specification
- Monkey (programming language)
- Intermediate language
- Recursive Transcompiling
- Xml & XSLT
References
- ↑ "Types of compilers". compilers.net. 1997–2005. Retrieved 28 October 2010.
- ↑ Fowler, Martin (February 12, 2013). "Transparent Compilation". Retrieved February 13, 2013.
- ↑ Lenz, Fitzgerald (11 February 2011). "Source Map Revision 3 Proposal".
- ↑ Seddon, Ryan (21 March 2012). "Introduction to JavaScript Source Maps". html5rocks.com. Retrieved 21 January 2015.
- ↑ Henson, Valerie (January 20, 2009). "Semantic patching with Coccinelle". lwn.net. Retrieved 28 October 2010.
- ↑ Epic Games; Mozilla. "HTML5 Epic Citadel".
- ↑ Digital Research (1981). XLT86 - 8080 to 8086 Assembly Language Translator - User's Guide (PDF). Pacific Grove, CA, USA: Digital Research Inc. Archived (PDF) from the original on 2016-11-18. Retrieved 2016-11-18.
- ↑ Seattle Computer Products (1980): 86-DOS - Disk Operating System for the 8086. User's manual, version 0.3 - Preliminary. Seattle Computer Products, Seattle ().
- ↑ Paterson, Tim (2013-12-19) [1982]. "Microsoft DOS V1.1 and V2.0: Z80 to 8086 Translator version 2.21 /msdos/v11source/TRANS.ASM". Computer History Museum, Microsoft. Retrieved 2014-03-25. (NB. While the publishers claim this would be MS-DOS 1.1 and 2.0, it actually is SCP MS-DOS 1.25 and TeleVideo PC DOS 2.11.)
- ↑ "List of languages that compile to JS". Retrieved December 15, 2014.
- ↑ Peter van Eerten. "BaCon - A free BAsic CONverter for Unix, BSD and MacOSX". Basic-converter.org. Retrieved 2014-07-08.
- ↑ "Script# by nikhilk". Scriptsharp.com. Retrieved 2013-08-02.
- ↑ "Smart Mobile Studio". SmartMobileStudio.com. Retrieved 2014-03-09.
- ↑ "Babel · The compiler for writing next generation JavaScript". babeljs.io. Retrieved 2016-04-10.
- ↑ "Traceur is a JavaScript.next-to-JavaScript-of-today compiler". github.com. Retrieved 2014-07-02.
- ↑ "j2objc - Java to iOS Objective-C translation tool and runtime.". j2objc.org. 2014-02-13. Retrieved 2015-08-18.
- ↑ "java2c-transcompiler - A simple source-to-source from Java to C - Google Project Hosting". Retrieved 8 October 2014.
- ↑ "IntelLabs/julia". GitHub.
- ↑ "Google Groups". google.com.
- ↑ "MoonScript, a language that compiles to Lua". Retrieved 2016-09-21.
- ↑ "Shed Skin, An experimental (restricted-Python)-to-C++ compiler". Retrieved 2014-10-01.
- ↑ Maptastic Maple (3.3.9). "Sass: Syntactically Awesome Style Sheets". Sass-lang.com. Retrieved 2014-07-08.
- ↑ "Xtend, modernized Java". Eclipse project. Retrieved 2014-10-01.
- ↑ "Js_of_ocaml". Retrieved 8 October 2014.
- ↑ "J2Eif Research Page - Chair of Software Engineering". Se.inf.ethz.ch. doi:10.1007/978-3-642-21952-8_4. Retrieved 2014-07-08.
- ↑ "C2Eif Research Page - Chair of Software Engineering". Se.inf.ethz.ch. Retrieved 2014-07-08.