def print_for(self, o): """ :type o: HdlStmFor :return: True if requires ;\n after end """ w = self.out.write w("for (") if isinstance(o.init, HdlStmBlock): init_stms = o.init.body else: init_stms = [ o.init, ] for is_last, stm in iter_with_last_flag(init_stms): self.print_statement(stm) if not is_last: w(", ") w("; ") self.print_expr(o.cond) w("; ") if isinstance(o.init, HdlStmBlock): step_stms = o.step.body else: step_stms = [ o.step, ] for is_last, stm in iter_with_last_flag(step_stms): self.print_statement(stm) if not is_last: w(", ") w(")") return self.print_statement_in_statement(o.body)
def print_module_header(self, e): self.print_doc(e) w = self.out.write w("ENTITY ") w(e.name) w(" IS\n") gs = e.params if gs: with Indent(self.out): w("GENERIC(\n") with Indent(self.out): for last, g in iter_with_last_flag(gs): self.print_generic_or_port_declr(g) if last: w("\n") else: w(",\n") w(");\n") ps = e.ports if ps: with Indent(self.out): w("PORT(\n") with Indent(self.out): for last, p in iter_with_last_flag(ps): self.print_generic_or_port_declr(p) if last: w("\n") else: w(",\n") w(");\n") w("END ENTITY;\n")
def print_module_header(self, e): """ :type e: HdlModuleDef """ self.print_doc(e) w = self.out.write w("module ") w(e.name) gs = e.params if gs: w(" #(\n") with Indent(self.out): for last, g in iter_with_last_flag(gs): self.print_generic_declr(g) if last: w("\n") else: w(",\n") w(")") ps = e.ports 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")
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")
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")
def print_map(self, map_): w = self.out.write with Indent(self.out): for last, m in iter_with_last_flag(map_): self.print_map_item(m) if last: w("\n") else: w(",\n")
def print_doc(self, obj): doc = obj.doc if doc is not None: doc = doc.split("\n") w = self.out.write for last, d in iter_with_last_flag(doc): if last and d == "": break w("//") w(d) w("\n")
def print_hdl_import(self, o): """ :type o: HdlImport """ self.print_doc(o) w = self.out.write w("USE ") for last, p in iter_with_last_flag(o.path): self.print_expr(p) if not last: w(".") w(";\n")
def print_for(self, o): """ :type o: HdlStmFor :return: True if requires ;\n after end """ w = self.out.write w("for (") for is_last, stm in iter_with_last_flag(o.init): self.print_statement(stm) if not is_last: w(", ") w("; ") self.print_expr(o.cond) w("; ") for is_last, stm in iter_with_last_flag(o.step): self.print_statement(stm) if not is_last: w(", ") w(")") return self.print_block(o.body, True)
def print_report(self, args): """ :type args: List[iHdlExpr] """ w = self.out.write w("REPORT ") for is_last, (prefix, a) in iter_with_last_flag( zip(("", "SEVERITY "), args)): w(prefix) self.print_expr(a) if not is_last: w(" ")
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")
def print_module_header(self, e, vhdl_obj_name="ENTITY"): """ :param e: Entity :type e: HdlModuleDec """ self.print_doc(e) w = self.out.write w(vhdl_obj_name) w(" ") w(e.name) w(" IS\n") gs = e.params if gs: with Indent(self.out): w("GENERIC(\n") with Indent(self.out): for last, g in iter_with_last_flag(gs): self.print_generic_or_port_declr(g) if last: w("\n") else: w(",\n") w(");\n") ps = e.ports if ps: with Indent(self.out): w("PORT(\n") with Indent(self.out): for last, p in iter_with_last_flag(ps): self.print_generic_or_port_declr(p) if last: w("\n") else: w(",\n") w(");\n") w("END ") w(vhdl_obj_name) w(";\n")
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")
def print_hdl_import(self, o): """ :type o: HdlImport """ self.print_doc(o) w = self.out.write lib_name = o.path[0] if lib_name not in self.used_libraries: self.print_library(lib_name) w("USE ") for last, p in iter_with_last_flag(o.path): self.print_expr(p) if not last: w(".") w(";\n")
def print_process(self, proc): sens = proc.sensitivity body = proc.body w = self.out.write w("PROCESS(") for last, item in iter_with_last_flag(sens): self.print_expr(item) if not last: w(", ") w(")\n") w("BEGIN\n") with Indent(self.out): for stm in body: self.print_statement(stm) w("END PROCESS;\n")
def print_process(self, proc, is_top=False): """ :type proc: HdlStmProcess """ sens = proc.sensitivity body = proc.body w = self.out.write skip_body = False if sens is None: if isinstance(body, HdlStmWait): skip_body = True wait = body elif (isinstance(body, HdlStmBlock) and body.body and isinstance(body.body[0], HdlStmWait)): wait = body.body[0] body = copy(body) body.body = body.body[1:] else: wait = None if wait is not None: if is_top: w("always ") w("#") assert len(wait.val) == 1 self.print_expr(wait.val[0]) else: assert is_top w("initial") else: if is_top: w("always ") w("@(") for last, item in iter_with_last_flag(sens): self.print_expr(item) if not last: w(", ") w(")") # to prevent useless newline for empty always/time waits if skip_body: return True else: return self.print_statement_in_statement(body)
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") self.print_block(body, force_space_before=False) w(" PROCESS;\n")
def print_process(self, proc, is_top=False): """ :type proc: HdlStmProcess """ sens = proc.sensitivity body = proc.body w = self.out.write skip_first = False if sens is None: if len(body) and isinstance(body[0], HdlStmWait): skip_first = True wait = body[0] if is_top: w("always ") w("#") assert len(wait.val) == 1 self.print_expr(wait.val[0]) else: assert is_top w("initial") else: if is_top: w("always ") w("@(") for last, item in iter_with_last_flag(sens): self.print_expr(item) if not last: w(", ") w(")") if skip_first: _body = body[1:] else: _body = body # to prevent useless newline for empty always/time waits if _body: return self.print_block(_body, True) else: return True
def print_assignment(self, a, is_top=False): """ :type a: HdlStmAssign :return: True if requires ;\n after end """ s = a.src d = a.dst w = self.out.write if is_top: w("assign ") self.print_expr(d) w(" = ") else: self.print_expr(d) if a.is_blocking: w(" = ") else: w(" <= ") if a.time_delay is not None: w("#") self.print_expr(a.time_delay) w(" ") if a.event_delay is not None: w("@") if len(a.event_delay) > 1: w("(") for is_last, e in iter_with_last_flag(a.event_delay): self.print_expr(e) if not is_last: w(", ") if len(a.event_delay) > 1: w(")") w(" ") self.print_expr(s) return True
def print_expr(self, expr, sensitivity=False): w = self.out.write if isinstance(expr, HdlName): w(expr) return elif isinstance(expr, str): w('"%s"' % expr) return elif isinstance(expr, HdlIntValue): if expr.bits is None: w(str(expr.val)) else: if expr.base is None: f = "{0}'h{1:x}" else: b = expr.base if b == 2: f = "{0}b'{1}" elif b == 8: f = "{0}O'{1}" elif b == 10: f = "{0}d'{1}" elif b == 16: f = "{0}h'{1}" else: raise NotImplementedError(b) w(f.format(expr.bits, expr.val)) return elif isinstance(expr, HdlName): w(expr.val) return elif isinstance(expr, HdlAll): w("*") return elif isinstance(expr, HdlCall): def pe(e): return self.print_expr(e, sensitivity=sensitivity) o = expr op = expr.fn symbol = self.GENERIC_BIN_OPS.get(op, None) if symbol is not None: if sensitivity and op == HdlBuildinFn.OR: symbol = "or" op_cnt = len(o.ops) if op_cnt == 1: w("(") w(symbol) pe(o.ops[0]) w(")") elif op_cnt == 2: w("(") pe(o.ops[0]) w(" ") w(symbol) w(" ") pe(o.ops[1]) w(")") return if op == HdlBuildinFn.DOWNTO: pe(o.ops[0]) w(":") pe(o.ops[1]) return elif op == HdlBuildinFn.TO: pe(o.ops[1]) w(":") pe(o.ops[0]) return elif op == HdlBuildinFn.NOT: w("!") pe(o.ops[0]) return elif op == HdlBuildinFn.NEG: w("~") pe(o.ops[0]) return elif op == HdlBuildinFn.RISING: assert sensitivity w("posedge ") pe(o.ops[0]) return elif op == HdlBuildinFn.FALLING: assert sensitivity w("negedge ") pe(o.ops[0]) return elif op == HdlBuildinFn.NEG: w("~") pe(o.ops[0]) return elif op == HdlBuildinFn.CONCAT: w("{") pe(o.ops[0]) w(", ") pe(o.ops[1]) w("}") return elif op == HdlBuildinFn.INDEX: pe(o.ops[0]) w("[") pe(o.ops[1]) w("]") return elif op == HdlBuildinFn.REPL_CONCAT: w("{(") pe(o.ops[0]) w("){") pe(o.ops[1]) w("}}") return elif op == HdlBuildinFn.TERNARY: pe(o.ops[0]) w(" ? ") o0, o1 = o.ops[1:] pe(o0) w(" : ") pe(o1) return elif op == HdlBuildinFn.CALL: pe(o.ops[0]) w("(") for is_last, o_n in iter_with_last_flag(o.ops[1:]): pe(o_n) if not is_last: w(", ") w(")") return else: raise NotImplementedError(op) elif expr is HdlAll: w("*") return raise NotImplementedError(expr)
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)
def print_expr(self, expr): """ :type expr: iHdlExpr """ w = self.out.write if isinstance(expr, HdlName): w(expr) return elif is_str(expr): w('"%s"' % expr) return elif isinstance(expr, HdlIntValue): if expr.bits is None: w(str(expr.val)) else: if expr.base is None: f = "{0}'h{1:x}" else: b = expr.base if b == 2: base_char = 'b' elif b == 8: base_char = 'O' elif b == 10: base_char = 'd' elif b == 16: base_char = 'h' else: raise NotImplementedError(b) f = "{0}'" + base_char + "{1}" w(f.format(expr.bits, expr.val)) return elif isinstance(expr, HdlName): w(expr.val) return elif isinstance(expr, HdlAll): w("*") return elif isinstance(expr, HdlCall): pe = self.print_expr o = expr op = expr.fn symbol = self.GENERIC_BIN_OPS.get(op, None) if symbol is not None: op_cnt = len(o.ops) if op_cnt == 1: w("(") w(symbol) pe(o.ops[0]) w(")") elif op_cnt == 2: w("(") pe(o.ops[0]) w(" ") w(symbol) w(" ") pe(o.ops[1]) w(")") return if op == HdlBuildinFn.DOWNTO: pe(o.ops[0]) w(":") pe(o.ops[1]) return elif op == HdlBuildinFn.TO: pe(o.ops[1]) w(":") pe(o.ops[0]) return elif op == HdlBuildinFn.NOT: w("!") pe(o.ops[0]) return elif op == HdlBuildinFn.NEG: w("~") pe(o.ops[0]) return elif op == HdlBuildinFn.RISING: w("posedge ") pe(o.ops[0]) return elif op == HdlBuildinFn.FALLING: w("negedge ") pe(o.ops[0]) return elif op == HdlBuildinFn.NEG: w("~") pe(o.ops[0]) return elif op == HdlBuildinFn.CONCAT: w("{") pe(o.ops[0]) w(", ") pe(o.ops[1]) w("}") return elif op == HdlBuildinFn.INDEX: pe(o.ops[0]) w("[") pe(o.ops[1]) w("]") return elif op == HdlBuildinFn.REPL_CONCAT: w("{(") pe(o.ops[0]) w("){") pe(o.ops[1]) w("}}") return elif op == HdlBuildinFn.TERNARY: w("(") pe(o.ops[0]) w(") ? (") o0, o1 = o.ops[1:] pe(o0) w(") : (") pe(o1) w(")") return elif op == HdlBuildinFn.CALL: pe(o.ops[0]) w("(") for is_last, o_n in iter_with_last_flag(o.ops[1:]): pe(o_n) if not is_last: w(", ") w(")") return else: raise NotImplementedError(op) elif expr is HdlAll: w("*") return elif expr is HdlTypeAuto: return raise NotImplementedError(expr)