Difference between revisions of "Runtime internals"
m (→Symbols) |
(→Symbols) |
||
Line 12: | Line 12: | ||
− | * "D main" - This is the | + | * "D main" - This is the <code>main</code> function provided by the user in nearly every D program. It is D-mangled making it distinguishable from C main above. |
<syntaxhighlight lang="D">int main(char[][] args)</syntaxhighlight> | <syntaxhighlight lang="D">int main(char[][] args)</syntaxhighlight> | ||
Revision as of 09:12, 24 May 2015
List of compiler hooks
D program startup sequence
This section provides a general overview of a typical D program's startup sequence. Each operating system and compiler implements this procedure slightly differently, but the fundamental procedure is the same.
Symbols
The startup sequence is essentially an orchestration of the following symbols:
- "C main" - In DMD and LDC C main seems to be provided by the compiler, while in GDC it is provided by the D Runtime.
extern(C) void main(int argc, char **argv)
- "D main" - This is the
main
function provided by the user in nearly every D program. It is D-mangled making it distinguishable from C main above.
int main(char[][] args)
_Dmain
is an alias provided by the compiler to refer to D main.
int _Dmain(char[][] args)
rt_init()
is the function called before D main to initialize the D Runtime. The D runtime initialization procedure is quite complex and out of scope for this document.
extern(C) int rt_init()
rt_term()
is the function called after D main to unwind the initialization of the D Runtime.
extern(C) int rt_term()
_d_run_main
orchestrates the calling sequence ofrt_init()
, D main, andrt_term()
.mainFunc
is a pointer to D main.
int _d_run_main(int argc, char **argv, void* mainFunc)
Sequence
The following pseudocode illustrates the calling sequence of the symbols above.
Every D program is started as if it were a C program. On Linux, the operating system calls _start which is defined the C Runtime. After performing C program initialization, the C Runtime calls C main just like a typical C program.
// 1. _start calls C main
extern(C) void main(int argc, char **argv)
{
// 2. C main calls _d_run_main passing program arguments and a pointer
// to D main
return _d_run_main(argc, argv, &_Dmain);
}
alias extern(C) int function(char[][] args) MainFunc;
int _d_run_main(int argc, char **argv, MainFunc mainFunc)
{
// 3. D some operating-system-specific logic to change argc and argv
// to char[][] args
char[][] args;
// 4. Do D runtime initialization
int result = EXIT_FAILURE;
if(rt_init()
{
// 5. call D main
result = mainFunc(args);
}
// 6. unwind runtime initialization
if (!rt_term())
{
result = (result == EXIT_SUCCESS) ? EXIT_FAILURE : result;
}
// 7. Return to C main
return result;
}