Skip to content

Commit

Permalink
Nim devel now requires {.exportc, dynlib.} instead of just the former
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Feb 14, 2020
1 parent b3a29ca commit c5cb9c6
Show file tree
Hide file tree
Showing 47 changed files with 141 additions and 141 deletions.
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ and so much more.
***** Nim code
#+begin_src nim
# libdpi.nim
proc hello() {.exportc.} =
proc hello() {.exportc, dynlib.} =
echo "Hello from Nim!"
#+end_src
***** SV code
Expand Down Expand Up @@ -132,7 +132,7 @@ proc handle_exception(a: cint) =
else:
raise newException(MyError, fmt"a is > 1! (value = {a})")

proc test_exception(a: cint) {.exportc.} =
proc test_exception(a: cint) {.exportc, dynlib.} =
try:
handle_exception(a)
except:
Expand Down
8 changes: 4 additions & 4 deletions _template/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ proc FROM_SV_TASK(in1, in2: cint; out1: var cint): cint {.importc, discardable.}
proc FROM_SV_FUNC(in1, in2: cint; out1: var cint) {.importc.}


proc TO_SV_TASK(in1, in2: cint; out1: var cint): cint {.exportc.} =
proc TO_SV_TASK(in1, in2: cint; out1: var cint): cint {.exportc, dynlib.} =
echo "Exported to SV task"
return 0 # tasks send a return value of 0

proc TO_SV_FUNC(in1, in2: cint; out1: var cint){.exportc.} =
proc TO_SV_FUNC(in1, in2: cint; out1: var cint){.exportc, dynlib.} =
echo "Exported to SV function"


proc TO_SV_TASK_c(in1, in2: cint; out1: var cint): cint {.exportc.} =
proc TO_SV_TASK_c(in1, in2: cint; out1: var cint): cint {.exportc, dynlib.} =
FROM_SV_TASK(in1, in2, out1)
return 0 # tasks send a return value of 0

proc TO_SV_FUNC_c(in1, in2: cint; out1: var cint){.exportc.} =
proc TO_SV_FUNC_c(in1, in2: cint; out1: var cint){.exportc, dynlib.} =
FROM_SV_FUNC(in1, in2, out1)
62 changes: 31 additions & 31 deletions amiq_dpi_c_examples/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ proc transform_char(inp: cschar): cschar =
# replacing that with the actual 8-bit signed value: -1.
return -1 - inp

proc compute_byte(i_value: cschar; resPtr: ptr cschar) {.exportc.} =
proc compute_byte(i_value: cschar; resPtr: ptr cschar) {.exportc, dynlib.} =
logInfo "dpi_c.compute_byte(): received value {i_value}"
resPtr[] = transform_char(i_value)
logInfo "dpi_c.compute_byte(): return value {resPtr[]}"

proc get_byte(i_value: cschar): cschar {.exportc.} =
proc get_byte(i_value: cschar): cschar {.exportc, dynlib.} =
logInfo "dpi_c.get_byte(): received {i_value}"
result = transform_char(i_value);
logInfo "dpi_c.get_byte(): return {result}"
Expand All @@ -30,68 +30,68 @@ proc transform_short_int(inp: cshort): cshort =
# replacing that with the actual 16-bit signed value: -1.
return -1 - inp

proc compute_shortint(i_value: cshort; resPtr: ptr cshort) {.exportc.} =
proc compute_shortint(i_value: cshort; resPtr: ptr cshort) {.exportc, dynlib.} =
logInfo "dpi_c.compute_shortint(): received {i_value}"
resPtr[] = transform_short_int(i_value)
logInfo "dpi_c.compute_shortint(): return {resPtr[]}"

proc get_shortint(i_value: cshort): cshort {.exportc.} =
proc get_shortint(i_value: cshort): cshort {.exportc, dynlib.} =
logInfo "dpi_c.get_shortint(): received {i_value}"
result = transform_short_int(i_value)
logInfo "dpi_c.get_shortint(): return {result}"

## int
proc transform_int(inp: cint): cint = cint(inp/23.cint)

proc compute_int(i_value: cint; resPtr: ptr cint) {.exportc.} =
proc compute_int(i_value: cint; resPtr: ptr cint) {.exportc, dynlib.} =
logInfo "dpi_c.compute_int(): received {i_value}"
resPtr[] = transform_int(i_value)
logInfo "dpi_c.compute_int(): return {resPtr[]}"

proc get_int(i_value: cint): cint {.exportc.} =
proc get_int(i_value: cint): cint {.exportc, dynlib.} =
logInfo "dpi_c.get_int(): received {i_value}"
result = transform_int(i_value)
logInfo "dpi_c.get_int(): return {result}"

## longint
proc transform_long_int(inp: clonglong): clonglong = clonglong(inp.float/123)

proc compute_longint(i_value: clonglong; resPtr: ptr clonglong) {.exportc.} =
proc compute_longint(i_value: clonglong; resPtr: ptr clonglong) {.exportc, dynlib.} =
logInfo "dpi_c.compute_longint(): received {i_value}"
resPtr[] = transform_long_int(i_value)
logInfo "dpi_c.compute_longint(): return {resPtr[]}"

proc get_longint(i_value: clonglong): clonglong {.exportc.} =
proc get_longint(i_value: clonglong): clonglong {.exportc, dynlib.} =
logInfo "dpi_c.get_longint(): received {i_value}"
result = transform_long_int(i_value)
logInfo "dpi_c.get_longint(): return {result}"

## real
proc transform_double(inp: cdouble): cdouble = inp*3

proc compute_real(i_value: cdouble; resPtr: ptr cdouble) {.exportc.} =
proc compute_real(i_value: cdouble; resPtr: ptr cdouble) {.exportc, dynlib.} =
logInfo "dpi_c.compute_real(): received {i_value}"
resPtr[] = transform_double(i_value)
logInfo "dpi_c.compute_real(): return {resPtr[]}"

proc get_real(i_value: cdouble): cdouble {.exportc.} =
proc get_real(i_value: cdouble): cdouble {.exportc, dynlib.} =
logInfo "dpi_c.get_real(): received {i_value}"
result = transform_double(i_value)
logInfo "dpi_c.get_real(): return {result}"

## string
proc compute_string(i_value: cstring; resPtr: ptr cstring) {.exportc.} =
proc compute_string(i_value: cstring; resPtr: ptr cstring) {.exportc, dynlib.} =
logInfo "dpi_c.compute_string(): received {i_value}"
resPtr[] = "DEAF_BEAF_DRINKS_COFFEE"
logInfo "dpi_c.compute_string(): return {resPtr[]}"

proc get_string(i_value: cstring): cstring {.exportc.} =
proc get_string(i_value: cstring): cstring {.exportc, dynlib.} =
logInfo "dpi_c.get_string(): received {i_value}"
result = "DEAF_BEAF_DRINKS_COFFEE"
logInfo "dpi_c.get_string(): return {result}"

## string array
proc compute_string_array(i_value: svOpenArrayHandle; resHandle: svOpenArrayHandle) {.exportc.} =
proc compute_string_array(i_value: svOpenArrayHandle; resHandle: svOpenArrayHandle) {.exportc, dynlib.} =
let
iValPtr = cast[ptr UncheckedArray[cstring]](svGetArrayPtr(i_value))
oValPtr = cast[ptr UncheckedArray[cstring]](svGetArrayPtr(resHandle))
Expand All @@ -107,25 +107,25 @@ proc transform_svBit(inp: svBit): svBit =
result = (not inp) and svBit(1)
# echo fmt"(not inp) && 8'b0000_0001 = {result}"

proc compute_bit(i_value: svBit; resPtr: ptr svBit) {.exportc.} =
proc compute_bit(i_value: svBit; resPtr: ptr svBit) {.exportc, dynlib.} =
logInfo "dpi_c.compute_bit(): input {i_value}"
resPtr[] = transform_svBit(i_value)
logInfo "dpi_c.compute_bit(): result {resPtr[]}"

proc get_bit(i_value: svBit): svBit {.exportc.} =
proc get_bit(i_value: svBit): svBit {.exportc, dynlib.} =
logInfo "dpi_c.get_bit(): input {i_value}"
result = transform_svBit(i_value)
logInfo "dpi_c.get_bit(): result {result}"

## bit vector
proc transform_svBitVecVal(inp: svBitVecVal): svBitVecVal = (inp shl 3) + 2

proc compute_bit_vector(iValuePtr: ptr svBitVecVal; resPtr: ptr svBitVecVal) {.exportc.} =
proc compute_bit_vector(iValuePtr: ptr svBitVecVal; resPtr: ptr svBitVecVal) {.exportc, dynlib.} =
logInfo "dpi_c.compute_bit_vector(): input {iValuePtr[]} ({iValuePtr[]:#b})"
resPtr[] = transform_svBitVecVal(iValuePtr[])
logInfo "dpi_c.compute_bit_vector(): result {resPtr[]} ({resPtr[]:#b})"

proc get_bit_vector(iValuePtr: ptr svBitVecVal): svBitVecVal {.exportc.} =
proc get_bit_vector(iValuePtr: ptr svBitVecVal): svBitVecVal {.exportc, dynlib.} =
logInfo "dpi_c.get_bit_vector(): input {iValuePtr[]} ({iValuePtr[]:#b})"
result = transform_svBitVecVal(iValuePtr[])
logInfo "dpi_c.get_bit_vector(): result {result} ({result:#b})"
Expand All @@ -149,12 +149,12 @@ proc transform_svLogic(inp: svLogic): svLogic =
of sv_x:
sv_z)

proc compute_logic(i_value: svLogic; resPtr: ptr svLogic) {.exportc.} =
proc compute_logic(i_value: svLogic; resPtr: ptr svLogic) {.exportc, dynlib.} =
logInfo "dpi_c.compute_logic(): integer value:{i_value}, input {SvLogic(i_value)}"
resPtr[] = transform_svLogic(i_value)
logInfo "dpi_c.compute_logic(): result {SvLogic(resPtr[])} <- {SvLogic(i_value)}"

proc get_logic(i_value: svLogic): svLogic {.exportc.} =
proc get_logic(i_value: svLogic): svLogic {.exportc, dynlib.} =
logInfo "dpi_c.get_logic(): input {SvLogic(i_value)}"
result = transform_svLogic(i_value)
logInfo "dpi_c.get_logic(): result {SvLogic(result)} <- {SvLogic(i_value)}"
Expand All @@ -166,15 +166,15 @@ proc svLogicVecVal2String(svlvvPtr: ptr svLogicVecVal; asize: cint): string =
for i in countDown(asize-1, 0):
result.add($SvLogic(svGetBitselLogic(svlvvPtr, i)))

proc compute_logic_vector(iValuePtr: ptr svLogicVecVal; resPtr: ptr svLogicVecVal; asize: cint) {.exportc.} =
proc compute_logic_vector(iValuePtr: ptr svLogicVecVal; resPtr: ptr svLogicVecVal; asize: cint) {.exportc, dynlib.} =
logInfo "dpi_c.compute_logic_vector(): input {svLogicVecVal2String(iValuePtr, asize)}"
for i in 0 ..< asize:
let
bit = transform_svLogic(svGetBitselLogic(iValuePtr, i))
svPutBitselLogic(resPtr, i, bit)
logInfo "dpi_c.compute_logic_vector(): result {svLogicVecVal2String(resPtr, asize)}"

proc get_logic_vector(iValuePtr: ptr svLogicVecVal; asize: cint): ptr svLogicVecVal {.exportc.} =
proc get_logic_vector(iValuePtr: ptr svLogicVecVal; asize: cint): ptr svLogicVecVal {.exportc, dynlib.} =
logInfo "dpi_c.get_logic_vector(): input {svLogicVecVal2String(iValuePtr, asize)}"
for i in 0 ..< asize:
let
Expand All @@ -183,26 +183,26 @@ proc get_logic_vector(iValuePtr: ptr svLogicVecVal; asize: cint): ptr svLogicVec
logInfo "dpi_c.get_logic_vector(): result {svLogicVecVal2String(result, asize)}"

## reg
proc compute_reg(i_value: svLogic; resPtr: ptr svLogic) {.exportc.} =
proc compute_reg(i_value: svLogic; resPtr: ptr svLogic) {.exportc, dynlib.} =
logInfo "dpi_c.compute_reg(): integer value:{i_value}, input {SvLogic(i_value)}"
resPtr[] = transform_svLogic(i_value)
logInfo "dpi_c.compute_reg(): result {SvLogic(resPtr[])} <- {SvLogic(i_value)}"

proc get_reg(i_value: svLogic): svLogic {.exportc.} =
proc get_reg(i_value: svLogic): svLogic {.exportc, dynlib.} =
logInfo "dpi_c.get_reg(): input {SvLogic(i_value)}"
result = transform_svLogic(i_value)
logInfo "dpi_c.get_reg(): result {SvLogic(result)} <- {SvLogic(i_value)}"

## reg vector
proc compute_reg_vector(iValuePtr: ptr svLogicVecVal; resPtr: ptr svLogicVecVal; asize: cint) {.exportc.} =
proc compute_reg_vector(iValuePtr: ptr svLogicVecVal; resPtr: ptr svLogicVecVal; asize: cint) {.exportc, dynlib.} =
logInfo "dpi_c.compute_reg_vector(): input {svLogicVecVal2String(iValuePtr, asize)}"
for i in 0 ..< asize:
let
bit = transform_svLogic(svGetBitselLogic(iValuePtr, i))
svPutBitselLogic(resPtr, i, bit)
logInfo "dpi_c.compute_reg_vector(): result {svLogicVecVal2String(resPtr, asize)}"

proc get_reg_vector(iValuePtr: ptr svLogicVecVal; asize: cint): ptr svLogicVecVal {.exportc.} =
proc get_reg_vector(iValuePtr: ptr svLogicVecVal; asize: cint): ptr svLogicVecVal {.exportc, dynlib.} =
logInfo "dpi_c.get_reg_vector(): input {svLogicVecVal2String(iValuePtr, asize)}"
for i in 0 ..< asize:
let
Expand All @@ -219,17 +219,17 @@ type
Chandle = pointer # Alias Nim type "pointer" to "Chandle" just for better readability

# import "DPI-C" function void compute_chandle(output chandle result);
proc compute_chandle(chandlePtr: ptr Chandle) {.exportc.} =
proc compute_chandle(chandlePtr: ptr Chandle) {.exportc, dynlib.} =
chandlePtr[] = cast[Chandle](print_chandle)
logInfo "dpi_c.compute_chandle() {repr(chandlePtr)}"

# import "DPI-C" function chandle get_chandle();
proc get_chandle(): Chandle {.exportc.} =
proc get_chandle(): Chandle {.exportc, dynlib.} =
result = cast[Chandle](print_chandle)
logInfo "dpi_c.get_chandle() {repr(result)}"

# import "DPI-C" function void call_chandle(input chandle i_value, output int result);
proc call_chandle(inChandle: Chandle; oValuePtr: ptr cint) {.exportc.} =
proc call_chandle(inChandle: Chandle; oValuePtr: ptr cint) {.exportc, dynlib.} =
logInfo "dpi_c.call_chandle() {repr(inChandle)}"
let
pcp = cast[proc(): cint {.cdecl.}](inChandle) # {.cdecl.} enforces "C function type" vs "Nim proc type"
Expand Down Expand Up @@ -259,11 +259,11 @@ proc compute_unsized_T_array[T: SomeInteger](inDArrHandle, outDArrHandle: svOpen
# outTArrElemPtr[] = inTArrElemPtr[] + 3

## unsized int array
proc compute_unsized_int_array(inDArrHandle, outDArrHandle: svOpenArrayHandle) {.exportc.} =
proc compute_unsized_int_array(inDArrHandle, outDArrHandle: svOpenArrayHandle) {.exportc, dynlib.} =
compute_unsized_T_array[cint](inDArrHandle, outDArrHandle)

## unsized byte array
proc compute_unsized_byte_array(inDArrHandle, outDArrHandle: svOpenArrayHandle) {.exportc.} =
proc compute_unsized_byte_array(inDArrHandle, outDArrHandle: svOpenArrayHandle) {.exportc, dynlib.} =
compute_unsized_T_array[cschar](inDArrHandle, outDArrHandle)

## struct
Expand All @@ -286,5 +286,5 @@ proc transform_struct(inpPtr: ptr MyStruct): MyStruct =
result.aBitVector = transform_svBitVecVal(result.aBitVector)
logInfo "dpi_c: output struct = {result}"

proc compute_struct(iValuePtr, oValuePtr: ptr MyStruct) {.exportc.} =
proc compute_struct(iValuePtr, oValuePtr: ptr MyStruct) {.exportc, dynlib.} =
oValuePtr[] = transform_struct(iValuePtr)
12 changes: 6 additions & 6 deletions chandle/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ proc callProc[T](ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle):
return procInst(args)

## Exported Procs
proc getProcHandleInt(procEnum: NimProc): Chandle {.exportc.} =
proc getProcHandleInt(procEnum: NimProc): Chandle {.exportc, dynlib.} =
getProcHandle[cint](procEnum)

proc callProcInt(ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle): cint {.exportc.} =
proc callProcInt(ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle): cint {.exportc, dynlib.} =
callProc[cint](ch, procEnum, chArgsPtr)

proc getProcHandleFloat(procEnum: NimProc): Chandle {.exportc.} =
proc getProcHandleFloat(procEnum: NimProc): Chandle {.exportc, dynlib.} =
getProcHandle[cdouble](procEnum)

proc callProcFloat(ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle): cdouble {.exportc.} =
proc callProcFloat(ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle): cdouble {.exportc, dynlib.} =
callProc[cdouble](ch, procEnum, chArgsPtr)

proc getProcHandleString(procEnum: NimProc): Chandle {.exportc.} =
proc getProcHandleString(procEnum: NimProc): Chandle {.exportc, dynlib.} =
getProcHandle[cstring](procEnum)

proc callProcString(ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle): cstring {.exportc.} =
proc callProcString(ch: Chandle; procEnum: NimProc; chArgsPtr: svOpenArrayHandle): cstring {.exportc, dynlib.} =
callProc[cstring](ch, procEnum, chArgsPtr)
2 changes: 1 addition & 1 deletion eval_nim_in_sv/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from osproc import execCmdEx
from strutils import strip, split, removeSuffix
from strformat import fmt

proc nim_eval_string(code: cstring) {.exportc.} =
proc nim_eval_string(code: cstring) {.exportc, dynlib.} =
## Evaluate the Nim code passed as string.
let
user = if getEnv("USER")=="":
Expand Down
2 changes: 1 addition & 1 deletion export_from_class/libdpi.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
proc wrapper_function(): cint {.importc, discardable.}

proc from_nim() {.exportc.} =
proc from_nim() {.exportc, dynlib.} =
echo "I am in Nim"
wrapper_function()
2 changes: 1 addition & 1 deletion gc_crash_debug/libdpi.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
proc imported_func(){.exportc.} =
proc imported_func(){.exportc, dynlib.} =
# https://gitter.im/nim-lang/Nim?at=5dc584763f4ea333f2c5345d
when defined(gcToggle):
GC_disable()
Expand Down
2 changes: 1 addition & 1 deletion hello_from_nim/libdpi.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://github.com/nim-lang/Nim/issues/10578#issuecomment-461635978

proc hello() {.exportc.} =
proc hello() {.exportc, dynlib.} =
let
str = when defined(cpp):
"C++!"
Expand Down
2 changes: 1 addition & 1 deletion hello_from_sv/libdpi.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
proc hello_from_sv() {.importc.}

proc hello_from_nim() {.exportc.} =
proc hello_from_nim() {.exportc, dynlib.} =
hello_from_sv()
2 changes: 1 addition & 1 deletion i_am_here/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import svdpi
var
hereCounter: cint = 0

proc hereDebug(initVal: cint; realVal: cdouble): cstring {.exportc.} =
proc hereDebug(initVal: cint; realVal: cdouble): cstring {.exportc, dynlib.} =
if initVal >= 0:
hereCounter = initVal
let
Expand Down
2 changes: 1 addition & 1 deletion inp_arr_outp_nested_struct_matlab/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ proc get_mean_func_2_out(inStruct: InpStructWrapper): OutputObj =
mean_func(inp1, out1)
return out1[]

proc get_params(inStruct: ref InpStructWrapper; outStruct: ref OutputObj) {.exportc.} =
proc get_params(inStruct: ref InpStructWrapper; outStruct: ref OutputObj) {.exportc, dynlib.} =
outStruct[] = get_mean_func_2_out(inStruct[])
2 changes: 1 addition & 1 deletion inp_outp_both_nested_struct_matlab/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ proc get_mean_func_3_out(inStruct: InpStructWrapper): OutputObj =
mean_func(inputObjPtr, outputObjPtr)
return outputObjPtr[]

proc get_params(inStruct: ptr InpStructWrapper; outStruct: ptr OutputObj) {.exportc.} =
proc get_params(inStruct: ptr InpStructWrapper; outStruct: ptr OutputObj) {.exportc, dynlib.} =
# echo $inStruct[]
outStruct[] = get_mean_func_3_out(inStruct[])
2 changes: 1 addition & 1 deletion interactive_sim/libdpi.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
proc get_user_input(): cstring {.exportc.} =
proc get_user_input(): cstring {.exportc, dynlib.} =
stdout.write(" Nim> Enter string to be passed to SV: ")
return readLine(stdin)
4 changes: 2 additions & 2 deletions nim_enum_and_range/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ type

MyCustomType = range[100.cint .. 199.cint]

proc print_val1(val: Color) {.exportc.} =
proc print_val1(val: Color) {.exportc, dynlib.} =
try:
discard parseEnum[Color]($val) # Check if val is a valid Color value
echo fmt"val = {val}, numeric value = {ord(val)}"
except ValueError:
echo fmt"[Error] {ord(val)} is an invalid value for Color type"

proc print_val2(val: MyCustomType) {.exportc.} =
proc print_val2(val: MyCustomType) {.exportc, dynlib.} =
try:
doAssert val >= low(MyCustomType) and
val <= high(MyCustomType)
Expand Down
2 changes: 1 addition & 1 deletion nim_exception/libdpi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ proc handle_exception(a: cint) =
else:
raise newException(MyError, fmt"a is > 1! (value = {a})")

proc test_exception(a: cint) {.exportc.} =
proc test_exception(a: cint) {.exportc, dynlib.} =
try:
handle_exception(a)
except:
Expand Down
Loading

0 comments on commit c5cb9c6

Please sign in to comment.