Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

P-code machine

From Wikipedia, the free encyclopedia
Programming virtual machine

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).

P-code versus machine code

[edit]

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.

Implementations of P-code

[edit]

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]

UCSD P-Machine

[edit]

Architecture

[edit]

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

Environment

[edit]

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):

  • SP points to the top of the stack (thestack pointer).
  • MP marks the beginning of the active stack frame (themark pointer).
  • EP points to the highest stack location used in the current procedure (theextreme pointer).

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.

Calling conventions

[edit]
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.

Example machine

[edit]
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:

  1. lit 0,a : load constanta
  2. opr 0,a : execute operationa (13 operations: RETURN, 5 mathematical functions, and 7 comparison functions)
  3. lodl,a : load variablel,a
  4. stol,a : store variablel,a
  5. call,a : call procedurea at levell
  6. int 0,a : increment t-register bya
  7. jmp 0,a : jump toa
  8. jpc 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]

Microsoft P-code

[edit]

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.

Other implementations

[edit]
For example implementations, seeBytecode § Examples.

See also

[edit]

References

[edit]
  1. ^Upton, Eben; Duntemann, Jeffrey; Roberts, Ralph; Mamtora, Tim; Everard, Ben (2016-09-13).Learning Computer Architecture with Raspberry Pi. John Wiley & Sons.ISBN 978-1-119-18393-8.
  2. ^Wirth, Niklaus; Weber, Helmut (1966)."EULER: a generalization of ALGOL, and its formal definition: Part II".Communications of the ACM.9 (2). New York, USA:Association for Computing Machinery (ACM):89–99.doi:10.1145/365170.365202.S2CID 12124100.
  3. ^Nori, Kesav V.; Ammann, Urs; Jensen, Kathleen; Nägeli, Hans-Heinrich; Jacobi, Christian (1975).The Pascal P Compiler Implementation Notes. Zürich, Switzerland:Eidgenössische Technische Hochschule (ETH).
  4. ^Pike, Robert C. (2016)."The Design of the Go Assembler".YouTube (Conference talk).Archived from the original on 2021-12-11. Retrieved2017-08-25.
  5. ^"Category Archives: Wirth - Euler - Designed by Niklaus Wirth and Helmut Weber".Pascal for small machines - Wirth languages, Pascal, UCSD, Turbo, Delphi, Freepascal, Oberon. 2018-08-02.
  6. ^Alpert, Donald (September 1979).A Pascal P-Code Interpreter for the Stanford Emmy(PDF) (Report). Computer Systems Laboratory, Departments of Eleotrioal Engineering and Computer Scienoes, Stanford University. Technioal Note No. 164.
  7. ^"Software".Computerworld.19 (15). Framingham, MA: CW Communications: 71. 1985-04-15.ISSN 0010-4841.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.
  8. ^Padawer, Andy (April 1992)."Microsoft P-Code Technology".Microsoft Developer Network. Archived fromthe original on 2001-02-22.
  9. ^"Compiling Your Project to Native Code".Visual Studio 6.0 Documentation. 2007. Archived fromthe original on 2007-02-27.

Further reading

[edit]

External links

[edit]
Authority control databases: NationalEdit this at Wikidata
Retrieved from "https://en.wikipedia.org/w/index.php?title=P-code_machine&oldid=1300819614"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp