| 1 |
#include "common_header.h" |
| 2 |
#ifdef PYPY_STANDALONE |
| 3 |
#include "structdef.h" |
| 4 |
#include "forwarddecl.h" |
| 5 |
#include "preimpl.h" |
| 6 |
#include <src/entrypoint.h> |
| 7 |
#include <src/commondefs.h> |
| 8 |
#include <src/mem.h> |
| 9 |
#include <src/instrument.h> |
| 10 |
#include <src/rtyper.h> |
| 11 |
#include <src/exception.h> |
| 12 |
#include <src/debug_traceback.h> |
| 13 |
#include <src/asm.h> |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
|
| 19 |
#if defined(MS_WINDOWS) |
| 20 |
# include <stdio.h> |
| 21 |
# include <fcntl.h> |
| 22 |
# include <io.h> |
| 23 |
# include <Windows.h> |
| 24 |
typedef unsigned short ARGV_T; |
| 25 |
#else |
| 26 |
typedef char ARGV_T; |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef RPY_WITH_GIL |
| 30 |
# include <src/thread.h> |
| 31 |
# include <src/threadlocal.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef RPY_REVERSE_DEBUGGER |
| 35 |
# include <src-revdb/revdb_include.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
static char already_initialized_non_threadsafe; |
| 39 |
static void mark_initialized_now() { already_initialized_non_threadsafe = 1; } |
| 40 |
|
| 41 |
RPY_EXPORTED |
| 42 |
int rpython_startup_code(void) |
| 43 |
{ |
| 44 |
if (already_initialized_non_threadsafe) |
| 45 |
return 67; |
| 46 |
|
| 47 |
#ifdef RPY_WITH_GIL |
| 48 |
RPython_ThreadLocals_ProgramInit(); |
| 49 |
RPyGilAcquire(); |
| 50 |
#endif |
| 51 |
RPython_StartupCode(); |
| 52 |
mark_initialized_now(); |
| 53 |
#ifdef RPY_WITH_GIL |
| 54 |
RPyGilRelease(); |
| 55 |
#endif |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
RPY_EXTERN |
| 61 |
int pypy_main_function(int argc, ARGV_T *argv[]) |
| 62 |
{ |
| 63 |
char *errmsg; |
| 64 |
int i, exitcode; |
| 65 |
|
| 66 |
#ifdef MS_WINDOWS |
| 67 |
_setmode(0, _O_BINARY); |
| 68 |
_setmode(1, _O_BINARY); |
| 69 |
_setmode(2, _O_BINARY); |
| 70 |
#endif |
| 71 |
|
| 72 |
#ifdef RPY_WITH_GIL |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
RPython_ThreadLocals_ProgramInit(); |
| 78 |
RPyGilAcquire(); |
| 79 |
#endif |
| 80 |
|
| 81 |
instrument_setup(); |
| 82 |
|
| 83 |
#ifdef RPY_REVERSE_DEBUGGER |
| 84 |
#ifdef MS_WINDOWS |
| 85 |
errmsg = "revdb not supported on windows" |
| 86 |
goto error; |
| 87 |
#endif |
| 88 |
rpy_reverse_db_setup(&argc, &argv); |
| 89 |
#endif |
| 90 |
|
| 91 |
#ifdef MS_WINDOWS |
| 92 |
|
| 93 |
char **converted_argv = malloc(sizeof(char*) * (argc + 1)); |
| 94 |
converted_argv[argc] = NULL; |
| 95 |
for (int i=0; i<argc; i++) { |
| 96 |
int wlen = wcslen(argv[i]); |
| 97 |
if (wlen < 1) { |
| 98 |
converted_argv[i] = malloc(1); |
| 99 |
converted_argv[i][0] = 0; |
| 100 |
continue; |
| 101 |
} |
| 102 |
int lchar = WideCharToMultiByte(CP_UTF8, 0, argv[i], wlen, NULL, 0, NULL, NULL); |
| 103 |
if (lchar == 0) { |
| 104 |
|
| 105 |
errmsg = "failed to convert command line arguments to UTF-8"; |
| 106 |
goto error; |
| 107 |
} |
| 108 |
converted_argv[i] = malloc(lchar + 1); |
| 109 |
|
| 110 |
converted_argv[i][0] = 0; |
| 111 |
int lchar2 = WideCharToMultiByte(CP_UTF8, 0, argv[i], wlen, |
| 112 |
converted_argv[i], lchar, NULL, NULL); |
| 113 |
if ((lchar2 != lchar) || (lchar == 0)) { |
| 114 |
errmsg = "failed to convert command line arguments to UTF-8"; |
| 115 |
goto error; |
| 116 |
} |
| 117 |
converted_argv[i][lchar] = 0; |
| 118 |
} |
| 119 |
#else |
| 120 |
|
| 121 |
if (sizeof(void*) != SIZEOF_LONG) { |
| 122 |
errmsg = "only support platforms where sizeof(void*) == sizeof(long)," |
| 123 |
" for now"; |
| 124 |
goto error; |
| 125 |
} |
| 126 |
char **converted_argv = argv; |
| 127 |
#endif |
| 128 |
|
| 129 |
RPython_StartupCode(); |
| 130 |
mark_initialized_now(); |
| 131 |
|
| 132 |
#ifndef RPY_REVERSE_DEBUGGER |
| 133 |
exitcode = STANDALONE_ENTRY_POINT(argc, converted_argv); |
| 134 |
#else |
| 135 |
exitcode = rpy_reverse_db_main(STANDALONE_ENTRY_POINT, argc, argv); |
| 136 |
#endif |
| 137 |
|
| 138 |
pypy_debug_alloc_results(); |
| 139 |
|
| 140 |
if (RPyExceptionOccurred()) { |
| 141 |
|
| 142 |
pypy_debug_catch_fatal_exception(); |
| 143 |
} |
| 144 |
|
| 145 |
pypy_malloc_counters_results(); |
| 146 |
pypy_print_field_stats(); |
| 147 |
|
| 148 |
#ifdef RPY_WITH_GIL |
| 149 |
RPyGilRelease(); |
| 150 |
#endif |
| 151 |
|
| 152 |
return exitcode; |
| 153 |
|
| 154 |
memory_out: |
| 155 |
errmsg = "out of memory"; |
| 156 |
error: |
| 157 |
fprintf(stderr, "Fatal error during initialization: %s\n", errmsg); |
| 158 |
abort(); |
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
int PYPY_MAIN_FUNCTION(int argc, ARGV_T *argv[]) |
| 163 |
{ |
| 164 |
#ifdef PYPY_X86_CHECK_SSE2_DEFINED |
| 165 |
pypy_x86_check_sse2(); |
| 166 |
#endif |
| 167 |
return pypy_main_function(argc, argv); |
| 168 |
} |
| 169 |
|
| 170 |
#endif |