The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 717 other subscribers

Trip down memory lane: book on p-Code based UCSD Pascal

Posted by jpluimers on 2025/05/06

Last week I wrote on File scoped namespaces – C# 10.0 draft specifications | Microsoft Learn, promising to write more on p-Code and UCSD Pascal. That’s now (:

I started with [Wayback/Archive] “java byte code” “ucsd” “p-code” – Google Search as I was looking for really old material on this (Java 1.0 versions became available in the 1994-1995 time frame, and a lot of material back then either did not make it to the World Wide Web (which slowly gained popularity around that time, see History of the World Wide Web) or has vanished due to link rot.

The cool thing is that many “new” people are not even aware of p-Code, as the 2019 thread [Wayback/Archive] What do you think about something like Pascal bytecode? shows.

I learned a thing or two from it as well, for instance that there has been a “recent” book on UCSD Pascal:

The p-code was invented by Urs Amman, a student of Niklas Wirth in the mid 70th.
Kenneth Bowles at the University San Diego California put Pascal and p-code together and developed UCSD Pascal, also known as Apple Pascal or UCSD Power Pascal.

Because only 15% of the OS are machine dependent UCSD Pascal became wide spread and was  implemented on over different 80 machine types. The most work was to rewrite the system.interpreter in the Assembler dialect of ther machine. Later the interpreter was rewritten in C by Knowledge Software GB. The Java byte code is based on the p-code.

…

A short book about the UCSD Pascal Compiler and the p-system.
From 1982.
Chapter 10 is about p-code.

https://homepages.cwi.nl/~steven/pascal/book/pascalimplementation.html

…

The book is not totally correct, but close. I started with UCSD pascal on an Apple 11e. (so, after the language originated)
The idea from the P-code originated with Wirth. The implementation of which is done by his student.
This is quite common in University circles.

…

Can I conclude that the idea of string [complex type] was invented at UCSD Pascal, but Borland Turbo Pascal 1.0 in 1983 was the first one to really implement the String [simple] type?

…

Yes, that is the correct history.

UCSD was inteded for educational reasons – to learn Pascal, to learn how to build a compiler and was very puristic with the syntax and the types. Anyway – there were a lot of programms for buisiness use made with UCSD. And it came free with Apple II and Clones.

Turbo came some years later and came from the practical side: you could make sound, you could write a working terminal program (with pascal extension port), the compiler was optimized – as Anders Heilsberg said – not along the rules but with some hacks to speed it up. And you should not forget: A compiler in these days  had a price of 1000.- $ and more. Turbo was sold in US for 99 $ and in Germany for 299.- DM (German Marks).

With Turbo you could play around with the hardware so you could get a serial RS 232 with 115.000 Baud – which was not due to the specifications but it worked. Later they sold this as “Poor mens network” with a serial (crossed) cable and some software (Kischbaum Link). You could connect 2 PCs and use neighbours disk C: as drive D: (or higher).

With UCSD you could not do such hardware hacks because it was machine independent. Then they recognized this and inserted unitread and unitwrite, but they tried to keep it secret before the students.

…

I vaguely remembered the first part about Pascal (and was happy about the refresher: I knew it was not Niklaus Wirth himself, now I know the name of the student at the ETH Zürich), but unlike [Wayback/Archive] ETH Zurich / N. Wirth / Books / PASCAL – User Manual, this book by Steven Pemberton and Martin Daniels was totally new for me!

The “Pascal Implementation” book: 20 years of work?!

Probably not 20 years of continuous work, but the copyright goes from 1982 to 2002, annotating the source code for the Pascal P4 Compiler.

It is at [Wayback/Archive] Pascal Implementation:

The full text of Pascal Implementation has been scanned from the original book, and OCR’d, before being updated. This initial release should therefore be considered a DRAFT. As a service to the community, you are therefore asked to communicate any errors, problems, or suggestions back to the authors at the email address below. Thanks!
If you understand this is a draft, and will endeavour to communicate errors you find back to the author, then [Wayback/Archive] please proceed.

Then continues at [Wayback/Archive] Pascal Implementation; I added the archive links to the table of contents.

The P4 Compiler and Interpreter

Part 1: The Compiler

Part 2: The Interpreter

Appendices

Copyright © 1982, 2002 Steven Pemberton and Martin Daniels, all rights reserved.

The compiler and interpreter themselves start at at another page named [Wayback/Archive] Pascal Implementation

Pascal Implementation: A Book and Sources

Included here is the Pascal source of a public-domain Pascal compiler and interpreter, the P4 compiler and interpreter. It is coded entirely in Pascal, and produces a high-level so-called intermediate code as output. The program ‘pint’ is an assembler and interpreter for this language.

The entire compiler and interpreter is documented in the book:

[Wayback/Archive] Pascal Implementation: The P4 Compiler and Interpreter, by [Wayback/Archive] Steven Pemberton and [Wayback/Archive] Martin Daniels, Ellis Horwood, ISBN: [W/Archive] 0-13-653-0311 (also available in [Wayback/Archive] Japanese).

It was distributed by John Wiley in other countries, but now that Prentice Hall has taken over Ellis Horwood, that will have changed.

Steven Pemberton is contactable by email as Steven.Pemberton@cwi.nl. He did not write the compiler, only documented it in the book.

What you have to do to use this compiler

  • Compile pcom.p and pint.p with a Pascal compiler. You obviously have to have a Pascal compiler already. This gives you a Pascal compiler (pcom) that produces P4 code, and an interpreter (pint) that runs P4 code.
  • To use the compiler, run pcom with the Pascal program as standard input. This produces any diagnostics on standard output, and its code on a Pascal file that is called prr. Check with your Pascal compiler how this gets assigned to a file in the filestore. You may have to change the lines ‘rewrite(prr)‘ in pcom.p and pint.p and ‘reset(prd)‘ in pint.p for your compiler, for instance to “rewrite(prr, 'prr')” etc.
  • To run the resulting code, run pint with the prr output produced by pcom as input for the file ‘prd‘, and input for the compiled pascal program on standard input.

For instance, do this once:

  • pc -o pcom pcom.p
  • pc -o pint pint.p

and for each program:

  • pcom < test.p # produces file prr containing the p4 code
  • mv prr prd
  • pint < input

You have to supply input to pint, even if the program doesn’t read from it, for instance:

  • pint < /dev/null

If you intend to compile pcom with itself, there are two lines that have to be commented out when you do; search for the word ‘comment‘ in the pcom source. There is no reason why you should want to compile pint.p with pcom.

What If You Haven’t Got a Pascal Compiler?

Differences with the Book

The code here is slightly different from that in the book, but the line numbers have been kept the same. The changes were to allow modern Pascal compilers to compile the source (there were some laxities in the original code).

[Wayback/Archive] The Compiler Source

[Wayback/Archive] The Interpreter Source

[Wayback/Archive] The Differences with the book

Source files themselves

Source files in HTML form

Two of the above files are also on Steven’s web site in HTML form:

  1. [Wayback/Archive] https://homepages.cwi.nl/~steven/pascal/book/pcom.html
         1  (*$c+,t-,d-,l-*)
         2   (***********************************************
         3    *                                             *
         4    *      Portable Pascal compiler               *
         5    *      ************************               *
         6    *                                             *
         7    *             Pascal P4                       *
         8    *                                             *
         9    *     Authors:                                *
        10    *           Urs Ammann                        *
        11    *           Kesav Nori                        *
        12    *           Christian Jacobi                  *
        13    *     Address:                                *
        14    *       Institut Fuer Informatik              *
        15    *       Eidg. Technische Hochschule           *
        16    *       CH-8096 Zuerich                       *
        17    *                                             *
        18    *  This code is fully documented in the book  *
        19    *        "Pascal Implementation"              *
        20    *   by Steven Pemberton and Martin Daniels    *
        21    * published by Ellis Horwood, Chichester, UK  *
        22    *         ISBN: 0-13-653-0311                 *
        23    *       (also available in Japanese)          *
        24    *                                             *
        25    * Steven Pemberton, CWI, Amsterdam            *
        26    * http://www.cwi.nl/~steven/                  *
        27    * Steven.Pemberton@cwi.nl                     *
        28    *                                             *
        29    ***********************************************)
  2. [Wayback/Archive] https://homepages.cwi.nl/~steven/pascal/book/pint.html
         1 (*Assembler and interpreter of Pascal code*)
         2 (*K. Jensen, N. Wirth, Ch. Jacobi, ETH May 76*)
         3
         4 program pcode(input,output,prd,prr);
         5 
         6 (* Note for the implementation.
         7    ===========================
         8 This interpreter is written for the case where all the fundamental types
         9 take one storage unit.
        10 In an actual implementation, the handling of the sp pointer has to take
        11 into account the fact that the types may have lengths different from one:
        12 in push and pop operations the sp has to be increased and decreased not
        13 by 1, but by a number depending on the type concerned.
        14 However, where the number of units of storage has been computed by the
        15 compiler, the value must not be corrected, since the lengths of the types
        16 involved have already been taken into account.
        17          *)

Page 2 of the “Pascal bytecode?” thread

I learned a few more things from [Wayback/Archive] What do you think about something like Pascal bytecode? (Page 2)

Well, [Wayback/Archive] https://sourceforge.net/projects/pascalp5/ is a pascal bytecode compiler(it is actually used in the tests to test ISO conformance in {$mode iso})
And the author sometimes visits this forum. Freepascal – at least 3.2.0 and trunk- can compile this compiler…
Scott Franco, the author, is a bit of a ISO standards guru, at least to me.

His website also contains the original pascal-s and some previous efforts.

…

Perhaps more to the point, having a compiler emit a bytecode sequence as a well-documented intermediate representation is “very 1980s”. With all respect to Scott, Bernd and the other maintainers of historic compilers, the way it’s done these days is for the frontend to generate a parse tree which might- or might not- be published and documented: and if OP wants to help document some of the FPC arcana I’m sure his help would be appreciated :-)

Noting that Bernd’s port of P4 http://bernd-oppolzer.de/job9.htm runs on at least some of the classic mainframe OSes, I’d add that poor understanding of FPC’s internal operation was one of the reasons that FPC didn’t get ported to those platforms.

…

Why did he port that? The original author already did that: Scott Franco (a.k.a. Scott Moore). Better use the source, the source and not some vague ports.

…

IIRC P4 originated at ETH, was built for portability by Wirth’s team in the seventies. and was indeed ported to many platforms. Stanford Pascal was one of those ports, for IBM mainframes. Scott Moore was not  the original author, but he did (much later in time!) quite a remarkable job digging up the original source code and publishing it. And made a ‘P5’ out of it. All compiler-interpreters with p-code (not bytecode!)

…

Afaik, ETH’s P4 was meant as bootstrap compiler because the small bytecode interpreter would be easily portable.
Since the compiler didn’t use the full ISO dialect, neither did P4.

That’s why Scott made P5, which is P4 expanded to support the full ISO dialect.

Hopefully more on that in a future blog post.

--jeroen


Gist with P4C source files from https://homepages.cwi.nl/~steven/pascal/p4c.tar.gz

[Wayback/Archive] P4C files by I.J.A.vanGeel@twi.tudelft.nl from https://homepages.cwi.nl/~steven/pascal/p4c.tar.gz (P2C translated C version of the P4 Pascal compiler, originally for UNIX and OS/2; requires GCC)

https://gist.github.com/jpluimers/c255ebd3a550c3b90e238914249291a6

No Responses Yet to “Trip down memory lane: book on p-Code based UCSD Pascal”

  1. … reposted this!

Leave a Comment