SDCC 4.6.0 #16555 (Homebrew, macOS arm64).
c
typedef struct { unsigned char x; unsigned char y; } S2;
typedef struct { S2 in; } W;
W w;
S2 f(void) { return w.in; }
sdcc -mstm8 -c repro.c:
repro.c:4: error 9: FATAL Compiler Internal Error in file 'gen.c' line number '2773' : code generator internal error
Caught signal 11: SIGSEGV
sdcc -mz80 -c repro.c:
repro.c:4: error 9: FATAL Compiler Internal Error in file 'gen.c' line number '6373' : code generator internal error
Caught signal 11: SIGSEGV
sdcc -mmcs51 -c repro.c: compiles without diagnostics.
Same symptom and the same port pattern as [#4131] (STM8 and Z80 crash, MCS51 accepts; its reproducer also hits gen.c:2773 on STM8 with this version), but no conditional operator is involved, so the candidate fix in #4131 (conditional-expression lowering) may not cover it.
Also crashing on STM8: return *p; (S2 *p), return u.s; (union member, local or global), return (S2)&tmp;.
Working: returning a named struct variable (local or global, sizes 1, 2, 4, 8 bytes); passing structs as parameters; workaround S2 r = w.in; return r;.
Found while porting ST’s SPIRIT1 library (return *status; in its SPI driver).
Analysis
Affected code
src/stm8/gen.cgenReturn()takes the scalar path becauseIS_STRUCT (operandType (IC_LEFT (ic)))is false for the pointer-typed operandaopRet()returns NULL for struct-returning functionsgenMove():wassert (result)fires (line 2773 in 4.6.0 #16555), thengenMove_o()dereferences NULL → SIGSEGVsrc/z80/gen.c, line 6373 in 4.6.0 #16555 (not analysed, presumably the same pattern)src/SDCCopt.c,replaceRegEqv(), the "Convert struct / union to pointer" loopiCode dumps (
--dump-i-code)In
repro.dumpraw0member access yields a struct-typed iTemp that holds an address, used by RETURN:In
repro.dumpraw1iTemp1 has become a pointer, including in RETURN:For
return g;(named struct variable) RETURN gets_g {struct __00000000 fixed}and compiles fine.Where the type changes
Between
DUMP_RAW0andDUMP_RAW1onlyreplaceRegEqv()andcreateLoopRegions()run. InreplaceRegEqv():RETURN iCodes are skipped, but the iCode that defines iTemp1 (
iTemp1 = iTemp0 + 0x0) is not, so iTemp1 is retyped to a pointer. RETURN uses the same iTemp, so its operand becomesstruct near*as well. Named variables are unaffected because they have no defining iCode.genReturn()then sees a 2-byte non-struct operand, takescase 2, and calls:→
wassert (result)ingenMove()→ NULL dereference → SIGSEGV.Fixing
genReturn()alone by forcing the bigreturn path would copy the 2-byte address instead of the struct contents.Relation to [#4131]
This looks like the same conversion described in [#4131] ("later conversion … changed the shared temporary's type into a pointer"), there triggered by the conditional operator. A fix in this loop, or handling a pointer-typed operand in RETURN for struct-returning functions by copying the pointed-to object, would likely cover both tickets.
Source inspected: a revision slightly different from the 4.6.0 binary;
wassert (result)ingenMove()was at line 2759 there, the conversion loop at lines ~1811–1819 ofSDCCopt.c.Related
Bugs: #4131