We need to generate hash tables keyed by types, so the Python scripts need to know the index used to represent types in Rust code. To enforce this, add a new gen_types.py script which generates constant definitions for the ir/types module. Also generate constants for common SIMD vector sizes.
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""
|
|
Generate sources with type info.
|
|
|
|
This generates a `types.rs` file which is included in
|
|
`libcretonne/ir/types/rs`. The file provides constant definitions for the most
|
|
commonly used types, including all of the scalar types.
|
|
|
|
This ensures that Python and Rust use the same type numbering.
|
|
"""
|
|
from __future__ import absolute_import
|
|
import srcgen
|
|
from cretonne import ValueType
|
|
|
|
|
|
def emit_type(ty, fmt):
|
|
"""
|
|
Emit a constant definition of a single value type.
|
|
"""
|
|
name = ty.name.upper()
|
|
fmt.doc_comment(ty.__doc__)
|
|
fmt.line(
|
|
'pub const {}: Type = Type({:#x});'
|
|
.format(name, ty.number))
|
|
|
|
|
|
def emit_vectors(bits, fmt):
|
|
"""
|
|
Emit definition for all vector types with `bits` total size.
|
|
"""
|
|
size = bits // 8
|
|
for ty in ValueType.all_scalars:
|
|
mb = ty.membytes
|
|
if mb == 0 or mb >= size:
|
|
continue
|
|
emit_type(ty.by(size // mb), fmt)
|
|
|
|
|
|
def emit_types(fmt):
|
|
for ty in ValueType.all_scalars:
|
|
emit_type(ty, fmt)
|
|
# Emit vector definitions for common SIMD sizes.
|
|
emit_vectors(64, fmt)
|
|
emit_vectors(128, fmt)
|
|
emit_vectors(256, fmt)
|
|
emit_vectors(512, fmt)
|
|
|
|
|
|
def generate(out_dir):
|
|
fmt = srcgen.Formatter()
|
|
emit_types(fmt)
|
|
fmt.update_file('types.rs', out_dir)
|