Esempio n. 1
0
    def print_function_def(self, o):
        """
        :type o: HdlFunctionDef
        """
        self.print_doc(o)
        w = self.out.write
        if o.is_task:
            w("task ")
        else:
            w("function ")
        if not o.is_static:
            w("automatic ")

        if not o.is_task:
            self.print_type_first_part(o.return_t)
            self.print_type_array_part(o.return_t)

        if o.is_virtual or o.is_operator:
            raise NotImplementedError(o)
        w(" ")
        w(o.name)
        ps = o.params
        if ps:
            w(" (\n")
            with Indent(self.out):
                for last, p in iter_with_last_flag(ps):
                    self.print_port_declr(p)
                    if last:
                        w("\n")
                    else:
                        w(",\n")
            w(")")
        w(";\n")
        with Indent(self.out):
            for s in o.body:
                if isinstance(s, HdlVariableDef):
                    self.print_variable(s)
                    w(";\n")
                elif isinstance(s, iHdlStatement):
                    need_semi = self.print_statement(s)
                    if need_semi:
                        w(";\n")
                    else:
                        w("\n")
                else:
                    self.print_expr(s)
                    w(";\n")

        if o.is_task:
            w("endtask")
        else:
            w("endfunction")
Esempio n. 2
0
    def print_function(self, o):
        """
        :type o: HdlFunctionDef
        """
        w = self.out.write
        self.print_doc(o)
        is_procedure = o.return_t is None
        if is_procedure:
            w("PROCEDURE ")
        else:
            w("FUNCTION ")

        w(o.name)

        w(" (")
        with Indent(self.out):
            for is_last, par in iter_with_last_flag(o.params):
                self.print_variable(par, end="")
                if not is_last:
                    w(",\n")
        w(")")
        if not is_procedure:
            w(" RETURN ")
            self.print_type(o.return_t)
        w("\n")
        w("IS\n")
        self.print_body_items(o.body)
        w("END FUNCTION;\n")
Esempio n. 3
0
    def print_body_items(self, objs):
        w = self.out.write
        in_def_section = True
        with Indent(self.out):
            for o in objs:
                if isinstance(o, HdlVariableDef):
                    assert in_def_section, o
                    self.print_variable(o)
                    continue
                elif isinstance(o, HdlModuleDec):
                    assert in_def_section, o
                    self.print_component(o)
                    continue
                elif isinstance(o, HdlFunctionDef):
                    assert in_def_section, o
                    self.print_function(o)
                    continue

                if in_def_section:
                    with UnIndent(self.out):
                        w("BEGIN\n")
                    in_def_section = False

                if isinstance(o, HdlComponentInst):
                    self.print_component_instance(o)
                    w("\n")
                elif isinstance(o, iHdlStatement):
                    self.print_statement(o)
                else:
                    raise NotImplementedError(o)
        if in_def_section:
            w("BEGIN\n")
Esempio n. 4
0
    def print_module_body(self, a):
        """
        :type a: HdlModuleDef
        """
        w = self.out.write
        with Indent(self.out):
            for o in a.objs:
                if isinstance(o, HdlVariableDef):
                    self.print_variable(o)
                    w(";\n")
                elif isinstance(o, HdlComponentInst):
                    self.print_component_instance(o)
                    w(";\n\n")
                elif isinstance(o, iHdlStatement):
                    need_semi = self.print_statement(o, is_top=True)
                    if need_semi:
                        w(";\n")
                    else:
                        w("\n\n")
                elif isinstance(o, HdlFunctionDef):
                    self.print_function_def(o)
                    w("\n")
                else:
                    raise NotImplementedError(o)

        self.out.write("endmodule\n")
Esempio n. 5
0
    def print_process(self, proc):
        """
        :type proc: HdlStmProcess
        """
        sens = proc.sensitivity
        body = proc.body
        w = self.out.write

        w("PROCESS")
        if sens:
            w("(")
            for last, item in iter_with_last_flag(sens):
                self.print_expr(item)
                if not last:
                    w(", ")
            w(")")
        w("\n")
        w("BEGIN\n")
        with Indent(self.out):
            for _o in body:
                if isinstance(_o, iHdlStatement):
                    self.print_statement(_o)
                else:
                    self.print_expr(_o)
                    w(";\n")
        w("END PROCESS;\n")
Esempio n. 6
0
 def print_for(self, o):
     """
     :type o: HdlStmFor
     """
     w = self.out.write
     w("FOR ")
     self.print_expr(o.params[0])
     w(" IN ")
     self.print_expr(o.params[1])
     w(" LOOP\n")
     with Indent(self.out):
         for b in o.body:
             self.print_statement(b)
     w("END FOR;\n")
Esempio n. 7
0
    def print_block(self, stm):
        """
        :type stm: HdlStmBlock
        """
        w = self.out.write

        w("begin\n")
        with Indent(self.out):
            for s in stm.body:
                need_semi = self.print_statement(s)
                if need_semi:
                    w(";\n")
                else:
                    w("\n")
        w("end")
        return False
Esempio n. 8
0
    def print_statement_in_statement(self, stm):
        """
        Print statement which is body of other statement
        e.g. body of process, branch of if-then-else or case of case stememnt
        """
        w = self.out.write
        if isinstance(stm, HdlStmBlock):
            if len(stm.body) == 1:
                stm = stm.body[0]
            else:
                w(" ")
                return self.print_block(stm)

        w("\n")
        with Indent(self.out):
            return self.print_statement(stm)
Esempio n. 9
0
    def print_process(self, proc):
        sens = proc.sensitivity
        body = proc.body
        w = self.out.write

        w("always @ (")
        for last, item in iter_with_last_flag(sens):
            self.print_expr(item, sensitivity=True)
            if not last:
                w(", ")
        w(") begin\n")
        with Indent(self.out):
            for stm in body:
                self.print_statement(stm)

        w("end\n")
Esempio n. 10
0
 def print_case(self, cstm):
     w = self.out.write
     w("case(")
     self.print_expr(cstm.switch_on)
     w(")\n")
     with Indent(self.out):
         cases = cstm.cases
         for k, stms in cases:
             self.print_expr(k)
             w(":")
             is_block = self.print_block(stms)
             if is_block:
                 w("\n")
         defal = cstm.default
         if defal is not None:
             is_block = w("default:")
             self.print_block(defal)
             if is_block:
                 w("\n")
     w("endcase\n")
Esempio n. 11
0
    def print_block(self, stms, force_space_before=True):
        """
        :type stms: Union[List[iHdlStatement], iHdlStatement, iHdlExpr]
        :return: True if statements are wrapped in begin-end block
        """
        w = self.out.write
        if isinstance(stms, HdlStmBlock):
            must_have_begin_end = True
            stms = stms.body
        elif isinstance(stms, list):
            must_have_begin_end = len(stms) != 1
        else:
            must_have_begin_end = False
            stms = [
                stms,
            ]

        if must_have_begin_end:
            if force_space_before:
                w(" BEGIN\n")
            else:
                w("BEGIN\n")
        else:
            w("\n")

        with Indent(self.out):
            for s in stms:
                if isinstance(s, iHdlStatement):
                    self.print_statement(s)
                else:
                    self.print_expr(s)
                    w(";\n")

        if must_have_begin_end:
            w("END")
            return True

        return False
Esempio n. 12
0
    def print_block(self, stms):
        """
        :type stms: List[iHdlStatement]
        :return: True if statements are wrapped in begin-end block
        """
        w = self.out.write
        if len(stms) != 1:
            w(" BEGIN\n")
        else:
            w("\n")

        with Indent(self.out):
            for s in stms:
                if isinstance(s, iHdlStatement):
                    self.print_statement(s)
                else:
                    self.print_expr(s)
                    w(";\n")

        if len(stms) != 1:
            w("END")
            return True
        return False
Esempio n. 13
0
    def print_expr(self, expr):
        w = self.out.write
        if isinstance(expr, HdlName):
            w(expr)
            return
        elif is_str(expr):
            w('"%s"' % expr)
            return
        elif isinstance(expr, HdlIntValue):
            v = expr.val
            bits = expr.bits
            if bits is None:
                if expr.base is not None:
                    if expr.base == 256:
                        w("'%s'" % str(v))
                        return
                    bases = {
                        2: "B",
                        8: "O",
                        16: "X",
                    }
                    b = bases[expr.base]
                    w('%s"%"' % (b, v))
                w(str(v))
                return

            elif bits % 8 == 0:
                f = 'X"{0:0%dX}"' % (bits / 8)
            else:
                f = '"{0:0%db}"' % (bits)
            w(f.format(v))

            return
        elif expr is HdlAll:  # one previous line had 'elif isinstance(expr, HdlAll)'
            w("ALL")
            return
        elif expr is HdlOthers:
            w("OTHERS")
            return
        elif isinstance(expr, HdlCall):
            pe = self.print_expr
            fn = expr.ops[0]
            if fn == HdlName("assert"):
                self.print_assert(expr.ops[1:])
                return
            elif fn == HdlName("report"):
                self.print_report(expr.ops[1:])
                return

            o = expr
            op = expr.fn
            symbol = self.GENERIC_BIN_OPS.get(op, None)
            if symbol is not None:
                w("(")
                pe(o.ops[0])
                w(" ")
                w(symbol)
                w(" ")
                pe(o.ops[1])
                w(")")
                return
            elif op == HdlBuiltinFn.NOT:
                w("!")
                pe(o.ops[0])
                return
            elif op == HdlBuiltinFn.NEG:
                w("~")
                pe(o.ops[0])
                return
            elif op == HdlBuiltinFn.RISING:
                w("RISIG_EDGE(")
                pe(o.ops[0])
                w(")")
                return
            elif op == HdlBuiltinFn.FALLING:
                w("FALLING_EDGE(")
                pe(o.ops[0])
                w(")")
                return
            elif op == HdlBuiltinFn.NEG:
                w("~")
                pe(o.ops[0])
                return
            elif op == HdlBuiltinFn.CONCAT:
                w("{")
                pe(o.ops[0])
                w(", ")
                pe(o.ops[1])
                w("}")
                return
            elif op == HdlBuiltinFn.INDEX or op == HdlBuiltinFn.CALL:
                pe(o.ops[0])
                w("(")
                for isLast, a in iter_with_last_flag(o.ops[1:]):
                    pe(a)
                    if not isLast:
                        w(", ")
                w(")")
                return
            elif op == HdlBuiltinFn.DOT:
                pe(o.ops[0])
                w(".")
                pe(o.ops[1])
                return
            elif op == HdlBuiltinFn.TERNARY:
                pe(o.ops[0])
                w(" ? ")
                o0, o1 = o.ops[1:]
                pe(o0)
                w(" : ")
                pe(o1)
                return
            elif op == HdlBuiltinFn.APOSTROPHE:
                pe(o.ops[0])
                w("'")
                args = o.ops[1]
                if isinstance(args, list):
                    # aggregate
                    w("(")
                    for isLast, a in iter_with_last_flag(args):
                        pe(a)
                        if not isLast:
                            w(", ")
                    w(")")
                else:
                    # normal attribute
                    pe(args)
                return
            else:
                raise NotImplementedError(op)
        elif isinstance(expr, list):
            w("(\n")
            with Indent(self.out):
                for is_last, elem in iter_with_last_flag(expr):
                    self.print_expr(elem)
                    if not is_last:
                        w(",\n")
            w(")")
            return
        raise NotImplementedError(expr)