talign.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. discard """
  2. ccodeCheck: "\\i @'NIM_ALIGN(128) NI mylocal1' .*"
  3. target: "c cpp"
  4. output: "align ok"
  5. """
  6. # This is for Azure. The keyword ``alignof`` only exists in ``c++11``
  7. # and newer. On Azure gcc does not default to c++11 yet.
  8. import globalalignas
  9. var toplevel1 {.align: 32.} : int32
  10. var toplevel2 {.align: 32.} : int32
  11. var toplevel3 {.align: 32.} : int32
  12. proc foobar() =
  13. var myvar1 {.global, align(64).}: int = 123
  14. var myvar2 {.global, align(64).}: int = 123
  15. var myvar3 {.global, align(64).}: int = 123
  16. doAssert (cast[uint](addr(myglobal1)) and 127) == 0
  17. doAssert (cast[uint](addr(myglobal2)) and 127) == 0
  18. doAssert (cast[uint](addr(myglobal3)) and 127) == 0
  19. doAssert (cast[uint](addr(myvar1)) and 63) == 0
  20. doAssert (cast[uint](addr(myvar2)) and 63) == 0
  21. doAssert (cast[uint](addr(myvar3)) and 63) == 0
  22. doAssert (cast[uint](addr(toplevel1)) and 31) == 0
  23. doAssert (cast[uint](addr(toplevel2)) and 31) == 0
  24. doAssert (cast[uint](addr(toplevel3)) and 31) == 0
  25. # test multiple align expressions
  26. var mylocal1 {.align(128), align(32).}: int = 123
  27. var mylocal2 {.align(128), align(32).}: int = 123
  28. var mylocal3 {.align(32), align(128).}: int = 123
  29. doAssert (cast[uint](addr(mylocal1)) and 127) == 0
  30. doAssert (cast[uint](addr(mylocal2)) and 127) == 0
  31. doAssert (cast[uint](addr(mylocal3)) and 127) == 0
  32. echo "align ok"
  33. foobar()
  34. # bug #13122
  35. type Bug[T] = object
  36. bug{.align:64.}: T
  37. sideffect{.align:64.}: int
  38. var bug: Bug[int]
  39. doAssert sizeof(bug) == 128, "Oops my size is " & $sizeof(bug) # 16