def offset(self): """ If this is a constant access (e.g. A(1)) return the offset in bytes from the beginning of the variable in memory. Otherwise, if it's not constant (e.g. A(i)) returns None """ offset = 0 # Now we must typecast each argument to a u16 (POINTER) type # i is the dimension ith index, b is the bound for i, b in zip(self.arglist, self.entry.bounds): tmp = i.children[0] if is_number(tmp) or is_const(tmp): if offset is not None: offset = offset * b.count + tmp.value else: offset = None break if offset is not None: offset = TYPE.size(gl.SIZE_TYPE) + TYPE.size(gl.BOUND_TYPE) * len( self.arglist) + offset * self.type_.size return offset
def __init__(self, type_, name=None): """ type_ = Internal representation (e.g. TYPE.ubyte) """ assert TYPE.is_valid(type_) if not name: name = TYPE.to_string(type_) super(SymbolBASICTYPE, self).__init__(name, 0) self.type_ = type_
def TYPE(type_): """ Converts a backend type (from api.constants) to a SymbolTYPE object (taken from the SYMBOL_TABLE). If type_ is already a SymbolTYPE object, nothing is done. """ if isinstance(type_, symbols.TYPE): return type_ assert TYPE.is_valid(type_) return gl.SYMBOL_TABLE.basic_types[type_]
def TSUFFIX(type_): assert isinstance(type_, symbols.TYPE) or TYPE.is_valid(type_) _TSUFFIX = {TYPE.byte_: 'i8', TYPE.ubyte: 'u8', TYPE.integer: 'i16', TYPE.uinteger: 'u16', TYPE.long_: 'i32', TYPE.ulong: 'u32', TYPE.fixed: 'f16', TYPE.float_: 'f', TYPE.string: 'str' } if isinstance(type_, symbols.TYPEREF): type_ = type_.final assert isinstance(type_, symbols.BASICTYPE) if isinstance(type_, symbols.BASICTYPE): return _TSUFFIX[type_.type_] return _TSUFFIX[type_]
def btyperef(self, type_): assert TYPE.is_valid(type_) return symbols.TYPEREF(symbols.BASICTYPE(type_), 0)
def memsize(self): """ Total array cell + indexes size """ return 2 * TYPE.size(gl.PTR_TYPE)
def size(self): return self.count * self.type_.size if self.scope != SCOPE.parameter else TYPE.size( gl.PTR_TYPE)
def test_size(self): for type_ in TYPE.types: t = SymbolBASICTYPE(type_) self.assertEqual(t.size, TYPE.size(type_))
def test_memsize(self): arr = symbols.VARARRAY('test', self.bounds, 1, type_=Type.ubyte) self.assertEqual(arr.memsize, 2 * TYPE.size(gl.PTR_TYPE))
def memsize(self): """ Total array cell + indexes size """ return (2 + (2 if self.lbound_used or self.ubound_used else 0) ) * TYPE.size(gl.PTR_TYPE)
def test_memsize(self): arr = symbols.VARARRAY('test', self.bounds, 1, type_=Type.ubyte) self.assertEqual( arr.memsize, arr.size + 1 + TYPE.size(gl.BOUND_TYPE) * len(arr.bounds))
def to_signed(self): """ Returns another instance with the signed equivalent of this type. """ return SymbolBASICTYPE(TYPE.to_signed(self.type_))
def is_signed(self): return TYPE.is_signed(self.type_)
def size(self): return TYPE.size(self.type_)
def memsize(self): """ Total array cell + indexes size """ return self.size + 1 + TYPE.size(gl.BOUND_TYPE) * len(self.bounds)