def set_val(self, val): if self.is_signed: # TODO: handle signed masking self.get_model().set_val(ValueScalar(int(val))) else: # Mask the user-specified value val = int(val) & ((1 << self.width) - 1) self.get_model().set_val(ValueScalar(val))
def __init__(self, val, is_signed, width): super().__init__() self._val = ValueScalar(val) self.signed = is_signed self._width = width if width < 1: raise Exception("Error: literal with a width of " + str(width))
def __init__(self, name, width, is_signed, is_rand, rand_if=None): super().__init__(name) self.width = width self.is_signed = is_signed self.is_declared_rand = is_rand self.is_used_rand = is_rand self.rand_if = rand_if self.var = None self.val = ValueScalar(0)
def __setitem__(self, k, v): if self.is_enum: ei: EnumInfo = self.t.enum_i val = ei.e2v(v) self.get_model().field_l[k].set_val(val) elif self.is_scalar: self.get_model().field_l[k].set_val( ValueScalar(int(v) & (1 << self.t.width) - 1)) else: self.backing_arr[k] = v
def val(self): ival = int(self.lhs.val()) if self.lower is not None: upper_i = int(self.upper.val()) lower_i = int(self.lower.val()) mask = (1 << (upper_i - lower_i + 1)) - 1 ival = ((ival >> lower_i) & mask) else: # Just a bit select upper_i = int(self.upper.val()) ival = ((ival >> upper_i) & 1) return ValueScalar(ival)
def test_wide_var(self): obj = FieldCompositeModel("obj") a = obj.add_field(FieldScalarModel("a", 1024, False, True)) obj.add_constraint( ConstraintBlockModel("c", [ ConstraintExprModel( ExprBinModel( a.expr(), BinExprType.Gt, ExprLiteralModel(0x80000000000000000, False, 72))) ])) randstate = RandState(0) rand = Randomizer(randstate) rand.do_randomize(randstate, SourceInfo("", -1), [obj]) print("a=" + hex(int(a.val))) self.assertGreater(a.val, ValueScalar(0x80000000000000000))
def visit_expr_bin(self, e: ExprBinModel): if self.debug: print("visit_expr_bin: op=%s" % str(e.op)) e.lhs.accept(self) lhs_is_x = self.is_x lhs_val = self.val e.rhs.accept(self) rhs_is_x = self.is_x rhs_val = self.val if e.op == BinExprType.Add: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val + rhs_val) elif e.op == BinExprType.And: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val & rhs_val) elif e.op == BinExprType.Or: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val | rhs_val) elif e.op == BinExprType.Le: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False if self.debug: print("lhs=%d rhs=%d" % (int(lhs_val), int(rhs_val))) if lhs_val <= rhs_val: self.val = ValueScalar(1) else: self.val = ValueScalar(0) elif e.op == BinExprType.Lt: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False if self.debug: print("lhs=%d rhs=%d" % (int(lhs_val), int(rhs_val))) if lhs_val < rhs_val: self.val = ValueScalar(1) else: self.val = ValueScalar(0) elif e.op == BinExprType.Ge: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False if self.debug: print("lhs=%d rhs=%d" % (int(lhs_val), int(rhs_val))) if lhs_val >= rhs_val: self.val = ValueScalar(1) else: self.val = ValueScalar(0) elif e.op == BinExprType.Gt: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False if self.debug: print("lhs=%d rhs=%d" % (int(lhs_val), int(rhs_val))) if lhs_val > rhs_val: self.val = ValueScalar(1) else: self.val = ValueScalar(0) elif e.op == BinExprType.Eq: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False if self.debug: print("lhs=%d rhs=%d" % (int(lhs_val), int(rhs_val))) if lhs_val == rhs_val: self.val = ValueScalar(1) else: self.val = ValueScalar(0) elif e.op == BinExprType.Ne: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False if self.debug: print("lhs=%d rhs=%d" % (int(lhs_val), int(rhs_val))) if lhs_val != rhs_val: self.val = ValueScalar(1) else: self.val = ValueScalar(0) elif e.op == BinExprType.Sub: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val - rhs_val) elif e.op == BinExprType.Div: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val / rhs_val) elif e.op == BinExprType.Mul: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val * rhs_val) elif e.op == BinExprType.Mod: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val % rhs_val) elif e.op == BinExprType.Sll: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val << rhs_val) elif e.op == BinExprType.Srl: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val >> rhs_val) elif e.op == BinExprType.Xor: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = (lhs_val ^ rhs_val) elif e.op == BinExprType.Not: if lhs_is_x or rhs_is_x: self.is_x = True self.val = None else: self.is_x = False self.val = ~lhs_val else: print("Unhandled op %s" % str(e.op)) if self.debug: print(" result: is_x=%s val=%d" % (str(self.is_x), int(self.val)))
def set_val(self, val): self._int_field_info.model.set_val(ValueScalar(int(val)))