v icon indicating copy to clipboard operation
v copied to clipboard

cgen: Erroneous generation of if as template argument

Open streaksu opened this issue 4 years ago • 1 comments

V version: 0.2.2 1178427 OS: Arch Linux

What did you do? Called this template

pub fn port_out<T>(port u16, value T) {
    asm volatile amd64 {
        out port, value
        ;
        ; a (value)
          Nd (port)
        ; memory
    }
}

In a statement as such

kio.port_out<byte>(dev.device_port, if dev.is_master { 0xa0 } else { 0xb0 })

Instead of declaring the value in a different variable as such

a := if dev.is_master { 0xa0 } else { 0xb0 }
kio.port_out<byte>(dev.device_port, byte(a))

What did you expect to see? No errors in the C codegen.

What did you see instead? The compiler generates the following erroneous code

    T _t2; /* if prepend */
    if (dev->is_master) {
        _t2 = 0xa0;
    } else {
        _t2 = 0xb0;
    }
    x86__kio__port_out_T_byte(dev->device_port,  _t2);

Which makes the C compiler fail.

streaksu avatar Aug 09 '21 12:08 streaksu

Does not reproduces on latest V:

PS D:\Games\Proekti\V\interactions> type test2.v
pub fn port_out[T](port u16, value T) {
        asm volatile amd64 {
                out port, value
                ; ; a (value)
                  Nd (port)
                ; memory
        }
}

is_master := true
port_out[u8](1, if is_master { 0xa0 } else { 0xb0 })
PS D:\Games\Proekti\V\interactions> v version
V 0.4.4 2a68e2b
PS D:\Games\Proekti\V\interactions> v run test2.v
Unhandled Exception 0xC0000096
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN3B37N2ZMVM1MT5EQT8WJKZ.tmp.c:6903: at print_backtrace_skipping_top_frames_tcc: Backtrace
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN3B37N2ZMVM1MT5EQT8WJKZ.tmp.c:6870: by print_backtrace_skipping_top_frames
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN3B37N2ZMVM1MT5EQT8WJKZ.tmp.c:7691: by unhandled_exception_handler
7ffecb7bc5fa : by ???
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN3B37N2ZMVM1MT5EQT8WJKZ.tmp.c:13000: at main__port_out_T_u8: RUNTIME ERROR: caught exception c0000096
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN3B37N2ZMVM1MT5EQT8WJKZ.tmp.c:13007: by main__main
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN3B37N2ZMVM1MT5EQT8WJKZ.tmp.c:13043: by wmain
0045ef68 : by ???
0045f0cb : by ???
7ffecb0953e0 : by ???

MCausc78 avatar Jan 26 '24 16:01 MCausc78