Skip to content

Commit 4f12825

Browse files
committed
Find c++ exception handling runtime in our own _runtime.cpp instead of libstdc++.
In particular, this makes it easier to run our code on platforms where we don't know where to look to find libstdc++. setup.py knows where this is and links us to it anyways.
1 parent 9da642c commit 4f12825

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

typed_python/_runtime.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ PyObject* getRuntimeSingleton() {
5252
// Note: extern C identifiers are distinguished only up to 32 characters
5353
// nativepython_runtime_12345678901
5454
extern "C" {
55+
/******************************
56+
these are exception-handling routines. rather than trying to find the c++ standard library
57+
and linking to them in the python llvm wrapper, we can just passthrough to them directly
58+
from here, since we're definitely linked into them anyways.
59+
*******************************/
60+
61+
//forward declarations
62+
void* __cxa_allocate_exception(long unsigned int i);
63+
void __cxa_throw(void* a, void* b, void (*c)(void*));
64+
void __cxa_end_catch();
65+
void* __cxa_begin_catch(void* a);
66+
void __gxx_personality_v0();
67+
68+
void* tp_cxa_allocate_exception(int64_t i) {
69+
return __cxa_allocate_exception(i);
70+
}
71+
void tp_cxa_throw(void* a, void* b, void (*c)(void*)) {
72+
__cxa_throw(a, b, c);
73+
}
74+
void* tp_cxa_end_catch(void* a) {
75+
__cxa_end_catch();
76+
return (void*)nullptr;
77+
}
78+
void* tp_cxa_begin_catch(void* a) {
79+
return __cxa_begin_catch(a);
80+
}
81+
82+
void tp_gxx_personality_v0() {
83+
__gxx_personality_v0();
84+
}
85+
/******************************/
86+
5587
void np_compileClassDispatch(ClassDispatchTable* classDispatchTable, int slot) {
5688
PyEnsureGilAcquired getTheGil;
5789

typed_python/compiler/llvm_compiler.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from typed_python.compiler.native_function_pointer import NativeFunctionPointer
2222
from typed_python.compiler.binary_shared_object import BinarySharedObject
2323

24-
import sys
2524
import ctypes
2625
from typed_python import _types
2726

@@ -34,12 +33,6 @@
3433
target_machine = target.create_target_machine()
3534
target_machine_shared_object = target.create_target_machine(reloc='pic', codemodel='default')
3635

37-
# we need to load the appropriate libstdc++ so that we can get __cxa_begin_catch and friends
38-
if sys.platform == "darwin":
39-
ctypes.CDLL("libstdc++.dylib", mode=ctypes.RTLD_GLOBAL)
40-
else:
41-
ctypes.CDLL("libstdc++.so.6", mode=ctypes.RTLD_GLOBAL)
42-
4336
ctypes.CDLL(_types.__file__, mode=ctypes.RTLD_GLOBAL)
4437

4538

typed_python/compiler/native_ast_to_llvm.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def generate_exception_landing_pad(self, block):
565565
)
566566

567567
actual_exception = self.builder.call(
568-
self.external_function_references["__cxa_begin_catch"],
568+
self.external_function_references["tp_cxa_begin_catch"],
569569
[self.builder.extract_value(res, 0)]
570570
)
571571

@@ -579,7 +579,7 @@ def generate_exception_landing_pad(self, block):
579579
self.builder.store(result, self.exception_slot)
580580

581581
self.builder.call(
582-
self.external_function_references["__cxa_end_catch"],
582+
self.external_function_references["tp_cxa_end_catch"],
583583
[self.builder.extract_value(res, 0)]
584584
)
585585

@@ -619,7 +619,7 @@ def convert_teardown(self, teardown, justClearTags=False):
619619
def generate_exception_and_store_value(self, llvm_pointer_val):
620620
exception_ptr = self.builder.bitcast(
621621
self.builder.call(
622-
self.external_function_references["__cxa_allocate_exception"],
622+
self.external_function_references["tp_cxa_allocate_exception"],
623623
[llvmI64(pointer_size)],
624624
name="alloc_e"
625625
),
@@ -696,7 +696,7 @@ def generate_throw_expression(self, llvm_pointer_val):
696696
exception_ptr = self.generate_exception_and_store_value(llvm_pointer_val)
697697

698698
self.builder.call(
699-
self.external_function_references["__cxa_throw"],
699+
self.external_function_references["tp_cxa_throw"],
700700
[exception_ptr] + [llvmlite.ir.Constant(llvm_i8ptr, None)] * 2
701701
)
702702

@@ -1474,11 +1474,11 @@ def define(fname, output, inputs, vararg=False):
14741474
),
14751475
fname
14761476
)
1477-
define("__cxa_allocate_exception", llvm_i8ptr, [llvm_i64])
1478-
define("__cxa_throw", llvm_void, [llvm_i8ptr, llvm_i8ptr, llvm_i8ptr])
1479-
define("__cxa_end_catch", llvm_i8ptr, [llvm_i8ptr])
1480-
define("__cxa_begin_catch", llvm_i8ptr, [llvm_i8ptr])
1481-
define("__gxx_personality_v0", llvm_i32, [], vararg=True)
1477+
define("tp_cxa_allocate_exception", llvm_i8ptr, [llvm_i64])
1478+
define("tp_cxa_throw", llvm_void, [llvm_i8ptr, llvm_i8ptr, llvm_i8ptr])
1479+
define("tp_cxa_end_catch", llvm_i8ptr, [llvm_i8ptr])
1480+
define("tp_cxa_begin_catch", llvm_i8ptr, [llvm_i8ptr])
1481+
define("tp_gxx_personality_v0", llvm_void, [])
14821482

14831483

14841484
class Converter:
@@ -1607,7 +1607,7 @@ def add_functions(self, names_to_definitions):
16071607
for name in sorted(names_to_definitions):
16081608
definition = names_to_definitions.pop(name)
16091609
func = self._functions_by_name[name]
1610-
func.attributes.personality = external_function_references["__gxx_personality_v0"]
1610+
func.attributes.personality = external_function_references["tp_gxx_personality_v0"]
16111611

16121612
# for a in func.args:
16131613
# if a.type.is_pointer:

0 commit comments

Comments
 (0)