TargetConversions.mesa
Copyright Ó 1986, 1991 by Xerox Corporation. All rights reserved.
Russ Atkinson (RRA) July 22, 1986 1:01:56 pm PDT
DIRECTORY
Rope USING [ROPE];
TargetConversions: CEDAR DEFINITIONS = BEGIN
The idea behind this interface is to provide isolation for the host to target and the target to host translation of values necessary in the compiler. The common external representation is a sequence of bytes, but the details of the encoding are up to the implementation.
Types
ByteSequence: TYPE = Rope.ROPE;
Temporary
Reader: TYPE = REF ReaderRep;
ReaderRep: TYPE;
A reader is like an input stream that can yield up basic Mesa values.
Writer: TYPE = REF WriterRep;
WriterRep: TYPE;
A writer is like an output stream that can be given basic Mesa values.
bitsPerWord: NAT = 32;
This length does not necessarily correspond to the bitsPerWord on the target. It is declared here for documentation purposes.
BitIndex: TYPE = [0..bitsPerWord);
BitLength: TYPE = [0..bitsPerWord];
ElemIndex: TYPE = [0..bitsPerWord);
ElemLength: TYPE = [0..bitsPerWord];
Bool: TYPE = BOOL;
a 1-bit boolean value {FALSE, TRUE}
Card1: TYPE = CARDINAL [0..1];
an unsigned 1-bit quantity
Card2: TYPE = CARDINAL [0..3];
an unsigned 2-bit quantity
Card4: TYPE = CARDINAL [0..15];
an unsigned 4-bit quantity
Card8: TYPE = CARDINAL [0..255];
an unsigned 8-bit quantity
Card16: TYPE = CARDINAL [0..65535];
an unsigned 16-bit quantity
Card32: TYPE = CARD;
an unsigned 32-bit quantity
Char: TYPE = CHAR;
an ASCII (more or less) 8-bit character
Int32: TYPE = INT;
a signed 32-bit quantity
Real: TYPE = RECORD [
SELECT realKind: RealKind FROM
real32 => [real32: Real32],
real48 => [real48: Real48],
real64 => [real64: Real64],
real80 => [real80: Real80],
ENDCASE
];
RealKind: TYPE = {real32, real48, real64, real80};
Real32: TYPE = REAL;
Real48: TYPE = PACKED ARRAY [0..48) OF BOOL;
Real64: TYPE = PACKED ARRAY [0..64) OF BOOL;
Real80: TYPE = PACKED ARRAY [0..80) OF BOOL;
For now only the single variant is supported, and the other formats are merely speculative.
Bit Stream Writers
NewWriter: PROC RETURNS [Writer];
Returns a new bit stream writer with no bits.
ResetWriter: PROC [writer: Writer];
Resets the writer to ground zero, but retains intermediate storage for the sake of efficiency.
WriterContents: PROC [writer: Writer] RETURNS [ByteSequence];
Returns the byte sequence corresponding to the currently written contents of the given writer. NIL will be returned if there are no bits in the stream.
BitsWritten: PROC [writer: Writer] RETURNS [INT];
Returns the number of bits currently written to the stream. Useful for hairy alignment problems not covered by PutAlignment.
PutBool: PROC [writer: Writer, bool: Bool];
Puts out the given 1-bit boolean value.
PutByteSeq: PROC [writer: Writer, seq: ByteSequence, len: INT];
Puts out the first len bytes of the given byte sequence. Requires byte alignment (BitsWritten[writer] MOD 8 = 0).
PutCard: PROC [writer: Writer, card: Card32, bits: BitLength];
Puts out the given unsigned number using the low-order BitLength bits.
PutCard32: PROC [writer: Writer, card: Card32];
Puts out the given 32-bit unsigned number. PutCard32[w, c] is the same as (but faster than) PutCard[w, c, 32]. Requires byte alignment.
PutChar: PROC [writer: Writer, char: Char];
Puts out the given character.
PutInt: PROC [writer: Writer, int: Int32, bits: BitLength];
Puts out the given signed number using the low-order bits of int. The given Int32 number must have correct sign extension.
PutInt32: PROC [writer: Writer, int: Int32];
Puts out the given 32-bit signed number. PutInt32[w, i] is the same as (but faster than) PutInt[w, i, 32].
PutPadBits: PROC [writer: Writer, bits: BitLength];
Puts out the requested number of padding bits.
PutReal: PROC [writer: Writer, real: Real];
Puts out the given Real.
SetOutputIndex: PROC [writer: Writer, bits: INT];
Sets the bit position in the writer.
GetOutputIndex: PROC [writer: Writer] RETURNS [INT];
Returns the bit position in the writer.
Bit Stream Readers
NewReader: PROC [seq: ByteSequence, start: INT ¬ 0, len: INT ¬ LAST[INT]] RETURNS [Reader];
Returns a new bit stream Reader with initial contents given by the ByteSequence (starting at byte offset start for len bytes).
InitReader: PROC [reader: Reader, seq: ByteSequence, start: INT ¬ 0, len: INT ¬ LAST[INT]];
Initializes an existing Reader with contents given by the ByteSequence (starting at byte offset start for len bytes). If len exceeds the amount of bytes remaining in the sequence, len will be reduced to describe the remaining number of bytes.
RemainingBits: PROC [reader: Reader] RETURNS [INT];
Returns the number of remaining bits in the reader.
BitsRead: PROC [reader: Reader] RETURNS [INT];
Returns the number of bits read from the reader.
GetBool: PROC [reader: Reader] RETURNS [Bool];
Reads a 1-bit boolean value.
GetByteSequence: PROC [reader: Reader, len: INT] RETURNS [ByteSequence];
Reads a sequence of bytes, len gives the number of bytes. Requires byte alignment (BitsRead[reader] MOD 8 = 0).
GetCard: PROC [reader: Reader, bits: BitLength] RETURNS [Card32];
Reads an unsigned number of bits precision.
GetCard32: PROC [reader: Reader] RETURNS [Card32];
Reads a 32-bit unsigned number.
GetChar: PROC [reader: Reader] RETURNS [Char];
Reads an 8-bit character.
GetInt: PROC [reader: Reader, bits: BitLength] RETURNS [Int32];
Reads a signed number, precision given by bits, and properly extends the sign (if necessary).
GetInt32: PROC [reader: Reader] RETURNS [Int32];
Reads a 32-bit signed number.
SkipPadBits: PROC [reader: Reader, bits: BitLength];
Skips a given number of padding bits.
GetReal: PROC [reader: Reader, kind: RealKind ¬ real32] RETURNS [Real];
Reads an IEEE floating-point number of the specified kind.
SetInputIndex: PROC [reader: Reader, bits: INT];
Sets the bit position in the reader.
Exceptions
InsufficientBits: ERROR;
Raised when a Get& operation is called and not enough bits remain in the reader to deliver the value.
InvalidArgument: ERROR;
Raised when an argument is illegal.
NotImplemented: ERROR;
Raised when an operation calls for a translation not supported by the implementation (as in use of Real when the kind is not real32).
NotAligned: ERROR;
Raised when an operation needs at least byte alignment and the stream is not byte aligned.
Simple Types to Byte Sequences
These operations are present for efficiency. An operation of the form &ToByteSeq[&&] has the same behavior (more or less) as:
{w: Writer ← NewWriter[]; Put&[w, &&]; RESULT[WriterContents[w]]};
BoolToByteSeq: PROC [bool: Bool] RETURNS [ByteSequence];
CardToByteSeq: PROC [card: Card32, bits: BitLength] RETURNS [ByteSequence];
Card32ToByteSeq: PROC [card: Card32] RETURNS [ByteSequence];
CharToByteSeq: PROC [char: Char] RETURNS [ByteSequence];
IntToByteSeq: PROC [int: Int32, bits: BitLength] RETURNS [ByteSequence];
Int32ToByteSeq: PROC [int: Int32] RETURNS [ByteSequence];
RealToByteSeq: PROC [real: Real] RETURNS [ByteSequence];
Byte Sequences to Simple Types
These operations are present for efficiency. An operation of the form val ← ByteSeqTo&[&&] has the same behavior (more or less) as:
val ← Get&[NewReader[&&]];
ByteSeqToBool: PROC [seq: ByteSequence] RETURNS [Bool];
ByteSeqToCard: PROC [seq: ByteSequence, bits: BitLength] RETURNS [Card32];
ByteSeqToCard32: PROC [seq: ByteSequence] RETURNS [Card32];
ByteSeqToChar: PROC [seq: ByteSequence] RETURNS [Char];
ByteSeqToInt: PROC [seq: ByteSequence, bits: BitLength] RETURNS [Int32];
ByteSeqToInt32: PROC [seq: ByteSequence] RETURNS [Int32];
ByteSeqToReal: PROC [seq: ByteSequence, kind: RealKind ¬ real32] RETURNS [Real];
END.