c2lang / c2compiler

the c2 programming language
c2lang.org
Apache License 2.0
703 stars 49 forks source link

Segmentation in generating C code (macOS, debian WSL) #111

Closed luarockr closed 4 years ago

luarockr commented 4 years ago

I tried porting a simple dhrystone from c to c2 and got a segmentation fault 11 during the step generating C code (output with c2c -v), on macOS as well as debian under WSL. c2 Source file attached.

gdb output (macOS): Thread 2 received signal SIGSEGV, Segmentation fault. 0x00000001000c09c7 in C2::CCodeGenerator::EmitDecl(C2::Decl const*, C2::StringBuilder&) () (gdb) where

0 0x00000001000c09c7 in C2::CCodeGenerator::EmitDecl(C2::Decl const*, C2::StringBuilder&) ()

1 0x00000001000c036c in C2::CCodeGenerator::EmitBinaryOperator(C2::Expr const*, C2::StringBuilder&) ()

2 0x00000001000bfce8 in C2::CCodeGenerator::EmitExpr(C2::Expr const*, C2::StringBuilder&) ()

3 0x00000001000c03d3 in C2::CCodeGenerator::EmitBinaryOperator(C2::Expr const*, C2::StringBuilder&) ()

4 0x00000001000c1aee in C2::CCodeGenerator::EmitStmt(C2::Stmt const*, unsigned int) ()

5 0x00000001000c216e in C2::CCodeGenerator::EmitForStmt(C2::Stmt const*, unsigned int) ()

6 0x00000001000bf5b2 in C2::CCodeGenerator::EmitFunction(C2::FunctionDecl const*) ()

7 0x00000001000be5fc in C2::CCodeGenerator::EmitAll() ()

8 0x00000001000bde74 in C2::CCodeGenerator::generate(bool, std::1::basic_string<char, std::__1::char_traits, std::1::allocator > const&) ()

9 0x00000001000c58b5 in C2::CGenerator::generate() ()

10 0x00000001000d284c in C2::C2Builder::generateOptionalC() ()

11 0x00000001000d0d11 in C2::C2Builder::build() ()

12 0x0000000100002958 in main ()

(gdb)

dhry.c2.txt

bvdberg commented 4 years ago

The attached dhry.c2 has one compile error: sys_time.timeval -> sys_time.Timeval. Otherwise it does crash during c-generation. The cause seemed to be that you used aliastypes (eg Array2Dim) in an ArraySubscript. (see proc8). It seemed that c2c did not resolve the aliastype correctly. I have fixed the issue and added 2 units tests to make sure it stays that way. Thx!

My output: Dhrystone time for 960000000 passes = 13.701680 This machine benchmarks at 70064400.000000 dhrystones/second = 70064.400000 Kdhry/second

luarockr commented 4 years ago

Sorry for the typo (seems was not recent version) and thanks a lot for so quickly solving this.