File: manual_dyncall_api.tex

package info (click to toggle)
nqp 2014.07-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,596 kB
  • ctags: 7,993
  • sloc: ansic: 22,689; java: 20,240; cpp: 4,956; asm: 3,976; perl: 950; python: 267; sh: 245; makefile: 14
file content (289 lines) | stat: -rw-r--r-- 10,725 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
%//////////////////////////////////////////////////////////////////////////////
%
% Copyright (c) 2007,2010 Daniel Adler <dadler@uni-goettingen.de>, 
%                         Tassilo Philipp <tphilipp@potion-studios.com>
%
% Permission to use, copy, modify, and distribute this software for any
% purpose with or without fee is hereby granted, provided that the above
% copyright notice and this permission notice appear in all copies.
%
% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
%//////////////////////////////////////////////////////////////////////////////

\newpage
\section{\emph{Dyncall} C library API}

The library provides low-level functionality to make foreign function calls
from different run-time environments. The flexibility is constrained by the
set of supported types.

\paragraph{C interface style conventions}

This manual and the \product{dyncall} library's C interface {\tt "dyncall.h"}
use the following C source code style.


\begin{table}[h]
\begin{center}
\begin{tabular*}{0.8\textwidth}{llll}
\hline
Subject    & C symbol & Details & Example \\
\hline  
Types      
  & {\tt DC\group{type name}}      
  & lower-case & \capi{DCint}, \capi{DCfloat}, \capi{DClong}, \ldots\\
Structures 
  & {\tt DC\group{structure name}} 
  & camel-case 
  & \capi{DCCallVM}\\
Functions  & {\tt dc\group{function name}}  & camel-case & \capi{dcNewCallVM}, \capi{dcArgInt}, \ldots\\
\hline
\end{tabular*}
\caption{C interface conventions}
\label{sourecode}
\end{center}
\end{table}

\subsection{Supported C/C++ argument and return types}

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{ll}
\hline
Type alias & C/C++ data type\\
\hline
DCbool     & \_Bool, bool\\
DCchar     & char\\
DCshort    & short\\
DCint      & int\\
DClong     & long\\
DClonglong & long long\\
DCfloat    & float\\
DCdouble   & double\\
DCpointer  & void*\\
DCvoid     & void\\
\hline
\end{tabular*}
\caption{Supported C/C++ argument and return types}
\label{types}
\end{center}
\end{table}

\pagebreak

\subsection{Call Virtual Machine - CallVM}

This \emph{CallVM} is the main entry to the functionality of the library.

\paragraph{Types}

\begin{lstlisting}[language=c]
typedef void DCCallVM; /* abstract handle */
\end{lstlisting}

\paragraph{Details}
The \emph{CallVM} is a state machine that manages all aspects of a function 
call from configuration, argument passing up the actual function call on
the processor.

\subsection{Allocation}

\paragraph{Functions}

\begin{lstlisting}[language=c]
DCCallVM* dcNewCallVM (DCsize size);
void      dcFree(DCCallVM* vm);
\end{lstlisting}

\lstinline{dcNewCallVM} creates a new \emph{CallVM} object, where
\lstinline{size} specifies the max size of the internal stack that will be
allocated and used to bind arguments to. Use \lstinline{dcFree} to
destroy the \emph{CallVM} object.\\
\\
This will allocate memory using the system allocators or custom ones provided
custom \capi{dcAllocMem} and \capi{dcFreeMem} macros are defined to override the
default behaviour. See \capi{dyncall\_alloc.h} for defails.


\subsection{Error Reporting}

\paragraph{Function}

\begin{lstlisting}[language=c]
DCint dcGetError(DCCallVM* vm);
\end{lstlisting}

Returns the most recent error state code out of the following:

\paragraph{Errors}

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{ll}
\hline
Constant & Description\\
\hline
\lstinline@DC_ERROR_NONE@             & No error occured. \\
\lstinline@DC_ERROR_UNSUPPORTED_MODE@ & Unsupported mode, caused by \lstinline@dcMode()@ \\
\hline
\end{tabular*}
\caption{CallVM calling convention modes}
\label{functioncalls}
\end{center}
\end{table}

\pagebreak

\subsection{Configuration}

\paragraph{Function}

\begin{lstlisting}[language=c]
void dcMode (DCCallVM* vm, DCint mode);
\end{lstlisting}

Sets the calling convention to use. Note that some mode/platform combination
don't make any sense (e.g. using a PowerPC calling convention on a MIPS
platform) and are silently ignored.

\paragraph{Modes}

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{ll}
\hline
Constant & Description\\
\hline
\lstinline@DC_CALL_C_DEFAULT@            & C default function call for current platform\\
\lstinline@DC_CALL_C_ELLIPSIS@           & C ellipsis function call (named arguments (before '...'))\\
\lstinline@DC_CALL_C_ELLIPSIS_VARARGS@   & C ellipsis function call (variable/unnamed arguments (after '...'))\\
\lstinline@DC_CALL_C_X86_CDECL@          & C x86 platforms standard call\\
\lstinline@DC_CALL_C_X86_WIN32_STD@      & C x86 Windows standard call\\
\lstinline@DC_CALL_C_X86_WIN32_FAST_MS@  & C x86 Windows Microsoft fast call\\
\lstinline@DC_CALL_C_X86_WIN32_FAST_GNU@ & C x86 Windows GCC fast call\\
\lstinline@DC_CALL_C_X86_WIN32_THIS_MS@  & C x86 Windows Microsoft this call\\
\lstinline@DC_CALL_C_X86_WIN32_THIS_GNU@ & C x86 Windows GCC this call\\
\lstinline@DC_CALL_C_X86_PLAN9@          & C x86 Plan9 call\\
\lstinline@DC_CALL_C_X64_WIN64@          & C x64 Windows standard call\\
\lstinline@DC_CALL_C_X64_SYSV@           & C x64 System V standard call\\
\lstinline@DC_CALL_C_PPC32_DARWIN@       & C ppc32 Mac OS X standard call\\
\lstinline@DC_CALL_C_PPC32_OSX@          & alias for DC\_CALL\_C\_PPC32\_DARWIN\\
\lstinline@DC_CALL_C_PPC32_SYSV@         & C ppc32 SystemV standard call\\
\lstinline@DC_CALL_C_PPC32_LINUX@        & alias for DC\_CALL\_C\_PPC32\_SYSV\\
\lstinline@DC_CALL_C_ARM_ARM@            & C arm call (arm mode)\\
\lstinline@DC_CALL_C_ARM_THUMB@          & C arm call (thumb mode)\\
\lstinline@DC_CALL_C_ARM_ARM_EABI@       & C arm eabi call (arm mode)\\
\lstinline@DC_CALL_C_ARM_THUMB_EABI@     & C arm eabi call (thumb mode)\\
\lstinline@DC_CALL_C_ARM_ARMHF@          & C arm call (arm hardfloat - e.g. raspberry pi)\\
\lstinline@DC_CALL_C_MIPS32_EABI@        & C mips32 eabi call\\
\lstinline@DC_CALL_C_MIPS32_PSPSDK@      & alias for DC\_CALL\_C\_MIPS32\_EABI (deprecated)\\
\lstinline@DC_CALL_C_MIPS32_O32@         & C mips32 o32 call\\
\lstinline@DC_CALL_C_MIPS64_N64@         & C mips64 n64 call\\
\lstinline@DC_CALL_C_MIPS64_N32@         & C mips64 n32 call\\
\lstinline@DC_CALL_C_SPARC32@            & C sparc32 call\\
\lstinline@DC_CALL_C_SPARC64@            & C sparc64 call\\
\lstinline@DC_CALL_SYS_DEFAULT@          & C default syscall for current platform\\
\lstinline@DC_CALL_SYS_X86_INT80H_BSD@   & C syscall for x86 BSD platforms\\
\lstinline@DC_CALL_SYS_X86_INT80H_LINUX@ & C syscall for x86 Linux\\
\hline
\end{tabular*}
\caption{CallVM calling convention modes}
\label{functioncalls}
\end{center}
\end{table}

\paragraph{Details}

\lstinline@DC_CALL_C_DEFAULT@ is the default standard C call on the target
platform. It uses the standard C calling convention.
\lstinline@DC_CALL_C_ELLIPSIS@ is used for C ellipsis calls which allow
to build up a variable argument list.
On many platforms, there is only one C calling convention. 
The X86 platform provides a rich family of different calling conventions.
\\


\subsection{Machine state reset}

\begin{lstlisting}[language=c]
void dcReset(DCCallVM* vm);
\end{lstlisting}

Resets the internal stack of arguments and prepares it for the selected mode.
This function should be called after setting the call mode (using dcMode), but
prior to binding arguments to the CallVM. Use it also when reusing a CallVM, as
arguments don't get flushed automatically after a function call invocation.\\

\subsection{Argument binding}

\paragraph{Functions}

\begin{lstlisting}[language=c]
void dcArgBool    (DCCallVM* vm, DCbool     arg);
void dcArgChar    (DCCallVM* vm, DCchar     arg);
void dcArgShort   (DCCallVM* vm, DCshort    arg);
void dcArgInt     (DCCallVM* vm, DCint      arg);
void dcArgLong    (DCCallVM* vm, DClong     arg);
void dcArgLongLong(DCCallVM* vm, DClonglong arg);
void dcArgFloat   (DCCallVM* vm, DCfloat    arg);
void dcArgDouble  (DCCallVM* vm, DCdouble   arg);
void dcArgPointer (DCCallVM* vm, DCpointer  arg);
\end{lstlisting}

\paragraph{Details}

Used to bind arguments of the named types to the CallVM object.
Arguments should be bound in \emph{left-to-right} order regarding the C
function prototype.\\

\subsection{Call invocation}

\paragraph{Functions}

\begin{lstlisting}[language=c]
DCvoid     dcCallVoid    (DCCallVM* vm, DCpointer funcptr);
DCbool     dcCallBool    (DCCallVM* vm, DCpointer funcptr);
DCchar     dcCallChar    (DCCallVM* vm, DCpointer funcptr);
DCshort    dcCallShort   (DCCallVM* vm, DCpointer funcptr);
DCint      dcCallInt     (DCCallVM* vm, DCpointer funcptr);
DClong     dcCallLong    (DCCallVM* vm, DCpointer funcptr);
DClonglong dcCallLongLong(DCCallVM* vm, DCpointer funcptr);
DCfloat    dcCallFloat   (DCCallVM* vm, DCpointer funcptr);
DCdouble   dcCallDouble  (DCCallVM* vm, DCpointer funcptr);
DCpointer  dcCallPointer (DCCallVM* vm, DCpointer funcptr);
\end{lstlisting}

\paragraph{Details}
Calls the function specified by \emph{funcptr} with the arguments bound to
the \emph{CallVM} and returns. Use the function that corresponds to the
dynamically called function's return value.\\
\\
After the invocation of the foreign function call, the argument values are
still bound and a second call using the same arguments can be issued. If you
need to clear the argument bindings, you have to reset the \emph{CallVM}.

\subsection{Formatted calls (ANSI C ellipsis interface)}

\paragraph{Functions}

\begin{lstlisting}[language=c]
void dcCallF (DCCallVM* vm, DCValue* result, DCpointer funcptr, 
              const DCsigchar* signature, ...);
void dcVCallF(DCCallVM* vm, DCValue* result, DCpointer funcptr, 
              const DCsigchar* signature, va_list args);
\end{lstlisting}

\paragraph{Details}

These functions can be used to operate \product{dyncall} via a printf-style
functional interface, using a signature string encoding the argument types and 
return type. The return value will be stored in what \lstinline{result} points to..
For more information about the signature format, refer to \ref{sigchar}.