comp.lang.ada
 help / color / mirror / Atom feed
* Errornous String instantiation
@ 2003-07-21 10:43 Lutz Donnerhacke
  2003-07-21 10:56 ` Preben Randhol
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Lutz Donnerhacke @ 2003-07-21 10:43 UTC (permalink / raw)


Sorry for posting this problem in such an early stage, but it's a moving
target where small modifications of the source the problem cause to disappear.
So I present a short (but not buggy) example of the source skeleton in
question and show you some results of the real data.

gcc (GCC) 3.2.2

************************************************************************
package Constants is
   t1 : aliased constant String := "Test";
   t2 : aliased constant String := "A longer test string";
end Constants;

package Top is
   type Constant_String is access constant String;
   type Constant_String_Array is array(Positive range <>) of Constant_String;
end Top;

generic
   data : Constant_String_Array;
package Top.Generic_Package is
   pragma Elaborate_Body;
end Top.Generic_Package;

with Ada.Text_IO;
use Ada.Text_IO;

package body Top.Generic_Package is
begin
   for i in data'Range loop
      Put_Line("data (" & i'Img & " ) =" & data(i)'Length'Img);
   end loop;
   for i in data'Range loop
      Put_Line("data (" & i'Img & " ) =" & data(i)'Length'Img);
   end loop;
end Top.Generic_Package;

with Top.Generic_Package, Constants;
use Constants;

package MyPackage is new Top.Generic_Package((t1'Access, t2'Access));

with MyPackage;

procedure Main is
begin
   null;
end Main;
************************************************************************
The expected result is:
 data ( 1 ) = 4
 data ( 2 ) = 20
 data ( 1 ) = 4
 data ( 2 ) = 20
\f
Doin this with the real program results in:
 Start Elaborating
 Testing Len  1 20
 Testing Len  2 4
 Testing Len  3 20
 Testing Len  4 50
 Testing Len  5 20
 Testing Len  6 80
 Testing Len  7 80
 Testing Len  1 20
 Testing Len  2 0
 Testing Len  3 20
 Testing Len  4 50
 Testing Len  5 20
 Testing Len  6 80
 Testing Len  7 80
 End Elaborating

using the Source:
 Debugging.Hex_Dump(True, "", "Start Elaborating"); 
 for i in keys'Range loop    
    Debugging("Testing Len " & i'Img & keys(i)'Length'Img);
 end loop; 
 for i in keys'Range loop    
    Debugging("Testing Len " & i'Img & keys(i)'Length'Img);
 end loop;
 Debugging.Hex_Dump(True, "", "End Elaborating");


The excerpt from the ASM-Output look strange:
	.section	.rodata
.LC17:
	.ascii	"Jefe"
[...]
	movl	$rfc2202__k1+8, -72(%ebp)
	movl	$rfc2202__k1, -68(%ebp)
	movl	$.LC17, -64(%ebp)
	movl	$1, %eax
	movl	$4, %edx
	movl	%eax, -80(%ebp)
	movl	%edx, -76(%ebp)
	leal	-80(%ebp), %eax
	movl	%eax, -60(%ebp)
	movl	$rfc2202__k3+8, -56(%ebp)
	movl	$rfc2202__k3, -52(%ebp)


Where the referred constants come from:

package RfC2202 is
   k1 : aliased constant String := (1 .. 20 => Character'Val(16#0b#));
   d1 : aliased constant String := "Hi There";
   sha1_1 : aliased constant Hex_String := "b6173186 55057264 e28bc0b6 fb378c8e f146be00";
   md5_1  : aliased constant Hex_String := "9294727a 3638bb1c 13f48ef8 158bfc9d";

   k2 : aliased constant String := "Jefe";
   d2 : aliased constant String := "what do ya want for nothing?";
   sha1_2 : aliased constant Hex_String := "effcdf6a e5eb2fa2 d27416d5 f184df9c 259a7c79";
   md5_2  : aliased constant Hex_String := "750c783e 6ab0b503 eaa86e31 0a5db738";
   [...]
end RfC2202;

.globl rfc2202__k1
	.align 4
	.type	rfc2202__k1,@object
	.size	rfc2202__k1,28
rfc2202__k1:
	.long	1
	.long	20
	.byte	11
	[...]
	.byte	11
.globl rfc2202__d1
	.data
	.align 4
	.type	rfc2202__d1,@object
	.size	rfc2202__d1,16
rfc2202__d1:
	.long	1
	.long	8
	.ascii	"Hi There"
.globl rfc2202__sha1_1
	.section	.rodata
	.align 32
	.type	rfc2202__sha1_1,@object
	.size	rfc2202__sha1_1,52
rfc2202__sha1_1:
	.long	1
	.long	44
	.ascii	"b6173186 55057264 e28bc0b6 fb378c8e f146be00"
.globl rfc2202__md5_1
	.align 32
	.type	rfc2202__md5_1,@object
	.size	rfc2202__md5_1,44
rfc2202__md5_1:
	.long	1
	.long	35
	.ascii	"9294727a 3638bb1c 13f48ef8 158bfc9d"
	.zero	1
.globl rfc2202__k2
	.data
	.align 4
	.type	rfc2202__k2,@object
	.size	rfc2202__k2,12
rfc2202__k2:
	.long	1
	.long	4
	.ascii	"Jefe"
.globl rfc2202__d2
	.section	.rodata
	.align 32
	.type	rfc2202__d2,@object
	.size	rfc2202__d2,36
rfc2202__d2:
	.long	1
	.long	28
	.ascii	"what do ya want for nothing?"
[...]

It's interesting to note, that short string constants (<= 8 Characters) are
deferred instantiated (not .rodata), while longer string constants are
stored in the object file rfc2202.o.

When constructing the array for the generic instantiation, the short
constant string is instantiated directly without the bounds. Beside array
with the bounds is constructed correctly on the stack (and then copied to an
explicit data section), the constructing stack space is reused directly
afterwards. That's why the bounds (which are stored indirectly) are
destroyed later in the program.

Did anybody came across this type of bug? Any idea how to solve the problem?



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2003-07-23 14:03 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-21 10:43 Errornous String instantiation Lutz Donnerhacke
2003-07-21 10:56 ` Preben Randhol
2003-07-21 11:02   ` Lutz Donnerhacke
2003-07-21 11:07     ` Preben Randhol
2003-07-21 12:35       ` Lutz Donnerhacke
2003-07-21 12:55         ` Lutz Donnerhacke
2003-07-21 21:54           ` Adrian Knoth
2003-07-21 11:08   ` Florian Weimer
2003-07-21 11:17     ` Preben Randhol
2003-07-21 16:19 ` Matthew Heaney
2003-07-21 16:26   ` Lutz Donnerhacke
2003-07-23 14:03 ` Lutz Donnerhacke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox