• Aucun résultat trouvé

Encyclopedia of Domain Pascal Code

Dans le document Domain Pascal Language Reference (Page 143-151)

QUAD WORD ALIGNMENT

4.7 Encyclopedia of Domain Pascal Code

The remainder of this chapter contains an explanation of the concepts and keywords that you can use in the action part of a Domain Pascal program or routine. These items are listed alphabetically.

The concepts that we include are as follows:

Array operations

Bit operators

Compiler directives

Expressions

Pointer operations

Record operations

Set operations

Statements

Type transfer functions

Variable-length string operations

4-10 Code

The keywords that we include are:

abs cos for new ptoc sqr

addr ctop get next put sqrt

align discard goto nil read, readln substr

and dispose if not repeat/until succ

and then div in odd replace trunc

append end in_range open reset unpack

arctan eof lastof or return while

arshft eoln In ord rewrite with

begin exit Ishft or else round write, writeln

case exp max pack rshft xor

chr find min page sin

dose firstof mod pred sizeof

Code 4-11

Abs

Abs Returns the absolute value of an argument.

FORMAT

abs(number) {abs is a function.}

ARGUMENTS

number Any real or integer expression.

FUNCTION RETURNS

The abs function returns a real value if number is real and an integer value if number is an integer.

DESCRIPTION

The abs function returns the absolute value of the argument. The absolute value is the number if it is nonnegative, and the negative of the number if it is negative. Note that number cannot be -2147483648 (which is the lowest negative integer).

EXAMPLE

program abs example;

{ This prog;am displays the absolute values for two numbers } VAR

x Y

BEGIN

INTEGER;

REAL;

x := -3;

y := -456.78;

WRITELN(x,Y);

END.

USING THIS EXAMPLE

x := ABS(x);

y := ABS(y);

If you execute the sample program named abs_example, you get the following output:

3 4. 567800E+02

4-12 Code

Addr

Addr Returns the address of the specified variable. (Extension)

FORMAT

addr(x) {addr is a function.}

ARGUMENTS x

FUNCTION RETURNS

Can be a variable declared as any data type except as a procedure or function data type having the internal attribute. x can also be a string constant but it cannot be a constant of any type other than string.

The addr function returns an univ _ptr value. (Chapter 3 describes the univ _ptr data type.)

DESCRIPTION

Use addr to return the address at which variable x is stored. If x is a variable-length string, addr returns the address of the entire record, not the address of the string compo-nent. Addr is particularly useful with variables defined as pointers to functions or proce-dures.

Using addr can prevent some compiler optimizations. If you apply addr to a variable that is local to a routine, and the variable is not a set, record, or array, you do not get op-timizations and register allocation for that variable or any expressions using the variable.

This means the routine's code might be larger and slower than it otherwise would be.

Applying addr to a variable is equivalent to declaring the variable volatile. See Chapter 3 for more information on volatile.

Refer to the "Pointer Operations" listing later in this chapter for an example of addr.

Code 4-13

Addr

NOTE: The compiler issues a warning if you assign the result of addr to a pointer type variable that expects an alignment greater than the alignment of the addr result. For example,

TYPE

natural _integer

=

[natural] integer32;

rec

=

record

In this example, the assignment of addr(r.int32) to iptr causes a compile-time warning. The code in the example declares iptr as a pointer to a naturally aligned integer32 type, but assigns to it the address of a word-aligned object, r.int32. (See the "Internal Representation of Records" and" Alignment" sections of Chap-ter 3 for further details about alignment.)

USING THIS EXAMPLE

Following is a sample run of the program named addr_example:

Enter a real number -- 5.3

5.300000E+OO

Addr

Code 4-15

Align

Align Causes the compiler to copy an expression that is being passed as a parameter. (Extension)

FORMAT

align (expression); {align is a function.}

ARGUMENTS expression

FUNCTION RETURNS

Any valid Domain Pascal expression that is being passed as an input parameter to a routine.

The align function returns a correctly aligned copy of expression.

DESCRIPTION

The align function tells the compiler to make a copy of an expression passed as an in pa-rameter to an external routine. The alignment of the copy matches the alignment specified for the formal parameter. The compiler uses the copy as the actual parameter when the external routine is called.

The align function is useful for making sure that expressions are correctly aligned when they are passed as arguments to functions and procedures. An expression is correctly aligned if its alignment matches the alignment specified for it in the formal parameter defi-nition.

The main use of the align function is to pass a record field that is not naturally. aligned to a routine that expects the parameter to be naturally aligned. This use is illustrated in the example.

Although correct alignment for parameters passed by reference generally produces at least somewhat faster executable code on all Apollo workstations, the improvement is very sig-nificant on Series 10000 workstations. If you run your program on a Series 10000 work-station and the compiler assumes that an object is naturally aligned when in fact, it is not naturally aligned, your program will suffer a severe loss of efficiency.

4-16 Code

Align

integer32 ) :integer32; extern;

write('The sum of the numbers is ');

Dans le document Domain Pascal Language Reference (Page 143-151)