Comparing HP C and HP Pascal [ HP C Programmer's Guide ] MPE/iX 5.0 Documentation
HP C Programmer's Guide
Comparing HP C and HP Pascal
Table 3-1 summarizes the differences in storage allocation between HP
C and HP Pascal. The footnote numbers refer to notes located in a
section immediately following the table.
Table 3-1. HP C versus HP Pascal Storage Allocation
-------------------------------------------------------------------------------------------------
| | | | |
| HP C Type | HP C Description | Corresponding HP | HP Pascal Description |
| | | Pascal Type | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| char, signed char | 1 byte, byte aligned | -- | 1 byte, byte aligned; |
| | | | Subrange: |
| | | | -128 .. 127 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| unsigned char | 1 byte, byte aligned | char | 1 byte, byte aligned; |
| | | | Subrange: 0 .. 255 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| short | 2 bytes, 2-byte | shortint | Subrange: |
| | aligned | | -32768..32767 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| unsigned short | 2 bytes, 2-byte | -- | Subrange: |
| | aligned | | 0 .. 65535 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| int | 4 bytes, 4-byte | integer | 4 bytes, 4-byte |
| | aligned | | aligned; Subrange: |
| | | | -2147483648 .. |
| | | | 2147483647 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| unsigned int | 4 bytes, 4-byte | -- | 4 bytes, 4-byte |
| | aligned | | aligned; Subrange: 0 |
| | | | .. 4294967295 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| long | 4 bytes, 4-byte | integer | Subrange: |
| | aligned | | -2147483648 .. |
| | | | 2147483647 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| unsigned long | 4 bytes, 4-byte | -- | 4 bytes, 4-byte |
| | aligned | | aligned; Subrange: 0 |
| | | | .. 4294967295 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| (See Note 1) | -- | longint | 8 bytes, 4-byte |
| | | | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| float | 4 bytes, 4-byte | real | 4 bytes, 4-byte |
| | aligned | | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
Table 3-1. HP C versus HP Pascal Storage Allocation (cont.)
-------------------------------------------------------------------------------------------------
| | | | |
| HP C Type | HP C Description | Corresponding HP | HP Pascal Description |
| | | Pascal Type | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| double | 8 bytes, 8-byte | longreal | 8 bytes, 8-byte |
| | aligned | | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| long double | 16 bytes, 16-byte | -- | -- |
| | aligned | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| enum | 4 bytes, 4-byte | enumeration or | 1 byte if fewer than |
| | aligned | integer (See Note 2) | 257 elements; 2 bytes |
| | | | if between 257 and |
| | | | 65536; otherwise, 4 |
| | | | bytes. 1, 2, or |
| | | | 4-byte aligned. |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| char enum | 1 byte, 1-byte | -- | 1 byte, 1-byte |
| | aligned | | aligned, subrange: |
| | | | -128..127 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| short enum | 2 bytes, 2-byte | short int | subrange: |
| | aligned | | -32768..32767 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| int enum | 4 bytes, 4-byte | integer | 4 bytes, 4-byte |
| | aligned | | aligned, subrange: |
| | | | -2,147,483,648.. |
| | | | 2,147,483,647 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| long enum | 4 bytes, 4-byte | integer | 4 bytes, 4-byte |
| | aligned | | aligned, subrange: |
| | | | -2,147,483,648.. |
| | | | 2,147,483,647 |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| array [n] of type | Size is number of | ARRAY [0 .. n-1] OF | Size is the number of |
| | elements times | type (See Note 3) | elements times |
| | element size. Align | | element size. Align |
| | according to element | | according to element |
| | type. | | type. |
| | | | |
-------------------------------------------------------------------------------------------------
Table 3-1. HP C versus HP Pascal Storage Allocation (cont.)
-------------------------------------------------------------------------------------------------
| | | | |
| HP C Type | HP C Description | Corresponding HP | HP Pascal Description |
| | | Pascal Type | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| array [n] of char | [n] bytes, byte | PACKED ARRAY [0 .. | [n] bytes, byte |
| | aligned | n-1] OF CHAR or not | aligned |
| | | PACKED (See Note 4) | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| struct (See Note 5) | Pascal string | STRING [n] | Size 4+[n]+1 bytes, |
| | descriptors may be | | 4-byte aligned. |
| | emulated using C | | |
| | structures, see the | | |
| | note for an example. | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| Pointer to string | Pascal VAR parameters | STRING | -- |
| descriptor structure | may be emulated using | | |
| (See Note 6) | C pointers to string | | |
| | descriptor | | |
| | structures. (See | | |
| | Note 6). | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| char * | Pointer to a null | pointer to character | (See Note 7) |
| | terminated array of | array | |
| | characters | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| struct | Size of elements plus | record | (See Note 8) |
| | padding, aligned | | |
| | according to largest | | |
| | type | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| union | Size of elements plus | (untagged) variant | (See Note 8) |
| | padding, aligned | record (See Note 9) | |
| | according to largest | | |
| | type | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| signed bit-fields | -- | packed record (See | -- |
| | | Note 10) | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| unsigned bit-fields | -- | packed record (See | -- |
| | | Note 11) | |
| | | | |
-------------------------------------------------------------------------------------------------
Table 3-1. HP C versus HP Pascal Storage Allocation (cont.)
-------------------------------------------------------------------------------------------------
| | | | |
| HP C Type | HP C Description | Corresponding HP | HP Pascal Description |
| | | Pascal Type | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| void | -- | Used when calling an | -- |
| | | HP Pascal procedure | |
| | | (See Note 12) | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| pointer | 4 bytes, 4-byte | pointer to | 4 bytes, 4-byte |
| | aligned | corresponding type | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| long pointer | 8 bytes, 8-byte | $ExtnAddr$ pointer or | 8 bytes, 4-byte |
| | aligned | $ExtnAddr$ VAR | aligned |
| | | parameter | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| char | 1 byte, 1-byte | boolean (See Note 13) | 1 byte, 1 byte |
| | aligned | | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| void function | 4 bytes, 4-byte | PROCEDURE parameter | 4 bytes, 4-byte |
| parameter | aligned | | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| function parameter | 4 bytes, 4-byte | FUNCTION parameter | 4 bytes, 4-byte |
| | aligned | | aligned |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| struct of 1-bit | (See Note 14) | set | -- |
| fields | | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| -- | Pascal files may be | file | external record |
| | read by C programs | | oriented file |
| | with some effort. | | |
| | (See Note 15) | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| pointer to void | -- | procedure | -- |
| function | | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| pointer to function | -- | function | -- |
| | | | |
-------------------------------------------------------------------------------------------------
Notes on HP C and HP Pascal
1. The longint type in HP Pascal is a 64-bit signed integer. A
corresponding HP C type could be any structure or array of 2
words; however, HP C cannot directly operate on such an object.
2. By default, HP C enumerations are allocated 4 bytes of storage,
while HP Pascal enumerations use the following scheme:
1 byte, if fewer than 257 elements.
2 bytes, if between 257 and 65536 elements.
4 bytes, otherwise.
If the default enumeration specifier is modified with a char or
short type specifier, 1 or 2 bytes of storage are allocated. See
Table 3-1 for a description of the sized enumerated types.
This is important if the items are packed. For example, a
25-element enumeration in HP Pascal can use 1 byte and be on a
byte boundary, so you must use the HP C type char or a sized enum
declaration char enum.
3. HP C always indexes arrays from zero, while HP Pascal arrays can
have lower bounds of any user-defined scalar value. This is only
important when passing an array using an index to subscript the
array. When passing the subscript between HP C and HP Pascal, you
must adjust the subscript accordingly. HP C always passes a
pointer to the first element of an array. To pass an array by
value, enclose the array in a struct and pass the struct.
4. HP C char arrays are packed one character per byte, as are HP
Pascal arrays (even if PACKED is not used). HP Pascal permits
certain string operations with a packed array of char when the
lower bound is one.
5. The HP Pascal type STRING [n] uses a string descriptor that
consists of the following: a word containing the current length
of the string, n bytes for the characters, and an extra byte
allocated by the HP Pascal compiler. Thus, the HP Pascal type
STRING[10] corresponds to the following HP C structure:
typedef struct {
int cur_len; /* 4 bytes */
char chars [10]; /* 10 bytes */
char extra_byte; /* 1 byte */
} STRING_10;
which is initialized like this:
STRING_10 this_string = {
0, /* The current length */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* The 10 bytes */
0 /* The null byte */
};
Both the C structure and the Pascal string are 4-byte aligned.
6. HP Pascal also has a generic string type in which the maximum
length is unknown at compile time. Objects of this type have the
same structure as in Note 5 above; the objects are only used as
VAR formal parameters.
7. A variable of this type is a pointer to a character array if the
string is null-terminated; HP Pascal will not handle the null byte
in any special way. An HP C parameter of type "pointer to char"
corresponds to an HP Pascal VAR parameter of type "packed array of
char." However, the type definition of that VAR parameter must
have the bounds specified.
8. The size is equal to the size of all members plus any padding
needed for the alignment. (See Chapter 2 for details on
alignment.) The alignment is that of the member with the
strictest alignment requirement.
9. A union corresponds directly to an untagged HP Pascal variant
record. For example, the HP C union:
typedef union {
int i;
float r;
unsigned char c;
} UNIONTYPE;
corresponds to:
TYPE
UNIONTYPE = RECORD CASE INTEGER OF
1 : (i : INTEGER);
2 : (r : REAL);
3 : (c : CHAR);
END;
The tagged HP Pascal variant record:
TYPE
TAGGED_UNIONTYPE = RECORD CASE tag : INTEGER OF
1 : (i : INTEGER);
2 : (r : REAL);
END;
corresponds to this HP C structure:
typedef struct {
int tag;
union {
int i;
float r;
};
} TAGGED_UNIONTYPE;
10. HP Pascal subranges with a negative value as their lower bound
have enough bits allocated to contain the upper bound, with an
extra bit for the sign. Thus, the HP C structure:
typedef struct {
int b1 : 1;
int b2 : 2;
int b3 : 3;
int b4 : 4;
int b5 : 5;
int b6 : 6;
int b7 : 7;
} BITS;
corresponds to the following untagged HP Pascal record:
TYPE
BITS = PACKED RECORD
b1 : BOOLEAN;
b2 : -2 .. 1;
b3 : -4 .. 3;
b4 : -8 .. 7;
b5 : -16 .. 15;
b6 : -32 .. 31;
b7 : -64 .. 63;
END;
11. Unsigned bit-fields map onto HP Pascal packed record fields whose
types are the appropriate subranges. For example, the HP C
structure:
typedef struct {
unsigned int b1 : 1;
unsigned int b2 : 2;
unsigned int b3 : 3;
unsigned int b4 : 4;
unsigned int b5 : 5;
unsigned int b6 : 6;
unsigned int b7 : 7;
} BITS;
corresponds to this untagged HP Pascal record:
TYPE
BITS = PACKED RECORD
b1 : 0 .. 1;
b2 : 0 .. 3;
b3 : 0 .. 7;
b4 : 0 .. 15;
b5 : 0 .. 31;
b6 : 0 .. 63;
b7 : 0 .. 127;
END;
12. The type void, when applied to a function declaration, corresponds
to an HP Pascal procedure.
13. HP Pascal allocates one byte for Boolean variables, and only
accesses the rightmost bit to determine its value. HP Pascal uses
a 1 to represent true and zero for false; HP C interprets any
nonzero value as true and interprets zero as false.
14. HP Pascal sets are packed arrays of unsigned bits. For example,
given the HP Pascal set:
TYPE
SET_10 = SET OF 0 .. 9;
VAR s: SET_10;
the corresponding HP C struct would be:
typedef struct {
unsigned int b0 : 1;
unsigned int b1 : 1;
unsigned int b2 : 1;
unsigned int b3 : 1;
unsigned int b4 : 1;
unsigned int b5 : 1;
unsigned int b6 : 1;
unsigned int b7 : 1;
unsigned int b8 : 1;
unsigned int b9 : 1;
} SET_10;
SET_10 s;
Also, the following operation in HP Pascal:
s := s + [9];
has the following corresponding HP C code:
s.b9 = 1;
15. HP C and HP Pascal file types and I/O operations do not
correspond.
Passing Parameters Between HP C and HP Pascal
This section describes additional information on parameter passing.
1. All HP C parameters are passed by value except arrays and
functions, which are always passed as pointers. Reference
parameters to HP Pascal can be implemented in two ways: first, by
passing the address of an object by applying the address operator
& to the variable; second, by declaring a variable to be a pointer
to such a type, assigning an address to the pointer variable, and
passing the pointer.
If an HP Pascal procedure or function has a parameter that is an
array by value, it can be called from HP C by passing a struct
that contains an array of the corresponding type.
2. Be careful when passing strings to HP Pascal. If the routine
expects a packed array of char, be sure to pass a char array. If
the routine expects a user-defined string, pass the structure
declared in Note 5 above.
The examples below are HP Pascal and HP C source files that show
the parameter passing rules. The HP Pascal file contains 2
subroutines, pass_char_arrays() and pass_a_string(). The HP C
file contains the main line routine that calls these two
subroutines and displays the results. The HP C program is
annotated with the expected results.
The following is the HP Pascal procedure called from HP C:
$subprogram$
program p;
const len = 10;
type
pac_10 = packed array [1..10] of char;
string_10 = string [len];
function pass_char_arrays (a: pac_10;
var b: pac_10;
c: string_10;
var d: string_10) : integer;
var
i : integer;
ret_val : integer;
begin
ret_val := 0;
for i := 1 to len - 1 do
begin
if ( a[i] <> 'a' ) then
ret_val := 1;
a[i] := 'z';
if ( b[i] <> 'b' ) then
ret_val := 256;
b[i] := 'y';
end;
for i := 1 to strlen (c) do
begin
if ( c[i] <> 'c' ) then
ret_val := 65536;
c[i] := 'x';
end;
for i := 1 to strlen (d) do
begin
if ( d[i] <> 'd' ) then
ret_val := maxint;
d[i] := 'w';
end;
pass_char_arrays := ret_val;
end;
function pass_a_string (var a: string) : integer;
var
i : integer;
ret_val : integer;
begin
ret_val := 0;
for i := 1 to strlen (a) do
begin
if (a[i] <> 'x' ) then
ret_val := maxint;
a[i] := 'q';
end;
pass_a_string := ret_val;
end;
begin
end.
Figure 3-1. HP Pascal Procedure
The following HP C main program calls the HP Pascal procedure:
#include <stdio.h>
#include <string.h>
static struct string_10 {
int cur_len;
char chars[10];
};
/* a Pascal routine */
extern int pass_char_arrays (/* pac10,
var pac10,
string_10,
var string[10] */);
main(void)
{
static struct string_10 a, b, c, d;
int ret_val;
strcpy (a.chars, "aaaaaaaaa");
strcpy (b.chars, "bbbbbbbbb");
strcpy (c.chars, "ccccccccc");
c.cur_len = strlen (c.chars);
strcpy (d.chars, "ddddddddd");
d.cur_len = 5;
ret_val = pass_char_arrays (a.chars, b.chars, &c, &d);
printf ("a: %s\n", a.chars); /* prints aaaaaaaaa */
printf ("b: %s\n", b.chars); /* prints yyyyyyyyy */
printf ("c: %s\n", c.chars); /* value parm prints xxxxxxxxx */
printf ("d: %s\n", d.chars); /* prints wwwwwdddd */
printf ("return mask: %d\n", ret_val); /* print 0 */
ret_val = pass_a_string (&c);
printf ("c: %s\n", c.chars); /* prints qqqqqqqqq */
printf ("return mask: %d\n", ret_val); /* print 0 */
return 0;
}
Figure 3-2. Calling a Pascal Procedure from HP C
The program produces the following output:
a: aaaaaaaaa
b: yyyyyyyyy
c: xxxxxxxxx
d: wwwwwdddd
return mask: 0
c: qqqqqqqqq
return mask: 0
The routine pass_a_string() expects a generic string (described in
Note 6 above), so you must pass an extra argument. The extra
argument consists of a value parameter containing the maximum
length of the char array.
3. HP Pascal routines do not maintain a null byte at the end of HP C
strings. HP Pascal determines the current length of the string by
maintaining the length in a 4-byte integer preceding the character
data. When an HP Pascal procedure or function (that takes as a
parameter a string by reference) is called, the following code is
necessary if the Pascal routine modifies the string:
pass_a_string (a, temp); /* From note 2 above */
a.chars[a.cur_len] = '\0';
4. In non-ANSI mode, HP C promotes most float (32-bit) arguments to
double (64-bit). Therefore, all arithmetic using objects defined
as float is actually using double code. Float code is only used
when the float objects are stored.
In ANSI mode where function prototypes have been declared with a
float parameter, no automatic promotion is performed. If the
prototype is within the current scope, floats will not be
automatically promoted.
To call an HP Pascal routine that expects an argument of type REAL
(32-bits), you may either declare a function prototype in ANSI
mode, use the +r command line option in non-ANSI mode to always
pass floats as floats, or declare the actual parameter as a struct
with a float as its only field, such as:
typedef struct {float f;} PASCAL_REAL_ARG;
5. HP Pascal global data can usually only be accessed by HP C if the
data is declared at the outermost level. HP Pascal stores the
names of the objects in lowercase letters.
For example, the HP Pascal global:
PROGRAM example;
VAR
PASCAL_GLOBAL: INTEGER;
BEGIN END.
is accessed by HP C with this declaration:
extern int pascal_global;
The Pascal compiler directives $GLOBAL$ and $EXTERNAL$ can be used to
share global data between HP Pascal and HP C.
The $EXTERNAL$ directive should be used to reference C globals from a
Pascal subprogram.
The $GLOBAL$ directive should be used to make Pascal globals visible to
other languages such as HP C. It should be used if it is necessary to
share globals when calling C functions from a Pascal program.
Linking HP Pascal Routines on HP-UX
When calling HP Pascal routines, you must include the HP Pascal run-time
libraries by adding the following option to the cc command line:
-lcl
Additionally, the -lm option may be necessary if the Pascal routines use
the Pascal predefined math functions.
For details on linking external libraries, see the -l option of the cc(1)
and ld(1) commands in the HP-UX Reference manual.
Linking HP Pascal Routines on MPE/iX
The object file or files containing the HP Pascal routines you want to
call must be added to the FROM list when linking. The HP Pascal run-time
libraries reside in the same file, XL.PUB.SYS, as the HP C library so no
additional action is necessary. For example:
LINK FROM=MYMAINO,MYCO,MYPASO; TO=PROG; RL=LIBCINIT.LIB.SYS, &
LIBCANSI.LIB.SYS
links the HP C object files MYMAINO and MYCO with the HP Pascal object
file MYPASO.
MPE/iX 5.0 Documentation