Skip to content

gds_software.c4.compile

Compiler: C4Model -> GDSSpec / SystemIR.

Public Functions

Compile a C4Model into a GDSSpec.

Source code in packages/gds-software/gds_software/c4/compile.py
def compile_c4(model: C4Model) -> GDSSpec:
    """Compile a C4Model into a GDSSpec."""
    spec = GDSSpec(name=model.name, description=model.description)

    # 1. Register types
    spec.collect(C4RequestType, C4StateType)

    # 2. Register spaces
    spec.collect(C4RequestSpace, C4StateSpace)

    # 3. Register entities for stateful containers/components
    for c in model.containers:
        if c.stateful:
            spec.register_entity(_build_entity(c.name))
    for c in model.components:
        if c.stateful:
            spec.register_entity(_build_entity(c.name))

    # 4. Register blocks
    for p in model.persons:
        spec.register_block(_build_person_block(p))
    for e in model.external_systems:
        spec.register_block(_build_external_system_block(e))
    for c in model.containers:
        spec.register_block(_build_container_block(c, model))
    for c in model.components:
        spec.register_block(_build_component_block(c, model))

    # 5. Register spec wirings
    all_block_names = [b.name for b in spec.blocks.values()]
    wires: list[Wire] = []

    for rel in model.relationships:
        wires.append(
            Wire(source=rel.source, target=rel.target, space="C4 RequestSpace")
        )

    spec.register_wiring(
        SpecWiring(
            name=f"{model.name} Wiring",
            block_names=all_block_names,
            wires=wires,
            description=f"Auto-generated wiring for C4 model {model.name!r}",
        )
    )

    return spec

Compile a C4Model directly to SystemIR.

Source code in packages/gds-software/gds_software/c4/compile.py
def compile_c4_to_system(model: C4Model) -> SystemIR:
    """Compile a C4Model directly to SystemIR."""
    root = _build_composition_tree(model)
    return compile_system(model.name, root)