HP SunSoft Pascal 4.0 User Manual

Pascal 4.0 User’s Guide
A Sun Microsystems, Inc. Business
2550 Garcia Avenue Mountain View, CA 94043 U.S.A.
Part No.: 802-2943-10 Revision A, November 1995
1995 Sun Microsystems, Inc. 2550 Garcia Avenue, Mountain View, California 94043-1 100 U.S.A. All rights reserved. This product or document is protected by copyright and distributed under licenses r estricting its use,
copying, distribution and decompilation. No part of this product or document may be reproduced in any form by any means without prior written authorization of Sun and its licensors, if any .
Portions of this product may be derived from the UNIX
®
system and from the Berkeley 4.3 BSD system, licensed from the University of California. Third-party software, including font technology in this product, is pr otected by copyright and licensed from Sun’s Suppliers. RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the government is subject to restrictions as set forth in
subparagraph (c)(1)(ii) of the Rights in T echnical Data and Computer Software clause at DFARS 252.227-7013 and F AR 52.227-19.
The product described in this manual may be protected by one or more U.S. patents, foreign patents, or pending applications. TRADEMARKS
Sun, the Sun logo, Sun Microsystems, Sun Microsystems Computer Corporation, Solaris, the Sun Microsystems Computer Corporation logo, SunSoft, the SunSoft logo, ProW orks, ProWorks/T eamWare, ProCompiler, Sun-4, SunOS, ONC, ONC+, NFS, OpenWindows, DeskSet, ToolT alk, SunV iew, XV iew , X1 1/NeWS, AnswerBook, and Magnify Help are trademarks or register ed trademarks of Sun Microsystems, Inc. in the United States and may be protected as trademarks in other countries. UNIX is a registered trademark in the United States and other countries, exclusively licensed through X/Open Company, Ltd. OPEN LOOK is a registered trademark of Novell, Inc. PostScript and Display PostScript are trademarks of Adobe Systems, Inc. PowerPC™ is a trademark of International Business Machines Corporation. HP
®
and HP-UX® are registered trademarks of
Hewlett-Packard Company . All other pr oduct names mentioned herein are the trademarks of their r espective owners. All SP ARC trademarks, including the SCD Compliant Logo, are trademarks or register ed trademarks of SP ARC International,
Inc. in the United States and may be protected as trademarks in other countries. SP ARCcenter , SPARCcluster , SP ARCompiler, SP ARCdesign, SP ARC81 1, SPARCengine, SP ARCprinter , SPARCserver , SPARCstation, SP ARCstorage, SPARCworks, microSP ARC, microSPARC-II, SP ARCware, and UltraSPARC are licensed exclusively to Sun Microsystems, Inc. Pr oducts bearing SP ARC trademarks are based upon an architectur e developed by Sun Microsystems, Inc.
Please
The OPEN LOOK™ and Sun™ Graphical User Interfaces were developed by Sun Microsystems, Inc. for its users and licensees. Sun acknowledges the pioneering efforts of Xerox in researching and developing the concept of visual or graphical user interfaces for the computer industry . Sun holds a non-exclusive license from Xer ox to the Xerox Graphical User Interface, which license also covers Sun’s licensees who implement OPEN LOOK GUI’s and otherwise comply with Sun’s written license agreements.
X Window System is a product of X Consortium, Inc. THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Pascal Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Text Editors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Debuggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
XView Toolkit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Native Language Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Internationalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Licensing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2. Pascal Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
A Simple Pascal Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
iii
Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Renaming the Executable File . . . . . . . . . . . . . . . . . . . . . . . . 9
An Interactive Pascal Program. . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Compiling the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Redirecting I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Using a File Name as a File Variable . . . . . . . . . . . . . . . . . . . 12
Where Did My Program Fail?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Using Pascal Traceback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Using a Sample Program with Segmentation Violation . . . 14
3. The Pascal Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
pc Version Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Compile and Link Sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Language Preprocessor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
File Name Extensions Accepted By pc . . . . . . . . . . . . . . . . . . . . 20
Option-Passing on the Command-Line. . . . . . . . . . . . . . . . . . . . 21
Option-Passing in the Program Text . . . . . . . . . . . . . . . . . . . . . . 21
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
–a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
–Bbinding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
–b. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
-bsdmalloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
–C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
iv Pascal 4.0 User’s Guide
–c. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
-calign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
–cg89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
–cg92 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
-cond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
–config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
–Dname[=def]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
–dalign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
-dn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
-dryrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
-dy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
–fast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
-fnonstd. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
–fns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
–fround=r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
–ftrap=t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
-G. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
–g. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
–H. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
-hname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
–help or -flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
–Ipathname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
–i name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
-keeptmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Contents v
–L. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
–l. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
-Ldirectory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
-libmieee. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
–libmil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
–llib. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
–misalign. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
-mt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
–native . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
-nocx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
-nolib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
–nolibmil. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
-noqueue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
-notrace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
–O[level]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
–o filename. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
–P. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
–p and –pg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
–pic, -Kpic and –PIC, -KPIC. . . . . . . . . . . . . . . . . . . . . . . 39
–Qoption. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
–Qpath pathname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
–Qproduce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
-qp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
-R. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
vi Pascal 4.0 User’s Guide
-R path[:dir] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
-Rw. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
–S. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
–s[level]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
–sb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
–sbfast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
-tc. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
–temp=dir. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
–time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
–U name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
–V. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
–V0 and –V1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
–v. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
–w. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
-xa. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
-xarch=a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
-xcache=c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
-xchip=c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
-xcg89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
-xcg92 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
–xF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
-xildoff. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
-xildon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
–xl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Contents vii
-xlibmieee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
-xlibmil. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
-xlibmopt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
–xlicinfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
–xMerge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
-xnolib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
-xnolibmopt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
-x05 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
-xpg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
-xprofile=p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
-xregs=r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
–xs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
-xsafe=mem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
-xsb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
-xsbfast. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
-xspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
-xtarget=t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
–Z. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
-ztext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4. Program Construction and Management . . . . . . . . . . . . . . . . . 67
Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Using Program Units and Module Units . . . . . . . . . . . . . . . 68
Compiling with Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Using Units and Header Files . . . . . . . . . . . . . . . . . . . . . . . . 70
viii Pascal 4.0 User’s Guide
Sharing Variables Between Units. . . . . . . . . . . . . . . . . . . . . . 71
Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
5. Separate Compilation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Working with Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Using Program Units. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Using Module Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Sharing Variables and Routines Across Multiple Units . . . . . . 76
Compiling without the -xl Option . . . . . . . . . . . . . . . . . . . . 77
Using the -xl Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Sharing Declarations in Multiple Units. . . . . . . . . . . . . . . . . . . . 87
6. The C–Pascal Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Compilation of Mixed-Language Programs. . . . . . . . . . . . . . . . 89
Compatibility of Types for C and Pascal. . . . . . . . . . . . . . . . . . . 90
Precautions with Compatible Types . . . . . . . . . . . . . . . . . . . 91
Incompatibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
General Parameter Passing in C and Pascal . . . . . . . . . . . . . . . . 93
Procedure Calls: C–Pascal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Variable Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Value Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Function Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Procedure Calls: Pascal–C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Variable Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Value Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Contents ix
Function Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Parameters That Are Pointers to Procedures . . . . . . . . . . . . 131
Procedures and Functions as Parameters . . . . . . . . . . . . . . . . . . 132
Global Variables in C and Pascal . . . . . . . . . . . . . . . . . . . . . . . . . 133
File-Passing Between Pascal and C . . . . . . . . . . . . . . . . . . . . . . . 134
7. The C++–Pascal Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Sample Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Compatibility of Types for C++ and Pascal . . . . . . . . . . . . . . . . 138
C++ Name Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Procedure Calls: C++–Pascal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Arguments Passed by Reference . . . . . . . . . . . . . . . . . . . . . . 139
Arguments Passed by Value. . . . . . . . . . . . . . . . . . . . . . . . . . 150
Function Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Procedure Calls: Pascal–C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Arguments Passed by Reference . . . . . . . . . . . . . . . . . . . . . . 155
Arguments Passed by Value. . . . . . . . . . . . . . . . . . . . . . . . . . 157
Function Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Global Variables in C++ and Pascal. . . . . . . . . . . . . . . . . . . . . . . 161
Pascal File Pointers to C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8. The FORTRAN–Pascal Interface . . . . . . . . . . . . . . . . . . . . . . . . 163
Compiler Mixed-Language Programs. . . . . . . . . . . . . . . . . . . . . 163
Compatibility of Types for FORTRAN and Pascal. . . . . . . . . . . 164
Precautions with Compatible Types . . . . . . . . . . . . . . . . . . . 165
Incompatibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
x Pascal 4.0 User’s Guide
General Parameter-Passing in FORTRAN and Pascal. . . . . . . . 167
Procedure Calls: FORTRAN-Pascal . . . . . . . . . . . . . . . . . . . . . . . 168
Variable Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Value Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Function Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Procedure Calls: Pascal-FORTRAN . . . . . . . . . . . . . . . . . . . . . . . 185
Variable Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Value Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Function Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Routines as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
9. Error Diagnostics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Compiler Syntax Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Illegal Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
String Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Digits in Real Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Replacements, Insertions, and Deletions . . . . . . . . . . . . . . . 207
Undefined or Improper Identifiers . . . . . . . . . . . . . . . . . . . . 208
Expected Symbols and Malformed Constructs . . . . . . . . . . 208
Expected and Unexpected End-of-file. . . . . . . . . . . . . . . . . . 209
Compiler Semantic Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Format of the Error Diagnostics. . . . . . . . . . . . . . . . . . . . . . . 210
Incompatible Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Contents xi
The scalar Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Procedure and Function Type Errors. . . . . . . . . . . . . . . . . . . 211
Scalar Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Expression Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Type Equivalence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Unreachable Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Uninitialized Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Unused Variables, Constants, Types, Labels, and Routines 216
Compiler Panics, I/O Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Runtime Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
10. The XView Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . 222
Pascal Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Compiling with Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Attribute Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Handles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Coding Fragment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Conversion of C to Pascal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
xii Pascal 4.0 User’s Guide
An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Sample Translation of an XView Function to Pascal . . . . . . 229
Sample Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Menu Demo Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
11. Math Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Contents of the Math Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . 234
libm Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
IEEE Support Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
ieee_functions(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
ieee_values() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
ieee_retrospective() . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
SPARC Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Arithmetic Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Math Library Exception-Handling Function: matherr() . . . . 240
libsunmath Support for IEEE Modes and Exceptions . . . . . . 242
Contents xiii
xiv Pascal 4.0 User’s Guide
Figures
Figure 3-1 Organization of Pascal Compilation. . . . . . . . . . . . . . . . . . . . . . 19
Figure 3-2 Options in Program Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Figure 10-1 A Sample Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
xv
xvi Pascal 4.0 User’s Guide
Tables
Table 3-1 File Name Suffixes Recognized by Pascal . . . . . . . . . . . . . . . . . 20
Table 3-2 Options That Can Be Passed in Program Text . . . . . . . . . . . . . 21
Table 3-3 The -xarch Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Table 3-4 The -xcache Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Table 3-5 The -xchip Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Table 3-6 The -xprofile Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Table 3-7 The -xregs Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Table 3-8 The -xtarget Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Table 3-9 -xtarget Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Table 6-1 C and Pascal Size and Alignment of Compatible Types . . . . . 90
Table 6-2 C and Pascal Size and Alignment of Compatible Types with –xl
91
Table 6-3 Set Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Table 8-1 Default Sizes and Alignments of Compatible Types (Pascal and
FORTRAN). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
–xl
with –xl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
xvii
Table 10-1 C Declarations to Pascal Declarations . . . . . . . . . . . . . . . . . . . . 228
Table 11-1 Contents of Math Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
xviii Pascal 4.0 User’s Guide
Operating Environment
Installation
Preface
This manual describes the Pascal 4.0 compiler from SunSoft™. The purpose of this manual is to help you begin writing and compiling Pascal programs on a SPARCstation™.
In a previous major release, this Pascal compiler also ran on Solaris 1.x. Some features remain in the documentation as being for Solaris 1.x only.
The README file that accompanies the product contains other release-specific information,
Note – All references to Pascal in this manual refer to the Pascal 4.0 compiler unless otherwise indicated.
For information on the operating environment, see the README file.
For instructions on how to install Pascal, refer to the Installing SunSoft Developer Products (SPARC/Solaris) manual.
xix
Audience
Organization
This guide was prepared for software engineers who write Pascal programs on a SPARCstation. It assumes you are familiar with ISO standard Pascal and the Solaris™ operating system.
This guide contains the following chapters:
Chapter 1, “Introduction,” gives basic information about the Pascal
compiler and related program development tools.
Chapter 2, “Pascal Programs,” describes how to write, compile, and run a
Pascal program.
Chapter 3, “The Pascal Compiler,” describes the pc command and its
options.
Chapter 4, “Program Construction and Management,” is an introduction to
how complex programs are built in Pascal.
Chapter 5, “Separate Compilation,” describes how programs can be
divided into several units, and how they are compiled and linked.
Chapter 6, “The C–Pascal Interface,” describes how to write programs that
are partly in C and partly in Pascal.
Chapter 7, “The C++–Pascal Interface,” describes how to write programs
that are partly in C++ and partly in Pascal.
Chapter 8, “The FORTRAN–Pascal Interface,” describes how to write
programs that are partly in FORTRAN and partly in Pascal.
Chapter 9, “Error Diagnostics,” describes the errors you may encounter
while writing programs with Pascal.
Chapter 10, “The XView Toolkit,” describes how to use the XView toolkit
with Pascal.
Chapter 11, “Math Libraries,” describes how to use the libm and
libsunmath functions in Pascal programs.
Appendix A, “Pascal Preprocessor,” describes the Pascal preprocessors,
with emphasis on the nonstandard preprocessor, cppas.
xx Pascal 4.0 User’s Guide
Appendix B, “Error Messages,” lists all the error messages the compiler
produces.
This guide concludes with an index.
Conventions Used in This Guide
This guide contains syntax diagrams of the Pascal language in extended Backus-Naur Formalism (BNF) notation. Here are the meta symbols:
Table P-1 BNF Meta Symbols
Meta Symbol Description
::= Defined as | Can be used as an alternative (a | b) Either a or b [ a ] Zero or one instance of { a } Zero or more instances of a 'abc' The characters abc
The following table describes the type styles and symbols used in this guide:
Table P-2 Typographic Conventions
a
Typeface or Symbol Meaning Example
AaBbCc123 The names of commands, files, and
directories; on-screen computer output
AaBbCc123 What you type, contrasted with on-
screen computer output
AaBbCc123 Command-line placeholder:
replace with a real name or value
AaBbCc123 Book titles, new words or terms, or
words to be emphasized
Preface xxi
Edit your .login file. Use ls -a to list all files.
hostname% You have mail.
hostname% su Password:
To delete a file, type rm filename.
Read the User’s Guide. These are called class options. You must be root to do this.
Shell Prompts in Command Examples
The following table shows the default system prompt and superuser prompt for the C shell, Bourne shell, and Korn shell.
Table P-3 Shell Prompts
Shell Prompt
C shell prompt machine_name% C shell superuser prompt machine_name# Bourne shell and Korn shell prompt $ Bourne shell and Korn shell superuser prompt #
Related Documentation
This manual is designed to accompany the following documents:
The Pascal 4.0 Reference Manual, which describes extensions to standard
Pascal
The Pascal 4.0 Quick Reference, which summarizes the compiler options
Both this manual and the Pascal 4.0 Reference Manual are available in the AnswerBook® system, an on-line documentation viewing tool that takes advantage of dynamically linked headings and cross-references. The Installing SunSoft Developer Products (SPARC/Solaris) manual shows you how to install AnswerBook.
Manual Page
Pascal 4.0 provides an on-line manual page (also known as man page), on pc(1), that describes the Pascal compiler. This document is included in the Pascal package and must be installed with the rest of the software
Once you install the documentation, you can read about pc by entering the
man command followed by the command name, as in:
hostname% man pc
xxii Pascal 4.0 User’s Guide
README Files
The README default directory is: /opt/SUNWspro/READMEs. This directory contains the following files:
A Pascal 4.0 README, called pascal, that describes the new features,
software incompatibilities, and software bugs
A floating-point white paper, “What Every Scientist Should Know About
Floating-Point Arithmetic,” by David Goldberg, in PostScript™ format. The
file is called floating-point.ps, and can be printed on any PostScript­compatible printer that has Palatino font. It can be viewed on-line by using the imagetool command:
hostname% imagetool floating-point.ps
This paper is also available in the AnswerBook system.
Other Related Documentation
Other reference material includes:
Profiling Tools Numerical Computation Guide
Documents in Hard Copy and in AnswerBook
The following table shows what documents are on-line, in hard copy, or both:
Table P-4 Documents in Hard Copy and in AnswerBook
Title Hard Copy On-Line
Pascal 4.0 User’s Guide X X (AnswerBook) Pascal 4.0 Reference Manual X X (AnswerBook) Pascal 4.0 Quick Reference X Installing SunSoft Developer Products (SPARC/Solaris) X X (AnswerBook) Profiling Tools X (AnswerBook)
Preface xxiii
Table P-4 Documents in Hard Copy and in AnswerBook (Continued)
Title Hard Copy On-Line
Numerical Computation Guide X (AnswerBook)
README X (CD-ROM)
What Every Scientist Should Know About Floating-Point Arithmetic X (AnswerBook and CD-ROM)
xxiv Pascal 4.0 User’s Guide
Standards
Introduction
This chapter gives an overview of the features of Pascal, including compatibility, internationalization, and licensing. It contains the following sections:
Standards page 1 Pascal Compiler page 2 Features page 2 Compatibility page 2 Text Editors page 3 Debuggers page 3 XView Toolkit page 3 Native Language Support page 3 Licensing page 5
Pascal is a derivative of the Berkeley Pascal system distributed with UNIX® 4.2 BSD. It complies with FIPS PUB 109 ANSI/IEEE 770 X3.97-1983 and
BS6192/ISO7185 at both level 0 and level 1.
1
1
1
Pascal Compiler
Features
The name of the Pascal compiler is pc. If given an argument file name ending with .p or .pas, pc compiles the file and leaves the result in an executable file, called a.out by default.
Pascal includes many extensions to the standard, including the following:
Separate compilation of programs and modules
dbx (symbolic debugger) support
Optimizer support
Multiple label, const, type, and var declarations
Variable-length character strings
Compile-time initializations
static and extern declarations
Different sizes of integer and real data types
Integer constants in any base, from 2 to 16
Extended input/output facilities
Extended library of built-in functions and procedures
Universal and function and procedure pointer types
Direction of parameter passing: into a routine, out of a routine, or both
Functions that return structured-type results
Note – For other release-specific information, please refer to the README file that accompanies the product release.
Compatibility
In general, Pascal 4.0 runs in the Solaris 2.x or above operating environment. This product is not compatible with /usr/ucblib/libucb.a on the Solaris
2.x environment.
2 Pascal 4.0 User’s Guide
Text Editors
Debuggers
1
The operating system provides two main editors:
Text Editor—A window-based text editor that runs in the OpenWindows
environment. Start this tool by typing textedit at the system prompt.
vi—The standard visual display editor that offers the capabilities of both a
line and a screen editor. It also provides several commands for editing programs. For example:
The autoindent option provides white space at the beginning of a line.
The showmatch option shows matching parentheses.
SunSoft offers a variety of programming tools that run in the Solaris operating environment. For debugging, the following tools are available:
dbx—A symbolic debugger
debugger—A window- and mouse-based version of the symbolic debugger
You can use Pascal with fix-and-continue, a debugger functionality. See the debugger documentation for details of this feature.
XV iew Toolkit
The XView application programmer ’s interface (API) is an object-oriented and server-based user-interface toolkit for the X Window System Version 11 (X11). It is designed for manipulating XView windows and other XView objects. Chapter 10, “The XView Toolkit,” describes how to use XView with Pascal.
Native Language Support
Sun supports the development of applications in languages other than English. These languages include most European languages and Japanese. As a result, you can easily switch your application from one native language to another. This feature is known as internationalization.
Introduction 3
1
Internationalization
A product can support up to four levels of internationalization:
Level 1—Allows native-language characters (such as the a-umlaut). This is
referred to as the 8-bit clean model because the eighth bit in each byte is used to represent native-language characters.
Level 2—Recognizes and displays international date and time formats, such
as 26.07.90 in Italian; international decimal units, such as 1.234.567,89 in French; and international monetary units, such as 1.234,56 Pts in Spanish.
Level 3—Contains support for localized messages and text presentation.
Level 4—Contains Asian language support.
Pascal supports all four levels. See the Pascal 4.0 Reference Manual for a description of the date and time functions in internationalized formats.
Pascal does not allow input and output in the various international formats. If it does, it does not comply with the Pascal language standard, ANSI/IEEE 770 X3.97-1983.
For example, the standard specifies a period (.) as the decimal unit in the floating-point representation. Consider the following program, which prints a floating-point value:
program sample(output);
var r : real := 1.2;
begin
writeln(r);
end.
When you compile and run the program on the internationalized Pascal compiler, the output is:
1.20000000000000e+00
4 Pascal 4.0 User’s Guide
Licensing
1
If you reset your system locale to, for example, France, and rerun the program, the output is the same. Pascal does not replace the period with a comma, the French decimal unit.
Locale
You can change your application from one native language to another by setting the locale. For information on this and other native language support features, see the Solaris documentation on internationalization.
This compiler uses network licensing, as described in the manual, Installing SunSoft Developer Products (SPARC/Solaris).
When you invoke the compiler, if a license is available, the compiler starts. If no license is available, your request for a license is put on a queue, and your compile job continues when a license becomes available. A single license can be used for any number of simultaneous compiles by a single user on a single machine. There are two licensing-related options:
-noqueue—Does not queue request if no license is available.
-xlicinfo—Returns information on the status of licensing.
The -xlicinfo option does not check out a license. For details on how to obtain a license—where to call, what information to have
ready—refer to the manual, Installing SunSoft Developer Products
(SPARC/Solaris).
Introduction 5
1
6 Pascal 4.0 User’s Guide
Pascal Programs
This chapter cites two examples that illustrate how to compile and execute a program. It also explains how to use the traceback facility to find out why a program fails. The sections are:
Building a program with SPARCompiler Pascal requires three steps:
1. Writing a program in Pascal using an editor and saving it in a file with a
2. Compiling the .p or .pas file using the pc command
3. Executing the program by typing the name of the executable file at the
A Simple Pascal Program
2
A Simple Pascal Program page 7 An Interactive Pascal Program page 10 Where Did My Program Fail? page 13
.p or .pas suffix
system prompt
The following is a simple Pascal program that converts temperatures from Fahrenheit to Celsius. Use an editor to type the code on your system and save it in a file called temp.p.
7
2
program temperature(output) ;
{ Program to convert temperatures from Fahrenheit to Celsius. }
const
MIN = 32 ; MAX = 50 ; CONVERT = 5 / 9 ;
var
fahren: integer ; celsius: real ;
begin
writeln('Fahrenheit Celsius') ; writeln('---------- -------') ; for fahren := MIN to MAX do begin
celsius := CONVERT * (fahren - 32) ; writeln(fahren: 5, celsius: 18: 2) ;
end ;
end.
Compiling the Program
Now compile the program with pc, the Pascal compiler, by typing at the system prompt:
hostname% pc temp.p
Pascal names the compiled version of the program a.out by default.
8 Pascal 4.0 User’s Guide
Running the Program
To run the program, enter a.out at the prompt. The output of temp.p is then displayed:
hostname% a.out Fahrenheit Celsius
---------- ------­32 0.00 33 0.56 34 1.11 35 1.67 36 2.22 37 2.78 38 3.33 39 3.89 40 4.44 41 5.00 42 5.56 43 6.11 44 6.67 45 7.22 46 7.78 47 8.33 48 8.89 49 9.44 50 10.00
2
Renaming the Executable File
It is inconvenient to have the result of every compilation in a file called a.out. If such a file already exists, it is overwritten. You can avoid this in either of the two following ways:
Change the name of a.out after each compilation with the mv command:
hostname% mv a.out temp
Use the compiler –o option to name the output executable file. This
example places the executable code in the file temp:
hostname% pc –o temp temp.p
Pascal Programs 9
2
Now run the program by typing the name of the executable file. The output follows:
hostname% temp Fahrenheit Celsius
---------- -------
32 0.00 33 0.56 34 1.11 .. .. ..
An Interactive Pascal Program
In Pascal, the predefined file variable, input, is equivalent to the operating system standard input file, stdin. Similarly, the file variable, output, is equivalent to the standard output file, stdout.
Following is a Pascal program that copies input to output. Use an editor to type the code on your system and store it in a file called copy.p:
program copy(input, output);
{ This program copies input to output. }
var c: char;
begin while not eof do begin while not eoln do begin read(c); write(c) end; readln; writeln end end. { copy }
10 Pascal 4.0 User’s Guide
Compiling the Program
Use the pc command to compile the program and store it in the executable file copy. Here is the command format:
hostname% pc -o copy copy.p
Running the Program
Because the standard files input and output default to the terminal, the program simply echoes each line you type. The program terminates when you type the end-of-file (Control-d) character at the beginning of a line. Try it:
hostname% copy hello, are you listening?
hello, are you listening? goodbye, I must go now. goodbye, I must go now. (Control-d)
2
Redirecting I/O
To write the output to a file instead of to the terminal, use the redirection operator, >, followed by a file name. For instance, to write to a file called data, enter the following:
Pascal Programs 11
hostname% copy > data hello, are you listening? goodbye, I must go now.
(Control-d)
2
Using the same program, but with the < operator to redirect input, you can print the file on the terminal:
hostname% copy < data hello, are you listening? goodbye, I must go now.
Using a File Name as a File V ariable
You can also redirect the output by listing the file as a file variable in the program statement. The Pascal library associates the file variable with a file of the same name. For example, copy2.p lists data as the input file variable:
program copy2(data, output);
{ This program redirects input. }
var c: char; data: text;
begin reset(data); while not eof(data) do begin while not eoln(data) do begin read(data, c); write(c) end; readln(data); writeln end end. { copy2 }
12 Pascal 4.0 User’s Guide
Assuming that the file data is still in the current directory, you can compile and run the program as follows:
hostname% pc -o copy2 copy2.p hostname% copy2 hello, are you listening? goodbye, I must go now.
Where Did My Program Fail?
SPARCompiler Pascal can trace why a program failed; its traceback utility finds the routine that triggers the error.
Using Pascal T raceback
Pascal traceback installs signal handlers on selected signals and dumps a backtrace when those signals are caught. The backtrace shows the chain of calls leading from the routine in which the error occurred, all the way back to the main program.
2
Pascal catches the following set of signals:
SIGQUIT SIGIOT SIGFPE SIGSYS SIGTERM SIGILL SIGABRT SIGBUS SIGPIPE SIGLOST SIGTRAP SIGEMT
See the signal(3) man page for further information on these signals. After the system produces the traceback, it continues with whatever action it
would have taken if the interposer had not been in place, including calling a user signal handler that was previously set.
The traceback facility uses the debugger dbx. To obtain a traceback, SPARCworks must be installed on your system, and the directory containing dbx must be in your PATH environment variable. If the traceback routine cannot find dbx, it does not produce the traceback.
Use the -notrace command-line option to disable traceback.
Pascal Programs 13
SIGSEGV
2
Using a Sample Program with Segmentation V iolation
A segmentation violation occurs when your program tries to reference memory outside your address space. The operating system detects this action and generates an error message. Following is an example program, SegViol.p, which contains a segmentation violation:
program SegmentationViolation; type Pinteger = ^integer;
procedure ErrorInHere; var IntVar: integer; NullPtr: Pinteger; begin NullPtr := nil; { Next statement causes a SEGV } IntVar := NullPtr^; end;
procedure Call1; procedure Call2; begin ErrorInHere; end; begin Call2; end;
begin Call1; end.
Compiling and Running the Program
When you compile and run the program, you receive output similar to the following. The first line indicates the name of the offending signal—in this case, a segmentation violation.
14 Pascal 4.0 User’s Guide
hostname% pc SegViol.p hostname% a.out
*** a.out terminated by signal 11: segmentation violation *** Traceback being written to a.out.trace Abort (core dumped)
hostname% more a.out.trace
*** Stacktrace of a.out *** Program terminated due to segmentation violation [3] __PC0__sigdie(0xb, 0xefffedf0, 0xefffec30, 0x0, 0x1, 0x0), at 0x12128
---- called from signal handler with signal 11 (SIGSEGV) -----­ [4] ErrorInHere(), at 0x115ec [5] Call2(0xefffefc8, 0xefffefa8, 0xefffef88, 0x0, 0xef74dd58, 0x0), at 0x11624 [6] Call1(0x25400, 0x25800, 0x25b80, 0x25b80, 0x3, 0x0), at 0x11660 [7] program(0x1, 0xeffff0fc, 0x4, 0xef7d0000, 0x2, 0xef74dae8), at 0x116a4 [8] main(0x1, 0xeffff0fc, 0xeffff104, 0x25000, 0x0, 0x0), at 0x116e0 detaching from process 17266
In this example, ErrorInHere reported the error. The ErrorInHere procedure was called by Call1.Call2, which was in turn called by the main program. Routine names, such as Call1.Call2, indicate a nested routine. If Pascal cannot find the name of a routine, for example, because the executable file has been stripped, it prints the hex address.
2
Using the -g Option
If you compile the program with the –g option, the traceback also reports the arguments, the line number, and the file name of each routine.
Pascal Programs 15
2
Try compiling SegViol.p with –g:
hostname% pc -g SegViol.p hostname% a.out
*** a.out terminated by signal 11: segmentation violation *** Traceback being written to a.out.trace Abort (core dumped)
hostname% more a.out.trace
*** Stacktrace of a.out *** Program terminated due to segmentation violation [3] __PC0__sigdie(0xb, 0xefffedf0, 0xefffec30, 0x0, 0x1, 0x0), at 0x12128
---- called from signal handler with signal 11 (SIGSEGV) -----­ [4] ErrorInHere(), line 12 in “SegViol.p” [5] Call2(), line 18 in "SegViol.p" [6] Call1(), line 21 in "SegViol.p" [7] program(), line 25 in "SegViol.p" detaching from process 17285
The program prints the ASCII values of character variables. If you compile some modules with –g and others without, the line numbers
may not be accurate for all the routines.
16 Pascal 4.0 User’s Guide
pc V ersion Number
The Pascal Compiler
The name of the Pascal compiler is pc. If you give pc a file name as an argument, and the file name ends with .p or .pas, pc compiles the file and leaves the result in an executable file, called a.out by default.
The syntax of this command is:
pc [options] filename
This chapter contains the following sections:
pc Version Number page 17 Compile and Link Sequence page 18 Language Preprocessor page 19 File Name Extensions Accepted By pc page 20 Option-Passing on the Command-Line page 21 Option-Passing in the Program Text page 21 Options page 23
3
To identify the version number of pc when you compile your program, call the compiler with the –V option. This option instructs the compiler to produce output that identifies the versions of all the programs used in compiling, the compiler itself, the code generator, and so on.
17
3
To identify the version number given an executable or object file created by the Pascal compiler, use the following command.
hostname% mcs -p a.out | grep Pascal SC4.0 18 Mar 1995 Pascal 4.0
Compile and Link Sequence
You can compile the file any.p with the following command-line:
hostname% pc any.p
This command actually invokes the compiler driver, which calls several programs or passes of the program, each of which processes the program. The output of each pass is the input to the next one.
After several passes, the object file any.o is created. An executable file is then generated with the default name a.out. Finally, the file any.o is removed.
pc calls:
cpp, the C preprocessor or cppas, the preprocessor used when you use the
-xl option
pc0, the Pascal front end
The global optimizer if you use the -O option
cg, the code generator, which generates the relocatable object file
pc3, which checks for conflicts in symbol names
ld, the linker, which generates the executable files using any libraries
necessary to resolve undefined symbols
The above is the default action of pc; some compiler options change what pc calls.
18 Pascal 4.0 User’s Guide
Figure 3-1 shows the sequence of events when you invoke pc.
Compiler
pc
Symbol con-
flict checking
pc3
3
Source and
include files
Compiler
preprocessor
cpp or cppas
Language Preprocessor
Compiler
frontend
pc0
Optimize with
-O[level] option
Figure 3-1 Organization of Pascal Compilation
Optimizer
iropt
Code
generator
cg
Link
editor
ld
Libraries
Executable
a.out
The cpp(1) program is the C language preprocessor. The compiler driver pc normally calls cpp(1) during the first pass of a Pascal compilation. If you use the –xl switch, pc calls the alternate preprocessor cppas. Then cpp(1) and cppas operate on files that contain the extension .p or .pas.
You can give directives to cpp(1) or cppas to define constants, conditionally compile parts of your program, include external files, and take other actions. For example, the following program shows the use of an include directive, which asks cpp(1) to copy the named file into the program before compilation.
The Pascal Compiler 19
3
program showinclude; #include "file.i" begin ... end.
See the man page for cpp(1) for information on its directives and other features. Appendix A, “Pascal Preprocessor,” describes cppas.
File Name Extensions Accepted By pc
Pascal source files generally use the extension .p. The compiler recognizes other file name extensions. Table 3-1 lists the most important extensions.
The table notes that pc can produce assembler source files as well as unlinked object files. In each case, you can pass these partially compiled files to pc, which then finishes the compilation, linking, and loading.
Table 3-1 File Name Suffixes Recognized by Pascal
Suffix Description
.p Usual extension for Pascal source files. .pas Valid extension for a Pascal source file. The extension instructs pc
to put object files in the current directory. The default name of the object file is the name of the source file, but with a .o suffix.
.pi Default extension for Pascal source files that have been processed by
the Pascal preprocessor (either cpp or cppas).
.s Extension for assembler source files that are produced when you call
pc with the -S option.
.o Extension for object files that are generated by the compiler when
you call pc with the -c option.
20 Pascal 4.0 User’s Guide
Option-Passing on the Command-Line
To pass an option on the command-line, use a dash (-) followed by the option name. In some cases, you must supply additional information, such as a file name. For example, this command activates the listing option -l, which is off by default:
hostname% pc -l rmc.p
The following command causes the generated object file to be named rmc instead of the default, a.out.
hostname% pc -o rmc rmc.p
Option-Passing in the Program Text
Some options can be passed to the compiler in program text as well as on the command-line. With this facility, you can use different option values for different parts of a program.
Here are four examples of how options can be passed in program text:
{$P+} {$H*} (*$I-*) {$l+,L-,n+}
3
Table 3-2 shows the options that can be passed in program text.
Table 3-2 Options That Can Be Passed in Program Text
Option Description
b Uses buffering of the file output. C Uses runtime checks (same as t). calign Uses C data formats. H Uses check heap pointers. l Makes a listing. L Maps identifiers and keywords to lowercase.
1
p Uses statement limit counting (different from command-line p
stlimit in the Pascal 4.0 Reference Manual.
The Pascal Compiler 21
). See
3
Table 3-2 Options That Can Be Passed in Program Text (Continued)
Option Description
P Uses partial evaluation of boolean expressions. t Uses runtime checks (same as C, but different from the command-line t u Trims to 72-character line (not usable on command-line). w Prints warning diagnostics.
1. The optionsp and t are different when they are used within pr ogram text and when they are used on the command-line because they are received directly bypc0 when they are used in program text, while the compiler driver gives them to other compiler passes when they are given on the command-line. If you want to set them on the command-line and also want them to have the same effect as passing them in program text, use the Qoption command-line option to pass them directly to pc0.
1
You set options within comments, which can be delimited by either { and } or (* and *). The first character in the comment must be the $ (dollar sign). $ must be immediately followed by an option letter. Next must be either +, -, or *.
If you want to set more than one option, you can insert a comma after the first option, followed by another option letter and +, -, or *. You can set any number of options on a single line. There must be no embedded spaces. You can place spaces and other ordinary comment text after the last +, -, or *.
).
The new option setting takes effect at the next noncomment token. The symbols + (plus sign) and - (minus sign) turn the option on and off,
respectively. To understand the symbol *, you must know how options work in Pascal.
Except for b, each option in Table 3-2 has a current value and a “first in, last out” stack of up to 16 values. Again, except for b, each option can be on or off.
When you begin compiling a program, each stack is empty and each option has an initial current value, which may be the option default value or may have been set on the command line.
22 Pascal 4.0 User’s Guide
When the compiler encounters an option followed by... This is what happens...
+ The current value is pushed onto the stack, and
-
*
If no values have been pushed onto the stack, the effect of * is undefined. Figure 3-2 illustrates how options are passed in program text.
Program: Output:
3
the current value becomes ON. The current value is pushed onto the stack, and
the current value becomes OFF. The last value is popped off the stack and
becomes the current value.
program options (output); begin {$l+ Turns on listing}
writeln ('After $l-'); {$l- Turns off listing} {Notice that this line prints.}
writeln ('After $l+');
{$l* Turns listing on again} {Notice that this line does not print.}
writeln ('After $l*')
end.
Figure 3-2 Options in Program Text
Options
This section describes all the pc command options in alphabetical order. Unless otherwise stated at the beginning of the description for the option, all of these options work for both the Solaris 1.x and Solaris 2.x environments.
In general, processing of the compiler options is from left to right, so selective overriding of macros can be done. This rule does not apply to linker options.
hostname% pc options.p Fri Mar 1 17:33:18 1995 options.p: 4 writeln ('After $l-'); 5 {$l- Turns off listing} 6 {Notice that this line prints.} 10 writeln ('After $l*') 11 end.
The Pascal Compiler 23
3
–a
The –a option is the old style of basic block profiling for tcov. See
-xprofile=tcov for information on the new style of profiling and the tcov(1) man page for more details. Also see the manual, Profiling Tools.
The –a option inserts code to count how many times each basic block is executed. It also calls a runtime recording mechanism that creates a .d file for every .p file at normal termination. The .d file accumulates execution data for the corresponding source file. The tcov(1) utility can then be run on the source file to generate statistics about the program.
If set at compile-time, the TCOVDIR environment variable specifies the directory of where the .d files are located. If this variable is not set, then the .d files remain in the same directory as the .f files.
The -xprofile=tcov and the -a options are compatible in a single executable. That is, you can link a program that contains some files which have been compiled with -xprofile=tcov, and others with -a. You cannot compile a single file with both options.
–Bbinding
The –B option specifies whether libraries for linking are static (not shared, indicated with -Bstatic), or dynamic (shared, indicated with -Bdynamic).
Link editing is the set of operations necessary to build an executable program from one or more object files. Static linking indicates that the results of these operations are saved to a file. Dynamic linking refers to these operations when performed at runtime. The executable that results from dynamic linking appears in the running process, but is not saved to a file.
–b
It is inefficient for Pascal to send each character to a terminal as it generates its output. It is even less efficient if the output is the input of another program, such as the line printer daemon, lpr(1).
To gain efficiency, Pascal buffers output characters; it saves the characters in memory until the buffer is full and then outputs the entire buffer in one system interaction. By default, Pascal output is line-buffered.
24 Pascal 4.0 User’s Guide
The –b option on the command-line turns on block-buffering with a block size of 1,024. You cannot turn off buffering from the command-line.
If you give the –b option in a comment in the program, you can turn off buffering or turn on block buffering. The valid values are:
{$b0} No buffering {$b1} Line buffering {$b2} Block buffering. The block size is 1,024.
Any number greater than 2 (for example, {$b5}) is treated as {$b2}. You can only use this option in the main program. The block buffering value in effect at the end of the main program is used for the entire program.
-bsdmalloc
(Solaris 1.x only) The -bsdmalloc option specifies faster malloc and uses the more efficient malloc from the library, libbsdmalloc.a. This option also causes the flags, -u _malloc /lib/libbsdmalloc.a, to be passed to the linker.
3
–C
The –C option enables runtime checks that verifies that:
Subscripts and subranges are in range.
The number of lines written to output does not exceed the number set by
the linelimit procedure. (See the Pascal 4.0 Reference Manual for information on linelimit.)
Overflow, underflow, and divide-by-zero do not exist.
The assert statement is correct. (See the Pascal 4.0 Reference Manual for
information on assert.)
If you do not specify –C, most runtime checks are disabled, and pc treats the assert statement as a comment and never uses calls to the linelimit procedure to halt the program. However, divide-by-zero checks are always made.
The –V0 and –V1 options implicitly turn on –C.
The Pascal Compiler 25
3
–c
The –c option instructs the compiler not to call the linker, ld(1). The Pascal compiler, pc, leaves a .o or object file for each source file. Without –c, pc calls the linker when it finishes compilation, and produces an executable file, called
a.out by default.
-calign
The -calign option instructs the compiler to allocate storage in records the same way as the C compiler allocates structures. See the Pascal 4.0 Reference Manual for details of how data is aligned with and without -calign.
You can use calign within a program as well as on the command-line. However, calign only has an effect in the type and var sections of the program. Any types you define when calign is on use C-style alignment whenever you define variables of that type.
–cg89
(Solaris 1.x only) The –cg89 option generates code to run on generic SPARC architecture.
(Solaris 2.x only) This option is a macro for:
-xarch=v7 -xchip=old -xcache=64/32/1.
–cg92
(Solaris 1.x only) The –cg92 option generates code to run on SPARC V8 architecture.
(Solaris 2.x only) This option is a macro for:
-xarch=v8 -xchip=super -xcache=16/64/4:1024/64/1
-cond
You can only use this option when you also use the -xl option.
26 Pascal 4.0 User’s Guide
–config
3
The –cond option instructs pc to compile the lines in your program that begin with the %debug compiler directive. If you compile your program without
–cond, pc treats lines with the %debug directive as comments. –xl runs your program through the preprocessor cppas, which handles the
Apollo DOMAIN®-style Pascal compiler directives, such as %debug. See Appendix A, “Pascal Preprocessor,” for a complete description of
conditional variables, cppas, and compiler directives.
You can only use this option when you also use the -xl option. The –config option sets a conditional variable to true. You can only use this
option when you use the preprocessor cppas, which is invoked when you use the -xl option.
Pascal supports the –config option with only one value. For example, Pascal accepts –config one, but not –config one two. To specify more than one variable, use multiple –config options on the command-line.
If you use -config but do not give a variable, the value of the predefined conditional variable %config is set to true.
–xl runs your program through the preprocessor cppas, which handles the Apollo DOMAIN-style Pascal compiler directives, such as %config.
See Appendix A, “Pascal Preprocessor,” for a complete description of conditional variables, cppas, and compiler directives.
–Dname[=def]
The –D option defines a symbol name to the C preprocessor, cpp. It is equivalent to using the #define statement in your program. If you do not include a definition, name is defined as 1. See cpp(1) for more information.
If you use this option with the -xl option, -D is equivalent to using the %config directive in your program.
The Pascal Compiler 27
3
–dalign
The –dalign option instructs the compiler to generate double load and store instructions wherever possible for faster execution. All double-typed data become double-aligned, so do not use this option when correct alignment is not ensured.
-dn
(Solaris 2.x only) The -dn option specifies static linking in the link editor.
-dryrun
The –dryrun option instructs the compiler to show, but not execute, the commands constructed by the compilation driver. You can then see the order of execution of compiler passes without actually executing them.
-dy
(Solaris 2.x only) The -dy option specifies dynamic linking in the link editor.
–fast
(Solaris 1.x only) The –fast option selects optimum compilation options for speed and provides close to the maximum performance for most realistic applications. A convenience option, it chooses the fastest code generation option available on the compile-time hardware, the optimization level -02, the
-dalign option, and a set of inline expansion templates. If you combine - fast with other options, the last specification applies.
(Solaris 2.x only) The -fast option includes -fns -ftrap=%none; that is, it turns off all trapping. In previous releases, the -fast macro option included
-fnonstd; now it does not.
-fast includes -native in its expansion.
The code generation option, the optimization level, and using inline template files can be overridden by subsequent switches. For example, although the optimization part of -fast is -O2, the optimization part of -fast -03 is -03.
28 Pascal 4.0 User’s Guide
-fnonstd
3
Do not use this option for programs that depend on IEEE standard exception handling; you can get different numerical results, premature program termination, or unexpected SIGFPE signals.
Note – The criteria for the -fast option vary with the compilers from SunSoft: C, C++, FORTRAN 77, Fortran 90, and Pascal. See the appropriate documentation for the specifics.
The -fnonstd option causes nonstandard initialization of floating-point arithmetic hardware. By default, IEEE 754 floating-point arithmetic is nonstop, and underflows are gradual. (See the Numerical Computation Guide for details.) The –fnonstd option causes hardware traps to be enabled for floating-point overflow, division by zero, and invalid operation exceptions. These hardware traps are converted into SIGFPE signals, and if the program has no SIGFPE handler, it terminates with a memory dump.
-fnonstd also causes the math library to be linked in by passing -lm to the linker.
–fns
–fround=r
(Solaris 2.x only) This option is a synonym for -fns -ftrap=common.
(Solaris 2.x only) The -fns option turns on the SPARC non-standard floating- point mode.
The default is the SPARC standard floating-point mode. If you compile one routine with -fns, then compile all routines of the program
with the –fns option; otherwise, unexpected results may occur.
(Solaris 2.x only) The -fround=r option sets the IEEE 754 rounding mode that is established during program initialization.
r must be one of: nearest, tozero, negative, positive.
The Pascal Compiler 29
3
The default is -fround=nearest. The meanings are the same as those for the ieee_flags subroutine. If you compile one routine with -fround=r, compile all routines of the
program with the same –fround=r option; otherwise, unexpected results may occur.
–ftrap=t
(Solaris 2.x only) The -ftrap=t option sets the IEEE 754 trapping mode in effect at startup.
t is a comma-separated list of one or more of the following: %all, %none, common, [no%]invalid, [no%]overflow, [no%]underflow, [no%]division,
[no%]inexact. The default is -ftrap=%none. This option sets the IEEE 754 trapping modes that are established at program
initialization. Processing is left-to-right. The common exceptions, by definition, are invalid, division by zero, and overflow.
Example: -ftrap=%all,no%inexact means set all traps, except inexact. The meanings are the same as for the ieee_flags function, except that:
%all turns on all the trapping modes.
%none, the default, turns off all trapping modes.
A no% prefix turns off that specific trapping mode.
If you compile one routine with -ftrap=t, compile all routines of the program with the same -ftrap=t option; otherwise, unexpected results may occur.
-G
(Solaris 2.x only) The -G option builds a shared library. All object files specified with this command option should have been compiled with either the -pic or the -PIC option.
30 Pascal 4.0 User’s Guide
–g
–H
3
The –g option instructs pc to produce additional symbol table information for dbx and debugger. With -g, the incremental linker, ild, is called, instead of ld.
You can compile using both the -g and -O options. However, there are some side effects:
The next and step commands do not work, but the cont command does.
If you have makefiles that rely on -g overriding -O, you must revise those
files.
If you have makefiles that check for a warning message that -g overrides
-O, you must revise those make files.
Note – Special case: -04 -g. The combination -04 -g turns off inlining that you usually get with -04.
-hname
The –H option instructs pc to compile code to perform range-checking on pointers into the heap. This option is implicitly turned on by the –V0 and –V1 options.
(Solaris 2.x only) The -hname option names a shared dynamic library and provides a way to have versions of a shared dynamic library.
This is a loader option, passed to ld. In general, the name after -h should be exactly the same as the one after -o. A space between the -h and name is optional.
The compile-time loader assigns the specified name to the shared dynamic library you are creating. It records the name in the library file as the intrinsic name of the library. If there is no -hname option, then no intrinsic name is recorded in the library file.
The Pascal Compiler 31
3
Every executable file has a list of needed shared library files. When the runtime linker links the library into an executable file, the linker copies the intrinsic name from the library into that list of needed shared library files. If there is no intrinsic name of a shared library, then the linker copies the path of the shared library file instead.
–help or -flags
The –help or -flags option lists and summarizes all available options.
–Ipathname
The –I option gives the preprocessor additional places to look for #include and %include files. For example,
hostname% pc -I/home/incfiles -I/usr/incfiles program.p
The preprocessor searches for #include and %include files in this order:
1. In /opt/SUNWspro/SC4.0/include/pascal
2. In the directory containing the source file, except when you use the #include <file> form, in which case this directory is not searched
3. In directories named with –I options, if any, in left to right order
4. In /usr/include
–i name
The –i option produces a listing for the specified procedure, function, #include, or %include file. For example, this command instructs the compiler to make a listing of the routines in the file scanner.i.
hostname% pc –i scanner.i program.p
See cpp(1), or Chapter 4, “Program Construction and Management,” or Chapter 5, “Separate Compilation,” for information on include files.
-keeptmp
The -keeptmp option keeps temporary files that are created during compilation, so they are retained instead of being deleted automatically.
32 Pascal 4.0 User’s Guide
–L
–l
3
The –L option maps all keywords and identifiers to lowercase. In Pascal, uppercase and lowercase are not interchangeable in identifiers and keywords. In standard Pascal, the case is insignificant outside of character strings and character constants. The –L option is most useful for transporting programs from other systems. See also the –s option.
The –l option produces a listing of the program. For example:
hostname% pc -l random.p Pascal PC -- Version SC4.0 09 Jan 1995 Pascal 4.0
Mon Jan 09 09:04 1995 random.p:
1 program random_number(output); 2 var 4 i: integer; 5 x: integer; 6 7 begin 8 for i := 1 to 5 do begin 9 write(trunc(random(x) * 101)) 10 end; 11 writeln 12 end.
-Ldirectory
The first line identifies the version of the compiler you are using. The next line gives the modification time of the file being compiled. The remainder of the listing is the source program.
The -Ldirectory option adds directory to the ld library search path for ld.
The Pascal Compiler 33
3
-libmieee
Forces IEEE 754 style return values for math routines in exceptional cases. In such cases, no exception message is printed, and errno is not set.
–libmil
The –libmil option instructs the compiler to select the best inline templates for the floating-point option and operating system release available on this system.
–llib
The –llib option links ld(1) with the object library, lib. Do not use the -lucb option because Pascal is not compatible with the object
library, libucb.
–misalign
The –misalign option allows for misaligned data in memory. Use this option only if you receive a warning message that your data is misaligned.
With the –misalign option, pc generates much slower code for references to formal parameters. If possible, recode the indicated section instead of recompiling your program with this option.
-mt
The -mt option uses multithread-safe libraries, eliminates conflicts between threads, so that Pascal library routines can be safely used in a multiprocessing environment.
The MT-safe library for Pascal is called libpc_mt. On a single-processor system, the code that is generated with this option runs
more slowly; the degradation in performance is usually insignificant, however. Refer to the Multithreaded Programming Guide in the Solaris documentation for
more information.
34 Pascal 4.0 User’s Guide
–native
-nocx
-nolib
3
The –native option causes pc to generate code for the best floating-point hardware available on the machine you are compiling on.
The -fast macro includes -native in its expansion.
(Solaris 2.x only) This option is a synonym for -xtarget=native.
(Solaris 1.x only) The -nocx option makes the output executable file about
128K bytes smaller by not linking with the -lcx option. However, the runtime performance and accuracy of binary-decimal base conversion is somewhat compromised.
The -nolib option instructs the compiler not to link any libraries by default—that is, no -l options are passed to ld. Normally, the pc driver passes -lc to ld.
–nolibmil
-noqueue
When you use -nolib, pass all -l options yourself. For example, the following command links libm statically and the other libraries dynamically:
hostname% pc -nolib -Bstatic -lm -Bdynamic -lc test.p
The –nolibmil option instructs the compiler to reset –fast so that it does not include inline templates. Use this option after the –fast option, as in:
hostname% pc –fast –nolibmil myprog.p
The -noqueue option instructs the compiler not to queue this compilation if a license is not available. Under normal circumstances, if no license is available, the compiler waits until one becomes available. With this option, the compiler returns immediately.
The Pascal Compiler 35
3
-notrace
The -notrace option disables runtime traceback. It is only effective when compiling the main program.
–O[level]
The –O option instructs the compiler to run the compiled program through the object code optimizer. The –O option also calls the –P option, which ensures boolean expressions are only evaluated as much as needed to determine the result. This process causes an increase in compile time in exchange for a decrease in compiled code size and execution time.
There are four levels of optimization. You indicate the level by specifying a digit from 1 to 4 after the –O option. If you leave out the digit, the optimization level defaults to –O2.
The level numbers are interpreted as follows:
–O
This is the most likely level of optimization to give fastest performance for most reasonable applications. The default is –O2.
–O1,-xO1
This is the minimum amount of optimization (peephole) and is postpass assembly-level. Do not use –O1 unless -O2 and -O3 result in excessive compilation time or shortage of swap space.
–O2, -xO2
This is the basic local and global optimization—induction-variable elimination, local and global common subexpression elimination, algebraic simplification, copy propagation, constant propagation, loop­invariant optimization, register allocation, control-flow optimization, tail-recursion elimination, dead-code elimination, and tail-call elimination.
Level -O2 does not optimize references to or definitions of external or indirect variables. This level is the appropriate level for device drivers and programs that modify external variables from within signal handlers.
36 Pascal 4.0 User’s Guide
3
–O3, -xO3
Same as -O2, but optimizes the uses and definitions of external variables. Level -O3 does not trace the effects of pointer assignments. Do not use Level -O3 when compiling device drivers or programs that modify external variables from within signal handlers.
–O4, -xO3
Same as -O3, but traces the effects of pointer assignments and gathers alias information. Do not use Level -O4 when compiling device drivers or programs that modify external variables from within signal handlers.
-O5, -xO5
(Solaris 2.x only) Generates the highest level of optimization. This level uses optimization algorithms that take more compilation time or that do not have as high a certainty of improving execution time.
Optimization at this level is more likely to improve performance if it is done with profile feedback. See -xprofile.
Note – Levels -O3 and -O4 may result in an increase in the size of the executables. When optimizing for size, use level -O2. For most programs, –O4 is faster than –O3, which is faster than –O2, which is faster than –O1. However, in a few cases –O2 may be faster than the others, and –O3 may be faster than –O4. You can try compiling with each level to see if you have one of these rare cases.
If the optimizer runs out of memory, it tries to recover by retrying the current procedure at a lower level of optimization, then resumes subsequent procedures at the original level specified in the –O command-line option.
If you optimize at –O3 or –O4 with very large procedures (thousands of lines of code in a single procedure), the optimizer may require an unreasonable amount of memory. Such cases may result in degraded machine performance.
You can prevent this from happening in the C shell by limiting the amount of virtual memory available to a single process. To do this, use the limit command (see csh(1)).
For example, to limit virtual memory to 16 megabytes:
hostname% limit datasize 16M
The Pascal Compiler 37
3
This command causes the optimizer to try to recover if it reaches 16 megabytes of data space.
This limit cannot be greater than the machine’s total available swap space, and in practice, should be small enough to permit normal use of the machine while a large compilation is in progress. For example, on a machine with 32 megabytes of swap space, the command limit datasize 16M ensures that a single compilation never consumes more than half of the machine’s swap space.
The best setting of data size depends on the degree of optimization requested and the amount of real memory and virtual memory available. To find the actual swap space:
hostname% /usr/sbin/swap -s
To find the actual real memory:
hostname% /usr/sbin/prtconf | grep Memory
–o filename
The –o option instructs the compiler to name the generated executable,
filename. The default file name for executable files is a.out; for object files, it is the source file name with a .o extension. For example, the following command stores the executable in the file, myprog:
hostname% pc -o myprog myprog.p
If you use this option with the -c option, the name you give is used as the name for the object file. The default file name for object files is the source file name with a .o extension. You cannot give the object file the same name as the source file.
–P
The –P option causes the compiler to use partial evaluation semantics on the boolean operators, and and or. Left-to-right evaluation is guaranteed, and
the second operand is evaluated only if necessary to determine the result.
38 Pascal 4.0 User’s Guide
–p and –pg
3
The –p and –pg options instruct the compiler to produce code that counts the number of times each routine is called. The profiling is based on a periodic sample taken by the system, rather than by line counters.
Using the -p Option
To generate an execution profile using the –p option:
1. Compile with the –p option.
2. Run a.out, which produces a mon.out executable file.
3. Type prof a.out. The program prints a profile.
Using the -pg Option
To generate an execution profile using the –pg option:
1. Compile with the –pg option.
2. Run a.out, which produces a gmon.out executable file, a more sophisticated profiling tool than mon.out.
3. Type gprof a.out. The program prints a profile.
–pic, -Kpic and –PIC, -KPIC
The -pic and -PIC options cause the compiler to generate position­independent code (PIC). One of these options should be used for objects which are then put into shared libraries. With PIC, each reference to a global datum is generated as a dereference of a pointer in the global offset table. Each function call is generated in pc-relative addressing mode through a procedure linkage table.
The size of the global offset table is limited to 8Kbytes with -pic. The -PIC option expands the global offset table to handle 32-bit addresses for those rare cases where there are too many data objects for -pic.
For more information on PIC, see the section on shared libraries in the Solaris documentation.
The Pascal Compiler 39
3
–Qoption
The –Qoption passes an option to the program. The option value must be appropriate to that program and can begin with a plus or minus sign. The program value can be either as(1) (Solaris 1.x only), fbe(1) (Solaris 2.x only), cpp(1), cppas, inline(1), iropt, ld(1), pc0, or pc3. For example, the following command passes the option -R to cpp and allows recursive macros:
hostname% pc -Qoption cpp -R myprog.p
–Qpath pathname
The –Qpath option inserts a path name into the compilation search path, hence providing an alternate path to search for each compiler component. You can select this option, for instance, to use a different linker or assembler. In the following command, pc searches /home/pascal/sparc for the compiler components and uses them if it finds them; if pc does not find the specified components, it uses the default components:
hostname% pc -Qpath /home/pascal/sparc testp.p
–Qproduce
The –Qproduce option instructs pc to produce source code of the type
sourcetype, which can be one of the following:
.o Object file from as(1) .pi Preprocessed Pascal source from cpp(1) .s Assembler source. This option is the same as the -S option.
For example, the following command produces the file, hello.s:
hostname% pc -Qproduce .s hello.p
-qp
The -qp option is the same as -p option.
40 Pascal 4.0 User’s Guide
-R
(Solaris 1.x only) The –R option instructs pc to call the assembler, as(1). This option merges the data segment of the resulting program with the text segment. See also -xMerge (Solaris 2.x only).
-R path[:dir]
(Solaris 2.x only) The -Rpath[:dir] option passes a colon-separated list of
directories that specify the library search path used by the runtime linker. If present and not null, it is recorded in the output object file and passed to the runtime linker.
If both LD_RUN_PATH and the -R option are specified, the -R option takes precedence.
-Rw
The -Rw option checks and issues warnings on record fields which are used, but not set.
3
By default, the Pascal compiler generates warnings of this kind for whole variables, but not for fields.
This option works only for local record variables that are defined in procedures or functions, not for global variables, that is, variables that are in the main program or in a separately compiled module. This is because global variables may appear to be initialized not in the main program itself, but in some procedure or function that is compiled separately, which is subsequently linked to the executable program.
This option is suppressed when the -Z option is on. See “–Z” on page 66. In this case, all local variables and their components are initialized by zero values.
When this option is on, the compiler performs a full analysis (as far as possible at compile time) of how record fields are assigned and used. Warnings contain full access constructs for fields which are used, but not set, for example, V.F1.F2^.F3.
The Pascal Compiler 41
3
The compiler issues warnings at the end of the procedure where the record variables are defined, that is, when some of the fields are definitely not set. However, no warnings are issued if fields are used in the source before they are initialized, as the control flow may be different.
In some cases, it is not possible to determine at compile time whether the fields have actually been initialized. For example:
For the array variable V, whose elements are records, if any assignment of
the kind V[i]:= X or V[i].F:= Y occurs, the compiler considers the corresponding fields of V[i] for all values of i to be initialized. If such a field is used, but not set, it is denoted as V[...].F in the warning message.
All formal parameters are assumed to be initialized. Consequently, the
compiler does not perform any checking for these component fields.
With the -Rw option, the compiler takes into account built-in procedures which initialize their argument variables, for example, reset(f) for the file buffer variable f^ and its components. rewrite(f) does not initialize f^. The compiler also examines field handling inside WITH statements.
Use the -Rw option to check the use of “unsafe” variant records, such as the assignment of a variant to a field, or the use of another field from a “parallel” variant. These practices may result in runtime errors which are hard to find.
Note – The -Rw option requires extra compile-time, and is, therefore, recommended for use in debugging only.
42 Pascal 4.0 User’s Guide
Examples:
3
The Pascal main program, r.p (record and array of records)
The commands to compile r.p and the -Rw warnings that are issued
The Pascal main program,
rr.p (two records)
program p; procedure qq; type compl = record re, im: integer end; arc = array[1..2] of compl; var z: compl; a: arc; begin writeln(z.im); writeln(a[1].re); end; begin end.
hostname% pc -Rw r.p Fri Jan 27 17:35:50 1995 r.p: In procedure qq: w 18280 field z.im is used but never set w 18280 field a[...].re is used but never set
program p; type r = record a,b: integer end; procedure qq; var r1, r2: r; var i: integer; begin i:=r1.a; i:=r2.a; i:=r1.b; i:=r2.b; end; begin qq; end.
The Pascal Compiler 43
3
The commands to compile rr.p and the -Rw warnings that are issued
The Pascal main program,
recvar.p (variant record)
hostname% pc -Rw rr.p Mon Feb 20 14:59:04 1995 pas/rr.p: In procedure qq: w 18280 field r1.b is used but never set w 18280 field r1.a is used but never set w 18280 field r2.b is used but never set w 18280 field r2.a is used but never set
program p; procedure qq; type r = record
x,y: integer;
case integer of 0:(a: integer); 1: (b: char);
end; var v: r; begin v.x:= 1; writeln(v.y); end; begin qq; end.
The commands to compile
recvar.p
44 Pascal 4.0 User’s Guide
hostname% pc -Rw recvar.p Mon Feb 20 15:55:18 1995 recvar.p: In procedure qq: w 18260 field v.a is neither used nor set w 18260 field v.b is neither used nor set w 18280 field v.y is used but never set hostname% a.out 0
3
The Pascal main program,
with.p (with statement)
program p; type C = record re, im: integer end; AC = array[1..2] of C; RC = record C1, C2: C end; PRC = ^RC; procedure qq; var c: C; ac: AC; rc: RC; prc: PRC; begin ac[1]:= c; with ac[1] do begin re:= 1; writeln(im); end; with prc^.C1 do begin writeln(im); end; end; begin qq; end.
The commands to compile and execute with.p
hostname% pc -Rw with.p Mon Feb 20 16:28:34 1995 with.p: In procedure qq: w 18280 variable c is used but never set w 18260 variable rc is neither used nor set w 18280 field prc^.C1.im is used but never set hostname% a.out 0
*** a.out terminated by signal 11: segmentation violation *** Traceback being written to a.out.trace Abort (core dumped)
The Pascal Compiler 45
3
–S
The –S option compiles the program and outputs the assembly language in the file, sourcefile.s. For example, the following command places the assembly language translation of rmc.p in the file rmc.s. No executable file is created.
hostname% pc –S rmc.p
–s[level]
The –s option instructs the compiler to accept standard Pascal only. Pascal has two levels of compliance with standard Pascal: Level 0 and Level 1. The only difference between the two is that Level 1 also allows conformant arrays.
Specify the level of compliance as follows:
–s0 Accept Level 0 compliance with standard Pascal
-s or –s1 Accept Level 1 compliance with standard Pascal
This option causes many features of Pascal that are not found in standard Pascal to be diagnosed with warning messages. These features include:
Nonstandard procedures and functions
Extensions to the procedure write
Padding of constant strings with blanks
Preprocessor directives
In addition, all letters, except character strings and constants, are mapped to lowercase. Thus, the case of keywords and identifiers is ignored.
This option is most useful when a program is to be ported to other machines.
–sb
The –sb option produces a database for source browsing.
–sbfast
The –sbfast option performs the same task as -sb, but does not compile.
46 Pascal 4.0 User’s Guide
-tc
3
The -tc option instructs the compiler to generate pc3 stab information that allows cross-module type checking.
This option can be used for two purposes:
To check for any name conflicts that your program may have with the
standard libraries with which it is to be linked, such as libc. The linker allows name conflicts, which may cause erroneous runtime behavior in your program.
For example, the following program has a name conflict with libc:
program p(output); var time: integer; begin writeln(wallclock); end.
When the program is compiled with the -tc option, pc3 issues a warning that the name time is already defined as a libc routine. Running a.out causes a core dump. To avoid this problem, change the name of the variable that has the conflict—in this case, time.
–temp=dir
To check for possible name conflicts in the various modules of your
program. These conflicts arise if you define a routine with the same name in several modules, or refer to an external, but undefined, variable. The linker detects these error situations and does not create the executable file.
The –temp option instructs pc to locate the temporary files that it creates during compilation in the directory named dir. For example, the following command puts the temporary files in the current directory.
hostname% pc -temp=. hello.p
If you do not specify a temporary directory, pc places the temporary files in the /tmp directory.
The Pascal Compiler 47
3
–time
The –time option instructs the compiler to report execution performance statistics for the various compilation passes. Here is some sample output; some spaces have been removed so the output would fit on the page.
hostname% pc -time hello.p cpp:time U:0.0s+S:0.1s=0.2s REAL:1.6s 11%. core T:0k D:0k. io IN:4b OUT:3b. pf IN:25p OUt:184p. pc0:time U:0.0s+S:0.3s=0.4s REAL:3.2s 13%. core T:0k D:4k. io IN:4b OUT:4b. pf IN:70pOUT:131p. cg: time U:0.0s+S:0.1s=0.2s REAL:2.0s 12%. core T:0k D:1k. io IN:2b OUT:1b. pf IN:39p OUT:163p. as: time U:0.0s+S:0.2s=0.3s REAL:1.5s 19%. core T:0k D:1k. io IN:3b OUT:10b.pf IN:33pOUT:117p. pc3:time U:0.1s+S:0.1s=0.3s REAL:0.9s 31%. core T:0k D:1k. io IN:7b OUT:0b. pf IN:20pOUT:109p. ld:time U:0.8s+S:0.9s=1.8sREAL:10.2s 17%. core T:0k D:21k.io IN:74bOUT:29b.pf IN:89pOUT:184p.
Each line begins with the name of the compiler pass. The rest of the line is divided into four parts: time, core, io, and pf.
time gives the time used by that pass of the compiler, in this order:
a. User time
b. System time
c. Total CPU time, which is the sum of user and system time
d. Real (clock) time
e. Percent of real time used by CPU time
core gives memory usage statistics for the pass, in this order:
a. The first item is always 0, and currently has no meaning.
b. The second item is the integral resident set size.
The io section gives the volume of input and output operations, expressed
in blocks.
The pf section gives the amount of page faults, expressed in pages, in this
order:
a. Page faults not requiring physical I/O
b. Page faults requiring physical I/O
48 Pascal 4.0 User’s Guide
–U name
–V
–V0 and –V1
–v
3
The –U option removes any initial definition of the cpp(1) symbol name. See cpp(1) for more information. You cannot use this option with the -xl option.
The –V option prints the version number of each compilation pass.
The –V0 and –V1 options turn on sets of options that insert checks into the object file, as follows:
–V0 Equivalent to -C, -H, -L, and -s0 –V1 Equivalent to –C, –H, –L, and –s1
–w
-xa
-xarch=a
The –v (verbose) option prints the command line used to call each compilation pass.
By default, the compiler prints warnings about inconsistencies it finds in the input program. The –w option turns off the warnings.
To turn off warnings in a program comment, use this command:
hostname% {$w-}
Same as -a.
(Solaris 2.x only) The -xarch=a option limits the set of instructions the compiler may use.
The Pascal Compiler 49
3
a must be one of: generic, v7, v8, v8a, v8plus, v8plusa.
Although this option can be used alone, it is part of the expansion of the
xtarget option; its primary use is to override a value supplied by the xtarget option.
This option limits the instructions generated to those of the specified architecture, and allows the specified set of instructions. It does not guarantee an instruction is used; however, under optimization, it is usually used.
If this option is used with optimization, the appropriate choice can provide good performance of the executable on the specified architecture. An inappropriate choice can result in serious degradation of performance.
50 Pascal 4.0 User’s Guide
3
v7, v8, and v8a are all binary compatible. v8plus and v8plusa are binary compatible with each other and forward, but not backward. For any particular choice, the generated executable can run much more slowly on earlier architectures (to the left in the above list).
Table 3-3 The -xarch Values
Value Meaning
generic Get good performance on most SPARCs, and major degradation on none.
This is the default. This option uses the best instruction set for good performance on most SPARC processors without major performance degradation on any of them. With each new release, this best instruction set will be adjusted, if appropriate.
v7 Limit the instruction set to V7 architecture.
This option uses the best instruction set for good performance on the V7 architecture, but without the quad-precision floating-point instructions. This is equivalent to using the best instruction set for good performance on the V8 architecture, but without the following instructions: The quad-precision floating-point instructions The integer mul and div instructions The fsmuld instruction
Examples: SPARCstation 1, SPARCstation 2
v8a Limit the instruction set to the V8a version of the V8 architecture.
This option uses the best instruction set for good performance on the V8 architecture, but without: The quad-precision floating-point instructions The fsmuld instruction
Example: Any machine based on MicroSPARC I chip architecture
The Pascal Compiler 51
3
Table 3-3 The -xarch Values (Continued)
Value Meaning
v8 Limit the instruction set to V8 architecture.
This option uses the best instruction set for good performance on the V8 architecture, but without quad-precision floating-point instructions.
Example: SPARCstation 10
v8plus Limit the instruction set to the V8plus version of the V9 architecture.
By definition, V8plus, or V8+, means the V9 architectue, except: Without the quad-precision floating point instructions Limited to the 32-bit subset defined by the V8+ specification Without the VIS instructions
This option uses the best instruction set for good performance on the V9 architecture. In V8+, a system with the 64-bit registers of V9 runs in 32-bit addressing mode, but the upper 32 bits of the i and l registers must not affect program results.
Example: Any machine based on UltraSPARC chip architecture.
Use of this option also causes the .o file to be marked as a Sun-specific V8+ binary; such files will not run on a
v8plusa Limit the instruction set to the V8plusa version of the V9 architecture.
By defintion, V8plusa means the V8plus arcitecture, plus: The UltraSPARC-specific instructions The VIS instructions This option uses the best instruction set for good performance on the UltraSPARC™ architecture but limited to the 32-bit subset defined by the V8+ specification.
Example: Any machine based on UltraSPARC chip architecture.
Use of this option also causes the .o file to be marked as a Sun-specific V8+ binary; such files will not run on a
52 Pascal 4.0 User’s Guide
v7 or v8 machine.
v7 or v8 machine.
-xcache=c
3
(Solaris 2.x only) The -xcache=c option defines the cache properties for use by the optimizer.
c must be one of the following:
generic
s1/l1/a1
s1/l1/a1:s2/l2/a2
s1/l1/a1:s2/l2/a2:s3/l3/a3
The si/li/ai are defined as follows:
si The size of the data cache at level i, in kilobytes li The line size of the data cache at level i, in bytes ai The associativity of the data cache at level i
Although this option can be used alone, it is part of the expansion of the ­target option; its primary use is to override a value supplied by the ­target option.
This option specifies the cache properties that the optimizer can use. It does not guarantee that any particular cache property is used.
Table 3-4 The -xcache Values
Value Meaning
generic Define the cache properties for good performance on most SPARCs.
This is the default value which directs the compiler to use cache properties for good performance on most SPARC processors, without major performance degradation on any of them.
With each new release, these best timing properties will be adjusted, if appropriate.
s1/l1/a1 Define level 1 cache properties. s1/l1/a1:s2/l2/a2 Define levels 1 and 2 cache properties. s1/l1/a1:s2/l2/a2:s3/l3/a3 Define levels 1, 2, and 3 cache properties
The Pascal Compiler 53
3
Example: -xcache=16/32/4:1024/32/1 specifies the following:
-xchip=c
Level 1 cache has: 16K bytes 32 bytes line size 4-way associativity
(Solaris 2.x only) The -xchip=c option specifies the target processor for use by the optimizer.
c must be one of: generic, old, super, super2, micro, micro2, hyper, hyper2, powerup, ultra
Although this option can be used alone, it is part of the expansion of the ­target option; its primary use is to provide a value supplied by the -target
option. This option specifies timing properties by specifying the target processor. Some effects are:
Level 2 cache has: 1024K bytes 32 bytes line size Direct mapping associativity
The ordering of instructions, that is, scheduling
The way the compiler uses branches
The instructions to use in cases where semantically equivalent
alternatives are available
Table 3-5 The -xchip Values
Value Meaning
generic Use timing properties for good performance on most SPARCs.
This is the default value that directs the compiler to use the best timing properties for good performance on most SPARC processors, without major performance degradation on any of them.
old Use timing properties of pre-SuperSPARC™ processors. super Use timing properties of the SuperSPARC chip. super2 Use timing properties of the SuperSPARC II chip. micro Use timing properties of the MicroSPARC™ chip.
54 Pascal 4.0 User’s Guide
-xcg89
-xcg92
–xF
Table 3-5 The -xchip Values (Continued)
Value Meaning
micro2 Use timing properties of the MicroSPARC II chip. hyper Use timing properties of the HyperSPARC™ chip. hyper2 Use timing properties of the HyperSPARC II chip.
®
powerup Use timing properties of the Weitek ultra Use timing properties of the UltraSPARC chip.
Same as -cg89.
Same as -cg92.
PowerUp™ chip.
3
(Solaris 2.x only) The –xF option enables performance analysis of the executable file using the SPARCworks Performance Analyzer and Debugger. This option also causes the assembler to generate some debugging information in the object file, necessary for data collection. The compiler generates code that can be reordered at the function level. It takes each function in the file and places it into a separate section. For example, functions fcn1() and fcn2() are placed in the sections .text%fcn1 and .text%fcn2. You can control the order of functions in the final executable by using the –xF and the loader –Mmapfile options.
In the map file, if you include the flag O in the string of segment flags, then the static linker ld attempts to place sections in the order they appear in the map file. See the Solaris documentation for details about this option, the segment flags, and the map file.
The Pascal Compiler 55
3
-xildoff
(Solaris 2.x only) Turns off the incremental linker and forces the use of ld. This option is the default if you do not use the -g option, or you do not use the -G option, or any source files are present on the command line. Override this default by using the -xildon option.
-xildon
(Solaris 2.x only) Turns on the incremental linker and forces the use of ild in incremental mode. This option is the default if you use the -g option, and you do not use the -G option, and there are no source files present on the command line. Override this default by using the -xildoff option.
–xl
The –xl option implements a set of features that provide broad compatibility with Apollo Pascal. We recommend using –xl only when porting Pascal systems from Apollo platforms to SPARC system platforms. See the Pascal 4.0 Reference Manual for details of the features controlled by -xl.
When you use -xl, the compiler invokes the cppas preprocessor in place of cpp(1). See Appendix A, “Pascal Preprocessor,” for information on cppas.
Modules compiled with –xl are not compatible with modules compiled without –xl. You should not link these two types of modules together.
-xlibmieee
Same as -libmieee.
-xlibmil
Same as -libmil.
56 Pascal 4.0 User’s Guide
-xlibmopt
–xlicinfo
–xMerge
3
Uses a math routine library optimized for performance. The results may be slightly different than those produced by the normal math library. This option is implied by the -fast option.
The -xlicinfo option returns information about the licensing system. In particular, it returns the name of the license server and the IDs of users who have licenses checked out. When you give this option, the compiler is not invoked and a license is not checked out.
(Solaris 2.x only) The –xMerge option instructs pc to call the assembler, as(1), with the –R option. This option merges the data segment of the resulting program with the text segment. See also -R (Solaris 1.x only).
-xnolib
Same as -nolib.
-xnolibmopt
Resets -fast, and does not use the math routine library. Use this option after the -fast option on the command-line, as in:
pc -fast -xnolibmopt ....
-x05
Optimizes the object code. (Solaris 2.x) This option can be combined with –g, but not with –xa. When -O is used with the -g option, a limited amount of debugging is
available.
The Pascal Compiler 57
3
Generates the highest level of optimization. Uses optimization algorithms that take more compilation time or that do not have as high a certainty of improving execution time. Optimization at this level is more likely to improve performance if it is done with profile feedback. See -xprofile=p.
If the optimizer runs out of memory, it tries to recover by retrying the current procedure at a lower level of optimization and resumes subsequent procedures at the original level specified in the command-line option.
-xpg
Same as -p and -pg
-xprofile=p
(Solaris 2.x only) The -xprofile=p option collects data for a profile or use a profile to optimize.
p must be collect, use[
:name
], or tcov.
58 Pascal 4.0 User’s Guide
3
This option causes execution frequency data to be collected and saved during execution, then the data can be used in subsequent runs to improve performance.
Table 3-6 The -xprofile Values
Value Meaning
collect Collect and save execution frequency for later use by the optimizer.
The compiler inserts code to measure the execution frequency at a low level. During execution, the measured frequency data is written into .prof files that correspond to each of the source files.
If you run the program several times, the execution frequency data accumulates in the .prof files; that is, output from prior runs is not lost.
use Use execution frequency data saved by the compiler.
Optimize by using the execution frequency data previously generated and saved in the .prof files by the compiler.
The source files and the compiler options (excepting only this option), must be exactly the same as for the compilation used to create the compiled program that was executed to create the .prof files.
tcov Correctly collects data for programs that have source code in header files
or make use of C++ templates. See -a for information on the old style of profiling, the tcov(1) man page, and the Profiling Tools manual for more details.
Code instrumentation is performed similarly to that of -a, but .d files are no longer generated. Instead, a single file is generated, whose name is based off of the final executable. For example, if the program is run out of /foo/bar/myprog, then the data file is stored in /foo/bar/myprog.profile/myprog.tcovd.
When running tcov, you must pass it the -x option to make it use the new style of data. If not, tcov uses the old .d files, if any, by default for data, and produces unexpected output.
Unlike -a, the TCOVDIR environment variable has no effect at compile­time. However, its value is used at program runtime.
The Pascal Compiler 59
3
-xregs=r
(Solaris 2.x only) The -xregs=r option specifies the usage of registers for the generated code.
r is a comma-separated list that consists of one or more of the following: [no%]appl, [no%]float.
Example: -xregs=appl,no%float
Table 3-7 The -xregs Values
Value Meaning
appl Allow using the registers g2, g3, and g4.
In the SPARC ABI, these registers are described as application registers. Using these registers can increase performance because fewer load and store instructions are needed. However, such use can conflict with some old library programs written in assembly code.
no%appl Do not use the appl registers. float Allow using the floating-point registers as specified in the SPARC ABI.
You can use these registers even if the program contains no floating­point code.
no%float Do not use the floating-point registers.
With this option, a source program cannot contain any floating-point code.
The default is -xregs=appl,float.
60 Pascal 4.0 User’s Guide
–xs
3
(Solaris 2.x only) The -xs option disables Auto-Read for dbx in case you cannot keep the .o files around. This option passes the -s option to the assembler and the linker.
No Auto-Read—This is the older way of loading symbol tables.
The compiler instructs the linker to place all symbol tables for dbx in the
executable file.
The linker links more slowly and dbx initializes more slowly.
If you move the executables to another directory, then to use dbx you
must move the source files, but you need not move the object (.o) files.
Auto-ReadThis is the newer (and default) way of loading symbol tables.
The compiler distributes this information in the .o files so that dbx loads
the symbol table information only if and when it is needed.
The linker links faster and dbx initializes faster.
If you move the executables to another directory, then to use dbx, you
must move both the source files and the object (.o) files.
-xsafe=mem
(Solaris 2.x only) The -xsafe=mem option allows the compiler to assume no memory-based traps occur.
This option grants permission to use the speculative load instruction on V9 machines.
-xsb
Same as -sb.
-xsbfast
Same as -sbfast.
The Pascal Compiler 61
3
-xspace
(Solaris 2.x only) The -xspace option does no optimizations that increase the code size.
Example: Do not unroll loops.
-xtarget=t
(Solaris 2.x only) The -xtarget=t option specifies the target system for the instruction set and optimization.
t must be one of: native, generic, system-name. The -xtarget option permits a quick and easy specification of the -xarch,
-xchip, and -xcache combinations that occur on real systems. The only meaning of -xtarget is in its expansion.
Table 3-8 The -xtarget Values
Value Meaning
native Get the best performance on the host system.
The compiler generates code for the best performance on the host system. It determines the available architecture, chip, and cache properties of the machine on which the compiler is running.
generic Get the best performance for generic architecture, chip, and cache.
The compiler expands -xtarget=generic to:
This is the default value.
system-name Get the best performance for the specified system.
You select a system name from Table 3-9 that lists the mnemonic encodings of the actual system names and numbers.
The performance of some programs may benefit by providing the compiler with an accurate description of the target computer hardware. When program performance is critical, the proper specification of the target hardware could be
62 Pascal 4.0 User’s Guide
-xarch=generic -xchip=generic -xcache=generic
3
very important. This is especially true when running on the newer SPARC processors. However, for most programs and older SPARC processors, the performance gain is negligible and a generic specification is sufficient.
Each specific value for -xtarget expands into a specific set of values for the ­xarch, -xchip, and -xcache options. See Table 3-9 for the values. For example:
-xtarget=sun4/15 is equivalent to:
-xarch=v8a -xchip=micro -xcache=2/16/1
Table 3-9 -xtarget Expansions
-xtarget -xarch -xchip -xcache sun4/15 v8a micro 2/16/1 sun4/20 v7 old 64/16/1 sun4/25 v7 old 64/32/1 sun4/30 v8a micro 2/16/1 sun4/40 v7 old 64/16/1 sun4/50 v7 old 64/32/1 sun4/60 v7 old 64/16/1 sun4/65 v7 old 64/16/1 sun4/75 v7 old 64/32/1 sun4/110 v7 old 2/16/1 sun4/150 v7 old 2/16/1 sun4/260 v7 old 128/16/1 sun4/280 v7 old 128/16/1 sun4/330 v7 old 128/16/1 sun4/370 v7 old 128/16/1 sun4/390 v7 old 128/16/1 sun4/470 v7 old 128/32/1 sun4/490 v7 old 128/32/1 sun4/630 v7 old 64/32/1
The Pascal Compiler 63
3
-xtarget -xarch -xchip -xcache sun4/670 v7 old 64/32/1 sun4/690 v7 old 64/32/1 sselc v7 old 64/32/1 ssipc v7 old 64/16/1 ssipx v7 old 64/32/1 sslc v8a micro 2/16/1 sslt v7 old 64/32/1 sslx v8a micro 2/16/1 sslx2 v8a micro2 8/64/1 ssslc v7 old 64/16/1 ss1 v7 old 64/16/1 ss1plus v7 old 64/16/1 ss2 v7 old 64/32/1 ss2p v7 powerup 64/31/1 ss4 v8a micro2 8/64/1 ss5 v8a micro2 8/64/1 ssvyger v8a micro2 8/64/1 ss10 v8 super 16/32/4 ss10/hs11 v8 hyper 256/64/1 ss10/hs12 v8 hyper 256/64/1 ss10/hs14 v8 hyper 256/64/1 ss10/20 v8 super 16/32/4 ss10/hs21 v8 hyper 256/64/1 ss10/hs22 v8 hyper 256/64/1 ss10/30 v8 super 16/32/4 ss10/40 v8 super 16/32/4 ss10/41 v8 super 16/32/4:1024/32/1 ss10/50 v8 super 16/32/4
64 Pascal 4.0 User’s Guide
-xtarget -xarch -xchip -xcache ss10/51 v8 super 16/32/4:1024/32/1 ss10/61 v8 super 16/32/4:1024/32/1 ss10/71 v8 super2 16/32/4:1024/32/1 ss10/402 v8 super 16/32/4 ss10/412 v8 super 16/32/4:1024/32/1 ss10/512 v8 super 16/32/4:1024/32/1 ss10/514 v8 super 16/32/4:1024/32/1 ss10/612 v8 super 16/32/4:1024/32/1 ss10/712 v8 super2 16/32/4:1024/32/1 ss20/hs11 v8 hyper 256/64/1 ss20/hs12 v8 hyper 256/64/1 ss20/hs14 v8 hyper 256/64/1 ss20/hs21 v8 hyper 256/64/1 ss20/hs22 v8 hyper 256/64/1
3
ss20/51 v8 super 16/32/4:1024/32/1 ss20/61 v8 super 16/32/4:1024/32/1 ss20/71 v8 super2 16/32/4:1024/32/1 ss20/91 v8 super2 16/32/4:1024/32/1 ss20/502 v8 super 16/32/4 ss10/512 v8 super 16/32/4:1024/32/1 ss20/514 v8 super 16/32/4:1024/32/1 ss20/612 v8 super 16/32/4:1024/32/1 ss20/712 v8 super 16/32/4:1024/32/1 ss20/912 v8 super 16/32/4:1024/32/1 ss600/41 v8 super 16/32/4:1024/32/1 ss600/51 v8 super 16/32/4:1024/32/1 ss600/61 v8 super 16/32/4:1024/32/1 ss600/120 v7 old 64/32/1
The Pascal Compiler 65
3
-xtarget -xarch -xchip -xcache ss600/140 v7 old 64/32/1 ss600/412 v8 super 16/32/4:1024/32/1 ss600/ v8 super 16/32/4:1024/32/1 ss600/ v8 super 16/32/4:1024/32/1 ss600/ v8 super 16/32/4:1024/32/1 ss1000 v8 super 16/32/4:1024/32/1 sc2000 v8 super 16/32/4:1024/64/1 cs6400 v8 super 16/32/4:2048/64/1 solb5 v7 old 128/32/1 solb6 v8 super 16/32/4:1024/32/1 ultra v8 ultra 16/32/1:512/64/1 ultra1/140 v8 ultra 16/32/1:512/64/1 ultra1/170 v8 ultra 16/32/1:512/64/1 ultra1/1170 v8 ultra 16/32/1:512/64/1 ultra1/2170 v8 ultra 16/32/1:512/64/1 ultra1/2200 v8 ultra 16/32/1:1024/64/1
–Z
The –Z option instructs pc to insert code that initializes all local variables to zero. Standard Pascal does not allow initialization of variables.
-ztext
(Solaris 2.x only) The -ztext option forces a fatal error if relocations remain against non-writable, allocatable sections.
66 Pascal 4.0 User’s Guide
Program Construction and
Units
Management
This chapter is an introduction to the methods generally used to construct and manage programs using Pascal. It describes units and libraries in two separate sections:
Units page 67 Libraries page 74
For many reasons, it is often inconvenient to store a program in a single file, as in the case of a very large program.
You can break up a program in several ways. Perhaps the simplest way is to use an include file. An include file is a separate file that is copied in by the compiler when it encounters an include compiler directive. For example, in the following program:
program include (output); #include "includefile"
4
the line #include "includefile" is a compiler directive to cpp(1), the Pascal compiler ’s preprocessor. The directive instructs cpp(1) to find the file includefile and copy it into the stream before continuing.
67
4
The actual includefile looks like this:
begin
writeln ('Hello, world.')
end.
In this example, the include file contains the entire program. In reality, an include file probably contains a set of variable or procedure declarations. include files are often used when a set of declarations needs to be shared
among a number of programs. However, suppose your program is very large and takes a long time to
compile. Using include files may make editing more convenient, but when you make a change in one part of your program, you still must recompile the entire program. As another example, suppose you want to be able to share compiled code with other people, but for reasons of security or convenience, do not want to share the source code.
Both of these problems are solved by separately compiled units, generally called units. A unit is a part of a program stored in its own file and linked with the rest of the program after compilation.
Using Program Units and Module Units
There are two kinds of units:
Program unit—This unit looks like any program. It begins with a program
header and contains the main program. Here is an example:
program program_unit (output); procedure say_hello; extern;
begin
say_hello
end.
68 Pascal 4.0 User’s Guide
4
The body of the procedure say_hello is not defined in this program unit, but the program unit does contain a declaration of the interface to the procedure. The keyword extern declares that say_hello is declared in a module unit.
Module unit—This unit can begin with an optional module header,
followed by a set of compilable declarations and definitions. Modules that call externally defined routines must have declarations for those routines.
Here is an example:
module module_unit; procedure say_hello; begin
writeln ('Hello, world.')
end;
Every program must have one and only one program unit; a program can have any number of module units. Any unit can call procedures declared in any other unit; each unit must have external declarations for every procedure it uses that is not defined in that unit.
1
A module unit can also be used as a library, that is, as a collection of useful routines that is shared among a number of programs.
Compiling with Units
Consider the units given in the previous section, “Using Program Units and Module Units.” You can compile and link these units on a single line by executing the following command, which then produces the executable,
a.out.
hostname% pc program_unit.p module_unit.p
1. A statement that shows the interface of a routine is called a declaration, because it declares the name and parameters of the routine. The set of statements that shows the entire routine, including the body, is called the definition of the routine. There can be only one definition for a given routine, but every routine must be declared in every module or program unit that uses it.
Program Construction and Management 69
4
You can also separate the compilation and linking or loading steps, as follows:
hostname% pc program_unit.p -c hostname% pc module_unit.p -c hostname% pc program_unit.o module_unit.o
In this case, you call pc on each unit with the “compile only” option (-c), which produces an object file with the extension .o. When you use this option, the compiler driver does not call the linker, ld. You then call pc a second time, giving the names of the object files, and pc calls pc3 to check for name and type conflicts before calling the linker.
Calling the linker or loader ld(1) directly does not have the same effect as calling pc; when you call ld(1) directly, the files are linked and loaded, but they are not checked for conflicts.
Using Units and Header Files
A complex program may have many routines defined in modules. Each routine must have a declaration (for example, procedure proc; extern;) in each file that calls the routine. The easiest way to be sure that you have a correct and consistent set of declarations is to create a header file.
A header file is a file that contains a set of declarations, nothing else. You use a header file by using an include directive to include the header file in the compilation.
For example, here is a modified version of the program, program_unit, that uses a header file:
program program_unit2 (output); include "header.h"
begin
say_hello
end.
In this case, the content of header.h is very simple:
procedure say_hello; extern;
70 Pascal 4.0 User’s Guide
In a real program, header.h would probably contain many declarations and would be included in several modules. Aside from routine declarations, header files often contain constant, type, and variable declarations.
Sharing V ariables Between Units
Variables that are global across a unit (that is, not declared locally in a routine) can be public or private. A public variable can be shared by any unit that is linked to the unit that declares the variable. A private variable cannot be shared.
You can use the public and private reserved words to declare that a var section declares public or private variables. For example:
program program_unit3 (output); public var
x : integer;
private var
y : integer;
4
When you do not use public or private, variables are public by default. However, when you compile with the -xl option, variables are private by default.
To share a public variable, simply declare it in each unit where you want to share it. As long as the variable is public, each reference to that variable accesses the same data.
Program Construction and Management 71
4
Here is a program unit that declares a variable:
program program_unit3 (output);
var
x : integer;
procedure say_hello; external;
begin
for x := 1 to 5 do say_hello
end.
Here is a module unit that declares a variable with the same name:
module module_unit3; var
x : integer;
procedure say_hello;
begin
writeln ('Hello, world for the', x, ' time.')
end;
72 Pascal 4.0 User’s Guide
4
By default, both definitions of variable x are public. Thus, when you compile and link the program and module units, references to x refer to the same variable, as follows:
hostname% pc program_unit3.p module_unit3.p program_unit.p: module_unit.p: Linking: hostname% a.out Hello, world for the 1 time. Hello, world for the 2 time. Hello, world for the 3 time. Hello, world for the 4 time.
Hello, world for the 5 time.
If you compile the program giving the -xl option, the variables are private by default, as follows:
hostname% pc -xl program_unit.p module_unit.p program_unit.p: module_unit.p: Linking: hostname% a.out Hello, world for the 0 time. Hello, world for the 0 time. Hello, world for the 0 time. Hello, world for the 0 time.
Hello, world for the 0 time.
You can get the same effect by explicitly declaring the variable in a private var section. Similarly, when you use -xl, you can create public variables by
declaring them in a public var section. As with routine declarations, it is often a good idea to declare public
variables in an include file. Doing so makes it easier to keep your declarations consistent.
There are other methods for making variables visible to different units. See Chapter 5, “Separate Compilation,” for more information.
Program Construction and Management 73
4
Libraries
You can use a module unit as a library of useful functions. The simplest way to do so is to create a source file containing the definitions of your library routines and then compile it using the -c option. You can then link the resulting .o file to any number of files. For convenience, you probably should create a header file containing the routine declarations for the library.
A simple library as described above has two problems:
When a library grows in size, it may become inconvenient to store its source
in a single file, both for ease of editing and so you can avoid recompiling a large file when you change only part of it.
On the other hand, it would be inconvenient to have to name many library modules on the command-line when you link your program. Thus, it would be helpful to be able to combine a number of library modules.
Several programs that you run at the same time may share the same library.
Under the scheme described above, each program has its own copy of the library. It saves space and even I/O time if several programs share library code.
Both problems have solutions. First, you can combine or archive modules together. Secondly, you can create a shared library.
See the Solaris documentation on the linker and libraries for information on creating archived and shared libraries.
74 Pascal 4.0 User’s Guide
Separate Compilation
This chapter describes how to compile Pascal programs in separate units. Chapter 4, “Program Construction and Management,” gives an introduction to the concepts in this chapter. Following are the sections:
Working with Units page 75 Sharing Variables and Routines Across Multiple Units page 76 Sharing Declarations in Multiple Units page 87
In separate compilation, a program is divided into several units that can be separately compiled into object (.o) files. The object files are then linked using pc, which invokes pc3 to check for the consistent use of global names and declarations across the different units, and then invokes ld(1) to link and load the units. You can also give pc the names of all the units at once, in which case pc compiles all the units, checks for consistency, and links the units in one step.
Separate compilation is different from independent compilation. In independent compilation, you invoke ld directly, so there is no consistency checking. Independent compilation is not addressed in this guide.
5
Working with Units
Pascal provides two types of source files or units: the program unit and the module unit.
75
5
Using Program Units
The program unit is the source program with the program header. It has the following syntax:
<program unit> ::= <program heading> <declaration list> <program body>
Each program you write can have only one program unit. The program body is the first code that Pascal executes.
Using Module Units
A module unit is a source program that does not have a program header. It has the following syntax:
<module unit> ::= [ <module heading>]<declaration list>
The module heading contains the reserved word module followed by an identifier:
<module heading> ::= [ 'module' <identifier> ';' ]
For example:
module sum;
This is a legal module heading. The module heading is optional.
Sharing V ariables and Routines Across Multiple Units
Pascal supports three methods of sharing variables and routines between units:
include files
Multiple variable declarations
extern/define variable declarations
These methods are not mutually exclusive; for example, you can declare a variable as either extern or define in an include file.
The following sections describe these methods.
76 Pascal 4.0 User’s Guide
Loading...