[comp.lang.modula3] Bad code for address arithmetic

mjordan (Mick Jordan) (04/02/91)

This program exhibits a bug in the code generated for address arithmetic.

UNSAFE MODULE Bug2 EXPORTS Main;

IMPORT Word, Wr, Stdio, Fmt;

TYPE 
  Entry = RECORD
    r: Word.T;
    val: REFANY := NIL;
  END;

VAR 
  e: Entry;
  eP1, eP2: UNTRACED REF Entry;
  eA1, eA2: ADDRESS;

BEGIN
  eP1 := ADR(e); eP2 := eP1; 
  eA1 := ADR(e); eA2 := eA1;
  Wr.PutText(Stdio.stdout, Fmt.F("ADRSIZE=%s\n", Fmt.Int(ADRSIZE(Entry))));
  INC(eP2, ADRSIZE(Entry)); 
  INC(eA2, ADRSIZE(Entry));
  Wr.PutText(Stdio.stdout, Fmt.F("eP1=%s, eP2 = %s\n",
      Fmt.Addr(eP1), Fmt.Addr(eP2)));
  Wr.PutText(Stdio.stdout, Fmt.F("eA1=%s, eA2 = %s\n",
      Fmt.Addr(eA1), Fmt.Addr(eA2)));
END Bug2.

The output from this (on my DECstation 5000) was:

ADRSIZE=8
eP1=1000f068, eP2=1000f0a8      <----  incorrect increase of 64 bytes
eA1=1000f068, eP2=1000f070      <----  correct increase of 8 bytes

The generated code is similar for the "UNTRACED REF Entry" and ADDRESS
case (eP2 += 8, eA2 += 8), but those of you who know C will realise
that the C compiler has chosen to scale the addition based on its
knowledge of the type, which is not what the Modula-3 compiler expected.

Mick Jordan

muller@src.dec.com (Eric Muller) (04/02/91)

In article <9104020125.AA12615@jumbo.pa.dec.com>, mjordan (Mick Jordan) writes:
> 
> This program exhibits a bug in the code generated for address arithmetic.

Sorry about that. It will be fixed for the next release.

-- 
Eric.

muller@src.dec.com (Eric Muller) (04/02/91)

In article <9104020125.AA12615@jumbo.pa.dec.com>, mjordan (Mick Jordan) writes:
> 
> This program exhibits a bug in the code generated for address arithmetic.
> ...
>   INC(eP2, ADRSIZE(Entry)); 
>   INC(eA2, ADRSIZE(Entry));

While waiting for the next release, you can use:

     eP2 := eP2 + ADRSIZE (Entry);
     eA2 := eA2 + ADRSIZE (Entry);

-- 
Eric.