Beispiel #1
0
def finish_regroup(hlir):
    """At this point, all nodes have been moved from hlir.objects.vec
    into separate attributes of hlir.
    Remove the unnecessary node."""
    if len(hlir.objects) != 0:
        addError(
            'cleaning up',
            f'{len(hlir.objects)} unexpected nodes found in hlir.objects')

    hlir.remove_attr('objects')
Beispiel #2
0
def check_meta_fields(fldinfos):
    count = Counter(fldinfos)
    for name, typename in count:
        if count[(name, typename)] > 1:

            def fld_urtype_info(fld):
                if fld.type == fld.urtype:
                    return f'{fld.type.name}'
                return f'{fld.type.name} (aka {fld.urtype.name})'

            typeinfos = ''.join((f'    - {fld.name}: {fld_urtype_info(fld)}\n'
                                 for fldname, fldtype in fldinfos))
            addError(
                'getting metadata',
                f'The name {name} appears in {count[(name, typename)]} metadata fields with different types:\n{typeinfos}'
            )
Beispiel #3
0
def get_hdrinst(arg0, component):
    if 'path' in arg0:
        hdrinst_name = arg0.path.name

        hdrtype = component.header.urtype
        dvar = parser.parserLocals.get(hdrinst_name, 'Declaration_Variable')
        if dvar:
            insts = [hi for hi in hlir.header_instances['StructField'] if hi.urtype.name == hdrtype.name]
            if len(insts) == 1:
                hdrinst = insts[0]
            elif len(insts) == 0:
                # note: it is defined as a local variable
                return dvar.urtype
            else:
                addError("Finding header", f"There is no single header that corresponds to {hdrtype.name}")
        else:
            return hlir.header_instances.get(hdrinst_name, 'Declaration_Variable', lambda hi: hi.urtype.name == hdrtype.name)
    elif 'hdr_ref' in (mexpr := component.methodCall.method.expr):
        hdrinst = mexpr.hdr_ref
        return hdrinst
Beispiel #4
0
    if smem.smem_type not in ["direct_counter", "direct_meter"]:
        continue
    #= gen_make_smem_code(smem, table)
for smem in unique_everseen([smem for table, smem in hlir.all_meters + hlir.all_counters if smem.smem_type not in ["direct_counter", "direct_meter"]]):
    #= gen_make_smem_code(smem)
for smem in hlir.registers:
    #= gen_make_smem_code(smem)



all_locals = unique_everseen([(param.name, format_type(param.type)) for table in hlir.tables for local in table.control.controlLocals["P4Action"] for param in local.parameters.parameters])
all_locals_dict = dict(all_locals)
if len(all_locals) != len(all_locals_dict):
    names = [name for name, _type in all_locals]
    dups = unique_everseen([name for name in names if names.count(name) > 1])
    addError("Collecting counters, meters, registers and controls' local variables", "The following names are used with different types, which is currently unsupported: {}".format(", ".join(dups)))

for locname, loctype in all_locals:
    #[     $loctype $locname;


# Note: currently all control locals are put together into the global state
for ctl in hlir.controls:
    for local_var_decl in ctl.controlLocals['Declaration_Variable'] + ctl.controlLocals['Declaration_Instance']:
        if 'name' in local_var_decl.urtype and local_var_decl.urtype.name in ('counter', 'direct_counter', 'meter', 'direct_meter', 'register'):
            continue

        #[     ${format_type(local_var_decl.type, varname = local_var_decl.name, resolve_names = False)};


Beispiel #5
0
        type_names = P4Node(sorted(typepars, key=lambda n: n.name))
        values = P4Node(sorted(named_apppars, key=lambda n: n.type.path.name))
    elif (extern := node).node_type == 'Type_Extern':
        if 'parameters' not in extern:
            extern.typeargs = {}
            return
        type_names = extern.typeParameters.parameters
        values = extern.parameters.parameters

    typeargs = dict(zip(type_names.map('name'), values))

    if 'typeargs' in method:
        if node.typeargs != typeargs:
            addError(
                'getting type arguments',
                f'Found differing sets of type arguments for {node.name}')
        return

    node.typeargs = typeargs


def attrs_typeargs(hlir: P4Node):
    """Resolve all Type_Name nodes to real type nodes"""

    for extern in hlir.all_nodes.by_type('Type_Extern'):
        for method in extern.methods:
            method.env_node = extern

    for extern in hlir.all_nodes.by_type('Type_Extern'):
        set_typeargs(extern)
Beispiel #6
0
def check_no_leftovers(base_node, leftover_nodes, node_description):
    if len(leftover_nodes) != 0:
        addError(
            f'visiting {node_description}s',
            f'{len(leftover_nodes)} {node_description}s of unexpected type found'
        )