Source-to-source compiler

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Template:Short description Script error: No such module "Distinguish". Template:Use dmy dates Template:Use list-defined references Template:Program execution Template:Data transformation A source-to-source translator, source-to-source compiler (S2S compiler), transcompiler, or transpiler[1][2][3] 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, usually as an intermediate representation. A source-to-source translator converts between programming languages that operate at approximately the same level of abstraction, while a traditional compiler translates from a higher level language to a lower level language. For example, a source-to-source translator may perform a translation of a program from Python to JavaScript, while a traditional compiler translates from a language like C to assembly or Java to bytecode.[4] 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).[2][5]

Another purpose of source-to-source-compiling is translating legacy code to use the next version of the underlying programming language or an application programming interface (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 structure 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.[6] There are also debugging utilities that map the transcompiled source code back to the original code; for example, the JavaScript Source Map standardScript error: No such module "Unsubst". allows mapping of the JavaScript code executed by a web browser back to the original source when the JavaScript code was, for example, minified or produced by a transcompiled-to-JavaScript language.Script error: No such module "Unsubst".

Examples include Closure Compiler, CoffeeScript, Dart, Haxe, Opal, TypeScript and Emscripten.[7]

Script error: No such module "anchor".Assembly language translators

So called Assembly language translators are a class of source-to-source translators converting code from one assembly language into another, including (but not limited to) across different processor families and system platforms.

Intel CONV86

Script error: No such module "anchor". Intel marketed their 16-bit processor 8086 to be source compatible to the 8080, an 8-bit processor.[8] To support this, Intel had an ISIS-II-based translator from 8080 to 8086 source code named CONV86[9][10][11][12] (also referred to as CONV-86[13] and CONVERT 86[14][15]) available to OEM customers since 1978, possibly the earliest program of this kind.[nb 1] It supported multiple levels of translation and ran at 2 MHz on an Intel Microprocessor Development System MDS-800 with 8-inch floppy drives. According to user reports, it did not work very reliably.[16][17]

SCP TRANS86

Script error: No such module "anchor".Seattle Computer Products (SCP) offered TRANS86.COM,[15][18][19] written by Tim Paterson in 1980 while developing 86-DOS.[20][21][22] The utility could translate Intel 8080 and Zilog Z80 assembly source code (with Zilog/Mostek mnemonics) into Template:Not a typo source code for the Intel 8086 (in a format only compatible with SCP's cross-assembler ASM86 for CP/M-80), but supported only a subset of opcodes, registers and modes, and often still required significant manual correction and rework afterwards.[23][20] Also, performing only a mere transliteration,[14][18][9][10] the brute-force single-pass translator did not carry out any register and jump optimizations.[24][25] It took about 24 KB of RAM.[15] The SCP version 1 of TRANS86.COM ran on Z80-based systems.[15][18] Once 86-DOS was running, Paterson, in a self-hosting-inspired approach, utilized TRANS86 to convert itself into a program running under 86-DOS.[22][18] Numbered version 2, this was named TRANS.COM instead.[18][25][24][26][27] Later in 1982, the translator was apparently also available from Microsoft.[15][28]

Sorcim TRANS86

Script error: No such module "anchor".Also named TRANS86, Sorcim offered an 8080 to 8086 translator as well since December 1980.[29][14] Like SCP's program it was designed to port CP/M-80 application code (in ASM, MAC, RMAC or ACT80 assembly format) to MS-DOS (in a format compatible with ACT86).[29][15][30][31] In ACT80 format it also supported a few Z80 mnemonics. The translation occurred on an instruction-by-instruction basis with some optimization applied to conditional jumps. The program ran under CP/M-80, MP/M-80 and Cromemco DOS with a minimum of 24 KB of RAM, and had no restrictions on the source file size.[15][32]

Digital Research XLT86

Script error: No such module "anchor".Much more sophisticated and the first to introduce optimizing compiler technologies into the source translation process was Digital Research's XLT86 1.0 in September 1981. XLT86 1.1 was available by April 1982.[33] The program was written by Gary Kildall[14][34][35][36] and translated Template:Not a typo source code for the Intel 8080 processor (in a format compatible with ASM, MAC or RMAC assemblers) into Template:Not a typo source code for the 8086 (compatible with ASM86). Using global data flow analysis on 8080 register usage,[37][14][38][39] the five-phase multi-pass translator would also optimize the output for code size and take care of calling conventions (CP/M-80 BDOS calls were mapped into BDOS calls for CP/M-86), 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.COM itself was written in PL/I-80 for CP/M-80 platforms.[40][15][33][41] The program occupied 30 KB of RAM for itself plus additional memory for the program graph. On a 64 KB memory system, the maximum source file size supported was about 6 KB,[40][15][42][33] so that larger files had to be broken down accordingly before translation.[15][33] Alternatively, XLT86 was also available for DEC VAX/VMS.[15][33] Although XLT86's input and output worked on source-code level, the translator's in-memory representation of the program and the applied code optimizing technologies set the foundation to binary recompilation.[43][44][45]

Others

Script error: No such module "anchor".2500 AD Software offered an 8080 to 8086 source-code translator as part of their XASM suite for CP/M-80 machines with Z80 as well as for Zilog ZEUS and Olivetti PCOS systems.[46]

Script error: No such module "anchor".Since 1979, Zilog offered a Z80 to Z8000 translator as part of their PDS 8000 development system.[47][48][49][50][51][17] Advanced Micro Computers (AMC)[51][17] and 2500 AD Software offered Z80 to Z8000 translators as well.[46] The latter was named TRANS[52][53] and was available for Z80 CP/M, CP/M-86, MS-DOS and PCOS.[46]

Script error: No such module "anchor".The Z88DK development kit provides a Z80 to i486 source code translator targeting nasm named "to86.awk", written in 2008 by Stefano Bodrato.[54] It is in turn based on an 8080 to Z80 converter written in 2003 by Douglas Beattie, Jr., named "toz80.awk".[54]

Script error: No such module "anchor".In 2021, Brian Callahan wrote an 8080 CP/M 2.2 to MS-DOS source code translator targeting nasm named 8088ify.[55]

Programming language implementations

Script error: No such module "Unsubst". 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.[56]

List of transcompilers[4]
Name Source language Target language Comments
Babel ECMAScript 6+ (JavaScript) ES5
Cerberus X Cerberus JavaScript, Java, C++, C#
Cfront C++ C
ClojureScript Clojure JavaScript
CoffeeScript CoffeeScript JavaScript
Dafny Dafny C#, JavaScript, Java, C++, Go, Python
Dart Dart JavaScript
h5[57] C# JavaScript
Eiffel, via EiffelStudio Eiffel C, Common Intermediate Language
Elm Elm JavaScript
Haxe Haxe ActionScript 3, JavaScript, Java, C++, C#, PHP, Python, Lua
HipHop for PHP (HPHPc) PHP C++
J2ObjC[58] Java Objective-C
JSweet[59] Java TypeScript
Maia[60] Maia Verilog
NACA COBOL, Java COBOL, Java
mrustc Rust C Experimental compiler able to bootstrap official Rust compiler (rustc)
Nim Nim C, C++, Objective-C, JavaScript
PureScript PureScript JavaScript
ReasonML Reason JavaScript
ReScript OCaml JavaScript
Sather Sather C
Scala.js Scala JavaScript
Swiftify[61] Objective-C Swift
V V C
Vala Vala C
Visual Eiffel Eiffel C
Fable F# JavaScript
Fable Python F# Python

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. Depending on the quality of the transcompiler, the code may or may not need manual intervention in order to work properly. This is different from "transcompiled languages" where the specifications demand that the output source code always works without modification. All transcompilers used to port a codebase will expect manual adjustment of the output source code if there is a need to achieve maximum code quality in terms of readability and platform convention.

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
c2go[62] C Go Before the 1.5 release, the Go compiler was written in C. An automatic translator was developed to automatically convert the compiler codebase from C into Go.[63][64] Since Go 1.5, the "compiler and runtime are now implemented in Go and assembler, without C".
C2Rust[65] C Rust C2Rust takes C code as input and outputs unsafe Rust code, focusing on preserving compatibility with the original codebase. Several documented limitations exist for this process. Converting the resulting code to safe and idiomatic Rust code is a manual effort post translation, although an automated tool exists to ease this task.[65]
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[66] of Ocsigen OCaml JavaScript
J2Eif[67] Java Eiffel The resulting Eiffel code has classes and structures similar to the Java program but following Eiffel syntax and conventions.
C2Eif[68] 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.
Skip[69] Swift Kotlin Skip is an Xcode plug-in that transpiles a Swift iOS app or library using SwiftUI into equivalent native Kotlin code for Android using Jetpack Compose.
Swiftify[70] Objective-C Swift Swiftify is an online source to source conversion tool from Objective-C into Swift. It assists developers who are migrating all or part of their iOS codebase into Swift. The conversion is aimed primarily at converting the syntax between Objective-C and Swift, and is helped because Apple took efforts to ensure compatibility between Swift and Objective-C runtimes.
Runtime Converter[71] PHP Java The Runtime Converter is an automatic tool which converts PHP source code into Java source code. There is a Java runtime library for certain features of the PHP language, as well as the ability to call into the PHP binary itself using JNI for PHP standard library and extension function calls.

Transcompiler pipelines

Script error: No such module "Unsubst". 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 that can be used between many different technologies, to create such a derivative code pipeline.[72]

Recursive transcompiling

Recursive transcompilation (or recursive transpiling) is the process of applying the notion of transcompiling recursively, to create a pipeline of transformations (often starting from a single source of truth) which repeatedly turn one technology into another.

By repeating this process, one can turn A → B → C → D → E → F and then back into Template:Not a typo. Some information will be preserved through this pipeline, from A → Template:Not a typo, and that information (at an abstract level) demonstrates what each of the components A–F agree on.

In each of the different versions that the transcompiler pipeline produces, that information is preserved. It might take on many different shapes and sizes, but by the time it comes back to Template:Not a typo, having been transcompiled six times in the pipeline above, the information returns to its original state.

This information which survives the transform through each format, from Template:Not a typo, is (by definition) derivative content or derivative code.

Recursive transcompilation takes advantage of the fact that 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 else they may change the structure of the original code so much, that the translated code does not look like the source code. There are also debugging utilities that map the transcompiled source code back to the original code; for example, JavaScript source maps allow mapping of the JavaScript code executed by a web browser back to the original source in a transcompiled-to-JavaScript language.

See also

Script error: No such module "Portal".

Template:Div col

Template:Div col end

Notes

Template:Reflist

References

Template:Reflist

Further reading

  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1". 1984-11-11 version 1.05 (NB. The DOS executable XLT86.COM [12 KB] translates Intel 8080 assembly language source code to Intel 8086 assembly language source code. Despite its name this implementation in 8086 assembly is not related to Digital Research's earlier and much more sophisticated XLT86.)
  • Script error: No such module "anchor".Script error: No such module "citation/CS1". [1] [2]
  • Template:Cite magazine and Template:Cite magazine, also available as Script error: No such module "citation/CS1". Template:Usurped
  • Script error: No such module "anchor".Script error: No such module "citation/CS1". (9 pages) (NB. This software translator was developed by ST and translates Motorola 6805/HC05 assembly source code in 2500AD Software format into ST7 source code. The MIGR2ST7.EXE executable for Windows is available from "MCU ON CD".)
  • Script error: No such module "citation/CS1".

External links

  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  1. Cite error: Invalid <ref> tag; no text was provided for refs named ARC_1988_Transpiler
  2. a b Cite error: Invalid <ref> tag; no text was provided for refs named Pountain_1989
  3. Cite error: Invalid <ref> tag; no text was provided for refs named Sector7_1993_Transpiler
  4. a b Cite error: Invalid <ref> tag; no text was provided for refs named Devopedia_Transpiler
  5. Cite error: Invalid <ref> tag; no text was provided for refs named Compiler_Types
  6. Cite error: Invalid <ref> tag; no text was provided for refs named Fowler_2013
  7. Cite error: Invalid <ref> tag; no text was provided for refs named Epic_Games
  8. Cite error: Invalid <ref> tag; no text was provided for refs named Scanlon_1988
  9. a b Cite error: Invalid <ref> tag; no text was provided for refs named Intel_1978_CONV86
  10. a b Cite error: Invalid <ref> tag; no text was provided for refs named Intel_1979_CONV86
  11. Cite error: Invalid <ref> tag; no text was provided for refs named Intel_1983_ASM86
  12. Cite error: Invalid <ref> tag; no text was provided for refs named Nelson_1988
  13. Cite error: Invalid <ref> tag; no text was provided for refs named Intel_1979_Family
  14. a b c d e Cite error: Invalid <ref> tag; no text was provided for refs named Freiberger_1981_Translators
  15. a b c d e f g h i j k Cite error: Invalid <ref> tag; no text was provided for refs named Taylor_1982_Translators
  16. Cite error: Invalid <ref> tag; no text was provided for refs named Guzis_2013_ISIS
  17. a b c Cite error: Invalid <ref> tag; no text was provided for refs named Guzis_2016_Translators
  18. a b c d e Cite error: Invalid <ref> tag; no text was provided for refs named BYTE_2018_MASM
  19. Cite error: Invalid <ref> tag; no text was provided for refs named Paterson_1983
  20. a b Cite error: Invalid <ref> tag; no text was provided for refs named BYTE_1980_86-DOS
  21. Cite error: Invalid <ref> tag; no text was provided for refs named Paterson_1994_Origins_DOS
  22. a b Cite error: Invalid <ref> tag; no text was provided for refs named Paterson_2007_Design-DOS
  23. Cite error: Invalid <ref> tag; no text was provided for refs named SCP_Cross
  24. a b Cite error: Invalid <ref> tag; no text was provided for refs named SCP_1980_86-DOS
  25. a b Cite error: Invalid <ref> tag; no text was provided for refs named Paterson_2014_MSDOS125
  26. Cite error: Invalid <ref> tag; no text was provided for refs named SCP_86-DOS
  27. Cite error: Invalid <ref> tag; no text was provided for refs named SCP_TRANS
  28. Cite error: Invalid <ref> tag; no text was provided for refs named Hughes_1982_MS-DOS
  29. a b Cite error: Invalid <ref> tag; no text was provided for refs named Garetz_1980_Sorcim
  30. Cite error: Invalid <ref> tag; no text was provided for refs named Blumenfeld_1982
  31. Cite error: Invalid <ref> tag; no text was provided for refs named CompuPro_1982
  32. Cite error: Invalid <ref> tag; no text was provided for refs named Warren_1982_Sorcim
  33. a b c d e Cite error: Invalid <ref> tag; no text was provided for refs named Barry_1982_XLT86
  34. Cite error: Invalid <ref> tag; no text was provided for refs named Swaine_1982_CPM
  35. Cite error: Invalid <ref> tag; no text was provided for refs named Bunnell_1982_CPM
  36. Cite error: Invalid <ref> tag; no text was provided for refs named Laws_2014_IEEE
  37. Cite error: Invalid <ref> tag; no text was provided for refs named Digital_Research_1981_News
  38. Cite error: Invalid <ref> tag; no text was provided for refs named Kildall_1973_Optimization
  39. Cite error: Invalid <ref> tag; no text was provided for refs named Kildall_1972_Optimization
  40. a b Cite error: Invalid <ref> tag; no text was provided for refs named XLT86_1981
  41. Cite error: Invalid <ref> tag; no text was provided for refs named Digital_Research_XLT86
  42. Cite error: Invalid <ref> tag; no text was provided for refs named Goldfarb_1982
  43. Cite error: Invalid <ref> tag; no text was provided for refs named Wharton_1994
  44. Cite error: Invalid <ref> tag; no text was provided for refs named SPA_1995
  45. Cite error: Invalid <ref> tag; no text was provided for refs named Swaine_1997
  46. a b c Cite error: Invalid <ref> tag; no text was provided for refs named ADSoft_1984_XASM
  47. Cite error: Invalid <ref> tag; no text was provided for refs named CW_1979_Zilog
  48. Cite error: Invalid <ref> tag; no text was provided for refs named Orlansky_1979_Z8000
  49. Cite error: Invalid <ref> tag; no text was provided for refs named Zilog_1980_Brief
  50. Cite error: Invalid <ref> tag; no text was provided for refs named Thomas_1981_CPM
  51. a b Cite error: Invalid <ref> tag; no text was provided for refs named Guzis_2009_AMC
  52. Cite error: Invalid <ref> tag; no text was provided for refs named ADSoft_1982_TRANS-206b
  53. Cite error: Invalid <ref> tag; no text was provided for refs named ADSoft_TRANS-206e
  54. a b Cite error: Invalid <ref> tag; no text was provided for refs named Bodrato_2008
  55. Cite error: Invalid <ref> tag; no text was provided for refs named Callahan_2021
  56. Cite error: Invalid <ref> tag; no text was provided for refs named Coffeescript
  57. Cite error: Invalid <ref> tag; no text was provided for refs named Olivenbaum
  58. Cite error: Invalid <ref> tag; no text was provided for refs named J2ObjC
  59. Cite error: Invalid <ref> tag; no text was provided for refs named JSweet
  60. Cite error: Invalid <ref> tag; no text was provided for refs named Maia_2020
  61. Cite error: Invalid <ref> tag; no text was provided for refs named Swiftify
  62. Cite error: Invalid <ref> tag; no text was provided for refs named C2GO
  63. Cite error: Invalid <ref> tag; no text was provided for refs named Go_ReleaseNotes
  64. Cite error: Invalid <ref> tag; no text was provided for refs named Cox_Go
  65. a b Cite error: Invalid <ref> tag; no text was provided for refs named C2Rust
  66. Cite error: Invalid <ref> tag; no text was provided for refs named Ocam1
  67. Cite error: Invalid <ref> tag; no text was provided for refs named J2EIF
  68. Cite error: Invalid <ref> tag; no text was provided for refs named C2EIF
  69. Cite error: Invalid <ref> tag; no text was provided for refs named Skip
  70. Cite error: Invalid <ref> tag; no text was provided for refs named Swiftify_Objective-C
  71. Cite error: Invalid <ref> tag; no text was provided for refs named RuntimeConverter
  72. World Wide Web Consortium (W3C). "XSL Transformations (XSLT) Version 2.0". https://www.w3.org/TR/xslt-20/


Cite error: <ref> tags exist for a group named "nb", but no corresponding <references group="nb"/> tag was found