from nir_opcodes import opcodes
+if sys.version_info < (3, 0):
+ integer_types = (int, long)
+ string_type = unicode
+
+else:
+ integer_types = (int, )
+ string_type = str
+
_type_re = re.compile(r"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
def type_bits(type_str):
class Value(object):
@staticmethod
def create(val, name_base, varset):
+ if isinstance(val, bytes):
+ val = val.decode('utf-8')
+
if isinstance(val, tuple):
return Expression(val, name_base, varset)
elif isinstance(val, Expression):
return val
- elif isinstance(val, (str, unicode)):
+ elif isinstance(val, string_type):
return Variable(val, name_base, varset)
- elif isinstance(val, (bool, int, long, float)):
+ elif isinstance(val, (bool, float) + integer_types):
return Constant(val, name_base)
__template = mako.template.Template("""
def hex(self):
if isinstance(self.value, (bool)):
return 'NIR_TRUE' if self.value else 'NIR_FALSE'
- if isinstance(self.value, (int, long)):
+ if isinstance(self.value, integer_types):
return hex(self.value)
elif isinstance(self.value, float):
- return hex(struct.unpack('Q', struct.pack('d', self.value))[0])
+ i = struct.unpack('Q', struct.pack('d', self.value))[0]
+ h = hex(i)
+
+ # On Python 2 this 'L' suffix is automatically added, but not on Python 3
+ # Adding it explicitly makes the generated file identical, regardless
+ # of the Python version running this script.
+ if h[-1] != 'L' and i > sys.maxsize:
+ h += 'L'
+
+ return h
else:
assert False
def type(self):
if isinstance(self.value, (bool)):
return "nir_type_bool32"
- elif isinstance(self.value, (int, long)):
+ elif isinstance(self.value, integer_types):
return "nir_type_int"
elif isinstance(self.value, float):
return "nir_type_float"