cc65 / cc65

cc65 - a freeware C compiler for 6502 based systems

Home Page:https://cc65.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

compiler does not include .dbg lines for function internally scoped variables

pm100 opened this issue · comments

For example

          int main(){
              int a=0;
              int b=42;
              printf("a+b=%d\n", a+b);
             if (b==99){
                  int c = 55;
                  printf("c=%d\n",c);
            }
      }

produces

.segment	"CODE"

.proc	_main: near

	.dbg	func, "main", "00", static, "_main"
	.dbg	sym, "a", "00", auto, -2
	.dbg	sym, "b", "00", auto, -4

.segment	"CODE"

	.dbg	line, "bug.c", 3
	ldx     #$00
	lda     #$00
	jsr     pushax
	.dbg	line, "bug.c", 4
	ldx     #$00
	lda     #$2A
	jsr     pushax
	.dbg	line, "bug.c", 5
	lda     #<(S0001)
	ldx     #>(S0001)
	jsr     pushax
	ldy     #$05
	jsr     ldaxysp
	jsr     pushax
	ldy     #$05
	jsr     ldaxysp
	jsr     tosaddax
	jsr     pushax
	ldy     #$04
	jsr     _printf
	.dbg	line, "bug.c", 6
	ldy     #$01
	jsr     ldaxysp
	cpx     #$00
	bne     L0003
	cmp     #$63
L0003:	jsr     booleq
	jeq     L0002
	.dbg	line, "bug.c", 7
	ldx     #$00
	lda     #$37
	jsr     pushax
	.dbg	line, "bug.c", 8
	lda     #<(S0002)
	ldx     #>(S0002)
	jsr     pushax
	ldy     #$03
	jsr     ldaxysp
	jsr     pushax
	ldy     #$04
	jsr     _printf
	.dbg	line, "bug.c", 9
	jsr     incsp2
	.dbg	line, "bug.c", 10
L0002:	ldx     #$00
	lda     #$00
	jsr     incsp4
	rts

	.dbg	line

no mention of 'c'.

For the simple case this is easily fixed but (and this is perhaps why this information isnt present) if I have the same name at function scope, or in another nested scope I get multiple .dbg lines mentioning the same variable name (its similar to the issue with local statics #2349).

eg, after simple fix applied with function level 'c' and 2 nested 'c' declarations I get

	.dbg	sym, "c", "00", auto, -8
	.dbg	sym, "c", "00", auto, -8
	.dbg	func, "main", "00", static, "_main"
	.dbg	sym, "a", "00", auto, -2
	.dbg	sym, "c", "00", auto, -4
	.dbg	sym, "b", "00", auto, -6

(simple fix is to add EmitDebugInfo just after EmitExternals in CompoundStatement)

I think that propagating the c source line number all the way to the dbginfo file would fix this. Thats a kind of disruptive change as it needs

  • extension of the .dbg sym statment to include a line number
  • adding that data to the .o file
  • adding that info to the csym statement

certainly doable (and can maybe used to fix the static locals issue too)