In computer programming, a p-code machine, or precompiled code machine is a virtual machine designed to execute p-code (the assembly language of a hypothetical CPU). This term is applied both generically to all such machines (such as the Java Virtual Machine and MATLAB precompiled code), and to specific implementations, the most famous being the p-Machine of the Pascal-P system, particularly in its UCSD Pascal incarnation.
Although the concept was first implemented circa 1966 (as O-code for BCPL), the term p-code first appeared in the early 1970s. Two early compilers generating p-code were the Pascal-P compiler in 1973, by Nori, Ammann, Jensen, Hageli, and Jacobi, and the Pascal-S compiler in 1975, by Niklaus Wirth.
Programs that have been translated to p-code are interpreted by a software program that emulates the behavior of the hypothetical CPU. If there is sufficient commercial interest, a hardware implementation of the CPU specification may be built (e.g., the Pascal MicroEngine).
Benefits and weaknesses of implementing p-code
Compared to direct translation into native machine code, a two-stage approach involving translation into p-code and execution by an interpreter or just-in-time compiler offers several advantages.
- Portability: It is much easier to write a small p-code interpreter for a new machine than it is to modify a compiler to generate native code for the same machine.
- Simple implementation: Generating machine code is one of the more complicated parts of writing a compiler. By comparison, generating p-code is much easier. This makes it useful for getting a compiler up and running quickly.
- Compact size: Since p-code is based on an ideal virtual machine, a p-code program is often much smaller than the same program translated to machine code.
- Debugging: When the p-code is interpreted, the interpreter can apply additional runtime checks that are difficult to implement with native code.
Full article ▸