nimhcr_unit.nim 3.2 KB

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