Alex Gutteridge wrote: > > The example Perl6 'Game of Life' program > (parrot/languages/perl6/examples/life.p6) is broken. As it stands it > doesn't parse correctly but this is easily fixed by adding the correct > string concatenation operator '~':
I've checked in your update to the concat operator. > However, now parrot crashes with a segfault somewhere in the Generate > subroutine: [...] > I've had a look at the generated (and attached) imc file, and can't see > anything obviously crazy. I'm not sure how to go about debugging this > further. (My apologies for being so verbose, I'm hoping to be educational as well as solve the problem. Leo, skip to the last 10 lines and scan backwards.) One good rule is to create the simplest code that duplicates the problem. For one bug this was: ---- sub Generate($input) { my $output = $input; return $output; } sub main() { my $world = "string"; $world = Generate($world)[0]; print "returned '$world' from Generate\n"; } ---- This made a much shorter generated .imc file (much easier to scan), and I found an orphaned ".return" directive without the surrounding ".pcc_begin_return" and ".pcc_end_return" directives. This bug was actually in the original generated output, just difficult to find: > $P305[0] = _SV_output1 > # scalar_in_context(array) from P6C::variable::val_in_context END > .return $P305 > goto L_1 I fixed this in the "prefix_return" method in P6C::IMCC::prefix. This fix was enough to get the code running on OSX. Linux is a bit trickier. It was still segfaulting after the fix. With a series of debug prints I managed to track the segfault down to a loop in the Generate routine. The annoying thing was the segfault kept moving. It would segfault in exactly the same place every time I ran the same code (always on a line with a call to 'substr'), but as I added more debug prints it would segfault earlier and earlier (still always on a line with a call to 'substr'). This is when I started thinking "Oooh, memory problems." (The code loops 100 times. Each outer loop calls a subroutine that loops 256 times, and each inner loop calls 'substr' 10/11 times on a string that's 256 characters long. That's a rather large number of string allocations.) This is the smallest bit of code I could get to segfault in my linux dev box: ---- sub main () { my $string = "****************************************************************************************************************************************************************************************************************************************************************"; for 1..11 { print1 "in loop $_\n"; substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); substr ($string, $_, 1); } print "made it past substr\n"; } ---- 15 calls to 'substr' in each loop segfaults (14 doesn't), and it always segfaults on the 11th loop. To confirm my suspicion, I ran gdb on the generated PIR code from the original source: $ gdb parrot # and from the gdb prompt (gdb) run life.imc # and when it segfaulted (gdb) back ---- #0 0x401571b7 in memcpy () from /lib/libc.so.6 #1 0x08158c41 in compact_pool (interpreter=0x82ebf90, pool=0x82ec770) at src/resources.c:341 #2 0x08158923 in mem_allocate (interpreter=0x82ebf90, req_size=0xbffff3e8, pool=0x82ec770, align_1=15) at src/resources.c:154 #3 0x081591f2 in Parrot_allocate (interpreter=0x82ebf90, buffer=0x4148e0d8, size=64) at src/resources.c:603 #4 0x0815922e in Parrot_allocate_zeroed (interpreter=0x82ebf90, buffer=0x4148e0d8, size=64) at src/resources.c:625 #5 0x080d7638 in allocate_chunk (interpreter=0x82ebf90, list=0x8416800, items=16, size=64) at src/list.c:240 #6 0x080d8013 in alloc_next_size (interpreter=0x82ebf90, list=0x8416800, where=1, idx=1) at src/list.c:667 ... ---- So, this is where I hand it off to Leo. It may be a known bug in Parrot, or maybe not. Allison