<commit_before>#!/bin/sh

#
# file: ARMv7-M.ld.sh
#
# author: Copyright (C) 2015-2016 <NAME> http://www.distortec.com http://www.freddiechopin.info
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

set -e
set -u

if [ $# -lt 5 ]; then
	echo "This script requires at least 5 arguments!" >&2
	exit 1
fi

chipName=$1
romDescription=$2
ramDescription=$3
mainStackSize=$4
processStackSize=$5

decimalOrHexadecimalRegex='\(\(0x[0-9a-fA-F]\{1,8\}\)\|\([0-9]\{1,9\}\)\)'
addressRegex="\($decimalOrHexadecimalRegex\)"
sizeRegex="\($decimalOrHexadecimalRegex[kM]\?\)"

if ! expr "$romDescription" : "\($addressRegex,$sizeRegex\)$" > /dev/null; then
	echo "Invalid description of ROM - \"$romDescription\"!" >&2
	exit 2
fi

if ! expr "$ramDescription" : "\($addressRegex,$sizeRegex\)$" > /dev/null; then
	echo "Invalid description of RAM - \"$ramDescription\"!" >&2
	exit 3
fi

if ! expr "$mainStackSize" : "\($sizeRegex\)$" > /dev/null; then
	echo "Invalid size of main stack - \"$mainStackSize\"!" >&2
	exit 4
fi

if ! expr "$processStackSize" : "\($sizeRegex\)$" > /dev/null; then
	echo "Invalid size of process stack - \"$processStackSize\"!" >&2
	exit 5
fi

# the matched string may (technically) be "0" - that's why " || true" is needed
romAddress=`expr "$romDescription" : '\([^,]\+\),[^,]\+' || true`
romSize=`expr "$romDescription" : '[^,]\+,\([^,]\+\)' || true`
ramAddress=`expr "$ramDescription" : '\([^,]\+\),[^,]\+' || true`
ramSize=`expr "$ramDescription" : '[^,]\+,\([^,]\+\)' || true`

shift
shift
shift
shift
shift

headerComments=
memoryEntries=
memorySizes=
dataArrayEntries=
bssArrayEntries=
sectionEntries=
sectionSizes=

while [ $# -gt 0 ]; do

	if ! expr "$1" : "\([^,]\+,$addressRegex,$sizeRegex\)$" > /dev/null; then
		echo "Invalid description of memory - \"$1\"!" >&2
		exit 6
	fi

	memoryName=`expr "$1" : '\([^,]\+\),[^,]\+,[^,]\+' || true`
	memoryAddress=`expr "$1" : '[^,]\+,\([^,]\+\),[^,]\+' || true`
	memorySize=`expr "$1" : '[^,]\+,[^,]\+,\([^,]\+\)' || true`

	headerComments+=" * - $memorySize bytes of $memoryName;\n"

	memoryEntries+="\t$memoryName : org = $memoryAddress, len = $memorySize\n"

	memorySizes+="\
__${memoryName}_start = ORIGIN(${memoryName});
__${memoryName}_size = LENGTH(${memoryName});
__${memoryName}_end = __${memoryName}_start + __${memoryName}_size;
PROVIDE(__${memoryName}_start = __${memoryName}_start);
PROVIDE(__${memoryName}_size = __${memoryName}_size);
PROVIDE(__${memoryName}_end = __${memoryName}_end);\n\n"

	dataArrayEntries+="\
\t\tLONG(LOADADDR(.${memoryName}.data)); LONG(ADDR(.${memoryName}.data)); \
LONG(ADDR(.${memoryName}.data) + SIZEOF(.${memoryName}.data));\n"

	bssArrayEntries+="\
\t\tLONG(ADDR(.${memoryName}.bss)); LONG(ADDR(.${memoryName}.bss) + SIZEOF(.${memoryName}.bss));\n"

	sectionEntries+="\
	.${memoryName}.bss :
	{
		. = ALIGN(4);
		__${memoryName}_bss_start = .;
		PROVIDE(__${memoryName}_bss_start = __${memoryName}_bss_start);

		*(.${memoryName}.bss)

		. = ALIGN(4);
		__${memoryName}_bss_end = .;
		PROVIDE(__${memoryName}_bss_end = __${memoryName}_bss_end);
	} > ${memoryName} AT > ${memoryName}

	.${memoryName}.data :
	{
		. = ALIGN(4);
		__${memoryName}_data_init_start = LOADADDR(.${memoryName}.data);
		PROVIDE(__${memoryName}_data_init_start = __${memoryName}_data_init_start);
		__${memoryName}_data_start = .;
		PROVIDE(__${memoryName}_data_start = __${memoryName}_data_start);

		*(.${memoryName}.data)

		. = ALIGN(4);
		__${memoryName}_data_end = .;
		PROVIDE(__${memoryName}_data_end = __${memoryName}_data_end);
	} > ${memoryName} AT > rom

	.${memoryName}.noinit :
	{
		. = ALIGN(4);
		__${memoryName}_noinit_start = .;
		PROVIDE(__${memoryName}_noinit_start = __${memoryName}_noinit_start);

		*(.${memoryName}.noinit)

		. = ALIGN(4);
		__${memoryName}_noinit_end = .;
		PROVIDE(__${memoryName}_noinit_end = __${memoryName}_noinit_end);
	} > ${memoryName} AT > ${memoryName}\n\n"

	sectionSizes+="\
PROVIDE(__${memoryName}_bss_size = __${memoryName}_bss_end - __${memoryName}_bss_start);<commit_msg>Don't use "+=" operator in ARMv7-M.ld.sh<commit_after>#!/bin/sh

#
# file: ARMv7-M.ld.sh
#
# author: Copyright (C) 2015-2016 <NAME> http://www.distortec.com http://www.freddiechopin.info
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

set -e
set -u

if [ $# -lt 5 ]; then
	echo "This script requires at least 5 arguments!" >&2
	exit 1
fi

chipName=$1
romDescription=$2
ramDescription=$3
mainStackSize=$4
processStackSize=$5

decimalOrHexadecimalRegex='\(\(0x[0-9a-fA-F]\{1,8\}\)\|\([0-9]\{1,9\}\)\)'
addressRegex="\($decimalOrHexadecimalRegex\)"
sizeRegex="\($decimalOrHexadecimalRegex[kM]\?\)"

if ! expr "$romDescription" : "\($addressRegex,$sizeRegex\)$" > /dev/null; then
	echo "Invalid description of ROM - \"$romDescription\"!" >&2
	exit 2
fi

if ! expr "$ramDescription" : "\($addressRegex,$sizeRegex\)$" > /dev/null; then
	echo "Invalid description of RAM - \"$ramDescription\"!" >&2
	exit 3
fi

if ! expr "$mainStackSize" : "\($sizeRegex\)$" > /dev/null; then
	echo "Invalid size of main stack - \"$mainStackSize\"!" >&2
	exit 4
fi

if ! expr "$processStackSize" : "\($sizeRegex\)$" > /dev/null; then
	echo "Invalid size of process stack - \"$processStackSize\"!" >&2
	exit 5
fi

# the matched string may (technically) be "0" - that's why " || true" is needed
romAddress=`expr "$romDescription" : '\([^,]\+\),[^,]\+' || true`
romSize=`expr "$romDescription" : '[^,]\+,\([^,]\+\)' || true`
ramAddress=`expr "$ramDescription" : '\([^,]\+\),[^,]\+' || true`
ramSize=`expr "$ramDescription" : '[^,]\+,\([^,]\+\)' || true`

shift
shift
shift
shift
shift

headerComments=
memoryEntries=
memorySizes=
dataArrayEntries=
bssArrayEntries=
sectionEntries=
sectionSizes=

while [ $# -gt 0 ]; do

	if ! expr "$1" : "\([^,]\+,$addressRegex,$sizeRegex\)$" > /dev/null; then
		echo "Invalid description of memory - \"$1\"!" >&2
		exit 6
	fi

	memoryName=`expr "$1" : '\([^,]\+\),[^,]\+,[^,]\+' || true`
	memoryAddress=`expr "$1" : '[^,]\+,\([^,]\+\),[^,]\+' || true`
	memorySize=`expr "$1" : '[^,]\+,[^,]\+,\([^,]\+\)' || true`

	headerComments="$headerComments * - $memorySize bytes of $memoryName;\n"

	memoryEntries="$memoryEntries\t$memoryName : org = $memoryAddress, len = $memorySize\n"

	memorySizes="${memorySizes}\
__${memoryName}_start = ORIGIN(${memoryName});
__${memoryName}_size = LENGTH(${memoryName});
__${memoryName}_end = __${memoryName}_start + __${memoryName}_size;
PROVIDE(__${memoryName}_start = __${memoryName}_start);
PROVIDE(__${memoryName}_size = __${memoryName}_size);
PROVIDE(__${memoryName}_end = __${memoryName}_end);\n\n"

	dataArrayEntries="$dataArrayEntries\
\t\tLONG(LOADADDR(.${memoryName}.data)); LONG(ADDR(.${memoryName}.data)); \
LONG(ADDR(.${memoryName}.data) + SIZEOF(.${memoryName}.data));\n"

	bssArrayEntries="$bssArrayEntries\
\t\tLONG(ADDR(.${memoryName}.bss)); LONG(ADDR(.${memoryName}.bss) + SIZEOF(.${memoryName}.bss));\n"

	sectionEntries="$sectionEntries\
	.${memoryName}.bss :
	{
		. = ALIGN(4);
		__${memoryName}_bss_start = .;
		PROVIDE(__${memoryName}_bss_start = __${memoryName}_bss_start);

		*(.${memoryName}.bss)

		. = ALIGN(4);
		__${memoryName}_bss_end = .;
		PROVIDE(__${memoryName}_bss_end = __${memoryName}_bss_end);
	} > ${memoryName} AT > ${memoryName}

	.${memoryName}.data :
	{
		. = ALIGN(4);
		__${memoryName}_data_init_start = LOADADDR(.${memoryName}.data);
		PROVIDE(__${memoryName}_data_init_start = __${memoryName}_data_init_start);
		__${memoryName}_data_start = .;
		PROVIDE(__${memoryName}_data_start = __${memoryName}_data_start);

		*(.${memoryName}.data)

		. = ALIGN(4);
		__${memoryName}_data_end = .;
		PROVIDE(__${memoryName}_data_end = __${memoryName}_data_end);
	} > ${memoryName} AT > rom

	.${memoryName}.noinit :
	{
		. = ALIGN(4);
		__${memoryName}_noinit_start = .;
		PROVIDE(__${memoryName}_noinit_start = __${memoryName}_noinit_start);

		*(.${memoryName}.noinit)

		. = ALIGN(4);
		__${memoryName}_noinit_end = .;
		PROVIDE(__${memoryName}_noinit_end = __${memoryName}_noinit_end);
	} > ${memoryName} AT > ${memoryName}\n\n"

	sectionSizes="${sectionSizes}\
PROVIDE(__${memoryName}_bss_size = __${memoryName}_bss_end - __${memoryName}_bss_start);
{'fraction_non_alphanumeric': 0.2235377693582761, 'fraction_numerical': 0.01290551740468861, 'mean_word_length': 5.037884203002144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3304708', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3161, 'n_words': 571}