views:

17

answers:

2

when a compiler compile a high-level language into the target language that are executable , what form is the target language in ?

Is it a lower-level language like machine code ? Or the compiler just translate it to the OS's functions from the OS's API and the OS do all the work under the hood?

+2  A: 

Compilers can have a number of different outputs:

  1. Machine code that runs directly on the computer
  2. Intermediate code that is translated to machine code on the fly, and
  3. Source code for an assembler

The advantage of option 2 is that it allows the output to be portable to different computers, as long as you have available an appropriate intermediate-code to native machine code translator available for the target machine. This is how Java is able to "write once, run anywhere."

Robert Harvey
then what is the relationship between the hight-level language and the OS's API? My assumption is that the JVM is implemented using different OS's API , thus all the .class code have to use the OS's API in the end . but for c and c++ they are compiled directly into machine code or Intermediate code? is that the case? exactly what is the native machine code?
dannynjust
Calls to the operating system are made by writing code in the language being compiled to call functions in the OS. The compiler then creates machine code that calls these functions directly. C and C++ just happen to be languages that are used a lot to write operating systems.
Robert Harvey
Native machine code is code that can be executed directly by the microprocessor in the computer. Intermediate code goes through a translator (called a Just-In-Time Compiler) which translates the intermediate language into native machine code, which is then executable by the microprocessor.
Robert Harvey
thanks , the answer is really 漂亮(beautiful)。
dannynjust
+1  A: 

A compiler is any program that transforms a program from one representation into another representation. That target representation can be anything, provided that it is at least as computationally powerful as the source representation. In particular, this means that if the source representation is Turing-complete, the target representation must also be Turing-complete.

A compiler can compile from a high-level language to another high-level language (e.g. GWT, which compiles Java to ECMAScript), from a high-level language to a low-level language (e.g. Gambit, which compiles Scheme to C), from a high-level language to machine code (e.g. GCJ, which compiles Java to native code), from a low-level language to a high-level language (e.g. Clue, which compiles C to Java, Lua, Perl, ECMAScript and Common Lisp), from a low-level language to another low-level language (e.g. the Android SDK, which compiles JVML bytecode to Dalvik bytecode), from a low-level language to machine code (e.g. the C1X compiler which is part of HotSpot, which compiles JVML bytecode to machine code), machine code to a high-level language (any so-called "decompiler"), machine code to low-level language (e.g. the JIT compiler in JPC, which compiles x86 native code to JVML bytecode) and native code to native code (e.g. the JIT compiler in PearPC, which compiles PowerPC native code to x86 native code).

Jörg W Mittag