This chapter is intended to be a QuickStart guide for programmers who are familiar with Borland Pascal, version 7 for DOS protected mode. Other versions don't differ too much but this one is the very last DOS version Borland has published.
Unlike other free compilers, GNU Pascal is not intended to be 100% Borland compatible. GNU Pascal is part of the GNU project, so portability between different machines is one of its primary goals. For this reason, non-portable features of Borland Pascal will probably not be included into GNU Pascal. Some other differences are so minimal that we have more important things to do than to implement them.
However if you want to contribute to GNU Pascal by eliminating holes in GPC's Borland compatibility, be welcome in the GNU Pascal development team! See section How you can contribute to GNU Pascal for details.
On the DOS platform with DJGPP, you can use the RHIDE for GNU Pascal; check the subdirectories of your DJGPP distribution.
Unfortunately, there is no IDE which would run on all platforms. We are working on it, but this will take some time. Please be patient--or offer your help!
Without an IDE, the GNU Pascal Compiler, GPC, is called about like the command-line version of the Borland Pascal Compiler, BPC. Edit your source file(s) with your favorite ASCII editor, then call GNU Pascal with a command line like
C:\GNU-PAS> gpc hello.pas -o hello.exe
on your DOS or OS/2 box (with EMX you may omit the `-o ...') or
myhost/home/joe/gnu-pascal> gpc hello.pas -o hello
on your UNIX (or UNIX-compatible) system. Don't omit the suffix `.pas': GPC is a common interface for a Pascal compiler, a C, ObjC and C++ compiler, an assembler, a linker, and perhaps a Modula and FORTRAN compiler. From the extension of your source file GPC figures out which processor to run.
The -o
is a command line option which tells GPC how the
executable has to be named. This is not necessary for DOS and
OS/2 with EMX; it is necessary for DOS with DJGPP.
Note that GPC is case-sensitive concerning file names, so it will not work if you type
C:\GNU-PAS> GPC HELLO.PAS -O HELLO.EXE
GPC is a very quiet compiler and doesn't print anything on the screen unless you request it or there is an error. If you want to see what is going on, invoke GPC with additional options:
-Q "don't be quiet" (or: Quassel-Modus in German)
(with capital `Q'!) means that GPC prints out the names of procedures and functions it processes, and
--verbose
means that GPC informs you about the stages of compilation, i.e. preprocessing, compiling, assembling, and linking.
One example (this time for OS/2):
[C:\GNU-Pascal] gpc --verbose -Q hello.pas
Throughout this chapter, we will tell you about a lot of command-line switches. They are all invoked this way.
After compilation, there will be an executable hello
file in
the current directory. (hello.exe
with DOS and OS/2.) Just
run it and enjoy. If there are errors, GNU Pascal will not stop
compilation after the first one--as Borland Pascal does--but
try to catch them all in one compilation. If you get more error
messages than your screen can hold, you can catch them in a file
(e.g. gpc.out
) in the following way:
gpc hello.pas 2>gpc.out
This works with OS/2 and any bash-like shell under UNIX; for DOS
you must get a replacement for command.com
which supports
this kind of redirection (see also the DJGPP FAQ).
You can also use Borland's IDE for GNU Pascal on the DOS platform: Install the GNU Pascal Compiler in the Tools menu (via Options/Tools).
Name: GNU Pascal Path: gpc Arguments: $SAVE ALL --verbose -Q $NAME($EDNAME).pas HotKey: Shift+F9
Note once more that GPC is case-sensitive, so it is important to
specify .pas
instead of the .PAS
Borland Pascal would
append otherwise!
You can include more command-line arguments to GNU Pascal (e.g. `--automake'; see below) as you will learn more about them.
Since Borland Pascal will try to recompile your program if you
use its run
menu function, you will need another "tool"
to run your program:
Name: run program Path: command.com Arguments: /c $NAME($EDNAME) HotKey: Shift+F10
According to ISO 7185 and ISO 10206 standard, GNU Pascal can recognize
comments opened with (*
and closed with }
. With Borland
Pascal, both types of comments can be nested, so you will probably have
sources where passages containing comments are "commented out". GPC's
default behavior is to allow nested comments, so you don't need to worry
about this. However you can switch it off either by a command-line
option, or by a compiler directive:
--no-nested-comments {$N-} (*$N-*)
The $N directive also exists in BP but has another meaning. The same holds for most of GPC's other compiler directives (also corresponding to command-line options in most cases):
--short-circuit $B+ $B- like in Borland Pascal: $B- means short-circuit Boolean operators; $B+ complete evaluation --c-numbers $C+ $C- enable/disable C-style octal 0177 and hexadecial 0xFF numbers --char-escapes $E+ $E- enable/disable C-style character escape sequences in strings --nested-comments $N+ $N- see above --pedantic $P+ $P- give/don't give portability warnings $W+ $W- enable/disable warnings $X+ $X- enable/disable extended syntax (function return value ignore, operator definitions) --borland-pascal disable or warn about GPC features not supported by Borland Pascal; do not warn about "dangerous" BP features {$I FileName } include filename.pas or filename.p (make it lowercase) {$include "filename.pas"} include (case-sensitive) {$include <filename.pas>} the same, but don't search in current directory {$M Hello!} write message "Hello!" to error device during compilation {$D GNU} define GNU (for conditional {$define GNU} compilation) -D GNU the same in command line {$D loop while true do} define "loop" to be "while true do" as a macro like in C. It is case-sensitive. {$ifdef GNU} conditional compilation ... (like in Borland Pascal). {$else} GPC predefines the symbol ... __GPC__ (with two leading {$endif} and trailing underscores).
You also can use C-style preprocessor directives, e.g. `#include.'
As in Borland Pascal, {$...}
and (*$...*)
are equivalent.
You can use Units in the same way as in Borland Pascal. However, there are some differences, problems and new features. (Please report the bug if something doesn't work.)
Concerning the syntax of a Unit, you can, if you want, use Extended Pascal syntax to specify a Unit initializer, i.e. instead of writing
begin ... end.
at the end of the Unit, you can get the same result with
to begin do begin ... end (* to begin *);
and there also exists
to end do begin ... end (* to end *);
which specifies a finalization routine. Use this instead of Borland Pascal's exit procedures. You also can specify an order in which initializers are run--see section GNU Pascal extensions and section About Pascal and Extended Pascal languages for more about this. There you can also find information about Extended Pascal Modules, an alternative to Units.
At the moment, there are no qualified identifiers, so take care about name clashes between different Units.
When GPC compiles a Unit, it produces two files: an .o
object
file (compatible with other GNU compilers such as GNU C) plus a
precompiled Interface which resides in a .gpi
file.
(See section GPI files--GNU Pascal Interfaces for GPI file internals.)
GPC does not automatically recognize that something is a Unit and cannot be linked; you have to tell this by a command line switch:
-c only compile, don't link.
For example, to compile two units, use:
gpc -c myunit1.pas myunit2.pas
Of course, one of the purposes of writing Units is to compile
them separately. However, GNU Pascal allows you to have one or
more Units in the same source file (producing only one .o
file
but separate .gpi
files). You even can have a Program and Units
in one and the same source file; in this case, no .o
file is
produced at all.
You can use the above as a workaround (*$include the Unit *) in case something goes wrong with the .gpi mechanism. (It is a new feature in GPC 2.0 and may be unstable.)
When you have all Units compiled and want to compile the
Program, specify the .o
files in the command line:
gpc hallo.pas myunit1.o myunit2.o
You also can specify the program and the units in one command line:
gpc hallo.pas myunit1.pas myunit2.pas
As an alternative to manually compiling and specifying object files, you can use GPC's AutoMake feature. (Warning: This is a new feature in GPC 2.0 and may be unstable!) With an additional command-line argument
gpc --automake hallo.pas
GPC tries to behave like Borland Pascal's make
facility and
automatically recompiles Units the source of which has been
changed. It also works if an included file has been changed.
To pass arguments to the compilation of Units, specify them in a
string surrounded by quotation marks after --automake=
. For
example, if you want to give the --verbose
argument not only at
top level but also for (re)compilation of Units, use:
gpc --verbose --automake="--verbose" hallo.pas
For more information about the AutoMake mechanism, see section GPC's AutoMake Mechanism--How it Works.
GNU Pascal is a 32 bit compiler with excellent optimization algorithms (which are identically the same as those of GNU C). There are three optimization levels, specified by the command line options `-O', `-O2', and `-O3'. (Levels up to `-O6' are worked on.)
One example:
Program Test; Var A, B: Integer; begin A:= 3; B:= 4; writeln ( A + B ); end.
When GNU Pascal compiles this program with optimization, it
recognizes that the argument of writeln is the constant 7--and
optimizes away the variables A
and B
.
For more about optimization, see the GNU C documentation.
The command line option `-g' specifies generation of debugging
information for GDB, the GNU debugger. GDB is not as comfortable as the
Turbo Debugger, but it has the same abilities but one: The program being
debugged is slowed down. For more information about GDB, call it (with
`gdb') and type in the help
command.
GDB has one very nice feature which might even make up for the
slowing-down problematic: When a GNU-compiled program crashes,
it puts out a core
file. When you say "target core core" to
the GNU debugger, it reads the core file and can tell you the
address of the crash, the contents of variables and of the
stack, and so on.
Sometimes it is nice to have a look at the assembler output of
the compiler. When you specify the -S
command line option, GPC
produces an .s
file instead of an .o
file which contains
assembler source for your program. More about this in the next
section.
See also "Notes for debugging" in the "Bugs" chapter; see section Known bugs and inconveniences; how to report bugs.
GNU Pascal has an inline assembler, but it is quite different from Borland's one. I have not yet found reasonable documentation about the use of this assembler, but I found out the following:
The syntax is
asm ( 'movl $12345678, %eax' ); asm ( 'movl %eax, %edx' );
to move the (hex) value `$12345678' to the EAX register and then to the EDX register. The String argument of the asm "procedure" is passed as a string to the assembler stage of the compilation. The assembler follows the AT&T, not the Intel syntax. Note that the order of operands is reversed with respect to the order you know from Intel assemblers and that the size of the arguments is appended to the mnemonic as a suffix b (byte), w (word = 2 bytes), or l (long = 4 bytes).
You can learn about the GNU assembler syntax when compiling your program with `-S' (see above) and looking into the resulting assembler source.
To access Pascal symbols from the assembler, do something like
asm ( 'movl %%eax, %0' : : 'rm' ( MyVariable ) );
Here, the % signs in front of the register names are doubled because % gets some special meaning. The 'rm' means that the operand may be in a register or in memory.
(More about this is in preparation.)
Objects in the Borland Pascal 7.0 notation are implemented into GNU Pascal with the following differences:
MyObj = object x: Integer; Procedure Foo; virtual; y: Real; Function Bar: Char; end (* MyObj *);
Strings are "Schema types" in GNU Pascal which is something more
complicated than Borland-style strings. For variables, you
cannot specify just String
as a type (always specify the
maximum length); for parameters you can. There is no 255 length
limit. According to Extended Pascal, the maximum string length
must be in (parantheses); GNU Pascal accepts [brackets],
however.
For more about Strings and Schema types see section GNU Pascal extensions and section About Pascal and Extended Pascal languages.
String-handling functions (see section GNU Pascal extensions) are different in both dialects but can approximately be mapped on each other:
Borland Pascal GNU Pascal length length Pos Index Str WriteStr Val ReadStr Copy SubStr, MyStr [ 2..7 ] MyStr [ 0 ]:= #7; Trim - EQ, NE, LT, LE, GT, GE Insert - Delete -
It should not be difficult to write a compatibility Unit--if you do so, please send it to us!
GNU Pascal supports Borland Pascal's "typed constants" but also Extended Pascal's initialized variables:
Var x: Integer value 7;
When a typed constant is misused as an initialized variable, a warning is given unless you specify `--borland-pascal'.
When you initialize a record, you may omit the field names. When you
initialize an array, you may provied indices with a :
. However
this additional information is ignored completely, so perhaps it's best
for the moment only to provide the values ...
When you want a local variable to preserve its value, define it as `__static__' instead of using a typed constant:
Procedure Foo; Var x: __static__ Real; begin (* Foo *) (* x keeps its value between two calls to this Procedure *) end (* Foo *);
Remark: If you don't like underscores, you can use the (*$define *) mechanism to get rid of them:
(*$define static __static__ *)
The (non-standard) bitwise operators `shl', `shr', `and', `or', `xor' work in GNU Pascal like in Borland Pascal. As an extension, you can use them as "procedures", for examples
and ( x, $0000FFFF );
as an alternative to
x:= x and $0000FFFF;
Instead of the Borland-specific notation `$ABCD' for hexadecimal numbers you also can use Extended Pascal notation:
2#11111111 for a binary, 8#177 for an octal, 16#FF for a hexadecimal number,
and so on up to a basis of 36.
`Inc' and `dec' are implemented like in Borland Pascal; pred and succ are generalized according to Extended Pascal:
a:= succ ( a, 5 );
`Absolute' variables work only in the context of overloading with other variables, not in the context of specifying an absolute address. The `Mem' and `Port' arrays don't exist in GNU Pascal.
Borland Pascal's procedures FillChar
and move
are not
built-in into GNU Pascal. You can write them by yourself using untyped
`Var' parameters (see section Data types in BP and GPC).
GNU Pascal allows the user to define operators according to the Pascal-SC syntax:
Type Point = record x, y: Real; end (* Point *); Operator + ( a, b: Point ) c: Point; begin (* Point + Point *) c.x:= a.x + b.x; c.y:= a.y + b.y; end (* Point + Point *);
The Pascal-SC operators `+>', `+<', etc. for exact numerical calculations are not implemented, but you can define them. If you write a module which really implements these operators, please send it to us, so we can include it into the next distribution.
(And if you know more about modules in Pascal-SC than just their existence, please contact us either! We could probably easily implement them if we knew how they look like. Something quite close to Pascal-SC modules already is implemented as "GNU specific modules".)
Borland Pascal GNU Pascal bits signed ShortInt ByteInt 8 yes Integer ShortInt 16 yes LongInt Integer 32 yes Comp LongInt, Comp 64 yes Byte Byte 8 no Word ShortWord 16 no - Word 32 no - LongWord 64 no
Borland Pascal GNU Pascal Single ShortReal Real - Double Real Extended LongReal Comp LongInt
writeln
variables of a
"modified" type, i.e. other than plain `Integer' or `Real'.
To work around, cast the other types to their basis type (`Integer'
or `Real') when writeln
ing them:
Var x: Extended; (* Or (perhaps better:-) LongReal *) ... writeln ( Real ( x ) );
Type myRec = record f, o, oo: Boolean; Bar: Integer; end (* myRec *);has 8 bytes, not 7. Use the
--pack-struct
option to force GPC
to pack it to 7 bytes; see section Options for Code Generation Conventions.
Type FuncPtr = ^Function ( Real ): Real;Furthermore, GNU Pascal supports Standard Pascal's procedural parameters--see below.
Assign
procedure for files, but you can
write it by yourself using the "Bind" mechanism of Extended
Pascal:
Procedure Assign ( Var T: Text; Name: String ); Var B: BindingType; begin (* Assign *) unbind ( T ); B:= binding ( T ); B.Name:= Name; bind ( T, B ); B:= binding ( T ); end (* Assign *);
pow
and **
which do not
exist in Borland Pascal. You can use x pow y
for integer
and x ** y
for real or complex exponents; the basis may be
integer, real or complex in both cases.
@
, but also
&
as an address operator.
set1 >< set2
.
For more about this, see section GNU Pascal extensions.
p:= GetMem ( 1024 );The second parameter to `FreeMem' is ignored by GNU Pascal and may be omitted. Memory blocks are always freed with the same size they were allocated with. Remark: Extended Pascal Schema types will provide a cleaner approach to most of the applications of `GetMem' and `FreeMem' in the future.
Procedure Foo ( Var x );like in Borland Pascal. In GNU Pascal, you can also use
Procedure Foo ( Var x: Void );
Procedure Foo ( a: Integer; ... );but does not (yet) provide a portable mechanism to access the additional arguments.
Procedure Foo ( a: array of Integer );are implemented. However, Standard Pascal conformant array parameters (only partially implemented) are a "cleaner" mechanism to pass arrays of variable size.
Procedure DrawGraph ( Function f ( x: Real ): Real );
Program Foo ( Input, Output );In GNU Pascal, headline parameters are optional. If the headline is omitted, a warning is given unless you have specified `--borland-pascal' in the command line.
otherwise
as an alternative to else
(according to Extended Pascal):
case x of 1: writeln ( 'one' ); 2: writeln ( 'two' ); otherwise: writeln ( 'many' ); end (* case *);If there are two ore more statements following
otherwise
,
they must be grouped with begin
and end
.
card ( myset )
function for sets
which counts their elements.
Function Max ( x, y: Integer ): Integer; Inline ( $58 / $59 / $3B / $C1 / $7F / $01 / $91 );GNU Pascal:
Inline Function Max ( x, y: Integer ): Integer; begin (* Max *) if x > y then Max:= x else Max:= y; end (* Max *);(Actually, a more general Max is already built-in.) This feature is not so important as it might seem because in optimization level 3 (see above), GNU Pascal automatically "inlines" short Procedures and Functions.