On 11 Oct 2001, at 16:40, Theo Carr-Brion wrote:
I am using gpc 20010924 with gcc 2.95.3 on UnixWare 7.1 and the following code does not work.
It fails on gpc 20010604 as well.
The following modification to your program illustrates the source of the error:
Program Test2(Output);
Type Str10 = String(10); LetterType = (AA, BB, CC, DD); MyRec = record LetterName: Array[LetterType] of Str10; end;
var ThisLetter: LetterType; ThisRec: MyRec;
begin for ThisLetter:=AA to DD do writeln (ThisRec.LetterName[ThisLetter].Capacity); end.
The problem is that "Capacity" is not being set for all four strings during program initialization (the type "String" is a schema consisting of "Capacity" -- the maximum length of the string, "Length" -- the current length of the string, and the actual characters themselves). To avoid overrunning the target string, the string assignment in the "SetIt" procedure assigns the shorter of the source string length and the target string capacity. As the target string capacity is not being set for the third and fourth strings in the array, their capacities are defaulting to zero. Therefore, the third and fourth string assignments are failing.
(For the string assignments in the body of the program, GPC does not generate a string length check, presumably because both the target string capacity and the source string length are known at compile time.)
Here is the relevant code generated by GPC (with -O2 -S):
_init_pascal_main_program:
[...]
xorl %edx,%edx movl $_Thisrec,%ecx jmp L12 .p2align 4,,7 L15: incl %edx L12: leal (%edx,%edx,4),%eax movl $10,(%ecx,%eax,4) testb %dl,%dl je L15
The "movl $10, ..." is correctly setting the string capacities to 10, but the statement is only being executed twice, rather than four times. It appears to me that the last two statements above should be:
cmpb $3,%dl jne L15
-- Dave