Three groups of procedures and operators are provided to control various aspects of the run-time environment. These are floating-point control, process termination control and garbage-collector control.
The Intel Pentium microprocessors all have a floating-point unit (FPU) as an integral part of the microprocessor. The action of the FPU is determined by the contents of a 16-bit register called the “control word register”. Details of the register can be found in the file
/usr/include/fpu_control.h
Details of the working of the FPU, as controlled by the control word register can be found in the three volumes of the Intel Architecture Software Developer's Manual. The control word contains bits which control rounding, precision and whether floating-point errors should cause an exception.
The QAD standard prelude provides two integers which control the rounding method. They are
The spaliens.a68 file also has three procedures which give access to two C functions.
Here is the source code (in spops.a68) for the operator
ROUND
:-
OP ROUND = (REAL r)INT: IF r > max int OR r < -max int THEN 0 ELSE INT method=fe get round; fe set round(fe tonearest); INT i=lrint(r); fe set round(method); i FI; #ROUND#
Notice how the FPU control word is reset to its original value before the end of the operator.
The FPU control word is also used to control whether overflow should be detectable. The standard mode of operation is to ignore integer overflow. The procedure to trap a signal (on signal) is declared as follows:-
PROC on signal=(INT sig, PROC(INT)VOID handler)VOID:
The example program testov.a68
shows how on signal
can be
used. The Algol 68 identifiers for all the Linux signals are the same
as the Linux signal identifiers, but in lower case. For example, the
signal used in FPU control is sigfpu
. The signal generated by
keying Ctrl-C
(sometimes depicted as ^C
) on program input
is sigint
. Here is a short program which illustrates signal
trapping:-
PROGRAM sig CONTEXT VOID USE standard BEGIN on signal(sigint, (INT sig)VOID: (write(("sigint trapped", newline)); exit(1))); write("Please key ^C: "); read(LOC CHAR); write(("No signal trapped",newline)) END FINISH
Usually, when you trap a signal such as
sigint, your program could close down
processing in an orderly manner: files could be closed properly, a
message to the user could be issued, and so on. You can do anything
you want in the procedure provided as a parameter to on
signal
. You can also predeclare the procedure and simply
provide its identifier in the on signal
call.
Integer overflow is ignored by the microprocessor. So the formula
max int + 3
simply yields an incorrect value.
The procedure ansi raise will cause any specified signal to occur. Here is the mode of ansi raise:
PROC ansi raise = (INT sig)INT:
As well as raising and trapping signals, it is sometimes useful to specify procedures to be elaborated when your program has finished, for whatever reason. Four procedures are provided for process termination:-
p
is registered to be elaborated when the program
terminates normally or when the procedure exit
(see procedure
3) is called. Registered procedures are elaborated in
the reverse order of being registered, so that the procedure registered
last is elaborated first. The procedure at exit
yields 0 for
success, -1 for an error.
[]CHAR arg)INT:
on exit
allows you to register a procedure which takes two
parameters. The first is the integer parameter given to the
exit
procedure (or 0 for normal termination) and the second is a
[]CHAR
which the procedure p
can use. on exit
yields 0 for success and -1 for an error.
status
will
be passed to the parent process of the program.
exit(0)
.
testexit.a68
shows one way in which
at exit
and on exit
may be used.
The garbage-collector manages the run-time heap. The term “garbage” is used to designate memory on the heap which is no longer referenced. Although the garbage-collector is usually called whenever space on the heap is required, a number of routines are provided for explicit garbage collection or for fine control of the garbage-collector.
cmd
should
consist of GET
or SET
followed by the string
identifying the required parameter followed by a nul ch
.
The available strings are
For further details about the garbage-collector, consult the code in
the library
directory in the a68toc source tree.