Please refer to the "other issues" section, too:
Porting Stanford Pascal to Windows, OS/2 and Linux - other issuesI decided to build the correct solution for the branch table problem now. That is, I removed the two constants for the lowest and highest value. And I marked the XJP instruction with a flag (N); this N stands for "new format of branch table".
The new XJP instruction needs only two labels, not four. But when working on the P-Code translator, I discovered that the translator still needs the min and max value fields and uses the label names for the machine code, although they aren't used in the P-Code any more. So I decided to reserve the names in the first pass, but not to use them; the names are left unused and can be used by the second pass.
Here is an example; the P-Code for a character based case statement looked like this before:
|
and it looks now like this:
|
The labels L3 and L4 are not used any more, but they are reserved and may be used by the P-Code generator to store (internally) the min and max values, which are derived from the DEF constants of the branch table (which need not be sorted by the char set sequence - in the char case).
There is an interesting difference between integer based branch tables and character based branch tables:
integer based branch tables need to be sorted by integer value; that means, that the first DEF constant contains the lowest value, and the position of all entries in the branch table can be determined by the difference of their DEF constant from this first (lowest) DEF constant;
char based branch tables are not sorted in the general case; that means that the lowest value is not known beforehand and the construction of the branch table must be deferred until the whole table of DEFs and UJPs is processed.
The difference between highest and lowest value is limited by the compiler constant CIXMAX; at the moment, CIXMAX = 400. This is equivalent to the largest possible branch table (equals 800 Bytes in the CSECT), and its by far large enough for the whole char type.
Some possible improvements for the future (if this should be a problem):
switch to a tree structure or hash, if flat table grows too large; this would even allow to have a much higher CIXMAX value
move branch table to static storage
In the end, the implementation of the case statement turned out to be pretty interesting; this is something that was already observed very early by C.A.R. Hoare, IIRC.