Incomputer programming, aP-code machine (portable code machine[1]) is avirtual machine designed to executeP-code, theassembly language ormachine code of a hypotheticalcentral processing unit (CPU). The termP-code machine is applied generically to all such machines (such as theJava virtual machine (JVM) andMATLABpre-compiled code), as well as specific implementations using those machines. One of the most notable uses of P-Code machines is the P-Machine of thePascal-P system. The developers of theUCSD Pascal implementation within this system construed theP inP-code to meanpseudo more often thanportable; they adopted a unique label forpseudo-code meaning instructions for a pseudo-machine.
Although the concept was first implemented circa 1966 asO-code for the Basic Combined Programming Language (BCPL) and P code for the languageEuler,[2] theterm P-code first appeared in the early 1970s. Two earlycompilers generating P-code were the Pascal-P compiler in 1973, by Kesav V. Nori, Urs Ammann, Kathleen Jensen, Hans-Heinrich Nägeli, and Christian Jacobi,[3] and thePascal-S compiler in 1975, byNiklaus Wirth.
Programs that have been translated to P-code can either beinterpreted by a software program that emulates the behaviour of the hypothetical CPU, ortranslated into the machine code of the CPU on which the program is to run and then executed. If there is sufficient commercial interest, a hardware implementation of the CPU specification may be built (e.g., thePascal MicroEngine or a version of aJava processor).
While a typicalcompiler model is aimed at translating a program code intomachine code, the idea of a P-code machine follows a two-stage approach involving translation into P-code and execution byinterpreting orjust-in-time compilation (JIT) through the P-code machine.
This separation makes it possible to detach the development of a P-codeinterpreter from the underlying machine code compiler, which has to consider machine-dependent behaviour in generating itsbytecode. This way a P-code interpreter can also be implemented quicker, and the ability to interpret the code at runtime allows for additionalrun-time checks which might not be similarly available in native code. Further, as P-code is based on an ideal virtual machine, a P-code program can often be smaller than the same program translated to machine code. Conversely, the two-step interpretation of a P-code-based program leads to a slower execution speed, though this can sometimes be addressed withjust-in-time compilation, and its simpler structure is easier toreverse-engineer than native code.
In the early 1980s, at least twooperating systems achievedmachine independence through extensive use of P-code[citation needed]. TheBusiness Operating System (BOS) was a cross-platformoperating system designed to run P-code programs exclusively. TheUCSD p-System, developed at The University of California, San Diego, was a self-compiling andself-hosting operating system based on P-code optimized for generation by thePascal language.
In the 1990s, translation into p-code became a popular strategy for implementations of languages such asPython,Microsoft P-Code inVisual Basic andJava bytecode inJava.
The languageGo uses a generic, portable assembly as a form of p-code, implemented byKen Thompson as an extension of the work onPlan 9 from Bell Labs. UnlikeCommon Language Runtime (CLR) bytecode or JVM bytecode, there is no stable specification and the Go build tools do not emit a bytecode format to be used at a later time. The Go assembler uses the generic assembly language as anintermediate representation and the Go executables are machine-specificstatically linked binaries.[4]
Like many other P-code machines, the UCSD P-Machine is astack machine, which means that most instructions take their operands from astack, and place results back on the stack. Thus, theadd
instruction replaces the two topmost elements of the stack with their sum. A few instructions take an immediate argument. Like Pascal, the P-code isstrongly typed, supporting Boolean (b), character (c), integer (i), real (r), set (s), and pointer (a)data types natively.
Some simple instructions:
Insn. Stack Stack Description before after adi i1 i2 i1+i2 add two integersadr r1 r2 r1+r2 add two realsinn i1 s1 b1 set membership; b1 = whether i1 is a member of s1ldi i1 i1 i1 load integer constantmov a1 a2 a2 movenot b1 b1 -b1 Boolean negation
Similar to a real target CPU, the P-System has only one stack shared by procedure stack frames (providingreturn address, etc.) and the arguments to local instructions. Three of the machine'sregisters point into the stack (which grows upwards):
Also present is a constant area, and, below that, theheap growing down towards the stack. The NP (thenew pointer) register points to the top (lowest used address) of the heap. When EP gets greater than NP, the machine's memory is exhausted.
The fifth register, PC, points at the current instruction in the code area.
![]() | This sectioncontainsinstructions or advice. Wikipedia is not a guidebook; please helprewrite the content to make it more encyclopedic ormove it toWikiversity,Wikibooks, orWikivoyage.(January 2024) |
Stack frames look like this:
EP -> local stackSP -> ... locals ... parameters ... return address (previous PC) previous EP dynamic link (previous MP) static link (MP of surrounding procedure)MP -> function return value
The procedure calling sequence works as follows: The call is introduced with
mst n
wheren
specifies the difference in nesting levels (remember that Pascal supports nested procedures). This instruction willmark the stack, i.e. reserve the first five cells of the above stack frame, and initialize previous EP, dynamic, and static link. The caller then computes and pushes any parameters for the procedure, and then issues
cup n, p
to call a user procedure (n
being the number of parameters,p
the procedure's address). This will save the PC in the return address cell, and set the procedure's address as the new PC.
User procedures begin with the two instructions
ent 1, i ent 2, j
The first sets SP to MP +i
, the second sets EP to SP +j
. Soi
essentially specifies the space reserved for locals (plus the number of parameters plus 5), andj
gives the number of entries needed locally for the stack. Memory exhaustion is checked at this point.
Returning to the caller is accomplished via
retC
withC
giving the return type (i, r, c, b, a as above, and p for no return value). The return value has to be stored in the appropriate cell previously. On all types except p, returning will leave this value on the stack.
Instead of calling a user procedure (cup), standard procedureq
can be called with
csp q
These standard procedures are Pascal procedures likereadln()
(csp rln
),sin()
(csp sin
), etc. Peculiarlyeof()
is a p-Code instruction instead.
![]() | This sectionreads like atextbook. Pleaseimprove this article to make itneutral in tone and meet Wikipedia'squality standards.(January 2024) |
Niklaus Wirth specified a simple p-code machine in the 1976 bookAlgorithms + Data Structures = Programs. The machine had 3 registers - aprogram counterp, abase registerb and atop-of-stack registert. There were 8 instructions:
lit 0,a
: load constantaopr 0,a
: execute operationa (13 operations: RETURN, 5 mathematical functions, and 7 comparison functions)lodl,a
: load variablel,astol,a
: store variablel,acall,a
: call procedurea at levellint 0,a
: increment t-register byajmp 0,a
: jump toajpc 0,a
: jump conditional toa[5]This is the code for the machine, written in Pascal:
constamax=2047;{maximum address}levmax=3;{maximum depth of block nesting}cxmax=200;{size of code array}typefct=(lit,opr,lod,sto,cal,int,jmp,jpc);instruction=packedrecordf:fct;l:0..levmax;a:0..amax;end;varcode:array[0..cxmax]ofinstruction;procedureinterpret;conststacksize=500;varp,b,t:integer;{program-, base-, topstack-registers}i:instruction;{instruction register}s:array[1..stacksize]ofinteger;{datastore}functionbase(l:integer):integer;varb1:integer;beginb1:=b;{find base l levels down}whilel>0dobeginb1:=s[b1];l:=l-1end;base:=b1end{base};beginwriteln(' start pl/0');t:=0;b:=1;p:=0;s[1]:=0;s[2]:=0;s[3]:=0;repeati:=code[p];p:=p+1;withidocasefoflit:begint:=t+1;s[t]:=aend;opr:caseaof{operator}0:begin{return}t:=b-1;p:=s[t+3];b:=s[t+2];end;1:s[t]:=-s[t];2:begint:=t-1;s[t]:=s[t]+s[t+1]end;3:begint:=t-1;s[t]:=s[t]-s[t+1]end;4:begint:=t-1;s[t]:=s[t]*s[t+1]end;5:begint:=t-1;s[t]:=s[t]divs[t+1]end;6:s[t]:=ord(odd(s[t]));8:begint:=t-1;s[t]:=ord(s[t]=s[t+1])end;9:begint:=t-1;s[t]:=ord(s[t]<>s[t+1])end;10:begint:=t-1;s[t]:=ord(s[t]<s[t+1])end;11:begint:=t-1;s[t]:=ord(s[t]>=s[t+1])end;12:begint:=t-1;s[t]:=ord(s[t]>s[t+1])end;13:begint:=t-1;s[t]:=ord(s[t]<=s[t+1])end;end;lod:begint:=t+1;s[t]:=s[base(l)+a]end;sto:begins[base(l)+a]:=s[t];writeln(s[t]);t:=t-1end;cal:begin{generate new block mark}s[t+1]:=base(l);s[t+2]:=b;s[t+3]:=p;b:=t+1;p:=aend;int:t:=t+a;jmp:p:=a;jpc:beginifs[t]=0thenp:=a;t:=t-1endend{with, case}untilp=0;writeln(' end pl/0');end{interpret};
This machine was used to run Wirth'sPL/0, a Pascal subset compiler used to teach compiler development.[6][failed verification]
As the goal of the company was to release software for all the major platforms and architectures that existed then. Between 1980 and 1982, Microsoft developed an early C compiler that produced P-Code (the C language itself was not standardized and wouldn't be until later in the 80s). The P-Code allowed software to run on most platforms with minimal code change. UCSD Pascal was using a similar approach. This C to P-Code was a success but was very slow. In 1983, Microsoft released the Microsoft C Compiler, MSC, based on a license of the Lattice C compiler for versions 1.0 and 2.0; then, from version 3.0 onward, the MSC was a complete rewrite by Microsoft.[7]
P-code is a name later used by some ofMicrosoft'sintermediate languages. They provided an alternate binary format to machine code. At various times, Microsoft has said P-code is an abbreviation for eitherpacked code[8] orpseudo code.[9]
Some Microsoft P-code flavor, quite different from the one used by the C compiler, was widely used withVisual Basic which had a runtime that included a VM or could be directly compiled to native code. Like other P-code implementations, Microsoft P-code enabled a more compactexecutable at the expense of slower execution.
Microsoft has announced an enhanced version of its Microsoft C Compiler for the IBM Personal Computer… A vendor spokesman said Version 3.0 is the first C compiler developed internally by Microsoft.