def visit_iHdlExpr(self, expr): w = self.out.write if expr is HdlAll: w("ALL") elif expr is HdlOthers: w("OTHERS") elif self.in_typedef and expr is None: w("<>") elif is_str(expr): self.visit_str(expr) elif isinstance(expr, list): with_nl = len(expr) > 3 if with_nl: w("(\n") else: w("(") with Indent(self.out): for is_last, elem in iter_with_last(expr): self.visit_iHdlExpr(elem) if not is_last: if with_nl: w(",\n") else: w(", ") w(")") else: ToHdlCommon.visit_iHdlExpr(self, expr)
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr :return: True, the flag used to mark that the ; should be added if this is a statement """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) elif is_str(o): w('"%s"' % o) elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) elif isinstance(o, HdlOp): _o, is_signed = pop_signed_flag(o) if o is not _o: self.visit_iHdlExpr(_o) else: self.visit_HdlOp(_o) if is_signed is True: w(" signed") elif is_signed is False: w(" unsigned") elif o is HdlAll: w("*") elif o is HdlTypeAuto: pass elif o is None: w("null") elif isinstance(o, float): w(str(o)) else: raise NotImplementedError(o.__class__, o) return True
def visit_HdlValueInt(self, o): w = self.out.write v = o.val bits = o.bits if is_str(v): v = v.upper() if o.base == 256: w("'%s'" % v) else: w('%s"%s"' % (self.NUM_BASES[o.base], v)) return if bits is None: if o.base is not None: b = self.NUM_BASES[o.base] if o.base == 256: w("'%d'" % v) elif o.base == 16: w('%s"%X"' % (b, v)) elif o.base == 8: w('%s"%o"' % (b, v)) elif o.base == 2: w('{0}"{1:b}"'.format(b, v)) else: raise NotImplementedError(o.base) else: 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))
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) elif is_str(o): w('"%s"' % o) elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) elif isinstance(o, (list, tuple)): with_nl = len(o) > 3 if isinstance(o, list): begin = "{" end = "}" else: begin = "(" end = ")" w(begin) for elem in o: self.visit_iHdlExpr(elem) if with_nl: w(", \n") else: w(", ") w(end) elif isinstance(o, HdlOp): self.visit_HdlOp(o) elif o is None: w("nullptr") else: raise NotImplementedError(o.__class__, o) return True
def get_object_and_scope_by_name(self, name): assert is_str(name), name if self.ignorecase: name = name.lower() actual = self while actual is not None: o = actual.get(name, _INVALID) if o is not _INVALID: return (actual, o) else: actual = actual.parent raise KeyError(name)
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) return elif is_str(o): w('"%s"' % o) return elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) return elif isinstance(o, (list, tuple)): with_nl = len(o) > 3 w("(") for elem in o: self.visit_iHdlExpr(elem) if with_nl: w(", \n") else: w(", ") w(")") return elif isinstance(o, HdlOp): self.visit_HdlOp(o) return elif o is None: w("None") return elif isinstance(o, dict): w("{") with Indent(self.out): for last, (k, v) in iter_with_last( sorted(o.items(), key=lambda x: x[0])): self.visit_iHdlExpr(k) w(": ") self.visit_iHdlExpr(v) if not last: w(",\n") else: w("\n") w("}") return elif isinstance(o, float): w("%f" % o) return raise NotImplementedError(o.__class__, o)
def visit_HdlIdDef(self, o): """ :type o: HdlIdDef """ if o.type is HdlTypeAuto: if is_str(o.value): o.type = HdlValueId("STR") return o elif isinstance(o.value, HdlValueInt): v = o.value.val if isinstance(v, int) and (v <= 1 and v >= 0): o.type = HdlValueId("BIT") else: o.type = HdlValueId("INT") return o return VerilogResolveTypes.visit_HdlIdDef(self, o)
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr :return: iHdlExpr """ if isinstance(o, HdlValueId): o = add_io_prefix(o) if not self._stm_dst: return hdl_getattr(o, "val") return o elif o is None or isinstance(o, HdlValueInt) or is_str(o): return o elif isinstance(o, list): return [self.visit_iHdlExpr(_o) for _o in o] elif isinstance(o, HdlOp): return self.visit_HdlOp(o) else: raise NotImplementedError("Do not know how to convert %s" % (o))
def level_push(self, name): assert is_str(name), name if self.ignorecase: name = name.lower() i = self.children.get(name, None) if i is not None: # there is already a child with such a name return i if name not in self: if self.debug: self[name] = object() else: raise AssertionError( name, "name not registered for any object in this scope") i = self.__class__(self, name, self.ignorecase) self.children[name] = i return i
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr """ w = self.out.write if isinstance(o, HdlValueId): w(o.val) return elif is_str(o): w('"%s"' % o) return elif isinstance(o, HdlValueInt): self.visit_HdlValueInt(o) return elif isinstance(o, HdlOp): self.visit_HdlOp(o) return else: raise NotImplementedError("Do not know how to convert %r" % (o))
def visit_iHdlExpr(self, o): """ :type o: iHdlExpr :return: iHdlExpr """ if isinstance(o, HdlValueId): return o.val elif is_str(o) or o is None: d = {"__class__": "str", "val": o} elif isinstance(o, HdlValueInt): d = self.visit_HdlValueInt(o) elif isinstance(o, HdlOp): d = self.visit_HdlOp(o) elif o is HdlAll or\ o is HdlTypeAuto or\ o is HdlOthers or\ o is HdlTypeType: d = { "__class__": o.__name__, } elif isinstance(o, (list, tuple, dict)): if isinstance(o, dict): items = [] for _k, _v in o.items(): k = self.visit_iHdlExpr(_k) v = self.visit_iHdlExpr(_v) items.append((k, v)) else: items = [self.visit_iHdlExpr(o2) for o2 in o] d = { "__class__": o.__class__.__name__, "items": items, } elif isinstance(o, iHdlTypeDef): return HdlAstVisitor.visit_iHdlObj(self, o) else: raise NotImplementedError("Unexpected object of type " + str(type(o))) return d
def visit_HdlValueInt(self, o): """ :type o: HdlValueInt """ w = self.out.write if o.bits is None: w(str(o.val)) else: if o.base is None: w(str(o.val)) else: b = o.base v = o.val if is_str(v): if set(v) == set('x'): v = None f = "{0}" elif b == 2: f = "0b{0}" elif b == 8: f = "0o{0}" elif b == 10: f = "{0}" elif b == 16: f = "0x{0}" else: raise NotImplementedError(b) else: if b == 2: f = "0b{0:b}" elif b == 8: f = "0o{0:o}" elif b == 10: f = "{0:d}" elif b == 16: f = "0x{0:x}" else: raise NotImplementedError(b) v = f.format(v) w(v)
def checked_name(self, suggested_name, obj): """ Get a non occupied name in current scope if name is occupied or name ends with _ the new name is generated. :return: str """ assert is_str(suggested_name), suggested_name if not suggested_name.endswith("_"): try: self.register_name(suggested_name, obj) return suggested_name except NameOccupiedErr: suggested_name += "_" actual = self while actual is not None: try: cntrVal = actual.cntrsForPrefixNames[suggested_name] break except KeyError: actual = actual.parent if actual is not None: # some parrent of self already have such a prefix counter usableName = actual.__incrPrefixCntr(suggested_name, cntrVal) else: # parrents and self does not have such a prefix counter # delete potentially existing prefix conterrs from children # and add prefix counter to self cntrVal = self.__discard_prefix_cntrs_from_children(suggested_name) usableName = self.__incrPrefixCntr(suggested_name, cntrVal) # setup for me and propagate to children self.register_name(usableName, obj) return usableName
def get_child(self, name): assert is_str(name), name if self.ignorecase: name = name.lower() return self.children[name]
def __init__(self, val, obj=None): assert is_str(val), val self.val = val self.obj = obj
def _parse_hdlConvertor_json(j): # handle primitive types if j is None: return j elif isinstance(j, float): return j elif is_str(j): return HdlValueId(j) elif isinstance(j, int): return HdlValueInt(j, None, None) elif isinstance(j, list): return [_parse_hdlConvertor_json(_j) for _j in j] # load a hdlAst object cls = j["__class__"] cls = KNOWN_NODES[cls] consumed = { "__class__", } if cls in NON_INSTANCIABLE_NODES: assert len(j) == 1 return cls elif cls in (dict, list, tuple): _items = j["items"] items = [_parse_hdlConvertor_json(i) for i in _items] if cls is dict: return {k: v for k, v in items} elif cls is tuple: return tuple(items) else: return items elif cls is str: return j["val"] elif cls is HdlValueInt: return HdlValueInt(j["val"], j.get("bits", None), j.get("base", None)) argc = cls.__init__.__code__.co_argcount if argc == 1: o = cls() else: # load argumets for __init__ argv = [] # 1st is self arg_names = cls.__init__.__code__.co_varnames[1:argc] for a in arg_names: v = j.get(a, None) if a == "fn": v = getattr(HdlOpType, v) else: v = _parse_hdlConvertor_json(v) argv.append(v) consumed.add(a) o = cls(*argv) not_consumed = set(j.keys()).difference(consumed) if not_consumed: # load rest of the properties which were not in __init__ params for k in not_consumed: v = j[k] # there are few cases where object class is not specified specified # explicitly we have to handle them first if k == "position": _v = CodePosition() (_v.start_line, _v.start_column, _v.stop_line, _v.stop_column) = v elif k == "direction": if v is None: _v = v else: _v = getattr(HdlDirection, v) elif k == "join_t": _v = getattr(HdlStmBlockJoinType, v) elif k == "trigger_constrain": _v = getattr(HdlStmProcessTriggerConstrain, v) elif cls is HdlStmCase and k == "type": _v = getattr(HdlStmCaseType, v) else: _v = _parse_hdlConvertor_json(v) setattr(o, k, _v) return o