Why should facebook's hiphop convert php to C ++ instead of a compiled language... Isn't it faster to directly execute compiled files? Why should facebook's hiphop convert php to C ++ instead of a compiled language... Isn't it faster to directly execute compiled files? Reply content: First, you have to read the entire history: Facebook launched HHVM (HipHop VM) after HipHop (HPHPc). The former is (before running) compile PHP into C ++ and then compile it into machine code, while the latter (at runtime) compiles PHP into machine code. Therefore, PHP can be compiled into machine code, and Facebook has already done so in HPHPc and HHVM.
(Of course, HHVM has gone through several internal forms to compile PHP into machine code, but it does not use exposed C ++ as the intermediate form of compilation like HPHPc .)
The remaining problems are:
1. Why does HPHPc choose to compile PHP into C ++ and then compile it into machine code?
2. When will it be compiled as a machine code? Which of the following cannot be compiled into machine code in advance (AOT?
First, we will discuss the first question.
HPHPc is an AOT compilation (Ahead-Of-Time) solution. The PHP subset supported by HPHPc is actually "compiled. The ultimate goal is to compile PHP as a machine code. The specific method is to compile the code to C ++ first, and then use the common C ++ compiler to compile the code to the machine code. In fact, C ++ is used as the compiler's IR (intermediate representation, intermediate representation), because a ready-made C ++ compiler can compile the C ++ source code to the machine code, it is equal to the compiler backend (IR-> machine code). You only need to write a compiler front-end (source code-> IR. This is the point mentioned in the previous answers.
Isn't it faster to directly execute compiled files?
The C ++ source code is not directly executed. You must use the C ++ compiler to compile the machine code. We need to complete the process.
In the past, similar solutions were implemented using C --
Or simply use C as the target language, first compile your language into C -- or C, and then use the ready-made C compiler as the compiler backend.
In fact, there is another benefit, that is, the HipHop runtime is written in C ++: Like Variant
This built-in type is implemented using a C ++ class, which should be linked to such a runtime, the most convenient way is to generate C ++ code. The rest will be handled by the ready-made linker. C ++ is rarely directly linked to the target code compiled by other languages. Normally, C ++ exports a set of C interfaces when interacting with other native languages; even the target Code Compiled by different C ++ compilers may not be linked together. By compiling PHP into C ++ source code, you don't have to worry about the link and the compatibility issue.
Curious students can see what the C ++ code generated by HPHPc looks like. For example, What does the C ++ output of the HipHop PHP compiler look like?
Let's discuss the second question.
Calculate the entire compilation outflow. HPHPc + g ++ compiles PHP into machine code before the PHP code written by the user is executed, which is AOT; HHVM compiles PHP into machine code when the PHP code written by the user is being executed. It is a dynamic Compiler (in general, it is also a JIT compiler, but strictly speaking, it is a little later than the JIT compiler ).
AOT compilation is not easy to cope with PHP source code generated/loaded at runtime, so it is difficult to support eval. Generally, an AOT solution should deal with dynamically generated/loaded code, and will include an interpreter or JIT compiler in the runtime, the implementation complexity of this runtime is almost the same as that of a complete VM. Therefore, HPHPc does not support dynamic functions such as eval and create_function.
During runtime compilation (JIT or dynamic compilation), you can perfectly cope with the Dynamically Loaded code. Anyway, you can dynamically generate the code and I will take it for dynamic compilation. HHVM selects this path, so it can support a larger PHP subset.
The disadvantage is that the compilation is performed at runtime, so the compilation time must be included in the running time. In this way, compilation is not convenient for heavyweight optimization with a low efficiency ratio, to implement the JIT compiler, You must be more careful about balance of cost effectiveness. Because they don't want to re-write the IL-to-machine code process, this is a huge headache. Do you know how mad a machine code is? Thank you ~ The answer is not enough. There is a fundamental difference between the explanatory language and the compilation language in terms of writing, project structure, and coding ideas. If a php is compiled, the cost of language creation is huge. The existing business is already written in PHP. If it is changed to a new language, it is better to use a mature compilation language such as java to write it again ~ As you can see, most of the languages invented by @ vczh are virtual machine languages or compiled to CLI. generating local code requires a lot of programming and optimization work, compared with the mature c ++ compiler, re-writing a php compiler is obviously a thankless task. This is too troublesome ~~~~