cznic / ccir

github.com/cznic/ccir has moved to modernc.org/ccir
https://godoc.org/modernc.org/ccir
BSD 3-Clause "New" or "Revised" License
29 stars 2 forks source link

typedef function paramater miscompiled #7

Closed cznic closed 7 years ago

cznic commented 7 years ago

This works

int foo()
{
    return 42;
}

int bar(int (*f) ())
{
    return f();
}

int main()
{
    __builtin_printf("%i\n", bar(foo));
}
$ 99c 99c_issue9.c && ./a.out
42
$ 

This does not

typedef int ft();

int foo()
{
    return 42;
}

int bar(ft f)
{
    return f();
}

int main()
{
    __builtin_printf("%i\n", bar(foo));
}
$ 99c 99c_issue9-2.c && ./a.out
99run: PANIC: runtime error: index out of range
rtdsc 0x1c, last instruction fetch: 0x0002c 99c_issue9-2.c:10:1: bar

goroutine 1 [running]:
runtime/debug.Stack(0xc4201f6000, 0x660340, 0xc4201b6c70)
    /home/jnml/go/src/runtime/debug/stack.go:24 +0xa7
github.com/cznic/virtual.(*cpu).run.func1(0xc42008fc88, 0xc4201f6000)
    /home/jnml/src/github.com/cznic/virtual/cpu.go:226 +0xc8
panic(0x55a2c0, 0x674690)
    /home/jnml/go/src/runtime/panic.go:491 +0x283
github.com/cznic/virtual.(*cpu).run(0xc4201f6000, 0x2, 0x0, 0x0, 0x0)
    /home/jnml/src/github.com/cznic/virtual/cpu.go:242 +0x7cf8
github.com/cznic/virtual.New(0xc4201d2000, 0xc42000a090, 0x1, 0x1, 0x660600, 0xc42000c010, 0x660640, 0xc42000c018, 0x660640, 0xc42000c020, ...)
    /home/jnml/src/github.com/cznic/virtual/virtual.go:133 +0x3ad
github.com/cznic/virtual.Exec(0xc4201d2000, 0xc42000a090, 0x1, 0x1, 0x660600, 0xc42000c010, 0x660640, 0xc42000c018, 0x660640, 0xc42000c020, ...)
    /home/jnml/src/github.com/cznic/virtual/virtual.go:144 +0x113
main.main()
    /home/jnml/src/github.com/cznic/99c/99run/main.go:56 +0x3a0

$ 

Ref: cznic/99c#9.