nimhcr_unit.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. discard """
  2. output: '''
  3. fastcall_proc implementation #1 10
  4. 11
  5. fastcall_proc implementation #2 20
  6. 22
  7. fastcall_proc implementation #2 20
  8. 22
  9. fastcall_proc implementation #3 30
  10. 33
  11. fastcall_proc implementation #3 30
  12. 33
  13. fastcall_proc implementation #3 30
  14. 33
  15. fastcall_proc implementation #3 40
  16. 43
  17. cdecl_proc implementation #1 10
  18. 11
  19. cdecl_proc implementation #2 20
  20. 22
  21. cdecl_proc implementation #2 20
  22. 22
  23. cdecl_proc implementation #3 30
  24. 33
  25. cdecl_proc implementation #3 30
  26. 33
  27. cdecl_proc implementation #3 30
  28. 33
  29. cdecl_proc implementation #3 40
  30. 43
  31. stdcall_proc implementation #1 10
  32. 11
  33. stdcall_proc implementation #2 20
  34. 22
  35. stdcall_proc implementation #2 20
  36. 22
  37. stdcall_proc implementation #3 30
  38. 33
  39. stdcall_proc implementation #3 30
  40. 33
  41. stdcall_proc implementation #3 30
  42. 33
  43. stdcall_proc implementation #3 40
  44. 43
  45. noconv_proc implementation #1 10
  46. 11
  47. noconv_proc implementation #2 20
  48. 22
  49. noconv_proc implementation #2 20
  50. 22
  51. noconv_proc implementation #3 30
  52. 33
  53. noconv_proc implementation #3 30
  54. 33
  55. noconv_proc implementation #3 30
  56. 33
  57. noconv_proc implementation #3 40
  58. 43
  59. inline_proc implementation #1 10
  60. 11
  61. inline_proc implementation #2 20
  62. 22
  63. inline_proc implementation #2 20
  64. 22
  65. inline_proc implementation #3 30
  66. 33
  67. inline_proc implementation #3 30
  68. 33
  69. inline_proc implementation #3 30
  70. 33
  71. inline_proc implementation #3 40
  72. 43
  73. '''
  74. """
  75. import macros
  76. macro carryOutTests(callingConv: untyped): untyped =
  77. let
  78. procName = $callingConv & "_proc"
  79. globalName = $callingConv & "_global"
  80. callingConv = callingConv
  81. p1 = ident(procName & "1")
  82. p2 = ident(procName & "2")
  83. p3 = ident(procName & "3")
  84. g1 = ident(globalName & "1")
  85. g2 = ident(globalName & "2")
  86. result = quote do:
  87. var `g1`: pointer = nil
  88. if hcrRegisterGlobal("dummy_module", `globalName`, sizeof(int), nil, addr `g1`):
  89. cast[ptr int](`g1`)[] = 10
  90. var `g2`: pointer = nil
  91. if hcrRegisterGlobal("dummy_module", `globalName`, sizeof(int), nil, addr `g2`):
  92. cast[ptr int](`g2`)[] = 20
  93. doAssert `g1` == `g2` and cast[ptr int](`g1`)[] == 10
  94. type
  95. F = proc (x: int): int {.placeholder.}
  96. proc `p1`(x: int): int {.placeholder.}=
  97. echo `procName`, " implementation #1 ", x
  98. return x + 1
  99. let fp1 = cast[F](hcrRegisterProc("dummy_module", `procName`, `p1`))
  100. echo fp1(10)
  101. proc `p2`(x: int): int {.placeholder.} =
  102. echo `procName`, " implementation #2 ", x
  103. return x + 2
  104. let fp2 = cast[F](hcrRegisterProc("dummy_module", `procName`, `p2`))
  105. echo fp1(20)
  106. echo fp2(20)
  107. proc `p3`(x: int): int {.placeholder.} =
  108. echo `procName`, " implementation #3 ", x
  109. return x + 3
  110. let fp3 = cast[F](hcrRegisterProc("dummy_module", `procName`, `p3`))
  111. echo fp1(30)
  112. echo fp2(30)
  113. echo fp3(30)
  114. let fp4 = cast[F](hcrGetProc("dummy_module", `procName`))
  115. echo fp4(40)
  116. proc replacePlaceholderPragmas(n: NimNode) =
  117. if n.kind == nnkPragma:
  118. n[0] = callingConv
  119. else:
  120. for i in 0 ..< n.len:
  121. replacePlaceholderPragmas n[i]
  122. replacePlaceholderPragmas result
  123. # echo result.treeRepr
  124. hcrAddModule("dummy_module")
  125. carryOutTests fastcall
  126. carryOutTests cdecl
  127. carryOutTests stdcall
  128. carryOutTests noconv
  129. carryOutTests inline