Some refactoring and cleanup (#1806)

* Use clang-tidy `misc-include-cleaner` for IWYU `#include` cleanup

* Use `std::optional<size_t>` instead of `ssize_t`

* Rename some functions in linkdefs.hpp

* Fix header order
This commit is contained in:
Rangi
2025-08-20 16:09:04 -04:00
committed by GitHub
parent 92ed6ece53
commit 3d155d5695
63 changed files with 271 additions and 118 deletions

View File

@@ -240,12 +240,12 @@ coverage:
# Target used in development to format source code with clang-format.
format:
$Qclang-format -i include/**/*.hpp src/**/*.cpp
$Qclang-format -i $$(git ls-files 'include/**/*.hpp' 'src/**/*.cpp')
# Target used in development to check code with clang-tidy.
# Requires Bison-generated header files to exist.
tidy: src/asm/parser.hpp src/link/script.hpp
$Qclang-tidy -p . $$(find src -name '*.cpp')
$Qclang-tidy -p . $$(git ls-files 'include/**/*.hpp' 'src/**/*.cpp')
# Target used in development to remove unused `#include` headers.
iwyu:

View File

@@ -4,14 +4,16 @@
#define RGBDS_ASM_ACTIONS_HPP
#include <optional>
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include "asm/output.hpp" // AssertionType
#include "asm/rpn.hpp" // RPNCommand
#include "linkdefs.hpp" // AssertionType, RPNCommand
#include "asm/rpn.hpp" // Expression
struct AlignmentSpec {
uint8_t alignment;

View File

@@ -4,6 +4,7 @@
#define RGBDS_ASM_CHARMAP_HPP
#include <optional>
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <string_view>

View File

@@ -6,6 +6,7 @@
#include <deque>
#include <memory>
#include <optional>
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <variant>

View File

@@ -7,8 +7,6 @@
#include <stdio.h>
#include <string>
#include "verbosity.hpp"
enum MissingInclude {
INC_ERROR, // A missing included file is an error that halts assembly
GEN_EXIT, // A missing included file is assumed to be generated; exit normally

View File

@@ -6,7 +6,6 @@
#include <memory>
#include <stdint.h>
#include <string>
#include <unordered_map>
#include <vector>
#include "linkdefs.hpp"

View File

@@ -6,9 +6,9 @@
#include <deque>
#include <memory>
#include <optional>
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <unordered_map>
#include <vector>
#include "linkdefs.hpp"

View File

@@ -5,9 +5,7 @@
#include <memory>
#include <stdint.h>
#include <string.h>
#include <string>
#include <string_view>
#include <time.h>
#include <utility>
#include <variant>

View File

@@ -9,8 +9,8 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#include "helpers.hpp"

View File

@@ -8,7 +8,6 @@
#include <ios>
#include <iostream>
#include <streambuf>
#include <string.h>
#include <string>
#include <variant>

View File

@@ -3,6 +3,8 @@
#ifndef RGBDS_FIX_WARNING_HPP
#define RGBDS_FIX_WARNING_HPP
#include <stdint.h>
#include "diagnostics.hpp"
enum WarningLevel {

View File

@@ -5,13 +5,12 @@
#include <array>
#include <optional>
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
#include "helpers.hpp"
#include "verbosity.hpp"
#include "gfx/rgba.hpp"

View File

@@ -5,6 +5,7 @@
#include <array>
#include <optional>
#include <stddef.h>
#include <vector>
#include "gfx/rgba.hpp"

View File

@@ -3,8 +3,8 @@
#ifndef RGBDS_GFX_PNG_HPP
#define RGBDS_GFX_PNG_HPP
#include <fstream>
#include <stdint.h>
#include <streambuf>
#include <vector>
#include "gfx/rgba.hpp"

View File

@@ -3,9 +3,6 @@
#ifndef RGBDS_LINK_SECTION_HPP
#define RGBDS_LINK_SECTION_HPP
// GUIDELINE: external code MUST NOT BE AWARE of the data structure used!
#include <deque>
#include <memory>
#include <stdint.h>
#include <string>
@@ -13,8 +10,6 @@
#include "linkdefs.hpp"
#include "link/main.hpp"
struct FileStackNode;
struct Section;
struct Symbol;

View File

@@ -101,18 +101,18 @@ extern struct SectionTypeInfo {
// Tells whether a section has data in its object file definition,
// depending on type.
static inline bool sect_HasData(SectionType type) {
static inline bool sectTypeHasData(SectionType type) {
assume(type != SECTTYPE_INVALID);
return type == SECTTYPE_ROM0 || type == SECTTYPE_ROMX;
}
// Returns a memory region's end address (last byte), e.g. 0x7FFF
static inline uint16_t endaddr(SectionType type) {
static inline uint16_t sectTypeEndAddr(SectionType type) {
return sectionTypeInfo[type].startAddr + sectionTypeInfo[type].size - 1;
}
// Returns a memory region's number of banks, or 1 for regions without banking
static inline uint32_t nbbanks(SectionType type) {
static inline uint32_t sectTypeBanks(SectionType type) {
return sectionTypeInfo[type].lastBank - sectionTypeInfo[type].firstBank + 1;
}

View File

@@ -2,16 +2,30 @@
#include "asm/actions.hpp"
#include <errno.h>
#include <inttypes.h>
#include <optional>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include "extern/utf8decoder.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "asm/charmap.hpp"
#include "asm/format.hpp"
#include "asm/fstack.hpp"
#include "asm/lexer.hpp"
#include "asm/output.hpp"
#include "asm/rpn.hpp" // Expression
#include "asm/section.hpp"
#include "asm/symbol.hpp"
#include "asm/warning.hpp"

View File

@@ -4,12 +4,17 @@
#include <deque>
#include <map>
#include <optional>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
#include "extern/utf8decoder.hpp"
#include "helpers.hpp"

View File

@@ -6,6 +6,7 @@
#include <math.h>
#include <numbers>
#include <stdint.h>
static constexpr double tau = std::numbers::pi * 2;

View File

@@ -5,11 +5,12 @@
#include <algorithm>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "asm/fixpoint.hpp"
#include "asm/main.hpp" // options
#include "asm/warning.hpp"

View File

@@ -7,15 +7,20 @@
#include <errno.h>
#include <inttypes.h>
#include <memory>
#include <optional>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "backtrace.hpp"
#include "diagnostics.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "platform.hpp" // S_ISDIR (stat macro)
#include "verbosity.hpp"
#include "asm/lexer.hpp"

View File

@@ -2,27 +2,34 @@
#include "asm/lexer.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <inttypes.h>
#include <ios>
#include <limits.h>
#include <math.h>
#include <memory>
#include <new> // nothrow
#include <optional>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
#include "helpers.hpp"
#include "platform.hpp"
#include "style.hpp"
#include "util.hpp"
#include "verbosity.hpp"
#include "asm/fixpoint.hpp"
#include "asm/format.hpp"
#include "asm/fstack.hpp"
#include "asm/macro.hpp"

View File

@@ -2,6 +2,8 @@
#include "asm/macro.hpp"
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>

View File

@@ -4,18 +4,25 @@
#include <algorithm>
#include <errno.h>
#include <limits.h>
#include <inttypes.h>
#include <memory>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>
#include "backtrace.hpp"
#include "diagnostics.hpp"
#include "extern/getopt.hpp"
#include "helpers.hpp"
#include "parser.hpp" // Generated from parser.y
#include "platform.hpp"
#include "style.hpp"
#include "usage.hpp"
#include "util.hpp" // UpperMap
@@ -26,6 +33,7 @@
#include "asm/fstack.hpp"
#include "asm/opt.hpp"
#include "asm/output.hpp"
#include "asm/section.hpp"
#include "asm/symbol.hpp"
#include "asm/warning.hpp"

View File

@@ -1,19 +1,20 @@
// SPDX-License-Identifier: MIT
#include <errno.h>
#include <iterator> // std::size
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "diagnostics.hpp"
#include "helpers.hpp" // assume
#include "util.hpp" // isBlankSpace
#include "asm/fixpoint.hpp"
#include "asm/fstack.hpp"
#include "asm/lexer.hpp"
#include "asm/main.hpp" // options
#include "asm/section.hpp"
#include "asm/warning.hpp"
struct OptStackEntry {

View File

@@ -6,13 +6,16 @@
#include <deque>
#include <errno.h>
#include <inttypes.h>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp" // assume, Defer
#include "linkdefs.hpp"
#include "platform.hpp"
#include "asm/charmap.hpp"
@@ -93,7 +96,7 @@ static void writeSection(Section const &sect, FILE *file) {
putc(sect.align, file);
putLong(sect.alignOfs, file);
if (sect_HasData(sect.type)) {
if (sectTypeHasData(sect.type)) {
fwrite(sect.data.data(), 1, sect.size, file);
putLong(sect.patches.size(), file);

View File

@@ -5,12 +5,16 @@
#include <inttypes.h>
#include <limits.h>
#include <optional>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <string_view>
#include <utility>
#include "helpers.hpp" // assume
#include "linkdefs.hpp"
#include "opmath.hpp"
#include "asm/output.hpp"

View File

@@ -3,15 +3,23 @@
#include "asm/section.hpp"
#include <algorithm>
#include <deque>
#include <errno.h>
#include <inttypes.h>
#include <iterator>
#include <optional>
#include <stack>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "asm/fstack.hpp"
#include "asm/lexer.hpp"
@@ -67,7 +75,7 @@ static bool requireCodeSection() {
return false;
}
if (sect_HasData(currentSection->type)) {
if (sectTypeHasData(currentSection->type)) {
return true;
}
@@ -128,7 +136,7 @@ static unsigned int mergeSectUnion(
// Unionized sections only need "compatible" constraints, and they end up with the strictest
// combination of both.
if (sect_HasData(type)) {
if (sectTypeHasData(type)) {
sectError("Cannot declare ROM sections as `UNION`");
}
@@ -333,7 +341,7 @@ static Section *createSection(
out_RegisterNode(sect.src);
// It is only needed to allocate memory for ROM sections.
if (sect_HasData(type)) {
if (sectTypeHasData(type)) {
sect.data.resize(sectionTypeInfo[type].size);
}
@@ -359,7 +367,7 @@ static Section *createSectionFragmentLiteral(Section const &parent) {
out_RegisterNode(sect.src);
// Section fragment literals must be ROM sections.
assume(sect_HasData(sect.type));
assume(sectTypeHasData(sect.type));
sect.data.resize(sectionTypeInfo[sect.type].size);
return &sect;
@@ -396,7 +404,7 @@ static Section *getSection(
sectionTypeInfo[type].lastBank
);
}
} else if (nbbanks(type) == 1) {
} else if (sectTypeBanks(type) == 1) {
// If the section type only has a single bank, implicitly force it
bank = sectionTypeInfo[type].firstBank;
}
@@ -411,14 +419,14 @@ static Section *getSection(
}
if (org != UINT32_MAX) {
if (org < sectionTypeInfo[type].startAddr || org > endaddr(type)) {
if (org < sectionTypeInfo[type].startAddr || org > sectTypeEndAddr(type)) {
error(
"Section \"%s\"'s fixed address $%04" PRIx32 " is outside of range [$%04" PRIx16
"; $%04" PRIx16 "]",
name.c_str(),
org,
sectionTypeInfo[type].startAddr,
endaddr(type)
sectTypeEndAddr(type)
);
}
}
@@ -539,7 +547,7 @@ void sect_SetLoadSection(
return;
}
if (sect_HasData(type)) {
if (sectTypeHasData(type)) {
error("`LOAD` blocks cannot create a ROM section");
return;
}
@@ -715,7 +723,7 @@ void sect_StartUnion() {
error("`UNION`s must be inside a `SECTION`");
return;
}
if (sect_HasData(currentSection->type)) {
if (sectTypeHasData(currentSection->type)) {
error("Cannot use `UNION` inside of `ROM0` or `ROMX` sections");
return;
}
@@ -812,7 +820,7 @@ void sect_Skip(uint32_t skip, bool ds) {
return;
}
if (!sect_HasData(currentSection->type)) {
if (!sectTypeHasData(currentSection->type)) {
growSection(skip);
} else {
if (!ds) {
@@ -1110,7 +1118,7 @@ std::string sect_PushSectionFragmentLiteral() {
if (!currentSection) {
fatal("Cannot output fragment literals outside of a `SECTION`");
}
if (!sect_HasData(currentSection->type)) {
if (!sectTypeHasData(currentSection->type)) {
fatal(
"Section \"%s\" cannot contain fragment literals (not `ROM0` or `ROMX`)",
currentSection->name.c_str()

View File

@@ -5,9 +5,16 @@
#include <algorithm>
#include <errno.h>
#include <inttypes.h>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include "diagnostics.hpp"
#include "helpers.hpp" // assume
@@ -19,6 +26,7 @@
#include "asm/macro.hpp"
#include "asm/main.hpp"
#include "asm/output.hpp"
#include "asm/section.hpp"
#include "asm/warning.hpp"
using namespace std::literals;

View File

@@ -2,20 +2,16 @@
#include "asm/warning.hpp"
#include <functional>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "diagnostics.hpp"
#include "helpers.hpp"
#include "itertools.hpp"
#include "style.hpp"
#include "asm/fstack.hpp"
#include "asm/lexer.hpp"
#include "asm/main.hpp"
// clang-format off: nested initializers

View File

@@ -2,6 +2,14 @@
#include "diagnostics.hpp"
#include <optional>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <utility>
#include "helpers.hpp"
#include "style.hpp"
#include "util.hpp" // isDigit

View File

@@ -6,6 +6,8 @@
#include "extern/utf8decoder.hpp"
#include <stdint.h>
// clang-format off: vertically align values
static uint8_t const utf8d[] = {
// The first part of the table maps bytes to character classes that

View File

@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <limits.h>

View File

@@ -2,6 +2,8 @@
#include "fix/mbc.hpp"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_map>
#include <utility>

View File

@@ -2,6 +2,13 @@
#include "fix/warning.hpp"
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "diagnostics.hpp"
#include "style.hpp"
// clang-format off: nested initializers

View File

@@ -3,6 +3,10 @@
#include "gfx/color_set.hpp"
#include <algorithm>
#include <iterator>
#include <stdint.h>
#include <stdlib.h>
#include <utility>
#include "helpers.hpp"

View File

@@ -4,10 +4,11 @@
#include <algorithm>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <ios>
#include <limits>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -17,6 +18,7 @@
#include "diagnostics.hpp"
#include "extern/getopt.hpp"
#include "file.hpp"
#include "helpers.hpp"
#include "platform.hpp"
#include "style.hpp"
#include "usage.hpp"
@@ -27,6 +29,7 @@
#include "gfx/pal_spec.hpp"
#include "gfx/process.hpp"
#include "gfx/reverse.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"
using namespace std::literals::string_view_literals;

View File

@@ -5,15 +5,19 @@
#include <algorithm>
#include <deque>
#include <inttypes.h>
#include <iterator>
#include <numeric>
#include <optional>
#include <queue>
#include <stdint.h>
#include <stdio.h>
#include <type_traits>
#include <unordered_set>
#include <utility>
#include <vector>
#include "helpers.hpp"
#include "style.hpp"
#include "verbosity.hpp"
#include "gfx/color_set.hpp"

View File

@@ -3,11 +3,16 @@
#include "gfx/pal_sorting.hpp"
#include <algorithm>
#include <array>
#include <optional>
#include <stdint.h>
#include <vector>
#include "helpers.hpp"
#include "verbosity.hpp"
#include "gfx/main.hpp"
#include "gfx/rgba.hpp"
void sortIndexed(std::vector<Palette> &palettes, std::vector<Rgba> const &embPal) {
verbosePrint(VERB_NOTICE, "Sorting palettes using embedded palette...\n");

View File

@@ -3,19 +3,20 @@
#include "gfx/pal_spec.hpp"
#include <algorithm>
#include <array>
#include <charconv>
#include <errno.h>
#include <fstream>
#include <inttypes.h>
#include <limits.h>
#include <ios>
#include <optional>
#include <stdint.h>
#include <stdio.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp"
@@ -24,6 +25,7 @@
#include "gfx/main.hpp"
#include "gfx/png.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"
using namespace std::string_view_literals;

View File

@@ -2,11 +2,23 @@
#include "gfx/png.hpp"
#include <array>
#include <errno.h>
#include <inttypes.h>
#include <ios>
#include <png.h>
#include <pngconf.h>
#include <stdint.h>
#include <stdio.h>
#include <streambuf>
#include <string.h>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp"
#include "style.hpp"
#include "verbosity.hpp"
#include "gfx/main.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"

View File

@@ -3,13 +3,17 @@
#include "gfx/process.hpp"
#include <algorithm>
#include <array>
#include <errno.h>
#include <inttypes.h>
#include <ios>
#include <optional>
#include <png.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>
@@ -18,6 +22,7 @@
#include "file.hpp"
#include "helpers.hpp"
#include "itertools.hpp"
#include "style.hpp"
#include "verbosity.hpp"
#include "gfx/color_set.hpp"
@@ -25,6 +30,7 @@
#include "gfx/pal_packing.hpp"
#include "gfx/pal_sorting.hpp"
#include "gfx/png.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"
static bool isBgColorTransparent() {
@@ -610,7 +616,7 @@ public:
template<>
struct std::hash<TileData> {
std::size_t operator()(TileData const &tile) const { return tile.hash(); }
size_t operator()(TileData const &tile) const { return tile.hash(); }
};
static void outputUnoptimizedTileData(

View File

@@ -6,10 +6,16 @@
#include <array>
#include <errno.h>
#include <inttypes.h>
#include <ios>
#include <math.h>
#include <optional>
#include <png.h>
#include <pngconf.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#include "diagnostics.hpp"
@@ -18,6 +24,7 @@
#include "verbosity.hpp"
#include "gfx/main.hpp"
#include "gfx/rgba.hpp"
#include "gfx/warning.hpp"
static std::vector<uint8_t> readInto(std::string const &path) {

View File

@@ -3,6 +3,7 @@
#include "gfx/rgba.hpp"
#include <algorithm>
#include <array>
#include <math.h>
#include <stdint.h>

View File

@@ -2,11 +2,12 @@
#include "gfx/warning.hpp"
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "diagnostics.hpp"
#include "style.hpp"
// clang-format off: nested initializers

View File

@@ -4,16 +4,17 @@
#include <deque>
#include <inttypes.h>
#include <optional>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp"
#include "itertools.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
#include "verbosity.hpp"
#include "link/main.hpp"
@@ -38,7 +39,7 @@ static std::vector<std::deque<FreeSpace>> memory[SECTTYPE_INVALID];
// Init the free space-modelling structs
static void initFreeSpace() {
for (SectionType type : EnumSeq(SECTTYPE_INVALID)) {
memory[type].resize(nbbanks(type));
memory[type].resize(sectTypeBanks(type));
for (std::deque<FreeSpace> &bankMem : memory[type]) {
bankMem.push_back({
.address = sectionTypeInfo[type].startAddr,
@@ -114,8 +115,8 @@ static MemoryLocation getStartLocation(Section const &section) {
}
// Returns a suitable free space index into `memory[section->type]` at which to place the given
// section, or -1 if none was found.
static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
// section, or `std::nullopt` if none was found.
static std::optional<size_t> getPlacement(Section const &section, MemoryLocation &location) {
SectionTypeInfo const &typeInfo = sectionTypeInfo[section.type];
// Switch to the beginning of the next bank
@@ -169,7 +170,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
// Try scrambled banks in descending order until no bank in the scrambled range is
// available. Otherwise, try in ascending order.
if (section.isBankFixed) {
return -1;
return std::nullopt;
} else if (options.scrambleROMX && section.type == SECTTYPE_ROMX
&& location.bank <= options.scrambleROMX) {
if (location.bank > typeInfo.firstBank) {
@@ -177,7 +178,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
} else if (options.scrambleROMX < typeInfo.lastBank) {
location.bank = options.scrambleROMX + 1;
} else {
return -1;
return std::nullopt;
}
} else if (options.scrambleWRAMX && section.type == SECTTYPE_WRAMX
&& location.bank <= options.scrambleWRAMX) {
@@ -186,7 +187,7 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
} else if (options.scrambleWRAMX < typeInfo.lastBank) {
location.bank = options.scrambleWRAMX + 1;
} else {
return -1;
return std::nullopt;
}
} else if (options.scrambleSRAM && section.type == SECTTYPE_SRAM
&& location.bank <= options.scrambleSRAM) {
@@ -195,12 +196,12 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
} else if (options.scrambleSRAM < typeInfo.lastBank) {
location.bank = options.scrambleSRAM + 1;
} else {
return -1;
return std::nullopt;
}
} else if (location.bank < typeInfo.lastBank) {
++location.bank;
} else {
return -1;
return std::nullopt;
}
return getPlacement(section, location); // Tail recursion
@@ -210,7 +211,7 @@ static std::string getSectionDescription(Section const &section) {
std::string where;
char bank[8], addr[8], mask[8], offset[8];
if (section.isBankFixed && nbbanks(section.type) != 1) {
if (section.isBankFixed && sectTypeBanks(section.type) != 1) {
snprintf(bank, sizeof(bank), "%02" PRIx32, section.bank);
}
if (section.isAddressFixed) {
@@ -221,7 +222,7 @@ static std::string getSectionDescription(Section const &section) {
snprintf(offset, sizeof(offset), "%" PRIx16, section.alignOfs);
}
if (section.isBankFixed && nbbanks(section.type) != 1) {
if (section.isBankFixed && sectTypeBanks(section.type) != 1) {
if (section.isAddressFixed) {
where = "at $";
where += bank;
@@ -272,10 +273,10 @@ static void placeSection(Section &section) {
// Place section using first-fit decreasing algorithm
// https://en.wikipedia.org/wiki/Bin_packing_problem#First-fit_algorithm
MemoryLocation location = getStartLocation(section);
if (ssize_t spaceIdx = getPlacement(section, location); spaceIdx != -1) {
if (std::optional<size_t> spaceIdx = getPlacement(section, location); spaceIdx) {
std::deque<FreeSpace> &bankMem =
memory[section.type][location.bank - sectionTypeInfo[section.type].firstBank];
FreeSpace &freeSpace = bankMem[spaceIdx];
FreeSpace &freeSpace = bankMem[*spaceIdx];
assignSection(section, location);
@@ -285,17 +286,17 @@ static void placeSection(Section &section) {
bool noRightSpace = freeSpace.address + freeSpace.size == sectionEnd;
if (noLeftSpace && noRightSpace) {
// The free space is entirely deleted
bankMem.erase(bankMem.begin() + spaceIdx);
bankMem.erase(bankMem.begin() + *spaceIdx);
} else if (!noLeftSpace && !noRightSpace) {
// The free space is split in two
// Append the new space after the original one
uint16_t size = static_cast<uint16_t>(freeSpace.address + freeSpace.size - sectionEnd);
bankMem.insert(bankMem.begin() + spaceIdx + 1, {.address = sectionEnd, .size = size});
bankMem.insert(bankMem.begin() + *spaceIdx + 1, {.address = sectionEnd, .size = size});
// **`freeSpace` cannot be reused from this point on, because `bankMem.insert`
// invalidates all references to itself!**
// Resize the original space (address is unmodified)
bankMem[spaceIdx].size = section.org - bankMem[spaceIdx].address;
bankMem[*spaceIdx].size = section.org - bankMem[*spaceIdx].address;
} else {
// The amount of free spaces doesn't change: resize!
freeSpace.size -= section.size;
@@ -315,7 +316,7 @@ static void placeSection(Section &section) {
sectionTypeInfo[section.type].name.c_str(),
getSectionDescription(section).c_str()
);
} else if (section.org + section.size > endaddr(section.type) + 1) {
} else if (section.org + section.size > sectTypeEndAddr(section.type) + 1) {
// If the section just can't fit the bank, report that
fatal(
"Unable to place \"%s\" (%s section) %s: section runs past end of region ($%04x > "
@@ -324,7 +325,7 @@ static void placeSection(Section &section) {
sectionTypeInfo[section.type].name.c_str(),
getSectionDescription(section).c_str(),
section.org + section.size,
endaddr(section.type) + 1
sectTypeEndAddr(section.type) + 1
);
} else {
// Otherwise there is overlap with another section

View File

@@ -1,8 +1,14 @@
#include "link/fstack.hpp"
#include <inttypes.h>
#include <stdint.h>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "backtrace.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "link/warning.hpp"

View File

@@ -5,10 +5,11 @@
#include <array>
#include <bit>
#include <inttypes.h>
#include <stdint.h>
#include <vector>
#include "helpers.hpp"
#include "util.hpp"
#include "linkdefs.hpp"
#include "link/section.hpp"
#include "link/warning.hpp"
@@ -30,7 +31,7 @@ static void setActiveTypeAndIdx(SectionType type, uint32_t idx) {
}
void layout_SetFloatingSectionType(SectionType type) {
if (nbbanks(type) == 1) {
if (sectTypeBanks(type) == 1) {
// There is only a single bank anyway, so just set the index to 0.
setActiveTypeAndIdx(type, 0);
} else {
@@ -45,7 +46,7 @@ void layout_SetFloatingSectionType(SectionType type) {
}
void layout_SetSectionType(SectionType type) {
if (nbbanks(type) != 1) {
if (sectTypeBanks(type) != 1) {
scriptError("A bank number must be specified for %s", sectionTypeInfo[type].name.c_str());
// Keep going with a default value for the bank index.
}
@@ -91,14 +92,14 @@ void layout_SetAddr(uint32_t addr) {
if (addr < pc) {
scriptError("Cannot decrease the current address (from $%04x to $%04x)", pc, addr);
} else if (addr > endaddr(activeType)) { // Allow "one past the end" sections.
} else if (addr > sectTypeEndAddr(activeType)) { // Allow "one past the end" sections.
scriptError(
"Cannot set the current address to $%04" PRIx32 ": %s ends at $%04" PRIx16,
addr,
typeInfo.name.c_str(),
endaddr(activeType)
sectTypeEndAddr(activeType)
);
pc = endaddr(activeType);
pc = sectTypeEndAddr(activeType);
} else {
pc = addr;
}
@@ -178,7 +179,7 @@ void layout_AlignTo(uint32_t alignment, uint32_t alignOfs) {
", past $%04" PRIx16,
pc,
static_cast<uint16_t>(pc + length),
static_cast<uint16_t>(endaddr(activeType) + 1)
static_cast<uint16_t>(sectTypeEndAddr(activeType) + 1)
);
return;
}
@@ -206,7 +207,7 @@ void layout_Pad(uint32_t length) {
"Cannot increase the current address by %u bytes: only %u bytes to $%04" PRIx16,
length,
typeInfo.size - offset,
static_cast<uint16_t>(endaddr(activeType) + 1)
static_cast<uint16_t>(sectTypeEndAddr(activeType) + 1)
);
} else {
pc += length;
@@ -232,13 +233,13 @@ void layout_PlaceSection(std::string const &name, bool isOptional) {
// Check that the linker script doesn't contradict what the code says.
if (section->type == SECTTYPE_INVALID) {
// A section that has data must get assigned a type that requires data.
if (!sect_HasData(activeType) && !section->data.empty()) {
if (!sectTypeHasData(activeType) && !section->data.empty()) {
scriptError(
"\"%s\" is specified to be a %s section, but it contains data",
name.c_str(),
typeInfo.name.c_str()
);
} else if (sect_HasData(activeType) && section->data.empty() && section->size != 0) {
} else if (sectTypeHasData(activeType) && section->data.empty() && section->size != 0) {
// A section that lacks data can only be assigned to a type that requires data
// if it's empty.
scriptError(

View File

@@ -2,16 +2,19 @@
#include "link/lexer.hpp"
#include <array>
#include <errno.h>
#include <fstream>
#include <inttypes.h>
#include <ios>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#include "backtrace.hpp"
#include "helpers.hpp"
#include "itertools.hpp"
#include "linkdefs.hpp"
#include "util.hpp"
#include "link/warning.hpp"

View File

@@ -1,11 +1,12 @@
// SPDX-License-Identifier: MIT
#include <sys/stat.h>
#include <sys/types.h>
#include "link/main.hpp"
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
@@ -13,9 +14,7 @@
#include "backtrace.hpp"
#include "diagnostics.hpp"
#include "extern/getopt.hpp"
#include "helpers.hpp" // assume
#include "itertools.hpp"
#include "platform.hpp"
#include "linkdefs.hpp"
#include "script.hpp" // Generated from script.y
#include "style.hpp"
#include "usage.hpp"
@@ -29,7 +28,6 @@
#include "link/output.hpp"
#include "link/patch.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "link/warning.hpp"
Options options;

View File

@@ -11,18 +11,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "platform.hpp"
#include "verbosity.hpp"
#include "version.hpp"
#include "link/assign.hpp"
#include "link/fstack.hpp"
#include "link/main.hpp"
#include "link/patch.hpp"
#include "link/sdas_obj.hpp"
#include "link/section.hpp"
@@ -360,7 +360,7 @@ static void readSection(
}
section.alignOfs = tmp;
if (sect_HasData(section.type)) {
if (sectTypeHasData(section.type)) {
if (section.size) {
section.data.resize(section.size);
if (fread(section.data.data(), 1, section.size, file) != section.size) {
@@ -549,7 +549,7 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) {
// Give patches' PC section pointers to their sections
for (uint32_t i = 0; i < nbSections; ++i) {
if (sect_HasData(fileSections[i]->type)) {
if (sectTypeHasData(fileSections[i]->type)) {
for (Patch &patch : fileSections[i]->patches) {
linkPatchToPCSect(patch, fileSections);
}

View File

@@ -6,9 +6,12 @@
#include <deque>
#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tuple>
#include <variant>
#include <vector>
#include "diagnostics.hpp"

View File

@@ -5,14 +5,15 @@
#include <deque>
#include <inttypes.h>
#include <stdint.h>
#include <variant>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp" // assume
#include "linkdefs.hpp"
#include "opmath.hpp"
#include "verbosity.hpp"
#include "link/main.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "link/warning.hpp"
@@ -566,7 +567,7 @@ static void applyFilePatches(Section &section, Section &dataSection) {
// Applies all of a section's patches, iterating over "components" of unionized sections
static void applyPatches(Section &section) {
if (!sect_HasData(section.type)) {
if (!sectTypeHasData(section.type)) {
return;
}

View File

@@ -6,16 +6,17 @@
#include <inttypes.h>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <variant>
#include <vector>
#include "helpers.hpp" // assume, literal_strlen
#include "linkdefs.hpp"
#include "platform.hpp"
#include "link/assign.hpp"
#include "link/fstack.hpp"
#include "link/main.hpp"
#include "link/section.hpp"
#include "link/symbol.hpp"
#include "link/warning.hpp"
@@ -865,7 +866,7 @@ void sdobj_ReadFile(FileStackNode const &src, FILE *file, std::vector<Symbol> &f
// Otherwise, how would the type already be known at this point?
assume(section->isAddressFixed);
if (!sect_HasData(section->type)) {
if (!sectTypeHasData(section->type)) {
if (!section->data.empty()) {
fatalAt(
where,

View File

@@ -3,13 +3,19 @@
#include "link/section.hpp"
#include <inttypes.h>
#include <memory>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "diagnostics.hpp"
#include "helpers.hpp"
#include "linkdefs.hpp"
#include "link/main.hpp"
#include "link/warning.hpp"
static std::vector<std::unique_ptr<Section>> sectionList;
@@ -167,7 +173,7 @@ static void mergeSections(Section &target, std::unique_ptr<Section> &&other) {
// Append `other` to `target`
other->offset = target.size;
target.size += other->size;
// Normally we'd check that `sect_HasData`, but SDCC areas may be `_INVALID` here
// Normally we'd check that `sectTypeHasData`, but SDCC areas may be `_INVALID` here
if (!other->data.empty()) {
target.data.insert(target.data.end(), RANGE(other->data));
// Adjust patches' PC offsets
@@ -195,7 +201,7 @@ void sect_AddSection(std::unique_ptr<Section> &&section) {
// Check if the section already exists
if (Section *target = sect_GetSection(section->name); target) {
mergeSections(*target, std::move(section));
} else if (section->modifier == SECTION_UNION && sect_HasData(section->type)) {
} else if (section->modifier == SECTION_UNION && sectTypeHasData(section->type)) {
fatal(
"Section \"%s\" is of type `%s`, which cannot be `UNION`ized",
section->name.c_str(),
@@ -315,23 +321,23 @@ static void doSanityChecks(Section &section) {
// Ensure the target address is valid
if (section.org < sectionTypeInfo[section.type].startAddr
|| section.org > endaddr(section.type)) {
|| section.org > sectTypeEndAddr(section.type)) {
error(
"Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04" PRIx16
"; $%04" PRIx16 "]",
section.name.c_str(),
section.org,
sectionTypeInfo[section.type].startAddr,
endaddr(section.type)
sectTypeEndAddr(section.type)
);
}
if (section.org + section.size > endaddr(section.type) + 1) {
if (section.org + section.size > sectTypeEndAddr(section.type) + 1) {
error(
"Section \"%s\"'s end address $%04x is greater than last address $%04x",
section.name.c_str(),
section.org + section.size,
endaddr(section.type) + 1
sectTypeEndAddr(section.type) + 1
);
}
}

View File

@@ -2,12 +2,17 @@
#include "link/symbol.hpp"
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
#include "helpers.hpp" // assume
#include "linkdefs.hpp"
#include "link/fstack.hpp"
#include "link/section.hpp"

View File

@@ -4,7 +4,11 @@
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "diagnostics.hpp"
#include "style.hpp"
#include "link/fstack.hpp"

View File

@@ -2,6 +2,8 @@
#include "linkdefs.hpp"
#include <string>
using namespace std::literals;
// The default values are the most lax, as they are used as-is by RGBASM; only RGBLINK has the full

View File

@@ -5,6 +5,7 @@
#include "style.hpp"
#include <stdio.h>
#include <stdlib.h> // getenv
#include <string.h>

View File

@@ -2,7 +2,7 @@
#include "usage.hpp"
#include <algorithm>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -8,6 +8,8 @@
#include <stdint.h>
#include <stdio.h>
#include "style.hpp"
static Verbosity verbosity = VERB_NONE;
bool checkVerbosity(Verbosity level) {

View File

@@ -2,8 +2,6 @@
#include "version.hpp"
#include <string.h>
#include "helpers.hpp"
// We do not build `make develop` with `-fsanitize=leak` because macOS clang++ does not support it.

View File

@@ -2,10 +2,10 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <cinttypes>
#include <assert.h>
#include <fcntl.h>
#include <fstream>
#include <inttypes.h>
#include <limits>
#include <png.h>
#include <stdarg.h>