Skip to content

Commit 08cd1d6

Browse files
authored
[mypyc] Split CPy.h into multiple C files (#9132)
Move functions from CPy.h to C files corresponding to various modules that define primitives. For example, the new file `int_ops.c` corresponds to `mypyc.primitives.int_ops`. Also group related things together in CPy.h The motivation is to clean things up by not having function implementations in a header (for non-inline functions), and to improve the organization of the code. CPy.h used to have a lot of only loosely related functionality. As a side effect, many of the functions are no longer declared as static. I left all inline functions in CPy.h to avoid regressing performance. Closes mypyc/mypyc#733.
1 parent e8e44e6 commit 08cd1d6

15 files changed

+1900
-1693
lines changed

mypyc/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from mypyc.namegen import exported_name
3838
from mypyc.options import CompilerOptions
3939
from mypyc.errors import Errors
40-
from mypyc.common import shared_lib_name
40+
from mypyc.common import RUNTIME_C_FILES, shared_lib_name
4141
from mypyc.ir.module_ir import format_modules
4242

4343
from mypyc.codegen import emitmodule
@@ -536,7 +536,7 @@ def mypycify(
536536
# compiler invocations.
537537
shared_cfilenames = []
538538
if not compiler_options.include_runtime_files:
539-
for name in ['CPy.c', 'getargs.c']:
539+
for name in RUNTIME_C_FILES:
540540
rt_file = os.path.join(build_dir, name)
541541
with open(os.path.join(include_dir(), name), encoding='utf-8') as f:
542542
write_file(rt_file, f.read())

mypyc/codegen/emitmodule.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from mypyc.irbuild.prepare import load_type_map
2424
from mypyc.irbuild.mapper import Mapper
2525
from mypyc.common import (
26-
PREFIX, TOP_LEVEL_NAME, INT_PREFIX, MODULE_PREFIX, shared_lib_name,
26+
PREFIX, TOP_LEVEL_NAME, INT_PREFIX, MODULE_PREFIX, RUNTIME_C_FILES, shared_lib_name,
2727
)
2828
from mypyc.codegen.cstring import encode_as_c_string, encode_bytes_as_c_string
2929
from mypyc.codegen.emit import EmitterContext, Emitter, HeaderDeclaration
@@ -493,8 +493,8 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
493493
# Optionally just include the runtime library c files to
494494
# reduce the number of compiler invocations needed
495495
if self.compiler_options.include_runtime_files:
496-
base_emitter.emit_line('#include "CPy.c"')
497-
base_emitter.emit_line('#include "getargs.c"')
496+
for name in RUNTIME_C_FILES:
497+
base_emitter.emit_line('#include "{}"'.format(name))
498498
base_emitter.emit_line('#include "__native{}.h"'.format(self.short_group_suffix))
499499
base_emitter.emit_line('#include "__native_internal{}.h"'.format(self.short_group_suffix))
500500
emitter = base_emitter

mypyc/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131

3232
IS_32_BIT_PLATFORM = sys.maxsize < (1 << 31) # type: Final
3333

34+
# Runtime C library files
35+
RUNTIME_C_FILES = [
36+
'init.c',
37+
'getargs.c',
38+
'int_ops.c',
39+
'list_ops.c',
40+
'dict_ops.c',
41+
'str_ops.c',
42+
'set_ops.c',
43+
'tuple_ops.c',
44+
'exc_ops.c',
45+
'misc_ops.c',
46+
'generic_ops.c',
47+
] # type: Final
48+
3449

3550
def decorator_helper_name(func_name: str) -> str:
3651
return '__mypyc_{}_decorator_helper__'.format(func_name)

0 commit comments

Comments
 (0)