<commit_before>#
# Copyright (c) 2018 <NAME>
#
# SPDX-License-Identifier: Apache-2.0
#

from extract.globals import *
from extract.directive import DTDirective

from extract.default import default

##
# @brief Manage flash directives.
#
class DTFlash(DTDirective):
    def __init__(self):
        self._flash_area = {}

    def _extract_partition(self, node_path):
        prop_def = {}
        prop_alias = {}
        node = reduced[node_path]

        partition_name = node['props']['label']
        partition_sectors = node['props']['reg']

        # Build Index based partition IDs
        if node_path in self._flash_area:
            area_id = self._flash_area[node_path]["id"]
        else:
            area_id = len(self._flash_area)
            self._flash_area[node_path] = {'id': area_id }
        partition_idx = str(area_id)

        # Extract a per partition dev name, something like:
        # #define DT_FLASH_AREA_1_DEV             "FLASH_CTRL"

        # For now assume node_path is something like:
        # /flash-controller@4001E000/flash@0/partitions/partition@fc000
        # first we go up 2 levels to get the flash, check its compat
        #
        # The flash controller might be the flash itself (for cases like NOR
        # flashes), for the case of 'soc-nv-flash' we assume its the parent
        # of the flash node.
        ctrl_addr = '/' + '/'.join(node_path.split('/')[1:-2])
        if get_compat(ctrl_addr) == "soc-nv-flash":
            ctrl_addr = '/' + '/'.join(node_path.split('/')[1:-3])

        node = reduced[ctrl_addr]
        name = "\"{}\"".format(node['props']['label'])

        for area in self._flash_area.keys():
            if ctrl_addr in area:
                label = self.get_label_string(["DT_FLASH_AREA", partition_idx, "DEV"])
                prop_def[label] = name

        label = self.get_label_string(["DT_FLASH_AREA"] + [partition_name] + ["ID",])
        prop_def[label] = area_id

        label_prefix = ["DT_FLASH_AREA", partition_idx]

        index = 0
        while index < len(partition_sectors):
            sector_index = int(index/2)
            sector_start_offset = partition_sectors[index]
            sector_size = partition_sectors[index + 1]
            label = self.get_label_string(
                label_prefix + ["OFFSET", str(sector_index)])
            prop_def[label] = "{}".format(sector_start_offset)
            label = self.get_label_string(
                label_prefix + ["SIZE", str(sector_index)])
            prop_def[label] = "{}".format(sector_size)
            index += 2

        # alias sector 0
        label = self.get_label_string(label_prefix + ["OFFSET",])
        prop_alias[label] = self.get_label_string(
            label_prefix + ["OFFSET", '0'])
        label = self.get_label_string(label_prefix + ["SIZE",])
        prop_alias[label] = self.get_label_string(
            label_prefix + ["SIZE", '0'])

        label = self.get_label_string(["DT_FLASH_AREA", partition_idx, "LABEL"])
        prop_def[label] = self.get_label_string([partition_name,])

        prop_def["DT_FLASH_AREA_NUM"] = len(self._flash_area)

        insert_defs("DT_FLASH_AREA", prop_def, prop_alias)

    def _create_legacy_label(self, prop_alias, label):
        prop_alias[label.lstrip('DT_')] = label

    def extract_partition(self, node_path):
        prop_def = {}
        prop_alias = {}
        node = reduced[node_path]

        self._extract_partition(node_path)

        partition_name = node['props']['label']
        partition_sectors = node['props']['reg']

        label_prefix = ["DT_FLASH_AREA", partition_name]
        label = self.get_label_string(label_prefix + ["LABEL",])
        prop_def[label] = '"{}"'.format(partition_name)
        self._create_legacy_label(prop_alias, label)

        label = self.get_label_string(label_prefix + ["READ_ONLY",])
        prop_def[label] = 1 if 'read-only' in node['props'] else 0
        self._create_legacy_label(prop_alias, label)

        index = 0
        while index < len(partition_sectors):
            sector_index = int(index/2)
            sector_start_offset = partition_sectors[index]
            sector_size = partition_sectors[index + 1]
            label = self.get_label_string(
                label_prefix + ["OFFSET", str(sector_index)])
            prop_def[label] = "{}".format(sector_start_offset)
            self._create_legacy_label(prop_alias, label)
            label = self.get_label_string(
                label_prefix + ["SIZE", str(sector_index)])
            prop_def[label] = "{}".format(sector_size)
            self._create_legacy_label(prop_alias, label)
            index += 2
        # alias sector 0
        label = self.get_label_string(label_prefix + ["OFFSET",])
        prop_alias[label] = self.get_label_string(
            label_prefix + ["OFFSET", '0'])
        self._create_legacy_label(prop_alias, label)
        label = self.get_label_string(label_prefix + ["SIZE",])
        prop_alias[label] = self.get_label_string(
            label_prefix + ["SIZE", '0'])
        self._create_legacy_label(prop_alias, label)

        insert_defs(node_path, prop_def, prop_alias)

    def extract_flash(self):
        node_path = chosen.get('zephyr,flash')
        if not node_path:
            # Add addr/size 0 for systems with no flash controller. This is
            # what they already do in the Kconfig options anyway.
            insert_defs('dummy-flash',
                        {'DT_FLASH_BASE_ADDRESS': 0, 'DT_FLASH_SIZE': 0},
                        {})
            return

        flash_node = reduced[node_path]
        orig_node_addr = node_path

        nr_address_cells, nr_size_cells = get_addr_size_cells(node_path)
        # if the nr_size_cells is 0, assume a SPI flash, need to look at parent
        # for addr/size info, and the second reg property (assume first is mmio
        # register for the controller itself)
        is_spi_flash = False
        if nr_size_cells == 0:
            is_spi_flash = True<commit_msg>scripts/dts: Make extract/flash.py easier to read<commit_after>#
# Copyright (c) 2018 <NAME>
#
# SPDX-License-Identifier: Apache-2.0
#

from extract.globals import *
from extract.directive import DTDirective

from extract.default import default

##
# @brief Manage flash directives.
#
class DTFlash(DTDirective):
    def __init__(self):
        self._flash_area = {}

    def _extract_partition(self, node_path):
        prop_def = {}
        prop_alias = {}
        node = reduced[node_path]

        # Build Index based partition IDs
        if node_path not in self._flash_area:
            self._flash_area[node_path] = len(self._flash_area)
        area_id = self._flash_area[node_path]

        # Extract a per partition dev name, something like:
        # #define DT_FLASH_AREA_1_DEV             "FLASH_CTRL"

        # For now assume node_path is something like:
        # /flash-controller@4001E000/flash@0/partitions/partition@fc000
        # first we go up 2 levels to get the flash, check its compat
        #
        # The flash controller might be the flash itself (for cases like NOR
        # flashes), for the case of 'soc-nv-flash' we assume its the parent
        # of the flash node.
        controller_path = '/' + '/'.join(node_path.split('/')[1:-2])
        if get_compat(controller_path) == "soc-nv-flash":
            controller_path = '/' + '/'.join(node_path.split('/')[1:-3])

        for flash_path in self._flash_area:
            if controller_path in flash_path:
                prop_def["DT_FLASH_AREA_{}_DEV".format(area_id)] = \
                    '"' + reduced[controller_path]['props']['label'] + '"'

        partition_label = str_to_label(node['props']['label'])
        prop_def["DT_FLASH_AREA_{}_LABEL".format(area_id)] = partition_label
        prop_def["DT_FLASH_AREA_{}_ID".format(partition_label)] = area_id

        reg = node['props']['reg']
        for i in range(len(reg)//2):
            prop_def["DT_FLASH_AREA_{}_OFFSET_{}".format(area_id, i)] = reg[2*i]
            prop_def["DT_FLASH_AREA_{}_SIZE_{}".format(area_id, i)] = reg[2*i + 1]

        prop_def["DT_FLASH_AREA_NUM"] = len(self._flash_area)

        # Alias sector 0
        prop_alias[
            "DT_FLASH_AREA_{}_OFFSET".format(area_id)
        ] = "DT_FLASH_AREA_{}_OFFSET_0".format(area_id)
        prop_alias[
            "DT_FLASH_AREA_{}_SIZE".format(area_id)
        ] = "DT_FLASH_AREA_{}_SIZE_0".format(area_id)

        insert_defs("DT_FLASH_AREA", prop_def, prop_alias)

    def _create_legacy_label(self, prop_alias, label):
        prop_alias[label.lstrip('DT_')] = label

    def extract_partition(self, node_path):
        prop_def = {}
        prop_alias = {}
        node = reduced[node_path]

        self._extract_partition(node_path)

        partition_label = str_to_label(node['props']['label'])

        label = "DT_FLASH_AREA_{}_LABEL".format(partition_label)
        prop_def[label] = '"' + node['props']['label'] + '"'
        self._create_legacy_label(prop_alias, label)

        label = "DT_FLASH_AREA_{}_READ_ONLY".format(partition_label)
        prop_def[label] = 1 if 'read-only' in node['props'] else 0
        self._create_legacy_label(prop_alias, label)

        reg = node['props']['reg']
        for i in range(len(reg)//2):
            label = "DT_FLASH_AREA_{}_OFFSET_{}".format(partition_label, i)
            prop_def[label] = reg[2*i]
            self._create_legacy_label(prop_alias, label)

            label = "DT_FLASH_AREA_{}_SIZE_{}".format(partition_label, i)
            prop_def[label] = reg[2*i + 1]
            self._create_legacy_label(prop_alias, label)

        # Alias sector 0

        label = "DT_FLASH_AREA_{}_OFFSET".format(partition_label)
        prop_alias[label] = "DT_FLASH_AREA_{}_OFFSET_0".format(partition_label)
        self._create_legacy_label(prop_alias, label)

        label = "DT_FLASH_AREA_{}_SIZE".format(partition_label)
        prop_alias[label] = "DT_FLASH_AREA_{}_SIZE_0".format(partition_label)
        self._create_legacy_label(prop_alias, label)

        insert_defs(node_path, prop_def, prop_alias)

    def extract_flash(self):
        node_path = chosen.get('zephyr,flash')
        if not node_path:
            # Add addr/size 0 for systems with no flash controller. This is
            # what they already do in the Kconfig options anyway.
            insert_defs('dummy-flash',
                        {'DT_FLASH_BASE_ADDRESS': 0, 'DT_FLASH_SIZE': 0},
                        {})
            return

        flash_node = reduced[node_path]
        orig_node_addr = node_path

        nr_address_cells, nr_size_cells = get_addr_size_cells(node_path)
        # if the nr_size_cells is 0, assume a SPI flash, need to look at parent
        # for addr/size info, and the second reg property (assume first is mmio
        # register for the controller itself)
        is_spi_flash = False
        if nr_size_cells == 0:
            is_spi_flash = True
{'fraction_non_alphanumeric': 0.11417645452062278, 'fraction_numerical': 0.008012382773377038, 'mean_word_length': 2.5455132343447384, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3074968', 'n_tokens_mistral': 3524, 'n_tokens_neox': 3388, 'n_words': 697}