| 1 |
"""Utilities for writing StencilGroups out to a C header file.""" |
| 2 |
|
| 3 |
import typing |
| 4 |
import math |
| 5 |
|
| 6 |
import _stencils |
| 7 |
|
| 8 |
|
| 9 |
def _dump_footer( |
| 10 |
groups: dict[str, _stencils.StencilGroup], symbols: dict[str, int] |
| 11 |
) -> typing.Iterator[str]: |
| 12 |
symbol_mask_size = max(math.ceil(len(symbols) / 32), 1) |
| 13 |
yield f'static_assert(SYMBOL_MASK_WORDS >= {symbol_mask_size}, "SYMBOL_MASK_WORDS too small");' |
| 14 |
yield "" |
| 15 |
yield "typedef struct {" |
| 16 |
yield " void (*emit)(" |
| 17 |
yield " unsigned char *code, unsigned char *data, _PyExecutorObject *executor," |
| 18 |
yield " const _PyUOpInstruction *instruction, jit_state *state);" |
| 19 |
yield " size_t code_size;" |
| 20 |
yield " size_t data_size;" |
| 21 |
yield " symbol_mask trampoline_mask;" |
| 22 |
yield "} StencilGroup;" |
| 23 |
yield "" |
| 24 |
yield f"static const StencilGroup shim = {groups['shim'].as_c('shim')};" |
| 25 |
yield "" |
| 26 |
yield "static const StencilGroup stencil_groups[MAX_UOP_ID + 1] = {" |
| 27 |
for opname, group in sorted(groups.items()): |
| 28 |
if opname == "shim": |
| 29 |
continue |
| 30 |
yield f" [{opname}] = {group.as_c(opname)}," |
| 31 |
yield "};" |
| 32 |
yield "" |
| 33 |
yield f"static const void * const symbols_map[{max(len(symbols), 1)}] = {{" |
| 34 |
if symbols: |
| 35 |
for symbol, ordinal in symbols.items(): |
| 36 |
yield f" [{ordinal}] = &{symbol}," |
| 37 |
else: |
| 38 |
yield " 0" |
| 39 |
yield "};" |
| 40 |
|
| 41 |
|
| 42 |
def _dump_stencil(opname: str, group: _stencils.StencilGroup) -> typing.Iterator[str]: |
| 43 |
yield "void" |
| 44 |
yield f"emit_{opname}(" |
| 45 |
yield " unsigned char *code, unsigned char *data, _PyExecutorObject *executor," |
| 46 |
yield " const _PyUOpInstruction *instruction, jit_state *state)" |
| 47 |
yield "{" |
| 48 |
for part, stencil in [("code", group.code), ("data", group.data)]: |
| 49 |
for line in stencil.disassembly: |
| 50 |
yield f" // {line}" |
| 51 |
stripped = stencil.body.rstrip(b"\x00") |
| 52 |
if stripped: |
| 53 |
yield f" const unsigned char {part}_body[{len(stencil.body)}] = {{" |
| 54 |
for i in range(0, len(stripped), 8): |
| 55 |
row = " ".join(f"{byte:#04x}," for byte in stripped[i : i + 8]) |
| 56 |
yield f" {row}" |
| 57 |
yield " };" |
| 58 |
# Data is written first (so relaxations in the code work properly): |
| 59 |
for part, stencil in [("data", group.data), ("code", group.code)]: |
| 60 |
if stencil.body.rstrip(b"\x00"): |
| 61 |
yield f" memcpy({part}, {part}_body, sizeof({part}_body));" |
| 62 |
stencil.holes.sort(key=lambda hole: hole.offset) |
| 63 |
for hole in stencil.holes: |
| 64 |
yield f" {hole.as_c(part)}" |
| 65 |
yield "}" |
| 66 |
yield "" |
| 67 |
|
| 68 |
|
| 69 |
def dump( |
| 70 |
groups: dict[str, _stencils.StencilGroup], symbols: dict[str, int] |
| 71 |
) -> typing.Iterator[str]: |
| 72 |
"""Yield a JIT compiler line-by-line as a C header file.""" |
| 73 |
for opname, group in groups.items(): |
| 74 |
yield from _dump_stencil(opname, group) |
| 75 |
yield from _dump_footer(groups, symbols) |