def test_type_size_in_bytes(self): self.assertEqual(1, type_utils.type_size_in_bytes('char')) self.assertEqual(4, type_utils.type_size_in_bytes('int')) self.assertEqual(4, type_utils.type_size_in_bytes('float')) self.assertEqual(8, type_utils.type_size_in_bytes('double'))
def size_in_bytes(self): if len(self.pointer_dims) > 0: return 4 else: return int(type_utils.type_size_in_bytes(self.type_specifiers))
def to_3ac(self, include_source=False): _3ac = [SOURCE(self.linerange[0], self.linerange[1])] if self.initializer is None: return {'3ac': _3ac} if self.symbol.global_memory_location: if self.initializer: if not self.initializer.immutable: raise Exception('Initializers to global objects must be constants.') _3ac.append(GLOBLDECL(self.symbol.global_memory_location, WORD_SPEC, self.initializer.value)) else: _3ac.append(GLOBLDECL(self.symbol.global_memory_location, WORD_SPEC)) return {'3ac': _3ac} # Get a register that points to the variable's memory so we can initialize it lvalue = tickets.INT_REGISTER_TICKETS.get() if self.symbol.global_memory_location: base_address = self.symbol.global_memory_location _3ac.append(LI(lvalue, base_address)) else: # Remember, stacks grow down, so go below FP _3ac.append(LA(lvalue, taci.Address(int_literal=-self.symbol.activation_frame_offset, register=tacr.FP))) # If the initializer is a list (for arrays) if isinstance(self.initializer, list): # Loop through initializer and store self.initializer = self.initializer[:min(len(self.initializer), self.symbol.array_dims[0])] for item in self.initializer: # Load the value item_tac = item.to_3ac(get_rval=True) if '3ac' in item_tac: _3ac.extend(item_tac['3ac']) # Store the value into memory, kick the register, and move to next type_size = int(type_utils.type_size_in_bytes(self.symbol.type_specifiers)) _3ac.append(STORE(item_tac['rvalue'], taci.Address(register=lvalue), type_size)) # Kick the temporaries if 'rvalue' in item_tac: _3ac.append(KICK(item_tac['rvalue'])) if 'lvalue' in item_tac: _3ac.append(KICK(item_tac['lvalue'])) # Go to the next index / offset by subtracting one element size _3ac.append(ADDI(lvalue, lvalue, -type_size)) else: # Load the value item_tac = self.initializer.to_3ac(get_rval=True) if '3ac' in item_tac: _3ac.extend(item_tac['3ac']) # Store the value into memory, kick the register, _3ac.append(STORE(item_tac['rvalue'], taci.Address(register=lvalue), self.symbol.size_in_bytes())) # Kick the temporaries if 'rvalue' in item_tac: _3ac.append(KICK(item_tac['rvalue'])) if 'lvalue' in item_tac: _3ac.append(KICK(item_tac['lvalue'])) # Kick the base address _3ac.append(KICK(lvalue)) return {'3ac': _3ac}