HP Pascal/HP-UX Programmer's Guide ABCDE HP Part No. 92431-90006 Printed in U.S.A.
The information contained in this document is subject to change without notice. HEWLETT-PACKARD MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Hewlett-Packard shall not be liable for errors contained herein or for incidental or consequential damages in connection with the furnishing, performance, or use of this material.
Printing History New editions are complete revisions of the manual. Update packages may be issued between editions. The software code printed alongside the date indicates the version level of the software product at the time the manual was issued. Many product updates and xes do not require manual changes and, conversely, manual corrections may be done without accompanying product changes. Therefore, do not expect a one-to-one correspondence between product updates and manual updates.
iv
Preface This HP Pascal/HP-UX Programmer's Guide for the Hewlett-Packard HP Pascal/iX and HP Pascal/HP-UX programming languages is intended for programmers with at least six months of Pascal programming experience, but no HP Pascal/iX or HP Pascal/HP-UX programming experience. It discusses selected HP Pascal/iX and HP Pascal/HP-UX topics in detail, and explains statement interaction where necessary.
Chapter 10 Chapter 11 Chapter 12 Appendix A Appendix B Explains how your program can use intrinsics. Explains how to write error recovery code that allows your program to handle its own run-time errors. Explains how to debug your program. Explains how to use the optimizer to improve your program. Explains how HP Pascal/iX works on the MPE/iX operating system. Explains how HP Pascal/HP-UX works on the HP-UX operating system.
Conventions UPPERCASE In a syntax statement, commands and keywords are shown in uppercase characters. The characters must be entered in the order shown; however, you can enter the characters in either upper or lowercase.
Conventions (continued) [ . .. ] In a syntax statement, horizontal ellipses enclosed in brackets indicate that you can repeatedly select the element(s) that appear within the immediately preceding pair of brackets or braces. In the example below, you can select Parameter zero or more times. Each instance of Parameter must be preceded by a comma: [,Parameter][...
Pascal Speci c Conventions The conventions followed in this manual are summarized below: For Text: The term PAC is used for the type PACKED ARRAY OF CHAR with the lower bound equal to 1. Reserved words and directives are in all uppercase letters. Examples: BEGIN, REPEAT, FORWARD Standard identi ers are in all lowercase letters. Examples: readln, maxint, text General information concerning an area of programming (topic) appears as a heading with initial capitalization.
Contents 1. Introduction 2. Program Structure Syntactic Structure . . . . . . . . . . . . . . . . Program Heading . . . . . . . . . . . . . . . . Program Block . . . . . . . . . . . . . . . . . Compilation Unit Structure . . . . . . . . . . . . Modules . . . . . . . . . . . . . . . . . . . . Global, Subprogram, and External Compilation Units Separate Compilation . . . . . . . . . . . . . . . Using Modules . . . . . . . . . . . . . . . . . When to Use Modules . . . . . . . . . . . . .
4. Prede ned Pascal Constants, Data Types, and Modules Values of Prede ned Constants . . . . . . . . . . . Ranges of Prede ned Data Types . . . . . . . . . . Bit16 . . . . . . . . . . . . . . . . . . . . . . Bit32 . . . . . . . . . . . . . . . . . . . . . . Bit52 . . . . . . . . . . . . . . . . . . . . . . Shortint . . . . . . . . . . . . . . . . . . . . . Longint . . . . . . . . . . . . . . . . . . . . . Prede ned Modules . . . . . . . . . . . . . . . . stdinput . . . . . . . . . . . . . . . . . . . .
7. Parameters Value versus Reference Parameters . . . . . . . ANYVAR Parameters . . . . . . . . . . . . . READONLY Parameters . . . . . . . . . . . Conformant Array Parameters . . . . . . . . . Routines as Parameters . . . . . . . . . . . . Routine Parameters . . . . . . . . . . . . . Parameters of Routine Types . . . . . . . . . Variables of Routine Types . . . . . . . . . . Call Procedure . . . . . . . . . . . . . . . Fcall Function . . . . . . . . . . . . . . . Congruent Parameter Lists . . . . . . . . .
Reference Parameter Compatibility . Value Parameter Compatibility . . . Using Intrinsic Functions as Procedures De ning Intrinsics . . . . . . . . . . . How to Build or Change an Intrinsic File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10 10-12 10-14 10-15 10-17 11. Error Recovery and Debugging Error Recovery . . . . . . . . . . . . . . . . Escape Procedure . . . . . . . . . . . .
A. MPE/iX Dependencies System-Dependent Features . . . . . . . . . . Compiler Options . . . . . . . . . . . . . . File Names . . . . . . . . . . . . . . . . . Associating Logical and Physical Files . . . . . Using File Equations . . . . . . . . . . . . Default File Attributes . . . . . . . . . . . Standard Modules . . . . . . . . . . . . . Additional Features . . . . . . . . . . . . . Restrictions on Using Executable Libraries (XLs) MPE/iX Extensions . . . . . . . . . . . . . . ccode Function . . . . . . .
Using +DA to Generate Code for a Speci c Version of PA-RISC Using +DS to Specify Instruction Scheduling . . . . . . . . . Guidelines for Using +DA and +DS . . . . . . . . . . . . . Compiling in Networked Environments . . . . . . . . . . . Glossary Index Contents-6 . . . . . . . . . . . . . . . . . . . .
Figures 1-1. 2-1. 2-2. 2-3. 3-1. 3-2. 3-3. 3-4. 9-1. 11-1. A-1. B-1. Relationship Between HP Pascal and Other Pascals . . . . . . . . Syntactic Structure of a Program . . . . . . . . . . . . . . . . Syntactic Structure of a Module . . . . . . . . . . . . . . . . What a Module Can Access . . . . . . . . . . . . . . . . . . Relationships Between I/O Varieties and File Types . . . . . . . . File Relationships . . . . . . . . . . . . . . . . . . . . . . .
Tables 3-1. 3-2. 3-3. 3-4. 3-5. 3-6. 3-7. 3-8. 3-9. 4-1. 5-1. 5-2. 5-3. 5-4. 5-5. 5-6. 5-7. 5-8. 5-9. 5-10. 5-11. 6-1. 7-1. 7-2. 7-3. 8-1. 8-2. 8-3. 9-1. 9-2. 9-3. 9-4. 10-1. 10-2. 10-3. Categories of Input/Output Routines . . . . . . . . . . . . . . . . . 3-2 Characteristics of File-Opening Procedures . . . . . . . . . . . . . . . 3-9 Characteristics of Associate Procedure . . . . . . . . . . . . . . . . . 3-11 Characteristics of Sequential I/O Procedures . . . . . . . . . . . . . .
10-4. 10-5. 12-1. A-1. A-2. A-3. A-4. A-5. A-6. A-7. A-8. A-9. A-10. A-11. A-12. Compatible Intrinsic and Formal Reference Parameter Types . . . . . . . Intrinsic-Compatible Intrinsic and Formal Value Parameter Types . . . . . Unoptimized and Optimized Branch Instruction Sequences . . . . . . . . Default File Attributes . . . . . . . . . . . . . . . . . . . . . . . Allocation and Alignment of Unpacked Variables (Pascal/V Packing Algorithm) . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 Introduction HP Pascal/iX and HP Pascal/HP-UX are supersets of HP Standard Pascal, the Pascal language that runs on all HP computers. HP Pascal/iX runs on the MPE/iX operating system and HP Pascal/HP-UX runs on the HP-UX operating system. Both operating systems run on HP PA-RISC computers, and both achieve ISO and ANSI validation. HP Pascal takes advantage of the architecture of these computers and has system programming extensions to HP Standard Pascal.
HP Pascal can interface with any subsystem that can be accessed through intrinsics. Some of the HP subsystems HP Pascal can interface with are listed below: Subsystem TurboIMAGE/XL SQL HP System Dictionary/XL VPLUS Description of Subsystem Reference TurboIMAGE/XL Reference Manual Network database management system. Your HP Pascal program accesses TurboIMAGE/XL routines with intrinsic calls.
2 Program Structure This chapter summarizes program structure|in terms of syntax and in terms of compilation units. For complete syntactic de nitions of programs and their components, refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation. Syntactic Structure Syntactically, every HP Pascal program is composed of two major parts: the program heading and the program block.
Example See the example in the section \Program Block". For more information about program parameters, see Appendix A and Appendix B. Program Block The program block consists of an optional declaration part and a statement (executable) part. The declaration part de nes whatever labels, constants, data types, variables (including program parameters), procedures, functions, or modules you want.
Example Program Structure 2-3
Compilation Unit Structure A compilation unit is a unit of source code that can be compiled independently of other code (for example, a program is a compilation unit; a block is not). You can design your program in two ways: As a single compilation unit. In this case you must compile the entire program at once. As two or more compilation units. In this case you can compile one unit at a time, or you can compile in groups. This is also known as separate compilation .
Figure 2-2 illustrates the syntactic structure of a module. For the exact syntax of a module and its components, refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation. Figure 2-2. Syntactic Structure of a Module A module's import declaration speci es the other modules that it imports. It can access items in the imported modules' export declarations. The import declaration can also be used to specify export of entire modules a second time.
Example 2-6 Program Structure
Figure 2-3. shows what a module can access. Figure 2-3. What a Module Can Access A module must be compiled before a program or another module imports it (therefore, two modules cannot import each other). For the compiler to compile a module with a program, the program must de ne the module in its declaration part. After de ning this module, the program can import it.
For more information on MLIBRARY and SEARCH, refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation. A program can de ne a module with the same name as a module in the library that SEARCH speci es. In that case, the program imports the module that it de nes, rather than the library module with the same name. If a library contains two modules with the same name, the second one overrides the rst.
Example Independently compiled modules (to be compiled together in a single compilation unit): MODULE Mod1; EXPORT . : IMPLEMENT . : END; {Mod1} MODULE Mod2; IMPORT Mod1; EXPORT . : IMPLEMENT . : END; {Mod2} MODULE Mod3; EXPORT . : END. {Mod3} {Mod1 is in Mod1.o} {Mod2 is in Mod1.o} {Mod2 imports Mod1} {This Mod3 is in Mod1.o} Program (to be compiled as a compilation unit that does not contain the above modules -- the program imports the modules from the above compilation unit): PROGRAM prog; .
Global, Subprogram, and External Compilation Units A global compilation unit de nes global constants, data types, and variables within a Pascal program. It also contains the body of the main program. Syntactically, it is a program that begins with the GLOBAL compiler option. For more information on the GLOBAL compiler option, refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation.
Separate Compilation Separate compilation is the process of separating the source for a large program into pieces that can be compiled independently of other pieces. There are several reasons why compiling pieces of a program separately is practical: When the program is too long to compile. When the program is too complex to manage. When the program is being worked on by more than one programmer or by a team of programmers. There are four methods used for separate compilation.
Using SUBPROGRAM The SUBPROGRAM compiler option turns a Pascal program into a subprogram compilation unit. For separate compilation, SUBPROGRAM must be included in all compilation units, except the compilation unit containing the outer block. No code is generated for the outer block if used. When to Use SUBPROGRAM SUBPROGRAM is recommended for use in compilation units where the global variables won't change much. SUBPROGRAM Advantages Using SUBPROGRAM results in smaller object les and less link time.
When to Use GLOBAL/EXTERNAL Use GLOBAL/EXTERNAL when sharing global information with another language, when the number of global variables are too large to recompile each time, and when building shared libraries or XLs. GLOBAL/EXTERNAL is also useful when global variables will change often. GLOBAL/EXTERNAL Advantages The following are some advantages of using GLOBAL/EXTERNAL: When you use GLOBAL/EXTERNAL for separate compilation, the global variables do not need to be listed in the same order.
External Interfaces Your program can interface with its external environment (other routines and les supported by the operating system) by using physical les, external routines, and intrinsics. A physical le is a program-independent entity that the operating system maintains. It can be a permanent le on a disk or other medium, or it can be an interactive le created at a terminal. Your program can manipulate a physical le by associating it with a logical le (a le that the program declares).
3 Input/Output Input/output depends on les: your program reads input from les and writes output to les. The terms that describe the three varieties of input/output|sequential, text le, and direct | also describe the associated les. This chapter: Gives general information about les. Explains the prede ned le-opening procedures and how they determine whether les are sequential or direct, for input or for output.
Input/output also depends upon the procedures that manipulate les and the functions that return information about them. Table 3-1 categorizes the prede ned input/output routines two ways: by purpose (for example, input or output) and by I/O type (sequential, text le, or direct). Table 3-1.
General File Information You need the general le information in this section to understand the rest of this chapter. Examine Figure 3-2, and then read the explanations of the entities in italics, whose relationships it shows. Figure 3-2 illustrates the relationship between physical les (in the operating system environment) and logical les (in the program environment). It also shows how logical les, text les, and the standard text les input and output are related. Figure 3-2.
Logical Files A logical le is a data structure that a program declares and controls. It is a sequence of components of the same type. The declaration of a logical le determines the type of its components but not their number. A logical le that is declared FILE OF x has components of type x . File operations can change the number of le components. A logical le does not exist outside the main program or routine that declares it.
Current Position Indexes Every logical le has a current position index that indicates either its current component, an end-of- le marker, or (in a text le) an end-of-line marker. This index is an integer| the ordinal number of the current component or marker. A le's current component is the component that the next I/O operation on that le will input or output. Figure 3-3 illustrates the relationship between current position index and current component. Figure 3-3.
Opening Files Except when using input and output les, your program must open les before it can use them. A call to a prede ned le-opening procedure has the following syntax and parameters. Syntax 8 reset > > < 9 > > = rewrite (logical > append > > > : ; open Parameters reset, rewrite, append, open logical le physical le open options 2 2 33 le , physical le , open options ) The names of the prede ned le-opening procedures. See Table 3-2 for more information on them.
Example 2 PROGRAM prog; VAR datafile : FILE OF integer; BEGIN open (datafile, 'file1'); open (datafile, 'file2'); END. {Logical file datafile is associated with physical file file1.} {Physical file file1 is closed. Logical file datafile is associated with physical file file2.} If logical le is not a program parameter, and physical le is not speci ed, logical le remains associated with its previously associated physical le.
Example 4 For this example, assume that the physical le file1 exists, but the physical le file2 does not. PROGRAM prog (file1,file2); VAR {Logical files file1 and file2 are program parameters.} file1, file2 : text; BEGIN rewrite(file1); {Logical file file1 is associated with the physical file file1.} rewrite(file2); {Logical file file2 is associated with a physical file file2. } END. A temporary, nameless physical le cannot be saved.
Table 3-2 summarizes the characteristics of the four prede ned le-opening procedures. Table 3-2.
Associate Procedure The prede ned procedure associate associates a logical le with an open physical le, and puts the current position index at the rst component. Syntax associate (logical le, le number, open options) Parameters logical le le number open options The name of the logical le. The le number of the open physical le. The physical le must have been opened with a direct call to an operating system routine or a non-Pascal routine.
Table 3-3 summarizes the characteristics of the prede ned procedure associate . Table 3-3. Characteristics of Associate Procedure * Type of File That it Can Open Any. State in Which it Opens File Speci ed in open Manner in Which File Can Be Accessed Either|De ned by characteristics of physical le. Purpose for Which it Opens File Input, output or both. Where it Puts Current Position Index Before rst component.
Figure 3-4 illustrates the e ect of the associate procedure on the open le whose le number is le num : Condition of le: After associate(examp le, le num,'READ') , the le is open in the read-only state and looks like this: Now examp le is open in the read-only state. Figure 3-4.
Example 1 This example applies to HP Pascal on the MPE/iX operating system only. For a description of the MPE/iX intrinsic FOPEN, refer to the MPE/iX Intrinsics Reference Manual . PROGRAM test; TYPE pac100 = PACKED ARRAY [1..
Example 2 This example applies to HP Pascal on the HP-UX operating system only. For descriptions of the HP-UX routines tmpnam and open, refer to the HP-UX Reference manual. PROGRAM test; TYPE pac100 VAR f buffer name mode fnum j e,g,h option = PACKED ARRAY [1..
Disassociate Procedure The prede ned procedure disassociate removes the logical-physical le association that was previously created with the standard procedure associate. As a result, you can no longer use the le f with Pascal input and output routines. Syntax disassociate (f) Parameters f A variable of type le. Normally, a le is closed on exit from the block in which it is declared. A disassociated le, however, remains open until it is closed with a direct call to an operating system routine.
Sequential Input/Output Sequential input/output is input/output that is performed with sequential les; that is, les whose current position indexes advance one component at a time. Sequential input comes from read-only les that the procedure reset opened. Sequential output goes to write-only les that the procedure rewrite or append opened. Table 3-4 summarizes the characteristics of the prede ned sequential input/output procedures. Table 3-4.
Example 1 PROGRAM prog; TYPE seqfile = FILE OF char; VAR f1,f2,f3 : seqfile; c1,c2 : char; BEGIN reset(f1); {Opens f1 for sequential input. First component of f1 becomes its current component.} c1 := f1^; {Assigns f1's first component to f1's buffer. Assigns f1's buffer (first component) to c1.} get(f1); {Advances f1's current position index. Second component of f1 becomes its current component.
get(f3); {Illegal -- append(f3) made f3 write-only.} f3^ := c1; {Assigns c1 to f3's buffer.} put(f3); {Assigns f3's buffer (c1) to f3's current (n+1st) component. Advances f3's current position index to position n+2, where its n+2nd component will be after write(f3,c2).} write(f3,c2); {Assigns c2 to f3's current (n+2nd) component. Advances f3's current position index to position n+3, where its n+3rd component will be.} END.
Example 2 PROGRAM prog; TYPE intfile = FILE OF integer; VAR f : intfile; x,y,z : integer; BEGIN reset(f); {Opens f for sequential input. First component becomes current component.} read(f,x); {Implicit reference to f's buffer -- deferred get from reset(f), above -- assigns current (first) component to buffer. Then read(f,x) assigns current (first) component to x. Second component becomes current component.
Table 3-5 gives the characteristics of the prede ned sequential le functions. Table 3-5. Characteristics of Sequential File Functions Function Returns: E ect on bu er: Eof Position if the current position index is Current position index (an integer). at the end-of- le marker; false otherwise (always true for a write-only le). True If eof returns false, and the bu er None. does not have a value, then eof assigns the value of the current component to the bu er; otherwise, no e ect.
Example 4 PROGRAM prog (input); {for this example, assume input is from terminal} TYPE readbuf = PACKED ARRAY [1..80] OF char; {for device read} VAR x : char; i : 1..100; a : readbuf; BEGIN i := 1; WHILE (NOT eof) AND (i <= 100) DO BEGIN readln(a); {perform device read} i := i + 1; END; END. By default, eof and readln apply to the standard text le input. The user running the program terminates input by pressing 4RETURN5. An input line can have up to 80 characters.
Textfile Input/Output Text le input/output is sequential input/output that is performed with text les (a subset of sequential les). The program reads text le input from read-only text les opened by the procedure reset, or from the standard text le input . The program writes text le output to write-only text les opened by the procedure rewrite or append, or to the standard text le output. Table 3-6 summarizes the characteristics of the prede ned text le input/output procedures. Table 3-6.
Example 1 PROGRAM prog (in,out); VAR in,out : text; w,x,y,z : char; BEGIN reset(in); rewrite(out); readln(in,x,y,z); write(out,x); overprint(out); writeln(out,y); page(out); writeln(out,z); prompt(out,'?'); readln(in,w); writeln(out,w); END.
Example 2 PROGRAM prog (infile,outfile,output); VAR infile, outfile : text; i : integer; c : char; BEGIN reset(infile); {Open infile for input} rewrite(outfile); {Open outfile for output} WHILE not(eof(infile)) DO BEGIN {If infile is not at end-of-file} IF eoln(infile) THEN BEGIN {but the current line of in has ended,} writeln(linepos(infile)); {print the number of characters read from the current line of infile,} readln(infile); {and advance to the next line.
Direct Input/Output Direct input/output is input/output that is performed with direct les; that is, les whose current position indices can be manipulated directly by the program. Direct input and output come from read-write les opened by the procedure open (they cannot be text les). Your program can use the same direct le for input and output. Table 3-8 summarizes the characteristics of the prede ned direct I/O procedures. (The I/O procedures in Table 3-3 also work on direct access les.) Table 3-8.
Example 1 PROGRAM prog; TYPE dirfile = FILE OF integer; VAR f : dirfile; i1,i2,i3,i4 : integer; BEGIN open(f); {Opens f for direct input/output} {READ TWO SPECIFIC COMPONENTS USING readdir AND read} readdir(f,50,i1); {Puts the current position index at component 50. Assigns component 50 to i1. Advances the current position index. Component 51 becomes the current component.} read(f,i2); {Assigns component 51 to i2.
The le-opening procedure open and the direct I/O procedures seek and writedir leave the bu er unde ned. After calling one of these procedures, your program must call get, read, or readdir before referencing the bu er implicitly (with a sequential I/O procedure) or explicitly. Table 3-9 summarizes the characteristics of the prede ned direct le functions. Table 3-9.
Example 2 PROGRAM prog; TYPE cfile = FILE OF char; VAR f : cfile; c : char; BEGIN reset(f); {Opens file for sequential input.} WHILE not(eof(f)) DO read(f,c); {Reads until eof is true.} read(f,c); {ERROR -- cannot read when eof is true. This statement would abort the program.} open(f); {Opens file for direct input/output.} IF lastpos(f) < maxpos(f) THEN BEGIN seek(f,lastpos(f)+1); {Puts current position index beyond last component, making eof true.} read(f,c); {ERROR -- cannot read beyond lastpos(f).
Closing Files When your program closes a le, it breaks the association between the logical le and the physical le; therefore, it cannot access the le or le bu er variable. It must reopen the le before attempting to operate on it in any other way, or it is a run-time error. One way to close a le is with the prede ned procedure close. A call to close has the following syntax and parameters.
Example PROGRAM prog; LABEL 9999; TYPE ftype = FILE OF integer; VAR f1 : ftype; PROCEDURE p; VAR f2 : ftype; BEGIN reset(f2); {Opens f2} goto 9999; {Closes f2 and f3} END; PROCEDURE q; VAR f3 : ftype; BEGIN open(f3); {Opens f3} p; {p never returns here} END; BEGIN rewrite(f1); {Opens f1} q; 9999 : reset(f1); {Closes and reopens f1} close(f1); {Closes f1} END.
Predefined Pascal Constants, Data Types, and 4 Modules This chapter: Gives the value of each prede ned constant. Gives the range of each prede ned data type. Explains in detail the prede ned data types bit16 , bit32 , bit52 , longint , and shortint , which are unique to HP Pascal. Explains each prede ned module.
Ranges of Predefined Data Types Table 4-1 gives the range and size of each prede ned data type available to HP Pascal. The data types are in alphabetical order and the sizes are in bits. To get a size in bytes, divide the number of bits by eight. Table 4-1. Ranges and Sizes of Predefined HP Pascal Types Range Type Unpacked Size in Bits Bit16 0..65535 16 Bit32 0..232 -1 32 Bit52 0..252 -1 64 Boolean FALSE or TRUE, where FALSE=0 and TRUE=1 8 Char ASCII character set 8 Integer -231 ..
Bit16 The prede ned data type bit16 is a subrange, 0..65535, that is stored in 16 bits. bit16 is a unique HP Pascal type because arithmetic operations on bit16 data are truncated to modulo 65535 when stored. To determine if a type T is assignment compatible with bit16 , treat bit16 as a subrange of integer: If variable v is of type T and variable b16 is of type bit16 , then the assignment b16 := v is legal if the value of v is within the range 0..65535.
Bit32 The prede ned data type bit32 is a subrange, 0..232 -1, that is stored in 32 bits. bit32 is a unique HP Pascal type because arithmetic operations on bit32 data are performed as unsigned 32-bit integers. Unsigned addition and subtraction do not over ow. Unsigned multiply can over ow unless the compiler option OVFLCHECK is used. Note that there are no bit32 constants in the compiler. Therefore, numbers in the range maxint + 1..232 -1 can not be expressed directly.
Example $standard_level 'hp_modcal'$ program prog_bit32(output); var i : integer; b : bit32; type rec = record f1 : bit32; end; $push; type_coercion 'conversion'$ const v_rec = rec[f1: hex('ffffffff')]; { bit32 constant field } $pop$ begin b := hex('ffffffff'); { compile-time error } i := -1; try b := i; recover ; { run-time error } $push; type_coercion 'conversion'; range off$ b := bit32(i) + 1; { zero is stored } b := bit32(hex('ffffffff')); $pop$ try i := b; recover ; try i := b + i; recover ; i := hex
Bit52 The prede ned data type bit52 is a subrange, 0..252 -1, that is stored in 64 bits. bit52 is a unique HP Pascal type because arithmetic operations on bit52 data are performed as unsigned 64-bit integers. Unsigned addition and subtraction do not over ow. Unsigned multiply may over ow. The compiler option OVFLCHECK has no e ect. Note that there are no bit52 constants in the compiler. Therefore, numbers in the range maxint + 1..252 -1 can not be expressed directly.
$push; type_coercion 'conversion'; range off$ b := bit52(i) + 1; { zero is stored } b := bit52(hex('ffffffff')); $pop$ try i := b; recover ; try i := b + i; recover ; i := hex('ffffffff'); { run-time error } { b and i are converted to longint and are } { too big to fit back into i } { both b and i now have all bits on } { the following never prints since i is sign extended to longint and b is zero extended to longint } $push; type_coercion 'conversion'$ if longint(i) = b then writeln('equal'); $pop$ end.
Shortint The prede ned data type shortint is an integer in the range -32768..32767 that is stored in 16 bits. (In contrast, if you declare a variable to be in that range, it is stored in 32 bits.) The type shortint has the following uses: If you want to access an external non-Pascal routine that has a formal parameter of a type whose range is -32768..32767, and uses 16-bits of storage, you can declare a corresponding formal Pascal parameter of type shortint , and it will be compatible.
Longint The prede ned data type longint is an integer in the range -263 ..263-1 that is stored in 64 bits. The compiler option OVFLCHECK has no e ect on 64 bit multiply. Note that there are no longint constants in the compiler. Therefore, numbers outside of the range minint .. maxint can not be expressed directly. The compiler option TYPE COERCION must be used with a run-time computation. If the numbers are constants, they must be typed coerced to longint so they do not integer over ow.
Predefined Modules On both the MPE/iX and HP-UX operating systems, HP Pascal has these prede ned modules: stdinput stdoutput On the HP-UX operating system only, HP Pascal has these additional prede ned modules: stderr arg pas_hp1000 In its import declaration section, your program can import any or all of the prede ned modules supported by the operating system on which it runs. This section shows the actual declarations in the prede ned modules for your information only.
stderr The stdrrr module contains the declaration for the prede ned global variable (standard text le) stderr . It allows an independent module (which has no program header) to use stderr . Importing the stderr module into an independent module is the same as declaring stderr in the program header of a program. The content of the prede ned module stderr is: VAR stderr : text; The prede ned module stderr is only available on the HP-UX operating system.
arg The arg module contains routines that access HP-UX command line arguments. (It also contains the types that these routines use, but only the routines are presented here.) The routines in the prede ned module arg are: Routine E ect and Declaration argc Returns the total number of arguments to the program (the name of the program is considered to be the rst argument). Declaration: FUNCTION argc : integer; argv Returns a pointer to an array of pointers to the actual arguments.
pas hp1000 The pas_hp1000 module contains routines that help you migrate Pascal/1000 programs to HP Pascal/HP-UX on the HP 9000 Series 700 or 800 machine. They emulate user-callable routines in the Pascal/1000 run-time library. The routines in the prede ned module pas_hp1000 are: Routine E ect and Declaration pas_init_hp1000_args Only for programs running under the RTE shell on the HP 9000 Series 700 or 800.
pas_filenamr Returns the name of the physical le associated with the speci ed logical le. Declaration: FUNCTION pas_filenamr (ANYVAR f : text) : pas_nametype; pas_timestring Returns the time of day as a 26-character PACKED ARRAY OF CHAR. Declaration: PROCEDURE pas_timestring (ANYVAR f : pas_timestringtype); pas_traceback Produces a stack trace of the program and writes it to stderr.
5 Allocation and Alignment This chapter: De nes allocation, alignment, and packing algorithm . Shows how unpacked and packed variables are allocated and aligned. Tells how entire arrays and records are allocated and aligned (whether they are unpacked, packed, or crunched). Shows how array elements and record elds are allocated and aligned when they are unpacked, packed, and crunched. Explains how enumeration and subrange types are related and shows how they are allocated and aligned.
Byte boundaries are broken where a variable crosses them. Bit boundaries are omitted where a variable crosses them. A space that is allocated to a variable contains the variable's name. If the name does not t the space, it is printed outside, with an arrow pointing to the space. Example The variables a and b occupy one bit each, c occupies six bits, d and e occupy two bytes each, f occupies three bytes, and g occupies eight bytes.
Allocation, Alignment, and Packing Algorithm Allocation is the assignment of memory to variables. When the compiler assigns one byte of memory to the variable x, you can say that both the byte and x are allocated (the byte is allocated to x, and x is allocated one byte of memory). Alignment refers to the position at which a variable's share of memory begins. There are several types of alignment. Bit-aligned: If the byte that the compiler allocates to x can begin on a bit boundary.
Unpacked Variables An unpacked variable is either not part of an array or record, or it is part of an unpacked array or record. Table 5-1 shows how the HP Pascal packing algorithm allocates and aligns unpacked variables of each HP Pascal type. The variable types are in alphabetical order. Sections that Table 5-1 references are in this chapter. Table 5-1.
Packed Variables A packed variable is the element of a packed array or the eld of a packed record. Table 5-2 shows how the HP Pascal packing algorithm allocates and aligns packed variables of each HP Pascal type. The variable types are in alphabetical order. The sections that Table 5-2 references are in this chapter. Table 5-2.
Table 5-2. Allocation and Alignment of Packed Variables (HP Pascal Packing Algorithm) (continued) Variable Type Alignment Record See \Records" for information on entire record and \Packed Records" for information on elds. Set See \Sets" Shortint 5-6 Allocation 2 bytes String See \Strings" Subrange See \Enumerations and Subranges".
Arrays Arrays are stored in row-major order . This means that an array is stored a row at a time, rather than a column at a time (column-major order ). Example VAR a : ARRAY [1..2,1..3] OF char; Row-major order: Column-major order: The HP Pascal packing algorithm uses this formula to allocate an array: number of elements * space for one element The space for one element depends upon the array element type and whether the array is unpacked, packed, or crunched.
Records A record allocation is the sum of the allocations of the elds in the xed part and (if the record has them) the allocations of the tag eld and the largest eld in the variant part, plus trailing bits. Field allocation depends on eld type and whether the record is unpacked, packed, or crunched. The same factors determine eld alignment.
A variable of type Rec is allocated 16 bytes. The TRUE and FALSE variants are aligned like this: TRUE Variant FALSE Variant Sometimes you can reduce the space that a record takes by declaring its elds in di erent order.
Example VAR upr1 : RECORD bf : Boolean; pf : 0..32767; cf : char; END; upr2 : RECORD bf : Boolean; cf : char; pf : 0..32767; END; The only di erence between the variables upr1 and upr2 is the order of their elds. The variable upr1 takes six bytes: Because pf must be 2-byte-aligned, it cannot start in the second byte. The extra byte after cf is allocated because the most restricted element, pf, is 2-byte-aligned.
Example VAR pr1 : PACKED RECORD srf : 0..32; b : Boolean; pf : 0..32767; cf : char; END; pr2 : PACKED RECORD srf : 0..32; b : Boolean; cf : char; pf : 0..32767; END; The only di erence between the variables pr1 and pr2 is the order of their elds.
Packed Arrays Table 5-3 shows how the HP Pascal packing algorithm allocates and aligns the elements of a packed array. The element types are in alphabetical order. Table 5-3. Allocation and Alignment of Packed Array Elements (HP Pascal Packing Algorithm) Allocation Element Type Alignment Anyptr 8 bytes 4-bytes Array, crunched Same as crunched array that is not part of an array or record (see Table 5-9); then padded to the nearest byte.
Table 5-3. Allocation and Alignment of Packed Array Elements (HP Pascal Packing Algorithm) (continued) Allocation Element Type Alignment Record, packed Fields are allocated by type, and record is padded to the alignment boundary. Largest alignment boundary of any eld, or byte, whichever is larger. Record, unpacked Fields are allocated by type, and record is padded to the alignment boundary. Largest alignment boundary of any eld. Set See \Sets". Shortint 2 bytes 2-byte Strings See \Strings".
Example VAR uba : ARRAY [1..3] OF Boolean; pba : PACKED ARRAY [1..3] OF Boolean; The array uba takes three bytes: The array pba takes three bits: If an array is not within a crunched structure, the compiler aligns the entire array on the same boundary as its rst element, or on a byte boundary. Declaring an array PACKED has no e ect on its elements if the elements are unpacked structures.
Packed Records Table 5-4 shows how the HP Pascal packing algorithm allocates and aligns the elds of a packed record. The eld types are in alphabetical order. Table 5-4. Allocation and Alignment of Packed Record Fields (HP Pascal Packing Algorithm) Allocation Field Type Field Alignment Anyptr 8 bytes 4-byte Array, crunched Minimum number of bits required to represent any value of the element type. Bit Array, packed Use formula in \Arrays" section and then pad to alignment boundary.
Table 5-4. Allocation and Alignment of Packed Record Fields (HP Pascal Packing Algorithm) (continued) Allocation Field Type Field Alignment Record, packed Fields are allocated by type, and record is padded to the alignment boundary. Largest alignment of any eld or byte, whichever is larger. Record, unpacked Fields are allocated by type, and record is padded to the alignment boundary. Largest alignment of any eld. Set See \Sets". Shortint 2 bytes 2-byte Strings See \Strings".
Example TYPE ua = ARRAY [1..4] OF Boolean; ur1 = RECORD i : integer; c : char; END; VAR ur2 : RECORD c : char; a : ua; r : ur1; END; pr : PACKED c : a : r : END; RECORD char; ua; ur1; The elds in ur2 and pr are allocated and aligned identically.
Crunched Arrays and Records Crunched packing, a systems programming extension, packs a record or array as tightly as possible: it bit-aligns every eld or element. Table 5-5 shows how the HP Pascal packing algorithm allocates elements of crunched arrays or elds of crunched records. If a type is not in Table 5-5, a crunched array or record cannot have elements or elds of that type. Table 5-5.
If any element can be negative, an extra bit is allocated for the sign; for example, this record takes three bits: CRUNCHED RECORD f : -4..3; END; Example A record that is de ned: TYPE u_rec = RECORD {4-byte aligned} a,b : Boolean; c : char; d : minint..maxint; e : Boolean; END; is allocated and aligned this way: A record that is de ned: TYPE p_rec1 a,b c : d : e : END; = PACKED RECORD {Byte-aligned} : Boolean; char; minint..
A record that is de ned: p_rec2 a,b c : d : e : END; = PACKED RECORD {4-byte-aligned} : Boolean; char; integer; Boolean; is allocated and aligned this way: A record that is de ned: TYPE c_rec1 a,b c d e END; = : : : : CRUNCHED RECORD Boolean; char; minint..
Crunched Sets Table 5-6 shows how the HP Pascal packing algorithm allocates and aligns a crunched set when it is the element of an array or the eld of a record. Table 5-6.
Enumerations and Subranges HP Pascal allocates and aligns variables of enumeration and subrange types the same way. An enumeration of n elements and the subrange 0..n -1 are equivalent. The allocation and alignment are based on the values of the subrange or the ordinal value of the enumeration. Example TYPE enum_type = (red,blue,yellow); {enumeration of 3 elements} subr_type = 0..2; {subrange 0..
Example The value zero is always included in the subrange when the minimum number of bits is calculated. TYPE enum_type subr_type1 subr_type2 subr_type3 subr_type4 VAR enum_var subr_var1 subr_var2 subr_var4 = = = = = : : : : (red,blue,yellow); 1..300; 1..66000; 100000..100010; -1..
Packed Array Elements of Enumeration or Subrange Types A packed enumeration or subrange variable requires the minimum number of bits needed to represent its values in a record. It is bit-aligned. If the enumeration or subrange variable belongs to a packed array, the HP Pascal packing algorithm allocates it the smallest power of two bits that is greater than or equal to the number of bits it requires, and aligns it on that boundary.
Example TYPE direction = (north,south,east,west); day = (sun,mon,tues,wed,thurs,fri,sat); VAR pa1 = PACKED ARRAY [1..5] OF direction; pa2 = PACKED ARRAY [1..5] OF day; Each element of the array pa1 requires two bits. Two is a power of two, so each element is allocated two bits. The entire array occupies 10 bits. It is allocated two bytes: Each element of the array pa2 requires three bits. The smallest power of two that is greater than or equal to three is four, so each element is allocated four bits.
Packed Record Elements of Enumeration or Subrange Types If the variable belongs to a packed record, the HP Pascal packing algorithm allocates it as many bits as it requires, and bit-aligns it. Example TYPE day = (sun,mon,tues,wed,thurs,fri,sat); VAR r : PACKED RECORD f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11 : day; END; Each eld of the record r requires three bits. The entire record occupies 33 bits. It is allocated ve bytes: Note Subranges can cross 4-byte boundaries, but code is less e cient when they do.
Files When your program declares a le, the compiler allocates space for the le control block and the le bu er variable. The amount of space allocated to each is xed by the packing algorithm. The le is 8-byte-aligned. Table 5-9 shows how the HP Pascal packing algorithm allocates le components for text les and nontext les. Table 5-9.
Sets The HP Pascal packing algorithm allocates sets in units called set chunks . Set chunk size depends on the number of bits required to represent the set and whether the set is unpacked, packed, or crunched. The number of bits required to represent the set is determined by the formula: bits required for set = ord(largest value in set) ord(smallest value in set) + 1 Table 5-10 shows how the HP Pascal packing algorithm determines set chunk size. Table 5-10.
Example 1 VAR days = SET OF (sun,mon,tues,wed,thurs,fri,sat); months = PACKED SET OF (ja,f,mr,ap,ma,jn,jl,au,s,o,n,d); set_33 = SET OF (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11, e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22, e23,e24,e25,e26,e27,e28,e29,e30,e31,e32,e33); p_set_33 = PACKED SET OF (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11, e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22, e23,e24,e25,e26,e27,e28,e29,e30,e31,e32,e33); The set days has seven elements and requires seven bits.
Example 2 VAR s : SET OF -7..18; The set s is unpacked, so it has a 32-bit set chunk (see Table 5-10). The upper bound of the subrange is 18, so upper bound set chunk number is zero ( oor(18/32=0)). The lower bound of the subrange is -7, so lower bound set chunk number is -1 ( oor(-7/32)=-1). The set s is allocated two set chunks ((0-(-1))+1=2). Each set element is represented by one bit, like this: To minimize storage space, avoid base types that are small subranges that overlap set chunk boundaries.
Strings A string is allocated four bytes for its current length (an integer), one byte per character, and one \housekeeping" byte. The number of characters is the string's declared maximum length. The \housekeeping" byte is only accessible to some of the standard string functions. The HP Pascal packing algorithm aligns strings on 4-byte boundaries in all structures. Because the current length (an integer) is allocated four bytes, eight bytes is the smallest possible string allocation.
The string s2 takes 12 bytes: (((4 + 7 + 1) + 3) DIV 4) * 4 = (15 DIV 4) * 4 = 3 * 4 = 12 The allocation is: 5-32 Allocation and Alignment
6 Dynamic Variables A dynamic variable is allocated during program execution. In contrast, a global, module, or local variable is allocated when the block containing its declaration is activated. Table 6-1 shows the di erences between dynamic and static variables. Table 6-1. Dynamic versus Static Variables Dynamic Variable Global or Module Variable Yes Local Variable Declared? No Yes Referenced by Pointer (which is Name declared). Allocated During execution, with the function new .
GLOBALANYPTR Variables The pointer type globalanyptr is assignment compatible with every pointer type and the value nil . Anyptr is another name for the same type, provided for compatibility with older Pascals. This manual uses the term globalanyptr exclusively, but anyptr can be substituted wherever it appears. A variable of type globalanyptr is not bound to a speci c pointer type.
PROGRAM prog (input); TYPE iptr = ^integer; rec = RECORD f1, f2 : real; END; rptr = ^rec; VAR v1, d1 : iptr; v2, d2 : rptr; anyv : globalanyptr; b : Boolean; BEGIN {Initialize v1 and v2} new(v1); new(v2); v1^ := 0; WITH v2^ DO BEGIN f1 := 0; f2 := 0; END; {Set anyv to v1 or v2, depending on b} read(b); IF b THEN anyv := v1 ELSE anyv := v2; {You cannot dereference anyv, because it is a globalanyptr.
LOCALANYPTR Variables The pointer type localanyptr is similar to the type globalanyptr (or anyptr ) in that it is assignment compatible with every pointer type and the value nil . A localanyptr variable di ers from a globalanyptr variable in that the compiler allocates it 32 bits instead of 64 bits. If your program does not use extended address pointers, you can save space by using localanyptr instead of globalanyptr .
PROGRAM prog (input); TYPE iptr = ^integer; rec = RECORD f1, f2 : real; END; rptr = ^rec; VAR v1, d1 : iptr; v2, d2 : rptr; anyv : localanyptr; b : Boolean; BEGIN {Initialize v1 and v2} new(v1); new(v2); v1^ := 0; WITH v2^ DO BEGIN f1 := 0; f2 := 0; END; {Set anyv to v1 or v2, depending on b} read(b); IF b THEN anyv := v1 ELSE anyv := v2; {You cannot dereference anyv, because it is a localanyptr.
New Procedure The prede ned procedure new takes a pointer variable as a parameter, allocates a variable of the type that the pointer references, and \points" the pointer at the new variable (that is, new assigns the address of the new variable to the pointer). The program can then access the new variable by dereferencing the pointer.
CASE mstat : marital_status OF single : (divorced, widowed, engaged : Boolean); married : (how_many_times: 1..10; how_long_this_time : 1..
If the new variable is a record with nested variant elds, you can specify a tag for each variant. If you do, you must specify them in the order that they are declared, and you cannot leave gaps in the sequence. Example 3 In this program, the declaration order of the tag elds is obviously t1, t2 or t1, t3.
Example 4 This program is semantically equivalent to the program in the immediately preceding example (Example 3), and the declaration order of the tag elds is the same.
You do not have to specify tag elds. If you omit them, new allocates enough space for the largest possible variant, wherever there are variants. This allocation is the default allocation for variables of the particular record type. If you use tags to specify smaller variants, new allocates less than the default allocation to the new variable. The advantage to using tags is that you save space. The disadvantage is that the new variable cannot appear in an assignment statement, or as an actual parameter.
default^.t := a; default^ := small^; {illegal} default^.t := b; default^ := large^; {illegal} small2^ := small^ {still illegal even though the spaces are allocated } {using the same tag } small2^.a1 := small^.a1 {legal} small2^.a2 := small^.a2 {legal} p(small^); {illegal} p(large^); {illegal} p(default^); {legal} END. The pointer parameter of new can belong to a PACKED structure. Example 6 PROGRAM prog; TYPE ptr = ^integer; pa = PACKED ARRAY [1..
Dispose Procedure The prede ned procedure dispose takes a pointer variable as a parameter and deallocates the dynamic variable that it references. When the variable is deallocated, it is inaccessible, and the pointer is unde ned. Files in the deallocated space are closed. The procedure new can only reallocate the space that dispose has deallocated if the program contains the compiler option HEAP DISPOSE.
PROCEDURE r (VAR z : recptr); PROCEDURE s; BEGIN dispose(v4); {illegal -- v4^ is the actual parameter for z} END; BEGIN s; END; BEGIN new(v1); WITH v1^ DO BEGIN f1 := 0; f2 := 0; f3 := 0; END; dispose(v1); dispose(v1); {illegal -- v1 is undefined} new(v2); dispose(v2); new(v3); v3 := nil; dispose(v3); {illegal -- v3 is nil} new(v4); p(v4^); new(v4); r(v4); {s (within r) disposes r's actual parameter v4, which is illegal} new(v4); new(v5); WITH v4^,v5^ DO BEGIN f1 := 1; f2 := 2; f3 := 3; q; {illegal -- q d
If you specify tags when you allocate a variable with new , you must specify the same tags in the same order when you deallocate the variable with dispose .
Mark and Release Procedures The prede ned procedure mark takes a pointer variable p as a parameter, marks the state of the heap, and sets the value of p to specify that state. The pointer variable p is called a mark (once a pointer variable becomes a mark, you cannot dereference it). You can allocate heap space beyond the mark, and then deallocate that space with the prede ned procedure release .
Example 1 PROGRAM prog; TYPE ftype = FILE OF integer; ptype1 = ^ftype; ptype2 = ^integer; VAR fptr : ptype1; iptr1, iptr2, m, iptr3, iptr4: ptype2; BEGIN new(iptr1); new(iptr2); {Allocate heap space to iptr1^} {Allocate heap space to iptr2^} iptr1^ := 0; iptr2^ := 0; mark(m); {Mark the heap with m} new(iptr3); new(iptr4); new(fptr); {Allocate heap space to iptr3^} {Allocate heap space to iptr4^} {Allocate heap space to fptr^, a file} iptr3^ := 0; iptr4^ := 0; reset(fptr^); {Open fptr^} release(m);
The parameter of mark (the mark) can be any pointer variable. The parameter of release must be a mark|a pointer variable whose current value was assigned by the mark procedure. It is an error to call release with a pointer whose current value was not assigned by the mark procedure.
If you set several marks, and release one of them, those set after it are also released. Example 3 PROGRAM prog; TYPE ptr = ^integer; VAR m1, i1, j1, k1, m2, i2, i3, j2, j3, k2, k3 : ptr; BEGIN new(i1); new(i2); new(i3); mark(m1); new(j1); new(j2); new(j3); mark(m2); new(k1); new(k2); new(k3); release(m1); {deallocates j1,j2,j3,k1,k2,k3; releases m1 and m2} release(m2); {illegal -- m2 is undefined because it was released with m1} END.
P getheap and P rtnheap Procedures The procedures p getheap and p rtnheap are intrinsics in the Pascal run-time library. Any program that runs on the operating system can call them, regardless of the language in which it is written. (For more information on intrinsics, Chapter 10). The procedure p getheap tries to allocate a region of heap space of a speci ed size and alignment.
The procedure p rtnheap tries to deallocate a region of heap space that the p getheap procedure allocated. If it succeeds, it assigns its VAR Boolean parameter the value true . If it fails, it assigns its VAR Boolean parameter the value false . P rtnheap does not close les residing in the region allocated by p getheap .
Example 1 $STANDARD_LEVEL 'HP_MODCAL'$ PROGRAM prog; TYPE intpointer = ^integer; VAR b : Boolean; i : integer; ptr1, ptr2 : intpointer; PROCEDURE p_getheap (VAR regptr regsize alignment VAR ok : : : : intpointer; integer; integer; Boolean); EXTERNAL; PROCEDURE p_rtnheap (VAR regptr regsize alignment VAR ok : : : : intpointer; integer; integer; Boolean); EXTERNAL; BEGIN p_getheap(ptr1,40,4,b); ptr2 := ptr1; {allocate a 40-byte region} {save ptr1 for later call to p_rtnheap} FOR i := 1 TO 10 DO BEGIN
The procedures p getheap and p rtnheap are independent from the procedures mark, release, new, and dispose .
release(ptr2); ptr1^ := 0; {The 4-byte region was not deallocated, and the values in it are still accessible} p1^ := p2^ + p3^; {illegal -- p1, p2, and p3 were deallocated} END. Getheap and Rtnheap Procedures The procedures getheap and rtnheap are intrinsics in the Pascal run-time library. They are provided only for compatibility with existing source code that was written for the MPE V operating system and only exists on MPE/iX.
7 Parameters This chapter explains: The di erences between value and reference parameters. ANYVAR and READONLY reference parameters (which are HP Pascal system programming extensions). Conformant array parameters. Routines (procedures and functions) as parameters. Congruent parameter lists. Hidden parameters (which a ect debugging and interfacing with external non-Pascal routines).
A reference parameter is passed by reference; that is, the address of the actual parameter is passed to the routine and associated with the formal parameter. If the routine changes the value of the formal parameter, it changes the value of the actual parameter. An actual reference parameter must be a variable access (a variable name or the name of a component of an unpacked structure). HP Pascal without system programming extensions has one kind of reference parameter: VAR.
Table 7-1 compares the four kinds of formal parameters. Table 7-1.
ANYVAR Parameters An ANYVAR parameter is similar to a VAR parameter in that its actual parameter is passed by reference and must be a variable access. If the routine changes the value of a formal ANYVAR parameter, it changes the value of the actual parameter. An ANYVAR parameter di ers from a VAR parameter in that its actual parameter can be of any type. HP Pascal treats the actual parameter as if it were of the data type of the formal ANYVAR parameter. This is implicit type coercion.
Example 2 PROGRAM prog; VAR c : PACKED ARRAY [1..2] OF char; j : shortint; i : integer; PROCEDURE show_anyvar_alignment (ANYVAR anyvar_parm : shortint); EXTERNAL; BEGIN show_anyvar_alignment(c); {illegal -- must be 2-byte-aligned} show_anyvar_alignment(j); {legal} show_anyvar_alignment(i); {legal -- references high-order 2 bytes} END. When HP Pascal passes an actual parameter to a formal ANYVAR parameter, it also passes a hidden parameter.
READONLY Parameters A READONLY parameter is similar to a value parameter in that the routine cannot directly modify its actual parameter, which can be a constant, an expression, or a variable. READONLY di ers from a value parameter in that the routine cannot modify the formal parameter: you cannot assign a value to the formal READONLY parameter, pass it to a VAR or ANYVAR parameter, or pass it to either of the prede ned functions addr , baddress , or waddress .
Conformant Array Parameters A conformant array parameter is a formal array parameter de ned by a conformant array schema (the syntax appears in the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual ). Its actual parameter must be an array variable that conforms to the schema. An array variable conforms to a conformant array schema if all of the following are true: The variable and the schema are both PACKED, or neither is PACKED.
Parameter no1 no2 no3 no4 no5 Why var1 Does Not Conform to Schema Schema is PACKED and var1 is not PACKED. Index types of var1 and schema are not compatible. Bounds of index type of var1 are not within bounds of index type of schema. Element types of var1 and schema are di erent. Schema speci es two dimensions, and var has only one dimension. Like array declarations, schemas can specify dimensions in syntactically di erent but structurally equivalent ways. Example 2 VAR var1 : ARRAY [3..5,1..
Example 3 TYPE itype = 0..20; VAR v : ARRAY [0..10] OF integer; PROCEDURE p (x : ARRAY [lb..ub : itype] OF integer); BEGIN p(v); END; The conformant array schema x is a formal parameter, so its bounds, lb and ub are read-only formal parameters. The array v is the actual parameter for x. The lower bound of v, zero, is the actual parameter for lb. The upper bound of v (10) is the actual parameter for ub.
Routines as Parameters A routine can be a parameter in two ways: it can be a routine parameter (a procedure or function parameter, as de ned by ANSI Pascal), or it can be a routine that is passed as a parameter (as de ned by the systems programming extensions of HP Pascal). Table 7-2 di erentiates between routine parameters and parameters of routine types. Table 7-2.
Example PROGRAM prog; VAR s : char; PROCEDURE p (PROCEDURE procparm1 (a,b : integer); {formal procedure parameter} FUNCTION funcparm1 (c : integer) : char); VAR ch : char; BEGIN procparm1(1,2); ch := funcparm1(3); END; FUNCTION f (PROCEDURE procparm2; {formal procedure parameter} FUNCTION funcparm2 : integer); {formal function parameter} VAR i : integer; BEGIN procparm2; i := funcparm2; END; PROCEDURE actual_procparm1 (x,y : integer); {user-defined procedure} BEGIN . .
Parameters of Routine Types Parameters of routine types (procedure or function types) are like parameters of other user-de ned types. They are de ned in the formal parameter lists of other routines, but their types|routine types|are de ned in type declaration sections. The types must be declared rst (see the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation, for more information on declaring routine types).
$STANDARD_LEVEL 'HP_MODCAL'$ PROGRAM prog; TYPE proctype1 functype1 proctype2 functype2 = = = = PROCEDURE (a,b : integer); FUNCTION (c : integer) : char; PROCEDURE; FUNCTION : integer; VAR s : char; PROCEDURE p (procparm1 : proctype1; funcparm1 : functype1); VAR ch : char; BEGIN call(procparm1,1,2); ch := fcall(funcparm1,3); END; FUNCTION f (procparm2 : proctype2; funcparm2 : functype2); VAR i : integer; BEGIN call(procparm2); i := fcall(funcparm2); END; PROCEDURE actual_procparm1 (x,y : integer); BEGIN
PROCEDURE actual_procparm2; BEGIN . . . END; FUNCTION actual_funcparm2 : integer; BEGIN . . . END; BEGIN {prog} p(addr(actual_procparm1), addr(actual_funcparm1)); s := f(addr(actual_procparm2), addr(actual_funcparm2)); END.
Variables of Routine Types Variables of routine types (procedure and function types) can be actual parameters for formal parameters of routine types (function and procedure types, respectively). See \Parameters of Routine Types". The values that you can assign to a function variable are: The value nil . The value returned by the prede ned function addr when you call it with the name of an appropriate function (appropriate is de ned below).
Example 1 This program uses the prede ned function addr to assign appropriate functions to a variable of function type and appropriate procedures to a variable of procedure type.
Example 2 This program declares procedures and procedure variables at di erent levels and assigns each procedure visible to each variable. The comments tell you which assignments are illegal and why.
Example 3 This program uses functions whose return types are function and procedure types to assign values to routine variables. The comments tell you which assignments are illegal and why.
Example 4 Unde ned routine variables are undetectable, and cause unpredictable results. The following program avoids problems caused by such unde ned variables by assigning the value nil to those variables.
Call Procedure The prede ned procedure call executes a call to the procedure speci ed by a procedure variable. Its parameters are a procedure variable and the actual parameters with which the procedure is to be called. Just as a pointer is dereferenced with ^, a procedure variable is dereferenced with call . Example $STANDARD_LEVEL 'EXT_MODCAL'$ PROGRAM prog; TYPE proctype = PROCEDURE (x,y : integer); VAR procvar : proctype; PROCEDURE p (a,b : integer); BEGIN . . .
Fcall Function The prede ned function fcall executes a call to the function speci ed by a function variable. Its parameters are a function variable (which speci es the function to be called) and the actual parameters with which the function is to be called. Just as a pointer is dereferenced with ^, a function variable is dereferenced with fcall .
Congruent Parameter Lists Two parameter lists are congruent if they have the same number of parameters, and if parameters in the same positions are equivalent. Two parameters are equivalent if any one of the following is true: They are value parameters of identical type. They are VAR parameters of identical type. They are parameters of procedure type with congruent parameter lists. They are parameters of function type with congruent parameter lists and identical result types.
Example 1 This program uses procedure parameters whose own parameter lists do not include conformant array parameters, function parameters, or other procedure parameters. PROGRAM prog; VAR r : real; PROCEDURE proc (PROCEDURE procvar (x : integer; VAR y : char)); BEGIN . . END; FUNCTION func (PROCEDURE pvar (x : integer)) : real; BEGIN . . END; PROCEDURE PROCEDURE PROCEDURE PROCEDURE PROCEDURE p1 p2 p3 p4 p5 BEGIN proc(p1); proc(p2); proc(p3); proc(p4); proc(p5); r r r r r END.
Example 2 This program uses function parameters whose own parameter lists do not include conformant array parameters, procedure parameters, or other function parameters. PROGRAM prog; VAR r : real; PROCEDURE proc (FUNCTION funcvar : (a,b,c : char) : Boolean); BEGIN . . . END; FUNCTION func (FUNCTION fvar : (a,b,c : char) : real) : real; BEGIN . . .
Example 3 This program uses a procedure parameter, procvar. The parameter list of procvar includes conformant array parameters, w and x, another procedure parameter, p1, and another function parameter, f1. PROGRAM prog; TYPE itype = 1..10; VAR a : ARRAY [1..6] OF integer; b : PACKED ARRAY [3..7] OF integer; PROCEDURE alpha (m : integer); EXTERNAL; FUNCTION beta (n : real) : integer; EXTERNAL; PROCEDURE p (VAR cvar1 : ARRAY [a..b : itype] OF integer; cvar2 : PACKED ARRAY [c..
Example 4 PROGRAM prog (output); PROCEDURE outer2 (PROCEDURE procvar (v : integer)); BEGIN {outer2} procvar(7); END; {outer2} PROCEDURE outer1 (p : integer); VAR x : integer; PROCEDURE inner (i : integer); BEGIN {inner} writeln(x,i,x+i,p); END; {inner} BEGIN {outer1} x := 5; outer2(inner); END; {outer1} BEGIN {prog} outer1(2); END.
Hidden Parameters Hidden parameters do not appear in formal or actual parameter lists, but are nevertheless passed to routines. They are always integers. You must know about hidden parameters in order to debug your program at the assembly language level, and you must include them in the parameter lists of external routines that are not written in Pascal. (For information, see Chapter 9.
Example 1 $STANDARD_LEVEL 'HP_MODCAL'$ PROGRAM prog; VAR x,y,z : integer; PROCEDURE p ( BEGIN {p} . . . END; {p} a ANYVAR b, c d ANYVAR e : : : : integer; integer; integer; integer); BEGIN {prog} x := 2; y := 3; z := 5; p(1,x,y,4,z); END.
Example 2 $STANDARD_LEVEL 'EXT_MODCAL'$ PROGRAM prog (output); TYPE t1 = ARRAY [1..20] OF integer; t2 = ARRAY [1..
Extensible Parameter List If a routine has an extensible parameter list, its physical parameter order begins with a hidden parameter. The value of the hidden parameter is the number of actual parameters passed, excluding hidden parameters. This value is always greater than or equal to the number of nonextension parameters, because the routine must have a value for each of them.
Multidimensional Conformant Array Parameters If a routine has multidimensional conformant array parameters, its physical parameter order contains one hidden parameter for each nested conformant array element. In the physical parameter order, each hidden parameter follows the actual parameters for the bounds of its corresponding dimension. The value of each hidden parameter is the size of its corresponding dimension. These hidden parameters are not accessible to the programmer.
Routine Parameters If a routine has routine parameters, its physical parameter order contains one hidden parameter for each routine parameter. (This is not true of parameters that are routine variables.) These hidden parameters are at the end of the physical parameter order, in the same order as their corresponding routine parameters. The value of a hidden parameter for a speci c routine parameter is the static link. This static link allows access to the variables and parameters of the enclosing routines.
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN Including hidden parameters ( highlighted ), the physical parameter order that appears in the preceding program as p(actual1,actual2,actual3,100) is: Procedure label for procedure actual1 Procedure label for procedure actual2 Function label for function actual3 Value 100 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN Static link for procedure actual1 (nil) NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
EXTERNAL SPL VARIABLE The EXTERNAL SPL VARIABLE directive causes the compiler to pass a hidden parameter that speci es the presence of parameters. The hidden parameter is a 32 bit integer with the mask right justi ed as required by SPL/V. Example program prog1; var count : integer; procedure ext_spl(p1, p2, p3 : integer); external spl variable; begin ext_spl(1,,count); ext_spl(1); end.
8 Procedure Options Procedure options, which immediately follow a routine head, can specify: That the routine has an extensible parameter list|that is, one or more optional parameters (EXTENSIBLE option). Default values for formal parameters, allowing their actual parameters to be left out of actual parameter lists (DEFAULT PARMS option). That formal ANYVAR parameters do not have the usual hidden parameters that specify their sizes (UNCHECKABLE ANYVAR option).
EXTENSIBLE The EXTENSIBLE routine option identi es a procedure that has an extensible parameter list. An extensible parameter list has a xed number of nonextension parameters and a variable number of extension parameters. The integer n after the keyword EXTENSIBLE speci es that the rst n parameters in the formal parameter list are nonextension parameters (n can be zero). Any other parameters are extension parameters. A nonextension parameter is required.
BEGIN b := b := b := b := f(36,45); f(20); f(,66); f; p; p(); p(100); p(250,13); p(,60); q(5,9.4); q(4,3.0,'z'); q(7,8.8,'w',55); q(2,1.1,,93); q(,); q(,,45); q(400,,22); END. {legal} {illegal} {illegal} {illegal} {legal} {legal} {legal} {legal} {illegal} {legal} {legal} {legal} {illegal} {illegal} {illegal} {illegal} Both parameters of the function f are nonextension parameters. Every call to f must specify actual parameters for them. Both parameters of the procedure p are extension parameters.
Example The procedure p has two nonextension parameters: PROCEDURE p (n1,n2 : integer) OPTION EXTENSIBLE 2; BEGIN {p} . . END; {p} The program oldprog calls the procedure p: PROGRAM oldprog; PROCEDURE p (n1,n2 : integer) OPTION EXTENSIBLE 2; EXTERNAL; BEGIN p(1,2); END. The procedure p is updated and two new parameters are added. It uses the prede ned function haveextension to determine whether its two new extension parameters were passed to it.
Note A routine with n extensible parameters is not the same as a procedure with n parameters that does not have EXTENSIBLE, even if the two procedures are otherwise identical.
DEFAULT PARMS The DEFAULT PARMS procedure option speci es default values to be assigned to formal parameters when actual parameters are not passed to them. If a nonextension parameter has a default value, its actual parameter can be left out of the actual parameter list, and its default value will be assigned to the formal parameter. A default value must be a constant expression that is assignment compatible with its parameter.
Example PROGRAM prog; PROCEDURE p (a,b,c : integer) OPTION EXTENSIBLE 0 {all parameters are extensible} DEFAULT_PARMS (a:=1,b:=2,c:=3); {all have default values} BEGIN . . . END; BEGIN p(9,,5); p(6,7); p(8); p(,4,5); END.
Table 8-1 tells the value that is passed to a formal parameter, x , when x is: Nonextension or extension. Its actual parameter is speci ed or not speci ed. It is before, the same as, or after the parameter n , where n is the last parameter for which an actual parameter is speci ed. Table 8-1. Values Passed to Formal Parameter x Type of Parameter Actual Parameter for x is Speci ed Position of x informal parameter list p(..,n,..
Haveoptvarparm Function A routine can use the prede ned function haveoptvarparm to determine whether the value that it received for a formal reference parameter was passed as an actual parameter or defaulted.
Table 8-2 tells the value of haveoptvarparm(x) when the formal parameter x meets the following conditions: Nonextension or extension. Its actual parameter is speci ed or not speci ed. It is before, the same as, or after the parameter n , where n is the last parameter for which an actual parameter is speci ed. Table 8-2.
Haveextension Function With the DEFAULT PARMS procedure option, the prede ned function haveextension returns true and false under these conditions: Function haveextension(x) where x is a formal parameter of the routine that called haveextension . Returns true If the routine was passed an actual parameter for x , or if DEFAULT PARMS speci ed a default for x . Returns false If the routine was not passed an actual parameter for x , and no default was speci ed for x with DEFAULT PARMS.
Table 8-3. Values Returned by Haveextension(x ) Type of Parameter Actual Position of x informal parameter list p(..,n,..) where n is Parameter for the last actual parameter speci ed in the actual parameter x is Speci ed list p(..,n) x is before n: x is n: p(..,x,..) x p(.x.,n,..) Nonextension Parameter Yes No Calling haveextension(x) causes a compile-time error. Extension Parameter Yes true true No true Impossible, because false x=n 8-12 Procedure Options is after n: p(..,n,.x.
UNCHECKABLE ANYVAR The UNCHECKABLE ANYVAR procedure option speci es that ANYVAR hidden parameters will not be created for a routine. This allows its parameter list to be compatible with the parameter list of a routine written in a language other than HP Pascal. (See Chapter 7 for an explanation of ANYVAR parameters.
UNRESOLVED The UNRESOLVED procedure option prevents the compiler/linker/loader from resolving a routine until the program calls it. The routine must be at level one. To resolve a routine is to associate it with its system name. Calling an OPTION UNRESOLVED routine implicitly resolves it at run-time, before it is called. The routine must be resolvable. Alternatively, an OPTION UNRESOLVED routine can be explicitly resolved by calling the prede ned function addr with the routine name as its parameter.
INLINE The INLINE procedure option duplicates a routine wherever the program calls it. It makes your program bigger, but faster. It is worthwhile for short routines and when speed is more important than size. Example The program: $STANDARD_LEVEL 'EXT_MODCAL'$ PROGRAM prog; VAR i,j,k : integer; PROCEDURE max (l1,l2: integer; VAR l3 : integer) OPTION INLINE; BEGIN IF l1 > l2 THEN l3 := l1 ELSE l3 := l2 ; END; BEGIN max(10,20,i); max(i,j,k); END.
The INLINE procedure option requires STANDARD LEVEL 'EXT MODCAL'. The equivalent INLINE compiler option does not. Refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation, for more information on the INLINE compiler option. You cannot debug inline routines with a symbolic debugger. You can debug routines that call inline routines, but the inlined code is treated as a single statement and skipped.
9 External Routines An external routine is a routine that is not in the compilation unit that calls it. Its source language can be the same as that of the calling compilation unit or it can be di erent. This chapter explains: The EXTERNAL directive, which allows an HP Pascal compilation unit to access an external routine. How an HP Pascal program accesses external routines written in C, COBOL II, FORTRAN 77, FORTRAN 66/V, and SPL.
Parameters None C COBOL FORTRAN FTN77 SPL SPL VARIABLE The source code of the external routine is HP Pascal or Pascal/V. The source code of the external routine is C. See Table 9-1 for corresponding HP Pascal and C types. The source code of the external routine is COBOL II. See Table 9-2 for corresponding HP Pascal and COBOL II types. The source code of the external routine is FORTRAN 66/V.
The EXTERNAL directive replaces the block in a routine declaration (see the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual for details). The declaration containing the EXTERNAL directive can be at any level, but the external routine itself must be at level one in its own compilation unit. Example 1 The Pascal program Pascal_Pascal calls the external Pascal procedure psubproc. This is the program: $GLOBAL$ PROGRAM Pascal_Pascal(output); CONST looplimit = 10; TYPE loopbound = 1..
This is the external Pascal procedure: $EXTERNAL$ PROGRAM PASCALSUB; VAR global : integer; PROCEDURE psubproc ( VAR adder : integer; VAR total : integer); localconstant : integer; BEGIN {psubproc} IF (global MOD 2) = 0 THEN localconstant := adder * 2 ELSE localconstant := adder; total := total + localconstant; END; {psubproc} BEGIN END. You can use the EXTERNAL directive with procedure declarations in the implement part of a module.
Calling HP C from HP Pascal The table and example in this section assume that the HP Pascal program and the C routine that it calls are both compiled in Native Mode. If the C routine is in a Compatibility Mode SL instead, you must write a switch stub to access it from your HP Pascal program (see \Switch Stubs"). For more information on C types, please refer to the HP C Programmer's Guide . Table 9-1 matches corresponding HP Pascal and C types.
Table 9-1. Corresponding HP Pascal and HP C Types (continued) HP Pascal Type Corresponding HP C Types Set Not available Shortint short String char *5 String[n ] char *5 VAR parameter: Not EXTNADDR VAR parameter: EXTNADDR 0..65535 Pointer to parameter Long pointer to parameter 7 unsigned short Table 9-1 Notes 1. The lower bound of an HP Pascal array can be any integer, but the lower bound of a C array must be zero. 2. HP Pascal allocates one byte for a Boolean variable.
corresponds to the C struct type typedef struct { Tag : int ; union { int : In ; float : Re ; } } Tagged_UnionType ; 5. The value of an HP C variable of type (char *) ends with a NULL. The HP Pascal type string[n] , where n is the maximum length, corresponds to the HP C type (char *) , but has a di erent layout. HP Pascal treats string parameters to external C routines di erently.
7. Declaring a long pointer in C is analogous to declaring an ordinary pointer in Pascal, except that the \*" is replaced by \^". For example, int Func (Rec) struct Stat ^Rec ; declares Rec to be a VAR $EXTNADDR$ of type Stat. 8. Limited compatibility exists if the callee is written in C to do raw I/O (using read(2) or write(2)) on a Pascal le. Such functions can be called from Pascal by passing the result of a call to fnum(pascal le) to the C function. 9.
Example 2 The Pascal program Pascal_C2 calls the external C routine cread. The Pascal program passes a string parameter to the C routine. Pascal program: PROGRAM Pascal_C2 (output); VAR str : string[40]; FUNCTION c_read (VAR s : string) : Boolean; EXTERNAL C; BEGIN setstrlen(str,0); IF c_read(str) THEN writeln('str = ', str) ELSE writeln('couldn''t read str'); END. C routine: #include
Calling COBOL II from HP Pascal The table and example in this section assume that the HP Pascal program and the COBOL II routine that it calls are both compiled in Native Mode. If the COBOL II routine is in a Compatibility Mode SL instead, you must write a switch stub to access it from your HP Pascal program (see \Switch Stubs"). Table 9-2 matches corresponding HP Pascal and COBOL II types. (It contains only the types that are acceptable for formal intrinsic parameters.) The variable n is an integer.
Example The Pascal program Pascal_COBOL calls the external COBOL II routine subprog1. Pascal program: PROGRAM Pascal_COBOL (input,output); VAR int1, int2, int3 : integer; PROCEDURE subprog1 (VAR parm1 : integer; VAR parm2 : integer; VAR parm3 : integer); EXTERNAL COBOL; BEGIN int1 := 25000; int2 := 30000; subprog1(int1,int2,int3); writeln(int3); END. COBOL routine: $CONTROL SUBPROGRAM IDENTIFICATION DIVISION. PROGRAM-ID. SUBPROG1. AUTHOR. BP. DATA DIVISION. LINKAGE SECTION. 77 IN1 PIC S9(07) COMP.
Calling FORTRAN 77 from HP Pascal The table and example in this section assume that the HP Pascal program and the FORTRAN 77 routine that it calls are both compiled in Native Mode. If the FORTRAN 77 routine is in a Compatibility Mode SL instead, you must write a switch stub to access it from your HP Pascal program (see \Switch Stubs"). Table 9-3 matches corresponding HP Pascal and FORTRAN 77 or FORTRAN 66/V types. (It contains only the types that are acceptable for formal intrinsic parameters.
Table 9-3. Corresponding HP Pascal and FORTRAN 77 or FORTRAN 66/V Types (continued) HP Pascal Type Corresponding FORTRAN 77 or FORTRAN 66/V Type Shortint INTEGER*2 String CHARACTER*(*) 2 String[n ] CHARACTER*(*) 2 VAR parameter Default parameter mechanism RECORD real part : real ; imaginary part : real ; END ; COMPLEX Table 9-3 Notes 1.
BEGIN { main program } ... Vbl2 := strrtrim(F77_Func(Vbl1,Pac1)) ; ... END ; 3. This is not correctly implemented in FORTRAN 77. Example The Pascal program Pascal Fort calls the external FORTRAN 77 routine FORTPRC . Pascal program: PROGRAM Pascal_Fort (input,output); TYPE char_str = PACKED ARRAY [1..
Calling FORTRAN 66/V from HP Pascal FORTRAN 66/V is a Compatibility Mode language only. The FORTRAN 66/V routine that your HP Pascal program calls must reside in a Compatibility Mode SL, and you must write a switch stub to access it from your HP Pascal program (see \Switch Stubs"). The directive EXTERNAL FORTRAN passes parameters the same way in HP Pascal as it does in FORTRAN 66/V. For corresponding HP Pascal and FORTRAN 66/V types, see Table 9-3 in \Calling FORTRAN 77 from HP Pascal".
Calling SPL from HP Pascal SPL is a Compatibility Mode language only. The SPL routine that your HP Pascal program calls must reside in a Compatibility Mode SL, and you must write a switch stub to access it from your HP Pascal program. The switch stub cannot be written in SPL. (See \Switch Stubs".) The directive EXTERNAL SPL passes parameters the same way in HP Pascal as it does in Pascal/V. Table 9-4 matches corresponding HP Pascal and SPL types.
Table 9-4. Corresponding HP Pascal and SPL Types (continued) HP Pascal Type Corresponding SPL Type Record Not available, but you can lay out the equivalent. Set Not available. Shortint Integer. String Not available, but you can lay out the equivalent. String[n ] (by value only) Not available, but you can lay out the equivalent. VAR parameter Not EXTNADDR Address of parameter. VAR parameter EXTNADDR Not available. -32768..32767 Integer. 0..65535 Logical.
Example 1 The Pascal program Pascal_SPL calls the external SPL routine splprc. Pascal program: $HP3000_16$ PROGRAM Pascal_SPL (input,output); TYPE char_str = PACKED ARRAY [1..20] OF char; small_int = -32768..
Example 2 The Pascal program Pascal_SPL_V calls splprv, an external SPL routine with variable parameters. Pascal program: $HP3000_16$ PROGRAM Pascal_SPL_V (input,output); TYPE char_str = PACKED ARRAY [1..20] OF char; small_int = -32768..
Switch Stubs A switch stub is a program that allows your HP Pascal program, which is compiled in Native Mode (the default on PA-RISC machines) to call a routine compiled in Compatibility Mode (the default on earlier HP 3000 machines). The routine must reside in a Compatibility Mode SL. Figure 9-1 shows how a switch stub works. When the program calls the routine, what actually happens is that the program calls the switch stub (in Pascal) and the switch stub calls the routine in the Compatibility Mode SL.
How Non-Pascal Programs Call Pascal Routines A program written in C, COBOL II, FORTRAN 66/V, FORTRAN 77, or SPL can call an external routine written in HP Pascal. You must match the formal parameters and result type of the HP Pascal routine with those that the calling program speci es. The matching rules are: Corresponding formal parameter lists must have the same number of parameters in the same order.
Example 2 The COBOL II program COBOL-TO-PASCAL calls the external Pascal procedure pasprog. COBOL II program: IDENTIFICATION DIVISION. PROGRAM-ID. COBOL-TO-PASCAL. AUTHOR. BP. DATA DIVISION. WORKING-STORAGE SECTION. 77 ASTRING PIC X(16) VALUE "A COBOL STRING!". 77 ANUM PIC 9(04) USAGE COMP. 77 ANUM2 PIC 9(04) USAGE COMP. 77 RESULT PIC -ZZZZ. PROCEDURE DIVISION. FIRST-PARA. MOVE 9999 TO ANUM. DISPLAY ASTRING. CALL "PASPROG" USING ASTRING, \ANUM\, ANUM2. MOVE ANUM2 TO RESULT. DISPLAY ASTRING, RESULT.
Example 3 The following FORTRAN 66/V program calls the external Pascal procedure pas: INTEGER INT1, INT2, ISUM CHARACTER CSTR*20 CSTR = "Add these 2 numbers" INT1 = 25 INT2 = 15 DISPLAY CSTR, INT1, INT2 CALL PAS(CSTR,\INT1\,\INT2\,ISUM) DISPLAY CSTR, ISUM STOP END Pascal procedure: $SUBPROGRAM$ PROGRAM example(input,output); TYPE arr = PACKED ARRAY [1..20] OF char; small_int = -32768..
Example 4 The following FORTRAN77 program calls the external Pascal procedure pas: $ALIAS PAS(%REF,%VAL,%VAL,%REF) INTEGER INT1, INT2, ISUM CHARACTER CSTR*20 CSTR = "Add these 2 numbers" INT1 = 25 INT2 = 15 PRINT *, CSTR, INT1, INT2 CALL PAS(CSTR, INT1, INT2, ISUM) PRINT *, CSTR, ISUM STOP END Pascal procedure: $SUBPROGRAM$ PROGRAM example; TYPE arr = PACKED ARRAY [1..20] OF char; small_int = -32768..
Example 5 The following SPL program calls the external Pascal procedure pas: BEGIN LOGICAL ARRAY chr(0:9) := "Add these 2 numbers:"; BYTE ARRAY bchr(*) = chr; INTEGER sint:=15,sint2:=25,len; INTEGER int, int2, sum; BYTE ARRAY csum(0:1), cint(0:1), cint2(0:1); INTRINSIC PRINT,ASCII PROCEDURE pas(chr,sint,sint2,sum); VALUE sint,sint2; INTEGER sint,sint2,sum; BYTE ARRAY chr; OPTION EXTERNAL; PRINT(chr,10,0); len := ASCII(sint,-10,cint); len := ASCII(sint2,-10,cint2); PRINT(cint,-2,0); PRINT(cint2,-2,0); pas(ch
How To Do Pascal I/O with a Non-Pascal Outer Block Normally, the outer block of a Pascal program allocates space for the default text les stdin, stdout, and stderr. The outer block allocates space even if these les are referenced through Pascal modules (see Appendix A and Appendix B). The outer block also opens these standard les. In addition, the outer block performs initialization for trap handling for TRY RECOVER and for the standard Pascal module arg.
procedure initialize_pascal_standard_files; begin $if 'hpux'$ p_init_args(argc,argv,env); { initialize for module arg } $endif$ u_init_traps; { initialize for trap handling } { now open standard files } reset(input,'$stdin','shared'); rewrite(output,'$stdlist'); $if 'hpux'$ rewrite(stderr,'$stderr'); $endif$ end; begin end.
10 Intrinsics An intrinsic is an external routine that can be called by a program written in any language that the operating system supports. An intrinsic can be written in any supported language, but its formal parameters must be of types that have counterparts in the other supported languages. An intrinsic de nition resides in an intrinsic le (though its code resides in a library). You can use existing intrinsics as they are, modify them, or de ne new intrinsics.
Specifying Intrinsic Files When compiling a program that references an intrinsic, the compiler reads the intrinsic de nition from an intrinsic le. The intrinsic le can be the default intrinsic le for the system, or it can be one that you or another programmer built (see \How to Build or Change an Intrinsic File"). The program can specify di erent intrinsic les for di erent intrinsics. The SYSINTR compiler option determines the intrinsic le.
Example $SYSINTR 'myintr'$ {myintr contains the intrinsic P'F'INFO} PROGRAM q (output); PROCEDURE pfileinfo $ALIAS 'P''F''INFO'$; INTRINSIC; BEGIN pfileinfo; END. The name P'F'INFO is illegal in HP Pascal because it contains single quotes. The program q can call the intrinsic procedure P'F'INFO by the name p leinfo because it declares it with the INTRINSIC directive and speci es its system name with the ALIAS compiler option.
Reference Parameter Compatibility A reference parameter is a parameter that is passed by reference. VAR, ANYVAR, and READONLY parameters are reference parameters. All actual reference parameters must be alignment-compatible with their corresponding intrinsic parameters. Actual VAR and READONLY parameters must also be size-compatible and intrinsic-compatible with their corresponding intrinsic parameters.
Value Parameter Compatibility A value parameter is a parameter that is passed by value. All parameters except VAR, ANYVAR, READONLY, function, and procedure parameters are value parameters. An actual value parameter of a structured type (array, record, or set) must be the same size as its corresponding intrinsic parameter. An actual value parameter of an unstructured type must be assignment-compatible with its corresponding intrinsic parameter.
Table 10-2.
There are a number of ways to obtain and update the string length: If the intrinsic returns the correct length as a parameter or function return, use the setstrlen procedure with the returned value. If the length is de ned in documentation of the intrinsic, use the setstrlen procedure with that value. If the intrinsic appends some end-of-string character (such as NUL), scan for the character and set the string length with the setstrlen procedure to one less than the character's position.
Formal and Intrinsic Function Type Compatibility A function type must be speci ed when using the intrinsic directive with functions. A formal function type is compatible with an intrinsic function type as long as the size of the formal type matches the size of the intrinsic type. In general, the formal type and the intrinsic type should match the function return type. If the types do not match, they are the same as a free union type coercion. This can cause problems for signed versus unsigned types.
User-Defined Formal Parameters If you want stricter type checking for an intrinsic's actual parameters, you can declare formal parameters for some or all of its intrinsic parameters. Then, actual parameter types are compared to their corresponding formal parameter types, not to their corresponding intrinsic parameter types.
Example 2 The intrinsic le de nes the intrinsic Pascal procedure intr this way: PROCEDURE intr (a, b : integer) OPTION EXTENSIBLE 2 DEFAULT_PARMS (a := 10, b := 20); If the program declares intr this way PROCEDURE intr (a, b: integer) OPTION EXTENSIBLE 2 DEFAULT_PARMS (a := 35, b := 60); INTRINSIC; Then the default value of a is 35 (not 10) and the default value of b is 60 (not 20).
An intrinsic and a formal reference parameter are intrinsic-compatible if their types are in the same row of Table 10-3. The intrinsic parameter type is the type of the intrinsic parameter, as the intrinsic le declares it. The formal parameter type is the type of the formal parameter in your program. Table 10-3.
Table 10-4 shows which intrinsic and formal reference parameter types are compatible. The intrinsic parameter type is the type that the intrinsic parameter has in the intrinsic le; the formal parameter types are the types that you can give the formal parameter when you declare it in your program. Table 10-4.
An intrinsic and formal value parameter are intrinsic-compatible if their types are in the same row of Table 10-5. The intrinsic parameter type is the type of the intrinsic parameter, as the intrinsic le declares it. The formal parameter type is the type of the formal parameter. Table 10-5.
Using Intrinsic Functions as Procedures Your program must use an intrinsic procedure as a procedure, but it can use an intrinsic function as a function, a procedure, or both. To use an intrinsic function as a function, declare it as a function in your program, including its result type in the declaration. To use an intrinsic function as a procedure, declare it as a procedure in your program, omitting the result type.
Defining Intrinsics Syntactically, an intrinsic is de ned in the same way as any other routine. (Refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual for details.) Because an intrinsic can be called by a program written in any language that the operating system supports, its intrinsic parameters must be of types that have counterparts in the other supported languages.
If you de ne your own intrinsics, restrict system programming extensions to: Compiler options ALIGNMENT and EXTNADDR (refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual ). ANYVAR and READONLY intrinsic parameters (explained in Chapter 7). Procedure options EXTENSIBLE, UNCHECKABLE ANYVAR, and DEFAULT PARMS (explained in Chapter 8). An intrinsic de nition can specify default values for some or all of its parameters with the procedure option DEFAULT PARMS.
How to Build or Change an Intrinsic File You can build an intrinsic le, or change an existing intrinsic le, with the BUILDINT compiler option and the EXTERNAL directive. To build a new intrinsic le: 1. Put the BUILDINT option at the front of the compilation unit. Specify a new name for your intrinsic le|do not give it the name of an existing le. (Refer to the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation, for more information on BUILDINT.) 2.
Example 1 This program builds an intrinsic le. $BUILDINT 'myintr'$ $STANDARD_LEVEL 'EXT_MODCAL'$ PROGRAM build_intrinsic_file; TYPE t_integer_1 = $ALIGNMENT 1$ integer; {allows byte-aligned integer} t_barray = PACKED ARRAY [1..
To change an existing intrinsic le: 1. Put the BUILDINT option at the front of the compilation unit. Specify the name of the intrinsic le that you want to change. 2. Declare any new constants, types, or variables that will appear in new or changed intrinsic routines headings. 3. Declare any new intrinsic routines (see the third instruction for building an intrinsic le).
11 Error Recovery and Debugging There are three types of Pascal errors. They are: An error , which violates the de nition of the HP Pascal language. A compile-time error , which occurs when you compile your program (as in the case of a syntax error). A run-time error, which occurs when you run your program (as in the case of a value out of range). Errors are not to be confused with notes and warnings, both of which occur at compile time.
Error Recovery The system programming extensions that support error recovery are the prede ned procedure escape, the prede ned function escapecode, and the TRY-RECOVER construct. They are interdependent. A typical TRY-RECOVER construct has the form: TRY statement; {statement;} . . .
Example PROGRAM p; VAR x : integer; ecode : integer; . . PROCEDURE PUTJCW; INTRINSIC; PROCEDURE proc (n : integer); BEGIN {proc} {Test for erroneous parameter} IF NOT (n IN [0..100]) THEN escape(-755); . . putjcw(jcwname,jcwvalue,error); {system call} IF error > 0 THEN escape(error); {system call failed} . .
Escapecode Function The prede ned function escapecode returns the integer value of error code , the parameter of the prede ned procedure escape (see \Escape Procedure"). The result of escapecode is unde ned if escape was never called, and after exit from the TRY-RECOVER construct by normal, sequential means (rather than exit by explicit escape, exit, or goto ). If you call escapecode when its result is unde ned, the result is indeterminate and meaningless.
Example 1 PROGRAM prog (input,output); $STANDARD_LEVEL 'HP_MODCAL'$ VAR i,j,k,l : integer; PROCEDURE proc; BEGIN i := 0; j := 0; k := 0; END; BEGIN TRY read(i); {Error here transfers control to proc.} read(j); {Executed only if no error occurs for read(i). Error here transfers control to proc.} read(k); {Executed only if no error occurs for read(i) or read(j). Error here transfers control to proc.} RECOVER proc; {Executed only if an error occurs for read(i), read(j), or read(k).} l := i+j+k; END.
Example 2 PROGRAM prog (input,output); $STANDARD_LEVEL 'HP_MODCAL'$ VAR i,j : integer; iok : Boolean; PROCEDURE newj; BEGIN writeln('That value is illegal.'); prompt('Please enter an integer for j:'); read(j); END; PROCEDURE newij; BEGIN IF NOT iok THEN i := 0 ELSE newj; END; BEGIN {prog} iok := FALSE; TRY prompt('Enter an integer for i:'); read(i); {An error here transfers control to newij} iok := TRUE; {Not executed if read(i) causes an error} TRY read(j); RECOVER newj; RECOVER newij; END.
Example 3 The following example illustrates how nested TRY-RECOVER statements divide the responsibility of error recovery. The diagram below shows when, in time, the TRY-RECOVER statements labeled A, B1, B2, and C in the preceding program are active. When more than one TRY-RECOVER statement is active, the innermost one takes precedence. The RECOVER's statement can use the prede ned function escapecode to determine the error that occurred and act accordingly.
Example 4 PROGRAM system; IMPORT system_escapecodes; {see note following example} PROCEDURE support; BEGIN IF error THEN escape(88); END; PROCEDURE userprogram; BEGIN support; END; BEGIN {system} TRY userprogram RECOVER CASE escapecode OF minuser..
TRY-RECOVER and Optimization If the OPTIMIZE compiler option is used with the TRY-RECOVER construct, the following information explains what will or will not work at di erent levels. If an ESCAPE is done in the TRY block, or in any procedure called from within the TRY block, all values on the left side of an assignment statement, appearing before an ESCAPE or a procedure call, are stored. If a trap occurs instead of an ESCAPE, the above statement is not true.
Assert Procedure The prede ned procedure assert allows your program to test assumptions, specify invariant conditions, and check data structure integrity. Syntax assert (b, i [, p]) Parameters b A Boolean expression that assert evaluates. If its value is true, the program executes the statement following the call to assert . If its value is false, the program's action depends upon whether p is speci ed and whether the ASSERT HALT compiler option is OFF or ON (see Figure 11-1).
Figure 11-1 illustrates how the prede ned procedure assert works. Figure 11-1. How the Predefined Procedure Assert Works The default for the ASSERT HALT compiler option is OFF (see the HP Pascal/iX Reference Manual or HP Pascal/HP-UX Reference Manual for more information).
Example PROCEDURE my_assert (value : integer); BEGIN writeln('my_assert #', value); END; PROCEDURE x (p : ptrtype; n : integer); BEGIN assert(p <> nil, 80101, my_assert); assert(n >= 0, 80102); END; 11-12 Error Recovery and Debugging
Traps Your HP Pascal program can use these MPE/iX traps: MPE/iX intrinsic XLIBTRAP, which traps library errors. MPE/iX intrinsic XARITRAP, which traps arithmetic errors. MPE/iX intrinsics ARITRAP and HPENBLTRAP, which allow you to enable and disable trap conditions. MPE intrinsic XCONTRAP, which speci es a user-de ned routine to handle the subsystem break (4CONTROL5 Y). The subsections of this section explain how to use these traps.
ARITRAP and HPENBLTRAP Intrinsics The MPE/iX intrinsics ARITRAP and HPENBLTRAP are supported by the Trap Subsystem. ARITRAP allows a user program to enable or disable traps collectively. HPENBLTRAP is a new MPE/iX intrinsic that allows a user program to enable selected trap conditions. These terms apply to trap conditions: Term Meaning enable To allow a trap to be raised if the trap condition occurs.
XLIBTRAP Intrinsic The MPE/iX intrinsic XLIBTRAP is supported by the HP Pascal run-time library. It enables a user program to arm a library trap handling procedure (Library Trap Handler). Subsequently, any Pascal library error causes this Library Trap Handler to be called, allowing the user to decide whether to abort or continue the program, or correct the error. Syntax XLIBTRAP (plabel, oldplabel); Parameters plabel 32-bit integer, passed by value, which is the address of the Library Trap Handler.
The user's trap handler must be declared this way: TYPE PStkMrk = RECORD {Stack Marker} users_PCS : integer; {space id of users code space} users_PCO : integer; {program counter offset within the code space} users_SP : integer; {stack pointer of the user's routine that called the library routine where the error occurred} users_DP : integer; {data pointer for the above routine} {future implementations may have further fields to return more information to the user's trap handler.
Example {the user declares the following Pascal record for the PStkMrk record} TYPE PStkMrk = RECORD {"Stack Marker"} users_PCS, users_PCO, users_SP, users_DP : integer; END; $INCLUDE '/usr/include/pasesc.
XARITRAP Intrinsic The MPE/iX intrinsic XARITRAP is supported by the Trap Subsystem. XARITRAP enables your program to arm an arithmetic trap handling procedure (Arithmetic Trap Handler). Subsequently, any arithmetic error causes this Arithmetic Trap Handler to be called, allowing the user to decide whether to abort or continue the program, or correct the error. For more information on trap handling, see the Trap Handling Programmer's Guide .
On MPE/iX, declare XARITRAP as an external procedure this way: PROCEDURE XARITRAP; INTRINSIC; On HP-UX, declare XARITRAP as an external procedure this way: $PUSH; UPPERCASE ON$ PROCEDURE XARITRAP ( $POP$ Mask, plabel : integer; VAR OldMask, OldPlabel : integer ); EXTERNAL; XARITRAP stores the address of the Arithmetic Trap Handler (plabel ) so that the system trap handler can nd the routine to call if an error occurs. The old value of plabel is returned in the parameter OldPLabel.
The user's trap handler must be declared this way: TYPE TrapInfo= RECORD Instruction : integer; {the actual instruction word that caused the arithmetic trap} PC_Offset : integer; {offset of the above instruction within the user's code space} PC_Space : integer; {space id of user's code space} Error_Code : integer; {Trap type. This word is formed by setting the bit corresponding to the trap condition in a 32-bit integer, with all other bits zero.
Note In the preceding example, the IEEE inexact result trap is not enabled. HP Precision Architecture has only three distinct hardware arithmetic trap conditions: condition, [integer] over ow, and assist exception (IEEE oating-point traps are in the last category). The system is able to categorize most integer and decimal traps (except integer over ow) because each category has its own unique trapping instructions.
Decimal Overflow Trap The TrapInfo record must have one extra eld, SubCode . SubCode (word #5) contains one of the following codes, which tells what kind of decimal over ow occurred. Type of Over ow Subcode Value 1 over ow in decimal arithmetic operation 2 over ow in conversion from ASCII to decimal Invalid ASCII Digit and Invalid Decimal Digit The TrapInfo record has three extra elds: 1. Subcode (word #5) contains a code 0..3. Refer to the Trap Handling Programmer's Guide for more information. 2.
3. Format (word #7) contains the type of the operands (single, double, or quadruple). If the operation was CONVERT (CNVxx ), then the following values are returned: Value Types of Operands 1 Source is single, result is double 3 Source is single, result is quadruple 4 Source is double, result is quadruple If the operation was NOT a CONVERT (CNVxx ), then the following values are returned: Value Type of Operand 0 Single 1 Double 3 Quadruple 4.
Compatibility Mode Floating-Point Traps The TrapInfo record has one extra eld, Result ptr . Result ptr (word #5) contains the address of the result of the operation, which can be a single- or double-word oating-point number, depending on the type of trap. You can examine and replace the contents of the area referenced by result ptr , and the Trap Subsystem will ensure that the change is re ected in the appropriate place.
CONST max_real = 3.402823E+38; max_longreal = 1.
XCONTRAP Intrinsic The MPE intrinsic XCONTRAP speci es a user-de ned routine (Subsystem Break Handler) that will be called when the user enters a subsystem break (4CONTROL5 Y) on the keyboard. When XCONTRAP is enabled and the user enters 4CONTROL5 Y: Program control is transferred to the speci ed user-de ned routine. The subsystem break function is temporarily disabled to reduce the chance of race conditions.
Example The main program is a loop. Whenever the user enters 4CONTROL5 Y on the keyboard, control transfers to the procedure control_y_handler, which writes the current loop counter value, then re-enables the subsystem break, and returns to the point in the loop where the interrupt occurred.
HP TOOLSET/XL and HP Symbolic Debuggers The HP TOOLSET/XL debugger is available on the MPE/iX operating system. The HP Symbolic Debugger is available on both the HP-UX and MPE/iX operating systems. The HP TOOLSET/XL debugger supports a subset of HP Pascal features. The HP Symbolic Debugger supports the HP Pascal language. To debug your program with HP TOOLSET/XL or HP Symbolic Debugger, you must compile it with the compiler option SYMDEBUG.
System Debuggers The compiler listing of your program is an indispensable debugging aid. The following compiler options provide the listing with additional information, as noted. The system debuggers are adb on HP-UX and NM Debug on MPE/iX.
12 The Optimizer The optimizer is an optional part of the compiler that modi es your program so that it uses machine resources more e ciently, using less space and running faster. This chapter explains: When and how to use the optimizer. Level one and level two optimization. Optimizer assumptions and what cannot be optimized. How to write code that is easily optimized. What to do if your optimized program fails.
Example FUNCTION f (p : integer) : integer; VAR v : integer; BEGIN f := p; IF p < 0 THEN f := v; END; The preceding program causes the compiler to issue the warning ACCESSED, BUT NOT INITIALIZED (535), which applies to the variable v. A run-time error occurs if v is accessed. If you are sure p will never have a value less than zero, you can ignore the warning, because the run-time error will never occur.
Invoking the Optimizer You can invoke the optimizer in the source code or in the compiler command. To invoke the optimizer in the source code, use the OPTIMIZE compiler option as follows: For Use the Form Level one optimization OPTIMIZE 'LEVEL1' Level two optimization OPTIMIZE 'LEVEL2' or OPTIMIZE ON (For more information on the OPTIMIZE compiler option, see the HP Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual , depending on your implementation.
Level One Optimization Level one optimization transforms small sections of code quickly, using little compile-time storage. Compile your program with level one optimization when you want to improve run-time performance with little increase in compile time.
Table 12-1. Unoptimized and Optimized Branch Instruction Sequences (continued) Unoptimized Sequence Target of unconditional branch is a routine exit, as in: PROCEDURE p; BEGIN . . . GOTO 99; . . .
Dead Code Elimination The dead code elimination transformation eliminates code that will never be executed. For example: a := 2; goto 1; writeln('debug_patch_01',a); 1: becomes: a := 2; Do not depend on dead code because the compiler can eliminate dead code even without optimization.
Faster Register Allocation The faster register allocation transformation: Inserts entry and exit code. Generates code for operations (such as multiplication and division). Eliminates unnecessary copy instructions. Allocates actual registers to the dummy registers in instructions. The faster register allocation transformation analyzes register use faster than the coloring register allocation transformation (at level two) does.
Peephole Optimization The peephole optimization transformation makes one pass through the program, shortening instruction sequences in small windows of code by: Changing the addressing modes of instructions, so that they use shorter sequences. Substituting smaller, equivalent instruction sequences.
Level Two Optimization Level two optimization transforms each routine as a unit, causing the compiler to use more compile-time storage and take longer than level one optimization or no optimization would. Compile your program with level two optimization when you want maximum run-time performance and compilation speed is not important. The functions of the seven level two optimizer transformations are: Coloring register allocation. Induction variable elaboration and strength reduction.
Induction Variable Elaboration and Strength Reduction The induction variables and strength reduction transformation: Replaces loop counters with induction variables where appropriate (in the following example, the loop counter i is replaced by an o set into the array r). Substitutes addition for multiplication where possible.
becomes: a := 1; b := 2; c := 3; Loop-Invariant Code Motion The loop-invariant code motion transformation moves loop-invariant code out of the loop (loop-invariant code is code whose e ect is independent of the value of the loop counter).
Store-Copy Optimization The store-copy optimization transformation substitutes registers for memory locations where possible, by replacing store instructions with copy instructions and deleting load instructions.
Unused Definition Elimination The unused de nition elimination transformation removes unused memory location and register de nitions (which are often the by-products of other optimizations).
Optimizer Assumptions The optimizer makes the following assumption about variable use when it optimizes a program: inside a routine, the only variables that can be accessed indirectly (through a pointer or by another function) are: Global variables. Reference parameters. Local variables that are passed to other routines by reference. Local variables or value parameters that are passed to other routines by reference. Local variables or value parameters used by the prede ned function addr .
Writing Programs That Are Easily Optimized To maximize the opportunities for optimization and the speed with which your program is optimized, observe the following guidelines whenever possible: Specify all possible assumptions with the ASSUME compiler option. Do not use system programming extensions (specify ASSUME 'PASCAL FEATURES'). Use local variables and parameters instead of global variables (this helps the optimizer promote variables to registers). Don't use CRUNCHED, PACKED, or shortint variables.
What to Do If the Optimized Program Fails Occasionally, a program works di erently after optimization. If your program fails after optimization: 1. Make sure that the program does not violate the optimizer assumptions. Check for program errors that may not occur without optimization, such as uninitialized variables and misaligned data. 2. If the program violates the optimizer assumptions, either correct and recompile it, or recompile it without optimization. 3.
A MPE/iX Dependencies This appendix explains how the HP Pascal compiler and HP Pascal programs work on the MPE/iX operating system. It explains: How MPE/iX a ects system dependent HP Pascal features. MPE/iX extensions to HP Pascal. How to compile, link, and run your HP Pascal program on MPE/iX. System-Dependent Features System dependent features are available to all HP Pascal programs (regardless of the system on which the compiler is running), but the system a ects their de nitions and behavior.
File Names An MPE/iX le name has the syntax lename [/lockword][.group[.account]][:nodename] where each of lename , lockword , group , account and node is a string of one to eight alphanumeric characters. The rst character in the string is a letter, and each of domain and organization is a string of one to 16 alphanumeric characters, the rst of which is a letter. The entire le name cannot have more than 86 characters. MPE/iX does not distinguish between uppercase and lowercase letters.
Example PROGRAM case_one (input,output,file1); VAR file1 : FILE OF integer; BEGIN reset(file1); . . . END. The operating system associates the logical le file1 with the physical le FILE1. If FILE1 does not exist, HP Pascal creates a temporary le named FILE1. The standard les input and output are exceptions to this scheme. When they are program parameters, the operating system associates them with the physical les $STDIN and $STDLIST, respectively.
Example PROGRAM case_three (input,output,file1); VAR {logical file name is a program parameter} file1 : FILE OF integer; fname : PACKED ARRAY [1..8] OF char; BEGIN fname := 'numfile'; rewrite(file1,fname); . . . END. The operating system still associates file1 with numfile, not FILE1. The second parameter of a le-opening procedure need not be a string literal. It can also be a PAC variable or string expression.
Using File Equations The MPE/iX FILE command redirects the association of one physical le to another physical le and speci es additional le attributes, which are MPE/iX dependent. Example PROGRAM prog (outfile); VAR i : integer; outfile : text; BEGIN rewrite(outfile); FOR i := 1 TO 20000 DO writeln(outfile,i); END. If PRG is the program le for prog and you execute the MPE/iX command sequence :FILE OUTFILE = FILE2 :RUN PRG then output goes to FILE2 instead of OUTFILE.
Default File Attributes When HP Pascal creates a le, the physical le attributes depend on the le component type. Table A-1 gives the default le attributes of les built by HP Pascal programs. After the program has executed, the MPE/iX command LISTF shows these values for the les that the program built (LISTF attribute names are in parentheses). Table A-1.
Additional Features The HP Pascal features in the left-hand column depend on the MPE/iX operating system in the ways explained in the right hand column. Feature MPE/iX Dependency Close options The optional third parameter of the prede ned procedure close can be SAVE, LOCK, TEMP, NORMAL, CRUNCH, or PURGE, whose meanings are: SAVE LOCK The le is saved as a permanent le after it is closed. TEMP The le is saved as a temporary le after it is closed.
Open options The third parameter of the prede ned le-opening procedures append , associate , open , read , reset , rewrite , and write . They and their meanings are: Option Meaning CCTL The le has carriage control. (Ignored for associate .) DIRECT The le is open for read and write access (associate only). NOCCTL The le does not have carriage control. (Ignored for associate .) READ The le is open for read access only (associate and open only).
Restrictions on Using Executable Libraries (XLs) Global variables cannot be referenced across load modules. This applies to globals declared through normal, global, external, and module subprogram compilation units. In particular, you cannot use the standard les input or output . If a subprogram compilation unit is put in an XL, memory is overwritten. You cannot put an external compilation unit in an XL.
MPE/iX Extensions MPE/iX extensions are available only to programs that are run on the MPE/iX operating system or contain the compiler option OS 'MPE/XL'. They are: Prede ned function ccode Prede ned function fnum Prede ned function get alignment Prede ned function statement number Prede ned procedure setconvert Prede ned procedure strconvert Pascal/V packing algorithm ccode Function The prede ned function ccode returns an integer in the range 0..
Example PROGRAM prog (output); PROCEDURE intrin; INTRINSIC; PROCEDURE extspl; EXTERNAL SPL; PROCEDURE p; BEGIN writeln(ccode); {Garbage -- no intrinsic or external SPL intrin; writeln(ccode); {Returns condition code that intrin set} extspl; writeln(ccode); {Returns condition code that extspl set} END; BEGIN p; END. Fnum Function The prede ned function fnum returns the MPE/iX le number of the physical le currently associated with a given logical le.
Get alignment Function The prede ned function get alignment returns the alignment requirement of a given type or variable. For a type, get alignment returns the minimum possible alignment. For a variable, it returns the actual alignment. Syntax variable get_alignment ( ) type Parameters variable type Any variable. The function get alignment returns its alignment requirement. Any type identi er (the name of any type). The function get alignment returns its alignment requirement.
Statement number Function The prede ned function statement number returns the statement number of the statement that calls it, as shown on the compiled listing. It is a useful debugging aid, especially when used with the prede ned procedure assert . Syntax statement_number Example PROGRAM prog (output); VAR i : integer; BEGIN i := statement_number; writeln('Current Statement Number is ', i); assert(a > b, statement_number); END.
Setconvert Procedure The prede ned procedure setconvert converts a set from HP Pascal packing algorithm (HP3000 32) format to Pascal/V packing algorithm (HP3000 16) format, or vice versa. It is enabled by the HP3000 16 compiler option. Syntax setconvert(set1,set2) Parameters set1 The name of the set variable to be converted. set2 The name of the set variable into which the converted set is to be stored. The sets set1 and set2 can vary only in packing algorithm format.
Strconvert Procedure The prede ned procedure strconvert converts a string from Pascal/V packing algorithm (HP3000 16) format to HP Pascal packing algorithm (HP3000 32) format. It is enabled by the HP3000 16 compiler option. Syntax strconvert(string1,string2) Parameters string1 string2 The name of the string variable to be converted. The string variable must be in Pascal/V packing algorithm (HP3000 16) format. The name of the string variable into which the converted string is to be stored.
Pascal/V Packing Algorithm The Pascal/V packing algorithm is an alternative to the default HP Pascal packing algorithm that Chapter 5 explains. If you want the compiler to use the Pascal/V packing algorithm, include the compiler option HP3000 16 in your program (see the HP Pascal/iX Reference Manual for more information on the compiler option HP3000 16). HP3000 16 causes the compiler to use the Pascal/V packing algorithm, with these exceptions: Pointers are allocated four bytes each and are 4-byte-aligned.
Table A-2.
Packed Variables A packed variable is the element of a packed array or the eld of a packed record. Packed elements and packed elds are allocated and aligned di erently. Table A-3 shows how the Pascal/V packing algorithm allocates and aligns the elements of a packed array. The element types are in alphabetical order. Subsections that Table A-3 references are in this section, \Pascal/V Packing Algorithm". Table A-3.
Table A-4 shows how the Pascal/V packing algorithm allocates and aligns the elds of a packed record. The eld types are in alphabetical order. Subsections that Table A-4 references are in this section, \Pascal/V Packing Algorithm". Table A-4.
Arrays This section applies to the allocation of unpacked and packed arrays. For alignment, see Table A-2 and Table A-3. The Pascal/V packing algorithm stores arrays in row-major order (for a de nition of row-major order, see Chapter 5).
Each element of pa takes three bits. No element can cross a 2-byte boundary, so the bit following pa[5] is unused. The entire array takes four bytes, with one internal unused bit and seven trailing pad bits. It is allocated and aligned like this: Files The HP Pascal compiler allocates space for an HP3000 16 le this way: The le control block is allocated according to the HP Pascal packing algorithm. The le bu er variable size is allocated according to the Pascal/V packing algorithm.
FALSE Variant The variants f1 and f2 do not start on the same boundary; therefore, f1 cannot be overlaid with f2. Sometimes you can reduce the space that a record takes by declaring its elds in di erent order. Example VAR upr1 : RECORD b : boolean; p : 0..32767; c : char; END; upr2 : RECORD b : boolean; c : char; p : 0..32767; END; The only di erence between the variables upr1 and upr2 above is the order of their elds.
Sets The Pascal/V packing algorithm allocates sets in byte pairs. The number of byte pairs allocated to a set depends on its type. For the types Boolean, char, enumeration, and integer, the formula for the number of byte pairs is: number of byte pairs = ceil(bits required for set/16) (where ceil(x ) means the integer closest to x that is greater than or equal to x ). Table A-5 gives the values for bits required for set and number of byte pairs for Boolean, char, and integer types. Table A-5.
The set days has seven elements and requires seven bits. It is allocated one byte pair (ceil(7/16) = 1). Each element is represented by one bit, like this: The set months has 12 elements and requires 12 bits. It is allocated one byte pair (ceil(12/16) = 1). Each element is represented by one bit. The set set_33 has 33 elements and requires 33 bits. It is allocated three byte pairs (ceil(33/16) = 3). Each element is represented by one bit.
Each set element is represented by one bit, like this: To minimize storage space, avoid base types that are small subranges that overlap byte pair boundaries. Example VAR s : SET OF 31..32; The set s takes two byte pairs, using 32 bits to represent a set that requires only two bits. The arithmetic is: floor(32/16) - floor31/16) + 1 = (2-1)+1 = 2.
Strings The Pascal/V packing algorithm aligns strings on 2-byte boundaries. Because the current length (0..32767) is allocated two bytes, four bytes is the smallest possible string allocation.
The allocation is: Packed Enumerations This subsection explains how the Pascal/V packing algorithm allocates and aligns packed enumeration variables. A packed enumeration variable is either the element of a packed array or the eld of a packed record. The algorithm treats the two cases di erently. Table A-6 shows the relationship between the number of bits that an enumeration element of a packed array requires, the number of bits that the Pascal/V packing algorithm allocates it, and its alignment.
Table A-7 shows the relationship between the number of bits that an enumeration eld of a packed record requires, the number of bits that the Pascal/V mapping algorithm allocates it, and its alignment. A bit-aligned eld never crosses a 2-byte boundary. Table A-7.
Example TYPE day = (sun,mon,tues,wed,thurs,fri,sat); enum_32 = (e1,e2,e3,e4,e5,e6,e7,e8, e9,e10,e11,e12,e13,e14,e15,e16, e17,e18,e19,e20,e21,e22,e23,e24, e25,e26,e27,e28,e29,e30,e31,e32); VAR a : PACKED ARRAY [1..11] OF day; r : PACKED RECORD f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11 : day; END; aa : PACKED ARRAY [1..4] OF enum_32; rr : PACKED RECORD f1,f2,f3,f4 : enum_32; END; Each element of the array a requires three bits, and no element can cross a 2-byte boundary.
Each eld of the record rr requires and is allocated six bits, and no eld can cross a 2-byte boundary. The entire record occupies 26 bits, and is allocated four bytes: Packed Subranges of Enumerations This subsection explains how the Pascal/V packing algorithm allocates and aligns packed variables whose types are subranges of enumerations. These packed variables are either the elements of packed arrays or the elds of packed records. The algorithm treats the two cases di erently.
Example TYPE enum_32 = (e1,e2,e3,e4,e5,e6,e7,e8,e9,e10, e11,e12,e13,e14,e15,e16,e17,e18,e19,e20, e21,e22,e23,e24,e25,e26,e27,e28,e29,e30, e31,e32); VAR a : PACKED ARRAY [1..4] OF e7..e15; b : PACKED ARRAY [1..4] OF e24..e31; Each element of array a requires and is allocated four bits (see Table A-6). The elements are bit-aligned, and the entire array occupies 16 bits. It is allocated two bytes: Each element of array b requires and is allocated ve bits (see Table A-6).
Each eld of record a requires and is allocated four bits. The elds are bit-aligned, but cannot cross 2-byte boundaries. The entire record is allocated two bytes: Each eld of record b requires and is allocated ve bits. The elds are bit-aligned, but cannot cross 2-byte boundaries. The entire record occupies 21 bits. It is allocated four bytes: Packed Subranges of Integers This subsection explains how the Pascal/V packing algorithm allocates and aligns packed variables whose types are subranges of integers.
Example VAR a : PACKED ARRAY [1..4] OF 0..16; b : PACKED ARRAY [1..4] OF 0..32; Each element of the array a requires and is allocated ve bits, and is bit-aligned (see Table A-8). The entire array occupies 20 bits. It is allocated four bytes: Each element of the array b requires six bits, is allocated one byte, and is byte-aligned (see Table A-8). The entire array occupies four bytes.
Compiling, Linking, and Running Your Program To make your HP Pascal program a valid MPE/iX process, you must compile, link, and run it. The HP Pascal compiler compiles your source program, which is in a text le. It translates your source code to binary form and stores it in an object le or in an RL. The MPE/iX linker prepares the object le for execution by binding the procedures in the object modules together and de ning the initial requirements of the user data stack.
Figure A-1 shows how a source program (in a text le ) becomes a running program on MPE/iX. Figure A-1. How Source Code Becomes a Running Program on MPE/iX This section explains: The MPE/iX command les that perform the steps shown in Figure A-1. How to run the HP Pascal compiler with the MPE/iX command :RUN PASCALXL.PUB.SYS. How to pass run-time parameters to your program.
Command Files Table A-10 shows the MPE/iX command les that you can use to perform the steps shown in Figure A-1. Each command or command le in the right-hand column of Table A-1 performs the step or steps in the left-hand column (for example, the command :PASXL performs the compilation step, the command :PASXLLK performs the compilation and linking steps, and the command :PASXLGO performs the compilation, linking, and running steps). Table A-10.
Syntax PASXL [text le][,[object le][,[list le][,lib le]]][; INFO="options"] PASXLLK [text le][,[prog le][,[list le][,lib le]]][; INFO="options"] PASXLGO [text le][,[list le][,lib le]][; INFO="options"] Parameters text le The name of the text le that contains the source code to be compiled. If you are running HP Pascal from your terminal, text le is usually a le, but the default is $STDIN. $STDIN is the current input device, usually your terminal.
:RUN PASCALXL.PUB.SYS The HP Pascal/XL compiler is a program le named PASCALXL.PUB.SYS. You can use the MPE/iX command :RUN to execute PASCALXL.PUB.SYS (that is, to invoke the HP Pascal/iX compiler). The compiler les and their defaults are: File Default Source le $STDIN Object le $OLDPASS or $NEWPASS Listing le $STDLIST Library le PASLIB To override the defaults: 1. Use the MPE/iX command :FILE to equate the nondefault le with its formal le designator (the :FILE parameter formaldesignator ).
Table A-12 lists the possible values for the PARM parameter and gives their meanings. Table A-12. PARM Values and Their Meanings PARM Value 0 y Means \File equations exist for the following les:" Object Listing * 1 y Source 2 * 3 * 4 * 5 * 6 * * 7 * * * * * PARM=0 is equivalent to the command :PASXL (without parameters). Example :RUN PASCALXL.PUB.SYS :FILE PASTEXT=Program1 :FILE PASOBJ=Object1 :FILE PASLIST=List1 :FILE PASLIB=Library1 :RUN PASCALXL.PUB.
Run-Time Parameters You can pass the run-time parameters PARM and INFO to your program with the RUN command. For each parameter that you want your program to access, you must: Specify a program parameter in the program heading (the position of the variable is not important). Declare the program parameter as a global variable. The program parameter that corresponds to PARM must be of type shortint . The program parameter that corresponds to INFO must be of type string or PAC .
B HP-UX Dependencies This appendix explains how the HP Pascal compiler works on the HP-UX operating system. It explains: How HP-UX a ects system dependent HP Pascal features. HP-UX extensions to HP Pascal. How to compile, prepare, and run your HP Pascal program on HP-UX. System Dependent Features System dependent features are available to all HP Pascal programs (regardless of the system on which the compiler is running), but the system a ects their de nitions and behavior.
File Names Syntax [/][pathname]... {identi er} Parameter pathname identi er Refer to the HP-UX Reference for syntax of pathname . The name of the main source le must end with \.p". Included les need not end with \.p". Example x.p Pascal/tsource/tabort.p /mnt/shankar/junk/t.p For more information on HP-UX le names, refer to the HP-UX Reference manual. Note B-2 The HP-UX operating system is case-sensitive.
Standard Modules Three standard modules are available on HP-UX: stdinput , stdoutput , and stderr . If a module imports the stdinput module, it can use the prede ned le input in I/O statements such as read and readln . If a module imports the stdoutput module, it can use the prede ned le output in I/O statements such as write and writeln . If a module imports the stderr module, it can use the prede ned le stderr in I/O statements such as write and writeln .
Additional Features The HP Pascal features in the left-hand column depend on the HP-UX operating system in the ways explained in the right hand column. Feature HP-UX Dependency Close options The optional third parameter of the prede ned procedure close can be SAVE, LOCK, TEMP, NORMAL, CRUNCH, or PURGE, whose meanings are: SAVE The le is saved as a permanent le after it is closed. LOCK TEMP NORMAL CRUNCH This option is ignored. PURGE Halt Input Internal table size The le is purged after it is closed.
Output Stderr The standard program parameter and text le output is stdout . The standard program parameter and text le stderr is the HP-UX le stderr . System intrinsic le System default module library Temporary les ../../sys/pub/sysintr Write /usr/lib/paslib If the environment variable TMPDIR is de ned (as a path to a directory to hold temporary les), temporary les are placed in that directory; otherwise, temporary les are created in the directory /usr/tmp .
HP-UX Extensions HP-UX extensions are available only to programs that are compiled by the HP Pascal compiler running on the HP-UX operating system. The programs themselves must also run on the HP-UX operating system. The HP-UX extensions are: Access to special global variables through the EXTERNAL directive. The prede ned function get alignment , which returns the alignment requirement of a given type or variable.
Fnum Function The prede ned function fnum returns the HP-UX le number of the physical le currently associated with a given logical le. You can use this le number in system calls. Syntax fnum ( lename) Parameter lename The name of the logical le. This parameter is required, even if the logical le is the standard le input or output . The logical le must be associated with a physical le.
Get alignment Function The prede ned function get alignment returns the alignment requirement of a given type or variable. Syntax get_alignment ( variable ) type Parameters variable type Any variable. The function get_alignment returns its alignment requirement. Any type identi er (the name of any type). The function get_alignment returns its alignment requirement.
Statement number Function The prede ned function statement number returns the statement number of the statement that calls it, as shown on the compiled listing. It is a useful debugging aid, especially when used with the prede ned procedure assert . Syntax statement_number Example PROGRAM prog (output); VAR i : integer; BEGIN i := statement_number; writeln('Current Statement Number is ', i); assert(a > b, statement_number); END.
Compiling, Linking, and Running Your Program To make your HP Pascal program a valid HP-UX process, you must compile, link (and load), and run it. The HP-UX command pc coordinates the HP Pascal compiler (/usr/lib/pascomp) and the HP-UX linker loader (/bin/ld). The name of the le containing your source program must end with .p (for example, prog.p). The extension .p causes the pc command to call the HP Pascal compiler, which compiles your program and stores the resultant code in an object le.
Figure B-1 shows how a le named prog.p becomes a running program on HP-UX. Figure B-1. How a File Becomes a Running Program on HP-UX This section explains: The HP-UX pc command. How to pass run-time parameters to your program. How HP-UX handles interrupts. How HP-UX handles run-time errors.
pc Command The HP-UX command pc coordinates the HP Pascal compiler (/usr/lib/pascomp) and the HP-UX linker loader (/bin/ld). Additionally, the compiler looks for a system-wide le called /usr/lib/pasopts. If the le exists and is not empty, the compiler opens and reads the le. The le should contain only directives and comments. If there is anything else in the le, the compiler emits an error message. If the le is empty, which is the default, the compiler does not attempt to open it.
-c -Dname=bool , -Dname +DAmodel Suppress linking and only produce object (.o) les from source les. De nes name is as if it has been set (with $SET) to the n th line on the source le. bool can be either TRUE or FALSE; if bool in not speci ed, name is set to TRUE. name and bool can be uppercase or lowercase. The order in which the compiler encounters $SETs (regardless of relative order on the command line) is: 1. -Dname =bool 2. +Q d le 3.
+DSmodel Perform instruction scheduling appropriate for a speci c implementation of the PA-RISC architecture. model can be a model number such as 750 or 870, or one of the following architecture speci cations: 1.0 Perform scheduling tuned to one implementation of PA-RISC 1.0. 1.1 Perform scheduling tuned to one implementation of PA-RISC 1.1. See the le /usr/lib/sched.models for model numbers and their architectures.
-G -g -Idir -L -lx -N -n -O +Oarg Prepare object les for pro ling with the gprof utility (see \GPROF" in the HP Pascal/HP-UX Reference Manual ). Generate additional information for the symbolic debugger, and ensure that the program is linked as required for the symbolic debugger. Add dir to the list of directories that search for $INCLUDE les whose names do not begin with /. The search is performed in the following order: 1. The directory containing the source le. 2.
Those in the second group can have arg de nes as: s Suppress optimizations which tend to increase the generated code size. Currently, these optimizations include software pipelining and loop unrolling. bbnum Specify the maximum number of basic blocks allowed in a procedure that is to be optimized at level 2. If a procedure contains more than num basic blocks, level 1 optimization is performed for that procedure. The default value for num is 500 (same as $OPTIMIZE 'BASIC_BLOCKSnum '$).
-t x,name Substitute or insert subprocess x with name where x is one or more of an implementation-de ned set of identi ers indicating the subprocesses. This option works in the following modes: If x is a single identi er, name represents the full path name of the new subprocess. If x is a set of identi ers, name represents a pre x to which the standard su xes are concatenated to construct the full pathname of the new subprocesses. The values x can assume are: c Compiler body (standard su x is pascomp ).
-y +z, +Z Generate additional information needed by static analysis tools and ensure that the program is linked as required for static analysis. This option is incompatible with optimization. Both of these options cause the compiler to generate position independent code (PIC) for use in building shared libraries. However, you must use +z to generate PIC, unless certain limits are exceeded. Use +Z when limits are exceeded. If both +z and +Z are speci ed, only the last one encountered will apply.
Example The command: pc main.p ext1.p ext2.p compiles the object les main.o, ext1.o, and ext2.o, into the nal program le a.out. It is equivalent to the command sequence: pc pc pc pc Note -c main.p -c ext1.p -c ext2.p main.o ext1.o ext2.
Run-Time Parameters You can pass run-time parameters to your program as HP-UX command-line arguments when starting your program. No arguments are automatically bound to program parameters. Even the three pre-opened (standard) les, stdin , stdout, and stderr are only bound to the HP Pascal text les input, output, and stderr if the program heading declares the text les.
The export section for the module arg is: MODULE arg; EXPORT TYPE arg_string1024 = string[1024]; arg_type = PACKED ARRAY[1..32000] OF char; argarray = ARRAY[0..32000] OF ^argtype; argarrayptr = ^argarray; FUNCTION argv : argarrayptr; FUNCTION argc : integer; FUNCTION argn (n : integer) : arg_string1024; IMPLEMENT . . . . END.
Associating Program Header Files with Run-Time Parameters On HP-UX, les de ned in the program header are implicitly associated with run-time parameters. For example, if the program header is: PROGRAM myprog (input, output, file1, file2); then when the program myprog is run with command-line arguments, file1 is bound to the rst argument, and file2 is bound to the second. The prede ned les input , output , and stderr are not subject to this implicit association.
Example PROGRAM prog; CONST BADSIG = -1; SIG_DFL = 0; SIG_IGN = 1; SIG_INT = 2; SIG_QUIT = 3; VAR Old_Action : integer; FUNCTION signal (SignalNum : integer; ProcAddress : integer) : integer; EXTERNAL; The function signal accepts a signal number, SignalNum, and a procedure address, ProcAddress. Whenever the signal with the number SignalNum is raised, the function transfers control to the procedure with the address ProcAddress. The function signal returns the old stored value of ProcAddress.
Run-Time Error Handling If HP-UX detects a run-time error, it aborts the program unless the program de nes error recovery code. Error recovery code can catch run-time errors that originate from: In-line compiled code (for example: range violation errors, nil pointer errors, math over ow errors). Run-time support routines (for example: string, set, math ). Pascal le system (I/O errors). HP-UX le system support (system errors). Hardware (signals), except the kill signal.
Compiling for Different Versions of the PA-RISC Architecture Di erent HP 9000 systems use di erent versions of the PA-RISC architecture. Some models use PA-RISC 1.0 while other models use PA-RISC 1.1. The instruction set on PA-RISC 1.1 is a superset of the instruction set on PA-RISC 1.0. As a result, code generated for PA-RISC 1.0 systems will run on PA-RISC 1.1 systems, though possibly less e ciently than if it were speci cally generated for PA-RISC 1.1. However, code generated for PA-RISC 1.
If you plan to run your program on many models of the HP 9000, you could use the following combination: +DA1.0 to ensure portability, and +DSmodel with the model number of the fastest system you will be running your application on. For example, using +DA1.0 +DS897 ensures your program can run on all series 700s and 800s, and uses scheduling for the model 897.
Glossary actual parameter An argument that is passed to a procedure, function, or subprogram. Contrast with formal parameter . address An exact location in memory. A program can store or retrieve data from this address. algorithm A procedure used to solve a task. It describes the sequence of steps or operations, done in a nite number of steps. allocate To set up a memory location to hold variable values. alpha character A character in the range of A through Z and a through z.
assignment statement Assigns a value to a variable or function by using the special Pascal symbol \:=". binary The method used to represent numbers, alphabetic characters, and symbols in digital computers. It is a base two numbering system that uses only two digits, 0's and 1's, to express numeric quantities. bit A unit of information with a value of 1 or 0. Usually eight bits equal one byte. A bit is the smallest unit of information in a digital computer.
compiler A program that translates source code into machine instructions. The compiler also diagnoses and reports syntax errors found in the application program. compound statement A group of statements enclosed with the reserved words begin and end , and which are treated as a single statement. concatenation The operation of joining two or more character strings together. constant A xed value, as opposed to a variable which is a symbol for a changing value.
expression A construct composed of operators and operands that represent the computation of a result of a particular type. external routine A routine de ned in another compilation unit. le-equate To redirect the association of one physical le to another physical le, or to specify additional le attributes using the MPE XL FILE command. formal parameter A parameter which is de ned in a procedure, function, or subprogram header. function A block that is invoked with a function call and returns a value.
minint The minimum value that an integer can contain. NLS An acronym for Native Language Support. operand The variables, constants, or literals that are used in an operation. operator De nes the action to be performed on one or more operands. optimization The process which the compiler uses to modify your program so that it uses machine resources more e ciently. parameter The argument used for sending and receiving information to and from functions and procedures.
semantic error An error which is caused by using the wrong wording in a program. separate compilation The process of separating the source for a large program into pieces that can be compiled independently of other pieces. source code The input program that is to be translated by the compiler. Standard Pascal All of the rules and de nitions of Pascal as de ned by the ANSI standard. statement Pascal's single unit of activity. Each statement is separated by a semicolon.
Index 1 16-bit Native Language Support enabling, B-17 A accessing an external routine, 9-1 actual parameters, 7-1 intrinsics, 10-3{7 string, 10-6 addr function, 7-15, 8-14 algorithm HP Pascal packing, 5-1, 5-3 Pascal/V packing, A-16{33 ALIAS compiler option, 10-2 used with intrinsic system names, 10-14 $ALIAS directive, 9-13 alignment arrays, 5-7 compatibility, 10-3 crunched arrays and records, 5-18{20 crunched sets, 5-21 de nition of, 5-3 enumerations, 5-22{26 les, 5-27 HP-UX extensions, B-8 implicit typ
B baddress function, 7-6 basic blocks, 12-3 bit16 data type, 4-2, 4-3 bit32 data type, 4-2, 4-4 bit52 data type, 4-2, 4-6 bits pair requirements, table of, A-23 bitsize function, 7-5, 7-27 block, program, 2-2 Boolean data type, 4-2 branch optimization, 12-4 break handler subsystem, 11-26 bu ers le, 3-5 BUILDINT compiler option, 10-17{19 bytes pair requirements, table of, A-23 C calling COBOL II from HP Pascal, 9-10{11 calling FORTRAN 66/V from HP Pascal, 9-15 calling FORTRAN 77 from HP Pascal, 9-12{14 cal
MPE/iX dependencies, A-1{40 NOTES, 12-14 OPTIMIZE, 12-3 RANGE, A-40 SEARCH, 2-7 SUBPROGRAM, 2-12 SYMDEBUG, 11-28, B-1 SYSINTR, 10-2 TABLES, 11-29 TYPE COERCION, ANYVAR parameters, 7-4 compiling di erent versions of PA-RISC architecture, B-25 large programs, 2-11{13 modules, 2-7 networked environments, B-26 condition code, A-10 conformant array parameters, 7-1, 7-7{9 conformant array schemas, 7-7{9 equivalent, 7-22 congruent parameter lists, 7-22 constants folding, 12-10 maxint, 4-1 minint, 4-1 prede ned, 4-
E enabling traps, 11-14 end-of-line markers, 3-4 enumerations allocation and alignment, 5-22{26, A-27{30 packed, A-27{30 packed subranges, A-30{32 unpacked, allocation and alignment, 5-22 eof function, 3-2, 3-19, 3-27 eoln function, 3-2, 3-23 equivalent conformant array schemas, 7-22 equivalent parameters, 7-22 error recovery, 11-2{12 escapecode function, 11-2 escape procedure, 11-2 TRY-RECOVER, 11-2 TRY-RECOVER construct, 11-4{9 errors compile-time, 11-1 de nition of, 11-1 recovery, 11-1{29 run-time, 11-1
demand loadable, B-16 direct access, 3-25 direct le functions, characteristics, 3-27 le-opening procedures, table of, 3-9 formal le designators, A-38 general information, 3-3 HP-UX, B-2 intrinsic, 10-1, 10-2 logical, 3-4 MPE/iX, A-2 object, A-34, B-13 opening, 3-6{9 physical, 2-14, 3-3 program, HP Pascal/iX, A-38 relationship between input/output and le types, 3-1 relationships between physical and logical, 3-3 sequential le input/output, 3-16{21 sequential I/O procedure characteristics, 3-16 stderr, B-5 sy
linking your program, B-10{24 maxpos feature, B-4 miscellaneous features, B-4 open options feature, B-4 optimizer, invoking, 12-3 ord feature, B-4 output feature, B-5 PASXDATA, B-4 pc command, B-12 prede ned modules, 4-10 program header les, B-22 running out of space during optimization, 12-16 running your program, B-10{24 run-time error handling, B-24 run-time parameters, B-20, B-22 special global variable access, B-6 standard modules, B-3 statement number function, B-9 stderr le, B-5 system-dependent feat
text le, 3-2 text les, 3-22{24 input text le, 3-4 instruction scheduling, 12-7 +DS, B-25 integer data type, 4-2 integer over ow trap, 11-21 integers packed subranges, A-32{33 internal table size HP-UX, B-4 internal table size feature, MPE/iX, A-7 interrupt handling HP-UX, B-22 MPE/iX, 11-26 INTRINSIC directive, 10-2 intrinsic function type compatibility, 10-8 intrinsic routines, 2-14 intrinsics, 2-8, 10-1 accessing a routine, 10-2 ARITRAP, 11-14 building a le, 10-17{19 changing a le, 10-17{19 compatibility,
access, 2-7 arg, 4-12, B-20 compiling, 2-7 duplicate names, 2-8 export declarations, 2-5 HP-UX, 4-10 implement parts, 2-5 import declarations, 2-5 MPE/iX, 4-10 object, A-34 pas hp1000, 4-13 prede ned, 4-10{14 scope, 2-8 separate compilation, 2-11 standard, HP-UX, B-3 standard, MPE/iX, A-6 stderr, 4-11 stdinput, 4-10 stdoutput, 4-10 syntactic structure, 2-5 module-text, 2-7 module variables, 6-1 modulo 65535, 4-3 MPE/iX ccode function, A-10 close option, A-7 command les, A-36{37 compiler dependencies, A-1{40
store-copy optimization, 12-12 strength reduction, 12-10 unused de nition elimination, 12-13 when to use, 12-1 writing programs for, 12-15 options parameters, A-37 procedure, 8-1{16 ord feature, HP-UX, B-4 outer block non-Pascal, 9-26 output, 3-1{30 direct, 3-25{28 direct I/O procedures, table of, 3-25 HP-UX feature, B-5 marking les as demand loadable, B-16 marking les as not demand loadable, B-16 marking les as shareable, B-15 marking les as unsharable, B-15 MPE/iX feature, A-8 sequential les, 3-16{21 text
to intrinsic, 10-5 passing strings, 10-6 pas sparameters function, 4-13 pas stringdata1 function, 4-14 pas stringdata2 function, 4-14 pas timestring function, 4-14 pas traceback function, 4-14 PASXDATA HP-UX, B-4 MPE/iX, A-7 PASXL command le, A-36 PASXLGO command le, A-36 PASXLLNK command le, A-36 pc command, B-10, B-12 parameters, B-12{18 peephole optimization, 12-8 p getheap procedure, 6-19{23 physical le, 2-14 physical les, 3-3 associating with logical les, 3-6 MPE/iX dependencies, A-2{4 relationship to
prog le parameters, A-37 program block, 2-2 heading, 2-1 name, 2-1 parameters, 2-1 program arguments, B-20 program header les association with run-time parameters, B-22 HP-UX, B-22 program listing, B-15 program parameters HP-UX, B-20 MPE/iX, A-40 programs failure of optimized, 12-16 writing for optimizer, 12-15 program structure, 2-1{14 compilation unit, 2-4 syntactic, 2-1 prompt procedure, 3-2, 3-22 p rtnheap procedure, 6-19{23 put procedure, 3-2, 3-16 R range checking turning o , B-16 RANGE compiler opti
characteristics of input/output procedures, 3-16 functions, characteristics of, 3-19 sequential input/output, 3-2 set chunks de nition of, 5-28 size determination, 5-28 setconvert procedure, A-14 sets allocation and alignment, 5-28{30, A-23{25 crunched, 5-21 Pascal/V packing algorithm, A-23{25 shared libraries, 2-11{13 shortint data type, 4-2, 4-8 signal routine, B-22 size compatibility, 10-3 size function, 7-5, 7-27, 7-31 SPL calling from HP Pascal, 9-16{19 hidden parameters, 7-34 SPL external routines, 9-
stderr le, HP-UX, B-5 temporary les, HP-UX, B-5 write feature, HP-UX, B-5 write feature, MPE/iX, A-7 system names of intrinsics, 10-2 resolution of, 8-14 T TABLES compiler option, 11-29 temporary les, 3-7 HP-UX, B-5 terminal, input from, 3-20 terminating a program, 3-29 text le input/output, 3-2 text les, 3-4 access, 3-22 allocation of le components, 5-27 function characteristics, table of, 3-23 input, independent module, 4-10 input/output, 3-2, 3-4, 3-22{24 input/output procedures, table of, 3-22 output,
localanyptr, 6-4{5 module, 6-1 optimization, 12-15 packed, 5-5{6, A-18 packed enumerations, A-27{30 procedure type, 7-15{19 routine type, 7-15{19 special global access, B-6 uninitialized, 12-1 unpacked, 5-4, A-16 variants FALSE, 5-9 TRUE, 5-9 VAR parameters, 7-3 verbose mode enabling, B-17 W warnings non-ANSI Pascal features, B-12 optimizer, 12-1 turning o messages, B-17 vs notes and errors, 11-1 Index-14 WITH statement optimization and, 12-15 writedir procedure, 3-2, 3-25 write feature, HP-UX, B-5 write