Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asm statement generating wrong C code with mutable objects in procs #18133

Open
rockcavera opened this issue May 31, 2021 · 4 comments
Open

asm statement generating wrong C code with mutable objects in procs #18133

rockcavera opened this issue May 31, 2021 · 4 comments
Labels

Comments

@rockcavera
Copy link
Contributor

Example

type
  UInt128 = object
    lo, hi: uint64

proc mulq(x, y: uint64, r: var UInt128) =
  asm """
    "mulq %[`y`]"
    : "=d"(`r.hi`), "=a"(`r.lo`)
    : [`x`] "a"(`x`), [`y`] "rm"(`y`)
  """

var r: UInt128

mulq(1234567890987654321'u64, 9876543210123456789'u64, r)

echo r

Current Output

nim c -d:danger teste
Hint: used config file 'D:\Nim\config\nim.cfg' [Conf]�
Hint: used config file 'D:\Nim\config\config.nims' [Conf]�
....
CC: teste.nim
C:\Users\Jose\nimcache\teste_r\@mteste.nim.c: In function 'mulq_teste_4':
C:\Users\Jose\nimcache\teste_r\@mteste.nim.c:69:13: error: 'r' is a pointer; did you mean to use '->'?
   69 |     : "=d"(r.hi), "=a"(r.lo)
      |             ^
      |             ->
C:\Users\Jose\nimcache\teste_r\@mteste.nim.c:69:25: error: 'r' is a pointer; did you mean to use '->'?
   69 |     : "=d"(r.hi), "=a"(r.lo)
      |                         ^
      |                         ->
C:\Users\Jose\nimcache\teste_r\@mteste.nim.c:68:9: error: invalid lvalue in 'asm' output 0
   68 |         asm(    "mulq %[y]"
      |         ^~~
compilation terminated due to -fmax-errors=3.
Error: execution of an external compiler program 'D:\mingw64\bin\gcc.exe -c  -w -fmax-errors=3 -mno-ms-bitfields -O3 -fno-strict-aliasing -fno-ident   -ID:\Nim\lib -Ic:\Users\Jose\Desktop -o C:\Users\Jose\nimcache\teste_r\@mteste.nim.c.o C:\Users\Jose\nimcache\teste_r\@mteste.nim.c' failed with exit code: 1

�

Expected Output

(lo: 3016209205842180997, hi: 660998118283024817)

Possible Solution

  • The Nim compiler does not add "->" to access the object's fields when passed as a pointer.

Additional Information

So it will compile correctly:

type
  UInt128 = object
    lo, hi: uint64

proc mulq(x, y: uint64, r: var UInt128) =
  asm """
    "mulq %[`y`]"
    : "=d"(`r->hi`), "=a"(`r->lo`)
    : [`x`] "a"(`x`), [`y`] "rm"(`y`)
  """

var r: UInt128

mulq(1234567890987654321'u64, 9876543210123456789'u64, r)

echo r
nim -v
Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-05-27
Copyright (c) 2006-2021 by Andreas Rumpf
�
git hash: 1e0165186bb8539cfd8aca1a7af8d01dc278bd46�
active boot switches: -d:release�
@rockcavera rockcavera changed the title asm statement generating wrong code with mutable objects in procs asm statement generating wrong C code with mutable objects in procs May 31, 2021
@rockcavera
Copy link
Contributor Author

Update

The case that used to work, using ->, now doesn't work anymore, because the compiler doesn't capture the symbol passed as an object field.

Analyzing the current compiler and the generated C code, I suppose that due to changes in the behavior of generating procedure parameter identifiers, which are now gensym, object field symbols passed by backtricks are treated as nkStrLit. This does not pass the name given to the parameter identifier.

A definitive solution would be to change the semantics of asm to the same as emit pragma, using ["""text""", identifier, """text"""].

@Araq Araq added the Easy label Mar 9, 2023
@Araq
Copy link
Member

Araq commented Mar 9, 2023

Search for semAsmOrEmit in compiler.

@rockcavera
Copy link
Contributor Author

Search for semAsmOrEmit in compiler.

I got to it these days doing the process backwards. I even made a slight hack to get it working again as before, but it's not ideal, as it would depend on the user using -> or ., depending on the case.

@Araq
Copy link
Member

Araq commented Mar 10, 2023

Shouldn't be hard, use semExpr and let the backend sort it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

2 participants