[M3devel] Array indexing

Rodney M. Bates rodney_bates at lcwb.coop
Thu Dec 30 21:12:22 CET 2010


I reproduced this immediately on LINUXLIBC6 and AMD64_LINUX.  It's a compiler bug.
A version of cm3 several years old, and an old pm3, both with the (much older)
gcc backend and the x86 internal backend also all show the same problem.

The memory area for each row of tab is getting initialized with dope for an
open array, instead of the actual values.  Moreover, the dope is not right
either, though it's recognizable.  Since the open array row constructor is
assignable to the fixed array row of tab, the code is correct and the compiler
should take care of the representation change.

Making tab a VAR instead of CONST shows the same bad behavior.

As a workaround, you can change the row constructors to fixed array constructors:

  CONST	
     tab = ARRAY [0..2], [0..15] OF CARDINAL {
	ARRAY [0..15] OF CARDINAL
	    { 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
	ARRAY [0..15] OF CARDINAL
	    {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 17, 17, 17, 17, 17, 17 },
	ARRAY [0..15] OF CARDINAL
	    { 17, 11, 12, 13, 14, 15, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17 }
     };

and this works as expected:

(m3gdb) run
Starting program: /home/rodney/proj/m3/exp/dknoto/AMD64_LINUX/prog
Can't disable VM GC.
Signal        Stop      Print   Pass to program Description
SIG64         No        No      Yes             Real-time event 64
[Thread debugging using libthread_db enabled]
[New LWP 9108]
k = 1, l = 9
k = tab[k][l] => 0x0a
k = tab[1][9]   => 0x0a
  17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
  01 02 03 04 05 06 07 08 09 10 17 17 17 17 17 17
  17 11 12 13 14 15 16 17 17 17 17 17 17 17 17 17

Program exited normally.
(m3gdb)

Amazing that in all these years, nobody has coded a 2-D array constructor this
way before, or at least didn't complain.


Rodney Bates


Dariusz Knociński wrote:
> Hi all,
> 
> my name is Dariusz Knociński, I am new in this list, it is my first letter.
> Best wishes for New Year to all.
> 
> I have a problem with two-dimensional arrays. In my simple source code I can't
> get value from array indexed by two variables. But, I can get these values  by
> indexing by constants. The program compile successfully but generates idiotic
> output.
> 
> Source:
> 
> MODULE aib EXPORTS Main;
> 
> IMPORT IO, Fmt;
> 
> CONST	
>     tab = ARRAY [0..2], [0..15] OF CARDINAL {
> 	ARRAY OF CARDINAL
> 	    { 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 },
> 	ARRAY OF CARDINAL 
> 	    {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 17, 17, 17, 17, 17, 17 },
> 	ARRAY OF CARDINAL 
> 	    { 17, 11, 12, 13, 14, 15, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17 }
>     };
> 
> VAR
>     k, l : CARDINAL;
> 	
> BEGIN
> 	
>     k := 1;
>     l := 9;
> 	
>     IO.Put( "k = " & Fmt.Int(k) & ", " );
>     IO.Put( "l = " & Fmt.Int(l) & "\n" );
>     k := tab[k][l];	
>     IO.Put( "k = tab[k][l] => 0x" & 
> 	Fmt.F( "%02s", Fmt.Unsigned(k)) & "\n" );
> 	
>     k := tab[1][9];
>     IO.Put( "k = tab[1][9]   => 0x" & 
> 	Fmt.F( "%02s", Fmt.Unsigned(k)) & "\n" );
> 		
>     FOR i := 0 TO 2 DO
> 	FOR j := 0 TO 15 DO
> 	    IO.Put( Fmt.F( " %02s", Fmt.Int( tab[i][j] ) ) );
> 	END;
> 	IO.Put( "\n" );
>     END;
> 	
> END aib.
> 
> Output:
> 
> k = 1, l = 9
> k = tab[k][l] => 0x00
> k = tab[1][9]   => 0x0a
>  6296864 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>  6296864 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>  6296864 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00
> 
> My system and compiler version is:
> 
> $ uname -a
> 
> Linux wenus.next.com.pl 2.6.35.10-74.fc14.x86_64 #1 SMP Thu Dec 23 16:04:50\
>     UTC 2010 x86_64 x86_64 x86_64 GNU/Linux
>     
> $ cm3 -version
> 
> Critical Mass Modula-3 version 5.8.6
>   last updated: 2010-04-11
>   compiled: 2010-07-12 20:10:34
>   configuration: /usr/local/cm3/bin/cm3.cfg
>   host: AMD64_LINUX
>   target: AMD64_LINUX
> 
> or
> 
> Critical Mass Modula-3 version d5.9.0
>   last updated: 2010-07-21
>   compiled: 2010-12-16 03:15:26
>   configuration: /usr/local/cm3/bin/cm3.cfg
>   host: AMD64_LINUX
> quake runtime error: "/usr/local/cm3/bin/cm3.cfg", line 2: quake runtime error:\
>  undefined variable: SL
> 
> --procedure--  -line-  -file---
>                     2  /usr/local/cm3/bin/cm3.cfg
>   target: 
> 
> Both compilers generate same results.
> 
> Best regards
> DKnoto.
> 



More information about the M3devel mailing list