mewmew / uc

A compiler for the µC language.
58 stars 5 forks source link

irgen: Implicit type conversion #69

Closed mewmew closed 8 years ago

mewmew commented 8 years ago

Add support for implicit type conversion. This feature affects the following test cases.

uclang testdata/quiet/semantic/s01.c
panic: unable to create add instruction; github.com/llir/llvm/ir/instruction.NewAdd (binary.go:32): error: type mismatch between x (i32) and y (i8)

github.com/mewmew/uc/irgen.(*Module).binaryExpr(0xc4200a1cc0, 0xc42000a840, 0xc420012480, 0x8e, 0x8d)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:426 +0xbf8

testdata/quiet/semantic/s05.c

uclang testdata/quiet/semantic/s05.c
panic: unable to create add instruction; github.com/llir/llvm/ir/instruction.NewAdd (binary.go:32): error: type mismatch between x (i32) and y (i8)

github.com/mewmew/uc/irgen.(*Module).binaryExpr(0xc420081cc0, 0xc42000a6e0, 0xc4200125d0, 0x40, 0xc42008a240)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:426 +0xbf8

testdata/quiet/semantic/s06.c

uclang testdata/quiet/semantic/s06.c
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).identDef(0xc420081cc0, 0xc42000a600, 0xc420012510, 0x61cdc0, 0xc42000a7a0)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:690 +0x272

testdata/quiet/rtl/r01.c

uclang testdata/quiet/rtl/r01.c
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).identDef(0xc420081cc0, 0xc42000a820, 0xc420012690, 0x61cd40, 0xc420085480)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:690 +0x272

testdata/quiet/rtl/r03.c

uclang testdata/quiet/rtl/r03.c
panic: unable to create add instruction; github.com/llir/llvm/ir/instruction.NewAdd (binary.go:32): error: type mismatch between x (i8) and y (i32)

github.com/mewmew/uc/irgen.(*Module).binaryExpr(0xc420081cc0, 0xc42000a9a0, 0xc420012750, 0x61cdc0, 0xc42000ac80)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:426 +0xbf8

testdata/quiet/mips/m01.c

uclang testdata/quiet/mips/m01.c 
panic: unable to create mul instruction; github.com/llir/llvm/ir/instruction.NewMul (binary.go:148): error: type mismatch between x (i32) and y (i8)

github.com/mewmew/uc/irgen.(*Module).binaryExpr(0xc4200a1cc0, 0xc420068960, 0xc4200725a0, 0x0, 0xc42006a2d0)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:446 +0x3fa

testdata/noisy/simple/sim04.c

uclang testdata/noisy/simple/sim04.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420095cc0, 0xc42000b820, 0xc4200126c0, 0x61cd40, 0xc420099f20)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/simple/sim05.c

uclang testdata/noisy/simple/sim05.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc4200af000, 0xc420012810, 0x61cd40, 0xc42009df50)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/simple/sim06.c

uclang testdata/noisy/simple/sim06.c
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420095cc0, 0xc42000af80, 0xc4200127e0, 0x61cd40, 0xc420099940)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/simple/sim07.c

uclang testdata/noisy/simple/sim07.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420095cc0, 0xc42000bb80, 0xc420012a80, 0x61cd40, 0xc4200da420)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/simple/sim08.c

uclang testdata/noisy/simple/sim08.c 
panic: unable to create sub instruction; github.com/llir/llvm/ir/instruction.NewSub (binary.go:90): error: type mismatch between x (i8) and y (i32)

github.com/mewmew/uc/irgen.(*Module).binaryExpr(0xc4200a1cc0, 0xc4200dd1a0, 0xc42006ec90, 0xc42007c008, 0x57ff6a)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:436 +0x86c

testdata/noisy/simple/sim09.c

uclang testdata/noisy/simple/sim09.c
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc4200bd4a0, 0xc420013560, 0x61cd40, 0xc4200bb140)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/simple/sim10.c

uclang testdata/noisy/simple/sim10.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420095cc0, 0xc4200d72a0, 0xc420012c60, 0x61cd40, 0xc4200bb330)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/simple/sim11.c

uclang testdata/noisy/simple/sim11.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420095cc0, 0xc42000b560, 0xc420012720, 0x61cdc0, 0xc42000b6c0)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/medium/circle.c

uclang testdata/noisy/medium/circle.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc42000b680, 0xc420012660, 0x61cd40, 0xc420085fa0)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/medium/fib.c

uclang testdata/noisy/medium/fib.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc42000bea0, 0xc420012bd0, 0x61cd40, 0xc4200b2180)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/advanced/bubble.c

uclang testdata/noisy/advanced/bubble.c
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc4200d73e0, 0xc420013800, 0x61cd40, 0xc4200c1080)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/advanced/eval.c

uclang testdata/noisy/advanced/eval.c 
panic: unable to create icmp instruction; github.com/llir/llvm/ir/instruction.NewICmp (other.go:36): error: type mismatch between type of x (i8) and y (i32)

github.com/mewmew/uc/irgen.(*Module).binaryExpr(0xc420095cc0, 0xc42010a180, 0xc4200130e0, 0xc42009c028, 0xc42009c000)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:515 +0x42ad

testdata/noisy/advanced/primes.c

uclang testdata/noisy/advanced/primes.c 
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc42000b7a0, 0xc420012a50, 0x61cd40, 0xc4200c2340)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272

testdata/noisy/advanced/quick.c

uclang testdata/noisy/advanced/quick.c
panic: unable to create store instruction; github.com/llir/llvm/ir/instruction.NewStore (memory.go:111): error: type mismatch between i32 and i8

github.com/mewmew/uc/irgen.(*Module).indexExprDef(0xc420081cc0, 0xc4200dcba0, 0xc420013e90, 0x61cd40, 0xc4200a7b40)
    /home/u/Desktop/go/src/github.com/mewmew/uc/irgen/lower.go:747 +0x272
mewmew commented 8 years ago

@sangisos If you feel like it, please take a look at how we may wish to implement implicit type conversion. Hopefully we can keep add it at the right part of the irgen abstraction, so that the code is generic and doesn't have to be sprinkled across every expression generation function.

Related to this, we may wish to remove the zext (and possibly sext) instructions which are currently used to convert conditional binary expressions to the type of their arguments. This could potentially help fix #66.

For now, lets try to get it working first. So it is ok to sprinkle type conversions across each IR generation function. When we have it working and stable, we could try to find the right point to insert the abstraction, and also look closer at perhaps fixing #66.

Feel free to ping me if you wish to discuss these things, or have any thoughts regarding the implementation. I'll try to ponder myself on how we may wish to do this.

Cheers /u

sangisos commented 8 years ago

I have been reading the C99 draft and the clang sources, a typeCast function has an initial implementation in util(not yet pushed), but will be needed to be used in different ways before every use of a value.

I'm currently looking at how it is used in clangs ast context.

mewmew commented 8 years ago

I have been reading the C99 draft and the clang sources, a typeCast function has an initial implementation in util(not yet pushed), but will be needed to be used in different ways before every use of a value.

Great! I'd be happy to take a look once you push it.

I'm currently looking at how it is used in clangs ast context.

Found anything interesting? How do they do it?

mewmew commented 8 years ago

A duplicate of this issue is located at #63.

mewmew commented 8 years ago

Still left to do, implement conversion of function arguments in call expressions.

From callExpr:

    for _, arg := range callExpr.Args {
        expr := m.expr(f, arg)
        // TODO: Add cast
        args = append(args, expr)
    }
mewmew commented 8 years ago

Implicit conversion of function arguments in call expressions was implemented in commit a62d7fc. Closing this issue.