annotate doc/manual/manual_motivation.tex @ 0:3e629dc19168

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:24:28 +0100
parents
children b47168dacba6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 %//////////////////////////////////////////////////////////////////////////////
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 % Copyright (c) 2007,2009 Daniel Adler <dadler@uni-goettingen.de>,
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 % Tassilo Philipp <tphilipp@potion-studios.com>
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 % Permission to use, copy, modify, and distribute this software for any
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 % purpose with or without fee is hereby granted, provided that the above
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 % copyright notice and this permission notice appear in all copies.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 % THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 % WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 % MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 % ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 % WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 %
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 %//////////////////////////////////////////////////////////////////////////////
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 \newpage
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 \section{Motivation}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 Interoperability between programming languages is a desirable feature
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 in complex software systems. While functions in scripting languages and virtual machine
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 languages can be called in a dynamic manner, statically compiled programming
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 languages such as C, C++ and Objective-C lack this ability.\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 The majority of systems use C function interfaces as their system-level
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 interface. Calling these (foreign) functions from within a dynamic environment
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 often involves the development of so called "glue code" on both sides,
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 the use of external tools generating communication code, or integration
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 of other middleware fulfilling that purpose. However, even inside a completely
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 static environment, without having to bridge multiple languages, it
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 can be very useful to call functions dynamically. Consider, for example, message
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 systems, dynamic function call dispatch mechanisms, without even knowing about the
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 target.\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 The \product{dyncall} library project provides a clean and portable C interface
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 to dynamically issue calls to foreign code using small call kernels written in
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 assembly. Instead of providing code for every bridged function call, which
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 unnecessarily results in code bloat, only a modest number of instructions are used
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 to invoke all the calls.\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 \subsection{Static function calls in C}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 The C programming language and its direct derivatives are limited in the way
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 function calls are handled. A C compiler regards a function call as a
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 fully qualified atomic operation. In such a statically typed environment, this
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 includes the function call's argument arity and type, as well as the
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 return type.\\
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 \subsection{Anatomy of machine-level calls}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 The process of calling a function on the machine level yields a common pattern:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 \begin{enumerate}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 \item The target function's calling convention dictates how the stack is
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 prepared, arguments are passed, results are returned and how to clean up
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 afterwards.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 \item Function call arguments are loaded in registers and on the stack according
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 to the calling convention that take alignment constraints into account.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 \item Control flow transfer from caller to callee.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 \item Process return value, if any. Some calling conventions specify that
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 the caller is responsible for cleaning up the argument stack.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 \end{enumerate}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 \newpage
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 %\paragraph{Example}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 The following example depicts a C source and the corresponding assembly for the X86 32-bit processor architecture.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 \begin{lstlisting}[label=cfuncall,caption=C function call,language=C]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 extern void f(int x, double y,float z);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 void caller()
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 {
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 f(1,2.0,3.0f);
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 }
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 \end{lstlisting}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 \begin{lstlisting}[label=x86asm,caption=Assembly X86 32-bit function call,language={[x86masm]Assembler}]
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 .global f ; external symbol 'f'
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 caller:
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 push 40400000H ; 3.0f (32 bit float)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 ; 2.0 (64 bit float)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 push 40000000H ; low DWORD
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 push 0H ; high DWORD
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 push 1H ; 1 (32 bit integer)
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 call f ; call 'f'
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 add esp, 16 ; cleanup stack
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 \end{lstlisting}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 \begin{comment}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 Due to the statically compiled nature of the
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 language itself, the abstraction to the underlying hardware machine
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 These languages make an abstraction to the machine in a way, where a function
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 call is an atomic operation that has to be fully qualified at compilation time.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 This library follows the approach to abstract the construction of a function call
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 and provides a small and clean implementation that is extendable by ports to
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 compilers, operating-systems and processor architectures.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106 The library solely uses a small kernel function to perform the actual call and
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 does not require just-in-time code generation.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 General-purpose programming languages such as C\footnote{and derived programming
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 languages such as C++ and Objective-C} are powerful statically compiled
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 programming languages that allow to implement low-level tasks. They abstract
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 the underlying hardware to a degree where one is allowed to write functions
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 that implement algorithms. At the same time, this
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 Although C is very powerful and a portable language to implement time-critical
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 and performance greedy application - it is limited in the way it handles calls
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 to functions.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 General-purpose programming languages such as C and C++ are limited in the way
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 function calls are handled. These languages make an abstraction to the
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 underlying hardware architecture, so that writing algorithms can be done in a
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 portable way. While C is quite flexible in case of pointer arithmetics and
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 I/O in main memory, the flexibility ends at the function call.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 In contrast to implementing algorithms, the function call in C is something
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 that is black-box to the language.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 One can either fully bind and call a function at once or none at all.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 construct a half-baked function-call without providing C code that performs a particular desired function call
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 in regard to arity, argument type list, return type and calling convention.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 The compiler requires exact information about the desired calling convention,
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 arity, argument- and return types.
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 \end{comment}
3e629dc19168 initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135