Exemplo n.º 1
0
def da_oper_convert(insts: dal.InstGrp, val: core.DType) -> core.DType:
    op = val.compileInfo
    assert isinstance(op, core.Operation)

    opInfos = op.op()
    op_inst = dal.Op(opInfos.opcode, opInfos.opargs, dal.Var(""))
    insts.addInst(op_inst)
    return val
Exemplo n.º 2
0
def da_test(insts: dal.InstGrp, snippet: Snippet) -> Snippet:

    vars = snippet.insts()

    if len(vars) == 1:
        insts.setFlagWith(insts.TEST_EXPR, vars[0])

    return snippet
Exemplo n.º 3
0
def da_as_assign_value(insts: dal.InstGrp, snippet: Snippet) -> Snippet:

    v = snippet.insts()

    # Multiple assignment is not supported
    assert len(v) == 1

    insts.setFlagWith(insts.ASSIGN_VALUE, v[0])

    return snippet
Exemplo n.º 4
0
def da_binOp_Eq_transform(insts: dal.InstGrp, loperand: object,
                          roperand: object) -> Snippet:

    s = Snippet()

    if not da_binOp_need_transformed(loperand, roperand):
        s.value = loperand == roperand
        return s
    else:
        l = insts.getFlag(insts.COMPARETOR_LEFT)
        if l is None:
            l = da_to_python_type(loperand)
        r = insts.getFlag(insts.COMPARETOR_RIGHT)
        if r is None:
            r = da_to_python_type(roperand)

        insts.setFlagWith(insts.COMPARETOR_LEFT, None)
        insts.setFlagWith(insts.COMPARETOR_RIGHT, None)

        ident = insts.new_da_var_ident()
        var = dal.Var(ident)

        eq_inst = dal.Equal(l, r, var)
        insts.addInst(eq_inst)
        s.addInst(var)

        ret = core.DBool()
        ret.compileInfo = 1
        ret.transInfo = TransformInfos()
        ret.transInfo.set_op_ret = var

        s.value = ret

    return s
Exemplo n.º 5
0
def da_if_transform(insts: dal.InstGrp, body: dal.InstGrp,
                    elseBody: dal.InstGrp) -> None:

    test_result = insts.getFlag(insts.TEST_EXPR)
    assert (isinstance(test_result, core.DBool)
            or isinstance(test_result, dal.Var))

    # Create Jmp instruction
    else_exists = len(elseBody) > 0
    body_exists = len(body) > 0

    if else_exists:
        next_inst_idx = len(elseBody) + 1 + len(insts)

        jmp_inst = dal.JmpTrue(test_result, core.DInt(next_inst_idx))
    else:
        if body_exists:
            next_inst_idx = len(body) + 1 + len(insts)
            jmp_inst = dal.JmpFalse(test_result, core.DInt(next_inst_idx))
        else:
            return

    insts.addInst(jmp_inst)
    insts.addInsts(elseBody.insts())
    insts.addInsts(body.insts())
Exemplo n.º 6
0
def da_comparator(insts: dal.InstGrp, snippet: Snippet) -> Snippet:

    v = snippet.insts()

    # There should be only one comparator
    assert len(v) == 1

    if insts.getFlag(insts.COMPARETOR_LEFT) is None:
        insts.setFlagWith(insts.COMPARETOR_LEFT, v[0])
    else:
        insts.setFlagWith(insts.COMPARETOR_RIGHT, v[0])

    return snippet
Exemplo n.º 7
0
def da_machine_transform(insts: dal.InstGrp, m: core.Machine) -> core.Machine:
    """
    Generate requirements
    """
    if isinstance(m, core.Executors):
        insts.addExecutor(m.ident())
    elif isinstance(m, core.Dut):
        insts.addDut(m.ident())
    else:
        """
        A machine without extra identity
        """
        ...

    return m
Exemplo n.º 8
0
def da_call_transform(insts: dal.InstGrp, o: typ.Any,
                      recur_count: int) -> Snippet:

    snippet = da_call_not_operation(insts, o)
    if not snippet is None:
        return snippet

    transInfos = o.transInfo = TransformInfos()
    snippet = Snippet(value=o)

    assert (isinstance(o, core.DType))

    op = o.compileInfo

    assert (isinstance(transInfos, TransformInfos))
    assert (isinstance(op, core.Operation))

    opInfo = op.op()
    args = []
    argv = len(opInfo.opargs)

    # Get Arguments if need
    if argv > 0:
        args_dict = insts.getFlag(insts.ARG_HOLDER)
        assert args_dict is not None

        args = args_dict[recur_count]

        if args is None:
            raise DA_CALL_TRANSFORM_NO_ARGS_FOUND()
        if len(args) != argv:
            raise DA_CALL_TRANSFORM_ARGS_MISMATCH()

        del args_dict[recur_count]

    retVar = insts.compileDict[insts.VAR_ID_GEN].gen()
    var = dal.Var(retVar)

    op_inst = dal.Op(opInfo.opcode, args, var)
    insts.addInst(op_inst)
    snippet.addInst(var)

    transInfos.set_op_ret(var)
    transInfos.transformed()

    return snippet
Exemplo n.º 9
0
def da_define(insts: dal.InstGrp, identifier: str,
              snippet: Snippet) -> Snippet:
    """
    Note: This modifier should be only used while Variable's value is
    unpredictable.

    For example:

    def f():
        if expr:
            v = da_unwrap(da_define("v", da_call_transform(DInt(1))))
        else:
            v = da_unwrap(da_define("v", da_call_transform(DInt(2))))

        f(v)

    v is unknowk until expr is evaluated, so need to bind DInt(1) and DInt(2)'s
    Def Inst to the same variable.
    """
    # To check that is variable is already defined
    da_var_ident = insts.get_da_var(identifier)
    if da_var_ident is None:
        da_var_ident = insts.new_da_var_ident()
        insts.add_var_map(identifier, da_var_ident)

    if len(snippet.insts()) != 0:
        # Make sure it's not a python value
        insts.addInst(
            dal.Def(da_var_ident,
                    typ.cast(core.DType, snippet.value).value()))

    return snippet
Exemplo n.º 10
0
def da_as_arg(insts: dal.InstGrp, snippet: Snippet,
              recur_count: int) -> Snippet:

    args_dict = insts.getFlag(insts.ARG_HOLDER)
    assert args_dict is not None

    if not recur_count in args_dict:
        args_dict[recur_count] = args = []
    else:
        args = args_dict[recur_count]

    for inst in snippet.insts():
        args.append(inst)

    return snippet