[Oberon] Bit-fiddling: SETs and type casts in Oberon-07

joerg.straube at iaeth.ch joerg.straube at iaeth.ch
Tue Aug 9 07:53:04 CEST 2022


Hans

Generally - although defined in the language and not in module SYSTEM - code using ORD() is not portable.

i := ORD(ch); (* is not portable as the charset is not defined in the Oberon-07 report *)
i := ORD(bool); (* is not portable as the Oberon-07 reports does not define the representation of TRUE and FALSE *)
i := ORD(set); (* is not portable as the bit numbering is not defined *)

Although not fully portable but doesn’t need SYSTEM, stay with CHR( ORD(ch) - ORD(“a”) + ORD(“A”) );
Don’t be afraid of the complex looking ORD(“a”) + ORD(“A”), the compiler folds these constants into one value

br
Jörg

Von: Oberon <oberon-bounces at lists.inf.ethz.ch> im Auftrag von Hans Klaver <hklaver at dds.nl>
Datum: Dienstag, 9. August 2022 um 01:07
An: ETH Oberon and related systems <oberon at lists.inf.ethz.ch>
Betreff: Re: [Oberon] Bit-fiddling: SETs and type casts in Oberon-07
Chris, Jörg and Florian,

Thanks for your answers. Now it makes more sense to me.

I looked into these matters a bit more.

It appears that Wirth's OR compiler always allows casts of 'scalar' types (here meaning all Oberon basic types) with SYSTEM.VAL to a type of the same or smaller SYSTEM.SIZE (narrowing casts), without warning. Widening casts (to a type with larger SYSTEM.SIZE) are only possible via cast to BYTE and INTEGER.

I suppose this is (as Florian wrote) because widening casts lead to 'undefined behaviour'. Well, I think the behaviour in cases of SYSTEM.VAL(SET, v) is only partially undefined: the part of the SET one is interested in (elements 0 .. 8*SYSTEM.SIZE(v)-1) is quite well defined as long as it is guaranteed that element 0 of the SET is the least significant bit of the variable; the undefined part, the elements > 8*SYSTEM.SIZE(v)-1, should just be disregarded.

The other two Oberon-07 compilers I checked (Astrobe for Cortex-M3 and OBNC) allow all casts of the basic types to and from each other, narrowing or widening; the Astrobe compiler gives a warning 'type cast !' with any use of SYSTEM.VAL.

Chris wrote:

> The Oberon-07 report states:
>
> EXCL(v, x) is equivalent to v := v - {x}.  (* where v is a variable not an expression *)
>
> Hence trying to write EXCL(SYSTEM.VAL(SET, ch), 5) would be equivalent to writing:
>
> SYSTEM.VAL(SET, ch) := SYSTEM.VAL(SET, ch) - {5}
>
> To me, that is as wrong as trying to write something like:
>
> ORD(ch) := ORD(ch) + 1
>
> or INC(ORD(ch))


Concerning this I found out that only OBNC requires a variable as first parameter of EXCL and INCL, and disallows SYSTEM.VAL in its place; Wirth's OR and the Astrobe compiler have no problem with SYSTEM.VAL as first parameter of these procedures. So for instance they accept the following (Astrobe with a warning 'type cast !'):

  VAR i: INTEGER;
  ...
  EXCL(SYSTEM.VAL(SET, i), 5)
  RETURN i

This is in accordance with the language report: there it says that SYSTEM.VAL has the function of 'identity' (earlier described as 'x interpreted as of type T'); use of this function here can be equated with use of a variable.

So I think that OBNC is a bit too restrictive in disallowing SYSTEM.VAL as first parameter of EXCL and INCL.

Regards,

Hans
--
Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
https://lists.inf.ethz.ch/mailman/listinfo/oberon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20220809/6149394f/attachment.html>


More information about the Oberon mailing list