5 ms·
Isn't a compiler supposed to produce one or more of: machine code, link-able segments of machine code, or at least 'bytecode' for a 'virtual' machine? The defi
by mjevans 5y ago
Isn't a compiler supposed to produce one or more of: machine code, link-able segments of machine code, or at least 'bytecode' for a 'virtual' machine?
The definition of this problem strikes me far more as either a transpiler or a pre-processor for a Domain Specific Language.
- thewakalix 5y ago“Compiling to C” is an established strategy; for instance, GHC used to compile to C--, a subset of C. This doesn’t seem wrong to me; after all, assembly is already an abstraction layer over machine code, and outputting assembly would hardly be “uncompilerish” behavior. I suppose it depends on whether you view C as a low-level language.
- anonymfus 5y ago> GHC used to compile to C--, a subset of C. C-- is not a subset of C.
- wccrawford 5y agohttps://en.wikipedia.org/wiki/C-- https://en.wikipedia.org/wiki/C-- > The name of the language is an in-joke, indicating that C-- is a reduced form of C, in the same way that C++ is basically an expanded form of C. ("--" and "++" mean "decrement" and "increment".)
- BenjiWiebe 5y agoQ&A #5 of https://www.cs.tufts.edu/~nr/c--/faq.html https://www.cs.tufts.edu/~nr/c--/faq.html explains why C-- is not a subset of C.
- DonHopkins 5y ago“C++++-= is the new language that is a little more than C++ and a lot less.” -Bill Joy Bill Joy’s Law: 2^(Year-1984) Million Instructions per Second https://donhopkins.medium.com/bill-joys-law-2-year-1984-million-instructions-per-second-3a8c92165cfe https://donhopkins.medium.com/bill-joys-law-2-year-1984-mill...
- thewakalix 5y agoYou’re right, and as it happens, GHC compiles first to Cmm and then (as one half-deprecated option) to C.
- DonHopkins 5y agoC combines the power of assembly language with the readability and maintainability of assembly language.
- pajko 5y agoA compiler compiles something to something. For example, gcc compiles C code to assembly, which is then compiled to machine code by gas. Clang does the same: https://freecompilercamp.org/clang-basics/ https://freecompilercamp.org/clang-basics/
- user-the-name 5y agoClang definitely does not output assembly code and invoke an assembler. It generates machine code directly. It can optionally also generate assembly code if you want to look at it, but it does not do this during normal compilation.
- misnome 5y agoClang doesn't generate LLVM IR as part of it's normal process?
- user-the-name 5y agoNot in a text file, no. It generates LLVM data structures that can be serialised to a text format, but aren't. The only external binary clang invokes is, optionally, the linker. Otherwise, everything is done in one executable invocation without temporary files.
- aetherspawn 5y agoYes and no. It uses the llvm API rather than pipe to llc
- pajko 5y agoNot saving everything to files and not doing things via multiple executables does not mean that a compiler does things in a single step, generating machine code directly from the source code. Both gcc and clang support the -save-temps switch to keep intermediate files during the compilation process. These files are created even without this argument, except that their names are randomly generated somewhere and the files are deleted afterwards.
- TheMatten 5y agoAdding to thewakalix's answer, many compilers use LLVM as a backend, where LLVM's IR is higher-level-than-bytecode language too. So that would mean that e.g Clang or current GHC wouldn't qualify either.
- user-the-name 5y agoLLVM is a static library that you link. Clang does not generate LLVM IR text files, it invokes LLVM's code generator directly.
- Al-Khwarizmi 5y agoIn classic compiler theory, the term "transpiler" doesn't even exist, they're all compilers.
- 411111111111111 5y agoThey literally didn't exist back then though. so not just the term was unknown back then, the practice itself too...
- KMnO4 5y agoApparently Intel shipped a transpiler for 8080 to 8086 source code in 1978. https://en.wikipedia.org/wiki/Source-to-source_compiler#Intel_CONV86 https://en.wikipedia.org/wiki/Source-to-source_compiler#Inte...
- cout 5y agoLikewise, the first C++ compiler (cfront) might today be considered a transpiler.
- nextaccountic 5y agoA transpiler is a compiler
- Banana699 5y agoThere is no fundamental difference between compilers, code formatters, preprocessors or transpilers. Anything that takes a program and outputs another program related to it by some semantics is a compiler. That said though, I do agree that calling an assembler like this (brainfuck is a kind of assembly) a compiler stretches things a bit, for an entirely different reason: usually there is some sort of a complexity threshold of the input language. The compiler has to at least maintain a symbol table for named entities (traditional assemblies has variable-like entities, macros and subroutines, the assembler has to keep track of all that). Brainfuck is completely linear, with no named entities at all. The "compiler" looks like a dumb string processor, just iterates over one buffer to transform it into another buffer, and doesn't maintain any sort of structures on the code being translated. By the strict "compiler : program->program" definition, this doesn't matter. But my intuition holds that dumb string processing is a bit short of "true" compilation.
- xg15 5y ago> There is no fundamental difference between compilers, code formatters, preprocessors or transpilers. Anything that takes a program and outputs another program related to it by some semantics is a compiler. That is arguing semantics and, while not wrong, I think it muddies the waters: By that logic, sed and awk are compilers. In the end, only machine code can be executed, so you'll need something at the end of your chain that produces machine code or at least executes your DSL in an interpreter loop. So I think a distinction between "compilers" that generate machine code and "compilers" that don't is worthwhile.
- dgb23 5y agoWhere do you draw the line? Is javac not a compiler?
- bidirectional 5y agoMost researchers I've spoken to loathe the term transpiler.
- TazeTSchnitzel 5y agoPlenty of compilers target relatively high-level languages. They're no less compilers for it; “transpiler” feels fairly arbitrary to me. With a bit of effort and the right add-on (https://github.com/JuliaComputing/llvm-cbe https://github.com/JuliaComputing/llvm-cbe) you can make any LLVM-based compiler produce C code!