Skip to content

asm keyword is generating wrong output C code when --cc:tcc #12988

Closed
@rockcavera

Description

@rockcavera

asm keyword is generating wrong output C code when --cc:tcc. The generated C code should be the same for --cc:gcc|clang. Look here

Example Nim Code

var
  eax, ebx, ecx, edx: uint32

eax = 1'u32

proc cpuid() =
  when defined(gcc) or defined(clang) or defined(tcc):
    asm """
      cpuid
      : "=a"(`eax`), "=b"(`ebx`), "=c"(`ecx`), "=d"(`edx`)
      : "0"(`eax`), "2"(`ecx`)
    """

proc popcntSupported(): bool =
  if ((ecx shr 22) and 1) == 1:
    result = true

cpuid()

echo "POPCNT supported? ", popcntSupported()

Output C code with --cc:tcc

N_LIB_PRIVATE N_NIMCALL(void, cpuid__zK4UNzi7M71mrmjIVqBcSw)(void) {	__asm{
      cpuid
      : "=a"(eax__wMLBI2U9chfYkOYd1K7nHVQ), "=b"(ebx__RNTC0gK57Uh1qU2JpDrt7A), "=c"(ecx__lNgxrjyiCIFlkB4Q6yvIfg), "=d"(edx__dqO2xuySrb2WMskvBE6j1A)
      : "0"(eax__wMLBI2U9chfYkOYd1K7nHVQ), "2"(ecx__lNgxrjyiCIFlkB4Q6yvIfg)
    

}
}

Expected Output C code with --cc:tcc

N_LIB_PRIVATE N_NIMCALL(void, cpuid__zK4UNzi7M71mrmjIVqBcSw)(void) {	asm("      cpuid\n"
      : "=a"(eax__wMLBI2U9chfYkOYd1K7nHVQ), "=b"(ebx__RNTC0gK57Uh1qU2JpDrt7A), "=c"(ecx__lNgxrjyiCIFlkB4Q6yvIfg), "=d"(edx__dqO2xuySrb2WMskvBE6j1A)
      : "0"(eax__wMLBI2U9chfYkOYd1K7nHVQ), "2"(ecx__lNgxrjyiCIFlkB4Q6yvIfg)
);
}

Possible Solution

  • Use same output C code for --cc:gcc|tcc

Additional Information

  • The tcc document suggests using keyword "__ asm __", but I was able to compile with tcc using "asm" and "__asm". The C code generated by Nim is using "{...}", must be used "(...)".
  • Another error is "cpuid" outside the double quotes in the generated C code.
$ nim -v
Nim Compiler Version 1.1.1 [Windows: amd64]
Compiled at 2019-12-25
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 649bf326bfc6b4eb983403d850b1dcfc0cc854ae
active boot switches: -d:release

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions