Repository navigation
Language Specification (14) : Low Level Facilities
Low-level facilities are provided by special module UNSAFE. Although presented as a library it is built into the language. Its facilities are potentially unsafe, their use may bypass the type safety of the language and they must therefore be used with caution. The module must be imported before its facilities can be used.
Module UNSAFE provides the following identifiers:
BitsPerByte,
BytesPerWord,
BytesPerLongWord,
BitsPerAddress,
MachineTypeMaxBits,
BitOpsMaxBits,
BYTE,
WORD,
LONGWORD,
ADDRESS,
ADD,
SUB,
SETBIT,
HALT,
ADR,
CAST,
TDIV,
TMOD,
BIT,
SHL,
SHR,
ASHR,
BWNOT,
BWAND,
BWOR;
Constant BitsPerByte represents the bit width of the smallest addressable machine unit of the target architecture measured in bits. Its value is therefore target architecture dependent. For the overwhelming majority of platforms in use this value will be eight.
Memory allocation in Modula-2 is strictly in units of eight bits and multiples thereof. On (legacy) targets where the smallest addressable machine unit is not eight bits, the mapping of type OCTET to machine types is implementation defined.
Constant BytesPerWord represents the size of a machine register of the target architecture smaller than the
largest machine register. Its value is target architecture dependent.
Constant BytesPerLongWord represents the size of the largest machine register of the target architecture. Its value is target architecture dependent. If the target architecture does not support machine registers of different sizes, BytesPerLongWord is equal to BytesPerWord.
Constant BitsPerAddress represents the bit width of type ADDRESS.
Constant MachineTypeMaxBits represents the bit width of the largest machine type.
Constant BitOpsMaxBits represents the bit width of the largest operand supported by the UNSAFE bit operations ADD(), SUB(), SETBIT(), BIT(), SHL(), SHR(), ASHR(), BWNOT(), BWAND() and BWOR().
Type BYTE is a machine type.
It represents the smallest addressable storage unit of the target architecture.
Its bit width is defined by constant BitsPerByte.
Its value range is [0..POW2(BitsPerByte)-1].
Whole number literals are compatible with type BYTE.
Type WORD is a machine type.
It represents a storage unit equivalent to the size of a machine register of the target architecture.
Its bit width is defined by constants BitsPerByte and BytesPerWord.
Its value range is [0..POW2(BitsPerByte*BytesPerWord)-1].
Whole number literals are compatible with type WORD.
Type LONGWORD is a machine type.
It represents a storage unit equivalent to the size of the largest machine register of the target architecture.
Its bit width is defined by constants BitsPerByte and BytesPerLongWord.
Its value range is [0..POW2(BitsPerByte*BytesPerLongWord)-1].
Whole number literals are compatible with type LONGWORD.
Type ADDRESS is a (semi-abstract) reference type.
It represents the addressable memory space available to the Modula-2 runtime environment.
Its bit width is defined by constant BitsPerAddress.
Its value range is [0..POW2(BitsPerAddress)-1].
Whole number literals and NIL are compatible with type ADDRESS.
On platforms where the smallest addressable storage unit is eight bits, type ADDRESS represents machine addresses of the target architecture. On (legacy) platforms where the smallest addressable unit is not eight bits, the type represents abstract addresses that need to be mapped to the platform’s actual address space. On such platforms, address mapping is implementation defined.
Regardless of the underlying architecture, type ADDRESS is classified as a machine type.
Type ADDRESS exhibits type duality. It is presented both as a pointer type and as an octet sequence. It may thus be thought of as having two overlapping definitions
TYPE ADDRESS = (* Pseudo-Definition *)
| POINTER TO OCTET
| OCTETSEQ [POW2(BitsPerAddress)]
END; (* ADDRESS *)An entity a of type ADDRESS may be dereferenced using the ^ operator. An expression of the form a^ is of type OCTET and represents the octet value stored at address a.
adr := UNSAFE.ADR(x);
WRITE "octet value at address ", adr, " is ", adr^;An entity a of type ADDRESS may also be referenced by subscript. An expression of the form a[n] is of type OCTET and represents the octet value stored at address a+n, where n is an offset of type LONGCARD. Negative indices are not supported.
adr := UNSAFE.ADR(x);
WRITE "octet value at address ", adr, "[", offset, "] is ", adr[offset];Import of module UNSAFE enables the use of casting formal type syntax CAST ADDRESS and CAST OCTETSEQ within the importing module scope. Without such import the syntax is unavailable and any attempt to use it will cause a syntax error at compile time.
An argument passed to a parameter of formal type CAST ADDRESS is cast to type ADDRESS. That is, within the body of the procedure, the argument is interpreted as a value of type ADDRESS. It must be of a pointer type.
PROCEDURE Allocate ( VAR ptr : CAST ADDRESS; size : LONGCARD );
...
TYPE FooPtr = POINTER TO Foo; VAR foo : FooPtr;
Allocate(foo, TSIZE(Foo));All pointer types are passing compatible to formal type CAST ADDRESS.
An argument passed to a parameter of formal type CAST OCTETSEQ is cast to an octet sequence*. That is, within the body of the procedure, the parameter is interpreted as a stream of values of type OCTET , individually addressable by subscript. The argument may be of any type.
[*] This replaces the implicit casting semantics of ARRAY OF WORD and ARRAY OF LOC in earlier versions of Modula-2.
PROCEDURE HexDump ( dataSeq : CAST OCTETSEQ );
...
VAR foo : Foo; bar : Bar; baz : Baz;
HexDump(foo), HexDump(bar); HexDump(baz);Any entity regardless of its type is passing compatible to formal parameters of type CAST OCTETSEQ, but a parameter of type OCTETSEQ is passing compatible only to formal parameters of type OCTETSEQ.
When an argument is passed to a parameter of formal type CAST OCTETSEQ, the allocation size of the argument is passed as a hidden parameter of type LONGCARD immediately before the casting parameter.
PROCEDURE HexDump ( (*hidden size : LONGCARD;*) dataSeq : CAST OCTETSEQ );The value of the hidden parameter may be obtained within the procedure body by calling function LENGTH with the identifier of the casting parameter as its argument.
LENGTH(dataSeq) (* number of octets passed to dataSeq *)Operations on parameters of type OCTETSEQ are strictly limited to subscript, FOR iteration, ADR() , CAST() and LENGTH(). The limitation does not apply to the octet values of an octet sequence parameter.
The values of an octet sequence parameter are addressable by subscript. Negative indices are not supported.
WRITE "octet value at index ", index, " is ", dataSeq[index];An octet sequence parameter is iterable by FOR loop.
PROCEDURE HexDump ( VAR dataSeq : CAST OCTETSEQ );
BEGIN
FOR value IN dataSeq DO
WRITE value, " "
END (* FOR *)
WRITE "\n", LENGTH(dataSeq), " values\n"
END HexDump;Procedure ADD interprets the bit patterns of its operands as unsigned numbers and adds the value of the second to the first operand, ignoring overflow.
PROCEDURE ADD ( VAR x : T1; y : T2 );where T1, T2 ∈ Tbuiltin ∖ Treal
∧ tsize T1 ≥ tsize T2
Procedure SUB interprets the bit patterns of its operands as unsigned numbers and subtracts the value of the second from the first operand, ignoring underflow.
PROCEDURE SUB ( VAR x : T1; y : T2 );where T1, T2 ∈ Tbuiltin ∖ Treal
∧ tsize T1 ≥ tsize T2
Procedure SETBIT sets the bit at the zero-based index given by its second argument of the value given by its first argument to the bit value given by its third argument.
PROCEDURE SETBIT ( VAR target : T1; bitIndex : T2; bit : BOOLEAN );where T1, T2 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ 0 ≤ bitIndex ≤ 8 · tsize T1
The bit index of the least significant bit is always zero.
Procedure HALT aborts the running program and passes an exit value to its host environment. On host environments that do not support an exit value, the exit value is discarded. On host environments that support an exit value, the range and meaning of exit values is dependent on the host environment.
PROCEDURE HALT ( exitValue : T );where T ∈ Twhole
Function ADR returns the address of its argument.
PROCEDURE ADR ( value : T ) : ADDRESS;where T ∈ Tany
Function CAST represents an unsafe type transfer operation. It returns the value of its second argument, interpreted as if it was of the type given by its first argument, regardless of whether the value makes sense for the target type or not.
PROCEDURE CAST ( TypeIdent; value : T1 ) : T2;where T ∈ Tany
∧ TypeIdent = id T2
∧ tsize T1 ≤ tsize T2
If the second argument is a compile time expression, then it must be any of NIL, TRUE or FALSE, or it must be an explicitly typed expression of the form expr :: T.
If the allocation size of the second argument is smaller than the allocation size of the target type, the higher bits will be filled with zeroes up to the bit width of the target type.
Function TDIV returns the truncated division quotient of its whole number arguments.
PROCEDURE TDIV ( dividend, divisor : T ) : T;where T ∈ Twhole
Function TMOD returns the truncated division modulus of its whole number arguments.
PROCEDURE TMOD ( dividend, divisor : T ) : T;where T ∈ Twhole
Function BIT returns TRUE if the bit at the zero-based index given by its second argument of the value given by its first argument is set, otherwise FALSE.
PROCEDURE BIT ( value : T1; bitIndex : T2 ) : BOOLEAN;where T1 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ 0 ≤ bitIndex < 8 · tsize T1
The bit index of the least significant bit is always zero.
Function SHL returns the value of its first argument bit-shifted to the left by the number of positions passed in its second argument.
PROCEDURE SHL ( value : T1; shiftFactor : T2 ) : T3;where T1 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ T3 = T1
Bits that are shifted out to the left are discarded and will not cause overflow.
Function SHR returns the value of its first argument bit-shifted logically to the right by the number of positions passed in its second argument.
PROCEDURE SHR ( value : T1; shiftFactor : T2 ) : T3;where T1 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ T3 = T1
Bits that are shifted out to the right are discarded and will not cause underflow.
Function ASHR returns the value of its first argument bit-shifted arithmetically to the right by the number of positions passed in its second argument.
PROCEDURE ASHR ( value : T1; shiftFactor : T2 ) : T3;where T1 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ T3 = T1
Bits that are shifted out to the right are discarded and will not cause underflow.
Function BWNOT returns the bitwise logical negation of its argument.
PROCEDURE BWNOT ( value : T1 ) : T2;where T1 ∈ Tbuiltin
∧ T2 = T1
Function BWAND returns the bitwise logical conjunction of its first argument and a bit mask given by its second argument.
PROCEDURE BWAND ( value : T1; mask : T2 ) : T3;where T1 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ tsize T2 = tsize T1
∧ T3 = T1
Function BWOR returns the bitwise logical disjunction of its first argument and a bit mask given by its second argument.
PROCEDURE BWOR ( value : T1; mask : T2 ) : T3;where T1 ∈ Tbuiltin
∧ T2 ∈ Tcardinal
∧ tsize T2 = tsize T1
∧ T3 = T1
Procedures and functions provided by module UNSAFE may not be assigned and may not be passed as arguments to any procedure, function or macro.
Copyright © 2015-2018 Modula-2 Software Foundation