forked from dlang/druntime
/
dmain2.d
550 lines (482 loc) · 13.4 KB
/
dmain2.d
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/**
* Contains main program entry point and support routines.
*
* Copyright: Copyright Digital Mars 2000 - 2010.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Walter Bright, Sean Kelly
*/
/* Copyright Digital Mars 2000 - 2010.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module rt.dmain2;
private
{
import rt.memory;
import rt.util.console;
import rt.util.string;
import core.stdc.stddef;
import core.stdc.stdlib;
import core.stdc.string;
import core.stdc.stdio; // for printf()
}
version (Windows)
{
private import core.stdc.wchar_;
extern (Windows) alias int function() FARPROC;
extern (Windows) FARPROC GetProcAddress(void*, in char*);
extern (Windows) void* LoadLibraryA(in char*);
extern (Windows) int FreeLibrary(void*);
extern (Windows) void* LocalFree(void*);
extern (Windows) wchar_t* GetCommandLineW();
extern (Windows) wchar_t** CommandLineToArgvW(wchar_t*, int*);
extern (Windows) export int WideCharToMultiByte(uint, uint, wchar_t*, int, char*, int, char*, int);
pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW
}
version (all)
{
extern (C) Throwable.TraceInfo _d_traceContext(void* ptr = null);
extern (C) void _d_createTrace(Object *o)
{
auto t = cast(Throwable) o;
if (t !is null && t.info is null &&
cast(byte*) t !is t.classinfo.init.ptr)
{
t.info = _d_traceContext();
}
}
}
version (FreeBSD)
{
import core.stdc.fenv;
}
extern (C) void _STI_monitor_staticctor();
extern (C) void _STD_monitor_staticdtor();
extern (C) void _STI_critical_init();
extern (C) void _STD_critical_term();
extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void rt_moduleCtor();
extern (C) void rt_moduleTlsCtor();
extern (C) void rt_moduleDtor();
extern (C) void rt_moduleTlsDtor();
extern (C) void thread_joinAll();
extern (C) void rt_lifetimeInit();
// NOTE: This is to preserve compatibility with old Windows DLLs.
extern (C) void _moduleCtor()
{
rt_moduleCtor();
}
extern (C) void _moduleDtor()
{
rt_moduleDtor();
}
extern (C) void _moduleTlsCtor()
{
rt_moduleTlsCtor();
}
extern (C) void _moduleTlsDtor()
{
rt_moduleTlsDtor();
}
version (OSX)
{
// The bottom of the stack
extern (C) __gshared void* __osx_stack_end = cast(void*)0xC0000000;
}
/***********************************
* These are a temporary means of providing a GC hook for DLL use. They may be
* replaced with some other similar functionality later.
*/
extern (C)
{
void* gc_getProxy();
void gc_setProxy(void* p);
void gc_clrProxy();
alias void* function() gcGetFn;
alias void function(void*) gcSetFn;
alias void function() gcClrFn;
}
extern (C) void* rt_loadLibrary(in char[] name)
{
version (Windows)
{
// Load a DLL at runtime
char[260] temp = void;
temp[0 .. name.length] = name[];
temp[name.length] = cast(char) 0;
// BUG: LoadLibraryA() call calls rt_init(), which fails if proxy is not set!
void* ptr = LoadLibraryA(temp.ptr);
if (ptr is null)
return ptr;
gcSetFn gcSet = cast(gcSetFn) GetProcAddress(ptr, "gc_setProxy");
if (gcSet !is null)
{ // BUG: Set proxy, but too late
gcSet(gc_getProxy());
}
return ptr;
}
else version (Posix)
{
throw new Exception("rt_loadLibrary not yet implemented on Posix.");
}
}
extern (C) bool rt_unloadLibrary(void* ptr)
{
version (Windows)
{
gcClrFn gcClr = cast(gcClrFn) GetProcAddress(ptr, "gc_clrProxy");
if (gcClr !is null)
gcClr();
return FreeLibrary(ptr) != 0;
}
else version (Posix)
{
throw new Exception("rt_unloadLibrary not yet implemented on Posix.");
}
}
/***********************************
* These functions must be defined for any D program linked
* against this library.
*/
extern (C) void onAssertError(string file, size_t line);
extern (C) void onAssertErrorMsg(string file, size_t line, string msg);
extern (C) void onUnittestErrorMsg(string file, size_t line, string msg);
extern (C) void onRangeError(string file, size_t line);
extern (C) void onHiddenFuncError(Object o);
extern (C) void onSwitchError(string file, size_t line);
extern (C) bool runModuleUnitTests();
// this function is called from the utf module
//extern (C) void onUnicodeError(string msg, size_t idx);
/***********************************
* These are internal callbacks for various language errors.
*/
extern (C)
{
// Use ModuleInfo to get file name for "m" versions
void _d_assertm(ModuleInfo* m, uint line)
{
onAssertError(m.name, line);
}
void _d_assert_msg(string msg, string file, uint line)
{
onAssertErrorMsg(file, line, msg);
}
void _d_assert(string file, uint line)
{
onAssertError(file, line);
}
void _d_unittestm(ModuleInfo* m, uint line)
{
_d_unittest(m.name, line);
}
void _d_unittest_msg(string msg, string file, uint line)
{
onUnittestErrorMsg(file, line, msg);
}
void _d_unittest(string file, uint line)
{
_d_unittest_msg("unittest failure", file, line);
}
void _d_array_bounds(ModuleInfo* m, uint line)
{
onRangeError(m.name, line);
}
void _d_switch_error(ModuleInfo* m, uint line)
{
onSwitchError(m.name, line);
}
}
extern (C) void _d_hidden_func()
{
Object o;
version(D_InlineAsm_X86)
asm
{
mov o, EAX;
}
else version(D_InlineAsm_X86_64)
asm
{
mov o, RDI;
}
else
static assert(0, "unknown os");
onHiddenFuncError(o);
}
shared bool _d_isHalting = false;
extern (C) bool rt_isHalting()
{
return _d_isHalting;
}
__gshared string[] _d_args = null;
extern (C) string[] rt_args()
{
return _d_args;
}
// This variable is only ever set by a debugger on initialization so it should
// be fine to leave it as __gshared.
extern (C) __gshared bool rt_trapExceptions = true;
void _d_criticalInit()
{
version (Posix)
{
_STI_monitor_staticctor();
_STI_critical_init();
}
}
alias void delegate(Throwable) ExceptionHandler;
extern (C) bool rt_init(ExceptionHandler dg = null)
{
_d_criticalInit();
try
{
gc_init();
initStaticDataGC();
rt_lifetimeInit();
rt_moduleCtor();
rt_moduleTlsCtor();
runModuleUnitTests();
return true;
}
catch (Throwable e)
{
if (dg)
dg(e);
else
throw e; // rethrow, don't silently ignore error
}
_d_criticalTerm();
return false;
}
void _d_criticalTerm()
{
version (Posix)
{
_STD_critical_term();
_STD_monitor_staticdtor();
}
}
extern (C) bool rt_term(ExceptionHandler dg = null)
{
try
{
rt_moduleTlsDtor();
thread_joinAll();
_d_isHalting = true;
rt_moduleDtor();
gc_term();
return true;
}
catch (Throwable e)
{
if (dg)
dg(e);
}
finally
{
_d_criticalTerm();
}
return false;
}
/***********************************
* The D main() function supplied by the user's program
*/
int main(char[][] args);
/***********************************
* Substitutes for the C main() function.
* It's purpose is to wrap the call to the D main()
* function and catch any unhandled exceptions.
*/
extern (C) int main(int argc, char** argv)
{
char[][] args;
int result;
version (OSX)
{ /* OSX does not provide a way to get at the top of the
* stack, except for the magic value 0xC0000000.
* But as far as the gc is concerned, argv is at the top
* of the main thread's stack, so save the address of that.
*/
__osx_stack_end = cast(void*)&argv;
}
version (FreeBSD) version (D_InlineAsm_X86)
{
/*
* FreeBSD/i386 sets the FPU precision mode to 53 bit double.
* Make it 64 bit extended.
*/
ushort fpucw;
asm
{
fstsw fpucw;
or fpucw, 0b11_00_111111; // 11: use 64 bit extended-precision
// 111111: mask all FP exceptions
fldcw fpucw;
}
}
version (Posix)
{
_STI_monitor_staticctor();
_STI_critical_init();
}
version (Windows)
{
wchar_t* wcbuf = GetCommandLineW();
size_t wclen = wcslen(wcbuf);
int wargc = 0;
wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc);
assert(wargc == argc);
// This is required because WideCharToMultiByte requires int as input.
assert(wclen <= int.max, "wclen must not exceed int.max");
char* cargp = null;
size_t cargl = WideCharToMultiByte(65001, 0, wcbuf, cast(int)wclen, null, 0, null, 0);
cargp = cast(char*) alloca(cargl);
args = ((cast(char[]*) alloca(wargc * (char[]).sizeof)))[0 .. wargc];
for (size_t i = 0, p = 0; i < wargc; i++)
{
size_t wlen = wcslen(wargs[i]);
assert(wlen <= int.max, "wlen cannot exceed int.max");
int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], cast(int)wlen, null, 0, null, 0);
args[i] = cargp[p .. p+clen];
p += clen; assert(p <= cargl);
WideCharToMultiByte(65001, 0, &wargs[i][0], cast(int)wlen, &args[i][0], clen, null, 0);
}
LocalFree(wargs);
wargs = null;
wargc = 0;
}
else version (Posix)
{
char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof);
scope(exit) free(am);
for (size_t i = 0; i < argc; i++)
{
auto len = strlen(argv[i]);
am[i] = argv[i][0 .. len];
}
args = am[0 .. argc];
}
_d_args = cast(string[]) args;
bool trapExceptions = rt_trapExceptions;
void tryExec(scope void delegate() dg)
{
void printLocLine(Throwable t)
{
if (t.file)
{
console(t.classinfo.name)("@")(t.file)("(")(t.line)(")");
}
else
{
console(t.classinfo.name);
}
console("\n");
}
void printMsgLine(Throwable t)
{
if (t.file)
{
console(t.classinfo.name)("@")(t.file)("(")(t.line)(")");
}
else
{
console(t.classinfo.name);
}
if (t.msg)
{
console(": ")(t.msg);
}
console("\n");
}
void printInfoBlock(Throwable t)
{
if (t.info)
{
console("----------------\n");
foreach (i; t.info)
console(i)("\n");
console("----------------\n");
}
}
void print(Throwable t)
{
Throwable firstWithBypass = null;
for (; t; t = t.next)
{
printMsgLine(t);
printInfoBlock(t);
auto e = cast(Error) t;
if (e && e.bypassedException)
{
console("Bypasses ");
printLocLine(e.bypassedException);
if (firstWithBypass is null)
firstWithBypass = t;
}
}
if (firstWithBypass is null)
return;
console("=== Bypassed ===\n");
for (t = firstWithBypass; t; t = t.next)
{
auto e = cast(Error) t;
if (e && e.bypassedException)
print(e.bypassedException);
}
}
if (trapExceptions)
{
try
{
dg();
}
catch (Throwable t)
{
print(t);
result = EXIT_FAILURE;
}
}
else
{
dg();
}
}
// NOTE: The lifetime of a process is much like the lifetime of an object:
// it is initialized, then used, then destroyed. If initialization
// fails, the successive two steps are never reached. However, if
// initialization succeeds, then cleanup will occur even if the use
// step fails in some way. Here, the use phase consists of running
// the user's main function. If main terminates with an exception,
// the exception is handled and then cleanup begins. An exception
// thrown during cleanup, however, will abort the cleanup process.
void runMain()
{
result = main(args);
}
void runAll()
{
gc_init();
initStaticDataGC();
rt_lifetimeInit();
rt_moduleCtor();
rt_moduleTlsCtor();
if (runModuleUnitTests())
tryExec(&runMain);
else
result = EXIT_FAILURE;
rt_moduleTlsDtor();
thread_joinAll();
_d_isHalting = true;
rt_moduleDtor();
gc_term();
}
tryExec(&runAll);
version (Posix)
{
_STD_critical_term();
_STD_monitor_staticdtor();
}
return result;
}